blob: 6528bfe5c2ff781ae7adce3bc711882c07fd7643 [file] [log] [blame]
Dylan Reid2880fc82014-11-13 11:18:29 -08001/*
2 * TS3A227E Autonomous Audio Accessory Detection and Configuration Switch
3 *
4 * Copyright (C) 2014 Google, Inc.
5 *
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.
9 */
10
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
19#include <sound/core.h>
20#include <sound/jack.h>
21#include <sound/soc.h>
22
Lad, Prabhakare7a03322015-02-04 17:29:30 +000023#include "ts3a227e.h"
24
Dylan Reid2880fc82014-11-13 11:18:29 -080025struct ts3a227e {
26 struct regmap *regmap;
27 struct snd_soc_jack *jack;
28 bool plugged;
29 bool mic_present;
30 unsigned int buttons_held;
31};
32
33/* Button values to be reported on the jack */
34static 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_ACCESSORY_STATUS 0x0b */
85#define TYPE_3_POLE 0x01
86#define TYPE_4_POLE_OMTP 0x02
87#define TYPE_4_POLE_STANDARD 0x04
88#define JACK_INSERTED 0x08
89#define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
90
91static const struct reg_default ts3a227e_reg_defaults[] = {
92 { TS3A227E_REG_DEVICE_ID, 0x10 },
93 { TS3A227E_REG_INTERRUPT, 0x00 },
94 { TS3A227E_REG_KP_INTERRUPT, 0x00 },
95 { TS3A227E_REG_INTERRUPT_DISABLE, 0x08 },
96 { TS3A227E_REG_SETTING_1, 0x23 },
97 { TS3A227E_REG_SETTING_2, 0x00 },
98 { TS3A227E_REG_SETTING_3, 0x0e },
99 { TS3A227E_REG_SWITCH_CONTROL_1, 0x00 },
100 { TS3A227E_REG_SWITCH_CONTROL_2, 0x00 },
101 { TS3A227E_REG_SWITCH_STATUS_1, 0x0c },
102 { TS3A227E_REG_SWITCH_STATUS_2, 0x00 },
103 { TS3A227E_REG_ACCESSORY_STATUS, 0x00 },
104 { TS3A227E_REG_ADC_OUTPUT, 0x00 },
105 { TS3A227E_REG_KP_THRESHOLD_1, 0x20 },
106 { TS3A227E_REG_KP_THRESHOLD_2, 0x40 },
107 { TS3A227E_REG_KP_THRESHOLD_3, 0x68 },
108};
109
110static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
111{
112 switch (reg) {
113 case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
114 return true;
115 default:
116 return false;
117 }
118}
119
120static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
121{
122 switch (reg) {
123 case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
124 case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
125 return true;
126 default:
127 return false;
128 }
129}
130
131static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
132{
133 switch (reg) {
134 case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE:
135 case TS3A227E_REG_SETTING_2:
136 case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT:
137 return true;
138 default:
139 return false;
140 }
141}
142
143static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
144{
145 unsigned int i;
146 int report = 0;
147
148 if (!ts3a227e->jack)
149 return;
150
151 if (ts3a227e->plugged)
152 report = SND_JACK_HEADPHONE;
153 if (ts3a227e->mic_present)
154 report |= SND_JACK_MICROPHONE;
155 for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
156 if (ts3a227e->buttons_held & (1 << i))
157 report |= ts3a227e_buttons[i];
158 }
159 snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
160}
161
162static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
163{
164 bool plugged, mic_present;
165
166 plugged = !!(acc_reg & JACK_INSERTED);
167 mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
168
169 ts3a227e->plugged = plugged;
170
171 if (mic_present != ts3a227e->mic_present) {
172 ts3a227e->mic_present = mic_present;
173 ts3a227e->buttons_held = 0;
174 if (mic_present) {
175 /* Enable key press detection. */
176 regmap_update_bits(ts3a227e->regmap,
177 TS3A227E_REG_SETTING_2,
178 KP_ENABLE, KP_ENABLE);
179 }
180 }
181}
182
183static irqreturn_t ts3a227e_interrupt(int irq, void *data)
184{
185 struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
186 struct regmap *regmap = ts3a227e->regmap;
187 unsigned int int_reg, kp_int_reg, acc_reg, i;
188
189 /* Check for plug/unplug. */
190 regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
191 if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
192 regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
193 ts3a227e_new_jack_state(ts3a227e, acc_reg);
194 }
195
196 /* Report any key events. */
197 regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
198 for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
199 if (kp_int_reg & PRESS_MASK(i))
200 ts3a227e->buttons_held |= (1 << i);
201 if (kp_int_reg & RELEASE_MASK(i))
202 ts3a227e->buttons_held &= ~(1 << i);
203 }
204
205 ts3a227e_jack_report(ts3a227e);
206
207 return IRQ_HANDLED;
208}
209
210/**
211 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
212 *
213 * @component: component to register the jack with
214 * @jack: jack to use to report headset and button events on
215 *
216 * After this function has been called the headset insert/remove and button
217 * events 0-3 will be routed to the given jack. Jack can be null to stop
218 * reporting.
219 */
220int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
221 struct snd_soc_jack *jack)
222{
223 struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
224
225 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
226 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
227 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
228 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
229
230 ts3a227e->jack = jack;
231 ts3a227e_jack_report(ts3a227e);
232
233 return 0;
234}
235EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
236
237static struct snd_soc_component_driver ts3a227e_soc_driver;
238
239static const struct regmap_config ts3a227e_regmap_config = {
240 .val_bits = 8,
241 .reg_bits = 8,
242
243 .max_register = TS3A227E_REG_KP_THRESHOLD_3,
244 .readable_reg = ts3a227e_readable_reg,
245 .writeable_reg = ts3a227e_writeable_reg,
246 .volatile_reg = ts3a227e_volatile_reg,
247
248 .cache_type = REGCACHE_RBTREE,
249 .reg_defaults = ts3a227e_reg_defaults,
250 .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
251};
252
253static int ts3a227e_i2c_probe(struct i2c_client *i2c,
254 const struct i2c_device_id *id)
255{
256 struct ts3a227e *ts3a227e;
257 struct device *dev = &i2c->dev;
258 int ret;
259
260 ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
261 if (ts3a227e == NULL)
262 return -ENOMEM;
263
264 i2c_set_clientdata(i2c, ts3a227e);
265
266 ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
267 if (IS_ERR(ts3a227e->regmap))
268 return PTR_ERR(ts3a227e->regmap);
269
270 ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
271 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
272 "TS3A227E", ts3a227e);
273 if (ret) {
274 dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
275 return ret;
276 }
277
278 ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
279 NULL, 0);
280 if (ret)
281 return ret;
282
283 /* Enable interrupts except for ADC complete. */
284 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
285 INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
286 ADC_COMPLETE_INT_DISABLE);
287
288 return 0;
289}
290
291static const struct i2c_device_id ts3a227e_i2c_ids[] = {
292 { "ts3a227e", 0 },
293 { }
294};
295MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
296
297static const struct of_device_id ts3a227e_of_match[] = {
298 { .compatible = "ti,ts3a227e", },
299 { }
300};
301MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
302
303static struct i2c_driver ts3a227e_driver = {
304 .driver = {
305 .name = "ts3a227e",
306 .owner = THIS_MODULE,
307 .of_match_table = of_match_ptr(ts3a227e_of_match),
308 },
309 .probe = ts3a227e_i2c_probe,
310 .id_table = ts3a227e_i2c_ids,
311};
312module_i2c_driver(ts3a227e_driver);
313
314MODULE_DESCRIPTION("ASoC ts3a227e driver");
315MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
316MODULE_LICENSE("GPL v2");