| Dylan Reid | 2880fc8 | 2014-11-13 11:18:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * TS3A227E Autonomous Audio Accessory Detection and Configuration Switch |
| 3 | * |
| 4 | * Copyright (C) 2014 Google, Inc. |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/gpio.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/input.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of_gpio.h> |
| 17 | #include <linux/regmap.h> |
| 18 | |
| 19 | #include <sound/core.h> |
| 20 | #include <sound/jack.h> |
| 21 | #include <sound/soc.h> |
| 22 | |
| 23 | struct ts3a227e { |
| 24 | struct regmap *regmap; |
| 25 | struct snd_soc_jack *jack; |
| 26 | bool plugged; |
| 27 | bool mic_present; |
| 28 | unsigned int buttons_held; |
| 29 | }; |
| 30 | |
| 31 | /* Button values to be reported on the jack */ |
| 32 | static const int ts3a227e_buttons[] = { |
| 33 | SND_JACK_BTN_0, |
| 34 | SND_JACK_BTN_1, |
| 35 | SND_JACK_BTN_2, |
| 36 | SND_JACK_BTN_3, |
| 37 | }; |
| 38 | |
| 39 | #define TS3A227E_NUM_BUTTONS 4 |
| 40 | #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \ |
| 41 | SND_JACK_MICROPHONE | \ |
| 42 | SND_JACK_BTN_0 | \ |
| 43 | SND_JACK_BTN_1 | \ |
| 44 | SND_JACK_BTN_2 | \ |
| 45 | SND_JACK_BTN_3) |
| 46 | |
| 47 | /* TS3A227E registers */ |
| 48 | #define TS3A227E_REG_DEVICE_ID 0x00 |
| 49 | #define TS3A227E_REG_INTERRUPT 0x01 |
| 50 | #define TS3A227E_REG_KP_INTERRUPT 0x02 |
| 51 | #define TS3A227E_REG_INTERRUPT_DISABLE 0x03 |
| 52 | #define TS3A227E_REG_SETTING_1 0x04 |
| 53 | #define TS3A227E_REG_SETTING_2 0x05 |
| 54 | #define TS3A227E_REG_SETTING_3 0x06 |
| 55 | #define TS3A227E_REG_SWITCH_CONTROL_1 0x07 |
| 56 | #define TS3A227E_REG_SWITCH_CONTROL_2 0x08 |
| 57 | #define TS3A227E_REG_SWITCH_STATUS_1 0x09 |
| 58 | #define TS3A227E_REG_SWITCH_STATUS_2 0x0a |
| 59 | #define TS3A227E_REG_ACCESSORY_STATUS 0x0b |
| 60 | #define TS3A227E_REG_ADC_OUTPUT 0x0c |
| 61 | #define TS3A227E_REG_KP_THRESHOLD_1 0x0d |
| 62 | #define TS3A227E_REG_KP_THRESHOLD_2 0x0e |
| 63 | #define TS3A227E_REG_KP_THRESHOLD_3 0x0f |
| 64 | |
| 65 | /* TS3A227E_REG_INTERRUPT 0x01 */ |
| 66 | #define INS_REM_EVENT 0x01 |
| 67 | #define DETECTION_COMPLETE_EVENT 0x02 |
| 68 | |
| 69 | /* TS3A227E_REG_KP_INTERRUPT 0x02 */ |
| 70 | #define PRESS_MASK(idx) (0x01 << (2 * (idx))) |
| 71 | #define RELEASE_MASK(idx) (0x02 << (2 * (idx))) |
| 72 | |
| 73 | /* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */ |
| 74 | #define INS_REM_INT_DISABLE 0x01 |
| 75 | #define DETECTION_COMPLETE_INT_DISABLE 0x02 |
| 76 | #define ADC_COMPLETE_INT_DISABLE 0x04 |
| 77 | #define INTB_DISABLE 0x08 |
| 78 | |
| 79 | /* TS3A227E_REG_SETTING_2 0x05 */ |
| 80 | #define KP_ENABLE 0x04 |
| 81 | |
| Anatol Pomozov | 39552d7 | 2015-01-22 15:47:24 -0800 | [diff] [blame^] | 82 | /* TS3A227E_REG_SETTING_3 0x06 */ |
| 83 | #define MICBIAS_SETTING_SFT (3) |
| 84 | #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT) |
| 85 | |
| Dylan Reid | 2880fc8 | 2014-11-13 11:18:29 -0800 | [diff] [blame] | 86 | /* TS3A227E_REG_ACCESSORY_STATUS 0x0b */ |
| 87 | #define TYPE_3_POLE 0x01 |
| 88 | #define TYPE_4_POLE_OMTP 0x02 |
| 89 | #define TYPE_4_POLE_STANDARD 0x04 |
| 90 | #define JACK_INSERTED 0x08 |
| 91 | #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD) |
| 92 | |
| 93 | static const struct reg_default ts3a227e_reg_defaults[] = { |
| 94 | { TS3A227E_REG_DEVICE_ID, 0x10 }, |
| 95 | { TS3A227E_REG_INTERRUPT, 0x00 }, |
| 96 | { TS3A227E_REG_KP_INTERRUPT, 0x00 }, |
| 97 | { TS3A227E_REG_INTERRUPT_DISABLE, 0x08 }, |
| 98 | { TS3A227E_REG_SETTING_1, 0x23 }, |
| 99 | { TS3A227E_REG_SETTING_2, 0x00 }, |
| 100 | { TS3A227E_REG_SETTING_3, 0x0e }, |
| 101 | { TS3A227E_REG_SWITCH_CONTROL_1, 0x00 }, |
| 102 | { TS3A227E_REG_SWITCH_CONTROL_2, 0x00 }, |
| 103 | { TS3A227E_REG_SWITCH_STATUS_1, 0x0c }, |
| 104 | { TS3A227E_REG_SWITCH_STATUS_2, 0x00 }, |
| 105 | { TS3A227E_REG_ACCESSORY_STATUS, 0x00 }, |
| 106 | { TS3A227E_REG_ADC_OUTPUT, 0x00 }, |
| 107 | { TS3A227E_REG_KP_THRESHOLD_1, 0x20 }, |
| 108 | { TS3A227E_REG_KP_THRESHOLD_2, 0x40 }, |
| 109 | { TS3A227E_REG_KP_THRESHOLD_3, 0x68 }, |
| 110 | }; |
| 111 | |
| 112 | static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg) |
| 113 | { |
| 114 | switch (reg) { |
| 115 | case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3: |
| 116 | return true; |
| 117 | default: |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg) |
| 123 | { |
| 124 | switch (reg) { |
| 125 | case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2: |
| 126 | case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3: |
| 127 | return true; |
| 128 | default: |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg) |
| 134 | { |
| 135 | switch (reg) { |
| 136 | case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE: |
| 137 | case TS3A227E_REG_SETTING_2: |
| 138 | case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT: |
| 139 | return true; |
| 140 | default: |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void ts3a227e_jack_report(struct ts3a227e *ts3a227e) |
| 146 | { |
| 147 | unsigned int i; |
| 148 | int report = 0; |
| 149 | |
| 150 | if (!ts3a227e->jack) |
| 151 | return; |
| 152 | |
| 153 | if (ts3a227e->plugged) |
| 154 | report = SND_JACK_HEADPHONE; |
| 155 | if (ts3a227e->mic_present) |
| 156 | report |= SND_JACK_MICROPHONE; |
| 157 | for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) { |
| 158 | if (ts3a227e->buttons_held & (1 << i)) |
| 159 | report |= ts3a227e_buttons[i]; |
| 160 | } |
| 161 | snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK); |
| 162 | } |
| 163 | |
| 164 | static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg) |
| 165 | { |
| 166 | bool plugged, mic_present; |
| 167 | |
| 168 | plugged = !!(acc_reg & JACK_INSERTED); |
| 169 | mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK); |
| 170 | |
| 171 | ts3a227e->plugged = plugged; |
| 172 | |
| 173 | if (mic_present != ts3a227e->mic_present) { |
| 174 | ts3a227e->mic_present = mic_present; |
| 175 | ts3a227e->buttons_held = 0; |
| 176 | if (mic_present) { |
| 177 | /* Enable key press detection. */ |
| 178 | regmap_update_bits(ts3a227e->regmap, |
| 179 | TS3A227E_REG_SETTING_2, |
| 180 | KP_ENABLE, KP_ENABLE); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static irqreturn_t ts3a227e_interrupt(int irq, void *data) |
| 186 | { |
| 187 | struct ts3a227e *ts3a227e = (struct ts3a227e *)data; |
| 188 | struct regmap *regmap = ts3a227e->regmap; |
| 189 | unsigned int int_reg, kp_int_reg, acc_reg, i; |
| 190 | |
| 191 | /* Check for plug/unplug. */ |
| 192 | regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg); |
| 193 | if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) { |
| 194 | regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg); |
| 195 | ts3a227e_new_jack_state(ts3a227e, acc_reg); |
| 196 | } |
| 197 | |
| 198 | /* Report any key events. */ |
| 199 | regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg); |
| 200 | for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) { |
| 201 | if (kp_int_reg & PRESS_MASK(i)) |
| 202 | ts3a227e->buttons_held |= (1 << i); |
| 203 | if (kp_int_reg & RELEASE_MASK(i)) |
| 204 | ts3a227e->buttons_held &= ~(1 << i); |
| 205 | } |
| 206 | |
| 207 | ts3a227e_jack_report(ts3a227e); |
| 208 | |
| 209 | return IRQ_HANDLED; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * ts3a227e_enable_jack_detect - Specify a jack for event reporting |
| 214 | * |
| 215 | * @component: component to register the jack with |
| 216 | * @jack: jack to use to report headset and button events on |
| 217 | * |
| 218 | * After this function has been called the headset insert/remove and button |
| 219 | * events 0-3 will be routed to the given jack. Jack can be null to stop |
| 220 | * reporting. |
| 221 | */ |
| 222 | int ts3a227e_enable_jack_detect(struct snd_soc_component *component, |
| 223 | struct snd_soc_jack *jack) |
| 224 | { |
| 225 | struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component); |
| 226 | |
| 227 | snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA); |
| Anatol Pomozov | ddf9ea2 | 2015-01-22 15:47:16 -0800 | [diff] [blame] | 228 | snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); |
| 229 | snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); |
| 230 | snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); |
| Dylan Reid | 2880fc8 | 2014-11-13 11:18:29 -0800 | [diff] [blame] | 231 | |
| 232 | ts3a227e->jack = jack; |
| 233 | ts3a227e_jack_report(ts3a227e); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect); |
| 238 | |
| 239 | static struct snd_soc_component_driver ts3a227e_soc_driver; |
| 240 | |
| 241 | static const struct regmap_config ts3a227e_regmap_config = { |
| 242 | .val_bits = 8, |
| 243 | .reg_bits = 8, |
| 244 | |
| 245 | .max_register = TS3A227E_REG_KP_THRESHOLD_3, |
| 246 | .readable_reg = ts3a227e_readable_reg, |
| 247 | .writeable_reg = ts3a227e_writeable_reg, |
| 248 | .volatile_reg = ts3a227e_volatile_reg, |
| 249 | |
| 250 | .cache_type = REGCACHE_RBTREE, |
| 251 | .reg_defaults = ts3a227e_reg_defaults, |
| 252 | .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults), |
| 253 | }; |
| 254 | |
| Anatol Pomozov | 39552d7 | 2015-01-22 15:47:24 -0800 | [diff] [blame^] | 255 | static int ts3a227e_parse_dt(struct ts3a227e *ts3a227e, struct device_node *np) |
| 256 | { |
| 257 | u32 micbias; |
| 258 | int err; |
| 259 | |
| 260 | err = of_property_read_u32(np, "ti,micbias", &micbias); |
| 261 | if (!err) { |
| 262 | regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3, |
| 263 | MICBIAS_SETTING_MASK, |
| 264 | (micbias & 0x07) << MICBIAS_SETTING_SFT); |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| Dylan Reid | 2880fc8 | 2014-11-13 11:18:29 -0800 | [diff] [blame] | 270 | static int ts3a227e_i2c_probe(struct i2c_client *i2c, |
| 271 | const struct i2c_device_id *id) |
| 272 | { |
| 273 | struct ts3a227e *ts3a227e; |
| 274 | struct device *dev = &i2c->dev; |
| 275 | int ret; |
| 276 | |
| 277 | ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL); |
| 278 | if (ts3a227e == NULL) |
| 279 | return -ENOMEM; |
| 280 | |
| 281 | i2c_set_clientdata(i2c, ts3a227e); |
| 282 | |
| 283 | ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config); |
| 284 | if (IS_ERR(ts3a227e->regmap)) |
| 285 | return PTR_ERR(ts3a227e->regmap); |
| 286 | |
| Anatol Pomozov | 39552d7 | 2015-01-22 15:47:24 -0800 | [diff] [blame^] | 287 | if (dev->of_node) { |
| 288 | ret = ts3a227e_parse_dt(ts3a227e, dev->of_node); |
| 289 | if (ret) { |
| 290 | dev_err(dev, "Failed to parse device tree: %d\n", ret); |
| 291 | return ret; |
| 292 | } |
| 293 | } |
| 294 | |
| Dylan Reid | 2880fc8 | 2014-11-13 11:18:29 -0800 | [diff] [blame] | 295 | ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt, |
| 296 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 297 | "TS3A227E", ts3a227e); |
| 298 | if (ret) { |
| 299 | dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret); |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver, |
| 304 | NULL, 0); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | /* Enable interrupts except for ADC complete. */ |
| 309 | regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE, |
| 310 | INTB_DISABLE | ADC_COMPLETE_INT_DISABLE, |
| 311 | ADC_COMPLETE_INT_DISABLE); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static const struct i2c_device_id ts3a227e_i2c_ids[] = { |
| 317 | { "ts3a227e", 0 }, |
| 318 | { } |
| 319 | }; |
| 320 | MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids); |
| 321 | |
| 322 | static const struct of_device_id ts3a227e_of_match[] = { |
| 323 | { .compatible = "ti,ts3a227e", }, |
| 324 | { } |
| 325 | }; |
| 326 | MODULE_DEVICE_TABLE(of, ts3a227e_of_match); |
| 327 | |
| 328 | static struct i2c_driver ts3a227e_driver = { |
| 329 | .driver = { |
| 330 | .name = "ts3a227e", |
| 331 | .owner = THIS_MODULE, |
| 332 | .of_match_table = of_match_ptr(ts3a227e_of_match), |
| 333 | }, |
| 334 | .probe = ts3a227e_i2c_probe, |
| 335 | .id_table = ts3a227e_i2c_ids, |
| 336 | }; |
| 337 | module_i2c_driver(ts3a227e_driver); |
| 338 | |
| 339 | MODULE_DESCRIPTION("ASoC ts3a227e driver"); |
| 340 | MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>"); |
| 341 | MODULE_LICENSE("GPL v2"); |