d759453cd9ff67746220a72dca9e738841cf84bf
[openwrt/staging/pepe2k.git] / package / kernel / leds-gca230718 / src / leds-gca230718.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * GCA230718 LED support (e.g. for D-Link M30) using I2C
4 *
5 * Copyright 2022 Roland Reinl <reinlroland+github@gmail.com>
6 *
7 * This driver can control RGBW LEDs which are connected to a GCA230718.
8 */
9
10 #include <linux/delay.h>
11 #include <linux/led-class-multicolor.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/property.h>
16 #include <linux/i2c.h>
17 #include <linux/mutex.h>
18 #include <linux/version.h>
19
20 #define GCA230718_MAX_LEDS (4u)
21
22 #define GCA230718_OPMODE_DISABLED (0x00u)
23 #define GCA230718_OPMODE_NO_TOGGLE (0x01u)
24 #define GCA230718_OPMODE_TOGGLE_RAMP_CONTROL_DISABLED (0x02u)
25 #define GCA230718_OPMODE_TOGGLE_RAMP_CONTROL_ENSABLED (0x03u)
26
27 #define GCA230718_1ST_SEQUENCE_BYTE_1 (0x02u)
28 #define GCA230718_2ND_SEQUENCE_BYTE_1 (0x01u)
29 #define GCA230718_3RD_SEQUENCE_BYTE_1 (0x03u)
30
31 struct gca230718_led
32 {
33 enum led_brightness brightness;
34 struct i2c_client *client;
35 struct led_classdev ledClassDev;
36 };
37
38 struct gca230718_private
39 {
40 struct mutex lock;
41 struct gca230718_led leds[GCA230718_MAX_LEDS];
42 };
43
44 static void gca230718_init_private_led_data(struct gca230718_private* data)
45 {
46 u8 ledIndex;
47 for (ledIndex = 0; ledIndex < GCA230718_MAX_LEDS; ledIndex++)
48 {
49 data->leds[ledIndex].client = NULL;
50 }
51 }
52
53 static void gca230718_send_sequence(struct i2c_client *client, u8 byte0, struct gca230718_private* gca230718_privateData)
54 {
55 int status = 0;
56 u8 ledIndex;
57 const u8 resetCommand[2] = { 0x81, 0xE4 };
58 const u8 resetCommandRegister = 0x00;
59
60 u8 controlCommand[13];
61 const u8 controlCommandRegister = 0x03;
62
63 controlCommand[0] = 0x0C; /* Unknown */
64 controlCommand[1] = byte0;
65 controlCommand[2] = GCA230718_OPMODE_NO_TOGGLE;
66 /* Byte 3-6 are set below to the brighness value of the individual LEDs */
67 controlCommand[7] = 0x01; /* Frequency, doesn't care as long as GCA230718_OPMODE_NO_TOGGLE is used above */
68 /* Byte 8-11 are set below to the brighness value of the individual LEDs */
69 controlCommand[12] = 0x87;
70
71 for (ledIndex = 0; ledIndex < GCA230718_MAX_LEDS; ledIndex++)
72 {
73 controlCommand[3 + ledIndex] = gca230718_privateData->leds[ledIndex].brightness;
74 controlCommand[8 + ledIndex] = gca230718_privateData->leds[ledIndex].brightness;
75 }
76
77 mutex_lock(&(gca230718_privateData->lock));
78
79 if ((status = i2c_smbus_write_i2c_block_data(client, resetCommandRegister, sizeof(resetCommand), resetCommand)) != 0)
80 {
81 pr_info("Error %i during call of i2c_smbus_write_i2c_block_data for reset command\n", status);
82 }
83 else if ((status = i2c_smbus_write_i2c_block_data(client, controlCommandRegister, sizeof(controlCommand), controlCommand)) != 0)
84 {
85 pr_info("Error %i during call of i2c_smbus_write_i2c_block_data for control command\n", status);
86 }
87
88 mutex_unlock(&(gca230718_privateData->lock));
89 }
90
91 static int gca230718_set_brightness(struct led_classdev *led_cdev, enum led_brightness value)
92 {
93 struct gca230718_led* led;
94 struct i2c_client* client;
95
96 led = container_of(led_cdev, struct gca230718_led, ledClassDev);
97 client = led->client;
98
99 if (client != NULL)
100 {
101 struct gca230718_private* gca230718_privateData;
102
103 led->brightness = value;
104 gca230718_privateData = i2c_get_clientdata(client);
105
106 gca230718_send_sequence(client, GCA230718_2ND_SEQUENCE_BYTE_1, gca230718_privateData);
107 }
108
109 return 0;
110 }
111
112 static int gca230718_probe(struct i2c_client *client, const struct i2c_device_id *id)
113 {
114 int status = 0;
115 struct gca230718_private* gca230718_privateData;
116
117 pr_info("Enter gca230718_probe for device address %u\n", client->addr);
118 gca230718_privateData = devm_kzalloc(&(client->dev), sizeof(struct gca230718_private), GFP_KERNEL);
119
120 if (gca230718_privateData == NULL)
121 {
122 pr_info("Error during allocating memory for private data\n");
123 status = -ENOMEM;
124 }
125 else
126 {
127 struct device_node* ledNode;
128 mutex_init(&gca230718_privateData->lock);
129 gca230718_init_private_led_data(gca230718_privateData);
130 i2c_set_clientdata(client, gca230718_privateData);
131
132 for_each_child_of_node(client->dev.of_node, ledNode)
133 {
134 u32 regValue = 0;
135 if (of_property_read_u32(ledNode, "reg", &regValue) != 0)
136 {
137 pr_info("Missing entry \"reg\" in node %s\n", ledNode->name);
138 }
139 else if (regValue >= GCA230718_MAX_LEDS)
140 {
141 pr_info("Invalid entry \"reg\" in node %s (%u)\n", ledNode->name, regValue);
142 }
143 else
144 {
145 struct led_classdev* ledClassDev = &(gca230718_privateData->leds[regValue].ledClassDev);
146 struct led_init_data init_data = {};
147
148 gca230718_privateData->leds[regValue].client = client;
149 init_data.fwnode = of_fwnode_handle(ledNode);
150
151 pr_info("Creating LED for node %s: reg=%u\n", ledNode->name, regValue);
152
153 ledClassDev->name = of_get_property(ledNode, "label", NULL);
154 if (ledClassDev->name == NULL)
155 {
156 ledClassDev->name = ledNode->name;
157 }
158
159 ledClassDev->brightness = LED_OFF;
160 ledClassDev->max_brightness = LED_FULL;
161 ledClassDev->brightness_set_blocking = gca230718_set_brightness;
162
163 if (devm_led_classdev_register_ext(&(client->dev), ledClassDev, &init_data) != 0)
164 {
165 pr_info("Error during call of devm_led_classdev_register_ext");
166 }
167 }
168 }
169 }
170
171 if (status == 0)
172 {
173 /*
174 Send full initialization sequence.
175 Afterwards only GCA230718_2ND_SEQUENCE_BYTE_1 must be send to upddate the brightness values.
176 */
177 gca230718_send_sequence(client, GCA230718_1ST_SEQUENCE_BYTE_1, gca230718_privateData);
178 gca230718_send_sequence(client, GCA230718_2ND_SEQUENCE_BYTE_1, gca230718_privateData);
179 gca230718_send_sequence(client, GCA230718_3RD_SEQUENCE_BYTE_1, gca230718_privateData);
180 }
181
182 return status;
183 }
184
185 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,18,0)
186 static void gca230718_remove(struct i2c_client *client)
187 #else
188 static int gca230718_remove(struct i2c_client *client)
189 #endif
190 {
191 struct gca230718_private* gca230718_privateData;
192 gca230718_privateData = i2c_get_clientdata(client);
193 mutex_destroy(&gca230718_privateData->lock);
194 gca230718_init_private_led_data(gca230718_privateData);
195
196 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,18,0)
197 return 0;
198 #endif
199 }
200
201 static const struct i2c_device_id gca230718_i2c_ids[] = {
202 { "gca230718", 0 },
203 {},
204 };
205 MODULE_DEVICE_TABLE(i2c, gca230718_i2c_ids);
206
207 static const struct of_device_id gca230718_dt_ids[] = {
208 { .compatible = "unknown,gca230718" },
209 {},
210 };
211 MODULE_DEVICE_TABLE(of, gca230718_dt_ids);
212
213 static struct i2c_driver gca230718_driver = {
214 .probe = gca230718_probe,
215 .remove = gca230718_remove,
216 .id_table = gca230718_i2c_ids,
217 .driver = {
218 .name = KBUILD_MODNAME,
219 .of_match_table = gca230718_dt_ids,
220 },
221 };
222
223 module_i2c_driver(gca230718_driver);
224
225 MODULE_AUTHOR("Roland Reinl <reinlroland+github@gmail.com>");
226 MODULE_DESCRIPTION("GCA230718 LED support (e.g. for D-Link M30) using I2C");
227 MODULE_LICENSE("GPL");