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