David Collins | 8885f79 | 2017-01-26 14:36:34 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2012-2015, 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 | #include <linux/kernel.h> |
| 14 | #include <linux/regmap.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/leds.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/spmi.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/qpnp/pwm.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/regulator/consumer.h> |
| 29 | #include <linux/delay.h> |
| 30 | |
| 31 | #define WLED_MOD_EN_REG(base, n) (base + 0x60 + n*0x10) |
| 32 | #define WLED_IDAC_DLY_REG(base, n) (WLED_MOD_EN_REG(base, n) + 0x01) |
| 33 | #define WLED_FULL_SCALE_REG(base, n) (WLED_IDAC_DLY_REG(base, n) + 0x01) |
| 34 | #define WLED_MOD_SRC_SEL_REG(base, n) (WLED_FULL_SCALE_REG(base, n) + 0x01) |
| 35 | |
| 36 | /* wled control registers */ |
| 37 | #define WLED_OVP_INT_STATUS(base) (base + 0x10) |
| 38 | #define WLED_BRIGHTNESS_CNTL_LSB(base, n) (base + 0x40 + 2*n) |
| 39 | #define WLED_BRIGHTNESS_CNTL_MSB(base, n) (base + 0x41 + 2*n) |
| 40 | #define WLED_MOD_CTRL_REG(base) (base + 0x46) |
| 41 | #define WLED_SYNC_REG(base) (base + 0x47) |
| 42 | #define WLED_FDBCK_CTRL_REG(base) (base + 0x48) |
| 43 | #define WLED_SWITCHING_FREQ_REG(base) (base + 0x4C) |
| 44 | #define WLED_OVP_CFG_REG(base) (base + 0x4D) |
| 45 | #define WLED_BOOST_LIMIT_REG(base) (base + 0x4E) |
| 46 | #define WLED_CURR_SINK_REG(base) (base + 0x4F) |
| 47 | #define WLED_HIGH_POLE_CAP_REG(base) (base + 0x58) |
| 48 | #define WLED_CURR_SINK_MASK 0xE0 |
| 49 | #define WLED_CURR_SINK_SHFT 0x05 |
| 50 | #define WLED_DISABLE_ALL_SINKS 0x00 |
| 51 | #define WLED_DISABLE_1_2_SINKS 0x80 |
| 52 | #define WLED_SWITCH_FREQ_MASK 0x0F |
| 53 | #define WLED_OVP_VAL_MASK 0x03 |
| 54 | #define WLED_OVP_INT_MASK 0x02 |
| 55 | #define WLED_OVP_VAL_BIT_SHFT 0x00 |
| 56 | #define WLED_BOOST_LIMIT_MASK 0x07 |
| 57 | #define WLED_BOOST_LIMIT_BIT_SHFT 0x00 |
| 58 | #define WLED_BOOST_ON 0x80 |
| 59 | #define WLED_BOOST_OFF 0x00 |
| 60 | #define WLED_EN_MASK 0x80 |
| 61 | #define WLED_NO_MASK 0x00 |
| 62 | #define WLED_CP_SELECT_MAX 0x03 |
| 63 | #define WLED_CP_SELECT_MASK 0x02 |
| 64 | #define WLED_USE_EXT_GEN_MOD_SRC 0x01 |
| 65 | #define WLED_CTL_DLY_STEP 200 |
| 66 | #define WLED_CTL_DLY_MAX 1400 |
| 67 | #define WLED_MAX_CURR 25 |
| 68 | #define WLED_NO_CURRENT 0x00 |
| 69 | #define WLED_OVP_DELAY 1000 |
| 70 | #define WLED_OVP_DELAY_INT 200 |
| 71 | #define WLED_OVP_DELAY_LOOP 100 |
| 72 | #define WLED_MSB_MASK 0x0F |
| 73 | #define WLED_MAX_CURR_MASK 0x1F |
| 74 | #define WLED_OP_FDBCK_MASK 0x07 |
| 75 | #define WLED_OP_FDBCK_BIT_SHFT 0x00 |
| 76 | #define WLED_OP_FDBCK_DEFAULT 0x00 |
| 77 | |
| 78 | #define WLED_SET_ILIM_CODE 0x01 |
| 79 | |
| 80 | #define WLED_MAX_LEVEL 4095 |
| 81 | #define WLED_8_BIT_MASK 0xFF |
| 82 | #define WLED_4_BIT_MASK 0x0F |
| 83 | #define WLED_8_BIT_SHFT 0x08 |
| 84 | #define WLED_MAX_DUTY_CYCLE 0xFFF |
| 85 | |
| 86 | #define WLED_SYNC_VAL 0x07 |
| 87 | #define WLED_SYNC_RESET_VAL 0x00 |
| 88 | |
| 89 | #define PMIC_VER_8026 0x04 |
| 90 | #define PMIC_VER_8941 0x01 |
| 91 | #define PMIC_VERSION_REG 0x0105 |
| 92 | |
| 93 | #define WLED_DEFAULT_STRINGS 0x01 |
| 94 | #define WLED_THREE_STRINGS 0x03 |
| 95 | #define WLED_MAX_TRIES 5 |
| 96 | #define WLED_DEFAULT_OVP_VAL 0x02 |
| 97 | #define WLED_BOOST_LIM_DEFAULT 0x03 |
| 98 | #define WLED_CP_SEL_DEFAULT 0x00 |
| 99 | #define WLED_CTRL_DLY_DEFAULT 0x00 |
| 100 | #define WLED_SWITCH_FREQ_DEFAULT 0x0B |
| 101 | |
| 102 | #define FLASH_SAFETY_TIMER(base) (base + 0x40) |
| 103 | #define FLASH_MAX_CURR(base) (base + 0x41) |
| 104 | #define FLASH_LED_0_CURR(base) (base + 0x42) |
| 105 | #define FLASH_LED_1_CURR(base) (base + 0x43) |
| 106 | #define FLASH_CLAMP_CURR(base) (base + 0x44) |
| 107 | #define FLASH_LED_TMR_CTRL(base) (base + 0x48) |
| 108 | #define FLASH_HEADROOM(base) (base + 0x4A) |
| 109 | #define FLASH_STARTUP_DELAY(base) (base + 0x4B) |
| 110 | #define FLASH_MASK_ENABLE(base) (base + 0x4C) |
| 111 | #define FLASH_VREG_OK_FORCE(base) (base + 0x4F) |
| 112 | #define FLASH_ENABLE_CONTROL(base) (base + 0x46) |
| 113 | #define FLASH_LED_STROBE_CTRL(base) (base + 0x47) |
| 114 | #define FLASH_WATCHDOG_TMR(base) (base + 0x49) |
| 115 | #define FLASH_FAULT_DETECT(base) (base + 0x51) |
| 116 | #define FLASH_PERIPHERAL_SUBTYPE(base) (base + 0x05) |
| 117 | #define FLASH_CURRENT_RAMP(base) (base + 0x54) |
| 118 | |
| 119 | #define FLASH_MAX_LEVEL 0x4F |
| 120 | #define TORCH_MAX_LEVEL 0x0F |
| 121 | #define FLASH_NO_MASK 0x00 |
| 122 | |
| 123 | #define FLASH_MASK_1 0x20 |
| 124 | #define FLASH_MASK_REG_MASK 0xE0 |
| 125 | #define FLASH_HEADROOM_MASK 0x03 |
| 126 | #define FLASH_SAFETY_TIMER_MASK 0x7F |
| 127 | #define FLASH_CURRENT_MASK 0xFF |
| 128 | #define FLASH_MAX_CURRENT_MASK 0x7F |
| 129 | #define FLASH_TMR_MASK 0x03 |
| 130 | #define FLASH_TMR_WATCHDOG 0x03 |
| 131 | #define FLASH_TMR_SAFETY 0x00 |
| 132 | #define FLASH_FAULT_DETECT_MASK 0X80 |
| 133 | #define FLASH_HW_VREG_OK 0x40 |
| 134 | #define FLASH_SW_VREG_OK 0x80 |
| 135 | #define FLASH_VREG_MASK 0xC0 |
| 136 | #define FLASH_STARTUP_DLY_MASK 0x02 |
| 137 | #define FLASH_CURRENT_RAMP_MASK 0xBF |
| 138 | |
| 139 | #define FLASH_ENABLE_ALL 0xE0 |
| 140 | #define FLASH_ENABLE_MODULE 0x80 |
| 141 | #define FLASH_ENABLE_MODULE_MASK 0x80 |
| 142 | #define FLASH_DISABLE_ALL 0x00 |
| 143 | #define FLASH_ENABLE_MASK 0xE0 |
| 144 | #define FLASH_ENABLE_LED_0 0xC0 |
| 145 | #define FLASH_ENABLE_LED_1 0xA0 |
| 146 | #define FLASH_INIT_MASK 0xE0 |
| 147 | #define FLASH_SELFCHECK_ENABLE 0x80 |
| 148 | #define FLASH_WATCHDOG_MASK 0x1F |
| 149 | #define FLASH_RAMP_STEP_27US 0xBF |
| 150 | |
| 151 | #define FLASH_HW_SW_STROBE_SEL_MASK 0x04 |
| 152 | #define FLASH_STROBE_MASK 0xC7 |
| 153 | #define FLASH_LED_0_OUTPUT 0x80 |
| 154 | #define FLASH_LED_1_OUTPUT 0x40 |
| 155 | #define FLASH_TORCH_OUTPUT 0xC0 |
| 156 | |
| 157 | #define FLASH_CURRENT_PRGM_MIN 1 |
| 158 | #define FLASH_CURRENT_PRGM_SHIFT 1 |
| 159 | #define FLASH_CURRENT_MAX 0x4F |
| 160 | #define FLASH_CURRENT_TORCH 0x07 |
| 161 | |
| 162 | #define FLASH_DURATION_200ms 0x13 |
| 163 | #define TORCH_DURATION_12s 0x0A |
| 164 | #define FLASH_CLAMP_200mA 0x0F |
| 165 | |
| 166 | #define FLASH_SUBTYPE_DUAL 0x01 |
| 167 | #define FLASH_SUBTYPE_SINGLE 0x02 |
| 168 | |
| 169 | #define FLASH_RAMP_UP_DELAY_US 1000 |
| 170 | #define FLASH_RAMP_DN_DELAY_US 2160 |
| 171 | |
| 172 | #define LED_TRIGGER_DEFAULT "none" |
| 173 | |
| 174 | #define RGB_LED_SRC_SEL(base) (base + 0x45) |
| 175 | #define RGB_LED_EN_CTL(base) (base + 0x46) |
| 176 | #define RGB_LED_ATC_CTL(base) (base + 0x47) |
| 177 | |
| 178 | #define RGB_MAX_LEVEL LED_FULL |
| 179 | #define RGB_LED_ENABLE_RED 0x80 |
| 180 | #define RGB_LED_ENABLE_GREEN 0x40 |
| 181 | #define RGB_LED_ENABLE_BLUE 0x20 |
| 182 | #define RGB_LED_SOURCE_VPH_PWR 0x01 |
| 183 | #define RGB_LED_ENABLE_MASK 0xE0 |
| 184 | #define RGB_LED_SRC_MASK 0x03 |
| 185 | #define QPNP_LED_PWM_FLAGS (PM_PWM_LUT_LOOP | PM_PWM_LUT_RAMP_UP) |
| 186 | #define QPNP_LUT_RAMP_STEP_DEFAULT 255 |
| 187 | #define PWM_LUT_MAX_SIZE 63 |
| 188 | #define PWM_GPLED_LUT_MAX_SIZE 31 |
| 189 | #define RGB_LED_DISABLE 0x00 |
| 190 | |
| 191 | #define MPP_MAX_LEVEL LED_FULL |
| 192 | #define LED_MPP_MODE_CTRL(base) (base + 0x40) |
| 193 | #define LED_MPP_VIN_CTRL(base) (base + 0x41) |
| 194 | #define LED_MPP_EN_CTRL(base) (base + 0x46) |
| 195 | #define LED_MPP_SINK_CTRL(base) (base + 0x4C) |
| 196 | |
| 197 | #define LED_MPP_CURRENT_MIN 5 |
| 198 | #define LED_MPP_CURRENT_MAX 40 |
| 199 | #define LED_MPP_VIN_CTRL_DEFAULT 0 |
| 200 | #define LED_MPP_CURRENT_PER_SETTING 5 |
| 201 | #define LED_MPP_SOURCE_SEL_DEFAULT LED_MPP_MODE_ENABLE |
| 202 | |
| 203 | #define LED_MPP_SINK_MASK 0x07 |
| 204 | #define LED_MPP_MODE_MASK 0x7F |
| 205 | #define LED_MPP_VIN_MASK 0x03 |
| 206 | #define LED_MPP_EN_MASK 0x80 |
| 207 | #define LED_MPP_SRC_MASK 0x0F |
| 208 | #define LED_MPP_MODE_CTRL_MASK 0x70 |
| 209 | |
| 210 | #define LED_MPP_MODE_SINK (0x06 << 4) |
| 211 | #define LED_MPP_MODE_ENABLE 0x01 |
| 212 | #define LED_MPP_MODE_OUTPUT 0x10 |
| 213 | #define LED_MPP_MODE_DISABLE 0x00 |
| 214 | #define LED_MPP_EN_ENABLE 0x80 |
| 215 | #define LED_MPP_EN_DISABLE 0x00 |
| 216 | |
| 217 | #define MPP_SOURCE_DTEST1 0x08 |
| 218 | |
| 219 | #define GPIO_MAX_LEVEL LED_FULL |
| 220 | #define LED_GPIO_MODE_CTRL(base) (base + 0x40) |
| 221 | #define LED_GPIO_VIN_CTRL(base) (base + 0x41) |
| 222 | #define LED_GPIO_EN_CTRL(base) (base + 0x46) |
| 223 | |
| 224 | #define LED_GPIO_VIN_CTRL_DEFAULT 0 |
| 225 | #define LED_GPIO_SOURCE_SEL_DEFAULT LED_GPIO_MODE_ENABLE |
| 226 | |
| 227 | #define LED_GPIO_MODE_MASK 0x3F |
| 228 | #define LED_GPIO_VIN_MASK 0x0F |
| 229 | #define LED_GPIO_EN_MASK 0x80 |
| 230 | #define LED_GPIO_SRC_MASK 0x0F |
| 231 | #define LED_GPIO_MODE_CTRL_MASK 0x30 |
| 232 | |
| 233 | #define LED_GPIO_MODE_ENABLE 0x01 |
| 234 | #define LED_GPIO_MODE_DISABLE 0x00 |
| 235 | #define LED_GPIO_MODE_OUTPUT 0x10 |
| 236 | #define LED_GPIO_EN_ENABLE 0x80 |
| 237 | #define LED_GPIO_EN_DISABLE 0x00 |
| 238 | |
| 239 | #define KPDBL_MAX_LEVEL LED_FULL |
| 240 | #define KPDBL_ROW_SRC_SEL(base) (base + 0x40) |
| 241 | #define KPDBL_ENABLE(base) (base + 0x46) |
| 242 | #define KPDBL_ROW_SRC(base) (base + 0xE5) |
| 243 | |
| 244 | #define KPDBL_ROW_SRC_SEL_VAL_MASK 0x0F |
| 245 | #define KPDBL_ROW_SCAN_EN_MASK 0x80 |
| 246 | #define KPDBL_ROW_SCAN_VAL_MASK 0x0F |
| 247 | #define KPDBL_ROW_SCAN_EN_SHIFT 7 |
| 248 | #define KPDBL_MODULE_EN 0x80 |
| 249 | #define KPDBL_MODULE_DIS 0x00 |
| 250 | #define KPDBL_MODULE_EN_MASK 0x80 |
| 251 | #define NUM_KPDBL_LEDS 4 |
| 252 | #define KPDBL_MASTER_BIT_INDEX 0 |
| 253 | |
| 254 | /** |
| 255 | * enum qpnp_leds - QPNP supported led ids |
| 256 | * @QPNP_ID_WLED - White led backlight |
| 257 | */ |
| 258 | enum qpnp_leds { |
| 259 | QPNP_ID_WLED = 0, |
| 260 | QPNP_ID_FLASH1_LED0, |
| 261 | QPNP_ID_FLASH1_LED1, |
| 262 | QPNP_ID_RGB_RED, |
| 263 | QPNP_ID_RGB_GREEN, |
| 264 | QPNP_ID_RGB_BLUE, |
| 265 | QPNP_ID_LED_MPP, |
| 266 | QPNP_ID_KPDBL, |
| 267 | QPNP_ID_LED_GPIO, |
| 268 | QPNP_ID_MAX, |
| 269 | }; |
| 270 | |
| 271 | /* current boost limit */ |
| 272 | enum wled_current_boost_limit { |
| 273 | WLED_CURR_LIMIT_105mA, |
| 274 | WLED_CURR_LIMIT_385mA, |
| 275 | WLED_CURR_LIMIT_525mA, |
| 276 | WLED_CURR_LIMIT_805mA, |
| 277 | WLED_CURR_LIMIT_980mA, |
| 278 | WLED_CURR_LIMIT_1260mA, |
| 279 | WLED_CURR_LIMIT_1400mA, |
| 280 | WLED_CURR_LIMIT_1680mA, |
| 281 | }; |
| 282 | |
| 283 | /* over voltage protection threshold */ |
| 284 | enum wled_ovp_threshold { |
| 285 | WLED_OVP_35V, |
| 286 | WLED_OVP_32V, |
| 287 | WLED_OVP_29V, |
| 288 | WLED_OVP_27V, |
| 289 | }; |
| 290 | |
| 291 | enum flash_headroom { |
| 292 | HEADROOM_250mV = 0, |
| 293 | HEADROOM_300mV, |
| 294 | HEADROOM_400mV, |
| 295 | HEADROOM_500mV, |
| 296 | }; |
| 297 | |
| 298 | enum flash_startup_dly { |
| 299 | DELAY_10us = 0, |
| 300 | DELAY_32us, |
| 301 | DELAY_64us, |
| 302 | DELAY_128us, |
| 303 | }; |
| 304 | |
| 305 | enum led_mode { |
| 306 | PWM_MODE = 0, |
| 307 | LPG_MODE, |
| 308 | MANUAL_MODE, |
| 309 | }; |
| 310 | |
| 311 | static u8 wled_debug_regs[] = { |
| 312 | /* brightness registers */ |
| 313 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, |
| 314 | /* common registers */ |
| 315 | 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 316 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, |
| 317 | /* LED1 */ |
| 318 | 0x60, 0x61, 0x62, 0x63, 0x66, |
| 319 | /* LED2 */ |
| 320 | 0x70, 0x71, 0x72, 0x73, 0x76, |
| 321 | /* LED3 */ |
| 322 | 0x80, 0x81, 0x82, 0x83, 0x86, |
| 323 | }; |
| 324 | |
| 325 | static u8 flash_debug_regs[] = { |
| 326 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x48, 0x49, 0x4b, 0x4c, |
| 327 | 0x4f, 0x46, 0x47, |
| 328 | }; |
| 329 | |
| 330 | static u8 rgb_pwm_debug_regs[] = { |
| 331 | 0x45, 0x46, 0x47, |
| 332 | }; |
| 333 | |
| 334 | static u8 mpp_debug_regs[] = { |
| 335 | 0x40, 0x41, 0x42, 0x45, 0x46, 0x4c, |
| 336 | }; |
| 337 | |
| 338 | static u8 kpdbl_debug_regs[] = { |
| 339 | 0x40, 0x46, 0xb1, 0xb3, 0xb4, 0xe5, |
| 340 | }; |
| 341 | |
| 342 | static u8 gpio_debug_regs[] = { |
| 343 | 0x40, 0x41, 0x42, 0x45, 0x46, |
| 344 | }; |
| 345 | |
| 346 | /** |
| 347 | * pwm_config_data - pwm configuration data |
| 348 | * @lut_params - lut parameters to be used by pwm driver |
| 349 | * @pwm_device - pwm device |
| 350 | * @pwm_period_us - period for pwm, in us |
| 351 | * @mode - mode the led operates in |
| 352 | * @old_duty_pcts - storage for duty pcts that may need to be reused |
| 353 | * @default_mode - default mode of LED as set in device tree |
| 354 | * @use_blink - use blink sysfs entry |
| 355 | * @blinking - device is currently blinking w/LPG mode |
| 356 | */ |
| 357 | struct pwm_config_data { |
| 358 | struct lut_params lut_params; |
| 359 | struct pwm_device *pwm_dev; |
| 360 | u32 pwm_period_us; |
| 361 | struct pwm_duty_cycles *duty_cycles; |
| 362 | int *old_duty_pcts; |
| 363 | u8 mode; |
| 364 | u8 default_mode; |
| 365 | bool pwm_enabled; |
| 366 | bool use_blink; |
| 367 | bool blinking; |
| 368 | }; |
| 369 | |
| 370 | /** |
| 371 | * wled_config_data - wled configuration data |
| 372 | * @num_strings - number of wled strings to be configured |
| 373 | * @num_physical_strings - physical number of strings supported |
| 374 | * @ovp_val - over voltage protection threshold |
| 375 | * @boost_curr_lim - boot current limit |
| 376 | * @cp_select - high pole capacitance |
| 377 | * @ctrl_delay_us - delay in activation of led |
| 378 | * @dig_mod_gen_en - digital module generator |
| 379 | * @cs_out_en - current sink output enable |
| 380 | * @op_fdbck - selection of output as feedback for the boost |
| 381 | */ |
| 382 | struct wled_config_data { |
| 383 | u8 num_strings; |
| 384 | u8 num_physical_strings; |
| 385 | u8 ovp_val; |
| 386 | u8 boost_curr_lim; |
| 387 | u8 cp_select; |
| 388 | u8 ctrl_delay_us; |
| 389 | u8 switch_freq; |
| 390 | u8 op_fdbck; |
| 391 | u8 pmic_version; |
| 392 | bool dig_mod_gen_en; |
| 393 | bool cs_out_en; |
| 394 | }; |
| 395 | |
| 396 | /** |
| 397 | * mpp_config_data - mpp configuration data |
| 398 | * @pwm_cfg - device pwm configuration |
| 399 | * @current_setting - current setting, 5ma-40ma in 5ma increments |
| 400 | * @source_sel - source selection |
| 401 | * @mode_ctrl - mode control |
| 402 | * @vin_ctrl - input control |
| 403 | * @min_brightness - minimum brightness supported |
| 404 | * @pwm_mode - pwm mode in use |
| 405 | * @max_uV - maximum regulator voltage |
| 406 | * @min_uV - minimum regulator voltage |
| 407 | * @mpp_reg - regulator to power mpp based LED |
| 408 | * @enable - flag indicating LED on or off |
| 409 | */ |
| 410 | struct mpp_config_data { |
| 411 | struct pwm_config_data *pwm_cfg; |
| 412 | u8 current_setting; |
| 413 | u8 source_sel; |
| 414 | u8 mode_ctrl; |
| 415 | u8 vin_ctrl; |
| 416 | u8 min_brightness; |
| 417 | u8 pwm_mode; |
| 418 | u32 max_uV; |
| 419 | u32 min_uV; |
| 420 | struct regulator *mpp_reg; |
| 421 | bool enable; |
| 422 | }; |
| 423 | |
| 424 | /** |
| 425 | * flash_config_data - flash configuration data |
| 426 | * @current_prgm - current to be programmed, scaled by max level |
| 427 | * @clamp_curr - clamp current to use |
| 428 | * @headroom - headroom value to use |
| 429 | * @duration - duration of the flash |
| 430 | * @enable_module - enable address for particular flash |
| 431 | * @trigger_flash - trigger flash |
| 432 | * @startup_dly - startup delay for flash |
| 433 | * @strobe_type - select between sw and hw strobe |
| 434 | * @peripheral_subtype - module peripheral subtype |
| 435 | * @current_addr - address to write for current |
| 436 | * @second_addr - address of secondary flash to be written |
| 437 | * @safety_timer - enable safety timer or watchdog timer |
| 438 | * @torch_enable - enable flash LED torch mode |
| 439 | * @flash_reg_get - flash regulator attached or not |
| 440 | * @flash_wa_reg_get - workaround regulator attached or not |
| 441 | * @flash_on - flash status, on or off |
| 442 | * @torch_on - torch status, on or off |
| 443 | * @vreg_ok - specifies strobe type, sw or hw |
| 444 | * @no_smbb_support - specifies if smbb boost is not required and there is a |
| 445 | single regulator for both flash and torch |
| 446 | * @flash_boost_reg - boost regulator for flash |
| 447 | * @torch_boost_reg - boost regulator for torch |
| 448 | * @flash_wa_reg - flash regulator for wa |
| 449 | */ |
| 450 | struct flash_config_data { |
| 451 | u8 current_prgm; |
| 452 | u8 clamp_curr; |
| 453 | u8 headroom; |
| 454 | u8 duration; |
| 455 | u8 enable_module; |
| 456 | u8 trigger_flash; |
| 457 | u8 startup_dly; |
| 458 | u8 strobe_type; |
| 459 | u8 peripheral_subtype; |
| 460 | u16 current_addr; |
| 461 | u16 second_addr; |
| 462 | bool safety_timer; |
| 463 | bool torch_enable; |
| 464 | bool flash_reg_get; |
| 465 | bool flash_wa_reg_get; |
| 466 | bool flash_on; |
| 467 | bool torch_on; |
| 468 | bool vreg_ok; |
| 469 | bool no_smbb_support; |
| 470 | struct regulator *flash_boost_reg; |
| 471 | struct regulator *torch_boost_reg; |
| 472 | struct regulator *flash_wa_reg; |
| 473 | }; |
| 474 | |
| 475 | /** |
| 476 | * kpdbl_config_data - kpdbl configuration data |
| 477 | * @pwm_cfg - device pwm configuration |
| 478 | * @mode - running mode: pwm or lut |
| 479 | * @row_id - row id of the led |
| 480 | * @row_src_vbst - 0 for vph_pwr and 1 for vbst |
| 481 | * @row_src_en - enable row source |
| 482 | * @always_on - always on row |
| 483 | * @lut_params - lut parameters to be used by pwm driver |
| 484 | * @duty_cycles - duty cycles for lut |
| 485 | * @pwm_mode - pwm mode in use |
| 486 | */ |
| 487 | struct kpdbl_config_data { |
| 488 | struct pwm_config_data *pwm_cfg; |
| 489 | u32 row_id; |
| 490 | bool row_src_vbst; |
| 491 | bool row_src_en; |
| 492 | bool always_on; |
| 493 | struct pwm_duty_cycles *duty_cycles; |
| 494 | struct lut_params lut_params; |
| 495 | u8 pwm_mode; |
| 496 | }; |
| 497 | |
| 498 | /** |
| 499 | * rgb_config_data - rgb configuration data |
| 500 | * @pwm_cfg - device pwm configuration |
| 501 | * @enable - bits to enable led |
| 502 | */ |
| 503 | struct rgb_config_data { |
| 504 | struct pwm_config_data *pwm_cfg; |
| 505 | u8 enable; |
| 506 | }; |
| 507 | |
| 508 | /** |
| 509 | * gpio_config_data - gpio configuration data |
| 510 | * @source_sel - source selection |
| 511 | * @mode_ctrl - mode control |
| 512 | * @vin_ctrl - input control |
| 513 | * @enable - flag indicating LED on or off |
| 514 | */ |
| 515 | struct gpio_config_data { |
| 516 | u8 source_sel; |
| 517 | u8 mode_ctrl; |
| 518 | u8 vin_ctrl; |
| 519 | bool enable; |
| 520 | }; |
| 521 | |
| 522 | /** |
| 523 | * struct qpnp_led_data - internal led data structure |
| 524 | * @led_classdev - led class device |
| 525 | * @delayed_work - delayed work for turning off the LED |
| 526 | * @workqueue - dedicated workqueue to handle concurrency |
| 527 | * @work - workqueue for led |
| 528 | * @id - led index |
| 529 | * @base_reg - base register given in device tree |
| 530 | * @lock - to protect the transactions |
| 531 | * @reg - cached value of led register |
| 532 | * @num_leds - number of leds in the module |
| 533 | * @max_current - maximum current supported by LED |
| 534 | * @default_on - true: default state max, false, default state 0 |
| 535 | * @turn_off_delay_ms - number of msec before turning off the LED |
| 536 | */ |
| 537 | struct qpnp_led_data { |
| 538 | struct led_classdev cdev; |
| 539 | struct platform_device *pdev; |
| 540 | struct regmap *regmap; |
| 541 | struct delayed_work dwork; |
| 542 | struct workqueue_struct *workqueue; |
| 543 | struct work_struct work; |
| 544 | int id; |
| 545 | u16 base; |
| 546 | u8 reg; |
| 547 | u8 num_leds; |
| 548 | struct mutex lock; |
| 549 | struct wled_config_data *wled_cfg; |
| 550 | struct flash_config_data *flash_cfg; |
| 551 | struct kpdbl_config_data *kpdbl_cfg; |
| 552 | struct rgb_config_data *rgb_cfg; |
| 553 | struct mpp_config_data *mpp_cfg; |
| 554 | struct gpio_config_data *gpio_cfg; |
| 555 | int max_current; |
| 556 | bool default_on; |
| 557 | bool in_order_command_processing; |
| 558 | int turn_off_delay_ms; |
| 559 | }; |
| 560 | |
| 561 | static DEFINE_MUTEX(flash_lock); |
| 562 | static struct pwm_device *kpdbl_master; |
| 563 | static u32 kpdbl_master_period_us; |
| 564 | DECLARE_BITMAP(kpdbl_leds_in_use, NUM_KPDBL_LEDS); |
| 565 | static bool is_kpdbl_master_turn_on; |
| 566 | |
| 567 | static int |
| 568 | qpnp_led_masked_write(struct qpnp_led_data *led, u16 addr, u8 mask, u8 val) |
| 569 | { |
| 570 | int rc; |
| 571 | |
| 572 | rc = regmap_update_bits(led->regmap, addr, mask, val); |
| 573 | if (rc) |
| 574 | dev_err(&led->pdev->dev, |
| 575 | "Unable to regmap_update_bits to addr=%x, rc(%d)\n", |
| 576 | addr, rc); |
| 577 | return rc; |
| 578 | } |
| 579 | |
| 580 | static void qpnp_dump_regs(struct qpnp_led_data *led, u8 regs[], u8 array_size) |
| 581 | { |
| 582 | int i; |
| 583 | u8 val; |
| 584 | |
| 585 | pr_debug("===== %s LED register dump start =====\n", led->cdev.name); |
| 586 | for (i = 0; i < array_size; i++) { |
| 587 | regmap_bulk_read(led->regmap, led->base + regs[i], &val, |
| 588 | sizeof(val)); |
| 589 | pr_debug("%s: 0x%x = 0x%x\n", led->cdev.name, |
| 590 | led->base + regs[i], val); |
| 591 | } |
| 592 | pr_debug("===== %s LED register dump end =====\n", led->cdev.name); |
| 593 | } |
| 594 | |
| 595 | static int qpnp_wled_sync(struct qpnp_led_data *led) |
| 596 | { |
| 597 | int rc; |
| 598 | u8 val; |
| 599 | |
| 600 | /* sync */ |
| 601 | val = WLED_SYNC_VAL; |
| 602 | rc = regmap_write(led->regmap, WLED_SYNC_REG(led->base), val); |
| 603 | if (rc) { |
| 604 | dev_err(&led->pdev->dev, |
| 605 | "WLED set sync reg failed(%d)\n", rc); |
| 606 | return rc; |
| 607 | } |
| 608 | |
| 609 | val = WLED_SYNC_RESET_VAL; |
| 610 | rc = regmap_write(led->regmap, WLED_SYNC_REG(led->base), val); |
| 611 | if (rc) { |
| 612 | dev_err(&led->pdev->dev, |
| 613 | "WLED reset sync reg failed(%d)\n", rc); |
| 614 | return rc; |
| 615 | } |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int qpnp_wled_set(struct qpnp_led_data *led) |
| 620 | { |
| 621 | int rc, duty, level, tries = 0; |
| 622 | u8 val, i, num_wled_strings; |
| 623 | uint sink_val, ilim_val, ovp_val; |
| 624 | |
| 625 | num_wled_strings = led->wled_cfg->num_strings; |
| 626 | |
| 627 | level = led->cdev.brightness; |
| 628 | |
| 629 | if (level > WLED_MAX_LEVEL) |
| 630 | level = WLED_MAX_LEVEL; |
| 631 | if (level == 0) { |
| 632 | for (i = 0; i < num_wled_strings; i++) { |
| 633 | rc = qpnp_led_masked_write(led, |
| 634 | WLED_FULL_SCALE_REG(led->base, i), |
| 635 | WLED_MAX_CURR_MASK, WLED_NO_CURRENT); |
| 636 | if (rc) { |
| 637 | dev_err(&led->pdev->dev, |
| 638 | "Write max current failure (%d)\n", |
| 639 | rc); |
| 640 | return rc; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | rc = qpnp_wled_sync(led); |
| 645 | if (rc) { |
| 646 | dev_err(&led->pdev->dev, |
| 647 | "WLED sync failed(%d)\n", rc); |
| 648 | return rc; |
| 649 | } |
| 650 | |
| 651 | rc = regmap_read(led->regmap, WLED_CURR_SINK_REG(led->base), |
| 652 | &sink_val); |
| 653 | if (rc) { |
| 654 | dev_err(&led->pdev->dev, |
| 655 | "WLED read sink reg failed(%d)\n", rc); |
| 656 | return rc; |
| 657 | } |
| 658 | |
| 659 | if (led->wled_cfg->pmic_version == PMIC_VER_8026) { |
| 660 | val = WLED_DISABLE_ALL_SINKS; |
| 661 | rc = regmap_write(led->regmap, |
| 662 | WLED_CURR_SINK_REG(led->base), val); |
| 663 | if (rc) { |
| 664 | dev_err(&led->pdev->dev, |
| 665 | "WLED write sink reg failed(%d)\n", rc); |
| 666 | return rc; |
| 667 | } |
| 668 | |
| 669 | usleep_range(WLED_OVP_DELAY, WLED_OVP_DELAY + 10); |
| 670 | } else if (led->wled_cfg->pmic_version == PMIC_VER_8941) { |
| 671 | if (led->wled_cfg->num_physical_strings <= |
| 672 | WLED_THREE_STRINGS) { |
| 673 | val = WLED_DISABLE_1_2_SINKS; |
| 674 | rc = regmap_write(led->regmap, |
| 675 | WLED_CURR_SINK_REG(led->base), |
| 676 | val); |
| 677 | if (rc) { |
| 678 | dev_err(&led->pdev->dev, |
| 679 | "WLED write sink reg failed"); |
| 680 | return rc; |
| 681 | } |
| 682 | |
| 683 | rc = regmap_read(led->regmap, |
| 684 | WLED_BOOST_LIMIT_REG(led->base), |
| 685 | &ilim_val); |
| 686 | if (rc) { |
| 687 | dev_err(&led->pdev->dev, |
| 688 | "Unable to read boost reg"); |
| 689 | } |
| 690 | val = WLED_SET_ILIM_CODE; |
| 691 | rc = regmap_write(led->regmap, |
| 692 | WLED_BOOST_LIMIT_REG(led->base), |
| 693 | val); |
| 694 | if (rc) { |
| 695 | dev_err(&led->pdev->dev, |
| 696 | "WLED write sink reg failed"); |
| 697 | return rc; |
| 698 | } |
| 699 | usleep_range(WLED_OVP_DELAY, |
| 700 | WLED_OVP_DELAY + 10); |
| 701 | } else { |
| 702 | val = WLED_DISABLE_ALL_SINKS; |
| 703 | rc = regmap_write(led->regmap, |
| 704 | WLED_CURR_SINK_REG(led->base), |
| 705 | val); |
| 706 | if (rc) { |
| 707 | dev_err(&led->pdev->dev, |
| 708 | "WLED write sink reg failed"); |
| 709 | return rc; |
| 710 | } |
| 711 | |
| 712 | msleep(WLED_OVP_DELAY_INT); |
| 713 | while (tries < WLED_MAX_TRIES) { |
| 714 | rc = regmap_read(led->regmap, |
| 715 | WLED_OVP_INT_STATUS(led->base), |
| 716 | &ovp_val); |
| 717 | if (rc) { |
| 718 | dev_err(&led->pdev->dev, |
| 719 | "Unable to read boost reg"); |
| 720 | } |
| 721 | |
| 722 | if (ovp_val & WLED_OVP_INT_MASK) |
| 723 | break; |
| 724 | |
| 725 | msleep(WLED_OVP_DELAY_LOOP); |
| 726 | tries++; |
| 727 | } |
| 728 | usleep_range(WLED_OVP_DELAY, |
| 729 | WLED_OVP_DELAY + 10); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | val = WLED_BOOST_OFF; |
| 734 | rc = regmap_write(led->regmap, WLED_MOD_CTRL_REG(led->base), |
| 735 | val); |
| 736 | if (rc) { |
| 737 | dev_err(&led->pdev->dev, |
| 738 | "WLED write ctrl reg failed(%d)\n", rc); |
| 739 | return rc; |
| 740 | } |
| 741 | |
| 742 | for (i = 0; i < num_wled_strings; i++) { |
| 743 | rc = qpnp_led_masked_write(led, |
| 744 | WLED_FULL_SCALE_REG(led->base, i), |
| 745 | WLED_MAX_CURR_MASK, (u8)led->max_current); |
| 746 | if (rc) { |
| 747 | dev_err(&led->pdev->dev, |
| 748 | "Write max current failure (%d)\n", |
| 749 | rc); |
| 750 | return rc; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | rc = qpnp_wled_sync(led); |
| 755 | if (rc) { |
| 756 | dev_err(&led->pdev->dev, |
| 757 | "WLED sync failed(%d)\n", rc); |
| 758 | return rc; |
| 759 | } |
| 760 | |
| 761 | if (led->wled_cfg->pmic_version == PMIC_VER_8941) { |
| 762 | if (led->wled_cfg->num_physical_strings <= |
| 763 | WLED_THREE_STRINGS) { |
| 764 | rc = regmap_write(led->regmap, |
| 765 | WLED_BOOST_LIMIT_REG(led->base), |
| 766 | ilim_val); |
| 767 | if (rc) { |
| 768 | dev_err(&led->pdev->dev, |
| 769 | "WLED write sink reg failed"); |
| 770 | return rc; |
| 771 | } |
| 772 | } else { |
| 773 | /* restore OVP to original value */ |
| 774 | rc = regmap_write(led->regmap, |
| 775 | WLED_OVP_CFG_REG(led->base), |
| 776 | *&led->wled_cfg->ovp_val); |
| 777 | if (rc) { |
| 778 | dev_err(&led->pdev->dev, |
| 779 | "WLED write sink reg failed"); |
| 780 | return rc; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* re-enable all sinks */ |
| 786 | rc = regmap_write(led->regmap, WLED_CURR_SINK_REG(led->base), |
| 787 | sink_val); |
| 788 | if (rc) { |
| 789 | dev_err(&led->pdev->dev, |
| 790 | "WLED write sink reg failed(%d)\n", rc); |
| 791 | return rc; |
| 792 | } |
| 793 | |
| 794 | } else { |
| 795 | val = WLED_BOOST_ON; |
| 796 | rc = regmap_write(led->regmap, WLED_MOD_CTRL_REG(led->base), |
| 797 | val); |
| 798 | if (rc) { |
| 799 | dev_err(&led->pdev->dev, |
| 800 | "WLED write ctrl reg failed(%d)\n", rc); |
| 801 | return rc; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | duty = (WLED_MAX_DUTY_CYCLE * level) / WLED_MAX_LEVEL; |
| 806 | |
| 807 | /* program brightness control registers */ |
| 808 | for (i = 0; i < num_wled_strings; i++) { |
| 809 | rc = qpnp_led_masked_write(led, |
| 810 | WLED_BRIGHTNESS_CNTL_MSB(led->base, i), WLED_MSB_MASK, |
| 811 | (duty >> WLED_8_BIT_SHFT) & WLED_4_BIT_MASK); |
| 812 | if (rc) { |
| 813 | dev_err(&led->pdev->dev, |
| 814 | "WLED set brightness MSB failed(%d)\n", rc); |
| 815 | return rc; |
| 816 | } |
| 817 | val = duty & WLED_8_BIT_MASK; |
| 818 | rc = regmap_write(led->regmap, |
| 819 | WLED_BRIGHTNESS_CNTL_LSB(led->base, i), val); |
| 820 | if (rc) { |
| 821 | dev_err(&led->pdev->dev, |
| 822 | "WLED set brightness LSB failed(%d)\n", rc); |
| 823 | return rc; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | rc = qpnp_wled_sync(led); |
| 828 | if (rc) { |
| 829 | dev_err(&led->pdev->dev, "WLED sync failed(%d)\n", rc); |
| 830 | return rc; |
| 831 | } |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static int qpnp_mpp_set(struct qpnp_led_data *led) |
| 836 | { |
| 837 | int rc; |
| 838 | u8 val; |
| 839 | int duty_us, duty_ns, period_us; |
| 840 | |
| 841 | if (led->cdev.brightness) { |
| 842 | if (led->mpp_cfg->mpp_reg && !led->mpp_cfg->enable) { |
| 843 | rc = regulator_set_voltage(led->mpp_cfg->mpp_reg, |
| 844 | led->mpp_cfg->min_uV, |
| 845 | led->mpp_cfg->max_uV); |
| 846 | if (rc) { |
| 847 | dev_err(&led->pdev->dev, |
| 848 | "Regulator voltage set failed rc=%d\n", |
| 849 | rc); |
| 850 | return rc; |
| 851 | } |
| 852 | |
| 853 | rc = regulator_enable(led->mpp_cfg->mpp_reg); |
| 854 | if (rc) { |
| 855 | dev_err(&led->pdev->dev, |
| 856 | "Regulator enable failed(%d)\n", rc); |
| 857 | goto err_reg_enable; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | led->mpp_cfg->enable = true; |
| 862 | |
| 863 | if (led->cdev.brightness < led->mpp_cfg->min_brightness) { |
| 864 | dev_warn(&led->pdev->dev, "brightness is less than supported, set to minimum supported\n"); |
| 865 | led->cdev.brightness = led->mpp_cfg->min_brightness; |
| 866 | } |
| 867 | |
| 868 | if (led->mpp_cfg->pwm_mode != MANUAL_MODE) { |
| 869 | if (!led->mpp_cfg->pwm_cfg->blinking) { |
| 870 | led->mpp_cfg->pwm_cfg->mode = |
| 871 | led->mpp_cfg->pwm_cfg->default_mode; |
| 872 | led->mpp_cfg->pwm_mode = |
| 873 | led->mpp_cfg->pwm_cfg->default_mode; |
| 874 | } |
| 875 | } |
| 876 | if (led->mpp_cfg->pwm_mode == PWM_MODE) { |
| 877 | /*config pwm for brightness scaling*/ |
| 878 | period_us = led->mpp_cfg->pwm_cfg->pwm_period_us; |
| 879 | if (period_us > INT_MAX / NSEC_PER_USEC) { |
| 880 | duty_us = (period_us * led->cdev.brightness) / |
| 881 | LED_FULL; |
| 882 | rc = pwm_config_us( |
| 883 | led->mpp_cfg->pwm_cfg->pwm_dev, |
| 884 | duty_us, |
| 885 | period_us); |
| 886 | } else { |
| 887 | duty_ns = ((period_us * NSEC_PER_USEC) / |
| 888 | LED_FULL) * led->cdev.brightness; |
| 889 | rc = pwm_config( |
| 890 | led->mpp_cfg->pwm_cfg->pwm_dev, |
| 891 | duty_ns, |
| 892 | period_us * NSEC_PER_USEC); |
| 893 | } |
| 894 | if (rc < 0) { |
| 895 | dev_err(&led->pdev->dev, "Failed to configure pwm for new values\n"); |
| 896 | goto err_mpp_reg_write; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | if (led->mpp_cfg->pwm_mode != MANUAL_MODE) |
| 901 | pwm_enable(led->mpp_cfg->pwm_cfg->pwm_dev); |
| 902 | else { |
| 903 | if (led->cdev.brightness < LED_MPP_CURRENT_MIN) |
| 904 | led->cdev.brightness = LED_MPP_CURRENT_MIN; |
| 905 | else { |
| 906 | /* |
| 907 | * PMIC supports LED intensity from 5mA - 40mA |
| 908 | * in steps of 5mA. Brightness is rounded to |
| 909 | * 5mA or nearest lower supported values |
| 910 | */ |
| 911 | led->cdev.brightness /= LED_MPP_CURRENT_MIN; |
| 912 | led->cdev.brightness *= LED_MPP_CURRENT_MIN; |
| 913 | } |
| 914 | |
| 915 | val = (led->cdev.brightness / LED_MPP_CURRENT_MIN) - 1; |
| 916 | |
| 917 | rc = qpnp_led_masked_write(led, |
| 918 | LED_MPP_SINK_CTRL(led->base), |
| 919 | LED_MPP_SINK_MASK, val); |
| 920 | if (rc) { |
| 921 | dev_err(&led->pdev->dev, |
| 922 | "Failed to write sink control reg\n"); |
| 923 | goto err_mpp_reg_write; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | val = (led->mpp_cfg->source_sel & LED_MPP_SRC_MASK) | |
| 928 | (led->mpp_cfg->mode_ctrl & LED_MPP_MODE_CTRL_MASK); |
| 929 | |
| 930 | rc = qpnp_led_masked_write(led, |
| 931 | LED_MPP_MODE_CTRL(led->base), LED_MPP_MODE_MASK, |
| 932 | val); |
| 933 | if (rc) { |
| 934 | dev_err(&led->pdev->dev, |
| 935 | "Failed to write led mode reg\n"); |
| 936 | goto err_mpp_reg_write; |
| 937 | } |
| 938 | |
| 939 | rc = qpnp_led_masked_write(led, |
| 940 | LED_MPP_EN_CTRL(led->base), LED_MPP_EN_MASK, |
| 941 | LED_MPP_EN_ENABLE); |
| 942 | if (rc) { |
| 943 | dev_err(&led->pdev->dev, "Failed to write led enable reg\n"); |
| 944 | goto err_mpp_reg_write; |
| 945 | } |
| 946 | } else { |
| 947 | if (led->mpp_cfg->pwm_mode != MANUAL_MODE) { |
| 948 | led->mpp_cfg->pwm_cfg->mode = |
| 949 | led->mpp_cfg->pwm_cfg->default_mode; |
| 950 | led->mpp_cfg->pwm_mode = |
| 951 | led->mpp_cfg->pwm_cfg->default_mode; |
| 952 | pwm_disable(led->mpp_cfg->pwm_cfg->pwm_dev); |
| 953 | } |
| 954 | rc = qpnp_led_masked_write(led, |
| 955 | LED_MPP_MODE_CTRL(led->base), |
| 956 | LED_MPP_MODE_MASK, |
| 957 | LED_MPP_MODE_DISABLE); |
| 958 | if (rc) { |
| 959 | dev_err(&led->pdev->dev, |
| 960 | "Failed to write led mode reg\n"); |
| 961 | goto err_mpp_reg_write; |
| 962 | } |
| 963 | |
| 964 | rc = qpnp_led_masked_write(led, |
| 965 | LED_MPP_EN_CTRL(led->base), |
| 966 | LED_MPP_EN_MASK, |
| 967 | LED_MPP_EN_DISABLE); |
| 968 | if (rc) { |
| 969 | dev_err(&led->pdev->dev, |
| 970 | "Failed to write led enable reg\n"); |
| 971 | goto err_mpp_reg_write; |
| 972 | } |
| 973 | |
| 974 | if (led->mpp_cfg->mpp_reg && led->mpp_cfg->enable) { |
| 975 | rc = regulator_disable(led->mpp_cfg->mpp_reg); |
| 976 | if (rc) { |
| 977 | dev_err(&led->pdev->dev, |
| 978 | "MPP regulator disable failed(%d)\n", |
| 979 | rc); |
| 980 | return rc; |
| 981 | } |
| 982 | |
| 983 | rc = regulator_set_voltage(led->mpp_cfg->mpp_reg, |
| 984 | 0, led->mpp_cfg->max_uV); |
| 985 | if (rc) { |
| 986 | dev_err(&led->pdev->dev, |
| 987 | "MPP regulator voltage set failed(%d)\n", |
| 988 | rc); |
| 989 | return rc; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | led->mpp_cfg->enable = false; |
| 994 | } |
| 995 | |
| 996 | if (led->mpp_cfg->pwm_mode != MANUAL_MODE) |
| 997 | led->mpp_cfg->pwm_cfg->blinking = false; |
| 998 | qpnp_dump_regs(led, mpp_debug_regs, ARRAY_SIZE(mpp_debug_regs)); |
| 999 | |
| 1000 | return 0; |
| 1001 | |
| 1002 | err_mpp_reg_write: |
| 1003 | if (led->mpp_cfg->mpp_reg) |
| 1004 | regulator_disable(led->mpp_cfg->mpp_reg); |
| 1005 | err_reg_enable: |
| 1006 | if (led->mpp_cfg->mpp_reg) |
| 1007 | regulator_set_voltage(led->mpp_cfg->mpp_reg, 0, |
| 1008 | led->mpp_cfg->max_uV); |
| 1009 | led->mpp_cfg->enable = false; |
| 1010 | |
| 1011 | return rc; |
| 1012 | } |
| 1013 | |
| 1014 | static int qpnp_gpio_set(struct qpnp_led_data *led) |
| 1015 | { |
| 1016 | int rc, val; |
| 1017 | |
| 1018 | if (led->cdev.brightness) { |
| 1019 | val = (led->gpio_cfg->source_sel & LED_GPIO_SRC_MASK) | |
| 1020 | (led->gpio_cfg->mode_ctrl & LED_GPIO_MODE_CTRL_MASK); |
| 1021 | |
| 1022 | rc = qpnp_led_masked_write(led, |
| 1023 | LED_GPIO_MODE_CTRL(led->base), |
| 1024 | LED_GPIO_MODE_MASK, |
| 1025 | val); |
| 1026 | if (rc) { |
| 1027 | dev_err(&led->pdev->dev, |
| 1028 | "Failed to write led mode reg\n"); |
| 1029 | goto err_gpio_reg_write; |
| 1030 | } |
| 1031 | |
| 1032 | rc = qpnp_led_masked_write(led, |
| 1033 | LED_GPIO_EN_CTRL(led->base), |
| 1034 | LED_GPIO_EN_MASK, |
| 1035 | LED_GPIO_EN_ENABLE); |
| 1036 | if (rc) { |
| 1037 | dev_err(&led->pdev->dev, |
| 1038 | "Failed to write led enable reg\n"); |
| 1039 | goto err_gpio_reg_write; |
| 1040 | } |
| 1041 | |
| 1042 | led->gpio_cfg->enable = true; |
| 1043 | } else { |
| 1044 | rc = qpnp_led_masked_write(led, |
| 1045 | LED_GPIO_MODE_CTRL(led->base), |
| 1046 | LED_GPIO_MODE_MASK, |
| 1047 | LED_GPIO_MODE_DISABLE); |
| 1048 | if (rc) { |
| 1049 | dev_err(&led->pdev->dev, |
| 1050 | "Failed to write led mode reg\n"); |
| 1051 | goto err_gpio_reg_write; |
| 1052 | } |
| 1053 | |
| 1054 | rc = qpnp_led_masked_write(led, |
| 1055 | LED_GPIO_EN_CTRL(led->base), |
| 1056 | LED_GPIO_EN_MASK, |
| 1057 | LED_GPIO_EN_DISABLE); |
| 1058 | if (rc) { |
| 1059 | dev_err(&led->pdev->dev, |
| 1060 | "Failed to write led enable reg\n"); |
| 1061 | goto err_gpio_reg_write; |
| 1062 | } |
| 1063 | |
| 1064 | led->gpio_cfg->enable = false; |
| 1065 | } |
| 1066 | |
| 1067 | qpnp_dump_regs(led, gpio_debug_regs, ARRAY_SIZE(gpio_debug_regs)); |
| 1068 | |
| 1069 | return 0; |
| 1070 | |
| 1071 | err_gpio_reg_write: |
| 1072 | led->gpio_cfg->enable = false; |
| 1073 | |
| 1074 | return rc; |
| 1075 | } |
| 1076 | |
| 1077 | static int qpnp_flash_regulator_operate(struct qpnp_led_data *led, bool on) |
| 1078 | { |
| 1079 | int rc, i; |
| 1080 | struct qpnp_led_data *led_array; |
| 1081 | bool regulator_on = false; |
| 1082 | |
| 1083 | led_array = dev_get_drvdata(&led->pdev->dev); |
| 1084 | if (!led_array) { |
| 1085 | dev_err(&led->pdev->dev, "Unable to get LED array\n"); |
| 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | |
| 1089 | for (i = 0; i < led->num_leds; i++) |
| 1090 | regulator_on |= led_array[i].flash_cfg->flash_on; |
| 1091 | |
| 1092 | if (!on) |
| 1093 | goto regulator_turn_off; |
| 1094 | |
| 1095 | if (!regulator_on && !led->flash_cfg->flash_on) { |
| 1096 | for (i = 0; i < led->num_leds; i++) { |
| 1097 | if (led_array[i].flash_cfg->flash_reg_get) { |
| 1098 | if (led_array[i].flash_cfg->flash_wa_reg_get) { |
| 1099 | rc = regulator_enable( |
| 1100 | led_array[i].flash_cfg-> |
| 1101 | flash_wa_reg); |
| 1102 | if (rc) { |
| 1103 | dev_err(&led->pdev->dev, "Flash wa regulator enable failed(%d)\n", |
| 1104 | rc); |
| 1105 | return rc; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | rc = regulator_enable( |
| 1110 | led_array[i].flash_cfg->flash_boost_reg); |
| 1111 | if (rc) { |
| 1112 | if (led_array[i].flash_cfg-> |
| 1113 | flash_wa_reg_get) |
| 1114 | /* |
| 1115 | * Disable flash wa regulator |
| 1116 | * when flash boost regulator |
| 1117 | * enable fails |
| 1118 | */ |
| 1119 | regulator_disable( |
| 1120 | led_array[i].flash_cfg-> |
| 1121 | flash_wa_reg); |
| 1122 | dev_err(&led->pdev->dev, "Flash boost regulator enable failed(%d)\n", |
| 1123 | rc); |
| 1124 | return rc; |
| 1125 | } |
| 1126 | led->flash_cfg->flash_on = true; |
| 1127 | } |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | |
| 1134 | regulator_turn_off: |
| 1135 | if (regulator_on && led->flash_cfg->flash_on) { |
| 1136 | for (i = 0; i < led->num_leds; i++) { |
| 1137 | if (led_array[i].flash_cfg->flash_reg_get) { |
| 1138 | rc = qpnp_led_masked_write(led, |
| 1139 | FLASH_ENABLE_CONTROL(led->base), |
| 1140 | FLASH_ENABLE_MASK, |
| 1141 | FLASH_DISABLE_ALL); |
| 1142 | if (rc) { |
| 1143 | dev_err(&led->pdev->dev, |
| 1144 | "Enable reg write failed(%d)\n", |
| 1145 | rc); |
| 1146 | } |
| 1147 | |
| 1148 | rc = regulator_disable( |
| 1149 | led_array[i].flash_cfg->flash_boost_reg); |
| 1150 | if (rc) { |
| 1151 | dev_err(&led->pdev->dev, "Flash boost regulator disable failed(%d)\n", |
| 1152 | rc); |
| 1153 | return rc; |
| 1154 | } |
| 1155 | if (led_array[i].flash_cfg->flash_wa_reg_get) { |
| 1156 | rc = regulator_disable( |
| 1157 | led_array[i].flash_cfg-> |
| 1158 | flash_wa_reg); |
| 1159 | if (rc) { |
| 1160 | dev_err(&led->pdev->dev, "Flash_wa regulator disable failed(%d)\n", |
| 1161 | rc); |
| 1162 | return rc; |
| 1163 | } |
| 1164 | } |
| 1165 | led->flash_cfg->flash_on = false; |
| 1166 | } |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static int qpnp_torch_regulator_operate(struct qpnp_led_data *led, bool on) |
| 1175 | { |
| 1176 | int rc; |
| 1177 | |
| 1178 | if (!on) |
| 1179 | goto regulator_turn_off; |
| 1180 | |
| 1181 | if (!led->flash_cfg->torch_on) { |
| 1182 | rc = regulator_enable(led->flash_cfg->torch_boost_reg); |
| 1183 | if (rc) { |
| 1184 | dev_err(&led->pdev->dev, |
| 1185 | "Regulator enable failed(%d)\n", rc); |
| 1186 | return rc; |
| 1187 | } |
| 1188 | led->flash_cfg->torch_on = true; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | |
| 1192 | regulator_turn_off: |
| 1193 | if (led->flash_cfg->torch_on) { |
| 1194 | rc = qpnp_led_masked_write(led, FLASH_ENABLE_CONTROL(led->base), |
| 1195 | FLASH_ENABLE_MODULE_MASK, FLASH_DISABLE_ALL); |
| 1196 | if (rc) { |
| 1197 | dev_err(&led->pdev->dev, |
| 1198 | "Enable reg write failed(%d)\n", rc); |
| 1199 | } |
| 1200 | |
| 1201 | rc = regulator_disable(led->flash_cfg->torch_boost_reg); |
| 1202 | if (rc) { |
| 1203 | dev_err(&led->pdev->dev, |
| 1204 | "Regulator disable failed(%d)\n", rc); |
| 1205 | return rc; |
| 1206 | } |
| 1207 | led->flash_cfg->torch_on = false; |
| 1208 | } |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static int qpnp_flash_set(struct qpnp_led_data *led) |
| 1213 | { |
| 1214 | int rc, error; |
| 1215 | int val = led->cdev.brightness; |
| 1216 | |
| 1217 | if (led->flash_cfg->torch_enable) |
| 1218 | led->flash_cfg->current_prgm = |
| 1219 | (val * TORCH_MAX_LEVEL / led->max_current); |
| 1220 | else |
| 1221 | led->flash_cfg->current_prgm = |
| 1222 | (val * FLASH_MAX_LEVEL / led->max_current); |
| 1223 | |
| 1224 | /* Set led current */ |
| 1225 | if (val > 0) { |
| 1226 | if (led->flash_cfg->torch_enable) { |
| 1227 | if (led->flash_cfg->peripheral_subtype == |
| 1228 | FLASH_SUBTYPE_DUAL) { |
| 1229 | if (!led->flash_cfg->no_smbb_support) |
| 1230 | rc = qpnp_torch_regulator_operate(led, |
| 1231 | true); |
| 1232 | else |
| 1233 | rc = qpnp_flash_regulator_operate(led, |
| 1234 | true); |
| 1235 | if (rc) { |
| 1236 | dev_err(&led->pdev->dev, |
| 1237 | "Torch regulator operate failed(%d)\n", |
| 1238 | rc); |
| 1239 | return rc; |
| 1240 | } |
| 1241 | } else if (led->flash_cfg->peripheral_subtype == |
| 1242 | FLASH_SUBTYPE_SINGLE) { |
| 1243 | rc = qpnp_flash_regulator_operate(led, true); |
| 1244 | if (rc) { |
| 1245 | dev_err(&led->pdev->dev, |
| 1246 | "Flash regulator operate failed(%d)\n", |
| 1247 | rc); |
| 1248 | goto error_flash_set; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | qpnp_led_masked_write(led, FLASH_MAX_CURR(led->base), |
| 1253 | FLASH_CURRENT_MASK, |
| 1254 | TORCH_MAX_LEVEL); |
| 1255 | if (rc) { |
| 1256 | dev_err(&led->pdev->dev, |
| 1257 | "Max current reg write failed(%d)\n", |
| 1258 | rc); |
| 1259 | goto error_reg_write; |
| 1260 | } |
| 1261 | |
| 1262 | qpnp_led_masked_write(led, |
| 1263 | FLASH_LED_TMR_CTRL(led->base), |
| 1264 | FLASH_TMR_MASK, |
| 1265 | FLASH_TMR_WATCHDOG); |
| 1266 | if (rc) { |
| 1267 | dev_err(&led->pdev->dev, |
| 1268 | "Timer control reg write failed(%d)\n", |
| 1269 | rc); |
| 1270 | goto error_reg_write; |
| 1271 | } |
| 1272 | |
| 1273 | rc = qpnp_led_masked_write(led, |
| 1274 | led->flash_cfg->current_addr, |
| 1275 | FLASH_CURRENT_MASK, |
| 1276 | led->flash_cfg->current_prgm); |
| 1277 | if (rc) { |
| 1278 | dev_err(&led->pdev->dev, |
| 1279 | "Current reg write failed(%d)\n", rc); |
| 1280 | goto error_reg_write; |
| 1281 | } |
| 1282 | |
| 1283 | rc = qpnp_led_masked_write(led, |
| 1284 | led->flash_cfg->second_addr, |
| 1285 | FLASH_CURRENT_MASK, |
| 1286 | led->flash_cfg->current_prgm); |
| 1287 | if (rc) { |
| 1288 | dev_err(&led->pdev->dev, |
| 1289 | "2nd Current reg write failed(%d)\n", |
| 1290 | rc); |
| 1291 | goto error_reg_write; |
| 1292 | } |
| 1293 | |
| 1294 | qpnp_led_masked_write(led, |
| 1295 | FLASH_WATCHDOG_TMR(led->base), |
| 1296 | FLASH_WATCHDOG_MASK, |
| 1297 | led->flash_cfg->duration); |
| 1298 | if (rc) { |
| 1299 | dev_err(&led->pdev->dev, |
| 1300 | "Max current reg write failed(%d)\n", |
| 1301 | rc); |
| 1302 | goto error_reg_write; |
| 1303 | } |
| 1304 | |
| 1305 | rc = qpnp_led_masked_write(led, |
| 1306 | FLASH_ENABLE_CONTROL(led->base), |
| 1307 | FLASH_ENABLE_MASK, |
| 1308 | led->flash_cfg->enable_module); |
| 1309 | if (rc) { |
| 1310 | dev_err(&led->pdev->dev, |
| 1311 | "Enable reg write failed(%d)\n", |
| 1312 | rc); |
| 1313 | goto error_reg_write; |
| 1314 | } |
| 1315 | |
| 1316 | if (!led->flash_cfg->strobe_type) |
| 1317 | led->flash_cfg->trigger_flash &= |
| 1318 | ~FLASH_HW_SW_STROBE_SEL_MASK; |
| 1319 | else |
| 1320 | led->flash_cfg->trigger_flash |= |
| 1321 | FLASH_HW_SW_STROBE_SEL_MASK; |
| 1322 | |
| 1323 | rc = qpnp_led_masked_write(led, |
| 1324 | FLASH_LED_STROBE_CTRL(led->base), |
| 1325 | led->flash_cfg->trigger_flash, |
| 1326 | led->flash_cfg->trigger_flash); |
| 1327 | if (rc) { |
| 1328 | dev_err(&led->pdev->dev, |
| 1329 | "LED %d strobe reg write failed(%d)\n", |
| 1330 | led->id, rc); |
| 1331 | goto error_reg_write; |
| 1332 | } |
| 1333 | } else { |
| 1334 | rc = qpnp_flash_regulator_operate(led, true); |
| 1335 | if (rc) { |
| 1336 | dev_err(&led->pdev->dev, |
| 1337 | "Flash regulator operate failed(%d)\n", |
| 1338 | rc); |
| 1339 | goto error_flash_set; |
| 1340 | } |
| 1341 | |
| 1342 | qpnp_led_masked_write(led, |
| 1343 | FLASH_LED_TMR_CTRL(led->base), |
| 1344 | FLASH_TMR_MASK, |
| 1345 | FLASH_TMR_SAFETY); |
| 1346 | if (rc) { |
| 1347 | dev_err(&led->pdev->dev, |
| 1348 | "Timer control reg write failed(%d)\n", |
| 1349 | rc); |
| 1350 | goto error_reg_write; |
| 1351 | } |
| 1352 | |
| 1353 | /* Set flash safety timer */ |
| 1354 | rc = qpnp_led_masked_write(led, |
| 1355 | FLASH_SAFETY_TIMER(led->base), |
| 1356 | FLASH_SAFETY_TIMER_MASK, |
| 1357 | led->flash_cfg->duration); |
| 1358 | if (rc) { |
| 1359 | dev_err(&led->pdev->dev, |
| 1360 | "Safety timer reg write failed(%d)\n", |
| 1361 | rc); |
| 1362 | goto error_flash_set; |
| 1363 | } |
| 1364 | |
| 1365 | /* Set max current */ |
| 1366 | rc = qpnp_led_masked_write(led, |
| 1367 | FLASH_MAX_CURR(led->base), FLASH_CURRENT_MASK, |
| 1368 | FLASH_MAX_LEVEL); |
| 1369 | if (rc) { |
| 1370 | dev_err(&led->pdev->dev, |
| 1371 | "Max current reg write failed(%d)\n", |
| 1372 | rc); |
| 1373 | goto error_flash_set; |
| 1374 | } |
| 1375 | |
| 1376 | /* Set clamp current */ |
| 1377 | rc = qpnp_led_masked_write(led, |
| 1378 | FLASH_CLAMP_CURR(led->base), |
| 1379 | FLASH_CURRENT_MASK, |
| 1380 | led->flash_cfg->clamp_curr); |
| 1381 | if (rc) { |
| 1382 | dev_err(&led->pdev->dev, |
| 1383 | "Clamp current reg write failed(%d)\n", |
| 1384 | rc); |
| 1385 | goto error_flash_set; |
| 1386 | } |
| 1387 | |
| 1388 | rc = qpnp_led_masked_write(led, |
| 1389 | led->flash_cfg->current_addr, |
| 1390 | FLASH_CURRENT_MASK, |
| 1391 | led->flash_cfg->current_prgm); |
| 1392 | if (rc) { |
| 1393 | dev_err(&led->pdev->dev, |
| 1394 | "Current reg write failed(%d)\n", rc); |
| 1395 | goto error_flash_set; |
| 1396 | } |
| 1397 | |
| 1398 | rc = qpnp_led_masked_write(led, |
| 1399 | FLASH_ENABLE_CONTROL(led->base), |
| 1400 | led->flash_cfg->enable_module, |
| 1401 | led->flash_cfg->enable_module); |
| 1402 | if (rc) { |
| 1403 | dev_err(&led->pdev->dev, |
| 1404 | "Enable reg write failed(%d)\n", rc); |
| 1405 | goto error_flash_set; |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * Add 1ms delay for bharger enter stable state |
| 1410 | */ |
| 1411 | usleep_range(FLASH_RAMP_UP_DELAY_US, |
| 1412 | FLASH_RAMP_UP_DELAY_US + 10); |
| 1413 | |
| 1414 | if (!led->flash_cfg->strobe_type) |
| 1415 | led->flash_cfg->trigger_flash &= |
| 1416 | ~FLASH_HW_SW_STROBE_SEL_MASK; |
| 1417 | else |
| 1418 | led->flash_cfg->trigger_flash |= |
| 1419 | FLASH_HW_SW_STROBE_SEL_MASK; |
| 1420 | |
| 1421 | rc = qpnp_led_masked_write(led, |
| 1422 | FLASH_LED_STROBE_CTRL(led->base), |
| 1423 | led->flash_cfg->trigger_flash, |
| 1424 | led->flash_cfg->trigger_flash); |
| 1425 | if (rc) { |
| 1426 | dev_err(&led->pdev->dev, |
| 1427 | "LED %d strobe reg write failed(%d)\n", |
| 1428 | led->id, rc); |
| 1429 | goto error_flash_set; |
| 1430 | } |
| 1431 | } |
| 1432 | } else { |
| 1433 | rc = qpnp_led_masked_write(led, |
| 1434 | FLASH_LED_STROBE_CTRL(led->base), |
| 1435 | led->flash_cfg->trigger_flash, |
| 1436 | FLASH_DISABLE_ALL); |
| 1437 | if (rc) { |
| 1438 | dev_err(&led->pdev->dev, |
| 1439 | "LED %d flash write failed(%d)\n", led->id, rc); |
| 1440 | if (led->flash_cfg->torch_enable) |
| 1441 | goto error_torch_set; |
| 1442 | else |
| 1443 | goto error_flash_set; |
| 1444 | } |
| 1445 | |
| 1446 | if (led->flash_cfg->torch_enable) { |
| 1447 | if (led->flash_cfg->peripheral_subtype == |
| 1448 | FLASH_SUBTYPE_DUAL) { |
| 1449 | if (!led->flash_cfg->no_smbb_support) |
| 1450 | rc = qpnp_torch_regulator_operate(led, |
| 1451 | false); |
| 1452 | else |
| 1453 | rc = qpnp_flash_regulator_operate(led, |
| 1454 | false); |
| 1455 | if (rc) { |
| 1456 | dev_err(&led->pdev->dev, |
| 1457 | "Torch regulator operate failed(%d)\n", |
| 1458 | rc); |
| 1459 | return rc; |
| 1460 | } |
| 1461 | } else if (led->flash_cfg->peripheral_subtype == |
| 1462 | FLASH_SUBTYPE_SINGLE) { |
| 1463 | rc = qpnp_flash_regulator_operate(led, false); |
| 1464 | if (rc) { |
| 1465 | dev_err(&led->pdev->dev, |
| 1466 | "Flash regulator operate failed(%d)\n", |
| 1467 | rc); |
| 1468 | return rc; |
| 1469 | } |
| 1470 | } |
| 1471 | } else { |
| 1472 | /* |
| 1473 | * Disable module after ramp down complete for stable |
| 1474 | * behavior |
| 1475 | */ |
| 1476 | usleep_range(FLASH_RAMP_UP_DELAY_US, |
| 1477 | FLASH_RAMP_UP_DELAY_US + 10); |
| 1478 | |
| 1479 | rc = qpnp_led_masked_write(led, |
| 1480 | FLASH_ENABLE_CONTROL(led->base), |
| 1481 | led->flash_cfg->enable_module & |
| 1482 | ~FLASH_ENABLE_MODULE_MASK, |
| 1483 | FLASH_DISABLE_ALL); |
| 1484 | if (rc) { |
| 1485 | dev_err(&led->pdev->dev, |
| 1486 | "Enable reg write failed(%d)\n", rc); |
| 1487 | if (led->flash_cfg->torch_enable) |
| 1488 | goto error_torch_set; |
| 1489 | else |
| 1490 | goto error_flash_set; |
| 1491 | } |
| 1492 | |
| 1493 | rc = qpnp_flash_regulator_operate(led, false); |
| 1494 | if (rc) { |
| 1495 | dev_err(&led->pdev->dev, |
| 1496 | "Flash regulator operate failed(%d)\n", |
| 1497 | rc); |
| 1498 | return rc; |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | qpnp_dump_regs(led, flash_debug_regs, ARRAY_SIZE(flash_debug_regs)); |
| 1504 | |
| 1505 | return 0; |
| 1506 | |
| 1507 | error_reg_write: |
| 1508 | if (led->flash_cfg->peripheral_subtype == FLASH_SUBTYPE_SINGLE) |
| 1509 | goto error_flash_set; |
| 1510 | |
| 1511 | error_torch_set: |
| 1512 | if (!led->flash_cfg->no_smbb_support) |
| 1513 | error = qpnp_torch_regulator_operate(led, false); |
| 1514 | else |
| 1515 | error = qpnp_flash_regulator_operate(led, false); |
| 1516 | if (error) { |
| 1517 | dev_err(&led->pdev->dev, |
| 1518 | "Torch regulator operate failed(%d)\n", rc); |
| 1519 | return error; |
| 1520 | } |
| 1521 | return rc; |
| 1522 | |
| 1523 | error_flash_set: |
| 1524 | error = qpnp_flash_regulator_operate(led, false); |
| 1525 | if (error) { |
| 1526 | dev_err(&led->pdev->dev, |
| 1527 | "Flash regulator operate failed(%d)\n", rc); |
| 1528 | return error; |
| 1529 | } |
| 1530 | return rc; |
| 1531 | } |
| 1532 | |
| 1533 | static int qpnp_kpdbl_set(struct qpnp_led_data *led) |
| 1534 | { |
| 1535 | int rc; |
| 1536 | int duty_us, duty_ns, period_us; |
| 1537 | |
| 1538 | if (led->cdev.brightness) { |
| 1539 | if (!led->kpdbl_cfg->pwm_cfg->blinking) |
| 1540 | led->kpdbl_cfg->pwm_cfg->mode = |
| 1541 | led->kpdbl_cfg->pwm_cfg->default_mode; |
| 1542 | |
| 1543 | if (bitmap_empty(kpdbl_leds_in_use, NUM_KPDBL_LEDS)) { |
| 1544 | rc = qpnp_led_masked_write(led, KPDBL_ENABLE(led->base), |
| 1545 | KPDBL_MODULE_EN_MASK, KPDBL_MODULE_EN); |
| 1546 | if (rc) { |
| 1547 | dev_err(&led->pdev->dev, |
| 1548 | "Enable reg write failed(%d)\n", rc); |
| 1549 | return rc; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | /* On some platforms, GPLED1 channel should always be enabled |
| 1554 | * for the other GPLEDs 2/3/4 to glow. Before enabling GPLED |
| 1555 | * 2/3/4, first check if GPLED1 is already enabled. If GPLED1 |
| 1556 | * channel is not enabled, then enable the GPLED1 channel but |
| 1557 | * with a 0 brightness |
| 1558 | */ |
| 1559 | if (!led->kpdbl_cfg->always_on && |
| 1560 | !test_bit(KPDBL_MASTER_BIT_INDEX, kpdbl_leds_in_use) && |
| 1561 | kpdbl_master) { |
| 1562 | rc = pwm_config_us(kpdbl_master, 0, |
| 1563 | kpdbl_master_period_us); |
| 1564 | if (rc < 0) { |
| 1565 | dev_err(&led->pdev->dev, |
| 1566 | "pwm config failed\n"); |
| 1567 | return rc; |
| 1568 | } |
| 1569 | |
| 1570 | rc = pwm_enable(kpdbl_master); |
| 1571 | if (rc < 0) { |
| 1572 | dev_err(&led->pdev->dev, |
| 1573 | "pwm enable failed\n"); |
| 1574 | return rc; |
| 1575 | } |
| 1576 | set_bit(KPDBL_MASTER_BIT_INDEX, |
| 1577 | kpdbl_leds_in_use); |
| 1578 | } |
| 1579 | |
| 1580 | if (led->kpdbl_cfg->pwm_cfg->mode == PWM_MODE) { |
| 1581 | period_us = led->kpdbl_cfg->pwm_cfg->pwm_period_us; |
| 1582 | if (period_us > INT_MAX / NSEC_PER_USEC) { |
| 1583 | duty_us = (period_us * led->cdev.brightness) / |
| 1584 | KPDBL_MAX_LEVEL; |
| 1585 | rc = pwm_config_us( |
| 1586 | led->kpdbl_cfg->pwm_cfg->pwm_dev, |
| 1587 | duty_us, |
| 1588 | period_us); |
| 1589 | } else { |
| 1590 | duty_ns = ((period_us * NSEC_PER_USEC) / |
| 1591 | KPDBL_MAX_LEVEL) * led->cdev.brightness; |
| 1592 | rc = pwm_config( |
| 1593 | led->kpdbl_cfg->pwm_cfg->pwm_dev, |
| 1594 | duty_ns, |
| 1595 | period_us * NSEC_PER_USEC); |
| 1596 | } |
| 1597 | if (rc < 0) { |
| 1598 | dev_err(&led->pdev->dev, |
| 1599 | "pwm config failed\n"); |
| 1600 | return rc; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | rc = pwm_enable(led->kpdbl_cfg->pwm_cfg->pwm_dev); |
| 1605 | if (rc < 0) { |
| 1606 | dev_err(&led->pdev->dev, "pwm enable failed\n"); |
| 1607 | return rc; |
| 1608 | } |
| 1609 | |
| 1610 | set_bit(led->kpdbl_cfg->row_id, kpdbl_leds_in_use); |
| 1611 | |
| 1612 | /* is_kpdbl_master_turn_on will be set to true when GPLED1 |
| 1613 | * channel is enabled and has a valid brightness value |
| 1614 | */ |
| 1615 | if (led->kpdbl_cfg->always_on) |
| 1616 | is_kpdbl_master_turn_on = true; |
| 1617 | |
| 1618 | } else { |
| 1619 | led->kpdbl_cfg->pwm_cfg->mode = |
| 1620 | led->kpdbl_cfg->pwm_cfg->default_mode; |
| 1621 | |
| 1622 | /* Before disabling GPLED1, check if any other GPLED 2/3/4 is |
| 1623 | * on. If any of the other GPLED 2/3/4 is on, then have the |
| 1624 | * GPLED1 channel enabled with 0 brightness. |
| 1625 | */ |
| 1626 | if (led->kpdbl_cfg->always_on) { |
| 1627 | if (bitmap_weight(kpdbl_leds_in_use, |
| 1628 | NUM_KPDBL_LEDS) > 1) { |
| 1629 | rc = pwm_config_us( |
| 1630 | led->kpdbl_cfg->pwm_cfg->pwm_dev, 0, |
| 1631 | led->kpdbl_cfg->pwm_cfg->pwm_period_us); |
| 1632 | if (rc < 0) { |
| 1633 | dev_err(&led->pdev->dev, |
| 1634 | "pwm config failed\n"); |
| 1635 | return rc; |
| 1636 | } |
| 1637 | |
| 1638 | rc = pwm_enable(led->kpdbl_cfg->pwm_cfg-> |
| 1639 | pwm_dev); |
| 1640 | if (rc < 0) { |
| 1641 | dev_err(&led->pdev->dev, |
| 1642 | "pwm enable failed\n"); |
| 1643 | return rc; |
| 1644 | } |
| 1645 | } else { |
| 1646 | if (kpdbl_master) { |
| 1647 | pwm_disable(kpdbl_master); |
| 1648 | clear_bit(KPDBL_MASTER_BIT_INDEX, |
| 1649 | kpdbl_leds_in_use); |
| 1650 | rc = qpnp_led_masked_write( |
| 1651 | led, KPDBL_ENABLE(led->base), |
| 1652 | KPDBL_MODULE_EN_MASK, |
| 1653 | KPDBL_MODULE_DIS); |
| 1654 | if (rc) { |
| 1655 | dev_err(&led->pdev->dev, "Failed to write led enable reg\n"); |
| 1656 | return rc; |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | is_kpdbl_master_turn_on = false; |
| 1661 | } else { |
| 1662 | pwm_disable(led->kpdbl_cfg->pwm_cfg->pwm_dev); |
| 1663 | clear_bit(led->kpdbl_cfg->row_id, kpdbl_leds_in_use); |
| 1664 | if (bitmap_weight(kpdbl_leds_in_use, |
| 1665 | NUM_KPDBL_LEDS) == 1 && kpdbl_master && |
| 1666 | !is_kpdbl_master_turn_on) { |
| 1667 | pwm_disable(kpdbl_master); |
| 1668 | clear_bit(KPDBL_MASTER_BIT_INDEX, |
| 1669 | kpdbl_leds_in_use); |
| 1670 | rc = qpnp_led_masked_write( |
| 1671 | led, KPDBL_ENABLE(led->base), |
| 1672 | KPDBL_MODULE_EN_MASK, KPDBL_MODULE_DIS); |
| 1673 | if (rc) { |
| 1674 | dev_err(&led->pdev->dev, |
| 1675 | "Failed to write led enable reg\n"); |
| 1676 | return rc; |
| 1677 | } |
| 1678 | is_kpdbl_master_turn_on = false; |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | led->kpdbl_cfg->pwm_cfg->blinking = false; |
| 1684 | |
| 1685 | qpnp_dump_regs(led, kpdbl_debug_regs, ARRAY_SIZE(kpdbl_debug_regs)); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static int qpnp_rgb_set(struct qpnp_led_data *led) |
| 1691 | { |
| 1692 | int rc; |
| 1693 | int duty_us, duty_ns, period_us; |
| 1694 | |
| 1695 | if (led->cdev.brightness) { |
| 1696 | if (!led->rgb_cfg->pwm_cfg->blinking) |
| 1697 | led->rgb_cfg->pwm_cfg->mode = |
| 1698 | led->rgb_cfg->pwm_cfg->default_mode; |
| 1699 | if (led->rgb_cfg->pwm_cfg->mode == PWM_MODE) { |
| 1700 | period_us = led->rgb_cfg->pwm_cfg->pwm_period_us; |
| 1701 | if (period_us > INT_MAX / NSEC_PER_USEC) { |
| 1702 | duty_us = (period_us * led->cdev.brightness) / |
| 1703 | LED_FULL; |
| 1704 | rc = pwm_config_us( |
| 1705 | led->rgb_cfg->pwm_cfg->pwm_dev, |
| 1706 | duty_us, |
| 1707 | period_us); |
| 1708 | } else { |
| 1709 | duty_ns = ((period_us * NSEC_PER_USEC) / |
| 1710 | LED_FULL) * led->cdev.brightness; |
| 1711 | rc = pwm_config( |
| 1712 | led->rgb_cfg->pwm_cfg->pwm_dev, |
| 1713 | duty_ns, |
| 1714 | period_us * NSEC_PER_USEC); |
| 1715 | } |
| 1716 | if (rc < 0) { |
| 1717 | dev_err(&led->pdev->dev, |
| 1718 | "pwm config failed\n"); |
| 1719 | return rc; |
| 1720 | } |
| 1721 | } |
| 1722 | rc = qpnp_led_masked_write(led, |
| 1723 | RGB_LED_EN_CTL(led->base), |
| 1724 | led->rgb_cfg->enable, led->rgb_cfg->enable); |
| 1725 | if (rc) { |
| 1726 | dev_err(&led->pdev->dev, |
| 1727 | "Failed to write led enable reg\n"); |
| 1728 | return rc; |
| 1729 | } |
| 1730 | |
| 1731 | if (led->rgb_cfg->pwm_cfg->pwm_enabled) { |
| 1732 | pwm_disable(led->rgb_cfg->pwm_cfg->pwm_dev); |
| 1733 | led->rgb_cfg->pwm_cfg->pwm_enabled = 0; |
| 1734 | } |
| 1735 | |
| 1736 | rc = pwm_enable(led->rgb_cfg->pwm_cfg->pwm_dev); |
| 1737 | if (!rc) |
| 1738 | led->rgb_cfg->pwm_cfg->pwm_enabled = 1; |
| 1739 | } else { |
| 1740 | led->rgb_cfg->pwm_cfg->mode = |
| 1741 | led->rgb_cfg->pwm_cfg->default_mode; |
| 1742 | pwm_disable(led->rgb_cfg->pwm_cfg->pwm_dev); |
| 1743 | led->rgb_cfg->pwm_cfg->pwm_enabled = 0; |
| 1744 | rc = qpnp_led_masked_write(led, |
| 1745 | RGB_LED_EN_CTL(led->base), |
| 1746 | led->rgb_cfg->enable, RGB_LED_DISABLE); |
| 1747 | if (rc) { |
| 1748 | dev_err(&led->pdev->dev, |
| 1749 | "Failed to write led enable reg\n"); |
| 1750 | return rc; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | led->rgb_cfg->pwm_cfg->blinking = false; |
| 1755 | qpnp_dump_regs(led, rgb_pwm_debug_regs, ARRAY_SIZE(rgb_pwm_debug_regs)); |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
| 1760 | static void qpnp_led_set(struct led_classdev *led_cdev, |
| 1761 | enum led_brightness value) |
| 1762 | { |
| 1763 | struct qpnp_led_data *led; |
| 1764 | |
| 1765 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 1766 | if (value < LED_OFF) { |
| 1767 | dev_err(&led->pdev->dev, "Invalid brightness value\n"); |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | if (value > led->cdev.max_brightness) |
| 1772 | value = led->cdev.max_brightness; |
| 1773 | |
| 1774 | led->cdev.brightness = value; |
| 1775 | if (led->in_order_command_processing) |
| 1776 | queue_work(led->workqueue, &led->work); |
| 1777 | else |
| 1778 | schedule_work(&led->work); |
| 1779 | } |
| 1780 | |
| 1781 | static void __qpnp_led_work(struct qpnp_led_data *led, |
| 1782 | enum led_brightness value) |
| 1783 | { |
| 1784 | int rc; |
| 1785 | |
| 1786 | if (led->id == QPNP_ID_FLASH1_LED0 || led->id == QPNP_ID_FLASH1_LED1) |
| 1787 | mutex_lock(&flash_lock); |
| 1788 | else |
| 1789 | mutex_lock(&led->lock); |
| 1790 | |
| 1791 | switch (led->id) { |
| 1792 | case QPNP_ID_WLED: |
| 1793 | rc = qpnp_wled_set(led); |
| 1794 | if (rc < 0) |
| 1795 | dev_err(&led->pdev->dev, |
| 1796 | "WLED set brightness failed (%d)\n", rc); |
| 1797 | break; |
| 1798 | case QPNP_ID_FLASH1_LED0: |
| 1799 | case QPNP_ID_FLASH1_LED1: |
| 1800 | rc = qpnp_flash_set(led); |
| 1801 | if (rc < 0) |
| 1802 | dev_err(&led->pdev->dev, |
| 1803 | "FLASH set brightness failed (%d)\n", rc); |
| 1804 | break; |
| 1805 | case QPNP_ID_RGB_RED: |
| 1806 | case QPNP_ID_RGB_GREEN: |
| 1807 | case QPNP_ID_RGB_BLUE: |
| 1808 | rc = qpnp_rgb_set(led); |
| 1809 | if (rc < 0) |
| 1810 | dev_err(&led->pdev->dev, |
| 1811 | "RGB set brightness failed (%d)\n", rc); |
| 1812 | break; |
| 1813 | case QPNP_ID_LED_MPP: |
| 1814 | rc = qpnp_mpp_set(led); |
| 1815 | if (rc < 0) |
| 1816 | dev_err(&led->pdev->dev, |
| 1817 | "MPP set brightness failed (%d)\n", rc); |
| 1818 | break; |
| 1819 | case QPNP_ID_LED_GPIO: |
| 1820 | rc = qpnp_gpio_set(led); |
| 1821 | if (rc < 0) |
| 1822 | dev_err(&led->pdev->dev, |
| 1823 | "GPIO set brightness failed (%d)\n", |
| 1824 | rc); |
| 1825 | break; |
| 1826 | case QPNP_ID_KPDBL: |
| 1827 | rc = qpnp_kpdbl_set(led); |
| 1828 | if (rc < 0) |
| 1829 | dev_err(&led->pdev->dev, |
| 1830 | "KPDBL set brightness failed (%d)\n", rc); |
| 1831 | break; |
| 1832 | default: |
| 1833 | dev_err(&led->pdev->dev, "Invalid LED(%d)\n", led->id); |
| 1834 | break; |
| 1835 | } |
| 1836 | if (led->id == QPNP_ID_FLASH1_LED0 || led->id == QPNP_ID_FLASH1_LED1) |
| 1837 | mutex_unlock(&flash_lock); |
| 1838 | else |
| 1839 | mutex_unlock(&led->lock); |
| 1840 | |
| 1841 | } |
| 1842 | |
| 1843 | static void qpnp_led_work(struct work_struct *work) |
| 1844 | { |
| 1845 | struct qpnp_led_data *led = container_of(work, |
| 1846 | struct qpnp_led_data, work); |
| 1847 | |
| 1848 | __qpnp_led_work(led, led->cdev.brightness); |
| 1849 | } |
| 1850 | |
| 1851 | static int qpnp_led_set_max_brightness(struct qpnp_led_data *led) |
| 1852 | { |
| 1853 | switch (led->id) { |
| 1854 | case QPNP_ID_WLED: |
| 1855 | led->cdev.max_brightness = WLED_MAX_LEVEL; |
| 1856 | break; |
| 1857 | case QPNP_ID_FLASH1_LED0: |
| 1858 | case QPNP_ID_FLASH1_LED1: |
| 1859 | led->cdev.max_brightness = led->max_current; |
| 1860 | break; |
| 1861 | case QPNP_ID_RGB_RED: |
| 1862 | case QPNP_ID_RGB_GREEN: |
| 1863 | case QPNP_ID_RGB_BLUE: |
| 1864 | led->cdev.max_brightness = RGB_MAX_LEVEL; |
| 1865 | break; |
| 1866 | case QPNP_ID_LED_MPP: |
| 1867 | if (led->mpp_cfg->pwm_mode == MANUAL_MODE) |
| 1868 | led->cdev.max_brightness = led->max_current; |
| 1869 | else |
| 1870 | led->cdev.max_brightness = MPP_MAX_LEVEL; |
| 1871 | break; |
| 1872 | case QPNP_ID_LED_GPIO: |
| 1873 | led->cdev.max_brightness = led->max_current; |
| 1874 | break; |
| 1875 | case QPNP_ID_KPDBL: |
| 1876 | led->cdev.max_brightness = KPDBL_MAX_LEVEL; |
| 1877 | break; |
| 1878 | default: |
| 1879 | dev_err(&led->pdev->dev, "Invalid LED(%d)\n", led->id); |
| 1880 | return -EINVAL; |
| 1881 | } |
| 1882 | |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | static enum led_brightness qpnp_led_get(struct led_classdev *led_cdev) |
| 1887 | { |
| 1888 | struct qpnp_led_data *led; |
| 1889 | |
| 1890 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 1891 | |
| 1892 | return led->cdev.brightness; |
| 1893 | } |
| 1894 | |
| 1895 | static void qpnp_led_turn_off_delayed(struct work_struct *work) |
| 1896 | { |
| 1897 | struct delayed_work *dwork = to_delayed_work(work); |
| 1898 | struct qpnp_led_data *led |
| 1899 | = container_of(dwork, struct qpnp_led_data, dwork); |
| 1900 | |
| 1901 | led->cdev.brightness = LED_OFF; |
| 1902 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 1903 | } |
| 1904 | |
| 1905 | static void qpnp_led_turn_off(struct qpnp_led_data *led) |
| 1906 | { |
| 1907 | INIT_DELAYED_WORK(&led->dwork, qpnp_led_turn_off_delayed); |
| 1908 | schedule_delayed_work(&led->dwork, |
| 1909 | msecs_to_jiffies(led->turn_off_delay_ms)); |
| 1910 | } |
| 1911 | |
| 1912 | static int qpnp_wled_init(struct qpnp_led_data *led) |
| 1913 | { |
| 1914 | int rc, i; |
| 1915 | u8 num_wled_strings, val = 0; |
| 1916 | |
| 1917 | num_wled_strings = led->wled_cfg->num_strings; |
| 1918 | |
| 1919 | /* verify ranges */ |
| 1920 | if (led->wled_cfg->ovp_val > WLED_OVP_27V) { |
| 1921 | dev_err(&led->pdev->dev, "Invalid ovp value\n"); |
| 1922 | return -EINVAL; |
| 1923 | } |
| 1924 | |
| 1925 | if (led->wled_cfg->boost_curr_lim > WLED_CURR_LIMIT_1680mA) { |
| 1926 | dev_err(&led->pdev->dev, "Invalid boost current limit\n"); |
| 1927 | return -EINVAL; |
| 1928 | } |
| 1929 | |
| 1930 | if (led->wled_cfg->cp_select > WLED_CP_SELECT_MAX) { |
| 1931 | dev_err(&led->pdev->dev, "Invalid pole capacitance\n"); |
| 1932 | return -EINVAL; |
| 1933 | } |
| 1934 | |
| 1935 | if (led->max_current > WLED_MAX_CURR) { |
| 1936 | dev_err(&led->pdev->dev, "Invalid max current\n"); |
| 1937 | return -EINVAL; |
| 1938 | } |
| 1939 | |
| 1940 | if ((led->wled_cfg->ctrl_delay_us % WLED_CTL_DLY_STEP) || |
| 1941 | (led->wled_cfg->ctrl_delay_us > WLED_CTL_DLY_MAX)) { |
| 1942 | dev_err(&led->pdev->dev, "Invalid control delay\n"); |
| 1943 | return -EINVAL; |
| 1944 | } |
| 1945 | |
| 1946 | /* program over voltage protection threshold */ |
| 1947 | rc = qpnp_led_masked_write(led, WLED_OVP_CFG_REG(led->base), |
| 1948 | WLED_OVP_VAL_MASK, |
| 1949 | (led->wled_cfg->ovp_val << WLED_OVP_VAL_BIT_SHFT)); |
| 1950 | if (rc) { |
| 1951 | dev_err(&led->pdev->dev, |
| 1952 | "WLED OVP reg write failed(%d)\n", rc); |
| 1953 | return rc; |
| 1954 | } |
| 1955 | |
| 1956 | /* program current boost limit */ |
| 1957 | rc = qpnp_led_masked_write(led, WLED_BOOST_LIMIT_REG(led->base), |
| 1958 | WLED_BOOST_LIMIT_MASK, led->wled_cfg->boost_curr_lim); |
| 1959 | if (rc) { |
| 1960 | dev_err(&led->pdev->dev, |
| 1961 | "WLED boost limit reg write failed(%d)\n", rc); |
| 1962 | return rc; |
| 1963 | } |
| 1964 | |
| 1965 | /* program output feedback */ |
| 1966 | rc = qpnp_led_masked_write(led, WLED_FDBCK_CTRL_REG(led->base), |
| 1967 | WLED_OP_FDBCK_MASK, |
| 1968 | (led->wled_cfg->op_fdbck << WLED_OP_FDBCK_BIT_SHFT)); |
| 1969 | if (rc) { |
| 1970 | dev_err(&led->pdev->dev, |
| 1971 | "WLED fdbck ctrl reg write failed(%d)\n", rc); |
| 1972 | return rc; |
| 1973 | } |
| 1974 | |
| 1975 | /* program switch frequency */ |
| 1976 | rc = qpnp_led_masked_write(led, |
| 1977 | WLED_SWITCHING_FREQ_REG(led->base), |
| 1978 | WLED_SWITCH_FREQ_MASK, led->wled_cfg->switch_freq); |
| 1979 | if (rc) { |
| 1980 | dev_err(&led->pdev->dev, |
| 1981 | "WLED switch freq reg write failed(%d)\n", rc); |
| 1982 | return rc; |
| 1983 | } |
| 1984 | |
| 1985 | /* program current sink */ |
| 1986 | if (led->wled_cfg->cs_out_en) { |
| 1987 | for (i = 0; i < led->wled_cfg->num_strings; i++) |
| 1988 | val |= 1 << i; |
| 1989 | rc = qpnp_led_masked_write(led, WLED_CURR_SINK_REG(led->base), |
| 1990 | WLED_CURR_SINK_MASK, (val << WLED_CURR_SINK_SHFT)); |
| 1991 | if (rc) { |
| 1992 | dev_err(&led->pdev->dev, |
| 1993 | "WLED curr sink reg write failed(%d)\n", rc); |
| 1994 | return rc; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | /* program high pole capacitance */ |
| 1999 | rc = qpnp_led_masked_write(led, WLED_HIGH_POLE_CAP_REG(led->base), |
| 2000 | WLED_CP_SELECT_MASK, led->wled_cfg->cp_select); |
| 2001 | if (rc) { |
| 2002 | dev_err(&led->pdev->dev, |
| 2003 | "WLED pole cap reg write failed(%d)\n", rc); |
| 2004 | return rc; |
| 2005 | } |
| 2006 | |
| 2007 | /* program modulator, current mod src and cabc */ |
| 2008 | for (i = 0; i < num_wled_strings; i++) { |
| 2009 | rc = qpnp_led_masked_write(led, WLED_MOD_EN_REG(led->base, i), |
| 2010 | WLED_NO_MASK, WLED_EN_MASK); |
| 2011 | if (rc) { |
| 2012 | dev_err(&led->pdev->dev, |
| 2013 | "WLED mod enable reg write failed(%d)\n", rc); |
| 2014 | return rc; |
| 2015 | } |
| 2016 | |
| 2017 | if (led->wled_cfg->dig_mod_gen_en) { |
| 2018 | rc = qpnp_led_masked_write(led, |
| 2019 | WLED_MOD_SRC_SEL_REG(led->base, i), |
| 2020 | WLED_NO_MASK, WLED_USE_EXT_GEN_MOD_SRC); |
| 2021 | if (rc) { |
| 2022 | dev_err(&led->pdev->dev, |
| 2023 | "WLED dig mod en reg write failed(%d)\n", rc); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | rc = qpnp_led_masked_write(led, |
| 2028 | WLED_FULL_SCALE_REG(led->base, i), WLED_MAX_CURR_MASK, |
| 2029 | (u8)led->max_current); |
| 2030 | if (rc) { |
| 2031 | dev_err(&led->pdev->dev, |
| 2032 | "WLED max current reg write failed(%d)\n", rc); |
| 2033 | return rc; |
| 2034 | } |
| 2035 | |
| 2036 | } |
| 2037 | |
| 2038 | /* Reset WLED enable register */ |
| 2039 | rc = qpnp_led_masked_write(led, WLED_MOD_CTRL_REG(led->base), |
| 2040 | WLED_8_BIT_MASK, WLED_BOOST_OFF); |
| 2041 | if (rc) { |
| 2042 | dev_err(&led->pdev->dev, |
| 2043 | "WLED write ctrl reg failed(%d)\n", rc); |
| 2044 | return rc; |
| 2045 | } |
| 2046 | |
| 2047 | /* dump wled registers */ |
| 2048 | qpnp_dump_regs(led, wled_debug_regs, ARRAY_SIZE(wled_debug_regs)); |
| 2049 | |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
| 2053 | static ssize_t led_mode_store(struct device *dev, |
| 2054 | struct device_attribute *attr, |
| 2055 | const char *buf, size_t count) |
| 2056 | { |
| 2057 | struct qpnp_led_data *led; |
| 2058 | unsigned long state; |
| 2059 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2060 | ssize_t ret = -EINVAL; |
| 2061 | |
| 2062 | ret = kstrtoul(buf, 10, &state); |
| 2063 | if (ret) |
| 2064 | return ret; |
| 2065 | |
| 2066 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2067 | |
| 2068 | /* '1' to enable torch mode; '0' to switch to flash mode */ |
| 2069 | if (state == 1) |
| 2070 | led->flash_cfg->torch_enable = true; |
| 2071 | else |
| 2072 | led->flash_cfg->torch_enable = false; |
| 2073 | |
| 2074 | return count; |
| 2075 | } |
| 2076 | |
| 2077 | static ssize_t led_strobe_type_store(struct device *dev, |
| 2078 | struct device_attribute *attr, |
| 2079 | const char *buf, size_t count) |
| 2080 | { |
| 2081 | struct qpnp_led_data *led; |
| 2082 | unsigned long state; |
| 2083 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2084 | ssize_t ret = -EINVAL; |
| 2085 | |
| 2086 | ret = kstrtoul(buf, 10, &state); |
| 2087 | if (ret) |
| 2088 | return ret; |
| 2089 | |
| 2090 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2091 | |
| 2092 | /* '0' for sw strobe; '1' for hw strobe */ |
| 2093 | if (state == 1) |
| 2094 | led->flash_cfg->strobe_type = 1; |
| 2095 | else |
| 2096 | led->flash_cfg->strobe_type = 0; |
| 2097 | |
| 2098 | return count; |
| 2099 | } |
| 2100 | |
| 2101 | static int qpnp_pwm_init(struct pwm_config_data *pwm_cfg, |
| 2102 | struct platform_device *pdev, |
| 2103 | const char *name) |
| 2104 | { |
| 2105 | int rc, start_idx, idx_len, lut_max_size; |
| 2106 | |
| 2107 | if (pwm_cfg->pwm_dev) { |
| 2108 | if (pwm_cfg->mode == LPG_MODE) { |
| 2109 | start_idx = |
| 2110 | pwm_cfg->duty_cycles->start_idx; |
| 2111 | idx_len = |
| 2112 | pwm_cfg->duty_cycles->num_duty_pcts; |
| 2113 | |
| 2114 | if (strnstr(name, "kpdbl", sizeof("kpdbl"))) |
| 2115 | lut_max_size = PWM_GPLED_LUT_MAX_SIZE; |
| 2116 | else |
| 2117 | lut_max_size = PWM_LUT_MAX_SIZE; |
| 2118 | |
| 2119 | if (idx_len >= lut_max_size && start_idx) { |
| 2120 | dev_err(&pdev->dev, |
| 2121 | "Wrong LUT size or index\n"); |
| 2122 | return -EINVAL; |
| 2123 | } |
| 2124 | |
| 2125 | if ((start_idx + idx_len) > lut_max_size) { |
| 2126 | dev_err(&pdev->dev, "Exceed LUT limit\n"); |
| 2127 | return -EINVAL; |
| 2128 | } |
| 2129 | rc = pwm_lut_config(pwm_cfg->pwm_dev, |
| 2130 | pwm_cfg->pwm_period_us, |
| 2131 | pwm_cfg->duty_cycles->duty_pcts, |
| 2132 | pwm_cfg->lut_params); |
| 2133 | if (rc < 0) { |
| 2134 | dev_err(&pdev->dev, "Failed to configure pwm LUT\n"); |
| 2135 | return rc; |
| 2136 | } |
| 2137 | } |
| 2138 | } else { |
| 2139 | dev_err(&pdev->dev, "Invalid PWM device\n"); |
| 2140 | return -EINVAL; |
| 2141 | } |
| 2142 | |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
| 2146 | static ssize_t pwm_us_store(struct device *dev, |
| 2147 | struct device_attribute *attr, |
| 2148 | const char *buf, size_t count) |
| 2149 | { |
| 2150 | struct qpnp_led_data *led; |
| 2151 | u32 pwm_us; |
| 2152 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2153 | ssize_t ret; |
| 2154 | u32 previous_pwm_us; |
| 2155 | struct pwm_config_data *pwm_cfg; |
| 2156 | |
| 2157 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2158 | |
| 2159 | ret = kstrtou32(buf, 10, &pwm_us); |
| 2160 | if (ret) |
| 2161 | return ret; |
| 2162 | |
| 2163 | switch (led->id) { |
| 2164 | case QPNP_ID_LED_MPP: |
| 2165 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2166 | break; |
| 2167 | case QPNP_ID_RGB_RED: |
| 2168 | case QPNP_ID_RGB_GREEN: |
| 2169 | case QPNP_ID_RGB_BLUE: |
| 2170 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2171 | break; |
| 2172 | case QPNP_ID_KPDBL: |
| 2173 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2174 | break; |
| 2175 | default: |
| 2176 | dev_err(&led->pdev->dev, "Invalid LED id type for pwm_us\n"); |
| 2177 | return -EINVAL; |
| 2178 | } |
| 2179 | |
| 2180 | if (pwm_cfg->mode == LPG_MODE) |
| 2181 | pwm_cfg->blinking = true; |
| 2182 | |
| 2183 | previous_pwm_us = pwm_cfg->pwm_period_us; |
| 2184 | |
| 2185 | pwm_cfg->pwm_period_us = pwm_us; |
| 2186 | pwm_free(pwm_cfg->pwm_dev); |
| 2187 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2188 | if (ret) { |
| 2189 | pwm_cfg->pwm_period_us = previous_pwm_us; |
| 2190 | pwm_free(pwm_cfg->pwm_dev); |
| 2191 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2192 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2193 | dev_err(&led->pdev->dev, |
| 2194 | "Failed to initialize pwm with new pwm_us value\n"); |
| 2195 | return ret; |
| 2196 | } |
| 2197 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2198 | return count; |
| 2199 | } |
| 2200 | |
| 2201 | static ssize_t pause_lo_store(struct device *dev, |
| 2202 | struct device_attribute *attr, |
| 2203 | const char *buf, size_t count) |
| 2204 | { |
| 2205 | struct qpnp_led_data *led; |
| 2206 | u32 pause_lo; |
| 2207 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2208 | ssize_t ret; |
| 2209 | u32 previous_pause_lo; |
| 2210 | struct pwm_config_data *pwm_cfg; |
| 2211 | |
| 2212 | ret = kstrtou32(buf, 10, &pause_lo); |
| 2213 | if (ret) |
| 2214 | return ret; |
| 2215 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2216 | |
| 2217 | switch (led->id) { |
| 2218 | case QPNP_ID_LED_MPP: |
| 2219 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2220 | break; |
| 2221 | case QPNP_ID_RGB_RED: |
| 2222 | case QPNP_ID_RGB_GREEN: |
| 2223 | case QPNP_ID_RGB_BLUE: |
| 2224 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2225 | break; |
| 2226 | case QPNP_ID_KPDBL: |
| 2227 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2228 | break; |
| 2229 | default: |
| 2230 | dev_err(&led->pdev->dev, |
| 2231 | "Invalid LED id type for pause lo\n"); |
| 2232 | return -EINVAL; |
| 2233 | } |
| 2234 | |
| 2235 | if (pwm_cfg->mode == LPG_MODE) |
| 2236 | pwm_cfg->blinking = true; |
| 2237 | |
| 2238 | previous_pause_lo = pwm_cfg->lut_params.lut_pause_lo; |
| 2239 | |
| 2240 | pwm_free(pwm_cfg->pwm_dev); |
| 2241 | pwm_cfg->lut_params.lut_pause_lo = pause_lo; |
| 2242 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2243 | if (ret) { |
| 2244 | pwm_cfg->lut_params.lut_pause_lo = previous_pause_lo; |
| 2245 | pwm_free(pwm_cfg->pwm_dev); |
| 2246 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2247 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2248 | dev_err(&led->pdev->dev, |
| 2249 | "Failed to initialize pwm with new pause lo value\n"); |
| 2250 | return ret; |
| 2251 | } |
| 2252 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2253 | return count; |
| 2254 | } |
| 2255 | |
| 2256 | static ssize_t pause_hi_store(struct device *dev, |
| 2257 | struct device_attribute *attr, |
| 2258 | const char *buf, size_t count) |
| 2259 | { |
| 2260 | struct qpnp_led_data *led; |
| 2261 | u32 pause_hi; |
| 2262 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2263 | ssize_t ret; |
| 2264 | u32 previous_pause_hi; |
| 2265 | struct pwm_config_data *pwm_cfg; |
| 2266 | |
| 2267 | ret = kstrtou32(buf, 10, &pause_hi); |
| 2268 | if (ret) |
| 2269 | return ret; |
| 2270 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2271 | |
| 2272 | switch (led->id) { |
| 2273 | case QPNP_ID_LED_MPP: |
| 2274 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2275 | break; |
| 2276 | case QPNP_ID_RGB_RED: |
| 2277 | case QPNP_ID_RGB_GREEN: |
| 2278 | case QPNP_ID_RGB_BLUE: |
| 2279 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2280 | break; |
| 2281 | case QPNP_ID_KPDBL: |
| 2282 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2283 | break; |
| 2284 | default: |
| 2285 | dev_err(&led->pdev->dev, |
| 2286 | "Invalid LED id type for pause hi\n"); |
| 2287 | return -EINVAL; |
| 2288 | } |
| 2289 | |
| 2290 | if (pwm_cfg->mode == LPG_MODE) |
| 2291 | pwm_cfg->blinking = true; |
| 2292 | |
| 2293 | previous_pause_hi = pwm_cfg->lut_params.lut_pause_hi; |
| 2294 | |
| 2295 | pwm_free(pwm_cfg->pwm_dev); |
| 2296 | pwm_cfg->lut_params.lut_pause_hi = pause_hi; |
| 2297 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2298 | if (ret) { |
| 2299 | pwm_cfg->lut_params.lut_pause_hi = previous_pause_hi; |
| 2300 | pwm_free(pwm_cfg->pwm_dev); |
| 2301 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2302 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2303 | dev_err(&led->pdev->dev, |
| 2304 | "Failed to initialize pwm with new pause hi value\n"); |
| 2305 | return ret; |
| 2306 | } |
| 2307 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2308 | return count; |
| 2309 | } |
| 2310 | |
| 2311 | static ssize_t start_idx_store(struct device *dev, |
| 2312 | struct device_attribute *attr, |
| 2313 | const char *buf, size_t count) |
| 2314 | { |
| 2315 | struct qpnp_led_data *led; |
| 2316 | u32 start_idx; |
| 2317 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2318 | ssize_t ret; |
| 2319 | u32 previous_start_idx; |
| 2320 | struct pwm_config_data *pwm_cfg; |
| 2321 | |
| 2322 | ret = kstrtou32(buf, 10, &start_idx); |
| 2323 | if (ret) |
| 2324 | return ret; |
| 2325 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2326 | |
| 2327 | switch (led->id) { |
| 2328 | case QPNP_ID_LED_MPP: |
| 2329 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2330 | break; |
| 2331 | case QPNP_ID_RGB_RED: |
| 2332 | case QPNP_ID_RGB_GREEN: |
| 2333 | case QPNP_ID_RGB_BLUE: |
| 2334 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2335 | break; |
| 2336 | case QPNP_ID_KPDBL: |
| 2337 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2338 | break; |
| 2339 | default: |
| 2340 | dev_err(&led->pdev->dev, |
| 2341 | "Invalid LED id type for start idx\n"); |
| 2342 | return -EINVAL; |
| 2343 | } |
| 2344 | |
| 2345 | if (pwm_cfg->mode == LPG_MODE) |
| 2346 | pwm_cfg->blinking = true; |
| 2347 | |
| 2348 | previous_start_idx = pwm_cfg->duty_cycles->start_idx; |
| 2349 | pwm_cfg->duty_cycles->start_idx = start_idx; |
| 2350 | pwm_cfg->lut_params.start_idx = pwm_cfg->duty_cycles->start_idx; |
| 2351 | pwm_free(pwm_cfg->pwm_dev); |
| 2352 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2353 | if (ret) { |
| 2354 | pwm_cfg->duty_cycles->start_idx = previous_start_idx; |
| 2355 | pwm_cfg->lut_params.start_idx = pwm_cfg->duty_cycles->start_idx; |
| 2356 | pwm_free(pwm_cfg->pwm_dev); |
| 2357 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2358 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2359 | dev_err(&led->pdev->dev, |
| 2360 | "Failed to initialize pwm with new start idx value\n"); |
| 2361 | return ret; |
| 2362 | } |
| 2363 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2364 | return count; |
| 2365 | } |
| 2366 | |
| 2367 | static ssize_t ramp_step_ms_store(struct device *dev, |
| 2368 | struct device_attribute *attr, |
| 2369 | const char *buf, size_t count) |
| 2370 | { |
| 2371 | struct qpnp_led_data *led; |
| 2372 | u32 ramp_step_ms; |
| 2373 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2374 | ssize_t ret; |
| 2375 | u32 previous_ramp_step_ms; |
| 2376 | struct pwm_config_data *pwm_cfg; |
| 2377 | |
| 2378 | ret = kstrtou32(buf, 10, &ramp_step_ms); |
| 2379 | if (ret) |
| 2380 | return ret; |
| 2381 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2382 | |
| 2383 | switch (led->id) { |
| 2384 | case QPNP_ID_LED_MPP: |
| 2385 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2386 | break; |
| 2387 | case QPNP_ID_RGB_RED: |
| 2388 | case QPNP_ID_RGB_GREEN: |
| 2389 | case QPNP_ID_RGB_BLUE: |
| 2390 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2391 | break; |
| 2392 | case QPNP_ID_KPDBL: |
| 2393 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2394 | break; |
| 2395 | default: |
| 2396 | dev_err(&led->pdev->dev, |
| 2397 | "Invalid LED id type for ramp step\n"); |
| 2398 | return -EINVAL; |
| 2399 | } |
| 2400 | |
| 2401 | if (pwm_cfg->mode == LPG_MODE) |
| 2402 | pwm_cfg->blinking = true; |
| 2403 | |
| 2404 | previous_ramp_step_ms = pwm_cfg->lut_params.ramp_step_ms; |
| 2405 | |
| 2406 | pwm_free(pwm_cfg->pwm_dev); |
| 2407 | pwm_cfg->lut_params.ramp_step_ms = ramp_step_ms; |
| 2408 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2409 | if (ret) { |
| 2410 | pwm_cfg->lut_params.ramp_step_ms = previous_ramp_step_ms; |
| 2411 | pwm_free(pwm_cfg->pwm_dev); |
| 2412 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2413 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2414 | dev_err(&led->pdev->dev, |
| 2415 | "Failed to initialize pwm with new ramp step value\n"); |
| 2416 | return ret; |
| 2417 | } |
| 2418 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2419 | return count; |
| 2420 | } |
| 2421 | |
| 2422 | static ssize_t lut_flags_store(struct device *dev, |
| 2423 | struct device_attribute *attr, |
| 2424 | const char *buf, size_t count) |
| 2425 | { |
| 2426 | struct qpnp_led_data *led; |
| 2427 | u32 lut_flags; |
| 2428 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2429 | ssize_t ret; |
| 2430 | u32 previous_lut_flags; |
| 2431 | struct pwm_config_data *pwm_cfg; |
| 2432 | |
| 2433 | ret = kstrtou32(buf, 10, &lut_flags); |
| 2434 | if (ret) |
| 2435 | return ret; |
| 2436 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2437 | |
| 2438 | switch (led->id) { |
| 2439 | case QPNP_ID_LED_MPP: |
| 2440 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2441 | break; |
| 2442 | case QPNP_ID_RGB_RED: |
| 2443 | case QPNP_ID_RGB_GREEN: |
| 2444 | case QPNP_ID_RGB_BLUE: |
| 2445 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2446 | break; |
| 2447 | case QPNP_ID_KPDBL: |
| 2448 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2449 | break; |
| 2450 | default: |
| 2451 | dev_err(&led->pdev->dev, |
| 2452 | "Invalid LED id type for lut flags\n"); |
| 2453 | return -EINVAL; |
| 2454 | } |
| 2455 | |
| 2456 | if (pwm_cfg->mode == LPG_MODE) |
| 2457 | pwm_cfg->blinking = true; |
| 2458 | |
| 2459 | previous_lut_flags = pwm_cfg->lut_params.flags; |
| 2460 | |
| 2461 | pwm_free(pwm_cfg->pwm_dev); |
| 2462 | pwm_cfg->lut_params.flags = lut_flags; |
| 2463 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2464 | if (ret) { |
| 2465 | pwm_cfg->lut_params.flags = previous_lut_flags; |
| 2466 | pwm_free(pwm_cfg->pwm_dev); |
| 2467 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2468 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2469 | dev_err(&led->pdev->dev, |
| 2470 | "Failed to initialize pwm with new lut flags value\n"); |
| 2471 | return ret; |
| 2472 | } |
| 2473 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2474 | return count; |
| 2475 | } |
| 2476 | |
| 2477 | static ssize_t duty_pcts_store(struct device *dev, |
| 2478 | struct device_attribute *attr, |
| 2479 | const char *buf, size_t count) |
| 2480 | { |
| 2481 | struct qpnp_led_data *led; |
| 2482 | int num_duty_pcts = 0; |
| 2483 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2484 | char *buffer; |
| 2485 | ssize_t ret; |
| 2486 | int i = 0; |
| 2487 | int max_duty_pcts; |
| 2488 | struct pwm_config_data *pwm_cfg; |
| 2489 | u32 previous_num_duty_pcts; |
| 2490 | int value; |
| 2491 | int *previous_duty_pcts; |
| 2492 | |
| 2493 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2494 | |
| 2495 | switch (led->id) { |
| 2496 | case QPNP_ID_LED_MPP: |
| 2497 | pwm_cfg = led->mpp_cfg->pwm_cfg; |
| 2498 | max_duty_pcts = PWM_LUT_MAX_SIZE; |
| 2499 | break; |
| 2500 | case QPNP_ID_RGB_RED: |
| 2501 | case QPNP_ID_RGB_GREEN: |
| 2502 | case QPNP_ID_RGB_BLUE: |
| 2503 | pwm_cfg = led->rgb_cfg->pwm_cfg; |
| 2504 | max_duty_pcts = PWM_LUT_MAX_SIZE; |
| 2505 | break; |
| 2506 | case QPNP_ID_KPDBL: |
| 2507 | pwm_cfg = led->kpdbl_cfg->pwm_cfg; |
| 2508 | max_duty_pcts = PWM_GPLED_LUT_MAX_SIZE; |
| 2509 | break; |
| 2510 | default: |
| 2511 | dev_err(&led->pdev->dev, |
| 2512 | "Invalid LED id type for duty pcts\n"); |
| 2513 | return -EINVAL; |
| 2514 | } |
| 2515 | |
| 2516 | if (pwm_cfg->mode == LPG_MODE) |
| 2517 | pwm_cfg->blinking = true; |
| 2518 | |
| 2519 | buffer = (char *)buf; |
| 2520 | |
| 2521 | for (i = 0; i < max_duty_pcts; i++) { |
| 2522 | if (buffer == NULL) |
| 2523 | break; |
| 2524 | ret = sscanf((const char *)buffer, "%u,%s", &value, buffer); |
| 2525 | pwm_cfg->old_duty_pcts[i] = value; |
| 2526 | num_duty_pcts++; |
| 2527 | if (ret <= 1) |
| 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | if (num_duty_pcts >= max_duty_pcts) { |
| 2532 | dev_err(&led->pdev->dev, |
| 2533 | "Number of duty pcts given exceeds max (%d)\n", |
| 2534 | max_duty_pcts); |
| 2535 | return -EINVAL; |
| 2536 | } |
| 2537 | |
| 2538 | previous_num_duty_pcts = pwm_cfg->duty_cycles->num_duty_pcts; |
| 2539 | previous_duty_pcts = pwm_cfg->duty_cycles->duty_pcts; |
| 2540 | |
| 2541 | pwm_cfg->duty_cycles->num_duty_pcts = num_duty_pcts; |
| 2542 | pwm_cfg->duty_cycles->duty_pcts = pwm_cfg->old_duty_pcts; |
| 2543 | pwm_cfg->old_duty_pcts = previous_duty_pcts; |
| 2544 | pwm_cfg->lut_params.idx_len = pwm_cfg->duty_cycles->num_duty_pcts; |
| 2545 | |
| 2546 | pwm_free(pwm_cfg->pwm_dev); |
| 2547 | ret = qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2548 | if (ret) |
| 2549 | goto restore; |
| 2550 | |
| 2551 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2552 | return count; |
| 2553 | |
| 2554 | restore: |
| 2555 | dev_err(&led->pdev->dev, |
| 2556 | "Failed to initialize pwm with new duty pcts value\n"); |
| 2557 | pwm_cfg->duty_cycles->num_duty_pcts = previous_num_duty_pcts; |
| 2558 | pwm_cfg->old_duty_pcts = pwm_cfg->duty_cycles->duty_pcts; |
| 2559 | pwm_cfg->duty_cycles->duty_pcts = previous_duty_pcts; |
| 2560 | pwm_cfg->lut_params.idx_len = pwm_cfg->duty_cycles->num_duty_pcts; |
| 2561 | pwm_free(pwm_cfg->pwm_dev); |
| 2562 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2563 | qpnp_led_set(&led->cdev, led->cdev.brightness); |
| 2564 | return ret; |
| 2565 | } |
| 2566 | |
| 2567 | static void led_blink(struct qpnp_led_data *led, |
| 2568 | struct pwm_config_data *pwm_cfg) |
| 2569 | { |
| 2570 | int rc; |
| 2571 | |
| 2572 | flush_work(&led->work); |
| 2573 | mutex_lock(&led->lock); |
| 2574 | if (pwm_cfg->use_blink) { |
| 2575 | if (led->cdev.brightness) { |
| 2576 | pwm_cfg->blinking = true; |
| 2577 | if (led->id == QPNP_ID_LED_MPP) |
| 2578 | led->mpp_cfg->pwm_mode = LPG_MODE; |
| 2579 | else if (led->id == QPNP_ID_KPDBL) |
| 2580 | led->kpdbl_cfg->pwm_mode = LPG_MODE; |
| 2581 | pwm_cfg->mode = LPG_MODE; |
| 2582 | } else { |
| 2583 | pwm_cfg->blinking = false; |
| 2584 | pwm_cfg->mode = pwm_cfg->default_mode; |
| 2585 | if (led->id == QPNP_ID_LED_MPP) |
| 2586 | led->mpp_cfg->pwm_mode = pwm_cfg->default_mode; |
| 2587 | else if (led->id == QPNP_ID_KPDBL) |
| 2588 | led->kpdbl_cfg->pwm_mode = |
| 2589 | pwm_cfg->default_mode; |
| 2590 | } |
| 2591 | pwm_free(pwm_cfg->pwm_dev); |
| 2592 | qpnp_pwm_init(pwm_cfg, led->pdev, led->cdev.name); |
| 2593 | if (led->id == QPNP_ID_RGB_RED || led->id == QPNP_ID_RGB_GREEN |
| 2594 | || led->id == QPNP_ID_RGB_BLUE) { |
| 2595 | rc = qpnp_rgb_set(led); |
| 2596 | if (rc < 0) |
| 2597 | dev_err(&led->pdev->dev, |
| 2598 | "RGB set brightness failed (%d)\n", rc); |
| 2599 | } else if (led->id == QPNP_ID_LED_MPP) { |
| 2600 | rc = qpnp_mpp_set(led); |
| 2601 | if (rc < 0) |
| 2602 | dev_err(&led->pdev->dev, |
| 2603 | "MPP set brightness failed (%d)\n", rc); |
| 2604 | } else if (led->id == QPNP_ID_KPDBL) { |
| 2605 | rc = qpnp_kpdbl_set(led); |
| 2606 | if (rc < 0) |
| 2607 | dev_err(&led->pdev->dev, |
| 2608 | "KPDBL set brightness failed (%d)\n", rc); |
| 2609 | } |
| 2610 | } |
| 2611 | mutex_unlock(&led->lock); |
| 2612 | } |
| 2613 | |
| 2614 | static ssize_t blink_store(struct device *dev, |
| 2615 | struct device_attribute *attr, |
| 2616 | const char *buf, size_t count) |
| 2617 | { |
| 2618 | struct qpnp_led_data *led; |
| 2619 | unsigned long blinking; |
| 2620 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 2621 | ssize_t ret = -EINVAL; |
| 2622 | |
| 2623 | ret = kstrtoul(buf, 10, &blinking); |
| 2624 | if (ret) |
| 2625 | return ret; |
| 2626 | led = container_of(led_cdev, struct qpnp_led_data, cdev); |
| 2627 | led->cdev.brightness = blinking ? led->cdev.max_brightness : 0; |
| 2628 | |
| 2629 | switch (led->id) { |
| 2630 | case QPNP_ID_LED_MPP: |
| 2631 | led_blink(led, led->mpp_cfg->pwm_cfg); |
| 2632 | break; |
| 2633 | case QPNP_ID_RGB_RED: |
| 2634 | case QPNP_ID_RGB_GREEN: |
| 2635 | case QPNP_ID_RGB_BLUE: |
| 2636 | led_blink(led, led->rgb_cfg->pwm_cfg); |
| 2637 | break; |
| 2638 | case QPNP_ID_KPDBL: |
| 2639 | led_blink(led, led->kpdbl_cfg->pwm_cfg); |
| 2640 | break; |
| 2641 | default: |
| 2642 | dev_err(&led->pdev->dev, "Invalid LED id type for blink\n"); |
| 2643 | return -EINVAL; |
| 2644 | } |
| 2645 | return count; |
| 2646 | } |
| 2647 | |
| 2648 | static DEVICE_ATTR(led_mode, 0664, NULL, led_mode_store); |
| 2649 | static DEVICE_ATTR(strobe, 0664, NULL, led_strobe_type_store); |
| 2650 | static DEVICE_ATTR(pwm_us, 0664, NULL, pwm_us_store); |
| 2651 | static DEVICE_ATTR(pause_lo, 0664, NULL, pause_lo_store); |
| 2652 | static DEVICE_ATTR(pause_hi, 0664, NULL, pause_hi_store); |
| 2653 | static DEVICE_ATTR(start_idx, 0664, NULL, start_idx_store); |
| 2654 | static DEVICE_ATTR(ramp_step_ms, 0664, NULL, ramp_step_ms_store); |
| 2655 | static DEVICE_ATTR(lut_flags, 0664, NULL, lut_flags_store); |
| 2656 | static DEVICE_ATTR(duty_pcts, 0664, NULL, duty_pcts_store); |
| 2657 | static DEVICE_ATTR(blink, 0664, NULL, blink_store); |
| 2658 | |
| 2659 | static struct attribute *led_attrs[] = { |
| 2660 | &dev_attr_led_mode.attr, |
| 2661 | &dev_attr_strobe.attr, |
| 2662 | NULL |
| 2663 | }; |
| 2664 | |
| 2665 | static const struct attribute_group led_attr_group = { |
| 2666 | .attrs = led_attrs, |
| 2667 | }; |
| 2668 | |
| 2669 | static struct attribute *pwm_attrs[] = { |
| 2670 | &dev_attr_pwm_us.attr, |
| 2671 | NULL |
| 2672 | }; |
| 2673 | |
| 2674 | static struct attribute *lpg_attrs[] = { |
| 2675 | &dev_attr_pause_lo.attr, |
| 2676 | &dev_attr_pause_hi.attr, |
| 2677 | &dev_attr_start_idx.attr, |
| 2678 | &dev_attr_ramp_step_ms.attr, |
| 2679 | &dev_attr_lut_flags.attr, |
| 2680 | &dev_attr_duty_pcts.attr, |
| 2681 | NULL |
| 2682 | }; |
| 2683 | |
| 2684 | static struct attribute *blink_attrs[] = { |
| 2685 | &dev_attr_blink.attr, |
| 2686 | NULL |
| 2687 | }; |
| 2688 | |
| 2689 | static const struct attribute_group pwm_attr_group = { |
| 2690 | .attrs = pwm_attrs, |
| 2691 | }; |
| 2692 | |
| 2693 | static const struct attribute_group lpg_attr_group = { |
| 2694 | .attrs = lpg_attrs, |
| 2695 | }; |
| 2696 | |
| 2697 | static const struct attribute_group blink_attr_group = { |
| 2698 | .attrs = blink_attrs, |
| 2699 | }; |
| 2700 | |
| 2701 | static int qpnp_flash_init(struct qpnp_led_data *led) |
| 2702 | { |
| 2703 | int rc; |
| 2704 | |
| 2705 | led->flash_cfg->flash_on = false; |
| 2706 | |
| 2707 | rc = qpnp_led_masked_write(led, |
| 2708 | FLASH_LED_STROBE_CTRL(led->base), |
| 2709 | FLASH_STROBE_MASK, FLASH_DISABLE_ALL); |
| 2710 | if (rc) { |
| 2711 | dev_err(&led->pdev->dev, |
| 2712 | "LED %d flash write failed(%d)\n", led->id, rc); |
| 2713 | return rc; |
| 2714 | } |
| 2715 | |
| 2716 | /* Disable flash LED module */ |
| 2717 | rc = qpnp_led_masked_write(led, FLASH_ENABLE_CONTROL(led->base), |
| 2718 | FLASH_ENABLE_MASK, FLASH_DISABLE_ALL); |
| 2719 | if (rc) { |
| 2720 | dev_err(&led->pdev->dev, "Enable reg write failed(%d)\n", rc); |
| 2721 | return rc; |
| 2722 | } |
| 2723 | |
| 2724 | if (led->flash_cfg->torch_enable) |
| 2725 | return 0; |
| 2726 | |
| 2727 | /* Set headroom */ |
| 2728 | rc = qpnp_led_masked_write(led, FLASH_HEADROOM(led->base), |
| 2729 | FLASH_HEADROOM_MASK, led->flash_cfg->headroom); |
| 2730 | if (rc) { |
| 2731 | dev_err(&led->pdev->dev, |
| 2732 | "Headroom reg write failed(%d)\n", rc); |
| 2733 | return rc; |
| 2734 | } |
| 2735 | |
| 2736 | /* Set startup delay */ |
| 2737 | rc = qpnp_led_masked_write(led, |
| 2738 | FLASH_STARTUP_DELAY(led->base), FLASH_STARTUP_DLY_MASK, |
| 2739 | led->flash_cfg->startup_dly); |
| 2740 | if (rc) { |
| 2741 | dev_err(&led->pdev->dev, |
| 2742 | "Startup delay reg write failed(%d)\n", rc); |
| 2743 | return rc; |
| 2744 | } |
| 2745 | |
| 2746 | /* Set timer control - safety or watchdog */ |
| 2747 | if (led->flash_cfg->safety_timer) { |
| 2748 | rc = qpnp_led_masked_write(led, |
| 2749 | FLASH_LED_TMR_CTRL(led->base), |
| 2750 | FLASH_TMR_MASK, FLASH_TMR_SAFETY); |
| 2751 | if (rc) { |
| 2752 | dev_err(&led->pdev->dev, |
| 2753 | "LED timer ctrl reg write failed(%d)\n", |
| 2754 | rc); |
| 2755 | return rc; |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | /* Set Vreg force */ |
| 2760 | if (led->flash_cfg->vreg_ok) |
| 2761 | rc = qpnp_led_masked_write(led, FLASH_VREG_OK_FORCE(led->base), |
| 2762 | FLASH_VREG_MASK, FLASH_SW_VREG_OK); |
| 2763 | else |
| 2764 | rc = qpnp_led_masked_write(led, FLASH_VREG_OK_FORCE(led->base), |
| 2765 | FLASH_VREG_MASK, FLASH_HW_VREG_OK); |
| 2766 | |
| 2767 | if (rc) { |
| 2768 | dev_err(&led->pdev->dev, |
| 2769 | "Vreg OK reg write failed(%d)\n", rc); |
| 2770 | return rc; |
| 2771 | } |
| 2772 | |
| 2773 | /* Set self fault check */ |
| 2774 | rc = qpnp_led_masked_write(led, FLASH_FAULT_DETECT(led->base), |
| 2775 | FLASH_FAULT_DETECT_MASK, FLASH_SELFCHECK_ENABLE); |
| 2776 | if (rc) { |
| 2777 | dev_err(&led->pdev->dev, |
| 2778 | "Fault detect reg write failed(%d)\n", rc); |
| 2779 | return rc; |
| 2780 | } |
| 2781 | |
| 2782 | /* Set mask enable */ |
| 2783 | rc = qpnp_led_masked_write(led, FLASH_MASK_ENABLE(led->base), |
| 2784 | FLASH_MASK_REG_MASK, FLASH_MASK_1); |
| 2785 | if (rc) { |
| 2786 | dev_err(&led->pdev->dev, |
| 2787 | "Mask enable reg write failed(%d)\n", rc); |
| 2788 | return rc; |
| 2789 | } |
| 2790 | |
| 2791 | /* Set current ramp */ |
| 2792 | rc = qpnp_led_masked_write(led, FLASH_CURRENT_RAMP(led->base), |
| 2793 | FLASH_CURRENT_RAMP_MASK, FLASH_RAMP_STEP_27US); |
| 2794 | if (rc) { |
| 2795 | dev_err(&led->pdev->dev, |
| 2796 | "Current ramp reg write failed(%d)\n", rc); |
| 2797 | return rc; |
| 2798 | } |
| 2799 | |
| 2800 | led->flash_cfg->strobe_type = 0; |
| 2801 | |
| 2802 | /* dump flash registers */ |
| 2803 | qpnp_dump_regs(led, flash_debug_regs, ARRAY_SIZE(flash_debug_regs)); |
| 2804 | |
| 2805 | return 0; |
| 2806 | } |
| 2807 | |
| 2808 | static int qpnp_kpdbl_init(struct qpnp_led_data *led) |
| 2809 | { |
| 2810 | int rc; |
| 2811 | uint val; |
| 2812 | |
| 2813 | /* select row source - vbst or vph */ |
| 2814 | rc = regmap_read(led->regmap, KPDBL_ROW_SRC_SEL(led->base), &val); |
| 2815 | if (rc) { |
| 2816 | dev_err(&led->pdev->dev, |
| 2817 | "Unable to read from addr=%x, rc(%d)\n", |
| 2818 | KPDBL_ROW_SRC_SEL(led->base), rc); |
| 2819 | return rc; |
| 2820 | } |
| 2821 | |
| 2822 | if (led->kpdbl_cfg->row_src_vbst) |
| 2823 | val |= 1 << led->kpdbl_cfg->row_id; |
| 2824 | else |
| 2825 | val &= ~(1 << led->kpdbl_cfg->row_id); |
| 2826 | |
| 2827 | rc = regmap_write(led->regmap, KPDBL_ROW_SRC_SEL(led->base), val); |
| 2828 | if (rc) { |
| 2829 | dev_err(&led->pdev->dev, |
| 2830 | "Unable to read from addr=%x, rc(%d)\n", |
| 2831 | KPDBL_ROW_SRC_SEL(led->base), rc); |
| 2832 | return rc; |
| 2833 | } |
| 2834 | |
| 2835 | /* row source enable */ |
| 2836 | rc = regmap_read(led->regmap, KPDBL_ROW_SRC(led->base), &val); |
| 2837 | if (rc) { |
| 2838 | dev_err(&led->pdev->dev, |
| 2839 | "Unable to read from addr=%x, rc(%d)\n", |
| 2840 | KPDBL_ROW_SRC(led->base), rc); |
| 2841 | return rc; |
| 2842 | } |
| 2843 | |
| 2844 | if (led->kpdbl_cfg->row_src_en) |
| 2845 | val |= KPDBL_ROW_SCAN_EN_MASK | (1 << led->kpdbl_cfg->row_id); |
| 2846 | else |
| 2847 | val &= ~(1 << led->kpdbl_cfg->row_id); |
| 2848 | |
| 2849 | rc = regmap_write(led->regmap, KPDBL_ROW_SRC(led->base), val); |
| 2850 | if (rc) { |
| 2851 | dev_err(&led->pdev->dev, |
| 2852 | "Unable to write to addr=%x, rc(%d)\n", |
| 2853 | KPDBL_ROW_SRC(led->base), rc); |
| 2854 | return rc; |
| 2855 | } |
| 2856 | |
| 2857 | /* enable module */ |
| 2858 | rc = qpnp_led_masked_write(led, KPDBL_ENABLE(led->base), |
| 2859 | KPDBL_MODULE_EN_MASK, KPDBL_MODULE_EN); |
| 2860 | if (rc) { |
| 2861 | dev_err(&led->pdev->dev, |
| 2862 | "Enable module write failed(%d)\n", rc); |
| 2863 | return rc; |
| 2864 | } |
| 2865 | |
| 2866 | rc = qpnp_pwm_init(led->kpdbl_cfg->pwm_cfg, led->pdev, |
| 2867 | led->cdev.name); |
| 2868 | if (rc) { |
| 2869 | dev_err(&led->pdev->dev, "Failed to initialize pwm\n"); |
| 2870 | return rc; |
| 2871 | } |
| 2872 | |
| 2873 | if (led->kpdbl_cfg->always_on) { |
| 2874 | kpdbl_master = led->kpdbl_cfg->pwm_cfg->pwm_dev; |
| 2875 | kpdbl_master_period_us = led->kpdbl_cfg->pwm_cfg->pwm_period_us; |
| 2876 | } |
| 2877 | |
| 2878 | /* dump kpdbl registers */ |
| 2879 | qpnp_dump_regs(led, kpdbl_debug_regs, ARRAY_SIZE(kpdbl_debug_regs)); |
| 2880 | |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
| 2884 | static int qpnp_rgb_init(struct qpnp_led_data *led) |
| 2885 | { |
| 2886 | int rc; |
| 2887 | |
| 2888 | rc = qpnp_led_masked_write(led, RGB_LED_SRC_SEL(led->base), |
| 2889 | RGB_LED_SRC_MASK, RGB_LED_SOURCE_VPH_PWR); |
| 2890 | if (rc) { |
| 2891 | dev_err(&led->pdev->dev, |
| 2892 | "Failed to write led source select register\n"); |
| 2893 | return rc; |
| 2894 | } |
| 2895 | |
| 2896 | rc = qpnp_pwm_init(led->rgb_cfg->pwm_cfg, led->pdev, led->cdev.name); |
| 2897 | if (rc) { |
| 2898 | dev_err(&led->pdev->dev, "Failed to initialize pwm\n"); |
| 2899 | return rc; |
| 2900 | } |
| 2901 | /* Initialize led for use in auto trickle charging mode */ |
| 2902 | rc = qpnp_led_masked_write(led, RGB_LED_ATC_CTL(led->base), |
| 2903 | led->rgb_cfg->enable, led->rgb_cfg->enable); |
| 2904 | |
| 2905 | return 0; |
| 2906 | } |
| 2907 | |
| 2908 | static int qpnp_mpp_init(struct qpnp_led_data *led) |
| 2909 | { |
| 2910 | int rc; |
| 2911 | u8 val; |
| 2912 | |
| 2913 | |
| 2914 | if (led->max_current < LED_MPP_CURRENT_MIN || |
| 2915 | led->max_current > LED_MPP_CURRENT_MAX) { |
| 2916 | dev_err(&led->pdev->dev, |
| 2917 | "max current for mpp is not valid\n"); |
| 2918 | return -EINVAL; |
| 2919 | } |
| 2920 | |
| 2921 | val = (led->mpp_cfg->current_setting / LED_MPP_CURRENT_PER_SETTING) - 1; |
| 2922 | |
| 2923 | if (val < 0) |
| 2924 | val = 0; |
| 2925 | |
| 2926 | rc = qpnp_led_masked_write(led, LED_MPP_VIN_CTRL(led->base), |
| 2927 | LED_MPP_VIN_MASK, led->mpp_cfg->vin_ctrl); |
| 2928 | if (rc) { |
| 2929 | dev_err(&led->pdev->dev, |
| 2930 | "Failed to write led vin control reg\n"); |
| 2931 | return rc; |
| 2932 | } |
| 2933 | |
| 2934 | rc = qpnp_led_masked_write(led, LED_MPP_SINK_CTRL(led->base), |
| 2935 | LED_MPP_SINK_MASK, val); |
| 2936 | if (rc) { |
| 2937 | dev_err(&led->pdev->dev, |
| 2938 | "Failed to write sink control reg\n"); |
| 2939 | return rc; |
| 2940 | } |
| 2941 | |
| 2942 | if (led->mpp_cfg->pwm_mode != MANUAL_MODE) { |
| 2943 | rc = qpnp_pwm_init(led->mpp_cfg->pwm_cfg, led->pdev, |
| 2944 | led->cdev.name); |
| 2945 | if (rc) { |
| 2946 | dev_err(&led->pdev->dev, |
| 2947 | "Failed to initialize pwm\n"); |
| 2948 | return rc; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static int qpnp_gpio_init(struct qpnp_led_data *led) |
| 2956 | { |
| 2957 | int rc; |
| 2958 | |
| 2959 | rc = qpnp_led_masked_write(led, LED_GPIO_VIN_CTRL(led->base), |
| 2960 | LED_GPIO_VIN_MASK, led->gpio_cfg->vin_ctrl); |
| 2961 | if (rc) { |
| 2962 | dev_err(&led->pdev->dev, |
| 2963 | "Failed to write led vin control reg\n"); |
| 2964 | return rc; |
| 2965 | } |
| 2966 | |
| 2967 | return 0; |
| 2968 | } |
| 2969 | |
| 2970 | static int qpnp_led_initialize(struct qpnp_led_data *led) |
| 2971 | { |
| 2972 | int rc = 0; |
| 2973 | |
| 2974 | switch (led->id) { |
| 2975 | case QPNP_ID_WLED: |
| 2976 | rc = qpnp_wled_init(led); |
| 2977 | if (rc) |
| 2978 | dev_err(&led->pdev->dev, |
| 2979 | "WLED initialize failed(%d)\n", rc); |
| 2980 | break; |
| 2981 | case QPNP_ID_FLASH1_LED0: |
| 2982 | case QPNP_ID_FLASH1_LED1: |
| 2983 | rc = qpnp_flash_init(led); |
| 2984 | if (rc) |
| 2985 | dev_err(&led->pdev->dev, |
| 2986 | "FLASH initialize failed(%d)\n", rc); |
| 2987 | break; |
| 2988 | case QPNP_ID_RGB_RED: |
| 2989 | case QPNP_ID_RGB_GREEN: |
| 2990 | case QPNP_ID_RGB_BLUE: |
| 2991 | rc = qpnp_rgb_init(led); |
| 2992 | if (rc) |
| 2993 | dev_err(&led->pdev->dev, |
| 2994 | "RGB initialize failed(%d)\n", rc); |
| 2995 | break; |
| 2996 | case QPNP_ID_LED_MPP: |
| 2997 | rc = qpnp_mpp_init(led); |
| 2998 | if (rc) |
| 2999 | dev_err(&led->pdev->dev, |
| 3000 | "MPP initialize failed(%d)\n", rc); |
| 3001 | break; |
| 3002 | case QPNP_ID_LED_GPIO: |
| 3003 | rc = qpnp_gpio_init(led); |
| 3004 | if (rc) |
| 3005 | dev_err(&led->pdev->dev, |
| 3006 | "GPIO initialize failed(%d)\n", rc); |
| 3007 | break; |
| 3008 | case QPNP_ID_KPDBL: |
| 3009 | rc = qpnp_kpdbl_init(led); |
| 3010 | if (rc) |
| 3011 | dev_err(&led->pdev->dev, |
| 3012 | "KPDBL initialize failed(%d)\n", rc); |
| 3013 | break; |
| 3014 | default: |
| 3015 | dev_err(&led->pdev->dev, "Invalid LED(%d)\n", led->id); |
| 3016 | return -EINVAL; |
| 3017 | } |
| 3018 | |
| 3019 | return rc; |
| 3020 | } |
| 3021 | |
| 3022 | static int qpnp_get_common_configs(struct qpnp_led_data *led, |
| 3023 | struct device_node *node) |
| 3024 | { |
| 3025 | int rc; |
| 3026 | u32 val; |
| 3027 | const char *temp_string; |
| 3028 | |
| 3029 | led->cdev.default_trigger = LED_TRIGGER_DEFAULT; |
| 3030 | rc = of_property_read_string(node, "linux,default-trigger", |
| 3031 | &temp_string); |
| 3032 | if (!rc) |
| 3033 | led->cdev.default_trigger = temp_string; |
| 3034 | else if (rc != -EINVAL) |
| 3035 | return rc; |
| 3036 | |
| 3037 | led->default_on = false; |
| 3038 | rc = of_property_read_string(node, "qcom,default-state", |
| 3039 | &temp_string); |
| 3040 | if (!rc) { |
| 3041 | if (strcmp(temp_string, "on") == 0) |
| 3042 | led->default_on = true; |
| 3043 | } else if (rc != -EINVAL) |
| 3044 | return rc; |
| 3045 | |
| 3046 | led->turn_off_delay_ms = 0; |
| 3047 | rc = of_property_read_u32(node, "qcom,turn-off-delay-ms", &val); |
| 3048 | if (!rc) |
| 3049 | led->turn_off_delay_ms = val; |
| 3050 | else if (rc != -EINVAL) |
| 3051 | return rc; |
| 3052 | |
| 3053 | return 0; |
| 3054 | } |
| 3055 | |
| 3056 | /* |
| 3057 | * Handlers for alternative sources of platform_data |
| 3058 | */ |
| 3059 | static int qpnp_get_config_wled(struct qpnp_led_data *led, |
| 3060 | struct device_node *node) |
| 3061 | { |
| 3062 | u32 val; |
| 3063 | uint tmp; |
| 3064 | int rc; |
| 3065 | |
| 3066 | led->wled_cfg = devm_kzalloc(&led->pdev->dev, |
| 3067 | sizeof(struct wled_config_data), GFP_KERNEL); |
| 3068 | if (!led->wled_cfg) |
| 3069 | return -ENOMEM; |
| 3070 | |
| 3071 | rc = regmap_read(led->regmap, PMIC_VERSION_REG, &tmp); |
| 3072 | if (rc) { |
| 3073 | dev_err(&led->pdev->dev, |
| 3074 | "Unable to read pmic ver, rc(%d)\n", rc); |
| 3075 | } |
| 3076 | led->wled_cfg->pmic_version = (u8)tmp; |
| 3077 | |
| 3078 | led->wled_cfg->num_strings = WLED_DEFAULT_STRINGS; |
| 3079 | rc = of_property_read_u32(node, "qcom,num-strings", &val); |
| 3080 | if (!rc) |
| 3081 | led->wled_cfg->num_strings = (u8) val; |
| 3082 | else if (rc != -EINVAL) |
| 3083 | return rc; |
| 3084 | |
| 3085 | led->wled_cfg->num_physical_strings = led->wled_cfg->num_strings; |
| 3086 | rc = of_property_read_u32(node, "qcom,num-physical-strings", &val); |
| 3087 | if (!rc) |
| 3088 | led->wled_cfg->num_physical_strings = (u8) val; |
| 3089 | else if (rc != -EINVAL) |
| 3090 | return rc; |
| 3091 | |
| 3092 | led->wled_cfg->ovp_val = WLED_DEFAULT_OVP_VAL; |
| 3093 | rc = of_property_read_u32(node, "qcom,ovp-val", &val); |
| 3094 | if (!rc) |
| 3095 | led->wled_cfg->ovp_val = (u8) val; |
| 3096 | else if (rc != -EINVAL) |
| 3097 | return rc; |
| 3098 | |
| 3099 | led->wled_cfg->boost_curr_lim = WLED_BOOST_LIM_DEFAULT; |
| 3100 | rc = of_property_read_u32(node, "qcom,boost-curr-lim", &val); |
| 3101 | if (!rc) |
| 3102 | led->wled_cfg->boost_curr_lim = (u8) val; |
| 3103 | else if (rc != -EINVAL) |
| 3104 | return rc; |
| 3105 | |
| 3106 | led->wled_cfg->cp_select = WLED_CP_SEL_DEFAULT; |
| 3107 | rc = of_property_read_u32(node, "qcom,cp-sel", &val); |
| 3108 | if (!rc) |
| 3109 | led->wled_cfg->cp_select = (u8) val; |
| 3110 | else if (rc != -EINVAL) |
| 3111 | return rc; |
| 3112 | |
| 3113 | led->wled_cfg->ctrl_delay_us = WLED_CTRL_DLY_DEFAULT; |
| 3114 | rc = of_property_read_u32(node, "qcom,ctrl-delay-us", &val); |
| 3115 | if (!rc) |
| 3116 | led->wled_cfg->ctrl_delay_us = (u8) val; |
| 3117 | else if (rc != -EINVAL) |
| 3118 | return rc; |
| 3119 | |
| 3120 | led->wled_cfg->op_fdbck = WLED_OP_FDBCK_DEFAULT; |
| 3121 | rc = of_property_read_u32(node, "qcom,op-fdbck", &val); |
| 3122 | if (!rc) |
| 3123 | led->wled_cfg->op_fdbck = (u8) val; |
| 3124 | else if (rc != -EINVAL) |
| 3125 | return rc; |
| 3126 | |
| 3127 | led->wled_cfg->switch_freq = WLED_SWITCH_FREQ_DEFAULT; |
| 3128 | rc = of_property_read_u32(node, "qcom,switch-freq", &val); |
| 3129 | if (!rc) |
| 3130 | led->wled_cfg->switch_freq = (u8) val; |
| 3131 | else if (rc != -EINVAL) |
| 3132 | return rc; |
| 3133 | |
| 3134 | led->wled_cfg->dig_mod_gen_en = |
| 3135 | of_property_read_bool(node, "qcom,dig-mod-gen-en"); |
| 3136 | |
| 3137 | led->wled_cfg->cs_out_en = |
| 3138 | of_property_read_bool(node, "qcom,cs-out-en"); |
| 3139 | |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
| 3143 | static int qpnp_get_config_flash(struct qpnp_led_data *led, |
| 3144 | struct device_node *node, bool *reg_set) |
| 3145 | { |
| 3146 | int rc; |
| 3147 | u32 val; |
| 3148 | uint tmp; |
| 3149 | |
| 3150 | led->flash_cfg = devm_kzalloc(&led->pdev->dev, |
| 3151 | sizeof(struct flash_config_data), GFP_KERNEL); |
| 3152 | if (!led->flash_cfg) |
| 3153 | return -ENOMEM; |
| 3154 | |
| 3155 | rc = regmap_read(led->regmap, FLASH_PERIPHERAL_SUBTYPE(led->base), |
| 3156 | &tmp); |
| 3157 | if (rc) { |
| 3158 | dev_err(&led->pdev->dev, |
| 3159 | "Unable to read from addr=%x, rc(%d)\n", |
| 3160 | FLASH_PERIPHERAL_SUBTYPE(led->base), rc); |
| 3161 | } |
| 3162 | led->flash_cfg->peripheral_subtype = (u8)tmp; |
| 3163 | |
| 3164 | led->flash_cfg->torch_enable = |
| 3165 | of_property_read_bool(node, "qcom,torch-enable"); |
| 3166 | |
| 3167 | led->flash_cfg->no_smbb_support = |
| 3168 | of_property_read_bool(node, "qcom,no-smbb-support"); |
| 3169 | |
| 3170 | if (of_find_property(of_get_parent(node), "flash-wa-supply", |
| 3171 | NULL) && (!*reg_set)) { |
| 3172 | led->flash_cfg->flash_wa_reg = |
| 3173 | devm_regulator_get(&led->pdev->dev, "flash-wa"); |
| 3174 | if (IS_ERR_OR_NULL(led->flash_cfg->flash_wa_reg)) { |
| 3175 | rc = PTR_ERR(led->flash_cfg->flash_wa_reg); |
| 3176 | if (rc != EPROBE_DEFER) { |
| 3177 | dev_err(&led->pdev->dev, |
| 3178 | "Flash wa regulator get failed(%d)\n", |
| 3179 | rc); |
| 3180 | } |
| 3181 | } else { |
| 3182 | led->flash_cfg->flash_wa_reg_get = true; |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | if (led->id == QPNP_ID_FLASH1_LED0) { |
| 3187 | led->flash_cfg->enable_module = FLASH_ENABLE_LED_0; |
| 3188 | led->flash_cfg->current_addr = FLASH_LED_0_CURR(led->base); |
| 3189 | led->flash_cfg->trigger_flash = FLASH_LED_0_OUTPUT; |
| 3190 | if (!*reg_set) { |
| 3191 | led->flash_cfg->flash_boost_reg = |
| 3192 | regulator_get(&led->pdev->dev, |
| 3193 | "flash-boost"); |
| 3194 | if (IS_ERR(led->flash_cfg->flash_boost_reg)) { |
| 3195 | rc = PTR_ERR(led->flash_cfg->flash_boost_reg); |
| 3196 | dev_err(&led->pdev->dev, |
| 3197 | "Regulator get failed(%d)\n", rc); |
| 3198 | goto error_get_flash_reg; |
| 3199 | } |
| 3200 | led->flash_cfg->flash_reg_get = true; |
| 3201 | *reg_set = true; |
| 3202 | } else |
| 3203 | led->flash_cfg->flash_reg_get = false; |
| 3204 | |
| 3205 | if (led->flash_cfg->torch_enable) { |
| 3206 | led->flash_cfg->second_addr = |
| 3207 | FLASH_LED_1_CURR(led->base); |
| 3208 | } |
| 3209 | } else if (led->id == QPNP_ID_FLASH1_LED1) { |
| 3210 | led->flash_cfg->enable_module = FLASH_ENABLE_LED_1; |
| 3211 | led->flash_cfg->current_addr = FLASH_LED_1_CURR(led->base); |
| 3212 | led->flash_cfg->trigger_flash = FLASH_LED_1_OUTPUT; |
| 3213 | if (!*reg_set) { |
| 3214 | led->flash_cfg->flash_boost_reg = |
| 3215 | regulator_get(&led->pdev->dev, |
| 3216 | "flash-boost"); |
| 3217 | if (IS_ERR(led->flash_cfg->flash_boost_reg)) { |
| 3218 | rc = PTR_ERR(led->flash_cfg->flash_boost_reg); |
| 3219 | dev_err(&led->pdev->dev, |
| 3220 | "Regulator get failed(%d)\n", rc); |
| 3221 | goto error_get_flash_reg; |
| 3222 | } |
| 3223 | led->flash_cfg->flash_reg_get = true; |
| 3224 | *reg_set = true; |
| 3225 | } else |
| 3226 | led->flash_cfg->flash_reg_get = false; |
| 3227 | |
| 3228 | if (led->flash_cfg->torch_enable) { |
| 3229 | led->flash_cfg->second_addr = |
| 3230 | FLASH_LED_0_CURR(led->base); |
| 3231 | } |
| 3232 | } else { |
| 3233 | dev_err(&led->pdev->dev, "Unknown flash LED name given\n"); |
| 3234 | return -EINVAL; |
| 3235 | } |
| 3236 | |
| 3237 | if (led->flash_cfg->torch_enable) { |
| 3238 | if (of_find_property(of_get_parent(node), "torch-boost-supply", |
| 3239 | NULL)) { |
| 3240 | if (!led->flash_cfg->no_smbb_support) { |
| 3241 | led->flash_cfg->torch_boost_reg = |
| 3242 | regulator_get(&led->pdev->dev, |
| 3243 | "torch-boost"); |
| 3244 | if (IS_ERR(led->flash_cfg->torch_boost_reg)) { |
| 3245 | rc = PTR_ERR(led->flash_cfg-> |
| 3246 | torch_boost_reg); |
| 3247 | dev_err(&led->pdev->dev, |
| 3248 | "Torch regulator get failed(%d)\n", rc); |
| 3249 | goto error_get_torch_reg; |
| 3250 | } |
| 3251 | } |
| 3252 | led->flash_cfg->enable_module = FLASH_ENABLE_MODULE; |
| 3253 | } else |
| 3254 | led->flash_cfg->enable_module = FLASH_ENABLE_ALL; |
| 3255 | led->flash_cfg->trigger_flash = FLASH_TORCH_OUTPUT; |
| 3256 | |
| 3257 | rc = of_property_read_u32(node, "qcom,duration", &val); |
| 3258 | if (!rc) |
| 3259 | led->flash_cfg->duration = ((u8) val) - 2; |
| 3260 | else if (rc == -EINVAL) |
| 3261 | led->flash_cfg->duration = TORCH_DURATION_12s; |
| 3262 | else { |
| 3263 | if (led->flash_cfg->peripheral_subtype == |
| 3264 | FLASH_SUBTYPE_SINGLE) |
| 3265 | goto error_get_flash_reg; |
| 3266 | else if (led->flash_cfg->peripheral_subtype == |
| 3267 | FLASH_SUBTYPE_DUAL) |
| 3268 | goto error_get_torch_reg; |
| 3269 | } |
| 3270 | |
| 3271 | rc = of_property_read_u32(node, "qcom,current", &val); |
| 3272 | if (!rc) |
| 3273 | led->flash_cfg->current_prgm = (val * |
| 3274 | TORCH_MAX_LEVEL / led->max_current); |
| 3275 | else { |
| 3276 | if (led->flash_cfg->peripheral_subtype == |
| 3277 | FLASH_SUBTYPE_SINGLE) |
| 3278 | goto error_get_flash_reg; |
| 3279 | else if (led->flash_cfg->peripheral_subtype == |
| 3280 | FLASH_SUBTYPE_DUAL) |
| 3281 | goto error_get_torch_reg; |
| 3282 | goto error_get_torch_reg; |
| 3283 | } |
| 3284 | |
| 3285 | return 0; |
| 3286 | } |
| 3287 | |
| 3288 | rc = of_property_read_u32(node, "qcom,duration", &val); |
| 3289 | if (!rc) |
| 3290 | led->flash_cfg->duration = (u8)((val - 10) / 10); |
| 3291 | else if (rc == -EINVAL) |
| 3292 | led->flash_cfg->duration = FLASH_DURATION_200ms; |
| 3293 | else |
| 3294 | goto error_get_flash_reg; |
| 3295 | |
| 3296 | rc = of_property_read_u32(node, "qcom,current", &val); |
| 3297 | if (!rc) |
| 3298 | led->flash_cfg->current_prgm = val * FLASH_MAX_LEVEL |
| 3299 | / led->max_current; |
| 3300 | else |
| 3301 | goto error_get_flash_reg; |
| 3302 | |
| 3303 | rc = of_property_read_u32(node, "qcom,headroom", &val); |
| 3304 | if (!rc) |
| 3305 | led->flash_cfg->headroom = (u8) val; |
| 3306 | else if (rc == -EINVAL) |
| 3307 | led->flash_cfg->headroom = HEADROOM_500mV; |
| 3308 | else |
| 3309 | goto error_get_flash_reg; |
| 3310 | |
| 3311 | rc = of_property_read_u32(node, "qcom,clamp-curr", &val); |
| 3312 | if (!rc) |
| 3313 | led->flash_cfg->clamp_curr = (val * |
| 3314 | FLASH_MAX_LEVEL / led->max_current); |
| 3315 | else if (rc == -EINVAL) |
| 3316 | led->flash_cfg->clamp_curr = FLASH_CLAMP_200mA; |
| 3317 | else |
| 3318 | goto error_get_flash_reg; |
| 3319 | |
| 3320 | rc = of_property_read_u32(node, "qcom,startup-dly", &val); |
| 3321 | if (!rc) |
| 3322 | led->flash_cfg->startup_dly = (u8) val; |
| 3323 | else if (rc == -EINVAL) |
| 3324 | led->flash_cfg->startup_dly = DELAY_128us; |
| 3325 | else |
| 3326 | goto error_get_flash_reg; |
| 3327 | |
| 3328 | led->flash_cfg->safety_timer = |
| 3329 | of_property_read_bool(node, "qcom,safety-timer"); |
| 3330 | |
| 3331 | led->flash_cfg->vreg_ok = |
| 3332 | of_property_read_bool(node, "qcom,sw_vreg_ok"); |
| 3333 | |
| 3334 | return 0; |
| 3335 | |
| 3336 | error_get_torch_reg: |
| 3337 | if (led->flash_cfg->no_smbb_support) |
| 3338 | regulator_put(led->flash_cfg->flash_boost_reg); |
| 3339 | else |
| 3340 | regulator_put(led->flash_cfg->torch_boost_reg); |
| 3341 | |
| 3342 | error_get_flash_reg: |
| 3343 | regulator_put(led->flash_cfg->flash_boost_reg); |
| 3344 | return rc; |
| 3345 | |
| 3346 | } |
| 3347 | |
| 3348 | static int qpnp_get_config_pwm(struct pwm_config_data *pwm_cfg, |
| 3349 | struct platform_device *pdev, |
| 3350 | struct device_node *node) |
| 3351 | { |
| 3352 | struct property *prop; |
| 3353 | int rc, i, lut_max_size; |
| 3354 | u32 val; |
| 3355 | u8 *temp_cfg; |
| 3356 | const char *led_label; |
| 3357 | |
| 3358 | pwm_cfg->pwm_dev = of_pwm_get(node, NULL); |
| 3359 | |
| 3360 | if (IS_ERR(pwm_cfg->pwm_dev)) { |
| 3361 | rc = PTR_ERR(pwm_cfg->pwm_dev); |
| 3362 | dev_err(&pdev->dev, "Cannot get PWM device rc:(%d)\n", rc); |
| 3363 | pwm_cfg->pwm_dev = NULL; |
| 3364 | return rc; |
| 3365 | } |
| 3366 | |
| 3367 | if (pwm_cfg->mode != MANUAL_MODE) { |
| 3368 | rc = of_property_read_u32(node, "qcom,pwm-us", &val); |
| 3369 | if (!rc) |
| 3370 | pwm_cfg->pwm_period_us = val; |
| 3371 | else |
| 3372 | return rc; |
| 3373 | } |
| 3374 | |
| 3375 | pwm_cfg->use_blink = |
| 3376 | of_property_read_bool(node, "qcom,use-blink"); |
| 3377 | |
| 3378 | if (pwm_cfg->mode == LPG_MODE || pwm_cfg->use_blink) { |
| 3379 | pwm_cfg->duty_cycles = |
| 3380 | devm_kzalloc(&pdev->dev, |
| 3381 | sizeof(struct pwm_duty_cycles), GFP_KERNEL); |
| 3382 | if (!pwm_cfg->duty_cycles) { |
| 3383 | dev_err(&pdev->dev, "Unable to allocate memory\n"); |
| 3384 | rc = -ENOMEM; |
| 3385 | goto bad_lpg_params; |
| 3386 | } |
| 3387 | |
| 3388 | prop = of_find_property(node, "qcom,duty-pcts", |
| 3389 | &pwm_cfg->duty_cycles->num_duty_pcts); |
| 3390 | if (!prop) { |
| 3391 | dev_err(&pdev->dev, "Looking up property node qcom,duty-pcts failed\n"); |
| 3392 | rc = -ENODEV; |
| 3393 | goto bad_lpg_params; |
| 3394 | } else if (!pwm_cfg->duty_cycles->num_duty_pcts) { |
| 3395 | dev_err(&pdev->dev, "Invalid length of duty pcts\n"); |
| 3396 | rc = -EINVAL; |
| 3397 | goto bad_lpg_params; |
| 3398 | } |
| 3399 | |
| 3400 | rc = of_property_read_string(node, "label", &led_label); |
| 3401 | |
| 3402 | if (rc < 0) { |
| 3403 | dev_err(&pdev->dev, |
| 3404 | "Failure reading label, rc = %d\n", rc); |
| 3405 | return rc; |
| 3406 | } |
| 3407 | |
| 3408 | if (strcmp(led_label, "kpdbl") == 0) |
| 3409 | lut_max_size = PWM_GPLED_LUT_MAX_SIZE; |
| 3410 | else |
| 3411 | lut_max_size = PWM_LUT_MAX_SIZE; |
| 3412 | |
| 3413 | pwm_cfg->duty_cycles->duty_pcts = |
| 3414 | devm_kzalloc(&pdev->dev, |
| 3415 | sizeof(int) * lut_max_size, |
| 3416 | GFP_KERNEL); |
| 3417 | if (!pwm_cfg->duty_cycles->duty_pcts) { |
| 3418 | dev_err(&pdev->dev, "Unable to allocate memory\n"); |
| 3419 | rc = -ENOMEM; |
| 3420 | goto bad_lpg_params; |
| 3421 | } |
| 3422 | |
| 3423 | pwm_cfg->old_duty_pcts = |
| 3424 | devm_kzalloc(&pdev->dev, |
| 3425 | sizeof(int) * lut_max_size, |
| 3426 | GFP_KERNEL); |
| 3427 | if (!pwm_cfg->old_duty_pcts) { |
| 3428 | dev_err(&pdev->dev, "Unable to allocate memory\n"); |
| 3429 | rc = -ENOMEM; |
| 3430 | goto bad_lpg_params; |
| 3431 | } |
| 3432 | |
| 3433 | temp_cfg = devm_kzalloc(&pdev->dev, |
| 3434 | pwm_cfg->duty_cycles->num_duty_pcts * |
| 3435 | sizeof(u8), GFP_KERNEL); |
| 3436 | if (!temp_cfg) { |
| 3437 | dev_err(&pdev->dev, "Failed to allocate memory for duty pcts\n"); |
| 3438 | rc = -ENOMEM; |
| 3439 | goto bad_lpg_params; |
| 3440 | } |
| 3441 | |
| 3442 | memcpy(temp_cfg, prop->value, |
| 3443 | pwm_cfg->duty_cycles->num_duty_pcts); |
| 3444 | |
| 3445 | for (i = 0; i < pwm_cfg->duty_cycles->num_duty_pcts; i++) |
| 3446 | pwm_cfg->duty_cycles->duty_pcts[i] = |
| 3447 | (int) temp_cfg[i]; |
| 3448 | |
| 3449 | rc = of_property_read_u32(node, "qcom,start-idx", &val); |
| 3450 | if (!rc) { |
| 3451 | pwm_cfg->lut_params.start_idx = val; |
| 3452 | pwm_cfg->duty_cycles->start_idx = val; |
| 3453 | } else |
| 3454 | goto bad_lpg_params; |
| 3455 | |
| 3456 | pwm_cfg->lut_params.lut_pause_hi = 0; |
| 3457 | rc = of_property_read_u32(node, "qcom,pause-hi", &val); |
| 3458 | if (!rc) |
| 3459 | pwm_cfg->lut_params.lut_pause_hi = val; |
| 3460 | else if (rc != -EINVAL) |
| 3461 | goto bad_lpg_params; |
| 3462 | |
| 3463 | pwm_cfg->lut_params.lut_pause_lo = 0; |
| 3464 | rc = of_property_read_u32(node, "qcom,pause-lo", &val); |
| 3465 | if (!rc) |
| 3466 | pwm_cfg->lut_params.lut_pause_lo = val; |
| 3467 | else if (rc != -EINVAL) |
| 3468 | goto bad_lpg_params; |
| 3469 | |
| 3470 | pwm_cfg->lut_params.ramp_step_ms = |
| 3471 | QPNP_LUT_RAMP_STEP_DEFAULT; |
| 3472 | rc = of_property_read_u32(node, "qcom,ramp-step-ms", &val); |
| 3473 | if (!rc) |
| 3474 | pwm_cfg->lut_params.ramp_step_ms = val; |
| 3475 | else if (rc != -EINVAL) |
| 3476 | goto bad_lpg_params; |
| 3477 | |
| 3478 | pwm_cfg->lut_params.flags = QPNP_LED_PWM_FLAGS; |
| 3479 | rc = of_property_read_u32(node, "qcom,lut-flags", &val); |
| 3480 | if (!rc) |
| 3481 | pwm_cfg->lut_params.flags = (u8) val; |
| 3482 | else if (rc != -EINVAL) |
| 3483 | goto bad_lpg_params; |
| 3484 | |
| 3485 | pwm_cfg->lut_params.idx_len = |
| 3486 | pwm_cfg->duty_cycles->num_duty_pcts; |
| 3487 | } |
| 3488 | return 0; |
| 3489 | |
| 3490 | bad_lpg_params: |
| 3491 | pwm_cfg->use_blink = false; |
| 3492 | if (pwm_cfg->mode == PWM_MODE) { |
| 3493 | dev_err(&pdev->dev, "LPG parameters not set for blink mode, defaulting to PWM mode\n"); |
| 3494 | return 0; |
| 3495 | } |
| 3496 | return rc; |
| 3497 | }; |
| 3498 | |
| 3499 | static int qpnp_led_get_mode(const char *mode) |
| 3500 | { |
| 3501 | if (strcmp(mode, "manual") == 0) |
| 3502 | return MANUAL_MODE; |
| 3503 | else if (strcmp(mode, "pwm") == 0) |
| 3504 | return PWM_MODE; |
| 3505 | else if (strcmp(mode, "lpg") == 0) |
| 3506 | return LPG_MODE; |
| 3507 | else |
| 3508 | return -EINVAL; |
| 3509 | }; |
| 3510 | |
| 3511 | static int qpnp_get_config_kpdbl(struct qpnp_led_data *led, |
| 3512 | struct device_node *node) |
| 3513 | { |
| 3514 | int rc; |
| 3515 | u32 val; |
| 3516 | u8 led_mode; |
| 3517 | const char *mode; |
| 3518 | |
| 3519 | led->kpdbl_cfg = devm_kzalloc(&led->pdev->dev, |
| 3520 | sizeof(struct kpdbl_config_data), GFP_KERNEL); |
| 3521 | if (!led->kpdbl_cfg) |
| 3522 | return -ENOMEM; |
| 3523 | |
| 3524 | rc = of_property_read_string(node, "qcom,mode", &mode); |
| 3525 | if (!rc) { |
| 3526 | led_mode = qpnp_led_get_mode(mode); |
| 3527 | if ((led_mode == MANUAL_MODE) || (led_mode == -EINVAL)) { |
| 3528 | dev_err(&led->pdev->dev, "Selected mode not supported for kpdbl.\n"); |
| 3529 | return -EINVAL; |
| 3530 | } |
| 3531 | led->kpdbl_cfg->pwm_cfg = devm_kzalloc(&led->pdev->dev, |
| 3532 | sizeof(struct pwm_config_data), |
| 3533 | GFP_KERNEL); |
| 3534 | if (!led->kpdbl_cfg->pwm_cfg) |
| 3535 | return -ENOMEM; |
| 3536 | |
| 3537 | led->kpdbl_cfg->pwm_cfg->mode = led_mode; |
| 3538 | led->kpdbl_cfg->pwm_cfg->default_mode = led_mode; |
| 3539 | } else { |
| 3540 | return rc; |
| 3541 | } |
| 3542 | |
| 3543 | rc = qpnp_get_config_pwm(led->kpdbl_cfg->pwm_cfg, led->pdev, node); |
| 3544 | if (rc < 0) |
| 3545 | return rc; |
| 3546 | |
| 3547 | rc = of_property_read_u32(node, "qcom,row-id", &val); |
| 3548 | if (!rc) |
| 3549 | led->kpdbl_cfg->row_id = val; |
| 3550 | else |
| 3551 | return rc; |
| 3552 | |
| 3553 | led->kpdbl_cfg->row_src_vbst = |
| 3554 | of_property_read_bool(node, "qcom,row-src-vbst"); |
| 3555 | |
| 3556 | led->kpdbl_cfg->row_src_en = |
| 3557 | of_property_read_bool(node, "qcom,row-src-en"); |
| 3558 | |
| 3559 | led->kpdbl_cfg->always_on = |
| 3560 | of_property_read_bool(node, "qcom,always-on"); |
| 3561 | |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
| 3565 | static int qpnp_get_config_rgb(struct qpnp_led_data *led, |
| 3566 | struct device_node *node) |
| 3567 | { |
| 3568 | int rc; |
| 3569 | u8 led_mode; |
| 3570 | const char *mode; |
| 3571 | |
| 3572 | led->rgb_cfg = devm_kzalloc(&led->pdev->dev, |
| 3573 | sizeof(struct rgb_config_data), GFP_KERNEL); |
| 3574 | if (!led->rgb_cfg) |
| 3575 | return -ENOMEM; |
| 3576 | |
| 3577 | if (led->id == QPNP_ID_RGB_RED) |
| 3578 | led->rgb_cfg->enable = RGB_LED_ENABLE_RED; |
| 3579 | else if (led->id == QPNP_ID_RGB_GREEN) |
| 3580 | led->rgb_cfg->enable = RGB_LED_ENABLE_GREEN; |
| 3581 | else if (led->id == QPNP_ID_RGB_BLUE) |
| 3582 | led->rgb_cfg->enable = RGB_LED_ENABLE_BLUE; |
| 3583 | else |
| 3584 | return -EINVAL; |
| 3585 | |
| 3586 | rc = of_property_read_string(node, "qcom,mode", &mode); |
| 3587 | if (!rc) { |
| 3588 | led_mode = qpnp_led_get_mode(mode); |
| 3589 | if ((led_mode == MANUAL_MODE) || (led_mode == -EINVAL)) { |
| 3590 | dev_err(&led->pdev->dev, "Selected mode not supported for rgb\n"); |
| 3591 | return -EINVAL; |
| 3592 | } |
| 3593 | led->rgb_cfg->pwm_cfg = devm_kzalloc(&led->pdev->dev, |
| 3594 | sizeof(struct pwm_config_data), |
| 3595 | GFP_KERNEL); |
| 3596 | if (!led->rgb_cfg->pwm_cfg) { |
| 3597 | dev_err(&led->pdev->dev, |
| 3598 | "Unable to allocate memory\n"); |
| 3599 | return -ENOMEM; |
| 3600 | } |
| 3601 | led->rgb_cfg->pwm_cfg->mode = led_mode; |
| 3602 | led->rgb_cfg->pwm_cfg->default_mode = led_mode; |
| 3603 | } else { |
| 3604 | return rc; |
| 3605 | } |
| 3606 | |
| 3607 | rc = qpnp_get_config_pwm(led->rgb_cfg->pwm_cfg, led->pdev, node); |
| 3608 | if (rc < 0) |
| 3609 | return rc; |
| 3610 | |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
| 3614 | static int qpnp_get_config_mpp(struct qpnp_led_data *led, |
| 3615 | struct device_node *node) |
| 3616 | { |
| 3617 | int rc; |
| 3618 | u32 val; |
| 3619 | u8 led_mode; |
| 3620 | const char *mode; |
| 3621 | |
| 3622 | led->mpp_cfg = devm_kzalloc(&led->pdev->dev, |
| 3623 | sizeof(struct mpp_config_data), GFP_KERNEL); |
| 3624 | if (!led->mpp_cfg) |
| 3625 | return -ENOMEM; |
| 3626 | |
| 3627 | if (of_find_property(of_get_parent(node), "mpp-power-supply", NULL)) { |
| 3628 | led->mpp_cfg->mpp_reg = |
| 3629 | regulator_get(&led->pdev->dev, |
| 3630 | "mpp-power"); |
| 3631 | if (IS_ERR(led->mpp_cfg->mpp_reg)) { |
| 3632 | rc = PTR_ERR(led->mpp_cfg->mpp_reg); |
| 3633 | dev_err(&led->pdev->dev, |
| 3634 | "MPP regulator get failed(%d)\n", rc); |
| 3635 | return rc; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | if (led->mpp_cfg->mpp_reg) { |
| 3640 | rc = of_property_read_u32(of_get_parent(node), |
| 3641 | "qcom,mpp-power-max-voltage", &val); |
| 3642 | if (!rc) |
| 3643 | led->mpp_cfg->max_uV = val; |
| 3644 | else |
| 3645 | goto err_config_mpp; |
| 3646 | |
| 3647 | rc = of_property_read_u32(of_get_parent(node), |
| 3648 | "qcom,mpp-power-min-voltage", &val); |
| 3649 | if (!rc) |
| 3650 | led->mpp_cfg->min_uV = val; |
| 3651 | else |
| 3652 | goto err_config_mpp; |
| 3653 | } else { |
| 3654 | rc = of_property_read_u32(of_get_parent(node), |
| 3655 | "qcom,mpp-power-max-voltage", &val); |
| 3656 | if (!rc) |
| 3657 | dev_warn(&led->pdev->dev, "No regulator specified\n"); |
| 3658 | |
| 3659 | rc = of_property_read_u32(of_get_parent(node), |
| 3660 | "qcom,mpp-power-min-voltage", &val); |
| 3661 | if (!rc) |
| 3662 | dev_warn(&led->pdev->dev, "No regulator specified\n"); |
| 3663 | } |
| 3664 | |
| 3665 | led->mpp_cfg->current_setting = LED_MPP_CURRENT_MIN; |
| 3666 | rc = of_property_read_u32(node, "qcom,current-setting", &val); |
| 3667 | if (!rc) { |
| 3668 | if (led->mpp_cfg->current_setting < LED_MPP_CURRENT_MIN) |
| 3669 | led->mpp_cfg->current_setting = LED_MPP_CURRENT_MIN; |
| 3670 | else if (led->mpp_cfg->current_setting > LED_MPP_CURRENT_MAX) |
| 3671 | led->mpp_cfg->current_setting = LED_MPP_CURRENT_MAX; |
| 3672 | else |
| 3673 | led->mpp_cfg->current_setting = (u8) val; |
| 3674 | } else if (rc != -EINVAL) |
| 3675 | goto err_config_mpp; |
| 3676 | |
| 3677 | led->mpp_cfg->source_sel = LED_MPP_SOURCE_SEL_DEFAULT; |
| 3678 | rc = of_property_read_u32(node, "qcom,source-sel", &val); |
| 3679 | if (!rc) |
| 3680 | led->mpp_cfg->source_sel = (u8) val; |
| 3681 | else if (rc != -EINVAL) |
| 3682 | goto err_config_mpp; |
| 3683 | |
| 3684 | led->mpp_cfg->mode_ctrl = LED_MPP_MODE_SINK; |
| 3685 | rc = of_property_read_u32(node, "qcom,mode-ctrl", &val); |
| 3686 | if (!rc) |
| 3687 | led->mpp_cfg->mode_ctrl = (u8) val; |
| 3688 | else if (rc != -EINVAL) |
| 3689 | goto err_config_mpp; |
| 3690 | |
| 3691 | led->mpp_cfg->vin_ctrl = LED_MPP_VIN_CTRL_DEFAULT; |
| 3692 | rc = of_property_read_u32(node, "qcom,vin-ctrl", &val); |
| 3693 | if (!rc) |
| 3694 | led->mpp_cfg->vin_ctrl = (u8) val; |
| 3695 | else if (rc != -EINVAL) |
| 3696 | goto err_config_mpp; |
| 3697 | |
| 3698 | led->mpp_cfg->min_brightness = 0; |
| 3699 | rc = of_property_read_u32(node, "qcom,min-brightness", &val); |
| 3700 | if (!rc) |
| 3701 | led->mpp_cfg->min_brightness = (u8) val; |
| 3702 | else if (rc != -EINVAL) |
| 3703 | goto err_config_mpp; |
| 3704 | |
| 3705 | rc = of_property_read_string(node, "qcom,mode", &mode); |
| 3706 | if (!rc) { |
| 3707 | led_mode = qpnp_led_get_mode(mode); |
| 3708 | led->mpp_cfg->pwm_mode = led_mode; |
| 3709 | if (led_mode == MANUAL_MODE) |
| 3710 | return MANUAL_MODE; |
| 3711 | else if (led_mode == -EINVAL) { |
| 3712 | dev_err(&led->pdev->dev, "Selected mode not supported for mpp\n"); |
| 3713 | rc = -EINVAL; |
| 3714 | goto err_config_mpp; |
| 3715 | } |
| 3716 | led->mpp_cfg->pwm_cfg = devm_kzalloc(&led->pdev->dev, |
| 3717 | sizeof(struct pwm_config_data), |
| 3718 | GFP_KERNEL); |
| 3719 | if (!led->mpp_cfg->pwm_cfg) { |
| 3720 | dev_err(&led->pdev->dev, |
| 3721 | "Unable to allocate memory\n"); |
| 3722 | rc = -ENOMEM; |
| 3723 | goto err_config_mpp; |
| 3724 | } |
| 3725 | led->mpp_cfg->pwm_cfg->mode = led_mode; |
| 3726 | led->mpp_cfg->pwm_cfg->default_mode = led_mode; |
| 3727 | } else { |
| 3728 | return rc; |
| 3729 | } |
| 3730 | |
| 3731 | rc = qpnp_get_config_pwm(led->mpp_cfg->pwm_cfg, led->pdev, node); |
| 3732 | if (rc < 0) |
| 3733 | goto err_config_mpp; |
| 3734 | |
| 3735 | return 0; |
| 3736 | |
| 3737 | err_config_mpp: |
| 3738 | if (led->mpp_cfg->mpp_reg) |
| 3739 | regulator_put(led->mpp_cfg->mpp_reg); |
| 3740 | return rc; |
| 3741 | } |
| 3742 | |
| 3743 | static int qpnp_get_config_gpio(struct qpnp_led_data *led, |
| 3744 | struct device_node *node) |
| 3745 | { |
| 3746 | int rc; |
| 3747 | u32 val; |
| 3748 | |
| 3749 | led->gpio_cfg = devm_kzalloc(&led->pdev->dev, |
| 3750 | sizeof(struct gpio_config_data), GFP_KERNEL); |
| 3751 | if (!led->gpio_cfg) |
| 3752 | return -ENOMEM; |
| 3753 | |
| 3754 | led->gpio_cfg->source_sel = LED_GPIO_SOURCE_SEL_DEFAULT; |
| 3755 | rc = of_property_read_u32(node, "qcom,source-sel", &val); |
| 3756 | if (!rc) |
| 3757 | led->gpio_cfg->source_sel = (u8) val; |
| 3758 | else if (rc != -EINVAL) |
| 3759 | goto err_config_gpio; |
| 3760 | |
| 3761 | led->gpio_cfg->mode_ctrl = LED_GPIO_MODE_OUTPUT; |
| 3762 | rc = of_property_read_u32(node, "qcom,mode-ctrl", &val); |
| 3763 | if (!rc) |
| 3764 | led->gpio_cfg->mode_ctrl = (u8) val; |
| 3765 | else if (rc != -EINVAL) |
| 3766 | goto err_config_gpio; |
| 3767 | |
| 3768 | led->gpio_cfg->vin_ctrl = LED_GPIO_VIN_CTRL_DEFAULT; |
| 3769 | rc = of_property_read_u32(node, "qcom,vin-ctrl", &val); |
| 3770 | if (!rc) |
| 3771 | led->gpio_cfg->vin_ctrl = (u8) val; |
| 3772 | else if (rc != -EINVAL) |
| 3773 | goto err_config_gpio; |
| 3774 | |
| 3775 | return 0; |
| 3776 | |
| 3777 | err_config_gpio: |
| 3778 | return rc; |
| 3779 | } |
| 3780 | |
| 3781 | static int qpnp_leds_probe(struct platform_device *pdev) |
| 3782 | { |
| 3783 | struct qpnp_led_data *led, *led_array; |
| 3784 | unsigned int base; |
| 3785 | struct device_node *node, *temp; |
| 3786 | int rc, i, num_leds = 0, parsed_leds = 0; |
| 3787 | const char *led_label; |
| 3788 | bool regulator_probe = false; |
| 3789 | |
| 3790 | node = pdev->dev.of_node; |
| 3791 | if (node == NULL) |
| 3792 | return -ENODEV; |
| 3793 | |
| 3794 | temp = NULL; |
| 3795 | while ((temp = of_get_next_child(node, temp))) |
| 3796 | num_leds++; |
| 3797 | |
| 3798 | if (!num_leds) |
| 3799 | return -ECHILD; |
| 3800 | |
| 3801 | led_array = devm_kcalloc(&pdev->dev, num_leds, sizeof(*led_array), |
| 3802 | GFP_KERNEL); |
| 3803 | if (!led_array) |
| 3804 | return -ENOMEM; |
| 3805 | |
| 3806 | for_each_child_of_node(node, temp) { |
| 3807 | led = &led_array[parsed_leds]; |
| 3808 | led->num_leds = num_leds; |
| 3809 | led->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 3810 | if (!led->regmap) { |
| 3811 | dev_err(&pdev->dev, "Couldn't get parent's regmap\n"); |
| 3812 | return -EINVAL; |
| 3813 | } |
| 3814 | led->pdev = pdev; |
| 3815 | |
| 3816 | rc = of_property_read_u32(pdev->dev.of_node, "reg", &base); |
| 3817 | if (rc < 0) { |
| 3818 | dev_err(&pdev->dev, |
| 3819 | "Couldn't find reg in node = %s rc = %d\n", |
| 3820 | pdev->dev.of_node->full_name, rc); |
| 3821 | goto fail_id_check; |
| 3822 | } |
| 3823 | led->base = base; |
| 3824 | |
| 3825 | rc = of_property_read_string(temp, "label", &led_label); |
| 3826 | if (rc < 0) { |
| 3827 | dev_err(&led->pdev->dev, |
| 3828 | "Failure reading label, rc = %d\n", rc); |
| 3829 | goto fail_id_check; |
| 3830 | } |
| 3831 | |
| 3832 | rc = of_property_read_string(temp, "linux,name", |
| 3833 | &led->cdev.name); |
| 3834 | if (rc < 0) { |
| 3835 | dev_err(&led->pdev->dev, |
| 3836 | "Failure reading led name, rc = %d\n", rc); |
| 3837 | goto fail_id_check; |
| 3838 | } |
| 3839 | |
| 3840 | rc = of_property_read_u32(temp, "qcom,max-current", |
| 3841 | &led->max_current); |
| 3842 | if (rc < 0) { |
| 3843 | dev_err(&led->pdev->dev, |
| 3844 | "Failure reading max_current, rc = %d\n", rc); |
| 3845 | goto fail_id_check; |
| 3846 | } |
| 3847 | |
| 3848 | rc = of_property_read_u32(temp, "qcom,id", &led->id); |
| 3849 | if (rc < 0) { |
| 3850 | dev_err(&led->pdev->dev, |
| 3851 | "Failure reading led id, rc = %d\n", rc); |
| 3852 | goto fail_id_check; |
| 3853 | } |
| 3854 | |
| 3855 | rc = qpnp_get_common_configs(led, temp); |
| 3856 | if (rc) { |
| 3857 | dev_err(&led->pdev->dev, "Failure reading common led configuration, rc = %d\n", |
| 3858 | rc); |
| 3859 | goto fail_id_check; |
| 3860 | } |
| 3861 | |
| 3862 | led->cdev.brightness_set = qpnp_led_set; |
| 3863 | led->cdev.brightness_get = qpnp_led_get; |
| 3864 | |
| 3865 | if (strcmp(led_label, "wled") == 0) { |
| 3866 | rc = qpnp_get_config_wled(led, temp); |
| 3867 | if (rc < 0) { |
| 3868 | dev_err(&led->pdev->dev, |
| 3869 | "Unable to read wled config data\n"); |
| 3870 | goto fail_id_check; |
| 3871 | } |
| 3872 | } else if (strcmp(led_label, "flash") == 0) { |
| 3873 | if (!of_find_property(node, "flash-boost-supply", NULL)) |
| 3874 | regulator_probe = true; |
| 3875 | rc = qpnp_get_config_flash(led, temp, ®ulator_probe); |
| 3876 | if (rc < 0) { |
| 3877 | dev_err(&led->pdev->dev, |
| 3878 | "Unable to read flash config data\n"); |
| 3879 | goto fail_id_check; |
| 3880 | } |
| 3881 | } else if (strcmp(led_label, "rgb") == 0) { |
| 3882 | rc = qpnp_get_config_rgb(led, temp); |
| 3883 | if (rc < 0) { |
| 3884 | dev_err(&led->pdev->dev, |
| 3885 | "Unable to read rgb config data\n"); |
| 3886 | goto fail_id_check; |
| 3887 | } |
| 3888 | } else if (strcmp(led_label, "mpp") == 0) { |
| 3889 | rc = qpnp_get_config_mpp(led, temp); |
| 3890 | if (rc < 0) { |
| 3891 | dev_err(&led->pdev->dev, |
| 3892 | "Unable to read mpp config data\n"); |
| 3893 | goto fail_id_check; |
| 3894 | } |
| 3895 | } else if (strcmp(led_label, "gpio") == 0) { |
| 3896 | rc = qpnp_get_config_gpio(led, temp); |
| 3897 | if (rc < 0) { |
| 3898 | dev_err(&led->pdev->dev, |
| 3899 | "Unable to read gpio config data\n"); |
| 3900 | goto fail_id_check; |
| 3901 | } |
| 3902 | } else if (strcmp(led_label, "kpdbl") == 0) { |
| 3903 | bitmap_zero(kpdbl_leds_in_use, NUM_KPDBL_LEDS); |
| 3904 | is_kpdbl_master_turn_on = false; |
| 3905 | rc = qpnp_get_config_kpdbl(led, temp); |
| 3906 | if (rc < 0) { |
| 3907 | dev_err(&led->pdev->dev, |
| 3908 | "Unable to read kpdbl config data\n"); |
| 3909 | goto fail_id_check; |
| 3910 | } |
| 3911 | } else { |
| 3912 | dev_err(&led->pdev->dev, "No LED matching label\n"); |
| 3913 | rc = -EINVAL; |
| 3914 | goto fail_id_check; |
| 3915 | } |
| 3916 | |
| 3917 | if (led->id != QPNP_ID_FLASH1_LED0 && |
| 3918 | led->id != QPNP_ID_FLASH1_LED1) |
| 3919 | mutex_init(&led->lock); |
| 3920 | |
| 3921 | led->in_order_command_processing = of_property_read_bool |
| 3922 | (temp, "qcom,in-order-command-processing"); |
| 3923 | |
| 3924 | if (led->in_order_command_processing) { |
| 3925 | /* |
| 3926 | * the command order from user space needs to be |
| 3927 | * maintained use ordered workqueue to prevent |
| 3928 | * concurrency |
| 3929 | */ |
| 3930 | led->workqueue = alloc_ordered_workqueue |
| 3931 | ("led_workqueue", 0); |
| 3932 | if (!led->workqueue) { |
| 3933 | rc = -ENOMEM; |
| 3934 | goto fail_id_check; |
| 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | INIT_WORK(&led->work, qpnp_led_work); |
| 3939 | |
| 3940 | rc = qpnp_led_initialize(led); |
| 3941 | if (rc < 0) |
| 3942 | goto fail_id_check; |
| 3943 | |
| 3944 | rc = qpnp_led_set_max_brightness(led); |
| 3945 | if (rc < 0) |
| 3946 | goto fail_id_check; |
| 3947 | |
| 3948 | rc = led_classdev_register(&pdev->dev, &led->cdev); |
| 3949 | if (rc) { |
| 3950 | dev_err(&pdev->dev, |
| 3951 | "unable to register led %d,rc=%d\n", |
| 3952 | led->id, rc); |
| 3953 | goto fail_id_check; |
| 3954 | } |
| 3955 | |
| 3956 | if (led->id == QPNP_ID_FLASH1_LED0 || |
| 3957 | led->id == QPNP_ID_FLASH1_LED1) { |
| 3958 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3959 | &led_attr_group); |
| 3960 | if (rc) |
| 3961 | goto fail_id_check; |
| 3962 | |
| 3963 | } |
| 3964 | |
| 3965 | if (led->id == QPNP_ID_LED_MPP) { |
| 3966 | if (!led->mpp_cfg->pwm_cfg) |
| 3967 | break; |
| 3968 | if (led->mpp_cfg->pwm_cfg->mode == PWM_MODE) { |
| 3969 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3970 | &pwm_attr_group); |
| 3971 | if (rc) |
| 3972 | goto fail_id_check; |
| 3973 | } |
| 3974 | if (led->mpp_cfg->pwm_cfg->use_blink) { |
| 3975 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3976 | &blink_attr_group); |
| 3977 | if (rc) |
| 3978 | goto fail_id_check; |
| 3979 | |
| 3980 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3981 | &lpg_attr_group); |
| 3982 | if (rc) |
| 3983 | goto fail_id_check; |
| 3984 | } else if (led->mpp_cfg->pwm_cfg->mode == LPG_MODE) { |
| 3985 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3986 | &lpg_attr_group); |
| 3987 | if (rc) |
| 3988 | goto fail_id_check; |
| 3989 | } |
| 3990 | } else if ((led->id == QPNP_ID_RGB_RED) || |
| 3991 | (led->id == QPNP_ID_RGB_GREEN) || |
| 3992 | (led->id == QPNP_ID_RGB_BLUE)) { |
| 3993 | if (led->rgb_cfg->pwm_cfg->mode == PWM_MODE) { |
| 3994 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 3995 | &pwm_attr_group); |
| 3996 | if (rc) |
| 3997 | goto fail_id_check; |
| 3998 | } |
| 3999 | if (led->rgb_cfg->pwm_cfg->use_blink) { |
| 4000 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4001 | &blink_attr_group); |
| 4002 | if (rc) |
| 4003 | goto fail_id_check; |
| 4004 | |
| 4005 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4006 | &lpg_attr_group); |
| 4007 | if (rc) |
| 4008 | goto fail_id_check; |
| 4009 | } else if (led->rgb_cfg->pwm_cfg->mode == LPG_MODE) { |
| 4010 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4011 | &lpg_attr_group); |
| 4012 | if (rc) |
| 4013 | goto fail_id_check; |
| 4014 | } |
| 4015 | } else if (led->id == QPNP_ID_KPDBL) { |
| 4016 | if (led->kpdbl_cfg->pwm_cfg->mode == PWM_MODE) { |
| 4017 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4018 | &pwm_attr_group); |
| 4019 | if (rc) |
| 4020 | goto fail_id_check; |
| 4021 | } |
| 4022 | if (led->kpdbl_cfg->pwm_cfg->use_blink) { |
| 4023 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4024 | &blink_attr_group); |
| 4025 | if (rc) |
| 4026 | goto fail_id_check; |
| 4027 | |
| 4028 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4029 | &lpg_attr_group); |
| 4030 | if (rc) |
| 4031 | goto fail_id_check; |
| 4032 | } else if (led->kpdbl_cfg->pwm_cfg->mode == LPG_MODE) { |
| 4033 | rc = sysfs_create_group(&led->cdev.dev->kobj, |
| 4034 | &lpg_attr_group); |
| 4035 | if (rc) |
| 4036 | goto fail_id_check; |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | /* configure default state */ |
| 4041 | if (led->default_on) { |
| 4042 | led->cdev.brightness = led->cdev.max_brightness; |
| 4043 | __qpnp_led_work(led, led->cdev.brightness); |
| 4044 | if (led->turn_off_delay_ms > 0) |
| 4045 | qpnp_led_turn_off(led); |
| 4046 | } else |
| 4047 | led->cdev.brightness = LED_OFF; |
| 4048 | |
| 4049 | parsed_leds++; |
| 4050 | } |
| 4051 | dev_set_drvdata(&pdev->dev, led_array); |
| 4052 | return 0; |
| 4053 | |
| 4054 | fail_id_check: |
| 4055 | for (i = 0; i < parsed_leds; i++) { |
| 4056 | if (led_array[i].id != QPNP_ID_FLASH1_LED0 && |
| 4057 | led_array[i].id != QPNP_ID_FLASH1_LED1) |
| 4058 | mutex_destroy(&led_array[i].lock); |
| 4059 | if (led_array[i].in_order_command_processing) |
| 4060 | destroy_workqueue(led_array[i].workqueue); |
| 4061 | led_classdev_unregister(&led_array[i].cdev); |
| 4062 | } |
| 4063 | |
| 4064 | return rc; |
| 4065 | } |
| 4066 | |
| 4067 | static int qpnp_leds_remove(struct platform_device *pdev) |
| 4068 | { |
| 4069 | struct qpnp_led_data *led_array = dev_get_drvdata(&pdev->dev); |
| 4070 | int i, parsed_leds = led_array->num_leds; |
| 4071 | |
| 4072 | for (i = 0; i < parsed_leds; i++) { |
| 4073 | cancel_work_sync(&led_array[i].work); |
| 4074 | if (led_array[i].id != QPNP_ID_FLASH1_LED0 && |
| 4075 | led_array[i].id != QPNP_ID_FLASH1_LED1) |
| 4076 | mutex_destroy(&led_array[i].lock); |
| 4077 | |
| 4078 | if (led_array[i].in_order_command_processing) |
| 4079 | destroy_workqueue(led_array[i].workqueue); |
| 4080 | led_classdev_unregister(&led_array[i].cdev); |
| 4081 | switch (led_array[i].id) { |
| 4082 | case QPNP_ID_WLED: |
| 4083 | break; |
| 4084 | case QPNP_ID_FLASH1_LED0: |
| 4085 | case QPNP_ID_FLASH1_LED1: |
| 4086 | if (led_array[i].flash_cfg->flash_reg_get) |
| 4087 | regulator_put( |
| 4088 | led_array[i].flash_cfg->flash_boost_reg); |
| 4089 | if (led_array[i].flash_cfg->torch_enable) |
| 4090 | if (!led_array[i].flash_cfg->no_smbb_support) |
| 4091 | regulator_put(led_array[i]. |
| 4092 | flash_cfg->torch_boost_reg); |
| 4093 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4094 | &led_attr_group); |
| 4095 | break; |
| 4096 | case QPNP_ID_RGB_RED: |
| 4097 | case QPNP_ID_RGB_GREEN: |
| 4098 | case QPNP_ID_RGB_BLUE: |
| 4099 | if (led_array[i].rgb_cfg->pwm_cfg->mode == PWM_MODE) |
| 4100 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4101 | &pwm_attr_group); |
| 4102 | if (led_array[i].rgb_cfg->pwm_cfg->use_blink) { |
| 4103 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4104 | &blink_attr_group); |
| 4105 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4106 | &lpg_attr_group); |
| 4107 | } else if (led_array[i].rgb_cfg->pwm_cfg->mode |
| 4108 | == LPG_MODE) |
| 4109 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4110 | &lpg_attr_group); |
| 4111 | break; |
| 4112 | case QPNP_ID_LED_MPP: |
| 4113 | if (!led_array[i].mpp_cfg->pwm_cfg) |
| 4114 | break; |
| 4115 | if (led_array[i].mpp_cfg->pwm_cfg->mode == PWM_MODE) |
| 4116 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4117 | &pwm_attr_group); |
| 4118 | if (led_array[i].mpp_cfg->pwm_cfg->use_blink) { |
| 4119 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4120 | &blink_attr_group); |
| 4121 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4122 | &lpg_attr_group); |
| 4123 | } else if (led_array[i].mpp_cfg->pwm_cfg->mode |
| 4124 | == LPG_MODE) |
| 4125 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4126 | &lpg_attr_group); |
| 4127 | if (led_array[i].mpp_cfg->mpp_reg) |
| 4128 | regulator_put(led_array[i].mpp_cfg->mpp_reg); |
| 4129 | break; |
| 4130 | case QPNP_ID_KPDBL: |
| 4131 | if (led_array[i].kpdbl_cfg->pwm_cfg->mode == PWM_MODE) |
| 4132 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4133 | &pwm_attr_group); |
| 4134 | if (led_array[i].kpdbl_cfg->pwm_cfg->use_blink) { |
| 4135 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4136 | &blink_attr_group); |
| 4137 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4138 | &lpg_attr_group); |
| 4139 | } else if (led_array[i].kpdbl_cfg->pwm_cfg->mode |
| 4140 | == LPG_MODE) |
| 4141 | sysfs_remove_group(&led_array[i].cdev.dev->kobj, |
| 4142 | &lpg_attr_group); |
| 4143 | break; |
| 4144 | default: |
| 4145 | dev_err(&led_array->pdev->dev, |
| 4146 | "Invalid LED(%d)\n", |
| 4147 | led_array[i].id); |
| 4148 | return -EINVAL; |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
| 4155 | #ifdef CONFIG_OF |
| 4156 | static const struct of_device_id spmi_match_table[] = { |
| 4157 | { .compatible = "qcom,leds-qpnp",}, |
| 4158 | { }, |
| 4159 | }; |
| 4160 | #else |
| 4161 | #define spmi_match_table NULL |
| 4162 | #endif |
| 4163 | |
| 4164 | static struct platform_driver qpnp_leds_driver = { |
| 4165 | .driver = { |
| 4166 | .name = "qcom,leds-qpnp", |
| 4167 | .of_match_table = spmi_match_table, |
| 4168 | }, |
| 4169 | .probe = qpnp_leds_probe, |
| 4170 | .remove = qpnp_leds_remove, |
| 4171 | }; |
| 4172 | |
| 4173 | static int __init qpnp_led_init(void) |
| 4174 | { |
| 4175 | return platform_driver_register(&qpnp_leds_driver); |
| 4176 | } |
| 4177 | module_init(qpnp_led_init); |
| 4178 | |
| 4179 | static void __exit qpnp_led_exit(void) |
| 4180 | { |
| 4181 | platform_driver_unregister(&qpnp_leds_driver); |
| 4182 | } |
| 4183 | module_exit(qpnp_led_exit); |
| 4184 | |
| 4185 | MODULE_DESCRIPTION("QPNP LEDs driver"); |
| 4186 | MODULE_LICENSE("GPL v2"); |
| 4187 | MODULE_ALIAS("leds:leds-qpnp"); |
| 4188 | |