Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.2
  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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 35
 36#include <linux/dmi.h>
 37#include <linux/err.h>
 38#include <linux/init.h>
 39#include <linux/io.h>
 40#include <linux/kernel.h>
 41#include <linux/leds.h>
 42#include <linux/module.h>
 43#include <linux/platform_device.h>
 44
 45#define APU1_FCH_ACPI_MMIO_BASE 0xFED80000
 46#define APU1_FCH_GPIO_BASE      (APU1_FCH_ACPI_MMIO_BASE + 0x01BD)
 47#define APU1_LEDON              0x08
 48#define APU1_LEDOFF             0xC8
 49#define APU1_NUM_GPIO           3
 50#define APU1_IOSIZE             sizeof(u8)
 51
 
 
 
 
 
 
 52/* LED access parameters */
 53struct apu_param {
 54	void __iomem *addr; /* for ioread/iowrite */
 55};
 56
 57/* LED private data */
 58struct apu_led_priv {
 59	struct led_classdev cdev;
 60	struct apu_param param;
 61};
 62#define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev)
 63
 64/* LED profile */
 65struct apu_led_profile {
 66	const char *name;
 67	enum led_brightness brightness;
 68	unsigned long offset; /* for devm_ioremap */
 69};
 70
 
 
 
 
 
 
 71struct apu_led_pdata {
 72	struct platform_device *pdev;
 73	struct apu_led_priv *pled;
 
 
 
 
 74	spinlock_t lock;
 75};
 76
 77static struct apu_led_pdata *apu_led;
 78
 79static const struct apu_led_profile apu1_led_profile[] = {
 80	{ "apu:green:1", LED_ON,  APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE },
 81	{ "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE },
 82	{ "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE },
 83};
 84
 
 
 
 
 
 
 85static const struct dmi_system_id apu_led_dmi_table[] __initconst = {
 86	/* PC Engines APU with factory bios "SageBios_PCEngines_APU-45" */
 87	{
 88		.ident = "apu",
 89		.matches = {
 90			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
 91			DMI_MATCH(DMI_PRODUCT_NAME, "APU")
 92		}
 93	},
 94	/* PC Engines APU with "Mainline" bios >= 4.6.8 */
 95	{
 96		.ident = "apu",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97		.matches = {
 98			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
 99			DMI_MATCH(DMI_PRODUCT_NAME, "apu1")
100		}
101	},
102	{}
103};
104MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
105
106static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
107{
108	struct apu_led_priv *pled = cdev_to_priv(led);
109
110	spin_lock(&apu_led->lock);
111	iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
112	spin_unlock(&apu_led->lock);
113}
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
116{
117	int i;
118	int err;
119
120	apu_led->pled = devm_kcalloc(dev,
121		ARRAY_SIZE(apu1_led_profile), sizeof(struct apu_led_priv),
122		GFP_KERNEL);
123
124	if (!apu_led->pled)
125		return -ENOMEM;
126
127	for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++) {
128		struct apu_led_priv *pled = &apu_led->pled[i];
129		struct led_classdev *led_cdev = &pled->cdev;
130
131		led_cdev->name = apu1_led_profile[i].name;
132		led_cdev->brightness = apu1_led_profile[i].brightness;
133		led_cdev->max_brightness = 1;
134		led_cdev->flags = LED_CORE_SUSPENDRESUME;
135		led_cdev->brightness_set = apu1_led_brightness_set;
 
 
 
136
137		pled->param.addr = devm_ioremap(dev,
138				apu1_led_profile[i].offset, APU1_IOSIZE);
139		if (!pled->param.addr) {
140			err = -ENOMEM;
141			goto error;
142		}
143
144		err = led_classdev_register(dev, led_cdev);
145		if (err)
146			goto error;
147
148		apu1_led_brightness_set(led_cdev, apu1_led_profile[i].brightness);
149	}
150
151	return 0;
152
153error:
154	while (i-- > 0)
155		led_classdev_unregister(&apu_led->pled[i].cdev);
156
157	return err;
158}
159
160static int __init apu_led_probe(struct platform_device *pdev)
161{
162	apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
163
164	if (!apu_led)
165		return -ENOMEM;
166
167	apu_led->pdev = pdev;
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169	spin_lock_init(&apu_led->lock);
170	return apu_led_config(&pdev->dev, apu_led);
171}
172
173static struct platform_driver apu_led_driver = {
174	.driver = {
175		.name = KBUILD_MODNAME,
176	},
177};
178
179static int __init apu_led_init(void)
180{
181	struct platform_device *pdev;
182	int err;
183
184	if (!(dmi_match(DMI_SYS_VENDOR, "PC Engines") &&
185	      (dmi_match(DMI_PRODUCT_NAME, "APU") || dmi_match(DMI_PRODUCT_NAME, "apu1")))) {
186		pr_err("No PC Engines APUv1 board detected. For APUv2,3 support, enable CONFIG_PCENGINES_APU2\n");
 
 
 
 
 
 
 
187		return -ENODEV;
188	}
189
190	pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
191	if (IS_ERR(pdev)) {
192		pr_err("Device allocation failed\n");
193		return PTR_ERR(pdev);
194	}
195
196	err = platform_driver_probe(&apu_led_driver, apu_led_probe);
197	if (err) {
198		pr_err("Probe platform driver failed\n");
199		platform_device_unregister(pdev);
200	}
201
202	return err;
203}
204
205static void __exit apu_led_exit(void)
206{
207	int i;
208
209	for (i = 0; i < ARRAY_SIZE(apu1_led_profile); i++)
210		led_classdev_unregister(&apu_led->pled[i].cdev);
211
212	platform_device_unregister(apu_led->pdev);
213	platform_driver_unregister(&apu_led_driver);
214}
215
216module_init(apu_led_init);
217module_exit(apu_led_exit);
218
219MODULE_AUTHOR("Alan Mizrahi");
220MODULE_DESCRIPTION("PC Engines APU1 front LED driver");
221MODULE_LICENSE("GPL v2");
222MODULE_ALIAS("platform:leds_apu");
v4.17
  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#define APU2_FCH_ACPI_MMIO_BASE 0xFED80000
 51#define APU2_FCH_GPIO_BASE      (APU2_FCH_ACPI_MMIO_BASE + 0x1500)
 52#define APU2_GPIO_BIT_WRITE     22
 53#define APU2_APU2_NUM_GPIO      4
 54#define APU2_IOSIZE             sizeof(u32)
 55
 56/* LED access parameters */
 57struct apu_param {
 58	void __iomem *addr; /* for ioread/iowrite */
 59};
 60
 61/* LED private data */
 62struct apu_led_priv {
 63	struct led_classdev cdev;
 64	struct apu_param param;
 65};
 66#define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev)
 67
 68/* LED profile */
 69struct apu_led_profile {
 70	const char *name;
 71	enum led_brightness brightness;
 72	unsigned long offset; /* for devm_ioremap */
 73};
 74
 75/* Supported platform types */
 76enum apu_led_platform_types {
 77	APU1_LED_PLATFORM,
 78	APU2_LED_PLATFORM,
 79};
 80
 81struct apu_led_pdata {
 82	struct platform_device *pdev;
 83	struct apu_led_priv *pled;
 84	const struct apu_led_profile *profile;
 85	enum apu_led_platform_types platform;
 86	int num_led_instances;
 87	int iosize; /* for devm_ioremap() */
 88	spinlock_t lock;
 89};
 90
 91static struct apu_led_pdata *apu_led;
 92
 93static const struct apu_led_profile apu1_led_profile[] = {
 94	{ "apu:green:1", LED_ON,  APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE },
 95	{ "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE },
 96	{ "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE },
 97};
 98
 99static const struct apu_led_profile apu2_led_profile[] = {
100	{ "apu2:green:1", LED_ON,  APU2_FCH_GPIO_BASE + 68 * APU2_IOSIZE },
101	{ "apu2:green:2", LED_OFF, APU2_FCH_GPIO_BASE + 69 * APU2_IOSIZE },
102	{ "apu2:green:3", LED_OFF, APU2_FCH_GPIO_BASE + 70 * APU2_IOSIZE },
103};
104
105static const struct dmi_system_id apu_led_dmi_table[] __initconst = {
 
106	{
107		.ident = "apu",
108		.matches = {
109			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
110			DMI_MATCH(DMI_PRODUCT_NAME, "APU")
111		}
112	},
113	/* PC Engines APU2 with "Legacy" bios < 4.0.8 */
114	{
115		.ident = "apu2",
116		.matches = {
117			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
118			DMI_MATCH(DMI_BOARD_NAME, "APU2")
119		}
120	},
121	/* PC Engines APU2 with "Legacy" bios >= 4.0.8 */
122	{
123		.ident = "apu2",
124		.matches = {
125			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
126			DMI_MATCH(DMI_BOARD_NAME, "apu2")
127		}
128	},
129	/* PC Engines APU2 with "Mainline" bios */
130	{
131		.ident = "apu2",
132		.matches = {
133			DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"),
134			DMI_MATCH(DMI_BOARD_NAME, "PC Engines apu2")
135		}
136	},
137	{}
138};
139MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table);
140
141static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value)
142{
143	struct apu_led_priv *pled = cdev_to_priv(led);
144
145	spin_lock(&apu_led->lock);
146	iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr);
147	spin_unlock(&apu_led->lock);
148}
149
150static void apu2_led_brightness_set(struct led_classdev *led, enum led_brightness value)
151{
152	struct apu_led_priv *pled = cdev_to_priv(led);
153	u32 value_new;
154
155	spin_lock(&apu_led->lock);
156
157	value_new = ioread32(pled->param.addr);
158
159	if (value)
160		value_new &= ~BIT(APU2_GPIO_BIT_WRITE);
161	else
162		value_new |= BIT(APU2_GPIO_BIT_WRITE);
163
164	iowrite32(value_new, pled->param.addr);
165
166	spin_unlock(&apu_led->lock);
167}
168
169static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld)
170{
171	int i;
172	int err;
173
174	apu_led->pled = devm_kzalloc(dev,
175		sizeof(struct apu_led_priv) * apu_led->num_led_instances,
176		GFP_KERNEL);
177
178	if (!apu_led->pled)
179		return -ENOMEM;
180
181	for (i = 0; i < apu_led->num_led_instances; i++) {
182		struct apu_led_priv *pled = &apu_led->pled[i];
183		struct led_classdev *led_cdev = &pled->cdev;
184
185		led_cdev->name = apu_led->profile[i].name;
186		led_cdev->brightness = apu_led->profile[i].brightness;
187		led_cdev->max_brightness = 1;
188		led_cdev->flags = LED_CORE_SUSPENDRESUME;
189		if (apu_led->platform == APU1_LED_PLATFORM)
190			led_cdev->brightness_set = apu1_led_brightness_set;
191		else if (apu_led->platform == APU2_LED_PLATFORM)
192			led_cdev->brightness_set = apu2_led_brightness_set;
193
194		pled->param.addr = devm_ioremap(dev,
195				apu_led->profile[i].offset, apu_led->iosize);
196		if (!pled->param.addr) {
197			err = -ENOMEM;
198			goto error;
199		}
200
201		err = led_classdev_register(dev, led_cdev);
202		if (err)
203			goto error;
204
205		led_cdev->brightness_set(led_cdev, apu_led->profile[i].brightness);
206	}
207
208	return 0;
209
210error:
211	while (i-- > 0)
212		led_classdev_unregister(&apu_led->pled[i].cdev);
213
214	return err;
215}
216
217static int __init apu_led_probe(struct platform_device *pdev)
218{
219	apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL);
220
221	if (!apu_led)
222		return -ENOMEM;
223
224	apu_led->pdev = pdev;
225
226	if (dmi_match(DMI_PRODUCT_NAME, "APU")) {
227		apu_led->profile = apu1_led_profile;
228		apu_led->platform = APU1_LED_PLATFORM;
229		apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile);
230		apu_led->iosize = APU1_IOSIZE;
231	} else if (dmi_match(DMI_BOARD_NAME, "APU2") ||
232		   dmi_match(DMI_BOARD_NAME, "apu2") ||
233		   dmi_match(DMI_BOARD_NAME, "PC Engines apu2")) {
234		apu_led->profile = apu2_led_profile;
235		apu_led->platform = APU2_LED_PLATFORM;
236		apu_led->num_led_instances = ARRAY_SIZE(apu2_led_profile);
237		apu_led->iosize = APU2_IOSIZE;
238	}
239
240	spin_lock_init(&apu_led->lock);
241	return apu_led_config(&pdev->dev, apu_led);
242}
243
244static struct platform_driver apu_led_driver = {
245	.driver = {
246		.name = KBUILD_MODNAME,
247	},
248};
249
250static int __init apu_led_init(void)
251{
252	struct platform_device *pdev;
253	int err;
254
255	if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) {
256		pr_err("No PC Engines board detected\n");
257		return -ENODEV;
258	}
259	if (!(dmi_match(DMI_PRODUCT_NAME, "APU") ||
260	      dmi_match(DMI_PRODUCT_NAME, "APU2") ||
261	      dmi_match(DMI_PRODUCT_NAME, "apu2") ||
262	      dmi_match(DMI_PRODUCT_NAME, "PC Engines apu2"))) {
263		pr_err("Unknown PC Engines board: %s\n",
264				dmi_get_system_info(DMI_PRODUCT_NAME));
265		return -ENODEV;
266	}
267
268	pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
269	if (IS_ERR(pdev)) {
270		pr_err("Device allocation failed\n");
271		return PTR_ERR(pdev);
272	}
273
274	err = platform_driver_probe(&apu_led_driver, apu_led_probe);
275	if (err) {
276		pr_err("Probe platform driver failed\n");
277		platform_device_unregister(pdev);
278	}
279
280	return err;
281}
282
283static void __exit apu_led_exit(void)
284{
285	int i;
286
287	for (i = 0; i < apu_led->num_led_instances; i++)
288		led_classdev_unregister(&apu_led->pled[i].cdev);
289
290	platform_device_unregister(apu_led->pdev);
291	platform_driver_unregister(&apu_led_driver);
292}
293
294module_init(apu_led_init);
295module_exit(apu_led_exit);
296
297MODULE_AUTHOR("Alan Mizrahi");
298MODULE_DESCRIPTION("PC Engines APU family LED driver");
299MODULE_LICENSE("GPL v2");
300MODULE_ALIAS("platform:leds_apu");