scpi-hwmon.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * System Control and Power Interface(SCPI) based hwmon sensor driver
  3. *
  4. * Copyright (C) 2015 ARM Ltd.
  5. * Punit Agrawal <punit.agrawal@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/hwmon.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/scpi_protocol.h>
  20. #include <linux/slab.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/thermal.h>
  23. struct sensor_data {
  24. struct scpi_sensor_info info;
  25. struct device_attribute dev_attr_input;
  26. struct device_attribute dev_attr_label;
  27. char input[20];
  28. char label[20];
  29. };
  30. struct scpi_thermal_zone {
  31. struct list_head list;
  32. int sensor_id;
  33. struct scpi_sensors *scpi_sensors;
  34. struct thermal_zone_device *tzd;
  35. };
  36. struct scpi_sensors {
  37. struct scpi_ops *scpi_ops;
  38. struct sensor_data *data;
  39. struct list_head thermal_zones;
  40. struct attribute **attrs;
  41. struct attribute_group group;
  42. const struct attribute_group *groups[2];
  43. };
  44. static int scpi_read_temp(void *dev, int *temp)
  45. {
  46. struct scpi_thermal_zone *zone = dev;
  47. struct scpi_sensors *scpi_sensors = zone->scpi_sensors;
  48. struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
  49. struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id];
  50. u32 value;
  51. int ret;
  52. ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
  53. if (ret)
  54. return ret;
  55. *temp = value;
  56. return 0;
  57. }
  58. /* hwmon callback functions */
  59. static ssize_t
  60. scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
  61. {
  62. struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
  63. struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
  64. struct sensor_data *sensor;
  65. u32 value;
  66. int ret;
  67. sensor = container_of(attr, struct sensor_data, dev_attr_input);
  68. ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
  69. if (ret)
  70. return ret;
  71. return sprintf(buf, "%u\n", value);
  72. }
  73. static ssize_t
  74. scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
  75. {
  76. struct sensor_data *sensor;
  77. sensor = container_of(attr, struct sensor_data, dev_attr_label);
  78. return sprintf(buf, "%s\n", sensor->info.name);
  79. }
  80. static void
  81. unregister_thermal_zones(struct platform_device *pdev,
  82. struct scpi_sensors *scpi_sensors)
  83. {
  84. struct list_head *pos;
  85. list_for_each(pos, &scpi_sensors->thermal_zones) {
  86. struct scpi_thermal_zone *zone;
  87. zone = list_entry(pos, struct scpi_thermal_zone, list);
  88. thermal_zone_of_sensor_unregister(&pdev->dev, zone->tzd);
  89. }
  90. }
  91. static struct thermal_zone_of_device_ops scpi_sensor_ops = {
  92. .get_temp = scpi_read_temp,
  93. };
  94. static int scpi_hwmon_probe(struct platform_device *pdev)
  95. {
  96. u16 nr_sensors, i;
  97. int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
  98. struct scpi_ops *scpi_ops;
  99. struct device *hwdev, *dev = &pdev->dev;
  100. struct scpi_sensors *scpi_sensors;
  101. int ret, idx;
  102. scpi_ops = get_scpi_ops();
  103. if (!scpi_ops)
  104. return -EPROBE_DEFER;
  105. ret = scpi_ops->sensor_get_capability(&nr_sensors);
  106. if (ret)
  107. return ret;
  108. if (!nr_sensors)
  109. return -ENODEV;
  110. scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
  111. if (!scpi_sensors)
  112. return -ENOMEM;
  113. scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
  114. sizeof(*scpi_sensors->data), GFP_KERNEL);
  115. if (!scpi_sensors->data)
  116. return -ENOMEM;
  117. scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
  118. sizeof(*scpi_sensors->attrs), GFP_KERNEL);
  119. if (!scpi_sensors->attrs)
  120. return -ENOMEM;
  121. scpi_sensors->scpi_ops = scpi_ops;
  122. for (i = 0, idx = 0; i < nr_sensors; i++) {
  123. struct sensor_data *sensor = &scpi_sensors->data[idx];
  124. ret = scpi_ops->sensor_get_info(i, &sensor->info);
  125. if (ret)
  126. return ret;
  127. switch (sensor->info.class) {
  128. case TEMPERATURE:
  129. snprintf(sensor->input, sizeof(sensor->input),
  130. "temp%d_input", num_temp + 1);
  131. snprintf(sensor->label, sizeof(sensor->input),
  132. "temp%d_label", num_temp + 1);
  133. num_temp++;
  134. break;
  135. case VOLTAGE:
  136. snprintf(sensor->input, sizeof(sensor->input),
  137. "in%d_input", num_volt);
  138. snprintf(sensor->label, sizeof(sensor->input),
  139. "in%d_label", num_volt);
  140. num_volt++;
  141. break;
  142. case CURRENT:
  143. snprintf(sensor->input, sizeof(sensor->input),
  144. "curr%d_input", num_current + 1);
  145. snprintf(sensor->label, sizeof(sensor->input),
  146. "curr%d_label", num_current + 1);
  147. num_current++;
  148. break;
  149. case POWER:
  150. snprintf(sensor->input, sizeof(sensor->input),
  151. "power%d_input", num_power + 1);
  152. snprintf(sensor->label, sizeof(sensor->input),
  153. "power%d_label", num_power + 1);
  154. num_power++;
  155. break;
  156. default:
  157. continue;
  158. }
  159. sensor->dev_attr_input.attr.mode = S_IRUGO;
  160. sensor->dev_attr_input.show = scpi_show_sensor;
  161. sensor->dev_attr_input.attr.name = sensor->input;
  162. sensor->dev_attr_label.attr.mode = S_IRUGO;
  163. sensor->dev_attr_label.show = scpi_show_label;
  164. sensor->dev_attr_label.attr.name = sensor->label;
  165. scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
  166. scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
  167. sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
  168. sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
  169. idx++;
  170. }
  171. scpi_sensors->group.attrs = scpi_sensors->attrs;
  172. scpi_sensors->groups[0] = &scpi_sensors->group;
  173. platform_set_drvdata(pdev, scpi_sensors);
  174. hwdev = devm_hwmon_device_register_with_groups(dev,
  175. "scpi_sensors", scpi_sensors, scpi_sensors->groups);
  176. if (IS_ERR(hwdev))
  177. return PTR_ERR(hwdev);
  178. /*
  179. * Register the temperature sensors with the thermal framework
  180. * to allow their usage in setting up the thermal zones from
  181. * device tree.
  182. *
  183. * NOTE: Not all temperature sensors maybe used for thermal
  184. * control
  185. */
  186. INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
  187. for (i = 0; i < nr_sensors; i++) {
  188. struct sensor_data *sensor = &scpi_sensors->data[i];
  189. struct scpi_thermal_zone *zone;
  190. if (sensor->info.class != TEMPERATURE)
  191. continue;
  192. zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
  193. if (!zone) {
  194. ret = -ENOMEM;
  195. goto unregister_tzd;
  196. }
  197. zone->sensor_id = i;
  198. zone->scpi_sensors = scpi_sensors;
  199. zone->tzd = thermal_zone_of_sensor_register(dev,
  200. sensor->info.sensor_id, zone, &scpi_sensor_ops);
  201. /*
  202. * The call to thermal_zone_of_sensor_register returns
  203. * an error for sensors that are not associated with
  204. * any thermal zones or if the thermal subsystem is
  205. * not configured.
  206. */
  207. if (IS_ERR(zone->tzd)) {
  208. devm_kfree(dev, zone);
  209. continue;
  210. }
  211. list_add(&zone->list, &scpi_sensors->thermal_zones);
  212. }
  213. return 0;
  214. unregister_tzd:
  215. unregister_thermal_zones(pdev, scpi_sensors);
  216. return ret;
  217. }
  218. static int scpi_hwmon_remove(struct platform_device *pdev)
  219. {
  220. struct scpi_sensors *scpi_sensors = platform_get_drvdata(pdev);
  221. unregister_thermal_zones(pdev, scpi_sensors);
  222. return 0;
  223. }
  224. static const struct of_device_id scpi_of_match[] = {
  225. {.compatible = "arm,scpi-sensors"},
  226. {},
  227. };
  228. MODULE_DEVICE_TABLE(of, scpi_of_match);
  229. static struct platform_driver scpi_hwmon_platdrv = {
  230. .driver = {
  231. .name = "scpi-hwmon",
  232. .owner = THIS_MODULE,
  233. .of_match_table = scpi_of_match,
  234. },
  235. .probe = scpi_hwmon_probe,
  236. .remove = scpi_hwmon_remove,
  237. };
  238. module_platform_driver(scpi_hwmon_platdrv);
  239. MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
  240. MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
  241. MODULE_LICENSE("GPL v2");