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>
24 struct regmap
*regmap
;
25 struct snd_soc_jack
*jack
;
28 unsigned int buttons_held
;
31 /* Button values to be reported on the jack */
32 static const int ts3a227e_buttons
[] = {
39 #define TS3A227E_NUM_BUTTONS 4
40 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
41 SND_JACK_MICROPHONE | \
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
65 /* TS3A227E_REG_INTERRUPT 0x01 */
66 #define INS_REM_EVENT 0x01
67 #define DETECTION_COMPLETE_EVENT 0x02
69 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
70 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
71 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
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
79 /* TS3A227E_REG_SETTING_2 0x05 */
80 #define KP_ENABLE 0x04
82 /* TS3A227E_REG_ACCESSORY_STATUS 0x0b */
83 #define TYPE_3_POLE 0x01
84 #define TYPE_4_POLE_OMTP 0x02
85 #define TYPE_4_POLE_STANDARD 0x04
86 #define JACK_INSERTED 0x08
87 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
89 static const struct reg_default ts3a227e_reg_defaults
[] = {
90 { TS3A227E_REG_DEVICE_ID
, 0x10 },
91 { TS3A227E_REG_INTERRUPT
, 0x00 },
92 { TS3A227E_REG_KP_INTERRUPT
, 0x00 },
93 { TS3A227E_REG_INTERRUPT_DISABLE
, 0x08 },
94 { TS3A227E_REG_SETTING_1
, 0x23 },
95 { TS3A227E_REG_SETTING_2
, 0x00 },
96 { TS3A227E_REG_SETTING_3
, 0x0e },
97 { TS3A227E_REG_SWITCH_CONTROL_1
, 0x00 },
98 { TS3A227E_REG_SWITCH_CONTROL_2
, 0x00 },
99 { TS3A227E_REG_SWITCH_STATUS_1
, 0x0c },
100 { TS3A227E_REG_SWITCH_STATUS_2
, 0x00 },
101 { TS3A227E_REG_ACCESSORY_STATUS
, 0x00 },
102 { TS3A227E_REG_ADC_OUTPUT
, 0x00 },
103 { TS3A227E_REG_KP_THRESHOLD_1
, 0x20 },
104 { TS3A227E_REG_KP_THRESHOLD_2
, 0x40 },
105 { TS3A227E_REG_KP_THRESHOLD_3
, 0x68 },
108 static bool ts3a227e_readable_reg(struct device
*dev
, unsigned int reg
)
111 case TS3A227E_REG_DEVICE_ID
... TS3A227E_REG_KP_THRESHOLD_3
:
118 static bool ts3a227e_writeable_reg(struct device
*dev
, unsigned int reg
)
121 case TS3A227E_REG_INTERRUPT_DISABLE
... TS3A227E_REG_SWITCH_CONTROL_2
:
122 case TS3A227E_REG_KP_THRESHOLD_1
... TS3A227E_REG_KP_THRESHOLD_3
:
129 static bool ts3a227e_volatile_reg(struct device
*dev
, unsigned int reg
)
132 case TS3A227E_REG_INTERRUPT
... TS3A227E_REG_INTERRUPT_DISABLE
:
133 case TS3A227E_REG_SETTING_2
:
134 case TS3A227E_REG_SWITCH_STATUS_1
... TS3A227E_REG_ADC_OUTPUT
:
141 static void ts3a227e_jack_report(struct ts3a227e
*ts3a227e
)
149 if (ts3a227e
->plugged
)
150 report
= SND_JACK_HEADPHONE
;
151 if (ts3a227e
->mic_present
)
152 report
|= SND_JACK_MICROPHONE
;
153 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
154 if (ts3a227e
->buttons_held
& (1 << i
))
155 report
|= ts3a227e_buttons
[i
];
157 snd_soc_jack_report(ts3a227e
->jack
, report
, TS3A227E_JACK_MASK
);
160 static void ts3a227e_new_jack_state(struct ts3a227e
*ts3a227e
, unsigned acc_reg
)
162 bool plugged
, mic_present
;
164 plugged
= !!(acc_reg
& JACK_INSERTED
);
165 mic_present
= plugged
&& !!(acc_reg
& EITHER_MIC_MASK
);
167 ts3a227e
->plugged
= plugged
;
169 if (mic_present
!= ts3a227e
->mic_present
) {
170 ts3a227e
->mic_present
= mic_present
;
171 ts3a227e
->buttons_held
= 0;
173 /* Enable key press detection. */
174 regmap_update_bits(ts3a227e
->regmap
,
175 TS3A227E_REG_SETTING_2
,
176 KP_ENABLE
, KP_ENABLE
);
181 static irqreturn_t
ts3a227e_interrupt(int irq
, void *data
)
183 struct ts3a227e
*ts3a227e
= (struct ts3a227e
*)data
;
184 struct regmap
*regmap
= ts3a227e
->regmap
;
185 unsigned int int_reg
, kp_int_reg
, acc_reg
, i
;
187 /* Check for plug/unplug. */
188 regmap_read(regmap
, TS3A227E_REG_INTERRUPT
, &int_reg
);
189 if (int_reg
& (DETECTION_COMPLETE_EVENT
| INS_REM_EVENT
)) {
190 regmap_read(regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
191 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
194 /* Report any key events. */
195 regmap_read(regmap
, TS3A227E_REG_KP_INTERRUPT
, &kp_int_reg
);
196 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
197 if (kp_int_reg
& PRESS_MASK(i
))
198 ts3a227e
->buttons_held
|= (1 << i
);
199 if (kp_int_reg
& RELEASE_MASK(i
))
200 ts3a227e
->buttons_held
&= ~(1 << i
);
203 ts3a227e_jack_report(ts3a227e
);
209 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
211 * @component: component to register the jack with
212 * @jack: jack to use to report headset and button events on
214 * After this function has been called the headset insert/remove and button
215 * events 0-3 will be routed to the given jack. Jack can be null to stop
218 int ts3a227e_enable_jack_detect(struct snd_soc_component
*component
,
219 struct snd_soc_jack
*jack
)
221 struct ts3a227e
*ts3a227e
= snd_soc_component_get_drvdata(component
);
223 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_0
, KEY_MEDIA
);
224 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_1
, KEY_VOLUMEUP
);
225 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_2
, KEY_VOLUMEDOWN
);
226 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_3
, KEY_VOICECOMMAND
);
228 ts3a227e
->jack
= jack
;
229 ts3a227e_jack_report(ts3a227e
);
233 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect
);
235 static struct snd_soc_component_driver ts3a227e_soc_driver
;
237 static const struct regmap_config ts3a227e_regmap_config
= {
241 .max_register
= TS3A227E_REG_KP_THRESHOLD_3
,
242 .readable_reg
= ts3a227e_readable_reg
,
243 .writeable_reg
= ts3a227e_writeable_reg
,
244 .volatile_reg
= ts3a227e_volatile_reg
,
246 .cache_type
= REGCACHE_RBTREE
,
247 .reg_defaults
= ts3a227e_reg_defaults
,
248 .num_reg_defaults
= ARRAY_SIZE(ts3a227e_reg_defaults
),
251 static int ts3a227e_i2c_probe(struct i2c_client
*i2c
,
252 const struct i2c_device_id
*id
)
254 struct ts3a227e
*ts3a227e
;
255 struct device
*dev
= &i2c
->dev
;
258 ts3a227e
= devm_kzalloc(&i2c
->dev
, sizeof(*ts3a227e
), GFP_KERNEL
);
259 if (ts3a227e
== NULL
)
262 i2c_set_clientdata(i2c
, ts3a227e
);
264 ts3a227e
->regmap
= devm_regmap_init_i2c(i2c
, &ts3a227e_regmap_config
);
265 if (IS_ERR(ts3a227e
->regmap
))
266 return PTR_ERR(ts3a227e
->regmap
);
268 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
, ts3a227e_interrupt
,
269 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
270 "TS3A227E", ts3a227e
);
272 dev_err(dev
, "Cannot request irq %d (%d)\n", i2c
->irq
, ret
);
276 ret
= devm_snd_soc_register_component(&i2c
->dev
, &ts3a227e_soc_driver
,
281 /* Enable interrupts except for ADC complete. */
282 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_INTERRUPT_DISABLE
,
283 INTB_DISABLE
| ADC_COMPLETE_INT_DISABLE
,
284 ADC_COMPLETE_INT_DISABLE
);
289 static const struct i2c_device_id ts3a227e_i2c_ids
[] = {
293 MODULE_DEVICE_TABLE(i2c
, ts3a227e_i2c_ids
);
295 static const struct of_device_id ts3a227e_of_match
[] = {
296 { .compatible
= "ti,ts3a227e", },
299 MODULE_DEVICE_TABLE(of
, ts3a227e_of_match
);
301 static struct i2c_driver ts3a227e_driver
= {
304 .owner
= THIS_MODULE
,
305 .of_match_table
= of_match_ptr(ts3a227e_of_match
),
307 .probe
= ts3a227e_i2c_probe
,
308 .id_table
= ts3a227e_i2c_ids
,
310 module_i2c_driver(ts3a227e_driver
);
312 MODULE_DESCRIPTION("ASoC ts3a227e driver");
313 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
314 MODULE_LICENSE("GPL v2");