macsmc-hwmon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Apple SMC hwmon driver for Apple Silicon platforms
  4. *
  5. * The System Management Controller on Apple Silicon devices is responsible for
  6. * measuring data from sensors across the SoC and machine. These include power,
  7. * temperature, voltage and current sensors. Some "sensors" actually expose
  8. * derived values. An example of this is the key PHPC, which is an estimate
  9. * of the heat energy being dissipated by the SoC.
  10. *
  11. * While each SoC only has one SMC variant, each platform exposes a different
  12. * set of sensors. For example, M1 MacBooks expose battery telemetry sensors
  13. * which are not present on the M1 Mac mini. For this reason, the available
  14. * sensors for a given platform are described in the device tree in a child
  15. * node of the SMC device. We must walk this list of available sensors and
  16. * populate the required hwmon data structures at runtime.
  17. *
  18. * Originally based on a concept by Jean-Francois Bortolotti <jeff@borto.fr>
  19. *
  20. * Copyright The Asahi Linux Contributors
  21. */
  22. #include <linux/bitfield.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/math64.h>
  25. #include <linux/mfd/macsmc.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #define MAX_LABEL_LENGTH 32
  30. /* Temperature, voltage, current, power, fan(s) */
  31. #define NUM_SENSOR_TYPES 5
  32. #define FLT_EXP_BIAS 127
  33. #define FLT_EXP_MASK GENMASK(30, 23)
  34. #define FLT_MANT_BIAS 23
  35. #define FLT_MANT_MASK GENMASK(22, 0)
  36. #define FLT_SIGN_MASK BIT(31)
  37. static bool fan_control;
  38. module_param_unsafe(fan_control, bool, 0644);
  39. MODULE_PARM_DESC(fan_control,
  40. "Override the SMC to set your own fan speeds on supported machines");
  41. struct macsmc_hwmon_sensor {
  42. struct apple_smc_key_info info;
  43. smc_key macsmc_key;
  44. char label[MAX_LABEL_LENGTH];
  45. u32 attrs;
  46. };
  47. struct macsmc_hwmon_fan {
  48. struct macsmc_hwmon_sensor now;
  49. struct macsmc_hwmon_sensor min;
  50. struct macsmc_hwmon_sensor max;
  51. struct macsmc_hwmon_sensor set;
  52. struct macsmc_hwmon_sensor mode;
  53. char label[MAX_LABEL_LENGTH];
  54. u32 attrs;
  55. bool manual;
  56. };
  57. struct macsmc_hwmon_sensors {
  58. struct hwmon_channel_info channel_info;
  59. struct macsmc_hwmon_sensor *sensors;
  60. u32 count;
  61. };
  62. struct macsmc_hwmon_fans {
  63. struct hwmon_channel_info channel_info;
  64. struct macsmc_hwmon_fan *fans;
  65. u32 count;
  66. };
  67. struct macsmc_hwmon {
  68. struct device *dev;
  69. struct apple_smc *smc;
  70. struct device *hwmon_dev;
  71. struct hwmon_chip_info chip_info;
  72. /* Chip + sensor types + NULL */
  73. const struct hwmon_channel_info *channel_infos[1 + NUM_SENSOR_TYPES + 1];
  74. struct macsmc_hwmon_sensors temp;
  75. struct macsmc_hwmon_sensors volt;
  76. struct macsmc_hwmon_sensors curr;
  77. struct macsmc_hwmon_sensors power;
  78. struct macsmc_hwmon_fans fan;
  79. };
  80. static int macsmc_hwmon_read_label(struct device *dev,
  81. enum hwmon_sensor_types type, u32 attr,
  82. int channel, const char **str)
  83. {
  84. struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
  85. switch (type) {
  86. case hwmon_temp:
  87. *str = hwmon->temp.sensors[channel].label;
  88. break;
  89. case hwmon_in:
  90. *str = hwmon->volt.sensors[channel].label;
  91. break;
  92. case hwmon_curr:
  93. *str = hwmon->curr.sensors[channel].label;
  94. break;
  95. case hwmon_power:
  96. *str = hwmon->power.sensors[channel].label;
  97. break;
  98. case hwmon_fan:
  99. *str = hwmon->fan.fans[channel].label;
  100. break;
  101. default:
  102. return -EOPNOTSUPP;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * A number of sensors report data in a 48.16 fixed-point decimal format that is
  108. * not used by any other function of the SMC.
  109. */
  110. static int macsmc_hwmon_read_ioft_scaled(struct apple_smc *smc, smc_key key,
  111. u64 *p, int scale)
  112. {
  113. u64 val;
  114. int ret;
  115. ret = apple_smc_read_u64(smc, key, &val);
  116. if (ret < 0)
  117. return ret;
  118. *p = mul_u64_u32_div(val, scale, 65536);
  119. return 0;
  120. }
  121. /*
  122. * Many sensors report their data as IEEE-754 floats. No other SMC function uses
  123. * them.
  124. */
  125. static int macsmc_hwmon_read_f32_scaled(struct apple_smc *smc, smc_key key,
  126. long *p, int scale)
  127. {
  128. u32 fval;
  129. u64 val;
  130. int ret, exp;
  131. ret = apple_smc_read_u32(smc, key, &fval);
  132. if (ret < 0)
  133. return ret;
  134. val = ((u64)((fval & FLT_MANT_MASK) | BIT(23)));
  135. exp = ((fval >> 23) & 0xff) - FLT_EXP_BIAS - FLT_MANT_BIAS;
  136. /* We never have negatively scaled SMC floats */
  137. val *= scale;
  138. if (exp > 63)
  139. val = U64_MAX;
  140. else if (exp < -63)
  141. val = 0;
  142. else if (exp < 0)
  143. val >>= -exp;
  144. else if (exp != 0 && (val & ~((1ULL << (64 - exp)) - 1))) /* overflow */
  145. val = U64_MAX;
  146. else
  147. val <<= exp;
  148. if (fval & FLT_SIGN_MASK) {
  149. if (val > (u64)LONG_MAX + 1)
  150. *p = LONG_MIN;
  151. else
  152. *p = -(long)val;
  153. } else {
  154. if (val > (u64)LONG_MAX)
  155. *p = LONG_MAX;
  156. else
  157. *p = (long)val;
  158. }
  159. return 0;
  160. }
  161. /*
  162. * The SMC has keys of multiple types, denoted by a FourCC of the same format
  163. * as the key ID. We don't know what data type a key encodes until we poke at it.
  164. */
  165. static int macsmc_hwmon_read_key(struct apple_smc *smc,
  166. struct macsmc_hwmon_sensor *sensor, int scale,
  167. long *val)
  168. {
  169. int ret;
  170. switch (sensor->info.type_code) {
  171. /* 32-bit IEEE 754 float */
  172. case __SMC_KEY('f', 'l', 't', ' '): {
  173. long flt_ = 0;
  174. ret = macsmc_hwmon_read_f32_scaled(smc, sensor->macsmc_key,
  175. &flt_, scale);
  176. if (ret)
  177. return ret;
  178. *val = flt_;
  179. break;
  180. }
  181. /* 48.16 fixed point decimal */
  182. case __SMC_KEY('i', 'o', 'f', 't'): {
  183. u64 ioft = 0;
  184. ret = macsmc_hwmon_read_ioft_scaled(smc, sensor->macsmc_key,
  185. &ioft, scale);
  186. if (ret)
  187. return ret;
  188. if (ioft > LONG_MAX)
  189. *val = LONG_MAX;
  190. else
  191. *val = (long)ioft;
  192. break;
  193. }
  194. default:
  195. return -EOPNOTSUPP;
  196. }
  197. return 0;
  198. }
  199. static int macsmc_hwmon_write_f32(struct apple_smc *smc, smc_key key, long value)
  200. {
  201. u64 val;
  202. u32 fval = 0;
  203. int exp, neg;
  204. neg = value < 0;
  205. val = abs(value);
  206. if (val) {
  207. exp = __fls(val);
  208. if (exp > 23)
  209. val >>= exp - 23;
  210. else
  211. val <<= 23 - exp;
  212. fval = FIELD_PREP(FLT_SIGN_MASK, neg) |
  213. FIELD_PREP(FLT_EXP_MASK, exp + FLT_EXP_BIAS) |
  214. FIELD_PREP(FLT_MANT_MASK, val & FLT_MANT_MASK);
  215. }
  216. return apple_smc_write_u32(smc, key, fval);
  217. }
  218. static int macsmc_hwmon_write_key(struct apple_smc *smc,
  219. struct macsmc_hwmon_sensor *sensor, long val)
  220. {
  221. switch (sensor->info.type_code) {
  222. /* 32-bit IEEE 754 float */
  223. case __SMC_KEY('f', 'l', 't', ' '):
  224. return macsmc_hwmon_write_f32(smc, sensor->macsmc_key, val);
  225. /* unsigned 8-bit integer */
  226. case __SMC_KEY('u', 'i', '8', ' '):
  227. return apple_smc_write_u8(smc, sensor->macsmc_key, val);
  228. default:
  229. return -EOPNOTSUPP;
  230. }
  231. }
  232. static int macsmc_hwmon_read_fan(struct macsmc_hwmon *hwmon, u32 attr, int chan,
  233. long *val)
  234. {
  235. switch (attr) {
  236. case hwmon_fan_input:
  237. return macsmc_hwmon_read_key(hwmon->smc,
  238. &hwmon->fan.fans[chan].now, 1, val);
  239. case hwmon_fan_min:
  240. return macsmc_hwmon_read_key(hwmon->smc,
  241. &hwmon->fan.fans[chan].min, 1, val);
  242. case hwmon_fan_max:
  243. return macsmc_hwmon_read_key(hwmon->smc,
  244. &hwmon->fan.fans[chan].max, 1, val);
  245. case hwmon_fan_target:
  246. return macsmc_hwmon_read_key(hwmon->smc,
  247. &hwmon->fan.fans[chan].set, 1, val);
  248. default:
  249. return -EOPNOTSUPP;
  250. }
  251. }
  252. static int macsmc_hwmon_write_fan(struct device *dev, u32 attr, int channel,
  253. long val)
  254. {
  255. struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
  256. long min, max;
  257. int ret;
  258. if (!fan_control || hwmon->fan.fans[channel].mode.macsmc_key == 0)
  259. return -EOPNOTSUPP;
  260. /*
  261. * The SMC does no sanity checks on requested fan speeds, so we need to.
  262. */
  263. ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->fan.fans[channel].min,
  264. 1, &min);
  265. if (ret)
  266. return ret;
  267. ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->fan.fans[channel].max,
  268. 1, &max);
  269. if (ret)
  270. return ret;
  271. if (val >= min && val <= max) {
  272. if (!hwmon->fan.fans[channel].manual) {
  273. /* Write 1 to mode key for manual control */
  274. ret = macsmc_hwmon_write_key(hwmon->smc,
  275. &hwmon->fan.fans[channel].mode, 1);
  276. if (ret < 0)
  277. return ret;
  278. hwmon->fan.fans[channel].manual = true;
  279. }
  280. return macsmc_hwmon_write_key(hwmon->smc,
  281. &hwmon->fan.fans[channel].set, val);
  282. } else if (!val) {
  283. if (hwmon->fan.fans[channel].manual) {
  284. ret = macsmc_hwmon_write_key(hwmon->smc,
  285. &hwmon->fan.fans[channel].mode, 0);
  286. if (ret < 0)
  287. return ret;
  288. hwmon->fan.fans[channel].manual = false;
  289. }
  290. } else {
  291. return -EINVAL;
  292. }
  293. return 0;
  294. }
  295. static int macsmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  296. u32 attr, int channel, long *val)
  297. {
  298. struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
  299. int ret = 0;
  300. switch (type) {
  301. case hwmon_temp:
  302. ret = macsmc_hwmon_read_key(hwmon->smc,
  303. &hwmon->temp.sensors[channel], 1000, val);
  304. break;
  305. case hwmon_in:
  306. ret = macsmc_hwmon_read_key(hwmon->smc,
  307. &hwmon->volt.sensors[channel], 1000, val);
  308. break;
  309. case hwmon_curr:
  310. ret = macsmc_hwmon_read_key(hwmon->smc,
  311. &hwmon->curr.sensors[channel], 1000, val);
  312. break;
  313. case hwmon_power:
  314. /* SMC returns power in Watts with acceptable precision to scale to uW */
  315. ret = macsmc_hwmon_read_key(hwmon->smc,
  316. &hwmon->power.sensors[channel],
  317. 1000000, val);
  318. break;
  319. case hwmon_fan:
  320. ret = macsmc_hwmon_read_fan(hwmon, attr, channel, val);
  321. break;
  322. default:
  323. return -EOPNOTSUPP;
  324. }
  325. return ret;
  326. }
  327. static int macsmc_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
  328. u32 attr, int channel, long val)
  329. {
  330. switch (type) {
  331. case hwmon_fan:
  332. return macsmc_hwmon_write_fan(dev, attr, channel, val);
  333. default:
  334. return -EOPNOTSUPP;
  335. }
  336. }
  337. static umode_t macsmc_hwmon_fan_is_visible(const struct macsmc_hwmon_fan *fan,
  338. u32 attr)
  339. {
  340. if (fan->attrs & BIT(attr)) {
  341. if (attr == hwmon_fan_target && fan_control && fan->mode.macsmc_key)
  342. return 0644;
  343. return 0444;
  344. }
  345. return 0;
  346. }
  347. static umode_t macsmc_hwmon_is_visible(const void *data,
  348. enum hwmon_sensor_types type, u32 attr,
  349. int channel)
  350. {
  351. const struct macsmc_hwmon *hwmon = data;
  352. struct macsmc_hwmon_sensor *sensor;
  353. switch (type) {
  354. case hwmon_in:
  355. sensor = &hwmon->volt.sensors[channel];
  356. break;
  357. case hwmon_curr:
  358. sensor = &hwmon->curr.sensors[channel];
  359. break;
  360. case hwmon_power:
  361. sensor = &hwmon->power.sensors[channel];
  362. break;
  363. case hwmon_temp:
  364. sensor = &hwmon->temp.sensors[channel];
  365. break;
  366. case hwmon_fan:
  367. return macsmc_hwmon_fan_is_visible(&hwmon->fan.fans[channel], attr);
  368. default:
  369. return 0;
  370. }
  371. /* Sensors only register ro attributes */
  372. if (sensor->attrs & BIT(attr))
  373. return 0444;
  374. return 0;
  375. }
  376. static const struct hwmon_ops macsmc_hwmon_ops = {
  377. .is_visible = macsmc_hwmon_is_visible,
  378. .read = macsmc_hwmon_read,
  379. .read_string = macsmc_hwmon_read_label,
  380. .write = macsmc_hwmon_write,
  381. };
  382. /*
  383. * Get the key metadata, including key data type, from the SMC.
  384. */
  385. static int macsmc_hwmon_parse_key(struct device *dev, struct apple_smc *smc,
  386. struct macsmc_hwmon_sensor *sensor,
  387. const char *key)
  388. {
  389. int ret;
  390. ret = apple_smc_get_key_info(smc, _SMC_KEY(key), &sensor->info);
  391. if (ret) {
  392. dev_dbg(dev, "Failed to retrieve key info for %s\n", key);
  393. return ret;
  394. }
  395. sensor->macsmc_key = _SMC_KEY(key);
  396. return 0;
  397. }
  398. /*
  399. * A sensor is a single key-value pair as made available by the SMC.
  400. * The devicetree gives us the SMC key ID and a friendly name where the
  401. * purpose of the sensor is known.
  402. */
  403. static int macsmc_hwmon_create_sensor(struct device *dev, struct apple_smc *smc,
  404. struct device_node *sensor_node,
  405. struct macsmc_hwmon_sensor *sensor)
  406. {
  407. const char *key, *label;
  408. int ret;
  409. ret = of_property_read_string(sensor_node, "apple,key-id", &key);
  410. if (ret) {
  411. dev_dbg(dev, "Could not find apple,key-id in sensor node\n");
  412. return ret;
  413. }
  414. ret = macsmc_hwmon_parse_key(dev, smc, sensor, key);
  415. if (ret)
  416. return ret;
  417. ret = of_property_read_string(sensor_node, "label", &label);
  418. if (ret)
  419. dev_dbg(dev, "No label found for sensor %s\n", key);
  420. else
  421. strscpy_pad(sensor->label, label, sizeof(sensor->label));
  422. return 0;
  423. }
  424. /*
  425. * Fan data is exposed by the SMC as multiple sensors.
  426. *
  427. * The devicetree schema reuses apple,key-id for the actual fan speed sensor.
  428. * Min, max and target keys do not need labels, so we can reuse label
  429. * for naming the entire fan.
  430. */
  431. static int macsmc_hwmon_create_fan(struct device *dev, struct apple_smc *smc,
  432. struct device_node *fan_node,
  433. struct macsmc_hwmon_fan *fan)
  434. {
  435. const char *label, *now, *min, *max, *set, *mode;
  436. int ret;
  437. ret = of_property_read_string(fan_node, "apple,key-id", &now);
  438. if (ret) {
  439. dev_err(dev, "apple,key-id not found in fan node!\n");
  440. return ret;
  441. }
  442. ret = macsmc_hwmon_parse_key(dev, smc, &fan->now, now);
  443. if (ret)
  444. return ret;
  445. fan->attrs = HWMON_F_INPUT;
  446. ret = of_property_read_string(fan_node, "label", &label);
  447. if (ret) {
  448. dev_dbg(dev, "No label found for fan %s\n", now);
  449. } else {
  450. strscpy_pad(fan->label, label, sizeof(fan->label));
  451. fan->attrs |= HWMON_F_LABEL;
  452. }
  453. /* The following keys are not required to simply monitor fan speed */
  454. if (!of_property_read_string(fan_node, "apple,fan-minimum", &min)) {
  455. ret = macsmc_hwmon_parse_key(dev, smc, &fan->min, min);
  456. if (ret)
  457. return ret;
  458. fan->attrs |= HWMON_F_MIN;
  459. }
  460. if (!of_property_read_string(fan_node, "apple,fan-maximum", &max)) {
  461. ret = macsmc_hwmon_parse_key(dev, smc, &fan->max, max);
  462. if (ret)
  463. return ret;
  464. fan->attrs |= HWMON_F_MAX;
  465. }
  466. if (!of_property_read_string(fan_node, "apple,fan-target", &set)) {
  467. ret = macsmc_hwmon_parse_key(dev, smc, &fan->set, set);
  468. if (ret)
  469. return ret;
  470. fan->attrs |= HWMON_F_TARGET;
  471. }
  472. if (!of_property_read_string(fan_node, "apple,fan-mode", &mode)) {
  473. ret = macsmc_hwmon_parse_key(dev, smc, &fan->mode, mode);
  474. if (ret)
  475. return ret;
  476. }
  477. /* Initialise fan control mode to automatic */
  478. fan->manual = false;
  479. return 0;
  480. }
  481. static int macsmc_hwmon_populate_sensors(struct macsmc_hwmon *hwmon,
  482. struct device_node *hwmon_node)
  483. {
  484. struct device_node *key_node __maybe_unused;
  485. struct macsmc_hwmon_sensor *sensor;
  486. u32 n_current = 0, n_fan = 0, n_power = 0, n_temperature = 0, n_voltage = 0;
  487. for_each_child_of_node_with_prefix(hwmon_node, key_node, "current-") {
  488. n_current++;
  489. }
  490. if (n_current) {
  491. hwmon->curr.sensors = devm_kcalloc(hwmon->dev, n_current,
  492. sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
  493. if (!hwmon->curr.sensors)
  494. return -ENOMEM;
  495. for_each_child_of_node_with_prefix(hwmon_node, key_node, "current-") {
  496. sensor = &hwmon->curr.sensors[hwmon->curr.count];
  497. if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
  498. sensor->attrs = HWMON_C_INPUT;
  499. if (*sensor->label)
  500. sensor->attrs |= HWMON_C_LABEL;
  501. hwmon->curr.count++;
  502. }
  503. }
  504. }
  505. for_each_child_of_node_with_prefix(hwmon_node, key_node, "fan-") {
  506. n_fan++;
  507. }
  508. if (n_fan) {
  509. hwmon->fan.fans = devm_kcalloc(hwmon->dev, n_fan,
  510. sizeof(struct macsmc_hwmon_fan), GFP_KERNEL);
  511. if (!hwmon->fan.fans)
  512. return -ENOMEM;
  513. for_each_child_of_node_with_prefix(hwmon_node, key_node, "fan-") {
  514. if (!macsmc_hwmon_create_fan(hwmon->dev, hwmon->smc, key_node,
  515. &hwmon->fan.fans[hwmon->fan.count]))
  516. hwmon->fan.count++;
  517. }
  518. }
  519. for_each_child_of_node_with_prefix(hwmon_node, key_node, "power-") {
  520. n_power++;
  521. }
  522. if (n_power) {
  523. hwmon->power.sensors = devm_kcalloc(hwmon->dev, n_power,
  524. sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
  525. if (!hwmon->power.sensors)
  526. return -ENOMEM;
  527. for_each_child_of_node_with_prefix(hwmon_node, key_node, "power-") {
  528. sensor = &hwmon->power.sensors[hwmon->power.count];
  529. if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
  530. sensor->attrs = HWMON_P_INPUT;
  531. if (*sensor->label)
  532. sensor->attrs |= HWMON_P_LABEL;
  533. hwmon->power.count++;
  534. }
  535. }
  536. }
  537. for_each_child_of_node_with_prefix(hwmon_node, key_node, "temperature-") {
  538. n_temperature++;
  539. }
  540. if (n_temperature) {
  541. hwmon->temp.sensors = devm_kcalloc(hwmon->dev, n_temperature,
  542. sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
  543. if (!hwmon->temp.sensors)
  544. return -ENOMEM;
  545. for_each_child_of_node_with_prefix(hwmon_node, key_node, "temperature-") {
  546. sensor = &hwmon->temp.sensors[hwmon->temp.count];
  547. if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
  548. sensor->attrs = HWMON_T_INPUT;
  549. if (*sensor->label)
  550. sensor->attrs |= HWMON_T_LABEL;
  551. hwmon->temp.count++;
  552. }
  553. }
  554. }
  555. for_each_child_of_node_with_prefix(hwmon_node, key_node, "voltage-") {
  556. n_voltage++;
  557. }
  558. if (n_voltage) {
  559. hwmon->volt.sensors = devm_kcalloc(hwmon->dev, n_voltage,
  560. sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
  561. if (!hwmon->volt.sensors)
  562. return -ENOMEM;
  563. for_each_child_of_node_with_prefix(hwmon_node, key_node, "voltage-") {
  564. sensor = &hwmon->volt.sensors[hwmon->volt.count];
  565. if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
  566. sensor->attrs = HWMON_I_INPUT;
  567. if (*sensor->label)
  568. sensor->attrs |= HWMON_I_LABEL;
  569. hwmon->volt.count++;
  570. }
  571. }
  572. }
  573. return 0;
  574. }
  575. /* Create NULL-terminated config arrays */
  576. static void macsmc_hwmon_populate_configs(u32 *configs, const struct macsmc_hwmon_sensors *sensors)
  577. {
  578. int idx;
  579. for (idx = 0; idx < sensors->count; idx++)
  580. configs[idx] = sensors->sensors[idx].attrs;
  581. }
  582. static void macsmc_hwmon_populate_fan_configs(u32 *configs, const struct macsmc_hwmon_fans *fans)
  583. {
  584. int idx;
  585. for (idx = 0; idx < fans->count; idx++)
  586. configs[idx] = fans->fans[idx].attrs;
  587. }
  588. static const struct hwmon_channel_info *const macsmc_chip_channel_info =
  589. HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ);
  590. static int macsmc_hwmon_create_infos(struct macsmc_hwmon *hwmon)
  591. {
  592. struct hwmon_channel_info *channel_info;
  593. int i = 0;
  594. /* chip */
  595. hwmon->channel_infos[i++] = macsmc_chip_channel_info;
  596. if (hwmon->curr.count) {
  597. channel_info = &hwmon->curr.channel_info;
  598. channel_info->type = hwmon_curr;
  599. channel_info->config = devm_kcalloc(hwmon->dev, hwmon->curr.count + 1,
  600. sizeof(u32), GFP_KERNEL);
  601. if (!channel_info->config)
  602. return -ENOMEM;
  603. macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->curr);
  604. hwmon->channel_infos[i++] = channel_info;
  605. }
  606. if (hwmon->fan.count) {
  607. channel_info = &hwmon->fan.channel_info;
  608. channel_info->type = hwmon_fan;
  609. channel_info->config = devm_kcalloc(hwmon->dev, hwmon->fan.count + 1,
  610. sizeof(u32), GFP_KERNEL);
  611. if (!channel_info->config)
  612. return -ENOMEM;
  613. macsmc_hwmon_populate_fan_configs((u32 *)channel_info->config, &hwmon->fan);
  614. hwmon->channel_infos[i++] = channel_info;
  615. }
  616. if (hwmon->power.count) {
  617. channel_info = &hwmon->power.channel_info;
  618. channel_info->type = hwmon_power;
  619. channel_info->config = devm_kcalloc(hwmon->dev, hwmon->power.count + 1,
  620. sizeof(u32), GFP_KERNEL);
  621. if (!channel_info->config)
  622. return -ENOMEM;
  623. macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->power);
  624. hwmon->channel_infos[i++] = channel_info;
  625. }
  626. if (hwmon->temp.count) {
  627. channel_info = &hwmon->temp.channel_info;
  628. channel_info->type = hwmon_temp;
  629. channel_info->config = devm_kcalloc(hwmon->dev, hwmon->temp.count + 1,
  630. sizeof(u32), GFP_KERNEL);
  631. if (!channel_info->config)
  632. return -ENOMEM;
  633. macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->temp);
  634. hwmon->channel_infos[i++] = channel_info;
  635. }
  636. if (hwmon->volt.count) {
  637. channel_info = &hwmon->volt.channel_info;
  638. channel_info->type = hwmon_in;
  639. channel_info->config = devm_kcalloc(hwmon->dev, hwmon->volt.count + 1,
  640. sizeof(u32), GFP_KERNEL);
  641. if (!channel_info->config)
  642. return -ENOMEM;
  643. macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->volt);
  644. hwmon->channel_infos[i++] = channel_info;
  645. }
  646. return 0;
  647. }
  648. static int macsmc_hwmon_probe(struct platform_device *pdev)
  649. {
  650. struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
  651. struct macsmc_hwmon *hwmon;
  652. int ret;
  653. /*
  654. * The MFD driver will try to probe us unconditionally. Some devices
  655. * with the SMC do not have hwmon capabilities. Only probe if we have
  656. * a hwmon node.
  657. */
  658. if (!pdev->dev.of_node)
  659. return -ENODEV;
  660. hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon),
  661. GFP_KERNEL);
  662. if (!hwmon)
  663. return -ENOMEM;
  664. hwmon->dev = &pdev->dev;
  665. hwmon->smc = smc;
  666. ret = macsmc_hwmon_populate_sensors(hwmon, hwmon->dev->of_node);
  667. if (ret) {
  668. dev_err(hwmon->dev, "Could not parse sensors\n");
  669. return ret;
  670. }
  671. if (!hwmon->curr.count && !hwmon->fan.count &&
  672. !hwmon->power.count && !hwmon->temp.count &&
  673. !hwmon->volt.count) {
  674. dev_err(hwmon->dev,
  675. "No valid sensors found of any supported type\n");
  676. return -ENODEV;
  677. }
  678. ret = macsmc_hwmon_create_infos(hwmon);
  679. if (ret)
  680. return ret;
  681. hwmon->chip_info.ops = &macsmc_hwmon_ops;
  682. hwmon->chip_info.info =
  683. (const struct hwmon_channel_info *const *)&hwmon->channel_infos;
  684. hwmon->hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  685. "macsmc_hwmon", hwmon,
  686. &hwmon->chip_info, NULL);
  687. if (IS_ERR(hwmon->hwmon_dev))
  688. return dev_err_probe(hwmon->dev, PTR_ERR(hwmon->hwmon_dev),
  689. "Probing SMC hwmon device failed\n");
  690. dev_dbg(hwmon->dev, "Registered SMC hwmon device. Sensors:\n");
  691. dev_dbg(hwmon->dev,
  692. "Current: %d, Fans: %d, Power: %d, Temperature: %d, Voltage: %d",
  693. hwmon->curr.count, hwmon->fan.count,
  694. hwmon->power.count, hwmon->temp.count,
  695. hwmon->volt.count);
  696. return 0;
  697. }
  698. static const struct of_device_id macsmc_hwmon_of_table[] = {
  699. { .compatible = "apple,smc-hwmon" },
  700. {}
  701. };
  702. MODULE_DEVICE_TABLE(of, macsmc_hwmon_of_table);
  703. static struct platform_driver macsmc_hwmon_driver = {
  704. .probe = macsmc_hwmon_probe,
  705. .driver = {
  706. .name = "macsmc-hwmon",
  707. .of_match_table = macsmc_hwmon_of_table,
  708. },
  709. };
  710. module_platform_driver(macsmc_hwmon_driver);
  711. MODULE_DESCRIPTION("Apple Silicon SMC hwmon driver");
  712. MODULE_AUTHOR("James Calligeros <jcalligeros99@gmail.com>");
  713. MODULE_LICENSE("Dual MIT/GPL");