Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * DA7210 ALSA Soc codec driver |
| 3 | * |
| 4 | * Copyright (c) 2009 Dialog Semiconductor |
| 5 | * Written by David Chen <Dajun.chen@diasemi.com> |
| 6 | * |
| 7 | * Copyright (C) 2009 Renesas Solutions Corp. |
| 8 | * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com> |
| 9 | * |
| 10 | * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 2 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | */ |
| 17 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 18 | #include <linux/delay.h> |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 19 | #include <linux/i2c.h> |
| 20 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 24 | #include <sound/soc.h> |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 25 | #include <sound/initval.h> |
Kuninori Morimoto | a7e7cd5 | 2010-07-21 14:12:16 +0900 | [diff] [blame] | 26 | #include <sound/tlv.h> |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 27 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 28 | /* DA7210 register space */ |
| 29 | #define DA7210_STATUS 0x02 |
| 30 | #define DA7210_STARTUP1 0x03 |
| 31 | #define DA7210_MIC_L 0x07 |
| 32 | #define DA7210_MIC_R 0x08 |
| 33 | #define DA7210_INMIX_L 0x0D |
| 34 | #define DA7210_INMIX_R 0x0E |
| 35 | #define DA7210_ADC_HPF 0x0F |
| 36 | #define DA7210_ADC 0x10 |
| 37 | #define DA7210_DAC_HPF 0x14 |
| 38 | #define DA7210_DAC_L 0x15 |
| 39 | #define DA7210_DAC_R 0x16 |
| 40 | #define DA7210_DAC_SEL 0x17 |
| 41 | #define DA7210_OUTMIX_L 0x1C |
| 42 | #define DA7210_OUTMIX_R 0x1D |
| 43 | #define DA7210_HP_L_VOL 0x21 |
| 44 | #define DA7210_HP_R_VOL 0x22 |
| 45 | #define DA7210_HP_CFG 0x23 |
| 46 | #define DA7210_DAI_SRC_SEL 0x25 |
| 47 | #define DA7210_DAI_CFG1 0x26 |
| 48 | #define DA7210_DAI_CFG3 0x28 |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 49 | #define DA7210_PLL_DIV1 0x29 |
| 50 | #define DA7210_PLL_DIV2 0x2A |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 51 | #define DA7210_PLL_DIV3 0x2B |
| 52 | #define DA7210_PLL 0x2C |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 53 | #define DA7210_A_HID_UNLOCK 0x8A |
| 54 | #define DA7210_A_TEST_UNLOCK 0x8B |
| 55 | #define DA7210_A_PLL1 0x90 |
| 56 | #define DA7210_A_CP_MODE 0xA7 |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 57 | |
| 58 | /* STARTUP1 bit fields */ |
| 59 | #define DA7210_SC_MST_EN (1 << 0) |
| 60 | |
| 61 | /* MIC_L bit fields */ |
| 62 | #define DA7210_MICBIAS_EN (1 << 6) |
| 63 | #define DA7210_MIC_L_EN (1 << 7) |
| 64 | |
| 65 | /* MIC_R bit fields */ |
| 66 | #define DA7210_MIC_R_EN (1 << 7) |
| 67 | |
| 68 | /* INMIX_L bit fields */ |
| 69 | #define DA7210_IN_L_EN (1 << 7) |
| 70 | |
| 71 | /* INMIX_R bit fields */ |
| 72 | #define DA7210_IN_R_EN (1 << 7) |
| 73 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 74 | /* ADC bit fields */ |
| 75 | #define DA7210_ADC_L_EN (1 << 3) |
| 76 | #define DA7210_ADC_R_EN (1 << 7) |
| 77 | |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 78 | /* DAC/ADC HPF fields */ |
| 79 | #define DA7210_VOICE_F0_MASK (0x7 << 4) |
| 80 | #define DA7210_VOICE_F0_25 (1 << 4) |
| 81 | #define DA7210_VOICE_EN (1 << 7) |
Mark Brown | c215143 | 2009-12-16 20:36:37 +0000 | [diff] [blame] | 82 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 83 | /* DAC_SEL bit fields */ |
| 84 | #define DA7210_DAC_L_SRC_DAI_L (4 << 0) |
| 85 | #define DA7210_DAC_L_EN (1 << 3) |
| 86 | #define DA7210_DAC_R_SRC_DAI_R (5 << 4) |
| 87 | #define DA7210_DAC_R_EN (1 << 7) |
| 88 | |
| 89 | /* OUTMIX_L bit fields */ |
| 90 | #define DA7210_OUT_L_EN (1 << 7) |
| 91 | |
| 92 | /* OUTMIX_R bit fields */ |
| 93 | #define DA7210_OUT_R_EN (1 << 7) |
| 94 | |
| 95 | /* HP_CFG bit fields */ |
| 96 | #define DA7210_HP_2CAP_MODE (1 << 1) |
| 97 | #define DA7210_HP_SENSE_EN (1 << 2) |
| 98 | #define DA7210_HP_L_EN (1 << 3) |
| 99 | #define DA7210_HP_MODE (1 << 6) |
| 100 | #define DA7210_HP_R_EN (1 << 7) |
| 101 | |
| 102 | /* DAI_SRC_SEL bit fields */ |
| 103 | #define DA7210_DAI_OUT_L_SRC (6 << 0) |
| 104 | #define DA7210_DAI_OUT_R_SRC (7 << 4) |
| 105 | |
| 106 | /* DAI_CFG1 bit fields */ |
| 107 | #define DA7210_DAI_WORD_S16_LE (0 << 0) |
| 108 | #define DA7210_DAI_WORD_S24_LE (2 << 0) |
| 109 | #define DA7210_DAI_FLEN_64BIT (1 << 2) |
| 110 | #define DA7210_DAI_MODE_MASTER (1 << 7) |
| 111 | |
| 112 | /* DAI_CFG3 bit fields */ |
| 113 | #define DA7210_DAI_FORMAT_I2SMODE (0 << 0) |
| 114 | #define DA7210_DAI_OE (1 << 3) |
| 115 | #define DA7210_DAI_EN (1 << 7) |
| 116 | |
| 117 | /*PLL_DIV3 bit fields */ |
| 118 | #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4) |
| 119 | #define DA7210_PLL_BYP (1 << 6) |
| 120 | |
| 121 | /* PLL bit fields */ |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 122 | #define DA7210_PLL_FS_MASK (0xF << 0) |
| 123 | #define DA7210_PLL_FS_8000 (0x1 << 0) |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 124 | #define DA7210_PLL_FS_11025 (0x2 << 0) |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 125 | #define DA7210_PLL_FS_12000 (0x3 << 0) |
| 126 | #define DA7210_PLL_FS_16000 (0x5 << 0) |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 127 | #define DA7210_PLL_FS_22050 (0x6 << 0) |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 128 | #define DA7210_PLL_FS_24000 (0x7 << 0) |
| 129 | #define DA7210_PLL_FS_32000 (0x9 << 0) |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 130 | #define DA7210_PLL_FS_44100 (0xA << 0) |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 131 | #define DA7210_PLL_FS_48000 (0xB << 0) |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 132 | #define DA7210_PLL_FS_88200 (0xE << 0) |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 133 | #define DA7210_PLL_FS_96000 (0xF << 0) |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 134 | #define DA7210_PLL_EN (0x1 << 7) |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 135 | |
| 136 | #define DA7210_VERSION "0.0.1" |
| 137 | |
Kuninori Morimoto | a7e7cd5 | 2010-07-21 14:12:16 +0900 | [diff] [blame] | 138 | /* |
| 139 | * Playback Volume |
| 140 | * |
| 141 | * max : 0x3F (+15.0 dB) |
| 142 | * (1.5 dB step) |
| 143 | * min : 0x11 (-54.0 dB) |
| 144 | * mute : 0x10 |
| 145 | * reserved : 0x00 - 0x0F |
| 146 | * |
| 147 | * ** FIXME ** |
| 148 | * |
| 149 | * Reserved area are considered as "mute". |
| 150 | * -> min = -79.5 dB |
| 151 | */ |
| 152 | static const DECLARE_TLV_DB_SCALE(hp_out_tlv, -7950, 150, 1); |
| 153 | |
| 154 | static const struct snd_kcontrol_new da7210_snd_controls[] = { |
| 155 | |
| 156 | SOC_DOUBLE_R_TLV("HeadPhone Playback Volume", |
| 157 | DA7210_HP_L_VOL, DA7210_HP_R_VOL, |
| 158 | 0, 0x3F, 0, hp_out_tlv), |
| 159 | }; |
| 160 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 161 | /* Codec private data */ |
| 162 | struct da7210_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 163 | enum snd_soc_control_type control_type; |
| 164 | void *control_data; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 165 | }; |
| 166 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 167 | /* |
| 168 | * Register cache |
| 169 | */ |
| 170 | static const u8 da7210_reg[] = { |
| 171 | 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */ |
| 172 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */ |
| 173 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */ |
| 174 | 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */ |
| 175 | 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */ |
| 176 | 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */ |
| 177 | 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */ |
| 178 | 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */ |
| 179 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */ |
| 180 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */ |
| 181 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */ |
| 182 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */ |
| 183 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */ |
| 184 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */ |
| 185 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */ |
| 186 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */ |
| 187 | 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */ |
| 188 | 0x00, /* R88 */ |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * Read da7210 register cache |
| 193 | */ |
| 194 | static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg) |
| 195 | { |
| 196 | u8 *cache = codec->reg_cache; |
Phil Carmody | 4f6f22d | 2010-04-14 17:03:13 +0300 | [diff] [blame] | 197 | BUG_ON(reg >= ARRAY_SIZE(da7210_reg)); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 198 | return cache[reg]; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Write to the da7210 register space |
| 203 | */ |
| 204 | static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value) |
| 205 | { |
| 206 | u8 *cache = codec->reg_cache; |
| 207 | u8 data[2]; |
| 208 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 209 | BUG_ON(codec->driver->volatile_register); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 210 | |
| 211 | data[0] = reg & 0xff; |
| 212 | data[1] = value & 0xff; |
| 213 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 214 | if (reg >= codec->driver->reg_cache_size) |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 215 | return -EIO; |
| 216 | |
| 217 | if (2 != codec->hw_write(codec->control_data, data, 2)) |
| 218 | return -EIO; |
| 219 | |
| 220 | cache[reg] = value; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Read from the da7210 register space. |
| 226 | */ |
| 227 | static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg) |
| 228 | { |
| 229 | if (DA7210_STATUS == reg) |
| 230 | return i2c_smbus_read_byte_data(codec->control_data, reg); |
| 231 | |
| 232 | return da7210_read_reg_cache(codec, reg); |
| 233 | } |
| 234 | |
| 235 | static int da7210_startup(struct snd_pcm_substream *substream, |
| 236 | struct snd_soc_dai *dai) |
| 237 | { |
| 238 | int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 239 | struct snd_soc_codec *codec = dai->codec; |
| 240 | |
| 241 | if (is_play) { |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 242 | /* Enable Out */ |
| 243 | snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10); |
| 244 | snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10); |
| 245 | |
| 246 | } else { |
| 247 | /* Volume 7 */ |
| 248 | snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7); |
| 249 | snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7); |
| 250 | |
| 251 | /* Enable Mic */ |
| 252 | snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1); |
| 253 | snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1); |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Set PCM DAI word length. |
| 261 | */ |
| 262 | static int da7210_hw_params(struct snd_pcm_substream *substream, |
| 263 | struct snd_pcm_hw_params *params, |
| 264 | struct snd_soc_dai *dai) |
| 265 | { |
| 266 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 267 | struct snd_soc_codec *codec = rtd->codec; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 268 | u32 dai_cfg1; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 269 | u32 hpf_reg, hpf_mask, hpf_value; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 270 | u32 fs, bypass; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 271 | |
| 272 | /* set DAI source to Left and Right ADC */ |
| 273 | da7210_write(codec, DA7210_DAI_SRC_SEL, |
| 274 | DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC); |
| 275 | |
| 276 | /* Enable DAI */ |
| 277 | da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN); |
| 278 | |
| 279 | dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1); |
| 280 | |
| 281 | switch (params_format(params)) { |
| 282 | case SNDRV_PCM_FORMAT_S16_LE: |
| 283 | dai_cfg1 |= DA7210_DAI_WORD_S16_LE; |
| 284 | break; |
| 285 | case SNDRV_PCM_FORMAT_S24_LE: |
| 286 | dai_cfg1 |= DA7210_DAI_WORD_S24_LE; |
| 287 | break; |
| 288 | default: |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); |
| 293 | |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 294 | hpf_reg = (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) ? |
| 295 | DA7210_DAC_HPF : DA7210_ADC_HPF; |
| 296 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 297 | switch (params_rate(params)) { |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 298 | case 8000: |
| 299 | fs = DA7210_PLL_FS_8000; |
| 300 | hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN; |
| 301 | hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 302 | bypass = DA7210_PLL_BYP; |
| 303 | break; |
| 304 | case 11025: |
| 305 | fs = DA7210_PLL_FS_11025; |
| 306 | hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN; |
| 307 | hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN; |
| 308 | bypass = 0; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 309 | break; |
| 310 | case 12000: |
| 311 | fs = DA7210_PLL_FS_12000; |
| 312 | hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN; |
| 313 | hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 314 | bypass = DA7210_PLL_BYP; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 315 | break; |
| 316 | case 16000: |
| 317 | fs = DA7210_PLL_FS_16000; |
| 318 | hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN; |
| 319 | hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 320 | bypass = DA7210_PLL_BYP; |
| 321 | break; |
| 322 | case 22050: |
| 323 | fs = DA7210_PLL_FS_22050; |
| 324 | hpf_mask = DA7210_VOICE_EN; |
| 325 | hpf_value = 0; |
| 326 | bypass = 0; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 327 | break; |
| 328 | case 32000: |
| 329 | fs = DA7210_PLL_FS_32000; |
| 330 | hpf_mask = DA7210_VOICE_EN; |
| 331 | hpf_value = 0; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 332 | bypass = DA7210_PLL_BYP; |
| 333 | break; |
| 334 | case 44100: |
| 335 | fs = DA7210_PLL_FS_44100; |
| 336 | hpf_mask = DA7210_VOICE_EN; |
| 337 | hpf_value = 0; |
| 338 | bypass = 0; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 339 | break; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 340 | case 48000: |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 341 | fs = DA7210_PLL_FS_48000; |
| 342 | hpf_mask = DA7210_VOICE_EN; |
| 343 | hpf_value = 0; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 344 | bypass = DA7210_PLL_BYP; |
| 345 | break; |
| 346 | case 88200: |
| 347 | fs = DA7210_PLL_FS_88200; |
| 348 | hpf_mask = DA7210_VOICE_EN; |
| 349 | hpf_value = 0; |
| 350 | bypass = 0; |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 351 | break; |
| 352 | case 96000: |
| 353 | fs = DA7210_PLL_FS_96000; |
| 354 | hpf_mask = DA7210_VOICE_EN; |
| 355 | hpf_value = 0; |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 356 | bypass = DA7210_PLL_BYP; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 362 | /* Disable active mode */ |
| 363 | snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0); |
| 364 | |
Kuninori Morimoto | 3a9d620 | 2010-03-01 10:10:43 +0900 | [diff] [blame] | 365 | snd_soc_update_bits(codec, hpf_reg, hpf_mask, hpf_value); |
| 366 | snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs); |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 367 | snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass); |
| 368 | |
| 369 | /* Enable active mode */ |
| 370 | snd_soc_update_bits(codec, DA7210_STARTUP1, |
| 371 | DA7210_SC_MST_EN, DA7210_SC_MST_EN); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Set DAI mode and Format |
| 378 | */ |
| 379 | static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt) |
| 380 | { |
| 381 | struct snd_soc_codec *codec = codec_dai->codec; |
| 382 | u32 dai_cfg1; |
| 383 | u32 dai_cfg3; |
| 384 | |
| 385 | dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1); |
| 386 | dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3); |
| 387 | |
| 388 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 389 | case SND_SOC_DAIFMT_CBM_CFM: |
| 390 | dai_cfg1 |= DA7210_DAI_MODE_MASTER; |
| 391 | break; |
| 392 | default: |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | /* FIXME |
| 397 | * |
| 398 | * It support I2S only now |
| 399 | */ |
| 400 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 401 | case SND_SOC_DAIFMT_I2S: |
| 402 | dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE; |
| 403 | break; |
| 404 | default: |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
| 408 | /* FIXME |
| 409 | * |
| 410 | * It support 64bit data transmission only now |
| 411 | */ |
| 412 | dai_cfg1 |= DA7210_DAI_FLEN_64BIT; |
| 413 | |
| 414 | da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1); |
| 415 | da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) |
| 421 | |
| 422 | /* DAI operations */ |
| 423 | static struct snd_soc_dai_ops da7210_dai_ops = { |
| 424 | .startup = da7210_startup, |
| 425 | .hw_params = da7210_hw_params, |
| 426 | .set_fmt = da7210_set_dai_fmt, |
| 427 | }; |
| 428 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 429 | static struct snd_soc_dai_driver da7210_dai = { |
| 430 | .name = "da7210-hifi", |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 431 | /* playback capabilities */ |
| 432 | .playback = { |
| 433 | .stream_name = "Playback", |
| 434 | .channels_min = 1, |
| 435 | .channels_max = 2, |
| 436 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 437 | .formats = DA7210_FORMATS, |
| 438 | }, |
| 439 | /* capture capabilities */ |
| 440 | .capture = { |
| 441 | .stream_name = "Capture", |
| 442 | .channels_min = 1, |
| 443 | .channels_max = 2, |
| 444 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 445 | .formats = DA7210_FORMATS, |
| 446 | }, |
| 447 | .ops = &da7210_dai_ops, |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 448 | .symmetric_rates = 1, |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 449 | }; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 450 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 451 | static int da7210_probe(struct snd_soc_codec *codec) |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 452 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 453 | struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 454 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 455 | dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 456 | |
Kuninori Morimoto | 4c62ed9 | 2010-09-16 13:07:06 +0900 | [diff] [blame] | 457 | codec->control_data = da7210->control_data; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 458 | codec->hw_write = (hw_write_t)i2c_master_send; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 459 | |
| 460 | /* FIXME |
| 461 | * |
| 462 | * This driver use fixed value here |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 463 | * And below settings expects MCLK = 12.288MHz |
| 464 | * |
| 465 | * When you select different MCLK, please check... |
| 466 | * DA7210_PLL_DIV1 val |
| 467 | * DA7210_PLL_DIV2 val |
| 468 | * DA7210_PLL_DIV3 val |
| 469 | * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 470 | */ |
| 471 | |
| 472 | /* |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 473 | * make sure that DA7210 use bypass mode before start up |
| 474 | */ |
| 475 | da7210_write(codec, DA7210_STARTUP1, 0); |
| 476 | da7210_write(codec, DA7210_PLL_DIV3, |
| 477 | DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP); |
| 478 | |
| 479 | /* |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 480 | * ADC settings |
| 481 | */ |
| 482 | |
| 483 | /* Enable Left & Right MIC PGA and Mic Bias */ |
| 484 | da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN); |
| 485 | da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN); |
| 486 | |
| 487 | /* Enable Left and Right input PGA */ |
| 488 | da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN); |
| 489 | da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN); |
| 490 | |
| 491 | /* Enable Left and Right ADC */ |
| 492 | da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN); |
| 493 | |
| 494 | /* |
| 495 | * DAC settings |
| 496 | */ |
| 497 | |
| 498 | /* Enable Left and Right DAC */ |
| 499 | da7210_write(codec, DA7210_DAC_SEL, |
| 500 | DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN | |
| 501 | DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN); |
| 502 | |
| 503 | /* Enable Left and Right out PGA */ |
| 504 | da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN); |
| 505 | da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN); |
| 506 | |
| 507 | /* Enable Left and Right HeadPhone PGA */ |
| 508 | da7210_write(codec, DA7210_HP_CFG, |
| 509 | DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN | |
| 510 | DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN); |
| 511 | |
| 512 | /* Diable PLL and bypass it */ |
| 513 | da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000); |
| 514 | |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 515 | /* |
| 516 | * If 48kHz sound came, it use bypass mode, |
| 517 | * and when it is 44.1kHz, it use PLL. |
| 518 | * |
| 519 | * This time, this driver sets PLL always ON |
| 520 | * and controls bypass/PLL mode by switching |
| 521 | * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit. |
| 522 | * see da7210_hw_params |
| 523 | */ |
| 524 | da7210_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */ |
| 525 | da7210_write(codec, DA7210_PLL_DIV2, 0x99); |
| 526 | da7210_write(codec, DA7210_PLL_DIV3, 0x0A | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 527 | DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP); |
Kuninori Morimoto | 960b3b4 | 2010-03-11 11:37:44 +0900 | [diff] [blame] | 528 | snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN); |
| 529 | |
| 530 | /* As suggested by Dialog */ |
| 531 | da7210_write(codec, DA7210_A_HID_UNLOCK, 0x8B); /* unlock */ |
| 532 | da7210_write(codec, DA7210_A_TEST_UNLOCK, 0xB4); |
| 533 | da7210_write(codec, DA7210_A_PLL1, 0x01); |
| 534 | da7210_write(codec, DA7210_A_CP_MODE, 0x7C); |
| 535 | da7210_write(codec, DA7210_A_HID_UNLOCK, 0x00); /* re-lock */ |
| 536 | da7210_write(codec, DA7210_A_TEST_UNLOCK, 0x00); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 537 | |
| 538 | /* Activate all enabled subsystem */ |
| 539 | da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN); |
| 540 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 541 | snd_soc_add_controls(codec, da7210_snd_controls, |
| 542 | ARRAY_SIZE(da7210_snd_controls)); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 543 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 544 | dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 545 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 546 | return 0; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 547 | } |
| 548 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 549 | static struct snd_soc_codec_driver soc_codec_dev_da7210 = { |
Kuninori Morimoto | 4c62ed9 | 2010-09-16 13:07:06 +0900 | [diff] [blame] | 550 | .probe = da7210_probe, |
| 551 | .read = da7210_read, |
| 552 | .write = da7210_write, |
| 553 | .reg_cache_size = ARRAY_SIZE(da7210_reg), |
| 554 | .reg_word_size = sizeof(u8), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 555 | .reg_cache_default = da7210_reg, |
| 556 | }; |
| 557 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 558 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 559 | static int __devinit da7210_i2c_probe(struct i2c_client *i2c, |
| 560 | const struct i2c_device_id *id) |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 561 | { |
| 562 | struct da7210_priv *da7210; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 563 | int ret; |
| 564 | |
| 565 | da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL); |
| 566 | if (!da7210) |
| 567 | return -ENOMEM; |
| 568 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 569 | i2c_set_clientdata(i2c, da7210); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 570 | da7210->control_data = i2c; |
| 571 | da7210->control_type = SND_SOC_I2C; |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 572 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 573 | ret = snd_soc_register_codec(&i2c->dev, |
| 574 | &soc_codec_dev_da7210, &da7210_dai, 1); |
| 575 | if (ret < 0) |
Axel Lin | 085efd2 | 2010-07-23 05:53:45 +0000 | [diff] [blame] | 576 | kfree(da7210); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 581 | static int __devexit da7210_i2c_remove(struct i2c_client *client) |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 582 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 583 | snd_soc_unregister_codec(&client->dev); |
| 584 | kfree(i2c_get_clientdata(client)); |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static const struct i2c_device_id da7210_i2c_id[] = { |
| 589 | { "da7210", 0 }, |
| 590 | { } |
| 591 | }; |
| 592 | MODULE_DEVICE_TABLE(i2c, da7210_i2c_id); |
| 593 | |
| 594 | /* I2C codec control layer */ |
| 595 | static struct i2c_driver da7210_i2c_driver = { |
| 596 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 597 | .name = "da7210-codec", |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 598 | .owner = THIS_MODULE, |
| 599 | }, |
Kuninori Morimoto | 4c62ed9 | 2010-09-16 13:07:06 +0900 | [diff] [blame] | 600 | .probe = da7210_i2c_probe, |
| 601 | .remove = __devexit_p(da7210_i2c_remove), |
| 602 | .id_table = da7210_i2c_id, |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 603 | }; |
| 604 | #endif |
| 605 | |
Kuninori Morimoto | 9861545 | 2009-12-14 13:21:56 +0900 | [diff] [blame] | 606 | static int __init da7210_modinit(void) |
| 607 | { |
| 608 | int ret = 0; |
| 609 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 610 | ret = i2c_add_driver(&da7210_i2c_driver); |
| 611 | #endif |
| 612 | return ret; |
| 613 | } |
| 614 | module_init(da7210_modinit); |
| 615 | |
| 616 | static void __exit da7210_exit(void) |
| 617 | { |
| 618 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 619 | i2c_del_driver(&da7210_i2c_driver); |
| 620 | #endif |
| 621 | } |
| 622 | module_exit(da7210_exit); |
| 623 | |
| 624 | MODULE_DESCRIPTION("ASoC DA7210 driver"); |
| 625 | MODULE_AUTHOR("David Chen, Kuninori Morimoto"); |
| 626 | MODULE_LICENSE("GPL"); |