Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8580.c -- WM8580 ALSA Soc Audio driver |
| 3 | * |
Mark Brown | 656baae | 2012-05-23 12:39:07 +0100 | [diff] [blame] | 4 | * Copyright 2008-11 Wolfson Microelectronics PLC. |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | * Notes: |
| 12 | * The WM8580 is a multichannel codec with S/PDIF support, featuring six |
| 13 | * DAC channels and two ADC channels. |
| 14 | * |
| 15 | * Currently only the primary audio interface is supported - S/PDIF and |
| 16 | * the secondary audio interfaces are not. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/pm.h> |
| 25 | #include <linux/i2c.h> |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 26 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Mark Brown | 733eef3 | 2011-08-02 13:22:36 +0900 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 29 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 30 | #include <sound/core.h> |
| 31 | #include <sound/pcm.h> |
| 32 | #include <sound/pcm_params.h> |
| 33 | #include <sound/soc.h> |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 34 | #include <sound/tlv.h> |
| 35 | #include <sound/initval.h> |
| 36 | #include <asm/div64.h> |
| 37 | |
| 38 | #include "wm8580.h" |
| 39 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 40 | /* WM8580 register space */ |
| 41 | #define WM8580_PLLA1 0x00 |
| 42 | #define WM8580_PLLA2 0x01 |
| 43 | #define WM8580_PLLA3 0x02 |
| 44 | #define WM8580_PLLA4 0x03 |
| 45 | #define WM8580_PLLB1 0x04 |
| 46 | #define WM8580_PLLB2 0x05 |
| 47 | #define WM8580_PLLB3 0x06 |
| 48 | #define WM8580_PLLB4 0x07 |
| 49 | #define WM8580_CLKSEL 0x08 |
| 50 | #define WM8580_PAIF1 0x09 |
| 51 | #define WM8580_PAIF2 0x0A |
| 52 | #define WM8580_SAIF1 0x0B |
| 53 | #define WM8580_PAIF3 0x0C |
| 54 | #define WM8580_PAIF4 0x0D |
| 55 | #define WM8580_SAIF2 0x0E |
| 56 | #define WM8580_DAC_CONTROL1 0x0F |
| 57 | #define WM8580_DAC_CONTROL2 0x10 |
| 58 | #define WM8580_DAC_CONTROL3 0x11 |
| 59 | #define WM8580_DAC_CONTROL4 0x12 |
| 60 | #define WM8580_DAC_CONTROL5 0x13 |
| 61 | #define WM8580_DIGITAL_ATTENUATION_DACL1 0x14 |
| 62 | #define WM8580_DIGITAL_ATTENUATION_DACR1 0x15 |
| 63 | #define WM8580_DIGITAL_ATTENUATION_DACL2 0x16 |
| 64 | #define WM8580_DIGITAL_ATTENUATION_DACR2 0x17 |
| 65 | #define WM8580_DIGITAL_ATTENUATION_DACL3 0x18 |
| 66 | #define WM8580_DIGITAL_ATTENUATION_DACR3 0x19 |
| 67 | #define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C |
| 68 | #define WM8580_ADC_CONTROL1 0x1D |
| 69 | #define WM8580_SPDTXCHAN0 0x1E |
| 70 | #define WM8580_SPDTXCHAN1 0x1F |
| 71 | #define WM8580_SPDTXCHAN2 0x20 |
| 72 | #define WM8580_SPDTXCHAN3 0x21 |
| 73 | #define WM8580_SPDTXCHAN4 0x22 |
| 74 | #define WM8580_SPDTXCHAN5 0x23 |
| 75 | #define WM8580_SPDMODE 0x24 |
| 76 | #define WM8580_INTMASK 0x25 |
| 77 | #define WM8580_GPO1 0x26 |
| 78 | #define WM8580_GPO2 0x27 |
| 79 | #define WM8580_GPO3 0x28 |
| 80 | #define WM8580_GPO4 0x29 |
| 81 | #define WM8580_GPO5 0x2A |
| 82 | #define WM8580_INTSTAT 0x2B |
| 83 | #define WM8580_SPDRXCHAN1 0x2C |
| 84 | #define WM8580_SPDRXCHAN2 0x2D |
| 85 | #define WM8580_SPDRXCHAN3 0x2E |
| 86 | #define WM8580_SPDRXCHAN4 0x2F |
| 87 | #define WM8580_SPDRXCHAN5 0x30 |
| 88 | #define WM8580_SPDSTAT 0x31 |
| 89 | #define WM8580_PWRDN1 0x32 |
| 90 | #define WM8580_PWRDN2 0x33 |
| 91 | #define WM8580_READBACK 0x34 |
| 92 | #define WM8580_RESET 0x35 |
| 93 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 94 | #define WM8580_MAX_REGISTER 0x35 |
| 95 | |
Mark Brown | 6bfb6aa | 2010-08-13 20:08:55 +0100 | [diff] [blame] | 96 | #define WM8580_DACOSR 0x40 |
| 97 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 98 | /* PLLB4 (register 7h) */ |
| 99 | #define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60 |
| 100 | #define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20 |
| 101 | #define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40 |
| 102 | #define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60 |
| 103 | |
| 104 | #define WM8580_PLLB4_CLKOUTSRC_MASK 0x180 |
| 105 | #define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080 |
| 106 | #define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100 |
| 107 | #define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180 |
| 108 | |
| 109 | /* CLKSEL (register 8h) */ |
| 110 | #define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03 |
| 111 | #define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01 |
| 112 | #define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02 |
| 113 | |
| 114 | /* AIF control 1 (registers 9h-bh) */ |
| 115 | #define WM8580_AIF_RATE_MASK 0x7 |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 116 | #define WM8580_AIF_BCLKSEL_MASK 0x18 |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 117 | |
| 118 | #define WM8580_AIF_MS 0x20 |
| 119 | |
| 120 | #define WM8580_AIF_CLKSRC_MASK 0xc0 |
| 121 | #define WM8580_AIF_CLKSRC_PLLA 0x40 |
| 122 | #define WM8580_AIF_CLKSRC_PLLB 0x40 |
| 123 | #define WM8580_AIF_CLKSRC_MCLK 0xc0 |
| 124 | |
| 125 | /* AIF control 2 (registers ch-eh) */ |
| 126 | #define WM8580_AIF_FMT_MASK 0x03 |
| 127 | #define WM8580_AIF_FMT_RIGHTJ 0x00 |
| 128 | #define WM8580_AIF_FMT_LEFTJ 0x01 |
| 129 | #define WM8580_AIF_FMT_I2S 0x02 |
| 130 | #define WM8580_AIF_FMT_DSP 0x03 |
| 131 | |
| 132 | #define WM8580_AIF_LENGTH_MASK 0x0c |
| 133 | #define WM8580_AIF_LENGTH_16 0x00 |
| 134 | #define WM8580_AIF_LENGTH_20 0x04 |
| 135 | #define WM8580_AIF_LENGTH_24 0x08 |
| 136 | #define WM8580_AIF_LENGTH_32 0x0c |
| 137 | |
| 138 | #define WM8580_AIF_LRP 0x10 |
| 139 | #define WM8580_AIF_BCP 0x20 |
| 140 | |
| 141 | /* Powerdown Register 1 (register 32h) */ |
| 142 | #define WM8580_PWRDN1_PWDN 0x001 |
| 143 | #define WM8580_PWRDN1_ALLDACPD 0x040 |
| 144 | |
| 145 | /* Powerdown Register 2 (register 33h) */ |
| 146 | #define WM8580_PWRDN2_OSSCPD 0x001 |
| 147 | #define WM8580_PWRDN2_PLLAPD 0x002 |
| 148 | #define WM8580_PWRDN2_PLLBPD 0x004 |
| 149 | #define WM8580_PWRDN2_SPDIFPD 0x008 |
| 150 | #define WM8580_PWRDN2_SPDIFTXD 0x010 |
| 151 | #define WM8580_PWRDN2_SPDIFRXD 0x020 |
| 152 | |
| 153 | #define WM8580_DAC_CONTROL5_MUTEALL 0x10 |
| 154 | |
| 155 | /* |
| 156 | * wm8580 register cache |
| 157 | * We can't read the WM8580 register space when we |
| 158 | * are using 2 wire for device control, so we cache them instead. |
| 159 | */ |
| 160 | static const u16 wm8580_reg[] = { |
| 161 | 0x0121, 0x017e, 0x007d, 0x0014, /*R3*/ |
| 162 | 0x0121, 0x017e, 0x007d, 0x0194, /*R7*/ |
Seungwhan Youn | a096862 | 2010-12-09 18:07:52 +0900 | [diff] [blame] | 163 | 0x0010, 0x0002, 0x0002, 0x00c2, /*R11*/ |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 164 | 0x0182, 0x0082, 0x000a, 0x0024, /*R15*/ |
| 165 | 0x0009, 0x0000, 0x00ff, 0x0000, /*R19*/ |
| 166 | 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R23*/ |
| 167 | 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R27*/ |
| 168 | 0x01f0, 0x0040, 0x0000, 0x0000, /*R31(0x1F)*/ |
| 169 | 0x0000, 0x0000, 0x0031, 0x000b, /*R35*/ |
| 170 | 0x0039, 0x0000, 0x0010, 0x0032, /*R39*/ |
| 171 | 0x0054, 0x0076, 0x0098, 0x0000, /*R43(0x2B)*/ |
| 172 | 0x0000, 0x0000, 0x0000, 0x0000, /*R47*/ |
| 173 | 0x0000, 0x0000, 0x005e, 0x003e, /*R51(0x33)*/ |
| 174 | 0x0000, 0x0000 /*R53*/ |
| 175 | }; |
| 176 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 177 | struct pll_state { |
| 178 | unsigned int in; |
| 179 | unsigned int out; |
| 180 | }; |
| 181 | |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 182 | #define WM8580_NUM_SUPPLIES 3 |
| 183 | static const char *wm8580_supply_names[WM8580_NUM_SUPPLIES] = { |
| 184 | "AVDD", |
| 185 | "DVDD", |
| 186 | "PVDD", |
| 187 | }; |
| 188 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 189 | /* codec private data */ |
| 190 | struct wm8580_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 191 | enum snd_soc_control_type control_type; |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 192 | struct regulator_bulk_data supplies[WM8580_NUM_SUPPLIES]; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 193 | struct pll_state a; |
| 194 | struct pll_state b; |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 195 | int sysclk[2]; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 198 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); |
| 199 | |
| 200 | static int wm8580_out_vu(struct snd_kcontrol *kcontrol, |
| 201 | struct snd_ctl_elem_value *ucontrol) |
| 202 | { |
Mark Brown | ccb077f | 2009-04-21 12:57:00 +0100 | [diff] [blame] | 203 | struct soc_mixer_control *mc = |
| 204 | (struct soc_mixer_control *)kcontrol->private_value; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 205 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 206 | u16 *reg_cache = codec->reg_cache; |
Mark Brown | ccb077f | 2009-04-21 12:57:00 +0100 | [diff] [blame] | 207 | unsigned int reg = mc->reg; |
| 208 | unsigned int reg2 = mc->rreg; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 209 | int ret; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 210 | |
| 211 | /* Clear the register cache so we write without VU set */ |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 212 | reg_cache[reg] = 0; |
| 213 | reg_cache[reg2] = 0; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 214 | |
Peter Ujfalusi | c4671a9 | 2011-10-06 09:59:12 +0300 | [diff] [blame] | 215 | ret = snd_soc_put_volsw(kcontrol, ucontrol); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | |
| 219 | /* Now write again with the volume update bit set */ |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 220 | snd_soc_update_bits(codec, reg, 0x100, 0x100); |
| 221 | snd_soc_update_bits(codec, reg2, 0x100, 0x100); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 226 | static const struct snd_kcontrol_new wm8580_snd_controls[] = { |
Peter Ujfalusi | 0f9887d | 2011-10-05 10:29:19 +0300 | [diff] [blame] | 227 | SOC_DOUBLE_R_EXT_TLV("DAC1 Playback Volume", |
| 228 | WM8580_DIGITAL_ATTENUATION_DACL1, |
| 229 | WM8580_DIGITAL_ATTENUATION_DACR1, |
Peter Ujfalusi | c4671a9 | 2011-10-06 09:59:12 +0300 | [diff] [blame] | 230 | 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv), |
Peter Ujfalusi | 0f9887d | 2011-10-05 10:29:19 +0300 | [diff] [blame] | 231 | SOC_DOUBLE_R_EXT_TLV("DAC2 Playback Volume", |
| 232 | WM8580_DIGITAL_ATTENUATION_DACL2, |
| 233 | WM8580_DIGITAL_ATTENUATION_DACR2, |
Peter Ujfalusi | c4671a9 | 2011-10-06 09:59:12 +0300 | [diff] [blame] | 234 | 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv), |
Peter Ujfalusi | 0f9887d | 2011-10-05 10:29:19 +0300 | [diff] [blame] | 235 | SOC_DOUBLE_R_EXT_TLV("DAC3 Playback Volume", |
| 236 | WM8580_DIGITAL_ATTENUATION_DACL3, |
| 237 | WM8580_DIGITAL_ATTENUATION_DACR3, |
Peter Ujfalusi | c4671a9 | 2011-10-06 09:59:12 +0300 | [diff] [blame] | 238 | 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv), |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 239 | |
| 240 | SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0), |
| 241 | SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0), |
| 242 | SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0), |
| 243 | |
| 244 | SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0), |
| 245 | SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0), |
| 246 | SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0), |
| 247 | |
| 248 | SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0), |
Mark Brown | 4f0ed9a | 2010-08-06 17:57:12 +0100 | [diff] [blame] | 249 | SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 1), |
| 250 | SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 1), |
| 251 | SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 1), |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 252 | |
Mark Brown | dacfe9f | 2010-08-13 20:01:32 +0100 | [diff] [blame] | 253 | SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 1), |
Mark Brown | 6f341d14 | 2010-08-06 18:14:09 +0100 | [diff] [blame] | 254 | SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0), |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 257 | static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = { |
| 258 | SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1), |
| 259 | SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1), |
| 260 | SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1), |
| 261 | |
| 262 | SND_SOC_DAPM_OUTPUT("VOUT1L"), |
| 263 | SND_SOC_DAPM_OUTPUT("VOUT1R"), |
| 264 | SND_SOC_DAPM_OUTPUT("VOUT2L"), |
| 265 | SND_SOC_DAPM_OUTPUT("VOUT2R"), |
| 266 | SND_SOC_DAPM_OUTPUT("VOUT3L"), |
| 267 | SND_SOC_DAPM_OUTPUT("VOUT3R"), |
| 268 | |
| 269 | SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1), |
| 270 | |
| 271 | SND_SOC_DAPM_INPUT("AINL"), |
| 272 | SND_SOC_DAPM_INPUT("AINR"), |
| 273 | }; |
| 274 | |
Mark Brown | f235c64 | 2011-12-03 11:42:01 +0000 | [diff] [blame] | 275 | static const struct snd_soc_dapm_route wm8580_dapm_routes[] = { |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 276 | { "VOUT1L", NULL, "DAC1" }, |
| 277 | { "VOUT1R", NULL, "DAC1" }, |
| 278 | |
| 279 | { "VOUT2L", NULL, "DAC2" }, |
| 280 | { "VOUT2R", NULL, "DAC2" }, |
| 281 | |
| 282 | { "VOUT3L", NULL, "DAC3" }, |
| 283 | { "VOUT3R", NULL, "DAC3" }, |
| 284 | |
| 285 | { "ADC", NULL, "AINL" }, |
| 286 | { "ADC", NULL, "AINR" }, |
| 287 | }; |
| 288 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 289 | /* PLL divisors */ |
| 290 | struct _pll_div { |
| 291 | u32 prescale:1; |
| 292 | u32 postscale:1; |
| 293 | u32 freqmode:2; |
| 294 | u32 n:4; |
| 295 | u32 k:24; |
| 296 | }; |
| 297 | |
| 298 | /* The size in bits of the pll divide */ |
| 299 | #define FIXED_PLL_SIZE (1 << 22) |
| 300 | |
| 301 | /* PLL rate to output rate divisions */ |
| 302 | static struct { |
| 303 | unsigned int div; |
| 304 | unsigned int freqmode; |
| 305 | unsigned int postscale; |
| 306 | } post_table[] = { |
| 307 | { 2, 0, 0 }, |
| 308 | { 4, 0, 1 }, |
| 309 | { 4, 1, 0 }, |
| 310 | { 8, 1, 1 }, |
| 311 | { 8, 2, 0 }, |
| 312 | { 16, 2, 1 }, |
| 313 | { 12, 3, 0 }, |
| 314 | { 24, 3, 1 } |
| 315 | }; |
| 316 | |
| 317 | static int pll_factors(struct _pll_div *pll_div, unsigned int target, |
| 318 | unsigned int source) |
| 319 | { |
| 320 | u64 Kpart; |
| 321 | unsigned int K, Ndiv, Nmod; |
| 322 | int i; |
| 323 | |
Roel Kluin | 449bd54 | 2009-05-27 17:08:39 -0700 | [diff] [blame] | 324 | pr_debug("wm8580: PLL %uHz->%uHz\n", source, target); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 325 | |
| 326 | /* Scale the output frequency up; the PLL should run in the |
| 327 | * region of 90-100MHz. |
| 328 | */ |
| 329 | for (i = 0; i < ARRAY_SIZE(post_table); i++) { |
| 330 | if (target * post_table[i].div >= 90000000 && |
| 331 | target * post_table[i].div <= 100000000) { |
| 332 | pll_div->freqmode = post_table[i].freqmode; |
| 333 | pll_div->postscale = post_table[i].postscale; |
| 334 | target *= post_table[i].div; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (i == ARRAY_SIZE(post_table)) { |
| 340 | printk(KERN_ERR "wm8580: Unable to scale output frequency " |
| 341 | "%u\n", target); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | Ndiv = target / source; |
| 346 | |
| 347 | if (Ndiv < 5) { |
| 348 | source /= 2; |
| 349 | pll_div->prescale = 1; |
| 350 | Ndiv = target / source; |
| 351 | } else |
| 352 | pll_div->prescale = 0; |
| 353 | |
| 354 | if ((Ndiv < 5) || (Ndiv > 13)) { |
| 355 | printk(KERN_ERR |
Roel Kluin | 449bd54 | 2009-05-27 17:08:39 -0700 | [diff] [blame] | 356 | "WM8580 N=%u outside supported range\n", Ndiv); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | pll_div->n = Ndiv; |
| 361 | Nmod = target % source; |
| 362 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 363 | |
| 364 | do_div(Kpart, source); |
| 365 | |
| 366 | K = Kpart & 0xFFFFFFFF; |
| 367 | |
| 368 | pll_div->k = K; |
| 369 | |
| 370 | pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n", |
| 371 | pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode, |
| 372 | pll_div->postscale); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 377 | static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
| 378 | int source, unsigned int freq_in, unsigned int freq_out) |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 379 | { |
| 380 | int offset; |
| 381 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 382 | struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 383 | struct pll_state *state; |
| 384 | struct _pll_div pll_div; |
| 385 | unsigned int reg; |
| 386 | unsigned int pwr_mask; |
| 387 | int ret; |
| 388 | |
| 389 | /* GCC isn't able to work out the ifs below for initialising/using |
| 390 | * pll_div so suppress warnings. |
| 391 | */ |
| 392 | memset(&pll_div, 0, sizeof(pll_div)); |
| 393 | |
| 394 | switch (pll_id) { |
| 395 | case WM8580_PLLA: |
| 396 | state = &wm8580->a; |
| 397 | offset = 0; |
| 398 | pwr_mask = WM8580_PWRDN2_PLLAPD; |
| 399 | break; |
| 400 | case WM8580_PLLB: |
| 401 | state = &wm8580->b; |
| 402 | offset = 4; |
| 403 | pwr_mask = WM8580_PWRDN2_PLLBPD; |
| 404 | break; |
| 405 | default: |
| 406 | return -ENODEV; |
| 407 | } |
| 408 | |
| 409 | if (freq_in && freq_out) { |
| 410 | ret = pll_factors(&pll_div, freq_out, freq_in); |
| 411 | if (ret != 0) |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | state->in = freq_in; |
| 416 | state->out = freq_out; |
| 417 | |
| 418 | /* Always disable the PLL - it is not safe to leave it running |
| 419 | * while reprogramming it. |
| 420 | */ |
Axel Lin | a6785d7 | 2011-10-17 11:50:46 +0800 | [diff] [blame] | 421 | snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, pwr_mask); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 422 | |
| 423 | if (!freq_in || !freq_out) |
| 424 | return 0; |
| 425 | |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 426 | snd_soc_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff); |
jassi brar | 5c0d38c | 2009-09-01 11:35:08 +0900 | [diff] [blame] | 427 | snd_soc_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0x1ff); |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 428 | snd_soc_write(codec, WM8580_PLLA3 + offset, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 429 | (pll_div.k >> 18 & 0xf) | (pll_div.n << 4)); |
| 430 | |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 431 | reg = snd_soc_read(codec, WM8580_PLLA4 + offset); |
jassi brar | 5c0d38c | 2009-09-01 11:35:08 +0900 | [diff] [blame] | 432 | reg &= ~0x1b; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 433 | reg |= pll_div.prescale | pll_div.postscale << 1 | |
Mark Brown | ce88168 | 2009-04-21 12:35:15 +0100 | [diff] [blame] | 434 | pll_div.freqmode << 3; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 435 | |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 436 | snd_soc_write(codec, WM8580_PLLA4 + offset, reg); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 437 | |
| 438 | /* All done, turn it on */ |
Axel Lin | a6785d7 | 2011-10-17 11:50:46 +0800 | [diff] [blame] | 439 | snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, 0); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 444 | static const int wm8580_sysclk_ratios[] = { |
| 445 | 128, 192, 256, 384, 512, 768, 1152, |
| 446 | }; |
| 447 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 448 | /* |
| 449 | * Set PCM DAI bit size and sample rate. |
| 450 | */ |
| 451 | static int wm8580_paif_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 452 | struct snd_pcm_hw_params *params, |
| 453 | struct snd_soc_dai *dai) |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 454 | { |
| 455 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 456 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 457 | struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec); |
| 458 | u16 paifa = 0; |
Mark Brown | e231cab | 2010-08-13 17:57:28 +0100 | [diff] [blame] | 459 | u16 paifb = 0; |
Mark Brown | 6bfb6aa | 2010-08-13 20:08:55 +0100 | [diff] [blame] | 460 | int i, ratio, osr; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 461 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 462 | /* bit size */ |
| 463 | switch (params_format(params)) { |
| 464 | case SNDRV_PCM_FORMAT_S16_LE: |
Mark Brown | ba2772e | 2010-08-13 19:36:56 +0100 | [diff] [blame] | 465 | paifa |= 0x8; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 466 | break; |
| 467 | case SNDRV_PCM_FORMAT_S20_3LE: |
Jassi Brar | 6b46432 | 2010-12-07 19:23:07 +0900 | [diff] [blame] | 468 | paifa |= 0x0; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 469 | paifb |= WM8580_AIF_LENGTH_20; |
| 470 | break; |
| 471 | case SNDRV_PCM_FORMAT_S24_LE: |
Jassi Brar | 6b46432 | 2010-12-07 19:23:07 +0900 | [diff] [blame] | 472 | paifa |= 0x0; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 473 | paifb |= WM8580_AIF_LENGTH_24; |
| 474 | break; |
| 475 | case SNDRV_PCM_FORMAT_S32_LE: |
Jassi Brar | 6b46432 | 2010-12-07 19:23:07 +0900 | [diff] [blame] | 476 | paifa |= 0x0; |
| 477 | paifb |= WM8580_AIF_LENGTH_32; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 478 | break; |
| 479 | default: |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 483 | /* Look up the SYSCLK ratio; accept only exact matches */ |
Jassi Brar | 0628693 | 2010-12-03 10:34:25 +0900 | [diff] [blame] | 484 | ratio = wm8580->sysclk[dai->driver->id] / params_rate(params); |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 485 | for (i = 0; i < ARRAY_SIZE(wm8580_sysclk_ratios); i++) |
| 486 | if (ratio == wm8580_sysclk_ratios[i]) |
| 487 | break; |
| 488 | if (i == ARRAY_SIZE(wm8580_sysclk_ratios)) { |
| 489 | dev_err(codec->dev, "Invalid clock ratio %d/%d\n", |
Jassi Brar | 0628693 | 2010-12-03 10:34:25 +0900 | [diff] [blame] | 490 | wm8580->sysclk[dai->driver->id], params_rate(params)); |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | } |
| 493 | paifa |= i; |
| 494 | dev_dbg(codec->dev, "Running at %dfs with %dHz clock\n", |
| 495 | wm8580_sysclk_ratios[i], wm8580->sysclk[dai->driver->id]); |
| 496 | |
Mark Brown | 6bfb6aa | 2010-08-13 20:08:55 +0100 | [diff] [blame] | 497 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 498 | switch (ratio) { |
| 499 | case 128: |
| 500 | case 192: |
| 501 | osr = WM8580_DACOSR; |
| 502 | dev_dbg(codec->dev, "Selecting 64x OSR\n"); |
| 503 | break; |
| 504 | default: |
| 505 | osr = 0; |
| 506 | dev_dbg(codec->dev, "Selecting 128x OSR\n"); |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | snd_soc_update_bits(codec, WM8580_PAIF3, WM8580_DACOSR, osr); |
| 511 | } |
| 512 | |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 513 | snd_soc_update_bits(codec, WM8580_PAIF1 + dai->driver->id, |
Mark Brown | ba2772e | 2010-08-13 19:36:56 +0100 | [diff] [blame] | 514 | WM8580_AIF_RATE_MASK | WM8580_AIF_BCLKSEL_MASK, |
| 515 | paifa); |
Mark Brown | e231cab | 2010-08-13 17:57:28 +0100 | [diff] [blame] | 516 | snd_soc_update_bits(codec, WM8580_PAIF3 + dai->driver->id, |
| 517 | WM8580_AIF_LENGTH_MASK, paifb); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai, |
| 522 | unsigned int fmt) |
| 523 | { |
| 524 | struct snd_soc_codec *codec = codec_dai->codec; |
| 525 | unsigned int aifa; |
| 526 | unsigned int aifb; |
| 527 | int can_invert_lrclk; |
| 528 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 529 | aifa = snd_soc_read(codec, WM8580_PAIF1 + codec_dai->driver->id); |
| 530 | aifb = snd_soc_read(codec, WM8580_PAIF3 + codec_dai->driver->id); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 531 | |
| 532 | aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP); |
| 533 | |
| 534 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 535 | case SND_SOC_DAIFMT_CBS_CFS: |
| 536 | aifa &= ~WM8580_AIF_MS; |
| 537 | break; |
| 538 | case SND_SOC_DAIFMT_CBM_CFM: |
| 539 | aifa |= WM8580_AIF_MS; |
| 540 | break; |
| 541 | default: |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
| 545 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 546 | case SND_SOC_DAIFMT_I2S: |
| 547 | can_invert_lrclk = 1; |
| 548 | aifb |= WM8580_AIF_FMT_I2S; |
| 549 | break; |
| 550 | case SND_SOC_DAIFMT_RIGHT_J: |
| 551 | can_invert_lrclk = 1; |
| 552 | aifb |= WM8580_AIF_FMT_RIGHTJ; |
| 553 | break; |
| 554 | case SND_SOC_DAIFMT_LEFT_J: |
| 555 | can_invert_lrclk = 1; |
| 556 | aifb |= WM8580_AIF_FMT_LEFTJ; |
| 557 | break; |
| 558 | case SND_SOC_DAIFMT_DSP_A: |
| 559 | can_invert_lrclk = 0; |
| 560 | aifb |= WM8580_AIF_FMT_DSP; |
| 561 | break; |
| 562 | case SND_SOC_DAIFMT_DSP_B: |
| 563 | can_invert_lrclk = 0; |
| 564 | aifb |= WM8580_AIF_FMT_DSP; |
| 565 | aifb |= WM8580_AIF_LRP; |
| 566 | break; |
| 567 | default: |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
| 571 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 572 | case SND_SOC_DAIFMT_NB_NF: |
| 573 | break; |
| 574 | |
| 575 | case SND_SOC_DAIFMT_IB_IF: |
| 576 | if (!can_invert_lrclk) |
| 577 | return -EINVAL; |
| 578 | aifb |= WM8580_AIF_BCP; |
| 579 | aifb |= WM8580_AIF_LRP; |
| 580 | break; |
| 581 | |
| 582 | case SND_SOC_DAIFMT_IB_NF: |
| 583 | aifb |= WM8580_AIF_BCP; |
| 584 | break; |
| 585 | |
| 586 | case SND_SOC_DAIFMT_NB_IF: |
| 587 | if (!can_invert_lrclk) |
| 588 | return -EINVAL; |
| 589 | aifb |= WM8580_AIF_LRP; |
| 590 | break; |
| 591 | |
| 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 596 | snd_soc_write(codec, WM8580_PAIF1 + codec_dai->driver->id, aifa); |
| 597 | snd_soc_write(codec, WM8580_PAIF3 + codec_dai->driver->id, aifb); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
| 603 | int div_id, int div) |
| 604 | { |
| 605 | struct snd_soc_codec *codec = codec_dai->codec; |
| 606 | unsigned int reg; |
| 607 | |
| 608 | switch (div_id) { |
| 609 | case WM8580_MCLK: |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 610 | reg = snd_soc_read(codec, WM8580_PLLB4); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 611 | reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK; |
| 612 | |
| 613 | switch (div) { |
| 614 | case WM8580_CLKSRC_MCLK: |
| 615 | /* Input */ |
| 616 | break; |
| 617 | |
| 618 | case WM8580_CLKSRC_PLLA: |
| 619 | reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA; |
| 620 | break; |
| 621 | case WM8580_CLKSRC_PLLB: |
| 622 | reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB; |
| 623 | break; |
| 624 | |
| 625 | case WM8580_CLKSRC_OSC: |
| 626 | reg |= WM8580_PLLB4_MCLKOUTSRC_OSC; |
| 627 | break; |
| 628 | |
| 629 | default: |
| 630 | return -EINVAL; |
| 631 | } |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 632 | snd_soc_write(codec, WM8580_PLLB4, reg); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 633 | break; |
| 634 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 635 | case WM8580_CLKOUTSRC: |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 636 | reg = snd_soc_read(codec, WM8580_PLLB4); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 637 | reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK; |
| 638 | |
| 639 | switch (div) { |
| 640 | case WM8580_CLKSRC_NONE: |
| 641 | break; |
| 642 | |
| 643 | case WM8580_CLKSRC_PLLA: |
| 644 | reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK; |
| 645 | break; |
| 646 | |
| 647 | case WM8580_CLKSRC_PLLB: |
| 648 | reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK; |
| 649 | break; |
| 650 | |
| 651 | case WM8580_CLKSRC_OSC: |
| 652 | reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK; |
| 653 | break; |
| 654 | |
| 655 | default: |
| 656 | return -EINVAL; |
| 657 | } |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 658 | snd_soc_write(codec, WM8580_PLLB4, reg); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 659 | break; |
| 660 | |
| 661 | default: |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 668 | static int wm8580_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 669 | unsigned int freq, int dir) |
| 670 | { |
| 671 | struct snd_soc_codec *codec = dai->codec; |
| 672 | struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | 748b217 | 2011-12-30 23:36:23 +0800 | [diff] [blame] | 673 | int ret, sel, sel_mask, sel_shift; |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 674 | |
| 675 | switch (dai->driver->id) { |
Mark Brown | c25edef | 2010-08-16 19:26:41 +0100 | [diff] [blame] | 676 | case WM8580_DAI_PAIFRX: |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 677 | sel_mask = 0x3; |
| 678 | sel_shift = 0; |
| 679 | break; |
| 680 | |
Mark Brown | c25edef | 2010-08-16 19:26:41 +0100 | [diff] [blame] | 681 | case WM8580_DAI_PAIFTX: |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 682 | sel_mask = 0xc; |
| 683 | sel_shift = 2; |
| 684 | break; |
| 685 | |
| 686 | default: |
| 687 | BUG_ON("Unknown DAI driver ID\n"); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | |
| 691 | switch (clk_id) { |
| 692 | case WM8580_CLKSRC_ADCMCLK: |
Jassi Brar | 0628693 | 2010-12-03 10:34:25 +0900 | [diff] [blame] | 693 | if (dai->driver->id != WM8580_DAI_PAIFTX) |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 694 | return -EINVAL; |
| 695 | sel = 0 << sel_shift; |
| 696 | break; |
| 697 | case WM8580_CLKSRC_PLLA: |
| 698 | sel = 1 << sel_shift; |
| 699 | break; |
| 700 | case WM8580_CLKSRC_PLLB: |
| 701 | sel = 2 << sel_shift; |
| 702 | break; |
| 703 | case WM8580_CLKSRC_MCLK: |
| 704 | sel = 3 << sel_shift; |
| 705 | break; |
| 706 | default: |
| 707 | dev_err(codec->dev, "Unknown clock %d\n", clk_id); |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | /* We really should validate PLL settings but not yet */ |
Jassi Brar | 0628693 | 2010-12-03 10:34:25 +0900 | [diff] [blame] | 712 | wm8580->sysclk[dai->driver->id] = freq; |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 713 | |
Axel Lin | 748b217 | 2011-12-30 23:36:23 +0800 | [diff] [blame] | 714 | ret = snd_soc_update_bits(codec, WM8580_CLKSEL, sel_mask, sel); |
| 715 | if (ret < 0) |
| 716 | return ret; |
| 717 | |
| 718 | return 0; |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 721 | static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute) |
| 722 | { |
| 723 | struct snd_soc_codec *codec = codec_dai->codec; |
| 724 | unsigned int reg; |
| 725 | |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 726 | reg = snd_soc_read(codec, WM8580_DAC_CONTROL5); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 727 | |
| 728 | if (mute) |
| 729 | reg |= WM8580_DAC_CONTROL5_MUTEALL; |
| 730 | else |
| 731 | reg &= ~WM8580_DAC_CONTROL5_MUTEALL; |
| 732 | |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 733 | snd_soc_write(codec, WM8580_DAC_CONTROL5, reg); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static int wm8580_set_bias_level(struct snd_soc_codec *codec, |
| 739 | enum snd_soc_bias_level level) |
| 740 | { |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 741 | switch (level) { |
| 742 | case SND_SOC_BIAS_ON: |
| 743 | case SND_SOC_BIAS_PREPARE: |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 744 | break; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 745 | |
| 746 | case SND_SOC_BIAS_STANDBY: |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 747 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 748 | /* Power up and get individual control of the DACs */ |
Axel Lin | a6785d7 | 2011-10-17 11:50:46 +0800 | [diff] [blame] | 749 | snd_soc_update_bits(codec, WM8580_PWRDN1, |
| 750 | WM8580_PWRDN1_PWDN | |
| 751 | WM8580_PWRDN1_ALLDACPD, 0); |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 752 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 753 | /* Make VMID high impedance */ |
Axel Lin | a6785d7 | 2011-10-17 11:50:46 +0800 | [diff] [blame] | 754 | snd_soc_update_bits(codec, WM8580_ADC_CONTROL1, |
| 755 | 0x100, 0); |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 756 | } |
| 757 | break; |
| 758 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 759 | case SND_SOC_BIAS_OFF: |
Axel Lin | a6785d7 | 2011-10-17 11:50:46 +0800 | [diff] [blame] | 760 | snd_soc_update_bits(codec, WM8580_PWRDN1, |
| 761 | WM8580_PWRDN1_PWDN, WM8580_PWRDN1_PWDN); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 762 | break; |
| 763 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 764 | codec->dapm.bias_level = level; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 769 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 770 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 771 | static const struct snd_soc_dai_ops wm8580_dai_ops_playback = { |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 772 | .set_sysclk = wm8580_set_sysclk, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 773 | .hw_params = wm8580_paif_hw_params, |
| 774 | .set_fmt = wm8580_set_paif_dai_fmt, |
| 775 | .set_clkdiv = wm8580_set_dai_clkdiv, |
| 776 | .set_pll = wm8580_set_dai_pll, |
| 777 | .digital_mute = wm8580_digital_mute, |
| 778 | }; |
| 779 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 780 | static const struct snd_soc_dai_ops wm8580_dai_ops_capture = { |
Mark Brown | c5607d8 | 2010-08-13 19:05:04 +0100 | [diff] [blame] | 781 | .set_sysclk = wm8580_set_sysclk, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 782 | .hw_params = wm8580_paif_hw_params, |
| 783 | .set_fmt = wm8580_set_paif_dai_fmt, |
| 784 | .set_clkdiv = wm8580_set_dai_clkdiv, |
| 785 | .set_pll = wm8580_set_dai_pll, |
| 786 | }; |
| 787 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 788 | static struct snd_soc_dai_driver wm8580_dai[] = { |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 789 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 790 | .name = "wm8580-hifi-playback", |
| 791 | .id = WM8580_DAI_PAIFRX, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 792 | .playback = { |
| 793 | .stream_name = "Playback", |
| 794 | .channels_min = 1, |
| 795 | .channels_max = 6, |
| 796 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 797 | .formats = WM8580_FORMATS, |
| 798 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 799 | .ops = &wm8580_dai_ops_playback, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 800 | }, |
| 801 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 802 | .name = "wm8580-hifi-capture", |
| 803 | .id = WM8580_DAI_PAIFTX, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 804 | .capture = { |
| 805 | .stream_name = "Capture", |
| 806 | .channels_min = 2, |
| 807 | .channels_max = 2, |
| 808 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 809 | .formats = WM8580_FORMATS, |
| 810 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 811 | .ops = &wm8580_dai_ops_capture, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 812 | }, |
| 813 | }; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 814 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 815 | static int wm8580_probe(struct snd_soc_codec *codec) |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 816 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 817 | struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec); |
| 818 | int ret = 0,i; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 819 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 820 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8580->control_type); |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 821 | if (ret < 0) { |
| 822 | 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] | 823 | return ret; |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 826 | for (i = 0; i < ARRAY_SIZE(wm8580->supplies); i++) |
| 827 | wm8580->supplies[i].supply = wm8580_supply_names[i]; |
| 828 | |
| 829 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8580->supplies), |
| 830 | wm8580->supplies); |
| 831 | if (ret != 0) { |
| 832 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 833 | return ret; |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8580->supplies), |
| 837 | wm8580->supplies); |
| 838 | if (ret != 0) { |
| 839 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 840 | goto err_regulator_get; |
| 841 | } |
| 842 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 843 | /* Get the codec into a known state */ |
Mark Brown | f6f1eb1 | 2009-07-05 17:57:57 +0100 | [diff] [blame] | 844 | ret = snd_soc_write(codec, WM8580_RESET, 0); |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 845 | if (ret != 0) { |
| 846 | dev_err(codec->dev, "Failed to reset codec: %d\n", ret); |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 847 | goto err_regulator_enable; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 850 | wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 851 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 852 | return 0; |
| 853 | |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 854 | err_regulator_enable: |
| 855 | regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies); |
| 856 | err_regulator_get: |
| 857 | regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies); |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 858 | return ret; |
| 859 | } |
| 860 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 861 | /* power down chip */ |
| 862 | static int wm8580_remove(struct snd_soc_codec *codec) |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 863 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 864 | struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec); |
| 865 | |
| 866 | wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 867 | |
Mark Brown | a583cd5 | 2009-06-17 17:30:14 +0100 | [diff] [blame] | 868 | regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies); |
| 869 | regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 870 | |
| 871 | return 0; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 872 | } |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 873 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 874 | static struct snd_soc_codec_driver soc_codec_dev_wm8580 = { |
| 875 | .probe = wm8580_probe, |
| 876 | .remove = wm8580_remove, |
| 877 | .set_bias_level = wm8580_set_bias_level, |
Dimitris Papastamos | e5eec34 | 2010-09-10 18:14:56 +0100 | [diff] [blame] | 878 | .reg_cache_size = ARRAY_SIZE(wm8580_reg), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 879 | .reg_word_size = sizeof(u16), |
Dimitris Papastamos | 0aa34b1 | 2010-11-08 10:41:53 +0000 | [diff] [blame] | 880 | .reg_cache_default = wm8580_reg, |
Mark Brown | f235c64 | 2011-12-03 11:42:01 +0000 | [diff] [blame] | 881 | |
| 882 | .controls = wm8580_snd_controls, |
| 883 | .num_controls = ARRAY_SIZE(wm8580_snd_controls), |
| 884 | .dapm_widgets = wm8580_dapm_widgets, |
| 885 | .num_dapm_widgets = ARRAY_SIZE(wm8580_dapm_widgets), |
| 886 | .dapm_routes = wm8580_dapm_routes, |
| 887 | .num_dapm_routes = ARRAY_SIZE(wm8580_dapm_routes), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 888 | }; |
| 889 | |
Mark Brown | 733eef3 | 2011-08-02 13:22:36 +0900 | [diff] [blame] | 890 | static const struct of_device_id wm8580_of_match[] = { |
| 891 | { .compatible = "wlf,wm8580" }, |
| 892 | { }, |
| 893 | }; |
| 894 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 895 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Jean Delvare | 17be552 | 2008-10-15 19:57:12 +0200 | [diff] [blame] | 896 | static int wm8580_i2c_probe(struct i2c_client *i2c, |
| 897 | const struct i2c_device_id *id) |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 898 | { |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 899 | struct wm8580_priv *wm8580; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 900 | int ret; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 901 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 902 | wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL); |
| 903 | if (wm8580 == NULL) |
| 904 | return -ENOMEM; |
| 905 | |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 906 | i2c_set_clientdata(i2c, wm8580); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 907 | wm8580->control_type = SND_SOC_I2C; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 908 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 909 | ret = snd_soc_register_codec(&i2c->dev, |
| 910 | &soc_codec_dev_wm8580, wm8580_dai, ARRAY_SIZE(wm8580_dai)); |
| 911 | if (ret < 0) |
| 912 | kfree(wm8580); |
| 913 | return ret; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Jean Delvare | 17be552 | 2008-10-15 19:57:12 +0200 | [diff] [blame] | 916 | static int wm8580_i2c_remove(struct i2c_client *client) |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 917 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 918 | snd_soc_unregister_codec(&client->dev); |
| 919 | kfree(i2c_get_clientdata(client)); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
Jean Delvare | 17be552 | 2008-10-15 19:57:12 +0200 | [diff] [blame] | 923 | static const struct i2c_device_id wm8580_i2c_id[] = { |
| 924 | { "wm8580", 0 }, |
| 925 | { } |
| 926 | }; |
| 927 | MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id); |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 928 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 929 | static struct i2c_driver wm8580_i2c_driver = { |
| 930 | .driver = { |
Mark Brown | 722d0da | 2011-08-02 13:21:53 +0900 | [diff] [blame] | 931 | .name = "wm8580", |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 932 | .owner = THIS_MODULE, |
Mark Brown | 733eef3 | 2011-08-02 13:22:36 +0900 | [diff] [blame] | 933 | .of_match_table = wm8580_of_match, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 934 | }, |
Jean Delvare | 17be552 | 2008-10-15 19:57:12 +0200 | [diff] [blame] | 935 | .probe = wm8580_i2c_probe, |
| 936 | .remove = wm8580_i2c_remove, |
| 937 | .id_table = wm8580_i2c_id, |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 938 | }; |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 939 | #endif |
| 940 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 941 | static int __init wm8580_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 942 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 943 | int ret = 0; |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 944 | |
| 945 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 946 | ret = i2c_add_driver(&wm8580_i2c_driver); |
| 947 | if (ret != 0) { |
| 948 | pr_err("Failed to register WM8580 I2C driver: %d\n", ret); |
| 949 | } |
| 950 | #endif |
| 951 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 952 | return ret; |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 953 | } |
| 954 | module_init(wm8580_modinit); |
| 955 | |
| 956 | static void __exit wm8580_exit(void) |
| 957 | { |
Mark Brown | 6f7cb44 | 2009-03-11 18:31:08 +0000 | [diff] [blame] | 958 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 959 | i2c_del_driver(&wm8580_i2c_driver); |
| 960 | #endif |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 961 | } |
| 962 | module_exit(wm8580_exit); |
| 963 | |
Mark Brown | e88ba01 | 2008-08-06 13:18:26 +0100 | [diff] [blame] | 964 | MODULE_DESCRIPTION("ASoC WM8580 driver"); |
| 965 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 966 | MODULE_LICENSE("GPL"); |