ARM: dts: omap5: Add bus_dma_limit for L3 bus
[linux/fpc-iii.git] / sound / soc / codecs / ts3a227e.c
blob3ed3b45fa7ba3ed2cc4d40400d1fa49cf8b5d249
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TS3A227E Autonomous Audio Accessory Detection and Configuration Switch
5 * Copyright (C) 2014 Google, Inc.
6 */
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>
17 #include <sound/core.h>
18 #include <sound/jack.h>
19 #include <sound/soc.h>
21 #include "ts3a227e.h"
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;
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,
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)
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
67 /* TS3A227E_REG_INTERRUPT 0x01 */
68 #define INS_REM_EVENT 0x01
69 #define DETECTION_COMPLETE_EVENT 0x02
71 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
72 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
73 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
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
81 /* TS3A227E_REG_SETTING_2 0x05 */
82 #define KP_ENABLE 0x04
84 /* TS3A227E_REG_SETTING_3 0x06 */
85 #define MICBIAS_SETTING_SFT (3)
86 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
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)
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 },
114 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
116 switch (reg) {
117 case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
118 return true;
119 default:
120 return false;
124 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
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;
135 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
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;
147 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
149 unsigned int i;
150 int report = 0;
152 if (!ts3a227e->jack)
153 return;
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];
163 snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
166 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
168 bool plugged, mic_present;
170 plugged = !!(acc_reg & JACK_INSERTED);
171 mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
173 ts3a227e->plugged = plugged;
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);
187 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
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 struct device *dev = ts3a227e->dev;
193 int ret;
195 /* Check for plug/unplug. */
196 ret = regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
197 if (ret) {
198 dev_err(dev, "failed to clear interrupt ret=%d\n", ret);
199 return IRQ_NONE;
202 if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
203 regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
204 ts3a227e_new_jack_state(ts3a227e, acc_reg);
207 /* Report any key events. */
208 ret = regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
209 if (ret) {
210 dev_err(dev, "failed to clear key interrupt ret=%d\n", ret);
211 return IRQ_NONE;
214 for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
215 if (kp_int_reg & PRESS_MASK(i))
216 ts3a227e->buttons_held |= (1 << i);
217 if (kp_int_reg & RELEASE_MASK(i))
218 ts3a227e->buttons_held &= ~(1 << i);
221 ts3a227e_jack_report(ts3a227e);
223 return IRQ_HANDLED;
227 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
229 * @component: component to register the jack with
230 * @jack: jack to use to report headset and button events on
232 * After this function has been called the headset insert/remove and button
233 * events 0-3 will be routed to the given jack. Jack can be null to stop
234 * reporting.
236 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
237 struct snd_soc_jack *jack)
239 struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
241 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
242 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
243 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
244 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
246 ts3a227e->jack = jack;
247 ts3a227e_jack_report(ts3a227e);
249 return 0;
251 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
253 static struct snd_soc_component_driver ts3a227e_soc_driver;
255 static const struct regmap_config ts3a227e_regmap_config = {
256 .val_bits = 8,
257 .reg_bits = 8,
259 .max_register = TS3A227E_REG_KP_THRESHOLD_3,
260 .readable_reg = ts3a227e_readable_reg,
261 .writeable_reg = ts3a227e_writeable_reg,
262 .volatile_reg = ts3a227e_volatile_reg,
264 .cache_type = REGCACHE_RBTREE,
265 .reg_defaults = ts3a227e_reg_defaults,
266 .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
269 static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
270 struct device *dev)
272 u32 micbias;
273 int err;
275 err = device_property_read_u32(dev, "ti,micbias", &micbias);
276 if (!err) {
277 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
278 MICBIAS_SETTING_MASK,
279 (micbias & 0x07) << MICBIAS_SETTING_SFT);
282 return 0;
285 static int ts3a227e_i2c_probe(struct i2c_client *i2c,
286 const struct i2c_device_id *id)
288 struct ts3a227e *ts3a227e;
289 struct device *dev = &i2c->dev;
290 int ret;
291 unsigned int acc_reg;
293 ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
294 if (ts3a227e == NULL)
295 return -ENOMEM;
297 i2c_set_clientdata(i2c, ts3a227e);
298 ts3a227e->dev = dev;
299 ts3a227e->irq = i2c->irq;
301 ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
302 if (IS_ERR(ts3a227e->regmap))
303 return PTR_ERR(ts3a227e->regmap);
305 ret = ts3a227e_parse_device_property(ts3a227e, dev);
306 if (ret) {
307 dev_err(dev, "Failed to parse device property: %d\n", ret);
308 return ret;
311 ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
312 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
313 "TS3A227E", ts3a227e);
314 if (ret) {
315 dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
316 return ret;
319 ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
320 NULL, 0);
321 if (ret)
322 return ret;
324 /* Enable interrupts except for ADC complete. */
325 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
326 INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
327 ADC_COMPLETE_INT_DISABLE);
329 /* Read jack status because chip might not trigger interrupt at boot. */
330 regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
331 ts3a227e_new_jack_state(ts3a227e, acc_reg);
332 ts3a227e_jack_report(ts3a227e);
334 return 0;
337 #ifdef CONFIG_PM_SLEEP
338 static int ts3a227e_suspend(struct device *dev)
340 struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
342 dev_dbg(ts3a227e->dev, "suspend disable irq\n");
343 disable_irq(ts3a227e->irq);
345 return 0;
348 static int ts3a227e_resume(struct device *dev)
350 struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
352 dev_dbg(ts3a227e->dev, "resume enable irq\n");
353 enable_irq(ts3a227e->irq);
355 return 0;
357 #endif
359 static const struct dev_pm_ops ts3a227e_pm = {
360 SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend, ts3a227e_resume)
363 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
364 { "ts3a227e", 0 },
367 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
369 static const struct of_device_id ts3a227e_of_match[] = {
370 { .compatible = "ti,ts3a227e", },
373 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
375 #ifdef CONFIG_ACPI
376 static struct acpi_device_id ts3a227e_acpi_match[] = {
377 { "104C227E", 0 },
380 MODULE_DEVICE_TABLE(acpi, ts3a227e_acpi_match);
381 #endif
383 static struct i2c_driver ts3a227e_driver = {
384 .driver = {
385 .name = "ts3a227e",
386 .pm = &ts3a227e_pm,
387 .of_match_table = of_match_ptr(ts3a227e_of_match),
388 .acpi_match_table = ACPI_PTR(ts3a227e_acpi_match),
390 .probe = ts3a227e_i2c_probe,
391 .id_table = ts3a227e_i2c_ids,
393 module_i2c_driver(ts3a227e_driver);
395 MODULE_DESCRIPTION("ASoC ts3a227e driver");
396 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
397 MODULE_LICENSE("GPL v2");