Skip to content

Commit a6c80be

Browse files
henning-schildlag-linaro
authored andcommitted
leds: simatic-ipc-leds-gpio: Add GPIO version of Siemens driver
On Apollo Lake the pinctrl drivers will now come up without ACPI. Use that instead of open coding it. Create a new driver for that which can later be filled with more GPIO based models, and which has different dependencies. Signed-off-by: Henning Schild <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent 446f0cf commit a6c80be

File tree

5 files changed

+117
-81
lines changed

5 files changed

+117
-81
lines changed

drivers/leds/simple/Kconfig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
config LEDS_SIEMENS_SIMATIC_IPC
33
tristate "LED driver for Siemens Simatic IPCs"
4-
depends on LEDS_CLASS
4+
depends on LEDS_GPIO
55
depends on SIEMENS_SIMATIC_IPC
6-
select P2SB
76
help
87
This option enables support for the LEDs of several Industrial PCs
98
from Siemens.
109

11-
To compile this driver as a module, choose M here: the module
12-
will be called simatic-ipc-leds.
10+
To compile this driver as a module, choose M here: the modules
11+
will be called simatic-ipc-leds and simatic-ipc-leds-gpio.

drivers/leds/simple/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_LEDS_SIEMENS_SIMATIC_IPC) += simatic-ipc-leds.o
3+
obj-$(CONFIG_LEDS_SIEMENS_SIMATIC_IPC) += simatic-ipc-leds-gpio.o
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Siemens SIMATIC IPC driver for GPIO based LEDs
4+
*
5+
* Copyright (c) Siemens AG, 2022
6+
*
7+
* Authors:
8+
* Henning Schild <[email protected]>
9+
*/
10+
11+
#include <linux/gpio/machine.h>
12+
#include <linux/gpio/consumer.h>
13+
#include <linux/leds.h>
14+
#include <linux/module.h>
15+
#include <linux/platform_device.h>
16+
17+
static struct gpiod_lookup_table simatic_ipc_led_gpio_table = {
18+
.dev_id = "leds-gpio",
19+
.table = {
20+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 51, NULL, 0, GPIO_ACTIVE_LOW),
21+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 52, NULL, 1, GPIO_ACTIVE_LOW),
22+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 53, NULL, 2, GPIO_ACTIVE_LOW),
23+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 57, NULL, 3, GPIO_ACTIVE_LOW),
24+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 58, NULL, 4, GPIO_ACTIVE_LOW),
25+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 60, NULL, 5, GPIO_ACTIVE_LOW),
26+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 56, NULL, 6, GPIO_ACTIVE_LOW),
27+
GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 59, NULL, 7, GPIO_ACTIVE_HIGH),
28+
},
29+
};
30+
31+
static const struct gpio_led simatic_ipc_gpio_leds[] = {
32+
{ .name = "green:" LED_FUNCTION_STATUS "-3" },
33+
{ .name = "red:" LED_FUNCTION_STATUS "-1" },
34+
{ .name = "green:" LED_FUNCTION_STATUS "-1" },
35+
{ .name = "red:" LED_FUNCTION_STATUS "-2" },
36+
{ .name = "green:" LED_FUNCTION_STATUS "-2" },
37+
{ .name = "red:" LED_FUNCTION_STATUS "-3" },
38+
};
39+
40+
static const struct gpio_led_platform_data simatic_ipc_gpio_leds_pdata = {
41+
.num_leds = ARRAY_SIZE(simatic_ipc_gpio_leds),
42+
.leds = simatic_ipc_gpio_leds,
43+
};
44+
45+
static struct platform_device *simatic_leds_pdev;
46+
47+
static int simatic_ipc_leds_gpio_remove(struct platform_device *pdev)
48+
{
49+
gpiod_remove_lookup_table(&simatic_ipc_led_gpio_table);
50+
platform_device_unregister(simatic_leds_pdev);
51+
52+
return 0;
53+
}
54+
55+
static int simatic_ipc_leds_gpio_probe(struct platform_device *pdev)
56+
{
57+
struct gpio_desc *gpiod;
58+
int err;
59+
60+
gpiod_add_lookup_table(&simatic_ipc_led_gpio_table);
61+
simatic_leds_pdev = platform_device_register_resndata(NULL,
62+
"leds-gpio", PLATFORM_DEVID_NONE, NULL, 0,
63+
&simatic_ipc_gpio_leds_pdata,
64+
sizeof(simatic_ipc_gpio_leds_pdata));
65+
if (IS_ERR(simatic_leds_pdev)) {
66+
err = PTR_ERR(simatic_leds_pdev);
67+
goto out;
68+
}
69+
70+
/* PM_BIOS_BOOT_N */
71+
gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 6, GPIOD_OUT_LOW);
72+
if (IS_ERR(gpiod)) {
73+
err = PTR_ERR(gpiod);
74+
goto out;
75+
}
76+
gpiod_put(gpiod);
77+
78+
/* PM_WDT_OUT */
79+
gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 7, GPIOD_OUT_LOW);
80+
if (IS_ERR(gpiod)) {
81+
err = PTR_ERR(gpiod);
82+
goto out;
83+
}
84+
gpiod_put(gpiod);
85+
86+
return 0;
87+
out:
88+
simatic_ipc_leds_gpio_remove(pdev);
89+
90+
return err;
91+
}
92+
93+
static struct platform_driver simatic_ipc_led_gpio_driver = {
94+
.probe = simatic_ipc_leds_gpio_probe,
95+
.remove = simatic_ipc_leds_gpio_remove,
96+
.driver = {
97+
.name = KBUILD_MODNAME,
98+
}
99+
};
100+
module_platform_driver(simatic_ipc_led_gpio_driver);
101+
102+
MODULE_LICENSE("GPL v2");
103+
MODULE_ALIAS("platform:" KBUILD_MODNAME);
104+
MODULE_SOFTDEP("pre: platform:leds-gpio");
105+
MODULE_AUTHOR("Henning Schild <[email protected]>");

drivers/leds/simple/simatic-ipc-leds.c

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <linux/leds.h>
1616
#include <linux/module.h>
1717
#include <linux/pci.h>
18-
#include <linux/platform_data/x86/p2sb.h>
1918
#include <linux/platform_data/x86/simatic-ipc-base.h>
2019
#include <linux/platform_device.h>
2120
#include <linux/sizes.h>
@@ -24,7 +23,7 @@
2423
#define SIMATIC_IPC_LED_PORT_BASE 0x404E
2524

2625
struct simatic_ipc_led {
27-
unsigned int value; /* mask for io and offset for mem */
26+
unsigned int value; /* mask for io */
2827
char *name;
2928
struct led_classdev cdev;
3029
};
@@ -39,21 +38,6 @@ static struct simatic_ipc_led simatic_ipc_leds_io[] = {
3938
{ }
4039
};
4140

42-
/* the actual start will be discovered with p2sb, 0 is a placeholder */
43-
static struct resource simatic_ipc_led_mem_res = DEFINE_RES_MEM_NAMED(0, 0, KBUILD_MODNAME);
44-
45-
static void __iomem *simatic_ipc_led_memory;
46-
47-
static struct simatic_ipc_led simatic_ipc_leds_mem[] = {
48-
{0x500 + 0x1A0, "red:" LED_FUNCTION_STATUS "-1"},
49-
{0x500 + 0x1A8, "green:" LED_FUNCTION_STATUS "-1"},
50-
{0x500 + 0x1C8, "red:" LED_FUNCTION_STATUS "-2"},
51-
{0x500 + 0x1D0, "green:" LED_FUNCTION_STATUS "-2"},
52-
{0x500 + 0x1E0, "red:" LED_FUNCTION_STATUS "-3"},
53-
{0x500 + 0x198, "green:" LED_FUNCTION_STATUS "-3"},
54-
{ }
55-
};
56-
5741
static struct resource simatic_ipc_led_io_res =
5842
DEFINE_RES_IO_NAMED(SIMATIC_IPC_LED_PORT_BASE, SZ_2, KBUILD_MODNAME);
5943

@@ -89,38 +73,14 @@ static enum led_brightness simatic_ipc_led_get_io(struct led_classdev *led_cd)
8973
return inw(SIMATIC_IPC_LED_PORT_BASE) & led->value ? LED_OFF : led_cd->max_brightness;
9074
}
9175

92-
static void simatic_ipc_led_set_mem(struct led_classdev *led_cd,
93-
enum led_brightness brightness)
94-
{
95-
struct simatic_ipc_led *led = cdev_to_led(led_cd);
96-
void __iomem *reg = simatic_ipc_led_memory + led->value;
97-
u32 val;
98-
99-
val = readl(reg);
100-
val = (val & ~1) | (brightness == LED_OFF);
101-
writel(val, reg);
102-
}
103-
104-
static enum led_brightness simatic_ipc_led_get_mem(struct led_classdev *led_cd)
105-
{
106-
struct simatic_ipc_led *led = cdev_to_led(led_cd);
107-
void __iomem *reg = simatic_ipc_led_memory + led->value;
108-
u32 val;
109-
110-
val = readl(reg);
111-
return (val & 1) ? LED_OFF : led_cd->max_brightness;
112-
}
113-
11476
static int simatic_ipc_leds_probe(struct platform_device *pdev)
11577
{
11678
const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
11779
struct device *dev = &pdev->dev;
11880
struct simatic_ipc_led *ipcled;
11981
struct led_classdev *cdev;
12082
struct resource *res;
121-
void __iomem *reg;
122-
int err, type;
123-
u32 val;
83+
int err;
12484

12585
switch (plat->devmode) {
12686
case SIMATIC_IPC_DEVICE_227D:
@@ -135,51 +95,19 @@ static int simatic_ipc_leds_probe(struct platform_device *pdev)
13595
}
13696
ipcled = simatic_ipc_leds_io;
13797
}
138-
type = IORESOURCE_IO;
13998
if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
14099
dev_err(dev, "Unable to register IO resource at %pR\n", res);
141100
return -EBUSY;
142101
}
143102
break;
144-
case SIMATIC_IPC_DEVICE_127E:
145-
res = &simatic_ipc_led_mem_res;
146-
ipcled = simatic_ipc_leds_mem;
147-
type = IORESOURCE_MEM;
148-
149-
err = p2sb_bar(NULL, 0, res);
150-
if (err)
151-
return err;
152-
153-
/* do the final address calculation */
154-
res->start = res->start + (0xC5 << 16);
155-
res->end = res->start + SZ_4K - 1;
156-
157-
simatic_ipc_led_memory = devm_ioremap_resource(dev, res);
158-
if (IS_ERR(simatic_ipc_led_memory))
159-
return PTR_ERR(simatic_ipc_led_memory);
160-
161-
/* initialize power/watchdog LED */
162-
reg = simatic_ipc_led_memory + 0x500 + 0x1D8; /* PM_WDT_OUT */
163-
val = readl(reg);
164-
writel(val & ~1, reg);
165-
166-
reg = simatic_ipc_led_memory + 0x500 + 0x1C0; /* PM_BIOS_BOOT_N */
167-
val = readl(reg);
168-
writel(val | 1, reg);
169-
break;
170103
default:
171104
return -ENODEV;
172105
}
173106

174107
while (ipcled->value) {
175108
cdev = &ipcled->cdev;
176-
if (type == IORESOURCE_MEM) {
177-
cdev->brightness_set = simatic_ipc_led_set_mem;
178-
cdev->brightness_get = simatic_ipc_led_get_mem;
179-
} else {
180-
cdev->brightness_set = simatic_ipc_led_set_io;
181-
cdev->brightness_get = simatic_ipc_led_get_io;
182-
}
109+
cdev->brightness_set = simatic_ipc_led_set_io;
110+
cdev->brightness_get = simatic_ipc_led_get_io;
183111
cdev->max_brightness = LED_ON;
184112
cdev->name = ipcled->name;
185113

drivers/platform/x86/simatic-ipc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static int register_platform_devices(u32 station_id)
5151
{
5252
u8 ledmode = SIMATIC_IPC_DEVICE_NONE;
5353
u8 wdtmode = SIMATIC_IPC_DEVICE_NONE;
54+
char *pdevname = KBUILD_MODNAME "_leds";
5455
int i;
5556

5657
platform_data.devmode = SIMATIC_IPC_DEVICE_NONE;
@@ -64,10 +65,12 @@ static int register_platform_devices(u32 station_id)
6465
}
6566

6667
if (ledmode != SIMATIC_IPC_DEVICE_NONE) {
68+
if (ledmode == SIMATIC_IPC_DEVICE_127E)
69+
pdevname = KBUILD_MODNAME "_leds_gpio";
6770
platform_data.devmode = ledmode;
6871
ipc_led_platform_device =
6972
platform_device_register_data(NULL,
70-
KBUILD_MODNAME "_leds", PLATFORM_DEVID_NONE,
73+
pdevname, PLATFORM_DEVID_NONE,
7174
&platform_data,
7275
sizeof(struct simatic_ipc_platform));
7376
if (IS_ERR(ipc_led_platform_device))

0 commit comments

Comments
 (0)