alienware-wmi-base.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Alienware special feature control
  4. *
  5. * Copyright (C) 2014 Dell Inc <Dell.Client.Kernel@dell.com>
  6. * Copyright (C) 2025 Kurt Borja <kuurtb@gmail.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/acpi.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dmi.h>
  14. #include <linux/leds.h>
  15. #include "alienware-wmi.h"
  16. MODULE_AUTHOR("Mario Limonciello <mario.limonciello@outlook.com>");
  17. MODULE_AUTHOR("Kurt Borja <kuurtb@gmail.com>");
  18. MODULE_DESCRIPTION("Alienware special feature control");
  19. MODULE_LICENSE("GPL");
  20. struct alienfx_quirks *alienfx;
  21. static struct alienfx_quirks quirk_inspiron5675 = {
  22. .num_zones = 2,
  23. .hdmi_mux = false,
  24. .amplifier = false,
  25. .deepslp = false,
  26. };
  27. static struct alienfx_quirks quirk_unknown = {
  28. .num_zones = 2,
  29. .hdmi_mux = false,
  30. .amplifier = false,
  31. .deepslp = false,
  32. };
  33. static struct alienfx_quirks quirk_x51_r1_r2 = {
  34. .num_zones = 3,
  35. .hdmi_mux = false,
  36. .amplifier = false,
  37. .deepslp = false,
  38. };
  39. static struct alienfx_quirks quirk_x51_r3 = {
  40. .num_zones = 4,
  41. .hdmi_mux = false,
  42. .amplifier = true,
  43. .deepslp = false,
  44. };
  45. static struct alienfx_quirks quirk_asm100 = {
  46. .num_zones = 2,
  47. .hdmi_mux = true,
  48. .amplifier = false,
  49. .deepslp = false,
  50. };
  51. static struct alienfx_quirks quirk_asm200 = {
  52. .num_zones = 2,
  53. .hdmi_mux = true,
  54. .amplifier = false,
  55. .deepslp = true,
  56. };
  57. static struct alienfx_quirks quirk_asm201 = {
  58. .num_zones = 2,
  59. .hdmi_mux = true,
  60. .amplifier = true,
  61. .deepslp = true,
  62. };
  63. static int __init dmi_matched(const struct dmi_system_id *dmi)
  64. {
  65. alienfx = dmi->driver_data;
  66. return 1;
  67. }
  68. static const struct dmi_system_id alienware_quirks[] __initconst = {
  69. {
  70. .callback = dmi_matched,
  71. .ident = "Alienware ASM100",
  72. .matches = {
  73. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  74. DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
  75. },
  76. .driver_data = &quirk_asm100,
  77. },
  78. {
  79. .callback = dmi_matched,
  80. .ident = "Alienware ASM200",
  81. .matches = {
  82. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  83. DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
  84. },
  85. .driver_data = &quirk_asm200,
  86. },
  87. {
  88. .callback = dmi_matched,
  89. .ident = "Alienware ASM201",
  90. .matches = {
  91. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  92. DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
  93. },
  94. .driver_data = &quirk_asm201,
  95. },
  96. {
  97. .callback = dmi_matched,
  98. .ident = "Alienware X51 R1",
  99. .matches = {
  100. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  101. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
  102. },
  103. .driver_data = &quirk_x51_r1_r2,
  104. },
  105. {
  106. .callback = dmi_matched,
  107. .ident = "Alienware X51 R2",
  108. .matches = {
  109. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  110. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
  111. },
  112. .driver_data = &quirk_x51_r1_r2,
  113. },
  114. {
  115. .callback = dmi_matched,
  116. .ident = "Alienware X51 R3",
  117. .matches = {
  118. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  119. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
  120. },
  121. .driver_data = &quirk_x51_r3,
  122. },
  123. {
  124. .callback = dmi_matched,
  125. .ident = "Dell Inc. Inspiron 5675",
  126. .matches = {
  127. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  128. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5675"),
  129. },
  130. .driver_data = &quirk_inspiron5675,
  131. },
  132. {}
  133. };
  134. u8 alienware_interface;
  135. int alienware_wmi_command(struct wmi_device *wdev, u32 method_id,
  136. void *in_args, size_t in_size, u32 *out_data)
  137. {
  138. struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
  139. struct acpi_buffer in = {in_size, in_args};
  140. acpi_status ret;
  141. ret = wmidev_evaluate_method(wdev, 0, method_id, &in, out_data ? &out : NULL);
  142. if (ACPI_FAILURE(ret))
  143. return -EIO;
  144. union acpi_object *obj __free(kfree) = out.pointer;
  145. if (out_data) {
  146. if (obj && obj->type == ACPI_TYPE_INTEGER)
  147. *out_data = (u32)obj->integer.value;
  148. else
  149. return -ENOMSG;
  150. }
  151. return 0;
  152. }
  153. /*
  154. * Helpers used for zone control
  155. */
  156. static int parse_rgb(const char *buf, struct color_platform *colors)
  157. {
  158. long unsigned int rgb;
  159. int ret;
  160. union color_union {
  161. struct color_platform cp;
  162. int package;
  163. } repackager;
  164. ret = kstrtoul(buf, 16, &rgb);
  165. if (ret)
  166. return ret;
  167. /* RGB triplet notation is 24-bit hexadecimal */
  168. if (rgb > 0xFFFFFF)
  169. return -EINVAL;
  170. repackager.package = rgb & 0x0f0f0f0f;
  171. pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
  172. repackager.cp.red, repackager.cp.green, repackager.cp.blue);
  173. *colors = repackager.cp;
  174. return 0;
  175. }
  176. /*
  177. * Individual RGB zone control
  178. */
  179. static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
  180. char *buf, u8 location)
  181. {
  182. struct alienfx_priv *priv = dev_get_drvdata(dev);
  183. struct color_platform *colors = &priv->colors[location];
  184. return sprintf(buf, "red: %d, green: %d, blue: %d\n",
  185. colors->red, colors->green, colors->blue);
  186. }
  187. static ssize_t zone_store(struct device *dev, struct device_attribute *attr,
  188. const char *buf, size_t count, u8 location)
  189. {
  190. struct alienfx_priv *priv = dev_get_drvdata(dev);
  191. struct color_platform *colors = &priv->colors[location];
  192. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  193. int ret;
  194. ret = parse_rgb(buf, colors);
  195. if (ret)
  196. return ret;
  197. ret = pdata->ops.upd_led(priv, pdata->wdev, location);
  198. return ret ? ret : count;
  199. }
  200. static ssize_t zone00_show(struct device *dev, struct device_attribute *attr,
  201. char *buf)
  202. {
  203. return zone_show(dev, attr, buf, 0);
  204. }
  205. static ssize_t zone00_store(struct device *dev, struct device_attribute *attr,
  206. const char *buf, size_t count)
  207. {
  208. return zone_store(dev, attr, buf, count, 0);
  209. }
  210. static DEVICE_ATTR_RW(zone00);
  211. static ssize_t zone01_show(struct device *dev, struct device_attribute *attr,
  212. char *buf)
  213. {
  214. return zone_show(dev, attr, buf, 1);
  215. }
  216. static ssize_t zone01_store(struct device *dev, struct device_attribute *attr,
  217. const char *buf, size_t count)
  218. {
  219. return zone_store(dev, attr, buf, count, 1);
  220. }
  221. static DEVICE_ATTR_RW(zone01);
  222. static ssize_t zone02_show(struct device *dev, struct device_attribute *attr,
  223. char *buf)
  224. {
  225. return zone_show(dev, attr, buf, 2);
  226. }
  227. static ssize_t zone02_store(struct device *dev, struct device_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. return zone_store(dev, attr, buf, count, 2);
  231. }
  232. static DEVICE_ATTR_RW(zone02);
  233. static ssize_t zone03_show(struct device *dev, struct device_attribute *attr,
  234. char *buf)
  235. {
  236. return zone_show(dev, attr, buf, 3);
  237. }
  238. static ssize_t zone03_store(struct device *dev, struct device_attribute *attr,
  239. const char *buf, size_t count)
  240. {
  241. return zone_store(dev, attr, buf, count, 3);
  242. }
  243. static DEVICE_ATTR_RW(zone03);
  244. /*
  245. * Lighting control state device attribute (Global)
  246. */
  247. static ssize_t lighting_control_state_show(struct device *dev,
  248. struct device_attribute *attr,
  249. char *buf)
  250. {
  251. struct alienfx_priv *priv = dev_get_drvdata(dev);
  252. if (priv->lighting_control_state == LEGACY_BOOTING)
  253. return sysfs_emit(buf, "[booting] running suspend\n");
  254. else if (priv->lighting_control_state == LEGACY_SUSPEND)
  255. return sysfs_emit(buf, "booting running [suspend]\n");
  256. return sysfs_emit(buf, "booting [running] suspend\n");
  257. }
  258. static ssize_t lighting_control_state_store(struct device *dev,
  259. struct device_attribute *attr,
  260. const char *buf, size_t count)
  261. {
  262. struct alienfx_priv *priv = dev_get_drvdata(dev);
  263. u8 val;
  264. if (strcmp(buf, "booting\n") == 0)
  265. val = LEGACY_BOOTING;
  266. else if (strcmp(buf, "suspend\n") == 0)
  267. val = LEGACY_SUSPEND;
  268. else if (alienware_interface == LEGACY)
  269. val = LEGACY_RUNNING;
  270. else
  271. val = WMAX_RUNNING;
  272. priv->lighting_control_state = val;
  273. pr_debug("alienware-wmi: updated control state to %d\n",
  274. priv->lighting_control_state);
  275. return count;
  276. }
  277. static DEVICE_ATTR_RW(lighting_control_state);
  278. static umode_t zone_attr_visible(struct kobject *kobj,
  279. struct attribute *attr, int n)
  280. {
  281. if (n < alienfx->num_zones + 1)
  282. return attr->mode;
  283. return 0;
  284. }
  285. static bool zone_group_visible(struct kobject *kobj)
  286. {
  287. return alienfx->num_zones > 0;
  288. }
  289. DEFINE_SYSFS_GROUP_VISIBLE(zone);
  290. static struct attribute *zone_attrs[] = {
  291. &dev_attr_lighting_control_state.attr,
  292. &dev_attr_zone00.attr,
  293. &dev_attr_zone01.attr,
  294. &dev_attr_zone02.attr,
  295. &dev_attr_zone03.attr,
  296. NULL
  297. };
  298. static struct attribute_group zone_attribute_group = {
  299. .name = "rgb_zones",
  300. .is_visible = SYSFS_GROUP_VISIBLE(zone),
  301. .attrs = zone_attrs,
  302. };
  303. /*
  304. * LED Brightness (Global)
  305. */
  306. static void global_led_set(struct led_classdev *led_cdev,
  307. enum led_brightness brightness)
  308. {
  309. struct alienfx_priv *priv = container_of(led_cdev, struct alienfx_priv,
  310. global_led);
  311. struct alienfx_platdata *pdata = dev_get_platdata(&priv->pdev->dev);
  312. int ret;
  313. priv->global_brightness = brightness;
  314. ret = pdata->ops.upd_brightness(priv, pdata->wdev, brightness);
  315. if (ret)
  316. pr_err("LED brightness update failed\n");
  317. }
  318. static enum led_brightness global_led_get(struct led_classdev *led_cdev)
  319. {
  320. struct alienfx_priv *priv = container_of(led_cdev, struct alienfx_priv,
  321. global_led);
  322. return priv->global_brightness;
  323. }
  324. /*
  325. * Platform Driver
  326. */
  327. static int alienfx_probe(struct platform_device *pdev)
  328. {
  329. struct alienfx_priv *priv;
  330. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  331. if (!priv)
  332. return -ENOMEM;
  333. if (alienware_interface == WMAX)
  334. priv->lighting_control_state = WMAX_RUNNING;
  335. else
  336. priv->lighting_control_state = LEGACY_RUNNING;
  337. priv->pdev = pdev;
  338. priv->global_led.name = "alienware::global_brightness";
  339. priv->global_led.brightness_set = global_led_set;
  340. priv->global_led.brightness_get = global_led_get;
  341. priv->global_led.max_brightness = 0x0F;
  342. priv->global_brightness = priv->global_led.max_brightness;
  343. platform_set_drvdata(pdev, priv);
  344. return devm_led_classdev_register(&pdev->dev, &priv->global_led);
  345. }
  346. static const struct attribute_group *alienfx_groups[] = {
  347. &zone_attribute_group,
  348. WMAX_DEV_GROUPS
  349. NULL
  350. };
  351. static struct platform_driver platform_driver = {
  352. .driver = {
  353. .name = "alienware-wmi",
  354. .dev_groups = alienfx_groups,
  355. },
  356. .probe = alienfx_probe,
  357. };
  358. static void alienware_alienfx_remove(void *data)
  359. {
  360. struct platform_device *pdev = data;
  361. platform_device_unregister(pdev);
  362. }
  363. int alienware_alienfx_setup(struct alienfx_platdata *pdata)
  364. {
  365. struct device *dev = &pdata->wdev->dev;
  366. struct platform_device *pdev;
  367. int ret;
  368. pdev = platform_device_register_data(NULL, "alienware-wmi",
  369. PLATFORM_DEVID_NONE, pdata,
  370. sizeof(*pdata));
  371. if (IS_ERR(pdev))
  372. return PTR_ERR(pdev);
  373. dev_set_drvdata(dev, pdev);
  374. ret = devm_add_action_or_reset(dev, alienware_alienfx_remove, pdev);
  375. if (ret)
  376. return ret;
  377. return 0;
  378. }
  379. static int __init alienware_wmi_init(void)
  380. {
  381. int ret;
  382. dmi_check_system(alienware_quirks);
  383. if (!alienfx)
  384. alienfx = &quirk_unknown;
  385. ret = platform_driver_register(&platform_driver);
  386. if (ret < 0)
  387. return ret;
  388. if (wmi_has_guid(WMAX_CONTROL_GUID)) {
  389. alienware_interface = WMAX;
  390. ret = alienware_wmax_wmi_init();
  391. } else {
  392. alienware_interface = LEGACY;
  393. ret = alienware_legacy_wmi_init();
  394. }
  395. if (ret < 0)
  396. platform_driver_unregister(&platform_driver);
  397. return ret;
  398. }
  399. module_init(alienware_wmi_init);
  400. static void __exit alienware_wmi_exit(void)
  401. {
  402. if (alienware_interface == WMAX)
  403. alienware_wmax_wmi_exit();
  404. else
  405. alienware_legacy_wmi_exit();
  406. platform_driver_unregister(&platform_driver);
  407. }
  408. module_exit(alienware_wmi_exit);