iio_hwmon.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * @attr_group: the group of attributes
  24. * @groups: null terminated array of attribute groups
  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 attribute_group attr_group;
  31. const struct attribute_group *groups[2];
  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 int iio_hwmon_probe(struct platform_device *pdev)
  54. {
  55. struct device *dev = &pdev->dev;
  56. struct iio_hwmon_state *st;
  57. struct sensor_device_attribute *a;
  58. int ret, i;
  59. int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1;
  60. enum iio_chan_type type;
  61. struct iio_channel *channels;
  62. const char *name = "iio_hwmon";
  63. struct device *hwmon_dev;
  64. char *sname;
  65. if (dev->of_node && dev->of_node->name)
  66. name = dev->of_node->name;
  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. a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
  87. if (a == NULL)
  88. return -ENOMEM;
  89. sysfs_attr_init(&a->dev_attr.attr);
  90. ret = iio_get_channel_type(&st->channels[i], &type);
  91. if (ret < 0)
  92. return ret;
  93. switch (type) {
  94. case IIO_VOLTAGE:
  95. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  96. "in%d_input",
  97. in_i++);
  98. break;
  99. case IIO_TEMP:
  100. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  101. "temp%d_input",
  102. temp_i++);
  103. break;
  104. case IIO_CURRENT:
  105. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  106. "curr%d_input",
  107. curr_i++);
  108. break;
  109. case IIO_HUMIDITYRELATIVE:
  110. a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
  111. "humidity%d_input",
  112. humidity_i++);
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. if (a->dev_attr.attr.name == NULL)
  118. return -ENOMEM;
  119. a->dev_attr.show = iio_hwmon_read_val;
  120. a->dev_attr.attr.mode = S_IRUGO;
  121. a->index = i;
  122. st->attrs[i] = &a->dev_attr.attr;
  123. }
  124. st->attr_group.attrs = st->attrs;
  125. st->groups[0] = &st->attr_group;
  126. sname = devm_kstrdup(dev, name, GFP_KERNEL);
  127. if (!sname)
  128. return -ENOMEM;
  129. strreplace(sname, '-', '_');
  130. hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
  131. st->groups);
  132. return PTR_ERR_OR_ZERO(hwmon_dev);
  133. }
  134. static const struct of_device_id iio_hwmon_of_match[] = {
  135. { .compatible = "iio-hwmon", },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
  139. static struct platform_driver __refdata iio_hwmon_driver = {
  140. .driver = {
  141. .name = "iio_hwmon",
  142. .of_match_table = iio_hwmon_of_match,
  143. },
  144. .probe = iio_hwmon_probe,
  145. };
  146. module_platform_driver(iio_hwmon_driver);
  147. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  148. MODULE_DESCRIPTION("IIO to hwmon driver");
  149. MODULE_LICENSE("GPL v2");