blob: d3b0a20744f1950ee2c0c882f77d72b6ab0a1959 [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>
Mark Brownb6de4312011-08-29 14:14:45 +010021#include <linux/of_device.h>
Mark Brown924914e2009-08-04 23:50:16 +010022#include <linux/platform_device.h>
23#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mark Brown924914e2009-08-04 23:50:16 +010025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Mark Brown924914e2009-08-04 23:50:16 +010029#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;
Mark Brown924914e2009-08-04 23:50:16 +010037 int sysclk[2];
38};
39
Mark Brown924914e2009-08-04 23:50:16 +010040static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
41 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
42 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
43 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
44 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
45 0xa6, 0x01, 0x01
46};
47
Mark Brown924914e2009-08-04 23:50:16 +010048static int wm8776_reset(struct snd_soc_codec *codec)
49{
Mark Brown35b12072009-08-08 10:37:33 +010050 return snd_soc_write(codec, WM8776_RESET, 0);
Mark Brown924914e2009-08-04 23:50:16 +010051}
52
53static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
54static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
55static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
56
57static const struct snd_kcontrol_new wm8776_snd_controls[] = {
58SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
59 0, 127, 0, hp_tlv),
60SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
61 0, 255, 0, dac_tlv),
62SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
63
64SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
65
66SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
67 0, 255, 0, adc_tlv),
68SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
69SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
70SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
71};
72
73static const struct snd_kcontrol_new inmix_controls[] = {
74SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
75SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
76SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
77SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
78SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
79};
80
81static const struct snd_kcontrol_new outmix_controls[] = {
82SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
83SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
84SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
85};
86
87static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
88SND_SOC_DAPM_INPUT("AUX"),
Mark Brown924914e2009-08-04 23:50:16 +010089
90SND_SOC_DAPM_INPUT("AIN1"),
91SND_SOC_DAPM_INPUT("AIN2"),
92SND_SOC_DAPM_INPUT("AIN3"),
93SND_SOC_DAPM_INPUT("AIN4"),
94SND_SOC_DAPM_INPUT("AIN5"),
95
96SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
97 inmix_controls, ARRAY_SIZE(inmix_controls)),
98
99SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
100SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
101
102SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
103 outmix_controls, ARRAY_SIZE(outmix_controls)),
104
105SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
106
107SND_SOC_DAPM_OUTPUT("VOUT"),
108
109SND_SOC_DAPM_OUTPUT("HPOUTL"),
110SND_SOC_DAPM_OUTPUT("HPOUTR"),
111};
112
113static const struct snd_soc_dapm_route routes[] = {
114 { "Input Mixer", "AIN1 Switch", "AIN1" },
115 { "Input Mixer", "AIN2 Switch", "AIN2" },
116 { "Input Mixer", "AIN3 Switch", "AIN3" },
117 { "Input Mixer", "AIN4 Switch", "AIN4" },
118 { "Input Mixer", "AIN5 Switch", "AIN5" },
119
120 { "ADC", NULL, "Input Mixer" },
121
122 { "Output Mixer", "DAC Switch", "DAC" },
123 { "Output Mixer", "AUX Switch", "AUX" },
124 { "Output Mixer", "Bypass Switch", "Input Mixer" },
125
126 { "VOUT", NULL, "Output Mixer" },
127
128 { "Headphone PGA", NULL, "Output Mixer" },
129
130 { "HPOUTL", NULL, "Headphone PGA" },
131 { "HPOUTR", NULL, "Headphone PGA" },
132};
133
134static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
135{
136 struct snd_soc_codec *codec = dai->codec;
137 int reg, iface, master;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 switch (dai->driver->id) {
Mark Brown924914e2009-08-04 23:50:16 +0100140 case WM8776_DAI_DAC:
141 reg = WM8776_DACIFCTRL;
142 master = 0x80;
143 break;
144 case WM8776_DAI_ADC:
145 reg = WM8776_ADCIFCTRL;
146 master = 0x100;
147 break;
148 default:
149 return -EINVAL;
150 }
151
152 iface = 0;
153
154 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
155 case SND_SOC_DAIFMT_CBM_CFM:
156 break;
157 case SND_SOC_DAIFMT_CBS_CFS:
158 master = 0;
159 break;
160 default:
161 return -EINVAL;
162 }
163
164 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
165 case SND_SOC_DAIFMT_I2S:
166 iface |= 0x0002;
167 break;
168 case SND_SOC_DAIFMT_RIGHT_J:
169 break;
170 case SND_SOC_DAIFMT_LEFT_J:
171 iface |= 0x0001;
172 break;
Mark Brown924914e2009-08-04 23:50:16 +0100173 default:
174 return -EINVAL;
175 }
176
177 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
178 case SND_SOC_DAIFMT_NB_NF:
179 break;
180 case SND_SOC_DAIFMT_IB_IF:
181 iface |= 0x00c;
182 break;
183 case SND_SOC_DAIFMT_IB_NF:
184 iface |= 0x008;
185 break;
186 case SND_SOC_DAIFMT_NB_IF:
187 iface |= 0x004;
188 break;
189 default:
190 return -EINVAL;
191 }
192
193 /* Finally, write out the values */
194 snd_soc_update_bits(codec, reg, 0xf, iface);
195 snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
196
197 return 0;
198}
199
200static int mclk_ratios[] = {
201 128,
202 192,
203 256,
204 384,
205 512,
206 768,
207};
208
209static int wm8776_hw_params(struct snd_pcm_substream *substream,
210 struct snd_pcm_hw_params *params,
211 struct snd_soc_dai *dai)
212{
213 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900214 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Mark Brown924914e2009-08-04 23:50:16 +0100215 int iface_reg, iface;
216 int ratio_shift, master;
217 int i;
218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 switch (dai->driver->id) {
Mark Brown924914e2009-08-04 23:50:16 +0100220 case WM8776_DAI_DAC:
221 iface_reg = WM8776_DACIFCTRL;
222 master = 0x80;
223 ratio_shift = 4;
224 break;
225 case WM8776_DAI_ADC:
226 iface_reg = WM8776_ADCIFCTRL;
227 master = 0x100;
228 ratio_shift = 0;
229 break;
230 default:
231 return -EINVAL;
232 }
233
Mark Brown924914e2009-08-04 23:50:16 +0100234 /* Set word length */
Timur Tabid1dc6982011-09-13 12:59:35 -0500235 switch (snd_pcm_format_width(params_format(params))) {
236 case 16:
237 iface = 0;
Joachim Eastwood990fc3d2011-12-23 17:32:25 +0100238 break;
Timur Tabid1dc6982011-09-13 12:59:35 -0500239 case 20:
240 iface = 0x10;
Mark Brown924914e2009-08-04 23:50:16 +0100241 break;
Timur Tabid1dc6982011-09-13 12:59:35 -0500242 case 24:
243 iface = 0x20;
Mark Brown924914e2009-08-04 23:50:16 +0100244 break;
Timur Tabid1dc6982011-09-13 12:59:35 -0500245 case 32:
246 iface = 0x30;
Mark Brown924914e2009-08-04 23:50:16 +0100247 break;
Timur Tabid1dc6982011-09-13 12:59:35 -0500248 default:
249 dev_err(codec->dev, "Unsupported sample size: %i\n",
250 snd_pcm_format_width(params_format(params)));
251 return -EINVAL;
Mark Brown924914e2009-08-04 23:50:16 +0100252 }
253
254 /* Only need to set MCLK/LRCLK ratio if we're master */
255 if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
256 for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000257 if (wm8776->sysclk[dai->driver->id] / params_rate(params)
Mark Brown924914e2009-08-04 23:50:16 +0100258 == mclk_ratios[i])
259 break;
260 }
261
262 if (i == ARRAY_SIZE(mclk_ratios)) {
263 dev_err(codec->dev,
264 "Unable to configure MCLK ratio %d/%d\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000265 wm8776->sysclk[dai->driver->id], params_rate(params));
Mark Brown924914e2009-08-04 23:50:16 +0100266 return -EINVAL;
267 }
268
269 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
270
271 snd_soc_update_bits(codec, WM8776_MSTRCTRL,
272 0x7 << ratio_shift, i << ratio_shift);
273 } else {
274 dev_dbg(codec->dev, "DAI in slave mode\n");
275 }
276
277 snd_soc_update_bits(codec, iface_reg, 0x30, iface);
278
279 return 0;
280}
281
282static int wm8776_mute(struct snd_soc_dai *dai, int mute)
283{
284 struct snd_soc_codec *codec = dai->codec;
285
286 return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
287}
288
289static int wm8776_set_sysclk(struct snd_soc_dai *dai,
290 int clk_id, unsigned int freq, int dir)
291{
292 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900293 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Mark Brown924914e2009-08-04 23:50:16 +0100294
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000295 BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
Mark Brown924914e2009-08-04 23:50:16 +0100296
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000297 wm8776->sysclk[dai->driver->id] = freq;
Mark Brown924914e2009-08-04 23:50:16 +0100298
299 return 0;
300}
301
302static int wm8776_set_bias_level(struct snd_soc_codec *codec,
303 enum snd_soc_bias_level level)
304{
305 switch (level) {
306 case SND_SOC_BIAS_ON:
307 break;
308 case SND_SOC_BIAS_PREPARE:
309 break;
310 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200311 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Axel Linabe11d02011-10-07 21:41:41 +0800312 snd_soc_cache_sync(codec);
313
Mark Brown924914e2009-08-04 23:50:16 +0100314 /* Disable the global powerdown; DAPM does the rest */
315 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
316 }
317
318 break;
319 case SND_SOC_BIAS_OFF:
320 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
321 break;
322 }
323
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200324 codec->dapm.bias_level = level;
Mark Brown924914e2009-08-04 23:50:16 +0100325 return 0;
326}
327
Mark Brown924914e2009-08-04 23:50:16 +0100328#define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
329 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
330
331static struct snd_soc_dai_ops wm8776_dac_ops = {
332 .digital_mute = wm8776_mute,
333 .hw_params = wm8776_hw_params,
334 .set_fmt = wm8776_set_fmt,
335 .set_sysclk = wm8776_set_sysclk,
336};
337
338static struct snd_soc_dai_ops wm8776_adc_ops = {
339 .hw_params = wm8776_hw_params,
340 .set_fmt = wm8776_set_fmt,
341 .set_sysclk = wm8776_set_sysclk,
342};
343
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000344static struct snd_soc_dai_driver wm8776_dai[] = {
Mark Brown924914e2009-08-04 23:50:16 +0100345 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000346 .name = "wm8776-hifi-playback",
347 .id = WM8776_DAI_DAC,
Mark Brown924914e2009-08-04 23:50:16 +0100348 .playback = {
349 .stream_name = "Playback",
350 .channels_min = 2,
351 .channels_max = 2,
Timur Tabi00162262011-09-16 09:16:54 -0500352 .rates = SNDRV_PCM_RATE_CONTINUOUS,
353 .rate_min = 32000,
354 .rate_max = 192000,
Mark Brown924914e2009-08-04 23:50:16 +0100355 .formats = WM8776_FORMATS,
356 },
357 .ops = &wm8776_dac_ops,
358 },
359 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000360 .name = "wm8776-hifi-capture",
361 .id = WM8776_DAI_ADC,
Mark Brown924914e2009-08-04 23:50:16 +0100362 .capture = {
363 .stream_name = "Capture",
364 .channels_min = 2,
365 .channels_max = 2,
Timur Tabi00162262011-09-16 09:16:54 -0500366 .rates = SNDRV_PCM_RATE_CONTINUOUS,
367 .rate_min = 32000,
368 .rate_max = 96000,
Mark Brown924914e2009-08-04 23:50:16 +0100369 .formats = WM8776_FORMATS,
370 },
371 .ops = &wm8776_adc_ops,
372 },
373};
Mark Brown924914e2009-08-04 23:50:16 +0100374
375#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000376static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown924914e2009-08-04 23:50:16 +0100377{
Mark Brown924914e2009-08-04 23:50:16 +0100378 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
379
380 return 0;
381}
382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383static int wm8776_resume(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100384{
Mark Brown924914e2009-08-04 23:50:16 +0100385 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brown924914e2009-08-04 23:50:16 +0100386 return 0;
387}
388#else
389#define wm8776_suspend NULL
390#define wm8776_resume NULL
391#endif
392
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000393static int wm8776_probe(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100394{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000395 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200396 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brown924914e2009-08-04 23:50:16 +0100397 int ret = 0;
398
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000399 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
Mark Brown35b12072009-08-08 10:37:33 +0100400 if (ret < 0) {
401 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000402 return ret;
Mark Brown35b12072009-08-08 10:37:33 +0100403 }
404
Mark Brown924914e2009-08-04 23:50:16 +0100405 ret = wm8776_reset(codec);
406 if (ret < 0) {
407 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100409 }
410
411 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
412
413 /* Latch the update bits; right channel only since we always
414 * update both. */
415 snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
416 snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
417
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 snd_soc_add_controls(codec, wm8776_snd_controls,
419 ARRAY_SIZE(wm8776_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200420 snd_soc_dapm_new_controls(dapm, wm8776_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000421 ARRAY_SIZE(wm8776_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200422 snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
Mark Brown924914e2009-08-04 23:50:16 +0100423
Mark Brown924914e2009-08-04 23:50:16 +0100424 return ret;
425}
426
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000427/* power down chip */
428static int wm8776_remove(struct snd_soc_codec *codec)
Mark Brown924914e2009-08-04 23:50:16 +0100429{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000430 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
431 return 0;
Mark Brown924914e2009-08-04 23:50:16 +0100432}
433
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
435 .probe = wm8776_probe,
436 .remove = wm8776_remove,
437 .suspend = wm8776_suspend,
438 .resume = wm8776_resume,
439 .set_bias_level = wm8776_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100440 .reg_cache_size = ARRAY_SIZE(wm8776_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000441 .reg_word_size = sizeof(u16),
442 .reg_cache_default = wm8776_reg,
443};
444
Mark Brownb6de4312011-08-29 14:14:45 +0100445static const struct of_device_id wm8776_of_match[] = {
446 { .compatible = "wlf,wm8776", },
447 { }
448};
449MODULE_DEVICE_TABLE(of, wm8776_of_match);
450
Mark Brown924914e2009-08-04 23:50:16 +0100451#if defined(CONFIG_SPI_MASTER)
Mark Brown924914e2009-08-04 23:50:16 +0100452static int __devinit wm8776_spi_probe(struct spi_device *spi)
453{
Mark Brown924914e2009-08-04 23:50:16 +0100454 struct wm8776_priv *wm8776;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 int ret;
Mark Brown924914e2009-08-04 23:50:16 +0100456
457 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
458 if (wm8776 == NULL)
459 return -ENOMEM;
460
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461 wm8776->control_type = SND_SOC_SPI;
462 spi_set_drvdata(spi, wm8776);
Mark Brown924914e2009-08-04 23:50:16 +0100463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 ret = snd_soc_register_codec(&spi->dev,
465 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
466 if (ret < 0)
467 kfree(wm8776);
468 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100469}
470
471static int __devexit wm8776_spi_remove(struct spi_device *spi)
472{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473 snd_soc_unregister_codec(&spi->dev);
474 kfree(spi_get_drvdata(spi));
Mark Brown924914e2009-08-04 23:50:16 +0100475 return 0;
476}
477
Mark Brown924914e2009-08-04 23:50:16 +0100478static struct spi_driver wm8776_spi_driver = {
479 .driver = {
Mark Brown1e3ad572011-08-29 14:10:42 +0100480 .name = "wm8776",
Mark Brown924914e2009-08-04 23:50:16 +0100481 .owner = THIS_MODULE,
Mark Brownb6de4312011-08-29 14:14:45 +0100482 .of_match_table = wm8776_of_match,
Mark Brown924914e2009-08-04 23:50:16 +0100483 },
484 .probe = wm8776_spi_probe,
Mark Brown924914e2009-08-04 23:50:16 +0100485 .remove = __devexit_p(wm8776_spi_remove),
486};
487#endif /* CONFIG_SPI_MASTER */
488
489#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
490static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
491 const struct i2c_device_id *id)
492{
493 struct wm8776_priv *wm8776;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494 int ret;
Mark Brown924914e2009-08-04 23:50:16 +0100495
496 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
497 if (wm8776 == NULL)
498 return -ENOMEM;
499
Mark Brown924914e2009-08-04 23:50:16 +0100500 i2c_set_clientdata(i2c, wm8776);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 wm8776->control_type = SND_SOC_I2C;
Mark Brown924914e2009-08-04 23:50:16 +0100502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 ret = snd_soc_register_codec(&i2c->dev,
504 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
505 if (ret < 0)
506 kfree(wm8776);
507 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100508}
509
510static __devexit int wm8776_i2c_remove(struct i2c_client *client)
511{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 snd_soc_unregister_codec(&client->dev);
513 kfree(i2c_get_clientdata(client));
Mark Brown924914e2009-08-04 23:50:16 +0100514 return 0;
515}
516
Mark Brown924914e2009-08-04 23:50:16 +0100517static const struct i2c_device_id wm8776_i2c_id[] = {
518 { "wm8776", 0 },
519 { }
520};
521MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
522
523static struct i2c_driver wm8776_i2c_driver = {
524 .driver = {
Mark Brown1e3ad572011-08-29 14:10:42 +0100525 .name = "wm8776",
Mark Brown924914e2009-08-04 23:50:16 +0100526 .owner = THIS_MODULE,
Mark Brownb6de4312011-08-29 14:14:45 +0100527 .of_match_table = wm8776_of_match,
Mark Brown924914e2009-08-04 23:50:16 +0100528 },
529 .probe = wm8776_i2c_probe,
530 .remove = __devexit_p(wm8776_i2c_remove),
Mark Brown924914e2009-08-04 23:50:16 +0100531 .id_table = wm8776_i2c_id,
532};
533#endif
534
535static int __init wm8776_modinit(void)
536{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 int ret = 0;
Mark Brown924914e2009-08-04 23:50:16 +0100538#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
539 ret = i2c_add_driver(&wm8776_i2c_driver);
540 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
Mark Brown924914e2009-08-04 23:50:16 +0100542 ret);
543 }
544#endif
545#if defined(CONFIG_SPI_MASTER)
546 ret = spi_register_driver(&wm8776_spi_driver);
547 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
Mark Brown924914e2009-08-04 23:50:16 +0100549 ret);
550 }
551#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 return ret;
Mark Brown924914e2009-08-04 23:50:16 +0100553}
554module_init(wm8776_modinit);
555
556static void __exit wm8776_exit(void)
557{
558#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
559 i2c_del_driver(&wm8776_i2c_driver);
560#endif
561#if defined(CONFIG_SPI_MASTER)
562 spi_unregister_driver(&wm8776_spi_driver);
563#endif
564}
565module_exit(wm8776_exit);
566
567MODULE_DESCRIPTION("ASoC WM8776 driver");
568MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
569MODULE_LICENSE("GPL");