1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. 4 */ 5 6 #include <linux/bitfield.h> 7 #include <linux/bits.h> 8 #include <linux/leds.h> 9 #include <linux/led-class-flash.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/property.h> 13 #include <linux/regmap.h> 14 #include <media/v4l2-flash-led-class.h> 15 16 /* registers definitions */ 17 #define FLASH_TYPE_REG 0x04 18 #define FLASH_TYPE_VAL 0x18 19 20 #define FLASH_SUBTYPE_REG 0x05 21 #define FLASH_SUBTYPE_3CH_VAL 0x04 22 #define FLASH_SUBTYPE_4CH_VAL 0x07 23 24 #define FLASH_STS_3CH_OTST1 BIT(0) 25 #define FLASH_STS_3CH_OTST2 BIT(1) 26 #define FLASH_STS_3CH_OTST3 BIT(2) 27 #define FLASH_STS_3CH_BOB_THM_OVERLOAD BIT(3) 28 #define FLASH_STS_3CH_VPH_DROOP BIT(4) 29 #define FLASH_STS_3CH_BOB_ILIM_S1 BIT(5) 30 #define FLASH_STS_3CH_BOB_ILIM_S2 BIT(6) 31 #define FLASH_STS_3CH_BCL_IBAT BIT(7) 32 33 #define FLASH_STS_4CH_VPH_LOW BIT(0) 34 #define FLASH_STS_4CH_BCL_IBAT BIT(1) 35 #define FLASH_STS_4CH_BOB_ILIM_S1 BIT(2) 36 #define FLASH_STS_4CH_BOB_ILIM_S2 BIT(3) 37 #define FLASH_STS_4CH_OTST2 BIT(4) 38 #define FLASH_STS_4CH_OTST1 BIT(5) 39 #define FLASH_STS_4CHG_BOB_THM_OVERLOAD BIT(6) 40 41 #define FLASH_TIMER_EN_BIT BIT(7) 42 #define FLASH_TIMER_VAL_MASK GENMASK(6, 0) 43 #define FLASH_TIMER_STEP_MS 10 44 45 #define FLASH_STROBE_HW_SW_SEL_BIT BIT(2) 46 #define SW_STROBE_VAL 0 47 #define HW_STROBE_VAL 1 48 #define FLASH_HW_STROBE_TRIGGER_SEL_BIT BIT(1) 49 #define STROBE_LEVEL_TRIGGER_VAL 0 50 #define STROBE_EDGE_TRIGGER_VAL 1 51 #define FLASH_STROBE_POLARITY_BIT BIT(0) 52 #define STROBE_ACTIVE_HIGH_VAL 1 53 54 #define FLASH_IRES_MASK_4CH BIT(0) 55 #define FLASH_IRES_MASK_3CH GENMASK(1, 0) 56 #define FLASH_IRES_12P5MA_VAL 0 57 #define FLASH_IRES_5MA_VAL_4CH 1 58 #define FLASH_IRES_5MA_VAL_3CH 3 59 60 /* constants */ 61 #define FLASH_CURRENT_MAX_UA 1500000 62 #define TORCH_CURRENT_MAX_UA 500000 63 #define FLASH_TOTAL_CURRENT_MAX_UA 2000000 64 #define FLASH_CURRENT_DEFAULT_UA 1000000 65 #define TORCH_CURRENT_DEFAULT_UA 200000 66 67 #define TORCH_IRES_UA 5000 68 #define FLASH_IRES_UA 12500 69 70 #define FLASH_TIMEOUT_MAX_US 1280000 71 #define FLASH_TIMEOUT_STEP_US 10000 72 73 #define UA_PER_MA 1000 74 75 enum hw_type { 76 QCOM_MVFLASH_3CH, 77 QCOM_MVFLASH_4CH, 78 }; 79 80 enum led_mode { 81 FLASH_MODE, 82 TORCH_MODE, 83 }; 84 85 enum led_strobe { 86 SW_STROBE, 87 HW_STROBE, 88 }; 89 90 enum { 91 REG_STATUS1, 92 REG_STATUS2, 93 REG_STATUS3, 94 REG_CHAN_TIMER, 95 REG_ITARGET, 96 REG_MODULE_EN, 97 REG_IRESOLUTION, 98 REG_CHAN_STROBE, 99 REG_CHAN_EN, 100 REG_MAX_COUNT, 101 }; 102 103 static struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = { 104 REG_FIELD(0x08, 0, 7), /* status1 */ 105 REG_FIELD(0x09, 0, 7), /* status2 */ 106 REG_FIELD(0x0a, 0, 7), /* status3 */ 107 REG_FIELD_ID(0x40, 0, 7, 3, 1), /* chan_timer */ 108 REG_FIELD_ID(0x43, 0, 6, 3, 1), /* itarget */ 109 REG_FIELD(0x46, 7, 7), /* module_en */ 110 REG_FIELD(0x47, 0, 5), /* iresolution */ 111 REG_FIELD_ID(0x49, 0, 2, 3, 1), /* chan_strobe */ 112 REG_FIELD(0x4c, 0, 2), /* chan_en */ 113 }; 114 115 static struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = { 116 REG_FIELD(0x06, 0, 7), /* status1 */ 117 REG_FIELD(0x07, 0, 6), /* status2 */ 118 REG_FIELD(0x09, 0, 7), /* status3 */ 119 REG_FIELD_ID(0x3e, 0, 7, 4, 1), /* chan_timer */ 120 REG_FIELD_ID(0x42, 0, 6, 4, 1), /* itarget */ 121 REG_FIELD(0x46, 7, 7), /* module_en */ 122 REG_FIELD(0x49, 0, 3), /* iresolution */ 123 REG_FIELD_ID(0x4a, 0, 6, 4, 1), /* chan_strobe */ 124 REG_FIELD(0x4e, 0, 3), /* chan_en */ 125 }; 126 127 struct qcom_flash_data { 128 struct v4l2_flash **v4l2_flash; 129 struct regmap_field *r_fields[REG_MAX_COUNT]; 130 struct mutex lock; 131 enum hw_type hw_type; 132 u8 leds_count; 133 u8 max_channels; 134 u8 chan_en_bits; 135 }; 136 137 struct qcom_flash_led { 138 struct qcom_flash_data *flash_data; 139 struct led_classdev_flash flash; 140 u32 max_flash_current_ma; 141 u32 max_torch_current_ma; 142 u32 max_timeout_ms; 143 u32 flash_current_ma; 144 u32 flash_timeout_ms; 145 u8 *chan_id; 146 u8 chan_count; 147 bool enabled; 148 }; 149 150 static int set_flash_module_en(struct qcom_flash_led *led, bool en) 151 { 152 struct qcom_flash_data *flash_data = led->flash_data; 153 u8 led_mask = 0, enable; 154 int i, rc; 155 156 for (i = 0; i < led->chan_count; i++) 157 led_mask |= BIT(led->chan_id[i]); 158 159 mutex_lock(&flash_data->lock); 160 if (en) 161 flash_data->chan_en_bits |= led_mask; 162 else 163 flash_data->chan_en_bits &= ~led_mask; 164 165 enable = !!flash_data->chan_en_bits; 166 rc = regmap_field_write(flash_data->r_fields[REG_MODULE_EN], enable); 167 if (rc) 168 dev_err(led->flash.led_cdev.dev, "write module_en failed, rc=%d\n", rc); 169 mutex_unlock(&flash_data->lock); 170 171 return rc; 172 } 173 174 static int set_flash_current(struct qcom_flash_led *led, u32 current_ma, enum led_mode mode) 175 { 176 struct qcom_flash_data *flash_data = led->flash_data; 177 u32 itarg_ua, ires_ua; 178 u8 shift, ires_mask = 0, ires_val = 0, chan_id; 179 int i, rc; 180 181 /* 182 * Split the current across the channels and set the 183 * IRESOLUTION and ITARGET registers accordingly. 184 */ 185 itarg_ua = (current_ma * UA_PER_MA) / led->chan_count + 1; 186 ires_ua = (mode == FLASH_MODE) ? FLASH_IRES_UA : TORCH_IRES_UA; 187 188 for (i = 0; i < led->chan_count; i++) { 189 u8 itarget = 0; 190 191 if (itarg_ua > ires_ua) 192 itarget = itarg_ua / ires_ua - 1; 193 194 chan_id = led->chan_id[i]; 195 196 rc = regmap_fields_write(flash_data->r_fields[REG_ITARGET], chan_id, itarget); 197 if (rc) 198 return rc; 199 200 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 201 shift = chan_id * 2; 202 ires_mask |= FLASH_IRES_MASK_3CH << shift; 203 ires_val |= ((mode == FLASH_MODE) ? 204 (FLASH_IRES_12P5MA_VAL << shift) : 205 (FLASH_IRES_5MA_VAL_3CH << shift)); 206 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 207 shift = chan_id; 208 ires_mask |= FLASH_IRES_MASK_4CH << shift; 209 ires_val |= ((mode == FLASH_MODE) ? 210 (FLASH_IRES_12P5MA_VAL << shift) : 211 (FLASH_IRES_5MA_VAL_4CH << shift)); 212 } else { 213 dev_err(led->flash.led_cdev.dev, 214 "HW type %d is not supported\n", flash_data->hw_type); 215 return -EOPNOTSUPP; 216 } 217 } 218 219 return regmap_field_update_bits(flash_data->r_fields[REG_IRESOLUTION], ires_mask, ires_val); 220 } 221 222 static int set_flash_timeout(struct qcom_flash_led *led, u32 timeout_ms) 223 { 224 struct qcom_flash_data *flash_data = led->flash_data; 225 u8 timer, chan_id; 226 int rc, i; 227 228 /* set SAFETY_TIMER for all the channels connected to the same LED */ 229 timeout_ms = min_t(u32, timeout_ms, led->max_timeout_ms); 230 231 for (i = 0; i < led->chan_count; i++) { 232 chan_id = led->chan_id[i]; 233 234 timer = timeout_ms / FLASH_TIMER_STEP_MS; 235 timer = clamp_t(u8, timer, 0, FLASH_TIMER_VAL_MASK); 236 237 if (timeout_ms) 238 timer |= FLASH_TIMER_EN_BIT; 239 240 rc = regmap_fields_write(flash_data->r_fields[REG_CHAN_TIMER], chan_id, timer); 241 if (rc) 242 return rc; 243 } 244 245 return 0; 246 } 247 248 static int set_flash_strobe(struct qcom_flash_led *led, enum led_strobe strobe, bool state) 249 { 250 struct qcom_flash_data *flash_data = led->flash_data; 251 u8 strobe_sel, chan_en, chan_id, chan_mask = 0; 252 int rc, i; 253 254 /* Set SW strobe config for all channels connected to the LED */ 255 for (i = 0; i < led->chan_count; i++) { 256 chan_id = led->chan_id[i]; 257 258 if (strobe == SW_STROBE) 259 strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, SW_STROBE_VAL); 260 else 261 strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, HW_STROBE_VAL); 262 263 strobe_sel |= 264 FIELD_PREP(FLASH_HW_STROBE_TRIGGER_SEL_BIT, STROBE_LEVEL_TRIGGER_VAL) | 265 FIELD_PREP(FLASH_STROBE_POLARITY_BIT, STROBE_ACTIVE_HIGH_VAL); 266 267 rc = regmap_fields_write( 268 flash_data->r_fields[REG_CHAN_STROBE], chan_id, strobe_sel); 269 if (rc) 270 return rc; 271 272 chan_mask |= BIT(chan_id); 273 } 274 275 /* Enable/disable flash channels */ 276 chan_en = state ? chan_mask : 0; 277 rc = regmap_field_update_bits(flash_data->r_fields[REG_CHAN_EN], chan_mask, chan_en); 278 if (rc) 279 return rc; 280 281 led->enabled = state; 282 return 0; 283 } 284 285 static inline struct qcom_flash_led *flcdev_to_qcom_fled(struct led_classdev_flash *flcdev) 286 { 287 return container_of(flcdev, struct qcom_flash_led, flash); 288 } 289 290 static int qcom_flash_brightness_set(struct led_classdev_flash *fled_cdev, u32 brightness) 291 { 292 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 293 294 led->flash_current_ma = min_t(u32, led->max_flash_current_ma, brightness / UA_PER_MA); 295 return 0; 296 } 297 298 static int qcom_flash_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout) 299 { 300 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 301 302 led->flash_timeout_ms = timeout / USEC_PER_MSEC; 303 return 0; 304 } 305 306 static int qcom_flash_strobe_set(struct led_classdev_flash *fled_cdev, bool state) 307 { 308 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 309 int rc; 310 311 rc = set_flash_current(led, led->flash_current_ma, FLASH_MODE); 312 if (rc) 313 return rc; 314 315 rc = set_flash_timeout(led, led->flash_timeout_ms); 316 if (rc) 317 return rc; 318 319 rc = set_flash_module_en(led, state); 320 if (rc) 321 return rc; 322 323 return set_flash_strobe(led, SW_STROBE, state); 324 } 325 326 static int qcom_flash_strobe_get(struct led_classdev_flash *fled_cdev, bool *state) 327 { 328 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 329 330 *state = led->enabled; 331 return 0; 332 } 333 334 static int qcom_flash_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault) 335 { 336 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 337 struct qcom_flash_data *flash_data = led->flash_data; 338 u8 shift, chan_id, chan_mask = 0; 339 u8 ot_mask = 0, oc_mask = 0, uv_mask = 0; 340 u32 val, fault_sts = 0; 341 int i, rc; 342 343 rc = regmap_field_read(flash_data->r_fields[REG_STATUS1], &val); 344 if (rc) 345 return rc; 346 347 for (i = 0; i < led->chan_count; i++) { 348 chan_id = led->chan_id[i]; 349 shift = chan_id * 2; 350 351 if (val & BIT(shift)) 352 fault_sts |= LED_FAULT_SHORT_CIRCUIT; 353 354 chan_mask |= BIT(chan_id); 355 } 356 357 rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &val); 358 if (rc) 359 return rc; 360 361 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 362 ot_mask = FLASH_STS_3CH_OTST1 | 363 FLASH_STS_3CH_OTST2 | 364 FLASH_STS_3CH_OTST3 | 365 FLASH_STS_3CH_BOB_THM_OVERLOAD; 366 oc_mask = FLASH_STS_3CH_BOB_ILIM_S1 | 367 FLASH_STS_3CH_BOB_ILIM_S2 | 368 FLASH_STS_3CH_BCL_IBAT; 369 uv_mask = FLASH_STS_3CH_VPH_DROOP; 370 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 371 ot_mask = FLASH_STS_4CH_OTST2 | 372 FLASH_STS_4CH_OTST1 | 373 FLASH_STS_4CHG_BOB_THM_OVERLOAD; 374 oc_mask = FLASH_STS_4CH_BCL_IBAT | 375 FLASH_STS_4CH_BOB_ILIM_S1 | 376 FLASH_STS_4CH_BOB_ILIM_S2; 377 uv_mask = FLASH_STS_4CH_VPH_LOW; 378 } 379 380 if (val & ot_mask) 381 fault_sts |= LED_FAULT_OVER_TEMPERATURE; 382 383 if (val & oc_mask) 384 fault_sts |= LED_FAULT_OVER_CURRENT; 385 386 if (val & uv_mask) 387 fault_sts |= LED_FAULT_INPUT_VOLTAGE; 388 389 rc = regmap_field_read(flash_data->r_fields[REG_STATUS3], &val); 390 if (rc) 391 return rc; 392 393 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 394 if (val & chan_mask) 395 fault_sts |= LED_FAULT_TIMEOUT; 396 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 397 for (i = 0; i < led->chan_count; i++) { 398 chan_id = led->chan_id[i]; 399 shift = chan_id * 2; 400 401 if (val & BIT(shift)) 402 fault_sts |= LED_FAULT_TIMEOUT; 403 } 404 } 405 406 *fault = fault_sts; 407 return 0; 408 } 409 410 static int qcom_flash_led_brightness_set(struct led_classdev *led_cdev, 411 enum led_brightness brightness) 412 { 413 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); 414 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 415 u32 current_ma = brightness * led->max_torch_current_ma / LED_FULL; 416 bool enable = !!brightness; 417 int rc; 418 419 rc = set_flash_current(led, current_ma, TORCH_MODE); 420 if (rc) 421 return rc; 422 423 /* Disable flash timeout for torch LED */ 424 rc = set_flash_timeout(led, 0); 425 if (rc) 426 return rc; 427 428 rc = set_flash_module_en(led, enable); 429 if (rc) 430 return rc; 431 432 return set_flash_strobe(led, SW_STROBE, enable); 433 } 434 435 static const struct led_flash_ops qcom_flash_ops = { 436 .flash_brightness_set = qcom_flash_brightness_set, 437 .strobe_set = qcom_flash_strobe_set, 438 .strobe_get = qcom_flash_strobe_get, 439 .timeout_set = qcom_flash_timeout_set, 440 .fault_get = qcom_flash_fault_get, 441 }; 442 443 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 444 static int qcom_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, bool enable) 445 { 446 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 447 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 448 int rc; 449 450 rc = set_flash_module_en(led, enable); 451 if (rc) 452 return rc; 453 454 if (enable) 455 return set_flash_strobe(led, HW_STROBE, true); 456 else 457 return set_flash_strobe(led, SW_STROBE, false); 458 } 459 460 static enum led_brightness 461 qcom_flash_intensity_to_led_brightness(struct v4l2_flash *v4l2_flash, s32 intensity) 462 { 463 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 464 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 465 u32 current_ma = intensity / UA_PER_MA; 466 467 current_ma = min_t(u32, current_ma, led->max_torch_current_ma); 468 if (!current_ma) 469 return LED_OFF; 470 471 return (current_ma * LED_FULL) / led->max_torch_current_ma; 472 } 473 474 static s32 qcom_flash_brightness_to_led_intensity(struct v4l2_flash *v4l2_flash, 475 enum led_brightness brightness) 476 { 477 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 478 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 479 480 return (brightness * led->max_torch_current_ma * UA_PER_MA) / LED_FULL; 481 } 482 483 static const struct v4l2_flash_ops qcom_v4l2_flash_ops = { 484 .external_strobe_set = qcom_flash_external_strobe_set, 485 .intensity_to_led_brightness = qcom_flash_intensity_to_led_brightness, 486 .led_brightness_to_intensity = qcom_flash_brightness_to_led_intensity, 487 }; 488 489 static int 490 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) 491 { 492 struct qcom_flash_data *flash_data = led->flash_data; 493 struct v4l2_flash_config v4l2_cfg = { 0 }; 494 struct led_flash_setting *intensity = &v4l2_cfg.intensity; 495 496 if (!(led->flash.led_cdev.flags & LED_DEV_CAP_FLASH)) 497 return 0; 498 499 intensity->min = intensity->step = TORCH_IRES_UA * led->chan_count; 500 intensity->max = led->max_torch_current_ma * UA_PER_MA; 501 intensity->val = min_t(u32, intensity->max, TORCH_CURRENT_DEFAULT_UA); 502 503 strscpy(v4l2_cfg.dev_name, led->flash.led_cdev.dev->kobj.name, 504 sizeof(v4l2_cfg.dev_name)); 505 506 v4l2_cfg.has_external_strobe = true; 507 v4l2_cfg.flash_faults = LED_FAULT_INPUT_VOLTAGE | 508 LED_FAULT_OVER_CURRENT | 509 LED_FAULT_SHORT_CIRCUIT | 510 LED_FAULT_OVER_TEMPERATURE | 511 LED_FAULT_TIMEOUT; 512 513 flash_data->v4l2_flash[flash_data->leds_count] = 514 v4l2_flash_init(dev, fwnode, &led->flash, &qcom_v4l2_flash_ops, &v4l2_cfg); 515 return PTR_ERR_OR_ZERO(flash_data->v4l2_flash); 516 } 517 # else 518 static int 519 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) 520 { 521 return 0; 522 } 523 #endif 524 525 static int qcom_flash_register_led_device(struct device *dev, 526 struct fwnode_handle *node, struct qcom_flash_led *led) 527 { 528 struct qcom_flash_data *flash_data = led->flash_data; 529 struct led_init_data init_data; 530 struct led_classdev_flash *flash = &led->flash; 531 struct led_flash_setting *brightness, *timeout; 532 u32 count, current_ua, timeout_us; 533 u32 channels[4]; 534 int i, rc; 535 536 count = fwnode_property_count_u32(node, "led-sources"); 537 if (count <= 0) { 538 dev_err(dev, "No led-sources specified\n"); 539 return -ENODEV; 540 } 541 542 if (count > flash_data->max_channels) { 543 dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n", 544 count, flash_data->max_channels); 545 return -EINVAL; 546 } 547 548 rc = fwnode_property_read_u32_array(node, "led-sources", channels, count); 549 if (rc < 0) { 550 dev_err(dev, "Failed to read led-sources property, rc=%d\n", rc); 551 return rc; 552 } 553 554 led->chan_count = count; 555 led->chan_id = devm_kcalloc(dev, count, sizeof(u8), GFP_KERNEL); 556 if (!led->chan_id) 557 return -ENOMEM; 558 559 for (i = 0; i < count; i++) { 560 if ((channels[i] == 0) || (channels[i] > flash_data->max_channels)) { 561 dev_err(dev, "led-source out of HW support range [1-%u]\n", 562 flash_data->max_channels); 563 return -EINVAL; 564 } 565 566 /* Make chan_id indexing from 0 */ 567 led->chan_id[i] = channels[i] - 1; 568 } 569 570 rc = fwnode_property_read_u32(node, "led-max-microamp", ¤t_ua); 571 if (rc < 0) { 572 dev_err(dev, "Failed to read led-max-microamp property, rc=%d\n", rc); 573 return rc; 574 } 575 576 if (current_ua == 0) { 577 dev_err(dev, "led-max-microamp shouldn't be 0\n"); 578 return -EINVAL; 579 } 580 581 current_ua = min_t(u32, current_ua, TORCH_CURRENT_MAX_UA * led->chan_count); 582 led->max_torch_current_ma = current_ua / UA_PER_MA; 583 584 if (fwnode_property_present(node, "flash-max-microamp")) { 585 flash->led_cdev.flags |= LED_DEV_CAP_FLASH; 586 587 rc = fwnode_property_read_u32(node, "flash-max-microamp", ¤t_ua); 588 if (rc < 0) { 589 dev_err(dev, "Failed to read flash-max-microamp property, rc=%d\n", 590 rc); 591 return rc; 592 } 593 594 current_ua = min_t(u32, current_ua, FLASH_CURRENT_MAX_UA * led->chan_count); 595 current_ua = min_t(u32, current_ua, FLASH_TOTAL_CURRENT_MAX_UA); 596 597 /* Initialize flash class LED device brightness settings */ 598 brightness = &flash->brightness; 599 brightness->min = brightness->step = FLASH_IRES_UA * led->chan_count; 600 brightness->max = current_ua; 601 brightness->val = min_t(u32, current_ua, FLASH_CURRENT_DEFAULT_UA); 602 603 led->max_flash_current_ma = current_ua / UA_PER_MA; 604 led->flash_current_ma = brightness->val / UA_PER_MA; 605 606 rc = fwnode_property_read_u32(node, "flash-max-timeout-us", &timeout_us); 607 if (rc < 0) { 608 dev_err(dev, "Failed to read flash-max-timeout-us property, rc=%d\n", 609 rc); 610 return rc; 611 } 612 613 timeout_us = min_t(u32, timeout_us, FLASH_TIMEOUT_MAX_US); 614 615 /* Initialize flash class LED device timeout settings */ 616 timeout = &flash->timeout; 617 timeout->min = timeout->step = FLASH_TIMEOUT_STEP_US; 618 timeout->val = timeout->max = timeout_us; 619 620 led->max_timeout_ms = led->flash_timeout_ms = timeout_us / USEC_PER_MSEC; 621 622 flash->ops = &qcom_flash_ops; 623 } 624 625 flash->led_cdev.brightness_set_blocking = qcom_flash_led_brightness_set; 626 627 init_data.fwnode = node; 628 init_data.devicename = NULL; 629 init_data.default_label = NULL; 630 init_data.devname_mandatory = false; 631 632 rc = devm_led_classdev_flash_register_ext(dev, flash, &init_data); 633 if (rc < 0) { 634 dev_err(dev, "Register flash LED classdev failed, rc=%d\n", rc); 635 return rc; 636 } 637 638 return qcom_flash_v4l2_init(dev, led, node); 639 } 640 641 static int qcom_flash_led_probe(struct platform_device *pdev) 642 { 643 struct qcom_flash_data *flash_data; 644 struct qcom_flash_led *led; 645 struct fwnode_handle *child; 646 struct device *dev = &pdev->dev; 647 struct regmap *regmap; 648 struct reg_field *regs; 649 int count, i, rc; 650 u32 val, reg_base; 651 652 flash_data = devm_kzalloc(dev, sizeof(*flash_data), GFP_KERNEL); 653 if (!flash_data) 654 return -ENOMEM; 655 656 regmap = dev_get_regmap(dev->parent, NULL); 657 if (!regmap) { 658 dev_err(dev, "Failed to get parent regmap\n"); 659 return -EINVAL; 660 } 661 662 rc = fwnode_property_read_u32(dev->fwnode, "reg", ®_base); 663 if (rc < 0) { 664 dev_err(dev, "Failed to get register base address, rc=%d\n", rc); 665 return rc; 666 } 667 668 rc = regmap_read(regmap, reg_base + FLASH_TYPE_REG, &val); 669 if (rc < 0) { 670 dev_err(dev, "Read flash LED module type failed, rc=%d\n", rc); 671 return rc; 672 } 673 674 if (val != FLASH_TYPE_VAL) { 675 dev_err(dev, "type %#x is not a flash LED module\n", val); 676 return -ENODEV; 677 } 678 679 rc = regmap_read(regmap, reg_base + FLASH_SUBTYPE_REG, &val); 680 if (rc < 0) { 681 dev_err(dev, "Read flash LED module subtype failed, rc=%d\n", rc); 682 return rc; 683 } 684 685 if (val == FLASH_SUBTYPE_3CH_VAL) { 686 flash_data->hw_type = QCOM_MVFLASH_3CH; 687 flash_data->max_channels = 3; 688 regs = mvflash_3ch_regs; 689 } else if (val == FLASH_SUBTYPE_4CH_VAL) { 690 flash_data->hw_type = QCOM_MVFLASH_4CH; 691 flash_data->max_channels = 4; 692 regs = mvflash_4ch_regs; 693 } else { 694 dev_err(dev, "flash LED subtype %#x is not yet supported\n", val); 695 return -ENODEV; 696 } 697 698 for (i = 0; i < REG_MAX_COUNT; i++) 699 regs[i].reg += reg_base; 700 701 rc = devm_regmap_field_bulk_alloc(dev, regmap, flash_data->r_fields, regs, REG_MAX_COUNT); 702 if (rc < 0) { 703 dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc); 704 return rc; 705 } 706 707 platform_set_drvdata(pdev, flash_data); 708 mutex_init(&flash_data->lock); 709 710 count = device_get_child_node_count(dev); 711 if (count == 0 || count > flash_data->max_channels) { 712 dev_err(dev, "No child or child count exceeds %d\n", flash_data->max_channels); 713 return -EINVAL; 714 } 715 716 flash_data->v4l2_flash = devm_kcalloc(dev, count, 717 sizeof(*flash_data->v4l2_flash), GFP_KERNEL); 718 if (!flash_data->v4l2_flash) 719 return -ENOMEM; 720 721 device_for_each_child_node(dev, child) { 722 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); 723 if (!led) { 724 rc = -ENOMEM; 725 goto release; 726 } 727 728 led->flash_data = flash_data; 729 rc = qcom_flash_register_led_device(dev, child, led); 730 if (rc < 0) 731 goto release; 732 733 flash_data->leds_count++; 734 } 735 736 return 0; 737 738 release: 739 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) 740 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); 741 return rc; 742 } 743 744 static int qcom_flash_led_remove(struct platform_device *pdev) 745 { 746 struct qcom_flash_data *flash_data = platform_get_drvdata(pdev); 747 748 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) 749 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); 750 751 mutex_destroy(&flash_data->lock); 752 return 0; 753 } 754 755 static const struct of_device_id qcom_flash_led_match_table[] = { 756 { .compatible = "qcom,spmi-flash-led" }, 757 { } 758 }; 759 760 MODULE_DEVICE_TABLE(of, qcom_flash_led_match_table); 761 static struct platform_driver qcom_flash_led_driver = { 762 .driver = { 763 .name = "leds-qcom-flash", 764 .of_match_table = qcom_flash_led_match_table, 765 }, 766 .probe = qcom_flash_led_probe, 767 .remove = qcom_flash_led_remove, 768 }; 769 770 module_platform_driver(qcom_flash_led_driver); 771 772 MODULE_DESCRIPTION("QCOM Flash LED driver"); 773 MODULE_LICENSE("GPL"); 774