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