blob: 7b8b6ef2f6325a80dc61b54184cbdb9f19f44a40 [file] [log] [blame]
Mark Brownf1c0a022008-08-26 13:05:27 +01001/*
2 * wm8903.c -- WM8903 ALSA SoC Audio driver
3 *
Mark Brown20c5fd32012-06-09 10:03:20 +08004 * Copyright 2008-12 Wolfson Microelectronics
Stephen Warren0bf79ef2012-05-22 16:08:52 -06005 * Copyright 2011-2012 NVIDIA, Inc.
Mark Brownf1c0a022008-08-26 13:05:27 +01006 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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 * TODO:
14 * - TDM mode configuration.
Mark Brownf1c0a022008-08-26 13:05:27 +010015 * - Digital microphone support.
Mark Brownf1c0a022008-08-26 13:05:27 +010016 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
Mark Brown8abd16a2010-03-15 18:25:26 +000021#include <linux/completion.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010022#include <linux/delay.h>
Linus Walleij8f416062015-12-08 23:38:20 +010023#include <linux/gpio/driver.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010024#include <linux/pm.h>
25#include <linux/i2c.h>
Mark Brownee244ce2011-12-02 18:33:32 +000026#include <linux/regmap.h>
Linus Walleijb3bbef42017-03-20 10:13:52 +010027#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Stephen Warren9d35f3e2011-12-02 15:08:40 -070029#include <linux/irq.h>
Lars-Peter Clausen78660af2014-11-09 17:01:01 +010030#include <linux/mutex.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010031#include <sound/core.h>
Mark Brown72453872010-03-15 21:22:58 +000032#include <sound/jack.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010033#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/tlv.h>
36#include <sound/soc.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010037#include <sound/initval.h>
Mark Brown8abd16a2010-03-15 18:25:26 +000038#include <sound/wm8903.h>
Mark Brown2bbb5d62010-12-05 12:50:12 +000039#include <trace/events/asoc.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010040
41#include "wm8903.h"
42
Mark Brownf1c0a022008-08-26 13:05:27 +010043/* Register defaults at reset */
Mark Brownee244ce2011-12-02 18:33:32 +000044static const struct reg_default wm8903_reg_defaults[] = {
45 { 4, 0x0018 }, /* R4 - Bias Control 0 */
46 { 5, 0x0000 }, /* R5 - VMID Control 0 */
47 { 6, 0x0000 }, /* R6 - Mic Bias Control 0 */
48 { 8, 0x0001 }, /* R8 - Analogue DAC 0 */
49 { 10, 0x0001 }, /* R10 - Analogue ADC 0 */
50 { 12, 0x0000 }, /* R12 - Power Management 0 */
51 { 13, 0x0000 }, /* R13 - Power Management 1 */
52 { 14, 0x0000 }, /* R14 - Power Management 2 */
53 { 15, 0x0000 }, /* R15 - Power Management 3 */
54 { 16, 0x0000 }, /* R16 - Power Management 4 */
55 { 17, 0x0000 }, /* R17 - Power Management 5 */
56 { 18, 0x0000 }, /* R18 - Power Management 6 */
57 { 20, 0x0400 }, /* R20 - Clock Rates 0 */
58 { 21, 0x0D07 }, /* R21 - Clock Rates 1 */
59 { 22, 0x0000 }, /* R22 - Clock Rates 2 */
60 { 24, 0x0050 }, /* R24 - Audio Interface 0 */
61 { 25, 0x0242 }, /* R25 - Audio Interface 1 */
62 { 26, 0x0008 }, /* R26 - Audio Interface 2 */
63 { 27, 0x0022 }, /* R27 - Audio Interface 3 */
64 { 30, 0x00C0 }, /* R30 - DAC Digital Volume Left */
65 { 31, 0x00C0 }, /* R31 - DAC Digital Volume Right */
66 { 32, 0x0000 }, /* R32 - DAC Digital 0 */
67 { 33, 0x0000 }, /* R33 - DAC Digital 1 */
68 { 36, 0x00C0 }, /* R36 - ADC Digital Volume Left */
69 { 37, 0x00C0 }, /* R37 - ADC Digital Volume Right */
70 { 38, 0x0000 }, /* R38 - ADC Digital 0 */
71 { 39, 0x0073 }, /* R39 - Digital Microphone 0 */
72 { 40, 0x09BF }, /* R40 - DRC 0 */
73 { 41, 0x3241 }, /* R41 - DRC 1 */
74 { 42, 0x0020 }, /* R42 - DRC 2 */
75 { 43, 0x0000 }, /* R43 - DRC 3 */
76 { 44, 0x0085 }, /* R44 - Analogue Left Input 0 */
77 { 45, 0x0085 }, /* R45 - Analogue Right Input 0 */
78 { 46, 0x0044 }, /* R46 - Analogue Left Input 1 */
79 { 47, 0x0044 }, /* R47 - Analogue Right Input 1 */
80 { 50, 0x0008 }, /* R50 - Analogue Left Mix 0 */
81 { 51, 0x0004 }, /* R51 - Analogue Right Mix 0 */
82 { 52, 0x0000 }, /* R52 - Analogue Spk Mix Left 0 */
83 { 53, 0x0000 }, /* R53 - Analogue Spk Mix Left 1 */
84 { 54, 0x0000 }, /* R54 - Analogue Spk Mix Right 0 */
85 { 55, 0x0000 }, /* R55 - Analogue Spk Mix Right 1 */
86 { 57, 0x002D }, /* R57 - Analogue OUT1 Left */
87 { 58, 0x002D }, /* R58 - Analogue OUT1 Right */
88 { 59, 0x0039 }, /* R59 - Analogue OUT2 Left */
89 { 60, 0x0039 }, /* R60 - Analogue OUT2 Right */
90 { 62, 0x0139 }, /* R62 - Analogue OUT3 Left */
91 { 63, 0x0139 }, /* R63 - Analogue OUT3 Right */
92 { 64, 0x0000 }, /* R65 - Analogue SPK Output Control 0 */
93 { 67, 0x0010 }, /* R67 - DC Servo 0 */
94 { 69, 0x00A4 }, /* R69 - DC Servo 2 */
95 { 90, 0x0000 }, /* R90 - Analogue HP 0 */
96 { 94, 0x0000 }, /* R94 - Analogue Lineout 0 */
97 { 98, 0x0000 }, /* R98 - Charge Pump 0 */
98 { 104, 0x0000 }, /* R104 - Class W 0 */
99 { 108, 0x0000 }, /* R108 - Write Sequencer 0 */
100 { 109, 0x0000 }, /* R109 - Write Sequencer 1 */
101 { 110, 0x0000 }, /* R110 - Write Sequencer 2 */
102 { 111, 0x0000 }, /* R111 - Write Sequencer 3 */
103 { 112, 0x0000 }, /* R112 - Write Sequencer 4 */
104 { 114, 0x0000 }, /* R114 - Control Interface */
105 { 116, 0x00A8 }, /* R116 - GPIO Control 1 */
106 { 117, 0x00A8 }, /* R117 - GPIO Control 2 */
107 { 118, 0x00A8 }, /* R118 - GPIO Control 3 */
108 { 119, 0x0220 }, /* R119 - GPIO Control 4 */
109 { 120, 0x01A0 }, /* R120 - GPIO Control 5 */
110 { 122, 0xFFFF }, /* R122 - Interrupt Status 1 Mask */
111 { 123, 0x0000 }, /* R123 - Interrupt Polarity 1 */
112 { 126, 0x0000 }, /* R126 - Interrupt Control */
113 { 129, 0x0000 }, /* R129 - Control Interface Test 1 */
114 { 149, 0x6810 }, /* R149 - Charge Pump Test 1 */
115 { 164, 0x0028 }, /* R164 - Clock Rate Test 4 */
116 { 172, 0x0000 }, /* R172 - Analogue Output Bias 0 */
Mark Brownf1c0a022008-08-26 13:05:27 +0100117};
118
Linus Walleijb3bbef42017-03-20 10:13:52 +0100119#define WM8903_NUM_SUPPLIES 4
120static const char *wm8903_supply_names[WM8903_NUM_SUPPLIES] = {
121 "AVDD",
122 "CPVDD",
123 "DBVDD",
124 "DCVDD",
125};
126
Mark Brownd58d5d52008-12-10 18:36:42 +0000127struct wm8903_priv {
Stephen Warrenc0eb27c2011-12-02 15:08:38 -0700128 struct wm8903_platform_data *pdata;
Stephen Warren0bf79ef2012-05-22 16:08:52 -0600129 struct device *dev;
Mark Brownee244ce2011-12-02 18:33:32 +0000130 struct regmap *regmap;
Linus Walleijb3bbef42017-03-20 10:13:52 +0100131 struct regulator_bulk_data supplies[WM8903_NUM_SUPPLIES];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000132
Mark Brownd58d5d52008-12-10 18:36:42 +0000133 int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000134 int irq;
Mark Brownd58d5d52008-12-10 18:36:42 +0000135
Lars-Peter Clausen78660af2014-11-09 17:01:01 +0100136 struct mutex lock;
Mark Brown69fff9b2010-12-10 19:17:08 +0000137 int fs;
138 int deemph;
139
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000140 int dcs_pending;
141 int dcs_cache[4];
142
Mark Brownf2c1fe02010-12-10 19:17:07 +0000143 /* Reference count */
Mark Brownd58d5d52008-12-10 18:36:42 +0000144 int class_w_users;
Mark Brownd58d5d52008-12-10 18:36:42 +0000145
Mark Brown72453872010-03-15 21:22:58 +0000146 struct snd_soc_jack *mic_jack;
147 int mic_det;
148 int mic_short;
149 int mic_last_report;
150 int mic_delay;
Stephen Warren7cfe5612011-01-20 13:52:08 -0700151
152#ifdef CONFIG_GPIOLIB
153 struct gpio_chip gpio_chip;
154#endif
Mark Brownd58d5d52008-12-10 18:36:42 +0000155};
156
Mark Brownee244ce2011-12-02 18:33:32 +0000157static bool wm8903_readable_register(struct device *dev, unsigned int reg)
158{
159 switch (reg) {
160 case WM8903_SW_RESET_AND_ID:
161 case WM8903_REVISION_NUMBER:
162 case WM8903_BIAS_CONTROL_0:
163 case WM8903_VMID_CONTROL_0:
164 case WM8903_MIC_BIAS_CONTROL_0:
165 case WM8903_ANALOGUE_DAC_0:
166 case WM8903_ANALOGUE_ADC_0:
167 case WM8903_POWER_MANAGEMENT_0:
168 case WM8903_POWER_MANAGEMENT_1:
169 case WM8903_POWER_MANAGEMENT_2:
170 case WM8903_POWER_MANAGEMENT_3:
171 case WM8903_POWER_MANAGEMENT_4:
172 case WM8903_POWER_MANAGEMENT_5:
173 case WM8903_POWER_MANAGEMENT_6:
174 case WM8903_CLOCK_RATES_0:
175 case WM8903_CLOCK_RATES_1:
176 case WM8903_CLOCK_RATES_2:
177 case WM8903_AUDIO_INTERFACE_0:
178 case WM8903_AUDIO_INTERFACE_1:
179 case WM8903_AUDIO_INTERFACE_2:
180 case WM8903_AUDIO_INTERFACE_3:
181 case WM8903_DAC_DIGITAL_VOLUME_LEFT:
182 case WM8903_DAC_DIGITAL_VOLUME_RIGHT:
183 case WM8903_DAC_DIGITAL_0:
184 case WM8903_DAC_DIGITAL_1:
185 case WM8903_ADC_DIGITAL_VOLUME_LEFT:
186 case WM8903_ADC_DIGITAL_VOLUME_RIGHT:
187 case WM8903_ADC_DIGITAL_0:
188 case WM8903_DIGITAL_MICROPHONE_0:
189 case WM8903_DRC_0:
190 case WM8903_DRC_1:
191 case WM8903_DRC_2:
192 case WM8903_DRC_3:
193 case WM8903_ANALOGUE_LEFT_INPUT_0:
194 case WM8903_ANALOGUE_RIGHT_INPUT_0:
195 case WM8903_ANALOGUE_LEFT_INPUT_1:
196 case WM8903_ANALOGUE_RIGHT_INPUT_1:
197 case WM8903_ANALOGUE_LEFT_MIX_0:
198 case WM8903_ANALOGUE_RIGHT_MIX_0:
199 case WM8903_ANALOGUE_SPK_MIX_LEFT_0:
200 case WM8903_ANALOGUE_SPK_MIX_LEFT_1:
201 case WM8903_ANALOGUE_SPK_MIX_RIGHT_0:
202 case WM8903_ANALOGUE_SPK_MIX_RIGHT_1:
203 case WM8903_ANALOGUE_OUT1_LEFT:
204 case WM8903_ANALOGUE_OUT1_RIGHT:
205 case WM8903_ANALOGUE_OUT2_LEFT:
206 case WM8903_ANALOGUE_OUT2_RIGHT:
207 case WM8903_ANALOGUE_OUT3_LEFT:
208 case WM8903_ANALOGUE_OUT3_RIGHT:
209 case WM8903_ANALOGUE_SPK_OUTPUT_CONTROL_0:
210 case WM8903_DC_SERVO_0:
211 case WM8903_DC_SERVO_2:
212 case WM8903_DC_SERVO_READBACK_1:
213 case WM8903_DC_SERVO_READBACK_2:
214 case WM8903_DC_SERVO_READBACK_3:
215 case WM8903_DC_SERVO_READBACK_4:
216 case WM8903_ANALOGUE_HP_0:
217 case WM8903_ANALOGUE_LINEOUT_0:
218 case WM8903_CHARGE_PUMP_0:
219 case WM8903_CLASS_W_0:
220 case WM8903_WRITE_SEQUENCER_0:
221 case WM8903_WRITE_SEQUENCER_1:
222 case WM8903_WRITE_SEQUENCER_2:
223 case WM8903_WRITE_SEQUENCER_3:
224 case WM8903_WRITE_SEQUENCER_4:
225 case WM8903_CONTROL_INTERFACE:
226 case WM8903_GPIO_CONTROL_1:
227 case WM8903_GPIO_CONTROL_2:
228 case WM8903_GPIO_CONTROL_3:
229 case WM8903_GPIO_CONTROL_4:
230 case WM8903_GPIO_CONTROL_5:
231 case WM8903_INTERRUPT_STATUS_1:
232 case WM8903_INTERRUPT_STATUS_1_MASK:
233 case WM8903_INTERRUPT_POLARITY_1:
234 case WM8903_INTERRUPT_CONTROL:
235 case WM8903_CLOCK_RATE_TEST_4:
236 case WM8903_ANALOGUE_OUTPUT_BIAS_0:
237 return true;
238 default:
239 return false;
240 }
241}
242
243static bool wm8903_volatile_register(struct device *dev, unsigned int reg)
Mark Brownf1c0a022008-08-26 13:05:27 +0100244{
245 switch (reg) {
246 case WM8903_SW_RESET_AND_ID:
247 case WM8903_REVISION_NUMBER:
248 case WM8903_INTERRUPT_STATUS_1:
249 case WM8903_WRITE_SEQUENCER_4:
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000250 case WM8903_DC_SERVO_READBACK_1:
251 case WM8903_DC_SERVO_READBACK_2:
252 case WM8903_DC_SERVO_READBACK_3:
253 case WM8903_DC_SERVO_READBACK_4:
Mark Brown8d50e442009-07-10 23:12:01 +0100254 return 1;
Mark Brownf1c0a022008-08-26 13:05:27 +0100255
256 default:
Mark Brownf1c0a022008-08-26 13:05:27 +0100257 return 0;
Mark Brown8d50e442009-07-10 23:12:01 +0100258 }
Mark Brownf1c0a022008-08-26 13:05:27 +0100259}
260
Mark Brown42768a12009-04-22 18:39:39 +0100261static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
262 struct snd_kcontrol *kcontrol, int event)
263{
264 WARN_ON(event != SND_SOC_DAPM_POST_PMU);
265 mdelay(4);
266
267 return 0;
268}
269
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000270static int wm8903_dcs_event(struct snd_soc_dapm_widget *w,
271 struct snd_kcontrol *kcontrol, int event)
272{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000273 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
274 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000275
276 switch (event) {
277 case SND_SOC_DAPM_POST_PMU:
278 wm8903->dcs_pending |= 1 << w->shift;
279 break;
280 case SND_SOC_DAPM_PRE_PMD:
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000281 snd_soc_component_update_bits(component, WM8903_DC_SERVO_0,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000282 1 << w->shift, 0);
283 break;
284 }
285
286 return 0;
287}
288
289#define WM8903_DCS_MODE_WRITE_STOP 0
290#define WM8903_DCS_MODE_START_STOP 2
291
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000292static void wm8903_seq_notifier(struct snd_soc_component *component,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000293 enum snd_soc_dapm_type event, int subseq)
294{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000295 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000296 int dcs_mode = WM8903_DCS_MODE_WRITE_STOP;
297 int i, val;
298
299 /* Complete any pending DC servo starts */
300 if (wm8903->dcs_pending) {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000301 dev_dbg(component->dev, "Starting DC servo for %x\n",
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000302 wm8903->dcs_pending);
303
304 /* If we've no cached values then we need to do startup */
305 for (i = 0; i < ARRAY_SIZE(wm8903->dcs_cache); i++) {
306 if (!(wm8903->dcs_pending & (1 << i)))
307 continue;
308
309 if (wm8903->dcs_cache[i]) {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000310 dev_dbg(component->dev,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000311 "Restore DC servo %d value %x\n",
312 3 - i, wm8903->dcs_cache[i]);
313
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000314 snd_soc_component_write(component, WM8903_DC_SERVO_4 + i,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000315 wm8903->dcs_cache[i] & 0xff);
316 } else {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000317 dev_dbg(component->dev,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000318 "Calibrate DC servo %d\n", 3 - i);
319 dcs_mode = WM8903_DCS_MODE_START_STOP;
320 }
321 }
322
323 /* Don't trust the cache for analogue */
324 if (wm8903->class_w_users)
325 dcs_mode = WM8903_DCS_MODE_START_STOP;
326
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000327 snd_soc_component_update_bits(component, WM8903_DC_SERVO_2,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000328 WM8903_DCS_MODE_MASK, dcs_mode);
329
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000330 snd_soc_component_update_bits(component, WM8903_DC_SERVO_0,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000331 WM8903_DCS_ENA_MASK, wm8903->dcs_pending);
332
333 switch (dcs_mode) {
334 case WM8903_DCS_MODE_WRITE_STOP:
335 break;
336
337 case WM8903_DCS_MODE_START_STOP:
338 msleep(270);
339
340 /* Cache the measured offsets for digital */
341 if (wm8903->class_w_users)
342 break;
343
344 for (i = 0; i < ARRAY_SIZE(wm8903->dcs_cache); i++) {
345 if (!(wm8903->dcs_pending & (1 << i)))
346 continue;
347
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000348 val = snd_soc_component_read32(component,
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000349 WM8903_DC_SERVO_READBACK_1 + i);
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000350 dev_dbg(component->dev, "DC servo %d: %x\n",
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000351 3 - i, val);
352 wm8903->dcs_cache[i] = val;
353 }
354 break;
355
356 default:
357 pr_warn("DCS mode %d delay not set\n", dcs_mode);
358 break;
359 }
360
361 wm8903->dcs_pending = 0;
362 }
363}
364
Mark Brownf1c0a022008-08-26 13:05:27 +0100365/*
Mark Brownf1c0a022008-08-26 13:05:27 +0100366 * When used with DAC outputs only the WM8903 charge pump supports
367 * operation in class W mode, providing very low power consumption
368 * when used with digital sources. Enable and disable this mode
369 * automatically depending on the mixer configuration.
370 *
371 * All the relevant controls are simple switches.
372 */
373static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
374 struct snd_ctl_elem_value *ucontrol)
375{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000376 struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
377 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownf1c0a022008-08-26 13:05:27 +0100378 u16 reg;
379 int ret;
380
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000381 reg = snd_soc_component_read32(component, WM8903_CLASS_W_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100382
383 /* Turn it off if we're about to enable bypass */
384 if (ucontrol->value.integer.value[0]) {
385 if (wm8903->class_w_users == 0) {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000386 dev_dbg(component->dev, "Disabling Class W\n");
387 snd_soc_component_write(component, WM8903_CLASS_W_0, reg &
Mark Brownf1c0a022008-08-26 13:05:27 +0100388 ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
389 }
390 wm8903->class_w_users++;
391 }
392
393 /* Implement the change */
394 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
395
396 /* If we've just disabled the last bypass path turn Class W on */
397 if (!ucontrol->value.integer.value[0]) {
398 if (wm8903->class_w_users == 1) {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000399 dev_dbg(component->dev, "Enabling Class W\n");
400 snd_soc_component_write(component, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100401 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
402 }
403 wm8903->class_w_users--;
404 }
405
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000406 dev_dbg(component->dev, "Bypass use count now %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +0100407 wm8903->class_w_users);
408
409 return ret;
410}
411
412#define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
Lars-Peter Clausenea3583d2013-06-19 19:33:55 +0200413 SOC_SINGLE_EXT(xname, reg, shift, max, invert, \
414 snd_soc_dapm_get_volsw, wm8903_class_w_put)
Mark Brownf1c0a022008-08-26 13:05:27 +0100415
416
Mark Brown69fff9b2010-12-10 19:17:08 +0000417static int wm8903_deemph[] = { 0, 32000, 44100, 48000 };
418
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000419static int wm8903_set_deemph(struct snd_soc_component *component)
Mark Brown69fff9b2010-12-10 19:17:08 +0000420{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000421 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brown69fff9b2010-12-10 19:17:08 +0000422 int val, i, best;
423
424 /* If we're using deemphasis select the nearest available sample
425 * rate.
426 */
427 if (wm8903->deemph) {
428 best = 1;
429 for (i = 2; i < ARRAY_SIZE(wm8903_deemph); i++) {
430 if (abs(wm8903_deemph[i] - wm8903->fs) <
431 abs(wm8903_deemph[best] - wm8903->fs))
432 best = i;
433 }
434
435 val = best << WM8903_DEEMPH_SHIFT;
436 } else {
437 best = 0;
438 val = 0;
439 }
440
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000441 dev_dbg(component->dev, "Set deemphasis %d (%dHz)\n",
Mark Brown69fff9b2010-12-10 19:17:08 +0000442 best, wm8903_deemph[best]);
443
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000444 return snd_soc_component_update_bits(component, WM8903_DAC_DIGITAL_1,
Mark Brown69fff9b2010-12-10 19:17:08 +0000445 WM8903_DEEMPH_MASK, val);
446}
447
448static int wm8903_get_deemph(struct snd_kcontrol *kcontrol,
449 struct snd_ctl_elem_value *ucontrol)
450{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000451 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
452 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brown69fff9b2010-12-10 19:17:08 +0000453
Takashi Iwai24cc8832015-03-10 12:39:11 +0100454 ucontrol->value.integer.value[0] = wm8903->deemph;
Mark Brown69fff9b2010-12-10 19:17:08 +0000455
456 return 0;
457}
458
459static int wm8903_put_deemph(struct snd_kcontrol *kcontrol,
460 struct snd_ctl_elem_value *ucontrol)
461{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000462 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
463 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Dan Carpenter4d0197a2015-10-13 10:10:18 +0300464 unsigned int deemph = ucontrol->value.integer.value[0];
Mark Brown69fff9b2010-12-10 19:17:08 +0000465 int ret = 0;
466
467 if (deemph > 1)
468 return -EINVAL;
469
Lars-Peter Clausen78660af2014-11-09 17:01:01 +0100470 mutex_lock(&wm8903->lock);
Mark Brown69fff9b2010-12-10 19:17:08 +0000471 if (wm8903->deemph != deemph) {
472 wm8903->deemph = deemph;
473
Kuninori Morimoto58bd2932018-01-29 03:05:11 +0000474 wm8903_set_deemph(component);
Mark Brown69fff9b2010-12-10 19:17:08 +0000475
476 ret = 1;
477 }
Lars-Peter Clausen78660af2014-11-09 17:01:01 +0100478 mutex_unlock(&wm8903->lock);
Mark Brown69fff9b2010-12-10 19:17:08 +0000479
480 return ret;
481}
482
Mark Brownf1c0a022008-08-26 13:05:27 +0100483/* ALSA can only do steps of .01dB */
484static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
485
Alban Bedel00aa0fa2013-03-20 17:37:32 +0100486static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
487
Mark Brown291ce182009-04-22 21:36:14 +0100488static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100489static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
490
491static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
492static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
493static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
494static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
495static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
496
Mark Brown460f4aa2010-12-10 18:42:58 +0000497static const char *hpf_mode_text[] = {
498 "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
499};
500
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100501static SOC_ENUM_SINGLE_DECL(hpf_mode,
502 WM8903_ADC_DIGITAL_0, 5, hpf_mode_text);
Mark Brown460f4aa2010-12-10 18:42:58 +0000503
Mark Browndcf9ada2010-12-10 19:17:06 +0000504static const char *osr_text[] = {
505 "Low power", "High performance"
506};
507
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100508static SOC_ENUM_SINGLE_DECL(adc_osr,
509 WM8903_ANALOGUE_ADC_0, 0, osr_text);
Mark Browndcf9ada2010-12-10 19:17:06 +0000510
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100511static SOC_ENUM_SINGLE_DECL(dac_osr,
512 WM8903_DAC_DIGITAL_1, 0, osr_text);
Mark Browndcf9ada2010-12-10 19:17:06 +0000513
Mark Brownf1c0a022008-08-26 13:05:27 +0100514static const char *drc_slope_text[] = {
515 "1", "1/2", "1/4", "1/8", "1/16", "0"
516};
517
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100518static SOC_ENUM_SINGLE_DECL(drc_slope_r0,
519 WM8903_DRC_2, 3, drc_slope_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100520
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100521static SOC_ENUM_SINGLE_DECL(drc_slope_r1,
522 WM8903_DRC_2, 0, drc_slope_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100523
524static const char *drc_attack_text[] = {
525 "instantaneous",
526 "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
527 "46.4ms", "92.8ms", "185.6ms"
528};
529
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100530static SOC_ENUM_SINGLE_DECL(drc_attack,
531 WM8903_DRC_1, 12, drc_attack_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100532
533static const char *drc_decay_text[] = {
534 "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
535 "23.87s", "47.56s"
536};
537
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100538static SOC_ENUM_SINGLE_DECL(drc_decay,
539 WM8903_DRC_1, 8, drc_decay_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100540
541static const char *drc_ff_delay_text[] = {
542 "5 samples", "9 samples"
543};
544
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100545static SOC_ENUM_SINGLE_DECL(drc_ff_delay,
546 WM8903_DRC_0, 5, drc_ff_delay_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100547
548static const char *drc_qr_decay_text[] = {
549 "0.725ms", "1.45ms", "5.8ms"
550};
551
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100552static SOC_ENUM_SINGLE_DECL(drc_qr_decay,
553 WM8903_DRC_1, 4, drc_qr_decay_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100554
555static const char *drc_smoothing_text[] = {
556 "Low", "Medium", "High"
557};
558
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100559static SOC_ENUM_SINGLE_DECL(drc_smoothing,
560 WM8903_DRC_0, 11, drc_smoothing_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100561
562static const char *soft_mute_text[] = {
563 "Fast (fs/2)", "Slow (fs/32)"
564};
565
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100566static SOC_ENUM_SINGLE_DECL(soft_mute,
567 WM8903_DAC_DIGITAL_1, 10, soft_mute_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100568
569static const char *mute_mode_text[] = {
570 "Hard", "Soft"
571};
572
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100573static SOC_ENUM_SINGLE_DECL(mute_mode,
574 WM8903_DAC_DIGITAL_1, 9, mute_mode_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100575
Mark Brownf1c0a022008-08-26 13:05:27 +0100576static const char *companding_text[] = {
577 "ulaw", "alaw"
578};
579
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100580static SOC_ENUM_SINGLE_DECL(dac_companding,
581 WM8903_AUDIO_INTERFACE_0, 0, companding_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100582
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100583static SOC_ENUM_SINGLE_DECL(adc_companding,
584 WM8903_AUDIO_INTERFACE_0, 2, companding_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100585
586static const char *input_mode_text[] = {
587 "Single-Ended", "Differential Line", "Differential Mic"
588};
589
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100590static SOC_ENUM_SINGLE_DECL(linput_mode_enum,
591 WM8903_ANALOGUE_LEFT_INPUT_1, 0, input_mode_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100592
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100593static SOC_ENUM_SINGLE_DECL(rinput_mode_enum,
594 WM8903_ANALOGUE_RIGHT_INPUT_1, 0, input_mode_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100595
596static const char *linput_mux_text[] = {
597 "IN1L", "IN2L", "IN3L"
598};
599
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100600static SOC_ENUM_SINGLE_DECL(linput_enum,
601 WM8903_ANALOGUE_LEFT_INPUT_1, 2, linput_mux_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100602
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100603static SOC_ENUM_SINGLE_DECL(linput_inv_enum,
604 WM8903_ANALOGUE_LEFT_INPUT_1, 4, linput_mux_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100605
606static const char *rinput_mux_text[] = {
607 "IN1R", "IN2R", "IN3R"
608};
609
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100610static SOC_ENUM_SINGLE_DECL(rinput_enum,
611 WM8903_ANALOGUE_RIGHT_INPUT_1, 2, rinput_mux_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100612
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100613static SOC_ENUM_SINGLE_DECL(rinput_inv_enum,
614 WM8903_ANALOGUE_RIGHT_INPUT_1, 4, rinput_mux_text);
Mark Brownf1c0a022008-08-26 13:05:27 +0100615
616
Mark Brown291ce182009-04-22 21:36:14 +0100617static const char *sidetone_text[] = {
618 "None", "Left", "Right"
619};
620
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100621static SOC_ENUM_SINGLE_DECL(lsidetone_enum,
622 WM8903_DAC_DIGITAL_0, 2, sidetone_text);
Mark Brown291ce182009-04-22 21:36:14 +0100623
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100624static SOC_ENUM_SINGLE_DECL(rsidetone_enum,
625 WM8903_DAC_DIGITAL_0, 0, sidetone_text);
Mark Brown291ce182009-04-22 21:36:14 +0100626
Stephen Warren97945c42011-04-18 20:58:11 -0600627static const char *adcinput_text[] = {
628 "ADC", "DMIC"
629};
630
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100631static SOC_ENUM_SINGLE_DECL(adcinput_enum,
632 WM8903_CLOCK_RATE_TEST_4, 9, adcinput_text);
Stephen Warren97945c42011-04-18 20:58:11 -0600633
Mark Brown1e113bf2011-02-09 13:47:08 +0000634static const char *aif_text[] = {
635 "Left", "Right"
636};
637
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100638static SOC_ENUM_SINGLE_DECL(lcapture_enum,
639 WM8903_AUDIO_INTERFACE_0, 7, aif_text);
Mark Brown1e113bf2011-02-09 13:47:08 +0000640
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100641static SOC_ENUM_SINGLE_DECL(rcapture_enum,
642 WM8903_AUDIO_INTERFACE_0, 6, aif_text);
Mark Brown1e113bf2011-02-09 13:47:08 +0000643
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100644static SOC_ENUM_SINGLE_DECL(lplay_enum,
645 WM8903_AUDIO_INTERFACE_0, 5, aif_text);
Mark Brown1e113bf2011-02-09 13:47:08 +0000646
Takashi Iwaia21bc5c2014-02-18 10:40:16 +0100647static SOC_ENUM_SINGLE_DECL(rplay_enum,
648 WM8903_AUDIO_INTERFACE_0, 4, aif_text);
Mark Brown1e113bf2011-02-09 13:47:08 +0000649
Mark Brownf1c0a022008-08-26 13:05:27 +0100650static const struct snd_kcontrol_new wm8903_snd_controls[] = {
651
652/* Input PGAs - No TLV since the scale depends on PGA mode */
653SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100654 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100655SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
656 0, 31, 0),
657SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
658 6, 1, 0),
659
660SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100661 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100662SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
663 0, 31, 0),
664SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
665 6, 1, 0),
666
667/* ADCs */
Mark Browndcf9ada2010-12-10 19:17:06 +0000668SOC_ENUM("ADC OSR", adc_osr),
Mark Brown460f4aa2010-12-10 18:42:58 +0000669SOC_SINGLE("HPF Switch", WM8903_ADC_DIGITAL_0, 4, 1, 0),
670SOC_ENUM("HPF Mode", hpf_mode),
Mark Brownf1c0a022008-08-26 13:05:27 +0100671SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
672SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
673SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200674SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100675 drc_tlv_thresh),
676SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
677SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
678SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
679SOC_ENUM("DRC Attack Rate", drc_attack),
680SOC_ENUM("DRC Decay Rate", drc_decay),
681SOC_ENUM("DRC FF Delay", drc_ff_delay),
682SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
683SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200684SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
Mark Brownf1c0a022008-08-26 13:05:27 +0100685SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
686SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
687SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200688SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
Mark Brownf1c0a022008-08-26 13:05:27 +0100689SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
690
691SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
Stephen Warren61bf35b2011-05-09 16:32:03 -0600692 WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
Mark Brownf1c0a022008-08-26 13:05:27 +0100693SOC_ENUM("ADC Companding Mode", adc_companding),
694SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
695
Mark Brown291ce182009-04-22 21:36:14 +0100696SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
697 12, 0, digital_sidetone_tlv),
698
Mark Brownf1c0a022008-08-26 13:05:27 +0100699/* DAC */
Mark Browndcf9ada2010-12-10 19:17:06 +0000700SOC_ENUM("DAC OSR", dac_osr),
Mark Brownf1c0a022008-08-26 13:05:27 +0100701SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
702 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
703SOC_ENUM("DAC Soft Mute Rate", soft_mute),
704SOC_ENUM("DAC Mute Mode", mute_mode),
705SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100706SOC_ENUM("DAC Companding Mode", dac_companding),
707SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
Alban Bedel00aa0fa2013-03-20 17:37:32 +0100708SOC_SINGLE_TLV("DAC Boost Volume", WM8903_AUDIO_INTERFACE_0, 9, 3, 0,
709 dac_boost_tlv),
Mark Brown69fff9b2010-12-10 19:17:08 +0000710SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
711 wm8903_get_deemph, wm8903_put_deemph),
Mark Brownf1c0a022008-08-26 13:05:27 +0100712
713/* Headphones */
714SOC_DOUBLE_R("Headphone Switch",
715 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
716 8, 1, 1),
717SOC_DOUBLE_R("Headphone ZC Switch",
718 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
719 6, 1, 0),
720SOC_DOUBLE_R_TLV("Headphone Volume",
721 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
722 0, 63, 0, out_tlv),
723
724/* Line out */
725SOC_DOUBLE_R("Line Out Switch",
726 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
727 8, 1, 1),
728SOC_DOUBLE_R("Line Out ZC Switch",
729 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
730 6, 1, 0),
731SOC_DOUBLE_R_TLV("Line Out Volume",
732 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
733 0, 63, 0, out_tlv),
734
735/* Speaker */
736SOC_DOUBLE_R("Speaker Switch",
737 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
738SOC_DOUBLE_R("Speaker ZC Switch",
739 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
740SOC_DOUBLE_R_TLV("Speaker Volume",
741 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
742 0, 63, 0, out_tlv),
743};
744
Mark Brownf1c0a022008-08-26 13:05:27 +0100745static const struct snd_kcontrol_new linput_mode_mux =
746 SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
747
748static const struct snd_kcontrol_new rinput_mode_mux =
749 SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
750
751static const struct snd_kcontrol_new linput_mux =
752 SOC_DAPM_ENUM("Left Input Mux", linput_enum);
753
754static const struct snd_kcontrol_new linput_inv_mux =
755 SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
756
757static const struct snd_kcontrol_new rinput_mux =
758 SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
759
760static const struct snd_kcontrol_new rinput_inv_mux =
761 SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
762
Mark Brown291ce182009-04-22 21:36:14 +0100763static const struct snd_kcontrol_new lsidetone_mux =
764 SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
765
766static const struct snd_kcontrol_new rsidetone_mux =
767 SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
768
Stephen Warren97945c42011-04-18 20:58:11 -0600769static const struct snd_kcontrol_new adcinput_mux =
770 SOC_DAPM_ENUM("ADC Input", adcinput_enum);
771
Mark Brown1e113bf2011-02-09 13:47:08 +0000772static const struct snd_kcontrol_new lcapture_mux =
773 SOC_DAPM_ENUM("Left Capture Mux", lcapture_enum);
774
775static const struct snd_kcontrol_new rcapture_mux =
776 SOC_DAPM_ENUM("Right Capture Mux", rcapture_enum);
777
778static const struct snd_kcontrol_new lplay_mux =
779 SOC_DAPM_ENUM("Left Playback Mux", lplay_enum);
780
781static const struct snd_kcontrol_new rplay_mux =
782 SOC_DAPM_ENUM("Right Playback Mux", rplay_enum);
783
Mark Brownf1c0a022008-08-26 13:05:27 +0100784static const struct snd_kcontrol_new left_output_mixer[] = {
785SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
786SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
787SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000788SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100789};
790
791static const struct snd_kcontrol_new right_output_mixer[] = {
792SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
793SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
794SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000795SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100796};
797
798static const struct snd_kcontrol_new left_speaker_mixer[] = {
799SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
800SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
801SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
802SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000803 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100804};
805
806static const struct snd_kcontrol_new right_speaker_mixer[] = {
807SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
808SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
809SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
810 1, 1, 0),
811SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000812 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100813};
814
815static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
816SND_SOC_DAPM_INPUT("IN1L"),
817SND_SOC_DAPM_INPUT("IN1R"),
818SND_SOC_DAPM_INPUT("IN2L"),
819SND_SOC_DAPM_INPUT("IN2R"),
820SND_SOC_DAPM_INPUT("IN3L"),
821SND_SOC_DAPM_INPUT("IN3R"),
Stephen Warren97945c42011-04-18 20:58:11 -0600822SND_SOC_DAPM_INPUT("DMICDAT"),
Mark Brownf1c0a022008-08-26 13:05:27 +0100823
824SND_SOC_DAPM_OUTPUT("HPOUTL"),
825SND_SOC_DAPM_OUTPUT("HPOUTR"),
826SND_SOC_DAPM_OUTPUT("LINEOUTL"),
827SND_SOC_DAPM_OUTPUT("LINEOUTR"),
828SND_SOC_DAPM_OUTPUT("LOP"),
829SND_SOC_DAPM_OUTPUT("LON"),
830SND_SOC_DAPM_OUTPUT("ROP"),
831SND_SOC_DAPM_OUTPUT("RON"),
832
Mark Brown5032dc32011-11-27 12:20:08 +0000833SND_SOC_DAPM_SUPPLY("MICBIAS", WM8903_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100834
835SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
836SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
837 &linput_inv_mux),
838SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
839
840SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
841SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
842 &rinput_inv_mux),
843SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
844
845SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
846SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
847
Stephen Warren97945c42011-04-18 20:58:11 -0600848SND_SOC_DAPM_MUX("Left ADC Input", SND_SOC_NOPM, 0, 0, &adcinput_mux),
849SND_SOC_DAPM_MUX("Right ADC Input", SND_SOC_NOPM, 0, 0, &adcinput_mux),
850
Mark Brown1e113bf2011-02-09 13:47:08 +0000851SND_SOC_DAPM_ADC("ADCL", NULL, WM8903_POWER_MANAGEMENT_6, 1, 0),
852SND_SOC_DAPM_ADC("ADCR", NULL, WM8903_POWER_MANAGEMENT_6, 0, 0),
853
854SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lcapture_mux),
855SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rcapture_mux),
856
857SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
858SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100859
Mark Brown291ce182009-04-22 21:36:14 +0100860SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
861SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
862
Mark Brown1e113bf2011-02-09 13:47:08 +0000863SND_SOC_DAPM_AIF_IN("AIFRXL", "Left Playback", 0, SND_SOC_NOPM, 0, 0),
864SND_SOC_DAPM_AIF_IN("AIFRXR", "Right Playback", 0, SND_SOC_NOPM, 0, 0),
865
866SND_SOC_DAPM_MUX("Left Playback Mux", SND_SOC_NOPM, 0, 0, &lplay_mux),
867SND_SOC_DAPM_MUX("Right Playback Mux", SND_SOC_NOPM, 0, 0, &rplay_mux),
868
869SND_SOC_DAPM_DAC("DACL", NULL, WM8903_POWER_MANAGEMENT_6, 3, 0),
870SND_SOC_DAPM_DAC("DACR", NULL, WM8903_POWER_MANAGEMENT_6, 2, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100871
872SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
873 left_output_mixer, ARRAY_SIZE(left_output_mixer)),
874SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
875 right_output_mixer, ARRAY_SIZE(right_output_mixer)),
876
877SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
878 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
879SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
880 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
881
Dilan Lee1b877cb2011-04-07 11:08:38 -0600882SND_SOC_DAPM_PGA_S("Left Headphone Output PGA", 0, WM8903_POWER_MANAGEMENT_2,
883 1, 0, NULL, 0),
884SND_SOC_DAPM_PGA_S("Right Headphone Output PGA", 0, WM8903_POWER_MANAGEMENT_2,
Mark Brown13a99832011-02-09 17:42:55 +0000885 0, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100886
Dilan Lee1b877cb2011-04-07 11:08:38 -0600887SND_SOC_DAPM_PGA_S("Left Line Output PGA", 0, WM8903_POWER_MANAGEMENT_3, 1, 0,
Mark Brown13a99832011-02-09 17:42:55 +0000888 NULL, 0),
Dilan Lee1b877cb2011-04-07 11:08:38 -0600889SND_SOC_DAPM_PGA_S("Right Line Output PGA", 0, WM8903_POWER_MANAGEMENT_3, 0, 0,
Mark Brown13a99832011-02-09 17:42:55 +0000890 NULL, 0),
891
892SND_SOC_DAPM_PGA_S("HPL_RMV_SHORT", 4, WM8903_ANALOGUE_HP_0, 7, 0, NULL, 0),
893SND_SOC_DAPM_PGA_S("HPL_ENA_OUTP", 3, WM8903_ANALOGUE_HP_0, 6, 0, NULL, 0),
Dilan Lee1b877cb2011-04-07 11:08:38 -0600894SND_SOC_DAPM_PGA_S("HPL_ENA_DLY", 2, WM8903_ANALOGUE_HP_0, 5, 0, NULL, 0),
895SND_SOC_DAPM_PGA_S("HPL_ENA", 1, WM8903_ANALOGUE_HP_0, 4, 0, NULL, 0),
Mark Brown13a99832011-02-09 17:42:55 +0000896SND_SOC_DAPM_PGA_S("HPR_RMV_SHORT", 4, WM8903_ANALOGUE_HP_0, 3, 0, NULL, 0),
897SND_SOC_DAPM_PGA_S("HPR_ENA_OUTP", 3, WM8903_ANALOGUE_HP_0, 2, 0, NULL, 0),
Dilan Lee1b877cb2011-04-07 11:08:38 -0600898SND_SOC_DAPM_PGA_S("HPR_ENA_DLY", 2, WM8903_ANALOGUE_HP_0, 1, 0, NULL, 0),
899SND_SOC_DAPM_PGA_S("HPR_ENA", 1, WM8903_ANALOGUE_HP_0, 0, 0, NULL, 0),
Mark Brown13a99832011-02-09 17:42:55 +0000900
901SND_SOC_DAPM_PGA_S("LINEOUTL_RMV_SHORT", 4, WM8903_ANALOGUE_LINEOUT_0, 7, 0,
902 NULL, 0),
903SND_SOC_DAPM_PGA_S("LINEOUTL_ENA_OUTP", 3, WM8903_ANALOGUE_LINEOUT_0, 6, 0,
904 NULL, 0),
Dilan Lee1b877cb2011-04-07 11:08:38 -0600905SND_SOC_DAPM_PGA_S("LINEOUTL_ENA_DLY", 2, WM8903_ANALOGUE_LINEOUT_0, 5, 0,
906 NULL, 0),
907SND_SOC_DAPM_PGA_S("LINEOUTL_ENA", 1, WM8903_ANALOGUE_LINEOUT_0, 4, 0,
Mark Brown13a99832011-02-09 17:42:55 +0000908 NULL, 0),
909SND_SOC_DAPM_PGA_S("LINEOUTR_RMV_SHORT", 4, WM8903_ANALOGUE_LINEOUT_0, 3, 0,
910 NULL, 0),
911SND_SOC_DAPM_PGA_S("LINEOUTR_ENA_OUTP", 3, WM8903_ANALOGUE_LINEOUT_0, 2, 0,
912 NULL, 0),
Dilan Lee1b877cb2011-04-07 11:08:38 -0600913SND_SOC_DAPM_PGA_S("LINEOUTR_ENA_DLY", 2, WM8903_ANALOGUE_LINEOUT_0, 1, 0,
914 NULL, 0),
915SND_SOC_DAPM_PGA_S("LINEOUTR_ENA", 1, WM8903_ANALOGUE_LINEOUT_0, 0, 0,
Mark Brown13a99832011-02-09 17:42:55 +0000916 NULL, 0),
917
Mark Brownc5b6a9f2011-02-09 20:14:42 +0000918SND_SOC_DAPM_SUPPLY("DCS Master", WM8903_DC_SERVO_0, 4, 0, NULL, 0),
919SND_SOC_DAPM_PGA_S("HPL_DCS", 3, SND_SOC_NOPM, 3, 0, wm8903_dcs_event,
920 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
921SND_SOC_DAPM_PGA_S("HPR_DCS", 3, SND_SOC_NOPM, 2, 0, wm8903_dcs_event,
922 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
923SND_SOC_DAPM_PGA_S("LINEOUTL_DCS", 3, SND_SOC_NOPM, 1, 0, wm8903_dcs_event,
924 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
925SND_SOC_DAPM_PGA_S("LINEOUTR_DCS", 3, SND_SOC_NOPM, 0, 0, wm8903_dcs_event,
926 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100927
928SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
929 NULL, 0),
930SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
931 NULL, 0),
932
Mark Brown42768a12009-04-22 18:39:39 +0100933SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
934 wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
Mark Brownc2aef4f2009-04-22 20:04:44 +0100935SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
Mark Brown2c8be5a2011-02-09 17:42:56 +0000936SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8903_CLOCK_RATES_2, 2, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100937};
938
Mark Brownecd015122011-03-27 13:43:45 +0100939static const struct snd_soc_dapm_route wm8903_intercon[] = {
Mark Brownf1c0a022008-08-26 13:05:27 +0100940
Mark Brown2c8be5a2011-02-09 17:42:56 +0000941 { "CLK_DSP", NULL, "CLK_SYS" },
Mark Brown5032dc32011-11-27 12:20:08 +0000942 { "MICBIAS", NULL, "CLK_SYS" },
Mark Brown2c8be5a2011-02-09 17:42:56 +0000943 { "HPL_DCS", NULL, "CLK_SYS" },
944 { "HPR_DCS", NULL, "CLK_SYS" },
945 { "LINEOUTL_DCS", NULL, "CLK_SYS" },
946 { "LINEOUTR_DCS", NULL, "CLK_SYS" },
947
Mark Brownf1c0a022008-08-26 13:05:27 +0100948 { "Left Input Mux", "IN1L", "IN1L" },
949 { "Left Input Mux", "IN2L", "IN2L" },
950 { "Left Input Mux", "IN3L", "IN3L" },
951
952 { "Left Input Inverting Mux", "IN1L", "IN1L" },
953 { "Left Input Inverting Mux", "IN2L", "IN2L" },
954 { "Left Input Inverting Mux", "IN3L", "IN3L" },
955
956 { "Right Input Mux", "IN1R", "IN1R" },
957 { "Right Input Mux", "IN2R", "IN2R" },
958 { "Right Input Mux", "IN3R", "IN3R" },
959
960 { "Right Input Inverting Mux", "IN1R", "IN1R" },
961 { "Right Input Inverting Mux", "IN2R", "IN2R" },
962 { "Right Input Inverting Mux", "IN3R", "IN3R" },
963
964 { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
965 { "Left Input Mode Mux", "Differential Line",
966 "Left Input Mux" },
967 { "Left Input Mode Mux", "Differential Line",
968 "Left Input Inverting Mux" },
969 { "Left Input Mode Mux", "Differential Mic",
970 "Left Input Mux" },
971 { "Left Input Mode Mux", "Differential Mic",
972 "Left Input Inverting Mux" },
973
974 { "Right Input Mode Mux", "Single-Ended",
975 "Right Input Inverting Mux" },
976 { "Right Input Mode Mux", "Differential Line",
977 "Right Input Mux" },
978 { "Right Input Mode Mux", "Differential Line",
979 "Right Input Inverting Mux" },
980 { "Right Input Mode Mux", "Differential Mic",
981 "Right Input Mux" },
982 { "Right Input Mode Mux", "Differential Mic",
983 "Right Input Inverting Mux" },
984
985 { "Left Input PGA", NULL, "Left Input Mode Mux" },
986 { "Right Input PGA", NULL, "Right Input Mode Mux" },
987
Stephen Warren97945c42011-04-18 20:58:11 -0600988 { "Left ADC Input", "ADC", "Left Input PGA" },
989 { "Left ADC Input", "DMIC", "DMICDAT" },
990 { "Right ADC Input", "ADC", "Right Input PGA" },
991 { "Right ADC Input", "DMIC", "DMICDAT" },
992
Mark Brown1e113bf2011-02-09 13:47:08 +0000993 { "Left Capture Mux", "Left", "ADCL" },
994 { "Left Capture Mux", "Right", "ADCR" },
995
996 { "Right Capture Mux", "Left", "ADCL" },
997 { "Right Capture Mux", "Right", "ADCR" },
998
999 { "AIFTXL", NULL, "Left Capture Mux" },
1000 { "AIFTXR", NULL, "Right Capture Mux" },
1001
Stephen Warren97945c42011-04-18 20:58:11 -06001002 { "ADCL", NULL, "Left ADC Input" },
Mark Brownc2aef4f2009-04-22 20:04:44 +01001003 { "ADCL", NULL, "CLK_DSP" },
Stephen Warren97945c42011-04-18 20:58:11 -06001004 { "ADCR", NULL, "Right ADC Input" },
Mark Brownc2aef4f2009-04-22 20:04:44 +01001005 { "ADCR", NULL, "CLK_DSP" },
1006
Mark Brown1e113bf2011-02-09 13:47:08 +00001007 { "Left Playback Mux", "Left", "AIFRXL" },
1008 { "Left Playback Mux", "Right", "AIFRXR" },
1009
1010 { "Right Playback Mux", "Left", "AIFRXL" },
1011 { "Right Playback Mux", "Right", "AIFRXR" },
1012
Mark Brown291ce182009-04-22 21:36:14 +01001013 { "DACL Sidetone", "Left", "ADCL" },
1014 { "DACL Sidetone", "Right", "ADCR" },
1015 { "DACR Sidetone", "Left", "ADCL" },
1016 { "DACR Sidetone", "Right", "ADCR" },
1017
Mark Brown1e113bf2011-02-09 13:47:08 +00001018 { "DACL", NULL, "Left Playback Mux" },
Mark Brown291ce182009-04-22 21:36:14 +01001019 { "DACL", NULL, "DACL Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +01001020 { "DACL", NULL, "CLK_DSP" },
Mark Brown1e113bf2011-02-09 13:47:08 +00001021
1022 { "DACR", NULL, "Right Playback Mux" },
Mark Brown291ce182009-04-22 21:36:14 +01001023 { "DACR", NULL, "DACR Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +01001024 { "DACR", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +01001025
1026 { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
1027 { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
1028 { "Left Output Mixer", "DACL Switch", "DACL" },
1029 { "Left Output Mixer", "DACR Switch", "DACR" },
1030
1031 { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
1032 { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
1033 { "Right Output Mixer", "DACL Switch", "DACL" },
1034 { "Right Output Mixer", "DACR Switch", "DACR" },
1035
1036 { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
1037 { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
1038 { "Left Speaker Mixer", "DACL Switch", "DACL" },
1039 { "Left Speaker Mixer", "DACR Switch", "DACR" },
1040
1041 { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
1042 { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
1043 { "Right Speaker Mixer", "DACL Switch", "DACL" },
1044 { "Right Speaker Mixer", "DACR Switch", "DACR" },
1045
1046 { "Left Line Output PGA", NULL, "Left Output Mixer" },
1047 { "Right Line Output PGA", NULL, "Right Output Mixer" },
1048
1049 { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
1050 { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
1051
1052 { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
1053 { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
1054
Dilan Lee1b877cb2011-04-07 11:08:38 -06001055 { "HPL_ENA", NULL, "Left Headphone Output PGA" },
1056 { "HPR_ENA", NULL, "Right Headphone Output PGA" },
1057 { "HPL_ENA_DLY", NULL, "HPL_ENA" },
1058 { "HPR_ENA_DLY", NULL, "HPR_ENA" },
1059 { "LINEOUTL_ENA", NULL, "Left Line Output PGA" },
1060 { "LINEOUTR_ENA", NULL, "Right Line Output PGA" },
1061 { "LINEOUTL_ENA_DLY", NULL, "LINEOUTL_ENA" },
1062 { "LINEOUTR_ENA_DLY", NULL, "LINEOUTR_ENA" },
Mark Brownf1c0a022008-08-26 13:05:27 +01001063
Mark Brownc5b6a9f2011-02-09 20:14:42 +00001064 { "HPL_DCS", NULL, "DCS Master" },
1065 { "HPR_DCS", NULL, "DCS Master" },
1066 { "LINEOUTL_DCS", NULL, "DCS Master" },
1067 { "LINEOUTR_DCS", NULL, "DCS Master" },
1068
Mark Brown13a99832011-02-09 17:42:55 +00001069 { "HPL_DCS", NULL, "HPL_ENA_DLY" },
1070 { "HPR_DCS", NULL, "HPR_ENA_DLY" },
1071 { "LINEOUTL_DCS", NULL, "LINEOUTL_ENA_DLY" },
1072 { "LINEOUTR_DCS", NULL, "LINEOUTR_ENA_DLY" },
1073
1074 { "HPL_ENA_OUTP", NULL, "HPL_DCS" },
1075 { "HPR_ENA_OUTP", NULL, "HPR_DCS" },
1076 { "LINEOUTL_ENA_OUTP", NULL, "LINEOUTL_DCS" },
1077 { "LINEOUTR_ENA_OUTP", NULL, "LINEOUTR_DCS" },
1078
1079 { "HPL_RMV_SHORT", NULL, "HPL_ENA_OUTP" },
1080 { "HPR_RMV_SHORT", NULL, "HPR_ENA_OUTP" },
1081 { "LINEOUTL_RMV_SHORT", NULL, "LINEOUTL_ENA_OUTP" },
1082 { "LINEOUTR_RMV_SHORT", NULL, "LINEOUTR_ENA_OUTP" },
1083
1084 { "HPOUTL", NULL, "HPL_RMV_SHORT" },
1085 { "HPOUTR", NULL, "HPR_RMV_SHORT" },
1086 { "LINEOUTL", NULL, "LINEOUTL_RMV_SHORT" },
1087 { "LINEOUTR", NULL, "LINEOUTR_RMV_SHORT" },
Mark Brownf1c0a022008-08-26 13:05:27 +01001088
1089 { "LOP", NULL, "Left Speaker PGA" },
1090 { "LON", NULL, "Left Speaker PGA" },
1091
1092 { "ROP", NULL, "Right Speaker PGA" },
1093 { "RON", NULL, "Right Speaker PGA" },
Mark Brown42768a12009-04-22 18:39:39 +01001094
Alban Bedelf1ca4932013-04-09 17:13:59 +02001095 { "Charge Pump", NULL, "CLK_DSP" },
1096
Mark Brown42768a12009-04-22 18:39:39 +01001097 { "Left Headphone Output PGA", NULL, "Charge Pump" },
1098 { "Right Headphone Output PGA", NULL, "Charge Pump" },
1099 { "Left Line Output PGA", NULL, "Charge Pump" },
1100 { "Right Line Output PGA", NULL, "Charge Pump" },
Mark Brownf1c0a022008-08-26 13:05:27 +01001101};
1102
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001103static int wm8903_set_bias_level(struct snd_soc_component *component,
Mark Brownf1c0a022008-08-26 13:05:27 +01001104 enum snd_soc_bias_level level)
1105{
Mark Brownf1c0a022008-08-26 13:05:27 +01001106 switch (level) {
1107 case SND_SOC_BIAS_ON:
Mark Brown66daaa592011-02-10 13:32:58 +00001108 break;
Mark Brown22f226d2011-02-10 14:01:38 +00001109
Mark Brownf1c0a022008-08-26 13:05:27 +01001110 case SND_SOC_BIAS_PREPARE:
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001111 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brown66daaa592011-02-10 13:32:58 +00001112 WM8903_VMID_RES_MASK,
1113 WM8903_VMID_RES_50K);
Mark Brownf1c0a022008-08-26 13:05:27 +01001114 break;
1115
1116 case SND_SOC_BIAS_STANDBY:
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001117 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
1118 snd_soc_component_update_bits(component, WM8903_BIAS_CONTROL_0,
Mark Brown22f226d2011-02-10 14:01:38 +00001119 WM8903_POBCTRL | WM8903_ISEL_MASK |
1120 WM8903_STARTUP_BIAS_ENA |
1121 WM8903_BIAS_ENA,
1122 WM8903_POBCTRL |
1123 (2 << WM8903_ISEL_SHIFT) |
1124 WM8903_STARTUP_BIAS_ENA);
Mark Brown3b1228a2008-12-10 19:27:10 +00001125
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001126 snd_soc_component_update_bits(component,
Mark Brown22f226d2011-02-10 14:01:38 +00001127 WM8903_ANALOGUE_SPK_OUTPUT_CONTROL_0,
1128 WM8903_SPK_DISCHARGE,
1129 WM8903_SPK_DISCHARGE);
Mark Brown4dbfe802009-04-22 20:32:40 +01001130
Mark Brown22f226d2011-02-10 14:01:38 +00001131 msleep(33);
1132
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001133 snd_soc_component_update_bits(component, WM8903_POWER_MANAGEMENT_5,
Mark Brown22f226d2011-02-10 14:01:38 +00001134 WM8903_SPKL_ENA | WM8903_SPKR_ENA,
1135 WM8903_SPKL_ENA | WM8903_SPKR_ENA);
1136
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001137 snd_soc_component_update_bits(component,
Mark Brown22f226d2011-02-10 14:01:38 +00001138 WM8903_ANALOGUE_SPK_OUTPUT_CONTROL_0,
1139 WM8903_SPK_DISCHARGE, 0);
1140
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001141 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brown22f226d2011-02-10 14:01:38 +00001142 WM8903_VMID_TIE_ENA |
1143 WM8903_BUFIO_ENA |
1144 WM8903_VMID_IO_ENA |
1145 WM8903_VMID_SOFT_MASK |
1146 WM8903_VMID_RES_MASK |
1147 WM8903_VMID_BUF_ENA,
1148 WM8903_VMID_TIE_ENA |
1149 WM8903_BUFIO_ENA |
1150 WM8903_VMID_IO_ENA |
1151 (2 << WM8903_VMID_SOFT_SHIFT) |
1152 WM8903_VMID_RES_250K |
1153 WM8903_VMID_BUF_ENA);
1154
1155 msleep(129);
1156
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001157 snd_soc_component_update_bits(component, WM8903_POWER_MANAGEMENT_5,
Mark Brown22f226d2011-02-10 14:01:38 +00001158 WM8903_SPKL_ENA | WM8903_SPKR_ENA,
1159 0);
1160
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001161 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brown22f226d2011-02-10 14:01:38 +00001162 WM8903_VMID_SOFT_MASK, 0);
1163
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001164 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brown22f226d2011-02-10 14:01:38 +00001165 WM8903_VMID_RES_MASK,
1166 WM8903_VMID_RES_50K);
1167
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001168 snd_soc_component_update_bits(component, WM8903_BIAS_CONTROL_0,
Mark Brown22f226d2011-02-10 14:01:38 +00001169 WM8903_BIAS_ENA | WM8903_POBCTRL,
1170 WM8903_BIAS_ENA);
Mark Brownf1c0a022008-08-26 13:05:27 +01001171
Mark Brownf1c0a022008-08-26 13:05:27 +01001172 /* By default no bypass paths are enabled so
1173 * enable Class W support.
1174 */
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001175 dev_dbg(component->dev, "Enabling Class W\n");
1176 snd_soc_component_update_bits(component, WM8903_CLASS_W_0,
Mark Brown524d7692010-12-23 11:17:24 +00001177 WM8903_CP_DYN_FREQ |
1178 WM8903_CP_DYN_V,
1179 WM8903_CP_DYN_FREQ |
1180 WM8903_CP_DYN_V);
Mark Brownf1c0a022008-08-26 13:05:27 +01001181 }
1182
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001183 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brown66daaa592011-02-10 13:32:58 +00001184 WM8903_VMID_RES_MASK,
1185 WM8903_VMID_RES_250K);
Mark Brownf1c0a022008-08-26 13:05:27 +01001186 break;
1187
1188 case SND_SOC_BIAS_OFF:
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001189 snd_soc_component_update_bits(component, WM8903_BIAS_CONTROL_0,
Mark Brownb4d06f42011-02-10 14:20:49 +00001190 WM8903_BIAS_ENA, 0);
1191
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001192 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brownb4d06f42011-02-10 14:20:49 +00001193 WM8903_VMID_SOFT_MASK,
1194 2 << WM8903_VMID_SOFT_SHIFT);
1195
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001196 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brownb4d06f42011-02-10 14:20:49 +00001197 WM8903_VMID_BUF_ENA, 0);
1198
1199 msleep(290);
1200
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001201 snd_soc_component_update_bits(component, WM8903_VMID_CONTROL_0,
Mark Brownb4d06f42011-02-10 14:20:49 +00001202 WM8903_VMID_TIE_ENA | WM8903_BUFIO_ENA |
1203 WM8903_VMID_IO_ENA | WM8903_VMID_RES_MASK |
1204 WM8903_VMID_SOFT_MASK |
1205 WM8903_VMID_BUF_ENA, 0);
1206
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001207 snd_soc_component_update_bits(component, WM8903_BIAS_CONTROL_0,
Mark Brownb4d06f42011-02-10 14:20:49 +00001208 WM8903_STARTUP_BIAS_ENA, 0);
Mark Brownf1c0a022008-08-26 13:05:27 +01001209 break;
1210 }
1211
Mark Brownf1c0a022008-08-26 13:05:27 +01001212 return 0;
1213}
1214
1215static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1216 int clk_id, unsigned int freq, int dir)
1217{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001218 struct snd_soc_component *component = codec_dai->component;
1219 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownf1c0a022008-08-26 13:05:27 +01001220
1221 wm8903->sysclk = freq;
1222
1223 return 0;
1224}
1225
1226static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
1227 unsigned int fmt)
1228{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001229 struct snd_soc_component *component = codec_dai->component;
1230 u16 aif1 = snd_soc_component_read32(component, WM8903_AUDIO_INTERFACE_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001231
1232 aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
1233 WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
1234
1235 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1236 case SND_SOC_DAIFMT_CBS_CFS:
1237 break;
1238 case SND_SOC_DAIFMT_CBS_CFM:
1239 aif1 |= WM8903_LRCLK_DIR;
1240 break;
1241 case SND_SOC_DAIFMT_CBM_CFM:
1242 aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
1243 break;
1244 case SND_SOC_DAIFMT_CBM_CFS:
1245 aif1 |= WM8903_BCLK_DIR;
1246 break;
1247 default:
1248 return -EINVAL;
1249 }
1250
1251 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1252 case SND_SOC_DAIFMT_DSP_A:
1253 aif1 |= 0x3;
1254 break;
1255 case SND_SOC_DAIFMT_DSP_B:
1256 aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
1257 break;
1258 case SND_SOC_DAIFMT_I2S:
1259 aif1 |= 0x2;
1260 break;
1261 case SND_SOC_DAIFMT_RIGHT_J:
1262 aif1 |= 0x1;
1263 break;
1264 case SND_SOC_DAIFMT_LEFT_J:
1265 break;
1266 default:
1267 return -EINVAL;
1268 }
1269
1270 /* Clock inversion */
1271 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1272 case SND_SOC_DAIFMT_DSP_A:
1273 case SND_SOC_DAIFMT_DSP_B:
1274 /* frame inversion not valid for DSP modes */
1275 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1276 case SND_SOC_DAIFMT_NB_NF:
1277 break;
1278 case SND_SOC_DAIFMT_IB_NF:
1279 aif1 |= WM8903_AIF_BCLK_INV;
1280 break;
1281 default:
1282 return -EINVAL;
1283 }
1284 break;
1285 case SND_SOC_DAIFMT_I2S:
1286 case SND_SOC_DAIFMT_RIGHT_J:
1287 case SND_SOC_DAIFMT_LEFT_J:
1288 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1289 case SND_SOC_DAIFMT_NB_NF:
1290 break;
1291 case SND_SOC_DAIFMT_IB_IF:
1292 aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
1293 break;
1294 case SND_SOC_DAIFMT_IB_NF:
1295 aif1 |= WM8903_AIF_BCLK_INV;
1296 break;
1297 case SND_SOC_DAIFMT_NB_IF:
1298 aif1 |= WM8903_AIF_LRCLK_INV;
1299 break;
1300 default:
1301 return -EINVAL;
1302 }
1303 break;
1304 default:
1305 return -EINVAL;
1306 }
1307
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001308 snd_soc_component_write(component, WM8903_AUDIO_INTERFACE_1, aif1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001309
1310 return 0;
1311}
1312
1313static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1314{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001315 struct snd_soc_component *component = codec_dai->component;
Mark Brownf1c0a022008-08-26 13:05:27 +01001316 u16 reg;
1317
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001318 reg = snd_soc_component_read32(component, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001319
1320 if (mute)
1321 reg |= WM8903_DAC_MUTE;
1322 else
1323 reg &= ~WM8903_DAC_MUTE;
1324
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001325 snd_soc_component_write(component, WM8903_DAC_DIGITAL_1, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001326
1327 return 0;
1328}
1329
1330/* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
1331 * for optimal performance so we list the lower rates first and match
1332 * on the last match we find. */
1333static struct {
1334 int div;
1335 int rate;
1336 int mode;
1337 int mclk_div;
1338} clk_sys_ratios[] = {
1339 { 64, 0x0, 0x0, 1 },
1340 { 68, 0x0, 0x1, 1 },
1341 { 125, 0x0, 0x2, 1 },
1342 { 128, 0x1, 0x0, 1 },
1343 { 136, 0x1, 0x1, 1 },
1344 { 192, 0x2, 0x0, 1 },
1345 { 204, 0x2, 0x1, 1 },
1346
1347 { 64, 0x0, 0x0, 2 },
1348 { 68, 0x0, 0x1, 2 },
1349 { 125, 0x0, 0x2, 2 },
1350 { 128, 0x1, 0x0, 2 },
1351 { 136, 0x1, 0x1, 2 },
1352 { 192, 0x2, 0x0, 2 },
1353 { 204, 0x2, 0x1, 2 },
1354
1355 { 250, 0x2, 0x2, 1 },
1356 { 256, 0x3, 0x0, 1 },
1357 { 272, 0x3, 0x1, 1 },
1358 { 384, 0x4, 0x0, 1 },
1359 { 408, 0x4, 0x1, 1 },
1360 { 375, 0x4, 0x2, 1 },
1361 { 512, 0x5, 0x0, 1 },
1362 { 544, 0x5, 0x1, 1 },
1363 { 500, 0x5, 0x2, 1 },
1364 { 768, 0x6, 0x0, 1 },
1365 { 816, 0x6, 0x1, 1 },
1366 { 750, 0x6, 0x2, 1 },
1367 { 1024, 0x7, 0x0, 1 },
1368 { 1088, 0x7, 0x1, 1 },
1369 { 1000, 0x7, 0x2, 1 },
1370 { 1408, 0x8, 0x0, 1 },
1371 { 1496, 0x8, 0x1, 1 },
1372 { 1536, 0x9, 0x0, 1 },
1373 { 1632, 0x9, 0x1, 1 },
1374 { 1500, 0x9, 0x2, 1 },
1375
1376 { 250, 0x2, 0x2, 2 },
1377 { 256, 0x3, 0x0, 2 },
1378 { 272, 0x3, 0x1, 2 },
1379 { 384, 0x4, 0x0, 2 },
1380 { 408, 0x4, 0x1, 2 },
1381 { 375, 0x4, 0x2, 2 },
1382 { 512, 0x5, 0x0, 2 },
1383 { 544, 0x5, 0x1, 2 },
1384 { 500, 0x5, 0x2, 2 },
1385 { 768, 0x6, 0x0, 2 },
1386 { 816, 0x6, 0x1, 2 },
1387 { 750, 0x6, 0x2, 2 },
1388 { 1024, 0x7, 0x0, 2 },
1389 { 1088, 0x7, 0x1, 2 },
1390 { 1000, 0x7, 0x2, 2 },
1391 { 1408, 0x8, 0x0, 2 },
1392 { 1496, 0x8, 0x1, 2 },
1393 { 1536, 0x9, 0x0, 2 },
1394 { 1632, 0x9, 0x1, 2 },
1395 { 1500, 0x9, 0x2, 2 },
1396};
1397
1398/* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
1399static struct {
1400 int ratio;
1401 int div;
1402} bclk_divs[] = {
1403 { 10, 0 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001404 { 20, 2 },
1405 { 30, 3 },
1406 { 40, 4 },
1407 { 50, 5 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001408 { 60, 7 },
1409 { 80, 8 },
1410 { 100, 9 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001411 { 120, 11 },
1412 { 160, 12 },
1413 { 200, 13 },
1414 { 220, 14 },
1415 { 240, 15 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001416 { 300, 17 },
1417 { 320, 18 },
1418 { 440, 19 },
1419 { 480, 20 },
1420};
1421
1422/* Sample rates for DSP */
1423static struct {
1424 int rate;
1425 int value;
1426} sample_rates[] = {
1427 { 8000, 0 },
1428 { 11025, 1 },
1429 { 12000, 2 },
1430 { 16000, 3 },
1431 { 22050, 4 },
1432 { 24000, 5 },
1433 { 32000, 6 },
1434 { 44100, 7 },
1435 { 48000, 8 },
1436 { 88200, 9 },
1437 { 96000, 10 },
1438 { 0, 0 },
1439};
1440
Mark Brownf1c0a022008-08-26 13:05:27 +01001441static int wm8903_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001442 struct snd_pcm_hw_params *params,
1443 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001444{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001445 struct snd_soc_component *component = dai->component;
1446 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownf1c0a022008-08-26 13:05:27 +01001447 int fs = params_rate(params);
1448 int bclk;
1449 int bclk_div;
1450 int i;
1451 int dsp_config;
1452 int clk_config;
1453 int best_val;
1454 int cur_val;
1455 int clk_sys;
1456
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001457 u16 aif1 = snd_soc_component_read32(component, WM8903_AUDIO_INTERFACE_1);
1458 u16 aif2 = snd_soc_component_read32(component, WM8903_AUDIO_INTERFACE_2);
1459 u16 aif3 = snd_soc_component_read32(component, WM8903_AUDIO_INTERFACE_3);
1460 u16 clock0 = snd_soc_component_read32(component, WM8903_CLOCK_RATES_0);
1461 u16 clock1 = snd_soc_component_read32(component, WM8903_CLOCK_RATES_1);
1462 u16 dac_digital1 = snd_soc_component_read32(component, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001463
Mark Brown9e792612009-06-12 17:27:07 +01001464 /* Enable sloping stopband filter for low sample rates */
1465 if (fs <= 24000)
1466 dac_digital1 |= WM8903_DAC_SB_FILT;
1467 else
1468 dac_digital1 &= ~WM8903_DAC_SB_FILT;
1469
Mark Brownf1c0a022008-08-26 13:05:27 +01001470 /* Configure sample rate logic for DSP - choose nearest rate */
1471 dsp_config = 0;
1472 best_val = abs(sample_rates[dsp_config].rate - fs);
1473 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1474 cur_val = abs(sample_rates[i].rate - fs);
1475 if (cur_val <= best_val) {
1476 dsp_config = i;
1477 best_val = cur_val;
1478 }
1479 }
1480
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001481 dev_dbg(component->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
Mark Brownf1c0a022008-08-26 13:05:27 +01001482 clock1 &= ~WM8903_SAMPLE_RATE_MASK;
1483 clock1 |= sample_rates[dsp_config].value;
1484
1485 aif1 &= ~WM8903_AIF_WL_MASK;
1486 bclk = 2 * fs;
Mark Brown6139ea22014-07-31 12:52:53 +01001487 switch (params_width(params)) {
1488 case 16:
Mark Brownf1c0a022008-08-26 13:05:27 +01001489 bclk *= 16;
1490 break;
Mark Brown6139ea22014-07-31 12:52:53 +01001491 case 20:
Mark Brownf1c0a022008-08-26 13:05:27 +01001492 bclk *= 20;
1493 aif1 |= 0x4;
1494 break;
Mark Brown6139ea22014-07-31 12:52:53 +01001495 case 24:
Mark Brownf1c0a022008-08-26 13:05:27 +01001496 bclk *= 24;
1497 aif1 |= 0x8;
1498 break;
Mark Brown6139ea22014-07-31 12:52:53 +01001499 case 32:
Mark Brownf1c0a022008-08-26 13:05:27 +01001500 bclk *= 32;
1501 aif1 |= 0xc;
1502 break;
1503 default:
1504 return -EINVAL;
1505 }
1506
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001507 dev_dbg(component->dev, "MCLK = %dHz, target sample rate = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001508 wm8903->sysclk, fs);
1509
1510 /* We may not have an MCLK which allows us to generate exactly
1511 * the clock we want, particularly with USB derived inputs, so
1512 * approximate.
1513 */
1514 clk_config = 0;
1515 best_val = abs((wm8903->sysclk /
1516 (clk_sys_ratios[0].mclk_div *
1517 clk_sys_ratios[0].div)) - fs);
1518 for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
1519 cur_val = abs((wm8903->sysclk /
1520 (clk_sys_ratios[i].mclk_div *
1521 clk_sys_ratios[i].div)) - fs);
1522
1523 if (cur_val <= best_val) {
1524 clk_config = i;
1525 best_val = cur_val;
1526 }
1527 }
1528
1529 if (clk_sys_ratios[clk_config].mclk_div == 2) {
1530 clock0 |= WM8903_MCLKDIV2;
1531 clk_sys = wm8903->sysclk / 2;
1532 } else {
1533 clock0 &= ~WM8903_MCLKDIV2;
1534 clk_sys = wm8903->sysclk;
1535 }
1536
1537 clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
1538 WM8903_CLK_SYS_MODE_MASK);
1539 clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
1540 clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
1541
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001542 dev_dbg(component->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001543 clk_sys_ratios[clk_config].rate,
1544 clk_sys_ratios[clk_config].mode,
1545 clk_sys_ratios[clk_config].div);
1546
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001547 dev_dbg(component->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
Mark Brownf1c0a022008-08-26 13:05:27 +01001548
1549 /* We may not get quite the right frequency if using
1550 * approximate clocks so look for the closest match that is
1551 * higher than the target (we need to ensure that there enough
1552 * BCLKs to clock out the samples).
1553 */
1554 bclk_div = 0;
1555 best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
1556 i = 1;
1557 while (i < ARRAY_SIZE(bclk_divs)) {
1558 cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
1559 if (cur_val < 0) /* BCLK table is sorted */
1560 break;
1561 bclk_div = i;
1562 best_val = cur_val;
1563 i++;
1564 }
1565
1566 aif2 &= ~WM8903_BCLK_DIV_MASK;
1567 aif3 &= ~WM8903_LRCLK_RATE_MASK;
1568
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001569 dev_dbg(component->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001570 bclk_divs[bclk_div].ratio / 10, bclk,
1571 (clk_sys * 10) / bclk_divs[bclk_div].ratio);
1572
1573 aif2 |= bclk_divs[bclk_div].div;
1574 aif3 |= bclk / fs;
1575
Mark Brown69fff9b2010-12-10 19:17:08 +00001576 wm8903->fs = params_rate(params);
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001577 wm8903_set_deemph(component);
Mark Brown69fff9b2010-12-10 19:17:08 +00001578
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001579 snd_soc_component_write(component, WM8903_CLOCK_RATES_0, clock0);
1580 snd_soc_component_write(component, WM8903_CLOCK_RATES_1, clock1);
1581 snd_soc_component_write(component, WM8903_AUDIO_INTERFACE_1, aif1);
1582 snd_soc_component_write(component, WM8903_AUDIO_INTERFACE_2, aif2);
1583 snd_soc_component_write(component, WM8903_AUDIO_INTERFACE_3, aif3);
1584 snd_soc_component_write(component, WM8903_DAC_DIGITAL_1, dac_digital1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001585
1586 return 0;
1587}
1588
Mark Brown72453872010-03-15 21:22:58 +00001589/**
1590 * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
1591 *
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001592 * @component: WM8903 component
Mark Brown72453872010-03-15 21:22:58 +00001593 * @jack: jack to report detection events on
1594 * @det: value to report for presence detection
1595 * @shrt: value to report for short detection
1596 *
1597 * Enable microphone detection via IRQ on the WM8903. If GPIOs are
1598 * being used to bring out signals to the processor then only platform
1599 * data configuration is needed for WM8903 and processor GPIOs should
1600 * be configured using snd_soc_jack_add_gpios() instead.
1601 *
1602 * The current threasholds for detection should be configured using
1603 * micdet_cfg in the platform data. Using this function will force on
1604 * the microphone bias for the device.
1605 */
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001606int wm8903_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *jack,
Mark Brown72453872010-03-15 21:22:58 +00001607 int det, int shrt)
1608{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001609 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brown69266862010-03-22 16:37:01 +00001610 int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
Mark Brown72453872010-03-15 21:22:58 +00001611
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001612 dev_dbg(component->dev, "Enabling microphone detection: %x %x\n",
Mark Brown72453872010-03-15 21:22:58 +00001613 det, shrt);
1614
1615 /* Store the configuration */
1616 wm8903->mic_jack = jack;
1617 wm8903->mic_det = det;
1618 wm8903->mic_short = shrt;
1619
1620 /* Enable interrupts we've got a report configured for */
1621 if (det)
1622 irq_mask &= ~WM8903_MICDET_EINT;
1623 if (shrt)
1624 irq_mask &= ~WM8903_MICSHRT_EINT;
1625
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001626 snd_soc_component_update_bits(component, WM8903_INTERRUPT_STATUS_1_MASK,
Mark Brown72453872010-03-15 21:22:58 +00001627 WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
1628 irq_mask);
1629
Stephen Warren3088e3b2011-02-10 15:37:14 -07001630 if (det || shrt) {
Mark Brown69266862010-03-22 16:37:01 +00001631 /* Enable mic detection, this may not have been set through
1632 * platform data (eg, if the defaults are OK). */
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001633 snd_soc_component_update_bits(component, WM8903_WRITE_SEQUENCER_0,
Mark Brown69266862010-03-22 16:37:01 +00001634 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001635 snd_soc_component_update_bits(component, WM8903_MIC_BIAS_CONTROL_0,
Mark Brown69266862010-03-22 16:37:01 +00001636 WM8903_MICDET_ENA, WM8903_MICDET_ENA);
1637 } else {
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001638 snd_soc_component_update_bits(component, WM8903_MIC_BIAS_CONTROL_0,
Mark Brown69266862010-03-22 16:37:01 +00001639 WM8903_MICDET_ENA, 0);
1640 }
Mark Brown72453872010-03-15 21:22:58 +00001641
1642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(wm8903_mic_detect);
1645
Mark Brown8abd16a2010-03-15 18:25:26 +00001646static irqreturn_t wm8903_irq(int irq, void *data)
1647{
Mark Browne373cbf2012-06-09 10:06:11 +08001648 struct wm8903_priv *wm8903 = data;
1649 int mic_report, ret;
1650 unsigned int int_val, mask, int_pol;
Mark Brown8abd16a2010-03-15 18:25:26 +00001651
Mark Browne373cbf2012-06-09 10:06:11 +08001652 ret = regmap_read(wm8903->regmap, WM8903_INTERRUPT_STATUS_1_MASK,
1653 &mask);
1654 if (ret != 0) {
1655 dev_err(wm8903->dev, "Failed to read IRQ mask: %d\n", ret);
1656 return IRQ_NONE;
1657 }
1658
1659 ret = regmap_read(wm8903->regmap, WM8903_INTERRUPT_STATUS_1, &int_val);
1660 if (ret != 0) {
1661 dev_err(wm8903->dev, "Failed to read IRQ status: %d\n", ret);
1662 return IRQ_NONE;
1663 }
1664
1665 int_val &= ~mask;
Mark Brown8abd16a2010-03-15 18:25:26 +00001666
Mark Brown72453872010-03-15 21:22:58 +00001667 if (int_val & WM8903_WSEQ_BUSY_EINT) {
Mark Browne373cbf2012-06-09 10:06:11 +08001668 dev_warn(wm8903->dev, "Write sequencer done\n");
Mark Brown8abd16a2010-03-15 18:25:26 +00001669 }
1670
Mark Brown72453872010-03-15 21:22:58 +00001671 /*
1672 * The rest is microphone jack detection. We need to manually
1673 * invert the polarity of the interrupt after each event - to
1674 * simplify the code keep track of the last state we reported
1675 * and just invert the relevant bits in both the report and
1676 * the polarity register.
1677 */
1678 mic_report = wm8903->mic_last_report;
Mark Browne373cbf2012-06-09 10:06:11 +08001679 ret = regmap_read(wm8903->regmap, WM8903_INTERRUPT_POLARITY_1,
1680 &int_pol);
1681 if (ret != 0) {
1682 dev_err(wm8903->dev, "Failed to read interrupt polarity: %d\n",
1683 ret);
1684 return IRQ_HANDLED;
1685 }
Mark Brown72453872010-03-15 21:22:58 +00001686
Mark Brown1435b942010-12-23 01:56:20 +00001687#ifndef CONFIG_SND_SOC_WM8903_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00001688 if (int_val & (WM8903_MICSHRT_EINT | WM8903_MICDET_EINT))
Mark Browne373cbf2012-06-09 10:06:11 +08001689 trace_snd_soc_jack_irq(dev_name(wm8903->dev));
Mark Brown1435b942010-12-23 01:56:20 +00001690#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00001691
Mark Brown72453872010-03-15 21:22:58 +00001692 if (int_val & WM8903_MICSHRT_EINT) {
Mark Browne373cbf2012-06-09 10:06:11 +08001693 dev_dbg(wm8903->dev, "Microphone short (pol=%x)\n", int_pol);
Mark Brown72453872010-03-15 21:22:58 +00001694
1695 mic_report ^= wm8903->mic_short;
1696 int_pol ^= WM8903_MICSHRT_INV;
1697 }
1698
1699 if (int_val & WM8903_MICDET_EINT) {
Mark Browne373cbf2012-06-09 10:06:11 +08001700 dev_dbg(wm8903->dev, "Microphone detect (pol=%x)\n", int_pol);
Mark Brown72453872010-03-15 21:22:58 +00001701
1702 mic_report ^= wm8903->mic_det;
1703 int_pol ^= WM8903_MICDET_INV;
1704
1705 msleep(wm8903->mic_delay);
1706 }
1707
Mark Browne373cbf2012-06-09 10:06:11 +08001708 regmap_update_bits(wm8903->regmap, WM8903_INTERRUPT_POLARITY_1,
1709 WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
Mark Brown72453872010-03-15 21:22:58 +00001710
1711 snd_soc_jack_report(wm8903->mic_jack, mic_report,
1712 wm8903->mic_short | wm8903->mic_det);
1713
1714 wm8903->mic_last_report = mic_report;
1715
Mark Brown8abd16a2010-03-15 18:25:26 +00001716 return IRQ_HANDLED;
1717}
1718
Mark Brownf1c0a022008-08-26 13:05:27 +01001719#define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
1720 SNDRV_PCM_RATE_11025 | \
1721 SNDRV_PCM_RATE_16000 | \
1722 SNDRV_PCM_RATE_22050 | \
1723 SNDRV_PCM_RATE_32000 | \
1724 SNDRV_PCM_RATE_44100 | \
1725 SNDRV_PCM_RATE_48000 | \
1726 SNDRV_PCM_RATE_88200 | \
1727 SNDRV_PCM_RATE_96000)
1728
1729#define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
1730 SNDRV_PCM_RATE_11025 | \
1731 SNDRV_PCM_RATE_16000 | \
1732 SNDRV_PCM_RATE_22050 | \
1733 SNDRV_PCM_RATE_32000 | \
1734 SNDRV_PCM_RATE_44100 | \
1735 SNDRV_PCM_RATE_48000)
1736
1737#define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1738 SNDRV_PCM_FMTBIT_S20_3LE |\
1739 SNDRV_PCM_FMTBIT_S24_LE)
1740
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001741static const struct snd_soc_dai_ops wm8903_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +08001742 .hw_params = wm8903_hw_params,
1743 .digital_mute = wm8903_digital_mute,
1744 .set_fmt = wm8903_set_dai_fmt,
1745 .set_sysclk = wm8903_set_dai_sysclk,
1746};
1747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001748static struct snd_soc_dai_driver wm8903_dai = {
1749 .name = "wm8903-hifi",
Mark Brownf1c0a022008-08-26 13:05:27 +01001750 .playback = {
1751 .stream_name = "Playback",
1752 .channels_min = 2,
1753 .channels_max = 2,
1754 .rates = WM8903_PLAYBACK_RATES,
1755 .formats = WM8903_FORMATS,
1756 },
1757 .capture = {
1758 .stream_name = "Capture",
1759 .channels_min = 2,
1760 .channels_max = 2,
1761 .rates = WM8903_CAPTURE_RATES,
1762 .formats = WM8903_FORMATS,
1763 },
Eric Miao6335d052009-03-03 09:41:00 +08001764 .ops = &wm8903_dai_ops,
Mark Brown0d960e82009-04-16 10:08:39 +01001765 .symmetric_rates = 1,
Mark Brownf1c0a022008-08-26 13:05:27 +01001766};
Mark Brownf1c0a022008-08-26 13:05:27 +01001767
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001768static int wm8903_resume(struct snd_soc_component *component)
Mark Brownf1c0a022008-08-26 13:05:27 +01001769{
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001770 struct wm8903_priv *wm8903 = snd_soc_component_get_drvdata(component);
Mark Brownf1c0a022008-08-26 13:05:27 +01001771
Mark Brownee244ce2011-12-02 18:33:32 +00001772 regcache_sync(wm8903->regmap);
Mark Brown45e96752011-12-02 18:23:37 +00001773
Mark Brownf1c0a022008-08-26 13:05:27 +01001774 return 0;
1775}
1776
Stephen Warren7cfe5612011-01-20 13:52:08 -07001777#ifdef CONFIG_GPIOLIB
Stephen Warren7cfe5612011-01-20 13:52:08 -07001778static int wm8903_gpio_request(struct gpio_chip *chip, unsigned offset)
1779{
1780 if (offset >= WM8903_NUM_GPIO)
1781 return -EINVAL;
1782
1783 return 0;
1784}
1785
1786static int wm8903_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
1787{
Linus Walleij8f416062015-12-08 23:38:20 +01001788 struct wm8903_priv *wm8903 = gpiochip_get_data(chip);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001789 unsigned int mask, val;
Axel Lin385bd932011-12-31 11:01:41 +08001790 int ret;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001791
1792 mask = WM8903_GP1_FN_MASK | WM8903_GP1_DIR_MASK;
1793 val = (WM8903_GPn_FN_GPIO_INPUT << WM8903_GP1_FN_SHIFT) |
1794 WM8903_GP1_DIR;
1795
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001796 ret = regmap_update_bits(wm8903->regmap,
1797 WM8903_GPIO_CONTROL_1 + offset, mask, val);
Axel Lin385bd932011-12-31 11:01:41 +08001798 if (ret < 0)
1799 return ret;
1800
1801 return 0;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001802}
1803
1804static int wm8903_gpio_get(struct gpio_chip *chip, unsigned offset)
1805{
Linus Walleij8f416062015-12-08 23:38:20 +01001806 struct wm8903_priv *wm8903 = gpiochip_get_data(chip);
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001807 unsigned int reg;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001808
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001809 regmap_read(wm8903->regmap, WM8903_GPIO_CONTROL_1 + offset, &reg);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001810
Linus Walleijb70381c2015-12-22 15:50:49 +01001811 return !!((reg & WM8903_GP1_LVL_MASK) >> WM8903_GP1_LVL_SHIFT);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001812}
1813
1814static int wm8903_gpio_direction_out(struct gpio_chip *chip,
1815 unsigned offset, int value)
1816{
Linus Walleij8f416062015-12-08 23:38:20 +01001817 struct wm8903_priv *wm8903 = gpiochip_get_data(chip);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001818 unsigned int mask, val;
Axel Lin385bd932011-12-31 11:01:41 +08001819 int ret;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001820
1821 mask = WM8903_GP1_FN_MASK | WM8903_GP1_DIR_MASK | WM8903_GP1_LVL_MASK;
1822 val = (WM8903_GPn_FN_GPIO_OUTPUT << WM8903_GP1_FN_SHIFT) |
1823 (value << WM8903_GP2_LVL_SHIFT);
1824
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001825 ret = regmap_update_bits(wm8903->regmap,
1826 WM8903_GPIO_CONTROL_1 + offset, mask, val);
Axel Lin385bd932011-12-31 11:01:41 +08001827 if (ret < 0)
1828 return ret;
1829
1830 return 0;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001831}
1832
1833static void wm8903_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1834{
Linus Walleij8f416062015-12-08 23:38:20 +01001835 struct wm8903_priv *wm8903 = gpiochip_get_data(chip);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001836
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001837 regmap_update_bits(wm8903->regmap, WM8903_GPIO_CONTROL_1 + offset,
1838 WM8903_GP1_LVL_MASK,
1839 !!value << WM8903_GP1_LVL_SHIFT);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001840}
1841
Julia Lawallc59b24f2016-09-11 14:14:42 +02001842static const struct gpio_chip wm8903_template_chip = {
Stephen Warren7cfe5612011-01-20 13:52:08 -07001843 .label = "wm8903",
1844 .owner = THIS_MODULE,
1845 .request = wm8903_gpio_request,
1846 .direction_input = wm8903_gpio_direction_in,
1847 .get = wm8903_gpio_get,
1848 .direction_output = wm8903_gpio_direction_out,
1849 .set = wm8903_gpio_set,
1850 .can_sleep = 1,
1851};
1852
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001853static void wm8903_init_gpio(struct wm8903_priv *wm8903)
Stephen Warren7cfe5612011-01-20 13:52:08 -07001854{
Stephen Warrenc0eb27c2011-12-02 15:08:38 -07001855 struct wm8903_platform_data *pdata = wm8903->pdata;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001856 int ret;
1857
1858 wm8903->gpio_chip = wm8903_template_chip;
1859 wm8903->gpio_chip.ngpio = WM8903_NUM_GPIO;
Linus Walleij58383c782015-11-04 09:56:26 +01001860 wm8903->gpio_chip.parent = wm8903->dev;
Stephen Warren7cfe5612011-01-20 13:52:08 -07001861
Stephen Warrendb817782011-12-02 15:08:39 -07001862 if (pdata->gpio_base)
Stephen Warren7cfe5612011-01-20 13:52:08 -07001863 wm8903->gpio_chip.base = pdata->gpio_base;
1864 else
1865 wm8903->gpio_chip.base = -1;
1866
Linus Walleij8f416062015-12-08 23:38:20 +01001867 ret = gpiochip_add_data(&wm8903->gpio_chip, wm8903);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001868 if (ret != 0)
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001869 dev_err(wm8903->dev, "Failed to add GPIOs: %d\n", ret);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001870}
1871
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001872static void wm8903_free_gpio(struct wm8903_priv *wm8903)
Stephen Warren7cfe5612011-01-20 13:52:08 -07001873{
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001874 gpiochip_remove(&wm8903->gpio_chip);
Stephen Warren7cfe5612011-01-20 13:52:08 -07001875}
1876#else
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001877static void wm8903_init_gpio(struct wm8903_priv *wm8903)
Stephen Warren7cfe5612011-01-20 13:52:08 -07001878{
1879}
1880
Stephen Warren0bf79ef2012-05-22 16:08:52 -06001881static void wm8903_free_gpio(struct wm8903_priv *wm8903)
Stephen Warren7cfe5612011-01-20 13:52:08 -07001882{
1883}
1884#endif
1885
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00001886static const struct snd_soc_component_driver soc_component_dev_wm8903 = {
1887 .resume = wm8903_resume,
1888 .set_bias_level = wm8903_set_bias_level,
1889 .seq_notifier = wm8903_seq_notifier,
1890 .controls = wm8903_snd_controls,
1891 .num_controls = ARRAY_SIZE(wm8903_snd_controls),
1892 .dapm_widgets = wm8903_dapm_widgets,
1893 .num_dapm_widgets = ARRAY_SIZE(wm8903_dapm_widgets),
1894 .dapm_routes = wm8903_intercon,
1895 .num_dapm_routes = ARRAY_SIZE(wm8903_intercon),
1896 .suspend_bias_off = 1,
1897 .idle_bias_on = 1,
1898 .use_pmdown_time = 1,
1899 .endianness = 1,
1900 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001901};
1902
Mark Brownee244ce2011-12-02 18:33:32 +00001903static const struct regmap_config wm8903_regmap = {
1904 .reg_bits = 8,
1905 .val_bits = 16,
1906
1907 .max_register = WM8903_MAX_REGISTER,
1908 .volatile_reg = wm8903_volatile_register,
1909 .readable_reg = wm8903_readable_register,
1910
1911 .cache_type = REGCACHE_RBTREE,
1912 .reg_defaults = wm8903_reg_defaults,
1913 .num_reg_defaults = ARRAY_SIZE(wm8903_reg_defaults),
1914};
1915
Stephen Warren9d35f3e2011-12-02 15:08:40 -07001916static int wm8903_set_pdata_irq_trigger(struct i2c_client *i2c,
1917 struct wm8903_platform_data *pdata)
1918{
1919 struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
1920 if (!irq_data) {
1921 dev_err(&i2c->dev, "Invalid IRQ: %d\n",
1922 i2c->irq);
1923 return -EINVAL;
1924 }
1925
1926 switch (irqd_get_trigger_type(irq_data)) {
1927 case IRQ_TYPE_NONE:
Mark Brown6664ee12011-12-06 10:30:24 +00001928 default:
Stephen Warren9d35f3e2011-12-02 15:08:40 -07001929 /*
1930 * We assume the controller imposes no restrictions,
1931 * so we are able to select active-high
1932 */
1933 /* Fall-through */
1934 case IRQ_TYPE_LEVEL_HIGH:
1935 pdata->irq_active_low = false;
1936 break;
1937 case IRQ_TYPE_LEVEL_LOW:
1938 pdata->irq_active_low = true;
1939 break;
Stephen Warren9d35f3e2011-12-02 15:08:40 -07001940 }
1941
1942 return 0;
1943}
1944
Stephen Warren5d680b32011-12-02 15:08:41 -07001945static int wm8903_set_pdata_from_of(struct i2c_client *i2c,
1946 struct wm8903_platform_data *pdata)
1947{
1948 const struct device_node *np = i2c->dev.of_node;
1949 u32 val32;
1950 int i;
1951
1952 if (of_property_read_u32(np, "micdet-cfg", &val32) >= 0)
1953 pdata->micdet_cfg = val32;
1954
1955 if (of_property_read_u32(np, "micdet-delay", &val32) >= 0)
1956 pdata->micdet_delay = val32;
1957
1958 if (of_property_read_u32_array(np, "gpio-cfg", pdata->gpio_cfg,
1959 ARRAY_SIZE(pdata->gpio_cfg)) >= 0) {
1960 /*
1961 * In device tree: 0 means "write 0",
1962 * 0xffffffff means "don't touch".
1963 *
1964 * In platform data: 0 means "don't touch",
1965 * 0x8000 means "write 0".
1966 *
1967 * Note: WM8903_GPIO_CONFIG_ZERO == 0x8000.
1968 *
1969 * Convert from DT to pdata representation here,
1970 * so no other code needs to change.
1971 */
1972 for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
1973 if (pdata->gpio_cfg[i] == 0) {
1974 pdata->gpio_cfg[i] = WM8903_GPIO_CONFIG_ZERO;
1975 } else if (pdata->gpio_cfg[i] == 0xffffffff) {
1976 pdata->gpio_cfg[i] = 0;
1977 } else if (pdata->gpio_cfg[i] > 0x7fff) {
1978 dev_err(&i2c->dev, "Invalid gpio-cfg[%d] %x\n",
1979 i, pdata->gpio_cfg[i]);
1980 return -EINVAL;
1981 }
1982 }
1983 }
1984
1985 return 0;
1986}
1987
Bill Pemberton7a79e942012-12-07 09:26:37 -05001988static int wm8903_i2c_probe(struct i2c_client *i2c,
1989 const struct i2c_device_id *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001990{
Stephen Warrenc0eb27c2011-12-02 15:08:38 -07001991 struct wm8903_platform_data *pdata = dev_get_platdata(&i2c->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 struct wm8903_priv *wm8903;
Mark Brownb7c95d92012-06-09 10:15:10 +08001993 int trigger;
Mark Brown20c5fd32012-06-09 10:03:20 +08001994 bool mic_gpio = false;
Mark Brownb7c95d92012-06-09 10:15:10 +08001995 unsigned int val, irq_pol;
Mark Brown20c5fd32012-06-09 10:03:20 +08001996 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001997
Markus Elfring017b9b32017-11-24 10:40:43 +01001998 wm8903 = devm_kzalloc(&i2c->dev, sizeof(*wm8903), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999 if (wm8903 == NULL)
2000 return -ENOMEM;
Lars-Peter Clausen78660af2014-11-09 17:01:01 +01002001
2002 mutex_init(&wm8903->lock);
Stephen Warren0bf79ef2012-05-22 16:08:52 -06002003 wm8903->dev = &i2c->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002004
Mark Brown7d116682012-06-09 10:21:16 +08002005 wm8903->regmap = devm_regmap_init_i2c(i2c, &wm8903_regmap);
Mark Brownee244ce2011-12-02 18:33:32 +00002006 if (IS_ERR(wm8903->regmap)) {
2007 ret = PTR_ERR(wm8903->regmap);
2008 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2009 ret);
2010 return ret;
2011 }
2012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002013 i2c_set_clientdata(i2c, wm8903);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002014
Stephen Warrenc0eb27c2011-12-02 15:08:38 -07002015 /* If no platform data was supplied, create storage for defaults */
2016 if (pdata) {
2017 wm8903->pdata = pdata;
2018 } else {
Markus Elfring017b9b32017-11-24 10:40:43 +01002019 wm8903->pdata = devm_kzalloc(&i2c->dev, sizeof(*wm8903->pdata),
2020 GFP_KERNEL);
Markus Elfringcce7c0a2017-11-24 10:05:43 +01002021 if (!wm8903->pdata)
Stephen Warrenc0eb27c2011-12-02 15:08:38 -07002022 return -ENOMEM;
Stephen Warren9d35f3e2011-12-02 15:08:40 -07002023
2024 if (i2c->irq) {
2025 ret = wm8903_set_pdata_irq_trigger(i2c, wm8903->pdata);
2026 if (ret != 0)
2027 return ret;
2028 }
Stephen Warren5d680b32011-12-02 15:08:41 -07002029
2030 if (i2c->dev.of_node) {
2031 ret = wm8903_set_pdata_from_of(i2c, wm8903->pdata);
2032 if (ret != 0)
2033 return ret;
2034 }
Stephen Warrenc0eb27c2011-12-02 15:08:38 -07002035 }
2036
Mark Brown20c5fd32012-06-09 10:03:20 +08002037 pdata = wm8903->pdata;
2038
Linus Walleijb3bbef42017-03-20 10:13:52 +01002039 for (i = 0; i < ARRAY_SIZE(wm8903->supplies); i++)
2040 wm8903->supplies[i].supply = wm8903_supply_names[i];
2041
2042 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8903->supplies),
2043 wm8903->supplies);
2044 if (ret != 0) {
2045 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
2046 return ret;
2047 }
2048
2049 ret = regulator_bulk_enable(ARRAY_SIZE(wm8903->supplies),
2050 wm8903->supplies);
2051 if (ret != 0) {
2052 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
2053 return ret;
2054 }
2055
Mark Brown7d46a522011-12-02 18:39:17 +00002056 ret = regmap_read(wm8903->regmap, WM8903_SW_RESET_AND_ID, &val);
2057 if (ret != 0) {
2058 dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
2059 goto err;
2060 }
2061 if (val != 0x8903) {
2062 dev_err(&i2c->dev, "Device with ID %x is not a WM8903\n", val);
2063 ret = -ENODEV;
2064 goto err;
2065 }
2066
2067 ret = regmap_read(wm8903->regmap, WM8903_REVISION_NUMBER, &val);
2068 if (ret != 0) {
2069 dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
2070 goto err;
2071 }
2072 dev_info(&i2c->dev, "WM8903 revision %c\n",
2073 (val & WM8903_CHIP_REV_MASK) + 'A');
2074
2075 /* Reset the device */
2076 regmap_write(wm8903->regmap, WM8903_SW_RESET_AND_ID, 0x8903);
2077
Stephen Warren0bf79ef2012-05-22 16:08:52 -06002078 wm8903_init_gpio(wm8903);
2079
Mark Brown20c5fd32012-06-09 10:03:20 +08002080 /* Set up GPIO pin state, detect if any are MIC detect outputs */
2081 for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
2082 if ((!pdata->gpio_cfg[i]) ||
2083 (pdata->gpio_cfg[i] > WM8903_GPIO_CONFIG_ZERO))
2084 continue;
2085
2086 regmap_write(wm8903->regmap, WM8903_GPIO_CONTROL_1 + i,
2087 pdata->gpio_cfg[i] & 0x7fff);
2088
2089 val = (pdata->gpio_cfg[i] & WM8903_GP1_FN_MASK)
2090 >> WM8903_GP1_FN_SHIFT;
2091
2092 switch (val) {
2093 case WM8903_GPn_FN_MICBIAS_CURRENT_DETECT:
2094 case WM8903_GPn_FN_MICBIAS_SHORT_DETECT:
2095 mic_gpio = true;
2096 break;
2097 default:
2098 break;
2099 }
2100 }
2101
2102 /* Set up microphone detection */
2103 regmap_write(wm8903->regmap, WM8903_MIC_BIAS_CONTROL_0,
2104 pdata->micdet_cfg);
2105
2106 /* Microphone detection needs the WSEQ clock */
2107 if (pdata->micdet_cfg)
2108 regmap_update_bits(wm8903->regmap, WM8903_WRITE_SEQUENCER_0,
2109 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
2110
2111 /* If microphone detection is enabled by pdata but
2112 * detected via IRQ then interrupts can be lost before
2113 * the machine driver has set up microphone detection
2114 * IRQs as the IRQs are clear on read. The detection
2115 * will be enabled when the machine driver configures.
2116 */
2117 WARN_ON(!mic_gpio && (pdata->micdet_cfg & WM8903_MICDET_ENA));
2118
2119 wm8903->mic_delay = pdata->micdet_delay;
2120
Mark Brownb7c95d92012-06-09 10:15:10 +08002121 if (i2c->irq) {
2122 if (pdata->irq_active_low) {
2123 trigger = IRQF_TRIGGER_LOW;
2124 irq_pol = WM8903_IRQ_POL;
2125 } else {
2126 trigger = IRQF_TRIGGER_HIGH;
2127 irq_pol = 0;
2128 }
2129
2130 regmap_update_bits(wm8903->regmap, WM8903_INTERRUPT_CONTROL,
2131 WM8903_IRQ_POL, irq_pol);
2132
2133 ret = request_threaded_irq(i2c->irq, NULL, wm8903_irq,
2134 trigger | IRQF_ONESHOT,
2135 "wm8903", wm8903);
2136 if (ret != 0) {
2137 dev_err(wm8903->dev, "Failed to request IRQ: %d\n",
2138 ret);
2139 return ret;
2140 }
2141
2142 /* Enable write sequencer interrupts */
2143 regmap_update_bits(wm8903->regmap,
2144 WM8903_INTERRUPT_STATUS_1_MASK,
2145 WM8903_IM_WSEQ_BUSY_EINT, 0);
2146 }
2147
Mark Browna89c3e92012-06-09 10:30:34 +08002148 /* Latch volume update bits */
2149 regmap_update_bits(wm8903->regmap, WM8903_ADC_DIGITAL_VOLUME_LEFT,
2150 WM8903_ADCVU, WM8903_ADCVU);
2151 regmap_update_bits(wm8903->regmap, WM8903_ADC_DIGITAL_VOLUME_RIGHT,
2152 WM8903_ADCVU, WM8903_ADCVU);
2153
2154 regmap_update_bits(wm8903->regmap, WM8903_DAC_DIGITAL_VOLUME_LEFT,
2155 WM8903_DACVU, WM8903_DACVU);
2156 regmap_update_bits(wm8903->regmap, WM8903_DAC_DIGITAL_VOLUME_RIGHT,
2157 WM8903_DACVU, WM8903_DACVU);
2158
2159 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT1_LEFT,
2160 WM8903_HPOUTVU, WM8903_HPOUTVU);
2161 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT1_RIGHT,
2162 WM8903_HPOUTVU, WM8903_HPOUTVU);
2163
2164 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT2_LEFT,
2165 WM8903_LINEOUTVU, WM8903_LINEOUTVU);
2166 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT2_RIGHT,
2167 WM8903_LINEOUTVU, WM8903_LINEOUTVU);
2168
2169 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT3_LEFT,
2170 WM8903_SPKVU, WM8903_SPKVU);
2171 regmap_update_bits(wm8903->regmap, WM8903_ANALOGUE_OUT3_RIGHT,
2172 WM8903_SPKVU, WM8903_SPKVU);
2173
2174 /* Enable DAC soft mute by default */
2175 regmap_update_bits(wm8903->regmap, WM8903_DAC_DIGITAL_1,
2176 WM8903_DAC_MUTEMODE | WM8903_DAC_MUTE,
2177 WM8903_DAC_MUTEMODE | WM8903_DAC_MUTE);
2178
Kuninori Morimoto58bd2932018-01-29 03:05:11 +00002179 ret = devm_snd_soc_register_component(&i2c->dev,
2180 &soc_component_dev_wm8903, &wm8903_dai, 1);
Mark Brownee244ce2011-12-02 18:33:32 +00002181 if (ret != 0)
2182 goto err;
Mark Brown2950cd222011-12-03 10:59:32 +00002183
Mark Brownee244ce2011-12-02 18:33:32 +00002184 return 0;
2185err:
Linus Walleijb3bbef42017-03-20 10:13:52 +01002186 regulator_bulk_disable(ARRAY_SIZE(wm8903->supplies),
2187 wm8903->supplies);
Mark Brownf1c0a022008-08-26 13:05:27 +01002188 return ret;
2189}
2190
Bill Pemberton7a79e942012-12-07 09:26:37 -05002191static int wm8903_i2c_remove(struct i2c_client *client)
Mark Brownf1c0a022008-08-26 13:05:27 +01002192{
Mark Brownee244ce2011-12-02 18:33:32 +00002193 struct wm8903_priv *wm8903 = i2c_get_clientdata(client);
2194
Linus Walleijb3bbef42017-03-20 10:13:52 +01002195 regulator_bulk_disable(ARRAY_SIZE(wm8903->supplies),
2196 wm8903->supplies);
Mark Brownb7c95d92012-06-09 10:15:10 +08002197 if (client->irq)
2198 free_irq(client->irq, wm8903);
Stephen Warren0bf79ef2012-05-22 16:08:52 -06002199 wm8903_free_gpio(wm8903);
Mark Brownee244ce2011-12-02 18:33:32 +00002200
Mark Brownf1c0a022008-08-26 13:05:27 +01002201 return 0;
2202}
2203
Stephen Warrenf18b4e22011-12-06 14:15:41 -07002204static const struct of_device_id wm8903_of_match[] = {
2205 { .compatible = "wlf,wm8903", },
2206 {},
2207};
2208MODULE_DEVICE_TABLE(of, wm8903_of_match);
2209
Mark Brownf1c0a022008-08-26 13:05:27 +01002210static const struct i2c_device_id wm8903_i2c_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002211 { "wm8903", 0 },
2212 { }
Mark Brownf1c0a022008-08-26 13:05:27 +01002213};
2214MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
2215
2216static struct i2c_driver wm8903_i2c_driver = {
2217 .driver = {
Mark Brown4b592c92011-02-09 13:47:06 +00002218 .name = "wm8903",
Stephen Warrenf18b4e22011-12-06 14:15:41 -07002219 .of_match_table = wm8903_of_match,
Mark Brownf1c0a022008-08-26 13:05:27 +01002220 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002221 .probe = wm8903_i2c_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -05002222 .remove = wm8903_i2c_remove,
Mark Brownf1c0a022008-08-26 13:05:27 +01002223 .id_table = wm8903_i2c_id,
2224};
Mark Brownf1c0a022008-08-26 13:05:27 +01002225
Sachin Kamat5c86ea42012-08-06 17:25:57 +05302226module_i2c_driver(wm8903_i2c_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002227
Mark Brownf1c0a022008-08-26 13:05:27 +01002228MODULE_DESCRIPTION("ASoC WM8903 driver");
2229MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
2230MODULE_LICENSE("GPL");