blob: d3cd924dc300b7f6c051be763219fb2347ae67de [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>
Andrew F. Davis283c3502017-12-07 09:38:51 -060021#include <linux/gpio/consumer.h>
Jyri Sarhae00447f2014-03-11 12:57:32 +020022#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
Andrew F. Davisca7840f2017-11-29 15:32:45 -0600139static const char * const aic31xx_supply_names[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200140 "HPVDD",
141 "SPRVDD",
142 "SPLVDD",
143 "AVDD",
144 "IOVDD",
145 "DVDD",
146};
147
Andrew F. Davisca7840f2017-11-29 15:32:45 -0600148#define AIC31XX_NUM_SUPPLIES ARRAY_SIZE(aic31xx_supply_names)
149
Jyri Sarhae00447f2014-03-11 12:57:32 +0200150struct aic31xx_disable_nb {
151 struct notifier_block nb;
152 struct aic31xx_priv *aic31xx;
153};
154
155struct aic31xx_priv {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000156 struct snd_soc_component *component;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200157 u8 i2c_regs_status;
158 struct device *dev;
159 struct regmap *regmap;
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600160 enum aic31xx_type codec_type;
Andrew F. Davisb6b247c2017-11-29 15:32:49 -0600161 struct gpio_desc *gpio_reset;
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600162 int micbias_vg;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200163 struct aic31xx_pdata pdata;
164 struct regulator_bulk_data supplies[AIC31XX_NUM_SUPPLIES];
165 struct aic31xx_disable_nb disable_nb[AIC31XX_NUM_SUPPLIES];
166 unsigned int sysclk;
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300167 u8 p_div;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200168 int rate_div_line;
169};
170
171struct aic31xx_rate_divs {
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300172 u32 mclk_p;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200173 u32 rate;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200174 u8 pll_j;
175 u16 pll_d;
176 u16 dosr;
177 u8 ndac;
178 u8 mdac;
179 u8 aosr;
180 u8 nadc;
181 u8 madc;
182};
183
Andrew F. Davisca7840f2017-11-29 15:32:45 -0600184/* ADC dividers can be disabled by configuring them to 0 */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200185static const struct aic31xx_rate_divs aic31xx_divs[] = {
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300186 /* mclk/p rate pll: j d dosr ndac mdac aors nadc madc */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200187 /* 8k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300188 {12000000, 8000, 8, 1920, 128, 48, 2, 128, 48, 2},
189 {12000000, 8000, 8, 1920, 128, 32, 3, 128, 32, 3},
190 {12500000, 8000, 7, 8643, 128, 48, 2, 128, 48, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200191 /* 11.025k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300192 {12000000, 11025, 7, 5264, 128, 32, 2, 128, 32, 2},
193 {12000000, 11025, 8, 4672, 128, 24, 3, 128, 24, 3},
194 {12500000, 11025, 7, 2253, 128, 32, 2, 128, 32, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200195 /* 16k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300196 {12000000, 16000, 8, 1920, 128, 24, 2, 128, 24, 2},
197 {12000000, 16000, 8, 1920, 128, 16, 3, 128, 16, 3},
198 {12500000, 16000, 7, 8643, 128, 24, 2, 128, 24, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200199 /* 22.05k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300200 {12000000, 22050, 7, 5264, 128, 16, 2, 128, 16, 2},
201 {12000000, 22050, 8, 4672, 128, 12, 3, 128, 12, 3},
202 {12500000, 22050, 7, 2253, 128, 16, 2, 128, 16, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200203 /* 32k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300204 {12000000, 32000, 8, 1920, 128, 12, 2, 128, 12, 2},
205 {12000000, 32000, 8, 1920, 128, 8, 3, 128, 8, 3},
206 {12500000, 32000, 7, 8643, 128, 12, 2, 128, 12, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200207 /* 44.1k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300208 {12000000, 44100, 7, 5264, 128, 8, 2, 128, 8, 2},
209 {12000000, 44100, 8, 4672, 128, 6, 3, 128, 6, 3},
210 {12500000, 44100, 7, 2253, 128, 8, 2, 128, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200211 /* 48k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300212 {12000000, 48000, 8, 1920, 128, 8, 2, 128, 8, 2},
213 {12000000, 48000, 7, 6800, 96, 5, 4, 96, 5, 4},
214 {12500000, 48000, 7, 8643, 128, 8, 2, 128, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200215 /* 88.2k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300216 {12000000, 88200, 7, 5264, 64, 8, 2, 64, 8, 2},
217 {12000000, 88200, 8, 4672, 64, 6, 3, 64, 6, 3},
218 {12500000, 88200, 7, 2253, 64, 8, 2, 64, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200219 /* 96k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300220 {12000000, 96000, 8, 1920, 64, 8, 2, 64, 8, 2},
221 {12000000, 96000, 7, 6800, 48, 5, 4, 48, 5, 4},
222 {12500000, 96000, 7, 8643, 64, 8, 2, 64, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200223 /* 176.4k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300224 {12000000, 176400, 7, 5264, 32, 8, 2, 32, 8, 2},
225 {12000000, 176400, 8, 4672, 32, 6, 3, 32, 6, 3},
226 {12500000, 176400, 7, 2253, 32, 8, 2, 32, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200227 /* 192k rate */
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300228 {12000000, 192000, 8, 1920, 32, 8, 2, 32, 8, 2},
229 {12000000, 192000, 7, 6800, 24, 5, 4, 24, 5, 4},
230 {12500000, 192000, 7, 8643, 32, 8, 2, 32, 8, 2},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200231};
232
233static const char * const ldac_in_text[] = {
234 "Off", "Left Data", "Right Data", "Mono"
235};
236
237static const char * const rdac_in_text[] = {
238 "Off", "Right Data", "Left Data", "Mono"
239};
240
241static SOC_ENUM_SINGLE_DECL(ldac_in_enum, AIC31XX_DACSETUP, 4, ldac_in_text);
242
243static SOC_ENUM_SINGLE_DECL(rdac_in_enum, AIC31XX_DACSETUP, 2, rdac_in_text);
244
245static const char * const mic_select_text[] = {
246 "Off", "FFR 10 Ohm", "FFR 20 Ohm", "FFR 40 Ohm"
247};
248
Lars-Peter Clausen914bc162014-06-19 09:40:27 +0200249static SOC_ENUM_SINGLE_DECL(mic1lp_p_enum, AIC31XX_MICPGAPI, 6,
250 mic_select_text);
251static SOC_ENUM_SINGLE_DECL(mic1rp_p_enum, AIC31XX_MICPGAPI, 4,
252 mic_select_text);
253static SOC_ENUM_SINGLE_DECL(mic1lm_p_enum, AIC31XX_MICPGAPI, 2,
254 mic_select_text);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200255
Lars-Peter Clausen914bc162014-06-19 09:40:27 +0200256static SOC_ENUM_SINGLE_DECL(cm_m_enum, AIC31XX_MICPGAMI, 6, mic_select_text);
257static SOC_ENUM_SINGLE_DECL(mic1lm_m_enum, AIC31XX_MICPGAMI, 4,
258 mic_select_text);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200259
260static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6350, 50, 0);
261static const DECLARE_TLV_DB_SCALE(adc_fgain_tlv, 0, 10, 0);
262static const DECLARE_TLV_DB_SCALE(adc_cgain_tlv, -2000, 50, 0);
263static const DECLARE_TLV_DB_SCALE(mic_pga_tlv, 0, 50, 0);
264static const DECLARE_TLV_DB_SCALE(hp_drv_tlv, 0, 100, 0);
265static const DECLARE_TLV_DB_SCALE(class_D_drv_tlv, 600, 600, 0);
266static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -6350, 50, 0);
267static const DECLARE_TLV_DB_SCALE(sp_vol_tlv, -6350, 50, 0);
268
269/*
270 * controls to be exported to the user space
271 */
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300272static const struct snd_kcontrol_new common31xx_snd_controls[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200273 SOC_DOUBLE_R_S_TLV("DAC Playback Volume", AIC31XX_LDACVOL,
274 AIC31XX_RDACVOL, 0, -127, 48, 7, 0, dac_vol_tlv),
275
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300276 SOC_DOUBLE_R("HP Driver Playback Switch", AIC31XX_HPLGAIN,
277 AIC31XX_HPRGAIN, 2, 1, 0),
278 SOC_DOUBLE_R_TLV("HP Driver Playback Volume", AIC31XX_HPLGAIN,
279 AIC31XX_HPRGAIN, 3, 0x09, 0, hp_drv_tlv),
280
281 SOC_DOUBLE_R_TLV("HP Analog Playback Volume", AIC31XX_LANALOGHPL,
282 AIC31XX_RANALOGHPR, 0, 0x7F, 1, hp_vol_tlv),
283};
284
285static const struct snd_kcontrol_new aic31xx_snd_controls[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200286 SOC_SINGLE_TLV("ADC Fine Capture Volume", AIC31XX_ADCFGA, 4, 4, 1,
287 adc_fgain_tlv),
288
289 SOC_SINGLE("ADC Capture Switch", AIC31XX_ADCFGA, 7, 1, 1),
290 SOC_DOUBLE_R_S_TLV("ADC Capture Volume", AIC31XX_ADCVOL, AIC31XX_ADCVOL,
291 0, -24, 40, 6, 0, adc_cgain_tlv),
292
293 SOC_SINGLE_TLV("Mic PGA Capture Volume", AIC31XX_MICPGA, 0,
294 119, 0, mic_pga_tlv),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200295};
296
297static const struct snd_kcontrol_new aic311x_snd_controls[] = {
298 SOC_DOUBLE_R("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
299 AIC31XX_SPRGAIN, 2, 1, 0),
300 SOC_DOUBLE_R_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
301 AIC31XX_SPRGAIN, 3, 3, 0, class_D_drv_tlv),
302
303 SOC_DOUBLE_R_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
304 AIC31XX_RANALOGSPR, 0, 0x7F, 1, sp_vol_tlv),
305};
306
307static const struct snd_kcontrol_new aic310x_snd_controls[] = {
308 SOC_SINGLE("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
309 2, 1, 0),
310 SOC_SINGLE_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
311 3, 3, 0, class_D_drv_tlv),
312
313 SOC_SINGLE_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
314 0, 0x7F, 1, sp_vol_tlv),
315};
316
317static const struct snd_kcontrol_new ldac_in_control =
318 SOC_DAPM_ENUM("DAC Left Input", ldac_in_enum);
319
320static const struct snd_kcontrol_new rdac_in_control =
321 SOC_DAPM_ENUM("DAC Right Input", rdac_in_enum);
322
Mark Brown9296f4d2014-03-13 17:44:22 +0000323static int aic31xx_wait_bits(struct aic31xx_priv *aic31xx, unsigned int reg,
324 unsigned int mask, unsigned int wbits, int sleep,
325 int count)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200326{
327 unsigned int bits;
328 int counter = count;
329 int ret = regmap_read(aic31xx->regmap, reg, &bits);
Shahina Shaik423ca882014-06-13 11:56:54 +0530330
Jyri Sarhae00447f2014-03-11 12:57:32 +0200331 while ((bits & mask) != wbits && counter && !ret) {
332 usleep_range(sleep, sleep * 2);
333 ret = regmap_read(aic31xx->regmap, reg, &bits);
334 counter--;
335 }
336 if ((bits & mask) != wbits) {
337 dev_err(aic31xx->dev,
338 "%s: Failed! 0x%x was 0x%x expected 0x%x (%d, 0x%x, %d us)\n",
339 __func__, reg, bits, wbits, ret, mask,
340 (count - counter) * sleep);
341 ret = -1;
342 }
343 return ret;
344}
345
346#define WIDGET_BIT(reg, shift) (((shift) << 8) | (reg))
347
348static int aic31xx_dapm_power_event(struct snd_soc_dapm_widget *w,
349 struct snd_kcontrol *kcontrol, int event)
350{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000351 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
352 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200353 unsigned int reg = AIC31XX_DACFLAG1;
354 unsigned int mask;
355
356 switch (WIDGET_BIT(w->reg, w->shift)) {
357 case WIDGET_BIT(AIC31XX_DACSETUP, 7):
358 mask = AIC31XX_LDACPWRSTATUS_MASK;
359 break;
360 case WIDGET_BIT(AIC31XX_DACSETUP, 6):
361 mask = AIC31XX_RDACPWRSTATUS_MASK;
362 break;
363 case WIDGET_BIT(AIC31XX_HPDRIVER, 7):
364 mask = AIC31XX_HPLDRVPWRSTATUS_MASK;
365 break;
366 case WIDGET_BIT(AIC31XX_HPDRIVER, 6):
367 mask = AIC31XX_HPRDRVPWRSTATUS_MASK;
368 break;
369 case WIDGET_BIT(AIC31XX_SPKAMP, 7):
370 mask = AIC31XX_SPLDRVPWRSTATUS_MASK;
371 break;
372 case WIDGET_BIT(AIC31XX_SPKAMP, 6):
373 mask = AIC31XX_SPRDRVPWRSTATUS_MASK;
374 break;
375 case WIDGET_BIT(AIC31XX_ADCSETUP, 7):
376 mask = AIC31XX_ADCPWRSTATUS_MASK;
377 reg = AIC31XX_ADCFLAG;
378 break;
379 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000380 dev_err(component->dev, "Unknown widget '%s' calling %s\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +0200381 w->name, __func__);
382 return -EINVAL;
383 }
384
385 switch (event) {
386 case SND_SOC_DAPM_POST_PMU:
387 return aic31xx_wait_bits(aic31xx, reg, mask, mask, 5000, 100);
388 case SND_SOC_DAPM_POST_PMD:
389 return aic31xx_wait_bits(aic31xx, reg, mask, 0, 5000, 100);
390 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000391 dev_dbg(component->dev,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200392 "Unhandled dapm widget event %d from %s\n",
393 event, w->name);
394 }
395 return 0;
396}
397
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300398static const struct snd_kcontrol_new aic31xx_left_output_switches[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200399 SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
400 SOC_DAPM_SINGLE("From MIC1LP", AIC31XX_DACMIXERROUTE, 5, 1, 0),
401 SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 4, 1, 0),
402};
403
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300404static const struct snd_kcontrol_new aic31xx_right_output_switches[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200405 SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
406 SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 1, 1, 0),
407};
408
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300409static const struct snd_kcontrol_new dac31xx_left_output_switches[] = {
410 SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
411 SOC_DAPM_SINGLE("From AIN1", AIC31XX_DACMIXERROUTE, 5, 1, 0),
412 SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 4, 1, 0),
413};
414
415static const struct snd_kcontrol_new dac31xx_right_output_switches[] = {
416 SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
417 SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 1, 1, 0),
418};
419
Jyri Sarhae00447f2014-03-11 12:57:32 +0200420static const struct snd_kcontrol_new p_term_mic1lp =
421 SOC_DAPM_ENUM("MIC1LP P-Terminal", mic1lp_p_enum);
422
423static const struct snd_kcontrol_new p_term_mic1rp =
424 SOC_DAPM_ENUM("MIC1RP P-Terminal", mic1rp_p_enum);
425
426static const struct snd_kcontrol_new p_term_mic1lm =
427 SOC_DAPM_ENUM("MIC1LM P-Terminal", mic1lm_p_enum);
428
429static const struct snd_kcontrol_new m_term_mic1lm =
430 SOC_DAPM_ENUM("MIC1LM M-Terminal", mic1lm_m_enum);
431
432static const struct snd_kcontrol_new aic31xx_dapm_hpl_switch =
433 SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGHPL, 7, 1, 0);
434
435static const struct snd_kcontrol_new aic31xx_dapm_hpr_switch =
436 SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGHPR, 7, 1, 0);
437
438static const struct snd_kcontrol_new aic31xx_dapm_spl_switch =
439 SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGSPL, 7, 1, 0);
440
441static const struct snd_kcontrol_new aic31xx_dapm_spr_switch =
442 SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGSPR, 7, 1, 0);
443
444static int mic_bias_event(struct snd_soc_dapm_widget *w,
445 struct snd_kcontrol *kcontrol, int event)
446{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000447 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
448 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Shahina Shaik423ca882014-06-13 11:56:54 +0530449
Jyri Sarhae00447f2014-03-11 12:57:32 +0200450 switch (event) {
451 case SND_SOC_DAPM_POST_PMU:
452 /* change mic bias voltage to user defined */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000453 snd_soc_component_update_bits(component, AIC31XX_MICBIAS,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200454 AIC31XX_MICBIAS_MASK,
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600455 aic31xx->micbias_vg <<
Jyri Sarhae00447f2014-03-11 12:57:32 +0200456 AIC31XX_MICBIAS_SHIFT);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000457 dev_dbg(component->dev, "%s: turned on\n", __func__);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200458 break;
459 case SND_SOC_DAPM_PRE_PMD:
460 /* turn mic bias off */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000461 snd_soc_component_update_bits(component, AIC31XX_MICBIAS,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200462 AIC31XX_MICBIAS_MASK, 0);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000463 dev_dbg(component->dev, "%s: turned off\n", __func__);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200464 break;
465 }
466 return 0;
467}
468
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300469static const struct snd_soc_dapm_widget common31xx_dapm_widgets[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200470 SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
471
472 SND_SOC_DAPM_MUX("DAC Left Input",
473 SND_SOC_NOPM, 0, 0, &ldac_in_control),
474 SND_SOC_DAPM_MUX("DAC Right Input",
475 SND_SOC_NOPM, 0, 0, &rdac_in_control),
476 /* DACs */
477 SND_SOC_DAPM_DAC_E("DAC Left", "Left Playback",
478 AIC31XX_DACSETUP, 7, 0, aic31xx_dapm_power_event,
479 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
480
481 SND_SOC_DAPM_DAC_E("DAC Right", "Right Playback",
482 AIC31XX_DACSETUP, 6, 0, aic31xx_dapm_power_event,
483 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
484
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300485 /* HP */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200486 SND_SOC_DAPM_SWITCH("HP Left", SND_SOC_NOPM, 0, 0,
487 &aic31xx_dapm_hpl_switch),
488 SND_SOC_DAPM_SWITCH("HP Right", SND_SOC_NOPM, 0, 0,
489 &aic31xx_dapm_hpr_switch),
490
491 /* Output drivers */
492 SND_SOC_DAPM_OUT_DRV_E("HPL Driver", AIC31XX_HPDRIVER, 7, 0,
493 NULL, 0, aic31xx_dapm_power_event,
494 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
495 SND_SOC_DAPM_OUT_DRV_E("HPR Driver", AIC31XX_HPDRIVER, 6, 0,
496 NULL, 0, aic31xx_dapm_power_event,
497 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
498
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300499 /* Mic Bias */
500 SND_SOC_DAPM_SUPPLY("MICBIAS", SND_SOC_NOPM, 0, 0, mic_bias_event,
501 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
502
503 /* Outputs */
504 SND_SOC_DAPM_OUTPUT("HPL"),
505 SND_SOC_DAPM_OUTPUT("HPR"),
506};
507
508static const struct snd_soc_dapm_widget dac31xx_dapm_widgets[] = {
509 /* Inputs */
510 SND_SOC_DAPM_INPUT("AIN1"),
511 SND_SOC_DAPM_INPUT("AIN2"),
512
513 /* Output Mixers */
514 SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
515 dac31xx_left_output_switches,
516 ARRAY_SIZE(dac31xx_left_output_switches)),
517 SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
518 dac31xx_right_output_switches,
519 ARRAY_SIZE(dac31xx_right_output_switches)),
520};
521
522static const struct snd_soc_dapm_widget aic31xx_dapm_widgets[] = {
523 /* Inputs */
524 SND_SOC_DAPM_INPUT("MIC1LP"),
525 SND_SOC_DAPM_INPUT("MIC1RP"),
526 SND_SOC_DAPM_INPUT("MIC1LM"),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200527
528 /* Input Selection to MIC_PGA */
529 SND_SOC_DAPM_MUX("MIC1LP P-Terminal", SND_SOC_NOPM, 0, 0,
530 &p_term_mic1lp),
531 SND_SOC_DAPM_MUX("MIC1RP P-Terminal", SND_SOC_NOPM, 0, 0,
532 &p_term_mic1rp),
533 SND_SOC_DAPM_MUX("MIC1LM P-Terminal", SND_SOC_NOPM, 0, 0,
534 &p_term_mic1lm),
535
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300536 /* ADC */
537 SND_SOC_DAPM_ADC_E("ADC", "Capture", AIC31XX_ADCSETUP, 7, 0,
538 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
539 SND_SOC_DAPM_POST_PMD),
540
Jyri Sarhae00447f2014-03-11 12:57:32 +0200541 SND_SOC_DAPM_MUX("MIC1LM M-Terminal", SND_SOC_NOPM, 0, 0,
542 &m_term_mic1lm),
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300543
Jyri Sarhae00447f2014-03-11 12:57:32 +0200544 /* Enabling & Disabling MIC Gain Ctl */
545 SND_SOC_DAPM_PGA("MIC_GAIN_CTL", AIC31XX_MICPGA,
546 7, 1, NULL, 0),
547
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300548 /* Output Mixers */
549 SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
550 aic31xx_left_output_switches,
551 ARRAY_SIZE(aic31xx_left_output_switches)),
552 SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
553 aic31xx_right_output_switches,
554 ARRAY_SIZE(aic31xx_right_output_switches)),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200555};
556
557static const struct snd_soc_dapm_widget aic311x_dapm_widgets[] = {
558 /* AIC3111 and AIC3110 have stereo class-D amplifier */
559 SND_SOC_DAPM_OUT_DRV_E("SPL ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
560 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
561 SND_SOC_DAPM_POST_PMD),
562 SND_SOC_DAPM_OUT_DRV_E("SPR ClassD", AIC31XX_SPKAMP, 6, 0, NULL, 0,
563 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
564 SND_SOC_DAPM_POST_PMD),
565 SND_SOC_DAPM_SWITCH("Speaker Left", SND_SOC_NOPM, 0, 0,
566 &aic31xx_dapm_spl_switch),
567 SND_SOC_DAPM_SWITCH("Speaker Right", SND_SOC_NOPM, 0, 0,
568 &aic31xx_dapm_spr_switch),
569 SND_SOC_DAPM_OUTPUT("SPL"),
570 SND_SOC_DAPM_OUTPUT("SPR"),
571};
572
573/* AIC3100 and AIC3120 have only mono class-D amplifier */
574static const struct snd_soc_dapm_widget aic310x_dapm_widgets[] = {
575 SND_SOC_DAPM_OUT_DRV_E("SPK ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
576 aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
577 SND_SOC_DAPM_POST_PMD),
578 SND_SOC_DAPM_SWITCH("Speaker", SND_SOC_NOPM, 0, 0,
579 &aic31xx_dapm_spl_switch),
580 SND_SOC_DAPM_OUTPUT("SPK"),
581};
582
583static const struct snd_soc_dapm_route
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300584common31xx_audio_map[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200585 /* DAC Input Routing */
586 {"DAC Left Input", "Left Data", "DAC IN"},
587 {"DAC Left Input", "Right Data", "DAC IN"},
588 {"DAC Left Input", "Mono", "DAC IN"},
589 {"DAC Right Input", "Left Data", "DAC IN"},
590 {"DAC Right Input", "Right Data", "DAC IN"},
591 {"DAC Right Input", "Mono", "DAC IN"},
592 {"DAC Left", NULL, "DAC Left Input"},
593 {"DAC Right", NULL, "DAC Right Input"},
594
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300595 /* HPL path */
596 {"HP Left", "Switch", "Output Left"},
597 {"HPL Driver", NULL, "HP Left"},
598 {"HPL", NULL, "HPL Driver"},
599
600 /* HPR path */
601 {"HP Right", "Switch", "Output Right"},
602 {"HPR Driver", NULL, "HP Right"},
603 {"HPR", NULL, "HPR Driver"},
604};
605
606static const struct snd_soc_dapm_route
607dac31xx_audio_map[] = {
608 /* Left Output */
609 {"Output Left", "From Left DAC", "DAC Left"},
610 {"Output Left", "From AIN1", "AIN1"},
611 {"Output Left", "From AIN2", "AIN2"},
612
613 /* Right Output */
614 {"Output Right", "From Right DAC", "DAC Right"},
615 {"Output Right", "From AIN2", "AIN2"},
616};
617
618static const struct snd_soc_dapm_route
619aic31xx_audio_map[] = {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200620 /* Mic input */
621 {"MIC1LP P-Terminal", "FFR 10 Ohm", "MIC1LP"},
622 {"MIC1LP P-Terminal", "FFR 20 Ohm", "MIC1LP"},
623 {"MIC1LP P-Terminal", "FFR 40 Ohm", "MIC1LP"},
624 {"MIC1RP P-Terminal", "FFR 10 Ohm", "MIC1RP"},
625 {"MIC1RP P-Terminal", "FFR 20 Ohm", "MIC1RP"},
626 {"MIC1RP P-Terminal", "FFR 40 Ohm", "MIC1RP"},
627 {"MIC1LM P-Terminal", "FFR 10 Ohm", "MIC1LM"},
628 {"MIC1LM P-Terminal", "FFR 20 Ohm", "MIC1LM"},
629 {"MIC1LM P-Terminal", "FFR 40 Ohm", "MIC1LM"},
630
631 {"MIC1LM M-Terminal", "FFR 10 Ohm", "MIC1LM"},
632 {"MIC1LM M-Terminal", "FFR 20 Ohm", "MIC1LM"},
633 {"MIC1LM M-Terminal", "FFR 40 Ohm", "MIC1LM"},
634
635 {"MIC_GAIN_CTL", NULL, "MIC1LP P-Terminal"},
636 {"MIC_GAIN_CTL", NULL, "MIC1RP P-Terminal"},
637 {"MIC_GAIN_CTL", NULL, "MIC1LM P-Terminal"},
638 {"MIC_GAIN_CTL", NULL, "MIC1LM M-Terminal"},
639
640 {"ADC", NULL, "MIC_GAIN_CTL"},
641
642 /* Left Output */
643 {"Output Left", "From Left DAC", "DAC Left"},
644 {"Output Left", "From MIC1LP", "MIC1LP"},
645 {"Output Left", "From MIC1RP", "MIC1RP"},
646
647 /* Right Output */
648 {"Output Right", "From Right DAC", "DAC Right"},
649 {"Output Right", "From MIC1RP", "MIC1RP"},
Jyri Sarhae00447f2014-03-11 12:57:32 +0200650};
651
652static const struct snd_soc_dapm_route
653aic311x_audio_map[] = {
654 /* SP L path */
655 {"Speaker Left", "Switch", "Output Left"},
656 {"SPL ClassD", NULL, "Speaker Left"},
657 {"SPL", NULL, "SPL ClassD"},
658
659 /* SP R path */
660 {"Speaker Right", "Switch", "Output Right"},
661 {"SPR ClassD", NULL, "Speaker Right"},
662 {"SPR", NULL, "SPR ClassD"},
663};
664
665static const struct snd_soc_dapm_route
666aic310x_audio_map[] = {
667 /* SP L path */
668 {"Speaker", "Switch", "Output Left"},
669 {"SPK ClassD", NULL, "Speaker"},
670 {"SPK", NULL, "SPK ClassD"},
671};
672
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000673static int aic31xx_add_controls(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200674{
675 int ret = 0;
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000676 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200677
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600678 if (!(aic31xx->codec_type & DAC31XX_BIT))
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000679 ret = snd_soc_add_component_controls(
680 component, aic31xx_snd_controls,
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300681 ARRAY_SIZE(aic31xx_snd_controls));
682 if (ret)
683 return ret;
684
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600685 if (aic31xx->codec_type & AIC31XX_STEREO_CLASS_D_BIT)
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000686 ret = snd_soc_add_component_controls(
687 component, aic311x_snd_controls,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200688 ARRAY_SIZE(aic311x_snd_controls));
689 else
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000690 ret = snd_soc_add_component_controls(
691 component, aic310x_snd_controls,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200692 ARRAY_SIZE(aic310x_snd_controls));
693
694 return ret;
695}
696
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000697static int aic31xx_add_widgets(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200698{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000699 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
700 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200701 int ret = 0;
702
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600703 if (aic31xx->codec_type & DAC31XX_BIT) {
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +0300704 ret = snd_soc_dapm_new_controls(
705 dapm, dac31xx_dapm_widgets,
706 ARRAY_SIZE(dac31xx_dapm_widgets));
707 if (ret)
708 return ret;
709
710 ret = snd_soc_dapm_add_routes(dapm, dac31xx_audio_map,
711 ARRAY_SIZE(dac31xx_audio_map));
712 if (ret)
713 return ret;
714 } else {
715 ret = snd_soc_dapm_new_controls(
716 dapm, aic31xx_dapm_widgets,
717 ARRAY_SIZE(aic31xx_dapm_widgets));
718 if (ret)
719 return ret;
720
721 ret = snd_soc_dapm_add_routes(dapm, aic31xx_audio_map,
722 ARRAY_SIZE(aic31xx_audio_map));
723 if (ret)
724 return ret;
725 }
726
Andrew F. Daviscd15da82017-12-07 09:38:52 -0600727 if (aic31xx->codec_type & AIC31XX_STEREO_CLASS_D_BIT) {
Jyri Sarhae00447f2014-03-11 12:57:32 +0200728 ret = snd_soc_dapm_new_controls(
729 dapm, aic311x_dapm_widgets,
730 ARRAY_SIZE(aic311x_dapm_widgets));
731 if (ret)
732 return ret;
733
734 ret = snd_soc_dapm_add_routes(dapm, aic311x_audio_map,
735 ARRAY_SIZE(aic311x_audio_map));
736 if (ret)
737 return ret;
738 } else {
739 ret = snd_soc_dapm_new_controls(
740 dapm, aic310x_dapm_widgets,
741 ARRAY_SIZE(aic310x_dapm_widgets));
742 if (ret)
743 return ret;
744
745 ret = snd_soc_dapm_add_routes(dapm, aic310x_audio_map,
746 ARRAY_SIZE(aic310x_audio_map));
747 if (ret)
748 return ret;
749 }
750
751 return 0;
752}
753
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000754static int aic31xx_setup_pll(struct snd_soc_component *component,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200755 struct snd_pcm_hw_params *params)
756{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000757 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarha03be88e2014-09-03 15:52:33 +0300758 int bclk_score = snd_soc_params_to_frame_size(params);
Andrew F. Davisc6b8c772017-11-29 15:32:52 -0600759 int mclk_p;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200760 int bclk_n = 0;
Jyri Sarha03be88e2014-09-03 15:52:33 +0300761 int match = -1;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200762 int i;
763
Andrew F. Davisc6b8c772017-11-29 15:32:52 -0600764 if (!aic31xx->sysclk || !aic31xx->p_div) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000765 dev_err(component->dev, "Master clock not supplied\n");
Andrew F. Davisc6b8c772017-11-29 15:32:52 -0600766 return -EINVAL;
767 }
768 mclk_p = aic31xx->sysclk / aic31xx->p_div;
769
Jyri Sarhae00447f2014-03-11 12:57:32 +0200770 /* Use PLL as CODEC_CLKIN and DAC_CLK as BDIV_CLKIN */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000771 snd_soc_component_update_bits(component, AIC31XX_CLKMUX,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200772 AIC31XX_CODEC_CLKIN_MASK, AIC31XX_CODEC_CLKIN_PLL);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000773 snd_soc_component_update_bits(component, AIC31XX_IFACE2,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200774 AIC31XX_BDIVCLK_MASK, AIC31XX_DAC2BCLK);
775
776 for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++) {
777 if (aic31xx_divs[i].rate == params_rate(params) &&
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300778 aic31xx_divs[i].mclk_p == mclk_p) {
Jyri Sarha03be88e2014-09-03 15:52:33 +0300779 int s = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) %
780 snd_soc_params_to_frame_size(params);
781 int bn = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) /
782 snd_soc_params_to_frame_size(params);
783 if (s < bclk_score && bn > 0) {
784 match = i;
785 bclk_n = bn;
786 bclk_score = s;
787 }
788 }
Jyri Sarhae00447f2014-03-11 12:57:32 +0200789 }
790
Jyri Sarha03be88e2014-09-03 15:52:33 +0300791 if (match == -1) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000792 dev_err(component->dev,
Jyri Sarha03be88e2014-09-03 15:52:33 +0300793 "%s: Sample rate (%u) and format not supported\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +0200794 __func__, params_rate(params));
Jyri Sarha03be88e2014-09-03 15:52:33 +0300795 /* See bellow for details how fix this. */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200796 return -EINVAL;
797 }
Jyri Sarha03be88e2014-09-03 15:52:33 +0300798 if (bclk_score != 0) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000799 dev_warn(component->dev, "Can not produce exact bitclock");
Jyri Sarha03be88e2014-09-03 15:52:33 +0300800 /* This is fine if using dsp format, but if using i2s
801 there may be trouble. To fix the issue edit the
802 aic31xx_divs table for your mclk and sample
803 rate. Details can be found from:
804 http://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
805 Section: 5.6 CLOCK Generation and PLL
806 */
807 }
808 i = match;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200809
810 /* PLL configuration */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000811 snd_soc_component_update_bits(component, AIC31XX_PLLPR, AIC31XX_PLL_MASK,
Jyri Sarha7ed36e92014-09-03 15:52:34 +0300812 (aic31xx->p_div << 4) | 0x01);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000813 snd_soc_component_write(component, AIC31XX_PLLJ, aic31xx_divs[i].pll_j);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200814
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000815 snd_soc_component_write(component, AIC31XX_PLLDMSB,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200816 aic31xx_divs[i].pll_d >> 8);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000817 snd_soc_component_write(component, AIC31XX_PLLDLSB,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200818 aic31xx_divs[i].pll_d & 0xff);
819
820 /* DAC dividers configuration */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000821 snd_soc_component_update_bits(component, AIC31XX_NDAC, AIC31XX_PLL_MASK,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200822 aic31xx_divs[i].ndac);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000823 snd_soc_component_update_bits(component, AIC31XX_MDAC, AIC31XX_PLL_MASK,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200824 aic31xx_divs[i].mdac);
825
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000826 snd_soc_component_write(component, AIC31XX_DOSRMSB, aic31xx_divs[i].dosr >> 8);
827 snd_soc_component_write(component, AIC31XX_DOSRLSB, aic31xx_divs[i].dosr & 0xff);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200828
829 /* ADC dividers configuration. Write reset value 1 if not used. */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000830 snd_soc_component_update_bits(component, AIC31XX_NADC, AIC31XX_PLL_MASK,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200831 aic31xx_divs[i].nadc ? aic31xx_divs[i].nadc : 1);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000832 snd_soc_component_update_bits(component, AIC31XX_MADC, AIC31XX_PLL_MASK,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200833 aic31xx_divs[i].madc ? aic31xx_divs[i].madc : 1);
834
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000835 snd_soc_component_write(component, AIC31XX_AOSR, aic31xx_divs[i].aosr);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200836
837 /* Bit clock divider configuration. */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000838 snd_soc_component_update_bits(component, AIC31XX_BCLKN,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200839 AIC31XX_PLL_MASK, bclk_n);
840
841 aic31xx->rate_div_line = i;
842
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000843 dev_dbg(component->dev,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200844 "pll %d.%04d/%d dosr %d n %d m %d aosr %d n %d m %d bclk_n %d\n",
Andrew F. Davisca7840f2017-11-29 15:32:45 -0600845 aic31xx_divs[i].pll_j,
846 aic31xx_divs[i].pll_d,
847 aic31xx->p_div,
848 aic31xx_divs[i].dosr,
849 aic31xx_divs[i].ndac,
850 aic31xx_divs[i].mdac,
851 aic31xx_divs[i].aosr,
852 aic31xx_divs[i].nadc,
853 aic31xx_divs[i].madc,
854 bclk_n
855 );
Jyri Sarhae00447f2014-03-11 12:57:32 +0200856
857 return 0;
858}
859
860static int aic31xx_hw_params(struct snd_pcm_substream *substream,
861 struct snd_pcm_hw_params *params,
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100862 struct snd_soc_dai *dai)
Jyri Sarhae00447f2014-03-11 12:57:32 +0200863{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000864 struct snd_soc_component *component = dai->component;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200865 u8 data = 0;
866
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000867 dev_dbg(component->dev, "## %s: width %d rate %d\n",
Mark Brown88be6812014-07-31 12:48:13 +0100868 __func__, params_width(params),
Jyri Sarhae00447f2014-03-11 12:57:32 +0200869 params_rate(params));
870
871 switch (params_width(params)) {
872 case 16:
873 break;
874 case 20:
875 data = (AIC31XX_WORD_LEN_20BITS <<
876 AIC31XX_IFACE1_DATALEN_SHIFT);
877 break;
878 case 24:
879 data = (AIC31XX_WORD_LEN_24BITS <<
880 AIC31XX_IFACE1_DATALEN_SHIFT);
881 break;
882 case 32:
883 data = (AIC31XX_WORD_LEN_32BITS <<
884 AIC31XX_IFACE1_DATALEN_SHIFT);
885 break;
886 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000887 dev_err(component->dev, "%s: Unsupported width %d\n",
Mark Brown88be6812014-07-31 12:48:13 +0100888 __func__, params_width(params));
Jyri Sarhae00447f2014-03-11 12:57:32 +0200889 return -EINVAL;
890 }
891
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000892 snd_soc_component_update_bits(component, AIC31XX_IFACE1,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200893 AIC31XX_IFACE1_DATALEN_MASK,
894 data);
895
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000896 return aic31xx_setup_pll(component, params);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200897}
898
899static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute)
900{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000901 struct snd_soc_component *component = codec_dai->component;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200902
903 if (mute) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000904 snd_soc_component_update_bits(component, AIC31XX_DACMUTE,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200905 AIC31XX_DACMUTE_MASK,
906 AIC31XX_DACMUTE_MASK);
907 } else {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000908 snd_soc_component_update_bits(component, AIC31XX_DACMUTE,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200909 AIC31XX_DACMUTE_MASK, 0x0);
910 }
911
912 return 0;
913}
914
915static int aic31xx_set_dai_fmt(struct snd_soc_dai *codec_dai,
916 unsigned int fmt)
917{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000918 struct snd_soc_component *component = codec_dai->component;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200919 u8 iface_reg1 = 0;
Peter Ujfalusi085f3ec2014-09-01 12:46:37 +0300920 u8 iface_reg2 = 0;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200921 u8 dsp_a_val = 0;
922
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000923 dev_dbg(component->dev, "## %s: fmt = 0x%x\n", __func__, fmt);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200924
925 /* set master/slave audio interface */
926 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
927 case SND_SOC_DAIFMT_CBM_CFM:
928 iface_reg1 |= AIC31XX_BCLK_MASTER | AIC31XX_WCLK_MASTER;
929 break;
Andrew F. Davis77f8b3c2017-11-29 15:32:53 -0600930 case SND_SOC_DAIFMT_CBS_CFM:
931 iface_reg1 |= AIC31XX_WCLK_MASTER;
932 break;
933 case SND_SOC_DAIFMT_CBM_CFS:
934 iface_reg1 |= AIC31XX_BCLK_MASTER;
935 break;
936 case SND_SOC_DAIFMT_CBS_CFS:
937 break;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200938 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000939 dev_err(component->dev, "Invalid DAI master/slave interface\n");
Jyri Sarhae00447f2014-03-11 12:57:32 +0200940 return -EINVAL;
941 }
942
Andrew F. Davisdcb407b2017-11-29 15:32:54 -0600943 /* signal polarity */
944 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
945 case SND_SOC_DAIFMT_NB_NF:
946 break;
947 case SND_SOC_DAIFMT_IB_NF:
948 iface_reg2 |= AIC31XX_BCLKINV_MASK;
949 break;
950 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000951 dev_err(component->dev, "Invalid DAI clock signal polarity\n");
Andrew F. Davisdcb407b2017-11-29 15:32:54 -0600952 return -EINVAL;
953 }
954
Jyri Sarhae00447f2014-03-11 12:57:32 +0200955 /* interface format */
956 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
957 case SND_SOC_DAIFMT_I2S:
958 break;
959 case SND_SOC_DAIFMT_DSP_A:
Gustavo A. R. Silva09fc38c2017-11-08 14:04:17 -0600960 dsp_a_val = 0x1; /* fall through */
Jyri Sarhae00447f2014-03-11 12:57:32 +0200961 case SND_SOC_DAIFMT_DSP_B:
Andrew F. Davisdcb407b2017-11-29 15:32:54 -0600962 /*
963 * NOTE: This CODEC samples on the falling edge of BCLK in
964 * DSP mode, this is inverted compared to what most DAIs
965 * expect, so we invert for this mode
966 */
967 iface_reg2 ^= AIC31XX_BCLKINV_MASK;
Jyri Sarhae00447f2014-03-11 12:57:32 +0200968 iface_reg1 |= (AIC31XX_DSP_MODE <<
969 AIC31XX_IFACE1_DATATYPE_SHIFT);
970 break;
971 case SND_SOC_DAIFMT_RIGHT_J:
972 iface_reg1 |= (AIC31XX_RIGHT_JUSTIFIED_MODE <<
973 AIC31XX_IFACE1_DATATYPE_SHIFT);
974 break;
975 case SND_SOC_DAIFMT_LEFT_J:
976 iface_reg1 |= (AIC31XX_LEFT_JUSTIFIED_MODE <<
977 AIC31XX_IFACE1_DATATYPE_SHIFT);
978 break;
979 default:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000980 dev_err(component->dev, "Invalid DAI interface format\n");
Jyri Sarhae00447f2014-03-11 12:57:32 +0200981 return -EINVAL;
982 }
983
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000984 snd_soc_component_update_bits(component, AIC31XX_IFACE1,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200985 AIC31XX_IFACE1_DATATYPE_MASK |
986 AIC31XX_IFACE1_MASTER_MASK,
987 iface_reg1);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000988 snd_soc_component_update_bits(component, AIC31XX_DATA_OFFSET,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200989 AIC31XX_DATA_OFFSET_MASK,
990 dsp_a_val);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +0000991 snd_soc_component_update_bits(component, AIC31XX_IFACE2,
Jyri Sarhae00447f2014-03-11 12:57:32 +0200992 AIC31XX_BCLKINV_MASK,
Peter Ujfalusi085f3ec2014-09-01 12:46:37 +0300993 iface_reg2);
Jyri Sarhae00447f2014-03-11 12:57:32 +0200994
995 return 0;
996}
997
998static int aic31xx_set_dai_sysclk(struct snd_soc_dai *codec_dai,
999 int clk_id, unsigned int freq, int dir)
1000{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001001 struct snd_soc_component *component = codec_dai->component;
1002 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001003 int i;
1004
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001005 dev_dbg(component->dev, "## %s: clk_id = %d, freq = %d, dir = %d\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +02001006 __func__, clk_id, freq, dir);
1007
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001008 for (i = 1; i < 8; i++)
1009 if (freq / i <= 20000000)
1010 break;
Jyri Sarha7ed36e92014-09-03 15:52:34 +03001011 if (freq/i > 20000000) {
1012 dev_err(aic31xx->dev, "%s: Too high mclk frequency %u\n",
1013 __func__, freq);
1014 return -EINVAL;
1015 }
1016 aic31xx->p_div = i;
1017
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001018 for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++)
1019 if (aic31xx_divs[i].mclk_p == freq / aic31xx->p_div)
1020 break;
Jyri Sarhabbc686b2014-11-24 20:37:12 +02001021 if (i == ARRAY_SIZE(aic31xx_divs)) {
1022 dev_err(aic31xx->dev, "%s: Unsupported frequency %d\n",
1023 __func__, freq);
1024 return -EINVAL;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001025 }
1026
1027 /* set clock on MCLK, BCLK, or GPIO1 as PLL input */
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001028 snd_soc_component_update_bits(component, AIC31XX_CLKMUX, AIC31XX_PLL_CLKIN_MASK,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001029 clk_id << AIC31XX_PLL_CLKIN_SHIFT);
1030
1031 aic31xx->sysclk = freq;
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001032
Jyri Sarhae00447f2014-03-11 12:57:32 +02001033 return 0;
1034}
1035
1036static int aic31xx_regulator_event(struct notifier_block *nb,
1037 unsigned long event, void *data)
1038{
1039 struct aic31xx_disable_nb *disable_nb =
1040 container_of(nb, struct aic31xx_disable_nb, nb);
1041 struct aic31xx_priv *aic31xx = disable_nb->aic31xx;
1042
1043 if (event & REGULATOR_EVENT_DISABLE) {
1044 /*
1045 * Put codec to reset and as at least one of the
1046 * supplies was disabled.
1047 */
Andrew F. Davisb6b247c2017-11-29 15:32:49 -06001048 if (aic31xx->gpio_reset)
1049 gpiod_set_value(aic31xx->gpio_reset, 1);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001050
1051 regcache_mark_dirty(aic31xx->regmap);
1052 dev_dbg(aic31xx->dev, "## %s: DISABLE received\n", __func__);
1053 }
1054
1055 return 0;
1056}
1057
Andrew F. Davis0ce918c2017-12-07 09:38:56 -06001058static int aic31xx_reset(struct aic31xx_priv *aic31xx)
1059{
1060 int ret = 0;
1061
1062 if (aic31xx->gpio_reset) {
1063 gpiod_set_value(aic31xx->gpio_reset, 1);
1064 ndelay(10); /* At least 10ns */
1065 gpiod_set_value(aic31xx->gpio_reset, 0);
1066 } else {
1067 ret = regmap_write(aic31xx->regmap, AIC31XX_RESET, 1);
1068 }
1069 mdelay(1); /* At least 1ms */
1070
1071 return ret;
1072}
1073
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001074static void aic31xx_clk_on(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001075{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001076 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001077 u8 mask = AIC31XX_PM_MASK;
1078 u8 on = AIC31XX_PM_MASK;
1079
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001080 dev_dbg(component->dev, "codec clock -> on (rate %d)\n",
Jyri Sarhae00447f2014-03-11 12:57:32 +02001081 aic31xx_divs[aic31xx->rate_div_line].rate);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001082 snd_soc_component_update_bits(component, AIC31XX_PLLPR, mask, on);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001083 mdelay(10);
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001084 snd_soc_component_update_bits(component, AIC31XX_NDAC, mask, on);
1085 snd_soc_component_update_bits(component, AIC31XX_MDAC, mask, on);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001086 if (aic31xx_divs[aic31xx->rate_div_line].nadc)
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001087 snd_soc_component_update_bits(component, AIC31XX_NADC, mask, on);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001088 if (aic31xx_divs[aic31xx->rate_div_line].madc)
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001089 snd_soc_component_update_bits(component, AIC31XX_MADC, mask, on);
1090 snd_soc_component_update_bits(component, AIC31XX_BCLKN, mask, on);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001091}
1092
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001093static void aic31xx_clk_off(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001094{
Jyri Sarhae00447f2014-03-11 12:57:32 +02001095 u8 mask = AIC31XX_PM_MASK;
1096 u8 off = 0;
1097
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001098 dev_dbg(component->dev, "codec clock -> off\n");
1099 snd_soc_component_update_bits(component, AIC31XX_BCLKN, mask, off);
1100 snd_soc_component_update_bits(component, AIC31XX_MADC, mask, off);
1101 snd_soc_component_update_bits(component, AIC31XX_NADC, mask, off);
1102 snd_soc_component_update_bits(component, AIC31XX_MDAC, mask, off);
1103 snd_soc_component_update_bits(component, AIC31XX_NDAC, mask, off);
1104 snd_soc_component_update_bits(component, AIC31XX_PLLPR, mask, off);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001105}
1106
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001107static int aic31xx_power_on(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001108{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001109 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001110 int ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001111
1112 ret = regulator_bulk_enable(ARRAY_SIZE(aic31xx->supplies),
1113 aic31xx->supplies);
1114 if (ret)
1115 return ret;
1116
Jyri Sarhae00447f2014-03-11 12:57:32 +02001117 regcache_cache_only(aic31xx->regmap, false);
Andrew F. Davis0ce918c2017-12-07 09:38:56 -06001118
1119 /* Reset device registers for a consistent power-on like state */
1120 ret = aic31xx_reset(aic31xx);
1121 if (ret < 0)
1122 dev_err(aic31xx->dev, "Could not reset device: %d\n", ret);
1123
Jyri Sarhae00447f2014-03-11 12:57:32 +02001124 ret = regcache_sync(aic31xx->regmap);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001125 if (ret) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001126 dev_err(component->dev,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001127 "Failed to restore cache: %d\n", ret);
1128 regcache_cache_only(aic31xx->regmap, true);
1129 regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
1130 aic31xx->supplies);
1131 return ret;
1132 }
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001133
Jyri Sarhae00447f2014-03-11 12:57:32 +02001134 return 0;
1135}
1136
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001137static void aic31xx_power_off(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001138{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001139 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001140
1141 regcache_cache_only(aic31xx->regmap, true);
Andrew F. Davisc7734e82017-11-29 15:32:43 -06001142 regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
1143 aic31xx->supplies);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001144}
1145
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001146static int aic31xx_set_bias_level(struct snd_soc_component *component,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001147 enum snd_soc_bias_level level)
1148{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001149 dev_dbg(component->dev, "## %s: %d -> %d\n", __func__,
1150 snd_soc_component_get_bias_level(component), level);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001151
1152 switch (level) {
1153 case SND_SOC_BIAS_ON:
1154 break;
1155 case SND_SOC_BIAS_PREPARE:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001156 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
1157 aic31xx_clk_on(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001158 break;
1159 case SND_SOC_BIAS_STANDBY:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001160 switch (snd_soc_component_get_bias_level(component)) {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001161 case SND_SOC_BIAS_OFF:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001162 aic31xx_power_on(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001163 break;
1164 case SND_SOC_BIAS_PREPARE:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001165 aic31xx_clk_off(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001166 break;
1167 default:
1168 BUG();
1169 }
1170 break;
1171 case SND_SOC_BIAS_OFF:
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001172 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
1173 aic31xx_power_off(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001174 break;
1175 }
Jyri Sarhae00447f2014-03-11 12:57:32 +02001176
1177 return 0;
1178}
1179
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001180static int aic31xx_codec_probe(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001181{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001182 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001183 int i, ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001184
1185 dev_dbg(aic31xx->dev, "## %s\n", __func__);
1186
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001187 aic31xx->component = component;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001188
Jyri Sarhae00447f2014-03-11 12:57:32 +02001189 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++) {
1190 aic31xx->disable_nb[i].nb.notifier_call =
1191 aic31xx_regulator_event;
1192 aic31xx->disable_nb[i].aic31xx = aic31xx;
1193 ret = regulator_register_notifier(aic31xx->supplies[i].consumer,
1194 &aic31xx->disable_nb[i].nb);
1195 if (ret) {
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001196 dev_err(component->dev,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001197 "Failed to request regulator notifier: %d\n",
1198 ret);
1199 return ret;
1200 }
1201 }
1202
1203 regcache_cache_only(aic31xx->regmap, true);
1204 regcache_mark_dirty(aic31xx->regmap);
1205
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001206 ret = aic31xx_add_controls(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001207 if (ret)
1208 return ret;
1209
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001210 ret = aic31xx_add_widgets(component);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001211 if (ret)
1212 return ret;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001213
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001214 return 0;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001215}
1216
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001217static void aic31xx_codec_remove(struct snd_soc_component *component)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001218{
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001219 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001220 int i;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001221
1222 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
1223 regulator_unregister_notifier(aic31xx->supplies[i].consumer,
1224 &aic31xx->disable_nb[i].nb);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001225}
1226
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001227static const struct snd_soc_component_driver soc_codec_driver_aic31xx = {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001228 .probe = aic31xx_codec_probe,
1229 .remove = aic31xx_codec_remove,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001230 .set_bias_level = aic31xx_set_bias_level,
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001231 .controls = common31xx_snd_controls,
1232 .num_controls = ARRAY_SIZE(common31xx_snd_controls),
1233 .dapm_widgets = common31xx_dapm_widgets,
1234 .num_dapm_widgets = ARRAY_SIZE(common31xx_dapm_widgets),
1235 .dapm_routes = common31xx_audio_map,
1236 .num_dapm_routes = ARRAY_SIZE(common31xx_audio_map),
1237 .suspend_bias_off = 1,
1238 .idle_bias_on = 1,
1239 .use_pmdown_time = 1,
1240 .endianness = 1,
1241 .non_legacy_dai_naming = 1,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001242};
1243
Axel Lin64793042015-07-15 15:38:14 +08001244static const struct snd_soc_dai_ops aic31xx_dai_ops = {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001245 .hw_params = aic31xx_hw_params,
1246 .set_sysclk = aic31xx_set_dai_sysclk,
1247 .set_fmt = aic31xx_set_dai_fmt,
1248 .digital_mute = aic31xx_dac_mute,
1249};
1250
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001251static struct snd_soc_dai_driver dac31xx_dai_driver[] = {
1252 {
Tomas Vilda51827c42017-05-13 00:29:37 +03001253 .name = "tlv320dac31xx-hifi",
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001254 .playback = {
1255 .stream_name = "Playback",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001256 .channels_min = 2,
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001257 .channels_max = 2,
1258 .rates = AIC31XX_RATES,
1259 .formats = AIC31XX_FORMATS,
1260 },
1261 .ops = &aic31xx_dai_ops,
1262 .symmetric_rates = 1,
1263 }
1264};
1265
Jyri Sarhae00447f2014-03-11 12:57:32 +02001266static struct snd_soc_dai_driver aic31xx_dai_driver[] = {
1267 {
1268 .name = "tlv320aic31xx-hifi",
1269 .playback = {
1270 .stream_name = "Playback",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001271 .channels_min = 2,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001272 .channels_max = 2,
1273 .rates = AIC31XX_RATES,
1274 .formats = AIC31XX_FORMATS,
1275 },
1276 .capture = {
1277 .stream_name = "Capture",
Nikita Yushchenko35206462016-09-27 11:30:15 +03001278 .channels_min = 2,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001279 .channels_max = 2,
1280 .rates = AIC31XX_RATES,
1281 .formats = AIC31XX_FORMATS,
1282 },
1283 .ops = &aic31xx_dai_ops,
1284 .symmetric_rates = 1,
1285 }
1286};
1287
1288#if defined(CONFIG_OF)
1289static const struct of_device_id tlv320aic31xx_of_match[] = {
1290 { .compatible = "ti,tlv320aic310x" },
1291 { .compatible = "ti,tlv320aic311x" },
1292 { .compatible = "ti,tlv320aic3100" },
1293 { .compatible = "ti,tlv320aic3110" },
1294 { .compatible = "ti,tlv320aic3120" },
1295 { .compatible = "ti,tlv320aic3111" },
Peter Ujfalusia4a1d792016-11-10 09:55:54 +02001296 { .compatible = "ti,tlv320dac3100" },
Peter Ujfalusi4e2cc812016-11-10 09:55:55 +02001297 { .compatible = "ti,tlv320dac3101" },
Jyri Sarhae00447f2014-03-11 12:57:32 +02001298 {},
1299};
1300MODULE_DEVICE_TABLE(of, tlv320aic31xx_of_match);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001301#endif /* CONFIG_OF */
1302
Andrew F. Davis09303602017-11-29 15:32:44 -06001303#ifdef CONFIG_ACPI
1304static const struct acpi_device_id aic31xx_acpi_match[] = {
1305 { "10TI3100", 0 },
1306 { }
1307};
1308MODULE_DEVICE_TABLE(acpi, aic31xx_acpi_match);
1309#endif
1310
Andrew F. Davise88c3882017-11-29 15:32:48 -06001311static int aic31xx_i2c_probe(struct i2c_client *i2c,
1312 const struct i2c_device_id *id)
Jyri Sarhae00447f2014-03-11 12:57:32 +02001313{
Andrew F. Davise88c3882017-11-29 15:32:48 -06001314 struct aic31xx_priv *aic31xx;
Andrew F. Daviscd15da82017-12-07 09:38:52 -06001315 unsigned int micbias_value = MICBIAS_2_0V;
Andrew F. Davise88c3882017-11-29 15:32:48 -06001316 int i, ret;
1317
1318 dev_dbg(&i2c->dev, "## %s: %s codec_type = %d\n", __func__,
1319 id->name, (int)id->driver_data);
1320
1321 aic31xx = devm_kzalloc(&i2c->dev, sizeof(*aic31xx), GFP_KERNEL);
1322 if (!aic31xx)
1323 return -ENOMEM;
1324
1325 aic31xx->regmap = devm_regmap_init_i2c(i2c, &aic31xx_i2c_regmap);
1326 if (IS_ERR(aic31xx->regmap)) {
1327 ret = PTR_ERR(aic31xx->regmap);
1328 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1329 ret);
1330 return ret;
1331 }
1332 aic31xx->dev = &i2c->dev;
1333
Andrew F. Daviscd15da82017-12-07 09:38:52 -06001334 aic31xx->codec_type = id->driver_data;
Jyri Sarhae00447f2014-03-11 12:57:32 +02001335
1336 dev_set_drvdata(aic31xx->dev, aic31xx);
1337
Andrew F. Daviscd15da82017-12-07 09:38:52 -06001338 fwnode_property_read_u32(aic31xx->dev->fwnode, "ai31xx-micbias-vg",
1339 &micbias_value);
1340 switch (micbias_value) {
1341 case MICBIAS_2_0V:
1342 case MICBIAS_2_5V:
1343 case MICBIAS_AVDDV:
1344 aic31xx->micbias_vg = micbias_value;
1345 break;
1346 default:
1347 dev_err(aic31xx->dev, "Bad ai31xx-micbias-vg value %d\n",
1348 micbias_value);
1349 aic31xx->micbias_vg = MICBIAS_2_0V;
1350 }
1351
1352 if (dev_get_platdata(aic31xx->dev)) {
1353 memcpy(&aic31xx->pdata, dev_get_platdata(aic31xx->dev), sizeof(aic31xx->pdata));
1354 aic31xx->codec_type = aic31xx->pdata.codec_type;
1355 aic31xx->micbias_vg = aic31xx->pdata.micbias_vg;
1356 }
Jyri Sarhae00447f2014-03-11 12:57:32 +02001357
Andrew F. Davisb6b247c2017-11-29 15:32:49 -06001358 aic31xx->gpio_reset = devm_gpiod_get_optional(aic31xx->dev, "reset",
1359 GPIOD_OUT_LOW);
1360 if (IS_ERR(aic31xx->gpio_reset)) {
1361 dev_err(aic31xx->dev, "not able to acquire gpio\n");
1362 return PTR_ERR(aic31xx->gpio_reset);
Jyri Sarhae00447f2014-03-11 12:57:32 +02001363 }
1364
1365 for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
1366 aic31xx->supplies[i].supply = aic31xx_supply_names[i];
1367
1368 ret = devm_regulator_bulk_get(aic31xx->dev,
1369 ARRAY_SIZE(aic31xx->supplies),
1370 aic31xx->supplies);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001371 if (ret) {
Jyri Sarhae00447f2014-03-11 12:57:32 +02001372 dev_err(aic31xx->dev, "Failed to request supplies: %d\n", ret);
Andrew F. Davisca7840f2017-11-29 15:32:45 -06001373 return ret;
1374 }
Jyri Sarhae00447f2014-03-11 12:57:32 +02001375
Andrew F. Daviscd15da82017-12-07 09:38:52 -06001376 if (aic31xx->codec_type & DAC31XX_BIT)
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001377 return devm_snd_soc_register_component(&i2c->dev,
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001378 &soc_codec_driver_aic31xx,
1379 dac31xx_dai_driver,
1380 ARRAY_SIZE(dac31xx_dai_driver));
1381 else
Kuninori Morimotof2e6f95b2018-01-29 04:15:27 +00001382 return devm_snd_soc_register_component(&i2c->dev,
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001383 &soc_codec_driver_aic31xx,
1384 aic31xx_dai_driver,
1385 ARRAY_SIZE(aic31xx_dai_driver));
Jyri Sarhae00447f2014-03-11 12:57:32 +02001386}
1387
Jyri Sarhae00447f2014-03-11 12:57:32 +02001388static const struct i2c_device_id aic31xx_i2c_id[] = {
1389 { "tlv320aic310x", AIC3100 },
1390 { "tlv320aic311x", AIC3110 },
1391 { "tlv320aic3100", AIC3100 },
1392 { "tlv320aic3110", AIC3110 },
1393 { "tlv320aic3120", AIC3120 },
1394 { "tlv320aic3111", AIC3111 },
Nikita Yushchenkoef9656b2016-09-23 14:52:52 +03001395 { "tlv320dac3100", DAC3100 },
Peter Ujfalusi4e2cc812016-11-10 09:55:55 +02001396 { "tlv320dac3101", DAC3101 },
Jyri Sarhae00447f2014-03-11 12:57:32 +02001397 { }
1398};
1399MODULE_DEVICE_TABLE(i2c, aic31xx_i2c_id);
1400
1401static struct i2c_driver aic31xx_i2c_driver = {
1402 .driver = {
1403 .name = "tlv320aic31xx-codec",
Jyri Sarhae00447f2014-03-11 12:57:32 +02001404 .of_match_table = of_match_ptr(tlv320aic31xx_of_match),
Bastien Noceraf5cc1772016-04-19 18:00:20 +02001405 .acpi_match_table = ACPI_PTR(aic31xx_acpi_match),
Jyri Sarhae00447f2014-03-11 12:57:32 +02001406 },
1407 .probe = aic31xx_i2c_probe,
Jyri Sarhae00447f2014-03-11 12:57:32 +02001408 .id_table = aic31xx_i2c_id,
1409};
Jyri Sarhae00447f2014-03-11 12:57:32 +02001410module_i2c_driver(aic31xx_i2c_driver);
1411
Andrew F. Davisb1c52b72017-11-29 15:32:42 -06001412MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1413MODULE_DESCRIPTION("ASoC TLV320AIC31xx CODEC Driver");
1414MODULE_LICENSE("GPL v2");