ibmpowernv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #include <asm/cputhreads.h>
  31. #include <asm/smp.h>
  32. #define MAX_ATTR_LEN 32
  33. #define MAX_LABEL_LEN 64
  34. /* Sensor suffix name from DT */
  35. #define DT_FAULT_ATTR_SUFFIX "faulted"
  36. #define DT_DATA_ATTR_SUFFIX "data"
  37. #define DT_THRESHOLD_ATTR_SUFFIX "thrs"
  38. /*
  39. * Enumerates all the types of sensors in the POWERNV platform and does index
  40. * into 'struct sensor_group'
  41. */
  42. enum sensors {
  43. FAN,
  44. TEMP,
  45. POWER_SUPPLY,
  46. POWER_INPUT,
  47. MAX_SENSOR_TYPE,
  48. };
  49. #define INVALID_INDEX (-1U)
  50. static struct sensor_group {
  51. const char *name;
  52. const char *compatible;
  53. struct attribute_group group;
  54. u32 attr_count;
  55. u32 hwmon_index;
  56. } sensor_groups[] = {
  57. {"fan", "ibm,opal-sensor-cooling-fan"},
  58. {"temp", "ibm,opal-sensor-amb-temp"},
  59. {"in", "ibm,opal-sensor-power-supply"},
  60. {"power", "ibm,opal-sensor-power"}
  61. };
  62. struct sensor_data {
  63. u32 id; /* An opaque id of the firmware for each sensor */
  64. u32 hwmon_index;
  65. u32 opal_index;
  66. enum sensors type;
  67. char label[MAX_LABEL_LEN];
  68. char name[MAX_ATTR_LEN];
  69. struct device_attribute dev_attr;
  70. };
  71. struct platform_data {
  72. const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
  73. u32 sensors_count; /* Total count of sensors from each group */
  74. };
  75. static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
  76. char *buf)
  77. {
  78. struct sensor_data *sdata = container_of(devattr, struct sensor_data,
  79. dev_attr);
  80. ssize_t ret;
  81. u32 x;
  82. ret = opal_get_sensor_data(sdata->id, &x);
  83. if (ret)
  84. return ret;
  85. /* Convert temperature to milli-degrees */
  86. if (sdata->type == TEMP)
  87. x *= 1000;
  88. /* Convert power to micro-watts */
  89. else if (sdata->type == POWER_INPUT)
  90. x *= 1000000;
  91. return sprintf(buf, "%u\n", x);
  92. }
  93. static ssize_t show_label(struct device *dev, struct device_attribute *devattr,
  94. char *buf)
  95. {
  96. struct sensor_data *sdata = container_of(devattr, struct sensor_data,
  97. dev_attr);
  98. return sprintf(buf, "%s\n", sdata->label);
  99. }
  100. static int __init get_logical_cpu(int hwcpu)
  101. {
  102. int cpu;
  103. for_each_possible_cpu(cpu)
  104. if (get_hard_smp_processor_id(cpu) == hwcpu)
  105. return cpu;
  106. return -ENOENT;
  107. }
  108. static void __init make_sensor_label(struct device_node *np,
  109. struct sensor_data *sdata,
  110. const char *label)
  111. {
  112. u32 id;
  113. size_t n;
  114. n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
  115. /*
  116. * Core temp pretty print
  117. */
  118. if (!of_property_read_u32(np, "ibm,pir", &id)) {
  119. int cpuid = get_logical_cpu(id);
  120. if (cpuid >= 0)
  121. /*
  122. * The digital thermal sensors are associated
  123. * with a core.
  124. */
  125. n += snprintf(sdata->label + n,
  126. sizeof(sdata->label) - n, " %d",
  127. cpuid);
  128. else
  129. n += snprintf(sdata->label + n,
  130. sizeof(sdata->label) - n, " phy%d", id);
  131. }
  132. /*
  133. * Membuffer pretty print
  134. */
  135. if (!of_property_read_u32(np, "ibm,chip-id", &id))
  136. n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
  137. " %d", id & 0xffff);
  138. }
  139. static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
  140. {
  141. char *hash_pos = strchr(name, '#');
  142. char buf[8] = { 0 };
  143. char *dash_pos;
  144. u32 copy_len;
  145. int err;
  146. if (!hash_pos)
  147. return -EINVAL;
  148. dash_pos = strchr(hash_pos, '-');
  149. if (!dash_pos)
  150. return -EINVAL;
  151. copy_len = dash_pos - hash_pos - 1;
  152. if (copy_len >= sizeof(buf))
  153. return -EINVAL;
  154. strncpy(buf, hash_pos + 1, copy_len);
  155. err = kstrtou32(buf, 10, index);
  156. if (err)
  157. return err;
  158. strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
  159. return 0;
  160. }
  161. static const char *convert_opal_attr_name(enum sensors type,
  162. const char *opal_attr)
  163. {
  164. const char *attr_name = NULL;
  165. if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
  166. attr_name = "fault";
  167. } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
  168. attr_name = "input";
  169. } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
  170. if (type == TEMP)
  171. attr_name = "max";
  172. else if (type == FAN)
  173. attr_name = "min";
  174. }
  175. return attr_name;
  176. }
  177. /*
  178. * This function translates the DT node name into the 'hwmon' attribute name.
  179. * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
  180. * which need to be mapped as fan2_input, temp1_max respectively before
  181. * populating them inside hwmon device class.
  182. */
  183. static const char *parse_opal_node_name(const char *node_name,
  184. enum sensors type, u32 *index)
  185. {
  186. char attr_suffix[MAX_ATTR_LEN];
  187. const char *attr_name;
  188. int err;
  189. err = get_sensor_index_attr(node_name, index, attr_suffix);
  190. if (err)
  191. return ERR_PTR(err);
  192. attr_name = convert_opal_attr_name(type, attr_suffix);
  193. if (!attr_name)
  194. return ERR_PTR(-ENOENT);
  195. return attr_name;
  196. }
  197. static int get_sensor_type(struct device_node *np)
  198. {
  199. enum sensors type;
  200. const char *str;
  201. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  202. if (of_device_is_compatible(np, sensor_groups[type].compatible))
  203. return type;
  204. }
  205. /*
  206. * Let's check if we have a newer device tree
  207. */
  208. if (!of_device_is_compatible(np, "ibm,opal-sensor"))
  209. return MAX_SENSOR_TYPE;
  210. if (of_property_read_string(np, "sensor-type", &str))
  211. return MAX_SENSOR_TYPE;
  212. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  213. if (!strcmp(str, sensor_groups[type].name))
  214. return type;
  215. return MAX_SENSOR_TYPE;
  216. }
  217. static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
  218. struct sensor_data *sdata_table, int count)
  219. {
  220. int i;
  221. /*
  222. * We don't use the OPAL index on newer device trees
  223. */
  224. if (sdata->opal_index != INVALID_INDEX) {
  225. for (i = 0; i < count; i++)
  226. if (sdata_table[i].opal_index == sdata->opal_index &&
  227. sdata_table[i].type == sdata->type)
  228. return sdata_table[i].hwmon_index;
  229. }
  230. return ++sensor_groups[sdata->type].hwmon_index;
  231. }
  232. static int populate_attr_groups(struct platform_device *pdev)
  233. {
  234. struct platform_data *pdata = platform_get_drvdata(pdev);
  235. const struct attribute_group **pgroups = pdata->attr_groups;
  236. struct device_node *opal, *np;
  237. enum sensors type;
  238. opal = of_find_node_by_path("/ibm,opal/sensors");
  239. for_each_child_of_node(opal, np) {
  240. const char *label;
  241. if (np->name == NULL)
  242. continue;
  243. type = get_sensor_type(np);
  244. if (type == MAX_SENSOR_TYPE)
  245. continue;
  246. sensor_groups[type].attr_count++;
  247. /*
  248. * add a new attribute for labels
  249. */
  250. if (!of_property_read_string(np, "label", &label))
  251. sensor_groups[type].attr_count++;
  252. }
  253. of_node_put(opal);
  254. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  255. sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
  256. sizeof(struct attribute *) *
  257. (sensor_groups[type].attr_count + 1),
  258. GFP_KERNEL);
  259. if (!sensor_groups[type].group.attrs)
  260. return -ENOMEM;
  261. pgroups[type] = &sensor_groups[type].group;
  262. pdata->sensors_count += sensor_groups[type].attr_count;
  263. sensor_groups[type].attr_count = 0;
  264. }
  265. return 0;
  266. }
  267. static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
  268. ssize_t (*show)(struct device *dev,
  269. struct device_attribute *attr,
  270. char *buf))
  271. {
  272. snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
  273. sensor_groups[sdata->type].name, sdata->hwmon_index,
  274. attr_name);
  275. sysfs_attr_init(&sdata->dev_attr.attr);
  276. sdata->dev_attr.attr.name = sdata->name;
  277. sdata->dev_attr.attr.mode = S_IRUGO;
  278. sdata->dev_attr.show = show;
  279. }
  280. /*
  281. * Iterate through the device tree for each child of 'sensors' node, create
  282. * a sysfs attribute file, the file is named by translating the DT node name
  283. * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
  284. * etc..
  285. */
  286. static int create_device_attrs(struct platform_device *pdev)
  287. {
  288. struct platform_data *pdata = platform_get_drvdata(pdev);
  289. const struct attribute_group **pgroups = pdata->attr_groups;
  290. struct device_node *opal, *np;
  291. struct sensor_data *sdata;
  292. u32 sensor_id;
  293. enum sensors type;
  294. u32 count = 0;
  295. int err = 0;
  296. opal = of_find_node_by_path("/ibm,opal/sensors");
  297. sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
  298. GFP_KERNEL);
  299. if (!sdata) {
  300. err = -ENOMEM;
  301. goto exit_put_node;
  302. }
  303. for_each_child_of_node(opal, np) {
  304. const char *attr_name;
  305. u32 opal_index;
  306. const char *label;
  307. if (np->name == NULL)
  308. continue;
  309. type = get_sensor_type(np);
  310. if (type == MAX_SENSOR_TYPE)
  311. continue;
  312. /*
  313. * Newer device trees use a "sensor-data" property
  314. * name for input.
  315. */
  316. if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
  317. of_property_read_u32(np, "sensor-data", &sensor_id)) {
  318. dev_info(&pdev->dev,
  319. "'sensor-id' missing in the node '%s'\n",
  320. np->name);
  321. continue;
  322. }
  323. sdata[count].id = sensor_id;
  324. sdata[count].type = type;
  325. /*
  326. * If we can not parse the node name, it means we are
  327. * running on a newer device tree. We can just forget
  328. * about the OPAL index and use a defaut value for the
  329. * hwmon attribute name
  330. */
  331. attr_name = parse_opal_node_name(np->name, type, &opal_index);
  332. if (IS_ERR(attr_name)) {
  333. attr_name = "input";
  334. opal_index = INVALID_INDEX;
  335. }
  336. sdata[count].opal_index = opal_index;
  337. sdata[count].hwmon_index =
  338. get_sensor_hwmon_index(&sdata[count], sdata, count);
  339. create_hwmon_attr(&sdata[count], attr_name, show_sensor);
  340. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  341. &sdata[count++].dev_attr.attr;
  342. if (!of_property_read_string(np, "label", &label)) {
  343. /*
  344. * For the label attribute, we can reuse the
  345. * "properties" of the previous "input"
  346. * attribute. They are related to the same
  347. * sensor.
  348. */
  349. sdata[count].type = type;
  350. sdata[count].opal_index = sdata[count - 1].opal_index;
  351. sdata[count].hwmon_index = sdata[count - 1].hwmon_index;
  352. make_sensor_label(np, &sdata[count], label);
  353. create_hwmon_attr(&sdata[count], "label", show_label);
  354. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  355. &sdata[count++].dev_attr.attr;
  356. }
  357. }
  358. exit_put_node:
  359. of_node_put(opal);
  360. return err;
  361. }
  362. static int ibmpowernv_probe(struct platform_device *pdev)
  363. {
  364. struct platform_data *pdata;
  365. struct device *hwmon_dev;
  366. int err;
  367. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  368. if (!pdata)
  369. return -ENOMEM;
  370. platform_set_drvdata(pdev, pdata);
  371. pdata->sensors_count = 0;
  372. err = populate_attr_groups(pdev);
  373. if (err)
  374. return err;
  375. /* Create sysfs attribute data for each sensor found in the DT */
  376. err = create_device_attrs(pdev);
  377. if (err)
  378. return err;
  379. /* Finally, register with hwmon */
  380. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
  381. pdata,
  382. pdata->attr_groups);
  383. return PTR_ERR_OR_ZERO(hwmon_dev);
  384. }
  385. static const struct platform_device_id opal_sensor_driver_ids[] = {
  386. {
  387. .name = "opal-sensor",
  388. },
  389. { }
  390. };
  391. MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
  392. static const struct of_device_id opal_sensor_match[] = {
  393. { .compatible = "ibm,opal-sensor" },
  394. { },
  395. };
  396. MODULE_DEVICE_TABLE(of, opal_sensor_match);
  397. static struct platform_driver ibmpowernv_driver = {
  398. .probe = ibmpowernv_probe,
  399. .id_table = opal_sensor_driver_ids,
  400. .driver = {
  401. .name = DRVNAME,
  402. .of_match_table = opal_sensor_match,
  403. },
  404. };
  405. module_platform_driver(ibmpowernv_driver);
  406. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  407. MODULE_DESCRIPTION("IBM POWERNV platform sensors");
  408. MODULE_LICENSE("GPL");