hwmon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  3. *
  4. * This file defines the sysfs class "hwmon", for use by sensors drivers.
  5. *
  6. * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/gfp.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/idr.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/thermal.h>
  24. #define HWMON_ID_PREFIX "hwmon"
  25. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  26. struct hwmon_device {
  27. const char *name;
  28. struct device dev;
  29. const struct hwmon_chip_info *chip;
  30. struct attribute_group group;
  31. const struct attribute_group **groups;
  32. };
  33. #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
  34. struct hwmon_device_attribute {
  35. struct device_attribute dev_attr;
  36. const struct hwmon_ops *ops;
  37. enum hwmon_sensor_types type;
  38. u32 attr;
  39. int index;
  40. };
  41. #define to_hwmon_attr(d) \
  42. container_of(d, struct hwmon_device_attribute, dev_attr)
  43. /*
  44. * Thermal zone information
  45. * In addition to the reference to the hwmon device,
  46. * also provides the sensor index.
  47. */
  48. struct hwmon_thermal_data {
  49. struct hwmon_device *hwdev; /* Reference to hwmon device */
  50. int index; /* sensor index */
  51. };
  52. static ssize_t
  53. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  54. {
  55. return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
  56. }
  57. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  58. static struct attribute *hwmon_dev_attrs[] = {
  59. &dev_attr_name.attr,
  60. NULL
  61. };
  62. static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
  63. struct attribute *attr, int n)
  64. {
  65. struct device *dev = container_of(kobj, struct device, kobj);
  66. if (to_hwmon_device(dev)->name == NULL)
  67. return 0;
  68. return attr->mode;
  69. }
  70. static struct attribute_group hwmon_dev_attr_group = {
  71. .attrs = hwmon_dev_attrs,
  72. .is_visible = hwmon_dev_name_is_visible,
  73. };
  74. static const struct attribute_group *hwmon_dev_attr_groups[] = {
  75. &hwmon_dev_attr_group,
  76. NULL
  77. };
  78. static void hwmon_dev_release(struct device *dev)
  79. {
  80. kfree(to_hwmon_device(dev));
  81. }
  82. static struct class hwmon_class = {
  83. .name = "hwmon",
  84. .owner = THIS_MODULE,
  85. .dev_groups = hwmon_dev_attr_groups,
  86. .dev_release = hwmon_dev_release,
  87. };
  88. static DEFINE_IDA(hwmon_ida);
  89. /* Thermal zone handling */
  90. /*
  91. * The complex conditional is necessary to avoid a cyclic dependency
  92. * between hwmon and thermal_sys modules.
  93. */
  94. #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
  95. (!defined(CONFIG_THERMAL_HWMON) || \
  96. !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
  97. static int hwmon_thermal_get_temp(void *data, int *temp)
  98. {
  99. struct hwmon_thermal_data *tdata = data;
  100. struct hwmon_device *hwdev = tdata->hwdev;
  101. int ret;
  102. long t;
  103. ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
  104. tdata->index, &t);
  105. if (ret < 0)
  106. return ret;
  107. *temp = t;
  108. return 0;
  109. }
  110. static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
  111. .get_temp = hwmon_thermal_get_temp,
  112. };
  113. static int hwmon_thermal_add_sensor(struct device *dev,
  114. struct hwmon_device *hwdev, int index)
  115. {
  116. struct hwmon_thermal_data *tdata;
  117. tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
  118. if (!tdata)
  119. return -ENOMEM;
  120. tdata->hwdev = hwdev;
  121. tdata->index = index;
  122. devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
  123. &hwmon_thermal_ops);
  124. return 0;
  125. }
  126. #else
  127. static int hwmon_thermal_add_sensor(struct device *dev,
  128. struct hwmon_device *hwdev, int index)
  129. {
  130. return 0;
  131. }
  132. #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
  133. /* sysfs attribute management */
  134. static ssize_t hwmon_attr_show(struct device *dev,
  135. struct device_attribute *devattr, char *buf)
  136. {
  137. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  138. long val;
  139. int ret;
  140. ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
  141. &val);
  142. if (ret < 0)
  143. return ret;
  144. return sprintf(buf, "%ld\n", val);
  145. }
  146. static ssize_t hwmon_attr_store(struct device *dev,
  147. struct device_attribute *devattr,
  148. const char *buf, size_t count)
  149. {
  150. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  151. long val;
  152. int ret;
  153. ret = kstrtol(buf, 10, &val);
  154. if (ret < 0)
  155. return ret;
  156. ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
  157. val);
  158. if (ret < 0)
  159. return ret;
  160. return count;
  161. }
  162. static int hwmon_attr_base(enum hwmon_sensor_types type)
  163. {
  164. if (type == hwmon_in)
  165. return 0;
  166. return 1;
  167. }
  168. static struct attribute *hwmon_genattr(struct device *dev,
  169. const void *drvdata,
  170. enum hwmon_sensor_types type,
  171. u32 attr,
  172. int index,
  173. const char *template,
  174. const struct hwmon_ops *ops)
  175. {
  176. struct hwmon_device_attribute *hattr;
  177. struct device_attribute *dattr;
  178. struct attribute *a;
  179. umode_t mode;
  180. char *name;
  181. /* The attribute is invisible if there is no template string */
  182. if (!template)
  183. return ERR_PTR(-ENOENT);
  184. mode = ops->is_visible(drvdata, type, attr, index);
  185. if (!mode)
  186. return ERR_PTR(-ENOENT);
  187. if ((mode & S_IRUGO) && !ops->read)
  188. return ERR_PTR(-EINVAL);
  189. if ((mode & S_IWUGO) && !ops->write)
  190. return ERR_PTR(-EINVAL);
  191. if (type == hwmon_chip) {
  192. name = (char *)template;
  193. } else {
  194. name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
  195. if (!name)
  196. return ERR_PTR(-ENOMEM);
  197. scnprintf(name, strlen(template) + 16, template,
  198. index + hwmon_attr_base(type));
  199. }
  200. hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
  201. if (!hattr)
  202. return ERR_PTR(-ENOMEM);
  203. hattr->type = type;
  204. hattr->attr = attr;
  205. hattr->index = index;
  206. hattr->ops = ops;
  207. dattr = &hattr->dev_attr;
  208. dattr->show = hwmon_attr_show;
  209. dattr->store = hwmon_attr_store;
  210. a = &dattr->attr;
  211. sysfs_attr_init(a);
  212. a->name = name;
  213. a->mode = mode;
  214. return a;
  215. }
  216. static const char * const hwmon_chip_attr_templates[] = {
  217. [hwmon_chip_temp_reset_history] = "temp_reset_history",
  218. [hwmon_chip_in_reset_history] = "in_reset_history",
  219. [hwmon_chip_curr_reset_history] = "curr_reset_history",
  220. [hwmon_chip_power_reset_history] = "power_reset_history",
  221. [hwmon_chip_update_interval] = "update_interval",
  222. [hwmon_chip_alarms] = "alarms",
  223. };
  224. static const char * const hwmon_temp_attr_templates[] = {
  225. [hwmon_temp_input] = "temp%d_input",
  226. [hwmon_temp_type] = "temp%d_type",
  227. [hwmon_temp_lcrit] = "temp%d_lcrit",
  228. [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
  229. [hwmon_temp_min] = "temp%d_min",
  230. [hwmon_temp_min_hyst] = "temp%d_min_hyst",
  231. [hwmon_temp_max] = "temp%d_max",
  232. [hwmon_temp_max_hyst] = "temp%d_max_hyst",
  233. [hwmon_temp_crit] = "temp%d_crit",
  234. [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
  235. [hwmon_temp_emergency] = "temp%d_emergency",
  236. [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
  237. [hwmon_temp_alarm] = "temp%d_alarm",
  238. [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
  239. [hwmon_temp_min_alarm] = "temp%d_min_alarm",
  240. [hwmon_temp_max_alarm] = "temp%d_max_alarm",
  241. [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
  242. [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
  243. [hwmon_temp_fault] = "temp%d_fault",
  244. [hwmon_temp_offset] = "temp%d_offset",
  245. [hwmon_temp_label] = "temp%d_label",
  246. [hwmon_temp_lowest] = "temp%d_lowest",
  247. [hwmon_temp_highest] = "temp%d_highest",
  248. [hwmon_temp_reset_history] = "temp%d_reset_history",
  249. };
  250. static const char * const hwmon_in_attr_templates[] = {
  251. [hwmon_in_input] = "in%d_input",
  252. [hwmon_in_min] = "in%d_min",
  253. [hwmon_in_max] = "in%d_max",
  254. [hwmon_in_lcrit] = "in%d_lcrit",
  255. [hwmon_in_crit] = "in%d_crit",
  256. [hwmon_in_average] = "in%d_average",
  257. [hwmon_in_lowest] = "in%d_lowest",
  258. [hwmon_in_highest] = "in%d_highest",
  259. [hwmon_in_reset_history] = "in%d_reset_history",
  260. [hwmon_in_label] = "in%d_label",
  261. [hwmon_in_alarm] = "in%d_alarm",
  262. [hwmon_in_min_alarm] = "in%d_min_alarm",
  263. [hwmon_in_max_alarm] = "in%d_max_alarm",
  264. [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
  265. [hwmon_in_crit_alarm] = "in%d_crit_alarm",
  266. };
  267. static const char * const hwmon_curr_attr_templates[] = {
  268. [hwmon_curr_input] = "curr%d_input",
  269. [hwmon_curr_min] = "curr%d_min",
  270. [hwmon_curr_max] = "curr%d_max",
  271. [hwmon_curr_lcrit] = "curr%d_lcrit",
  272. [hwmon_curr_crit] = "curr%d_crit",
  273. [hwmon_curr_average] = "curr%d_average",
  274. [hwmon_curr_lowest] = "curr%d_lowest",
  275. [hwmon_curr_highest] = "curr%d_highest",
  276. [hwmon_curr_reset_history] = "curr%d_reset_history",
  277. [hwmon_curr_label] = "curr%d_label",
  278. [hwmon_curr_alarm] = "curr%d_alarm",
  279. [hwmon_curr_min_alarm] = "curr%d_min_alarm",
  280. [hwmon_curr_max_alarm] = "curr%d_max_alarm",
  281. [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
  282. [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
  283. };
  284. static const char * const hwmon_power_attr_templates[] = {
  285. [hwmon_power_average] = "power%d_average",
  286. [hwmon_power_average_interval] = "power%d_average_interval",
  287. [hwmon_power_average_interval_max] = "power%d_interval_max",
  288. [hwmon_power_average_interval_min] = "power%d_interval_min",
  289. [hwmon_power_average_highest] = "power%d_average_highest",
  290. [hwmon_power_average_lowest] = "power%d_average_lowest",
  291. [hwmon_power_average_max] = "power%d_average_max",
  292. [hwmon_power_average_min] = "power%d_average_min",
  293. [hwmon_power_input] = "power%d_input",
  294. [hwmon_power_input_highest] = "power%d_input_highest",
  295. [hwmon_power_input_lowest] = "power%d_input_lowest",
  296. [hwmon_power_reset_history] = "power%d_reset_history",
  297. [hwmon_power_accuracy] = "power%d_accuracy",
  298. [hwmon_power_cap] = "power%d_cap",
  299. [hwmon_power_cap_hyst] = "power%d_cap_hyst",
  300. [hwmon_power_cap_max] = "power%d_cap_max",
  301. [hwmon_power_cap_min] = "power%d_cap_min",
  302. [hwmon_power_max] = "power%d_max",
  303. [hwmon_power_crit] = "power%d_crit",
  304. [hwmon_power_label] = "power%d_label",
  305. [hwmon_power_alarm] = "power%d_alarm",
  306. [hwmon_power_cap_alarm] = "power%d_cap_alarm",
  307. [hwmon_power_max_alarm] = "power%d_max_alarm",
  308. [hwmon_power_crit_alarm] = "power%d_crit_alarm",
  309. };
  310. static const char * const hwmon_energy_attr_templates[] = {
  311. [hwmon_energy_input] = "energy%d_input",
  312. [hwmon_energy_label] = "energy%d_label",
  313. };
  314. static const char * const hwmon_humidity_attr_templates[] = {
  315. [hwmon_humidity_input] = "humidity%d_input",
  316. [hwmon_humidity_label] = "humidity%d_label",
  317. [hwmon_humidity_min] = "humidity%d_min",
  318. [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
  319. [hwmon_humidity_max] = "humidity%d_max",
  320. [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
  321. [hwmon_humidity_alarm] = "humidity%d_alarm",
  322. [hwmon_humidity_fault] = "humidity%d_fault",
  323. };
  324. static const char * const hwmon_fan_attr_templates[] = {
  325. [hwmon_fan_input] = "fan%d_input",
  326. [hwmon_fan_label] = "fan%d_label",
  327. [hwmon_fan_min] = "fan%d_min",
  328. [hwmon_fan_max] = "fan%d_max",
  329. [hwmon_fan_div] = "fan%d_div",
  330. [hwmon_fan_pulses] = "fan%d_pulses",
  331. [hwmon_fan_target] = "fan%d_target",
  332. [hwmon_fan_alarm] = "fan%d_alarm",
  333. [hwmon_fan_min_alarm] = "fan%d_min_alarm",
  334. [hwmon_fan_max_alarm] = "fan%d_max_alarm",
  335. [hwmon_fan_fault] = "fan%d_fault",
  336. };
  337. static const char * const hwmon_pwm_attr_templates[] = {
  338. [hwmon_pwm_input] = "pwm%d",
  339. [hwmon_pwm_enable] = "pwm%d_enable",
  340. [hwmon_pwm_mode] = "pwm%d_mode",
  341. [hwmon_pwm_freq] = "pwm%d_freq",
  342. };
  343. static const char * const *__templates[] = {
  344. [hwmon_chip] = hwmon_chip_attr_templates,
  345. [hwmon_temp] = hwmon_temp_attr_templates,
  346. [hwmon_in] = hwmon_in_attr_templates,
  347. [hwmon_curr] = hwmon_curr_attr_templates,
  348. [hwmon_power] = hwmon_power_attr_templates,
  349. [hwmon_energy] = hwmon_energy_attr_templates,
  350. [hwmon_humidity] = hwmon_humidity_attr_templates,
  351. [hwmon_fan] = hwmon_fan_attr_templates,
  352. [hwmon_pwm] = hwmon_pwm_attr_templates,
  353. };
  354. static const int __templates_size[] = {
  355. [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
  356. [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
  357. [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
  358. [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
  359. [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
  360. [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
  361. [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
  362. [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
  363. [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
  364. };
  365. static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
  366. {
  367. int i, n;
  368. for (i = n = 0; info->config[i]; i++)
  369. n += hweight32(info->config[i]);
  370. return n;
  371. }
  372. static int hwmon_genattrs(struct device *dev,
  373. const void *drvdata,
  374. struct attribute **attrs,
  375. const struct hwmon_ops *ops,
  376. const struct hwmon_channel_info *info)
  377. {
  378. const char * const *templates;
  379. int template_size;
  380. int i, aindex = 0;
  381. if (info->type >= ARRAY_SIZE(__templates))
  382. return -EINVAL;
  383. templates = __templates[info->type];
  384. template_size = __templates_size[info->type];
  385. for (i = 0; info->config[i]; i++) {
  386. u32 attr_mask = info->config[i];
  387. u32 attr;
  388. while (attr_mask) {
  389. struct attribute *a;
  390. attr = __ffs(attr_mask);
  391. attr_mask &= ~BIT(attr);
  392. if (attr >= template_size)
  393. return -EINVAL;
  394. a = hwmon_genattr(dev, drvdata, info->type, attr, i,
  395. templates[attr], ops);
  396. if (IS_ERR(a)) {
  397. if (PTR_ERR(a) != -ENOENT)
  398. return PTR_ERR(a);
  399. continue;
  400. }
  401. attrs[aindex++] = a;
  402. }
  403. }
  404. return aindex;
  405. }
  406. static struct attribute **
  407. __hwmon_create_attrs(struct device *dev, const void *drvdata,
  408. const struct hwmon_chip_info *chip)
  409. {
  410. int ret, i, aindex = 0, nattrs = 0;
  411. struct attribute **attrs;
  412. for (i = 0; chip->info[i]; i++)
  413. nattrs += hwmon_num_channel_attrs(chip->info[i]);
  414. if (nattrs == 0)
  415. return ERR_PTR(-EINVAL);
  416. attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
  417. if (!attrs)
  418. return ERR_PTR(-ENOMEM);
  419. for (i = 0; chip->info[i]; i++) {
  420. ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
  421. chip->info[i]);
  422. if (ret < 0)
  423. return ERR_PTR(ret);
  424. aindex += ret;
  425. }
  426. return attrs;
  427. }
  428. static struct device *
  429. __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
  430. const struct hwmon_chip_info *chip,
  431. const struct attribute_group **groups)
  432. {
  433. struct hwmon_device *hwdev;
  434. struct device *hdev;
  435. int i, j, err, id;
  436. /* Do not accept invalid characters in hwmon name attribute */
  437. if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
  438. return ERR_PTR(-EINVAL);
  439. id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
  440. if (id < 0)
  441. return ERR_PTR(id);
  442. hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
  443. if (hwdev == NULL) {
  444. err = -ENOMEM;
  445. goto ida_remove;
  446. }
  447. hdev = &hwdev->dev;
  448. if (chip && chip->ops->is_visible) {
  449. struct attribute **attrs;
  450. int ngroups = 2;
  451. if (groups)
  452. for (i = 0; groups[i]; i++)
  453. ngroups++;
  454. hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
  455. GFP_KERNEL);
  456. if (!hwdev->groups) {
  457. err = -ENOMEM;
  458. goto free_hwmon;
  459. }
  460. attrs = __hwmon_create_attrs(dev, drvdata, chip);
  461. if (IS_ERR(attrs)) {
  462. err = PTR_ERR(attrs);
  463. goto free_hwmon;
  464. }
  465. hwdev->group.attrs = attrs;
  466. ngroups = 0;
  467. hwdev->groups[ngroups++] = &hwdev->group;
  468. if (groups) {
  469. for (i = 0; groups[i]; i++)
  470. hwdev->groups[ngroups++] = groups[i];
  471. }
  472. hdev->groups = hwdev->groups;
  473. } else {
  474. hdev->groups = groups;
  475. }
  476. hwdev->name = name;
  477. hdev->class = &hwmon_class;
  478. hdev->parent = dev;
  479. hdev->of_node = dev ? dev->of_node : NULL;
  480. hwdev->chip = chip;
  481. dev_set_drvdata(hdev, drvdata);
  482. dev_set_name(hdev, HWMON_ID_FORMAT, id);
  483. err = device_register(hdev);
  484. if (err)
  485. goto free_hwmon;
  486. if (chip && chip->ops->is_visible && chip->ops->read &&
  487. chip->info[0]->type == hwmon_chip &&
  488. (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
  489. const struct hwmon_channel_info **info = chip->info;
  490. for (i = 1; info[i]; i++) {
  491. if (info[i]->type != hwmon_temp)
  492. continue;
  493. for (j = 0; info[i]->config[j]; j++) {
  494. if (!chip->ops->is_visible(drvdata, hwmon_temp,
  495. hwmon_temp_input, j))
  496. continue;
  497. if (info[i]->config[j] & HWMON_T_INPUT)
  498. hwmon_thermal_add_sensor(dev, hwdev, j);
  499. }
  500. }
  501. }
  502. return hdev;
  503. free_hwmon:
  504. kfree(hwdev);
  505. ida_remove:
  506. ida_simple_remove(&hwmon_ida, id);
  507. return ERR_PTR(err);
  508. }
  509. /**
  510. * hwmon_device_register_with_groups - register w/ hwmon
  511. * @dev: the parent device
  512. * @name: hwmon name attribute
  513. * @drvdata: driver data to attach to created device
  514. * @groups: List of attribute groups to create
  515. *
  516. * hwmon_device_unregister() must be called when the device is no
  517. * longer needed.
  518. *
  519. * Returns the pointer to the new device.
  520. */
  521. struct device *
  522. hwmon_device_register_with_groups(struct device *dev, const char *name,
  523. void *drvdata,
  524. const struct attribute_group **groups)
  525. {
  526. return __hwmon_device_register(dev, name, drvdata, NULL, groups);
  527. }
  528. EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
  529. /**
  530. * hwmon_device_register_with_info - register w/ hwmon
  531. * @dev: the parent device
  532. * @name: hwmon name attribute
  533. * @drvdata: driver data to attach to created device
  534. * @info: Pointer to hwmon chip information
  535. * @groups - pointer to list of driver specific attribute groups
  536. *
  537. * hwmon_device_unregister() must be called when the device is no
  538. * longer needed.
  539. *
  540. * Returns the pointer to the new device.
  541. */
  542. struct device *
  543. hwmon_device_register_with_info(struct device *dev, const char *name,
  544. void *drvdata,
  545. const struct hwmon_chip_info *chip,
  546. const struct attribute_group **groups)
  547. {
  548. if (chip && (!chip->ops || !chip->info))
  549. return ERR_PTR(-EINVAL);
  550. return __hwmon_device_register(dev, name, drvdata, chip, groups);
  551. }
  552. EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
  553. /**
  554. * hwmon_device_register - register w/ hwmon
  555. * @dev: the device to register
  556. *
  557. * hwmon_device_unregister() must be called when the device is no
  558. * longer needed.
  559. *
  560. * Returns the pointer to the new device.
  561. */
  562. struct device *hwmon_device_register(struct device *dev)
  563. {
  564. return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
  565. }
  566. EXPORT_SYMBOL_GPL(hwmon_device_register);
  567. /**
  568. * hwmon_device_unregister - removes the previously registered class device
  569. *
  570. * @dev: the class device to destroy
  571. */
  572. void hwmon_device_unregister(struct device *dev)
  573. {
  574. int id;
  575. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  576. device_unregister(dev);
  577. ida_simple_remove(&hwmon_ida, id);
  578. } else
  579. dev_dbg(dev->parent,
  580. "hwmon_device_unregister() failed: bad class ID!\n");
  581. }
  582. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  583. static void devm_hwmon_release(struct device *dev, void *res)
  584. {
  585. struct device *hwdev = *(struct device **)res;
  586. hwmon_device_unregister(hwdev);
  587. }
  588. /**
  589. * devm_hwmon_device_register_with_groups - register w/ hwmon
  590. * @dev: the parent device
  591. * @name: hwmon name attribute
  592. * @drvdata: driver data to attach to created device
  593. * @groups: List of attribute groups to create
  594. *
  595. * Returns the pointer to the new device. The new device is automatically
  596. * unregistered with the parent device.
  597. */
  598. struct device *
  599. devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
  600. void *drvdata,
  601. const struct attribute_group **groups)
  602. {
  603. struct device **ptr, *hwdev;
  604. if (!dev)
  605. return ERR_PTR(-EINVAL);
  606. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  607. if (!ptr)
  608. return ERR_PTR(-ENOMEM);
  609. hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
  610. if (IS_ERR(hwdev))
  611. goto error;
  612. *ptr = hwdev;
  613. devres_add(dev, ptr);
  614. return hwdev;
  615. error:
  616. devres_free(ptr);
  617. return hwdev;
  618. }
  619. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
  620. /**
  621. * devm_hwmon_device_register_with_info - register w/ hwmon
  622. * @dev: the parent device
  623. * @name: hwmon name attribute
  624. * @drvdata: driver data to attach to created device
  625. * @info: Pointer to hwmon chip information
  626. * @groups - pointer to list of driver specific attribute groups
  627. *
  628. * Returns the pointer to the new device. The new device is automatically
  629. * unregistered with the parent device.
  630. */
  631. struct device *
  632. devm_hwmon_device_register_with_info(struct device *dev, const char *name,
  633. void *drvdata,
  634. const struct hwmon_chip_info *chip,
  635. const struct attribute_group **groups)
  636. {
  637. struct device **ptr, *hwdev;
  638. if (!dev)
  639. return ERR_PTR(-EINVAL);
  640. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  641. if (!ptr)
  642. return ERR_PTR(-ENOMEM);
  643. hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
  644. groups);
  645. if (IS_ERR(hwdev))
  646. goto error;
  647. *ptr = hwdev;
  648. devres_add(dev, ptr);
  649. return hwdev;
  650. error:
  651. devres_free(ptr);
  652. return hwdev;
  653. }
  654. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
  655. static int devm_hwmon_match(struct device *dev, void *res, void *data)
  656. {
  657. struct device **hwdev = res;
  658. return *hwdev == data;
  659. }
  660. /**
  661. * devm_hwmon_device_unregister - removes a previously registered hwmon device
  662. *
  663. * @dev: the parent device of the device to unregister
  664. */
  665. void devm_hwmon_device_unregister(struct device *dev)
  666. {
  667. WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
  668. }
  669. EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
  670. static void __init hwmon_pci_quirks(void)
  671. {
  672. #if defined CONFIG_X86 && defined CONFIG_PCI
  673. struct pci_dev *sb;
  674. u16 base;
  675. u8 enable;
  676. /* Open access to 0x295-0x296 on MSI MS-7031 */
  677. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  678. if (sb) {
  679. if (sb->subsystem_vendor == 0x1462 && /* MSI */
  680. sb->subsystem_device == 0x0031) { /* MS-7031 */
  681. pci_read_config_byte(sb, 0x48, &enable);
  682. pci_read_config_word(sb, 0x64, &base);
  683. if (base == 0 && !(enable & BIT(2))) {
  684. dev_info(&sb->dev,
  685. "Opening wide generic port at 0x295\n");
  686. pci_write_config_word(sb, 0x64, 0x295);
  687. pci_write_config_byte(sb, 0x48,
  688. enable | BIT(2));
  689. }
  690. }
  691. pci_dev_put(sb);
  692. }
  693. #endif
  694. }
  695. static int __init hwmon_init(void)
  696. {
  697. int err;
  698. hwmon_pci_quirks();
  699. err = class_register(&hwmon_class);
  700. if (err) {
  701. pr_err("couldn't register hwmon sysfs class\n");
  702. return err;
  703. }
  704. return 0;
  705. }
  706. static void __exit hwmon_exit(void)
  707. {
  708. class_unregister(&hwmon_class);
  709. }
  710. subsys_initcall(hwmon_init);
  711. module_exit(hwmon_exit);
  712. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  713. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  714. MODULE_LICENSE("GPL");