1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Simple driver for Texas Instruments LM3630A Backlight driver chip
4 * Copyright (C) 2012 Texas Instruments
5 */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/i2c.h>
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/delay.h>
12 #include <linux/uaccess.h>
13 #include <linux/interrupt.h>
14 #include <linux/regmap.h>
15 #include <linux/pwm.h>
16 #include <linux/platform_data/lm3630a_bl.h>
17
18 #define REG_CTRL 0x00
19 #define REG_BOOST 0x02
20 #define REG_CONFIG 0x01
21 #define REG_BRT_A 0x03
22 #define REG_BRT_B 0x04
23 #define REG_I_A 0x05
24 #define REG_I_B 0x06
25 #define REG_INT_STATUS 0x09
26 #define REG_INT_EN 0x0A
27 #define REG_FAULT 0x0B
28 #define REG_PWM_OUTLOW 0x12
29 #define REG_PWM_OUTHIGH 0x13
30 #define REG_FILTER_STRENGTH 0x50
31 #define REG_MAX 0x50
32
33 #define INT_DEBOUNCE_MSEC 10
34
35 #define LM3630A_BANK_0 0
36 #define LM3630A_BANK_1 1
37
38 #define LM3630A_NUM_SINKS 2
39 #define LM3630A_SINK_0 0
40 #define LM3630A_SINK_1 1
41
42 struct lm3630a_chip {
43 struct device *dev;
44 struct delayed_work work;
45
46 int irq;
47 struct workqueue_struct *irqthread;
48 struct lm3630a_platform_data *pdata;
49 struct backlight_device *bleda;
50 struct backlight_device *bledb;
51 struct regmap *regmap;
52 struct pwm_device *pwmd;
53 };
54
55 /* i2c access */
lm3630a_read(struct lm3630a_chip * pchip,unsigned int reg)56 static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
57 {
58 int rval;
59 unsigned int reg_val;
60
61 rval = regmap_read(pchip->regmap, reg, ®_val);
62 if (rval < 0)
63 return rval;
64 return reg_val & 0xFF;
65 }
66
lm3630a_write(struct lm3630a_chip * pchip,unsigned int reg,unsigned int data)67 static int lm3630a_write(struct lm3630a_chip *pchip,
68 unsigned int reg, unsigned int data)
69 {
70 return regmap_write(pchip->regmap, reg, data);
71 }
72
lm3630a_update(struct lm3630a_chip * pchip,unsigned int reg,unsigned int mask,unsigned int data)73 static int lm3630a_update(struct lm3630a_chip *pchip,
74 unsigned int reg, unsigned int mask,
75 unsigned int data)
76 {
77 return regmap_update_bits(pchip->regmap, reg, mask, data);
78 }
79
80 /* initialize chip */
lm3630a_chip_init(struct lm3630a_chip * pchip)81 static int lm3630a_chip_init(struct lm3630a_chip *pchip)
82 {
83 int rval;
84 struct lm3630a_platform_data *pdata = pchip->pdata;
85
86 usleep_range(1000, 2000);
87 /* set Filter Strength Register */
88 rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03);
89 /* set Cofig. register */
90 rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
91 /* set boost control */
92 rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
93 /* set current A */
94 rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
95 /* set current B */
96 rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
97 /* set control */
98 rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
99 rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
100 usleep_range(1000, 2000);
101 /* set brightness A and B */
102 rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
103 rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
104
105 if (rval < 0)
106 dev_err(pchip->dev, "i2c failed to access register\n");
107 return rval;
108 }
109
110 /* interrupt handling */
lm3630a_delayed_func(struct work_struct * work)111 static void lm3630a_delayed_func(struct work_struct *work)
112 {
113 int rval;
114 struct lm3630a_chip *pchip;
115
116 pchip = container_of(work, struct lm3630a_chip, work.work);
117
118 rval = lm3630a_read(pchip, REG_INT_STATUS);
119 if (rval < 0) {
120 dev_err(pchip->dev,
121 "i2c failed to access REG_INT_STATUS Register\n");
122 return;
123 }
124
125 dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
126 }
127
lm3630a_isr_func(int irq,void * chip)128 static irqreturn_t lm3630a_isr_func(int irq, void *chip)
129 {
130 int rval;
131 struct lm3630a_chip *pchip = chip;
132 unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
133
134 queue_delayed_work(pchip->irqthread, &pchip->work, delay);
135
136 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
137 if (rval < 0) {
138 dev_err(pchip->dev, "i2c failed to access register\n");
139 return IRQ_NONE;
140 }
141 return IRQ_HANDLED;
142 }
143
lm3630a_intr_config(struct lm3630a_chip * pchip)144 static int lm3630a_intr_config(struct lm3630a_chip *pchip)
145 {
146 int rval;
147
148 rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
149 if (rval < 0)
150 return rval;
151
152 INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
153 pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
154 if (!pchip->irqthread) {
155 dev_err(pchip->dev, "create irq thread fail\n");
156 return -ENOMEM;
157 }
158 if (request_threaded_irq
159 (pchip->irq, NULL, lm3630a_isr_func,
160 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
161 dev_err(pchip->dev, "request threaded irq fail\n");
162 destroy_workqueue(pchip->irqthread);
163 return -ENOMEM;
164 }
165 return rval;
166 }
167
lm3630a_pwm_ctrl(struct lm3630a_chip * pchip,int br,int br_max)168 static void lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
169 {
170 unsigned int period = pchip->pdata->pwm_period;
171 unsigned int duty = br * period / br_max;
172
173 pwm_config(pchip->pwmd, duty, period);
174 if (duty)
175 pwm_enable(pchip->pwmd);
176 else
177 pwm_disable(pchip->pwmd);
178 }
179
180 /* update and get brightness */
lm3630a_bank_a_update_status(struct backlight_device * bl)181 static int lm3630a_bank_a_update_status(struct backlight_device *bl)
182 {
183 int ret;
184 struct lm3630a_chip *pchip = bl_get_data(bl);
185 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
186
187 /* pwm control */
188 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
189 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
190 bl->props.max_brightness);
191 return bl->props.brightness;
192 }
193
194 /* disable sleep */
195 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
196 if (ret < 0)
197 goto out_i2c_err;
198 usleep_range(1000, 2000);
199 /* minimum brightness is 0x04 */
200 ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
201 if (bl->props.brightness < 0x4)
202 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
203 else
204 ret |= lm3630a_update(pchip, REG_CTRL,
205 LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
206 if (ret < 0)
207 goto out_i2c_err;
208 return 0;
209
210 out_i2c_err:
211 dev_err(pchip->dev, "i2c failed to access\n");
212 return bl->props.brightness;
213 }
214
lm3630a_bank_a_get_brightness(struct backlight_device * bl)215 static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
216 {
217 int brightness, rval;
218 struct lm3630a_chip *pchip = bl_get_data(bl);
219 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
220
221 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
222 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
223 if (rval < 0)
224 goto out_i2c_err;
225 brightness = (rval & 0x01) << 8;
226 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
227 if (rval < 0)
228 goto out_i2c_err;
229 brightness |= rval;
230 goto out;
231 }
232
233 /* disable sleep */
234 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
235 if (rval < 0)
236 goto out_i2c_err;
237 usleep_range(1000, 2000);
238 rval = lm3630a_read(pchip, REG_BRT_A);
239 if (rval < 0)
240 goto out_i2c_err;
241 brightness = rval;
242
243 out:
244 bl->props.brightness = brightness;
245 return bl->props.brightness;
246 out_i2c_err:
247 dev_err(pchip->dev, "i2c failed to access register\n");
248 return 0;
249 }
250
251 static const struct backlight_ops lm3630a_bank_a_ops = {
252 .options = BL_CORE_SUSPENDRESUME,
253 .update_status = lm3630a_bank_a_update_status,
254 .get_brightness = lm3630a_bank_a_get_brightness,
255 };
256
257 /* update and get brightness */
lm3630a_bank_b_update_status(struct backlight_device * bl)258 static int lm3630a_bank_b_update_status(struct backlight_device *bl)
259 {
260 int ret;
261 struct lm3630a_chip *pchip = bl_get_data(bl);
262 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
263
264 /* pwm control */
265 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
266 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
267 bl->props.max_brightness);
268 return bl->props.brightness;
269 }
270
271 /* disable sleep */
272 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
273 if (ret < 0)
274 goto out_i2c_err;
275 usleep_range(1000, 2000);
276 /* minimum brightness is 0x04 */
277 ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
278 if (bl->props.brightness < 0x4)
279 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
280 else
281 ret |= lm3630a_update(pchip, REG_CTRL,
282 LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
283 if (ret < 0)
284 goto out_i2c_err;
285 return 0;
286
287 out_i2c_err:
288 dev_err(pchip->dev, "i2c failed to access REG_CTRL\n");
289 return bl->props.brightness;
290 }
291
lm3630a_bank_b_get_brightness(struct backlight_device * bl)292 static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
293 {
294 int brightness, rval;
295 struct lm3630a_chip *pchip = bl_get_data(bl);
296 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
297
298 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
299 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
300 if (rval < 0)
301 goto out_i2c_err;
302 brightness = (rval & 0x01) << 8;
303 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
304 if (rval < 0)
305 goto out_i2c_err;
306 brightness |= rval;
307 goto out;
308 }
309
310 /* disable sleep */
311 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
312 if (rval < 0)
313 goto out_i2c_err;
314 usleep_range(1000, 2000);
315 rval = lm3630a_read(pchip, REG_BRT_B);
316 if (rval < 0)
317 goto out_i2c_err;
318 brightness = rval;
319
320 out:
321 bl->props.brightness = brightness;
322 return bl->props.brightness;
323 out_i2c_err:
324 dev_err(pchip->dev, "i2c failed to access register\n");
325 return 0;
326 }
327
328 static const struct backlight_ops lm3630a_bank_b_ops = {
329 .options = BL_CORE_SUSPENDRESUME,
330 .update_status = lm3630a_bank_b_update_status,
331 .get_brightness = lm3630a_bank_b_get_brightness,
332 };
333
lm3630a_backlight_register(struct lm3630a_chip * pchip)334 static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
335 {
336 struct lm3630a_platform_data *pdata = pchip->pdata;
337 struct backlight_properties props;
338 const char *label;
339
340 props.type = BACKLIGHT_RAW;
341 if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
342 props.brightness = pdata->leda_init_brt;
343 props.max_brightness = pdata->leda_max_brt;
344 label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda";
345 pchip->bleda =
346 devm_backlight_device_register(pchip->dev, label,
347 pchip->dev, pchip,
348 &lm3630a_bank_a_ops, &props);
349 if (IS_ERR(pchip->bleda))
350 return PTR_ERR(pchip->bleda);
351 }
352
353 if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
354 (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
355 props.brightness = pdata->ledb_init_brt;
356 props.max_brightness = pdata->ledb_max_brt;
357 label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb";
358 pchip->bledb =
359 devm_backlight_device_register(pchip->dev, label,
360 pchip->dev, pchip,
361 &lm3630a_bank_b_ops, &props);
362 if (IS_ERR(pchip->bledb))
363 return PTR_ERR(pchip->bledb);
364 }
365 return 0;
366 }
367
368 static const struct regmap_config lm3630a_regmap = {
369 .reg_bits = 8,
370 .val_bits = 8,
371 .max_register = REG_MAX,
372 };
373
lm3630a_parse_led_sources(struct fwnode_handle * node,int default_led_sources)374 static int lm3630a_parse_led_sources(struct fwnode_handle *node,
375 int default_led_sources)
376 {
377 u32 sources[LM3630A_NUM_SINKS];
378 int ret, num_sources, i;
379
380 num_sources = fwnode_property_count_u32(node, "led-sources");
381 if (num_sources < 0)
382 return default_led_sources;
383 else if (num_sources > ARRAY_SIZE(sources))
384 return -EINVAL;
385
386 ret = fwnode_property_read_u32_array(node, "led-sources", sources,
387 num_sources);
388 if (ret)
389 return ret;
390
391 for (i = 0; i < num_sources; i++) {
392 if (sources[i] < LM3630A_SINK_0 || sources[i] > LM3630A_SINK_1)
393 return -EINVAL;
394
395 ret |= BIT(sources[i]);
396 }
397
398 return ret;
399 }
400
lm3630a_parse_bank(struct lm3630a_platform_data * pdata,struct fwnode_handle * node,int * seen_led_sources)401 static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata,
402 struct fwnode_handle *node, int *seen_led_sources)
403 {
404 int led_sources, ret;
405 const char *label;
406 u32 bank, val;
407 bool linear;
408
409 ret = fwnode_property_read_u32(node, "reg", &bank);
410 if (ret)
411 return ret;
412
413 if (bank < LM3630A_BANK_0 || bank > LM3630A_BANK_1)
414 return -EINVAL;
415
416 led_sources = lm3630a_parse_led_sources(node, BIT(bank));
417 if (led_sources < 0)
418 return led_sources;
419
420 if (*seen_led_sources & led_sources)
421 return -EINVAL;
422
423 *seen_led_sources |= led_sources;
424
425 linear = fwnode_property_read_bool(node,
426 "ti,linear-mapping-mode");
427 if (bank) {
428 if (led_sources & BIT(LM3630A_SINK_0) ||
429 !(led_sources & BIT(LM3630A_SINK_1)))
430 return -EINVAL;
431
432 pdata->ledb_ctrl = linear ?
433 LM3630A_LEDB_ENABLE_LINEAR :
434 LM3630A_LEDB_ENABLE;
435 } else {
436 if (!(led_sources & BIT(LM3630A_SINK_0)))
437 return -EINVAL;
438
439 pdata->leda_ctrl = linear ?
440 LM3630A_LEDA_ENABLE_LINEAR :
441 LM3630A_LEDA_ENABLE;
442
443 if (led_sources & BIT(LM3630A_SINK_1))
444 pdata->ledb_ctrl = LM3630A_LEDB_ON_A;
445 }
446
447 ret = fwnode_property_read_string(node, "label", &label);
448 if (!ret) {
449 if (bank)
450 pdata->ledb_label = label;
451 else
452 pdata->leda_label = label;
453 }
454
455 ret = fwnode_property_read_u32(node, "default-brightness",
456 &val);
457 if (!ret) {
458 if (bank)
459 pdata->ledb_init_brt = val;
460 else
461 pdata->leda_init_brt = val;
462 }
463
464 ret = fwnode_property_read_u32(node, "max-brightness", &val);
465 if (!ret) {
466 if (bank)
467 pdata->ledb_max_brt = val;
468 else
469 pdata->leda_max_brt = val;
470 }
471
472 return 0;
473 }
474
lm3630a_parse_node(struct lm3630a_chip * pchip,struct lm3630a_platform_data * pdata)475 static int lm3630a_parse_node(struct lm3630a_chip *pchip,
476 struct lm3630a_platform_data *pdata)
477 {
478 int ret = -ENODEV, seen_led_sources = 0;
479 struct fwnode_handle *node;
480
481 device_for_each_child_node(pchip->dev, node) {
482 ret = lm3630a_parse_bank(pdata, node, &seen_led_sources);
483 if (ret)
484 return ret;
485 }
486
487 return ret;
488 }
489
lm3630a_probe(struct i2c_client * client,const struct i2c_device_id * id)490 static int lm3630a_probe(struct i2c_client *client,
491 const struct i2c_device_id *id)
492 {
493 struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
494 struct lm3630a_chip *pchip;
495 int rval;
496
497 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
498 dev_err(&client->dev, "fail : i2c functionality check\n");
499 return -EOPNOTSUPP;
500 }
501
502 pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
503 GFP_KERNEL);
504 if (!pchip)
505 return -ENOMEM;
506 pchip->dev = &client->dev;
507
508 pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
509 if (IS_ERR(pchip->regmap)) {
510 rval = PTR_ERR(pchip->regmap);
511 dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
512 return rval;
513 }
514
515 i2c_set_clientdata(client, pchip);
516 if (pdata == NULL) {
517 pdata = devm_kzalloc(pchip->dev,
518 sizeof(struct lm3630a_platform_data),
519 GFP_KERNEL);
520 if (pdata == NULL)
521 return -ENOMEM;
522
523 /* default values */
524 pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
525 pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
526 pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
527 pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
528
529 rval = lm3630a_parse_node(pchip, pdata);
530 if (rval) {
531 dev_err(&client->dev, "fail : parse node\n");
532 return rval;
533 }
534 }
535 pchip->pdata = pdata;
536
537 /* chip initialize */
538 rval = lm3630a_chip_init(pchip);
539 if (rval < 0) {
540 dev_err(&client->dev, "fail : init chip\n");
541 return rval;
542 }
543 /* backlight register */
544 rval = lm3630a_backlight_register(pchip);
545 if (rval < 0) {
546 dev_err(&client->dev, "fail : backlight register.\n");
547 return rval;
548 }
549 /* pwm */
550 if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
551 pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
552 if (IS_ERR(pchip->pwmd)) {
553 dev_err(&client->dev, "fail : get pwm device\n");
554 return PTR_ERR(pchip->pwmd);
555 }
556
557 /*
558 * FIXME: pwm_apply_args() should be removed when switching to
559 * the atomic PWM API.
560 */
561 pwm_apply_args(pchip->pwmd);
562 }
563
564 /* interrupt enable : irq 0 is not allowed */
565 pchip->irq = client->irq;
566 if (pchip->irq) {
567 rval = lm3630a_intr_config(pchip);
568 if (rval < 0)
569 return rval;
570 }
571 dev_info(&client->dev, "LM3630A backlight register OK.\n");
572 return 0;
573 }
574
lm3630a_remove(struct i2c_client * client)575 static int lm3630a_remove(struct i2c_client *client)
576 {
577 int rval;
578 struct lm3630a_chip *pchip = i2c_get_clientdata(client);
579
580 rval = lm3630a_write(pchip, REG_BRT_A, 0);
581 if (rval < 0)
582 dev_err(pchip->dev, "i2c failed to access register\n");
583
584 rval = lm3630a_write(pchip, REG_BRT_B, 0);
585 if (rval < 0)
586 dev_err(pchip->dev, "i2c failed to access register\n");
587
588 if (pchip->irq) {
589 free_irq(pchip->irq, pchip);
590 flush_workqueue(pchip->irqthread);
591 destroy_workqueue(pchip->irqthread);
592 }
593 return 0;
594 }
595
596 static const struct i2c_device_id lm3630a_id[] = {
597 {LM3630A_NAME, 0},
598 {}
599 };
600
601 static const struct of_device_id lm3630a_match_table[] = {
602 { .compatible = "ti,lm3630a", },
603 { },
604 };
605
606 MODULE_DEVICE_TABLE(i2c, lm3630a_id);
607
608 static struct i2c_driver lm3630a_i2c_driver = {
609 .driver = {
610 .name = LM3630A_NAME,
611 .of_match_table = lm3630a_match_table,
612 },
613 .probe = lm3630a_probe,
614 .remove = lm3630a_remove,
615 .id_table = lm3630a_id,
616 };
617
618 module_i2c_driver(lm3630a_i2c_driver);
619
620 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
621 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
622 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
623 MODULE_LICENSE("GPL v2");
624