Linux Audio

Check our new training course

Loading...
  1/*
  2 * wm8770.c  --  WM8770 ALSA SoC Audio driver
  3 *
  4 * Copyright 2010 Wolfson Microelectronics plc
  5 *
  6 * Author: Dimitris Papastamos <dp@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/of_device.h>
 18#include <linux/pm.h>
 
 19#include <linux/spi/spi.h>
 20#include <linux/regmap.h>
 21#include <linux/regulator/consumer.h>
 22#include <linux/slab.h>
 23#include <sound/core.h>
 24#include <sound/pcm.h>
 25#include <sound/pcm_params.h>
 26#include <sound/soc.h>
 27#include <sound/initval.h>
 28#include <sound/tlv.h>
 29
 30#include "wm8770.h"
 31
 32#define WM8770_NUM_SUPPLIES 3
 33static const char *wm8770_supply_names[WM8770_NUM_SUPPLIES] = {
 34	"AVDD1",
 35	"AVDD2",
 36	"DVDD"
 37};
 38
 39static const struct reg_default wm8770_reg_defaults[] = {
 40	{  0, 0x7f },
 41	{  1, 0x7f },
 42	{  2, 0x7f },
 43	{  3, 0x7f },
 44	{  4, 0x7f },
 45	{  5, 0x7f },
 46	{  6, 0x7f },
 47	{  7, 0x7f },
 48	{  8, 0x7f },
 49	{  9, 0xff },
 50	{ 10, 0xff },
 51	{ 11, 0xff },
 52	{ 12, 0xff },
 53	{ 13, 0xff },
 54	{ 14, 0xff },
 55	{ 15, 0xff },
 56	{ 16, 0xff },
 57	{ 17, 0xff },
 58	{ 18, 0    },
 59	{ 19, 0x90 },
 60	{ 20, 0    },
 61	{ 21, 0    },
 62	{ 22, 0x22 },
 63	{ 23, 0x22 },
 64	{ 24, 0x3e },
 65	{ 25, 0xc  },
 66	{ 26, 0xc  },
 67	{ 27, 0x100 },
 68	{ 28, 0x189 },
 69	{ 29, 0x189 },
 70	{ 30, 0x8770 },
 71};
 72
 73static bool wm8770_volatile_reg(struct device *dev, unsigned int reg)
 74{
 75	switch (reg) {
 76	case WM8770_RESET:
 77		return true;
 78	default:
 79		return false;
 80	}
 81}
 82
 83struct wm8770_priv {
 84	struct regmap *regmap;
 85	struct regulator_bulk_data supplies[WM8770_NUM_SUPPLIES];
 86	struct notifier_block disable_nb[WM8770_NUM_SUPPLIES];
 87	struct snd_soc_component *component;
 88	int sysclk;
 89};
 90
 91static int vout12supply_event(struct snd_soc_dapm_widget *w,
 92	struct snd_kcontrol *kcontrol, int event);
 93static int vout34supply_event(struct snd_soc_dapm_widget *w,
 94	struct snd_kcontrol *kcontrol, int event);
 95
 96/*
 97 * We can't use the same notifier block for more than one supply and
 98 * there's no way I can see to get from a callback to the caller
 99 * except container_of().
100 */
101#define WM8770_REGULATOR_EVENT(n) \
102static int wm8770_regulator_event_##n(struct notifier_block *nb, \
103				      unsigned long event, void *data)    \
104{ \
105	struct wm8770_priv *wm8770 = container_of(nb, struct wm8770_priv, \
106				     disable_nb[n]); \
107	if (event & REGULATOR_EVENT_DISABLE) { \
108		regcache_mark_dirty(wm8770->regmap);	\
109	} \
110	return 0; \
111}
112
113WM8770_REGULATOR_EVENT(0)
114WM8770_REGULATOR_EVENT(1)
115WM8770_REGULATOR_EVENT(2)
116
117static const DECLARE_TLV_DB_SCALE(adc_tlv, -1200, 100, 0);
118static const DECLARE_TLV_DB_SCALE(dac_dig_tlv, -12750, 50, 1);
119static const DECLARE_TLV_DB_SCALE(dac_alg_tlv, -12700, 100, 1);
120
121static const char *dac_phase_text[][2] = {
122	{ "DAC1 Normal", "DAC1 Inverted" },
123	{ "DAC2 Normal", "DAC2 Inverted" },
124	{ "DAC3 Normal", "DAC3 Inverted" },
125	{ "DAC4 Normal", "DAC4 Inverted" },
126};
127
128static const struct soc_enum dac_phase[] = {
129	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 0, 1, 2, dac_phase_text[0]),
130	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 2, 3, 2, dac_phase_text[1]),
131	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 4, 5, 2, dac_phase_text[2]),
132	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 6, 7, 2, dac_phase_text[3]),
133};
134
135static const struct snd_kcontrol_new wm8770_snd_controls[] = {
136	/* global DAC playback controls */
137	SOC_SINGLE_TLV("DAC Playback Volume", WM8770_MSDIGVOL, 0, 255, 0,
138		dac_dig_tlv),
139	SOC_SINGLE("DAC Playback Switch", WM8770_DACMUTE, 4, 1, 1),
140	SOC_SINGLE("DAC Playback ZC Switch", WM8770_DACCTRL1, 0, 1, 0),
141
142	/* global VOUT playback controls */
143	SOC_SINGLE_TLV("VOUT Playback Volume", WM8770_MSALGVOL, 0, 127, 0,
144		dac_alg_tlv),
145	SOC_SINGLE("VOUT Playback ZC Switch", WM8770_MSALGVOL, 7, 1, 0),
146
147	/* VOUT1/2/3/4 specific controls */
148	SOC_DOUBLE_R_TLV("VOUT1 Playback Volume", WM8770_VOUT1LVOL,
149		WM8770_VOUT1RVOL, 0, 127, 0, dac_alg_tlv),
150	SOC_DOUBLE_R("VOUT1 Playback ZC Switch", WM8770_VOUT1LVOL,
151		WM8770_VOUT1RVOL, 7, 1, 0),
152	SOC_DOUBLE_R_TLV("VOUT2 Playback Volume", WM8770_VOUT2LVOL,
153		WM8770_VOUT2RVOL, 0, 127, 0, dac_alg_tlv),
154	SOC_DOUBLE_R("VOUT2 Playback ZC Switch", WM8770_VOUT2LVOL,
155		WM8770_VOUT2RVOL, 7, 1, 0),
156	SOC_DOUBLE_R_TLV("VOUT3 Playback Volume", WM8770_VOUT3LVOL,
157		WM8770_VOUT3RVOL, 0, 127, 0, dac_alg_tlv),
158	SOC_DOUBLE_R("VOUT3 Playback ZC Switch", WM8770_VOUT3LVOL,
159		WM8770_VOUT3RVOL, 7, 1, 0),
160	SOC_DOUBLE_R_TLV("VOUT4 Playback Volume", WM8770_VOUT4LVOL,
161		WM8770_VOUT4RVOL, 0, 127, 0, dac_alg_tlv),
162	SOC_DOUBLE_R("VOUT4 Playback ZC Switch", WM8770_VOUT4LVOL,
163		WM8770_VOUT4RVOL, 7, 1, 0),
164
165	/* DAC1/2/3/4 specific controls */
166	SOC_DOUBLE_R_TLV("DAC1 Playback Volume", WM8770_DAC1LVOL,
167		WM8770_DAC1RVOL, 0, 255, 0, dac_dig_tlv),
168	SOC_SINGLE("DAC1 Deemphasis Switch", WM8770_DACCTRL2, 0, 1, 0),
169	SOC_ENUM("DAC1 Phase", dac_phase[0]),
170	SOC_DOUBLE_R_TLV("DAC2 Playback Volume", WM8770_DAC2LVOL,
171		WM8770_DAC2RVOL, 0, 255, 0, dac_dig_tlv),
172	SOC_SINGLE("DAC2 Deemphasis Switch", WM8770_DACCTRL2, 1, 1, 0),
173	SOC_ENUM("DAC2 Phase", dac_phase[1]),
174	SOC_DOUBLE_R_TLV("DAC3 Playback Volume", WM8770_DAC3LVOL,
175		WM8770_DAC3RVOL, 0, 255, 0, dac_dig_tlv),
176	SOC_SINGLE("DAC3 Deemphasis Switch", WM8770_DACCTRL2, 2, 1, 0),
177	SOC_ENUM("DAC3 Phase", dac_phase[2]),
178	SOC_DOUBLE_R_TLV("DAC4 Playback Volume", WM8770_DAC4LVOL,
179		WM8770_DAC4RVOL, 0, 255, 0, dac_dig_tlv),
180	SOC_SINGLE("DAC4 Deemphasis Switch", WM8770_DACCTRL2, 3, 1, 0),
181	SOC_ENUM("DAC4 Phase", dac_phase[3]),
182
183	/* ADC specific controls */
184	SOC_DOUBLE_R_TLV("Capture Volume", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
185		0, 31, 0, adc_tlv),
186	SOC_DOUBLE_R("Capture Switch", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
187		5, 1, 1),
188
189	/* other controls */
190	SOC_SINGLE("ADC 128x Oversampling Switch", WM8770_MSTRCTRL, 3, 1, 0),
191	SOC_SINGLE("ADC Highpass Filter Switch", WM8770_IFACECTRL, 8, 1, 1)
192};
193
194static const char *ain_text[] = {
195	"AIN1", "AIN2", "AIN3", "AIN4",
196	"AIN5", "AIN6", "AIN7", "AIN8"
197};
198
199static SOC_ENUM_DOUBLE_DECL(ain_enum,
200			    WM8770_ADCMUX, 0, 4, ain_text);
201
202static const struct snd_kcontrol_new ain_mux =
203	SOC_DAPM_ENUM("Capture Mux", ain_enum);
204
205static const struct snd_kcontrol_new vout1_mix_controls[] = {
206	SOC_DAPM_SINGLE("DAC1 Switch", WM8770_OUTMUX1, 0, 1, 0),
207	SOC_DAPM_SINGLE("AUX1 Switch", WM8770_OUTMUX1, 1, 1, 0),
208	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 2, 1, 0)
209};
210
211static const struct snd_kcontrol_new vout2_mix_controls[] = {
212	SOC_DAPM_SINGLE("DAC2 Switch", WM8770_OUTMUX1, 3, 1, 0),
213	SOC_DAPM_SINGLE("AUX2 Switch", WM8770_OUTMUX1, 4, 1, 0),
214	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 5, 1, 0)
215};
216
217static const struct snd_kcontrol_new vout3_mix_controls[] = {
218	SOC_DAPM_SINGLE("DAC3 Switch", WM8770_OUTMUX2, 0, 1, 0),
219	SOC_DAPM_SINGLE("AUX3 Switch", WM8770_OUTMUX2, 1, 1, 0),
220	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 2, 1, 0)
221};
222
223static const struct snd_kcontrol_new vout4_mix_controls[] = {
224	SOC_DAPM_SINGLE("DAC4 Switch", WM8770_OUTMUX2, 3, 1, 0),
225	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 4, 1, 0)
226};
227
228static const struct snd_soc_dapm_widget wm8770_dapm_widgets[] = {
229	SND_SOC_DAPM_INPUT("AUX1"),
230	SND_SOC_DAPM_INPUT("AUX2"),
231	SND_SOC_DAPM_INPUT("AUX3"),
232
233	SND_SOC_DAPM_INPUT("AIN1"),
234	SND_SOC_DAPM_INPUT("AIN2"),
235	SND_SOC_DAPM_INPUT("AIN3"),
236	SND_SOC_DAPM_INPUT("AIN4"),
237	SND_SOC_DAPM_INPUT("AIN5"),
238	SND_SOC_DAPM_INPUT("AIN6"),
239	SND_SOC_DAPM_INPUT("AIN7"),
240	SND_SOC_DAPM_INPUT("AIN8"),
241
242	SND_SOC_DAPM_MUX("Capture Mux", WM8770_ADCMUX, 8, 1, &ain_mux),
243
244	SND_SOC_DAPM_ADC("ADC", "Capture", WM8770_PWDNCTRL, 1, 1),
245
246	SND_SOC_DAPM_DAC("DAC1", "Playback", WM8770_PWDNCTRL, 2, 1),
247	SND_SOC_DAPM_DAC("DAC2", "Playback", WM8770_PWDNCTRL, 3, 1),
248	SND_SOC_DAPM_DAC("DAC3", "Playback", WM8770_PWDNCTRL, 4, 1),
249	SND_SOC_DAPM_DAC("DAC4", "Playback", WM8770_PWDNCTRL, 5, 1),
250
251	SND_SOC_DAPM_SUPPLY("VOUT12 Supply", SND_SOC_NOPM, 0, 0,
252		vout12supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
253	SND_SOC_DAPM_SUPPLY("VOUT34 Supply", SND_SOC_NOPM, 0, 0,
254		vout34supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
255
256	SND_SOC_DAPM_MIXER("VOUT1 Mixer", SND_SOC_NOPM, 0, 0,
257		vout1_mix_controls, ARRAY_SIZE(vout1_mix_controls)),
258	SND_SOC_DAPM_MIXER("VOUT2 Mixer", SND_SOC_NOPM, 0, 0,
259		vout2_mix_controls, ARRAY_SIZE(vout2_mix_controls)),
260	SND_SOC_DAPM_MIXER("VOUT3 Mixer", SND_SOC_NOPM, 0, 0,
261		vout3_mix_controls, ARRAY_SIZE(vout3_mix_controls)),
262	SND_SOC_DAPM_MIXER("VOUT4 Mixer", SND_SOC_NOPM, 0, 0,
263		vout4_mix_controls, ARRAY_SIZE(vout4_mix_controls)),
264
265	SND_SOC_DAPM_OUTPUT("VOUT1"),
266	SND_SOC_DAPM_OUTPUT("VOUT2"),
267	SND_SOC_DAPM_OUTPUT("VOUT3"),
268	SND_SOC_DAPM_OUTPUT("VOUT4")
269};
270
271static const struct snd_soc_dapm_route wm8770_intercon[] = {
272	{ "Capture Mux", "AIN1", "AIN1" },
273	{ "Capture Mux", "AIN2", "AIN2" },
274	{ "Capture Mux", "AIN3", "AIN3" },
275	{ "Capture Mux", "AIN4", "AIN4" },
276	{ "Capture Mux", "AIN5", "AIN5" },
277	{ "Capture Mux", "AIN6", "AIN6" },
278	{ "Capture Mux", "AIN7", "AIN7" },
279	{ "Capture Mux", "AIN8", "AIN8" },
280
281	{ "ADC", NULL, "Capture Mux" },
282
283	{ "VOUT1 Mixer", NULL, "VOUT12 Supply" },
284	{ "VOUT1 Mixer", "DAC1 Switch", "DAC1" },
285	{ "VOUT1 Mixer", "AUX1 Switch", "AUX1" },
286	{ "VOUT1 Mixer", "Bypass Switch", "Capture Mux" },
287
288	{ "VOUT2 Mixer", NULL, "VOUT12 Supply" },
289	{ "VOUT2 Mixer", "DAC2 Switch", "DAC2" },
290	{ "VOUT2 Mixer", "AUX2 Switch", "AUX2" },
291	{ "VOUT2 Mixer", "Bypass Switch", "Capture Mux" },
292
293	{ "VOUT3 Mixer", NULL, "VOUT34 Supply" },
294	{ "VOUT3 Mixer", "DAC3 Switch", "DAC3" },
295	{ "VOUT3 Mixer", "AUX3 Switch", "AUX3" },
296	{ "VOUT3 Mixer", "Bypass Switch", "Capture Mux" },
297
298	{ "VOUT4 Mixer", NULL, "VOUT34 Supply" },
299	{ "VOUT4 Mixer", "DAC4 Switch", "DAC4" },
300	{ "VOUT4 Mixer", "Bypass Switch", "Capture Mux" },
301
302	{ "VOUT1", NULL, "VOUT1 Mixer" },
303	{ "VOUT2", NULL, "VOUT2 Mixer" },
304	{ "VOUT3", NULL, "VOUT3 Mixer" },
305	{ "VOUT4", NULL, "VOUT4 Mixer" }
306};
307
308static int vout12supply_event(struct snd_soc_dapm_widget *w,
309	struct snd_kcontrol *kcontrol, int event)
310{
311	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 
 
312
313	switch (event) {
314	case SND_SOC_DAPM_PRE_PMU:
315		snd_soc_component_update_bits(component, WM8770_OUTMUX1, 0x180, 0);
316		break;
317	case SND_SOC_DAPM_POST_PMD:
318		snd_soc_component_update_bits(component, WM8770_OUTMUX1, 0x180, 0x180);
319		break;
320	}
321
322	return 0;
323}
324
325static int vout34supply_event(struct snd_soc_dapm_widget *w,
326	struct snd_kcontrol *kcontrol, int event)
327{
328	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 
 
329
330	switch (event) {
331	case SND_SOC_DAPM_PRE_PMU:
332		snd_soc_component_update_bits(component, WM8770_OUTMUX2, 0x180, 0);
333		break;
334	case SND_SOC_DAPM_POST_PMD:
335		snd_soc_component_update_bits(component, WM8770_OUTMUX2, 0x180, 0x180);
336		break;
337	}
338
339	return 0;
340}
341
342static int wm8770_reset(struct snd_soc_component *component)
343{
344	return snd_soc_component_write(component, WM8770_RESET, 0);
345}
346
347static int wm8770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
348{
349	struct snd_soc_component *component;
350	int iface, master;
351
352	component = dai->component;
353
354	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
355	case SND_SOC_DAIFMT_CBM_CFM:
356		master = 0x100;
357		break;
358	case SND_SOC_DAIFMT_CBS_CFS:
359		master = 0;
360		break;
361	default:
362		return -EINVAL;
363	}
364
365	iface = 0;
366	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
367	case SND_SOC_DAIFMT_I2S:
368		iface |= 0x2;
369		break;
370	case SND_SOC_DAIFMT_RIGHT_J:
371		break;
372	case SND_SOC_DAIFMT_LEFT_J:
373		iface |= 0x1;
374		break;
375	default:
376		return -EINVAL;
377	}
378
379	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
380	case SND_SOC_DAIFMT_NB_NF:
381		break;
382	case SND_SOC_DAIFMT_IB_IF:
383		iface |= 0xc;
384		break;
385	case SND_SOC_DAIFMT_IB_NF:
386		iface |= 0x8;
387		break;
388	case SND_SOC_DAIFMT_NB_IF:
389		iface |= 0x4;
390		break;
391	default:
392		return -EINVAL;
393	}
394
395	snd_soc_component_update_bits(component, WM8770_IFACECTRL, 0xf, iface);
396	snd_soc_component_update_bits(component, WM8770_MSTRCTRL, 0x100, master);
397
398	return 0;
399}
400
401static const int mclk_ratios[] = {
402	128,
403	192,
404	256,
405	384,
406	512,
407	768
408};
409
410static int wm8770_hw_params(struct snd_pcm_substream *substream,
411			    struct snd_pcm_hw_params *params,
412			    struct snd_soc_dai *dai)
413{
414	struct snd_soc_component *component;
415	struct wm8770_priv *wm8770;
416	int i;
417	int iface;
418	int shift;
419	int ratio;
420
421	component = dai->component;
422	wm8770 = snd_soc_component_get_drvdata(component);
423
424	iface = 0;
425	switch (params_width(params)) {
426	case 16:
427		break;
428	case 20:
429		iface |= 0x10;
430		break;
431	case 24:
432		iface |= 0x20;
433		break;
434	case 32:
435		iface |= 0x30;
436		break;
437	}
438
439	switch (substream->stream) {
440	case SNDRV_PCM_STREAM_PLAYBACK:
441		i = 0;
442		shift = 4;
443		break;
444	case SNDRV_PCM_STREAM_CAPTURE:
445		i = 2;
446		shift = 0;
447		break;
448	default:
449		return -EINVAL;
450	}
451
452	/* Only need to set MCLK/LRCLK ratio if we're master */
453	if (snd_soc_component_read32(component, WM8770_MSTRCTRL) & 0x100) {
454		for (; i < ARRAY_SIZE(mclk_ratios); ++i) {
455			ratio = wm8770->sysclk / params_rate(params);
456			if (ratio == mclk_ratios[i])
457				break;
458		}
459
460		if (i == ARRAY_SIZE(mclk_ratios)) {
461			dev_err(component->dev,
462				"Unable to configure MCLK ratio %d/%d\n",
463				wm8770->sysclk, params_rate(params));
464			return -EINVAL;
465		}
466
467		dev_dbg(component->dev, "MCLK is %dfs\n", mclk_ratios[i]);
468
469		snd_soc_component_update_bits(component, WM8770_MSTRCTRL, 0x7 << shift,
470				    i << shift);
471	}
472
473	snd_soc_component_update_bits(component, WM8770_IFACECTRL, 0x30, iface);
474
475	return 0;
476}
477
478static int wm8770_mute(struct snd_soc_dai *dai, int mute)
479{
480	struct snd_soc_component *component;
481
482	component = dai->component;
483	return snd_soc_component_update_bits(component, WM8770_DACMUTE, 0x10,
484				   !!mute << 4);
485}
486
487static int wm8770_set_sysclk(struct snd_soc_dai *dai,
488			     int clk_id, unsigned int freq, int dir)
489{
490	struct snd_soc_component *component;
491	struct wm8770_priv *wm8770;
492
493	component = dai->component;
494	wm8770 = snd_soc_component_get_drvdata(component);
495	wm8770->sysclk = freq;
496	return 0;
497}
498
499static int wm8770_set_bias_level(struct snd_soc_component *component,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500				 enum snd_soc_bias_level level)
501{
502	int ret;
503	struct wm8770_priv *wm8770;
504
505	wm8770 = snd_soc_component_get_drvdata(component);
506
507	switch (level) {
508	case SND_SOC_BIAS_ON:
509		break;
510	case SND_SOC_BIAS_PREPARE:
511		break;
512	case SND_SOC_BIAS_STANDBY:
513		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
514			ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
515						    wm8770->supplies);
516			if (ret) {
517				dev_err(component->dev,
518					"Failed to enable supplies: %d\n",
519					ret);
520				return ret;
521			}
522
523			regcache_sync(wm8770->regmap);
524
525			/* global powerup */
526			snd_soc_component_write(component, WM8770_PWDNCTRL, 0);
527		}
528		break;
529	case SND_SOC_BIAS_OFF:
530		/* global powerdown */
531		snd_soc_component_write(component, WM8770_PWDNCTRL, 1);
532		regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies),
533				       wm8770->supplies);
534		break;
535	}
536
 
537	return 0;
538}
539
540#define WM8770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
541			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
542
543static const struct snd_soc_dai_ops wm8770_dai_ops = {
544	.digital_mute = wm8770_mute,
545	.hw_params = wm8770_hw_params,
546	.set_fmt = wm8770_set_fmt,
547	.set_sysclk = wm8770_set_sysclk,
548};
549
550static struct snd_soc_dai_driver wm8770_dai = {
551	.name = "wm8770-hifi",
552	.playback = {
553		.stream_name = "Playback",
554		.channels_min = 2,
555		.channels_max = 2,
556		.rates = SNDRV_PCM_RATE_8000_192000,
557		.formats = WM8770_FORMATS
558	},
559	.capture = {
560		.stream_name = "Capture",
561		.channels_min = 2,
562		.channels_max = 2,
563		.rates = SNDRV_PCM_RATE_8000_96000,
564		.formats = WM8770_FORMATS
565	},
566	.ops = &wm8770_dai_ops,
567	.symmetric_rates = 1
568};
569
570static int wm8770_probe(struct snd_soc_component *component)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571{
572	struct wm8770_priv *wm8770;
573	int ret;
 
574
575	wm8770 = snd_soc_component_get_drvdata(component);
576	wm8770->component = component;
577
578	ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
579				    wm8770->supplies);
580	if (ret) {
581		dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
582		return ret;
583	}
584
585	ret = wm8770_reset(component);
586	if (ret < 0) {
587		dev_err(component->dev, "Failed to issue reset: %d\n", ret);
588		goto err_reg_enable;
589	}
590
591	/* latch the volume update bits */
592	snd_soc_component_update_bits(component, WM8770_MSDIGVOL, 0x100, 0x100);
593	snd_soc_component_update_bits(component, WM8770_MSALGVOL, 0x100, 0x100);
594	snd_soc_component_update_bits(component, WM8770_VOUT1RVOL, 0x100, 0x100);
595	snd_soc_component_update_bits(component, WM8770_VOUT2RVOL, 0x100, 0x100);
596	snd_soc_component_update_bits(component, WM8770_VOUT3RVOL, 0x100, 0x100);
597	snd_soc_component_update_bits(component, WM8770_VOUT4RVOL, 0x100, 0x100);
598	snd_soc_component_update_bits(component, WM8770_DAC1RVOL, 0x100, 0x100);
599	snd_soc_component_update_bits(component, WM8770_DAC2RVOL, 0x100, 0x100);
600	snd_soc_component_update_bits(component, WM8770_DAC3RVOL, 0x100, 0x100);
601	snd_soc_component_update_bits(component, WM8770_DAC4RVOL, 0x100, 0x100);
602
603	/* mute all DACs */
604	snd_soc_component_update_bits(component, WM8770_DACMUTE, 0x10, 0x10);
605
606err_reg_enable:
607	regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
608	return ret;
609}
610
611static const struct snd_soc_component_driver soc_component_dev_wm8770 = {
612	.probe			= wm8770_probe,
613	.set_bias_level		= wm8770_set_bias_level,
614	.controls		= wm8770_snd_controls,
615	.num_controls		= ARRAY_SIZE(wm8770_snd_controls),
616	.dapm_widgets		= wm8770_dapm_widgets,
617	.num_dapm_widgets	= ARRAY_SIZE(wm8770_dapm_widgets),
618	.dapm_routes		= wm8770_intercon,
619	.num_dapm_routes	= ARRAY_SIZE(wm8770_intercon),
620	.use_pmdown_time	= 1,
621	.endianness		= 1,
622	.non_legacy_dai_naming	= 1,
623};
624
625static const struct of_device_id wm8770_of_match[] = {
626	{ .compatible = "wlf,wm8770", },
627	{ }
628};
629MODULE_DEVICE_TABLE(of, wm8770_of_match);
630
631static const struct regmap_config wm8770_regmap = {
632	.reg_bits = 7,
633	.val_bits = 9,
634	.max_register = WM8770_RESET,
635
636	.reg_defaults = wm8770_reg_defaults,
637	.num_reg_defaults = ARRAY_SIZE(wm8770_reg_defaults),
638	.cache_type = REGCACHE_RBTREE,
639
640	.volatile_reg = wm8770_volatile_reg,
641};
642
643static int wm8770_spi_probe(struct spi_device *spi)
644{
645	struct wm8770_priv *wm8770;
646	int ret, i;
647
648	wm8770 = devm_kzalloc(&spi->dev, sizeof(struct wm8770_priv),
649			      GFP_KERNEL);
650	if (!wm8770)
651		return -ENOMEM;
652
653	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++)
654		wm8770->supplies[i].supply = wm8770_supply_names[i];
655
656	ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8770->supplies),
657				      wm8770->supplies);
658	if (ret) {
659		dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
660		return ret;
661	}
662
663	wm8770->disable_nb[0].notifier_call = wm8770_regulator_event_0;
664	wm8770->disable_nb[1].notifier_call = wm8770_regulator_event_1;
665	wm8770->disable_nb[2].notifier_call = wm8770_regulator_event_2;
666
667	/* This should really be moved into the regulator core */
668	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++) {
669		ret = regulator_register_notifier(wm8770->supplies[i].consumer,
670						  &wm8770->disable_nb[i]);
671		if (ret) {
672			dev_err(&spi->dev,
673				"Failed to register regulator notifier: %d\n",
674				ret);
675		}
676	}
677
678	wm8770->regmap = devm_regmap_init_spi(spi, &wm8770_regmap);
679	if (IS_ERR(wm8770->regmap))
680		return PTR_ERR(wm8770->regmap);
 
 
 
 
 
 
 
 
 
681
682	spi_set_drvdata(spi, wm8770);
683
684	ret = devm_snd_soc_register_component(&spi->dev,
685				     &soc_component_dev_wm8770, &wm8770_dai, 1);
 
 
 
 
 
 
 
 
 
686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687	return ret;
688}
689
690static int wm8770_spi_remove(struct spi_device *spi)
691{
692	struct wm8770_priv *wm8770 = spi_get_drvdata(spi);
693	int i;
694
 
 
 
695	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); ++i)
696		regulator_unregister_notifier(wm8770->supplies[i].consumer,
697					      &wm8770->disable_nb[i]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698
 
 
 
 
 
 
 
 
 
 
 
699	return 0;
700}
701
702static struct spi_driver wm8770_spi_driver = {
703	.driver = {
704		.name = "wm8770",
705		.of_match_table = wm8770_of_match,
706	},
707	.probe = wm8770_spi_probe,
708	.remove = wm8770_spi_remove
709};
 
 
 
 
 
710
711module_spi_driver(wm8770_spi_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712
713MODULE_DESCRIPTION("ASoC WM8770 driver");
714MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
715MODULE_LICENSE("GPL");
  1/*
  2 * wm8770.c  --  WM8770 ALSA SoC Audio driver
  3 *
  4 * Copyright 2010 Wolfson Microelectronics plc
  5 *
  6 * Author: Dimitris Papastamos <dp@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/platform_device.h>
 19#include <linux/spi/spi.h>
 
 20#include <linux/regulator/consumer.h>
 21#include <linux/slab.h>
 22#include <sound/core.h>
 23#include <sound/pcm.h>
 24#include <sound/pcm_params.h>
 25#include <sound/soc.h>
 26#include <sound/initval.h>
 27#include <sound/tlv.h>
 28
 29#include "wm8770.h"
 30
 31#define WM8770_NUM_SUPPLIES 3
 32static const char *wm8770_supply_names[WM8770_NUM_SUPPLIES] = {
 33	"AVDD1",
 34	"AVDD2",
 35	"DVDD"
 36};
 37
 38static const u16 wm8770_reg_defs[WM8770_CACHEREGNUM] = {
 39	0x7f, 0x7f, 0x7f, 0x7f,
 40	0x7f, 0x7f, 0x7f, 0x7f,
 41	0x7f, 0xff, 0xff, 0xff,
 42	0xff, 0xff, 0xff, 0xff,
 43	0xff, 0xff, 0, 0x90, 0,
 44	0, 0x22, 0x22, 0x3e,
 45	0xc, 0xc, 0x100, 0x189,
 46	0x189, 0x8770
 47};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 48
 49struct wm8770_priv {
 50	enum snd_soc_control_type control_type;
 51	struct regulator_bulk_data supplies[WM8770_NUM_SUPPLIES];
 52	struct notifier_block disable_nb[WM8770_NUM_SUPPLIES];
 53	struct snd_soc_codec *codec;
 54	int sysclk;
 55};
 56
 57static int vout12supply_event(struct snd_soc_dapm_widget *w,
 58	struct snd_kcontrol *kcontrol, int event);
 59static int vout34supply_event(struct snd_soc_dapm_widget *w,
 60	struct snd_kcontrol *kcontrol, int event);
 61
 62/*
 63 * We can't use the same notifier block for more than one supply and
 64 * there's no way I can see to get from a callback to the caller
 65 * except container_of().
 66 */
 67#define WM8770_REGULATOR_EVENT(n) \
 68static int wm8770_regulator_event_##n(struct notifier_block *nb, \
 69				      unsigned long event, void *data)    \
 70{ \
 71	struct wm8770_priv *wm8770 = container_of(nb, struct wm8770_priv, \
 72				     disable_nb[n]); \
 73	if (event & REGULATOR_EVENT_DISABLE) { \
 74		wm8770->codec->cache_sync = 1; \
 75	} \
 76	return 0; \
 77}
 78
 79WM8770_REGULATOR_EVENT(0)
 80WM8770_REGULATOR_EVENT(1)
 81WM8770_REGULATOR_EVENT(2)
 82
 83static const DECLARE_TLV_DB_SCALE(adc_tlv, -1200, 100, 0);
 84static const DECLARE_TLV_DB_SCALE(dac_dig_tlv, -12750, 50, 1);
 85static const DECLARE_TLV_DB_SCALE(dac_alg_tlv, -12700, 100, 1);
 86
 87static const char *dac_phase_text[][2] = {
 88	{ "DAC1 Normal", "DAC1 Inverted" },
 89	{ "DAC2 Normal", "DAC2 Inverted" },
 90	{ "DAC3 Normal", "DAC3 Inverted" },
 91	{ "DAC4 Normal", "DAC4 Inverted" },
 92};
 93
 94static const struct soc_enum dac_phase[] = {
 95	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 0, 1, 2, dac_phase_text[0]),
 96	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 2, 3, 2, dac_phase_text[1]),
 97	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 4, 5, 2, dac_phase_text[2]),
 98	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 6, 7, 2, dac_phase_text[3]),
 99};
100
101static const struct snd_kcontrol_new wm8770_snd_controls[] = {
102	/* global DAC playback controls */
103	SOC_SINGLE_TLV("DAC Playback Volume", WM8770_MSDIGVOL, 0, 255, 0,
104		dac_dig_tlv),
105	SOC_SINGLE("DAC Playback Switch", WM8770_DACMUTE, 4, 1, 1),
106	SOC_SINGLE("DAC Playback ZC Switch", WM8770_DACCTRL1, 0, 1, 0),
107
108	/* global VOUT playback controls */
109	SOC_SINGLE_TLV("VOUT Playback Volume", WM8770_MSALGVOL, 0, 127, 0,
110		dac_alg_tlv),
111	SOC_SINGLE("VOUT Playback ZC Switch", WM8770_MSALGVOL, 7, 1, 0),
112
113	/* VOUT1/2/3/4 specific controls */
114	SOC_DOUBLE_R_TLV("VOUT1 Playback Volume", WM8770_VOUT1LVOL,
115		WM8770_VOUT1RVOL, 0, 127, 0, dac_alg_tlv),
116	SOC_DOUBLE_R("VOUT1 Playback ZC Switch", WM8770_VOUT1LVOL,
117		WM8770_VOUT1RVOL, 7, 1, 0),
118	SOC_DOUBLE_R_TLV("VOUT2 Playback Volume", WM8770_VOUT2LVOL,
119		WM8770_VOUT2RVOL, 0, 127, 0, dac_alg_tlv),
120	SOC_DOUBLE_R("VOUT2 Playback ZC Switch", WM8770_VOUT2LVOL,
121		WM8770_VOUT2RVOL, 7, 1, 0),
122	SOC_DOUBLE_R_TLV("VOUT3 Playback Volume", WM8770_VOUT3LVOL,
123		WM8770_VOUT3RVOL, 0, 127, 0, dac_alg_tlv),
124	SOC_DOUBLE_R("VOUT3 Playback ZC Switch", WM8770_VOUT3LVOL,
125		WM8770_VOUT3RVOL, 7, 1, 0),
126	SOC_DOUBLE_R_TLV("VOUT4 Playback Volume", WM8770_VOUT4LVOL,
127		WM8770_VOUT4RVOL, 0, 127, 0, dac_alg_tlv),
128	SOC_DOUBLE_R("VOUT4 Playback ZC Switch", WM8770_VOUT4LVOL,
129		WM8770_VOUT4RVOL, 7, 1, 0),
130
131	/* DAC1/2/3/4 specific controls */
132	SOC_DOUBLE_R_TLV("DAC1 Playback Volume", WM8770_DAC1LVOL,
133		WM8770_DAC1RVOL, 0, 255, 0, dac_dig_tlv),
134	SOC_SINGLE("DAC1 Deemphasis Switch", WM8770_DACCTRL2, 0, 1, 0),
135	SOC_ENUM("DAC1 Phase", dac_phase[0]),
136	SOC_DOUBLE_R_TLV("DAC2 Playback Volume", WM8770_DAC2LVOL,
137		WM8770_DAC2RVOL, 0, 255, 0, dac_dig_tlv),
138	SOC_SINGLE("DAC2 Deemphasis Switch", WM8770_DACCTRL2, 1, 1, 0),
139	SOC_ENUM("DAC2 Phase", dac_phase[1]),
140	SOC_DOUBLE_R_TLV("DAC3 Playback Volume", WM8770_DAC3LVOL,
141		WM8770_DAC3RVOL, 0, 255, 0, dac_dig_tlv),
142	SOC_SINGLE("DAC3 Deemphasis Switch", WM8770_DACCTRL2, 2, 1, 0),
143	SOC_ENUM("DAC3 Phase", dac_phase[2]),
144	SOC_DOUBLE_R_TLV("DAC4 Playback Volume", WM8770_DAC4LVOL,
145		WM8770_DAC4RVOL, 0, 255, 0, dac_dig_tlv),
146	SOC_SINGLE("DAC4 Deemphasis Switch", WM8770_DACCTRL2, 3, 1, 0),
147	SOC_ENUM("DAC4 Phase", dac_phase[3]),
148
149	/* ADC specific controls */
150	SOC_DOUBLE_R_TLV("Capture Volume", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
151		0, 31, 0, adc_tlv),
152	SOC_DOUBLE_R("Capture Switch", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
153		5, 1, 1),
154
155	/* other controls */
156	SOC_SINGLE("ADC 128x Oversampling Switch", WM8770_MSTRCTRL, 3, 1, 0),
157	SOC_SINGLE("ADC Highpass Filter Switch", WM8770_IFACECTRL, 8, 1, 1)
158};
159
160static const char *ain_text[] = {
161	"AIN1", "AIN2", "AIN3", "AIN4",
162	"AIN5", "AIN6", "AIN7", "AIN8"
163};
164
165static const struct soc_enum ain_enum =
166	SOC_ENUM_DOUBLE(WM8770_ADCMUX, 0, 4, 8, ain_text);
167
168static const struct snd_kcontrol_new ain_mux =
169	SOC_DAPM_ENUM("Capture Mux", ain_enum);
170
171static const struct snd_kcontrol_new vout1_mix_controls[] = {
172	SOC_DAPM_SINGLE("DAC1 Switch", WM8770_OUTMUX1, 0, 1, 0),
173	SOC_DAPM_SINGLE("AUX1 Switch", WM8770_OUTMUX1, 1, 1, 0),
174	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 2, 1, 0)
175};
176
177static const struct snd_kcontrol_new vout2_mix_controls[] = {
178	SOC_DAPM_SINGLE("DAC2 Switch", WM8770_OUTMUX1, 3, 1, 0),
179	SOC_DAPM_SINGLE("AUX2 Switch", WM8770_OUTMUX1, 4, 1, 0),
180	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 5, 1, 0)
181};
182
183static const struct snd_kcontrol_new vout3_mix_controls[] = {
184	SOC_DAPM_SINGLE("DAC3 Switch", WM8770_OUTMUX2, 0, 1, 0),
185	SOC_DAPM_SINGLE("AUX3 Switch", WM8770_OUTMUX2, 1, 1, 0),
186	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 2, 1, 0)
187};
188
189static const struct snd_kcontrol_new vout4_mix_controls[] = {
190	SOC_DAPM_SINGLE("DAC4 Switch", WM8770_OUTMUX2, 3, 1, 0),
191	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 4, 1, 0)
192};
193
194static const struct snd_soc_dapm_widget wm8770_dapm_widgets[] = {
195	SND_SOC_DAPM_INPUT("AUX1"),
196	SND_SOC_DAPM_INPUT("AUX2"),
197	SND_SOC_DAPM_INPUT("AUX3"),
198
199	SND_SOC_DAPM_INPUT("AIN1"),
200	SND_SOC_DAPM_INPUT("AIN2"),
201	SND_SOC_DAPM_INPUT("AIN3"),
202	SND_SOC_DAPM_INPUT("AIN4"),
203	SND_SOC_DAPM_INPUT("AIN5"),
204	SND_SOC_DAPM_INPUT("AIN6"),
205	SND_SOC_DAPM_INPUT("AIN7"),
206	SND_SOC_DAPM_INPUT("AIN8"),
207
208	SND_SOC_DAPM_MUX("Capture Mux", WM8770_ADCMUX, 8, 1, &ain_mux),
209
210	SND_SOC_DAPM_ADC("ADC", "Capture", WM8770_PWDNCTRL, 1, 1),
211
212	SND_SOC_DAPM_DAC("DAC1", "Playback", WM8770_PWDNCTRL, 2, 1),
213	SND_SOC_DAPM_DAC("DAC2", "Playback", WM8770_PWDNCTRL, 3, 1),
214	SND_SOC_DAPM_DAC("DAC3", "Playback", WM8770_PWDNCTRL, 4, 1),
215	SND_SOC_DAPM_DAC("DAC4", "Playback", WM8770_PWDNCTRL, 5, 1),
216
217	SND_SOC_DAPM_SUPPLY("VOUT12 Supply", SND_SOC_NOPM, 0, 0,
218		vout12supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
219	SND_SOC_DAPM_SUPPLY("VOUT34 Supply", SND_SOC_NOPM, 0, 0,
220		vout34supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
221
222	SND_SOC_DAPM_MIXER("VOUT1 Mixer", SND_SOC_NOPM, 0, 0,
223		vout1_mix_controls, ARRAY_SIZE(vout1_mix_controls)),
224	SND_SOC_DAPM_MIXER("VOUT2 Mixer", SND_SOC_NOPM, 0, 0,
225		vout2_mix_controls, ARRAY_SIZE(vout2_mix_controls)),
226	SND_SOC_DAPM_MIXER("VOUT3 Mixer", SND_SOC_NOPM, 0, 0,
227		vout3_mix_controls, ARRAY_SIZE(vout3_mix_controls)),
228	SND_SOC_DAPM_MIXER("VOUT4 Mixer", SND_SOC_NOPM, 0, 0,
229		vout4_mix_controls, ARRAY_SIZE(vout4_mix_controls)),
230
231	SND_SOC_DAPM_OUTPUT("VOUT1"),
232	SND_SOC_DAPM_OUTPUT("VOUT2"),
233	SND_SOC_DAPM_OUTPUT("VOUT3"),
234	SND_SOC_DAPM_OUTPUT("VOUT4")
235};
236
237static const struct snd_soc_dapm_route wm8770_intercon[] = {
238	{ "Capture Mux", "AIN1", "AIN1" },
239	{ "Capture Mux", "AIN2", "AIN2" },
240	{ "Capture Mux", "AIN3", "AIN3" },
241	{ "Capture Mux", "AIN4", "AIN4" },
242	{ "Capture Mux", "AIN5", "AIN5" },
243	{ "Capture Mux", "AIN6", "AIN6" },
244	{ "Capture Mux", "AIN7", "AIN7" },
245	{ "Capture Mux", "AIN8", "AIN8" },
246
247	{ "ADC", NULL, "Capture Mux" },
248
249	{ "VOUT1 Mixer", NULL, "VOUT12 Supply" },
250	{ "VOUT1 Mixer", "DAC1 Switch", "DAC1" },
251	{ "VOUT1 Mixer", "AUX1 Switch", "AUX1" },
252	{ "VOUT1 Mixer", "Bypass Switch", "Capture Mux" },
253
254	{ "VOUT2 Mixer", NULL, "VOUT12 Supply" },
255	{ "VOUT2 Mixer", "DAC2 Switch", "DAC2" },
256	{ "VOUT2 Mixer", "AUX2 Switch", "AUX2" },
257	{ "VOUT2 Mixer", "Bypass Switch", "Capture Mux" },
258
259	{ "VOUT3 Mixer", NULL, "VOUT34 Supply" },
260	{ "VOUT3 Mixer", "DAC3 Switch", "DAC3" },
261	{ "VOUT3 Mixer", "AUX3 Switch", "AUX3" },
262	{ "VOUT3 Mixer", "Bypass Switch", "Capture Mux" },
263
264	{ "VOUT4 Mixer", NULL, "VOUT34 Supply" },
265	{ "VOUT4 Mixer", "DAC4 Switch", "DAC4" },
266	{ "VOUT4 Mixer", "Bypass Switch", "Capture Mux" },
267
268	{ "VOUT1", NULL, "VOUT1 Mixer" },
269	{ "VOUT2", NULL, "VOUT2 Mixer" },
270	{ "VOUT3", NULL, "VOUT3 Mixer" },
271	{ "VOUT4", NULL, "VOUT4 Mixer" }
272};
273
274static int vout12supply_event(struct snd_soc_dapm_widget *w,
275	struct snd_kcontrol *kcontrol, int event)
276{
277	struct snd_soc_codec *codec;
278
279	codec = w->codec;
280
281	switch (event) {
282	case SND_SOC_DAPM_PRE_PMU:
283		snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0);
284		break;
285	case SND_SOC_DAPM_POST_PMD:
286		snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0x180);
287		break;
288	}
289
290	return 0;
291}
292
293static int vout34supply_event(struct snd_soc_dapm_widget *w,
294	struct snd_kcontrol *kcontrol, int event)
295{
296	struct snd_soc_codec *codec;
297
298	codec = w->codec;
299
300	switch (event) {
301	case SND_SOC_DAPM_PRE_PMU:
302		snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0);
303		break;
304	case SND_SOC_DAPM_POST_PMD:
305		snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0x180);
306		break;
307	}
308
309	return 0;
310}
311
312static int wm8770_reset(struct snd_soc_codec *codec)
313{
314	return snd_soc_write(codec, WM8770_RESET, 0);
315}
316
317static int wm8770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
318{
319	struct snd_soc_codec *codec;
320	int iface, master;
321
322	codec = dai->codec;
323
324	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
325	case SND_SOC_DAIFMT_CBM_CFM:
326		master = 0x100;
327		break;
328	case SND_SOC_DAIFMT_CBS_CFS:
329		master = 0;
330		break;
331	default:
332		return -EINVAL;
333	}
334
335	iface = 0;
336	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
337	case SND_SOC_DAIFMT_I2S:
338		iface |= 0x2;
339		break;
340	case SND_SOC_DAIFMT_RIGHT_J:
341		break;
342	case SND_SOC_DAIFMT_LEFT_J:
343		iface |= 0x1;
344		break;
345	default:
346		return -EINVAL;
347	}
348
349	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
350	case SND_SOC_DAIFMT_NB_NF:
351		break;
352	case SND_SOC_DAIFMT_IB_IF:
353		iface |= 0xc;
354		break;
355	case SND_SOC_DAIFMT_IB_NF:
356		iface |= 0x8;
357		break;
358	case SND_SOC_DAIFMT_NB_IF:
359		iface |= 0x4;
360		break;
361	default:
362		return -EINVAL;
363	}
364
365	snd_soc_update_bits(codec, WM8770_IFACECTRL, 0xf, iface);
366	snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x100, master);
367
368	return 0;
369}
370
371static const int mclk_ratios[] = {
372	128,
373	192,
374	256,
375	384,
376	512,
377	768
378};
379
380static int wm8770_hw_params(struct snd_pcm_substream *substream,
381			    struct snd_pcm_hw_params *params,
382			    struct snd_soc_dai *dai)
383{
384	struct snd_soc_codec *codec;
385	struct wm8770_priv *wm8770;
386	int i;
387	int iface;
388	int shift;
389	int ratio;
390
391	codec = dai->codec;
392	wm8770 = snd_soc_codec_get_drvdata(codec);
393
394	iface = 0;
395	switch (params_format(params)) {
396	case SNDRV_PCM_FORMAT_S16_LE:
397		break;
398	case SNDRV_PCM_FORMAT_S20_3LE:
399		iface |= 0x10;
400		break;
401	case SNDRV_PCM_FORMAT_S24_LE:
402		iface |= 0x20;
403		break;
404	case SNDRV_PCM_FORMAT_S32_LE:
405		iface |= 0x30;
406		break;
407	}
408
409	switch (substream->stream) {
410	case SNDRV_PCM_STREAM_PLAYBACK:
411		i = 0;
412		shift = 4;
413		break;
414	case SNDRV_PCM_STREAM_CAPTURE:
415		i = 2;
416		shift = 0;
417		break;
418	default:
419		return -EINVAL;
420	}
421
422	/* Only need to set MCLK/LRCLK ratio if we're master */
423	if (snd_soc_read(codec, WM8770_MSTRCTRL) & 0x100) {
424		for (; i < ARRAY_SIZE(mclk_ratios); ++i) {
425			ratio = wm8770->sysclk / params_rate(params);
426			if (ratio == mclk_ratios[i])
427				break;
428		}
429
430		if (i == ARRAY_SIZE(mclk_ratios)) {
431			dev_err(codec->dev,
432				"Unable to configure MCLK ratio %d/%d\n",
433				wm8770->sysclk, params_rate(params));
434			return -EINVAL;
435		}
436
437		dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
438
439		snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x7 << shift,
440				    i << shift);
441	}
442
443	snd_soc_update_bits(codec, WM8770_IFACECTRL, 0x30, iface);
444
445	return 0;
446}
447
448static int wm8770_mute(struct snd_soc_dai *dai, int mute)
449{
450	struct snd_soc_codec *codec;
451
452	codec = dai->codec;
453	return snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10,
454				   !!mute << 4);
455}
456
457static int wm8770_set_sysclk(struct snd_soc_dai *dai,
458			     int clk_id, unsigned int freq, int dir)
459{
460	struct snd_soc_codec *codec;
461	struct wm8770_priv *wm8770;
462
463	codec = dai->codec;
464	wm8770 = snd_soc_codec_get_drvdata(codec);
465	wm8770->sysclk = freq;
466	return 0;
467}
468
469static void wm8770_sync_cache(struct snd_soc_codec *codec)
470{
471	int i;
472	u16 *cache;
473
474	if (!codec->cache_sync)
475		return;
476
477	codec->cache_only = 0;
478	cache = codec->reg_cache;
479	for (i = 0; i < codec->driver->reg_cache_size; i++) {
480		if (i == WM8770_RESET || cache[i] == wm8770_reg_defs[i])
481			continue;
482		snd_soc_write(codec, i, cache[i]);
483	}
484	codec->cache_sync = 0;
485}
486
487static int wm8770_set_bias_level(struct snd_soc_codec *codec,
488				 enum snd_soc_bias_level level)
489{
490	int ret;
491	struct wm8770_priv *wm8770;
492
493	wm8770 = snd_soc_codec_get_drvdata(codec);
494
495	switch (level) {
496	case SND_SOC_BIAS_ON:
497		break;
498	case SND_SOC_BIAS_PREPARE:
499		break;
500	case SND_SOC_BIAS_STANDBY:
501		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
502			ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
503						    wm8770->supplies);
504			if (ret) {
505				dev_err(codec->dev,
506					"Failed to enable supplies: %d\n",
507					ret);
508				return ret;
509			}
510			wm8770_sync_cache(codec);
 
 
511			/* global powerup */
512			snd_soc_write(codec, WM8770_PWDNCTRL, 0);
513		}
514		break;
515	case SND_SOC_BIAS_OFF:
516		/* global powerdown */
517		snd_soc_write(codec, WM8770_PWDNCTRL, 1);
518		regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies),
519				       wm8770->supplies);
520		break;
521	}
522
523	codec->dapm.bias_level = level;
524	return 0;
525}
526
527#define WM8770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
528			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
529
530static struct snd_soc_dai_ops wm8770_dai_ops = {
531	.digital_mute = wm8770_mute,
532	.hw_params = wm8770_hw_params,
533	.set_fmt = wm8770_set_fmt,
534	.set_sysclk = wm8770_set_sysclk,
535};
536
537static struct snd_soc_dai_driver wm8770_dai = {
538	.name = "wm8770-hifi",
539	.playback = {
540		.stream_name = "Playback",
541		.channels_min = 2,
542		.channels_max = 2,
543		.rates = SNDRV_PCM_RATE_8000_192000,
544		.formats = WM8770_FORMATS
545	},
546	.capture = {
547		.stream_name = "Capture",
548		.channels_min = 2,
549		.channels_max = 2,
550		.rates = SNDRV_PCM_RATE_8000_96000,
551		.formats = WM8770_FORMATS
552	},
553	.ops = &wm8770_dai_ops,
554	.symmetric_rates = 1
555};
556
557#ifdef CONFIG_PM
558static int wm8770_suspend(struct snd_soc_codec *codec, pm_message_t state)
559{
560	wm8770_set_bias_level(codec, SND_SOC_BIAS_OFF);
561	return 0;
562}
563
564static int wm8770_resume(struct snd_soc_codec *codec)
565{
566	wm8770_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
567	return 0;
568}
569#else
570#define wm8770_suspend NULL
571#define wm8770_resume NULL
572#endif
573
574static int wm8770_probe(struct snd_soc_codec *codec)
575{
576	struct wm8770_priv *wm8770;
577	int ret;
578	int i;
579
580	wm8770 = snd_soc_codec_get_drvdata(codec);
581	wm8770->codec = codec;
582
583	codec->dapm.idle_bias_off = 1;
 
 
 
 
 
584
585	ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8770->control_type);
586	if (ret < 0) {
587		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
588		return ret;
589	}
590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++)
592		wm8770->supplies[i].supply = wm8770_supply_names[i];
593
594	ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8770->supplies),
595				 wm8770->supplies);
596	if (ret) {
597		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
598		return ret;
599	}
600
601	wm8770->disable_nb[0].notifier_call = wm8770_regulator_event_0;
602	wm8770->disable_nb[1].notifier_call = wm8770_regulator_event_1;
603	wm8770->disable_nb[2].notifier_call = wm8770_regulator_event_2;
604
605	/* This should really be moved into the regulator core */
606	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++) {
607		ret = regulator_register_notifier(wm8770->supplies[i].consumer,
608						  &wm8770->disable_nb[i]);
609		if (ret) {
610			dev_err(codec->dev,
611				"Failed to register regulator notifier: %d\n",
612				ret);
613		}
614	}
615
616	ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
617				    wm8770->supplies);
618	if (ret) {
619		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
620		goto err_reg_get;
621	}
622
623	ret = wm8770_reset(codec);
624	if (ret < 0) {
625		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
626		goto err_reg_enable;
627	}
628
629	wm8770_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
630
631	/* latch the volume update bits */
632	snd_soc_update_bits(codec, WM8770_MSDIGVOL, 0x100, 0x100);
633	snd_soc_update_bits(codec, WM8770_MSALGVOL, 0x100, 0x100);
634	snd_soc_update_bits(codec, WM8770_VOUT1RVOL, 0x100, 0x100);
635	snd_soc_update_bits(codec, WM8770_VOUT2RVOL, 0x100, 0x100);
636	snd_soc_update_bits(codec, WM8770_VOUT3RVOL, 0x100, 0x100);
637	snd_soc_update_bits(codec, WM8770_VOUT4RVOL, 0x100, 0x100);
638	snd_soc_update_bits(codec, WM8770_DAC1RVOL, 0x100, 0x100);
639	snd_soc_update_bits(codec, WM8770_DAC2RVOL, 0x100, 0x100);
640	snd_soc_update_bits(codec, WM8770_DAC3RVOL, 0x100, 0x100);
641	snd_soc_update_bits(codec, WM8770_DAC4RVOL, 0x100, 0x100);
642
643	/* mute all DACs */
644	snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10, 0x10);
645
646	snd_soc_add_controls(codec, wm8770_snd_controls,
647			     ARRAY_SIZE(wm8770_snd_controls));
648	snd_soc_dapm_new_controls(&codec->dapm, wm8770_dapm_widgets,
649				  ARRAY_SIZE(wm8770_dapm_widgets));
650	snd_soc_dapm_add_routes(&codec->dapm, wm8770_intercon,
651				ARRAY_SIZE(wm8770_intercon));
652	return 0;
653
654err_reg_enable:
655	regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
656err_reg_get:
657	regulator_bulk_free(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
658	return ret;
659}
660
661static int wm8770_remove(struct snd_soc_codec *codec)
662{
663	struct wm8770_priv *wm8770;
664	int i;
665
666	wm8770 = snd_soc_codec_get_drvdata(codec);
667	wm8770_set_bias_level(codec, SND_SOC_BIAS_OFF);
668
669	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); ++i)
670		regulator_unregister_notifier(wm8770->supplies[i].consumer,
671					      &wm8770->disable_nb[i]);
672	regulator_bulk_free(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
673	return 0;
674}
675
676static struct snd_soc_codec_driver soc_codec_dev_wm8770 = {
677	.probe = wm8770_probe,
678	.remove = wm8770_remove,
679	.suspend = wm8770_suspend,
680	.resume = wm8770_resume,
681	.set_bias_level = wm8770_set_bias_level,
682	.reg_cache_size = ARRAY_SIZE(wm8770_reg_defs),
683	.reg_word_size = sizeof (u16),
684	.reg_cache_default = wm8770_reg_defs
685};
686
687#if defined(CONFIG_SPI_MASTER)
688static int __devinit wm8770_spi_probe(struct spi_device *spi)
689{
690	struct wm8770_priv *wm8770;
691	int ret;
692
693	wm8770 = kzalloc(sizeof(struct wm8770_priv), GFP_KERNEL);
694	if (!wm8770)
695		return -ENOMEM;
696
697	wm8770->control_type = SND_SOC_SPI;
698	spi_set_drvdata(spi, wm8770);
699
700	ret = snd_soc_register_codec(&spi->dev,
701				     &soc_codec_dev_wm8770, &wm8770_dai, 1);
702	if (ret < 0)
703		kfree(wm8770);
704	return ret;
705}
706
707static int __devexit wm8770_spi_remove(struct spi_device *spi)
708{
709	snd_soc_unregister_codec(&spi->dev);
710	kfree(spi_get_drvdata(spi));
711	return 0;
712}
713
714static struct spi_driver wm8770_spi_driver = {
715	.driver = {
716		.name = "wm8770",
717		.owner = THIS_MODULE,
718	},
719	.probe = wm8770_spi_probe,
720	.remove = __devexit_p(wm8770_spi_remove)
721};
722#endif
723
724static int __init wm8770_modinit(void)
725{
726	int ret = 0;
727
728#if defined(CONFIG_SPI_MASTER)
729	ret = spi_register_driver(&wm8770_spi_driver);
730	if (ret) {
731		printk(KERN_ERR "Failed to register wm8770 SPI driver: %d\n",
732		       ret);
733	}
734#endif
735	return ret;
736}
737module_init(wm8770_modinit);
738
739static void __exit wm8770_exit(void)
740{
741#if defined(CONFIG_SPI_MASTER)
742	spi_unregister_driver(&wm8770_spi_driver);
743#endif
744}
745module_exit(wm8770_exit);
746
747MODULE_DESCRIPTION("ASoC WM8770 driver");
748MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
749MODULE_LICENSE("GPL");