blob: ab4685a8a570cb163c1359f3c30bd52d27b0e88f [file] [log] [blame]
Mark Brown942c4352009-06-05 16:32:59 +01001/*
2 * wm8993.c -- WM8993 ALSA SoC audio driver
3 *
Mark Brownbe587ef2010-02-01 18:31:06 +00004 * Copyright 2009, 2010 Wolfson Microelectronics plc
Mark Brown942c4352009-06-05 16:32:59 +01005 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/pm.h>
18#include <linux/i2c.h>
Mark Brownd0ad0af2011-12-14 11:53:06 +080019#include <linux/regmap.h>
Mark Brownb37e3992010-02-03 11:51:42 +000020#include <linux/regulator/consumer.h>
Mark Brown942c4352009-06-05 16:32:59 +010021#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brown942c4352009-06-05 16:32:59 +010023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/tlv.h>
27#include <sound/soc.h>
Mark Brown942c4352009-06-05 16:32:59 +010028#include <sound/initval.h>
29#include <sound/wm8993.h>
30
31#include "wm8993.h"
Mark Browna2342ae2009-07-29 21:21:49 +010032#include "wm_hubs.h"
Mark Brown942c4352009-06-05 16:32:59 +010033
Mark Brownb37e3992010-02-03 11:51:42 +000034#define WM8993_NUM_SUPPLIES 6
35static const char *wm8993_supply_names[WM8993_NUM_SUPPLIES] = {
36 "DCVDD",
37 "DBVDD",
38 "AVDD1",
39 "AVDD2",
40 "CPVDD",
41 "SPKVDD",
42};
43
Mark Brownd0ad0af2011-12-14 11:53:06 +080044static struct reg_default wm8993_reg_defaults[] = {
45 { 1, 0x0000 }, /* R1 - Power Management (1) */
46 { 2, 0x6000 }, /* R2 - Power Management (2) */
47 { 3, 0x0000 }, /* R3 - Power Management (3) */
48 { 4, 0x4050 }, /* R4 - Audio Interface (1) */
49 { 5, 0x4000 }, /* R5 - Audio Interface (2) */
50 { 6, 0x01C8 }, /* R6 - Clocking 1 */
51 { 7, 0x0000 }, /* R7 - Clocking 2 */
52 { 8, 0x0000 }, /* R8 - Audio Interface (3) */
53 { 9, 0x0040 }, /* R9 - Audio Interface (4) */
54 { 10, 0x0004 }, /* R10 - DAC CTRL */
55 { 11, 0x00C0 }, /* R11 - Left DAC Digital Volume */
56 { 12, 0x00C0 }, /* R12 - Right DAC Digital Volume */
57 { 13, 0x0000 }, /* R13 - Digital Side Tone */
58 { 14, 0x0300 }, /* R14 - ADC CTRL */
59 { 15, 0x00C0 }, /* R15 - Left ADC Digital Volume */
60 { 16, 0x00C0 }, /* R16 - Right ADC Digital Volume */
61 { 18, 0x0000 }, /* R18 - GPIO CTRL 1 */
62 { 19, 0x0010 }, /* R19 - GPIO1 */
63 { 20, 0x0000 }, /* R20 - IRQ_DEBOUNCE */
Mark Brownb8034222012-02-10 18:09:59 +000064 { 21, 0x0000 }, /* R21 - Inputs Clamp */
Mark Browne2da2672012-02-10 18:02:58 +000065 { 22, 0x8000 }, /* R22 - GPIOCTRL 2 */
66 { 23, 0x0800 }, /* R23 - GPIO_POL */
Mark Brownd0ad0af2011-12-14 11:53:06 +080067 { 24, 0x008B }, /* R24 - Left Line Input 1&2 Volume */
68 { 25, 0x008B }, /* R25 - Left Line Input 3&4 Volume */
69 { 26, 0x008B }, /* R26 - Right Line Input 1&2 Volume */
70 { 27, 0x008B }, /* R27 - Right Line Input 3&4 Volume */
71 { 28, 0x006D }, /* R28 - Left Output Volume */
72 { 29, 0x006D }, /* R29 - Right Output Volume */
73 { 30, 0x0066 }, /* R30 - Line Outputs Volume */
74 { 31, 0x0020 }, /* R31 - HPOUT2 Volume */
75 { 32, 0x0079 }, /* R32 - Left OPGA Volume */
76 { 33, 0x0079 }, /* R33 - Right OPGA Volume */
77 { 34, 0x0003 }, /* R34 - SPKMIXL Attenuation */
78 { 35, 0x0003 }, /* R35 - SPKMIXR Attenuation */
79 { 36, 0x0011 }, /* R36 - SPKOUT Mixers */
80 { 37, 0x0100 }, /* R37 - SPKOUT Boost */
81 { 38, 0x0079 }, /* R38 - Speaker Volume Left */
82 { 39, 0x0079 }, /* R39 - Speaker Volume Right */
83 { 40, 0x0000 }, /* R40 - Input Mixer2 */
84 { 41, 0x0000 }, /* R41 - Input Mixer3 */
85 { 42, 0x0000 }, /* R42 - Input Mixer4 */
86 { 43, 0x0000 }, /* R43 - Input Mixer5 */
87 { 44, 0x0000 }, /* R44 - Input Mixer6 */
88 { 45, 0x0000 }, /* R45 - Output Mixer1 */
89 { 46, 0x0000 }, /* R46 - Output Mixer2 */
90 { 47, 0x0000 }, /* R47 - Output Mixer3 */
91 { 48, 0x0000 }, /* R48 - Output Mixer4 */
92 { 49, 0x0000 }, /* R49 - Output Mixer5 */
93 { 50, 0x0000 }, /* R50 - Output Mixer6 */
94 { 51, 0x0000 }, /* R51 - HPOUT2 Mixer */
95 { 52, 0x0000 }, /* R52 - Line Mixer1 */
96 { 53, 0x0000 }, /* R53 - Line Mixer2 */
97 { 54, 0x0000 }, /* R54 - Speaker Mixer */
98 { 55, 0x0000 }, /* R55 - Additional Control */
99 { 56, 0x0000 }, /* R56 - AntiPOP1 */
100 { 57, 0x0000 }, /* R57 - AntiPOP2 */
101 { 58, 0x0000 }, /* R58 - MICBIAS */
102 { 60, 0x0000 }, /* R60 - FLL Control 1 */
103 { 61, 0x0000 }, /* R61 - FLL Control 2 */
104 { 62, 0x0000 }, /* R62 - FLL Control 3 */
105 { 63, 0x2EE0 }, /* R63 - FLL Control 4 */
106 { 64, 0x0002 }, /* R64 - FLL Control 5 */
107 { 65, 0x2287 }, /* R65 - Clocking 3 */
108 { 66, 0x025F }, /* R66 - Clocking 4 */
109 { 67, 0x0000 }, /* R67 - MW Slave Control */
110 { 69, 0x0002 }, /* R69 - Bus Control 1 */
111 { 70, 0x0000 }, /* R70 - Write Sequencer 0 */
112 { 71, 0x0000 }, /* R71 - Write Sequencer 1 */
113 { 72, 0x0000 }, /* R72 - Write Sequencer 2 */
114 { 73, 0x0000 }, /* R73 - Write Sequencer 3 */
115 { 74, 0x0000 }, /* R74 - Write Sequencer 4 */
116 { 75, 0x0000 }, /* R75 - Write Sequencer 5 */
117 { 76, 0x1F25 }, /* R76 - Charge Pump 1 */
118 { 81, 0x0000 }, /* R81 - Class W 0 */
119 { 85, 0x054A }, /* R85 - DC Servo 1 */
120 { 87, 0x0000 }, /* R87 - DC Servo 3 */
121 { 96, 0x0100 }, /* R96 - Analogue HP 0 */
122 { 98, 0x0000 }, /* R98 - EQ1 */
123 { 99, 0x000C }, /* R99 - EQ2 */
124 { 100, 0x000C }, /* R100 - EQ3 */
125 { 101, 0x000C }, /* R101 - EQ4 */
126 { 102, 0x000C }, /* R102 - EQ5 */
127 { 103, 0x000C }, /* R103 - EQ6 */
128 { 104, 0x0FCA }, /* R104 - EQ7 */
129 { 105, 0x0400 }, /* R105 - EQ8 */
130 { 106, 0x00D8 }, /* R106 - EQ9 */
131 { 107, 0x1EB5 }, /* R107 - EQ10 */
132 { 108, 0xF145 }, /* R108 - EQ11 */
133 { 109, 0x0B75 }, /* R109 - EQ12 */
134 { 110, 0x01C5 }, /* R110 - EQ13 */
135 { 111, 0x1C58 }, /* R111 - EQ14 */
136 { 112, 0xF373 }, /* R112 - EQ15 */
137 { 113, 0x0A54 }, /* R113 - EQ16 */
138 { 114, 0x0558 }, /* R114 - EQ17 */
139 { 115, 0x168E }, /* R115 - EQ18 */
140 { 116, 0xF829 }, /* R116 - EQ19 */
141 { 117, 0x07AD }, /* R117 - EQ20 */
142 { 118, 0x1103 }, /* R118 - EQ21 */
143 { 119, 0x0564 }, /* R119 - EQ22 */
144 { 120, 0x0559 }, /* R120 - EQ23 */
145 { 121, 0x4000 }, /* R121 - EQ24 */
146 { 122, 0x0000 }, /* R122 - Digital Pulls */
147 { 123, 0x0F08 }, /* R123 - DRC Control 1 */
148 { 124, 0x0000 }, /* R124 - DRC Control 2 */
149 { 125, 0x0080 }, /* R125 - DRC Control 3 */
150 { 126, 0x0000 }, /* R126 - DRC Control 4 */
Mark Brown942c4352009-06-05 16:32:59 +0100151};
152
153static struct {
154 int ratio;
155 int clk_sys_rate;
156} clk_sys_rates[] = {
157 { 64, 0 },
158 { 128, 1 },
159 { 192, 2 },
160 { 256, 3 },
161 { 384, 4 },
162 { 512, 5 },
163 { 768, 6 },
164 { 1024, 7 },
165 { 1408, 8 },
166 { 1536, 9 },
167};
168
169static struct {
170 int rate;
171 int sample_rate;
172} sample_rates[] = {
173 { 8000, 0 },
174 { 11025, 1 },
175 { 12000, 1 },
176 { 16000, 2 },
177 { 22050, 3 },
178 { 24000, 3 },
179 { 32000, 4 },
180 { 44100, 5 },
181 { 48000, 5 },
182};
183
184static struct {
185 int div; /* *10 due to .5s */
186 int bclk_div;
187} bclk_divs[] = {
188 { 10, 0 },
189 { 15, 1 },
190 { 20, 2 },
191 { 30, 3 },
192 { 40, 4 },
193 { 55, 5 },
194 { 60, 6 },
195 { 80, 7 },
196 { 110, 8 },
197 { 120, 9 },
198 { 160, 10 },
199 { 220, 11 },
200 { 240, 12 },
201 { 320, 13 },
202 { 440, 14 },
203 { 480, 15 },
204};
205
206struct wm8993_priv {
Mark Brown3ed70742010-01-20 17:39:45 +0000207 struct wm_hubs_data hubs_data;
Mark Brown164548d2012-01-17 16:42:05 +0000208 struct device *dev;
Mark Brownd0ad0af2011-12-14 11:53:06 +0800209 struct regmap *regmap;
Mark Brownb37e3992010-02-03 11:51:42 +0000210 struct regulator_bulk_data supplies[WM8993_NUM_SUPPLIES];
Mark Brown942c4352009-06-05 16:32:59 +0100211 struct wm8993_platform_data pdata;
Mark Brown164548d2012-01-17 16:42:05 +0000212 struct completion fll_lock;
Mark Brown942c4352009-06-05 16:32:59 +0100213 int master;
214 int sysclk_source;
Mark Brownd3c9e9a2009-08-17 18:52:47 +0100215 int tdm_slots;
216 int tdm_width;
Mark Brown942c4352009-06-05 16:32:59 +0100217 unsigned int mclk_rate;
218 unsigned int sysclk_rate;
219 unsigned int fs;
220 unsigned int bclk;
221 int class_w_users;
222 unsigned int fll_fref;
223 unsigned int fll_fout;
Mark Brown53242c62010-01-02 13:15:56 +0000224 int fll_src;
Mark Brown942c4352009-06-05 16:32:59 +0100225};
226
Mark Brownd0ad0af2011-12-14 11:53:06 +0800227static bool wm8993_volatile(struct device *dev, unsigned int reg)
Mark Brown942c4352009-06-05 16:32:59 +0100228{
229 switch (reg) {
230 case WM8993_SOFTWARE_RESET:
Mark Brown164548d2012-01-17 16:42:05 +0000231 case WM8993_GPIO_CTRL_1:
Mark Brown942c4352009-06-05 16:32:59 +0100232 case WM8993_DC_SERVO_0:
233 case WM8993_DC_SERVO_READBACK_0:
234 case WM8993_DC_SERVO_READBACK_1:
235 case WM8993_DC_SERVO_READBACK_2:
Mark Brownd0ad0af2011-12-14 11:53:06 +0800236 return true;
Mark Brown942c4352009-06-05 16:32:59 +0100237 default:
Mark Brownd0ad0af2011-12-14 11:53:06 +0800238 return false;
239 }
240}
241
242static bool wm8993_readable(struct device *dev, unsigned int reg)
243{
244 switch (reg) {
245 case WM8993_SOFTWARE_RESET:
246 case WM8993_POWER_MANAGEMENT_1:
247 case WM8993_POWER_MANAGEMENT_2:
248 case WM8993_POWER_MANAGEMENT_3:
249 case WM8993_AUDIO_INTERFACE_1:
250 case WM8993_AUDIO_INTERFACE_2:
251 case WM8993_CLOCKING_1:
252 case WM8993_CLOCKING_2:
253 case WM8993_AUDIO_INTERFACE_3:
254 case WM8993_AUDIO_INTERFACE_4:
255 case WM8993_DAC_CTRL:
256 case WM8993_LEFT_DAC_DIGITAL_VOLUME:
257 case WM8993_RIGHT_DAC_DIGITAL_VOLUME:
258 case WM8993_DIGITAL_SIDE_TONE:
259 case WM8993_ADC_CTRL:
260 case WM8993_LEFT_ADC_DIGITAL_VOLUME:
261 case WM8993_RIGHT_ADC_DIGITAL_VOLUME:
262 case WM8993_GPIO_CTRL_1:
263 case WM8993_GPIO1:
264 case WM8993_IRQ_DEBOUNCE:
265 case WM8993_GPIOCTRL_2:
266 case WM8993_GPIO_POL:
267 case WM8993_LEFT_LINE_INPUT_1_2_VOLUME:
268 case WM8993_LEFT_LINE_INPUT_3_4_VOLUME:
269 case WM8993_RIGHT_LINE_INPUT_1_2_VOLUME:
270 case WM8993_RIGHT_LINE_INPUT_3_4_VOLUME:
271 case WM8993_LEFT_OUTPUT_VOLUME:
272 case WM8993_RIGHT_OUTPUT_VOLUME:
273 case WM8993_LINE_OUTPUTS_VOLUME:
274 case WM8993_HPOUT2_VOLUME:
275 case WM8993_LEFT_OPGA_VOLUME:
276 case WM8993_RIGHT_OPGA_VOLUME:
277 case WM8993_SPKMIXL_ATTENUATION:
278 case WM8993_SPKMIXR_ATTENUATION:
279 case WM8993_SPKOUT_MIXERS:
280 case WM8993_SPKOUT_BOOST:
281 case WM8993_SPEAKER_VOLUME_LEFT:
282 case WM8993_SPEAKER_VOLUME_RIGHT:
283 case WM8993_INPUT_MIXER2:
284 case WM8993_INPUT_MIXER3:
285 case WM8993_INPUT_MIXER4:
286 case WM8993_INPUT_MIXER5:
287 case WM8993_INPUT_MIXER6:
288 case WM8993_OUTPUT_MIXER1:
289 case WM8993_OUTPUT_MIXER2:
290 case WM8993_OUTPUT_MIXER3:
291 case WM8993_OUTPUT_MIXER4:
292 case WM8993_OUTPUT_MIXER5:
293 case WM8993_OUTPUT_MIXER6:
294 case WM8993_HPOUT2_MIXER:
295 case WM8993_LINE_MIXER1:
296 case WM8993_LINE_MIXER2:
297 case WM8993_SPEAKER_MIXER:
298 case WM8993_ADDITIONAL_CONTROL:
299 case WM8993_ANTIPOP1:
300 case WM8993_ANTIPOP2:
301 case WM8993_MICBIAS:
302 case WM8993_FLL_CONTROL_1:
303 case WM8993_FLL_CONTROL_2:
304 case WM8993_FLL_CONTROL_3:
305 case WM8993_FLL_CONTROL_4:
306 case WM8993_FLL_CONTROL_5:
307 case WM8993_CLOCKING_3:
308 case WM8993_CLOCKING_4:
309 case WM8993_MW_SLAVE_CONTROL:
310 case WM8993_BUS_CONTROL_1:
311 case WM8993_WRITE_SEQUENCER_0:
312 case WM8993_WRITE_SEQUENCER_1:
313 case WM8993_WRITE_SEQUENCER_2:
314 case WM8993_WRITE_SEQUENCER_3:
315 case WM8993_WRITE_SEQUENCER_4:
316 case WM8993_WRITE_SEQUENCER_5:
317 case WM8993_CHARGE_PUMP_1:
318 case WM8993_CLASS_W_0:
319 case WM8993_DC_SERVO_0:
320 case WM8993_DC_SERVO_1:
321 case WM8993_DC_SERVO_3:
322 case WM8993_DC_SERVO_READBACK_0:
323 case WM8993_DC_SERVO_READBACK_1:
324 case WM8993_DC_SERVO_READBACK_2:
325 case WM8993_ANALOGUE_HP_0:
326 case WM8993_EQ1:
327 case WM8993_EQ2:
328 case WM8993_EQ3:
329 case WM8993_EQ4:
330 case WM8993_EQ5:
331 case WM8993_EQ6:
332 case WM8993_EQ7:
333 case WM8993_EQ8:
334 case WM8993_EQ9:
335 case WM8993_EQ10:
336 case WM8993_EQ11:
337 case WM8993_EQ12:
338 case WM8993_EQ13:
339 case WM8993_EQ14:
340 case WM8993_EQ15:
341 case WM8993_EQ16:
342 case WM8993_EQ17:
343 case WM8993_EQ18:
344 case WM8993_EQ19:
345 case WM8993_EQ20:
346 case WM8993_EQ21:
347 case WM8993_EQ22:
348 case WM8993_EQ23:
349 case WM8993_EQ24:
350 case WM8993_DIGITAL_PULLS:
351 case WM8993_DRC_CONTROL_1:
352 case WM8993_DRC_CONTROL_2:
353 case WM8993_DRC_CONTROL_3:
354 case WM8993_DRC_CONTROL_4:
355 return true;
356 default:
357 return false;
Mark Brown942c4352009-06-05 16:32:59 +0100358 }
359}
360
Mark Brown942c4352009-06-05 16:32:59 +0100361struct _fll_div {
362 u16 fll_fratio;
363 u16 fll_outdiv;
364 u16 fll_clk_ref_div;
365 u16 n;
366 u16 k;
367};
368
369/* The size in bits of the FLL divide multiplied by 10
370 * to allow rounding later */
371#define FIXED_FLL_SIZE ((1 << 16) * 10)
372
373static struct {
374 unsigned int min;
375 unsigned int max;
376 u16 fll_fratio;
377 int ratio;
378} fll_fratios[] = {
379 { 0, 64000, 4, 16 },
380 { 64000, 128000, 3, 8 },
381 { 128000, 256000, 2, 4 },
382 { 256000, 1000000, 1, 2 },
383 { 1000000, 13500000, 0, 1 },
384};
385
386static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
387 unsigned int Fout)
388{
389 u64 Kpart;
390 unsigned int K, Ndiv, Nmod, target;
391 unsigned int div;
392 int i;
393
394 /* Fref must be <=13.5MHz */
395 div = 1;
Mark Brown0c11f652009-07-17 22:13:01 +0100396 fll_div->fll_clk_ref_div = 0;
Mark Brown942c4352009-06-05 16:32:59 +0100397 while ((Fref / div) > 13500000) {
398 div *= 2;
Mark Brown0c11f652009-07-17 22:13:01 +0100399 fll_div->fll_clk_ref_div++;
Mark Brown942c4352009-06-05 16:32:59 +0100400
401 if (div > 8) {
402 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
403 Fref);
404 return -EINVAL;
405 }
406 }
407
408 pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
409
410 /* Apply the division for our remaining calculations */
411 Fref /= div;
412
413 /* Fvco should be 90-100MHz; don't check the upper bound */
414 div = 0;
415 target = Fout * 2;
416 while (target < 90000000) {
417 div++;
418 target *= 2;
419 if (div > 7) {
420 pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
421 Fout);
422 return -EINVAL;
423 }
424 }
425 fll_div->fll_outdiv = div;
426
427 pr_debug("Fvco=%dHz\n", target);
428
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 /* Find an appropriate FLL_FRATIO and factor it out of the target */
Mark Brown942c4352009-06-05 16:32:59 +0100430 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
431 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
432 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
433 target /= fll_fratios[i].ratio;
434 break;
435 }
436 }
437 if (i == ARRAY_SIZE(fll_fratios)) {
438 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
439 return -EINVAL;
440 }
441
442 /* Now, calculate N.K */
443 Ndiv = target / Fref;
444
445 fll_div->n = Ndiv;
446 Nmod = target % Fref;
447 pr_debug("Nmod=%d\n", Nmod);
448
449 /* Calculate fractional part - scale up so we can round. */
450 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
451
452 do_div(Kpart, Fref);
453
454 K = Kpart & 0xFFFFFFFF;
455
456 if ((K % 10) >= 5)
457 K += 5;
458
459 /* Move down to proper range now rounding is done */
460 fll_div->k = K / 10;
461
462 pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
463 fll_div->n, fll_div->k,
464 fll_div->fll_fratio, fll_div->fll_outdiv,
465 fll_div->fll_clk_ref_div);
466
467 return 0;
468}
469
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470static int _wm8993_set_fll(struct snd_soc_codec *codec, int fll_id, int source,
Mark Brown942c4352009-06-05 16:32:59 +0100471 unsigned int Fref, unsigned int Fout)
472{
Mark Brownb2c812e2010-04-14 15:35:19 +0900473 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown164548d2012-01-17 16:42:05 +0000474 struct i2c_client *i2c = to_i2c_client(codec->dev);
Mark Brown942c4352009-06-05 16:32:59 +0100475 u16 reg1, reg4, reg5;
476 struct _fll_div fll_div;
Mark Brown164548d2012-01-17 16:42:05 +0000477 unsigned int timeout;
Mark Brown942c4352009-06-05 16:32:59 +0100478 int ret;
479
480 /* Any change? */
481 if (Fref == wm8993->fll_fref && Fout == wm8993->fll_fout)
482 return 0;
483
484 /* Disable the FLL */
485 if (Fout == 0) {
486 dev_dbg(codec->dev, "FLL disabled\n");
487 wm8993->fll_fref = 0;
488 wm8993->fll_fout = 0;
489
Mark Brown3bf6e422010-02-01 19:05:09 +0000490 reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
Mark Brown942c4352009-06-05 16:32:59 +0100491 reg1 &= ~WM8993_FLL_ENA;
Mark Brown3bf6e422010-02-01 19:05:09 +0000492 snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
Mark Brown942c4352009-06-05 16:32:59 +0100493
494 return 0;
495 }
496
497 ret = fll_factors(&fll_div, Fref, Fout);
498 if (ret != 0)
499 return ret;
500
Mark Brown3bf6e422010-02-01 19:05:09 +0000501 reg5 = snd_soc_read(codec, WM8993_FLL_CONTROL_5);
Mark Brown942c4352009-06-05 16:32:59 +0100502 reg5 &= ~WM8993_FLL_CLK_SRC_MASK;
503
504 switch (fll_id) {
505 case WM8993_FLL_MCLK:
506 break;
507
508 case WM8993_FLL_LRCLK:
509 reg5 |= 1;
510 break;
511
512 case WM8993_FLL_BCLK:
513 reg5 |= 2;
514 break;
515
516 default:
517 dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
518 return -EINVAL;
519 }
520
521 /* Any FLL configuration change requires that the FLL be
522 * disabled first. */
Mark Brown3bf6e422010-02-01 19:05:09 +0000523 reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
Mark Brown942c4352009-06-05 16:32:59 +0100524 reg1 &= ~WM8993_FLL_ENA;
Mark Brown3bf6e422010-02-01 19:05:09 +0000525 snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
Mark Brown942c4352009-06-05 16:32:59 +0100526
527 /* Apply the configuration */
528 if (fll_div.k)
529 reg1 |= WM8993_FLL_FRAC_MASK;
530 else
531 reg1 &= ~WM8993_FLL_FRAC_MASK;
Mark Brown3bf6e422010-02-01 19:05:09 +0000532 snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
Mark Brown942c4352009-06-05 16:32:59 +0100533
Mark Brown3bf6e422010-02-01 19:05:09 +0000534 snd_soc_write(codec, WM8993_FLL_CONTROL_2,
535 (fll_div.fll_outdiv << WM8993_FLL_OUTDIV_SHIFT) |
536 (fll_div.fll_fratio << WM8993_FLL_FRATIO_SHIFT));
537 snd_soc_write(codec, WM8993_FLL_CONTROL_3, fll_div.k);
Mark Brown942c4352009-06-05 16:32:59 +0100538
Mark Brown3bf6e422010-02-01 19:05:09 +0000539 reg4 = snd_soc_read(codec, WM8993_FLL_CONTROL_4);
Mark Brown942c4352009-06-05 16:32:59 +0100540 reg4 &= ~WM8993_FLL_N_MASK;
541 reg4 |= fll_div.n << WM8993_FLL_N_SHIFT;
Mark Brown3bf6e422010-02-01 19:05:09 +0000542 snd_soc_write(codec, WM8993_FLL_CONTROL_4, reg4);
Mark Brown942c4352009-06-05 16:32:59 +0100543
544 reg5 &= ~WM8993_FLL_CLK_REF_DIV_MASK;
545 reg5 |= fll_div.fll_clk_ref_div << WM8993_FLL_CLK_REF_DIV_SHIFT;
Mark Brown3bf6e422010-02-01 19:05:09 +0000546 snd_soc_write(codec, WM8993_FLL_CONTROL_5, reg5);
Mark Brown942c4352009-06-05 16:32:59 +0100547
Mark Brown164548d2012-01-17 16:42:05 +0000548 /* If we've got an interrupt wired up make sure we get it */
549 if (i2c->irq)
550 timeout = msecs_to_jiffies(20);
551 else if (Fref < 1000000)
552 timeout = msecs_to_jiffies(3);
553 else
554 timeout = msecs_to_jiffies(1);
555
556 try_wait_for_completion(&wm8993->fll_lock);
557
Mark Brown942c4352009-06-05 16:32:59 +0100558 /* Enable the FLL */
Mark Brown3bf6e422010-02-01 19:05:09 +0000559 snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1 | WM8993_FLL_ENA);
Mark Brown942c4352009-06-05 16:32:59 +0100560
Mark Brown164548d2012-01-17 16:42:05 +0000561 timeout = wait_for_completion_timeout(&wm8993->fll_lock, timeout);
562 if (i2c->irq && !timeout)
563 dev_warn(codec->dev, "Timed out waiting for FLL\n");
Mark Brown986b2f22012-01-17 16:28:59 +0000564
Mark Brown942c4352009-06-05 16:32:59 +0100565 dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
566
567 wm8993->fll_fref = Fref;
568 wm8993->fll_fout = Fout;
Mark Brown53242c62010-01-02 13:15:56 +0000569 wm8993->fll_src = source;
Mark Brown942c4352009-06-05 16:32:59 +0100570
571 return 0;
572}
573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574static int wm8993_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
575 unsigned int Fref, unsigned int Fout)
576{
577 return _wm8993_set_fll(dai->codec, fll_id, source, Fref, Fout);
578}
579
Mark Brown942c4352009-06-05 16:32:59 +0100580static int configure_clock(struct snd_soc_codec *codec)
581{
Mark Brownb2c812e2010-04-14 15:35:19 +0900582 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown942c4352009-06-05 16:32:59 +0100583 unsigned int reg;
584
585 /* This should be done on init() for bypass paths */
586 switch (wm8993->sysclk_source) {
587 case WM8993_SYSCLK_MCLK:
588 dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8993->mclk_rate);
589
Mark Brown3bf6e422010-02-01 19:05:09 +0000590 reg = snd_soc_read(codec, WM8993_CLOCKING_2);
Mark Brown0182dcc2009-08-17 18:51:44 +0100591 reg &= ~(WM8993_MCLK_DIV | WM8993_SYSCLK_SRC);
Mark Brown942c4352009-06-05 16:32:59 +0100592 if (wm8993->mclk_rate > 13500000) {
593 reg |= WM8993_MCLK_DIV;
594 wm8993->sysclk_rate = wm8993->mclk_rate / 2;
595 } else {
596 reg &= ~WM8993_MCLK_DIV;
597 wm8993->sysclk_rate = wm8993->mclk_rate;
598 }
Mark Brown3bf6e422010-02-01 19:05:09 +0000599 snd_soc_write(codec, WM8993_CLOCKING_2, reg);
Mark Brown942c4352009-06-05 16:32:59 +0100600 break;
601
602 case WM8993_SYSCLK_FLL:
603 dev_dbg(codec->dev, "Using %dHz FLL clock\n",
604 wm8993->fll_fout);
605
Mark Brown3bf6e422010-02-01 19:05:09 +0000606 reg = snd_soc_read(codec, WM8993_CLOCKING_2);
Mark Brown942c4352009-06-05 16:32:59 +0100607 reg |= WM8993_SYSCLK_SRC;
608 if (wm8993->fll_fout > 13500000) {
609 reg |= WM8993_MCLK_DIV;
610 wm8993->sysclk_rate = wm8993->fll_fout / 2;
611 } else {
612 reg &= ~WM8993_MCLK_DIV;
613 wm8993->sysclk_rate = wm8993->fll_fout;
614 }
Mark Brown3bf6e422010-02-01 19:05:09 +0000615 snd_soc_write(codec, WM8993_CLOCKING_2, reg);
Mark Brown942c4352009-06-05 16:32:59 +0100616 break;
617
618 default:
619 dev_err(codec->dev, "System clock not configured\n");
620 return -EINVAL;
621 }
622
623 dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8993->sysclk_rate);
624
625 return 0;
626}
627
Mark Brown942c4352009-06-05 16:32:59 +0100628static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
629static const DECLARE_TLV_DB_SCALE(drc_comp_threash, -4500, 75, 0);
630static const DECLARE_TLV_DB_SCALE(drc_comp_amp, -2250, 75, 0);
631static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
632static const unsigned int drc_max_tlv[] = {
Clemens Ladischdac678f2011-11-20 15:14:11 +0100633 TLV_DB_RANGE_HEAD(2),
Mark Brown942c4352009-06-05 16:32:59 +0100634 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
635 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
636};
637static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
638static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -1800, 300, 0);
639static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
640static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
641static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
Mark Brown942c4352009-06-05 16:32:59 +0100642
643static const char *dac_deemph_text[] = {
644 "None",
645 "32kHz",
646 "44.1kHz",
647 "48kHz",
648};
649
650static const struct soc_enum dac_deemph =
651 SOC_ENUM_SINGLE(WM8993_DAC_CTRL, 4, 4, dac_deemph_text);
652
653static const char *adc_hpf_text[] = {
654 "Hi-Fi",
655 "Voice 1",
656 "Voice 2",
657 "Voice 3",
658};
659
660static const struct soc_enum adc_hpf =
661 SOC_ENUM_SINGLE(WM8993_ADC_CTRL, 5, 4, adc_hpf_text);
662
663static const char *drc_path_text[] = {
664 "ADC",
665 "DAC"
666};
667
668static const struct soc_enum drc_path =
669 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 14, 2, drc_path_text);
670
671static const char *drc_r0_text[] = {
672 "1",
673 "1/2",
674 "1/4",
675 "1/8",
676 "1/16",
677 "0",
678};
679
680static const struct soc_enum drc_r0 =
681 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 8, 6, drc_r0_text);
682
683static const char *drc_r1_text[] = {
684 "1",
685 "1/2",
686 "1/4",
687 "1/8",
688 "0",
689};
690
691static const struct soc_enum drc_r1 =
692 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_4, 13, 5, drc_r1_text);
693
694static const char *drc_attack_text[] = {
695 "Reserved",
696 "181us",
697 "363us",
698 "726us",
699 "1.45ms",
700 "2.9ms",
701 "5.8ms",
702 "11.6ms",
703 "23.2ms",
704 "46.4ms",
705 "92.8ms",
706 "185.6ms",
707};
708
709static const struct soc_enum drc_attack =
710 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 12, 12, drc_attack_text);
711
712static const char *drc_decay_text[] = {
713 "186ms",
714 "372ms",
715 "743ms",
716 "1.49s",
717 "2.97ms",
718 "5.94ms",
719 "11.89ms",
720 "23.78ms",
721 "47.56ms",
722};
723
724static const struct soc_enum drc_decay =
725 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 8, 9, drc_decay_text);
726
727static const char *drc_ff_text[] = {
728 "5 samples",
729 "9 samples",
730};
731
732static const struct soc_enum drc_ff =
733 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 7, 2, drc_ff_text);
734
735static const char *drc_qr_rate_text[] = {
736 "0.725ms",
737 "1.45ms",
738 "5.8ms",
739};
740
741static const struct soc_enum drc_qr_rate =
742 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 0, 3, drc_qr_rate_text);
743
744static const char *drc_smooth_text[] = {
745 "Low",
746 "Medium",
747 "High",
748};
749
750static const struct soc_enum drc_smooth =
751 SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 4, 3, drc_smooth_text);
752
Mark Brown942c4352009-06-05 16:32:59 +0100753static const struct snd_kcontrol_new wm8993_snd_controls[] = {
Mark Brown942c4352009-06-05 16:32:59 +0100754SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8993_DIGITAL_SIDE_TONE,
755 5, 9, 12, 0, sidetone_tlv),
756
757SOC_SINGLE("DRC Switch", WM8993_DRC_CONTROL_1, 15, 1, 0),
758SOC_ENUM("DRC Path", drc_path),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200759SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8993_DRC_CONTROL_2,
Mark Brown942c4352009-06-05 16:32:59 +0100760 2, 60, 1, drc_comp_threash),
761SOC_SINGLE_TLV("DRC Compressor Amplitude Volume", WM8993_DRC_CONTROL_3,
762 11, 30, 1, drc_comp_amp),
763SOC_ENUM("DRC R0", drc_r0),
764SOC_ENUM("DRC R1", drc_r1),
765SOC_SINGLE_TLV("DRC Minimum Volume", WM8993_DRC_CONTROL_1, 2, 3, 1,
766 drc_min_tlv),
767SOC_SINGLE_TLV("DRC Maximum Volume", WM8993_DRC_CONTROL_1, 0, 3, 0,
768 drc_max_tlv),
769SOC_ENUM("DRC Attack Rate", drc_attack),
770SOC_ENUM("DRC Decay Rate", drc_decay),
771SOC_ENUM("DRC FF Delay", drc_ff),
772SOC_SINGLE("DRC Anti-clip Switch", WM8993_DRC_CONTROL_1, 9, 1, 0),
773SOC_SINGLE("DRC Quick Release Switch", WM8993_DRC_CONTROL_1, 10, 1, 0),
774SOC_SINGLE_TLV("DRC Quick Release Volume", WM8993_DRC_CONTROL_3, 2, 3, 0,
775 drc_qr_tlv),
776SOC_ENUM("DRC Quick Release Rate", drc_qr_rate),
777SOC_SINGLE("DRC Smoothing Switch", WM8993_DRC_CONTROL_1, 11, 1, 0),
778SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8993_DRC_CONTROL_1, 8, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200779SOC_ENUM("DRC Smoothing Hysteresis Threshold", drc_smooth),
Mark Brown942c4352009-06-05 16:32:59 +0100780SOC_SINGLE_TLV("DRC Startup Volume", WM8993_DRC_CONTROL_4, 8, 18, 0,
781 drc_startup_tlv),
782
783SOC_SINGLE("EQ Switch", WM8993_EQ1, 0, 1, 0),
784
785SOC_DOUBLE_R_TLV("Capture Volume", WM8993_LEFT_ADC_DIGITAL_VOLUME,
786 WM8993_RIGHT_ADC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
787SOC_SINGLE("ADC High Pass Filter Switch", WM8993_ADC_CTRL, 8, 1, 0),
788SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
789
790SOC_DOUBLE_R_TLV("Playback Volume", WM8993_LEFT_DAC_DIGITAL_VOLUME,
791 WM8993_RIGHT_DAC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
792SOC_SINGLE_TLV("Playback Boost Volume", WM8993_AUDIO_INTERFACE_2, 10, 3, 0,
793 dac_boost_tlv),
794SOC_ENUM("DAC Deemphasis", dac_deemph),
795
Mark Brown942c4352009-06-05 16:32:59 +0100796SOC_SINGLE_TLV("SPKL DAC Volume", WM8993_SPKMIXL_ATTENUATION,
Mark Browna2342ae2009-07-29 21:21:49 +0100797 2, 1, 1, wm_hubs_spkmix_tlv),
Mark Brown942c4352009-06-05 16:32:59 +0100798
Mark Brown942c4352009-06-05 16:32:59 +0100799SOC_SINGLE_TLV("SPKR DAC Volume", WM8993_SPKMIXR_ATTENUATION,
Mark Browna2342ae2009-07-29 21:21:49 +0100800 2, 1, 1, wm_hubs_spkmix_tlv),
Mark Brown942c4352009-06-05 16:32:59 +0100801};
802
803static const struct snd_kcontrol_new wm8993_eq_controls[] = {
804SOC_SINGLE_TLV("EQ1 Volume", WM8993_EQ2, 0, 24, 0, eq_tlv),
805SOC_SINGLE_TLV("EQ2 Volume", WM8993_EQ3, 0, 24, 0, eq_tlv),
806SOC_SINGLE_TLV("EQ3 Volume", WM8993_EQ4, 0, 24, 0, eq_tlv),
807SOC_SINGLE_TLV("EQ4 Volume", WM8993_EQ5, 0, 24, 0, eq_tlv),
808SOC_SINGLE_TLV("EQ5 Volume", WM8993_EQ6, 0, 24, 0, eq_tlv),
809};
810
Mark Brown942c4352009-06-05 16:32:59 +0100811static int clk_sys_event(struct snd_soc_dapm_widget *w,
812 struct snd_kcontrol *kcontrol, int event)
813{
814 struct snd_soc_codec *codec = w->codec;
815
816 switch (event) {
817 case SND_SOC_DAPM_PRE_PMU:
818 return configure_clock(codec);
819
820 case SND_SOC_DAPM_POST_PMD:
821 break;
822 }
823
824 return 0;
825}
826
827/*
828 * When used with DAC outputs only the WM8993 charge pump supports
829 * operation in class W mode, providing very low power consumption
830 * when used with digital sources. Enable and disable this mode
831 * automatically depending on the mixer configuration.
832 *
833 * Currently the only supported paths are the direct DAC->headphone
834 * paths (which provide minimum power consumption anyway).
835 */
Mark Browna2342ae2009-07-29 21:21:49 +0100836static int class_w_put(struct snd_kcontrol *kcontrol,
837 struct snd_ctl_elem_value *ucontrol)
Mark Brown942c4352009-06-05 16:32:59 +0100838{
Jarkko Nikula9d035452011-05-13 19:16:52 +0300839 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
840 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brown942c4352009-06-05 16:32:59 +0100841 struct snd_soc_codec *codec = widget->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900842 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown942c4352009-06-05 16:32:59 +0100843 int ret;
844
845 /* Turn it off if we're using the main output mixer */
846 if (ucontrol->value.integer.value[0] == 0) {
847 if (wm8993->class_w_users == 0) {
848 dev_dbg(codec->dev, "Disabling Class W\n");
849 snd_soc_update_bits(codec, WM8993_CLASS_W_0,
850 WM8993_CP_DYN_FREQ |
851 WM8993_CP_DYN_V,
852 0);
853 }
854 wm8993->class_w_users++;
Mark Brownfec6dd82010-10-27 13:48:36 -0700855 wm8993->hubs_data.class_w = true;
Mark Brown942c4352009-06-05 16:32:59 +0100856 }
857
858 /* Implement the change */
859 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
860
861 /* Enable it if we're using the direct DAC path */
862 if (ucontrol->value.integer.value[0] == 1) {
863 if (wm8993->class_w_users == 1) {
864 dev_dbg(codec->dev, "Enabling Class W\n");
865 snd_soc_update_bits(codec, WM8993_CLASS_W_0,
866 WM8993_CP_DYN_FREQ |
867 WM8993_CP_DYN_V,
868 WM8993_CP_DYN_FREQ |
869 WM8993_CP_DYN_V);
870 }
871 wm8993->class_w_users--;
Mark Brownfec6dd82010-10-27 13:48:36 -0700872 wm8993->hubs_data.class_w = false;
Mark Brown942c4352009-06-05 16:32:59 +0100873 }
874
875 dev_dbg(codec->dev, "Indirect DAC use count now %d\n",
876 wm8993->class_w_users);
877
878 return ret;
879}
880
881#define SOC_DAPM_ENUM_W(xname, xenum) \
882{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
883 .info = snd_soc_info_enum_double, \
884 .get = snd_soc_dapm_get_enum_double, \
Mark Browna2342ae2009-07-29 21:21:49 +0100885 .put = class_w_put, \
Mark Brown942c4352009-06-05 16:32:59 +0100886 .private_value = (unsigned long)&xenum }
887
Mark Brown942c4352009-06-05 16:32:59 +0100888static const char *hp_mux_text[] = {
889 "Mixer",
890 "DAC",
891};
892
893static const struct soc_enum hpl_enum =
894 SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER1, 8, 2, hp_mux_text);
895
896static const struct snd_kcontrol_new hpl_mux =
897 SOC_DAPM_ENUM_W("Left Headphone Mux", hpl_enum);
898
899static const struct soc_enum hpr_enum =
900 SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER2, 8, 2, hp_mux_text);
901
902static const struct snd_kcontrol_new hpr_mux =
903 SOC_DAPM_ENUM_W("Right Headphone Mux", hpr_enum);
904
Mark Browna2342ae2009-07-29 21:21:49 +0100905static const struct snd_kcontrol_new left_speaker_mixer[] = {
906SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 7, 1, 0),
907SOC_DAPM_SINGLE("IN1LP Switch", WM8993_SPEAKER_MIXER, 5, 1, 0),
908SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 3, 1, 0),
909SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100910};
911
Mark Browna2342ae2009-07-29 21:21:49 +0100912static const struct snd_kcontrol_new right_speaker_mixer[] = {
913SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
914SOC_DAPM_SINGLE("IN1RP Switch", WM8993_SPEAKER_MIXER, 4, 1, 0),
915SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 2, 1, 0),
916SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 0, 1, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100917};
918
Mark Brown59ae07a2009-08-18 16:01:57 +0100919static const char *aif_text[] = {
920 "Left", "Right"
921};
922
923static const struct soc_enum aifoutl_enum =
924 SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 15, 2, aif_text);
925
926static const struct snd_kcontrol_new aifoutl_mux =
927 SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
928
929static const struct soc_enum aifoutr_enum =
930 SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 14, 2, aif_text);
931
932static const struct snd_kcontrol_new aifoutr_mux =
933 SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
934
935static const struct soc_enum aifinl_enum =
936 SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 15, 2, aif_text);
937
938static const struct snd_kcontrol_new aifinl_mux =
939 SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
940
941static const struct soc_enum aifinr_enum =
942 SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 14, 2, aif_text);
943
944static const struct snd_kcontrol_new aifinr_mux =
945 SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
946
947static const char *sidetone_text[] = {
948 "None", "Left", "Right"
949};
950
951static const struct soc_enum sidetonel_enum =
952 SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 2, 3, sidetone_text);
953
954static const struct snd_kcontrol_new sidetonel_mux =
955 SOC_DAPM_ENUM("Left Sidetone", sidetonel_enum);
956
957static const struct soc_enum sidetoner_enum =
958 SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 0, 3, sidetone_text);
959
960static const struct snd_kcontrol_new sidetoner_mux =
961 SOC_DAPM_ENUM("Right Sidetone", sidetoner_enum);
962
Mark Brown942c4352009-06-05 16:32:59 +0100963static const struct snd_soc_dapm_widget wm8993_dapm_widgets[] = {
Mark Brown942c4352009-06-05 16:32:59 +0100964SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8993_BUS_CONTROL_1, 1, 0, clk_sys_event,
965 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
966SND_SOC_DAPM_SUPPLY("TOCLK", WM8993_CLOCKING_1, 14, 0, NULL, 0),
967SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8993_CLOCKING_3, 0, 0, NULL, 0),
Mark Brown4e04ada2011-07-15 15:12:31 +0900968SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, NULL, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100969
Mark Brown59ae07a2009-08-18 16:01:57 +0100970SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
971SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100972
Mark Brown59ae07a2009-08-18 16:01:57 +0100973SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
974SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
Mark Brown942c4352009-06-05 16:32:59 +0100975
Mark Brown59ae07a2009-08-18 16:01:57 +0100976SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
977SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
978
979SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
980SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
981
982SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
983SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
984
985SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &sidetonel_mux),
986SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &sidetoner_mux),
987
988SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
989SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100990
Mark Browna2342ae2009-07-29 21:21:49 +0100991SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
992SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
Mark Brown942c4352009-06-05 16:32:59 +0100993
994SND_SOC_DAPM_MIXER("SPKL", WM8993_POWER_MANAGEMENT_3, 8, 0,
995 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
996SND_SOC_DAPM_MIXER("SPKR", WM8993_POWER_MANAGEMENT_3, 9, 0,
997 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
Mark Brownb70a51b2011-06-29 00:21:09 -0700998SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
Mark Brown942c4352009-06-05 16:32:59 +0100999};
1000
1001static const struct snd_soc_dapm_route routes[] = {
Mark Brown4e04ada2011-07-15 15:12:31 +09001002 { "MICBIAS1", NULL, "VMID" },
1003 { "MICBIAS2", NULL, "VMID" },
1004
Mark Brown942c4352009-06-05 16:32:59 +01001005 { "ADCL", NULL, "CLK_SYS" },
1006 { "ADCL", NULL, "CLK_DSP" },
Mark Brown942c4352009-06-05 16:32:59 +01001007 { "ADCR", NULL, "CLK_SYS" },
1008 { "ADCR", NULL, "CLK_DSP" },
1009
Mark Brown59ae07a2009-08-18 16:01:57 +01001010 { "AIFOUTL Mux", "Left", "ADCL" },
1011 { "AIFOUTL Mux", "Right", "ADCR" },
1012 { "AIFOUTR Mux", "Left", "ADCL" },
1013 { "AIFOUTR Mux", "Right", "ADCR" },
1014
1015 { "AIFOUTL", NULL, "AIFOUTL Mux" },
1016 { "AIFOUTR", NULL, "AIFOUTR Mux" },
1017
1018 { "DACL Mux", "Left", "AIFINL" },
1019 { "DACL Mux", "Right", "AIFINR" },
1020 { "DACR Mux", "Left", "AIFINL" },
1021 { "DACR Mux", "Right", "AIFINR" },
1022
1023 { "DACL Sidetone", "Left", "ADCL" },
1024 { "DACL Sidetone", "Right", "ADCR" },
1025 { "DACR Sidetone", "Left", "ADCL" },
1026 { "DACR Sidetone", "Right", "ADCR" },
1027
Mark Brown942c4352009-06-05 16:32:59 +01001028 { "DACL", NULL, "CLK_SYS" },
1029 { "DACL", NULL, "CLK_DSP" },
Mark Brown59ae07a2009-08-18 16:01:57 +01001030 { "DACL", NULL, "DACL Mux" },
1031 { "DACL", NULL, "DACL Sidetone" },
Mark Brown942c4352009-06-05 16:32:59 +01001032 { "DACR", NULL, "CLK_SYS" },
1033 { "DACR", NULL, "CLK_DSP" },
Mark Brown59ae07a2009-08-18 16:01:57 +01001034 { "DACR", NULL, "DACR Mux" },
1035 { "DACR", NULL, "DACR Sidetone" },
Mark Brown942c4352009-06-05 16:32:59 +01001036
Mark Brown942c4352009-06-05 16:32:59 +01001037 { "Left Output Mixer", "DAC Switch", "DACL" },
1038
Mark Brown942c4352009-06-05 16:32:59 +01001039 { "Right Output Mixer", "DAC Switch", "DACR" },
1040
Mark Brown942c4352009-06-05 16:32:59 +01001041 { "Left Output PGA", NULL, "CLK_SYS" },
Mark Brown942c4352009-06-05 16:32:59 +01001042
Mark Brown942c4352009-06-05 16:32:59 +01001043 { "Right Output PGA", NULL, "CLK_SYS" },
Mark Brown942c4352009-06-05 16:32:59 +01001044
Mark Brown942c4352009-06-05 16:32:59 +01001045 { "SPKL", "DAC Switch", "DACL" },
1046 { "SPKL", NULL, "CLK_SYS" },
Mark Brown942c4352009-06-05 16:32:59 +01001047
Mark Brown942c4352009-06-05 16:32:59 +01001048 { "SPKR", "DAC Switch", "DACR" },
1049 { "SPKR", NULL, "CLK_SYS" },
Mark Brown942c4352009-06-05 16:32:59 +01001050
1051 { "Left Headphone Mux", "DAC", "DACL" },
Mark Brown942c4352009-06-05 16:32:59 +01001052 { "Right Headphone Mux", "DAC", "DACR" },
Mark Brown942c4352009-06-05 16:32:59 +01001053};
1054
1055static int wm8993_set_bias_level(struct snd_soc_codec *codec,
1056 enum snd_soc_bias_level level)
1057{
Mark Brownb2c812e2010-04-14 15:35:19 +09001058 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Browncf56f622010-02-03 17:55:55 +00001059 int ret;
Mark Brown942c4352009-06-05 16:32:59 +01001060
1061 switch (level) {
1062 case SND_SOC_BIAS_ON:
1063 case SND_SOC_BIAS_PREPARE:
1064 /* VMID=2*40k */
1065 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
1066 WM8993_VMID_SEL_MASK, 0x2);
1067 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
1068 WM8993_TSHUT_ENA, WM8993_TSHUT_ENA);
1069 break;
1070
1071 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001072 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Browncf56f622010-02-03 17:55:55 +00001073 ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
1074 wm8993->supplies);
1075 if (ret != 0)
1076 return ret;
1077
Mark Brownd0ad0af2011-12-14 11:53:06 +08001078 regcache_cache_only(wm8993->regmap, false);
1079 regcache_sync(wm8993->regmap);
Mark Browncf56f622010-02-03 17:55:55 +00001080
Mark Brown942c4352009-06-05 16:32:59 +01001081 /* Bring up VMID with fast soft start */
1082 snd_soc_update_bits(codec, WM8993_ANTIPOP2,
1083 WM8993_STARTUP_BIAS_ENA |
1084 WM8993_VMID_BUF_ENA |
1085 WM8993_VMID_RAMP_MASK |
1086 WM8993_BIAS_SRC,
1087 WM8993_STARTUP_BIAS_ENA |
1088 WM8993_VMID_BUF_ENA |
1089 WM8993_VMID_RAMP_MASK |
1090 WM8993_BIAS_SRC);
1091
1092 /* If either line output is single ended we
1093 * need the VMID buffer */
1094 if (!wm8993->pdata.lineout1_diff ||
1095 !wm8993->pdata.lineout2_diff)
1096 snd_soc_update_bits(codec, WM8993_ANTIPOP1,
1097 WM8993_LINEOUT_VMID_BUF_ENA,
1098 WM8993_LINEOUT_VMID_BUF_ENA);
1099
1100 /* VMID=2*40k */
1101 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
1102 WM8993_VMID_SEL_MASK |
1103 WM8993_BIAS_ENA,
1104 WM8993_BIAS_ENA | 0x2);
1105 msleep(32);
1106
1107 /* Switch to normal bias */
1108 snd_soc_update_bits(codec, WM8993_ANTIPOP2,
1109 WM8993_BIAS_SRC |
1110 WM8993_STARTUP_BIAS_ENA, 0);
1111 }
1112
1113 /* VMID=2*240k */
1114 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
1115 WM8993_VMID_SEL_MASK, 0x4);
1116
1117 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
1118 WM8993_TSHUT_ENA, 0);
1119 break;
1120
1121 case SND_SOC_BIAS_OFF:
1122 snd_soc_update_bits(codec, WM8993_ANTIPOP1,
1123 WM8993_LINEOUT_VMID_BUF_ENA, 0);
1124
1125 snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
1126 WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
1127 0);
Mark Browncf56f622010-02-03 17:55:55 +00001128
Mark Brown83b65422010-12-14 11:25:18 +00001129 snd_soc_update_bits(codec, WM8993_ANTIPOP2,
1130 WM8993_STARTUP_BIAS_ENA |
1131 WM8993_VMID_BUF_ENA |
1132 WM8993_VMID_RAMP_MASK |
1133 WM8993_BIAS_SRC, 0);
1134
Mark Brownd0ad0af2011-12-14 11:53:06 +08001135 regcache_cache_only(wm8993->regmap, true);
1136 regcache_mark_dirty(wm8993->regmap);
Mark Browncf56f622010-02-03 17:55:55 +00001137
1138 regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies),
1139 wm8993->supplies);
Mark Brown942c4352009-06-05 16:32:59 +01001140 break;
1141 }
1142
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001143 codec->dapm.bias_level = level;
Mark Brown942c4352009-06-05 16:32:59 +01001144
1145 return 0;
1146}
1147
1148static int wm8993_set_sysclk(struct snd_soc_dai *codec_dai,
1149 int clk_id, unsigned int freq, int dir)
1150{
1151 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001152 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown942c4352009-06-05 16:32:59 +01001153
1154 switch (clk_id) {
1155 case WM8993_SYSCLK_MCLK:
1156 wm8993->mclk_rate = freq;
1157 case WM8993_SYSCLK_FLL:
1158 wm8993->sysclk_source = clk_id;
1159 break;
1160
1161 default:
1162 return -EINVAL;
1163 }
1164
1165 return 0;
1166}
1167
1168static int wm8993_set_dai_fmt(struct snd_soc_dai *dai,
1169 unsigned int fmt)
1170{
1171 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001172 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown3bf6e422010-02-01 19:05:09 +00001173 unsigned int aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
1174 unsigned int aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
Mark Brown942c4352009-06-05 16:32:59 +01001175
1176 aif1 &= ~(WM8993_BCLK_DIR | WM8993_AIF_BCLK_INV |
1177 WM8993_AIF_LRCLK_INV | WM8993_AIF_FMT_MASK);
1178 aif4 &= ~WM8993_LRCLK_DIR;
1179
1180 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1181 case SND_SOC_DAIFMT_CBS_CFS:
1182 wm8993->master = 0;
1183 break;
1184 case SND_SOC_DAIFMT_CBS_CFM:
1185 aif4 |= WM8993_LRCLK_DIR;
1186 wm8993->master = 1;
1187 break;
1188 case SND_SOC_DAIFMT_CBM_CFS:
1189 aif1 |= WM8993_BCLK_DIR;
1190 wm8993->master = 1;
1191 break;
1192 case SND_SOC_DAIFMT_CBM_CFM:
1193 aif1 |= WM8993_BCLK_DIR;
1194 aif4 |= WM8993_LRCLK_DIR;
1195 wm8993->master = 1;
1196 break;
1197 default:
1198 return -EINVAL;
1199 }
1200
1201 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1202 case SND_SOC_DAIFMT_DSP_B:
1203 aif1 |= WM8993_AIF_LRCLK_INV;
1204 case SND_SOC_DAIFMT_DSP_A:
1205 aif1 |= 0x18;
1206 break;
1207 case SND_SOC_DAIFMT_I2S:
1208 aif1 |= 0x10;
1209 break;
1210 case SND_SOC_DAIFMT_RIGHT_J:
1211 break;
1212 case SND_SOC_DAIFMT_LEFT_J:
1213 aif1 |= 0x8;
1214 break;
1215 default:
1216 return -EINVAL;
1217 }
1218
1219 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1220 case SND_SOC_DAIFMT_DSP_A:
1221 case SND_SOC_DAIFMT_DSP_B:
1222 /* frame inversion not valid for DSP modes */
1223 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1224 case SND_SOC_DAIFMT_NB_NF:
1225 break;
1226 case SND_SOC_DAIFMT_IB_NF:
1227 aif1 |= WM8993_AIF_BCLK_INV;
1228 break;
1229 default:
1230 return -EINVAL;
1231 }
1232 break;
1233
1234 case SND_SOC_DAIFMT_I2S:
1235 case SND_SOC_DAIFMT_RIGHT_J:
1236 case SND_SOC_DAIFMT_LEFT_J:
1237 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1238 case SND_SOC_DAIFMT_NB_NF:
1239 break;
1240 case SND_SOC_DAIFMT_IB_IF:
1241 aif1 |= WM8993_AIF_BCLK_INV | WM8993_AIF_LRCLK_INV;
1242 break;
1243 case SND_SOC_DAIFMT_IB_NF:
1244 aif1 |= WM8993_AIF_BCLK_INV;
1245 break;
1246 case SND_SOC_DAIFMT_NB_IF:
1247 aif1 |= WM8993_AIF_LRCLK_INV;
1248 break;
1249 default:
1250 return -EINVAL;
1251 }
1252 break;
1253 default:
1254 return -EINVAL;
1255 }
1256
Mark Brown3bf6e422010-02-01 19:05:09 +00001257 snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
1258 snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
Mark Brown942c4352009-06-05 16:32:59 +01001259
1260 return 0;
1261}
1262
1263static int wm8993_hw_params(struct snd_pcm_substream *substream,
1264 struct snd_pcm_hw_params *params,
1265 struct snd_soc_dai *dai)
1266{
1267 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001268 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown942c4352009-06-05 16:32:59 +01001269 int ret, i, best, best_val, cur_val;
1270 unsigned int clocking1, clocking3, aif1, aif4;
1271
Mark Brown3bf6e422010-02-01 19:05:09 +00001272 clocking1 = snd_soc_read(codec, WM8993_CLOCKING_1);
Mark Brown942c4352009-06-05 16:32:59 +01001273 clocking1 &= ~WM8993_BCLK_DIV_MASK;
1274
Mark Brown3bf6e422010-02-01 19:05:09 +00001275 clocking3 = snd_soc_read(codec, WM8993_CLOCKING_3);
Mark Brown942c4352009-06-05 16:32:59 +01001276 clocking3 &= ~(WM8993_CLK_SYS_RATE_MASK | WM8993_SAMPLE_RATE_MASK);
1277
Mark Brown3bf6e422010-02-01 19:05:09 +00001278 aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
Mark Brown942c4352009-06-05 16:32:59 +01001279 aif1 &= ~WM8993_AIF_WL_MASK;
1280
Mark Brown3bf6e422010-02-01 19:05:09 +00001281 aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
Mark Brown942c4352009-06-05 16:32:59 +01001282 aif4 &= ~WM8993_LRCLK_RATE_MASK;
1283
1284 /* What BCLK do we need? */
1285 wm8993->fs = params_rate(params);
1286 wm8993->bclk = 2 * wm8993->fs;
Mark Brownd3c9e9a2009-08-17 18:52:47 +01001287 if (wm8993->tdm_slots) {
1288 dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
1289 wm8993->tdm_slots, wm8993->tdm_width);
1290 wm8993->bclk *= wm8993->tdm_width * wm8993->tdm_slots;
1291 } else {
1292 switch (params_format(params)) {
1293 case SNDRV_PCM_FORMAT_S16_LE:
1294 wm8993->bclk *= 16;
1295 break;
1296 case SNDRV_PCM_FORMAT_S20_3LE:
1297 wm8993->bclk *= 20;
1298 aif1 |= 0x8;
1299 break;
1300 case SNDRV_PCM_FORMAT_S24_LE:
1301 wm8993->bclk *= 24;
1302 aif1 |= 0x10;
1303 break;
1304 case SNDRV_PCM_FORMAT_S32_LE:
1305 wm8993->bclk *= 32;
1306 aif1 |= 0x18;
1307 break;
1308 default:
1309 return -EINVAL;
1310 }
Mark Brown942c4352009-06-05 16:32:59 +01001311 }
1312
1313 dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8993->bclk);
1314
1315 ret = configure_clock(codec);
1316 if (ret != 0)
1317 return ret;
1318
1319 /* Select nearest CLK_SYS_RATE */
1320 best = 0;
1321 best_val = abs((wm8993->sysclk_rate / clk_sys_rates[0].ratio)
1322 - wm8993->fs);
1323 for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1324 cur_val = abs((wm8993->sysclk_rate /
Joe Perchesef995e32010-11-15 09:09:17 -08001325 clk_sys_rates[i].ratio) - wm8993->fs);
Mark Brown942c4352009-06-05 16:32:59 +01001326 if (cur_val < best_val) {
1327 best = i;
1328 best_val = cur_val;
1329 }
1330 }
1331 dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
1332 clk_sys_rates[best].ratio);
1333 clocking3 |= (clk_sys_rates[best].clk_sys_rate
1334 << WM8993_CLK_SYS_RATE_SHIFT);
1335
1336 /* SAMPLE_RATE */
1337 best = 0;
1338 best_val = abs(wm8993->fs - sample_rates[0].rate);
1339 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1340 /* Closest match */
1341 cur_val = abs(wm8993->fs - sample_rates[i].rate);
1342 if (cur_val < best_val) {
1343 best = i;
1344 best_val = cur_val;
1345 }
1346 }
1347 dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
1348 sample_rates[best].rate);
Mark Browne465d542009-07-15 10:01:30 +01001349 clocking3 |= (sample_rates[best].sample_rate
1350 << WM8993_SAMPLE_RATE_SHIFT);
Mark Brown942c4352009-06-05 16:32:59 +01001351
1352 /* BCLK_DIV */
1353 best = 0;
1354 best_val = INT_MAX;
1355 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1356 cur_val = ((wm8993->sysclk_rate * 10) / bclk_divs[i].div)
1357 - wm8993->bclk;
1358 if (cur_val < 0) /* Table is sorted */
1359 break;
1360 if (cur_val < best_val) {
1361 best = i;
1362 best_val = cur_val;
1363 }
1364 }
1365 wm8993->bclk = (wm8993->sysclk_rate * 10) / bclk_divs[best].div;
1366 dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1367 bclk_divs[best].div, wm8993->bclk);
1368 clocking1 |= bclk_divs[best].bclk_div << WM8993_BCLK_DIV_SHIFT;
1369
1370 /* LRCLK is a simple fraction of BCLK */
1371 dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8993->bclk / wm8993->fs);
1372 aif4 |= wm8993->bclk / wm8993->fs;
1373
Mark Brown3bf6e422010-02-01 19:05:09 +00001374 snd_soc_write(codec, WM8993_CLOCKING_1, clocking1);
1375 snd_soc_write(codec, WM8993_CLOCKING_3, clocking3);
1376 snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
1377 snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
Mark Brown942c4352009-06-05 16:32:59 +01001378
1379 /* ReTune Mobile? */
1380 if (wm8993->pdata.num_retune_configs) {
Mark Brown3bf6e422010-02-01 19:05:09 +00001381 u16 eq1 = snd_soc_read(codec, WM8993_EQ1);
Mark Brown942c4352009-06-05 16:32:59 +01001382 struct wm8993_retune_mobile_setting *s;
1383
1384 best = 0;
1385 best_val = abs(wm8993->pdata.retune_configs[0].rate
1386 - wm8993->fs);
1387 for (i = 0; i < wm8993->pdata.num_retune_configs; i++) {
1388 cur_val = abs(wm8993->pdata.retune_configs[i].rate
1389 - wm8993->fs);
1390 if (cur_val < best_val) {
1391 best_val = cur_val;
1392 best = i;
1393 }
1394 }
1395 s = &wm8993->pdata.retune_configs[best];
1396
1397 dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
1398 s->name, s->rate);
1399
1400 /* Disable EQ while we reconfigure */
1401 snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, 0);
1402
1403 for (i = 1; i < ARRAY_SIZE(s->config); i++)
Mark Brown3bf6e422010-02-01 19:05:09 +00001404 snd_soc_write(codec, WM8993_EQ1 + i, s->config[i]);
Mark Brown942c4352009-06-05 16:32:59 +01001405
1406 snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, eq1);
1407 }
1408
1409 return 0;
1410}
1411
1412static int wm8993_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1413{
1414 struct snd_soc_codec *codec = codec_dai->codec;
1415 unsigned int reg;
1416
Mark Brown3bf6e422010-02-01 19:05:09 +00001417 reg = snd_soc_read(codec, WM8993_DAC_CTRL);
Mark Brown942c4352009-06-05 16:32:59 +01001418
1419 if (mute)
1420 reg |= WM8993_DAC_MUTE;
1421 else
1422 reg &= ~WM8993_DAC_MUTE;
1423
Mark Brown3bf6e422010-02-01 19:05:09 +00001424 snd_soc_write(codec, WM8993_DAC_CTRL, reg);
Mark Brown942c4352009-06-05 16:32:59 +01001425
1426 return 0;
1427}
1428
Mark Brownd3c9e9a2009-08-17 18:52:47 +01001429static int wm8993_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1430 unsigned int rx_mask, int slots, int slot_width)
1431{
1432 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001433 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brownd3c9e9a2009-08-17 18:52:47 +01001434 int aif1 = 0;
1435 int aif2 = 0;
1436
1437 /* Don't need to validate anything if we're turning off TDM */
1438 if (slots == 0) {
1439 wm8993->tdm_slots = 0;
1440 goto out;
1441 }
1442
1443 /* Note that we allow configurations we can't handle ourselves -
1444 * for example, we can generate clocks for slots 2 and up even if
1445 * we can't use those slots ourselves.
1446 */
1447 aif1 |= WM8993_AIFADC_TDM;
1448 aif2 |= WM8993_AIFDAC_TDM;
1449
1450 switch (rx_mask) {
1451 case 3:
1452 break;
1453 case 0xc:
1454 aif1 |= WM8993_AIFADC_TDM_CHAN;
1455 break;
1456 default:
1457 return -EINVAL;
1458 }
1459
1460
1461 switch (tx_mask) {
1462 case 3:
1463 break;
1464 case 0xc:
1465 aif2 |= WM8993_AIFDAC_TDM_CHAN;
1466 break;
1467 default:
1468 return -EINVAL;
1469 }
1470
1471out:
1472 wm8993->tdm_width = slot_width;
1473 wm8993->tdm_slots = slots / 2;
1474
1475 snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_1,
1476 WM8993_AIFADC_TDM | WM8993_AIFADC_TDM_CHAN, aif1);
1477 snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_2,
1478 WM8993_AIFDAC_TDM | WM8993_AIFDAC_TDM_CHAN, aif2);
1479
1480 return 0;
1481}
1482
Mark Brown164548d2012-01-17 16:42:05 +00001483static irqreturn_t wm8993_irq(int irq, void *data)
1484{
1485 struct wm8993_priv *wm8993 = data;
1486 int mask, val, ret;
1487
1488 ret = regmap_read(wm8993->regmap, WM8993_GPIO_CTRL_1, &val);
1489 if (ret != 0) {
1490 dev_err(wm8993->dev, "Failed to read interrupt status: %d\n",
1491 ret);
1492 return IRQ_NONE;
1493 }
1494
1495 ret = regmap_read(wm8993->regmap, WM8993_GPIOCTRL_2, &mask);
1496 if (ret != 0) {
1497 dev_err(wm8993->dev, "Failed to read interrupt mask: %d\n",
1498 ret);
1499 return IRQ_NONE;
1500 }
1501
1502 /* The IRQ pin status is visible in the register too */
1503 val &= ~(mask | WM8993_IRQ);
1504 if (!val)
1505 return IRQ_NONE;
1506
1507 if (val & WM8993_TEMPOK_EINT)
1508 dev_crit(wm8993->dev, "Thermal warning\n");
1509
1510 if (val & WM8993_FLL_LOCK_EINT) {
1511 dev_dbg(wm8993->dev, "FLL locked\n");
1512 complete(&wm8993->fll_lock);
1513 }
1514
1515 ret = regmap_write(wm8993->regmap, WM8993_GPIO_CTRL_1, val);
1516 if (ret != 0)
1517 dev_err(wm8993->dev, "Failed to ack interrupt: %d\n", ret);
1518
1519 return IRQ_HANDLED;
1520}
1521
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001522static const struct snd_soc_dai_ops wm8993_ops = {
Mark Brown942c4352009-06-05 16:32:59 +01001523 .set_sysclk = wm8993_set_sysclk,
1524 .set_fmt = wm8993_set_dai_fmt,
1525 .hw_params = wm8993_hw_params,
1526 .digital_mute = wm8993_digital_mute,
1527 .set_pll = wm8993_set_fll,
Mark Brownd3c9e9a2009-08-17 18:52:47 +01001528 .set_tdm_slot = wm8993_set_tdm_slot,
Mark Brown942c4352009-06-05 16:32:59 +01001529};
1530
1531#define WM8993_RATES SNDRV_PCM_RATE_8000_48000
1532
1533#define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1534 SNDRV_PCM_FMTBIT_S20_3LE |\
1535 SNDRV_PCM_FMTBIT_S24_LE |\
1536 SNDRV_PCM_FMTBIT_S32_LE)
1537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538static struct snd_soc_dai_driver wm8993_dai = {
1539 .name = "wm8993-hifi",
Mark Brown942c4352009-06-05 16:32:59 +01001540 .playback = {
1541 .stream_name = "Playback",
1542 .channels_min = 1,
1543 .channels_max = 2,
1544 .rates = WM8993_RATES,
1545 .formats = WM8993_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00001546 .sig_bits = 24,
Mark Brown942c4352009-06-05 16:32:59 +01001547 },
1548 .capture = {
1549 .stream_name = "Capture",
1550 .channels_min = 1,
1551 .channels_max = 2,
1552 .rates = WM8993_RATES,
1553 .formats = WM8993_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00001554 .sig_bits = 24,
Mark Brown942c4352009-06-05 16:32:59 +01001555 },
1556 .ops = &wm8993_ops,
1557 .symmetric_rates = 1,
1558};
Mark Brown942c4352009-06-05 16:32:59 +01001559
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560static int wm8993_probe(struct snd_soc_codec *codec)
Mark Brown942c4352009-06-05 16:32:59 +01001561{
Mark Brownb2c812e2010-04-14 15:35:19 +09001562 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001563 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownbfea3ab2011-12-14 12:31:14 +08001564 int ret;
Mark Brown53242c62010-01-02 13:15:56 +00001565
Mark Brown3ed70742010-01-20 17:39:45 +00001566 wm8993->hubs_data.hp_startup_mode = 1;
Mark Brown4537c4e2011-08-01 13:10:16 +09001567 wm8993->hubs_data.dcs_codes_l = -2;
1568 wm8993->hubs_data.dcs_codes_r = -2;
Mark Brownf9acf9f2011-06-07 23:23:52 +01001569 wm8993->hubs_data.series_startup = 1;
Mark Brown3ed70742010-01-20 17:39:45 +00001570
Mark Brownd0ad0af2011-12-14 11:53:06 +08001571 codec->control_data = wm8993->regmap;
1572 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
Mark Brown3bf6e422010-02-01 19:05:09 +00001573 if (ret != 0) {
1574 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575 return ret;
Mark Brown3bf6e422010-02-01 19:05:09 +00001576 }
1577
Mark Brown942c4352009-06-05 16:32:59 +01001578 /* By default we're using the output mixers */
1579 wm8993->class_w_users = 2;
1580
1581 /* Latch volume update bits and default ZC on */
Mark Brown942c4352009-06-05 16:32:59 +01001582 snd_soc_update_bits(codec, WM8993_RIGHT_DAC_DIGITAL_VOLUME,
1583 WM8993_DAC_VU, WM8993_DAC_VU);
1584 snd_soc_update_bits(codec, WM8993_RIGHT_ADC_DIGITAL_VOLUME,
1585 WM8993_ADC_VU, WM8993_ADC_VU);
1586
1587 /* Manualy manage the HPOUT sequencing for independent stereo
1588 * control. */
1589 snd_soc_update_bits(codec, WM8993_ANALOGUE_HP_0,
1590 WM8993_HPOUT1_AUTO_PU, 0);
1591
1592 /* Use automatic clock configuration */
1593 snd_soc_update_bits(codec, WM8993_CLOCKING_4, WM8993_SR_MODE, 0);
1594
Mark Brownaa983d92009-09-30 14:16:11 +01001595 wm_hubs_handle_analogue_pdata(codec, wm8993->pdata.lineout1_diff,
1596 wm8993->pdata.lineout2_diff,
1597 wm8993->pdata.lineout1fb,
1598 wm8993->pdata.lineout2fb,
1599 wm8993->pdata.jd_scthr,
1600 wm8993->pdata.jd_thr,
1601 wm8993->pdata.micbias1_lvl,
1602 wm8993->pdata.micbias2_lvl);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001603
Mark Brown942c4352009-06-05 16:32:59 +01001604 ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1605 if (ret != 0)
Mark Brownbfea3ab2011-12-14 12:31:14 +08001606 return ret;
Mark Brown942c4352009-06-05 16:32:59 +01001607
Liam Girdwood022658b2012-02-03 17:43:09 +00001608 snd_soc_add_codec_controls(codec, wm8993_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 ARRAY_SIZE(wm8993_snd_controls));
1610 if (wm8993->pdata.num_retune_configs != 0) {
1611 dev_dbg(codec->dev, "Using ReTune Mobile\n");
1612 } else {
1613 dev_dbg(codec->dev, "No ReTune Mobile, using normal EQ\n");
Liam Girdwood022658b2012-02-03 17:43:09 +00001614 snd_soc_add_codec_controls(codec, wm8993_eq_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615 ARRAY_SIZE(wm8993_eq_controls));
1616 }
Mark Brown942c4352009-06-05 16:32:59 +01001617
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001618 snd_soc_dapm_new_controls(dapm, wm8993_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619 ARRAY_SIZE(wm8993_dapm_widgets));
1620 wm_hubs_add_analogue_controls(codec);
Mark Brown942c4352009-06-05 16:32:59 +01001621
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001622 snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623 wm_hubs_add_analogue_routes(codec, wm8993->pdata.lineout1_diff,
1624 wm8993->pdata.lineout2_diff);
Mark Brown942c4352009-06-05 16:32:59 +01001625
Mark Brownf959dee2012-01-31 16:16:47 +00001626 /* If the line outputs are differential then we aren't presenting
1627 * VMID as an output and can disable it.
1628 */
1629 if (wm8993->pdata.lineout1_diff && wm8993->pdata.lineout2_diff)
1630 codec->dapm.idle_bias_off = 1;
1631
Mark Brown942c4352009-06-05 16:32:59 +01001632 return 0;
1633
Mark Brown942c4352009-06-05 16:32:59 +01001634}
1635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001636static int wm8993_remove(struct snd_soc_codec *codec)
Mark Brown942c4352009-06-05 16:32:59 +01001637{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
Mark Brown942c4352009-06-05 16:32:59 +01001639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brownb37e3992010-02-03 11:51:42 +00001641 regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 return 0;
1643}
Mark Brown942c4352009-06-05 16:32:59 +01001644
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001645#ifdef CONFIG_PM
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +01001646static int wm8993_suspend(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647{
1648 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1649 int fll_fout = wm8993->fll_fout;
1650 int fll_fref = wm8993->fll_fref;
1651 int ret;
1652
1653 /* Stop the FLL in an orderly fashion */
1654 ret = _wm8993_set_fll(codec, 0, 0, 0, 0);
1655 if (ret != 0) {
1656 dev_err(codec->dev, "Failed to stop FLL\n");
1657 return ret;
1658 }
1659
1660 wm8993->fll_fout = fll_fout;
1661 wm8993->fll_fref = fll_fref;
1662
1663 wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
1664
1665 return 0;
1666}
1667
1668static int wm8993_resume(struct snd_soc_codec *codec)
1669{
1670 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1671 int ret;
1672
1673 wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1674
1675 /* Restart the FLL? */
1676 if (wm8993->fll_fout) {
1677 int fll_fout = wm8993->fll_fout;
1678 int fll_fref = wm8993->fll_fref;
1679
1680 wm8993->fll_fref = 0;
1681 wm8993->fll_fout = 0;
1682
1683 ret = _wm8993_set_fll(codec, 0, wm8993->fll_src,
1684 fll_fref, fll_fout);
1685 if (ret != 0)
1686 dev_err(codec->dev, "Failed to restart FLL\n");
1687 }
1688
1689 return 0;
1690}
1691#else
1692#define wm8993_suspend NULL
1693#define wm8993_resume NULL
1694#endif
1695
Mark Brown489773c2012-01-31 15:20:24 +00001696/* Tune DC servo configuration */
1697static struct reg_default wm8993_regmap_patch[] = {
1698 { 0x44, 3 },
1699 { 0x56, 3 },
1700 { 0x44, 0 },
1701};
1702
Mark Brownd0ad0af2011-12-14 11:53:06 +08001703static const struct regmap_config wm8993_regmap = {
1704 .reg_bits = 8,
1705 .val_bits = 16,
1706
1707 .max_register = WM8993_MAX_REGISTER,
1708 .volatile_reg = wm8993_volatile,
1709 .readable_reg = wm8993_readable,
1710
1711 .cache_type = REGCACHE_RBTREE,
1712 .reg_defaults = wm8993_reg_defaults,
1713 .num_reg_defaults = ARRAY_SIZE(wm8993_reg_defaults),
1714};
1715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001716static struct snd_soc_codec_driver soc_codec_dev_wm8993 = {
1717 .probe = wm8993_probe,
1718 .remove = wm8993_remove,
1719 .suspend = wm8993_suspend,
1720 .resume = wm8993_resume,
1721 .set_bias_level = wm8993_set_bias_level,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001722};
1723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724static __devinit int wm8993_i2c_probe(struct i2c_client *i2c,
1725 const struct i2c_device_id *id)
1726{
1727 struct wm8993_priv *wm8993;
Mark Brownbfea3ab2011-12-14 12:31:14 +08001728 unsigned int reg;
1729 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730
Mark Brownec641c42011-12-15 11:54:00 +08001731 wm8993 = devm_kzalloc(&i2c->dev, sizeof(struct wm8993_priv),
Mark Brownf6a93362011-12-14 11:11:52 +08001732 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001733 if (wm8993 == NULL)
1734 return -ENOMEM;
1735
Mark Brown164548d2012-01-17 16:42:05 +00001736 wm8993->dev = &i2c->dev;
1737 init_completion(&wm8993->fll_lock);
1738
Mark Brownd0ad0af2011-12-14 11:53:06 +08001739 wm8993->regmap = regmap_init_i2c(i2c, &wm8993_regmap);
1740 if (IS_ERR(wm8993->regmap)) {
1741 ret = PTR_ERR(wm8993->regmap);
1742 dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
1743 return ret;
1744 }
1745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 i2c_set_clientdata(i2c, wm8993);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747
Mark Brownbfea3ab2011-12-14 12:31:14 +08001748 for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
1749 wm8993->supplies[i].supply = wm8993_supply_names[i];
1750
1751 ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8993->supplies),
1752 wm8993->supplies);
1753 if (ret != 0) {
1754 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
1755 goto err;
1756 }
1757
1758 ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
1759 wm8993->supplies);
1760 if (ret != 0) {
1761 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
1762 goto err_get;
1763 }
1764
1765 ret = regmap_read(wm8993->regmap, WM8993_SOFTWARE_RESET, &reg);
1766 if (ret != 0) {
1767 dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
1768 goto err_enable;
1769 }
1770
1771 if (reg != 0x8993) {
1772 dev_err(&i2c->dev, "Invalid ID register value %x\n", reg);
1773 ret = -EINVAL;
1774 goto err_enable;
1775 }
1776
1777 ret = regmap_write(wm8993->regmap, WM8993_SOFTWARE_RESET, 0xffff);
1778 if (ret != 0)
1779 goto err_enable;
1780
Mark Brown489773c2012-01-31 15:20:24 +00001781 ret = regmap_register_patch(wm8993->regmap, wm8993_regmap_patch,
1782 ARRAY_SIZE(wm8993_regmap_patch));
1783 if (ret != 0)
1784 dev_warn(wm8993->dev, "Failed to apply regmap patch: %d\n",
1785 ret);
1786
Mark Brown164548d2012-01-17 16:42:05 +00001787 if (i2c->irq) {
1788 /* Put GPIO1 into interrupt mode (only GPIO1 can output IRQ) */
1789 ret = regmap_update_bits(wm8993->regmap, WM8993_GPIO1,
1790 WM8993_GPIO1_PD |
1791 WM8993_GPIO1_SEL_MASK, 7);
1792 if (ret != 0)
1793 goto err_enable;
1794
1795 ret = request_threaded_irq(i2c->irq, NULL, wm8993_irq,
1796 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1797 "wm8993", wm8993);
1798 if (ret != 0)
1799 goto err_enable;
1800
1801 }
1802
Mark Brownbfea3ab2011-12-14 12:31:14 +08001803 regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
1804
1805 regcache_cache_only(wm8993->regmap, true);
1806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 ret = snd_soc_register_codec(&i2c->dev,
1808 &soc_codec_dev_wm8993, &wm8993_dai, 1);
Mark Brownd0ad0af2011-12-14 11:53:06 +08001809 if (ret != 0) {
1810 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
Mark Brown164548d2012-01-17 16:42:05 +00001811 goto err_irq;
Mark Brownd0ad0af2011-12-14 11:53:06 +08001812 }
1813
Mark Brownbfea3ab2011-12-14 12:31:14 +08001814 return 0;
Mark Brownd0ad0af2011-12-14 11:53:06 +08001815
Mark Brown164548d2012-01-17 16:42:05 +00001816err_irq:
1817 if (i2c->irq)
1818 free_irq(i2c->irq, wm8993);
Mark Brownbfea3ab2011-12-14 12:31:14 +08001819err_enable:
1820 regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
1821err_get:
1822 regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
Mark Brownd0ad0af2011-12-14 11:53:06 +08001823err:
1824 regmap_exit(wm8993->regmap);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001825 return ret;
1826}
1827
Mark Brown164548d2012-01-17 16:42:05 +00001828static __devexit int wm8993_i2c_remove(struct i2c_client *i2c)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829{
Mark Brown164548d2012-01-17 16:42:05 +00001830 struct wm8993_priv *wm8993 = i2c_get_clientdata(i2c);
Mark Brownd0ad0af2011-12-14 11:53:06 +08001831
Mark Brown164548d2012-01-17 16:42:05 +00001832 snd_soc_unregister_codec(&i2c->dev);
1833 if (i2c->irq)
1834 free_irq(i2c->irq, wm8993);
Mark Brownd0ad0af2011-12-14 11:53:06 +08001835 regmap_exit(wm8993->regmap);
Mark Brownbfea3ab2011-12-14 12:31:14 +08001836 regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
1837 regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
Mark Brownd0ad0af2011-12-14 11:53:06 +08001838
Mark Brown942c4352009-06-05 16:32:59 +01001839 return 0;
1840}
1841
1842static const struct i2c_device_id wm8993_i2c_id[] = {
1843 { "wm8993", 0 },
1844 { }
1845};
1846MODULE_DEVICE_TABLE(i2c, wm8993_i2c_id);
1847
1848static struct i2c_driver wm8993_i2c_driver = {
1849 .driver = {
Mark Brown091edcc2011-12-02 22:08:49 +00001850 .name = "wm8993",
Mark Brown942c4352009-06-05 16:32:59 +01001851 .owner = THIS_MODULE,
1852 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001853 .probe = wm8993_i2c_probe,
1854 .remove = __devexit_p(wm8993_i2c_remove),
Mark Brown942c4352009-06-05 16:32:59 +01001855 .id_table = wm8993_i2c_id,
1856};
Mark Brown942c4352009-06-05 16:32:59 +01001857
1858static int __init wm8993_modinit(void)
1859{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001860 int ret = 0;
Mark Brown942c4352009-06-05 16:32:59 +01001861 ret = i2c_add_driver(&wm8993_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001862 if (ret != 0) {
1863 pr_err("WM8993: Unable to register I2C driver: %d\n",
1864 ret);
1865 }
Mark Brown942c4352009-06-05 16:32:59 +01001866 return ret;
1867}
1868module_init(wm8993_modinit);
1869
1870static void __exit wm8993_exit(void)
1871{
1872 i2c_del_driver(&wm8993_i2c_driver);
Mark Brown942c4352009-06-05 16:32:59 +01001873}
1874module_exit(wm8993_exit);
1875
1876
1877MODULE_DESCRIPTION("ASoC WM8993 driver");
1878MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1879MODULE_LICENSE("GPL");