GNU Linux-libre 6.1.24-gnu
[releases.git] / drivers / leds / simple / simatic-ipc-leds-gpio.c
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 <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 static struct gpiod_lookup_table *simatic_ipc_led_gpio_table;
19
20 static struct gpiod_lookup_table simatic_ipc_led_gpio_table_127e = {
21         .dev_id = "leds-gpio",
22         .table = {
23                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 52, NULL, 0, GPIO_ACTIVE_LOW),
24                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 53, NULL, 1, GPIO_ACTIVE_LOW),
25                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 57, NULL, 2, GPIO_ACTIVE_LOW),
26                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 58, NULL, 3, GPIO_ACTIVE_LOW),
27                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 60, NULL, 4, GPIO_ACTIVE_LOW),
28                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 51, NULL, 5, GPIO_ACTIVE_LOW),
29                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 56, NULL, 6, GPIO_ACTIVE_LOW),
30                 GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 59, NULL, 7, GPIO_ACTIVE_HIGH),
31         },
32 };
33
34 static struct gpiod_lookup_table simatic_ipc_led_gpio_table_227g = {
35         .dev_id = "leds-gpio",
36         .table = {
37                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 0, NULL, 0, GPIO_ACTIVE_LOW),
38                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 1, NULL, 1, GPIO_ACTIVE_LOW),
39                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 2, NULL, 2, GPIO_ACTIVE_LOW),
40                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 3, NULL, 3, GPIO_ACTIVE_LOW),
41                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 4, NULL, 4, GPIO_ACTIVE_LOW),
42                 GPIO_LOOKUP_IDX("gpio-f7188x-2", 5, NULL, 5, GPIO_ACTIVE_LOW),
43                 GPIO_LOOKUP_IDX("gpio-f7188x-3", 6, NULL, 6, GPIO_ACTIVE_HIGH),
44                 GPIO_LOOKUP_IDX("gpio-f7188x-3", 7, NULL, 7, GPIO_ACTIVE_HIGH),
45         }
46 };
47
48 static const struct gpio_led simatic_ipc_gpio_leds[] = {
49         { .name = "red:" LED_FUNCTION_STATUS "-1" },
50         { .name = "green:" LED_FUNCTION_STATUS "-1" },
51         { .name = "red:" LED_FUNCTION_STATUS "-2" },
52         { .name = "green:" LED_FUNCTION_STATUS "-2" },
53         { .name = "red:" LED_FUNCTION_STATUS "-3" },
54         { .name = "green:" LED_FUNCTION_STATUS "-3" },
55 };
56
57 static const struct gpio_led_platform_data simatic_ipc_gpio_leds_pdata = {
58         .num_leds       = ARRAY_SIZE(simatic_ipc_gpio_leds),
59         .leds           = simatic_ipc_gpio_leds,
60 };
61
62 static struct platform_device *simatic_leds_pdev;
63
64 static int simatic_ipc_leds_gpio_remove(struct platform_device *pdev)
65 {
66         gpiod_remove_lookup_table(simatic_ipc_led_gpio_table);
67         platform_device_unregister(simatic_leds_pdev);
68
69         return 0;
70 }
71
72 static int simatic_ipc_leds_gpio_probe(struct platform_device *pdev)
73 {
74         const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
75         struct gpio_desc *gpiod;
76         int err;
77
78         switch (plat->devmode) {
79         case SIMATIC_IPC_DEVICE_127E:
80                 if (!IS_ENABLED(CONFIG_PINCTRL_BROXTON))
81                         return -ENODEV;
82                 simatic_ipc_led_gpio_table = &simatic_ipc_led_gpio_table_127e;
83                 break;
84         case SIMATIC_IPC_DEVICE_227G:
85                 if (!IS_ENABLED(CONFIG_GPIO_F7188X))
86                         return -ENODEV;
87                 request_module("gpio-f7188x");
88                 simatic_ipc_led_gpio_table = &simatic_ipc_led_gpio_table_227g;
89                 break;
90         default:
91                 return -ENODEV;
92         }
93
94         gpiod_add_lookup_table(simatic_ipc_led_gpio_table);
95         simatic_leds_pdev = platform_device_register_resndata(NULL,
96                 "leds-gpio", PLATFORM_DEVID_NONE, NULL, 0,
97                 &simatic_ipc_gpio_leds_pdata,
98                 sizeof(simatic_ipc_gpio_leds_pdata));
99         if (IS_ERR(simatic_leds_pdev)) {
100                 err = PTR_ERR(simatic_leds_pdev);
101                 goto out;
102         }
103
104         /* PM_BIOS_BOOT_N */
105         gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 6, GPIOD_OUT_LOW);
106         if (IS_ERR(gpiod)) {
107                 err = PTR_ERR(gpiod);
108                 goto out;
109         }
110         gpiod_put(gpiod);
111
112         /* PM_WDT_OUT */
113         gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 7, GPIOD_OUT_LOW);
114         if (IS_ERR(gpiod)) {
115                 err = PTR_ERR(gpiod);
116                 goto out;
117         }
118         gpiod_put(gpiod);
119
120         return 0;
121 out:
122         simatic_ipc_leds_gpio_remove(pdev);
123
124         return err;
125 }
126
127 static struct platform_driver simatic_ipc_led_gpio_driver = {
128         .probe = simatic_ipc_leds_gpio_probe,
129         .remove = simatic_ipc_leds_gpio_remove,
130         .driver = {
131                 .name = KBUILD_MODNAME,
132         }
133 };
134 module_platform_driver(simatic_ipc_led_gpio_driver);
135
136 MODULE_LICENSE("GPL v2");
137 MODULE_ALIAS("platform:" KBUILD_MODNAME);
138 MODULE_SOFTDEP("pre: platform:leds-gpio");
139 MODULE_AUTHOR("Henning Schild <henning.schild@siemens.com>");