blob: 7c68226376e415a5e058bb10153531b68d49c990 [file] [log] [blame]
Mark Browne88ba012008-08-06 13:18:26 +01001/*
2 * wm8580.c -- WM8580 ALSA Soc Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2008-11 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 *
15 * Currently only the primary audio interface is supported - S/PDIF and
16 * the secondary audio interfaces are not.
17 */
18
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Mark Browne88ba012008-08-06 13:18:26 +010021#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/i2c.h>
Mark Browna583cd52009-06-17 17:30:14 +010026#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Mark Brown733eef32011-08-02 13:22:36 +090028#include <linux/of_device.h>
Mark Browna583cd52009-06-17 17:30:14 +010029
Mark Browne88ba012008-08-06 13:18:26 +010030#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
Mark Browne88ba012008-08-06 13:18:26 +010034#include <sound/tlv.h>
35#include <sound/initval.h>
36#include <asm/div64.h>
37
38#include "wm8580.h"
39
Mark Browne88ba012008-08-06 13:18:26 +010040/* WM8580 register space */
41#define WM8580_PLLA1 0x00
42#define WM8580_PLLA2 0x01
43#define WM8580_PLLA3 0x02
44#define WM8580_PLLA4 0x03
45#define WM8580_PLLB1 0x04
46#define WM8580_PLLB2 0x05
47#define WM8580_PLLB3 0x06
48#define WM8580_PLLB4 0x07
49#define WM8580_CLKSEL 0x08
50#define WM8580_PAIF1 0x09
51#define WM8580_PAIF2 0x0A
52#define WM8580_SAIF1 0x0B
53#define WM8580_PAIF3 0x0C
54#define WM8580_PAIF4 0x0D
55#define WM8580_SAIF2 0x0E
56#define WM8580_DAC_CONTROL1 0x0F
57#define WM8580_DAC_CONTROL2 0x10
58#define WM8580_DAC_CONTROL3 0x11
59#define WM8580_DAC_CONTROL4 0x12
60#define WM8580_DAC_CONTROL5 0x13
61#define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
62#define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
63#define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
64#define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
65#define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
66#define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
67#define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
68#define WM8580_ADC_CONTROL1 0x1D
69#define WM8580_SPDTXCHAN0 0x1E
70#define WM8580_SPDTXCHAN1 0x1F
71#define WM8580_SPDTXCHAN2 0x20
72#define WM8580_SPDTXCHAN3 0x21
73#define WM8580_SPDTXCHAN4 0x22
74#define WM8580_SPDTXCHAN5 0x23
75#define WM8580_SPDMODE 0x24
76#define WM8580_INTMASK 0x25
77#define WM8580_GPO1 0x26
78#define WM8580_GPO2 0x27
79#define WM8580_GPO3 0x28
80#define WM8580_GPO4 0x29
81#define WM8580_GPO5 0x2A
82#define WM8580_INTSTAT 0x2B
83#define WM8580_SPDRXCHAN1 0x2C
84#define WM8580_SPDRXCHAN2 0x2D
85#define WM8580_SPDRXCHAN3 0x2E
86#define WM8580_SPDRXCHAN4 0x2F
87#define WM8580_SPDRXCHAN5 0x30
88#define WM8580_SPDSTAT 0x31
89#define WM8580_PWRDN1 0x32
90#define WM8580_PWRDN2 0x33
91#define WM8580_READBACK 0x34
92#define WM8580_RESET 0x35
93
Mark Brown6f7cb442009-03-11 18:31:08 +000094#define WM8580_MAX_REGISTER 0x35
95
Mark Brown6bfb6aa2010-08-13 20:08:55 +010096#define WM8580_DACOSR 0x40
97
Mark Browne88ba012008-08-06 13:18:26 +010098/* PLLB4 (register 7h) */
99#define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
100#define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
101#define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
102#define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
103
104#define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
105#define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
106#define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
107#define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
108
109/* CLKSEL (register 8h) */
110#define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
111#define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
112#define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
113
114/* AIF control 1 (registers 9h-bh) */
115#define WM8580_AIF_RATE_MASK 0x7
Mark Browne88ba012008-08-06 13:18:26 +0100116#define WM8580_AIF_BCLKSEL_MASK 0x18
Mark Browne88ba012008-08-06 13:18:26 +0100117
118#define WM8580_AIF_MS 0x20
119
120#define WM8580_AIF_CLKSRC_MASK 0xc0
121#define WM8580_AIF_CLKSRC_PLLA 0x40
122#define WM8580_AIF_CLKSRC_PLLB 0x40
123#define WM8580_AIF_CLKSRC_MCLK 0xc0
124
125/* AIF control 2 (registers ch-eh) */
126#define WM8580_AIF_FMT_MASK 0x03
127#define WM8580_AIF_FMT_RIGHTJ 0x00
128#define WM8580_AIF_FMT_LEFTJ 0x01
129#define WM8580_AIF_FMT_I2S 0x02
130#define WM8580_AIF_FMT_DSP 0x03
131
132#define WM8580_AIF_LENGTH_MASK 0x0c
133#define WM8580_AIF_LENGTH_16 0x00
134#define WM8580_AIF_LENGTH_20 0x04
135#define WM8580_AIF_LENGTH_24 0x08
136#define WM8580_AIF_LENGTH_32 0x0c
137
138#define WM8580_AIF_LRP 0x10
139#define WM8580_AIF_BCP 0x20
140
141/* Powerdown Register 1 (register 32h) */
142#define WM8580_PWRDN1_PWDN 0x001
143#define WM8580_PWRDN1_ALLDACPD 0x040
144
145/* Powerdown Register 2 (register 33h) */
146#define WM8580_PWRDN2_OSSCPD 0x001
147#define WM8580_PWRDN2_PLLAPD 0x002
148#define WM8580_PWRDN2_PLLBPD 0x004
149#define WM8580_PWRDN2_SPDIFPD 0x008
150#define WM8580_PWRDN2_SPDIFTXD 0x010
151#define WM8580_PWRDN2_SPDIFRXD 0x020
152
153#define WM8580_DAC_CONTROL5_MUTEALL 0x10
154
155/*
156 * wm8580 register cache
157 * We can't read the WM8580 register space when we
158 * are using 2 wire for device control, so we cache them instead.
159 */
160static const u16 wm8580_reg[] = {
161 0x0121, 0x017e, 0x007d, 0x0014, /*R3*/
162 0x0121, 0x017e, 0x007d, 0x0194, /*R7*/
Seungwhan Youna0968622010-12-09 18:07:52 +0900163 0x0010, 0x0002, 0x0002, 0x00c2, /*R11*/
Mark Browne88ba012008-08-06 13:18:26 +0100164 0x0182, 0x0082, 0x000a, 0x0024, /*R15*/
165 0x0009, 0x0000, 0x00ff, 0x0000, /*R19*/
166 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R23*/
167 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R27*/
168 0x01f0, 0x0040, 0x0000, 0x0000, /*R31(0x1F)*/
169 0x0000, 0x0000, 0x0031, 0x000b, /*R35*/
170 0x0039, 0x0000, 0x0010, 0x0032, /*R39*/
171 0x0054, 0x0076, 0x0098, 0x0000, /*R43(0x2B)*/
172 0x0000, 0x0000, 0x0000, 0x0000, /*R47*/
173 0x0000, 0x0000, 0x005e, 0x003e, /*R51(0x33)*/
174 0x0000, 0x0000 /*R53*/
175};
176
Mark Brown6f7cb442009-03-11 18:31:08 +0000177struct pll_state {
178 unsigned int in;
179 unsigned int out;
180};
181
Mark Browna583cd52009-06-17 17:30:14 +0100182#define WM8580_NUM_SUPPLIES 3
183static const char *wm8580_supply_names[WM8580_NUM_SUPPLIES] = {
184 "AVDD",
185 "DVDD",
186 "PVDD",
187};
188
Mark Brown6f7cb442009-03-11 18:31:08 +0000189/* codec private data */
190struct wm8580_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000191 enum snd_soc_control_type control_type;
Mark Browna583cd52009-06-17 17:30:14 +0100192 struct regulator_bulk_data supplies[WM8580_NUM_SUPPLIES];
Mark Brown6f7cb442009-03-11 18:31:08 +0000193 struct pll_state a;
194 struct pll_state b;
Mark Brownc5607d82010-08-13 19:05:04 +0100195 int sysclk[2];
Mark Brown6f7cb442009-03-11 18:31:08 +0000196};
197
Mark Browne88ba012008-08-06 13:18:26 +0100198static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
199
200static int wm8580_out_vu(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
202{
Mark Brownccb077f2009-04-21 12:57:00 +0100203 struct soc_mixer_control *mc =
204 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne88ba012008-08-06 13:18:26 +0100205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf6f1eb12009-07-05 17:57:57 +0100206 u16 *reg_cache = codec->reg_cache;
Mark Brownccb077f2009-04-21 12:57:00 +0100207 unsigned int reg = mc->reg;
208 unsigned int reg2 = mc->rreg;
Mark Browne88ba012008-08-06 13:18:26 +0100209 int ret;
Mark Browne88ba012008-08-06 13:18:26 +0100210
211 /* Clear the register cache so we write without VU set */
Mark Brownf6f1eb12009-07-05 17:57:57 +0100212 reg_cache[reg] = 0;
213 reg_cache[reg2] = 0;
Mark Browne88ba012008-08-06 13:18:26 +0100214
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300215 ret = snd_soc_put_volsw(kcontrol, ucontrol);
Mark Browne88ba012008-08-06 13:18:26 +0100216 if (ret < 0)
217 return ret;
218
219 /* Now write again with the volume update bit set */
Mark Brownf6f1eb12009-07-05 17:57:57 +0100220 snd_soc_update_bits(codec, reg, 0x100, 0x100);
221 snd_soc_update_bits(codec, reg2, 0x100, 0x100);
Mark Browne88ba012008-08-06 13:18:26 +0100222
223 return 0;
224}
225
Mark Browne88ba012008-08-06 13:18:26 +0100226static const struct snd_kcontrol_new wm8580_snd_controls[] = {
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300227SOC_DOUBLE_R_EXT_TLV("DAC1 Playback Volume",
228 WM8580_DIGITAL_ATTENUATION_DACL1,
229 WM8580_DIGITAL_ATTENUATION_DACR1,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300230 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300231SOC_DOUBLE_R_EXT_TLV("DAC2 Playback Volume",
232 WM8580_DIGITAL_ATTENUATION_DACL2,
233 WM8580_DIGITAL_ATTENUATION_DACR2,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300234 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300235SOC_DOUBLE_R_EXT_TLV("DAC3 Playback Volume",
236 WM8580_DIGITAL_ATTENUATION_DACL3,
237 WM8580_DIGITAL_ATTENUATION_DACR3,
Peter Ujfalusic4671a92011-10-06 09:59:12 +0300238 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
Mark Browne88ba012008-08-06 13:18:26 +0100239
240SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
241SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
242SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
243
244SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
245SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
246SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
247
248SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
Mark Brown4f0ed9a2010-08-06 17:57:12 +0100249SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 1),
250SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 1),
251SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 1),
Mark Browne88ba012008-08-06 13:18:26 +0100252
Mark Browndacfe9f2010-08-13 20:01:32 +0100253SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 1),
Mark Brown6f341d142010-08-06 18:14:09 +0100254SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
Mark Browne88ba012008-08-06 13:18:26 +0100255};
256
Mark Browne88ba012008-08-06 13:18:26 +0100257static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
258SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
259SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
260SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
261
262SND_SOC_DAPM_OUTPUT("VOUT1L"),
263SND_SOC_DAPM_OUTPUT("VOUT1R"),
264SND_SOC_DAPM_OUTPUT("VOUT2L"),
265SND_SOC_DAPM_OUTPUT("VOUT2R"),
266SND_SOC_DAPM_OUTPUT("VOUT3L"),
267SND_SOC_DAPM_OUTPUT("VOUT3R"),
268
269SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
270
271SND_SOC_DAPM_INPUT("AINL"),
272SND_SOC_DAPM_INPUT("AINR"),
273};
274
Mark Brownf235c642011-12-03 11:42:01 +0000275static const struct snd_soc_dapm_route wm8580_dapm_routes[] = {
Mark Browne88ba012008-08-06 13:18:26 +0100276 { "VOUT1L", NULL, "DAC1" },
277 { "VOUT1R", NULL, "DAC1" },
278
279 { "VOUT2L", NULL, "DAC2" },
280 { "VOUT2R", NULL, "DAC2" },
281
282 { "VOUT3L", NULL, "DAC3" },
283 { "VOUT3R", NULL, "DAC3" },
284
285 { "ADC", NULL, "AINL" },
286 { "ADC", NULL, "AINR" },
287};
288
Mark Browne88ba012008-08-06 13:18:26 +0100289/* PLL divisors */
290struct _pll_div {
291 u32 prescale:1;
292 u32 postscale:1;
293 u32 freqmode:2;
294 u32 n:4;
295 u32 k:24;
296};
297
298/* The size in bits of the pll divide */
299#define FIXED_PLL_SIZE (1 << 22)
300
301/* PLL rate to output rate divisions */
302static struct {
303 unsigned int div;
304 unsigned int freqmode;
305 unsigned int postscale;
306} post_table[] = {
307 { 2, 0, 0 },
308 { 4, 0, 1 },
309 { 4, 1, 0 },
310 { 8, 1, 1 },
311 { 8, 2, 0 },
312 { 16, 2, 1 },
313 { 12, 3, 0 },
314 { 24, 3, 1 }
315};
316
317static int pll_factors(struct _pll_div *pll_div, unsigned int target,
318 unsigned int source)
319{
320 u64 Kpart;
321 unsigned int K, Ndiv, Nmod;
322 int i;
323
Roel Kluin449bd542009-05-27 17:08:39 -0700324 pr_debug("wm8580: PLL %uHz->%uHz\n", source, target);
Mark Browne88ba012008-08-06 13:18:26 +0100325
326 /* Scale the output frequency up; the PLL should run in the
327 * region of 90-100MHz.
328 */
329 for (i = 0; i < ARRAY_SIZE(post_table); i++) {
330 if (target * post_table[i].div >= 90000000 &&
331 target * post_table[i].div <= 100000000) {
332 pll_div->freqmode = post_table[i].freqmode;
333 pll_div->postscale = post_table[i].postscale;
334 target *= post_table[i].div;
335 break;
336 }
337 }
338
339 if (i == ARRAY_SIZE(post_table)) {
340 printk(KERN_ERR "wm8580: Unable to scale output frequency "
341 "%u\n", target);
342 return -EINVAL;
343 }
344
345 Ndiv = target / source;
346
347 if (Ndiv < 5) {
348 source /= 2;
349 pll_div->prescale = 1;
350 Ndiv = target / source;
351 } else
352 pll_div->prescale = 0;
353
354 if ((Ndiv < 5) || (Ndiv > 13)) {
355 printk(KERN_ERR
Roel Kluin449bd542009-05-27 17:08:39 -0700356 "WM8580 N=%u outside supported range\n", Ndiv);
Mark Browne88ba012008-08-06 13:18:26 +0100357 return -EINVAL;
358 }
359
360 pll_div->n = Ndiv;
361 Nmod = target % source;
362 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
363
364 do_div(Kpart, source);
365
366 K = Kpart & 0xFFFFFFFF;
367
368 pll_div->k = K;
369
370 pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
371 pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
372 pll_div->postscale);
373
374 return 0;
375}
376
Mark Brown85488032009-09-05 18:52:16 +0100377static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
378 int source, unsigned int freq_in, unsigned int freq_out)
Mark Browne88ba012008-08-06 13:18:26 +0100379{
380 int offset;
381 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900382 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
Mark Browne88ba012008-08-06 13:18:26 +0100383 struct pll_state *state;
384 struct _pll_div pll_div;
385 unsigned int reg;
386 unsigned int pwr_mask;
387 int ret;
388
389 /* GCC isn't able to work out the ifs below for initialising/using
390 * pll_div so suppress warnings.
391 */
392 memset(&pll_div, 0, sizeof(pll_div));
393
394 switch (pll_id) {
395 case WM8580_PLLA:
396 state = &wm8580->a;
397 offset = 0;
398 pwr_mask = WM8580_PWRDN2_PLLAPD;
399 break;
400 case WM8580_PLLB:
401 state = &wm8580->b;
402 offset = 4;
403 pwr_mask = WM8580_PWRDN2_PLLBPD;
404 break;
405 default:
406 return -ENODEV;
407 }
408
409 if (freq_in && freq_out) {
410 ret = pll_factors(&pll_div, freq_out, freq_in);
411 if (ret != 0)
412 return ret;
413 }
414
415 state->in = freq_in;
416 state->out = freq_out;
417
418 /* Always disable the PLL - it is not safe to leave it running
419 * while reprogramming it.
420 */
Axel Lina6785d72011-10-17 11:50:46 +0800421 snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, pwr_mask);
Mark Browne88ba012008-08-06 13:18:26 +0100422
423 if (!freq_in || !freq_out)
424 return 0;
425
Mark Brownf6f1eb12009-07-05 17:57:57 +0100426 snd_soc_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
jassi brar5c0d38c2009-09-01 11:35:08 +0900427 snd_soc_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0x1ff);
Mark Brownf6f1eb12009-07-05 17:57:57 +0100428 snd_soc_write(codec, WM8580_PLLA3 + offset,
Mark Browne88ba012008-08-06 13:18:26 +0100429 (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
430
Mark Brownf6f1eb12009-07-05 17:57:57 +0100431 reg = snd_soc_read(codec, WM8580_PLLA4 + offset);
jassi brar5c0d38c2009-09-01 11:35:08 +0900432 reg &= ~0x1b;
Mark Browne88ba012008-08-06 13:18:26 +0100433 reg |= pll_div.prescale | pll_div.postscale << 1 |
Mark Brownce881682009-04-21 12:35:15 +0100434 pll_div.freqmode << 3;
Mark Browne88ba012008-08-06 13:18:26 +0100435
Mark Brownf6f1eb12009-07-05 17:57:57 +0100436 snd_soc_write(codec, WM8580_PLLA4 + offset, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100437
438 /* All done, turn it on */
Axel Lina6785d72011-10-17 11:50:46 +0800439 snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, 0);
Mark Browne88ba012008-08-06 13:18:26 +0100440
441 return 0;
442}
443
Mark Brownc5607d82010-08-13 19:05:04 +0100444static const int wm8580_sysclk_ratios[] = {
445 128, 192, 256, 384, 512, 768, 1152,
446};
447
Mark Browne88ba012008-08-06 13:18:26 +0100448/*
449 * Set PCM DAI bit size and sample rate.
450 */
451static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000452 struct snd_pcm_hw_params *params,
453 struct snd_soc_dai *dai)
Mark Browne88ba012008-08-06 13:18:26 +0100454{
455 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 struct snd_soc_codec *codec = rtd->codec;
Mark Brownc5607d82010-08-13 19:05:04 +0100457 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
458 u16 paifa = 0;
Mark Browne231cab2010-08-13 17:57:28 +0100459 u16 paifb = 0;
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100460 int i, ratio, osr;
Mark Browne88ba012008-08-06 13:18:26 +0100461
Mark Browne88ba012008-08-06 13:18:26 +0100462 /* bit size */
463 switch (params_format(params)) {
464 case SNDRV_PCM_FORMAT_S16_LE:
Mark Brownba2772e2010-08-13 19:36:56 +0100465 paifa |= 0x8;
Mark Browne88ba012008-08-06 13:18:26 +0100466 break;
467 case SNDRV_PCM_FORMAT_S20_3LE:
Jassi Brar6b464322010-12-07 19:23:07 +0900468 paifa |= 0x0;
Mark Browne88ba012008-08-06 13:18:26 +0100469 paifb |= WM8580_AIF_LENGTH_20;
470 break;
471 case SNDRV_PCM_FORMAT_S24_LE:
Jassi Brar6b464322010-12-07 19:23:07 +0900472 paifa |= 0x0;
Mark Browne88ba012008-08-06 13:18:26 +0100473 paifb |= WM8580_AIF_LENGTH_24;
474 break;
475 case SNDRV_PCM_FORMAT_S32_LE:
Jassi Brar6b464322010-12-07 19:23:07 +0900476 paifa |= 0x0;
477 paifb |= WM8580_AIF_LENGTH_32;
Mark Browne88ba012008-08-06 13:18:26 +0100478 break;
479 default:
480 return -EINVAL;
481 }
482
Mark Brownc5607d82010-08-13 19:05:04 +0100483 /* Look up the SYSCLK ratio; accept only exact matches */
Jassi Brar06286932010-12-03 10:34:25 +0900484 ratio = wm8580->sysclk[dai->driver->id] / params_rate(params);
Mark Brownc5607d82010-08-13 19:05:04 +0100485 for (i = 0; i < ARRAY_SIZE(wm8580_sysclk_ratios); i++)
486 if (ratio == wm8580_sysclk_ratios[i])
487 break;
488 if (i == ARRAY_SIZE(wm8580_sysclk_ratios)) {
489 dev_err(codec->dev, "Invalid clock ratio %d/%d\n",
Jassi Brar06286932010-12-03 10:34:25 +0900490 wm8580->sysclk[dai->driver->id], params_rate(params));
Mark Brownc5607d82010-08-13 19:05:04 +0100491 return -EINVAL;
492 }
493 paifa |= i;
494 dev_dbg(codec->dev, "Running at %dfs with %dHz clock\n",
495 wm8580_sysclk_ratios[i], wm8580->sysclk[dai->driver->id]);
496
Mark Brown6bfb6aa2010-08-13 20:08:55 +0100497 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
498 switch (ratio) {
499 case 128:
500 case 192:
501 osr = WM8580_DACOSR;
502 dev_dbg(codec->dev, "Selecting 64x OSR\n");
503 break;
504 default:
505 osr = 0;
506 dev_dbg(codec->dev, "Selecting 128x OSR\n");
507 break;
508 }
509
510 snd_soc_update_bits(codec, WM8580_PAIF3, WM8580_DACOSR, osr);
511 }
512
Mark Brownc5607d82010-08-13 19:05:04 +0100513 snd_soc_update_bits(codec, WM8580_PAIF1 + dai->driver->id,
Mark Brownba2772e2010-08-13 19:36:56 +0100514 WM8580_AIF_RATE_MASK | WM8580_AIF_BCLKSEL_MASK,
515 paifa);
Mark Browne231cab2010-08-13 17:57:28 +0100516 snd_soc_update_bits(codec, WM8580_PAIF3 + dai->driver->id,
517 WM8580_AIF_LENGTH_MASK, paifb);
Mark Browne88ba012008-08-06 13:18:26 +0100518 return 0;
519}
520
521static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
522 unsigned int fmt)
523{
524 struct snd_soc_codec *codec = codec_dai->codec;
525 unsigned int aifa;
526 unsigned int aifb;
527 int can_invert_lrclk;
528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 aifa = snd_soc_read(codec, WM8580_PAIF1 + codec_dai->driver->id);
530 aifb = snd_soc_read(codec, WM8580_PAIF3 + codec_dai->driver->id);
Mark Browne88ba012008-08-06 13:18:26 +0100531
532 aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
533
534 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
535 case SND_SOC_DAIFMT_CBS_CFS:
536 aifa &= ~WM8580_AIF_MS;
537 break;
538 case SND_SOC_DAIFMT_CBM_CFM:
539 aifa |= WM8580_AIF_MS;
540 break;
541 default:
542 return -EINVAL;
543 }
544
545 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
546 case SND_SOC_DAIFMT_I2S:
547 can_invert_lrclk = 1;
548 aifb |= WM8580_AIF_FMT_I2S;
549 break;
550 case SND_SOC_DAIFMT_RIGHT_J:
551 can_invert_lrclk = 1;
552 aifb |= WM8580_AIF_FMT_RIGHTJ;
553 break;
554 case SND_SOC_DAIFMT_LEFT_J:
555 can_invert_lrclk = 1;
556 aifb |= WM8580_AIF_FMT_LEFTJ;
557 break;
558 case SND_SOC_DAIFMT_DSP_A:
559 can_invert_lrclk = 0;
560 aifb |= WM8580_AIF_FMT_DSP;
561 break;
562 case SND_SOC_DAIFMT_DSP_B:
563 can_invert_lrclk = 0;
564 aifb |= WM8580_AIF_FMT_DSP;
565 aifb |= WM8580_AIF_LRP;
566 break;
567 default:
568 return -EINVAL;
569 }
570
571 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
572 case SND_SOC_DAIFMT_NB_NF:
573 break;
574
575 case SND_SOC_DAIFMT_IB_IF:
576 if (!can_invert_lrclk)
577 return -EINVAL;
578 aifb |= WM8580_AIF_BCP;
579 aifb |= WM8580_AIF_LRP;
580 break;
581
582 case SND_SOC_DAIFMT_IB_NF:
583 aifb |= WM8580_AIF_BCP;
584 break;
585
586 case SND_SOC_DAIFMT_NB_IF:
587 if (!can_invert_lrclk)
588 return -EINVAL;
589 aifb |= WM8580_AIF_LRP;
590 break;
591
592 default:
593 return -EINVAL;
594 }
595
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 snd_soc_write(codec, WM8580_PAIF1 + codec_dai->driver->id, aifa);
597 snd_soc_write(codec, WM8580_PAIF3 + codec_dai->driver->id, aifb);
Mark Browne88ba012008-08-06 13:18:26 +0100598
599 return 0;
600}
601
602static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
603 int div_id, int div)
604{
605 struct snd_soc_codec *codec = codec_dai->codec;
606 unsigned int reg;
607
608 switch (div_id) {
609 case WM8580_MCLK:
Mark Brownf6f1eb12009-07-05 17:57:57 +0100610 reg = snd_soc_read(codec, WM8580_PLLB4);
Mark Browne88ba012008-08-06 13:18:26 +0100611 reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
612
613 switch (div) {
614 case WM8580_CLKSRC_MCLK:
615 /* Input */
616 break;
617
618 case WM8580_CLKSRC_PLLA:
619 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
620 break;
621 case WM8580_CLKSRC_PLLB:
622 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
623 break;
624
625 case WM8580_CLKSRC_OSC:
626 reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
627 break;
628
629 default:
630 return -EINVAL;
631 }
Mark Brownf6f1eb12009-07-05 17:57:57 +0100632 snd_soc_write(codec, WM8580_PLLB4, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100633 break;
634
Mark Browne88ba012008-08-06 13:18:26 +0100635 case WM8580_CLKOUTSRC:
Mark Brownf6f1eb12009-07-05 17:57:57 +0100636 reg = snd_soc_read(codec, WM8580_PLLB4);
Mark Browne88ba012008-08-06 13:18:26 +0100637 reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
638
639 switch (div) {
640 case WM8580_CLKSRC_NONE:
641 break;
642
643 case WM8580_CLKSRC_PLLA:
644 reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
645 break;
646
647 case WM8580_CLKSRC_PLLB:
648 reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
649 break;
650
651 case WM8580_CLKSRC_OSC:
652 reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
653 break;
654
655 default:
656 return -EINVAL;
657 }
Mark Brownf6f1eb12009-07-05 17:57:57 +0100658 snd_soc_write(codec, WM8580_PLLB4, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100659 break;
660
661 default:
662 return -EINVAL;
663 }
664
665 return 0;
666}
667
Mark Brownc5607d82010-08-13 19:05:04 +0100668static int wm8580_set_sysclk(struct snd_soc_dai *dai, int clk_id,
669 unsigned int freq, int dir)
670{
671 struct snd_soc_codec *codec = dai->codec;
672 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
Axel Lin748b2172011-12-30 23:36:23 +0800673 int ret, sel, sel_mask, sel_shift;
Mark Brownc5607d82010-08-13 19:05:04 +0100674
675 switch (dai->driver->id) {
Mark Brownc25edef2010-08-16 19:26:41 +0100676 case WM8580_DAI_PAIFRX:
Mark Brownc5607d82010-08-13 19:05:04 +0100677 sel_mask = 0x3;
678 sel_shift = 0;
679 break;
680
Mark Brownc25edef2010-08-16 19:26:41 +0100681 case WM8580_DAI_PAIFTX:
Mark Brownc5607d82010-08-13 19:05:04 +0100682 sel_mask = 0xc;
683 sel_shift = 2;
684 break;
685
686 default:
687 BUG_ON("Unknown DAI driver ID\n");
688 return -EINVAL;
689 }
690
691 switch (clk_id) {
692 case WM8580_CLKSRC_ADCMCLK:
Jassi Brar06286932010-12-03 10:34:25 +0900693 if (dai->driver->id != WM8580_DAI_PAIFTX)
Mark Brownc5607d82010-08-13 19:05:04 +0100694 return -EINVAL;
695 sel = 0 << sel_shift;
696 break;
697 case WM8580_CLKSRC_PLLA:
698 sel = 1 << sel_shift;
699 break;
700 case WM8580_CLKSRC_PLLB:
701 sel = 2 << sel_shift;
702 break;
703 case WM8580_CLKSRC_MCLK:
704 sel = 3 << sel_shift;
705 break;
706 default:
707 dev_err(codec->dev, "Unknown clock %d\n", clk_id);
708 return -EINVAL;
709 }
710
711 /* We really should validate PLL settings but not yet */
Jassi Brar06286932010-12-03 10:34:25 +0900712 wm8580->sysclk[dai->driver->id] = freq;
Mark Brownc5607d82010-08-13 19:05:04 +0100713
Axel Lin748b2172011-12-30 23:36:23 +0800714 ret = snd_soc_update_bits(codec, WM8580_CLKSEL, sel_mask, sel);
715 if (ret < 0)
716 return ret;
717
718 return 0;
Mark Brownc5607d82010-08-13 19:05:04 +0100719}
720
Mark Browne88ba012008-08-06 13:18:26 +0100721static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
722{
723 struct snd_soc_codec *codec = codec_dai->codec;
724 unsigned int reg;
725
Mark Brownf6f1eb12009-07-05 17:57:57 +0100726 reg = snd_soc_read(codec, WM8580_DAC_CONTROL5);
Mark Browne88ba012008-08-06 13:18:26 +0100727
728 if (mute)
729 reg |= WM8580_DAC_CONTROL5_MUTEALL;
730 else
731 reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
732
Mark Brownf6f1eb12009-07-05 17:57:57 +0100733 snd_soc_write(codec, WM8580_DAC_CONTROL5, reg);
Mark Browne88ba012008-08-06 13:18:26 +0100734
735 return 0;
736}
737
738static int wm8580_set_bias_level(struct snd_soc_codec *codec,
739 enum snd_soc_bias_level level)
740{
Mark Browne88ba012008-08-06 13:18:26 +0100741 switch (level) {
742 case SND_SOC_BIAS_ON:
743 case SND_SOC_BIAS_PREPARE:
Mark Browne88ba012008-08-06 13:18:26 +0100744 break;
Mark Brown6f7cb442009-03-11 18:31:08 +0000745
746 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200747 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown6f7cb442009-03-11 18:31:08 +0000748 /* Power up and get individual control of the DACs */
Axel Lina6785d72011-10-17 11:50:46 +0800749 snd_soc_update_bits(codec, WM8580_PWRDN1,
750 WM8580_PWRDN1_PWDN |
751 WM8580_PWRDN1_ALLDACPD, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000752
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300753 /* Make VMID high impedance */
Axel Lina6785d72011-10-17 11:50:46 +0800754 snd_soc_update_bits(codec, WM8580_ADC_CONTROL1,
755 0x100, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000756 }
757 break;
758
Mark Browne88ba012008-08-06 13:18:26 +0100759 case SND_SOC_BIAS_OFF:
Axel Lina6785d72011-10-17 11:50:46 +0800760 snd_soc_update_bits(codec, WM8580_PWRDN1,
761 WM8580_PWRDN1_PWDN, WM8580_PWRDN1_PWDN);
Mark Browne88ba012008-08-06 13:18:26 +0100762 break;
763 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200764 codec->dapm.bias_level = level;
Mark Browne88ba012008-08-06 13:18:26 +0100765 return 0;
766}
767
768#define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
769 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
770
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100771static const struct snd_soc_dai_ops wm8580_dai_ops_playback = {
Mark Brownc5607d82010-08-13 19:05:04 +0100772 .set_sysclk = wm8580_set_sysclk,
Eric Miao6335d052009-03-03 09:41:00 +0800773 .hw_params = wm8580_paif_hw_params,
774 .set_fmt = wm8580_set_paif_dai_fmt,
775 .set_clkdiv = wm8580_set_dai_clkdiv,
776 .set_pll = wm8580_set_dai_pll,
777 .digital_mute = wm8580_digital_mute,
778};
779
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100780static const struct snd_soc_dai_ops wm8580_dai_ops_capture = {
Mark Brownc5607d82010-08-13 19:05:04 +0100781 .set_sysclk = wm8580_set_sysclk,
Eric Miao6335d052009-03-03 09:41:00 +0800782 .hw_params = wm8580_paif_hw_params,
783 .set_fmt = wm8580_set_paif_dai_fmt,
784 .set_clkdiv = wm8580_set_dai_clkdiv,
785 .set_pll = wm8580_set_dai_pll,
786};
787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788static struct snd_soc_dai_driver wm8580_dai[] = {
Mark Browne88ba012008-08-06 13:18:26 +0100789 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 .name = "wm8580-hifi-playback",
791 .id = WM8580_DAI_PAIFRX,
Mark Browne88ba012008-08-06 13:18:26 +0100792 .playback = {
793 .stream_name = "Playback",
794 .channels_min = 1,
795 .channels_max = 6,
796 .rates = SNDRV_PCM_RATE_8000_192000,
797 .formats = WM8580_FORMATS,
798 },
Eric Miao6335d052009-03-03 09:41:00 +0800799 .ops = &wm8580_dai_ops_playback,
Mark Browne88ba012008-08-06 13:18:26 +0100800 },
801 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802 .name = "wm8580-hifi-capture",
803 .id = WM8580_DAI_PAIFTX,
Mark Browne88ba012008-08-06 13:18:26 +0100804 .capture = {
805 .stream_name = "Capture",
806 .channels_min = 2,
807 .channels_max = 2,
808 .rates = SNDRV_PCM_RATE_8000_192000,
809 .formats = WM8580_FORMATS,
810 },
Eric Miao6335d052009-03-03 09:41:00 +0800811 .ops = &wm8580_dai_ops_capture,
Mark Browne88ba012008-08-06 13:18:26 +0100812 },
813};
Mark Browne88ba012008-08-06 13:18:26 +0100814
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000815static int wm8580_probe(struct snd_soc_codec *codec)
Mark Browne88ba012008-08-06 13:18:26 +0100816{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
818 int ret = 0,i;
Mark Browne88ba012008-08-06 13:18:26 +0100819
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000820 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8580->control_type);
Mark Brownf6f1eb12009-07-05 17:57:57 +0100821 if (ret < 0) {
822 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 return ret;
Mark Brownf6f1eb12009-07-05 17:57:57 +0100824 }
825
Mark Browna583cd52009-06-17 17:30:14 +0100826 for (i = 0; i < ARRAY_SIZE(wm8580->supplies); i++)
827 wm8580->supplies[i].supply = wm8580_supply_names[i];
828
829 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8580->supplies),
830 wm8580->supplies);
831 if (ret != 0) {
832 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 return ret;
Mark Browna583cd52009-06-17 17:30:14 +0100834 }
835
836 ret = regulator_bulk_enable(ARRAY_SIZE(wm8580->supplies),
837 wm8580->supplies);
838 if (ret != 0) {
839 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
840 goto err_regulator_get;
841 }
842
Mark Brown6f7cb442009-03-11 18:31:08 +0000843 /* Get the codec into a known state */
Mark Brownf6f1eb12009-07-05 17:57:57 +0100844 ret = snd_soc_write(codec, WM8580_RESET, 0);
Mark Brown6f7cb442009-03-11 18:31:08 +0000845 if (ret != 0) {
846 dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
Mark Browna583cd52009-06-17 17:30:14 +0100847 goto err_regulator_enable;
Mark Brown6f7cb442009-03-11 18:31:08 +0000848 }
849
Mark Brown6f7cb442009-03-11 18:31:08 +0000850 wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
851
Mark Brown6f7cb442009-03-11 18:31:08 +0000852 return 0;
853
Mark Browna583cd52009-06-17 17:30:14 +0100854err_regulator_enable:
855 regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
856err_regulator_get:
857 regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
Mark Brown6f7cb442009-03-11 18:31:08 +0000858 return ret;
859}
860
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000861/* power down chip */
862static int wm8580_remove(struct snd_soc_codec *codec)
Mark Brown6f7cb442009-03-11 18:31:08 +0000863{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
865
866 wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF);
867
Mark Browna583cd52009-06-17 17:30:14 +0100868 regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
869 regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000870
871 return 0;
Mark Brown6f7cb442009-03-11 18:31:08 +0000872}
Mark Browne88ba012008-08-06 13:18:26 +0100873
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874static struct snd_soc_codec_driver soc_codec_dev_wm8580 = {
875 .probe = wm8580_probe,
876 .remove = wm8580_remove,
877 .set_bias_level = wm8580_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100878 .reg_cache_size = ARRAY_SIZE(wm8580_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 .reg_word_size = sizeof(u16),
Dimitris Papastamos0aa34b12010-11-08 10:41:53 +0000880 .reg_cache_default = wm8580_reg,
Mark Brownf235c642011-12-03 11:42:01 +0000881
882 .controls = wm8580_snd_controls,
883 .num_controls = ARRAY_SIZE(wm8580_snd_controls),
884 .dapm_widgets = wm8580_dapm_widgets,
885 .num_dapm_widgets = ARRAY_SIZE(wm8580_dapm_widgets),
886 .dapm_routes = wm8580_dapm_routes,
887 .num_dapm_routes = ARRAY_SIZE(wm8580_dapm_routes),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888};
889
Mark Brown733eef32011-08-02 13:22:36 +0900890static const struct of_device_id wm8580_of_match[] = {
891 { .compatible = "wlf,wm8580" },
892 { },
893};
894
Mark Browne88ba012008-08-06 13:18:26 +0100895#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare17be5522008-10-15 19:57:12 +0200896static int wm8580_i2c_probe(struct i2c_client *i2c,
897 const struct i2c_device_id *id)
Mark Browne88ba012008-08-06 13:18:26 +0100898{
Mark Brown6f7cb442009-03-11 18:31:08 +0000899 struct wm8580_priv *wm8580;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 int ret;
Mark Browne88ba012008-08-06 13:18:26 +0100901
Mark Brown6f7cb442009-03-11 18:31:08 +0000902 wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
903 if (wm8580 == NULL)
904 return -ENOMEM;
905
Mark Brown6f7cb442009-03-11 18:31:08 +0000906 i2c_set_clientdata(i2c, wm8580);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 wm8580->control_type = SND_SOC_I2C;
Mark Browne88ba012008-08-06 13:18:26 +0100908
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 ret = snd_soc_register_codec(&i2c->dev,
910 &soc_codec_dev_wm8580, wm8580_dai, ARRAY_SIZE(wm8580_dai));
911 if (ret < 0)
912 kfree(wm8580);
913 return ret;
Mark Browne88ba012008-08-06 13:18:26 +0100914}
915
Jean Delvare17be5522008-10-15 19:57:12 +0200916static int wm8580_i2c_remove(struct i2c_client *client)
Mark Browne88ba012008-08-06 13:18:26 +0100917{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 snd_soc_unregister_codec(&client->dev);
919 kfree(i2c_get_clientdata(client));
Mark Browne88ba012008-08-06 13:18:26 +0100920 return 0;
921}
922
Jean Delvare17be5522008-10-15 19:57:12 +0200923static const struct i2c_device_id wm8580_i2c_id[] = {
924 { "wm8580", 0 },
925 { }
926};
927MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
Mark Browne88ba012008-08-06 13:18:26 +0100928
Mark Browne88ba012008-08-06 13:18:26 +0100929static struct i2c_driver wm8580_i2c_driver = {
930 .driver = {
Mark Brown722d0da2011-08-02 13:21:53 +0900931 .name = "wm8580",
Mark Browne88ba012008-08-06 13:18:26 +0100932 .owner = THIS_MODULE,
Mark Brown733eef32011-08-02 13:22:36 +0900933 .of_match_table = wm8580_of_match,
Mark Browne88ba012008-08-06 13:18:26 +0100934 },
Jean Delvare17be5522008-10-15 19:57:12 +0200935 .probe = wm8580_i2c_probe,
936 .remove = wm8580_i2c_remove,
937 .id_table = wm8580_i2c_id,
Mark Browne88ba012008-08-06 13:18:26 +0100938};
Mark Browne88ba012008-08-06 13:18:26 +0100939#endif
940
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100941static int __init wm8580_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000942{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 int ret = 0;
Mark Brown6f7cb442009-03-11 18:31:08 +0000944
945#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
946 ret = i2c_add_driver(&wm8580_i2c_driver);
947 if (ret != 0) {
948 pr_err("Failed to register WM8580 I2C driver: %d\n", ret);
949 }
950#endif
951
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000952 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000953}
954module_init(wm8580_modinit);
955
956static void __exit wm8580_exit(void)
957{
Mark Brown6f7cb442009-03-11 18:31:08 +0000958#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
959 i2c_del_driver(&wm8580_i2c_driver);
960#endif
Mark Brown64089b82008-12-08 19:17:58 +0000961}
962module_exit(wm8580_exit);
963
Mark Browne88ba012008-08-06 13:18:26 +0100964MODULE_DESCRIPTION("ASoC WM8580 driver");
965MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
966MODULE_LICENSE("GPL");