140e0aa64SRichard Purdie /* 240e0aa64SRichard Purdie * wm8731.c -- WM8731 ALSA SoC Audio driver 340e0aa64SRichard Purdie * 440e0aa64SRichard Purdie * Copyright 2005 Openedhand Ltd. 540e0aa64SRichard Purdie * 640e0aa64SRichard Purdie * Author: Richard Purdie <richard@openedhand.com> 740e0aa64SRichard Purdie * 840e0aa64SRichard Purdie * Based on wm8753.c by Liam Girdwood 940e0aa64SRichard Purdie * 1040e0aa64SRichard Purdie * This program is free software; you can redistribute it and/or modify 1140e0aa64SRichard Purdie * it under the terms of the GNU General Public License version 2 as 1240e0aa64SRichard Purdie * published by the Free Software Foundation. 1340e0aa64SRichard Purdie */ 1440e0aa64SRichard Purdie 1540e0aa64SRichard Purdie #include <linux/module.h> 1640e0aa64SRichard Purdie #include <linux/moduleparam.h> 1740e0aa64SRichard Purdie #include <linux/init.h> 1840e0aa64SRichard Purdie #include <linux/delay.h> 1940e0aa64SRichard Purdie #include <linux/pm.h> 2040e0aa64SRichard Purdie #include <linux/i2c.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 2240e0aa64SRichard Purdie #include <linux/platform_device.h> 237dea7c01SMark Brown #include <linux/regulator/consumer.h> 24d2a40355SCliff Cai #include <linux/spi/spi.h> 2540e0aa64SRichard Purdie #include <sound/core.h> 2640e0aa64SRichard Purdie #include <sound/pcm.h> 2740e0aa64SRichard Purdie #include <sound/pcm_params.h> 2840e0aa64SRichard Purdie #include <sound/soc.h> 2940e0aa64SRichard Purdie #include <sound/initval.h> 30d00efa64SMark Brown #include <sound/tlv.h> 3140e0aa64SRichard Purdie 3240e0aa64SRichard Purdie #include "wm8731.h" 3340e0aa64SRichard Purdie 347dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4 357dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = { 367dea7c01SMark Brown "AVDD", 377dea7c01SMark Brown "HPVDD", 387dea7c01SMark Brown "DCVDD", 397dea7c01SMark Brown "DBVDD", 407dea7c01SMark Brown }; 417dea7c01SMark Brown 42b36d61d4SFrank Mandarino /* codec private data */ 43b36d61d4SFrank Mandarino struct wm8731_priv { 44f0fba2adSLiam Girdwood enum snd_soc_control_type control_type; 457dea7c01SMark Brown struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; 465998102bSMark Brown u16 reg_cache[WM8731_CACHEREGNUM]; 47b36d61d4SFrank Mandarino unsigned int sysclk; 489745e824SMark Brown int sysclk_type; 49b36d61d4SFrank Mandarino }; 50b36d61d4SFrank Mandarino 51a8035c8fSMark Brown 5240e0aa64SRichard Purdie /* 5340e0aa64SRichard Purdie * wm8731 register cache 5440e0aa64SRichard Purdie * We can't read the WM8731 register space when we are 5540e0aa64SRichard Purdie * using 2 wire for device control, so we cache them instead. 5640e0aa64SRichard Purdie * There is no point in caching the reset register 5740e0aa64SRichard Purdie */ 5840e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { 5940e0aa64SRichard Purdie 0x0097, 0x0097, 0x0079, 0x0079, 6040e0aa64SRichard Purdie 0x000a, 0x0008, 0x009f, 0x000a, 6140e0aa64SRichard Purdie 0x0000, 0x0000 6240e0aa64SRichard Purdie }; 6340e0aa64SRichard Purdie 6417a52fd6SMark Brown #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) 6540e0aa64SRichard Purdie 6640e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"}; 67*59f72970SMark Brown 6840e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 6940e0aa64SRichard Purdie 70*59f72970SMark Brown static const struct soc_enum wm8731_insel_enum = 71*59f72970SMark Brown SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select); 72*59f72970SMark Brown 73*59f72970SMark Brown static const struct soc_enum wm8731_deemph_enum = 74*59f72970SMark Brown SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph); 75*59f72970SMark Brown 7640e0aa64SRichard Purdie 77d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); 78d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); 79d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 80d921184eSMark Brown static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0); 81d00efa64SMark Brown 8240e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = { 8340e0aa64SRichard Purdie 84d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, 85d00efa64SMark Brown 0, 127, 0, out_tlv), 86bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, 87bd903b6eSLiam Girdwood 7, 1, 0), 8840e0aa64SRichard Purdie 89d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, 90d00efa64SMark Brown in_tlv), 9140e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), 9240e0aa64SRichard Purdie 93d921184eSMark Brown SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv), 94ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), 9540e0aa64SRichard Purdie 96d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, 97d00efa64SMark Brown sidetone_tlv), 9840e0aa64SRichard Purdie 9940e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), 10040e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), 10140e0aa64SRichard Purdie 102*59f72970SMark Brown SOC_ENUM("Playback De-emphasis", wm8731_deemph_enum), 10340e0aa64SRichard Purdie }; 10440e0aa64SRichard Purdie 10540e0aa64SRichard Purdie /* Output Mixer */ 10640e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { 10740e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), 10840e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), 10940e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), 11040e0aa64SRichard Purdie }; 11140e0aa64SRichard Purdie 11240e0aa64SRichard Purdie /* Input mux */ 11340e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls = 114*59f72970SMark Brown SOC_DAPM_ENUM("Input Select", wm8731_insel_enum); 11540e0aa64SRichard Purdie 11640e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { 1179745e824SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0), 11840e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, 11940e0aa64SRichard Purdie &wm8731_output_mixer_controls[0], 12040e0aa64SRichard Purdie ARRAY_SIZE(wm8731_output_mixer_controls)), 12140e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), 12240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"), 12340e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"), 12440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"), 12540e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"), 12640e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), 12740e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), 12840e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), 12940e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), 13040e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"), 13140e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"), 13240e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"), 13340e0aa64SRichard Purdie }; 13440e0aa64SRichard Purdie 1359745e824SMark Brown static int wm8731_check_osc(struct snd_soc_dapm_widget *source, 1369745e824SMark Brown struct snd_soc_dapm_widget *sink) 1379745e824SMark Brown { 1389745e824SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec); 1399745e824SMark Brown 1409745e824SMark Brown return wm8731->sysclk_type == WM8731_SYSCLK_MCLK; 1419745e824SMark Brown } 1429745e824SMark Brown 143a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = { 1449745e824SMark Brown {"DAC", NULL, "OSC", wm8731_check_osc}, 1459745e824SMark Brown {"ADC", NULL, "OSC", wm8731_check_osc}, 1469745e824SMark Brown 14740e0aa64SRichard Purdie /* output mixer */ 14840e0aa64SRichard Purdie {"Output Mixer", "Line Bypass Switch", "Line Input"}, 14940e0aa64SRichard Purdie {"Output Mixer", "HiFi Playback Switch", "DAC"}, 15040e0aa64SRichard Purdie {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, 15140e0aa64SRichard Purdie 15240e0aa64SRichard Purdie /* outputs */ 15340e0aa64SRichard Purdie {"RHPOUT", NULL, "Output Mixer"}, 15440e0aa64SRichard Purdie {"ROUT", NULL, "Output Mixer"}, 15540e0aa64SRichard Purdie {"LHPOUT", NULL, "Output Mixer"}, 15640e0aa64SRichard Purdie {"LOUT", NULL, "Output Mixer"}, 15740e0aa64SRichard Purdie 15840e0aa64SRichard Purdie /* input mux */ 15940e0aa64SRichard Purdie {"Input Mux", "Line In", "Line Input"}, 16040e0aa64SRichard Purdie {"Input Mux", "Mic", "Mic Bias"}, 16140e0aa64SRichard Purdie {"ADC", NULL, "Input Mux"}, 16240e0aa64SRichard Purdie 16340e0aa64SRichard Purdie /* inputs */ 16440e0aa64SRichard Purdie {"Line Input", NULL, "LLINEIN"}, 16540e0aa64SRichard Purdie {"Line Input", NULL, "RLINEIN"}, 16640e0aa64SRichard Purdie {"Mic Bias", NULL, "MICIN"}, 16740e0aa64SRichard Purdie }; 16840e0aa64SRichard Purdie 16940e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec) 17040e0aa64SRichard Purdie { 171ce6120ccSLiam Girdwood struct snd_soc_dapm_context *dapm = &codec->dapm; 17240e0aa64SRichard Purdie 173ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets, 174ce6120ccSLiam Girdwood ARRAY_SIZE(wm8731_dapm_widgets)); 175ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); 17640e0aa64SRichard Purdie 17740e0aa64SRichard Purdie return 0; 17840e0aa64SRichard Purdie } 17940e0aa64SRichard Purdie 18040e0aa64SRichard Purdie struct _coeff_div { 18140e0aa64SRichard Purdie u32 mclk; 18240e0aa64SRichard Purdie u32 rate; 18340e0aa64SRichard Purdie u16 fs; 18440e0aa64SRichard Purdie u8 sr:4; 18540e0aa64SRichard Purdie u8 bosr:1; 18640e0aa64SRichard Purdie u8 usb:1; 18740e0aa64SRichard Purdie }; 18840e0aa64SRichard Purdie 18940e0aa64SRichard Purdie /* codec mclk clock divider coefficients */ 19040e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = { 19140e0aa64SRichard Purdie /* 48k */ 19240e0aa64SRichard Purdie {12288000, 48000, 256, 0x0, 0x0, 0x0}, 19340e0aa64SRichard Purdie {18432000, 48000, 384, 0x0, 0x1, 0x0}, 19440e0aa64SRichard Purdie {12000000, 48000, 250, 0x0, 0x0, 0x1}, 19540e0aa64SRichard Purdie 19640e0aa64SRichard Purdie /* 32k */ 19740e0aa64SRichard Purdie {12288000, 32000, 384, 0x6, 0x0, 0x0}, 19840e0aa64SRichard Purdie {18432000, 32000, 576, 0x6, 0x1, 0x0}, 199298a2c75SFrank Mandarino {12000000, 32000, 375, 0x6, 0x0, 0x1}, 20040e0aa64SRichard Purdie 20140e0aa64SRichard Purdie /* 8k */ 20240e0aa64SRichard Purdie {12288000, 8000, 1536, 0x3, 0x0, 0x0}, 20340e0aa64SRichard Purdie {18432000, 8000, 2304, 0x3, 0x1, 0x0}, 20440e0aa64SRichard Purdie {11289600, 8000, 1408, 0xb, 0x0, 0x0}, 20540e0aa64SRichard Purdie {16934400, 8000, 2112, 0xb, 0x1, 0x0}, 20640e0aa64SRichard Purdie {12000000, 8000, 1500, 0x3, 0x0, 0x1}, 20740e0aa64SRichard Purdie 20840e0aa64SRichard Purdie /* 96k */ 20940e0aa64SRichard Purdie {12288000, 96000, 128, 0x7, 0x0, 0x0}, 21040e0aa64SRichard Purdie {18432000, 96000, 192, 0x7, 0x1, 0x0}, 21140e0aa64SRichard Purdie {12000000, 96000, 125, 0x7, 0x0, 0x1}, 21240e0aa64SRichard Purdie 21340e0aa64SRichard Purdie /* 44.1k */ 21440e0aa64SRichard Purdie {11289600, 44100, 256, 0x8, 0x0, 0x0}, 21540e0aa64SRichard Purdie {16934400, 44100, 384, 0x8, 0x1, 0x0}, 21640e0aa64SRichard Purdie {12000000, 44100, 272, 0x8, 0x1, 0x1}, 21740e0aa64SRichard Purdie 21840e0aa64SRichard Purdie /* 88.2k */ 21940e0aa64SRichard Purdie {11289600, 88200, 128, 0xf, 0x0, 0x0}, 22040e0aa64SRichard Purdie {16934400, 88200, 192, 0xf, 0x1, 0x0}, 22140e0aa64SRichard Purdie {12000000, 88200, 136, 0xf, 0x1, 0x1}, 22240e0aa64SRichard Purdie }; 22340e0aa64SRichard Purdie 22440e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate) 22540e0aa64SRichard Purdie { 22640e0aa64SRichard Purdie int i; 22740e0aa64SRichard Purdie 22840e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 22940e0aa64SRichard Purdie if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 23040e0aa64SRichard Purdie return i; 23140e0aa64SRichard Purdie } 23240e0aa64SRichard Purdie return 0; 23340e0aa64SRichard Purdie } 23440e0aa64SRichard Purdie 235b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream, 236dee89c4dSMark Brown struct snd_pcm_hw_params *params, 237dee89c4dSMark Brown struct snd_soc_dai *dai) 23840e0aa64SRichard Purdie { 239f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 240b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 24117a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; 242b36d61d4SFrank Mandarino int i = get_coeff(wm8731->sysclk, params_rate(params)); 243b36d61d4SFrank Mandarino u16 srate = (coeff_div[i].sr << 2) | 244b36d61d4SFrank Mandarino (coeff_div[i].bosr << 1) | coeff_div[i].usb; 24540e0aa64SRichard Purdie 24617a52fd6SMark Brown snd_soc_write(codec, WM8731_SRATE, srate); 24740e0aa64SRichard Purdie 248b36d61d4SFrank Mandarino /* bit size */ 249b36d61d4SFrank Mandarino switch (params_format(params)) { 250b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S16_LE: 251b36d61d4SFrank Mandarino break; 252b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S20_3LE: 253b36d61d4SFrank Mandarino iface |= 0x0004; 254b36d61d4SFrank Mandarino break; 255b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S24_LE: 256b36d61d4SFrank Mandarino iface |= 0x0008; 257b36d61d4SFrank Mandarino break; 258b36d61d4SFrank Mandarino } 259b36d61d4SFrank Mandarino 26017a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 261b36d61d4SFrank Mandarino return 0; 26240e0aa64SRichard Purdie } 26340e0aa64SRichard Purdie 264dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, 265dee89c4dSMark Brown struct snd_soc_dai *dai) 26640e0aa64SRichard Purdie { 267f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 26840e0aa64SRichard Purdie 26940e0aa64SRichard Purdie /* set active */ 27017a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0001); 271b36d61d4SFrank Mandarino 27240e0aa64SRichard Purdie return 0; 27340e0aa64SRichard Purdie } 27440e0aa64SRichard Purdie 275dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream, 276dee89c4dSMark Brown struct snd_soc_dai *dai) 27740e0aa64SRichard Purdie { 278f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 27940e0aa64SRichard Purdie 28040e0aa64SRichard Purdie /* deactivate */ 28140e0aa64SRichard Purdie if (!codec->active) { 28240e0aa64SRichard Purdie udelay(50); 28317a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 28440e0aa64SRichard Purdie } 28540e0aa64SRichard Purdie } 28640e0aa64SRichard Purdie 287e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute) 28840e0aa64SRichard Purdie { 289b36d61d4SFrank Mandarino struct snd_soc_codec *codec = dai->codec; 29017a52fd6SMark Brown u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; 291b36d61d4SFrank Mandarino 29240e0aa64SRichard Purdie if (mute) 29317a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); 29440e0aa64SRichard Purdie else 29517a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg); 29640e0aa64SRichard Purdie return 0; 29740e0aa64SRichard Purdie } 29840e0aa64SRichard Purdie 299e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, 300b36d61d4SFrank Mandarino int clk_id, unsigned int freq, int dir) 301b36d61d4SFrank Mandarino { 302b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 303b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 304b36d61d4SFrank Mandarino 3059745e824SMark Brown switch (clk_id) { 3069745e824SMark Brown case WM8731_SYSCLK_XTAL: 3079745e824SMark Brown case WM8731_SYSCLK_MCLK: 3089745e824SMark Brown wm8731->sysclk_type = clk_id; 3099745e824SMark Brown break; 3109745e824SMark Brown default: 3119745e824SMark Brown return -EINVAL; 3129745e824SMark Brown } 3139745e824SMark Brown 314b36d61d4SFrank Mandarino switch (freq) { 315b36d61d4SFrank Mandarino case 11289600: 316b36d61d4SFrank Mandarino case 12000000: 317b36d61d4SFrank Mandarino case 12288000: 318b36d61d4SFrank Mandarino case 16934400: 319b36d61d4SFrank Mandarino case 18432000: 320b36d61d4SFrank Mandarino wm8731->sysclk = freq; 3219745e824SMark Brown break; 3229745e824SMark Brown default: 323b36d61d4SFrank Mandarino return -EINVAL; 324b36d61d4SFrank Mandarino } 325b36d61d4SFrank Mandarino 326ce6120ccSLiam Girdwood snd_soc_dapm_sync(&codec->dapm); 3279745e824SMark Brown 3289745e824SMark Brown return 0; 3299745e824SMark Brown } 3309745e824SMark Brown 331b36d61d4SFrank Mandarino 332e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, 333b36d61d4SFrank Mandarino unsigned int fmt) 334b36d61d4SFrank Mandarino { 335b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 336b36d61d4SFrank Mandarino u16 iface = 0; 337b36d61d4SFrank Mandarino 338b36d61d4SFrank Mandarino /* set master/slave audio interface */ 339b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 340b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBM_CFM: 341b36d61d4SFrank Mandarino iface |= 0x0040; 342b36d61d4SFrank Mandarino break; 343b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBS_CFS: 344b36d61d4SFrank Mandarino break; 345b36d61d4SFrank Mandarino default: 346b36d61d4SFrank Mandarino return -EINVAL; 347b36d61d4SFrank Mandarino } 348b36d61d4SFrank Mandarino 349b36d61d4SFrank Mandarino /* interface format */ 350b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 351b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_I2S: 352b36d61d4SFrank Mandarino iface |= 0x0002; 353b36d61d4SFrank Mandarino break; 354b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_RIGHT_J: 355b36d61d4SFrank Mandarino break; 356b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_LEFT_J: 357b36d61d4SFrank Mandarino iface |= 0x0001; 358b36d61d4SFrank Mandarino break; 359b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_A: 360b36d61d4SFrank Mandarino iface |= 0x0003; 361b36d61d4SFrank Mandarino break; 362b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_B: 363b36d61d4SFrank Mandarino iface |= 0x0013; 364b36d61d4SFrank Mandarino break; 365b36d61d4SFrank Mandarino default: 366b36d61d4SFrank Mandarino return -EINVAL; 367b36d61d4SFrank Mandarino } 368b36d61d4SFrank Mandarino 369b36d61d4SFrank Mandarino /* clock inversion */ 370b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 371b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_NF: 372b36d61d4SFrank Mandarino break; 373b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_IF: 374b36d61d4SFrank Mandarino iface |= 0x0090; 375b36d61d4SFrank Mandarino break; 376b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_NF: 377b36d61d4SFrank Mandarino iface |= 0x0080; 378b36d61d4SFrank Mandarino break; 379b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_IF: 380b36d61d4SFrank Mandarino iface |= 0x0010; 381b36d61d4SFrank Mandarino break; 382b36d61d4SFrank Mandarino default: 383b36d61d4SFrank Mandarino return -EINVAL; 384b36d61d4SFrank Mandarino } 385b36d61d4SFrank Mandarino 386b36d61d4SFrank Mandarino /* set iface */ 38717a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 388b36d61d4SFrank Mandarino return 0; 389b36d61d4SFrank Mandarino } 390b36d61d4SFrank Mandarino 3910be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec, 3920be9898aSMark Brown enum snd_soc_bias_level level) 39340e0aa64SRichard Purdie { 39406ae9988SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 39506ae9988SMark Brown int i, ret; 39606ae9988SMark Brown u8 data[2]; 39706ae9988SMark Brown u16 *cache = codec->reg_cache; 39822d22ee5SMark Brown u16 reg; 39940e0aa64SRichard Purdie 4000be9898aSMark Brown switch (level) { 4010be9898aSMark Brown case SND_SOC_BIAS_ON: 40240e0aa64SRichard Purdie break; 4030be9898aSMark Brown case SND_SOC_BIAS_PREPARE: 40440e0aa64SRichard Purdie break; 4050be9898aSMark Brown case SND_SOC_BIAS_STANDBY: 406ce6120ccSLiam Girdwood if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { 40706ae9988SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 40806ae9988SMark Brown wm8731->supplies); 40906ae9988SMark Brown if (ret != 0) 41006ae9988SMark Brown return ret; 41106ae9988SMark Brown 41206ae9988SMark Brown /* Sync reg_cache with the hardware */ 41306ae9988SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { 41406ae9988SMark Brown if (cache[i] == wm8731_reg[i]) 41506ae9988SMark Brown continue; 41606ae9988SMark Brown 41706ae9988SMark Brown data[0] = (i << 1) | ((cache[i] >> 8) 41806ae9988SMark Brown & 0x0001); 41906ae9988SMark Brown data[1] = cache[i] & 0x00ff; 42006ae9988SMark Brown codec->hw_write(codec->control_data, data, 2); 42106ae9988SMark Brown } 42206ae9988SMark Brown } 42306ae9988SMark Brown 42422d22ee5SMark Brown /* Clear PWROFF, gate CLKOUT, everything else as-is */ 42517a52fd6SMark Brown reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; 42617a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, reg | 0x0040); 42740e0aa64SRichard Purdie break; 4280be9898aSMark Brown case SND_SOC_BIAS_OFF: 42917a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 43017a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, 0xffff); 43106ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), 43206ae9988SMark Brown wm8731->supplies); 43340e0aa64SRichard Purdie break; 43440e0aa64SRichard Purdie } 435ce6120ccSLiam Girdwood codec->dapm.bias_level = level; 43640e0aa64SRichard Purdie return 0; 43740e0aa64SRichard Purdie } 43840e0aa64SRichard Purdie 439e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 440b36d61d4SFrank Mandarino 441b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 442b36d61d4SFrank Mandarino SNDRV_PCM_FMTBIT_S24_LE) 443b36d61d4SFrank Mandarino 4446335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = { 4456335d055SEric Miao .prepare = wm8731_pcm_prepare, 4466335d055SEric Miao .hw_params = wm8731_hw_params, 4476335d055SEric Miao .shutdown = wm8731_shutdown, 4486335d055SEric Miao .digital_mute = wm8731_mute, 4496335d055SEric Miao .set_sysclk = wm8731_set_dai_sysclk, 4506335d055SEric Miao .set_fmt = wm8731_set_dai_fmt, 4516335d055SEric Miao }; 4526335d055SEric Miao 453f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = { 454f0fba2adSLiam Girdwood .name = "wm8731-hifi", 45540e0aa64SRichard Purdie .playback = { 45640e0aa64SRichard Purdie .stream_name = "Playback", 45740e0aa64SRichard Purdie .channels_min = 1, 45840e0aa64SRichard Purdie .channels_max = 2, 459b36d61d4SFrank Mandarino .rates = WM8731_RATES, 460b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 46140e0aa64SRichard Purdie .capture = { 46240e0aa64SRichard Purdie .stream_name = "Capture", 46340e0aa64SRichard Purdie .channels_min = 1, 46440e0aa64SRichard Purdie .channels_max = 2, 465b36d61d4SFrank Mandarino .rates = WM8731_RATES, 466b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 4676335d055SEric Miao .ops = &wm8731_dai_ops, 4684934482dSMark Brown .symmetric_rates = 1, 46940e0aa64SRichard Purdie }; 47040e0aa64SRichard Purdie 471b3b50b3fSMark Brown #ifdef CONFIG_PM 472f0fba2adSLiam Girdwood static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state) 47340e0aa64SRichard Purdie { 4740be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 47506ae9988SMark Brown 47640e0aa64SRichard Purdie return 0; 47740e0aa64SRichard Purdie } 47840e0aa64SRichard Purdie 479f0fba2adSLiam Girdwood static int wm8731_resume(struct snd_soc_codec *codec) 48040e0aa64SRichard Purdie { 4810be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 4827dea7c01SMark Brown 48340e0aa64SRichard Purdie return 0; 48440e0aa64SRichard Purdie } 485b3b50b3fSMark Brown #else 486b3b50b3fSMark Brown #define wm8731_suspend NULL 487b3b50b3fSMark Brown #define wm8731_resume NULL 488b3b50b3fSMark Brown #endif 48940e0aa64SRichard Purdie 490f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec) 49140e0aa64SRichard Purdie { 492f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 493f0fba2adSLiam Girdwood int ret = 0, i; 49440e0aa64SRichard Purdie 495f0fba2adSLiam Girdwood ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type); 49617a52fd6SMark Brown if (ret < 0) { 49717a52fd6SMark Brown dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 498f0fba2adSLiam Girdwood return ret; 49917a52fd6SMark Brown } 50017a52fd6SMark Brown 5017dea7c01SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) 5027dea7c01SMark Brown wm8731->supplies[i].supply = wm8731_supply_names[i]; 5037dea7c01SMark Brown 5047dea7c01SMark Brown ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), 5057dea7c01SMark Brown wm8731->supplies); 5067dea7c01SMark Brown if (ret != 0) { 5077dea7c01SMark Brown dev_err(codec->dev, "Failed to request supplies: %d\n", ret); 508f0fba2adSLiam Girdwood return ret; 5097dea7c01SMark Brown } 5107dea7c01SMark Brown 5117dea7c01SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 5127dea7c01SMark Brown wm8731->supplies); 5137dea7c01SMark Brown if (ret != 0) { 5147dea7c01SMark Brown dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); 5157dea7c01SMark Brown goto err_regulator_get; 5167dea7c01SMark Brown } 5177dea7c01SMark Brown 518519cf2dfSMark Brown ret = wm8731_reset(codec); 519519cf2dfSMark Brown if (ret < 0) { 520fe5422fcSMark Brown dev_err(codec->dev, "Failed to issue reset: %d\n", ret); 5217dea7c01SMark Brown goto err_regulator_enable; 522519cf2dfSMark Brown } 523519cf2dfSMark Brown 5245998102bSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 5255998102bSMark Brown 5265998102bSMark Brown /* Latch the update bits */ 52717a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); 52817a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); 52917a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); 53017a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); 5315998102bSMark Brown 532ce3bdaa8SMark Brown /* Disable bypass path by default */ 5332062ea52SDimitris Papastamos snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0); 534ce3bdaa8SMark Brown 535f0fba2adSLiam Girdwood snd_soc_add_controls(codec, wm8731_snd_controls, 536f0fba2adSLiam Girdwood ARRAY_SIZE(wm8731_snd_controls)); 537f0fba2adSLiam Girdwood wm8731_add_widgets(codec); 5385998102bSMark Brown 53906ae9988SMark Brown /* Regulators will have been enabled by bias management */ 54006ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 54106ae9988SMark Brown 542a8035c8fSMark Brown return 0; 543fe5422fcSMark Brown 5447dea7c01SMark Brown err_regulator_enable: 5457dea7c01SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5467dea7c01SMark Brown err_regulator_get: 5477dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 548f0fba2adSLiam Girdwood 549fe5422fcSMark Brown return ret; 550a8035c8fSMark Brown } 551a8035c8fSMark Brown 552f0fba2adSLiam Girdwood /* power down chip */ 553f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec) 5545998102bSMark Brown { 555f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 556f0fba2adSLiam Girdwood 557f0fba2adSLiam Girdwood wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 558f0fba2adSLiam Girdwood 559f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5607dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 561f0fba2adSLiam Girdwood 562f0fba2adSLiam Girdwood return 0; 5635998102bSMark Brown } 564a8035c8fSMark Brown 565f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { 566f0fba2adSLiam Girdwood .probe = wm8731_probe, 567f0fba2adSLiam Girdwood .remove = wm8731_remove, 568f0fba2adSLiam Girdwood .suspend = wm8731_suspend, 569f0fba2adSLiam Girdwood .resume = wm8731_resume, 570f0fba2adSLiam Girdwood .set_bias_level = wm8731_set_bias_level, 571e5eec34cSDimitris Papastamos .reg_cache_size = ARRAY_SIZE(wm8731_reg), 572f0fba2adSLiam Girdwood .reg_word_size = sizeof(u16), 573f0fba2adSLiam Girdwood .reg_cache_default = wm8731_reg, 574f0fba2adSLiam Girdwood }; 575f0fba2adSLiam Girdwood 5765998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 5775998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi) 5785998102bSMark Brown { 5795998102bSMark Brown struct wm8731_priv *wm8731; 580f0fba2adSLiam Girdwood int ret; 5815998102bSMark Brown 5825998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 5835998102bSMark Brown if (wm8731 == NULL) 5845998102bSMark Brown return -ENOMEM; 5855998102bSMark Brown 586f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_SPI; 587f0fba2adSLiam Girdwood spi_set_drvdata(spi, wm8731); 5885998102bSMark Brown 589f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&spi->dev, 590f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 591f0fba2adSLiam Girdwood if (ret < 0) 592f0fba2adSLiam Girdwood kfree(wm8731); 593f0fba2adSLiam Girdwood return ret; 5945998102bSMark Brown } 5955998102bSMark Brown 5965998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi) 5975998102bSMark Brown { 598f0fba2adSLiam Girdwood snd_soc_unregister_codec(&spi->dev); 599f0fba2adSLiam Girdwood kfree(spi_get_drvdata(spi)); 6005998102bSMark Brown return 0; 6015998102bSMark Brown } 6025998102bSMark Brown 6035998102bSMark Brown static struct spi_driver wm8731_spi_driver = { 6045998102bSMark Brown .driver = { 605f0fba2adSLiam Girdwood .name = "wm8731-codec", 6065998102bSMark Brown .owner = THIS_MODULE, 6075998102bSMark Brown }, 6085998102bSMark Brown .probe = wm8731_spi_probe, 6095998102bSMark Brown .remove = __devexit_p(wm8731_spi_remove), 6105998102bSMark Brown }; 611a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */ 612a8035c8fSMark Brown 613a8035c8fSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 614c6f29811SMark Brown static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, 615a8035c8fSMark Brown const struct i2c_device_id *id) 616a8035c8fSMark Brown { 6175998102bSMark Brown struct wm8731_priv *wm8731; 618f0fba2adSLiam Girdwood int ret; 619a8035c8fSMark Brown 6205998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 6215998102bSMark Brown if (wm8731 == NULL) 6225998102bSMark Brown return -ENOMEM; 6235998102bSMark Brown 6245998102bSMark Brown i2c_set_clientdata(i2c, wm8731); 625f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_I2C; 626a8035c8fSMark Brown 627f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&i2c->dev, 628f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 629f0fba2adSLiam Girdwood if (ret < 0) 630f0fba2adSLiam Girdwood kfree(wm8731); 631f0fba2adSLiam Girdwood return ret; 632a8035c8fSMark Brown } 633a8035c8fSMark Brown 634c6f29811SMark Brown static __devexit int wm8731_i2c_remove(struct i2c_client *client) 635a8035c8fSMark Brown { 636f0fba2adSLiam Girdwood snd_soc_unregister_codec(&client->dev); 637f0fba2adSLiam Girdwood kfree(i2c_get_clientdata(client)); 638a8035c8fSMark Brown return 0; 639a8035c8fSMark Brown } 640a8035c8fSMark Brown 641a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = { 642a8035c8fSMark Brown { "wm8731", 0 }, 643a8035c8fSMark Brown { } 644a8035c8fSMark Brown }; 645a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); 646a8035c8fSMark Brown 647a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = { 648a8035c8fSMark Brown .driver = { 649f0fba2adSLiam Girdwood .name = "wm8731-codec", 650a8035c8fSMark Brown .owner = THIS_MODULE, 651a8035c8fSMark Brown }, 652a8035c8fSMark Brown .probe = wm8731_i2c_probe, 653c6f29811SMark Brown .remove = __devexit_p(wm8731_i2c_remove), 654a8035c8fSMark Brown .id_table = wm8731_i2c_id, 655a8035c8fSMark Brown }; 656a8035c8fSMark Brown #endif 657a8035c8fSMark Brown 658c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void) 65964089b84SMark Brown { 660f0fba2adSLiam Girdwood int ret = 0; 6615998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6625998102bSMark Brown ret = i2c_add_driver(&wm8731_i2c_driver); 6635998102bSMark Brown if (ret != 0) { 6645998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", 6655998102bSMark Brown ret); 6665998102bSMark Brown } 6675998102bSMark Brown #endif 6685998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6695998102bSMark Brown ret = spi_register_driver(&wm8731_spi_driver); 6705998102bSMark Brown if (ret != 0) { 6715998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", 6725998102bSMark Brown ret); 6735998102bSMark Brown } 6745998102bSMark Brown #endif 675f0fba2adSLiam Girdwood return ret; 67664089b84SMark Brown } 67764089b84SMark Brown module_init(wm8731_modinit); 67864089b84SMark Brown 67964089b84SMark Brown static void __exit wm8731_exit(void) 68064089b84SMark Brown { 6815998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6825998102bSMark Brown i2c_del_driver(&wm8731_i2c_driver); 6835998102bSMark Brown #endif 6845998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6855998102bSMark Brown spi_unregister_driver(&wm8731_spi_driver); 6865998102bSMark Brown #endif 68764089b84SMark Brown } 68864089b84SMark Brown module_exit(wm8731_exit); 68964089b84SMark Brown 69040e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver"); 69140e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie"); 69240e0aa64SRichard Purdie MODULE_LICENSE("GPL"); 693