blob: 51a2d265d40ece37da36bd1d854515aaf7916a18 [file] [log] [blame]
Mark Brown924914e2009-08-04 23:50:16 +01001/*
2 * wm8776.c -- WM8776 ALSA SoC Audio driver
3 *
4 * Copyright 2009 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * TODO: Input ALC/limiter support
13 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
22#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown924914e2009-08-04 23:50:16 +010024#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/soc-dapm.h>
29#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "wm8776.h"
33
Mark Brown924914e2009-08-04 23:50:16 +010034/* codec private data */
35struct wm8776_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000036 enum snd_soc_control_type control_type;
37 void *control_data;
Mark Brown924914e2009-08-04 23:50:16 +010038 u16 reg_cache[WM8776_CACHEREGNUM];
39 int sysclk[2];
40};
41
Mark Brown924914e2009-08-04 23:50:16 +010042static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
43 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
44 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
45 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
46 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
47 0xa6, 0x01, 0x01
48};
49
Mark Brown924914e2009-08-04 23:50:16 +010050static int wm8776_reset(struct snd_soc_codec *codec)
51{
Mark Brown35b12072009-08-08 10:37:33 +010052 return snd_soc_write(codec, WM8776_RESET, 0);
Mark Brown924914e2009-08-04 23:50:16 +010053}
54
55static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
56static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
57static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
58
59static const struct snd_kcontrol_new wm8776_snd_controls[] = {
60SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
61 0, 127, 0, hp_tlv),
62SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
63 0, 255, 0, dac_tlv),
64SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
65
66SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
67
68SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
69 0, 255, 0, adc_tlv),
70SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
71SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
72SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
73};
74
75static const struct snd_kcontrol_new inmix_controls[] = {
76SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
77SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
78SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
79SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
80SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
81};
82
83static const struct snd_kcontrol_new outmix_controls[] = {
84SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
85SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
86SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
87};
88
89static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
90SND_SOC_DAPM_INPUT("AUX"),
Mark Brown924914e2009-08-04 23:50:16 +010091
92SND_SOC_DAPM_INPUT("AIN1"),
93SND_SOC_DAPM_INPUT("AIN2"),
94SND_SOC_DAPM_INPUT("AIN3"),
95SND_SOC_DAPM_INPUT("AIN4"),
96SND_SOC_DAPM_INPUT("AIN5"),
97
98SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
99 inmix_controls, ARRAY_SIZE(inmix_controls)),
100
101SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
102SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
103
104SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
105 outmix_controls, ARRAY_SIZE(outmix_controls)),
106
107SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
108
109SND_SOC_DAPM_OUTPUT("VOUT"),
110
111SND_SOC_DAPM_OUTPUT("HPOUTL"),
112SND_SOC_DAPM_OUTPUT("HPOUTR"),
113};
114
115static const struct snd_soc_dapm_route routes[] = {
116 { "Input Mixer", "AIN1 Switch", "AIN1" },
117 { "Input Mixer", "AIN2 Switch", "AIN2" },
118 { "Input Mixer", "AIN3 Switch", "AIN3" },
119 { "Input Mixer", "AIN4 Switch", "AIN4" },
120 { "Input Mixer", "AIN5 Switch", "AIN5" },
121
122 { "ADC", NULL, "Input Mixer" },
123
124 { "Output Mixer", "DAC Switch", "DAC" },
125 { "Output Mixer", "AUX Switch", "AUX" },
126 { "Output Mixer", "Bypass Switch", "Input Mixer" },
127
128 { "VOUT", NULL, "Output Mixer" },
129
130 { "Headphone PGA", NULL, "Output Mixer" },
131
132 { "HPOUTL", NULL, "Headphone PGA" },
133 { "HPOUTR", NULL, "Headphone PGA" },
134};
135
136static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
137{
138 struct snd_soc_codec *codec = dai->codec;
139 int reg, iface, master;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 switch (dai->driver->id) {
Mark Brown924914e2009-08-04 23:50:16 +0100142 case WM8776_DAI_DAC:
143 reg = WM8776_DACIFCTRL;
144 master = 0x80;
145 break;
146 case WM8776_DAI_ADC:
147 reg = WM8776_ADCIFCTRL;
148 master = 0x100;
149 break;
150 default:
151 return -EINVAL;
152 }
153
154 iface = 0;
155
156 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
157 case SND_SOC_DAIFMT_CBM_CFM:
158 break;
159 case SND_SOC_DAIFMT_CBS_CFS:
160 master = 0;
161 break;
162 default:
163 return -EINVAL;
164 }
165
166 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
167 case SND_SOC_DAIFMT_I2S:
168 iface |= 0x0002;
169 break;
170 case SND_SOC_DAIFMT_RIGHT_J:
171 break;
172 case SND_SOC_DAIFMT_LEFT_J:
173 iface |= 0x0001;
174 break;
175 /* FIXME: CHECK A/B */
176 case SND_SOC_DAIFMT_DSP_A:
177 iface |= 0x0003;
178 break;
179 case SND_SOC_DAIFMT_DSP_B:
180 iface |= 0x0007;
181 break;
182 default:
183 return -EINVAL;
184 }
185
186 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
187 case SND_SOC_DAIFMT_NB_NF:
188 break;
189 case SND_SOC_DAIFMT_IB_IF:
190 iface |= 0x00c;
191 break;
192 case SND_SOC_DAIFMT_IB_NF:
193 iface |= 0x008;
194 break;
195 case SND_SOC_DAIFMT_NB_IF:
196 iface |= 0x004;
197 break;
198 default:
199 return -EINVAL;
200 }
201
202 /* Finally, write out the values */
203 snd_soc_update_bits(codec, reg, 0xf, iface);
204 snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
205
206 return 0;
207}
208
209static int mclk_ratios[] = {
210 128,
211 192,
212 256,
213 384,
214 512,
215 768,
216};
217
218static int wm8776_hw_params(struct snd_pcm_substream *substream,
219 struct snd_pcm_hw_params *params,
220 struct snd_soc_dai *dai)
221{
222 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900223 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Mark Brown924914e2009-08-04 23:50:16 +0100224 int iface_reg, iface;
225 int ratio_shift, master;
226 int i;
227
228 iface = 0;
229
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000230 switch (dai->driver->id) {
Mark Brown924914e2009-08-04 23:50:16 +0100231 case WM8776_DAI_DAC:
232 iface_reg = WM8776_DACIFCTRL;
233 master = 0x80;
234 ratio_shift = 4;
235 break;
236 case WM8776_DAI_ADC:
237 iface_reg = WM8776_ADCIFCTRL;
238 master = 0x100;
239 ratio_shift = 0;
240 break;
241 default:
242 return -EINVAL;
243 }
244
245
246 /* Set word length */
247 switch (params_format(params)) {
248 case SNDRV_PCM_FORMAT_S16_LE:
249 break;
250 case SNDRV_PCM_FORMAT_S20_3LE:
251 iface |= 0x10;
252 break;
253 case SNDRV_PCM_FORMAT_S24_LE:
254 iface |= 0x20;
255 break;
256 case SNDRV_PCM_FORMAT_S32_LE:
257 iface |= 0x30;
258 break;
259 }
260
261 /* Only need to set MCLK/LRCLK ratio if we're master */
262 if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
263 for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264 if (wm8776->sysclk[dai->driver->id] / params_rate(params)
Mark Brown924914e2009-08-04 23:50:16 +0100265 == mclk_ratios[i])
266 break;
267 }
268
269 if (i == ARRAY_SIZE(mclk_ratios)) {
270 dev_err(codec->dev,
271 "Unable to configure MCLK ratio %d/%d\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000272 wm8776->sysclk[dai->driver->id], params_rate(params));
Mark Brown924914e2009-08-04 23:50:16 +0100273 return -EINVAL;
274 }
275
276 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
277
278 snd_soc_update_bits(codec, WM8776_MSTRCTRL,
279 0x7 << ratio_shift, i << ratio_shift);
280 } else {
281 dev_dbg(codec->dev, "DAI in slave mode\n");
282 }
283
284 snd_soc_update_bits(codec, iface_reg, 0x30, iface);
285
286 return 0;
287}
288
289static int wm8776_mute(struct snd_soc_dai *dai, int mute)
290{
291 struct snd_soc_codec *codec = dai->codec;
292
293 return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
294}
295
296static int wm8776_set_sysclk(struct snd_soc_dai *dai,
297 int clk_id, unsigned int freq, int dir)
298{
299 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900300 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Mark Brown924914e2009-08-04 23:50:16 +0100301
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000302 BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
Mark Brown924914e2009-08-04 23:50:16 +0100303
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000304 wm8776->sysclk[dai->driver->id] = freq;
Mark Brown924914e2009-08-04 23:50:16 +0100305
306 return 0;
307}
308
309static int wm8776_set_bias_level(struct snd_soc_codec *codec,
310 enum snd_soc_bias_level level)
311{
312 switch (level) {
313 case SND_SOC_BIAS_ON:
314 break;
315 case SND_SOC_BIAS_PREPARE:
316 break;
317 case SND_SOC_BIAS_STANDBY:
318 if (codec->bias_level == SND_SOC_BIAS_OFF) {
319 /* Disable the global powerdown; DAPM does the rest */
320 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
321 }
322
323 break;
324 case SND_SOC_BIAS_OFF:
325 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
326 break;
327 }
328
329 codec->bias_level = level;
330 return 0;
331}
332
333#define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
334 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
335 SNDRV_PCM_RATE_96000)
336
337
338#define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
339 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
340
341static struct snd_soc_dai_ops wm8776_dac_ops = {
342 .digital_mute = wm8776_mute,
343 .hw_params = wm8776_hw_params,
344 .set_fmt = wm8776_set_fmt,
345 .set_sysclk = wm8776_set_sysclk,
346};
347
348static struct snd_soc_dai_ops wm8776_adc_ops = {
349 .hw_params = wm8776_hw_params,
350 .set_fmt = wm8776_set_fmt,
351 .set_sysclk = wm8776_set_sysclk,
352};
353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000354static struct snd_soc_dai_driver wm8776_dai[] = {
Mark Brown924914e2009-08-04 23:50:16 +0100355 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356 .name = "wm8776-hifi-playback",
357 .id = WM8776_DAI_DAC,
Mark Brown924914e2009-08-04 23:50:16 +0100358 .playback = {
359 .stream_name = "Playback",
360 .channels_min = 2,
361 .channels_max = 2,
362 .rates = WM8776_RATES,
363 .formats = WM8776_FORMATS,
364 },
365 .ops = &wm8776_dac_ops,
366 },
367 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000368 .name = "wm8776-hifi-capture",
369 .id = WM8776_DAI_ADC,
Mark Brown924914e2009-08-04 23:50:16 +0100370 .capture = {
371 .stream_name = "Capture",
372 .channels_min = 2,
373 .channels_max = 2,
374 .rates = WM8776_RATES,
375 .formats = WM8776_FORMATS,
376 },
377 .ops = &wm8776_adc_ops,
378 },
379};
Mark Brown924914e2009-08-04 23:50:16 +0100380
381#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000382static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown924914e2009-08-04 23:50:16 +0100383{
Mark Brown924914e2009-08-04 23:50:16 +0100384 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
385
386 return 0;
387}
388
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000389static int wm8776_resume(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100390{
Mark Brown924914e2009-08-04 23:50:16 +0100391 int i;
392 u8 data[2];
393 u16 *cache = codec->reg_cache;
394
395 /* Sync reg_cache with the hardware */
396 for (i = 0; i < ARRAY_SIZE(wm8776_reg); i++) {
Mark Browne0fb28e2010-01-02 13:14:23 +0000397 if (cache[i] == wm8776_reg[i])
398 continue;
Mark Brown924914e2009-08-04 23:50:16 +0100399 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
400 data[1] = cache[i] & 0x00ff;
401 codec->hw_write(codec->control_data, data, 2);
402 }
403
404 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
405
406 return 0;
407}
408#else
409#define wm8776_suspend NULL
410#define wm8776_resume NULL
411#endif
412
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000413static int wm8776_probe(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100414{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Mark Brown924914e2009-08-04 23:50:16 +0100416 int ret = 0;
417
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 codec->control_data = wm8776->control_data;
419 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
Mark Brown35b12072009-08-08 10:37:33 +0100420 if (ret < 0) {
421 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000422 return ret;
Mark Brown35b12072009-08-08 10:37:33 +0100423 }
424
Mark Brown924914e2009-08-04 23:50:16 +0100425 ret = wm8776_reset(codec);
426 if (ret < 0) {
427 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100429 }
430
431 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
432
433 /* Latch the update bits; right channel only since we always
434 * update both. */
435 snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
436 snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
437
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 snd_soc_add_controls(codec, wm8776_snd_controls,
439 ARRAY_SIZE(wm8776_snd_controls));
440 snd_soc_dapm_new_controls(codec, wm8776_dapm_widgets,
441 ARRAY_SIZE(wm8776_dapm_widgets));
442 snd_soc_dapm_add_routes(codec, routes, ARRAY_SIZE(routes));
Mark Brown924914e2009-08-04 23:50:16 +0100443
Mark Brown924914e2009-08-04 23:50:16 +0100444 return ret;
445}
446
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447/* power down chip */
448static int wm8776_remove(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100449{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000450 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
451 return 0;
Mark Brown924914e2009-08-04 23:50:16 +0100452}
453
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
455 .probe = wm8776_probe,
456 .remove = wm8776_remove,
457 .suspend = wm8776_suspend,
458 .resume = wm8776_resume,
459 .set_bias_level = wm8776_set_bias_level,
460 .reg_cache_size = sizeof(wm8776_reg),
461 .reg_word_size = sizeof(u16),
462 .reg_cache_default = wm8776_reg,
463};
464
Mark Brown924914e2009-08-04 23:50:16 +0100465#if defined(CONFIG_SPI_MASTER)
Mark Brown924914e2009-08-04 23:50:16 +0100466static int __devinit wm8776_spi_probe(struct spi_device *spi)
467{
Mark Brown924914e2009-08-04 23:50:16 +0100468 struct wm8776_priv *wm8776;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 int ret;
Mark Brown924914e2009-08-04 23:50:16 +0100470
471 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
472 if (wm8776 == NULL)
473 return -ENOMEM;
474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 wm8776->control_data = spi;
476 wm8776->control_type = SND_SOC_SPI;
477 spi_set_drvdata(spi, wm8776);
Mark Brown924914e2009-08-04 23:50:16 +0100478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479 ret = snd_soc_register_codec(&spi->dev,
480 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
481 if (ret < 0)
482 kfree(wm8776);
483 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100484}
485
486static int __devexit wm8776_spi_remove(struct spi_device *spi)
487{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 snd_soc_unregister_codec(&spi->dev);
489 kfree(spi_get_drvdata(spi));
Mark Brown924914e2009-08-04 23:50:16 +0100490 return 0;
491}
492
Mark Brown924914e2009-08-04 23:50:16 +0100493static struct spi_driver wm8776_spi_driver = {
494 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 .name = "wm8776-codec",
Mark Brown924914e2009-08-04 23:50:16 +0100496 .bus = &spi_bus_type,
497 .owner = THIS_MODULE,
498 },
499 .probe = wm8776_spi_probe,
Mark Brown924914e2009-08-04 23:50:16 +0100500 .remove = __devexit_p(wm8776_spi_remove),
501};
502#endif /* CONFIG_SPI_MASTER */
503
504#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
505static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
506 const struct i2c_device_id *id)
507{
508 struct wm8776_priv *wm8776;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 int ret;
Mark Brown924914e2009-08-04 23:50:16 +0100510
511 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
512 if (wm8776 == NULL)
513 return -ENOMEM;
514
Mark Brown924914e2009-08-04 23:50:16 +0100515 i2c_set_clientdata(i2c, wm8776);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 wm8776->control_data = i2c;
517 wm8776->control_type = SND_SOC_I2C;
Mark Brown924914e2009-08-04 23:50:16 +0100518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 ret = snd_soc_register_codec(&i2c->dev,
520 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
521 if (ret < 0)
522 kfree(wm8776);
523 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100524}
525
526static __devexit int wm8776_i2c_remove(struct i2c_client *client)
527{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 snd_soc_unregister_codec(&client->dev);
529 kfree(i2c_get_clientdata(client));
Mark Brown924914e2009-08-04 23:50:16 +0100530 return 0;
531}
532
Mark Brown924914e2009-08-04 23:50:16 +0100533static const struct i2c_device_id wm8776_i2c_id[] = {
534 { "wm8776", 0 },
535 { }
536};
537MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
538
539static struct i2c_driver wm8776_i2c_driver = {
540 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 .name = "wm8776-codec",
Mark Brown924914e2009-08-04 23:50:16 +0100542 .owner = THIS_MODULE,
543 },
544 .probe = wm8776_i2c_probe,
545 .remove = __devexit_p(wm8776_i2c_remove),
Mark Brown924914e2009-08-04 23:50:16 +0100546 .id_table = wm8776_i2c_id,
547};
548#endif
549
550static int __init wm8776_modinit(void)
551{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 int ret = 0;
Mark Brown924914e2009-08-04 23:50:16 +0100553#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
554 ret = i2c_add_driver(&wm8776_i2c_driver);
555 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
Mark Brown924914e2009-08-04 23:50:16 +0100557 ret);
558 }
559#endif
560#if defined(CONFIG_SPI_MASTER)
561 ret = spi_register_driver(&wm8776_spi_driver);
562 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
Mark Brown924914e2009-08-04 23:50:16 +0100564 ret);
565 }
566#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100568}
569module_init(wm8776_modinit);
570
571static void __exit wm8776_exit(void)
572{
573#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
574 i2c_del_driver(&wm8776_i2c_driver);
575#endif
576#if defined(CONFIG_SPI_MASTER)
577 spi_unregister_driver(&wm8776_spi_driver);
578#endif
579}
580module_exit(wm8776_exit);
581
582MODULE_DESCRIPTION("ASoC WM8776 driver");
583MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
584MODULE_LICENSE("GPL");