root/drivers/leds/leds-max77693.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. max77693_led_iout_to_reg
  2. max77693_flash_timeout_to_reg
  3. flcdev_to_sub_led
  4. sub_led_to_led
  5. max77693_led_vsys_to_reg
  6. max77693_led_vout_to_reg
  7. max77693_fled_used
  8. max77693_set_mode_reg
  9. max77693_add_mode
  10. max77693_clear_mode
  11. max77693_add_allowed_modes
  12. max77693_distribute_currents
  13. max77693_set_torch_current
  14. max77693_set_flash_current
  15. max77693_set_timeout
  16. max77693_get_strobe_status
  17. max77693_get_flash_faults
  18. max77693_setup
  19. max77693_led_brightness_set
  20. max77693_led_flash_brightness_set
  21. max77693_led_flash_strobe_set
  22. max77693_led_flash_fault_get
  23. max77693_led_flash_strobe_get
  24. max77693_led_flash_timeout_set
  25. max77693_led_parse_dt
  26. clamp_align
  27. max77693_align_iout_current
  28. max77693_led_validate_configuration
  29. max77693_led_get_configuration
  30. max77693_init_flash_settings
  31. max77693_led_external_strobe_set
  32. max77693_init_v4l2_flash_config
  33. max77693_init_v4l2_flash_config
  34. max77693_init_fled_cdev
  35. max77693_register_led
  36. max77693_led_probe
  37. max77693_led_remove

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

/* [<][>][^][v][top][bottom][index][help] */