power_supply_hwmon.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * power_supply_hwmon.c - power supply hwmon support.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/hwmon.h>
  7. #include <linux/power_supply.h>
  8. #include <linux/slab.h>
  9. struct power_supply_hwmon {
  10. struct power_supply *psy;
  11. unsigned long *props;
  12. };
  13. static const char *const ps_temp_label[] = {
  14. "temp",
  15. "ambient temp",
  16. };
  17. static int power_supply_hwmon_in_to_property(u32 attr)
  18. {
  19. switch (attr) {
  20. case hwmon_in_average:
  21. return POWER_SUPPLY_PROP_VOLTAGE_AVG;
  22. case hwmon_in_min:
  23. return POWER_SUPPLY_PROP_VOLTAGE_MIN;
  24. case hwmon_in_max:
  25. return POWER_SUPPLY_PROP_VOLTAGE_MAX;
  26. case hwmon_in_input:
  27. return POWER_SUPPLY_PROP_VOLTAGE_NOW;
  28. default:
  29. return -EINVAL;
  30. }
  31. }
  32. static int power_supply_hwmon_curr_to_property(u32 attr)
  33. {
  34. switch (attr) {
  35. case hwmon_curr_average:
  36. return POWER_SUPPLY_PROP_CURRENT_AVG;
  37. case hwmon_curr_max:
  38. return POWER_SUPPLY_PROP_CURRENT_MAX;
  39. case hwmon_curr_input:
  40. return POWER_SUPPLY_PROP_CURRENT_NOW;
  41. default:
  42. return -EINVAL;
  43. }
  44. }
  45. static int power_supply_hwmon_temp_to_property(u32 attr, int channel)
  46. {
  47. if (channel) {
  48. switch (attr) {
  49. case hwmon_temp_input:
  50. return POWER_SUPPLY_PROP_TEMP_AMBIENT;
  51. case hwmon_temp_min_alarm:
  52. return POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN;
  53. case hwmon_temp_max_alarm:
  54. return POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX;
  55. default:
  56. break;
  57. }
  58. } else {
  59. switch (attr) {
  60. case hwmon_temp_input:
  61. return POWER_SUPPLY_PROP_TEMP;
  62. case hwmon_temp_max:
  63. return POWER_SUPPLY_PROP_TEMP_MAX;
  64. case hwmon_temp_min:
  65. return POWER_SUPPLY_PROP_TEMP_MIN;
  66. case hwmon_temp_min_alarm:
  67. return POWER_SUPPLY_PROP_TEMP_ALERT_MIN;
  68. case hwmon_temp_max_alarm:
  69. return POWER_SUPPLY_PROP_TEMP_ALERT_MAX;
  70. default:
  71. break;
  72. }
  73. }
  74. return -EINVAL;
  75. }
  76. static int
  77. power_supply_hwmon_to_property(enum hwmon_sensor_types type,
  78. u32 attr, int channel)
  79. {
  80. switch (type) {
  81. case hwmon_in:
  82. return power_supply_hwmon_in_to_property(attr);
  83. case hwmon_curr:
  84. return power_supply_hwmon_curr_to_property(attr);
  85. case hwmon_temp:
  86. return power_supply_hwmon_temp_to_property(attr, channel);
  87. default:
  88. return -EINVAL;
  89. }
  90. }
  91. static bool power_supply_hwmon_is_a_label(enum hwmon_sensor_types type,
  92. u32 attr)
  93. {
  94. return type == hwmon_temp && attr == hwmon_temp_label;
  95. }
  96. struct hwmon_type_attr_list {
  97. const u32 *attrs;
  98. size_t n_attrs;
  99. };
  100. static const u32 ps_temp_attrs[] = {
  101. hwmon_temp_input,
  102. hwmon_temp_min, hwmon_temp_max,
  103. hwmon_temp_min_alarm, hwmon_temp_max_alarm,
  104. };
  105. static const struct hwmon_type_attr_list ps_type_attrs[hwmon_max] = {
  106. [hwmon_temp] = { ps_temp_attrs, ARRAY_SIZE(ps_temp_attrs) },
  107. };
  108. static bool power_supply_hwmon_has_input(
  109. const struct power_supply_hwmon *psyhw,
  110. enum hwmon_sensor_types type, int channel)
  111. {
  112. const struct hwmon_type_attr_list *attr_list = &ps_type_attrs[type];
  113. size_t i;
  114. for (i = 0; i < attr_list->n_attrs; ++i) {
  115. int prop = power_supply_hwmon_to_property(type,
  116. attr_list->attrs[i], channel);
  117. if (prop >= 0 && test_bit(prop, psyhw->props))
  118. return true;
  119. }
  120. return false;
  121. }
  122. static bool power_supply_hwmon_is_writable(enum hwmon_sensor_types type,
  123. u32 attr)
  124. {
  125. switch (type) {
  126. case hwmon_in:
  127. return attr == hwmon_in_min ||
  128. attr == hwmon_in_max;
  129. case hwmon_curr:
  130. return attr == hwmon_curr_max;
  131. case hwmon_temp:
  132. return attr == hwmon_temp_max ||
  133. attr == hwmon_temp_min ||
  134. attr == hwmon_temp_min_alarm ||
  135. attr == hwmon_temp_max_alarm;
  136. default:
  137. return false;
  138. }
  139. }
  140. static umode_t power_supply_hwmon_is_visible(const void *data,
  141. enum hwmon_sensor_types type,
  142. u32 attr, int channel)
  143. {
  144. const struct power_supply_hwmon *psyhw = data;
  145. int prop;
  146. if (power_supply_hwmon_is_a_label(type, attr)) {
  147. if (power_supply_hwmon_has_input(psyhw, type, channel))
  148. return 0444;
  149. else
  150. return 0;
  151. }
  152. prop = power_supply_hwmon_to_property(type, attr, channel);
  153. if (prop < 0 || !test_bit(prop, psyhw->props))
  154. return 0;
  155. if (power_supply_property_is_writeable(psyhw->psy, prop) > 0 &&
  156. power_supply_hwmon_is_writable(type, attr))
  157. return 0644;
  158. return 0444;
  159. }
  160. static int power_supply_hwmon_read_string(struct device *dev,
  161. enum hwmon_sensor_types type,
  162. u32 attr, int channel,
  163. const char **str)
  164. {
  165. switch (type) {
  166. case hwmon_temp:
  167. *str = ps_temp_label[channel];
  168. break;
  169. default:
  170. /* unreachable, but see:
  171. * gcc bug #51513 [1] and clang bug #978 [2]
  172. *
  173. * [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51513
  174. * [2] https://github.com/ClangBuiltLinux/linux/issues/978
  175. */
  176. break;
  177. }
  178. return 0;
  179. }
  180. static int
  181. power_supply_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  182. u32 attr, int channel, long *val)
  183. {
  184. struct power_supply_hwmon *psyhw = dev_get_drvdata(dev);
  185. struct power_supply *psy = psyhw->psy;
  186. union power_supply_propval pspval;
  187. int ret, prop;
  188. prop = power_supply_hwmon_to_property(type, attr, channel);
  189. if (prop < 0)
  190. return prop;
  191. ret = power_supply_get_property(psy, prop, &pspval);
  192. if (ret)
  193. return ret;
  194. switch (type) {
  195. /*
  196. * Both voltage and current is reported in units of
  197. * microvolts/microamps, so we need to adjust it to
  198. * milliamps(volts)
  199. */
  200. case hwmon_curr:
  201. case hwmon_in:
  202. pspval.intval = DIV_ROUND_CLOSEST(pspval.intval, 1000);
  203. break;
  204. /*
  205. * Temp needs to be converted from 1/10 C to milli-C
  206. */
  207. case hwmon_temp:
  208. if (check_mul_overflow(pspval.intval, 100,
  209. &pspval.intval))
  210. return -EOVERFLOW;
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. *val = pspval.intval;
  216. return 0;
  217. }
  218. static int
  219. power_supply_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
  220. u32 attr, int channel, long val)
  221. {
  222. struct power_supply_hwmon *psyhw = dev_get_drvdata(dev);
  223. struct power_supply *psy = psyhw->psy;
  224. union power_supply_propval pspval;
  225. int prop;
  226. prop = power_supply_hwmon_to_property(type, attr, channel);
  227. if (prop < 0)
  228. return prop;
  229. pspval.intval = val;
  230. switch (type) {
  231. /*
  232. * Both voltage and current is reported in units of
  233. * microvolts/microamps, so we need to adjust it to
  234. * milliamps(volts)
  235. */
  236. case hwmon_curr:
  237. case hwmon_in:
  238. if (check_mul_overflow(pspval.intval, 1000,
  239. &pspval.intval))
  240. return -EOVERFLOW;
  241. break;
  242. /*
  243. * Temp needs to be converted from 1/10 C to milli-C
  244. */
  245. case hwmon_temp:
  246. pspval.intval = DIV_ROUND_CLOSEST(pspval.intval, 100);
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. return power_supply_set_property(psy, prop, &pspval);
  252. }
  253. static const struct hwmon_ops power_supply_hwmon_ops = {
  254. .is_visible = power_supply_hwmon_is_visible,
  255. .read = power_supply_hwmon_read,
  256. .write = power_supply_hwmon_write,
  257. .read_string = power_supply_hwmon_read_string,
  258. };
  259. static const struct hwmon_channel_info *power_supply_hwmon_info[] = {
  260. HWMON_CHANNEL_INFO(temp,
  261. HWMON_T_LABEL |
  262. HWMON_T_INPUT |
  263. HWMON_T_MAX |
  264. HWMON_T_MIN |
  265. HWMON_T_MIN_ALARM |
  266. HWMON_T_MIN_ALARM,
  267. HWMON_T_LABEL |
  268. HWMON_T_INPUT |
  269. HWMON_T_MIN_ALARM |
  270. HWMON_T_LABEL |
  271. HWMON_T_MAX_ALARM),
  272. HWMON_CHANNEL_INFO(curr,
  273. HWMON_C_AVERAGE |
  274. HWMON_C_MAX |
  275. HWMON_C_INPUT),
  276. HWMON_CHANNEL_INFO(in,
  277. HWMON_I_AVERAGE |
  278. HWMON_I_MIN |
  279. HWMON_I_MAX |
  280. HWMON_I_INPUT),
  281. NULL
  282. };
  283. static const struct hwmon_chip_info power_supply_hwmon_chip_info = {
  284. .ops = &power_supply_hwmon_ops,
  285. .info = power_supply_hwmon_info,
  286. };
  287. static void power_supply_hwmon_bitmap_free(void *data)
  288. {
  289. bitmap_free(data);
  290. }
  291. int power_supply_add_hwmon_sysfs(struct power_supply *psy)
  292. {
  293. const struct power_supply_desc *desc = psy->desc;
  294. struct power_supply_hwmon *psyhw;
  295. struct device *dev = &psy->dev;
  296. struct device *hwmon;
  297. int ret, i;
  298. const char *name;
  299. if (!devres_open_group(dev, power_supply_add_hwmon_sysfs,
  300. GFP_KERNEL))
  301. return -ENOMEM;
  302. psyhw = devm_kzalloc(dev, sizeof(*psyhw), GFP_KERNEL);
  303. if (!psyhw) {
  304. ret = -ENOMEM;
  305. goto error;
  306. }
  307. psyhw->psy = psy;
  308. psyhw->props = bitmap_zalloc(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG + 1,
  309. GFP_KERNEL);
  310. if (!psyhw->props) {
  311. ret = -ENOMEM;
  312. goto error;
  313. }
  314. ret = devm_add_action_or_reset(dev, power_supply_hwmon_bitmap_free,
  315. psyhw->props);
  316. if (ret)
  317. goto error;
  318. for (i = 0; i < desc->num_properties; i++) {
  319. const enum power_supply_property prop = desc->properties[i];
  320. switch (prop) {
  321. case POWER_SUPPLY_PROP_CURRENT_AVG:
  322. case POWER_SUPPLY_PROP_CURRENT_MAX:
  323. case POWER_SUPPLY_PROP_CURRENT_NOW:
  324. case POWER_SUPPLY_PROP_TEMP:
  325. case POWER_SUPPLY_PROP_TEMP_MAX:
  326. case POWER_SUPPLY_PROP_TEMP_MIN:
  327. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  328. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  329. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  330. case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
  331. case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
  332. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  333. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  334. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  335. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  336. set_bit(prop, psyhw->props);
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. name = psy->desc->name;
  343. if (strchr(name, '-')) {
  344. char *new_name;
  345. new_name = devm_kstrdup(dev, name, GFP_KERNEL);
  346. if (!new_name) {
  347. ret = -ENOMEM;
  348. goto error;
  349. }
  350. strreplace(new_name, '-', '_');
  351. name = new_name;
  352. }
  353. hwmon = devm_hwmon_device_register_with_info(dev, name,
  354. psyhw,
  355. &power_supply_hwmon_chip_info,
  356. NULL);
  357. ret = PTR_ERR_OR_ZERO(hwmon);
  358. if (ret)
  359. goto error;
  360. devres_close_group(dev, power_supply_add_hwmon_sysfs);
  361. return 0;
  362. error:
  363. devres_release_group(dev, NULL);
  364. return ret;
  365. }
  366. void power_supply_remove_hwmon_sysfs(struct power_supply *psy)
  367. {
  368. devres_release_group(&psy->dev, power_supply_add_hwmon_sysfs);
  369. }