hwmon.c 24 KB

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