Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * extcon-arizona.c - Extcon driver Wolfson Arizona devices |
| 3 | * |
| 4 | * Copyright (C) 2012 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/gpio.h> |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 24 | #include <linux/input.h> |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm_runtime.h> |
Charles Keepax | feffb0c | 2015-06-19 17:23:29 +0100 | [diff] [blame^] | 27 | #include <linux/property.h> |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 28 | #include <linux/regulator/consumer.h> |
| 29 | #include <linux/extcon.h> |
| 30 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 31 | #include <sound/soc.h> |
| 32 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 33 | #include <linux/mfd/arizona/core.h> |
| 34 | #include <linux/mfd/arizona/pdata.h> |
| 35 | #include <linux/mfd/arizona/registers.h> |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 36 | #include <dt-bindings/mfd/arizona.h> |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 37 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 38 | #define ARIZONA_MAX_MICD_RANGE 8 |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 39 | |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 40 | #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4 |
| 41 | #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5 |
| 42 | #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9 |
| 43 | #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb |
| 44 | |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 45 | #define ARIZONA_HPDET_MAX 10000 |
| 46 | |
Mark Brown | 2643fd6 | 2013-04-01 19:07:28 +0100 | [diff] [blame] | 47 | #define HPDET_DEBOUNCE 500 |
Mark Brown | 7abd4e2 | 2013-04-01 19:25:55 +0100 | [diff] [blame] | 48 | #define DEFAULT_MICD_TIMEOUT 2000 |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 49 | |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 50 | #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \ |
| 51 | ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \ |
| 52 | ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \ |
| 53 | ARIZONA_MICD_LVL_7) |
| 54 | |
| 55 | #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7) |
| 56 | |
| 57 | #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8) |
| 58 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 59 | struct arizona_extcon_info { |
| 60 | struct device *dev; |
| 61 | struct arizona *arizona; |
| 62 | struct mutex lock; |
| 63 | struct regulator *micvdd; |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 64 | struct input_dev *input; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 65 | |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 66 | u16 last_jackdet; |
| 67 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 68 | int micd_mode; |
| 69 | const struct arizona_micd_config *micd_modes; |
| 70 | int micd_num_modes; |
| 71 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 72 | const struct arizona_micd_range *micd_ranges; |
| 73 | int num_micd_ranges; |
| 74 | |
Mark Brown | 7abd4e2 | 2013-04-01 19:25:55 +0100 | [diff] [blame] | 75 | int micd_timeout; |
| 76 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 77 | bool micd_reva; |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 78 | bool micd_clamp; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 79 | |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 80 | struct delayed_work hpdet_work; |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 81 | struct delayed_work micd_detect_work; |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 82 | struct delayed_work micd_timeout_work; |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 83 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 84 | bool hpdet_active; |
Mark Brown | bf14ee5 | 2013-02-05 20:20:17 +0000 | [diff] [blame] | 85 | bool hpdet_done; |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 86 | bool hpdet_retried; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 87 | |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 88 | int num_hpdet_res; |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 89 | unsigned int hpdet_res[3]; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 90 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 91 | bool mic; |
| 92 | bool detecting; |
| 93 | int jack_flips; |
| 94 | |
Richard Fitzgerald | d0fd5fb | 2015-04-28 13:34:27 +0100 | [diff] [blame] | 95 | int hpdet_ip_version; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 96 | |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 97 | struct extcon_dev *edev; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static const struct arizona_micd_config micd_default_modes[] = { |
Charles Keepax | 4102424 | 2013-09-23 14:33:59 +0100 | [diff] [blame] | 101 | { ARIZONA_ACCDET_SRC, 1, 0 }, |
| 102 | { 0, 2, 1 }, |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 105 | static const struct arizona_micd_range micd_default_ranges[] = { |
| 106 | { .max = 11, .key = BTN_0 }, |
| 107 | { .max = 28, .key = BTN_1 }, |
| 108 | { .max = 54, .key = BTN_2 }, |
| 109 | { .max = 100, .key = BTN_3 }, |
| 110 | { .max = 186, .key = BTN_4 }, |
| 111 | { .max = 430, .key = BTN_5 }, |
| 112 | }; |
| 113 | |
| 114 | static const int arizona_micd_levels[] = { |
| 115 | 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46, |
| 116 | 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100, |
| 117 | 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245, |
| 118 | 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071, |
| 119 | 1257, |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 122 | static const unsigned int arizona_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 123 | EXTCON_MECHANICAL, |
| 124 | EXTCON_MICROPHONE, |
| 125 | EXTCON_HEADPHONE, |
| 126 | EXTCON_LINE_OUT, |
| 127 | EXTCON_NONE, |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 130 | static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info); |
| 131 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 132 | static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info, |
| 133 | bool clamp) |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 134 | { |
| 135 | struct arizona *arizona = info->arizona; |
Charles Keepax | 43f0acd | 2015-02-16 15:41:03 +0000 | [diff] [blame] | 136 | unsigned int mask = 0, val = 0; |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 137 | int ret; |
| 138 | |
Charles Keepax | 43f0acd | 2015-02-16 15:41:03 +0000 | [diff] [blame] | 139 | switch (arizona->type) { |
| 140 | case WM5110: |
Charles Keepax | 2b51f9c | 2015-04-30 23:43:37 +0900 | [diff] [blame] | 141 | case WM8280: |
Charles Keepax | 43f0acd | 2015-02-16 15:41:03 +0000 | [diff] [blame] | 142 | mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR | |
| 143 | ARIZONA_HP1L_SHRTI; |
| 144 | if (clamp) |
| 145 | val = ARIZONA_HP1L_SHRTO; |
| 146 | else |
| 147 | val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI; |
| 148 | break; |
| 149 | default: |
| 150 | mask = ARIZONA_RMV_SHRT_HP1L; |
| 151 | if (clamp) |
| 152 | val = ARIZONA_RMV_SHRT_HP1L; |
| 153 | break; |
| 154 | }; |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 155 | |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 156 | mutex_lock(&arizona->dapm->card->dapm_mutex); |
| 157 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 158 | arizona->hpdet_clamp = clamp; |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 159 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 160 | /* Keep the HP output stages disabled while doing the clamp */ |
| 161 | if (clamp) { |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 162 | ret = regmap_update_bits(arizona->regmap, |
| 163 | ARIZONA_OUTPUT_ENABLES_1, |
| 164 | ARIZONA_OUT1L_ENA | |
| 165 | ARIZONA_OUT1R_ENA, 0); |
| 166 | if (ret != 0) |
| 167 | dev_warn(arizona->dev, |
| 168 | "Failed to disable headphone outputs: %d\n", |
| 169 | ret); |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 172 | ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L, |
Charles Keepax | 43f0acd | 2015-02-16 15:41:03 +0000 | [diff] [blame] | 173 | mask, val); |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 174 | if (ret != 0) |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 175 | dev_warn(arizona->dev, "Failed to do clamp: %d\n", |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 176 | ret); |
| 177 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 178 | ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R, |
Charles Keepax | 43f0acd | 2015-02-16 15:41:03 +0000 | [diff] [blame] | 179 | mask, val); |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 180 | if (ret != 0) |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 181 | dev_warn(arizona->dev, "Failed to do clamp: %d\n", |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 182 | ret); |
| 183 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 184 | /* Restore the desired state while not doing the clamp */ |
| 185 | if (!clamp) { |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 186 | ret = regmap_update_bits(arizona->regmap, |
| 187 | ARIZONA_OUTPUT_ENABLES_1, |
| 188 | ARIZONA_OUT1L_ENA | |
| 189 | ARIZONA_OUT1R_ENA, arizona->hp_ena); |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 190 | if (ret != 0) |
Mark Brown | df8c3db | 2013-02-22 18:38:03 +0000 | [diff] [blame] | 191 | dev_warn(arizona->dev, |
| 192 | "Failed to restore headphone outputs: %d\n", |
Mark Brown | 0340907 | 2013-02-12 13:00:31 +0000 | [diff] [blame] | 193 | ret); |
| 194 | } |
| 195 | |
| 196 | mutex_unlock(&arizona->dapm->card->dapm_mutex); |
| 197 | } |
| 198 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 199 | static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode) |
| 200 | { |
| 201 | struct arizona *arizona = info->arizona; |
| 202 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 203 | mode %= info->micd_num_modes; |
Mark Brown | 84eaa13 | 2013-01-25 20:14:44 +0800 | [diff] [blame] | 204 | |
Mark Brown | cd74f7b | 2012-11-27 16:14:26 +0900 | [diff] [blame] | 205 | if (arizona->pdata.micd_pol_gpio > 0) |
| 206 | gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio, |
| 207 | info->micd_modes[mode].gpio); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 208 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 209 | ARIZONA_MICD_BIAS_SRC_MASK, |
Charles Keepax | 4102424 | 2013-09-23 14:33:59 +0100 | [diff] [blame] | 210 | info->micd_modes[mode].bias << |
| 211 | ARIZONA_MICD_BIAS_SRC_SHIFT); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 212 | regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 213 | ARIZONA_ACCDET_SRC, info->micd_modes[mode].src); |
| 214 | |
| 215 | info->micd_mode = mode; |
| 216 | |
| 217 | dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode); |
| 218 | } |
| 219 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 220 | static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info) |
| 221 | { |
Charles Keepax | 4102424 | 2013-09-23 14:33:59 +0100 | [diff] [blame] | 222 | switch (info->micd_modes[0].bias) { |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 223 | case 1: |
| 224 | return "MICBIAS1"; |
| 225 | case 2: |
| 226 | return "MICBIAS2"; |
| 227 | case 3: |
| 228 | return "MICBIAS3"; |
| 229 | default: |
| 230 | return "MICVDD"; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info) |
| 235 | { |
| 236 | struct arizona *arizona = info->arizona; |
| 237 | const char *widget = arizona_extcon_get_micbias(info); |
| 238 | struct snd_soc_dapm_context *dapm = arizona->dapm; |
| 239 | int ret; |
| 240 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 241 | ret = snd_soc_dapm_force_enable_pin(dapm, widget); |
| 242 | if (ret != 0) |
| 243 | dev_warn(arizona->dev, "Failed to enable %s: %d\n", |
| 244 | widget, ret); |
| 245 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 246 | snd_soc_dapm_sync(dapm); |
| 247 | |
| 248 | if (!arizona->pdata.micd_force_micbias) { |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 249 | ret = snd_soc_dapm_disable_pin(arizona->dapm, widget); |
| 250 | if (ret != 0) |
| 251 | dev_warn(arizona->dev, "Failed to disable %s: %d\n", |
| 252 | widget, ret); |
| 253 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 254 | snd_soc_dapm_sync(dapm); |
| 255 | } |
| 256 | } |
| 257 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 258 | static void arizona_start_mic(struct arizona_extcon_info *info) |
| 259 | { |
| 260 | struct arizona *arizona = info->arizona; |
| 261 | bool change; |
| 262 | int ret; |
| 263 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 264 | /* Microphone detection can't use idle mode */ |
| 265 | pm_runtime_get(info->dev); |
| 266 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 267 | if (info->detecting) { |
| 268 | ret = regulator_allow_bypass(info->micvdd, false); |
| 269 | if (ret != 0) { |
| 270 | dev_err(arizona->dev, |
| 271 | "Failed to regulate MICVDD: %d\n", |
| 272 | ret); |
| 273 | } |
| 274 | } |
| 275 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 276 | ret = regulator_enable(info->micvdd); |
| 277 | if (ret != 0) { |
| 278 | dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", |
| 279 | ret); |
| 280 | } |
| 281 | |
| 282 | if (info->micd_reva) { |
| 283 | regmap_write(arizona->regmap, 0x80, 0x3); |
| 284 | regmap_write(arizona->regmap, 0x294, 0); |
| 285 | regmap_write(arizona->regmap, 0x80, 0x0); |
| 286 | } |
| 287 | |
| 288 | regmap_update_bits(arizona->regmap, |
| 289 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 290 | ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC); |
| 291 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 292 | arizona_extcon_pulse_micbias(info); |
| 293 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 294 | regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 295 | ARIZONA_MICD_ENA, ARIZONA_MICD_ENA, |
| 296 | &change); |
| 297 | if (!change) { |
| 298 | regulator_disable(info->micvdd); |
| 299 | pm_runtime_put_autosuspend(info->dev); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static void arizona_stop_mic(struct arizona_extcon_info *info) |
| 304 | { |
| 305 | struct arizona *arizona = info->arizona; |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 306 | const char *widget = arizona_extcon_get_micbias(info); |
| 307 | struct snd_soc_dapm_context *dapm = arizona->dapm; |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 308 | bool change; |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 309 | int ret; |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 310 | |
| 311 | regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 312 | ARIZONA_MICD_ENA, 0, |
| 313 | &change); |
| 314 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 315 | ret = snd_soc_dapm_disable_pin(dapm, widget); |
| 316 | if (ret != 0) |
| 317 | dev_warn(arizona->dev, |
| 318 | "Failed to disable %s: %d\n", |
| 319 | widget, ret); |
| 320 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 321 | snd_soc_dapm_sync(dapm); |
| 322 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 323 | if (info->micd_reva) { |
| 324 | regmap_write(arizona->regmap, 0x80, 0x3); |
| 325 | regmap_write(arizona->regmap, 0x294, 2); |
| 326 | regmap_write(arizona->regmap, 0x80, 0x0); |
| 327 | } |
| 328 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 329 | ret = regulator_allow_bypass(info->micvdd, true); |
| 330 | if (ret != 0) { |
| 331 | dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", |
| 332 | ret); |
| 333 | } |
| 334 | |
Mark Brown | 9b1270c | 2013-01-11 08:55:46 +0900 | [diff] [blame] | 335 | if (change) { |
| 336 | regulator_disable(info->micvdd); |
| 337 | pm_runtime_mark_last_busy(info->dev); |
| 338 | pm_runtime_put_autosuspend(info->dev); |
| 339 | } |
| 340 | } |
| 341 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 342 | static struct { |
Charles Keepax | 24a279b | 2014-05-30 13:19:17 +0100 | [diff] [blame] | 343 | unsigned int threshold; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 344 | unsigned int factor_a; |
| 345 | unsigned int factor_b; |
| 346 | } arizona_hpdet_b_ranges[] = { |
Charles Keepax | 24a279b | 2014-05-30 13:19:17 +0100 | [diff] [blame] | 347 | { 100, 5528, 362464 }, |
| 348 | { 169, 11084, 6186851 }, |
| 349 | { 169, 11065, 65460395 }, |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 350 | }; |
| 351 | |
Charles Keepax | 24a279b | 2014-05-30 13:19:17 +0100 | [diff] [blame] | 352 | #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb |
| 353 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 354 | static struct { |
| 355 | int min; |
| 356 | int max; |
| 357 | } arizona_hpdet_c_ranges[] = { |
| 358 | { 0, 30 }, |
| 359 | { 8, 100 }, |
| 360 | { 100, 1000 }, |
| 361 | { 1000, 10000 }, |
| 362 | }; |
| 363 | |
| 364 | static int arizona_hpdet_read(struct arizona_extcon_info *info) |
| 365 | { |
| 366 | struct arizona *arizona = info->arizona; |
| 367 | unsigned int val, range; |
| 368 | int ret; |
| 369 | |
| 370 | ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val); |
| 371 | if (ret != 0) { |
| 372 | dev_err(arizona->dev, "Failed to read HPDET status: %d\n", |
| 373 | ret); |
| 374 | return ret; |
| 375 | } |
| 376 | |
Richard Fitzgerald | d0fd5fb | 2015-04-28 13:34:27 +0100 | [diff] [blame] | 377 | switch (info->hpdet_ip_version) { |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 378 | case 0: |
| 379 | if (!(val & ARIZONA_HP_DONE)) { |
| 380 | dev_err(arizona->dev, "HPDET did not complete: %x\n", |
| 381 | val); |
Mark Brown | e6dd8cf | 2013-01-21 17:30:02 +0900 | [diff] [blame] | 382 | return -EAGAIN; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | val &= ARIZONA_HP_LVL_MASK; |
| 386 | break; |
| 387 | |
| 388 | case 1: |
| 389 | if (!(val & ARIZONA_HP_DONE_B)) { |
| 390 | dev_err(arizona->dev, "HPDET did not complete: %x\n", |
| 391 | val); |
Mark Brown | e6dd8cf | 2013-01-21 17:30:02 +0900 | [diff] [blame] | 392 | return -EAGAIN; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val); |
| 396 | if (ret != 0) { |
| 397 | dev_err(arizona->dev, "Failed to read HP value: %d\n", |
| 398 | ret); |
Mark Brown | e6dd8cf | 2013-01-21 17:30:02 +0900 | [diff] [blame] | 399 | return -EAGAIN; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, |
| 403 | &range); |
| 404 | range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK) |
| 405 | >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT; |
| 406 | |
| 407 | if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 && |
Charles Keepax | 24a279b | 2014-05-30 13:19:17 +0100 | [diff] [blame] | 408 | (val < arizona_hpdet_b_ranges[range].threshold || |
| 409 | val >= ARIZONA_HPDET_B_RANGE_MAX)) { |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 410 | range++; |
| 411 | dev_dbg(arizona->dev, "Moving to HPDET range %d\n", |
| 412 | range); |
| 413 | regmap_update_bits(arizona->regmap, |
| 414 | ARIZONA_HEADPHONE_DETECT_1, |
| 415 | ARIZONA_HP_IMPEDANCE_RANGE_MASK, |
| 416 | range << |
| 417 | ARIZONA_HP_IMPEDANCE_RANGE_SHIFT); |
| 418 | return -EAGAIN; |
| 419 | } |
| 420 | |
| 421 | /* If we go out of range report top of range */ |
Charles Keepax | 24a279b | 2014-05-30 13:19:17 +0100 | [diff] [blame] | 422 | if (val < arizona_hpdet_b_ranges[range].threshold || |
| 423 | val >= ARIZONA_HPDET_B_RANGE_MAX) { |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 424 | dev_dbg(arizona->dev, "Measurement out of range\n"); |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 425 | return ARIZONA_HPDET_MAX; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | dev_dbg(arizona->dev, "HPDET read %d in range %d\n", |
| 429 | val, range); |
| 430 | |
| 431 | val = arizona_hpdet_b_ranges[range].factor_b |
| 432 | / ((val * 100) - |
| 433 | arizona_hpdet_b_ranges[range].factor_a); |
| 434 | break; |
| 435 | |
| 436 | default: |
| 437 | dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", |
Richard Fitzgerald | d0fd5fb | 2015-04-28 13:34:27 +0100 | [diff] [blame] | 438 | info->hpdet_ip_version); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 439 | case 2: |
| 440 | if (!(val & ARIZONA_HP_DONE_B)) { |
| 441 | dev_err(arizona->dev, "HPDET did not complete: %x\n", |
| 442 | val); |
Mark Brown | e6dd8cf | 2013-01-21 17:30:02 +0900 | [diff] [blame] | 443 | return -EAGAIN; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | val &= ARIZONA_HP_LVL_B_MASK; |
Charles Keepax | 7743861 | 2013-11-14 16:18:25 +0000 | [diff] [blame] | 447 | /* Convert to ohms, the value is in 0.5 ohm increments */ |
| 448 | val /= 2; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 449 | |
| 450 | regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, |
| 451 | &range); |
| 452 | range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK) |
| 453 | >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT; |
| 454 | |
Charles Keepax | 9141461 | 2013-11-14 16:18:24 +0000 | [diff] [blame] | 455 | /* Skip up a range, or report? */ |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 456 | if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 && |
| 457 | (val >= arizona_hpdet_c_ranges[range].max)) { |
| 458 | range++; |
| 459 | dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n", |
| 460 | arizona_hpdet_c_ranges[range].min, |
| 461 | arizona_hpdet_c_ranges[range].max); |
| 462 | regmap_update_bits(arizona->regmap, |
| 463 | ARIZONA_HEADPHONE_DETECT_1, |
| 464 | ARIZONA_HP_IMPEDANCE_RANGE_MASK, |
| 465 | range << |
| 466 | ARIZONA_HP_IMPEDANCE_RANGE_SHIFT); |
| 467 | return -EAGAIN; |
| 468 | } |
Charles Keepax | 9141461 | 2013-11-14 16:18:24 +0000 | [diff] [blame] | 469 | |
| 470 | if (range && (val < arizona_hpdet_c_ranges[range].min)) { |
| 471 | dev_dbg(arizona->dev, "Reporting range boundary %d\n", |
| 472 | arizona_hpdet_c_ranges[range].min); |
| 473 | val = arizona_hpdet_c_ranges[range].min; |
| 474 | } |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | dev_dbg(arizona->dev, "HP impedance %d ohms\n", val); |
| 478 | return val; |
| 479 | } |
| 480 | |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 481 | static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading, |
| 482 | bool *mic) |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 483 | { |
| 484 | struct arizona *arizona = info->arizona; |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 485 | int id_gpio = arizona->pdata.hpdet_id_gpio; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * If we're using HPDET for accessory identification we need |
| 489 | * to take multiple measurements, step through them in sequence. |
| 490 | */ |
| 491 | if (arizona->pdata.hpdet_acc_id) { |
| 492 | info->hpdet_res[info->num_hpdet_res++] = *reading; |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 493 | |
| 494 | /* Only check the mic directly if we didn't already ID it */ |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 495 | if (id_gpio && info->num_hpdet_res == 1) { |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 496 | dev_dbg(arizona->dev, "Measuring mic\n"); |
| 497 | |
| 498 | regmap_update_bits(arizona->regmap, |
| 499 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 500 | ARIZONA_ACCDET_MODE_MASK | |
| 501 | ARIZONA_ACCDET_SRC, |
| 502 | ARIZONA_ACCDET_MODE_HPR | |
| 503 | info->micd_modes[0].src); |
| 504 | |
| 505 | gpio_set_value_cansleep(id_gpio, 1); |
| 506 | |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 507 | regmap_update_bits(arizona->regmap, |
| 508 | ARIZONA_HEADPHONE_DETECT_1, |
| 509 | ARIZONA_HP_POLL, ARIZONA_HP_POLL); |
| 510 | return -EAGAIN; |
| 511 | } |
| 512 | |
| 513 | /* OK, got both. Now, compare... */ |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 514 | dev_dbg(arizona->dev, "HPDET measured %d %d\n", |
| 515 | info->hpdet_res[0], info->hpdet_res[1]); |
Mark Brown | c37b387 | 2013-02-05 17:48:49 +0000 | [diff] [blame] | 516 | |
| 517 | /* Take the headphone impedance for the main report */ |
| 518 | *reading = info->hpdet_res[0]; |
| 519 | |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 520 | /* Sometimes we get false readings due to slow insert */ |
| 521 | if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) { |
| 522 | dev_dbg(arizona->dev, "Retrying high impedance\n"); |
| 523 | info->num_hpdet_res = 0; |
| 524 | info->hpdet_retried = true; |
| 525 | arizona_start_hpdet_acc_id(info); |
| 526 | pm_runtime_put(info->dev); |
| 527 | return -EAGAIN; |
| 528 | } |
| 529 | |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 530 | /* |
Sachin Kamat | d97abdd | 2013-08-05 14:30:46 +0530 | [diff] [blame] | 531 | * If we measure the mic as high impedance |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 532 | */ |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 533 | if (!id_gpio || info->hpdet_res[1] > 50) { |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 534 | dev_dbg(arizona->dev, "Detected mic\n"); |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 535 | *mic = true; |
Mark Brown | bf14ee5 | 2013-02-05 20:20:17 +0000 | [diff] [blame] | 536 | info->detecting = true; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 537 | } else { |
| 538 | dev_dbg(arizona->dev, "Detected headphone\n"); |
| 539 | } |
| 540 | |
| 541 | /* Make sure everything is reset back to the real polarity */ |
| 542 | regmap_update_bits(arizona->regmap, |
| 543 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 544 | ARIZONA_ACCDET_SRC, |
| 545 | info->micd_modes[0].src); |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 551 | static irqreturn_t arizona_hpdet_irq(int irq, void *data) |
| 552 | { |
| 553 | struct arizona_extcon_info *info = data; |
| 554 | struct arizona *arizona = info->arizona; |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 555 | int id_gpio = arizona->pdata.hpdet_id_gpio; |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 556 | unsigned int report = EXTCON_HEADPHONE; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 557 | int ret, reading; |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 558 | bool mic = false; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 559 | |
| 560 | mutex_lock(&info->lock); |
| 561 | |
| 562 | /* If we got a spurious IRQ for some reason then ignore it */ |
| 563 | if (!info->hpdet_active) { |
| 564 | dev_warn(arizona->dev, "Spurious HPDET IRQ\n"); |
| 565 | mutex_unlock(&info->lock); |
| 566 | return IRQ_NONE; |
| 567 | } |
| 568 | |
| 569 | /* If the cable was removed while measuring ignore the result */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 570 | ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 571 | if (ret < 0) { |
| 572 | dev_err(arizona->dev, "Failed to check cable state: %d\n", |
| 573 | ret); |
| 574 | goto out; |
| 575 | } else if (!ret) { |
| 576 | dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n"); |
| 577 | goto done; |
| 578 | } |
| 579 | |
| 580 | ret = arizona_hpdet_read(info); |
Chanwoo Choi | d667566 | 2013-08-23 10:21:39 +0900 | [diff] [blame] | 581 | if (ret == -EAGAIN) |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 582 | goto out; |
Chanwoo Choi | d667566 | 2013-08-23 10:21:39 +0900 | [diff] [blame] | 583 | else if (ret < 0) |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 584 | goto done; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 585 | reading = ret; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 586 | |
| 587 | /* Reset back to starting range */ |
| 588 | regmap_update_bits(arizona->regmap, |
| 589 | ARIZONA_HEADPHONE_DETECT_1, |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 590 | ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL, |
| 591 | 0); |
| 592 | |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 593 | ret = arizona_hpdet_do_id(info, &reading, &mic); |
Chanwoo Choi | d667566 | 2013-08-23 10:21:39 +0900 | [diff] [blame] | 594 | if (ret == -EAGAIN) |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 595 | goto out; |
Chanwoo Choi | d667566 | 2013-08-23 10:21:39 +0900 | [diff] [blame] | 596 | else if (ret < 0) |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 597 | goto done; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 598 | |
| 599 | /* Report high impedence cables as line outputs */ |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 600 | if (reading >= 5000) |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 601 | report = EXTCON_LINE_OUT; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 602 | else |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 603 | report = EXTCON_HEADPHONE; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 604 | |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 605 | ret = extcon_set_cable_state_(info->edev, report, true); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 606 | if (ret != 0) |
| 607 | dev_err(arizona->dev, "Failed to report HP/line: %d\n", |
| 608 | ret); |
| 609 | |
Charles Keepax | a3e00d4 | 2013-11-14 16:18:22 +0000 | [diff] [blame] | 610 | done: |
| 611 | /* Reset back to starting range */ |
| 612 | regmap_update_bits(arizona->regmap, |
| 613 | ARIZONA_HEADPHONE_DETECT_1, |
| 614 | ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL, |
| 615 | 0); |
| 616 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 617 | arizona_extcon_hp_clamp(info, false); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 618 | |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 619 | if (id_gpio) |
| 620 | gpio_set_value_cansleep(id_gpio, 0); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 621 | |
| 622 | /* Revert back to MICDET mode */ |
| 623 | regmap_update_bits(arizona->regmap, |
| 624 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 625 | ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC); |
| 626 | |
| 627 | /* If we have a mic then reenable MICDET */ |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 628 | if (mic || info->mic) |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 629 | arizona_start_mic(info); |
| 630 | |
| 631 | if (info->hpdet_active) { |
| 632 | pm_runtime_put_autosuspend(info->dev); |
| 633 | info->hpdet_active = false; |
| 634 | } |
| 635 | |
Mark Brown | bf14ee5 | 2013-02-05 20:20:17 +0000 | [diff] [blame] | 636 | info->hpdet_done = true; |
| 637 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 638 | out: |
| 639 | mutex_unlock(&info->lock); |
| 640 | |
| 641 | return IRQ_HANDLED; |
| 642 | } |
| 643 | |
| 644 | static void arizona_identify_headphone(struct arizona_extcon_info *info) |
| 645 | { |
| 646 | struct arizona *arizona = info->arizona; |
| 647 | int ret; |
| 648 | |
Mark Brown | bf14ee5 | 2013-02-05 20:20:17 +0000 | [diff] [blame] | 649 | if (info->hpdet_done) |
| 650 | return; |
| 651 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 652 | dev_dbg(arizona->dev, "Starting HPDET\n"); |
| 653 | |
| 654 | /* Make sure we keep the device enabled during the measurement */ |
| 655 | pm_runtime_get(info->dev); |
| 656 | |
| 657 | info->hpdet_active = true; |
| 658 | |
| 659 | if (info->mic) |
| 660 | arizona_stop_mic(info); |
| 661 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 662 | arizona_extcon_hp_clamp(info, true); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 663 | |
| 664 | ret = regmap_update_bits(arizona->regmap, |
| 665 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 666 | ARIZONA_ACCDET_MODE_MASK, |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 667 | arizona->pdata.hpdet_channel); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 668 | if (ret != 0) { |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 669 | dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 670 | goto err; |
| 671 | } |
| 672 | |
| 673 | ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, |
| 674 | ARIZONA_HP_POLL, ARIZONA_HP_POLL); |
| 675 | if (ret != 0) { |
| 676 | dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", |
| 677 | ret); |
| 678 | goto err; |
| 679 | } |
| 680 | |
| 681 | return; |
| 682 | |
| 683 | err: |
| 684 | regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 685 | ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC); |
| 686 | |
| 687 | /* Just report headphone */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 688 | ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 689 | if (ret != 0) |
| 690 | dev_err(arizona->dev, "Failed to report headphone: %d\n", ret); |
| 691 | |
| 692 | if (info->mic) |
| 693 | arizona_start_mic(info); |
| 694 | |
| 695 | info->hpdet_active = false; |
| 696 | } |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 697 | |
| 698 | static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info) |
| 699 | { |
| 700 | struct arizona *arizona = info->arizona; |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 701 | int hp_reading = 32; |
| 702 | bool mic; |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 703 | int ret; |
| 704 | |
| 705 | dev_dbg(arizona->dev, "Starting identification via HPDET\n"); |
| 706 | |
| 707 | /* Make sure we keep the device enabled during the measurement */ |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 708 | pm_runtime_get_sync(info->dev); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 709 | |
| 710 | info->hpdet_active = true; |
| 711 | |
Charles Keepax | 112bdfa | 2015-02-16 15:41:02 +0000 | [diff] [blame] | 712 | arizona_extcon_hp_clamp(info, true); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 713 | |
| 714 | ret = regmap_update_bits(arizona->regmap, |
| 715 | ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 716 | ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK, |
| 717 | info->micd_modes[0].src | |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 718 | arizona->pdata.hpdet_channel); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 719 | if (ret != 0) { |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 720 | dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 721 | goto err; |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 722 | } |
| 723 | |
Mark Brown | 9c2ba27 | 2013-02-25 23:42:31 +0000 | [diff] [blame] | 724 | if (arizona->pdata.hpdet_acc_id_line) { |
| 725 | ret = regmap_update_bits(arizona->regmap, |
| 726 | ARIZONA_HEADPHONE_DETECT_1, |
| 727 | ARIZONA_HP_POLL, ARIZONA_HP_POLL); |
| 728 | if (ret != 0) { |
| 729 | dev_err(arizona->dev, |
| 730 | "Can't start HPDETL measurement: %d\n", |
| 731 | ret); |
| 732 | goto err; |
| 733 | } |
| 734 | } else { |
| 735 | arizona_hpdet_do_id(info, &hp_reading, &mic); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | return; |
| 739 | |
| 740 | err: |
| 741 | regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1, |
| 742 | ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC); |
| 743 | |
| 744 | /* Just report headphone */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 745 | ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 746 | if (ret != 0) |
| 747 | dev_err(arizona->dev, "Failed to report headphone: %d\n", ret); |
| 748 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 749 | info->hpdet_active = false; |
| 750 | } |
| 751 | |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 752 | static void arizona_micd_timeout_work(struct work_struct *work) |
| 753 | { |
| 754 | struct arizona_extcon_info *info = container_of(work, |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 755 | struct arizona_extcon_info, |
| 756 | micd_timeout_work.work); |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 757 | |
| 758 | mutex_lock(&info->lock); |
| 759 | |
| 760 | dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n"); |
| 761 | arizona_identify_headphone(info); |
| 762 | |
| 763 | info->detecting = false; |
| 764 | |
| 765 | arizona_stop_mic(info); |
| 766 | |
| 767 | mutex_unlock(&info->lock); |
| 768 | } |
| 769 | |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 770 | static void arizona_micd_detect(struct work_struct *work) |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 771 | { |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 772 | struct arizona_extcon_info *info = container_of(work, |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 773 | struct arizona_extcon_info, |
| 774 | micd_detect_work.work); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 775 | struct arizona *arizona = info->arizona; |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 776 | unsigned int val = 0, lvl; |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 777 | int ret, i, key; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 778 | |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 779 | cancel_delayed_work_sync(&info->micd_timeout_work); |
| 780 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 781 | mutex_lock(&info->lock); |
| 782 | |
Charles Keepax | 31a847e | 2013-11-14 16:18:23 +0000 | [diff] [blame] | 783 | /* If the cable was removed while measuring ignore the result */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 784 | ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL); |
Charles Keepax | 31a847e | 2013-11-14 16:18:23 +0000 | [diff] [blame] | 785 | if (ret < 0) { |
| 786 | dev_err(arizona->dev, "Failed to check cable state: %d\n", |
| 787 | ret); |
| 788 | mutex_unlock(&info->lock); |
| 789 | return; |
| 790 | } else if (!ret) { |
| 791 | dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n"); |
| 792 | mutex_unlock(&info->lock); |
| 793 | return; |
| 794 | } |
| 795 | |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 796 | for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) { |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 797 | ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val); |
| 798 | if (ret != 0) { |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 799 | dev_err(arizona->dev, |
| 800 | "Failed to read MICDET: %d\n", ret); |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 801 | mutex_unlock(&info->lock); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 802 | return; |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | dev_dbg(arizona->dev, "MICDET: %x\n", val); |
| 806 | |
| 807 | if (!(val & ARIZONA_MICD_VALID)) { |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 808 | dev_warn(arizona->dev, |
| 809 | "Microphone detection state invalid\n"); |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 810 | mutex_unlock(&info->lock); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 811 | return; |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 812 | } |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 815 | if (i == 10 && !(val & MICD_LVL_0_TO_8)) { |
Charles Keepax | e2c0f47 | 2013-04-01 19:06:29 +0100 | [diff] [blame] | 816 | dev_err(arizona->dev, "Failed to get valid MICDET value\n"); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 817 | mutex_unlock(&info->lock); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 818 | return; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* Due to jack detect this should never happen */ |
| 822 | if (!(val & ARIZONA_MICD_STS)) { |
| 823 | dev_warn(arizona->dev, "Detected open circuit\n"); |
| 824 | info->detecting = false; |
| 825 | goto handled; |
| 826 | } |
| 827 | |
| 828 | /* If we got a high impedence we should have a headset, report it. */ |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 829 | if (info->detecting && (val & ARIZONA_MICD_LVL_8)) { |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 830 | arizona_identify_headphone(info); |
| 831 | |
Nikesh Oswal | 3460248 | 2014-05-29 16:27:52 +0100 | [diff] [blame] | 832 | ret = extcon_set_cable_state_(info->edev, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 833 | EXTCON_MICROPHONE, true); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 834 | if (ret != 0) |
| 835 | dev_err(arizona->dev, "Headset report failed: %d\n", |
| 836 | ret); |
| 837 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 838 | /* Don't need to regulate for button detection */ |
Charles Keepax | e368f52 | 2014-05-29 16:27:54 +0100 | [diff] [blame] | 839 | ret = regulator_allow_bypass(info->micvdd, true); |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 840 | if (ret != 0) { |
| 841 | dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", |
| 842 | ret); |
| 843 | } |
| 844 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 845 | info->mic = true; |
| 846 | info->detecting = false; |
| 847 | goto handled; |
| 848 | } |
| 849 | |
| 850 | /* If we detected a lower impedence during initial startup |
| 851 | * then we probably have the wrong polarity, flip it. Don't |
| 852 | * do this for the lowest impedences to speed up detection of |
| 853 | * plain headphones. If both polarities report a low |
| 854 | * impedence then give up and report headphones. |
| 855 | */ |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 856 | if (info->detecting && (val & MICD_LVL_1_TO_7)) { |
Mark Brown | 84eaa13 | 2013-01-25 20:14:44 +0800 | [diff] [blame] | 857 | if (info->jack_flips >= info->micd_num_modes * 10) { |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 858 | dev_dbg(arizona->dev, "Detected HP/line\n"); |
| 859 | arizona_identify_headphone(info); |
Mark Brown | 9ef2224d | 2012-06-28 13:08:31 +0100 | [diff] [blame] | 860 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 861 | info->detecting = false; |
| 862 | |
| 863 | arizona_stop_mic(info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 864 | } else { |
| 865 | info->micd_mode++; |
| 866 | if (info->micd_mode == info->micd_num_modes) |
| 867 | info->micd_mode = 0; |
| 868 | arizona_extcon_set_mode(info, info->micd_mode); |
| 869 | |
| 870 | info->jack_flips++; |
| 871 | } |
| 872 | |
| 873 | goto handled; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * If we're still detecting and we detect a short then we've |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 878 | * got a headphone. Otherwise it's a button press. |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 879 | */ |
Charles Keepax | ffae24f | 2013-11-14 16:18:21 +0000 | [diff] [blame] | 880 | if (val & MICD_LVL_0_TO_7) { |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 881 | if (info->mic) { |
| 882 | dev_dbg(arizona->dev, "Mic button detected\n"); |
| 883 | |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 884 | lvl = val & ARIZONA_MICD_LVL_MASK; |
| 885 | lvl >>= ARIZONA_MICD_LVL_SHIFT; |
| 886 | |
Mark Brown | 41a5785 | 2013-04-01 19:18:18 +0100 | [diff] [blame] | 887 | for (i = 0; i < info->num_micd_ranges; i++) |
| 888 | input_report_key(info->input, |
| 889 | info->micd_ranges[i].key, 0); |
| 890 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 891 | WARN_ON(!lvl); |
| 892 | WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges); |
| 893 | if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) { |
| 894 | key = info->micd_ranges[ffs(lvl) - 1].key; |
| 895 | input_report_key(info->input, key, 1); |
| 896 | input_sync(info->input); |
| 897 | } |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 898 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 899 | } else if (info->detecting) { |
| 900 | dev_dbg(arizona->dev, "Headphone detected\n"); |
| 901 | info->detecting = false; |
| 902 | arizona_stop_mic(info); |
| 903 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 904 | arizona_identify_headphone(info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 905 | } else { |
| 906 | dev_warn(arizona->dev, "Button with no mic: %x\n", |
| 907 | val); |
| 908 | } |
| 909 | } else { |
| 910 | dev_dbg(arizona->dev, "Mic button released\n"); |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 911 | for (i = 0; i < info->num_micd_ranges; i++) |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 912 | input_report_key(info->input, |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 913 | info->micd_ranges[i].key, 0); |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 914 | input_sync(info->input); |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 915 | arizona_extcon_pulse_micbias(info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | handled: |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 919 | if (info->detecting) |
Mark Brown | df9a5ab | 2013-07-18 22:42:22 +0100 | [diff] [blame] | 920 | queue_delayed_work(system_power_efficient_wq, |
| 921 | &info->micd_timeout_work, |
| 922 | msecs_to_jiffies(info->micd_timeout)); |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 923 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 924 | pm_runtime_mark_last_busy(info->dev); |
| 925 | mutex_unlock(&info->lock); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static irqreturn_t arizona_micdet(int irq, void *data) |
| 929 | { |
| 930 | struct arizona_extcon_info *info = data; |
| 931 | struct arizona *arizona = info->arizona; |
| 932 | int debounce = arizona->pdata.micd_detect_debounce; |
| 933 | |
| 934 | cancel_delayed_work_sync(&info->micd_detect_work); |
| 935 | cancel_delayed_work_sync(&info->micd_timeout_work); |
| 936 | |
| 937 | mutex_lock(&info->lock); |
| 938 | if (!info->detecting) |
| 939 | debounce = 0; |
| 940 | mutex_unlock(&info->lock); |
| 941 | |
| 942 | if (debounce) |
Mark Brown | df9a5ab | 2013-07-18 22:42:22 +0100 | [diff] [blame] | 943 | queue_delayed_work(system_power_efficient_wq, |
| 944 | &info->micd_detect_work, |
| 945 | msecs_to_jiffies(debounce)); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 946 | else |
| 947 | arizona_micd_detect(&info->micd_detect_work.work); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 948 | |
| 949 | return IRQ_HANDLED; |
| 950 | } |
| 951 | |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 952 | static void arizona_hpdet_work(struct work_struct *work) |
| 953 | { |
| 954 | struct arizona_extcon_info *info = container_of(work, |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 955 | struct arizona_extcon_info, |
| 956 | hpdet_work.work); |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 957 | |
| 958 | mutex_lock(&info->lock); |
| 959 | arizona_start_hpdet_acc_id(info); |
| 960 | mutex_unlock(&info->lock); |
| 961 | } |
| 962 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 963 | static irqreturn_t arizona_jackdet(int irq, void *data) |
| 964 | { |
| 965 | struct arizona_extcon_info *info = data; |
| 966 | struct arizona *arizona = info->arizona; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 967 | unsigned int val, present, mask; |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 968 | bool cancelled_hp, cancelled_mic; |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 969 | int ret, i; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 970 | |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 971 | cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work); |
| 972 | cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 973 | |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 974 | pm_runtime_get_sync(info->dev); |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 975 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 976 | mutex_lock(&info->lock); |
| 977 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 978 | if (arizona->pdata.jd_gpio5) { |
| 979 | mask = ARIZONA_MICD_CLAMP_STS; |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 980 | if (arizona->pdata.jd_invert) |
| 981 | present = ARIZONA_MICD_CLAMP_STS; |
| 982 | else |
| 983 | present = 0; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 984 | } else { |
| 985 | mask = ARIZONA_JD1_STS; |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 986 | if (arizona->pdata.jd_invert) |
| 987 | present = 0; |
| 988 | else |
| 989 | present = ARIZONA_JD1_STS; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 990 | } |
| 991 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 992 | ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val); |
| 993 | if (ret != 0) { |
| 994 | dev_err(arizona->dev, "Failed to read jackdet status: %d\n", |
| 995 | ret); |
| 996 | mutex_unlock(&info->lock); |
| 997 | pm_runtime_put_autosuspend(info->dev); |
| 998 | return IRQ_NONE; |
| 999 | } |
| 1000 | |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 1001 | val &= mask; |
| 1002 | if (val == info->last_jackdet) { |
| 1003 | dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n"); |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 1004 | if (cancelled_hp) |
Mark Brown | df9a5ab | 2013-07-18 22:42:22 +0100 | [diff] [blame] | 1005 | queue_delayed_work(system_power_efficient_wq, |
| 1006 | &info->hpdet_work, |
| 1007 | msecs_to_jiffies(HPDET_DEBOUNCE)); |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 1008 | |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 1009 | if (cancelled_mic) { |
| 1010 | int micd_timeout = info->micd_timeout; |
| 1011 | |
Mark Brown | df9a5ab | 2013-07-18 22:42:22 +0100 | [diff] [blame] | 1012 | queue_delayed_work(system_power_efficient_wq, |
| 1013 | &info->micd_timeout_work, |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 1014 | msecs_to_jiffies(micd_timeout)); |
| 1015 | } |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 1016 | |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 1017 | goto out; |
| 1018 | } |
| 1019 | info->last_jackdet = val; |
| 1020 | |
| 1021 | if (info->last_jackdet == present) { |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1022 | dev_dbg(arizona->dev, "Detected jack\n"); |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 1023 | ret = extcon_set_cable_state_(info->edev, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1024 | EXTCON_MECHANICAL, true); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1025 | |
| 1026 | if (ret != 0) |
| 1027 | dev_err(arizona->dev, "Mechanical report failed: %d\n", |
| 1028 | ret); |
| 1029 | |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 1030 | if (!arizona->pdata.hpdet_acc_id) { |
| 1031 | info->detecting = true; |
| 1032 | info->mic = false; |
| 1033 | info->jack_flips = 0; |
| 1034 | |
| 1035 | arizona_start_mic(info); |
| 1036 | } else { |
Mark Brown | df9a5ab | 2013-07-18 22:42:22 +0100 | [diff] [blame] | 1037 | queue_delayed_work(system_power_efficient_wq, |
| 1038 | &info->hpdet_work, |
| 1039 | msecs_to_jiffies(HPDET_DEBOUNCE)); |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 1040 | } |
Mark Brown | 4e61687 | 2013-01-15 22:09:20 +0900 | [diff] [blame] | 1041 | |
| 1042 | regmap_update_bits(arizona->regmap, |
| 1043 | ARIZONA_JACK_DETECT_DEBOUNCE, |
| 1044 | ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1045 | } else { |
| 1046 | dev_dbg(arizona->dev, "Detected jack removal\n"); |
| 1047 | |
| 1048 | arizona_stop_mic(info); |
| 1049 | |
Mark Brown | dd235ee | 2013-01-11 08:55:51 +0900 | [diff] [blame] | 1050 | info->num_hpdet_res = 0; |
| 1051 | for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++) |
| 1052 | info->hpdet_res[i] = 0; |
| 1053 | info->mic = false; |
Mark Brown | bf14ee5 | 2013-02-05 20:20:17 +0000 | [diff] [blame] | 1054 | info->hpdet_done = false; |
Mark Brown | 9dd5e53 | 2013-04-01 19:09:45 +0100 | [diff] [blame] | 1055 | info->hpdet_retried = false; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1056 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1057 | for (i = 0; i < info->num_micd_ranges; i++) |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1058 | input_report_key(info->input, |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1059 | info->micd_ranges[i].key, 0); |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1060 | input_sync(info->input); |
| 1061 | |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 1062 | ret = extcon_update_state(info->edev, 0xffffffff, 0); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1063 | if (ret != 0) |
| 1064 | dev_err(arizona->dev, "Removal report failed: %d\n", |
| 1065 | ret); |
Mark Brown | 4e61687 | 2013-01-15 22:09:20 +0900 | [diff] [blame] | 1066 | |
| 1067 | regmap_update_bits(arizona->regmap, |
| 1068 | ARIZONA_JACK_DETECT_DEBOUNCE, |
| 1069 | ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, |
| 1070 | ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
Mark Brown | 7abd4e2 | 2013-04-01 19:25:55 +0100 | [diff] [blame] | 1073 | if (arizona->pdata.micd_timeout) |
| 1074 | info->micd_timeout = arizona->pdata.micd_timeout; |
| 1075 | else |
| 1076 | info->micd_timeout = DEFAULT_MICD_TIMEOUT; |
| 1077 | |
Charles Keepax | cb9005d | 2013-08-07 12:17:14 +0100 | [diff] [blame] | 1078 | out: |
Charles Keepax | 5d9ab70 | 2013-02-05 10:13:38 +0000 | [diff] [blame] | 1079 | /* Clear trig_sts to make sure DCVDD is not forced up */ |
| 1080 | regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG, |
| 1081 | ARIZONA_MICD_CLAMP_FALL_TRIG_STS | |
| 1082 | ARIZONA_MICD_CLAMP_RISE_TRIG_STS | |
| 1083 | ARIZONA_JD1_FALL_TRIG_STS | |
| 1084 | ARIZONA_JD1_RISE_TRIG_STS); |
| 1085 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1086 | mutex_unlock(&info->lock); |
| 1087 | |
| 1088 | pm_runtime_mark_last_busy(info->dev); |
| 1089 | pm_runtime_put_autosuspend(info->dev); |
| 1090 | |
| 1091 | return IRQ_HANDLED; |
| 1092 | } |
| 1093 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1094 | /* Map a level onto a slot in the register bank */ |
| 1095 | static void arizona_micd_set_level(struct arizona *arizona, int index, |
| 1096 | unsigned int level) |
| 1097 | { |
| 1098 | int reg; |
| 1099 | unsigned int mask; |
| 1100 | |
| 1101 | reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2); |
| 1102 | |
| 1103 | if (!(index % 2)) { |
| 1104 | mask = 0x3f00; |
| 1105 | level <<= 8; |
| 1106 | } else { |
| 1107 | mask = 0x3f; |
| 1108 | } |
| 1109 | |
| 1110 | /* Program the level itself */ |
| 1111 | regmap_update_bits(arizona->regmap, reg, mask, level); |
| 1112 | } |
| 1113 | |
Charles Keepax | feffb0c | 2015-06-19 17:23:29 +0100 | [diff] [blame^] | 1114 | static int arizona_extcon_device_get_pdata(struct arizona *arizona) |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 1115 | { |
| 1116 | struct arizona_pdata *pdata = &arizona->pdata; |
| 1117 | unsigned int val = ARIZONA_ACCDET_MODE_HPL; |
| 1118 | |
Charles Keepax | feffb0c | 2015-06-19 17:23:29 +0100 | [diff] [blame^] | 1119 | device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val); |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 1120 | switch (val) { |
| 1121 | case ARIZONA_ACCDET_MODE_HPL: |
| 1122 | case ARIZONA_ACCDET_MODE_HPR: |
| 1123 | pdata->hpdet_channel = val; |
| 1124 | break; |
| 1125 | default: |
| 1126 | dev_err(arizona->dev, |
| 1127 | "Wrong wlf,hpdet-channel DT value %d\n", val); |
| 1128 | pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL; |
| 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 1134 | static int arizona_extcon_probe(struct platform_device *pdev) |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1135 | { |
| 1136 | struct arizona *arizona = dev_get_drvdata(pdev->dev.parent); |
Charles Keepax | 6ac6b47 | 2013-09-28 15:34:57 +0100 | [diff] [blame] | 1137 | struct arizona_pdata *pdata = &arizona->pdata; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1138 | struct arizona_extcon_info *info; |
Mark Brown | e56a0a5 | 2013-04-01 19:03:52 +0100 | [diff] [blame] | 1139 | unsigned int val; |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 1140 | unsigned int clamp_mode; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1141 | int jack_irq_fall, jack_irq_rise; |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1142 | int ret, mode, i, j; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1143 | |
Mark Brown | bbbd46e | 2013-01-10 19:38:43 +0000 | [diff] [blame] | 1144 | if (!arizona->dapm || !arizona->dapm->card) |
| 1145 | return -EPROBE_DEFER; |
| 1146 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1147 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
Jingoo Han | 0a16ee6 | 2014-07-23 10:07:09 +0900 | [diff] [blame] | 1148 | if (!info) |
Sangjung Woo | d88cc36 | 2014-04-21 19:10:15 +0900 | [diff] [blame] | 1149 | return -ENOMEM; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1150 | |
Charles Keepax | feffb0c | 2015-06-19 17:23:29 +0100 | [diff] [blame^] | 1151 | if (!dev_get_platdata(arizona->dev)) |
| 1152 | arizona_extcon_device_get_pdata(arizona); |
Inha Song | 9e86b2a | 2015-05-04 13:42:13 +0900 | [diff] [blame] | 1153 | |
Charles Keepax | 17271f6 | 2014-07-18 12:59:00 +0100 | [diff] [blame] | 1154 | info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD"); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1155 | if (IS_ERR(info->micvdd)) { |
| 1156 | ret = PTR_ERR(info->micvdd); |
| 1157 | dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret); |
Sangjung Woo | d88cc36 | 2014-04-21 19:10:15 +0900 | [diff] [blame] | 1158 | return ret; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | mutex_init(&info->lock); |
| 1162 | info->arizona = arizona; |
| 1163 | info->dev = &pdev->dev; |
Mark Brown | a3e2078 | 2013-04-01 19:05:27 +0100 | [diff] [blame] | 1164 | info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS); |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 1165 | INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work); |
Mark Brown | cd59e79 | 2013-04-01 19:21:48 +0100 | [diff] [blame] | 1166 | INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect); |
Mark Brown | 939c567 | 2013-04-01 19:17:34 +0100 | [diff] [blame] | 1167 | INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1168 | platform_set_drvdata(pdev, info); |
| 1169 | |
| 1170 | switch (arizona->type) { |
| 1171 | case WM5102: |
| 1172 | switch (arizona->rev) { |
| 1173 | case 0: |
| 1174 | info->micd_reva = true; |
| 1175 | break; |
| 1176 | default: |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1177 | info->micd_clamp = true; |
Richard Fitzgerald | d0fd5fb | 2015-04-28 13:34:27 +0100 | [diff] [blame] | 1178 | info->hpdet_ip_version = 1; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | break; |
Charles Keepax | 7743861 | 2013-11-14 16:18:25 +0000 | [diff] [blame] | 1182 | case WM5110: |
Richard Fitzgerald | 2f2b6aa | 2015-01-17 15:21:26 +0000 | [diff] [blame] | 1183 | case WM8280: |
Charles Keepax | 7743861 | 2013-11-14 16:18:25 +0000 | [diff] [blame] | 1184 | switch (arizona->rev) { |
| 1185 | case 0 ... 2: |
| 1186 | break; |
| 1187 | default: |
| 1188 | info->micd_clamp = true; |
Richard Fitzgerald | d0fd5fb | 2015-04-28 13:34:27 +0100 | [diff] [blame] | 1189 | info->hpdet_ip_version = 2; |
Charles Keepax | 7743861 | 2013-11-14 16:18:25 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | } |
| 1192 | break; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1193 | default: |
| 1194 | break; |
| 1195 | } |
| 1196 | |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 1197 | info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable); |
| 1198 | if (IS_ERR(info->edev)) { |
| 1199 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 1200 | return -ENOMEM; |
| 1201 | } |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1202 | |
Chanwoo Choi | ef70a21 | 2014-04-21 20:47:31 +0900 | [diff] [blame] | 1203 | ret = devm_extcon_dev_register(&pdev->dev, info->edev); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1204 | if (ret < 0) { |
Peter Meerwald | 8e5f501 | 2012-08-23 09:11:50 +0900 | [diff] [blame] | 1205 | dev_err(arizona->dev, "extcon_dev_register() failed: %d\n", |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1206 | ret); |
Sangjung Woo | d88cc36 | 2014-04-21 19:10:15 +0900 | [diff] [blame] | 1207 | return ret; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1210 | info->input = devm_input_allocate_device(&pdev->dev); |
| 1211 | if (!info->input) { |
| 1212 | dev_err(arizona->dev, "Can't allocate input dev\n"); |
| 1213 | ret = -ENOMEM; |
| 1214 | goto err_register; |
| 1215 | } |
| 1216 | |
| 1217 | info->input->name = "Headset"; |
| 1218 | info->input->phys = "arizona/extcon"; |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1219 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1220 | if (pdata->num_micd_configs) { |
| 1221 | info->micd_modes = pdata->micd_configs; |
| 1222 | info->micd_num_modes = pdata->num_micd_configs; |
| 1223 | } else { |
| 1224 | info->micd_modes = micd_default_modes; |
| 1225 | info->micd_num_modes = ARRAY_SIZE(micd_default_modes); |
| 1226 | } |
| 1227 | |
| 1228 | if (arizona->pdata.micd_pol_gpio > 0) { |
| 1229 | if (info->micd_modes[0].gpio) |
| 1230 | mode = GPIOF_OUT_INIT_HIGH; |
| 1231 | else |
| 1232 | mode = GPIOF_OUT_INIT_LOW; |
| 1233 | |
| 1234 | ret = devm_gpio_request_one(&pdev->dev, |
| 1235 | arizona->pdata.micd_pol_gpio, |
| 1236 | mode, |
| 1237 | "MICD polarity"); |
| 1238 | if (ret != 0) { |
| 1239 | dev_err(arizona->dev, "Failed to request GPIO%d: %d\n", |
| 1240 | arizona->pdata.micd_pol_gpio, ret); |
| 1241 | goto err_register; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Mark Brown | 1eda6aa | 2013-01-11 08:55:54 +0900 | [diff] [blame] | 1245 | if (arizona->pdata.hpdet_id_gpio > 0) { |
| 1246 | ret = devm_gpio_request_one(&pdev->dev, |
| 1247 | arizona->pdata.hpdet_id_gpio, |
| 1248 | GPIOF_OUT_INIT_LOW, |
| 1249 | "HPDET"); |
| 1250 | if (ret != 0) { |
| 1251 | dev_err(arizona->dev, "Failed to request GPIO%d: %d\n", |
| 1252 | arizona->pdata.hpdet_id_gpio, ret); |
| 1253 | goto err_register; |
| 1254 | } |
| 1255 | } |
| 1256 | |
Mark Brown | b17e546 | 2013-01-11 08:55:24 +0900 | [diff] [blame] | 1257 | if (arizona->pdata.micd_bias_start_time) |
| 1258 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 1259 | ARIZONA_MICD_BIAS_STARTTIME_MASK, |
| 1260 | arizona->pdata.micd_bias_start_time |
| 1261 | << ARIZONA_MICD_BIAS_STARTTIME_SHIFT); |
| 1262 | |
Mark Brown | 2e033db | 2013-01-21 17:36:33 +0900 | [diff] [blame] | 1263 | if (arizona->pdata.micd_rate) |
| 1264 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 1265 | ARIZONA_MICD_RATE_MASK, |
| 1266 | arizona->pdata.micd_rate |
| 1267 | << ARIZONA_MICD_RATE_SHIFT); |
| 1268 | |
| 1269 | if (arizona->pdata.micd_dbtime) |
| 1270 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, |
| 1271 | ARIZONA_MICD_DBTIME_MASK, |
| 1272 | arizona->pdata.micd_dbtime |
| 1273 | << ARIZONA_MICD_DBTIME_SHIFT); |
| 1274 | |
Mark Brown | 6fed4d8 | 2013-04-01 22:03:06 +0100 | [diff] [blame] | 1275 | BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40); |
| 1276 | |
| 1277 | if (arizona->pdata.num_micd_ranges) { |
| 1278 | info->micd_ranges = pdata->micd_ranges; |
| 1279 | info->num_micd_ranges = pdata->num_micd_ranges; |
| 1280 | } else { |
| 1281 | info->micd_ranges = micd_default_ranges; |
| 1282 | info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges); |
| 1283 | } |
| 1284 | |
| 1285 | if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) { |
| 1286 | dev_err(arizona->dev, "Too many MICD ranges: %d\n", |
| 1287 | arizona->pdata.num_micd_ranges); |
| 1288 | } |
| 1289 | |
| 1290 | if (info->num_micd_ranges > 1) { |
| 1291 | for (i = 1; i < info->num_micd_ranges; i++) { |
| 1292 | if (info->micd_ranges[i - 1].max > |
| 1293 | info->micd_ranges[i].max) { |
| 1294 | dev_err(arizona->dev, |
| 1295 | "MICD ranges must be sorted\n"); |
| 1296 | ret = -EINVAL; |
| 1297 | goto err_input; |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | /* Disable all buttons by default */ |
| 1303 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2, |
| 1304 | ARIZONA_MICD_LVL_SEL_MASK, 0x81); |
| 1305 | |
| 1306 | /* Set up all the buttons the user specified */ |
| 1307 | for (i = 0; i < info->num_micd_ranges; i++) { |
| 1308 | for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++) |
| 1309 | if (arizona_micd_levels[j] >= info->micd_ranges[i].max) |
| 1310 | break; |
| 1311 | |
| 1312 | if (j == ARRAY_SIZE(arizona_micd_levels)) { |
| 1313 | dev_err(arizona->dev, "Unsupported MICD level %d\n", |
| 1314 | info->micd_ranges[i].max); |
| 1315 | ret = -EINVAL; |
| 1316 | goto err_input; |
| 1317 | } |
| 1318 | |
| 1319 | dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n", |
| 1320 | arizona_micd_levels[j], i); |
| 1321 | |
| 1322 | arizona_micd_set_level(arizona, i, j); |
| 1323 | input_set_capability(info->input, EV_KEY, |
| 1324 | info->micd_ranges[i].key); |
| 1325 | |
| 1326 | /* Enable reporting of that range */ |
| 1327 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2, |
| 1328 | 1 << i, 1 << i); |
| 1329 | } |
| 1330 | |
| 1331 | /* Set all the remaining keys to a maximum */ |
| 1332 | for (; i < ARIZONA_MAX_MICD_RANGE; i++) |
| 1333 | arizona_micd_set_level(arizona, i, 0x3f); |
| 1334 | |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1335 | /* |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1336 | * If we have a clamp use it, activating in conjunction with |
| 1337 | * GPIO5 if that is connected for jack detect operation. |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1338 | */ |
| 1339 | if (info->micd_clamp) { |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1340 | if (arizona->pdata.jd_gpio5) { |
Mark Brown | e56a0a5 | 2013-04-01 19:03:52 +0100 | [diff] [blame] | 1341 | /* Put the GPIO into input mode with optional pull */ |
| 1342 | val = 0xc101; |
| 1343 | if (arizona->pdata.jd_gpio5_nopull) |
| 1344 | val &= ~ARIZONA_GPN_PU; |
| 1345 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1346 | regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL, |
Mark Brown | e56a0a5 | 2013-04-01 19:03:52 +0100 | [diff] [blame] | 1347 | val); |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1348 | |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 1349 | if (arizona->pdata.jd_invert) |
| 1350 | clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H; |
| 1351 | else |
| 1352 | clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1353 | } else { |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 1354 | if (arizona->pdata.jd_invert) |
| 1355 | clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH; |
| 1356 | else |
| 1357 | clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1358 | } |
| 1359 | |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1360 | regmap_update_bits(arizona->regmap, |
Richard Fitzgerald | a288d64 | 2014-05-23 12:54:57 +0100 | [diff] [blame] | 1361 | ARIZONA_MICD_CLAMP_CONTROL, |
| 1362 | ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode); |
| 1363 | |
| 1364 | regmap_update_bits(arizona->regmap, |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1365 | ARIZONA_JACK_DETECT_DEBOUNCE, |
| 1366 | ARIZONA_MICD_CLAMP_DB, |
| 1367 | ARIZONA_MICD_CLAMP_DB); |
| 1368 | } |
| 1369 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1370 | arizona_extcon_set_mode(info, 0); |
| 1371 | |
| 1372 | pm_runtime_enable(&pdev->dev); |
| 1373 | pm_runtime_idle(&pdev->dev); |
| 1374 | pm_runtime_get_sync(&pdev->dev); |
| 1375 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1376 | if (arizona->pdata.jd_gpio5) { |
| 1377 | jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE; |
| 1378 | jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL; |
| 1379 | } else { |
| 1380 | jack_irq_rise = ARIZONA_IRQ_JD_RISE; |
| 1381 | jack_irq_fall = ARIZONA_IRQ_JD_FALL; |
| 1382 | } |
| 1383 | |
| 1384 | ret = arizona_request_irq(arizona, jack_irq_rise, |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1385 | "JACKDET rise", arizona_jackdet, info); |
| 1386 | if (ret != 0) { |
| 1387 | dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n", |
| 1388 | ret); |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1389 | goto err_input; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1392 | ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1393 | if (ret != 0) { |
| 1394 | dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n", |
| 1395 | ret); |
| 1396 | goto err_rise; |
| 1397 | } |
| 1398 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1399 | ret = arizona_request_irq(arizona, jack_irq_fall, |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1400 | "JACKDET fall", arizona_jackdet, info); |
| 1401 | if (ret != 0) { |
| 1402 | dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret); |
| 1403 | goto err_rise_wake; |
| 1404 | } |
| 1405 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1406 | ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1407 | if (ret != 0) { |
| 1408 | dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n", |
| 1409 | ret); |
| 1410 | goto err_fall; |
| 1411 | } |
| 1412 | |
| 1413 | ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET, |
| 1414 | "MICDET", arizona_micdet, info); |
| 1415 | if (ret != 0) { |
| 1416 | dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret); |
| 1417 | goto err_fall_wake; |
| 1418 | } |
| 1419 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 1420 | ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET, |
| 1421 | "HPDET", arizona_hpdet_irq, info); |
| 1422 | if (ret != 0) { |
| 1423 | dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret); |
| 1424 | goto err_micdet; |
| 1425 | } |
| 1426 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1427 | arizona_clk32k_enable(arizona); |
| 1428 | regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE, |
| 1429 | ARIZONA_JD1_DB, ARIZONA_JD1_DB); |
| 1430 | regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, |
| 1431 | ARIZONA_JD1_ENA, ARIZONA_JD1_ENA); |
| 1432 | |
Mark Brown | b8575a1 | 2012-09-07 17:01:15 +0800 | [diff] [blame] | 1433 | ret = regulator_allow_bypass(info->micvdd, true); |
| 1434 | if (ret != 0) |
| 1435 | dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", |
| 1436 | ret); |
| 1437 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1438 | pm_runtime_put(&pdev->dev); |
| 1439 | |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1440 | ret = input_register_device(info->input); |
| 1441 | if (ret) { |
| 1442 | dev_err(&pdev->dev, "Can't register input device: %d\n", ret); |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 1443 | goto err_hpdet; |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1446 | return 0; |
| 1447 | |
Mark Brown | 4f34033 | 2013-01-11 08:55:43 +0900 | [diff] [blame] | 1448 | err_hpdet: |
| 1449 | arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info); |
Mark Brown | 80732cc | 2012-08-26 13:58:20 -0700 | [diff] [blame] | 1450 | err_micdet: |
| 1451 | arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1452 | err_fall_wake: |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1453 | arizona_set_irq_wake(arizona, jack_irq_fall, 0); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1454 | err_fall: |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1455 | arizona_free_irq(arizona, jack_irq_fall, info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1456 | err_rise_wake: |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1457 | arizona_set_irq_wake(arizona, jack_irq_rise, 0); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1458 | err_rise: |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1459 | arizona_free_irq(arizona, jack_irq_rise, info); |
Mark Brown | 34efe4d | 2012-07-20 17:07:29 +0100 | [diff] [blame] | 1460 | err_input: |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1461 | err_register: |
| 1462 | pm_runtime_disable(&pdev->dev); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1463 | return ret; |
| 1464 | } |
| 1465 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 1466 | static int arizona_extcon_remove(struct platform_device *pdev) |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1467 | { |
| 1468 | struct arizona_extcon_info *info = platform_get_drvdata(pdev); |
| 1469 | struct arizona *arizona = info->arizona; |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1470 | int jack_irq_rise, jack_irq_fall; |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1471 | |
| 1472 | pm_runtime_disable(&pdev->dev); |
| 1473 | |
Mark Brown | dab63eb | 2013-01-11 08:55:36 +0900 | [diff] [blame] | 1474 | regmap_update_bits(arizona->regmap, |
| 1475 | ARIZONA_MICD_CLAMP_CONTROL, |
| 1476 | ARIZONA_MICD_CLAMP_MODE_MASK, 0); |
| 1477 | |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1478 | if (arizona->pdata.jd_gpio5) { |
| 1479 | jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE; |
| 1480 | jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL; |
| 1481 | } else { |
| 1482 | jack_irq_rise = ARIZONA_IRQ_JD_RISE; |
| 1483 | jack_irq_fall = ARIZONA_IRQ_JD_FALL; |
| 1484 | } |
| 1485 | |
| 1486 | arizona_set_irq_wake(arizona, jack_irq_rise, 0); |
| 1487 | arizona_set_irq_wake(arizona, jack_irq_fall, 0); |
| 1488 | arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1489 | arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info); |
Mark Brown | 92a4987 | 2013-01-11 08:55:39 +0900 | [diff] [blame] | 1490 | arizona_free_irq(arizona, jack_irq_rise, info); |
| 1491 | arizona_free_irq(arizona, jack_irq_fall, info); |
Mark Brown | 0e27bd3 | 2013-02-05 21:00:15 +0000 | [diff] [blame] | 1492 | cancel_delayed_work_sync(&info->hpdet_work); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1493 | regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, |
| 1494 | ARIZONA_JD1_ENA, 0); |
| 1495 | arizona_clk32k_disable(arizona); |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static struct platform_driver arizona_extcon_driver = { |
| 1501 | .driver = { |
| 1502 | .name = "arizona-extcon", |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1503 | }, |
| 1504 | .probe = arizona_extcon_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 1505 | .remove = arizona_extcon_remove, |
Mark Brown | f2c32a8 | 2012-06-24 12:09:45 +0100 | [diff] [blame] | 1506 | }; |
| 1507 | |
| 1508 | module_platform_driver(arizona_extcon_driver); |
| 1509 | |
| 1510 | MODULE_DESCRIPTION("Arizona Extcon driver"); |
| 1511 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 1512 | MODULE_LICENSE("GPL"); |
| 1513 | MODULE_ALIAS("platform:extcon-arizona"); |