leds-apu.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * drivers/leds/leds-apu.c
  3. * Copyright (C) 2017 Alan Mizrahi, alan at mizrahi dot com dot ve
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the names of the copyright holders nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * Alternatively, this software may be distributed under the terms of the
  18. * GNU General Public License ("GPL") version 2 as published by the Free
  19. * Software Foundation.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <linux/dmi.h>
  34. #include <linux/err.h>
  35. #include <linux/init.h>
  36. #include <linux/io.h>
  37. #include <linux/kernel.h>
  38. #include <linux/leds.h>
  39. #include <linux/module.h>
  40. #include <linux/platform_device.h>
  41. #define APU1_FCH_ACPI_MMIO_BASE 0xFED80000
  42. #define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x01BD)
  43. #define APU1_LEDON 0x08
  44. #define APU1_LEDOFF 0xC8
  45. #define APU1_NUM_GPIO 3
  46. #define APU1_IOSIZE sizeof(u8)
  47. #define APU2_FCH_ACPI_MMIO_BASE 0xFED80000
  48. #define APU2_FCH_GPIO_BASE (APU2_FCH_ACPI_MMIO_BASE + 0x1500)
  49. #define APU2_GPIO_BIT_WRITE 22
  50. #define APU2_APU2_NUM_GPIO 4
  51. #define APU2_IOSIZE sizeof(u32)
  52. /* LED access parameters */
  53. struct apu_param {
  54. void __iomem *addr; /* for ioread/iowrite */
  55. };
  56. /* LED private data */
  57. struct apu_led_priv {
  58. struct led_classdev cdev;
  59. struct apu_param param;
  60. };
  61. #define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev)
  62. /* LED profile */
  63. struct apu_led_profile {
  64. const char *name;
  65. enum led_brightness brightness;
  66. unsigned long offset; /* for devm_ioremap */
  67. };
  68. /* Supported platform types */
  69. enum apu_led_platform_types {
  70. APU1_LED_PLATFORM,
  71. APU2_LED_PLATFORM,
  72. };
  73. struct apu_led_pdata {
  74. struct platform_device *pdev;
  75. struct apu_led_priv *pled;
  76. const struct apu_led_profile *profile;
  77. enum apu_led_platform_types platform;
  78. int num_led_instances;
  79. int iosize; /* for devm_ioremap() */
  80. spinlock_t lock;
  81. };
  82. static struct apu_led_pdata *apu_led;
  83. static const struct apu_led_profile apu1_led_profile[] = {
  84. { "apu:green:1", LED_ON, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE },
  85. { "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE },
  86. { "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE },
  87. };
  88. static const struct apu_led_profile apu2_led_profile[] = {
  89. { "apu2:green:1", LED_ON, APU2_FCH_GPIO_BASE + 68 * APU2_IOSIZE },
  90. { "apu2:green:2", LED_OFF, APU2_FCH_GPIO_BASE + 69 * APU2_IOSIZE },
  91. { "apu2:green:3", LED_OFF, APU2_FCH_GPIO_BASE + 70 * APU2_IOSIZE },
  92. };
  93. /* Same as apu2_led_profile, but with "3" in the LED names. */
  94. static const struct apu_led_profile apu3_led_profile[] = {
  95. { "apu3:green:1", LED_ON, APU2_FCH_GPIO_BASE + 68 * APU2_IOSIZE },
  96. { "apu3:green:2", LED_OFF, APU2_FCH_GPIO_BASE + 69 * APU2_IOSIZE },
  97. { "apu3:green:3", LED_OFF, APU2_FCH_GPIO_BASE + 70 * APU2_IOSIZE },
  98. };
  99. static const struct dmi_system_id apu_led_dmi_table[] __initconst = {
  100. {
  101. .ident = "apu",
  102. .matches = {
  103. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  104. DMI_MATCH(DMI_PRODUCT_NAME, "APU")
  105. }
  106. },
  107. /* PC Engines APU2 with "Legacy" bios < 4.0.8 */
  108. {
  109. .ident = "apu2",
  110. .matches = {
  111. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  112. DMI_MATCH(DMI_BOARD_NAME, "APU2")
  113. }
  114. },
  115. /* PC Engines APU2 with "Legacy" bios >= 4.0.8 */
  116. {
  117. .ident = "apu2",
  118. .matches = {
  119. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  120. DMI_MATCH(DMI_BOARD_NAME, "apu2")
  121. }
  122. },
  123. /* PC Engines APU2 with "Mainline" bios */
  124. {
  125. .ident = "apu2",
  126. .matches = {
  127. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  128. DMI_MATCH(DMI_BOARD_NAME, "PC Engines apu2")
  129. }
  130. },
  131. /* PC Engines APU3 with "Legacy" bios < 4.0.8 */
  132. {
  133. .ident = "apu3",
  134. .matches = {
  135. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  136. DMI_MATCH(DMI_BOARD_NAME, "APU3")
  137. }
  138. },
  139. /* PC Engines APU3 with "Legacy" bios >= 4.0.8 */
  140. {
  141. .ident = "apu3",
  142. .matches = {
  143. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  144. DMI_MATCH(DMI_BOARD_NAME, "apu3")
  145. }
  146. },
  147. /* PC Engines APU2 with "Mainline" bios */
  148. {
  149. .ident = "apu3",
  150. .matches = {
  151. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  152. DMI_MATCH(DMI_BOARD_NAME, "PC Engines apu3")
  153. }
  154. },
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
  158. static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  159. {
  160. struct apu_led_priv *pled = cdev_to_priv(led);
  161. spin_lock(&apu_led->lock);
  162. iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
  163. spin_unlock(&apu_led->lock);
  164. }
  165. static void apu2_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  166. {
  167. struct apu_led_priv *pled = cdev_to_priv(led);
  168. u32 value_new;
  169. spin_lock(&apu_led->lock);
  170. value_new = ioread32(pled->param.addr);
  171. if (value)
  172. value_new &= ~BIT(APU2_GPIO_BIT_WRITE);
  173. else
  174. value_new |= BIT(APU2_GPIO_BIT_WRITE);
  175. iowrite32(value_new, pled->param.addr);
  176. spin_unlock(&apu_led->lock);
  177. }
  178. static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
  179. {
  180. int i;
  181. int err;
  182. apu_led->pled = devm_kcalloc(dev,
  183. apu_led->num_led_instances, sizeof(struct apu_led_priv),
  184. GFP_KERNEL);
  185. if (!apu_led->pled)
  186. return -ENOMEM;
  187. for (i = 0; i < apu_led->num_led_instances; i++) {
  188. struct apu_led_priv *pled = &apu_led->pled[i];
  189. struct led_classdev *led_cdev = &pled->cdev;
  190. led_cdev->name = apu_led->profile[i].name;
  191. led_cdev->brightness = apu_led->profile[i].brightness;
  192. led_cdev->max_brightness = 1;
  193. led_cdev->flags = LED_CORE_SUSPENDRESUME;
  194. if (apu_led->platform == APU1_LED_PLATFORM)
  195. led_cdev->brightness_set = apu1_led_brightness_set;
  196. else if (apu_led->platform == APU2_LED_PLATFORM)
  197. led_cdev->brightness_set = apu2_led_brightness_set;
  198. pled->param.addr = devm_ioremap(dev,
  199. apu_led->profile[i].offset, apu_led->iosize);
  200. if (!pled->param.addr) {
  201. err = -ENOMEM;
  202. goto error;
  203. }
  204. err = led_classdev_register(dev, led_cdev);
  205. if (err)
  206. goto error;
  207. led_cdev->brightness_set(led_cdev, apu_led->profile[i].brightness);
  208. }
  209. return 0;
  210. error:
  211. while (i-- > 0)
  212. led_classdev_unregister(&apu_led->pled[i].cdev);
  213. return err;
  214. }
  215. static int __init apu_led_probe(struct platform_device *pdev)
  216. {
  217. apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
  218. if (!apu_led)
  219. return -ENOMEM;
  220. apu_led->pdev = pdev;
  221. if (dmi_match(DMI_PRODUCT_NAME, "APU")) {
  222. apu_led->profile = apu1_led_profile;
  223. apu_led->platform = APU1_LED_PLATFORM;
  224. apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile);
  225. apu_led->iosize = APU1_IOSIZE;
  226. } else if (dmi_match(DMI_BOARD_NAME, "APU2") ||
  227. dmi_match(DMI_BOARD_NAME, "apu2") ||
  228. dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) {
  229. apu_led->profile = apu2_led_profile;
  230. apu_led->platform = APU2_LED_PLATFORM;
  231. apu_led->num_led_instances = ARRAY_SIZE(apu2_led_profile);
  232. apu_led->iosize = APU2_IOSIZE;
  233. } else if (dmi_match(DMI_BOARD_NAME, "APU3") ||
  234. dmi_match(DMI_BOARD_NAME, "apu3") ||
  235. dmi_match(DMI_BOARD_NAME, "PC Engines apu3")) {
  236. apu_led->profile = apu3_led_profile;
  237. /* Otherwise identical to APU2. */
  238. apu_led->platform = APU2_LED_PLATFORM;
  239. apu_led->num_led_instances = ARRAY_SIZE(apu3_led_profile);
  240. apu_led->iosize = APU2_IOSIZE;
  241. }
  242. spin_lock_init(&apu_led->lock);
  243. return apu_led_config(&pdev->dev, apu_led);
  244. }
  245. static struct platform_driver apu_led_driver = {
  246. .driver = {
  247. .name = KBUILD_MODNAME,
  248. },
  249. };
  250. static int __init apu_led_init(void)
  251. {
  252. struct platform_device *pdev;
  253. int err;
  254. if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) {
  255. pr_err("No PC Engines board detected\n");
  256. return -ENODEV;
  257. }
  258. if (!(dmi_match(DMI_PRODUCT_NAME, "APU") ||
  259. dmi_match(DMI_PRODUCT_NAME, "APU2") ||
  260. dmi_match(DMI_PRODUCT_NAME, "apu2") ||
  261. dmi_match(DMI_PRODUCT_NAME, "PC Engines apu2") ||
  262. dmi_match(DMI_PRODUCT_NAME, "APU3") ||
  263. dmi_match(DMI_PRODUCT_NAME, "apu3") ||
  264. dmi_match(DMI_PRODUCT_NAME, "PC Engines apu3"))) {
  265. pr_err("Unknown PC Engines board: %s\n",
  266. dmi_get_system_info(DMI_PRODUCT_NAME));
  267. return -ENODEV;
  268. }
  269. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  270. if (IS_ERR(pdev)) {
  271. pr_err("Device allocation failed\n");
  272. return PTR_ERR(pdev);
  273. }
  274. err = platform_driver_probe(&apu_led_driver, apu_led_probe);
  275. if (err) {
  276. pr_err("Probe platform driver failed\n");
  277. platform_device_unregister(pdev);
  278. }
  279. return err;
  280. }
  281. static void __exit apu_led_exit(void)
  282. {
  283. int i;
  284. for (i = 0; i < apu_led->num_led_instances; i++)
  285. led_classdev_unregister(&apu_led->pled[i].cdev);
  286. platform_device_unregister(apu_led->pdev);
  287. platform_driver_unregister(&apu_led_driver);
  288. }
  289. module_init(apu_led_init);
  290. module_exit(apu_led_exit);
  291. MODULE_AUTHOR("Alan Mizrahi");
  292. MODULE_DESCRIPTION("PC Engines APU family LED driver");
  293. MODULE_LICENSE("GPL v2");
  294. MODULE_ALIAS("platform:leds_apu");