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