blob: fa4ad1b093eb5d45343020dcd386a3ee593ee518 [file] [log] [blame]
Mark Browne88ba012008-08-06 13:18:26 +01001/*
Matt Flax028f5a52016-10-20 10:29:14 +11002 * wm8580.c -- WM8580 and WM8581 ALSA Soc Audio driver
Mark Browne88ba012008-08-06 13:18:26 +01003 *
Mark Brownb689d9f2012-09-12 11:59:51 +08004 * Copyright 2008-12 Wolfson Microelectronics PLC.
Mark Browne88ba012008-08-06 13:18:26 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * Notes:
12 * The WM8580 is a multichannel codec with S/PDIF support, featuring six
13 * DAC channels and two ADC channels.
14 *
Matt Flax028f5a52016-10-20 10:29:14 +110015 * The WM8581 is a multichannel codec with S/PDIF support, featuring eight
16 * DAC channels and two ADC channels.
17 *
Mark Browne88ba012008-08-06 13:18:26 +010018 * Currently only the primary audio interface is supported - S/PDIF and
19 * the secondary audio interfaces are not.
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
Mark Browne88ba012008-08-06 13:18:26 +010024#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/i2c.h>
Mark Brownb689d9f2012-09-12 11:59:51 +080029#include <linux/regmap.h>
Mark Browna583cd52009-06-17 17:30:14 +010030#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Mark Brown733eef32011-08-02 13:22:36 +090032#include <linux/of_device.h>
Mark Browna583cd52009-06-17 17:30:14 +010033
Mark Browne88ba012008-08-06 13:18:26 +010034#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
Mark Browne88ba012008-08-06 13:18:26 +010038#include <sound/tlv.h>
39#include <sound/initval.h>
40#include <asm/div64.h>
41
42#include "wm8580.h"
43
Mark Browne88ba012008-08-06 13:18:26 +010044/* WM8580 register space */
45#define WM8580_PLLA1 0x00
46#define WM8580_PLLA2 0x01
47#define WM8580_PLLA3 0x02
48#define WM8580_PLLA4 0x03
49#define WM8580_PLLB1 0x04
50#define WM8580_PLLB2 0x05
51#define WM8580_PLLB3 0x06
52#define WM8580_PLLB4 0x07
53#define WM8580_CLKSEL 0x08
54#define WM8580_PAIF1 0x09
55#define WM8580_PAIF2 0x0A
56#define WM8580_SAIF1 0x0B
57#define WM8580_PAIF3 0x0C
58#define WM8580_PAIF4 0x0D
59#define WM8580_SAIF2 0x0E
60#define WM8580_DAC_CONTROL1 0x0F
61#define WM8580_DAC_CONTROL2 0x10
62#define WM8580_DAC_CONTROL3 0x11
63#define WM8580_DAC_CONTROL4 0x12
64#define WM8580_DAC_CONTROL5 0x13
65#define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
66#define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
67#define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
68#define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
69#define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
70#define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
Matt Flax028f5a52016-10-20 10:29:14 +110071#define WM8581_DIGITAL_ATTENUATION_DACL4 0x1A
72#define WM8581_DIGITAL_ATTENUATION_DACR4 0x1B
Mark Browne88ba012008-08-06 13:18:26 +010073#define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
74#define WM8580_ADC_CONTROL1 0x1D
75#define WM8580_SPDTXCHAN0 0x1E
76#define WM8580_SPDTXCHAN1 0x1F
77#define WM8580_SPDTXCHAN2 0x20
78#define WM8580_SPDTXCHAN3 0x21
79#define WM8580_SPDTXCHAN4 0x22
80#define WM8580_SPDTXCHAN5 0x23
81#define WM8580_SPDMODE 0x24
82#define WM8580_INTMASK 0x25
83#define WM8580_GPO1 0x26
84#define WM8580_GPO2 0x27
85#define WM8580_GPO3 0x28
86#define WM8580_GPO4 0x29
87#define WM8580_GPO5 0x2A
88#define WM8580_INTSTAT 0x2B
89#define WM8580_SPDRXCHAN1 0x2C
90#define WM8580_SPDRXCHAN2 0x2D
91#define WM8580_SPDRXCHAN3 0x2E
92#define WM8580_SPDRXCHAN4 0x2F
93#define WM8580_SPDRXCHAN5 0x30
94#define WM8580_SPDSTAT 0x31
95#define WM8580_PWRDN1 0x32
96#define WM8580_PWRDN2 0x33
97#define WM8580_READBACK 0x34
98#define WM8580_RESET 0x35
99
Mark Brown6f7cb442009-03-11 18:31:08 +0000100#define WM8580_MAX_REGISTER 0x35
101
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100102#define WM8580_DACOSR 0x40
103
Mark Browne88ba012008-08-06 13:18:26 +0100104/* PLLB4 (register 7h) */
105#define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
106#define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
107#define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
108#define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
109
110#define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
111#define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
112#define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
113#define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
114
115/* CLKSEL (register 8h) */
116#define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
117#define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
118#define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
119
120/* AIF control 1 (registers 9h-bh) */
121#define WM8580_AIF_RATE_MASK 0x7
Mark Browne88ba012008-08-06 13:18:26 +0100122#define WM8580_AIF_BCLKSEL_MASK 0x18
Mark Browne88ba012008-08-06 13:18:26 +0100123
124#define WM8580_AIF_MS 0x20
125
126#define WM8580_AIF_CLKSRC_MASK 0xc0
127#define WM8580_AIF_CLKSRC_PLLA 0x40
128#define WM8580_AIF_CLKSRC_PLLB 0x40
129#define WM8580_AIF_CLKSRC_MCLK 0xc0
130
131/* AIF control 2 (registers ch-eh) */
132#define WM8580_AIF_FMT_MASK 0x03
133#define WM8580_AIF_FMT_RIGHTJ 0x00
134#define WM8580_AIF_FMT_LEFTJ 0x01
135#define WM8580_AIF_FMT_I2S 0x02
136#define WM8580_AIF_FMT_DSP 0x03
137
138#define WM8580_AIF_LENGTH_MASK 0x0c
139#define WM8580_AIF_LENGTH_16 0x00
140#define WM8580_AIF_LENGTH_20 0x04
141#define WM8580_AIF_LENGTH_24 0x08
142#define WM8580_AIF_LENGTH_32 0x0c
143
144#define WM8580_AIF_LRP 0x10
145#define WM8580_AIF_BCP 0x20
146
147/* Powerdown Register 1 (register 32h) */
148#define WM8580_PWRDN1_PWDN 0x001
149#define WM8580_PWRDN1_ALLDACPD 0x040
150
151/* Powerdown Register 2 (register 33h) */
152#define WM8580_PWRDN2_OSSCPD 0x001
153#define WM8580_PWRDN2_PLLAPD 0x002
154#define WM8580_PWRDN2_PLLBPD 0x004
155#define WM8580_PWRDN2_SPDIFPD 0x008
156#define WM8580_PWRDN2_SPDIFTXD 0x010
157#define WM8580_PWRDN2_SPDIFRXD 0x020
158
159#define WM8580_DAC_CONTROL5_MUTEALL 0x10
160
161/*
162 * wm8580 register cache
163 * We can't read the WM8580 register space when we
164 * are using 2 wire for device control, so we cache them instead.
165 */
Mark Brownb689d9f2012-09-12 11:59:51 +0800166static const struct reg_default wm8580_reg_defaults[] = {
167 { 0, 0x0121 },
168 { 1, 0x017e },
169 { 2, 0x007d },
170 { 3, 0x0014 },
171 { 4, 0x0121 },
172 { 5, 0x017e },
173 { 6, 0x007d },
174 { 7, 0x0194 },
175 { 8, 0x0010 },
176 { 9, 0x0002 },
177 { 10, 0x0002 },
178 { 11, 0x00c2 },
179 { 12, 0x0182 },
180 { 13, 0x0082 },
181 { 14, 0x000a },
182 { 15, 0x0024 },
183 { 16, 0x0009 },
184 { 17, 0x0000 },
185 { 18, 0x00ff },
186 { 19, 0x0000 },
187 { 20, 0x00ff },
188 { 21, 0x00ff },
189 { 22, 0x00ff },
190 { 23, 0x00ff },
191 { 24, 0x00ff },
192 { 25, 0x00ff },
193 { 26, 0x00ff },
194 { 27, 0x00ff },
195 { 28, 0x01f0 },
196 { 29, 0x0040 },
197 { 30, 0x0000 },
198 { 31, 0x0000 },
199 { 32, 0x0000 },
200 { 33, 0x0000 },
201 { 34, 0x0031 },
202 { 35, 0x000b },
203 { 36, 0x0039 },
204 { 37, 0x0000 },
205 { 38, 0x0010 },
206 { 39, 0x0032 },
207 { 40, 0x0054 },
208 { 41, 0x0076 },
209 { 42, 0x0098 },
210 { 43, 0x0000 },
211 { 44, 0x0000 },
212 { 45, 0x0000 },
213 { 46, 0x0000 },
214 { 47, 0x0000 },
215 { 48, 0x0000 },
216 { 49, 0x0000 },
217 { 50, 0x005e },
218 { 51, 0x003e },
219 { 52, 0x0000 },
Mark Browne88ba012008-08-06 13:18:26 +0100220};
221
Mark Brownb689d9f2012-09-12 11:59:51 +0800222static bool wm8580_volatile(struct device *dev, unsigned int reg)
223{
224 switch (reg) {
225 case WM8580_RESET:
226 return true;
227 default:
228 return false;
229 }
230}
231
Mark Brown6f7cb442009-03-11 18:31:08 +0000232struct pll_state {
233 unsigned int in;
234 unsigned int out;
235};
236
Mark Browna583cd52009-06-17 17:30:14 +0100237#define WM8580_NUM_SUPPLIES 3
238static const char *wm8580_supply_names[WM8580_NUM_SUPPLIES] = {
239 "AVDD",
240 "DVDD",
241 "PVDD",
242};
243
Matt Flax028f5a52016-10-20 10:29:14 +1100244struct wm8580_driver_data {
245 int num_dacs;
246};
247
Mark Brown6f7cb442009-03-11 18:31:08 +0000248/* codec private data */
249struct wm8580_priv {
Mark Brownb689d9f2012-09-12 11:59:51 +0800250 struct regmap *regmap;
Mark Browna583cd52009-06-17 17:30:14 +0100251 struct regulator_bulk_data supplies[WM8580_NUM_SUPPLIES];
Mark Brown6f7cb442009-03-11 18:31:08 +0000252 struct pll_state a;
253 struct pll_state b;
Matt Flax028f5a52016-10-20 10:29:14 +1100254 const struct wm8580_driver_data *drvdata;
Mark Brownc5607d82010-08-13 19:05:04 +0100255 int sysclk[2];
Mark Brown6f7cb442009-03-11 18:31:08 +0000256};
257
Mark Browne88ba012008-08-06 13:18:26 +0100258static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
259
260static int wm8580_out_vu(struct snd_kcontrol *kcontrol,
261 struct snd_ctl_elem_value *ucontrol)
262{
Mark Brownccb077f2009-04-21 12:57:00 +0100263 struct soc_mixer_control *mc =
264 (struct soc_mixer_control *)kcontrol->private_value;
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000265 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
266 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Mark Brownccb077f2009-04-21 12:57:00 +0100267 unsigned int reg = mc->reg;
268 unsigned int reg2 = mc->rreg;
Mark Browne88ba012008-08-06 13:18:26 +0100269 int ret;
Mark Browne88ba012008-08-06 13:18:26 +0100270
Mark Brownb689d9f2012-09-12 11:59:51 +0800271 /* Clear the register cache VU so we write without VU set */
272 regcache_cache_only(wm8580->regmap, true);
273 regmap_update_bits(wm8580->regmap, reg, 0x100, 0x000);
274 regmap_update_bits(wm8580->regmap, reg2, 0x100, 0x000);
275 regcache_cache_only(wm8580->regmap, false);
Mark Browne88ba012008-08-06 13:18:26 +0100276
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300277 ret = snd_soc_put_volsw(kcontrol, ucontrol);
Mark Browne88ba012008-08-06 13:18:26 +0100278 if (ret < 0)
279 return ret;
280
281 /* Now write again with the volume update bit set */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000282 snd_soc_component_update_bits(component, reg, 0x100, 0x100);
283 snd_soc_component_update_bits(component, reg2, 0x100, 0x100);
Mark Browne88ba012008-08-06 13:18:26 +0100284
285 return 0;
286}
287
Mark Browne88ba012008-08-06 13:18:26 +0100288static const struct snd_kcontrol_new wm8580_snd_controls[] = {
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300289SOC_DOUBLE_R_EXT_TLV("DAC1 Playback Volume",
290 WM8580_DIGITAL_ATTENUATION_DACL1,
291 WM8580_DIGITAL_ATTENUATION_DACR1,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300292 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300293SOC_DOUBLE_R_EXT_TLV("DAC2 Playback Volume",
294 WM8580_DIGITAL_ATTENUATION_DACL2,
295 WM8580_DIGITAL_ATTENUATION_DACR2,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300296 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300297SOC_DOUBLE_R_EXT_TLV("DAC3 Playback Volume",
298 WM8580_DIGITAL_ATTENUATION_DACL3,
299 WM8580_DIGITAL_ATTENUATION_DACR3,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300300 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Mark Browne88ba012008-08-06 13:18:26 +0100301
302SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
303SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
304SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
305
306SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
307SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
308SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
309
310SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
Mark Brown4f0ed9a2010-08-06 17:57:12 +0100311SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 1),
312SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 1),
313SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 1),
Mark Browne88ba012008-08-06 13:18:26 +0100314
Mark Browndacfe9f2010-08-13 20:01:32 +0100315SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 1),
Mark Brown6f341d142010-08-06 18:14:09 +0100316SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
Mark Browne88ba012008-08-06 13:18:26 +0100317};
318
Matt Flax028f5a52016-10-20 10:29:14 +1100319static const struct snd_kcontrol_new wm8581_snd_controls[] = {
320SOC_DOUBLE_R_EXT_TLV("DAC4 Playback Volume",
321 WM8581_DIGITAL_ATTENUATION_DACL4,
322 WM8581_DIGITAL_ATTENUATION_DACR4,
323 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
324
325SOC_SINGLE("DAC4 Deemphasis Switch", WM8580_DAC_CONTROL3, 3, 1, 0),
326
327SOC_DOUBLE("DAC4 Invert Switch", WM8580_DAC_CONTROL4, 8, 7, 1, 0),
328
329SOC_SINGLE("DAC4 Switch", WM8580_DAC_CONTROL5, 3, 1, 1),
330};
331
Mark Browne88ba012008-08-06 13:18:26 +0100332static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
333SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
334SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
335SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
336
337SND_SOC_DAPM_OUTPUT("VOUT1L"),
338SND_SOC_DAPM_OUTPUT("VOUT1R"),
339SND_SOC_DAPM_OUTPUT("VOUT2L"),
340SND_SOC_DAPM_OUTPUT("VOUT2R"),
341SND_SOC_DAPM_OUTPUT("VOUT3L"),
342SND_SOC_DAPM_OUTPUT("VOUT3R"),
343
344SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
345
346SND_SOC_DAPM_INPUT("AINL"),
347SND_SOC_DAPM_INPUT("AINR"),
348};
349
Matt Flax028f5a52016-10-20 10:29:14 +1100350static const struct snd_soc_dapm_widget wm8581_dapm_widgets[] = {
351SND_SOC_DAPM_DAC("DAC4", "Playback", WM8580_PWRDN1, 5, 1),
352
353SND_SOC_DAPM_OUTPUT("VOUT4L"),
354SND_SOC_DAPM_OUTPUT("VOUT4R"),
355};
356
Mark Brownf235c642011-12-03 11:42:01 +0000357static const struct snd_soc_dapm_route wm8580_dapm_routes[] = {
Mark Browne88ba012008-08-06 13:18:26 +0100358 { "VOUT1L", NULL, "DAC1" },
359 { "VOUT1R", NULL, "DAC1" },
360
361 { "VOUT2L", NULL, "DAC2" },
362 { "VOUT2R", NULL, "DAC2" },
363
364 { "VOUT3L", NULL, "DAC3" },
365 { "VOUT3R", NULL, "DAC3" },
366
367 { "ADC", NULL, "AINL" },
368 { "ADC", NULL, "AINR" },
369};
370
Matt Flax028f5a52016-10-20 10:29:14 +1100371static const struct snd_soc_dapm_route wm8581_dapm_routes[] = {
372 { "VOUT4L", NULL, "DAC4" },
373 { "VOUT4R", NULL, "DAC4" },
374};
375
Mark Browne88ba012008-08-06 13:18:26 +0100376/* PLL divisors */
377struct _pll_div {
378 u32 prescale:1;
379 u32 postscale:1;
380 u32 freqmode:2;
381 u32 n:4;
382 u32 k:24;
383};
384
385/* The size in bits of the pll divide */
386#define FIXED_PLL_SIZE (1 << 22)
387
388/* PLL rate to output rate divisions */
389static struct {
390 unsigned int div;
391 unsigned int freqmode;
392 unsigned int postscale;
393} post_table[] = {
394 { 2, 0, 0 },
395 { 4, 0, 1 },
396 { 4, 1, 0 },
397 { 8, 1, 1 },
398 { 8, 2, 0 },
399 { 16, 2, 1 },
400 { 12, 3, 0 },
401 { 24, 3, 1 }
402};
403
404static int pll_factors(struct _pll_div *pll_div, unsigned int target,
405 unsigned int source)
406{
407 u64 Kpart;
408 unsigned int K, Ndiv, Nmod;
409 int i;
410
Roel Kluin449bd542009-05-27 17:08:39 -0700411 pr_debug("wm8580: PLL %uHz->%uHz\n", source, target);
Mark Browne88ba012008-08-06 13:18:26 +0100412
413 /* Scale the output frequency up; the PLL should run in the
414 * region of 90-100MHz.
415 */
416 for (i = 0; i < ARRAY_SIZE(post_table); i++) {
417 if (target * post_table[i].div >= 90000000 &&
418 target * post_table[i].div <= 100000000) {
419 pll_div->freqmode = post_table[i].freqmode;
420 pll_div->postscale = post_table[i].postscale;
421 target *= post_table[i].div;
422 break;
423 }
424 }
425
426 if (i == ARRAY_SIZE(post_table)) {
427 printk(KERN_ERR "wm8580: Unable to scale output frequency "
428 "%u\n", target);
429 return -EINVAL;
430 }
431
432 Ndiv = target / source;
433
434 if (Ndiv < 5) {
435 source /= 2;
436 pll_div->prescale = 1;
437 Ndiv = target / source;
438 } else
439 pll_div->prescale = 0;
440
441 if ((Ndiv < 5) || (Ndiv > 13)) {
442 printk(KERN_ERR
Roel Kluin449bd542009-05-27 17:08:39 -0700443 "WM8580 N=%u outside supported range\n", Ndiv);
Mark Browne88ba012008-08-06 13:18:26 +0100444 return -EINVAL;
445 }
446
447 pll_div->n = Ndiv;
448 Nmod = target % source;
449 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
450
451 do_div(Kpart, source);
452
453 K = Kpart & 0xFFFFFFFF;
454
455 pll_div->k = K;
456
457 pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
458 pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
459 pll_div->postscale);
460
461 return 0;
462}
463
Mark Brown85488032009-09-05 18:52:16 +0100464static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
465 int source, unsigned int freq_in, unsigned int freq_out)
Mark Browne88ba012008-08-06 13:18:26 +0100466{
467 int offset;
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000468 struct snd_soc_component *component = codec_dai->component;
469 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Mark Browne88ba012008-08-06 13:18:26 +0100470 struct pll_state *state;
471 struct _pll_div pll_div;
472 unsigned int reg;
473 unsigned int pwr_mask;
474 int ret;
475
476 /* GCC isn't able to work out the ifs below for initialising/using
477 * pll_div so suppress warnings.
478 */
479 memset(&pll_div, 0, sizeof(pll_div));
480
481 switch (pll_id) {
482 case WM8580_PLLA:
483 state = &wm8580->a;
484 offset = 0;
485 pwr_mask = WM8580_PWRDN2_PLLAPD;
486 break;
487 case WM8580_PLLB:
488 state = &wm8580->b;
489 offset = 4;
490 pwr_mask = WM8580_PWRDN2_PLLBPD;
491 break;
492 default:
493 return -ENODEV;
494 }
495
496 if (freq_in && freq_out) {
497 ret = pll_factors(&pll_div, freq_out, freq_in);
498 if (ret != 0)
499 return ret;
500 }
501
502 state->in = freq_in;
503 state->out = freq_out;
504
505 /* Always disable the PLL - it is not safe to leave it running
506 * while reprogramming it.
507 */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000508 snd_soc_component_update_bits(component, WM8580_PWRDN2, pwr_mask, pwr_mask);
Mark Browne88ba012008-08-06 13:18:26 +0100509
510 if (!freq_in || !freq_out)
511 return 0;
512
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000513 snd_soc_component_write(component, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
514 snd_soc_component_write(component, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0x1ff);
515 snd_soc_component_write(component, WM8580_PLLA3 + offset,
Mark Browne88ba012008-08-06 13:18:26 +0100516 (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
517
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000518 reg = snd_soc_component_read32(component, WM8580_PLLA4 + offset);
jassi brar5c0d38c2009-09-01 11:35:08 +0900519 reg &= ~0x1b;
Mark Browne88ba012008-08-06 13:18:26 +0100520 reg |= pll_div.prescale | pll_div.postscale << 1 |
Mark Brownce881682009-04-21 12:35:15 +0100521 pll_div.freqmode << 3;
Mark Browne88ba012008-08-06 13:18:26 +0100522
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000523 snd_soc_component_write(component, WM8580_PLLA4 + offset, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100524
525 /* All done, turn it on */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000526 snd_soc_component_update_bits(component, WM8580_PWRDN2, pwr_mask, 0);
Mark Browne88ba012008-08-06 13:18:26 +0100527
528 return 0;
529}
530
Mark Brownc5607d82010-08-13 19:05:04 +0100531static const int wm8580_sysclk_ratios[] = {
532 128, 192, 256, 384, 512, 768, 1152,
533};
534
Mark Browne88ba012008-08-06 13:18:26 +0100535/*
536 * Set PCM DAI bit size and sample rate.
537 */
538static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000539 struct snd_pcm_hw_params *params,
540 struct snd_soc_dai *dai)
Mark Browne88ba012008-08-06 13:18:26 +0100541{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000542 struct snd_soc_component *component = dai->component;
543 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Mark Brownc5607d82010-08-13 19:05:04 +0100544 u16 paifa = 0;
Mark Browne231cab2010-08-13 17:57:28 +0100545 u16 paifb = 0;
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100546 int i, ratio, osr;
Mark Browne88ba012008-08-06 13:18:26 +0100547
Mark Browne88ba012008-08-06 13:18:26 +0100548 /* bit size */
Mark Brown993ba8e2014-07-31 12:51:10 +0100549 switch (params_width(params)) {
550 case 16:
Mark Brownba2772e2010-08-13 19:36:56 +0100551 paifa |= 0x8;
Mark Browne88ba012008-08-06 13:18:26 +0100552 break;
Mark Brown993ba8e2014-07-31 12:51:10 +0100553 case 20:
Jassi Brar6b464322010-12-07 19:23:07 +0900554 paifa |= 0x0;
Mark Browne88ba012008-08-06 13:18:26 +0100555 paifb |= WM8580_AIF_LENGTH_20;
556 break;
Mark Brown993ba8e2014-07-31 12:51:10 +0100557 case 24:
Jassi Brar6b464322010-12-07 19:23:07 +0900558 paifa |= 0x0;
Mark Browne88ba012008-08-06 13:18:26 +0100559 paifb |= WM8580_AIF_LENGTH_24;
560 break;
Mark Brown993ba8e2014-07-31 12:51:10 +0100561 case 32:
Jassi Brar6b464322010-12-07 19:23:07 +0900562 paifa |= 0x0;
563 paifb |= WM8580_AIF_LENGTH_32;
Mark Browne88ba012008-08-06 13:18:26 +0100564 break;
565 default:
566 return -EINVAL;
567 }
568
Mark Brownc5607d82010-08-13 19:05:04 +0100569 /* Look up the SYSCLK ratio; accept only exact matches */
Jassi Brar06286932010-12-03 10:34:25 +0900570 ratio = wm8580->sysclk[dai->driver->id] / params_rate(params);
Mark Brownc5607d82010-08-13 19:05:04 +0100571 for (i = 0; i < ARRAY_SIZE(wm8580_sysclk_ratios); i++)
572 if (ratio == wm8580_sysclk_ratios[i])
573 break;
574 if (i == ARRAY_SIZE(wm8580_sysclk_ratios)) {
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000575 dev_err(component->dev, "Invalid clock ratio %d/%d\n",
Jassi Brar06286932010-12-03 10:34:25 +0900576 wm8580->sysclk[dai->driver->id], params_rate(params));
Mark Brownc5607d82010-08-13 19:05:04 +0100577 return -EINVAL;
578 }
579 paifa |= i;
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000580 dev_dbg(component->dev, "Running at %dfs with %dHz clock\n",
Mark Brownc5607d82010-08-13 19:05:04 +0100581 wm8580_sysclk_ratios[i], wm8580->sysclk[dai->driver->id]);
582
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100583 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
584 switch (ratio) {
585 case 128:
586 case 192:
587 osr = WM8580_DACOSR;
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000588 dev_dbg(component->dev, "Selecting 64x OSR\n");
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100589 break;
590 default:
591 osr = 0;
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000592 dev_dbg(component->dev, "Selecting 128x OSR\n");
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100593 break;
594 }
595
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000596 snd_soc_component_update_bits(component, WM8580_PAIF3, WM8580_DACOSR, osr);
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100597 }
598
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000599 snd_soc_component_update_bits(component, WM8580_PAIF1 + dai->driver->id,
Mark Brownba2772e2010-08-13 19:36:56 +0100600 WM8580_AIF_RATE_MASK | WM8580_AIF_BCLKSEL_MASK,
601 paifa);
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000602 snd_soc_component_update_bits(component, WM8580_PAIF3 + dai->driver->id,
Mark Browne231cab2010-08-13 17:57:28 +0100603 WM8580_AIF_LENGTH_MASK, paifb);
Mark Browne88ba012008-08-06 13:18:26 +0100604 return 0;
605}
606
607static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
608 unsigned int fmt)
609{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000610 struct snd_soc_component *component = codec_dai->component;
Mark Browne88ba012008-08-06 13:18:26 +0100611 unsigned int aifa;
612 unsigned int aifb;
613 int can_invert_lrclk;
614
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000615 aifa = snd_soc_component_read32(component, WM8580_PAIF1 + codec_dai->driver->id);
616 aifb = snd_soc_component_read32(component, WM8580_PAIF3 + codec_dai->driver->id);
Mark Browne88ba012008-08-06 13:18:26 +0100617
618 aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
619
620 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
621 case SND_SOC_DAIFMT_CBS_CFS:
622 aifa &= ~WM8580_AIF_MS;
623 break;
624 case SND_SOC_DAIFMT_CBM_CFM:
625 aifa |= WM8580_AIF_MS;
626 break;
627 default:
628 return -EINVAL;
629 }
630
631 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
632 case SND_SOC_DAIFMT_I2S:
633 can_invert_lrclk = 1;
634 aifb |= WM8580_AIF_FMT_I2S;
635 break;
636 case SND_SOC_DAIFMT_RIGHT_J:
637 can_invert_lrclk = 1;
638 aifb |= WM8580_AIF_FMT_RIGHTJ;
639 break;
640 case SND_SOC_DAIFMT_LEFT_J:
641 can_invert_lrclk = 1;
642 aifb |= WM8580_AIF_FMT_LEFTJ;
643 break;
644 case SND_SOC_DAIFMT_DSP_A:
645 can_invert_lrclk = 0;
646 aifb |= WM8580_AIF_FMT_DSP;
647 break;
648 case SND_SOC_DAIFMT_DSP_B:
649 can_invert_lrclk = 0;
650 aifb |= WM8580_AIF_FMT_DSP;
651 aifb |= WM8580_AIF_LRP;
652 break;
653 default:
654 return -EINVAL;
655 }
656
657 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
658 case SND_SOC_DAIFMT_NB_NF:
659 break;
660
661 case SND_SOC_DAIFMT_IB_IF:
662 if (!can_invert_lrclk)
663 return -EINVAL;
664 aifb |= WM8580_AIF_BCP;
665 aifb |= WM8580_AIF_LRP;
666 break;
667
668 case SND_SOC_DAIFMT_IB_NF:
669 aifb |= WM8580_AIF_BCP;
670 break;
671
672 case SND_SOC_DAIFMT_NB_IF:
673 if (!can_invert_lrclk)
674 return -EINVAL;
675 aifb |= WM8580_AIF_LRP;
676 break;
677
678 default:
679 return -EINVAL;
680 }
681
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000682 snd_soc_component_write(component, WM8580_PAIF1 + codec_dai->driver->id, aifa);
683 snd_soc_component_write(component, WM8580_PAIF3 + codec_dai->driver->id, aifb);
Mark Browne88ba012008-08-06 13:18:26 +0100684
685 return 0;
686}
687
688static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
689 int div_id, int div)
690{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000691 struct snd_soc_component *component = codec_dai->component;
Mark Browne88ba012008-08-06 13:18:26 +0100692 unsigned int reg;
693
694 switch (div_id) {
695 case WM8580_MCLK:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000696 reg = snd_soc_component_read32(component, WM8580_PLLB4);
Mark Browne88ba012008-08-06 13:18:26 +0100697 reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
698
699 switch (div) {
700 case WM8580_CLKSRC_MCLK:
701 /* Input */
702 break;
703
704 case WM8580_CLKSRC_PLLA:
705 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
706 break;
707 case WM8580_CLKSRC_PLLB:
708 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
709 break;
710
711 case WM8580_CLKSRC_OSC:
712 reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
713 break;
714
715 default:
716 return -EINVAL;
717 }
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000718 snd_soc_component_write(component, WM8580_PLLB4, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100719 break;
720
Mark Browne88ba012008-08-06 13:18:26 +0100721 case WM8580_CLKOUTSRC:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000722 reg = snd_soc_component_read32(component, WM8580_PLLB4);
Mark Browne88ba012008-08-06 13:18:26 +0100723 reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
724
725 switch (div) {
726 case WM8580_CLKSRC_NONE:
727 break;
728
729 case WM8580_CLKSRC_PLLA:
730 reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
731 break;
732
733 case WM8580_CLKSRC_PLLB:
734 reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
735 break;
736
737 case WM8580_CLKSRC_OSC:
738 reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
739 break;
740
741 default:
742 return -EINVAL;
743 }
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000744 snd_soc_component_write(component, WM8580_PLLB4, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100745 break;
746
747 default:
748 return -EINVAL;
749 }
750
751 return 0;
752}
753
Mark Brownc5607d82010-08-13 19:05:04 +0100754static int wm8580_set_sysclk(struct snd_soc_dai *dai, int clk_id,
755 unsigned int freq, int dir)
756{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000757 struct snd_soc_component *component = dai->component;
758 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Axel Lin748b2172011-12-30 23:36:23 +0800759 int ret, sel, sel_mask, sel_shift;
Mark Brownc5607d82010-08-13 19:05:04 +0100760
761 switch (dai->driver->id) {
Mark Brownc25edef2010-08-16 19:26:41 +0100762 case WM8580_DAI_PAIFRX:
Mark Brownc5607d82010-08-13 19:05:04 +0100763 sel_mask = 0x3;
764 sel_shift = 0;
765 break;
766
Mark Brownc25edef2010-08-16 19:26:41 +0100767 case WM8580_DAI_PAIFTX:
Mark Brownc5607d82010-08-13 19:05:04 +0100768 sel_mask = 0xc;
769 sel_shift = 2;
770 break;
771
772 default:
Takashi Iwai4a9e0f92013-11-05 18:39:54 +0100773 WARN(1, "Unknown DAI driver ID\n");
Mark Brownc5607d82010-08-13 19:05:04 +0100774 return -EINVAL;
775 }
776
777 switch (clk_id) {
778 case WM8580_CLKSRC_ADCMCLK:
Jassi Brar06286932010-12-03 10:34:25 +0900779 if (dai->driver->id != WM8580_DAI_PAIFTX)
Mark Brownc5607d82010-08-13 19:05:04 +0100780 return -EINVAL;
781 sel = 0 << sel_shift;
782 break;
783 case WM8580_CLKSRC_PLLA:
784 sel = 1 << sel_shift;
785 break;
786 case WM8580_CLKSRC_PLLB:
787 sel = 2 << sel_shift;
788 break;
789 case WM8580_CLKSRC_MCLK:
790 sel = 3 << sel_shift;
791 break;
792 default:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000793 dev_err(component->dev, "Unknown clock %d\n", clk_id);
Mark Brownc5607d82010-08-13 19:05:04 +0100794 return -EINVAL;
795 }
796
797 /* We really should validate PLL settings but not yet */
Jassi Brar06286932010-12-03 10:34:25 +0900798 wm8580->sysclk[dai->driver->id] = freq;
Mark Brownc5607d82010-08-13 19:05:04 +0100799
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000800 ret = snd_soc_component_update_bits(component, WM8580_CLKSEL, sel_mask, sel);
Axel Lin748b2172011-12-30 23:36:23 +0800801 if (ret < 0)
802 return ret;
803
804 return 0;
Mark Brownc5607d82010-08-13 19:05:04 +0100805}
806
Mark Browne88ba012008-08-06 13:18:26 +0100807static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
808{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000809 struct snd_soc_component *component = codec_dai->component;
Mark Browne88ba012008-08-06 13:18:26 +0100810 unsigned int reg;
811
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000812 reg = snd_soc_component_read32(component, WM8580_DAC_CONTROL5);
Mark Browne88ba012008-08-06 13:18:26 +0100813
814 if (mute)
815 reg |= WM8580_DAC_CONTROL5_MUTEALL;
816 else
817 reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
818
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000819 snd_soc_component_write(component, WM8580_DAC_CONTROL5, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100820
821 return 0;
822}
823
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000824static int wm8580_set_bias_level(struct snd_soc_component *component,
Mark Browne88ba012008-08-06 13:18:26 +0100825 enum snd_soc_bias_level level)
826{
Mark Browne88ba012008-08-06 13:18:26 +0100827 switch (level) {
828 case SND_SOC_BIAS_ON:
829 case SND_SOC_BIAS_PREPARE:
Mark Browne88ba012008-08-06 13:18:26 +0100830 break;
Mark Brown6f7cb442009-03-11 18:31:08 +0000831
832 case SND_SOC_BIAS_STANDBY:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000833 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
Mark Brown6f7cb442009-03-11 18:31:08 +0000834 /* Power up and get individual control of the DACs */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000835 snd_soc_component_update_bits(component, WM8580_PWRDN1,
Axel Lina6785d72011-10-17 11:50:46 +0800836 WM8580_PWRDN1_PWDN |
837 WM8580_PWRDN1_ALLDACPD, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000838
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300839 /* Make VMID high impedance */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000840 snd_soc_component_update_bits(component, WM8580_ADC_CONTROL1,
Axel Lina6785d72011-10-17 11:50:46 +0800841 0x100, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000842 }
843 break;
844
Mark Browne88ba012008-08-06 13:18:26 +0100845 case SND_SOC_BIAS_OFF:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000846 snd_soc_component_update_bits(component, WM8580_PWRDN1,
Axel Lina6785d72011-10-17 11:50:46 +0800847 WM8580_PWRDN1_PWDN, WM8580_PWRDN1_PWDN);
Mark Browne88ba012008-08-06 13:18:26 +0100848 break;
849 }
Mark Browne88ba012008-08-06 13:18:26 +0100850 return 0;
851}
852
Matt Flax028f5a52016-10-20 10:29:14 +1100853static int wm8580_playback_startup(struct snd_pcm_substream *substream,
854 struct snd_soc_dai *dai)
855{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000856 struct snd_soc_component *component = dai->component;
857 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Matt Flax028f5a52016-10-20 10:29:14 +1100858
859 return snd_pcm_hw_constraint_minmax(substream->runtime,
860 SNDRV_PCM_HW_PARAM_CHANNELS, 1, wm8580->drvdata->num_dacs * 2);
861}
862
Mark Browne88ba012008-08-06 13:18:26 +0100863#define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
864 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
865
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100866static const struct snd_soc_dai_ops wm8580_dai_ops_playback = {
Matt Flax028f5a52016-10-20 10:29:14 +1100867 .startup = wm8580_playback_startup,
Mark Brownc5607d82010-08-13 19:05:04 +0100868 .set_sysclk = wm8580_set_sysclk,
Eric Miao6335d052009-03-03 09:41:00 +0800869 .hw_params = wm8580_paif_hw_params,
870 .set_fmt = wm8580_set_paif_dai_fmt,
871 .set_clkdiv = wm8580_set_dai_clkdiv,
872 .set_pll = wm8580_set_dai_pll,
873 .digital_mute = wm8580_digital_mute,
874};
875
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100876static const struct snd_soc_dai_ops wm8580_dai_ops_capture = {
Mark Brownc5607d82010-08-13 19:05:04 +0100877 .set_sysclk = wm8580_set_sysclk,
Eric Miao6335d052009-03-03 09:41:00 +0800878 .hw_params = wm8580_paif_hw_params,
879 .set_fmt = wm8580_set_paif_dai_fmt,
880 .set_clkdiv = wm8580_set_dai_clkdiv,
881 .set_pll = wm8580_set_dai_pll,
882};
883
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884static struct snd_soc_dai_driver wm8580_dai[] = {
Mark Browne88ba012008-08-06 13:18:26 +0100885 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 .name = "wm8580-hifi-playback",
887 .id = WM8580_DAI_PAIFRX,
Mark Browne88ba012008-08-06 13:18:26 +0100888 .playback = {
889 .stream_name = "Playback",
890 .channels_min = 1,
Mark Browne88ba012008-08-06 13:18:26 +0100891 .rates = SNDRV_PCM_RATE_8000_192000,
892 .formats = WM8580_FORMATS,
893 },
Eric Miao6335d052009-03-03 09:41:00 +0800894 .ops = &wm8580_dai_ops_playback,
Mark Browne88ba012008-08-06 13:18:26 +0100895 },
896 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 .name = "wm8580-hifi-capture",
898 .id = WM8580_DAI_PAIFTX,
Mark Browne88ba012008-08-06 13:18:26 +0100899 .capture = {
900 .stream_name = "Capture",
901 .channels_min = 2,
902 .channels_max = 2,
903 .rates = SNDRV_PCM_RATE_8000_192000,
904 .formats = WM8580_FORMATS,
905 },
Eric Miao6335d052009-03-03 09:41:00 +0800906 .ops = &wm8580_dai_ops_capture,
Mark Browne88ba012008-08-06 13:18:26 +0100907 },
908};
Mark Browne88ba012008-08-06 13:18:26 +0100909
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000910static int wm8580_probe(struct snd_soc_component *component)
Mark Browne88ba012008-08-06 13:18:26 +0100911{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000912 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
913 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown18273b02012-09-12 12:03:51 +0800914 int ret = 0;
Mark Browne88ba012008-08-06 13:18:26 +0100915
Matt Flax028f5a52016-10-20 10:29:14 +1100916 switch (wm8580->drvdata->num_dacs) {
917 case 4:
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000918 snd_soc_add_component_controls(component, wm8581_snd_controls,
Matt Flax028f5a52016-10-20 10:29:14 +1100919 ARRAY_SIZE(wm8581_snd_controls));
920 snd_soc_dapm_new_controls(dapm, wm8581_dapm_widgets,
921 ARRAY_SIZE(wm8581_dapm_widgets));
922 snd_soc_dapm_add_routes(dapm, wm8581_dapm_routes,
923 ARRAY_SIZE(wm8581_dapm_routes));
924 break;
925 default:
926 break;
927 }
928
Mark Browna583cd52009-06-17 17:30:14 +0100929 ret = regulator_bulk_enable(ARRAY_SIZE(wm8580->supplies),
930 wm8580->supplies);
931 if (ret != 0) {
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000932 dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
Mark Browna583cd52009-06-17 17:30:14 +0100933 goto err_regulator_get;
934 }
935
Mark Brown6f7cb442009-03-11 18:31:08 +0000936 /* Get the codec into a known state */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000937 ret = snd_soc_component_write(component, WM8580_RESET, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000938 if (ret != 0) {
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000939 dev_err(component->dev, "Failed to reset component: %d\n", ret);
Mark Browna583cd52009-06-17 17:30:14 +0100940 goto err_regulator_enable;
Mark Brown6f7cb442009-03-11 18:31:08 +0000941 }
942
Mark Brown6f7cb442009-03-11 18:31:08 +0000943 return 0;
944
Mark Browna583cd52009-06-17 17:30:14 +0100945err_regulator_enable:
946 regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
947err_regulator_get:
Mark Brown6f7cb442009-03-11 18:31:08 +0000948 return ret;
949}
950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951/* power down chip */
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000952static void wm8580_remove(struct snd_soc_component *component)
Mark Brown6f7cb442009-03-11 18:31:08 +0000953{
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000954 struct wm8580_priv *wm8580 = snd_soc_component_get_drvdata(component);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955
Mark Browna583cd52009-06-17 17:30:14 +0100956 regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
Mark Brown6f7cb442009-03-11 18:31:08 +0000957}
Mark Browne88ba012008-08-06 13:18:26 +0100958
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +0000959static const struct snd_soc_component_driver soc_component_dev_wm8580 = {
960 .probe = wm8580_probe,
961 .remove = wm8580_remove,
962 .set_bias_level = wm8580_set_bias_level,
963 .controls = wm8580_snd_controls,
964 .num_controls = ARRAY_SIZE(wm8580_snd_controls),
965 .dapm_widgets = wm8580_dapm_widgets,
966 .num_dapm_widgets = ARRAY_SIZE(wm8580_dapm_widgets),
967 .dapm_routes = wm8580_dapm_routes,
968 .num_dapm_routes = ARRAY_SIZE(wm8580_dapm_routes),
969 .idle_bias_on = 1,
970 .use_pmdown_time = 1,
971 .endianness = 1,
972 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973};
974
Mark Brownb689d9f2012-09-12 11:59:51 +0800975static const struct regmap_config wm8580_regmap = {
976 .reg_bits = 7,
977 .val_bits = 9,
978 .max_register = WM8580_MAX_REGISTER,
979
980 .reg_defaults = wm8580_reg_defaults,
981 .num_reg_defaults = ARRAY_SIZE(wm8580_reg_defaults),
982 .cache_type = REGCACHE_RBTREE,
983
984 .volatile_reg = wm8580_volatile,
985};
986
Wei Yongjun2f3d1b62016-10-29 16:27:23 +0000987static const struct wm8580_driver_data wm8580_data = {
Matt Flax028f5a52016-10-20 10:29:14 +1100988 .num_dacs = 3,
989};
990
Wei Yongjun2f3d1b62016-10-29 16:27:23 +0000991static const struct wm8580_driver_data wm8581_data = {
Matt Flax028f5a52016-10-20 10:29:14 +1100992 .num_dacs = 4,
993};
994
995static const struct of_device_id wm8580_of_match[] = {
996 { .compatible = "wlf,wm8580", .data = &wm8580_data },
997 { .compatible = "wlf,wm8581", .data = &wm8581_data },
998 { },
999};
1000MODULE_DEVICE_TABLE(of, wm8580_of_match);
1001
Jean Delvare17be5522008-10-15 19:57:12 +02001002static int wm8580_i2c_probe(struct i2c_client *i2c,
1003 const struct i2c_device_id *id)
Mark Browne88ba012008-08-06 13:18:26 +01001004{
Matt Flax028f5a52016-10-20 10:29:14 +11001005 const struct of_device_id *of_id;
Mark Brown6f7cb442009-03-11 18:31:08 +00001006 struct wm8580_priv *wm8580;
Mark Brown18273b02012-09-12 12:03:51 +08001007 int ret, i;
Mark Browne88ba012008-08-06 13:18:26 +01001008
Mark Brown398c02f2012-09-12 09:25:59 +08001009 wm8580 = devm_kzalloc(&i2c->dev, sizeof(struct wm8580_priv),
1010 GFP_KERNEL);
Mark Brown6f7cb442009-03-11 18:31:08 +00001011 if (wm8580 == NULL)
1012 return -ENOMEM;
1013
Mark Brownb689d9f2012-09-12 11:59:51 +08001014 wm8580->regmap = devm_regmap_init_i2c(i2c, &wm8580_regmap);
1015 if (IS_ERR(wm8580->regmap))
1016 return PTR_ERR(wm8580->regmap);
1017
Mark Brown18273b02012-09-12 12:03:51 +08001018 for (i = 0; i < ARRAY_SIZE(wm8580->supplies); i++)
1019 wm8580->supplies[i].supply = wm8580_supply_names[i];
1020
1021 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8580->supplies),
1022 wm8580->supplies);
1023 if (ret != 0) {
1024 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
1025 return ret;
1026 }
1027
Mark Brown6f7cb442009-03-11 18:31:08 +00001028 i2c_set_clientdata(i2c, wm8580);
Mark Browne88ba012008-08-06 13:18:26 +01001029
Matt Flax028f5a52016-10-20 10:29:14 +11001030 of_id = of_match_device(wm8580_of_match, &i2c->dev);
1031 if (of_id)
1032 wm8580->drvdata = of_id->data;
1033
1034 if (!wm8580->drvdata) {
1035 dev_err(&i2c->dev, "failed to find driver data\n");
1036 return -EINVAL;
1037 }
1038
Kuninori Morimoto8f5a5932018-01-29 03:00:20 +00001039 ret = devm_snd_soc_register_component(&i2c->dev,
1040 &soc_component_dev_wm8580, wm8580_dai, ARRAY_SIZE(wm8580_dai));
Mark Brown398c02f2012-09-12 09:25:59 +08001041
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042 return ret;
Mark Browne88ba012008-08-06 13:18:26 +01001043}
1044
Jean Delvare17be5522008-10-15 19:57:12 +02001045static const struct i2c_device_id wm8580_i2c_id[] = {
Matt Flax028f5a52016-10-20 10:29:14 +11001046 { "wm8580", (kernel_ulong_t)&wm8580_data },
1047 { "wm8581", (kernel_ulong_t)&wm8581_data },
Jean Delvare17be5522008-10-15 19:57:12 +02001048 { }
1049};
1050MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
Mark Browne88ba012008-08-06 13:18:26 +01001051
Mark Browne88ba012008-08-06 13:18:26 +01001052static struct i2c_driver wm8580_i2c_driver = {
1053 .driver = {
Mark Brown722d0da2011-08-02 13:21:53 +09001054 .name = "wm8580",
Mark Brown733eef32011-08-02 13:22:36 +09001055 .of_match_table = wm8580_of_match,
Mark Browne88ba012008-08-06 13:18:26 +01001056 },
Jean Delvare17be5522008-10-15 19:57:12 +02001057 .probe = wm8580_i2c_probe,
Jean Delvare17be5522008-10-15 19:57:12 +02001058 .id_table = wm8580_i2c_id,
Mark Browne88ba012008-08-06 13:18:26 +01001059};
Mark Browne88ba012008-08-06 13:18:26 +01001060
Kuninori Morimoto60bc6172016-11-17 01:13:18 +00001061module_i2c_driver(wm8580_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +00001062
Mark Browne88ba012008-08-06 13:18:26 +01001063MODULE_DESCRIPTION("ASoC WM8580 driver");
1064MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
Matt Flax028f5a52016-10-20 10:29:14 +11001065MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
Mark Browne88ba012008-08-06 13:18:26 +01001066MODULE_LICENSE("GPL");