blob: 0563a49cc5e4b3ec69901aab8593b76f5bc1c6bd [file] [log] [blame]
Andrew F. Davisb1c52b72017-11-29 15:32:42 -06001// SPDX-License-Identifier: GPL-2.0
Jyri Sarhae00447f2014-03-11 12:57:32 +02002/*
Andrew F. Davisb1c52b72017-11-29 15:32:42 -06003 * ALSA SoC TLV320AIC31xx CODEC Driver
Jyri Sarhae00447f2014-03-11 12:57:32 +02004 *
Andrew F. Davisb1c52b72017-11-29 15:32:42 -06005 * Copyright (C) 2014-2017 Texas Instruments Incorporated - http://www.ti.com/
6 * Jyri Sarha <jsarha@ti.com>
Jyri Sarhae00447f2014-03-11 12:57:32 +02007 *
8 * Based on ground work by: Ajit Kulkarni <x0175765@ti.com>
9 *
Andrew F. Davisb1c52b72017-11-29 15:32:42 -060010 * The TLV320AIC31xx series of audio codecs are low-power, highly integrated
11 * high performance codecs which provides a stereo DAC, a mono ADC,
Jyri Sarhae00447f2014-03-11 12:57:32 +020012 * and mono/stereo Class-D speaker driver.
13 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20#include <linux/i2c.h>
21#include <linux/gpio.h>
22#include <linux/regulator/consumer.h>
Bastien Noceraf5cc1772016-04-19 18:00:20 +020023#include <linux/acpi.h>
Sachin Kamat0faabc42014-04-04 11:29:12 +053024#include <linux/of.h>
Jyri Sarhae00447f2014-03-11 12:57:32 +020025#include <linux/of_gpio.h>
26#include <linux/slab.h>
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
31#include <sound/initval.h>
32#include <sound/tlv.h>
33#include <dt-bindings/sound/tlv320aic31xx-micbias.h>
34
35#include "tlv320aic31xx.h"
36
37static const struct reg_default aic31xx_reg_defaults[] = {
38 { AIC31XX_CLKMUX, 0x00 },
39 { AIC31XX_PLLPR, 0x11 },
40 { AIC31XX_PLLJ, 0x04 },
41 { AIC31XX_PLLDMSB, 0x00 },
42 { AIC31XX_PLLDLSB, 0x00 },
43 { AIC31XX_NDAC, 0x01 },
44 { AIC31XX_MDAC, 0x01 },
45 { AIC31XX_DOSRMSB, 0x00 },
46 { AIC31XX_DOSRLSB, 0x80 },
47 { AIC31XX_NADC, 0x01 },
48 { AIC31XX_MADC, 0x01 },
49 { AIC31XX_AOSR, 0x80 },
50 { AIC31XX_IFACE1, 0x00 },
51 { AIC31XX_DATA_OFFSET, 0x00 },
52 { AIC31XX_IFACE2, 0x00 },
53 { AIC31XX_BCLKN, 0x01 },
54 { AIC31XX_DACSETUP, 0x14 },
55 { AIC31XX_DACMUTE, 0x0c },
56 { AIC31XX_LDACVOL, 0x00 },
57 { AIC31XX_RDACVOL, 0x00 },
58 { AIC31XX_ADCSETUP, 0x00 },
59 { AIC31XX_ADCFGA, 0x80 },
60 { AIC31XX_ADCVOL, 0x00 },
61 { AIC31XX_HPDRIVER, 0x04 },
62 { AIC31XX_SPKAMP, 0x06 },
63 { AIC31XX_DACMIXERROUTE, 0x00 },
64 { AIC31XX_LANALOGHPL, 0x7f },
65 { AIC31XX_RANALOGHPR, 0x7f },
66 { AIC31XX_LANALOGSPL, 0x7f },
67 { AIC31XX_RANALOGSPR, 0x7f },
68 { AIC31XX_HPLGAIN, 0x02 },
69 { AIC31XX_HPRGAIN, 0x02 },
70 { AIC31XX_SPLGAIN, 0x00 },
71 { AIC31XX_SPRGAIN, 0x00 },
72 { AIC31XX_MICBIAS, 0x00 },
73 { AIC31XX_MICPGA, 0x80 },
74 { AIC31XX_MICPGAPI, 0x00 },
75 { AIC31XX_MICPGAMI, 0x00 },
76};
77
78static bool aic31xx_volatile(struct device *dev, unsigned int reg)
79{
80 switch (reg) {
81 case AIC31XX_PAGECTL: /* regmap implementation requires this */
82 case AIC31XX_RESET: /* always clears after write */
83 case AIC31XX_OT_FLAG:
84 case AIC31XX_ADCFLAG:
85 case AIC31XX_DACFLAG1:
86 case AIC31XX_DACFLAG2:
87 case AIC31XX_OFFLAG: /* Sticky interrupt flags */
88 case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
89 case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
90 case AIC31XX_INTRDACFLAG2:
91 case AIC31XX_INTRADCFLAG2:
92 return true;
93 }
94 return false;
95}
96
97static bool aic31xx_writeable(struct device *dev, unsigned int reg)
98{
99 switch (reg) {
100 case AIC31XX_OT_FLAG:
101 case AIC31XX_ADCFLAG:
102 case AIC31XX_DACFLAG1:
103 case AIC31XX_DACFLAG2:
104 case AIC31XX_OFFLAG: /* Sticky interrupt flags */
105 case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
106 case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
107 case AIC31XX_INTRDACFLAG2:
108 case AIC31XX_INTRADCFLAG2:
109 return false;
110 }
111 return true;
112}
113
114static const struct regmap_range_cfg aic31xx_ranges[] = {
115 {
116 .range_min = 0,
117 .range_max = 12 * 128,
118 .selector_reg = AIC31XX_PAGECTL,
119 .selector_mask = 0xff,
120 .selector_shift = 0,
121 .window_start = 0,
122 .window_len = 128,
123 },
124};
125
Mark Brown9296f4d2014-03-13 17:44:22 +0000126static const struct regmap_config aic31xx_i2c_regmap = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200127 .reg_bits = 8,
128 .val_bits = 8,
129 .writeable_reg = aic31xx_writeable,
130 .volatile_reg = aic31xx_volatile,
131 .reg_defaults = aic31xx_reg_defaults,
132 .num_reg_defaults = ARRAY_SIZE(aic31xx_reg_defaults),
133 .cache_type = REGCACHE_RBTREE,
134 .ranges = aic31xx_ranges,
135 .num_ranges = ARRAY_SIZE(aic31xx_ranges),
136 .max_register = 12 * 128,
137};
138
139#define AIC31XX_NUM_SUPPLIES 6
140static const char * const aic31xx_supply_names[AIC31XX_NUM_SUPPLIES] = {
141 "HPVDD",
142 "SPRVDD",
143 "SPLVDD",
144 "AVDD",
145 "IOVDD",
146 "DVDD",
147};
148
149struct aic31xx_disable_nb {
150 struct notifier_block nb;
151 struct aic31xx_priv *aic31xx;
152};
153
154struct aic31xx_priv {
155 struct snd_soc_codec *codec;
156 u8 i2c_regs_status;
157 struct device *dev;
158 struct regmap *regmap;
159 struct aic31xx_pdata pdata;
160 struct regulator_bulk_data supplies[AIC31XX_NUM_SUPPLIES];
161 struct aic31xx_disable_nb disable_nb[AIC31XX_NUM_SUPPLIES];
162 unsigned int sysclk;
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300163 u8 p_div;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200164 int rate_div_line;
165};
166
167struct aic31xx_rate_divs {
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300168 u32 mclk_p;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200169 u32 rate;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200170 u8 pll_j;
171 u16 pll_d;
172 u16 dosr;
173 u8 ndac;
174 u8 mdac;
175 u8 aosr;
176 u8 nadc;
177 u8 madc;
178};
179
180/* ADC dividers can be disabled by cofiguring them to 0 */
181static const struct aic31xx_rate_divs aic31xx_divs[] = {
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300182 /* mclk/p rate pll: j d dosr ndac mdac aors nadc madc */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200183 /* 8k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300184 {12000000, 8000, 8, 1920, 128, 48, 2, 128, 48, 2},
185 {12000000, 8000, 8, 1920, 128, 32, 3, 128, 32, 3},
186 {12500000, 8000, 7, 8643, 128, 48, 2, 128, 48, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200187 /* 11.025k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300188 {12000000, 11025, 7, 5264, 128, 32, 2, 128, 32, 2},
189 {12000000, 11025, 8, 4672, 128, 24, 3, 128, 24, 3},
190 {12500000, 11025, 7, 2253, 128, 32, 2, 128, 32, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200191 /* 16k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300192 {12000000, 16000, 8, 1920, 128, 24, 2, 128, 24, 2},
193 {12000000, 16000, 8, 1920, 128, 16, 3, 128, 16, 3},
194 {12500000, 16000, 7, 8643, 128, 24, 2, 128, 24, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200195 /* 22.05k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300196 {12000000, 22050, 7, 5264, 128, 16, 2, 128, 16, 2},
197 {12000000, 22050, 8, 4672, 128, 12, 3, 128, 12, 3},
198 {12500000, 22050, 7, 2253, 128, 16, 2, 128, 16, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200199 /* 32k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300200 {12000000, 32000, 8, 1920, 128, 12, 2, 128, 12, 2},
201 {12000000, 32000, 8, 1920, 128, 8, 3, 128, 8, 3},
202 {12500000, 32000, 7, 8643, 128, 12, 2, 128, 12, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200203 /* 44.1k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300204 {12000000, 44100, 7, 5264, 128, 8, 2, 128, 8, 2},
205 {12000000, 44100, 8, 4672, 128, 6, 3, 128, 6, 3},
206 {12500000, 44100, 7, 2253, 128, 8, 2, 128, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200207 /* 48k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300208 {12000000, 48000, 8, 1920, 128, 8, 2, 128, 8, 2},
209 {12000000, 48000, 7, 6800, 96, 5, 4, 96, 5, 4},
210 {12500000, 48000, 7, 8643, 128, 8, 2, 128, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200211 /* 88.2k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300212 {12000000, 88200, 7, 5264, 64, 8, 2, 64, 8, 2},
213 {12000000, 88200, 8, 4672, 64, 6, 3, 64, 6, 3},
214 {12500000, 88200, 7, 2253, 64, 8, 2, 64, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200215 /* 96k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300216 {12000000, 96000, 8, 1920, 64, 8, 2, 64, 8, 2},
217 {12000000, 96000, 7, 6800, 48, 5, 4, 48, 5, 4},
218 {12500000, 96000, 7, 8643, 64, 8, 2, 64, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200219 /* 176.4k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300220 {12000000, 176400, 7, 5264, 32, 8, 2, 32, 8, 2},
221 {12000000, 176400, 8, 4672, 32, 6, 3, 32, 6, 3},
222 {12500000, 176400, 7, 2253, 32, 8, 2, 32, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200223 /* 192k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300224 {12000000, 192000, 8, 1920, 32, 8, 2, 32, 8, 2},
225 {12000000, 192000, 7, 6800, 24, 5, 4, 24, 5, 4},
226 {12500000, 192000, 7, 8643, 32, 8, 2, 32, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200227};
228
229static const char * const ldac_in_text[] = {
230 "Off", "Left Data", "Right Data", "Mono"
231};
232
233static const char * const rdac_in_text[] = {
234 "Off", "Right Data", "Left Data", "Mono"
235};
236
237static SOC_ENUM_SINGLE_DECL(ldac_in_enum, AIC31XX_DACSETUP, 4, ldac_in_text);
238
239static SOC_ENUM_SINGLE_DECL(rdac_in_enum, AIC31XX_DACSETUP, 2, rdac_in_text);
240
241static const char * const mic_select_text[] = {
242 "Off", "FFR 10 Ohm", "FFR 20 Ohm", "FFR 40 Ohm"
243};
244
Lars-Peter Clausen914bc162014-06-19 09:40:27 +0200245static SOC_ENUM_SINGLE_DECL(mic1lp_p_enum, AIC31XX_MICPGAPI, 6,
246 mic_select_text);
247static SOC_ENUM_SINGLE_DECL(mic1rp_p_enum, AIC31XX_MICPGAPI, 4,
248 mic_select_text);
249static SOC_ENUM_SINGLE_DECL(mic1lm_p_enum, AIC31XX_MICPGAPI, 2,
250 mic_select_text);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200251
Lars-Peter Clausen914bc162014-06-19 09:40:27 +0200252static SOC_ENUM_SINGLE_DECL(cm_m_enum, AIC31XX_MICPGAMI, 6, mic_select_text);
253static SOC_ENUM_SINGLE_DECL(mic1lm_m_enum, AIC31XX_MICPGAMI, 4,
254 mic_select_text);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200255
256static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6350, 50, 0);
257static const DECLARE_TLV_DB_SCALE(adc_fgain_tlv, 0, 10, 0);
258static const DECLARE_TLV_DB_SCALE(adc_cgain_tlv, -2000, 50, 0);
259static const DECLARE_TLV_DB_SCALE(mic_pga_tlv, 0, 50, 0);
260static const DECLARE_TLV_DB_SCALE(hp_drv_tlv, 0, 100, 0);
261static const DECLARE_TLV_DB_SCALE(class_D_drv_tlv, 600, 600, 0);
262static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -6350, 50, 0);
263static const DECLARE_TLV_DB_SCALE(sp_vol_tlv, -6350, 50, 0);
264
265/*
266 * controls to be exported to the user space
267 */
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300268static const struct snd_kcontrol_new common31xx_snd_controls[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200269 SOC_DOUBLE_R_S_TLV("DAC Playback Volume", AIC31XX_LDACVOL,
270 AIC31XX_RDACVOL, 0, -127, 48, 7, 0, dac_vol_tlv),
271
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300272 SOC_DOUBLE_R("HP Driver Playback Switch", AIC31XX_HPLGAIN,
273 AIC31XX_HPRGAIN, 2, 1, 0),
274 SOC_DOUBLE_R_TLV("HP Driver Playback Volume", AIC31XX_HPLGAIN,
275 AIC31XX_HPRGAIN, 3, 0x09, 0, hp_drv_tlv),
276
277 SOC_DOUBLE_R_TLV("HP Analog Playback Volume", AIC31XX_LANALOGHPL,
278 AIC31XX_RANALOGHPR, 0, 0x7F, 1, hp_vol_tlv),
279};
280
281static const struct snd_kcontrol_new aic31xx_snd_controls[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200282 SOC_SINGLE_TLV("ADC Fine Capture Volume", AIC31XX_ADCFGA, 4, 4, 1,
283 adc_fgain_tlv),
284
285 SOC_SINGLE("ADC Capture Switch", AIC31XX_ADCFGA, 7, 1, 1),
286 SOC_DOUBLE_R_S_TLV("ADC Capture Volume", AIC31XX_ADCVOL, AIC31XX_ADCVOL,
287 0, -24, 40, 6, 0, adc_cgain_tlv),
288
289 SOC_SINGLE_TLV("Mic PGA Capture Volume", AIC31XX_MICPGA, 0,
290 119, 0, mic_pga_tlv),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200291};
292
293static const struct snd_kcontrol_new aic311x_snd_controls[] = {
294 SOC_DOUBLE_R("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
295 AIC31XX_SPRGAIN, 2, 1, 0),
296 SOC_DOUBLE_R_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
297 AIC31XX_SPRGAIN, 3, 3, 0, class_D_drv_tlv),
298
299 SOC_DOUBLE_R_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
300 AIC31XX_RANALOGSPR, 0, 0x7F, 1, sp_vol_tlv),
301};
302
303static const struct snd_kcontrol_new aic310x_snd_controls[] = {
304 SOC_SINGLE("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
305 2, 1, 0),
306 SOC_SINGLE_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
307 3, 3, 0, class_D_drv_tlv),
308
309 SOC_SINGLE_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
310 0, 0x7F, 1, sp_vol_tlv),
311};
312
313static const struct snd_kcontrol_new ldac_in_control =
314 SOC_DAPM_ENUM("DAC Left Input", ldac_in_enum);
315
316static const struct snd_kcontrol_new rdac_in_control =
317 SOC_DAPM_ENUM("DAC Right Input", rdac_in_enum);
318
Mark Brown9296f4d2014-03-13 17:44:22 +0000319static int aic31xx_wait_bits(struct aic31xx_priv *aic31xx, unsigned int reg,
320 unsigned int mask, unsigned int wbits, int sleep,
321 int count)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200322{
323 unsigned int bits;
324 int counter = count;
325 int ret = regmap_read(aic31xx->regmap, reg, &bits);
Shahina Shaik423ca882014-06-13 11:56:54 +0530326
Jyri Sarhae00447f2014-03-11 12:57:32 +0200327 while ((bits & mask) != wbits && counter && !ret) {
328 usleep_range(sleep, sleep * 2);
329 ret = regmap_read(aic31xx->regmap, reg, &bits);
330 counter--;
331 }
332 if ((bits & mask) != wbits) {
333 dev_err(aic31xx->dev,
334 "%s: Failed! 0x%x was 0x%x expected 0x%x (%d, 0x%x, %d us)\n",
335 __func__, reg, bits, wbits, ret, mask,
336 (count - counter) * sleep);
337 ret = -1;
338 }
339 return ret;
340}
341
342#define WIDGET_BIT(reg, shift) (((shift) << 8) | (reg))
343
344static int aic31xx_dapm_power_event(struct snd_soc_dapm_widget *w,
345 struct snd_kcontrol *kcontrol, int event)
346{
Lars-Peter Clausendd943d32015-01-15 12:52:03 +0100347 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
348 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200349 unsigned int reg = AIC31XX_DACFLAG1;
350 unsigned int mask;
351
352 switch (WIDGET_BIT(w->reg, w->shift)) {
353 case WIDGET_BIT(AIC31XX_DACSETUP, 7):
354 mask = AIC31XX_LDACPWRSTATUS_MASK;
355 break;
356 case WIDGET_BIT(AIC31XX_DACSETUP, 6):
357 mask = AIC31XX_RDACPWRSTATUS_MASK;
358 break;
359 case WIDGET_BIT(AIC31XX_HPDRIVER, 7):
360 mask = AIC31XX_HPLDRVPWRSTATUS_MASK;
361 break;
362 case WIDGET_BIT(AIC31XX_HPDRIVER, 6):
363 mask = AIC31XX_HPRDRVPWRSTATUS_MASK;
364 break;
365 case WIDGET_BIT(AIC31XX_SPKAMP, 7):
366 mask = AIC31XX_SPLDRVPWRSTATUS_MASK;
367 break;
368 case WIDGET_BIT(AIC31XX_SPKAMP, 6):
369 mask = AIC31XX_SPRDRVPWRSTATUS_MASK;
370 break;
371 case WIDGET_BIT(AIC31XX_ADCSETUP, 7):
372 mask = AIC31XX_ADCPWRSTATUS_MASK;
373 reg = AIC31XX_ADCFLAG;
374 break;
375 default:
Lars-Peter Clausendd943d32015-01-15 12:52:03 +0100376 dev_err(codec->dev, "Unknown widget '%s' calling %s\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +0200377 w->name, __func__);
378 return -EINVAL;
379 }
380
381 switch (event) {
382 case SND_SOC_DAPM_POST_PMU:
383 return aic31xx_wait_bits(aic31xx, reg, mask, mask, 5000, 100);
384 case SND_SOC_DAPM_POST_PMD:
385 return aic31xx_wait_bits(aic31xx, reg, mask, 0, 5000, 100);
386 default:
Lars-Peter Clausendd943d32015-01-15 12:52:03 +0100387 dev_dbg(codec->dev,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200388 "Unhandled dapm widget event %d from %s\n",
389 event, w->name);
390 }
391 return 0;
392}
393
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300394static const struct snd_kcontrol_new aic31xx_left_output_switches[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200395 SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
396 SOC_DAPM_SINGLE("From MIC1LP", AIC31XX_DACMIXERROUTE, 5, 1, 0),
397 SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 4, 1, 0),
398};
399
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300400static const struct snd_kcontrol_new aic31xx_right_output_switches[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200401 SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
402 SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 1, 1, 0),
403};
404
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300405static const struct snd_kcontrol_new dac31xx_left_output_switches[] = {
406 SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
407 SOC_DAPM_SINGLE("From AIN1", AIC31XX_DACMIXERROUTE, 5, 1, 0),
408 SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 4, 1, 0),
409};
410
411static const struct snd_kcontrol_new dac31xx_right_output_switches[] = {
412 SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
413 SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 1, 1, 0),
414};
415
Jyri Sarhae00447f2014-03-11 12:57:32 +0200416static const struct snd_kcontrol_new p_term_mic1lp =
417 SOC_DAPM_ENUM("MIC1LP P-Terminal", mic1lp_p_enum);
418
419static const struct snd_kcontrol_new p_term_mic1rp =
420 SOC_DAPM_ENUM("MIC1RP P-Terminal", mic1rp_p_enum);
421
422static const struct snd_kcontrol_new p_term_mic1lm =
423 SOC_DAPM_ENUM("MIC1LM P-Terminal", mic1lm_p_enum);
424
425static const struct snd_kcontrol_new m_term_mic1lm =
426 SOC_DAPM_ENUM("MIC1LM M-Terminal", mic1lm_m_enum);
427
428static const struct snd_kcontrol_new aic31xx_dapm_hpl_switch =
429 SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGHPL, 7, 1, 0);
430
431static const struct snd_kcontrol_new aic31xx_dapm_hpr_switch =
432 SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGHPR, 7, 1, 0);
433
434static const struct snd_kcontrol_new aic31xx_dapm_spl_switch =
435 SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGSPL, 7, 1, 0);
436
437static const struct snd_kcontrol_new aic31xx_dapm_spr_switch =
438 SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGSPR, 7, 1, 0);
439
440static int mic_bias_event(struct snd_soc_dapm_widget *w,
441 struct snd_kcontrol *kcontrol, int event)
442{
Lars-Peter Clausendd943d32015-01-15 12:52:03 +0100443 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200444 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
Shahina Shaik423ca882014-06-13 11:56:54 +0530445
Jyri Sarhae00447f2014-03-11 12:57:32 +0200446 switch (event) {
447 case SND_SOC_DAPM_POST_PMU:
448 /* change mic bias voltage to user defined */
449 snd_soc_update_bits(codec, AIC31XX_MICBIAS,
450 AIC31XX_MICBIAS_MASK,
451 aic31xx->pdata.micbias_vg <<
452 AIC31XX_MICBIAS_SHIFT);
453 dev_dbg(codec->dev, "%s: turned on\n", __func__);
454 break;
455 case SND_SOC_DAPM_PRE_PMD:
456 /* turn mic bias off */
457 snd_soc_update_bits(codec, AIC31XX_MICBIAS,
458 AIC31XX_MICBIAS_MASK, 0);
459 dev_dbg(codec->dev, "%s: turned off\n", __func__);
460 break;
461 }
462 return 0;
463}
464
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300465static const struct snd_soc_dapm_widget common31xx_dapm_widgets[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200466 SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
467
468 SND_SOC_DAPM_MUX("DAC Left Input",
469 SND_SOC_NOPM, 0, 0, &ldac_in_control),
470 SND_SOC_DAPM_MUX("DAC Right Input",
471 SND_SOC_NOPM, 0, 0, &rdac_in_control),
472 /* DACs */
473 SND_SOC_DAPM_DAC_E("DAC Left", "Left Playback",
474 AIC31XX_DACSETUP, 7, 0, aic31xx_dapm_power_event,
475 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
476
477 SND_SOC_DAPM_DAC_E("DAC Right", "Right Playback",
478 AIC31XX_DACSETUP, 6, 0, aic31xx_dapm_power_event,
479 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
480
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300481 /* HP */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200482 SND_SOC_DAPM_SWITCH("HP Left", SND_SOC_NOPM, 0, 0,
483 &aic31xx_dapm_hpl_switch),
484 SND_SOC_DAPM_SWITCH("HP Right", SND_SOC_NOPM, 0, 0,
485 &aic31xx_dapm_hpr_switch),
486
487 /* Output drivers */
488 SND_SOC_DAPM_OUT_DRV_E("HPL Driver", AIC31XX_HPDRIVER, 7, 0,
489 NULL, 0, aic31xx_dapm_power_event,
490 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
491 SND_SOC_DAPM_OUT_DRV_E("HPR Driver", AIC31XX_HPDRIVER, 6, 0,
492 NULL, 0, aic31xx_dapm_power_event,
493 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
494
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300495 /* Mic Bias */
496 SND_SOC_DAPM_SUPPLY("MICBIAS", SND_SOC_NOPM, 0, 0, mic_bias_event,
497 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
498
499 /* Outputs */
500 SND_SOC_DAPM_OUTPUT("HPL"),
501 SND_SOC_DAPM_OUTPUT("HPR"),
502};
503
504static const struct snd_soc_dapm_widget dac31xx_dapm_widgets[] = {
505 /* Inputs */
506 SND_SOC_DAPM_INPUT("AIN1"),
507 SND_SOC_DAPM_INPUT("AIN2"),
508
509 /* Output Mixers */
510 SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
511 dac31xx_left_output_switches,
512 ARRAY_SIZE(dac31xx_left_output_switches)),
513 SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
514 dac31xx_right_output_switches,
515 ARRAY_SIZE(dac31xx_right_output_switches)),
516};
517
518static const struct snd_soc_dapm_widget aic31xx_dapm_widgets[] = {
519 /* Inputs */
520 SND_SOC_DAPM_INPUT("MIC1LP"),
521 SND_SOC_DAPM_INPUT("MIC1RP"),
522 SND_SOC_DAPM_INPUT("MIC1LM"),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200523
524 /* Input Selection to MIC_PGA */
525 SND_SOC_DAPM_MUX("MIC1LP P-Terminal", SND_SOC_NOPM, 0, 0,
526 &p_term_mic1lp),
527 SND_SOC_DAPM_MUX("MIC1RP P-Terminal", SND_SOC_NOPM, 0, 0,
528 &p_term_mic1rp),
529 SND_SOC_DAPM_MUX("MIC1LM P-Terminal", SND_SOC_NOPM, 0, 0,
530 &p_term_mic1lm),
531
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300532 /* ADC */
533 SND_SOC_DAPM_ADC_E("ADC", "Capture", AIC31XX_ADCSETUP, 7, 0,
534 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
535 SND_SOC_DAPM_POST_PMD),
536
Jyri Sarhae00447f2014-03-11 12:57:32 +0200537 SND_SOC_DAPM_MUX("MIC1LM M-Terminal", SND_SOC_NOPM, 0, 0,
538 &m_term_mic1lm),
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300539
Jyri Sarhae00447f2014-03-11 12:57:32 +0200540 /* Enabling & Disabling MIC Gain Ctl */
541 SND_SOC_DAPM_PGA("MIC_GAIN_CTL", AIC31XX_MICPGA,
542 7, 1, NULL, 0),
543
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300544 /* Output Mixers */
545 SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
546 aic31xx_left_output_switches,
547 ARRAY_SIZE(aic31xx_left_output_switches)),
548 SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
549 aic31xx_right_output_switches,
550 ARRAY_SIZE(aic31xx_right_output_switches)),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200551};
552
553static const struct snd_soc_dapm_widget aic311x_dapm_widgets[] = {
554 /* AIC3111 and AIC3110 have stereo class-D amplifier */
555 SND_SOC_DAPM_OUT_DRV_E("SPL ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
556 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
557 SND_SOC_DAPM_POST_PMD),
558 SND_SOC_DAPM_OUT_DRV_E("SPR ClassD", AIC31XX_SPKAMP, 6, 0, NULL, 0,
559 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
560 SND_SOC_DAPM_POST_PMD),
561 SND_SOC_DAPM_SWITCH("Speaker Left", SND_SOC_NOPM, 0, 0,
562 &aic31xx_dapm_spl_switch),
563 SND_SOC_DAPM_SWITCH("Speaker Right", SND_SOC_NOPM, 0, 0,
564 &aic31xx_dapm_spr_switch),
565 SND_SOC_DAPM_OUTPUT("SPL"),
566 SND_SOC_DAPM_OUTPUT("SPR"),
567};
568
569/* AIC3100 and AIC3120 have only mono class-D amplifier */
570static const struct snd_soc_dapm_widget aic310x_dapm_widgets[] = {
571 SND_SOC_DAPM_OUT_DRV_E("SPK ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
572 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
573 SND_SOC_DAPM_POST_PMD),
574 SND_SOC_DAPM_SWITCH("Speaker", SND_SOC_NOPM, 0, 0,
575 &aic31xx_dapm_spl_switch),
576 SND_SOC_DAPM_OUTPUT("SPK"),
577};
578
579static const struct snd_soc_dapm_route
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300580common31xx_audio_map[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200581 /* DAC Input Routing */
582 {"DAC Left Input", "Left Data", "DAC IN"},
583 {"DAC Left Input", "Right Data", "DAC IN"},
584 {"DAC Left Input", "Mono", "DAC IN"},
585 {"DAC Right Input", "Left Data", "DAC IN"},
586 {"DAC Right Input", "Right Data", "DAC IN"},
587 {"DAC Right Input", "Mono", "DAC IN"},
588 {"DAC Left", NULL, "DAC Left Input"},
589 {"DAC Right", NULL, "DAC Right Input"},
590
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300591 /* HPL path */
592 {"HP Left", "Switch", "Output Left"},
593 {"HPL Driver", NULL, "HP Left"},
594 {"HPL", NULL, "HPL Driver"},
595
596 /* HPR path */
597 {"HP Right", "Switch", "Output Right"},
598 {"HPR Driver", NULL, "HP Right"},
599 {"HPR", NULL, "HPR Driver"},
600};
601
602static const struct snd_soc_dapm_route
603dac31xx_audio_map[] = {
604 /* Left Output */
605 {"Output Left", "From Left DAC", "DAC Left"},
606 {"Output Left", "From AIN1", "AIN1"},
607 {"Output Left", "From AIN2", "AIN2"},
608
609 /* Right Output */
610 {"Output Right", "From Right DAC", "DAC Right"},
611 {"Output Right", "From AIN2", "AIN2"},
612};
613
614static const struct snd_soc_dapm_route
615aic31xx_audio_map[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200616 /* Mic input */
617 {"MIC1LP P-Terminal", "FFR 10 Ohm", "MIC1LP"},
618 {"MIC1LP P-Terminal", "FFR 20 Ohm", "MIC1LP"},
619 {"MIC1LP P-Terminal", "FFR 40 Ohm", "MIC1LP"},
620 {"MIC1RP P-Terminal", "FFR 10 Ohm", "MIC1RP"},
621 {"MIC1RP P-Terminal", "FFR 20 Ohm", "MIC1RP"},
622 {"MIC1RP P-Terminal", "FFR 40 Ohm", "MIC1RP"},
623 {"MIC1LM P-Terminal", "FFR 10 Ohm", "MIC1LM"},
624 {"MIC1LM P-Terminal", "FFR 20 Ohm", "MIC1LM"},
625 {"MIC1LM P-Terminal", "FFR 40 Ohm", "MIC1LM"},
626
627 {"MIC1LM M-Terminal", "FFR 10 Ohm", "MIC1LM"},
628 {"MIC1LM M-Terminal", "FFR 20 Ohm", "MIC1LM"},
629 {"MIC1LM M-Terminal", "FFR 40 Ohm", "MIC1LM"},
630
631 {"MIC_GAIN_CTL", NULL, "MIC1LP P-Terminal"},
632 {"MIC_GAIN_CTL", NULL, "MIC1RP P-Terminal"},
633 {"MIC_GAIN_CTL", NULL, "MIC1LM P-Terminal"},
634 {"MIC_GAIN_CTL", NULL, "MIC1LM M-Terminal"},
635
636 {"ADC", NULL, "MIC_GAIN_CTL"},
637
638 /* Left Output */
639 {"Output Left", "From Left DAC", "DAC Left"},
640 {"Output Left", "From MIC1LP", "MIC1LP"},
641 {"Output Left", "From MIC1RP", "MIC1RP"},
642
643 /* Right Output */
644 {"Output Right", "From Right DAC", "DAC Right"},
645 {"Output Right", "From MIC1RP", "MIC1RP"},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200646};
647
648static const struct snd_soc_dapm_route
649aic311x_audio_map[] = {
650 /* SP L path */
651 {"Speaker Left", "Switch", "Output Left"},
652 {"SPL ClassD", NULL, "Speaker Left"},
653 {"SPL", NULL, "SPL ClassD"},
654
655 /* SP R path */
656 {"Speaker Right", "Switch", "Output Right"},
657 {"SPR ClassD", NULL, "Speaker Right"},
658 {"SPR", NULL, "SPR ClassD"},
659};
660
661static const struct snd_soc_dapm_route
662aic310x_audio_map[] = {
663 /* SP L path */
664 {"Speaker", "Switch", "Output Left"},
665 {"SPK ClassD", NULL, "Speaker"},
666 {"SPK", NULL, "SPK ClassD"},
667};
668
669static int aic31xx_add_controls(struct snd_soc_codec *codec)
670{
671 int ret = 0;
672 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
673
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300674 if (!(aic31xx->pdata.codec_type & DAC31XX_BIT))
675 ret = snd_soc_add_codec_controls(
676 codec, aic31xx_snd_controls,
677 ARRAY_SIZE(aic31xx_snd_controls));
678 if (ret)
679 return ret;
680
Jyri Sarhae00447f2014-03-11 12:57:32 +0200681 if (aic31xx->pdata.codec_type & AIC31XX_STEREO_CLASS_D_BIT)
682 ret = snd_soc_add_codec_controls(
683 codec, aic311x_snd_controls,
684 ARRAY_SIZE(aic311x_snd_controls));
685 else
686 ret = snd_soc_add_codec_controls(
687 codec, aic310x_snd_controls,
688 ARRAY_SIZE(aic310x_snd_controls));
689
690 return ret;
691}
692
693static int aic31xx_add_widgets(struct snd_soc_codec *codec)
694{
Lars-Peter Clausen378d1e42015-05-15 12:32:56 +0200695 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200696 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
697 int ret = 0;
698
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300699 if (aic31xx->pdata.codec_type & DAC31XX_BIT) {
700 ret = snd_soc_dapm_new_controls(
701 dapm, dac31xx_dapm_widgets,
702 ARRAY_SIZE(dac31xx_dapm_widgets));
703 if (ret)
704 return ret;
705
706 ret = snd_soc_dapm_add_routes(dapm, dac31xx_audio_map,
707 ARRAY_SIZE(dac31xx_audio_map));
708 if (ret)
709 return ret;
710 } else {
711 ret = snd_soc_dapm_new_controls(
712 dapm, aic31xx_dapm_widgets,
713 ARRAY_SIZE(aic31xx_dapm_widgets));
714 if (ret)
715 return ret;
716
717 ret = snd_soc_dapm_add_routes(dapm, aic31xx_audio_map,
718 ARRAY_SIZE(aic31xx_audio_map));
719 if (ret)
720 return ret;
721 }
722
Jyri Sarhae00447f2014-03-11 12:57:32 +0200723 if (aic31xx->pdata.codec_type & AIC31XX_STEREO_CLASS_D_BIT) {
724 ret = snd_soc_dapm_new_controls(
725 dapm, aic311x_dapm_widgets,
726 ARRAY_SIZE(aic311x_dapm_widgets));
727 if (ret)
728 return ret;
729
730 ret = snd_soc_dapm_add_routes(dapm, aic311x_audio_map,
731 ARRAY_SIZE(aic311x_audio_map));
732 if (ret)
733 return ret;
734 } else {
735 ret = snd_soc_dapm_new_controls(
736 dapm, aic310x_dapm_widgets,
737 ARRAY_SIZE(aic310x_dapm_widgets));
738 if (ret)
739 return ret;
740
741 ret = snd_soc_dapm_add_routes(dapm, aic310x_audio_map,
742 ARRAY_SIZE(aic310x_audio_map));
743 if (ret)
744 return ret;
745 }
746
747 return 0;
748}
749
750static int aic31xx_setup_pll(struct snd_soc_codec *codec,
751 struct snd_pcm_hw_params *params)
752{
753 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
Jyri Sarha03be88e2014-09-03 15:52:33 +0300754 int bclk_score = snd_soc_params_to_frame_size(params);
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300755 int mclk_p = aic31xx->sysclk / aic31xx->p_div;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200756 int bclk_n = 0;
Jyri Sarha03be88e2014-09-03 15:52:33 +0300757 int match = -1;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200758 int i;
759
760 /* Use PLL as CODEC_CLKIN and DAC_CLK as BDIV_CLKIN */
761 snd_soc_update_bits(codec, AIC31XX_CLKMUX,
762 AIC31XX_CODEC_CLKIN_MASK, AIC31XX_CODEC_CLKIN_PLL);
763 snd_soc_update_bits(codec, AIC31XX_IFACE2,
764 AIC31XX_BDIVCLK_MASK, AIC31XX_DAC2BCLK);
765
766 for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++) {
767 if (aic31xx_divs[i].rate == params_rate(params) &&
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300768 aic31xx_divs[i].mclk_p == mclk_p) {
Jyri Sarha03be88e2014-09-03 15:52:33 +0300769 int s = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) %
770 snd_soc_params_to_frame_size(params);
771 int bn = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) /
772 snd_soc_params_to_frame_size(params);
773 if (s < bclk_score && bn > 0) {
774 match = i;
775 bclk_n = bn;
776 bclk_score = s;
777 }
778 }
Jyri Sarhae00447f2014-03-11 12:57:32 +0200779 }
780
Jyri Sarha03be88e2014-09-03 15:52:33 +0300781 if (match == -1) {
782 dev_err(codec->dev,
783 "%s: Sample rate (%u) and format not supported\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +0200784 __func__, params_rate(params));
Jyri Sarha03be88e2014-09-03 15:52:33 +0300785 /* See bellow for details how fix this. */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200786 return -EINVAL;
787 }
Jyri Sarha03be88e2014-09-03 15:52:33 +0300788 if (bclk_score != 0) {
789 dev_warn(codec->dev, "Can not produce exact bitclock");
790 /* This is fine if using dsp format, but if using i2s
791 there may be trouble. To fix the issue edit the
792 aic31xx_divs table for your mclk and sample
793 rate. Details can be found from:
794 http://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
795 Section: 5.6 CLOCK Generation and PLL
796 */
797 }
798 i = match;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200799
800 /* PLL configuration */
801 snd_soc_update_bits(codec, AIC31XX_PLLPR, AIC31XX_PLL_MASK,
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300802 (aic31xx->p_div << 4) | 0x01);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200803 snd_soc_write(codec, AIC31XX_PLLJ, aic31xx_divs[i].pll_j);
804
805 snd_soc_write(codec, AIC31XX_PLLDMSB,
806 aic31xx_divs[i].pll_d >> 8);
807 snd_soc_write(codec, AIC31XX_PLLDLSB,
808 aic31xx_divs[i].pll_d & 0xff);
809
810 /* DAC dividers configuration */
811 snd_soc_update_bits(codec, AIC31XX_NDAC, AIC31XX_PLL_MASK,
812 aic31xx_divs[i].ndac);
813 snd_soc_update_bits(codec, AIC31XX_MDAC, AIC31XX_PLL_MASK,
814 aic31xx_divs[i].mdac);
815
816 snd_soc_write(codec, AIC31XX_DOSRMSB, aic31xx_divs[i].dosr >> 8);
817 snd_soc_write(codec, AIC31XX_DOSRLSB, aic31xx_divs[i].dosr & 0xff);
818
819 /* ADC dividers configuration. Write reset value 1 if not used. */
820 snd_soc_update_bits(codec, AIC31XX_NADC, AIC31XX_PLL_MASK,
821 aic31xx_divs[i].nadc ? aic31xx_divs[i].nadc : 1);
822 snd_soc_update_bits(codec, AIC31XX_MADC, AIC31XX_PLL_MASK,
823 aic31xx_divs[i].madc ? aic31xx_divs[i].madc : 1);
824
825 snd_soc_write(codec, AIC31XX_AOSR, aic31xx_divs[i].aosr);
826
827 /* Bit clock divider configuration. */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200828 snd_soc_update_bits(codec, AIC31XX_BCLKN,
829 AIC31XX_PLL_MASK, bclk_n);
830
831 aic31xx->rate_div_line = i;
832
833 dev_dbg(codec->dev,
834 "pll %d.%04d/%d dosr %d n %d m %d aosr %d n %d m %d bclk_n %d\n",
835 aic31xx_divs[i].pll_j, aic31xx_divs[i].pll_d,
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300836 aic31xx->p_div, aic31xx_divs[i].dosr,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200837 aic31xx_divs[i].ndac, aic31xx_divs[i].mdac,
838 aic31xx_divs[i].aosr, aic31xx_divs[i].nadc,
839 aic31xx_divs[i].madc, bclk_n);
840
841 return 0;
842}
843
844static int aic31xx_hw_params(struct snd_pcm_substream *substream,
845 struct snd_pcm_hw_params *params,
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100846 struct snd_soc_dai *dai)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200847{
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100848 struct snd_soc_codec *codec = dai->codec;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200849 u8 data = 0;
850
Mark Brown88be6812014-07-31 12:48:13 +0100851 dev_dbg(codec->dev, "## %s: width %d rate %d\n",
852 __func__, params_width(params),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200853 params_rate(params));
854
855 switch (params_width(params)) {
856 case 16:
857 break;
858 case 20:
859 data = (AIC31XX_WORD_LEN_20BITS <<
860 AIC31XX_IFACE1_DATALEN_SHIFT);
861 break;
862 case 24:
863 data = (AIC31XX_WORD_LEN_24BITS <<
864 AIC31XX_IFACE1_DATALEN_SHIFT);
865 break;
866 case 32:
867 data = (AIC31XX_WORD_LEN_32BITS <<
868 AIC31XX_IFACE1_DATALEN_SHIFT);
869 break;
870 default:
Mark Brown88be6812014-07-31 12:48:13 +0100871 dev_err(codec->dev, "%s: Unsupported width %d\n",
872 __func__, params_width(params));
Jyri Sarhae00447f2014-03-11 12:57:32 +0200873 return -EINVAL;
874 }
875
876 snd_soc_update_bits(codec, AIC31XX_IFACE1,
877 AIC31XX_IFACE1_DATALEN_MASK,
878 data);
879
880 return aic31xx_setup_pll(codec, params);
881}
882
883static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute)
884{
885 struct snd_soc_codec *codec = codec_dai->codec;
886
887 if (mute) {
888 snd_soc_update_bits(codec, AIC31XX_DACMUTE,
889 AIC31XX_DACMUTE_MASK,
890 AIC31XX_DACMUTE_MASK);
891 } else {
892 snd_soc_update_bits(codec, AIC31XX_DACMUTE,
893 AIC31XX_DACMUTE_MASK, 0x0);
894 }
895
896 return 0;
897}
898
899static int aic31xx_set_dai_fmt(struct snd_soc_dai *codec_dai,
900 unsigned int fmt)
901{
902 struct snd_soc_codec *codec = codec_dai->codec;
903 u8 iface_reg1 = 0;
Peter Ujfalusi085f3ec2014-09-01 12:46:37 +0300904 u8 iface_reg2 = 0;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200905 u8 dsp_a_val = 0;
906
907 dev_dbg(codec->dev, "## %s: fmt = 0x%x\n", __func__, fmt);
908
909 /* set master/slave audio interface */
910 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
911 case SND_SOC_DAIFMT_CBM_CFM:
912 iface_reg1 |= AIC31XX_BCLK_MASTER | AIC31XX_WCLK_MASTER;
913 break;
914 default:
915 dev_alert(codec->dev, "Invalid DAI master/slave interface\n");
916 return -EINVAL;
917 }
918
919 /* interface format */
920 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
921 case SND_SOC_DAIFMT_I2S:
922 break;
923 case SND_SOC_DAIFMT_DSP_A:
Gustavo A. R. Silva09fc38c2017-11-08 14:04:17 -0600924 dsp_a_val = 0x1; /* fall through */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200925 case SND_SOC_DAIFMT_DSP_B:
926 /* NOTE: BCLKINV bit value 1 equas NB and 0 equals IB */
927 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
928 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusi085f3ec2014-09-01 12:46:37 +0300929 iface_reg2 |= AIC31XX_BCLKINV_MASK;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200930 break;
931 case SND_SOC_DAIFMT_IB_NF:
932 break;
933 default:
934 return -EINVAL;
935 }
936 iface_reg1 |= (AIC31XX_DSP_MODE <<
937 AIC31XX_IFACE1_DATATYPE_SHIFT);
938 break;
939 case SND_SOC_DAIFMT_RIGHT_J:
940 iface_reg1 |= (AIC31XX_RIGHT_JUSTIFIED_MODE <<
941 AIC31XX_IFACE1_DATATYPE_SHIFT);
942 break;
943 case SND_SOC_DAIFMT_LEFT_J:
944 iface_reg1 |= (AIC31XX_LEFT_JUSTIFIED_MODE <<
945 AIC31XX_IFACE1_DATATYPE_SHIFT);
946 break;
947 default:
948 dev_err(codec->dev, "Invalid DAI interface format\n");
949 return -EINVAL;
950 }
951
952 snd_soc_update_bits(codec, AIC31XX_IFACE1,
953 AIC31XX_IFACE1_DATATYPE_MASK |
954 AIC31XX_IFACE1_MASTER_MASK,
955 iface_reg1);
956 snd_soc_update_bits(codec, AIC31XX_DATA_OFFSET,
957 AIC31XX_DATA_OFFSET_MASK,
958 dsp_a_val);
959 snd_soc_update_bits(codec, AIC31XX_IFACE2,
960 AIC31XX_BCLKINV_MASK,
Peter Ujfalusi085f3ec2014-09-01 12:46:37 +0300961 iface_reg2);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200962
963 return 0;
964}
965
966static int aic31xx_set_dai_sysclk(struct snd_soc_dai *codec_dai,
967 int clk_id, unsigned int freq, int dir)
968{
969 struct snd_soc_codec *codec = codec_dai->codec;
970 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
971 int i;
972
973 dev_dbg(codec->dev, "## %s: clk_id = %d, freq = %d, dir = %d\n",
974 __func__, clk_id, freq, dir);
975
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300976 for (i = 1; freq/i > 20000000 && i < 8; i++)
977 ;
978 if (freq/i > 20000000) {
979 dev_err(aic31xx->dev, "%s: Too high mclk frequency %u\n",
980 __func__, freq);
981 return -EINVAL;
982 }
983 aic31xx->p_div = i;
984
Jyri Sarhabbc686b2014-11-24 20:37:12 +0200985 for (i = 0; i < ARRAY_SIZE(aic31xx_divs) &&
986 aic31xx_divs[i].mclk_p != freq/aic31xx->p_div; i++)
987 ;
988 if (i == ARRAY_SIZE(aic31xx_divs)) {
989 dev_err(aic31xx->dev, "%s: Unsupported frequency %d\n",
990 __func__, freq);
991 return -EINVAL;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200992 }
993
994 /* set clock on MCLK, BCLK, or GPIO1 as PLL input */
995 snd_soc_update_bits(codec, AIC31XX_CLKMUX, AIC31XX_PLL_CLKIN_MASK,
996 clk_id << AIC31XX_PLL_CLKIN_SHIFT);
997
998 aic31xx->sysclk = freq;
999 return 0;
1000}
1001
1002static int aic31xx_regulator_event(struct notifier_block *nb,
1003 unsigned long event, void *data)
1004{
1005 struct aic31xx_disable_nb *disable_nb =
1006 container_of(nb, struct aic31xx_disable_nb, nb);
1007 struct aic31xx_priv *aic31xx = disable_nb->aic31xx;
1008
1009 if (event & REGULATOR_EVENT_DISABLE) {
1010 /*
1011 * Put codec to reset and as at least one of the
1012 * supplies was disabled.
1013 */
1014 if (gpio_is_valid(aic31xx->pdata.gpio_reset))
1015 gpio_set_value(aic31xx->pdata.gpio_reset, 0);
1016
1017 regcache_mark_dirty(aic31xx->regmap);
1018 dev_dbg(aic31xx->dev, "## %s: DISABLE received\n", __func__);
1019 }
1020
1021 return 0;
1022}
1023
1024static void aic31xx_clk_on(struct snd_soc_codec *codec)
1025{
1026 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
1027 u8 mask = AIC31XX_PM_MASK;
1028 u8 on = AIC31XX_PM_MASK;
1029
1030 dev_dbg(codec->dev, "codec clock -> on (rate %d)\n",
1031 aic31xx_divs[aic31xx->rate_div_line].rate);
1032 snd_soc_update_bits(codec, AIC31XX_PLLPR, mask, on);
1033 mdelay(10);
1034 snd_soc_update_bits(codec, AIC31XX_NDAC, mask, on);
1035 snd_soc_update_bits(codec, AIC31XX_MDAC, mask, on);
1036 if (aic31xx_divs[aic31xx->rate_div_line].nadc)
1037 snd_soc_update_bits(codec, AIC31XX_NADC, mask, on);
1038 if (aic31xx_divs[aic31xx->rate_div_line].madc)
1039 snd_soc_update_bits(codec, AIC31XX_MADC, mask, on);
1040 snd_soc_update_bits(codec, AIC31XX_BCLKN, mask, on);
1041}
1042
1043static void aic31xx_clk_off(struct snd_soc_codec *codec)
1044{
Jyri Sarhae00447f2014-03-11 12:57:32 +02001045 u8 mask = AIC31XX_PM_MASK;
1046 u8 off = 0;
1047
1048 dev_dbg(codec->dev, "codec clock -> off\n");
1049 snd_soc_update_bits(codec, AIC31XX_BCLKN, mask, off);
1050 snd_soc_update_bits(codec, AIC31XX_MADC, mask, off);
1051 snd_soc_update_bits(codec, AIC31XX_NADC, mask, off);
1052 snd_soc_update_bits(codec, AIC31XX_MDAC, mask, off);
1053 snd_soc_update_bits(codec, AIC31XX_NDAC, mask, off);
1054 snd_soc_update_bits(codec, AIC31XX_PLLPR, mask, off);
1055}
1056
1057static int aic31xx_power_on(struct snd_soc_codec *codec)
1058{
1059 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
1060 int ret = 0;
1061
1062 ret = regulator_bulk_enable(ARRAY_SIZE(aic31xx->supplies),
1063 aic31xx->supplies);
1064 if (ret)
1065 return ret;
1066
1067 if (gpio_is_valid(aic31xx->pdata.gpio_reset)) {
1068 gpio_set_value(aic31xx->pdata.gpio_reset, 1);
1069 udelay(100);
1070 }
1071 regcache_cache_only(aic31xx->regmap, false);
1072 ret = regcache_sync(aic31xx->regmap);
1073 if (ret != 0) {
1074 dev_err(codec->dev,
1075 "Failed to restore cache: %d\n", ret);
1076 regcache_cache_only(aic31xx->regmap, true);
1077 regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
1078 aic31xx->supplies);
1079 return ret;
1080 }
1081 return 0;
1082}
1083
Andrew F. Davisc7734e82017-11-29 15:32:43 -06001084static void aic31xx_power_off(struct snd_soc_codec *codec)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001085{
1086 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001087
1088 regcache_cache_only(aic31xx->regmap, true);
Andrew F. Davisc7734e82017-11-29 15:32:43 -06001089 regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
1090 aic31xx->supplies);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001091}
1092
1093static int aic31xx_set_bias_level(struct snd_soc_codec *codec,
1094 enum snd_soc_bias_level level)
1095{
1096 dev_dbg(codec->dev, "## %s: %d -> %d\n", __func__,
Lars-Peter Clausen378d1e42015-05-15 12:32:56 +02001097 snd_soc_codec_get_bias_level(codec), level);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001098
1099 switch (level) {
1100 case SND_SOC_BIAS_ON:
1101 break;
1102 case SND_SOC_BIAS_PREPARE:
Lars-Peter Clausen378d1e42015-05-15 12:32:56 +02001103 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001104 aic31xx_clk_on(codec);
1105 break;
1106 case SND_SOC_BIAS_STANDBY:
Lars-Peter Clausen378d1e42015-05-15 12:32:56 +02001107 switch (snd_soc_codec_get_bias_level(codec)) {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001108 case SND_SOC_BIAS_OFF:
1109 aic31xx_power_on(codec);
1110 break;
1111 case SND_SOC_BIAS_PREPARE:
1112 aic31xx_clk_off(codec);
1113 break;
1114 default:
1115 BUG();
1116 }
1117 break;
1118 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen378d1e42015-05-15 12:32:56 +02001119 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
Jyri Sarhafd218aa2014-03-17 09:31:31 +02001120 aic31xx_power_off(codec);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001121 break;
1122 }
Jyri Sarhae00447f2014-03-11 12:57:32 +02001123
1124 return 0;
1125}
1126
Jyri Sarhae00447f2014-03-11 12:57:32 +02001127static int aic31xx_codec_probe(struct snd_soc_codec *codec)
1128{
1129 int ret = 0;
1130 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
1131 int i;
1132
1133 dev_dbg(aic31xx->dev, "## %s\n", __func__);
1134
1135 aic31xx = snd_soc_codec_get_drvdata(codec);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001136
1137 aic31xx->codec = codec;
1138
Jyri Sarhae00447f2014-03-11 12:57:32 +02001139 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++) {
1140 aic31xx->disable_nb[i].nb.notifier_call =
1141 aic31xx_regulator_event;
1142 aic31xx->disable_nb[i].aic31xx = aic31xx;
1143 ret = regulator_register_notifier(aic31xx->supplies[i].consumer,
1144 &aic31xx->disable_nb[i].nb);
1145 if (ret) {
1146 dev_err(codec->dev,
1147 "Failed to request regulator notifier: %d\n",
1148 ret);
1149 return ret;
1150 }
1151 }
1152
1153 regcache_cache_only(aic31xx->regmap, true);
1154 regcache_mark_dirty(aic31xx->regmap);
1155
1156 ret = aic31xx_add_controls(codec);
1157 if (ret)
1158 return ret;
1159
1160 ret = aic31xx_add_widgets(codec);
1161
1162 return ret;
1163}
1164
1165static int aic31xx_codec_remove(struct snd_soc_codec *codec)
1166{
1167 struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
1168 int i;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001169
1170 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
1171 regulator_unregister_notifier(aic31xx->supplies[i].consumer,
1172 &aic31xx->disable_nb[i].nb);
1173
1174 return 0;
1175}
1176
Bhumika Goyala180ba42017-08-03 21:30:19 +05301177static const struct snd_soc_codec_driver soc_codec_driver_aic31xx = {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001178 .probe = aic31xx_codec_probe,
1179 .remove = aic31xx_codec_remove,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001180 .set_bias_level = aic31xx_set_bias_level,
Lars-Peter Clausena43a2622014-11-26 20:57:55 +01001181 .suspend_bias_off = true,
1182
Kuninori Morimoto1bb99f22016-08-08 08:53:34 +00001183 .component_driver = {
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001184 .controls = common31xx_snd_controls,
1185 .num_controls = ARRAY_SIZE(common31xx_snd_controls),
1186 .dapm_widgets = common31xx_dapm_widgets,
1187 .num_dapm_widgets = ARRAY_SIZE(common31xx_dapm_widgets),
1188 .dapm_routes = common31xx_audio_map,
1189 .num_dapm_routes = ARRAY_SIZE(common31xx_audio_map),
Kuninori Morimoto1bb99f22016-08-08 08:53:34 +00001190 },
Jyri Sarhae00447f2014-03-11 12:57:32 +02001191};
1192
Axel Lin64793042015-07-15 15:38:14 +08001193static const struct snd_soc_dai_ops aic31xx_dai_ops = {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001194 .hw_params = aic31xx_hw_params,
1195 .set_sysclk = aic31xx_set_dai_sysclk,
1196 .set_fmt = aic31xx_set_dai_fmt,
1197 .digital_mute = aic31xx_dac_mute,
1198};
1199
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001200static struct snd_soc_dai_driver dac31xx_dai_driver[] = {
1201 {
Tomas Vilda51827c42017-05-13 00:29:37 +03001202 .name = "tlv320dac31xx-hifi",
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001203 .playback = {
1204 .stream_name = "Playback",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001205 .channels_min = 2,
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001206 .channels_max = 2,
1207 .rates = AIC31XX_RATES,
1208 .formats = AIC31XX_FORMATS,
1209 },
1210 .ops = &aic31xx_dai_ops,
1211 .symmetric_rates = 1,
1212 }
1213};
1214
Jyri Sarhae00447f2014-03-11 12:57:32 +02001215static struct snd_soc_dai_driver aic31xx_dai_driver[] = {
1216 {
1217 .name = "tlv320aic31xx-hifi",
1218 .playback = {
1219 .stream_name = "Playback",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001220 .channels_min = 2,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001221 .channels_max = 2,
1222 .rates = AIC31XX_RATES,
1223 .formats = AIC31XX_FORMATS,
1224 },
1225 .capture = {
1226 .stream_name = "Capture",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001227 .channels_min = 2,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001228 .channels_max = 2,
1229 .rates = AIC31XX_RATES,
1230 .formats = AIC31XX_FORMATS,
1231 },
1232 .ops = &aic31xx_dai_ops,
1233 .symmetric_rates = 1,
1234 }
1235};
1236
1237#if defined(CONFIG_OF)
1238static const struct of_device_id tlv320aic31xx_of_match[] = {
1239 { .compatible = "ti,tlv320aic310x" },
1240 { .compatible = "ti,tlv320aic311x" },
1241 { .compatible = "ti,tlv320aic3100" },
1242 { .compatible = "ti,tlv320aic3110" },
1243 { .compatible = "ti,tlv320aic3120" },
1244 { .compatible = "ti,tlv320aic3111" },
Peter Ujfalusia4a1d792016-11-10 09:55:54 +02001245 { .compatible = "ti,tlv320dac3100" },
Peter Ujfalusi4e2cc812016-11-10 09:55:55 +02001246 { .compatible = "ti,tlv320dac3101" },
Jyri Sarhae00447f2014-03-11 12:57:32 +02001247 {},
1248};
1249MODULE_DEVICE_TABLE(of, tlv320aic31xx_of_match);
1250
1251static void aic31xx_pdata_from_of(struct aic31xx_priv *aic31xx)
1252{
1253 struct device_node *np = aic31xx->dev->of_node;
1254 unsigned int value = MICBIAS_2_0V;
1255 int ret;
1256
1257 of_property_read_u32(np, "ai31xx-micbias-vg", &value);
1258 switch (value) {
1259 case MICBIAS_2_0V:
1260 case MICBIAS_2_5V:
1261 case MICBIAS_AVDDV:
1262 aic31xx->pdata.micbias_vg = value;
1263 break;
1264 default:
1265 dev_err(aic31xx->dev,
1266 "Bad ai31xx-micbias-vg value %d DT\n",
1267 value);
1268 aic31xx->pdata.micbias_vg = MICBIAS_2_0V;
1269 }
1270
Andrew F. Davisa825f312017-11-29 11:13:54 -06001271 ret = of_get_named_gpio(np, "reset-gpios", 0);
1272 if (ret > 0) {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001273 aic31xx->pdata.gpio_reset = ret;
Andrew F. Davisa825f312017-11-29 11:13:54 -06001274 } else {
1275 ret = of_get_named_gpio(np, "gpio-reset", 0);
1276 if (ret > 0) {
1277 dev_warn(aic31xx->dev, "Using deprecated property \"gpio-reset\", please update your DT");
1278 aic31xx->pdata.gpio_reset = ret;
1279 }
1280 }
Jyri Sarhae00447f2014-03-11 12:57:32 +02001281}
1282#else /* CONFIG_OF */
1283static void aic31xx_pdata_from_of(struct aic31xx_priv *aic31xx)
1284{
1285}
1286#endif /* CONFIG_OF */
1287
Peter Ujfalusia72d2ab2014-07-25 13:12:54 +03001288static int aic31xx_device_init(struct aic31xx_priv *aic31xx)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001289{
1290 int ret, i;
1291
1292 dev_set_drvdata(aic31xx->dev, aic31xx);
1293
1294 if (dev_get_platdata(aic31xx->dev))
1295 memcpy(&aic31xx->pdata, dev_get_platdata(aic31xx->dev),
1296 sizeof(aic31xx->pdata));
1297 else if (aic31xx->dev->of_node)
1298 aic31xx_pdata_from_of(aic31xx);
1299
1300 if (aic31xx->pdata.gpio_reset) {
1301 ret = devm_gpio_request_one(aic31xx->dev,
1302 aic31xx->pdata.gpio_reset,
1303 GPIOF_OUT_INIT_HIGH,
1304 "aic31xx-reset-pin");
1305 if (ret < 0) {
1306 dev_err(aic31xx->dev, "not able to acquire gpio\n");
Peter Ujfalusia72d2ab2014-07-25 13:12:54 +03001307 return ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001308 }
1309 }
1310
1311 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
1312 aic31xx->supplies[i].supply = aic31xx_supply_names[i];
1313
1314 ret = devm_regulator_bulk_get(aic31xx->dev,
1315 ARRAY_SIZE(aic31xx->supplies),
1316 aic31xx->supplies);
1317 if (ret != 0)
1318 dev_err(aic31xx->dev, "Failed to request supplies: %d\n", ret);
1319
Peter Ujfalusia72d2ab2014-07-25 13:12:54 +03001320 return ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001321}
1322
1323static int aic31xx_i2c_probe(struct i2c_client *i2c,
1324 const struct i2c_device_id *id)
1325{
1326 struct aic31xx_priv *aic31xx;
1327 int ret;
1328 const struct regmap_config *regmap_config;
1329
1330 dev_dbg(&i2c->dev, "## %s: %s codec_type = %d\n", __func__,
1331 id->name, (int) id->driver_data);
1332
1333 regmap_config = &aic31xx_i2c_regmap;
1334
1335 aic31xx = devm_kzalloc(&i2c->dev, sizeof(*aic31xx), GFP_KERNEL);
1336 if (aic31xx == NULL)
1337 return -ENOMEM;
1338
1339 aic31xx->regmap = devm_regmap_init_i2c(i2c, regmap_config);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001340 if (IS_ERR(aic31xx->regmap)) {
1341 ret = PTR_ERR(aic31xx->regmap);
1342 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1343 ret);
1344 return ret;
1345 }
1346 aic31xx->dev = &i2c->dev;
1347
1348 aic31xx->pdata.codec_type = id->driver_data;
1349
Peter Ujfalusia72d2ab2014-07-25 13:12:54 +03001350 ret = aic31xx_device_init(aic31xx);
1351 if (ret)
1352 return ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001353
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001354 if (aic31xx->pdata.codec_type & DAC31XX_BIT)
1355 return snd_soc_register_codec(&i2c->dev,
1356 &soc_codec_driver_aic31xx,
1357 dac31xx_dai_driver,
1358 ARRAY_SIZE(dac31xx_dai_driver));
1359 else
1360 return snd_soc_register_codec(&i2c->dev,
1361 &soc_codec_driver_aic31xx,
1362 aic31xx_dai_driver,
1363 ARRAY_SIZE(aic31xx_dai_driver));
Jyri Sarhae00447f2014-03-11 12:57:32 +02001364}
1365
1366static int aic31xx_i2c_remove(struct i2c_client *i2c)
1367{
Axel Lindac7e402014-03-16 23:06:25 +08001368 snd_soc_unregister_codec(&i2c->dev);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001369 return 0;
1370}
1371
1372static const struct i2c_device_id aic31xx_i2c_id[] = {
1373 { "tlv320aic310x", AIC3100 },
1374 { "tlv320aic311x", AIC3110 },
1375 { "tlv320aic3100", AIC3100 },
1376 { "tlv320aic3110", AIC3110 },
1377 { "tlv320aic3120", AIC3120 },
1378 { "tlv320aic3111", AIC3111 },
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001379 { "tlv320dac3100", DAC3100 },
Peter Ujfalusi4e2cc812016-11-10 09:55:55 +02001380 { "tlv320dac3101", DAC3101 },
Jyri Sarhae00447f2014-03-11 12:57:32 +02001381 { }
1382};
1383MODULE_DEVICE_TABLE(i2c, aic31xx_i2c_id);
1384
Bastien Noceraf5cc1772016-04-19 18:00:20 +02001385#ifdef CONFIG_ACPI
1386static const struct acpi_device_id aic31xx_acpi_match[] = {
1387 { "10TI3100", 0 },
1388 { }
1389};
1390MODULE_DEVICE_TABLE(acpi, aic31xx_acpi_match);
1391#endif
1392
Jyri Sarhae00447f2014-03-11 12:57:32 +02001393static struct i2c_driver aic31xx_i2c_driver = {
1394 .driver = {
1395 .name = "tlv320aic31xx-codec",
Jyri Sarhae00447f2014-03-11 12:57:32 +02001396 .of_match_table = of_match_ptr(tlv320aic31xx_of_match),
Bastien Noceraf5cc1772016-04-19 18:00:20 +02001397 .acpi_match_table = ACPI_PTR(aic31xx_acpi_match),
Jyri Sarhae00447f2014-03-11 12:57:32 +02001398 },
1399 .probe = aic31xx_i2c_probe,
Axel Lindac7e402014-03-16 23:06:25 +08001400 .remove = aic31xx_i2c_remove,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001401 .id_table = aic31xx_i2c_id,
1402};
1403
1404module_i2c_driver(aic31xx_i2c_driver);
1405
Andrew F. Davisb1c52b72017-11-29 15:32:42 -06001406MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1407MODULE_DESCRIPTION("ASoC TLV320AIC31xx CODEC Driver");
1408MODULE_LICENSE("GPL v2");