leds-apu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static const struct dmi_system_id apu_led_dmi_table[] __initconst = {
  94. {
  95. .ident = "apu",
  96. .matches = {
  97. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  98. DMI_MATCH(DMI_PRODUCT_NAME, "APU")
  99. }
  100. },
  101. /* PC Engines APU2 with "Legacy" bios < 4.0.8 */
  102. {
  103. .ident = "apu2",
  104. .matches = {
  105. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  106. DMI_MATCH(DMI_BOARD_NAME, "APU2")
  107. }
  108. },
  109. /* PC Engines APU2 with "Legacy" bios >= 4.0.8 */
  110. {
  111. .ident = "apu2",
  112. .matches = {
  113. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  114. DMI_MATCH(DMI_BOARD_NAME, "apu2")
  115. }
  116. },
  117. /* PC Engines APU2 with "Mainline" bios */
  118. {
  119. .ident = "apu2",
  120. .matches = {
  121. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  122. DMI_MATCH(DMI_BOARD_NAME, "PC Engines apu2")
  123. }
  124. },
  125. {}
  126. };
  127. MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
  128. static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  129. {
  130. struct apu_led_priv *pled = cdev_to_priv(led);
  131. spin_lock(&apu_led->lock);
  132. iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
  133. spin_unlock(&apu_led->lock);
  134. }
  135. static void apu2_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  136. {
  137. struct apu_led_priv *pled = cdev_to_priv(led);
  138. u32 value_new;
  139. spin_lock(&apu_led->lock);
  140. value_new = ioread32(pled->param.addr);
  141. if (value)
  142. value_new &= ~BIT(APU2_GPIO_BIT_WRITE);
  143. else
  144. value_new |= BIT(APU2_GPIO_BIT_WRITE);
  145. iowrite32(value_new, pled->param.addr);
  146. spin_unlock(&apu_led->lock);
  147. }
  148. static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
  149. {
  150. int i;
  151. int err;
  152. apu_led->pled = devm_kcalloc(dev,
  153. apu_led->num_led_instances, sizeof(struct apu_led_priv),
  154. GFP_KERNEL);
  155. if (!apu_led->pled)
  156. return -ENOMEM;
  157. for (i = 0; i < apu_led->num_led_instances; i++) {
  158. struct apu_led_priv *pled = &apu_led->pled[i];
  159. struct led_classdev *led_cdev = &pled->cdev;
  160. led_cdev->name = apu_led->profile[i].name;
  161. led_cdev->brightness = apu_led->profile[i].brightness;
  162. led_cdev->max_brightness = 1;
  163. led_cdev->flags = LED_CORE_SUSPENDRESUME;
  164. if (apu_led->platform == APU1_LED_PLATFORM)
  165. led_cdev->brightness_set = apu1_led_brightness_set;
  166. else if (apu_led->platform == APU2_LED_PLATFORM)
  167. led_cdev->brightness_set = apu2_led_brightness_set;
  168. pled->param.addr = devm_ioremap(dev,
  169. apu_led->profile[i].offset, apu_led->iosize);
  170. if (!pled->param.addr) {
  171. err = -ENOMEM;
  172. goto error;
  173. }
  174. err = led_classdev_register(dev, led_cdev);
  175. if (err)
  176. goto error;
  177. led_cdev->brightness_set(led_cdev, apu_led->profile[i].brightness);
  178. }
  179. return 0;
  180. error:
  181. while (i-- > 0)
  182. led_classdev_unregister(&apu_led->pled[i].cdev);
  183. return err;
  184. }
  185. static int __init apu_led_probe(struct platform_device *pdev)
  186. {
  187. apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
  188. if (!apu_led)
  189. return -ENOMEM;
  190. apu_led->pdev = pdev;
  191. if (dmi_match(DMI_PRODUCT_NAME, "APU")) {
  192. apu_led->profile = apu1_led_profile;
  193. apu_led->platform = APU1_LED_PLATFORM;
  194. apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile);
  195. apu_led->iosize = APU1_IOSIZE;
  196. } else if (dmi_match(DMI_BOARD_NAME, "APU2") ||
  197. dmi_match(DMI_BOARD_NAME, "apu2") ||
  198. dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) {
  199. apu_led->profile = apu2_led_profile;
  200. apu_led->platform = APU2_LED_PLATFORM;
  201. apu_led->num_led_instances = ARRAY_SIZE(apu2_led_profile);
  202. apu_led->iosize = APU2_IOSIZE;
  203. }
  204. spin_lock_init(&apu_led->lock);
  205. return apu_led_config(&pdev->dev, apu_led);
  206. }
  207. static struct platform_driver apu_led_driver = {
  208. .driver = {
  209. .name = KBUILD_MODNAME,
  210. },
  211. };
  212. static int __init apu_led_init(void)
  213. {
  214. struct platform_device *pdev;
  215. int err;
  216. if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) {
  217. pr_err("No PC Engines board detected\n");
  218. return -ENODEV;
  219. }
  220. if (!(dmi_match(DMI_PRODUCT_NAME, "APU") ||
  221. dmi_match(DMI_PRODUCT_NAME, "APU2") ||
  222. dmi_match(DMI_PRODUCT_NAME, "apu2") ||
  223. dmi_match(DMI_PRODUCT_NAME, "PC Engines apu2"))) {
  224. pr_err("Unknown PC Engines board: %s\n",
  225. dmi_get_system_info(DMI_PRODUCT_NAME));
  226. return -ENODEV;
  227. }
  228. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  229. if (IS_ERR(pdev)) {
  230. pr_err("Device allocation failed\n");
  231. return PTR_ERR(pdev);
  232. }
  233. err = platform_driver_probe(&apu_led_driver, apu_led_probe);
  234. if (err) {
  235. pr_err("Probe platform driver failed\n");
  236. platform_device_unregister(pdev);
  237. }
  238. return err;
  239. }
  240. static void __exit apu_led_exit(void)
  241. {
  242. int i;
  243. for (i = 0; i < apu_led->num_led_instances; i++)
  244. led_classdev_unregister(&apu_led->pled[i].cdev);
  245. platform_device_unregister(apu_led->pdev);
  246. platform_driver_unregister(&apu_led_driver);
  247. }
  248. module_init(apu_led_init);
  249. module_exit(apu_led_exit);
  250. MODULE_AUTHOR("Alan Mizrahi");
  251. MODULE_DESCRIPTION("PC Engines APU family LED driver");
  252. MODULE_LICENSE("GPL v2");
  253. MODULE_ALIAS("platform:leds_apu");