mlxreg-io.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mellanox register access driver
  4. *
  5. * Copyright (C) 2018 Mellanox Technologies
  6. * Copyright (C) 2018 Vadim Pasternak <vadimp@mellanox.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/device.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/hwmon-sysfs.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_data/mlxreg.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. /* Attribute parameters. */
  17. #define MLXREG_IO_ATT_SIZE 10
  18. #define MLXREG_IO_ATT_NUM 96
  19. /**
  20. * struct mlxreg_io_priv_data - driver's private data:
  21. *
  22. * @pdev: platform device;
  23. * @pdata: platform data;
  24. * @hwmon: hwmon device;
  25. * @mlxreg_io_attr: sysfs attributes array;
  26. * @mlxreg_io_dev_attr: sysfs sensor device attribute array;
  27. * @group: sysfs attribute group;
  28. * @groups: list of sysfs attribute group for hwmon registration;
  29. * @regsize: size of a register value;
  30. * @io_lock: user access locking;
  31. */
  32. struct mlxreg_io_priv_data {
  33. struct platform_device *pdev;
  34. struct mlxreg_core_platform_data *pdata;
  35. struct device *hwmon;
  36. struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1];
  37. struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM];
  38. struct attribute_group group;
  39. const struct attribute_group *groups[2];
  40. int regsize;
  41. struct mutex io_lock; /* Protects user access. */
  42. };
  43. static int
  44. mlxreg_io_get_reg(void *regmap, struct mlxreg_core_data *data, u32 in_val,
  45. bool rw_flag, int regsize, u32 *regval)
  46. {
  47. int i, val, ret;
  48. ret = regmap_read(regmap, data->reg, regval);
  49. if (ret)
  50. goto access_error;
  51. /*
  52. * There are four kinds of attributes: single bit, full register's
  53. * bits, bit sequence, bits in few registers For the first kind field
  54. * mask indicates which bits are not related and field bit is set zero.
  55. * For the second kind field mask is set to zero and field bit is set
  56. * with all bits one. No special handling for such kind of attributes -
  57. * pass value as is. For the third kind, the field mask indicates which
  58. * bits are related and the field bit is set to the first bit number
  59. * (from 1 to 32) is the bit sequence. For the fourth kind - the number
  60. * of registers which should be read for getting an attribute are
  61. * specified through 'data->regnum' field.
  62. */
  63. if (!data->bit) {
  64. /* Single bit. */
  65. if (rw_flag) {
  66. /* For show: expose effective bit value as 0 or 1. */
  67. *regval = !!(*regval & ~data->mask);
  68. } else {
  69. /* For store: set effective bit value. */
  70. *regval &= data->mask;
  71. if (in_val)
  72. *regval |= ~data->mask;
  73. }
  74. } else if (data->mask) {
  75. /* Bit sequence. */
  76. if (rw_flag) {
  77. /* For show: mask and shift right. */
  78. *regval = ror32(*regval & data->mask, (data->bit - 1));
  79. } else {
  80. /* For store: shift to the position and mask. */
  81. in_val = rol32(in_val, data->bit - 1) & data->mask;
  82. /* Clear relevant bits and set them to new value. */
  83. *regval = (*regval & ~data->mask) | in_val;
  84. }
  85. } else {
  86. /*
  87. * Some attributes could occupied few registers in case regmap
  88. * bit size is 8 or 16. Compose such attributes from 'regnum'
  89. * registers. Such attributes contain read-only data.
  90. */
  91. for (i = 1; i < data->regnum; i++) {
  92. ret = regmap_read(regmap, data->reg + i, &val);
  93. if (ret)
  94. goto access_error;
  95. *regval |= rol32(val, regsize * i * 8);
  96. }
  97. }
  98. access_error:
  99. return ret;
  100. }
  101. static ssize_t
  102. mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
  106. int index = to_sensor_dev_attr(attr)->index;
  107. struct mlxreg_core_data *data = priv->pdata->data + index;
  108. u32 regval = 0;
  109. int ret;
  110. mutex_lock(&priv->io_lock);
  111. ret = mlxreg_io_get_reg(priv->pdata->regmap, data, 0, true,
  112. priv->regsize, &regval);
  113. if (ret)
  114. goto access_error;
  115. mutex_unlock(&priv->io_lock);
  116. return sprintf(buf, "%u\n", regval);
  117. access_error:
  118. mutex_unlock(&priv->io_lock);
  119. return ret;
  120. }
  121. static ssize_t
  122. mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
  123. const char *buf, size_t len)
  124. {
  125. struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
  126. int index = to_sensor_dev_attr(attr)->index;
  127. struct mlxreg_core_data *data = priv->pdata->data + index;
  128. u32 input_val, regval;
  129. int ret;
  130. if (len > MLXREG_IO_ATT_SIZE)
  131. return -EINVAL;
  132. /* Convert buffer to input value. */
  133. ret = kstrtou32(buf, 0, &input_val);
  134. if (ret)
  135. return ret;
  136. mutex_lock(&priv->io_lock);
  137. ret = mlxreg_io_get_reg(priv->pdata->regmap, data, input_val, false,
  138. priv->regsize, &regval);
  139. if (ret)
  140. goto access_error;
  141. ret = regmap_write(priv->pdata->regmap, data->reg, regval);
  142. if (ret)
  143. goto access_error;
  144. mutex_unlock(&priv->io_lock);
  145. return len;
  146. access_error:
  147. mutex_unlock(&priv->io_lock);
  148. dev_err(&priv->pdev->dev, "Bus access error\n");
  149. return ret;
  150. }
  151. static struct device_attribute mlxreg_io_devattr_rw = {
  152. .show = mlxreg_io_attr_show,
  153. .store = mlxreg_io_attr_store,
  154. };
  155. static int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv)
  156. {
  157. int i;
  158. priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
  159. priv->pdata->counter,
  160. sizeof(struct attribute *),
  161. GFP_KERNEL);
  162. if (!priv->group.attrs)
  163. return -ENOMEM;
  164. for (i = 0; i < priv->pdata->counter; i++) {
  165. priv->mlxreg_io_attr[i] =
  166. &priv->mlxreg_io_dev_attr[i].dev_attr.attr;
  167. memcpy(&priv->mlxreg_io_dev_attr[i].dev_attr,
  168. &mlxreg_io_devattr_rw, sizeof(struct device_attribute));
  169. /* Set attribute name as a label. */
  170. priv->mlxreg_io_attr[i]->name =
  171. devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
  172. priv->pdata->data[i].label);
  173. if (!priv->mlxreg_io_attr[i]->name) {
  174. dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n",
  175. i + 1);
  176. return -ENOMEM;
  177. }
  178. priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode =
  179. priv->pdata->data[i].mode;
  180. priv->mlxreg_io_dev_attr[i].dev_attr.attr.name =
  181. priv->mlxreg_io_attr[i]->name;
  182. priv->mlxreg_io_dev_attr[i].index = i;
  183. sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr);
  184. }
  185. priv->group.attrs = priv->mlxreg_io_attr;
  186. priv->groups[0] = &priv->group;
  187. priv->groups[1] = NULL;
  188. return 0;
  189. }
  190. static int mlxreg_io_probe(struct platform_device *pdev)
  191. {
  192. struct mlxreg_io_priv_data *priv;
  193. int err;
  194. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  195. if (!priv)
  196. return -ENOMEM;
  197. priv->pdata = dev_get_platdata(&pdev->dev);
  198. if (!priv->pdata) {
  199. dev_err(&pdev->dev, "Failed to get platform data.\n");
  200. return -EINVAL;
  201. }
  202. priv->pdev = pdev;
  203. priv->regsize = regmap_get_val_bytes(priv->pdata->regmap);
  204. if (priv->regsize < 0)
  205. return priv->regsize;
  206. err = mlxreg_io_attr_init(priv);
  207. if (err) {
  208. dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n",
  209. err);
  210. return err;
  211. }
  212. priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
  213. "mlxreg_io",
  214. priv,
  215. priv->groups);
  216. if (IS_ERR(priv->hwmon)) {
  217. dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
  218. PTR_ERR(priv->hwmon));
  219. return PTR_ERR(priv->hwmon);
  220. }
  221. mutex_init(&priv->io_lock);
  222. dev_set_drvdata(&pdev->dev, priv);
  223. return 0;
  224. }
  225. static void mlxreg_io_remove(struct platform_device *pdev)
  226. {
  227. struct mlxreg_io_priv_data *priv = dev_get_drvdata(&pdev->dev);
  228. mutex_destroy(&priv->io_lock);
  229. }
  230. static struct platform_driver mlxreg_io_driver = {
  231. .driver = {
  232. .name = "mlxreg-io",
  233. },
  234. .probe = mlxreg_io_probe,
  235. .remove_new = mlxreg_io_remove,
  236. };
  237. module_platform_driver(mlxreg_io_driver);
  238. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  239. MODULE_DESCRIPTION("Mellanox regmap I/O access driver");
  240. MODULE_LICENSE("GPL");
  241. MODULE_ALIAS("platform:mlxreg-io");