Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Texas Instruments TLV320AIC26 low power audio CODEC |
| 3 | * ALSA SoC CODEC driver |
| 4 | * |
| 5 | * Copyright (C) 2008 Secret Lab Technologies Ltd. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/moduleparam.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/pm.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/sysfs.h> |
| 15 | #include <linux/spi/spi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 17 | #include <sound/core.h> |
| 18 | #include <sound/pcm.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include <sound/soc.h> |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 21 | #include <sound/initval.h> |
| 22 | |
| 23 | #include "tlv320aic26.h" |
| 24 | |
| 25 | MODULE_DESCRIPTION("ASoC TLV320AIC26 codec driver"); |
| 26 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
| 29 | /* AIC26 driver private data */ |
| 30 | struct aic26 { |
| 31 | struct spi_device *spi; |
Lars-Peter Clausen | f8f1179 | 2013-08-06 13:39:29 +0200 | [diff] [blame] | 32 | struct snd_soc_codec *codec; |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 33 | int master; |
| 34 | int datfm; |
| 35 | int mclk; |
| 36 | |
| 37 | /* Keyclick parameters */ |
| 38 | int keyclick_amplitude; |
| 39 | int keyclick_freq; |
| 40 | int keyclick_len; |
| 41 | }; |
| 42 | |
| 43 | /* --------------------------------------------------------------------- |
| 44 | * Register access routines |
| 45 | */ |
| 46 | static unsigned int aic26_reg_read(struct snd_soc_codec *codec, |
| 47 | unsigned int reg) |
| 48 | { |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 49 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 50 | u16 *cache = codec->reg_cache; |
| 51 | u16 cmd, value; |
| 52 | u8 buffer[2]; |
| 53 | int rc; |
| 54 | |
| 55 | if (reg >= AIC26_NUM_REGS) { |
| 56 | WARN_ON_ONCE(1); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | /* Do SPI transfer; first 16bits are command; remaining is |
| 61 | * register contents */ |
| 62 | cmd = AIC26_READ_COMMAND_WORD(reg); |
| 63 | buffer[0] = (cmd >> 8) & 0xff; |
| 64 | buffer[1] = cmd & 0xff; |
| 65 | rc = spi_write_then_read(aic26->spi, buffer, 2, buffer, 2); |
| 66 | if (rc) { |
| 67 | dev_err(&aic26->spi->dev, "AIC26 reg read error\n"); |
| 68 | return -EIO; |
| 69 | } |
| 70 | value = (buffer[0] << 8) | buffer[1]; |
| 71 | |
| 72 | /* Update the cache before returning with the value */ |
| 73 | cache[reg] = value; |
| 74 | return value; |
| 75 | } |
| 76 | |
| 77 | static unsigned int aic26_reg_read_cache(struct snd_soc_codec *codec, |
| 78 | unsigned int reg) |
| 79 | { |
| 80 | u16 *cache = codec->reg_cache; |
| 81 | |
| 82 | if (reg >= AIC26_NUM_REGS) { |
| 83 | WARN_ON_ONCE(1); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | return cache[reg]; |
| 88 | } |
| 89 | |
| 90 | static int aic26_reg_write(struct snd_soc_codec *codec, unsigned int reg, |
| 91 | unsigned int value) |
| 92 | { |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 93 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 94 | u16 *cache = codec->reg_cache; |
| 95 | u16 cmd; |
| 96 | u8 buffer[4]; |
| 97 | int rc; |
| 98 | |
| 99 | if (reg >= AIC26_NUM_REGS) { |
| 100 | WARN_ON_ONCE(1); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | /* Do SPI transfer; first 16bits are command; remaining is data |
| 105 | * to write into register */ |
| 106 | cmd = AIC26_WRITE_COMMAND_WORD(reg); |
| 107 | buffer[0] = (cmd >> 8) & 0xff; |
| 108 | buffer[1] = cmd & 0xff; |
| 109 | buffer[2] = value >> 8; |
| 110 | buffer[3] = value; |
| 111 | rc = spi_write(aic26->spi, buffer, 4); |
| 112 | if (rc) { |
| 113 | dev_err(&aic26->spi->dev, "AIC26 reg read error\n"); |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
| 117 | /* update cache before returning */ |
| 118 | cache[reg] = value; |
| 119 | return 0; |
| 120 | } |
| 121 | |
Mark Brown | 4a11bc2 | 2013-08-16 12:24:47 +0100 | [diff] [blame] | 122 | static const struct snd_soc_dapm_widget tlv320aic26_dapm_widgets[] = { |
| 123 | SND_SOC_DAPM_INPUT("MICIN"), |
| 124 | SND_SOC_DAPM_INPUT("AUX"), |
| 125 | |
| 126 | SND_SOC_DAPM_OUTPUT("HPL"), |
| 127 | SND_SOC_DAPM_OUTPUT("HPR"), |
| 128 | }; |
| 129 | |
| 130 | static const struct snd_soc_dapm_route tlv320aic26_dapm_routes[] = { |
| 131 | { "Capture", NULL, "MICIN" }, |
| 132 | { "Capture", NULL, "AUX" }, |
| 133 | |
| 134 | { "HPL", NULL, "Playback" }, |
| 135 | { "HPR", NULL, "Playback" }, |
| 136 | }; |
| 137 | |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 138 | /* --------------------------------------------------------------------- |
| 139 | * Digital Audio Interface Operations |
| 140 | */ |
| 141 | static int aic26_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 142 | struct snd_pcm_hw_params *params, |
| 143 | struct snd_soc_dai *dai) |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 144 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 145 | struct snd_soc_codec *codec = dai->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 146 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 147 | int fsref, divisor, wlen, pval, jval, dval, qval; |
| 148 | u16 reg; |
| 149 | |
| 150 | dev_dbg(&aic26->spi->dev, "aic26_hw_params(substream=%p, params=%p)\n", |
| 151 | substream, params); |
| 152 | dev_dbg(&aic26->spi->dev, "rate=%i format=%i\n", params_rate(params), |
| 153 | params_format(params)); |
| 154 | |
| 155 | switch (params_rate(params)) { |
| 156 | case 8000: fsref = 48000; divisor = AIC26_DIV_6; break; |
| 157 | case 11025: fsref = 44100; divisor = AIC26_DIV_4; break; |
| 158 | case 12000: fsref = 48000; divisor = AIC26_DIV_4; break; |
| 159 | case 16000: fsref = 48000; divisor = AIC26_DIV_3; break; |
| 160 | case 22050: fsref = 44100; divisor = AIC26_DIV_2; break; |
| 161 | case 24000: fsref = 48000; divisor = AIC26_DIV_2; break; |
| 162 | case 32000: fsref = 48000; divisor = AIC26_DIV_1_5; break; |
| 163 | case 44100: fsref = 44100; divisor = AIC26_DIV_1; break; |
| 164 | case 48000: fsref = 48000; divisor = AIC26_DIV_1; break; |
| 165 | default: |
| 166 | dev_dbg(&aic26->spi->dev, "bad rate\n"); return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | /* select data word length */ |
| 170 | switch (params_format(params)) { |
| 171 | case SNDRV_PCM_FORMAT_S8: wlen = AIC26_WLEN_16; break; |
| 172 | case SNDRV_PCM_FORMAT_S16_BE: wlen = AIC26_WLEN_16; break; |
| 173 | case SNDRV_PCM_FORMAT_S24_BE: wlen = AIC26_WLEN_24; break; |
| 174 | case SNDRV_PCM_FORMAT_S32_BE: wlen = AIC26_WLEN_32; break; |
| 175 | default: |
| 176 | dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL; |
| 177 | } |
| 178 | |
Michael Williamson | 2aba76f | 2011-05-20 10:26:06 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Configure PLL |
| 181 | * fsref = (mclk * PLLM) / 2048 |
| 182 | * where PLLM = J.DDDD (DDDD register ranges from 0 to 9999, decimal) |
| 183 | */ |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 184 | pval = 1; |
Michael Williamson | 2aba76f | 2011-05-20 10:26:06 -0400 | [diff] [blame] | 185 | /* compute J portion of multiplier */ |
| 186 | jval = fsref / (aic26->mclk / 2048); |
| 187 | /* compute fractional DDDD component of multiplier */ |
| 188 | dval = fsref - (jval * (aic26->mclk / 2048)); |
| 189 | dval = (10000 * dval) / (aic26->mclk / 2048); |
| 190 | dev_dbg(&aic26->spi->dev, "Setting PLLM to %d.%04d\n", jval, dval); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 191 | qval = 0; |
| 192 | reg = 0x8000 | qval << 11 | pval << 8 | jval << 2; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 193 | snd_soc_write(codec, AIC26_REG_PLL_PROG1, reg); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 194 | reg = dval << 2; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 195 | snd_soc_write(codec, AIC26_REG_PLL_PROG2, reg); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 196 | |
| 197 | /* Audio Control 3 (master mode, fsref rate) */ |
| 198 | reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL3); |
| 199 | reg &= ~0xf800; |
| 200 | if (aic26->master) |
| 201 | reg |= 0x0800; |
| 202 | if (fsref == 48000) |
| 203 | reg |= 0x2000; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 204 | snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 205 | |
| 206 | /* Audio Control 1 (FSref divisor) */ |
| 207 | reg = aic26_reg_read_cache(codec, AIC26_REG_AUDIO_CTRL1); |
| 208 | reg &= ~0x0fff; |
| 209 | reg |= wlen | aic26->datfm | (divisor << 3) | divisor; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 210 | snd_soc_write(codec, AIC26_REG_AUDIO_CTRL1, reg); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * aic26_mute - Mute control to reduce noise when changing audio format |
| 217 | */ |
| 218 | static int aic26_mute(struct snd_soc_dai *dai, int mute) |
| 219 | { |
| 220 | struct snd_soc_codec *codec = dai->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 221 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 222 | u16 reg = aic26_reg_read_cache(codec, AIC26_REG_DAC_GAIN); |
| 223 | |
| 224 | dev_dbg(&aic26->spi->dev, "aic26_mute(dai=%p, mute=%i)\n", |
| 225 | dai, mute); |
| 226 | |
| 227 | if (mute) |
| 228 | reg |= 0x8080; |
| 229 | else |
| 230 | reg &= ~0x8080; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 231 | snd_soc_write(codec, AIC26_REG_DAC_GAIN, reg); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int aic26_set_sysclk(struct snd_soc_dai *codec_dai, |
| 237 | int clk_id, unsigned int freq, int dir) |
| 238 | { |
| 239 | struct snd_soc_codec *codec = codec_dai->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 240 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 241 | |
| 242 | dev_dbg(&aic26->spi->dev, "aic26_set_sysclk(dai=%p, clk_id==%i," |
| 243 | " freq=%i, dir=%i)\n", |
| 244 | codec_dai, clk_id, freq, dir); |
| 245 | |
| 246 | /* MCLK needs to fall between 2MHz and 50 MHz */ |
| 247 | if ((freq < 2000000) || (freq > 50000000)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | aic26->mclk = freq; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int aic26_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) |
| 255 | { |
| 256 | struct snd_soc_codec *codec = codec_dai->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 257 | struct aic26 *aic26 = snd_soc_codec_get_drvdata(codec); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 258 | |
| 259 | dev_dbg(&aic26->spi->dev, "aic26_set_fmt(dai=%p, fmt==%i)\n", |
| 260 | codec_dai, fmt); |
| 261 | |
| 262 | /* set master/slave audio interface */ |
| 263 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 264 | case SND_SOC_DAIFMT_CBM_CFM: aic26->master = 1; break; |
| 265 | case SND_SOC_DAIFMT_CBS_CFS: aic26->master = 0; break; |
| 266 | default: |
| 267 | dev_dbg(&aic26->spi->dev, "bad master\n"); return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | /* interface format */ |
| 271 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 272 | case SND_SOC_DAIFMT_I2S: aic26->datfm = AIC26_DATFM_I2S; break; |
| 273 | case SND_SOC_DAIFMT_DSP_A: aic26->datfm = AIC26_DATFM_DSP; break; |
| 274 | case SND_SOC_DAIFMT_RIGHT_J: aic26->datfm = AIC26_DATFM_RIGHTJ; break; |
| 275 | case SND_SOC_DAIFMT_LEFT_J: aic26->datfm = AIC26_DATFM_LEFTJ; break; |
| 276 | default: |
| 277 | dev_dbg(&aic26->spi->dev, "bad format\n"); return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* --------------------------------------------------------------------- |
| 284 | * Digital Audio Interface Definition |
| 285 | */ |
| 286 | #define AIC26_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 287 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 288 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ |
| 289 | SNDRV_PCM_RATE_48000) |
| 290 | #define AIC26_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |\ |
| 291 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE) |
| 292 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 293 | static const struct snd_soc_dai_ops aic26_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 294 | .hw_params = aic26_hw_params, |
| 295 | .digital_mute = aic26_mute, |
| 296 | .set_sysclk = aic26_set_sysclk, |
| 297 | .set_fmt = aic26_set_fmt, |
| 298 | }; |
| 299 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 300 | static struct snd_soc_dai_driver aic26_dai = { |
| 301 | .name = "tlv320aic26-hifi", |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 302 | .playback = { |
| 303 | .stream_name = "Playback", |
| 304 | .channels_min = 2, |
| 305 | .channels_max = 2, |
| 306 | .rates = AIC26_RATES, |
| 307 | .formats = AIC26_FORMATS, |
| 308 | }, |
| 309 | .capture = { |
| 310 | .stream_name = "Capture", |
| 311 | .channels_min = 2, |
| 312 | .channels_max = 2, |
| 313 | .rates = AIC26_RATES, |
| 314 | .formats = AIC26_FORMATS, |
| 315 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 316 | .ops = &aic26_dai_ops, |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 317 | }; |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 318 | |
| 319 | /* --------------------------------------------------------------------- |
| 320 | * ALSA controls |
| 321 | */ |
| 322 | static const char *aic26_capture_src_text[] = {"Mic", "Aux"}; |
| 323 | static const struct soc_enum aic26_capture_src_enum = |
| 324 | SOC_ENUM_SINGLE(AIC26_REG_AUDIO_CTRL1, 12, 2, aic26_capture_src_text); |
| 325 | |
| 326 | static const struct snd_kcontrol_new aic26_snd_controls[] = { |
| 327 | /* Output */ |
| 328 | SOC_DOUBLE("PCM Playback Volume", AIC26_REG_DAC_GAIN, 8, 0, 0x7f, 1), |
| 329 | SOC_DOUBLE("PCM Playback Switch", AIC26_REG_DAC_GAIN, 15, 7, 1, 1), |
| 330 | SOC_SINGLE("PCM Capture Volume", AIC26_REG_ADC_GAIN, 8, 0x7f, 0), |
| 331 | SOC_SINGLE("PCM Capture Mute", AIC26_REG_ADC_GAIN, 15, 1, 1), |
| 332 | SOC_SINGLE("Keyclick activate", AIC26_REG_AUDIO_CTRL2, 15, 0x1, 0), |
| 333 | SOC_SINGLE("Keyclick amplitude", AIC26_REG_AUDIO_CTRL2, 12, 0x7, 0), |
| 334 | SOC_SINGLE("Keyclick frequency", AIC26_REG_AUDIO_CTRL2, 8, 0x7, 0), |
| 335 | SOC_SINGLE("Keyclick period", AIC26_REG_AUDIO_CTRL2, 4, 0xf, 0), |
| 336 | SOC_ENUM("Capture Source", aic26_capture_src_enum), |
| 337 | }; |
| 338 | |
| 339 | /* --------------------------------------------------------------------- |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 340 | * SPI device portion of driver: sysfs files for debugging |
| 341 | */ |
| 342 | |
| 343 | static ssize_t aic26_keyclick_show(struct device *dev, |
| 344 | struct device_attribute *attr, char *buf) |
| 345 | { |
| 346 | struct aic26 *aic26 = dev_get_drvdata(dev); |
| 347 | int val, amp, freq, len; |
| 348 | |
Lars-Peter Clausen | f8f1179 | 2013-08-06 13:39:29 +0200 | [diff] [blame] | 349 | val = aic26_reg_read_cache(aic26->codec, AIC26_REG_AUDIO_CTRL2); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 350 | amp = (val >> 12) & 0x7; |
| 351 | freq = (125 << ((val >> 8) & 0x7)) >> 1; |
| 352 | len = 2 * (1 + ((val >> 4) & 0xf)); |
| 353 | |
| 354 | return sprintf(buf, "amp=%x freq=%iHz len=%iclks\n", amp, freq, len); |
| 355 | } |
| 356 | |
| 357 | /* Any write to the keyclick attribute will trigger the keyclick event */ |
| 358 | static ssize_t aic26_keyclick_set(struct device *dev, |
| 359 | struct device_attribute *attr, |
| 360 | const char *buf, size_t count) |
| 361 | { |
| 362 | struct aic26 *aic26 = dev_get_drvdata(dev); |
| 363 | int val; |
| 364 | |
Lars-Peter Clausen | f8f1179 | 2013-08-06 13:39:29 +0200 | [diff] [blame] | 365 | val = aic26_reg_read_cache(aic26->codec, AIC26_REG_AUDIO_CTRL2); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 366 | val |= 0x8000; |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 367 | snd_soc_write(aic26->codec, AIC26_REG_AUDIO_CTRL2, val); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 368 | |
| 369 | return count; |
| 370 | } |
| 371 | |
Mark Brown | 9cce39a | 2008-07-29 11:42:33 +0100 | [diff] [blame] | 372 | static DEVICE_ATTR(keyclick, 0644, aic26_keyclick_show, aic26_keyclick_set); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 373 | |
| 374 | /* --------------------------------------------------------------------- |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 375 | * SoC CODEC portion of driver: probe and release routines |
| 376 | */ |
| 377 | static int aic26_probe(struct snd_soc_codec *codec) |
| 378 | { |
Lars-Peter Clausen | f8f1179 | 2013-08-06 13:39:29 +0200 | [diff] [blame] | 379 | struct aic26 *aic26 = dev_get_drvdata(codec->dev); |
Mark Brown | 806955d | 2013-09-25 13:10:33 +0100 | [diff] [blame^] | 380 | int ret, i, reg; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 381 | |
Lars-Peter Clausen | f8f1179 | 2013-08-06 13:39:29 +0200 | [diff] [blame] | 382 | aic26->codec = codec; |
| 383 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 384 | /* Reset the codec to power on defaults */ |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 385 | snd_soc_write(codec, AIC26_REG_RESET, 0xBB00); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 386 | |
| 387 | /* Power up CODEC */ |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 388 | snd_soc_write(codec, AIC26_REG_POWER_CTRL, 0); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 389 | |
| 390 | /* Audio Control 3 (master mode, fsref rate) */ |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 391 | reg = snd_soc_read(codec, AIC26_REG_AUDIO_CTRL3); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 392 | reg &= ~0xf800; |
| 393 | reg |= 0x0800; /* set master mode */ |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 394 | snd_soc_write(codec, AIC26_REG_AUDIO_CTRL3, reg); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 395 | |
| 396 | /* Fill register cache */ |
Lars-Peter Clausen | 839d271 | 2010-12-28 21:37:55 +0100 | [diff] [blame] | 397 | for (i = 0; i < codec->driver->reg_cache_size; i++) |
Mark Brown | 1220139 | 2013-08-16 12:13:50 +0100 | [diff] [blame] | 398 | snd_soc_read(codec, i); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 399 | |
| 400 | /* Register the sysfs files for debugging */ |
| 401 | /* Create SysFS files */ |
| 402 | ret = device_create_file(codec->dev, &dev_attr_keyclick); |
| 403 | if (ret) |
| 404 | dev_info(codec->dev, "error creating sysfs files\n"); |
| 405 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static struct snd_soc_codec_driver aic26_soc_codec_dev = { |
| 410 | .probe = aic26_probe, |
| 411 | .read = aic26_reg_read, |
| 412 | .write = aic26_reg_write, |
| 413 | .reg_cache_size = AIC26_NUM_REGS, |
| 414 | .reg_word_size = sizeof(u16), |
Mark Brown | 806955d | 2013-09-25 13:10:33 +0100 | [diff] [blame^] | 415 | .controls = aic26_snd_controls, |
| 416 | .num_controls = ARRAY_SIZE(aic26_snd_controls), |
Mark Brown | 4a11bc2 | 2013-08-16 12:24:47 +0100 | [diff] [blame] | 417 | .dapm_widgets = tlv320aic26_dapm_widgets, |
| 418 | .num_dapm_widgets = ARRAY_SIZE(tlv320aic26_dapm_widgets), |
| 419 | .dapm_routes = tlv320aic26_dapm_routes, |
| 420 | .num_dapm_routes = ARRAY_SIZE(tlv320aic26_dapm_routes), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | /* --------------------------------------------------------------------- |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 424 | * SPI device portion of driver: probe and release routines and SPI |
| 425 | * driver registration. |
| 426 | */ |
| 427 | static int aic26_spi_probe(struct spi_device *spi) |
| 428 | { |
| 429 | struct aic26 *aic26; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 430 | int ret; |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 431 | |
| 432 | dev_dbg(&spi->dev, "probing tlv320aic26 spi device\n"); |
| 433 | |
| 434 | /* Allocate driver data */ |
Axel Lin | a816302 | 2011-12-29 12:08:36 +0800 | [diff] [blame] | 435 | aic26 = devm_kzalloc(&spi->dev, sizeof *aic26, GFP_KERNEL); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 436 | if (!aic26) |
| 437 | return -ENOMEM; |
| 438 | |
| 439 | /* Initialize the driver data */ |
| 440 | aic26->spi = spi; |
| 441 | dev_set_drvdata(&spi->dev, aic26); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 442 | aic26->master = 1; |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 443 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 444 | ret = snd_soc_register_codec(&spi->dev, |
| 445 | &aic26_soc_codec_dev, &aic26_dai, 1); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 446 | return ret; |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static int aic26_spi_remove(struct spi_device *spi) |
| 450 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 451 | snd_soc_unregister_codec(&spi->dev); |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static struct spi_driver aic26_spi = { |
| 456 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 457 | .name = "tlv320aic26-codec", |
Grant Likely | d8e3bb7 | 2008-07-29 11:42:31 +0100 | [diff] [blame] | 458 | .owner = THIS_MODULE, |
| 459 | }, |
| 460 | .probe = aic26_spi_probe, |
| 461 | .remove = aic26_spi_remove, |
| 462 | }; |
| 463 | |
Sachin Kamat | 9bb280a | 2012-08-27 17:00:26 +0530 | [diff] [blame] | 464 | module_spi_driver(aic26_spi); |