iio_hwmon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /*
  33. * Assumes that IIO and hwmon operate in the same base units.
  34. * This is supposed to be true, but needs verification for
  35. * new channel types.
  36. */
  37. static ssize_t iio_hwmon_read_val(struct device *dev,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. int result;
  42. int ret;
  43. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  44. struct iio_hwmon_state *state = dev_get_drvdata(dev);
  45. struct iio_channel *chan = &state->channels[sattr->index];
  46. enum iio_chan_type type;
  47. ret = iio_read_channel_processed(chan, &result);
  48. if (ret < 0)
  49. return ret;
  50. ret = iio_get_channel_type(chan, &type);
  51. if (ret < 0)
  52. return ret;
  53. if (type == IIO_POWER)
  54. result *= 1000; /* mili-Watts to micro-Watts conversion */
  55. return sprintf(buf, "%d\n", result);
  56. }
  57. static int iio_hwmon_probe(struct platform_device *pdev)
  58. {
  59. struct device *dev = &pdev->dev;
  60. struct iio_hwmon_state *st;
  61. struct sensor_device_attribute *a;
  62. int ret, i;
  63. int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
  64. enum iio_chan_type type;
  65. struct iio_channel *channels;
  66. struct device *hwmon_dev;
  67. char *sname;
  68. channels = devm_iio_channel_get_all(dev);
  69. if (IS_ERR(channels)) {
  70. if (PTR_ERR(channels) == -ENODEV)
  71. return -EPROBE_DEFER;
  72. return PTR_ERR(channels);
  73. }
  74. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  75. if (st == NULL)
  76. return -ENOMEM;
  77. st->channels = channels;
  78. /* count how many attributes we have */
  79. while (st->channels[st->num_channels].indio_dev)
  80. st->num_channels++;
  81. st->attrs = devm_kcalloc(dev,
  82. st->num_channels + 1, sizeof(*st->attrs),
  83. GFP_KERNEL);
  84. if (st->attrs == NULL)
  85. return -ENOMEM;
  86. for (i = 0; i < st->num_channels; i++) {
  87. const char *prefix;
  88. int n;
  89. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  90. if (a == NULL)
  91. return -ENOMEM;
  92. sysfs_attr_init(&a->dev_attr.attr);
  93. ret = iio_get_channel_type(&st->channels[i], &type);
  94. if (ret < 0)
  95. return ret;
  96. switch (type) {
  97. case IIO_VOLTAGE:
  98. n = in_i++;
  99. prefix = "in";
  100. break;
  101. case IIO_TEMP:
  102. n = temp_i++;
  103. prefix = "temp";
  104. break;
  105. case IIO_CURRENT:
  106. n = curr_i++;
  107. prefix = "curr";
  108. break;
  109. case IIO_POWER:
  110. n = power_i++;
  111. prefix = "power";
  112. break;
  113. case IIO_HUMIDITYRELATIVE:
  114. n = humidity_i++;
  115. prefix = "humidity";
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  121. "%s%d_input",
  122. prefix, n);
  123. if (a->dev_attr.attr.name == NULL)
  124. return -ENOMEM;
  125. a->dev_attr.show = iio_hwmon_read_val;
  126. a->dev_attr.attr.mode = 0444;
  127. a->index = i;
  128. st->attrs[i] = &a->dev_attr.attr;
  129. }
  130. st->attr_group.attrs = st->attrs;
  131. st->groups[0] = &st->attr_group;
  132. if (dev_fwnode(dev)) {
  133. sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev));
  134. if (!sname)
  135. return -ENOMEM;
  136. strreplace(sname, '-', '_');
  137. } else {
  138. sname = "iio_hwmon";
  139. }
  140. hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
  141. st->groups);
  142. return PTR_ERR_OR_ZERO(hwmon_dev);
  143. }
  144. static const struct of_device_id iio_hwmon_of_match[] = {
  145. { .compatible = "iio-hwmon", },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
  149. static struct platform_driver iio_hwmon_driver = {
  150. .driver = {
  151. .name = "iio_hwmon",
  152. .of_match_table = iio_hwmon_of_match,
  153. },
  154. .probe = iio_hwmon_probe,
  155. };
  156. module_platform_driver(iio_hwmon_driver);
  157. MODULE_AUTHOR("Jonathan Cameron <[email protected]>");
  158. MODULE_DESCRIPTION("IIO to hwmon driver");
  159. MODULE_LICENSE("GPL v2");