Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wm8731.c -- WM8731 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2005 Openedhand Ltd. |
| 5 | * |
| 6 | * Author: Richard Purdie <richard@openedhand.com> |
| 7 | * |
| 8 | * Based on wm8753.c by Liam Girdwood |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/pm.h> |
| 20 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 23 | #include <linux/regulator/consumer.h> |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 24 | #include <linux/spi/spi.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 29 | #include <sound/initval.h> |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 30 | #include <sound/tlv.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 31 | |
| 32 | #include "wm8731.h" |
| 33 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 34 | #define WM8731_NUM_SUPPLIES 4 |
| 35 | static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = { |
| 36 | "AVDD", |
| 37 | "HPVDD", |
| 38 | "DCVDD", |
| 39 | "DBVDD", |
| 40 | }; |
| 41 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 42 | /* codec private data */ |
| 43 | struct wm8731_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 44 | enum snd_soc_control_type control_type; |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 45 | struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 46 | u16 reg_cache[WM8731_CACHEREGNUM]; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 47 | unsigned int sysclk; |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 48 | int sysclk_type; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 51 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 52 | /* |
| 53 | * wm8731 register cache |
| 54 | * We can't read the WM8731 register space when we are |
| 55 | * using 2 wire for device control, so we cache them instead. |
| 56 | * There is no point in caching the reset register |
| 57 | */ |
| 58 | static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 59 | 0x0097, 0x0097, 0x0079, 0x0079, |
| 60 | 0x000a, 0x0008, 0x009f, 0x000a, |
| 61 | 0x0000, 0x0000 |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 64 | #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 65 | |
| 66 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; |
| 67 | static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; |
| 68 | |
| 69 | static const struct soc_enum wm8731_enum[] = { |
| 70 | SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), |
| 71 | SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), |
| 72 | }; |
| 73 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 74 | static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); |
| 75 | static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); |
| 76 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); |
Mark Brown | d921184 | 2010-11-30 21:03:09 +0000 | [diff] [blame^] | 77 | static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0); |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 78 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 79 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
| 80 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 81 | SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
| 82 | 0, 127, 0, out_tlv), |
Liam Girdwood | bd903b6 | 2006-11-09 16:35:01 +0100 | [diff] [blame] | 83 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
| 84 | 7, 1, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 85 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 86 | SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, |
| 87 | in_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 88 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
| 89 | |
Mark Brown | d921184 | 2010-11-30 21:03:09 +0000 | [diff] [blame^] | 90 | SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv), |
Mark Brown | ef38ed8 | 2009-07-08 17:05:43 +0100 | [diff] [blame] | 91 | SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 92 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 93 | SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, |
| 94 | sidetone_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 95 | |
| 96 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), |
| 97 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), |
| 98 | |
| 99 | SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), |
| 100 | }; |
| 101 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 102 | /* Output Mixer */ |
| 103 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { |
| 104 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), |
| 105 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), |
| 106 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), |
| 107 | }; |
| 108 | |
| 109 | /* Input mux */ |
| 110 | static const struct snd_kcontrol_new wm8731_input_mux_controls = |
| 111 | SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); |
| 112 | |
| 113 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 114 | SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 115 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, |
| 116 | &wm8731_output_mixer_controls[0], |
| 117 | ARRAY_SIZE(wm8731_output_mixer_controls)), |
| 118 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), |
| 119 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 120 | SND_SOC_DAPM_OUTPUT("LHPOUT"), |
| 121 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 122 | SND_SOC_DAPM_OUTPUT("RHPOUT"), |
| 123 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), |
| 124 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), |
| 125 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), |
| 126 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), |
| 127 | SND_SOC_DAPM_INPUT("MICIN"), |
| 128 | SND_SOC_DAPM_INPUT("RLINEIN"), |
| 129 | SND_SOC_DAPM_INPUT("LLINEIN"), |
| 130 | }; |
| 131 | |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 132 | static int wm8731_check_osc(struct snd_soc_dapm_widget *source, |
| 133 | struct snd_soc_dapm_widget *sink) |
| 134 | { |
| 135 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec); |
| 136 | |
| 137 | return wm8731->sysclk_type == WM8731_SYSCLK_MCLK; |
| 138 | } |
| 139 | |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 140 | static const struct snd_soc_dapm_route intercon[] = { |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 141 | {"DAC", NULL, "OSC", wm8731_check_osc}, |
| 142 | {"ADC", NULL, "OSC", wm8731_check_osc}, |
| 143 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 144 | /* output mixer */ |
| 145 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, |
| 146 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, |
| 147 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, |
| 148 | |
| 149 | /* outputs */ |
| 150 | {"RHPOUT", NULL, "Output Mixer"}, |
| 151 | {"ROUT", NULL, "Output Mixer"}, |
| 152 | {"LHPOUT", NULL, "Output Mixer"}, |
| 153 | {"LOUT", NULL, "Output Mixer"}, |
| 154 | |
| 155 | /* input mux */ |
| 156 | {"Input Mux", "Line In", "Line Input"}, |
| 157 | {"Input Mux", "Mic", "Mic Bias"}, |
| 158 | {"ADC", NULL, "Input Mux"}, |
| 159 | |
| 160 | /* inputs */ |
| 161 | {"Line Input", NULL, "LLINEIN"}, |
| 162 | {"Line Input", NULL, "RLINEIN"}, |
| 163 | {"Mic Bias", NULL, "MICIN"}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | static int wm8731_add_widgets(struct snd_soc_codec *codec) |
| 167 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 168 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 169 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 170 | snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets, |
| 171 | ARRAY_SIZE(wm8731_dapm_widgets)); |
| 172 | snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 173 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | struct _coeff_div { |
| 178 | u32 mclk; |
| 179 | u32 rate; |
| 180 | u16 fs; |
| 181 | u8 sr:4; |
| 182 | u8 bosr:1; |
| 183 | u8 usb:1; |
| 184 | }; |
| 185 | |
| 186 | /* codec mclk clock divider coefficients */ |
| 187 | static const struct _coeff_div coeff_div[] = { |
| 188 | /* 48k */ |
| 189 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, |
| 190 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, |
| 191 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, |
| 192 | |
| 193 | /* 32k */ |
| 194 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, |
| 195 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, |
Frank Mandarino | 298a2c7 | 2007-01-31 10:02:56 +0100 | [diff] [blame] | 196 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 197 | |
| 198 | /* 8k */ |
| 199 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, |
| 200 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, |
| 201 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, |
| 202 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, |
| 203 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, |
| 204 | |
| 205 | /* 96k */ |
| 206 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, |
| 207 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, |
| 208 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, |
| 209 | |
| 210 | /* 44.1k */ |
| 211 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, |
| 212 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, |
| 213 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, |
| 214 | |
| 215 | /* 88.2k */ |
| 216 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, |
| 217 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, |
| 218 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, |
| 219 | }; |
| 220 | |
| 221 | static inline int get_coeff(int mclk, int rate) |
| 222 | { |
| 223 | int i; |
| 224 | |
| 225 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { |
| 226 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) |
| 227 | return i; |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 232 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 233 | struct snd_pcm_hw_params *params, |
| 234 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 235 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 236 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 237 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 238 | u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 239 | int i = get_coeff(wm8731->sysclk, params_rate(params)); |
| 240 | u16 srate = (coeff_div[i].sr << 2) | |
| 241 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 242 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 243 | snd_soc_write(codec, WM8731_SRATE, srate); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 244 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 245 | /* bit size */ |
| 246 | switch (params_format(params)) { |
| 247 | case SNDRV_PCM_FORMAT_S16_LE: |
| 248 | break; |
| 249 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 250 | iface |= 0x0004; |
| 251 | break; |
| 252 | case SNDRV_PCM_FORMAT_S24_LE: |
| 253 | iface |= 0x0008; |
| 254 | break; |
| 255 | } |
| 256 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 257 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 258 | return 0; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 261 | static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, |
| 262 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 263 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 264 | struct snd_soc_codec *codec = dai->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 265 | |
| 266 | /* set active */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 267 | snd_soc_write(codec, WM8731_ACTIVE, 0x0001); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 268 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 272 | static void wm8731_shutdown(struct snd_pcm_substream *substream, |
| 273 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 274 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 275 | struct snd_soc_codec *codec = dai->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 276 | |
| 277 | /* deactivate */ |
| 278 | if (!codec->active) { |
| 279 | udelay(50); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 280 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 284 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 285 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 286 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 287 | u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 288 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 289 | if (mute) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 290 | snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 291 | else |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 292 | snd_soc_write(codec, WM8731_APDIGI, mute_reg); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 296 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 297 | int clk_id, unsigned int freq, int dir) |
| 298 | { |
| 299 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 300 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 301 | |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 302 | switch (clk_id) { |
| 303 | case WM8731_SYSCLK_XTAL: |
| 304 | case WM8731_SYSCLK_MCLK: |
| 305 | wm8731->sysclk_type = clk_id; |
| 306 | break; |
| 307 | default: |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 311 | switch (freq) { |
| 312 | case 11289600: |
| 313 | case 12000000: |
| 314 | case 12288000: |
| 315 | case 16934400: |
| 316 | case 18432000: |
| 317 | wm8731->sysclk = freq; |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 318 | break; |
| 319 | default: |
| 320 | return -EINVAL; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 321 | } |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 322 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 323 | snd_soc_dapm_sync(&codec->dapm); |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 324 | |
| 325 | return 0; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 329 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 330 | unsigned int fmt) |
| 331 | { |
| 332 | struct snd_soc_codec *codec = codec_dai->codec; |
| 333 | u16 iface = 0; |
| 334 | |
| 335 | /* set master/slave audio interface */ |
| 336 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 337 | case SND_SOC_DAIFMT_CBM_CFM: |
| 338 | iface |= 0x0040; |
| 339 | break; |
| 340 | case SND_SOC_DAIFMT_CBS_CFS: |
| 341 | break; |
| 342 | default: |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | /* interface format */ |
| 347 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 348 | case SND_SOC_DAIFMT_I2S: |
| 349 | iface |= 0x0002; |
| 350 | break; |
| 351 | case SND_SOC_DAIFMT_RIGHT_J: |
| 352 | break; |
| 353 | case SND_SOC_DAIFMT_LEFT_J: |
| 354 | iface |= 0x0001; |
| 355 | break; |
| 356 | case SND_SOC_DAIFMT_DSP_A: |
| 357 | iface |= 0x0003; |
| 358 | break; |
| 359 | case SND_SOC_DAIFMT_DSP_B: |
| 360 | iface |= 0x0013; |
| 361 | break; |
| 362 | default: |
| 363 | return -EINVAL; |
| 364 | } |
| 365 | |
| 366 | /* clock inversion */ |
| 367 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 368 | case SND_SOC_DAIFMT_NB_NF: |
| 369 | break; |
| 370 | case SND_SOC_DAIFMT_IB_IF: |
| 371 | iface |= 0x0090; |
| 372 | break; |
| 373 | case SND_SOC_DAIFMT_IB_NF: |
| 374 | iface |= 0x0080; |
| 375 | break; |
| 376 | case SND_SOC_DAIFMT_NB_IF: |
| 377 | iface |= 0x0010; |
| 378 | break; |
| 379 | default: |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | /* set iface */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 384 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 388 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
| 389 | enum snd_soc_bias_level level) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 390 | { |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 391 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 392 | int i, ret; |
| 393 | u8 data[2]; |
| 394 | u16 *cache = codec->reg_cache; |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 395 | u16 reg; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 396 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 397 | switch (level) { |
| 398 | case SND_SOC_BIAS_ON: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 399 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 400 | case SND_SOC_BIAS_PREPARE: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 401 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 402 | case SND_SOC_BIAS_STANDBY: |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 403 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 404 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), |
| 405 | wm8731->supplies); |
| 406 | if (ret != 0) |
| 407 | return ret; |
| 408 | |
| 409 | /* Sync reg_cache with the hardware */ |
| 410 | for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { |
| 411 | if (cache[i] == wm8731_reg[i]) |
| 412 | continue; |
| 413 | |
| 414 | data[0] = (i << 1) | ((cache[i] >> 8) |
| 415 | & 0x0001); |
| 416 | data[1] = cache[i] & 0x00ff; |
| 417 | codec->hw_write(codec->control_data, data, 2); |
| 418 | } |
| 419 | } |
| 420 | |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 421 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 422 | reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; |
| 423 | snd_soc_write(codec, WM8731_PWR, reg | 0x0040); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 424 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 425 | case SND_SOC_BIAS_OFF: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 426 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
| 427 | snd_soc_write(codec, WM8731_PWR, 0xffff); |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 428 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), |
| 429 | wm8731->supplies); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 430 | break; |
| 431 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 432 | codec->dapm.bias_level = level; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
Bill Gatliff | e135443 | 2010-04-09 18:08:08 +0100 | [diff] [blame] | 436 | #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 437 | |
| 438 | #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 439 | SNDRV_PCM_FMTBIT_S24_LE) |
| 440 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 441 | static struct snd_soc_dai_ops wm8731_dai_ops = { |
| 442 | .prepare = wm8731_pcm_prepare, |
| 443 | .hw_params = wm8731_hw_params, |
| 444 | .shutdown = wm8731_shutdown, |
| 445 | .digital_mute = wm8731_mute, |
| 446 | .set_sysclk = wm8731_set_dai_sysclk, |
| 447 | .set_fmt = wm8731_set_dai_fmt, |
| 448 | }; |
| 449 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 450 | static struct snd_soc_dai_driver wm8731_dai = { |
| 451 | .name = "wm8731-hifi", |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 452 | .playback = { |
| 453 | .stream_name = "Playback", |
| 454 | .channels_min = 1, |
| 455 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 456 | .rates = WM8731_RATES, |
| 457 | .formats = WM8731_FORMATS,}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 458 | .capture = { |
| 459 | .stream_name = "Capture", |
| 460 | .channels_min = 1, |
| 461 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 462 | .rates = WM8731_RATES, |
| 463 | .formats = WM8731_FORMATS,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 464 | .ops = &wm8731_dai_ops, |
Mark Brown | 4934482 | 2009-07-08 17:48:12 +0100 | [diff] [blame] | 465 | .symmetric_rates = 1, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 466 | }; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 467 | |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 468 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 469 | static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 470 | { |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 471 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 472 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 476 | static int wm8731_resume(struct snd_soc_codec *codec) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 477 | { |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 478 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 479 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 480 | return 0; |
| 481 | } |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 482 | #else |
| 483 | #define wm8731_suspend NULL |
| 484 | #define wm8731_resume NULL |
| 485 | #endif |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 486 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 487 | static int wm8731_probe(struct snd_soc_codec *codec) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 488 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 489 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 490 | int ret = 0, i; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 491 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 492 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 493 | if (ret < 0) { |
| 494 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 495 | return ret; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 498 | for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) |
| 499 | wm8731->supplies[i].supply = wm8731_supply_names[i]; |
| 500 | |
| 501 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), |
| 502 | wm8731->supplies); |
| 503 | if (ret != 0) { |
| 504 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 505 | return ret; |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), |
| 509 | wm8731->supplies); |
| 510 | if (ret != 0) { |
| 511 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 512 | goto err_regulator_get; |
| 513 | } |
| 514 | |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 515 | ret = wm8731_reset(codec); |
| 516 | if (ret < 0) { |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 517 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 518 | goto err_regulator_enable; |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 521 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 522 | |
| 523 | /* Latch the update bits */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 524 | snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); |
| 525 | snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); |
| 526 | snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); |
| 527 | snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 528 | |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 529 | /* Disable bypass path by default */ |
Dimitris Papastamos | 2062ea5 | 2010-12-01 09:38:55 +0000 | [diff] [blame] | 530 | snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0); |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 531 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 532 | snd_soc_add_controls(codec, wm8731_snd_controls, |
| 533 | ARRAY_SIZE(wm8731_snd_controls)); |
| 534 | wm8731_add_widgets(codec); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 535 | |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 536 | /* Regulators will have been enabled by bias management */ |
| 537 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
| 538 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 539 | return 0; |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 540 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 541 | err_regulator_enable: |
| 542 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
| 543 | err_regulator_get: |
| 544 | regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 545 | |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 546 | return ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 549 | /* power down chip */ |
| 550 | static int wm8731_remove(struct snd_soc_codec *codec) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 551 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 552 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 553 | |
| 554 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 555 | |
| 556 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 557 | regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 558 | |
| 559 | return 0; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 560 | } |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 561 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 562 | static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { |
| 563 | .probe = wm8731_probe, |
| 564 | .remove = wm8731_remove, |
| 565 | .suspend = wm8731_suspend, |
| 566 | .resume = wm8731_resume, |
| 567 | .set_bias_level = wm8731_set_bias_level, |
Dimitris Papastamos | e5eec34 | 2010-09-10 18:14:56 +0100 | [diff] [blame] | 568 | .reg_cache_size = ARRAY_SIZE(wm8731_reg), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 569 | .reg_word_size = sizeof(u16), |
| 570 | .reg_cache_default = wm8731_reg, |
| 571 | }; |
| 572 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 573 | #if defined(CONFIG_SPI_MASTER) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 574 | static int __devinit wm8731_spi_probe(struct spi_device *spi) |
| 575 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 576 | struct wm8731_priv *wm8731; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 577 | int ret; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 578 | |
| 579 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); |
| 580 | if (wm8731 == NULL) |
| 581 | return -ENOMEM; |
| 582 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 583 | wm8731->control_type = SND_SOC_SPI; |
| 584 | spi_set_drvdata(spi, wm8731); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 585 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 586 | ret = snd_soc_register_codec(&spi->dev, |
| 587 | &soc_codec_dev_wm8731, &wm8731_dai, 1); |
| 588 | if (ret < 0) |
| 589 | kfree(wm8731); |
| 590 | return ret; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static int __devexit wm8731_spi_remove(struct spi_device *spi) |
| 594 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 595 | snd_soc_unregister_codec(&spi->dev); |
| 596 | kfree(spi_get_drvdata(spi)); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static struct spi_driver wm8731_spi_driver = { |
| 601 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 602 | .name = "wm8731-codec", |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 603 | .owner = THIS_MODULE, |
| 604 | }, |
| 605 | .probe = wm8731_spi_probe, |
| 606 | .remove = __devexit_p(wm8731_spi_remove), |
| 607 | }; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 608 | #endif /* CONFIG_SPI_MASTER */ |
| 609 | |
| 610 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 611 | static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, |
| 612 | const struct i2c_device_id *id) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 613 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 614 | struct wm8731_priv *wm8731; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 615 | int ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 616 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 617 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); |
| 618 | if (wm8731 == NULL) |
| 619 | return -ENOMEM; |
| 620 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 621 | i2c_set_clientdata(i2c, wm8731); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 622 | wm8731->control_type = SND_SOC_I2C; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 623 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 624 | ret = snd_soc_register_codec(&i2c->dev, |
| 625 | &soc_codec_dev_wm8731, &wm8731_dai, 1); |
| 626 | if (ret < 0) |
| 627 | kfree(wm8731); |
| 628 | return ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 631 | static __devexit int wm8731_i2c_remove(struct i2c_client *client) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 632 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 633 | snd_soc_unregister_codec(&client->dev); |
| 634 | kfree(i2c_get_clientdata(client)); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static const struct i2c_device_id wm8731_i2c_id[] = { |
| 639 | { "wm8731", 0 }, |
| 640 | { } |
| 641 | }; |
| 642 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); |
| 643 | |
| 644 | static struct i2c_driver wm8731_i2c_driver = { |
| 645 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 646 | .name = "wm8731-codec", |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 647 | .owner = THIS_MODULE, |
| 648 | }, |
| 649 | .probe = wm8731_i2c_probe, |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 650 | .remove = __devexit_p(wm8731_i2c_remove), |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 651 | .id_table = wm8731_i2c_id, |
| 652 | }; |
| 653 | #endif |
| 654 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 655 | static int __init wm8731_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 656 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 657 | int ret = 0; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 658 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 659 | ret = i2c_add_driver(&wm8731_i2c_driver); |
| 660 | if (ret != 0) { |
| 661 | printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", |
| 662 | ret); |
| 663 | } |
| 664 | #endif |
| 665 | #if defined(CONFIG_SPI_MASTER) |
| 666 | ret = spi_register_driver(&wm8731_spi_driver); |
| 667 | if (ret != 0) { |
| 668 | printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", |
| 669 | ret); |
| 670 | } |
| 671 | #endif |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 672 | return ret; |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 673 | } |
| 674 | module_init(wm8731_modinit); |
| 675 | |
| 676 | static void __exit wm8731_exit(void) |
| 677 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 678 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 679 | i2c_del_driver(&wm8731_i2c_driver); |
| 680 | #endif |
| 681 | #if defined(CONFIG_SPI_MASTER) |
| 682 | spi_unregister_driver(&wm8731_spi_driver); |
| 683 | #endif |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 684 | } |
| 685 | module_exit(wm8731_exit); |
| 686 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 687 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
| 688 | MODULE_AUTHOR("Richard Purdie"); |
| 689 | MODULE_LICENSE("GPL"); |