18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * extcon-arizona.c - Extcon driver Wolfson Arizona devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2012-2014 Wolfson Microelectronics plc
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
158c2ecf20Sopenharmony_ci#include <linux/gpio.h>
168c2ecf20Sopenharmony_ci#include <linux/input.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
198c2ecf20Sopenharmony_ci#include <linux/property.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
218c2ecf20Sopenharmony_ci#include <linux/extcon-provider.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <sound/soc.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/mfd/arizona/core.h>
268c2ecf20Sopenharmony_ci#include <linux/mfd/arizona/pdata.h>
278c2ecf20Sopenharmony_ci#include <linux/mfd/arizona/registers.h>
288c2ecf20Sopenharmony_ci#include <dt-bindings/mfd/arizona.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define ARIZONA_MAX_MICD_RANGE 8
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define ARIZONA_MICD_CLAMP_MODE_JDL      0x4
338c2ecf20Sopenharmony_ci#define ARIZONA_MICD_CLAMP_MODE_JDH      0x5
348c2ecf20Sopenharmony_ci#define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
358c2ecf20Sopenharmony_ci#define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define ARIZONA_TST_CAP_DEFAULT 0x3
388c2ecf20Sopenharmony_ci#define ARIZONA_TST_CAP_CLAMP   0x1
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define ARIZONA_HPDET_MAX 10000
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define HPDET_DEBOUNCE 500
438c2ecf20Sopenharmony_ci#define DEFAULT_MICD_TIMEOUT 2000
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define ARIZONA_HPDET_WAIT_COUNT 15
468c2ecf20Sopenharmony_ci#define ARIZONA_HPDET_WAIT_DELAY_MS 20
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define QUICK_HEADPHONE_MAX_OHM 3
498c2ecf20Sopenharmony_ci#define MICROPHONE_MIN_OHM      1257
508c2ecf20Sopenharmony_ci#define MICROPHONE_MAX_OHM      30000
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define MICD_DBTIME_TWO_READINGS 2
538c2ecf20Sopenharmony_ci#define MICD_DBTIME_FOUR_READINGS 4
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
568c2ecf20Sopenharmony_ci			 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
578c2ecf20Sopenharmony_ci			 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
588c2ecf20Sopenharmony_ci			 ARIZONA_MICD_LVL_7)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct arizona_extcon_info {
658c2ecf20Sopenharmony_ci	struct device *dev;
668c2ecf20Sopenharmony_ci	struct arizona *arizona;
678c2ecf20Sopenharmony_ci	struct mutex lock;
688c2ecf20Sopenharmony_ci	struct regulator *micvdd;
698c2ecf20Sopenharmony_ci	struct input_dev *input;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	u16 last_jackdet;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	int micd_mode;
748c2ecf20Sopenharmony_ci	const struct arizona_micd_config *micd_modes;
758c2ecf20Sopenharmony_ci	int micd_num_modes;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	const struct arizona_micd_range *micd_ranges;
788c2ecf20Sopenharmony_ci	int num_micd_ranges;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	bool micd_reva;
818c2ecf20Sopenharmony_ci	bool micd_clamp;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	struct delayed_work hpdet_work;
848c2ecf20Sopenharmony_ci	struct delayed_work micd_detect_work;
858c2ecf20Sopenharmony_ci	struct delayed_work micd_timeout_work;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	bool hpdet_active;
888c2ecf20Sopenharmony_ci	bool hpdet_done;
898c2ecf20Sopenharmony_ci	bool hpdet_retried;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	int num_hpdet_res;
928c2ecf20Sopenharmony_ci	unsigned int hpdet_res[3];
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	bool mic;
958c2ecf20Sopenharmony_ci	bool detecting;
968c2ecf20Sopenharmony_ci	int jack_flips;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	int hpdet_ip_version;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	struct extcon_dev *edev;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	struct gpio_desc *micd_pol_gpio;
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic const struct arizona_micd_config micd_default_modes[] = {
1068c2ecf20Sopenharmony_ci	{ ARIZONA_ACCDET_SRC, 1, 0 },
1078c2ecf20Sopenharmony_ci	{ 0,                  2, 1 },
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic const struct arizona_micd_range micd_default_ranges[] = {
1118c2ecf20Sopenharmony_ci	{ .max =  11, .key = BTN_0 },
1128c2ecf20Sopenharmony_ci	{ .max =  28, .key = BTN_1 },
1138c2ecf20Sopenharmony_ci	{ .max =  54, .key = BTN_2 },
1148c2ecf20Sopenharmony_ci	{ .max = 100, .key = BTN_3 },
1158c2ecf20Sopenharmony_ci	{ .max = 186, .key = BTN_4 },
1168c2ecf20Sopenharmony_ci	{ .max = 430, .key = BTN_5 },
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* The number of levels in arizona_micd_levels valid for button thresholds */
1208c2ecf20Sopenharmony_ci#define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const int arizona_micd_levels[] = {
1238c2ecf20Sopenharmony_ci	3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
1248c2ecf20Sopenharmony_ci	49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
1258c2ecf20Sopenharmony_ci	105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
1268c2ecf20Sopenharmony_ci	270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
1278c2ecf20Sopenharmony_ci	1257, 30000,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const unsigned int arizona_cable[] = {
1318c2ecf20Sopenharmony_ci	EXTCON_MECHANICAL,
1328c2ecf20Sopenharmony_ci	EXTCON_JACK_MICROPHONE,
1338c2ecf20Sopenharmony_ci	EXTCON_JACK_HEADPHONE,
1348c2ecf20Sopenharmony_ci	EXTCON_JACK_LINE_OUT,
1358c2ecf20Sopenharmony_ci	EXTCON_NONE,
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
1418c2ecf20Sopenharmony_ci				    bool clamp)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
1448c2ecf20Sopenharmony_ci	unsigned int mask = 0, val = 0;
1458c2ecf20Sopenharmony_ci	unsigned int cap_sel = 0;
1468c2ecf20Sopenharmony_ci	int ret;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	switch (arizona->type) {
1498c2ecf20Sopenharmony_ci	case WM8998:
1508c2ecf20Sopenharmony_ci	case WM1814:
1518c2ecf20Sopenharmony_ci		mask = 0;
1528c2ecf20Sopenharmony_ci		break;
1538c2ecf20Sopenharmony_ci	case WM5110:
1548c2ecf20Sopenharmony_ci	case WM8280:
1558c2ecf20Sopenharmony_ci		mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
1568c2ecf20Sopenharmony_ci		       ARIZONA_HP1L_SHRTI;
1578c2ecf20Sopenharmony_ci		if (clamp) {
1588c2ecf20Sopenharmony_ci			val = ARIZONA_HP1L_SHRTO;
1598c2ecf20Sopenharmony_ci			cap_sel = ARIZONA_TST_CAP_CLAMP;
1608c2ecf20Sopenharmony_ci		} else {
1618c2ecf20Sopenharmony_ci			val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
1628c2ecf20Sopenharmony_ci			cap_sel = ARIZONA_TST_CAP_DEFAULT;
1638c2ecf20Sopenharmony_ci		}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap,
1668c2ecf20Sopenharmony_ci					 ARIZONA_HP_TEST_CTRL_1,
1678c2ecf20Sopenharmony_ci					 ARIZONA_HP1_TST_CAP_SEL_MASK,
1688c2ecf20Sopenharmony_ci					 cap_sel);
1698c2ecf20Sopenharmony_ci		if (ret != 0)
1708c2ecf20Sopenharmony_ci			dev_warn(arizona->dev,
1718c2ecf20Sopenharmony_ci				 "Failed to set TST_CAP_SEL: %d\n", ret);
1728c2ecf20Sopenharmony_ci		break;
1738c2ecf20Sopenharmony_ci	default:
1748c2ecf20Sopenharmony_ci		mask = ARIZONA_RMV_SHRT_HP1L;
1758c2ecf20Sopenharmony_ci		if (clamp)
1768c2ecf20Sopenharmony_ci			val = ARIZONA_RMV_SHRT_HP1L;
1778c2ecf20Sopenharmony_ci		break;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_lock(arizona->dapm);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	arizona->hpdet_clamp = clamp;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* Keep the HP output stages disabled while doing the clamp */
1858c2ecf20Sopenharmony_ci	if (clamp) {
1868c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap,
1878c2ecf20Sopenharmony_ci					 ARIZONA_OUTPUT_ENABLES_1,
1888c2ecf20Sopenharmony_ci					 ARIZONA_OUT1L_ENA |
1898c2ecf20Sopenharmony_ci					 ARIZONA_OUT1R_ENA, 0);
1908c2ecf20Sopenharmony_ci		if (ret != 0)
1918c2ecf20Sopenharmony_ci			dev_warn(arizona->dev,
1928c2ecf20Sopenharmony_ci				"Failed to disable headphone outputs: %d\n",
1938c2ecf20Sopenharmony_ci				 ret);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (mask) {
1978c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
1988c2ecf20Sopenharmony_ci					 mask, val);
1998c2ecf20Sopenharmony_ci		if (ret != 0)
2008c2ecf20Sopenharmony_ci			dev_warn(arizona->dev, "Failed to do clamp: %d\n",
2018c2ecf20Sopenharmony_ci				 ret);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
2048c2ecf20Sopenharmony_ci					 mask, val);
2058c2ecf20Sopenharmony_ci		if (ret != 0)
2068c2ecf20Sopenharmony_ci			dev_warn(arizona->dev, "Failed to do clamp: %d\n",
2078c2ecf20Sopenharmony_ci				 ret);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Restore the desired state while not doing the clamp */
2118c2ecf20Sopenharmony_ci	if (!clamp) {
2128c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap,
2138c2ecf20Sopenharmony_ci					 ARIZONA_OUTPUT_ENABLES_1,
2148c2ecf20Sopenharmony_ci					 ARIZONA_OUT1L_ENA |
2158c2ecf20Sopenharmony_ci					 ARIZONA_OUT1R_ENA, arizona->hp_ena);
2168c2ecf20Sopenharmony_ci		if (ret != 0)
2178c2ecf20Sopenharmony_ci			dev_warn(arizona->dev,
2188c2ecf20Sopenharmony_ci				 "Failed to restore headphone outputs: %d\n",
2198c2ecf20Sopenharmony_ci				 ret);
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	snd_soc_dapm_mutex_unlock(arizona->dapm);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	mode %= info->micd_num_modes;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(info->micd_pol_gpio,
2328c2ecf20Sopenharmony_ci				 info->micd_modes[mode].gpio);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
2358c2ecf20Sopenharmony_ci			   ARIZONA_MICD_BIAS_SRC_MASK,
2368c2ecf20Sopenharmony_ci			   info->micd_modes[mode].bias <<
2378c2ecf20Sopenharmony_ci			   ARIZONA_MICD_BIAS_SRC_SHIFT);
2388c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
2398c2ecf20Sopenharmony_ci			   ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	info->micd_mode = mode;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	switch (info->micd_modes[0].bias) {
2498c2ecf20Sopenharmony_ci	case 1:
2508c2ecf20Sopenharmony_ci		return "MICBIAS1";
2518c2ecf20Sopenharmony_ci	case 2:
2528c2ecf20Sopenharmony_ci		return "MICBIAS2";
2538c2ecf20Sopenharmony_ci	case 3:
2548c2ecf20Sopenharmony_ci		return "MICBIAS3";
2558c2ecf20Sopenharmony_ci	default:
2568c2ecf20Sopenharmony_ci		return "MICVDD";
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
2638c2ecf20Sopenharmony_ci	const char *widget = arizona_extcon_get_micbias(info);
2648c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = arizona->dapm;
2658c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
2668c2ecf20Sopenharmony_ci	int ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	ret = snd_soc_component_force_enable_pin(component, widget);
2698c2ecf20Sopenharmony_ci	if (ret != 0)
2708c2ecf20Sopenharmony_ci		dev_warn(arizona->dev, "Failed to enable %s: %d\n",
2718c2ecf20Sopenharmony_ci			 widget, ret);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	snd_soc_dapm_sync(dapm);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (!arizona->pdata.micd_force_micbias) {
2768c2ecf20Sopenharmony_ci		ret = snd_soc_component_disable_pin(component, widget);
2778c2ecf20Sopenharmony_ci		if (ret != 0)
2788c2ecf20Sopenharmony_ci			dev_warn(arizona->dev, "Failed to disable %s: %d\n",
2798c2ecf20Sopenharmony_ci				 widget, ret);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci		snd_soc_dapm_sync(dapm);
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic void arizona_start_mic(struct arizona_extcon_info *info)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
2888c2ecf20Sopenharmony_ci	bool change;
2898c2ecf20Sopenharmony_ci	int ret;
2908c2ecf20Sopenharmony_ci	unsigned int mode;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* Microphone detection can't use idle mode */
2938c2ecf20Sopenharmony_ci	pm_runtime_get(info->dev);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (info->detecting) {
2968c2ecf20Sopenharmony_ci		ret = regulator_allow_bypass(info->micvdd, false);
2978c2ecf20Sopenharmony_ci		if (ret != 0) {
2988c2ecf20Sopenharmony_ci			dev_err(arizona->dev,
2998c2ecf20Sopenharmony_ci				"Failed to regulate MICVDD: %d\n",
3008c2ecf20Sopenharmony_ci				ret);
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	ret = regulator_enable(info->micvdd);
3058c2ecf20Sopenharmony_ci	if (ret != 0) {
3068c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
3078c2ecf20Sopenharmony_ci			ret);
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (info->micd_reva) {
3118c2ecf20Sopenharmony_ci		const struct reg_sequence reva[] = {
3128c2ecf20Sopenharmony_ci			{ 0x80,  0x3 },
3138c2ecf20Sopenharmony_ci			{ 0x294, 0x0 },
3148c2ecf20Sopenharmony_ci			{ 0x80,  0x0 },
3158c2ecf20Sopenharmony_ci		};
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (info->detecting && arizona->pdata.micd_software_compare)
3218c2ecf20Sopenharmony_ci		mode = ARIZONA_ACCDET_MODE_ADC;
3228c2ecf20Sopenharmony_ci	else
3238c2ecf20Sopenharmony_ci		mode = ARIZONA_ACCDET_MODE_MIC;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap,
3268c2ecf20Sopenharmony_ci			   ARIZONA_ACCESSORY_DETECT_MODE_1,
3278c2ecf20Sopenharmony_ci			   ARIZONA_ACCDET_MODE_MASK, mode);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	arizona_extcon_pulse_micbias(info);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
3328c2ecf20Sopenharmony_ci				       ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
3338c2ecf20Sopenharmony_ci				       &change);
3348c2ecf20Sopenharmony_ci	if (ret < 0) {
3358c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
3368c2ecf20Sopenharmony_ci	} else if (!change) {
3378c2ecf20Sopenharmony_ci		regulator_disable(info->micvdd);
3388c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(info->dev);
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic void arizona_stop_mic(struct arizona_extcon_info *info)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
3458c2ecf20Sopenharmony_ci	const char *widget = arizona_extcon_get_micbias(info);
3468c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = arizona->dapm;
3478c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
3488c2ecf20Sopenharmony_ci	bool change = false;
3498c2ecf20Sopenharmony_ci	int ret;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
3528c2ecf20Sopenharmony_ci				       ARIZONA_MICD_ENA, 0,
3538c2ecf20Sopenharmony_ci				       &change);
3548c2ecf20Sopenharmony_ci	if (ret < 0)
3558c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	ret = snd_soc_component_disable_pin(component, widget);
3588c2ecf20Sopenharmony_ci	if (ret != 0)
3598c2ecf20Sopenharmony_ci		dev_warn(arizona->dev,
3608c2ecf20Sopenharmony_ci			 "Failed to disable %s: %d\n",
3618c2ecf20Sopenharmony_ci			 widget, ret);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	snd_soc_dapm_sync(dapm);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	if (info->micd_reva) {
3668c2ecf20Sopenharmony_ci		const struct reg_sequence reva[] = {
3678c2ecf20Sopenharmony_ci			{ 0x80,  0x3 },
3688c2ecf20Sopenharmony_ci			{ 0x294, 0x2 },
3698c2ecf20Sopenharmony_ci			{ 0x80,  0x0 },
3708c2ecf20Sopenharmony_ci		};
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	ret = regulator_allow_bypass(info->micvdd, true);
3768c2ecf20Sopenharmony_ci	if (ret != 0) {
3778c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
3788c2ecf20Sopenharmony_ci			ret);
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (change) {
3828c2ecf20Sopenharmony_ci		regulator_disable(info->micvdd);
3838c2ecf20Sopenharmony_ci		pm_runtime_mark_last_busy(info->dev);
3848c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(info->dev);
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic struct {
3898c2ecf20Sopenharmony_ci	unsigned int threshold;
3908c2ecf20Sopenharmony_ci	unsigned int factor_a;
3918c2ecf20Sopenharmony_ci	unsigned int factor_b;
3928c2ecf20Sopenharmony_ci} arizona_hpdet_b_ranges[] = {
3938c2ecf20Sopenharmony_ci	{ 100,  5528,   362464 },
3948c2ecf20Sopenharmony_ci	{ 169, 11084,  6186851 },
3958c2ecf20Sopenharmony_ci	{ 169, 11065, 65460395 },
3968c2ecf20Sopenharmony_ci};
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci#define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic struct {
4018c2ecf20Sopenharmony_ci	int min;
4028c2ecf20Sopenharmony_ci	int max;
4038c2ecf20Sopenharmony_ci} arizona_hpdet_c_ranges[] = {
4048c2ecf20Sopenharmony_ci	{ 0,       30 },
4058c2ecf20Sopenharmony_ci	{ 8,      100 },
4068c2ecf20Sopenharmony_ci	{ 100,   1000 },
4078c2ecf20Sopenharmony_ci	{ 1000, 10000 },
4088c2ecf20Sopenharmony_ci};
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic int arizona_hpdet_read(struct arizona_extcon_info *info)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
4138c2ecf20Sopenharmony_ci	unsigned int val, range;
4148c2ecf20Sopenharmony_ci	int ret;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
4178c2ecf20Sopenharmony_ci	if (ret != 0) {
4188c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
4198c2ecf20Sopenharmony_ci			ret);
4208c2ecf20Sopenharmony_ci		return ret;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	switch (info->hpdet_ip_version) {
4248c2ecf20Sopenharmony_ci	case 0:
4258c2ecf20Sopenharmony_ci		if (!(val & ARIZONA_HP_DONE)) {
4268c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "HPDET did not complete: %x\n",
4278c2ecf20Sopenharmony_ci				val);
4288c2ecf20Sopenharmony_ci			return -EAGAIN;
4298c2ecf20Sopenharmony_ci		}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci		val &= ARIZONA_HP_LVL_MASK;
4328c2ecf20Sopenharmony_ci		break;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	case 1:
4358c2ecf20Sopenharmony_ci		if (!(val & ARIZONA_HP_DONE_B)) {
4368c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "HPDET did not complete: %x\n",
4378c2ecf20Sopenharmony_ci				val);
4388c2ecf20Sopenharmony_ci			return -EAGAIN;
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
4428c2ecf20Sopenharmony_ci		if (ret != 0) {
4438c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Failed to read HP value: %d\n",
4448c2ecf20Sopenharmony_ci				ret);
4458c2ecf20Sopenharmony_ci			return -EAGAIN;
4468c2ecf20Sopenharmony_ci		}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci		regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
4498c2ecf20Sopenharmony_ci			    &range);
4508c2ecf20Sopenharmony_ci		range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
4518c2ecf20Sopenharmony_ci			   >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
4548c2ecf20Sopenharmony_ci		    (val < arizona_hpdet_b_ranges[range].threshold ||
4558c2ecf20Sopenharmony_ci		     val >= ARIZONA_HPDET_B_RANGE_MAX)) {
4568c2ecf20Sopenharmony_ci			range++;
4578c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
4588c2ecf20Sopenharmony_ci				range);
4598c2ecf20Sopenharmony_ci			regmap_update_bits(arizona->regmap,
4608c2ecf20Sopenharmony_ci					   ARIZONA_HEADPHONE_DETECT_1,
4618c2ecf20Sopenharmony_ci					   ARIZONA_HP_IMPEDANCE_RANGE_MASK,
4628c2ecf20Sopenharmony_ci					   range <<
4638c2ecf20Sopenharmony_ci					   ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
4648c2ecf20Sopenharmony_ci			return -EAGAIN;
4658c2ecf20Sopenharmony_ci		}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci		/* If we go out of range report top of range */
4688c2ecf20Sopenharmony_ci		if (val < arizona_hpdet_b_ranges[range].threshold ||
4698c2ecf20Sopenharmony_ci		    val >= ARIZONA_HPDET_B_RANGE_MAX) {
4708c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Measurement out of range\n");
4718c2ecf20Sopenharmony_ci			return ARIZONA_HPDET_MAX;
4728c2ecf20Sopenharmony_ci		}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
4758c2ecf20Sopenharmony_ci			val, range);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci		val = arizona_hpdet_b_ranges[range].factor_b
4788c2ecf20Sopenharmony_ci			/ ((val * 100) -
4798c2ecf20Sopenharmony_ci			   arizona_hpdet_b_ranges[range].factor_a);
4808c2ecf20Sopenharmony_ci		break;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	case 2:
4838c2ecf20Sopenharmony_ci		if (!(val & ARIZONA_HP_DONE_B)) {
4848c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "HPDET did not complete: %x\n",
4858c2ecf20Sopenharmony_ci				val);
4868c2ecf20Sopenharmony_ci			return -EAGAIN;
4878c2ecf20Sopenharmony_ci		}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		val &= ARIZONA_HP_LVL_B_MASK;
4908c2ecf20Sopenharmony_ci		/* Convert to ohms, the value is in 0.5 ohm increments */
4918c2ecf20Sopenharmony_ci		val /= 2;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
4948c2ecf20Sopenharmony_ci			    &range);
4958c2ecf20Sopenharmony_ci		range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
4968c2ecf20Sopenharmony_ci			   >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		/* Skip up a range, or report? */
4998c2ecf20Sopenharmony_ci		if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
5008c2ecf20Sopenharmony_ci		    (val >= arizona_hpdet_c_ranges[range].max)) {
5018c2ecf20Sopenharmony_ci			range++;
5028c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
5038c2ecf20Sopenharmony_ci				arizona_hpdet_c_ranges[range].min,
5048c2ecf20Sopenharmony_ci				arizona_hpdet_c_ranges[range].max);
5058c2ecf20Sopenharmony_ci			regmap_update_bits(arizona->regmap,
5068c2ecf20Sopenharmony_ci					   ARIZONA_HEADPHONE_DETECT_1,
5078c2ecf20Sopenharmony_ci					   ARIZONA_HP_IMPEDANCE_RANGE_MASK,
5088c2ecf20Sopenharmony_ci					   range <<
5098c2ecf20Sopenharmony_ci					   ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
5108c2ecf20Sopenharmony_ci			return -EAGAIN;
5118c2ecf20Sopenharmony_ci		}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci		if (range && (val < arizona_hpdet_c_ranges[range].min)) {
5148c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Reporting range boundary %d\n",
5158c2ecf20Sopenharmony_ci				arizona_hpdet_c_ranges[range].min);
5168c2ecf20Sopenharmony_ci			val = arizona_hpdet_c_ranges[range].min;
5178c2ecf20Sopenharmony_ci		}
5188c2ecf20Sopenharmony_ci		break;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	default:
5218c2ecf20Sopenharmony_ci		dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
5228c2ecf20Sopenharmony_ci			 info->hpdet_ip_version);
5238c2ecf20Sopenharmony_ci		return -EINVAL;
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
5278c2ecf20Sopenharmony_ci	return val;
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_cistatic int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
5318c2ecf20Sopenharmony_ci			       bool *mic)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
5348c2ecf20Sopenharmony_ci	int id_gpio = arizona->pdata.hpdet_id_gpio;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (!arizona->pdata.hpdet_acc_id)
5378c2ecf20Sopenharmony_ci		return 0;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/*
5408c2ecf20Sopenharmony_ci	 * If we're using HPDET for accessory identification we need
5418c2ecf20Sopenharmony_ci	 * to take multiple measurements, step through them in sequence.
5428c2ecf20Sopenharmony_ci	 */
5438c2ecf20Sopenharmony_ci	info->hpdet_res[info->num_hpdet_res++] = *reading;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	/* Only check the mic directly if we didn't already ID it */
5468c2ecf20Sopenharmony_ci	if (id_gpio && info->num_hpdet_res == 1) {
5478c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Measuring mic\n");
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap,
5508c2ecf20Sopenharmony_ci				   ARIZONA_ACCESSORY_DETECT_MODE_1,
5518c2ecf20Sopenharmony_ci				   ARIZONA_ACCDET_MODE_MASK |
5528c2ecf20Sopenharmony_ci				   ARIZONA_ACCDET_SRC,
5538c2ecf20Sopenharmony_ci				   ARIZONA_ACCDET_MODE_HPR |
5548c2ecf20Sopenharmony_ci				   info->micd_modes[0].src);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		gpio_set_value_cansleep(id_gpio, 1);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
5598c2ecf20Sopenharmony_ci				   ARIZONA_HP_POLL, ARIZONA_HP_POLL);
5608c2ecf20Sopenharmony_ci		return -EAGAIN;
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	/* OK, got both.  Now, compare... */
5648c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "HPDET measured %d %d\n",
5658c2ecf20Sopenharmony_ci		info->hpdet_res[0], info->hpdet_res[1]);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* Take the headphone impedance for the main report */
5688c2ecf20Sopenharmony_ci	*reading = info->hpdet_res[0];
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	/* Sometimes we get false readings due to slow insert */
5718c2ecf20Sopenharmony_ci	if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
5728c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Retrying high impedance\n");
5738c2ecf20Sopenharmony_ci		info->num_hpdet_res = 0;
5748c2ecf20Sopenharmony_ci		info->hpdet_retried = true;
5758c2ecf20Sopenharmony_ci		arizona_start_hpdet_acc_id(info);
5768c2ecf20Sopenharmony_ci		pm_runtime_put(info->dev);
5778c2ecf20Sopenharmony_ci		return -EAGAIN;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/*
5818c2ecf20Sopenharmony_ci	 * If we measure the mic as high impedance
5828c2ecf20Sopenharmony_ci	 */
5838c2ecf20Sopenharmony_ci	if (!id_gpio || info->hpdet_res[1] > 50) {
5848c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Detected mic\n");
5858c2ecf20Sopenharmony_ci		*mic = true;
5868c2ecf20Sopenharmony_ci		info->detecting = true;
5878c2ecf20Sopenharmony_ci	} else {
5888c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Detected headphone\n");
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* Make sure everything is reset back to the real polarity */
5928c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
5938c2ecf20Sopenharmony_ci			   ARIZONA_ACCDET_SRC, info->micd_modes[0].src);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	return 0;
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic irqreturn_t arizona_hpdet_irq(int irq, void *data)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = data;
6018c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
6028c2ecf20Sopenharmony_ci	int id_gpio = arizona->pdata.hpdet_id_gpio;
6038c2ecf20Sopenharmony_ci	unsigned int report = EXTCON_JACK_HEADPHONE;
6048c2ecf20Sopenharmony_ci	int ret, reading, state;
6058c2ecf20Sopenharmony_ci	bool mic = false;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* If we got a spurious IRQ for some reason then ignore it */
6108c2ecf20Sopenharmony_ci	if (!info->hpdet_active) {
6118c2ecf20Sopenharmony_ci		dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
6128c2ecf20Sopenharmony_ci		mutex_unlock(&info->lock);
6138c2ecf20Sopenharmony_ci		return IRQ_NONE;
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* If the cable was removed while measuring ignore the result */
6178c2ecf20Sopenharmony_ci	state = extcon_get_state(info->edev, EXTCON_MECHANICAL);
6188c2ecf20Sopenharmony_ci	if (state < 0) {
6198c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to check cable state: %d\n", state);
6208c2ecf20Sopenharmony_ci		goto out;
6218c2ecf20Sopenharmony_ci	} else if (!state) {
6228c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
6238c2ecf20Sopenharmony_ci		goto done;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ret = arizona_hpdet_read(info);
6278c2ecf20Sopenharmony_ci	if (ret == -EAGAIN)
6288c2ecf20Sopenharmony_ci		goto out;
6298c2ecf20Sopenharmony_ci	else if (ret < 0)
6308c2ecf20Sopenharmony_ci		goto done;
6318c2ecf20Sopenharmony_ci	reading = ret;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	/* Reset back to starting range */
6348c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap,
6358c2ecf20Sopenharmony_ci			   ARIZONA_HEADPHONE_DETECT_1,
6368c2ecf20Sopenharmony_ci			   ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
6378c2ecf20Sopenharmony_ci			   0);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	ret = arizona_hpdet_do_id(info, &reading, &mic);
6408c2ecf20Sopenharmony_ci	if (ret == -EAGAIN)
6418c2ecf20Sopenharmony_ci		goto out;
6428c2ecf20Sopenharmony_ci	else if (ret < 0)
6438c2ecf20Sopenharmony_ci		goto done;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	/* Report high impedence cables as line outputs */
6468c2ecf20Sopenharmony_ci	if (reading >= 5000)
6478c2ecf20Sopenharmony_ci		report = EXTCON_JACK_LINE_OUT;
6488c2ecf20Sopenharmony_ci	else
6498c2ecf20Sopenharmony_ci		report = EXTCON_JACK_HEADPHONE;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	ret = extcon_set_state_sync(info->edev, report, true);
6528c2ecf20Sopenharmony_ci	if (ret != 0)
6538c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to report HP/line: %d\n",
6548c2ecf20Sopenharmony_ci			ret);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cidone:
6578c2ecf20Sopenharmony_ci	/* Reset back to starting range */
6588c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap,
6598c2ecf20Sopenharmony_ci			   ARIZONA_HEADPHONE_DETECT_1,
6608c2ecf20Sopenharmony_ci			   ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
6618c2ecf20Sopenharmony_ci			   0);
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	arizona_extcon_hp_clamp(info, false);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	if (id_gpio)
6668c2ecf20Sopenharmony_ci		gpio_set_value_cansleep(id_gpio, 0);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* If we have a mic then reenable MICDET */
6698c2ecf20Sopenharmony_ci	if (state && (mic || info->mic))
6708c2ecf20Sopenharmony_ci		arizona_start_mic(info);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	if (info->hpdet_active) {
6738c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(info->dev);
6748c2ecf20Sopenharmony_ci		info->hpdet_active = false;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/* Do not set hp_det done when the cable has been unplugged */
6788c2ecf20Sopenharmony_ci	if (state)
6798c2ecf20Sopenharmony_ci		info->hpdet_done = true;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ciout:
6828c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic void arizona_identify_headphone(struct arizona_extcon_info *info)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
6908c2ecf20Sopenharmony_ci	int ret;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	if (info->hpdet_done)
6938c2ecf20Sopenharmony_ci		return;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "Starting HPDET\n");
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	/* Make sure we keep the device enabled during the measurement */
6988c2ecf20Sopenharmony_ci	pm_runtime_get(info->dev);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	info->hpdet_active = true;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	arizona_stop_mic(info);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	arizona_extcon_hp_clamp(info, true);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	ret = regmap_update_bits(arizona->regmap,
7078c2ecf20Sopenharmony_ci				 ARIZONA_ACCESSORY_DETECT_MODE_1,
7088c2ecf20Sopenharmony_ci				 ARIZONA_ACCDET_MODE_MASK,
7098c2ecf20Sopenharmony_ci				 arizona->pdata.hpdet_channel);
7108c2ecf20Sopenharmony_ci	if (ret != 0) {
7118c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
7128c2ecf20Sopenharmony_ci		goto err;
7138c2ecf20Sopenharmony_ci	}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
7168c2ecf20Sopenharmony_ci				 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
7178c2ecf20Sopenharmony_ci	if (ret != 0) {
7188c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
7198c2ecf20Sopenharmony_ci			ret);
7208c2ecf20Sopenharmony_ci		goto err;
7218c2ecf20Sopenharmony_ci	}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	return;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_cierr:
7268c2ecf20Sopenharmony_ci	arizona_extcon_hp_clamp(info, false);
7278c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(info->dev);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	/* Just report headphone */
7308c2ecf20Sopenharmony_ci	ret = extcon_set_state_sync(info->edev, EXTCON_JACK_HEADPHONE, true);
7318c2ecf20Sopenharmony_ci	if (ret != 0)
7328c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (info->mic)
7358c2ecf20Sopenharmony_ci		arizona_start_mic(info);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	info->hpdet_active = false;
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cistatic void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
7438c2ecf20Sopenharmony_ci	int hp_reading = 32;
7448c2ecf20Sopenharmony_ci	bool mic;
7458c2ecf20Sopenharmony_ci	int ret;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "Starting identification via HPDET\n");
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	/* Make sure we keep the device enabled during the measurement */
7508c2ecf20Sopenharmony_ci	pm_runtime_get_sync(info->dev);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	info->hpdet_active = true;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	arizona_extcon_hp_clamp(info, true);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	ret = regmap_update_bits(arizona->regmap,
7578c2ecf20Sopenharmony_ci				 ARIZONA_ACCESSORY_DETECT_MODE_1,
7588c2ecf20Sopenharmony_ci				 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
7598c2ecf20Sopenharmony_ci				 info->micd_modes[0].src |
7608c2ecf20Sopenharmony_ci				 arizona->pdata.hpdet_channel);
7618c2ecf20Sopenharmony_ci	if (ret != 0) {
7628c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
7638c2ecf20Sopenharmony_ci		goto err;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (arizona->pdata.hpdet_acc_id_line) {
7678c2ecf20Sopenharmony_ci		ret = regmap_update_bits(arizona->regmap,
7688c2ecf20Sopenharmony_ci					 ARIZONA_HEADPHONE_DETECT_1,
7698c2ecf20Sopenharmony_ci					 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
7708c2ecf20Sopenharmony_ci		if (ret != 0) {
7718c2ecf20Sopenharmony_ci			dev_err(arizona->dev,
7728c2ecf20Sopenharmony_ci				"Can't start HPDETL measurement: %d\n",
7738c2ecf20Sopenharmony_ci				ret);
7748c2ecf20Sopenharmony_ci			goto err;
7758c2ecf20Sopenharmony_ci		}
7768c2ecf20Sopenharmony_ci	} else {
7778c2ecf20Sopenharmony_ci		arizona_hpdet_do_id(info, &hp_reading, &mic);
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	return;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cierr:
7838c2ecf20Sopenharmony_ci	/* Just report headphone */
7848c2ecf20Sopenharmony_ci	ret = extcon_set_state_sync(info->edev, EXTCON_JACK_HEADPHONE, true);
7858c2ecf20Sopenharmony_ci	if (ret != 0)
7868c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	info->hpdet_active = false;
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_cistatic void arizona_micd_timeout_work(struct work_struct *work)
7928c2ecf20Sopenharmony_ci{
7938c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = container_of(work,
7948c2ecf20Sopenharmony_ci						struct arizona_extcon_info,
7958c2ecf20Sopenharmony_ci						micd_timeout_work.work);
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	info->detecting = false;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	arizona_identify_headphone(info);
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic int arizona_micd_adc_read(struct arizona_extcon_info *info)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
8118c2ecf20Sopenharmony_ci	unsigned int val;
8128c2ecf20Sopenharmony_ci	int ret;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	/* Must disable MICD before we read the ADCVAL */
8158c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
8168c2ecf20Sopenharmony_ci			   ARIZONA_MICD_ENA, 0);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
8198c2ecf20Sopenharmony_ci	if (ret != 0) {
8208c2ecf20Sopenharmony_ci		dev_err(arizona->dev,
8218c2ecf20Sopenharmony_ci			"Failed to read MICDET_ADCVAL: %d\n", ret);
8228c2ecf20Sopenharmony_ci		return ret;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	val &= ARIZONA_MICDET_ADCVAL_MASK;
8288c2ecf20Sopenharmony_ci	if (val < ARRAY_SIZE(arizona_micd_levels))
8298c2ecf20Sopenharmony_ci		val = arizona_micd_levels[val];
8308c2ecf20Sopenharmony_ci	else
8318c2ecf20Sopenharmony_ci		val = INT_MAX;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (val <= QUICK_HEADPHONE_MAX_OHM)
8348c2ecf20Sopenharmony_ci		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
8358c2ecf20Sopenharmony_ci	else if (val <= MICROPHONE_MIN_OHM)
8368c2ecf20Sopenharmony_ci		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
8378c2ecf20Sopenharmony_ci	else if (val <= MICROPHONE_MAX_OHM)
8388c2ecf20Sopenharmony_ci		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
8398c2ecf20Sopenharmony_ci	else
8408c2ecf20Sopenharmony_ci		val = ARIZONA_MICD_LVL_8;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	return val;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic int arizona_micd_read(struct arizona_extcon_info *info)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
8488c2ecf20Sopenharmony_ci	unsigned int val = 0;
8498c2ecf20Sopenharmony_ci	int ret, i;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
8528c2ecf20Sopenharmony_ci		ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
8538c2ecf20Sopenharmony_ci		if (ret != 0) {
8548c2ecf20Sopenharmony_ci			dev_err(arizona->dev,
8558c2ecf20Sopenharmony_ci				"Failed to read MICDET: %d\n", ret);
8568c2ecf20Sopenharmony_ci			return ret;
8578c2ecf20Sopenharmony_ci		}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "MICDET: %x\n", val);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		if (!(val & ARIZONA_MICD_VALID)) {
8628c2ecf20Sopenharmony_ci			dev_warn(arizona->dev,
8638c2ecf20Sopenharmony_ci				 "Microphone detection state invalid\n");
8648c2ecf20Sopenharmony_ci			return -EINVAL;
8658c2ecf20Sopenharmony_ci		}
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
8698c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to get valid MICDET value\n");
8708c2ecf20Sopenharmony_ci		return -EINVAL;
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	return val;
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cistatic int arizona_micdet_reading(void *priv)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = priv;
8798c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
8808c2ecf20Sopenharmony_ci	int ret, val;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (info->detecting && arizona->pdata.micd_software_compare)
8838c2ecf20Sopenharmony_ci		ret = arizona_micd_adc_read(info);
8848c2ecf20Sopenharmony_ci	else
8858c2ecf20Sopenharmony_ci		ret = arizona_micd_read(info);
8868c2ecf20Sopenharmony_ci	if (ret < 0)
8878c2ecf20Sopenharmony_ci		return ret;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	val = ret;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	/* Due to jack detect this should never happen */
8928c2ecf20Sopenharmony_ci	if (!(val & ARIZONA_MICD_STS)) {
8938c2ecf20Sopenharmony_ci		dev_warn(arizona->dev, "Detected open circuit\n");
8948c2ecf20Sopenharmony_ci		info->mic = false;
8958c2ecf20Sopenharmony_ci		info->detecting = false;
8968c2ecf20Sopenharmony_ci		arizona_identify_headphone(info);
8978c2ecf20Sopenharmony_ci		return 0;
8988c2ecf20Sopenharmony_ci	}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	/* If we got a high impedence we should have a headset, report it. */
9018c2ecf20Sopenharmony_ci	if (val & ARIZONA_MICD_LVL_8) {
9028c2ecf20Sopenharmony_ci		info->mic = true;
9038c2ecf20Sopenharmony_ci		info->detecting = false;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci		arizona_identify_headphone(info);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci		ret = extcon_set_state_sync(info->edev,
9088c2ecf20Sopenharmony_ci					      EXTCON_JACK_MICROPHONE, true);
9098c2ecf20Sopenharmony_ci		if (ret != 0)
9108c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Headset report failed: %d\n",
9118c2ecf20Sopenharmony_ci				ret);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci		/* Don't need to regulate for button detection */
9148c2ecf20Sopenharmony_ci		ret = regulator_allow_bypass(info->micvdd, true);
9158c2ecf20Sopenharmony_ci		if (ret != 0) {
9168c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
9178c2ecf20Sopenharmony_ci				ret);
9188c2ecf20Sopenharmony_ci		}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		return 0;
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	/* If we detected a lower impedence during initial startup
9248c2ecf20Sopenharmony_ci	 * then we probably have the wrong polarity, flip it.  Don't
9258c2ecf20Sopenharmony_ci	 * do this for the lowest impedences to speed up detection of
9268c2ecf20Sopenharmony_ci	 * plain headphones.  If both polarities report a low
9278c2ecf20Sopenharmony_ci	 * impedence then give up and report headphones.
9288c2ecf20Sopenharmony_ci	 */
9298c2ecf20Sopenharmony_ci	if (val & MICD_LVL_1_TO_7) {
9308c2ecf20Sopenharmony_ci		if (info->jack_flips >= info->micd_num_modes * 10) {
9318c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Detected HP/line\n");
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci			info->detecting = false;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci			arizona_identify_headphone(info);
9368c2ecf20Sopenharmony_ci		} else {
9378c2ecf20Sopenharmony_ci			info->micd_mode++;
9388c2ecf20Sopenharmony_ci			if (info->micd_mode == info->micd_num_modes)
9398c2ecf20Sopenharmony_ci				info->micd_mode = 0;
9408c2ecf20Sopenharmony_ci			arizona_extcon_set_mode(info, info->micd_mode);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci			info->jack_flips++;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci			if (arizona->pdata.micd_software_compare)
9458c2ecf20Sopenharmony_ci				regmap_update_bits(arizona->regmap,
9468c2ecf20Sopenharmony_ci						   ARIZONA_MIC_DETECT_1,
9478c2ecf20Sopenharmony_ci						   ARIZONA_MICD_ENA,
9488c2ecf20Sopenharmony_ci						   ARIZONA_MICD_ENA);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci			queue_delayed_work(system_power_efficient_wq,
9518c2ecf20Sopenharmony_ci					   &info->micd_timeout_work,
9528c2ecf20Sopenharmony_ci					   msecs_to_jiffies(arizona->pdata.micd_timeout));
9538c2ecf20Sopenharmony_ci		}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci		return 0;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	/*
9598c2ecf20Sopenharmony_ci	 * If we're still detecting and we detect a short then we've
9608c2ecf20Sopenharmony_ci	 * got a headphone.
9618c2ecf20Sopenharmony_ci	 */
9628c2ecf20Sopenharmony_ci	dev_dbg(arizona->dev, "Headphone detected\n");
9638c2ecf20Sopenharmony_ci	info->detecting = false;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	arizona_identify_headphone(info);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	return 0;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic int arizona_button_reading(void *priv)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = priv;
9738c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
9748c2ecf20Sopenharmony_ci	int val, key, lvl, i;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	val = arizona_micd_read(info);
9778c2ecf20Sopenharmony_ci	if (val < 0)
9788c2ecf20Sopenharmony_ci		return val;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	/*
9818c2ecf20Sopenharmony_ci	 * If we're still detecting and we detect a short then we've
9828c2ecf20Sopenharmony_ci	 * got a headphone.  Otherwise it's a button press.
9838c2ecf20Sopenharmony_ci	 */
9848c2ecf20Sopenharmony_ci	if (val & MICD_LVL_0_TO_7) {
9858c2ecf20Sopenharmony_ci		if (info->mic) {
9868c2ecf20Sopenharmony_ci			dev_dbg(arizona->dev, "Mic button detected\n");
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci			lvl = val & ARIZONA_MICD_LVL_MASK;
9898c2ecf20Sopenharmony_ci			lvl >>= ARIZONA_MICD_LVL_SHIFT;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci			for (i = 0; i < info->num_micd_ranges; i++)
9928c2ecf20Sopenharmony_ci				input_report_key(info->input,
9938c2ecf20Sopenharmony_ci						 info->micd_ranges[i].key, 0);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci			if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
9968c2ecf20Sopenharmony_ci				key = info->micd_ranges[ffs(lvl) - 1].key;
9978c2ecf20Sopenharmony_ci				input_report_key(info->input, key, 1);
9988c2ecf20Sopenharmony_ci				input_sync(info->input);
9998c2ecf20Sopenharmony_ci			} else {
10008c2ecf20Sopenharmony_ci				dev_err(arizona->dev, "Button out of range\n");
10018c2ecf20Sopenharmony_ci			}
10028c2ecf20Sopenharmony_ci		} else {
10038c2ecf20Sopenharmony_ci			dev_warn(arizona->dev, "Button with no mic: %x\n",
10048c2ecf20Sopenharmony_ci				 val);
10058c2ecf20Sopenharmony_ci		}
10068c2ecf20Sopenharmony_ci	} else {
10078c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Mic button released\n");
10088c2ecf20Sopenharmony_ci		for (i = 0; i < info->num_micd_ranges; i++)
10098c2ecf20Sopenharmony_ci			input_report_key(info->input,
10108c2ecf20Sopenharmony_ci					 info->micd_ranges[i].key, 0);
10118c2ecf20Sopenharmony_ci		input_sync(info->input);
10128c2ecf20Sopenharmony_ci		arizona_extcon_pulse_micbias(info);
10138c2ecf20Sopenharmony_ci	}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	return 0;
10168c2ecf20Sopenharmony_ci}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_cistatic void arizona_micd_detect(struct work_struct *work)
10198c2ecf20Sopenharmony_ci{
10208c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = container_of(work,
10218c2ecf20Sopenharmony_ci						struct arizona_extcon_info,
10228c2ecf20Sopenharmony_ci						micd_detect_work.work);
10238c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
10248c2ecf20Sopenharmony_ci	int ret;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->micd_timeout_work);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	/* If the cable was removed while measuring ignore the result */
10318c2ecf20Sopenharmony_ci	ret = extcon_get_state(info->edev, EXTCON_MECHANICAL);
10328c2ecf20Sopenharmony_ci	if (ret < 0) {
10338c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to check cable state: %d\n",
10348c2ecf20Sopenharmony_ci				ret);
10358c2ecf20Sopenharmony_ci		mutex_unlock(&info->lock);
10368c2ecf20Sopenharmony_ci		return;
10378c2ecf20Sopenharmony_ci	} else if (!ret) {
10388c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
10398c2ecf20Sopenharmony_ci		mutex_unlock(&info->lock);
10408c2ecf20Sopenharmony_ci		return;
10418c2ecf20Sopenharmony_ci	}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	if (info->detecting)
10448c2ecf20Sopenharmony_ci		arizona_micdet_reading(info);
10458c2ecf20Sopenharmony_ci	else
10468c2ecf20Sopenharmony_ci		arizona_button_reading(info);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(info->dev);
10498c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_cistatic irqreturn_t arizona_micdet(int irq, void *data)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = data;
10558c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
10568c2ecf20Sopenharmony_ci	int debounce = arizona->pdata.micd_detect_debounce;
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->micd_detect_work);
10598c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->micd_timeout_work);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
10628c2ecf20Sopenharmony_ci	if (!info->detecting)
10638c2ecf20Sopenharmony_ci		debounce = 0;
10648c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (debounce)
10678c2ecf20Sopenharmony_ci		queue_delayed_work(system_power_efficient_wq,
10688c2ecf20Sopenharmony_ci				   &info->micd_detect_work,
10698c2ecf20Sopenharmony_ci				   msecs_to_jiffies(debounce));
10708c2ecf20Sopenharmony_ci	else
10718c2ecf20Sopenharmony_ci		arizona_micd_detect(&info->micd_detect_work.work);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
10748c2ecf20Sopenharmony_ci}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_cistatic void arizona_hpdet_work(struct work_struct *work)
10778c2ecf20Sopenharmony_ci{
10788c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = container_of(work,
10798c2ecf20Sopenharmony_ci						struct arizona_extcon_info,
10808c2ecf20Sopenharmony_ci						hpdet_work.work);
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
10838c2ecf20Sopenharmony_ci	arizona_start_hpdet_acc_id(info);
10848c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_cistatic int arizona_hpdet_wait(struct arizona_extcon_info *info)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
10908c2ecf20Sopenharmony_ci	unsigned int val;
10918c2ecf20Sopenharmony_ci	int i, ret;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
10948c2ecf20Sopenharmony_ci		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
10958c2ecf20Sopenharmony_ci				&val);
10968c2ecf20Sopenharmony_ci		if (ret) {
10978c2ecf20Sopenharmony_ci			dev_err(arizona->dev,
10988c2ecf20Sopenharmony_ci				"Failed to read HPDET state: %d\n", ret);
10998c2ecf20Sopenharmony_ci			return ret;
11008c2ecf20Sopenharmony_ci		}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci		switch (info->hpdet_ip_version) {
11038c2ecf20Sopenharmony_ci		case 0:
11048c2ecf20Sopenharmony_ci			if (val & ARIZONA_HP_DONE)
11058c2ecf20Sopenharmony_ci				return 0;
11068c2ecf20Sopenharmony_ci			break;
11078c2ecf20Sopenharmony_ci		default:
11088c2ecf20Sopenharmony_ci			if (val & ARIZONA_HP_DONE_B)
11098c2ecf20Sopenharmony_ci				return 0;
11108c2ecf20Sopenharmony_ci			break;
11118c2ecf20Sopenharmony_ci		}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci		msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
11148c2ecf20Sopenharmony_ci	}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	dev_warn(arizona->dev, "HPDET did not appear to complete\n");
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
11198c2ecf20Sopenharmony_ci}
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_cistatic irqreturn_t arizona_jackdet(int irq, void *data)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = data;
11248c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
11258c2ecf20Sopenharmony_ci	unsigned int val, present, mask;
11268c2ecf20Sopenharmony_ci	bool cancelled_hp, cancelled_mic;
11278c2ecf20Sopenharmony_ci	int ret, i;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
11308c2ecf20Sopenharmony_ci	cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	pm_runtime_get_sync(info->dev);
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	mutex_lock(&info->lock);
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	if (info->micd_clamp) {
11378c2ecf20Sopenharmony_ci		mask = ARIZONA_MICD_CLAMP_STS;
11388c2ecf20Sopenharmony_ci		present = 0;
11398c2ecf20Sopenharmony_ci	} else {
11408c2ecf20Sopenharmony_ci		mask = ARIZONA_JD1_STS;
11418c2ecf20Sopenharmony_ci		if (arizona->pdata.jd_invert)
11428c2ecf20Sopenharmony_ci			present = 0;
11438c2ecf20Sopenharmony_ci		else
11448c2ecf20Sopenharmony_ci			present = ARIZONA_JD1_STS;
11458c2ecf20Sopenharmony_ci	}
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
11488c2ecf20Sopenharmony_ci	if (ret != 0) {
11498c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
11508c2ecf20Sopenharmony_ci			ret);
11518c2ecf20Sopenharmony_ci		mutex_unlock(&info->lock);
11528c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(info->dev);
11538c2ecf20Sopenharmony_ci		return IRQ_NONE;
11548c2ecf20Sopenharmony_ci	}
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	val &= mask;
11578c2ecf20Sopenharmony_ci	if (val == info->last_jackdet) {
11588c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
11598c2ecf20Sopenharmony_ci		if (cancelled_hp)
11608c2ecf20Sopenharmony_ci			queue_delayed_work(system_power_efficient_wq,
11618c2ecf20Sopenharmony_ci					   &info->hpdet_work,
11628c2ecf20Sopenharmony_ci					   msecs_to_jiffies(HPDET_DEBOUNCE));
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci		if (cancelled_mic) {
11658c2ecf20Sopenharmony_ci			int micd_timeout = arizona->pdata.micd_timeout;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci			queue_delayed_work(system_power_efficient_wq,
11688c2ecf20Sopenharmony_ci					   &info->micd_timeout_work,
11698c2ecf20Sopenharmony_ci					   msecs_to_jiffies(micd_timeout));
11708c2ecf20Sopenharmony_ci		}
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci		goto out;
11738c2ecf20Sopenharmony_ci	}
11748c2ecf20Sopenharmony_ci	info->last_jackdet = val;
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	if (info->last_jackdet == present) {
11778c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Detected jack\n");
11788c2ecf20Sopenharmony_ci		ret = extcon_set_state_sync(info->edev,
11798c2ecf20Sopenharmony_ci					      EXTCON_MECHANICAL, true);
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci		if (ret != 0)
11828c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Mechanical report failed: %d\n",
11838c2ecf20Sopenharmony_ci				ret);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci		info->detecting = true;
11868c2ecf20Sopenharmony_ci		info->mic = false;
11878c2ecf20Sopenharmony_ci		info->jack_flips = 0;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci		if (!arizona->pdata.hpdet_acc_id) {
11908c2ecf20Sopenharmony_ci			arizona_start_mic(info);
11918c2ecf20Sopenharmony_ci		} else {
11928c2ecf20Sopenharmony_ci			queue_delayed_work(system_power_efficient_wq,
11938c2ecf20Sopenharmony_ci					   &info->hpdet_work,
11948c2ecf20Sopenharmony_ci					   msecs_to_jiffies(HPDET_DEBOUNCE));
11958c2ecf20Sopenharmony_ci		}
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci		if (info->micd_clamp || !arizona->pdata.jd_invert)
11988c2ecf20Sopenharmony_ci			regmap_update_bits(arizona->regmap,
11998c2ecf20Sopenharmony_ci					   ARIZONA_JACK_DETECT_DEBOUNCE,
12008c2ecf20Sopenharmony_ci					   ARIZONA_MICD_CLAMP_DB |
12018c2ecf20Sopenharmony_ci					   ARIZONA_JD1_DB, 0);
12028c2ecf20Sopenharmony_ci	} else {
12038c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "Detected jack removal\n");
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci		arizona_stop_mic(info);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci		info->num_hpdet_res = 0;
12088c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
12098c2ecf20Sopenharmony_ci			info->hpdet_res[i] = 0;
12108c2ecf20Sopenharmony_ci		info->mic = false;
12118c2ecf20Sopenharmony_ci		info->hpdet_done = false;
12128c2ecf20Sopenharmony_ci		info->hpdet_retried = false;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci		for (i = 0; i < info->num_micd_ranges; i++)
12158c2ecf20Sopenharmony_ci			input_report_key(info->input,
12168c2ecf20Sopenharmony_ci					 info->micd_ranges[i].key, 0);
12178c2ecf20Sopenharmony_ci		input_sync(info->input);
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(arizona_cable) - 1; i++) {
12208c2ecf20Sopenharmony_ci			ret = extcon_set_state_sync(info->edev,
12218c2ecf20Sopenharmony_ci					arizona_cable[i], false);
12228c2ecf20Sopenharmony_ci			if (ret != 0)
12238c2ecf20Sopenharmony_ci				dev_err(arizona->dev,
12248c2ecf20Sopenharmony_ci					"Removal report failed: %d\n", ret);
12258c2ecf20Sopenharmony_ci		}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci		/*
12288c2ecf20Sopenharmony_ci		 * If the jack was removed during a headphone detection we
12298c2ecf20Sopenharmony_ci		 * need to wait for the headphone detection to finish, as
12308c2ecf20Sopenharmony_ci		 * it can not be aborted. We don't want to be able to start
12318c2ecf20Sopenharmony_ci		 * a new headphone detection from a fresh insert until this
12328c2ecf20Sopenharmony_ci		 * one is finished.
12338c2ecf20Sopenharmony_ci		 */
12348c2ecf20Sopenharmony_ci		arizona_hpdet_wait(info);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap,
12378c2ecf20Sopenharmony_ci				   ARIZONA_JACK_DETECT_DEBOUNCE,
12388c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
12398c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
12408c2ecf20Sopenharmony_ci	}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ciout:
12438c2ecf20Sopenharmony_ci	/* Clear trig_sts to make sure DCVDD is not forced up */
12448c2ecf20Sopenharmony_ci	regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
12458c2ecf20Sopenharmony_ci		     ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
12468c2ecf20Sopenharmony_ci		     ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
12478c2ecf20Sopenharmony_ci		     ARIZONA_JD1_FALL_TRIG_STS |
12488c2ecf20Sopenharmony_ci		     ARIZONA_JD1_RISE_TRIG_STS);
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	mutex_unlock(&info->lock);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(info->dev);
12538c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(info->dev);
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
12568c2ecf20Sopenharmony_ci}
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci/* Map a level onto a slot in the register bank */
12598c2ecf20Sopenharmony_cistatic void arizona_micd_set_level(struct arizona *arizona, int index,
12608c2ecf20Sopenharmony_ci				   unsigned int level)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	int reg;
12638c2ecf20Sopenharmony_ci	unsigned int mask;
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	if (!(index % 2)) {
12688c2ecf20Sopenharmony_ci		mask = 0x3f00;
12698c2ecf20Sopenharmony_ci		level <<= 8;
12708c2ecf20Sopenharmony_ci	} else {
12718c2ecf20Sopenharmony_ci		mask = 0x3f;
12728c2ecf20Sopenharmony_ci	}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	/* Program the level itself */
12758c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, reg, mask, level);
12768c2ecf20Sopenharmony_ci}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cistatic int arizona_extcon_get_micd_configs(struct device *dev,
12798c2ecf20Sopenharmony_ci					   struct arizona *arizona)
12808c2ecf20Sopenharmony_ci{
12818c2ecf20Sopenharmony_ci	const char * const prop = "wlf,micd-configs";
12828c2ecf20Sopenharmony_ci	const int entries_per_config = 3;
12838c2ecf20Sopenharmony_ci	struct arizona_micd_config *micd_configs;
12848c2ecf20Sopenharmony_ci	int nconfs, ret;
12858c2ecf20Sopenharmony_ci	int i, j;
12868c2ecf20Sopenharmony_ci	u32 *vals;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	nconfs = device_property_count_u32(arizona->dev, prop);
12898c2ecf20Sopenharmony_ci	if (nconfs <= 0)
12908c2ecf20Sopenharmony_ci		return 0;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
12938c2ecf20Sopenharmony_ci	if (!vals)
12948c2ecf20Sopenharmony_ci		return -ENOMEM;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
12978c2ecf20Sopenharmony_ci	if (ret < 0)
12988c2ecf20Sopenharmony_ci		goto out;
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	nconfs /= entries_per_config;
13018c2ecf20Sopenharmony_ci	micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
13028c2ecf20Sopenharmony_ci				    GFP_KERNEL);
13038c2ecf20Sopenharmony_ci	if (!micd_configs) {
13048c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13058c2ecf20Sopenharmony_ci		goto out;
13068c2ecf20Sopenharmony_ci	}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	for (i = 0, j = 0; i < nconfs; ++i) {
13098c2ecf20Sopenharmony_ci		micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
13108c2ecf20Sopenharmony_ci		micd_configs[i].bias = vals[j++];
13118c2ecf20Sopenharmony_ci		micd_configs[i].gpio = vals[j++];
13128c2ecf20Sopenharmony_ci	}
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	arizona->pdata.micd_configs = micd_configs;
13158c2ecf20Sopenharmony_ci	arizona->pdata.num_micd_configs = nconfs;
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ciout:
13188c2ecf20Sopenharmony_ci	kfree(vals);
13198c2ecf20Sopenharmony_ci	return ret;
13208c2ecf20Sopenharmony_ci}
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_cistatic int arizona_extcon_device_get_pdata(struct device *dev,
13238c2ecf20Sopenharmony_ci					   struct arizona *arizona)
13248c2ecf20Sopenharmony_ci{
13258c2ecf20Sopenharmony_ci	struct arizona_pdata *pdata = &arizona->pdata;
13268c2ecf20Sopenharmony_ci	unsigned int val = ARIZONA_ACCDET_MODE_HPL;
13278c2ecf20Sopenharmony_ci	int ret;
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
13308c2ecf20Sopenharmony_ci	switch (val) {
13318c2ecf20Sopenharmony_ci	case ARIZONA_ACCDET_MODE_HPL:
13328c2ecf20Sopenharmony_ci	case ARIZONA_ACCDET_MODE_HPR:
13338c2ecf20Sopenharmony_ci		pdata->hpdet_channel = val;
13348c2ecf20Sopenharmony_ci		break;
13358c2ecf20Sopenharmony_ci	default:
13368c2ecf20Sopenharmony_ci		dev_err(arizona->dev,
13378c2ecf20Sopenharmony_ci			"Wrong wlf,hpdet-channel DT value %d\n", val);
13388c2ecf20Sopenharmony_ci		pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
13398c2ecf20Sopenharmony_ci	}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
13428c2ecf20Sopenharmony_ci				 &pdata->micd_detect_debounce);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
13458c2ecf20Sopenharmony_ci				 &pdata->micd_bias_start_time);
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,micd-rate",
13488c2ecf20Sopenharmony_ci				 &pdata->micd_rate);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
13518c2ecf20Sopenharmony_ci				 &pdata->micd_dbtime);
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
13548c2ecf20Sopenharmony_ci				 &pdata->micd_timeout);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
13578c2ecf20Sopenharmony_ci						"wlf,micd-force-micbias");
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	pdata->micd_software_compare = device_property_read_bool(arizona->dev,
13608c2ecf20Sopenharmony_ci						"wlf,micd-software-compare");
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	pdata->jd_invert = device_property_read_bool(arizona->dev,
13638c2ecf20Sopenharmony_ci						     "wlf,jd-invert");
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
13688c2ecf20Sopenharmony_ci						    "wlf,use-jd2");
13698c2ecf20Sopenharmony_ci	pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
13708c2ecf20Sopenharmony_ci						"wlf,use-jd2-nopull");
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	ret = arizona_extcon_get_micd_configs(dev, arizona);
13738c2ecf20Sopenharmony_ci	if (ret < 0)
13748c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	return 0;
13778c2ecf20Sopenharmony_ci}
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_cistatic int arizona_extcon_probe(struct platform_device *pdev)
13808c2ecf20Sopenharmony_ci{
13818c2ecf20Sopenharmony_ci	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
13828c2ecf20Sopenharmony_ci	struct arizona_pdata *pdata = &arizona->pdata;
13838c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info;
13848c2ecf20Sopenharmony_ci	unsigned int val;
13858c2ecf20Sopenharmony_ci	unsigned int clamp_mode;
13868c2ecf20Sopenharmony_ci	int jack_irq_fall, jack_irq_rise;
13878c2ecf20Sopenharmony_ci	int ret, mode, i, j;
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	if (!arizona->dapm || !arizona->dapm->card)
13908c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
13938c2ecf20Sopenharmony_ci	if (!info)
13948c2ecf20Sopenharmony_ci		return -ENOMEM;
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	if (!dev_get_platdata(arizona->dev))
13978c2ecf20Sopenharmony_ci		arizona_extcon_device_get_pdata(&pdev->dev, arizona);
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci	info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
14008c2ecf20Sopenharmony_ci	if (IS_ERR(info->micvdd)) {
14018c2ecf20Sopenharmony_ci		ret = PTR_ERR(info->micvdd);
14028c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
14038c2ecf20Sopenharmony_ci		return ret;
14048c2ecf20Sopenharmony_ci	}
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	mutex_init(&info->lock);
14078c2ecf20Sopenharmony_ci	info->arizona = arizona;
14088c2ecf20Sopenharmony_ci	info->dev = &pdev->dev;
14098c2ecf20Sopenharmony_ci	info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
14108c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
14118c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
14128c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
14138c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, info);
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	switch (arizona->type) {
14168c2ecf20Sopenharmony_ci	case WM5102:
14178c2ecf20Sopenharmony_ci		switch (arizona->rev) {
14188c2ecf20Sopenharmony_ci		case 0:
14198c2ecf20Sopenharmony_ci			info->micd_reva = true;
14208c2ecf20Sopenharmony_ci			break;
14218c2ecf20Sopenharmony_ci		default:
14228c2ecf20Sopenharmony_ci			info->micd_clamp = true;
14238c2ecf20Sopenharmony_ci			info->hpdet_ip_version = 1;
14248c2ecf20Sopenharmony_ci			break;
14258c2ecf20Sopenharmony_ci		}
14268c2ecf20Sopenharmony_ci		break;
14278c2ecf20Sopenharmony_ci	case WM5110:
14288c2ecf20Sopenharmony_ci	case WM8280:
14298c2ecf20Sopenharmony_ci		switch (arizona->rev) {
14308c2ecf20Sopenharmony_ci		case 0 ... 2:
14318c2ecf20Sopenharmony_ci			break;
14328c2ecf20Sopenharmony_ci		default:
14338c2ecf20Sopenharmony_ci			info->micd_clamp = true;
14348c2ecf20Sopenharmony_ci			info->hpdet_ip_version = 2;
14358c2ecf20Sopenharmony_ci			break;
14368c2ecf20Sopenharmony_ci		}
14378c2ecf20Sopenharmony_ci		break;
14388c2ecf20Sopenharmony_ci	case WM8998:
14398c2ecf20Sopenharmony_ci	case WM1814:
14408c2ecf20Sopenharmony_ci		info->micd_clamp = true;
14418c2ecf20Sopenharmony_ci		info->hpdet_ip_version = 2;
14428c2ecf20Sopenharmony_ci		break;
14438c2ecf20Sopenharmony_ci	default:
14448c2ecf20Sopenharmony_ci		break;
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
14488c2ecf20Sopenharmony_ci	if (IS_ERR(info->edev)) {
14498c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to allocate extcon device\n");
14508c2ecf20Sopenharmony_ci		return -ENOMEM;
14518c2ecf20Sopenharmony_ci	}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
14548c2ecf20Sopenharmony_ci	if (ret < 0) {
14558c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
14568c2ecf20Sopenharmony_ci			ret);
14578c2ecf20Sopenharmony_ci		return ret;
14588c2ecf20Sopenharmony_ci	}
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	info->input = devm_input_allocate_device(&pdev->dev);
14618c2ecf20Sopenharmony_ci	if (!info->input) {
14628c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Can't allocate input dev\n");
14638c2ecf20Sopenharmony_ci		ret = -ENOMEM;
14648c2ecf20Sopenharmony_ci		return ret;
14658c2ecf20Sopenharmony_ci	}
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	info->input->name = "Headset";
14688c2ecf20Sopenharmony_ci	info->input->phys = "arizona/extcon";
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	if (!pdata->micd_timeout)
14718c2ecf20Sopenharmony_ci		pdata->micd_timeout = DEFAULT_MICD_TIMEOUT;
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	if (pdata->num_micd_configs) {
14748c2ecf20Sopenharmony_ci		info->micd_modes = pdata->micd_configs;
14758c2ecf20Sopenharmony_ci		info->micd_num_modes = pdata->num_micd_configs;
14768c2ecf20Sopenharmony_ci	} else {
14778c2ecf20Sopenharmony_ci		info->micd_modes = micd_default_modes;
14788c2ecf20Sopenharmony_ci		info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
14798c2ecf20Sopenharmony_ci	}
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	if (arizona->pdata.gpsw > 0)
14828c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
14838c2ecf20Sopenharmony_ci				ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	if (pdata->micd_pol_gpio > 0) {
14868c2ecf20Sopenharmony_ci		if (info->micd_modes[0].gpio)
14878c2ecf20Sopenharmony_ci			mode = GPIOF_OUT_INIT_HIGH;
14888c2ecf20Sopenharmony_ci		else
14898c2ecf20Sopenharmony_ci			mode = GPIOF_OUT_INIT_LOW;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci		ret = devm_gpio_request_one(&pdev->dev, pdata->micd_pol_gpio,
14928c2ecf20Sopenharmony_ci					    mode, "MICD polarity");
14938c2ecf20Sopenharmony_ci		if (ret != 0) {
14948c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
14958c2ecf20Sopenharmony_ci				pdata->micd_pol_gpio, ret);
14968c2ecf20Sopenharmony_ci			return ret;
14978c2ecf20Sopenharmony_ci		}
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci		info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio);
15008c2ecf20Sopenharmony_ci	} else {
15018c2ecf20Sopenharmony_ci		if (info->micd_modes[0].gpio)
15028c2ecf20Sopenharmony_ci			mode = GPIOD_OUT_HIGH;
15038c2ecf20Sopenharmony_ci		else
15048c2ecf20Sopenharmony_ci			mode = GPIOD_OUT_LOW;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci		/* We can't use devm here because we need to do the get
15078c2ecf20Sopenharmony_ci		 * against the MFD device, as that is where the of_node
15088c2ecf20Sopenharmony_ci		 * will reside, but if we devm against that the GPIO
15098c2ecf20Sopenharmony_ci		 * will not be freed if the extcon driver is unloaded.
15108c2ecf20Sopenharmony_ci		 */
15118c2ecf20Sopenharmony_ci		info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
15128c2ecf20Sopenharmony_ci							 "wlf,micd-pol",
15138c2ecf20Sopenharmony_ci							 GPIOD_OUT_LOW);
15148c2ecf20Sopenharmony_ci		if (IS_ERR(info->micd_pol_gpio)) {
15158c2ecf20Sopenharmony_ci			ret = PTR_ERR(info->micd_pol_gpio);
15168c2ecf20Sopenharmony_ci			dev_err(arizona->dev,
15178c2ecf20Sopenharmony_ci				"Failed to get microphone polarity GPIO: %d\n",
15188c2ecf20Sopenharmony_ci				ret);
15198c2ecf20Sopenharmony_ci			return ret;
15208c2ecf20Sopenharmony_ci		}
15218c2ecf20Sopenharmony_ci	}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	if (arizona->pdata.hpdet_id_gpio > 0) {
15248c2ecf20Sopenharmony_ci		ret = devm_gpio_request_one(&pdev->dev,
15258c2ecf20Sopenharmony_ci					    arizona->pdata.hpdet_id_gpio,
15268c2ecf20Sopenharmony_ci					    GPIOF_OUT_INIT_LOW,
15278c2ecf20Sopenharmony_ci					    "HPDET");
15288c2ecf20Sopenharmony_ci		if (ret != 0) {
15298c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
15308c2ecf20Sopenharmony_ci				arizona->pdata.hpdet_id_gpio, ret);
15318c2ecf20Sopenharmony_ci			goto err_gpio;
15328c2ecf20Sopenharmony_ci		}
15338c2ecf20Sopenharmony_ci	}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci	if (arizona->pdata.micd_bias_start_time)
15368c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
15378c2ecf20Sopenharmony_ci				   ARIZONA_MICD_BIAS_STARTTIME_MASK,
15388c2ecf20Sopenharmony_ci				   arizona->pdata.micd_bias_start_time
15398c2ecf20Sopenharmony_ci				   << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	if (arizona->pdata.micd_rate)
15428c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
15438c2ecf20Sopenharmony_ci				   ARIZONA_MICD_RATE_MASK,
15448c2ecf20Sopenharmony_ci				   arizona->pdata.micd_rate
15458c2ecf20Sopenharmony_ci				   << ARIZONA_MICD_RATE_SHIFT);
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	switch (arizona->pdata.micd_dbtime) {
15488c2ecf20Sopenharmony_ci	case MICD_DBTIME_FOUR_READINGS:
15498c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
15508c2ecf20Sopenharmony_ci				   ARIZONA_MICD_DBTIME_MASK,
15518c2ecf20Sopenharmony_ci				   ARIZONA_MICD_DBTIME);
15528c2ecf20Sopenharmony_ci		break;
15538c2ecf20Sopenharmony_ci	case MICD_DBTIME_TWO_READINGS:
15548c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
15558c2ecf20Sopenharmony_ci				   ARIZONA_MICD_DBTIME_MASK, 0);
15568c2ecf20Sopenharmony_ci		break;
15578c2ecf20Sopenharmony_ci	default:
15588c2ecf20Sopenharmony_ci		break;
15598c2ecf20Sopenharmony_ci	}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
15628c2ecf20Sopenharmony_ci		     ARIZONA_NUM_MICD_BUTTON_LEVELS);
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	if (arizona->pdata.num_micd_ranges) {
15658c2ecf20Sopenharmony_ci		info->micd_ranges = pdata->micd_ranges;
15668c2ecf20Sopenharmony_ci		info->num_micd_ranges = pdata->num_micd_ranges;
15678c2ecf20Sopenharmony_ci	} else {
15688c2ecf20Sopenharmony_ci		info->micd_ranges = micd_default_ranges;
15698c2ecf20Sopenharmony_ci		info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
15708c2ecf20Sopenharmony_ci	}
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
15738c2ecf20Sopenharmony_ci		dev_err(arizona->dev, "Too many MICD ranges: %d\n",
15748c2ecf20Sopenharmony_ci			arizona->pdata.num_micd_ranges);
15758c2ecf20Sopenharmony_ci	}
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	if (info->num_micd_ranges > 1) {
15788c2ecf20Sopenharmony_ci		for (i = 1; i < info->num_micd_ranges; i++) {
15798c2ecf20Sopenharmony_ci			if (info->micd_ranges[i - 1].max >
15808c2ecf20Sopenharmony_ci			    info->micd_ranges[i].max) {
15818c2ecf20Sopenharmony_ci				dev_err(arizona->dev,
15828c2ecf20Sopenharmony_ci					"MICD ranges must be sorted\n");
15838c2ecf20Sopenharmony_ci				ret = -EINVAL;
15848c2ecf20Sopenharmony_ci				goto err_gpio;
15858c2ecf20Sopenharmony_ci			}
15868c2ecf20Sopenharmony_ci		}
15878c2ecf20Sopenharmony_ci	}
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	/* Disable all buttons by default */
15908c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
15918c2ecf20Sopenharmony_ci			   ARIZONA_MICD_LVL_SEL_MASK, 0x81);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	/* Set up all the buttons the user specified */
15948c2ecf20Sopenharmony_ci	for (i = 0; i < info->num_micd_ranges; i++) {
15958c2ecf20Sopenharmony_ci		for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
15968c2ecf20Sopenharmony_ci			if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
15978c2ecf20Sopenharmony_ci				break;
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci		if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
16008c2ecf20Sopenharmony_ci			dev_err(arizona->dev, "Unsupported MICD level %d\n",
16018c2ecf20Sopenharmony_ci				info->micd_ranges[i].max);
16028c2ecf20Sopenharmony_ci			ret = -EINVAL;
16038c2ecf20Sopenharmony_ci			goto err_gpio;
16048c2ecf20Sopenharmony_ci		}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci		dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
16078c2ecf20Sopenharmony_ci			arizona_micd_levels[j], i);
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci		arizona_micd_set_level(arizona, i, j);
16108c2ecf20Sopenharmony_ci		input_set_capability(info->input, EV_KEY,
16118c2ecf20Sopenharmony_ci				     info->micd_ranges[i].key);
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci		/* Enable reporting of that range */
16148c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
16158c2ecf20Sopenharmony_ci				   1 << i, 1 << i);
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	/* Set all the remaining keys to a maximum */
16198c2ecf20Sopenharmony_ci	for (; i < ARIZONA_MAX_MICD_RANGE; i++)
16208c2ecf20Sopenharmony_ci		arizona_micd_set_level(arizona, i, 0x3f);
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	/*
16238c2ecf20Sopenharmony_ci	 * If we have a clamp use it, activating in conjunction with
16248c2ecf20Sopenharmony_ci	 * GPIO5 if that is connected for jack detect operation.
16258c2ecf20Sopenharmony_ci	 */
16268c2ecf20Sopenharmony_ci	if (info->micd_clamp) {
16278c2ecf20Sopenharmony_ci		if (arizona->pdata.jd_gpio5) {
16288c2ecf20Sopenharmony_ci			/* Put the GPIO into input mode with optional pull */
16298c2ecf20Sopenharmony_ci			val = 0xc101;
16308c2ecf20Sopenharmony_ci			if (arizona->pdata.jd_gpio5_nopull)
16318c2ecf20Sopenharmony_ci				val &= ~ARIZONA_GPN_PU;
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci			regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
16348c2ecf20Sopenharmony_ci				     val);
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci			if (arizona->pdata.jd_invert)
16378c2ecf20Sopenharmony_ci				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
16388c2ecf20Sopenharmony_ci			else
16398c2ecf20Sopenharmony_ci				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
16408c2ecf20Sopenharmony_ci		} else {
16418c2ecf20Sopenharmony_ci			if (arizona->pdata.jd_invert)
16428c2ecf20Sopenharmony_ci				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
16438c2ecf20Sopenharmony_ci			else
16448c2ecf20Sopenharmony_ci				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
16458c2ecf20Sopenharmony_ci		}
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap,
16488c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_CONTROL,
16498c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci		regmap_update_bits(arizona->regmap,
16528c2ecf20Sopenharmony_ci				   ARIZONA_JACK_DETECT_DEBOUNCE,
16538c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_DB,
16548c2ecf20Sopenharmony_ci				   ARIZONA_MICD_CLAMP_DB);
16558c2ecf20Sopenharmony_ci	}
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci	arizona_extcon_set_mode(info, 0);
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
16608c2ecf20Sopenharmony_ci	pm_runtime_idle(&pdev->dev);
16618c2ecf20Sopenharmony_ci	pm_runtime_get_sync(&pdev->dev);
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	if (info->micd_clamp) {
16648c2ecf20Sopenharmony_ci		jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
16658c2ecf20Sopenharmony_ci		jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
16668c2ecf20Sopenharmony_ci	} else {
16678c2ecf20Sopenharmony_ci		jack_irq_rise = ARIZONA_IRQ_JD_RISE;
16688c2ecf20Sopenharmony_ci		jack_irq_fall = ARIZONA_IRQ_JD_FALL;
16698c2ecf20Sopenharmony_ci	}
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	ret = arizona_request_irq(arizona, jack_irq_rise,
16728c2ecf20Sopenharmony_ci				  "JACKDET rise", arizona_jackdet, info);
16738c2ecf20Sopenharmony_ci	if (ret != 0) {
16748c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
16758c2ecf20Sopenharmony_ci			ret);
16768c2ecf20Sopenharmony_ci		goto err_pm;
16778c2ecf20Sopenharmony_ci	}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
16808c2ecf20Sopenharmony_ci	if (ret != 0) {
16818c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
16828c2ecf20Sopenharmony_ci			ret);
16838c2ecf20Sopenharmony_ci		goto err_rise;
16848c2ecf20Sopenharmony_ci	}
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	ret = arizona_request_irq(arizona, jack_irq_fall,
16878c2ecf20Sopenharmony_ci				  "JACKDET fall", arizona_jackdet, info);
16888c2ecf20Sopenharmony_ci	if (ret != 0) {
16898c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
16908c2ecf20Sopenharmony_ci		goto err_rise_wake;
16918c2ecf20Sopenharmony_ci	}
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
16948c2ecf20Sopenharmony_ci	if (ret != 0) {
16958c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
16968c2ecf20Sopenharmony_ci			ret);
16978c2ecf20Sopenharmony_ci		goto err_fall;
16988c2ecf20Sopenharmony_ci	}
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_ci	ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
17018c2ecf20Sopenharmony_ci				  "MICDET", arizona_micdet, info);
17028c2ecf20Sopenharmony_ci	if (ret != 0) {
17038c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
17048c2ecf20Sopenharmony_ci		goto err_fall_wake;
17058c2ecf20Sopenharmony_ci	}
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci	ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
17088c2ecf20Sopenharmony_ci				  "HPDET", arizona_hpdet_irq, info);
17098c2ecf20Sopenharmony_ci	if (ret != 0) {
17108c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
17118c2ecf20Sopenharmony_ci		goto err_micdet;
17128c2ecf20Sopenharmony_ci	}
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	arizona_clk32k_enable(arizona);
17158c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
17168c2ecf20Sopenharmony_ci			   ARIZONA_JD1_DB, ARIZONA_JD1_DB);
17178c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
17188c2ecf20Sopenharmony_ci			   ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_ci	ret = regulator_allow_bypass(info->micvdd, true);
17218c2ecf20Sopenharmony_ci	if (ret != 0)
17228c2ecf20Sopenharmony_ci		dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
17238c2ecf20Sopenharmony_ci			 ret);
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci	ret = input_register_device(info->input);
17268c2ecf20Sopenharmony_ci	if (ret) {
17278c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
17288c2ecf20Sopenharmony_ci		goto err_hpdet;
17298c2ecf20Sopenharmony_ci	}
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	pm_runtime_put(&pdev->dev);
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	return 0;
17348c2ecf20Sopenharmony_ci
17358c2ecf20Sopenharmony_cierr_hpdet:
17368c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
17378c2ecf20Sopenharmony_cierr_micdet:
17388c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
17398c2ecf20Sopenharmony_cierr_fall_wake:
17408c2ecf20Sopenharmony_ci	arizona_set_irq_wake(arizona, jack_irq_fall, 0);
17418c2ecf20Sopenharmony_cierr_fall:
17428c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, jack_irq_fall, info);
17438c2ecf20Sopenharmony_cierr_rise_wake:
17448c2ecf20Sopenharmony_ci	arizona_set_irq_wake(arizona, jack_irq_rise, 0);
17458c2ecf20Sopenharmony_cierr_rise:
17468c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, jack_irq_rise, info);
17478c2ecf20Sopenharmony_cierr_pm:
17488c2ecf20Sopenharmony_ci	pm_runtime_put(&pdev->dev);
17498c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
17508c2ecf20Sopenharmony_cierr_gpio:
17518c2ecf20Sopenharmony_ci	gpiod_put(info->micd_pol_gpio);
17528c2ecf20Sopenharmony_ci	return ret;
17538c2ecf20Sopenharmony_ci}
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_cistatic int arizona_extcon_remove(struct platform_device *pdev)
17568c2ecf20Sopenharmony_ci{
17578c2ecf20Sopenharmony_ci	struct arizona_extcon_info *info = platform_get_drvdata(pdev);
17588c2ecf20Sopenharmony_ci	struct arizona *arizona = info->arizona;
17598c2ecf20Sopenharmony_ci	int jack_irq_rise, jack_irq_fall;
17608c2ecf20Sopenharmony_ci	bool change;
17618c2ecf20Sopenharmony_ci	int ret;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	if (info->micd_clamp) {
17648c2ecf20Sopenharmony_ci		jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
17658c2ecf20Sopenharmony_ci		jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
17668c2ecf20Sopenharmony_ci	} else {
17678c2ecf20Sopenharmony_ci		jack_irq_rise = ARIZONA_IRQ_JD_RISE;
17688c2ecf20Sopenharmony_ci		jack_irq_fall = ARIZONA_IRQ_JD_FALL;
17698c2ecf20Sopenharmony_ci	}
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	arizona_set_irq_wake(arizona, jack_irq_rise, 0);
17728c2ecf20Sopenharmony_ci	arizona_set_irq_wake(arizona, jack_irq_fall, 0);
17738c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
17748c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
17758c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, jack_irq_rise, info);
17768c2ecf20Sopenharmony_ci	arizona_free_irq(arizona, jack_irq_fall, info);
17778c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->hpdet_work);
17788c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->micd_detect_work);
17798c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&info->micd_timeout_work);
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
17828c2ecf20Sopenharmony_ci				       ARIZONA_MICD_ENA, 0,
17838c2ecf20Sopenharmony_ci				       &change);
17848c2ecf20Sopenharmony_ci	if (ret < 0) {
17858c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to disable micd on remove: %d\n",
17868c2ecf20Sopenharmony_ci			ret);
17878c2ecf20Sopenharmony_ci	} else if (change) {
17888c2ecf20Sopenharmony_ci		regulator_disable(info->micvdd);
17898c2ecf20Sopenharmony_ci		pm_runtime_put(info->dev);
17908c2ecf20Sopenharmony_ci	}
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap,
17938c2ecf20Sopenharmony_ci			   ARIZONA_MICD_CLAMP_CONTROL,
17948c2ecf20Sopenharmony_ci			   ARIZONA_MICD_CLAMP_MODE_MASK, 0);
17958c2ecf20Sopenharmony_ci	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
17968c2ecf20Sopenharmony_ci			   ARIZONA_JD1_ENA, 0);
17978c2ecf20Sopenharmony_ci	arizona_clk32k_disable(arizona);
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci	gpiod_put(info->micd_pol_gpio);
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci	return 0;
18048c2ecf20Sopenharmony_ci}
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_cistatic struct platform_driver arizona_extcon_driver = {
18078c2ecf20Sopenharmony_ci	.driver		= {
18088c2ecf20Sopenharmony_ci		.name	= "arizona-extcon",
18098c2ecf20Sopenharmony_ci	},
18108c2ecf20Sopenharmony_ci	.probe		= arizona_extcon_probe,
18118c2ecf20Sopenharmony_ci	.remove		= arizona_extcon_remove,
18128c2ecf20Sopenharmony_ci};
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_cimodule_platform_driver(arizona_extcon_driver);
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Arizona Extcon driver");
18178c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
18188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
18198c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:extcon-arizona");
1820