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"}; 6740e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 6840e0aa64SRichard Purdie 6940e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = { 7040e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), 7140e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), 7240e0aa64SRichard Purdie }; 7340e0aa64SRichard Purdie 74d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); 75d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); 76d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 77*d921184eSMark Brown static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0); 78d00efa64SMark Brown 7940e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = { 8040e0aa64SRichard Purdie 81d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, 82d00efa64SMark Brown 0, 127, 0, out_tlv), 83bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, 84bd903b6eSLiam Girdwood 7, 1, 0), 8540e0aa64SRichard Purdie 86d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, 87d00efa64SMark Brown in_tlv), 8840e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), 8940e0aa64SRichard Purdie 90*d921184eSMark Brown SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv), 91ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), 9240e0aa64SRichard Purdie 93d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, 94d00efa64SMark Brown sidetone_tlv), 9540e0aa64SRichard Purdie 9640e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), 9740e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), 9840e0aa64SRichard Purdie 9940e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), 10040e0aa64SRichard Purdie }; 10140e0aa64SRichard Purdie 10240e0aa64SRichard Purdie /* Output Mixer */ 10340e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { 10440e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), 10540e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), 10640e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), 10740e0aa64SRichard Purdie }; 10840e0aa64SRichard Purdie 10940e0aa64SRichard Purdie /* Input mux */ 11040e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls = 11140e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); 11240e0aa64SRichard Purdie 11340e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { 1149745e824SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0), 11540e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, 11640e0aa64SRichard Purdie &wm8731_output_mixer_controls[0], 11740e0aa64SRichard Purdie ARRAY_SIZE(wm8731_output_mixer_controls)), 11840e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), 11940e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"), 12040e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"), 12140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"), 12240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"), 12340e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), 12440e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), 12540e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), 12640e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), 12740e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"), 12840e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"), 12940e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"), 13040e0aa64SRichard Purdie }; 13140e0aa64SRichard Purdie 1329745e824SMark Brown static int wm8731_check_osc(struct snd_soc_dapm_widget *source, 1339745e824SMark Brown struct snd_soc_dapm_widget *sink) 1349745e824SMark Brown { 1359745e824SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec); 1369745e824SMark Brown 1379745e824SMark Brown return wm8731->sysclk_type == WM8731_SYSCLK_MCLK; 1389745e824SMark Brown } 1399745e824SMark Brown 140a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = { 1419745e824SMark Brown {"DAC", NULL, "OSC", wm8731_check_osc}, 1429745e824SMark Brown {"ADC", NULL, "OSC", wm8731_check_osc}, 1439745e824SMark Brown 14440e0aa64SRichard Purdie /* output mixer */ 14540e0aa64SRichard Purdie {"Output Mixer", "Line Bypass Switch", "Line Input"}, 14640e0aa64SRichard Purdie {"Output Mixer", "HiFi Playback Switch", "DAC"}, 14740e0aa64SRichard Purdie {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, 14840e0aa64SRichard Purdie 14940e0aa64SRichard Purdie /* outputs */ 15040e0aa64SRichard Purdie {"RHPOUT", NULL, "Output Mixer"}, 15140e0aa64SRichard Purdie {"ROUT", NULL, "Output Mixer"}, 15240e0aa64SRichard Purdie {"LHPOUT", NULL, "Output Mixer"}, 15340e0aa64SRichard Purdie {"LOUT", NULL, "Output Mixer"}, 15440e0aa64SRichard Purdie 15540e0aa64SRichard Purdie /* input mux */ 15640e0aa64SRichard Purdie {"Input Mux", "Line In", "Line Input"}, 15740e0aa64SRichard Purdie {"Input Mux", "Mic", "Mic Bias"}, 15840e0aa64SRichard Purdie {"ADC", NULL, "Input Mux"}, 15940e0aa64SRichard Purdie 16040e0aa64SRichard Purdie /* inputs */ 16140e0aa64SRichard Purdie {"Line Input", NULL, "LLINEIN"}, 16240e0aa64SRichard Purdie {"Line Input", NULL, "RLINEIN"}, 16340e0aa64SRichard Purdie {"Mic Bias", NULL, "MICIN"}, 16440e0aa64SRichard Purdie }; 16540e0aa64SRichard Purdie 16640e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec) 16740e0aa64SRichard Purdie { 168ce6120ccSLiam Girdwood struct snd_soc_dapm_context *dapm = &codec->dapm; 16940e0aa64SRichard Purdie 170ce6120ccSLiam Girdwood snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets, 171ce6120ccSLiam Girdwood ARRAY_SIZE(wm8731_dapm_widgets)); 172ce6120ccSLiam Girdwood snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); 17340e0aa64SRichard Purdie 17440e0aa64SRichard Purdie return 0; 17540e0aa64SRichard Purdie } 17640e0aa64SRichard Purdie 17740e0aa64SRichard Purdie struct _coeff_div { 17840e0aa64SRichard Purdie u32 mclk; 17940e0aa64SRichard Purdie u32 rate; 18040e0aa64SRichard Purdie u16 fs; 18140e0aa64SRichard Purdie u8 sr:4; 18240e0aa64SRichard Purdie u8 bosr:1; 18340e0aa64SRichard Purdie u8 usb:1; 18440e0aa64SRichard Purdie }; 18540e0aa64SRichard Purdie 18640e0aa64SRichard Purdie /* codec mclk clock divider coefficients */ 18740e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = { 18840e0aa64SRichard Purdie /* 48k */ 18940e0aa64SRichard Purdie {12288000, 48000, 256, 0x0, 0x0, 0x0}, 19040e0aa64SRichard Purdie {18432000, 48000, 384, 0x0, 0x1, 0x0}, 19140e0aa64SRichard Purdie {12000000, 48000, 250, 0x0, 0x0, 0x1}, 19240e0aa64SRichard Purdie 19340e0aa64SRichard Purdie /* 32k */ 19440e0aa64SRichard Purdie {12288000, 32000, 384, 0x6, 0x0, 0x0}, 19540e0aa64SRichard Purdie {18432000, 32000, 576, 0x6, 0x1, 0x0}, 196298a2c75SFrank Mandarino {12000000, 32000, 375, 0x6, 0x0, 0x1}, 19740e0aa64SRichard Purdie 19840e0aa64SRichard Purdie /* 8k */ 19940e0aa64SRichard Purdie {12288000, 8000, 1536, 0x3, 0x0, 0x0}, 20040e0aa64SRichard Purdie {18432000, 8000, 2304, 0x3, 0x1, 0x0}, 20140e0aa64SRichard Purdie {11289600, 8000, 1408, 0xb, 0x0, 0x0}, 20240e0aa64SRichard Purdie {16934400, 8000, 2112, 0xb, 0x1, 0x0}, 20340e0aa64SRichard Purdie {12000000, 8000, 1500, 0x3, 0x0, 0x1}, 20440e0aa64SRichard Purdie 20540e0aa64SRichard Purdie /* 96k */ 20640e0aa64SRichard Purdie {12288000, 96000, 128, 0x7, 0x0, 0x0}, 20740e0aa64SRichard Purdie {18432000, 96000, 192, 0x7, 0x1, 0x0}, 20840e0aa64SRichard Purdie {12000000, 96000, 125, 0x7, 0x0, 0x1}, 20940e0aa64SRichard Purdie 21040e0aa64SRichard Purdie /* 44.1k */ 21140e0aa64SRichard Purdie {11289600, 44100, 256, 0x8, 0x0, 0x0}, 21240e0aa64SRichard Purdie {16934400, 44100, 384, 0x8, 0x1, 0x0}, 21340e0aa64SRichard Purdie {12000000, 44100, 272, 0x8, 0x1, 0x1}, 21440e0aa64SRichard Purdie 21540e0aa64SRichard Purdie /* 88.2k */ 21640e0aa64SRichard Purdie {11289600, 88200, 128, 0xf, 0x0, 0x0}, 21740e0aa64SRichard Purdie {16934400, 88200, 192, 0xf, 0x1, 0x0}, 21840e0aa64SRichard Purdie {12000000, 88200, 136, 0xf, 0x1, 0x1}, 21940e0aa64SRichard Purdie }; 22040e0aa64SRichard Purdie 22140e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate) 22240e0aa64SRichard Purdie { 22340e0aa64SRichard Purdie int i; 22440e0aa64SRichard Purdie 22540e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 22640e0aa64SRichard Purdie if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 22740e0aa64SRichard Purdie return i; 22840e0aa64SRichard Purdie } 22940e0aa64SRichard Purdie return 0; 23040e0aa64SRichard Purdie } 23140e0aa64SRichard Purdie 232b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream, 233dee89c4dSMark Brown struct snd_pcm_hw_params *params, 234dee89c4dSMark Brown struct snd_soc_dai *dai) 23540e0aa64SRichard Purdie { 236f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 237b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 23817a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; 239b36d61d4SFrank Mandarino int i = get_coeff(wm8731->sysclk, params_rate(params)); 240b36d61d4SFrank Mandarino u16 srate = (coeff_div[i].sr << 2) | 241b36d61d4SFrank Mandarino (coeff_div[i].bosr << 1) | coeff_div[i].usb; 24240e0aa64SRichard Purdie 24317a52fd6SMark Brown snd_soc_write(codec, WM8731_SRATE, srate); 24440e0aa64SRichard Purdie 245b36d61d4SFrank Mandarino /* bit size */ 246b36d61d4SFrank Mandarino switch (params_format(params)) { 247b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S16_LE: 248b36d61d4SFrank Mandarino break; 249b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S20_3LE: 250b36d61d4SFrank Mandarino iface |= 0x0004; 251b36d61d4SFrank Mandarino break; 252b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S24_LE: 253b36d61d4SFrank Mandarino iface |= 0x0008; 254b36d61d4SFrank Mandarino break; 255b36d61d4SFrank Mandarino } 256b36d61d4SFrank Mandarino 25717a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 258b36d61d4SFrank Mandarino return 0; 25940e0aa64SRichard Purdie } 26040e0aa64SRichard Purdie 261dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, 262dee89c4dSMark Brown struct snd_soc_dai *dai) 26340e0aa64SRichard Purdie { 264f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 26540e0aa64SRichard Purdie 26640e0aa64SRichard Purdie /* set active */ 26717a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0001); 268b36d61d4SFrank Mandarino 26940e0aa64SRichard Purdie return 0; 27040e0aa64SRichard Purdie } 27140e0aa64SRichard Purdie 272dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream, 273dee89c4dSMark Brown struct snd_soc_dai *dai) 27440e0aa64SRichard Purdie { 275f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 27640e0aa64SRichard Purdie 27740e0aa64SRichard Purdie /* deactivate */ 27840e0aa64SRichard Purdie if (!codec->active) { 27940e0aa64SRichard Purdie udelay(50); 28017a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 28140e0aa64SRichard Purdie } 28240e0aa64SRichard Purdie } 28340e0aa64SRichard Purdie 284e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute) 28540e0aa64SRichard Purdie { 286b36d61d4SFrank Mandarino struct snd_soc_codec *codec = dai->codec; 28717a52fd6SMark Brown u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; 288b36d61d4SFrank Mandarino 28940e0aa64SRichard Purdie if (mute) 29017a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); 29140e0aa64SRichard Purdie else 29217a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg); 29340e0aa64SRichard Purdie return 0; 29440e0aa64SRichard Purdie } 29540e0aa64SRichard Purdie 296e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, 297b36d61d4SFrank Mandarino int clk_id, unsigned int freq, int dir) 298b36d61d4SFrank Mandarino { 299b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 300b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 301b36d61d4SFrank Mandarino 3029745e824SMark Brown switch (clk_id) { 3039745e824SMark Brown case WM8731_SYSCLK_XTAL: 3049745e824SMark Brown case WM8731_SYSCLK_MCLK: 3059745e824SMark Brown wm8731->sysclk_type = clk_id; 3069745e824SMark Brown break; 3079745e824SMark Brown default: 3089745e824SMark Brown return -EINVAL; 3099745e824SMark Brown } 3109745e824SMark Brown 311b36d61d4SFrank Mandarino switch (freq) { 312b36d61d4SFrank Mandarino case 11289600: 313b36d61d4SFrank Mandarino case 12000000: 314b36d61d4SFrank Mandarino case 12288000: 315b36d61d4SFrank Mandarino case 16934400: 316b36d61d4SFrank Mandarino case 18432000: 317b36d61d4SFrank Mandarino wm8731->sysclk = freq; 3189745e824SMark Brown break; 3199745e824SMark Brown default: 320b36d61d4SFrank Mandarino return -EINVAL; 321b36d61d4SFrank Mandarino } 322b36d61d4SFrank Mandarino 323ce6120ccSLiam Girdwood snd_soc_dapm_sync(&codec->dapm); 3249745e824SMark Brown 3259745e824SMark Brown return 0; 3269745e824SMark Brown } 3279745e824SMark Brown 328b36d61d4SFrank Mandarino 329e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, 330b36d61d4SFrank Mandarino unsigned int fmt) 331b36d61d4SFrank Mandarino { 332b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 333b36d61d4SFrank Mandarino u16 iface = 0; 334b36d61d4SFrank Mandarino 335b36d61d4SFrank Mandarino /* set master/slave audio interface */ 336b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 337b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBM_CFM: 338b36d61d4SFrank Mandarino iface |= 0x0040; 339b36d61d4SFrank Mandarino break; 340b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBS_CFS: 341b36d61d4SFrank Mandarino break; 342b36d61d4SFrank Mandarino default: 343b36d61d4SFrank Mandarino return -EINVAL; 344b36d61d4SFrank Mandarino } 345b36d61d4SFrank Mandarino 346b36d61d4SFrank Mandarino /* interface format */ 347b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 348b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_I2S: 349b36d61d4SFrank Mandarino iface |= 0x0002; 350b36d61d4SFrank Mandarino break; 351b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_RIGHT_J: 352b36d61d4SFrank Mandarino break; 353b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_LEFT_J: 354b36d61d4SFrank Mandarino iface |= 0x0001; 355b36d61d4SFrank Mandarino break; 356b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_A: 357b36d61d4SFrank Mandarino iface |= 0x0003; 358b36d61d4SFrank Mandarino break; 359b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_B: 360b36d61d4SFrank Mandarino iface |= 0x0013; 361b36d61d4SFrank Mandarino break; 362b36d61d4SFrank Mandarino default: 363b36d61d4SFrank Mandarino return -EINVAL; 364b36d61d4SFrank Mandarino } 365b36d61d4SFrank Mandarino 366b36d61d4SFrank Mandarino /* clock inversion */ 367b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 368b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_NF: 369b36d61d4SFrank Mandarino break; 370b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_IF: 371b36d61d4SFrank Mandarino iface |= 0x0090; 372b36d61d4SFrank Mandarino break; 373b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_NF: 374b36d61d4SFrank Mandarino iface |= 0x0080; 375b36d61d4SFrank Mandarino break; 376b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_IF: 377b36d61d4SFrank Mandarino iface |= 0x0010; 378b36d61d4SFrank Mandarino break; 379b36d61d4SFrank Mandarino default: 380b36d61d4SFrank Mandarino return -EINVAL; 381b36d61d4SFrank Mandarino } 382b36d61d4SFrank Mandarino 383b36d61d4SFrank Mandarino /* set iface */ 38417a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 385b36d61d4SFrank Mandarino return 0; 386b36d61d4SFrank Mandarino } 387b36d61d4SFrank Mandarino 3880be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec, 3890be9898aSMark Brown enum snd_soc_bias_level level) 39040e0aa64SRichard Purdie { 39106ae9988SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 39206ae9988SMark Brown int i, ret; 39306ae9988SMark Brown u8 data[2]; 39406ae9988SMark Brown u16 *cache = codec->reg_cache; 39522d22ee5SMark Brown u16 reg; 39640e0aa64SRichard Purdie 3970be9898aSMark Brown switch (level) { 3980be9898aSMark Brown case SND_SOC_BIAS_ON: 39940e0aa64SRichard Purdie break; 4000be9898aSMark Brown case SND_SOC_BIAS_PREPARE: 40140e0aa64SRichard Purdie break; 4020be9898aSMark Brown case SND_SOC_BIAS_STANDBY: 403ce6120ccSLiam Girdwood if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { 40406ae9988SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 40506ae9988SMark Brown wm8731->supplies); 40606ae9988SMark Brown if (ret != 0) 40706ae9988SMark Brown return ret; 40806ae9988SMark Brown 40906ae9988SMark Brown /* Sync reg_cache with the hardware */ 41006ae9988SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { 41106ae9988SMark Brown if (cache[i] == wm8731_reg[i]) 41206ae9988SMark Brown continue; 41306ae9988SMark Brown 41406ae9988SMark Brown data[0] = (i << 1) | ((cache[i] >> 8) 41506ae9988SMark Brown & 0x0001); 41606ae9988SMark Brown data[1] = cache[i] & 0x00ff; 41706ae9988SMark Brown codec->hw_write(codec->control_data, data, 2); 41806ae9988SMark Brown } 41906ae9988SMark Brown } 42006ae9988SMark Brown 42122d22ee5SMark Brown /* Clear PWROFF, gate CLKOUT, everything else as-is */ 42217a52fd6SMark Brown reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; 42317a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, reg | 0x0040); 42440e0aa64SRichard Purdie break; 4250be9898aSMark Brown case SND_SOC_BIAS_OFF: 42617a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 42717a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, 0xffff); 42806ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), 42906ae9988SMark Brown wm8731->supplies); 43040e0aa64SRichard Purdie break; 43140e0aa64SRichard Purdie } 432ce6120ccSLiam Girdwood codec->dapm.bias_level = level; 43340e0aa64SRichard Purdie return 0; 43440e0aa64SRichard Purdie } 43540e0aa64SRichard Purdie 436e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 437b36d61d4SFrank Mandarino 438b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 439b36d61d4SFrank Mandarino SNDRV_PCM_FMTBIT_S24_LE) 440b36d61d4SFrank Mandarino 4416335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = { 4426335d055SEric Miao .prepare = wm8731_pcm_prepare, 4436335d055SEric Miao .hw_params = wm8731_hw_params, 4446335d055SEric Miao .shutdown = wm8731_shutdown, 4456335d055SEric Miao .digital_mute = wm8731_mute, 4466335d055SEric Miao .set_sysclk = wm8731_set_dai_sysclk, 4476335d055SEric Miao .set_fmt = wm8731_set_dai_fmt, 4486335d055SEric Miao }; 4496335d055SEric Miao 450f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = { 451f0fba2adSLiam Girdwood .name = "wm8731-hifi", 45240e0aa64SRichard Purdie .playback = { 45340e0aa64SRichard Purdie .stream_name = "Playback", 45440e0aa64SRichard Purdie .channels_min = 1, 45540e0aa64SRichard Purdie .channels_max = 2, 456b36d61d4SFrank Mandarino .rates = WM8731_RATES, 457b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 45840e0aa64SRichard Purdie .capture = { 45940e0aa64SRichard Purdie .stream_name = "Capture", 46040e0aa64SRichard Purdie .channels_min = 1, 46140e0aa64SRichard Purdie .channels_max = 2, 462b36d61d4SFrank Mandarino .rates = WM8731_RATES, 463b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 4646335d055SEric Miao .ops = &wm8731_dai_ops, 4654934482dSMark Brown .symmetric_rates = 1, 46640e0aa64SRichard Purdie }; 46740e0aa64SRichard Purdie 468b3b50b3fSMark Brown #ifdef CONFIG_PM 469f0fba2adSLiam Girdwood static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state) 47040e0aa64SRichard Purdie { 4710be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 47206ae9988SMark Brown 47340e0aa64SRichard Purdie return 0; 47440e0aa64SRichard Purdie } 47540e0aa64SRichard Purdie 476f0fba2adSLiam Girdwood static int wm8731_resume(struct snd_soc_codec *codec) 47740e0aa64SRichard Purdie { 4780be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 4797dea7c01SMark Brown 48040e0aa64SRichard Purdie return 0; 48140e0aa64SRichard Purdie } 482b3b50b3fSMark Brown #else 483b3b50b3fSMark Brown #define wm8731_suspend NULL 484b3b50b3fSMark Brown #define wm8731_resume NULL 485b3b50b3fSMark Brown #endif 48640e0aa64SRichard Purdie 487f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec) 48840e0aa64SRichard Purdie { 489f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 490f0fba2adSLiam Girdwood int ret = 0, i; 49140e0aa64SRichard Purdie 492f0fba2adSLiam Girdwood ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type); 49317a52fd6SMark Brown if (ret < 0) { 49417a52fd6SMark Brown dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 495f0fba2adSLiam Girdwood return ret; 49617a52fd6SMark Brown } 49717a52fd6SMark Brown 4987dea7c01SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) 4997dea7c01SMark Brown wm8731->supplies[i].supply = wm8731_supply_names[i]; 5007dea7c01SMark Brown 5017dea7c01SMark Brown ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), 5027dea7c01SMark Brown wm8731->supplies); 5037dea7c01SMark Brown if (ret != 0) { 5047dea7c01SMark Brown dev_err(codec->dev, "Failed to request supplies: %d\n", ret); 505f0fba2adSLiam Girdwood return ret; 5067dea7c01SMark Brown } 5077dea7c01SMark Brown 5087dea7c01SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 5097dea7c01SMark Brown wm8731->supplies); 5107dea7c01SMark Brown if (ret != 0) { 5117dea7c01SMark Brown dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); 5127dea7c01SMark Brown goto err_regulator_get; 5137dea7c01SMark Brown } 5147dea7c01SMark Brown 515519cf2dfSMark Brown ret = wm8731_reset(codec); 516519cf2dfSMark Brown if (ret < 0) { 517fe5422fcSMark Brown dev_err(codec->dev, "Failed to issue reset: %d\n", ret); 5187dea7c01SMark Brown goto err_regulator_enable; 519519cf2dfSMark Brown } 520519cf2dfSMark Brown 5215998102bSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 5225998102bSMark Brown 5235998102bSMark Brown /* Latch the update bits */ 52417a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); 52517a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); 52617a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); 52717a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); 5285998102bSMark Brown 529ce3bdaa8SMark Brown /* Disable bypass path by default */ 5302062ea52SDimitris Papastamos snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0); 531ce3bdaa8SMark Brown 532f0fba2adSLiam Girdwood snd_soc_add_controls(codec, wm8731_snd_controls, 533f0fba2adSLiam Girdwood ARRAY_SIZE(wm8731_snd_controls)); 534f0fba2adSLiam Girdwood wm8731_add_widgets(codec); 5355998102bSMark Brown 53606ae9988SMark Brown /* Regulators will have been enabled by bias management */ 53706ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 53806ae9988SMark Brown 539a8035c8fSMark Brown return 0; 540fe5422fcSMark Brown 5417dea7c01SMark Brown err_regulator_enable: 5427dea7c01SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5437dea7c01SMark Brown err_regulator_get: 5447dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 545f0fba2adSLiam Girdwood 546fe5422fcSMark Brown return ret; 547a8035c8fSMark Brown } 548a8035c8fSMark Brown 549f0fba2adSLiam Girdwood /* power down chip */ 550f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec) 5515998102bSMark Brown { 552f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 553f0fba2adSLiam Girdwood 554f0fba2adSLiam Girdwood wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 555f0fba2adSLiam Girdwood 556f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5577dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 558f0fba2adSLiam Girdwood 559f0fba2adSLiam Girdwood return 0; 5605998102bSMark Brown } 561a8035c8fSMark Brown 562f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { 563f0fba2adSLiam Girdwood .probe = wm8731_probe, 564f0fba2adSLiam Girdwood .remove = wm8731_remove, 565f0fba2adSLiam Girdwood .suspend = wm8731_suspend, 566f0fba2adSLiam Girdwood .resume = wm8731_resume, 567f0fba2adSLiam Girdwood .set_bias_level = wm8731_set_bias_level, 568e5eec34cSDimitris Papastamos .reg_cache_size = ARRAY_SIZE(wm8731_reg), 569f0fba2adSLiam Girdwood .reg_word_size = sizeof(u16), 570f0fba2adSLiam Girdwood .reg_cache_default = wm8731_reg, 571f0fba2adSLiam Girdwood }; 572f0fba2adSLiam Girdwood 5735998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 5745998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi) 5755998102bSMark Brown { 5765998102bSMark Brown struct wm8731_priv *wm8731; 577f0fba2adSLiam Girdwood int ret; 5785998102bSMark Brown 5795998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 5805998102bSMark Brown if (wm8731 == NULL) 5815998102bSMark Brown return -ENOMEM; 5825998102bSMark Brown 583f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_SPI; 584f0fba2adSLiam Girdwood spi_set_drvdata(spi, wm8731); 5855998102bSMark Brown 586f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&spi->dev, 587f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 588f0fba2adSLiam Girdwood if (ret < 0) 589f0fba2adSLiam Girdwood kfree(wm8731); 590f0fba2adSLiam Girdwood return ret; 5915998102bSMark Brown } 5925998102bSMark Brown 5935998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi) 5945998102bSMark Brown { 595f0fba2adSLiam Girdwood snd_soc_unregister_codec(&spi->dev); 596f0fba2adSLiam Girdwood kfree(spi_get_drvdata(spi)); 5975998102bSMark Brown return 0; 5985998102bSMark Brown } 5995998102bSMark Brown 6005998102bSMark Brown static struct spi_driver wm8731_spi_driver = { 6015998102bSMark Brown .driver = { 602f0fba2adSLiam Girdwood .name = "wm8731-codec", 6035998102bSMark Brown .owner = THIS_MODULE, 6045998102bSMark Brown }, 6055998102bSMark Brown .probe = wm8731_spi_probe, 6065998102bSMark Brown .remove = __devexit_p(wm8731_spi_remove), 6075998102bSMark Brown }; 608a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */ 609a8035c8fSMark Brown 610a8035c8fSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 611c6f29811SMark Brown static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, 612a8035c8fSMark Brown const struct i2c_device_id *id) 613a8035c8fSMark Brown { 6145998102bSMark Brown struct wm8731_priv *wm8731; 615f0fba2adSLiam Girdwood int ret; 616a8035c8fSMark Brown 6175998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 6185998102bSMark Brown if (wm8731 == NULL) 6195998102bSMark Brown return -ENOMEM; 6205998102bSMark Brown 6215998102bSMark Brown i2c_set_clientdata(i2c, wm8731); 622f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_I2C; 623a8035c8fSMark Brown 624f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&i2c->dev, 625f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 626f0fba2adSLiam Girdwood if (ret < 0) 627f0fba2adSLiam Girdwood kfree(wm8731); 628f0fba2adSLiam Girdwood return ret; 629a8035c8fSMark Brown } 630a8035c8fSMark Brown 631c6f29811SMark Brown static __devexit int wm8731_i2c_remove(struct i2c_client *client) 632a8035c8fSMark Brown { 633f0fba2adSLiam Girdwood snd_soc_unregister_codec(&client->dev); 634f0fba2adSLiam Girdwood kfree(i2c_get_clientdata(client)); 635a8035c8fSMark Brown return 0; 636a8035c8fSMark Brown } 637a8035c8fSMark Brown 638a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = { 639a8035c8fSMark Brown { "wm8731", 0 }, 640a8035c8fSMark Brown { } 641a8035c8fSMark Brown }; 642a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); 643a8035c8fSMark Brown 644a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = { 645a8035c8fSMark Brown .driver = { 646f0fba2adSLiam Girdwood .name = "wm8731-codec", 647a8035c8fSMark Brown .owner = THIS_MODULE, 648a8035c8fSMark Brown }, 649a8035c8fSMark Brown .probe = wm8731_i2c_probe, 650c6f29811SMark Brown .remove = __devexit_p(wm8731_i2c_remove), 651a8035c8fSMark Brown .id_table = wm8731_i2c_id, 652a8035c8fSMark Brown }; 653a8035c8fSMark Brown #endif 654a8035c8fSMark Brown 655c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void) 65664089b84SMark Brown { 657f0fba2adSLiam Girdwood int ret = 0; 6585998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6595998102bSMark Brown ret = i2c_add_driver(&wm8731_i2c_driver); 6605998102bSMark Brown if (ret != 0) { 6615998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", 6625998102bSMark Brown ret); 6635998102bSMark Brown } 6645998102bSMark Brown #endif 6655998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6665998102bSMark Brown ret = spi_register_driver(&wm8731_spi_driver); 6675998102bSMark Brown if (ret != 0) { 6685998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", 6695998102bSMark Brown ret); 6705998102bSMark Brown } 6715998102bSMark Brown #endif 672f0fba2adSLiam Girdwood return ret; 67364089b84SMark Brown } 67464089b84SMark Brown module_init(wm8731_modinit); 67564089b84SMark Brown 67664089b84SMark Brown static void __exit wm8731_exit(void) 67764089b84SMark Brown { 6785998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6795998102bSMark Brown i2c_del_driver(&wm8731_i2c_driver); 6805998102bSMark Brown #endif 6815998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6825998102bSMark Brown spi_unregister_driver(&wm8731_spi_driver); 6835998102bSMark Brown #endif 68464089b84SMark Brown } 68564089b84SMark Brown module_exit(wm8731_exit); 68664089b84SMark Brown 68740e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver"); 68840e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie"); 68940e0aa64SRichard Purdie MODULE_LICENSE("GPL"); 690