iio_hwmon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. struct attribute **attrs;
  33. };
  34. /*
  35. * Assumes that IIO and hwmon operate in the same base units.
  36. * This is supposed to be true, but needs verification for
  37. * new channel types.
  38. */
  39. static ssize_t iio_hwmon_read_val(struct device *dev,
  40. struct device_attribute *attr,
  41. char *buf)
  42. {
  43. int result;
  44. int ret;
  45. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  46. struct iio_hwmon_state *state = dev_get_drvdata(dev);
  47. ret = iio_read_channel_processed(&state->channels[sattr->index],
  48. &result);
  49. if (ret < 0)
  50. return ret;
  51. return sprintf(buf, "%d\n", result);
  52. }
  53. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  54. char *buf)
  55. {
  56. const char *name = "iio_hwmon";
  57. if (dev->of_node && dev->of_node->name)
  58. name = dev->of_node->name;
  59. return sprintf(buf, "%s\n", name);
  60. }
  61. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  62. static int iio_hwmon_probe(struct platform_device *pdev)
  63. {
  64. struct device *dev = &pdev->dev;
  65. struct iio_hwmon_state *st;
  66. struct sensor_device_attribute *a;
  67. int ret, i;
  68. int in_i = 1, temp_i = 1, curr_i = 1;
  69. enum iio_chan_type type;
  70. struct iio_channel *channels;
  71. channels = iio_channel_get_all(dev);
  72. if (IS_ERR(channels))
  73. return PTR_ERR(channels);
  74. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  75. if (st == NULL) {
  76. ret = -ENOMEM;
  77. goto error_release_channels;
  78. }
  79. st->channels = channels;
  80. /* count how many attributes we have */
  81. while (st->channels[st->num_channels].indio_dev)
  82. st->num_channels++;
  83. st->attrs = devm_kzalloc(dev,
  84. sizeof(*st->attrs) * (st->num_channels + 2),
  85. GFP_KERNEL);
  86. if (st->attrs == NULL) {
  87. ret = -ENOMEM;
  88. goto error_release_channels;
  89. }
  90. for (i = 0; i < st->num_channels; i++) {
  91. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  92. if (a == NULL) {
  93. ret = -ENOMEM;
  94. goto error_release_channels;
  95. }
  96. sysfs_attr_init(&a->dev_attr.attr);
  97. ret = iio_get_channel_type(&st->channels[i], &type);
  98. if (ret < 0)
  99. goto error_release_channels;
  100. switch (type) {
  101. case IIO_VOLTAGE:
  102. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  103. "in%d_input",
  104. in_i++);
  105. break;
  106. case IIO_TEMP:
  107. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  108. "temp%d_input",
  109. temp_i++);
  110. break;
  111. case IIO_CURRENT:
  112. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  113. "curr%d_input",
  114. curr_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->attrs[st->num_channels] = &dev_attr_name.attr;
  130. st->attr_group.attrs = st->attrs;
  131. platform_set_drvdata(pdev, st);
  132. ret = sysfs_create_group(&dev->kobj, &st->attr_group);
  133. if (ret < 0)
  134. goto error_release_channels;
  135. st->hwmon_dev = hwmon_device_register(dev);
  136. if (IS_ERR(st->hwmon_dev)) {
  137. ret = PTR_ERR(st->hwmon_dev);
  138. goto error_remove_group;
  139. }
  140. return 0;
  141. error_remove_group:
  142. sysfs_remove_group(&dev->kobj, &st->attr_group);
  143. error_release_channels:
  144. iio_channel_release_all(channels);
  145. return ret;
  146. }
  147. static int iio_hwmon_remove(struct platform_device *pdev)
  148. {
  149. struct iio_hwmon_state *st = platform_get_drvdata(pdev);
  150. hwmon_device_unregister(st->hwmon_dev);
  151. sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
  152. iio_channel_release_all(st->channels);
  153. return 0;
  154. }
  155. static struct of_device_id iio_hwmon_of_match[] = {
  156. { .compatible = "iio-hwmon", },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
  160. static struct platform_driver __refdata iio_hwmon_driver = {
  161. .driver = {
  162. .name = "iio_hwmon",
  163. .owner = THIS_MODULE,
  164. .of_match_table = iio_hwmon_of_match,
  165. },
  166. .probe = iio_hwmon_probe,
  167. .remove = iio_hwmon_remove,
  168. };
  169. module_platform_driver(iio_hwmon_driver);
  170. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  171. MODULE_DESCRIPTION("IIO to hwmon driver");
  172. MODULE_LICENSE("GPL v2");