mlxreg-io.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/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 48
  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. */
  31. struct mlxreg_io_priv_data {
  32. struct platform_device *pdev;
  33. struct mlxreg_core_platform_data *pdata;
  34. struct device *hwmon;
  35. struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1];
  36. struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM];
  37. struct attribute_group group;
  38. const struct attribute_group *groups[2];
  39. };
  40. static int
  41. mlxreg_io_get_reg(void *regmap, struct mlxreg_core_data *data, u32 in_val,
  42. bool rw_flag, u32 *regval)
  43. {
  44. int ret;
  45. ret = regmap_read(regmap, data->reg, regval);
  46. if (ret)
  47. goto access_error;
  48. /*
  49. * There are three kinds of attributes: single bit, full register's
  50. * bits and bit sequence. For the first kind field mask indicates which
  51. * bits are not related and field bit is set zero. For the second kind
  52. * field mask is set to zero and field bit is set with all bits one.
  53. * No special handling for such kind of attributes - pass value as is.
  54. * For the third kind, field mask indicates which bits are related and
  55. * field bit is set to the first bit number (from 1 to 32) is the bit
  56. * sequence.
  57. */
  58. if (!data->bit) {
  59. /* Single bit. */
  60. if (rw_flag) {
  61. /* For show: expose effective bit value as 0 or 1. */
  62. *regval = !!(*regval & ~data->mask);
  63. } else {
  64. /* For store: set effective bit value. */
  65. *regval &= data->mask;
  66. if (in_val)
  67. *regval |= ~data->mask;
  68. }
  69. } else if (data->mask) {
  70. /* Bit sequence. */
  71. if (rw_flag) {
  72. /* For show: mask and shift right. */
  73. *regval = ror32(*regval & data->mask, (data->bit - 1));
  74. } else {
  75. /* For store: shift to the position and mask. */
  76. in_val = rol32(in_val, data->bit - 1) & data->mask;
  77. /* Clear relevant bits and set them to new value. */
  78. *regval = (*regval & ~data->mask) | in_val;
  79. }
  80. }
  81. access_error:
  82. return ret;
  83. }
  84. static ssize_t
  85. mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
  86. char *buf)
  87. {
  88. struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
  89. int index = to_sensor_dev_attr(attr)->index;
  90. struct mlxreg_core_data *data = priv->pdata->data + index;
  91. u32 regval = 0;
  92. int ret;
  93. ret = mlxreg_io_get_reg(priv->pdata->regmap, data, 0, true, &regval);
  94. if (ret)
  95. goto access_error;
  96. return sprintf(buf, "%u\n", regval);
  97. access_error:
  98. return ret;
  99. }
  100. static ssize_t
  101. mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
  102. const char *buf, size_t len)
  103. {
  104. struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
  105. int index = to_sensor_dev_attr(attr)->index;
  106. struct mlxreg_core_data *data = priv->pdata->data + index;
  107. u32 input_val, regval;
  108. int ret;
  109. if (len > MLXREG_IO_ATT_SIZE)
  110. return -EINVAL;
  111. /* Convert buffer to input value. */
  112. ret = kstrtou32(buf, len, &input_val);
  113. if (ret)
  114. return ret;
  115. ret = mlxreg_io_get_reg(priv->pdata->regmap, data, input_val, false,
  116. &regval);
  117. if (ret)
  118. goto access_error;
  119. ret = regmap_write(priv->pdata->regmap, data->reg, regval);
  120. if (ret)
  121. goto access_error;
  122. return len;
  123. access_error:
  124. dev_err(&priv->pdev->dev, "Bus access error\n");
  125. return ret;
  126. }
  127. static struct device_attribute mlxreg_io_devattr_rw = {
  128. .show = mlxreg_io_attr_show,
  129. .store = mlxreg_io_attr_store,
  130. };
  131. static int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv)
  132. {
  133. int i;
  134. priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
  135. priv->pdata->counter,
  136. sizeof(struct attribute *),
  137. GFP_KERNEL);
  138. if (!priv->group.attrs)
  139. return -ENOMEM;
  140. for (i = 0; i < priv->pdata->counter; i++) {
  141. priv->mlxreg_io_attr[i] =
  142. &priv->mlxreg_io_dev_attr[i].dev_attr.attr;
  143. memcpy(&priv->mlxreg_io_dev_attr[i].dev_attr,
  144. &mlxreg_io_devattr_rw, sizeof(struct device_attribute));
  145. /* Set attribute name as a label. */
  146. priv->mlxreg_io_attr[i]->name =
  147. devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
  148. priv->pdata->data[i].label);
  149. if (!priv->mlxreg_io_attr[i]->name) {
  150. dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n",
  151. i + 1);
  152. return -ENOMEM;
  153. }
  154. priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode =
  155. priv->pdata->data[i].mode;
  156. priv->mlxreg_io_dev_attr[i].dev_attr.attr.name =
  157. priv->mlxreg_io_attr[i]->name;
  158. priv->mlxreg_io_dev_attr[i].index = i;
  159. sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr);
  160. }
  161. priv->group.attrs = priv->mlxreg_io_attr;
  162. priv->groups[0] = &priv->group;
  163. priv->groups[1] = NULL;
  164. return 0;
  165. }
  166. static int mlxreg_io_probe(struct platform_device *pdev)
  167. {
  168. struct mlxreg_io_priv_data *priv;
  169. int err;
  170. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  171. if (!priv)
  172. return -ENOMEM;
  173. priv->pdata = dev_get_platdata(&pdev->dev);
  174. if (!priv->pdata) {
  175. dev_err(&pdev->dev, "Failed to get platform data.\n");
  176. return -EINVAL;
  177. }
  178. priv->pdev = pdev;
  179. err = mlxreg_io_attr_init(priv);
  180. if (err) {
  181. dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n",
  182. err);
  183. return err;
  184. }
  185. priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
  186. "mlxreg_io",
  187. priv,
  188. priv->groups);
  189. if (IS_ERR(priv->hwmon)) {
  190. dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
  191. PTR_ERR(priv->hwmon));
  192. return PTR_ERR(priv->hwmon);
  193. }
  194. dev_set_drvdata(&pdev->dev, priv);
  195. return 0;
  196. }
  197. static struct platform_driver mlxreg_io_driver = {
  198. .driver = {
  199. .name = "mlxreg-io",
  200. },
  201. .probe = mlxreg_io_probe,
  202. };
  203. module_platform_driver(mlxreg_io_driver);
  204. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  205. MODULE_DESCRIPTION("Mellanox regmap I/O access driver");
  206. MODULE_LICENSE("GPL");
  207. MODULE_ALIAS("platform:mlxreg-io");