Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: sound/soc/codecs/ad1836.c |
| 3 | * Author: Barry Song <Barry.Song@analog.com> |
| 4 | * |
| 5 | * Created: Aug 04 2009 |
| 6 | * Description: Driver for AD1836 sound chip |
| 7 | * |
| 8 | * Modified: |
| 9 | * Copyright 2009 Analog Devices Inc. |
| 10 | * |
| 11 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 21 | #include <linux/module.h> |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <sound/core.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/initval.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/tlv.h> |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 30 | #include <linux/spi/spi.h> |
| 31 | #include "ad1836.h" |
| 32 | |
| 33 | /* codec private data */ |
| 34 | struct ad1836_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 35 | enum snd_soc_control_type control_type; |
| 36 | void *control_data; |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 39 | /* |
| 40 | * AD1836 volume/mute/de-emphasis etc. controls |
| 41 | */ |
| 42 | static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"}; |
| 43 | |
| 44 | static const struct soc_enum ad1836_deemp_enum = |
| 45 | SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp); |
| 46 | |
Lars-Peter Clausen | 90bc11d | 2011-06-06 13:38:36 +0200 | [diff] [blame] | 47 | #define AD1836_DAC_VOLUME(x) \ |
| 48 | SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \ |
| 49 | AD1836_DAC_R_VOL(x), 0, 0x3FF, 0) |
| 50 | |
| 51 | #define AD1836_DAC_SWITCH(x) \ |
| 52 | SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \ |
| 53 | AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1) |
| 54 | |
| 55 | #define AD1836_ADC_SWITCH(x) \ |
| 56 | SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \ |
| 57 | AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1) |
| 58 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 59 | static const struct snd_kcontrol_new ad1836_snd_controls[] = { |
| 60 | /* DAC volume control */ |
Lars-Peter Clausen | 90bc11d | 2011-06-06 13:38:36 +0200 | [diff] [blame] | 61 | AD1836_DAC_VOLUME(1), |
| 62 | AD1836_DAC_VOLUME(2), |
| 63 | AD1836_DAC_VOLUME(3), |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 64 | |
| 65 | /* ADC switch control */ |
Lars-Peter Clausen | 90bc11d | 2011-06-06 13:38:36 +0200 | [diff] [blame] | 66 | AD1836_ADC_SWITCH(1), |
| 67 | AD1836_ADC_SWITCH(2), |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 68 | |
| 69 | /* DAC switch control */ |
Lars-Peter Clausen | 90bc11d | 2011-06-06 13:38:36 +0200 | [diff] [blame] | 70 | AD1836_DAC_SWITCH(1), |
| 71 | AD1836_DAC_SWITCH(2), |
| 72 | AD1836_DAC_SWITCH(3), |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 73 | |
| 74 | /* ADC high-pass filter */ |
| 75 | SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1, |
| 76 | AD1836_ADC_HIGHPASS_FILTER, 1, 0), |
| 77 | |
| 78 | /* DAC de-emphasis */ |
| 79 | SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum), |
| 80 | }; |
| 81 | |
| 82 | static const struct snd_soc_dapm_widget ad1836_dapm_widgets[] = { |
| 83 | SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1, |
| 84 | AD1836_DAC_POWERDOWN, 1), |
| 85 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
| 86 | SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1, |
| 87 | AD1836_ADC_POWERDOWN, 1, NULL, 0), |
| 88 | SND_SOC_DAPM_OUTPUT("DAC1OUT"), |
| 89 | SND_SOC_DAPM_OUTPUT("DAC2OUT"), |
| 90 | SND_SOC_DAPM_OUTPUT("DAC3OUT"), |
| 91 | SND_SOC_DAPM_INPUT("ADC1IN"), |
| 92 | SND_SOC_DAPM_INPUT("ADC2IN"), |
| 93 | }; |
| 94 | |
| 95 | static const struct snd_soc_dapm_route audio_paths[] = { |
| 96 | { "DAC", NULL, "ADC_PWR" }, |
| 97 | { "ADC", NULL, "ADC_PWR" }, |
| 98 | { "DAC1OUT", "DAC1 Switch", "DAC" }, |
| 99 | { "DAC2OUT", "DAC2 Switch", "DAC" }, |
| 100 | { "DAC3OUT", "DAC3 Switch", "DAC" }, |
| 101 | { "ADC", "ADC1 Switch", "ADC1IN" }, |
| 102 | { "ADC", "ADC2 Switch", "ADC2IN" }, |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * DAI ops entries |
| 107 | */ |
| 108 | |
| 109 | static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 110 | unsigned int fmt) |
| 111 | { |
| 112 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 113 | /* at present, we support adc aux mode to interface with |
| 114 | * blackfin sport tdm mode |
| 115 | */ |
| 116 | case SND_SOC_DAIFMT_DSP_A: |
| 117 | break; |
| 118 | default: |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
| 122 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 123 | case SND_SOC_DAIFMT_IB_IF: |
| 124 | break; |
| 125 | default: |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 130 | /* ALCLK,ABCLK are both output, AD1836 can only be master */ |
| 131 | case SND_SOC_DAIFMT_CBM_CFM: |
| 132 | break; |
| 133 | default: |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int ad1836_hw_params(struct snd_pcm_substream *substream, |
| 141 | struct snd_pcm_hw_params *params, |
| 142 | struct snd_soc_dai *dai) |
| 143 | { |
| 144 | int word_len = 0; |
| 145 | |
| 146 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 147 | struct snd_soc_codec *codec = rtd->codec; |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 148 | |
| 149 | /* bit size */ |
| 150 | switch (params_format(params)) { |
| 151 | case SNDRV_PCM_FORMAT_S16_LE: |
| 152 | word_len = 3; |
| 153 | break; |
| 154 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 155 | word_len = 1; |
| 156 | break; |
| 157 | case SNDRV_PCM_FORMAT_S24_LE: |
| 158 | case SNDRV_PCM_FORMAT_S32_LE: |
| 159 | word_len = 0; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | snd_soc_update_bits(codec, AD1836_DAC_CTRL1, |
| 164 | AD1836_DAC_WORD_LEN_MASK, word_len); |
| 165 | |
| 166 | snd_soc_update_bits(codec, AD1836_ADC_CTRL2, |
| 167 | AD1836_ADC_WORD_LEN_MASK, word_len); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 172 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 173 | static int ad1836_soc_suspend(struct snd_soc_codec *codec, |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 174 | pm_message_t state) |
| 175 | { |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 176 | /* reset clock control mode */ |
Lars-Peter Clausen | 2cf0342 | 2011-06-06 13:38:37 +0200 | [diff] [blame^] | 177 | return snd_soc_update_bits(codec, AD1836_ADC_CTRL2, |
| 178 | AD1836_ADC_SERFMT_MASK, 0); |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 181 | static int ad1836_soc_resume(struct snd_soc_codec *codec) |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 182 | { |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 183 | /* restore clock control mode */ |
Lars-Peter Clausen | 2cf0342 | 2011-06-06 13:38:37 +0200 | [diff] [blame^] | 184 | return snd_soc_update_bits(codec, AD1836_ADC_CTRL2, |
| 185 | AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX); |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 186 | } |
| 187 | #else |
| 188 | #define ad1836_soc_suspend NULL |
| 189 | #define ad1836_soc_resume NULL |
| 190 | #endif |
| 191 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 192 | static struct snd_soc_dai_ops ad1836_dai_ops = { |
| 193 | .hw_params = ad1836_hw_params, |
| 194 | .set_fmt = ad1836_set_dai_fmt, |
| 195 | }; |
| 196 | |
| 197 | /* codec DAI instance */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 198 | static struct snd_soc_dai_driver ad1836_dai = { |
| 199 | .name = "ad1836-hifi", |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 200 | .playback = { |
| 201 | .stream_name = "Playback", |
| 202 | .channels_min = 2, |
| 203 | .channels_max = 6, |
| 204 | .rates = SNDRV_PCM_RATE_48000, |
| 205 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 206 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 207 | }, |
| 208 | .capture = { |
| 209 | .stream_name = "Capture", |
| 210 | .channels_min = 2, |
| 211 | .channels_max = 4, |
| 212 | .rates = SNDRV_PCM_RATE_48000, |
| 213 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 214 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 215 | }, |
| 216 | .ops = &ad1836_dai_ops, |
| 217 | }; |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 218 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 219 | static int ad1836_probe(struct snd_soc_codec *codec) |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 220 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 221 | struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 222 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 223 | int ret = 0; |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 224 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 225 | codec->control_data = ad1836->control_data; |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 226 | ret = snd_soc_codec_set_cache_io(codec, 4, 12, SND_SOC_SPI); |
| 227 | if (ret < 0) { |
| 228 | dev_err(codec->dev, "failed to set cache I/O: %d\n", |
| 229 | ret); |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 230 | return ret; |
| 231 | } |
| 232 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 233 | /* default setting for ad1836 */ |
| 234 | /* de-emphasis: 48kHz, power-on dac */ |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 235 | snd_soc_write(codec, AD1836_DAC_CTRL1, 0x300); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 236 | /* unmute dac channels */ |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 237 | snd_soc_write(codec, AD1836_DAC_CTRL2, 0x0); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 238 | /* high-pass filter enable, power-on adc */ |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 239 | snd_soc_write(codec, AD1836_ADC_CTRL1, 0x100); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 240 | /* unmute adc channles, adc aux mode */ |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 241 | snd_soc_write(codec, AD1836_ADC_CTRL2, 0x180); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 242 | /* left/right diff:PGA/MUX */ |
Barry Song | 63b62ab | 2010-01-27 11:46:17 +0800 | [diff] [blame] | 243 | snd_soc_write(codec, AD1836_ADC_CTRL3, 0x3A); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 244 | /* volume */ |
Lars-Peter Clausen | 90bc11d | 2011-06-06 13:38:36 +0200 | [diff] [blame] | 245 | snd_soc_write(codec, AD1836_DAC_L_VOL(1), 0x3FF); |
| 246 | snd_soc_write(codec, AD1836_DAC_R_VOL(1), 0x3FF); |
| 247 | snd_soc_write(codec, AD1836_DAC_L_VOL(2), 0x3FF); |
| 248 | snd_soc_write(codec, AD1836_DAC_R_VOL(2), 0x3FF); |
| 249 | snd_soc_write(codec, AD1836_DAC_L_VOL(3), 0x3FF); |
| 250 | snd_soc_write(codec, AD1836_DAC_R_VOL(3), 0x3FF); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 251 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 252 | snd_soc_add_controls(codec, ad1836_snd_controls, |
| 253 | ARRAY_SIZE(ad1836_snd_controls)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 254 | snd_soc_dapm_new_controls(dapm, ad1836_dapm_widgets, |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 255 | ARRAY_SIZE(ad1836_dapm_widgets)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 256 | snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths)); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 257 | |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /* power down chip */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 262 | static int ad1836_remove(struct snd_soc_codec *codec) |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 263 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 264 | /* reset clock control mode */ |
Lars-Peter Clausen | 2cf0342 | 2011-06-06 13:38:37 +0200 | [diff] [blame^] | 265 | return snd_soc_update_bits(codec, AD1836_ADC_CTRL2, |
| 266 | AD1836_ADC_SERFMT_MASK, 0); |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 267 | } |
| 268 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 269 | static struct snd_soc_codec_driver soc_codec_dev_ad1836 = { |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 270 | .probe = ad1836_probe, |
| 271 | .remove = ad1836_remove, |
Barry Song | 84549d2 | 2010-01-25 16:42:25 +0800 | [diff] [blame] | 272 | .suspend = ad1836_soc_suspend, |
| 273 | .resume = ad1836_soc_resume, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 274 | .reg_cache_size = AD1836_NUM_REGS, |
| 275 | .reg_word_size = sizeof(u16), |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 276 | }; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 277 | |
| 278 | static int __devinit ad1836_spi_probe(struct spi_device *spi) |
| 279 | { |
| 280 | struct ad1836_priv *ad1836; |
| 281 | int ret; |
| 282 | |
| 283 | ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL); |
| 284 | if (ad1836 == NULL) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | spi_set_drvdata(spi, ad1836); |
| 288 | ad1836->control_data = spi; |
| 289 | ad1836->control_type = SND_SOC_SPI; |
| 290 | |
| 291 | ret = snd_soc_register_codec(&spi->dev, |
| 292 | &soc_codec_dev_ad1836, &ad1836_dai, 1); |
| 293 | if (ret < 0) |
| 294 | kfree(ad1836); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static int __devexit ad1836_spi_remove(struct spi_device *spi) |
| 299 | { |
| 300 | snd_soc_unregister_codec(&spi->dev); |
| 301 | kfree(spi_get_drvdata(spi)); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static struct spi_driver ad1836_spi_driver = { |
| 306 | .driver = { |
| 307 | .name = "ad1836-codec", |
| 308 | .owner = THIS_MODULE, |
| 309 | }, |
| 310 | .probe = ad1836_spi_probe, |
| 311 | .remove = __devexit_p(ad1836_spi_remove), |
| 312 | }; |
Barry Song | 7eaae41 | 2009-08-13 11:59:32 +0800 | [diff] [blame] | 313 | |
| 314 | static int __init ad1836_init(void) |
| 315 | { |
| 316 | int ret; |
| 317 | |
| 318 | ret = spi_register_driver(&ad1836_spi_driver); |
| 319 | if (ret != 0) { |
| 320 | printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n", |
| 321 | ret); |
| 322 | } |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | module_init(ad1836_init); |
| 327 | |
| 328 | static void __exit ad1836_exit(void) |
| 329 | { |
| 330 | spi_unregister_driver(&ad1836_spi_driver); |
| 331 | } |
| 332 | module_exit(ad1836_exit); |
| 333 | |
| 334 | MODULE_DESCRIPTION("ASoC ad1836 driver"); |
| 335 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 336 | MODULE_LICENSE("GPL"); |