iio_hwmon.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Hwmon client for industrial I/O devices
  2. *
  3. * Copyright (c) 2011 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/of.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/types.h>
  19. /**
  20. * struct iio_hwmon_state - device instance state
  21. * @channels: filled with array of channels from iio
  22. * @num_channels: number of channels in channels (saves counting twice)
  23. * @hwmon_dev: associated hwmon device
  24. * @attr_group: the group of attributes
  25. * @attrs: null terminated array of attribute pointers.
  26. */
  27. struct iio_hwmon_state {
  28. struct iio_channel *channels;
  29. int num_channels;
  30. struct device *hwmon_dev;
  31. struct attribute_group attr_group;
  32. const struct attribute_group *groups[2];
  33. struct attribute **attrs;
  34. };
  35. /*
  36. * Assumes that IIO and hwmon operate in the same base units.
  37. * This is supposed to be true, but needs verification for
  38. * new channel types.
  39. */
  40. static ssize_t iio_hwmon_read_val(struct device *dev,
  41. struct device_attribute *attr,
  42. char *buf)
  43. {
  44. int result;
  45. int ret;
  46. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  47. struct iio_hwmon_state *state = dev_get_drvdata(dev);
  48. ret = iio_read_channel_processed(&state->channels[sattr->index],
  49. &result);
  50. if (ret < 0)
  51. return ret;
  52. return sprintf(buf, "%d\n", result);
  53. }
  54. static int iio_hwmon_probe(struct platform_device *pdev)
  55. {
  56. struct device *dev = &pdev->dev;
  57. struct iio_hwmon_state *st;
  58. struct sensor_device_attribute *a;
  59. int ret, i;
  60. int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1;
  61. enum iio_chan_type type;
  62. struct iio_channel *channels;
  63. const char *name = "iio_hwmon";
  64. if (dev->of_node && dev->of_node->name)
  65. name = dev->of_node->name;
  66. channels = iio_channel_get_all(dev);
  67. if (IS_ERR(channels))
  68. return PTR_ERR(channels);
  69. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  70. if (st == NULL) {
  71. ret = -ENOMEM;
  72. goto error_release_channels;
  73. }
  74. st->channels = channels;
  75. /* count how many attributes we have */
  76. while (st->channels[st->num_channels].indio_dev)
  77. st->num_channels++;
  78. st->attrs = devm_kzalloc(dev,
  79. sizeof(*st->attrs) * (st->num_channels + 1),
  80. GFP_KERNEL);
  81. if (st->attrs == NULL) {
  82. ret = -ENOMEM;
  83. goto error_release_channels;
  84. }
  85. for (i = 0; i < st->num_channels; i++) {
  86. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  87. if (a == NULL) {
  88. ret = -ENOMEM;
  89. goto error_release_channels;
  90. }
  91. sysfs_attr_init(&a->dev_attr.attr);
  92. ret = iio_get_channel_type(&st->channels[i], &type);
  93. if (ret < 0)
  94. goto error_release_channels;
  95. switch (type) {
  96. case IIO_VOLTAGE:
  97. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  98. "in%d_input",
  99. in_i++);
  100. break;
  101. case IIO_TEMP:
  102. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  103. "temp%d_input",
  104. temp_i++);
  105. break;
  106. case IIO_CURRENT:
  107. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  108. "curr%d_input",
  109. curr_i++);
  110. break;
  111. case IIO_HUMIDITYRELATIVE:
  112. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  113. "humidity%d_input",
  114. humidity_i++);
  115. break;
  116. default:
  117. ret = -EINVAL;
  118. goto error_release_channels;
  119. }
  120. if (a->dev_attr.attr.name == NULL) {
  121. ret = -ENOMEM;
  122. goto error_release_channels;
  123. }
  124. a->dev_attr.show = iio_hwmon_read_val;
  125. a->dev_attr.attr.mode = S_IRUGO;
  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. st->hwmon_dev = hwmon_device_register_with_groups(dev, name, st,
  132. st->groups);
  133. if (IS_ERR(st->hwmon_dev)) {
  134. ret = PTR_ERR(st->hwmon_dev);
  135. goto error_release_channels;
  136. }
  137. platform_set_drvdata(pdev, st);
  138. return 0;
  139. error_release_channels:
  140. iio_channel_release_all(channels);
  141. return ret;
  142. }
  143. static int iio_hwmon_remove(struct platform_device *pdev)
  144. {
  145. struct iio_hwmon_state *st = platform_get_drvdata(pdev);
  146. hwmon_device_unregister(st->hwmon_dev);
  147. iio_channel_release_all(st->channels);
  148. return 0;
  149. }
  150. static const struct of_device_id iio_hwmon_of_match[] = {
  151. { .compatible = "iio-hwmon", },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
  155. static struct platform_driver __refdata iio_hwmon_driver = {
  156. .driver = {
  157. .name = "iio_hwmon",
  158. .of_match_table = iio_hwmon_of_match,
  159. },
  160. .probe = iio_hwmon_probe,
  161. .remove = iio_hwmon_remove,
  162. };
  163. module_platform_driver(iio_hwmon_driver);
  164. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  165. MODULE_DESCRIPTION("IIO to hwmon driver");
  166. MODULE_LICENSE("GPL v2");