Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8741.c -- WM8741 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2010 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Ian Lartey <ian@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/consumer.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
| 26 | #include <sound/soc.h> |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 27 | #include <sound/initval.h> |
| 28 | #include <sound/tlv.h> |
| 29 | |
| 30 | #include "wm8741.h" |
| 31 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 32 | #define WM8741_NUM_SUPPLIES 2 |
| 33 | static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = { |
| 34 | "AVDD", |
| 35 | "DVDD", |
| 36 | }; |
| 37 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 38 | #define WM8741_NUM_RATES 6 |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 39 | |
| 40 | /* codec private data */ |
| 41 | struct wm8741_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 42 | enum snd_soc_control_type control_type; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 43 | struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES]; |
| 44 | unsigned int sysclk; |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 45 | struct snd_pcm_hw_constraint_list *sysclk_constraints; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = { |
| 49 | 0x0000, /* R0 - DACLLSB Attenuation */ |
| 50 | 0x0000, /* R1 - DACLMSB Attenuation */ |
| 51 | 0x0000, /* R2 - DACRLSB Attenuation */ |
| 52 | 0x0000, /* R3 - DACRMSB Attenuation */ |
| 53 | 0x0000, /* R4 - Volume Control */ |
| 54 | 0x000A, /* R5 - Format Control */ |
| 55 | 0x0000, /* R6 - Filter Control */ |
| 56 | 0x0000, /* R7 - Mode Control 1 */ |
| 57 | 0x0002, /* R8 - Mode Control 2 */ |
| 58 | 0x0000, /* R9 - Reset */ |
| 59 | 0x0002, /* R32 - ADDITONAL_CONTROL_1 */ |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | static int wm8741_reset(struct snd_soc_codec *codec) |
| 64 | { |
| 65 | return snd_soc_write(codec, WM8741_RESET, 0); |
| 66 | } |
| 67 | |
| 68 | static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0); |
| 69 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0); |
| 70 | |
| 71 | static const struct snd_kcontrol_new wm8741_snd_controls[] = { |
| 72 | SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION, |
| 73 | WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine), |
| 74 | SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION, |
| 75 | WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv), |
| 76 | }; |
| 77 | |
| 78 | static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = { |
| 79 | SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0), |
| 80 | SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0), |
| 81 | SND_SOC_DAPM_OUTPUT("VOUTLP"), |
| 82 | SND_SOC_DAPM_OUTPUT("VOUTLN"), |
| 83 | SND_SOC_DAPM_OUTPUT("VOUTRP"), |
| 84 | SND_SOC_DAPM_OUTPUT("VOUTRN"), |
| 85 | }; |
| 86 | |
| 87 | static const struct snd_soc_dapm_route intercon[] = { |
| 88 | { "VOUTLP", NULL, "DACL" }, |
| 89 | { "VOUTLN", NULL, "DACL" }, |
| 90 | { "VOUTRP", NULL, "DACR" }, |
| 91 | { "VOUTRN", NULL, "DACR" }, |
| 92 | }; |
| 93 | |
| 94 | static int wm8741_add_widgets(struct snd_soc_codec *codec) |
| 95 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 96 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 97 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 98 | snd_soc_dapm_new_controls(dapm, wm8741_dapm_widgets, |
| 99 | ARRAY_SIZE(wm8741_dapm_widgets)); |
| 100 | snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static struct { |
| 106 | int value; |
| 107 | int ratio; |
| 108 | } lrclk_ratios[WM8741_NUM_RATES] = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 109 | { 1, 128 }, |
| 110 | { 2, 192 }, |
| 111 | { 3, 256 }, |
| 112 | { 4, 384 }, |
| 113 | { 5, 512 }, |
| 114 | { 6, 768 }, |
| 115 | }; |
| 116 | |
| 117 | static unsigned int rates_11289[] = { |
| 118 | 44100, 88235, |
| 119 | }; |
| 120 | |
| 121 | static struct snd_pcm_hw_constraint_list constraints_11289 = { |
| 122 | .count = ARRAY_SIZE(rates_11289), |
| 123 | .list = rates_11289, |
| 124 | }; |
| 125 | |
| 126 | static unsigned int rates_12288[] = { |
| 127 | 32000, 48000, 96000, |
| 128 | }; |
| 129 | |
| 130 | static struct snd_pcm_hw_constraint_list constraints_12288 = { |
| 131 | .count = ARRAY_SIZE(rates_12288), |
| 132 | .list = rates_12288, |
| 133 | }; |
| 134 | |
| 135 | static unsigned int rates_16384[] = { |
| 136 | 32000, |
| 137 | }; |
| 138 | |
| 139 | static struct snd_pcm_hw_constraint_list constraints_16384 = { |
| 140 | .count = ARRAY_SIZE(rates_16384), |
| 141 | .list = rates_16384, |
| 142 | }; |
| 143 | |
| 144 | static unsigned int rates_16934[] = { |
| 145 | 44100, 88235, |
| 146 | }; |
| 147 | |
| 148 | static struct snd_pcm_hw_constraint_list constraints_16934 = { |
| 149 | .count = ARRAY_SIZE(rates_16934), |
| 150 | .list = rates_16934, |
| 151 | }; |
| 152 | |
| 153 | static unsigned int rates_18432[] = { |
| 154 | 48000, 96000, |
| 155 | }; |
| 156 | |
| 157 | static struct snd_pcm_hw_constraint_list constraints_18432 = { |
| 158 | .count = ARRAY_SIZE(rates_18432), |
| 159 | .list = rates_18432, |
| 160 | }; |
| 161 | |
| 162 | static unsigned int rates_22579[] = { |
| 163 | 44100, 88235, 1764000 |
| 164 | }; |
| 165 | |
| 166 | static struct snd_pcm_hw_constraint_list constraints_22579 = { |
| 167 | .count = ARRAY_SIZE(rates_22579), |
| 168 | .list = rates_22579, |
| 169 | }; |
| 170 | |
| 171 | static unsigned int rates_24576[] = { |
| 172 | 32000, 48000, 96000, 192000 |
| 173 | }; |
| 174 | |
| 175 | static struct snd_pcm_hw_constraint_list constraints_24576 = { |
| 176 | .count = ARRAY_SIZE(rates_24576), |
| 177 | .list = rates_24576, |
| 178 | }; |
| 179 | |
| 180 | static unsigned int rates_36864[] = { |
| 181 | 48000, 96000, 19200 |
| 182 | }; |
| 183 | |
| 184 | static struct snd_pcm_hw_constraint_list constraints_36864 = { |
| 185 | .count = ARRAY_SIZE(rates_36864), |
| 186 | .list = rates_36864, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | |
| 190 | static int wm8741_startup(struct snd_pcm_substream *substream, |
| 191 | struct snd_soc_dai *dai) |
| 192 | { |
| 193 | struct snd_soc_codec *codec = dai->codec; |
| 194 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 195 | |
| 196 | /* The set of sample rates that can be supported depends on the |
| 197 | * MCLK supplied to the CODEC - enforce this. |
| 198 | */ |
| 199 | if (!wm8741->sysclk) { |
| 200 | dev_err(codec->dev, |
| 201 | "No MCLK configured, call set_sysclk() on init\n"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 206 | SNDRV_PCM_HW_PARAM_RATE, |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 207 | wm8741->sysclk_constraints); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int wm8741_hw_params(struct snd_pcm_substream *substream, |
| 213 | struct snd_pcm_hw_params *params, |
| 214 | struct snd_soc_dai *dai) |
| 215 | { |
| 216 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 217 | struct snd_soc_codec *codec = rtd->codec; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 218 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 219 | u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC; |
| 220 | int i; |
| 221 | |
| 222 | /* Find a supported LRCLK ratio */ |
| 223 | for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) { |
| 224 | if (wm8741->sysclk / params_rate(params) == |
| 225 | lrclk_ratios[i].ratio) |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | /* Should never happen, should be handled by constraints */ |
| 230 | if (i == ARRAY_SIZE(lrclk_ratios)) { |
| 231 | dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n", |
| 232 | wm8741->sysclk / params_rate(params)); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | /* bit size */ |
| 237 | switch (params_format(params)) { |
| 238 | case SNDRV_PCM_FORMAT_S16_LE: |
| 239 | break; |
| 240 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 241 | iface |= 0x0001; |
| 242 | break; |
| 243 | case SNDRV_PCM_FORMAT_S24_LE: |
| 244 | iface |= 0x0002; |
| 245 | break; |
| 246 | case SNDRV_PCM_FORMAT_S32_LE: |
| 247 | iface |= 0x0003; |
| 248 | break; |
| 249 | default: |
| 250 | dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d", |
| 251 | params_format(params)); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
| 255 | dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d", |
| 256 | params_format(params)); |
| 257 | |
| 258 | snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 263 | int clk_id, unsigned int freq, int dir) |
| 264 | { |
| 265 | struct snd_soc_codec *codec = codec_dai->codec; |
| 266 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 267 | |
| 268 | dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq); |
| 269 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 270 | switch (freq) { |
| 271 | case 11289600: |
| 272 | wm8741->sysclk_constraints = &constraints_11289; |
| 273 | wm8741->sysclk = freq; |
| 274 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 275 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 276 | case 12288000: |
| 277 | wm8741->sysclk_constraints = &constraints_12288; |
| 278 | wm8741->sysclk = freq; |
| 279 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 280 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 281 | case 16384000: |
| 282 | wm8741->sysclk_constraints = &constraints_16384; |
| 283 | wm8741->sysclk = freq; |
| 284 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 285 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 286 | case 16934400: |
| 287 | wm8741->sysclk_constraints = &constraints_16934; |
| 288 | wm8741->sysclk = freq; |
| 289 | return 0; |
| 290 | |
| 291 | case 18432000: |
| 292 | wm8741->sysclk_constraints = &constraints_18432; |
| 293 | wm8741->sysclk = freq; |
| 294 | return 0; |
| 295 | |
| 296 | case 22579200: |
| 297 | case 33868800: |
| 298 | wm8741->sysclk_constraints = &constraints_22579; |
| 299 | wm8741->sysclk = freq; |
| 300 | return 0; |
| 301 | |
| 302 | case 24576000: |
| 303 | wm8741->sysclk_constraints = &constraints_24576; |
| 304 | wm8741->sysclk = freq; |
| 305 | return 0; |
| 306 | |
| 307 | case 36864000: |
| 308 | wm8741->sysclk_constraints = &constraints_36864; |
| 309 | wm8741->sysclk = freq; |
| 310 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 311 | } |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 312 | return -EINVAL; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 316 | unsigned int fmt) |
| 317 | { |
| 318 | struct snd_soc_codec *codec = codec_dai->codec; |
| 319 | u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3; |
| 320 | |
| 321 | /* check master/slave audio interface */ |
| 322 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 323 | case SND_SOC_DAIFMT_CBS_CFS: |
| 324 | break; |
| 325 | default: |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | |
| 329 | /* interface format */ |
| 330 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 331 | case SND_SOC_DAIFMT_I2S: |
| 332 | iface |= 0x0008; |
| 333 | break; |
| 334 | case SND_SOC_DAIFMT_RIGHT_J: |
| 335 | break; |
| 336 | case SND_SOC_DAIFMT_LEFT_J: |
| 337 | iface |= 0x0004; |
| 338 | break; |
| 339 | case SND_SOC_DAIFMT_DSP_A: |
| 340 | iface |= 0x0003; |
| 341 | break; |
| 342 | case SND_SOC_DAIFMT_DSP_B: |
| 343 | iface |= 0x0013; |
| 344 | break; |
| 345 | default: |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | /* clock inversion */ |
| 350 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 351 | case SND_SOC_DAIFMT_NB_NF: |
| 352 | break; |
| 353 | case SND_SOC_DAIFMT_IB_IF: |
| 354 | iface |= 0x0010; |
| 355 | break; |
| 356 | case SND_SOC_DAIFMT_IB_NF: |
| 357 | iface |= 0x0020; |
| 358 | break; |
| 359 | case SND_SOC_DAIFMT_NB_IF: |
| 360 | iface |= 0x0030; |
| 361 | break; |
| 362 | default: |
| 363 | return -EINVAL; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n", |
| 368 | fmt & SND_SOC_DAIFMT_FORMAT_MASK, |
| 369 | ((fmt & SND_SOC_DAIFMT_INV_MASK))); |
| 370 | |
| 371 | snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
| 376 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ |
| 377 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \ |
| 378 | SNDRV_PCM_RATE_192000) |
| 379 | |
| 380 | #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 381 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 382 | |
| 383 | static struct snd_soc_dai_ops wm8741_dai_ops = { |
| 384 | .startup = wm8741_startup, |
| 385 | .hw_params = wm8741_hw_params, |
| 386 | .set_sysclk = wm8741_set_dai_sysclk, |
| 387 | .set_fmt = wm8741_set_dai_fmt, |
| 388 | }; |
| 389 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 390 | static struct snd_soc_dai_driver wm8741_dai = { |
Ian Lartey | 30e2d36 | 2010-08-20 17:18:44 +0100 | [diff] [blame] | 391 | .name = "wm8741", |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 392 | .playback = { |
| 393 | .stream_name = "Playback", |
| 394 | .channels_min = 2, /* Mono modes not yet supported */ |
| 395 | .channels_max = 2, |
| 396 | .rates = WM8741_RATES, |
| 397 | .formats = WM8741_FORMATS, |
| 398 | }, |
| 399 | .ops = &wm8741_dai_ops, |
| 400 | }; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 401 | |
| 402 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 403 | static int wm8741_resume(struct snd_soc_codec *codec) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 404 | { |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 405 | u16 *cache = codec->reg_cache; |
| 406 | int i; |
| 407 | |
| 408 | /* RESTORE REG Cache */ |
| 409 | for (i = 0; i < WM8741_REGISTER_COUNT; i++) { |
| 410 | if (cache[i] == wm8741_reg_defaults[i] || WM8741_RESET == i) |
| 411 | continue; |
| 412 | snd_soc_write(codec, i, cache[i]); |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | #else |
| 417 | #define wm8741_suspend NULL |
| 418 | #define wm8741_resume NULL |
| 419 | #endif |
| 420 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 421 | static int wm8741_probe(struct snd_soc_codec *codec) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 422 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 423 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 424 | int ret = 0; |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 425 | int i; |
| 426 | |
| 427 | for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++) |
| 428 | wm8741->supplies[i].supply = wm8741_supply_names[i]; |
| 429 | |
| 430 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8741->supplies), |
| 431 | wm8741->supplies); |
| 432 | if (ret != 0) { |
| 433 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); |
| 434 | goto err; |
| 435 | } |
| 436 | |
| 437 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies), |
| 438 | wm8741->supplies); |
| 439 | if (ret != 0) { |
| 440 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 441 | goto err_get; |
| 442 | } |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 443 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 444 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 445 | if (ret != 0) { |
| 446 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 447 | goto err_enable; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | ret = wm8741_reset(codec); |
| 451 | if (ret < 0) { |
| 452 | dev_err(codec->dev, "Failed to issue reset\n"); |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 453 | goto err_enable; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 456 | /* Change some default settings - latch VU */ |
Mark Brown | a1b3b5e | 2010-12-24 16:59:30 +0000 | [diff] [blame] | 457 | snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION, |
| 458 | WM8741_UPDATELL, WM8741_UPDATELL); |
| 459 | snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION, |
| 460 | WM8741_UPDATELM, WM8741_UPDATELM); |
| 461 | snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION, |
| 462 | WM8741_UPDATERL, WM8741_UPDATERL); |
| 463 | snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION, |
| 464 | WM8741_UPDATERM, WM8741_UPDATERM); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 465 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 466 | snd_soc_add_controls(codec, wm8741_snd_controls, |
| 467 | ARRAY_SIZE(wm8741_snd_controls)); |
| 468 | wm8741_add_widgets(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 469 | |
| 470 | dev_dbg(codec->dev, "Successful registration\n"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 471 | return ret; |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 472 | |
| 473 | err_enable: |
| 474 | regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
| 475 | err_get: |
| 476 | regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
| 477 | err: |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static int wm8741_remove(struct snd_soc_codec *codec) |
| 482 | { |
| 483 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 484 | |
| 485 | regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
| 486 | regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
| 487 | |
| 488 | return 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static struct snd_soc_codec_driver soc_codec_dev_wm8741 = { |
| 492 | .probe = wm8741_probe, |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 493 | .remove = wm8741_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 494 | .resume = wm8741_resume, |
Dimitris Papastamos | e5eec34 | 2010-09-10 18:14:56 +0100 | [diff] [blame] | 495 | .reg_cache_size = ARRAY_SIZE(wm8741_reg_defaults), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 496 | .reg_word_size = sizeof(u16), |
Dimitris Papastamos | 0aa34b1 | 2010-11-08 10:41:53 +0000 | [diff] [blame] | 497 | .reg_cache_default = wm8741_reg_defaults, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
| 500 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 501 | static int wm8741_i2c_probe(struct i2c_client *i2c, |
| 502 | const struct i2c_device_id *id) |
| 503 | { |
| 504 | struct wm8741_priv *wm8741; |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 505 | int ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 506 | |
| 507 | wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL); |
| 508 | if (wm8741 == NULL) |
| 509 | return -ENOMEM; |
| 510 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 511 | i2c_set_clientdata(i2c, wm8741); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 512 | wm8741->control_type = SND_SOC_I2C; |
| 513 | |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame^] | 514 | ret = snd_soc_register_codec(&i2c->dev, |
| 515 | &soc_codec_dev_wm8741, &wm8741_dai, 1); |
| 516 | if (ret != 0) |
| 517 | goto err; |
| 518 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 519 | return ret; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 520 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 521 | err: |
| 522 | kfree(wm8741); |
| 523 | return ret; |
| 524 | } |
| 525 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 526 | static int wm8741_i2c_remove(struct i2c_client *client) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 527 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 528 | snd_soc_unregister_codec(&client->dev); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 529 | kfree(i2c_get_clientdata(client)); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static const struct i2c_device_id wm8741_i2c_id[] = { |
| 534 | { "wm8741", 0 }, |
| 535 | { } |
| 536 | }; |
| 537 | MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id); |
| 538 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 539 | static struct i2c_driver wm8741_i2c_driver = { |
| 540 | .driver = { |
Mark Brown | 0473e61 | 2011-08-03 16:52:10 +0900 | [diff] [blame] | 541 | .name = "wm8741", |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 542 | .owner = THIS_MODULE, |
| 543 | }, |
| 544 | .probe = wm8741_i2c_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 545 | .remove = wm8741_i2c_remove, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 546 | .id_table = wm8741_i2c_id, |
| 547 | }; |
| 548 | #endif |
| 549 | |
| 550 | static int __init wm8741_modinit(void) |
| 551 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 552 | int ret = 0; |
| 553 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 554 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 555 | ret = i2c_add_driver(&wm8741_i2c_driver); |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 556 | if (ret != 0) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 557 | pr_err("Failed to register WM8741 I2C driver: %d\n", ret); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 558 | #endif |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 559 | |
| 560 | return ret; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 561 | } |
| 562 | module_init(wm8741_modinit); |
| 563 | |
| 564 | static void __exit wm8741_exit(void) |
| 565 | { |
| 566 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 567 | i2c_del_driver(&wm8741_i2c_driver); |
| 568 | #endif |
| 569 | } |
| 570 | module_exit(wm8741_exit); |
| 571 | |
| 572 | MODULE_DESCRIPTION("ASoC WM8741 driver"); |
| 573 | MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>"); |
| 574 | MODULE_LICENSE("GPL"); |