David Collins | 8885f79 | 2017-01-26 14:36:34 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "flashv2: %s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/of_gpio.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <linux/regmap.h> |
| 26 | #include <linux/power_supply.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/regulator/consumer.h> |
| 30 | #include <linux/leds-qpnp-flash.h> |
| 31 | #include <linux/leds-qpnp-flash-v2.h> |
| 32 | #include <linux/qpnp/qpnp-revid.h> |
| 33 | #include <linux/log2.h> |
| 34 | #include "leds.h" |
| 35 | |
| 36 | #define FLASH_LED_REG_LED_STATUS1(base) (base + 0x08) |
| 37 | #define FLASH_LED_REG_LED_STATUS2(base) (base + 0x09) |
| 38 | #define FLASH_LED_REG_INT_RT_STS(base) (base + 0x10) |
| 39 | #define FLASH_LED_REG_SAFETY_TMR(base) (base + 0x40) |
| 40 | #define FLASH_LED_REG_TGR_CURRENT(base) (base + 0x43) |
| 41 | #define FLASH_LED_REG_MOD_CTRL(base) (base + 0x46) |
| 42 | #define FLASH_LED_REG_IRES(base) (base + 0x47) |
| 43 | #define FLASH_LED_REG_STROBE_CFG(base) (base + 0x48) |
| 44 | #define FLASH_LED_REG_STROBE_CTRL(base) (base + 0x49) |
| 45 | #define FLASH_LED_EN_LED_CTRL(base) (base + 0x4C) |
| 46 | #define FLASH_LED_REG_HDRM_PRGM(base) (base + 0x4D) |
| 47 | #define FLASH_LED_REG_HDRM_AUTO_MODE_CTRL(base) (base + 0x50) |
| 48 | #define FLASH_LED_REG_WARMUP_DELAY(base) (base + 0x51) |
| 49 | #define FLASH_LED_REG_ISC_DELAY(base) (base + 0x52) |
| 50 | #define FLASH_LED_REG_THERMAL_RMP_DN_RATE(base) (base + 0x55) |
| 51 | #define FLASH_LED_REG_THERMAL_THRSH1(base) (base + 0x56) |
| 52 | #define FLASH_LED_REG_THERMAL_THRSH2(base) (base + 0x57) |
| 53 | #define FLASH_LED_REG_THERMAL_THRSH3(base) (base + 0x58) |
| 54 | #define FLASH_LED_REG_THERMAL_HYSTERESIS(base) (base + 0x59) |
| 55 | #define FLASH_LED_REG_THERMAL_DEBOUNCE(base) (base + 0x5A) |
| 56 | #define FLASH_LED_REG_VPH_DROOP_THRESHOLD(base) (base + 0x61) |
| 57 | #define FLASH_LED_REG_VPH_DROOP_DEBOUNCE(base) (base + 0x62) |
| 58 | #define FLASH_LED_REG_ILED_GRT_THRSH(base) (base + 0x67) |
| 59 | #define FLASH_LED_REG_LED1N2_ICLAMP_LOW(base) (base + 0x68) |
| 60 | #define FLASH_LED_REG_LED1N2_ICLAMP_MID(base) (base + 0x69) |
| 61 | #define FLASH_LED_REG_LED3_ICLAMP_LOW(base) (base + 0x6A) |
| 62 | #define FLASH_LED_REG_LED3_ICLAMP_MID(base) (base + 0x6B) |
| 63 | #define FLASH_LED_REG_MITIGATION_SEL(base) (base + 0x6E) |
| 64 | #define FLASH_LED_REG_MITIGATION_SW(base) (base + 0x6F) |
| 65 | #define FLASH_LED_REG_LMH_LEVEL(base) (base + 0x70) |
| 66 | #define FLASH_LED_REG_CURRENT_DERATE_EN(base) (base + 0x76) |
| 67 | |
| 68 | #define FLASH_LED_HDRM_VOL_MASK GENMASK(7, 4) |
| 69 | #define FLASH_LED_CURRENT_MASK GENMASK(6, 0) |
| 70 | #define FLASH_LED_ENABLE_MASK GENMASK(2, 0) |
| 71 | #define FLASH_HW_STROBE_MASK GENMASK(2, 0) |
| 72 | #define FLASH_LED_ISC_WARMUP_DELAY_MASK GENMASK(1, 0) |
| 73 | #define FLASH_LED_CURRENT_DERATE_EN_MASK GENMASK(2, 0) |
| 74 | #define FLASH_LED_VPH_DROOP_DEBOUNCE_MASK GENMASK(1, 0) |
| 75 | #define FLASH_LED_CHGR_MITIGATION_SEL_MASK GENMASK(5, 4) |
| 76 | #define FLASH_LED_LMH_MITIGATION_SEL_MASK GENMASK(1, 0) |
| 77 | #define FLASH_LED_ILED_GRT_THRSH_MASK GENMASK(5, 0) |
| 78 | #define FLASH_LED_LMH_LEVEL_MASK GENMASK(1, 0) |
| 79 | #define FLASH_LED_VPH_DROOP_HYSTERESIS_MASK GENMASK(5, 4) |
| 80 | #define FLASH_LED_VPH_DROOP_THRESHOLD_MASK GENMASK(2, 0) |
| 81 | #define FLASH_LED_THERMAL_HYSTERESIS_MASK GENMASK(1, 0) |
| 82 | #define FLASH_LED_THERMAL_DEBOUNCE_MASK GENMASK(1, 0) |
| 83 | #define FLASH_LED_THERMAL_THRSH_MASK GENMASK(2, 0) |
| 84 | #define FLASH_LED_MOD_CTRL_MASK BIT(7) |
| 85 | #define FLASH_LED_HW_SW_STROBE_SEL_BIT BIT(2) |
| 86 | #define FLASH_LED_VPH_DROOP_FAULT_MASK BIT(4) |
| 87 | #define FLASH_LED_LMH_MITIGATION_EN_MASK BIT(0) |
| 88 | #define FLASH_LED_CHGR_MITIGATION_EN_MASK BIT(4) |
| 89 | #define THERMAL_OTST1_RAMP_CTRL_MASK BIT(7) |
| 90 | #define THERMAL_OTST1_RAMP_CTRL_SHIFT 7 |
| 91 | #define THERMAL_DERATE_SLOW_SHIFT 4 |
| 92 | #define THERMAL_DERATE_SLOW_MASK GENMASK(6, 4) |
| 93 | #define THERMAL_DERATE_FAST_MASK GENMASK(2, 0) |
| 94 | |
| 95 | #define VPH_DROOP_DEBOUNCE_US_TO_VAL(val_us) (val_us / 8) |
| 96 | #define VPH_DROOP_HYST_MV_TO_VAL(val_mv) (val_mv / 25) |
| 97 | #define VPH_DROOP_THRESH_MV_TO_VAL(val_mv) ((val_mv / 100) - 25) |
| 98 | #define VPH_DROOP_THRESH_VAL_TO_UV(val) ((val + 25) * 100000) |
| 99 | #define MITIGATION_THRSH_MA_TO_VAL(val_ma) (val_ma / 100) |
| 100 | #define CURRENT_MA_TO_REG_VAL(curr_ma, ires_ua) ((curr_ma * 1000) / ires_ua - 1) |
| 101 | #define SAFETY_TMR_TO_REG_VAL(duration_ms) ((duration_ms / 10) - 1) |
| 102 | #define THERMAL_HYST_TEMP_TO_VAL(val, divisor) (val / divisor) |
| 103 | |
| 104 | #define FLASH_LED_ISC_WARMUP_DELAY_SHIFT 6 |
| 105 | #define FLASH_LED_WARMUP_DELAY_DEFAULT 2 |
| 106 | #define FLASH_LED_ISC_DELAY_DEFAULT 3 |
| 107 | #define FLASH_LED_VPH_DROOP_DEBOUNCE_DEFAULT 2 |
| 108 | #define FLASH_LED_VPH_DROOP_HYST_SHIFT 4 |
| 109 | #define FLASH_LED_VPH_DROOP_HYST_DEFAULT 2 |
| 110 | #define FLASH_LED_VPH_DROOP_THRESH_DEFAULT 5 |
| 111 | #define FLASH_LED_DEBOUNCE_MAX 3 |
| 112 | #define FLASH_LED_HYSTERESIS_MAX 3 |
| 113 | #define FLASH_LED_VPH_DROOP_THRESH_MAX 7 |
| 114 | #define THERMAL_DERATE_SLOW_MAX 314592 |
| 115 | #define THERMAL_DERATE_FAST_MAX 512 |
| 116 | #define THERMAL_DEBOUNCE_TIME_MAX 64 |
| 117 | #define THERMAL_DERATE_HYSTERESIS_MAX 3 |
| 118 | #define FLASH_LED_THERMAL_THRSH_MIN 3 |
| 119 | #define FLASH_LED_THERMAL_THRSH_MAX 7 |
| 120 | #define FLASH_LED_THERMAL_OTST_LEVELS 3 |
| 121 | #define FLASH_LED_VLED_MAX_DEFAULT_UV 3500000 |
| 122 | #define FLASH_LED_IBATT_OCP_THRESH_DEFAULT_UA 4500000 |
| 123 | #define FLASH_LED_RPARA_DEFAULT_UOHM 0 |
| 124 | #define FLASH_LED_SAFETY_TMR_ENABLE BIT(7) |
| 125 | #define FLASH_LED_LMH_LEVEL_DEFAULT 0 |
| 126 | #define FLASH_LED_LMH_MITIGATION_ENABLE 1 |
| 127 | #define FLASH_LED_LMH_MITIGATION_DISABLE 0 |
| 128 | #define FLASH_LED_CHGR_MITIGATION_ENABLE BIT(4) |
| 129 | #define FLASH_LED_CHGR_MITIGATION_DISABLE 0 |
| 130 | #define FLASH_LED_MITIGATION_SEL_DEFAULT 2 |
| 131 | #define FLASH_LED_MITIGATION_SEL_MAX 2 |
| 132 | #define FLASH_LED_CHGR_MITIGATION_SEL_SHIFT 4 |
| 133 | #define FLASH_LED_MITIGATION_THRSH_DEFAULT 0xA |
| 134 | #define FLASH_LED_MITIGATION_THRSH_MAX 0x1F |
| 135 | #define FLASH_LED_LMH_OCV_THRESH_DEFAULT_UV 3700000 |
| 136 | #define FLASH_LED_LMH_RBATT_THRESH_DEFAULT_UOHM 400000 |
| 137 | #define FLASH_LED_IRES_BASE 3 |
| 138 | #define FLASH_LED_IRES_DIVISOR 2500 |
| 139 | #define FLASH_LED_IRES_MIN_UA 5000 |
| 140 | #define FLASH_LED_IRES_DEFAULT_UA 12500 |
| 141 | #define FLASH_LED_IRES_DEFAULT_VAL 0x00 |
| 142 | #define FLASH_LED_HDRM_VOL_SHIFT 4 |
| 143 | #define FLASH_LED_HDRM_VOL_DEFAULT_MV 0x80 |
| 144 | #define FLASH_LED_HDRM_VOL_HI_LO_WIN_DEFAULT_MV 0x04 |
| 145 | #define FLASH_LED_HDRM_VOL_BASE_MV 125 |
| 146 | #define FLASH_LED_HDRM_VOL_STEP_MV 25 |
| 147 | #define FLASH_LED_STROBE_CFG_DEFAULT 0x00 |
| 148 | #define FLASH_LED_HW_STROBE_OPTION_1 0x00 |
| 149 | #define FLASH_LED_HW_STROBE_OPTION_2 0x01 |
| 150 | #define FLASH_LED_HW_STROBE_OPTION_3 0x02 |
| 151 | #define FLASH_LED_ENABLE BIT(0) |
| 152 | #define FLASH_LED_MOD_ENABLE BIT(7) |
| 153 | #define FLASH_LED_DISABLE 0x00 |
| 154 | #define FLASH_LED_SAFETY_TMR_DISABLED 0x13 |
| 155 | #define FLASH_LED_MIN_CURRENT_MA 25 |
| 156 | #define FLASH_LED_MAX_TOTAL_CURRENT_MA 3750 |
| 157 | |
| 158 | /* notifier call chain for flash-led irqs */ |
| 159 | static ATOMIC_NOTIFIER_HEAD(irq_notifier_list); |
| 160 | |
| 161 | enum flash_led_type { |
| 162 | FLASH_LED_TYPE_FLASH, |
| 163 | FLASH_LED_TYPE_TORCH, |
| 164 | }; |
| 165 | |
| 166 | enum { |
| 167 | LED1 = 0, |
| 168 | LED2, |
| 169 | LED3, |
| 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * Configurations for each individual LED |
| 174 | */ |
| 175 | struct flash_node_data { |
| 176 | struct platform_device *pdev; |
| 177 | struct led_classdev cdev; |
| 178 | struct pinctrl *pinctrl; |
| 179 | struct pinctrl_state *gpio_state_active; |
| 180 | struct pinctrl_state *gpio_state_suspend; |
| 181 | struct pinctrl_state *hw_strobe_state_active; |
| 182 | struct pinctrl_state *hw_strobe_state_suspend; |
| 183 | int hw_strobe_gpio; |
| 184 | int ires_ua; |
| 185 | int max_current; |
| 186 | int current_ma; |
| 187 | u8 duration; |
| 188 | u8 id; |
| 189 | u8 type; |
| 190 | u8 ires; |
| 191 | u8 hdrm_val; |
| 192 | u8 current_reg_val; |
| 193 | u8 trigger; |
| 194 | bool led_on; |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | struct flash_switch_data { |
| 199 | struct platform_device *pdev; |
| 200 | struct regulator *vreg; |
| 201 | struct led_classdev cdev; |
| 202 | int led_mask; |
| 203 | bool regulator_on; |
| 204 | bool enabled; |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | * Flash LED configuration read from device tree |
| 209 | */ |
| 210 | struct flash_led_platform_data { |
| 211 | struct pmic_revid_data *pmic_rev_id; |
| 212 | int *thermal_derate_current; |
| 213 | int all_ramp_up_done_irq; |
| 214 | int all_ramp_down_done_irq; |
| 215 | int led_fault_irq; |
| 216 | int ibatt_ocp_threshold_ua; |
| 217 | int vled_max_uv; |
| 218 | int rpara_uohm; |
| 219 | int lmh_rbatt_threshold_uohm; |
| 220 | int lmh_ocv_threshold_uv; |
| 221 | int thermal_derate_slow; |
| 222 | int thermal_derate_fast; |
| 223 | int thermal_hysteresis; |
| 224 | int thermal_debounce; |
| 225 | int thermal_thrsh1; |
| 226 | int thermal_thrsh2; |
| 227 | int thermal_thrsh3; |
| 228 | u32 led1n2_iclamp_low_ma; |
| 229 | u32 led1n2_iclamp_mid_ma; |
| 230 | u32 led3_iclamp_low_ma; |
| 231 | u32 led3_iclamp_mid_ma; |
| 232 | u8 isc_delay; |
| 233 | u8 warmup_delay; |
| 234 | u8 current_derate_en_cfg; |
| 235 | u8 vph_droop_threshold; |
| 236 | u8 vph_droop_hysteresis; |
| 237 | u8 vph_droop_debounce; |
| 238 | u8 lmh_mitigation_sel; |
| 239 | u8 chgr_mitigation_sel; |
| 240 | u8 lmh_level; |
| 241 | u8 iled_thrsh_val; |
| 242 | u8 hw_strobe_option; |
| 243 | bool hdrm_auto_mode_en; |
| 244 | bool thermal_derate_en; |
| 245 | bool otst_ramp_bkup_en; |
| 246 | }; |
| 247 | |
| 248 | /* |
| 249 | * Flash LED data structure containing flash LED attributes |
| 250 | */ |
| 251 | struct qpnp_flash_led { |
| 252 | struct flash_led_platform_data *pdata; |
| 253 | struct platform_device *pdev; |
| 254 | struct regmap *regmap; |
| 255 | struct flash_node_data *fnode; |
| 256 | struct flash_switch_data *snode; |
| 257 | struct power_supply *bms_psy; |
| 258 | struct notifier_block nb; |
| 259 | spinlock_t lock; |
| 260 | int num_fnodes; |
| 261 | int num_snodes; |
| 262 | int enable; |
| 263 | u16 base; |
| 264 | bool trigger_lmh; |
| 265 | bool trigger_chgr; |
| 266 | }; |
| 267 | |
| 268 | static int thermal_derate_slow_table[] = { |
| 269 | 128, 256, 512, 1024, 2048, 4096, 8192, 314592, |
| 270 | }; |
| 271 | |
| 272 | static int thermal_derate_fast_table[] = { |
| 273 | 32, 64, 96, 128, 256, 384, 512, |
| 274 | }; |
| 275 | |
| 276 | static int otst1_threshold_table[] = { |
| 277 | 85, 79, 73, 67, 109, 103, 97, 91, |
| 278 | }; |
| 279 | |
| 280 | static int otst2_threshold_table[] = { |
| 281 | 110, 104, 98, 92, 134, 128, 122, 116, |
| 282 | }; |
| 283 | |
| 284 | static int otst3_threshold_table[] = { |
| 285 | 125, 119, 113, 107, 149, 143, 137, 131, |
| 286 | }; |
| 287 | |
| 288 | static int qpnp_flash_led_read(struct qpnp_flash_led *led, u16 addr, u8 *data) |
| 289 | { |
| 290 | int rc; |
| 291 | uint val; |
| 292 | |
| 293 | rc = regmap_read(led->regmap, addr, &val); |
| 294 | if (rc < 0) { |
| 295 | pr_err("Unable to read from 0x%04X rc = %d\n", addr, rc); |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | pr_debug("Read 0x%02X from addr 0x%04X\n", val, addr); |
| 300 | *data = (u8)val; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int qpnp_flash_led_write(struct qpnp_flash_led *led, u16 addr, u8 data) |
| 305 | { |
| 306 | int rc; |
| 307 | |
| 308 | rc = regmap_write(led->regmap, addr, data); |
| 309 | if (rc < 0) { |
| 310 | pr_err("Unable to write to 0x%04X rc = %d\n", addr, rc); |
| 311 | return rc; |
| 312 | } |
| 313 | |
| 314 | pr_debug("Wrote 0x%02X to addr 0x%04X\n", data, addr); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int |
| 319 | qpnp_flash_led_masked_read(struct qpnp_flash_led *led, u16 addr, u8 mask, |
| 320 | u8 *val) |
| 321 | { |
| 322 | int rc; |
| 323 | |
| 324 | rc = qpnp_flash_led_read(led, addr, val); |
| 325 | if (rc < 0) |
| 326 | return rc; |
| 327 | |
| 328 | *val &= mask; |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | static int |
| 333 | qpnp_flash_led_masked_write(struct qpnp_flash_led *led, u16 addr, u8 mask, |
| 334 | u8 val) |
| 335 | { |
| 336 | int rc; |
| 337 | |
| 338 | rc = regmap_update_bits(led->regmap, addr, mask, val); |
| 339 | if (rc < 0) |
| 340 | pr_err("Unable to update bits from 0x%04X, rc = %d\n", addr, |
| 341 | rc); |
| 342 | else |
| 343 | pr_debug("Wrote 0x%02X to addr 0x%04X\n", val, addr); |
| 344 | |
| 345 | return rc; |
| 346 | } |
| 347 | |
| 348 | static enum |
| 349 | led_brightness qpnp_flash_led_brightness_get(struct led_classdev *led_cdev) |
| 350 | { |
| 351 | return led_cdev->brightness; |
| 352 | } |
| 353 | |
| 354 | static int qpnp_flash_led_init_settings(struct qpnp_flash_led *led) |
| 355 | { |
| 356 | int rc, i, addr_offset; |
| 357 | u8 val = 0, mask; |
| 358 | |
| 359 | for (i = 0; i < led->num_fnodes; i++) { |
| 360 | addr_offset = led->fnode[i].id; |
| 361 | rc = qpnp_flash_led_write(led, |
| 362 | FLASH_LED_REG_HDRM_PRGM(led->base + addr_offset), |
| 363 | led->fnode[i].hdrm_val); |
| 364 | if (rc < 0) |
| 365 | return rc; |
| 366 | |
| 367 | val |= 0x1 << led->fnode[i].id; |
| 368 | } |
| 369 | |
| 370 | rc = qpnp_flash_led_write(led, |
| 371 | FLASH_LED_REG_HDRM_AUTO_MODE_CTRL(led->base), |
| 372 | val); |
| 373 | if (rc < 0) |
| 374 | return rc; |
| 375 | |
| 376 | rc = qpnp_flash_led_masked_write(led, |
| 377 | FLASH_LED_REG_ISC_DELAY(led->base), |
| 378 | FLASH_LED_ISC_WARMUP_DELAY_MASK, |
| 379 | led->pdata->isc_delay); |
| 380 | if (rc < 0) |
| 381 | return rc; |
| 382 | |
| 383 | rc = qpnp_flash_led_masked_write(led, |
| 384 | FLASH_LED_REG_WARMUP_DELAY(led->base), |
| 385 | FLASH_LED_ISC_WARMUP_DELAY_MASK, |
| 386 | led->pdata->warmup_delay); |
| 387 | if (rc < 0) |
| 388 | return rc; |
| 389 | |
| 390 | rc = qpnp_flash_led_masked_write(led, |
| 391 | FLASH_LED_REG_CURRENT_DERATE_EN(led->base), |
| 392 | FLASH_LED_CURRENT_DERATE_EN_MASK, |
| 393 | led->pdata->current_derate_en_cfg); |
| 394 | if (rc < 0) |
| 395 | return rc; |
| 396 | |
| 397 | val = (led->pdata->otst_ramp_bkup_en << THERMAL_OTST1_RAMP_CTRL_SHIFT); |
| 398 | mask = THERMAL_OTST1_RAMP_CTRL_MASK; |
| 399 | if (led->pdata->thermal_derate_slow >= 0) { |
| 400 | val |= (led->pdata->thermal_derate_slow << |
| 401 | THERMAL_DERATE_SLOW_SHIFT); |
| 402 | mask |= THERMAL_DERATE_SLOW_MASK; |
| 403 | } |
| 404 | |
| 405 | if (led->pdata->thermal_derate_fast >= 0) { |
| 406 | val |= led->pdata->thermal_derate_fast; |
| 407 | mask |= THERMAL_DERATE_FAST_MASK; |
| 408 | } |
| 409 | |
| 410 | rc = qpnp_flash_led_masked_write(led, |
| 411 | FLASH_LED_REG_THERMAL_RMP_DN_RATE(led->base), |
| 412 | mask, val); |
| 413 | if (rc < 0) |
| 414 | return rc; |
| 415 | |
| 416 | if (led->pdata->thermal_debounce >= 0) { |
| 417 | rc = qpnp_flash_led_masked_write(led, |
| 418 | FLASH_LED_REG_THERMAL_DEBOUNCE(led->base), |
| 419 | FLASH_LED_THERMAL_DEBOUNCE_MASK, |
| 420 | led->pdata->thermal_debounce); |
| 421 | if (rc < 0) |
| 422 | return rc; |
| 423 | } |
| 424 | |
| 425 | if (led->pdata->thermal_hysteresis >= 0) { |
| 426 | rc = qpnp_flash_led_masked_write(led, |
| 427 | FLASH_LED_REG_THERMAL_HYSTERESIS(led->base), |
| 428 | FLASH_LED_THERMAL_HYSTERESIS_MASK, |
| 429 | led->pdata->thermal_hysteresis); |
| 430 | if (rc < 0) |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | if (led->pdata->thermal_thrsh1 >= 0) { |
| 435 | rc = qpnp_flash_led_masked_write(led, |
| 436 | FLASH_LED_REG_THERMAL_THRSH1(led->base), |
| 437 | FLASH_LED_THERMAL_THRSH_MASK, |
| 438 | led->pdata->thermal_thrsh1); |
| 439 | if (rc < 0) |
| 440 | return rc; |
| 441 | } |
| 442 | |
| 443 | if (led->pdata->thermal_thrsh2 >= 0) { |
| 444 | rc = qpnp_flash_led_masked_write(led, |
| 445 | FLASH_LED_REG_THERMAL_THRSH2(led->base), |
| 446 | FLASH_LED_THERMAL_THRSH_MASK, |
| 447 | led->pdata->thermal_thrsh2); |
| 448 | if (rc < 0) |
| 449 | return rc; |
| 450 | } |
| 451 | |
| 452 | if (led->pdata->thermal_thrsh3 >= 0) { |
| 453 | rc = qpnp_flash_led_masked_write(led, |
| 454 | FLASH_LED_REG_THERMAL_THRSH3(led->base), |
| 455 | FLASH_LED_THERMAL_THRSH_MASK, |
| 456 | led->pdata->thermal_thrsh3); |
| 457 | if (rc < 0) |
| 458 | return rc; |
| 459 | } |
| 460 | |
| 461 | rc = qpnp_flash_led_masked_write(led, |
| 462 | FLASH_LED_REG_VPH_DROOP_DEBOUNCE(led->base), |
| 463 | FLASH_LED_VPH_DROOP_DEBOUNCE_MASK, |
| 464 | led->pdata->vph_droop_debounce); |
| 465 | if (rc < 0) |
| 466 | return rc; |
| 467 | |
| 468 | rc = qpnp_flash_led_masked_write(led, |
| 469 | FLASH_LED_REG_VPH_DROOP_THRESHOLD(led->base), |
| 470 | FLASH_LED_VPH_DROOP_THRESHOLD_MASK, |
| 471 | led->pdata->vph_droop_threshold); |
| 472 | if (rc < 0) |
| 473 | return rc; |
| 474 | |
| 475 | rc = qpnp_flash_led_masked_write(led, |
| 476 | FLASH_LED_REG_VPH_DROOP_THRESHOLD(led->base), |
| 477 | FLASH_LED_VPH_DROOP_HYSTERESIS_MASK, |
| 478 | led->pdata->vph_droop_hysteresis); |
| 479 | if (rc < 0) |
| 480 | return rc; |
| 481 | |
| 482 | rc = qpnp_flash_led_masked_write(led, |
| 483 | FLASH_LED_REG_MITIGATION_SEL(led->base), |
| 484 | FLASH_LED_LMH_MITIGATION_SEL_MASK, |
| 485 | led->pdata->lmh_mitigation_sel); |
| 486 | if (rc < 0) |
| 487 | return rc; |
| 488 | |
| 489 | rc = qpnp_flash_led_masked_write(led, |
| 490 | FLASH_LED_REG_MITIGATION_SEL(led->base), |
| 491 | FLASH_LED_CHGR_MITIGATION_SEL_MASK, |
| 492 | led->pdata->chgr_mitigation_sel); |
| 493 | if (rc < 0) |
| 494 | return rc; |
| 495 | |
| 496 | rc = qpnp_flash_led_masked_write(led, |
| 497 | FLASH_LED_REG_LMH_LEVEL(led->base), |
| 498 | FLASH_LED_LMH_LEVEL_MASK, |
| 499 | led->pdata->lmh_level); |
| 500 | if (rc < 0) |
| 501 | return rc; |
| 502 | |
| 503 | rc = qpnp_flash_led_masked_write(led, |
| 504 | FLASH_LED_REG_ILED_GRT_THRSH(led->base), |
| 505 | FLASH_LED_ILED_GRT_THRSH_MASK, |
| 506 | led->pdata->iled_thrsh_val); |
| 507 | if (rc < 0) |
| 508 | return rc; |
| 509 | |
| 510 | if (led->pdata->led1n2_iclamp_low_ma) { |
| 511 | val = CURRENT_MA_TO_REG_VAL(led->pdata->led1n2_iclamp_low_ma, |
| 512 | led->fnode[0].ires_ua); |
| 513 | rc = qpnp_flash_led_masked_write(led, |
| 514 | FLASH_LED_REG_LED1N2_ICLAMP_LOW(led->base), |
| 515 | FLASH_LED_CURRENT_MASK, val); |
| 516 | if (rc < 0) |
| 517 | return rc; |
| 518 | } |
| 519 | |
| 520 | if (led->pdata->led1n2_iclamp_mid_ma) { |
| 521 | val = CURRENT_MA_TO_REG_VAL(led->pdata->led1n2_iclamp_mid_ma, |
| 522 | led->fnode[0].ires_ua); |
| 523 | rc = qpnp_flash_led_masked_write(led, |
| 524 | FLASH_LED_REG_LED1N2_ICLAMP_MID(led->base), |
| 525 | FLASH_LED_CURRENT_MASK, val); |
| 526 | if (rc < 0) |
| 527 | return rc; |
| 528 | } |
| 529 | |
| 530 | if (led->pdata->led3_iclamp_low_ma) { |
| 531 | val = CURRENT_MA_TO_REG_VAL(led->pdata->led3_iclamp_low_ma, |
| 532 | led->fnode[3].ires_ua); |
| 533 | rc = qpnp_flash_led_masked_write(led, |
| 534 | FLASH_LED_REG_LED3_ICLAMP_LOW(led->base), |
| 535 | FLASH_LED_CURRENT_MASK, val); |
| 536 | if (rc < 0) |
| 537 | return rc; |
| 538 | } |
| 539 | |
| 540 | if (led->pdata->led3_iclamp_mid_ma) { |
| 541 | val = CURRENT_MA_TO_REG_VAL(led->pdata->led3_iclamp_mid_ma, |
| 542 | led->fnode[3].ires_ua); |
| 543 | rc = qpnp_flash_led_masked_write(led, |
| 544 | FLASH_LED_REG_LED3_ICLAMP_MID(led->base), |
| 545 | FLASH_LED_CURRENT_MASK, val); |
| 546 | if (rc < 0) |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int qpnp_flash_led_hw_strobe_enable(struct flash_node_data *fnode, |
| 554 | int hw_strobe_option, bool on) |
| 555 | { |
| 556 | int rc = 0; |
| 557 | |
| 558 | /* |
| 559 | * If the LED controlled by this fnode is not GPIO controlled |
| 560 | * for the given strobe_option, return. |
| 561 | */ |
| 562 | if (hw_strobe_option == FLASH_LED_HW_STROBE_OPTION_1) |
| 563 | return 0; |
| 564 | else if (hw_strobe_option == FLASH_LED_HW_STROBE_OPTION_2 |
| 565 | && fnode->id != LED3) |
| 566 | return 0; |
| 567 | else if (hw_strobe_option == FLASH_LED_HW_STROBE_OPTION_3 |
| 568 | && fnode->id == LED1) |
| 569 | return 0; |
| 570 | |
| 571 | if (gpio_is_valid(fnode->hw_strobe_gpio)) { |
| 572 | gpio_set_value(fnode->hw_strobe_gpio, on ? 1 : 0); |
| 573 | } else if (fnode->hw_strobe_state_active && |
| 574 | fnode->hw_strobe_state_suspend) { |
| 575 | rc = pinctrl_select_state(fnode->pinctrl, |
| 576 | on ? fnode->hw_strobe_state_active : |
| 577 | fnode->hw_strobe_state_suspend); |
| 578 | if (rc < 0) { |
| 579 | pr_err("failed to change hw strobe pin state\n"); |
| 580 | return rc; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return rc; |
| 585 | } |
| 586 | |
| 587 | static int qpnp_flash_led_regulator_enable(struct qpnp_flash_led *led, |
| 588 | struct flash_switch_data *snode, bool on) |
| 589 | { |
| 590 | int rc = 0; |
| 591 | |
| 592 | if (!snode || !snode->vreg) |
| 593 | return 0; |
| 594 | |
| 595 | if (snode->regulator_on == on) |
| 596 | return 0; |
| 597 | |
| 598 | if (on) |
| 599 | rc = regulator_enable(snode->vreg); |
| 600 | else |
| 601 | rc = regulator_disable(snode->vreg); |
| 602 | |
| 603 | if (rc < 0) { |
| 604 | pr_err("regulator_%s failed, rc=%d\n", |
| 605 | on ? "enable" : "disable", rc); |
| 606 | return rc; |
| 607 | } |
| 608 | |
| 609 | snode->regulator_on = on ? true : false; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int get_property_from_fg(struct qpnp_flash_led *led, |
| 614 | enum power_supply_property prop, int *val) |
| 615 | { |
| 616 | int rc; |
| 617 | union power_supply_propval pval = {0, }; |
| 618 | |
| 619 | if (!led->bms_psy) { |
| 620 | pr_err("no bms psy found\n"); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | rc = power_supply_get_property(led->bms_psy, prop, &pval); |
| 625 | if (rc) { |
| 626 | pr_err("bms psy doesn't support reading prop %d rc = %d\n", |
| 627 | prop, rc); |
| 628 | return rc; |
| 629 | } |
| 630 | |
| 631 | *val = pval.intval; |
| 632 | return rc; |
| 633 | } |
| 634 | |
| 635 | #define VOLTAGE_HDRM_DEFAULT_MV 350 |
| 636 | static int qpnp_flash_led_get_voltage_headroom(struct qpnp_flash_led *led) |
| 637 | { |
| 638 | int i, voltage_hdrm_mv = 0, voltage_hdrm_max = 0; |
| 639 | |
| 640 | for (i = 0; i < led->num_fnodes; i++) { |
| 641 | if (led->fnode[i].led_on) { |
| 642 | if (led->fnode[i].id < 2) { |
| 643 | if (led->fnode[i].current_ma < 750) |
| 644 | voltage_hdrm_mv = 125; |
| 645 | else if (led->fnode[i].current_ma < 1000) |
| 646 | voltage_hdrm_mv = 175; |
| 647 | else if (led->fnode[i].current_ma < 1250) |
| 648 | voltage_hdrm_mv = 250; |
| 649 | else |
| 650 | voltage_hdrm_mv = 350; |
| 651 | } else { |
| 652 | if (led->fnode[i].current_ma < 375) |
| 653 | voltage_hdrm_mv = 125; |
| 654 | else if (led->fnode[i].current_ma < 500) |
| 655 | voltage_hdrm_mv = 175; |
| 656 | else if (led->fnode[i].current_ma < 625) |
| 657 | voltage_hdrm_mv = 250; |
| 658 | else |
| 659 | voltage_hdrm_mv = 350; |
| 660 | } |
| 661 | |
| 662 | voltage_hdrm_max = max(voltage_hdrm_max, |
| 663 | voltage_hdrm_mv); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (!voltage_hdrm_max) |
| 668 | return VOLTAGE_HDRM_DEFAULT_MV; |
| 669 | |
| 670 | return voltage_hdrm_max; |
| 671 | } |
| 672 | |
| 673 | #define UCONV 1000000LL |
| 674 | #define MCONV 1000LL |
| 675 | #define FLASH_VDIP_MARGIN 50000 |
| 676 | #define BOB_EFFICIENCY 900LL |
| 677 | #define VIN_FLASH_MIN_UV 3300000LL |
| 678 | static int qpnp_flash_led_calc_max_current(struct qpnp_flash_led *led) |
| 679 | { |
| 680 | int ocv_uv, rbatt_uohm, ibat_now, voltage_hdrm_mv, rc; |
| 681 | int64_t ibat_flash_ua, avail_flash_ua, avail_flash_power_fw; |
| 682 | int64_t ibat_safe_ua, vin_flash_uv, vph_flash_uv, vph_flash_vdip; |
| 683 | |
| 684 | /* RESISTANCE = esr_uohm + rslow_uohm */ |
| 685 | rc = get_property_from_fg(led, POWER_SUPPLY_PROP_RESISTANCE, |
| 686 | &rbatt_uohm); |
| 687 | if (rc < 0) { |
| 688 | pr_err("bms psy does not support resistance, rc=%d\n", rc); |
| 689 | return rc; |
| 690 | } |
| 691 | |
| 692 | /* If no battery is connected, return max possible flash current */ |
| 693 | if (!rbatt_uohm) |
| 694 | return FLASH_LED_MAX_TOTAL_CURRENT_MA; |
| 695 | |
| 696 | rc = get_property_from_fg(led, POWER_SUPPLY_PROP_VOLTAGE_OCV, &ocv_uv); |
| 697 | if (rc < 0) { |
| 698 | pr_err("bms psy does not support OCV, rc=%d\n", rc); |
| 699 | return rc; |
| 700 | } |
| 701 | |
| 702 | rc = get_property_from_fg(led, POWER_SUPPLY_PROP_CURRENT_NOW, |
| 703 | &ibat_now); |
| 704 | if (rc < 0) { |
| 705 | pr_err("bms psy does not support current, rc=%d\n", rc); |
| 706 | return rc; |
| 707 | } |
| 708 | |
| 709 | rbatt_uohm += led->pdata->rpara_uohm; |
| 710 | voltage_hdrm_mv = qpnp_flash_led_get_voltage_headroom(led); |
| 711 | vph_flash_vdip = |
| 712 | VPH_DROOP_THRESH_VAL_TO_UV(led->pdata->vph_droop_threshold) |
| 713 | + FLASH_VDIP_MARGIN; |
| 714 | |
| 715 | /* Check if LMH_MITIGATION needs to be triggered */ |
| 716 | if (!led->trigger_lmh && (ocv_uv < led->pdata->lmh_ocv_threshold_uv || |
| 717 | rbatt_uohm > led->pdata->lmh_rbatt_threshold_uohm)) { |
| 718 | led->trigger_lmh = true; |
| 719 | rc = qpnp_flash_led_masked_write(led, |
| 720 | FLASH_LED_REG_MITIGATION_SW(led->base), |
| 721 | FLASH_LED_LMH_MITIGATION_EN_MASK, |
| 722 | FLASH_LED_LMH_MITIGATION_ENABLE); |
| 723 | if (rc < 0) { |
| 724 | pr_err("trigger lmh mitigation failed, rc=%d\n", rc); |
| 725 | return rc; |
| 726 | } |
| 727 | |
| 728 | /* Wait for LMH mitigation to take effect */ |
| 729 | udelay(100); |
| 730 | |
| 731 | return qpnp_flash_led_calc_max_current(led); |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Calculate the maximum current that can pulled out of the battery |
| 736 | * before the battery voltage dips below a safe threshold. |
| 737 | */ |
| 738 | ibat_safe_ua = div_s64((ocv_uv - vph_flash_vdip) * UCONV, |
| 739 | rbatt_uohm); |
| 740 | |
| 741 | if (ibat_safe_ua <= led->pdata->ibatt_ocp_threshold_ua) { |
| 742 | /* |
| 743 | * If the calculated current is below the OCP threshold, then |
| 744 | * use it as the possible flash current. |
| 745 | */ |
| 746 | ibat_flash_ua = ibat_safe_ua - ibat_now; |
| 747 | vph_flash_uv = vph_flash_vdip; |
| 748 | } else { |
| 749 | /* |
| 750 | * If the calculated current is above the OCP threshold, then |
| 751 | * use the ocp threshold instead. |
| 752 | * |
| 753 | * Any higher current will be tripping the battery OCP. |
| 754 | */ |
| 755 | ibat_flash_ua = led->pdata->ibatt_ocp_threshold_ua - ibat_now; |
| 756 | vph_flash_uv = ocv_uv - div64_s64((int64_t)rbatt_uohm |
| 757 | * led->pdata->ibatt_ocp_threshold_ua, UCONV); |
| 758 | } |
| 759 | /* Calculate the input voltage of the flash module. */ |
| 760 | vin_flash_uv = max((led->pdata->vled_max_uv + |
| 761 | (voltage_hdrm_mv * MCONV)), VIN_FLASH_MIN_UV); |
| 762 | /* Calculate the available power for the flash module. */ |
| 763 | avail_flash_power_fw = BOB_EFFICIENCY * vph_flash_uv * ibat_flash_ua; |
| 764 | /* |
| 765 | * Calculate the available amount of current the flash module can draw |
| 766 | * before collapsing the battery. (available power/ flash input voltage) |
| 767 | */ |
| 768 | avail_flash_ua = div64_s64(avail_flash_power_fw, vin_flash_uv * MCONV); |
| 769 | pr_debug("avail_iflash=%lld, ocv=%d, ibat=%d, rbatt=%d, trigger_lmh=%d\n", |
| 770 | avail_flash_ua, ocv_uv, ibat_now, rbatt_uohm, led->trigger_lmh); |
| 771 | return min(FLASH_LED_MAX_TOTAL_CURRENT_MA, |
| 772 | (int)(div64_s64(avail_flash_ua, MCONV))); |
| 773 | } |
| 774 | |
| 775 | static int qpnp_flash_led_calc_thermal_current_lim(struct qpnp_flash_led *led) |
| 776 | { |
| 777 | int thermal_current_lim = 0; |
| 778 | int rc; |
| 779 | u8 thermal_thrsh1, thermal_thrsh2, thermal_thrsh3, otst_status; |
| 780 | |
| 781 | /* Store THERMAL_THRSHx register values */ |
| 782 | rc = qpnp_flash_led_masked_read(led, |
| 783 | FLASH_LED_REG_THERMAL_THRSH1(led->base), |
| 784 | FLASH_LED_THERMAL_THRSH_MASK, |
| 785 | &thermal_thrsh1); |
| 786 | if (rc < 0) |
| 787 | return rc; |
| 788 | |
| 789 | rc = qpnp_flash_led_masked_read(led, |
| 790 | FLASH_LED_REG_THERMAL_THRSH2(led->base), |
| 791 | FLASH_LED_THERMAL_THRSH_MASK, |
| 792 | &thermal_thrsh2); |
| 793 | if (rc < 0) |
| 794 | return rc; |
| 795 | |
| 796 | rc = qpnp_flash_led_masked_read(led, |
| 797 | FLASH_LED_REG_THERMAL_THRSH3(led->base), |
| 798 | FLASH_LED_THERMAL_THRSH_MASK, |
| 799 | &thermal_thrsh3); |
| 800 | if (rc < 0) |
| 801 | return rc; |
| 802 | |
| 803 | /* Lower THERMAL_THRSHx thresholds to minimum */ |
| 804 | rc = qpnp_flash_led_masked_write(led, |
| 805 | FLASH_LED_REG_THERMAL_THRSH1(led->base), |
| 806 | FLASH_LED_THERMAL_THRSH_MASK, |
| 807 | FLASH_LED_THERMAL_THRSH_MIN); |
| 808 | if (rc < 0) |
| 809 | return rc; |
| 810 | |
| 811 | rc = qpnp_flash_led_masked_write(led, |
| 812 | FLASH_LED_REG_THERMAL_THRSH2(led->base), |
| 813 | FLASH_LED_THERMAL_THRSH_MASK, |
| 814 | FLASH_LED_THERMAL_THRSH_MIN); |
| 815 | if (rc < 0) |
| 816 | return rc; |
| 817 | |
| 818 | rc = qpnp_flash_led_masked_write(led, |
| 819 | FLASH_LED_REG_THERMAL_THRSH3(led->base), |
| 820 | FLASH_LED_THERMAL_THRSH_MASK, |
| 821 | FLASH_LED_THERMAL_THRSH_MIN); |
| 822 | if (rc < 0) |
| 823 | return rc; |
| 824 | |
| 825 | /* Check THERMAL_OTST status */ |
| 826 | rc = qpnp_flash_led_read(led, |
| 827 | FLASH_LED_REG_LED_STATUS2(led->base), |
| 828 | &otst_status); |
| 829 | if (rc < 0) |
| 830 | return rc; |
| 831 | |
| 832 | /* Look up current limit based on THERMAL_OTST status */ |
| 833 | if (otst_status) |
| 834 | thermal_current_lim = |
| 835 | led->pdata->thermal_derate_current[otst_status >> 1]; |
| 836 | |
| 837 | /* Restore THERMAL_THRESHx registers to original values */ |
| 838 | rc = qpnp_flash_led_masked_write(led, |
| 839 | FLASH_LED_REG_THERMAL_THRSH1(led->base), |
| 840 | FLASH_LED_THERMAL_THRSH_MASK, |
| 841 | thermal_thrsh1); |
| 842 | if (rc < 0) |
| 843 | return rc; |
| 844 | |
| 845 | rc = qpnp_flash_led_masked_write(led, |
| 846 | FLASH_LED_REG_THERMAL_THRSH2(led->base), |
| 847 | FLASH_LED_THERMAL_THRSH_MASK, |
| 848 | thermal_thrsh2); |
| 849 | if (rc < 0) |
| 850 | return rc; |
| 851 | |
| 852 | rc = qpnp_flash_led_masked_write(led, |
| 853 | FLASH_LED_REG_THERMAL_THRSH3(led->base), |
| 854 | FLASH_LED_THERMAL_THRSH_MASK, |
| 855 | thermal_thrsh3); |
| 856 | if (rc < 0) |
| 857 | return rc; |
| 858 | |
| 859 | return thermal_current_lim; |
| 860 | } |
| 861 | |
| 862 | static int qpnp_flash_led_get_max_avail_current(struct qpnp_flash_led *led) |
| 863 | { |
| 864 | int max_avail_current, thermal_current_lim = 0; |
| 865 | |
| 866 | led->trigger_lmh = false; |
| 867 | max_avail_current = qpnp_flash_led_calc_max_current(led); |
| 868 | if (led->pdata->thermal_derate_en) |
| 869 | thermal_current_lim = |
| 870 | qpnp_flash_led_calc_thermal_current_lim(led); |
| 871 | |
| 872 | if (thermal_current_lim) |
| 873 | max_avail_current = min(max_avail_current, thermal_current_lim); |
| 874 | |
| 875 | return max_avail_current; |
| 876 | } |
| 877 | |
| 878 | static void qpnp_flash_led_node_set(struct flash_node_data *fnode, int value) |
| 879 | { |
| 880 | int prgm_current_ma = value; |
| 881 | |
| 882 | if (value <= 0) |
| 883 | prgm_current_ma = 0; |
| 884 | else if (value < FLASH_LED_MIN_CURRENT_MA) |
| 885 | prgm_current_ma = FLASH_LED_MIN_CURRENT_MA; |
| 886 | |
| 887 | prgm_current_ma = min(prgm_current_ma, fnode->max_current); |
| 888 | fnode->current_ma = prgm_current_ma; |
| 889 | fnode->cdev.brightness = prgm_current_ma; |
| 890 | fnode->current_reg_val = CURRENT_MA_TO_REG_VAL(prgm_current_ma, |
| 891 | fnode->ires_ua); |
| 892 | fnode->led_on = prgm_current_ma != 0; |
| 893 | } |
| 894 | |
| 895 | static int qpnp_flash_led_switch_disable(struct flash_switch_data *snode) |
| 896 | { |
| 897 | struct qpnp_flash_led *led = dev_get_drvdata(&snode->pdev->dev); |
| 898 | int i, rc, addr_offset; |
| 899 | |
| 900 | rc = qpnp_flash_led_masked_write(led, |
| 901 | FLASH_LED_EN_LED_CTRL(led->base), |
| 902 | snode->led_mask, FLASH_LED_DISABLE); |
| 903 | if (rc < 0) |
| 904 | return rc; |
| 905 | |
| 906 | if (led->trigger_lmh) { |
| 907 | rc = qpnp_flash_led_masked_write(led, |
| 908 | FLASH_LED_REG_MITIGATION_SW(led->base), |
| 909 | FLASH_LED_LMH_MITIGATION_EN_MASK, |
| 910 | FLASH_LED_LMH_MITIGATION_DISABLE); |
| 911 | if (rc < 0) { |
| 912 | pr_err("disable lmh mitigation failed, rc=%d\n", rc); |
| 913 | return rc; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | if (!led->trigger_chgr) { |
| 918 | rc = qpnp_flash_led_masked_write(led, |
| 919 | FLASH_LED_REG_MITIGATION_SW(led->base), |
| 920 | FLASH_LED_CHGR_MITIGATION_EN_MASK, |
| 921 | FLASH_LED_CHGR_MITIGATION_DISABLE); |
| 922 | if (rc < 0) { |
| 923 | pr_err("disable chgr mitigation failed, rc=%d\n", rc); |
| 924 | return rc; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | led->enable--; |
| 929 | if (led->enable == 0) { |
| 930 | rc = qpnp_flash_led_masked_write(led, |
| 931 | FLASH_LED_REG_MOD_CTRL(led->base), |
| 932 | FLASH_LED_MOD_CTRL_MASK, FLASH_LED_DISABLE); |
| 933 | if (rc < 0) |
| 934 | return rc; |
| 935 | } |
| 936 | |
| 937 | for (i = 0; i < led->num_fnodes; i++) { |
| 938 | if (!led->fnode[i].led_on || |
| 939 | !(snode->led_mask & BIT(led->fnode[i].id))) |
| 940 | continue; |
| 941 | |
| 942 | addr_offset = led->fnode[i].id; |
| 943 | rc = qpnp_flash_led_masked_write(led, |
| 944 | FLASH_LED_REG_TGR_CURRENT(led->base + addr_offset), |
| 945 | FLASH_LED_CURRENT_MASK, 0); |
| 946 | if (rc < 0) |
| 947 | return rc; |
| 948 | |
| 949 | led->fnode[i].led_on = false; |
| 950 | |
| 951 | if (led->fnode[i].pinctrl) { |
| 952 | rc = pinctrl_select_state(led->fnode[i].pinctrl, |
| 953 | led->fnode[i].gpio_state_suspend); |
| 954 | if (rc < 0) { |
| 955 | pr_err("failed to disable GPIO, rc=%d\n", rc); |
| 956 | return rc; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (led->fnode[i].trigger & FLASH_LED_HW_SW_STROBE_SEL_BIT) { |
| 961 | rc = qpnp_flash_led_hw_strobe_enable(&led->fnode[i], |
| 962 | led->pdata->hw_strobe_option, false); |
| 963 | if (rc < 0) { |
| 964 | pr_err("Unable to disable hw strobe, rc=%d\n", |
| 965 | rc); |
| 966 | return rc; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | snode->enabled = false; |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static int qpnp_flash_led_switch_set(struct flash_switch_data *snode, bool on) |
| 976 | { |
| 977 | struct qpnp_flash_led *led = dev_get_drvdata(&snode->pdev->dev); |
| 978 | int rc, i, addr_offset; |
| 979 | u8 val, mask; |
| 980 | |
| 981 | if (snode->enabled == on) { |
| 982 | pr_debug("Switch node is already %s!\n", |
| 983 | on ? "enabled" : "disabled"); |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | if (!on) { |
| 988 | rc = qpnp_flash_led_switch_disable(snode); |
| 989 | return rc; |
| 990 | } |
| 991 | |
| 992 | /* Iterate over all leds for this switch node */ |
| 993 | val = 0; |
| 994 | for (i = 0; i < led->num_fnodes; i++) |
| 995 | if (snode->led_mask & BIT(led->fnode[i].id)) |
| 996 | val |= led->fnode[i].ires << (led->fnode[i].id * 2); |
| 997 | |
| 998 | rc = qpnp_flash_led_masked_write(led, FLASH_LED_REG_IRES(led->base), |
| 999 | FLASH_LED_CURRENT_MASK, val); |
| 1000 | if (rc < 0) |
| 1001 | return rc; |
| 1002 | |
| 1003 | rc = qpnp_flash_led_masked_write(led, |
| 1004 | FLASH_LED_REG_STROBE_CFG(led->base), |
| 1005 | FLASH_LED_ENABLE_MASK, |
| 1006 | led->pdata->hw_strobe_option); |
| 1007 | if (rc < 0) |
| 1008 | return rc; |
| 1009 | |
| 1010 | val = 0; |
| 1011 | for (i = 0; i < led->num_fnodes; i++) { |
| 1012 | if (!led->fnode[i].led_on || |
| 1013 | !(snode->led_mask & BIT(led->fnode[i].id))) |
| 1014 | continue; |
| 1015 | |
| 1016 | addr_offset = led->fnode[i].id; |
| 1017 | if (led->fnode[i].trigger & FLASH_LED_HW_SW_STROBE_SEL_BIT) |
| 1018 | mask = FLASH_HW_STROBE_MASK; |
| 1019 | else |
| 1020 | mask = FLASH_LED_HW_SW_STROBE_SEL_BIT; |
| 1021 | rc = qpnp_flash_led_masked_write(led, |
| 1022 | FLASH_LED_REG_STROBE_CTRL(led->base + addr_offset), |
| 1023 | mask, led->fnode[i].trigger); |
| 1024 | if (rc < 0) |
| 1025 | return rc; |
| 1026 | |
| 1027 | rc = qpnp_flash_led_masked_write(led, |
| 1028 | FLASH_LED_REG_TGR_CURRENT(led->base + addr_offset), |
| 1029 | FLASH_LED_CURRENT_MASK, led->fnode[i].current_reg_val); |
| 1030 | if (rc < 0) |
| 1031 | return rc; |
| 1032 | |
| 1033 | rc = qpnp_flash_led_write(led, |
| 1034 | FLASH_LED_REG_SAFETY_TMR(led->base + addr_offset), |
| 1035 | led->fnode[i].duration); |
| 1036 | if (rc < 0) |
| 1037 | return rc; |
| 1038 | |
| 1039 | val |= FLASH_LED_ENABLE << led->fnode[i].id; |
| 1040 | |
| 1041 | if (led->fnode[i].pinctrl) { |
| 1042 | rc = pinctrl_select_state(led->fnode[i].pinctrl, |
| 1043 | led->fnode[i].gpio_state_active); |
| 1044 | if (rc < 0) { |
| 1045 | pr_err("failed to enable GPIO rc=%d\n", rc); |
| 1046 | return rc; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | if (led->fnode[i].trigger & FLASH_LED_HW_SW_STROBE_SEL_BIT) { |
| 1051 | rc = qpnp_flash_led_hw_strobe_enable(&led->fnode[i], |
| 1052 | led->pdata->hw_strobe_option, true); |
| 1053 | if (rc < 0) { |
| 1054 | pr_err("Unable to enable hw strobe rc=%d\n", |
| 1055 | rc); |
| 1056 | return rc; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if (led->enable == 0) { |
| 1062 | rc = qpnp_flash_led_masked_write(led, |
| 1063 | FLASH_LED_REG_MOD_CTRL(led->base), |
| 1064 | FLASH_LED_MOD_CTRL_MASK, FLASH_LED_MOD_ENABLE); |
| 1065 | if (rc < 0) |
| 1066 | return rc; |
| 1067 | } |
| 1068 | led->enable++; |
| 1069 | |
| 1070 | if (led->trigger_lmh) { |
| 1071 | rc = qpnp_flash_led_masked_write(led, |
| 1072 | FLASH_LED_REG_MITIGATION_SW(led->base), |
| 1073 | FLASH_LED_LMH_MITIGATION_EN_MASK, |
| 1074 | FLASH_LED_LMH_MITIGATION_ENABLE); |
| 1075 | if (rc < 0) { |
| 1076 | pr_err("trigger lmh mitigation failed, rc=%d\n", rc); |
| 1077 | return rc; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | if (led->trigger_chgr) { |
| 1082 | rc = qpnp_flash_led_masked_write(led, |
| 1083 | FLASH_LED_REG_MITIGATION_SW(led->base), |
| 1084 | FLASH_LED_CHGR_MITIGATION_EN_MASK, |
| 1085 | FLASH_LED_CHGR_MITIGATION_ENABLE); |
| 1086 | if (rc < 0) { |
| 1087 | pr_err("trigger chgr mitigation failed, rc=%d\n", rc); |
| 1088 | return rc; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | rc = qpnp_flash_led_masked_write(led, |
| 1093 | FLASH_LED_EN_LED_CTRL(led->base), |
| 1094 | snode->led_mask, val); |
| 1095 | if (rc < 0) |
| 1096 | return rc; |
| 1097 | |
| 1098 | snode->enabled = true; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | int qpnp_flash_led_prepare(struct led_trigger *trig, int options, |
| 1103 | int *max_current) |
| 1104 | { |
| 1105 | struct led_classdev *led_cdev; |
| 1106 | struct flash_switch_data *snode; |
| 1107 | struct qpnp_flash_led *led; |
| 1108 | int rc; |
| 1109 | |
| 1110 | if (!trig) { |
| 1111 | pr_err("Invalid led_trigger provided\n"); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
| 1115 | led_cdev = trigger_to_lcdev(trig); |
| 1116 | if (!led_cdev) { |
| 1117 | pr_err("Invalid led_cdev in trigger %s\n", trig->name); |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | |
| 1121 | snode = container_of(led_cdev, struct flash_switch_data, cdev); |
| 1122 | led = dev_get_drvdata(&snode->pdev->dev); |
| 1123 | |
| 1124 | if (!(options & FLASH_LED_PREPARE_OPTIONS_MASK)) { |
| 1125 | pr_err("Invalid options %d\n", options); |
| 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | |
| 1129 | if (options & ENABLE_REGULATOR) { |
| 1130 | rc = qpnp_flash_led_regulator_enable(led, snode, true); |
| 1131 | if (rc < 0) { |
| 1132 | pr_err("enable regulator failed, rc=%d\n", rc); |
| 1133 | return rc; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (options & DISABLE_REGULATOR) { |
| 1138 | rc = qpnp_flash_led_regulator_enable(led, snode, false); |
| 1139 | if (rc < 0) { |
| 1140 | pr_err("disable regulator failed, rc=%d\n", rc); |
| 1141 | return rc; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (options & QUERY_MAX_CURRENT) { |
| 1146 | rc = qpnp_flash_led_get_max_avail_current(led); |
| 1147 | if (rc < 0) { |
| 1148 | pr_err("query max current failed, rc=%d\n", rc); |
| 1149 | return rc; |
| 1150 | } |
| 1151 | *max_current = rc; |
| 1152 | } |
| 1153 | |
| 1154 | led->trigger_chgr = false; |
| 1155 | if (options & PRE_FLASH) |
| 1156 | led->trigger_chgr = true; |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | static void qpnp_flash_led_brightness_set(struct led_classdev *led_cdev, |
| 1162 | enum led_brightness value) |
| 1163 | { |
| 1164 | struct flash_node_data *fnode = NULL; |
| 1165 | struct flash_switch_data *snode = NULL; |
| 1166 | struct qpnp_flash_led *led = NULL; |
| 1167 | int rc; |
| 1168 | |
| 1169 | /* |
| 1170 | * strncmp() must be used here since a prefix comparison is required |
| 1171 | * in order to support names like led:switch_0 and led:flash_1. |
| 1172 | */ |
| 1173 | if (!strncmp(led_cdev->name, "led:switch", strlen("led:switch"))) { |
| 1174 | snode = container_of(led_cdev, struct flash_switch_data, cdev); |
| 1175 | led = dev_get_drvdata(&snode->pdev->dev); |
| 1176 | } else if (!strncmp(led_cdev->name, "led:flash", strlen("led:flash")) || |
| 1177 | !strncmp(led_cdev->name, "led:torch", |
| 1178 | strlen("led:torch"))) { |
| 1179 | fnode = container_of(led_cdev, struct flash_node_data, cdev); |
| 1180 | led = dev_get_drvdata(&fnode->pdev->dev); |
| 1181 | } |
| 1182 | |
| 1183 | if (!led) { |
| 1184 | pr_err("Failed to get flash driver data\n"); |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | spin_lock(&led->lock); |
| 1189 | if (snode) { |
| 1190 | rc = qpnp_flash_led_switch_set(snode, value > 0); |
| 1191 | if (rc < 0) |
| 1192 | pr_err("Failed to set flash LED switch rc=%d\n", rc); |
| 1193 | } else if (fnode) { |
| 1194 | qpnp_flash_led_node_set(fnode, value); |
| 1195 | } |
| 1196 | |
| 1197 | spin_unlock(&led->lock); |
| 1198 | } |
| 1199 | |
| 1200 | /* sysfs show function for flash_max_current */ |
| 1201 | static ssize_t qpnp_flash_led_max_current_show(struct device *dev, |
| 1202 | struct device_attribute *attr, char *buf) |
| 1203 | { |
| 1204 | int rc; |
| 1205 | struct flash_switch_data *snode; |
| 1206 | struct qpnp_flash_led *led; |
| 1207 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 1208 | |
| 1209 | snode = container_of(led_cdev, struct flash_switch_data, cdev); |
| 1210 | led = dev_get_drvdata(&snode->pdev->dev); |
| 1211 | |
| 1212 | rc = qpnp_flash_led_get_max_avail_current(led); |
| 1213 | if (rc < 0) |
| 1214 | pr_err("query max current failed, rc=%d\n", rc); |
| 1215 | |
| 1216 | return snprintf(buf, PAGE_SIZE, "%d\n", rc); |
| 1217 | } |
| 1218 | |
| 1219 | /* sysfs attributes exported by flash_led */ |
| 1220 | static struct device_attribute qpnp_flash_led_attrs[] = { |
| 1221 | __ATTR(max_current, 0664, qpnp_flash_led_max_current_show, NULL), |
| 1222 | }; |
| 1223 | |
| 1224 | static int flash_led_psy_notifier_call(struct notifier_block *nb, |
| 1225 | unsigned long ev, void *v) |
| 1226 | { |
| 1227 | struct power_supply *psy = v; |
| 1228 | struct qpnp_flash_led *led = |
| 1229 | container_of(nb, struct qpnp_flash_led, nb); |
| 1230 | |
| 1231 | if (ev != PSY_EVENT_PROP_CHANGED) |
| 1232 | return NOTIFY_OK; |
| 1233 | |
| 1234 | if (!strcmp(psy->desc->name, "bms")) { |
| 1235 | led->bms_psy = power_supply_get_by_name("bms"); |
| 1236 | if (!led->bms_psy) |
| 1237 | pr_err("Failed to get bms power_supply\n"); |
| 1238 | else |
| 1239 | power_supply_unreg_notifier(&led->nb); |
| 1240 | } |
| 1241 | |
| 1242 | return NOTIFY_OK; |
| 1243 | } |
| 1244 | |
| 1245 | static int flash_led_psy_register_notifier(struct qpnp_flash_led *led) |
| 1246 | { |
| 1247 | int rc; |
| 1248 | |
| 1249 | led->nb.notifier_call = flash_led_psy_notifier_call; |
| 1250 | rc = power_supply_reg_notifier(&led->nb); |
| 1251 | if (rc < 0) { |
| 1252 | pr_err("Couldn't register psy notifier, rc = %d\n", rc); |
| 1253 | return rc; |
| 1254 | } |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* irq handler */ |
| 1260 | static irqreturn_t qpnp_flash_led_irq_handler(int irq, void *_led) |
| 1261 | { |
| 1262 | struct qpnp_flash_led *led = _led; |
| 1263 | enum flash_led_irq_type irq_type = INVALID_IRQ; |
| 1264 | int rc; |
| 1265 | u8 irq_status, led_status1, led_status2; |
| 1266 | |
| 1267 | pr_debug("irq received, irq=%d\n", irq); |
| 1268 | |
| 1269 | rc = qpnp_flash_led_read(led, |
| 1270 | FLASH_LED_REG_INT_RT_STS(led->base), &irq_status); |
| 1271 | if (rc < 0) { |
| 1272 | pr_err("Failed to read interrupt status reg, rc=%d\n", rc); |
| 1273 | goto exit; |
| 1274 | } |
| 1275 | |
| 1276 | if (irq == led->pdata->all_ramp_up_done_irq) |
| 1277 | irq_type = ALL_RAMP_UP_DONE_IRQ; |
| 1278 | else if (irq == led->pdata->all_ramp_down_done_irq) |
| 1279 | irq_type = ALL_RAMP_DOWN_DONE_IRQ; |
| 1280 | else if (irq == led->pdata->led_fault_irq) |
| 1281 | irq_type = LED_FAULT_IRQ; |
| 1282 | |
| 1283 | if (irq_type == ALL_RAMP_UP_DONE_IRQ) |
| 1284 | atomic_notifier_call_chain(&irq_notifier_list, |
| 1285 | irq_type, NULL); |
| 1286 | |
| 1287 | if (irq_type == LED_FAULT_IRQ) { |
| 1288 | rc = qpnp_flash_led_read(led, |
| 1289 | FLASH_LED_REG_LED_STATUS1(led->base), &led_status1); |
| 1290 | if (rc < 0) { |
| 1291 | pr_err("Failed to read led_status1 reg, rc=%d\n", rc); |
| 1292 | goto exit; |
| 1293 | } |
| 1294 | |
| 1295 | rc = qpnp_flash_led_read(led, |
| 1296 | FLASH_LED_REG_LED_STATUS2(led->base), &led_status2); |
| 1297 | if (rc < 0) { |
| 1298 | pr_err("Failed to read led_status2 reg, rc=%d\n", rc); |
| 1299 | goto exit; |
| 1300 | } |
| 1301 | |
| 1302 | if (led_status1) |
| 1303 | pr_emerg("led short/open fault detected! led_status1=%x\n", |
| 1304 | led_status1); |
| 1305 | |
| 1306 | if (led_status2 & FLASH_LED_VPH_DROOP_FAULT_MASK) |
| 1307 | pr_emerg("led vph_droop fault detected!\n"); |
| 1308 | } |
| 1309 | |
| 1310 | pr_debug("irq handled, irq_type=%x, irq_status=%x\n", irq_type, |
| 1311 | irq_status); |
| 1312 | |
| 1313 | exit: |
| 1314 | return IRQ_HANDLED; |
| 1315 | } |
| 1316 | |
| 1317 | int qpnp_flash_led_register_irq_notifier(struct notifier_block *nb) |
| 1318 | { |
| 1319 | return atomic_notifier_chain_register(&irq_notifier_list, nb); |
| 1320 | } |
| 1321 | |
| 1322 | int qpnp_flash_led_unregister_irq_notifier(struct notifier_block *nb) |
| 1323 | { |
| 1324 | return atomic_notifier_chain_unregister(&irq_notifier_list, nb); |
| 1325 | } |
| 1326 | |
| 1327 | static int qpnp_flash_led_parse_each_led_dt(struct qpnp_flash_led *led, |
| 1328 | struct flash_node_data *fnode, struct device_node *node) |
| 1329 | { |
| 1330 | const char *temp_string; |
| 1331 | int rc; |
| 1332 | u32 val; |
| 1333 | bool strobe_sel = 0, edge_trigger = 0, active_high = 0; |
| 1334 | |
| 1335 | fnode->pdev = led->pdev; |
| 1336 | fnode->cdev.brightness_set = qpnp_flash_led_brightness_set; |
| 1337 | fnode->cdev.brightness_get = qpnp_flash_led_brightness_get; |
| 1338 | |
| 1339 | rc = of_property_read_string(node, "qcom,led-name", &fnode->cdev.name); |
| 1340 | if (rc < 0) { |
| 1341 | pr_err("Unable to read flash LED names\n"); |
| 1342 | return rc; |
| 1343 | } |
| 1344 | |
| 1345 | rc = of_property_read_string(node, "label", &temp_string); |
| 1346 | if (!rc) { |
| 1347 | if (!strcmp(temp_string, "flash")) { |
| 1348 | fnode->type = FLASH_LED_TYPE_FLASH; |
| 1349 | } else if (!strcmp(temp_string, "torch")) { |
| 1350 | fnode->type = FLASH_LED_TYPE_TORCH; |
| 1351 | } else { |
| 1352 | pr_err("Wrong flash LED type\n"); |
| 1353 | return rc; |
| 1354 | } |
| 1355 | } else { |
| 1356 | pr_err("Unable to read flash LED label\n"); |
| 1357 | return rc; |
| 1358 | } |
| 1359 | |
| 1360 | rc = of_property_read_u32(node, "qcom,id", &val); |
| 1361 | if (!rc) { |
| 1362 | fnode->id = (u8)val; |
| 1363 | } else { |
| 1364 | pr_err("Unable to read flash LED ID\n"); |
| 1365 | return rc; |
| 1366 | } |
| 1367 | |
| 1368 | rc = of_property_read_string(node, "qcom,default-led-trigger", |
| 1369 | &fnode->cdev.default_trigger); |
| 1370 | if (rc < 0) { |
| 1371 | pr_err("Unable to read trigger name\n"); |
| 1372 | return rc; |
| 1373 | } |
| 1374 | |
| 1375 | fnode->ires_ua = FLASH_LED_IRES_DEFAULT_UA; |
| 1376 | fnode->ires = FLASH_LED_IRES_DEFAULT_VAL; |
| 1377 | rc = of_property_read_u32(node, "qcom,ires-ua", &val); |
| 1378 | if (!rc) { |
| 1379 | fnode->ires_ua = val; |
| 1380 | fnode->ires = FLASH_LED_IRES_BASE - |
| 1381 | (val - FLASH_LED_IRES_MIN_UA) / FLASH_LED_IRES_DIVISOR; |
| 1382 | } else if (rc != -EINVAL) { |
| 1383 | pr_err("Unable to read current resolution rc=%d\n", rc); |
| 1384 | return rc; |
| 1385 | } |
| 1386 | |
| 1387 | rc = of_property_read_u32(node, "qcom,max-current", &val); |
| 1388 | if (!rc) { |
| 1389 | if (val < FLASH_LED_MIN_CURRENT_MA) |
| 1390 | val = FLASH_LED_MIN_CURRENT_MA; |
| 1391 | fnode->max_current = val; |
| 1392 | fnode->cdev.max_brightness = val; |
| 1393 | } else { |
| 1394 | pr_err("Unable to read max current, rc=%d\n", rc); |
| 1395 | return rc; |
| 1396 | } |
| 1397 | |
| 1398 | rc = of_property_read_u32(node, "qcom,current-ma", &val); |
| 1399 | if (!rc) { |
| 1400 | if (val < FLASH_LED_MIN_CURRENT_MA || |
| 1401 | val > fnode->max_current) |
| 1402 | pr_warn("Invalid operational current specified, capping it\n"); |
| 1403 | if (val < FLASH_LED_MIN_CURRENT_MA) |
| 1404 | val = FLASH_LED_MIN_CURRENT_MA; |
| 1405 | if (val > fnode->max_current) |
| 1406 | val = fnode->max_current; |
| 1407 | fnode->current_ma = val; |
| 1408 | fnode->cdev.brightness = val; |
| 1409 | } else if (rc != -EINVAL) { |
| 1410 | pr_err("Unable to read operational current, rc=%d\n", rc); |
| 1411 | return rc; |
| 1412 | } |
| 1413 | |
| 1414 | fnode->duration = FLASH_LED_SAFETY_TMR_DISABLED; |
| 1415 | rc = of_property_read_u32(node, "qcom,duration-ms", &val); |
| 1416 | if (!rc) { |
| 1417 | fnode->duration = (u8)(SAFETY_TMR_TO_REG_VAL(val) | |
| 1418 | FLASH_LED_SAFETY_TMR_ENABLE); |
| 1419 | } else if (rc == -EINVAL) { |
| 1420 | if (fnode->type == FLASH_LED_TYPE_FLASH) { |
| 1421 | pr_err("Timer duration is required for flash LED\n"); |
| 1422 | return rc; |
| 1423 | } |
| 1424 | } else { |
| 1425 | pr_err("Unable to read timer duration\n"); |
| 1426 | return rc; |
| 1427 | } |
| 1428 | |
| 1429 | fnode->hdrm_val = FLASH_LED_HDRM_VOL_DEFAULT_MV; |
| 1430 | rc = of_property_read_u32(node, "qcom,hdrm-voltage-mv", &val); |
| 1431 | if (!rc) { |
| 1432 | val = (val - FLASH_LED_HDRM_VOL_BASE_MV) / |
| 1433 | FLASH_LED_HDRM_VOL_STEP_MV; |
| 1434 | fnode->hdrm_val = (val << FLASH_LED_HDRM_VOL_SHIFT) & |
| 1435 | FLASH_LED_HDRM_VOL_MASK; |
| 1436 | } else if (rc != -EINVAL) { |
| 1437 | pr_err("Unable to read headroom voltage\n"); |
| 1438 | return rc; |
| 1439 | } |
| 1440 | |
| 1441 | rc = of_property_read_u32(node, "qcom,hdrm-vol-hi-lo-win-mv", &val); |
| 1442 | if (!rc) { |
| 1443 | fnode->hdrm_val |= (val / FLASH_LED_HDRM_VOL_STEP_MV) & |
| 1444 | ~FLASH_LED_HDRM_VOL_MASK; |
| 1445 | } else if (rc == -EINVAL) { |
| 1446 | fnode->hdrm_val |= FLASH_LED_HDRM_VOL_HI_LO_WIN_DEFAULT_MV; |
| 1447 | } else { |
| 1448 | pr_err("Unable to read hdrm hi-lo window voltage\n"); |
| 1449 | return rc; |
| 1450 | } |
| 1451 | |
| 1452 | strobe_sel = of_property_read_bool(node, "qcom,hw-strobe-sel"); |
| 1453 | if (strobe_sel) { |
| 1454 | edge_trigger = of_property_read_bool(node, |
| 1455 | "qcom,hw-strobe-edge-trigger"); |
| 1456 | active_high = !of_property_read_bool(node, |
| 1457 | "qcom,hw-strobe-active-low"); |
| 1458 | } |
| 1459 | fnode->trigger = (strobe_sel << 2) | (edge_trigger << 1) | active_high; |
| 1460 | |
| 1461 | if (fnode->trigger & FLASH_LED_HW_SW_STROBE_SEL_BIT) { |
| 1462 | if (of_find_property(node, "qcom,hw-strobe-gpio", NULL)) { |
| 1463 | fnode->hw_strobe_gpio = of_get_named_gpio(node, |
| 1464 | "qcom,hw-strobe-gpio", 0); |
| 1465 | if (fnode->hw_strobe_gpio < 0) { |
| 1466 | pr_err("Invalid gpio specified\n"); |
| 1467 | return fnode->hw_strobe_gpio; |
| 1468 | } |
| 1469 | gpio_direction_output(fnode->hw_strobe_gpio, 0); |
| 1470 | } else { |
| 1471 | fnode->hw_strobe_gpio = -1; |
| 1472 | fnode->hw_strobe_state_active = |
| 1473 | pinctrl_lookup_state(fnode->pinctrl, |
| 1474 | "strobe_enable"); |
| 1475 | if (IS_ERR_OR_NULL(fnode->hw_strobe_state_active)) { |
| 1476 | pr_err("No active pin for hardware strobe, rc=%ld\n", |
| 1477 | PTR_ERR(fnode->hw_strobe_state_active)); |
| 1478 | fnode->hw_strobe_state_active = NULL; |
| 1479 | } |
| 1480 | |
| 1481 | fnode->hw_strobe_state_suspend = |
| 1482 | pinctrl_lookup_state(fnode->pinctrl, |
| 1483 | "strobe_disable"); |
| 1484 | if (IS_ERR_OR_NULL(fnode->hw_strobe_state_suspend)) { |
| 1485 | pr_err("No suspend pin for hardware strobe, rc=%ld\n", |
| 1486 | PTR_ERR(fnode->hw_strobe_state_suspend) |
| 1487 | ); |
| 1488 | fnode->hw_strobe_state_suspend = NULL; |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | rc = led_classdev_register(&led->pdev->dev, &fnode->cdev); |
| 1494 | if (rc < 0) { |
| 1495 | pr_err("Unable to register led node %d\n", fnode->id); |
| 1496 | return rc; |
| 1497 | } |
| 1498 | |
| 1499 | fnode->cdev.dev->of_node = node; |
| 1500 | |
| 1501 | fnode->pinctrl = devm_pinctrl_get(fnode->cdev.dev); |
| 1502 | if (IS_ERR_OR_NULL(fnode->pinctrl)) { |
| 1503 | pr_debug("No pinctrl defined\n"); |
| 1504 | fnode->pinctrl = NULL; |
| 1505 | } else { |
| 1506 | fnode->gpio_state_active = |
| 1507 | pinctrl_lookup_state(fnode->pinctrl, "led_enable"); |
| 1508 | if (IS_ERR_OR_NULL(fnode->gpio_state_active)) { |
| 1509 | pr_err("Cannot lookup LED active state\n"); |
| 1510 | devm_pinctrl_put(fnode->pinctrl); |
| 1511 | fnode->pinctrl = NULL; |
| 1512 | return PTR_ERR(fnode->gpio_state_active); |
| 1513 | } |
| 1514 | |
| 1515 | fnode->gpio_state_suspend = |
| 1516 | pinctrl_lookup_state(fnode->pinctrl, "led_disable"); |
| 1517 | if (IS_ERR_OR_NULL(fnode->gpio_state_suspend)) { |
| 1518 | pr_err("Cannot lookup LED disable state\n"); |
| 1519 | devm_pinctrl_put(fnode->pinctrl); |
| 1520 | fnode->pinctrl = NULL; |
| 1521 | return PTR_ERR(fnode->gpio_state_suspend); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int qpnp_flash_led_parse_and_register_switch(struct qpnp_flash_led *led, |
| 1529 | struct flash_switch_data *snode, |
| 1530 | struct device_node *node) |
| 1531 | { |
| 1532 | int rc = 0, num; |
| 1533 | char reg_name[16], reg_sup_name[16]; |
| 1534 | |
| 1535 | rc = of_property_read_string(node, "qcom,led-name", &snode->cdev.name); |
| 1536 | if (rc < 0) { |
| 1537 | pr_err("Failed to read switch node name, rc=%d\n", rc); |
| 1538 | return rc; |
| 1539 | } |
| 1540 | |
| 1541 | rc = sscanf(snode->cdev.name, "led:switch_%d", &num); |
| 1542 | if (!rc) { |
| 1543 | pr_err("No number for switch device?\n"); |
| 1544 | return -EINVAL; |
| 1545 | } |
| 1546 | |
| 1547 | rc = of_property_read_string(node, "qcom,default-led-trigger", |
| 1548 | &snode->cdev.default_trigger); |
| 1549 | if (rc < 0) { |
| 1550 | pr_err("Unable to read trigger name, rc=%d\n", rc); |
| 1551 | return rc; |
| 1552 | } |
| 1553 | |
| 1554 | rc = of_property_read_u32(node, "qcom,led-mask", &snode->led_mask); |
| 1555 | if (rc < 0) { |
| 1556 | pr_err("Unable to read led mask rc=%d\n", rc); |
| 1557 | return rc; |
| 1558 | } |
| 1559 | |
| 1560 | if (snode->led_mask < 1 || snode->led_mask > 7) { |
| 1561 | pr_err("Invalid value for led-mask\n"); |
| 1562 | return -EINVAL; |
| 1563 | } |
| 1564 | |
| 1565 | scnprintf(reg_name, sizeof(reg_name), "switch%d-supply", num); |
| 1566 | if (of_find_property(led->pdev->dev.of_node, reg_name, NULL)) { |
| 1567 | scnprintf(reg_sup_name, sizeof(reg_sup_name), "switch%d", num); |
| 1568 | snode->vreg = devm_regulator_get(&led->pdev->dev, reg_sup_name); |
| 1569 | if (IS_ERR_OR_NULL(snode->vreg)) { |
| 1570 | rc = PTR_ERR(snode->vreg); |
| 1571 | if (rc != -EPROBE_DEFER) |
| 1572 | pr_err("Failed to get regulator, rc=%d\n", rc); |
| 1573 | snode->vreg = NULL; |
| 1574 | return rc; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | snode->pdev = led->pdev; |
| 1579 | snode->cdev.brightness_set = qpnp_flash_led_brightness_set; |
| 1580 | snode->cdev.brightness_get = qpnp_flash_led_brightness_get; |
| 1581 | snode->cdev.flags |= LED_KEEP_TRIGGER; |
| 1582 | rc = led_classdev_register(&led->pdev->dev, &snode->cdev); |
| 1583 | if (rc < 0) { |
| 1584 | pr_err("Unable to register led switch node\n"); |
| 1585 | return rc; |
| 1586 | } |
| 1587 | |
| 1588 | snode->cdev.dev->of_node = node; |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | static int get_code_from_table(int *table, int len, int value) |
| 1593 | { |
| 1594 | int i; |
| 1595 | |
| 1596 | for (i = 0; i < len; i++) { |
| 1597 | if (value == table[i]) |
| 1598 | break; |
| 1599 | } |
| 1600 | |
| 1601 | if (i == len) { |
| 1602 | pr_err("Couldn't find %d from table\n", value); |
| 1603 | return -ENODATA; |
| 1604 | } |
| 1605 | |
| 1606 | return i; |
| 1607 | } |
| 1608 | |
| 1609 | static int qpnp_flash_led_parse_common_dt(struct qpnp_flash_led *led, |
| 1610 | struct device_node *node) |
| 1611 | { |
| 1612 | struct device_node *revid_node; |
| 1613 | int rc; |
| 1614 | u32 val; |
| 1615 | bool short_circuit_det, open_circuit_det, vph_droop_det; |
| 1616 | |
| 1617 | revid_node = of_parse_phandle(node, "qcom,pmic-revid", 0); |
| 1618 | if (!revid_node) { |
| 1619 | pr_err("Missing qcom,pmic-revid property - driver failed\n"); |
| 1620 | return -EINVAL; |
| 1621 | } |
| 1622 | |
| 1623 | led->pdata->pmic_rev_id = get_revid_data(revid_node); |
| 1624 | if (IS_ERR_OR_NULL(led->pdata->pmic_rev_id)) { |
| 1625 | pr_err("Unable to get pmic_revid rc=%ld\n", |
| 1626 | PTR_ERR(led->pdata->pmic_rev_id)); |
| 1627 | /* |
| 1628 | * the revid peripheral must be registered, any failure |
| 1629 | * here only indicates that the rev-id module has not |
| 1630 | * probed yet. |
| 1631 | */ |
| 1632 | return -EPROBE_DEFER; |
| 1633 | } |
| 1634 | |
| 1635 | pr_debug("PMIC subtype %d Digital major %d\n", |
| 1636 | led->pdata->pmic_rev_id->pmic_subtype, |
| 1637 | led->pdata->pmic_rev_id->rev4); |
| 1638 | |
| 1639 | led->pdata->hdrm_auto_mode_en = of_property_read_bool(node, |
| 1640 | "qcom,hdrm-auto-mode"); |
| 1641 | |
| 1642 | led->pdata->isc_delay = FLASH_LED_ISC_DELAY_DEFAULT; |
| 1643 | rc = of_property_read_u32(node, "qcom,isc-delay-us", &val); |
| 1644 | if (!rc) { |
| 1645 | led->pdata->isc_delay = |
| 1646 | val >> FLASH_LED_ISC_WARMUP_DELAY_SHIFT; |
| 1647 | } else if (rc != -EINVAL) { |
| 1648 | pr_err("Unable to read ISC delay, rc=%d\n", rc); |
| 1649 | return rc; |
| 1650 | } |
| 1651 | |
| 1652 | led->pdata->warmup_delay = FLASH_LED_WARMUP_DELAY_DEFAULT; |
| 1653 | rc = of_property_read_u32(node, "qcom,warmup-delay-us", &val); |
| 1654 | if (!rc) { |
| 1655 | led->pdata->warmup_delay = |
| 1656 | val >> FLASH_LED_ISC_WARMUP_DELAY_SHIFT; |
| 1657 | } else if (rc != -EINVAL) { |
| 1658 | pr_err("Unable to read WARMUP delay, rc=%d\n", rc); |
| 1659 | return rc; |
| 1660 | } |
| 1661 | |
| 1662 | short_circuit_det = |
| 1663 | of_property_read_bool(node, "qcom,short-circuit-det"); |
| 1664 | open_circuit_det = of_property_read_bool(node, "qcom,open-circuit-det"); |
| 1665 | vph_droop_det = of_property_read_bool(node, "qcom,vph-droop-det"); |
| 1666 | led->pdata->current_derate_en_cfg = (vph_droop_det << 2) | |
| 1667 | (open_circuit_det << 1) | short_circuit_det; |
| 1668 | |
| 1669 | led->pdata->thermal_derate_en = |
| 1670 | of_property_read_bool(node, "qcom,thermal-derate-en"); |
| 1671 | |
| 1672 | if (led->pdata->thermal_derate_en) { |
| 1673 | led->pdata->thermal_derate_current = |
| 1674 | devm_kcalloc(&led->pdev->dev, |
| 1675 | FLASH_LED_THERMAL_OTST_LEVELS, |
| 1676 | sizeof(int), GFP_KERNEL); |
| 1677 | if (!led->pdata->thermal_derate_current) |
| 1678 | return -ENOMEM; |
| 1679 | |
| 1680 | rc = of_property_read_u32_array(node, |
| 1681 | "qcom,thermal-derate-current", |
| 1682 | led->pdata->thermal_derate_current, |
| 1683 | FLASH_LED_THERMAL_OTST_LEVELS); |
| 1684 | if (rc < 0) { |
| 1685 | pr_err("Unable to read thermal current limits, rc=%d\n", |
| 1686 | rc); |
| 1687 | return rc; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | led->pdata->otst_ramp_bkup_en = |
| 1692 | !of_property_read_bool(node, "qcom,otst-ramp-back-up-dis"); |
| 1693 | |
| 1694 | led->pdata->thermal_derate_slow = -EINVAL; |
| 1695 | rc = of_property_read_u32(node, "qcom,thermal-derate-slow", &val); |
| 1696 | if (!rc) { |
| 1697 | if (val < 0 || val > THERMAL_DERATE_SLOW_MAX) { |
| 1698 | pr_err("Invalid thermal_derate_slow %d\n", val); |
| 1699 | return -EINVAL; |
| 1700 | } |
| 1701 | |
| 1702 | led->pdata->thermal_derate_slow = |
| 1703 | get_code_from_table(thermal_derate_slow_table, |
| 1704 | ARRAY_SIZE(thermal_derate_slow_table), val); |
| 1705 | } else if (rc != -EINVAL) { |
| 1706 | pr_err("Unable to read thermal derate slow, rc=%d\n", rc); |
| 1707 | return rc; |
| 1708 | } |
| 1709 | |
| 1710 | led->pdata->thermal_derate_fast = -EINVAL; |
| 1711 | rc = of_property_read_u32(node, "qcom,thermal-derate-fast", &val); |
| 1712 | if (!rc) { |
| 1713 | if (val < 0 || val > THERMAL_DERATE_FAST_MAX) { |
| 1714 | pr_err("Invalid thermal_derate_fast %d\n", val); |
| 1715 | return -EINVAL; |
| 1716 | } |
| 1717 | |
| 1718 | led->pdata->thermal_derate_fast = |
| 1719 | get_code_from_table(thermal_derate_fast_table, |
| 1720 | ARRAY_SIZE(thermal_derate_fast_table), val); |
| 1721 | } else if (rc != -EINVAL) { |
| 1722 | pr_err("Unable to read thermal derate fast, rc=%d\n", rc); |
| 1723 | return rc; |
| 1724 | } |
| 1725 | |
| 1726 | led->pdata->thermal_debounce = -EINVAL; |
| 1727 | rc = of_property_read_u32(node, "qcom,thermal-debounce", &val); |
| 1728 | if (!rc) { |
| 1729 | if (val < 0 || val > THERMAL_DEBOUNCE_TIME_MAX) { |
| 1730 | pr_err("Invalid thermal_debounce %d\n", val); |
| 1731 | return -EINVAL; |
| 1732 | } |
| 1733 | |
| 1734 | if (val >= 0 && val < 16) |
| 1735 | led->pdata->thermal_debounce = 0; |
| 1736 | else |
| 1737 | led->pdata->thermal_debounce = ilog2(val) - 3; |
| 1738 | } else if (rc != -EINVAL) { |
| 1739 | pr_err("Unable to read thermal debounce, rc=%d\n", rc); |
| 1740 | return rc; |
| 1741 | } |
| 1742 | |
| 1743 | led->pdata->thermal_hysteresis = -EINVAL; |
| 1744 | rc = of_property_read_u32(node, "qcom,thermal-hysteresis", &val); |
| 1745 | if (!rc) { |
| 1746 | if (led->pdata->pmic_rev_id->pmic_subtype == PM660L_SUBTYPE) |
| 1747 | val = THERMAL_HYST_TEMP_TO_VAL(val, 20); |
| 1748 | else |
| 1749 | val = THERMAL_HYST_TEMP_TO_VAL(val, 15); |
| 1750 | |
| 1751 | if (val < 0 || val > THERMAL_DERATE_HYSTERESIS_MAX) { |
| 1752 | pr_err("Invalid thermal_derate_hysteresis %d\n", val); |
| 1753 | return -EINVAL; |
| 1754 | } |
| 1755 | |
| 1756 | led->pdata->thermal_hysteresis = val; |
| 1757 | } else if (rc != -EINVAL) { |
| 1758 | pr_err("Unable to read thermal hysteresis, rc=%d\n", rc); |
| 1759 | return rc; |
| 1760 | } |
| 1761 | |
| 1762 | led->pdata->thermal_thrsh1 = -EINVAL; |
| 1763 | rc = of_property_read_u32(node, "qcom,thermal-thrsh1", &val); |
| 1764 | if (!rc) { |
| 1765 | led->pdata->thermal_thrsh1 = |
| 1766 | get_code_from_table(otst1_threshold_table, |
| 1767 | ARRAY_SIZE(otst1_threshold_table), val); |
| 1768 | } else if (rc != -EINVAL) { |
| 1769 | pr_err("Unable to read thermal thrsh1, rc=%d\n", rc); |
| 1770 | return rc; |
| 1771 | } |
| 1772 | |
| 1773 | led->pdata->thermal_thrsh2 = -EINVAL; |
| 1774 | rc = of_property_read_u32(node, "qcom,thermal-thrsh2", &val); |
| 1775 | if (!rc) { |
| 1776 | led->pdata->thermal_thrsh2 = |
| 1777 | get_code_from_table(otst2_threshold_table, |
| 1778 | ARRAY_SIZE(otst2_threshold_table), val); |
| 1779 | } else if (rc != -EINVAL) { |
| 1780 | pr_err("Unable to read thermal thrsh2, rc=%d\n", rc); |
| 1781 | return rc; |
| 1782 | } |
| 1783 | |
| 1784 | led->pdata->thermal_thrsh3 = -EINVAL; |
| 1785 | rc = of_property_read_u32(node, "qcom,thermal-thrsh3", &val); |
| 1786 | if (!rc) { |
| 1787 | led->pdata->thermal_thrsh3 = |
| 1788 | get_code_from_table(otst3_threshold_table, |
| 1789 | ARRAY_SIZE(otst3_threshold_table), val); |
| 1790 | } else if (rc != -EINVAL) { |
| 1791 | pr_err("Unable to read thermal thrsh3, rc=%d\n", rc); |
| 1792 | return rc; |
| 1793 | } |
| 1794 | |
| 1795 | led->pdata->vph_droop_debounce = FLASH_LED_VPH_DROOP_DEBOUNCE_DEFAULT; |
| 1796 | rc = of_property_read_u32(node, "qcom,vph-droop-debounce-us", &val); |
| 1797 | if (!rc) { |
| 1798 | led->pdata->vph_droop_debounce = |
| 1799 | VPH_DROOP_DEBOUNCE_US_TO_VAL(val); |
| 1800 | } else if (rc != -EINVAL) { |
| 1801 | pr_err("Unable to read VPH droop debounce, rc=%d\n", rc); |
| 1802 | return rc; |
| 1803 | } |
| 1804 | |
| 1805 | if (led->pdata->vph_droop_debounce > FLASH_LED_DEBOUNCE_MAX) { |
| 1806 | pr_err("Invalid VPH droop debounce specified\n"); |
| 1807 | return -EINVAL; |
| 1808 | } |
| 1809 | |
| 1810 | led->pdata->vph_droop_threshold = FLASH_LED_VPH_DROOP_THRESH_DEFAULT; |
| 1811 | rc = of_property_read_u32(node, "qcom,vph-droop-threshold-mv", &val); |
| 1812 | if (!rc) { |
| 1813 | led->pdata->vph_droop_threshold = |
| 1814 | VPH_DROOP_THRESH_MV_TO_VAL(val); |
| 1815 | } else if (rc != -EINVAL) { |
| 1816 | pr_err("Unable to read VPH droop threshold, rc=%d\n", rc); |
| 1817 | return rc; |
| 1818 | } |
| 1819 | |
| 1820 | if (led->pdata->vph_droop_threshold > FLASH_LED_VPH_DROOP_THRESH_MAX) { |
| 1821 | pr_err("Invalid VPH droop threshold specified\n"); |
| 1822 | return -EINVAL; |
| 1823 | } |
| 1824 | |
| 1825 | led->pdata->vph_droop_hysteresis = |
| 1826 | FLASH_LED_VPH_DROOP_HYST_DEFAULT; |
| 1827 | rc = of_property_read_u32(node, "qcom,vph-droop-hysteresis-mv", &val); |
| 1828 | if (!rc) { |
| 1829 | led->pdata->vph_droop_hysteresis = |
| 1830 | VPH_DROOP_HYST_MV_TO_VAL(val); |
| 1831 | } else if (rc != -EINVAL) { |
| 1832 | pr_err("Unable to read VPH droop hysteresis, rc=%d\n", rc); |
| 1833 | return rc; |
| 1834 | } |
| 1835 | |
| 1836 | if (led->pdata->vph_droop_hysteresis > FLASH_LED_HYSTERESIS_MAX) { |
| 1837 | pr_err("Invalid VPH droop hysteresis specified\n"); |
| 1838 | return -EINVAL; |
| 1839 | } |
| 1840 | |
| 1841 | led->pdata->vph_droop_hysteresis <<= FLASH_LED_VPH_DROOP_HYST_SHIFT; |
| 1842 | |
| 1843 | rc = of_property_read_u32(node, "qcom,hw-strobe-option", &val); |
| 1844 | if (!rc) { |
| 1845 | led->pdata->hw_strobe_option = (u8)val; |
| 1846 | } else if (rc != -EINVAL) { |
| 1847 | pr_err("Unable to parse hw strobe option, rc=%d\n", rc); |
| 1848 | return rc; |
| 1849 | } |
| 1850 | |
| 1851 | rc = of_property_read_u32(node, "qcom,led1n2-iclamp-low-ma", &val); |
| 1852 | if (!rc) { |
| 1853 | led->pdata->led1n2_iclamp_low_ma = val; |
| 1854 | } else if (rc != -EINVAL) { |
| 1855 | pr_err("Unable to read led1n2_iclamp_low current, rc=%d\n", rc); |
| 1856 | return rc; |
| 1857 | } |
| 1858 | |
| 1859 | rc = of_property_read_u32(node, "qcom,led1n2-iclamp-mid-ma", &val); |
| 1860 | if (!rc) { |
| 1861 | led->pdata->led1n2_iclamp_mid_ma = val; |
| 1862 | } else if (rc != -EINVAL) { |
| 1863 | pr_err("Unable to read led1n2_iclamp_mid current, rc=%d\n", rc); |
| 1864 | return rc; |
| 1865 | } |
| 1866 | |
| 1867 | rc = of_property_read_u32(node, "qcom,led3-iclamp-low-ma", &val); |
| 1868 | if (!rc) { |
| 1869 | led->pdata->led3_iclamp_low_ma = val; |
| 1870 | } else if (rc != -EINVAL) { |
| 1871 | pr_err("Unable to read led3_iclamp_low current, rc=%d\n", rc); |
| 1872 | return rc; |
| 1873 | } |
| 1874 | |
| 1875 | rc = of_property_read_u32(node, "qcom,led3-iclamp-mid-ma", &val); |
| 1876 | if (!rc) { |
| 1877 | led->pdata->led3_iclamp_mid_ma = val; |
| 1878 | } else if (rc != -EINVAL) { |
| 1879 | pr_err("Unable to read led3_iclamp_mid current, rc=%d\n", rc); |
| 1880 | return rc; |
| 1881 | } |
| 1882 | |
| 1883 | led->pdata->vled_max_uv = FLASH_LED_VLED_MAX_DEFAULT_UV; |
| 1884 | rc = of_property_read_u32(node, "qcom,vled-max-uv", &val); |
| 1885 | if (!rc) { |
| 1886 | led->pdata->vled_max_uv = val; |
| 1887 | } else if (rc != -EINVAL) { |
| 1888 | pr_err("Unable to parse vled_max voltage, rc=%d\n", rc); |
| 1889 | return rc; |
| 1890 | } |
| 1891 | |
| 1892 | led->pdata->ibatt_ocp_threshold_ua = |
| 1893 | FLASH_LED_IBATT_OCP_THRESH_DEFAULT_UA; |
| 1894 | rc = of_property_read_u32(node, "qcom,ibatt-ocp-threshold-ua", &val); |
| 1895 | if (!rc) { |
| 1896 | led->pdata->ibatt_ocp_threshold_ua = val; |
| 1897 | } else if (rc != -EINVAL) { |
| 1898 | pr_err("Unable to parse ibatt_ocp threshold, rc=%d\n", rc); |
| 1899 | return rc; |
| 1900 | } |
| 1901 | |
| 1902 | led->pdata->rpara_uohm = FLASH_LED_RPARA_DEFAULT_UOHM; |
| 1903 | rc = of_property_read_u32(node, "qcom,rparasitic-uohm", &val); |
| 1904 | if (!rc) { |
| 1905 | led->pdata->rpara_uohm = val; |
| 1906 | } else if (rc != -EINVAL) { |
| 1907 | pr_err("Unable to parse rparasitic, rc=%d\n", rc); |
| 1908 | return rc; |
| 1909 | } |
| 1910 | |
| 1911 | led->pdata->lmh_ocv_threshold_uv = |
| 1912 | FLASH_LED_LMH_OCV_THRESH_DEFAULT_UV; |
| 1913 | rc = of_property_read_u32(node, "qcom,lmh-ocv-threshold-uv", &val); |
| 1914 | if (!rc) { |
| 1915 | led->pdata->lmh_ocv_threshold_uv = val; |
| 1916 | } else if (rc != -EINVAL) { |
| 1917 | pr_err("Unable to parse lmh ocv threshold, rc=%d\n", rc); |
| 1918 | return rc; |
| 1919 | } |
| 1920 | |
| 1921 | led->pdata->lmh_rbatt_threshold_uohm = |
| 1922 | FLASH_LED_LMH_RBATT_THRESH_DEFAULT_UOHM; |
| 1923 | rc = of_property_read_u32(node, "qcom,lmh-rbatt-threshold-uohm", &val); |
| 1924 | if (!rc) { |
| 1925 | led->pdata->lmh_rbatt_threshold_uohm = val; |
| 1926 | } else if (rc != -EINVAL) { |
| 1927 | pr_err("Unable to parse lmh rbatt threshold, rc=%d\n", rc); |
| 1928 | return rc; |
| 1929 | } |
| 1930 | |
| 1931 | led->pdata->lmh_level = FLASH_LED_LMH_LEVEL_DEFAULT; |
| 1932 | rc = of_property_read_u32(node, "qcom,lmh-level", &val); |
| 1933 | if (!rc) { |
| 1934 | led->pdata->lmh_level = val; |
| 1935 | } else if (rc != -EINVAL) { |
| 1936 | pr_err("Unable to parse lmh_level, rc=%d\n", rc); |
| 1937 | return rc; |
| 1938 | } |
| 1939 | |
| 1940 | led->pdata->lmh_mitigation_sel = FLASH_LED_MITIGATION_SEL_DEFAULT; |
| 1941 | rc = of_property_read_u32(node, "qcom,lmh-mitigation-sel", &val); |
| 1942 | if (!rc) { |
| 1943 | led->pdata->lmh_mitigation_sel = val; |
| 1944 | } else if (rc != -EINVAL) { |
| 1945 | pr_err("Unable to parse lmh_mitigation_sel, rc=%d\n", rc); |
| 1946 | return rc; |
| 1947 | } |
| 1948 | |
| 1949 | if (led->pdata->lmh_mitigation_sel > FLASH_LED_MITIGATION_SEL_MAX) { |
| 1950 | pr_err("Invalid lmh_mitigation_sel specified\n"); |
| 1951 | return -EINVAL; |
| 1952 | } |
| 1953 | |
| 1954 | led->pdata->chgr_mitigation_sel = FLASH_LED_MITIGATION_SEL_DEFAULT; |
| 1955 | rc = of_property_read_u32(node, "qcom,chgr-mitigation-sel", &val); |
| 1956 | if (!rc) { |
| 1957 | led->pdata->chgr_mitigation_sel = val; |
| 1958 | } else if (rc != -EINVAL) { |
| 1959 | pr_err("Unable to parse chgr_mitigation_sel, rc=%d\n", rc); |
| 1960 | return rc; |
| 1961 | } |
| 1962 | |
| 1963 | if (led->pdata->chgr_mitigation_sel > FLASH_LED_MITIGATION_SEL_MAX) { |
| 1964 | pr_err("Invalid chgr_mitigation_sel specified\n"); |
| 1965 | return -EINVAL; |
| 1966 | } |
| 1967 | |
| 1968 | led->pdata->chgr_mitigation_sel <<= FLASH_LED_CHGR_MITIGATION_SEL_SHIFT; |
| 1969 | |
| 1970 | led->pdata->iled_thrsh_val = FLASH_LED_MITIGATION_THRSH_DEFAULT; |
| 1971 | rc = of_property_read_u32(node, "qcom,iled-thrsh-ma", &val); |
| 1972 | if (!rc) { |
| 1973 | led->pdata->iled_thrsh_val = MITIGATION_THRSH_MA_TO_VAL(val); |
| 1974 | } else if (rc != -EINVAL) { |
| 1975 | pr_err("Unable to parse iled_thrsh_val, rc=%d\n", rc); |
| 1976 | return rc; |
| 1977 | } |
| 1978 | |
| 1979 | if (led->pdata->iled_thrsh_val > FLASH_LED_MITIGATION_THRSH_MAX) { |
| 1980 | pr_err("Invalid iled_thrsh_val specified\n"); |
| 1981 | return -EINVAL; |
| 1982 | } |
| 1983 | |
| 1984 | led->pdata->all_ramp_up_done_irq = |
| 1985 | of_irq_get_byname(node, "all-ramp-up-done-irq"); |
| 1986 | if (led->pdata->all_ramp_up_done_irq < 0) |
| 1987 | pr_debug("all-ramp-up-done-irq not used\n"); |
| 1988 | |
| 1989 | led->pdata->all_ramp_down_done_irq = |
| 1990 | of_irq_get_byname(node, "all-ramp-down-done-irq"); |
| 1991 | if (led->pdata->all_ramp_down_done_irq < 0) |
| 1992 | pr_debug("all-ramp-down-done-irq not used\n"); |
| 1993 | |
| 1994 | led->pdata->led_fault_irq = |
| 1995 | of_irq_get_byname(node, "led-fault-irq"); |
| 1996 | if (led->pdata->led_fault_irq < 0) |
| 1997 | pr_debug("led-fault-irq not used\n"); |
| 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | static int qpnp_flash_led_probe(struct platform_device *pdev) |
| 2003 | { |
| 2004 | struct qpnp_flash_led *led; |
| 2005 | struct device_node *node, *temp; |
| 2006 | const char *temp_string; |
| 2007 | unsigned int base; |
| 2008 | int rc, i = 0, j = 0; |
| 2009 | |
| 2010 | node = pdev->dev.of_node; |
| 2011 | if (!node) { |
| 2012 | pr_err("No flash LED nodes defined\n"); |
| 2013 | return -ENODEV; |
| 2014 | } |
| 2015 | |
| 2016 | rc = of_property_read_u32(node, "reg", &base); |
| 2017 | if (rc < 0) { |
| 2018 | pr_err("Couldn't find reg in node %s, rc = %d\n", |
| 2019 | node->full_name, rc); |
| 2020 | return rc; |
| 2021 | } |
| 2022 | |
| 2023 | led = devm_kzalloc(&pdev->dev, sizeof(struct qpnp_flash_led), |
| 2024 | GFP_KERNEL); |
| 2025 | if (!led) |
| 2026 | return -ENOMEM; |
| 2027 | |
| 2028 | led->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 2029 | if (!led->regmap) { |
| 2030 | pr_err("Couldn't get parent's regmap\n"); |
| 2031 | return -EINVAL; |
| 2032 | } |
| 2033 | |
| 2034 | led->base = base; |
| 2035 | led->pdev = pdev; |
| 2036 | led->pdata = devm_kzalloc(&pdev->dev, |
| 2037 | sizeof(struct flash_led_platform_data), GFP_KERNEL); |
| 2038 | if (!led->pdata) |
| 2039 | return -ENOMEM; |
| 2040 | |
| 2041 | rc = qpnp_flash_led_parse_common_dt(led, node); |
| 2042 | if (rc < 0) { |
| 2043 | pr_err("Failed to parse common flash LED device tree\n"); |
| 2044 | return rc; |
| 2045 | } |
| 2046 | |
| 2047 | for_each_available_child_of_node(node, temp) { |
| 2048 | rc = of_property_read_string(temp, "label", &temp_string); |
| 2049 | if (rc < 0) { |
| 2050 | pr_err("Failed to parse label, rc=%d\n", rc); |
| 2051 | return rc; |
| 2052 | } |
| 2053 | |
| 2054 | if (!strcmp("switch", temp_string)) { |
| 2055 | led->num_snodes++; |
| 2056 | } else if (!strcmp("flash", temp_string) || |
| 2057 | !strcmp("torch", temp_string)) { |
| 2058 | led->num_fnodes++; |
| 2059 | } else { |
| 2060 | pr_err("Invalid label for led node\n"); |
| 2061 | return -EINVAL; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | if (!led->num_fnodes) { |
| 2066 | pr_err("No LED nodes defined\n"); |
| 2067 | return -ECHILD; |
| 2068 | } |
| 2069 | |
| 2070 | led->fnode = devm_kcalloc(&pdev->dev, led->num_fnodes, |
| 2071 | sizeof(*led->fnode), |
| 2072 | GFP_KERNEL); |
| 2073 | if (!led->fnode) |
| 2074 | return -ENOMEM; |
| 2075 | |
| 2076 | led->snode = devm_kcalloc(&pdev->dev, led->num_snodes, |
| 2077 | sizeof(*led->snode), |
| 2078 | GFP_KERNEL); |
| 2079 | if (!led->snode) |
| 2080 | return -ENOMEM; |
| 2081 | |
| 2082 | temp = NULL; |
| 2083 | i = 0; |
| 2084 | j = 0; |
| 2085 | for_each_available_child_of_node(node, temp) { |
| 2086 | rc = of_property_read_string(temp, "label", &temp_string); |
| 2087 | if (rc < 0) { |
| 2088 | pr_err("Failed to parse label, rc=%d\n", rc); |
| 2089 | return rc; |
| 2090 | } |
| 2091 | |
| 2092 | if (!strcmp("flash", temp_string) || |
| 2093 | !strcmp("torch", temp_string)) { |
| 2094 | rc = qpnp_flash_led_parse_each_led_dt(led, |
| 2095 | &led->fnode[i++], temp); |
| 2096 | if (rc < 0) { |
| 2097 | pr_err("Unable to parse flash node %d rc=%d\n", |
| 2098 | i, rc); |
| 2099 | goto error_led_register; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | if (!strcmp("switch", temp_string)) { |
| 2104 | rc = qpnp_flash_led_parse_and_register_switch(led, |
| 2105 | &led->snode[j++], temp); |
| 2106 | if (rc < 0) { |
| 2107 | pr_err("Unable to parse and register switch node, rc=%d\n", |
| 2108 | rc); |
| 2109 | goto error_switch_register; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | /* setup irqs */ |
| 2115 | if (led->pdata->all_ramp_up_done_irq >= 0) { |
| 2116 | rc = devm_request_threaded_irq(&led->pdev->dev, |
| 2117 | led->pdata->all_ramp_up_done_irq, |
| 2118 | NULL, qpnp_flash_led_irq_handler, |
| 2119 | IRQF_ONESHOT, |
| 2120 | "qpnp_flash_led_all_ramp_up_done_irq", led); |
| 2121 | if (rc < 0) { |
| 2122 | pr_err("Unable to request all_ramp_up_done(%d) IRQ(err:%d)\n", |
| 2123 | led->pdata->all_ramp_up_done_irq, rc); |
| 2124 | goto error_switch_register; |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | if (led->pdata->all_ramp_down_done_irq >= 0) { |
| 2129 | rc = devm_request_threaded_irq(&led->pdev->dev, |
| 2130 | led->pdata->all_ramp_down_done_irq, |
| 2131 | NULL, qpnp_flash_led_irq_handler, |
| 2132 | IRQF_ONESHOT, |
| 2133 | "qpnp_flash_led_all_ramp_down_done_irq", led); |
| 2134 | if (rc < 0) { |
| 2135 | pr_err("Unable to request all_ramp_down_done(%d) IRQ(err:%d)\n", |
| 2136 | led->pdata->all_ramp_down_done_irq, rc); |
| 2137 | goto error_switch_register; |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | if (led->pdata->led_fault_irq >= 0) { |
| 2142 | rc = devm_request_threaded_irq(&led->pdev->dev, |
| 2143 | led->pdata->led_fault_irq, |
| 2144 | NULL, qpnp_flash_led_irq_handler, |
| 2145 | IRQF_ONESHOT, |
| 2146 | "qpnp_flash_led_fault_irq", led); |
| 2147 | if (rc < 0) { |
| 2148 | pr_err("Unable to request led_fault(%d) IRQ(err:%d)\n", |
| 2149 | led->pdata->led_fault_irq, rc); |
| 2150 | goto error_switch_register; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | led->bms_psy = power_supply_get_by_name("bms"); |
| 2155 | if (!led->bms_psy) { |
| 2156 | rc = flash_led_psy_register_notifier(led); |
| 2157 | if (rc < 0) { |
| 2158 | pr_err("Couldn't register psy notifier, rc = %d\n", rc); |
| 2159 | goto error_switch_register; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | rc = qpnp_flash_led_init_settings(led); |
| 2164 | if (rc < 0) { |
| 2165 | pr_err("Failed to initialize flash LED, rc=%d\n", rc); |
| 2166 | goto unreg_notifier; |
| 2167 | } |
| 2168 | |
| 2169 | for (i = 0; i < led->num_snodes; i++) { |
| 2170 | for (j = 0; j < ARRAY_SIZE(qpnp_flash_led_attrs); j++) { |
| 2171 | rc = sysfs_create_file(&led->snode[i].cdev.dev->kobj, |
| 2172 | &qpnp_flash_led_attrs[j].attr); |
| 2173 | if (rc < 0) { |
| 2174 | pr_err("sysfs creation failed, rc=%d\n", rc); |
| 2175 | goto sysfs_fail; |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | spin_lock_init(&led->lock); |
| 2181 | |
| 2182 | dev_set_drvdata(&pdev->dev, led); |
| 2183 | |
| 2184 | return 0; |
| 2185 | |
| 2186 | sysfs_fail: |
| 2187 | for (--j; j >= 0; j--) |
| 2188 | sysfs_remove_file(&led->snode[i].cdev.dev->kobj, |
| 2189 | &qpnp_flash_led_attrs[j].attr); |
| 2190 | |
| 2191 | for (--i; i >= 0; i--) { |
| 2192 | for (j = 0; j < ARRAY_SIZE(qpnp_flash_led_attrs); j++) |
| 2193 | sysfs_remove_file(&led->snode[i].cdev.dev->kobj, |
| 2194 | &qpnp_flash_led_attrs[j].attr); |
| 2195 | } |
| 2196 | |
| 2197 | i = led->num_snodes; |
| 2198 | unreg_notifier: |
| 2199 | power_supply_unreg_notifier(&led->nb); |
| 2200 | error_switch_register: |
| 2201 | while (i > 0) |
| 2202 | led_classdev_unregister(&led->snode[--i].cdev); |
| 2203 | i = led->num_fnodes; |
| 2204 | error_led_register: |
| 2205 | while (i > 0) |
| 2206 | led_classdev_unregister(&led->fnode[--i].cdev); |
| 2207 | |
| 2208 | return rc; |
| 2209 | } |
| 2210 | |
| 2211 | static int qpnp_flash_led_remove(struct platform_device *pdev) |
| 2212 | { |
| 2213 | struct qpnp_flash_led *led = dev_get_drvdata(&pdev->dev); |
| 2214 | int i, j; |
| 2215 | |
| 2216 | for (i = 0; i < led->num_snodes; i++) { |
| 2217 | for (j = 0; j < ARRAY_SIZE(qpnp_flash_led_attrs); j++) |
| 2218 | sysfs_remove_file(&led->snode[i].cdev.dev->kobj, |
| 2219 | &qpnp_flash_led_attrs[j].attr); |
| 2220 | |
| 2221 | if (led->snode[i].regulator_on) |
| 2222 | qpnp_flash_led_regulator_enable(led, |
| 2223 | &led->snode[i], false); |
| 2224 | } |
| 2225 | |
| 2226 | while (i > 0) |
| 2227 | led_classdev_unregister(&led->snode[--i].cdev); |
| 2228 | |
| 2229 | i = led->num_fnodes; |
| 2230 | while (i > 0) |
| 2231 | led_classdev_unregister(&led->fnode[--i].cdev); |
| 2232 | |
| 2233 | power_supply_unreg_notifier(&led->nb); |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | const struct of_device_id qpnp_flash_led_match_table[] = { |
| 2238 | { .compatible = "qcom,qpnp-flash-led-v2",}, |
| 2239 | { }, |
| 2240 | }; |
| 2241 | |
| 2242 | static struct platform_driver qpnp_flash_led_driver = { |
| 2243 | .driver = { |
| 2244 | .name = "qcom,qpnp-flash-led-v2", |
| 2245 | .of_match_table = qpnp_flash_led_match_table, |
| 2246 | }, |
| 2247 | .probe = qpnp_flash_led_probe, |
| 2248 | .remove = qpnp_flash_led_remove, |
| 2249 | }; |
| 2250 | |
| 2251 | static int __init qpnp_flash_led_init(void) |
| 2252 | { |
| 2253 | return platform_driver_register(&qpnp_flash_led_driver); |
| 2254 | } |
| 2255 | late_initcall(qpnp_flash_led_init); |
| 2256 | |
| 2257 | static void __exit qpnp_flash_led_exit(void) |
| 2258 | { |
| 2259 | platform_driver_unregister(&qpnp_flash_led_driver); |
| 2260 | } |
| 2261 | module_exit(qpnp_flash_led_exit); |
| 2262 | |
| 2263 | MODULE_DESCRIPTION("QPNP Flash LED driver v2"); |
| 2264 | MODULE_LICENSE("GPL v2"); |
| 2265 | MODULE_ALIAS("leds:leds-qpnp-flash-v2"); |