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 343faee942SAlan Mizrahi #include <linux/dmi.h> 353faee942SAlan Mizrahi #include <linux/err.h> 363faee942SAlan Mizrahi #include <linux/init.h> 373faee942SAlan Mizrahi #include <linux/io.h> 383faee942SAlan Mizrahi #include <linux/kernel.h> 393faee942SAlan Mizrahi #include <linux/leds.h> 403faee942SAlan Mizrahi #include <linux/module.h> 413faee942SAlan Mizrahi #include <linux/platform_device.h> 423faee942SAlan Mizrahi 433faee942SAlan Mizrahi #define APU1_FCH_ACPI_MMIO_BASE 0xFED80000 443faee942SAlan Mizrahi #define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x01BD) 453faee942SAlan Mizrahi #define APU1_LEDON 0x08 463faee942SAlan Mizrahi #define APU1_LEDOFF 0xC8 473faee942SAlan Mizrahi #define APU1_NUM_GPIO 3 483faee942SAlan Mizrahi #define APU1_IOSIZE sizeof(u8) 493faee942SAlan Mizrahi 503faee942SAlan Mizrahi #define APU2_FCH_ACPI_MMIO_BASE 0xFED80000 513faee942SAlan Mizrahi #define APU2_FCH_GPIO_BASE (APU2_FCH_ACPI_MMIO_BASE + 0x1500) 523faee942SAlan Mizrahi #define APU2_GPIO_BIT_WRITE 22 533faee942SAlan Mizrahi #define APU2_APU2_NUM_GPIO 4 543faee942SAlan Mizrahi #define APU2_IOSIZE sizeof(u32) 553faee942SAlan Mizrahi 563faee942SAlan Mizrahi /* LED access parameters */ 573faee942SAlan Mizrahi struct apu_param { 583faee942SAlan Mizrahi void __iomem *addr; /* for ioread/iowrite */ 593faee942SAlan Mizrahi }; 603faee942SAlan Mizrahi 613faee942SAlan Mizrahi /* LED private data */ 623faee942SAlan Mizrahi struct apu_led_priv { 633faee942SAlan Mizrahi struct led_classdev cdev; 643faee942SAlan Mizrahi struct apu_param param; 653faee942SAlan Mizrahi }; 663faee942SAlan Mizrahi #define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev) 673faee942SAlan Mizrahi 683faee942SAlan Mizrahi /* LED profile */ 693faee942SAlan Mizrahi struct apu_led_profile { 703faee942SAlan Mizrahi const char *name; 713faee942SAlan Mizrahi enum led_brightness brightness; 723faee942SAlan Mizrahi unsigned long offset; /* for devm_ioremap */ 733faee942SAlan Mizrahi }; 743faee942SAlan Mizrahi 753faee942SAlan Mizrahi /* Supported platform types */ 763faee942SAlan Mizrahi enum apu_led_platform_types { 773faee942SAlan Mizrahi APU1_LED_PLATFORM, 783faee942SAlan Mizrahi APU2_LED_PLATFORM, 793faee942SAlan Mizrahi }; 803faee942SAlan Mizrahi 813faee942SAlan Mizrahi struct apu_led_pdata { 823faee942SAlan Mizrahi struct platform_device *pdev; 833faee942SAlan Mizrahi struct apu_led_priv *pled; 843faee942SAlan Mizrahi const struct apu_led_profile *profile; 853faee942SAlan Mizrahi enum apu_led_platform_types platform; 863faee942SAlan Mizrahi int num_led_instances; 873faee942SAlan Mizrahi int iosize; /* for devm_ioremap() */ 883faee942SAlan Mizrahi spinlock_t lock; 893faee942SAlan Mizrahi }; 903faee942SAlan Mizrahi 913faee942SAlan Mizrahi static struct apu_led_pdata *apu_led; 923faee942SAlan Mizrahi 933faee942SAlan Mizrahi static const struct apu_led_profile apu1_led_profile[] = { 943faee942SAlan Mizrahi { "apu:green:1", LED_ON, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE }, 953faee942SAlan Mizrahi { "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE }, 963faee942SAlan Mizrahi { "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE }, 973faee942SAlan Mizrahi }; 983faee942SAlan Mizrahi 993faee942SAlan Mizrahi static const struct apu_led_profile apu2_led_profile[] = { 1003faee942SAlan Mizrahi { "apu2:green:1", LED_ON, APU2_FCH_GPIO_BASE + 68 * APU2_IOSIZE }, 1013faee942SAlan Mizrahi { "apu2:green:2", LED_OFF, APU2_FCH_GPIO_BASE + 69 * APU2_IOSIZE }, 1023faee942SAlan Mizrahi { "apu2:green:3", LED_OFF, APU2_FCH_GPIO_BASE + 70 * APU2_IOSIZE }, 1033faee942SAlan Mizrahi }; 1043faee942SAlan Mizrahi 1053faee942SAlan Mizrahi static const struct dmi_system_id apu_led_dmi_table[] __initconst = { 1063faee942SAlan Mizrahi { 1073faee942SAlan Mizrahi .ident = "apu", 1083faee942SAlan Mizrahi .matches = { 1093faee942SAlan Mizrahi DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), 1103faee942SAlan Mizrahi DMI_MATCH(DMI_PRODUCT_NAME, "APU") 1113faee942SAlan Mizrahi } 1123faee942SAlan Mizrahi }, 1132dd1ea5bSTimothy Redaelli /* PC Engines APU2 with "Legacy" bios < 4.0.8 */ 1143faee942SAlan Mizrahi { 1153faee942SAlan Mizrahi .ident = "apu2", 1163faee942SAlan Mizrahi .matches = { 1173faee942SAlan Mizrahi DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), 1183faee942SAlan Mizrahi DMI_MATCH(DMI_BOARD_NAME, "APU2") 1193faee942SAlan Mizrahi } 1203faee942SAlan Mizrahi }, 1212dd1ea5bSTimothy Redaelli /* PC Engines APU2 with "Legacy" bios >= 4.0.8 */ 1222dd1ea5bSTimothy Redaelli { 1232dd1ea5bSTimothy Redaelli .ident = "apu2", 1242dd1ea5bSTimothy Redaelli .matches = { 1252dd1ea5bSTimothy Redaelli DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), 1262dd1ea5bSTimothy Redaelli DMI_MATCH(DMI_BOARD_NAME, "apu2") 1272dd1ea5bSTimothy Redaelli } 1282dd1ea5bSTimothy Redaelli }, 1292dd1ea5bSTimothy Redaelli /* PC Engines APU2 with "Mainline" bios */ 1302dd1ea5bSTimothy Redaelli { 1312dd1ea5bSTimothy Redaelli .ident = "apu2", 1322dd1ea5bSTimothy Redaelli .matches = { 1332dd1ea5bSTimothy Redaelli DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), 1342dd1ea5bSTimothy Redaelli DMI_MATCH(DMI_BOARD_NAME, "PC Engines apu2") 1352dd1ea5bSTimothy Redaelli } 1362dd1ea5bSTimothy Redaelli }, 1373faee942SAlan Mizrahi {} 1383faee942SAlan Mizrahi }; 1393faee942SAlan Mizrahi MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table); 1403faee942SAlan Mizrahi 1413faee942SAlan Mizrahi static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value) 1423faee942SAlan Mizrahi { 1433faee942SAlan Mizrahi struct apu_led_priv *pled = cdev_to_priv(led); 1443faee942SAlan Mizrahi 1453faee942SAlan Mizrahi spin_lock(&apu_led->lock); 1463faee942SAlan Mizrahi iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr); 1473faee942SAlan Mizrahi spin_unlock(&apu_led->lock); 1483faee942SAlan Mizrahi } 1493faee942SAlan Mizrahi 1503faee942SAlan Mizrahi static void apu2_led_brightness_set(struct led_classdev *led, enum led_brightness value) 1513faee942SAlan Mizrahi { 1523faee942SAlan Mizrahi struct apu_led_priv *pled = cdev_to_priv(led); 1533faee942SAlan Mizrahi u32 value_new; 1543faee942SAlan Mizrahi 1553faee942SAlan Mizrahi spin_lock(&apu_led->lock); 1563faee942SAlan Mizrahi 1573faee942SAlan Mizrahi value_new = ioread32(pled->param.addr); 1583faee942SAlan Mizrahi 1593faee942SAlan Mizrahi if (value) 1603faee942SAlan Mizrahi value_new &= ~BIT(APU2_GPIO_BIT_WRITE); 1613faee942SAlan Mizrahi else 1623faee942SAlan Mizrahi value_new |= BIT(APU2_GPIO_BIT_WRITE); 1633faee942SAlan Mizrahi 1643faee942SAlan Mizrahi iowrite32(value_new, pled->param.addr); 1653faee942SAlan Mizrahi 1663faee942SAlan Mizrahi spin_unlock(&apu_led->lock); 1673faee942SAlan Mizrahi } 1683faee942SAlan Mizrahi 1693faee942SAlan Mizrahi static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld) 1703faee942SAlan Mizrahi { 1713faee942SAlan Mizrahi int i; 1723faee942SAlan Mizrahi int err; 1733faee942SAlan Mizrahi 174*a86854d0SKees Cook apu_led->pled = devm_kcalloc(dev, 175*a86854d0SKees Cook apu_led->num_led_instances, sizeof(struct apu_led_priv), 1763faee942SAlan Mizrahi GFP_KERNEL); 1773faee942SAlan Mizrahi 1783faee942SAlan Mizrahi if (!apu_led->pled) 1793faee942SAlan Mizrahi return -ENOMEM; 1803faee942SAlan Mizrahi 1813faee942SAlan Mizrahi for (i = 0; i < apu_led->num_led_instances; i++) { 1823faee942SAlan Mizrahi struct apu_led_priv *pled = &apu_led->pled[i]; 1833faee942SAlan Mizrahi struct led_classdev *led_cdev = &pled->cdev; 1843faee942SAlan Mizrahi 1853faee942SAlan Mizrahi led_cdev->name = apu_led->profile[i].name; 1863faee942SAlan Mizrahi led_cdev->brightness = apu_led->profile[i].brightness; 1873faee942SAlan Mizrahi led_cdev->max_brightness = 1; 1883faee942SAlan Mizrahi led_cdev->flags = LED_CORE_SUSPENDRESUME; 1893faee942SAlan Mizrahi if (apu_led->platform == APU1_LED_PLATFORM) 1903faee942SAlan Mizrahi led_cdev->brightness_set = apu1_led_brightness_set; 1913faee942SAlan Mizrahi else if (apu_led->platform == APU2_LED_PLATFORM) 1923faee942SAlan Mizrahi led_cdev->brightness_set = apu2_led_brightness_set; 1933faee942SAlan Mizrahi 1943faee942SAlan Mizrahi pled->param.addr = devm_ioremap(dev, 1953faee942SAlan Mizrahi apu_led->profile[i].offset, apu_led->iosize); 1963faee942SAlan Mizrahi if (!pled->param.addr) { 1973faee942SAlan Mizrahi err = -ENOMEM; 1983faee942SAlan Mizrahi goto error; 1993faee942SAlan Mizrahi } 2003faee942SAlan Mizrahi 2013faee942SAlan Mizrahi err = led_classdev_register(dev, led_cdev); 2023faee942SAlan Mizrahi if (err) 2033faee942SAlan Mizrahi goto error; 2043faee942SAlan Mizrahi 2053faee942SAlan Mizrahi led_cdev->brightness_set(led_cdev, apu_led->profile[i].brightness); 2063faee942SAlan Mizrahi } 2073faee942SAlan Mizrahi 2083faee942SAlan Mizrahi return 0; 2093faee942SAlan Mizrahi 2103faee942SAlan Mizrahi error: 2113faee942SAlan Mizrahi while (i-- > 0) 2123faee942SAlan Mizrahi led_classdev_unregister(&apu_led->pled[i].cdev); 2133faee942SAlan Mizrahi 2143faee942SAlan Mizrahi return err; 2153faee942SAlan Mizrahi } 2163faee942SAlan Mizrahi 2173faee942SAlan Mizrahi static int __init apu_led_probe(struct platform_device *pdev) 2183faee942SAlan Mizrahi { 2193faee942SAlan Mizrahi apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL); 2203faee942SAlan Mizrahi 2213faee942SAlan Mizrahi if (!apu_led) 2223faee942SAlan Mizrahi return -ENOMEM; 2233faee942SAlan Mizrahi 2243faee942SAlan Mizrahi apu_led->pdev = pdev; 2253faee942SAlan Mizrahi 22692d7ec1dSHans Ulli Kroll if (dmi_match(DMI_PRODUCT_NAME, "APU")) { 2273faee942SAlan Mizrahi apu_led->profile = apu1_led_profile; 2283faee942SAlan Mizrahi apu_led->platform = APU1_LED_PLATFORM; 2293faee942SAlan Mizrahi apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile); 2303faee942SAlan Mizrahi apu_led->iosize = APU1_IOSIZE; 2312dd1ea5bSTimothy Redaelli } else if (dmi_match(DMI_BOARD_NAME, "APU2") || 2322dd1ea5bSTimothy Redaelli dmi_match(DMI_BOARD_NAME, "apu2") || 2332dd1ea5bSTimothy Redaelli dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) { 2343faee942SAlan Mizrahi apu_led->profile = apu2_led_profile; 2353faee942SAlan Mizrahi apu_led->platform = APU2_LED_PLATFORM; 2363faee942SAlan Mizrahi apu_led->num_led_instances = ARRAY_SIZE(apu2_led_profile); 2373faee942SAlan Mizrahi apu_led->iosize = APU2_IOSIZE; 2383faee942SAlan Mizrahi } 2393faee942SAlan Mizrahi 2403faee942SAlan Mizrahi spin_lock_init(&apu_led->lock); 2413faee942SAlan Mizrahi return apu_led_config(&pdev->dev, apu_led); 2423faee942SAlan Mizrahi } 2433faee942SAlan Mizrahi 2443faee942SAlan Mizrahi static struct platform_driver apu_led_driver = { 2453faee942SAlan Mizrahi .driver = { 2463faee942SAlan Mizrahi .name = KBUILD_MODNAME, 2473faee942SAlan Mizrahi }, 2483faee942SAlan Mizrahi }; 2493faee942SAlan Mizrahi 2503faee942SAlan Mizrahi static int __init apu_led_init(void) 2513faee942SAlan Mizrahi { 2523faee942SAlan Mizrahi struct platform_device *pdev; 2533faee942SAlan Mizrahi int err; 2543faee942SAlan Mizrahi 2553faee942SAlan Mizrahi if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) { 2563faee942SAlan Mizrahi pr_err("No PC Engines board detected\n"); 2573faee942SAlan Mizrahi return -ENODEV; 2583faee942SAlan Mizrahi } 2592dd1ea5bSTimothy Redaelli if (!(dmi_match(DMI_PRODUCT_NAME, "APU") || 2602dd1ea5bSTimothy Redaelli dmi_match(DMI_PRODUCT_NAME, "APU2") || 2612dd1ea5bSTimothy Redaelli dmi_match(DMI_PRODUCT_NAME, "apu2") || 2622dd1ea5bSTimothy Redaelli dmi_match(DMI_PRODUCT_NAME, "PC Engines apu2"))) { 2633faee942SAlan Mizrahi pr_err("Unknown PC Engines board: %s\n", 2643faee942SAlan Mizrahi dmi_get_system_info(DMI_PRODUCT_NAME)); 2653faee942SAlan Mizrahi return -ENODEV; 2663faee942SAlan Mizrahi } 2673faee942SAlan Mizrahi 2683faee942SAlan Mizrahi pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0); 2693faee942SAlan Mizrahi if (IS_ERR(pdev)) { 2703faee942SAlan Mizrahi pr_err("Device allocation failed\n"); 2713faee942SAlan Mizrahi return PTR_ERR(pdev); 2723faee942SAlan Mizrahi } 2733faee942SAlan Mizrahi 2743faee942SAlan Mizrahi err = platform_driver_probe(&apu_led_driver, apu_led_probe); 2753faee942SAlan Mizrahi if (err) { 2763faee942SAlan Mizrahi pr_err("Probe platform driver failed\n"); 2773faee942SAlan Mizrahi platform_device_unregister(pdev); 2783faee942SAlan Mizrahi } 2793faee942SAlan Mizrahi 2803faee942SAlan Mizrahi return err; 2813faee942SAlan Mizrahi } 2823faee942SAlan Mizrahi 2833faee942SAlan Mizrahi static void __exit apu_led_exit(void) 2843faee942SAlan Mizrahi { 2853faee942SAlan Mizrahi int i; 2863faee942SAlan Mizrahi 2873faee942SAlan Mizrahi for (i = 0; i < apu_led->num_led_instances; i++) 2883faee942SAlan Mizrahi led_classdev_unregister(&apu_led->pled[i].cdev); 2893faee942SAlan Mizrahi 2903faee942SAlan Mizrahi platform_device_unregister(apu_led->pdev); 2913faee942SAlan Mizrahi platform_driver_unregister(&apu_led_driver); 2923faee942SAlan Mizrahi } 2933faee942SAlan Mizrahi 2943faee942SAlan Mizrahi module_init(apu_led_init); 2953faee942SAlan Mizrahi module_exit(apu_led_exit); 2963faee942SAlan Mizrahi 2973faee942SAlan Mizrahi MODULE_AUTHOR("Alan Mizrahi"); 2983faee942SAlan Mizrahi MODULE_DESCRIPTION("PC Engines APU family LED driver"); 2993faee942SAlan Mizrahi MODULE_LICENSE("GPL v2"); 3003faee942SAlan Mizrahi MODULE_ALIAS("platform:leds_apu"); 301