scpi-hwmon.c 8.1 KB

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