xref: /linux/drivers/leds/simple/simatic-ipc-leds-gpio-core.c (revision afeea2758b4f1210361ce2a91d8fa3e7df606ad2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Siemens SIMATIC IPC driver for GPIO based LEDs
4  *
5  * Copyright (c) Siemens AG, 2023
6  *
7  * Author:
8  *  Henning Schild <henning.schild@siemens.com>
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 #include <linux/platform_data/x86/simatic-ipc-base.h>
17 
18 #include "simatic-ipc-leds-gpio.h"
19 
20 static struct platform_device *simatic_leds_pdev;
21 
22 static const struct gpio_led simatic_ipc_gpio_leds[] = {
23 	{ .name = "red:" LED_FUNCTION_STATUS "-1" },
24 	{ .name = "green:" LED_FUNCTION_STATUS "-1" },
25 	{ .name = "red:" LED_FUNCTION_STATUS "-2" },
26 	{ .name = "green:" LED_FUNCTION_STATUS "-2" },
27 	{ .name = "red:" LED_FUNCTION_STATUS "-3" },
28 	{ .name = "green:" LED_FUNCTION_STATUS "-3" },
29 };
30 
31 static const struct gpio_led_platform_data simatic_ipc_gpio_leds_pdata = {
32 	.num_leds	= ARRAY_SIZE(simatic_ipc_gpio_leds),
33 	.leds		= simatic_ipc_gpio_leds,
34 };
35 
36 void simatic_ipc_leds_gpio_remove(struct platform_device *pdev,
37 				 struct gpiod_lookup_table *table,
38 				 struct gpiod_lookup_table *table_extra)
39 {
40 	gpiod_remove_lookup_table(table);
41 	gpiod_remove_lookup_table(table_extra);
42 	platform_device_unregister(simatic_leds_pdev);
43 }
44 EXPORT_SYMBOL_GPL(simatic_ipc_leds_gpio_remove);
45 
46 int simatic_ipc_leds_gpio_probe(struct platform_device *pdev,
47 				struct gpiod_lookup_table *table,
48 				struct gpiod_lookup_table *table_extra)
49 {
50 	const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
51 	struct device *dev = &pdev->dev;
52 	struct gpio_desc *gpiod;
53 	int err;
54 
55 	switch (plat->devmode) {
56 	case SIMATIC_IPC_DEVICE_127E:
57 	case SIMATIC_IPC_DEVICE_227G:
58 	case SIMATIC_IPC_DEVICE_BX_21A:
59 	case SIMATIC_IPC_DEVICE_BX_59A:
60 		break;
61 	default:
62 		return -ENODEV;
63 	}
64 
65 	gpiod_add_lookup_table(table);
66 	simatic_leds_pdev = platform_device_register_resndata(NULL,
67 		"leds-gpio", PLATFORM_DEVID_NONE, NULL, 0,
68 		&simatic_ipc_gpio_leds_pdata,
69 		sizeof(simatic_ipc_gpio_leds_pdata));
70 	if (IS_ERR(simatic_leds_pdev)) {
71 		err = PTR_ERR(simatic_leds_pdev);
72 		goto out;
73 	}
74 
75 	if (!table_extra)
76 		return 0;
77 
78 	table_extra->dev_id = dev_name(dev);
79 	gpiod_add_lookup_table(table_extra);
80 
81 	/* PM_BIOS_BOOT_N */
82 	gpiod = gpiod_get_index(dev, NULL, 6, GPIOD_OUT_LOW);
83 	if (IS_ERR(gpiod)) {
84 		err = PTR_ERR(gpiod);
85 		goto out;
86 	}
87 	gpiod_put(gpiod);
88 
89 	/* PM_WDT_OUT */
90 	gpiod = gpiod_get_index(dev, NULL, 7, GPIOD_OUT_LOW);
91 	if (IS_ERR(gpiod)) {
92 		err = PTR_ERR(gpiod);
93 		goto out;
94 	}
95 	gpiod_put(gpiod);
96 
97 	return 0;
98 out:
99 	simatic_ipc_leds_gpio_remove(pdev, table, table_extra);
100 
101 	return err;
102 }
103 EXPORT_SYMBOL_GPL(simatic_ipc_leds_gpio_probe);
104 
105 MODULE_LICENSE("GPL v2");
106 MODULE_SOFTDEP("pre: platform:leds-gpio");
107 MODULE_AUTHOR("Henning Schild <henning.schild@siemens.com>");
108