1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED driver for STMicroelectronics LED1202 chip
4 *
5 * Copyright (C) 2024 Remote-Tech Ltd. UK
6 */
7
8 #include <linux/cleanup.h>
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/gpio.h>
13 #include <linux/i2c.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18
19 #define ST1202_CHAN_DISABLE_ALL 0x00
20 #define ST1202_CHAN_ENABLE_HIGH 0x03
21 #define ST1202_CHAN_ENABLE_LOW 0x02
22 #define ST1202_CONFIG_REG 0x04
23 /* PATS: Pattern sequence feature enable */
24 #define ST1202_CONFIG_REG_PATS BIT(7)
25 /* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
26 #define ST1202_CONFIG_REG_PATSR BIT(6)
27 #define ST1202_CONFIG_REG_SHFT BIT(3)
28 #define ST1202_DEV_ENABLE 0x01
29 #define ST1202_DEV_ENABLE_ON BIT(0)
30 #define ST1202_DEV_ENABLE_RESET BIT(7)
31 #define ST1202_DEVICE_ID 0x00
32 #define ST1202_ILED_REG0 0x09
33 #define ST1202_MAX_LEDS 12
34 #define ST1202_MAX_PATTERNS 8
35 #define ST1202_MILLIS_PATTERN_DUR_MAX 5660
36 #define ST1202_MILLIS_PATTERN_DUR_MIN 22
37 #define ST1202_PATTERN_DUR 0x16
38 #define ST1202_PATTERN_PWM 0x1E
39 #define ST1202_PATTERN_REP 0x15
40
41 struct st1202_led {
42 struct fwnode_handle *fwnode;
43 struct led_classdev led_cdev;
44 struct st1202_chip *chip;
45 bool is_active;
46 int led_num;
47 };
48
49 struct st1202_chip {
50 struct i2c_client *client;
51 struct mutex lock;
52 struct st1202_led leds[ST1202_MAX_LEDS];
53 };
54
cdev_to_st1202_led(struct led_classdev * cdev)55 static struct st1202_led *cdev_to_st1202_led(struct led_classdev *cdev)
56 {
57 return container_of(cdev, struct st1202_led, led_cdev);
58 }
59
st1202_read_reg(struct st1202_chip * chip,int reg,uint8_t * val)60 static int st1202_read_reg(struct st1202_chip *chip, int reg, uint8_t *val)
61 {
62 struct device *dev = &chip->client->dev;
63 int ret;
64
65 ret = i2c_smbus_read_byte_data(chip->client, reg);
66 if (ret < 0) {
67 dev_err(dev, "Failed to read register [0x%x]: %d\n", reg, ret);
68 return ret;
69 }
70
71 *val = (uint8_t)ret;
72 return 0;
73 }
74
st1202_write_reg(struct st1202_chip * chip,int reg,uint8_t val)75 static int st1202_write_reg(struct st1202_chip *chip, int reg, uint8_t val)
76 {
77 struct device *dev = &chip->client->dev;
78 int ret;
79
80 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
81 if (ret != 0)
82 dev_err(dev, "Failed to write %d to register [0x%x]: %d\n", val, reg, ret);
83
84 return ret;
85 }
86
st1202_prescalar_to_miliseconds(unsigned int value)87 static uint8_t st1202_prescalar_to_miliseconds(unsigned int value)
88 {
89 return value / ST1202_MILLIS_PATTERN_DUR_MIN - 1;
90 }
91
st1202_pwm_pattern_write(struct st1202_chip * chip,int led_num,int pattern,unsigned int value)92 static int st1202_pwm_pattern_write(struct st1202_chip *chip, int led_num,
93 int pattern, unsigned int value)
94 {
95 u8 value_l, value_h;
96 int ret;
97
98 value_l = (u8)value;
99 value_h = (u8)(value >> 8);
100
101 /*
102 * Datasheet: Register address low = 1Eh + 2*(xh) + 18h*(yh),
103 * where x is the channel number (led number) in hexadecimal (x = 00h .. 0Bh)
104 * and y is the pattern number in hexadecimal (y = 00h .. 07h)
105 */
106 ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + (led_num * 2) + 0x18 * pattern),
107 value_l);
108 if (ret != 0)
109 return ret;
110
111 /*
112 * Datasheet: Register address high = 1Eh + 01h + 2(xh) +18h*(yh),
113 * where x is the channel number in hexadecimal (x = 00h .. 0Bh)
114 * and y is the pattern number in hexadecimal (y = 00h .. 07h)
115 */
116 ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + 0x1 + (led_num * 2) + 0x18 * pattern),
117 value_h);
118 if (ret != 0)
119 return ret;
120
121 return 0;
122 }
123
st1202_duration_pattern_write(struct st1202_chip * chip,int pattern,unsigned int value)124 static int st1202_duration_pattern_write(struct st1202_chip *chip, int pattern,
125 unsigned int value)
126 {
127 return st1202_write_reg(chip, (ST1202_PATTERN_DUR + pattern),
128 st1202_prescalar_to_miliseconds(value));
129 }
130
st1202_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)131 static void st1202_brightness_set(struct led_classdev *led_cdev,
132 enum led_brightness value)
133 {
134 struct st1202_led *led = cdev_to_st1202_led(led_cdev);
135 struct st1202_chip *chip = led->chip;
136
137 guard(mutex)(&chip->lock);
138
139 st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, value);
140 }
141
st1202_brightness_get(struct led_classdev * led_cdev)142 static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
143 {
144 struct st1202_led *led = cdev_to_st1202_led(led_cdev);
145 struct st1202_chip *chip = led->chip;
146 u8 value = 0;
147
148 guard(mutex)(&chip->lock);
149
150 st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
151
152 return value;
153 }
154
st1202_channel_set(struct st1202_chip * chip,int led_num,bool active)155 static int st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
156 {
157 u8 chan_low, chan_high;
158 int ret;
159
160 guard(mutex)(&chip->lock);
161
162 if (led_num <= 7) {
163 ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
164 if (ret < 0)
165 return ret;
166
167 chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
168
169 ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
170 if (ret < 0)
171 return ret;
172
173 } else {
174 ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
175 if (ret < 0)
176 return ret;
177
178 chan_high = active ? chan_high | (BIT(led_num) >> 8) :
179 chan_high & ~(BIT(led_num) >> 8);
180
181 ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
182 if (ret < 0)
183 return ret;
184 }
185
186 return 0;
187 }
188
st1202_led_set(struct led_classdev * ldev,enum led_brightness value)189 static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
190 {
191 struct st1202_led *led = cdev_to_st1202_led(ldev);
192 struct st1202_chip *chip = led->chip;
193
194 return st1202_channel_set(chip, led->led_num, value == LED_OFF ? false : true);
195 }
196
st1202_led_pattern_clear(struct led_classdev * ldev)197 static int st1202_led_pattern_clear(struct led_classdev *ldev)
198 {
199 struct st1202_led *led = cdev_to_st1202_led(ldev);
200 struct st1202_chip *chip = led->chip;
201 int ret;
202
203 guard(mutex)(&chip->lock);
204
205 for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++) {
206 ret = st1202_pwm_pattern_write(chip, led->led_num, patt, LED_OFF);
207 if (ret != 0)
208 return ret;
209
210 ret = st1202_duration_pattern_write(chip, patt, ST1202_MILLIS_PATTERN_DUR_MIN);
211 if (ret != 0)
212 return ret;
213 }
214
215 return 0;
216 }
217
st1202_led_pattern_set(struct led_classdev * ldev,struct led_pattern * pattern,u32 len,int repeat)218 static int st1202_led_pattern_set(struct led_classdev *ldev,
219 struct led_pattern *pattern,
220 u32 len, int repeat)
221 {
222 struct st1202_led *led = cdev_to_st1202_led(ldev);
223 struct st1202_chip *chip = led->chip;
224 int ret;
225
226 if (len > ST1202_MAX_PATTERNS)
227 return -EINVAL;
228
229 guard(mutex)(&chip->lock);
230
231 for (int patt = 0; patt < len; patt++) {
232 if (pattern[patt].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
233 pattern[patt].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
234 return -EINVAL;
235
236 ret = st1202_pwm_pattern_write(chip, led->led_num, patt, pattern[patt].brightness);
237 if (ret != 0)
238 return ret;
239
240 ret = st1202_duration_pattern_write(chip, patt, pattern[patt].delta_t);
241 if (ret != 0)
242 return ret;
243 }
244
245 ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
246 if (ret != 0)
247 return ret;
248
249 ret = st1202_write_reg(chip, ST1202_CONFIG_REG, (ST1202_CONFIG_REG_PATSR |
250 ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_SHFT));
251 if (ret != 0)
252 return ret;
253
254 return 0;
255 }
256
st1202_dt_init(struct st1202_chip * chip)257 static int st1202_dt_init(struct st1202_chip *chip)
258 {
259 struct device *dev = &chip->client->dev;
260 struct st1202_led *led;
261 int err, reg;
262
263 for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
264 err = of_property_read_u32(child, "reg", ®);
265 if (err)
266 return dev_err_probe(dev, err, "Invalid register\n");
267
268 led = &chip->leds[reg];
269 led->is_active = true;
270 led->fwnode = of_fwnode_handle(child);
271
272 led->led_cdev.max_brightness = U8_MAX;
273 led->led_cdev.brightness_set_blocking = st1202_led_set;
274 led->led_cdev.pattern_set = st1202_led_pattern_set;
275 led->led_cdev.pattern_clear = st1202_led_pattern_clear;
276 led->led_cdev.default_trigger = "pattern";
277 led->led_cdev.brightness_set = st1202_brightness_set;
278 led->led_cdev.brightness_get = st1202_brightness_get;
279 }
280
281 return 0;
282 }
283
st1202_setup(struct st1202_chip * chip)284 static int st1202_setup(struct st1202_chip *chip)
285 {
286 int ret;
287
288 guard(mutex)(&chip->lock);
289
290 /*
291 * Once the supply voltage is applied, the LED1202 executes some internal checks,
292 * afterwords it stops the oscillator and puts the internal LDO in quiescent mode.
293 * To start the device, EN bit must be set inside the “Device Enable” register at
294 * address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
295 * from the internal non-volatile memory and performs an auto-calibration procedure
296 * in order to increase the output current precision.
297 * Such initialization lasts about 6.5 ms.
298 */
299
300 /* Reset the chip during setup */
301 ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
302 if (ret < 0)
303 return ret;
304
305 /* Enable phase-shift delay feature */
306 ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
307 if (ret < 0)
308 return ret;
309
310 /* Enable the device */
311 ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
312 if (ret < 0)
313 return ret;
314
315 /* Duration of initialization */
316 usleep_range(6500, 10000);
317
318 /* Deactivate all LEDS (channels) and activate only the ones found in Device Tree */
319 ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
320 if (ret < 0)
321 return ret;
322
323 ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
324 if (ret < 0)
325 return ret;
326
327 ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
328 ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_PATSR);
329 if (ret < 0)
330 return ret;
331
332 return 0;
333 }
334
st1202_probe(struct i2c_client * client)335 static int st1202_probe(struct i2c_client *client)
336 {
337 struct st1202_chip *chip;
338 struct st1202_led *led;
339 int ret;
340
341 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
342 return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
343
344 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
345 if (!chip)
346 return -ENOMEM;
347
348 ret = devm_mutex_init(&client->dev, &chip->lock);
349 if (ret < 0)
350 return ret;
351 chip->client = client;
352
353 ret = st1202_dt_init(chip);
354 if (ret < 0)
355 return ret;
356
357 ret = st1202_setup(chip);
358 if (ret < 0)
359 return ret;
360
361 for (int i = 0; i < ST1202_MAX_LEDS; i++) {
362 struct led_init_data init_data = {};
363 led = &chip->leds[i];
364 led->chip = chip;
365 led->led_num = i;
366
367 if (!led->is_active)
368 continue;
369
370 ret = st1202_channel_set(led->chip, led->led_num, true);
371 if (ret < 0)
372 return dev_err_probe(&client->dev, ret,
373 "Failed to activate LED channel\n");
374
375 ret = st1202_led_pattern_clear(&led->led_cdev);
376 if (ret < 0)
377 return dev_err_probe(&client->dev, ret,
378 "Failed to clear LED pattern\n");
379
380 init_data.fwnode = led->fwnode;
381 init_data.devicename = "st1202";
382 init_data.default_label = ":";
383
384 ret = devm_led_classdev_register_ext(&client->dev, &led->led_cdev, &init_data);
385 if (ret < 0)
386 return dev_err_probe(&client->dev, ret,
387 "Failed to register LED class device\n");
388 }
389
390 return 0;
391 }
392
393 static const struct i2c_device_id st1202_id[] = {
394 { "st1202-i2c" },
395 { /* sentinel */ }
396 };
397 MODULE_DEVICE_TABLE(i2c, st1202_id);
398
399 static const struct of_device_id st1202_dt_ids[] = {
400 { .compatible = "st,led1202" },
401 { /* sentinel */ }
402 };
403 MODULE_DEVICE_TABLE(of, st1202_dt_ids);
404
405 static struct i2c_driver st1202_driver = {
406 .driver = {
407 .name = "leds-st1202",
408 .of_match_table = of_match_ptr(st1202_dt_ids),
409 },
410 .probe = st1202_probe,
411 .id_table = st1202_id,
412 };
413 module_i2c_driver(st1202_driver);
414
415 MODULE_AUTHOR("Remote Tech LTD");
416 MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
417 MODULE_LICENSE("GPL");
418