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