iio_hwmon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Hwmon client for industrial I/O devices
  3. *
  4. * Copyright (c) 2011 Jonathan Cameron
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/hwmon-sysfs.h>
  15. #include <linux/iio/consumer.h>
  16. #include <linux/iio/types.h>
  17. /**
  18. * struct iio_hwmon_state - device instance state
  19. * @channels: filled with array of channels from iio
  20. * @num_channels: number of channels in channels (saves counting twice)
  21. * @attr_group: the group of attributes
  22. * @groups: null terminated array of attribute groups
  23. * @attrs: null terminated array of attribute pointers.
  24. */
  25. struct iio_hwmon_state {
  26. struct iio_channel *channels;
  27. int num_channels;
  28. struct attribute_group attr_group;
  29. const struct attribute_group *groups[2];
  30. struct attribute **attrs;
  31. };
  32. static ssize_t iio_hwmon_read_label(struct device *dev,
  33. struct device_attribute *attr,
  34. char *buf)
  35. {
  36. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  37. struct iio_hwmon_state *state = dev_get_drvdata(dev);
  38. struct iio_channel *chan = &state->channels[sattr->index];
  39. return iio_read_channel_label(chan, buf);
  40. }
  41. /*
  42. * Assumes that IIO and hwmon operate in the same base units.
  43. * This is supposed to be true, but needs verification for
  44. * new channel types.
  45. */
  46. static ssize_t iio_hwmon_read_val(struct device *dev,
  47. struct device_attribute *attr,
  48. char *buf)
  49. {
  50. int result;
  51. int ret;
  52. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  53. struct iio_hwmon_state *state = dev_get_drvdata(dev);
  54. struct iio_channel *chan = &state->channels[sattr->index];
  55. enum iio_chan_type type;
  56. ret = iio_get_channel_type(chan, &type);
  57. if (ret < 0)
  58. return ret;
  59. if (type == IIO_POWER)
  60. /* mili-Watts to micro-Watts conversion */
  61. ret = iio_read_channel_processed_scale(chan, &result, 1000);
  62. else
  63. ret = iio_read_channel_processed(chan, &result);
  64. if (ret < 0)
  65. return ret;
  66. return sprintf(buf, "%d\n", result);
  67. }
  68. static int iio_hwmon_probe(struct platform_device *pdev)
  69. {
  70. struct device *dev = &pdev->dev;
  71. struct iio_hwmon_state *st;
  72. struct sensor_device_attribute *a;
  73. int ret, i, attr = 0;
  74. int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
  75. enum iio_chan_type type;
  76. struct iio_channel *channels;
  77. struct device *hwmon_dev;
  78. char *sname;
  79. void *buf;
  80. channels = devm_iio_channel_get_all(dev);
  81. if (IS_ERR(channels)) {
  82. ret = PTR_ERR(channels);
  83. if (ret == -ENODEV)
  84. ret = -EPROBE_DEFER;
  85. return dev_err_probe(dev, ret,
  86. "Failed to get channels\n");
  87. }
  88. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  89. buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
  90. if (!st || !buf)
  91. return -ENOMEM;
  92. st->channels = channels;
  93. /* count how many channels we have */
  94. while (st->channels[st->num_channels].indio_dev)
  95. st->num_channels++;
  96. st->attrs = devm_kcalloc(dev,
  97. 2 * st->num_channels + 1, sizeof(*st->attrs),
  98. GFP_KERNEL);
  99. if (st->attrs == NULL)
  100. return -ENOMEM;
  101. for (i = 0; i < st->num_channels; i++) {
  102. const char *prefix;
  103. int n;
  104. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  105. if (a == NULL)
  106. return -ENOMEM;
  107. sysfs_attr_init(&a->dev_attr.attr);
  108. ret = iio_get_channel_type(&st->channels[i], &type);
  109. if (ret < 0)
  110. return ret;
  111. switch (type) {
  112. case IIO_VOLTAGE:
  113. n = in_i++;
  114. prefix = "in";
  115. break;
  116. case IIO_TEMP:
  117. n = temp_i++;
  118. prefix = "temp";
  119. break;
  120. case IIO_CURRENT:
  121. n = curr_i++;
  122. prefix = "curr";
  123. break;
  124. case IIO_POWER:
  125. n = power_i++;
  126. prefix = "power";
  127. break;
  128. case IIO_HUMIDITYRELATIVE:
  129. n = humidity_i++;
  130. prefix = "humidity";
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  136. "%s%d_input",
  137. prefix, n);
  138. if (a->dev_attr.attr.name == NULL)
  139. return -ENOMEM;
  140. a->dev_attr.show = iio_hwmon_read_val;
  141. a->dev_attr.attr.mode = 0444;
  142. a->index = i;
  143. st->attrs[attr++] = &a->dev_attr.attr;
  144. /* Let's see if we have a label... */
  145. if (iio_read_channel_label(&st->channels[i], buf) < 0)
  146. continue;
  147. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  148. if (a == NULL)
  149. return -ENOMEM;
  150. sysfs_attr_init(&a->dev_attr.attr);
  151. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  152. "%s%d_label",
  153. prefix, n);
  154. if (!a->dev_attr.attr.name)
  155. return -ENOMEM;
  156. a->dev_attr.show = iio_hwmon_read_label;
  157. a->dev_attr.attr.mode = 0444;
  158. a->index = i;
  159. st->attrs[attr++] = &a->dev_attr.attr;
  160. }
  161. devm_free_pages(dev, (unsigned long)buf);
  162. st->attr_group.attrs = st->attrs;
  163. st->groups[0] = &st->attr_group;
  164. if (dev_fwnode(dev)) {
  165. sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev));
  166. if (!sname)
  167. return -ENOMEM;
  168. strreplace(sname, '-', '_');
  169. } else {
  170. sname = "iio_hwmon";
  171. }
  172. hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
  173. st->groups);
  174. return PTR_ERR_OR_ZERO(hwmon_dev);
  175. }
  176. static const struct of_device_id iio_hwmon_of_match[] = {
  177. { .compatible = "iio-hwmon", },
  178. { }
  179. };
  180. MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
  181. static struct platform_driver iio_hwmon_driver = {
  182. .driver = {
  183. .name = "iio_hwmon",
  184. .of_match_table = iio_hwmon_of_match,
  185. },
  186. .probe = iio_hwmon_probe,
  187. };
  188. module_platform_driver(iio_hwmon_driver);
  189. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  190. MODULE_DESCRIPTION("IIO to hwmon driver");
  191. MODULE_LICENSE("GPL v2");