2 * TS3A227E Autonomous Audio Accessory Detection and Configuration Switch
4 * Copyright (C) 2014 Google, Inc.
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.
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>
19 #include <sound/core.h>
20 #include <sound/jack.h>
21 #include <sound/soc.h>
27 struct regmap
*regmap
;
28 struct snd_soc_jack
*jack
;
31 unsigned int buttons_held
;
35 /* Button values to be reported on the jack */
36 static const int ts3a227e_buttons
[] = {
43 #define TS3A227E_NUM_BUTTONS 4
44 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
45 SND_JACK_MICROPHONE | \
51 /* TS3A227E registers */
52 #define TS3A227E_REG_DEVICE_ID 0x00
53 #define TS3A227E_REG_INTERRUPT 0x01
54 #define TS3A227E_REG_KP_INTERRUPT 0x02
55 #define TS3A227E_REG_INTERRUPT_DISABLE 0x03
56 #define TS3A227E_REG_SETTING_1 0x04
57 #define TS3A227E_REG_SETTING_2 0x05
58 #define TS3A227E_REG_SETTING_3 0x06
59 #define TS3A227E_REG_SWITCH_CONTROL_1 0x07
60 #define TS3A227E_REG_SWITCH_CONTROL_2 0x08
61 #define TS3A227E_REG_SWITCH_STATUS_1 0x09
62 #define TS3A227E_REG_SWITCH_STATUS_2 0x0a
63 #define TS3A227E_REG_ACCESSORY_STATUS 0x0b
64 #define TS3A227E_REG_ADC_OUTPUT 0x0c
65 #define TS3A227E_REG_KP_THRESHOLD_1 0x0d
66 #define TS3A227E_REG_KP_THRESHOLD_2 0x0e
67 #define TS3A227E_REG_KP_THRESHOLD_3 0x0f
69 /* TS3A227E_REG_INTERRUPT 0x01 */
70 #define INS_REM_EVENT 0x01
71 #define DETECTION_COMPLETE_EVENT 0x02
73 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
74 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
75 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
77 /* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */
78 #define INS_REM_INT_DISABLE 0x01
79 #define DETECTION_COMPLETE_INT_DISABLE 0x02
80 #define ADC_COMPLETE_INT_DISABLE 0x04
81 #define INTB_DISABLE 0x08
83 /* TS3A227E_REG_SETTING_2 0x05 */
84 #define KP_ENABLE 0x04
86 /* TS3A227E_REG_SETTING_3 0x06 */
87 #define MICBIAS_SETTING_SFT (3)
88 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
90 /* TS3A227E_REG_ACCESSORY_STATUS 0x0b */
91 #define TYPE_3_POLE 0x01
92 #define TYPE_4_POLE_OMTP 0x02
93 #define TYPE_4_POLE_STANDARD 0x04
94 #define JACK_INSERTED 0x08
95 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
97 static const struct reg_default ts3a227e_reg_defaults
[] = {
98 { TS3A227E_REG_DEVICE_ID
, 0x10 },
99 { TS3A227E_REG_INTERRUPT
, 0x00 },
100 { TS3A227E_REG_KP_INTERRUPT
, 0x00 },
101 { TS3A227E_REG_INTERRUPT_DISABLE
, 0x08 },
102 { TS3A227E_REG_SETTING_1
, 0x23 },
103 { TS3A227E_REG_SETTING_2
, 0x00 },
104 { TS3A227E_REG_SETTING_3
, 0x0e },
105 { TS3A227E_REG_SWITCH_CONTROL_1
, 0x00 },
106 { TS3A227E_REG_SWITCH_CONTROL_2
, 0x00 },
107 { TS3A227E_REG_SWITCH_STATUS_1
, 0x0c },
108 { TS3A227E_REG_SWITCH_STATUS_2
, 0x00 },
109 { TS3A227E_REG_ACCESSORY_STATUS
, 0x00 },
110 { TS3A227E_REG_ADC_OUTPUT
, 0x00 },
111 { TS3A227E_REG_KP_THRESHOLD_1
, 0x20 },
112 { TS3A227E_REG_KP_THRESHOLD_2
, 0x40 },
113 { TS3A227E_REG_KP_THRESHOLD_3
, 0x68 },
116 static bool ts3a227e_readable_reg(struct device
*dev
, unsigned int reg
)
119 case TS3A227E_REG_DEVICE_ID
... TS3A227E_REG_KP_THRESHOLD_3
:
126 static bool ts3a227e_writeable_reg(struct device
*dev
, unsigned int reg
)
129 case TS3A227E_REG_INTERRUPT_DISABLE
... TS3A227E_REG_SWITCH_CONTROL_2
:
130 case TS3A227E_REG_KP_THRESHOLD_1
... TS3A227E_REG_KP_THRESHOLD_3
:
137 static bool ts3a227e_volatile_reg(struct device
*dev
, unsigned int reg
)
140 case TS3A227E_REG_INTERRUPT
... TS3A227E_REG_INTERRUPT_DISABLE
:
141 case TS3A227E_REG_SETTING_2
:
142 case TS3A227E_REG_SWITCH_STATUS_1
... TS3A227E_REG_ADC_OUTPUT
:
149 static void ts3a227e_jack_report(struct ts3a227e
*ts3a227e
)
157 if (ts3a227e
->plugged
)
158 report
= SND_JACK_HEADPHONE
;
159 if (ts3a227e
->mic_present
)
160 report
|= SND_JACK_MICROPHONE
;
161 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
162 if (ts3a227e
->buttons_held
& (1 << i
))
163 report
|= ts3a227e_buttons
[i
];
165 snd_soc_jack_report(ts3a227e
->jack
, report
, TS3A227E_JACK_MASK
);
168 static void ts3a227e_new_jack_state(struct ts3a227e
*ts3a227e
, unsigned acc_reg
)
170 bool plugged
, mic_present
;
172 plugged
= !!(acc_reg
& JACK_INSERTED
);
173 mic_present
= plugged
&& !!(acc_reg
& EITHER_MIC_MASK
);
175 ts3a227e
->plugged
= plugged
;
177 if (mic_present
!= ts3a227e
->mic_present
) {
178 ts3a227e
->mic_present
= mic_present
;
179 ts3a227e
->buttons_held
= 0;
181 /* Enable key press detection. */
182 regmap_update_bits(ts3a227e
->regmap
,
183 TS3A227E_REG_SETTING_2
,
184 KP_ENABLE
, KP_ENABLE
);
189 static irqreturn_t
ts3a227e_interrupt(int irq
, void *data
)
191 struct ts3a227e
*ts3a227e
= (struct ts3a227e
*)data
;
192 struct regmap
*regmap
= ts3a227e
->regmap
;
193 unsigned int int_reg
, kp_int_reg
, acc_reg
, i
;
194 struct device
*dev
= ts3a227e
->dev
;
197 /* Check for plug/unplug. */
198 ret
= regmap_read(regmap
, TS3A227E_REG_INTERRUPT
, &int_reg
);
200 dev_err(dev
, "failed to clear interrupt ret=%d\n", ret
);
204 if (int_reg
& (DETECTION_COMPLETE_EVENT
| INS_REM_EVENT
)) {
205 regmap_read(regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
206 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
209 /* Report any key events. */
210 ret
= regmap_read(regmap
, TS3A227E_REG_KP_INTERRUPT
, &kp_int_reg
);
212 dev_err(dev
, "failed to clear key interrupt ret=%d\n", ret
);
216 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
217 if (kp_int_reg
& PRESS_MASK(i
))
218 ts3a227e
->buttons_held
|= (1 << i
);
219 if (kp_int_reg
& RELEASE_MASK(i
))
220 ts3a227e
->buttons_held
&= ~(1 << i
);
223 ts3a227e_jack_report(ts3a227e
);
229 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
231 * @component: component to register the jack with
232 * @jack: jack to use to report headset and button events on
234 * After this function has been called the headset insert/remove and button
235 * events 0-3 will be routed to the given jack. Jack can be null to stop
238 int ts3a227e_enable_jack_detect(struct snd_soc_component
*component
,
239 struct snd_soc_jack
*jack
)
241 struct ts3a227e
*ts3a227e
= snd_soc_component_get_drvdata(component
);
243 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_0
, KEY_MEDIA
);
244 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_1
, KEY_VOICECOMMAND
);
245 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_2
, KEY_VOLUMEUP
);
246 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_3
, KEY_VOLUMEDOWN
);
248 ts3a227e
->jack
= jack
;
249 ts3a227e_jack_report(ts3a227e
);
253 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect
);
255 static struct snd_soc_component_driver ts3a227e_soc_driver
;
257 static const struct regmap_config ts3a227e_regmap_config
= {
261 .max_register
= TS3A227E_REG_KP_THRESHOLD_3
,
262 .readable_reg
= ts3a227e_readable_reg
,
263 .writeable_reg
= ts3a227e_writeable_reg
,
264 .volatile_reg
= ts3a227e_volatile_reg
,
266 .cache_type
= REGCACHE_RBTREE
,
267 .reg_defaults
= ts3a227e_reg_defaults
,
268 .num_reg_defaults
= ARRAY_SIZE(ts3a227e_reg_defaults
),
271 static int ts3a227e_parse_device_property(struct ts3a227e
*ts3a227e
,
277 err
= device_property_read_u32(dev
, "ti,micbias", &micbias
);
279 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_SETTING_3
,
280 MICBIAS_SETTING_MASK
,
281 (micbias
& 0x07) << MICBIAS_SETTING_SFT
);
287 static int ts3a227e_i2c_probe(struct i2c_client
*i2c
,
288 const struct i2c_device_id
*id
)
290 struct ts3a227e
*ts3a227e
;
291 struct device
*dev
= &i2c
->dev
;
293 unsigned int acc_reg
;
295 ts3a227e
= devm_kzalloc(&i2c
->dev
, sizeof(*ts3a227e
), GFP_KERNEL
);
296 if (ts3a227e
== NULL
)
299 i2c_set_clientdata(i2c
, ts3a227e
);
301 ts3a227e
->irq
= i2c
->irq
;
303 ts3a227e
->regmap
= devm_regmap_init_i2c(i2c
, &ts3a227e_regmap_config
);
304 if (IS_ERR(ts3a227e
->regmap
))
305 return PTR_ERR(ts3a227e
->regmap
);
307 ret
= ts3a227e_parse_device_property(ts3a227e
, dev
);
309 dev_err(dev
, "Failed to parse device property: %d\n", ret
);
313 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
, ts3a227e_interrupt
,
314 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
315 "TS3A227E", ts3a227e
);
317 dev_err(dev
, "Cannot request irq %d (%d)\n", i2c
->irq
, ret
);
321 ret
= devm_snd_soc_register_component(&i2c
->dev
, &ts3a227e_soc_driver
,
326 /* Enable interrupts except for ADC complete. */
327 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_INTERRUPT_DISABLE
,
328 INTB_DISABLE
| ADC_COMPLETE_INT_DISABLE
,
329 ADC_COMPLETE_INT_DISABLE
);
331 /* Read jack status because chip might not trigger interrupt at boot. */
332 regmap_read(ts3a227e
->regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
333 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
334 ts3a227e_jack_report(ts3a227e
);
339 #ifdef CONFIG_PM_SLEEP
340 static int ts3a227e_suspend(struct device
*dev
)
342 struct ts3a227e
*ts3a227e
= dev_get_drvdata(dev
);
344 dev_dbg(ts3a227e
->dev
, "suspend disable irq\n");
345 disable_irq(ts3a227e
->irq
);
350 static int ts3a227e_resume(struct device
*dev
)
352 struct ts3a227e
*ts3a227e
= dev_get_drvdata(dev
);
354 dev_dbg(ts3a227e
->dev
, "resume enable irq\n");
355 enable_irq(ts3a227e
->irq
);
361 static const struct dev_pm_ops ts3a227e_pm
= {
362 SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend
, ts3a227e_resume
)
365 static const struct i2c_device_id ts3a227e_i2c_ids
[] = {
369 MODULE_DEVICE_TABLE(i2c
, ts3a227e_i2c_ids
);
371 static const struct of_device_id ts3a227e_of_match
[] = {
372 { .compatible
= "ti,ts3a227e", },
375 MODULE_DEVICE_TABLE(of
, ts3a227e_of_match
);
377 static struct i2c_driver ts3a227e_driver
= {
381 .of_match_table
= of_match_ptr(ts3a227e_of_match
),
383 .probe
= ts3a227e_i2c_probe
,
384 .id_table
= ts3a227e_i2c_ids
,
386 module_i2c_driver(ts3a227e_driver
);
388 MODULE_DESCRIPTION("ASoC ts3a227e driver");
389 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
390 MODULE_LICENSE("GPL v2");