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