xref: /linux/drivers/leds/simple/simatic-ipc-leds.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Siemens SIMATIC IPC driver for LEDs
4  *
5  * Copyright (c) Siemens AG, 2018-2021
6  *
7  * Authors:
8  *  Henning Schild <henning.schild@siemens.com>
9  *  Jan Kiszka <jan.kiszka@siemens.com>
10  *  Gerd Haeussler <gerd.haeussler.ext@siemens.com>
11  */
12 
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/platform_data/x86/simatic-ipc-base.h>
19 #include <linux/platform_device.h>
20 #include <linux/sizes.h>
21 #include <linux/spinlock.h>
22 
23 #define SIMATIC_IPC_LED_PORT_BASE	0x404E
24 
25 struct simatic_ipc_led {
26 	unsigned int value; /* mask for io and offset for mem */
27 	char *name;
28 	struct led_classdev cdev;
29 };
30 
31 static struct simatic_ipc_led simatic_ipc_leds_io[] = {
32 	{1 << 15, "green:" LED_FUNCTION_STATUS "-1" },
33 	{1 << 7,  "yellow:" LED_FUNCTION_STATUS "-1" },
34 	{1 << 14, "red:" LED_FUNCTION_STATUS "-2" },
35 	{1 << 6,  "yellow:" LED_FUNCTION_STATUS "-2" },
36 	{1 << 13, "red:" LED_FUNCTION_STATUS "-3" },
37 	{1 << 5,  "yellow:" LED_FUNCTION_STATUS "-3" },
38 	{ }
39 };
40 
41 /* the actual start will be discovered with PCI, 0 is a placeholder */
42 static struct resource simatic_ipc_led_mem_res = DEFINE_RES_MEM_NAMED(0, SZ_4K, KBUILD_MODNAME);
43 
44 static void __iomem *simatic_ipc_led_memory;
45 
46 static struct simatic_ipc_led simatic_ipc_leds_mem[] = {
47 	{0x500 + 0x1A0, "red:" LED_FUNCTION_STATUS "-1"},
48 	{0x500 + 0x1A8, "green:" LED_FUNCTION_STATUS "-1"},
49 	{0x500 + 0x1C8, "red:" LED_FUNCTION_STATUS "-2"},
50 	{0x500 + 0x1D0, "green:" LED_FUNCTION_STATUS "-2"},
51 	{0x500 + 0x1E0, "red:" LED_FUNCTION_STATUS "-3"},
52 	{0x500 + 0x198, "green:" LED_FUNCTION_STATUS "-3"},
53 	{ }
54 };
55 
56 static struct resource simatic_ipc_led_io_res =
57 	DEFINE_RES_IO_NAMED(SIMATIC_IPC_LED_PORT_BASE, SZ_2, KBUILD_MODNAME);
58 
59 static DEFINE_SPINLOCK(reg_lock);
60 
61 static inline struct simatic_ipc_led *cdev_to_led(struct led_classdev *led_cd)
62 {
63 	return container_of(led_cd, struct simatic_ipc_led, cdev);
64 }
65 
66 static void simatic_ipc_led_set_io(struct led_classdev *led_cd,
67 				   enum led_brightness brightness)
68 {
69 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
70 	unsigned long flags;
71 	unsigned int val;
72 
73 	spin_lock_irqsave(&reg_lock, flags);
74 
75 	val = inw(SIMATIC_IPC_LED_PORT_BASE);
76 	if (brightness == LED_OFF)
77 		outw(val | led->value, SIMATIC_IPC_LED_PORT_BASE);
78 	else
79 		outw(val & ~led->value, SIMATIC_IPC_LED_PORT_BASE);
80 
81 	spin_unlock_irqrestore(&reg_lock, flags);
82 }
83 
84 static enum led_brightness simatic_ipc_led_get_io(struct led_classdev *led_cd)
85 {
86 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
87 
88 	return inw(SIMATIC_IPC_LED_PORT_BASE) & led->value ? LED_OFF : led_cd->max_brightness;
89 }
90 
91 static void simatic_ipc_led_set_mem(struct led_classdev *led_cd,
92 				    enum led_brightness brightness)
93 {
94 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
95 	void __iomem *reg = simatic_ipc_led_memory + led->value;
96 	u32 val;
97 
98 	val = readl(reg);
99 	val = (val & ~1) | (brightness == LED_OFF);
100 	writel(val, reg);
101 }
102 
103 static enum led_brightness simatic_ipc_led_get_mem(struct led_classdev *led_cd)
104 {
105 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
106 	void __iomem *reg = simatic_ipc_led_memory + led->value;
107 	u32 val;
108 
109 	val = readl(reg);
110 	return (val & 1) ? LED_OFF : led_cd->max_brightness;
111 }
112 
113 static int simatic_ipc_leds_probe(struct platform_device *pdev)
114 {
115 	const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
116 	struct device *dev = &pdev->dev;
117 	struct simatic_ipc_led *ipcled;
118 	struct led_classdev *cdev;
119 	struct resource *res;
120 	void __iomem *reg;
121 	int err, type;
122 	u32 val;
123 
124 	switch (plat->devmode) {
125 	case SIMATIC_IPC_DEVICE_227D:
126 	case SIMATIC_IPC_DEVICE_427E:
127 		res = &simatic_ipc_led_io_res;
128 		ipcled = simatic_ipc_leds_io;
129 		/* on 227D the two bytes work the other way araound */
130 		if (plat->devmode == SIMATIC_IPC_DEVICE_227D) {
131 			while (ipcled->value) {
132 				ipcled->value = swab16(ipcled->value);
133 				ipcled++;
134 			}
135 			ipcled = simatic_ipc_leds_io;
136 		}
137 		type = IORESOURCE_IO;
138 		if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
139 			dev_err(dev, "Unable to register IO resource at %pR\n", res);
140 			return -EBUSY;
141 		}
142 		break;
143 	case SIMATIC_IPC_DEVICE_127E:
144 		res = &simatic_ipc_led_mem_res;
145 		ipcled = simatic_ipc_leds_mem;
146 		type = IORESOURCE_MEM;
147 
148 		/* get GPIO base from PCI */
149 		res->start = simatic_ipc_get_membase0(PCI_DEVFN(13, 0));
150 		if (res->start == 0)
151 			return -ENODEV;
152 
153 		/* do the final address calculation */
154 		res->start = res->start + (0xC5 << 16);
155 		res->end += res->start;
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;
170 	default:
171 		return -ENODEV;
172 	}
173 
174 	while (ipcled->value) {
175 		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 		}
183 		cdev->max_brightness = LED_ON;
184 		cdev->name = ipcled->name;
185 
186 		err = devm_led_classdev_register(dev, cdev);
187 		if (err < 0)
188 			return err;
189 		ipcled++;
190 	}
191 
192 	return 0;
193 }
194 
195 static struct platform_driver simatic_ipc_led_driver = {
196 	.probe = simatic_ipc_leds_probe,
197 	.driver = {
198 		.name = KBUILD_MODNAME,
199 	}
200 };
201 
202 module_platform_driver(simatic_ipc_led_driver);
203 
204 MODULE_LICENSE("GPL v2");
205 MODULE_ALIAS("platform:" KBUILD_MODNAME);
206 MODULE_AUTHOR("Henning Schild <henning.schild@siemens.com>");
207