leds-apu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. {
  77. .ident = "apu",
  78. .matches = {
  79. DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
  80. DMI_MATCH(DMI_PRODUCT_NAME, "APU")
  81. }
  82. },
  83. {}
  84. };
  85. MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
  86. static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
  87. {
  88. struct apu_led_priv *pled = cdev_to_priv(led);
  89. spin_lock(&apu_led->lock);
  90. iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
  91. spin_unlock(&apu_led->lock);
  92. }
  93. static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
  94. {
  95. int i;
  96. int err;
  97. apu_led->pled = devm_kcalloc(dev,
  98. ARRAY_SIZE(apu1_led_profile), sizeof(struct apu_led_priv),
  99. GFP_KERNEL);
  100. if (!apu_led->pled)
  101. return -ENOMEM;
  102. for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) {
  103. struct apu_led_priv *pled = &apu_led->pled[i];
  104. struct led_classdev *led_cdev = &pled->cdev;
  105. led_cdev->name = apu1_led_profile[i].name;
  106. led_cdev->brightness = apu1_led_profile[i].brightness;
  107. led_cdev->max_brightness = 1;
  108. led_cdev->flags = LED_CORE_SUSPENDRESUME;
  109. led_cdev->brightness_set = apu1_led_brightness_set;
  110. pled->param.addr = devm_ioremap(dev,
  111. apu1_led_profile[i].offset, APU1_IOSIZE);
  112. if (!pled->param.addr) {
  113. err = -ENOMEM;
  114. goto error;
  115. }
  116. err = led_classdev_register(dev, led_cdev);
  117. if (err)
  118. goto error;
  119. apu1_led_brightness_set(led_cdev, apu1_led_profile[i].brightness);
  120. }
  121. return 0;
  122. error:
  123. while (i-- > 0)
  124. led_classdev_unregister(&apu_led->pled[i].cdev);
  125. return err;
  126. }
  127. static int __init apu_led_probe(struct platform_device *pdev)
  128. {
  129. apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
  130. if (!apu_led)
  131. return -ENOMEM;
  132. apu_led->pdev = pdev;
  133. spin_lock_init(&apu_led->lock);
  134. return apu_led_config(&pdev->dev, apu_led);
  135. }
  136. static struct platform_driver apu_led_driver = {
  137. .driver = {
  138. .name = KBUILD_MODNAME,
  139. },
  140. };
  141. static int __init apu_led_init(void)
  142. {
  143. struct platform_device *pdev;
  144. int err;
  145. if (!(dmi_match(DMI_SYS_VENDOR, "PC Engines") &&
  146. dmi_match(DMI_PRODUCT_NAME, "APU"))) {
  147. pr_err("No PC Engines APUv1 board detected. For APUv2,3 support, enable CONFIG_PCENGINES_APU2\n");
  148. return -ENODEV;
  149. }
  150. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  151. if (IS_ERR(pdev)) {
  152. pr_err("Device allocation failed\n");
  153. return PTR_ERR(pdev);
  154. }
  155. err = platform_driver_probe(&apu_led_driver, apu_led_probe);
  156. if (err) {
  157. pr_err("Probe platform driver failed\n");
  158. platform_device_unregister(pdev);
  159. }
  160. return err;
  161. }
  162. static void __exit apu_led_exit(void)
  163. {
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++)
  166. led_classdev_unregister(&apu_led->pled[i].cdev);
  167. platform_device_unregister(apu_led->pdev);
  168. platform_driver_unregister(&apu_led_driver);
  169. }
  170. module_init(apu_led_init);
  171. module_exit(apu_led_exit);
  172. MODULE_AUTHOR("Alan Mizrahi");
  173. MODULE_DESCRIPTION("PC Engines APU1 front LED driver");
  174. MODULE_LICENSE("GPL v2");
  175. MODULE_ALIAS("platform:leds_apu");