Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 1 | /* |
| 2 | * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <morimoto.kuninori@renesas.com> |
| 6 | * |
| 7 | * Based on wm8731.c by Richard Purdie |
| 8 | * Based on ak4535.c by Richard Purdie |
| 9 | * Based on wm8753.c by Liam Girdwood |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | /* ** CAUTION ** |
| 17 | * |
| 18 | * This is very simple driver. |
| 19 | * It can use headphone output / stereo input only |
| 20 | * |
Kuninori Morimoto | 2021139 | 2011-11-06 22:04:53 -0800 | [diff] [blame] | 21 | * AK4642 is tested. |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 22 | * AK4643 is tested. |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 23 | * AK4648 is tested. |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 24 | */ |
| 25 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 26 | #include <linux/delay.h> |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 27 | #include <linux/i2c.h> |
| 28 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 30 | #include <linux/module.h> |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 31 | #include <sound/soc.h> |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 32 | #include <sound/initval.h> |
Kuninori Morimoto | a300de3 | 2010-07-01 14:23:45 +0900 | [diff] [blame] | 33 | #include <sound/tlv.h> |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 34 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 35 | #define AK4642_VERSION "0.0.1" |
| 36 | |
| 37 | #define PW_MGMT1 0x00 |
| 38 | #define PW_MGMT2 0x01 |
| 39 | #define SG_SL1 0x02 |
| 40 | #define SG_SL2 0x03 |
| 41 | #define MD_CTL1 0x04 |
| 42 | #define MD_CTL2 0x05 |
| 43 | #define TIMER 0x06 |
| 44 | #define ALC_CTL1 0x07 |
| 45 | #define ALC_CTL2 0x08 |
| 46 | #define L_IVC 0x09 |
| 47 | #define L_DVC 0x0a |
| 48 | #define ALC_CTL3 0x0b |
| 49 | #define R_IVC 0x0c |
| 50 | #define R_DVC 0x0d |
| 51 | #define MD_CTL3 0x0e |
| 52 | #define MD_CTL4 0x0f |
| 53 | #define PW_MGMT3 0x10 |
| 54 | #define DF_S 0x11 |
| 55 | #define FIL3_0 0x12 |
| 56 | #define FIL3_1 0x13 |
| 57 | #define FIL3_2 0x14 |
| 58 | #define FIL3_3 0x15 |
| 59 | #define EQ_0 0x16 |
| 60 | #define EQ_1 0x17 |
| 61 | #define EQ_2 0x18 |
| 62 | #define EQ_3 0x19 |
| 63 | #define EQ_4 0x1a |
| 64 | #define EQ_5 0x1b |
| 65 | #define FIL1_0 0x1c |
| 66 | #define FIL1_1 0x1d |
| 67 | #define FIL1_2 0x1e |
| 68 | #define FIL1_3 0x1f |
| 69 | #define PW_MGMT4 0x20 |
| 70 | #define MD_CTL5 0x21 |
| 71 | #define LO_MS 0x22 |
| 72 | #define HP_MS 0x23 |
| 73 | #define SPK_MS 0x24 |
| 74 | |
Kuninori Morimoto | a347123 | 2010-10-15 14:23:18 +0900 | [diff] [blame] | 75 | /* PW_MGMT1*/ |
| 76 | #define PMVCM (1 << 6) /* VCOM Power Management */ |
| 77 | #define PMMIN (1 << 5) /* MIN Input Power Management */ |
| 78 | #define PMDAC (1 << 2) /* DAC Power Management */ |
| 79 | #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */ |
| 80 | |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 81 | /* PW_MGMT2 */ |
| 82 | #define HPMTN (1 << 6) |
| 83 | #define PMHPL (1 << 5) |
| 84 | #define PMHPR (1 << 4) |
| 85 | #define MS (1 << 3) /* master/slave select */ |
| 86 | #define MCKO (1 << 1) |
| 87 | #define PMPLL (1 << 0) |
| 88 | |
| 89 | #define PMHP_MASK (PMHPL | PMHPR) |
| 90 | #define PMHP PMHP_MASK |
| 91 | |
Kuninori Morimoto | a347123 | 2010-10-15 14:23:18 +0900 | [diff] [blame] | 92 | /* PW_MGMT3 */ |
| 93 | #define PMADR (1 << 0) /* MIC L / ADC R Power Management */ |
| 94 | |
| 95 | /* SG_SL1 */ |
| 96 | #define MINS (1 << 6) /* Switch from MIN to Speaker */ |
| 97 | #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */ |
| 98 | #define PMMP (1 << 2) /* MPWR pin Power Management */ |
| 99 | #define MGAIN0 (1 << 0) /* MIC amp gain*/ |
| 100 | |
| 101 | /* TIMER */ |
| 102 | #define ZTM(param) ((param & 0x3) << 4) /* ALC Zoro Crossing TimeOut */ |
| 103 | #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2)) |
| 104 | |
| 105 | /* ALC_CTL1 */ |
| 106 | #define ALC (1 << 5) /* ALC Enable */ |
| 107 | #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */ |
| 108 | |
Kuninori Morimoto | 4b6316b | 2010-03-23 16:27:28 +0900 | [diff] [blame] | 109 | /* MD_CTL1 */ |
| 110 | #define PLL3 (1 << 7) |
| 111 | #define PLL2 (1 << 6) |
| 112 | #define PLL1 (1 << 5) |
| 113 | #define PLL0 (1 << 4) |
| 114 | #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0) |
| 115 | |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 116 | #define BCKO_MASK (1 << 3) |
| 117 | #define BCKO_64 BCKO_MASK |
| 118 | |
Kuninori Morimoto | cb9c130 | 2011-01-20 11:45:34 +0900 | [diff] [blame] | 119 | #define DIF_MASK (3 << 0) |
| 120 | #define DSP (0 << 0) |
| 121 | #define RIGHT_J (1 << 0) |
| 122 | #define LEFT_J (2 << 0) |
| 123 | #define I2S (3 << 0) |
| 124 | |
Kuninori Morimoto | 1ad747c | 2010-03-23 16:27:38 +0900 | [diff] [blame] | 125 | /* MD_CTL2 */ |
| 126 | #define FS0 (1 << 0) |
| 127 | #define FS1 (1 << 1) |
| 128 | #define FS2 (1 << 2) |
| 129 | #define FS3 (1 << 5) |
| 130 | #define FS_MASK (FS0 | FS1 | FS2 | FS3) |
| 131 | |
Kuninori Morimoto | a347123 | 2010-10-15 14:23:18 +0900 | [diff] [blame] | 132 | /* MD_CTL3 */ |
| 133 | #define BST1 (1 << 3) |
| 134 | |
| 135 | /* MD_CTL4 */ |
| 136 | #define DACH (1 << 0) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 137 | |
Kuninori Morimoto | a300de3 | 2010-07-01 14:23:45 +0900 | [diff] [blame] | 138 | /* |
| 139 | * Playback Volume (table 39) |
| 140 | * |
| 141 | * max : 0x00 : +12.0 dB |
| 142 | * ( 0.5 dB step ) |
| 143 | * min : 0xFE : -115.0 dB |
| 144 | * mute: 0xFF |
| 145 | */ |
| 146 | static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1); |
| 147 | |
| 148 | static const struct snd_kcontrol_new ak4642_snd_controls[] = { |
| 149 | |
| 150 | SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC, |
| 151 | 0, 0xFF, 1, out_tlv), |
Kuninori Morimoto | 3c70352 | 2011-11-10 16:21:42 -0800 | [diff] [blame] | 152 | |
| 153 | SOC_SINGLE("Headphone Switch", PW_MGMT2, 6, 1, 0), |
Kuninori Morimoto | a300de3 | 2010-07-01 14:23:45 +0900 | [diff] [blame] | 154 | }; |
| 155 | |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 156 | static const struct snd_kcontrol_new ak4642_hpout_mixer_controls[] = { |
| 157 | SOC_DAPM_SINGLE("DACH", MD_CTL4, 0, 1, 0), |
| 158 | }; |
| 159 | |
Kuninori Morimoto | e8c83db | 2011-11-10 16:21:55 -0800 | [diff] [blame] | 160 | static const struct snd_kcontrol_new ak4642_lout_mixer_controls[] = { |
| 161 | SOC_DAPM_SINGLE("DACL", SG_SL1, 4, 1, 0), |
| 162 | }; |
| 163 | |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 164 | static const struct snd_soc_dapm_widget ak4642_dapm_widgets[] = { |
| 165 | |
| 166 | /* Outputs */ |
| 167 | SND_SOC_DAPM_OUTPUT("HPOUTL"), |
| 168 | SND_SOC_DAPM_OUTPUT("HPOUTR"), |
Kuninori Morimoto | e8c83db | 2011-11-10 16:21:55 -0800 | [diff] [blame] | 169 | SND_SOC_DAPM_OUTPUT("LINEOUT"), |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 170 | |
| 171 | SND_SOC_DAPM_MIXER("HPOUTL Mixer", PW_MGMT2, 5, 0, |
| 172 | &ak4642_hpout_mixer_controls[0], |
| 173 | ARRAY_SIZE(ak4642_hpout_mixer_controls)), |
| 174 | |
| 175 | SND_SOC_DAPM_MIXER("HPOUTR Mixer", PW_MGMT2, 4, 0, |
| 176 | &ak4642_hpout_mixer_controls[0], |
| 177 | ARRAY_SIZE(ak4642_hpout_mixer_controls)), |
| 178 | |
Kuninori Morimoto | e8c83db | 2011-11-10 16:21:55 -0800 | [diff] [blame] | 179 | SND_SOC_DAPM_MIXER("LINEOUT Mixer", PW_MGMT1, 3, 0, |
| 180 | &ak4642_lout_mixer_controls[0], |
| 181 | ARRAY_SIZE(ak4642_lout_mixer_controls)), |
| 182 | |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 183 | /* DAC */ |
| 184 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", PW_MGMT1, 2, 0), |
| 185 | }; |
| 186 | |
| 187 | static const struct snd_soc_dapm_route ak4642_intercon[] = { |
| 188 | |
| 189 | /* Outputs */ |
| 190 | {"HPOUTL", NULL, "HPOUTL Mixer"}, |
| 191 | {"HPOUTR", NULL, "HPOUTR Mixer"}, |
Kuninori Morimoto | e8c83db | 2011-11-10 16:21:55 -0800 | [diff] [blame] | 192 | {"LINEOUT", NULL, "LINEOUT Mixer"}, |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 193 | |
| 194 | {"HPOUTL Mixer", "DACH", "DAC"}, |
| 195 | {"HPOUTR Mixer", "DACH", "DAC"}, |
Kuninori Morimoto | e8c83db | 2011-11-10 16:21:55 -0800 | [diff] [blame] | 196 | {"LINEOUT Mixer", "DACL", "DAC"}, |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 197 | }; |
Kuninori Morimoto | a300de3 | 2010-07-01 14:23:45 +0900 | [diff] [blame] | 198 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 199 | /* codec private data */ |
| 200 | struct ak4642_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 201 | unsigned int sysclk; |
| 202 | enum snd_soc_control_type control_type; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 203 | }; |
| 204 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 205 | /* |
| 206 | * ak4642 register cache |
| 207 | */ |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 208 | static const u8 ak4642_reg[] = { |
Kuninori Morimoto | 19b115e | 2011-10-13 02:03:54 -0700 | [diff] [blame] | 209 | 0x00, 0x00, 0x01, 0x00, |
| 210 | 0x02, 0x00, 0x00, 0x00, |
| 211 | 0xe1, 0xe1, 0x18, 0x00, |
| 212 | 0xe1, 0x18, 0x11, 0x08, |
| 213 | 0x00, 0x00, 0x00, 0x00, |
| 214 | 0x00, 0x00, 0x00, 0x00, |
| 215 | 0x00, 0x00, 0x00, 0x00, |
| 216 | 0x00, 0x00, 0x00, 0x00, |
| 217 | 0x00, 0x00, 0x00, 0x00, |
| 218 | 0x00, |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 219 | }; |
| 220 | |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 221 | static const u8 ak4648_reg[] = { |
| 222 | 0x00, 0x00, 0x01, 0x00, |
| 223 | 0x02, 0x00, 0x00, 0x00, |
| 224 | 0xe1, 0xe1, 0x18, 0x00, |
| 225 | 0xe1, 0x18, 0x11, 0xb8, |
| 226 | 0x00, 0x00, 0x00, 0x00, |
| 227 | 0x00, 0x00, 0x00, 0x00, |
| 228 | 0x00, 0x00, 0x00, 0x00, |
| 229 | 0x00, 0x00, 0x00, 0x00, |
| 230 | 0x00, 0x00, 0x00, 0x00, |
| 231 | 0x00, 0x88, 0x88, 0x08, |
| 232 | }; |
| 233 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 234 | static int ak4642_dai_startup(struct snd_pcm_substream *substream, |
| 235 | struct snd_soc_dai *dai) |
| 236 | { |
| 237 | int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 238 | struct snd_soc_codec *codec = dai->codec; |
| 239 | |
| 240 | if (is_play) { |
| 241 | /* |
| 242 | * start headphone output |
| 243 | * |
| 244 | * PLL, Master Mode |
| 245 | * Audio I/F Format :MSB justified (ADC & DAC) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 246 | * Bass Boost Level : Middle |
| 247 | * |
| 248 | * This operation came from example code of |
| 249 | * "ASAHI KASEI AK4642" (japanese) manual p97. |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 250 | */ |
Axel Lin | b91470b | 2011-10-11 20:20:53 +0800 | [diff] [blame] | 251 | snd_soc_write(codec, L_IVC, 0x91); /* volume */ |
| 252 | snd_soc_write(codec, R_IVC, 0x91); /* volume */ |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 253 | } else { |
| 254 | /* |
| 255 | * start stereo input |
| 256 | * |
| 257 | * PLL Master Mode |
| 258 | * Audio I/F Format:MSB justified (ADC & DAC) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 259 | * Pre MIC AMP:+20dB |
| 260 | * MIC Power On |
| 261 | * ALC setting:Refer to Table 35 |
| 262 | * ALC bit=“1” |
| 263 | * |
| 264 | * This operation came from example code of |
| 265 | * "ASAHI KASEI AK4642" (japanese) manual p94. |
| 266 | */ |
Axel Lin | b91470b | 2011-10-11 20:20:53 +0800 | [diff] [blame] | 267 | snd_soc_write(codec, SG_SL1, PMMP | MGAIN0); |
| 268 | snd_soc_write(codec, TIMER, ZTM(0x3) | WTM(0x3)); |
| 269 | snd_soc_write(codec, ALC_CTL1, ALC | LMTH0); |
Kuninori Morimoto | ed2dd7d | 2011-11-10 16:21:01 -0800 | [diff] [blame] | 270 | snd_soc_update_bits(codec, PW_MGMT1, PMADL, PMADL); |
Kuninori Morimoto | a347123 | 2010-10-15 14:23:18 +0900 | [diff] [blame] | 271 | snd_soc_update_bits(codec, PW_MGMT3, PMADR, PMADR); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static void ak4642_dai_shutdown(struct snd_pcm_substream *substream, |
| 278 | struct snd_soc_dai *dai) |
| 279 | { |
| 280 | int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 281 | struct snd_soc_codec *codec = dai->codec; |
| 282 | |
| 283 | if (is_play) { |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 284 | } else { |
| 285 | /* stop stereo input */ |
Kuninori Morimoto | a347123 | 2010-10-15 14:23:18 +0900 | [diff] [blame] | 286 | snd_soc_update_bits(codec, PW_MGMT1, PMADL, 0); |
| 287 | snd_soc_update_bits(codec, PW_MGMT3, PMADR, 0); |
| 288 | snd_soc_update_bits(codec, ALC_CTL1, ALC, 0); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai, |
| 293 | int clk_id, unsigned int freq, int dir) |
| 294 | { |
| 295 | struct snd_soc_codec *codec = codec_dai->codec; |
Kuninori Morimoto | 4b6316b | 2010-03-23 16:27:28 +0900 | [diff] [blame] | 296 | u8 pll; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 297 | |
Kuninori Morimoto | 4b6316b | 2010-03-23 16:27:28 +0900 | [diff] [blame] | 298 | switch (freq) { |
| 299 | case 11289600: |
| 300 | pll = PLL2; |
| 301 | break; |
| 302 | case 12288000: |
| 303 | pll = PLL2 | PLL0; |
| 304 | break; |
| 305 | case 12000000: |
| 306 | pll = PLL2 | PLL1; |
| 307 | break; |
| 308 | case 24000000: |
| 309 | pll = PLL2 | PLL1 | PLL0; |
| 310 | break; |
| 311 | case 13500000: |
| 312 | pll = PLL3 | PLL2; |
| 313 | break; |
| 314 | case 27000000: |
| 315 | pll = PLL3 | PLL2 | PLL0; |
| 316 | break; |
| 317 | default: |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll); |
| 321 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 325 | static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 326 | { |
| 327 | struct snd_soc_codec *codec = dai->codec; |
| 328 | u8 data; |
| 329 | u8 bcko; |
| 330 | |
| 331 | data = MCKO | PMPLL; /* use MCKO */ |
| 332 | bcko = 0; |
| 333 | |
| 334 | /* set master/slave audio interface */ |
| 335 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 336 | case SND_SOC_DAIFMT_CBM_CFM: |
| 337 | data |= MS; |
| 338 | bcko = BCKO_64; |
| 339 | break; |
| 340 | case SND_SOC_DAIFMT_CBS_CFS: |
| 341 | break; |
| 342 | default: |
| 343 | return -EINVAL; |
| 344 | } |
Kuninori Morimoto | bd7fdbc | 2011-07-06 17:58:56 -0700 | [diff] [blame] | 345 | snd_soc_update_bits(codec, PW_MGMT2, MS | MCKO | PMPLL, data); |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 346 | snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko); |
| 347 | |
Kuninori Morimoto | cb9c130 | 2011-01-20 11:45:34 +0900 | [diff] [blame] | 348 | /* format type */ |
| 349 | data = 0; |
| 350 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 351 | case SND_SOC_DAIFMT_LEFT_J: |
| 352 | data = LEFT_J; |
| 353 | break; |
| 354 | case SND_SOC_DAIFMT_I2S: |
| 355 | data = I2S; |
| 356 | break; |
| 357 | /* FIXME |
| 358 | * Please add RIGHT_J / DSP support here |
| 359 | */ |
| 360 | default: |
| 361 | return -EINVAL; |
| 362 | break; |
| 363 | } |
| 364 | snd_soc_update_bits(codec, MD_CTL1, DIF_MASK, data); |
| 365 | |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
Kuninori Morimoto | 1ad747c | 2010-03-23 16:27:38 +0900 | [diff] [blame] | 369 | static int ak4642_dai_hw_params(struct snd_pcm_substream *substream, |
| 370 | struct snd_pcm_hw_params *params, |
| 371 | struct snd_soc_dai *dai) |
| 372 | { |
| 373 | struct snd_soc_codec *codec = dai->codec; |
| 374 | u8 rate; |
| 375 | |
| 376 | switch (params_rate(params)) { |
| 377 | case 7350: |
| 378 | rate = FS2; |
| 379 | break; |
| 380 | case 8000: |
| 381 | rate = 0; |
| 382 | break; |
| 383 | case 11025: |
| 384 | rate = FS2 | FS0; |
| 385 | break; |
| 386 | case 12000: |
| 387 | rate = FS0; |
| 388 | break; |
| 389 | case 14700: |
| 390 | rate = FS2 | FS1; |
| 391 | break; |
| 392 | case 16000: |
| 393 | rate = FS1; |
| 394 | break; |
| 395 | case 22050: |
| 396 | rate = FS2 | FS1 | FS0; |
| 397 | break; |
| 398 | case 24000: |
| 399 | rate = FS1 | FS0; |
| 400 | break; |
| 401 | case 29400: |
| 402 | rate = FS3 | FS2 | FS1; |
| 403 | break; |
| 404 | case 32000: |
| 405 | rate = FS3 | FS1; |
| 406 | break; |
| 407 | case 44100: |
| 408 | rate = FS3 | FS2 | FS1 | FS0; |
| 409 | break; |
| 410 | case 48000: |
| 411 | rate = FS3 | FS1 | FS0; |
| 412 | break; |
| 413 | default: |
| 414 | return -EINVAL; |
| 415 | break; |
| 416 | } |
| 417 | snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate); |
| 418 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Kuninori Morimoto | ed2dd7d | 2011-11-10 16:21:01 -0800 | [diff] [blame] | 422 | static int ak4642_set_bias_level(struct snd_soc_codec *codec, |
| 423 | enum snd_soc_bias_level level) |
| 424 | { |
| 425 | switch (level) { |
| 426 | case SND_SOC_BIAS_OFF: |
| 427 | snd_soc_write(codec, PW_MGMT1, 0x00); |
| 428 | break; |
| 429 | default: |
| 430 | snd_soc_update_bits(codec, PW_MGMT1, PMVCM, PMVCM); |
| 431 | break; |
| 432 | } |
| 433 | codec->dapm.bias_level = level; |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame^] | 438 | static const struct snd_soc_dai_ops ak4642_dai_ops = { |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 439 | .startup = ak4642_dai_startup, |
| 440 | .shutdown = ak4642_dai_shutdown, |
| 441 | .set_sysclk = ak4642_dai_set_sysclk, |
Kuninori Morimoto | 0643ce8 | 2010-03-15 18:10:50 +0900 | [diff] [blame] | 442 | .set_fmt = ak4642_dai_set_fmt, |
Kuninori Morimoto | 1ad747c | 2010-03-23 16:27:38 +0900 | [diff] [blame] | 443 | .hw_params = ak4642_dai_hw_params, |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 444 | }; |
| 445 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 446 | static struct snd_soc_dai_driver ak4642_dai = { |
| 447 | .name = "ak4642-hifi", |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 448 | .playback = { |
| 449 | .stream_name = "Playback", |
| 450 | .channels_min = 1, |
| 451 | .channels_max = 2, |
| 452 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 453 | .formats = SNDRV_PCM_FMTBIT_S16_LE }, |
| 454 | .capture = { |
| 455 | .stream_name = "Capture", |
| 456 | .channels_min = 1, |
| 457 | .channels_max = 2, |
| 458 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 459 | .formats = SNDRV_PCM_FMTBIT_S16_LE }, |
| 460 | .ops = &ak4642_dai_ops, |
Kuninori Morimoto | 1ad747c | 2010-03-23 16:27:38 +0900 | [diff] [blame] | 461 | .symmetric_rates = 1, |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 462 | }; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 463 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 464 | static int ak4642_resume(struct snd_soc_codec *codec) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 465 | { |
Axel Lin | b91470b | 2011-10-11 20:20:53 +0800 | [diff] [blame] | 466 | snd_soc_cache_sync(codec); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 470 | |
| 471 | static int ak4642_probe(struct snd_soc_codec *codec) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 472 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 473 | struct ak4642_priv *ak4642 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | b91470b | 2011-10-11 20:20:53 +0800 | [diff] [blame] | 474 | int ret; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 475 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 476 | dev_info(codec->dev, "AK4642 Audio Codec %s", AK4642_VERSION); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 477 | |
Axel Lin | b91470b | 2011-10-11 20:20:53 +0800 | [diff] [blame] | 478 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, ak4642->control_type); |
| 479 | if (ret < 0) { |
| 480 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 481 | return ret; |
| 482 | } |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 483 | |
Kuninori Morimoto | 73bb379 | 2010-09-03 16:12:36 +0900 | [diff] [blame] | 484 | snd_soc_add_controls(codec, ak4642_snd_controls, |
| 485 | ARRAY_SIZE(ak4642_snd_controls)); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 486 | |
Kuninori Morimoto | ed2dd7d | 2011-11-10 16:21:01 -0800 | [diff] [blame] | 487 | ak4642_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int ak4642_remove(struct snd_soc_codec *codec) |
| 493 | { |
| 494 | ak4642_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 495 | return 0; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 496 | } |
| 497 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 498 | static struct snd_soc_codec_driver soc_codec_dev_ak4642 = { |
Kuninori Morimoto | 0ce75aa | 2010-09-16 13:06:40 +0900 | [diff] [blame] | 499 | .probe = ak4642_probe, |
Kuninori Morimoto | ed2dd7d | 2011-11-10 16:21:01 -0800 | [diff] [blame] | 500 | .remove = ak4642_remove, |
Kuninori Morimoto | 0ce75aa | 2010-09-16 13:06:40 +0900 | [diff] [blame] | 501 | .resume = ak4642_resume, |
Kuninori Morimoto | ed2dd7d | 2011-11-10 16:21:01 -0800 | [diff] [blame] | 502 | .set_bias_level = ak4642_set_bias_level, |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 503 | .reg_cache_default = ak4642_reg, /* ak4642 reg */ |
| 504 | .reg_cache_size = ARRAY_SIZE(ak4642_reg), /* ak4642 reg */ |
Kuninori Morimoto | 0ce75aa | 2010-09-16 13:06:40 +0900 | [diff] [blame] | 505 | .reg_word_size = sizeof(u8), |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 506 | .dapm_widgets = ak4642_dapm_widgets, |
| 507 | .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets), |
| 508 | .dapm_routes = ak4642_intercon, |
| 509 | .num_dapm_routes = ARRAY_SIZE(ak4642_intercon), |
| 510 | }; |
| 511 | |
| 512 | static struct snd_soc_codec_driver soc_codec_dev_ak4648 = { |
| 513 | .probe = ak4642_probe, |
| 514 | .remove = ak4642_remove, |
| 515 | .resume = ak4642_resume, |
| 516 | .set_bias_level = ak4642_set_bias_level, |
| 517 | .reg_cache_default = ak4648_reg, /* ak4648 reg */ |
| 518 | .reg_cache_size = ARRAY_SIZE(ak4648_reg), /* ak4648 reg */ |
| 519 | .reg_word_size = sizeof(u8), |
Kuninori Morimoto | 24747da | 2011-11-10 16:21:31 -0800 | [diff] [blame] | 520 | .dapm_widgets = ak4642_dapm_widgets, |
| 521 | .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets), |
| 522 | .dapm_routes = ak4642_intercon, |
| 523 | .num_dapm_routes = ARRAY_SIZE(ak4642_intercon), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 526 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 527 | static __devinit int ak4642_i2c_probe(struct i2c_client *i2c, |
| 528 | const struct i2c_device_id *id) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 529 | { |
| 530 | struct ak4642_priv *ak4642; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 531 | int ret; |
| 532 | |
| 533 | ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL); |
Kuninori Morimoto | 0ce75aa | 2010-09-16 13:06:40 +0900 | [diff] [blame] | 534 | if (!ak4642) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 535 | return -ENOMEM; |
| 536 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 537 | i2c_set_clientdata(i2c, ak4642); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 538 | ak4642->control_type = SND_SOC_I2C; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 539 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 540 | ret = snd_soc_register_codec(&i2c->dev, |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 541 | (struct snd_soc_codec_driver *)id->driver_data, |
| 542 | &ak4642_dai, 1); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 543 | if (ret < 0) |
Axel Lin | 7bcaad9 | 2010-07-23 05:53:44 +0000 | [diff] [blame] | 544 | kfree(ak4642); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 545 | return ret; |
| 546 | } |
| 547 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 548 | static __devexit int ak4642_i2c_remove(struct i2c_client *client) |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 549 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 550 | snd_soc_unregister_codec(&client->dev); |
| 551 | kfree(i2c_get_clientdata(client)); |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static const struct i2c_device_id ak4642_i2c_id[] = { |
Kuninori Morimoto | a9317e8 | 2011-11-10 16:22:05 -0800 | [diff] [blame] | 556 | { "ak4642", (kernel_ulong_t)&soc_codec_dev_ak4642 }, |
| 557 | { "ak4643", (kernel_ulong_t)&soc_codec_dev_ak4642 }, |
| 558 | { "ak4648", (kernel_ulong_t)&soc_codec_dev_ak4648 }, |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 559 | { } |
| 560 | }; |
| 561 | MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id); |
| 562 | |
| 563 | static struct i2c_driver ak4642_i2c_driver = { |
| 564 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 565 | .name = "ak4642-codec", |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 566 | .owner = THIS_MODULE, |
| 567 | }, |
Kuninori Morimoto | 0ce75aa | 2010-09-16 13:06:40 +0900 | [diff] [blame] | 568 | .probe = ak4642_i2c_probe, |
| 569 | .remove = __devexit_p(ak4642_i2c_remove), |
| 570 | .id_table = ak4642_i2c_id, |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 571 | }; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 572 | #endif |
| 573 | |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 574 | static int __init ak4642_modinit(void) |
| 575 | { |
Kuninori Morimoto | 1cf86f6 | 2009-12-15 15:54:21 +0900 | [diff] [blame] | 576 | int ret = 0; |
Kuninori Morimoto | a3a83d9 | 2009-08-21 10:23:41 +0900 | [diff] [blame] | 577 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 578 | ret = i2c_add_driver(&ak4642_i2c_driver); |
| 579 | #endif |
| 580 | return ret; |
| 581 | |
| 582 | } |
| 583 | module_init(ak4642_modinit); |
| 584 | |
| 585 | static void __exit ak4642_exit(void) |
| 586 | { |
| 587 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 588 | i2c_del_driver(&ak4642_i2c_driver); |
| 589 | #endif |
| 590 | |
| 591 | } |
| 592 | module_exit(ak4642_exit); |
| 593 | |
| 594 | MODULE_DESCRIPTION("Soc AK4642 driver"); |
| 595 | MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>"); |
| 596 | MODULE_LICENSE("GPL"); |