xref: /linux/sound/soc/codecs/ts3a227e.c (revision c0e297dc61f8d4453e07afbea1fa8d0e67cd4a34)
1 /*
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 
23 #include "ts3a227e.h"
24 
25 struct 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 */
34 static 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_SETTING_3 0x06 */
85 #define MICBIAS_SETTING_SFT (3)
86 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
87 
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)
94 
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 },
112 };
113 
114 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
115 {
116 	switch (reg) {
117 	case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
118 		return true;
119 	default:
120 		return false;
121 	}
122 }
123 
124 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
125 {
126 	switch (reg) {
127 	case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
128 	case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
129 		return true;
130 	default:
131 		return false;
132 	}
133 }
134 
135 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
136 {
137 	switch (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:
141 		return true;
142 	default:
143 		return false;
144 	}
145 }
146 
147 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
148 {
149 	unsigned int i;
150 	int report = 0;
151 
152 	if (!ts3a227e->jack)
153 		return;
154 
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];
162 	}
163 	snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
164 }
165 
166 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
167 {
168 	bool plugged, mic_present;
169 
170 	plugged = !!(acc_reg & JACK_INSERTED);
171 	mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
172 
173 	ts3a227e->plugged = plugged;
174 
175 	if (mic_present != ts3a227e->mic_present) {
176 		ts3a227e->mic_present = mic_present;
177 		ts3a227e->buttons_held = 0;
178 		if (mic_present) {
179 			/* Enable key press detection. */
180 			regmap_update_bits(ts3a227e->regmap,
181 					   TS3A227E_REG_SETTING_2,
182 					   KP_ENABLE, KP_ENABLE);
183 		}
184 	}
185 }
186 
187 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
188 {
189 	struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
190 	struct regmap *regmap = ts3a227e->regmap;
191 	unsigned int int_reg, kp_int_reg, acc_reg, i;
192 
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);
198 	}
199 
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);
207 	}
208 
209 	ts3a227e_jack_report(ts3a227e);
210 
211 	return IRQ_HANDLED;
212 }
213 
214 /**
215  * ts3a227e_enable_jack_detect - Specify a jack for event reporting
216  *
217  * @component:  component to register the jack with
218  * @jack: jack to use to report headset and button events on
219  *
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
222  * reporting.
223  */
224 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
225 				struct snd_soc_jack *jack)
226 {
227 	struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
228 
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);
233 
234 	ts3a227e->jack = jack;
235 	ts3a227e_jack_report(ts3a227e);
236 
237 	return 0;
238 }
239 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
240 
241 static struct snd_soc_component_driver ts3a227e_soc_driver;
242 
243 static const struct regmap_config ts3a227e_regmap_config = {
244 	.val_bits = 8,
245 	.reg_bits = 8,
246 
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,
251 
252 	.cache_type = REGCACHE_RBTREE,
253 	.reg_defaults = ts3a227e_reg_defaults,
254 	.num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
255 };
256 
257 static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
258 				struct device *dev)
259 {
260 	u32 micbias;
261 	int err;
262 
263 	err = device_property_read_u32(dev, "ti,micbias", &micbias);
264 	if (!err) {
265 		regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
266 			MICBIAS_SETTING_MASK,
267 			(micbias & 0x07) << MICBIAS_SETTING_SFT);
268 	}
269 
270 	return 0;
271 }
272 
273 static int ts3a227e_i2c_probe(struct i2c_client *i2c,
274 			      const struct i2c_device_id *id)
275 {
276 	struct ts3a227e *ts3a227e;
277 	struct device *dev = &i2c->dev;
278 	int ret;
279 	unsigned int acc_reg;
280 
281 	ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
282 	if (ts3a227e == NULL)
283 		return -ENOMEM;
284 
285 	i2c_set_clientdata(i2c, ts3a227e);
286 
287 	ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
288 	if (IS_ERR(ts3a227e->regmap))
289 		return PTR_ERR(ts3a227e->regmap);
290 
291 	ret = ts3a227e_parse_device_property(ts3a227e, dev);
292 	if (ret) {
293 		dev_err(dev, "Failed to parse device property: %d\n", ret);
294 		return ret;
295 	}
296 
297 	ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
298 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
299 					"TS3A227E", ts3a227e);
300 	if (ret) {
301 		dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
302 		return ret;
303 	}
304 
305 	ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
306 					      NULL, 0);
307 	if (ret)
308 		return ret;
309 
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);
314 
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);
319 
320 	return 0;
321 }
322 
323 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
324 	{ "ts3a227e", 0 },
325 	{ }
326 };
327 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
328 
329 static const struct of_device_id ts3a227e_of_match[] = {
330 	{ .compatible = "ti,ts3a227e", },
331 	{ }
332 };
333 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
334 
335 static struct i2c_driver ts3a227e_driver = {
336 	.driver = {
337 		.name = "ts3a227e",
338 		.owner = THIS_MODULE,
339 		.of_match_table = of_match_ptr(ts3a227e_of_match),
340 	},
341 	.probe = ts3a227e_i2c_probe,
342 	.id_table = ts3a227e_i2c_ids,
343 };
344 module_i2c_driver(ts3a227e_driver);
345 
346 MODULE_DESCRIPTION("ASoC ts3a227e driver");
347 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
348 MODULE_LICENSE("GPL v2");
349