0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/err.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/gpio.h>
0015 #include <linux/input.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/property.h>
0018 #include <linux/regulator/consumer.h>
0019
0020 #include <sound/jack.h>
0021 #include <sound/soc.h>
0022
0023 #include <linux/mfd/arizona/core.h>
0024 #include <linux/mfd/arizona/pdata.h>
0025 #include <linux/mfd/arizona/registers.h>
0026 #include <dt-bindings/mfd/arizona.h>
0027
0028 #include "arizona.h"
0029
0030 #define ARIZONA_MAX_MICD_RANGE 8
0031
0032
0033
0034
0035
0036 #define ARIZONA_MAX_MICD_BUTTONS 6
0037
0038 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
0039 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
0040 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
0041 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
0042
0043 #define ARIZONA_TST_CAP_DEFAULT 0x3
0044 #define ARIZONA_TST_CAP_CLAMP 0x1
0045
0046 #define ARIZONA_HPDET_MAX 10000
0047
0048 #define HPDET_DEBOUNCE 500
0049 #define DEFAULT_MICD_TIMEOUT 2000
0050
0051 #define ARIZONA_HPDET_WAIT_COUNT 15
0052 #define ARIZONA_HPDET_WAIT_DELAY_MS 20
0053
0054 #define QUICK_HEADPHONE_MAX_OHM 3
0055 #define MICROPHONE_MIN_OHM 1257
0056 #define MICROPHONE_MAX_OHM 30000
0057
0058 #define MICD_DBTIME_TWO_READINGS 2
0059 #define MICD_DBTIME_FOUR_READINGS 4
0060
0061 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
0062 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
0063 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
0064 ARIZONA_MICD_LVL_7)
0065
0066 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
0067
0068 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
0069
0070 static const struct arizona_micd_config micd_default_modes[] = {
0071 { ARIZONA_ACCDET_SRC, 1, 0 },
0072 { 0, 2, 1 },
0073 };
0074
0075 static const struct arizona_micd_range micd_default_ranges[] = {
0076 { .max = 11, .key = BTN_0 },
0077 { .max = 28, .key = BTN_1 },
0078 { .max = 54, .key = BTN_2 },
0079 { .max = 100, .key = BTN_3 },
0080 { .max = 186, .key = BTN_4 },
0081 { .max = 430, .key = BTN_5 },
0082 };
0083
0084
0085 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
0086
0087 static const int arizona_micd_levels[] = {
0088 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
0089 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
0090 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
0091 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
0092 1257, 30000,
0093 };
0094
0095 static void arizona_start_hpdet_acc_id(struct arizona_priv *info);
0096
0097 static void arizona_extcon_hp_clamp(struct arizona_priv *info,
0098 bool clamp)
0099 {
0100 struct arizona *arizona = info->arizona;
0101 unsigned int mask = 0, val = 0;
0102 unsigned int cap_sel = 0;
0103 int ret;
0104
0105 switch (arizona->type) {
0106 case WM8998:
0107 case WM1814:
0108 mask = 0;
0109 break;
0110 case WM5110:
0111 case WM8280:
0112 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
0113 ARIZONA_HP1L_SHRTI;
0114 if (clamp) {
0115 val = ARIZONA_HP1L_SHRTO;
0116 cap_sel = ARIZONA_TST_CAP_CLAMP;
0117 } else {
0118 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
0119 cap_sel = ARIZONA_TST_CAP_DEFAULT;
0120 }
0121
0122 ret = regmap_update_bits(arizona->regmap,
0123 ARIZONA_HP_TEST_CTRL_1,
0124 ARIZONA_HP1_TST_CAP_SEL_MASK,
0125 cap_sel);
0126 if (ret)
0127 dev_warn(arizona->dev, "Failed to set TST_CAP_SEL: %d\n", ret);
0128 break;
0129 default:
0130 mask = ARIZONA_RMV_SHRT_HP1L;
0131 if (clamp)
0132 val = ARIZONA_RMV_SHRT_HP1L;
0133 break;
0134 }
0135
0136 snd_soc_dapm_mutex_lock(arizona->dapm);
0137
0138 arizona->hpdet_clamp = clamp;
0139
0140
0141 if (clamp) {
0142 ret = regmap_update_bits(arizona->regmap,
0143 ARIZONA_OUTPUT_ENABLES_1,
0144 ARIZONA_OUT1L_ENA |
0145 ARIZONA_OUT1R_ENA, 0);
0146 if (ret)
0147 dev_warn(arizona->dev, "Failed to disable headphone outputs: %d\n", ret);
0148 }
0149
0150 if (mask) {
0151 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
0152 mask, val);
0153 if (ret)
0154 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
0155
0156 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
0157 mask, val);
0158 if (ret)
0159 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
0160 }
0161
0162
0163 if (!clamp) {
0164 ret = regmap_update_bits(arizona->regmap,
0165 ARIZONA_OUTPUT_ENABLES_1,
0166 ARIZONA_OUT1L_ENA |
0167 ARIZONA_OUT1R_ENA, arizona->hp_ena);
0168 if (ret)
0169 dev_warn(arizona->dev, "Failed to restore headphone outputs: %d\n", ret);
0170 }
0171
0172 snd_soc_dapm_mutex_unlock(arizona->dapm);
0173 }
0174
0175 static void arizona_extcon_set_mode(struct arizona_priv *info, int mode)
0176 {
0177 struct arizona *arizona = info->arizona;
0178
0179 mode %= info->micd_num_modes;
0180
0181 gpiod_set_value_cansleep(info->micd_pol_gpio,
0182 info->micd_modes[mode].gpio);
0183
0184 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
0185 ARIZONA_MICD_BIAS_SRC_MASK,
0186 info->micd_modes[mode].bias <<
0187 ARIZONA_MICD_BIAS_SRC_SHIFT);
0188 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
0189 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
0190
0191 info->micd_mode = mode;
0192
0193 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
0194 }
0195
0196 static const char *arizona_extcon_get_micbias(struct arizona_priv *info)
0197 {
0198 switch (info->micd_modes[0].bias) {
0199 case 1:
0200 return "MICBIAS1";
0201 case 2:
0202 return "MICBIAS2";
0203 case 3:
0204 return "MICBIAS3";
0205 default:
0206 return "MICVDD";
0207 }
0208 }
0209
0210 static void arizona_extcon_pulse_micbias(struct arizona_priv *info)
0211 {
0212 struct arizona *arizona = info->arizona;
0213 const char *widget = arizona_extcon_get_micbias(info);
0214 struct snd_soc_dapm_context *dapm = arizona->dapm;
0215 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
0216 int ret;
0217
0218 ret = snd_soc_component_force_enable_pin(component, widget);
0219 if (ret)
0220 dev_warn(arizona->dev, "Failed to enable %s: %d\n", widget, ret);
0221
0222 snd_soc_dapm_sync(dapm);
0223
0224 if (!arizona->pdata.micd_force_micbias) {
0225 ret = snd_soc_component_disable_pin(component, widget);
0226 if (ret)
0227 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
0228
0229 snd_soc_dapm_sync(dapm);
0230 }
0231 }
0232
0233 static void arizona_start_mic(struct arizona_priv *info)
0234 {
0235 struct arizona *arizona = info->arizona;
0236 bool change;
0237 int ret;
0238 unsigned int mode;
0239
0240
0241 pm_runtime_get_sync(arizona->dev);
0242
0243 if (info->detecting) {
0244 ret = regulator_allow_bypass(info->micvdd, false);
0245 if (ret)
0246 dev_err(arizona->dev, "Failed to regulate MICVDD: %d\n", ret);
0247 }
0248
0249 ret = regulator_enable(info->micvdd);
0250 if (ret)
0251 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", ret);
0252
0253 if (info->micd_reva) {
0254 const struct reg_sequence reva[] = {
0255 { 0x80, 0x3 },
0256 { 0x294, 0x0 },
0257 { 0x80, 0x0 },
0258 };
0259
0260 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
0261 }
0262
0263 if (info->detecting && arizona->pdata.micd_software_compare)
0264 mode = ARIZONA_ACCDET_MODE_ADC;
0265 else
0266 mode = ARIZONA_ACCDET_MODE_MIC;
0267
0268 regmap_update_bits(arizona->regmap,
0269 ARIZONA_ACCESSORY_DETECT_MODE_1,
0270 ARIZONA_ACCDET_MODE_MASK, mode);
0271
0272 arizona_extcon_pulse_micbias(info);
0273
0274 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
0275 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
0276 &change);
0277 if (ret < 0) {
0278 dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
0279 } else if (!change) {
0280 regulator_disable(info->micvdd);
0281 pm_runtime_put_autosuspend(arizona->dev);
0282 }
0283 }
0284
0285 static void arizona_stop_mic(struct arizona_priv *info)
0286 {
0287 struct arizona *arizona = info->arizona;
0288 const char *widget = arizona_extcon_get_micbias(info);
0289 struct snd_soc_dapm_context *dapm = arizona->dapm;
0290 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
0291 bool change = false;
0292 int ret;
0293
0294 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
0295 ARIZONA_MICD_ENA, 0,
0296 &change);
0297 if (ret < 0)
0298 dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
0299
0300 ret = snd_soc_component_disable_pin(component, widget);
0301 if (ret)
0302 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
0303
0304 snd_soc_dapm_sync(dapm);
0305
0306 if (info->micd_reva) {
0307 const struct reg_sequence reva[] = {
0308 { 0x80, 0x3 },
0309 { 0x294, 0x2 },
0310 { 0x80, 0x0 },
0311 };
0312
0313 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
0314 }
0315
0316 ret = regulator_allow_bypass(info->micvdd, true);
0317 if (ret)
0318 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
0319
0320 if (change) {
0321 regulator_disable(info->micvdd);
0322 pm_runtime_mark_last_busy(arizona->dev);
0323 pm_runtime_put_autosuspend(arizona->dev);
0324 }
0325 }
0326
0327 static struct {
0328 unsigned int threshold;
0329 unsigned int factor_a;
0330 unsigned int factor_b;
0331 } arizona_hpdet_b_ranges[] = {
0332 { 100, 5528, 362464 },
0333 { 169, 11084, 6186851 },
0334 { 169, 11065, 65460395 },
0335 };
0336
0337 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
0338
0339 static struct {
0340 int min;
0341 int max;
0342 } arizona_hpdet_c_ranges[] = {
0343 { 0, 30 },
0344 { 8, 100 },
0345 { 100, 1000 },
0346 { 1000, 10000 },
0347 };
0348
0349 static int arizona_hpdet_read(struct arizona_priv *info)
0350 {
0351 struct arizona *arizona = info->arizona;
0352 unsigned int val, range;
0353 int ret;
0354
0355 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
0356 if (ret) {
0357 dev_err(arizona->dev, "Failed to read HPDET status: %d\n", ret);
0358 return ret;
0359 }
0360
0361 switch (info->hpdet_ip_version) {
0362 case 0:
0363 if (!(val & ARIZONA_HP_DONE)) {
0364 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
0365 return -EAGAIN;
0366 }
0367
0368 val &= ARIZONA_HP_LVL_MASK;
0369 break;
0370
0371 case 1:
0372 if (!(val & ARIZONA_HP_DONE_B)) {
0373 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
0374 return -EAGAIN;
0375 }
0376
0377 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
0378 if (ret) {
0379 dev_err(arizona->dev, "Failed to read HP value: %d\n", ret);
0380 return -EAGAIN;
0381 }
0382
0383 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
0384 &range);
0385 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
0386 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
0387
0388 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
0389 (val < arizona_hpdet_b_ranges[range].threshold ||
0390 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
0391 range++;
0392 dev_dbg(arizona->dev, "Moving to HPDET range %d\n", range);
0393 regmap_update_bits(arizona->regmap,
0394 ARIZONA_HEADPHONE_DETECT_1,
0395 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
0396 range <<
0397 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
0398 return -EAGAIN;
0399 }
0400
0401
0402 if (val < arizona_hpdet_b_ranges[range].threshold ||
0403 val >= ARIZONA_HPDET_B_RANGE_MAX) {
0404 dev_dbg(arizona->dev, "Measurement out of range\n");
0405 return ARIZONA_HPDET_MAX;
0406 }
0407
0408 dev_dbg(arizona->dev, "HPDET read %d in range %d\n", val, range);
0409
0410 val = arizona_hpdet_b_ranges[range].factor_b
0411 / ((val * 100) -
0412 arizona_hpdet_b_ranges[range].factor_a);
0413 break;
0414
0415 case 2:
0416 if (!(val & ARIZONA_HP_DONE_B)) {
0417 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
0418 return -EAGAIN;
0419 }
0420
0421 val &= ARIZONA_HP_LVL_B_MASK;
0422
0423 val /= 2;
0424
0425 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
0426 &range);
0427 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
0428 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
0429
0430
0431 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
0432 (val >= arizona_hpdet_c_ranges[range].max)) {
0433 range++;
0434 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
0435 arizona_hpdet_c_ranges[range].min,
0436 arizona_hpdet_c_ranges[range].max);
0437 regmap_update_bits(arizona->regmap,
0438 ARIZONA_HEADPHONE_DETECT_1,
0439 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
0440 range <<
0441 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
0442 return -EAGAIN;
0443 }
0444
0445 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
0446 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
0447 arizona_hpdet_c_ranges[range].min);
0448 val = arizona_hpdet_c_ranges[range].min;
0449 }
0450 break;
0451
0452 default:
0453 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", info->hpdet_ip_version);
0454 return -EINVAL;
0455 }
0456
0457 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
0458 return val;
0459 }
0460
0461 static int arizona_hpdet_do_id(struct arizona_priv *info, int *reading,
0462 bool *mic)
0463 {
0464 struct arizona *arizona = info->arizona;
0465 int id_gpio = arizona->pdata.hpdet_id_gpio;
0466
0467 if (!arizona->pdata.hpdet_acc_id)
0468 return 0;
0469
0470
0471
0472
0473
0474 info->hpdet_res[info->num_hpdet_res++] = *reading;
0475
0476
0477 if (id_gpio && info->num_hpdet_res == 1) {
0478 dev_dbg(arizona->dev, "Measuring mic\n");
0479
0480 regmap_update_bits(arizona->regmap,
0481 ARIZONA_ACCESSORY_DETECT_MODE_1,
0482 ARIZONA_ACCDET_MODE_MASK |
0483 ARIZONA_ACCDET_SRC,
0484 ARIZONA_ACCDET_MODE_HPR |
0485 info->micd_modes[0].src);
0486
0487 gpio_set_value_cansleep(id_gpio, 1);
0488
0489 regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
0490 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
0491 return -EAGAIN;
0492 }
0493
0494
0495 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
0496 info->hpdet_res[0], info->hpdet_res[1]);
0497
0498
0499 *reading = info->hpdet_res[0];
0500
0501
0502 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
0503 dev_dbg(arizona->dev, "Retrying high impedance\n");
0504 info->num_hpdet_res = 0;
0505 info->hpdet_retried = true;
0506 arizona_start_hpdet_acc_id(info);
0507 pm_runtime_put(arizona->dev);
0508 return -EAGAIN;
0509 }
0510
0511
0512
0513
0514 if (!id_gpio || info->hpdet_res[1] > 50) {
0515 dev_dbg(arizona->dev, "Detected mic\n");
0516 *mic = true;
0517 info->detecting = true;
0518 } else {
0519 dev_dbg(arizona->dev, "Detected headphone\n");
0520 }
0521
0522
0523 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
0524 ARIZONA_ACCDET_SRC, info->micd_modes[0].src);
0525
0526 return 0;
0527 }
0528
0529 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
0530 {
0531 struct arizona_priv *info = data;
0532 struct arizona *arizona = info->arizona;
0533 int id_gpio = arizona->pdata.hpdet_id_gpio;
0534 int ret, reading, state, report;
0535 bool mic = false;
0536
0537 mutex_lock(&info->lock);
0538
0539
0540 if (!info->hpdet_active) {
0541 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
0542 mutex_unlock(&info->lock);
0543 return IRQ_NONE;
0544 }
0545
0546
0547 state = info->jack->status & SND_JACK_MECHANICAL;
0548 if (!state) {
0549 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
0550 goto done;
0551 }
0552
0553 ret = arizona_hpdet_read(info);
0554 if (ret == -EAGAIN)
0555 goto out;
0556 else if (ret < 0)
0557 goto done;
0558 reading = ret;
0559
0560
0561 regmap_update_bits(arizona->regmap,
0562 ARIZONA_HEADPHONE_DETECT_1,
0563 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
0564 0);
0565
0566 ret = arizona_hpdet_do_id(info, &reading, &mic);
0567 if (ret == -EAGAIN)
0568 goto out;
0569 else if (ret < 0)
0570 goto done;
0571
0572
0573 if (reading >= 5000)
0574 report = SND_JACK_LINEOUT;
0575 else
0576 report = SND_JACK_HEADPHONE;
0577
0578 snd_soc_jack_report(info->jack, report, SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
0579
0580 done:
0581
0582 regmap_update_bits(arizona->regmap,
0583 ARIZONA_HEADPHONE_DETECT_1,
0584 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
0585 0);
0586
0587 arizona_extcon_hp_clamp(info, false);
0588
0589 if (id_gpio)
0590 gpio_set_value_cansleep(id_gpio, 0);
0591
0592
0593 if (state && (mic || info->mic))
0594 arizona_start_mic(info);
0595
0596 if (info->hpdet_active) {
0597 pm_runtime_put_autosuspend(arizona->dev);
0598 info->hpdet_active = false;
0599 }
0600
0601
0602 if (state)
0603 info->hpdet_done = true;
0604
0605 out:
0606 mutex_unlock(&info->lock);
0607
0608 return IRQ_HANDLED;
0609 }
0610
0611 static void arizona_identify_headphone(struct arizona_priv *info)
0612 {
0613 struct arizona *arizona = info->arizona;
0614 int ret;
0615
0616 if (info->hpdet_done)
0617 return;
0618
0619 dev_dbg(arizona->dev, "Starting HPDET\n");
0620
0621
0622 pm_runtime_get_sync(arizona->dev);
0623
0624 info->hpdet_active = true;
0625
0626 arizona_stop_mic(info);
0627
0628 arizona_extcon_hp_clamp(info, true);
0629
0630 ret = regmap_update_bits(arizona->regmap,
0631 ARIZONA_ACCESSORY_DETECT_MODE_1,
0632 ARIZONA_ACCDET_MODE_MASK,
0633 arizona->pdata.hpdet_channel);
0634 if (ret != 0) {
0635 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
0636 goto err;
0637 }
0638
0639 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
0640 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
0641 if (ret) {
0642 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
0643 goto err;
0644 }
0645
0646 return;
0647
0648 err:
0649 arizona_extcon_hp_clamp(info, false);
0650 pm_runtime_put_autosuspend(arizona->dev);
0651
0652
0653 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
0654 SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
0655
0656 if (info->mic)
0657 arizona_start_mic(info);
0658
0659 info->hpdet_active = false;
0660 }
0661
0662 static void arizona_start_hpdet_acc_id(struct arizona_priv *info)
0663 {
0664 struct arizona *arizona = info->arizona;
0665 int hp_reading = 32;
0666 bool mic;
0667 int ret;
0668
0669 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
0670
0671
0672 pm_runtime_get_sync(arizona->dev);
0673
0674 info->hpdet_active = true;
0675
0676 arizona_extcon_hp_clamp(info, true);
0677
0678 ret = regmap_update_bits(arizona->regmap,
0679 ARIZONA_ACCESSORY_DETECT_MODE_1,
0680 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
0681 info->micd_modes[0].src |
0682 arizona->pdata.hpdet_channel);
0683 if (ret != 0) {
0684 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
0685 goto err;
0686 }
0687
0688 if (arizona->pdata.hpdet_acc_id_line) {
0689 ret = regmap_update_bits(arizona->regmap,
0690 ARIZONA_HEADPHONE_DETECT_1,
0691 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
0692 if (ret) {
0693 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
0694 goto err;
0695 }
0696 } else {
0697 arizona_hpdet_do_id(info, &hp_reading, &mic);
0698 }
0699
0700 return;
0701
0702 err:
0703
0704 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
0705 SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
0706
0707 info->hpdet_active = false;
0708 }
0709
0710 static void arizona_micd_timeout_work(struct work_struct *work)
0711 {
0712 struct arizona_priv *info = container_of(work,
0713 struct arizona_priv,
0714 micd_timeout_work.work);
0715
0716 mutex_lock(&info->lock);
0717
0718 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
0719
0720 info->detecting = false;
0721
0722 arizona_identify_headphone(info);
0723
0724 mutex_unlock(&info->lock);
0725 }
0726
0727 static int arizona_micd_adc_read(struct arizona_priv *info)
0728 {
0729 struct arizona *arizona = info->arizona;
0730 unsigned int val;
0731 int ret;
0732
0733
0734 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
0735 ARIZONA_MICD_ENA, 0);
0736
0737 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
0738 if (ret) {
0739 dev_err(arizona->dev, "Failed to read MICDET_ADCVAL: %d\n", ret);
0740 return ret;
0741 }
0742
0743 dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
0744
0745 val &= ARIZONA_MICDET_ADCVAL_MASK;
0746 if (val < ARRAY_SIZE(arizona_micd_levels))
0747 val = arizona_micd_levels[val];
0748 else
0749 val = INT_MAX;
0750
0751 if (val <= QUICK_HEADPHONE_MAX_OHM)
0752 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
0753 else if (val <= MICROPHONE_MIN_OHM)
0754 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
0755 else if (val <= MICROPHONE_MAX_OHM)
0756 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
0757 else
0758 val = ARIZONA_MICD_LVL_8;
0759
0760 return val;
0761 }
0762
0763 static int arizona_micd_read(struct arizona_priv *info)
0764 {
0765 struct arizona *arizona = info->arizona;
0766 unsigned int val = 0;
0767 int ret, i;
0768
0769 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
0770 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
0771 if (ret) {
0772 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
0773 return ret;
0774 }
0775
0776 dev_dbg(arizona->dev, "MICDET: %x\n", val);
0777
0778 if (!(val & ARIZONA_MICD_VALID)) {
0779 dev_warn(arizona->dev, "Microphone detection state invalid\n");
0780 return -EINVAL;
0781 }
0782 }
0783
0784 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
0785 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
0786 return -EINVAL;
0787 }
0788
0789 return val;
0790 }
0791
0792 static int arizona_micdet_reading(void *priv)
0793 {
0794 struct arizona_priv *info = priv;
0795 struct arizona *arizona = info->arizona;
0796 int ret, val;
0797
0798 if (info->detecting && arizona->pdata.micd_software_compare)
0799 ret = arizona_micd_adc_read(info);
0800 else
0801 ret = arizona_micd_read(info);
0802 if (ret < 0)
0803 return ret;
0804
0805 val = ret;
0806
0807
0808 if (!(val & ARIZONA_MICD_STS)) {
0809 dev_warn(arizona->dev, "Detected open circuit\n");
0810 info->mic = false;
0811 info->detecting = false;
0812 arizona_identify_headphone(info);
0813 return 0;
0814 }
0815
0816
0817 if (val & ARIZONA_MICD_LVL_8) {
0818 info->mic = true;
0819 info->detecting = false;
0820
0821 arizona_identify_headphone(info);
0822
0823 snd_soc_jack_report(info->jack, SND_JACK_MICROPHONE, SND_JACK_MICROPHONE);
0824
0825
0826 ret = regulator_allow_bypass(info->micvdd, true);
0827 if (ret)
0828 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
0829
0830 return 0;
0831 }
0832
0833
0834
0835
0836
0837
0838
0839 if (val & MICD_LVL_1_TO_7) {
0840 if (info->jack_flips >= info->micd_num_modes * 10) {
0841 dev_dbg(arizona->dev, "Detected HP/line\n");
0842
0843 info->detecting = false;
0844
0845 arizona_identify_headphone(info);
0846 } else {
0847 info->micd_mode++;
0848 if (info->micd_mode == info->micd_num_modes)
0849 info->micd_mode = 0;
0850 arizona_extcon_set_mode(info, info->micd_mode);
0851
0852 info->jack_flips++;
0853
0854 if (arizona->pdata.micd_software_compare)
0855 regmap_update_bits(arizona->regmap,
0856 ARIZONA_MIC_DETECT_1,
0857 ARIZONA_MICD_ENA,
0858 ARIZONA_MICD_ENA);
0859
0860 queue_delayed_work(system_power_efficient_wq,
0861 &info->micd_timeout_work,
0862 msecs_to_jiffies(arizona->pdata.micd_timeout));
0863 }
0864
0865 return 0;
0866 }
0867
0868
0869
0870
0871
0872 dev_dbg(arizona->dev, "Headphone detected\n");
0873 info->detecting = false;
0874
0875 arizona_identify_headphone(info);
0876
0877 return 0;
0878 }
0879
0880 static int arizona_button_reading(void *priv)
0881 {
0882 struct arizona_priv *info = priv;
0883 struct arizona *arizona = info->arizona;
0884 int val, key, lvl;
0885
0886 val = arizona_micd_read(info);
0887 if (val < 0)
0888 return val;
0889
0890
0891
0892
0893
0894 if (val & MICD_LVL_0_TO_7) {
0895 if (info->mic) {
0896 dev_dbg(arizona->dev, "Mic button detected\n");
0897
0898 lvl = val & ARIZONA_MICD_LVL_MASK;
0899 lvl >>= ARIZONA_MICD_LVL_SHIFT;
0900
0901 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
0902 key = ffs(lvl) - 1;
0903 snd_soc_jack_report(info->jack,
0904 SND_JACK_BTN_0 >> key,
0905 info->micd_button_mask);
0906 } else {
0907 dev_err(arizona->dev, "Button out of range\n");
0908 }
0909 } else {
0910 dev_warn(arizona->dev, "Button with no mic: %x\n", val);
0911 }
0912 } else {
0913 dev_dbg(arizona->dev, "Mic button released\n");
0914 snd_soc_jack_report(info->jack, 0, info->micd_button_mask);
0915 arizona_extcon_pulse_micbias(info);
0916 }
0917
0918 return 0;
0919 }
0920
0921 static void arizona_micd_detect(struct work_struct *work)
0922 {
0923 struct arizona_priv *info = container_of(work,
0924 struct arizona_priv,
0925 micd_detect_work.work);
0926 struct arizona *arizona = info->arizona;
0927
0928 cancel_delayed_work_sync(&info->micd_timeout_work);
0929
0930 mutex_lock(&info->lock);
0931
0932
0933 if (!(info->jack->status & SND_JACK_MECHANICAL)) {
0934 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
0935 mutex_unlock(&info->lock);
0936 return;
0937 }
0938
0939 if (info->detecting)
0940 arizona_micdet_reading(info);
0941 else
0942 arizona_button_reading(info);
0943
0944 pm_runtime_mark_last_busy(arizona->dev);
0945 mutex_unlock(&info->lock);
0946 }
0947
0948 static irqreturn_t arizona_micdet(int irq, void *data)
0949 {
0950 struct arizona_priv *info = data;
0951 struct arizona *arizona = info->arizona;
0952 int debounce = arizona->pdata.micd_detect_debounce;
0953
0954 cancel_delayed_work_sync(&info->micd_detect_work);
0955 cancel_delayed_work_sync(&info->micd_timeout_work);
0956
0957 mutex_lock(&info->lock);
0958 if (!info->detecting)
0959 debounce = 0;
0960 mutex_unlock(&info->lock);
0961
0962 if (debounce)
0963 queue_delayed_work(system_power_efficient_wq,
0964 &info->micd_detect_work,
0965 msecs_to_jiffies(debounce));
0966 else
0967 arizona_micd_detect(&info->micd_detect_work.work);
0968
0969 return IRQ_HANDLED;
0970 }
0971
0972 static void arizona_hpdet_work(struct work_struct *work)
0973 {
0974 struct arizona_priv *info = container_of(work,
0975 struct arizona_priv,
0976 hpdet_work.work);
0977
0978 mutex_lock(&info->lock);
0979 arizona_start_hpdet_acc_id(info);
0980 mutex_unlock(&info->lock);
0981 }
0982
0983 static int arizona_hpdet_wait(struct arizona_priv *info)
0984 {
0985 struct arizona *arizona = info->arizona;
0986 unsigned int val;
0987 int i, ret;
0988
0989 for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
0990 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
0991 &val);
0992 if (ret) {
0993 dev_err(arizona->dev, "Failed to read HPDET state: %d\n", ret);
0994 return ret;
0995 }
0996
0997 switch (info->hpdet_ip_version) {
0998 case 0:
0999 if (val & ARIZONA_HP_DONE)
1000 return 0;
1001 break;
1002 default:
1003 if (val & ARIZONA_HP_DONE_B)
1004 return 0;
1005 break;
1006 }
1007
1008 msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
1009 }
1010
1011 dev_warn(arizona->dev, "HPDET did not appear to complete\n");
1012
1013 return -ETIMEDOUT;
1014 }
1015
1016 static irqreturn_t arizona_jackdet(int irq, void *data)
1017 {
1018 struct arizona_priv *info = data;
1019 struct arizona *arizona = info->arizona;
1020 unsigned int val, present, mask;
1021 bool cancelled_hp, cancelled_mic;
1022 int ret, i;
1023
1024 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
1025 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
1026
1027 pm_runtime_get_sync(arizona->dev);
1028
1029 mutex_lock(&info->lock);
1030
1031 if (info->micd_clamp) {
1032 mask = ARIZONA_MICD_CLAMP_STS;
1033 present = 0;
1034 } else {
1035 mask = ARIZONA_JD1_STS;
1036 if (arizona->pdata.jd_invert)
1037 present = 0;
1038 else
1039 present = ARIZONA_JD1_STS;
1040 }
1041
1042 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1043 if (ret) {
1044 dev_err(arizona->dev, "Failed to read jackdet status: %d\n", ret);
1045 mutex_unlock(&info->lock);
1046 pm_runtime_put_autosuspend(arizona->dev);
1047 return IRQ_NONE;
1048 }
1049
1050 val &= mask;
1051 if (val == info->last_jackdet) {
1052 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1053 if (cancelled_hp)
1054 queue_delayed_work(system_power_efficient_wq,
1055 &info->hpdet_work,
1056 msecs_to_jiffies(HPDET_DEBOUNCE));
1057
1058 if (cancelled_mic) {
1059 int micd_timeout = arizona->pdata.micd_timeout;
1060
1061 queue_delayed_work(system_power_efficient_wq,
1062 &info->micd_timeout_work,
1063 msecs_to_jiffies(micd_timeout));
1064 }
1065
1066 goto out;
1067 }
1068 info->last_jackdet = val;
1069
1070 if (info->last_jackdet == present) {
1071 dev_dbg(arizona->dev, "Detected jack\n");
1072 snd_soc_jack_report(info->jack, SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
1073
1074 info->detecting = true;
1075 info->mic = false;
1076 info->jack_flips = 0;
1077
1078 if (!arizona->pdata.hpdet_acc_id) {
1079 arizona_start_mic(info);
1080 } else {
1081 queue_delayed_work(system_power_efficient_wq,
1082 &info->hpdet_work,
1083 msecs_to_jiffies(HPDET_DEBOUNCE));
1084 }
1085
1086 if (info->micd_clamp || !arizona->pdata.jd_invert)
1087 regmap_update_bits(arizona->regmap,
1088 ARIZONA_JACK_DETECT_DEBOUNCE,
1089 ARIZONA_MICD_CLAMP_DB |
1090 ARIZONA_JD1_DB, 0);
1091 } else {
1092 dev_dbg(arizona->dev, "Detected jack removal\n");
1093
1094 arizona_stop_mic(info);
1095
1096 info->num_hpdet_res = 0;
1097 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1098 info->hpdet_res[i] = 0;
1099 info->mic = false;
1100 info->hpdet_done = false;
1101 info->hpdet_retried = false;
1102
1103 snd_soc_jack_report(info->jack, 0, ARIZONA_JACK_MASK | info->micd_button_mask);
1104
1105
1106
1107
1108
1109
1110
1111
1112 arizona_hpdet_wait(info);
1113
1114 regmap_update_bits(arizona->regmap,
1115 ARIZONA_JACK_DETECT_DEBOUNCE,
1116 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1117 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1118 }
1119
1120 out:
1121
1122 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1123 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1124 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1125 ARIZONA_JD1_FALL_TRIG_STS |
1126 ARIZONA_JD1_RISE_TRIG_STS);
1127
1128 mutex_unlock(&info->lock);
1129
1130 pm_runtime_mark_last_busy(arizona->dev);
1131 pm_runtime_put_autosuspend(arizona->dev);
1132
1133 return IRQ_HANDLED;
1134 }
1135
1136
1137 static void arizona_micd_set_level(struct arizona *arizona, int index,
1138 unsigned int level)
1139 {
1140 int reg;
1141 unsigned int mask;
1142
1143 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1144
1145 if (!(index % 2)) {
1146 mask = 0x3f00;
1147 level <<= 8;
1148 } else {
1149 mask = 0x3f;
1150 }
1151
1152
1153 regmap_update_bits(arizona->regmap, reg, mask, level);
1154 }
1155
1156 static int arizona_extcon_get_micd_configs(struct device *dev,
1157 struct arizona *arizona)
1158 {
1159 const char * const prop = "wlf,micd-configs";
1160 const int entries_per_config = 3;
1161 struct arizona_micd_config *micd_configs;
1162 int nconfs, ret;
1163 int i, j;
1164 u32 *vals;
1165
1166 nconfs = device_property_count_u32(arizona->dev, prop);
1167 if (nconfs <= 0)
1168 return 0;
1169
1170 vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
1171 if (!vals)
1172 return -ENOMEM;
1173
1174 ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
1175 if (ret < 0)
1176 goto out;
1177
1178 nconfs /= entries_per_config;
1179 micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
1180 GFP_KERNEL);
1181 if (!micd_configs) {
1182 ret = -ENOMEM;
1183 goto out;
1184 }
1185
1186 for (i = 0, j = 0; i < nconfs; ++i) {
1187 micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
1188 micd_configs[i].bias = vals[j++];
1189 micd_configs[i].gpio = vals[j++];
1190 }
1191
1192 arizona->pdata.micd_configs = micd_configs;
1193 arizona->pdata.num_micd_configs = nconfs;
1194
1195 out:
1196 kfree(vals);
1197 return ret;
1198 }
1199
1200 static int arizona_extcon_device_get_pdata(struct device *dev,
1201 struct arizona *arizona)
1202 {
1203 struct arizona_pdata *pdata = &arizona->pdata;
1204 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1205 int ret;
1206
1207 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1208 switch (val) {
1209 case ARIZONA_ACCDET_MODE_HPL:
1210 case ARIZONA_ACCDET_MODE_HPR:
1211 pdata->hpdet_channel = val;
1212 break;
1213 default:
1214 dev_err(arizona->dev, "Wrong wlf,hpdet-channel DT value %d\n", val);
1215 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1216 }
1217
1218 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1219 &pdata->micd_detect_debounce);
1220
1221 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1222 &pdata->micd_bias_start_time);
1223
1224 device_property_read_u32(arizona->dev, "wlf,micd-rate",
1225 &pdata->micd_rate);
1226
1227 device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1228 &pdata->micd_dbtime);
1229
1230 device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
1231 &pdata->micd_timeout);
1232
1233 pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1234 "wlf,micd-force-micbias");
1235
1236 pdata->micd_software_compare = device_property_read_bool(arizona->dev,
1237 "wlf,micd-software-compare");
1238
1239 pdata->jd_invert = device_property_read_bool(arizona->dev,
1240 "wlf,jd-invert");
1241
1242 device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
1243
1244 pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
1245 "wlf,use-jd2");
1246 pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
1247 "wlf,use-jd2-nopull");
1248
1249 ret = arizona_extcon_get_micd_configs(dev, arizona);
1250 if (ret < 0)
1251 dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
1252
1253 return 0;
1254 }
1255
1256 int arizona_jack_codec_dev_probe(struct arizona_priv *info, struct device *dev)
1257 {
1258 struct arizona *arizona = info->arizona;
1259 struct arizona_pdata *pdata = &arizona->pdata;
1260 int ret, mode;
1261
1262 if (!dev_get_platdata(arizona->dev))
1263 arizona_extcon_device_get_pdata(dev, arizona);
1264
1265 info->micvdd = devm_regulator_get(dev, "MICVDD");
1266 if (IS_ERR(info->micvdd))
1267 return dev_err_probe(arizona->dev, PTR_ERR(info->micvdd), "getting MICVDD\n");
1268
1269 mutex_init(&info->lock);
1270 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1271 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1272 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1273 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1274
1275 switch (arizona->type) {
1276 case WM5102:
1277 switch (arizona->rev) {
1278 case 0:
1279 info->micd_reva = true;
1280 break;
1281 default:
1282 info->micd_clamp = true;
1283 info->hpdet_ip_version = 1;
1284 break;
1285 }
1286 break;
1287 case WM5110:
1288 case WM8280:
1289 switch (arizona->rev) {
1290 case 0 ... 2:
1291 break;
1292 default:
1293 info->micd_clamp = true;
1294 info->hpdet_ip_version = 2;
1295 break;
1296 }
1297 break;
1298 case WM8998:
1299 case WM1814:
1300 info->micd_clamp = true;
1301 info->hpdet_ip_version = 2;
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 if (!pdata->micd_timeout)
1308 pdata->micd_timeout = DEFAULT_MICD_TIMEOUT;
1309
1310 if (pdata->num_micd_configs) {
1311 info->micd_modes = pdata->micd_configs;
1312 info->micd_num_modes = pdata->num_micd_configs;
1313 } else {
1314 info->micd_modes = micd_default_modes;
1315 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1316 }
1317
1318 if (arizona->pdata.gpsw > 0)
1319 regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
1320 ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
1321
1322 if (pdata->micd_pol_gpio > 0) {
1323 if (info->micd_modes[0].gpio)
1324 mode = GPIOF_OUT_INIT_HIGH;
1325 else
1326 mode = GPIOF_OUT_INIT_LOW;
1327
1328 ret = devm_gpio_request_one(dev, pdata->micd_pol_gpio,
1329 mode, "MICD polarity");
1330 if (ret != 0) {
1331 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1332 pdata->micd_pol_gpio, ret);
1333 return ret;
1334 }
1335
1336 info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio);
1337 } else {
1338 if (info->micd_modes[0].gpio)
1339 mode = GPIOD_OUT_HIGH;
1340 else
1341 mode = GPIOD_OUT_LOW;
1342
1343
1344
1345
1346
1347
1348 info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1349 "wlf,micd-pol",
1350 mode);
1351 if (IS_ERR(info->micd_pol_gpio)) {
1352 ret = PTR_ERR(info->micd_pol_gpio);
1353 dev_err_probe(arizona->dev, ret, "getting microphone polarity GPIO\n");
1354 return ret;
1355 }
1356 }
1357
1358 if (arizona->pdata.hpdet_id_gpio > 0) {
1359 ret = devm_gpio_request_one(dev, arizona->pdata.hpdet_id_gpio,
1360 GPIOF_OUT_INIT_LOW,
1361 "HPDET");
1362 if (ret != 0) {
1363 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1364 arizona->pdata.hpdet_id_gpio, ret);
1365 gpiod_put(info->micd_pol_gpio);
1366 return ret;
1367 }
1368 }
1369
1370 return 0;
1371 }
1372 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_probe);
1373
1374 int arizona_jack_codec_dev_remove(struct arizona_priv *info)
1375 {
1376 gpiod_put(info->micd_pol_gpio);
1377 return 0;
1378 }
1379 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_remove);
1380
1381 static int arizona_jack_enable_jack_detect(struct arizona_priv *info,
1382 struct snd_soc_jack *jack)
1383 {
1384 struct arizona *arizona = info->arizona;
1385 struct arizona_pdata *pdata = &arizona->pdata;
1386 unsigned int val;
1387 unsigned int clamp_mode;
1388 int jack_irq_fall, jack_irq_rise;
1389 int ret, i, j;
1390
1391 if (arizona->pdata.micd_bias_start_time)
1392 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1393 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1394 arizona->pdata.micd_bias_start_time
1395 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1396
1397 if (arizona->pdata.micd_rate)
1398 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1399 ARIZONA_MICD_RATE_MASK,
1400 arizona->pdata.micd_rate
1401 << ARIZONA_MICD_RATE_SHIFT);
1402
1403 switch (arizona->pdata.micd_dbtime) {
1404 case MICD_DBTIME_FOUR_READINGS:
1405 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1406 ARIZONA_MICD_DBTIME_MASK,
1407 ARIZONA_MICD_DBTIME);
1408 break;
1409 case MICD_DBTIME_TWO_READINGS:
1410 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1411 ARIZONA_MICD_DBTIME_MASK, 0);
1412 break;
1413 default:
1414 break;
1415 }
1416
1417 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
1418 ARIZONA_NUM_MICD_BUTTON_LEVELS);
1419
1420 if (arizona->pdata.num_micd_ranges) {
1421 info->micd_ranges = pdata->micd_ranges;
1422 info->num_micd_ranges = pdata->num_micd_ranges;
1423 } else {
1424 info->micd_ranges = micd_default_ranges;
1425 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1426 }
1427
1428 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_BUTTONS) {
1429 dev_err(arizona->dev, "Too many MICD ranges: %d > %d\n",
1430 arizona->pdata.num_micd_ranges, ARIZONA_MAX_MICD_BUTTONS);
1431 return -EINVAL;
1432 }
1433
1434 if (info->num_micd_ranges > 1) {
1435 for (i = 1; i < info->num_micd_ranges; i++) {
1436 if (info->micd_ranges[i - 1].max >
1437 info->micd_ranges[i].max) {
1438 dev_err(arizona->dev, "MICD ranges must be sorted\n");
1439 return -EINVAL;
1440 }
1441 }
1442 }
1443
1444
1445 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1446 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1447
1448
1449 for (i = 0; i < info->num_micd_ranges; i++) {
1450 for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
1451 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1452 break;
1453
1454 if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
1455 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1456 info->micd_ranges[i].max);
1457 return -EINVAL;
1458 }
1459
1460 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1461 arizona_micd_levels[j], i);
1462
1463 arizona_micd_set_level(arizona, i, j);
1464
1465
1466 info->micd_button_mask |= SND_JACK_BTN_0 >> i;
1467 snd_jack_set_key(jack->jack, SND_JACK_BTN_0 >> i,
1468 info->micd_ranges[i].key);
1469
1470
1471 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1472 1 << i, 1 << i);
1473 }
1474
1475
1476 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1477 arizona_micd_set_level(arizona, i, 0x3f);
1478
1479
1480
1481
1482
1483 if (info->micd_clamp) {
1484 if (arizona->pdata.jd_gpio5) {
1485
1486 val = 0xc101;
1487 if (arizona->pdata.jd_gpio5_nopull)
1488 val &= ~ARIZONA_GPN_PU;
1489
1490 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1491 val);
1492
1493 if (arizona->pdata.jd_invert)
1494 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1495 else
1496 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1497 } else {
1498 if (arizona->pdata.jd_invert)
1499 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1500 else
1501 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1502 }
1503
1504 regmap_update_bits(arizona->regmap,
1505 ARIZONA_MICD_CLAMP_CONTROL,
1506 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1507
1508 regmap_update_bits(arizona->regmap,
1509 ARIZONA_JACK_DETECT_DEBOUNCE,
1510 ARIZONA_MICD_CLAMP_DB,
1511 ARIZONA_MICD_CLAMP_DB);
1512 }
1513
1514 arizona_extcon_set_mode(info, 0);
1515
1516 info->jack = jack;
1517
1518 pm_runtime_get_sync(arizona->dev);
1519
1520 if (info->micd_clamp) {
1521 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1522 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1523 } else {
1524 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1525 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1526 }
1527
1528 ret = arizona_request_irq(arizona, jack_irq_rise,
1529 "JACKDET rise", arizona_jackdet, info);
1530 if (ret != 0) {
1531 dev_err(arizona->dev, "Failed to get JACKDET rise IRQ: %d\n", ret);
1532 goto err_pm;
1533 }
1534
1535 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1536 if (ret != 0) {
1537 dev_err(arizona->dev, "Failed to set JD rise IRQ wake: %d\n", ret);
1538 goto err_rise;
1539 }
1540
1541 ret = arizona_request_irq(arizona, jack_irq_fall,
1542 "JACKDET fall", arizona_jackdet, info);
1543 if (ret != 0) {
1544 dev_err(arizona->dev, "Failed to get JD fall IRQ: %d\n", ret);
1545 goto err_rise_wake;
1546 }
1547
1548 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1549 if (ret != 0) {
1550 dev_err(arizona->dev, "Failed to set JD fall IRQ wake: %d\n", ret);
1551 goto err_fall;
1552 }
1553
1554 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1555 "MICDET", arizona_micdet, info);
1556 if (ret != 0) {
1557 dev_err(arizona->dev, "Failed to get MICDET IRQ: %d\n", ret);
1558 goto err_fall_wake;
1559 }
1560
1561 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1562 "HPDET", arizona_hpdet_irq, info);
1563 if (ret != 0) {
1564 dev_err(arizona->dev, "Failed to get HPDET IRQ: %d\n", ret);
1565 goto err_micdet;
1566 }
1567
1568 arizona_clk32k_enable(arizona);
1569 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1570 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1571 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1572 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1573
1574 ret = regulator_allow_bypass(info->micvdd, true);
1575 if (ret != 0)
1576 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", ret);
1577
1578 pm_runtime_put(arizona->dev);
1579
1580 return 0;
1581
1582 err_micdet:
1583 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1584 err_fall_wake:
1585 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1586 err_fall:
1587 arizona_free_irq(arizona, jack_irq_fall, info);
1588 err_rise_wake:
1589 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1590 err_rise:
1591 arizona_free_irq(arizona, jack_irq_rise, info);
1592 err_pm:
1593 pm_runtime_put(arizona->dev);
1594 info->jack = NULL;
1595 return ret;
1596 }
1597
1598 static int arizona_jack_disable_jack_detect(struct arizona_priv *info)
1599 {
1600 struct arizona *arizona = info->arizona;
1601 int jack_irq_rise, jack_irq_fall;
1602 bool change;
1603 int ret;
1604
1605 if (!info->jack)
1606 return 0;
1607
1608 if (info->micd_clamp) {
1609 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1610 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1611 } else {
1612 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1613 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1614 }
1615
1616 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1617 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1618 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1619 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1620 arizona_free_irq(arizona, jack_irq_rise, info);
1621 arizona_free_irq(arizona, jack_irq_fall, info);
1622 cancel_delayed_work_sync(&info->hpdet_work);
1623 cancel_delayed_work_sync(&info->micd_detect_work);
1624 cancel_delayed_work_sync(&info->micd_timeout_work);
1625
1626 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
1627 ARIZONA_MICD_ENA, 0,
1628 &change);
1629 if (ret < 0) {
1630 dev_err(arizona->dev, "Failed to disable micd on remove: %d\n", ret);
1631 } else if (change) {
1632 regulator_disable(info->micvdd);
1633 pm_runtime_put(arizona->dev);
1634 }
1635
1636 regmap_update_bits(arizona->regmap,
1637 ARIZONA_MICD_CLAMP_CONTROL,
1638 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1639 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1640 ARIZONA_JD1_ENA, 0);
1641 arizona_clk32k_disable(arizona);
1642 info->jack = NULL;
1643
1644 return 0;
1645 }
1646
1647 int arizona_jack_set_jack(struct snd_soc_component *component,
1648 struct snd_soc_jack *jack, void *data)
1649 {
1650 struct arizona_priv *info = snd_soc_component_get_drvdata(component);
1651
1652 if (jack)
1653 return arizona_jack_enable_jack_detect(info, jack);
1654 else
1655 return arizona_jack_disable_jack_detect(info);
1656 }
1657 EXPORT_SYMBOL_GPL(arizona_jack_set_jack);