Merge branch 'acpi-ec'
[linux-drm-fsl-dcu.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 struct ts3a227e {
24         struct regmap *regmap;
25         struct snd_soc_jack *jack;
26         bool plugged;
27         bool mic_present;
28         unsigned int buttons_held;
29 };
30
31 /* Button values to be reported on the jack */
32 static const int ts3a227e_buttons[] = {
33         SND_JACK_BTN_0,
34         SND_JACK_BTN_1,
35         SND_JACK_BTN_2,
36         SND_JACK_BTN_3,
37 };
38
39 #define TS3A227E_NUM_BUTTONS 4
40 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
41                             SND_JACK_MICROPHONE | \
42                             SND_JACK_BTN_0 | \
43                             SND_JACK_BTN_1 | \
44                             SND_JACK_BTN_2 | \
45                             SND_JACK_BTN_3)
46
47 /* TS3A227E registers */
48 #define TS3A227E_REG_DEVICE_ID          0x00
49 #define TS3A227E_REG_INTERRUPT          0x01
50 #define TS3A227E_REG_KP_INTERRUPT       0x02
51 #define TS3A227E_REG_INTERRUPT_DISABLE  0x03
52 #define TS3A227E_REG_SETTING_1          0x04
53 #define TS3A227E_REG_SETTING_2          0x05
54 #define TS3A227E_REG_SETTING_3          0x06
55 #define TS3A227E_REG_SWITCH_CONTROL_1   0x07
56 #define TS3A227E_REG_SWITCH_CONTROL_2   0x08
57 #define TS3A227E_REG_SWITCH_STATUS_1    0x09
58 #define TS3A227E_REG_SWITCH_STATUS_2    0x0a
59 #define TS3A227E_REG_ACCESSORY_STATUS   0x0b
60 #define TS3A227E_REG_ADC_OUTPUT         0x0c
61 #define TS3A227E_REG_KP_THRESHOLD_1     0x0d
62 #define TS3A227E_REG_KP_THRESHOLD_2     0x0e
63 #define TS3A227E_REG_KP_THRESHOLD_3     0x0f
64
65 /* TS3A227E_REG_INTERRUPT 0x01 */
66 #define INS_REM_EVENT 0x01
67 #define DETECTION_COMPLETE_EVENT 0x02
68
69 /* TS3A227E_REG_KP_INTERRUPT 0x02 */
70 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
71 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
72
73 /* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */
74 #define INS_REM_INT_DISABLE 0x01
75 #define DETECTION_COMPLETE_INT_DISABLE 0x02
76 #define ADC_COMPLETE_INT_DISABLE 0x04
77 #define INTB_DISABLE 0x08
78
79 /* TS3A227E_REG_SETTING_2 0x05 */
80 #define KP_ENABLE 0x04
81
82 /* TS3A227E_REG_ACCESSORY_STATUS  0x0b */
83 #define TYPE_3_POLE 0x01
84 #define TYPE_4_POLE_OMTP 0x02
85 #define TYPE_4_POLE_STANDARD 0x04
86 #define JACK_INSERTED 0x08
87 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
88
89 static const struct reg_default ts3a227e_reg_defaults[] = {
90         { TS3A227E_REG_DEVICE_ID, 0x10 },
91         { TS3A227E_REG_INTERRUPT, 0x00 },
92         { TS3A227E_REG_KP_INTERRUPT, 0x00 },
93         { TS3A227E_REG_INTERRUPT_DISABLE, 0x08 },
94         { TS3A227E_REG_SETTING_1, 0x23 },
95         { TS3A227E_REG_SETTING_2, 0x00 },
96         { TS3A227E_REG_SETTING_3, 0x0e },
97         { TS3A227E_REG_SWITCH_CONTROL_1, 0x00 },
98         { TS3A227E_REG_SWITCH_CONTROL_2, 0x00 },
99         { TS3A227E_REG_SWITCH_STATUS_1, 0x0c },
100         { TS3A227E_REG_SWITCH_STATUS_2, 0x00 },
101         { TS3A227E_REG_ACCESSORY_STATUS, 0x00 },
102         { TS3A227E_REG_ADC_OUTPUT, 0x00 },
103         { TS3A227E_REG_KP_THRESHOLD_1, 0x20 },
104         { TS3A227E_REG_KP_THRESHOLD_2, 0x40 },
105         { TS3A227E_REG_KP_THRESHOLD_3, 0x68 },
106 };
107
108 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
109 {
110         switch (reg) {
111         case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
112                 return true;
113         default:
114                 return false;
115         }
116 }
117
118 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
119 {
120         switch (reg) {
121         case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
122         case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
123                 return true;
124         default:
125                 return false;
126         }
127 }
128
129 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
130 {
131         switch (reg) {
132         case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE:
133         case TS3A227E_REG_SETTING_2:
134         case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT:
135                 return true;
136         default:
137                 return false;
138         }
139 }
140
141 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
142 {
143         unsigned int i;
144         int report = 0;
145
146         if (!ts3a227e->jack)
147                 return;
148
149         if (ts3a227e->plugged)
150                 report = SND_JACK_HEADPHONE;
151         if (ts3a227e->mic_present)
152                 report |= SND_JACK_MICROPHONE;
153         for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
154                 if (ts3a227e->buttons_held & (1 << i))
155                         report |= ts3a227e_buttons[i];
156         }
157         snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
158 }
159
160 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
161 {
162         bool plugged, mic_present;
163
164         plugged = !!(acc_reg & JACK_INSERTED);
165         mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
166
167         ts3a227e->plugged = plugged;
168
169         if (mic_present != ts3a227e->mic_present) {
170                 ts3a227e->mic_present = mic_present;
171                 ts3a227e->buttons_held = 0;
172                 if (mic_present) {
173                         /* Enable key press detection. */
174                         regmap_update_bits(ts3a227e->regmap,
175                                            TS3A227E_REG_SETTING_2,
176                                            KP_ENABLE, KP_ENABLE);
177                 }
178         }
179 }
180
181 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
182 {
183         struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
184         struct regmap *regmap = ts3a227e->regmap;
185         unsigned int int_reg, kp_int_reg, acc_reg, i;
186
187         /* Check for plug/unplug. */
188         regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
189         if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
190                 regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
191                 ts3a227e_new_jack_state(ts3a227e, acc_reg);
192         }
193
194         /* Report any key events. */
195         regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
196         for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
197                 if (kp_int_reg & PRESS_MASK(i))
198                         ts3a227e->buttons_held |= (1 << i);
199                 if (kp_int_reg & RELEASE_MASK(i))
200                         ts3a227e->buttons_held &= ~(1 << i);
201         }
202
203         ts3a227e_jack_report(ts3a227e);
204
205         return IRQ_HANDLED;
206 }
207
208 /**
209  * ts3a227e_enable_jack_detect - Specify a jack for event reporting
210  *
211  * @component:  component to register the jack with
212  * @jack: jack to use to report headset and button events on
213  *
214  * After this function has been called the headset insert/remove and button
215  * events 0-3 will be routed to the given jack.  Jack can be null to stop
216  * reporting.
217  */
218 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
219                                 struct snd_soc_jack *jack)
220 {
221         struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
222
223         snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
224         snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
225         snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
226         snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
227
228         ts3a227e->jack = jack;
229         ts3a227e_jack_report(ts3a227e);
230
231         return 0;
232 }
233 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
234
235 static struct snd_soc_component_driver ts3a227e_soc_driver;
236
237 static const struct regmap_config ts3a227e_regmap_config = {
238         .val_bits = 8,
239         .reg_bits = 8,
240
241         .max_register = TS3A227E_REG_KP_THRESHOLD_3,
242         .readable_reg = ts3a227e_readable_reg,
243         .writeable_reg = ts3a227e_writeable_reg,
244         .volatile_reg = ts3a227e_volatile_reg,
245
246         .cache_type = REGCACHE_RBTREE,
247         .reg_defaults = ts3a227e_reg_defaults,
248         .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
249 };
250
251 static int ts3a227e_i2c_probe(struct i2c_client *i2c,
252                               const struct i2c_device_id *id)
253 {
254         struct ts3a227e *ts3a227e;
255         struct device *dev = &i2c->dev;
256         int ret;
257         unsigned int acc_reg;
258
259         ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
260         if (ts3a227e == NULL)
261                 return -ENOMEM;
262
263         i2c_set_clientdata(i2c, ts3a227e);
264
265         ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
266         if (IS_ERR(ts3a227e->regmap))
267                 return PTR_ERR(ts3a227e->regmap);
268
269         ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
270                                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
271                                         "TS3A227E", ts3a227e);
272         if (ret) {
273                 dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
274                 return ret;
275         }
276
277         ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
278                                               NULL, 0);
279         if (ret)
280                 return ret;
281
282         /* Enable interrupts except for ADC complete. */
283         regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
284                            INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
285                            ADC_COMPLETE_INT_DISABLE);
286
287         /* Read jack status because chip might not trigger interrupt at boot. */
288         regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
289         ts3a227e_new_jack_state(ts3a227e, acc_reg);
290         ts3a227e_jack_report(ts3a227e);
291
292         return 0;
293 }
294
295 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
296         { "ts3a227e", 0 },
297         { }
298 };
299 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
300
301 static const struct of_device_id ts3a227e_of_match[] = {
302         { .compatible = "ti,ts3a227e", },
303         { }
304 };
305 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
306
307 static struct i2c_driver ts3a227e_driver = {
308         .driver = {
309                 .name = "ts3a227e",
310                 .owner = THIS_MODULE,
311                 .of_match_table = of_match_ptr(ts3a227e_of_match),
312         },
313         .probe = ts3a227e_i2c_probe,
314         .id_table = ts3a227e_i2c_ids,
315 };
316 module_i2c_driver(ts3a227e_driver);
317
318 MODULE_DESCRIPTION("ASoC ts3a227e driver");
319 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
320 MODULE_LICENSE("GPL v2");