blob: 8af344b2fdbf6a9cb9ca91d4e24e680adcfb9e15 [file] [log] [blame]
Kuninori Morimotoc0ca5602018-07-30 07:52:46 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// DA7210 ALSA Soc codec driver
4//
5// Copyright (c) 2009 Dialog Semiconductor
6// Written by David Chen <Dajun.chen@diasemi.com>
7//
8// Copyright (C) 2009 Renesas Solutions Corp.
9// Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
10//
11// Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
Kuninori Morimoto98615452009-12-14 13:21:56 +090012
Kuninori Morimoto98615452009-12-14 13:21:56 +090013#include <linux/delay.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090014#include <linux/i2c.h>
Ashish Chavanaa0e25c2012-03-29 19:06:29 +053015#include <linux/spi/spi.h>
Ashish Chavan33593b52012-03-08 19:04:56 +053016#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040018#include <linux/module.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090019#include <sound/pcm.h>
20#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020021#include <sound/soc.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090022#include <sound/initval.h>
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +090023#include <sound/tlv.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090024
Kuninori Morimoto98615452009-12-14 13:21:56 +090025/* DA7210 register space */
Ashish Chavanaa0e25c2012-03-29 19:06:29 +053026#define DA7210_PAGE_CONTROL 0x00
Ashish Chavande5eaf82011-10-19 14:24:37 +053027#define DA7210_CONTROL 0x01
Kuninori Morimoto98615452009-12-14 13:21:56 +090028#define DA7210_STATUS 0x02
29#define DA7210_STARTUP1 0x03
Ashish Chavan6950c602011-10-21 18:16:08 +053030#define DA7210_STARTUP2 0x04
31#define DA7210_STARTUP3 0x05
Kuninori Morimoto98615452009-12-14 13:21:56 +090032#define DA7210_MIC_L 0x07
33#define DA7210_MIC_R 0x08
Ashish Chavande5eaf82011-10-19 14:24:37 +053034#define DA7210_AUX1_L 0x09
35#define DA7210_AUX1_R 0x0A
Ashish Chavan5eda1942011-10-19 14:19:06 +053036#define DA7210_AUX2 0x0B
Ashish Chavande5eaf82011-10-19 14:24:37 +053037#define DA7210_IN_GAIN 0x0C
Kuninori Morimoto98615452009-12-14 13:21:56 +090038#define DA7210_INMIX_L 0x0D
39#define DA7210_INMIX_R 0x0E
40#define DA7210_ADC_HPF 0x0F
41#define DA7210_ADC 0x10
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053042#define DA7210_ADC_EQ1_2 0X11
43#define DA7210_ADC_EQ3_4 0x12
44#define DA7210_ADC_EQ5 0x13
Kuninori Morimoto98615452009-12-14 13:21:56 +090045#define DA7210_DAC_HPF 0x14
46#define DA7210_DAC_L 0x15
47#define DA7210_DAC_R 0x16
48#define DA7210_DAC_SEL 0x17
Ashish Chavan5eda1942011-10-19 14:19:06 +053049#define DA7210_SOFTMUTE 0x18
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053050#define DA7210_DAC_EQ1_2 0x19
51#define DA7210_DAC_EQ3_4 0x1A
52#define DA7210_DAC_EQ5 0x1B
Kuninori Morimoto98615452009-12-14 13:21:56 +090053#define DA7210_OUTMIX_L 0x1C
54#define DA7210_OUTMIX_R 0x1D
Ashish Chavan52082d82011-10-21 19:06:23 +053055#define DA7210_OUT1_L 0x1E
56#define DA7210_OUT1_R 0x1F
57#define DA7210_OUT2 0x20
Kuninori Morimoto98615452009-12-14 13:21:56 +090058#define DA7210_HP_L_VOL 0x21
59#define DA7210_HP_R_VOL 0x22
60#define DA7210_HP_CFG 0x23
Ashish Chavan5eda1942011-10-19 14:19:06 +053061#define DA7210_ZERO_CROSS 0x24
Kuninori Morimoto98615452009-12-14 13:21:56 +090062#define DA7210_DAI_SRC_SEL 0x25
63#define DA7210_DAI_CFG1 0x26
64#define DA7210_DAI_CFG3 0x28
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090065#define DA7210_PLL_DIV1 0x29
66#define DA7210_PLL_DIV2 0x2A
Kuninori Morimoto98615452009-12-14 13:21:56 +090067#define DA7210_PLL_DIV3 0x2B
68#define DA7210_PLL 0x2C
Ashish Chavande5eaf82011-10-19 14:24:37 +053069#define DA7210_ALC_MAX 0x83
70#define DA7210_ALC_MIN 0x84
71#define DA7210_ALC_NOIS 0x85
72#define DA7210_ALC_ATT 0x86
73#define DA7210_ALC_REL 0x87
74#define DA7210_ALC_DEL 0x88
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090075#define DA7210_A_HID_UNLOCK 0x8A
76#define DA7210_A_TEST_UNLOCK 0x8B
77#define DA7210_A_PLL1 0x90
78#define DA7210_A_CP_MODE 0xA7
Kuninori Morimoto98615452009-12-14 13:21:56 +090079
80/* STARTUP1 bit fields */
81#define DA7210_SC_MST_EN (1 << 0)
82
83/* MIC_L bit fields */
84#define DA7210_MICBIAS_EN (1 << 6)
85#define DA7210_MIC_L_EN (1 << 7)
86
87/* MIC_R bit fields */
88#define DA7210_MIC_R_EN (1 << 7)
89
90/* INMIX_L bit fields */
91#define DA7210_IN_L_EN (1 << 7)
92
93/* INMIX_R bit fields */
94#define DA7210_IN_R_EN (1 << 7)
95
Kuninori Morimoto98615452009-12-14 13:21:56 +090096/* ADC bit fields */
Ashish Chavande5eaf82011-10-19 14:24:37 +053097#define DA7210_ADC_ALC_EN (1 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +090098#define DA7210_ADC_L_EN (1 << 3)
99#define DA7210_ADC_R_EN (1 << 7)
100
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900101/* DAC/ADC HPF fields */
102#define DA7210_VOICE_F0_MASK (0x7 << 4)
103#define DA7210_VOICE_F0_25 (1 << 4)
104#define DA7210_VOICE_EN (1 << 7)
Mark Brownc2151432009-12-16 20:36:37 +0000105
Kuninori Morimoto98615452009-12-14 13:21:56 +0900106/* DAC_SEL bit fields */
107#define DA7210_DAC_L_SRC_DAI_L (4 << 0)
108#define DA7210_DAC_L_EN (1 << 3)
109#define DA7210_DAC_R_SRC_DAI_R (5 << 4)
110#define DA7210_DAC_R_EN (1 << 7)
111
112/* OUTMIX_L bit fields */
113#define DA7210_OUT_L_EN (1 << 7)
114
115/* OUTMIX_R bit fields */
116#define DA7210_OUT_R_EN (1 << 7)
117
118/* HP_CFG bit fields */
119#define DA7210_HP_2CAP_MODE (1 << 1)
120#define DA7210_HP_SENSE_EN (1 << 2)
121#define DA7210_HP_L_EN (1 << 3)
122#define DA7210_HP_MODE (1 << 6)
123#define DA7210_HP_R_EN (1 << 7)
124
125/* DAI_SRC_SEL bit fields */
126#define DA7210_DAI_OUT_L_SRC (6 << 0)
127#define DA7210_DAI_OUT_R_SRC (7 << 4)
128
129/* DAI_CFG1 bit fields */
130#define DA7210_DAI_WORD_S16_LE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530131#define DA7210_DAI_WORD_S20_3LE (1 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900132#define DA7210_DAI_WORD_S24_LE (2 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530133#define DA7210_DAI_WORD_S32_LE (3 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900134#define DA7210_DAI_FLEN_64BIT (1 << 2)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530135#define DA7210_DAI_MODE_SLAVE (0 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900136#define DA7210_DAI_MODE_MASTER (1 << 7)
137
138/* DAI_CFG3 bit fields */
139#define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530140#define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
141#define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900142#define DA7210_DAI_OE (1 << 3)
143#define DA7210_DAI_EN (1 << 7)
144
145/*PLL_DIV3 bit fields */
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530146#define DA7210_PLL_DIV_L_MASK (0xF << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900147#define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
148#define DA7210_PLL_BYP (1 << 6)
149
150/* PLL bit fields */
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900151#define DA7210_PLL_FS_MASK (0xF << 0)
152#define DA7210_PLL_FS_8000 (0x1 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900153#define DA7210_PLL_FS_11025 (0x2 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900154#define DA7210_PLL_FS_12000 (0x3 << 0)
155#define DA7210_PLL_FS_16000 (0x5 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900156#define DA7210_PLL_FS_22050 (0x6 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900157#define DA7210_PLL_FS_24000 (0x7 << 0)
158#define DA7210_PLL_FS_32000 (0x9 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900159#define DA7210_PLL_FS_44100 (0xA << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900160#define DA7210_PLL_FS_48000 (0xB << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900161#define DA7210_PLL_FS_88200 (0xE << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900162#define DA7210_PLL_FS_96000 (0xF << 0)
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530163#define DA7210_MCLK_DET_EN (0x1 << 5)
164#define DA7210_MCLK_SRM_EN (0x1 << 6)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900165#define DA7210_PLL_EN (0x1 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900166
Ashish Chavan5eda1942011-10-19 14:19:06 +0530167/* SOFTMUTE bit fields */
168#define DA7210_RAMP_EN (1 << 6)
169
Ashish Chavande5eaf82011-10-19 14:24:37 +0530170/* CONTROL bit fields */
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530171#define DA7210_REG_EN (1 << 0)
172#define DA7210_BIAS_EN (1 << 2)
Ashish Chavande5eaf82011-10-19 14:24:37 +0530173#define DA7210_NOISE_SUP_EN (1 << 3)
174
175/* IN_GAIN bit fields */
176#define DA7210_INPGA_L_VOL (0x0F << 0)
177#define DA7210_INPGA_R_VOL (0xF0 << 0)
178
179/* ZERO_CROSS bit fields */
180#define DA7210_AUX1_L_ZC (1 << 0)
181#define DA7210_AUX1_R_ZC (1 << 1)
182#define DA7210_HP_L_ZC (1 << 6)
183#define DA7210_HP_R_ZC (1 << 7)
184
185/* AUX1_L bit fields */
186#define DA7210_AUX1_L_VOL (0x3F << 0)
Ashish Chavan24b6f262012-01-02 17:35:52 +0530187#define DA7210_AUX1_L_EN (1 << 7)
Ashish Chavande5eaf82011-10-19 14:24:37 +0530188
189/* AUX1_R bit fields */
190#define DA7210_AUX1_R_VOL (0x3F << 0)
Ashish Chavan24b6f262012-01-02 17:35:52 +0530191#define DA7210_AUX1_R_EN (1 << 7)
192
193/* AUX2 bit fields */
194#define DA7210_AUX2_EN (1 << 3)
Ashish Chavande5eaf82011-10-19 14:24:37 +0530195
196/* Minimum INPGA and AUX1 volume to enable noise suppression */
197#define DA7210_INPGA_MIN_VOL_NS 0x0A /* 10.5dB */
198#define DA7210_AUX1_MIN_VOL_NS 0x35 /* 6dB */
199
Ashish Chavan52082d82011-10-21 19:06:23 +0530200/* OUT1_L bit fields */
201#define DA7210_OUT1_L_EN (1 << 7)
202
203/* OUT1_R bit fields */
204#define DA7210_OUT1_R_EN (1 << 7)
205
206/* OUT2 bit fields */
207#define DA7210_OUT2_OUTMIX_R (1 << 5)
208#define DA7210_OUT2_OUTMIX_L (1 << 6)
209#define DA7210_OUT2_EN (1 << 7)
210
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530211struct pll_div {
212 int fref;
213 int fout;
214 u8 div1;
215 u8 div2;
216 u8 div3;
217 u8 mode; /* 0 = slave, 1 = master */
218};
219
220/* PLL dividers table */
221static const struct pll_div da7210_pll_div[] = {
222 /* for MASTER mode, fs = 44.1Khz */
223 { 12000000, 2822400, 0xE8, 0x6C, 0x2, 1}, /* MCLK=12Mhz */
224 { 13000000, 2822400, 0xDF, 0x28, 0xC, 1}, /* MCLK=13Mhz */
225 { 13500000, 2822400, 0xDB, 0x0A, 0xD, 1}, /* MCLK=13.5Mhz */
226 { 14400000, 2822400, 0xD4, 0x5A, 0x2, 1}, /* MCLK=14.4Mhz */
227 { 19200000, 2822400, 0xBB, 0x43, 0x9, 1}, /* MCLK=19.2Mhz */
228 { 19680000, 2822400, 0xB9, 0x6D, 0xA, 1}, /* MCLK=19.68Mhz */
229 { 19800000, 2822400, 0xB8, 0xFB, 0xB, 1}, /* MCLK=19.8Mhz */
230 /* for MASTER mode, fs = 48Khz */
231 { 12000000, 3072000, 0xF3, 0x12, 0x7, 1}, /* MCLK=12Mhz */
232 { 13000000, 3072000, 0xE8, 0xFD, 0x5, 1}, /* MCLK=13Mhz */
233 { 13500000, 3072000, 0xE4, 0x82, 0x3, 1}, /* MCLK=13.5Mhz */
234 { 14400000, 3072000, 0xDD, 0x3A, 0x0, 1}, /* MCLK=14.4Mhz */
235 { 19200000, 3072000, 0xC1, 0xEB, 0x8, 1}, /* MCLK=19.2Mhz */
236 { 19680000, 3072000, 0xBF, 0xEC, 0x0, 1}, /* MCLK=19.68Mhz */
237 { 19800000, 3072000, 0xBF, 0x70, 0x0, 1}, /* MCLK=19.8Mhz */
238 /* for SLAVE mode with SRM */
239 { 12000000, 2822400, 0xED, 0xBF, 0x5, 0}, /* MCLK=12Mhz */
240 { 13000000, 2822400, 0xE4, 0x13, 0x0, 0}, /* MCLK=13Mhz */
241 { 13500000, 2822400, 0xDF, 0xC6, 0x8, 0}, /* MCLK=13.5Mhz */
242 { 14400000, 2822400, 0xD8, 0xCA, 0x1, 0}, /* MCLK=14.4Mhz */
243 { 19200000, 2822400, 0xBE, 0x97, 0x9, 0}, /* MCLK=19.2Mhz */
244 { 19680000, 2822400, 0xBC, 0xAC, 0xD, 0}, /* MCLK=19.68Mhz */
245 { 19800000, 2822400, 0xBC, 0x35, 0xE, 0}, /* MCLK=19.8Mhz */
246};
247
248enum clk_src {
249 DA7210_CLKSRC_MCLK
250};
251
Kuninori Morimoto98615452009-12-14 13:21:56 +0900252#define DA7210_VERSION "0.0.1"
253
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900254/*
255 * Playback Volume
256 *
257 * max : 0x3F (+15.0 dB)
258 * (1.5 dB step)
259 * min : 0x11 (-54.0 dB)
260 * mute : 0x10
261 * reserved : 0x00 - 0x0F
262 *
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900263 * Reserved area are considered as "mute".
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900264 */
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200265static const DECLARE_TLV_DB_RANGE(hp_out_tlv,
Ashish Chavan7a0e67b2011-10-14 16:25:25 +0530266 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
267 /* -54 dB to +15 dB */
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200268 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
269);
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900270
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200271static const DECLARE_TLV_DB_RANGE(lineout_vol_tlv,
Ashish Chavan52082d82011-10-21 19:06:23 +0530272 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
273 /* -54dB to 15dB */
274 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200275);
Ashish Chavan52082d82011-10-21 19:06:23 +0530276
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200277static const DECLARE_TLV_DB_RANGE(mono_vol_tlv,
Ashish Chavan52082d82011-10-21 19:06:23 +0530278 0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
279 /* -18dB to 6dB */
280 0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200281);
Ashish Chavan52082d82011-10-21 19:06:23 +0530282
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200283static const DECLARE_TLV_DB_RANGE(aux1_vol_tlv,
Ashish Chavan24b6f262012-01-02 17:35:52 +0530284 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
285 /* -48dB to 21dB */
286 0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
Lars-Peter Clausen6c1e1bf2015-08-02 17:19:37 +0200287);
Ashish Chavan24b6f262012-01-02 17:35:52 +0530288
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530289static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
290static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
Ashish Chavan52082d82011-10-21 19:06:23 +0530291static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -7725, 75, 0);
Ashish Chavan24b6f262012-01-02 17:35:52 +0530292static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
293static const DECLARE_TLV_DB_SCALE(aux2_vol_tlv, -600, 600, 0);
294static const DECLARE_TLV_DB_SCALE(inpga_gain_tlv, -450, 150, 0);
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530295
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530296/* ADC and DAC high pass filter f0 value */
Mark Brownf4034142011-11-09 23:15:26 +0000297static const char * const da7210_hpf_cutoff_txt[] = {
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530298 "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
299};
300
Takashi Iwaie34042d2014-02-18 10:06:00 +0100301static SOC_ENUM_SINGLE_DECL(da7210_dac_hpf_cutoff,
302 DA7210_DAC_HPF, 0, da7210_hpf_cutoff_txt);
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530303
Takashi Iwaie34042d2014-02-18 10:06:00 +0100304static SOC_ENUM_SINGLE_DECL(da7210_adc_hpf_cutoff,
305 DA7210_ADC_HPF, 0, da7210_hpf_cutoff_txt);
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530306
307/* ADC and DAC voice (8kHz) high pass cutoff value */
Mark Brownf4034142011-11-09 23:15:26 +0000308static const char * const da7210_vf_cutoff_txt[] = {
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530309 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
310};
311
Takashi Iwaie34042d2014-02-18 10:06:00 +0100312static SOC_ENUM_SINGLE_DECL(da7210_dac_vf_cutoff,
313 DA7210_DAC_HPF, 4, da7210_vf_cutoff_txt);
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530314
Takashi Iwaie34042d2014-02-18 10:06:00 +0100315static SOC_ENUM_SINGLE_DECL(da7210_adc_vf_cutoff,
316 DA7210_ADC_HPF, 4, da7210_vf_cutoff_txt);
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530317
Ashish Chavan5eda1942011-10-19 14:19:06 +0530318static const char *da7210_hp_mode_txt[] = {
319 "Class H", "Class G"
320};
321
Takashi Iwaie34042d2014-02-18 10:06:00 +0100322static SOC_ENUM_SINGLE_DECL(da7210_hp_mode_sel,
323 DA7210_HP_CFG, 0, da7210_hp_mode_txt);
Ashish Chavan5eda1942011-10-19 14:19:06 +0530324
Ashish Chavande5eaf82011-10-19 14:24:37 +0530325/* ALC can be enabled only if noise suppression is disabled */
326static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
327 struct snd_ctl_elem_value *ucontrol)
328{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000329 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Ashish Chavande5eaf82011-10-19 14:24:37 +0530330
331 if (ucontrol->value.integer.value[0]) {
332 /* Check if noise suppression is enabled */
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900333 if (snd_soc_component_read(component, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000334 dev_dbg(component->dev,
Ashish Chavande5eaf82011-10-19 14:24:37 +0530335 "Disable noise suppression to enable ALC\n");
336 return -EINVAL;
337 }
338 }
339 /* If all conditions are met or we are actually disabling ALC */
340 return snd_soc_put_volsw(kcontrol, ucontrol);
341}
342
343/* Noise suppression can be enabled only if following conditions are met
344 * ALC disabled
345 * ZC enabled for HP and AUX1 PGA
346 * INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
347 * AUX1_L_VOL and AUX1_R_VOL >= 6 dB
348 */
349static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
350 struct snd_ctl_elem_value *ucontrol)
351{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000352 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Ashish Chavande5eaf82011-10-19 14:24:37 +0530353 u8 val;
354
355 if (ucontrol->value.integer.value[0]) {
356 /* Check if ALC is enabled */
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900357 if (snd_soc_component_read(component, DA7210_ADC) & DA7210_ADC_ALC_EN)
Ashish Chavande5eaf82011-10-19 14:24:37 +0530358 goto err;
359
360 /* Check ZC for HP and AUX1 PGA */
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900361 if ((snd_soc_component_read(component, DA7210_ZERO_CROSS) &
Ashish Chavande5eaf82011-10-19 14:24:37 +0530362 (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
363 DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
364 DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
365 goto err;
366
367 /* Check INPGA_L_VOL and INPGA_R_VOL */
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900368 val = snd_soc_component_read(component, DA7210_IN_GAIN);
Ashish Chavande5eaf82011-10-19 14:24:37 +0530369 if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
370 (((val & DA7210_INPGA_R_VOL) >> 4) <
371 DA7210_INPGA_MIN_VOL_NS))
372 goto err;
373
374 /* Check AUX1_L_VOL and AUX1_R_VOL */
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900375 if (((snd_soc_component_read(component, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
Ashish Chavande5eaf82011-10-19 14:24:37 +0530376 DA7210_AUX1_MIN_VOL_NS) ||
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900377 ((snd_soc_component_read(component, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
Ashish Chavande5eaf82011-10-19 14:24:37 +0530378 DA7210_AUX1_MIN_VOL_NS))
379 goto err;
380 }
381 /* If all conditions are met or we are actually disabling Noise sup */
382 return snd_soc_put_volsw(kcontrol, ucontrol);
383
384err:
385 return -EINVAL;
386}
387
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900388static const struct snd_kcontrol_new da7210_snd_controls[] = {
389
390 SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
391 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
392 0, 0x3F, 0, hp_out_tlv),
Ashish Chavan52082d82011-10-21 19:06:23 +0530393 SOC_DOUBLE_R_TLV("Digital Playback Volume",
394 DA7210_DAC_L, DA7210_DAC_R,
395 0, 0x77, 1, dac_gain_tlv),
396 SOC_DOUBLE_R_TLV("Lineout Playback Volume",
397 DA7210_OUT1_L, DA7210_OUT1_R,
398 0, 0x3f, 0, lineout_vol_tlv),
399 SOC_SINGLE_TLV("Mono Playback Volume", DA7210_OUT2, 0, 0x7, 0,
400 mono_vol_tlv),
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530401
Ashish Chavan24b6f262012-01-02 17:35:52 +0530402 SOC_DOUBLE_R_TLV("Mic Capture Volume",
403 DA7210_MIC_L, DA7210_MIC_R,
404 0, 0x5, 0, mic_vol_tlv),
405 SOC_DOUBLE_R_TLV("Aux1 Capture Volume",
406 DA7210_AUX1_L, DA7210_AUX1_R,
407 0, 0x3f, 0, aux1_vol_tlv),
408 SOC_SINGLE_TLV("Aux2 Capture Volume", DA7210_AUX2, 0, 0x3, 0,
409 aux2_vol_tlv),
410 SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
411 inpga_gain_tlv),
412
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530413 /* DAC Equalizer controls */
414 SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
415 SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
416 eq_gain_tlv),
417 SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
418 eq_gain_tlv),
419 SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
420 eq_gain_tlv),
421 SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
422 eq_gain_tlv),
423 SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
424 eq_gain_tlv),
425
426 /* ADC Equalizer controls */
427 SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
428 SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
429 1, adc_eq_master_gain_tlv),
430 SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
431 eq_gain_tlv),
432 SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
433 eq_gain_tlv),
434 SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
435 eq_gain_tlv),
436 SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
437 eq_gain_tlv),
438 SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
439 eq_gain_tlv),
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530440
441 SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
442 SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
443 SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
444 SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
445
446 SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
447 SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
448 SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
449 SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
Ashish Chavan5eda1942011-10-19 14:19:06 +0530450
451 /* Mute controls */
452 SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
453 SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
454 SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
455 SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
456 SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
457
458 /* Zero cross controls */
459 SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
460 SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
461 SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
462 SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
463
464 SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
Ashish Chavande5eaf82011-10-19 14:24:37 +0530465
466 /* ALC controls */
467 SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
468 snd_soc_get_volsw, da7210_put_alc_sw),
469 SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
470 SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
471 SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
472 SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
473 SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
474 SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
475
476 SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
477 0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900478};
479
Ashish Chavan6950c602011-10-21 18:16:08 +0530480/*
481 * DAPM Controls
482 *
483 * Current DAPM implementation covers almost all codec components e.g. IOs,
484 * mixers, PGAs,ADC and DAC.
485 */
486/* In Mixer Left */
487static const struct snd_kcontrol_new da7210_dapm_inmixl_controls[] = {
488 SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_L, 0, 1, 0),
489 SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_L, 1, 1, 0),
Ashish Chavan24b6f262012-01-02 17:35:52 +0530490 SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_INMIX_L, 2, 1, 0),
491 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_L, 3, 1, 0),
492 SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_INMIX_L, 4, 1, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530493};
494
495/* In Mixer Right */
496static const struct snd_kcontrol_new da7210_dapm_inmixr_controls[] = {
497 SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_R, 0, 1, 0),
498 SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_R, 1, 1, 0),
Ashish Chavan24b6f262012-01-02 17:35:52 +0530499 SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_INMIX_R, 2, 1, 0),
500 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_R, 3, 1, 0),
501 SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_INMIX_R, 4, 1, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530502};
503
504/* Out Mixer Left */
505static const struct snd_kcontrol_new da7210_dapm_outmixl_controls[] = {
Ashish Chavan24b6f262012-01-02 17:35:52 +0530506 SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_OUTMIX_L, 0, 1, 0),
507 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_L, 1, 1, 0),
508 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_L, 2, 1, 0),
509 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_L, 3, 1, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530510 SOC_DAPM_SINGLE("DAC Left Switch", DA7210_OUTMIX_L, 4, 1, 0),
511};
512
513/* Out Mixer Right */
514static const struct snd_kcontrol_new da7210_dapm_outmixr_controls[] = {
Ashish Chavan24b6f262012-01-02 17:35:52 +0530515 SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_OUTMIX_R, 0, 1, 0),
516 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_R, 1, 1, 0),
517 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_R, 2, 1, 0),
518 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_R, 3, 1, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530519 SOC_DAPM_SINGLE("DAC Right Switch", DA7210_OUTMIX_R, 4, 1, 0),
520};
521
Ashish Chavan52082d82011-10-21 19:06:23 +0530522/* Mono Mixer */
523static const struct snd_kcontrol_new da7210_dapm_monomix_controls[] = {
Ashish Chavan24b6f262012-01-02 17:35:52 +0530524 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUT2, 3, 1, 0),
525 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUT2, 4, 1, 0),
Ashish Chavan52082d82011-10-21 19:06:23 +0530526 SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_OUT2, 5, 1, 0),
527 SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_OUT2, 6, 1, 0),
528};
529
Ashish Chavan6950c602011-10-21 18:16:08 +0530530/* DAPM widgets */
531static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
532 /* Input Side */
533 /* Input Lines */
534 SND_SOC_DAPM_INPUT("MICL"),
535 SND_SOC_DAPM_INPUT("MICR"),
Ashish Chavan24b6f262012-01-02 17:35:52 +0530536 SND_SOC_DAPM_INPUT("AUX1L"),
537 SND_SOC_DAPM_INPUT("AUX1R"),
538 SND_SOC_DAPM_INPUT("AUX2"),
Ashish Chavan6950c602011-10-21 18:16:08 +0530539
540 /* Input PGAs */
541 SND_SOC_DAPM_PGA("Mic Left", DA7210_STARTUP3, 0, 1, NULL, 0),
542 SND_SOC_DAPM_PGA("Mic Right", DA7210_STARTUP3, 1, 1, NULL, 0),
Ashish Chavan24b6f262012-01-02 17:35:52 +0530543 SND_SOC_DAPM_PGA("Aux1 Left", DA7210_STARTUP3, 2, 1, NULL, 0),
544 SND_SOC_DAPM_PGA("Aux1 Right", DA7210_STARTUP3, 3, 1, NULL, 0),
545 SND_SOC_DAPM_PGA("Aux2 Mono", DA7210_STARTUP3, 4, 1, NULL, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530546
547 SND_SOC_DAPM_PGA("INPGA Left", DA7210_INMIX_L, 7, 0, NULL, 0),
548 SND_SOC_DAPM_PGA("INPGA Right", DA7210_INMIX_R, 7, 0, NULL, 0),
549
Ashish Chavan24b6f262012-01-02 17:35:52 +0530550 /* MICBIAS */
551 SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
552
Ashish Chavan6950c602011-10-21 18:16:08 +0530553 /* Input Mixers */
554 SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
555 &da7210_dapm_inmixl_controls[0],
556 ARRAY_SIZE(da7210_dapm_inmixl_controls)),
557
558 SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
559 &da7210_dapm_inmixr_controls[0],
560 ARRAY_SIZE(da7210_dapm_inmixr_controls)),
561
562 /* ADCs */
563 SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
564 SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
565
566 /* Output Side */
567 /* DACs */
568 SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
569 SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
570
571 /* Output Mixers */
572 SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
573 &da7210_dapm_outmixl_controls[0],
574 ARRAY_SIZE(da7210_dapm_outmixl_controls)),
575
576 SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
577 &da7210_dapm_outmixr_controls[0],
578 ARRAY_SIZE(da7210_dapm_outmixr_controls)),
579
Ashish Chavan52082d82011-10-21 19:06:23 +0530580 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
581 &da7210_dapm_monomix_controls[0],
582 ARRAY_SIZE(da7210_dapm_monomix_controls)),
583
Ashish Chavan6950c602011-10-21 18:16:08 +0530584 /* Output PGAs */
585 SND_SOC_DAPM_PGA("OUTPGA Left Enable", DA7210_OUTMIX_L, 7, 0, NULL, 0),
586 SND_SOC_DAPM_PGA("OUTPGA Right Enable", DA7210_OUTMIX_R, 7, 0, NULL, 0),
587
Ashish Chavan52082d82011-10-21 19:06:23 +0530588 SND_SOC_DAPM_PGA("Out1 Left", DA7210_STARTUP2, 0, 1, NULL, 0),
589 SND_SOC_DAPM_PGA("Out1 Right", DA7210_STARTUP2, 1, 1, NULL, 0),
590 SND_SOC_DAPM_PGA("Out2 Mono", DA7210_STARTUP2, 2, 1, NULL, 0),
Ashish Chavan6950c602011-10-21 18:16:08 +0530591 SND_SOC_DAPM_PGA("Headphone Left", DA7210_STARTUP2, 3, 1, NULL, 0),
592 SND_SOC_DAPM_PGA("Headphone Right", DA7210_STARTUP2, 4, 1, NULL, 0),
593
594 /* Output Lines */
Ashish Chavan52082d82011-10-21 19:06:23 +0530595 SND_SOC_DAPM_OUTPUT("OUT1L"),
596 SND_SOC_DAPM_OUTPUT("OUT1R"),
Ashish Chavan6950c602011-10-21 18:16:08 +0530597 SND_SOC_DAPM_OUTPUT("HPL"),
598 SND_SOC_DAPM_OUTPUT("HPR"),
Ashish Chavan52082d82011-10-21 19:06:23 +0530599 SND_SOC_DAPM_OUTPUT("OUT2"),
Ashish Chavan6950c602011-10-21 18:16:08 +0530600};
601
602/* DAPM audio route definition */
603static const struct snd_soc_dapm_route da7210_audio_map[] = {
604 /* Dest Connecting Widget source */
605 /* Input path */
606 {"Mic Left", NULL, "MICL"},
607 {"Mic Right", NULL, "MICR"},
Ashish Chavan24b6f262012-01-02 17:35:52 +0530608 {"Aux1 Left", NULL, "AUX1L"},
609 {"Aux1 Right", NULL, "AUX1R"},
610 {"Aux2 Mono", NULL, "AUX2"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530611
612 {"In Mixer Left", "Mic Left Switch", "Mic Left"},
613 {"In Mixer Left", "Mic Right Switch", "Mic Right"},
Ashish Chavan24b6f262012-01-02 17:35:52 +0530614 {"In Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
615 {"In Mixer Left", "Aux2 Switch", "Aux2 Mono"},
616 {"In Mixer Left", "Outmix Left Switch", "Out Mixer Left"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530617
618 {"In Mixer Right", "Mic Right Switch", "Mic Right"},
619 {"In Mixer Right", "Mic Left Switch", "Mic Left"},
Ashish Chavan24b6f262012-01-02 17:35:52 +0530620 {"In Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
621 {"In Mixer Right", "Aux2 Switch", "Aux2 Mono"},
622 {"In Mixer Right", "Outmix Right Switch", "Out Mixer Right"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530623
624 {"INPGA Left", NULL, "In Mixer Left"},
625 {"ADC Left", NULL, "INPGA Left"},
626
627 {"INPGA Right", NULL, "In Mixer Right"},
628 {"ADC Right", NULL, "INPGA Right"},
629
630 /* Output path */
Ashish Chavan24b6f262012-01-02 17:35:52 +0530631 {"Out Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
632 {"Out Mixer Left", "Aux2 Switch", "Aux2 Mono"},
633 {"Out Mixer Left", "INPGA Left Switch", "INPGA Left"},
634 {"Out Mixer Left", "INPGA Right Switch", "INPGA Right"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530635 {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
Ashish Chavan24b6f262012-01-02 17:35:52 +0530636
637 {"Out Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
638 {"Out Mixer Right", "Aux2 Switch", "Aux2 Mono"},
639 {"Out Mixer Right", "INPGA Right Switch", "INPGA Right"},
640 {"Out Mixer Right", "INPGA Left Switch", "INPGA Left"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530641 {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
642
Ashish Chavan24b6f262012-01-02 17:35:52 +0530643 {"Mono Mixer", "INPGA Right Switch", "INPGA Right"},
644 {"Mono Mixer", "INPGA Left Switch", "INPGA Left"},
Ashish Chavan52082d82011-10-21 19:06:23 +0530645 {"Mono Mixer", "Outmix Right Switch", "Out Mixer Right"},
646 {"Mono Mixer", "Outmix Left Switch", "Out Mixer Left"},
647
Ashish Chavan6950c602011-10-21 18:16:08 +0530648 {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
649 {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
650
Ashish Chavan52082d82011-10-21 19:06:23 +0530651 {"Out1 Left", NULL, "OUTPGA Left Enable"},
652 {"OUT1L", NULL, "Out1 Left"},
653
654 {"Out1 Right", NULL, "OUTPGA Right Enable"},
655 {"OUT1R", NULL, "Out1 Right"},
656
Ashish Chavan6950c602011-10-21 18:16:08 +0530657 {"Headphone Left", NULL, "OUTPGA Left Enable"},
658 {"HPL", NULL, "Headphone Left"},
659
660 {"Headphone Right", NULL, "OUTPGA Right Enable"},
661 {"HPR", NULL, "Headphone Right"},
Ashish Chavan52082d82011-10-21 19:06:23 +0530662
663 {"Out2 Mono", NULL, "Mono Mixer"},
664 {"OUT2", NULL, "Out2 Mono"},
Ashish Chavan6950c602011-10-21 18:16:08 +0530665};
666
Kuninori Morimoto98615452009-12-14 13:21:56 +0900667/* Codec private data */
668struct da7210_priv {
Ashish Chavan33593b52012-03-08 19:04:56 +0530669 struct regmap *regmap;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530670 unsigned int mclk_rate;
671 int master;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900672};
673
Axel Linc418a842015-07-05 17:48:29 +0800674static const struct reg_default da7210_reg_defaults[] = {
Ashish Chavanaa0e25c2012-03-29 19:06:29 +0530675 { 0x00, 0x00 },
Ashish Chavan33593b52012-03-08 19:04:56 +0530676 { 0x01, 0x11 },
Ashish Chavan33593b52012-03-08 19:04:56 +0530677 { 0x03, 0x00 },
678 { 0x04, 0x00 },
679 { 0x05, 0x00 },
680 { 0x06, 0x00 },
681 { 0x07, 0x00 },
682 { 0x08, 0x00 },
683 { 0x09, 0x00 },
684 { 0x0a, 0x00 },
685 { 0x0b, 0x00 },
686 { 0x0c, 0x00 },
687 { 0x0d, 0x00 },
688 { 0x0e, 0x00 },
689 { 0x0f, 0x08 },
690 { 0x10, 0x00 },
691 { 0x11, 0x00 },
692 { 0x12, 0x00 },
693 { 0x13, 0x00 },
694 { 0x14, 0x08 },
695 { 0x15, 0x10 },
696 { 0x16, 0x10 },
697 { 0x17, 0x54 },
698 { 0x18, 0x40 },
699 { 0x19, 0x00 },
700 { 0x1a, 0x00 },
701 { 0x1b, 0x00 },
702 { 0x1c, 0x00 },
703 { 0x1d, 0x00 },
704 { 0x1e, 0x00 },
705 { 0x1f, 0x00 },
706 { 0x20, 0x00 },
707 { 0x21, 0x00 },
708 { 0x22, 0x00 },
709 { 0x23, 0x02 },
710 { 0x24, 0x00 },
711 { 0x25, 0x76 },
712 { 0x26, 0x00 },
713 { 0x27, 0x00 },
714 { 0x28, 0x04 },
715 { 0x29, 0x00 },
716 { 0x2a, 0x00 },
717 { 0x2b, 0x30 },
718 { 0x2c, 0x2A },
Ashish Chavan33593b52012-03-08 19:04:56 +0530719 { 0x83, 0x00 },
720 { 0x84, 0x00 },
721 { 0x85, 0x00 },
722 { 0x86, 0x00 },
723 { 0x87, 0x00 },
724 { 0x88, 0x00 },
Kuninori Morimoto98615452009-12-14 13:21:56 +0900725};
726
Ashish Chavan33593b52012-03-08 19:04:56 +0530727static bool da7210_readable_register(struct device *dev, unsigned int reg)
728{
729 switch (reg) {
730 case DA7210_A_HID_UNLOCK:
731 case DA7210_A_TEST_UNLOCK:
732 case DA7210_A_PLL1:
733 case DA7210_A_CP_MODE:
734 return false;
735 default:
736 return true;
737 }
738}
739
740static bool da7210_volatile_register(struct device *dev,
Axel Lin40a49712011-10-12 07:16:25 +0800741 unsigned int reg)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900742{
Axel Lin40a49712011-10-12 07:16:25 +0800743 switch (reg) {
744 case DA7210_STATUS:
Ashish Chavan33593b52012-03-08 19:04:56 +0530745 return true;
Axel Lin40a49712011-10-12 07:16:25 +0800746 default:
Ashish Chavan33593b52012-03-08 19:04:56 +0530747 return false;
Axel Lin40a49712011-10-12 07:16:25 +0800748 }
Kuninori Morimoto98615452009-12-14 13:21:56 +0900749}
Kuninori Morimoto98615452009-12-14 13:21:56 +0900750
751/*
752 * Set PCM DAI word length.
753 */
754static int da7210_hw_params(struct snd_pcm_substream *substream,
755 struct snd_pcm_hw_params *params,
756 struct snd_soc_dai *dai)
757{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000758 struct snd_soc_component *component = dai->component;
759 struct da7210_priv *da7210 = snd_soc_component_get_drvdata(component);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900760 u32 dai_cfg1;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530761 u32 fs, sysclk;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900762
763 /* set DAI source to Left and Right ADC */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000764 snd_soc_component_write(component, DA7210_DAI_SRC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900765 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
766
767 /* Enable DAI */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000768 snd_soc_component_write(component, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900769
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900770 dai_cfg1 = 0xFC & snd_soc_component_read(component, DA7210_DAI_CFG1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900771
Mark Brown0194c422014-01-08 19:49:37 +0000772 switch (params_width(params)) {
773 case 16:
Kuninori Morimoto98615452009-12-14 13:21:56 +0900774 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
775 break;
Mark Brown0194c422014-01-08 19:49:37 +0000776 case 20:
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530777 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
778 break;
Mark Brown0194c422014-01-08 19:49:37 +0000779 case 24:
Kuninori Morimoto98615452009-12-14 13:21:56 +0900780 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
781 break;
Mark Brown0194c422014-01-08 19:49:37 +0000782 case 32:
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530783 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
784 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900785 default:
786 return -EINVAL;
787 }
788
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000789 snd_soc_component_write(component, DA7210_DAI_CFG1, dai_cfg1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900790
Kuninori Morimoto98615452009-12-14 13:21:56 +0900791 switch (params_rate(params)) {
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900792 case 8000:
793 fs = DA7210_PLL_FS_8000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530794 sysclk = 3072000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900795 break;
796 case 11025:
797 fs = DA7210_PLL_FS_11025;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530798 sysclk = 2822400;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900799 break;
800 case 12000:
801 fs = DA7210_PLL_FS_12000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530802 sysclk = 3072000;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900803 break;
804 case 16000:
805 fs = DA7210_PLL_FS_16000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530806 sysclk = 3072000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900807 break;
808 case 22050:
809 fs = DA7210_PLL_FS_22050;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530810 sysclk = 2822400;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900811 break;
812 case 32000:
813 fs = DA7210_PLL_FS_32000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530814 sysclk = 3072000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900815 break;
816 case 44100:
817 fs = DA7210_PLL_FS_44100;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530818 sysclk = 2822400;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900819 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900820 case 48000:
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900821 fs = DA7210_PLL_FS_48000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530822 sysclk = 3072000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900823 break;
824 case 88200:
825 fs = DA7210_PLL_FS_88200;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530826 sysclk = 2822400;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900827 break;
828 case 96000:
829 fs = DA7210_PLL_FS_96000;
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530830 sysclk = 3072000;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900831 break;
832 default:
833 return -EINVAL;
834 }
835
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900836 /* Disable active mode */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000837 snd_soc_component_update_bits(component, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900838
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000839 snd_soc_component_update_bits(component, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900840
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530841 if (da7210->mclk_rate && (da7210->mclk_rate != sysclk)) {
842 /* PLL mode, disable PLL bypass */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000843 snd_soc_component_update_bits(component, DA7210_PLL_DIV3, DA7210_PLL_BYP, 0);
Ashish Chavan3cb81652012-05-03 19:27:52 +0530844
845 if (!da7210->master) {
846 /* PLL slave mode, also enable SRM */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000847 snd_soc_component_update_bits(component, DA7210_PLL,
Ashish Chavan3cb81652012-05-03 19:27:52 +0530848 (DA7210_MCLK_SRM_EN |
849 DA7210_MCLK_DET_EN),
850 (DA7210_MCLK_SRM_EN |
851 DA7210_MCLK_DET_EN));
852 }
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530853 } else {
Ashish Chavan3cb81652012-05-03 19:27:52 +0530854 /* PLL bypass mode, enable PLL bypass and Auto Detection */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000855 snd_soc_component_update_bits(component, DA7210_PLL, DA7210_MCLK_DET_EN,
Ashish Chavan3cb81652012-05-03 19:27:52 +0530856 DA7210_MCLK_DET_EN);
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000857 snd_soc_component_update_bits(component, DA7210_PLL_DIV3, DA7210_PLL_BYP,
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530858 DA7210_PLL_BYP);
859 }
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900860 /* Enable active mode */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000861 snd_soc_component_update_bits(component, DA7210_STARTUP1,
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900862 DA7210_SC_MST_EN, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900863
864 return 0;
865}
866
867/*
868 * Set DAI mode and Format
869 */
870static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
871{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000872 struct snd_soc_component *component = codec_dai->component;
873 struct da7210_priv *da7210 = snd_soc_component_get_drvdata(component);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900874 u32 dai_cfg1;
875 u32 dai_cfg3;
876
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900877 dai_cfg1 = 0x7f & snd_soc_component_read(component, DA7210_DAI_CFG1);
878 dai_cfg3 = 0xfc & snd_soc_component_read(component, DA7210_DAI_CFG3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900879
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900880 if ((snd_soc_component_read(component, DA7210_PLL) & DA7210_PLL_EN) &&
881 (!(snd_soc_component_read(component, DA7210_PLL_DIV3) & DA7210_PLL_BYP)))
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530882 return -EINVAL;
883
Kuninori Morimoto98615452009-12-14 13:21:56 +0900884 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
885 case SND_SOC_DAIFMT_CBM_CFM:
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530886 da7210->master = 1;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900887 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
888 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530889 case SND_SOC_DAIFMT_CBS_CFS:
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530890 da7210->master = 0;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530891 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
892 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900893 default:
894 return -EINVAL;
895 }
896
897 /* FIXME
898 *
899 * It support I2S only now
900 */
901 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
902 case SND_SOC_DAIFMT_I2S:
903 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
904 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530905 case SND_SOC_DAIFMT_LEFT_J:
906 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
907 break;
908 case SND_SOC_DAIFMT_RIGHT_J:
909 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
910 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900911 default:
912 return -EINVAL;
913 }
914
915 /* FIXME
916 *
917 * It support 64bit data transmission only now
918 */
919 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
920
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000921 snd_soc_component_write(component, DA7210_DAI_CFG1, dai_cfg1);
922 snd_soc_component_write(component, DA7210_DAI_CFG3, dai_cfg3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900923
924 return 0;
925}
926
Kuninori Morimotof39c0542020-07-09 10:57:02 +0900927static int da7210_mute(struct snd_soc_dai *dai, int mute, int direction)
Ashish Chavan5eda1942011-10-19 14:19:06 +0530928{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000929 struct snd_soc_component *component = dai->component;
Kuninori Morimoto2925b582020-06-16 14:21:42 +0900930 u8 mute_reg = snd_soc_component_read(component, DA7210_DAC_HPF) & 0xFB;
Ashish Chavan5eda1942011-10-19 14:19:06 +0530931
932 if (mute)
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000933 snd_soc_component_write(component, DA7210_DAC_HPF, mute_reg | 0x4);
Ashish Chavan5eda1942011-10-19 14:19:06 +0530934 else
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000935 snd_soc_component_write(component, DA7210_DAC_HPF, mute_reg);
Ashish Chavan5eda1942011-10-19 14:19:06 +0530936 return 0;
937}
938
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530939#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
940 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900941
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530942static int da7210_set_dai_sysclk(struct snd_soc_dai *codec_dai,
943 int clk_id, unsigned int freq, int dir)
944{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000945 struct snd_soc_component *component = codec_dai->component;
946 struct da7210_priv *da7210 = snd_soc_component_get_drvdata(component);
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530947
948 switch (clk_id) {
949 case DA7210_CLKSRC_MCLK:
950 switch (freq) {
951 case 12000000:
952 case 13000000:
953 case 13500000:
954 case 14400000:
955 case 19200000:
956 case 19680000:
957 case 19800000:
958 da7210->mclk_rate = freq;
959 return 0;
960 default:
961 dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
962 freq);
963 return -EINVAL;
964 }
965 break;
966 default:
967 dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
968 return -EINVAL;
969 }
970}
971
972/**
973 * da7210_set_dai_pll :Configure the codec PLL
Pierre-Louis Bossartd3d05022020-07-07 14:06:05 -0500974 * @codec_dai: pointer to codec DAI
975 * @pll_id: da7210 has only one pll, so pll_id is always zero
976 * @source: clock source
977 * @fref: MCLK frequency, should be < 20MHz
978 * @fout: FsDM value, Refer page 44 & 45 of datasheet
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530979 *
980 * Note: Supported PLL input frequencies are 12MHz, 13MHz, 13.5MHz, 14.4MHz,
981 * 19.2MHz, 19.6MHz and 19.8MHz
Pierre-Louis Bossartd3d05022020-07-07 14:06:05 -0500982 *
983 * Return: Zero for success, negative error code for error
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530984 */
985static int da7210_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
986 int source, unsigned int fref, unsigned int fout)
987{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +0000988 struct snd_soc_component *component = codec_dai->component;
989 struct da7210_priv *da7210 = snd_soc_component_get_drvdata(component);
Ashish Chavan570aa7b2012-04-17 18:04:06 +0530990
991 u8 pll_div1, pll_div2, pll_div3, cnt;
992
993 /* In slave mode, there is only one set of divisors */
994 if (!da7210->master)
995 fout = 2822400;
996
997 /* Search pll div array for correct divisors */
998 for (cnt = 0; cnt < ARRAY_SIZE(da7210_pll_div); cnt++) {
Ashish Chavanc4b14e72012-04-17 21:07:44 +0530999 /* check fref, mode and fout */
1000 if ((fref == da7210_pll_div[cnt].fref) &&
1001 (da7210->master == da7210_pll_div[cnt].mode) &&
1002 (fout == da7210_pll_div[cnt].fout)) {
1003 /* all match, pick up divisors */
1004 pll_div1 = da7210_pll_div[cnt].div1;
1005 pll_div2 = da7210_pll_div[cnt].div2;
1006 pll_div3 = da7210_pll_div[cnt].div3;
1007 break;
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301008 }
1009 }
1010 if (cnt >= ARRAY_SIZE(da7210_pll_div))
1011 goto err;
1012
1013 /* Disable active mode */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001014 snd_soc_component_update_bits(component, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301015 /* Write PLL dividers */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001016 snd_soc_component_write(component, DA7210_PLL_DIV1, pll_div1);
1017 snd_soc_component_write(component, DA7210_PLL_DIV2, pll_div2);
1018 snd_soc_component_update_bits(component, DA7210_PLL_DIV3,
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301019 DA7210_PLL_DIV_L_MASK, pll_div3);
1020
Ashish Chavan3cb81652012-05-03 19:27:52 +05301021 /* Enable PLL */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001022 snd_soc_component_update_bits(component, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
Ashish Chavan3cb81652012-05-03 19:27:52 +05301023
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301024 /* Enable active mode */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001025 snd_soc_component_update_bits(component, DA7210_STARTUP1, DA7210_SC_MST_EN,
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301026 DA7210_SC_MST_EN);
1027 return 0;
1028err:
1029 dev_err(codec_dai->dev, "Unsupported PLL input frequency %d\n", fref);
1030 return -EINVAL;
1031}
1032
Kuninori Morimoto98615452009-12-14 13:21:56 +09001033/* DAI operations */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001034static const struct snd_soc_dai_ops da7210_dai_ops = {
Kuninori Morimoto98615452009-12-14 13:21:56 +09001035 .hw_params = da7210_hw_params,
1036 .set_fmt = da7210_set_dai_fmt,
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301037 .set_sysclk = da7210_set_dai_sysclk,
1038 .set_pll = da7210_set_dai_pll,
Kuninori Morimotof39c0542020-07-09 10:57:02 +09001039 .mute_stream = da7210_mute,
1040 .no_capture_mute = 1,
Kuninori Morimoto98615452009-12-14 13:21:56 +09001041};
1042
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043static struct snd_soc_dai_driver da7210_dai = {
1044 .name = "da7210-hifi",
Kuninori Morimoto98615452009-12-14 13:21:56 +09001045 /* playback capabilities */
1046 .playback = {
1047 .stream_name = "Playback",
1048 .channels_min = 1,
1049 .channels_max = 2,
1050 .rates = SNDRV_PCM_RATE_8000_96000,
1051 .formats = DA7210_FORMATS,
1052 },
1053 /* capture capabilities */
1054 .capture = {
1055 .stream_name = "Capture",
1056 .channels_min = 1,
1057 .channels_max = 2,
1058 .rates = SNDRV_PCM_RATE_8000_96000,
1059 .formats = DA7210_FORMATS,
1060 },
1061 .ops = &da7210_dai_ops,
Kuninori Morimoto1c6d1c42021-01-15 13:54:33 +09001062 .symmetric_rate = 1,
Kuninori Morimoto98615452009-12-14 13:21:56 +09001063};
Kuninori Morimoto98615452009-12-14 13:21:56 +09001064
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001065static int da7210_probe(struct snd_soc_component *component)
Kuninori Morimoto98615452009-12-14 13:21:56 +09001066{
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001067 struct da7210_priv *da7210 = snd_soc_component_get_drvdata(component);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001068
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001069 dev_info(component->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001070
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301071 da7210->mclk_rate = 0; /* This will be set from set_sysclk() */
1072 da7210->master = 0; /* This will be set from set_fmt() */
1073
1074 /* Enable internal regulator & bias current */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001075 snd_soc_component_write(component, DA7210_CONTROL, DA7210_REG_EN | DA7210_BIAS_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001076
1077 /*
1078 * ADC settings
1079 */
1080
1081 /* Enable Left & Right MIC PGA and Mic Bias */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001082 snd_soc_component_write(component, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
1083 snd_soc_component_write(component, DA7210_MIC_R, DA7210_MIC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001084
1085 /* Enable Left and Right input PGA */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001086 snd_soc_component_write(component, DA7210_INMIX_L, DA7210_IN_L_EN);
1087 snd_soc_component_write(component, DA7210_INMIX_R, DA7210_IN_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001088
1089 /* Enable Left and Right ADC */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001090 snd_soc_component_write(component, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001091
1092 /*
1093 * DAC settings
1094 */
1095
1096 /* Enable Left and Right DAC */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001097 snd_soc_component_write(component, DA7210_DAC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +09001098 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
1099 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
1100
1101 /* Enable Left and Right out PGA */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001102 snd_soc_component_write(component, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
1103 snd_soc_component_write(component, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001104
1105 /* Enable Left and Right HeadPhone PGA */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001106 snd_soc_component_write(component, DA7210_HP_CFG,
Kuninori Morimoto98615452009-12-14 13:21:56 +09001107 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
1108 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
1109
Ashish Chavan5eda1942011-10-19 14:19:06 +05301110 /* Enable ramp mode for DAC gain update */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001111 snd_soc_component_write(component, DA7210_SOFTMUTE, DA7210_RAMP_EN);
Ashish Chavan5eda1942011-10-19 14:19:06 +05301112
Ashish Chavan52082d82011-10-21 19:06:23 +05301113 /*
1114 * For DA7210 codec, there are two ways to enable/disable analog IOs
1115 * and ADC/DAC,
1116 * (1) Using "Enable Bit" of register associated with that IO
1117 * (or ADC/DAC)
1118 * e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
1119 *
1120 * (2) Using "Standby Bit" of STARTUP2 or STARTUP3 register
1121 * e.g. Mic left can be put to STANDBY using bit 0 of STARTUP3(0x5)
1122 *
1123 * Out of these two methods, the one using STANDBY bits is preferred
1124 * way to enable/disable individual blocks. This is because STANDBY
1125 * registers are part of system controller which allows system power
1126 * up/down in a controlled, pop-free manner. Also, as per application
1127 * note of DA7210, STANDBY register bits are only effective if a
1128 * particular IO (or ADC/DAC) is already enabled using enable/disable
1129 * register bits. Keeping these things in mind, current DAPM
1130 * implementation manipulates only STANDBY bits.
1131 *
1132 * Overall implementation can be outlined as below,
1133 *
1134 * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
1135 * - "STANDBY bit" is controlled by DAPM
1136 */
1137
1138 /* Enable Line out amplifiers */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001139 snd_soc_component_write(component, DA7210_OUT1_L, DA7210_OUT1_L_EN);
1140 snd_soc_component_write(component, DA7210_OUT1_R, DA7210_OUT1_R_EN);
1141 snd_soc_component_write(component, DA7210_OUT2, DA7210_OUT2_EN |
Ashish Chavan52082d82011-10-21 19:06:23 +05301142 DA7210_OUT2_OUTMIX_L | DA7210_OUT2_OUTMIX_R);
1143
Ashish Chavan24b6f262012-01-02 17:35:52 +05301144 /* Enable Aux1 */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001145 snd_soc_component_write(component, DA7210_AUX1_L, DA7210_AUX1_L_EN);
1146 snd_soc_component_write(component, DA7210_AUX1_R, DA7210_AUX1_R_EN);
Ashish Chavan24b6f262012-01-02 17:35:52 +05301147 /* Enable Aux2 */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001148 snd_soc_component_write(component, DA7210_AUX2, DA7210_AUX2_EN);
Ashish Chavan24b6f262012-01-02 17:35:52 +05301149
Ashish Chavan604bb222012-04-27 16:44:04 +05301150 /* Set PLL Master clock range 10-20 MHz, enable PLL bypass */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001151 snd_soc_component_write(component, DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ |
Ashish Chavan604bb222012-04-27 16:44:04 +05301152 DA7210_PLL_BYP);
Ashish Chavan570aa7b2012-04-17 18:04:06 +05301153
Kuninori Morimoto98615452009-12-14 13:21:56 +09001154 /* Diable PLL and bypass it */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001155 snd_soc_component_write(component, DA7210_PLL, DA7210_PLL_FS_48000);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001156
Kuninori Morimoto98615452009-12-14 13:21:56 +09001157 /* Activate all enabled subsystem */
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001158 snd_soc_component_write(component, DA7210_STARTUP1, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001159
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001160 dev_info(component->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 return 0;
Kuninori Morimoto98615452009-12-14 13:21:56 +09001163}
1164
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001165static const struct snd_soc_component_driver soc_component_dev_da7210 = {
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +09001166 .probe = da7210_probe,
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001167 .controls = da7210_snd_controls,
1168 .num_controls = ARRAY_SIZE(da7210_snd_controls),
1169 .dapm_widgets = da7210_dapm_widgets,
1170 .num_dapm_widgets = ARRAY_SIZE(da7210_dapm_widgets),
1171 .dapm_routes = da7210_audio_map,
1172 .num_dapm_routes = ARRAY_SIZE(da7210_audio_map),
1173 .idle_bias_on = 1,
1174 .use_pmdown_time = 1,
1175 .endianness = 1,
1176 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001177};
1178
Fabio Estevam25c1a632013-11-21 12:38:40 -02001179#if IS_ENABLED(CONFIG_I2C)
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301180
Mark Brown41a5fefe2015-07-17 19:32:04 +01001181static const struct reg_sequence da7210_regmap_i2c_patch[] = {
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301182
1183 /* System controller master disable */
1184 { DA7210_STARTUP1, 0x00 },
Ashish Chavan604bb222012-04-27 16:44:04 +05301185 /* Set PLL Master clock range 10-20 MHz */
1186 { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ },
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301187
1188 /* to unlock */
1189 { DA7210_A_HID_UNLOCK, 0x8B},
1190 { DA7210_A_TEST_UNLOCK, 0xB4},
1191 { DA7210_A_PLL1, 0x01},
1192 { DA7210_A_CP_MODE, 0x7C},
1193 /* to re-lock */
1194 { DA7210_A_HID_UNLOCK, 0x00},
1195 { DA7210_A_TEST_UNLOCK, 0x00},
1196};
1197
1198static const struct regmap_config da7210_regmap_config_i2c = {
Ashish Chavan33593b52012-03-08 19:04:56 +05301199 .reg_bits = 8,
1200 .val_bits = 8,
1201
1202 .reg_defaults = da7210_reg_defaults,
1203 .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
1204 .volatile_reg = da7210_volatile_register,
1205 .readable_reg = da7210_readable_register,
1206 .cache_type = REGCACHE_RBTREE,
1207};
1208
Bill Pemberton7a79e942012-12-07 09:26:37 -05001209static int da7210_i2c_probe(struct i2c_client *i2c,
1210 const struct i2c_device_id *id)
Kuninori Morimoto98615452009-12-14 13:21:56 +09001211{
1212 struct da7210_priv *da7210;
Kuninori Morimoto98615452009-12-14 13:21:56 +09001213 int ret;
1214
Axel Lin306bf6b2011-12-26 20:54:04 +08001215 da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
1216 GFP_KERNEL);
Kuninori Morimoto98615452009-12-14 13:21:56 +09001217 if (!da7210)
1218 return -ENOMEM;
1219
Kuninori Morimoto98615452009-12-14 13:21:56 +09001220 i2c_set_clientdata(i2c, da7210);
Ashish Chavan33593b52012-03-08 19:04:56 +05301221
Sachin Kamat4e759552012-11-26 17:19:39 +05301222 da7210->regmap = devm_regmap_init_i2c(i2c, &da7210_regmap_config_i2c);
Ashish Chavan33593b52012-03-08 19:04:56 +05301223 if (IS_ERR(da7210->regmap)) {
1224 ret = PTR_ERR(da7210->regmap);
1225 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
1226 return ret;
1227 }
Kuninori Morimoto98615452009-12-14 13:21:56 +09001228
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301229 ret = regmap_register_patch(da7210->regmap, da7210_regmap_i2c_patch,
1230 ARRAY_SIZE(da7210_regmap_i2c_patch));
1231 if (ret != 0)
1232 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1233
weiyongjun (A)c0153b12018-03-16 07:32:14 +00001234 ret = devm_snd_soc_register_component(&i2c->dev,
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001235 &soc_component_dev_da7210, &da7210_dai, 1);
Sachin Kamat4e759552012-11-26 17:19:39 +05301236 if (ret < 0)
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001237 dev_err(&i2c->dev, "Failed to register component: %d\n", ret);
Ashish Chavan33593b52012-03-08 19:04:56 +05301238
Kuninori Morimoto98615452009-12-14 13:21:56 +09001239 return ret;
1240}
1241
Kuninori Morimoto98615452009-12-14 13:21:56 +09001242static const struct i2c_device_id da7210_i2c_id[] = {
1243 { "da7210", 0 },
1244 { }
1245};
1246MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
1247
1248/* I2C codec control layer */
1249static struct i2c_driver da7210_i2c_driver = {
1250 .driver = {
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301251 .name = "da7210",
Kuninori Morimoto98615452009-12-14 13:21:56 +09001252 },
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +09001253 .probe = da7210_i2c_probe,
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +09001254 .id_table = da7210_i2c_id,
Kuninori Morimoto98615452009-12-14 13:21:56 +09001255};
1256#endif
1257
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301258#if defined(CONFIG_SPI_MASTER)
1259
Mark Brown41a5fefe2015-07-17 19:32:04 +01001260static const struct reg_sequence da7210_regmap_spi_patch[] = {
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301261 /* Dummy read to give two pulses over nCS for SPI */
1262 { DA7210_AUX2, 0x00 },
1263 { DA7210_AUX2, 0x00 },
1264
1265 /* System controller master disable */
1266 { DA7210_STARTUP1, 0x00 },
Ashish Chavan604bb222012-04-27 16:44:04 +05301267 /* Set PLL Master clock range 10-20 MHz */
1268 { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ },
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301269
1270 /* to set PAGE1 of SPI register space */
1271 { DA7210_PAGE_CONTROL, 0x80 },
1272 /* to unlock */
1273 { DA7210_A_HID_UNLOCK, 0x8B},
1274 { DA7210_A_TEST_UNLOCK, 0xB4},
1275 { DA7210_A_PLL1, 0x01},
1276 { DA7210_A_CP_MODE, 0x7C},
1277 /* to re-lock */
1278 { DA7210_A_HID_UNLOCK, 0x00},
1279 { DA7210_A_TEST_UNLOCK, 0x00},
1280 /* to set back PAGE0 of SPI register space */
1281 { DA7210_PAGE_CONTROL, 0x00 },
1282};
1283
1284static const struct regmap_config da7210_regmap_config_spi = {
1285 .reg_bits = 8,
1286 .val_bits = 8,
1287 .read_flag_mask = 0x01,
1288 .write_flag_mask = 0x00,
1289
1290 .reg_defaults = da7210_reg_defaults,
1291 .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
1292 .volatile_reg = da7210_volatile_register,
1293 .readable_reg = da7210_readable_register,
1294 .cache_type = REGCACHE_RBTREE,
1295};
1296
Bill Pemberton7a79e942012-12-07 09:26:37 -05001297static int da7210_spi_probe(struct spi_device *spi)
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301298{
1299 struct da7210_priv *da7210;
1300 int ret;
1301
1302 da7210 = devm_kzalloc(&spi->dev, sizeof(struct da7210_priv),
1303 GFP_KERNEL);
1304 if (!da7210)
1305 return -ENOMEM;
1306
1307 spi_set_drvdata(spi, da7210);
1308 da7210->regmap = devm_regmap_init_spi(spi, &da7210_regmap_config_spi);
1309 if (IS_ERR(da7210->regmap)) {
1310 ret = PTR_ERR(da7210->regmap);
1311 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
1312 return ret;
1313 }
1314
1315 ret = regmap_register_patch(da7210->regmap, da7210_regmap_spi_patch,
1316 ARRAY_SIZE(da7210_regmap_spi_patch));
1317 if (ret != 0)
1318 dev_warn(&spi->dev, "Failed to apply regmap patch: %d\n", ret);
1319
Kuninori Morimotod06f33a2018-01-29 04:01:06 +00001320 ret = devm_snd_soc_register_component(&spi->dev,
1321 &soc_component_dev_da7210, &da7210_dai, 1);
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301322
1323 return ret;
1324}
1325
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301326static struct spi_driver da7210_spi_driver = {
1327 .driver = {
1328 .name = "da7210",
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301329 },
1330 .probe = da7210_spi_probe,
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301331};
1332#endif
1333
Kuninori Morimoto98615452009-12-14 13:21:56 +09001334static int __init da7210_modinit(void)
1335{
1336 int ret = 0;
Fabio Estevam25c1a632013-11-21 12:38:40 -02001337#if IS_ENABLED(CONFIG_I2C)
Kuninori Morimoto98615452009-12-14 13:21:56 +09001338 ret = i2c_add_driver(&da7210_i2c_driver);
1339#endif
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301340#if defined(CONFIG_SPI_MASTER)
1341 ret = spi_register_driver(&da7210_spi_driver);
1342 if (ret) {
1343 printk(KERN_ERR "Failed to register da7210 SPI driver: %d\n",
1344 ret);
1345 }
1346#endif
Kuninori Morimoto98615452009-12-14 13:21:56 +09001347 return ret;
1348}
1349module_init(da7210_modinit);
1350
1351static void __exit da7210_exit(void)
1352{
Fabio Estevam25c1a632013-11-21 12:38:40 -02001353#if IS_ENABLED(CONFIG_I2C)
Kuninori Morimoto98615452009-12-14 13:21:56 +09001354 i2c_del_driver(&da7210_i2c_driver);
1355#endif
Ashish Chavanaa0e25c2012-03-29 19:06:29 +05301356#if defined(CONFIG_SPI_MASTER)
1357 spi_unregister_driver(&da7210_spi_driver);
1358#endif
Kuninori Morimoto98615452009-12-14 13:21:56 +09001359}
1360module_exit(da7210_exit);
1361
1362MODULE_DESCRIPTION("ASoC DA7210 driver");
1363MODULE_AUTHOR("David Chen, Kuninori Morimoto");
1364MODULE_LICENSE("GPL");