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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Kinetic KTD2026/7 RGB/White LED driver with I2C interface
*
* Copyright 2023 André Apitzsch <git@apitzsch.eu>
*
* Datasheet: https://www.kinet-ic.com/uploads/KTD2026-7-04h.pdf
*/
#include <linux/i2c.h>
#include <linux/led-class-multicolor.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#define KTD2026_NUM_LEDS 3
#define KTD2027_NUM_LEDS 4
#define KTD202X_MAX_LEDS 4
/* Register bank */
#define KTD202X_REG_RESET_CONTROL 0x00
#define KTD202X_REG_FLASH_PERIOD 0x01
#define KTD202X_REG_PWM1_TIMER 0x02
#define KTD202X_REG_PWM2_TIMER 0x03
#define KTD202X_REG_CHANNEL_CTRL 0x04
#define KTD202X_REG_TRISE_FALL 0x05
#define KTD202X_REG_LED_IOUT(x) (0x06 + (x))
/* Register 0 */
#define KTD202X_TIMER_SLOT_CONTROL_TSLOT1 0x00
#define KTD202X_TIMER_SLOT_CONTROL_TSLOT2 0x01
#define KTD202X_TIMER_SLOT_CONTROL_TSLOT3 0x02
#define KTD202X_TIMER_SLOT_CONTROL_TSLOT4 0x03
#define KTD202X_RSTR_RESET 0x07
#define KTD202X_ENABLE_CTRL_WAKE 0x00 /* SCL High & SDA High */
#define KTD202X_ENABLE_CTRL_SLEEP 0x08 /* SCL High & SDA Toggling */
#define KTD202X_TRISE_FALL_SCALE_NORMAL 0x00
#define KTD202X_TRISE_FALL_SCALE_SLOW_X2 0x20
#define KTD202X_TRISE_FALL_SCALE_SLOW_X4 0x40
#define KTD202X_TRISE_FALL_SCALE_FAST_X8 0x60
/* Register 1 */
#define KTD202X_FLASH_PERIOD_256_MS_LOG_RAMP 0x00
/* Register 2-3 */
#define KTD202X_FLASH_ON_TIME_0_4_PERCENT 0x01
/* Register 4 */
#define KTD202X_CHANNEL_CTRL_MASK(x) (BIT(2 * (x)) | BIT(2 * (x) + 1))
#define KTD202X_CHANNEL_CTRL_OFF 0x00
#define KTD202X_CHANNEL_CTRL_ON(x) BIT(2 * (x))
#define KTD202X_CHANNEL_CTRL_PWM1(x) BIT(2 * (x) + 1)
#define KTD202X_CHANNEL_CTRL_PWM2(x) (BIT(2 * (x)) | BIT(2 * (x) + 1))
/* Register 5 */
#define KTD202X_RAMP_TIMES_2_MS 0x00
/* Register 6-9 */
#define KTD202X_LED_CURRENT_10_mA 0x4f
#define KTD202X_FLASH_PERIOD_MIN_MS 256
#define KTD202X_FLASH_PERIOD_STEP_MS 128
#define KTD202X_FLASH_PERIOD_MAX_STEPS 126
#define KTD202X_FLASH_ON_MAX 256
#define KTD202X_MAX_BRIGHTNESS 192
static const struct reg_default ktd202x_reg_defaults[] = {
{ KTD202X_REG_RESET_CONTROL, KTD202X_TIMER_SLOT_CONTROL_TSLOT1 |
KTD202X_ENABLE_CTRL_WAKE | KTD202X_TRISE_FALL_SCALE_NORMAL },
{ KTD202X_REG_FLASH_PERIOD, KTD202X_FLASH_PERIOD_256_MS_LOG_RAMP },
{ KTD202X_REG_PWM1_TIMER, KTD202X_FLASH_ON_TIME_0_4_PERCENT },
{ KTD202X_REG_PWM2_TIMER, KTD202X_FLASH_ON_TIME_0_4_PERCENT },
{ KTD202X_REG_CHANNEL_CTRL, KTD202X_CHANNEL_CTRL_OFF },
{ KTD202X_REG_TRISE_FALL, KTD202X_RAMP_TIMES_2_MS },
{ KTD202X_REG_LED_IOUT(0), KTD202X_LED_CURRENT_10_mA },
{ KTD202X_REG_LED_IOUT(1), KTD202X_LED_CURRENT_10_mA },
{ KTD202X_REG_LED_IOUT(2), KTD202X_LED_CURRENT_10_mA },
{ KTD202X_REG_LED_IOUT(3), KTD202X_LED_CURRENT_10_mA },
};
struct ktd202x_led {
struct ktd202x *chip;
union {
struct led_classdev cdev;
struct led_classdev_mc mcdev;
};
u32 index;
};
struct ktd202x {
struct mutex mutex;
struct regulator_bulk_data regulators[2];
struct device *dev;
struct regmap *regmap;
bool enabled;
int num_leds;
struct ktd202x_led leds[] __counted_by(num_leds);
};
static int ktd202x_chip_disable(struct ktd202x *chip)
{
int ret;
if (!chip->enabled)
return 0;
regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_ENABLE_CTRL_SLEEP);
ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators), chip->regulators);
if (ret) {
dev_err(chip->dev, "Failed to disable regulators: %d\n", ret);
return ret;
}
chip->enabled = false;
return 0;
}
static int ktd202x_chip_enable(struct ktd202x *chip)
{
int ret;
if (chip->enabled)
return 0;
ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators), chip->regulators);
if (ret) {
dev_err(chip->dev, "Failed to enable regulators: %d\n", ret);
return ret;
}
chip->enabled = true;
ret = regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_ENABLE_CTRL_WAKE);
if (ret) {
dev_err(chip->dev, "Failed to enable the chip: %d\n", ret);
ktd202x_chip_disable(chip);
}
return ret;
}
static bool ktd202x_chip_in_use(struct ktd202x *chip)
{
int i;
for (i = 0; i < chip->num_leds; i++) {
if (chip->leds[i].cdev.brightness)
return true;
}
return false;
}
static int ktd202x_brightness_set(struct ktd202x_led *led,
struct mc_subled *subleds,
unsigned int num_channels)
{
bool mode_blink = false;
int channel;
int state;
int ret;
int i;
if (ktd202x_chip_in_use(led->chip)) {
ret = ktd202x_chip_enable(led->chip);
if (ret)
return ret;
}
ret = regmap_read(led->chip->regmap, KTD202X_REG_CHANNEL_CTRL, &state);
if (ret)
return ret;
/*
* In multicolor case, assume blink mode if PWM is set for at least one
* channel because another channel cannot be in state ON at the same time
*/
for (i = 0; i < num_channels; i++) {
int channel_state;
channel = subleds[i].channel;
channel_state = (state >> 2 * channel) & KTD202X_CHANNEL_CTRL_MASK(0);
if (channel_state == KTD202X_CHANNEL_CTRL_OFF)
continue;
mode_blink = channel_state == KTD202X_CHANNEL_CTRL_PWM1(0);
break;
}
for (i = 0; i < num_channels; i++) {
enum led_brightness brightness;
int mode;
brightness = subleds[i].brightness;
channel = subleds[i].channel;
if (brightness) {
/* Register expects brightness between 0 and MAX_BRIGHTNESS - 1 */
ret = regmap_write(led->chip->regmap, KTD202X_REG_LED_IOUT(channel),
brightness - 1);
if (ret)
return ret;
if (mode_blink)
mode = KTD202X_CHANNEL_CTRL_PWM1(channel);
else
mode = KTD202X_CHANNEL_CTRL_ON(channel);
} else {
mode = KTD202X_CHANNEL_CTRL_OFF;
}
ret = regmap_update_bits(led->chip->regmap, KTD202X_REG_CHANNEL_CTRL,
KTD202X_CHANNEL_CTRL_MASK(channel), mode);
if (ret)
return ret;
}
if (!ktd202x_chip_in_use(led->chip))
return ktd202x_chip_disable(led->chip);
return 0;
}
static int ktd202x_brightness_single_set(struct led_classdev *cdev,
enum led_brightness value)
{
struct ktd202x_led *led = container_of(cdev, struct ktd202x_led, cdev);
struct mc_subled info;
int ret;
cdev->brightness = value;
mutex_lock(&led->chip->mutex);
info.brightness = value;
info.channel = led->index;
ret = ktd202x_brightness_set(led, &info, 1);
mutex_unlock(&led->chip->mutex);
return ret;
}
static int ktd202x_brightness_mc_set(struct led_classdev *cdev,
enum led_brightness value)
{
struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
struct ktd202x_led *led = container_of(mc, struct ktd202x_led, mcdev);
int ret;
cdev->brightness = value;
mutex_lock(&led->chip->mutex);
led_mc_calc_color_components(mc, value);
ret = ktd202x_brightness_set(led, mc->subled_info, mc->num_colors);
mutex_unlock(&led->chip->mutex);
return ret;
}
static int ktd202x_blink_set(struct ktd202x_led *led, unsigned long *delay_on,
unsigned long *delay_off, struct mc_subled *subleds,
unsigned int num_channels)
{
unsigned long delay_total_ms;
int ret, num_steps, on;
u8 ctrl_mask = 0;
u8 ctrl_pwm1 = 0;
u8 ctrl_on = 0;
int i;
mutex_lock(&led->chip->mutex);
for (i = 0; i < num_channels; i++) {
int channel = subleds[i].channel;
ctrl_mask |= KTD202X_CHANNEL_CTRL_MASK(channel);
ctrl_on |= KTD202X_CHANNEL_CTRL_ON(channel);
ctrl_pwm1 |= KTD202X_CHANNEL_CTRL_PWM1(channel);
}
/* Never off - brightness is already set, disable blinking */
if (!*delay_off) {
ret = regmap_update_bits(led->chip->regmap, KTD202X_REG_CHANNEL_CTRL,
ctrl_mask, ctrl_on);
goto out;
}
/* Convert into values the HW will understand. */
/* Integer representation of time of flash period */
num_steps = (*delay_on + *delay_off - KTD202X_FLASH_PERIOD_MIN_MS) /
KTD202X_FLASH_PERIOD_STEP_MS;
num_steps = clamp(num_steps, 0, KTD202X_FLASH_PERIOD_MAX_STEPS);
/* Integer representation of percentage of LED ON time */
on = (*delay_on * KTD202X_FLASH_ON_MAX) / (*delay_on + *delay_off);
/* Actually used delay_{on,off} values */
delay_total_ms = num_steps * KTD202X_FLASH_PERIOD_STEP_MS + KTD202X_FLASH_PERIOD_MIN_MS;
*delay_on = (delay_total_ms * on) / KTD202X_FLASH_ON_MAX;
*delay_off = delay_total_ms - *delay_on;
/* Set timings */
ret = regmap_write(led->chip->regmap, KTD202X_REG_FLASH_PERIOD, num_steps);
if (ret)
goto out;
ret = regmap_write(led->chip->regmap, KTD202X_REG_PWM1_TIMER, on);
if (ret)
goto out;
ret = regmap_update_bits(led->chip->regmap, KTD202X_REG_CHANNEL_CTRL,
ctrl_mask, ctrl_pwm1);
out:
mutex_unlock(&led->chip->mutex);
return ret;
}
static int ktd202x_blink_single_set(struct led_classdev *cdev,
unsigned long *delay_on,
unsigned long *delay_off)
{
struct ktd202x_led *led = container_of(cdev, struct ktd202x_led, cdev);
struct mc_subled info;
int ret;
if (!cdev->brightness) {
ret = ktd202x_brightness_single_set(cdev, KTD202X_MAX_BRIGHTNESS);
if (ret)
return ret;
}
/* If no blink specified, default to 1 Hz. */
if (!*delay_off && !*delay_on) {
*delay_off = 500;
*delay_on = 500;
}
/* Never on - just set to off */
if (!*delay_on)
return ktd202x_brightness_single_set(cdev, LED_OFF);
info.channel = led->index;
return ktd202x_blink_set(led, delay_on, delay_off, &info, 1);
}
static int ktd202x_blink_mc_set(struct led_classdev *cdev,
unsigned long *delay_on,
unsigned long *delay_off)
{
struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
struct ktd202x_led *led = container_of(mc, struct ktd202x_led, mcdev);
int ret;
if (!cdev->brightness) {
ret = ktd202x_brightness_mc_set(cdev, KTD202X_MAX_BRIGHTNESS);
if (ret)
return ret;
}
/* If no blink specified, default to 1 Hz. */
if (!*delay_off && !*delay_on) {
*delay_off = 500;
*delay_on = 500;
}
/* Never on - just set to off */
if (!*delay_on)
return ktd202x_brightness_mc_set(cdev, LED_OFF);
return ktd202x_blink_set(led, delay_on, delay_off, mc->subled_info,
mc->num_colors);
}
static int ktd202x_setup_led_rgb(struct ktd202x *chip, struct device_node *np,
struct ktd202x_led *led, struct led_init_data *init_data)
{
struct led_classdev *cdev;
struct device_node *child;
struct mc_subled *info;
int num_channels;
int i = 0;
num_channels = of_get_available_child_count(np);
if (!num_channels || num_channels > chip->num_leds)
return -EINVAL;
info = devm_kcalloc(chip->dev, num_channels, sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
for_each_available_child_of_node(np, child) {
u32 mono_color;
u32 reg;
int ret;
ret = of_property_read_u32(child, "reg", ®);
if (ret != 0 || reg >= chip->num_leds) {
dev_err(chip->dev, "invalid 'reg' of %pOFn\n", child);
of_node_put(child);
return -EINVAL;
}
ret = of_property_read_u32(child, "color", &mono_color);
if (ret < 0 && ret != -EINVAL) {
dev_err(chip->dev, "failed to parse 'color' of %pOF\n", child);
of_node_put(child);
return ret;
}
info[i].color_index = mono_color;
info[i].channel = reg;
info[i].intensity = KTD202X_MAX_BRIGHTNESS;
i++;
}
led->mcdev.subled_info = info;
led->mcdev.num_colors = num_channels;
cdev = &led->mcdev.led_cdev;
cdev->brightness_set_blocking = ktd202x_brightness_mc_set;
cdev->blink_set = ktd202x_blink_mc_set;
return devm_led_classdev_multicolor_register_ext(chip->dev, &led->mcdev, init_data);
}
static int ktd202x_setup_led_single(struct ktd202x *chip, struct device_node *np,
struct ktd202x_led *led, struct led_init_data *init_data)
{
struct led_classdev *cdev;
u32 reg;
int ret;
ret = of_property_read_u32(np, "reg", ®);
if (ret != 0 || reg >= chip->num_leds) {
dev_err(chip->dev, "invalid 'reg' of %pOFn\n", np);
return -EINVAL;
}
led->index = reg;
cdev = &led->cdev;
cdev->brightness_set_blocking = ktd202x_brightness_single_set;
cdev->blink_set = ktd202x_blink_single_set;
return devm_led_classdev_register_ext(chip->dev, &led->cdev, init_data);
}
static int ktd202x_add_led(struct ktd202x *chip, struct device_node *np, unsigned int index)
{
struct ktd202x_led *led = &chip->leds[index];
struct led_init_data init_data = {};
struct led_classdev *cdev;
u32 color;
int ret;
/* Color property is optional in single color case */
ret = of_property_read_u32(np, "color", &color);
if (ret < 0 && ret != -EINVAL) {
dev_err(chip->dev, "failed to parse 'color' of %pOF\n", np);
return ret;
}
led->chip = chip;
init_data.fwnode = of_fwnode_handle(np);
if (color == LED_COLOR_ID_RGB) {
cdev = &led->mcdev.led_cdev;
ret = ktd202x_setup_led_rgb(chip, np, led, &init_data);
} else {
cdev = &led->cdev;
ret = ktd202x_setup_led_single(chip, np, led, &init_data);
}
if (ret) {
dev_err(chip->dev, "unable to register %s\n", cdev->name);
return ret;
}
cdev->max_brightness = KTD202X_MAX_BRIGHTNESS;
return 0;
}
static int ktd202x_probe_dt(struct ktd202x *chip)
{
struct device_node *np = dev_of_node(chip->dev), *child;
int count;
int i = 0;
chip->num_leds = (int)(unsigned long)of_device_get_match_data(chip->dev);
count = of_get_available_child_count(np);
if (!count || count > chip->num_leds)
return -EINVAL;
regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_RSTR_RESET);
/* Allow the device to execute the complete reset */
usleep_range(200, 300);
for_each_available_child_of_node(np, child) {
int ret = ktd202x_add_led(chip, child, i);
if (ret) {
of_node_put(child);
return ret;
}
i++;
}
return 0;
}
static const struct regmap_config ktd202x_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x09,
.cache_type = REGCACHE_FLAT,
.reg_defaults = ktd202x_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(ktd202x_reg_defaults),
};
static int ktd202x_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
struct ktd202x *chip;
int count;
int ret;
count = device_get_child_node_count(dev);
if (!count || count > KTD202X_MAX_LEDS)
return dev_err_probe(dev, -EINVAL, "Incorrect number of leds (%d)", count);
chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
if (!chip)
return -ENOMEM;
chip->dev = dev;
i2c_set_clientdata(client, chip);
chip->regmap = devm_regmap_init_i2c(client, &ktd202x_regmap_config);
if (IS_ERR(chip->regmap)) {
ret = dev_err_probe(dev, PTR_ERR(chip->regmap),
"Failed to allocate register map.\n");
return ret;
}
chip->regulators[0].supply = "vin";
chip->regulators[1].supply = "vio";
ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(chip->regulators), chip->regulators);
if (ret < 0) {
dev_err_probe(dev, ret, "Failed to request regulators.\n");
return ret;
}
ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators), chip->regulators);
if (ret) {
dev_err_probe(dev, ret, "Failed to enable regulators.\n");
return ret;
}
ret = ktd202x_probe_dt(chip);
if (ret < 0) {
regulator_bulk_disable(ARRAY_SIZE(chip->regulators), chip->regulators);
return ret;
}
ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators), chip->regulators);
if (ret) {
dev_err_probe(dev, ret, "Failed to disable regulators.\n");
return ret;
}
mutex_init(&chip->mutex);
return 0;
}
static void ktd202x_remove(struct i2c_client *client)
{
struct ktd202x *chip = i2c_get_clientdata(client);
ktd202x_chip_disable(chip);
mutex_destroy(&chip->mutex);
}
static void ktd202x_shutdown(struct i2c_client *client)
{
struct ktd202x *chip = i2c_get_clientdata(client);
/* Reset registers to make sure all LEDs are off before shutdown */
regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_RSTR_RESET);
}
static const struct of_device_id ktd202x_match_table[] = {
{ .compatible = "kinetic,ktd2026", .data = (void *)KTD2026_NUM_LEDS },
{ .compatible = "kinetic,ktd2027", .data = (void *)KTD2027_NUM_LEDS },
{},
};
MODULE_DEVICE_TABLE(of, ktd202x_match_table);
static struct i2c_driver ktd202x_driver = {
.driver = {
.name = "leds-ktd202x",
.of_match_table = ktd202x_match_table,
},
.probe = ktd202x_probe,
.remove = ktd202x_remove,
.shutdown = ktd202x_shutdown,
};
module_i2c_driver(ktd202x_driver);
MODULE_AUTHOR("André Apitzsch <git@apitzsch.eu>");
MODULE_DESCRIPTION("Kinetic KTD2026/7 LED driver");
MODULE_LICENSE("GPL");
|