blob: 3c96af057a3e3390b22dcd4e36948e773db7913f [file] [log] [blame]
Ian Lartey992bee42010-07-31 00:32:11 +01001/*
2 * wm8741.c -- WM8741 ALSA SoC Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2010-1 Wolfson Microelectronics plc
Ian Lartey992bee42010-07-31 00:32:11 +01005 *
6 * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
Mark Brown39e9b8d2011-08-03 17:30:57 +090020#include <linux/spi/spi.h>
Mark Brownfe98c0c2012-09-10 18:00:21 +080021#include <linux/regmap.h>
Ian Lartey992bee42010-07-31 00:32:11 +010022#include <linux/regulator/consumer.h>
23#include <linux/slab.h>
Mark Brown80080ec2011-08-03 17:31:26 +090024#include <linux/of_device.h>
Ian Lartey992bee42010-07-31 00:32:11 +010025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Ian Lartey992bee42010-07-31 00:32:11 +010029#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "wm8741.h"
33
Ian Lartey992bee42010-07-31 00:32:11 +010034#define WM8741_NUM_SUPPLIES 2
35static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
36 "AVDD",
37 "DVDD",
38};
39
Ian Lartey992bee42010-07-31 00:32:11 +010040/* codec private data */
41struct wm8741_priv {
Sergej Sawazkic354b542015-05-13 11:39:01 +020042 struct wm8741_platform_data pdata;
Mark Brownfe98c0c2012-09-10 18:00:21 +080043 struct regmap *regmap;
Ian Lartey992bee42010-07-31 00:32:11 +010044 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
45 unsigned int sysclk;
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +010046 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
Ian Lartey992bee42010-07-31 00:32:11 +010047};
48
Mark Brownfe98c0c2012-09-10 18:00:21 +080049static 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 */
Ian Lartey992bee42010-07-31 00:32:11 +010060};
61
Ian Lartey992bee42010-07-31 00:32:11 +010062static int wm8741_reset(struct snd_soc_codec *codec)
63{
64 return snd_soc_write(codec, WM8741_RESET, 0);
65}
66
67static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
68static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
69
Sergej Sawazkic354b542015-05-13 11:39:01 +020070static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = {
Ian Lartey992bee42010-07-31 00:32:11 +010071SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
72 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
73SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
74 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
75};
76
Sergej Sawazkic354b542015-05-13 11:39:01 +020077static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = {
78SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
79 1, 255, 1, dac_tlv_fine),
80SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
81 0, 511, 1, dac_tlv),
82};
83
84static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = {
85SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
86 1, 255, 1, dac_tlv_fine),
87SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION,
88 0, 511, 1, dac_tlv),
89};
90
Ian Lartey992bee42010-07-31 00:32:11 +010091static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
92SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
93SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
94SND_SOC_DAPM_OUTPUT("VOUTLP"),
95SND_SOC_DAPM_OUTPUT("VOUTLN"),
96SND_SOC_DAPM_OUTPUT("VOUTRP"),
97SND_SOC_DAPM_OUTPUT("VOUTRN"),
98};
99
Mark Brown0e627802011-12-03 17:15:06 +0000100static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
Ian Lartey992bee42010-07-31 00:32:11 +0100101 { "VOUTLP", NULL, "DACL" },
102 { "VOUTLN", NULL, "DACL" },
103 { "VOUTRP", NULL, "DACR" },
104 { "VOUTRN", NULL, "DACR" },
105};
106
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100107static const unsigned int rates_11289[] = {
Sergej Sawazki87870412015-03-24 21:13:22 +0100108 44100, 88200,
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100109};
110
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100111static const struct snd_pcm_hw_constraint_list constraints_11289 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100112 .count = ARRAY_SIZE(rates_11289),
113 .list = rates_11289,
114};
115
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100116static const unsigned int rates_12288[] = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100117 32000, 48000, 96000,
118};
119
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100120static const struct snd_pcm_hw_constraint_list constraints_12288 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100121 .count = ARRAY_SIZE(rates_12288),
122 .list = rates_12288,
123};
124
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100125static const unsigned int rates_16384[] = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100126 32000,
127};
128
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100129static const struct snd_pcm_hw_constraint_list constraints_16384 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100130 .count = ARRAY_SIZE(rates_16384),
131 .list = rates_16384,
132};
133
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100134static const unsigned int rates_16934[] = {
Sergej Sawazki87870412015-03-24 21:13:22 +0100135 44100, 88200,
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100136};
137
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100138static const struct snd_pcm_hw_constraint_list constraints_16934 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100139 .count = ARRAY_SIZE(rates_16934),
140 .list = rates_16934,
141};
142
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100143static const unsigned int rates_18432[] = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100144 48000, 96000,
145};
146
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100147static const struct snd_pcm_hw_constraint_list constraints_18432 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100148 .count = ARRAY_SIZE(rates_18432),
149 .list = rates_18432,
150};
151
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100152static const unsigned int rates_22579[] = {
Sergej Sawazki87870412015-03-24 21:13:22 +0100153 44100, 88200, 176400
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100154};
155
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100156static const struct snd_pcm_hw_constraint_list constraints_22579 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100157 .count = ARRAY_SIZE(rates_22579),
158 .list = rates_22579,
159};
160
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100161static const unsigned int rates_24576[] = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100162 32000, 48000, 96000, 192000
163};
164
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100165static const struct snd_pcm_hw_constraint_list constraints_24576 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100166 .count = ARRAY_SIZE(rates_24576),
167 .list = rates_24576,
168};
169
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100170static const unsigned int rates_36864[] = {
Sergej Sawazki87870412015-03-24 21:13:22 +0100171 48000, 96000, 192000
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100172};
173
Lars-Peter Clausen70bad2c2014-02-05 21:54:35 +0100174static const struct snd_pcm_hw_constraint_list constraints_36864 = {
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100175 .count = ARRAY_SIZE(rates_36864),
176 .list = rates_36864,
Ian Lartey992bee42010-07-31 00:32:11 +0100177};
178
Ian Lartey992bee42010-07-31 00:32:11 +0100179static 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
Sergej Sawazkie369bd02015-06-06 11:25:48 +0200185 if (wm8741->sysclk)
186 snd_pcm_hw_constraint_list(substream->runtime, 0,
187 SNDRV_PCM_HW_PARAM_RATE,
188 wm8741->sysclk_constraints);
Ian Lartey992bee42010-07-31 00:32:11 +0100189
190 return 0;
191}
192
193static int wm8741_hw_params(struct snd_pcm_substream *substream,
194 struct snd_pcm_hw_params *params,
195 struct snd_soc_dai *dai)
196{
Mark Browne6968a12012-04-04 15:58:16 +0100197 struct snd_soc_codec *codec = dai->codec;
Ian Lartey992bee42010-07-31 00:32:11 +0100198 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000199 unsigned int iface;
Ian Lartey992bee42010-07-31 00:32:11 +0100200 int i;
201
Sergej Sawazkie369bd02015-06-06 11:25:48 +0200202 /* 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))
Ian Lartey992bee42010-07-31 00:32:11 +0100214 break;
215 }
216
Sergej Sawazkie369bd02015-06-06 11:25:48 +0200217 if (i == wm8741->sysclk_constraints->count) {
218 dev_err(codec->dev, "LRCLK %d unsupported with MCLK %d\n",
219 params_rate(params), wm8741->sysclk);
Ian Lartey992bee42010-07-31 00:32:11 +0100220 return -EINVAL;
221 }
222
223 /* bit size */
Mark Brown34967ad2014-07-31 12:51:45 +0100224 switch (params_width(params)) {
225 case 16:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000226 iface = 0x0;
Ian Lartey992bee42010-07-31 00:32:11 +0100227 break;
Mark Brown34967ad2014-07-31 12:51:45 +0100228 case 20:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000229 iface = 0x1;
Ian Lartey992bee42010-07-31 00:32:11 +0100230 break;
Mark Brown34967ad2014-07-31 12:51:45 +0100231 case 24:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000232 iface = 0x2;
Ian Lartey992bee42010-07-31 00:32:11 +0100233 break;
Mark Brown34967ad2014-07-31 12:51:45 +0100234 case 32:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000235 iface = 0x3;
Ian Lartey992bee42010-07-31 00:32:11 +0100236 break;
237 default:
238 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
Mark Brown34967ad2014-07-31 12:51:45 +0100239 params_width(params));
Ian Lartey992bee42010-07-31 00:32:11 +0100240 return -EINVAL;
241 }
242
Sergej Sawazkie369bd02015-06-06 11:25:48 +0200243 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d, rate param = %d",
244 params_width(params), params_rate(params));
Ian Lartey992bee42010-07-31 00:32:11 +0100245
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000246 snd_soc_update_bits(codec, WM8741_FORMAT_CONTROL, WM8741_IWL_MASK,
247 iface);
248
Ian Lartey992bee42010-07-31 00:32:11 +0100249 return 0;
250}
251
252static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
253 int clk_id, unsigned int freq, int dir)
254{
255 struct snd_soc_codec *codec = codec_dai->codec;
256 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100257
258 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
259
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100260 switch (freq) {
Sergej Sawazkie369bd02015-06-06 11:25:48 +0200261 case 0:
262 wm8741->sysclk_constraints = NULL;
Axel Lin6f55a042015-07-30 10:24:03 +0800263 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100264 case 11289600:
265 wm8741->sysclk_constraints = &constraints_11289;
Axel Lin6f55a042015-07-30 10:24:03 +0800266 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100267 case 12288000:
268 wm8741->sysclk_constraints = &constraints_12288;
Axel Lin6f55a042015-07-30 10:24:03 +0800269 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100270 case 16384000:
271 wm8741->sysclk_constraints = &constraints_16384;
Axel Lin6f55a042015-07-30 10:24:03 +0800272 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100273 case 16934400:
274 wm8741->sysclk_constraints = &constraints_16934;
Axel Lin6f55a042015-07-30 10:24:03 +0800275 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100276 case 18432000:
277 wm8741->sysclk_constraints = &constraints_18432;
Axel Lin6f55a042015-07-30 10:24:03 +0800278 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100279 case 22579200:
280 case 33868800:
281 wm8741->sysclk_constraints = &constraints_22579;
Axel Lin6f55a042015-07-30 10:24:03 +0800282 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100283 case 24576000:
284 wm8741->sysclk_constraints = &constraints_24576;
Axel Lin6f55a042015-07-30 10:24:03 +0800285 break;
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100286 case 36864000:
287 wm8741->sysclk_constraints = &constraints_36864;
Axel Lin6f55a042015-07-30 10:24:03 +0800288 break;
289 default:
290 return -EINVAL;
Ian Lartey992bee42010-07-31 00:32:11 +0100291 }
Axel Lin6f55a042015-07-30 10:24:03 +0800292
293 wm8741->sysclk = freq;
294 return 0;
Ian Lartey992bee42010-07-31 00:32:11 +0100295}
296
297static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
298 unsigned int fmt)
299{
300 struct snd_soc_codec *codec = codec_dai->codec;
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000301 unsigned int iface;
Ian Lartey992bee42010-07-31 00:32:11 +0100302
303 /* check master/slave audio interface */
304 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
305 case SND_SOC_DAIFMT_CBS_CFS:
306 break;
307 default:
308 return -EINVAL;
309 }
310
311 /* interface format */
312 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
313 case SND_SOC_DAIFMT_I2S:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000314 iface = 0x08;
Ian Lartey992bee42010-07-31 00:32:11 +0100315 break;
316 case SND_SOC_DAIFMT_RIGHT_J:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000317 iface = 0x00;
Ian Lartey992bee42010-07-31 00:32:11 +0100318 break;
319 case SND_SOC_DAIFMT_LEFT_J:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000320 iface = 0x04;
Ian Lartey992bee42010-07-31 00:32:11 +0100321 break;
322 case SND_SOC_DAIFMT_DSP_A:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000323 iface = 0x0C;
Ian Lartey992bee42010-07-31 00:32:11 +0100324 break;
325 case SND_SOC_DAIFMT_DSP_B:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000326 iface = 0x1C;
Ian Lartey992bee42010-07-31 00:32:11 +0100327 break;
328 default:
329 return -EINVAL;
330 }
331
332 /* clock inversion */
333 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
334 case SND_SOC_DAIFMT_NB_NF:
335 break;
336 case SND_SOC_DAIFMT_IB_IF:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000337 iface |= 0x10;
Ian Lartey992bee42010-07-31 00:32:11 +0100338 break;
339 case SND_SOC_DAIFMT_IB_NF:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000340 iface |= 0x20;
Ian Lartey992bee42010-07-31 00:32:11 +0100341 break;
342 case SND_SOC_DAIFMT_NB_IF:
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000343 iface |= 0x30;
Ian Lartey992bee42010-07-31 00:32:11 +0100344 break;
345 default:
346 return -EINVAL;
347 }
348
349
350 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
351 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
352 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
353
Charles Keepaxeaf8abc2017-11-01 11:03:25 +0000354 snd_soc_update_bits(codec, WM8741_FORMAT_CONTROL,
355 WM8741_BCP_MASK | WM8741_LRP_MASK | WM8741_FMT_MASK,
356 iface);
357
Ian Lartey992bee42010-07-31 00:32:11 +0100358 return 0;
359}
360
361#define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
362 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
363 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
364 SNDRV_PCM_RATE_192000)
365
366#define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
367 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
368
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100369static const struct snd_soc_dai_ops wm8741_dai_ops = {
Ian Lartey992bee42010-07-31 00:32:11 +0100370 .startup = wm8741_startup,
371 .hw_params = wm8741_hw_params,
372 .set_sysclk = wm8741_set_dai_sysclk,
373 .set_fmt = wm8741_set_dai_fmt,
374};
375
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000376static struct snd_soc_dai_driver wm8741_dai = {
Ian Lartey30e2d362010-08-20 17:18:44 +0100377 .name = "wm8741",
Ian Lartey992bee42010-07-31 00:32:11 +0100378 .playback = {
379 .stream_name = "Playback",
Sergej Sawazkic354b542015-05-13 11:39:01 +0200380 .channels_min = 2,
Ian Lartey992bee42010-07-31 00:32:11 +0100381 .channels_max = 2,
382 .rates = WM8741_RATES,
383 .formats = WM8741_FORMATS,
384 },
385 .ops = &wm8741_dai_ops,
386};
Ian Lartey992bee42010-07-31 00:32:11 +0100387
388#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000389static int wm8741_resume(struct snd_soc_codec *codec)
Ian Lartey992bee42010-07-31 00:32:11 +0100390{
Axel Lindf3431b2011-10-17 20:16:37 +0800391 snd_soc_cache_sync(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100392 return 0;
393}
394#else
Ian Lartey992bee42010-07-31 00:32:11 +0100395#define wm8741_resume NULL
396#endif
397
Sergej Sawazkic354b542015-05-13 11:39:01 +0200398static int wm8741_configure(struct snd_soc_codec *codec)
399{
400 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
401
402 /* Configure differential mode */
403 switch (wm8741->pdata.diff_mode) {
404 case WM8741_DIFF_MODE_STEREO:
405 case WM8741_DIFF_MODE_STEREO_REVERSED:
406 case WM8741_DIFF_MODE_MONO_LEFT:
407 case WM8741_DIFF_MODE_MONO_RIGHT:
408 snd_soc_update_bits(codec, WM8741_MODE_CONTROL_2,
409 WM8741_DIFF_MASK,
410 wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT);
411 break;
412 default:
413 return -EINVAL;
414 }
415
416 /* Change some default settings - latch VU */
417 snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
418 WM8741_UPDATELL, WM8741_UPDATELL);
419 snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
420 WM8741_UPDATELM, WM8741_UPDATELM);
421 snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
422 WM8741_UPDATERL, WM8741_UPDATERL);
423 snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
424 WM8741_UPDATERM, WM8741_UPDATERM);
425
426 return 0;
427}
428
429static int wm8741_add_controls(struct snd_soc_codec *codec)
430{
431 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
432
433 switch (wm8741->pdata.diff_mode) {
434 case WM8741_DIFF_MODE_STEREO:
435 case WM8741_DIFF_MODE_STEREO_REVERSED:
436 snd_soc_add_codec_controls(codec,
437 wm8741_snd_controls_stereo,
438 ARRAY_SIZE(wm8741_snd_controls_stereo));
439 break;
440 case WM8741_DIFF_MODE_MONO_LEFT:
441 snd_soc_add_codec_controls(codec,
442 wm8741_snd_controls_mono_left,
443 ARRAY_SIZE(wm8741_snd_controls_mono_left));
444 break;
445 case WM8741_DIFF_MODE_MONO_RIGHT:
446 snd_soc_add_codec_controls(codec,
447 wm8741_snd_controls_mono_right,
448 ARRAY_SIZE(wm8741_snd_controls_mono_right));
449 break;
450 default:
451 return -EINVAL;
452 }
453
454 return 0;
455}
456
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000457static int wm8741_probe(struct snd_soc_codec *codec)
Ian Lartey992bee42010-07-31 00:32:11 +0100458{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000459 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
Ian Lartey992bee42010-07-31 00:32:11 +0100460 int ret = 0;
Mark Brown398575d2011-08-03 17:16:11 +0900461
462 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
463 wm8741->supplies);
464 if (ret != 0) {
465 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
466 goto err_get;
467 }
Ian Lartey992bee42010-07-31 00:32:11 +0100468
Ian Lartey992bee42010-07-31 00:32:11 +0100469 ret = wm8741_reset(codec);
470 if (ret < 0) {
471 dev_err(codec->dev, "Failed to issue reset\n");
Mark Brown398575d2011-08-03 17:16:11 +0900472 goto err_enable;
Ian Lartey992bee42010-07-31 00:32:11 +0100473 }
474
Sergej Sawazkic354b542015-05-13 11:39:01 +0200475 ret = wm8741_configure(codec);
476 if (ret < 0) {
477 dev_err(codec->dev, "Failed to change default settings\n");
478 goto err_enable;
479 }
480
481 ret = wm8741_add_controls(codec);
482 if (ret < 0) {
483 dev_err(codec->dev, "Failed to add controls\n");
484 goto err_enable;
485 }
Ian Lartey992bee42010-07-31 00:32:11 +0100486
Ian Lartey992bee42010-07-31 00:32:11 +0100487 dev_dbg(codec->dev, "Successful registration\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 return ret;
Mark Brown398575d2011-08-03 17:16:11 +0900489
490err_enable:
491 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
492err_get:
Mark Brown398575d2011-08-03 17:16:11 +0900493 return ret;
494}
495
496static int wm8741_remove(struct snd_soc_codec *codec)
497{
498 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
499
500 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
Mark Brown398575d2011-08-03 17:16:11 +0900501
502 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503}
504
Julia Lawallf802d6c2016-08-31 23:52:27 +0200505static const struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 .probe = wm8741_probe,
Mark Brown398575d2011-08-03 17:16:11 +0900507 .remove = wm8741_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 .resume = wm8741_resume,
Mark Brown0e627802011-12-03 17:15:06 +0000509
Kuninori Morimoto818d2aa2016-08-08 08:50:19 +0000510 .component_driver = {
511 .dapm_widgets = wm8741_dapm_widgets,
512 .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
513 .dapm_routes = wm8741_dapm_routes,
514 .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
515 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516};
517
Mark Brown80080ec2011-08-03 17:31:26 +0900518static const struct of_device_id wm8741_of_match[] = {
519 { .compatible = "wlf,wm8741", },
520 { }
521};
522MODULE_DEVICE_TABLE(of, wm8741_of_match);
523
Mark Brownfe98c0c2012-09-10 18:00:21 +0800524static const struct regmap_config wm8741_regmap = {
525 .reg_bits = 7,
526 .val_bits = 9,
527 .max_register = WM8741_MAX_REGISTER,
528
529 .reg_defaults = wm8741_reg_defaults,
530 .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults),
531 .cache_type = REGCACHE_RBTREE,
Mark Brownfe98c0c2012-09-10 18:00:21 +0800532};
533
Sergej Sawazkic354b542015-05-13 11:39:01 +0200534static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741)
535{
536 const struct wm8741_platform_data *pdata = dev_get_platdata(dev);
537 u32 diff_mode;
538
539 if (dev->of_node) {
540 if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode)
541 >= 0)
542 wm8741->pdata.diff_mode = diff_mode;
543 } else {
544 if (pdata != NULL)
545 memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata));
546 }
547
548 return 0;
549}
550
Fabio Estevam26090a82013-11-21 12:38:45 -0200551#if IS_ENABLED(CONFIG_I2C)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552static int wm8741_i2c_probe(struct i2c_client *i2c,
553 const struct i2c_device_id *id)
554{
555 struct wm8741_priv *wm8741;
Mark Brownd9780552012-09-10 17:52:59 +0800556 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557
Mark Brown5aefb302011-12-03 17:17:05 +0000558 wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
559 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 if (wm8741 == NULL)
561 return -ENOMEM;
562
Mark Brownd9780552012-09-10 17:52:59 +0800563 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
564 wm8741->supplies[i].supply = wm8741_supply_names[i];
565
566 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
567 wm8741->supplies);
568 if (ret != 0) {
Mark Brownfe98c0c2012-09-10 18:00:21 +0800569 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
570 return ret;
571 }
572
Tushar Beherafd64c452012-11-22 09:38:37 +0530573 wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap);
Mark Brownfe98c0c2012-09-10 18:00:21 +0800574 if (IS_ERR(wm8741->regmap)) {
575 ret = PTR_ERR(wm8741->regmap);
576 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
577 return ret;
Mark Brownd9780552012-09-10 17:52:59 +0800578 }
579
Dan Carpenter2d52d172015-05-20 10:40:35 +0300580 ret = wm8741_set_pdata(&i2c->dev, wm8741);
Sergej Sawazkic354b542015-05-13 11:39:01 +0200581 if (ret != 0) {
582 dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret);
583 return ret;
584 }
585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 i2c_set_clientdata(i2c, wm8741);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587
Mark Brown398575d2011-08-03 17:16:11 +0900588 ret = snd_soc_register_codec(&i2c->dev,
589 &soc_codec_dev_wm8741, &wm8741_dai, 1);
Mark Brown398575d2011-08-03 17:16:11 +0900590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 return ret;
Ian Lartey992bee42010-07-31 00:32:11 +0100592}
593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594static int wm8741_i2c_remove(struct i2c_client *client)
Ian Lartey992bee42010-07-31 00:32:11 +0100595{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 snd_soc_unregister_codec(&client->dev);
Ian Lartey992bee42010-07-31 00:32:11 +0100597 return 0;
598}
599
600static const struct i2c_device_id wm8741_i2c_id[] = {
601 { "wm8741", 0 },
602 { }
603};
604MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
605
Ian Lartey992bee42010-07-31 00:32:11 +0100606static struct i2c_driver wm8741_i2c_driver = {
607 .driver = {
Mark Brown0473e612011-08-03 16:52:10 +0900608 .name = "wm8741",
Mark Brown80080ec2011-08-03 17:31:26 +0900609 .of_match_table = wm8741_of_match,
Ian Lartey992bee42010-07-31 00:32:11 +0100610 },
611 .probe = wm8741_i2c_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 .remove = wm8741_i2c_remove,
Ian Lartey992bee42010-07-31 00:32:11 +0100613 .id_table = wm8741_i2c_id,
614};
615#endif
616
Mark Brown39e9b8d2011-08-03 17:30:57 +0900617#if defined(CONFIG_SPI_MASTER)
Bill Pemberton7a79e942012-12-07 09:26:37 -0500618static int wm8741_spi_probe(struct spi_device *spi)
Mark Brown39e9b8d2011-08-03 17:30:57 +0900619{
620 struct wm8741_priv *wm8741;
Mark Brownd9780552012-09-10 17:52:59 +0800621 int ret, i;
Mark Brown39e9b8d2011-08-03 17:30:57 +0900622
Mark Brown5aefb302011-12-03 17:17:05 +0000623 wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
624 GFP_KERNEL);
Mark Brown39e9b8d2011-08-03 17:30:57 +0900625 if (wm8741 == NULL)
626 return -ENOMEM;
627
Mark Brownd9780552012-09-10 17:52:59 +0800628 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
629 wm8741->supplies[i].supply = wm8741_supply_names[i];
630
Mark Brownfe98c0c2012-09-10 18:00:21 +0800631 ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies),
Mark Brownd9780552012-09-10 17:52:59 +0800632 wm8741->supplies);
633 if (ret != 0) {
634 dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
Mark Brownfe98c0c2012-09-10 18:00:21 +0800635 return ret;
Mark Brownd9780552012-09-10 17:52:59 +0800636 }
637
Tushar Beherafd64c452012-11-22 09:38:37 +0530638 wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap);
Mark Brownfe98c0c2012-09-10 18:00:21 +0800639 if (IS_ERR(wm8741->regmap)) {
640 ret = PTR_ERR(wm8741->regmap);
641 dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
642 return ret;
643 }
644
Dan Carpenter2d52d172015-05-20 10:40:35 +0300645 ret = wm8741_set_pdata(&spi->dev, wm8741);
Sergej Sawazkic354b542015-05-13 11:39:01 +0200646 if (ret != 0) {
647 dev_err(&spi->dev, "Failed to set pdata: %d\n", ret);
648 return ret;
649 }
650
Mark Brown39e9b8d2011-08-03 17:30:57 +0900651 spi_set_drvdata(spi, wm8741);
652
653 ret = snd_soc_register_codec(&spi->dev,
654 &soc_codec_dev_wm8741, &wm8741_dai, 1);
Mark Brown39e9b8d2011-08-03 17:30:57 +0900655 return ret;
656}
657
Bill Pemberton7a79e942012-12-07 09:26:37 -0500658static int wm8741_spi_remove(struct spi_device *spi)
Mark Brown39e9b8d2011-08-03 17:30:57 +0900659{
660 snd_soc_unregister_codec(&spi->dev);
Mark Brown39e9b8d2011-08-03 17:30:57 +0900661 return 0;
662}
663
664static struct spi_driver wm8741_spi_driver = {
665 .driver = {
666 .name = "wm8741",
Mark Brown80080ec2011-08-03 17:31:26 +0900667 .of_match_table = wm8741_of_match,
Mark Brown39e9b8d2011-08-03 17:30:57 +0900668 },
669 .probe = wm8741_spi_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500670 .remove = wm8741_spi_remove,
Mark Brown39e9b8d2011-08-03 17:30:57 +0900671};
672#endif /* CONFIG_SPI_MASTER */
673
Ian Lartey992bee42010-07-31 00:32:11 +0100674static int __init wm8741_modinit(void)
675{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 int ret = 0;
677
Fabio Estevam26090a82013-11-21 12:38:45 -0200678#if IS_ENABLED(CONFIG_I2C)
Ian Lartey992bee42010-07-31 00:32:11 +0100679 ret = i2c_add_driver(&wm8741_i2c_driver);
Ian Lartey3fe4a5e2010-08-27 15:26:27 +0100680 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
Ian Lartey992bee42010-07-31 00:32:11 +0100682#endif
Mark Brown39e9b8d2011-08-03 17:30:57 +0900683#if defined(CONFIG_SPI_MASTER)
684 ret = spi_register_driver(&wm8741_spi_driver);
685 if (ret != 0) {
686 printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
687 ret);
688 }
689#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690
691 return ret;
Ian Lartey992bee42010-07-31 00:32:11 +0100692}
693module_init(wm8741_modinit);
694
695static void __exit wm8741_exit(void)
696{
Mark Brown39e9b8d2011-08-03 17:30:57 +0900697#if defined(CONFIG_SPI_MASTER)
698 spi_unregister_driver(&wm8741_spi_driver);
699#endif
Fabio Estevam26090a82013-11-21 12:38:45 -0200700#if IS_ENABLED(CONFIG_I2C)
Ian Lartey992bee42010-07-31 00:32:11 +0100701 i2c_del_driver(&wm8741_i2c_driver);
702#endif
703}
704module_exit(wm8741_exit);
705
706MODULE_DESCRIPTION("ASoC WM8741 driver");
707MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
708MODULE_LICENSE("GPL");