leds-apu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/dmi.h>
  35. #include <linux/err.h>
  36. #include <linux/init.h>
  37. #include <linux/io.h>
  38. #include <linux/kernel.h>
  39. #include <linux/leds.h>
  40. #include <linux/module.h>
  41. #include <linux/platform_device.h>
  42. #define APU1_FCH_ACPI_MMIO_BASE 0xFED80000
  43. #define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x01BD)
  44. #define APU1_LEDON 0x08
  45. #define APU1_LEDOFF 0xC8
  46. #define APU1_NUM_GPIO 3
  47. #define APU1_IOSIZE sizeof(u8)
  48. /* LED access parameters */
  49. struct apu_param {
  50. void __iomem *addr; /* for ioread/iowrite */
  51. };
  52. /* LED private data */
  53. struct apu_led_priv {
  54. struct led_classdev cdev;
  55. struct apu_param param;
  56. };
  57. #define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev)
  58. /* LED profile */
  59. struct apu_led_profile {
  60. const char *name;
  61. enum led_brightness brightness;
  62. unsigned long offset; /* for devm_ioremap */
  63. };
  64. struct apu_led_pdata {
  65. struct platform_device *pdev;
  66. struct apu_led_priv *pled;
  67. spinlock_t lock;
  68. };
  69. static struct apu_led_pdata *apu_led;
  70. static const struct apu_led_profile apu1_led_profile[] = {
  71. { "apu:green:1", LED_ON, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE },
  72. { "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE },
  73. { "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE },
  74. };
  75. static const struct dmi_system_id apu_led_dmi_table[] __initconst = {
  76. /* PC Engines APU with factory bios "SageBios_PCEngines_APU-45" */
  77. {
  78. .ident = "apu",
  79. .matches = {
  80. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  81. DMI_MATCH(DMI_PRODUCT_NAME, "APU")
  82. }
  83. },
  84. /* PC Engines APU with "Mainline" bios >= 4.6.8 */
  85. {
  86. .ident = "apu",
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  89. DMI_MATCH(DMI_PRODUCT_NAME, "apu1")
  90. }
  91. },
  92. {}
  93. };
  94. MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
  95. static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  96. {
  97. struct apu_led_priv *pled = cdev_to_priv(led);
  98. spin_lock(&apu_led->lock);
  99. iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
  100. spin_unlock(&apu_led->lock);
  101. }
  102. static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
  103. {
  104. int i;
  105. int err;
  106. apu_led->pled = devm_kcalloc(dev,
  107. ARRAY_SIZE(apu1_led_profile), sizeof(struct apu_led_priv),
  108. GFP_KERNEL);
  109. if (!apu_led->pled)
  110. return -ENOMEM;
  111. for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) {
  112. struct apu_led_priv *pled = &apu_led->pled[i];
  113. struct led_classdev *led_cdev = &pled->cdev;
  114. led_cdev->name = apu1_led_profile[i].name;
  115. led_cdev->brightness = apu1_led_profile[i].brightness;
  116. led_cdev->max_brightness = 1;
  117. led_cdev->flags = LED_CORE_SUSPENDRESUME;
  118. led_cdev->brightness_set = apu1_led_brightness_set;
  119. pled->param.addr = devm_ioremap(dev,
  120. apu1_led_profile[i].offset, APU1_IOSIZE);
  121. if (!pled->param.addr) {
  122. err = -ENOMEM;
  123. goto error;
  124. }
  125. err = led_classdev_register(dev, led_cdev);
  126. if (err)
  127. goto error;
  128. apu1_led_brightness_set(led_cdev, apu1_led_profile[i].brightness);
  129. }
  130. return 0;
  131. error:
  132. while (i-- > 0)
  133. led_classdev_unregister(&apu_led->pled[i].cdev);
  134. return err;
  135. }
  136. static int __init apu_led_probe(struct platform_device *pdev)
  137. {
  138. apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
  139. if (!apu_led)
  140. return -ENOMEM;
  141. apu_led->pdev = pdev;
  142. spin_lock_init(&apu_led->lock);
  143. return apu_led_config(&pdev->dev, apu_led);
  144. }
  145. static struct platform_driver apu_led_driver = {
  146. .driver = {
  147. .name = KBUILD_MODNAME,
  148. },
  149. };
  150. static int __init apu_led_init(void)
  151. {
  152. struct platform_device *pdev;
  153. int err;
  154. if (!(dmi_match(DMI_SYS_VENDOR, "PC Engines") &&
  155. (dmi_match(DMI_PRODUCT_NAME, "APU") || dmi_match(DMI_PRODUCT_NAME, "apu1")))) {
  156. pr_err("No PC Engines APUv1 board detected. For APUv2,3 support, enable CONFIG_PCENGINES_APU2\n");
  157. return -ENODEV;
  158. }
  159. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  160. if (IS_ERR(pdev)) {
  161. pr_err("Device allocation failed\n");
  162. return PTR_ERR(pdev);
  163. }
  164. err = platform_driver_probe(&apu_led_driver, apu_led_probe);
  165. if (err) {
  166. pr_err("Probe platform driver failed\n");
  167. platform_device_unregister(pdev);
  168. }
  169. return err;
  170. }
  171. static void __exit apu_led_exit(void)
  172. {
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++)
  175. led_classdev_unregister(&apu_led->pled[i].cdev);
  176. platform_device_unregister(apu_led->pdev);
  177. platform_driver_unregister(&apu_led_driver);
  178. }
  179. module_init(apu_led_init);
  180. module_exit(apu_led_exit);
  181. MODULE_AUTHOR("Alan Mizrahi");
  182. MODULE_DESCRIPTION("PC Engines APU1 front LED driver");
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_ALIAS("platform:leds_apu");