18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Mellanox register access driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Mellanox Technologies
68c2ecf20Sopenharmony_ci * Copyright (C) 2018 Vadim Pasternak <vadimp@mellanox.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/bitops.h>
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
128c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of_device.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_data/mlxreg.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Attribute parameters. */
208c2ecf20Sopenharmony_ci#define MLXREG_IO_ATT_SIZE	10
218c2ecf20Sopenharmony_ci#define MLXREG_IO_ATT_NUM	48
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/**
248c2ecf20Sopenharmony_ci * struct mlxreg_io_priv_data - driver's private data:
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * @pdev: platform device;
278c2ecf20Sopenharmony_ci * @pdata: platform data;
288c2ecf20Sopenharmony_ci * @hwmon: hwmon device;
298c2ecf20Sopenharmony_ci * @mlxreg_io_attr: sysfs attributes array;
308c2ecf20Sopenharmony_ci * @mlxreg_io_dev_attr: sysfs sensor device attribute array;
318c2ecf20Sopenharmony_ci * @group: sysfs attribute group;
328c2ecf20Sopenharmony_ci * @groups: list of sysfs attribute group for hwmon registration;
338c2ecf20Sopenharmony_ci * @regsize: size of a register value;
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistruct mlxreg_io_priv_data {
368c2ecf20Sopenharmony_ci	struct platform_device *pdev;
378c2ecf20Sopenharmony_ci	struct mlxreg_core_platform_data *pdata;
388c2ecf20Sopenharmony_ci	struct device *hwmon;
398c2ecf20Sopenharmony_ci	struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1];
408c2ecf20Sopenharmony_ci	struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM];
418c2ecf20Sopenharmony_ci	struct attribute_group group;
428c2ecf20Sopenharmony_ci	const struct attribute_group *groups[2];
438c2ecf20Sopenharmony_ci	int regsize;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int
478c2ecf20Sopenharmony_cimlxreg_io_get_reg(void *regmap, struct mlxreg_core_data *data, u32 in_val,
488c2ecf20Sopenharmony_ci		  bool rw_flag, int regsize, u32 *regval)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	int i, val, ret;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	ret = regmap_read(regmap, data->reg, regval);
538c2ecf20Sopenharmony_ci	if (ret)
548c2ecf20Sopenharmony_ci		goto access_error;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/*
578c2ecf20Sopenharmony_ci	 * There are four kinds of attributes: single bit, full register's
588c2ecf20Sopenharmony_ci	 * bits, bit sequence, bits in few registers For the first kind field
598c2ecf20Sopenharmony_ci	 * mask indicates which bits are not related and field bit is set zero.
608c2ecf20Sopenharmony_ci	 * For the second kind field mask is set to zero and field bit is set
618c2ecf20Sopenharmony_ci	 * with all bits one. No special handling for such kind of attributes -
628c2ecf20Sopenharmony_ci	 * pass value as is. For the third kind, the field mask indicates which
638c2ecf20Sopenharmony_ci	 * bits are related and the field bit is set to the first bit number
648c2ecf20Sopenharmony_ci	 * (from 1 to 32) is the bit sequence. For the fourth kind - the number
658c2ecf20Sopenharmony_ci	 * of registers which should be read for getting an attribute are
668c2ecf20Sopenharmony_ci	 * specified through 'data->regnum' field.
678c2ecf20Sopenharmony_ci	 */
688c2ecf20Sopenharmony_ci	if (!data->bit) {
698c2ecf20Sopenharmony_ci		/* Single bit. */
708c2ecf20Sopenharmony_ci		if (rw_flag) {
718c2ecf20Sopenharmony_ci			/* For show: expose effective bit value as 0 or 1. */
728c2ecf20Sopenharmony_ci			*regval = !!(*regval & ~data->mask);
738c2ecf20Sopenharmony_ci		} else {
748c2ecf20Sopenharmony_ci			/* For store: set effective bit value. */
758c2ecf20Sopenharmony_ci			*regval &= data->mask;
768c2ecf20Sopenharmony_ci			if (in_val)
778c2ecf20Sopenharmony_ci				*regval |= ~data->mask;
788c2ecf20Sopenharmony_ci		}
798c2ecf20Sopenharmony_ci	} else if (data->mask) {
808c2ecf20Sopenharmony_ci		/* Bit sequence. */
818c2ecf20Sopenharmony_ci		if (rw_flag) {
828c2ecf20Sopenharmony_ci			/* For show: mask and shift right. */
838c2ecf20Sopenharmony_ci			*regval = ror32(*regval & data->mask, (data->bit - 1));
848c2ecf20Sopenharmony_ci		} else {
858c2ecf20Sopenharmony_ci			/* For store: shift to the position and mask. */
868c2ecf20Sopenharmony_ci			in_val = rol32(in_val, data->bit - 1) & data->mask;
878c2ecf20Sopenharmony_ci			/* Clear relevant bits and set them to new value. */
888c2ecf20Sopenharmony_ci			*regval = (*regval & ~data->mask) | in_val;
898c2ecf20Sopenharmony_ci		}
908c2ecf20Sopenharmony_ci	} else {
918c2ecf20Sopenharmony_ci		/*
928c2ecf20Sopenharmony_ci		 * Some attributes could occupied few registers in case regmap
938c2ecf20Sopenharmony_ci		 * bit size is 8 or 16. Compose such attributes from 'regnum'
948c2ecf20Sopenharmony_ci		 * registers. Such attributes contain read-only data.
958c2ecf20Sopenharmony_ci		 */
968c2ecf20Sopenharmony_ci		for (i = 1; i < data->regnum; i++) {
978c2ecf20Sopenharmony_ci			ret = regmap_read(regmap, data->reg + i, &val);
988c2ecf20Sopenharmony_ci			if (ret)
998c2ecf20Sopenharmony_ci				goto access_error;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci			*regval |= rol32(val, regsize * i * 8);
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciaccess_error:
1068c2ecf20Sopenharmony_ci	return ret;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic ssize_t
1108c2ecf20Sopenharmony_cimlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
1118c2ecf20Sopenharmony_ci		    char *buf)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
1148c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1158c2ecf20Sopenharmony_ci	struct mlxreg_core_data *data = priv->pdata->data + index;
1168c2ecf20Sopenharmony_ci	u32 regval = 0;
1178c2ecf20Sopenharmony_ci	int ret;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ret = mlxreg_io_get_reg(priv->pdata->regmap, data, 0, true,
1208c2ecf20Sopenharmony_ci				priv->regsize, &regval);
1218c2ecf20Sopenharmony_ci	if (ret)
1228c2ecf20Sopenharmony_ci		goto access_error;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", regval);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciaccess_error:
1278c2ecf20Sopenharmony_ci	return ret;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic ssize_t
1318c2ecf20Sopenharmony_cimlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
1328c2ecf20Sopenharmony_ci		     const char *buf, size_t len)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
1358c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1368c2ecf20Sopenharmony_ci	struct mlxreg_core_data *data = priv->pdata->data + index;
1378c2ecf20Sopenharmony_ci	u32 input_val, regval;
1388c2ecf20Sopenharmony_ci	int ret;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (len > MLXREG_IO_ATT_SIZE)
1418c2ecf20Sopenharmony_ci		return -EINVAL;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* Convert buffer to input value. */
1448c2ecf20Sopenharmony_ci	ret = kstrtou32(buf, 0, &input_val);
1458c2ecf20Sopenharmony_ci	if (ret)
1468c2ecf20Sopenharmony_ci		return ret;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	ret = mlxreg_io_get_reg(priv->pdata->regmap, data, input_val, false,
1498c2ecf20Sopenharmony_ci				priv->regsize, &regval);
1508c2ecf20Sopenharmony_ci	if (ret)
1518c2ecf20Sopenharmony_ci		goto access_error;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	ret = regmap_write(priv->pdata->regmap, data->reg, regval);
1548c2ecf20Sopenharmony_ci	if (ret)
1558c2ecf20Sopenharmony_ci		goto access_error;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return len;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciaccess_error:
1608c2ecf20Sopenharmony_ci	dev_err(&priv->pdev->dev, "Bus access error\n");
1618c2ecf20Sopenharmony_ci	return ret;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic struct device_attribute mlxreg_io_devattr_rw = {
1658c2ecf20Sopenharmony_ci	.show	= mlxreg_io_attr_show,
1668c2ecf20Sopenharmony_ci	.store	= mlxreg_io_attr_store,
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	int i;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
1748c2ecf20Sopenharmony_ci					 priv->pdata->counter,
1758c2ecf20Sopenharmony_ci					 sizeof(struct attribute *),
1768c2ecf20Sopenharmony_ci					 GFP_KERNEL);
1778c2ecf20Sopenharmony_ci	if (!priv->group.attrs)
1788c2ecf20Sopenharmony_ci		return -ENOMEM;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	for (i = 0; i < priv->pdata->counter; i++) {
1818c2ecf20Sopenharmony_ci		priv->mlxreg_io_attr[i] =
1828c2ecf20Sopenharmony_ci				&priv->mlxreg_io_dev_attr[i].dev_attr.attr;
1838c2ecf20Sopenharmony_ci		memcpy(&priv->mlxreg_io_dev_attr[i].dev_attr,
1848c2ecf20Sopenharmony_ci		       &mlxreg_io_devattr_rw, sizeof(struct device_attribute));
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci		/* Set attribute name as a label. */
1878c2ecf20Sopenharmony_ci		priv->mlxreg_io_attr[i]->name =
1888c2ecf20Sopenharmony_ci				devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
1898c2ecf20Sopenharmony_ci					       priv->pdata->data[i].label);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		if (!priv->mlxreg_io_attr[i]->name) {
1928c2ecf20Sopenharmony_ci			dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n",
1938c2ecf20Sopenharmony_ci				i + 1);
1948c2ecf20Sopenharmony_ci			return -ENOMEM;
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci		priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode =
1988c2ecf20Sopenharmony_ci						priv->pdata->data[i].mode;
1998c2ecf20Sopenharmony_ci		priv->mlxreg_io_dev_attr[i].dev_attr.attr.name =
2008c2ecf20Sopenharmony_ci					priv->mlxreg_io_attr[i]->name;
2018c2ecf20Sopenharmony_ci		priv->mlxreg_io_dev_attr[i].index = i;
2028c2ecf20Sopenharmony_ci		sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr);
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	priv->group.attrs = priv->mlxreg_io_attr;
2068c2ecf20Sopenharmony_ci	priv->groups[0] = &priv->group;
2078c2ecf20Sopenharmony_ci	priv->groups[1] = NULL;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int mlxreg_io_probe(struct platform_device *pdev)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct mlxreg_io_priv_data *priv;
2158c2ecf20Sopenharmony_ci	int err;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2188c2ecf20Sopenharmony_ci	if (!priv)
2198c2ecf20Sopenharmony_ci		return -ENOMEM;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	priv->pdata = dev_get_platdata(&pdev->dev);
2228c2ecf20Sopenharmony_ci	if (!priv->pdata) {
2238c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get platform data.\n");
2248c2ecf20Sopenharmony_ci		return -EINVAL;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	priv->pdev = pdev;
2288c2ecf20Sopenharmony_ci	priv->regsize = regmap_get_val_bytes(priv->pdata->regmap);
2298c2ecf20Sopenharmony_ci	if (priv->regsize < 0)
2308c2ecf20Sopenharmony_ci		return priv->regsize;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	err = mlxreg_io_attr_init(priv);
2338c2ecf20Sopenharmony_ci	if (err) {
2348c2ecf20Sopenharmony_ci		dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n",
2358c2ecf20Sopenharmony_ci			err);
2368c2ecf20Sopenharmony_ci		return err;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
2408c2ecf20Sopenharmony_ci							     "mlxreg_io",
2418c2ecf20Sopenharmony_ci							      priv,
2428c2ecf20Sopenharmony_ci							      priv->groups);
2438c2ecf20Sopenharmony_ci	if (IS_ERR(priv->hwmon)) {
2448c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
2458c2ecf20Sopenharmony_ci			PTR_ERR(priv->hwmon));
2468c2ecf20Sopenharmony_ci		return PTR_ERR(priv->hwmon);
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, priv);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic struct platform_driver mlxreg_io_driver = {
2558c2ecf20Sopenharmony_ci	.driver = {
2568c2ecf20Sopenharmony_ci	    .name = "mlxreg-io",
2578c2ecf20Sopenharmony_ci	},
2588c2ecf20Sopenharmony_ci	.probe = mlxreg_io_probe,
2598c2ecf20Sopenharmony_ci};
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cimodule_platform_driver(mlxreg_io_driver);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
2648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Mellanox regmap I/O access driver");
2658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2668c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:mlxreg-io");
267