blob: 94675da514c8f3e83e0680e962a936d33e8eac82 [file] [log] [blame]
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001/*
2 * ALSA SoC TWL6040 codec driver
3 *
4 * Author: Misael Lopez Cruz <x0052729@ti.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/pm.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000027#include <linux/platform_device.h>
Stephen Rothwell68b40cc2010-03-29 17:55:51 +110028#include <linux/slab.h>
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050029#include <linux/mfd/twl6040.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000030
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
Liam Girdwoode48b46b2012-01-11 12:43:24 +000035#include <sound/soc-dapm.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000036#include <sound/initval.h>
37#include <sound/tlv.h>
38
39#include "twl6040.h"
40
Peter Ujfalusi68897492013-06-24 15:42:05 +020041enum twl6040_dai_id {
42 TWL6040_DAI_LEGACY = 0,
43 TWL6040_DAI_UL,
44 TWL6040_DAI_DL1,
45 TWL6040_DAI_DL2,
46 TWL6040_DAI_VIB,
47};
48
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -060049#define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -060050#define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
51
52#define TWL6040_OUTHS_0dB 0x00
53#define TWL6040_OUTHS_M30dB 0x0F
54#define TWL6040_OUTHF_0dB 0x03
55#define TWL6040_OUTHF_M52dB 0x1D
56
Lars-Peter Clausen290c3482013-10-06 13:43:51 +020057#define TWL6040_CACHEREGNUM (TWL6040_REG_STATUS + 1)
Peter Ujfalusi317596a2011-09-22 11:05:49 +030058
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060059struct twl6040_jack_data {
60 struct snd_soc_jack *jack;
Peter Ujfalusi46dd0b92011-09-26 16:26:27 +030061 struct delayed_work work;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060062 int report;
63};
64
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000065/* codec private data */
66struct twl6040_data {
Peter Ujfalusi2a433b92011-07-04 19:52:26 +030067 int plug_irq;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000068 int codec_powered;
69 int pll;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030070 int pll_power_mode;
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -060071 int hs_power_mode;
72 int hs_power_mode_locked;
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +020073 bool dl1_unmuted;
74 bool dl2_unmuted;
Peter Ujfalusi53509102013-11-29 16:03:44 +020075 u8 dl12_cache[TWL6040_REG_HFRCTL - TWL6040_REG_HSLCTL + 1];
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050076 unsigned int clk_in;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000077 unsigned int sysclk;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060078 struct twl6040_jack_data hs_jack;
Kuninori Morimoto74803892018-01-29 04:20:09 +000079 struct snd_soc_component *component;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060080 struct mutex mutex;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000081};
82
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030083/* set of rates for each pll: low-power and high-performance */
Lars-Peter Clausen0d76fc62014-02-05 21:54:34 +010084static const unsigned int lp_rates[] = {
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030085 8000,
86 11250,
87 16000,
88 22500,
89 32000,
90 44100,
91 48000,
92 88200,
93 96000,
94};
95
Lars-Peter Clausen0d76fc62014-02-05 21:54:34 +010096static const unsigned int hp_rates[] = {
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030097 8000,
98 16000,
99 32000,
100 48000,
101 96000,
102};
103
Lars-Peter Clausen0d76fc62014-02-05 21:54:34 +0100104static const struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
Peter Ujfalusif53c3462011-06-29 13:28:18 +0300105 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
106 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300107};
108
Kuninori Morimoto74803892018-01-29 04:20:09 +0000109#define to_twl6040(component) dev_get_drvdata((component)->dev->parent)
Kuninori Morimoto3bd33362017-11-09 01:04:37 +0000110
Kuninori Morimoto74803892018-01-29 04:20:09 +0000111static unsigned int twl6040_read(struct snd_soc_component *component, unsigned int reg)
Peter Ujfalusi53509102013-11-29 16:03:44 +0200112{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000113 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
114 struct twl6040 *twl6040 = to_twl6040(component);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000115 u8 value;
116
117 if (reg >= TWL6040_CACHEREGNUM)
118 return -EIO;
119
Peter Ujfalusi626bcac2013-11-29 16:03:47 +0200120 switch (reg) {
121 case TWL6040_REG_HSLCTL:
122 case TWL6040_REG_HSRCTL:
123 case TWL6040_REG_EARCTL:
124 case TWL6040_REG_HFLCTL:
125 case TWL6040_REG_HFRCTL:
126 value = priv->dl12_cache[reg - TWL6040_REG_HSLCTL];
127 break;
128 default:
129 value = twl6040_reg_read(twl6040, reg);
130 break;
131 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000132
133 return value;
134}
135
Kuninori Morimoto74803892018-01-29 04:20:09 +0000136static bool twl6040_can_write_to_chip(struct snd_soc_component *component,
Peter Ujfalusi0d35d0802013-11-29 16:03:43 +0200137 unsigned int reg)
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200138{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000139 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200140
141 switch (reg) {
142 case TWL6040_REG_HSLCTL:
143 case TWL6040_REG_HSRCTL:
144 case TWL6040_REG_EARCTL:
145 /* DL1 path */
146 return priv->dl1_unmuted;
147 case TWL6040_REG_HFLCTL:
148 case TWL6040_REG_HFRCTL:
149 return priv->dl2_unmuted;
150 default:
Gustavo A. R. Silvabc94c882018-08-04 16:53:38 -0500151 return true;
Sachin Kamatbf551412013-09-13 16:16:17 +0530152 }
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200153}
154
Kuninori Morimoto74803892018-01-29 04:20:09 +0000155static inline void twl6040_update_dl12_cache(struct snd_soc_component *component,
Peter Ujfalusi626bcac2013-11-29 16:03:47 +0200156 u8 reg, u8 value)
157{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000158 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusi626bcac2013-11-29 16:03:47 +0200159
160 switch (reg) {
161 case TWL6040_REG_HSLCTL:
162 case TWL6040_REG_HSRCTL:
163 case TWL6040_REG_EARCTL:
164 case TWL6040_REG_HFLCTL:
165 case TWL6040_REG_HFRCTL:
166 priv->dl12_cache[reg - TWL6040_REG_HSLCTL] = value;
167 break;
168 default:
169 break;
170 }
171}
172
Kuninori Morimoto74803892018-01-29 04:20:09 +0000173static int twl6040_write(struct snd_soc_component *component,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000174 unsigned int reg, unsigned int value)
175{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000176 struct twl6040 *twl6040 = to_twl6040(component);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500177
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000178 if (reg >= TWL6040_CACHEREGNUM)
179 return -EIO;
180
Kuninori Morimoto74803892018-01-29 04:20:09 +0000181 twl6040_update_dl12_cache(component, reg, value);
182 if (twl6040_can_write_to_chip(component, reg))
Peter Ujfalusid17bf312011-09-22 11:05:48 +0300183 return twl6040_reg_write(twl6040, reg, value);
184 else
185 return 0;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000186}
187
Kuninori Morimoto74803892018-01-29 04:20:09 +0000188static void twl6040_init_chip(struct snd_soc_component *component)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000189{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000190 twl6040_read(component, TWL6040_REG_TRIM1);
191 twl6040_read(component, TWL6040_REG_TRIM2);
192 twl6040_read(component, TWL6040_REG_TRIM3);
193 twl6040_read(component, TWL6040_REG_HSOTRIM);
194 twl6040_read(component, TWL6040_REG_HFOTRIM);
Peter Ujfalusif97217f2011-09-26 16:05:56 +0300195
Peter Ujfalusi2c27ff42011-09-15 15:39:28 +0300196 /* Change chip defaults */
197 /* No imput selected for microphone amplifiers */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000198 twl6040_write(component, TWL6040_REG_MICLCTL, 0x18);
199 twl6040_write(component, TWL6040_REG_MICRCTL, 0x18);
Peter Ujfalusi3acef682011-09-22 11:05:45 +0300200
201 /*
202 * We need to lower the default gain values, so the ramp code
203 * can work correctly for the first playback.
204 * This reduces the pop noise heard at the first playback.
205 */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000206 twl6040_write(component, TWL6040_REG_HSGAIN, 0xff);
207 twl6040_write(component, TWL6040_REG_EARCTL, 0x1e);
208 twl6040_write(component, TWL6040_REG_HFLGAIN, 0x1d);
209 twl6040_write(component, TWL6040_REG_HFRGAIN, 0x1d);
210 twl6040_write(component, TWL6040_REG_LINEGAIN, 0);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000211}
212
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000213/* set headset dac and driver power mode */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000214static int headset_power_mode(struct snd_soc_component *component, int high_perf)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000215{
216 int hslctl, hsrctl;
Peter Ujfalusiab6cf132011-09-22 11:05:54 +0300217 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000218
Kuninori Morimoto74803892018-01-29 04:20:09 +0000219 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL);
220 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000221
222 if (high_perf) {
223 hslctl &= ~mask;
224 hsrctl &= ~mask;
225 } else {
226 hslctl |= mask;
227 hsrctl |= mask;
228 }
229
Kuninori Morimoto74803892018-01-29 04:20:09 +0000230 twl6040_write(component, TWL6040_REG_HSLCTL, hslctl);
231 twl6040_write(component, TWL6040_REG_HSRCTL, hsrctl);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000232
233 return 0;
234}
235
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500236static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
237 struct snd_kcontrol *kcontrol, int event)
238{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000239 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
Peter Ujfalusi33b68162011-10-12 14:46:02 +0300240 u8 hslctl, hsrctl;
241
242 /*
243 * Workaround for Headset DC offset caused pop noise:
244 * Both HS DAC need to be turned on (before the HS driver) and off at
245 * the same time.
246 */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000247 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL);
248 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL);
Peter Ujfalusi33b68162011-10-12 14:46:02 +0300249 if (SND_SOC_DAPM_EVENT_ON(event)) {
250 hslctl |= TWL6040_HSDACENA;
251 hsrctl |= TWL6040_HSDACENA;
252 } else {
253 hslctl &= ~TWL6040_HSDACENA;
254 hsrctl &= ~TWL6040_HSDACENA;
255 }
Kuninori Morimoto74803892018-01-29 04:20:09 +0000256 twl6040_write(component, TWL6040_REG_HSLCTL, hslctl);
257 twl6040_write(component, TWL6040_REG_HSRCTL, hsrctl);
Peter Ujfalusi33b68162011-10-12 14:46:02 +0300258
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500259 msleep(1);
260 return 0;
261}
262
Peter Ujfalusi694b0002011-10-13 15:05:44 +0300263static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000264 struct snd_kcontrol *kcontrol, int event)
265{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000266 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
267 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600268 int ret = 0;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000269
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600270 if (SND_SOC_DAPM_EVENT_ON(event)) {
Peter Ujfalusifac2f3e2011-10-13 15:05:42 +0300271 /* Earphone doesn't support low power mode */
272 priv->hs_power_mode_locked = 1;
Kuninori Morimoto74803892018-01-29 04:20:09 +0000273 ret = headset_power_mode(component, 1);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600274 } else {
Peter Ujfalusifac2f3e2011-10-13 15:05:42 +0300275 priv->hs_power_mode_locked = 0;
Kuninori Morimoto74803892018-01-29 04:20:09 +0000276 ret = headset_power_mode(component, priv->hs_power_mode);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600277 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000278
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500279 msleep(1);
280
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600281 return ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000282}
283
Kuninori Morimoto74803892018-01-29 04:20:09 +0000284static void twl6040_hs_jack_report(struct snd_soc_component *component,
Mark Brown64ed9832011-01-20 21:43:44 +0000285 struct snd_soc_jack *jack, int report)
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600286{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000287 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600288 int status;
289
290 mutex_lock(&priv->mutex);
291
292 /* Sync status */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000293 status = twl6040_read(component, TWL6040_REG_STATUS);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600294 if (status & TWL6040_PLUGCOMP)
295 snd_soc_jack_report(jack, report, report);
296 else
297 snd_soc_jack_report(jack, 0, report);
298
299 mutex_unlock(&priv->mutex);
300}
301
Kuninori Morimoto74803892018-01-29 04:20:09 +0000302void twl6040_hs_jack_detect(struct snd_soc_component *component,
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600303 struct snd_soc_jack *jack, int report)
304{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000305 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600306 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
307
308 hs_jack->jack = jack;
309 hs_jack->report = report;
310
Kuninori Morimoto74803892018-01-29 04:20:09 +0000311 twl6040_hs_jack_report(component, hs_jack->jack, hs_jack->report);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600312}
313EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
314
315static void twl6040_accessory_work(struct work_struct *work)
316{
317 struct twl6040_data *priv = container_of(work,
Peter Ujfalusi46dd0b92011-09-26 16:26:27 +0300318 struct twl6040_data, hs_jack.work.work);
Kuninori Morimoto74803892018-01-29 04:20:09 +0000319 struct snd_soc_component *component = priv->component;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600320 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
321
Kuninori Morimoto74803892018-01-29 04:20:09 +0000322 twl6040_hs_jack_report(component, hs_jack->jack, hs_jack->report);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600323}
324
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000325/* audio interrupt handler */
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500326static irqreturn_t twl6040_audio_handler(int irq, void *data)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000327{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000328 struct snd_soc_component *component = data;
329 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000330
Mark Browna06e4272013-07-18 22:44:03 +0100331 queue_delayed_work(system_power_efficient_wq,
332 &priv->hs_jack.work, msecs_to_jiffies(200));
Olaya, Margaritacf370a52010-12-10 21:05:30 -0600333
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000334 return IRQ_HANDLED;
335}
336
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300337static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
338 struct snd_ctl_elem_value *ucontrol)
339{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000340 struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300341 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
342 unsigned int val;
343
344 /* Do not allow changes while Input/FF efect is running */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000345 val = twl6040_read(component, e->reg);
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300346 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
347 return -EBUSY;
348
349 return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
350}
351
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000352/*
353 * MICATT volume control:
354 * from -6 to 0 dB in 6 dB steps
355 */
356static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
357
358/*
359 * MICGAIN volume control:
Ricardo Neri2763f452011-05-01 15:35:55 +0100360 * from 6 to 30 dB in 6 dB steps
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000361 */
Ricardo Neri2763f452011-05-01 15:35:55 +0100362static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000363
364/*
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600365 * AFMGAIN volume control:
Liam Girdwood1f71a3b2011-03-28 19:23:23 +0100366 * from -18 to 24 dB in 6 dB steps
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600367 */
Liam Girdwood1f71a3b2011-03-28 19:23:23 +0100368static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600369
370/*
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000371 * HSGAIN volume control:
372 * from -30 to 0 dB in 2 dB steps
373 */
374static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
375
376/*
377 * HFGAIN volume control:
378 * from -52 to 6 dB in 2 dB steps
379 */
380static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
381
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500382/*
383 * EPGAIN volume control:
384 * from -24 to 6 dB in 2 dB steps
385 */
386static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
387
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000388/* Left analog microphone selection */
389static const char *twl6040_amicl_texts[] =
390 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
391
392/* Right analog microphone selection */
393static const char *twl6040_amicr_texts[] =
394 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
395
396static const struct soc_enum twl6040_enum[] = {
Takashi Iwaia1d0d782014-02-18 10:30:20 +0100397 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3,
398 ARRAY_SIZE(twl6040_amicl_texts), twl6040_amicl_texts),
399 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3,
400 ARRAY_SIZE(twl6040_amicr_texts), twl6040_amicr_texts),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000401};
402
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600403static const char *twl6040_hs_texts[] = {
404 "Off", "HS DAC", "Line-In amp"
405};
406
407static const struct soc_enum twl6040_hs_enum[] = {
408 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
409 twl6040_hs_texts),
410 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
411 twl6040_hs_texts),
412};
413
414static const char *twl6040_hf_texts[] = {
415 "Off", "HF DAC", "Line-In amp"
416};
417
418static const struct soc_enum twl6040_hf_enum[] = {
419 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
420 twl6040_hf_texts),
421 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
422 twl6040_hf_texts),
423};
424
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300425static const char *twl6040_vibrapath_texts[] = {
426 "Input FF", "Audio PDM"
427};
428
429static const struct soc_enum twl6040_vibra_enum[] = {
430 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
431 ARRAY_SIZE(twl6040_vibrapath_texts),
432 twl6040_vibrapath_texts),
433 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
434 ARRAY_SIZE(twl6040_vibrapath_texts),
435 twl6040_vibrapath_texts),
436};
437
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000438static const struct snd_kcontrol_new amicl_control =
439 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
440
441static const struct snd_kcontrol_new amicr_control =
442 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
443
444/* Headset DAC playback switches */
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600445static const struct snd_kcontrol_new hsl_mux_controls =
446 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000447
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600448static const struct snd_kcontrol_new hsr_mux_controls =
449 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000450
451/* Handsfree DAC playback switches */
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600452static const struct snd_kcontrol_new hfl_mux_controls =
453 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000454
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600455static const struct snd_kcontrol_new hfr_mux_controls =
456 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000457
Peter Ujfalusi317596a2011-09-22 11:05:49 +0300458static const struct snd_kcontrol_new ep_path_enable_control =
Lars-Peter Clausen290c3482013-10-06 13:43:51 +0200459 SOC_DAPM_SINGLE_VIRT("Switch", 1);
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500460
Peter Ujfalusifdb625f2011-09-22 11:05:52 +0300461static const struct snd_kcontrol_new auxl_switch_control =
462 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
463
464static const struct snd_kcontrol_new auxr_switch_control =
465 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
466
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300467/* Vibra playback switches */
468static const struct snd_kcontrol_new vibral_mux_controls =
469 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
470 snd_soc_dapm_get_enum_double,
471 twl6040_soc_dapm_put_vibra_enum);
472
473static const struct snd_kcontrol_new vibrar_mux_controls =
474 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
475 snd_soc_dapm_get_enum_double,
476 twl6040_soc_dapm_put_vibra_enum);
477
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600478/* Headset power mode */
Peter Ujfalusi7cca6062011-06-27 13:33:14 +0300479static const char *twl6040_power_mode_texts[] = {
Simon Wilson3e453652012-07-06 17:04:17 +0100480 "Low-Power", "High-Performance",
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600481};
482
Takashi Iwaia1d0d782014-02-18 10:30:20 +0100483static SOC_ENUM_SINGLE_EXT_DECL(twl6040_power_mode_enum,
484 twl6040_power_mode_texts);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600485
486static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
487 struct snd_ctl_elem_value *ucontrol)
488{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000489 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
490 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600491
492 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
493
494 return 0;
495}
496
497static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
498 struct snd_ctl_elem_value *ucontrol)
499{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000500 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
501 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600502 int high_perf = ucontrol->value.enumerated.item[0];
503 int ret = 0;
504
505 if (!priv->hs_power_mode_locked)
Kuninori Morimoto74803892018-01-29 04:20:09 +0000506 ret = headset_power_mode(component, high_perf);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600507
508 if (!ret)
509 priv->hs_power_mode = high_perf;
510
511 return ret;
512}
513
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300514static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
515 struct snd_ctl_elem_value *ucontrol)
516{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000517 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
518 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300519
520 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
521
522 return 0;
523}
524
525static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
526 struct snd_ctl_elem_value *ucontrol)
527{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000528 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
529 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300530
531 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300532
533 return 0;
534}
535
Kuninori Morimoto74803892018-01-29 04:20:09 +0000536int twl6040_get_dl1_gain(struct snd_soc_component *component)
Liam Girdwoode48b46b2012-01-11 12:43:24 +0000537{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000538 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Liam Girdwoode48b46b2012-01-11 12:43:24 +0000539
540 if (snd_soc_dapm_get_pin_status(dapm, "EP"))
541 return -1; /* -1dB */
542
543 if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
544 snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
545
Kuninori Morimoto74803892018-01-29 04:20:09 +0000546 u8 val = twl6040_read(component, TWL6040_REG_HSLCTL);
Liam Girdwoode48b46b2012-01-11 12:43:24 +0000547 if (val & TWL6040_HSDACMODE)
548 /* HSDACL in LP mode */
549 return -8; /* -8dB */
550 else
551 /* HSDACL in HP mode */
552 return -1; /* -1dB */
553 }
554 return 0; /* 0dB */
555}
556EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
557
Kuninori Morimoto74803892018-01-29 04:20:09 +0000558int twl6040_get_clk_id(struct snd_soc_component *component)
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300559{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000560 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300561
Peter Ujfalusiff593ca2011-07-04 10:35:23 +0300562 return priv->pll_power_mode;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300563}
564EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
565
Kuninori Morimoto74803892018-01-29 04:20:09 +0000566int twl6040_get_trim_value(struct snd_soc_component *component, enum twl6040_trim trim)
Peter Ujfalusidb4aabc2011-09-26 16:05:57 +0300567{
568 if (unlikely(trim >= TWL6040_TRIM_INVAL))
569 return -EINVAL;
570
Kuninori Morimoto74803892018-01-29 04:20:09 +0000571 return twl6040_read(component, TWL6040_REG_TRIM1 + trim);
Peter Ujfalusidb4aabc2011-09-26 16:05:57 +0300572}
573EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
574
Kuninori Morimoto74803892018-01-29 04:20:09 +0000575int twl6040_get_hs_step_size(struct snd_soc_component *component)
Liam Girdwood08656912012-01-09 12:10:16 +0000576{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000577 struct twl6040 *twl6040 = to_twl6040(component);
Liam Girdwood08656912012-01-09 12:10:16 +0000578
Peter Ujfalusibe4ac002012-07-16 11:49:43 +0200579 if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
Liam Girdwood08656912012-01-09 12:10:16 +0000580 /* For ES under ES_1.3 HS step is 2 mV */
581 return 2;
582 else
583 /* For ES_1.3 HS step is 1 mV */
584 return 1;
585}
586EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
587
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000588static const struct snd_kcontrol_new twl6040_snd_controls[] = {
589 /* Capture gains */
590 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
591 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
592 SOC_DOUBLE_TLV("Capture Volume",
593 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
594
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600595 /* AFM gains */
596 SOC_DOUBLE_TLV("Aux FM Volume",
Liam Girdwood1f71a3b2011-03-28 19:23:23 +0100597 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600598
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000599 /* Playback gains */
Peter Ujfalusi3bb8a812012-05-04 15:17:20 +0300600 SOC_DOUBLE_TLV("Headset Playback Volume",
601 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
602 SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
603 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500604 SOC_SINGLE_TLV("Earphone Playback Volume",
605 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600606
Peter Ujfalusi7cca6062011-06-27 13:33:14 +0300607 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600608 twl6040_headset_power_get_enum,
609 twl6040_headset_power_put_enum),
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300610
Peter Ujfalusi0636e8b2017-04-03 13:12:39 +0300611 /* Left HS PDM data routed to Right HSDAC */
612 SOC_SINGLE("Headset Mono to Stereo Playback Switch",
613 TWL6040_REG_HSRCTL, 7, 1, 0),
614
615 /* Left HF PDM data routed to Right HFDAC */
616 SOC_SINGLE("Handsfree Mono to Stereo Playback Switch",
617 TWL6040_REG_HFRCTL, 5, 1, 0),
618
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300619 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
620 twl6040_pll_get_enum, twl6040_pll_put_enum),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000621};
622
623static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
624 /* Inputs */
625 SND_SOC_DAPM_INPUT("MAINMIC"),
626 SND_SOC_DAPM_INPUT("HSMIC"),
627 SND_SOC_DAPM_INPUT("SUBMIC"),
628 SND_SOC_DAPM_INPUT("AFML"),
629 SND_SOC_DAPM_INPUT("AFMR"),
630
631 /* Outputs */
632 SND_SOC_DAPM_OUTPUT("HSOL"),
633 SND_SOC_DAPM_OUTPUT("HSOR"),
634 SND_SOC_DAPM_OUTPUT("HFL"),
635 SND_SOC_DAPM_OUTPUT("HFR"),
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500636 SND_SOC_DAPM_OUTPUT("EP"),
Peter Ujfalusifdb625f2011-09-22 11:05:52 +0300637 SND_SOC_DAPM_OUTPUT("AUXL"),
638 SND_SOC_DAPM_OUTPUT("AUXR"),
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300639 SND_SOC_DAPM_OUTPUT("VIBRAL"),
640 SND_SOC_DAPM_OUTPUT("VIBRAR"),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000641
642 /* Analog input muxes for the capture amplifiers */
643 SND_SOC_DAPM_MUX("Analog Left Capture Route",
644 SND_SOC_NOPM, 0, 0, &amicl_control),
645 SND_SOC_DAPM_MUX("Analog Right Capture Route",
646 SND_SOC_NOPM, 0, 0, &amicr_control),
647
648 /* Analog capture PGAs */
649 SND_SOC_DAPM_PGA("MicAmpL",
650 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
651 SND_SOC_DAPM_PGA("MicAmpR",
652 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
653
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600654 /* Auxiliary FM PGAs */
655 SND_SOC_DAPM_PGA("AFMAmpL",
656 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
657 SND_SOC_DAPM_PGA("AFMAmpR",
658 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
659
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000660 /* ADCs */
Peter Ujfalusi805238b2012-09-20 16:32:15 +0300661 SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
662 SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000663
664 /* Microphone bias */
Peter Ujfalusi778cee72011-12-22 12:47:21 +0200665 SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
666 TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
667 SND_SOC_DAPM_SUPPLY("Main Mic Bias",
668 TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
669 SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
670 TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
671 SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
672 TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000673
674 /* DACs */
Peter Ujfalusi805238b2012-09-20 16:32:15 +0300675 SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
676 SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
677 SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
678 SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300679 /* Virtual DAC for vibra path (DL4 channel) */
Peter Ujfalusi805238b2012-09-20 16:32:15 +0300680 SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000681
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300682 SND_SOC_DAPM_MUX("Handsfree Left Playback",
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600683 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300684 SND_SOC_DAPM_MUX("Handsfree Right Playback",
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600685 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
686 /* Analog playback Muxes */
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300687 SND_SOC_DAPM_MUX("Headset Left Playback",
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600688 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300689 SND_SOC_DAPM_MUX("Headset Right Playback",
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600690 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000691
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300692 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
693 &vibral_mux_controls),
694 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
695 &vibrar_mux_controls),
696
Peter Ujfalusi317596a2011-09-22 11:05:49 +0300697 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
698 &ep_path_enable_control),
Peter Ujfalusifdb625f2011-09-22 11:05:52 +0300699 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
700 &auxl_switch_control),
701 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
702 &auxr_switch_control),
Peter Ujfalusi317596a2011-09-22 11:05:49 +0300703
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500704 /* Analog playback drivers */
Peter Ujfalusi3bb8a812012-05-04 15:17:20 +0300705 SND_SOC_DAPM_OUT_DRV("HF Left Driver",
706 TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
707 SND_SOC_DAPM_OUT_DRV("HF Right Driver",
708 TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
709 SND_SOC_DAPM_OUT_DRV("HS Left Driver",
710 TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
711 SND_SOC_DAPM_OUT_DRV("HS Right Driver",
712 TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
Peter Ujfalusi317596a2011-09-22 11:05:49 +0300713 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
714 TWL6040_REG_EARCTL, 0, 0, NULL, 0,
Peter Ujfalusi694b0002011-10-13 15:05:44 +0300715 twl6040_ep_drv_event,
Peter Ujfalusiaa1a4102011-10-13 15:05:43 +0300716 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300717 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
718 TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
719 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
720 TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
721
722 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
723 NULL, 0),
724 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
725 NULL, 0),
Peter Ujfalusi33b68162011-10-12 14:46:02 +0300726 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
727 twl6040_hs_dac_event,
728 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000729
730 /* Analog playback PGAs */
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300731 SND_SOC_DAPM_PGA("HF Left PGA",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000732 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300733 SND_SOC_DAPM_PGA("HF Right PGA",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000734 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
735
736};
737
738static const struct snd_soc_dapm_route intercon[] = {
Peter Ujfalusi805238b2012-09-20 16:32:15 +0300739 /* Stream -> DAC mapping */
740 {"HSDAC Left", NULL, "Legacy Playback"},
741 {"HSDAC Left", NULL, "Headset Playback"},
742 {"HSDAC Right", NULL, "Legacy Playback"},
743 {"HSDAC Right", NULL, "Headset Playback"},
744
745 {"HFDAC Left", NULL, "Legacy Playback"},
746 {"HFDAC Left", NULL, "Handsfree Playback"},
747 {"HFDAC Right", NULL, "Legacy Playback"},
748 {"HFDAC Right", NULL, "Handsfree Playback"},
749
750 {"VIBRA DAC", NULL, "Legacy Playback"},
751 {"VIBRA DAC", NULL, "Vibra Playback"},
752
753 /* ADC -> Stream mapping */
Peter Ujfalusi07ac2292012-10-04 14:15:02 +0300754 {"Legacy Capture" , NULL, "ADC Left"},
755 {"Capture", NULL, "ADC Left"},
756 {"Legacy Capture", NULL, "ADC Right"},
757 {"Capture" , NULL, "ADC Right"},
Peter Ujfalusi805238b2012-09-20 16:32:15 +0300758
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000759 /* Capture path */
760 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
761 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
762 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
763
764 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
765 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
766 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
767
768 {"MicAmpL", NULL, "Analog Left Capture Route"},
769 {"MicAmpR", NULL, "Analog Right Capture Route"},
770
771 {"ADC Left", NULL, "MicAmpL"},
772 {"ADC Right", NULL, "MicAmpR"},
773
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600774 /* AFM path */
Peter Ujfalusi5bf692d2011-09-22 11:05:47 +0300775 {"AFMAmpL", NULL, "AFML"},
776 {"AFMAmpR", NULL, "AFMR"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000777
Peter Ujfalusi33b68162011-10-12 14:46:02 +0300778 {"HSDAC Left", NULL, "HSDAC Power"},
779 {"HSDAC Right", NULL, "HSDAC Power"},
780
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300781 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
782 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600783
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300784 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
785 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600786
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300787 {"HS Left Driver", NULL, "Headset Left Playback"},
788 {"HS Right Driver", NULL, "Headset Right Playback"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000789
Peter Ujfalusi45b0f602011-09-22 11:05:51 +0300790 {"HSOL", NULL, "HS Left Driver"},
791 {"HSOR", NULL, "HS Right Driver"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000792
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500793 /* Earphone playback path */
Peter Ujfalusi317596a2011-09-22 11:05:49 +0300794 {"Earphone Playback", "Switch", "HSDAC Left"},
795 {"Earphone Driver", NULL, "Earphone Playback"},
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500796 {"EP", NULL, "Earphone Driver"},
797
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300798 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
799 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000800
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300801 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
802 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600803
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300804 {"HF Left PGA", NULL, "Handsfree Left Playback"},
805 {"HF Right PGA", NULL, "Handsfree Right Playback"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000806
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300807 {"HF Left Driver", NULL, "HF Left PGA"},
808 {"HF Right Driver", NULL, "HF Right PGA"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000809
Peter Ujfalusidf11ce22011-09-22 11:05:50 +0300810 {"HFL", NULL, "HF Left Driver"},
811 {"HFR", NULL, "HF Right Driver"},
Peter Ujfalusifdb625f2011-09-22 11:05:52 +0300812
813 {"AUXL Playback", "Switch", "HF Left PGA"},
814 {"AUXR Playback", "Switch", "HF Right PGA"},
815
816 {"AUXL", NULL, "AUXL Playback"},
817 {"AUXR", NULL, "AUXR Playback"},
Peter Ujfalusi67c34132011-10-12 11:57:57 +0300818
819 /* Vibrator paths */
820 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
821 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
822
823 {"Vibra Left Driver", NULL, "Vibra Left Playback"},
824 {"Vibra Right Driver", NULL, "Vibra Right Playback"},
825 {"Vibra Left Driver", NULL, "Vibra Left Control"},
826 {"Vibra Right Driver", NULL, "Vibra Right Control"},
827
828 {"VIBRAL", NULL, "Vibra Left Driver"},
829 {"VIBRAR", NULL, "Vibra Right Driver"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000830};
831
Kuninori Morimoto74803892018-01-29 04:20:09 +0000832static int twl6040_set_bias_level(struct snd_soc_component *component,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000833 enum snd_soc_bias_level level)
834{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000835 struct twl6040 *twl6040 = to_twl6040(component);
836 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusi1135ef12016-05-11 14:14:05 +0300837 int ret = 0;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000838
839 switch (level) {
840 case SND_SOC_BIAS_ON:
841 break;
842 case SND_SOC_BIAS_PREPARE:
843 break;
844 case SND_SOC_BIAS_STANDBY:
Peter Ujfalusi1135ef12016-05-11 14:14:05 +0300845 if (priv->codec_powered) {
846 /* Select low power PLL in standby */
847 ret = twl6040_set_pll(twl6040, TWL6040_SYSCLK_SEL_LPPLL,
848 32768, 19200000);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000849 break;
Peter Ujfalusi1135ef12016-05-11 14:14:05 +0300850 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000851
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500852 ret = twl6040_power(twl6040, 1);
853 if (ret)
Peter Ujfalusi1135ef12016-05-11 14:14:05 +0300854 break;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000855
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500856 priv->codec_powered = 1;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000857
Olaya, Margarita65b7cec2010-12-14 19:18:36 -0600858 /* Set external boost GPO */
Kuninori Morimoto74803892018-01-29 04:20:09 +0000859 twl6040_write(component, TWL6040_REG_GPOCTL, 0x02);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000860 break;
861 case SND_SOC_BIAS_OFF:
862 if (!priv->codec_powered)
863 break;
864
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500865 twl6040_power(twl6040, 0);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000866 priv->codec_powered = 0;
867 break;
868 }
869
Peter Ujfalusi1135ef12016-05-11 14:14:05 +0300870 return ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000871}
872
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000873static int twl6040_startup(struct snd_pcm_substream *substream,
874 struct snd_soc_dai *dai)
875{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000876 struct snd_soc_component *component = dai->component;
877 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000878
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000879 snd_pcm_hw_constraint_list(substream->runtime, 0,
880 SNDRV_PCM_HW_PARAM_RATE,
Peter Ujfalusif53c3462011-06-29 13:28:18 +0300881 &sysclk_constraints[priv->pll_power_mode]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000882
883 return 0;
884}
885
886static int twl6040_hw_params(struct snd_pcm_substream *substream,
887 struct snd_pcm_hw_params *params,
888 struct snd_soc_dai *dai)
889{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000890 struct snd_soc_component *component = dai->component;
891 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000892 int rate;
893
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000894 rate = params_rate(params);
895 switch (rate) {
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -0600896 case 11250:
897 case 22500:
898 case 44100:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000899 case 88200:
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300900 /* These rates are not supported when HPPLL is in use */
901 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
Kuninori Morimoto74803892018-01-29 04:20:09 +0000902 dev_err(component->dev, "HPPLL does not support rate %d\n",
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300903 rate);
904 return -EINVAL;
905 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000906 priv->sysclk = 17640000;
907 break;
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -0600908 case 8000:
909 case 16000:
910 case 32000:
911 case 48000:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000912 case 96000:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000913 priv->sysclk = 19200000;
914 break;
915 default:
Kuninori Morimoto74803892018-01-29 04:20:09 +0000916 dev_err(component->dev, "unsupported rate %d\n", rate);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000917 return -EINVAL;
918 }
919
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000920 return 0;
921}
922
Olaya, Margarita4e624d02010-12-10 21:05:54 -0600923static int twl6040_prepare(struct snd_pcm_substream *substream,
924 struct snd_soc_dai *dai)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000925{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000926 struct snd_soc_component *component = dai->component;
927 struct twl6040 *twl6040 = to_twl6040(component);
928 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300929 int ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000930
Olaya, Margarita4e624d02010-12-10 21:05:54 -0600931 if (!priv->sysclk) {
Kuninori Morimoto74803892018-01-29 04:20:09 +0000932 dev_err(component->dev,
Olaya, Margarita4e624d02010-12-10 21:05:54 -0600933 "no mclk configured, call set_sysclk() on init\n");
934 return -EINVAL;
935 }
936
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300937 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
938 if (ret) {
Kuninori Morimoto74803892018-01-29 04:20:09 +0000939 dev_err(component->dev, "Can not set PLL (%d)\n", ret);
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300940 return -EPERM;
941 }
942
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000943 return 0;
944}
945
946static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
947 int clk_id, unsigned int freq, int dir)
948{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000949 struct snd_soc_component *component = codec_dai->component;
950 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000951
952 switch (clk_id) {
953 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000954 case TWL6040_SYSCLK_SEL_HPPLL:
Peter Ujfalusi753621c2011-07-03 02:06:07 +0300955 priv->pll = clk_id;
956 priv->clk_in = freq;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000957 break;
958 default:
Kuninori Morimoto74803892018-01-29 04:20:09 +0000959 dev_err(component->dev, "unknown clk_id %d\n", clk_id);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000960 return -EINVAL;
961 }
962
963 return 0;
964}
965
Kuninori Morimoto74803892018-01-29 04:20:09 +0000966static void twl6040_mute_path(struct snd_soc_component *component, enum twl6040_dai_id id,
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200967 int mute)
968{
Kuninori Morimoto74803892018-01-29 04:20:09 +0000969 struct twl6040 *twl6040 = to_twl6040(component);
970 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200971 int hslctl, hsrctl, earctl;
972 int hflctl, hfrctl;
973
974 switch (id) {
975 case TWL6040_DAI_DL1:
Kuninori Morimoto74803892018-01-29 04:20:09 +0000976 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL);
977 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL);
978 earctl = twl6040_read(component, TWL6040_REG_EARCTL);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200979
980 if (mute) {
981 /* Power down drivers and DACs */
982 earctl &= ~0x01;
983 hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
984 hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
985
986 }
987
988 twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
989 twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
990 twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
991 priv->dl1_unmuted = !mute;
992 break;
993 case TWL6040_DAI_DL2:
Kuninori Morimoto74803892018-01-29 04:20:09 +0000994 hflctl = twl6040_read(component, TWL6040_REG_HFLCTL);
995 hfrctl = twl6040_read(component, TWL6040_REG_HFRCTL);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +0200996
997 if (mute) {
998 /* Power down drivers and DACs */
999 hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
Peter Ujfalusi45c04702016-05-18 16:19:01 +03001000 TWL6040_HFDRVENA | TWL6040_HFSWENA);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001001 hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
Peter Ujfalusi45c04702016-05-18 16:19:01 +03001002 TWL6040_HFDRVENA | TWL6040_HFSWENA);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001003 }
1004
1005 twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
1006 twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
1007 priv->dl2_unmuted = !mute;
1008 break;
1009 default:
1010 break;
Sachin Kamatbf551412013-09-13 16:16:17 +05301011 }
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001012}
1013
1014static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
1015{
1016 switch (dai->id) {
1017 case TWL6040_DAI_LEGACY:
Kuninori Morimoto74803892018-01-29 04:20:09 +00001018 twl6040_mute_path(dai->component, TWL6040_DAI_DL1, mute);
1019 twl6040_mute_path(dai->component, TWL6040_DAI_DL2, mute);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001020 break;
1021 case TWL6040_DAI_DL1:
1022 case TWL6040_DAI_DL2:
Kuninori Morimoto74803892018-01-29 04:20:09 +00001023 twl6040_mute_path(dai->component, dai->id, mute);
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001024 break;
1025 default:
1026 break;
1027 }
1028
1029 return 0;
1030}
1031
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001032static const struct snd_soc_dai_ops twl6040_dai_ops = {
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001033 .startup = twl6040_startup,
1034 .hw_params = twl6040_hw_params,
Olaya, Margarita4e624d02010-12-10 21:05:54 -06001035 .prepare = twl6040_prepare,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001036 .set_sysclk = twl6040_set_dai_sysclk,
Peter Ujfalusi98c5fb12013-06-24 15:42:06 +02001037 .digital_mute = twl6040_digital_mute,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001038};
1039
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001040static struct snd_soc_dai_driver twl6040_dai[] = {
1041{
Peter Ujfalusid13f1fe2011-09-22 11:05:53 +03001042 .name = "twl6040-legacy",
Peter Ujfalusi68897492013-06-24 15:42:05 +02001043 .id = TWL6040_DAI_LEGACY,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001044 .playback = {
Peter Ujfalusi805238b2012-09-20 16:32:15 +03001045 .stream_name = "Legacy Playback",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001046 .channels_min = 1,
Peter Ujfalusicdd50542011-09-15 15:59:19 +03001047 .channels_max = 5,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001048 .rates = TWL6040_RATES,
1049 .formats = TWL6040_FORMATS,
1050 },
1051 .capture = {
Peter Ujfalusi805238b2012-09-20 16:32:15 +03001052 .stream_name = "Legacy Capture",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001053 .channels_min = 1,
1054 .channels_max = 2,
1055 .rates = TWL6040_RATES,
1056 .formats = TWL6040_FORMATS,
1057 },
1058 .ops = &twl6040_dai_ops,
Peter Ujfalusi21385ee2011-07-05 22:35:53 +03001059},
1060{
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001061 .name = "twl6040-ul",
Peter Ujfalusi68897492013-06-24 15:42:05 +02001062 .id = TWL6040_DAI_UL,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001063 .capture = {
1064 .stream_name = "Capture",
1065 .channels_min = 1,
1066 .channels_max = 2,
1067 .rates = TWL6040_RATES,
1068 .formats = TWL6040_FORMATS,
1069 },
1070 .ops = &twl6040_dai_ops,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001071},
1072{
1073 .name = "twl6040-dl1",
Peter Ujfalusi68897492013-06-24 15:42:05 +02001074 .id = TWL6040_DAI_DL1,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001075 .playback = {
1076 .stream_name = "Headset Playback",
1077 .channels_min = 1,
1078 .channels_max = 2,
1079 .rates = TWL6040_RATES,
1080 .formats = TWL6040_FORMATS,
1081 },
1082 .ops = &twl6040_dai_ops,
1083},
1084{
1085 .name = "twl6040-dl2",
Peter Ujfalusi68897492013-06-24 15:42:05 +02001086 .id = TWL6040_DAI_DL2,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001087 .playback = {
1088 .stream_name = "Handsfree Playback",
1089 .channels_min = 1,
1090 .channels_max = 2,
1091 .rates = TWL6040_RATES,
1092 .formats = TWL6040_FORMATS,
1093 },
1094 .ops = &twl6040_dai_ops,
1095},
1096{
1097 .name = "twl6040-vib",
Peter Ujfalusi68897492013-06-24 15:42:05 +02001098 .id = TWL6040_DAI_VIB,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001099 .playback = {
1100 .stream_name = "Vibra Playback",
Peter Ujfalusid8dd0322011-09-15 15:59:18 +03001101 .channels_min = 1,
1102 .channels_max = 1,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001103 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1104 .formats = TWL6040_FORMATS,
1105 },
1106 .ops = &twl6040_dai_ops,
1107},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001108};
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001109
Kuninori Morimoto74803892018-01-29 04:20:09 +00001110static int twl6040_probe(struct snd_soc_component *component)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001111{
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001112 struct twl6040_data *priv;
Kuninori Morimoto74803892018-01-29 04:20:09 +00001113 struct platform_device *pdev = to_platform_device(component->dev);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001114 int ret = 0;
1115
Kuninori Morimoto74803892018-01-29 04:20:09 +00001116 priv = devm_kzalloc(component->dev, sizeof(*priv), GFP_KERNEL);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001117 if (priv == NULL)
1118 return -ENOMEM;
Peter Ujfalusi9523fcd2013-01-11 17:01:01 +01001119
Kuninori Morimoto74803892018-01-29 04:20:09 +00001120 snd_soc_component_set_drvdata(component, priv);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001121
Kuninori Morimoto74803892018-01-29 04:20:09 +00001122 priv->component = component;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001123
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001124 priv->plug_irq = platform_get_irq(pdev, 0);
1125 if (priv->plug_irq < 0) {
Kuninori Morimoto74803892018-01-29 04:20:09 +00001126 dev_err(component->dev, "invalid irq: %d\n", priv->plug_irq);
Gustavo A. R. Silvacfe267d2017-06-30 17:26:48 -05001127 return priv->plug_irq;
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001128 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001129
Peter Ujfalusi46dd0b92011-09-26 16:26:27 +03001130 INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001131
1132 mutex_init(&priv->mutex);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001133
Peter Ujfalusif60596d2013-06-24 15:42:03 +02001134 ret = request_threaded_irq(priv->plug_irq, NULL,
Fabio Estevam208ba892015-05-12 01:22:59 -03001135 twl6040_audio_handler,
1136 IRQF_NO_SUSPEND | IRQF_ONESHOT,
Kuninori Morimoto74803892018-01-29 04:20:09 +00001137 "twl6040_irq_plug", component);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001138 if (ret) {
Kuninori Morimoto74803892018-01-29 04:20:09 +00001139 dev_err(component->dev, "PLUG IRQ request failed: %d\n", ret);
Peter Ujfalusida2107d2013-01-11 17:01:02 +01001140 return ret;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001141 }
1142
Kuninori Morimoto74803892018-01-29 04:20:09 +00001143 snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
1144 twl6040_init_chip(component);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001145
Peter Ujfalusi626bcac2013-11-29 16:03:47 +02001146 return 0;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001147}
1148
Kuninori Morimoto74803892018-01-29 04:20:09 +00001149static void twl6040_remove(struct snd_soc_component *component)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001150{
Kuninori Morimoto74803892018-01-29 04:20:09 +00001151 struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
Peter Ujfalusif60596d2013-06-24 15:42:03 +02001152
Kuninori Morimoto74803892018-01-29 04:20:09 +00001153 free_irq(priv->plug_irq, component);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001154}
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001155
Kuninori Morimoto74803892018-01-29 04:20:09 +00001156static const struct snd_soc_component_driver soc_component_dev_twl6040 = {
1157 .probe = twl6040_probe,
1158 .remove = twl6040_remove,
1159 .read = twl6040_read,
Peter Ujfalusic9d06662018-03-12 16:24:22 +02001160 .write = twl6040_write,
Kuninori Morimoto74803892018-01-29 04:20:09 +00001161 .set_bias_level = twl6040_set_bias_level,
1162 .controls = twl6040_snd_controls,
1163 .num_controls = ARRAY_SIZE(twl6040_snd_controls),
1164 .dapm_widgets = twl6040_dapm_widgets,
1165 .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
1166 .dapm_routes = intercon,
1167 .num_dapm_routes = ARRAY_SIZE(intercon),
1168 .suspend_bias_off = 1,
1169 .idle_bias_on = 1,
1170 .endianness = 1,
1171 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172};
1173
Bill Pemberton7a79e942012-12-07 09:26:37 -05001174static int twl6040_codec_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175{
Kuninori Morimoto74803892018-01-29 04:20:09 +00001176 return devm_snd_soc_register_component(&pdev->dev,
1177 &soc_component_dev_twl6040,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001178 twl6040_dai, ARRAY_SIZE(twl6040_dai));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001179}
1180
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001181static struct platform_driver twl6040_codec_driver = {
1182 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183 .name = "twl6040-codec",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001184 },
1185 .probe = twl6040_codec_probe,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001186};
1187
Mark Brown5bbcc3c2011-11-23 22:52:08 +00001188module_platform_driver(twl6040_codec_driver);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001189
1190MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1191MODULE_AUTHOR("Misael Lopez Cruz");
1192MODULE_LICENSE("GPL");