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>
18 #include <linux/acpi.h>
20 #include <sound/core.h>
21 #include <sound/jack.h>
22 #include <sound/soc.h>
28 struct regmap
*regmap
;
29 struct snd_soc_jack
*jack
;
32 unsigned int buttons_held
;
36 /* Button values to be reported on the jack */
37 static const int ts3a227e_buttons
[] = {
44 #define TS3A227E_NUM_BUTTONS 4
45 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
46 SND_JACK_MICROPHONE | \
52 /* TS3A227E registers */
53 #define TS3A227E_REG_DEVICE_ID 0x00
54 #define TS3A227E_REG_INTERRUPT 0x01
55 #define TS3A227E_REG_KP_INTERRUPT 0x02
56 #define TS3A227E_REG_INTERRUPT_DISABLE 0x03
57 #define TS3A227E_REG_SETTING_1 0x04
58 #define TS3A227E_REG_SETTING_2 0x05
59 #define TS3A227E_REG_SETTING_3 0x06
60 #define TS3A227E_REG_SWITCH_CONTROL_1 0x07
61 #define TS3A227E_REG_SWITCH_CONTROL_2 0x08
62 #define TS3A227E_REG_SWITCH_STATUS_1 0x09
63 #define TS3A227E_REG_SWITCH_STATUS_2 0x0a
64 #define TS3A227E_REG_ACCESSORY_STATUS 0x0b
65 #define TS3A227E_REG_ADC_OUTPUT 0x0c
66 #define TS3A227E_REG_KP_THRESHOLD_1 0x0d
67 #define TS3A227E_REG_KP_THRESHOLD_2 0x0e
68 #define TS3A227E_REG_KP_THRESHOLD_3 0x0f
70 /* TS3A227E_REG_INTERRUPT 0x01 */
71 #define INS_REM_EVENT 0x01
72 #define DETECTION_COMPLETE_EVENT 0x02
74 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
75 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
76 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
78 /* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */
79 #define INS_REM_INT_DISABLE 0x01
80 #define DETECTION_COMPLETE_INT_DISABLE 0x02
81 #define ADC_COMPLETE_INT_DISABLE 0x04
82 #define INTB_DISABLE 0x08
84 /* TS3A227E_REG_SETTING_2 0x05 */
85 #define KP_ENABLE 0x04
87 /* TS3A227E_REG_SETTING_3 0x06 */
88 #define MICBIAS_SETTING_SFT (3)
89 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
91 /* TS3A227E_REG_ACCESSORY_STATUS 0x0b */
92 #define TYPE_3_POLE 0x01
93 #define TYPE_4_POLE_OMTP 0x02
94 #define TYPE_4_POLE_STANDARD 0x04
95 #define JACK_INSERTED 0x08
96 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
98 static const struct reg_default ts3a227e_reg_defaults
[] = {
99 { TS3A227E_REG_DEVICE_ID
, 0x10 },
100 { TS3A227E_REG_INTERRUPT
, 0x00 },
101 { TS3A227E_REG_KP_INTERRUPT
, 0x00 },
102 { TS3A227E_REG_INTERRUPT_DISABLE
, 0x08 },
103 { TS3A227E_REG_SETTING_1
, 0x23 },
104 { TS3A227E_REG_SETTING_2
, 0x00 },
105 { TS3A227E_REG_SETTING_3
, 0x0e },
106 { TS3A227E_REG_SWITCH_CONTROL_1
, 0x00 },
107 { TS3A227E_REG_SWITCH_CONTROL_2
, 0x00 },
108 { TS3A227E_REG_SWITCH_STATUS_1
, 0x0c },
109 { TS3A227E_REG_SWITCH_STATUS_2
, 0x00 },
110 { TS3A227E_REG_ACCESSORY_STATUS
, 0x00 },
111 { TS3A227E_REG_ADC_OUTPUT
, 0x00 },
112 { TS3A227E_REG_KP_THRESHOLD_1
, 0x20 },
113 { TS3A227E_REG_KP_THRESHOLD_2
, 0x40 },
114 { TS3A227E_REG_KP_THRESHOLD_3
, 0x68 },
117 static bool ts3a227e_readable_reg(struct device
*dev
, unsigned int reg
)
120 case TS3A227E_REG_DEVICE_ID
... TS3A227E_REG_KP_THRESHOLD_3
:
127 static bool ts3a227e_writeable_reg(struct device
*dev
, unsigned int reg
)
130 case TS3A227E_REG_INTERRUPT_DISABLE
... TS3A227E_REG_SWITCH_CONTROL_2
:
131 case TS3A227E_REG_KP_THRESHOLD_1
... TS3A227E_REG_KP_THRESHOLD_3
:
138 static bool ts3a227e_volatile_reg(struct device
*dev
, unsigned int reg
)
141 case TS3A227E_REG_INTERRUPT
... TS3A227E_REG_INTERRUPT_DISABLE
:
142 case TS3A227E_REG_SETTING_2
:
143 case TS3A227E_REG_SWITCH_STATUS_1
... TS3A227E_REG_ADC_OUTPUT
:
150 static void ts3a227e_jack_report(struct ts3a227e
*ts3a227e
)
158 if (ts3a227e
->plugged
)
159 report
= SND_JACK_HEADPHONE
;
160 if (ts3a227e
->mic_present
)
161 report
|= SND_JACK_MICROPHONE
;
162 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
163 if (ts3a227e
->buttons_held
& (1 << i
))
164 report
|= ts3a227e_buttons
[i
];
166 snd_soc_jack_report(ts3a227e
->jack
, report
, TS3A227E_JACK_MASK
);
169 static void ts3a227e_new_jack_state(struct ts3a227e
*ts3a227e
, unsigned acc_reg
)
171 bool plugged
, mic_present
;
173 plugged
= !!(acc_reg
& JACK_INSERTED
);
174 mic_present
= plugged
&& !!(acc_reg
& EITHER_MIC_MASK
);
176 ts3a227e
->plugged
= plugged
;
178 if (mic_present
!= ts3a227e
->mic_present
) {
179 ts3a227e
->mic_present
= mic_present
;
180 ts3a227e
->buttons_held
= 0;
182 /* Enable key press detection. */
183 regmap_update_bits(ts3a227e
->regmap
,
184 TS3A227E_REG_SETTING_2
,
185 KP_ENABLE
, KP_ENABLE
);
190 static irqreturn_t
ts3a227e_interrupt(int irq
, void *data
)
192 struct ts3a227e
*ts3a227e
= (struct ts3a227e
*)data
;
193 struct regmap
*regmap
= ts3a227e
->regmap
;
194 unsigned int int_reg
, kp_int_reg
, acc_reg
, i
;
195 struct device
*dev
= ts3a227e
->dev
;
198 /* Check for plug/unplug. */
199 ret
= regmap_read(regmap
, TS3A227E_REG_INTERRUPT
, &int_reg
);
201 dev_err(dev
, "failed to clear interrupt ret=%d\n", ret
);
205 if (int_reg
& (DETECTION_COMPLETE_EVENT
| INS_REM_EVENT
)) {
206 regmap_read(regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
207 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
210 /* Report any key events. */
211 ret
= regmap_read(regmap
, TS3A227E_REG_KP_INTERRUPT
, &kp_int_reg
);
213 dev_err(dev
, "failed to clear key interrupt ret=%d\n", ret
);
217 for (i
= 0; i
< TS3A227E_NUM_BUTTONS
; i
++) {
218 if (kp_int_reg
& PRESS_MASK(i
))
219 ts3a227e
->buttons_held
|= (1 << i
);
220 if (kp_int_reg
& RELEASE_MASK(i
))
221 ts3a227e
->buttons_held
&= ~(1 << i
);
224 ts3a227e_jack_report(ts3a227e
);
230 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
232 * @component: component to register the jack with
233 * @jack: jack to use to report headset and button events on
235 * After this function has been called the headset insert/remove and button
236 * events 0-3 will be routed to the given jack. Jack can be null to stop
239 int ts3a227e_enable_jack_detect(struct snd_soc_component
*component
,
240 struct snd_soc_jack
*jack
)
242 struct ts3a227e
*ts3a227e
= snd_soc_component_get_drvdata(component
);
244 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_0
, KEY_PLAYPAUSE
);
245 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_1
, KEY_VOICECOMMAND
);
246 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_2
, KEY_VOLUMEUP
);
247 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_3
, KEY_VOLUMEDOWN
);
249 ts3a227e
->jack
= jack
;
250 ts3a227e_jack_report(ts3a227e
);
254 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect
);
256 static struct snd_soc_component_driver ts3a227e_soc_driver
;
258 static const struct regmap_config ts3a227e_regmap_config
= {
262 .max_register
= TS3A227E_REG_KP_THRESHOLD_3
,
263 .readable_reg
= ts3a227e_readable_reg
,
264 .writeable_reg
= ts3a227e_writeable_reg
,
265 .volatile_reg
= ts3a227e_volatile_reg
,
267 .cache_type
= REGCACHE_RBTREE
,
268 .reg_defaults
= ts3a227e_reg_defaults
,
269 .num_reg_defaults
= ARRAY_SIZE(ts3a227e_reg_defaults
),
272 static int ts3a227e_parse_device_property(struct ts3a227e
*ts3a227e
,
278 err
= device_property_read_u32(dev
, "ti,micbias", &micbias
);
280 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_SETTING_3
,
281 MICBIAS_SETTING_MASK
,
282 (micbias
& 0x07) << MICBIAS_SETTING_SFT
);
288 static int ts3a227e_i2c_probe(struct i2c_client
*i2c
,
289 const struct i2c_device_id
*id
)
291 struct ts3a227e
*ts3a227e
;
292 struct device
*dev
= &i2c
->dev
;
294 unsigned int acc_reg
;
296 ts3a227e
= devm_kzalloc(&i2c
->dev
, sizeof(*ts3a227e
), GFP_KERNEL
);
297 if (ts3a227e
== NULL
)
300 i2c_set_clientdata(i2c
, ts3a227e
);
302 ts3a227e
->irq
= i2c
->irq
;
304 ts3a227e
->regmap
= devm_regmap_init_i2c(i2c
, &ts3a227e_regmap_config
);
305 if (IS_ERR(ts3a227e
->regmap
))
306 return PTR_ERR(ts3a227e
->regmap
);
308 ret
= ts3a227e_parse_device_property(ts3a227e
, dev
);
310 dev_err(dev
, "Failed to parse device property: %d\n", ret
);
314 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
, ts3a227e_interrupt
,
315 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
316 "TS3A227E", ts3a227e
);
318 dev_err(dev
, "Cannot request irq %d (%d)\n", i2c
->irq
, ret
);
322 ret
= devm_snd_soc_register_component(&i2c
->dev
, &ts3a227e_soc_driver
,
327 /* Enable interrupts except for ADC complete. */
328 regmap_update_bits(ts3a227e
->regmap
, TS3A227E_REG_INTERRUPT_DISABLE
,
329 INTB_DISABLE
| ADC_COMPLETE_INT_DISABLE
,
330 ADC_COMPLETE_INT_DISABLE
);
332 /* Read jack status because chip might not trigger interrupt at boot. */
333 regmap_read(ts3a227e
->regmap
, TS3A227E_REG_ACCESSORY_STATUS
, &acc_reg
);
334 ts3a227e_new_jack_state(ts3a227e
, acc_reg
);
335 ts3a227e_jack_report(ts3a227e
);
340 #ifdef CONFIG_PM_SLEEP
341 static int ts3a227e_suspend(struct device
*dev
)
343 struct ts3a227e
*ts3a227e
= dev_get_drvdata(dev
);
345 dev_dbg(ts3a227e
->dev
, "suspend disable irq\n");
346 disable_irq(ts3a227e
->irq
);
351 static int ts3a227e_resume(struct device
*dev
)
353 struct ts3a227e
*ts3a227e
= dev_get_drvdata(dev
);
355 dev_dbg(ts3a227e
->dev
, "resume enable irq\n");
356 enable_irq(ts3a227e
->irq
);
362 static const struct dev_pm_ops ts3a227e_pm
= {
363 SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend
, ts3a227e_resume
)
366 static const struct i2c_device_id ts3a227e_i2c_ids
[] = {
370 MODULE_DEVICE_TABLE(i2c
, ts3a227e_i2c_ids
);
372 static const struct of_device_id ts3a227e_of_match
[] = {
373 { .compatible
= "ti,ts3a227e", },
376 MODULE_DEVICE_TABLE(of
, ts3a227e_of_match
);
379 static struct acpi_device_id ts3a227e_acpi_match
[] = {
383 MODULE_DEVICE_TABLE(acpi
, ts3a227e_acpi_match
);
386 static struct i2c_driver ts3a227e_driver
= {
390 .of_match_table
= of_match_ptr(ts3a227e_of_match
),
391 .acpi_match_table
= ACPI_PTR(ts3a227e_acpi_match
),
393 .probe
= ts3a227e_i2c_probe
,
394 .id_table
= ts3a227e_i2c_ids
,
396 module_i2c_driver(ts3a227e_driver
);
398 MODULE_DESCRIPTION("ASoC ts3a227e driver");
399 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
400 MODULE_LICENSE("GPL v2");