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/soc-dapm.h> 3040e0aa64SRichard Purdie #include <sound/initval.h> 31d00efa64SMark Brown #include <sound/tlv.h> 3240e0aa64SRichard Purdie 3340e0aa64SRichard Purdie #include "wm8731.h" 3440e0aa64SRichard Purdie 357dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4 367dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = { 377dea7c01SMark Brown "AVDD", 387dea7c01SMark Brown "HPVDD", 397dea7c01SMark Brown "DCVDD", 407dea7c01SMark Brown "DBVDD", 417dea7c01SMark Brown }; 427dea7c01SMark Brown 43b36d61d4SFrank Mandarino /* codec private data */ 44b36d61d4SFrank Mandarino struct wm8731_priv { 45*f0fba2adSLiam Girdwood enum snd_soc_control_type control_type; 46*f0fba2adSLiam Girdwood void *control_data; 477dea7c01SMark Brown struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; 485998102bSMark Brown u16 reg_cache[WM8731_CACHEREGNUM]; 49b36d61d4SFrank Mandarino unsigned int sysclk; 50b36d61d4SFrank Mandarino }; 51b36d61d4SFrank Mandarino 52a8035c8fSMark Brown 5340e0aa64SRichard Purdie /* 5440e0aa64SRichard Purdie * wm8731 register cache 5540e0aa64SRichard Purdie * We can't read the WM8731 register space when we are 5640e0aa64SRichard Purdie * using 2 wire for device control, so we cache them instead. 5740e0aa64SRichard Purdie * There is no point in caching the reset register 5840e0aa64SRichard Purdie */ 5940e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { 6040e0aa64SRichard Purdie 0x0097, 0x0097, 0x0079, 0x0079, 6140e0aa64SRichard Purdie 0x000a, 0x0008, 0x009f, 0x000a, 6240e0aa64SRichard Purdie 0x0000, 0x0000 6340e0aa64SRichard Purdie }; 6440e0aa64SRichard Purdie 6517a52fd6SMark Brown #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) 6640e0aa64SRichard Purdie 6740e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"}; 6840e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 6940e0aa64SRichard Purdie 7040e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = { 7140e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), 7240e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), 7340e0aa64SRichard Purdie }; 7440e0aa64SRichard Purdie 75d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); 76d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); 77d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 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 9040e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), 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[] = { 11440e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, 11540e0aa64SRichard Purdie &wm8731_output_mixer_controls[0], 11640e0aa64SRichard Purdie ARRAY_SIZE(wm8731_output_mixer_controls)), 11740e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), 11840e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"), 11940e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"), 12040e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"), 12140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"), 12240e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), 12340e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), 12440e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), 12540e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), 12640e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"), 12740e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"), 12840e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"), 12940e0aa64SRichard Purdie }; 13040e0aa64SRichard Purdie 131a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = { 13240e0aa64SRichard Purdie /* output mixer */ 13340e0aa64SRichard Purdie {"Output Mixer", "Line Bypass Switch", "Line Input"}, 13440e0aa64SRichard Purdie {"Output Mixer", "HiFi Playback Switch", "DAC"}, 13540e0aa64SRichard Purdie {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, 13640e0aa64SRichard Purdie 13740e0aa64SRichard Purdie /* outputs */ 13840e0aa64SRichard Purdie {"RHPOUT", NULL, "Output Mixer"}, 13940e0aa64SRichard Purdie {"ROUT", NULL, "Output Mixer"}, 14040e0aa64SRichard Purdie {"LHPOUT", NULL, "Output Mixer"}, 14140e0aa64SRichard Purdie {"LOUT", NULL, "Output Mixer"}, 14240e0aa64SRichard Purdie 14340e0aa64SRichard Purdie /* input mux */ 14440e0aa64SRichard Purdie {"Input Mux", "Line In", "Line Input"}, 14540e0aa64SRichard Purdie {"Input Mux", "Mic", "Mic Bias"}, 14640e0aa64SRichard Purdie {"ADC", NULL, "Input Mux"}, 14740e0aa64SRichard Purdie 14840e0aa64SRichard Purdie /* inputs */ 14940e0aa64SRichard Purdie {"Line Input", NULL, "LLINEIN"}, 15040e0aa64SRichard Purdie {"Line Input", NULL, "RLINEIN"}, 15140e0aa64SRichard Purdie {"Mic Bias", NULL, "MICIN"}, 15240e0aa64SRichard Purdie }; 15340e0aa64SRichard Purdie 15440e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec) 15540e0aa64SRichard Purdie { 156a65f0568SMark Brown snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets, 157a65f0568SMark Brown ARRAY_SIZE(wm8731_dapm_widgets)); 15840e0aa64SRichard Purdie 159a65f0568SMark Brown snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); 16040e0aa64SRichard Purdie 16140e0aa64SRichard Purdie return 0; 16240e0aa64SRichard Purdie } 16340e0aa64SRichard Purdie 16440e0aa64SRichard Purdie struct _coeff_div { 16540e0aa64SRichard Purdie u32 mclk; 16640e0aa64SRichard Purdie u32 rate; 16740e0aa64SRichard Purdie u16 fs; 16840e0aa64SRichard Purdie u8 sr:4; 16940e0aa64SRichard Purdie u8 bosr:1; 17040e0aa64SRichard Purdie u8 usb:1; 17140e0aa64SRichard Purdie }; 17240e0aa64SRichard Purdie 17340e0aa64SRichard Purdie /* codec mclk clock divider coefficients */ 17440e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = { 17540e0aa64SRichard Purdie /* 48k */ 17640e0aa64SRichard Purdie {12288000, 48000, 256, 0x0, 0x0, 0x0}, 17740e0aa64SRichard Purdie {18432000, 48000, 384, 0x0, 0x1, 0x0}, 17840e0aa64SRichard Purdie {12000000, 48000, 250, 0x0, 0x0, 0x1}, 17940e0aa64SRichard Purdie 18040e0aa64SRichard Purdie /* 32k */ 18140e0aa64SRichard Purdie {12288000, 32000, 384, 0x6, 0x0, 0x0}, 18240e0aa64SRichard Purdie {18432000, 32000, 576, 0x6, 0x1, 0x0}, 183298a2c75SFrank Mandarino {12000000, 32000, 375, 0x6, 0x0, 0x1}, 18440e0aa64SRichard Purdie 18540e0aa64SRichard Purdie /* 8k */ 18640e0aa64SRichard Purdie {12288000, 8000, 1536, 0x3, 0x0, 0x0}, 18740e0aa64SRichard Purdie {18432000, 8000, 2304, 0x3, 0x1, 0x0}, 18840e0aa64SRichard Purdie {11289600, 8000, 1408, 0xb, 0x0, 0x0}, 18940e0aa64SRichard Purdie {16934400, 8000, 2112, 0xb, 0x1, 0x0}, 19040e0aa64SRichard Purdie {12000000, 8000, 1500, 0x3, 0x0, 0x1}, 19140e0aa64SRichard Purdie 19240e0aa64SRichard Purdie /* 96k */ 19340e0aa64SRichard Purdie {12288000, 96000, 128, 0x7, 0x0, 0x0}, 19440e0aa64SRichard Purdie {18432000, 96000, 192, 0x7, 0x1, 0x0}, 19540e0aa64SRichard Purdie {12000000, 96000, 125, 0x7, 0x0, 0x1}, 19640e0aa64SRichard Purdie 19740e0aa64SRichard Purdie /* 44.1k */ 19840e0aa64SRichard Purdie {11289600, 44100, 256, 0x8, 0x0, 0x0}, 19940e0aa64SRichard Purdie {16934400, 44100, 384, 0x8, 0x1, 0x0}, 20040e0aa64SRichard Purdie {12000000, 44100, 272, 0x8, 0x1, 0x1}, 20140e0aa64SRichard Purdie 20240e0aa64SRichard Purdie /* 88.2k */ 20340e0aa64SRichard Purdie {11289600, 88200, 128, 0xf, 0x0, 0x0}, 20440e0aa64SRichard Purdie {16934400, 88200, 192, 0xf, 0x1, 0x0}, 20540e0aa64SRichard Purdie {12000000, 88200, 136, 0xf, 0x1, 0x1}, 20640e0aa64SRichard Purdie }; 20740e0aa64SRichard Purdie 20840e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate) 20940e0aa64SRichard Purdie { 21040e0aa64SRichard Purdie int i; 21140e0aa64SRichard Purdie 21240e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 21340e0aa64SRichard Purdie if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 21440e0aa64SRichard Purdie return i; 21540e0aa64SRichard Purdie } 21640e0aa64SRichard Purdie return 0; 21740e0aa64SRichard Purdie } 21840e0aa64SRichard Purdie 219b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream, 220dee89c4dSMark Brown struct snd_pcm_hw_params *params, 221dee89c4dSMark Brown struct snd_soc_dai *dai) 22240e0aa64SRichard Purdie { 223*f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 224b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 22517a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; 226b36d61d4SFrank Mandarino int i = get_coeff(wm8731->sysclk, params_rate(params)); 227b36d61d4SFrank Mandarino u16 srate = (coeff_div[i].sr << 2) | 228b36d61d4SFrank Mandarino (coeff_div[i].bosr << 1) | coeff_div[i].usb; 22940e0aa64SRichard Purdie 23017a52fd6SMark Brown snd_soc_write(codec, WM8731_SRATE, srate); 23140e0aa64SRichard Purdie 232b36d61d4SFrank Mandarino /* bit size */ 233b36d61d4SFrank Mandarino switch (params_format(params)) { 234b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S16_LE: 235b36d61d4SFrank Mandarino break; 236b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S20_3LE: 237b36d61d4SFrank Mandarino iface |= 0x0004; 238b36d61d4SFrank Mandarino break; 239b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S24_LE: 240b36d61d4SFrank Mandarino iface |= 0x0008; 241b36d61d4SFrank Mandarino break; 242b36d61d4SFrank Mandarino } 243b36d61d4SFrank Mandarino 24417a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 245b36d61d4SFrank Mandarino return 0; 24640e0aa64SRichard Purdie } 24740e0aa64SRichard Purdie 248dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, 249dee89c4dSMark Brown struct snd_soc_dai *dai) 25040e0aa64SRichard Purdie { 251*f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 25240e0aa64SRichard Purdie 25340e0aa64SRichard Purdie /* set active */ 25417a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0001); 255b36d61d4SFrank Mandarino 25640e0aa64SRichard Purdie return 0; 25740e0aa64SRichard Purdie } 25840e0aa64SRichard Purdie 259dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream, 260dee89c4dSMark Brown struct snd_soc_dai *dai) 26140e0aa64SRichard Purdie { 262*f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 26340e0aa64SRichard Purdie 26440e0aa64SRichard Purdie /* deactivate */ 26540e0aa64SRichard Purdie if (!codec->active) { 26640e0aa64SRichard Purdie udelay(50); 26717a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 26840e0aa64SRichard Purdie } 26940e0aa64SRichard Purdie } 27040e0aa64SRichard Purdie 271e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute) 27240e0aa64SRichard Purdie { 273b36d61d4SFrank Mandarino struct snd_soc_codec *codec = dai->codec; 27417a52fd6SMark Brown u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; 275b36d61d4SFrank Mandarino 27640e0aa64SRichard Purdie if (mute) 27717a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); 27840e0aa64SRichard Purdie else 27917a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg); 28040e0aa64SRichard Purdie return 0; 28140e0aa64SRichard Purdie } 28240e0aa64SRichard Purdie 283e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, 284b36d61d4SFrank Mandarino int clk_id, unsigned int freq, int dir) 285b36d61d4SFrank Mandarino { 286b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 287b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 288b36d61d4SFrank Mandarino 289b36d61d4SFrank Mandarino switch (freq) { 290b36d61d4SFrank Mandarino case 11289600: 291b36d61d4SFrank Mandarino case 12000000: 292b36d61d4SFrank Mandarino case 12288000: 293b36d61d4SFrank Mandarino case 16934400: 294b36d61d4SFrank Mandarino case 18432000: 295b36d61d4SFrank Mandarino wm8731->sysclk = freq; 296b36d61d4SFrank Mandarino return 0; 297b36d61d4SFrank Mandarino } 298b36d61d4SFrank Mandarino return -EINVAL; 299b36d61d4SFrank Mandarino } 300b36d61d4SFrank Mandarino 301b36d61d4SFrank Mandarino 302e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, 303b36d61d4SFrank Mandarino unsigned int fmt) 304b36d61d4SFrank Mandarino { 305b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 306b36d61d4SFrank Mandarino u16 iface = 0; 307b36d61d4SFrank Mandarino 308b36d61d4SFrank Mandarino /* set master/slave audio interface */ 309b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 310b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBM_CFM: 311b36d61d4SFrank Mandarino iface |= 0x0040; 312b36d61d4SFrank Mandarino break; 313b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBS_CFS: 314b36d61d4SFrank Mandarino break; 315b36d61d4SFrank Mandarino default: 316b36d61d4SFrank Mandarino return -EINVAL; 317b36d61d4SFrank Mandarino } 318b36d61d4SFrank Mandarino 319b36d61d4SFrank Mandarino /* interface format */ 320b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 321b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_I2S: 322b36d61d4SFrank Mandarino iface |= 0x0002; 323b36d61d4SFrank Mandarino break; 324b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_RIGHT_J: 325b36d61d4SFrank Mandarino break; 326b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_LEFT_J: 327b36d61d4SFrank Mandarino iface |= 0x0001; 328b36d61d4SFrank Mandarino break; 329b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_A: 330b36d61d4SFrank Mandarino iface |= 0x0003; 331b36d61d4SFrank Mandarino break; 332b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_B: 333b36d61d4SFrank Mandarino iface |= 0x0013; 334b36d61d4SFrank Mandarino break; 335b36d61d4SFrank Mandarino default: 336b36d61d4SFrank Mandarino return -EINVAL; 337b36d61d4SFrank Mandarino } 338b36d61d4SFrank Mandarino 339b36d61d4SFrank Mandarino /* clock inversion */ 340b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 341b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_NF: 342b36d61d4SFrank Mandarino break; 343b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_IF: 344b36d61d4SFrank Mandarino iface |= 0x0090; 345b36d61d4SFrank Mandarino break; 346b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_NF: 347b36d61d4SFrank Mandarino iface |= 0x0080; 348b36d61d4SFrank Mandarino break; 349b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_IF: 350b36d61d4SFrank Mandarino iface |= 0x0010; 351b36d61d4SFrank Mandarino break; 352b36d61d4SFrank Mandarino default: 353b36d61d4SFrank Mandarino return -EINVAL; 354b36d61d4SFrank Mandarino } 355b36d61d4SFrank Mandarino 356b36d61d4SFrank Mandarino /* set iface */ 35717a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 358b36d61d4SFrank Mandarino return 0; 359b36d61d4SFrank Mandarino } 360b36d61d4SFrank Mandarino 3610be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec, 3620be9898aSMark Brown enum snd_soc_bias_level level) 36340e0aa64SRichard Purdie { 36406ae9988SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 36506ae9988SMark Brown int i, ret; 36606ae9988SMark Brown u8 data[2]; 36706ae9988SMark Brown u16 *cache = codec->reg_cache; 36822d22ee5SMark Brown u16 reg; 36940e0aa64SRichard Purdie 3700be9898aSMark Brown switch (level) { 3710be9898aSMark Brown case SND_SOC_BIAS_ON: 37240e0aa64SRichard Purdie break; 3730be9898aSMark Brown case SND_SOC_BIAS_PREPARE: 37440e0aa64SRichard Purdie break; 3750be9898aSMark Brown case SND_SOC_BIAS_STANDBY: 37606ae9988SMark Brown if (codec->bias_level == SND_SOC_BIAS_OFF) { 37706ae9988SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 37806ae9988SMark Brown wm8731->supplies); 37906ae9988SMark Brown if (ret != 0) 38006ae9988SMark Brown return ret; 38106ae9988SMark Brown 38206ae9988SMark Brown /* Sync reg_cache with the hardware */ 38306ae9988SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { 38406ae9988SMark Brown if (cache[i] == wm8731_reg[i]) 38506ae9988SMark Brown continue; 38606ae9988SMark Brown 38706ae9988SMark Brown data[0] = (i << 1) | ((cache[i] >> 8) 38806ae9988SMark Brown & 0x0001); 38906ae9988SMark Brown data[1] = cache[i] & 0x00ff; 39006ae9988SMark Brown codec->hw_write(codec->control_data, data, 2); 39106ae9988SMark Brown } 39206ae9988SMark Brown } 39306ae9988SMark Brown 39422d22ee5SMark Brown /* Clear PWROFF, gate CLKOUT, everything else as-is */ 39517a52fd6SMark Brown reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; 39617a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, reg | 0x0040); 39740e0aa64SRichard Purdie break; 3980be9898aSMark Brown case SND_SOC_BIAS_OFF: 39917a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 40017a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, 0xffff); 40106ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), 40206ae9988SMark Brown wm8731->supplies); 40340e0aa64SRichard Purdie break; 40440e0aa64SRichard Purdie } 4050be9898aSMark Brown codec->bias_level = level; 40640e0aa64SRichard Purdie return 0; 40740e0aa64SRichard Purdie } 40840e0aa64SRichard Purdie 409e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 410b36d61d4SFrank Mandarino 411b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 412b36d61d4SFrank Mandarino SNDRV_PCM_FMTBIT_S24_LE) 413b36d61d4SFrank Mandarino 4146335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = { 4156335d055SEric Miao .prepare = wm8731_pcm_prepare, 4166335d055SEric Miao .hw_params = wm8731_hw_params, 4176335d055SEric Miao .shutdown = wm8731_shutdown, 4186335d055SEric Miao .digital_mute = wm8731_mute, 4196335d055SEric Miao .set_sysclk = wm8731_set_dai_sysclk, 4206335d055SEric Miao .set_fmt = wm8731_set_dai_fmt, 4216335d055SEric Miao }; 4226335d055SEric Miao 423*f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = { 424*f0fba2adSLiam Girdwood .name = "wm8731-hifi", 42540e0aa64SRichard Purdie .playback = { 42640e0aa64SRichard Purdie .stream_name = "Playback", 42740e0aa64SRichard Purdie .channels_min = 1, 42840e0aa64SRichard Purdie .channels_max = 2, 429b36d61d4SFrank Mandarino .rates = WM8731_RATES, 430b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 43140e0aa64SRichard Purdie .capture = { 43240e0aa64SRichard Purdie .stream_name = "Capture", 43340e0aa64SRichard Purdie .channels_min = 1, 43440e0aa64SRichard Purdie .channels_max = 2, 435b36d61d4SFrank Mandarino .rates = WM8731_RATES, 436b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 4376335d055SEric Miao .ops = &wm8731_dai_ops, 4384934482dSMark Brown .symmetric_rates = 1, 43940e0aa64SRichard Purdie }; 44040e0aa64SRichard Purdie 441b3b50b3fSMark Brown #ifdef CONFIG_PM 442*f0fba2adSLiam Girdwood static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state) 44340e0aa64SRichard Purdie { 4440be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 44506ae9988SMark Brown 44640e0aa64SRichard Purdie return 0; 44740e0aa64SRichard Purdie } 44840e0aa64SRichard Purdie 449*f0fba2adSLiam Girdwood static int wm8731_resume(struct snd_soc_codec *codec) 45040e0aa64SRichard Purdie { 4510be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 4527dea7c01SMark Brown 45340e0aa64SRichard Purdie return 0; 45440e0aa64SRichard Purdie } 455b3b50b3fSMark Brown #else 456b3b50b3fSMark Brown #define wm8731_suspend NULL 457b3b50b3fSMark Brown #define wm8731_resume NULL 458b3b50b3fSMark Brown #endif 45940e0aa64SRichard Purdie 460*f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec) 46140e0aa64SRichard Purdie { 462*f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 463*f0fba2adSLiam Girdwood int ret = 0, i; 46440e0aa64SRichard Purdie 465*f0fba2adSLiam Girdwood codec->bias_level = SND_SOC_BIAS_OFF, 466*f0fba2adSLiam Girdwood codec->control_data = wm8731->control_data; 46740e0aa64SRichard Purdie 468*f0fba2adSLiam Girdwood ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type); 46917a52fd6SMark Brown if (ret < 0) { 47017a52fd6SMark Brown dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 471*f0fba2adSLiam Girdwood return ret; 47217a52fd6SMark Brown } 47317a52fd6SMark Brown 4747dea7c01SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) 4757dea7c01SMark Brown wm8731->supplies[i].supply = wm8731_supply_names[i]; 4767dea7c01SMark Brown 4777dea7c01SMark Brown ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), 4787dea7c01SMark Brown wm8731->supplies); 4797dea7c01SMark Brown if (ret != 0) { 4807dea7c01SMark Brown dev_err(codec->dev, "Failed to request supplies: %d\n", ret); 481*f0fba2adSLiam Girdwood return ret; 4827dea7c01SMark Brown } 4837dea7c01SMark Brown 4847dea7c01SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 4857dea7c01SMark Brown wm8731->supplies); 4867dea7c01SMark Brown if (ret != 0) { 4877dea7c01SMark Brown dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); 4887dea7c01SMark Brown goto err_regulator_get; 4897dea7c01SMark Brown } 4907dea7c01SMark Brown 491519cf2dfSMark Brown ret = wm8731_reset(codec); 492519cf2dfSMark Brown if (ret < 0) { 493fe5422fcSMark Brown dev_err(codec->dev, "Failed to issue reset: %d\n", ret); 4947dea7c01SMark Brown goto err_regulator_enable; 495519cf2dfSMark Brown } 496519cf2dfSMark Brown 4975998102bSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 4985998102bSMark Brown 4995998102bSMark Brown /* Latch the update bits */ 50017a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); 50117a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); 50217a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); 50317a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); 5045998102bSMark Brown 505ce3bdaa8SMark Brown /* Disable bypass path by default */ 50617a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0); 507ce3bdaa8SMark Brown 508*f0fba2adSLiam Girdwood snd_soc_add_controls(codec, wm8731_snd_controls, 509*f0fba2adSLiam Girdwood ARRAY_SIZE(wm8731_snd_controls)); 510*f0fba2adSLiam Girdwood wm8731_add_widgets(codec); 5115998102bSMark Brown 51206ae9988SMark Brown /* Regulators will have been enabled by bias management */ 51306ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 51406ae9988SMark Brown 515a8035c8fSMark Brown return 0; 516fe5422fcSMark Brown 5177dea7c01SMark Brown err_regulator_enable: 5187dea7c01SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5197dea7c01SMark Brown err_regulator_get: 5207dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 521*f0fba2adSLiam Girdwood 522fe5422fcSMark Brown kfree(wm8731); 523fe5422fcSMark Brown return ret; 524a8035c8fSMark Brown } 525a8035c8fSMark Brown 526*f0fba2adSLiam Girdwood /* power down chip */ 527*f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec) 5285998102bSMark Brown { 529*f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 530*f0fba2adSLiam Girdwood 531*f0fba2adSLiam Girdwood wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 532*f0fba2adSLiam Girdwood 533*f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5347dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 535*f0fba2adSLiam Girdwood 536*f0fba2adSLiam Girdwood return 0; 5375998102bSMark Brown } 538a8035c8fSMark Brown 539*f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { 540*f0fba2adSLiam Girdwood .probe = wm8731_probe, 541*f0fba2adSLiam Girdwood .remove = wm8731_remove, 542*f0fba2adSLiam Girdwood .suspend = wm8731_suspend, 543*f0fba2adSLiam Girdwood .resume = wm8731_resume, 544*f0fba2adSLiam Girdwood .set_bias_level = wm8731_set_bias_level, 545*f0fba2adSLiam Girdwood .reg_cache_size = sizeof(wm8731_reg), 546*f0fba2adSLiam Girdwood .reg_word_size = sizeof(u16), 547*f0fba2adSLiam Girdwood .reg_cache_default = wm8731_reg, 548*f0fba2adSLiam Girdwood }; 549*f0fba2adSLiam Girdwood 5505998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 5515998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi) 5525998102bSMark Brown { 5535998102bSMark Brown struct wm8731_priv *wm8731; 554*f0fba2adSLiam Girdwood int ret; 5555998102bSMark Brown 5565998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 5575998102bSMark Brown if (wm8731 == NULL) 5585998102bSMark Brown return -ENOMEM; 5595998102bSMark Brown 560*f0fba2adSLiam Girdwood wm8731->control_data = spi; 561*f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_SPI; 562*f0fba2adSLiam Girdwood spi_set_drvdata(spi, wm8731); 5635998102bSMark Brown 564*f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&spi->dev, 565*f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 566*f0fba2adSLiam Girdwood if (ret < 0) 567*f0fba2adSLiam Girdwood kfree(wm8731); 568*f0fba2adSLiam Girdwood return ret; 5695998102bSMark Brown } 5705998102bSMark Brown 5715998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi) 5725998102bSMark Brown { 573*f0fba2adSLiam Girdwood snd_soc_unregister_codec(&spi->dev); 574*f0fba2adSLiam Girdwood kfree(spi_get_drvdata(spi)); 5755998102bSMark Brown return 0; 5765998102bSMark Brown } 5775998102bSMark Brown 5785998102bSMark Brown static struct spi_driver wm8731_spi_driver = { 5795998102bSMark Brown .driver = { 580*f0fba2adSLiam Girdwood .name = "wm8731-codec", 5815998102bSMark Brown .bus = &spi_bus_type, 5825998102bSMark Brown .owner = THIS_MODULE, 5835998102bSMark Brown }, 5845998102bSMark Brown .probe = wm8731_spi_probe, 5855998102bSMark Brown .remove = __devexit_p(wm8731_spi_remove), 5865998102bSMark Brown }; 587a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */ 588a8035c8fSMark Brown 589a8035c8fSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 590c6f29811SMark Brown static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, 591a8035c8fSMark Brown const struct i2c_device_id *id) 592a8035c8fSMark Brown { 5935998102bSMark Brown struct wm8731_priv *wm8731; 594*f0fba2adSLiam Girdwood int ret; 595a8035c8fSMark Brown 5965998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 5975998102bSMark Brown if (wm8731 == NULL) 5985998102bSMark Brown return -ENOMEM; 5995998102bSMark Brown 6005998102bSMark Brown i2c_set_clientdata(i2c, wm8731); 601*f0fba2adSLiam Girdwood wm8731->control_data = i2c; 602*f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_I2C; 603a8035c8fSMark Brown 604*f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&i2c->dev, 605*f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 606*f0fba2adSLiam Girdwood if (ret < 0) 607*f0fba2adSLiam Girdwood kfree(wm8731); 608*f0fba2adSLiam Girdwood return ret; 609a8035c8fSMark Brown } 610a8035c8fSMark Brown 611c6f29811SMark Brown static __devexit int wm8731_i2c_remove(struct i2c_client *client) 612a8035c8fSMark Brown { 613*f0fba2adSLiam Girdwood snd_soc_unregister_codec(&client->dev); 614*f0fba2adSLiam Girdwood kfree(i2c_get_clientdata(client)); 615a8035c8fSMark Brown return 0; 616a8035c8fSMark Brown } 617a8035c8fSMark Brown 618a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = { 619a8035c8fSMark Brown { "wm8731", 0 }, 620a8035c8fSMark Brown { } 621a8035c8fSMark Brown }; 622a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); 623a8035c8fSMark Brown 624a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = { 625a8035c8fSMark Brown .driver = { 626*f0fba2adSLiam Girdwood .name = "wm8731-codec", 627a8035c8fSMark Brown .owner = THIS_MODULE, 628a8035c8fSMark Brown }, 629a8035c8fSMark Brown .probe = wm8731_i2c_probe, 630c6f29811SMark Brown .remove = __devexit_p(wm8731_i2c_remove), 631a8035c8fSMark Brown .id_table = wm8731_i2c_id, 632a8035c8fSMark Brown }; 633a8035c8fSMark Brown #endif 634a8035c8fSMark Brown 635c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void) 63664089b84SMark Brown { 637*f0fba2adSLiam Girdwood int ret = 0; 6385998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6395998102bSMark Brown ret = i2c_add_driver(&wm8731_i2c_driver); 6405998102bSMark Brown if (ret != 0) { 6415998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", 6425998102bSMark Brown ret); 6435998102bSMark Brown } 6445998102bSMark Brown #endif 6455998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6465998102bSMark Brown ret = spi_register_driver(&wm8731_spi_driver); 6475998102bSMark Brown if (ret != 0) { 6485998102bSMark Brown printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", 6495998102bSMark Brown ret); 6505998102bSMark Brown } 6515998102bSMark Brown #endif 652*f0fba2adSLiam Girdwood return ret; 65364089b84SMark Brown } 65464089b84SMark Brown module_init(wm8731_modinit); 65564089b84SMark Brown 65664089b84SMark Brown static void __exit wm8731_exit(void) 65764089b84SMark Brown { 6585998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 6595998102bSMark Brown i2c_del_driver(&wm8731_i2c_driver); 6605998102bSMark Brown #endif 6615998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 6625998102bSMark Brown spi_unregister_driver(&wm8731_spi_driver); 6635998102bSMark Brown #endif 66464089b84SMark Brown } 66564089b84SMark Brown module_exit(wm8731_exit); 66664089b84SMark Brown 66740e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver"); 66840e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie"); 66940e0aa64SRichard Purdie MODULE_LICENSE("GPL"); 670