a3fc731c471967c0731e05d3c5b751e40de15959
[openwrt/staging/stintel.git] / target / linux / mvebu / patches-5.15 / 904-drivers-leds-Add-the-IEI-WT61P803-PUZZLE-LED-driver.patch
1 From f3b44eb69cc561cf05d00506dcec0dd9be003ed8 Mon Sep 17 00:00:00 2001
2 From: Luka Kovacic <luka.kovacic () sartura ! hr>
3 Date: Tue, 24 Aug 2021 12:44:35 +0000
4 Subject: [PATCH 4/7] drivers: leds: Add the IEI WT61P803 PUZZLE LED driver
5
6 Add support for the IEI WT61P803 PUZZLE LED driver.
7 Currently only the front panel power LED is supported,
8 since it is the only LED on this board wired through the
9 MCU.
10
11 The LED is wired directly to the on-board MCU controller
12 and is toggled using an MCU command.
13
14 Support for more LEDs is going to be added in case more
15 boards implement this microcontroller, as LEDs use many
16 different GPIOs.
17
18 This driver depends on the IEI WT61P803 PUZZLE MFD driver.
19
20 Signed-off-by: Luka Kovacic <luka.kovacic@sartura.hr>
21 Signed-off-by: Pavo Banicevic <pavo.banicevic@sartura.hr>
22 Cc: Luka Perkov <luka.perkov@sartura.hr>
23 Cc: Robert Marko <robert.marko@sartura.hr>
24 ---
25 drivers/leds/Kconfig | 8 ++
26 drivers/leds/Makefile | 1 +
27 drivers/leds/leds-iei-wt61p803-puzzle.c | 147 ++++++++++++++++++++++++
28 3 files changed, 156 insertions(+)
29 create mode 100644 drivers/leds/leds-iei-wt61p803-puzzle.c
30
31 --- a/drivers/leds/Kconfig
32 +++ b/drivers/leds/Kconfig
33 @@ -306,6 +306,14 @@ config LEDS_IPAQ_MICRO
34 Choose this option if you want to use the notification LED on
35 Compaq/HP iPAQ h3100 and h3600.
36
37 +config LEDS_IEI_WT61P803_PUZZLE
38 + tristate "LED Support for the IEI WT61P803 PUZZLE MCU"
39 + depends on LEDS_CLASS
40 + depends on MFD_IEI_WT61P803_PUZZLE
41 + help
42 + This option enables support for LEDs controlled by the IEI WT61P803
43 + M801 MCU.
44 +
45 config LEDS_HP6XX
46 tristate "LED Support for the HP Jornada 6xx"
47 depends on LEDS_CLASS
48 --- a/drivers/leds/Makefile
49 +++ b/drivers/leds/Makefile
50 @@ -33,6 +33,7 @@ obj-$(CONFIG_LEDS_HP6XX) += leds-hp6xx.
51 obj-$(CONFIG_LEDS_INTEL_SS4200) += leds-ss4200.o
52 obj-$(CONFIG_LEDS_IP30) += leds-ip30.o
53 obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o
54 +obj-$(CONFIG_LEDS_IEI_WT61P803_PUZZLE) += leds-iei-wt61p803-puzzle.o
55 obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o
56 obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o
57 obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o
58 --- /dev/null
59 +++ b/drivers/leds/leds-iei-wt61p803-puzzle.c
60 @@ -0,0 +1,147 @@
61 +// SPDX-License-Identifier: GPL-2.0-only
62 +/* IEI WT61P803 PUZZLE MCU LED Driver
63 + *
64 + * Copyright (C) 2020 Sartura Ltd.
65 + * Author: Luka Kovacic <luka.kovacic@sartura.hr>
66 + */
67 +
68 +#include <linux/leds.h>
69 +#include <linux/mfd/iei-wt61p803-puzzle.h>
70 +#include <linux/mod_devicetable.h>
71 +#include <linux/module.h>
72 +#include <linux/platform_device.h>
73 +#include <linux/property.h>
74 +#include <linux/slab.h>
75 +
76 +enum iei_wt61p803_puzzle_led_state {
77 + IEI_LED_OFF = 0x30,
78 + IEI_LED_ON = 0x31,
79 + IEI_LED_BLINK_5HZ = 0x32,
80 + IEI_LED_BLINK_1HZ = 0x33,
81 +};
82 +
83 +/**
84 + * struct iei_wt61p803_puzzle_led - MCU LED Driver
85 + * @cdev: LED classdev
86 + * @mcu: MCU struct pointer
87 + * @response_buffer Global MCU response buffer
88 + * @lock: General mutex lock to protect simultaneous R/W access to led_power_state
89 + * @led_power_state: State of the front panel power LED
90 + */
91 +struct iei_wt61p803_puzzle_led {
92 + struct led_classdev cdev;
93 + struct iei_wt61p803_puzzle *mcu;
94 + unsigned char response_buffer[IEI_WT61P803_PUZZLE_BUF_SIZE];
95 + struct mutex lock; /* mutex to protect led_power_state */
96 + int led_power_state;
97 +};
98 +
99 +static inline struct iei_wt61p803_puzzle_led *cdev_to_iei_wt61p803_puzzle_led
100 + (struct led_classdev *led_cdev)
101 +{
102 + return container_of(led_cdev, struct iei_wt61p803_puzzle_led, cdev);
103 +}
104 +
105 +static int iei_wt61p803_puzzle_led_brightness_set_blocking(struct led_classdev *cdev,
106 + enum led_brightness brightness)
107 +{
108 + struct iei_wt61p803_puzzle_led *priv = cdev_to_iei_wt61p803_puzzle_led(cdev);
109 + unsigned char *resp_buf = priv->response_buffer;
110 + unsigned char led_power_cmd[5] = {};
111 + size_t reply_size;
112 + int ret;
113 +
114 + led_power_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
115 + led_power_cmd[1] = IEI_WT61P803_PUZZLE_CMD_LED;
116 + led_power_cmd[2] = IEI_WT61P803_PUZZLE_CMD_LED_POWER;
117 + led_power_cmd[3] = brightness == LED_OFF ? IEI_LED_OFF : IEI_LED_ON;
118 +
119 + ret = iei_wt61p803_puzzle_write_command(priv->mcu, led_power_cmd,
120 + sizeof(led_power_cmd),
121 + resp_buf,
122 + &reply_size);
123 + if (ret)
124 + return ret;
125 +
126 + if (reply_size != 3)
127 + return -EIO;
128 +
129 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
130 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
131 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK))
132 + return -EIO;
133 +
134 + mutex_lock(&priv->lock);
135 + priv->led_power_state = brightness;
136 + mutex_unlock(&priv->lock);
137 +
138 + return 0;
139 +}
140 +
141 +static enum led_brightness iei_wt61p803_puzzle_led_brightness_get(struct led_classdev *cdev)
142 +{
143 + struct iei_wt61p803_puzzle_led *priv = cdev_to_iei_wt61p803_puzzle_led(cdev);
144 + int led_state;
145 +
146 + mutex_lock(&priv->lock);
147 + led_state = priv->led_power_state;
148 + mutex_unlock(&priv->lock);
149 +
150 + return led_state;
151 +}
152 +
153 +static int iei_wt61p803_puzzle_led_probe(struct platform_device *pdev)
154 +{
155 + struct device *dev = &pdev->dev;
156 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev->parent);
157 + struct iei_wt61p803_puzzle_led *priv;
158 + struct led_init_data init_data = {};
159 + struct fwnode_handle *child;
160 + int ret;
161 +
162 + if (device_get_child_node_count(dev) != 1)
163 + return -EINVAL;
164 +
165 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
166 + if (!priv)
167 + return -ENOMEM;
168 +
169 + priv->mcu = mcu;
170 + priv->led_power_state = 1;
171 + mutex_init(&priv->lock);
172 + dev_set_drvdata(dev, priv);
173 +
174 + child = device_get_next_child_node(dev, NULL);
175 + init_data.fwnode = child;
176 +
177 + priv->cdev.brightness_set_blocking = iei_wt61p803_puzzle_led_brightness_set_blocking;
178 + priv->cdev.brightness_get = iei_wt61p803_puzzle_led_brightness_get;
179 + priv->cdev.max_brightness = 1;
180 +
181 + ret = devm_led_classdev_register_ext(dev, &priv->cdev, &init_data);
182 + if (ret)
183 + dev_err(dev, "Could not register LED\n");
184 +
185 + fwnode_handle_put(child);
186 + return ret;
187 +}
188 +
189 +static const struct of_device_id iei_wt61p803_puzzle_led_of_match[] = {
190 + { .compatible = "iei,wt61p803-puzzle-leds" },
191 + { }
192 +};
193 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_led_of_match);
194 +
195 +static struct platform_driver iei_wt61p803_puzzle_led_driver = {
196 + .driver = {
197 + .name = "iei-wt61p803-puzzle-led",
198 + .of_match_table = iei_wt61p803_puzzle_led_of_match,
199 + },
200 + .probe = iei_wt61p803_puzzle_led_probe,
201 +};
202 +module_platform_driver(iei_wt61p803_puzzle_led_driver);
203 +
204 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE front panel LED driver");
205 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
206 +MODULE_LICENSE("GPL v2");
207 +MODULE_ALIAS("platform:leds-iei-wt61p803-puzzle");