1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/media/i2c/lm3560.c
4 * General device driver for TI lm3559, lm3560, FLASH LED Driver
5 *
6 * Copyright (C) 2013 Texas Instruments
7 *
8 * Contact: Daniel Jeong <gshark.jeong@gmail.com>
9 * Ldd-Mlp <ldd-mlp@list.ti.com>
10 */
11
12 #include <linux/bitmap.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/mutex.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
27
28 /* registers definitions */
29 #define REG_ENABLE 0x10
30 #define REG_TORCH_BR 0xa0
31 #define REG_FLASH_BR 0xb0
32 #define REG_FLASH_TOUT 0xc0
33 #define REG_FLAG 0xd0
34 #define REG_CONFIG1 0xe0
35
36 /* fault mask */
37 #define FAULT_TIMEOUT (1<<0)
38 #define FAULT_OVERTEMP (1<<1)
39 #define FAULT_SHORT_CIRCUIT (1<<2)
40
41 #define LM3560_FLASH_TOUT_MIN 32
42 #define LM3560_FLASH_TOUT_STEP 32
43 #define LM3560_FLASH_TOUT_MAX 1024
44 #define LM3560_FLASH_TOUT_ms_TO_REG(a) \
45 ((a) < LM3560_FLASH_TOUT_MIN ? 0 : \
46 (((a) - LM3560_FLASH_TOUT_MIN) / LM3560_FLASH_TOUT_STEP))
47 #define LM3560_FLASH_TOUT_REG_TO_ms(a) \
48 ((a) * LM3560_FLASH_TOUT_STEP + LM3560_FLASH_TOUT_MIN)
49
50 enum lm3560_led_id {
51 LM3560_LED0 = 0,
52 LM3560_LED1,
53 LM3560_LED_MAX
54 };
55
56 enum lm3560_peak_current {
57 LM3560_PEAK_1600mA = 0x00,
58 LM3560_PEAK_2300mA = 0x20,
59 LM3560_PEAK_3000mA = 0x40,
60 LM3560_PEAK_3600mA = 0x60
61 };
62
63 enum led_enable {
64 MODE_SHDN = 0x0,
65 MODE_TORCH = 0x2,
66 MODE_FLASH = 0x3,
67 };
68
69 struct lm3560_flash_config {
70 u32 flash_brt_min_ua;
71 u32 flash_brt_max_ua;
72 u32 flash_brt_step_ua;
73
74 u32 torch_brt_min_ua;
75 u32 torch_brt_max_ua;
76 u32 torch_brt_step_ua;
77 };
78
79 /**
80 * struct lm3560_flash
81 *
82 * @dev: pointer to &struct device
83 * @regmap: reg. map for i2c
84 * @lock: muxtex for serial access.
85 * @hwen_gpio: line connected to HWEN pin
86 * @vin_supply: line connected to IN supply (2.5V - 5.5V)
87 * @led_mode: V4L2 LED mode
88 * @ctrls_led: V4L2 controls
89 * @subdev_led: V4L2 subdev
90 * @led_id: LED status holder
91 * @peak: peak current
92 * @max_flash_timeout: flash timeout
93 * @max_flash_brt: flash mode led brightness
94 * @max_torch_brt: torch mode led brightness
95 * @config: device specific current configuration
96 */
97 struct lm3560_flash {
98 struct device *dev;
99 struct regmap *regmap;
100 struct mutex lock;
101
102 struct gpio_desc *hwen_gpio;
103 struct regulator *vin_supply;
104
105 enum v4l2_flash_led_mode led_mode;
106 struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
107 struct v4l2_subdev subdev_led[LM3560_LED_MAX];
108
109 DECLARE_BITMAP(led_id, LM3560_LED_MAX);
110
111 enum lm3560_peak_current peak;
112 u32 max_flash_timeout;
113
114 u32 max_flash_brt[LM3560_LED_MAX];
115 u32 max_torch_brt[LM3560_LED_MAX];
116
117 const struct lm3560_flash_config *config;
118 };
119
120 #define to_lm3560_flash(_ctrl, _no) \
121 container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
122
123 /* enable mode control */
lm3560_mode_ctrl(struct lm3560_flash * flash)124 static int lm3560_mode_ctrl(struct lm3560_flash *flash)
125 {
126 int rval = -EINVAL;
127
128 switch (flash->led_mode) {
129 case V4L2_FLASH_LED_MODE_NONE:
130 rval = regmap_update_bits(flash->regmap,
131 REG_ENABLE, 0x03, MODE_SHDN);
132 break;
133 case V4L2_FLASH_LED_MODE_TORCH:
134 rval = regmap_update_bits(flash->regmap,
135 REG_ENABLE, 0x03, MODE_TORCH);
136 break;
137 case V4L2_FLASH_LED_MODE_FLASH:
138 rval = regmap_update_bits(flash->regmap,
139 REG_ENABLE, 0x03, MODE_FLASH);
140 break;
141 }
142 return rval;
143 }
144
145 /* led1/2 enable/disable */
lm3560_enable_ctrl(struct lm3560_flash * flash,enum lm3560_led_id led_no,bool on)146 static int lm3560_enable_ctrl(struct lm3560_flash *flash,
147 enum lm3560_led_id led_no, bool on)
148 {
149 int rval;
150
151 if (led_no == LM3560_LED0) {
152 if (on)
153 rval = regmap_update_bits(flash->regmap,
154 REG_ENABLE, 0x08, 0x08);
155 else
156 rval = regmap_update_bits(flash->regmap,
157 REG_ENABLE, 0x08, 0x00);
158 } else {
159 if (on)
160 rval = regmap_update_bits(flash->regmap,
161 REG_ENABLE, 0x10, 0x10);
162 else
163 rval = regmap_update_bits(flash->regmap,
164 REG_ENABLE, 0x10, 0x00);
165 }
166 return rval;
167 }
168
169 /* torch1/2 brightness control */
lm3560_torch_brt_ctrl(struct lm3560_flash * flash,enum lm3560_led_id led_no,unsigned int brt)170 static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
171 enum lm3560_led_id led_no, unsigned int brt)
172 {
173 const struct lm3560_flash_config *config = flash->config;
174 int rval;
175 u32 br_bits;
176
177 if (brt < config->torch_brt_min_ua)
178 return lm3560_enable_ctrl(flash, led_no, false);
179 else
180 rval = lm3560_enable_ctrl(flash, led_no, true);
181
182 br_bits = clamp(brt, config->torch_brt_min_ua,
183 config->torch_brt_max_ua);
184 br_bits = (br_bits - config->torch_brt_min_ua) /
185 config->torch_brt_step_ua;
186
187 if (led_no == LM3560_LED0)
188 rval = regmap_update_bits(flash->regmap,
189 REG_TORCH_BR, 0x07, br_bits);
190 else
191 rval = regmap_update_bits(flash->regmap,
192 REG_TORCH_BR, 0x38, br_bits << 3);
193
194 return rval;
195 }
196
197 /* flash1/2 brightness control */
lm3560_flash_brt_ctrl(struct lm3560_flash * flash,enum lm3560_led_id led_no,unsigned int brt)198 static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
199 enum lm3560_led_id led_no, unsigned int brt)
200 {
201 const struct lm3560_flash_config *config = flash->config;
202 int rval;
203 u32 br_bits;
204
205 if (brt < config->flash_brt_min_ua)
206 return lm3560_enable_ctrl(flash, led_no, false);
207 else
208 rval = lm3560_enable_ctrl(flash, led_no, true);
209
210 br_bits = clamp(brt, config->flash_brt_min_ua,
211 config->flash_brt_max_ua);
212 br_bits = (br_bits - config->flash_brt_min_ua) /
213 config->flash_brt_step_ua;
214
215 if (led_no == LM3560_LED0)
216 rval = regmap_update_bits(flash->regmap,
217 REG_FLASH_BR, 0x0f, br_bits);
218 else
219 rval = regmap_update_bits(flash->regmap,
220 REG_FLASH_BR, 0xf0, br_bits << 4);
221
222 return rval;
223 }
224
225 /* v4l2 controls */
lm3560_get_ctrl(struct v4l2_ctrl * ctrl,enum lm3560_led_id led_no)226 static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
227 {
228 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
229 int rval = -EINVAL;
230
231 if (!pm_runtime_get_if_active(flash->dev))
232 return 0;
233
234 if (ctrl->id == V4L2_CID_FLASH_FAULT) {
235 s32 fault = 0;
236 unsigned int reg_val;
237 rval = regmap_read(flash->regmap, REG_FLAG, ®_val);
238 if (rval < 0) {
239 pm_runtime_put(flash->dev);
240 return rval;
241 }
242 if (reg_val & FAULT_SHORT_CIRCUIT)
243 fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
244 if (reg_val & FAULT_OVERTEMP)
245 fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
246 if (reg_val & FAULT_TIMEOUT)
247 fault |= V4L2_FLASH_FAULT_TIMEOUT;
248 ctrl->cur.val = fault;
249 }
250
251 pm_runtime_put(flash->dev);
252
253 return rval;
254 }
255
lm3560_set_ctrl(struct v4l2_ctrl * ctrl,enum lm3560_led_id led_no)256 static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
257 {
258 struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
259 u8 tout_bits;
260 int rval = -EINVAL;
261
262 if (!pm_runtime_get_if_active(flash->dev))
263 return 0;
264
265 switch (ctrl->id) {
266 case V4L2_CID_FLASH_LED_MODE:
267 flash->led_mode = ctrl->val;
268 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
269 rval = lm3560_mode_ctrl(flash);
270 break;
271
272 case V4L2_CID_FLASH_STROBE_SOURCE:
273 rval = regmap_update_bits(flash->regmap,
274 REG_CONFIG1, 0x04, (ctrl->val) << 2);
275 break;
276
277 case V4L2_CID_FLASH_STROBE:
278 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
279 rval = -EBUSY;
280 break;
281 }
282 flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
283 rval = lm3560_mode_ctrl(flash);
284 break;
285
286 case V4L2_CID_FLASH_STROBE_STOP:
287 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
288 rval = -EBUSY;
289 break;
290 }
291 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
292 rval = lm3560_mode_ctrl(flash);
293 break;
294
295 case V4L2_CID_FLASH_TIMEOUT:
296 tout_bits = LM3560_FLASH_TOUT_ms_TO_REG(ctrl->val);
297 rval = regmap_update_bits(flash->regmap,
298 REG_FLASH_TOUT, 0x1f, tout_bits);
299 break;
300
301 case V4L2_CID_FLASH_INTENSITY:
302 rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
303 break;
304
305 case V4L2_CID_FLASH_TORCH_INTENSITY:
306 rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
307 break;
308 }
309
310 pm_runtime_put(flash->dev);
311
312 return rval;
313 }
314
lm3560_led1_get_ctrl(struct v4l2_ctrl * ctrl)315 static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
316 {
317 return lm3560_get_ctrl(ctrl, LM3560_LED1);
318 }
319
lm3560_led1_set_ctrl(struct v4l2_ctrl * ctrl)320 static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
321 {
322 return lm3560_set_ctrl(ctrl, LM3560_LED1);
323 }
324
lm3560_led0_get_ctrl(struct v4l2_ctrl * ctrl)325 static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
326 {
327 return lm3560_get_ctrl(ctrl, LM3560_LED0);
328 }
329
lm3560_led0_set_ctrl(struct v4l2_ctrl * ctrl)330 static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
331 {
332 return lm3560_set_ctrl(ctrl, LM3560_LED0);
333 }
334
335 static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops[LM3560_LED_MAX] = {
336 [LM3560_LED0] = {
337 .g_volatile_ctrl = lm3560_led0_get_ctrl,
338 .s_ctrl = lm3560_led0_set_ctrl,
339 },
340 [LM3560_LED1] = {
341 .g_volatile_ctrl = lm3560_led1_get_ctrl,
342 .s_ctrl = lm3560_led1_set_ctrl,
343 }
344 };
345
lm3560_init_controls(struct lm3560_flash * flash,enum lm3560_led_id led_no)346 static int lm3560_init_controls(struct lm3560_flash *flash,
347 enum lm3560_led_id led_no)
348 {
349 struct v4l2_ctrl *fault;
350 u32 max_flash_brt = flash->max_flash_brt[led_no];
351 u32 max_torch_brt = flash->max_torch_brt[led_no];
352 struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no];
353 const struct v4l2_ctrl_ops *ops = &lm3560_led_ctrl_ops[led_no];
354 const struct lm3560_flash_config *config = flash->config;
355
356 v4l2_ctrl_handler_init(hdl, 8);
357
358 /* flash mode */
359 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
360 V4L2_FLASH_LED_MODE_TORCH, ~0x7,
361 V4L2_FLASH_LED_MODE_NONE);
362 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
363
364 /* flash source */
365 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
366 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
367
368 /* flash strobe */
369 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
370
371 /* flash strobe stop */
372 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
373
374 /* flash strobe timeout */
375 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
376 LM3560_FLASH_TOUT_MIN,
377 flash->max_flash_timeout,
378 LM3560_FLASH_TOUT_STEP,
379 flash->max_flash_timeout);
380
381 /* flash brt */
382 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
383 config->flash_brt_min_ua, max_flash_brt,
384 config->flash_brt_step_ua, max_flash_brt);
385
386 /* torch brt */
387 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
388 config->torch_brt_min_ua, max_torch_brt,
389 config->torch_brt_step_ua, max_torch_brt);
390
391 /* fault */
392 fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
393 V4L2_FLASH_FAULT_OVER_VOLTAGE
394 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
395 | V4L2_FLASH_FAULT_SHORT_CIRCUIT
396 | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
397 if (fault != NULL)
398 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
399
400 hdl->lock = &flash->lock;
401
402 if (hdl->error)
403 return hdl->error;
404
405 flash->subdev_led[led_no].ctrl_handler = hdl;
406 return 0;
407 }
408
409 /* initialize device */
410 static const struct v4l2_subdev_ops lm3560_ops = {
411 .core = NULL,
412 };
413
414 static const struct regmap_config lm3560_regmap = {
415 .reg_bits = 8,
416 .val_bits = 8,
417 .max_register = 0xFF,
418 };
419
lm3560_subdev_init(struct lm3560_flash * flash,enum lm3560_led_id led_no,struct fwnode_handle * fwnode)420 static int lm3560_subdev_init(struct lm3560_flash *flash,
421 enum lm3560_led_id led_no,
422 struct fwnode_handle *fwnode)
423 {
424 struct i2c_client *client = to_i2c_client(flash->dev);
425 int rval;
426
427 v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3560_ops);
428 flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
429 snprintf(flash->subdev_led[led_no].name,
430 sizeof(flash->subdev_led[led_no].name),
431 "lm3560-led%d", led_no);
432 flash->subdev_led[led_no].fwnode = fwnode_handle_get(fwnode);
433 rval = lm3560_init_controls(flash, led_no);
434 if (rval)
435 goto err_out;
436 rval = media_entity_pads_init(&flash->subdev_led[led_no].entity, 0, NULL);
437 if (rval < 0)
438 goto err_out;
439 flash->subdev_led[led_no].entity.function = MEDIA_ENT_F_FLASH;
440 flash->subdev_led[led_no].state_lock = &flash->lock;
441
442 rval = v4l2_async_register_subdev(&flash->subdev_led[led_no]);
443 if (rval < 0) {
444 dev_err(flash->dev, "failed to register V4L2 subdev");
445 goto error_out_media;
446 }
447
448 return rval;
449 error_out_media:
450 media_entity_cleanup(&flash->subdev_led[led_no].entity);
451 err_out:
452 v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
453 return rval;
454 }
455
lm3560_init_device(struct lm3560_flash * flash)456 static int lm3560_init_device(struct lm3560_flash *flash)
457 {
458 int rval;
459 unsigned int reg_val;
460
461 /* set peak current */
462 rval = regmap_update_bits(flash->regmap,
463 REG_FLASH_TOUT, 0x60, flash->peak);
464 if (rval < 0)
465 return rval;
466 /* output disable */
467 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
468 rval = lm3560_mode_ctrl(flash);
469 if (rval < 0)
470 return rval;
471 /* reset faults */
472 rval = regmap_read(flash->regmap, REG_FLAG, ®_val);
473 return rval;
474 }
475
lm3560_power_off(struct device * dev)476 static int __maybe_unused lm3560_power_off(struct device *dev)
477 {
478 struct lm3560_flash *flash = dev_get_drvdata(dev);
479
480 gpiod_set_value_cansleep(flash->hwen_gpio, 0);
481 regulator_disable(flash->vin_supply);
482
483 return 0;
484 }
485
lm3560_power_on(struct device * dev)486 static int __maybe_unused lm3560_power_on(struct device *dev)
487 {
488 struct lm3560_flash *flash = dev_get_drvdata(dev);
489 int rval;
490
491 rval = regulator_enable(flash->vin_supply);
492 if (rval < 0) {
493 dev_err(flash->dev, "failed to enable vin power supply\n");
494 return rval;
495 }
496
497 gpiod_set_value_cansleep(flash->hwen_gpio, 1);
498
499 rval = lm3560_init_device(flash);
500 if (rval < 0) {
501 lm3560_power_off(dev);
502 return rval;
503 }
504
505 return 0;
506 }
507
lm3560_subdev_cleanup(struct lm3560_flash * flash)508 static void lm3560_subdev_cleanup(struct lm3560_flash *flash)
509 {
510 int led_no;
511
512 for_each_set_bit(led_no, flash->led_id, LM3560_LED_MAX) {
513 v4l2_async_unregister_subdev(&flash->subdev_led[led_no]);
514 v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
515 media_entity_cleanup(&flash->subdev_led[led_no].entity);
516 }
517 }
518
lm3560_probe(struct i2c_client * client)519 static int lm3560_probe(struct i2c_client *client)
520 {
521 struct lm3560_flash *flash;
522 u32 peak_ua;
523 int rval;
524
525 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
526 if (flash == NULL)
527 return -ENOMEM;
528
529 flash->config = device_get_match_data(&client->dev);
530 if (!flash->config)
531 return -ENODEV;
532
533 flash->regmap = devm_regmap_init_i2c(client, &lm3560_regmap);
534 if (IS_ERR(flash->regmap)) {
535 rval = PTR_ERR(flash->regmap);
536 return rval;
537 }
538
539 flash->dev = &client->dev;
540 mutex_init(&flash->lock);
541
542 bitmap_zero(flash->led_id, LM3560_LED_MAX);
543
544 flash->hwen_gpio = devm_gpiod_get_optional(flash->dev, "enable",
545 GPIOD_OUT_LOW);
546 if (IS_ERR(flash->hwen_gpio))
547 return dev_err_probe(flash->dev, PTR_ERR(flash->hwen_gpio),
548 "failed to get hwen gpio\n");
549
550 flash->vin_supply = devm_regulator_get(flash->dev, "vin");
551 if (IS_ERR(flash->vin_supply))
552 return dev_err_probe(flash->dev, PTR_ERR(flash->vin_supply),
553 "failed to get vin-supply\n");
554
555 flash->peak = LM3560_PEAK_1600mA;
556 rval = device_property_read_u32(flash->dev,
557 "ti,peak-current-microamp", &peak_ua);
558 if (!rval) {
559 /*
560 * LM3559 has lower peak current limit, but
561 * bit configuration matches LM3560.
562 * Correct current restrictions are enforced
563 * by the LM3560 schema.
564 */
565 switch (peak_ua) {
566 case 1400000:
567 case 1600000:
568 flash->peak = LM3560_PEAK_1600mA;
569 break;
570 case 2100000:
571 case 2300000:
572 flash->peak = LM3560_PEAK_2300mA;
573 break;
574 case 2700000:
575 case 3000000:
576 flash->peak = LM3560_PEAK_3000mA;
577 break;
578 case 3200000:
579 case 3600000:
580 flash->peak = LM3560_PEAK_3600mA;
581 break;
582 default:
583 return -EINVAL;
584 }
585 }
586
587 flash->max_flash_timeout = LM3560_FLASH_TOUT_MIN * 1000;
588 device_property_read_u32(flash->dev, "flash-max-timeout-us",
589 &flash->max_flash_timeout);
590 flash->max_flash_timeout /= 1000;
591
592 rval = regulator_enable(flash->vin_supply);
593 if (rval < 0)
594 return dev_err_probe(flash->dev, rval,
595 "failed to enable vin power supply\n");
596
597 gpiod_set_value_cansleep(flash->hwen_gpio, 1);
598
599 rval = lm3560_init_device(flash);
600 if (rval < 0)
601 goto error_disable;
602
603 pm_runtime_set_active(flash->dev);
604 pm_runtime_enable(flash->dev);
605
606 device_for_each_child_node_scoped(flash->dev, node) {
607 const struct lm3560_flash_config *config = flash->config;
608 u32 reg;
609
610 rval = fwnode_property_read_u32(node, "reg", ®);
611 if (rval < 0)
612 /* We care only about nodes with reg property */
613 continue;
614
615 if (reg == LM3560_LED0 || reg == LM3560_LED1) {
616 flash->max_flash_brt[reg] = config->flash_brt_min_ua;
617 fwnode_property_read_u32(node, "flash-max-microamp",
618 &flash->max_flash_brt[reg]);
619
620 flash->max_torch_brt[reg] = config->torch_brt_min_ua;
621 fwnode_property_read_u32(node, "led-max-microamp",
622 &flash->max_torch_brt[reg]);
623
624 rval = lm3560_subdev_init(flash, reg, node);
625 if (rval < 0) {
626 dev_err(flash->dev,
627 "failed to register led%d: %d\n",
628 reg, rval);
629 goto error_clean;
630 }
631
632 set_bit(reg, flash->led_id);
633 }
634 }
635
636 i2c_set_clientdata(client, flash);
637
638 pm_runtime_set_autosuspend_delay(flash->dev, 1000);
639 pm_runtime_use_autosuspend(flash->dev);
640 pm_runtime_idle(flash->dev);
641
642 return 0;
643
644 error_clean:
645 pm_runtime_disable(flash->dev);
646 pm_runtime_set_suspended(flash->dev);
647
648 lm3560_subdev_cleanup(flash);
649
650 error_disable:
651 gpiod_set_value_cansleep(flash->hwen_gpio, 0);
652 regulator_disable(flash->vin_supply);
653
654 return rval;
655 }
656
lm3560_remove(struct i2c_client * client)657 static void lm3560_remove(struct i2c_client *client)
658 {
659 struct lm3560_flash *flash = i2c_get_clientdata(client);
660
661 lm3560_subdev_cleanup(flash);
662
663 /*
664 * Disable runtime PM. In case runtime PM is disabled in the kernel,
665 * make sure to turn power off manually.
666 */
667 pm_runtime_disable(&client->dev);
668 if (!pm_runtime_status_suspended(&client->dev)) {
669 lm3560_power_off(&client->dev);
670 pm_runtime_set_suspended(&client->dev);
671 }
672 }
673
674 static const struct dev_pm_ops lm3560_pm_ops = {
675 SET_RUNTIME_PM_OPS(lm3560_power_off, lm3560_power_on, NULL)
676 };
677
678 static const struct lm3560_flash_config lm3559_config = {
679 .flash_brt_min_ua = 56250,
680 .flash_brt_max_ua = 900000,
681 .flash_brt_step_ua = 56250,
682
683 .torch_brt_min_ua = 28125,
684 .torch_brt_max_ua = 225000,
685 .torch_brt_step_ua = 28125,
686 };
687
688 static const struct lm3560_flash_config lm3560_config = {
689 .flash_brt_min_ua = 62500,
690 .flash_brt_max_ua = 1000000,
691 .flash_brt_step_ua = 62500,
692
693 .torch_brt_min_ua = 31250,
694 .torch_brt_max_ua = 250000,
695 .torch_brt_step_ua = 31250,
696 };
697
698 static const struct of_device_id lm3560_of_match[] = {
699 { .compatible = "ti,lm3559", .data = &lm3559_config },
700 { .compatible = "ti,lm3560", .data = &lm3560_config },
701 { }
702 };
703 MODULE_DEVICE_TABLE(of, lm3560_of_match);
704
705 static const struct i2c_device_id lm3560_id_table[] = {
706 { .name = "lm3559", .driver_data = (kernel_ulong_t)&lm3559_config },
707 { .name = "lm3560", .driver_data = (kernel_ulong_t)&lm3560_config },
708 { }
709 };
710 MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
711
712 static struct i2c_driver lm3560_i2c_driver = {
713 .driver = {
714 .name = "lm3560",
715 .pm = pm_ptr(&lm3560_pm_ops),
716 .of_match_table = lm3560_of_match,
717 },
718 .probe = lm3560_probe,
719 .remove = lm3560_remove,
720 .id_table = lm3560_id_table,
721 };
722
723 module_i2c_driver(lm3560_i2c_driver);
724
725 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
726 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
727 MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
728 MODULE_LICENSE("GPL");
729