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