Merge remote-tracking branch 'sound-asoc/for-next'
[deliverable/linux.git] / sound / soc / codecs / wm8741.c
CommitLineData
992bee40
IL
1/*
2 * wm8741.c -- WM8741 ALSA SoC Audio driver
3 *
656baaeb 4 * Copyright 2010-1 Wolfson Microelectronics plc
992bee40
IL
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>
39e9b8d2 20#include <linux/spi/spi.h>
fe98c0cf 21#include <linux/regmap.h>
992bee40
IL
22#include <linux/regulator/consumer.h>
23#include <linux/slab.h>
80080ec5 24#include <linux/of_device.h>
992bee40
IL
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
992bee40
IL
29#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "wm8741.h"
33
992bee40
IL
34#define WM8741_NUM_SUPPLIES 2
35static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
36 "AVDD",
37 "DVDD",
38};
39
3fe4a5ee 40#define WM8741_NUM_RATES 6
992bee40
IL
41
42/* codec private data */
43struct wm8741_priv {
c354b54c 44 struct wm8741_platform_data pdata;
fe98c0cf 45 struct regmap *regmap;
992bee40
IL
46 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
47 unsigned int sysclk;
70bad2c7 48 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
992bee40
IL
49};
50
fe98c0cf
MB
51static const struct reg_default wm8741_reg_defaults[] = {
52 { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
53 { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
54 { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
55 { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
56 { 4, 0x0000 }, /* R4 - Volume Control */
57 { 5, 0x000A }, /* R5 - Format Control */
58 { 6, 0x0000 }, /* R6 - Filter Control */
59 { 7, 0x0000 }, /* R7 - Mode Control 1 */
60 { 8, 0x0002 }, /* R8 - Mode Control 2 */
61 { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
992bee40
IL
62};
63
992bee40
IL
64static int wm8741_reset(struct snd_soc_codec *codec)
65{
66 return snd_soc_write(codec, WM8741_RESET, 0);
67}
68
69static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
70static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
71
c354b54c 72static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = {
992bee40
IL
73SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
74 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
75SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
76 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
77};
78
c354b54c
SS
79static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = {
80SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
81 1, 255, 1, dac_tlv_fine),
82SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
83 0, 511, 1, dac_tlv),
84};
85
86static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = {
87SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
88 1, 255, 1, dac_tlv_fine),
89SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION,
90 0, 511, 1, dac_tlv),
91};
92
992bee40
IL
93static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
94SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
95SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
96SND_SOC_DAPM_OUTPUT("VOUTLP"),
97SND_SOC_DAPM_OUTPUT("VOUTLN"),
98SND_SOC_DAPM_OUTPUT("VOUTRP"),
99SND_SOC_DAPM_OUTPUT("VOUTRN"),
100};
101
0e62780f 102static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
992bee40
IL
103 { "VOUTLP", NULL, "DACL" },
104 { "VOUTLN", NULL, "DACL" },
105 { "VOUTRP", NULL, "DACR" },
106 { "VOUTRN", NULL, "DACR" },
107};
108
70bad2c7 109static const unsigned int rates_11289[] = {
8787041d 110 44100, 88200,
3fe4a5ee
IL
111};
112
70bad2c7 113static const struct snd_pcm_hw_constraint_list constraints_11289 = {
3fe4a5ee
IL
114 .count = ARRAY_SIZE(rates_11289),
115 .list = rates_11289,
116};
117
70bad2c7 118static const unsigned int rates_12288[] = {
3fe4a5ee
IL
119 32000, 48000, 96000,
120};
121
70bad2c7 122static const struct snd_pcm_hw_constraint_list constraints_12288 = {
3fe4a5ee
IL
123 .count = ARRAY_SIZE(rates_12288),
124 .list = rates_12288,
125};
126
70bad2c7 127static const unsigned int rates_16384[] = {
3fe4a5ee
IL
128 32000,
129};
130
70bad2c7 131static const struct snd_pcm_hw_constraint_list constraints_16384 = {
3fe4a5ee
IL
132 .count = ARRAY_SIZE(rates_16384),
133 .list = rates_16384,
134};
135
70bad2c7 136static const unsigned int rates_16934[] = {
8787041d 137 44100, 88200,
3fe4a5ee
IL
138};
139
70bad2c7 140static const struct snd_pcm_hw_constraint_list constraints_16934 = {
3fe4a5ee
IL
141 .count = ARRAY_SIZE(rates_16934),
142 .list = rates_16934,
143};
144
70bad2c7 145static const unsigned int rates_18432[] = {
3fe4a5ee
IL
146 48000, 96000,
147};
148
70bad2c7 149static const struct snd_pcm_hw_constraint_list constraints_18432 = {
3fe4a5ee
IL
150 .count = ARRAY_SIZE(rates_18432),
151 .list = rates_18432,
152};
153
70bad2c7 154static const unsigned int rates_22579[] = {
8787041d 155 44100, 88200, 176400
3fe4a5ee
IL
156};
157
70bad2c7 158static const struct snd_pcm_hw_constraint_list constraints_22579 = {
3fe4a5ee
IL
159 .count = ARRAY_SIZE(rates_22579),
160 .list = rates_22579,
161};
162
70bad2c7 163static const unsigned int rates_24576[] = {
3fe4a5ee
IL
164 32000, 48000, 96000, 192000
165};
166
70bad2c7 167static const struct snd_pcm_hw_constraint_list constraints_24576 = {
3fe4a5ee
IL
168 .count = ARRAY_SIZE(rates_24576),
169 .list = rates_24576,
170};
171
70bad2c7 172static const unsigned int rates_36864[] = {
8787041d 173 48000, 96000, 192000
3fe4a5ee
IL
174};
175
70bad2c7 176static const struct snd_pcm_hw_constraint_list constraints_36864 = {
3fe4a5ee
IL
177 .count = ARRAY_SIZE(rates_36864),
178 .list = rates_36864,
992bee40
IL
179};
180
992bee40
IL
181static int wm8741_startup(struct snd_pcm_substream *substream,
182 struct snd_soc_dai *dai)
183{
184 struct snd_soc_codec *codec = dai->codec;
185 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
186
e369bd00
SS
187 if (wm8741->sysclk)
188 snd_pcm_hw_constraint_list(substream->runtime, 0,
189 SNDRV_PCM_HW_PARAM_RATE,
190 wm8741->sysclk_constraints);
992bee40
IL
191
192 return 0;
193}
194
195static int wm8741_hw_params(struct snd_pcm_substream *substream,
196 struct snd_pcm_hw_params *params,
197 struct snd_soc_dai *dai)
198{
e6968a17 199 struct snd_soc_codec *codec = dai->codec;
992bee40
IL
200 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
201 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
202 int i;
203
e369bd00
SS
204 /* The set of sample rates that can be supported depends on the
205 * MCLK supplied to the CODEC - enforce this.
206 */
207 if (!wm8741->sysclk) {
208 dev_err(codec->dev,
209 "No MCLK configured, call set_sysclk() on init or in hw_params\n");
210 return -EINVAL;
211 }
212
213 /* Find a supported LRCLK rate */
214 for (i = 0; i < wm8741->sysclk_constraints->count; i++) {
215 if (wm8741->sysclk_constraints->list[i] == params_rate(params))
992bee40
IL
216 break;
217 }
218
e369bd00
SS
219 if (i == wm8741->sysclk_constraints->count) {
220 dev_err(codec->dev, "LRCLK %d unsupported with MCLK %d\n",
221 params_rate(params), wm8741->sysclk);
992bee40
IL
222 return -EINVAL;
223 }
224
225 /* bit size */
34967ad2
MB
226 switch (params_width(params)) {
227 case 16:
992bee40 228 break;
34967ad2 229 case 20:
992bee40
IL
230 iface |= 0x0001;
231 break;
34967ad2 232 case 24:
992bee40
IL
233 iface |= 0x0002;
234 break;
34967ad2 235 case 32:
992bee40
IL
236 iface |= 0x0003;
237 break;
238 default:
239 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
34967ad2 240 params_width(params));
992bee40
IL
241 return -EINVAL;
242 }
243
e369bd00
SS
244 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d, rate param = %d",
245 params_width(params), params_rate(params));
992bee40
IL
246
247 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
248 return 0;
249}
250
251static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
252 int clk_id, unsigned int freq, int dir)
253{
254 struct snd_soc_codec *codec = codec_dai->codec;
255 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
992bee40
IL
256
257 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
258
3fe4a5ee 259 switch (freq) {
e369bd00
SS
260 case 0:
261 wm8741->sysclk_constraints = NULL;
6f55a041 262 break;
3fe4a5ee
IL
263 case 11289600:
264 wm8741->sysclk_constraints = &constraints_11289;
6f55a041 265 break;
3fe4a5ee
IL
266 case 12288000:
267 wm8741->sysclk_constraints = &constraints_12288;
6f55a041 268 break;
3fe4a5ee
IL
269 case 16384000:
270 wm8741->sysclk_constraints = &constraints_16384;
6f55a041 271 break;
3fe4a5ee
IL
272 case 16934400:
273 wm8741->sysclk_constraints = &constraints_16934;
6f55a041 274 break;
3fe4a5ee
IL
275 case 18432000:
276 wm8741->sysclk_constraints = &constraints_18432;
6f55a041 277 break;
3fe4a5ee
IL
278 case 22579200:
279 case 33868800:
280 wm8741->sysclk_constraints = &constraints_22579;
6f55a041 281 break;
3fe4a5ee
IL
282 case 24576000:
283 wm8741->sysclk_constraints = &constraints_24576;
6f55a041 284 break;
3fe4a5ee
IL
285 case 36864000:
286 wm8741->sysclk_constraints = &constraints_36864;
6f55a041
AL
287 break;
288 default:
289 return -EINVAL;
992bee40 290 }
6f55a041
AL
291
292 wm8741->sysclk = freq;
293 return 0;
992bee40
IL
294}
295
296static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
297 unsigned int fmt)
298{
299 struct snd_soc_codec *codec = codec_dai->codec;
300 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
301
302 /* check master/slave audio interface */
303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
304 case SND_SOC_DAIFMT_CBS_CFS:
305 break;
306 default:
307 return -EINVAL;
308 }
309
310 /* interface format */
311 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
312 case SND_SOC_DAIFMT_I2S:
313 iface |= 0x0008;
314 break;
315 case SND_SOC_DAIFMT_RIGHT_J:
316 break;
317 case SND_SOC_DAIFMT_LEFT_J:
318 iface |= 0x0004;
319 break;
320 case SND_SOC_DAIFMT_DSP_A:
3a340104 321 iface |= 0x000C;
992bee40
IL
322 break;
323 case SND_SOC_DAIFMT_DSP_B:
3a340104 324 iface |= 0x001C;
992bee40
IL
325 break;
326 default:
327 return -EINVAL;
328 }
329
330 /* clock inversion */
331 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
332 case SND_SOC_DAIFMT_NB_NF:
333 break;
334 case SND_SOC_DAIFMT_IB_IF:
335 iface |= 0x0010;
336 break;
337 case SND_SOC_DAIFMT_IB_NF:
338 iface |= 0x0020;
339 break;
340 case SND_SOC_DAIFMT_NB_IF:
341 iface |= 0x0030;
342 break;
343 default:
344 return -EINVAL;
345 }
346
347
348 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
349 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
350 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
351
352 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
353 return 0;
354}
355
356#define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
357 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
358 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
359 SNDRV_PCM_RATE_192000)
360
361#define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
362 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
363
85e7652d 364static const struct snd_soc_dai_ops wm8741_dai_ops = {
992bee40
IL
365 .startup = wm8741_startup,
366 .hw_params = wm8741_hw_params,
367 .set_sysclk = wm8741_set_dai_sysclk,
368 .set_fmt = wm8741_set_dai_fmt,
369};
370
f0fba2ad 371static struct snd_soc_dai_driver wm8741_dai = {
30e2d368 372 .name = "wm8741",
992bee40
IL
373 .playback = {
374 .stream_name = "Playback",
c354b54c 375 .channels_min = 2,
992bee40
IL
376 .channels_max = 2,
377 .rates = WM8741_RATES,
378 .formats = WM8741_FORMATS,
379 },
380 .ops = &wm8741_dai_ops,
381};
992bee40
IL
382
383#ifdef CONFIG_PM
f0fba2ad 384static int wm8741_resume(struct snd_soc_codec *codec)
992bee40 385{
df3431b7 386 snd_soc_cache_sync(codec);
992bee40
IL
387 return 0;
388}
389#else
992bee40
IL
390#define wm8741_resume NULL
391#endif
392
c354b54c
SS
393static int wm8741_configure(struct snd_soc_codec *codec)
394{
395 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
396
397 /* Configure differential mode */
398 switch (wm8741->pdata.diff_mode) {
399 case WM8741_DIFF_MODE_STEREO:
400 case WM8741_DIFF_MODE_STEREO_REVERSED:
401 case WM8741_DIFF_MODE_MONO_LEFT:
402 case WM8741_DIFF_MODE_MONO_RIGHT:
403 snd_soc_update_bits(codec, WM8741_MODE_CONTROL_2,
404 WM8741_DIFF_MASK,
405 wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT);
406 break;
407 default:
408 return -EINVAL;
409 }
410
411 /* Change some default settings - latch VU */
412 snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
413 WM8741_UPDATELL, WM8741_UPDATELL);
414 snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
415 WM8741_UPDATELM, WM8741_UPDATELM);
416 snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
417 WM8741_UPDATERL, WM8741_UPDATERL);
418 snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
419 WM8741_UPDATERM, WM8741_UPDATERM);
420
421 return 0;
422}
423
424static int wm8741_add_controls(struct snd_soc_codec *codec)
425{
426 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
427
428 switch (wm8741->pdata.diff_mode) {
429 case WM8741_DIFF_MODE_STEREO:
430 case WM8741_DIFF_MODE_STEREO_REVERSED:
431 snd_soc_add_codec_controls(codec,
432 wm8741_snd_controls_stereo,
433 ARRAY_SIZE(wm8741_snd_controls_stereo));
434 break;
435 case WM8741_DIFF_MODE_MONO_LEFT:
436 snd_soc_add_codec_controls(codec,
437 wm8741_snd_controls_mono_left,
438 ARRAY_SIZE(wm8741_snd_controls_mono_left));
439 break;
440 case WM8741_DIFF_MODE_MONO_RIGHT:
441 snd_soc_add_codec_controls(codec,
442 wm8741_snd_controls_mono_right,
443 ARRAY_SIZE(wm8741_snd_controls_mono_right));
444 break;
445 default:
446 return -EINVAL;
447 }
448
449 return 0;
450}
451
f0fba2ad 452static int wm8741_probe(struct snd_soc_codec *codec)
992bee40 453{
f0fba2ad 454 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
992bee40 455 int ret = 0;
398575db
MB
456
457 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
458 wm8741->supplies);
459 if (ret != 0) {
460 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
461 goto err_get;
462 }
992bee40 463
f0fba2ad 464 ret = wm8741_reset(codec);
992bee40 465 if (ret < 0) {
f0fba2ad 466 dev_err(codec->dev, "Failed to issue reset\n");
398575db 467 goto err_enable;
992bee40
IL
468 }
469
c354b54c
SS
470 ret = wm8741_configure(codec);
471 if (ret < 0) {
472 dev_err(codec->dev, "Failed to change default settings\n");
473 goto err_enable;
474 }
475
476 ret = wm8741_add_controls(codec);
477 if (ret < 0) {
478 dev_err(codec->dev, "Failed to add controls\n");
479 goto err_enable;
480 }
f0fba2ad 481
f0fba2ad 482 dev_dbg(codec->dev, "Successful registration\n");
992bee40 483 return ret;
398575db
MB
484
485err_enable:
486 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
487err_get:
398575db
MB
488 return ret;
489}
490
491static int wm8741_remove(struct snd_soc_codec *codec)
492{
493 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
494
495 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
398575db
MB
496
497 return 0;
992bee40
IL
498}
499
f802d6c0 500static const struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
992bee40 501 .probe = wm8741_probe,
398575db 502 .remove = wm8741_remove,
992bee40 503 .resume = wm8741_resume,
0e62780f 504
818d2aa1
KM
505 .component_driver = {
506 .dapm_widgets = wm8741_dapm_widgets,
507 .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
508 .dapm_routes = wm8741_dapm_routes,
509 .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
510 },
992bee40 511};
992bee40 512
80080ec5
MB
513static const struct of_device_id wm8741_of_match[] = {
514 { .compatible = "wlf,wm8741", },
515 { }
516};
517MODULE_DEVICE_TABLE(of, wm8741_of_match);
518
fe98c0cf
MB
519static const struct regmap_config wm8741_regmap = {
520 .reg_bits = 7,
521 .val_bits = 9,
522 .max_register = WM8741_MAX_REGISTER,
523
524 .reg_defaults = wm8741_reg_defaults,
525 .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults),
526 .cache_type = REGCACHE_RBTREE,
fe98c0cf
MB
527};
528
c354b54c
SS
529static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741)
530{
531 const struct wm8741_platform_data *pdata = dev_get_platdata(dev);
532 u32 diff_mode;
533
534 if (dev->of_node) {
535 if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode)
536 >= 0)
537 wm8741->pdata.diff_mode = diff_mode;
538 } else {
539 if (pdata != NULL)
540 memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata));
541 }
542
543 return 0;
544}
545
26090a83 546#if IS_ENABLED(CONFIG_I2C)
f0fba2ad
LG
547static int wm8741_i2c_probe(struct i2c_client *i2c,
548 const struct i2c_device_id *id)
992bee40 549{
f0fba2ad 550 struct wm8741_priv *wm8741;
d9780550 551 int ret, i;
992bee40 552
5aefb306
MB
553 wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
554 GFP_KERNEL);
f0fba2ad
LG
555 if (wm8741 == NULL)
556 return -ENOMEM;
992bee40 557
d9780550
MB
558 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
559 wm8741->supplies[i].supply = wm8741_supply_names[i];
560
561 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
562 wm8741->supplies);
563 if (ret != 0) {
fe98c0cf
MB
564 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
565 return ret;
566 }
567
fd64c455 568 wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap);
fe98c0cf
MB
569 if (IS_ERR(wm8741->regmap)) {
570 ret = PTR_ERR(wm8741->regmap);
571 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
572 return ret;
d9780550
MB
573 }
574
2d52d172 575 ret = wm8741_set_pdata(&i2c->dev, wm8741);
c354b54c
SS
576 if (ret != 0) {
577 dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret);
578 return ret;
579 }
580
f0fba2ad 581 i2c_set_clientdata(i2c, wm8741);
992bee40 582
398575db
MB
583 ret = snd_soc_register_codec(&i2c->dev,
584 &soc_codec_dev_wm8741, &wm8741_dai, 1);
992bee40 585
992bee40
IL
586 return ret;
587}
588
f0fba2ad 589static int wm8741_i2c_remove(struct i2c_client *client)
992bee40 590{
f0fba2ad 591 snd_soc_unregister_codec(&client->dev);
992bee40
IL
592 return 0;
593}
594
595static const struct i2c_device_id wm8741_i2c_id[] = {
596 { "wm8741", 0 },
597 { }
598};
599MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
600
992bee40
IL
601static struct i2c_driver wm8741_i2c_driver = {
602 .driver = {
0473e61b 603 .name = "wm8741",
80080ec5 604 .of_match_table = wm8741_of_match,
992bee40
IL
605 },
606 .probe = wm8741_i2c_probe,
f0fba2ad 607 .remove = wm8741_i2c_remove,
992bee40
IL
608 .id_table = wm8741_i2c_id,
609};
610#endif
611
39e9b8d2 612#if defined(CONFIG_SPI_MASTER)
7a79e94e 613static int wm8741_spi_probe(struct spi_device *spi)
39e9b8d2
MB
614{
615 struct wm8741_priv *wm8741;
d9780550 616 int ret, i;
39e9b8d2 617
5aefb306
MB
618 wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
619 GFP_KERNEL);
39e9b8d2
MB
620 if (wm8741 == NULL)
621 return -ENOMEM;
622
d9780550
MB
623 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
624 wm8741->supplies[i].supply = wm8741_supply_names[i];
625
fe98c0cf 626 ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies),
d9780550
MB
627 wm8741->supplies);
628 if (ret != 0) {
629 dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
fe98c0cf
MB
630 return ret;
631 }
632
fd64c455 633 wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap);
fe98c0cf
MB
634 if (IS_ERR(wm8741->regmap)) {
635 ret = PTR_ERR(wm8741->regmap);
636 dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
637 return ret;
d9780550
MB
638 }
639
2d52d172 640 ret = wm8741_set_pdata(&spi->dev, wm8741);
c354b54c
SS
641 if (ret != 0) {
642 dev_err(&spi->dev, "Failed to set pdata: %d\n", ret);
643 return ret;
644 }
645
39e9b8d2
MB
646 spi_set_drvdata(spi, wm8741);
647
648 ret = snd_soc_register_codec(&spi->dev,
649 &soc_codec_dev_wm8741, &wm8741_dai, 1);
39e9b8d2
MB
650 return ret;
651}
652
7a79e94e 653static int wm8741_spi_remove(struct spi_device *spi)
39e9b8d2
MB
654{
655 snd_soc_unregister_codec(&spi->dev);
39e9b8d2
MB
656 return 0;
657}
658
659static struct spi_driver wm8741_spi_driver = {
660 .driver = {
661 .name = "wm8741",
80080ec5 662 .of_match_table = wm8741_of_match,
39e9b8d2
MB
663 },
664 .probe = wm8741_spi_probe,
7a79e94e 665 .remove = wm8741_spi_remove,
39e9b8d2
MB
666};
667#endif /* CONFIG_SPI_MASTER */
668
992bee40
IL
669static int __init wm8741_modinit(void)
670{
f0fba2ad
LG
671 int ret = 0;
672
26090a83 673#if IS_ENABLED(CONFIG_I2C)
992bee40 674 ret = i2c_add_driver(&wm8741_i2c_driver);
3fe4a5ee 675 if (ret != 0)
f0fba2ad 676 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
992bee40 677#endif
39e9b8d2
MB
678#if defined(CONFIG_SPI_MASTER)
679 ret = spi_register_driver(&wm8741_spi_driver);
680 if (ret != 0) {
681 printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
682 ret);
683 }
684#endif
f0fba2ad
LG
685
686 return ret;
992bee40
IL
687}
688module_init(wm8741_modinit);
689
690static void __exit wm8741_exit(void)
691{
39e9b8d2
MB
692#if defined(CONFIG_SPI_MASTER)
693 spi_unregister_driver(&wm8741_spi_driver);
694#endif
26090a83 695#if IS_ENABLED(CONFIG_I2C)
992bee40
IL
696 i2c_del_driver(&wm8741_i2c_driver);
697#endif
698}
699module_exit(wm8741_exit);
700
701MODULE_DESCRIPTION("ASoC WM8741 driver");
702MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
703MODULE_LICENSE("GPL");
This page took 0.332782 seconds and 5 git commands to generate.