xref: /openbmc/linux/drivers/leds/simple/simatic-ipc-leds.c (revision 759273c3c429ffbd8f24655fd0480cd408a1804c)
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/p2sb.h>
19 #include <linux/platform_data/x86/simatic-ipc-base.h>
20 #include <linux/platform_device.h>
21 #include <linux/sizes.h>
22 #include <linux/spinlock.h>
23 
24 #define SIMATIC_IPC_LED_PORT_BASE	0x404E
25 
26 struct simatic_ipc_led {
27 	unsigned int value; /* mask for io and offset for mem */
28 	char *name;
29 	struct led_classdev cdev;
30 };
31 
32 static struct simatic_ipc_led simatic_ipc_leds_io[] = {
33 	{1 << 15, "green:" LED_FUNCTION_STATUS "-1" },
34 	{1 << 7,  "yellow:" LED_FUNCTION_STATUS "-1" },
35 	{1 << 14, "red:" LED_FUNCTION_STATUS "-2" },
36 	{1 << 6,  "yellow:" LED_FUNCTION_STATUS "-2" },
37 	{1 << 13, "red:" LED_FUNCTION_STATUS "-3" },
38 	{1 << 5,  "yellow:" LED_FUNCTION_STATUS "-3" },
39 	{ }
40 };
41 
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 
57 static struct resource simatic_ipc_led_io_res =
58 	DEFINE_RES_IO_NAMED(SIMATIC_IPC_LED_PORT_BASE, SZ_2, KBUILD_MODNAME);
59 
60 static DEFINE_SPINLOCK(reg_lock);
61 
62 static inline struct simatic_ipc_led *cdev_to_led(struct led_classdev *led_cd)
63 {
64 	return container_of(led_cd, struct simatic_ipc_led, cdev);
65 }
66 
67 static void simatic_ipc_led_set_io(struct led_classdev *led_cd,
68 				   enum led_brightness brightness)
69 {
70 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
71 	unsigned long flags;
72 	unsigned int val;
73 
74 	spin_lock_irqsave(&reg_lock, flags);
75 
76 	val = inw(SIMATIC_IPC_LED_PORT_BASE);
77 	if (brightness == LED_OFF)
78 		outw(val | led->value, SIMATIC_IPC_LED_PORT_BASE);
79 	else
80 		outw(val & ~led->value, SIMATIC_IPC_LED_PORT_BASE);
81 
82 	spin_unlock_irqrestore(&reg_lock, flags);
83 }
84 
85 static enum led_brightness simatic_ipc_led_get_io(struct led_classdev *led_cd)
86 {
87 	struct simatic_ipc_led *led = cdev_to_led(led_cd);
88 
89 	return inw(SIMATIC_IPC_LED_PORT_BASE) & led->value ? LED_OFF : led_cd->max_brightness;
90 }
91 
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 
114 static int simatic_ipc_leds_probe(struct platform_device *pdev)
115 {
116 	const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
117 	struct device *dev = &pdev->dev;
118 	struct simatic_ipc_led *ipcled;
119 	struct led_classdev *cdev;
120 	struct resource *res;
121 	void __iomem *reg;
122 	int err, type;
123 	u32 val;
124 
125 	switch (plat->devmode) {
126 	case SIMATIC_IPC_DEVICE_227D:
127 	case SIMATIC_IPC_DEVICE_427E:
128 		res = &simatic_ipc_led_io_res;
129 		ipcled = simatic_ipc_leds_io;
130 		/* on 227D the two bytes work the other way araound */
131 		if (plat->devmode == SIMATIC_IPC_DEVICE_227D) {
132 			while (ipcled->value) {
133 				ipcled->value = swab16(ipcled->value);
134 				ipcled++;
135 			}
136 			ipcled = simatic_ipc_leds_io;
137 		}
138 		type = IORESOURCE_IO;
139 		if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
140 			dev_err(dev, "Unable to register IO resource at %pR\n", res);
141 			return -EBUSY;
142 		}
143 		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;
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