Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved. |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "haptics: %s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/atomic.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/hrtimer.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/log2.h> |
| 21 | #include <linux/leds.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/pwm.h> |
| 26 | #include <linux/regmap.h> |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/qpnp/qpnp-misc.h> |
| 30 | #include <linux/qpnp/qpnp-revid.h> |
| 31 | |
| 32 | /* Register definitions */ |
| 33 | #define HAP_STATUS_1_REG(chip) (chip->base + 0x0A) |
| 34 | #define HAP_BUSY_BIT BIT(1) |
| 35 | #define SC_FLAG_BIT BIT(3) |
| 36 | #define AUTO_RES_ERROR_BIT BIT(4) |
| 37 | |
| 38 | #define HAP_LRA_AUTO_RES_LO_REG(chip) (chip->base + 0x0B) |
| 39 | #define HAP_LRA_AUTO_RES_HI_REG(chip) (chip->base + 0x0C) |
| 40 | |
| 41 | #define HAP_INT_RT_STS_REG(chip) (chip->base + 0x10) |
| 42 | #define SC_INT_RT_STS_BIT BIT(0) |
| 43 | #define PLAY_INT_RT_STS_BIT BIT(1) |
| 44 | |
| 45 | #define HAP_EN_CTL_REG(chip) (chip->base + 0x46) |
| 46 | #define HAP_EN_BIT BIT(7) |
| 47 | |
| 48 | #define HAP_EN_CTL2_REG(chip) (chip->base + 0x48) |
| 49 | #define BRAKE_EN_BIT BIT(0) |
| 50 | |
| 51 | #define HAP_AUTO_RES_CTRL_REG(chip) (chip->base + 0x4B) |
| 52 | #define AUTO_RES_EN_BIT BIT(7) |
| 53 | #define AUTO_RES_ERR_RECOVERY_BIT BIT(3) |
| 54 | |
| 55 | #define HAP_CFG1_REG(chip) (chip->base + 0x4C) |
| 56 | #define HAP_ACT_TYPE_MASK BIT(0) |
| 57 | #define HAP_LRA 0 |
| 58 | #define HAP_ERM 1 |
| 59 | |
| 60 | #define HAP_CFG2_REG(chip) (chip->base + 0x4D) |
| 61 | #define HAP_WAVE_SINE 0 |
| 62 | #define HAP_WAVE_SQUARE 1 |
| 63 | #define HAP_LRA_RES_TYPE_MASK BIT(0) |
| 64 | |
| 65 | #define HAP_SEL_REG(chip) (chip->base + 0x4E) |
| 66 | #define HAP_WF_SOURCE_MASK GENMASK(5, 4) |
| 67 | #define HAP_WF_SOURCE_SHIFT 4 |
| 68 | |
| 69 | #define HAP_LRA_AUTO_RES_REG(chip) (chip->base + 0x4F) |
| 70 | /* For pmi8998 */ |
| 71 | #define LRA_AUTO_RES_MODE_MASK GENMASK(6, 4) |
| 72 | #define LRA_AUTO_RES_MODE_SHIFT 4 |
| 73 | #define LRA_HIGH_Z_MASK GENMASK(3, 2) |
| 74 | #define LRA_HIGH_Z_SHIFT 2 |
| 75 | #define LRA_RES_CAL_MASK GENMASK(1, 0) |
| 76 | #define HAP_RES_CAL_PERIOD_MIN 4 |
| 77 | #define HAP_RES_CAL_PERIOD_MAX 32 |
| 78 | /* For pm660 */ |
| 79 | #define PM660_AUTO_RES_MODE_BIT BIT(7) |
| 80 | #define PM660_AUTO_RES_MODE_SHIFT 7 |
| 81 | #define PM660_CAL_DURATION_MASK GENMASK(6, 5) |
| 82 | #define PM660_CAL_DURATION_SHIFT 5 |
| 83 | #define PM660_QWD_DRIVE_DURATION_BIT BIT(4) |
| 84 | #define PM660_QWD_DRIVE_DURATION_SHIFT 4 |
| 85 | #define PM660_CAL_EOP_BIT BIT(3) |
| 86 | #define PM660_CAL_EOP_SHIFT 3 |
| 87 | #define PM660_LRA_RES_CAL_MASK GENMASK(2, 0) |
| 88 | #define HAP_PM660_RES_CAL_PERIOD_MAX 256 |
| 89 | |
| 90 | #define HAP_VMAX_CFG_REG(chip) (chip->base + 0x51) |
| 91 | #define HAP_VMAX_OVD_BIT BIT(6) |
| 92 | #define HAP_VMAX_MASK GENMASK(5, 1) |
| 93 | #define HAP_VMAX_SHIFT 1 |
| 94 | #define HAP_VMAX_MIN_MV 116 |
| 95 | #define HAP_VMAX_MAX_MV 3596 |
| 96 | |
| 97 | #define HAP_ILIM_CFG_REG(chip) (chip->base + 0x52) |
| 98 | #define HAP_ILIM_SEL_MASK BIT(0) |
| 99 | #define HAP_ILIM_400_MA 0 |
| 100 | #define HAP_ILIM_800_MA 1 |
| 101 | |
| 102 | #define HAP_SC_DEB_REG(chip) (chip->base + 0x53) |
| 103 | #define HAP_SC_DEB_MASK GENMASK(2, 0) |
| 104 | #define HAP_SC_DEB_CYCLES_MIN 0 |
| 105 | #define HAP_DEF_SC_DEB_CYCLES 8 |
| 106 | #define HAP_SC_DEB_CYCLES_MAX 32 |
| 107 | |
| 108 | #define HAP_RATE_CFG1_REG(chip) (chip->base + 0x54) |
| 109 | #define HAP_RATE_CFG1_MASK GENMASK(7, 0) |
| 110 | |
| 111 | #define HAP_RATE_CFG2_REG(chip) (chip->base + 0x55) |
| 112 | #define HAP_RATE_CFG2_MASK GENMASK(3, 0) |
| 113 | /* Shift needed to convert drive period upper bits [11:8] */ |
| 114 | #define HAP_RATE_CFG2_SHIFT 8 |
| 115 | |
| 116 | #define HAP_INT_PWM_REG(chip) (chip->base + 0x56) |
| 117 | #define INT_PWM_FREQ_SEL_MASK GENMASK(1, 0) |
| 118 | #define INT_PWM_FREQ_253_KHZ 0 |
| 119 | #define INT_PWM_FREQ_505_KHZ 1 |
| 120 | #define INT_PWM_FREQ_739_KHZ 2 |
| 121 | #define INT_PWM_FREQ_1076_KHZ 3 |
| 122 | |
| 123 | #define HAP_EXT_PWM_REG(chip) (chip->base + 0x57) |
| 124 | #define EXT_PWM_FREQ_SEL_MASK GENMASK(1, 0) |
| 125 | #define EXT_PWM_FREQ_25_KHZ 0 |
| 126 | #define EXT_PWM_FREQ_50_KHZ 1 |
| 127 | #define EXT_PWM_FREQ_75_KHZ 2 |
| 128 | #define EXT_PWM_FREQ_100_KHZ 3 |
| 129 | |
| 130 | #define HAP_PWM_CAP_REG(chip) (chip->base + 0x58) |
| 131 | |
| 132 | #define HAP_SC_CLR_REG(chip) (chip->base + 0x59) |
| 133 | #define SC_CLR_BIT BIT(0) |
| 134 | |
| 135 | #define HAP_BRAKE_REG(chip) (chip->base + 0x5C) |
| 136 | #define HAP_BRAKE_PAT_MASK 0x3 |
| 137 | |
| 138 | #define HAP_WF_REPEAT_REG(chip) (chip->base + 0x5E) |
| 139 | #define WF_REPEAT_MASK GENMASK(6, 4) |
| 140 | #define WF_REPEAT_SHIFT 4 |
| 141 | #define WF_REPEAT_MIN 1 |
| 142 | #define WF_REPEAT_MAX 128 |
| 143 | #define WF_S_REPEAT_MASK GENMASK(1, 0) |
| 144 | #define WF_S_REPEAT_MIN 1 |
| 145 | #define WF_S_REPEAT_MAX 8 |
| 146 | |
| 147 | #define HAP_WF_S1_REG(chip) (chip->base + 0x60) |
| 148 | #define HAP_WF_SIGN_BIT BIT(7) |
| 149 | #define HAP_WF_OVD_BIT BIT(6) |
| 150 | #define HAP_WF_SAMP_MAX GENMASK(5, 1) |
| 151 | #define HAP_WF_SAMPLE_LEN 8 |
| 152 | |
| 153 | #define HAP_PLAY_REG(chip) (chip->base + 0x70) |
| 154 | #define PLAY_BIT BIT(7) |
| 155 | #define PAUSE_BIT BIT(0) |
| 156 | |
| 157 | #define HAP_SEC_ACCESS_REG(chip) (chip->base + 0xD0) |
| 158 | |
| 159 | #define HAP_TEST2_REG(chip) (chip->base + 0xE3) |
| 160 | #define HAP_EXT_PWM_DTEST_MASK GENMASK(6, 4) |
| 161 | #define HAP_EXT_PWM_DTEST_SHIFT 4 |
| 162 | #define PWM_MAX_DTEST_LINES 4 |
| 163 | #define HAP_EXT_PWM_PEAK_DATA 0x7F |
| 164 | #define HAP_EXT_PWM_HALF_DUTY 50 |
| 165 | #define HAP_EXT_PWM_FULL_DUTY 100 |
| 166 | #define HAP_EXT_PWM_DATA_FACTOR 39 |
| 167 | |
| 168 | /* Other definitions */ |
| 169 | #define HAP_BRAKE_PAT_LEN 4 |
| 170 | #define HAP_WAVE_SAMP_LEN 8 |
| 171 | #define NUM_WF_SET 4 |
| 172 | #define HAP_WAVE_SAMP_SET_LEN (HAP_WAVE_SAMP_LEN * NUM_WF_SET) |
| 173 | #define HAP_RATE_CFG_STEP_US 5 |
| 174 | #define HAP_WAVE_PLAY_RATE_US_MIN 0 |
| 175 | #define HAP_DEF_WAVE_PLAY_RATE_US 5715 |
| 176 | #define HAP_WAVE_PLAY_RATE_US_MAX 20475 |
| 177 | #define HAP_MAX_PLAY_TIME_MS 15000 |
| 178 | |
| 179 | enum hap_brake_pat { |
| 180 | NO_BRAKE = 0, |
| 181 | BRAKE_VMAX_4, |
| 182 | BRAKE_VMAX_2, |
| 183 | BRAKE_VMAX, |
| 184 | }; |
| 185 | |
| 186 | enum hap_auto_res_mode { |
| 187 | HAP_AUTO_RES_NONE, |
| 188 | HAP_AUTO_RES_ZXD, |
| 189 | HAP_AUTO_RES_QWD, |
| 190 | HAP_AUTO_RES_MAX_QWD, |
| 191 | HAP_AUTO_RES_ZXD_EOP, |
| 192 | }; |
| 193 | |
| 194 | enum hap_pm660_auto_res_mode { |
| 195 | HAP_PM660_AUTO_RES_ZXD, |
| 196 | HAP_PM660_AUTO_RES_QWD, |
| 197 | }; |
| 198 | |
| 199 | /* high Z option lines */ |
| 200 | enum hap_high_z { |
| 201 | HAP_LRA_HIGH_Z_NONE, /* opt0 for PM660 */ |
| 202 | HAP_LRA_HIGH_Z_OPT1, |
| 203 | HAP_LRA_HIGH_Z_OPT2, |
| 204 | HAP_LRA_HIGH_Z_OPT3, |
| 205 | }; |
| 206 | |
| 207 | /* play modes */ |
| 208 | enum hap_mode { |
| 209 | HAP_DIRECT, |
| 210 | HAP_BUFFER, |
| 211 | HAP_AUDIO, |
| 212 | HAP_PWM, |
| 213 | }; |
| 214 | |
| 215 | /* wave/sample repeat */ |
| 216 | enum hap_rep_type { |
| 217 | HAP_WAVE_REPEAT = 1, |
| 218 | HAP_WAVE_SAMP_REPEAT, |
| 219 | }; |
| 220 | |
| 221 | /* status flags */ |
| 222 | enum hap_status { |
| 223 | AUTO_RESONANCE_ENABLED = BIT(0), |
| 224 | }; |
| 225 | |
| 226 | enum hap_play_control { |
| 227 | HAP_STOP, |
| 228 | HAP_PAUSE, |
| 229 | HAP_PLAY, |
| 230 | }; |
| 231 | |
| 232 | /* pwm channel parameters */ |
| 233 | struct pwm_param { |
| 234 | struct pwm_device *pwm_dev; |
| 235 | u32 duty_us; |
| 236 | u32 period_us; |
| 237 | }; |
| 238 | |
| 239 | /* |
| 240 | * hap_lra_ares_param - Haptic auto_resonance parameters |
| 241 | * @ lra_qwd_drive_duration - LRA QWD drive duration |
| 242 | * @ calibrate_at_eop - Calibrate at EOP |
| 243 | * @ lra_res_cal_period - LRA resonance calibration period |
| 244 | * @ auto_res_mode - auto resonace mode |
| 245 | * @ lra_high_z - high z option line |
| 246 | */ |
| 247 | struct hap_lra_ares_param { |
| 248 | int lra_qwd_drive_duration; |
| 249 | int calibrate_at_eop; |
| 250 | enum hap_high_z lra_high_z; |
| 251 | u16 lra_res_cal_period; |
| 252 | u8 auto_res_mode; |
| 253 | }; |
| 254 | |
| 255 | /* |
| 256 | * hap_chip - Haptics data structure |
| 257 | * @ pdev - platform device pointer |
| 258 | * @ regmap - regmap pointer |
| 259 | * @ bus_lock - spin lock for bus read/write |
| 260 | * @ play_lock - mutex lock for haptics play/enable control |
| 261 | * @ haptics_work - haptics worker |
| 262 | * @ stop_timer - hrtimer for stopping haptics |
| 263 | * @ auto_res_err_poll_timer - hrtimer for auto-resonance error |
| 264 | * @ base - base address |
| 265 | * @ play_irq - irq for play |
| 266 | * @ sc_irq - irq for short circuit |
| 267 | * @ pwm_data - pwm configuration |
| 268 | * @ ares_cfg - auto resonance configuration |
| 269 | * @ play_time_ms - play time set by the user in ms |
| 270 | * @ max_play_time_ms - max play time in ms |
| 271 | * @ vmax_mv - max voltage in mv |
| 272 | * @ ilim_ma - limiting current in ma |
| 273 | * @ sc_deb_cycles - short circuit debounce cycles |
| 274 | * @ wave_play_rate_us - play rate for waveform |
| 275 | * @ last_rate_cfg - Last rate config updated |
| 276 | * @ wave_rep_cnt - waveform repeat count |
| 277 | * @ wave_s_rep_cnt - waveform sample repeat count |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 278 | * @ wf_samp_len - waveform sample length |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 279 | * @ ext_pwm_freq_khz - external pwm frequency in KHz |
| 280 | * @ ext_pwm_dtest_line - DTEST line for external pwm |
| 281 | * @ status_flags - status |
| 282 | * @ play_mode - play mode |
| 283 | * @ act_type - actuator type |
| 284 | * @ wave_shape - waveform shape |
| 285 | * @ wave_samp_idx - wave sample id used to refer start of a sample set |
| 286 | * @ wave_samp - array of wave samples |
| 287 | * @ brake_pat - pattern for active breaking |
| 288 | * @ en_brake - brake state |
| 289 | * @ misc_clk_trim_error_reg - MISC clock trim error register if present |
| 290 | * @ clk_trim_error_code - MISC clock trim error code |
| 291 | * @ drive_period_code_max_limit - calculated drive period code with |
| 292 | percentage variation on the higher side. |
| 293 | * @ drive_period_code_min_limit - calculated drive period code with |
| 294 | percentage variation on the lower side |
| 295 | * @ drive_period_code_max_var_pct - maximum limit of percentage variation of |
| 296 | drive period code |
| 297 | * @ drive_period_code_min_var_pct - minimum limit of percentage variation of |
| 298 | drive period code |
| 299 | * @ last_sc_time - Last time short circuit was detected |
| 300 | * @ sc_count - counter to determine the duration of short circuit |
| 301 | condition |
| 302 | * @ perm_disable - Flag to disable module permanently |
| 303 | * @ state - current state of haptics |
| 304 | * @ module_en - module enable status of haptics |
| 305 | * @ lra_auto_mode - Auto mode selection |
| 306 | * @ play_irq_en - Play interrupt enable status |
| 307 | * @ auto_res_err_recovery_hw - Enable auto resonance error recovery by HW |
| 308 | */ |
| 309 | struct hap_chip { |
| 310 | struct platform_device *pdev; |
| 311 | struct regmap *regmap; |
| 312 | struct pmic_revid_data *revid; |
| 313 | struct led_classdev cdev; |
| 314 | spinlock_t bus_lock; |
| 315 | struct mutex play_lock; |
| 316 | struct mutex param_lock; |
| 317 | struct work_struct haptics_work; |
| 318 | struct hrtimer stop_timer; |
| 319 | struct hrtimer auto_res_err_poll_timer; |
| 320 | u16 base; |
| 321 | int play_irq; |
| 322 | int sc_irq; |
| 323 | struct pwm_param pwm_data; |
| 324 | struct hap_lra_ares_param ares_cfg; |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 325 | struct regulator *vcc_pon; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 326 | u32 play_time_ms; |
| 327 | u32 max_play_time_ms; |
| 328 | u32 vmax_mv; |
| 329 | u8 ilim_ma; |
| 330 | u32 sc_deb_cycles; |
| 331 | u32 wave_play_rate_us; |
| 332 | u16 last_rate_cfg; |
| 333 | u32 wave_rep_cnt; |
| 334 | u32 wave_s_rep_cnt; |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 335 | u32 wf_samp_len; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 336 | u32 ext_pwm_freq_khz; |
| 337 | u8 ext_pwm_dtest_line; |
| 338 | u32 status_flags; |
| 339 | enum hap_mode play_mode; |
| 340 | u8 act_type; |
| 341 | u8 wave_shape; |
| 342 | u8 wave_samp_idx; |
| 343 | u32 wave_samp[HAP_WAVE_SAMP_SET_LEN]; |
| 344 | u32 brake_pat[HAP_BRAKE_PAT_LEN]; |
| 345 | bool en_brake; |
| 346 | u32 misc_clk_trim_error_reg; |
| 347 | u8 clk_trim_error_code; |
| 348 | u16 drive_period_code_max_limit; |
| 349 | u16 drive_period_code_min_limit; |
| 350 | u8 drive_period_code_max_var_pct; |
| 351 | u8 drive_period_code_min_var_pct; |
| 352 | ktime_t last_sc_time; |
| 353 | u8 sc_count; |
| 354 | bool perm_disable; |
| 355 | atomic_t state; |
| 356 | bool module_en; |
| 357 | bool lra_auto_mode; |
| 358 | bool play_irq_en; |
| 359 | bool auto_res_err_recovery_hw; |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 360 | bool vcc_pon_enabled; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | static int qpnp_haptics_parse_buffer_dt(struct hap_chip *chip); |
| 364 | static int qpnp_haptics_parse_pwm_dt(struct hap_chip *chip); |
| 365 | |
| 366 | static int qpnp_haptics_read_reg(struct hap_chip *chip, u16 addr, u8 *val, |
| 367 | int len) |
| 368 | { |
| 369 | int rc; |
| 370 | |
| 371 | rc = regmap_bulk_read(chip->regmap, addr, val, len); |
| 372 | if (rc < 0) |
| 373 | pr_err("Error reading address: 0x%x - rc %d\n", addr, rc); |
| 374 | |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | static inline bool is_secure(u16 addr) |
| 379 | { |
| 380 | return ((addr & 0xFF) > 0xD0); |
| 381 | } |
| 382 | |
| 383 | static int qpnp_haptics_write_reg(struct hap_chip *chip, u16 addr, u8 *val, |
| 384 | int len) |
| 385 | { |
| 386 | unsigned long flags; |
| 387 | unsigned int unlock = 0xA5; |
| 388 | int rc = 0, i; |
| 389 | |
| 390 | spin_lock_irqsave(&chip->bus_lock, flags); |
| 391 | |
| 392 | if (is_secure(addr)) { |
| 393 | for (i = 0; i < len; i++) { |
| 394 | rc = regmap_write(chip->regmap, |
| 395 | HAP_SEC_ACCESS_REG(chip), unlock); |
| 396 | if (rc < 0) { |
| 397 | pr_err("Error writing unlock code - rc %d\n", |
| 398 | rc); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | rc = regmap_write(chip->regmap, addr + i, val[i]); |
| 403 | if (rc < 0) { |
| 404 | pr_err("Error writing address 0x%x - rc %d\n", |
| 405 | addr + i, rc); |
| 406 | goto out; |
| 407 | } |
| 408 | } |
| 409 | } else { |
| 410 | if (len > 1) |
| 411 | rc = regmap_bulk_write(chip->regmap, addr, val, len); |
| 412 | else |
| 413 | rc = regmap_write(chip->regmap, addr, *val); |
| 414 | } |
| 415 | |
| 416 | if (rc < 0) |
| 417 | pr_err("Error writing address: 0x%x - rc %d\n", addr, rc); |
| 418 | |
| 419 | out: |
| 420 | spin_unlock_irqrestore(&chip->bus_lock, flags); |
| 421 | return rc; |
| 422 | } |
| 423 | |
| 424 | static int qpnp_haptics_masked_write_reg(struct hap_chip *chip, u16 addr, |
| 425 | u8 mask, u8 val) |
| 426 | { |
| 427 | unsigned long flags; |
| 428 | unsigned int unlock = 0xA5; |
| 429 | int rc; |
| 430 | |
| 431 | spin_lock_irqsave(&chip->bus_lock, flags); |
| 432 | if (is_secure(addr)) { |
| 433 | rc = regmap_write(chip->regmap, HAP_SEC_ACCESS_REG(chip), |
| 434 | unlock); |
| 435 | if (rc < 0) { |
| 436 | pr_err("Error writing unlock code - rc %d\n", rc); |
| 437 | goto out; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | rc = regmap_update_bits(chip->regmap, addr, mask, val); |
| 442 | if (rc < 0) |
| 443 | pr_err("Error writing address: 0x%x - rc %d\n", addr, rc); |
| 444 | |
| 445 | if (!rc) |
| 446 | pr_debug("wrote to address 0x%x = 0x%x\n", addr, val); |
| 447 | out: |
| 448 | spin_unlock_irqrestore(&chip->bus_lock, flags); |
| 449 | return rc; |
| 450 | } |
| 451 | |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 452 | static inline int get_buffer_mode_duration(struct hap_chip *chip) |
| 453 | { |
| 454 | int sample_count, sample_duration; |
| 455 | |
| 456 | sample_count = chip->wave_rep_cnt * chip->wave_s_rep_cnt * |
| 457 | chip->wf_samp_len; |
| 458 | sample_duration = sample_count * chip->wave_play_rate_us; |
| 459 | pr_debug("sample_count: %d sample_duration: %d\n", sample_count, |
| 460 | sample_duration); |
| 461 | |
| 462 | return (sample_duration / 1000); |
| 463 | } |
| 464 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 465 | static bool is_sw_lra_auto_resonance_control(struct hap_chip *chip) |
| 466 | { |
| 467 | if (chip->act_type != HAP_LRA) |
| 468 | return false; |
| 469 | |
| 470 | if (chip->auto_res_err_recovery_hw) |
| 471 | return false; |
| 472 | |
| 473 | /* |
| 474 | * For short pattern in auto mode, we use buffer mode and auto |
| 475 | * resonance is not needed. |
| 476 | */ |
| 477 | if (chip->lra_auto_mode && chip->play_mode == HAP_BUFFER) |
| 478 | return false; |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | #define HAPTICS_BACK_EMF_DELAY_US 20000 |
| 484 | static int qpnp_haptics_auto_res_enable(struct hap_chip *chip, bool enable) |
| 485 | { |
| 486 | int rc = 0; |
| 487 | u32 delay_us = HAPTICS_BACK_EMF_DELAY_US; |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 488 | u8 val; |
| 489 | bool auto_res_mode_qwd; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 490 | |
| 491 | if (chip->act_type != HAP_LRA) |
| 492 | return 0; |
| 493 | |
| 494 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) |
| 495 | auto_res_mode_qwd = (chip->ares_cfg.auto_res_mode == |
| 496 | HAP_PM660_AUTO_RES_QWD); |
| 497 | else |
| 498 | auto_res_mode_qwd = (chip->ares_cfg.auto_res_mode == |
| 499 | HAP_AUTO_RES_QWD); |
| 500 | |
| 501 | /* |
| 502 | * Do not enable auto resonance if auto mode is enabled and auto |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 503 | * resonance mode is QWD, meaning long pattern. |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 504 | */ |
| 505 | if (chip->lra_auto_mode && auto_res_mode_qwd && enable) { |
| 506 | pr_debug("auto_mode enabled, not enabling auto_res\n"); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * For auto resonance detection to work properly, sufficient back-emf |
| 512 | * has to be generated. In general, back-emf takes some time to build |
| 513 | * up. When the auto resonance mode is chosen as QWD, high-z will be |
| 514 | * applied for every LRA cycle and hence there won't be enough back-emf |
| 515 | * at the start-up. Hence, the motor needs to vibrate for few LRA cycles |
| 516 | * after the PLAY bit is asserted. Enable the auto resonance after |
| 517 | * 'time_required_to_generate_back_emf_us' is completed. |
| 518 | */ |
| 519 | |
| 520 | if (auto_res_mode_qwd && enable) |
| 521 | usleep_range(delay_us, delay_us + 1); |
| 522 | |
| 523 | val = enable ? AUTO_RES_EN_BIT : 0; |
| 524 | |
| 525 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) |
| 526 | rc = qpnp_haptics_masked_write_reg(chip, |
| 527 | HAP_AUTO_RES_CTRL_REG(chip), |
| 528 | AUTO_RES_EN_BIT, val); |
| 529 | else |
| 530 | rc = qpnp_haptics_masked_write_reg(chip, HAP_TEST2_REG(chip), |
| 531 | AUTO_RES_EN_BIT, val); |
| 532 | if (rc < 0) |
| 533 | return rc; |
| 534 | |
| 535 | if (enable) |
| 536 | chip->status_flags |= AUTO_RESONANCE_ENABLED; |
| 537 | else |
| 538 | chip->status_flags &= ~AUTO_RESONANCE_ENABLED; |
| 539 | |
| 540 | pr_debug("auto_res %sabled\n", enable ? "en" : "dis"); |
| 541 | return rc; |
| 542 | } |
| 543 | |
| 544 | static int qpnp_haptics_update_rate_cfg(struct hap_chip *chip, u16 play_rate) |
| 545 | { |
| 546 | int rc; |
| 547 | u8 val[2]; |
| 548 | |
| 549 | if (chip->last_rate_cfg == play_rate) { |
| 550 | pr_debug("Same rate_cfg %x\n", play_rate); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | val[0] = play_rate & HAP_RATE_CFG1_MASK; |
| 555 | val[1] = (play_rate >> HAP_RATE_CFG2_SHIFT) & HAP_RATE_CFG2_MASK; |
| 556 | rc = qpnp_haptics_write_reg(chip, HAP_RATE_CFG1_REG(chip), val, 2); |
| 557 | if (rc < 0) |
| 558 | return rc; |
| 559 | |
| 560 | pr_debug("Play rate code 0x%x\n", play_rate); |
| 561 | chip->last_rate_cfg = play_rate; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static void qpnp_haptics_update_lra_frequency(struct hap_chip *chip) |
| 566 | { |
| 567 | u8 lra_auto_res[2], val; |
| 568 | u32 play_rate_code; |
| 569 | u16 rate_cfg; |
| 570 | int rc; |
| 571 | |
| 572 | rc = qpnp_haptics_read_reg(chip, HAP_LRA_AUTO_RES_LO_REG(chip), |
| 573 | lra_auto_res, 2); |
| 574 | if (rc < 0) { |
| 575 | pr_err("Error in reading LRA_AUTO_RES_LO/HI, rc=%d\n", rc); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | play_rate_code = |
| 580 | (lra_auto_res[1] & 0xF0) << 4 | (lra_auto_res[0] & 0xFF); |
| 581 | |
| 582 | pr_debug("lra_auto_res_lo = 0x%x lra_auto_res_hi = 0x%x play_rate_code = 0x%x\n", |
| 583 | lra_auto_res[0], lra_auto_res[1], play_rate_code); |
| 584 | |
| 585 | rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1); |
| 586 | if (rc < 0) |
| 587 | return; |
| 588 | |
| 589 | /* |
| 590 | * If the drive period code read from AUTO_RES_LO and AUTO_RES_HI |
| 591 | * registers is more than the max limit percent variation or less |
| 592 | * than the min limit percent variation specified through DT, then |
| 593 | * auto-resonance is disabled. |
| 594 | */ |
| 595 | |
| 596 | if ((val & AUTO_RES_ERROR_BIT) || |
| 597 | ((play_rate_code <= chip->drive_period_code_min_limit) || |
| 598 | (play_rate_code >= chip->drive_period_code_max_limit))) { |
| 599 | if (val & AUTO_RES_ERROR_BIT) |
| 600 | pr_debug("Auto-resonance error %x\n", val); |
| 601 | else |
| 602 | pr_debug("play rate %x out of bounds [min: 0x%x, max: 0x%x]\n", |
| 603 | play_rate_code, |
| 604 | chip->drive_period_code_min_limit, |
| 605 | chip->drive_period_code_max_limit); |
| 606 | rc = qpnp_haptics_auto_res_enable(chip, false); |
| 607 | if (rc < 0) |
| 608 | pr_debug("Auto-resonance disable failed\n"); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * bits[7:4] of AUTO_RES_HI should be written to bits[3:0] of RATE_CFG2 |
| 614 | */ |
| 615 | lra_auto_res[1] >>= 4; |
| 616 | rate_cfg = lra_auto_res[1] << 8 | lra_auto_res[0]; |
| 617 | rc = qpnp_haptics_update_rate_cfg(chip, rate_cfg); |
| 618 | if (rc < 0) |
| 619 | pr_debug("Error in updating rate_cfg\n"); |
| 620 | } |
| 621 | |
| 622 | #define MAX_RETRIES 5 |
| 623 | #define HAP_CYCLES 4 |
| 624 | static bool is_haptics_idle(struct hap_chip *chip) |
| 625 | { |
| 626 | unsigned long wait_time_us; |
| 627 | int rc, i; |
| 628 | u8 val; |
| 629 | |
| 630 | rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1); |
| 631 | if (rc < 0) |
| 632 | return false; |
| 633 | |
| 634 | if (!(val & HAP_BUSY_BIT)) |
| 635 | return true; |
| 636 | |
| 637 | if (chip->play_time_ms <= 20) |
| 638 | wait_time_us = chip->play_time_ms * 1000; |
| 639 | else |
| 640 | wait_time_us = chip->wave_play_rate_us * HAP_CYCLES; |
| 641 | |
| 642 | for (i = 0; i < MAX_RETRIES; i++) { |
| 643 | /* wait for play_rate cycles */ |
| 644 | usleep_range(wait_time_us, wait_time_us + 1); |
| 645 | |
| 646 | if (chip->play_mode == HAP_DIRECT || |
| 647 | chip->play_mode == HAP_PWM) |
| 648 | return true; |
| 649 | |
| 650 | rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, |
| 651 | 1); |
| 652 | if (rc < 0) |
| 653 | return false; |
| 654 | |
| 655 | if (!(val & HAP_BUSY_BIT)) |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | if (i >= MAX_RETRIES && (val & HAP_BUSY_BIT)) { |
| 660 | pr_debug("Haptics Busy after %d retries\n", i); |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | static int qpnp_haptics_mod_enable(struct hap_chip *chip, bool enable) |
| 668 | { |
| 669 | u8 val; |
| 670 | int rc; |
| 671 | |
| 672 | if (chip->module_en == enable) |
| 673 | return 0; |
| 674 | |
| 675 | if (!enable) { |
| 676 | if (!is_haptics_idle(chip)) |
| 677 | pr_debug("Disabling module forcibly\n"); |
| 678 | } |
| 679 | |
| 680 | val = enable ? HAP_EN_BIT : 0; |
| 681 | rc = qpnp_haptics_write_reg(chip, HAP_EN_CTL_REG(chip), &val, 1); |
| 682 | if (rc < 0) |
| 683 | return rc; |
| 684 | |
| 685 | chip->module_en = enable; |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int qpnp_haptics_play_control(struct hap_chip *chip, |
| 690 | enum hap_play_control ctrl) |
| 691 | { |
| 692 | u8 val; |
| 693 | int rc; |
| 694 | |
| 695 | switch (ctrl) { |
| 696 | case HAP_STOP: |
| 697 | val = 0; |
| 698 | break; |
| 699 | case HAP_PAUSE: |
| 700 | val = PAUSE_BIT; |
| 701 | break; |
| 702 | case HAP_PLAY: |
| 703 | val = PLAY_BIT; |
| 704 | break; |
| 705 | default: |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | rc = qpnp_haptics_write_reg(chip, HAP_PLAY_REG(chip), &val, 1); |
| 710 | if (rc < 0) { |
| 711 | pr_err("Error in writing to PLAY_REG, rc=%d\n", rc); |
| 712 | return rc; |
| 713 | } |
| 714 | |
| 715 | pr_debug("haptics play ctrl: %d\n", ctrl); |
| 716 | return rc; |
| 717 | } |
| 718 | |
| 719 | #define AUTO_RES_ERR_POLL_TIME_NS (20 * NSEC_PER_MSEC) |
| 720 | static int qpnp_haptics_play(struct hap_chip *chip, bool enable) |
| 721 | { |
| 722 | int rc = 0, time_ms = chip->play_time_ms; |
| 723 | |
| 724 | if (chip->perm_disable && enable) |
| 725 | return 0; |
| 726 | |
| 727 | mutex_lock(&chip->play_lock); |
| 728 | |
| 729 | if (enable) { |
| 730 | if (chip->play_mode == HAP_PWM) { |
| 731 | rc = pwm_enable(chip->pwm_data.pwm_dev); |
| 732 | if (rc < 0) { |
| 733 | pr_err("Error in enabling PWM, rc=%d\n", rc); |
| 734 | goto out; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | rc = qpnp_haptics_auto_res_enable(chip, false); |
| 739 | if (rc < 0) { |
| 740 | pr_err("Error in disabling auto_res, rc=%d\n", rc); |
| 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | rc = qpnp_haptics_mod_enable(chip, true); |
| 745 | if (rc < 0) { |
| 746 | pr_err("Error in enabling module, rc=%d\n", rc); |
| 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | rc = qpnp_haptics_play_control(chip, HAP_PLAY); |
| 751 | if (rc < 0) { |
| 752 | pr_err("Error in enabling play, rc=%d\n", rc); |
| 753 | goto out; |
| 754 | } |
| 755 | |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 756 | if (chip->play_mode == HAP_BUFFER) |
| 757 | time_ms = get_buffer_mode_duration(chip); |
| 758 | hrtimer_start(&chip->stop_timer, |
| 759 | ktime_set(time_ms / MSEC_PER_SEC, |
| 760 | (time_ms % MSEC_PER_SEC) * NSEC_PER_MSEC), |
| 761 | HRTIMER_MODE_REL); |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 762 | |
| 763 | rc = qpnp_haptics_auto_res_enable(chip, true); |
| 764 | if (rc < 0) { |
| 765 | pr_err("Error in enabling auto_res, rc=%d\n", rc); |
| 766 | goto out; |
| 767 | } |
| 768 | |
| 769 | if (is_sw_lra_auto_resonance_control(chip)) |
| 770 | hrtimer_start(&chip->auto_res_err_poll_timer, |
| 771 | ktime_set(0, AUTO_RES_ERR_POLL_TIME_NS), |
| 772 | HRTIMER_MODE_REL); |
| 773 | } else { |
| 774 | rc = qpnp_haptics_play_control(chip, HAP_STOP); |
| 775 | if (rc < 0) { |
| 776 | pr_err("Error in disabling play, rc=%d\n", rc); |
| 777 | goto out; |
| 778 | } |
| 779 | |
| 780 | if (is_sw_lra_auto_resonance_control(chip)) { |
| 781 | if (chip->status_flags & AUTO_RESONANCE_ENABLED) |
| 782 | qpnp_haptics_update_lra_frequency(chip); |
| 783 | hrtimer_cancel(&chip->auto_res_err_poll_timer); |
| 784 | } |
| 785 | |
| 786 | if (chip->play_mode == HAP_PWM) |
| 787 | pwm_disable(chip->pwm_data.pwm_dev); |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 788 | |
| 789 | if (chip->play_mode == HAP_BUFFER) |
| 790 | chip->wave_samp_idx = 0; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | out: |
| 794 | mutex_unlock(&chip->play_lock); |
| 795 | return rc; |
| 796 | } |
| 797 | |
| 798 | static void qpnp_haptics_work(struct work_struct *work) |
| 799 | { |
| 800 | struct hap_chip *chip = container_of(work, struct hap_chip, |
| 801 | haptics_work); |
| 802 | int rc; |
| 803 | bool enable; |
| 804 | |
| 805 | enable = atomic_read(&chip->state); |
| 806 | pr_debug("state: %d\n", enable); |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 807 | |
| 808 | if (chip->vcc_pon && enable && !chip->vcc_pon_enabled) { |
| 809 | rc = regulator_enable(chip->vcc_pon); |
| 810 | if (rc < 0) |
| 811 | pr_err("%s: could not enable vcc_pon regulator rc=%d\n", |
| 812 | __func__, rc); |
| 813 | else |
| 814 | chip->vcc_pon_enabled = true; |
| 815 | } |
| 816 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 817 | rc = qpnp_haptics_play(chip, enable); |
| 818 | if (rc < 0) |
| 819 | pr_err("Error in %sing haptics, rc=%d\n", |
| 820 | enable ? "play" : "stopp", rc); |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 821 | |
| 822 | if (chip->vcc_pon && !enable && chip->vcc_pon_enabled) { |
| 823 | rc = regulator_disable(chip->vcc_pon); |
| 824 | if (rc) |
| 825 | pr_err("%s: could not disable vcc_pon regulator rc=%d\n", |
| 826 | __func__, rc); |
| 827 | else |
| 828 | chip->vcc_pon_enabled = false; |
| 829 | } |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static enum hrtimer_restart hap_stop_timer(struct hrtimer *timer) |
| 833 | { |
| 834 | struct hap_chip *chip = container_of(timer, struct hap_chip, |
| 835 | stop_timer); |
| 836 | |
| 837 | atomic_set(&chip->state, 0); |
| 838 | schedule_work(&chip->haptics_work); |
| 839 | |
| 840 | return HRTIMER_NORESTART; |
| 841 | } |
| 842 | |
| 843 | static enum hrtimer_restart hap_auto_res_err_poll_timer(struct hrtimer *timer) |
| 844 | { |
| 845 | struct hap_chip *chip = container_of(timer, struct hap_chip, |
| 846 | auto_res_err_poll_timer); |
| 847 | |
| 848 | if (!(chip->status_flags & AUTO_RESONANCE_ENABLED)) |
| 849 | return HRTIMER_NORESTART; |
| 850 | |
| 851 | qpnp_haptics_update_lra_frequency(chip); |
| 852 | hrtimer_forward(&chip->auto_res_err_poll_timer, ktime_get(), |
| 853 | ktime_set(0, AUTO_RES_ERR_POLL_TIME_NS)); |
| 854 | |
| 855 | return HRTIMER_NORESTART; |
| 856 | } |
| 857 | |
| 858 | static int qpnp_haptics_suspend(struct device *dev) |
| 859 | { |
| 860 | struct hap_chip *chip = dev_get_drvdata(dev); |
| 861 | int rc; |
| 862 | |
| 863 | rc = qpnp_haptics_play(chip, false); |
| 864 | if (rc < 0) |
| 865 | pr_err("Error in stopping haptics, rc=%d\n", rc); |
| 866 | |
| 867 | rc = qpnp_haptics_mod_enable(chip, false); |
| 868 | if (rc < 0) |
| 869 | pr_err("Error in disabling module, rc=%d\n", rc); |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int qpnp_haptics_wave_rep_config(struct hap_chip *chip, |
| 875 | enum hap_rep_type type) |
| 876 | { |
| 877 | int rc; |
| 878 | u8 val = 0, mask = 0; |
| 879 | |
| 880 | if (type & HAP_WAVE_REPEAT) { |
| 881 | if (chip->wave_rep_cnt < WF_REPEAT_MIN) |
| 882 | chip->wave_rep_cnt = WF_REPEAT_MIN; |
| 883 | else if (chip->wave_rep_cnt > WF_REPEAT_MAX) |
| 884 | chip->wave_rep_cnt = WF_REPEAT_MAX; |
| 885 | mask = WF_REPEAT_MASK; |
| 886 | val = ilog2(chip->wave_rep_cnt) << WF_REPEAT_SHIFT; |
| 887 | } |
| 888 | |
| 889 | if (type & HAP_WAVE_SAMP_REPEAT) { |
| 890 | if (chip->wave_s_rep_cnt < WF_S_REPEAT_MIN) |
| 891 | chip->wave_s_rep_cnt = WF_S_REPEAT_MIN; |
| 892 | else if (chip->wave_s_rep_cnt > WF_S_REPEAT_MAX) |
| 893 | chip->wave_s_rep_cnt = WF_S_REPEAT_MAX; |
| 894 | mask |= WF_S_REPEAT_MASK; |
| 895 | val |= ilog2(chip->wave_s_rep_cnt); |
| 896 | } |
| 897 | |
| 898 | rc = qpnp_haptics_masked_write_reg(chip, HAP_WF_REPEAT_REG(chip), |
| 899 | mask, val); |
| 900 | return rc; |
| 901 | } |
| 902 | |
| 903 | /* configuration api for buffer mode */ |
| 904 | static int qpnp_haptics_buffer_config(struct hap_chip *chip, u32 *wave_samp, |
| 905 | bool overdrive) |
| 906 | { |
| 907 | u8 buf[HAP_WAVE_SAMP_LEN]; |
| 908 | u32 *ptr; |
| 909 | int rc, i; |
| 910 | |
| 911 | if (wave_samp) { |
| 912 | ptr = wave_samp; |
| 913 | } else { |
| 914 | if (chip->wave_samp_idx >= ARRAY_SIZE(chip->wave_samp)) { |
| 915 | pr_err("Incorrect wave_samp_idx %d\n", |
| 916 | chip->wave_samp_idx); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
| 920 | ptr = &chip->wave_samp[chip->wave_samp_idx]; |
| 921 | } |
| 922 | |
| 923 | /* Don't set override bit in waveform sample for PM660 */ |
| 924 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) |
| 925 | overdrive = false; |
| 926 | |
| 927 | /* Configure WAVE_SAMPLE1 to WAVE_SAMPLE8 register */ |
| 928 | for (i = 0; i < HAP_WAVE_SAMP_LEN; i++) { |
| 929 | buf[i] = ptr[i]; |
| 930 | if (buf[i]) |
| 931 | buf[i] |= (overdrive ? HAP_WF_OVD_BIT : 0); |
| 932 | } |
| 933 | |
| 934 | rc = qpnp_haptics_write_reg(chip, HAP_WF_S1_REG(chip), buf, |
| 935 | HAP_WAVE_SAMP_LEN); |
| 936 | return rc; |
| 937 | } |
| 938 | |
| 939 | /* configuration api for pwm */ |
| 940 | static int qpnp_haptics_pwm_config(struct hap_chip *chip) |
| 941 | { |
| 942 | u8 val = 0; |
| 943 | int rc; |
| 944 | |
| 945 | if (chip->ext_pwm_freq_khz == 0) |
| 946 | return 0; |
| 947 | |
| 948 | /* Configure the EXTERNAL_PWM register */ |
| 949 | if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_25_KHZ) { |
| 950 | chip->ext_pwm_freq_khz = EXT_PWM_FREQ_25_KHZ; |
| 951 | val = 0; |
| 952 | } else if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_50_KHZ) { |
| 953 | chip->ext_pwm_freq_khz = EXT_PWM_FREQ_50_KHZ; |
| 954 | val = 1; |
| 955 | } else if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_75_KHZ) { |
| 956 | chip->ext_pwm_freq_khz = EXT_PWM_FREQ_75_KHZ; |
| 957 | val = 2; |
| 958 | } else { |
| 959 | chip->ext_pwm_freq_khz = EXT_PWM_FREQ_100_KHZ; |
| 960 | val = 3; |
| 961 | } |
| 962 | |
| 963 | rc = qpnp_haptics_masked_write_reg(chip, HAP_EXT_PWM_REG(chip), |
| 964 | EXT_PWM_FREQ_SEL_MASK, val); |
| 965 | if (rc < 0) |
| 966 | return rc; |
| 967 | |
| 968 | if (chip->ext_pwm_dtest_line < 0 || |
| 969 | chip->ext_pwm_dtest_line > PWM_MAX_DTEST_LINES) { |
| 970 | pr_err("invalid dtest line\n"); |
| 971 | return -EINVAL; |
| 972 | } |
| 973 | |
| 974 | if (chip->ext_pwm_dtest_line > 0) { |
| 975 | /* disable auto res for PWM mode */ |
| 976 | val = chip->ext_pwm_dtest_line << HAP_EXT_PWM_DTEST_SHIFT; |
| 977 | rc = qpnp_haptics_masked_write_reg(chip, HAP_TEST2_REG(chip), |
| 978 | HAP_EXT_PWM_DTEST_MASK | AUTO_RES_EN_BIT, val); |
| 979 | if (rc < 0) |
| 980 | return rc; |
| 981 | } |
| 982 | |
| 983 | rc = pwm_config(chip->pwm_data.pwm_dev, |
| 984 | chip->pwm_data.duty_us * NSEC_PER_USEC, |
| 985 | chip->pwm_data.period_us * NSEC_PER_USEC); |
| 986 | if (rc < 0) { |
| 987 | pr_err("pwm_config failed, rc=%d\n", rc); |
| 988 | return rc; |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static int qpnp_haptics_lra_auto_res_config(struct hap_chip *chip, |
| 995 | struct hap_lra_ares_param *tmp_cfg) |
| 996 | { |
| 997 | struct hap_lra_ares_param *ares_cfg; |
| 998 | int rc; |
| 999 | u8 val = 0, mask = 0; |
| 1000 | |
| 1001 | /* disable auto resonance for ERM */ |
| 1002 | if (chip->act_type == HAP_ERM) { |
| 1003 | val = 0x00; |
| 1004 | rc = qpnp_haptics_write_reg(chip, HAP_LRA_AUTO_RES_REG(chip), |
| 1005 | &val, 1); |
| 1006 | return rc; |
| 1007 | } |
| 1008 | |
| 1009 | if (chip->auto_res_err_recovery_hw) { |
| 1010 | rc = qpnp_haptics_masked_write_reg(chip, |
| 1011 | HAP_AUTO_RES_CTRL_REG(chip), |
| 1012 | AUTO_RES_ERR_RECOVERY_BIT, AUTO_RES_ERR_RECOVERY_BIT); |
| 1013 | if (rc < 0) |
| 1014 | return rc; |
| 1015 | } |
| 1016 | |
| 1017 | if (tmp_cfg) |
| 1018 | ares_cfg = tmp_cfg; |
| 1019 | else |
| 1020 | ares_cfg = &chip->ares_cfg; |
| 1021 | |
| 1022 | if (ares_cfg->lra_res_cal_period < HAP_RES_CAL_PERIOD_MIN) |
| 1023 | ares_cfg->lra_res_cal_period = HAP_RES_CAL_PERIOD_MIN; |
| 1024 | |
| 1025 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 1026 | if (ares_cfg->lra_res_cal_period > |
| 1027 | HAP_PM660_RES_CAL_PERIOD_MAX) |
| 1028 | ares_cfg->lra_res_cal_period = |
| 1029 | HAP_PM660_RES_CAL_PERIOD_MAX; |
| 1030 | |
| 1031 | if (ares_cfg->auto_res_mode == HAP_PM660_AUTO_RES_QWD) |
| 1032 | ares_cfg->lra_res_cal_period = 0; |
| 1033 | |
| 1034 | if (ares_cfg->lra_res_cal_period) |
| 1035 | val = ilog2(ares_cfg->lra_res_cal_period / |
| 1036 | HAP_RES_CAL_PERIOD_MIN) + 1; |
| 1037 | } else { |
| 1038 | if (ares_cfg->lra_res_cal_period > HAP_RES_CAL_PERIOD_MAX) |
| 1039 | ares_cfg->lra_res_cal_period = |
| 1040 | HAP_RES_CAL_PERIOD_MAX; |
| 1041 | |
| 1042 | if (ares_cfg->lra_res_cal_period) |
| 1043 | val = ilog2(ares_cfg->lra_res_cal_period / |
| 1044 | HAP_RES_CAL_PERIOD_MIN); |
| 1045 | } |
| 1046 | |
| 1047 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 1048 | val |= ares_cfg->auto_res_mode << PM660_AUTO_RES_MODE_SHIFT; |
| 1049 | mask = PM660_AUTO_RES_MODE_BIT; |
| 1050 | val |= ares_cfg->lra_high_z << PM660_CAL_DURATION_SHIFT; |
| 1051 | mask |= PM660_CAL_DURATION_MASK; |
| 1052 | if (ares_cfg->lra_qwd_drive_duration != -EINVAL) { |
| 1053 | val |= ares_cfg->lra_qwd_drive_duration << |
| 1054 | PM660_QWD_DRIVE_DURATION_SHIFT; |
| 1055 | mask |= PM660_QWD_DRIVE_DURATION_BIT; |
| 1056 | } |
| 1057 | if (ares_cfg->calibrate_at_eop != -EINVAL) { |
| 1058 | val |= ares_cfg->calibrate_at_eop << |
| 1059 | PM660_CAL_EOP_SHIFT; |
| 1060 | mask |= PM660_CAL_EOP_BIT; |
| 1061 | } |
| 1062 | mask |= PM660_LRA_RES_CAL_MASK; |
| 1063 | } else { |
| 1064 | val |= (ares_cfg->auto_res_mode << LRA_AUTO_RES_MODE_SHIFT); |
| 1065 | val |= (ares_cfg->lra_high_z << LRA_HIGH_Z_SHIFT); |
| 1066 | mask = LRA_AUTO_RES_MODE_MASK | LRA_HIGH_Z_MASK | |
| 1067 | LRA_RES_CAL_MASK; |
| 1068 | } |
| 1069 | |
| 1070 | pr_debug("mode: %d hi_z period: %d cal_period: %d\n", |
| 1071 | ares_cfg->auto_res_mode, ares_cfg->lra_high_z, |
| 1072 | ares_cfg->lra_res_cal_period); |
| 1073 | |
| 1074 | rc = qpnp_haptics_masked_write_reg(chip, HAP_LRA_AUTO_RES_REG(chip), |
| 1075 | mask, val); |
| 1076 | return rc; |
| 1077 | } |
| 1078 | |
| 1079 | /* configuration api for play mode */ |
| 1080 | static int qpnp_haptics_play_mode_config(struct hap_chip *chip) |
| 1081 | { |
| 1082 | u8 val = 0; |
| 1083 | int rc; |
| 1084 | |
| 1085 | if (!is_haptics_idle(chip)) |
| 1086 | return -EBUSY; |
| 1087 | |
| 1088 | val = chip->play_mode << HAP_WF_SOURCE_SHIFT; |
| 1089 | rc = qpnp_haptics_masked_write_reg(chip, HAP_SEL_REG(chip), |
| 1090 | HAP_WF_SOURCE_MASK, val); |
| 1091 | if (!rc) { |
| 1092 | if (chip->play_mode == HAP_BUFFER && !chip->play_irq_en) { |
| 1093 | enable_irq(chip->play_irq); |
| 1094 | chip->play_irq_en = true; |
| 1095 | } else if (chip->play_mode != HAP_BUFFER && chip->play_irq_en) { |
| 1096 | disable_irq(chip->play_irq); |
| 1097 | chip->play_irq_en = false; |
| 1098 | } |
| 1099 | } |
| 1100 | return rc; |
| 1101 | } |
| 1102 | |
| 1103 | /* configuration api for max voltage */ |
| 1104 | static int qpnp_haptics_vmax_config(struct hap_chip *chip, int vmax_mv, |
| 1105 | bool overdrive) |
| 1106 | { |
| 1107 | u8 val = 0; |
| 1108 | int rc; |
| 1109 | |
| 1110 | if (vmax_mv < 0) |
| 1111 | return -EINVAL; |
| 1112 | |
| 1113 | /* Allow setting override bit in VMAX_CFG only for PM660 */ |
| 1114 | if (chip->revid->pmic_subtype != PM660_SUBTYPE) |
| 1115 | overdrive = false; |
| 1116 | |
| 1117 | if (vmax_mv < HAP_VMAX_MIN_MV) |
| 1118 | vmax_mv = HAP_VMAX_MIN_MV; |
| 1119 | else if (vmax_mv > HAP_VMAX_MAX_MV) |
| 1120 | vmax_mv = HAP_VMAX_MAX_MV; |
| 1121 | |
| 1122 | val = DIV_ROUND_CLOSEST(vmax_mv, HAP_VMAX_MIN_MV); |
| 1123 | val <<= HAP_VMAX_SHIFT; |
| 1124 | if (overdrive) |
| 1125 | val |= HAP_VMAX_OVD_BIT; |
| 1126 | |
| 1127 | rc = qpnp_haptics_masked_write_reg(chip, HAP_VMAX_CFG_REG(chip), |
| 1128 | HAP_VMAX_MASK | HAP_VMAX_OVD_BIT, val); |
| 1129 | return rc; |
| 1130 | } |
| 1131 | |
| 1132 | /* configuration api for ilim */ |
| 1133 | static int qpnp_haptics_ilim_config(struct hap_chip *chip) |
| 1134 | { |
| 1135 | int rc; |
| 1136 | |
| 1137 | if (chip->ilim_ma < HAP_ILIM_400_MA) |
| 1138 | chip->ilim_ma = HAP_ILIM_400_MA; |
| 1139 | else if (chip->ilim_ma > HAP_ILIM_800_MA) |
| 1140 | chip->ilim_ma = HAP_ILIM_800_MA; |
| 1141 | |
| 1142 | rc = qpnp_haptics_masked_write_reg(chip, HAP_ILIM_CFG_REG(chip), |
| 1143 | HAP_ILIM_SEL_MASK, chip->ilim_ma); |
| 1144 | return rc; |
| 1145 | } |
| 1146 | |
| 1147 | /* configuration api for short circuit debounce */ |
| 1148 | static int qpnp_haptics_sc_deb_config(struct hap_chip *chip) |
| 1149 | { |
| 1150 | u8 val = 0; |
| 1151 | int rc; |
| 1152 | |
| 1153 | if (chip->sc_deb_cycles < HAP_SC_DEB_CYCLES_MIN) |
| 1154 | chip->sc_deb_cycles = HAP_SC_DEB_CYCLES_MIN; |
| 1155 | else if (chip->sc_deb_cycles > HAP_SC_DEB_CYCLES_MAX) |
| 1156 | chip->sc_deb_cycles = HAP_SC_DEB_CYCLES_MAX; |
| 1157 | |
| 1158 | if (chip->sc_deb_cycles != HAP_SC_DEB_CYCLES_MIN) |
| 1159 | val = ilog2(chip->sc_deb_cycles / |
| 1160 | HAP_DEF_SC_DEB_CYCLES) + 1; |
| 1161 | else |
| 1162 | val = HAP_SC_DEB_CYCLES_MIN; |
| 1163 | |
| 1164 | rc = qpnp_haptics_masked_write_reg(chip, HAP_SC_DEB_REG(chip), |
| 1165 | HAP_SC_DEB_MASK, val); |
| 1166 | |
| 1167 | return rc; |
| 1168 | } |
| 1169 | |
| 1170 | static int qpnp_haptics_brake_config(struct hap_chip *chip, u32 *brake_pat) |
| 1171 | { |
| 1172 | int rc, i; |
| 1173 | u32 temp, *ptr; |
| 1174 | u8 val; |
| 1175 | |
| 1176 | /* Configure BRAKE register */ |
| 1177 | rc = qpnp_haptics_masked_write_reg(chip, HAP_EN_CTL2_REG(chip), |
| 1178 | BRAKE_EN_BIT, (u8)chip->en_brake); |
| 1179 | if (rc < 0) |
| 1180 | return rc; |
| 1181 | |
| 1182 | /* If braking is not enabled, skip configuring brake pattern */ |
| 1183 | if (!chip->en_brake) |
| 1184 | return 0; |
| 1185 | |
| 1186 | if (!brake_pat) |
| 1187 | ptr = chip->brake_pat; |
| 1188 | else |
| 1189 | ptr = brake_pat; |
| 1190 | |
| 1191 | for (i = HAP_BRAKE_PAT_LEN - 1, val = 0; i >= 0; i--) { |
| 1192 | ptr[i] &= HAP_BRAKE_PAT_MASK; |
| 1193 | temp = i << 1; |
| 1194 | val |= ptr[i] << temp; |
| 1195 | } |
| 1196 | |
| 1197 | rc = qpnp_haptics_write_reg(chip, HAP_BRAKE_REG(chip), &val, 1); |
| 1198 | if (rc < 0) |
| 1199 | return rc; |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static int qpnp_haptics_auto_mode_config(struct hap_chip *chip, int time_ms) |
| 1205 | { |
| 1206 | struct hap_lra_ares_param ares_cfg; |
| 1207 | enum hap_mode old_play_mode; |
| 1208 | u8 old_ares_mode; |
| 1209 | u32 brake_pat[HAP_BRAKE_PAT_LEN] = {0}; |
| 1210 | u32 wave_samp[HAP_WAVE_SAMP_LEN] = {0}; |
| 1211 | int rc, vmax_mv; |
| 1212 | |
| 1213 | if (!chip->lra_auto_mode) |
| 1214 | return false; |
| 1215 | |
| 1216 | /* For now, this is for LRA only */ |
| 1217 | if (chip->act_type == HAP_ERM) |
| 1218 | return 0; |
| 1219 | |
| 1220 | old_ares_mode = chip->ares_cfg.auto_res_mode; |
| 1221 | old_play_mode = chip->play_mode; |
| 1222 | pr_debug("auto_mode, time_ms: %d\n", time_ms); |
| 1223 | if (time_ms <= 20) { |
| 1224 | wave_samp[0] = HAP_WF_SAMP_MAX; |
| 1225 | wave_samp[1] = HAP_WF_SAMP_MAX; |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 1226 | chip->wf_samp_len = 2; |
| 1227 | if (time_ms > 15) { |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1228 | wave_samp[2] = HAP_WF_SAMP_MAX; |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 1229 | chip->wf_samp_len = 3; |
| 1230 | } |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* short pattern */ |
| 1233 | rc = qpnp_haptics_parse_buffer_dt(chip); |
| 1234 | if (!rc) { |
| 1235 | rc = qpnp_haptics_wave_rep_config(chip, |
| 1236 | HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT); |
| 1237 | if (rc < 0) { |
| 1238 | pr_err("Error in configuring wave_rep config %d\n", |
| 1239 | rc); |
| 1240 | return rc; |
| 1241 | } |
| 1242 | |
| 1243 | rc = qpnp_haptics_buffer_config(chip, wave_samp, true); |
| 1244 | if (rc < 0) { |
| 1245 | pr_err("Error in configuring buffer mode %d\n", |
| 1246 | rc); |
| 1247 | return rc; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT1; |
| 1252 | ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MIN; |
| 1253 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 1254 | ares_cfg.auto_res_mode = HAP_PM660_AUTO_RES_QWD; |
| 1255 | ares_cfg.lra_qwd_drive_duration = 0; |
| 1256 | ares_cfg.calibrate_at_eop = 0; |
| 1257 | } else { |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 1258 | ares_cfg.auto_res_mode = HAP_AUTO_RES_ZXD_EOP; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1259 | ares_cfg.lra_qwd_drive_duration = -EINVAL; |
| 1260 | ares_cfg.calibrate_at_eop = -EINVAL; |
| 1261 | } |
| 1262 | |
| 1263 | vmax_mv = HAP_VMAX_MAX_MV; |
| 1264 | rc = qpnp_haptics_vmax_config(chip, vmax_mv, true); |
| 1265 | if (rc < 0) |
| 1266 | return rc; |
| 1267 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1268 | /* enable play_irq for buffer mode */ |
| 1269 | if (chip->play_irq >= 0 && !chip->play_irq_en) { |
| 1270 | enable_irq(chip->play_irq); |
| 1271 | chip->play_irq_en = true; |
| 1272 | } |
| 1273 | |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 1274 | brake_pat[0] = BRAKE_VMAX; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1275 | chip->play_mode = HAP_BUFFER; |
| 1276 | chip->wave_shape = HAP_WAVE_SQUARE; |
| 1277 | } else { |
| 1278 | /* long pattern */ |
| 1279 | ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT1; |
| 1280 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 1281 | ares_cfg.auto_res_mode = HAP_PM660_AUTO_RES_ZXD; |
| 1282 | ares_cfg.lra_res_cal_period = |
| 1283 | HAP_PM660_RES_CAL_PERIOD_MAX; |
| 1284 | ares_cfg.lra_qwd_drive_duration = 0; |
| 1285 | ares_cfg.calibrate_at_eop = 1; |
| 1286 | } else { |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 1287 | ares_cfg.auto_res_mode = HAP_AUTO_RES_QWD; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1288 | ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MAX; |
| 1289 | ares_cfg.lra_qwd_drive_duration = -EINVAL; |
| 1290 | ares_cfg.calibrate_at_eop = -EINVAL; |
| 1291 | } |
| 1292 | |
| 1293 | vmax_mv = chip->vmax_mv; |
| 1294 | rc = qpnp_haptics_vmax_config(chip, vmax_mv, false); |
| 1295 | if (rc < 0) |
| 1296 | return rc; |
| 1297 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1298 | /* enable play_irq for direct mode */ |
| 1299 | if (chip->play_irq >= 0 && chip->play_irq_en) { |
| 1300 | disable_irq(chip->play_irq); |
| 1301 | chip->play_irq_en = false; |
| 1302 | } |
| 1303 | |
| 1304 | chip->play_mode = HAP_DIRECT; |
| 1305 | chip->wave_shape = HAP_WAVE_SINE; |
| 1306 | } |
| 1307 | |
| 1308 | chip->ares_cfg.auto_res_mode = ares_cfg.auto_res_mode; |
| 1309 | rc = qpnp_haptics_lra_auto_res_config(chip, &ares_cfg); |
| 1310 | if (rc < 0) { |
| 1311 | chip->ares_cfg.auto_res_mode = old_ares_mode; |
| 1312 | return rc; |
| 1313 | } |
| 1314 | |
| 1315 | rc = qpnp_haptics_play_mode_config(chip); |
| 1316 | if (rc < 0) { |
| 1317 | chip->play_mode = old_play_mode; |
| 1318 | return rc; |
| 1319 | } |
| 1320 | |
Subbaraman Narayanamurthy | 69a4b94 | 2018-01-22 17:09:11 -0800 | [diff] [blame] | 1321 | rc = qpnp_haptics_brake_config(chip, brake_pat); |
| 1322 | if (rc < 0) |
| 1323 | return rc; |
| 1324 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1325 | rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG2_REG(chip), |
| 1326 | HAP_LRA_RES_TYPE_MASK, chip->wave_shape); |
| 1327 | if (rc < 0) |
| 1328 | return rc; |
| 1329 | |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | static irqreturn_t qpnp_haptics_play_irq_handler(int irq, void *data) |
| 1334 | { |
| 1335 | struct hap_chip *chip = data; |
| 1336 | int rc; |
| 1337 | |
| 1338 | if (chip->play_mode != HAP_BUFFER) |
| 1339 | goto irq_handled; |
| 1340 | |
| 1341 | if (chip->wave_samp[chip->wave_samp_idx + HAP_WAVE_SAMP_LEN] > 0) { |
| 1342 | chip->wave_samp_idx += HAP_WAVE_SAMP_LEN; |
| 1343 | if (chip->wave_samp_idx >= ARRAY_SIZE(chip->wave_samp)) { |
| 1344 | pr_debug("Samples over\n"); |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1345 | } else { |
| 1346 | pr_debug("moving to next sample set %d\n", |
| 1347 | chip->wave_samp_idx); |
| 1348 | |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 1349 | /* Moving to next set of wave sample */ |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1350 | rc = qpnp_haptics_buffer_config(chip, NULL, false); |
| 1351 | if (rc < 0) { |
| 1352 | pr_err("Error in configuring buffer, rc=%d\n", |
| 1353 | rc); |
| 1354 | goto irq_handled; |
| 1355 | } |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1359 | irq_handled: |
| 1360 | return IRQ_HANDLED; |
| 1361 | } |
| 1362 | |
| 1363 | #define SC_MAX_COUNT 5 |
| 1364 | #define SC_COUNT_RST_DELAY_US 1000000 |
| 1365 | static irqreturn_t qpnp_haptics_sc_irq_handler(int irq, void *data) |
| 1366 | { |
| 1367 | struct hap_chip *chip = data; |
| 1368 | int rc; |
| 1369 | u8 val; |
| 1370 | s64 sc_delta_time_us; |
| 1371 | ktime_t temp; |
| 1372 | |
| 1373 | rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1); |
| 1374 | if (rc < 0) |
| 1375 | goto irq_handled; |
| 1376 | |
| 1377 | if (!(val & SC_FLAG_BIT)) { |
| 1378 | chip->sc_count = 0; |
| 1379 | goto irq_handled; |
| 1380 | } |
| 1381 | |
| 1382 | pr_debug("SC irq fired\n"); |
| 1383 | temp = ktime_get(); |
| 1384 | sc_delta_time_us = ktime_us_delta(temp, chip->last_sc_time); |
| 1385 | chip->last_sc_time = temp; |
| 1386 | |
| 1387 | if (sc_delta_time_us > SC_COUNT_RST_DELAY_US) |
| 1388 | chip->sc_count = 0; |
| 1389 | else |
| 1390 | chip->sc_count++; |
| 1391 | |
| 1392 | val = SC_CLR_BIT; |
| 1393 | rc = qpnp_haptics_write_reg(chip, HAP_SC_CLR_REG(chip), &val, 1); |
| 1394 | if (rc < 0) { |
| 1395 | pr_err("Error in writing to SC_CLR_REG, rc=%d\n", rc); |
| 1396 | goto irq_handled; |
| 1397 | } |
| 1398 | |
| 1399 | /* Permanently disable module if SC condition persists */ |
| 1400 | if (chip->sc_count > SC_MAX_COUNT) { |
| 1401 | pr_crit("SC persists, permanently disabling haptics\n"); |
| 1402 | rc = qpnp_haptics_mod_enable(chip, false); |
| 1403 | if (rc < 0) { |
| 1404 | pr_err("Error in disabling module, rc=%d\n", rc); |
| 1405 | goto irq_handled; |
| 1406 | } |
| 1407 | chip->perm_disable = true; |
| 1408 | } |
| 1409 | |
| 1410 | irq_handled: |
| 1411 | return IRQ_HANDLED; |
| 1412 | } |
| 1413 | |
| 1414 | /* All sysfs show/store functions below */ |
| 1415 | |
| 1416 | #define HAP_STR_SIZE 128 |
| 1417 | static int parse_string(const char *in_buf, char *out_buf) |
| 1418 | { |
| 1419 | int i; |
| 1420 | |
| 1421 | if (snprintf(out_buf, HAP_STR_SIZE, "%s", in_buf) > HAP_STR_SIZE) |
| 1422 | return -EINVAL; |
| 1423 | |
| 1424 | for (i = 0; i < strlen(out_buf); i++) { |
| 1425 | if (out_buf[i] == ' ' || out_buf[i] == '\n' || |
| 1426 | out_buf[i] == '\t') { |
| 1427 | out_buf[i] = '\0'; |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | static ssize_t qpnp_haptics_show_state(struct device *dev, |
| 1436 | struct device_attribute *attr, char *buf) |
| 1437 | { |
| 1438 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1439 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1440 | |
| 1441 | return snprintf(buf, PAGE_SIZE, "%d\n", chip->module_en); |
| 1442 | } |
| 1443 | |
| 1444 | static ssize_t qpnp_haptics_store_state(struct device *dev, |
| 1445 | struct device_attribute *attr, const char *buf, size_t count) |
| 1446 | { |
| 1447 | |
| 1448 | /* At present, nothing to do with setting state */ |
| 1449 | return count; |
| 1450 | } |
| 1451 | |
| 1452 | static ssize_t qpnp_haptics_show_duration(struct device *dev, |
| 1453 | struct device_attribute *attr, char *buf) |
| 1454 | { |
| 1455 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1456 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1457 | ktime_t time_rem; |
| 1458 | s64 time_us = 0; |
| 1459 | |
| 1460 | if (hrtimer_active(&chip->stop_timer)) { |
| 1461 | time_rem = hrtimer_get_remaining(&chip->stop_timer); |
| 1462 | time_us = ktime_to_us(time_rem); |
| 1463 | } |
| 1464 | |
Tirupathi Reddy | 8f0b419 | 2018-01-11 15:34:54 +0530 | [diff] [blame] | 1465 | return snprintf(buf, PAGE_SIZE, "%lld\n", div_s64(time_us, 1000)); |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static ssize_t qpnp_haptics_store_duration(struct device *dev, |
| 1469 | struct device_attribute *attr, const char *buf, size_t count) |
| 1470 | { |
| 1471 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1472 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1473 | u32 val; |
| 1474 | int rc; |
| 1475 | |
| 1476 | rc = kstrtouint(buf, 0, &val); |
| 1477 | if (rc < 0) |
| 1478 | return rc; |
| 1479 | |
| 1480 | /* setting 0 on duration is NOP for now */ |
| 1481 | if (val <= 0) |
| 1482 | return count; |
| 1483 | |
| 1484 | if (val > chip->max_play_time_ms) |
| 1485 | return -EINVAL; |
| 1486 | |
| 1487 | mutex_lock(&chip->param_lock); |
| 1488 | rc = qpnp_haptics_auto_mode_config(chip, val); |
| 1489 | if (rc < 0) { |
| 1490 | pr_err("Unable to do auto mode config\n"); |
| 1491 | mutex_unlock(&chip->param_lock); |
| 1492 | return rc; |
| 1493 | } |
| 1494 | |
| 1495 | chip->play_time_ms = val; |
| 1496 | mutex_unlock(&chip->param_lock); |
| 1497 | |
| 1498 | return count; |
| 1499 | } |
| 1500 | |
| 1501 | static ssize_t qpnp_haptics_show_activate(struct device *dev, |
| 1502 | struct device_attribute *attr, char *buf) |
| 1503 | { |
| 1504 | /* For now nothing to show */ |
| 1505 | return snprintf(buf, PAGE_SIZE, "%d\n", 0); |
| 1506 | } |
| 1507 | |
| 1508 | static ssize_t qpnp_haptics_store_activate(struct device *dev, |
| 1509 | struct device_attribute *attr, const char *buf, size_t count) |
| 1510 | { |
| 1511 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1512 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1513 | u32 val; |
| 1514 | int rc; |
| 1515 | |
| 1516 | rc = kstrtouint(buf, 0, &val); |
| 1517 | if (rc < 0) |
| 1518 | return rc; |
| 1519 | |
| 1520 | if (val != 0 && val != 1) |
| 1521 | return count; |
| 1522 | |
| 1523 | if (val) { |
| 1524 | hrtimer_cancel(&chip->stop_timer); |
| 1525 | if (is_sw_lra_auto_resonance_control(chip)) |
| 1526 | hrtimer_cancel(&chip->auto_res_err_poll_timer); |
| 1527 | cancel_work_sync(&chip->haptics_work); |
| 1528 | |
| 1529 | atomic_set(&chip->state, 1); |
| 1530 | schedule_work(&chip->haptics_work); |
| 1531 | } else { |
| 1532 | rc = qpnp_haptics_mod_enable(chip, false); |
| 1533 | if (rc < 0) { |
| 1534 | pr_err("Error in disabling module, rc=%d\n", rc); |
| 1535 | return rc; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | return count; |
| 1540 | } |
| 1541 | |
| 1542 | static ssize_t qpnp_haptics_show_play_mode(struct device *dev, |
| 1543 | struct device_attribute *attr, char *buf) |
| 1544 | { |
| 1545 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1546 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1547 | char *str; |
| 1548 | |
| 1549 | if (chip->play_mode == HAP_BUFFER) |
| 1550 | str = "buffer"; |
| 1551 | else if (chip->play_mode == HAP_DIRECT) |
| 1552 | str = "direct"; |
| 1553 | else if (chip->play_mode == HAP_AUDIO) |
| 1554 | str = "audio"; |
| 1555 | else if (chip->play_mode == HAP_PWM) |
| 1556 | str = "pwm"; |
| 1557 | else |
| 1558 | return -EINVAL; |
| 1559 | |
| 1560 | return snprintf(buf, PAGE_SIZE, "%s\n", str); |
| 1561 | } |
| 1562 | |
| 1563 | static ssize_t qpnp_haptics_store_play_mode(struct device *dev, |
| 1564 | struct device_attribute *attr, const char *buf, size_t count) |
| 1565 | { |
| 1566 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1567 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1568 | char str[HAP_STR_SIZE + 1]; |
| 1569 | int rc = 0, temp, old_mode; |
| 1570 | |
| 1571 | rc = parse_string(buf, str); |
| 1572 | if (rc < 0) |
| 1573 | return rc; |
| 1574 | |
| 1575 | if (strcmp(str, "buffer") == 0) |
| 1576 | temp = HAP_BUFFER; |
| 1577 | else if (strcmp(str, "direct") == 0) |
| 1578 | temp = HAP_DIRECT; |
| 1579 | else if (strcmp(str, "audio") == 0) |
| 1580 | temp = HAP_AUDIO; |
| 1581 | else if (strcmp(str, "pwm") == 0) |
| 1582 | temp = HAP_PWM; |
| 1583 | else |
| 1584 | return -EINVAL; |
| 1585 | |
| 1586 | if (temp == chip->play_mode) |
| 1587 | return count; |
| 1588 | |
| 1589 | if (temp == HAP_BUFFER) { |
| 1590 | rc = qpnp_haptics_parse_buffer_dt(chip); |
| 1591 | if (!rc) { |
| 1592 | rc = qpnp_haptics_wave_rep_config(chip, |
| 1593 | HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT); |
| 1594 | if (rc < 0) { |
| 1595 | pr_err("Error in configuring wave_rep config %d\n", |
| 1596 | rc); |
| 1597 | return rc; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | rc = qpnp_haptics_buffer_config(chip, NULL, true); |
| 1602 | } else if (temp == HAP_PWM) { |
| 1603 | rc = qpnp_haptics_parse_pwm_dt(chip); |
| 1604 | if (!rc) |
| 1605 | rc = qpnp_haptics_pwm_config(chip); |
| 1606 | } |
| 1607 | |
| 1608 | if (rc < 0) |
| 1609 | return rc; |
| 1610 | |
| 1611 | rc = qpnp_haptics_mod_enable(chip, false); |
| 1612 | if (rc < 0) |
| 1613 | return rc; |
| 1614 | |
| 1615 | old_mode = chip->play_mode; |
| 1616 | chip->play_mode = temp; |
| 1617 | rc = qpnp_haptics_play_mode_config(chip); |
| 1618 | if (rc < 0) { |
| 1619 | chip->play_mode = old_mode; |
| 1620 | return rc; |
| 1621 | } |
| 1622 | |
| 1623 | if (chip->play_mode == HAP_AUDIO) { |
| 1624 | rc = qpnp_haptics_mod_enable(chip, true); |
| 1625 | if (rc < 0) { |
| 1626 | chip->play_mode = old_mode; |
| 1627 | return rc; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | return count; |
| 1632 | } |
| 1633 | |
| 1634 | static ssize_t qpnp_haptics_show_wf_samp(struct device *dev, |
| 1635 | struct device_attribute *attr, char *buf) |
| 1636 | { |
| 1637 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1638 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1639 | char str[HAP_STR_SIZE + 1]; |
| 1640 | char *ptr = str; |
| 1641 | int i, len = 0; |
| 1642 | |
| 1643 | for (i = 0; i < ARRAY_SIZE(chip->wave_samp); i++) { |
| 1644 | len = scnprintf(ptr, HAP_STR_SIZE, "%x ", chip->wave_samp[i]); |
| 1645 | ptr += len; |
| 1646 | } |
| 1647 | ptr[len] = '\0'; |
| 1648 | |
| 1649 | return snprintf(buf, PAGE_SIZE, "%s\n", str); |
| 1650 | } |
| 1651 | |
| 1652 | static ssize_t qpnp_haptics_store_wf_samp(struct device *dev, |
| 1653 | struct device_attribute *attr, const char *buf, size_t count) |
| 1654 | { |
| 1655 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1656 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1657 | u8 samp[HAP_WAVE_SAMP_SET_LEN] = {0}; |
| 1658 | int bytes_read, rc; |
| 1659 | unsigned int data, pos = 0, i = 0; |
| 1660 | |
| 1661 | while (pos < count && i < ARRAY_SIZE(samp) && |
| 1662 | sscanf(buf + pos, "%x%n", &data, &bytes_read) == 1) { |
| 1663 | /* bit 0 is not used in WF_Sx */ |
| 1664 | samp[i++] = data & GENMASK(7, 1); |
| 1665 | pos += bytes_read; |
| 1666 | } |
| 1667 | |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 1668 | chip->wf_samp_len = i; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 1669 | for (i = 0; i < ARRAY_SIZE(chip->wave_samp); i++) |
| 1670 | chip->wave_samp[i] = samp[i]; |
| 1671 | |
| 1672 | rc = qpnp_haptics_buffer_config(chip, NULL, false); |
| 1673 | if (rc < 0) { |
| 1674 | pr_err("Error in configuring buffer mode %d\n", rc); |
| 1675 | return rc; |
| 1676 | } |
| 1677 | |
| 1678 | return count; |
| 1679 | } |
| 1680 | |
| 1681 | static ssize_t qpnp_haptics_show_wf_rep_count(struct device *dev, |
| 1682 | struct device_attribute *attr, char *buf) |
| 1683 | { |
| 1684 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1685 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1686 | |
| 1687 | return snprintf(buf, PAGE_SIZE, "%d\n", chip->wave_rep_cnt); |
| 1688 | } |
| 1689 | |
| 1690 | static ssize_t qpnp_haptics_store_wf_rep_count(struct device *dev, |
| 1691 | struct device_attribute *attr, const char *buf, size_t count) |
| 1692 | { |
| 1693 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1694 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1695 | int data, rc, old_wave_rep_cnt; |
| 1696 | |
| 1697 | rc = kstrtoint(buf, 10, &data); |
| 1698 | if (rc < 0) |
| 1699 | return rc; |
| 1700 | |
| 1701 | old_wave_rep_cnt = chip->wave_rep_cnt; |
| 1702 | chip->wave_rep_cnt = data; |
| 1703 | rc = qpnp_haptics_wave_rep_config(chip, HAP_WAVE_REPEAT); |
| 1704 | if (rc < 0) { |
| 1705 | chip->wave_rep_cnt = old_wave_rep_cnt; |
| 1706 | return rc; |
| 1707 | } |
| 1708 | |
| 1709 | return count; |
| 1710 | } |
| 1711 | |
| 1712 | static ssize_t qpnp_haptics_show_wf_s_rep_count(struct device *dev, |
| 1713 | struct device_attribute *attr, char *buf) |
| 1714 | { |
| 1715 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1716 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1717 | |
| 1718 | return snprintf(buf, PAGE_SIZE, "%d\n", chip->wave_s_rep_cnt); |
| 1719 | } |
| 1720 | |
| 1721 | static ssize_t qpnp_haptics_store_wf_s_rep_count(struct device *dev, |
| 1722 | struct device_attribute *attr, const char *buf, size_t count) |
| 1723 | { |
| 1724 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1725 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1726 | int data, rc, old_wave_s_rep_cnt; |
| 1727 | |
| 1728 | rc = kstrtoint(buf, 10, &data); |
| 1729 | if (rc < 0) |
| 1730 | return rc; |
| 1731 | |
| 1732 | old_wave_s_rep_cnt = chip->wave_s_rep_cnt; |
| 1733 | chip->wave_s_rep_cnt = data; |
| 1734 | rc = qpnp_haptics_wave_rep_config(chip, HAP_WAVE_SAMP_REPEAT); |
| 1735 | if (rc < 0) { |
| 1736 | chip->wave_s_rep_cnt = old_wave_s_rep_cnt; |
| 1737 | return rc; |
| 1738 | } |
| 1739 | |
| 1740 | return count; |
| 1741 | } |
| 1742 | |
| 1743 | static ssize_t qpnp_haptics_show_vmax(struct device *dev, |
| 1744 | struct device_attribute *attr, char *buf) |
| 1745 | { |
| 1746 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1747 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1748 | |
| 1749 | return snprintf(buf, PAGE_SIZE, "%d\n", chip->vmax_mv); |
| 1750 | } |
| 1751 | |
| 1752 | static ssize_t qpnp_haptics_store_vmax(struct device *dev, |
| 1753 | struct device_attribute *attr, const char *buf, size_t count) |
| 1754 | { |
| 1755 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1756 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1757 | int data, rc, old_vmax_mv; |
| 1758 | |
| 1759 | rc = kstrtoint(buf, 10, &data); |
| 1760 | if (rc < 0) |
| 1761 | return rc; |
| 1762 | |
| 1763 | old_vmax_mv = chip->vmax_mv; |
| 1764 | chip->vmax_mv = data; |
| 1765 | rc = qpnp_haptics_vmax_config(chip, chip->vmax_mv, false); |
| 1766 | if (rc < 0) { |
| 1767 | chip->vmax_mv = old_vmax_mv; |
| 1768 | return rc; |
| 1769 | } |
| 1770 | |
| 1771 | return count; |
| 1772 | } |
| 1773 | |
| 1774 | static ssize_t qpnp_haptics_show_lra_auto_mode(struct device *dev, |
| 1775 | struct device_attribute *attr, char *buf) |
| 1776 | { |
| 1777 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1778 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1779 | |
| 1780 | return snprintf(buf, PAGE_SIZE, "%d\n", chip->lra_auto_mode); |
| 1781 | } |
| 1782 | |
| 1783 | static ssize_t qpnp_haptics_store_lra_auto_mode(struct device *dev, |
| 1784 | struct device_attribute *attr, const char *buf, size_t count) |
| 1785 | { |
| 1786 | struct led_classdev *cdev = dev_get_drvdata(dev); |
| 1787 | struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev); |
| 1788 | int rc, data; |
| 1789 | |
| 1790 | rc = kstrtoint(buf, 10, &data); |
| 1791 | if (rc < 0) |
| 1792 | return rc; |
| 1793 | |
| 1794 | if (data != 0 && data != 1) |
| 1795 | return count; |
| 1796 | |
| 1797 | chip->lra_auto_mode = !!data; |
| 1798 | return count; |
| 1799 | } |
| 1800 | |
| 1801 | static struct device_attribute qpnp_haptics_attrs[] = { |
| 1802 | __ATTR(state, 0664, qpnp_haptics_show_state, qpnp_haptics_store_state), |
| 1803 | __ATTR(duration, 0664, qpnp_haptics_show_duration, |
| 1804 | qpnp_haptics_store_duration), |
| 1805 | __ATTR(activate, 0664, qpnp_haptics_show_activate, |
| 1806 | qpnp_haptics_store_activate), |
| 1807 | __ATTR(play_mode, 0664, qpnp_haptics_show_play_mode, |
| 1808 | qpnp_haptics_store_play_mode), |
| 1809 | __ATTR(wf_samp, 0664, qpnp_haptics_show_wf_samp, |
| 1810 | qpnp_haptics_store_wf_samp), |
| 1811 | __ATTR(wf_rep_count, 0664, qpnp_haptics_show_wf_rep_count, |
| 1812 | qpnp_haptics_store_wf_rep_count), |
| 1813 | __ATTR(wf_s_rep_count, 0664, qpnp_haptics_show_wf_s_rep_count, |
| 1814 | qpnp_haptics_store_wf_s_rep_count), |
| 1815 | __ATTR(vmax_mv, 0664, qpnp_haptics_show_vmax, qpnp_haptics_store_vmax), |
| 1816 | __ATTR(lra_auto_mode, 0664, qpnp_haptics_show_lra_auto_mode, |
| 1817 | qpnp_haptics_store_lra_auto_mode), |
| 1818 | }; |
| 1819 | |
| 1820 | /* Dummy functions for brightness */ |
| 1821 | static |
| 1822 | enum led_brightness qpnp_haptics_brightness_get(struct led_classdev *cdev) |
| 1823 | { |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | static void qpnp_haptics_brightness_set(struct led_classdev *cdev, |
| 1828 | enum led_brightness level) |
| 1829 | { |
| 1830 | } |
| 1831 | |
| 1832 | static int qpnp_haptics_config(struct hap_chip *chip) |
| 1833 | { |
| 1834 | u8 rc_clk_err_deci_pct; |
| 1835 | u16 play_rate = 0; |
| 1836 | int rc; |
| 1837 | |
| 1838 | /* Configure the CFG1 register for actuator type */ |
| 1839 | rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG1_REG(chip), |
| 1840 | HAP_ACT_TYPE_MASK, chip->act_type); |
| 1841 | if (rc < 0) |
| 1842 | return rc; |
| 1843 | |
| 1844 | /* Configure auto resonance parameters */ |
| 1845 | rc = qpnp_haptics_lra_auto_res_config(chip, NULL); |
| 1846 | if (rc < 0) |
| 1847 | return rc; |
| 1848 | |
| 1849 | /* Configure the PLAY MODE register */ |
| 1850 | rc = qpnp_haptics_play_mode_config(chip); |
| 1851 | if (rc < 0) |
| 1852 | return rc; |
| 1853 | |
| 1854 | /* Configure the VMAX register */ |
| 1855 | rc = qpnp_haptics_vmax_config(chip, chip->vmax_mv, false); |
| 1856 | if (rc < 0) |
| 1857 | return rc; |
| 1858 | |
| 1859 | /* Configure the ILIM register */ |
| 1860 | rc = qpnp_haptics_ilim_config(chip); |
| 1861 | if (rc < 0) |
| 1862 | return rc; |
| 1863 | |
| 1864 | /* Configure the short circuit debounce register */ |
| 1865 | rc = qpnp_haptics_sc_deb_config(chip); |
| 1866 | if (rc < 0) |
| 1867 | return rc; |
| 1868 | |
| 1869 | /* Configure the WAVE SHAPE register */ |
| 1870 | rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG2_REG(chip), |
| 1871 | HAP_LRA_RES_TYPE_MASK, chip->wave_shape); |
| 1872 | if (rc < 0) |
| 1873 | return rc; |
| 1874 | |
| 1875 | play_rate = chip->wave_play_rate_us / HAP_RATE_CFG_STEP_US; |
| 1876 | |
| 1877 | /* |
| 1878 | * The frequency of 19.2 MHz RC clock is subject to variation. Currently |
| 1879 | * some PMI chips have MISC_TRIM_ERROR_RC19P2_CLK register present in |
| 1880 | * MISC peripheral. This register holds the trim error of RC clock. |
| 1881 | */ |
| 1882 | if (chip->act_type == HAP_LRA && chip->misc_clk_trim_error_reg) { |
| 1883 | /* |
| 1884 | * Error is available in bits[3:0] and each LSB is 0.7%. |
| 1885 | * Bit 7 is the sign bit for error code. If it is set, then a |
| 1886 | * negative error correction needs to be made. Otherwise, a |
| 1887 | * positive error correction needs to be made. |
| 1888 | */ |
| 1889 | rc_clk_err_deci_pct = (chip->clk_trim_error_code & 0x0F) * 7; |
| 1890 | if (chip->clk_trim_error_code & BIT(7)) |
| 1891 | play_rate = (play_rate * |
| 1892 | (1000 - rc_clk_err_deci_pct)) / 1000; |
| 1893 | else |
| 1894 | play_rate = (play_rate * |
| 1895 | (1000 + rc_clk_err_deci_pct)) / 1000; |
| 1896 | |
| 1897 | pr_debug("TRIM register = 0x%x, play_rate=%d\n", |
| 1898 | chip->clk_trim_error_code, play_rate); |
| 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * Configure RATE_CFG1 and RATE_CFG2 registers. |
| 1903 | * Note: For ERM these registers act as play rate and |
| 1904 | * for LRA these represent resonance period |
| 1905 | */ |
| 1906 | rc = qpnp_haptics_update_rate_cfg(chip, play_rate); |
| 1907 | if (chip->act_type == HAP_LRA) { |
| 1908 | chip->drive_period_code_max_limit = (play_rate * |
| 1909 | (100 + chip->drive_period_code_max_var_pct)) / 100; |
| 1910 | chip->drive_period_code_min_limit = (play_rate * |
| 1911 | (100 - chip->drive_period_code_min_var_pct)) / 100; |
| 1912 | pr_debug("Drive period code max limit %x min limit %x\n", |
| 1913 | chip->drive_period_code_max_limit, |
| 1914 | chip->drive_period_code_min_limit); |
| 1915 | } |
| 1916 | |
| 1917 | rc = qpnp_haptics_brake_config(chip, NULL); |
| 1918 | if (rc < 0) |
| 1919 | return rc; |
| 1920 | |
| 1921 | if (chip->play_mode == HAP_BUFFER) { |
| 1922 | rc = qpnp_haptics_wave_rep_config(chip, |
| 1923 | HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT); |
| 1924 | if (rc < 0) |
| 1925 | return rc; |
| 1926 | |
| 1927 | rc = qpnp_haptics_buffer_config(chip, NULL, false); |
| 1928 | } else if (chip->play_mode == HAP_PWM) { |
| 1929 | rc = qpnp_haptics_pwm_config(chip); |
| 1930 | } else if (chip->play_mode == HAP_AUDIO) { |
| 1931 | rc = qpnp_haptics_mod_enable(chip, true); |
| 1932 | } |
| 1933 | |
| 1934 | if (rc < 0) |
| 1935 | return rc; |
| 1936 | |
| 1937 | /* setup play irq */ |
| 1938 | if (chip->play_irq >= 0) { |
| 1939 | rc = devm_request_threaded_irq(&chip->pdev->dev, chip->play_irq, |
| 1940 | NULL, qpnp_haptics_play_irq_handler, IRQF_ONESHOT, |
| 1941 | "haptics_play_irq", chip); |
| 1942 | if (rc < 0) { |
| 1943 | pr_err("Unable to request play(%d) IRQ(err:%d)\n", |
| 1944 | chip->play_irq, rc); |
| 1945 | return rc; |
| 1946 | } |
| 1947 | |
| 1948 | /* use play_irq only for buffer mode */ |
| 1949 | if (chip->play_mode != HAP_BUFFER) { |
| 1950 | disable_irq(chip->play_irq); |
| 1951 | chip->play_irq_en = false; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /* setup short circuit irq */ |
| 1956 | if (chip->sc_irq >= 0) { |
| 1957 | rc = devm_request_threaded_irq(&chip->pdev->dev, chip->sc_irq, |
| 1958 | NULL, qpnp_haptics_sc_irq_handler, IRQF_ONESHOT, |
| 1959 | "haptics_sc_irq", chip); |
| 1960 | if (rc < 0) { |
| 1961 | pr_err("Unable to request sc(%d) IRQ(err:%d)\n", |
| 1962 | chip->sc_irq, rc); |
| 1963 | return rc; |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | return rc; |
| 1968 | } |
| 1969 | |
| 1970 | static int qpnp_haptics_parse_buffer_dt(struct hap_chip *chip) |
| 1971 | { |
| 1972 | struct device_node *node = chip->pdev->dev.of_node; |
| 1973 | u32 temp; |
| 1974 | int rc, i, wf_samp_len; |
| 1975 | |
| 1976 | if (chip->wave_rep_cnt > 0 || chip->wave_s_rep_cnt > 0) |
| 1977 | return 0; |
| 1978 | |
| 1979 | chip->wave_rep_cnt = WF_REPEAT_MIN; |
| 1980 | rc = of_property_read_u32(node, "qcom,wave-rep-cnt", &temp); |
| 1981 | if (!rc) { |
| 1982 | chip->wave_rep_cnt = temp; |
| 1983 | } else if (rc != -EINVAL) { |
| 1984 | pr_err("Unable to read rep cnt rc=%d\n", rc); |
| 1985 | return rc; |
| 1986 | } |
| 1987 | |
| 1988 | chip->wave_s_rep_cnt = WF_S_REPEAT_MIN; |
| 1989 | rc = of_property_read_u32(node, |
| 1990 | "qcom,wave-samp-rep-cnt", &temp); |
| 1991 | if (!rc) { |
| 1992 | chip->wave_s_rep_cnt = temp; |
| 1993 | } else if (rc != -EINVAL) { |
| 1994 | pr_err("Unable to read samp rep cnt rc=%d\n", rc); |
| 1995 | return rc; |
| 1996 | } |
| 1997 | |
| 1998 | wf_samp_len = of_property_count_elems_of_size(node, |
| 1999 | "qcom,wave-samples", sizeof(u32)); |
| 2000 | if (wf_samp_len > 0) { |
| 2001 | if (wf_samp_len > HAP_WAVE_SAMP_SET_LEN) { |
| 2002 | pr_err("Invalid length for wave samples\n"); |
| 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | |
| 2006 | rc = of_property_read_u32_array(node, "qcom,wave-samples", |
| 2007 | chip->wave_samp, wf_samp_len); |
| 2008 | if (rc < 0) { |
| 2009 | pr_err("Error in reading qcom,wave-samples, rc=%d\n", |
| 2010 | rc); |
| 2011 | return rc; |
| 2012 | } |
| 2013 | } else { |
| 2014 | /* Use default values */ |
| 2015 | for (i = 0; i < HAP_WAVE_SAMP_LEN; i++) |
| 2016 | chip->wave_samp[i] = HAP_WF_SAMP_MAX; |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 2017 | |
| 2018 | wf_samp_len = HAP_WAVE_SAMP_LEN; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2019 | } |
Subbaraman Narayanamurthy | 6ff7c75 | 2018-01-08 14:57:12 -0800 | [diff] [blame] | 2020 | chip->wf_samp_len = wf_samp_len; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2021 | |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
| 2025 | static int qpnp_haptics_parse_pwm_dt(struct hap_chip *chip) |
| 2026 | { |
| 2027 | struct device_node *node = chip->pdev->dev.of_node; |
| 2028 | u32 temp; |
| 2029 | int rc; |
| 2030 | |
| 2031 | if (chip->pwm_data.period_us > 0 && chip->pwm_data.duty_us > 0) |
| 2032 | return 0; |
| 2033 | |
| 2034 | chip->pwm_data.pwm_dev = of_pwm_get(node, NULL); |
| 2035 | if (IS_ERR(chip->pwm_data.pwm_dev)) { |
| 2036 | rc = PTR_ERR(chip->pwm_data.pwm_dev); |
| 2037 | pr_err("Cannot get PWM device rc=%d\n", rc); |
| 2038 | chip->pwm_data.pwm_dev = NULL; |
| 2039 | return rc; |
| 2040 | } |
| 2041 | |
| 2042 | rc = of_property_read_u32(node, "qcom,period-us", &temp); |
| 2043 | if (!rc) { |
| 2044 | chip->pwm_data.period_us = temp; |
| 2045 | } else { |
| 2046 | pr_err("Cannot read PWM period rc=%d\n", rc); |
| 2047 | return rc; |
| 2048 | } |
| 2049 | |
| 2050 | rc = of_property_read_u32(node, "qcom,duty-us", &temp); |
| 2051 | if (!rc) { |
| 2052 | chip->pwm_data.duty_us = temp; |
| 2053 | } else { |
| 2054 | pr_err("Cannot read PWM duty rc=%d\n", rc); |
| 2055 | return rc; |
| 2056 | } |
| 2057 | |
| 2058 | rc = of_property_read_u32(node, "qcom,ext-pwm-dtest-line", &temp); |
| 2059 | if (!rc) |
| 2060 | chip->ext_pwm_dtest_line = temp; |
| 2061 | |
| 2062 | rc = of_property_read_u32(node, "qcom,ext-pwm-freq-khz", &temp); |
| 2063 | if (!rc) { |
| 2064 | chip->ext_pwm_freq_khz = temp; |
| 2065 | } else if (rc != -EINVAL) { |
| 2066 | pr_err("Unable to read ext pwm freq rc=%d\n", rc); |
| 2067 | return rc; |
| 2068 | } |
| 2069 | |
| 2070 | return 0; |
| 2071 | } |
| 2072 | |
| 2073 | static int qpnp_haptics_parse_dt(struct hap_chip *chip) |
| 2074 | { |
| 2075 | struct device_node *node = chip->pdev->dev.of_node; |
| 2076 | struct device_node *revid_node, *misc_node; |
| 2077 | const char *temp_str; |
| 2078 | int rc, temp; |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 2079 | struct regulator *vcc_pon; |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2080 | |
| 2081 | rc = of_property_read_u32(node, "reg", &temp); |
| 2082 | if (rc < 0) { |
| 2083 | pr_err("Couldn't find reg in node = %s rc = %d\n", |
| 2084 | node->full_name, rc); |
| 2085 | return rc; |
| 2086 | } |
| 2087 | |
| 2088 | if (temp <= 0) { |
| 2089 | pr_err("Invalid base address %x\n", temp); |
| 2090 | return -EINVAL; |
| 2091 | } |
| 2092 | chip->base = (u16)temp; |
| 2093 | |
| 2094 | revid_node = of_parse_phandle(node, "qcom,pmic-revid", 0); |
| 2095 | if (!revid_node) { |
| 2096 | pr_err("Missing qcom,pmic-revid property\n"); |
| 2097 | return -EINVAL; |
| 2098 | } |
| 2099 | |
| 2100 | chip->revid = get_revid_data(revid_node); |
| 2101 | of_node_put(revid_node); |
| 2102 | if (IS_ERR_OR_NULL(chip->revid)) { |
| 2103 | pr_err("Unable to get pmic_revid rc=%ld\n", |
| 2104 | PTR_ERR(chip->revid)); |
| 2105 | /* |
| 2106 | * the revid peripheral must be registered, any failure |
| 2107 | * here only indicates that the rev-id module has not |
| 2108 | * probed yet. |
| 2109 | */ |
| 2110 | return -EPROBE_DEFER; |
| 2111 | } |
| 2112 | |
| 2113 | if (of_find_property(node, "qcom,pmic-misc", NULL)) { |
| 2114 | misc_node = of_parse_phandle(node, "qcom,pmic-misc", 0); |
| 2115 | if (!misc_node) |
| 2116 | return -EINVAL; |
| 2117 | |
| 2118 | rc = of_property_read_u32(node, "qcom,misc-clk-trim-error-reg", |
| 2119 | &chip->misc_clk_trim_error_reg); |
| 2120 | if (rc < 0 || !chip->misc_clk_trim_error_reg) { |
| 2121 | pr_err("Invalid or missing misc-clk-trim-error-reg\n"); |
| 2122 | of_node_put(misc_node); |
| 2123 | return rc; |
| 2124 | } |
| 2125 | |
| 2126 | rc = qpnp_misc_read_reg(misc_node, |
| 2127 | chip->misc_clk_trim_error_reg, |
| 2128 | &chip->clk_trim_error_code); |
| 2129 | if (rc < 0) { |
| 2130 | pr_err("Couldn't get clk_trim_error_code, rc=%d\n", rc); |
| 2131 | of_node_put(misc_node); |
| 2132 | return -EPROBE_DEFER; |
| 2133 | } |
| 2134 | of_node_put(misc_node); |
| 2135 | } |
| 2136 | |
| 2137 | chip->play_irq = platform_get_irq_byname(chip->pdev, "hap-play-irq"); |
| 2138 | if (chip->play_irq < 0) { |
| 2139 | pr_err("Unable to get play irq\n"); |
| 2140 | return chip->play_irq; |
| 2141 | } |
| 2142 | |
| 2143 | chip->sc_irq = platform_get_irq_byname(chip->pdev, "hap-sc-irq"); |
| 2144 | if (chip->sc_irq < 0) { |
| 2145 | pr_err("Unable to get sc irq\n"); |
| 2146 | return chip->sc_irq; |
| 2147 | } |
| 2148 | |
| 2149 | chip->act_type = HAP_LRA; |
| 2150 | rc = of_property_read_u32(node, "qcom,actuator-type", &temp); |
| 2151 | if (!rc) { |
| 2152 | if (temp != HAP_LRA && temp != HAP_ERM) { |
| 2153 | pr_err("Incorrect actuator type\n"); |
| 2154 | return -EINVAL; |
| 2155 | } |
| 2156 | chip->act_type = temp; |
| 2157 | } |
| 2158 | |
| 2159 | chip->lra_auto_mode = of_property_read_bool(node, "qcom,lra-auto-mode"); |
| 2160 | |
| 2161 | rc = of_property_read_string(node, "qcom,play-mode", &temp_str); |
| 2162 | if (!rc) { |
| 2163 | if (strcmp(temp_str, "direct") == 0) |
| 2164 | chip->play_mode = HAP_DIRECT; |
| 2165 | else if (strcmp(temp_str, "buffer") == 0) |
| 2166 | chip->play_mode = HAP_BUFFER; |
| 2167 | else if (strcmp(temp_str, "pwm") == 0) |
| 2168 | chip->play_mode = HAP_PWM; |
| 2169 | else if (strcmp(temp_str, "audio") == 0) |
| 2170 | chip->play_mode = HAP_AUDIO; |
| 2171 | else { |
| 2172 | pr_err("Invalid play mode\n"); |
| 2173 | return -EINVAL; |
| 2174 | } |
| 2175 | } else { |
| 2176 | if (rc == -EINVAL && chip->act_type == HAP_LRA) { |
| 2177 | pr_info("Play mode not specified, using auto mode\n"); |
| 2178 | chip->lra_auto_mode = true; |
| 2179 | } else { |
| 2180 | pr_err("Unable to read play mode\n"); |
| 2181 | return rc; |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | chip->max_play_time_ms = HAP_MAX_PLAY_TIME_MS; |
| 2186 | rc = of_property_read_u32(node, "qcom,max-play-time-ms", &temp); |
| 2187 | if (!rc) { |
| 2188 | chip->max_play_time_ms = temp; |
| 2189 | } else if (rc != -EINVAL) { |
| 2190 | pr_err("Unable to read max-play-time rc=%d\n", rc); |
| 2191 | return rc; |
| 2192 | } |
| 2193 | |
| 2194 | chip->vmax_mv = HAP_VMAX_MAX_MV; |
| 2195 | rc = of_property_read_u32(node, "qcom,vmax-mv", &temp); |
| 2196 | if (!rc) { |
| 2197 | chip->vmax_mv = temp; |
| 2198 | } else if (rc != -EINVAL) { |
| 2199 | pr_err("Unable to read Vmax rc=%d\n", rc); |
| 2200 | return rc; |
| 2201 | } |
| 2202 | |
| 2203 | chip->ilim_ma = HAP_ILIM_400_MA; |
| 2204 | rc = of_property_read_u32(node, "qcom,ilim-ma", &temp); |
| 2205 | if (!rc) { |
| 2206 | chip->ilim_ma = (u8)temp; |
| 2207 | } else if (rc != -EINVAL) { |
| 2208 | pr_err("Unable to read ILIM rc=%d\n", rc); |
| 2209 | return rc; |
| 2210 | } |
| 2211 | |
| 2212 | chip->sc_deb_cycles = HAP_DEF_SC_DEB_CYCLES; |
| 2213 | rc = of_property_read_u32(node, "qcom,sc-dbc-cycles", &temp); |
| 2214 | if (!rc) { |
| 2215 | chip->sc_deb_cycles = temp; |
| 2216 | } else if (rc != -EINVAL) { |
| 2217 | pr_err("Unable to read sc debounce rc=%d\n", rc); |
| 2218 | return rc; |
| 2219 | } |
| 2220 | |
| 2221 | chip->wave_shape = HAP_WAVE_SQUARE; |
| 2222 | rc = of_property_read_string(node, "qcom,wave-shape", &temp_str); |
| 2223 | if (!rc) { |
| 2224 | if (strcmp(temp_str, "sine") == 0) |
| 2225 | chip->wave_shape = HAP_WAVE_SINE; |
| 2226 | else if (strcmp(temp_str, "square") == 0) |
| 2227 | chip->wave_shape = HAP_WAVE_SQUARE; |
| 2228 | else { |
| 2229 | pr_err("Unsupported wave shape\n"); |
| 2230 | return -EINVAL; |
| 2231 | } |
| 2232 | } else if (rc != -EINVAL) { |
| 2233 | pr_err("Unable to read wave shape rc=%d\n", rc); |
| 2234 | return rc; |
| 2235 | } |
| 2236 | |
| 2237 | chip->wave_play_rate_us = HAP_DEF_WAVE_PLAY_RATE_US; |
| 2238 | rc = of_property_read_u32(node, |
| 2239 | "qcom,wave-play-rate-us", &temp); |
| 2240 | if (!rc) { |
| 2241 | chip->wave_play_rate_us = temp; |
| 2242 | } else if (rc != -EINVAL) { |
| 2243 | pr_err("Unable to read play rate rc=%d\n", rc); |
| 2244 | return rc; |
| 2245 | } |
| 2246 | |
| 2247 | if (chip->wave_play_rate_us < HAP_WAVE_PLAY_RATE_US_MIN) |
| 2248 | chip->wave_play_rate_us = HAP_WAVE_PLAY_RATE_US_MIN; |
| 2249 | else if (chip->wave_play_rate_us > HAP_WAVE_PLAY_RATE_US_MAX) |
| 2250 | chip->wave_play_rate_us = HAP_WAVE_PLAY_RATE_US_MAX; |
| 2251 | |
| 2252 | chip->en_brake = of_property_read_bool(node, "qcom,en-brake"); |
| 2253 | |
| 2254 | rc = of_property_count_elems_of_size(node, |
| 2255 | "qcom,brake-pattern", sizeof(u32)); |
| 2256 | if (rc > 0) { |
| 2257 | if (rc != HAP_BRAKE_PAT_LEN) { |
| 2258 | pr_err("Invalid length for brake pattern\n"); |
| 2259 | return -EINVAL; |
| 2260 | } |
| 2261 | |
| 2262 | rc = of_property_read_u32_array(node, "qcom,brake-pattern", |
| 2263 | chip->brake_pat, HAP_BRAKE_PAT_LEN); |
| 2264 | if (rc < 0) { |
| 2265 | pr_err("Error in reading qcom,brake-pattern, rc=%d\n", |
| 2266 | rc); |
| 2267 | return rc; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | /* Read the following properties only for LRA */ |
| 2272 | if (chip->act_type == HAP_LRA) { |
| 2273 | rc = of_property_read_string(node, "qcom,lra-auto-res-mode", |
| 2274 | &temp_str); |
| 2275 | if (!rc) { |
| 2276 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 2277 | chip->ares_cfg.auto_res_mode = |
| 2278 | HAP_PM660_AUTO_RES_QWD; |
| 2279 | if (strcmp(temp_str, "zxd") == 0) |
| 2280 | chip->ares_cfg.auto_res_mode = |
| 2281 | HAP_PM660_AUTO_RES_ZXD; |
| 2282 | else if (strcmp(temp_str, "qwd") == 0) |
| 2283 | chip->ares_cfg.auto_res_mode = |
| 2284 | HAP_PM660_AUTO_RES_QWD; |
| 2285 | } else { |
| 2286 | chip->ares_cfg.auto_res_mode = |
| 2287 | HAP_AUTO_RES_ZXD_EOP; |
| 2288 | if (strcmp(temp_str, "none") == 0) |
| 2289 | chip->ares_cfg.auto_res_mode = |
| 2290 | HAP_AUTO_RES_NONE; |
| 2291 | else if (strcmp(temp_str, "zxd") == 0) |
| 2292 | chip->ares_cfg.auto_res_mode = |
| 2293 | HAP_AUTO_RES_ZXD; |
| 2294 | else if (strcmp(temp_str, "qwd") == 0) |
| 2295 | chip->ares_cfg.auto_res_mode = |
| 2296 | HAP_AUTO_RES_QWD; |
| 2297 | else if (strcmp(temp_str, "max-qwd") == 0) |
| 2298 | chip->ares_cfg.auto_res_mode = |
| 2299 | HAP_AUTO_RES_MAX_QWD; |
| 2300 | else |
| 2301 | chip->ares_cfg.auto_res_mode = |
| 2302 | HAP_AUTO_RES_ZXD_EOP; |
| 2303 | } |
| 2304 | } else if (rc != -EINVAL) { |
| 2305 | pr_err("Unable to read auto res mode rc=%d\n", rc); |
| 2306 | return rc; |
| 2307 | } |
| 2308 | |
| 2309 | chip->ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT3; |
| 2310 | rc = of_property_read_string(node, "qcom,lra-high-z", |
| 2311 | &temp_str); |
| 2312 | if (!rc) { |
| 2313 | if (strcmp(temp_str, "none") == 0) |
| 2314 | chip->ares_cfg.lra_high_z = |
| 2315 | HAP_LRA_HIGH_Z_NONE; |
| 2316 | else if (strcmp(temp_str, "opt1") == 0) |
| 2317 | chip->ares_cfg.lra_high_z = |
| 2318 | HAP_LRA_HIGH_Z_OPT1; |
| 2319 | else if (strcmp(temp_str, "opt2") == 0) |
| 2320 | chip->ares_cfg.lra_high_z = |
| 2321 | HAP_LRA_HIGH_Z_OPT2; |
| 2322 | else |
| 2323 | chip->ares_cfg.lra_high_z = |
| 2324 | HAP_LRA_HIGH_Z_OPT3; |
| 2325 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 2326 | if (strcmp(temp_str, "opt0") == 0) |
| 2327 | chip->ares_cfg.lra_high_z = |
| 2328 | HAP_LRA_HIGH_Z_NONE; |
| 2329 | } |
| 2330 | } else if (rc != -EINVAL) { |
| 2331 | pr_err("Unable to read LRA high-z rc=%d\n", rc); |
| 2332 | return rc; |
| 2333 | } |
| 2334 | |
| 2335 | chip->ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MAX; |
| 2336 | rc = of_property_read_u32(node, |
| 2337 | "qcom,lra-res-cal-period", &temp); |
| 2338 | if (!rc) { |
| 2339 | chip->ares_cfg.lra_res_cal_period = temp; |
| 2340 | } else if (rc != -EINVAL) { |
| 2341 | pr_err("Unable to read cal period rc=%d\n", rc); |
| 2342 | return rc; |
| 2343 | } |
| 2344 | |
| 2345 | chip->ares_cfg.lra_qwd_drive_duration = -EINVAL; |
| 2346 | chip->ares_cfg.calibrate_at_eop = -EINVAL; |
| 2347 | if (chip->revid->pmic_subtype == PM660_SUBTYPE) { |
| 2348 | rc = of_property_read_u32(node, |
| 2349 | "qcom,lra-qwd-drive-duration", |
| 2350 | &chip->ares_cfg.lra_qwd_drive_duration); |
| 2351 | if (rc && rc != -EINVAL) { |
| 2352 | pr_err("Unable to read LRA QWD drive duration rc=%d\n", |
| 2353 | rc); |
| 2354 | return rc; |
| 2355 | } |
| 2356 | |
| 2357 | rc = of_property_read_u32(node, |
| 2358 | "qcom,lra-calibrate-at-eop", |
| 2359 | &chip->ares_cfg.calibrate_at_eop); |
| 2360 | if (rc && rc != -EINVAL) { |
| 2361 | pr_err("Unable to read Calibrate at EOP rc=%d\n", |
| 2362 | rc); |
| 2363 | return rc; |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | chip->drive_period_code_max_var_pct = 25; |
| 2368 | rc = of_property_read_u32(node, |
| 2369 | "qcom,drive-period-code-max-variation-pct", &temp); |
| 2370 | if (!rc) { |
| 2371 | if (temp > 0 && temp < 100) |
| 2372 | chip->drive_period_code_max_var_pct = (u8)temp; |
| 2373 | } else if (rc != -EINVAL) { |
| 2374 | pr_err("Unable to read drive period code max var pct rc=%d\n", |
| 2375 | rc); |
| 2376 | return rc; |
| 2377 | } |
| 2378 | |
| 2379 | chip->drive_period_code_min_var_pct = 25; |
| 2380 | rc = of_property_read_u32(node, |
| 2381 | "qcom,drive-period-code-min-variation-pct", &temp); |
| 2382 | if (!rc) { |
| 2383 | if (temp > 0 && temp < 100) |
| 2384 | chip->drive_period_code_min_var_pct = (u8)temp; |
| 2385 | } else if (rc != -EINVAL) { |
| 2386 | pr_err("Unable to read drive period code min var pct rc=%d\n", |
| 2387 | rc); |
| 2388 | return rc; |
| 2389 | } |
| 2390 | |
| 2391 | chip->auto_res_err_recovery_hw = |
| 2392 | of_property_read_bool(node, |
| 2393 | "qcom,auto-res-err-recovery-hw"); |
| 2394 | |
| 2395 | if (chip->revid->pmic_subtype != PM660_SUBTYPE) |
| 2396 | chip->auto_res_err_recovery_hw = false; |
| 2397 | } |
| 2398 | |
| 2399 | if (rc == -EINVAL) |
| 2400 | rc = 0; |
| 2401 | |
| 2402 | if (chip->play_mode == HAP_BUFFER) |
| 2403 | rc = qpnp_haptics_parse_buffer_dt(chip); |
| 2404 | else if (chip->play_mode == HAP_PWM) |
| 2405 | rc = qpnp_haptics_parse_pwm_dt(chip); |
| 2406 | |
Vamshi Krishna B V | e99b337 | 2018-05-02 14:43:32 +0530 | [diff] [blame] | 2407 | if (of_find_property(node, "vcc_pon-supply", NULL)) { |
| 2408 | vcc_pon = regulator_get(&chip->pdev->dev, "vcc_pon"); |
| 2409 | if (IS_ERR(vcc_pon)) { |
| 2410 | rc = PTR_ERR(vcc_pon); |
| 2411 | dev_err(&chip->pdev->dev, |
| 2412 | "regulator get failed vcc_pon rc=%d\n", rc); |
| 2413 | } |
| 2414 | chip->vcc_pon = vcc_pon; |
| 2415 | } |
| 2416 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2417 | return rc; |
| 2418 | } |
| 2419 | |
| 2420 | static int qpnp_haptics_probe(struct platform_device *pdev) |
| 2421 | { |
| 2422 | struct hap_chip *chip; |
| 2423 | int rc, i; |
| 2424 | |
| 2425 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| 2426 | if (!chip) |
| 2427 | return -ENOMEM; |
| 2428 | |
| 2429 | chip->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 2430 | if (!chip->regmap) { |
| 2431 | dev_err(&pdev->dev, "Couldn't get parent's regmap\n"); |
| 2432 | return -EINVAL; |
| 2433 | } |
| 2434 | |
| 2435 | chip->pdev = pdev; |
| 2436 | rc = qpnp_haptics_parse_dt(chip); |
| 2437 | if (rc < 0) { |
| 2438 | dev_err(&pdev->dev, "Error in parsing DT parameters, rc=%d\n", |
| 2439 | rc); |
| 2440 | return rc; |
| 2441 | } |
| 2442 | |
| 2443 | spin_lock_init(&chip->bus_lock); |
| 2444 | mutex_init(&chip->play_lock); |
| 2445 | mutex_init(&chip->param_lock); |
| 2446 | INIT_WORK(&chip->haptics_work, qpnp_haptics_work); |
| 2447 | |
| 2448 | rc = qpnp_haptics_config(chip); |
| 2449 | if (rc < 0) { |
| 2450 | dev_err(&pdev->dev, "Error in configuring haptics, rc=%d\n", |
| 2451 | rc); |
| 2452 | goto fail; |
| 2453 | } |
| 2454 | |
| 2455 | hrtimer_init(&chip->stop_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2456 | chip->stop_timer.function = hap_stop_timer; |
| 2457 | hrtimer_init(&chip->auto_res_err_poll_timer, CLOCK_MONOTONIC, |
| 2458 | HRTIMER_MODE_REL); |
| 2459 | chip->auto_res_err_poll_timer.function = hap_auto_res_err_poll_timer; |
| 2460 | dev_set_drvdata(&pdev->dev, chip); |
| 2461 | |
| 2462 | chip->cdev.name = "vibrator"; |
| 2463 | chip->cdev.brightness_get = qpnp_haptics_brightness_get; |
| 2464 | chip->cdev.brightness_set = qpnp_haptics_brightness_set; |
| 2465 | chip->cdev.max_brightness = 100; |
| 2466 | rc = devm_led_classdev_register(&pdev->dev, &chip->cdev); |
| 2467 | if (rc < 0) { |
| 2468 | dev_err(&pdev->dev, "Error in registering led class device, rc=%d\n", |
| 2469 | rc); |
| 2470 | goto register_fail; |
| 2471 | } |
| 2472 | |
| 2473 | for (i = 0; i < ARRAY_SIZE(qpnp_haptics_attrs); i++) { |
| 2474 | rc = sysfs_create_file(&chip->cdev.dev->kobj, |
| 2475 | &qpnp_haptics_attrs[i].attr); |
| 2476 | if (rc < 0) { |
| 2477 | dev_err(&pdev->dev, "Error in creating sysfs file, rc=%d\n", |
| 2478 | rc); |
| 2479 | goto sysfs_fail; |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | return 0; |
| 2484 | |
| 2485 | sysfs_fail: |
| 2486 | for (--i; i >= 0; i--) |
| 2487 | sysfs_remove_file(&chip->cdev.dev->kobj, |
| 2488 | &qpnp_haptics_attrs[i].attr); |
| 2489 | register_fail: |
| 2490 | cancel_work_sync(&chip->haptics_work); |
| 2491 | hrtimer_cancel(&chip->auto_res_err_poll_timer); |
| 2492 | hrtimer_cancel(&chip->stop_timer); |
| 2493 | fail: |
| 2494 | mutex_destroy(&chip->play_lock); |
| 2495 | mutex_destroy(&chip->param_lock); |
| 2496 | if (chip->pwm_data.pwm_dev) |
| 2497 | pwm_put(chip->pwm_data.pwm_dev); |
| 2498 | dev_set_drvdata(&pdev->dev, NULL); |
| 2499 | return rc; |
| 2500 | } |
| 2501 | |
| 2502 | static int qpnp_haptics_remove(struct platform_device *pdev) |
| 2503 | { |
| 2504 | struct hap_chip *chip = dev_get_drvdata(&pdev->dev); |
| 2505 | |
| 2506 | cancel_work_sync(&chip->haptics_work); |
| 2507 | hrtimer_cancel(&chip->auto_res_err_poll_timer); |
| 2508 | hrtimer_cancel(&chip->stop_timer); |
| 2509 | mutex_destroy(&chip->play_lock); |
| 2510 | mutex_destroy(&chip->param_lock); |
| 2511 | if (chip->pwm_data.pwm_dev) |
| 2512 | pwm_put(chip->pwm_data.pwm_dev); |
| 2513 | dev_set_drvdata(&pdev->dev, NULL); |
| 2514 | |
| 2515 | return 0; |
| 2516 | } |
| 2517 | |
Anirudh Ghayal | 628a5f1 | 2018-07-19 19:34:41 +0530 | [diff] [blame] | 2518 | static void qpnp_haptics_shutdown(struct platform_device *pdev) |
| 2519 | { |
| 2520 | struct hap_chip *chip = dev_get_drvdata(&pdev->dev); |
| 2521 | |
| 2522 | cancel_work_sync(&chip->haptics_work); |
| 2523 | |
| 2524 | /* disable haptics */ |
| 2525 | qpnp_haptics_mod_enable(chip, false); |
| 2526 | } |
| 2527 | |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2528 | static const struct dev_pm_ops qpnp_haptics_pm_ops = { |
| 2529 | .suspend = qpnp_haptics_suspend, |
| 2530 | }; |
| 2531 | |
| 2532 | static const struct of_device_id hap_match_table[] = { |
| 2533 | { .compatible = "qcom,qpnp-haptics" }, |
| 2534 | { }, |
| 2535 | }; |
| 2536 | |
| 2537 | static struct platform_driver qpnp_haptics_driver = { |
| 2538 | .driver = { |
| 2539 | .name = "qcom,qpnp-haptics", |
| 2540 | .of_match_table = hap_match_table, |
| 2541 | .pm = &qpnp_haptics_pm_ops, |
| 2542 | }, |
| 2543 | .probe = qpnp_haptics_probe, |
| 2544 | .remove = qpnp_haptics_remove, |
Anirudh Ghayal | 628a5f1 | 2018-07-19 19:34:41 +0530 | [diff] [blame] | 2545 | .shutdown = qpnp_haptics_shutdown, |
Subbaraman Narayanamurthy | 0b5fbf3 | 2017-05-11 18:50:43 -0700 | [diff] [blame] | 2546 | }; |
| 2547 | module_platform_driver(qpnp_haptics_driver); |
| 2548 | |
| 2549 | MODULE_DESCRIPTION("QPNP haptics driver"); |
| 2550 | MODULE_LICENSE("GPL v2"); |