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 struct resource simatic_ipc_led_mem_res = DEFINE_RES_MEM_NAMED(0, SZ_4K, KBUILD_MODNAME);
43 
44 static void *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 
96 	u32 *p;
97 
98 	p = simatic_ipc_led_memory + led->value;
99 	*p = (*p & ~1) | (brightness == LED_OFF);
100 }
101 
102 static enum led_brightness simatic_ipc_led_get_mem(struct led_classdev *led_cd)
103 {
104 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
105 
106 	u32 *p;
107 
108 	p = simatic_ipc_led_memory + led->value;
109 	return (*p & 1) ? LED_OFF : led_cd->max_brightness;
110 }
111 
112 static int simatic_ipc_leds_probe(struct platform_device *pdev)
113 {
114 	const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
115 	struct device *dev = &pdev->dev;
116 	struct simatic_ipc_led *ipcled;
117 	struct led_classdev *cdev;
118 	struct resource *res;
119 	int err, type;
120 	u32 *p;
121 
122 	switch (plat->devmode) {
123 	case SIMATIC_IPC_DEVICE_227D:
124 	case SIMATIC_IPC_DEVICE_427E:
125 		res = &simatic_ipc_led_io_res;
126 		ipcled = simatic_ipc_leds_io;
127 		/* on 227D the two bytes work the other way araound */
128 		if (plat->devmode == SIMATIC_IPC_DEVICE_227D) {
129 			while (ipcled->value) {
130 				ipcled->value = swab16(ipcled->value);
131 				ipcled++;
132 			}
133 			ipcled = simatic_ipc_leds_io;
134 		}
135 		type = IORESOURCE_IO;
136 		if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
137 			dev_err(dev, "Unable to register IO resource at %pR\n", res);
138 			return -EBUSY;
139 		}
140 		break;
141 	case SIMATIC_IPC_DEVICE_127E:
142 		res = &simatic_ipc_led_mem_res;
143 		ipcled = simatic_ipc_leds_mem;
144 		type = IORESOURCE_MEM;
145 
146 		/* get GPIO base from PCI */
147 		res->start = simatic_ipc_get_membase0(PCI_DEVFN(13, 0));
148 		if (res->start == 0)
149 			return -ENODEV;
150 
151 		/* do the final address calculation */
152 		res->start = res->start + (0xC5 << 16);
153 		res->end += res->start;
154 
155 		simatic_ipc_led_memory = devm_ioremap_resource(dev, res);
156 		if (IS_ERR(simatic_ipc_led_memory))
157 			return PTR_ERR(simatic_ipc_led_memory);
158 
159 		/* initialize power/watchdog LED */
160 		p = simatic_ipc_led_memory + 0x500 + 0x1D8; /* PM_WDT_OUT */
161 		*p = (*p & ~1);
162 		p = simatic_ipc_led_memory + 0x500 + 0x1C0; /* PM_BIOS_BOOT_N */
163 		*p = (*p | 1);
164 
165 		break;
166 	default:
167 		return -ENODEV;
168 	}
169 
170 	while (ipcled->value) {
171 		cdev = &ipcled->cdev;
172 		if (type == IORESOURCE_MEM) {
173 			cdev->brightness_set = simatic_ipc_led_set_mem;
174 			cdev->brightness_get = simatic_ipc_led_get_mem;
175 		} else {
176 			cdev->brightness_set = simatic_ipc_led_set_io;
177 			cdev->brightness_get = simatic_ipc_led_get_io;
178 		}
179 		cdev->max_brightness = LED_ON;
180 		cdev->name = ipcled->name;
181 
182 		err = devm_led_classdev_register(dev, cdev);
183 		if (err < 0)
184 			return err;
185 		ipcled++;
186 	}
187 
188 	return 0;
189 }
190 
191 static struct platform_driver simatic_ipc_led_driver = {
192 	.probe = simatic_ipc_leds_probe,
193 	.driver = {
194 		.name = KBUILD_MODNAME,
195 	}
196 };
197 
198 module_platform_driver(simatic_ipc_led_driver);
199 
200 MODULE_LICENSE("GPL v2");
201 MODULE_ALIAS("platform:" KBUILD_MODNAME);
202 MODULE_AUTHOR("Henning Schild <henning.schild@siemens.com>");
203