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 { 45f0fba2adSLiam Girdwood enum snd_soc_control_type control_type; 467dea7c01SMark Brown struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; 475998102bSMark Brown u16 reg_cache[WM8731_CACHEREGNUM]; 48b36d61d4SFrank Mandarino unsigned int sysclk; 499745e824SMark Brown int sysclk_type; 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[] = { 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 { 168a65f0568SMark Brown snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets, 169a65f0568SMark Brown ARRAY_SIZE(wm8731_dapm_widgets)); 17040e0aa64SRichard Purdie 171a65f0568SMark Brown snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); 17240e0aa64SRichard Purdie 17340e0aa64SRichard Purdie return 0; 17440e0aa64SRichard Purdie } 17540e0aa64SRichard Purdie 17640e0aa64SRichard Purdie struct _coeff_div { 17740e0aa64SRichard Purdie u32 mclk; 17840e0aa64SRichard Purdie u32 rate; 17940e0aa64SRichard Purdie u16 fs; 18040e0aa64SRichard Purdie u8 sr:4; 18140e0aa64SRichard Purdie u8 bosr:1; 18240e0aa64SRichard Purdie u8 usb:1; 18340e0aa64SRichard Purdie }; 18440e0aa64SRichard Purdie 18540e0aa64SRichard Purdie /* codec mclk clock divider coefficients */ 18640e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = { 18740e0aa64SRichard Purdie /* 48k */ 18840e0aa64SRichard Purdie {12288000, 48000, 256, 0x0, 0x0, 0x0}, 18940e0aa64SRichard Purdie {18432000, 48000, 384, 0x0, 0x1, 0x0}, 19040e0aa64SRichard Purdie {12000000, 48000, 250, 0x0, 0x0, 0x1}, 19140e0aa64SRichard Purdie 19240e0aa64SRichard Purdie /* 32k */ 19340e0aa64SRichard Purdie {12288000, 32000, 384, 0x6, 0x0, 0x0}, 19440e0aa64SRichard Purdie {18432000, 32000, 576, 0x6, 0x1, 0x0}, 195298a2c75SFrank Mandarino {12000000, 32000, 375, 0x6, 0x0, 0x1}, 19640e0aa64SRichard Purdie 19740e0aa64SRichard Purdie /* 8k */ 19840e0aa64SRichard Purdie {12288000, 8000, 1536, 0x3, 0x0, 0x0}, 19940e0aa64SRichard Purdie {18432000, 8000, 2304, 0x3, 0x1, 0x0}, 20040e0aa64SRichard Purdie {11289600, 8000, 1408, 0xb, 0x0, 0x0}, 20140e0aa64SRichard Purdie {16934400, 8000, 2112, 0xb, 0x1, 0x0}, 20240e0aa64SRichard Purdie {12000000, 8000, 1500, 0x3, 0x0, 0x1}, 20340e0aa64SRichard Purdie 20440e0aa64SRichard Purdie /* 96k */ 20540e0aa64SRichard Purdie {12288000, 96000, 128, 0x7, 0x0, 0x0}, 20640e0aa64SRichard Purdie {18432000, 96000, 192, 0x7, 0x1, 0x0}, 20740e0aa64SRichard Purdie {12000000, 96000, 125, 0x7, 0x0, 0x1}, 20840e0aa64SRichard Purdie 20940e0aa64SRichard Purdie /* 44.1k */ 21040e0aa64SRichard Purdie {11289600, 44100, 256, 0x8, 0x0, 0x0}, 21140e0aa64SRichard Purdie {16934400, 44100, 384, 0x8, 0x1, 0x0}, 21240e0aa64SRichard Purdie {12000000, 44100, 272, 0x8, 0x1, 0x1}, 21340e0aa64SRichard Purdie 21440e0aa64SRichard Purdie /* 88.2k */ 21540e0aa64SRichard Purdie {11289600, 88200, 128, 0xf, 0x0, 0x0}, 21640e0aa64SRichard Purdie {16934400, 88200, 192, 0xf, 0x1, 0x0}, 21740e0aa64SRichard Purdie {12000000, 88200, 136, 0xf, 0x1, 0x1}, 21840e0aa64SRichard Purdie }; 21940e0aa64SRichard Purdie 22040e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate) 22140e0aa64SRichard Purdie { 22240e0aa64SRichard Purdie int i; 22340e0aa64SRichard Purdie 22440e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 22540e0aa64SRichard Purdie if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 22640e0aa64SRichard Purdie return i; 22740e0aa64SRichard Purdie } 22840e0aa64SRichard Purdie return 0; 22940e0aa64SRichard Purdie } 23040e0aa64SRichard Purdie 231b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream, 232dee89c4dSMark Brown struct snd_pcm_hw_params *params, 233dee89c4dSMark Brown struct snd_soc_dai *dai) 23440e0aa64SRichard Purdie { 235f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 236b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 23717a52fd6SMark Brown u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; 238b36d61d4SFrank Mandarino int i = get_coeff(wm8731->sysclk, params_rate(params)); 239b36d61d4SFrank Mandarino u16 srate = (coeff_div[i].sr << 2) | 240b36d61d4SFrank Mandarino (coeff_div[i].bosr << 1) | coeff_div[i].usb; 24140e0aa64SRichard Purdie 24217a52fd6SMark Brown snd_soc_write(codec, WM8731_SRATE, srate); 24340e0aa64SRichard Purdie 244b36d61d4SFrank Mandarino /* bit size */ 245b36d61d4SFrank Mandarino switch (params_format(params)) { 246b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S16_LE: 247b36d61d4SFrank Mandarino break; 248b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S20_3LE: 249b36d61d4SFrank Mandarino iface |= 0x0004; 250b36d61d4SFrank Mandarino break; 251b36d61d4SFrank Mandarino case SNDRV_PCM_FORMAT_S24_LE: 252b36d61d4SFrank Mandarino iface |= 0x0008; 253b36d61d4SFrank Mandarino break; 254b36d61d4SFrank Mandarino } 255b36d61d4SFrank Mandarino 25617a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 257b36d61d4SFrank Mandarino return 0; 25840e0aa64SRichard Purdie } 25940e0aa64SRichard Purdie 260dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, 261dee89c4dSMark Brown struct snd_soc_dai *dai) 26240e0aa64SRichard Purdie { 263f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 26440e0aa64SRichard Purdie 26540e0aa64SRichard Purdie /* set active */ 26617a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0001); 267b36d61d4SFrank Mandarino 26840e0aa64SRichard Purdie return 0; 26940e0aa64SRichard Purdie } 27040e0aa64SRichard Purdie 271dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream, 272dee89c4dSMark Brown struct snd_soc_dai *dai) 27340e0aa64SRichard Purdie { 274f0fba2adSLiam Girdwood struct snd_soc_codec *codec = dai->codec; 27540e0aa64SRichard Purdie 27640e0aa64SRichard Purdie /* deactivate */ 27740e0aa64SRichard Purdie if (!codec->active) { 27840e0aa64SRichard Purdie udelay(50); 27917a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 28040e0aa64SRichard Purdie } 28140e0aa64SRichard Purdie } 28240e0aa64SRichard Purdie 283e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute) 28440e0aa64SRichard Purdie { 285b36d61d4SFrank Mandarino struct snd_soc_codec *codec = dai->codec; 28617a52fd6SMark Brown u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; 287b36d61d4SFrank Mandarino 28840e0aa64SRichard Purdie if (mute) 28917a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); 29040e0aa64SRichard Purdie else 29117a52fd6SMark Brown snd_soc_write(codec, WM8731_APDIGI, mute_reg); 29240e0aa64SRichard Purdie return 0; 29340e0aa64SRichard Purdie } 29440e0aa64SRichard Purdie 295e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, 296b36d61d4SFrank Mandarino int clk_id, unsigned int freq, int dir) 297b36d61d4SFrank Mandarino { 298b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 299b2c812e2SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 300b36d61d4SFrank Mandarino 3019745e824SMark Brown switch (clk_id) { 3029745e824SMark Brown case WM8731_SYSCLK_XTAL: 3039745e824SMark Brown case WM8731_SYSCLK_MCLK: 3049745e824SMark Brown wm8731->sysclk_type = clk_id; 3059745e824SMark Brown break; 3069745e824SMark Brown default: 3079745e824SMark Brown return -EINVAL; 3089745e824SMark Brown } 3099745e824SMark Brown 310b36d61d4SFrank Mandarino switch (freq) { 311b36d61d4SFrank Mandarino case 11289600: 312b36d61d4SFrank Mandarino case 12000000: 313b36d61d4SFrank Mandarino case 12288000: 314b36d61d4SFrank Mandarino case 16934400: 315b36d61d4SFrank Mandarino case 18432000: 316b36d61d4SFrank Mandarino wm8731->sysclk = freq; 3179745e824SMark Brown break; 3189745e824SMark Brown default: 319b36d61d4SFrank Mandarino return -EINVAL; 320b36d61d4SFrank Mandarino } 321b36d61d4SFrank Mandarino 3229745e824SMark Brown snd_soc_dapm_sync(codec); 3239745e824SMark Brown 3249745e824SMark Brown return 0; 3259745e824SMark Brown } 3269745e824SMark Brown 327b36d61d4SFrank Mandarino 328e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, 329b36d61d4SFrank Mandarino unsigned int fmt) 330b36d61d4SFrank Mandarino { 331b36d61d4SFrank Mandarino struct snd_soc_codec *codec = codec_dai->codec; 332b36d61d4SFrank Mandarino u16 iface = 0; 333b36d61d4SFrank Mandarino 334b36d61d4SFrank Mandarino /* set master/slave audio interface */ 335b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 336b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBM_CFM: 337b36d61d4SFrank Mandarino iface |= 0x0040; 338b36d61d4SFrank Mandarino break; 339b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_CBS_CFS: 340b36d61d4SFrank Mandarino break; 341b36d61d4SFrank Mandarino default: 342b36d61d4SFrank Mandarino return -EINVAL; 343b36d61d4SFrank Mandarino } 344b36d61d4SFrank Mandarino 345b36d61d4SFrank Mandarino /* interface format */ 346b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 347b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_I2S: 348b36d61d4SFrank Mandarino iface |= 0x0002; 349b36d61d4SFrank Mandarino break; 350b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_RIGHT_J: 351b36d61d4SFrank Mandarino break; 352b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_LEFT_J: 353b36d61d4SFrank Mandarino iface |= 0x0001; 354b36d61d4SFrank Mandarino break; 355b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_A: 356b36d61d4SFrank Mandarino iface |= 0x0003; 357b36d61d4SFrank Mandarino break; 358b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_DSP_B: 359b36d61d4SFrank Mandarino iface |= 0x0013; 360b36d61d4SFrank Mandarino break; 361b36d61d4SFrank Mandarino default: 362b36d61d4SFrank Mandarino return -EINVAL; 363b36d61d4SFrank Mandarino } 364b36d61d4SFrank Mandarino 365b36d61d4SFrank Mandarino /* clock inversion */ 366b36d61d4SFrank Mandarino switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 367b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_NF: 368b36d61d4SFrank Mandarino break; 369b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_IF: 370b36d61d4SFrank Mandarino iface |= 0x0090; 371b36d61d4SFrank Mandarino break; 372b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_IB_NF: 373b36d61d4SFrank Mandarino iface |= 0x0080; 374b36d61d4SFrank Mandarino break; 375b36d61d4SFrank Mandarino case SND_SOC_DAIFMT_NB_IF: 376b36d61d4SFrank Mandarino iface |= 0x0010; 377b36d61d4SFrank Mandarino break; 378b36d61d4SFrank Mandarino default: 379b36d61d4SFrank Mandarino return -EINVAL; 380b36d61d4SFrank Mandarino } 381b36d61d4SFrank Mandarino 382b36d61d4SFrank Mandarino /* set iface */ 38317a52fd6SMark Brown snd_soc_write(codec, WM8731_IFACE, iface); 384b36d61d4SFrank Mandarino return 0; 385b36d61d4SFrank Mandarino } 386b36d61d4SFrank Mandarino 3870be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec, 3880be9898aSMark Brown enum snd_soc_bias_level level) 38940e0aa64SRichard Purdie { 39006ae9988SMark Brown struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 39106ae9988SMark Brown int i, ret; 39206ae9988SMark Brown u8 data[2]; 39306ae9988SMark Brown u16 *cache = codec->reg_cache; 39422d22ee5SMark Brown u16 reg; 39540e0aa64SRichard Purdie 3960be9898aSMark Brown switch (level) { 3970be9898aSMark Brown case SND_SOC_BIAS_ON: 39840e0aa64SRichard Purdie break; 3990be9898aSMark Brown case SND_SOC_BIAS_PREPARE: 40040e0aa64SRichard Purdie break; 4010be9898aSMark Brown case SND_SOC_BIAS_STANDBY: 40206ae9988SMark Brown if (codec->bias_level == SND_SOC_BIAS_OFF) { 40306ae9988SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 40406ae9988SMark Brown wm8731->supplies); 40506ae9988SMark Brown if (ret != 0) 40606ae9988SMark Brown return ret; 40706ae9988SMark Brown 40806ae9988SMark Brown /* Sync reg_cache with the hardware */ 40906ae9988SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { 41006ae9988SMark Brown if (cache[i] == wm8731_reg[i]) 41106ae9988SMark Brown continue; 41206ae9988SMark Brown 41306ae9988SMark Brown data[0] = (i << 1) | ((cache[i] >> 8) 41406ae9988SMark Brown & 0x0001); 41506ae9988SMark Brown data[1] = cache[i] & 0x00ff; 41606ae9988SMark Brown codec->hw_write(codec->control_data, data, 2); 41706ae9988SMark Brown } 41806ae9988SMark Brown } 41906ae9988SMark Brown 42022d22ee5SMark Brown /* Clear PWROFF, gate CLKOUT, everything else as-is */ 42117a52fd6SMark Brown reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; 42217a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, reg | 0x0040); 42340e0aa64SRichard Purdie break; 4240be9898aSMark Brown case SND_SOC_BIAS_OFF: 42517a52fd6SMark Brown snd_soc_write(codec, WM8731_ACTIVE, 0x0); 42617a52fd6SMark Brown snd_soc_write(codec, WM8731_PWR, 0xffff); 42706ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), 42806ae9988SMark Brown wm8731->supplies); 42940e0aa64SRichard Purdie break; 43040e0aa64SRichard Purdie } 4310be9898aSMark Brown codec->bias_level = level; 43240e0aa64SRichard Purdie return 0; 43340e0aa64SRichard Purdie } 43440e0aa64SRichard Purdie 435e135443eSBill Gatliff #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 436b36d61d4SFrank Mandarino 437b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 438b36d61d4SFrank Mandarino SNDRV_PCM_FMTBIT_S24_LE) 439b36d61d4SFrank Mandarino 4406335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = { 4416335d055SEric Miao .prepare = wm8731_pcm_prepare, 4426335d055SEric Miao .hw_params = wm8731_hw_params, 4436335d055SEric Miao .shutdown = wm8731_shutdown, 4446335d055SEric Miao .digital_mute = wm8731_mute, 4456335d055SEric Miao .set_sysclk = wm8731_set_dai_sysclk, 4466335d055SEric Miao .set_fmt = wm8731_set_dai_fmt, 4476335d055SEric Miao }; 4486335d055SEric Miao 449f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8731_dai = { 450f0fba2adSLiam Girdwood .name = "wm8731-hifi", 45140e0aa64SRichard Purdie .playback = { 45240e0aa64SRichard Purdie .stream_name = "Playback", 45340e0aa64SRichard Purdie .channels_min = 1, 45440e0aa64SRichard Purdie .channels_max = 2, 455b36d61d4SFrank Mandarino .rates = WM8731_RATES, 456b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 45740e0aa64SRichard Purdie .capture = { 45840e0aa64SRichard Purdie .stream_name = "Capture", 45940e0aa64SRichard Purdie .channels_min = 1, 46040e0aa64SRichard Purdie .channels_max = 2, 461b36d61d4SFrank Mandarino .rates = WM8731_RATES, 462b36d61d4SFrank Mandarino .formats = WM8731_FORMATS,}, 4636335d055SEric Miao .ops = &wm8731_dai_ops, 4644934482dSMark Brown .symmetric_rates = 1, 46540e0aa64SRichard Purdie }; 46640e0aa64SRichard Purdie 467b3b50b3fSMark Brown #ifdef CONFIG_PM 468f0fba2adSLiam Girdwood static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state) 46940e0aa64SRichard Purdie { 4700be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 47106ae9988SMark Brown 47240e0aa64SRichard Purdie return 0; 47340e0aa64SRichard Purdie } 47440e0aa64SRichard Purdie 475f0fba2adSLiam Girdwood static int wm8731_resume(struct snd_soc_codec *codec) 47640e0aa64SRichard Purdie { 4770be9898aSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 4787dea7c01SMark Brown 47940e0aa64SRichard Purdie return 0; 48040e0aa64SRichard Purdie } 481b3b50b3fSMark Brown #else 482b3b50b3fSMark Brown #define wm8731_suspend NULL 483b3b50b3fSMark Brown #define wm8731_resume NULL 484b3b50b3fSMark Brown #endif 48540e0aa64SRichard Purdie 486f0fba2adSLiam Girdwood static int wm8731_probe(struct snd_soc_codec *codec) 48740e0aa64SRichard Purdie { 488f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 489f0fba2adSLiam Girdwood int ret = 0, i; 49040e0aa64SRichard Purdie 491f0fba2adSLiam Girdwood codec->bias_level = SND_SOC_BIAS_OFF, 49240e0aa64SRichard Purdie 493f0fba2adSLiam Girdwood ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type); 49417a52fd6SMark Brown if (ret < 0) { 49517a52fd6SMark Brown dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 496f0fba2adSLiam Girdwood return ret; 49717a52fd6SMark Brown } 49817a52fd6SMark Brown 4997dea7c01SMark Brown for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) 5007dea7c01SMark Brown wm8731->supplies[i].supply = wm8731_supply_names[i]; 5017dea7c01SMark Brown 5027dea7c01SMark Brown ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), 5037dea7c01SMark Brown wm8731->supplies); 5047dea7c01SMark Brown if (ret != 0) { 5057dea7c01SMark Brown dev_err(codec->dev, "Failed to request supplies: %d\n", ret); 506f0fba2adSLiam Girdwood return ret; 5077dea7c01SMark Brown } 5087dea7c01SMark Brown 5097dea7c01SMark Brown ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), 5107dea7c01SMark Brown wm8731->supplies); 5117dea7c01SMark Brown if (ret != 0) { 5127dea7c01SMark Brown dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); 5137dea7c01SMark Brown goto err_regulator_get; 5147dea7c01SMark Brown } 5157dea7c01SMark Brown 516519cf2dfSMark Brown ret = wm8731_reset(codec); 517519cf2dfSMark Brown if (ret < 0) { 518fe5422fcSMark Brown dev_err(codec->dev, "Failed to issue reset: %d\n", ret); 5197dea7c01SMark Brown goto err_regulator_enable; 520519cf2dfSMark Brown } 521519cf2dfSMark Brown 5225998102bSMark Brown wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 5235998102bSMark Brown 5245998102bSMark Brown /* Latch the update bits */ 52517a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); 52617a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); 52717a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); 52817a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); 5295998102bSMark Brown 530ce3bdaa8SMark Brown /* Disable bypass path by default */ 53117a52fd6SMark Brown snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0); 532ce3bdaa8SMark Brown 533f0fba2adSLiam Girdwood snd_soc_add_controls(codec, wm8731_snd_controls, 534f0fba2adSLiam Girdwood ARRAY_SIZE(wm8731_snd_controls)); 535f0fba2adSLiam Girdwood wm8731_add_widgets(codec); 5365998102bSMark Brown 53706ae9988SMark Brown /* Regulators will have been enabled by bias management */ 53806ae9988SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 53906ae9988SMark Brown 540a8035c8fSMark Brown return 0; 541fe5422fcSMark Brown 5427dea7c01SMark Brown err_regulator_enable: 5437dea7c01SMark Brown regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5447dea7c01SMark Brown err_regulator_get: 5457dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 546f0fba2adSLiam Girdwood 547fe5422fcSMark Brown kfree(wm8731); 548fe5422fcSMark Brown return ret; 549a8035c8fSMark Brown } 550a8035c8fSMark Brown 551f0fba2adSLiam Girdwood /* power down chip */ 552f0fba2adSLiam Girdwood static int wm8731_remove(struct snd_soc_codec *codec) 5535998102bSMark Brown { 554f0fba2adSLiam Girdwood struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); 555f0fba2adSLiam Girdwood 556f0fba2adSLiam Girdwood wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); 557f0fba2adSLiam Girdwood 558f0fba2adSLiam Girdwood regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 5597dea7c01SMark Brown regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); 560f0fba2adSLiam Girdwood 561f0fba2adSLiam Girdwood return 0; 5625998102bSMark Brown } 563a8035c8fSMark Brown 564f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { 565f0fba2adSLiam Girdwood .probe = wm8731_probe, 566f0fba2adSLiam Girdwood .remove = wm8731_remove, 567f0fba2adSLiam Girdwood .suspend = wm8731_suspend, 568f0fba2adSLiam Girdwood .resume = wm8731_resume, 569f0fba2adSLiam Girdwood .set_bias_level = wm8731_set_bias_level, 570*e5eec34cSDimitris Papastamos .reg_cache_size = ARRAY_SIZE(wm8731_reg), 571f0fba2adSLiam Girdwood .reg_word_size = sizeof(u16), 572f0fba2adSLiam Girdwood .reg_cache_default = wm8731_reg, 573f0fba2adSLiam Girdwood }; 574f0fba2adSLiam Girdwood 5755998102bSMark Brown #if defined(CONFIG_SPI_MASTER) 5765998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi) 5775998102bSMark Brown { 5785998102bSMark Brown struct wm8731_priv *wm8731; 579f0fba2adSLiam Girdwood int ret; 5805998102bSMark Brown 5815998102bSMark Brown wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); 5825998102bSMark Brown if (wm8731 == NULL) 5835998102bSMark Brown return -ENOMEM; 5845998102bSMark Brown 585f0fba2adSLiam Girdwood wm8731->control_type = SND_SOC_SPI; 586f0fba2adSLiam Girdwood spi_set_drvdata(spi, wm8731); 5875998102bSMark Brown 588f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&spi->dev, 589f0fba2adSLiam Girdwood &soc_codec_dev_wm8731, &wm8731_dai, 1); 590f0fba2adSLiam Girdwood if (ret < 0) 591f0fba2adSLiam Girdwood kfree(wm8731); 592f0fba2adSLiam Girdwood return ret; 5935998102bSMark Brown } 5945998102bSMark Brown 5955998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi) 5965998102bSMark Brown { 597f0fba2adSLiam Girdwood snd_soc_unregister_codec(&spi->dev); 598f0fba2adSLiam Girdwood kfree(spi_get_drvdata(spi)); 5995998102bSMark Brown return 0; 6005998102bSMark Brown } 6015998102bSMark Brown 6025998102bSMark Brown static struct spi_driver wm8731_spi_driver = { 6035998102bSMark Brown .driver = { 604f0fba2adSLiam Girdwood .name = "wm8731-codec", 6055998102bSMark Brown .bus = &spi_bus_type, 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