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