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