ibmpowernv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * IBM PowerNV platform sensors for temperature/fan/voltage/power
  3. * Copyright (C) 2014 IBM
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program.
  17. */
  18. #define DRVNAME "ibmpowernv"
  19. #define pr_fmt(fmt) DRVNAME ": " fmt
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/of.h>
  26. #include <linux/slab.h>
  27. #include <linux/platform_device.h>
  28. #include <asm/opal.h>
  29. #include <linux/err.h>
  30. #define MAX_ATTR_LEN 32
  31. /* Sensor suffix name from DT */
  32. #define DT_FAULT_ATTR_SUFFIX "faulted"
  33. #define DT_DATA_ATTR_SUFFIX "data"
  34. #define DT_THRESHOLD_ATTR_SUFFIX "thrs"
  35. /*
  36. * Enumerates all the types of sensors in the POWERNV platform and does index
  37. * into 'struct sensor_group'
  38. */
  39. enum sensors {
  40. FAN,
  41. AMBIENT_TEMP,
  42. POWER_SUPPLY,
  43. POWER_INPUT,
  44. MAX_SENSOR_TYPE,
  45. };
  46. static struct sensor_group {
  47. const char *name;
  48. const char *compatible;
  49. struct attribute_group group;
  50. u32 attr_count;
  51. } sensor_groups[] = {
  52. {"fan", "ibm,opal-sensor-cooling-fan"},
  53. {"temp", "ibm,opal-sensor-amb-temp"},
  54. {"in", "ibm,opal-sensor-power-supply"},
  55. {"power", "ibm,opal-sensor-power"}
  56. };
  57. struct sensor_data {
  58. u32 id; /* An opaque id of the firmware for each sensor */
  59. enum sensors type;
  60. char name[MAX_ATTR_LEN];
  61. struct device_attribute dev_attr;
  62. };
  63. struct platform_data {
  64. const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
  65. u32 sensors_count; /* Total count of sensors from each group */
  66. };
  67. static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
  68. char *buf)
  69. {
  70. struct sensor_data *sdata = container_of(devattr, struct sensor_data,
  71. dev_attr);
  72. ssize_t ret;
  73. u32 x;
  74. ret = opal_get_sensor_data(sdata->id, &x);
  75. if (ret)
  76. return ret;
  77. /* Convert temperature to milli-degrees */
  78. if (sdata->type == AMBIENT_TEMP)
  79. x *= 1000;
  80. /* Convert power to micro-watts */
  81. else if (sdata->type == POWER_INPUT)
  82. x *= 1000000;
  83. return sprintf(buf, "%u\n", x);
  84. }
  85. static int get_sensor_index_attr(const char *name, u32 *index,
  86. char *attr)
  87. {
  88. char *hash_pos = strchr(name, '#');
  89. char buf[8] = { 0 };
  90. char *dash_pos;
  91. u32 copy_len;
  92. int err;
  93. if (!hash_pos)
  94. return -EINVAL;
  95. dash_pos = strchr(hash_pos, '-');
  96. if (!dash_pos)
  97. return -EINVAL;
  98. copy_len = dash_pos - hash_pos - 1;
  99. if (copy_len >= sizeof(buf))
  100. return -EINVAL;
  101. strncpy(buf, hash_pos + 1, copy_len);
  102. err = kstrtou32(buf, 10, index);
  103. if (err)
  104. return err;
  105. strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
  106. return 0;
  107. }
  108. /*
  109. * This function translates the DT node name into the 'hwmon' attribute name.
  110. * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
  111. * which need to be mapped as fan2_input, temp1_max respectively before
  112. * populating them inside hwmon device class.
  113. */
  114. static int create_hwmon_attr_name(struct device *dev, enum sensors type,
  115. const char *node_name,
  116. char *hwmon_attr_name)
  117. {
  118. char attr_suffix[MAX_ATTR_LEN];
  119. char *attr_name;
  120. u32 index;
  121. int err;
  122. err = get_sensor_index_attr(node_name, &index, attr_suffix);
  123. if (err) {
  124. dev_err(dev, "Sensor device node name '%s' is invalid\n",
  125. node_name);
  126. return err;
  127. }
  128. if (!strcmp(attr_suffix, DT_FAULT_ATTR_SUFFIX)) {
  129. attr_name = "fault";
  130. } else if (!strcmp(attr_suffix, DT_DATA_ATTR_SUFFIX)) {
  131. attr_name = "input";
  132. } else if (!strcmp(attr_suffix, DT_THRESHOLD_ATTR_SUFFIX)) {
  133. if (type == AMBIENT_TEMP)
  134. attr_name = "max";
  135. else if (type == FAN)
  136. attr_name = "min";
  137. else
  138. return -ENOENT;
  139. } else {
  140. return -ENOENT;
  141. }
  142. snprintf(hwmon_attr_name, MAX_ATTR_LEN, "%s%d_%s",
  143. sensor_groups[type].name, index, attr_name);
  144. return 0;
  145. }
  146. static int populate_attr_groups(struct platform_device *pdev)
  147. {
  148. struct platform_data *pdata = platform_get_drvdata(pdev);
  149. const struct attribute_group **pgroups = pdata->attr_groups;
  150. struct device_node *opal, *np;
  151. enum sensors type;
  152. opal = of_find_node_by_path("/ibm,opal/sensors");
  153. for_each_child_of_node(opal, np) {
  154. if (np->name == NULL)
  155. continue;
  156. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  157. if (of_device_is_compatible(np,
  158. sensor_groups[type].compatible)) {
  159. sensor_groups[type].attr_count++;
  160. break;
  161. }
  162. }
  163. of_node_put(opal);
  164. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  165. sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
  166. sizeof(struct attribute *) *
  167. (sensor_groups[type].attr_count + 1),
  168. GFP_KERNEL);
  169. if (!sensor_groups[type].group.attrs)
  170. return -ENOMEM;
  171. pgroups[type] = &sensor_groups[type].group;
  172. pdata->sensors_count += sensor_groups[type].attr_count;
  173. sensor_groups[type].attr_count = 0;
  174. }
  175. return 0;
  176. }
  177. /*
  178. * Iterate through the device tree for each child of 'sensors' node, create
  179. * a sysfs attribute file, the file is named by translating the DT node name
  180. * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
  181. * etc..
  182. */
  183. static int create_device_attrs(struct platform_device *pdev)
  184. {
  185. struct platform_data *pdata = platform_get_drvdata(pdev);
  186. const struct attribute_group **pgroups = pdata->attr_groups;
  187. struct device_node *opal, *np;
  188. struct sensor_data *sdata;
  189. u32 sensor_id;
  190. enum sensors type;
  191. u32 count = 0;
  192. int err = 0;
  193. opal = of_find_node_by_path("/ibm,opal/sensors");
  194. sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
  195. GFP_KERNEL);
  196. if (!sdata) {
  197. err = -ENOMEM;
  198. goto exit_put_node;
  199. }
  200. for_each_child_of_node(opal, np) {
  201. if (np->name == NULL)
  202. continue;
  203. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  204. if (of_device_is_compatible(np,
  205. sensor_groups[type].compatible))
  206. break;
  207. if (type == MAX_SENSOR_TYPE)
  208. continue;
  209. if (of_property_read_u32(np, "sensor-id", &sensor_id)) {
  210. dev_info(&pdev->dev,
  211. "'sensor-id' missing in the node '%s'\n",
  212. np->name);
  213. continue;
  214. }
  215. sdata[count].id = sensor_id;
  216. sdata[count].type = type;
  217. err = create_hwmon_attr_name(&pdev->dev, type, np->name,
  218. sdata[count].name);
  219. if (err)
  220. goto exit_put_node;
  221. sysfs_attr_init(&sdata[count].dev_attr.attr);
  222. sdata[count].dev_attr.attr.name = sdata[count].name;
  223. sdata[count].dev_attr.attr.mode = S_IRUGO;
  224. sdata[count].dev_attr.show = show_sensor;
  225. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  226. &sdata[count++].dev_attr.attr;
  227. }
  228. exit_put_node:
  229. of_node_put(opal);
  230. return err;
  231. }
  232. static int ibmpowernv_probe(struct platform_device *pdev)
  233. {
  234. struct platform_data *pdata;
  235. struct device *hwmon_dev;
  236. int err;
  237. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  238. if (!pdata)
  239. return -ENOMEM;
  240. platform_set_drvdata(pdev, pdata);
  241. pdata->sensors_count = 0;
  242. err = populate_attr_groups(pdev);
  243. if (err)
  244. return err;
  245. /* Create sysfs attribute data for each sensor found in the DT */
  246. err = create_device_attrs(pdev);
  247. if (err)
  248. return err;
  249. /* Finally, register with hwmon */
  250. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
  251. pdata,
  252. pdata->attr_groups);
  253. return PTR_ERR_OR_ZERO(hwmon_dev);
  254. }
  255. static const struct platform_device_id opal_sensor_driver_ids[] = {
  256. {
  257. .name = "opal-sensor",
  258. },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
  262. static struct platform_driver ibmpowernv_driver = {
  263. .probe = ibmpowernv_probe,
  264. .id_table = opal_sensor_driver_ids,
  265. .driver = {
  266. .name = DRVNAME,
  267. },
  268. };
  269. module_platform_driver(ibmpowernv_driver);
  270. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  271. MODULE_DESCRIPTION("IBM POWERNV platform sensors");
  272. MODULE_LICENSE("GPL");