mlxreg-io.c 7.5 KB

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