xref: /openbmc/linux/drivers/leds/leds-apu.c (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
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 
3460439711SEnrico Weigelt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3560439711SEnrico 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 = {
86*1b40faf7SAndreas Eberlein 	/* PC Engines APU with factory bios "SageBios_PCEngines_APU-45" */
873faee942SAlan Mizrahi 	{
883faee942SAlan Mizrahi 		.ident = "apu",
893faee942SAlan Mizrahi 		.matches = {
903faee942SAlan Mizrahi 			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
913faee942SAlan Mizrahi 			DMI_MATCH(DMI_PRODUCT_NAME, "APU")
923faee942SAlan Mizrahi 		}
933faee942SAlan Mizrahi 	},
94*1b40faf7SAndreas Eberlein 	/* PC Engines APU with "Mainline" bios >= 4.6.8 */
95*1b40faf7SAndreas Eberlein 	{
96*1b40faf7SAndreas Eberlein 		.ident = "apu",
97*1b40faf7SAndreas Eberlein 		.matches = {
98*1b40faf7SAndreas Eberlein 			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
99*1b40faf7SAndreas Eberlein 			DMI_MATCH(DMI_PRODUCT_NAME, "apu1")
100*1b40faf7SAndreas Eberlein 		}
101*1b40faf7SAndreas Eberlein 	},
1023faee942SAlan Mizrahi 	{}
1033faee942SAlan Mizrahi };
1043faee942SAlan Mizrahi MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
1053faee942SAlan Mizrahi 
apu1_led_brightness_set(struct led_classdev * led,enum led_brightness value)1063faee942SAlan Mizrahi static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
1073faee942SAlan Mizrahi {
1083faee942SAlan Mizrahi 	struct apu_led_priv *pled = cdev_to_priv(led);
1093faee942SAlan Mizrahi 
1103faee942SAlan Mizrahi 	spin_lock(&apu_led->lock);
1113faee942SAlan Mizrahi 	iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
1123faee942SAlan Mizrahi 	spin_unlock(&apu_led->lock);
1133faee942SAlan Mizrahi }
1143faee942SAlan Mizrahi 
apu_led_config(struct device * dev,struct apu_led_pdata * apuld)1153faee942SAlan Mizrahi static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
1163faee942SAlan Mizrahi {
1173faee942SAlan Mizrahi 	int i;
1183faee942SAlan Mizrahi 	int err;
1193faee942SAlan Mizrahi 
120a86854d0SKees Cook 	apu_led->pled = devm_kcalloc(dev,
121cc92e3ccSEnrico Weigelt 		ARRAY_SIZE(apu1_led_profile), sizeof(struct apu_led_priv),
1223faee942SAlan Mizrahi 		GFP_KERNEL);
1233faee942SAlan Mizrahi 
1243faee942SAlan Mizrahi 	if (!apu_led->pled)
1253faee942SAlan Mizrahi 		return -ENOMEM;
1263faee942SAlan Mizrahi 
127cc92e3ccSEnrico Weigelt 	for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) {
1283faee942SAlan Mizrahi 		struct apu_led_priv *pled = &apu_led->pled[i];
1293faee942SAlan Mizrahi 		struct led_classdev *led_cdev = &pled->cdev;
1303faee942SAlan Mizrahi 
131cc92e3ccSEnrico Weigelt 		led_cdev->name = apu1_led_profile[i].name;
132cc92e3ccSEnrico Weigelt 		led_cdev->brightness = apu1_led_profile[i].brightness;
1333faee942SAlan Mizrahi 		led_cdev->max_brightness = 1;
1343faee942SAlan Mizrahi 		led_cdev->flags = LED_CORE_SUSPENDRESUME;
1353faee942SAlan Mizrahi 		led_cdev->brightness_set = apu1_led_brightness_set;
1363faee942SAlan Mizrahi 
1373faee942SAlan Mizrahi 		pled->param.addr = devm_ioremap(dev,
138cc92e3ccSEnrico Weigelt 				apu1_led_profile[i].offset, APU1_IOSIZE);
1393faee942SAlan Mizrahi 		if (!pled->param.addr) {
1403faee942SAlan Mizrahi 			err = -ENOMEM;
1413faee942SAlan Mizrahi 			goto error;
1423faee942SAlan Mizrahi 		}
1433faee942SAlan Mizrahi 
1443faee942SAlan Mizrahi 		err = led_classdev_register(dev, led_cdev);
1453faee942SAlan Mizrahi 		if (err)
1463faee942SAlan Mizrahi 			goto error;
1473faee942SAlan Mizrahi 
148cc92e3ccSEnrico Weigelt 		apu1_led_brightness_set(led_cdev, apu1_led_profile[i].brightness);
1493faee942SAlan Mizrahi 	}
1503faee942SAlan Mizrahi 
1513faee942SAlan Mizrahi 	return 0;
1523faee942SAlan Mizrahi 
1533faee942SAlan Mizrahi error:
1543faee942SAlan Mizrahi 	while (i-- > 0)
1553faee942SAlan Mizrahi 		led_classdev_unregister(&apu_led->pled[i].cdev);
1563faee942SAlan Mizrahi 
1573faee942SAlan Mizrahi 	return err;
1583faee942SAlan Mizrahi }
1593faee942SAlan Mizrahi 
apu_led_probe(struct platform_device * pdev)1603faee942SAlan Mizrahi static int __init apu_led_probe(struct platform_device *pdev)
1613faee942SAlan Mizrahi {
1623faee942SAlan Mizrahi 	apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
1633faee942SAlan Mizrahi 
1643faee942SAlan Mizrahi 	if (!apu_led)
1653faee942SAlan Mizrahi 		return -ENOMEM;
1663faee942SAlan Mizrahi 
1673faee942SAlan Mizrahi 	apu_led->pdev = pdev;
1683faee942SAlan Mizrahi 
1693faee942SAlan Mizrahi 	spin_lock_init(&apu_led->lock);
1703faee942SAlan Mizrahi 	return apu_led_config(&pdev->dev, apu_led);
1713faee942SAlan Mizrahi }
1723faee942SAlan Mizrahi 
1733faee942SAlan Mizrahi static struct platform_driver apu_led_driver = {
1743faee942SAlan Mizrahi 	.driver = {
1753faee942SAlan Mizrahi 		.name = KBUILD_MODNAME,
1763faee942SAlan Mizrahi 	},
1773faee942SAlan Mizrahi };
1783faee942SAlan Mizrahi 
apu_led_init(void)1793faee942SAlan Mizrahi static int __init apu_led_init(void)
1803faee942SAlan Mizrahi {
1813faee942SAlan Mizrahi 	struct platform_device *pdev;
1823faee942SAlan Mizrahi 	int err;
1833faee942SAlan Mizrahi 
18493562049SEnrico Weigelt 	if (!(dmi_match(DMI_SYS_VENDOR, "PC Engines") &&
185*1b40faf7SAndreas Eberlein 	      (dmi_match(DMI_PRODUCT_NAME, "APU") || dmi_match(DMI_PRODUCT_NAME, "apu1")))) {
18693562049SEnrico Weigelt 		pr_err("No PC Engines APUv1 board detected. For APUv2,3 support, enable CONFIG_PCENGINES_APU2\n");
1873faee942SAlan Mizrahi 		return -ENODEV;
1883faee942SAlan Mizrahi 	}
1893faee942SAlan Mizrahi 
1903faee942SAlan Mizrahi 	pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
1913faee942SAlan Mizrahi 	if (IS_ERR(pdev)) {
1923faee942SAlan Mizrahi 		pr_err("Device allocation failed\n");
1933faee942SAlan Mizrahi 		return PTR_ERR(pdev);
1943faee942SAlan Mizrahi 	}
1953faee942SAlan Mizrahi 
1963faee942SAlan Mizrahi 	err = platform_driver_probe(&apu_led_driver, apu_led_probe);
1973faee942SAlan Mizrahi 	if (err) {
1983faee942SAlan Mizrahi 		pr_err("Probe platform driver failed\n");
1993faee942SAlan Mizrahi 		platform_device_unregister(pdev);
2003faee942SAlan Mizrahi 	}
2013faee942SAlan Mizrahi 
2023faee942SAlan Mizrahi 	return err;
2033faee942SAlan Mizrahi }
2043faee942SAlan Mizrahi 
apu_led_exit(void)2053faee942SAlan Mizrahi static void __exit apu_led_exit(void)
2063faee942SAlan Mizrahi {
2073faee942SAlan Mizrahi 	int i;
2083faee942SAlan Mizrahi 
209cc92e3ccSEnrico Weigelt 	for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++)
2103faee942SAlan Mizrahi 		led_classdev_unregister(&apu_led->pled[i].cdev);
2113faee942SAlan Mizrahi 
2123faee942SAlan Mizrahi 	platform_device_unregister(apu_led->pdev);
2133faee942SAlan Mizrahi 	platform_driver_unregister(&apu_led_driver);
2143faee942SAlan Mizrahi }
2153faee942SAlan Mizrahi 
2163faee942SAlan Mizrahi module_init(apu_led_init);
2173faee942SAlan Mizrahi module_exit(apu_led_exit);
2183faee942SAlan Mizrahi 
2193faee942SAlan Mizrahi MODULE_AUTHOR("Alan Mizrahi");
2200344e616SEnrico Weigelt MODULE_DESCRIPTION("PC Engines APU1 front LED driver");
2213faee942SAlan Mizrahi MODULE_LICENSE("GPL v2");
2223faee942SAlan Mizrahi MODULE_ALIAS("platform:leds_apu");
223