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