hwmon.c 23 KB

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