13faee942SAlan Mizrahi /* 23faee942SAlan Mizrahi * drivers/leds/leds-apu.c 33faee942SAlan Mizrahi * Copyright (C) 2017 Alan Mizrahi, alan at mizrahi dot com dot ve 43faee942SAlan Mizrahi * 53faee942SAlan Mizrahi * Redistribution and use in source and binary forms, with or without 63faee942SAlan Mizrahi * modification, are permitted provided that the following conditions are met: 73faee942SAlan Mizrahi * 83faee942SAlan Mizrahi * 1. Redistributions of source code must retain the above copyright 93faee942SAlan Mizrahi * notice, this list of conditions and the following disclaimer. 103faee942SAlan Mizrahi * 2. Redistributions in binary form must reproduce the above copyright 113faee942SAlan Mizrahi * notice, this list of conditions and the following disclaimer in the 123faee942SAlan Mizrahi * documentation and/or other materials provided with the distribution. 133faee942SAlan Mizrahi * 3. Neither the names of the copyright holders nor the names of its 143faee942SAlan Mizrahi * contributors may be used to endorse or promote products derived from 153faee942SAlan Mizrahi * this software without specific prior written permission. 163faee942SAlan Mizrahi * 173faee942SAlan Mizrahi * Alternatively, this software may be distributed under the terms of the 183faee942SAlan Mizrahi * GNU General Public License ("GPL") version 2 as published by the Free 193faee942SAlan Mizrahi * Software Foundation. 203faee942SAlan Mizrahi * 213faee942SAlan Mizrahi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 223faee942SAlan Mizrahi * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 233faee942SAlan Mizrahi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 243faee942SAlan Mizrahi * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 253faee942SAlan Mizrahi * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 263faee942SAlan Mizrahi * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 273faee942SAlan Mizrahi * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 283faee942SAlan Mizrahi * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 293faee942SAlan Mizrahi * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 303faee942SAlan Mizrahi * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 313faee942SAlan Mizrahi * POSSIBILITY OF SUCH DAMAGE. 323faee942SAlan Mizrahi */ 333faee942SAlan Mizrahi 34*60439711SEnrico Weigelt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 35*60439711SEnrico Weigelt 363faee942SAlan Mizrahi #include <linux/dmi.h> 373faee942SAlan Mizrahi #include <linux/err.h> 383faee942SAlan Mizrahi #include <linux/init.h> 393faee942SAlan Mizrahi #include <linux/io.h> 403faee942SAlan Mizrahi #include <linux/kernel.h> 413faee942SAlan Mizrahi #include <linux/leds.h> 423faee942SAlan Mizrahi #include <linux/module.h> 433faee942SAlan Mizrahi #include <linux/platform_device.h> 443faee942SAlan Mizrahi 453faee942SAlan Mizrahi #define APU1_FCH_ACPI_MMIO_BASE 0xFED80000 463faee942SAlan Mizrahi #define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x01BD) 473faee942SAlan Mizrahi #define APU1_LEDON 0x08 483faee942SAlan Mizrahi #define APU1_LEDOFF 0xC8 493faee942SAlan Mizrahi #define APU1_NUM_GPIO 3 503faee942SAlan Mizrahi #define APU1_IOSIZE sizeof(u8) 513faee942SAlan Mizrahi 523faee942SAlan Mizrahi /* LED access parameters */ 533faee942SAlan Mizrahi struct apu_param { 543faee942SAlan Mizrahi void __iomem *addr; /* for ioread/iowrite */ 553faee942SAlan Mizrahi }; 563faee942SAlan Mizrahi 573faee942SAlan Mizrahi /* LED private data */ 583faee942SAlan Mizrahi struct apu_led_priv { 593faee942SAlan Mizrahi struct led_classdev cdev; 603faee942SAlan Mizrahi struct apu_param param; 613faee942SAlan Mizrahi }; 623faee942SAlan Mizrahi #define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev) 633faee942SAlan Mizrahi 643faee942SAlan Mizrahi /* LED profile */ 653faee942SAlan Mizrahi struct apu_led_profile { 663faee942SAlan Mizrahi const char *name; 673faee942SAlan Mizrahi enum led_brightness brightness; 683faee942SAlan Mizrahi unsigned long offset; /* for devm_ioremap */ 693faee942SAlan Mizrahi }; 703faee942SAlan Mizrahi 713faee942SAlan Mizrahi struct apu_led_pdata { 723faee942SAlan Mizrahi struct platform_device *pdev; 733faee942SAlan Mizrahi struct apu_led_priv *pled; 743faee942SAlan Mizrahi spinlock_t lock; 753faee942SAlan Mizrahi }; 763faee942SAlan Mizrahi 773faee942SAlan Mizrahi static struct apu_led_pdata *apu_led; 783faee942SAlan Mizrahi 793faee942SAlan Mizrahi static const struct apu_led_profile apu1_led_profile[] = { 803faee942SAlan Mizrahi { "apu:green:1", LED_ON, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE }, 813faee942SAlan Mizrahi { "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE }, 823faee942SAlan Mizrahi { "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE }, 833faee942SAlan Mizrahi }; 843faee942SAlan Mizrahi 853faee942SAlan Mizrahi static const struct dmi_system_id apu_led_dmi_table[] __initconst = { 863faee942SAlan Mizrahi { 873faee942SAlan Mizrahi .ident = "apu", 883faee942SAlan Mizrahi .matches = { 893faee942SAlan Mizrahi DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), 903faee942SAlan Mizrahi DMI_MATCH(DMI_PRODUCT_NAME, "APU") 913faee942SAlan Mizrahi } 923faee942SAlan Mizrahi }, 933faee942SAlan Mizrahi {} 943faee942SAlan Mizrahi }; 953faee942SAlan Mizrahi MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table); 963faee942SAlan Mizrahi 973faee942SAlan Mizrahi static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value) 983faee942SAlan Mizrahi { 993faee942SAlan Mizrahi struct apu_led_priv *pled = cdev_to_priv(led); 1003faee942SAlan Mizrahi 1013faee942SAlan Mizrahi spin_lock(&apu_led->lock); 1023faee942SAlan Mizrahi iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr); 1033faee942SAlan Mizrahi spin_unlock(&apu_led->lock); 1043faee942SAlan Mizrahi } 1053faee942SAlan Mizrahi 1063faee942SAlan Mizrahi static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld) 1073faee942SAlan Mizrahi { 1083faee942SAlan Mizrahi int i; 1093faee942SAlan Mizrahi int err; 1103faee942SAlan Mizrahi 111a86854d0SKees Cook apu_led->pled = devm_kcalloc(dev, 112cc92e3ccSEnrico Weigelt ARRAY_SIZE(apu1_led_profile), sizeof(struct apu_led_priv), 1133faee942SAlan Mizrahi GFP_KERNEL); 1143faee942SAlan Mizrahi 1153faee942SAlan Mizrahi if (!apu_led->pled) 1163faee942SAlan Mizrahi return -ENOMEM; 1173faee942SAlan Mizrahi 118cc92e3ccSEnrico Weigelt for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) { 1193faee942SAlan Mizrahi struct apu_led_priv *pled = &apu_led->pled[i]; 1203faee942SAlan Mizrahi struct led_classdev *led_cdev = &pled->cdev; 1213faee942SAlan Mizrahi 122cc92e3ccSEnrico Weigelt led_cdev->name = apu1_led_profile[i].name; 123cc92e3ccSEnrico Weigelt led_cdev->brightness = apu1_led_profile[i].brightness; 1243faee942SAlan Mizrahi led_cdev->max_brightness = 1; 1253faee942SAlan Mizrahi led_cdev->flags = LED_CORE_SUSPENDRESUME; 1263faee942SAlan Mizrahi led_cdev->brightness_set = apu1_led_brightness_set; 1273faee942SAlan Mizrahi 1283faee942SAlan Mizrahi pled->param.addr = devm_ioremap(dev, 129cc92e3ccSEnrico Weigelt apu1_led_profile[i].offset, APU1_IOSIZE); 1303faee942SAlan Mizrahi if (!pled->param.addr) { 1313faee942SAlan Mizrahi err = -ENOMEM; 1323faee942SAlan Mizrahi goto error; 1333faee942SAlan Mizrahi } 1343faee942SAlan Mizrahi 1353faee942SAlan Mizrahi err = led_classdev_register(dev, led_cdev); 1363faee942SAlan Mizrahi if (err) 1373faee942SAlan Mizrahi goto error; 1383faee942SAlan Mizrahi 139cc92e3ccSEnrico Weigelt apu1_led_brightness_set(led_cdev, apu1_led_profile[i].brightness); 1403faee942SAlan Mizrahi } 1413faee942SAlan Mizrahi 1423faee942SAlan Mizrahi return 0; 1433faee942SAlan Mizrahi 1443faee942SAlan Mizrahi error: 1453faee942SAlan Mizrahi while (i-- > 0) 1463faee942SAlan Mizrahi led_classdev_unregister(&apu_led->pled[i].cdev); 1473faee942SAlan Mizrahi 1483faee942SAlan Mizrahi return err; 1493faee942SAlan Mizrahi } 1503faee942SAlan Mizrahi 1513faee942SAlan Mizrahi static int __init apu_led_probe(struct platform_device *pdev) 1523faee942SAlan Mizrahi { 1533faee942SAlan Mizrahi apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL); 1543faee942SAlan Mizrahi 1553faee942SAlan Mizrahi if (!apu_led) 1563faee942SAlan Mizrahi return -ENOMEM; 1573faee942SAlan Mizrahi 1583faee942SAlan Mizrahi apu_led->pdev = pdev; 1593faee942SAlan Mizrahi 1603faee942SAlan Mizrahi spin_lock_init(&apu_led->lock); 1613faee942SAlan Mizrahi return apu_led_config(&pdev->dev, apu_led); 1623faee942SAlan Mizrahi } 1633faee942SAlan Mizrahi 1643faee942SAlan Mizrahi static struct platform_driver apu_led_driver = { 1653faee942SAlan Mizrahi .driver = { 1663faee942SAlan Mizrahi .name = KBUILD_MODNAME, 1673faee942SAlan Mizrahi }, 1683faee942SAlan Mizrahi }; 1693faee942SAlan Mizrahi 1703faee942SAlan Mizrahi static int __init apu_led_init(void) 1713faee942SAlan Mizrahi { 1723faee942SAlan Mizrahi struct platform_device *pdev; 1733faee942SAlan Mizrahi int err; 1743faee942SAlan Mizrahi 17593562049SEnrico Weigelt if (!(dmi_match(DMI_SYS_VENDOR, "PC Engines") && 17693562049SEnrico Weigelt dmi_match(DMI_PRODUCT_NAME, "APU"))) { 17793562049SEnrico Weigelt pr_err("No PC Engines APUv1 board detected. For APUv2,3 support, enable CONFIG_PCENGINES_APU2\n"); 1783faee942SAlan Mizrahi return -ENODEV; 1793faee942SAlan Mizrahi } 1803faee942SAlan Mizrahi 1813faee942SAlan Mizrahi pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0); 1823faee942SAlan Mizrahi if (IS_ERR(pdev)) { 1833faee942SAlan Mizrahi pr_err("Device allocation failed\n"); 1843faee942SAlan Mizrahi return PTR_ERR(pdev); 1853faee942SAlan Mizrahi } 1863faee942SAlan Mizrahi 1873faee942SAlan Mizrahi err = platform_driver_probe(&apu_led_driver, apu_led_probe); 1883faee942SAlan Mizrahi if (err) { 1893faee942SAlan Mizrahi pr_err("Probe platform driver failed\n"); 1903faee942SAlan Mizrahi platform_device_unregister(pdev); 1913faee942SAlan Mizrahi } 1923faee942SAlan Mizrahi 1933faee942SAlan Mizrahi return err; 1943faee942SAlan Mizrahi } 1953faee942SAlan Mizrahi 1963faee942SAlan Mizrahi static void __exit apu_led_exit(void) 1973faee942SAlan Mizrahi { 1983faee942SAlan Mizrahi int i; 1993faee942SAlan Mizrahi 200cc92e3ccSEnrico Weigelt for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) 2013faee942SAlan Mizrahi led_classdev_unregister(&apu_led->pled[i].cdev); 2023faee942SAlan Mizrahi 2033faee942SAlan Mizrahi platform_device_unregister(apu_led->pdev); 2043faee942SAlan Mizrahi platform_driver_unregister(&apu_led_driver); 2053faee942SAlan Mizrahi } 2063faee942SAlan Mizrahi 2073faee942SAlan Mizrahi module_init(apu_led_init); 2083faee942SAlan Mizrahi module_exit(apu_led_exit); 2093faee942SAlan Mizrahi 2103faee942SAlan Mizrahi MODULE_AUTHOR("Alan Mizrahi"); 2110344e616SEnrico Weigelt MODULE_DESCRIPTION("PC Engines APU1 front LED driver"); 2123faee942SAlan Mizrahi MODULE_LICENSE("GPL v2"); 2133faee942SAlan Mizrahi MODULE_ALIAS("platform:leds_apu"); 214