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> 2140e0aa64SRichard Purdie #include <linux/platform_device.h> 2240e0aa64SRichard Purdie #include <sound/driver.h> 2340e0aa64SRichard Purdie #include <sound/core.h> 2440e0aa64SRichard Purdie #include <sound/pcm.h> 2540e0aa64SRichard Purdie #include <sound/pcm_params.h> 2640e0aa64SRichard Purdie #include <sound/soc.h> 2740e0aa64SRichard Purdie #include <sound/soc-dapm.h> 2840e0aa64SRichard Purdie #include <sound/initval.h> 2940e0aa64SRichard Purdie 3040e0aa64SRichard Purdie #include "wm8731.h" 3140e0aa64SRichard Purdie 3240e0aa64SRichard Purdie #define AUDIO_NAME "wm8731" 3340e0aa64SRichard Purdie #define WM8731_VERSION "0.12" 3440e0aa64SRichard Purdie 3540e0aa64SRichard Purdie /* 3640e0aa64SRichard Purdie * Debug 3740e0aa64SRichard Purdie */ 3840e0aa64SRichard Purdie 3940e0aa64SRichard Purdie #define WM8731_DEBUG 0 4040e0aa64SRichard Purdie 4140e0aa64SRichard Purdie #ifdef WM8731_DEBUG 4240e0aa64SRichard Purdie #define dbg(format, arg...) \ 4340e0aa64SRichard Purdie printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg) 4440e0aa64SRichard Purdie #else 4540e0aa64SRichard Purdie #define dbg(format, arg...) do {} while (0) 4640e0aa64SRichard Purdie #endif 4740e0aa64SRichard Purdie #define err(format, arg...) \ 4840e0aa64SRichard Purdie printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg) 4940e0aa64SRichard Purdie #define info(format, arg...) \ 5040e0aa64SRichard Purdie printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg) 5140e0aa64SRichard Purdie #define warn(format, arg...) \ 5240e0aa64SRichard Purdie printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg) 5340e0aa64SRichard Purdie 5440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731; 5540e0aa64SRichard Purdie 5640e0aa64SRichard Purdie /* 5740e0aa64SRichard Purdie * wm8731 register cache 5840e0aa64SRichard Purdie * We can't read the WM8731 register space when we are 5940e0aa64SRichard Purdie * using 2 wire for device control, so we cache them instead. 6040e0aa64SRichard Purdie * There is no point in caching the reset register 6140e0aa64SRichard Purdie */ 6240e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { 6340e0aa64SRichard Purdie 0x0097, 0x0097, 0x0079, 0x0079, 6440e0aa64SRichard Purdie 0x000a, 0x0008, 0x009f, 0x000a, 6540e0aa64SRichard Purdie 0x0000, 0x0000 6640e0aa64SRichard Purdie }; 6740e0aa64SRichard Purdie 6840e0aa64SRichard Purdie #define WM8731_DAIFMT \ 6940e0aa64SRichard Purdie (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_RIGHT_J | \ 7040e0aa64SRichard Purdie SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_IB_NF | \ 7140e0aa64SRichard Purdie SND_SOC_DAIFMT_IB_IF) 7240e0aa64SRichard Purdie 7340e0aa64SRichard Purdie #define WM8731_DIR \ 7440e0aa64SRichard Purdie (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE) 7540e0aa64SRichard Purdie 7640e0aa64SRichard Purdie #define WM8731_RATES \ 7740e0aa64SRichard Purdie (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \ 7840e0aa64SRichard Purdie SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ 7940e0aa64SRichard Purdie SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) 8040e0aa64SRichard Purdie 8140e0aa64SRichard Purdie #define WM8731_HIFI_BITS \ 8240e0aa64SRichard Purdie (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 8340e0aa64SRichard Purdie SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 8440e0aa64SRichard Purdie 8540e0aa64SRichard Purdie static struct snd_soc_dai_mode wm8731_modes[] = { 8640e0aa64SRichard Purdie /* codec frame and clock master modes */ 8740e0aa64SRichard Purdie /* 8k */ 88527541f9SLiam Girdwood { 89527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 90527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 91527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_8000, 92527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 93a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 94527541f9SLiam Girdwood .fs = 1536, 95a71a468aSLiam Girdwood .bfs = 64, 96527541f9SLiam Girdwood }, 97527541f9SLiam Girdwood { 98527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 99527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 100527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_8000, 101527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 102a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 103527541f9SLiam Girdwood .fs = 2304, 104a71a468aSLiam Girdwood .bfs = 64, 105527541f9SLiam Girdwood }, 106527541f9SLiam Girdwood { 107527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 108527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 109527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_8000, 110527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 111a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 112527541f9SLiam Girdwood .fs = 1408, 113a71a468aSLiam Girdwood .bfs = 64, 114527541f9SLiam Girdwood }, 115527541f9SLiam Girdwood { 116527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 117527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 118527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_8000, 119527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 120a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 121527541f9SLiam Girdwood .fs = 2112, 122a71a468aSLiam Girdwood .bfs = 64, 123527541f9SLiam Girdwood }, 12440e0aa64SRichard Purdie 12540e0aa64SRichard Purdie /* 32k */ 126527541f9SLiam Girdwood { 127527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 128527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 129527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_32000, 130527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 131a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 132527541f9SLiam Girdwood .fs = 384, 133a71a468aSLiam Girdwood .bfs = 64, 134527541f9SLiam Girdwood }, 135527541f9SLiam Girdwood { 136527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 137527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 138527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_32000, 139527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 140a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 141527541f9SLiam Girdwood .fs = 576, 142a71a468aSLiam Girdwood .bfs = 64, 143527541f9SLiam Girdwood }, 14440e0aa64SRichard Purdie 14540e0aa64SRichard Purdie /* 44.1k & 48k */ 146527541f9SLiam Girdwood { 147527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 148527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 149527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 150527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 151a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 152527541f9SLiam Girdwood .fs = 256, 153a71a468aSLiam Girdwood .bfs = 64, 154527541f9SLiam Girdwood }, 155527541f9SLiam Girdwood { 156527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 157527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 158527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 159527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 160a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 161527541f9SLiam Girdwood .fs = 384, 162a71a468aSLiam Girdwood .bfs = 64, 163527541f9SLiam Girdwood }, 16440e0aa64SRichard Purdie 16540e0aa64SRichard Purdie /* 88.2 & 96k */ 166527541f9SLiam Girdwood { 167527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 168527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 169527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000, 170527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 171a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 172527541f9SLiam Girdwood .fs = 128, 173a71a468aSLiam Girdwood .bfs = 64, 174527541f9SLiam Girdwood }, 175527541f9SLiam Girdwood { 176527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 177527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 178527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000, 179527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 180a71a468aSLiam Girdwood .flags = SND_SOC_DAI_BFS_RATE, 181527541f9SLiam Girdwood .fs = 192, 182a71a468aSLiam Girdwood .bfs = 64, 183527541f9SLiam Girdwood }, 18440e0aa64SRichard Purdie 18540e0aa64SRichard Purdie /* USB codec frame and clock master modes */ 18640e0aa64SRichard Purdie /* 8k */ 187527541f9SLiam Girdwood { 188527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 189527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 190527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_8000, 191527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 192527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 193527541f9SLiam Girdwood .fs = 1500, 194527541f9SLiam Girdwood .bfs = SND_SOC_FSBD(1), 195527541f9SLiam Girdwood }, 19640e0aa64SRichard Purdie 19740e0aa64SRichard Purdie /* 44.1k */ 198527541f9SLiam Girdwood { 199527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 200527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 201527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_44100, 202527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 203527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 204527541f9SLiam Girdwood .fs = 272, 205527541f9SLiam Girdwood .bfs = SND_SOC_FSBD(1), 206527541f9SLiam Girdwood }, 20740e0aa64SRichard Purdie 20840e0aa64SRichard Purdie /* 48k */ 209527541f9SLiam Girdwood { 210527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 211527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 212527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_48000, 213527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 214527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 215527541f9SLiam Girdwood .fs = 250, 216527541f9SLiam Girdwood .bfs = SND_SOC_FSBD(1), 217527541f9SLiam Girdwood }, 21840e0aa64SRichard Purdie 21940e0aa64SRichard Purdie /* 88.2k */ 220527541f9SLiam Girdwood { 221527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 222527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 223527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_88200, 224527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 225527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 226527541f9SLiam Girdwood .fs = 136, 227527541f9SLiam Girdwood .bfs = SND_SOC_FSBD(1), 228527541f9SLiam Girdwood }, 22940e0aa64SRichard Purdie 23040e0aa64SRichard Purdie /* 96k */ 231527541f9SLiam Girdwood { 232527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, 233527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 234527541f9SLiam Girdwood .pcmrate = SNDRV_PCM_RATE_96000, 235527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 236527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 237527541f9SLiam Girdwood .fs = 125, 238527541f9SLiam Girdwood .bfs = SND_SOC_FSBD(1), 239527541f9SLiam Girdwood }, 24040e0aa64SRichard Purdie 24140e0aa64SRichard Purdie /* codec frame and clock slave modes */ 242527541f9SLiam Girdwood { 243527541f9SLiam Girdwood .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBS_CFS, 244527541f9SLiam Girdwood .pcmfmt = WM8731_HIFI_BITS, 245527541f9SLiam Girdwood .pcmrate = WM8731_RATES, 246527541f9SLiam Girdwood .pcmdir = WM8731_DIR, 247527541f9SLiam Girdwood .flags = SND_SOC_DAI_BFS_DIV, 248527541f9SLiam Girdwood .fs = SND_SOC_FS_ALL, 249a71a468aSLiam Girdwood .bfs = SND_SOC_FSB_ALL, 250527541f9SLiam Girdwood }, 25140e0aa64SRichard Purdie }; 25240e0aa64SRichard Purdie 25340e0aa64SRichard Purdie /* 25440e0aa64SRichard Purdie * read wm8731 register cache 25540e0aa64SRichard Purdie */ 25640e0aa64SRichard Purdie static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec, 25740e0aa64SRichard Purdie unsigned int reg) 25840e0aa64SRichard Purdie { 25940e0aa64SRichard Purdie u16 *cache = codec->reg_cache; 26040e0aa64SRichard Purdie if (reg == WM8731_RESET) 26140e0aa64SRichard Purdie return 0; 26240e0aa64SRichard Purdie if (reg >= WM8731_CACHEREGNUM) 26340e0aa64SRichard Purdie return -1; 26440e0aa64SRichard Purdie return cache[reg]; 26540e0aa64SRichard Purdie } 26640e0aa64SRichard Purdie 26740e0aa64SRichard Purdie /* 26840e0aa64SRichard Purdie * write wm8731 register cache 26940e0aa64SRichard Purdie */ 27040e0aa64SRichard Purdie static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec, 27140e0aa64SRichard Purdie u16 reg, unsigned int value) 27240e0aa64SRichard Purdie { 27340e0aa64SRichard Purdie u16 *cache = codec->reg_cache; 27440e0aa64SRichard Purdie if (reg >= WM8731_CACHEREGNUM) 27540e0aa64SRichard Purdie return; 27640e0aa64SRichard Purdie cache[reg] = value; 27740e0aa64SRichard Purdie } 27840e0aa64SRichard Purdie 27940e0aa64SRichard Purdie /* 28040e0aa64SRichard Purdie * write to the WM8731 register space 28140e0aa64SRichard Purdie */ 28240e0aa64SRichard Purdie static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg, 28340e0aa64SRichard Purdie unsigned int value) 28440e0aa64SRichard Purdie { 28540e0aa64SRichard Purdie u8 data[2]; 28640e0aa64SRichard Purdie 28740e0aa64SRichard Purdie /* data is 28840e0aa64SRichard Purdie * D15..D9 WM8731 register offset 28940e0aa64SRichard Purdie * D8...D0 register data 29040e0aa64SRichard Purdie */ 29140e0aa64SRichard Purdie data[0] = (reg << 1) | ((value >> 8) & 0x0001); 29240e0aa64SRichard Purdie data[1] = value & 0x00ff; 29340e0aa64SRichard Purdie 29440e0aa64SRichard Purdie wm8731_write_reg_cache (codec, reg, value); 29540e0aa64SRichard Purdie if (codec->hw_write(codec->control_data, data, 2) == 2) 29640e0aa64SRichard Purdie return 0; 29740e0aa64SRichard Purdie else 29840e0aa64SRichard Purdie return -EIO; 29940e0aa64SRichard Purdie } 30040e0aa64SRichard Purdie 30140e0aa64SRichard Purdie #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0) 30240e0aa64SRichard Purdie 30340e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"}; 30440e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 30540e0aa64SRichard Purdie 30640e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = { 30740e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), 30840e0aa64SRichard Purdie SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), 30940e0aa64SRichard Purdie }; 31040e0aa64SRichard Purdie 31140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = { 31240e0aa64SRichard Purdie 313bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, 314bd903b6eSLiam Girdwood 0, 127, 0), 315bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, 316bd903b6eSLiam Girdwood 7, 1, 0), 31740e0aa64SRichard Purdie 31840e0aa64SRichard Purdie SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0), 31940e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), 32040e0aa64SRichard Purdie 32140e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), 32240e0aa64SRichard Purdie SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1), 32340e0aa64SRichard Purdie 32440e0aa64SRichard Purdie SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1), 32540e0aa64SRichard Purdie 32640e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), 32740e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), 32840e0aa64SRichard Purdie 32940e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), 33040e0aa64SRichard Purdie }; 33140e0aa64SRichard Purdie 33240e0aa64SRichard Purdie /* add non dapm controls */ 33340e0aa64SRichard Purdie static int wm8731_add_controls(struct snd_soc_codec *codec) 33440e0aa64SRichard Purdie { 33540e0aa64SRichard Purdie int err, i; 33640e0aa64SRichard Purdie 33740e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) { 33840e0aa64SRichard Purdie if ((err = snd_ctl_add(codec->card, 33940e0aa64SRichard Purdie snd_soc_cnew(&wm8731_snd_controls[i],codec, NULL))) < 0) 34040e0aa64SRichard Purdie return err; 34140e0aa64SRichard Purdie } 34240e0aa64SRichard Purdie 34340e0aa64SRichard Purdie return 0; 34440e0aa64SRichard Purdie } 34540e0aa64SRichard Purdie 34640e0aa64SRichard Purdie /* Output Mixer */ 34740e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { 34840e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), 34940e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), 35040e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), 35140e0aa64SRichard Purdie }; 35240e0aa64SRichard Purdie 35340e0aa64SRichard Purdie /* Input mux */ 35440e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls = 35540e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); 35640e0aa64SRichard Purdie 35740e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { 35840e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, 35940e0aa64SRichard Purdie &wm8731_output_mixer_controls[0], 36040e0aa64SRichard Purdie ARRAY_SIZE(wm8731_output_mixer_controls)), 36140e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), 36240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"), 36340e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"), 36440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"), 36540e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"), 36640e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), 36740e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), 36840e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), 36940e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), 37040e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"), 37140e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"), 37240e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"), 37340e0aa64SRichard Purdie }; 37440e0aa64SRichard Purdie 37540e0aa64SRichard Purdie static const char *intercon[][3] = { 37640e0aa64SRichard Purdie /* output mixer */ 37740e0aa64SRichard Purdie {"Output Mixer", "Line Bypass Switch", "Line Input"}, 37840e0aa64SRichard Purdie {"Output Mixer", "HiFi Playback Switch", "DAC"}, 37940e0aa64SRichard Purdie {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, 38040e0aa64SRichard Purdie 38140e0aa64SRichard Purdie /* outputs */ 38240e0aa64SRichard Purdie {"RHPOUT", NULL, "Output Mixer"}, 38340e0aa64SRichard Purdie {"ROUT", NULL, "Output Mixer"}, 38440e0aa64SRichard Purdie {"LHPOUT", NULL, "Output Mixer"}, 38540e0aa64SRichard Purdie {"LOUT", NULL, "Output Mixer"}, 38640e0aa64SRichard Purdie 38740e0aa64SRichard Purdie /* input mux */ 38840e0aa64SRichard Purdie {"Input Mux", "Line In", "Line Input"}, 38940e0aa64SRichard Purdie {"Input Mux", "Mic", "Mic Bias"}, 39040e0aa64SRichard Purdie {"ADC", NULL, "Input Mux"}, 39140e0aa64SRichard Purdie 39240e0aa64SRichard Purdie /* inputs */ 39340e0aa64SRichard Purdie {"Line Input", NULL, "LLINEIN"}, 39440e0aa64SRichard Purdie {"Line Input", NULL, "RLINEIN"}, 39540e0aa64SRichard Purdie {"Mic Bias", NULL, "MICIN"}, 39640e0aa64SRichard Purdie 39740e0aa64SRichard Purdie /* terminator */ 39840e0aa64SRichard Purdie {NULL, NULL, NULL}, 39940e0aa64SRichard Purdie }; 40040e0aa64SRichard Purdie 40140e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec) 40240e0aa64SRichard Purdie { 40340e0aa64SRichard Purdie int i; 40440e0aa64SRichard Purdie 40540e0aa64SRichard Purdie for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) { 40640e0aa64SRichard Purdie snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]); 40740e0aa64SRichard Purdie } 40840e0aa64SRichard Purdie 40940e0aa64SRichard Purdie /* set up audio path interconnects */ 41040e0aa64SRichard Purdie for(i = 0; intercon[i][0] != NULL; i++) { 41140e0aa64SRichard Purdie snd_soc_dapm_connect_input(codec, intercon[i][0], 41240e0aa64SRichard Purdie intercon[i][1], intercon[i][2]); 41340e0aa64SRichard Purdie } 41440e0aa64SRichard Purdie 41540e0aa64SRichard Purdie snd_soc_dapm_new_widgets(codec); 41640e0aa64SRichard Purdie return 0; 41740e0aa64SRichard Purdie } 41840e0aa64SRichard Purdie 41940e0aa64SRichard Purdie struct _coeff_div { 42040e0aa64SRichard Purdie u32 mclk; 42140e0aa64SRichard Purdie u32 rate; 42240e0aa64SRichard Purdie u16 fs; 42340e0aa64SRichard Purdie u8 sr:4; 42440e0aa64SRichard Purdie u8 bosr:1; 42540e0aa64SRichard Purdie u8 usb:1; 42640e0aa64SRichard Purdie }; 42740e0aa64SRichard Purdie 42840e0aa64SRichard Purdie /* codec mclk clock divider coefficients */ 42940e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = { 43040e0aa64SRichard Purdie /* 48k */ 43140e0aa64SRichard Purdie {12288000, 48000, 256, 0x0, 0x0, 0x0}, 43240e0aa64SRichard Purdie {18432000, 48000, 384, 0x0, 0x1, 0x0}, 43340e0aa64SRichard Purdie {12000000, 48000, 250, 0x0, 0x0, 0x1}, 43440e0aa64SRichard Purdie 43540e0aa64SRichard Purdie /* 32k */ 43640e0aa64SRichard Purdie {12288000, 32000, 384, 0x6, 0x0, 0x0}, 43740e0aa64SRichard Purdie {18432000, 32000, 576, 0x6, 0x1, 0x0}, 438*298a2c75SFrank Mandarino {12000000, 32000, 375, 0x6, 0x0, 0x1}, 43940e0aa64SRichard Purdie 44040e0aa64SRichard Purdie /* 8k */ 44140e0aa64SRichard Purdie {12288000, 8000, 1536, 0x3, 0x0, 0x0}, 44240e0aa64SRichard Purdie {18432000, 8000, 2304, 0x3, 0x1, 0x0}, 44340e0aa64SRichard Purdie {11289600, 8000, 1408, 0xb, 0x0, 0x0}, 44440e0aa64SRichard Purdie {16934400, 8000, 2112, 0xb, 0x1, 0x0}, 44540e0aa64SRichard Purdie {12000000, 8000, 1500, 0x3, 0x0, 0x1}, 44640e0aa64SRichard Purdie 44740e0aa64SRichard Purdie /* 96k */ 44840e0aa64SRichard Purdie {12288000, 96000, 128, 0x7, 0x0, 0x0}, 44940e0aa64SRichard Purdie {18432000, 96000, 192, 0x7, 0x1, 0x0}, 45040e0aa64SRichard Purdie {12000000, 96000, 125, 0x7, 0x0, 0x1}, 45140e0aa64SRichard Purdie 45240e0aa64SRichard Purdie /* 44.1k */ 45340e0aa64SRichard Purdie {11289600, 44100, 256, 0x8, 0x0, 0x0}, 45440e0aa64SRichard Purdie {16934400, 44100, 384, 0x8, 0x1, 0x0}, 45540e0aa64SRichard Purdie {12000000, 44100, 272, 0x8, 0x1, 0x1}, 45640e0aa64SRichard Purdie 45740e0aa64SRichard Purdie /* 88.2k */ 45840e0aa64SRichard Purdie {11289600, 88200, 128, 0xf, 0x0, 0x0}, 45940e0aa64SRichard Purdie {16934400, 88200, 192, 0xf, 0x1, 0x0}, 46040e0aa64SRichard Purdie {12000000, 88200, 136, 0xf, 0x1, 0x1}, 46140e0aa64SRichard Purdie }; 46240e0aa64SRichard Purdie 46340e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate) 46440e0aa64SRichard Purdie { 46540e0aa64SRichard Purdie int i; 46640e0aa64SRichard Purdie 46740e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { 46840e0aa64SRichard Purdie if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 46940e0aa64SRichard Purdie return i; 47040e0aa64SRichard Purdie } 47140e0aa64SRichard Purdie return 0; 47240e0aa64SRichard Purdie } 47340e0aa64SRichard Purdie 47440e0aa64SRichard Purdie /* WM8731 supports numerous clocks per sample rate */ 47540e0aa64SRichard Purdie static unsigned int wm8731_config_sysclk(struct snd_soc_codec_dai *dai, 47640e0aa64SRichard Purdie struct snd_soc_clock_info *info, unsigned int clk) 47740e0aa64SRichard Purdie { 47840e0aa64SRichard Purdie dai->mclk = 0; 47940e0aa64SRichard Purdie 48040e0aa64SRichard Purdie /* check that the calculated FS and rate actually match a clock from 48140e0aa64SRichard Purdie * the machine driver */ 48240e0aa64SRichard Purdie if (info->fs * info->rate == clk) 48340e0aa64SRichard Purdie dai->mclk = clk; 48440e0aa64SRichard Purdie 48540e0aa64SRichard Purdie return dai->mclk; 48640e0aa64SRichard Purdie } 48740e0aa64SRichard Purdie 48840e0aa64SRichard Purdie static int wm8731_pcm_prepare(struct snd_pcm_substream *substream) 48940e0aa64SRichard Purdie { 49040e0aa64SRichard Purdie struct snd_soc_pcm_runtime *rtd = substream->private_data; 49140e0aa64SRichard Purdie struct snd_soc_device *socdev = rtd->socdev; 49240e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 49340e0aa64SRichard Purdie u16 iface = 0, srate; 49440e0aa64SRichard Purdie int i = get_coeff(rtd->codec_dai->mclk, 49540e0aa64SRichard Purdie snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate)); 49640e0aa64SRichard Purdie 49740e0aa64SRichard Purdie /* set master/slave audio interface */ 49840e0aa64SRichard Purdie switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) { 49940e0aa64SRichard Purdie case SND_SOC_DAIFMT_CBM_CFM: 50040e0aa64SRichard Purdie iface |= 0x0040; 50140e0aa64SRichard Purdie break; 50240e0aa64SRichard Purdie case SND_SOC_DAIFMT_CBS_CFS: 50340e0aa64SRichard Purdie break; 50440e0aa64SRichard Purdie } 50540e0aa64SRichard Purdie srate = (coeff_div[i].sr << 2) | 50640e0aa64SRichard Purdie (coeff_div[i].bosr << 1) | coeff_div[i].usb; 50740e0aa64SRichard Purdie wm8731_write(codec, WM8731_SRATE, srate); 50840e0aa64SRichard Purdie 50940e0aa64SRichard Purdie /* interface format */ 51040e0aa64SRichard Purdie switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 51140e0aa64SRichard Purdie case SND_SOC_DAIFMT_I2S: 51240e0aa64SRichard Purdie iface |= 0x0002; 51340e0aa64SRichard Purdie break; 51440e0aa64SRichard Purdie case SND_SOC_DAIFMT_RIGHT_J: 51540e0aa64SRichard Purdie break; 51640e0aa64SRichard Purdie case SND_SOC_DAIFMT_LEFT_J: 51740e0aa64SRichard Purdie iface |= 0x0001; 51840e0aa64SRichard Purdie break; 51940e0aa64SRichard Purdie case SND_SOC_DAIFMT_DSP_A: 52040e0aa64SRichard Purdie iface |= 0x0003; 52140e0aa64SRichard Purdie break; 52240e0aa64SRichard Purdie case SND_SOC_DAIFMT_DSP_B: 52340e0aa64SRichard Purdie iface |= 0x0013; 52440e0aa64SRichard Purdie break; 52540e0aa64SRichard Purdie } 52640e0aa64SRichard Purdie 52740e0aa64SRichard Purdie /* bit size */ 52840e0aa64SRichard Purdie switch (rtd->codec_dai->dai_runtime.pcmfmt) { 52940e0aa64SRichard Purdie case SNDRV_PCM_FMTBIT_S16_LE: 53040e0aa64SRichard Purdie break; 53140e0aa64SRichard Purdie case SNDRV_PCM_FMTBIT_S20_3LE: 53240e0aa64SRichard Purdie iface |= 0x0004; 53340e0aa64SRichard Purdie break; 53440e0aa64SRichard Purdie case SNDRV_PCM_FMTBIT_S24_LE: 53540e0aa64SRichard Purdie iface |= 0x0008; 53640e0aa64SRichard Purdie break; 53740e0aa64SRichard Purdie case SNDRV_PCM_FMTBIT_S32_LE: 53840e0aa64SRichard Purdie iface |= 0x000c; 53940e0aa64SRichard Purdie break; 54040e0aa64SRichard Purdie } 54140e0aa64SRichard Purdie 54240e0aa64SRichard Purdie /* clock inversion */ 54340e0aa64SRichard Purdie switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) { 54440e0aa64SRichard Purdie case SND_SOC_DAIFMT_NB_NF: 54540e0aa64SRichard Purdie break; 54640e0aa64SRichard Purdie case SND_SOC_DAIFMT_IB_IF: 54740e0aa64SRichard Purdie iface |= 0x0090; 54840e0aa64SRichard Purdie break; 54940e0aa64SRichard Purdie case SND_SOC_DAIFMT_IB_NF: 55040e0aa64SRichard Purdie iface |= 0x0080; 55140e0aa64SRichard Purdie break; 55240e0aa64SRichard Purdie case SND_SOC_DAIFMT_NB_IF: 55340e0aa64SRichard Purdie iface |= 0x0010; 55440e0aa64SRichard Purdie break; 55540e0aa64SRichard Purdie } 55640e0aa64SRichard Purdie 55740e0aa64SRichard Purdie /* set iface */ 55840e0aa64SRichard Purdie wm8731_write(codec, WM8731_IFACE, iface); 55940e0aa64SRichard Purdie 56040e0aa64SRichard Purdie /* set active */ 56140e0aa64SRichard Purdie wm8731_write(codec, WM8731_ACTIVE, 0x0001); 56240e0aa64SRichard Purdie return 0; 56340e0aa64SRichard Purdie } 56440e0aa64SRichard Purdie 56540e0aa64SRichard Purdie static void wm8731_shutdown(struct snd_pcm_substream *substream) 56640e0aa64SRichard Purdie { 56740e0aa64SRichard Purdie struct snd_soc_pcm_runtime *rtd = substream->private_data; 56840e0aa64SRichard Purdie struct snd_soc_device *socdev = rtd->socdev; 56940e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 57040e0aa64SRichard Purdie 57140e0aa64SRichard Purdie /* deactivate */ 57240e0aa64SRichard Purdie if (!codec->active) { 57340e0aa64SRichard Purdie udelay(50); 57440e0aa64SRichard Purdie wm8731_write(codec, WM8731_ACTIVE, 0x0); 57540e0aa64SRichard Purdie } 57640e0aa64SRichard Purdie } 57740e0aa64SRichard Purdie 57840e0aa64SRichard Purdie static int wm8731_mute(struct snd_soc_codec *codec, 57940e0aa64SRichard Purdie struct snd_soc_codec_dai *dai, int mute) 58040e0aa64SRichard Purdie { 58140e0aa64SRichard Purdie u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7; 58240e0aa64SRichard Purdie if (mute) 58340e0aa64SRichard Purdie wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8); 58440e0aa64SRichard Purdie else 58540e0aa64SRichard Purdie wm8731_write(codec, WM8731_APDIGI, mute_reg); 58640e0aa64SRichard Purdie return 0; 58740e0aa64SRichard Purdie } 58840e0aa64SRichard Purdie 58940e0aa64SRichard Purdie static int wm8731_dapm_event(struct snd_soc_codec *codec, int event) 59040e0aa64SRichard Purdie { 59140e0aa64SRichard Purdie u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f; 59240e0aa64SRichard Purdie 59340e0aa64SRichard Purdie switch (event) { 59440e0aa64SRichard Purdie case SNDRV_CTL_POWER_D0: /* full On */ 59540e0aa64SRichard Purdie /* vref/mid, osc on, dac unmute */ 59640e0aa64SRichard Purdie wm8731_write(codec, WM8731_PWR, reg); 59740e0aa64SRichard Purdie break; 59840e0aa64SRichard Purdie case SNDRV_CTL_POWER_D1: /* partial On */ 59940e0aa64SRichard Purdie case SNDRV_CTL_POWER_D2: /* partial On */ 60040e0aa64SRichard Purdie break; 60140e0aa64SRichard Purdie case SNDRV_CTL_POWER_D3hot: /* Off, with power */ 60240e0aa64SRichard Purdie /* everything off except vref/vmid, */ 60340e0aa64SRichard Purdie wm8731_write(codec, WM8731_PWR, reg | 0x0040); 60440e0aa64SRichard Purdie break; 60540e0aa64SRichard Purdie case SNDRV_CTL_POWER_D3cold: /* Off, without power */ 60640e0aa64SRichard Purdie /* everything off, dac mute, inactive */ 60740e0aa64SRichard Purdie wm8731_write(codec, WM8731_ACTIVE, 0x0); 60840e0aa64SRichard Purdie wm8731_write(codec, WM8731_PWR, 0xffff); 60940e0aa64SRichard Purdie break; 61040e0aa64SRichard Purdie } 61140e0aa64SRichard Purdie codec->dapm_state = event; 61240e0aa64SRichard Purdie return 0; 61340e0aa64SRichard Purdie } 61440e0aa64SRichard Purdie 61540e0aa64SRichard Purdie struct snd_soc_codec_dai wm8731_dai = { 61640e0aa64SRichard Purdie .name = "WM8731", 61740e0aa64SRichard Purdie .playback = { 61840e0aa64SRichard Purdie .stream_name = "Playback", 61940e0aa64SRichard Purdie .channels_min = 1, 62040e0aa64SRichard Purdie .channels_max = 2, 62140e0aa64SRichard Purdie }, 62240e0aa64SRichard Purdie .capture = { 62340e0aa64SRichard Purdie .stream_name = "Capture", 62440e0aa64SRichard Purdie .channels_min = 1, 62540e0aa64SRichard Purdie .channels_max = 2, 62640e0aa64SRichard Purdie }, 62740e0aa64SRichard Purdie .config_sysclk = wm8731_config_sysclk, 62840e0aa64SRichard Purdie .digital_mute = wm8731_mute, 62940e0aa64SRichard Purdie .ops = { 63040e0aa64SRichard Purdie .prepare = wm8731_pcm_prepare, 63140e0aa64SRichard Purdie .shutdown = wm8731_shutdown, 63240e0aa64SRichard Purdie }, 63340e0aa64SRichard Purdie .caps = { 63440e0aa64SRichard Purdie .num_modes = ARRAY_SIZE(wm8731_modes), 63540e0aa64SRichard Purdie .mode = wm8731_modes, 63640e0aa64SRichard Purdie }, 63740e0aa64SRichard Purdie }; 63840e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai); 63940e0aa64SRichard Purdie 64040e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state) 64140e0aa64SRichard Purdie { 64240e0aa64SRichard Purdie struct snd_soc_device *socdev = platform_get_drvdata(pdev); 64340e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 64440e0aa64SRichard Purdie 64540e0aa64SRichard Purdie wm8731_write(codec, WM8731_ACTIVE, 0x0); 64640e0aa64SRichard Purdie wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold); 64740e0aa64SRichard Purdie return 0; 64840e0aa64SRichard Purdie } 64940e0aa64SRichard Purdie 65040e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev) 65140e0aa64SRichard Purdie { 65240e0aa64SRichard Purdie struct snd_soc_device *socdev = platform_get_drvdata(pdev); 65340e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 65440e0aa64SRichard Purdie int i; 65540e0aa64SRichard Purdie u8 data[2]; 65640e0aa64SRichard Purdie u16 *cache = codec->reg_cache; 65740e0aa64SRichard Purdie 65840e0aa64SRichard Purdie /* Sync reg_cache with the hardware */ 65940e0aa64SRichard Purdie for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { 66040e0aa64SRichard Purdie data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); 66140e0aa64SRichard Purdie data[1] = cache[i] & 0x00ff; 66240e0aa64SRichard Purdie codec->hw_write(codec->control_data, data, 2); 66340e0aa64SRichard Purdie } 66440e0aa64SRichard Purdie wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot); 66540e0aa64SRichard Purdie wm8731_dapm_event(codec, codec->suspend_dapm_state); 66640e0aa64SRichard Purdie return 0; 66740e0aa64SRichard Purdie } 66840e0aa64SRichard Purdie 66940e0aa64SRichard Purdie /* 67040e0aa64SRichard Purdie * initialise the WM8731 driver 67140e0aa64SRichard Purdie * register the mixer and dsp interfaces with the kernel 67240e0aa64SRichard Purdie */ 67340e0aa64SRichard Purdie static int wm8731_init(struct snd_soc_device *socdev) 67440e0aa64SRichard Purdie { 67540e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 67640e0aa64SRichard Purdie int reg, ret = 0; 67740e0aa64SRichard Purdie 67840e0aa64SRichard Purdie codec->name = "WM8731"; 67940e0aa64SRichard Purdie codec->owner = THIS_MODULE; 68040e0aa64SRichard Purdie codec->read = wm8731_read_reg_cache; 68140e0aa64SRichard Purdie codec->write = wm8731_write; 68240e0aa64SRichard Purdie codec->dapm_event = wm8731_dapm_event; 68340e0aa64SRichard Purdie codec->dai = &wm8731_dai; 68440e0aa64SRichard Purdie codec->num_dai = 1; 68540e0aa64SRichard Purdie codec->reg_cache_size = ARRAY_SIZE(wm8731_reg); 68640e0aa64SRichard Purdie 68740e0aa64SRichard Purdie codec->reg_cache = 68840e0aa64SRichard Purdie kzalloc(sizeof(u16) * ARRAY_SIZE(wm8731_reg), GFP_KERNEL); 68940e0aa64SRichard Purdie if (codec->reg_cache == NULL) 69040e0aa64SRichard Purdie return -ENOMEM; 69140e0aa64SRichard Purdie memcpy(codec->reg_cache, 69240e0aa64SRichard Purdie wm8731_reg, sizeof(u16) * ARRAY_SIZE(wm8731_reg)); 69340e0aa64SRichard Purdie codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8731_reg); 69440e0aa64SRichard Purdie 69540e0aa64SRichard Purdie wm8731_reset(codec); 69640e0aa64SRichard Purdie 69740e0aa64SRichard Purdie /* register pcms */ 69840e0aa64SRichard Purdie ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 69940e0aa64SRichard Purdie if (ret < 0) { 700e35115a5SLiam Girdwood printk(KERN_ERR "wm8731: failed to create pcms\n"); 701e35115a5SLiam Girdwood goto pcm_err; 70240e0aa64SRichard Purdie } 70340e0aa64SRichard Purdie 70440e0aa64SRichard Purdie /* power on device */ 70540e0aa64SRichard Purdie wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot); 70640e0aa64SRichard Purdie 70740e0aa64SRichard Purdie /* set the update bits */ 70840e0aa64SRichard Purdie reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V); 70940e0aa64SRichard Purdie wm8731_write(codec, WM8731_LOUT1V, reg | 0x0100); 71040e0aa64SRichard Purdie reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V); 71140e0aa64SRichard Purdie wm8731_write(codec, WM8731_ROUT1V, reg | 0x0100); 71240e0aa64SRichard Purdie reg = wm8731_read_reg_cache(codec, WM8731_LINVOL); 71340e0aa64SRichard Purdie wm8731_write(codec, WM8731_LINVOL, reg | 0x0100); 71440e0aa64SRichard Purdie reg = wm8731_read_reg_cache(codec, WM8731_RINVOL); 71540e0aa64SRichard Purdie wm8731_write(codec, WM8731_RINVOL, reg | 0x0100); 71640e0aa64SRichard Purdie 71740e0aa64SRichard Purdie wm8731_add_controls(codec); 71840e0aa64SRichard Purdie wm8731_add_widgets(codec); 71940e0aa64SRichard Purdie ret = snd_soc_register_card(socdev); 72040e0aa64SRichard Purdie if (ret < 0) { 721e35115a5SLiam Girdwood printk(KERN_ERR "wm8731: failed to register card\n"); 722e35115a5SLiam Girdwood goto card_err; 72340e0aa64SRichard Purdie } 72440e0aa64SRichard Purdie 72540e0aa64SRichard Purdie return ret; 726e35115a5SLiam Girdwood 727e35115a5SLiam Girdwood card_err: 728e35115a5SLiam Girdwood snd_soc_free_pcms(socdev); 729e35115a5SLiam Girdwood snd_soc_dapm_free(socdev); 730e35115a5SLiam Girdwood pcm_err: 731e35115a5SLiam Girdwood kfree(codec->reg_cache); 732e35115a5SLiam Girdwood return ret; 73340e0aa64SRichard Purdie } 73440e0aa64SRichard Purdie 73540e0aa64SRichard Purdie static struct snd_soc_device *wm8731_socdev; 73640e0aa64SRichard Purdie 73740e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) 73840e0aa64SRichard Purdie 73940e0aa64SRichard Purdie /* 74040e0aa64SRichard Purdie * WM8731 2 wire address is determined by GPIO5 74140e0aa64SRichard Purdie * state during powerup. 74240e0aa64SRichard Purdie * low = 0x1a 74340e0aa64SRichard Purdie * high = 0x1b 74440e0aa64SRichard Purdie */ 74540e0aa64SRichard Purdie static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END }; 74640e0aa64SRichard Purdie 74740e0aa64SRichard Purdie /* Magic definition of all other variables and things */ 74840e0aa64SRichard Purdie I2C_CLIENT_INSMOD; 74940e0aa64SRichard Purdie 75040e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver; 75140e0aa64SRichard Purdie static struct i2c_client client_template; 75240e0aa64SRichard Purdie 75340e0aa64SRichard Purdie /* If the i2c layer weren't so broken, we could pass this kind of data 75440e0aa64SRichard Purdie around */ 75540e0aa64SRichard Purdie 75640e0aa64SRichard Purdie static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind) 75740e0aa64SRichard Purdie { 75840e0aa64SRichard Purdie struct snd_soc_device *socdev = wm8731_socdev; 75940e0aa64SRichard Purdie struct wm8731_setup_data *setup = socdev->codec_data; 76040e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 76140e0aa64SRichard Purdie struct i2c_client *i2c; 76240e0aa64SRichard Purdie int ret; 76340e0aa64SRichard Purdie 76440e0aa64SRichard Purdie if (addr != setup->i2c_address) 76540e0aa64SRichard Purdie return -ENODEV; 76640e0aa64SRichard Purdie 76740e0aa64SRichard Purdie client_template.adapter = adap; 76840e0aa64SRichard Purdie client_template.addr = addr; 76940e0aa64SRichard Purdie 77040e0aa64SRichard Purdie i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); 77140e0aa64SRichard Purdie if (i2c == NULL) { 77240e0aa64SRichard Purdie kfree(codec); 77340e0aa64SRichard Purdie return -ENOMEM; 77440e0aa64SRichard Purdie } 77540e0aa64SRichard Purdie memcpy(i2c, &client_template, sizeof(struct i2c_client)); 77640e0aa64SRichard Purdie i2c_set_clientdata(i2c, codec); 77740e0aa64SRichard Purdie codec->control_data = i2c; 77840e0aa64SRichard Purdie 77940e0aa64SRichard Purdie ret = i2c_attach_client(i2c); 78040e0aa64SRichard Purdie if (ret < 0) { 78140e0aa64SRichard Purdie err("failed to attach codec at addr %x\n", addr); 78240e0aa64SRichard Purdie goto err; 78340e0aa64SRichard Purdie } 78440e0aa64SRichard Purdie 78540e0aa64SRichard Purdie ret = wm8731_init(socdev); 78640e0aa64SRichard Purdie if (ret < 0) { 78740e0aa64SRichard Purdie err("failed to initialise WM8731\n"); 78840e0aa64SRichard Purdie goto err; 78940e0aa64SRichard Purdie } 79040e0aa64SRichard Purdie return ret; 79140e0aa64SRichard Purdie 79240e0aa64SRichard Purdie err: 79340e0aa64SRichard Purdie kfree(codec); 79440e0aa64SRichard Purdie kfree(i2c); 79540e0aa64SRichard Purdie return ret; 79640e0aa64SRichard Purdie } 79740e0aa64SRichard Purdie 79840e0aa64SRichard Purdie static int wm8731_i2c_detach(struct i2c_client *client) 79940e0aa64SRichard Purdie { 80040e0aa64SRichard Purdie struct snd_soc_codec* codec = i2c_get_clientdata(client); 80140e0aa64SRichard Purdie i2c_detach_client(client); 80240e0aa64SRichard Purdie kfree(codec->reg_cache); 80340e0aa64SRichard Purdie kfree(client); 80440e0aa64SRichard Purdie return 0; 80540e0aa64SRichard Purdie } 80640e0aa64SRichard Purdie 80740e0aa64SRichard Purdie static int wm8731_i2c_attach(struct i2c_adapter *adap) 80840e0aa64SRichard Purdie { 80940e0aa64SRichard Purdie return i2c_probe(adap, &addr_data, wm8731_codec_probe); 81040e0aa64SRichard Purdie } 81140e0aa64SRichard Purdie 81240e0aa64SRichard Purdie /* corgi i2c codec control layer */ 81340e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver = { 81440e0aa64SRichard Purdie .driver = { 81540e0aa64SRichard Purdie .name = "WM8731 I2C Codec", 81640e0aa64SRichard Purdie .owner = THIS_MODULE, 81740e0aa64SRichard Purdie }, 81840e0aa64SRichard Purdie .id = I2C_DRIVERID_WM8731, 81940e0aa64SRichard Purdie .attach_adapter = wm8731_i2c_attach, 82040e0aa64SRichard Purdie .detach_client = wm8731_i2c_detach, 82140e0aa64SRichard Purdie .command = NULL, 82240e0aa64SRichard Purdie }; 82340e0aa64SRichard Purdie 82440e0aa64SRichard Purdie static struct i2c_client client_template = { 82540e0aa64SRichard Purdie .name = "WM8731", 82640e0aa64SRichard Purdie .driver = &wm8731_i2c_driver, 82740e0aa64SRichard Purdie }; 82840e0aa64SRichard Purdie #endif 82940e0aa64SRichard Purdie 83040e0aa64SRichard Purdie static int wm8731_probe(struct platform_device *pdev) 83140e0aa64SRichard Purdie { 83240e0aa64SRichard Purdie struct snd_soc_device *socdev = platform_get_drvdata(pdev); 83340e0aa64SRichard Purdie struct wm8731_setup_data *setup; 83440e0aa64SRichard Purdie struct snd_soc_codec *codec; 83540e0aa64SRichard Purdie int ret = 0; 83640e0aa64SRichard Purdie 83740e0aa64SRichard Purdie info("WM8731 Audio Codec %s", WM8731_VERSION); 83840e0aa64SRichard Purdie 83940e0aa64SRichard Purdie setup = socdev->codec_data; 84040e0aa64SRichard Purdie codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 84140e0aa64SRichard Purdie if (codec == NULL) 84240e0aa64SRichard Purdie return -ENOMEM; 84340e0aa64SRichard Purdie 84440e0aa64SRichard Purdie socdev->codec = codec; 84540e0aa64SRichard Purdie mutex_init(&codec->mutex); 84640e0aa64SRichard Purdie INIT_LIST_HEAD(&codec->dapm_widgets); 84740e0aa64SRichard Purdie INIT_LIST_HEAD(&codec->dapm_paths); 84840e0aa64SRichard Purdie 84940e0aa64SRichard Purdie wm8731_socdev = socdev; 85040e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) 85140e0aa64SRichard Purdie if (setup->i2c_address) { 85240e0aa64SRichard Purdie normal_i2c[0] = setup->i2c_address; 85340e0aa64SRichard Purdie codec->hw_write = (hw_write_t)i2c_master_send; 85440e0aa64SRichard Purdie ret = i2c_add_driver(&wm8731_i2c_driver); 85540e0aa64SRichard Purdie if (ret != 0) 85640e0aa64SRichard Purdie printk(KERN_ERR "can't add i2c driver"); 85740e0aa64SRichard Purdie } 85840e0aa64SRichard Purdie #else 85940e0aa64SRichard Purdie /* Add other interfaces here */ 86040e0aa64SRichard Purdie #endif 86140e0aa64SRichard Purdie return ret; 86240e0aa64SRichard Purdie } 86340e0aa64SRichard Purdie 86440e0aa64SRichard Purdie /* power down chip */ 86540e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev) 86640e0aa64SRichard Purdie { 86740e0aa64SRichard Purdie struct snd_soc_device *socdev = platform_get_drvdata(pdev); 86840e0aa64SRichard Purdie struct snd_soc_codec *codec = socdev->codec; 86940e0aa64SRichard Purdie 87040e0aa64SRichard Purdie if (codec->control_data) 87140e0aa64SRichard Purdie wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold); 87240e0aa64SRichard Purdie 87340e0aa64SRichard Purdie snd_soc_free_pcms(socdev); 87440e0aa64SRichard Purdie snd_soc_dapm_free(socdev); 87540e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) 87640e0aa64SRichard Purdie i2c_del_driver(&wm8731_i2c_driver); 87740e0aa64SRichard Purdie #endif 87840e0aa64SRichard Purdie kfree(codec); 87940e0aa64SRichard Purdie 88040e0aa64SRichard Purdie return 0; 88140e0aa64SRichard Purdie } 88240e0aa64SRichard Purdie 88340e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = { 88440e0aa64SRichard Purdie .probe = wm8731_probe, 88540e0aa64SRichard Purdie .remove = wm8731_remove, 88640e0aa64SRichard Purdie .suspend = wm8731_suspend, 88740e0aa64SRichard Purdie .resume = wm8731_resume, 88840e0aa64SRichard Purdie }; 88940e0aa64SRichard Purdie 89040e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731); 89140e0aa64SRichard Purdie 89240e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver"); 89340e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie"); 89440e0aa64SRichard Purdie MODULE_LICENSE("GPL"); 895