xref: /openbmc/linux/sound/soc/codecs/ts3a227e.c (revision 9cfc5c90)
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 device *dev;
27 	struct regmap *regmap;
28 	struct snd_soc_jack *jack;
29 	bool plugged;
30 	bool mic_present;
31 	unsigned int buttons_held;
32 	int irq;
33 };
34 
35 /* Button values to be reported on the jack */
36 static const int ts3a227e_buttons[] = {
37 	SND_JACK_BTN_0,
38 	SND_JACK_BTN_1,
39 	SND_JACK_BTN_2,
40 	SND_JACK_BTN_3,
41 };
42 
43 #define TS3A227E_NUM_BUTTONS 4
44 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
45 			    SND_JACK_MICROPHONE | \
46 			    SND_JACK_BTN_0 | \
47 			    SND_JACK_BTN_1 | \
48 			    SND_JACK_BTN_2 | \
49 			    SND_JACK_BTN_3)
50 
51 /* TS3A227E registers */
52 #define TS3A227E_REG_DEVICE_ID		0x00
53 #define TS3A227E_REG_INTERRUPT		0x01
54 #define TS3A227E_REG_KP_INTERRUPT	0x02
55 #define TS3A227E_REG_INTERRUPT_DISABLE	0x03
56 #define TS3A227E_REG_SETTING_1		0x04
57 #define TS3A227E_REG_SETTING_2		0x05
58 #define TS3A227E_REG_SETTING_3		0x06
59 #define TS3A227E_REG_SWITCH_CONTROL_1	0x07
60 #define TS3A227E_REG_SWITCH_CONTROL_2	0x08
61 #define TS3A227E_REG_SWITCH_STATUS_1	0x09
62 #define TS3A227E_REG_SWITCH_STATUS_2	0x0a
63 #define TS3A227E_REG_ACCESSORY_STATUS	0x0b
64 #define TS3A227E_REG_ADC_OUTPUT		0x0c
65 #define TS3A227E_REG_KP_THRESHOLD_1	0x0d
66 #define TS3A227E_REG_KP_THRESHOLD_2	0x0e
67 #define TS3A227E_REG_KP_THRESHOLD_3	0x0f
68 
69 /* TS3A227E_REG_INTERRUPT 0x01 */
70 #define INS_REM_EVENT 0x01
71 #define DETECTION_COMPLETE_EVENT 0x02
72 
73 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
74 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
75 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
76 
77 /* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */
78 #define INS_REM_INT_DISABLE 0x01
79 #define DETECTION_COMPLETE_INT_DISABLE 0x02
80 #define ADC_COMPLETE_INT_DISABLE 0x04
81 #define INTB_DISABLE 0x08
82 
83 /* TS3A227E_REG_SETTING_2 0x05 */
84 #define KP_ENABLE 0x04
85 
86 /* TS3A227E_REG_SETTING_3 0x06 */
87 #define MICBIAS_SETTING_SFT (3)
88 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
89 
90 /* TS3A227E_REG_ACCESSORY_STATUS  0x0b */
91 #define TYPE_3_POLE 0x01
92 #define TYPE_4_POLE_OMTP 0x02
93 #define TYPE_4_POLE_STANDARD 0x04
94 #define JACK_INSERTED 0x08
95 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
96 
97 static const struct reg_default ts3a227e_reg_defaults[] = {
98 	{ TS3A227E_REG_DEVICE_ID, 0x10 },
99 	{ TS3A227E_REG_INTERRUPT, 0x00 },
100 	{ TS3A227E_REG_KP_INTERRUPT, 0x00 },
101 	{ TS3A227E_REG_INTERRUPT_DISABLE, 0x08 },
102 	{ TS3A227E_REG_SETTING_1, 0x23 },
103 	{ TS3A227E_REG_SETTING_2, 0x00 },
104 	{ TS3A227E_REG_SETTING_3, 0x0e },
105 	{ TS3A227E_REG_SWITCH_CONTROL_1, 0x00 },
106 	{ TS3A227E_REG_SWITCH_CONTROL_2, 0x00 },
107 	{ TS3A227E_REG_SWITCH_STATUS_1, 0x0c },
108 	{ TS3A227E_REG_SWITCH_STATUS_2, 0x00 },
109 	{ TS3A227E_REG_ACCESSORY_STATUS, 0x00 },
110 	{ TS3A227E_REG_ADC_OUTPUT, 0x00 },
111 	{ TS3A227E_REG_KP_THRESHOLD_1, 0x20 },
112 	{ TS3A227E_REG_KP_THRESHOLD_2, 0x40 },
113 	{ TS3A227E_REG_KP_THRESHOLD_3, 0x68 },
114 };
115 
116 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
117 {
118 	switch (reg) {
119 	case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
120 		return true;
121 	default:
122 		return false;
123 	}
124 }
125 
126 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
127 {
128 	switch (reg) {
129 	case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
130 	case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
131 		return true;
132 	default:
133 		return false;
134 	}
135 }
136 
137 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
138 {
139 	switch (reg) {
140 	case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE:
141 	case TS3A227E_REG_SETTING_2:
142 	case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT:
143 		return true;
144 	default:
145 		return false;
146 	}
147 }
148 
149 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
150 {
151 	unsigned int i;
152 	int report = 0;
153 
154 	if (!ts3a227e->jack)
155 		return;
156 
157 	if (ts3a227e->plugged)
158 		report = SND_JACK_HEADPHONE;
159 	if (ts3a227e->mic_present)
160 		report |= SND_JACK_MICROPHONE;
161 	for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
162 		if (ts3a227e->buttons_held & (1 << i))
163 			report |= ts3a227e_buttons[i];
164 	}
165 	snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
166 }
167 
168 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
169 {
170 	bool plugged, mic_present;
171 
172 	plugged = !!(acc_reg & JACK_INSERTED);
173 	mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
174 
175 	ts3a227e->plugged = plugged;
176 
177 	if (mic_present != ts3a227e->mic_present) {
178 		ts3a227e->mic_present = mic_present;
179 		ts3a227e->buttons_held = 0;
180 		if (mic_present) {
181 			/* Enable key press detection. */
182 			regmap_update_bits(ts3a227e->regmap,
183 					   TS3A227E_REG_SETTING_2,
184 					   KP_ENABLE, KP_ENABLE);
185 		}
186 	}
187 }
188 
189 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
190 {
191 	struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
192 	struct regmap *regmap = ts3a227e->regmap;
193 	unsigned int int_reg, kp_int_reg, acc_reg, i;
194 	struct device *dev = ts3a227e->dev;
195 	int ret;
196 
197 	/* Check for plug/unplug. */
198 	ret = regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
199 	if (ret) {
200 		dev_err(dev, "failed to clear interrupt ret=%d\n", ret);
201 		return IRQ_NONE;
202 	}
203 
204 	if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
205 		regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
206 		ts3a227e_new_jack_state(ts3a227e, acc_reg);
207 	}
208 
209 	/* Report any key events. */
210 	ret = regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
211 	if (ret) {
212 		dev_err(dev, "failed to clear key interrupt ret=%d\n", ret);
213 		return IRQ_NONE;
214 	}
215 
216 	for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
217 		if (kp_int_reg & PRESS_MASK(i))
218 			ts3a227e->buttons_held |= (1 << i);
219 		if (kp_int_reg & RELEASE_MASK(i))
220 			ts3a227e->buttons_held &= ~(1 << i);
221 	}
222 
223 	ts3a227e_jack_report(ts3a227e);
224 
225 	return IRQ_HANDLED;
226 }
227 
228 /**
229  * ts3a227e_enable_jack_detect - Specify a jack for event reporting
230  *
231  * @component:  component to register the jack with
232  * @jack: jack to use to report headset and button events on
233  *
234  * After this function has been called the headset insert/remove and button
235  * events 0-3 will be routed to the given jack.  Jack can be null to stop
236  * reporting.
237  */
238 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
239 				struct snd_soc_jack *jack)
240 {
241 	struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
242 
243 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
244 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
245 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
246 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
247 
248 	ts3a227e->jack = jack;
249 	ts3a227e_jack_report(ts3a227e);
250 
251 	return 0;
252 }
253 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
254 
255 static struct snd_soc_component_driver ts3a227e_soc_driver;
256 
257 static const struct regmap_config ts3a227e_regmap_config = {
258 	.val_bits = 8,
259 	.reg_bits = 8,
260 
261 	.max_register = TS3A227E_REG_KP_THRESHOLD_3,
262 	.readable_reg = ts3a227e_readable_reg,
263 	.writeable_reg = ts3a227e_writeable_reg,
264 	.volatile_reg = ts3a227e_volatile_reg,
265 
266 	.cache_type = REGCACHE_RBTREE,
267 	.reg_defaults = ts3a227e_reg_defaults,
268 	.num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
269 };
270 
271 static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
272 				struct device *dev)
273 {
274 	u32 micbias;
275 	int err;
276 
277 	err = device_property_read_u32(dev, "ti,micbias", &micbias);
278 	if (!err) {
279 		regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
280 			MICBIAS_SETTING_MASK,
281 			(micbias & 0x07) << MICBIAS_SETTING_SFT);
282 	}
283 
284 	return 0;
285 }
286 
287 static int ts3a227e_i2c_probe(struct i2c_client *i2c,
288 			      const struct i2c_device_id *id)
289 {
290 	struct ts3a227e *ts3a227e;
291 	struct device *dev = &i2c->dev;
292 	int ret;
293 	unsigned int acc_reg;
294 
295 	ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
296 	if (ts3a227e == NULL)
297 		return -ENOMEM;
298 
299 	i2c_set_clientdata(i2c, ts3a227e);
300 	ts3a227e->dev = dev;
301 	ts3a227e->irq = i2c->irq;
302 
303 	ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
304 	if (IS_ERR(ts3a227e->regmap))
305 		return PTR_ERR(ts3a227e->regmap);
306 
307 	ret = ts3a227e_parse_device_property(ts3a227e, dev);
308 	if (ret) {
309 		dev_err(dev, "Failed to parse device property: %d\n", ret);
310 		return ret;
311 	}
312 
313 	ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
314 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
315 					"TS3A227E", ts3a227e);
316 	if (ret) {
317 		dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
318 		return ret;
319 	}
320 
321 	ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
322 					      NULL, 0);
323 	if (ret)
324 		return ret;
325 
326 	/* Enable interrupts except for ADC complete. */
327 	regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
328 			   INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
329 			   ADC_COMPLETE_INT_DISABLE);
330 
331 	/* Read jack status because chip might not trigger interrupt at boot. */
332 	regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
333 	ts3a227e_new_jack_state(ts3a227e, acc_reg);
334 	ts3a227e_jack_report(ts3a227e);
335 
336 	return 0;
337 }
338 
339 #ifdef CONFIG_PM_SLEEP
340 static int ts3a227e_suspend(struct device *dev)
341 {
342 	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
343 
344 	dev_dbg(ts3a227e->dev, "suspend disable irq\n");
345 	disable_irq(ts3a227e->irq);
346 
347 	return 0;
348 }
349 
350 static int ts3a227e_resume(struct device *dev)
351 {
352 	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
353 
354 	dev_dbg(ts3a227e->dev, "resume enable irq\n");
355 	enable_irq(ts3a227e->irq);
356 
357 	return 0;
358 }
359 #endif
360 
361 static const struct dev_pm_ops ts3a227e_pm = {
362 	SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend, ts3a227e_resume)
363 };
364 
365 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
366 	{ "ts3a227e", 0 },
367 	{ }
368 };
369 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
370 
371 static const struct of_device_id ts3a227e_of_match[] = {
372 	{ .compatible = "ti,ts3a227e", },
373 	{ }
374 };
375 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
376 
377 static struct i2c_driver ts3a227e_driver = {
378 	.driver = {
379 		.name = "ts3a227e",
380 		.pm = &ts3a227e_pm,
381 		.of_match_table = of_match_ptr(ts3a227e_of_match),
382 	},
383 	.probe = ts3a227e_i2c_probe,
384 	.id_table = ts3a227e_i2c_ids,
385 };
386 module_i2c_driver(ts3a227e_driver);
387 
388 MODULE_DESCRIPTION("ASoC ts3a227e driver");
389 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
390 MODULE_LICENSE("GPL v2");
391