scpi-hwmon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. int sensor_id;
  32. struct scpi_sensors *scpi_sensors;
  33. };
  34. struct scpi_sensors {
  35. struct scpi_ops *scpi_ops;
  36. struct sensor_data *data;
  37. struct list_head thermal_zones;
  38. struct attribute **attrs;
  39. struct attribute_group group;
  40. const struct attribute_group *groups[2];
  41. };
  42. static int scpi_read_temp(void *dev, int *temp)
  43. {
  44. struct scpi_thermal_zone *zone = dev;
  45. struct scpi_sensors *scpi_sensors = zone->scpi_sensors;
  46. struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
  47. struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id];
  48. u64 value;
  49. int ret;
  50. ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
  51. if (ret)
  52. return ret;
  53. *temp = value;
  54. return 0;
  55. }
  56. /* hwmon callback functions */
  57. static ssize_t
  58. scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
  59. {
  60. struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
  61. struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
  62. struct sensor_data *sensor;
  63. u64 value;
  64. int ret;
  65. sensor = container_of(attr, struct sensor_data, dev_attr_input);
  66. ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
  67. if (ret)
  68. return ret;
  69. return sprintf(buf, "%llu\n", value);
  70. }
  71. static ssize_t
  72. scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
  73. {
  74. struct sensor_data *sensor;
  75. sensor = container_of(attr, struct sensor_data, dev_attr_label);
  76. return sprintf(buf, "%s\n", sensor->info.name);
  77. }
  78. static struct thermal_zone_of_device_ops scpi_sensor_ops = {
  79. .get_temp = scpi_read_temp,
  80. };
  81. static int scpi_hwmon_probe(struct platform_device *pdev)
  82. {
  83. u16 nr_sensors, i;
  84. int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
  85. int num_energy = 0;
  86. struct scpi_ops *scpi_ops;
  87. struct device *hwdev, *dev = &pdev->dev;
  88. struct scpi_sensors *scpi_sensors;
  89. int idx, ret;
  90. scpi_ops = get_scpi_ops();
  91. if (!scpi_ops)
  92. return -EPROBE_DEFER;
  93. ret = scpi_ops->sensor_get_capability(&nr_sensors);
  94. if (ret)
  95. return ret;
  96. if (!nr_sensors)
  97. return -ENODEV;
  98. scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
  99. if (!scpi_sensors)
  100. return -ENOMEM;
  101. scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
  102. sizeof(*scpi_sensors->data), GFP_KERNEL);
  103. if (!scpi_sensors->data)
  104. return -ENOMEM;
  105. scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
  106. sizeof(*scpi_sensors->attrs), GFP_KERNEL);
  107. if (!scpi_sensors->attrs)
  108. return -ENOMEM;
  109. scpi_sensors->scpi_ops = scpi_ops;
  110. for (i = 0, idx = 0; i < nr_sensors; i++) {
  111. struct sensor_data *sensor = &scpi_sensors->data[idx];
  112. ret = scpi_ops->sensor_get_info(i, &sensor->info);
  113. if (ret)
  114. return ret;
  115. switch (sensor->info.class) {
  116. case TEMPERATURE:
  117. snprintf(sensor->input, sizeof(sensor->input),
  118. "temp%d_input", num_temp + 1);
  119. snprintf(sensor->label, sizeof(sensor->input),
  120. "temp%d_label", num_temp + 1);
  121. num_temp++;
  122. break;
  123. case VOLTAGE:
  124. snprintf(sensor->input, sizeof(sensor->input),
  125. "in%d_input", num_volt);
  126. snprintf(sensor->label, sizeof(sensor->input),
  127. "in%d_label", num_volt);
  128. num_volt++;
  129. break;
  130. case CURRENT:
  131. snprintf(sensor->input, sizeof(sensor->input),
  132. "curr%d_input", num_current + 1);
  133. snprintf(sensor->label, sizeof(sensor->input),
  134. "curr%d_label", num_current + 1);
  135. num_current++;
  136. break;
  137. case POWER:
  138. snprintf(sensor->input, sizeof(sensor->input),
  139. "power%d_input", num_power + 1);
  140. snprintf(sensor->label, sizeof(sensor->input),
  141. "power%d_label", num_power + 1);
  142. num_power++;
  143. break;
  144. case ENERGY:
  145. snprintf(sensor->input, sizeof(sensor->input),
  146. "energy%d_input", num_energy + 1);
  147. snprintf(sensor->label, sizeof(sensor->input),
  148. "energy%d_label", num_energy + 1);
  149. num_energy++;
  150. break;
  151. default:
  152. continue;
  153. }
  154. sensor->dev_attr_input.attr.mode = S_IRUGO;
  155. sensor->dev_attr_input.show = scpi_show_sensor;
  156. sensor->dev_attr_input.attr.name = sensor->input;
  157. sensor->dev_attr_label.attr.mode = S_IRUGO;
  158. sensor->dev_attr_label.show = scpi_show_label;
  159. sensor->dev_attr_label.attr.name = sensor->label;
  160. scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
  161. scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
  162. sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
  163. sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
  164. idx++;
  165. }
  166. scpi_sensors->group.attrs = scpi_sensors->attrs;
  167. scpi_sensors->groups[0] = &scpi_sensors->group;
  168. platform_set_drvdata(pdev, scpi_sensors);
  169. hwdev = devm_hwmon_device_register_with_groups(dev,
  170. "scpi_sensors", scpi_sensors, scpi_sensors->groups);
  171. if (IS_ERR(hwdev))
  172. return PTR_ERR(hwdev);
  173. /*
  174. * Register the temperature sensors with the thermal framework
  175. * to allow their usage in setting up the thermal zones from
  176. * device tree.
  177. *
  178. * NOTE: Not all temperature sensors maybe used for thermal
  179. * control
  180. */
  181. INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
  182. for (i = 0; i < nr_sensors; i++) {
  183. struct sensor_data *sensor = &scpi_sensors->data[i];
  184. struct thermal_zone_device *z;
  185. struct scpi_thermal_zone *zone;
  186. if (sensor->info.class != TEMPERATURE)
  187. continue;
  188. zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
  189. if (!zone)
  190. return -ENOMEM;
  191. zone->sensor_id = i;
  192. zone->scpi_sensors = scpi_sensors;
  193. z = devm_thermal_zone_of_sensor_register(dev,
  194. sensor->info.sensor_id,
  195. zone,
  196. &scpi_sensor_ops);
  197. /*
  198. * The call to thermal_zone_of_sensor_register returns
  199. * an error for sensors that are not associated with
  200. * any thermal zones or if the thermal subsystem is
  201. * not configured.
  202. */
  203. if (IS_ERR(z)) {
  204. devm_kfree(dev, zone);
  205. continue;
  206. }
  207. }
  208. return 0;
  209. }
  210. static const struct of_device_id scpi_of_match[] = {
  211. {.compatible = "arm,scpi-sensors"},
  212. {},
  213. };
  214. MODULE_DEVICE_TABLE(of, scpi_of_match);
  215. static struct platform_driver scpi_hwmon_platdrv = {
  216. .driver = {
  217. .name = "scpi-hwmon",
  218. .of_match_table = scpi_of_match,
  219. },
  220. .probe = scpi_hwmon_probe,
  221. };
  222. module_platform_driver(scpi_hwmon_platdrv);
  223. MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
  224. MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
  225. MODULE_LICENSE("GPL v2");