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>
26 struct regmap
*regmap
;
27 struct snd_soc_jack
*jack
;
30 unsigned int buttons_held
;
33 /* Button values to be reported on the jack */
34 static const int ts3a227e_buttons
[] = {
41 #define TS3A227E_NUM_BUTTONS 4
42 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
43 SND_JACK_MICROPHONE | \
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
67 /* TS3A227E_REG_INTERRUPT 0x01 */
68 #define INS_REM_EVENT 0x01
69 #define DETECTION_COMPLETE_EVENT 0x02
71 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
72 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
73 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
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
81 /* TS3A227E_REG_SETTING_2 0x05 */
82 #define KP_ENABLE 0x04
84 /* TS3A227E_REG_SETTING_3 0x06 */
85 #define MICBIAS_SETTING_SFT (3)
86 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
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)
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 },
114 static bool ts3a227e_readable_reg(struct device
*dev
, unsigned int reg
)
117 case TS3A227E_REG_DEVICE_ID
... TS3A227E_REG_KP_THRESHOLD_3
:
124 static bool ts3a227e_writeable_reg(struct device
*dev
, unsigned int reg
)
127 case TS3A227E_REG_INTERRUPT_DISABLE
... TS3A227E_REG_SWITCH_CONTROL_2
:
128 case TS3A227E_REG_KP_THRESHOLD_1
... TS3A227E_REG_KP_THRESHOLD_3
:
135 static bool ts3a227e_volatile_reg(struct device
*dev
, unsigned int 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
:
147 static void ts3a227e_jack_report(struct ts3a227e
*ts3a227e
)
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
];
163 snd_soc_jack_report(ts3a227e
->jack
, report
, TS3A227E_JACK_MASK
);
166 static void ts3a227e_new_jack_state(struct ts3a227e
*ts3a227e
, unsigned acc_reg
)
168 bool plugged
, mic_present
;
170 plugged
= !!(acc_reg
& JACK_INSERTED
);
171 mic_present
= plugged
&& !!(acc_reg
& EITHER_MIC_MASK
);
173 ts3a227e
->plugged
= plugged
;
175 if (mic_present
!= ts3a227e
->mic_present
) {
176 ts3a227e
->mic_present
= mic_present
;
177 ts3a227e
->buttons_held
= 0;
179 /* Enable key press detection. */
180 regmap_update_bits(ts3a227e
->regmap
,
181 TS3A227E_REG_SETTING_2
,
182 KP_ENABLE
, KP_ENABLE
);
187 static irqreturn_t
ts3a227e_interrupt(int irq
, void *data
)
189 struct ts3a227e
*ts3a227e
= (struct ts3a227e
*)data
;
190 struct regmap
*regmap
= ts3a227e
->regmap
;
191 unsigned int int_reg
, kp_int_reg
, acc_reg
, i
;
193 /* Check for plug/unplug. */
194 regmap_read(regmap
, TS3A227E_REG_INTERRUPT
, &int_reg
);
195 if (int_reg
& (DETECTION_COMPLETE_EVENT
| INS_REM_EVENT
)) {
196 regmap_read(regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
197 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
200 /* Report any key events. */
201 regmap_read(regmap
, TS3A227E_REG_KP_INTERRUPT
, &kp_int_reg
);
202 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
203 if (kp_int_reg
& PRESS_MASK(i
))
204 ts3a227e
->buttons_held
|= (1 << i
);
205 if (kp_int_reg
& RELEASE_MASK(i
))
206 ts3a227e
->buttons_held
&= ~(1 << i
);
209 ts3a227e_jack_report(ts3a227e
);
215 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
217 * @component: component to register the jack with
218 * @jack: jack to use to report headset and button events on
220 * After this function has been called the headset insert/remove and button
221 * events 0-3 will be routed to the given jack. Jack can be null to stop
224 int ts3a227e_enable_jack_detect(struct snd_soc_component
*component
,
225 struct snd_soc_jack
*jack
)
227 struct ts3a227e
*ts3a227e
= snd_soc_component_get_drvdata(component
);
229 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_0
, KEY_MEDIA
);
230 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_1
, KEY_VOICECOMMAND
);
231 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_2
, KEY_VOLUMEUP
);
232 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_3
, KEY_VOLUMEDOWN
);
234 ts3a227e
->jack
= jack
;
235 ts3a227e_jack_report(ts3a227e
);
239 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect
);
241 static struct snd_soc_component_driver ts3a227e_soc_driver
;
243 static const struct regmap_config ts3a227e_regmap_config
= {
247 .max_register
= TS3A227E_REG_KP_THRESHOLD_3
,
248 .readable_reg
= ts3a227e_readable_reg
,
249 .writeable_reg
= ts3a227e_writeable_reg
,
250 .volatile_reg
= ts3a227e_volatile_reg
,
252 .cache_type
= REGCACHE_RBTREE
,
253 .reg_defaults
= ts3a227e_reg_defaults
,
254 .num_reg_defaults
= ARRAY_SIZE(ts3a227e_reg_defaults
),
257 static int ts3a227e_parse_device_property(struct ts3a227e
*ts3a227e
,
263 err
= device_property_read_u32(dev
, "ti,micbias", &micbias
);
265 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_SETTING_3
,
266 MICBIAS_SETTING_MASK
,
267 (micbias
& 0x07) << MICBIAS_SETTING_SFT
);
273 static int ts3a227e_i2c_probe(struct i2c_client
*i2c
,
274 const struct i2c_device_id
*id
)
276 struct ts3a227e
*ts3a227e
;
277 struct device
*dev
= &i2c
->dev
;
279 unsigned int acc_reg
;
281 ts3a227e
= devm_kzalloc(&i2c
->dev
, sizeof(*ts3a227e
), GFP_KERNEL
);
282 if (ts3a227e
== NULL
)
285 i2c_set_clientdata(i2c
, ts3a227e
);
287 ts3a227e
->regmap
= devm_regmap_init_i2c(i2c
, &ts3a227e_regmap_config
);
288 if (IS_ERR(ts3a227e
->regmap
))
289 return PTR_ERR(ts3a227e
->regmap
);
291 ret
= ts3a227e_parse_device_property(ts3a227e
, dev
);
293 dev_err(dev
, "Failed to parse device property: %d\n", ret
);
297 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
, ts3a227e_interrupt
,
298 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
299 "TS3A227E", ts3a227e
);
301 dev_err(dev
, "Cannot request irq %d (%d)\n", i2c
->irq
, ret
);
305 ret
= devm_snd_soc_register_component(&i2c
->dev
, &ts3a227e_soc_driver
,
310 /* Enable interrupts except for ADC complete. */
311 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_INTERRUPT_DISABLE
,
312 INTB_DISABLE
| ADC_COMPLETE_INT_DISABLE
,
313 ADC_COMPLETE_INT_DISABLE
);
315 /* Read jack status because chip might not trigger interrupt at boot. */
316 regmap_read(ts3a227e
->regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
317 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
318 ts3a227e_jack_report(ts3a227e
);
323 static const struct i2c_device_id ts3a227e_i2c_ids
[] = {
327 MODULE_DEVICE_TABLE(i2c
, ts3a227e_i2c_ids
);
329 static const struct of_device_id ts3a227e_of_match
[] = {
330 { .compatible
= "ti,ts3a227e", },
333 MODULE_DEVICE_TABLE(of
, ts3a227e_of_match
);
335 static struct i2c_driver ts3a227e_driver
= {
338 .owner
= THIS_MODULE
,
339 .of_match_table
= of_match_ptr(ts3a227e_of_match
),
341 .probe
= ts3a227e_i2c_probe
,
342 .id_table
= ts3a227e_i2c_ids
,
344 module_i2c_driver(ts3a227e_driver
);
346 MODULE_DESCRIPTION("ASoC ts3a227e driver");
347 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
348 MODULE_LICENSE("GPL v2");