1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2022, 2024-2025 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_REVISION_REG 0x00 18 #define FLASH_4CH_REVISION_V0P1 0x01 19 20 #define FLASH_TYPE_REG 0x04 21 #define FLASH_TYPE_VAL 0x18 22 23 #define FLASH_SUBTYPE_REG 0x05 24 #define FLASH_SUBTYPE_3CH_PM8150_VAL 0x04 25 #define FLASH_SUBTYPE_3CH_PMI8998_VAL 0x03 26 #define FLASH_SUBTYPE_4CH_VAL 0x07 27 28 #define FLASH_STS_3CH_OTST1 BIT(0) 29 #define FLASH_STS_3CH_OTST2 BIT(1) 30 #define FLASH_STS_3CH_OTST3 BIT(2) 31 #define FLASH_STS_3CH_BOB_THM_OVERLOAD BIT(3) 32 #define FLASH_STS_3CH_VPH_DROOP BIT(4) 33 #define FLASH_STS_3CH_BOB_ILIM_S1 BIT(5) 34 #define FLASH_STS_3CH_BOB_ILIM_S2 BIT(6) 35 #define FLASH_STS_3CH_BCL_IBAT BIT(7) 36 37 #define FLASH_STS_4CH_VPH_LOW BIT(0) 38 #define FLASH_STS_4CH_BCL_IBAT BIT(1) 39 #define FLASH_STS_4CH_BOB_ILIM_S1 BIT(2) 40 #define FLASH_STS_4CH_BOB_ILIM_S2 BIT(3) 41 #define FLASH_STS_4CH_OTST2 BIT(4) 42 #define FLASH_STS_4CH_OTST1 BIT(5) 43 #define FLASH_STS_4CHG_BOB_THM_OVERLOAD BIT(6) 44 45 #define FLASH_TIMER_EN_BIT BIT(7) 46 #define FLASH_TIMER_VAL_MASK GENMASK(6, 0) 47 #define FLASH_TIMER_STEP_MS 10 48 49 #define FLASH_STROBE_HW_SW_SEL_BIT BIT(2) 50 #define SW_STROBE_VAL 0 51 #define HW_STROBE_VAL 1 52 #define FLASH_HW_STROBE_TRIGGER_SEL_BIT BIT(1) 53 #define STROBE_LEVEL_TRIGGER_VAL 0 54 #define STROBE_EDGE_TRIGGER_VAL 1 55 #define FLASH_STROBE_POLARITY_BIT BIT(0) 56 #define STROBE_ACTIVE_HIGH_VAL 1 57 58 #define FLASH_IRES_MASK_4CH BIT(0) 59 #define FLASH_IRES_MASK_3CH GENMASK(1, 0) 60 #define FLASH_IRES_12P5MA_VAL 0 61 #define FLASH_IRES_5MA_VAL_4CH 1 62 #define FLASH_IRES_5MA_VAL_3CH 3 63 64 /* constants */ 65 #define FLASH_CURRENT_MAX_UA 1500000 66 #define TORCH_CURRENT_MAX_UA 500000 67 #define FLASH_TOTAL_CURRENT_MAX_UA 2000000 68 #define FLASH_CURRENT_DEFAULT_UA 1000000 69 #define TORCH_CURRENT_DEFAULT_UA 200000 70 71 #define TORCH_IRES_UA 5000 72 #define FLASH_IRES_UA 12500 73 74 #define FLASH_TIMEOUT_MAX_US 1280000 75 #define FLASH_TIMEOUT_STEP_US 10000 76 77 #define UA_PER_MA 1000 78 79 /* thermal threshold constants */ 80 #define OTST_3CH_MIN_VAL 3 81 #define OTST1_4CH_MIN_VAL 0 82 #define OTST1_4CH_V0P1_MIN_VAL 3 83 #define OTST2_4CH_MIN_VAL 0 84 85 #define OTST1_MAX_CURRENT_MA 1000 86 #define OTST2_MAX_CURRENT_MA 500 87 #define OTST3_MAX_CURRENT_MA 200 88 89 enum hw_type { 90 QCOM_MVFLASH_3CH, 91 QCOM_MVFLASH_4CH, 92 }; 93 94 enum led_mode { 95 FLASH_MODE, 96 TORCH_MODE, 97 }; 98 99 enum led_strobe { 100 SW_STROBE, 101 HW_STROBE, 102 }; 103 104 enum { 105 REG_STATUS1, 106 REG_STATUS2, 107 REG_STATUS3, 108 REG_CHAN_TIMER, 109 REG_ITARGET, 110 REG_MODULE_EN, 111 REG_IRESOLUTION, 112 REG_CHAN_STROBE, 113 REG_CHAN_EN, 114 REG_THERM_THRSH1, 115 REG_THERM_THRSH2, 116 REG_THERM_THRSH3, 117 REG_TORCH_CLAMP, 118 REG_MAX_COUNT, 119 }; 120 121 static const struct reg_field mvflash_3ch_pmi8998_regs[REG_MAX_COUNT] = { 122 [REG_STATUS1] = REG_FIELD(0x08, 0, 5), 123 [REG_STATUS2] = REG_FIELD(0x09, 0, 7), 124 [REG_STATUS3] = REG_FIELD(0x0a, 0, 7), 125 [REG_CHAN_TIMER] = REG_FIELD_ID(0x40, 0, 7, 3, 1), 126 [REG_ITARGET] = REG_FIELD_ID(0x43, 0, 6, 3, 1), 127 [REG_MODULE_EN] = REG_FIELD(0x46, 7, 7), 128 [REG_IRESOLUTION] = REG_FIELD(0x47, 0, 5), 129 [REG_CHAN_STROBE] = REG_FIELD_ID(0x49, 0, 2, 3, 1), 130 [REG_CHAN_EN] = REG_FIELD(0x4c, 0, 2), 131 [REG_THERM_THRSH1] = REG_FIELD(0x56, 0, 2), 132 [REG_THERM_THRSH2] = REG_FIELD(0x57, 0, 2), 133 [REG_THERM_THRSH3] = REG_FIELD(0x58, 0, 2), 134 [REG_TORCH_CLAMP] = REG_FIELD(0xea, 0, 6), 135 }; 136 137 static const struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = { 138 [REG_STATUS1] = REG_FIELD(0x08, 0, 7), 139 [REG_STATUS2] = REG_FIELD(0x09, 0, 7), 140 [REG_STATUS3] = REG_FIELD(0x0a, 0, 7), 141 [REG_CHAN_TIMER] = REG_FIELD_ID(0x40, 0, 7, 3, 1), 142 [REG_ITARGET] = REG_FIELD_ID(0x43, 0, 6, 3, 1), 143 [REG_MODULE_EN] = REG_FIELD(0x46, 7, 7), 144 [REG_IRESOLUTION] = REG_FIELD(0x47, 0, 5), 145 [REG_CHAN_STROBE] = REG_FIELD_ID(0x49, 0, 2, 3, 1), 146 [REG_CHAN_EN] = REG_FIELD(0x4c, 0, 2), 147 [REG_THERM_THRSH1] = REG_FIELD(0x56, 0, 2), 148 [REG_THERM_THRSH2] = REG_FIELD(0x57, 0, 2), 149 [REG_THERM_THRSH3] = REG_FIELD(0x58, 0, 2), 150 [REG_TORCH_CLAMP] = REG_FIELD(0xec, 0, 6), 151 }; 152 153 static const struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = { 154 [REG_STATUS1] = REG_FIELD(0x06, 0, 7), 155 [REG_STATUS2] = REG_FIELD(0x07, 0, 6), 156 [REG_STATUS3] = REG_FIELD(0x09, 0, 7), 157 [REG_CHAN_TIMER] = REG_FIELD_ID(0x3e, 0, 7, 4, 1), 158 [REG_ITARGET] = REG_FIELD_ID(0x42, 0, 6, 4, 1), 159 [REG_MODULE_EN] = REG_FIELD(0x46, 7, 7), 160 [REG_IRESOLUTION] = REG_FIELD(0x49, 0, 3), 161 [REG_CHAN_STROBE] = REG_FIELD_ID(0x4a, 0, 6, 4, 1), 162 [REG_CHAN_EN] = REG_FIELD(0x4e, 0, 3), 163 [REG_THERM_THRSH1] = REG_FIELD(0x7a, 0, 2), 164 [REG_THERM_THRSH2] = REG_FIELD(0x78, 0, 2), 165 [REG_TORCH_CLAMP] = REG_FIELD(0xed, 0, 6), 166 }; 167 168 struct qcom_flash_data { 169 struct v4l2_flash **v4l2_flash; 170 struct regmap_field *r_fields[REG_MAX_COUNT]; 171 struct mutex lock; 172 enum hw_type hw_type; 173 u32 total_ma; 174 u8 leds_count; 175 u8 max_channels; 176 u8 chan_en_bits; 177 u8 revision; 178 u8 torch_clamp; 179 }; 180 181 struct qcom_flash_led { 182 struct qcom_flash_data *flash_data; 183 struct led_classdev_flash flash; 184 u32 max_flash_current_ma; 185 u32 max_torch_current_ma; 186 u32 max_timeout_ms; 187 u32 flash_current_ma; 188 u32 flash_timeout_ms; 189 u32 current_in_use_ma; 190 u8 *chan_id; 191 u8 chan_count; 192 bool enabled; 193 }; 194 195 static int set_flash_module_en(struct qcom_flash_led *led, bool en) 196 { 197 struct qcom_flash_data *flash_data = led->flash_data; 198 u8 led_mask = 0, enable; 199 int i, rc; 200 201 for (i = 0; i < led->chan_count; i++) 202 led_mask |= BIT(led->chan_id[i]); 203 204 mutex_lock(&flash_data->lock); 205 if (en) 206 flash_data->chan_en_bits |= led_mask; 207 else 208 flash_data->chan_en_bits &= ~led_mask; 209 210 enable = !!flash_data->chan_en_bits; 211 rc = regmap_field_write(flash_data->r_fields[REG_MODULE_EN], enable); 212 if (rc) 213 dev_err(led->flash.led_cdev.dev, "write module_en failed, rc=%d\n", rc); 214 mutex_unlock(&flash_data->lock); 215 216 return rc; 217 } 218 219 static int update_allowed_flash_current(struct qcom_flash_led *led, u32 *current_ma, bool strobe) 220 { 221 struct qcom_flash_data *flash_data = led->flash_data; 222 u32 therm_ma, avail_ma, thrsh[3], min_thrsh, sts; 223 int rc = 0; 224 225 mutex_lock(&flash_data->lock); 226 /* 227 * Put previously allocated current into allowed budget in either of these two cases: 228 * 1) LED is disabled; 229 * 2) LED is enabled repeatedly 230 */ 231 if (!strobe || led->current_in_use_ma != 0) { 232 if (flash_data->total_ma >= led->current_in_use_ma) 233 flash_data->total_ma -= led->current_in_use_ma; 234 else 235 flash_data->total_ma = 0; 236 237 led->current_in_use_ma = 0; 238 if (!strobe) 239 goto unlock; 240 } 241 242 /* 243 * Cache the default thermal threshold settings, and set them to the lowest levels before 244 * reading over-temp real time status. If over-temp has been triggered at the lowest 245 * threshold, it's very likely that it would be triggered at a higher (default) threshold 246 * when more flash current is requested. Prevent device from triggering over-temp condition 247 * by limiting the flash current for the new request. 248 */ 249 rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH1], &thrsh[0]); 250 if (rc < 0) 251 goto unlock; 252 253 rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH2], &thrsh[1]); 254 if (rc < 0) 255 goto unlock; 256 257 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 258 rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH3], &thrsh[2]); 259 if (rc < 0) 260 goto unlock; 261 } 262 263 min_thrsh = OTST_3CH_MIN_VAL; 264 if (flash_data->hw_type == QCOM_MVFLASH_4CH) 265 min_thrsh = (flash_data->revision == FLASH_4CH_REVISION_V0P1) ? 266 OTST1_4CH_V0P1_MIN_VAL : OTST1_4CH_MIN_VAL; 267 268 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH1], min_thrsh); 269 if (rc < 0) 270 goto unlock; 271 272 if (flash_data->hw_type == QCOM_MVFLASH_4CH) 273 min_thrsh = OTST2_4CH_MIN_VAL; 274 275 /* 276 * The default thermal threshold settings have been updated hence 277 * restore them if any fault happens starting from here. 278 */ 279 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH2], min_thrsh); 280 if (rc < 0) 281 goto restore; 282 283 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 284 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH3], min_thrsh); 285 if (rc < 0) 286 goto restore; 287 } 288 289 /* Read thermal level status to get corresponding derating flash current */ 290 rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &sts); 291 if (rc) 292 goto restore; 293 294 therm_ma = FLASH_TOTAL_CURRENT_MAX_UA / 1000; 295 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 296 if (sts & FLASH_STS_3CH_OTST3) 297 therm_ma = OTST3_MAX_CURRENT_MA; 298 else if (sts & FLASH_STS_3CH_OTST2) 299 therm_ma = OTST2_MAX_CURRENT_MA; 300 else if (sts & FLASH_STS_3CH_OTST1) 301 therm_ma = OTST1_MAX_CURRENT_MA; 302 } else { 303 if (sts & FLASH_STS_4CH_OTST2) 304 therm_ma = OTST2_MAX_CURRENT_MA; 305 else if (sts & FLASH_STS_4CH_OTST1) 306 therm_ma = OTST1_MAX_CURRENT_MA; 307 } 308 309 /* Calculate the allowed flash current for the request */ 310 if (therm_ma <= flash_data->total_ma) 311 avail_ma = 0; 312 else 313 avail_ma = therm_ma - flash_data->total_ma; 314 315 *current_ma = min_t(u32, *current_ma, avail_ma); 316 led->current_in_use_ma = *current_ma; 317 flash_data->total_ma += led->current_in_use_ma; 318 319 dev_dbg(led->flash.led_cdev.dev, "allowed flash current: %dmA, total current: %dmA\n", 320 led->current_in_use_ma, flash_data->total_ma); 321 322 restore: 323 /* Restore to default thermal threshold settings */ 324 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH1], thrsh[0]); 325 if (rc < 0) 326 goto unlock; 327 328 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH2], thrsh[1]); 329 if (rc < 0) 330 goto unlock; 331 332 if (flash_data->hw_type == QCOM_MVFLASH_3CH) 333 rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH3], thrsh[2]); 334 335 unlock: 336 mutex_unlock(&flash_data->lock); 337 return rc; 338 } 339 340 static int set_flash_current(struct qcom_flash_led *led, u32 current_ma, enum led_mode mode) 341 { 342 struct qcom_flash_data *flash_data = led->flash_data; 343 u32 itarg_ua, ires_ua; 344 u8 shift, ires_mask = 0, ires_val = 0, chan_id; 345 int i, rc; 346 347 /* 348 * Split the current across the channels and set the 349 * IRESOLUTION and ITARGET registers accordingly. 350 */ 351 itarg_ua = (current_ma * UA_PER_MA) / led->chan_count + 1; 352 ires_ua = (mode == FLASH_MODE) ? FLASH_IRES_UA : TORCH_IRES_UA; 353 354 for (i = 0; i < led->chan_count; i++) { 355 u8 itarget = 0; 356 357 if (itarg_ua > ires_ua) 358 itarget = itarg_ua / ires_ua - 1; 359 360 chan_id = led->chan_id[i]; 361 362 rc = regmap_fields_write(flash_data->r_fields[REG_ITARGET], chan_id, itarget); 363 if (rc) 364 return rc; 365 366 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 367 shift = chan_id * 2; 368 ires_mask |= FLASH_IRES_MASK_3CH << shift; 369 ires_val |= ((mode == FLASH_MODE) ? 370 (FLASH_IRES_12P5MA_VAL << shift) : 371 (FLASH_IRES_5MA_VAL_3CH << shift)); 372 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 373 shift = chan_id; 374 ires_mask |= FLASH_IRES_MASK_4CH << shift; 375 ires_val |= ((mode == FLASH_MODE) ? 376 (FLASH_IRES_12P5MA_VAL << shift) : 377 (FLASH_IRES_5MA_VAL_4CH << shift)); 378 } else { 379 dev_err(led->flash.led_cdev.dev, 380 "HW type %d is not supported\n", flash_data->hw_type); 381 return -EOPNOTSUPP; 382 } 383 } 384 385 return regmap_field_update_bits(flash_data->r_fields[REG_IRESOLUTION], ires_mask, ires_val); 386 } 387 388 static int set_flash_timeout(struct qcom_flash_led *led, u32 timeout_ms) 389 { 390 struct qcom_flash_data *flash_data = led->flash_data; 391 u8 timer, chan_id; 392 int rc, i; 393 394 /* set SAFETY_TIMER for all the channels connected to the same LED */ 395 timeout_ms = min_t(u32, timeout_ms, led->max_timeout_ms); 396 397 for (i = 0; i < led->chan_count; i++) { 398 chan_id = led->chan_id[i]; 399 400 timer = timeout_ms / FLASH_TIMER_STEP_MS; 401 timer = clamp_t(u8, timer, 0, FLASH_TIMER_VAL_MASK); 402 403 if (timeout_ms) 404 timer |= FLASH_TIMER_EN_BIT; 405 406 rc = regmap_fields_write(flash_data->r_fields[REG_CHAN_TIMER], chan_id, timer); 407 if (rc) 408 return rc; 409 } 410 411 return 0; 412 } 413 414 static int set_flash_strobe(struct qcom_flash_led *led, enum led_strobe strobe, bool state) 415 { 416 struct qcom_flash_data *flash_data = led->flash_data; 417 u8 strobe_sel, chan_en, chan_id, chan_mask = 0; 418 int rc, i; 419 420 /* Set SW strobe config for all channels connected to the LED */ 421 for (i = 0; i < led->chan_count; i++) { 422 chan_id = led->chan_id[i]; 423 424 if (strobe == SW_STROBE) 425 strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, SW_STROBE_VAL); 426 else 427 strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, HW_STROBE_VAL); 428 429 strobe_sel |= 430 FIELD_PREP(FLASH_HW_STROBE_TRIGGER_SEL_BIT, STROBE_LEVEL_TRIGGER_VAL) | 431 FIELD_PREP(FLASH_STROBE_POLARITY_BIT, STROBE_ACTIVE_HIGH_VAL); 432 433 rc = regmap_fields_write( 434 flash_data->r_fields[REG_CHAN_STROBE], chan_id, strobe_sel); 435 if (rc) 436 return rc; 437 438 chan_mask |= BIT(chan_id); 439 } 440 441 /* Enable/disable flash channels */ 442 chan_en = state ? chan_mask : 0; 443 rc = regmap_field_update_bits(flash_data->r_fields[REG_CHAN_EN], chan_mask, chan_en); 444 if (rc) 445 return rc; 446 447 led->enabled = state; 448 return 0; 449 } 450 451 static inline struct qcom_flash_led *flcdev_to_qcom_fled(struct led_classdev_flash *flcdev) 452 { 453 return container_of(flcdev, struct qcom_flash_led, flash); 454 } 455 456 static int qcom_flash_brightness_set(struct led_classdev_flash *fled_cdev, u32 brightness) 457 { 458 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 459 460 led->flash_current_ma = min_t(u32, led->max_flash_current_ma, brightness / UA_PER_MA); 461 return 0; 462 } 463 464 static int qcom_flash_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout) 465 { 466 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 467 468 led->flash_timeout_ms = timeout / USEC_PER_MSEC; 469 return 0; 470 } 471 472 static int qcom_flash_strobe_set(struct led_classdev_flash *fled_cdev, bool state) 473 { 474 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 475 int rc; 476 477 rc = set_flash_strobe(led, SW_STROBE, false); 478 if (rc) 479 return rc; 480 481 rc = update_allowed_flash_current(led, &led->flash_current_ma, state); 482 if (rc < 0) 483 return rc; 484 485 rc = set_flash_current(led, led->flash_current_ma, FLASH_MODE); 486 if (rc) 487 return rc; 488 489 rc = set_flash_timeout(led, led->flash_timeout_ms); 490 if (rc) 491 return rc; 492 493 rc = set_flash_module_en(led, state); 494 if (rc) 495 return rc; 496 497 return set_flash_strobe(led, SW_STROBE, state); 498 } 499 500 static int qcom_flash_strobe_get(struct led_classdev_flash *fled_cdev, bool *state) 501 { 502 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 503 504 *state = led->enabled; 505 return 0; 506 } 507 508 static int qcom_flash_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault) 509 { 510 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 511 struct qcom_flash_data *flash_data = led->flash_data; 512 u8 shift, chan_id, chan_mask = 0; 513 u8 ot_mask = 0, oc_mask = 0, uv_mask = 0; 514 u32 val, fault_sts = 0; 515 int i, rc; 516 517 rc = regmap_field_read(flash_data->r_fields[REG_STATUS1], &val); 518 if (rc) 519 return rc; 520 521 for (i = 0; i < led->chan_count; i++) { 522 chan_id = led->chan_id[i]; 523 shift = chan_id * 2; 524 525 if (val & BIT(shift)) 526 fault_sts |= LED_FAULT_SHORT_CIRCUIT; 527 528 chan_mask |= BIT(chan_id); 529 } 530 531 rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &val); 532 if (rc) 533 return rc; 534 535 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 536 ot_mask = FLASH_STS_3CH_OTST1 | 537 FLASH_STS_3CH_OTST2 | 538 FLASH_STS_3CH_OTST3 | 539 FLASH_STS_3CH_BOB_THM_OVERLOAD; 540 oc_mask = FLASH_STS_3CH_BOB_ILIM_S1 | 541 FLASH_STS_3CH_BOB_ILIM_S2 | 542 FLASH_STS_3CH_BCL_IBAT; 543 uv_mask = FLASH_STS_3CH_VPH_DROOP; 544 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 545 ot_mask = FLASH_STS_4CH_OTST2 | 546 FLASH_STS_4CH_OTST1 | 547 FLASH_STS_4CHG_BOB_THM_OVERLOAD; 548 oc_mask = FLASH_STS_4CH_BCL_IBAT | 549 FLASH_STS_4CH_BOB_ILIM_S1 | 550 FLASH_STS_4CH_BOB_ILIM_S2; 551 uv_mask = FLASH_STS_4CH_VPH_LOW; 552 } 553 554 if (val & ot_mask) 555 fault_sts |= LED_FAULT_OVER_TEMPERATURE; 556 557 if (val & oc_mask) 558 fault_sts |= LED_FAULT_OVER_CURRENT; 559 560 if (val & uv_mask) 561 fault_sts |= LED_FAULT_INPUT_VOLTAGE; 562 563 rc = regmap_field_read(flash_data->r_fields[REG_STATUS3], &val); 564 if (rc) 565 return rc; 566 567 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { 568 if (val & chan_mask) 569 fault_sts |= LED_FAULT_TIMEOUT; 570 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { 571 for (i = 0; i < led->chan_count; i++) { 572 chan_id = led->chan_id[i]; 573 shift = chan_id * 2; 574 575 if (val & BIT(shift)) 576 fault_sts |= LED_FAULT_TIMEOUT; 577 } 578 } 579 580 *fault = fault_sts; 581 return 0; 582 } 583 584 static int qcom_flash_led_brightness_set(struct led_classdev *led_cdev, 585 enum led_brightness brightness) 586 { 587 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); 588 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 589 u32 current_ma = brightness * led->max_torch_current_ma / LED_FULL; 590 bool enable = !!brightness; 591 int rc; 592 593 rc = set_flash_strobe(led, SW_STROBE, false); 594 if (rc) 595 return rc; 596 597 rc = set_flash_module_en(led, false); 598 if (rc) 599 return rc; 600 601 rc = update_allowed_flash_current(led, ¤t_ma, enable); 602 if (rc < 0) 603 return rc; 604 605 rc = set_flash_current(led, current_ma, TORCH_MODE); 606 if (rc) 607 return rc; 608 609 /* Disable flash timeout for torch LED */ 610 rc = set_flash_timeout(led, 0); 611 if (rc) 612 return rc; 613 614 rc = set_flash_module_en(led, enable); 615 if (rc) 616 return rc; 617 618 return set_flash_strobe(led, SW_STROBE, enable); 619 } 620 621 static const struct led_flash_ops qcom_flash_ops = { 622 .flash_brightness_set = qcom_flash_brightness_set, 623 .strobe_set = qcom_flash_strobe_set, 624 .strobe_get = qcom_flash_strobe_get, 625 .timeout_set = qcom_flash_timeout_set, 626 .fault_get = qcom_flash_fault_get, 627 }; 628 629 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) 630 static int qcom_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, bool enable) 631 { 632 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 633 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 634 int rc; 635 636 rc = set_flash_module_en(led, enable); 637 if (rc) 638 return rc; 639 640 if (enable) 641 return set_flash_strobe(led, HW_STROBE, true); 642 else 643 return set_flash_strobe(led, SW_STROBE, false); 644 } 645 646 static enum led_brightness 647 qcom_flash_intensity_to_led_brightness(struct v4l2_flash *v4l2_flash, s32 intensity) 648 { 649 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 650 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 651 u32 current_ma = intensity / UA_PER_MA; 652 653 current_ma = min_t(u32, current_ma, led->max_torch_current_ma); 654 if (!current_ma) 655 return LED_OFF; 656 657 return (current_ma * LED_FULL) / led->max_torch_current_ma; 658 } 659 660 static s32 qcom_flash_brightness_to_led_intensity(struct v4l2_flash *v4l2_flash, 661 enum led_brightness brightness) 662 { 663 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; 664 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); 665 666 return (brightness * led->max_torch_current_ma * UA_PER_MA) / LED_FULL; 667 } 668 669 static const struct v4l2_flash_ops qcom_v4l2_flash_ops = { 670 .external_strobe_set = qcom_flash_external_strobe_set, 671 .intensity_to_led_brightness = qcom_flash_intensity_to_led_brightness, 672 .led_brightness_to_intensity = qcom_flash_brightness_to_led_intensity, 673 }; 674 675 static int 676 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) 677 { 678 struct qcom_flash_data *flash_data = led->flash_data; 679 struct v4l2_flash_config v4l2_cfg = { 0 }; 680 struct led_flash_setting *intensity = &v4l2_cfg.intensity; 681 struct v4l2_flash *v4l2_flash; 682 683 if (!(led->flash.led_cdev.flags & LED_DEV_CAP_FLASH)) 684 return 0; 685 686 intensity->min = intensity->step = TORCH_IRES_UA * led->chan_count; 687 intensity->max = led->max_torch_current_ma * UA_PER_MA; 688 intensity->val = min_t(u32, intensity->max, TORCH_CURRENT_DEFAULT_UA); 689 690 strscpy(v4l2_cfg.dev_name, led->flash.led_cdev.dev->kobj.name, 691 sizeof(v4l2_cfg.dev_name)); 692 693 v4l2_cfg.has_external_strobe = true; 694 v4l2_cfg.flash_faults = LED_FAULT_INPUT_VOLTAGE | 695 LED_FAULT_OVER_CURRENT | 696 LED_FAULT_SHORT_CIRCUIT | 697 LED_FAULT_OVER_TEMPERATURE | 698 LED_FAULT_TIMEOUT; 699 700 v4l2_flash = v4l2_flash_init(dev, fwnode, &led->flash, &qcom_v4l2_flash_ops, &v4l2_cfg); 701 if (IS_ERR(v4l2_flash)) 702 return PTR_ERR(v4l2_flash); 703 704 flash_data->v4l2_flash[flash_data->leds_count] = v4l2_flash; 705 return 0; 706 } 707 # else 708 static int 709 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) 710 { 711 return 0; 712 } 713 #endif 714 715 static int qcom_flash_register_led_device(struct device *dev, 716 struct fwnode_handle *node, struct qcom_flash_led *led) 717 { 718 struct qcom_flash_data *flash_data = led->flash_data; 719 struct led_init_data init_data; 720 struct led_classdev_flash *flash = &led->flash; 721 struct led_flash_setting *brightness, *timeout; 722 u32 current_ua, timeout_us; 723 u32 channels[4]; 724 int i, rc, count; 725 u8 torch_clamp; 726 727 count = fwnode_property_count_u32(node, "led-sources"); 728 if (count <= 0) { 729 dev_err(dev, "No led-sources specified\n"); 730 return -ENODEV; 731 } 732 733 if (count > flash_data->max_channels) { 734 dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n", 735 count, flash_data->max_channels); 736 return -EINVAL; 737 } 738 739 rc = fwnode_property_read_u32_array(node, "led-sources", channels, count); 740 if (rc < 0) { 741 dev_err(dev, "Failed to read led-sources property, rc=%d\n", rc); 742 return rc; 743 } 744 745 led->chan_count = count; 746 led->chan_id = devm_kcalloc(dev, count, sizeof(u8), GFP_KERNEL); 747 if (!led->chan_id) 748 return -ENOMEM; 749 750 for (i = 0; i < count; i++) { 751 if ((channels[i] == 0) || (channels[i] > flash_data->max_channels)) { 752 dev_err(dev, "led-source out of HW support range [1-%u]\n", 753 flash_data->max_channels); 754 return -EINVAL; 755 } 756 757 /* Make chan_id indexing from 0 */ 758 led->chan_id[i] = channels[i] - 1; 759 } 760 761 rc = fwnode_property_read_u32(node, "led-max-microamp", ¤t_ua); 762 if (rc < 0) { 763 dev_err(dev, "Failed to read led-max-microamp property, rc=%d\n", rc); 764 return rc; 765 } 766 767 if (current_ua == 0) { 768 dev_err(dev, "led-max-microamp shouldn't be 0\n"); 769 return -EINVAL; 770 } 771 772 current_ua = min_t(u32, current_ua, TORCH_CURRENT_MAX_UA * led->chan_count); 773 led->max_torch_current_ma = current_ua / UA_PER_MA; 774 775 torch_clamp = (current_ua / led->chan_count) / TORCH_IRES_UA; 776 if (torch_clamp != 0) 777 torch_clamp--; 778 779 flash_data->torch_clamp = max_t(u8, flash_data->torch_clamp, torch_clamp); 780 781 if (fwnode_property_present(node, "flash-max-microamp")) { 782 flash->led_cdev.flags |= LED_DEV_CAP_FLASH; 783 784 rc = fwnode_property_read_u32(node, "flash-max-microamp", ¤t_ua); 785 if (rc < 0) { 786 dev_err(dev, "Failed to read flash-max-microamp property, rc=%d\n", 787 rc); 788 return rc; 789 } 790 791 current_ua = min_t(u32, current_ua, FLASH_CURRENT_MAX_UA * led->chan_count); 792 current_ua = min_t(u32, current_ua, FLASH_TOTAL_CURRENT_MAX_UA); 793 794 /* Initialize flash class LED device brightness settings */ 795 brightness = &flash->brightness; 796 brightness->min = brightness->step = FLASH_IRES_UA * led->chan_count; 797 brightness->max = current_ua; 798 brightness->val = min_t(u32, current_ua, FLASH_CURRENT_DEFAULT_UA); 799 800 led->max_flash_current_ma = current_ua / UA_PER_MA; 801 led->flash_current_ma = brightness->val / UA_PER_MA; 802 803 rc = fwnode_property_read_u32(node, "flash-max-timeout-us", &timeout_us); 804 if (rc < 0) { 805 dev_err(dev, "Failed to read flash-max-timeout-us property, rc=%d\n", 806 rc); 807 return rc; 808 } 809 810 timeout_us = min_t(u32, timeout_us, FLASH_TIMEOUT_MAX_US); 811 812 /* Initialize flash class LED device timeout settings */ 813 timeout = &flash->timeout; 814 timeout->min = timeout->step = FLASH_TIMEOUT_STEP_US; 815 timeout->val = timeout->max = timeout_us; 816 817 led->max_timeout_ms = led->flash_timeout_ms = timeout_us / USEC_PER_MSEC; 818 819 flash->ops = &qcom_flash_ops; 820 } 821 822 flash->led_cdev.brightness_set_blocking = qcom_flash_led_brightness_set; 823 824 init_data.fwnode = node; 825 init_data.devicename = NULL; 826 init_data.default_label = NULL; 827 init_data.devname_mandatory = false; 828 829 rc = devm_led_classdev_flash_register_ext(dev, flash, &init_data); 830 if (rc < 0) { 831 dev_err(dev, "Register flash LED classdev failed, rc=%d\n", rc); 832 return rc; 833 } 834 835 return qcom_flash_v4l2_init(dev, led, node); 836 } 837 838 static int qcom_flash_led_probe(struct platform_device *pdev) 839 { 840 struct qcom_flash_data *flash_data; 841 struct qcom_flash_led *led; 842 struct device *dev = &pdev->dev; 843 struct regmap *regmap; 844 struct reg_field *regs; 845 int count, i, rc; 846 u32 val, reg_base; 847 848 flash_data = devm_kzalloc(dev, sizeof(*flash_data), GFP_KERNEL); 849 if (!flash_data) 850 return -ENOMEM; 851 852 regmap = dev_get_regmap(dev->parent, NULL); 853 if (!regmap) { 854 dev_err(dev, "Failed to get parent regmap\n"); 855 return -EINVAL; 856 } 857 858 rc = fwnode_property_read_u32(dev->fwnode, "reg", ®_base); 859 if (rc < 0) { 860 dev_err(dev, "Failed to get register base address, rc=%d\n", rc); 861 return rc; 862 } 863 864 rc = regmap_read(regmap, reg_base + FLASH_TYPE_REG, &val); 865 if (rc < 0) { 866 dev_err(dev, "Read flash LED module type failed, rc=%d\n", rc); 867 return rc; 868 } 869 870 if (val != FLASH_TYPE_VAL) { 871 dev_err(dev, "type %#x is not a flash LED module\n", val); 872 return -ENODEV; 873 } 874 875 rc = regmap_read(regmap, reg_base + FLASH_SUBTYPE_REG, &val); 876 if (rc < 0) { 877 dev_err(dev, "Read flash LED module subtype failed, rc=%d\n", rc); 878 return rc; 879 } 880 881 if (val == FLASH_SUBTYPE_3CH_PM8150_VAL) { 882 flash_data->hw_type = QCOM_MVFLASH_3CH; 883 flash_data->max_channels = 3; 884 regs = devm_kmemdup(dev, mvflash_3ch_regs, sizeof(mvflash_3ch_regs), 885 GFP_KERNEL); 886 if (!regs) 887 return -ENOMEM; 888 } else if (val == FLASH_SUBTYPE_3CH_PMI8998_VAL) { 889 flash_data->hw_type = QCOM_MVFLASH_3CH; 890 flash_data->max_channels = 3; 891 regs = devm_kmemdup(dev, mvflash_3ch_pmi8998_regs, 892 sizeof(mvflash_3ch_pmi8998_regs), GFP_KERNEL); 893 if (!regs) 894 return -ENOMEM; 895 } else if (val == FLASH_SUBTYPE_4CH_VAL) { 896 flash_data->hw_type = QCOM_MVFLASH_4CH; 897 flash_data->max_channels = 4; 898 regs = devm_kmemdup(dev, mvflash_4ch_regs, sizeof(mvflash_4ch_regs), 899 GFP_KERNEL); 900 if (!regs) 901 return -ENOMEM; 902 903 rc = regmap_read(regmap, reg_base + FLASH_REVISION_REG, &val); 904 if (rc < 0) { 905 dev_err(dev, "Failed to read flash LED module revision, rc=%d\n", rc); 906 return rc; 907 } 908 909 flash_data->revision = val; 910 } else { 911 dev_err(dev, "flash LED subtype %#x is not yet supported\n", val); 912 return -ENODEV; 913 } 914 915 for (i = 0; i < REG_MAX_COUNT; i++) 916 regs[i].reg += reg_base; 917 918 rc = devm_regmap_field_bulk_alloc(dev, regmap, flash_data->r_fields, regs, REG_MAX_COUNT); 919 if (rc < 0) { 920 dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc); 921 return rc; 922 } 923 devm_kfree(dev, regs); /* devm_regmap_field_bulk_alloc() makes copies */ 924 925 platform_set_drvdata(pdev, flash_data); 926 mutex_init(&flash_data->lock); 927 928 count = device_get_child_node_count(dev); 929 if (count == 0 || count > flash_data->max_channels) { 930 dev_err(dev, "No child or child count exceeds %d\n", flash_data->max_channels); 931 return -EINVAL; 932 } 933 934 flash_data->v4l2_flash = devm_kcalloc(dev, count, 935 sizeof(*flash_data->v4l2_flash), GFP_KERNEL); 936 if (!flash_data->v4l2_flash) 937 return -ENOMEM; 938 939 device_for_each_child_node_scoped(dev, child) { 940 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); 941 if (!led) { 942 rc = -ENOMEM; 943 goto release; 944 } 945 946 led->flash_data = flash_data; 947 rc = qcom_flash_register_led_device(dev, child, led); 948 if (rc < 0) 949 goto release; 950 951 flash_data->leds_count++; 952 } 953 954 return regmap_field_write(flash_data->r_fields[REG_TORCH_CLAMP], flash_data->torch_clamp); 955 release: 956 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) 957 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); 958 return rc; 959 } 960 961 static void qcom_flash_led_remove(struct platform_device *pdev) 962 { 963 struct qcom_flash_data *flash_data = platform_get_drvdata(pdev); 964 965 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) 966 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); 967 968 mutex_destroy(&flash_data->lock); 969 } 970 971 static const struct of_device_id qcom_flash_led_match_table[] = { 972 { .compatible = "qcom,spmi-flash-led" }, 973 { } 974 }; 975 976 MODULE_DEVICE_TABLE(of, qcom_flash_led_match_table); 977 static struct platform_driver qcom_flash_led_driver = { 978 .driver = { 979 .name = "leds-qcom-flash", 980 .of_match_table = qcom_flash_led_match_table, 981 }, 982 .probe = qcom_flash_led_probe, 983 .remove = qcom_flash_led_remove, 984 }; 985 986 module_platform_driver(qcom_flash_led_driver); 987 988 MODULE_DESCRIPTION("QCOM Flash LED driver"); 989 MODULE_LICENSE("GPL"); 990