blob: 57ad22aacc516dc17e18042a98712266f761afa3 [file] [log] [blame]
Ian Lartey992bee42010-07-31 00:32:11 +01001/*
2 * wm8741.c -- WM8741 ALSA SoC Audio driver
3 *
4 * Copyright 2010 Wolfson Microelectronics plc
5 *
6 * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
Mark Brown39e9b8d2011-08-03 17:30:57 +090020#include <linux/spi/spi.h>
Ian Lartey992bee42010-07-31 00:32:11 +010021#include <linux/platform_device.h>
22#include <linux/regulator/consumer.h>
23#include <linux/slab.h>
Mark Brown80080ec2011-08-03 17:31:26 +090024#include <linux/of_device.h>
Ian Lartey992bee42010-07-31 00:32:11 +010025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Ian Lartey992bee42010-07-31 00:32:11 +010029#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "wm8741.h"
33
Ian Lartey992bee42010-07-31 00:32:11 +010034#define WM8741_NUM_SUPPLIES 2
35static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
36 "AVDD",
37 "DVDD",
38};
39
Ian Lartey3fe4a5e2010-08-27 15:26:27 +010040#define WM8741_NUM_RATES 6
Ian Lartey992bee42010-07-31 00:32:11 +010041
42/* codec private data */
43struct wm8741_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044 enum snd_soc_control_type control_type;
Ian Lartey992bee42010-07-31 00:32:11 +010045 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
46 unsigned int sysclk;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +010047 struct snd_pcm_hw_constraint_list *sysclk_constraints;
Ian Lartey992bee42010-07-31 00:32:11 +010048};
49
50static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
51 0x0000, /* R0 - DACLLSB Attenuation */
52 0x0000, /* R1 - DACLMSB Attenuation */
53 0x0000, /* R2 - DACRLSB Attenuation */
54 0x0000, /* R3 - DACRMSB Attenuation */
55 0x0000, /* R4 - Volume Control */
56 0x000A, /* R5 - Format Control */
57 0x0000, /* R6 - Filter Control */
58 0x0000, /* R7 - Mode Control 1 */
59 0x0002, /* R8 - Mode Control 2 */
60 0x0000, /* R9 - Reset */
61 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
62};
63
64
65static int wm8741_reset(struct snd_soc_codec *codec)
66{
67 return snd_soc_write(codec, WM8741_RESET, 0);
68}
69
70static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
71static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
72
73static const struct snd_kcontrol_new wm8741_snd_controls[] = {
74SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
75 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
76SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
77 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
78};
79
80static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
81SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
82SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
83SND_SOC_DAPM_OUTPUT("VOUTLP"),
84SND_SOC_DAPM_OUTPUT("VOUTLN"),
85SND_SOC_DAPM_OUTPUT("VOUTRP"),
86SND_SOC_DAPM_OUTPUT("VOUTRN"),
87};
88
89static const struct snd_soc_dapm_route intercon[] = {
90 { "VOUTLP", NULL, "DACL" },
91 { "VOUTLN", NULL, "DACL" },
92 { "VOUTRP", NULL, "DACR" },
93 { "VOUTRN", NULL, "DACR" },
94};
95
96static int wm8741_add_widgets(struct snd_soc_codec *codec)
97{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020098 struct snd_soc_dapm_context *dapm = &codec->dapm;
Ian Lartey992bee42010-07-31 00:32:11 +010099
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200100 snd_soc_dapm_new_controls(dapm, wm8741_dapm_widgets,
101 ARRAY_SIZE(wm8741_dapm_widgets));
102 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Ian Lartey992bee42010-07-31 00:32:11 +0100103
104 return 0;
105}
106
107static struct {
108 int value;
109 int ratio;
110} lrclk_ratios[WM8741_NUM_RATES] = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100111 { 1, 128 },
112 { 2, 192 },
113 { 3, 256 },
114 { 4, 384 },
115 { 5, 512 },
116 { 6, 768 },
117};
118
119static unsigned int rates_11289[] = {
120 44100, 88235,
121};
122
123static struct snd_pcm_hw_constraint_list constraints_11289 = {
124 .count = ARRAY_SIZE(rates_11289),
125 .list = rates_11289,
126};
127
128static unsigned int rates_12288[] = {
129 32000, 48000, 96000,
130};
131
132static struct snd_pcm_hw_constraint_list constraints_12288 = {
133 .count = ARRAY_SIZE(rates_12288),
134 .list = rates_12288,
135};
136
137static unsigned int rates_16384[] = {
138 32000,
139};
140
141static struct snd_pcm_hw_constraint_list constraints_16384 = {
142 .count = ARRAY_SIZE(rates_16384),
143 .list = rates_16384,
144};
145
146static unsigned int rates_16934[] = {
147 44100, 88235,
148};
149
150static struct snd_pcm_hw_constraint_list constraints_16934 = {
151 .count = ARRAY_SIZE(rates_16934),
152 .list = rates_16934,
153};
154
155static unsigned int rates_18432[] = {
156 48000, 96000,
157};
158
159static struct snd_pcm_hw_constraint_list constraints_18432 = {
160 .count = ARRAY_SIZE(rates_18432),
161 .list = rates_18432,
162};
163
164static unsigned int rates_22579[] = {
165 44100, 88235, 1764000
166};
167
168static struct snd_pcm_hw_constraint_list constraints_22579 = {
169 .count = ARRAY_SIZE(rates_22579),
170 .list = rates_22579,
171};
172
173static unsigned int rates_24576[] = {
174 32000, 48000, 96000, 192000
175};
176
177static struct snd_pcm_hw_constraint_list constraints_24576 = {
178 .count = ARRAY_SIZE(rates_24576),
179 .list = rates_24576,
180};
181
182static unsigned int rates_36864[] = {
183 48000, 96000, 19200
184};
185
186static struct snd_pcm_hw_constraint_list constraints_36864 = {
187 .count = ARRAY_SIZE(rates_36864),
188 .list = rates_36864,
Ian Lartey992bee42010-07-31 00:32:11 +0100189};
190
191
192static int wm8741_startup(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *dai)
194{
195 struct snd_soc_codec *codec = dai->codec;
196 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
197
198 /* The set of sample rates that can be supported depends on the
199 * MCLK supplied to the CODEC - enforce this.
200 */
201 if (!wm8741->sysclk) {
202 dev_err(codec->dev,
203 "No MCLK configured, call set_sysclk() on init\n");
204 return -EINVAL;
205 }
206
207 snd_pcm_hw_constraint_list(substream->runtime, 0,
208 SNDRV_PCM_HW_PARAM_RATE,
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100209 wm8741->sysclk_constraints);
Ian Lartey992bee42010-07-31 00:32:11 +0100210
211 return 0;
212}
213
214static int wm8741_hw_params(struct snd_pcm_substream *substream,
215 struct snd_pcm_hw_params *params,
216 struct snd_soc_dai *dai)
217{
218 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 struct snd_soc_codec *codec = rtd->codec;
Ian Lartey992bee42010-07-31 00:32:11 +0100220 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
221 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
222 int i;
223
224 /* Find a supported LRCLK ratio */
225 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
226 if (wm8741->sysclk / params_rate(params) ==
227 lrclk_ratios[i].ratio)
228 break;
229 }
230
231 /* Should never happen, should be handled by constraints */
232 if (i == ARRAY_SIZE(lrclk_ratios)) {
233 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
234 wm8741->sysclk / params_rate(params));
235 return -EINVAL;
236 }
237
238 /* bit size */
239 switch (params_format(params)) {
240 case SNDRV_PCM_FORMAT_S16_LE:
241 break;
242 case SNDRV_PCM_FORMAT_S20_3LE:
243 iface |= 0x0001;
244 break;
245 case SNDRV_PCM_FORMAT_S24_LE:
246 iface |= 0x0002;
247 break;
248 case SNDRV_PCM_FORMAT_S32_LE:
249 iface |= 0x0003;
250 break;
251 default:
252 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
253 params_format(params));
254 return -EINVAL;
255 }
256
257 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
258 params_format(params));
259
260 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
261 return 0;
262}
263
264static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
265 int clk_id, unsigned int freq, int dir)
266{
267 struct snd_soc_codec *codec = codec_dai->codec;
268 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100269
270 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
271
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100272 switch (freq) {
273 case 11289600:
274 wm8741->sysclk_constraints = &constraints_11289;
275 wm8741->sysclk = freq;
276 return 0;
Ian Lartey992bee42010-07-31 00:32:11 +0100277
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100278 case 12288000:
279 wm8741->sysclk_constraints = &constraints_12288;
280 wm8741->sysclk = freq;
281 return 0;
Ian Lartey992bee42010-07-31 00:32:11 +0100282
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100283 case 16384000:
284 wm8741->sysclk_constraints = &constraints_16384;
285 wm8741->sysclk = freq;
286 return 0;
Ian Lartey992bee42010-07-31 00:32:11 +0100287
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100288 case 16934400:
289 wm8741->sysclk_constraints = &constraints_16934;
290 wm8741->sysclk = freq;
291 return 0;
292
293 case 18432000:
294 wm8741->sysclk_constraints = &constraints_18432;
295 wm8741->sysclk = freq;
296 return 0;
297
298 case 22579200:
299 case 33868800:
300 wm8741->sysclk_constraints = &constraints_22579;
301 wm8741->sysclk = freq;
302 return 0;
303
304 case 24576000:
305 wm8741->sysclk_constraints = &constraints_24576;
306 wm8741->sysclk = freq;
307 return 0;
308
309 case 36864000:
310 wm8741->sysclk_constraints = &constraints_36864;
311 wm8741->sysclk = freq;
312 return 0;
Ian Lartey992bee42010-07-31 00:32:11 +0100313 }
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100314 return -EINVAL;
Ian Lartey992bee42010-07-31 00:32:11 +0100315}
316
317static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
318 unsigned int fmt)
319{
320 struct snd_soc_codec *codec = codec_dai->codec;
321 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
322
323 /* check master/slave audio interface */
324 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
325 case SND_SOC_DAIFMT_CBS_CFS:
326 break;
327 default:
328 return -EINVAL;
329 }
330
331 /* interface format */
332 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
333 case SND_SOC_DAIFMT_I2S:
334 iface |= 0x0008;
335 break;
336 case SND_SOC_DAIFMT_RIGHT_J:
337 break;
338 case SND_SOC_DAIFMT_LEFT_J:
339 iface |= 0x0004;
340 break;
341 case SND_SOC_DAIFMT_DSP_A:
Axel Lin3a340102011-10-17 20:14:56 +0800342 iface |= 0x000C;
Ian Lartey992bee42010-07-31 00:32:11 +0100343 break;
344 case SND_SOC_DAIFMT_DSP_B:
Axel Lin3a340102011-10-17 20:14:56 +0800345 iface |= 0x001C;
Ian Lartey992bee42010-07-31 00:32:11 +0100346 break;
347 default:
348 return -EINVAL;
349 }
350
351 /* clock inversion */
352 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
353 case SND_SOC_DAIFMT_NB_NF:
354 break;
355 case SND_SOC_DAIFMT_IB_IF:
356 iface |= 0x0010;
357 break;
358 case SND_SOC_DAIFMT_IB_NF:
359 iface |= 0x0020;
360 break;
361 case SND_SOC_DAIFMT_NB_IF:
362 iface |= 0x0030;
363 break;
364 default:
365 return -EINVAL;
366 }
367
368
369 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
370 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
371 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
372
373 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
374 return 0;
375}
376
377#define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
378 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
379 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
380 SNDRV_PCM_RATE_192000)
381
382#define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
383 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
384
385static struct snd_soc_dai_ops wm8741_dai_ops = {
386 .startup = wm8741_startup,
387 .hw_params = wm8741_hw_params,
388 .set_sysclk = wm8741_set_dai_sysclk,
389 .set_fmt = wm8741_set_dai_fmt,
390};
391
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000392static struct snd_soc_dai_driver wm8741_dai = {
Ian Lartey30e2d362010-08-20 17:18:44 +0100393 .name = "wm8741",
Ian Lartey992bee42010-07-31 00:32:11 +0100394 .playback = {
395 .stream_name = "Playback",
396 .channels_min = 2, /* Mono modes not yet supported */
397 .channels_max = 2,
398 .rates = WM8741_RATES,
399 .formats = WM8741_FORMATS,
400 },
401 .ops = &wm8741_dai_ops,
402};
Ian Lartey992bee42010-07-31 00:32:11 +0100403
404#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405static int wm8741_resume(struct snd_soc_codec *codec)
Ian Lartey992bee42010-07-31 00:32:11 +0100406{
Axel Lindf3431b2011-10-17 20:16:37 +0800407 snd_soc_cache_sync(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100408 return 0;
409}
410#else
411#define wm8741_suspend NULL
412#define wm8741_resume NULL
413#endif
414
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415static int wm8741_probe(struct snd_soc_codec *codec)
Ian Lartey992bee42010-07-31 00:32:11 +0100416{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000417 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100418 int ret = 0;
Mark Brown398575d2011-08-03 17:16:11 +0900419 int i;
420
421 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
422 wm8741->supplies[i].supply = wm8741_supply_names[i];
423
424 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8741->supplies),
425 wm8741->supplies);
426 if (ret != 0) {
427 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
428 goto err;
429 }
430
431 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
432 wm8741->supplies);
433 if (ret != 0) {
434 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
435 goto err_get;
436 }
Ian Lartey992bee42010-07-31 00:32:11 +0100437
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
Ian Lartey992bee42010-07-31 00:32:11 +0100439 if (ret != 0) {
440 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Mark Brown398575d2011-08-03 17:16:11 +0900441 goto err_enable;
Ian Lartey992bee42010-07-31 00:32:11 +0100442 }
443
444 ret = wm8741_reset(codec);
445 if (ret < 0) {
446 dev_err(codec->dev, "Failed to issue reset\n");
Mark Brown398575d2011-08-03 17:16:11 +0900447 goto err_enable;
Ian Lartey992bee42010-07-31 00:32:11 +0100448 }
449
Ian Lartey992bee42010-07-31 00:32:11 +0100450 /* Change some default settings - latch VU */
Mark Browna1b3b5e2010-12-24 16:59:30 +0000451 snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
452 WM8741_UPDATELL, WM8741_UPDATELL);
453 snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
454 WM8741_UPDATELM, WM8741_UPDATELM);
455 snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
456 WM8741_UPDATERL, WM8741_UPDATERL);
Axel Lin9b5999b2011-10-03 11:09:01 +0800457 snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
Mark Browna1b3b5e2010-12-24 16:59:30 +0000458 WM8741_UPDATERM, WM8741_UPDATERM);
Ian Lartey992bee42010-07-31 00:32:11 +0100459
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000460 snd_soc_add_controls(codec, wm8741_snd_controls,
461 ARRAY_SIZE(wm8741_snd_controls));
462 wm8741_add_widgets(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100463
464 dev_dbg(codec->dev, "Successful registration\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 return ret;
Mark Brown398575d2011-08-03 17:16:11 +0900466
467err_enable:
468 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
469err_get:
470 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
471err:
472 return ret;
473}
474
475static int wm8741_remove(struct snd_soc_codec *codec)
476{
477 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
478
479 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
480 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
481
482 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483}
484
485static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
486 .probe = wm8741_probe,
Mark Brown398575d2011-08-03 17:16:11 +0900487 .remove = wm8741_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 .resume = wm8741_resume,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100489 .reg_cache_size = ARRAY_SIZE(wm8741_reg_defaults),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 .reg_word_size = sizeof(u16),
Dimitris Papastamos0aa34b12010-11-08 10:41:53 +0000491 .reg_cache_default = wm8741_reg_defaults,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492};
493
Mark Brown80080ec2011-08-03 17:31:26 +0900494static const struct of_device_id wm8741_of_match[] = {
495 { .compatible = "wlf,wm8741", },
496 { }
497};
498MODULE_DEVICE_TABLE(of, wm8741_of_match);
499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
501static int wm8741_i2c_probe(struct i2c_client *i2c,
502 const struct i2c_device_id *id)
503{
504 struct wm8741_priv *wm8741;
Mark Brown398575d2011-08-03 17:16:11 +0900505 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506
507 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
508 if (wm8741 == NULL)
509 return -ENOMEM;
510
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 i2c_set_clientdata(i2c, wm8741);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 wm8741->control_type = SND_SOC_I2C;
513
Mark Brown398575d2011-08-03 17:16:11 +0900514 ret = snd_soc_register_codec(&i2c->dev,
515 &soc_codec_dev_wm8741, &wm8741_dai, 1);
516 if (ret != 0)
517 goto err;
518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 return ret;
Ian Lartey992bee42010-07-31 00:32:11 +0100520
Ian Lartey992bee42010-07-31 00:32:11 +0100521err:
522 kfree(wm8741);
523 return ret;
524}
525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526static int wm8741_i2c_remove(struct i2c_client *client)
Ian Lartey992bee42010-07-31 00:32:11 +0100527{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 snd_soc_unregister_codec(&client->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 kfree(i2c_get_clientdata(client));
Ian Lartey992bee42010-07-31 00:32:11 +0100530 return 0;
531}
532
533static const struct i2c_device_id wm8741_i2c_id[] = {
534 { "wm8741", 0 },
535 { }
536};
537MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
538
Ian Lartey992bee42010-07-31 00:32:11 +0100539static struct i2c_driver wm8741_i2c_driver = {
540 .driver = {
Mark Brown0473e612011-08-03 16:52:10 +0900541 .name = "wm8741",
Ian Lartey992bee42010-07-31 00:32:11 +0100542 .owner = THIS_MODULE,
Mark Brown80080ec2011-08-03 17:31:26 +0900543 .of_match_table = wm8741_of_match,
Ian Lartey992bee42010-07-31 00:32:11 +0100544 },
545 .probe = wm8741_i2c_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 .remove = wm8741_i2c_remove,
Ian Lartey992bee42010-07-31 00:32:11 +0100547 .id_table = wm8741_i2c_id,
548};
549#endif
550
Mark Brown39e9b8d2011-08-03 17:30:57 +0900551#if defined(CONFIG_SPI_MASTER)
552static int __devinit wm8741_spi_probe(struct spi_device *spi)
553{
554 struct wm8741_priv *wm8741;
555 int ret;
556
557 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
558 if (wm8741 == NULL)
559 return -ENOMEM;
560
561 wm8741->control_type = SND_SOC_SPI;
562 spi_set_drvdata(spi, wm8741);
563
564 ret = snd_soc_register_codec(&spi->dev,
565 &soc_codec_dev_wm8741, &wm8741_dai, 1);
566 if (ret < 0)
567 kfree(wm8741);
568 return ret;
569}
570
571static int __devexit wm8741_spi_remove(struct spi_device *spi)
572{
573 snd_soc_unregister_codec(&spi->dev);
574 kfree(spi_get_drvdata(spi));
575 return 0;
576}
577
578static struct spi_driver wm8741_spi_driver = {
579 .driver = {
580 .name = "wm8741",
581 .owner = THIS_MODULE,
Mark Brown80080ec2011-08-03 17:31:26 +0900582 .of_match_table = wm8741_of_match,
Mark Brown39e9b8d2011-08-03 17:30:57 +0900583 },
584 .probe = wm8741_spi_probe,
585 .remove = __devexit_p(wm8741_spi_remove),
586};
587#endif /* CONFIG_SPI_MASTER */
588
Ian Lartey992bee42010-07-31 00:32:11 +0100589static int __init wm8741_modinit(void)
590{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 int ret = 0;
592
Ian Lartey992bee42010-07-31 00:32:11 +0100593#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
594 ret = i2c_add_driver(&wm8741_i2c_driver);
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100595 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
Ian Lartey992bee42010-07-31 00:32:11 +0100597#endif
Mark Brown39e9b8d2011-08-03 17:30:57 +0900598#if defined(CONFIG_SPI_MASTER)
599 ret = spi_register_driver(&wm8741_spi_driver);
600 if (ret != 0) {
601 printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
602 ret);
603 }
604#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605
606 return ret;
Ian Lartey992bee42010-07-31 00:32:11 +0100607}
608module_init(wm8741_modinit);
609
610static void __exit wm8741_exit(void)
611{
Mark Brown39e9b8d2011-08-03 17:30:57 +0900612#if defined(CONFIG_SPI_MASTER)
613 spi_unregister_driver(&wm8741_spi_driver);
614#endif
Ian Lartey992bee42010-07-31 00:32:11 +0100615#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
616 i2c_del_driver(&wm8741_i2c_driver);
617#endif
618}
619module_exit(wm8741_exit);
620
621MODULE_DESCRIPTION("ASoC WM8741 driver");
622MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
623MODULE_LICENSE("GPL");