hwmon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_show_string(struct device *dev,
  147. struct device_attribute *devattr,
  148. char *buf)
  149. {
  150. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  151. char *s;
  152. int ret;
  153. ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
  154. hattr->index, &s);
  155. if (ret < 0)
  156. return ret;
  157. return sprintf(buf, "%s\n", s);
  158. }
  159. static ssize_t hwmon_attr_store(struct device *dev,
  160. struct device_attribute *devattr,
  161. const char *buf, size_t count)
  162. {
  163. struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
  164. long val;
  165. int ret;
  166. ret = kstrtol(buf, 10, &val);
  167. if (ret < 0)
  168. return ret;
  169. ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
  170. val);
  171. if (ret < 0)
  172. return ret;
  173. return count;
  174. }
  175. static int hwmon_attr_base(enum hwmon_sensor_types type)
  176. {
  177. if (type == hwmon_in)
  178. return 0;
  179. return 1;
  180. }
  181. static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
  182. {
  183. return (type == hwmon_temp && attr == hwmon_temp_label) ||
  184. (type == hwmon_in && attr == hwmon_in_label) ||
  185. (type == hwmon_curr && attr == hwmon_curr_label) ||
  186. (type == hwmon_power && attr == hwmon_power_label) ||
  187. (type == hwmon_energy && attr == hwmon_energy_label) ||
  188. (type == hwmon_humidity && attr == hwmon_humidity_label) ||
  189. (type == hwmon_fan && attr == hwmon_fan_label);
  190. }
  191. static struct attribute *hwmon_genattr(struct device *dev,
  192. const void *drvdata,
  193. enum hwmon_sensor_types type,
  194. u32 attr,
  195. int index,
  196. const char *template,
  197. const struct hwmon_ops *ops)
  198. {
  199. struct hwmon_device_attribute *hattr;
  200. struct device_attribute *dattr;
  201. struct attribute *a;
  202. umode_t mode;
  203. char *name;
  204. bool is_string = is_string_attr(type, attr);
  205. /* The attribute is invisible if there is no template string */
  206. if (!template)
  207. return ERR_PTR(-ENOENT);
  208. mode = ops->is_visible(drvdata, type, attr, index);
  209. if (!mode)
  210. return ERR_PTR(-ENOENT);
  211. if ((mode & S_IRUGO) && ((is_string && !ops->read_string) ||
  212. (!is_string && !ops->read)))
  213. return ERR_PTR(-EINVAL);
  214. if ((mode & S_IWUGO) && !ops->write)
  215. return ERR_PTR(-EINVAL);
  216. if (type == hwmon_chip) {
  217. name = (char *)template;
  218. } else {
  219. name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
  220. if (!name)
  221. return ERR_PTR(-ENOMEM);
  222. scnprintf(name, strlen(template) + 16, template,
  223. index + hwmon_attr_base(type));
  224. }
  225. hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
  226. if (!hattr)
  227. return ERR_PTR(-ENOMEM);
  228. hattr->type = type;
  229. hattr->attr = attr;
  230. hattr->index = index;
  231. hattr->ops = ops;
  232. dattr = &hattr->dev_attr;
  233. dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
  234. dattr->store = hwmon_attr_store;
  235. a = &dattr->attr;
  236. sysfs_attr_init(a);
  237. a->name = name;
  238. a->mode = mode;
  239. return a;
  240. }
  241. static const char * const hwmon_chip_attr_templates[] = {
  242. [hwmon_chip_temp_reset_history] = "temp_reset_history",
  243. [hwmon_chip_in_reset_history] = "in_reset_history",
  244. [hwmon_chip_curr_reset_history] = "curr_reset_history",
  245. [hwmon_chip_power_reset_history] = "power_reset_history",
  246. [hwmon_chip_update_interval] = "update_interval",
  247. [hwmon_chip_alarms] = "alarms",
  248. };
  249. static const char * const hwmon_temp_attr_templates[] = {
  250. [hwmon_temp_input] = "temp%d_input",
  251. [hwmon_temp_type] = "temp%d_type",
  252. [hwmon_temp_lcrit] = "temp%d_lcrit",
  253. [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
  254. [hwmon_temp_min] = "temp%d_min",
  255. [hwmon_temp_min_hyst] = "temp%d_min_hyst",
  256. [hwmon_temp_max] = "temp%d_max",
  257. [hwmon_temp_max_hyst] = "temp%d_max_hyst",
  258. [hwmon_temp_crit] = "temp%d_crit",
  259. [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
  260. [hwmon_temp_emergency] = "temp%d_emergency",
  261. [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
  262. [hwmon_temp_alarm] = "temp%d_alarm",
  263. [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
  264. [hwmon_temp_min_alarm] = "temp%d_min_alarm",
  265. [hwmon_temp_max_alarm] = "temp%d_max_alarm",
  266. [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
  267. [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
  268. [hwmon_temp_fault] = "temp%d_fault",
  269. [hwmon_temp_offset] = "temp%d_offset",
  270. [hwmon_temp_label] = "temp%d_label",
  271. [hwmon_temp_lowest] = "temp%d_lowest",
  272. [hwmon_temp_highest] = "temp%d_highest",
  273. [hwmon_temp_reset_history] = "temp%d_reset_history",
  274. };
  275. static const char * const hwmon_in_attr_templates[] = {
  276. [hwmon_in_input] = "in%d_input",
  277. [hwmon_in_min] = "in%d_min",
  278. [hwmon_in_max] = "in%d_max",
  279. [hwmon_in_lcrit] = "in%d_lcrit",
  280. [hwmon_in_crit] = "in%d_crit",
  281. [hwmon_in_average] = "in%d_average",
  282. [hwmon_in_lowest] = "in%d_lowest",
  283. [hwmon_in_highest] = "in%d_highest",
  284. [hwmon_in_reset_history] = "in%d_reset_history",
  285. [hwmon_in_label] = "in%d_label",
  286. [hwmon_in_alarm] = "in%d_alarm",
  287. [hwmon_in_min_alarm] = "in%d_min_alarm",
  288. [hwmon_in_max_alarm] = "in%d_max_alarm",
  289. [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
  290. [hwmon_in_crit_alarm] = "in%d_crit_alarm",
  291. };
  292. static const char * const hwmon_curr_attr_templates[] = {
  293. [hwmon_curr_input] = "curr%d_input",
  294. [hwmon_curr_min] = "curr%d_min",
  295. [hwmon_curr_max] = "curr%d_max",
  296. [hwmon_curr_lcrit] = "curr%d_lcrit",
  297. [hwmon_curr_crit] = "curr%d_crit",
  298. [hwmon_curr_average] = "curr%d_average",
  299. [hwmon_curr_lowest] = "curr%d_lowest",
  300. [hwmon_curr_highest] = "curr%d_highest",
  301. [hwmon_curr_reset_history] = "curr%d_reset_history",
  302. [hwmon_curr_label] = "curr%d_label",
  303. [hwmon_curr_alarm] = "curr%d_alarm",
  304. [hwmon_curr_min_alarm] = "curr%d_min_alarm",
  305. [hwmon_curr_max_alarm] = "curr%d_max_alarm",
  306. [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
  307. [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
  308. };
  309. static const char * const hwmon_power_attr_templates[] = {
  310. [hwmon_power_average] = "power%d_average",
  311. [hwmon_power_average_interval] = "power%d_average_interval",
  312. [hwmon_power_average_interval_max] = "power%d_interval_max",
  313. [hwmon_power_average_interval_min] = "power%d_interval_min",
  314. [hwmon_power_average_highest] = "power%d_average_highest",
  315. [hwmon_power_average_lowest] = "power%d_average_lowest",
  316. [hwmon_power_average_max] = "power%d_average_max",
  317. [hwmon_power_average_min] = "power%d_average_min",
  318. [hwmon_power_input] = "power%d_input",
  319. [hwmon_power_input_highest] = "power%d_input_highest",
  320. [hwmon_power_input_lowest] = "power%d_input_lowest",
  321. [hwmon_power_reset_history] = "power%d_reset_history",
  322. [hwmon_power_accuracy] = "power%d_accuracy",
  323. [hwmon_power_cap] = "power%d_cap",
  324. [hwmon_power_cap_hyst] = "power%d_cap_hyst",
  325. [hwmon_power_cap_max] = "power%d_cap_max",
  326. [hwmon_power_cap_min] = "power%d_cap_min",
  327. [hwmon_power_max] = "power%d_max",
  328. [hwmon_power_crit] = "power%d_crit",
  329. [hwmon_power_label] = "power%d_label",
  330. [hwmon_power_alarm] = "power%d_alarm",
  331. [hwmon_power_cap_alarm] = "power%d_cap_alarm",
  332. [hwmon_power_max_alarm] = "power%d_max_alarm",
  333. [hwmon_power_crit_alarm] = "power%d_crit_alarm",
  334. };
  335. static const char * const hwmon_energy_attr_templates[] = {
  336. [hwmon_energy_input] = "energy%d_input",
  337. [hwmon_energy_label] = "energy%d_label",
  338. };
  339. static const char * const hwmon_humidity_attr_templates[] = {
  340. [hwmon_humidity_input] = "humidity%d_input",
  341. [hwmon_humidity_label] = "humidity%d_label",
  342. [hwmon_humidity_min] = "humidity%d_min",
  343. [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
  344. [hwmon_humidity_max] = "humidity%d_max",
  345. [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
  346. [hwmon_humidity_alarm] = "humidity%d_alarm",
  347. [hwmon_humidity_fault] = "humidity%d_fault",
  348. };
  349. static const char * const hwmon_fan_attr_templates[] = {
  350. [hwmon_fan_input] = "fan%d_input",
  351. [hwmon_fan_label] = "fan%d_label",
  352. [hwmon_fan_min] = "fan%d_min",
  353. [hwmon_fan_max] = "fan%d_max",
  354. [hwmon_fan_div] = "fan%d_div",
  355. [hwmon_fan_pulses] = "fan%d_pulses",
  356. [hwmon_fan_target] = "fan%d_target",
  357. [hwmon_fan_alarm] = "fan%d_alarm",
  358. [hwmon_fan_min_alarm] = "fan%d_min_alarm",
  359. [hwmon_fan_max_alarm] = "fan%d_max_alarm",
  360. [hwmon_fan_fault] = "fan%d_fault",
  361. };
  362. static const char * const hwmon_pwm_attr_templates[] = {
  363. [hwmon_pwm_input] = "pwm%d",
  364. [hwmon_pwm_enable] = "pwm%d_enable",
  365. [hwmon_pwm_mode] = "pwm%d_mode",
  366. [hwmon_pwm_freq] = "pwm%d_freq",
  367. };
  368. static const char * const *__templates[] = {
  369. [hwmon_chip] = hwmon_chip_attr_templates,
  370. [hwmon_temp] = hwmon_temp_attr_templates,
  371. [hwmon_in] = hwmon_in_attr_templates,
  372. [hwmon_curr] = hwmon_curr_attr_templates,
  373. [hwmon_power] = hwmon_power_attr_templates,
  374. [hwmon_energy] = hwmon_energy_attr_templates,
  375. [hwmon_humidity] = hwmon_humidity_attr_templates,
  376. [hwmon_fan] = hwmon_fan_attr_templates,
  377. [hwmon_pwm] = hwmon_pwm_attr_templates,
  378. };
  379. static const int __templates_size[] = {
  380. [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
  381. [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
  382. [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
  383. [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
  384. [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
  385. [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
  386. [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
  387. [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
  388. [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
  389. };
  390. static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
  391. {
  392. int i, n;
  393. for (i = n = 0; info->config[i]; i++)
  394. n += hweight32(info->config[i]);
  395. return n;
  396. }
  397. static int hwmon_genattrs(struct device *dev,
  398. const void *drvdata,
  399. struct attribute **attrs,
  400. const struct hwmon_ops *ops,
  401. const struct hwmon_channel_info *info)
  402. {
  403. const char * const *templates;
  404. int template_size;
  405. int i, aindex = 0;
  406. if (info->type >= ARRAY_SIZE(__templates))
  407. return -EINVAL;
  408. templates = __templates[info->type];
  409. template_size = __templates_size[info->type];
  410. for (i = 0; info->config[i]; i++) {
  411. u32 attr_mask = info->config[i];
  412. u32 attr;
  413. while (attr_mask) {
  414. struct attribute *a;
  415. attr = __ffs(attr_mask);
  416. attr_mask &= ~BIT(attr);
  417. if (attr >= template_size)
  418. return -EINVAL;
  419. a = hwmon_genattr(dev, drvdata, info->type, attr, i,
  420. templates[attr], ops);
  421. if (IS_ERR(a)) {
  422. if (PTR_ERR(a) != -ENOENT)
  423. return PTR_ERR(a);
  424. continue;
  425. }
  426. attrs[aindex++] = a;
  427. }
  428. }
  429. return aindex;
  430. }
  431. static struct attribute **
  432. __hwmon_create_attrs(struct device *dev, const void *drvdata,
  433. const struct hwmon_chip_info *chip)
  434. {
  435. int ret, i, aindex = 0, nattrs = 0;
  436. struct attribute **attrs;
  437. for (i = 0; chip->info[i]; i++)
  438. nattrs += hwmon_num_channel_attrs(chip->info[i]);
  439. if (nattrs == 0)
  440. return ERR_PTR(-EINVAL);
  441. attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
  442. if (!attrs)
  443. return ERR_PTR(-ENOMEM);
  444. for (i = 0; chip->info[i]; i++) {
  445. ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
  446. chip->info[i]);
  447. if (ret < 0)
  448. return ERR_PTR(ret);
  449. aindex += ret;
  450. }
  451. return attrs;
  452. }
  453. static struct device *
  454. __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
  455. const struct hwmon_chip_info *chip,
  456. const struct attribute_group **groups)
  457. {
  458. struct hwmon_device *hwdev;
  459. struct device *hdev;
  460. int i, j, err, id;
  461. /* Do not accept invalid characters in hwmon name attribute */
  462. if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
  463. return ERR_PTR(-EINVAL);
  464. id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
  465. if (id < 0)
  466. return ERR_PTR(id);
  467. hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
  468. if (hwdev == NULL) {
  469. err = -ENOMEM;
  470. goto ida_remove;
  471. }
  472. hdev = &hwdev->dev;
  473. if (chip && chip->ops->is_visible) {
  474. struct attribute **attrs;
  475. int ngroups = 2;
  476. if (groups)
  477. for (i = 0; groups[i]; i++)
  478. ngroups++;
  479. hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
  480. GFP_KERNEL);
  481. if (!hwdev->groups) {
  482. err = -ENOMEM;
  483. goto free_hwmon;
  484. }
  485. attrs = __hwmon_create_attrs(dev, drvdata, chip);
  486. if (IS_ERR(attrs)) {
  487. err = PTR_ERR(attrs);
  488. goto free_hwmon;
  489. }
  490. hwdev->group.attrs = attrs;
  491. ngroups = 0;
  492. hwdev->groups[ngroups++] = &hwdev->group;
  493. if (groups) {
  494. for (i = 0; groups[i]; i++)
  495. hwdev->groups[ngroups++] = groups[i];
  496. }
  497. hdev->groups = hwdev->groups;
  498. } else {
  499. hdev->groups = groups;
  500. }
  501. hwdev->name = name;
  502. hdev->class = &hwmon_class;
  503. hdev->parent = dev;
  504. hdev->of_node = dev ? dev->of_node : NULL;
  505. hwdev->chip = chip;
  506. dev_set_drvdata(hdev, drvdata);
  507. dev_set_name(hdev, HWMON_ID_FORMAT, id);
  508. err = device_register(hdev);
  509. if (err)
  510. goto free_hwmon;
  511. if (chip && chip->ops->is_visible && chip->ops->read &&
  512. chip->info[0]->type == hwmon_chip &&
  513. (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
  514. const struct hwmon_channel_info **info = chip->info;
  515. for (i = 1; info[i]; i++) {
  516. if (info[i]->type != hwmon_temp)
  517. continue;
  518. for (j = 0; info[i]->config[j]; j++) {
  519. if (!chip->ops->is_visible(drvdata, hwmon_temp,
  520. hwmon_temp_input, j))
  521. continue;
  522. if (info[i]->config[j] & HWMON_T_INPUT)
  523. hwmon_thermal_add_sensor(dev, hwdev, j);
  524. }
  525. }
  526. }
  527. return hdev;
  528. free_hwmon:
  529. kfree(hwdev);
  530. ida_remove:
  531. ida_simple_remove(&hwmon_ida, id);
  532. return ERR_PTR(err);
  533. }
  534. /**
  535. * hwmon_device_register_with_groups - register w/ hwmon
  536. * @dev: the parent device
  537. * @name: hwmon name attribute
  538. * @drvdata: driver data to attach to created device
  539. * @groups: List of attribute groups to create
  540. *
  541. * hwmon_device_unregister() must be called when the device is no
  542. * longer needed.
  543. *
  544. * Returns the pointer to the new device.
  545. */
  546. struct device *
  547. hwmon_device_register_with_groups(struct device *dev, const char *name,
  548. void *drvdata,
  549. const struct attribute_group **groups)
  550. {
  551. return __hwmon_device_register(dev, name, drvdata, NULL, groups);
  552. }
  553. EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
  554. /**
  555. * hwmon_device_register_with_info - register w/ hwmon
  556. * @dev: the parent device
  557. * @name: hwmon name attribute
  558. * @drvdata: driver data to attach to created device
  559. * @info: Pointer to hwmon chip information
  560. * @groups - pointer to list of driver specific attribute groups
  561. *
  562. * hwmon_device_unregister() must be called when the device is no
  563. * longer needed.
  564. *
  565. * Returns the pointer to the new device.
  566. */
  567. struct device *
  568. hwmon_device_register_with_info(struct device *dev, const char *name,
  569. void *drvdata,
  570. const struct hwmon_chip_info *chip,
  571. const struct attribute_group **groups)
  572. {
  573. if (chip && (!chip->ops || !chip->info))
  574. return ERR_PTR(-EINVAL);
  575. return __hwmon_device_register(dev, name, drvdata, chip, groups);
  576. }
  577. EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
  578. /**
  579. * hwmon_device_register - register w/ hwmon
  580. * @dev: the device to register
  581. *
  582. * hwmon_device_unregister() must be called when the device is no
  583. * longer needed.
  584. *
  585. * Returns the pointer to the new device.
  586. */
  587. struct device *hwmon_device_register(struct device *dev)
  588. {
  589. return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
  590. }
  591. EXPORT_SYMBOL_GPL(hwmon_device_register);
  592. /**
  593. * hwmon_device_unregister - removes the previously registered class device
  594. *
  595. * @dev: the class device to destroy
  596. */
  597. void hwmon_device_unregister(struct device *dev)
  598. {
  599. int id;
  600. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  601. device_unregister(dev);
  602. ida_simple_remove(&hwmon_ida, id);
  603. } else
  604. dev_dbg(dev->parent,
  605. "hwmon_device_unregister() failed: bad class ID!\n");
  606. }
  607. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  608. static void devm_hwmon_release(struct device *dev, void *res)
  609. {
  610. struct device *hwdev = *(struct device **)res;
  611. hwmon_device_unregister(hwdev);
  612. }
  613. /**
  614. * devm_hwmon_device_register_with_groups - register w/ hwmon
  615. * @dev: the parent device
  616. * @name: hwmon name attribute
  617. * @drvdata: driver data to attach to created device
  618. * @groups: List of attribute groups to create
  619. *
  620. * Returns the pointer to the new device. The new device is automatically
  621. * unregistered with the parent device.
  622. */
  623. struct device *
  624. devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
  625. void *drvdata,
  626. const struct attribute_group **groups)
  627. {
  628. struct device **ptr, *hwdev;
  629. if (!dev)
  630. return ERR_PTR(-EINVAL);
  631. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  632. if (!ptr)
  633. return ERR_PTR(-ENOMEM);
  634. hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
  635. if (IS_ERR(hwdev))
  636. goto error;
  637. *ptr = hwdev;
  638. devres_add(dev, ptr);
  639. return hwdev;
  640. error:
  641. devres_free(ptr);
  642. return hwdev;
  643. }
  644. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
  645. /**
  646. * devm_hwmon_device_register_with_info - register w/ hwmon
  647. * @dev: the parent device
  648. * @name: hwmon name attribute
  649. * @drvdata: driver data to attach to created device
  650. * @info: Pointer to hwmon chip information
  651. * @groups - pointer to list of driver specific attribute groups
  652. *
  653. * Returns the pointer to the new device. The new device is automatically
  654. * unregistered with the parent device.
  655. */
  656. struct device *
  657. devm_hwmon_device_register_with_info(struct device *dev, const char *name,
  658. void *drvdata,
  659. const struct hwmon_chip_info *chip,
  660. const struct attribute_group **groups)
  661. {
  662. struct device **ptr, *hwdev;
  663. if (!dev)
  664. return ERR_PTR(-EINVAL);
  665. ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
  666. if (!ptr)
  667. return ERR_PTR(-ENOMEM);
  668. hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
  669. groups);
  670. if (IS_ERR(hwdev))
  671. goto error;
  672. *ptr = hwdev;
  673. devres_add(dev, ptr);
  674. return hwdev;
  675. error:
  676. devres_free(ptr);
  677. return hwdev;
  678. }
  679. EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
  680. static int devm_hwmon_match(struct device *dev, void *res, void *data)
  681. {
  682. struct device **hwdev = res;
  683. return *hwdev == data;
  684. }
  685. /**
  686. * devm_hwmon_device_unregister - removes a previously registered hwmon device
  687. *
  688. * @dev: the parent device of the device to unregister
  689. */
  690. void devm_hwmon_device_unregister(struct device *dev)
  691. {
  692. WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
  693. }
  694. EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
  695. static void __init hwmon_pci_quirks(void)
  696. {
  697. #if defined CONFIG_X86 && defined CONFIG_PCI
  698. struct pci_dev *sb;
  699. u16 base;
  700. u8 enable;
  701. /* Open access to 0x295-0x296 on MSI MS-7031 */
  702. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  703. if (sb) {
  704. if (sb->subsystem_vendor == 0x1462 && /* MSI */
  705. sb->subsystem_device == 0x0031) { /* MS-7031 */
  706. pci_read_config_byte(sb, 0x48, &enable);
  707. pci_read_config_word(sb, 0x64, &base);
  708. if (base == 0 && !(enable & BIT(2))) {
  709. dev_info(&sb->dev,
  710. "Opening wide generic port at 0x295\n");
  711. pci_write_config_word(sb, 0x64, 0x295);
  712. pci_write_config_byte(sb, 0x48,
  713. enable | BIT(2));
  714. }
  715. }
  716. pci_dev_put(sb);
  717. }
  718. #endif
  719. }
  720. static int __init hwmon_init(void)
  721. {
  722. int err;
  723. hwmon_pci_quirks();
  724. err = class_register(&hwmon_class);
  725. if (err) {
  726. pr_err("couldn't register hwmon sysfs class\n");
  727. return err;
  728. }
  729. return 0;
  730. }
  731. static void __exit hwmon_exit(void)
  732. {
  733. class_unregister(&hwmon_class);
  734. }
  735. subsys_initcall(hwmon_init);
  736. module_exit(hwmon_exit);
  737. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  738. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  739. MODULE_LICENSE("GPL");