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