Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.6 1061 lines 28 kB view raw
1/* 2 * LED Flash class driver for the flash cell of max77693 mfd. 3 * 4 * Copyright (C) 2015, Samsung Electronics Co., Ltd. 5 * 6 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com> 7 * Andrzej Hajda <a.hajda@samsung.com> 8 * 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. 12 */ 13 14#include <linux/led-class-flash.h> 15#include <linux/mfd/max77693.h> 16#include <linux/mfd/max77693-common.h> 17#include <linux/mfd/max77693-private.h> 18#include <linux/module.h> 19#include <linux/mutex.h> 20#include <linux/platform_device.h> 21#include <linux/regmap.h> 22#include <linux/slab.h> 23#include <media/v4l2-flash-led-class.h> 24 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))) 29 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)) 34 35#define FLED1_IOUT (1 << 0) 36#define FLED2_IOUT (1 << 1) 37 38enum max77693_fled { 39 FLED1, 40 FLED2, 41}; 42 43enum max77693_led_mode { 44 FLASH, 45 TORCH, 46}; 47 48struct 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; 57}; 58 59struct 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 /* V4L2 Flash device */ 65 struct v4l2_flash *v4l2_flash; 66 67 /* brightness cache */ 68 unsigned int torch_brightness; 69 /* flash timeout cache */ 70 unsigned int flash_timeout; 71 /* flash faults that may have occurred */ 72 u32 flash_faults; 73}; 74 75struct max77693_led_device { 76 /* parent mfd regmap */ 77 struct regmap *regmap; 78 /* platform device data */ 79 struct platform_device *pdev; 80 /* secures access to the device */ 81 struct mutex lock; 82 83 /* sub led data */ 84 struct max77693_sub_led sub_leds[2]; 85 86 /* maximum torch current values for FLED outputs */ 87 u32 iout_torch_max[2]; 88 /* maximum flash current values for FLED outputs */ 89 u32 iout_flash_max[2]; 90 91 /* current flash timeout cache */ 92 unsigned int current_flash_timeout; 93 /* ITORCH register cache */ 94 u8 torch_iout_reg; 95 /* mode of fled outputs */ 96 unsigned int mode_flags; 97 /* recently strobed fled */ 98 int strobing_sub_led_id; 99 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */ 100 u8 fled_mask; 101 /* FLED modes that can be set */ 102 u8 allowed_modes; 103 104 /* arrangement of current outputs */ 105 bool iout_joint; 106}; 107 108static u8 max77693_led_iout_to_reg(u32 ua) 109{ 110 if (ua < FLASH_IOUT_MIN) 111 ua = FLASH_IOUT_MIN; 112 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP; 113} 114 115static u8 max77693_flash_timeout_to_reg(u32 us) 116{ 117 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP; 118} 119 120static inline struct max77693_sub_led *flcdev_to_sub_led( 121 struct led_classdev_flash *fled_cdev) 122{ 123 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev); 124} 125 126static inline struct max77693_led_device *sub_led_to_led( 127 struct max77693_sub_led *sub_led) 128{ 129 return container_of(sub_led, struct max77693_led_device, 130 sub_leds[sub_led->fled_id]); 131} 132 133static inline u8 max77693_led_vsys_to_reg(u32 mv) 134{ 135 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2; 136} 137 138static inline u8 max77693_led_vout_to_reg(u32 mv) 139{ 140 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN; 141} 142 143static inline bool max77693_fled_used(struct max77693_led_device *led, 144 int fled_id) 145{ 146 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT; 147 148 return led->fled_mask & fled_bit; 149} 150 151static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode) 152{ 153 struct regmap *rmap = led->regmap; 154 int ret, v = 0, i; 155 156 for (i = FLED1; i <= FLED2; ++i) { 157 if (mode & MODE_TORCH(i)) 158 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i); 159 160 if (mode & MODE_FLASH(i)) { 161 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i); 162 } else if (mode & MODE_FLASH_EXTERNAL(i)) { 163 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i); 164 /* 165 * Enable hw triggering also for torch mode, as some 166 * camera sensors use torch led to fathom ambient light 167 * conditions before strobing the flash. 168 */ 169 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i); 170 } 171 } 172 173 /* Reset the register only prior setting flash modes */ 174 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) { 175 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0); 176 if (ret < 0) 177 return ret; 178 } 179 180 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v); 181} 182 183static int max77693_add_mode(struct max77693_led_device *led, u8 mode) 184{ 185 u8 new_mode_flags; 186 int i, ret; 187 188 if (led->iout_joint) 189 /* Span the mode on FLED2 for joint iouts case */ 190 mode |= (mode << 1); 191 192 /* 193 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device. 194 * Corresponding register bit fields interfere with SW triggered modes, 195 * thus clear them to ensure proper device configuration. 196 */ 197 for (i = FLED1; i <= FLED2; ++i) 198 if (mode & MODE_FLASH_EXTERNAL(i)) 199 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i)); 200 201 new_mode_flags = mode | led->mode_flags; 202 new_mode_flags &= led->allowed_modes; 203 204 if (new_mode_flags ^ led->mode_flags) 205 led->mode_flags = new_mode_flags; 206 else 207 return 0; 208 209 ret = max77693_set_mode_reg(led, led->mode_flags); 210 if (ret < 0) 211 return ret; 212 213 /* 214 * Clear flash mode flag after setting the mode to avoid spurious flash 215 * strobing on each subsequent torch mode setting. 216 */ 217 if (mode & MODE_FLASH_MASK) 218 led->mode_flags &= ~mode; 219 220 return ret; 221} 222 223static int max77693_clear_mode(struct max77693_led_device *led, 224 u8 mode) 225{ 226 if (led->iout_joint) 227 /* Clear mode also on FLED2 for joint iouts case */ 228 mode |= (mode << 1); 229 230 led->mode_flags &= ~mode; 231 232 return max77693_set_mode_reg(led, led->mode_flags); 233} 234 235static void max77693_add_allowed_modes(struct max77693_led_device *led, 236 int fled_id, enum max77693_led_mode mode) 237{ 238 if (mode == FLASH) 239 led->allowed_modes |= (MODE_FLASH(fled_id) | 240 MODE_FLASH_EXTERNAL(fled_id)); 241 else 242 led->allowed_modes |= MODE_TORCH(fled_id); 243} 244 245static void max77693_distribute_currents(struct max77693_led_device *led, 246 int fled_id, enum max77693_led_mode mode, 247 u32 micro_amp, u32 iout_max[2], u32 iout[2]) 248{ 249 if (!led->iout_joint) { 250 iout[fled_id] = micro_amp; 251 max77693_add_allowed_modes(led, fled_id, mode); 252 return; 253 } 254 255 iout[FLED1] = min(micro_amp, iout_max[FLED1]); 256 iout[FLED2] = micro_amp - iout[FLED1]; 257 258 if (mode == FLASH) 259 led->allowed_modes &= ~MODE_FLASH_MASK; 260 else 261 led->allowed_modes &= ~MODE_TORCH_MASK; 262 263 max77693_add_allowed_modes(led, FLED1, mode); 264 265 if (iout[FLED2]) 266 max77693_add_allowed_modes(led, FLED2, mode); 267} 268 269static int max77693_set_torch_current(struct max77693_led_device *led, 270 int fled_id, u32 micro_amp) 271{ 272 struct regmap *rmap = led->regmap; 273 u8 iout1_reg = 0, iout2_reg = 0; 274 u32 iout[2]; 275 276 max77693_distribute_currents(led, fled_id, TORCH, micro_amp, 277 led->iout_torch_max, iout); 278 279 if (fled_id == FLED1 || led->iout_joint) { 280 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]); 281 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT); 282 } 283 if (fled_id == FLED2 || led->iout_joint) { 284 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]); 285 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT); 286 } 287 288 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) | 289 (iout2_reg << TORCH_IOUT2_SHIFT)); 290 291 return regmap_write(rmap, MAX77693_LED_REG_ITORCH, 292 led->torch_iout_reg); 293} 294 295static int max77693_set_flash_current(struct max77693_led_device *led, 296 int fled_id, 297 u32 micro_amp) 298{ 299 struct regmap *rmap = led->regmap; 300 u8 iout1_reg, iout2_reg; 301 u32 iout[2]; 302 int ret = -EINVAL; 303 304 max77693_distribute_currents(led, fled_id, FLASH, micro_amp, 305 led->iout_flash_max, iout); 306 307 if (fled_id == FLED1 || led->iout_joint) { 308 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]); 309 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1, 310 iout1_reg); 311 if (ret < 0) 312 return ret; 313 } 314 if (fled_id == FLED2 || led->iout_joint) { 315 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]); 316 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2, 317 iout2_reg); 318 } 319 320 return ret; 321} 322 323static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec) 324{ 325 struct regmap *rmap = led->regmap; 326 u8 v; 327 int ret; 328 329 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL; 330 331 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v); 332 if (ret < 0) 333 return ret; 334 335 led->current_flash_timeout = microsec; 336 337 return 0; 338} 339 340static int max77693_get_strobe_status(struct max77693_led_device *led, 341 bool *state) 342{ 343 struct regmap *rmap = led->regmap; 344 unsigned int v; 345 int ret; 346 347 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v); 348 if (ret < 0) 349 return ret; 350 351 *state = v & FLASH_STATUS_FLASH_ON; 352 353 return ret; 354} 355 356static int max77693_get_flash_faults(struct max77693_sub_led *sub_led) 357{ 358 struct max77693_led_device *led = sub_led_to_led(sub_led); 359 struct regmap *rmap = led->regmap; 360 unsigned int v; 361 u8 fault_open_mask, fault_short_mask; 362 int ret; 363 364 sub_led->flash_faults = 0; 365 366 if (led->iout_joint) { 367 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN; 368 fault_short_mask = FLASH_INT_FLED1_SHORT | 369 FLASH_INT_FLED2_SHORT; 370 } else { 371 fault_open_mask = (sub_led->fled_id == FLED1) ? 372 FLASH_INT_FLED1_OPEN : 373 FLASH_INT_FLED2_OPEN; 374 fault_short_mask = (sub_led->fled_id == FLED1) ? 375 FLASH_INT_FLED1_SHORT : 376 FLASH_INT_FLED2_SHORT; 377 } 378 379 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v); 380 if (ret < 0) 381 return ret; 382 383 if (v & fault_open_mask) 384 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE; 385 if (v & fault_short_mask) 386 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT; 387 if (v & FLASH_INT_OVER_CURRENT) 388 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT; 389 390 return 0; 391} 392 393static int max77693_setup(struct max77693_led_device *led, 394 struct max77693_led_config_data *led_cfg) 395{ 396 struct regmap *rmap = led->regmap; 397 int i, first_led, last_led, ret; 398 u32 max_flash_curr[2]; 399 u8 v; 400 401 /* 402 * Initialize only flash current. Torch current doesn't 403 * require initialization as ITORCH register is written with 404 * new value each time brightness_set op is called. 405 */ 406 if (led->iout_joint) { 407 first_led = FLED1; 408 last_led = FLED1; 409 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] + 410 led_cfg->iout_flash_max[FLED2]; 411 } else { 412 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2; 413 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1; 414 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1]; 415 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2]; 416 } 417 418 for (i = first_led; i <= last_led; ++i) { 419 ret = max77693_set_flash_current(led, i, 420 max_flash_curr[i]); 421 if (ret < 0) 422 return ret; 423 } 424 425 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL; 426 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v); 427 if (ret < 0) 428 return ret; 429 430 if (led_cfg->low_vsys > 0) 431 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) | 432 MAX_FLASH1_MAX_FL_EN; 433 else 434 v = 0; 435 436 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v); 437 if (ret < 0) 438 return ret; 439 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0); 440 if (ret < 0) 441 return ret; 442 443 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED) 444 v = FLASH_BOOST_FIXED; 445 else 446 v = led_cfg->boost_mode | led_cfg->boost_mode << 1; 447 448 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2)) 449 v |= FLASH_BOOST_LEDNUM_2; 450 451 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v); 452 if (ret < 0) 453 return ret; 454 455 v = max77693_led_vout_to_reg(led_cfg->boost_vout); 456 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v); 457 if (ret < 0) 458 return ret; 459 460 return max77693_set_mode_reg(led, MODE_OFF); 461} 462 463/* LED subsystem callbacks */ 464static int max77693_led_brightness_set(struct led_classdev *led_cdev, 465 enum led_brightness value) 466{ 467 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); 468 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 469 struct max77693_led_device *led = sub_led_to_led(sub_led); 470 int fled_id = sub_led->fled_id, ret; 471 472 mutex_lock(&led->lock); 473 474 if (value == 0) { 475 ret = max77693_clear_mode(led, MODE_TORCH(fled_id)); 476 if (ret < 0) 477 dev_dbg(&led->pdev->dev, 478 "Failed to clear torch mode (%d)\n", 479 ret); 480 goto unlock; 481 } 482 483 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP); 484 if (ret < 0) { 485 dev_dbg(&led->pdev->dev, 486 "Failed to set torch current (%d)\n", 487 ret); 488 goto unlock; 489 } 490 491 ret = max77693_add_mode(led, MODE_TORCH(fled_id)); 492 if (ret < 0) 493 dev_dbg(&led->pdev->dev, 494 "Failed to set torch mode (%d)\n", 495 ret); 496unlock: 497 mutex_unlock(&led->lock); 498 499 return ret; 500} 501 502static int max77693_led_flash_brightness_set( 503 struct led_classdev_flash *fled_cdev, 504 u32 brightness) 505{ 506 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 507 struct max77693_led_device *led = sub_led_to_led(sub_led); 508 int ret; 509 510 mutex_lock(&led->lock); 511 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness); 512 mutex_unlock(&led->lock); 513 514 return ret; 515} 516 517static int max77693_led_flash_strobe_set( 518 struct led_classdev_flash *fled_cdev, 519 bool state) 520{ 521 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 522 struct max77693_led_device *led = sub_led_to_led(sub_led); 523 int fled_id = sub_led->fled_id; 524 int ret; 525 526 mutex_lock(&led->lock); 527 528 if (!state) { 529 ret = max77693_clear_mode(led, MODE_FLASH(fled_id)); 530 goto unlock; 531 } 532 533 if (sub_led->flash_timeout != led->current_flash_timeout) { 534 ret = max77693_set_timeout(led, sub_led->flash_timeout); 535 if (ret < 0) 536 goto unlock; 537 } 538 539 led->strobing_sub_led_id = fled_id; 540 541 ret = max77693_add_mode(led, MODE_FLASH(fled_id)); 542 if (ret < 0) 543 goto unlock; 544 545 ret = max77693_get_flash_faults(sub_led); 546 547unlock: 548 mutex_unlock(&led->lock); 549 return ret; 550} 551 552static int max77693_led_flash_fault_get( 553 struct led_classdev_flash *fled_cdev, 554 u32 *fault) 555{ 556 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 557 558 *fault = sub_led->flash_faults; 559 560 return 0; 561} 562 563static int max77693_led_flash_strobe_get( 564 struct led_classdev_flash *fled_cdev, 565 bool *state) 566{ 567 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 568 struct max77693_led_device *led = sub_led_to_led(sub_led); 569 int ret; 570 571 if (!state) 572 return -EINVAL; 573 574 mutex_lock(&led->lock); 575 576 ret = max77693_get_strobe_status(led, state); 577 578 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id)); 579 580 mutex_unlock(&led->lock); 581 582 return ret; 583} 584 585static int max77693_led_flash_timeout_set( 586 struct led_classdev_flash *fled_cdev, 587 u32 timeout) 588{ 589 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev); 590 struct max77693_led_device *led = sub_led_to_led(sub_led); 591 592 mutex_lock(&led->lock); 593 sub_led->flash_timeout = timeout; 594 mutex_unlock(&led->lock); 595 596 return 0; 597} 598 599static int max77693_led_parse_dt(struct max77693_led_device *led, 600 struct max77693_led_config_data *cfg, 601 struct device_node **sub_nodes) 602{ 603 struct device *dev = &led->pdev->dev; 604 struct max77693_sub_led *sub_leds = led->sub_leds; 605 struct device_node *node = dev->of_node, *child_node; 606 struct property *prop; 607 u32 led_sources[2]; 608 int i, ret, fled_id; 609 610 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode); 611 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout); 612 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys); 613 614 for_each_available_child_of_node(node, child_node) { 615 prop = of_find_property(child_node, "led-sources", NULL); 616 if (prop) { 617 const __be32 *srcs = NULL; 618 619 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) { 620 srcs = of_prop_next_u32(prop, srcs, 621 &led_sources[i]); 622 if (!srcs) 623 break; 624 } 625 } else { 626 dev_err(dev, 627 "led-sources DT property missing\n"); 628 of_node_put(child_node); 629 return -EINVAL; 630 } 631 632 if (i == 2) { 633 fled_id = FLED1; 634 led->fled_mask = FLED1_IOUT | FLED2_IOUT; 635 } else if (led_sources[0] == FLED1) { 636 fled_id = FLED1; 637 led->fled_mask |= FLED1_IOUT; 638 } else if (led_sources[0] == FLED2) { 639 fled_id = FLED2; 640 led->fled_mask |= FLED2_IOUT; 641 } else { 642 dev_err(dev, 643 "Wrong led-sources DT property value.\n"); 644 of_node_put(child_node); 645 return -EINVAL; 646 } 647 648 if (sub_nodes[fled_id]) { 649 dev_err(dev, 650 "Conflicting \"led-sources\" DT properties\n"); 651 of_node_put(child_node); 652 return -EINVAL; 653 } 654 655 sub_nodes[fled_id] = child_node; 656 sub_leds[fled_id].fled_id = fled_id; 657 658 cfg->label[fled_id] = 659 of_get_property(child_node, "label", NULL) ? : 660 child_node->name; 661 662 ret = of_property_read_u32(child_node, "led-max-microamp", 663 &cfg->iout_torch_max[fled_id]); 664 if (ret < 0) { 665 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN; 666 dev_warn(dev, "led-max-microamp DT property missing\n"); 667 } 668 669 ret = of_property_read_u32(child_node, "flash-max-microamp", 670 &cfg->iout_flash_max[fled_id]); 671 if (ret < 0) { 672 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN; 673 dev_warn(dev, 674 "flash-max-microamp DT property missing\n"); 675 } 676 677 ret = of_property_read_u32(child_node, "flash-max-timeout-us", 678 &cfg->flash_timeout_max[fled_id]); 679 if (ret < 0) { 680 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN; 681 dev_warn(dev, 682 "flash-max-timeout-us DT property missing\n"); 683 } 684 685 if (++cfg->num_leds == 2 || 686 (max77693_fled_used(led, FLED1) && 687 max77693_fled_used(led, FLED2))) { 688 of_node_put(child_node); 689 break; 690 } 691 } 692 693 if (cfg->num_leds == 0) { 694 dev_err(dev, "No DT child node found for connected LED(s).\n"); 695 return -EINVAL; 696 } 697 698 return 0; 699} 700 701static void clamp_align(u32 *v, u32 min, u32 max, u32 step) 702{ 703 *v = clamp_val(*v, min, max); 704 if (step > 1) 705 *v = (*v - min) / step * step + min; 706} 707 708static void max77693_align_iout_current(struct max77693_led_device *led, 709 u32 *iout, u32 min, u32 max, u32 step) 710{ 711 int i; 712 713 if (led->iout_joint) { 714 if (iout[FLED1] > min) { 715 iout[FLED1] /= 2; 716 iout[FLED2] = iout[FLED1]; 717 } else { 718 iout[FLED1] = min; 719 iout[FLED2] = 0; 720 return; 721 } 722 } 723 724 for (i = FLED1; i <= FLED2; ++i) 725 if (max77693_fled_used(led, i)) 726 clamp_align(&iout[i], min, max, step); 727 else 728 iout[i] = 0; 729} 730 731static void max77693_led_validate_configuration(struct max77693_led_device *led, 732 struct max77693_led_config_data *cfg) 733{ 734 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS : 735 FLASH_IOUT_MAX_1LED; 736 int i; 737 738 if (cfg->num_leds == 1 && 739 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2)) 740 led->iout_joint = true; 741 742 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE, 743 MAX77693_LED_BOOST_FIXED); 744 745 /* Boost must be enabled if both current outputs are used */ 746 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint) 747 cfg->boost_mode = MAX77693_LED_BOOST_FIXED; 748 749 max77693_align_iout_current(led, cfg->iout_torch_max, 750 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP); 751 752 max77693_align_iout_current(led, cfg->iout_flash_max, 753 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP); 754 755 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i) 756 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN, 757 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP); 758 759 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX, 760 FLASH_VOUT_STEP); 761 762 if (cfg->low_vsys) 763 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN, 764 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP); 765} 766 767static int max77693_led_get_configuration(struct max77693_led_device *led, 768 struct max77693_led_config_data *cfg, 769 struct device_node **sub_nodes) 770{ 771 int ret; 772 773 ret = max77693_led_parse_dt(led, cfg, sub_nodes); 774 if (ret < 0) 775 return ret; 776 777 max77693_led_validate_configuration(led, cfg); 778 779 memcpy(led->iout_torch_max, cfg->iout_torch_max, 780 sizeof(led->iout_torch_max)); 781 memcpy(led->iout_flash_max, cfg->iout_flash_max, 782 sizeof(led->iout_flash_max)); 783 784 return 0; 785} 786 787static const struct led_flash_ops flash_ops = { 788 .flash_brightness_set = max77693_led_flash_brightness_set, 789 .strobe_set = max77693_led_flash_strobe_set, 790 .strobe_get = max77693_led_flash_strobe_get, 791 .timeout_set = max77693_led_flash_timeout_set, 792 .fault_get = max77693_led_flash_fault_get, 793}; 794 795static void max77693_init_flash_settings(struct max77693_sub_led *sub_led, 796 struct max77693_led_config_data *led_cfg) 797{ 798 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev; 799 struct max77693_led_device *led = sub_led_to_led(sub_led); 800 int fled_id = sub_led->fled_id; 801 struct led_flash_setting *setting; 802 803 /* Init flash intensity setting */ 804 setting = &fled_cdev->brightness; 805 setting->min = FLASH_IOUT_MIN; 806 setting->max = led->iout_joint ? 807 led_cfg->iout_flash_max[FLED1] + 808 led_cfg->iout_flash_max[FLED2] : 809 led_cfg->iout_flash_max[fled_id]; 810 setting->step = FLASH_IOUT_STEP; 811 setting->val = setting->max; 812 813 /* Init flash timeout setting */ 814 setting = &fled_cdev->timeout; 815 setting->min = FLASH_TIMEOUT_MIN; 816 setting->max = led_cfg->flash_timeout_max[fled_id]; 817 setting->step = FLASH_TIMEOUT_STEP; 818 setting->val = setting->max; 819} 820 821#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 822 823static int max77693_led_external_strobe_set( 824 struct v4l2_flash *v4l2_flash, 825 bool enable) 826{ 827 struct max77693_sub_led *sub_led = 828 flcdev_to_sub_led(v4l2_flash->fled_cdev); 829 struct max77693_led_device *led = sub_led_to_led(sub_led); 830 int fled_id = sub_led->fled_id; 831 int ret; 832 833 mutex_lock(&led->lock); 834 835 if (enable) 836 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id)); 837 else 838 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id)); 839 840 mutex_unlock(&led->lock); 841 842 return ret; 843} 844 845static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led, 846 struct max77693_led_config_data *led_cfg, 847 struct v4l2_flash_config *v4l2_sd_cfg) 848{ 849 struct max77693_led_device *led = sub_led_to_led(sub_led); 850 struct device *dev = &led->pdev->dev; 851 struct max77693_dev *iodev = dev_get_drvdata(dev->parent); 852 struct i2c_client *i2c = iodev->i2c; 853 struct led_flash_setting *s; 854 855 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name), 856 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name, 857 i2c_adapter_id(i2c->adapter), i2c->addr); 858 859 s = &v4l2_sd_cfg->torch_intensity; 860 s->min = TORCH_IOUT_MIN; 861 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP; 862 s->step = TORCH_IOUT_STEP; 863 s->val = s->max; 864 865 /* Init flash faults config */ 866 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE | 867 LED_FAULT_SHORT_CIRCUIT | 868 LED_FAULT_OVER_CURRENT; 869 870 v4l2_sd_cfg->has_external_strobe = true; 871} 872 873static const struct v4l2_flash_ops v4l2_flash_ops = { 874 .external_strobe_set = max77693_led_external_strobe_set, 875}; 876#else 877static inline void max77693_init_v4l2_flash_config( 878 struct max77693_sub_led *sub_led, 879 struct max77693_led_config_data *led_cfg, 880 struct v4l2_flash_config *v4l2_sd_cfg) 881{ 882} 883static const struct v4l2_flash_ops v4l2_flash_ops; 884#endif 885 886static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led, 887 struct max77693_led_config_data *led_cfg) 888{ 889 struct max77693_led_device *led = sub_led_to_led(sub_led); 890 int fled_id = sub_led->fled_id; 891 struct led_classdev_flash *fled_cdev; 892 struct led_classdev *led_cdev; 893 894 /* Initialize LED Flash class device */ 895 fled_cdev = &sub_led->fled_cdev; 896 fled_cdev->ops = &flash_ops; 897 led_cdev = &fled_cdev->led_cdev; 898 899 led_cdev->name = led_cfg->label[fled_id]; 900 901 led_cdev->brightness_set_blocking = max77693_led_brightness_set; 902 led_cdev->max_brightness = (led->iout_joint ? 903 led_cfg->iout_torch_max[FLED1] + 904 led_cfg->iout_torch_max[FLED2] : 905 led_cfg->iout_torch_max[fled_id]) / 906 TORCH_IOUT_STEP; 907 led_cdev->flags |= LED_DEV_CAP_FLASH; 908 909 max77693_init_flash_settings(sub_led, led_cfg); 910 911 /* Init flash timeout cache */ 912 sub_led->flash_timeout = fled_cdev->timeout.val; 913} 914 915static int max77693_register_led(struct max77693_sub_led *sub_led, 916 struct max77693_led_config_data *led_cfg, 917 struct device_node *sub_node) 918{ 919 struct max77693_led_device *led = sub_led_to_led(sub_led); 920 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev; 921 struct device *dev = &led->pdev->dev; 922 struct v4l2_flash_config v4l2_sd_cfg = {}; 923 int ret; 924 925 /* Register in the LED subsystem */ 926 ret = led_classdev_flash_register(dev, fled_cdev); 927 if (ret < 0) 928 return ret; 929 930 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg); 931 932 /* Register in the V4L2 subsystem. */ 933 sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL, 934 &v4l2_flash_ops, &v4l2_sd_cfg); 935 if (IS_ERR(sub_led->v4l2_flash)) { 936 ret = PTR_ERR(sub_led->v4l2_flash); 937 goto err_v4l2_flash_init; 938 } 939 940 return 0; 941 942err_v4l2_flash_init: 943 led_classdev_flash_unregister(fled_cdev); 944 return ret; 945} 946 947static int max77693_led_probe(struct platform_device *pdev) 948{ 949 struct device *dev = &pdev->dev; 950 struct max77693_dev *iodev = dev_get_drvdata(dev->parent); 951 struct max77693_led_device *led; 952 struct max77693_sub_led *sub_leds; 953 struct device_node *sub_nodes[2] = {}; 954 struct max77693_led_config_data led_cfg = {}; 955 int init_fled_cdev[2], i, ret; 956 957 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); 958 if (!led) 959 return -ENOMEM; 960 961 led->pdev = pdev; 962 led->regmap = iodev->regmap; 963 led->allowed_modes = MODE_FLASH_MASK; 964 sub_leds = led->sub_leds; 965 966 platform_set_drvdata(pdev, led); 967 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes); 968 if (ret < 0) 969 return ret; 970 971 ret = max77693_setup(led, &led_cfg); 972 if (ret < 0) 973 return ret; 974 975 mutex_init(&led->lock); 976 977 init_fled_cdev[FLED1] = 978 led->iout_joint || max77693_fled_used(led, FLED1); 979 init_fled_cdev[FLED2] = 980 !led->iout_joint && max77693_fled_used(led, FLED2); 981 982 for (i = FLED1; i <= FLED2; ++i) { 983 if (!init_fled_cdev[i]) 984 continue; 985 986 /* Initialize LED Flash class device */ 987 max77693_init_fled_cdev(&sub_leds[i], &led_cfg); 988 989 /* 990 * Register LED Flash class device and corresponding 991 * V4L2 Flash device. 992 */ 993 ret = max77693_register_led(&sub_leds[i], &led_cfg, 994 sub_nodes[i]); 995 if (ret < 0) { 996 /* 997 * At this moment FLED1 might have been already 998 * registered and it needs to be released. 999 */ 1000 if (i == FLED2) 1001 goto err_register_led2; 1002 else 1003 goto err_register_led1; 1004 } 1005 } 1006 1007 return 0; 1008 1009err_register_led2: 1010 /* It is possible than only FLED2 was to be registered */ 1011 if (!init_fled_cdev[FLED1]) 1012 goto err_register_led1; 1013 v4l2_flash_release(sub_leds[FLED1].v4l2_flash); 1014 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev); 1015err_register_led1: 1016 mutex_destroy(&led->lock); 1017 1018 return ret; 1019} 1020 1021static int max77693_led_remove(struct platform_device *pdev) 1022{ 1023 struct max77693_led_device *led = platform_get_drvdata(pdev); 1024 struct max77693_sub_led *sub_leds = led->sub_leds; 1025 1026 if (led->iout_joint || max77693_fled_used(led, FLED1)) { 1027 v4l2_flash_release(sub_leds[FLED1].v4l2_flash); 1028 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev); 1029 } 1030 1031 if (!led->iout_joint && max77693_fled_used(led, FLED2)) { 1032 v4l2_flash_release(sub_leds[FLED2].v4l2_flash); 1033 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev); 1034 } 1035 1036 mutex_destroy(&led->lock); 1037 1038 return 0; 1039} 1040 1041static const struct of_device_id max77693_led_dt_match[] = { 1042 { .compatible = "maxim,max77693-led" }, 1043 {}, 1044}; 1045MODULE_DEVICE_TABLE(of, max77693_led_dt_match); 1046 1047static struct platform_driver max77693_led_driver = { 1048 .probe = max77693_led_probe, 1049 .remove = max77693_led_remove, 1050 .driver = { 1051 .name = "max77693-led", 1052 .of_match_table = max77693_led_dt_match, 1053 }, 1054}; 1055 1056module_platform_driver(max77693_led_driver); 1057 1058MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>"); 1059MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>"); 1060MODULE_DESCRIPTION("Maxim MAX77693 led flash driver"); 1061MODULE_LICENSE("GPL v2");