0001
0002
0003
0004
0005 #include <linux/delay.h>
0006 #include <linux/interrupt.h>
0007 #include <linux/ktime.h>
0008 #include <linux/kernel.h>
0009 #include <linux/backlight.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/of_device.h>
0013 #include <linux/of_address.h>
0014 #include <linux/regmap.h>
0015
0016
0017 #define WLED_MAX_STRINGS 4
0018 #define MOD_A 0
0019 #define MOD_B 1
0020
0021 #define WLED_DEFAULT_BRIGHTNESS 2048
0022 #define WLED_SOFT_START_DLY_US 10000
0023 #define WLED3_SINK_REG_BRIGHT_MAX 0xFFF
0024 #define WLED5_SINK_REG_BRIGHT_MAX_12B 0xFFF
0025 #define WLED5_SINK_REG_BRIGHT_MAX_15B 0x7FFF
0026
0027
0028 #define WLED3_CTRL_REG_FAULT_STATUS 0x08
0029 #define WLED3_CTRL_REG_ILIM_FAULT_BIT BIT(0)
0030 #define WLED3_CTRL_REG_OVP_FAULT_BIT BIT(1)
0031 #define WLED4_CTRL_REG_SC_FAULT_BIT BIT(2)
0032 #define WLED5_CTRL_REG_OVP_PRE_ALARM_BIT BIT(4)
0033
0034 #define WLED3_CTRL_REG_INT_RT_STS 0x10
0035 #define WLED3_CTRL_REG_OVP_FAULT_STATUS BIT(1)
0036
0037 #define WLED3_CTRL_REG_MOD_EN 0x46
0038 #define WLED3_CTRL_REG_MOD_EN_MASK BIT(7)
0039 #define WLED3_CTRL_REG_MOD_EN_SHIFT 7
0040
0041 #define WLED3_CTRL_REG_FEEDBACK_CONTROL 0x48
0042
0043 #define WLED3_CTRL_REG_FREQ 0x4c
0044 #define WLED3_CTRL_REG_FREQ_MASK GENMASK(3, 0)
0045
0046 #define WLED3_CTRL_REG_OVP 0x4d
0047 #define WLED3_CTRL_REG_OVP_MASK GENMASK(1, 0)
0048 #define WLED5_CTRL_REG_OVP_MASK GENMASK(3, 0)
0049
0050 #define WLED3_CTRL_REG_ILIMIT 0x4e
0051 #define WLED3_CTRL_REG_ILIMIT_MASK GENMASK(2, 0)
0052
0053
0054 #define WLED3_SINK_REG_SYNC 0x47
0055 #define WLED3_SINK_REG_SYNC_CLEAR 0x00
0056
0057 #define WLED3_SINK_REG_CURR_SINK 0x4f
0058 #define WLED3_SINK_REG_CURR_SINK_MASK GENMASK(7, 5)
0059 #define WLED3_SINK_REG_CURR_SINK_SHFT 5
0060
0061
0062 #define WLED3_SINK_REG_BRIGHT(n) (0x40 + n)
0063
0064 #define WLED3_SINK_REG_STR_MOD_EN(n) (0x60 + (n * 0x10))
0065 #define WLED3_SINK_REG_STR_MOD_MASK BIT(7)
0066
0067 #define WLED3_SINK_REG_STR_FULL_SCALE_CURR(n) (0x62 + (n * 0x10))
0068 #define WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK GENMASK(4, 0)
0069
0070 #define WLED3_SINK_REG_STR_MOD_SRC(n) (0x63 + (n * 0x10))
0071 #define WLED3_SINK_REG_STR_MOD_SRC_MASK BIT(0)
0072 #define WLED3_SINK_REG_STR_MOD_SRC_INT 0x00
0073 #define WLED3_SINK_REG_STR_MOD_SRC_EXT 0x01
0074
0075 #define WLED3_SINK_REG_STR_CABC(n) (0x66 + (n * 0x10))
0076 #define WLED3_SINK_REG_STR_CABC_MASK BIT(7)
0077
0078
0079 #define WLED4_CTRL_REG_SHORT_PROTECT 0x5e
0080 #define WLED4_CTRL_REG_SHORT_EN_MASK BIT(7)
0081
0082 #define WLED4_CTRL_REG_SEC_ACCESS 0xd0
0083 #define WLED4_CTRL_REG_SEC_UNLOCK 0xa5
0084
0085 #define WLED4_CTRL_REG_TEST1 0xe2
0086 #define WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2 0x09
0087
0088
0089 #define WLED4_SINK_REG_CURR_SINK 0x46
0090 #define WLED4_SINK_REG_CURR_SINK_MASK GENMASK(7, 4)
0091 #define WLED4_SINK_REG_CURR_SINK_SHFT 4
0092
0093
0094 #define WLED4_SINK_REG_STR_MOD_EN(n) (0x50 + (n * 0x10))
0095 #define WLED4_SINK_REG_STR_MOD_MASK BIT(7)
0096
0097 #define WLED4_SINK_REG_STR_FULL_SCALE_CURR(n) (0x52 + (n * 0x10))
0098 #define WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK GENMASK(3, 0)
0099
0100 #define WLED4_SINK_REG_STR_MOD_SRC(n) (0x53 + (n * 0x10))
0101 #define WLED4_SINK_REG_STR_MOD_SRC_MASK BIT(0)
0102 #define WLED4_SINK_REG_STR_MOD_SRC_INT 0x00
0103 #define WLED4_SINK_REG_STR_MOD_SRC_EXT 0x01
0104
0105 #define WLED4_SINK_REG_STR_CABC(n) (0x56 + (n * 0x10))
0106 #define WLED4_SINK_REG_STR_CABC_MASK BIT(7)
0107
0108 #define WLED4_SINK_REG_BRIGHT(n) (0x57 + (n * 0x10))
0109
0110
0111 #define WLED5_CTRL_REG_OVP_INT_CTL 0x5f
0112 #define WLED5_CTRL_REG_OVP_INT_TIMER_MASK GENMASK(2, 0)
0113
0114
0115 #define WLED5_SINK_REG_MOD_A_EN 0x50
0116 #define WLED5_SINK_REG_MOD_B_EN 0x60
0117 #define WLED5_SINK_REG_MOD_EN_MASK BIT(7)
0118
0119 #define WLED5_SINK_REG_MOD_A_SRC_SEL 0x51
0120 #define WLED5_SINK_REG_MOD_B_SRC_SEL 0x61
0121 #define WLED5_SINK_REG_MOD_SRC_SEL_HIGH 0
0122 #define WLED5_SINK_REG_MOD_SRC_SEL_EXT 0x03
0123 #define WLED5_SINK_REG_MOD_SRC_SEL_MASK GENMASK(1, 0)
0124
0125 #define WLED5_SINK_REG_MOD_A_BRIGHTNESS_WIDTH_SEL 0x52
0126 #define WLED5_SINK_REG_MOD_B_BRIGHTNESS_WIDTH_SEL 0x62
0127 #define WLED5_SINK_REG_BRIGHTNESS_WIDTH_12B 0
0128 #define WLED5_SINK_REG_BRIGHTNESS_WIDTH_15B 1
0129
0130 #define WLED5_SINK_REG_MOD_A_BRIGHTNESS_LSB 0x53
0131 #define WLED5_SINK_REG_MOD_A_BRIGHTNESS_MSB 0x54
0132 #define WLED5_SINK_REG_MOD_B_BRIGHTNESS_LSB 0x63
0133 #define WLED5_SINK_REG_MOD_B_BRIGHTNESS_MSB 0x64
0134
0135 #define WLED5_SINK_REG_MOD_SYNC_BIT 0x65
0136 #define WLED5_SINK_REG_SYNC_MOD_A_BIT BIT(0)
0137 #define WLED5_SINK_REG_SYNC_MOD_B_BIT BIT(1)
0138 #define WLED5_SINK_REG_SYNC_MASK GENMASK(1, 0)
0139
0140
0141 #define WLED5_SINK_REG_STR_FULL_SCALE_CURR(n) (0x72 + (n * 0x10))
0142
0143 #define WLED5_SINK_REG_STR_SRC_SEL(n) (0x73 + (n * 0x10))
0144 #define WLED5_SINK_REG_SRC_SEL_MOD_A 0
0145 #define WLED5_SINK_REG_SRC_SEL_MOD_B 1
0146 #define WLED5_SINK_REG_SRC_SEL_MASK GENMASK(1, 0)
0147
0148 struct wled_var_cfg {
0149 const u32 *values;
0150 u32 (*fn)(u32);
0151 int size;
0152 };
0153
0154 struct wled_u32_opts {
0155 const char *name;
0156 u32 *val_ptr;
0157 const struct wled_var_cfg *cfg;
0158 };
0159
0160 struct wled_bool_opts {
0161 const char *name;
0162 bool *val_ptr;
0163 };
0164
0165 struct wled_config {
0166 u32 boost_i_limit;
0167 u32 ovp;
0168 u32 switch_freq;
0169 u32 num_strings;
0170 u32 string_i_limit;
0171 u32 enabled_strings[WLED_MAX_STRINGS];
0172 u32 mod_sel;
0173 u32 cabc_sel;
0174 bool cs_out_en;
0175 bool ext_gen;
0176 bool cabc;
0177 bool external_pfet;
0178 bool auto_detection_enabled;
0179 };
0180
0181 struct wled {
0182 const char *name;
0183 struct device *dev;
0184 struct regmap *regmap;
0185 struct mutex lock;
0186 ktime_t last_short_event;
0187 ktime_t start_ovp_fault_time;
0188 u16 ctrl_addr;
0189 u16 sink_addr;
0190 u16 max_string_count;
0191 u16 auto_detection_ovp_count;
0192 u32 brightness;
0193 u32 max_brightness;
0194 u32 short_count;
0195 u32 auto_detect_count;
0196 u32 version;
0197 bool disabled_by_short;
0198 bool has_short_detect;
0199 bool cabc_disabled;
0200 int short_irq;
0201 int ovp_irq;
0202
0203 struct wled_config cfg;
0204 struct delayed_work ovp_work;
0205
0206
0207 int (*wled_set_brightness)(struct wled *wled, u16 brightness);
0208
0209
0210 int (*wled_cabc_config)(struct wled *wled, bool enable);
0211
0212
0213
0214
0215
0216 int (*wled_sync_toggle)(struct wled *wled);
0217
0218
0219
0220
0221
0222 int (*wled_ovp_delay)(struct wled *wled);
0223
0224
0225
0226
0227
0228 bool (*wled_auto_detection_required)(struct wled *wled);
0229 };
0230
0231 static int wled3_set_brightness(struct wled *wled, u16 brightness)
0232 {
0233 int rc, i;
0234 __le16 v;
0235
0236 v = cpu_to_le16(brightness & WLED3_SINK_REG_BRIGHT_MAX);
0237
0238 for (i = 0; i < wled->cfg.num_strings; ++i) {
0239 rc = regmap_bulk_write(wled->regmap, wled->ctrl_addr +
0240 WLED3_SINK_REG_BRIGHT(wled->cfg.enabled_strings[i]),
0241 &v, sizeof(v));
0242 if (rc < 0)
0243 return rc;
0244 }
0245
0246 return 0;
0247 }
0248
0249 static int wled4_set_brightness(struct wled *wled, u16 brightness)
0250 {
0251 int rc, i;
0252 u16 low_limit = wled->max_brightness * 4 / 1000;
0253 __le16 v;
0254
0255
0256 if (brightness > 0 && brightness < low_limit)
0257 brightness = low_limit;
0258
0259 v = cpu_to_le16(brightness & WLED3_SINK_REG_BRIGHT_MAX);
0260
0261 for (i = 0; i < wled->cfg.num_strings; ++i) {
0262 rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
0263 WLED4_SINK_REG_BRIGHT(wled->cfg.enabled_strings[i]),
0264 &v, sizeof(v));
0265 if (rc < 0)
0266 return rc;
0267 }
0268
0269 return 0;
0270 }
0271
0272 static int wled5_set_brightness(struct wled *wled, u16 brightness)
0273 {
0274 int rc, offset;
0275 u16 low_limit = wled->max_brightness * 1 / 1000;
0276 __le16 v;
0277
0278
0279 if (brightness < low_limit)
0280 brightness = low_limit;
0281
0282 v = cpu_to_le16(brightness & WLED5_SINK_REG_BRIGHT_MAX_15B);
0283
0284 offset = (wled->cfg.mod_sel == MOD_A) ?
0285 WLED5_SINK_REG_MOD_A_BRIGHTNESS_LSB :
0286 WLED5_SINK_REG_MOD_B_BRIGHTNESS_LSB;
0287
0288 rc = regmap_bulk_write(wled->regmap, wled->sink_addr + offset,
0289 &v, sizeof(v));
0290 return rc;
0291 }
0292
0293 static void wled_ovp_work(struct work_struct *work)
0294 {
0295 struct wled *wled = container_of(work,
0296 struct wled, ovp_work.work);
0297 enable_irq(wled->ovp_irq);
0298 }
0299
0300 static int wled_module_enable(struct wled *wled, int val)
0301 {
0302 int rc;
0303
0304 if (wled->disabled_by_short)
0305 return -ENXIO;
0306
0307 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
0308 WLED3_CTRL_REG_MOD_EN,
0309 WLED3_CTRL_REG_MOD_EN_MASK,
0310 val << WLED3_CTRL_REG_MOD_EN_SHIFT);
0311 if (rc < 0)
0312 return rc;
0313
0314 if (wled->ovp_irq > 0) {
0315 if (val) {
0316
0317
0318
0319
0320
0321
0322 schedule_delayed_work(&wled->ovp_work, HZ / 100);
0323 } else {
0324 if (!cancel_delayed_work_sync(&wled->ovp_work))
0325 disable_irq(wled->ovp_irq);
0326 }
0327 }
0328
0329 return 0;
0330 }
0331
0332 static int wled3_sync_toggle(struct wled *wled)
0333 {
0334 int rc;
0335 unsigned int mask = GENMASK(wled->max_string_count - 1, 0);
0336
0337 rc = regmap_update_bits(wled->regmap,
0338 wled->sink_addr + WLED3_SINK_REG_SYNC,
0339 mask, WLED3_SINK_REG_SYNC_CLEAR);
0340 if (rc < 0)
0341 return rc;
0342
0343 rc = regmap_update_bits(wled->regmap,
0344 wled->sink_addr + WLED3_SINK_REG_SYNC,
0345 mask, mask);
0346
0347 return rc;
0348 }
0349
0350 static int wled5_mod_sync_toggle(struct wled *wled)
0351 {
0352 int rc;
0353 u8 val;
0354
0355 rc = regmap_update_bits(wled->regmap,
0356 wled->sink_addr + WLED5_SINK_REG_MOD_SYNC_BIT,
0357 WLED5_SINK_REG_SYNC_MASK, 0);
0358 if (rc < 0)
0359 return rc;
0360
0361 val = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_SYNC_MOD_A_BIT :
0362 WLED5_SINK_REG_SYNC_MOD_B_BIT;
0363 return regmap_update_bits(wled->regmap,
0364 wled->sink_addr + WLED5_SINK_REG_MOD_SYNC_BIT,
0365 WLED5_SINK_REG_SYNC_MASK, val);
0366 }
0367
0368 static int wled_ovp_fault_status(struct wled *wled, bool *fault_set)
0369 {
0370 int rc;
0371 u32 int_rt_sts, fault_sts;
0372
0373 *fault_set = false;
0374 rc = regmap_read(wled->regmap,
0375 wled->ctrl_addr + WLED3_CTRL_REG_INT_RT_STS,
0376 &int_rt_sts);
0377 if (rc < 0) {
0378 dev_err(wled->dev, "Failed to read INT_RT_STS rc=%d\n", rc);
0379 return rc;
0380 }
0381
0382 rc = regmap_read(wled->regmap,
0383 wled->ctrl_addr + WLED3_CTRL_REG_FAULT_STATUS,
0384 &fault_sts);
0385 if (rc < 0) {
0386 dev_err(wled->dev, "Failed to read FAULT_STATUS rc=%d\n", rc);
0387 return rc;
0388 }
0389
0390 if (int_rt_sts & WLED3_CTRL_REG_OVP_FAULT_STATUS)
0391 *fault_set = true;
0392
0393 if (wled->version == 4 && (fault_sts & WLED3_CTRL_REG_OVP_FAULT_BIT))
0394 *fault_set = true;
0395
0396 if (wled->version == 5 && (fault_sts & (WLED3_CTRL_REG_OVP_FAULT_BIT |
0397 WLED5_CTRL_REG_OVP_PRE_ALARM_BIT)))
0398 *fault_set = true;
0399
0400 if (*fault_set)
0401 dev_dbg(wled->dev, "WLED OVP fault detected, int_rt_sts=0x%x fault_sts=0x%x\n",
0402 int_rt_sts, fault_sts);
0403
0404 return rc;
0405 }
0406
0407 static int wled4_ovp_delay(struct wled *wled)
0408 {
0409 return WLED_SOFT_START_DLY_US;
0410 }
0411
0412 static int wled5_ovp_delay(struct wled *wled)
0413 {
0414 int rc, delay_us;
0415 u32 val;
0416 u8 ovp_timer_ms[8] = {1, 2, 4, 8, 12, 16, 20, 24};
0417
0418
0419 rc = regmap_read(wled->regmap, wled->ctrl_addr +
0420 WLED5_CTRL_REG_OVP_INT_CTL, &val);
0421 if (rc < 0)
0422 delay_us =
0423 ovp_timer_ms[val & WLED5_CTRL_REG_OVP_INT_TIMER_MASK] * 1000;
0424 else
0425 delay_us = 2 * WLED_SOFT_START_DLY_US;
0426
0427 dev_dbg(wled->dev, "delay_time_us: %d\n", delay_us);
0428
0429 return delay_us;
0430 }
0431
0432 static int wled_update_status(struct backlight_device *bl)
0433 {
0434 struct wled *wled = bl_get_data(bl);
0435 u16 brightness = backlight_get_brightness(bl);
0436 int rc = 0;
0437
0438 mutex_lock(&wled->lock);
0439 if (brightness) {
0440 rc = wled->wled_set_brightness(wled, brightness);
0441 if (rc < 0) {
0442 dev_err(wled->dev, "wled failed to set brightness rc:%d\n",
0443 rc);
0444 goto unlock_mutex;
0445 }
0446
0447 if (wled->version < 5) {
0448 rc = wled->wled_sync_toggle(wled);
0449 if (rc < 0) {
0450 dev_err(wled->dev, "wled sync failed rc:%d\n", rc);
0451 goto unlock_mutex;
0452 }
0453 } else {
0454
0455
0456
0457
0458 rc = wled5_mod_sync_toggle(wled);
0459 if (rc < 0) {
0460 dev_err(wled->dev, "wled mod sync failed rc:%d\n",
0461 rc);
0462 goto unlock_mutex;
0463 }
0464 }
0465 }
0466
0467 if (!!brightness != !!wled->brightness) {
0468 rc = wled_module_enable(wled, !!brightness);
0469 if (rc < 0) {
0470 dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
0471 goto unlock_mutex;
0472 }
0473 }
0474
0475 wled->brightness = brightness;
0476
0477 unlock_mutex:
0478 mutex_unlock(&wled->lock);
0479
0480 return rc;
0481 }
0482
0483 static int wled4_cabc_config(struct wled *wled, bool enable)
0484 {
0485 int i, j, rc;
0486 u8 val;
0487
0488 for (i = 0; i < wled->cfg.num_strings; i++) {
0489 j = wled->cfg.enabled_strings[i];
0490
0491 val = enable ? WLED4_SINK_REG_STR_CABC_MASK : 0;
0492 rc = regmap_update_bits(wled->regmap, wled->sink_addr +
0493 WLED4_SINK_REG_STR_CABC(j),
0494 WLED4_SINK_REG_STR_CABC_MASK, val);
0495 if (rc < 0)
0496 return rc;
0497 }
0498
0499 return 0;
0500 }
0501
0502 static int wled5_cabc_config(struct wled *wled, bool enable)
0503 {
0504 int rc, offset;
0505 u8 reg;
0506
0507 if (wled->cabc_disabled)
0508 return 0;
0509
0510 reg = enable ? wled->cfg.cabc_sel : 0;
0511 offset = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_MOD_A_SRC_SEL :
0512 WLED5_SINK_REG_MOD_B_SRC_SEL;
0513
0514 rc = regmap_update_bits(wled->regmap, wled->sink_addr + offset,
0515 WLED5_SINK_REG_MOD_SRC_SEL_MASK, reg);
0516 if (rc < 0) {
0517 pr_err("Error in configuring CABC rc=%d\n", rc);
0518 return rc;
0519 }
0520
0521 if (!wled->cfg.cabc_sel)
0522 wled->cabc_disabled = true;
0523
0524 return 0;
0525 }
0526
0527 #define WLED_SHORT_DLY_MS 20
0528 #define WLED_SHORT_CNT_MAX 5
0529 #define WLED_SHORT_RESET_CNT_DLY_US USEC_PER_SEC
0530
0531 static irqreturn_t wled_short_irq_handler(int irq, void *_wled)
0532 {
0533 struct wled *wled = _wled;
0534 int rc;
0535 s64 elapsed_time;
0536
0537 wled->short_count++;
0538 mutex_lock(&wled->lock);
0539 rc = wled_module_enable(wled, false);
0540 if (rc < 0) {
0541 dev_err(wled->dev, "wled disable failed rc:%d\n", rc);
0542 goto unlock_mutex;
0543 }
0544
0545 elapsed_time = ktime_us_delta(ktime_get(),
0546 wled->last_short_event);
0547 if (elapsed_time > WLED_SHORT_RESET_CNT_DLY_US)
0548 wled->short_count = 1;
0549
0550 if (wled->short_count > WLED_SHORT_CNT_MAX) {
0551 dev_err(wled->dev, "Short triggered %d times, disabling WLED forever!\n",
0552 wled->short_count);
0553 wled->disabled_by_short = true;
0554 goto unlock_mutex;
0555 }
0556
0557 wled->last_short_event = ktime_get();
0558
0559 msleep(WLED_SHORT_DLY_MS);
0560 rc = wled_module_enable(wled, true);
0561 if (rc < 0)
0562 dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
0563
0564 unlock_mutex:
0565 mutex_unlock(&wled->lock);
0566
0567 return IRQ_HANDLED;
0568 }
0569
0570 #define AUTO_DETECT_BRIGHTNESS 200
0571
0572 static void wled_auto_string_detection(struct wled *wled)
0573 {
0574 int rc = 0, i, j, delay_time_us;
0575 u32 sink_config = 0;
0576 u8 sink_test = 0, sink_valid = 0, val;
0577 bool fault_set;
0578
0579
0580 rc = regmap_read(wled->regmap, wled->sink_addr +
0581 WLED4_SINK_REG_CURR_SINK, &sink_config);
0582 if (rc < 0) {
0583 dev_err(wled->dev, "Failed to read SINK configuration rc=%d\n",
0584 rc);
0585 goto failed_detect;
0586 }
0587
0588
0589 rc = regmap_update_bits(wled->regmap,
0590 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
0591 WLED3_CTRL_REG_MOD_EN_MASK, 0);
0592 if (rc < 0) {
0593 dev_err(wled->dev, "Failed to disable WLED module rc=%d\n", rc);
0594 goto failed_detect;
0595 }
0596
0597
0598 rc = wled4_set_brightness(wled, AUTO_DETECT_BRIGHTNESS);
0599 if (rc < 0) {
0600 dev_err(wled->dev, "Failed to set brightness for auto detection rc=%d\n",
0601 rc);
0602 goto failed_detect;
0603 }
0604
0605 if (wled->cfg.cabc) {
0606 rc = wled->wled_cabc_config(wled, false);
0607 if (rc < 0)
0608 goto failed_detect;
0609 }
0610
0611
0612 rc = regmap_write(wled->regmap,
0613 wled->sink_addr + WLED4_SINK_REG_CURR_SINK, 0);
0614 if (rc < 0) {
0615 dev_err(wled->dev, "Failed to disable all sinks rc=%d\n", rc);
0616 goto failed_detect;
0617 }
0618
0619
0620 for (i = 0; i < wled->cfg.num_strings; i++) {
0621 j = wled->cfg.enabled_strings[i];
0622 sink_test = BIT((WLED4_SINK_REG_CURR_SINK_SHFT + j));
0623
0624
0625 rc = regmap_write(wled->regmap, wled->ctrl_addr +
0626 WLED3_CTRL_REG_FEEDBACK_CONTROL, j + 1);
0627 if (rc < 0) {
0628 dev_err(wled->dev, "Failed to enable feedback for SINK %d rc = %d\n",
0629 j + 1, rc);
0630 goto failed_detect;
0631 }
0632
0633
0634 rc = regmap_write(wled->regmap, wled->sink_addr +
0635 WLED4_SINK_REG_CURR_SINK, sink_test);
0636 if (rc < 0) {
0637 dev_err(wled->dev, "Failed to configure SINK %d rc=%d\n",
0638 j + 1, rc);
0639 goto failed_detect;
0640 }
0641
0642
0643 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
0644 WLED3_CTRL_REG_MOD_EN,
0645 WLED3_CTRL_REG_MOD_EN_MASK,
0646 WLED3_CTRL_REG_MOD_EN_MASK);
0647 if (rc < 0) {
0648 dev_err(wled->dev, "Failed to enable WLED module rc=%d\n",
0649 rc);
0650 goto failed_detect;
0651 }
0652
0653 delay_time_us = wled->wled_ovp_delay(wled);
0654 usleep_range(delay_time_us, delay_time_us + 1000);
0655
0656 rc = wled_ovp_fault_status(wled, &fault_set);
0657 if (rc < 0) {
0658 dev_err(wled->dev, "Error in getting OVP fault_sts, rc=%d\n",
0659 rc);
0660 goto failed_detect;
0661 }
0662
0663 if (fault_set)
0664 dev_dbg(wled->dev, "WLED OVP fault detected with SINK %d\n",
0665 j + 1);
0666 else
0667 sink_valid |= sink_test;
0668
0669
0670 rc = regmap_update_bits(wled->regmap,
0671 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
0672 WLED3_CTRL_REG_MOD_EN_MASK, 0);
0673 if (rc < 0) {
0674 dev_err(wled->dev, "Failed to disable WLED module rc=%d\n",
0675 rc);
0676 goto failed_detect;
0677 }
0678 }
0679
0680 if (!sink_valid) {
0681 dev_err(wled->dev, "No valid WLED sinks found\n");
0682 wled->disabled_by_short = true;
0683 goto failed_detect;
0684 }
0685
0686 if (sink_valid != sink_config) {
0687 dev_warn(wled->dev, "%x is not a valid sink configuration - using %x instead\n",
0688 sink_config, sink_valid);
0689 sink_config = sink_valid;
0690 }
0691
0692
0693 rc = regmap_write(wled->regmap,
0694 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
0695 sink_config);
0696 if (rc < 0) {
0697 dev_err(wled->dev, "Failed to reconfigure the default sink rc=%d\n",
0698 rc);
0699 goto failed_detect;
0700 }
0701
0702
0703 if (wled->version == 4) {
0704 for (i = 0; i < wled->cfg.num_strings; i++) {
0705 j = wled->cfg.enabled_strings[i];
0706 if (sink_config &
0707 BIT(WLED4_SINK_REG_CURR_SINK_SHFT + j))
0708 val = WLED4_SINK_REG_STR_MOD_MASK;
0709 else
0710
0711 val = 0;
0712
0713 rc = regmap_write(wled->regmap, wled->sink_addr +
0714 WLED4_SINK_REG_STR_MOD_EN(j), val);
0715 if (rc < 0) {
0716 dev_err(wled->dev, "Failed to configure MODULATOR_EN rc=%d\n",
0717 rc);
0718 goto failed_detect;
0719 }
0720 }
0721 }
0722
0723
0724 rc = wled->wled_cabc_config(wled, true);
0725 if (rc < 0)
0726 goto failed_detect;
0727
0728
0729 rc = regmap_write(wled->regmap,
0730 wled->ctrl_addr + WLED3_CTRL_REG_FEEDBACK_CONTROL, 0);
0731 if (rc < 0) {
0732 dev_err(wled->dev, "Failed to restore feedback setting rc=%d\n",
0733 rc);
0734 goto failed_detect;
0735 }
0736
0737
0738 rc = wled4_set_brightness(wled, wled->brightness);
0739 if (rc < 0) {
0740 dev_err(wled->dev, "Failed to set brightness after auto detection rc=%d\n",
0741 rc);
0742 goto failed_detect;
0743 }
0744
0745 rc = regmap_update_bits(wled->regmap,
0746 wled->ctrl_addr + WLED3_CTRL_REG_MOD_EN,
0747 WLED3_CTRL_REG_MOD_EN_MASK,
0748 WLED3_CTRL_REG_MOD_EN_MASK);
0749 if (rc < 0) {
0750 dev_err(wled->dev, "Failed to enable WLED module rc=%d\n", rc);
0751 goto failed_detect;
0752 }
0753
0754 failed_detect:
0755 return;
0756 }
0757
0758 #define WLED_AUTO_DETECT_OVP_COUNT 5
0759 #define WLED_AUTO_DETECT_CNT_DLY_US USEC_PER_SEC
0760
0761 static bool wled4_auto_detection_required(struct wled *wled)
0762 {
0763 s64 elapsed_time_us;
0764
0765 if (!wled->cfg.auto_detection_enabled)
0766 return false;
0767
0768
0769
0770
0771
0772
0773 if (!wled->auto_detection_ovp_count) {
0774 wled->start_ovp_fault_time = ktime_get();
0775 wled->auto_detection_ovp_count++;
0776 } else {
0777 elapsed_time_us = ktime_us_delta(ktime_get(),
0778 wled->start_ovp_fault_time);
0779 if (elapsed_time_us > WLED_AUTO_DETECT_CNT_DLY_US)
0780 wled->auto_detection_ovp_count = 0;
0781 else
0782 wled->auto_detection_ovp_count++;
0783
0784 if (wled->auto_detection_ovp_count >=
0785 WLED_AUTO_DETECT_OVP_COUNT) {
0786 wled->auto_detection_ovp_count = 0;
0787 return true;
0788 }
0789 }
0790
0791 return false;
0792 }
0793
0794 static bool wled5_auto_detection_required(struct wled *wled)
0795 {
0796 if (!wled->cfg.auto_detection_enabled)
0797 return false;
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808 return true;
0809 }
0810
0811 static int wled_auto_detection_at_init(struct wled *wled)
0812 {
0813 int rc;
0814 bool fault_set;
0815
0816 if (!wled->cfg.auto_detection_enabled)
0817 return 0;
0818
0819 rc = wled_ovp_fault_status(wled, &fault_set);
0820 if (rc < 0) {
0821 dev_err(wled->dev, "Error in getting OVP fault_sts, rc=%d\n",
0822 rc);
0823 return rc;
0824 }
0825
0826 if (fault_set) {
0827 mutex_lock(&wled->lock);
0828 wled_auto_string_detection(wled);
0829 mutex_unlock(&wled->lock);
0830 }
0831
0832 return rc;
0833 }
0834
0835 static irqreturn_t wled_ovp_irq_handler(int irq, void *_wled)
0836 {
0837 struct wled *wled = _wled;
0838 int rc;
0839 u32 int_sts, fault_sts;
0840
0841 rc = regmap_read(wled->regmap,
0842 wled->ctrl_addr + WLED3_CTRL_REG_INT_RT_STS, &int_sts);
0843 if (rc < 0) {
0844 dev_err(wled->dev, "Error in reading WLED3_INT_RT_STS rc=%d\n",
0845 rc);
0846 return IRQ_HANDLED;
0847 }
0848
0849 rc = regmap_read(wled->regmap, wled->ctrl_addr +
0850 WLED3_CTRL_REG_FAULT_STATUS, &fault_sts);
0851 if (rc < 0) {
0852 dev_err(wled->dev, "Error in reading WLED_FAULT_STATUS rc=%d\n",
0853 rc);
0854 return IRQ_HANDLED;
0855 }
0856
0857 if (fault_sts & (WLED3_CTRL_REG_OVP_FAULT_BIT |
0858 WLED3_CTRL_REG_ILIM_FAULT_BIT))
0859 dev_dbg(wled->dev, "WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
0860 int_sts, fault_sts);
0861
0862 if (fault_sts & WLED3_CTRL_REG_OVP_FAULT_BIT) {
0863 if (wled->wled_auto_detection_required(wled)) {
0864 mutex_lock(&wled->lock);
0865 wled_auto_string_detection(wled);
0866 mutex_unlock(&wled->lock);
0867 }
0868 }
0869
0870 return IRQ_HANDLED;
0871 }
0872
0873 static int wled3_setup(struct wled *wled)
0874 {
0875 u16 addr;
0876 u8 sink_en = 0;
0877 int rc, i, j;
0878
0879 rc = regmap_update_bits(wled->regmap,
0880 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
0881 WLED3_CTRL_REG_OVP_MASK, wled->cfg.ovp);
0882 if (rc)
0883 return rc;
0884
0885 rc = regmap_update_bits(wled->regmap,
0886 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
0887 WLED3_CTRL_REG_ILIMIT_MASK,
0888 wled->cfg.boost_i_limit);
0889 if (rc)
0890 return rc;
0891
0892 rc = regmap_update_bits(wled->regmap,
0893 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
0894 WLED3_CTRL_REG_FREQ_MASK,
0895 wled->cfg.switch_freq);
0896 if (rc)
0897 return rc;
0898
0899 for (i = 0; i < wled->cfg.num_strings; ++i) {
0900 j = wled->cfg.enabled_strings[i];
0901 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_EN(j);
0902 rc = regmap_update_bits(wled->regmap, addr,
0903 WLED3_SINK_REG_STR_MOD_MASK,
0904 WLED3_SINK_REG_STR_MOD_MASK);
0905 if (rc)
0906 return rc;
0907
0908 if (wled->cfg.ext_gen) {
0909 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_SRC(j);
0910 rc = regmap_update_bits(wled->regmap, addr,
0911 WLED3_SINK_REG_STR_MOD_SRC_MASK,
0912 WLED3_SINK_REG_STR_MOD_SRC_EXT);
0913 if (rc)
0914 return rc;
0915 }
0916
0917 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_FULL_SCALE_CURR(j);
0918 rc = regmap_update_bits(wled->regmap, addr,
0919 WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK,
0920 wled->cfg.string_i_limit);
0921 if (rc)
0922 return rc;
0923
0924 addr = wled->ctrl_addr + WLED3_SINK_REG_STR_CABC(j);
0925 rc = regmap_update_bits(wled->regmap, addr,
0926 WLED3_SINK_REG_STR_CABC_MASK,
0927 wled->cfg.cabc ?
0928 WLED3_SINK_REG_STR_CABC_MASK : 0);
0929 if (rc)
0930 return rc;
0931
0932 sink_en |= BIT(j + WLED3_SINK_REG_CURR_SINK_SHFT);
0933 }
0934
0935 rc = regmap_update_bits(wled->regmap,
0936 wled->ctrl_addr + WLED3_SINK_REG_CURR_SINK,
0937 WLED3_SINK_REG_CURR_SINK_MASK, sink_en);
0938 if (rc)
0939 return rc;
0940
0941 return 0;
0942 }
0943
0944 static const struct wled_config wled3_config_defaults = {
0945 .boost_i_limit = 3,
0946 .string_i_limit = 20,
0947 .ovp = 2,
0948 .num_strings = 3,
0949 .switch_freq = 5,
0950 .cs_out_en = false,
0951 .ext_gen = false,
0952 .cabc = false,
0953 .enabled_strings = {0, 1, 2},
0954 };
0955
0956 static int wled4_setup(struct wled *wled)
0957 {
0958 int rc, temp, i, j;
0959 u16 addr;
0960 u8 sink_en = 0;
0961 u32 sink_cfg;
0962
0963 rc = regmap_update_bits(wled->regmap,
0964 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
0965 WLED3_CTRL_REG_OVP_MASK, wled->cfg.ovp);
0966 if (rc < 0)
0967 return rc;
0968
0969 rc = regmap_update_bits(wled->regmap,
0970 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
0971 WLED3_CTRL_REG_ILIMIT_MASK,
0972 wled->cfg.boost_i_limit);
0973 if (rc < 0)
0974 return rc;
0975
0976 rc = regmap_update_bits(wled->regmap,
0977 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
0978 WLED3_CTRL_REG_FREQ_MASK,
0979 wled->cfg.switch_freq);
0980 if (rc < 0)
0981 return rc;
0982
0983 if (wled->cfg.external_pfet) {
0984
0985 rc = regmap_write(wled->regmap, wled->ctrl_addr +
0986 WLED4_CTRL_REG_SEC_ACCESS,
0987 WLED4_CTRL_REG_SEC_UNLOCK);
0988 if (rc < 0)
0989 return rc;
0990
0991 rc = regmap_write(wled->regmap,
0992 wled->ctrl_addr + WLED4_CTRL_REG_TEST1,
0993 WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2);
0994 if (rc < 0)
0995 return rc;
0996 }
0997
0998 rc = regmap_read(wled->regmap, wled->sink_addr +
0999 WLED4_SINK_REG_CURR_SINK, &sink_cfg);
1000 if (rc < 0)
1001 return rc;
1002
1003 for (i = 0; i < wled->cfg.num_strings; i++) {
1004 j = wled->cfg.enabled_strings[i];
1005 temp = j + WLED4_SINK_REG_CURR_SINK_SHFT;
1006 sink_en |= 1 << temp;
1007 }
1008
1009 if (sink_cfg == sink_en) {
1010 rc = wled_auto_detection_at_init(wled);
1011 return rc;
1012 }
1013
1014 rc = regmap_update_bits(wled->regmap,
1015 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1016 WLED4_SINK_REG_CURR_SINK_MASK, 0);
1017 if (rc < 0)
1018 return rc;
1019
1020 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1021 WLED3_CTRL_REG_MOD_EN,
1022 WLED3_CTRL_REG_MOD_EN_MASK, 0);
1023 if (rc < 0)
1024 return rc;
1025
1026
1027 for (i = 0; i < wled->cfg.num_strings; i++) {
1028 j = wled->cfg.enabled_strings[i];
1029
1030 addr = wled->sink_addr +
1031 WLED4_SINK_REG_STR_MOD_EN(j);
1032 rc = regmap_update_bits(wled->regmap, addr,
1033 WLED4_SINK_REG_STR_MOD_MASK,
1034 WLED4_SINK_REG_STR_MOD_MASK);
1035 if (rc < 0)
1036 return rc;
1037
1038 addr = wled->sink_addr +
1039 WLED4_SINK_REG_STR_FULL_SCALE_CURR(j);
1040 rc = regmap_update_bits(wled->regmap, addr,
1041 WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK,
1042 wled->cfg.string_i_limit);
1043 if (rc < 0)
1044 return rc;
1045 }
1046
1047 rc = wled4_cabc_config(wled, wled->cfg.cabc);
1048 if (rc < 0)
1049 return rc;
1050
1051 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1052 WLED3_CTRL_REG_MOD_EN,
1053 WLED3_CTRL_REG_MOD_EN_MASK,
1054 WLED3_CTRL_REG_MOD_EN_MASK);
1055 if (rc < 0)
1056 return rc;
1057
1058 rc = regmap_update_bits(wled->regmap,
1059 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1060 WLED4_SINK_REG_CURR_SINK_MASK, sink_en);
1061 if (rc < 0)
1062 return rc;
1063
1064 rc = wled->wled_sync_toggle(wled);
1065 if (rc < 0) {
1066 dev_err(wled->dev, "Failed to toggle sync reg rc:%d\n", rc);
1067 return rc;
1068 }
1069
1070 rc = wled_auto_detection_at_init(wled);
1071
1072 return rc;
1073 }
1074
1075 static const struct wled_config wled4_config_defaults = {
1076 .boost_i_limit = 4,
1077 .string_i_limit = 10,
1078 .ovp = 1,
1079 .num_strings = 4,
1080 .switch_freq = 11,
1081 .cabc = false,
1082 .external_pfet = false,
1083 .auto_detection_enabled = false,
1084 .enabled_strings = {0, 1, 2, 3},
1085 };
1086
1087 static int wled5_setup(struct wled *wled)
1088 {
1089 int rc, temp, i, j, offset;
1090 u8 sink_en = 0;
1091 u16 addr;
1092 u32 val;
1093
1094 rc = regmap_update_bits(wled->regmap,
1095 wled->ctrl_addr + WLED3_CTRL_REG_OVP,
1096 WLED5_CTRL_REG_OVP_MASK, wled->cfg.ovp);
1097 if (rc < 0)
1098 return rc;
1099
1100 rc = regmap_update_bits(wled->regmap,
1101 wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT,
1102 WLED3_CTRL_REG_ILIMIT_MASK,
1103 wled->cfg.boost_i_limit);
1104 if (rc < 0)
1105 return rc;
1106
1107 rc = regmap_update_bits(wled->regmap,
1108 wled->ctrl_addr + WLED3_CTRL_REG_FREQ,
1109 WLED3_CTRL_REG_FREQ_MASK,
1110 wled->cfg.switch_freq);
1111 if (rc < 0)
1112 return rc;
1113
1114
1115 for (i = 0; i < wled->cfg.num_strings; ++i) {
1116 j = wled->cfg.enabled_strings[i];
1117 addr = wled->sink_addr +
1118 WLED4_SINK_REG_STR_FULL_SCALE_CURR(j);
1119 rc = regmap_update_bits(wled->regmap, addr,
1120 WLED4_SINK_REG_STR_FULL_SCALE_CURR_MASK,
1121 wled->cfg.string_i_limit);
1122 if (rc < 0)
1123 return rc;
1124
1125 addr = wled->sink_addr + WLED5_SINK_REG_STR_SRC_SEL(j);
1126 rc = regmap_update_bits(wled->regmap, addr,
1127 WLED5_SINK_REG_SRC_SEL_MASK,
1128 wled->cfg.mod_sel == MOD_A ?
1129 WLED5_SINK_REG_SRC_SEL_MOD_A :
1130 WLED5_SINK_REG_SRC_SEL_MOD_B);
1131
1132 temp = j + WLED4_SINK_REG_CURR_SINK_SHFT;
1133 sink_en |= 1 << temp;
1134 }
1135
1136 rc = wled5_cabc_config(wled, wled->cfg.cabc_sel ? true : false);
1137 if (rc < 0)
1138 return rc;
1139
1140
1141 addr = wled->sink_addr + WLED5_SINK_REG_MOD_A_EN;
1142 val = (wled->cfg.mod_sel == MOD_A) ? WLED5_SINK_REG_MOD_EN_MASK : 0;
1143 rc = regmap_update_bits(wled->regmap, addr,
1144 WLED5_SINK_REG_MOD_EN_MASK, val);
1145 if (rc < 0)
1146 return rc;
1147
1148 addr = wled->sink_addr + WLED5_SINK_REG_MOD_B_EN;
1149 val = (wled->cfg.mod_sel == MOD_B) ? WLED5_SINK_REG_MOD_EN_MASK : 0;
1150 rc = regmap_update_bits(wled->regmap, addr,
1151 WLED5_SINK_REG_MOD_EN_MASK, val);
1152 if (rc < 0)
1153 return rc;
1154
1155 offset = (wled->cfg.mod_sel == MOD_A) ?
1156 WLED5_SINK_REG_MOD_A_BRIGHTNESS_WIDTH_SEL :
1157 WLED5_SINK_REG_MOD_B_BRIGHTNESS_WIDTH_SEL;
1158
1159 addr = wled->sink_addr + offset;
1160 val = (wled->max_brightness == WLED5_SINK_REG_BRIGHT_MAX_15B) ?
1161 WLED5_SINK_REG_BRIGHTNESS_WIDTH_15B :
1162 WLED5_SINK_REG_BRIGHTNESS_WIDTH_12B;
1163 rc = regmap_write(wled->regmap, addr, val);
1164 if (rc < 0)
1165 return rc;
1166
1167 rc = regmap_update_bits(wled->regmap,
1168 wled->sink_addr + WLED4_SINK_REG_CURR_SINK,
1169 WLED4_SINK_REG_CURR_SINK_MASK, sink_en);
1170 if (rc < 0)
1171 return rc;
1172
1173
1174 rc = wled->wled_sync_toggle(wled);
1175 if (rc < 0) {
1176 pr_err("Failed to toggle sync reg rc:%d\n", rc);
1177 return rc;
1178 }
1179
1180 rc = wled_auto_detection_at_init(wled);
1181 if (rc < 0)
1182 return rc;
1183
1184 return 0;
1185 }
1186
1187 static const struct wled_config wled5_config_defaults = {
1188 .boost_i_limit = 5,
1189 .string_i_limit = 10,
1190 .ovp = 4,
1191 .num_strings = 4,
1192 .switch_freq = 11,
1193 .mod_sel = 0,
1194 .cabc_sel = 0,
1195 .cabc = false,
1196 .external_pfet = false,
1197 .auto_detection_enabled = false,
1198 .enabled_strings = {0, 1, 2, 3},
1199 };
1200
1201 static const u32 wled3_boost_i_limit_values[] = {
1202 105, 385, 525, 805, 980, 1260, 1400, 1680,
1203 };
1204
1205 static const struct wled_var_cfg wled3_boost_i_limit_cfg = {
1206 .values = wled3_boost_i_limit_values,
1207 .size = ARRAY_SIZE(wled3_boost_i_limit_values),
1208 };
1209
1210 static const u32 wled4_boost_i_limit_values[] = {
1211 105, 280, 450, 620, 970, 1150, 1300, 1500,
1212 };
1213
1214 static const struct wled_var_cfg wled4_boost_i_limit_cfg = {
1215 .values = wled4_boost_i_limit_values,
1216 .size = ARRAY_SIZE(wled4_boost_i_limit_values),
1217 };
1218
1219 static inline u32 wled5_boost_i_limit_values_fn(u32 idx)
1220 {
1221 return 525 + (idx * 175);
1222 }
1223
1224 static const struct wled_var_cfg wled5_boost_i_limit_cfg = {
1225 .fn = wled5_boost_i_limit_values_fn,
1226 .size = 8,
1227 };
1228
1229 static const u32 wled3_ovp_values[] = {
1230 35, 32, 29, 27,
1231 };
1232
1233 static const struct wled_var_cfg wled3_ovp_cfg = {
1234 .values = wled3_ovp_values,
1235 .size = ARRAY_SIZE(wled3_ovp_values),
1236 };
1237
1238 static const u32 wled4_ovp_values[] = {
1239 31100, 29600, 19600, 18100,
1240 };
1241
1242 static const struct wled_var_cfg wled4_ovp_cfg = {
1243 .values = wled4_ovp_values,
1244 .size = ARRAY_SIZE(wled4_ovp_values),
1245 };
1246
1247 static inline u32 wled5_ovp_values_fn(u32 idx)
1248 {
1249
1250
1251
1252
1253
1254 return 38500 - (idx * 1500);
1255 }
1256
1257 static const struct wled_var_cfg wled5_ovp_cfg = {
1258 .fn = wled5_ovp_values_fn,
1259 .size = 16,
1260 };
1261
1262 static u32 wled3_switch_freq_values_fn(u32 idx)
1263 {
1264 return 19200 / (2 * (1 + idx));
1265 }
1266
1267 static const struct wled_var_cfg wled3_switch_freq_cfg = {
1268 .fn = wled3_switch_freq_values_fn,
1269 .size = 16,
1270 };
1271
1272 static const struct wled_var_cfg wled3_string_i_limit_cfg = {
1273 .size = 26,
1274 };
1275
1276 static const u32 wled4_string_i_limit_values[] = {
1277 0, 2500, 5000, 7500, 10000, 12500, 15000, 17500, 20000,
1278 22500, 25000, 27500, 30000,
1279 };
1280
1281 static const struct wled_var_cfg wled4_string_i_limit_cfg = {
1282 .values = wled4_string_i_limit_values,
1283 .size = ARRAY_SIZE(wled4_string_i_limit_values),
1284 };
1285
1286 static const struct wled_var_cfg wled5_mod_sel_cfg = {
1287 .size = 2,
1288 };
1289
1290 static const struct wled_var_cfg wled5_cabc_sel_cfg = {
1291 .size = 4,
1292 };
1293
1294 static u32 wled_values(const struct wled_var_cfg *cfg, u32 idx)
1295 {
1296 if (idx >= cfg->size)
1297 return UINT_MAX;
1298 if (cfg->fn)
1299 return cfg->fn(idx);
1300 if (cfg->values)
1301 return cfg->values[idx];
1302 return idx;
1303 }
1304
1305 static int wled_configure(struct wled *wled)
1306 {
1307 struct wled_config *cfg = &wled->cfg;
1308 struct device *dev = wled->dev;
1309 const __be32 *prop_addr;
1310 u32 size, val, c;
1311 int rc, i, j, string_len;
1312
1313 const struct wled_u32_opts *u32_opts = NULL;
1314 const struct wled_u32_opts wled3_opts[] = {
1315 {
1316 .name = "qcom,current-boost-limit",
1317 .val_ptr = &cfg->boost_i_limit,
1318 .cfg = &wled3_boost_i_limit_cfg,
1319 },
1320 {
1321 .name = "qcom,current-limit",
1322 .val_ptr = &cfg->string_i_limit,
1323 .cfg = &wled3_string_i_limit_cfg,
1324 },
1325 {
1326 .name = "qcom,ovp",
1327 .val_ptr = &cfg->ovp,
1328 .cfg = &wled3_ovp_cfg,
1329 },
1330 {
1331 .name = "qcom,switching-freq",
1332 .val_ptr = &cfg->switch_freq,
1333 .cfg = &wled3_switch_freq_cfg,
1334 },
1335 };
1336
1337 const struct wled_u32_opts wled4_opts[] = {
1338 {
1339 .name = "qcom,current-boost-limit",
1340 .val_ptr = &cfg->boost_i_limit,
1341 .cfg = &wled4_boost_i_limit_cfg,
1342 },
1343 {
1344 .name = "qcom,current-limit-microamp",
1345 .val_ptr = &cfg->string_i_limit,
1346 .cfg = &wled4_string_i_limit_cfg,
1347 },
1348 {
1349 .name = "qcom,ovp-millivolt",
1350 .val_ptr = &cfg->ovp,
1351 .cfg = &wled4_ovp_cfg,
1352 },
1353 {
1354 .name = "qcom,switching-freq",
1355 .val_ptr = &cfg->switch_freq,
1356 .cfg = &wled3_switch_freq_cfg,
1357 },
1358 };
1359
1360 const struct wled_u32_opts wled5_opts[] = {
1361 {
1362 .name = "qcom,current-boost-limit",
1363 .val_ptr = &cfg->boost_i_limit,
1364 .cfg = &wled5_boost_i_limit_cfg,
1365 },
1366 {
1367 .name = "qcom,current-limit-microamp",
1368 .val_ptr = &cfg->string_i_limit,
1369 .cfg = &wled4_string_i_limit_cfg,
1370 },
1371 {
1372 .name = "qcom,ovp-millivolt",
1373 .val_ptr = &cfg->ovp,
1374 .cfg = &wled5_ovp_cfg,
1375 },
1376 {
1377 .name = "qcom,switching-freq",
1378 .val_ptr = &cfg->switch_freq,
1379 .cfg = &wled3_switch_freq_cfg,
1380 },
1381 {
1382 .name = "qcom,modulator-sel",
1383 .val_ptr = &cfg->mod_sel,
1384 .cfg = &wled5_mod_sel_cfg,
1385 },
1386 {
1387 .name = "qcom,cabc-sel",
1388 .val_ptr = &cfg->cabc_sel,
1389 .cfg = &wled5_cabc_sel_cfg,
1390 },
1391 };
1392
1393 const struct wled_bool_opts bool_opts[] = {
1394 { "qcom,cs-out", &cfg->cs_out_en, },
1395 { "qcom,ext-gen", &cfg->ext_gen, },
1396 { "qcom,cabc", &cfg->cabc, },
1397 { "qcom,external-pfet", &cfg->external_pfet, },
1398 { "qcom,auto-string-detection", &cfg->auto_detection_enabled, },
1399 };
1400
1401 prop_addr = of_get_address(dev->of_node, 0, NULL, NULL);
1402 if (!prop_addr) {
1403 dev_err(wled->dev, "invalid IO resources\n");
1404 return -EINVAL;
1405 }
1406 wled->ctrl_addr = be32_to_cpu(*prop_addr);
1407
1408 rc = of_property_read_string(dev->of_node, "label", &wled->name);
1409 if (rc)
1410 wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
1411
1412 switch (wled->version) {
1413 case 3:
1414 u32_opts = wled3_opts;
1415 size = ARRAY_SIZE(wled3_opts);
1416 *cfg = wled3_config_defaults;
1417 wled->wled_set_brightness = wled3_set_brightness;
1418 wled->wled_sync_toggle = wled3_sync_toggle;
1419 wled->max_string_count = 3;
1420 wled->sink_addr = wled->ctrl_addr;
1421 break;
1422
1423 case 4:
1424 u32_opts = wled4_opts;
1425 size = ARRAY_SIZE(wled4_opts);
1426 *cfg = wled4_config_defaults;
1427 wled->wled_set_brightness = wled4_set_brightness;
1428 wled->wled_sync_toggle = wled3_sync_toggle;
1429 wled->wled_cabc_config = wled4_cabc_config;
1430 wled->wled_ovp_delay = wled4_ovp_delay;
1431 wled->wled_auto_detection_required =
1432 wled4_auto_detection_required;
1433 wled->max_string_count = 4;
1434
1435 prop_addr = of_get_address(dev->of_node, 1, NULL, NULL);
1436 if (!prop_addr) {
1437 dev_err(wled->dev, "invalid IO resources\n");
1438 return -EINVAL;
1439 }
1440 wled->sink_addr = be32_to_cpu(*prop_addr);
1441 break;
1442
1443 case 5:
1444 u32_opts = wled5_opts;
1445 size = ARRAY_SIZE(wled5_opts);
1446 *cfg = wled5_config_defaults;
1447 wled->wled_set_brightness = wled5_set_brightness;
1448 wled->wled_sync_toggle = wled3_sync_toggle;
1449 wled->wled_cabc_config = wled5_cabc_config;
1450 wled->wled_ovp_delay = wled5_ovp_delay;
1451 wled->wled_auto_detection_required =
1452 wled5_auto_detection_required;
1453 wled->max_string_count = 4;
1454
1455 prop_addr = of_get_address(dev->of_node, 1, NULL, NULL);
1456 if (!prop_addr) {
1457 dev_err(wled->dev, "invalid IO resources\n");
1458 return -EINVAL;
1459 }
1460 wled->sink_addr = be32_to_cpu(*prop_addr);
1461 break;
1462
1463 default:
1464 dev_err(wled->dev, "Invalid WLED version\n");
1465 return -EINVAL;
1466 }
1467
1468 for (i = 0; i < size; ++i) {
1469 rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val);
1470 if (rc == -EINVAL) {
1471 continue;
1472 } else if (rc) {
1473 dev_err(dev, "error reading '%s'\n", u32_opts[i].name);
1474 return rc;
1475 }
1476
1477 c = UINT_MAX;
1478 for (j = 0; c != val; j++) {
1479 c = wled_values(u32_opts[i].cfg, j);
1480 if (c == UINT_MAX) {
1481 dev_err(dev, "invalid value for '%s'\n",
1482 u32_opts[i].name);
1483 return -EINVAL;
1484 }
1485
1486 if (c == val)
1487 break;
1488 }
1489
1490 dev_dbg(dev, "'%s' = %u\n", u32_opts[i].name, c);
1491 *u32_opts[i].val_ptr = j;
1492 }
1493
1494 for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) {
1495 if (of_property_read_bool(dev->of_node, bool_opts[i].name))
1496 *bool_opts[i].val_ptr = true;
1497 }
1498
1499 string_len = of_property_count_elems_of_size(dev->of_node,
1500 "qcom,enabled-strings",
1501 sizeof(u32));
1502 if (string_len > 0) {
1503 if (string_len > wled->max_string_count) {
1504 dev_err(dev, "Cannot have more than %d strings\n",
1505 wled->max_string_count);
1506 return -EINVAL;
1507 }
1508
1509 rc = of_property_read_u32_array(dev->of_node,
1510 "qcom,enabled-strings",
1511 wled->cfg.enabled_strings,
1512 string_len);
1513 if (rc) {
1514 dev_err(dev, "Failed to read %d elements from qcom,enabled-strings: %d\n",
1515 string_len, rc);
1516 return rc;
1517 }
1518
1519 for (i = 0; i < string_len; ++i) {
1520 if (wled->cfg.enabled_strings[i] >= wled->max_string_count) {
1521 dev_err(dev,
1522 "qcom,enabled-strings index %d at %d is out of bounds\n",
1523 wled->cfg.enabled_strings[i], i);
1524 return -EINVAL;
1525 }
1526 }
1527
1528 cfg->num_strings = string_len;
1529 }
1530
1531 rc = of_property_read_u32(dev->of_node, "qcom,num-strings", &val);
1532 if (!rc) {
1533 if (val < 1 || val > wled->max_string_count) {
1534 dev_err(dev, "qcom,num-strings must be between 1 and %d\n",
1535 wled->max_string_count);
1536 return -EINVAL;
1537 }
1538
1539 if (string_len > 0) {
1540 dev_warn(dev, "Only one of qcom,num-strings or qcom,enabled-strings"
1541 " should be set\n");
1542 if (val > string_len) {
1543 dev_err(dev, "qcom,num-strings exceeds qcom,enabled-strings\n");
1544 return -EINVAL;
1545 }
1546 }
1547
1548 cfg->num_strings = val;
1549 }
1550
1551 return 0;
1552 }
1553
1554 static int wled_configure_short_irq(struct wled *wled,
1555 struct platform_device *pdev)
1556 {
1557 int rc;
1558
1559 if (!wled->has_short_detect)
1560 return 0;
1561
1562 rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
1563 WLED4_CTRL_REG_SHORT_PROTECT,
1564 WLED4_CTRL_REG_SHORT_EN_MASK,
1565 WLED4_CTRL_REG_SHORT_EN_MASK);
1566 if (rc < 0)
1567 return rc;
1568
1569 wled->short_irq = platform_get_irq_byname(pdev, "short");
1570 if (wled->short_irq < 0) {
1571 dev_dbg(&pdev->dev, "short irq is not used\n");
1572 return 0;
1573 }
1574
1575 rc = devm_request_threaded_irq(wled->dev, wled->short_irq,
1576 NULL, wled_short_irq_handler,
1577 IRQF_ONESHOT,
1578 "wled_short_irq", wled);
1579 if (rc < 0)
1580 dev_err(wled->dev, "Unable to request short_irq (err:%d)\n",
1581 rc);
1582
1583 return rc;
1584 }
1585
1586 static int wled_configure_ovp_irq(struct wled *wled,
1587 struct platform_device *pdev)
1588 {
1589 int rc;
1590 u32 val;
1591
1592 wled->ovp_irq = platform_get_irq_byname(pdev, "ovp");
1593 if (wled->ovp_irq < 0) {
1594 dev_dbg(&pdev->dev, "OVP IRQ not found - disabling automatic string detection\n");
1595 return 0;
1596 }
1597
1598 rc = devm_request_threaded_irq(wled->dev, wled->ovp_irq, NULL,
1599 wled_ovp_irq_handler, IRQF_ONESHOT,
1600 "wled_ovp_irq", wled);
1601 if (rc < 0) {
1602 dev_err(wled->dev, "Unable to request ovp_irq (err:%d)\n",
1603 rc);
1604 wled->ovp_irq = 0;
1605 return 0;
1606 }
1607
1608 rc = regmap_read(wled->regmap, wled->ctrl_addr +
1609 WLED3_CTRL_REG_MOD_EN, &val);
1610 if (rc < 0)
1611 return rc;
1612
1613
1614 if (!(val & WLED3_CTRL_REG_MOD_EN_MASK))
1615 disable_irq(wled->ovp_irq);
1616
1617 return 0;
1618 }
1619
1620 static const struct backlight_ops wled_ops = {
1621 .update_status = wled_update_status,
1622 };
1623
1624 static int wled_probe(struct platform_device *pdev)
1625 {
1626 struct backlight_properties props;
1627 struct backlight_device *bl;
1628 struct wled *wled;
1629 struct regmap *regmap;
1630 u32 val;
1631 int rc;
1632
1633 regmap = dev_get_regmap(pdev->dev.parent, NULL);
1634 if (!regmap) {
1635 dev_err(&pdev->dev, "Unable to get regmap\n");
1636 return -EINVAL;
1637 }
1638
1639 wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL);
1640 if (!wled)
1641 return -ENOMEM;
1642
1643 wled->regmap = regmap;
1644 wled->dev = &pdev->dev;
1645
1646 wled->version = (uintptr_t)of_device_get_match_data(&pdev->dev);
1647 if (!wled->version) {
1648 dev_err(&pdev->dev, "Unknown device version\n");
1649 return -ENODEV;
1650 }
1651
1652 mutex_init(&wled->lock);
1653 rc = wled_configure(wled);
1654 if (rc)
1655 return rc;
1656
1657 val = WLED3_SINK_REG_BRIGHT_MAX;
1658 of_property_read_u32(pdev->dev.of_node, "max-brightness", &val);
1659 wled->max_brightness = val;
1660
1661 switch (wled->version) {
1662 case 3:
1663 wled->cfg.auto_detection_enabled = false;
1664 rc = wled3_setup(wled);
1665 if (rc) {
1666 dev_err(&pdev->dev, "wled3_setup failed\n");
1667 return rc;
1668 }
1669 break;
1670
1671 case 4:
1672 wled->has_short_detect = true;
1673 rc = wled4_setup(wled);
1674 if (rc) {
1675 dev_err(&pdev->dev, "wled4_setup failed\n");
1676 return rc;
1677 }
1678 break;
1679
1680 case 5:
1681 wled->has_short_detect = true;
1682 if (wled->cfg.cabc_sel)
1683 wled->max_brightness = WLED5_SINK_REG_BRIGHT_MAX_12B;
1684
1685 rc = wled5_setup(wled);
1686 if (rc) {
1687 dev_err(&pdev->dev, "wled5_setup failed\n");
1688 return rc;
1689 }
1690 break;
1691
1692 default:
1693 dev_err(wled->dev, "Invalid WLED version\n");
1694 break;
1695 }
1696
1697 INIT_DELAYED_WORK(&wled->ovp_work, wled_ovp_work);
1698
1699 rc = wled_configure_short_irq(wled, pdev);
1700 if (rc < 0)
1701 return rc;
1702
1703 rc = wled_configure_ovp_irq(wled, pdev);
1704 if (rc < 0)
1705 return rc;
1706
1707 val = WLED_DEFAULT_BRIGHTNESS;
1708 of_property_read_u32(pdev->dev.of_node, "default-brightness", &val);
1709
1710 memset(&props, 0, sizeof(struct backlight_properties));
1711 props.type = BACKLIGHT_RAW;
1712 props.brightness = val;
1713 props.max_brightness = wled->max_brightness;
1714 bl = devm_backlight_device_register(&pdev->dev, wled->name,
1715 &pdev->dev, wled,
1716 &wled_ops, &props);
1717 return PTR_ERR_OR_ZERO(bl);
1718 };
1719
1720 static int wled_remove(struct platform_device *pdev)
1721 {
1722 struct wled *wled = platform_get_drvdata(pdev);
1723
1724 mutex_destroy(&wled->lock);
1725 cancel_delayed_work_sync(&wled->ovp_work);
1726 disable_irq(wled->short_irq);
1727 disable_irq(wled->ovp_irq);
1728
1729 return 0;
1730 }
1731
1732 static const struct of_device_id wled_match_table[] = {
1733 { .compatible = "qcom,pm8941-wled", .data = (void *)3 },
1734 { .compatible = "qcom,pmi8994-wled", .data = (void *)4 },
1735 { .compatible = "qcom,pmi8998-wled", .data = (void *)4 },
1736 { .compatible = "qcom,pm660l-wled", .data = (void *)4 },
1737 { .compatible = "qcom,pm6150l-wled", .data = (void *)5 },
1738 { .compatible = "qcom,pm8150l-wled", .data = (void *)5 },
1739 {}
1740 };
1741 MODULE_DEVICE_TABLE(of, wled_match_table);
1742
1743 static struct platform_driver wled_driver = {
1744 .probe = wled_probe,
1745 .remove = wled_remove,
1746 .driver = {
1747 .name = "qcom,wled",
1748 .of_match_table = wled_match_table,
1749 },
1750 };
1751
1752 module_platform_driver(wled_driver);
1753
1754 MODULE_DESCRIPTION("Qualcomm WLED driver");
1755 MODULE_LICENSE("GPL v2");