iio_hwmon.c 4.3 KB

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