1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* LED driver for STMicroelectronics LED1202 chip
*
* Copyright (C) 2024 Remote-Tech Ltd. UK
*/
#include <linux/cleanup.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string.h>
#define ST1202_BLINK_DEFAULT_DELAY 500
#define ST1202_CHAN_DISABLE_ALL 0x00
#define ST1202_CHAN_ENABLE_HIGH 0x03
#define ST1202_CHAN_ENABLE_LOW 0x02
#define ST1202_CONFIG_REG 0x04
/* PATS: Pattern sequence feature enable */
#define ST1202_CONFIG_REG_PATS BIT(7)
/* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
#define ST1202_CONFIG_REG_PATSR BIT(6)
/* SHFT: Phase-shift delay enable */
#define ST1202_CONFIG_REG_PHASE_SHIFT BIT(3)
#define ST1202_DEV_ENABLE 0x01
#define ST1202_DEV_ENABLE_ON BIT(0)
#define ST1202_DEV_ENABLE_RESET BIT(7)
#define ST1202_DEVICE_ID 0x00
#define ST1202_ILED_REG0 0x09
#define ST1202_MAX_LEDS 12
#define ST1202_MAX_PATTERNS 8
#define ST1202_MILLIS_PATTERN_DUR_MAX (ST1202_MILLIS_PATTERN_DUR_MIN * U8_MAX)
#define ST1202_MILLIS_PATTERN_DUR_MIN 22
#define ST1202_PATTERN_DUR 0x16
#define ST1202_PATTERN_PWM 0x1E
#define ST1202_PATTERN_PWM_FULL 0x0FFF
#define ST1202_PATTERN_REP 0x15
struct st1202_led {
struct fwnode_handle *fwnode;
struct led_classdev led_cdev;
struct st1202_chip *chip;
bool is_active;
int led_num;
};
struct st1202_chip {
struct i2c_client *client;
struct mutex lock;
struct st1202_led leds[ST1202_MAX_LEDS];
};
static struct st1202_led *cdev_to_st1202_led(struct led_classdev *cdev)
{
return container_of(cdev, struct st1202_led, led_cdev);
}
static int st1202_read_reg(struct st1202_chip *chip, int reg, u8 *val)
{
struct device *dev = &chip->client->dev;
int ret;
ret = i2c_smbus_read_byte_data(chip->client, reg);
if (ret < 0) {
dev_err(dev, "Failed to read register [0x%x]: %d\n", reg, ret);
return ret;
}
*val = (u8)ret;
return 0;
}
static int st1202_write_reg(struct st1202_chip *chip, int reg, u8 val)
{
struct device *dev = &chip->client->dev;
int ret;
ret = i2c_smbus_write_byte_data(chip->client, reg, val);
if (ret != 0)
dev_err(dev, "Failed to write %d to register [0x%x]: %d\n", val, reg, ret);
return ret;
}
static u8 st1202_milliseconds_to_prescaler(unsigned int value)
{
return value / ST1202_MILLIS_PATTERN_DUR_MIN;
}
/* The channel current register is 8 bits wide, whatever max-brightness says */
static u8 st1202_iled_max(struct led_classdev *led_cdev)
{
return min_t(unsigned int, led_cdev->max_brightness, U8_MAX);
}
static u16 st1202_brightness_to_pwm(int brightness, unsigned int max_brightness)
{
if (!max_brightness)
return 0;
brightness = clamp_t(int, brightness, LED_OFF, max_brightness);
return brightness * ST1202_PATTERN_PWM_FULL / max_brightness;
}
static int st1202_pwm_pattern_write(struct st1202_chip *chip, int led_num,
int pattern, unsigned int value)
{
u8 value_l, value_h;
int ret;
value_l = (u8)value;
value_h = (u8)(value >> 8);
/*
* Datasheet: Register address low = 1Eh + 2*(xh) + 18h*(yh),
* where x is the channel number (led number) in hexadecimal (x = 00h .. 0Bh)
* and y is the pattern number in hexadecimal (y = 00h .. 07h)
*/
ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + (led_num * 2) + 0x18 * pattern),
value_l);
if (ret != 0)
return ret;
/*
* Datasheet: Register address high = 1Eh + 01h + 2(xh) +18h*(yh),
* where x is the channel number in hexadecimal (x = 00h .. 0Bh)
* and y is the pattern number in hexadecimal (y = 00h .. 07h)
*/
ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + 0x1 + (led_num * 2) + 0x18 * pattern),
value_h);
if (ret != 0)
return ret;
return 0;
}
static int st1202_duration_pattern_write(struct st1202_chip *chip, int pattern,
unsigned int value)
{
return st1202_write_reg(chip, (ST1202_PATTERN_DUR + pattern),
st1202_milliseconds_to_prescaler(value));
}
static int __st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
{
u8 chan_low, chan_high;
int ret;
if (led_num <= 7) {
ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
if (ret < 0)
return ret;
chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
if (ret < 0)
return ret;
} else {
ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
if (ret < 0)
return ret;
chan_high = active ? chan_high | (BIT(led_num) >> 8) :
chan_high & ~(BIT(led_num) >> 8);
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
if (ret < 0)
return ret;
}
return 0;
}
static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
{
struct st1202_led *led = cdev_to_st1202_led(led_cdev);
struct st1202_chip *chip = led->chip;
u8 value = 0;
guard(mutex)(&chip->lock);
st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
return value;
}
static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
struct st1202_chip *chip = led->chip;
int ret;
guard(mutex)(&chip->lock);
/*
* The output of a channel is ILED x Pattern_PWM / 4095. Setting every
* PWM slot to full scale makes it equal ILED whatever the state of the
* sequencer, so the brightness takes effect without stopping the
* sequencer, which is global and would disturb the other channels.
*/
for (int pattern = 0; pattern < ST1202_MAX_PATTERNS; pattern++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, pattern,
ST1202_PATTERN_PWM_FULL);
if (ret)
return ret;
}
ret = st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num,
min_t(unsigned int, value, st1202_iled_max(ldev)));
if (ret)
return ret;
return __st1202_channel_set(chip, led->led_num, !!value);
}
static int st1202_led_pattern_clear(struct led_classdev *ldev)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
struct st1202_chip *chip = led->chip;
int ret;
guard(mutex)(&chip->lock);
ret = st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, LED_OFF);
if (ret != 0)
return ret;
ret = __st1202_channel_set(chip, led->led_num, false);
if (ret != 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret != 0)
return ret;
for (int pattern = 0; pattern < ST1202_MAX_PATTERNS; pattern++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, pattern,
ST1202_PATTERN_PWM_FULL);
if (ret != 0)
return ret;
ret = st1202_write_reg(chip, ST1202_PATTERN_DUR + pattern, 0);
if (ret != 0)
return ret;
}
return 0;
}
static int st1202_led_pattern_set(struct led_classdev *ldev,
struct led_pattern *patterns,
u32 len, int repeat)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
struct st1202_chip *chip = led->chip;
unsigned int max_brightness;
int ret;
max_brightness = st1202_iled_max(ldev);
if (len > ST1202_MAX_PATTERNS)
return -EINVAL;
for (int pattern = 0; pattern < len; pattern++) {
if (patterns[pattern].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
patterns[pattern].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
return -EINVAL;
}
guard(mutex)(&chip->lock);
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret != 0)
return ret;
for (int pattern = 0; pattern < len; pattern++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, pattern,
st1202_brightness_to_pwm(patterns[pattern].brightness,
max_brightness));
if (ret != 0)
return ret;
ret = st1202_duration_pattern_write(chip, pattern, patterns[pattern].delta_t);
if (ret != 0)
return ret;
}
for (int pattern = len; pattern < ST1202_MAX_PATTERNS; pattern++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, pattern, LED_OFF);
if (ret != 0)
return ret;
}
ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
if (ret != 0)
return ret;
ret = st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, max_brightness);
if (ret != 0)
return ret;
ret = __st1202_channel_set(chip, led->led_num, true);
if (ret != 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
ST1202_CONFIG_REG_PATSR | ST1202_CONFIG_REG_PATS |
ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret != 0)
return ret;
return 0;
}
static int st1202_blink_set(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off)
{
struct st1202_led *led = cdev_to_st1202_led(led_cdev);
struct st1202_chip *chip = led->chip;
unsigned long on, off;
int ret;
if (!*delay_on)
*delay_on = ST1202_BLINK_DEFAULT_DELAY;
if (!*delay_off)
*delay_off = ST1202_BLINK_DEFAULT_DELAY;
on = *delay_on;
off = *delay_off;
on = clamp_val(on, ST1202_MILLIS_PATTERN_DUR_MIN, ST1202_MILLIS_PATTERN_DUR_MAX);
off = clamp_val(off, ST1202_MILLIS_PATTERN_DUR_MIN, ST1202_MILLIS_PATTERN_DUR_MAX);
on = roundup(on, ST1202_MILLIS_PATTERN_DUR_MIN);
off = roundup(off, ST1202_MILLIS_PATTERN_DUR_MIN);
guard(mutex)(&chip->lock);
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret)
return ret;
/* Zero out PWM for all other active channels to prevent them from blinking */
for (int chan = 0; chan < ST1202_MAX_LEDS; chan++) {
if (!chip->leds[chan].is_active || chan == led->led_num)
continue;
ret = st1202_pwm_pattern_write(chip, chan, 0, LED_OFF);
if (ret)
return ret;
ret = st1202_pwm_pattern_write(chip, chan, 1, LED_OFF);
if (ret)
return ret;
}
ret = st1202_pwm_pattern_write(chip, led->led_num, 0, ST1202_PATTERN_PWM_FULL);
if (ret)
return ret;
for (int pattern = 1; pattern < ST1202_MAX_PATTERNS; pattern++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, pattern, LED_OFF);
if (ret)
return ret;
}
ret = st1202_duration_pattern_write(chip, 0, on);
if (ret)
return ret;
ret = st1202_duration_pattern_write(chip, 1, off);
if (ret)
return ret;
for (int pattern = 2; pattern < ST1202_MAX_PATTERNS; pattern++) {
ret = st1202_write_reg(chip, ST1202_PATTERN_DUR + pattern, 0);
if (ret)
return ret;
}
ret = st1202_write_reg(chip, ST1202_PATTERN_REP, U8_MAX);
if (ret)
return ret;
ret = st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, st1202_iled_max(led_cdev));
if (ret)
return ret;
ret = __st1202_channel_set(chip, led->led_num, true);
if (ret)
return ret;
ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
ST1202_CONFIG_REG_PATSR | ST1202_CONFIG_REG_PATS |
ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret)
return ret;
*delay_on = on;
*delay_off = off;
return 0;
}
static void st1202_fwnode_put(void *data)
{
fwnode_handle_put(data);
}
static int st1202_dt_init(struct st1202_chip *chip)
{
struct device *dev = &chip->client->dev;
struct st1202_led *led;
int err;
u32 reg;
for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
err = of_property_read_u32(child, "reg", ®);
if (err)
return dev_err_probe(dev, err, "Invalid register\n");
if (reg >= ST1202_MAX_LEDS)
return dev_err_probe(dev, -EINVAL,
"LED reg %u out of range [0, %d]\n",
reg, ST1202_MAX_LEDS - 1);
led = &chip->leds[reg];
led->is_active = true;
led->fwnode = fwnode_handle_get(of_fwnode_handle(child));
err = devm_add_action_or_reset(dev, st1202_fwnode_put, led->fwnode);
if (err)
return err;
led->led_cdev.max_brightness = U8_MAX;
led->led_cdev.brightness_set_blocking = st1202_led_set;
led->led_cdev.pattern_set = st1202_led_pattern_set;
led->led_cdev.pattern_clear = st1202_led_pattern_clear;
led->led_cdev.default_trigger = "pattern";
led->led_cdev.blink_set = st1202_blink_set;
led->led_cdev.brightness_get = st1202_brightness_get;
}
return 0;
}
static int st1202_setup(struct st1202_chip *chip)
{
int ret;
guard(mutex)(&chip->lock);
/*
* Once the supply voltage is applied, the LED1202 executes some internal checks.
* Afterwards, it stops the oscillator and puts the internal LDO in quiescent mode.
* To start the device, EN bit must be set inside the “Device Enable” register at
* address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
* from the internal non-volatile memory and performs an auto-calibration procedure
* in order to increase the output current precision.
* Such initialization lasts about 6.5 ms.
*/
/* Reset the chip during setup */
ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
if (ret < 0)
return ret;
/* Enable phase-shift delay feature */
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_PHASE_SHIFT);
if (ret < 0)
return ret;
/* Enable the device */
ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
if (ret < 0)
return ret;
/* Duration of initialization */
usleep_range(6500, 10000);
/* Deactivate all LEDs (channels); each is enabled when it is lit */
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
if (ret < 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
if (ret < 0)
return ret;
return 0;
}
static int st1202_probe(struct i2c_client *client)
{
struct st1202_chip *chip;
struct st1202_led *led;
int ret;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
ret = devm_mutex_init(&client->dev, &chip->lock);
if (ret < 0)
return ret;
chip->client = client;
ret = st1202_setup(chip);
if (ret < 0)
return ret;
ret = st1202_dt_init(chip);
if (ret < 0)
return ret;
for (int i = 0; i < ST1202_MAX_LEDS; i++) {
struct led_init_data init_data = {};
led = &chip->leds[i];
led->chip = chip;
led->led_num = i;
if (!led->is_active)
continue;
ret = st1202_led_pattern_clear(&led->led_cdev);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to clear LED pattern\n");
init_data.fwnode = led->fwnode;
init_data.devicename = "st1202";
init_data.default_label = ":";
ret = devm_led_classdev_register_ext(&client->dev, &led->led_cdev, &init_data);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to register LED class device\n");
}
return 0;
}
static const struct i2c_device_id st1202_id[] = {
{ .name = "st1202-i2c" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, st1202_id);
static const struct of_device_id st1202_dt_ids[] = {
{ .compatible = "st,led1202" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, st1202_dt_ids);
static struct i2c_driver st1202_driver = {
.driver = {
.name = "leds-st1202",
.of_match_table = of_match_ptr(st1202_dt_ids),
},
.probe = st1202_probe,
.id_table = st1202_id,
};
module_i2c_driver(st1202_driver);
MODULE_AUTHOR("Remote Tech LTD");
MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
MODULE_LICENSE("GPL");
|