~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/codecs/ts3a227e.c

Version: ~ [ linux-6.1-rc7 ] ~ [ linux-6.0.10 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.80 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.156 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.225 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.267 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.300 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.334 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp