Linux 4.2.1
[linux/fpc-iii.git] / drivers / leds / leds-max77693.c
blobb8b0eec7b54058cb1c671e4e76739277589406aa
1 /*
2 * LED Flash class driver for the flash cell of max77693 mfd.
4 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
6 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
14 #include <linux/led-class-flash.h>
15 #include <linux/mfd/max77693.h>
16 #include <linux/mfd/max77693-private.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-flash-led-class.h>
25 #define MODE_OFF 0
26 #define MODE_FLASH(a) (1 << (a))
27 #define MODE_TORCH(a) (1 << (2 + (a)))
28 #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
30 #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
31 MODE_FLASH_EXTERNAL(FLED1) | \
32 MODE_FLASH_EXTERNAL(FLED2))
33 #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
35 #define FLED1_IOUT (1 << 0)
36 #define FLED2_IOUT (1 << 1)
38 enum max77693_fled {
39 FLED1,
40 FLED2,
43 enum max77693_led_mode {
44 FLASH,
45 TORCH,
48 struct max77693_led_config_data {
49 const char *label[2];
50 u32 iout_torch_max[2];
51 u32 iout_flash_max[2];
52 u32 flash_timeout_max[2];
53 u32 num_leds;
54 u32 boost_mode;
55 u32 boost_vout;
56 u32 low_vsys;
59 struct max77693_sub_led {
60 /* corresponding FLED output identifier */
61 int fled_id;
62 /* corresponding LED Flash class device */
63 struct led_classdev_flash fled_cdev;
64 /* assures led-triggers compatibility */
65 struct work_struct work_brightness_set;
66 /* V4L2 Flash device */
67 struct v4l2_flash *v4l2_flash;
69 /* brightness cache */
70 unsigned int torch_brightness;
71 /* flash timeout cache */
72 unsigned int flash_timeout;
73 /* flash faults that may have occurred */
74 u32 flash_faults;
77 struct max77693_led_device {
78 /* parent mfd regmap */
79 struct regmap *regmap;
80 /* platform device data */
81 struct platform_device *pdev;
82 /* secures access to the device */
83 struct mutex lock;
85 /* sub led data */
86 struct max77693_sub_led sub_leds[2];
88 /* maximum torch current values for FLED outputs */
89 u32 iout_torch_max[2];
90 /* maximum flash current values for FLED outputs */
91 u32 iout_flash_max[2];
93 /* current flash timeout cache */
94 unsigned int current_flash_timeout;
95 /* ITORCH register cache */
96 u8 torch_iout_reg;
97 /* mode of fled outputs */
98 unsigned int mode_flags;
99 /* recently strobed fled */
100 int strobing_sub_led_id;
101 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
102 u8 fled_mask;
103 /* FLED modes that can be set */
104 u8 allowed_modes;
106 /* arrangement of current outputs */
107 bool iout_joint;
110 static u8 max77693_led_iout_to_reg(u32 ua)
112 if (ua < FLASH_IOUT_MIN)
113 ua = FLASH_IOUT_MIN;
114 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
117 static u8 max77693_flash_timeout_to_reg(u32 us)
119 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
122 static inline struct max77693_sub_led *flcdev_to_sub_led(
123 struct led_classdev_flash *fled_cdev)
125 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
128 static inline struct max77693_led_device *sub_led_to_led(
129 struct max77693_sub_led *sub_led)
131 return container_of(sub_led, struct max77693_led_device,
132 sub_leds[sub_led->fled_id]);
135 static inline u8 max77693_led_vsys_to_reg(u32 mv)
137 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
140 static inline u8 max77693_led_vout_to_reg(u32 mv)
142 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
145 static inline bool max77693_fled_used(struct max77693_led_device *led,
146 int fled_id)
148 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
150 return led->fled_mask & fled_bit;
153 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
155 struct regmap *rmap = led->regmap;
156 int ret, v = 0, i;
158 for (i = FLED1; i <= FLED2; ++i) {
159 if (mode & MODE_TORCH(i))
160 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
162 if (mode & MODE_FLASH(i)) {
163 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
164 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
165 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
167 * Enable hw triggering also for torch mode, as some
168 * camera sensors use torch led to fathom ambient light
169 * conditions before strobing the flash.
171 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
175 /* Reset the register only prior setting flash modes */
176 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
177 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
178 if (ret < 0)
179 return ret;
182 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
185 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
187 u8 new_mode_flags;
188 int i, ret;
190 if (led->iout_joint)
191 /* Span the mode on FLED2 for joint iouts case */
192 mode |= (mode << 1);
195 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
196 * Corresponding register bit fields interfere with SW triggered modes,
197 * thus clear them to ensure proper device configuration.
199 for (i = FLED1; i <= FLED2; ++i)
200 if (mode & MODE_FLASH_EXTERNAL(i))
201 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
203 new_mode_flags = mode | led->mode_flags;
204 new_mode_flags &= led->allowed_modes;
206 if (new_mode_flags ^ led->mode_flags)
207 led->mode_flags = new_mode_flags;
208 else
209 return 0;
211 ret = max77693_set_mode_reg(led, led->mode_flags);
212 if (ret < 0)
213 return ret;
216 * Clear flash mode flag after setting the mode to avoid spurious flash
217 * strobing on each subsequent torch mode setting.
219 if (mode & MODE_FLASH_MASK)
220 led->mode_flags &= ~mode;
222 return ret;
225 static int max77693_clear_mode(struct max77693_led_device *led,
226 u8 mode)
228 if (led->iout_joint)
229 /* Clear mode also on FLED2 for joint iouts case */
230 mode |= (mode << 1);
232 led->mode_flags &= ~mode;
234 return max77693_set_mode_reg(led, led->mode_flags);
237 static void max77693_add_allowed_modes(struct max77693_led_device *led,
238 int fled_id, enum max77693_led_mode mode)
240 if (mode == FLASH)
241 led->allowed_modes |= (MODE_FLASH(fled_id) |
242 MODE_FLASH_EXTERNAL(fled_id));
243 else
244 led->allowed_modes |= MODE_TORCH(fled_id);
247 static void max77693_distribute_currents(struct max77693_led_device *led,
248 int fled_id, enum max77693_led_mode mode,
249 u32 micro_amp, u32 iout_max[2], u32 iout[2])
251 if (!led->iout_joint) {
252 iout[fled_id] = micro_amp;
253 max77693_add_allowed_modes(led, fled_id, mode);
254 return;
257 iout[FLED1] = min(micro_amp, iout_max[FLED1]);
258 iout[FLED2] = micro_amp - iout[FLED1];
260 if (mode == FLASH)
261 led->allowed_modes &= ~MODE_FLASH_MASK;
262 else
263 led->allowed_modes &= ~MODE_TORCH_MASK;
265 max77693_add_allowed_modes(led, FLED1, mode);
267 if (iout[FLED2])
268 max77693_add_allowed_modes(led, FLED2, mode);
271 static int max77693_set_torch_current(struct max77693_led_device *led,
272 int fled_id, u32 micro_amp)
274 struct regmap *rmap = led->regmap;
275 u8 iout1_reg = 0, iout2_reg = 0;
276 u32 iout[2];
278 max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
279 led->iout_torch_max, iout);
281 if (fled_id == FLED1 || led->iout_joint) {
282 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
283 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
285 if (fled_id == FLED2 || led->iout_joint) {
286 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
287 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
290 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
291 (iout2_reg << TORCH_IOUT2_SHIFT));
293 return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
294 led->torch_iout_reg);
297 static int max77693_set_flash_current(struct max77693_led_device *led,
298 int fled_id,
299 u32 micro_amp)
301 struct regmap *rmap = led->regmap;
302 u8 iout1_reg, iout2_reg;
303 u32 iout[2];
304 int ret = -EINVAL;
306 max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
307 led->iout_flash_max, iout);
309 if (fled_id == FLED1 || led->iout_joint) {
310 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
311 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
312 iout1_reg);
313 if (ret < 0)
314 return ret;
316 if (fled_id == FLED2 || led->iout_joint) {
317 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
318 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
319 iout2_reg);
322 return ret;
325 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
327 struct regmap *rmap = led->regmap;
328 u8 v;
329 int ret;
331 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
333 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
334 if (ret < 0)
335 return ret;
337 led->current_flash_timeout = microsec;
339 return 0;
342 static int max77693_get_strobe_status(struct max77693_led_device *led,
343 bool *state)
345 struct regmap *rmap = led->regmap;
346 unsigned int v;
347 int ret;
349 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
350 if (ret < 0)
351 return ret;
353 *state = v & FLASH_STATUS_FLASH_ON;
355 return ret;
358 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
360 struct max77693_led_device *led = sub_led_to_led(sub_led);
361 struct regmap *rmap = led->regmap;
362 unsigned int v;
363 u8 fault_open_mask, fault_short_mask;
364 int ret;
366 sub_led->flash_faults = 0;
368 if (led->iout_joint) {
369 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
370 fault_short_mask = FLASH_INT_FLED1_SHORT |
371 FLASH_INT_FLED2_SHORT;
372 } else {
373 fault_open_mask = (sub_led->fled_id == FLED1) ?
374 FLASH_INT_FLED1_OPEN :
375 FLASH_INT_FLED2_OPEN;
376 fault_short_mask = (sub_led->fled_id == FLED1) ?
377 FLASH_INT_FLED1_SHORT :
378 FLASH_INT_FLED2_SHORT;
381 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
382 if (ret < 0)
383 return ret;
385 if (v & fault_open_mask)
386 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
387 if (v & fault_short_mask)
388 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
389 if (v & FLASH_INT_OVER_CURRENT)
390 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
392 return 0;
395 static int max77693_setup(struct max77693_led_device *led,
396 struct max77693_led_config_data *led_cfg)
398 struct regmap *rmap = led->regmap;
399 int i, first_led, last_led, ret;
400 u32 max_flash_curr[2];
401 u8 v;
404 * Initialize only flash current. Torch current doesn't
405 * require initialization as ITORCH register is written with
406 * new value each time brightness_set op is called.
408 if (led->iout_joint) {
409 first_led = FLED1;
410 last_led = FLED1;
411 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
412 led_cfg->iout_flash_max[FLED2];
413 } else {
414 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
415 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
416 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
417 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
420 for (i = first_led; i <= last_led; ++i) {
421 ret = max77693_set_flash_current(led, i,
422 max_flash_curr[i]);
423 if (ret < 0)
424 return ret;
427 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
428 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
429 if (ret < 0)
430 return ret;
432 if (led_cfg->low_vsys > 0)
433 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
434 MAX_FLASH1_MAX_FL_EN;
435 else
436 v = 0;
438 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
439 if (ret < 0)
440 return ret;
441 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
442 if (ret < 0)
443 return ret;
445 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
446 v = FLASH_BOOST_FIXED;
447 else
448 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
450 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
451 v |= FLASH_BOOST_LEDNUM_2;
453 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
454 if (ret < 0)
455 return ret;
457 v = max77693_led_vout_to_reg(led_cfg->boost_vout);
458 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
459 if (ret < 0)
460 return ret;
462 return max77693_set_mode_reg(led, MODE_OFF);
465 static int __max77693_led_brightness_set(struct max77693_led_device *led,
466 int fled_id, enum led_brightness value)
468 int ret;
470 mutex_lock(&led->lock);
472 if (value == 0) {
473 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
474 if (ret < 0)
475 dev_dbg(&led->pdev->dev,
476 "Failed to clear torch mode (%d)\n",
477 ret);
478 goto unlock;
481 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
482 if (ret < 0) {
483 dev_dbg(&led->pdev->dev,
484 "Failed to set torch current (%d)\n",
485 ret);
486 goto unlock;
489 ret = max77693_add_mode(led, MODE_TORCH(fled_id));
490 if (ret < 0)
491 dev_dbg(&led->pdev->dev,
492 "Failed to set torch mode (%d)\n",
493 ret);
494 unlock:
495 mutex_unlock(&led->lock);
496 return ret;
499 static void max77693_led_brightness_set_work(
500 struct work_struct *work)
502 struct max77693_sub_led *sub_led =
503 container_of(work, struct max77693_sub_led,
504 work_brightness_set);
505 struct max77693_led_device *led = sub_led_to_led(sub_led);
507 __max77693_led_brightness_set(led, sub_led->fled_id,
508 sub_led->torch_brightness);
511 /* LED subsystem callbacks */
513 static int max77693_led_brightness_set_sync(
514 struct led_classdev *led_cdev,
515 enum led_brightness value)
517 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
518 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
519 struct max77693_led_device *led = sub_led_to_led(sub_led);
521 return __max77693_led_brightness_set(led, sub_led->fled_id, value);
524 static void max77693_led_brightness_set(
525 struct led_classdev *led_cdev,
526 enum led_brightness value)
528 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
529 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
531 sub_led->torch_brightness = value;
532 schedule_work(&sub_led->work_brightness_set);
535 static int max77693_led_flash_brightness_set(
536 struct led_classdev_flash *fled_cdev,
537 u32 brightness)
539 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
540 struct max77693_led_device *led = sub_led_to_led(sub_led);
541 int ret;
543 mutex_lock(&led->lock);
544 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
545 mutex_unlock(&led->lock);
547 return ret;
550 static int max77693_led_flash_strobe_set(
551 struct led_classdev_flash *fled_cdev,
552 bool state)
554 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
555 struct max77693_led_device *led = sub_led_to_led(sub_led);
556 int fled_id = sub_led->fled_id;
557 int ret;
559 mutex_lock(&led->lock);
561 if (!state) {
562 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
563 goto unlock;
566 if (sub_led->flash_timeout != led->current_flash_timeout) {
567 ret = max77693_set_timeout(led, sub_led->flash_timeout);
568 if (ret < 0)
569 goto unlock;
572 led->strobing_sub_led_id = fled_id;
574 ret = max77693_add_mode(led, MODE_FLASH(fled_id));
575 if (ret < 0)
576 goto unlock;
578 ret = max77693_get_flash_faults(sub_led);
580 unlock:
581 mutex_unlock(&led->lock);
582 return ret;
585 static int max77693_led_flash_fault_get(
586 struct led_classdev_flash *fled_cdev,
587 u32 *fault)
589 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
591 *fault = sub_led->flash_faults;
593 return 0;
596 static int max77693_led_flash_strobe_get(
597 struct led_classdev_flash *fled_cdev,
598 bool *state)
600 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
601 struct max77693_led_device *led = sub_led_to_led(sub_led);
602 int ret;
604 if (!state)
605 return -EINVAL;
607 mutex_lock(&led->lock);
609 ret = max77693_get_strobe_status(led, state);
611 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
613 mutex_unlock(&led->lock);
615 return ret;
618 static int max77693_led_flash_timeout_set(
619 struct led_classdev_flash *fled_cdev,
620 u32 timeout)
622 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
623 struct max77693_led_device *led = sub_led_to_led(sub_led);
625 mutex_lock(&led->lock);
626 sub_led->flash_timeout = timeout;
627 mutex_unlock(&led->lock);
629 return 0;
632 static int max77693_led_parse_dt(struct max77693_led_device *led,
633 struct max77693_led_config_data *cfg,
634 struct device_node **sub_nodes)
636 struct device *dev = &led->pdev->dev;
637 struct max77693_sub_led *sub_leds = led->sub_leds;
638 struct device_node *node = dev->of_node, *child_node;
639 struct property *prop;
640 u32 led_sources[2];
641 int i, ret, fled_id;
643 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
644 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
645 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
647 for_each_available_child_of_node(node, child_node) {
648 prop = of_find_property(child_node, "led-sources", NULL);
649 if (prop) {
650 const __be32 *srcs = NULL;
652 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
653 srcs = of_prop_next_u32(prop, srcs,
654 &led_sources[i]);
655 if (!srcs)
656 break;
658 } else {
659 dev_err(dev,
660 "led-sources DT property missing\n");
661 of_node_put(child_node);
662 return -EINVAL;
665 if (i == 2) {
666 fled_id = FLED1;
667 led->fled_mask = FLED1_IOUT | FLED2_IOUT;
668 } else if (led_sources[0] == FLED1) {
669 fled_id = FLED1;
670 led->fled_mask |= FLED1_IOUT;
671 } else if (led_sources[0] == FLED2) {
672 fled_id = FLED2;
673 led->fled_mask |= FLED2_IOUT;
674 } else {
675 dev_err(dev,
676 "Wrong led-sources DT property value.\n");
677 of_node_put(child_node);
678 return -EINVAL;
681 if (sub_nodes[fled_id]) {
682 dev_err(dev,
683 "Conflicting \"led-sources\" DT properties\n");
684 return -EINVAL;
687 sub_nodes[fled_id] = child_node;
688 sub_leds[fled_id].fled_id = fled_id;
690 cfg->label[fled_id] =
691 of_get_property(child_node, "label", NULL) ? :
692 child_node->name;
694 ret = of_property_read_u32(child_node, "led-max-microamp",
695 &cfg->iout_torch_max[fled_id]);
696 if (ret < 0) {
697 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
698 dev_warn(dev, "led-max-microamp DT property missing\n");
701 ret = of_property_read_u32(child_node, "flash-max-microamp",
702 &cfg->iout_flash_max[fled_id]);
703 if (ret < 0) {
704 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
705 dev_warn(dev,
706 "flash-max-microamp DT property missing\n");
709 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
710 &cfg->flash_timeout_max[fled_id]);
711 if (ret < 0) {
712 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
713 dev_warn(dev,
714 "flash-max-timeout-us DT property missing\n");
717 if (++cfg->num_leds == 2 ||
718 (max77693_fled_used(led, FLED1) &&
719 max77693_fled_used(led, FLED2))) {
720 of_node_put(child_node);
721 break;
725 if (cfg->num_leds == 0) {
726 dev_err(dev, "No DT child node found for connected LED(s).\n");
727 return -EINVAL;
730 return 0;
733 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
735 *v = clamp_val(*v, min, max);
736 if (step > 1)
737 *v = (*v - min) / step * step + min;
740 static void max77693_align_iout_current(struct max77693_led_device *led,
741 u32 *iout, u32 min, u32 max, u32 step)
743 int i;
745 if (led->iout_joint) {
746 if (iout[FLED1] > min) {
747 iout[FLED1] /= 2;
748 iout[FLED2] = iout[FLED1];
749 } else {
750 iout[FLED1] = min;
751 iout[FLED2] = 0;
752 return;
756 for (i = FLED1; i <= FLED2; ++i)
757 if (max77693_fled_used(led, i))
758 clamp_align(&iout[i], min, max, step);
759 else
760 iout[i] = 0;
763 static void max77693_led_validate_configuration(struct max77693_led_device *led,
764 struct max77693_led_config_data *cfg)
766 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
767 FLASH_IOUT_MAX_1LED;
768 int i;
770 if (cfg->num_leds == 1 &&
771 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
772 led->iout_joint = true;
774 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
775 MAX77693_LED_BOOST_FIXED);
777 /* Boost must be enabled if both current outputs are used */
778 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
779 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
781 max77693_align_iout_current(led, cfg->iout_torch_max,
782 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
784 max77693_align_iout_current(led, cfg->iout_flash_max,
785 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
787 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
788 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
789 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
791 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
792 FLASH_VOUT_STEP);
794 if (cfg->low_vsys)
795 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
796 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
799 static int max77693_led_get_configuration(struct max77693_led_device *led,
800 struct max77693_led_config_data *cfg,
801 struct device_node **sub_nodes)
803 int ret;
805 ret = max77693_led_parse_dt(led, cfg, sub_nodes);
806 if (ret < 0)
807 return ret;
809 max77693_led_validate_configuration(led, cfg);
811 memcpy(led->iout_torch_max, cfg->iout_torch_max,
812 sizeof(led->iout_torch_max));
813 memcpy(led->iout_flash_max, cfg->iout_flash_max,
814 sizeof(led->iout_flash_max));
816 return 0;
819 static const struct led_flash_ops flash_ops = {
820 .flash_brightness_set = max77693_led_flash_brightness_set,
821 .strobe_set = max77693_led_flash_strobe_set,
822 .strobe_get = max77693_led_flash_strobe_get,
823 .timeout_set = max77693_led_flash_timeout_set,
824 .fault_get = max77693_led_flash_fault_get,
827 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
828 struct max77693_led_config_data *led_cfg)
830 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
831 struct max77693_led_device *led = sub_led_to_led(sub_led);
832 int fled_id = sub_led->fled_id;
833 struct led_flash_setting *setting;
835 /* Init flash intensity setting */
836 setting = &fled_cdev->brightness;
837 setting->min = FLASH_IOUT_MIN;
838 setting->max = led->iout_joint ?
839 led_cfg->iout_flash_max[FLED1] +
840 led_cfg->iout_flash_max[FLED2] :
841 led_cfg->iout_flash_max[fled_id];
842 setting->step = FLASH_IOUT_STEP;
843 setting->val = setting->max;
845 /* Init flash timeout setting */
846 setting = &fled_cdev->timeout;
847 setting->min = FLASH_TIMEOUT_MIN;
848 setting->max = led_cfg->flash_timeout_max[fled_id];
849 setting->step = FLASH_TIMEOUT_STEP;
850 setting->val = setting->max;
853 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
855 static int max77693_led_external_strobe_set(
856 struct v4l2_flash *v4l2_flash,
857 bool enable)
859 struct max77693_sub_led *sub_led =
860 flcdev_to_sub_led(v4l2_flash->fled_cdev);
861 struct max77693_led_device *led = sub_led_to_led(sub_led);
862 int fled_id = sub_led->fled_id;
863 int ret;
865 mutex_lock(&led->lock);
867 if (enable)
868 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
869 else
870 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
872 mutex_unlock(&led->lock);
874 return ret;
877 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
878 struct max77693_led_config_data *led_cfg,
879 struct v4l2_flash_config *v4l2_sd_cfg)
881 struct max77693_led_device *led = sub_led_to_led(sub_led);
882 struct device *dev = &led->pdev->dev;
883 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
884 struct i2c_client *i2c = iodev->i2c;
885 struct led_flash_setting *s;
887 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
888 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
889 i2c_adapter_id(i2c->adapter), i2c->addr);
891 s = &v4l2_sd_cfg->torch_intensity;
892 s->min = TORCH_IOUT_MIN;
893 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
894 s->step = TORCH_IOUT_STEP;
895 s->val = s->max;
897 /* Init flash faults config */
898 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
899 LED_FAULT_SHORT_CIRCUIT |
900 LED_FAULT_OVER_CURRENT;
902 v4l2_sd_cfg->has_external_strobe = true;
905 static const struct v4l2_flash_ops v4l2_flash_ops = {
906 .external_strobe_set = max77693_led_external_strobe_set,
908 #else
909 static inline void max77693_init_v4l2_flash_config(
910 struct max77693_sub_led *sub_led,
911 struct max77693_led_config_data *led_cfg,
912 struct v4l2_flash_config *v4l2_sd_cfg)
915 static const struct v4l2_flash_ops v4l2_flash_ops;
916 #endif
918 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
919 struct max77693_led_config_data *led_cfg)
921 struct max77693_led_device *led = sub_led_to_led(sub_led);
922 int fled_id = sub_led->fled_id;
923 struct led_classdev_flash *fled_cdev;
924 struct led_classdev *led_cdev;
926 /* Initialize LED Flash class device */
927 fled_cdev = &sub_led->fled_cdev;
928 fled_cdev->ops = &flash_ops;
929 led_cdev = &fled_cdev->led_cdev;
931 led_cdev->name = led_cfg->label[fled_id];
933 led_cdev->brightness_set = max77693_led_brightness_set;
934 led_cdev->brightness_set_sync = max77693_led_brightness_set_sync;
935 led_cdev->max_brightness = (led->iout_joint ?
936 led_cfg->iout_torch_max[FLED1] +
937 led_cfg->iout_torch_max[FLED2] :
938 led_cfg->iout_torch_max[fled_id]) /
939 TORCH_IOUT_STEP;
940 led_cdev->flags |= LED_DEV_CAP_FLASH;
941 INIT_WORK(&sub_led->work_brightness_set,
942 max77693_led_brightness_set_work);
944 max77693_init_flash_settings(sub_led, led_cfg);
946 /* Init flash timeout cache */
947 sub_led->flash_timeout = fled_cdev->timeout.val;
950 static int max77693_register_led(struct max77693_sub_led *sub_led,
951 struct max77693_led_config_data *led_cfg,
952 struct device_node *sub_node)
954 struct max77693_led_device *led = sub_led_to_led(sub_led);
955 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
956 struct device *dev = &led->pdev->dev;
957 struct v4l2_flash_config v4l2_sd_cfg = {};
958 int ret;
960 /* Register in the LED subsystem */
961 ret = led_classdev_flash_register(dev, fled_cdev);
962 if (ret < 0)
963 return ret;
965 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
967 /* Register in the V4L2 subsystem. */
968 sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
969 &v4l2_flash_ops, &v4l2_sd_cfg);
970 if (IS_ERR(sub_led->v4l2_flash)) {
971 ret = PTR_ERR(sub_led->v4l2_flash);
972 goto err_v4l2_flash_init;
975 return 0;
977 err_v4l2_flash_init:
978 led_classdev_flash_unregister(fled_cdev);
979 return ret;
982 static int max77693_led_probe(struct platform_device *pdev)
984 struct device *dev = &pdev->dev;
985 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
986 struct max77693_led_device *led;
987 struct max77693_sub_led *sub_leds;
988 struct device_node *sub_nodes[2] = {};
989 struct max77693_led_config_data led_cfg = {};
990 int init_fled_cdev[2], i, ret;
992 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
993 if (!led)
994 return -ENOMEM;
996 led->pdev = pdev;
997 led->regmap = iodev->regmap;
998 led->allowed_modes = MODE_FLASH_MASK;
999 sub_leds = led->sub_leds;
1001 platform_set_drvdata(pdev, led);
1002 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
1003 if (ret < 0)
1004 return ret;
1006 ret = max77693_setup(led, &led_cfg);
1007 if (ret < 0)
1008 return ret;
1010 mutex_init(&led->lock);
1012 init_fled_cdev[FLED1] =
1013 led->iout_joint || max77693_fled_used(led, FLED1);
1014 init_fled_cdev[FLED2] =
1015 !led->iout_joint && max77693_fled_used(led, FLED2);
1017 for (i = FLED1; i <= FLED2; ++i) {
1018 if (!init_fled_cdev[i])
1019 continue;
1021 /* Initialize LED Flash class device */
1022 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
1025 * Register LED Flash class device and corresponding
1026 * V4L2 Flash device.
1028 ret = max77693_register_led(&sub_leds[i], &led_cfg,
1029 sub_nodes[i]);
1030 if (ret < 0) {
1032 * At this moment FLED1 might have been already
1033 * registered and it needs to be released.
1035 if (i == FLED2)
1036 goto err_register_led2;
1037 else
1038 goto err_register_led1;
1042 return 0;
1044 err_register_led2:
1045 /* It is possible than only FLED2 was to be registered */
1046 if (!init_fled_cdev[FLED1])
1047 goto err_register_led1;
1048 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1049 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1050 err_register_led1:
1051 mutex_destroy(&led->lock);
1053 return ret;
1056 static int max77693_led_remove(struct platform_device *pdev)
1058 struct max77693_led_device *led = platform_get_drvdata(pdev);
1059 struct max77693_sub_led *sub_leds = led->sub_leds;
1061 if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1062 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1063 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1064 cancel_work_sync(&sub_leds[FLED1].work_brightness_set);
1067 if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1068 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1069 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1070 cancel_work_sync(&sub_leds[FLED2].work_brightness_set);
1073 mutex_destroy(&led->lock);
1075 return 0;
1078 static const struct of_device_id max77693_led_dt_match[] = {
1079 { .compatible = "maxim,max77693-led" },
1083 static struct platform_driver max77693_led_driver = {
1084 .probe = max77693_led_probe,
1085 .remove = max77693_led_remove,
1086 .driver = {
1087 .name = "max77693-led",
1088 .of_match_table = max77693_led_dt_match,
1092 module_platform_driver(max77693_led_driver);
1094 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1095 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1096 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1097 MODULE_LICENSE("GPL v2");