ibmpowernv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. Let's print out the range of
  124. * cpu ids corresponding to the hardware
  125. * threads of the core.
  126. */
  127. n += snprintf(sdata->label + n,
  128. sizeof(sdata->label) - n, " %d-%d",
  129. cpuid, cpuid + threads_per_core - 1);
  130. else
  131. n += snprintf(sdata->label + n,
  132. sizeof(sdata->label) - n, " phy%d", id);
  133. }
  134. /*
  135. * Membuffer pretty print
  136. */
  137. if (!of_property_read_u32(np, "ibm,chip-id", &id))
  138. n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
  139. " %d", id & 0xffff);
  140. }
  141. static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
  142. {
  143. char *hash_pos = strchr(name, '#');
  144. char buf[8] = { 0 };
  145. char *dash_pos;
  146. u32 copy_len;
  147. int err;
  148. if (!hash_pos)
  149. return -EINVAL;
  150. dash_pos = strchr(hash_pos, '-');
  151. if (!dash_pos)
  152. return -EINVAL;
  153. copy_len = dash_pos - hash_pos - 1;
  154. if (copy_len >= sizeof(buf))
  155. return -EINVAL;
  156. strncpy(buf, hash_pos + 1, copy_len);
  157. err = kstrtou32(buf, 10, index);
  158. if (err)
  159. return err;
  160. strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
  161. return 0;
  162. }
  163. static const char *convert_opal_attr_name(enum sensors type,
  164. const char *opal_attr)
  165. {
  166. const char *attr_name = NULL;
  167. if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
  168. attr_name = "fault";
  169. } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
  170. attr_name = "input";
  171. } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
  172. if (type == TEMP)
  173. attr_name = "max";
  174. else if (type == FAN)
  175. attr_name = "min";
  176. }
  177. return attr_name;
  178. }
  179. /*
  180. * This function translates the DT node name into the 'hwmon' attribute name.
  181. * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
  182. * which need to be mapped as fan2_input, temp1_max respectively before
  183. * populating them inside hwmon device class.
  184. */
  185. static const char *parse_opal_node_name(const char *node_name,
  186. enum sensors type, u32 *index)
  187. {
  188. char attr_suffix[MAX_ATTR_LEN];
  189. const char *attr_name;
  190. int err;
  191. err = get_sensor_index_attr(node_name, index, attr_suffix);
  192. if (err)
  193. return ERR_PTR(err);
  194. attr_name = convert_opal_attr_name(type, attr_suffix);
  195. if (!attr_name)
  196. return ERR_PTR(-ENOENT);
  197. return attr_name;
  198. }
  199. static int get_sensor_type(struct device_node *np)
  200. {
  201. enum sensors type;
  202. const char *str;
  203. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  204. if (of_device_is_compatible(np, sensor_groups[type].compatible))
  205. return type;
  206. }
  207. /*
  208. * Let's check if we have a newer device tree
  209. */
  210. if (!of_device_is_compatible(np, "ibm,opal-sensor"))
  211. return MAX_SENSOR_TYPE;
  212. if (of_property_read_string(np, "sensor-type", &str))
  213. return MAX_SENSOR_TYPE;
  214. for (type = 0; type < MAX_SENSOR_TYPE; type++)
  215. if (!strcmp(str, sensor_groups[type].name))
  216. return type;
  217. return MAX_SENSOR_TYPE;
  218. }
  219. static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
  220. struct sensor_data *sdata_table, int count)
  221. {
  222. int i;
  223. /*
  224. * We don't use the OPAL index on newer device trees
  225. */
  226. if (sdata->opal_index != INVALID_INDEX) {
  227. for (i = 0; i < count; i++)
  228. if (sdata_table[i].opal_index == sdata->opal_index &&
  229. sdata_table[i].type == sdata->type)
  230. return sdata_table[i].hwmon_index;
  231. }
  232. return ++sensor_groups[sdata->type].hwmon_index;
  233. }
  234. static int populate_attr_groups(struct platform_device *pdev)
  235. {
  236. struct platform_data *pdata = platform_get_drvdata(pdev);
  237. const struct attribute_group **pgroups = pdata->attr_groups;
  238. struct device_node *opal, *np;
  239. enum sensors type;
  240. opal = of_find_node_by_path("/ibm,opal/sensors");
  241. for_each_child_of_node(opal, np) {
  242. const char *label;
  243. if (np->name == NULL)
  244. continue;
  245. type = get_sensor_type(np);
  246. if (type == MAX_SENSOR_TYPE)
  247. continue;
  248. sensor_groups[type].attr_count++;
  249. /*
  250. * add a new attribute for labels
  251. */
  252. if (!of_property_read_string(np, "label", &label))
  253. sensor_groups[type].attr_count++;
  254. }
  255. of_node_put(opal);
  256. for (type = 0; type < MAX_SENSOR_TYPE; type++) {
  257. sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
  258. sizeof(struct attribute *) *
  259. (sensor_groups[type].attr_count + 1),
  260. GFP_KERNEL);
  261. if (!sensor_groups[type].group.attrs)
  262. return -ENOMEM;
  263. pgroups[type] = &sensor_groups[type].group;
  264. pdata->sensors_count += sensor_groups[type].attr_count;
  265. sensor_groups[type].attr_count = 0;
  266. }
  267. return 0;
  268. }
  269. static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
  270. ssize_t (*show)(struct device *dev,
  271. struct device_attribute *attr,
  272. char *buf))
  273. {
  274. snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
  275. sensor_groups[sdata->type].name, sdata->hwmon_index,
  276. attr_name);
  277. sysfs_attr_init(&sdata->dev_attr.attr);
  278. sdata->dev_attr.attr.name = sdata->name;
  279. sdata->dev_attr.attr.mode = S_IRUGO;
  280. sdata->dev_attr.show = show;
  281. }
  282. /*
  283. * Iterate through the device tree for each child of 'sensors' node, create
  284. * a sysfs attribute file, the file is named by translating the DT node name
  285. * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
  286. * etc..
  287. */
  288. static int create_device_attrs(struct platform_device *pdev)
  289. {
  290. struct platform_data *pdata = platform_get_drvdata(pdev);
  291. const struct attribute_group **pgroups = pdata->attr_groups;
  292. struct device_node *opal, *np;
  293. struct sensor_data *sdata;
  294. u32 sensor_id;
  295. enum sensors type;
  296. u32 count = 0;
  297. int err = 0;
  298. opal = of_find_node_by_path("/ibm,opal/sensors");
  299. sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
  300. GFP_KERNEL);
  301. if (!sdata) {
  302. err = -ENOMEM;
  303. goto exit_put_node;
  304. }
  305. for_each_child_of_node(opal, np) {
  306. const char *attr_name;
  307. u32 opal_index;
  308. const char *label;
  309. if (np->name == NULL)
  310. continue;
  311. type = get_sensor_type(np);
  312. if (type == MAX_SENSOR_TYPE)
  313. continue;
  314. /*
  315. * Newer device trees use a "sensor-data" property
  316. * name for input.
  317. */
  318. if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
  319. of_property_read_u32(np, "sensor-data", &sensor_id)) {
  320. dev_info(&pdev->dev,
  321. "'sensor-id' missing in the node '%s'\n",
  322. np->name);
  323. continue;
  324. }
  325. sdata[count].id = sensor_id;
  326. sdata[count].type = type;
  327. /*
  328. * If we can not parse the node name, it means we are
  329. * running on a newer device tree. We can just forget
  330. * about the OPAL index and use a defaut value for the
  331. * hwmon attribute name
  332. */
  333. attr_name = parse_opal_node_name(np->name, type, &opal_index);
  334. if (IS_ERR(attr_name)) {
  335. attr_name = "input";
  336. opal_index = INVALID_INDEX;
  337. }
  338. sdata[count].opal_index = opal_index;
  339. sdata[count].hwmon_index =
  340. get_sensor_hwmon_index(&sdata[count], sdata, count);
  341. create_hwmon_attr(&sdata[count], attr_name, show_sensor);
  342. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  343. &sdata[count++].dev_attr.attr;
  344. if (!of_property_read_string(np, "label", &label)) {
  345. /*
  346. * For the label attribute, we can reuse the
  347. * "properties" of the previous "input"
  348. * attribute. They are related to the same
  349. * sensor.
  350. */
  351. sdata[count].type = type;
  352. sdata[count].opal_index = sdata[count - 1].opal_index;
  353. sdata[count].hwmon_index = sdata[count - 1].hwmon_index;
  354. make_sensor_label(np, &sdata[count], label);
  355. create_hwmon_attr(&sdata[count], "label", show_label);
  356. pgroups[type]->attrs[sensor_groups[type].attr_count++] =
  357. &sdata[count++].dev_attr.attr;
  358. }
  359. }
  360. exit_put_node:
  361. of_node_put(opal);
  362. return err;
  363. }
  364. static int ibmpowernv_probe(struct platform_device *pdev)
  365. {
  366. struct platform_data *pdata;
  367. struct device *hwmon_dev;
  368. int err;
  369. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  370. if (!pdata)
  371. return -ENOMEM;
  372. platform_set_drvdata(pdev, pdata);
  373. pdata->sensors_count = 0;
  374. err = populate_attr_groups(pdev);
  375. if (err)
  376. return err;
  377. /* Create sysfs attribute data for each sensor found in the DT */
  378. err = create_device_attrs(pdev);
  379. if (err)
  380. return err;
  381. /* Finally, register with hwmon */
  382. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
  383. pdata,
  384. pdata->attr_groups);
  385. return PTR_ERR_OR_ZERO(hwmon_dev);
  386. }
  387. static const struct platform_device_id opal_sensor_driver_ids[] = {
  388. {
  389. .name = "opal-sensor",
  390. },
  391. { }
  392. };
  393. MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
  394. static struct platform_driver ibmpowernv_driver = {
  395. .probe = ibmpowernv_probe,
  396. .id_table = opal_sensor_driver_ids,
  397. .driver = {
  398. .name = DRVNAME,
  399. },
  400. };
  401. module_platform_driver(ibmpowernv_driver);
  402. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  403. MODULE_DESCRIPTION("IBM POWERNV platform sensors");
  404. MODULE_LICENSE("GPL");