hwmon.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  4. *
  5. * This file defines the sysfs class "hwmon", for use by sensors drivers.
  6. *
  7. * Copyright (C) 2005 Mark M. Hoffman <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/bitops.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/gfp.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/idr.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/property.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/thermal.h>
  23. #define CREATE_TRACE_POINTS
  24. #include <trace/events/hwmon.h>
  25. #define HWMON_ID_PREFIX "hwmon"
  26. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  27. struct hwmon_device {
  28. const char *name;
  29. const char *label;
  30. struct device dev;
  31. const struct hwmon_chip_info *chip;
  32. struct list_head tzdata;
  33. struct attribute_group group;
  34. const struct attribute_group **groups;
  35. };
  36. #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
  37. #define MAX_SYSFS_ATTR_NAME_LENGTH 32
  38. struct hwmon_device_attribute {
  39. struct device_attribute dev_attr;
  40. const struct hwmon_ops *ops;
  41. enum hwmon_sensor_types type;
  42. u32 attr;
  43. int index;
  44. char name[MAX_SYSFS_ATTR_NAME_LENGTH];
  45. };
  46. #define to_hwmon_attr(d) \
  47. container_of(d, struct hwmon_device_attribute, dev_attr)
  48. #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
  49. /*
  50. * Thermal zone information
  51. */
  52. struct hwmon_thermal_data {
  53. struct list_head node; /* hwmon tzdata list entry */
  54. struct device *dev; /* Reference to hwmon device */
  55. int index; /* sensor index */
  56. struct thermal_zone_device *tzd;/* thermal zone device */
  57. };
  58. static ssize_t
  59. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  60. {
  61. return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
  62. }
  63. static DEVICE_ATTR_RO(name);
  64. static ssize_t
  65. label_show(struct device *dev, struct device_attribute *attr, char *buf)
  66. {
  67. return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
  68. }
  69. static DEVICE_ATTR_RO(label);
  70. static struct attribute *hwmon_dev_attrs[] = {
  71. &dev_attr_name.attr,
  72. &dev_attr_label.attr,
  73. NULL
  74. };
  75. static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
  76. struct attribute *attr, int n)
  77. {
  78. struct device *dev = kobj_to_dev(kobj);
  79. struct hwmon_device *hdev = to_hwmon_device(dev);
  80. if (attr == &dev_attr_name.attr && hdev->name == NULL)
  81. return 0;
  82. if (attr == &dev_attr_label.attr && hdev->label == NULL)
  83. return 0;
  84. return attr->mode;
  85. }
  86. static const struct attribute_group hwmon_dev_attr_group = {
  87. .attrs = hwmon_dev_attrs,
  88. .is_visible = hwmon_dev_attr_is_visible,
  89. };
  90. static const struct attribute_group *hwmon_dev_attr_groups[] = {
  91. &hwmon_dev_attr_group,
  92. NULL
  93. };
  94. static void hwmon_free_attrs(struct attribute **attrs)
  95. {
  96. int i;
  97. for (i = 0; attrs[i]; i++) {
  98. struct device_attribute *dattr = to_dev_attr(attrs[i]);
  99. struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
  100. kfree(hattr);
  101. }
  102. kfree(attrs);
  103. }
  104. static void hwmon_dev_release(struct device *dev)
  105. {
  106. struct hwmon_device *hwdev = to_hwmon_device(dev);
  107. if (hwdev->group.attrs)
  108. hwmon_free_attrs(hwdev->group.attrs);
  109. kfree(hwdev->groups);
  110. kfree(hwdev->label);
  111. kfree(hwdev);
  112. }
  113. static struct class hwmon_class = {
  114. .name = "hwmon",
  115. .owner = THIS_MODULE,
  116. .dev_groups = hwmon_dev_attr_groups,
  117. .dev_release = hwmon_dev_release,
  118. };
  119. static DEFINE_IDA(hwmon_ida);
  120. /* Thermal zone handling */
  121. /*
  122. * The complex conditional is necessary to avoid a cyclic dependency
  123. * between hwmon and thermal_sys modules.
  124. */
  125. #ifdef CONFIG_THERMAL_OF
  126. static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
  127. {
  128. struct hwmon_thermal_data *tdata = tz->devdata;
  129. struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
  130. int ret;
  131. long t;
  132. ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
  133. tdata->index, &t);
  134. if (ret < 0)
  135. return ret;
  136. *temp = t;
  137. return 0;
  138. }
  139. static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
  140. {
  141. struct hwmon_thermal_data *tdata = tz->devdata;
  142. struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
  143. const struct hwmon_chip_info *chip = hwdev->chip;
  144. const struct hwmon_channel_info **info = chip->info;
  145. unsigned int i;
  146. int err;
  147. if (!chip->ops->write)
  148. return 0;
  149. for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
  150. continue;
  151. if (!info[i])
  152. return 0;
  153. if (info[i]->config[tdata->index] & HWMON_T_MIN) {
  154. err = chip->ops->write(tdata->dev, hwmon_temp,
  155. hwmon_temp_min, tdata->index, low);
  156. if (err && err != -EOPNOTSUPP)
  157. return err;
  158. }
  159. if (info[i]->config[tdata->index] & HWMON_T_MAX) {
  160. err = chip->ops->write(tdata->dev, hwmon_temp,
  161. hwmon_temp_max, tdata->index, high);
  162. if (err && err != -EOPNOTSUPP)
  163. return err;
  164. }
  165. return 0;
  166. }
  167. static const struct thermal_zone_device_ops hwmon_thermal_ops = {
  168. .get_temp = hwmon_thermal_get_temp,
  169. .set_trips = hwmon_thermal_set_trips,
  170. };
  171. static void hwmon_thermal_remove_sensor(void *data)
  172. {
  173. list_del(data);
  174. }
  175. static int hwmon_thermal_add_sensor(struct device *dev, int index)
  176. {
  177. struct hwmon_device *hwdev = to_hwmon_device(dev);
  178. struct hwmon_thermal_data *tdata;
  179. struct thermal_zone_device *tzd;
  180. int err;
  181. tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
  182. if (!tdata)
  183. return -ENOMEM;
  184. tdata->dev = dev;
  185. tdata->index = index;
  186. tzd = devm_thermal_of_zone_register(dev, index, tdata,
  187. &hwmon_thermal_ops);
  188. if (IS_ERR(tzd)) {
  189. if (PTR_ERR(tzd) != -ENODEV)
  190. return PTR_ERR(tzd);
  191. dev_info(dev, "temp%d_input not attached to any thermal zone\n",
  192. index + 1);
  193. devm_kfree(dev, tdata);
  194. return 0;
  195. }
  196. err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
  197. if (err)
  198. return err;
  199. tdata->tzd = tzd;
  200. list_add(&tdata->node, &hwdev->tzdata);
  201. return 0;
  202. }
  203. static int hwmon_thermal_register_sensors(struct device *dev)
  204. {
  205. struct hwmon_device *hwdev = to_hwmon_device(dev);
  206. const struct hwmon_chip_info *chip = hwdev->chip;
  207. const struct hwmon_channel_info **info = chip->info;
  208. void *drvdata = dev_get_drvdata(dev);
  209. int i;
  210. for (i = 1; info[i]; i++) {
  211. int j;
  212. if (info[i]->type != hwmon_temp)
  213. continue;
  214. for (j = 0; info[i]->config[j]; j++) {
  215. int err;
  216. if (!(info[i]->config[j] & HWMON_T_INPUT) ||
  217. !chip->ops->is_visible(drvdata, hwmon_temp,
  218. hwmon_temp_input, j))
  219. continue;
  220. err = hwmon_thermal_add_sensor(dev, j);
  221. if (err)
  222. return err;
  223. }
  224. }
  225. return 0;
  226. }
  227. static void hwmon_thermal_notify(struct device *dev, int index)
  228. {
  229. struct hwmon_device *hwdev = to_hwmon_device(dev);
  230. struct hwmon_thermal_data *tzdata;
  231. list_for_each_entry(tzdata, &hwdev->tzdata, node) {
  232. if (tzdata->index == index) {
  233. thermal_zone_device_update(tzdata->tzd,
  234. THERMAL_EVENT_UNSPECIFIED);
  235. }
  236. }
  237. }
  238. #else
  239. static int hwmon_thermal_register_sensors(struct device *dev)
  240. {
  241. return 0;
  242. }
  243. static void hwmon_thermal_notify(struct device *dev, int index) { }
  244. #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
  245. static int hwmon_attr_base(enum hwmon_sensor_types type)
  246. {
  247. if (type == hwmon_in || type == hwmon_intrusion)
  248. return 0;
  249. return 1;
  250. }
  251. /* sysfs attribute management */
  252. static ssize_t hwmon_attr_show(struct device *dev,
  253. struct device_attribute *devattr, char *buf)
  254. {
  255. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  256. long val;
  257. int ret;
  258. ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
  259. &val);
  260. if (ret < 0)
  261. return ret;
  262. trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
  263. hattr->name, val);
  264. return sprintf(buf, "%ld\n", val);
  265. }
  266. static ssize_t hwmon_attr_show_string(struct device *dev,
  267. struct device_attribute *devattr,
  268. char *buf)
  269. {
  270. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  271. enum hwmon_sensor_types type = hattr->type;
  272. const char *s;
  273. int ret;
  274. ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
  275. hattr->index, &s);
  276. if (ret < 0)
  277. return ret;
  278. trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
  279. hattr->name, s);
  280. return sprintf(buf, "%s\n", s);
  281. }
  282. static ssize_t hwmon_attr_store(struct device *dev,
  283. struct device_attribute *devattr,
  284. const char *buf, size_t count)
  285. {
  286. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  287. long val;
  288. int ret;
  289. ret = kstrtol(buf, 10, &val);
  290. if (ret < 0)
  291. return ret;
  292. ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
  293. val);
  294. if (ret < 0)
  295. return ret;
  296. trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
  297. hattr->name, val);
  298. return count;
  299. }
  300. static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
  301. {
  302. return (type == hwmon_temp && attr == hwmon_temp_label) ||
  303. (type == hwmon_in && attr == hwmon_in_label) ||
  304. (type == hwmon_curr && attr == hwmon_curr_label) ||
  305. (type == hwmon_power && attr == hwmon_power_label) ||
  306. (type == hwmon_energy && attr == hwmon_energy_label) ||
  307. (type == hwmon_humidity && attr == hwmon_humidity_label) ||
  308. (type == hwmon_fan && attr == hwmon_fan_label);
  309. }
  310. static struct attribute *hwmon_genattr(const void *drvdata,
  311. enum hwmon_sensor_types type,
  312. u32 attr,
  313. int index,
  314. const char *template,
  315. const struct hwmon_ops *ops)
  316. {
  317. struct hwmon_device_attribute *hattr;
  318. struct device_attribute *dattr;
  319. struct attribute *a;
  320. umode_t mode;
  321. const char *name;
  322. bool is_string = is_string_attr(type, attr);
  323. /* The attribute is invisible if there is no template string */
  324. if (!template)
  325. return ERR_PTR(-ENOENT);
  326. mode = ops->is_visible(drvdata, type, attr, index);
  327. if (!mode)
  328. return ERR_PTR(-ENOENT);
  329. if ((mode & 0444) && ((is_string && !ops->read_string) ||
  330. (!is_string && !ops->read)))
  331. return ERR_PTR(-EINVAL);
  332. if ((mode & 0222) && !ops->write)
  333. return ERR_PTR(-EINVAL);
  334. hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
  335. if (!hattr)
  336. return ERR_PTR(-ENOMEM);
  337. if (type == hwmon_chip) {
  338. name = template;
  339. } else {
  340. scnprintf(hattr->name, sizeof(hattr->name), template,
  341. index + hwmon_attr_base(type));
  342. name = hattr->name;
  343. }
  344. hattr->type = type;
  345. hattr->attr = attr;
  346. hattr->index = index;
  347. hattr->ops = ops;
  348. dattr = &hattr->dev_attr;
  349. dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
  350. dattr->store = hwmon_attr_store;
  351. a = &dattr->attr;
  352. sysfs_attr_init(a);
  353. a->name = name;
  354. a->mode = mode;
  355. return a;
  356. }
  357. /*
  358. * Chip attributes are not attribute templates but actual sysfs attributes.
  359. * See hwmon_genattr() for special handling.
  360. */
  361. static const char * const hwmon_chip_attrs[] = {
  362. [hwmon_chip_temp_reset_history] = "temp_reset_history",
  363. [hwmon_chip_in_reset_history] = "in_reset_history",
  364. [hwmon_chip_curr_reset_history] = "curr_reset_history",
  365. [hwmon_chip_power_reset_history] = "power_reset_history",
  366. [hwmon_chip_update_interval] = "update_interval",
  367. [hwmon_chip_alarms] = "alarms",
  368. [hwmon_chip_samples] = "samples",
  369. [hwmon_chip_curr_samples] = "curr_samples",
  370. [hwmon_chip_in_samples] = "in_samples",
  371. [hwmon_chip_power_samples] = "power_samples",
  372. [hwmon_chip_temp_samples] = "temp_samples",
  373. };
  374. static const char * const hwmon_temp_attr_templates[] = {
  375. [hwmon_temp_enable] = "temp%d_enable",
  376. [hwmon_temp_input] = "temp%d_input",
  377. [hwmon_temp_type] = "temp%d_type",
  378. [hwmon_temp_lcrit] = "temp%d_lcrit",
  379. [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
  380. [hwmon_temp_min] = "temp%d_min",
  381. [hwmon_temp_min_hyst] = "temp%d_min_hyst",
  382. [hwmon_temp_max] = "temp%d_max",
  383. [hwmon_temp_max_hyst] = "temp%d_max_hyst",
  384. [hwmon_temp_crit] = "temp%d_crit",
  385. [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
  386. [hwmon_temp_emergency] = "temp%d_emergency",
  387. [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
  388. [hwmon_temp_alarm] = "temp%d_alarm",
  389. [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
  390. [hwmon_temp_min_alarm] = "temp%d_min_alarm",
  391. [hwmon_temp_max_alarm] = "temp%d_max_alarm",
  392. [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
  393. [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
  394. [hwmon_temp_fault] = "temp%d_fault",
  395. [hwmon_temp_offset] = "temp%d_offset",
  396. [hwmon_temp_label] = "temp%d_label",
  397. [hwmon_temp_lowest] = "temp%d_lowest",
  398. [hwmon_temp_highest] = "temp%d_highest",
  399. [hwmon_temp_reset_history] = "temp%d_reset_history",
  400. [hwmon_temp_rated_min] = "temp%d_rated_min",
  401. [hwmon_temp_rated_max] = "temp%d_rated_max",
  402. };
  403. static const char * const hwmon_in_attr_templates[] = {
  404. [hwmon_in_enable] = "in%d_enable",
  405. [hwmon_in_input] = "in%d_input",
  406. [hwmon_in_min] = "in%d_min",
  407. [hwmon_in_max] = "in%d_max",
  408. [hwmon_in_lcrit] = "in%d_lcrit",
  409. [hwmon_in_crit] = "in%d_crit",
  410. [hwmon_in_average] = "in%d_average",
  411. [hwmon_in_lowest] = "in%d_lowest",
  412. [hwmon_in_highest] = "in%d_highest",
  413. [hwmon_in_reset_history] = "in%d_reset_history",
  414. [hwmon_in_label] = "in%d_label",
  415. [hwmon_in_alarm] = "in%d_alarm",
  416. [hwmon_in_min_alarm] = "in%d_min_alarm",
  417. [hwmon_in_max_alarm] = "in%d_max_alarm",
  418. [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
  419. [hwmon_in_crit_alarm] = "in%d_crit_alarm",
  420. [hwmon_in_rated_min] = "in%d_rated_min",
  421. [hwmon_in_rated_max] = "in%d_rated_max",
  422. };
  423. static const char * const hwmon_curr_attr_templates[] = {
  424. [hwmon_curr_enable] = "curr%d_enable",
  425. [hwmon_curr_input] = "curr%d_input",
  426. [hwmon_curr_min] = "curr%d_min",
  427. [hwmon_curr_max] = "curr%d_max",
  428. [hwmon_curr_lcrit] = "curr%d_lcrit",
  429. [hwmon_curr_crit] = "curr%d_crit",
  430. [hwmon_curr_average] = "curr%d_average",
  431. [hwmon_curr_lowest] = "curr%d_lowest",
  432. [hwmon_curr_highest] = "curr%d_highest",
  433. [hwmon_curr_reset_history] = "curr%d_reset_history",
  434. [hwmon_curr_label] = "curr%d_label",
  435. [hwmon_curr_alarm] = "curr%d_alarm",
  436. [hwmon_curr_min_alarm] = "curr%d_min_alarm",
  437. [hwmon_curr_max_alarm] = "curr%d_max_alarm",
  438. [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
  439. [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
  440. [hwmon_curr_rated_min] = "curr%d_rated_min",
  441. [hwmon_curr_rated_max] = "curr%d_rated_max",
  442. };
  443. static const char * const hwmon_power_attr_templates[] = {
  444. [hwmon_power_enable] = "power%d_enable",
  445. [hwmon_power_average] = "power%d_average",
  446. [hwmon_power_average_interval] = "power%d_average_interval",
  447. [hwmon_power_average_interval_max] = "power%d_interval_max",
  448. [hwmon_power_average_interval_min] = "power%d_interval_min",
  449. [hwmon_power_average_highest] = "power%d_average_highest",
  450. [hwmon_power_average_lowest] = "power%d_average_lowest",
  451. [hwmon_power_average_max] = "power%d_average_max",
  452. [hwmon_power_average_min] = "power%d_average_min",
  453. [hwmon_power_input] = "power%d_input",
  454. [hwmon_power_input_highest] = "power%d_input_highest",
  455. [hwmon_power_input_lowest] = "power%d_input_lowest",
  456. [hwmon_power_reset_history] = "power%d_reset_history",
  457. [hwmon_power_accuracy] = "power%d_accuracy",
  458. [hwmon_power_cap] = "power%d_cap",
  459. [hwmon_power_cap_hyst] = "power%d_cap_hyst",
  460. [hwmon_power_cap_max] = "power%d_cap_max",
  461. [hwmon_power_cap_min] = "power%d_cap_min",
  462. [hwmon_power_min] = "power%d_min",
  463. [hwmon_power_max] = "power%d_max",
  464. [hwmon_power_lcrit] = "power%d_lcrit",
  465. [hwmon_power_crit] = "power%d_crit",
  466. [hwmon_power_label] = "power%d_label",
  467. [hwmon_power_alarm] = "power%d_alarm",
  468. [hwmon_power_cap_alarm] = "power%d_cap_alarm",
  469. [hwmon_power_min_alarm] = "power%d_min_alarm",
  470. [hwmon_power_max_alarm] = "power%d_max_alarm",
  471. [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
  472. [hwmon_power_crit_alarm] = "power%d_crit_alarm",
  473. [hwmon_power_rated_min] = "power%d_rated_min",
  474. [hwmon_power_rated_max] = "power%d_rated_max",
  475. };
  476. static const char * const hwmon_energy_attr_templates[] = {
  477. [hwmon_energy_enable] = "energy%d_enable",
  478. [hwmon_energy_input] = "energy%d_input",
  479. [hwmon_energy_label] = "energy%d_label",
  480. };
  481. static const char * const hwmon_humidity_attr_templates[] = {
  482. [hwmon_humidity_enable] = "humidity%d_enable",
  483. [hwmon_humidity_input] = "humidity%d_input",
  484. [hwmon_humidity_label] = "humidity%d_label",
  485. [hwmon_humidity_min] = "humidity%d_min",
  486. [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
  487. [hwmon_humidity_max] = "humidity%d_max",
  488. [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
  489. [hwmon_humidity_alarm] = "humidity%d_alarm",
  490. [hwmon_humidity_fault] = "humidity%d_fault",
  491. [hwmon_humidity_rated_min] = "humidity%d_rated_min",
  492. [hwmon_humidity_rated_max] = "humidity%d_rated_max",
  493. };
  494. static const char * const hwmon_fan_attr_templates[] = {
  495. [hwmon_fan_enable] = "fan%d_enable",
  496. [hwmon_fan_input] = "fan%d_input",
  497. [hwmon_fan_label] = "fan%d_label",
  498. [hwmon_fan_min] = "fan%d_min",
  499. [hwmon_fan_max] = "fan%d_max",
  500. [hwmon_fan_div] = "fan%d_div",
  501. [hwmon_fan_pulses] = "fan%d_pulses",
  502. [hwmon_fan_target] = "fan%d_target",
  503. [hwmon_fan_alarm] = "fan%d_alarm",
  504. [hwmon_fan_min_alarm] = "fan%d_min_alarm",
  505. [hwmon_fan_max_alarm] = "fan%d_max_alarm",
  506. [hwmon_fan_fault] = "fan%d_fault",
  507. };
  508. static const char * const hwmon_pwm_attr_templates[] = {
  509. [hwmon_pwm_input] = "pwm%d",
  510. [hwmon_pwm_enable] = "pwm%d_enable",
  511. [hwmon_pwm_mode] = "pwm%d_mode",
  512. [hwmon_pwm_freq] = "pwm%d_freq",
  513. [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
  514. };
  515. static const char * const hwmon_intrusion_attr_templates[] = {
  516. [hwmon_intrusion_alarm] = "intrusion%d_alarm",
  517. [hwmon_intrusion_beep] = "intrusion%d_beep",
  518. };
  519. static const char * const *__templates[] = {
  520. [hwmon_chip] = hwmon_chip_attrs,
  521. [hwmon_temp] = hwmon_temp_attr_templates,
  522. [hwmon_in] = hwmon_in_attr_templates,
  523. [hwmon_curr] = hwmon_curr_attr_templates,
  524. [hwmon_power] = hwmon_power_attr_templates,
  525. [hwmon_energy] = hwmon_energy_attr_templates,
  526. [hwmon_humidity] = hwmon_humidity_attr_templates,
  527. [hwmon_fan] = hwmon_fan_attr_templates,
  528. [hwmon_pwm] = hwmon_pwm_attr_templates,
  529. [hwmon_intrusion] = hwmon_intrusion_attr_templates,
  530. };
  531. static const int __templates_size[] = {
  532. [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
  533. [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
  534. [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
  535. [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
  536. [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
  537. [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
  538. [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
  539. [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
  540. [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
  541. [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
  542. };
  543. int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
  544. u32 attr, int channel)
  545. {
  546. char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
  547. char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
  548. char *envp[] = { event, NULL };
  549. const char * const *templates;
  550. const char *template;
  551. int base;
  552. if (type >= ARRAY_SIZE(__templates))
  553. return -EINVAL;
  554. if (attr >= __templates_size[type])
  555. return -EINVAL;
  556. templates = __templates[type];
  557. template = templates[attr];
  558. base = hwmon_attr_base(type);
  559. scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
  560. scnprintf(event, sizeof(event), "NAME=%s", sattr);
  561. sysfs_notify(&dev->kobj, NULL, sattr);
  562. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  563. if (type == hwmon_temp)
  564. hwmon_thermal_notify(dev, channel);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(hwmon_notify_event);
  568. static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
  569. {
  570. int i, n;
  571. for (i = n = 0; info->config[i]; i++)
  572. n += hweight32(info->config[i]);
  573. return n;
  574. }
  575. static int hwmon_genattrs(const void *drvdata,
  576. struct attribute **attrs,
  577. const struct hwmon_ops *ops,
  578. const struct hwmon_channel_info *info)
  579. {
  580. const char * const *templates;
  581. int template_size;
  582. int i, aindex = 0;
  583. if (info->type >= ARRAY_SIZE(__templates))
  584. return -EINVAL;
  585. templates = __templates[info->type];
  586. template_size = __templates_size[info->type];
  587. for (i = 0; info->config[i]; i++) {
  588. u32 attr_mask = info->config[i];
  589. u32 attr;
  590. while (attr_mask) {
  591. struct attribute *a;
  592. attr = __ffs(attr_mask);
  593. attr_mask &= ~BIT(attr);
  594. if (attr >= template_size)
  595. return -EINVAL;
  596. a = hwmon_genattr(drvdata, info->type, attr, i,
  597. templates[attr], ops);
  598. if (IS_ERR(a)) {
  599. if (PTR_ERR(a) != -ENOENT)
  600. return PTR_ERR(a);
  601. continue;
  602. }
  603. attrs[aindex++] = a;
  604. }
  605. }
  606. return aindex;
  607. }
  608. static struct attribute **
  609. __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
  610. {
  611. int ret, i, aindex = 0, nattrs = 0;
  612. struct attribute **attrs;
  613. for (i = 0; chip->info[i]; i++)
  614. nattrs += hwmon_num_channel_attrs(chip->info[i]);
  615. if (nattrs == 0)
  616. return ERR_PTR(-EINVAL);
  617. attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
  618. if (!attrs)
  619. return ERR_PTR(-ENOMEM);
  620. for (i = 0; chip->info[i]; i++) {
  621. ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
  622. chip->info[i]);
  623. if (ret < 0) {
  624. hwmon_free_attrs(attrs);
  625. return ERR_PTR(ret);
  626. }
  627. aindex += ret;
  628. }
  629. return attrs;
  630. }
  631. static struct device *
  632. __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
  633. const struct hwmon_chip_info *chip,
  634. const struct attribute_group **groups)
  635. {
  636. struct hwmon_device *hwdev;
  637. const char *label;
  638. struct device *hdev;
  639. struct device *tdev = dev;
  640. int i, err, id;
  641. /* Complain about invalid characters in hwmon name attribute */
  642. if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
  643. dev_warn(dev,
  644. "hwmon: '%s' is not a valid name attribute, please fix\n",
  645. name);
  646. id = ida_alloc(&hwmon_ida, GFP_KERNEL);
  647. if (id < 0)
  648. return ERR_PTR(id);
  649. hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
  650. if (hwdev == NULL) {
  651. err = -ENOMEM;
  652. goto ida_remove;
  653. }
  654. hdev = &hwdev->dev;
  655. if (chip) {
  656. struct attribute **attrs;
  657. int ngroups = 2; /* terminating NULL plus &hwdev->groups */
  658. if (groups)
  659. for (i = 0; groups[i]; i++)
  660. ngroups++;
  661. hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
  662. if (!hwdev->groups) {
  663. err = -ENOMEM;
  664. goto free_hwmon;
  665. }
  666. attrs = __hwmon_create_attrs(drvdata, chip);
  667. if (IS_ERR(attrs)) {
  668. err = PTR_ERR(attrs);
  669. goto free_hwmon;
  670. }
  671. hwdev->group.attrs = attrs;
  672. ngroups = 0;
  673. hwdev->groups[ngroups++] = &hwdev->group;
  674. if (groups) {
  675. for (i = 0; groups[i]; i++)
  676. hwdev->groups[ngroups++] = groups[i];
  677. }
  678. hdev->groups = hwdev->groups;
  679. } else {
  680. hdev->groups = groups;
  681. }
  682. if (dev && device_property_present(dev, "label")) {
  683. err = device_property_read_string(dev, "label", &label);
  684. if (err < 0)
  685. goto free_hwmon;
  686. hwdev->label = kstrdup(label, GFP_KERNEL);
  687. if (hwdev->label == NULL) {
  688. err = -ENOMEM;
  689. goto free_hwmon;
  690. }
  691. }
  692. hwdev->name = name;
  693. hdev->class = &hwmon_class;
  694. hdev->parent = dev;
  695. while (tdev && !tdev->of_node)
  696. tdev = tdev->parent;
  697. hdev->of_node = tdev ? tdev->of_node : NULL;
  698. hwdev->chip = chip;
  699. dev_set_drvdata(hdev, drvdata);
  700. dev_set_name(hdev, HWMON_ID_FORMAT, id);
  701. err = device_register(hdev);
  702. if (err) {
  703. put_device(hdev);
  704. goto ida_remove;
  705. }
  706. INIT_LIST_HEAD(&hwdev->tzdata);
  707. if (hdev->of_node && chip && chip->ops->read &&
  708. chip->info[0]->type == hwmon_chip &&
  709. (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
  710. err = hwmon_thermal_register_sensors(hdev);
  711. if (err) {
  712. device_unregister(hdev);
  713. /*
  714. * Don't worry about hwdev; hwmon_dev_release(), called
  715. * from device_unregister(), will free it.
  716. */
  717. goto ida_remove;
  718. }
  719. }
  720. return hdev;
  721. free_hwmon:
  722. hwmon_dev_release(hdev);
  723. ida_remove:
  724. ida_free(&hwmon_ida, id);
  725. return ERR_PTR(err);
  726. }
  727. /**
  728. * hwmon_device_register_with_groups - register w/ hwmon
  729. * @dev: the parent device
  730. * @name: hwmon name attribute
  731. * @drvdata: driver data to attach to created device
  732. * @groups: List of attribute groups to create
  733. *
  734. * hwmon_device_unregister() must be called when the device is no
  735. * longer needed.
  736. *
  737. * Returns the pointer to the new device.
  738. */
  739. struct device *
  740. hwmon_device_register_with_groups(struct device *dev, const char *name,
  741. void *drvdata,
  742. const struct attribute_group **groups)
  743. {
  744. if (!name)
  745. return ERR_PTR(-EINVAL);
  746. return __hwmon_device_register(dev, name, drvdata, NULL, groups);
  747. }
  748. EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
  749. /**
  750. * hwmon_device_register_with_info - register w/ hwmon
  751. * @dev: the parent device (mandatory)
  752. * @name: hwmon name attribute (mandatory)
  753. * @drvdata: driver data to attach to created device (optional)
  754. * @chip: pointer to hwmon chip information (mandatory)
  755. * @extra_groups: pointer to list of additional non-standard attribute groups
  756. * (optional)
  757. *
  758. * hwmon_device_unregister() must be called when the device is no
  759. * longer needed.
  760. *
  761. * Returns the pointer to the new device.
  762. */
  763. struct device *
  764. hwmon_device_register_with_info(struct device *dev, const char *name,
  765. void *drvdata,
  766. const struct hwmon_chip_info *chip,
  767. const struct attribute_group **extra_groups)
  768. {
  769. if (!dev || !name || !chip)
  770. return ERR_PTR(-EINVAL);
  771. if (!chip->ops || !chip->ops->is_visible || !chip->info)
  772. return ERR_PTR(-EINVAL);
  773. return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
  774. }
  775. EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
  776. /**
  777. * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
  778. * @dev: the parent device
  779. * @name: hwmon name attribute
  780. * @drvdata: driver data to attach to created device
  781. *
  782. * The use of this function is restricted. It is provided for legacy reasons
  783. * and must only be called from the thermal subsystem.
  784. *
  785. * hwmon_device_unregister() must be called when the device is no
  786. * longer needed.
  787. *
  788. * Returns the pointer to the new device.
  789. */
  790. struct device *
  791. hwmon_device_register_for_thermal(struct device *dev, const char *name,
  792. void *drvdata)
  793. {
  794. if (!name || !dev)
  795. return ERR_PTR(-EINVAL);
  796. return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
  797. }
  798. EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
  799. /**
  800. * hwmon_device_register - register w/ hwmon
  801. * @dev: the device to register
  802. *
  803. * hwmon_device_unregister() must be called when the device is no
  804. * longer needed.
  805. *
  806. * Returns the pointer to the new device.
  807. */
  808. struct device *hwmon_device_register(struct device *dev)
  809. {
  810. dev_warn(dev,
  811. "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
  812. return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
  813. }
  814. EXPORT_SYMBOL_GPL(hwmon_device_register);
  815. /**
  816. * hwmon_device_unregister - removes the previously registered class device
  817. *
  818. * @dev: the class device to destroy
  819. */
  820. void hwmon_device_unregister(struct device *dev)
  821. {
  822. int id;
  823. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  824. device_unregister(dev);
  825. ida_free(&hwmon_ida, id);
  826. } else
  827. dev_dbg(dev->parent,
  828. "hwmon_device_unregister() failed: bad class ID!\n");
  829. }
  830. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  831. static void devm_hwmon_release(struct device *dev, void *res)
  832. {
  833. struct device *hwdev = *(struct device **)res;
  834. hwmon_device_unregister(hwdev);
  835. }
  836. /**
  837. * devm_hwmon_device_register_with_groups - register w/ hwmon
  838. * @dev: the parent device
  839. * @name: hwmon name attribute
  840. * @drvdata: driver data to attach to created device
  841. * @groups: List of attribute groups to create
  842. *
  843. * Returns the pointer to the new device. The new device is automatically
  844. * unregistered with the parent device.
  845. */
  846. struct device *
  847. devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
  848. void *drvdata,
  849. const struct attribute_group **groups)
  850. {
  851. struct device **ptr, *hwdev;
  852. if (!dev)
  853. return ERR_PTR(-EINVAL);
  854. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  855. if (!ptr)
  856. return ERR_PTR(-ENOMEM);
  857. hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
  858. if (IS_ERR(hwdev))
  859. goto error;
  860. *ptr = hwdev;
  861. devres_add(dev, ptr);
  862. return hwdev;
  863. error:
  864. devres_free(ptr);
  865. return hwdev;
  866. }
  867. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
  868. /**
  869. * devm_hwmon_device_register_with_info - register w/ hwmon
  870. * @dev: the parent device
  871. * @name: hwmon name attribute
  872. * @drvdata: driver data to attach to created device
  873. * @chip: pointer to hwmon chip information
  874. * @groups: pointer to list of driver specific attribute groups
  875. *
  876. * Returns the pointer to the new device. The new device is automatically
  877. * unregistered with the parent device.
  878. */
  879. struct device *
  880. devm_hwmon_device_register_with_info(struct device *dev, const char *name,
  881. void *drvdata,
  882. const struct hwmon_chip_info *chip,
  883. const struct attribute_group **groups)
  884. {
  885. struct device **ptr, *hwdev;
  886. if (!dev)
  887. return ERR_PTR(-EINVAL);
  888. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  889. if (!ptr)
  890. return ERR_PTR(-ENOMEM);
  891. hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
  892. groups);
  893. if (IS_ERR(hwdev))
  894. goto error;
  895. *ptr = hwdev;
  896. devres_add(dev, ptr);
  897. return hwdev;
  898. error:
  899. devres_free(ptr);
  900. return hwdev;
  901. }
  902. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
  903. static int devm_hwmon_match(struct device *dev, void *res, void *data)
  904. {
  905. struct device **hwdev = res;
  906. return *hwdev == data;
  907. }
  908. /**
  909. * devm_hwmon_device_unregister - removes a previously registered hwmon device
  910. *
  911. * @dev: the parent device of the device to unregister
  912. */
  913. void devm_hwmon_device_unregister(struct device *dev)
  914. {
  915. WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
  916. }
  917. EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
  918. static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
  919. {
  920. char *name, *p;
  921. if (dev)
  922. name = devm_kstrdup(dev, old_name, GFP_KERNEL);
  923. else
  924. name = kstrdup(old_name, GFP_KERNEL);
  925. if (!name)
  926. return ERR_PTR(-ENOMEM);
  927. for (p = name; *p; p++)
  928. if (hwmon_is_bad_char(*p))
  929. *p = '_';
  930. return name;
  931. }
  932. /**
  933. * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
  934. * @name: NUL-terminated name
  935. *
  936. * Allocates a new string where any invalid characters will be replaced
  937. * by an underscore. It is the responsibility of the caller to release
  938. * the memory.
  939. *
  940. * Returns newly allocated name, or ERR_PTR on error.
  941. */
  942. char *hwmon_sanitize_name(const char *name)
  943. {
  944. return __hwmon_sanitize_name(NULL, name);
  945. }
  946. EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
  947. /**
  948. * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
  949. * @dev: device to allocate memory for
  950. * @name: NUL-terminated name
  951. *
  952. * Allocates a new string where any invalid characters will be replaced
  953. * by an underscore.
  954. *
  955. * Returns newly allocated name, or ERR_PTR on error.
  956. */
  957. char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
  958. {
  959. if (!dev)
  960. return ERR_PTR(-EINVAL);
  961. return __hwmon_sanitize_name(dev, name);
  962. }
  963. EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
  964. static void __init hwmon_pci_quirks(void)
  965. {
  966. #if defined CONFIG_X86 && defined CONFIG_PCI
  967. struct pci_dev *sb;
  968. u16 base;
  969. u8 enable;
  970. /* Open access to 0x295-0x296 on MSI MS-7031 */
  971. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  972. if (sb) {
  973. if (sb->subsystem_vendor == 0x1462 && /* MSI */
  974. sb->subsystem_device == 0x0031) { /* MS-7031 */
  975. pci_read_config_byte(sb, 0x48, &enable);
  976. pci_read_config_word(sb, 0x64, &base);
  977. if (base == 0 && !(enable & BIT(2))) {
  978. dev_info(&sb->dev,
  979. "Opening wide generic port at 0x295\n");
  980. pci_write_config_word(sb, 0x64, 0x295);
  981. pci_write_config_byte(sb, 0x48,
  982. enable | BIT(2));
  983. }
  984. }
  985. pci_dev_put(sb);
  986. }
  987. #endif
  988. }
  989. static int __init hwmon_init(void)
  990. {
  991. int err;
  992. hwmon_pci_quirks();
  993. err = class_register(&hwmon_class);
  994. if (err) {
  995. pr_err("couldn't register hwmon sysfs class\n");
  996. return err;
  997. }
  998. return 0;
  999. }
  1000. static void __exit hwmon_exit(void)
  1001. {
  1002. class_unregister(&hwmon_class);
  1003. }
  1004. subsys_initcall(hwmon_init);
  1005. module_exit(hwmon_exit);
  1006. MODULE_AUTHOR("Mark M. Hoffman <[email protected]>");
  1007. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  1008. MODULE_LICENSE("GPL");