blob: ab790d5fe53dfff8596136e925adbe653cbe837f [file] [log] [blame]
Lars-Peter Clausencc526882011-06-27 17:04:01 +02001/*
2 * ADAV80X Audio Codec driver supporting ADAV801, ADAV803
3 *
4 * Copyright 2011 Analog Devices Inc.
5 * Author: Yi Li <yi.li@analog.com>
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/i2c.h>
15#include <linux/spi/spi.h>
16#include <linux/slab.h>
17#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/tlv.h>
21#include <sound/soc.h>
22
23#include "adav80x.h"
24
25#define ADAV80X_PLAYBACK_CTRL 0x04
26#define ADAV80X_AUX_IN_CTRL 0x05
27#define ADAV80X_REC_CTRL 0x06
28#define ADAV80X_AUX_OUT_CTRL 0x07
29#define ADAV80X_DPATH_CTRL1 0x62
30#define ADAV80X_DPATH_CTRL2 0x63
31#define ADAV80X_DAC_CTRL1 0x64
32#define ADAV80X_DAC_CTRL2 0x65
33#define ADAV80X_DAC_CTRL3 0x66
34#define ADAV80X_DAC_L_VOL 0x68
35#define ADAV80X_DAC_R_VOL 0x69
36#define ADAV80X_PGA_L_VOL 0x6c
37#define ADAV80X_PGA_R_VOL 0x6d
38#define ADAV80X_ADC_CTRL1 0x6e
39#define ADAV80X_ADC_CTRL2 0x6f
40#define ADAV80X_ADC_L_VOL 0x70
41#define ADAV80X_ADC_R_VOL 0x71
42#define ADAV80X_PLL_CTRL1 0x74
43#define ADAV80X_PLL_CTRL2 0x75
44#define ADAV80X_ICLK_CTRL1 0x76
45#define ADAV80X_ICLK_CTRL2 0x77
46#define ADAV80X_PLL_CLK_SRC 0x78
47#define ADAV80X_PLL_OUTE 0x7a
48
49#define ADAV80X_PLL_CLK_SRC_PLL_XIN(pll) 0x00
50#define ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll) (0x40 << (pll))
51#define ADAV80X_PLL_CLK_SRC_PLL_MASK(pll) (0x40 << (pll))
52
53#define ADAV80X_ICLK_CTRL1_DAC_SRC(src) ((src) << 5)
54#define ADAV80X_ICLK_CTRL1_ADC_SRC(src) ((src) << 2)
55#define ADAV80X_ICLK_CTRL1_ICLK2_SRC(src) (src)
56#define ADAV80X_ICLK_CTRL2_ICLK1_SRC(src) ((src) << 3)
57
58#define ADAV80X_PLL_CTRL1_PLLDIV 0x10
59#define ADAV80X_PLL_CTRL1_PLLPD(pll) (0x04 << (pll))
60#define ADAV80X_PLL_CTRL1_XTLPD 0x02
61
62#define ADAV80X_PLL_CTRL2_FIELD(pll, x) ((x) << ((pll) * 4))
63
64#define ADAV80X_PLL_CTRL2_FS_48(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x00)
65#define ADAV80X_PLL_CTRL2_FS_32(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x08)
66#define ADAV80X_PLL_CTRL2_FS_44(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0c)
67
68#define ADAV80X_PLL_CTRL2_SEL(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x02)
69#define ADAV80X_PLL_CTRL2_DOUB(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x01)
70#define ADAV80X_PLL_CTRL2_PLL_MASK(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0f)
71
72#define ADAV80X_ADC_CTRL1_MODULATOR_MASK 0x80
73#define ADAV80X_ADC_CTRL1_MODULATOR_128FS 0x00
74#define ADAV80X_ADC_CTRL1_MODULATOR_64FS 0x80
75
76#define ADAV80X_DAC_CTRL1_PD 0x80
77
78#define ADAV80X_DAC_CTRL2_DIV1 0x00
79#define ADAV80X_DAC_CTRL2_DIV1_5 0x10
80#define ADAV80X_DAC_CTRL2_DIV2 0x20
81#define ADAV80X_DAC_CTRL2_DIV3 0x30
82#define ADAV80X_DAC_CTRL2_DIV_MASK 0x30
83
84#define ADAV80X_DAC_CTRL2_INTERPOL_256FS 0x00
85#define ADAV80X_DAC_CTRL2_INTERPOL_128FS 0x40
86#define ADAV80X_DAC_CTRL2_INTERPOL_64FS 0x80
87#define ADAV80X_DAC_CTRL2_INTERPOL_MASK 0xc0
88
89#define ADAV80X_DAC_CTRL2_DEEMPH_NONE 0x00
90#define ADAV80X_DAC_CTRL2_DEEMPH_44 0x01
91#define ADAV80X_DAC_CTRL2_DEEMPH_32 0x02
92#define ADAV80X_DAC_CTRL2_DEEMPH_48 0x03
93#define ADAV80X_DAC_CTRL2_DEEMPH_MASK 0x01
94
95#define ADAV80X_CAPTURE_MODE_MASTER 0x20
96#define ADAV80X_CAPTURE_WORD_LEN24 0x00
97#define ADAV80X_CAPTURE_WORD_LEN20 0x04
98#define ADAV80X_CAPTRUE_WORD_LEN18 0x08
99#define ADAV80X_CAPTURE_WORD_LEN16 0x0c
100#define ADAV80X_CAPTURE_WORD_LEN_MASK 0x0c
101
102#define ADAV80X_CAPTURE_MODE_LEFT_J 0x00
103#define ADAV80X_CAPTURE_MODE_I2S 0x01
104#define ADAV80X_CAPTURE_MODE_RIGHT_J 0x03
105#define ADAV80X_CAPTURE_MODE_MASK 0x03
106
107#define ADAV80X_PLAYBACK_MODE_MASTER 0x10
108#define ADAV80X_PLAYBACK_MODE_LEFT_J 0x00
109#define ADAV80X_PLAYBACK_MODE_I2S 0x01
110#define ADAV80X_PLAYBACK_MODE_RIGHT_J_24 0x04
111#define ADAV80X_PLAYBACK_MODE_RIGHT_J_20 0x05
112#define ADAV80X_PLAYBACK_MODE_RIGHT_J_18 0x06
113#define ADAV80X_PLAYBACK_MODE_RIGHT_J_16 0x07
114#define ADAV80X_PLAYBACK_MODE_MASK 0x07
115
116#define ADAV80X_PLL_OUTE_SYSCLKPD(x) BIT(2 - (x))
117
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200118static struct reg_default adav80x_reg_defaults[] = {
119 { ADAV80X_PLAYBACK_CTRL, 0x01 },
120 { ADAV80X_AUX_IN_CTRL, 0x01 },
121 { ADAV80X_REC_CTRL, 0x02 },
122 { ADAV80X_AUX_OUT_CTRL, 0x01 },
123 { ADAV80X_DPATH_CTRL1, 0xc0 },
124 { ADAV80X_DPATH_CTRL2, 0x11 },
125 { ADAV80X_DAC_CTRL1, 0x00 },
126 { ADAV80X_DAC_CTRL2, 0x00 },
127 { ADAV80X_DAC_CTRL3, 0x00 },
128 { ADAV80X_DAC_L_VOL, 0xff },
129 { ADAV80X_DAC_R_VOL, 0xff },
130 { ADAV80X_PGA_L_VOL, 0x00 },
131 { ADAV80X_PGA_R_VOL, 0x00 },
132 { ADAV80X_ADC_CTRL1, 0x00 },
133 { ADAV80X_ADC_CTRL2, 0x00 },
134 { ADAV80X_ADC_L_VOL, 0xff },
135 { ADAV80X_ADC_R_VOL, 0xff },
136 { ADAV80X_PLL_CTRL1, 0x00 },
137 { ADAV80X_PLL_CTRL2, 0x00 },
138 { ADAV80X_ICLK_CTRL1, 0x00 },
139 { ADAV80X_ICLK_CTRL2, 0x00 },
140 { ADAV80X_PLL_CLK_SRC, 0x00 },
141 { ADAV80X_PLL_OUTE, 0x00 },
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200142};
143
144struct adav80x {
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200145 struct regmap *regmap;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200146
147 enum adav80x_clk_src clk_src;
148 unsigned int sysclk;
149 enum adav80x_pll_src pll_src;
150
151 unsigned int dai_fmt[2];
152 unsigned int rate;
153 bool deemph;
154 bool sysclk_pd[3];
155};
156
157static const char *adav80x_mux_text[] = {
158 "ADC",
159 "Playback",
160 "Aux Playback",
161};
162
163static const unsigned int adav80x_mux_values[] = {
164 0, 2, 3,
165};
166
167#define ADAV80X_MUX_ENUM_DECL(name, reg, shift) \
168 SOC_VALUE_ENUM_DOUBLE_DECL(name, reg, shift, 7, \
169 ARRAY_SIZE(adav80x_mux_text), adav80x_mux_text, \
170 adav80x_mux_values)
171
172static ADAV80X_MUX_ENUM_DECL(adav80x_aux_capture_enum, ADAV80X_DPATH_CTRL1, 0);
173static ADAV80X_MUX_ENUM_DECL(adav80x_capture_enum, ADAV80X_DPATH_CTRL1, 3);
174static ADAV80X_MUX_ENUM_DECL(adav80x_dac_enum, ADAV80X_DPATH_CTRL2, 3);
175
176static const struct snd_kcontrol_new adav80x_aux_capture_mux_ctrl =
177 SOC_DAPM_VALUE_ENUM("Route", adav80x_aux_capture_enum);
178static const struct snd_kcontrol_new adav80x_capture_mux_ctrl =
179 SOC_DAPM_VALUE_ENUM("Route", adav80x_capture_enum);
180static const struct snd_kcontrol_new adav80x_dac_mux_ctrl =
181 SOC_DAPM_VALUE_ENUM("Route", adav80x_dac_enum);
182
183#define ADAV80X_MUX(name, ctrl) \
184 SND_SOC_DAPM_VALUE_MUX(name, SND_SOC_NOPM, 0, 0, ctrl)
185
186static const struct snd_soc_dapm_widget adav80x_dapm_widgets[] = {
187 SND_SOC_DAPM_DAC("DAC", NULL, ADAV80X_DAC_CTRL1, 7, 1),
188 SND_SOC_DAPM_ADC("ADC", NULL, ADAV80X_ADC_CTRL1, 5, 1),
189
190 SND_SOC_DAPM_PGA("Right PGA", ADAV80X_ADC_CTRL1, 0, 1, NULL, 0),
191 SND_SOC_DAPM_PGA("Left PGA", ADAV80X_ADC_CTRL1, 1, 1, NULL, 0),
192
193 SND_SOC_DAPM_AIF_OUT("AIFOUT", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
194 SND_SOC_DAPM_AIF_IN("AIFIN", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
195
196 SND_SOC_DAPM_AIF_OUT("AIFAUXOUT", "Aux Capture", 0, SND_SOC_NOPM, 0, 0),
197 SND_SOC_DAPM_AIF_IN("AIFAUXIN", "Aux Playback", 0, SND_SOC_NOPM, 0, 0),
198
199 ADAV80X_MUX("Aux Capture Select", &adav80x_aux_capture_mux_ctrl),
200 ADAV80X_MUX("Capture Select", &adav80x_capture_mux_ctrl),
201 ADAV80X_MUX("DAC Select", &adav80x_dac_mux_ctrl),
202
203 SND_SOC_DAPM_INPUT("VINR"),
204 SND_SOC_DAPM_INPUT("VINL"),
205 SND_SOC_DAPM_OUTPUT("VOUTR"),
206 SND_SOC_DAPM_OUTPUT("VOUTL"),
207
208 SND_SOC_DAPM_SUPPLY("SYSCLK", SND_SOC_NOPM, 0, 0, NULL, 0),
209 SND_SOC_DAPM_SUPPLY("PLL1", ADAV80X_PLL_CTRL1, 2, 1, NULL, 0),
210 SND_SOC_DAPM_SUPPLY("PLL2", ADAV80X_PLL_CTRL1, 3, 1, NULL, 0),
211 SND_SOC_DAPM_SUPPLY("OSC", ADAV80X_PLL_CTRL1, 1, 1, NULL, 0),
212};
213
214static int adav80x_dapm_sysclk_check(struct snd_soc_dapm_widget *source,
215 struct snd_soc_dapm_widget *sink)
216{
217 struct snd_soc_codec *codec = source->codec;
218 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
219 const char *clk;
220
221 switch (adav80x->clk_src) {
222 case ADAV80X_CLK_PLL1:
223 clk = "PLL1";
224 break;
225 case ADAV80X_CLK_PLL2:
226 clk = "PLL2";
227 break;
228 case ADAV80X_CLK_XTAL:
229 clk = "OSC";
230 break;
231 default:
232 return 0;
233 }
234
235 return strcmp(source->name, clk) == 0;
236}
237
238static int adav80x_dapm_pll_check(struct snd_soc_dapm_widget *source,
239 struct snd_soc_dapm_widget *sink)
240{
241 struct snd_soc_codec *codec = source->codec;
242 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
243
244 return adav80x->pll_src == ADAV80X_PLL_SRC_XTAL;
245}
246
247
248static const struct snd_soc_dapm_route adav80x_dapm_routes[] = {
249 { "DAC Select", "ADC", "ADC" },
250 { "DAC Select", "Playback", "AIFIN" },
251 { "DAC Select", "Aux Playback", "AIFAUXIN" },
252 { "DAC", NULL, "DAC Select" },
253
254 { "Capture Select", "ADC", "ADC" },
255 { "Capture Select", "Playback", "AIFIN" },
256 { "Capture Select", "Aux Playback", "AIFAUXIN" },
257 { "AIFOUT", NULL, "Capture Select" },
258
259 { "Aux Capture Select", "ADC", "ADC" },
260 { "Aux Capture Select", "Playback", "AIFIN" },
261 { "Aux Capture Select", "Aux Playback", "AIFAUXIN" },
262 { "AIFAUXOUT", NULL, "Aux Capture Select" },
263
264 { "VOUTR", NULL, "DAC" },
265 { "VOUTL", NULL, "DAC" },
266
267 { "Left PGA", NULL, "VINL" },
268 { "Right PGA", NULL, "VINR" },
269 { "ADC", NULL, "Left PGA" },
270 { "ADC", NULL, "Right PGA" },
271
272 { "SYSCLK", NULL, "PLL1", adav80x_dapm_sysclk_check },
273 { "SYSCLK", NULL, "PLL2", adav80x_dapm_sysclk_check },
274 { "SYSCLK", NULL, "OSC", adav80x_dapm_sysclk_check },
275 { "PLL1", NULL, "OSC", adav80x_dapm_pll_check },
276 { "PLL2", NULL, "OSC", adav80x_dapm_pll_check },
277
278 { "ADC", NULL, "SYSCLK" },
279 { "DAC", NULL, "SYSCLK" },
280 { "AIFOUT", NULL, "SYSCLK" },
281 { "AIFAUXOUT", NULL, "SYSCLK" },
282 { "AIFIN", NULL, "SYSCLK" },
283 { "AIFAUXIN", NULL, "SYSCLK" },
284};
285
286static int adav80x_set_deemph(struct snd_soc_codec *codec)
287{
288 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
289 unsigned int val;
290
291 if (adav80x->deemph) {
292 switch (adav80x->rate) {
293 case 32000:
294 val = ADAV80X_DAC_CTRL2_DEEMPH_32;
295 break;
296 case 44100:
297 val = ADAV80X_DAC_CTRL2_DEEMPH_44;
298 break;
299 case 48000:
300 case 64000:
301 case 88200:
302 case 96000:
303 val = ADAV80X_DAC_CTRL2_DEEMPH_48;
304 break;
305 default:
306 val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
307 break;
308 }
309 } else {
310 val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
311 }
312
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200313 return regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL2,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200314 ADAV80X_DAC_CTRL2_DEEMPH_MASK, val);
315}
316
317static int adav80x_put_deemph(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_value *ucontrol)
319{
320 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
321 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
322 unsigned int deemph = ucontrol->value.enumerated.item[0];
323
324 if (deemph > 1)
325 return -EINVAL;
326
327 adav80x->deemph = deemph;
328
329 return adav80x_set_deemph(codec);
330}
331
332static int adav80x_get_deemph(struct snd_kcontrol *kcontrol,
333 struct snd_ctl_elem_value *ucontrol)
334{
335 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
336 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
337
338 ucontrol->value.enumerated.item[0] = adav80x->deemph;
339 return 0;
340};
341
342static const DECLARE_TLV_DB_SCALE(adav80x_inpga_tlv, 0, 50, 0);
343static const DECLARE_TLV_DB_MINMAX(adav80x_digital_tlv, -9563, 0);
344
345static const struct snd_kcontrol_new adav80x_controls[] = {
346 SOC_DOUBLE_R_TLV("Master Playback Volume", ADAV80X_DAC_L_VOL,
347 ADAV80X_DAC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
348 SOC_DOUBLE_R_TLV("Master Capture Volume", ADAV80X_ADC_L_VOL,
349 ADAV80X_ADC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
350
351 SOC_DOUBLE_R_TLV("PGA Capture Volume", ADAV80X_PGA_L_VOL,
352 ADAV80X_PGA_R_VOL, 0, 0x30, 0, adav80x_inpga_tlv),
353
354 SOC_DOUBLE("Master Playback Switch", ADAV80X_DAC_CTRL1, 0, 1, 1, 0),
355 SOC_DOUBLE("Master Capture Switch", ADAV80X_ADC_CTRL1, 2, 3, 1, 1),
356
357 SOC_SINGLE("ADC High Pass Filter Switch", ADAV80X_ADC_CTRL1, 6, 1, 0),
358
359 SOC_SINGLE_BOOL_EXT("Playback De-emphasis Switch", 0,
360 adav80x_get_deemph, adav80x_put_deemph),
361};
362
363static unsigned int adav80x_port_ctrl_regs[2][2] = {
364 { ADAV80X_REC_CTRL, ADAV80X_PLAYBACK_CTRL, },
365 { ADAV80X_AUX_OUT_CTRL, ADAV80X_AUX_IN_CTRL },
366};
367
368static int adav80x_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
369{
370 struct snd_soc_codec *codec = dai->codec;
371 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
372 unsigned int capture = 0x00;
373 unsigned int playback = 0x00;
374
375 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
376 case SND_SOC_DAIFMT_CBM_CFM:
377 capture |= ADAV80X_CAPTURE_MODE_MASTER;
378 playback |= ADAV80X_PLAYBACK_MODE_MASTER;
379 case SND_SOC_DAIFMT_CBS_CFS:
380 break;
381 default:
382 return -EINVAL;
383 }
384
385 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
386 case SND_SOC_DAIFMT_I2S:
387 capture |= ADAV80X_CAPTURE_MODE_I2S;
388 playback |= ADAV80X_PLAYBACK_MODE_I2S;
389 break;
390 case SND_SOC_DAIFMT_LEFT_J:
391 capture |= ADAV80X_CAPTURE_MODE_LEFT_J;
392 playback |= ADAV80X_PLAYBACK_MODE_LEFT_J;
393 break;
394 case SND_SOC_DAIFMT_RIGHT_J:
395 capture |= ADAV80X_CAPTURE_MODE_RIGHT_J;
396 playback |= ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
397 break;
398 default:
399 return -EINVAL;
400 }
401
402 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
403 case SND_SOC_DAIFMT_NB_NF:
404 break;
405 default:
406 return -EINVAL;
407 }
408
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200409 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][0],
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200410 ADAV80X_CAPTURE_MODE_MASK | ADAV80X_CAPTURE_MODE_MASTER,
411 capture);
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200412 regmap_write(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][1],
413 playback);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200414
415 adav80x->dai_fmt[dai->id] = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
416
417 return 0;
418}
419
420static int adav80x_set_adc_clock(struct snd_soc_codec *codec,
421 unsigned int sample_rate)
422{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200423 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200424 unsigned int val;
425
426 if (sample_rate <= 48000)
427 val = ADAV80X_ADC_CTRL1_MODULATOR_128FS;
428 else
429 val = ADAV80X_ADC_CTRL1_MODULATOR_64FS;
430
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200431 regmap_update_bits(adav80x->regmap, ADAV80X_ADC_CTRL1,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200432 ADAV80X_ADC_CTRL1_MODULATOR_MASK, val);
433
434 return 0;
435}
436
437static int adav80x_set_dac_clock(struct snd_soc_codec *codec,
438 unsigned int sample_rate)
439{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200440 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200441 unsigned int val;
442
443 if (sample_rate <= 48000)
444 val = ADAV80X_DAC_CTRL2_DIV1 | ADAV80X_DAC_CTRL2_INTERPOL_256FS;
445 else
446 val = ADAV80X_DAC_CTRL2_DIV2 | ADAV80X_DAC_CTRL2_INTERPOL_128FS;
447
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200448 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL2,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200449 ADAV80X_DAC_CTRL2_DIV_MASK | ADAV80X_DAC_CTRL2_INTERPOL_MASK,
450 val);
451
452 return 0;
453}
454
455static int adav80x_set_capture_pcm_format(struct snd_soc_codec *codec,
Mark Browncf7d8b22014-01-08 18:50:40 +0000456 struct snd_soc_dai *dai, struct snd_pcm_hw_params *params)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200457{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200458 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200459 unsigned int val;
460
Mark Browncf7d8b22014-01-08 18:50:40 +0000461 switch (params_width(params)) {
462 case 16:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200463 val = ADAV80X_CAPTURE_WORD_LEN16;
464 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000465 case 18:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200466 val = ADAV80X_CAPTRUE_WORD_LEN18;
467 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000468 case 20:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200469 val = ADAV80X_CAPTURE_WORD_LEN20;
470 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000471 case 24:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200472 val = ADAV80X_CAPTURE_WORD_LEN24;
473 break;
474 default:
Mark Brownca1004b2011-07-16 11:34:58 +0900475 return -EINVAL;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200476 }
477
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200478 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][0],
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200479 ADAV80X_CAPTURE_WORD_LEN_MASK, val);
480
481 return 0;
482}
483
484static int adav80x_set_playback_pcm_format(struct snd_soc_codec *codec,
Mark Browncf7d8b22014-01-08 18:50:40 +0000485 struct snd_soc_dai *dai, struct snd_pcm_hw_params *params)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200486{
487 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
488 unsigned int val;
489
490 if (adav80x->dai_fmt[dai->id] != SND_SOC_DAIFMT_RIGHT_J)
491 return 0;
492
Mark Browncf7d8b22014-01-08 18:50:40 +0000493 switch (params_width(params)) {
494 case 16:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200495 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_16;
496 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000497 case 18:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200498 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_18;
499 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000500 case 20:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200501 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_20;
502 break;
Mark Browncf7d8b22014-01-08 18:50:40 +0000503 case 24:
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200504 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
505 break;
506 default:
Mark Brownca1004b2011-07-16 11:34:58 +0900507 return -EINVAL;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200508 }
509
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200510 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][1],
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200511 ADAV80X_PLAYBACK_MODE_MASK, val);
512
513 return 0;
514}
515
516static int adav80x_hw_params(struct snd_pcm_substream *substream,
517 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
518{
519 struct snd_soc_codec *codec = dai->codec;
520 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
521 unsigned int rate = params_rate(params);
522
523 if (rate * 256 != adav80x->sysclk)
524 return -EINVAL;
525
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Browncf7d8b22014-01-08 18:50:40 +0000527 adav80x_set_playback_pcm_format(codec, dai, params);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200528 adav80x_set_dac_clock(codec, rate);
529 } else {
Mark Browncf7d8b22014-01-08 18:50:40 +0000530 adav80x_set_capture_pcm_format(codec, dai, params);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200531 adav80x_set_adc_clock(codec, rate);
532 }
533 adav80x->rate = rate;
534 adav80x_set_deemph(codec);
535
536 return 0;
537}
538
539static int adav80x_set_sysclk(struct snd_soc_codec *codec,
Mark Brownda1c6ea2011-08-24 20:09:01 +0100540 int clk_id, int source,
541 unsigned int freq, int dir)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200542{
543 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000544 struct snd_soc_dapm_context *dapm = &codec->dapm;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200545
546 if (dir == SND_SOC_CLOCK_IN) {
547 switch (clk_id) {
548 case ADAV80X_CLK_XIN:
549 case ADAV80X_CLK_XTAL:
550 case ADAV80X_CLK_MCLKI:
551 case ADAV80X_CLK_PLL1:
552 case ADAV80X_CLK_PLL2:
553 break;
554 default:
555 return -EINVAL;
556 }
557
558 adav80x->sysclk = freq;
559
560 if (adav80x->clk_src != clk_id) {
561 unsigned int iclk_ctrl1, iclk_ctrl2;
562
563 adav80x->clk_src = clk_id;
564 if (clk_id == ADAV80X_CLK_XTAL)
565 clk_id = ADAV80X_CLK_XIN;
566
567 iclk_ctrl1 = ADAV80X_ICLK_CTRL1_DAC_SRC(clk_id) |
568 ADAV80X_ICLK_CTRL1_ADC_SRC(clk_id) |
569 ADAV80X_ICLK_CTRL1_ICLK2_SRC(clk_id);
570 iclk_ctrl2 = ADAV80X_ICLK_CTRL2_ICLK1_SRC(clk_id);
571
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200572 regmap_write(adav80x->regmap, ADAV80X_ICLK_CTRL1,
573 iclk_ctrl1);
574 regmap_write(adav80x->regmap, ADAV80X_ICLK_CTRL2,
575 iclk_ctrl2);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200576
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000577 snd_soc_dapm_sync(dapm);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200578 }
579 } else {
580 unsigned int mask;
581
582 switch (clk_id) {
583 case ADAV80X_CLK_SYSCLK1:
584 case ADAV80X_CLK_SYSCLK2:
585 case ADAV80X_CLK_SYSCLK3:
586 break;
587 default:
588 return -EINVAL;
589 }
590
591 clk_id -= ADAV80X_CLK_SYSCLK1;
592 mask = ADAV80X_PLL_OUTE_SYSCLKPD(clk_id);
593
594 if (freq == 0) {
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200595 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_OUTE,
596 mask, mask);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200597 adav80x->sysclk_pd[clk_id] = true;
598 } else {
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200599 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_OUTE,
600 mask, 0);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200601 adav80x->sysclk_pd[clk_id] = false;
602 }
603
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000604 snd_soc_dapm_mutex_lock(dapm);
605
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200606 if (adav80x->sysclk_pd[0])
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000607 snd_soc_dapm_disable_pin_unlocked(dapm, "PLL1");
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200608 else
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000609 snd_soc_dapm_force_enable_pin_unlocked(dapm, "PLL1");
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200610
611 if (adav80x->sysclk_pd[1] || adav80x->sysclk_pd[2])
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000612 snd_soc_dapm_disable_pin_unlocked(dapm, "PLL2");
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200613 else
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000614 snd_soc_dapm_force_enable_pin_unlocked(dapm, "PLL2");
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200615
Charles Keepax1bf1b8c2014-02-18 15:22:16 +0000616 snd_soc_dapm_sync_unlocked(dapm);
617
618 snd_soc_dapm_mutex_unlock(dapm);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200619 }
620
621 return 0;
622}
623
624static int adav80x_set_pll(struct snd_soc_codec *codec, int pll_id,
625 int source, unsigned int freq_in, unsigned int freq_out)
626{
627 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
628 unsigned int pll_ctrl1 = 0;
629 unsigned int pll_ctrl2 = 0;
630 unsigned int pll_src;
631
632 switch (source) {
633 case ADAV80X_PLL_SRC_XTAL:
634 case ADAV80X_PLL_SRC_XIN:
635 case ADAV80X_PLL_SRC_MCLKI:
636 break;
637 default:
638 return -EINVAL;
639 }
640
641 if (!freq_out)
642 return 0;
643
644 switch (freq_in) {
645 case 27000000:
646 break;
647 case 54000000:
648 if (source == ADAV80X_PLL_SRC_XIN) {
649 pll_ctrl1 |= ADAV80X_PLL_CTRL1_PLLDIV;
650 break;
651 }
652 default:
653 return -EINVAL;
654 }
655
656 if (freq_out > 12288000) {
657 pll_ctrl2 |= ADAV80X_PLL_CTRL2_DOUB(pll_id);
658 freq_out /= 2;
659 }
660
661 /* freq_out = sample_rate * 256 */
662 switch (freq_out) {
663 case 8192000:
664 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_32(pll_id);
665 break;
666 case 11289600:
667 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_44(pll_id);
668 break;
669 case 12288000:
670 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_48(pll_id);
671 break;
672 default:
673 return -EINVAL;
674 }
675
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200676 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CTRL1,
677 ADAV80X_PLL_CTRL1_PLLDIV, pll_ctrl1);
678 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CTRL2,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200679 ADAV80X_PLL_CTRL2_PLL_MASK(pll_id), pll_ctrl2);
680
681 if (source != adav80x->pll_src) {
682 if (source == ADAV80X_PLL_SRC_MCLKI)
683 pll_src = ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll_id);
684 else
685 pll_src = ADAV80X_PLL_CLK_SRC_PLL_XIN(pll_id);
686
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200687 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CLK_SRC,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200688 ADAV80X_PLL_CLK_SRC_PLL_MASK(pll_id), pll_src);
689
690 adav80x->pll_src = source;
691
692 snd_soc_dapm_sync(&codec->dapm);
693 }
694
695 return 0;
696}
697
698static int adav80x_set_bias_level(struct snd_soc_codec *codec,
699 enum snd_soc_bias_level level)
700{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200701 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200702 unsigned int mask = ADAV80X_DAC_CTRL1_PD;
703
704 switch (level) {
705 case SND_SOC_BIAS_ON:
706 break;
707 case SND_SOC_BIAS_PREPARE:
708 break;
709 case SND_SOC_BIAS_STANDBY:
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200710 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL1, mask,
711 0x00);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200712 break;
713 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200714 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL1, mask,
715 mask);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200716 break;
717 }
718
719 codec->dapm.bias_level = level;
720 return 0;
721}
722
723/* Enforce the same sample rate on all audio interfaces */
724static int adav80x_dai_startup(struct snd_pcm_substream *substream,
725 struct snd_soc_dai *dai)
726{
727 struct snd_soc_codec *codec = dai->codec;
728 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
729
730 if (!codec->active || !adav80x->rate)
731 return 0;
732
733 return snd_pcm_hw_constraint_minmax(substream->runtime,
734 SNDRV_PCM_HW_PARAM_RATE, adav80x->rate, adav80x->rate);
735}
736
737static void adav80x_dai_shutdown(struct snd_pcm_substream *substream,
738 struct snd_soc_dai *dai)
739{
740 struct snd_soc_codec *codec = dai->codec;
741 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
742
743 if (!codec->active)
744 adav80x->rate = 0;
745}
746
Lars-Peter Clausen890754a2011-11-23 14:11:21 +0100747static const struct snd_soc_dai_ops adav80x_dai_ops = {
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200748 .set_fmt = adav80x_set_dai_fmt,
749 .hw_params = adav80x_hw_params,
750 .startup = adav80x_dai_startup,
751 .shutdown = adav80x_dai_shutdown,
752};
753
754#define ADAV80X_PLAYBACK_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
755 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | \
756 SNDRV_PCM_RATE_96000)
757
758#define ADAV80X_CAPTURE_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
759
760#define ADAV80X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \
761 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
762
763static struct snd_soc_dai_driver adav80x_dais[] = {
764 {
765 .name = "adav80x-hifi",
766 .id = 0,
767 .playback = {
768 .stream_name = "HiFi Playback",
769 .channels_min = 2,
770 .channels_max = 2,
771 .rates = ADAV80X_PLAYBACK_RATES,
772 .formats = ADAV80X_FORMATS,
773 },
774 .capture = {
775 .stream_name = "HiFi Capture",
776 .channels_min = 2,
777 .channels_max = 2,
778 .rates = ADAV80X_CAPTURE_RATES,
779 .formats = ADAV80X_FORMATS,
780 },
781 .ops = &adav80x_dai_ops,
782 },
783 {
784 .name = "adav80x-aux",
785 .id = 1,
786 .playback = {
787 .stream_name = "Aux Playback",
788 .channels_min = 2,
789 .channels_max = 2,
790 .rates = ADAV80X_PLAYBACK_RATES,
791 .formats = ADAV80X_FORMATS,
792 },
793 .capture = {
794 .stream_name = "Aux Capture",
795 .channels_min = 2,
796 .channels_max = 2,
797 .rates = ADAV80X_CAPTURE_RATES,
798 .formats = ADAV80X_FORMATS,
799 },
800 .ops = &adav80x_dai_ops,
801 },
802};
803
804static int adav80x_probe(struct snd_soc_codec *codec)
805{
806 int ret;
807 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
808
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200809 ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200810 if (ret) {
811 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
812 return ret;
813 }
814
815 /* Force PLLs on for SYSCLK output */
816 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
817 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL2");
818
819 /* Power down S/PDIF receiver, since it is currently not supported */
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200820 regmap_write(adav80x->regmap, ADAV80X_PLL_OUTE, 0x20);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200821 /* Disable DAC zero flag */
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200822 regmap_write(adav80x->regmap, ADAV80X_DAC_CTRL3, 0x6);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200823
824 return adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
825}
826
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100827static int adav80x_suspend(struct snd_soc_codec *codec)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200828{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200829 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
830 int ret;
831
832 ret = adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
833 regcache_cache_only(adav80x->regmap, true);
834
835 return ret;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200836}
837
838static int adav80x_resume(struct snd_soc_codec *codec)
839{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200840 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
841
842 regcache_cache_only(adav80x->regmap, false);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200843 adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200844 regcache_sync(adav80x->regmap);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200845
846 return 0;
847}
848
849static int adav80x_remove(struct snd_soc_codec *codec)
850{
851 return adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
852}
853
854static struct snd_soc_codec_driver adav80x_codec_driver = {
855 .probe = adav80x_probe,
856 .remove = adav80x_remove,
857 .suspend = adav80x_suspend,
858 .resume = adav80x_resume,
859 .set_bias_level = adav80x_set_bias_level,
860
861 .set_pll = adav80x_set_pll,
862 .set_sysclk = adav80x_set_sysclk,
863
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200864 .controls = adav80x_controls,
865 .num_controls = ARRAY_SIZE(adav80x_controls),
866 .dapm_widgets = adav80x_dapm_widgets,
867 .num_dapm_widgets = ARRAY_SIZE(adav80x_dapm_widgets),
868 .dapm_routes = adav80x_dapm_routes,
869 .num_dapm_routes = ARRAY_SIZE(adav80x_dapm_routes),
870};
871
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200872static int adav80x_bus_probe(struct device *dev, struct regmap *regmap)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200873{
874 struct adav80x *adav80x;
875 int ret;
876
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200877 if (IS_ERR(regmap))
878 return PTR_ERR(regmap);
879
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200880 adav80x = kzalloc(sizeof(*adav80x), GFP_KERNEL);
881 if (!adav80x)
882 return -ENOMEM;
883
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200884
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200885 dev_set_drvdata(dev, adav80x);
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200886 adav80x->regmap = regmap;
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200887
888 ret = snd_soc_register_codec(dev, &adav80x_codec_driver,
889 adav80x_dais, ARRAY_SIZE(adav80x_dais));
890 if (ret)
891 kfree(adav80x);
892
893 return ret;
894}
895
Bill Pemberton7a79e942012-12-07 09:26:37 -0500896static int adav80x_bus_remove(struct device *dev)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200897{
898 snd_soc_unregister_codec(dev);
899 kfree(dev_get_drvdata(dev));
900 return 0;
901}
902
903#if defined(CONFIG_SPI_MASTER)
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200904static const struct regmap_config adav80x_spi_regmap_config = {
905 .val_bits = 8,
906 .pad_bits = 1,
907 .reg_bits = 7,
908 .read_flag_mask = 0x01,
909
910 .max_register = ADAV80X_PLL_OUTE,
911
912 .cache_type = REGCACHE_RBTREE,
913 .reg_defaults = adav80x_reg_defaults,
914 .num_reg_defaults = ARRAY_SIZE(adav80x_reg_defaults),
915};
916
Axel Line394fe52013-07-01 16:49:04 +0800917static const struct spi_device_id adav80x_spi_id[] = {
918 { "adav801", 0 },
919 { }
920};
921MODULE_DEVICE_TABLE(spi, adav80x_spi_id);
922
Bill Pemberton7a79e942012-12-07 09:26:37 -0500923static int adav80x_spi_probe(struct spi_device *spi)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200924{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200925 return adav80x_bus_probe(&spi->dev,
926 devm_regmap_init_spi(spi, &adav80x_spi_regmap_config));
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200927}
928
Bill Pemberton7a79e942012-12-07 09:26:37 -0500929static int adav80x_spi_remove(struct spi_device *spi)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200930{
931 return adav80x_bus_remove(&spi->dev);
932}
933
934static struct spi_driver adav80x_spi_driver = {
935 .driver = {
936 .name = "adav801",
937 .owner = THIS_MODULE,
938 },
939 .probe = adav80x_spi_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500940 .remove = adav80x_spi_remove,
Axel Line394fe52013-07-01 16:49:04 +0800941 .id_table = adav80x_spi_id,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200942};
943#endif
944
Fabio Estevam04c3a852013-11-20 15:37:44 -0200945#if IS_ENABLED(CONFIG_I2C)
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200946static const struct regmap_config adav80x_i2c_regmap_config = {
947 .val_bits = 8,
948 .pad_bits = 1,
949 .reg_bits = 7,
950
951 .max_register = ADAV80X_PLL_OUTE,
952
953 .cache_type = REGCACHE_RBTREE,
954 .reg_defaults = adav80x_reg_defaults,
955 .num_reg_defaults = ARRAY_SIZE(adav80x_reg_defaults),
956};
957
Axel Line394fe52013-07-01 16:49:04 +0800958static const struct i2c_device_id adav80x_i2c_id[] = {
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200959 { "adav803", 0 },
960 { }
961};
Axel Line394fe52013-07-01 16:49:04 +0800962MODULE_DEVICE_TABLE(i2c, adav80x_i2c_id);
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200963
Bill Pemberton7a79e942012-12-07 09:26:37 -0500964static int adav80x_i2c_probe(struct i2c_client *client,
965 const struct i2c_device_id *id)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200966{
Lars-Peter Clausen2560b3d2013-09-27 15:18:25 +0200967 return adav80x_bus_probe(&client->dev,
968 devm_regmap_init_i2c(client, &adav80x_i2c_regmap_config));
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200969}
970
Bill Pemberton7a79e942012-12-07 09:26:37 -0500971static int adav80x_i2c_remove(struct i2c_client *client)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200972{
973 return adav80x_bus_remove(&client->dev);
974}
975
976static struct i2c_driver adav80x_i2c_driver = {
977 .driver = {
978 .name = "adav803",
979 .owner = THIS_MODULE,
980 },
981 .probe = adav80x_i2c_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500982 .remove = adav80x_i2c_remove,
Axel Line394fe52013-07-01 16:49:04 +0800983 .id_table = adav80x_i2c_id,
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200984};
985#endif
986
987static int __init adav80x_init(void)
988{
989 int ret = 0;
990
Fabio Estevam04c3a852013-11-20 15:37:44 -0200991#if IS_ENABLED(CONFIG_I2C)
Lars-Peter Clausencc526882011-06-27 17:04:01 +0200992 ret = i2c_add_driver(&adav80x_i2c_driver);
993 if (ret)
994 return ret;
995#endif
996
997#if defined(CONFIG_SPI_MASTER)
998 ret = spi_register_driver(&adav80x_spi_driver);
999#endif
1000
1001 return ret;
1002}
1003module_init(adav80x_init);
1004
1005static void __exit adav80x_exit(void)
1006{
Fabio Estevam04c3a852013-11-20 15:37:44 -02001007#if IS_ENABLED(CONFIG_I2C)
Lars-Peter Clausencc526882011-06-27 17:04:01 +02001008 i2c_del_driver(&adav80x_i2c_driver);
1009#endif
1010#if defined(CONFIG_SPI_MASTER)
1011 spi_unregister_driver(&adav80x_spi_driver);
1012#endif
1013}
1014module_exit(adav80x_exit);
1015
1016MODULE_DESCRIPTION("ASoC ADAV80x driver");
1017MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1018MODULE_AUTHOR("Yi Li <yi.li@analog.com>>");
1019MODULE_LICENSE("GPL");