iio_hwmon.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. 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_kzalloc(dev,
  82. sizeof(*st->attrs) * (st->num_channels + 2),
  83. GFP_KERNEL);
  84. if (st->attrs == NULL) {
  85. ret = -ENOMEM;
  86. goto error_release_channels;
  87. }
  88. for (i = 0; i < st->num_channels; i++) {
  89. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  90. if (a == NULL) {
  91. ret = -ENOMEM;
  92. goto error_release_channels;
  93. }
  94. sysfs_attr_init(&a->dev_attr.attr);
  95. ret = iio_get_channel_type(&st->channels[i], &type);
  96. if (ret < 0)
  97. goto error_release_channels;
  98. switch (type) {
  99. case IIO_VOLTAGE:
  100. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  101. "in%d_input",
  102. in_i++);
  103. break;
  104. case IIO_TEMP:
  105. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  106. "temp%d_input",
  107. temp_i++);
  108. break;
  109. case IIO_CURRENT:
  110. a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
  111. "curr%d_input",
  112. curr_i++);
  113. break;
  114. default:
  115. ret = -EINVAL;
  116. goto error_release_channels;
  117. }
  118. if (a->dev_attr.attr.name == NULL) {
  119. ret = -ENOMEM;
  120. goto error_release_channels;
  121. }
  122. a->dev_attr.show = iio_hwmon_read_val;
  123. a->dev_attr.attr.mode = S_IRUGO;
  124. a->index = i;
  125. st->attrs[i] = &a->dev_attr.attr;
  126. }
  127. st->attrs[st->num_channels] = &dev_attr_name.attr;
  128. st->attr_group.attrs = st->attrs;
  129. platform_set_drvdata(pdev, st);
  130. ret = sysfs_create_group(&dev->kobj, &st->attr_group);
  131. if (ret < 0)
  132. goto error_release_channels;
  133. st->hwmon_dev = hwmon_device_register(dev);
  134. if (IS_ERR(st->hwmon_dev)) {
  135. ret = PTR_ERR(st->hwmon_dev);
  136. goto error_remove_group;
  137. }
  138. return 0;
  139. error_remove_group:
  140. sysfs_remove_group(&dev->kobj, &st->attr_group);
  141. error_release_channels:
  142. iio_channel_release_all(st->channels);
  143. return ret;
  144. }
  145. static int iio_hwmon_remove(struct platform_device *pdev)
  146. {
  147. struct iio_hwmon_state *st = platform_get_drvdata(pdev);
  148. hwmon_device_unregister(st->hwmon_dev);
  149. sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
  150. iio_channel_release_all(st->channels);
  151. return 0;
  152. }
  153. static struct of_device_id iio_hwmon_of_match[] = {
  154. { .compatible = "iio-hwmon", },
  155. { }
  156. };
  157. static struct platform_driver __refdata iio_hwmon_driver = {
  158. .driver = {
  159. .name = "iio_hwmon",
  160. .owner = THIS_MODULE,
  161. .of_match_table = iio_hwmon_of_match,
  162. },
  163. .probe = iio_hwmon_probe,
  164. .remove = iio_hwmon_remove,
  165. };
  166. module_platform_driver(iio_hwmon_driver);
  167. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  168. MODULE_DESCRIPTION("IIO to hwmon driver");
  169. MODULE_LICENSE("GPL v2");