ayaneo-ec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Platform driver for the Embedded Controller (EC) of Ayaneo devices. Handles
  4. * hwmon (fan speed, fan control), battery charge limits, and magic module
  5. * control (connected modules, controller disconnection).
  6. *
  7. * Copyright (C) 2025 Antheas Kapenekakis <lkml@antheas.dev>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/bits.h>
  11. #include <linux/dmi.h>
  12. #include <linux/err.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/sysfs.h>
  21. #include <acpi/battery.h>
  22. #define AYANEO_PWM_ENABLE_REG 0x4A
  23. #define AYANEO_PWM_REG 0x4B
  24. #define AYANEO_PWM_MODE_AUTO 0x00
  25. #define AYANEO_PWM_MODE_MANUAL 0x01
  26. #define AYANEO_FAN_REG 0x76
  27. #define EC_CHARGE_CONTROL_BEHAVIOURS \
  28. (BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | \
  29. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
  30. #define AYANEO_CHARGE_REG 0x1e
  31. #define AYANEO_CHARGE_VAL_AUTO 0xaa
  32. #define AYANEO_CHARGE_VAL_INHIBIT 0x55
  33. #define AYANEO_POWER_REG 0x2d
  34. #define AYANEO_POWER_OFF 0xfe
  35. #define AYANEO_POWER_ON 0xff
  36. #define AYANEO_MODULE_REG 0x2f
  37. #define AYANEO_MODULE_LEFT BIT(0)
  38. #define AYANEO_MODULE_RIGHT BIT(1)
  39. #define AYANEO_MODULE_MASK (AYANEO_MODULE_LEFT | AYANEO_MODULE_RIGHT)
  40. struct ayaneo_ec_quirk {
  41. bool has_fan_control;
  42. bool has_charge_control;
  43. bool has_magic_modules;
  44. };
  45. struct ayaneo_ec_platform_data {
  46. struct platform_device *pdev;
  47. struct ayaneo_ec_quirk *quirks;
  48. struct acpi_battery_hook battery_hook;
  49. // Protects access to restore_pwm
  50. struct mutex hwmon_lock;
  51. bool restore_charge_limit;
  52. bool restore_pwm;
  53. };
  54. static const struct ayaneo_ec_quirk quirk_fan = {
  55. .has_fan_control = true,
  56. };
  57. static const struct ayaneo_ec_quirk quirk_charge_limit = {
  58. .has_fan_control = true,
  59. .has_charge_control = true,
  60. };
  61. static const struct ayaneo_ec_quirk quirk_ayaneo3 = {
  62. .has_fan_control = true,
  63. .has_charge_control = true,
  64. .has_magic_modules = true,
  65. };
  66. static const struct dmi_system_id dmi_table[] = {
  67. {
  68. .matches = {
  69. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  70. DMI_MATCH(DMI_BOARD_NAME, "AYANEO 2"),
  71. },
  72. .driver_data = (void *)&quirk_fan,
  73. },
  74. {
  75. .matches = {
  76. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  77. DMI_MATCH(DMI_BOARD_NAME, "FLIP"),
  78. },
  79. .driver_data = (void *)&quirk_fan,
  80. },
  81. {
  82. .matches = {
  83. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  84. DMI_MATCH(DMI_BOARD_NAME, "GEEK"),
  85. },
  86. .driver_data = (void *)&quirk_fan,
  87. },
  88. {
  89. .matches = {
  90. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  91. DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR"),
  92. },
  93. .driver_data = (void *)&quirk_charge_limit,
  94. },
  95. {
  96. .matches = {
  97. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  98. DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR 1S"),
  99. },
  100. .driver_data = (void *)&quirk_charge_limit,
  101. },
  102. {
  103. .matches = {
  104. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  105. DMI_EXACT_MATCH(DMI_BOARD_NAME, "AB05-Mendocino"),
  106. },
  107. .driver_data = (void *)&quirk_charge_limit,
  108. },
  109. {
  110. .matches = {
  111. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  112. DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR Pro"),
  113. },
  114. .driver_data = (void *)&quirk_charge_limit,
  115. },
  116. {
  117. .matches = {
  118. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  119. DMI_EXACT_MATCH(DMI_BOARD_NAME, "KUN"),
  120. },
  121. .driver_data = (void *)&quirk_charge_limit,
  122. },
  123. {
  124. .matches = {
  125. DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
  126. DMI_EXACT_MATCH(DMI_BOARD_NAME, "AYANEO 3"),
  127. },
  128. .driver_data = (void *)&quirk_ayaneo3,
  129. },
  130. {},
  131. };
  132. /* Callbacks for hwmon interface */
  133. static umode_t ayaneo_ec_hwmon_is_visible(const void *drvdata,
  134. enum hwmon_sensor_types type, u32 attr,
  135. int channel)
  136. {
  137. switch (type) {
  138. case hwmon_fan:
  139. return 0444;
  140. case hwmon_pwm:
  141. return 0644;
  142. default:
  143. return 0;
  144. }
  145. }
  146. static int ayaneo_ec_read(struct device *dev, enum hwmon_sensor_types type,
  147. u32 attr, int channel, long *val)
  148. {
  149. u8 tmp;
  150. int ret;
  151. switch (type) {
  152. case hwmon_fan:
  153. switch (attr) {
  154. case hwmon_fan_input:
  155. ret = ec_read(AYANEO_FAN_REG, &tmp);
  156. if (ret)
  157. return ret;
  158. *val = tmp << 8;
  159. ret = ec_read(AYANEO_FAN_REG + 1, &tmp);
  160. if (ret)
  161. return ret;
  162. *val |= tmp;
  163. return 0;
  164. default:
  165. break;
  166. }
  167. break;
  168. case hwmon_pwm:
  169. switch (attr) {
  170. case hwmon_pwm_input:
  171. ret = ec_read(AYANEO_PWM_REG, &tmp);
  172. if (ret)
  173. return ret;
  174. if (tmp > 100)
  175. return -EIO;
  176. *val = (255 * tmp) / 100;
  177. return 0;
  178. case hwmon_pwm_enable:
  179. ret = ec_read(AYANEO_PWM_ENABLE_REG, &tmp);
  180. if (ret)
  181. return ret;
  182. if (tmp == AYANEO_PWM_MODE_MANUAL)
  183. *val = 1;
  184. else if (tmp == AYANEO_PWM_MODE_AUTO)
  185. *val = 2;
  186. else
  187. return -EIO;
  188. return 0;
  189. default:
  190. break;
  191. }
  192. break;
  193. default:
  194. break;
  195. }
  196. return -EOPNOTSUPP;
  197. }
  198. static int ayaneo_ec_write(struct device *dev, enum hwmon_sensor_types type,
  199. u32 attr, int channel, long val)
  200. {
  201. struct ayaneo_ec_platform_data *data = dev_get_drvdata(dev);
  202. int ret;
  203. guard(mutex)(&data->hwmon_lock);
  204. switch (type) {
  205. case hwmon_pwm:
  206. switch (attr) {
  207. case hwmon_pwm_enable:
  208. data->restore_pwm = false;
  209. switch (val) {
  210. case 1:
  211. return ec_write(AYANEO_PWM_ENABLE_REG,
  212. AYANEO_PWM_MODE_MANUAL);
  213. case 2:
  214. return ec_write(AYANEO_PWM_ENABLE_REG,
  215. AYANEO_PWM_MODE_AUTO);
  216. default:
  217. return -EINVAL;
  218. }
  219. case hwmon_pwm_input:
  220. if (val < 0 || val > 255)
  221. return -EINVAL;
  222. if (data->restore_pwm) {
  223. /*
  224. * Defer restoring PWM control to after
  225. * userspace resumes successfully
  226. */
  227. ret = ec_write(AYANEO_PWM_ENABLE_REG,
  228. AYANEO_PWM_MODE_MANUAL);
  229. if (ret)
  230. return ret;
  231. data->restore_pwm = false;
  232. }
  233. return ec_write(AYANEO_PWM_REG, (val * 100) / 255);
  234. default:
  235. break;
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. return -EOPNOTSUPP;
  242. }
  243. static const struct hwmon_ops ayaneo_ec_hwmon_ops = {
  244. .is_visible = ayaneo_ec_hwmon_is_visible,
  245. .read = ayaneo_ec_read,
  246. .write = ayaneo_ec_write,
  247. };
  248. static const struct hwmon_channel_info *const ayaneo_ec_sensors[] = {
  249. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
  250. HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
  251. NULL,
  252. };
  253. static const struct hwmon_chip_info ayaneo_ec_chip_info = {
  254. .ops = &ayaneo_ec_hwmon_ops,
  255. .info = ayaneo_ec_sensors,
  256. };
  257. static int ayaneo_psy_ext_get_prop(struct power_supply *psy,
  258. const struct power_supply_ext *ext,
  259. void *data,
  260. enum power_supply_property psp,
  261. union power_supply_propval *val)
  262. {
  263. int ret;
  264. u8 tmp;
  265. switch (psp) {
  266. case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
  267. ret = ec_read(AYANEO_CHARGE_REG, &tmp);
  268. if (ret)
  269. return ret;
  270. if (tmp == AYANEO_CHARGE_VAL_INHIBIT)
  271. val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
  272. else
  273. val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
  274. return 0;
  275. default:
  276. return -EINVAL;
  277. }
  278. }
  279. static int ayaneo_psy_ext_set_prop(struct power_supply *psy,
  280. const struct power_supply_ext *ext,
  281. void *data,
  282. enum power_supply_property psp,
  283. const union power_supply_propval *val)
  284. {
  285. u8 raw_val;
  286. switch (psp) {
  287. case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
  288. switch (val->intval) {
  289. case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
  290. raw_val = AYANEO_CHARGE_VAL_AUTO;
  291. break;
  292. case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
  293. raw_val = AYANEO_CHARGE_VAL_INHIBIT;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. return ec_write(AYANEO_CHARGE_REG, raw_val);
  299. default:
  300. return -EINVAL;
  301. }
  302. }
  303. static int ayaneo_psy_prop_is_writeable(struct power_supply *psy,
  304. const struct power_supply_ext *ext,
  305. void *data,
  306. enum power_supply_property psp)
  307. {
  308. return true;
  309. }
  310. static const enum power_supply_property ayaneo_psy_ext_props[] = {
  311. POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
  312. };
  313. static const struct power_supply_ext ayaneo_psy_ext = {
  314. .name = "ayaneo-charge-control",
  315. .properties = ayaneo_psy_ext_props,
  316. .num_properties = ARRAY_SIZE(ayaneo_psy_ext_props),
  317. .charge_behaviours = EC_CHARGE_CONTROL_BEHAVIOURS,
  318. .get_property = ayaneo_psy_ext_get_prop,
  319. .set_property = ayaneo_psy_ext_set_prop,
  320. .property_is_writeable = ayaneo_psy_prop_is_writeable,
  321. };
  322. static int ayaneo_add_battery(struct power_supply *battery,
  323. struct acpi_battery_hook *hook)
  324. {
  325. struct ayaneo_ec_platform_data *data =
  326. container_of(hook, struct ayaneo_ec_platform_data, battery_hook);
  327. return power_supply_register_extension(battery, &ayaneo_psy_ext,
  328. &data->pdev->dev, NULL);
  329. }
  330. static int ayaneo_remove_battery(struct power_supply *battery,
  331. struct acpi_battery_hook *hook)
  332. {
  333. power_supply_unregister_extension(battery, &ayaneo_psy_ext);
  334. return 0;
  335. }
  336. static ssize_t controller_power_store(struct device *dev,
  337. struct device_attribute *attr,
  338. const char *buf,
  339. size_t count)
  340. {
  341. bool value;
  342. int ret;
  343. ret = kstrtobool(buf, &value);
  344. if (ret)
  345. return ret;
  346. ret = ec_write(AYANEO_POWER_REG, value ? AYANEO_POWER_ON : AYANEO_POWER_OFF);
  347. if (ret)
  348. return ret;
  349. return count;
  350. }
  351. static ssize_t controller_power_show(struct device *dev,
  352. struct device_attribute *attr,
  353. char *buf)
  354. {
  355. int ret;
  356. u8 val;
  357. ret = ec_read(AYANEO_POWER_REG, &val);
  358. if (ret)
  359. return ret;
  360. return sysfs_emit(buf, "%d\n", val == AYANEO_POWER_ON);
  361. }
  362. static DEVICE_ATTR_RW(controller_power);
  363. static ssize_t controller_modules_show(struct device *dev,
  364. struct device_attribute *attr, char *buf)
  365. {
  366. u8 unconnected_modules;
  367. char *out;
  368. int ret;
  369. ret = ec_read(AYANEO_MODULE_REG, &unconnected_modules);
  370. if (ret)
  371. return ret;
  372. switch (~unconnected_modules & AYANEO_MODULE_MASK) {
  373. case AYANEO_MODULE_LEFT | AYANEO_MODULE_RIGHT:
  374. out = "both";
  375. break;
  376. case AYANEO_MODULE_LEFT:
  377. out = "left";
  378. break;
  379. case AYANEO_MODULE_RIGHT:
  380. out = "right";
  381. break;
  382. default:
  383. out = "none";
  384. break;
  385. }
  386. return sysfs_emit(buf, "%s\n", out);
  387. }
  388. static DEVICE_ATTR_RO(controller_modules);
  389. static struct attribute *aya_mm_attrs[] = {
  390. &dev_attr_controller_power.attr,
  391. &dev_attr_controller_modules.attr,
  392. NULL
  393. };
  394. static umode_t aya_mm_is_visible(struct kobject *kobj,
  395. struct attribute *attr, int n)
  396. {
  397. struct device *dev = kobj_to_dev(kobj);
  398. struct platform_device *pdev = to_platform_device(dev);
  399. struct ayaneo_ec_platform_data *data = platform_get_drvdata(pdev);
  400. if (data->quirks->has_magic_modules)
  401. return attr->mode;
  402. return 0;
  403. }
  404. static const struct attribute_group aya_mm_attribute_group = {
  405. .is_visible = aya_mm_is_visible,
  406. .attrs = aya_mm_attrs,
  407. };
  408. static const struct attribute_group *ayaneo_ec_groups[] = {
  409. &aya_mm_attribute_group,
  410. NULL
  411. };
  412. static int ayaneo_ec_probe(struct platform_device *pdev)
  413. {
  414. const struct dmi_system_id *dmi_entry;
  415. struct ayaneo_ec_platform_data *data;
  416. struct device *hwdev;
  417. int ret;
  418. dmi_entry = dmi_first_match(dmi_table);
  419. if (!dmi_entry)
  420. return -ENODEV;
  421. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  422. if (!data)
  423. return -ENOMEM;
  424. data->pdev = pdev;
  425. data->quirks = dmi_entry->driver_data;
  426. ret = devm_mutex_init(&pdev->dev, &data->hwmon_lock);
  427. if (ret)
  428. return ret;
  429. platform_set_drvdata(pdev, data);
  430. if (data->quirks->has_fan_control) {
  431. hwdev = devm_hwmon_device_register_with_info(&pdev->dev,
  432. "ayaneo_ec", data, &ayaneo_ec_chip_info, NULL);
  433. if (IS_ERR(hwdev))
  434. return PTR_ERR(hwdev);
  435. }
  436. if (data->quirks->has_charge_control) {
  437. data->battery_hook.add_battery = ayaneo_add_battery;
  438. data->battery_hook.remove_battery = ayaneo_remove_battery;
  439. data->battery_hook.name = "Ayaneo Battery";
  440. ret = devm_battery_hook_register(&pdev->dev, &data->battery_hook);
  441. if (ret)
  442. return ret;
  443. }
  444. return 0;
  445. }
  446. static int ayaneo_freeze(struct device *dev)
  447. {
  448. struct platform_device *pdev = to_platform_device(dev);
  449. struct ayaneo_ec_platform_data *data = platform_get_drvdata(pdev);
  450. int ret;
  451. u8 tmp;
  452. if (data->quirks->has_charge_control) {
  453. ret = ec_read(AYANEO_CHARGE_REG, &tmp);
  454. if (ret)
  455. return ret;
  456. data->restore_charge_limit = tmp == AYANEO_CHARGE_VAL_INHIBIT;
  457. }
  458. if (data->quirks->has_fan_control) {
  459. ret = ec_read(AYANEO_PWM_ENABLE_REG, &tmp);
  460. if (ret)
  461. return ret;
  462. data->restore_pwm = tmp == AYANEO_PWM_MODE_MANUAL;
  463. /*
  464. * Release the fan when entering hibernation to avoid
  465. * overheating if hibernation fails and hangs.
  466. */
  467. if (data->restore_pwm) {
  468. ret = ec_write(AYANEO_PWM_ENABLE_REG, AYANEO_PWM_MODE_AUTO);
  469. if (ret)
  470. return ret;
  471. }
  472. }
  473. return 0;
  474. }
  475. static int ayaneo_restore(struct device *dev)
  476. {
  477. struct platform_device *pdev = to_platform_device(dev);
  478. struct ayaneo_ec_platform_data *data = platform_get_drvdata(pdev);
  479. int ret;
  480. if (data->quirks->has_charge_control && data->restore_charge_limit) {
  481. ret = ec_write(AYANEO_CHARGE_REG, AYANEO_CHARGE_VAL_INHIBIT);
  482. if (ret)
  483. return ret;
  484. }
  485. return 0;
  486. }
  487. static const struct dev_pm_ops ayaneo_pm_ops = {
  488. .freeze = ayaneo_freeze,
  489. .restore = ayaneo_restore,
  490. };
  491. static struct platform_driver ayaneo_platform_driver = {
  492. .driver = {
  493. .name = "ayaneo-ec",
  494. .dev_groups = ayaneo_ec_groups,
  495. .pm = pm_sleep_ptr(&ayaneo_pm_ops),
  496. },
  497. .probe = ayaneo_ec_probe,
  498. };
  499. static struct platform_device *ayaneo_platform_device;
  500. static int __init ayaneo_ec_init(void)
  501. {
  502. ayaneo_platform_device =
  503. platform_create_bundle(&ayaneo_platform_driver,
  504. ayaneo_ec_probe, NULL, 0, NULL, 0);
  505. return PTR_ERR_OR_ZERO(ayaneo_platform_device);
  506. }
  507. static void __exit ayaneo_ec_exit(void)
  508. {
  509. platform_device_unregister(ayaneo_platform_device);
  510. platform_driver_unregister(&ayaneo_platform_driver);
  511. }
  512. MODULE_DEVICE_TABLE(dmi, dmi_table);
  513. module_init(ayaneo_ec_init);
  514. module_exit(ayaneo_ec_exit);
  515. MODULE_AUTHOR("Antheas Kapenekakis <lkml@antheas.dev>");
  516. MODULE_DESCRIPTION("Ayaneo Embedded Controller (EC) platform features");
  517. MODULE_LICENSE("GPL");