]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/codecs/ts3a227e.c
Merge remote-tracking branches 'asoc/topic/ts3a227e', 'asoc/topic/ts3a277e' and ...
[karo-tx-linux.git] / sound / soc / codecs / ts3a227e.c
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 regmap *regmap;
27         struct snd_soc_jack *jack;
28         bool plugged;
29         bool mic_present;
30         unsigned int buttons_held;
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_2 0x05 */
82 #define KP_ENABLE 0x04
83
84 /* TS3A227E_REG_SETTING_3 0x06 */
85 #define MICBIAS_SETTING_SFT (3)
86 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
87
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)
94
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 },
112 };
113
114 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
115 {
116         switch (reg) {
117         case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
118                 return true;
119         default:
120                 return false;
121         }
122 }
123
124 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
125 {
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;
132         }
133 }
134
135 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
136 {
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;
144         }
145 }
146
147 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
148 {
149         unsigned int i;
150         int report = 0;
151
152         if (!ts3a227e->jack)
153                 return;
154
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];
162         }
163         snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
164 }
165
166 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
167 {
168         bool plugged, mic_present;
169
170         plugged = !!(acc_reg & JACK_INSERTED);
171         mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
172
173         ts3a227e->plugged = plugged;
174
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);
183                 }
184         }
185 }
186
187 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
188 {
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
193         /* Check for plug/unplug. */
194         regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
195         if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
196                 regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
197                 ts3a227e_new_jack_state(ts3a227e, acc_reg);
198         }
199
200         /* Report any key events. */
201         regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
202         for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
203                 if (kp_int_reg & PRESS_MASK(i))
204                         ts3a227e->buttons_held |= (1 << i);
205                 if (kp_int_reg & RELEASE_MASK(i))
206                         ts3a227e->buttons_held &= ~(1 << i);
207         }
208
209         ts3a227e_jack_report(ts3a227e);
210
211         return IRQ_HANDLED;
212 }
213
214 /**
215  * ts3a227e_enable_jack_detect - Specify a jack for event reporting
216  *
217  * @component:  component to register the jack with
218  * @jack: jack to use to report headset and button events on
219  *
220  * After this function has been called the headset insert/remove and button
221  * events 0-3 will be routed to the given jack.  Jack can be null to stop
222  * reporting.
223  */
224 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
225                                 struct snd_soc_jack *jack)
226 {
227         struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
228
229         snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
230         snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
231         snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
232         snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
233
234         ts3a227e->jack = jack;
235         ts3a227e_jack_report(ts3a227e);
236
237         return 0;
238 }
239 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
240
241 static struct snd_soc_component_driver ts3a227e_soc_driver;
242
243 static const struct regmap_config ts3a227e_regmap_config = {
244         .val_bits = 8,
245         .reg_bits = 8,
246
247         .max_register = TS3A227E_REG_KP_THRESHOLD_3,
248         .readable_reg = ts3a227e_readable_reg,
249         .writeable_reg = ts3a227e_writeable_reg,
250         .volatile_reg = ts3a227e_volatile_reg,
251
252         .cache_type = REGCACHE_RBTREE,
253         .reg_defaults = ts3a227e_reg_defaults,
254         .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
255 };
256
257 static int ts3a227e_parse_dt(struct ts3a227e *ts3a227e, struct device_node *np)
258 {
259         u32 micbias;
260         int err;
261
262         err = of_property_read_u32(np, "ti,micbias", &micbias);
263         if (!err) {
264                 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
265                         MICBIAS_SETTING_MASK,
266                         (micbias & 0x07) << MICBIAS_SETTING_SFT);
267         }
268
269         return 0;
270 }
271
272 static int ts3a227e_i2c_probe(struct i2c_client *i2c,
273                               const struct i2c_device_id *id)
274 {
275         struct ts3a227e *ts3a227e;
276         struct device *dev = &i2c->dev;
277         int ret;
278         unsigned int acc_reg;
279
280         ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
281         if (ts3a227e == NULL)
282                 return -ENOMEM;
283
284         i2c_set_clientdata(i2c, ts3a227e);
285
286         ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
287         if (IS_ERR(ts3a227e->regmap))
288                 return PTR_ERR(ts3a227e->regmap);
289
290         if (dev->of_node) {
291                 ret = ts3a227e_parse_dt(ts3a227e, dev->of_node);
292                 if (ret) {
293                         dev_err(dev, "Failed to parse device tree: %d\n", ret);
294                         return ret;
295                 }
296         }
297
298         ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
299                                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
300                                         "TS3A227E", ts3a227e);
301         if (ret) {
302                 dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
303                 return ret;
304         }
305
306         ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
307                                               NULL, 0);
308         if (ret)
309                 return ret;
310
311         /* Enable interrupts except for ADC complete. */
312         regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
313                            INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
314                            ADC_COMPLETE_INT_DISABLE);
315
316         /* Read jack status because chip might not trigger interrupt at boot. */
317         regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
318         ts3a227e_new_jack_state(ts3a227e, acc_reg);
319         ts3a227e_jack_report(ts3a227e);
320
321         return 0;
322 }
323
324 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
325         { "ts3a227e", 0 },
326         { }
327 };
328 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
329
330 static const struct of_device_id ts3a227e_of_match[] = {
331         { .compatible = "ti,ts3a227e", },
332         { }
333 };
334 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
335
336 static struct i2c_driver ts3a227e_driver = {
337         .driver = {
338                 .name = "ts3a227e",
339                 .owner = THIS_MODULE,
340                 .of_match_table = of_match_ptr(ts3a227e_of_match),
341         },
342         .probe = ts3a227e_i2c_probe,
343         .id_table = ts3a227e_i2c_ids,
344 };
345 module_i2c_driver(ts3a227e_driver);
346
347 MODULE_DESCRIPTION("ASoC ts3a227e driver");
348 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
349 MODULE_LICENSE("GPL v2");