Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * CS4270 ALSA SoC (ASoC) codec driver |
| 3 | * |
| 4 | * Author: Timur Tabi <timur@freescale.com> |
| 5 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 6 | * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed |
| 7 | * under the terms of the GNU General Public License version 2. This |
| 8 | * program is licensed "as is" without any warranty of any kind, whether |
| 9 | * express or implied. |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 10 | * |
| 11 | * This is an ASoC device driver for the Cirrus Logic CS4270 codec. |
| 12 | * |
| 13 | * Current features/limitations: |
| 14 | * |
Daniel Mack | b191f63 | 2009-03-08 17:51:52 +0100 | [diff] [blame] | 15 | * - Software mode is supported. Stand-alone mode is not supported. |
| 16 | * - Only I2C is supported, not SPI |
| 17 | * - Support for master and slave mode |
| 18 | * - The machine driver's 'startup' function must call |
| 19 | * cs4270_set_dai_sysclk() with the value of MCLK. |
| 20 | * - Only I2S and left-justified modes are supported |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 21 | * - Power management is supported |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 26 | #include <sound/core.h> |
| 27 | #include <sound/soc.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <linux/i2c.h> |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 30 | #include <linux/delay.h> |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 31 | #include <linux/regulator/consumer.h> |
Daniel Mack | 85d07e4 | 2012-07-25 15:28:34 +0200 | [diff] [blame] | 32 | #include <linux/of_device.h> |
Daniel Mack | 0228619 | 2012-07-25 15:28:35 +0200 | [diff] [blame] | 33 | #include <linux/of_gpio.h> |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 34 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 35 | /* |
| 36 | * The codec isn't really big-endian or little-endian, since the I2S |
| 37 | * interface requires data to be sent serially with the MSbit first. |
| 38 | * However, to support BE and LE I2S devices, we specify both here. That |
| 39 | * way, ALSA will always match the bit patterns. |
| 40 | */ |
| 41 | #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 42 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 43 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \ |
| 44 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 45 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ |
| 46 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) |
| 47 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 48 | /* CS4270 registers addresses */ |
| 49 | #define CS4270_CHIPID 0x01 /* Chip ID */ |
| 50 | #define CS4270_PWRCTL 0x02 /* Power Control */ |
| 51 | #define CS4270_MODE 0x03 /* Mode Control */ |
| 52 | #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */ |
| 53 | #define CS4270_TRANS 0x05 /* Transition Control */ |
| 54 | #define CS4270_MUTE 0x06 /* Mute Control */ |
| 55 | #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */ |
| 56 | #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */ |
| 57 | |
| 58 | #define CS4270_FIRSTREG 0x01 |
| 59 | #define CS4270_LASTREG 0x08 |
| 60 | #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1) |
Daniel Mack | 80ab881 | 2009-05-05 11:25:00 +0200 | [diff] [blame] | 61 | #define CS4270_I2C_INCR 0x80 |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 62 | |
| 63 | /* Bit masks for the CS4270 registers */ |
| 64 | #define CS4270_CHIPID_ID 0xF0 |
| 65 | #define CS4270_CHIPID_REV 0x0F |
| 66 | #define CS4270_PWRCTL_FREEZE 0x80 |
| 67 | #define CS4270_PWRCTL_PDN_ADC 0x20 |
| 68 | #define CS4270_PWRCTL_PDN_DAC 0x02 |
| 69 | #define CS4270_PWRCTL_PDN 0x01 |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 70 | #define CS4270_PWRCTL_PDN_ALL \ |
| 71 | (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 72 | #define CS4270_MODE_SPEED_MASK 0x30 |
| 73 | #define CS4270_MODE_1X 0x00 |
| 74 | #define CS4270_MODE_2X 0x10 |
| 75 | #define CS4270_MODE_4X 0x20 |
| 76 | #define CS4270_MODE_SLAVE 0x30 |
| 77 | #define CS4270_MODE_DIV_MASK 0x0E |
| 78 | #define CS4270_MODE_DIV1 0x00 |
| 79 | #define CS4270_MODE_DIV15 0x02 |
| 80 | #define CS4270_MODE_DIV2 0x04 |
| 81 | #define CS4270_MODE_DIV3 0x06 |
| 82 | #define CS4270_MODE_DIV4 0x08 |
| 83 | #define CS4270_MODE_POPGUARD 0x01 |
| 84 | #define CS4270_FORMAT_FREEZE_A 0x80 |
| 85 | #define CS4270_FORMAT_FREEZE_B 0x40 |
| 86 | #define CS4270_FORMAT_LOOPBACK 0x20 |
| 87 | #define CS4270_FORMAT_DAC_MASK 0x18 |
| 88 | #define CS4270_FORMAT_DAC_LJ 0x00 |
| 89 | #define CS4270_FORMAT_DAC_I2S 0x08 |
| 90 | #define CS4270_FORMAT_DAC_RJ16 0x18 |
| 91 | #define CS4270_FORMAT_DAC_RJ24 0x10 |
| 92 | #define CS4270_FORMAT_ADC_MASK 0x01 |
| 93 | #define CS4270_FORMAT_ADC_LJ 0x00 |
| 94 | #define CS4270_FORMAT_ADC_I2S 0x01 |
| 95 | #define CS4270_TRANS_ONE_VOL 0x80 |
| 96 | #define CS4270_TRANS_SOFT 0x40 |
| 97 | #define CS4270_TRANS_ZERO 0x20 |
| 98 | #define CS4270_TRANS_INV_ADC_A 0x08 |
| 99 | #define CS4270_TRANS_INV_ADC_B 0x10 |
| 100 | #define CS4270_TRANS_INV_DAC_A 0x02 |
| 101 | #define CS4270_TRANS_INV_DAC_B 0x04 |
| 102 | #define CS4270_TRANS_DEEMPH 0x01 |
| 103 | #define CS4270_MUTE_AUTO 0x20 |
| 104 | #define CS4270_MUTE_ADC_A 0x08 |
| 105 | #define CS4270_MUTE_ADC_B 0x10 |
| 106 | #define CS4270_MUTE_POLARITY 0x04 |
| 107 | #define CS4270_MUTE_DAC_A 0x01 |
| 108 | #define CS4270_MUTE_DAC_B 0x02 |
| 109 | |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 110 | /* Power-on default values for the registers |
| 111 | * |
| 112 | * This array contains the power-on default values of the registers, with the |
| 113 | * exception of the "CHIPID" register (01h). The lower four bits of that |
| 114 | * register contain the hardware revision, so it is treated as volatile. |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 115 | */ |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 116 | static const struct reg_default cs4270_reg_defaults[] = { |
| 117 | { 2, 0x00 }, |
| 118 | { 3, 0x30 }, |
| 119 | { 4, 0x00 }, |
| 120 | { 5, 0x60 }, |
| 121 | { 6, 0x20 }, |
| 122 | { 7, 0x00 }, |
| 123 | { 8, 0x00 }, |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 124 | }; |
| 125 | |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 126 | static const char *supply_names[] = { |
| 127 | "va", "vd", "vlc" |
| 128 | }; |
| 129 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 130 | /* Private data for the CS4270 */ |
| 131 | struct cs4270_private { |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 132 | struct regmap *regmap; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 133 | unsigned int mclk; /* Input frequency of the MCLK pin */ |
| 134 | unsigned int mode; /* The mode (I2S or left-justified) */ |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 135 | unsigned int slave_mode; |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 136 | unsigned int manual_mute; |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 137 | |
| 138 | /* power domain regulators */ |
| 139 | struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 140 | }; |
| 141 | |
Mark Brown | 782fbab | 2013-08-11 12:29:07 +0100 | [diff] [blame] | 142 | static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = { |
| 143 | SND_SOC_DAPM_INPUT("AINL"), |
| 144 | SND_SOC_DAPM_INPUT("AINR"), |
| 145 | |
| 146 | SND_SOC_DAPM_OUTPUT("AOUTL"), |
| 147 | SND_SOC_DAPM_OUTPUT("AOUTR"), |
| 148 | }; |
| 149 | |
| 150 | static const struct snd_soc_dapm_route cs4270_dapm_routes[] = { |
| 151 | { "Capture", NULL, "AINA" }, |
| 152 | { "Capture", NULL, "AINB" }, |
| 153 | |
| 154 | { "AOUTA", NULL, "Playback" }, |
| 155 | { "AOUTB", NULL, "Playback" }, |
| 156 | }; |
| 157 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 158 | /** |
| 159 | * struct cs4270_mode_ratios - clock ratio tables |
| 160 | * @ratio: the ratio of MCLK to the sample rate |
| 161 | * @speed_mode: the Speed Mode bits to set in the Mode Control register for |
| 162 | * this ratio |
| 163 | * @mclk: the Ratio Select bits to set in the Mode Control register for this |
| 164 | * ratio |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 165 | * |
| 166 | * The data for this chart is taken from Table 5 of the CS4270 reference |
| 167 | * manual. |
| 168 | * |
| 169 | * This table is used to determine how to program the Mode Control register. |
| 170 | * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling |
| 171 | * rates the CS4270 currently supports. |
| 172 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 173 | * @speed_mode is the corresponding bit pattern to be written to the |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 174 | * MODE bits of the Mode Control Register |
| 175 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 176 | * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 177 | * the Mode Control Register. |
| 178 | * |
| 179 | * In situations where a single ratio is represented by multiple speed |
| 180 | * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick |
| 181 | * double-speed instead of quad-speed. However, the CS4270 errata states |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 182 | * that divide-By-1.5 can cause failures, so we avoid that mode where |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 183 | * possible. |
| 184 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 185 | * Errata: There is an errata for the CS4270 where divide-by-1.5 does not |
| 186 | * work if Vd is 3.3V. If this effects you, select the |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 187 | * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will |
| 188 | * never select any sample rates that require divide-by-1.5. |
| 189 | */ |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 190 | struct cs4270_mode_ratios { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 191 | unsigned int ratio; |
| 192 | u8 speed_mode; |
| 193 | u8 mclk; |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 194 | }; |
| 195 | |
Timur Tabi | d9fb7fb | 2009-02-02 14:50:45 -0600 | [diff] [blame] | 196 | static struct cs4270_mode_ratios cs4270_mode_ratios[] = { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 197 | {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, |
| 198 | #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA |
| 199 | {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, |
| 200 | #endif |
| 201 | {128, CS4270_MODE_2X, CS4270_MODE_DIV1}, |
| 202 | {192, CS4270_MODE_4X, CS4270_MODE_DIV3}, |
| 203 | {256, CS4270_MODE_1X, CS4270_MODE_DIV1}, |
| 204 | {384, CS4270_MODE_2X, CS4270_MODE_DIV3}, |
| 205 | {512, CS4270_MODE_1X, CS4270_MODE_DIV2}, |
| 206 | {768, CS4270_MODE_1X, CS4270_MODE_DIV3}, |
| 207 | {1024, CS4270_MODE_1X, CS4270_MODE_DIV4} |
| 208 | }; |
| 209 | |
| 210 | /* The number of MCLK/LRCK ratios supported by the CS4270 */ |
| 211 | #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) |
| 212 | |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 213 | static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg) |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 214 | { |
| 215 | return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG); |
| 216 | } |
| 217 | |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 218 | static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg) |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 219 | { |
| 220 | /* Unreadable registers are considered volatile */ |
| 221 | if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG)) |
| 222 | return 1; |
| 223 | |
| 224 | return reg == CS4270_CHIPID; |
| 225 | } |
| 226 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 227 | /** |
| 228 | * cs4270_set_dai_sysclk - determine the CS4270 samples rates. |
| 229 | * @codec_dai: the codec DAI |
| 230 | * @clk_id: the clock ID (ignored) |
| 231 | * @freq: the MCLK input frequency |
| 232 | * @dir: the clock direction (ignored) |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 233 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 234 | * This function is used to tell the codec driver what the input MCLK |
| 235 | * frequency is. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 236 | * |
| 237 | * The value of MCLK is used to determine which sample rates are supported |
| 238 | * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 239 | * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 240 | * |
| 241 | * This function calculates the nine ratios and determines which ones match |
| 242 | * a standard sample rate. If there's a match, then it is added to the list |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 243 | * of supported sample rates. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 244 | * |
| 245 | * This function must be called by the machine driver's 'startup' function, |
| 246 | * otherwise the list of supported sample rates will not be available in |
| 247 | * time for ALSA. |
Daniel Mack | 6aababd | 2010-01-15 17:36:48 +0100 | [diff] [blame] | 248 | * |
| 249 | * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause |
| 250 | * theoretically possible sample rates to be enabled. Call it again with a |
| 251 | * proper value set one the external clock is set (most probably you would do |
| 252 | * that from a machine's driver 'hw_param' hook. |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 253 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 254 | static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 255 | int clk_id, unsigned int freq, int dir) |
| 256 | { |
| 257 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 258 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 259 | |
| 260 | cs4270->mclk = freq; |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 264 | /** |
| 265 | * cs4270_set_dai_fmt - configure the codec for the selected audio format |
| 266 | * @codec_dai: the codec DAI |
| 267 | * @format: a SND_SOC_DAIFMT_x value indicating the data format |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 268 | * |
| 269 | * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the |
| 270 | * codec accordingly. |
| 271 | * |
| 272 | * Currently, this function only supports SND_SOC_DAIFMT_I2S and |
| 273 | * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified |
| 274 | * data for playback only, but ASoC currently does not support different |
| 275 | * formats for playback vs. record. |
| 276 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 277 | static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 278 | unsigned int format) |
| 279 | { |
| 280 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 281 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 282 | |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 283 | /* set DAI format */ |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 284 | switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 285 | case SND_SOC_DAIFMT_I2S: |
| 286 | case SND_SOC_DAIFMT_LEFT_J: |
| 287 | cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; |
| 288 | break; |
| 289 | default: |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 290 | dev_err(codec->dev, "invalid dai format\n"); |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 291 | return -EINVAL; |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 294 | /* set master/slave audio interface */ |
| 295 | switch (format & SND_SOC_DAIFMT_MASTER_MASK) { |
| 296 | case SND_SOC_DAIFMT_CBS_CFS: |
| 297 | cs4270->slave_mode = 1; |
| 298 | break; |
| 299 | case SND_SOC_DAIFMT_CBM_CFM: |
| 300 | cs4270->slave_mode = 0; |
| 301 | break; |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 302 | default: |
Daniel Mack | ff09d49 | 2009-02-28 13:21:03 +0100 | [diff] [blame] | 303 | /* all other modes are unsupported by the hardware */ |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 304 | dev_err(codec->dev, "Unknown master/slave configuration\n"); |
| 305 | return -EINVAL; |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Axel Lin | ac60155 | 2011-10-06 07:29:56 +0800 | [diff] [blame] | 308 | return 0; |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 311 | /** |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 312 | * cs4270_hw_params - program the CS4270 with the given hardware parameters. |
| 313 | * @substream: the audio stream |
| 314 | * @params: the hardware parameters to set |
| 315 | * @dai: the SOC DAI (ignored) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 316 | * |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 317 | * This function programs the hardware with the values provided. |
| 318 | * Specifically, the sample rate and the data format. |
| 319 | * |
| 320 | * The .ops functions are used to provide board-specific data, like input |
| 321 | * frequencies, to this driver. This function takes that information, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 322 | * combines it with the hardware parameters provided, and programs the |
| 323 | * hardware accordingly. |
| 324 | */ |
| 325 | static int cs4270_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 326 | struct snd_pcm_hw_params *params, |
| 327 | struct snd_soc_dai *dai) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 328 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 329 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 330 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Roel Kluin | e34ba21 | 2008-04-17 18:58:34 +0200 | [diff] [blame] | 331 | int ret; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 332 | unsigned int i; |
| 333 | unsigned int rate; |
| 334 | unsigned int ratio; |
| 335 | int reg; |
| 336 | |
| 337 | /* Figure out which MCLK/LRCK ratio to use */ |
| 338 | |
| 339 | rate = params_rate(params); /* Sampling rate, in Hz */ |
| 340 | ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */ |
| 341 | |
Timur Tabi | 9dbd627 | 2007-08-01 12:22:07 +0200 | [diff] [blame] | 342 | for (i = 0; i < NUM_MCLK_RATIOS; i++) { |
Timur Tabi | 8432395 | 2007-12-18 15:42:53 +0100 | [diff] [blame] | 343 | if (cs4270_mode_ratios[i].ratio == ratio) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | |
Timur Tabi | 9dbd627 | 2007-08-01 12:22:07 +0200 | [diff] [blame] | 347 | if (i == NUM_MCLK_RATIOS) { |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 348 | /* We did not find a matching ratio */ |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 349 | dev_err(codec->dev, "could not find matching ratio\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 350 | return -EINVAL; |
| 351 | } |
| 352 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 353 | /* Set the sample rate */ |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 354 | |
| 355 | reg = snd_soc_read(codec, CS4270_MODE); |
| 356 | reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); |
Daniel Mack | 4eae080 | 2009-02-25 14:37:21 +0100 | [diff] [blame] | 357 | reg |= cs4270_mode_ratios[i].mclk; |
| 358 | |
| 359 | if (cs4270->slave_mode) |
| 360 | reg |= CS4270_MODE_SLAVE; |
| 361 | else |
| 362 | reg |= cs4270_mode_ratios[i].speed_mode; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 363 | |
| 364 | ret = snd_soc_write(codec, CS4270_MODE, reg); |
| 365 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 366 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 367 | return ret; |
| 368 | } |
| 369 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 370 | /* Set the DAI format */ |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 371 | |
| 372 | reg = snd_soc_read(codec, CS4270_FORMAT); |
| 373 | reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); |
| 374 | |
| 375 | switch (cs4270->mode) { |
| 376 | case SND_SOC_DAIFMT_I2S: |
| 377 | reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S; |
| 378 | break; |
| 379 | case SND_SOC_DAIFMT_LEFT_J: |
| 380 | reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; |
| 381 | break; |
| 382 | default: |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 383 | dev_err(codec->dev, "unknown dai format\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | } |
| 386 | |
| 387 | ret = snd_soc_write(codec, CS4270_FORMAT, reg); |
| 388 | if (ret < 0) { |
Timur Tabi | a6c255e | 2009-02-02 15:08:29 -0600 | [diff] [blame] | 389 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 390 | return ret; |
| 391 | } |
| 392 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 396 | /** |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 397 | * cs4270_dai_mute - enable/disable the CS4270 external mute |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 398 | * @dai: the SOC DAI |
| 399 | * @mute: 0 = disable mute, 1 = enable mute |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 400 | * |
| 401 | * This function toggles the mute bits in the MUTE register. The CS4270's |
| 402 | * mute capability is intended for external muting circuitry, so if the |
| 403 | * board does not have the MUTEA or MUTEB pins connected to such circuitry, |
| 404 | * then this function will do nothing. |
| 405 | */ |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 406 | static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 407 | { |
| 408 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 409 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 410 | int reg6; |
| 411 | |
| 412 | reg6 = snd_soc_read(codec, CS4270_MUTE); |
| 413 | |
| 414 | if (mute) |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 415 | reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B; |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 416 | else { |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 417 | reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B); |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 418 | reg6 |= cs4270->manual_mute; |
| 419 | } |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 420 | |
| 421 | return snd_soc_write(codec, CS4270_MUTE, reg6); |
| 422 | } |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 423 | |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 424 | /** |
| 425 | * cs4270_soc_put_mute - put callback for the 'Master Playback switch' |
| 426 | * alsa control. |
| 427 | * @kcontrol: mixer control |
| 428 | * @ucontrol: control element information |
| 429 | * |
| 430 | * This function basically passes the arguments on to the generic |
| 431 | * snd_soc_put_volsw() function and saves the mute information in |
| 432 | * our private data structure. This is because we want to prevent |
| 433 | * cs4270_dai_mute() neglecting the user's decision to manually |
| 434 | * mute the codec's output. |
| 435 | * |
| 436 | * Returns 0 for success. |
| 437 | */ |
| 438 | static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol, |
| 439 | struct snd_ctl_elem_value *ucontrol) |
| 440 | { |
| 441 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 442 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 443 | int left = !ucontrol->value.integer.value[0]; |
| 444 | int right = !ucontrol->value.integer.value[1]; |
| 445 | |
| 446 | cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) | |
| 447 | (right ? CS4270_MUTE_DAC_B : 0); |
| 448 | |
| 449 | return snd_soc_put_volsw(kcontrol, ucontrol); |
| 450 | } |
| 451 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 452 | /* A list of non-DAPM controls that the CS4270 supports */ |
| 453 | static const struct snd_kcontrol_new cs4270_snd_controls[] = { |
| 454 | SOC_DOUBLE_R("Master Playback Volume", |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 455 | CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1), |
| 456 | SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0), |
| 457 | SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0), |
| 458 | SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0), |
Daniel Mack | 7e1aa1d | 2009-10-29 02:24:32 +0100 | [diff] [blame] | 459 | SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0), |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 460 | SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1), |
| 461 | SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0), |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 462 | SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1), |
| 463 | SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1, |
| 464 | snd_soc_get_volsw, cs4270_soc_put_mute), |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 465 | }; |
| 466 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 467 | static const struct snd_soc_dai_ops cs4270_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 468 | .hw_params = cs4270_hw_params, |
| 469 | .set_sysclk = cs4270_set_dai_sysclk, |
| 470 | .set_fmt = cs4270_set_dai_fmt, |
Daniel Mack | 1a4ba05 | 2009-04-24 16:37:45 +0200 | [diff] [blame] | 471 | .digital_mute = cs4270_dai_mute, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 472 | }; |
| 473 | |
Mark Brown | 5c75848 | 2010-10-06 16:18:17 -0700 | [diff] [blame] | 474 | static struct snd_soc_dai_driver cs4270_dai = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 475 | .name = "cs4270-hifi", |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 476 | .playback = { |
| 477 | .stream_name = "Playback", |
Fabio Estevam | f76fe05 | 2012-09-18 13:03:54 -0300 | [diff] [blame] | 478 | .channels_min = 2, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 479 | .channels_max = 2, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 480 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 481 | .rate_min = 4000, |
| 482 | .rate_max = 216000, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 483 | .formats = CS4270_FORMATS, |
| 484 | }, |
| 485 | .capture = { |
| 486 | .stream_name = "Capture", |
Fabio Estevam | f76fe05 | 2012-09-18 13:03:54 -0300 | [diff] [blame] | 487 | .channels_min = 2, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 488 | .channels_max = 2, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 489 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 490 | .rate_min = 4000, |
| 491 | .rate_max = 216000, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 492 | .formats = CS4270_FORMATS, |
| 493 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 494 | .ops = &cs4270_dai_ops, |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 495 | }; |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 496 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 497 | /** |
| 498 | * cs4270_probe - ASoC probe function |
| 499 | * @pdev: platform device |
| 500 | * |
| 501 | * This function is called when ASoC has all the pieces it needs to |
| 502 | * instantiate a sound driver. |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 503 | */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 504 | static int cs4270_probe(struct snd_soc_codec *codec) |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 505 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 506 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | b61d6d4 | 2012-09-10 12:53:12 +0800 | [diff] [blame] | 507 | int ret; |
Timur Tabi | 04eb093 | 2009-01-29 14:28:37 -0600 | [diff] [blame] | 508 | |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 509 | /* Disable auto-mute. This feature appears to be buggy. In some |
| 510 | * situations, auto-mute will not deactivate when it should, so we want |
| 511 | * this feature disabled by default. An application (e.g. alsactl) can |
| 512 | * re-enabled it by using the controls. |
| 513 | */ |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 514 | ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0); |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 515 | if (ret < 0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 516 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | /* Disable automatic volume control. The hardware enables, and it |
| 521 | * causes volume change commands to be delayed, sometimes until after |
| 522 | * playback has started. An application (e.g. alsactl) can |
| 523 | * re-enabled it by using the controls. |
| 524 | */ |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 525 | ret = snd_soc_update_bits(codec, CS4270_TRANS, |
| 526 | CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0); |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 527 | if (ret < 0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 528 | dev_err(codec->dev, "i2c write failed\n"); |
Timur Tabi | d5e9ba1 | 2009-02-03 11:09:32 -0600 | [diff] [blame] | 529 | return ret; |
| 530 | } |
| 531 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 532 | ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), |
| 533 | cs4270->supplies); |
Jean Delvare | e3145df | 2008-09-30 11:40:37 +0200 | [diff] [blame] | 534 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 535 | return ret; |
| 536 | } |
| 537 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 538 | /** |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 539 | * cs4270_remove - ASoC remove function |
| 540 | * @pdev: platform device |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 541 | * |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 542 | * This function is the counterpart to cs4270_probe(). |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 543 | */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 544 | static int cs4270_remove(struct snd_soc_codec *codec) |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 545 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 546 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 547 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 548 | regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies); |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 549 | |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 550 | return 0; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 551 | }; |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 552 | |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 553 | #ifdef CONFIG_PM |
| 554 | |
| 555 | /* This suspend/resume implementation can handle both - a simple standby |
| 556 | * where the codec remains powered, and a full suspend, where the voltage |
| 557 | * domain the codec is connected to is teared down and/or any other hardware |
| 558 | * reset condition is asserted. |
| 559 | * |
| 560 | * The codec's own power saving features are enabled in the suspend callback, |
| 561 | * and all registers are written back to the hardware when resuming. |
| 562 | */ |
| 563 | |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 564 | static int cs4270_soc_suspend(struct snd_soc_codec *codec) |
Daniel Mack | 15b5bda | 2009-08-05 20:50:43 +0200 | [diff] [blame] | 565 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 566 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 567 | int reg, ret; |
Daniel Mack | 15b5bda | 2009-08-05 20:50:43 +0200 | [diff] [blame] | 568 | |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 569 | reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL; |
| 570 | if (reg < 0) |
| 571 | return reg; |
| 572 | |
| 573 | ret = snd_soc_write(codec, CS4270_PWRCTL, reg); |
| 574 | if (ret < 0) |
| 575 | return ret; |
| 576 | |
| 577 | regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), |
| 578 | cs4270->supplies); |
| 579 | |
| 580 | return 0; |
Daniel Mack | 15b5bda | 2009-08-05 20:50:43 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 583 | static int cs4270_soc_resume(struct snd_soc_codec *codec) |
Daniel Mack | 15b5bda | 2009-08-05 20:50:43 +0200 | [diff] [blame] | 584 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 585 | struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | ab92d09 | 2012-03-19 16:15:43 +0000 | [diff] [blame] | 586 | int reg, ret; |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 587 | |
Mark Brown | ab92d09 | 2012-03-19 16:15:43 +0000 | [diff] [blame] | 588 | ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies), |
| 589 | cs4270->supplies); |
| 590 | if (ret != 0) |
| 591 | return ret; |
Daniel Mack | ffbfd33 | 2009-11-30 17:56:11 +0100 | [diff] [blame] | 592 | |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 593 | /* In case the device was put to hard reset during sleep, we need to |
| 594 | * wait 500ns here before any I2C communication. */ |
| 595 | ndelay(500); |
| 596 | |
| 597 | /* first restore the entire register cache ... */ |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 598 | regcache_sync(cs4270->regmap); |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 599 | |
| 600 | /* ... then disable the power-down bits */ |
| 601 | reg = snd_soc_read(codec, CS4270_PWRCTL); |
| 602 | reg &= ~CS4270_PWRCTL_PDN_ALL; |
| 603 | |
| 604 | return snd_soc_write(codec, CS4270_PWRCTL, reg); |
| 605 | } |
| 606 | #else |
Daniel Mack | 15b5bda | 2009-08-05 20:50:43 +0200 | [diff] [blame] | 607 | #define cs4270_soc_suspend NULL |
| 608 | #define cs4270_soc_resume NULL |
Daniel Mack | 5e7c034 | 2009-05-06 01:26:01 +0200 | [diff] [blame] | 609 | #endif /* CONFIG_PM */ |
| 610 | |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 611 | /* |
Daniel Mack | b6f7d7c | 2011-05-25 09:53:12 +0200 | [diff] [blame] | 612 | * ASoC codec driver structure |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 613 | */ |
Timur Tabi | 11b8fca | 2011-01-10 13:28:32 -0600 | [diff] [blame] | 614 | static const struct snd_soc_codec_driver soc_codec_device_cs4270 = { |
| 615 | .probe = cs4270_probe, |
| 616 | .remove = cs4270_remove, |
| 617 | .suspend = cs4270_soc_suspend, |
| 618 | .resume = cs4270_soc_resume, |
Mark Brown | 19ace0e | 2012-09-10 12:48:11 +0800 | [diff] [blame] | 619 | |
| 620 | .controls = cs4270_snd_controls, |
| 621 | .num_controls = ARRAY_SIZE(cs4270_snd_controls), |
Mark Brown | 782fbab | 2013-08-11 12:29:07 +0100 | [diff] [blame] | 622 | .dapm_widgets = cs4270_dapm_widgets, |
| 623 | .num_dapm_widgets = ARRAY_SIZE(cs4270_dapm_widgets), |
| 624 | .dapm_routes = cs4270_dapm_routes, |
| 625 | .num_dapm_routes = ARRAY_SIZE(cs4270_dapm_routes), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
Daniel Mack | 85d07e4 | 2012-07-25 15:28:34 +0200 | [diff] [blame] | 628 | /* |
| 629 | * cs4270_of_match - the device tree bindings |
| 630 | */ |
| 631 | static const struct of_device_id cs4270_of_match[] = { |
| 632 | { .compatible = "cirrus,cs4270", }, |
| 633 | { } |
| 634 | }; |
| 635 | MODULE_DEVICE_TABLE(of, cs4270_of_match); |
| 636 | |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 637 | static const struct regmap_config cs4270_regmap = { |
| 638 | .reg_bits = 8, |
| 639 | .val_bits = 8, |
| 640 | .max_register = CS4270_LASTREG, |
| 641 | .reg_defaults = cs4270_reg_defaults, |
| 642 | .num_reg_defaults = ARRAY_SIZE(cs4270_reg_defaults), |
| 643 | .cache_type = REGCACHE_RBTREE, |
| 644 | |
| 645 | .readable_reg = cs4270_reg_is_readable, |
| 646 | .volatile_reg = cs4270_reg_is_volatile, |
| 647 | }; |
| 648 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 649 | /** |
| 650 | * cs4270_i2c_probe - initialize the I2C interface of the CS4270 |
| 651 | * @i2c_client: the I2C client object |
| 652 | * @id: the I2C device ID (ignored) |
| 653 | * |
| 654 | * This function is called whenever the I2C subsystem finds a device that |
| 655 | * matches the device ID given via a prior call to i2c_add_driver(). |
| 656 | */ |
| 657 | static int cs4270_i2c_probe(struct i2c_client *i2c_client, |
| 658 | const struct i2c_device_id *id) |
| 659 | { |
Daniel Mack | 0228619 | 2012-07-25 15:28:35 +0200 | [diff] [blame] | 660 | struct device_node *np = i2c_client->dev.of_node; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 661 | struct cs4270_private *cs4270; |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 662 | unsigned int val; |
Mark Brown | b61d6d4 | 2012-09-10 12:53:12 +0800 | [diff] [blame] | 663 | int ret, i; |
| 664 | |
| 665 | cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private), |
| 666 | GFP_KERNEL); |
| 667 | if (!cs4270) { |
| 668 | dev_err(&i2c_client->dev, "could not allocate codec\n"); |
| 669 | return -ENOMEM; |
| 670 | } |
| 671 | |
| 672 | /* get the power supply regulators */ |
| 673 | for (i = 0; i < ARRAY_SIZE(supply_names); i++) |
| 674 | cs4270->supplies[i].supply = supply_names[i]; |
| 675 | |
| 676 | ret = devm_regulator_bulk_get(&i2c_client->dev, |
| 677 | ARRAY_SIZE(cs4270->supplies), |
| 678 | cs4270->supplies); |
| 679 | if (ret < 0) |
| 680 | return ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 681 | |
Daniel Mack | 0228619 | 2012-07-25 15:28:35 +0200 | [diff] [blame] | 682 | /* See if we have a way to bring the codec out of reset */ |
| 683 | if (np) { |
| 684 | enum of_gpio_flags flags; |
| 685 | int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags); |
| 686 | |
| 687 | if (gpio_is_valid(gpio)) { |
| 688 | ret = devm_gpio_request_one(&i2c_client->dev, gpio, |
| 689 | flags & OF_GPIO_ACTIVE_LOW ? |
| 690 | GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, |
| 691 | "cs4270 reset"); |
| 692 | if (ret < 0) |
| 693 | return ret; |
| 694 | } |
| 695 | } |
| 696 | |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 697 | cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap); |
| 698 | if (IS_ERR(cs4270->regmap)) |
| 699 | return PTR_ERR(cs4270->regmap); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 700 | |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 701 | /* Verify that we have a CS4270 */ |
| 702 | ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 703 | if (ret < 0) { |
| 704 | dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n", |
| 705 | i2c_client->addr); |
| 706 | return ret; |
| 707 | } |
| 708 | /* The top four bits of the chip ID should be 1100. */ |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 709 | if ((val & 0xF0) != 0xC0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 710 | dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n", |
| 711 | i2c_client->addr); |
| 712 | return -ENODEV; |
| 713 | } |
| 714 | |
| 715 | dev_info(&i2c_client->dev, "found device at i2c address %X\n", |
| 716 | i2c_client->addr); |
Mark Brown | 1ca6517 | 2012-09-10 12:57:03 +0800 | [diff] [blame] | 717 | dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 718 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 719 | i2c_set_clientdata(i2c_client, cs4270); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 720 | |
| 721 | ret = snd_soc_register_codec(&i2c_client->dev, |
| 722 | &soc_codec_device_cs4270, &cs4270_dai, 1); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * cs4270_i2c_remove - remove an I2C device |
| 728 | * @i2c_client: the I2C client object |
| 729 | * |
| 730 | * This function is the counterpart to cs4270_i2c_probe(). |
| 731 | */ |
| 732 | static int cs4270_i2c_remove(struct i2c_client *i2c_client) |
| 733 | { |
| 734 | snd_soc_unregister_codec(&i2c_client->dev); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * cs4270_id - I2C device IDs supported by this driver |
| 740 | */ |
Axel Lin | 79a54ea | 2011-03-04 15:22:03 +0800 | [diff] [blame] | 741 | static const struct i2c_device_id cs4270_id[] = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 742 | {"cs4270", 0}, |
| 743 | {} |
| 744 | }; |
| 745 | MODULE_DEVICE_TABLE(i2c, cs4270_id); |
| 746 | |
| 747 | /* |
Timur Tabi | ff7bf02 | 2009-01-30 11:14:49 -0600 | [diff] [blame] | 748 | * cs4270_i2c_driver - I2C device identification |
| 749 | * |
| 750 | * This structure tells the I2C subsystem how to identify and support a |
| 751 | * given I2C device type. |
| 752 | */ |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 753 | static struct i2c_driver cs4270_i2c_driver = { |
| 754 | .driver = { |
Shawn Guo | 64902b2 | 2012-02-24 22:09:38 +0800 | [diff] [blame] | 755 | .name = "cs4270", |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 756 | .owner = THIS_MODULE, |
Daniel Mack | 85d07e4 | 2012-07-25 15:28:34 +0200 | [diff] [blame] | 757 | .of_match_table = cs4270_of_match, |
Timur Tabi | 0db4d07 | 2009-01-23 16:31:19 -0600 | [diff] [blame] | 758 | }, |
| 759 | .id_table = cs4270_id, |
| 760 | .probe = cs4270_i2c_probe, |
| 761 | .remove = cs4270_i2c_remove, |
| 762 | }; |
| 763 | |
Sachin Kamat | 5e383f5 | 2012-08-06 17:25:41 +0530 | [diff] [blame] | 764 | module_i2c_driver(cs4270_i2c_driver); |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 765 | |
Timur Tabi | b0c813c | 2007-07-31 18:18:44 +0200 | [diff] [blame] | 766 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
| 767 | MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver"); |
| 768 | MODULE_LICENSE("GPL"); |