ibmpowernv.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /* Platform device representing all the ibmpowernv sensors */
  68. static struct platform_device *pdevice;
  69. static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
  70. char *buf)
  71. {
  72. struct sensor_data *sdata = container_of(devattr, struct sensor_data,
  73. dev_attr);
  74. ssize_t ret;
  75. u32 x;
  76. ret = opal_get_sensor_data(sdata->id, &x);
  77. if (ret)
  78. return ret;
  79. /* Convert temperature to milli-degrees */
  80. if (sdata->type == AMBIENT_TEMP)
  81. x *= 1000;
  82. /* Convert power to micro-watts */
  83. else if (sdata->type == POWER_INPUT)
  84. x *= 1000000;
  85. return sprintf(buf, "%u\n", x);
  86. }
  87. static int __init get_sensor_index_attr(const char *name, u32 *index,
  88. char *attr)
  89. {
  90. char *hash_pos = strchr(name, '#');
  91. char buf[8] = { 0 };
  92. char *dash_pos;
  93. u32 copy_len;
  94. int err;
  95. if (!hash_pos)
  96. return -EINVAL;
  97. dash_pos = strchr(hash_pos, '-');
  98. if (!dash_pos)
  99. return -EINVAL;
  100. copy_len = dash_pos - hash_pos - 1;
  101. if (copy_len >= sizeof(buf))
  102. return -EINVAL;
  103. strncpy(buf, hash_pos + 1, copy_len);
  104. err = kstrtou32(buf, 10, index);
  105. if (err)
  106. return err;
  107. strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
  108. return 0;
  109. }
  110. /*
  111. * This function translates the DT node name into the 'hwmon' attribute name.
  112. * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
  113. * which need to be mapped as fan2_input, temp1_max respectively before
  114. * populating them inside hwmon device class.
  115. */
  116. static int __init create_hwmon_attr_name(struct device *dev, enum sensors type,
  117. const char *node_name,
  118. char *hwmon_attr_name)
  119. {
  120. char attr_suffix[MAX_ATTR_LEN];
  121. char *attr_name;
  122. u32 index;
  123. int err;
  124. err = get_sensor_index_attr(node_name, &index, attr_suffix);
  125. if (err) {
  126. dev_err(dev, "Sensor device node name '%s' is invalid\n",
  127. node_name);
  128. return err;
  129. }
  130. if (!strcmp(attr_suffix, DT_FAULT_ATTR_SUFFIX)) {
  131. attr_name = "fault";
  132. } else if (!strcmp(attr_suffix, DT_DATA_ATTR_SUFFIX)) {
  133. attr_name = "input";
  134. } else if (!strcmp(attr_suffix, DT_THRESHOLD_ATTR_SUFFIX)) {
  135. if (type == AMBIENT_TEMP)
  136. attr_name = "max";
  137. else if (type == FAN)
  138. attr_name = "min";
  139. else
  140. return -ENOENT;
  141. } else {
  142. return -ENOENT;
  143. }
  144. snprintf(hwmon_attr_name, MAX_ATTR_LEN, "%s%d_%s",
  145. sensor_groups[type].name, index, attr_name);
  146. return 0;
  147. }
  148. static int __init populate_attr_groups(struct platform_device *pdev)
  149. {
  150. struct platform_data *pdata = platform_get_drvdata(pdev);
  151. const struct attribute_group **pgroups = pdata->attr_groups;
  152. struct device_node *opal, *np;
  153. enum sensors type;
  154. opal = of_find_node_by_path("/ibm,opal/sensors");
  155. if (!opal) {
  156. dev_err(&pdev->dev, "Opal node 'sensors' not found\n");
  157. return -ENODEV;
  158. }
  159. for_each_child_of_node(opal, np) {
  160. if (np->name == NULL)
  161. continue;
  162. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  163. if (of_device_is_compatible(np,
  164. sensor_groups[type].compatible)) {
  165. sensor_groups[type].attr_count++;
  166. break;
  167. }
  168. }
  169. of_node_put(opal);
  170. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  171. sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
  172. sizeof(struct attribute *) *
  173. (sensor_groups[type].attr_count + 1),
  174. GFP_KERNEL);
  175. if (!sensor_groups[type].group.attrs)
  176. return -ENOMEM;
  177. pgroups[type] = &sensor_groups[type].group;
  178. pdata->sensors_count += sensor_groups[type].attr_count;
  179. sensor_groups[type].attr_count = 0;
  180. }
  181. return 0;
  182. }
  183. /*
  184. * Iterate through the device tree for each child of 'sensors' node, create
  185. * a sysfs attribute file, the file is named by translating the DT node name
  186. * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
  187. * etc..
  188. */
  189. static int __init create_device_attrs(struct platform_device *pdev)
  190. {
  191. struct platform_data *pdata = platform_get_drvdata(pdev);
  192. const struct attribute_group **pgroups = pdata->attr_groups;
  193. struct device_node *opal, *np;
  194. struct sensor_data *sdata;
  195. u32 sensor_id;
  196. enum sensors type;
  197. u32 count = 0;
  198. int err = 0;
  199. opal = of_find_node_by_path("/ibm,opal/sensors");
  200. sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
  201. GFP_KERNEL);
  202. if (!sdata) {
  203. err = -ENOMEM;
  204. goto exit_put_node;
  205. }
  206. for_each_child_of_node(opal, np) {
  207. if (np->name == NULL)
  208. continue;
  209. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  210. if (of_device_is_compatible(np,
  211. sensor_groups[type].compatible))
  212. break;
  213. if (type == MAX_SENSOR_TYPE)
  214. continue;
  215. if (of_property_read_u32(np, "sensor-id", &sensor_id)) {
  216. dev_info(&pdev->dev,
  217. "'sensor-id' missing in the node '%s'\n",
  218. np->name);
  219. continue;
  220. }
  221. sdata[count].id = sensor_id;
  222. sdata[count].type = type;
  223. err = create_hwmon_attr_name(&pdev->dev, type, np->name,
  224. sdata[count].name);
  225. if (err)
  226. goto exit_put_node;
  227. sysfs_attr_init(&sdata[count].dev_attr.attr);
  228. sdata[count].dev_attr.attr.name = sdata[count].name;
  229. sdata[count].dev_attr.attr.mode = S_IRUGO;
  230. sdata[count].dev_attr.show = show_sensor;
  231. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  232. &sdata[count++].dev_attr.attr;
  233. }
  234. exit_put_node:
  235. of_node_put(opal);
  236. return err;
  237. }
  238. static int __init ibmpowernv_probe(struct platform_device *pdev)
  239. {
  240. struct platform_data *pdata;
  241. struct device *hwmon_dev;
  242. int err;
  243. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  244. if (!pdata)
  245. return -ENOMEM;
  246. platform_set_drvdata(pdev, pdata);
  247. pdata->sensors_count = 0;
  248. err = populate_attr_groups(pdev);
  249. if (err)
  250. return err;
  251. /* Create sysfs attribute data for each sensor found in the DT */
  252. err = create_device_attrs(pdev);
  253. if (err)
  254. return err;
  255. /* Finally, register with hwmon */
  256. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
  257. pdata,
  258. pdata->attr_groups);
  259. return PTR_ERR_OR_ZERO(hwmon_dev);
  260. }
  261. static struct platform_driver ibmpowernv_driver = {
  262. .driver = {
  263. .owner = THIS_MODULE,
  264. .name = DRVNAME,
  265. },
  266. };
  267. static int __init ibmpowernv_init(void)
  268. {
  269. int err;
  270. pdevice = platform_device_alloc(DRVNAME, 0);
  271. if (!pdevice) {
  272. pr_err("Device allocation failed\n");
  273. err = -ENOMEM;
  274. goto exit;
  275. }
  276. err = platform_device_add(pdevice);
  277. if (err) {
  278. pr_err("Device addition failed (%d)\n", err);
  279. goto exit_device_put;
  280. }
  281. err = platform_driver_probe(&ibmpowernv_driver, ibmpowernv_probe);
  282. if (err) {
  283. pr_err("Platfrom driver probe failed\n");
  284. goto exit_device_del;
  285. }
  286. return 0;
  287. exit_device_del:
  288. platform_device_del(pdevice);
  289. exit_device_put:
  290. platform_device_put(pdevice);
  291. exit:
  292. return err;
  293. }
  294. static void __exit ibmpowernv_exit(void)
  295. {
  296. platform_driver_unregister(&ibmpowernv_driver);
  297. platform_device_unregister(pdevice);
  298. }
  299. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  300. MODULE_DESCRIPTION("IBM POWERNV platform sensors");
  301. MODULE_LICENSE("GPL");
  302. module_init(ibmpowernv_init);
  303. module_exit(ibmpowernv_exit);