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