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