xref: /openbmc/linux/sound/soc/codecs/ts3a227e.c (revision 31354121)
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 struct snd_soc_component_driver ts3a227e_soc_driver;
262 
263 static const struct regmap_config ts3a227e_regmap_config = {
264 	.val_bits = 8,
265 	.reg_bits = 8,
266 
267 	.max_register = TS3A227E_REG_KP_THRESHOLD_3,
268 	.readable_reg = ts3a227e_readable_reg,
269 	.writeable_reg = ts3a227e_writeable_reg,
270 	.volatile_reg = ts3a227e_volatile_reg,
271 
272 	.cache_type = REGCACHE_RBTREE,
273 	.reg_defaults = ts3a227e_reg_defaults,
274 	.num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
275 };
276 
277 static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
278 				struct device *dev)
279 {
280 	u32 value;
281 	u32 value_ms;
282 	u32 setting3_value = 0;
283 	u32 setting3_mask = 0;
284 	int err;
285 
286 	err = device_property_read_u32(dev, "ti,micbias", &value);
287 	if (!err) {
288 		setting3_mask = MICBIAS_SETTING_MASK;
289 		setting3_value = (value << MICBIAS_SETTING_SFT) &
290 				 MICBIAS_SETTING_MASK;
291 	}
292 
293 	err = device_property_read_u32(dev, "ti,debounce-release-ms",
294 				       &value_ms);
295 	if (!err) {
296 		value = (value_ms > 10);
297 		setting3_mask |= DEBOUNCE_RELEASE_SETTING_MASK;
298 		setting3_value |= (value << DEBOUNCE_RELEASE_SETTING_SFT) &
299 				  DEBOUNCE_RELEASE_SETTING_MASK;
300 	}
301 
302 	err = device_property_read_u32(dev, "ti,debounce-press-ms", &value_ms);
303 	if (!err) {
304 		value = (value_ms + 20) / 40;
305 		if (value > 3)
306 			value = 3;
307 		setting3_mask |= DEBOUNCE_PRESS_SETTING_MASK;
308 		setting3_value |= (value << DEBOUNCE_PRESS_SETTING_SFT) &
309 				  DEBOUNCE_PRESS_SETTING_MASK;
310 	}
311 
312 	if (setting3_mask)
313 		regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
314 				   setting3_mask, setting3_value);
315 
316 	err = device_property_read_u32(dev, "ti,debounce-insertion-ms",
317 				       &value_ms);
318 	if (!err) {
319 		if (value_ms < 165)
320 			value = (value_ms + 15) / 30;
321 		else if (value_ms < 1500)
322 			value = 6;
323 		else
324 			value = 7;
325 		regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_1,
326 				   DEBOUNCE_INSERTION_SETTING_MASK,
327 				   (value << DEBOUNCE_INSERTION_SETTING_SFT) &
328 					   DEBOUNCE_INSERTION_SETTING_MASK);
329 	}
330 
331 	return 0;
332 }
333 
334 static int ts3a227e_i2c_probe(struct i2c_client *i2c)
335 {
336 	struct ts3a227e *ts3a227e;
337 	struct device *dev = &i2c->dev;
338 	int ret;
339 	unsigned int acc_reg;
340 
341 	ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
342 	if (ts3a227e == NULL)
343 		return -ENOMEM;
344 
345 	i2c_set_clientdata(i2c, ts3a227e);
346 	ts3a227e->dev = dev;
347 	ts3a227e->irq = i2c->irq;
348 
349 	ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
350 	if (IS_ERR(ts3a227e->regmap))
351 		return PTR_ERR(ts3a227e->regmap);
352 
353 	ret = ts3a227e_parse_device_property(ts3a227e, dev);
354 	if (ret) {
355 		dev_err(dev, "Failed to parse device property: %d\n", ret);
356 		return ret;
357 	}
358 
359 	ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
360 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
361 					"TS3A227E", ts3a227e);
362 	if (ret) {
363 		dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
364 		return ret;
365 	}
366 
367 	ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
368 					      NULL, 0);
369 	if (ret)
370 		return ret;
371 
372 	/* Enable interrupts except for ADC complete. */
373 	regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
374 			   INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
375 			   ADC_COMPLETE_INT_DISABLE);
376 
377 	/* Read jack status because chip might not trigger interrupt at boot. */
378 	regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
379 	ts3a227e_new_jack_state(ts3a227e, acc_reg);
380 	ts3a227e_jack_report(ts3a227e);
381 
382 	return 0;
383 }
384 
385 #ifdef CONFIG_PM_SLEEP
386 static int ts3a227e_suspend(struct device *dev)
387 {
388 	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
389 
390 	dev_dbg(ts3a227e->dev, "suspend disable irq\n");
391 	disable_irq(ts3a227e->irq);
392 
393 	return 0;
394 }
395 
396 static int ts3a227e_resume(struct device *dev)
397 {
398 	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
399 
400 	dev_dbg(ts3a227e->dev, "resume enable irq\n");
401 	enable_irq(ts3a227e->irq);
402 
403 	return 0;
404 }
405 #endif
406 
407 static const struct dev_pm_ops ts3a227e_pm = {
408 	SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend, ts3a227e_resume)
409 };
410 
411 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
412 	{ "ts3a227e", 0 },
413 	{ }
414 };
415 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
416 
417 #ifdef CONFIG_OF
418 static const struct of_device_id ts3a227e_of_match[] = {
419 	{ .compatible = "ti,ts3a227e", },
420 	{ }
421 };
422 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
423 #endif
424 
425 #ifdef CONFIG_ACPI
426 static struct acpi_device_id ts3a227e_acpi_match[] = {
427 	{ "104C227E", 0 },
428 	{},
429 };
430 MODULE_DEVICE_TABLE(acpi, ts3a227e_acpi_match);
431 #endif
432 
433 static struct i2c_driver ts3a227e_driver = {
434 	.driver = {
435 		.name = "ts3a227e",
436 		.pm = &ts3a227e_pm,
437 		.of_match_table = of_match_ptr(ts3a227e_of_match),
438 		.acpi_match_table = ACPI_PTR(ts3a227e_acpi_match),
439 	},
440 	.probe_new = ts3a227e_i2c_probe,
441 	.id_table = ts3a227e_i2c_ids,
442 };
443 module_i2c_driver(ts3a227e_driver);
444 
445 MODULE_DESCRIPTION("ASoC ts3a227e driver");
446 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
447 MODULE_LICENSE("GPL v2");
448