Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC Texas Instruments TLV320DAC33 codec driver |
| 3 | * |
| 4 | * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com> |
| 5 | * |
| 6 | * Copyright: (C) 2009 Nokia Corporation |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/pm.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/gpio.h> |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 33 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 35 | #include <sound/core.h> |
| 36 | #include <sound/pcm.h> |
| 37 | #include <sound/pcm_params.h> |
| 38 | #include <sound/soc.h> |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 39 | #include <sound/initval.h> |
| 40 | #include <sound/tlv.h> |
| 41 | |
| 42 | #include <sound/tlv320dac33-plat.h> |
| 43 | #include "tlv320dac33.h" |
| 44 | |
| 45 | #define DAC33_BUFFER_SIZE_BYTES 24576 /* bytes, 12288 16 bit words, |
| 46 | * 6144 stereo */ |
| 47 | #define DAC33_BUFFER_SIZE_SAMPLES 6144 |
| 48 | |
| 49 | #define NSAMPLE_MAX 5700 |
| 50 | |
Peter Ujfalusi | 4260393 | 2010-04-23 10:09:59 +0300 | [diff] [blame] | 51 | #define MODE7_LTHR 10 |
| 52 | #define MODE7_UTHR (DAC33_BUFFER_SIZE_SAMPLES - 10) |
| 53 | |
Peter Ujfalusi | 76f47127 | 2010-04-23 10:10:00 +0300 | [diff] [blame] | 54 | #define BURST_BASEFREQ_HZ 49152000 |
| 55 | |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 56 | #define SAMPLES_TO_US(rate, samples) \ |
| 57 | (1000000000 / ((rate * 1000) / samples)) |
| 58 | |
| 59 | #define US_TO_SAMPLES(rate, us) \ |
Peter Ujfalusi | d54e1f4 | 2010-10-29 14:07:25 +0300 | [diff] [blame] | 60 | (rate / (1000000 / (us < 1000000 ? us : 1000000))) |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 61 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 62 | #define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \ |
| 63 | ((samples * 5000) / ((burstrate * 5000) / (burstrate - playrate))) |
| 64 | |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 65 | static void dac33_calculate_times(struct snd_pcm_substream *substream); |
| 66 | static int dac33_prepare_chip(struct snd_pcm_substream *substream); |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 67 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 68 | enum dac33_state { |
| 69 | DAC33_IDLE = 0, |
| 70 | DAC33_PREFILL, |
| 71 | DAC33_PLAYBACK, |
| 72 | DAC33_FLUSH, |
| 73 | }; |
| 74 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 75 | enum dac33_fifo_modes { |
| 76 | DAC33_FIFO_BYPASS = 0, |
| 77 | DAC33_FIFO_MODE1, |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 78 | DAC33_FIFO_MODE7, |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 79 | DAC33_FIFO_LAST_MODE, |
| 80 | }; |
| 81 | |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 82 | #define DAC33_NUM_SUPPLIES 3 |
| 83 | static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = { |
| 84 | "AVDD", |
| 85 | "DVDD", |
| 86 | "IOVDD", |
| 87 | }; |
| 88 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 89 | struct tlv320dac33_priv { |
| 90 | struct mutex mutex; |
| 91 | struct workqueue_struct *dac33_wq; |
| 92 | struct work_struct work; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 93 | struct snd_soc_codec *codec; |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 94 | struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES]; |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 95 | struct snd_pcm_substream *substream; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 96 | int power_gpio; |
| 97 | int chip_power; |
| 98 | int irq; |
| 99 | unsigned int refclk; |
| 100 | |
| 101 | unsigned int alarm_threshold; /* set to be half of LATENCY_TIME_MS */ |
| 102 | unsigned int nsample_min; /* nsample should not be lower than |
| 103 | * this */ |
| 104 | unsigned int nsample_max; /* nsample should not be higher than |
| 105 | * this */ |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 106 | enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 107 | unsigned int nsample; /* burst read amount from host */ |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 108 | int mode1_latency; /* latency caused by the i2c writes in |
| 109 | * us */ |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 110 | int auto_fifo_config; /* Configure the FIFO based on the |
| 111 | * period size */ |
Peter Ujfalusi | 6aceabb | 2010-01-20 09:39:36 +0200 | [diff] [blame] | 112 | u8 burst_bclkdiv; /* BCLK divider value in burst mode */ |
Peter Ujfalusi | 76f47127 | 2010-04-23 10:10:00 +0300 | [diff] [blame] | 113 | unsigned int burst_rate; /* Interface speed in Burst modes */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 114 | |
Peter Ujfalusi | eeb309a | 2010-03-11 16:26:22 +0200 | [diff] [blame] | 115 | int keep_bclk; /* Keep the BCLK continuously running |
| 116 | * in FIFO modes */ |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 117 | spinlock_t lock; |
| 118 | unsigned long long t_stamp1; /* Time stamp for FIFO modes to */ |
| 119 | unsigned long long t_stamp2; /* calculate the FIFO caused delay */ |
| 120 | |
| 121 | unsigned int mode1_us_burst; /* Time to burst read n number of |
| 122 | * samples */ |
| 123 | unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 124 | |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 125 | unsigned int uthr; |
| 126 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 127 | enum dac33_state state; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 128 | enum snd_soc_control_type control_type; |
| 129 | void *control_data; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static const u8 dac33_reg[DAC33_CACHEREGNUM] = { |
| 133 | 0x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */ |
| 134 | 0x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */ |
| 135 | 0x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */ |
| 136 | 0x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */ |
| 137 | 0x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */ |
| 138 | 0x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */ |
| 139 | 0x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */ |
| 140 | 0x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */ |
| 141 | 0x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */ |
| 142 | 0x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */ |
| 143 | 0x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */ |
| 144 | 0x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */ |
| 145 | 0x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */ |
| 146 | 0x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */ |
| 147 | 0x00, 0x00, /* 0x38 - 0x39 */ |
| 148 | /* Registers 0x3a - 0x3f are reserved */ |
| 149 | 0x00, 0x00, /* 0x3a - 0x3b */ |
| 150 | 0x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */ |
| 151 | |
| 152 | 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */ |
| 153 | 0x00, 0x80, /* 0x44 - 0x45 */ |
| 154 | /* Registers 0x46 - 0x47 are reserved */ |
| 155 | 0x80, 0x80, /* 0x46 - 0x47 */ |
| 156 | |
| 157 | 0x80, 0x00, 0x00, /* 0x48 - 0x4a */ |
| 158 | /* Registers 0x4b - 0x7c are reserved */ |
| 159 | 0x00, /* 0x4b */ |
| 160 | 0x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */ |
| 161 | 0x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */ |
| 162 | 0x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */ |
| 163 | 0x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */ |
| 164 | 0x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */ |
| 165 | 0x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */ |
| 166 | 0x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */ |
| 167 | 0x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */ |
| 168 | 0x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */ |
| 169 | 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */ |
| 170 | 0x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */ |
| 171 | 0x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */ |
| 172 | 0x00, /* 0x7c */ |
| 173 | |
| 174 | 0xda, 0x33, 0x03, /* 0x7d - 0x7f */ |
| 175 | }; |
| 176 | |
| 177 | /* Register read and write */ |
| 178 | static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec *codec, |
| 179 | unsigned reg) |
| 180 | { |
| 181 | u8 *cache = codec->reg_cache; |
| 182 | if (reg >= DAC33_CACHEREGNUM) |
| 183 | return 0; |
| 184 | |
| 185 | return cache[reg]; |
| 186 | } |
| 187 | |
| 188 | static inline void dac33_write_reg_cache(struct snd_soc_codec *codec, |
| 189 | u8 reg, u8 value) |
| 190 | { |
| 191 | u8 *cache = codec->reg_cache; |
| 192 | if (reg >= DAC33_CACHEREGNUM) |
| 193 | return; |
| 194 | |
| 195 | cache[reg] = value; |
| 196 | } |
| 197 | |
| 198 | static int dac33_read(struct snd_soc_codec *codec, unsigned int reg, |
| 199 | u8 *value) |
| 200 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 201 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 202 | int val, ret = 0; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 203 | |
| 204 | *value = reg & 0xff; |
| 205 | |
| 206 | /* If powered off, return the cached value */ |
| 207 | if (dac33->chip_power) { |
| 208 | val = i2c_smbus_read_byte_data(codec->control_data, value[0]); |
| 209 | if (val < 0) { |
| 210 | dev_err(codec->dev, "Read failed (%d)\n", val); |
| 211 | value[0] = dac33_read_reg_cache(codec, reg); |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 212 | ret = val; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 213 | } else { |
| 214 | value[0] = val; |
| 215 | dac33_write_reg_cache(codec, reg, val); |
| 216 | } |
| 217 | } else { |
| 218 | value[0] = dac33_read_reg_cache(codec, reg); |
| 219 | } |
| 220 | |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 221 | return ret; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int dac33_write(struct snd_soc_codec *codec, unsigned int reg, |
| 225 | unsigned int value) |
| 226 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 227 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 228 | u8 data[2]; |
| 229 | int ret = 0; |
| 230 | |
| 231 | /* |
| 232 | * data is |
| 233 | * D15..D8 dac33 register offset |
| 234 | * D7...D0 register data |
| 235 | */ |
| 236 | data[0] = reg & 0xff; |
| 237 | data[1] = value & 0xff; |
| 238 | |
| 239 | dac33_write_reg_cache(codec, data[0], data[1]); |
| 240 | if (dac33->chip_power) { |
| 241 | ret = codec->hw_write(codec->control_data, data, 2); |
| 242 | if (ret != 2) |
| 243 | dev_err(codec->dev, "Write failed (%d)\n", ret); |
| 244 | else |
| 245 | ret = 0; |
| 246 | } |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg, |
| 252 | unsigned int value) |
| 253 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 254 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 255 | int ret; |
| 256 | |
| 257 | mutex_lock(&dac33->mutex); |
| 258 | ret = dac33_write(codec, reg, value); |
| 259 | mutex_unlock(&dac33->mutex); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | #define DAC33_I2C_ADDR_AUTOINC 0x80 |
| 265 | static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg, |
| 266 | unsigned int value) |
| 267 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 268 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 269 | u8 data[3]; |
| 270 | int ret = 0; |
| 271 | |
| 272 | /* |
| 273 | * data is |
| 274 | * D23..D16 dac33 register offset |
| 275 | * D15..D8 register data MSB |
| 276 | * D7...D0 register data LSB |
| 277 | */ |
| 278 | data[0] = reg & 0xff; |
| 279 | data[1] = (value >> 8) & 0xff; |
| 280 | data[2] = value & 0xff; |
| 281 | |
| 282 | dac33_write_reg_cache(codec, data[0], data[1]); |
| 283 | dac33_write_reg_cache(codec, data[0] + 1, data[2]); |
| 284 | |
| 285 | if (dac33->chip_power) { |
| 286 | /* We need to set autoincrement mode for 16 bit writes */ |
| 287 | data[0] |= DAC33_I2C_ADDR_AUTOINC; |
| 288 | ret = codec->hw_write(codec->control_data, data, 3); |
| 289 | if (ret != 3) |
| 290 | dev_err(codec->dev, "Write failed (%d)\n", ret); |
| 291 | else |
| 292 | ret = 0; |
| 293 | } |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
Peter Ujfalusi | ef909d6 | 2010-04-30 14:59:33 +0300 | [diff] [blame] | 298 | static void dac33_init_chip(struct snd_soc_codec *codec) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 299 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 300 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 301 | |
Peter Ujfalusi | ef909d6 | 2010-04-30 14:59:33 +0300 | [diff] [blame] | 302 | if (unlikely(!dac33->chip_power)) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 303 | return; |
| 304 | |
Peter Ujfalusi | ef909d6 | 2010-04-30 14:59:33 +0300 | [diff] [blame] | 305 | /* 44-46: DAC Control Registers */ |
| 306 | /* A : DAC sample rate Fsref/1.5 */ |
| 307 | dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(0)); |
| 308 | /* B : DAC src=normal, not muted */ |
| 309 | dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT | |
| 310 | DAC33_DACSRCL_LEFT); |
| 311 | /* C : (defaults) */ |
| 312 | dac33_write(codec, DAC33_DAC_CTRL_C, 0x00); |
| 313 | |
Peter Ujfalusi | ef909d6 | 2010-04-30 14:59:33 +0300 | [diff] [blame] | 314 | /* 73 : volume soft stepping control, |
| 315 | clock source = internal osc (?) */ |
| 316 | dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN); |
| 317 | |
Peter Ujfalusi | ef909d6 | 2010-04-30 14:59:33 +0300 | [diff] [blame] | 318 | /* Restore only selected registers (gains mostly) */ |
| 319 | dac33_write(codec, DAC33_LDAC_DIG_VOL_CTRL, |
| 320 | dac33_read_reg_cache(codec, DAC33_LDAC_DIG_VOL_CTRL)); |
| 321 | dac33_write(codec, DAC33_RDAC_DIG_VOL_CTRL, |
| 322 | dac33_read_reg_cache(codec, DAC33_RDAC_DIG_VOL_CTRL)); |
| 323 | |
| 324 | dac33_write(codec, DAC33_LINEL_TO_LLO_VOL, |
| 325 | dac33_read_reg_cache(codec, DAC33_LINEL_TO_LLO_VOL)); |
| 326 | dac33_write(codec, DAC33_LINER_TO_RLO_VOL, |
| 327 | dac33_read_reg_cache(codec, DAC33_LINER_TO_RLO_VOL)); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 328 | } |
| 329 | |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 330 | static inline int dac33_read_id(struct snd_soc_codec *codec) |
Peter Ujfalusi | 239fe55 | 2010-04-30 14:59:34 +0300 | [diff] [blame] | 331 | { |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 332 | int i, ret = 0; |
Peter Ujfalusi | 239fe55 | 2010-04-30 14:59:34 +0300 | [diff] [blame] | 333 | u8 reg; |
| 334 | |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 335 | for (i = 0; i < 3; i++) { |
| 336 | ret = dac33_read(codec, DAC33_DEVICE_ID_MSB + i, ®); |
| 337 | if (ret < 0) |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | return ret; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static inline void dac33_soft_power(struct snd_soc_codec *codec, int power) |
| 345 | { |
| 346 | u8 reg; |
| 347 | |
| 348 | reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL); |
| 349 | if (power) |
| 350 | reg |= DAC33_PDNALLB; |
| 351 | else |
Peter Ujfalusi | c3746a0 | 2010-03-11 16:26:21 +0200 | [diff] [blame] | 352 | reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB | |
| 353 | DAC33_DACRPDNB | DAC33_DACLPDNB); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 354 | dac33_write(codec, DAC33_PWR_CTRL, reg); |
| 355 | } |
| 356 | |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 357 | static int dac33_hard_power(struct snd_soc_codec *codec, int power) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 358 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 359 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 360 | int ret = 0; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 361 | |
| 362 | mutex_lock(&dac33->mutex); |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 363 | |
| 364 | /* Safety check */ |
| 365 | if (unlikely(power == dac33->chip_power)) { |
Felipe Balbi | 7fd1d74 | 2010-05-17 14:21:45 +0300 | [diff] [blame] | 366 | dev_dbg(codec->dev, "Trying to set the same power state: %s\n", |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 367 | power ? "ON" : "OFF"); |
| 368 | goto exit; |
| 369 | } |
| 370 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 371 | if (power) { |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 372 | ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies), |
| 373 | dac33->supplies); |
| 374 | if (ret != 0) { |
| 375 | dev_err(codec->dev, |
| 376 | "Failed to enable supplies: %d\n", ret); |
| 377 | goto exit; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 378 | } |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 379 | |
| 380 | if (dac33->power_gpio >= 0) |
| 381 | gpio_set_value(dac33->power_gpio, 1); |
| 382 | |
| 383 | dac33->chip_power = 1; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 384 | } else { |
| 385 | dac33_soft_power(codec, 0); |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 386 | if (dac33->power_gpio >= 0) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 387 | gpio_set_value(dac33->power_gpio, 0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 388 | |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 389 | ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), |
| 390 | dac33->supplies); |
| 391 | if (ret != 0) { |
| 392 | dev_err(codec->dev, |
| 393 | "Failed to disable supplies: %d\n", ret); |
| 394 | goto exit; |
| 395 | } |
| 396 | |
| 397 | dac33->chip_power = 0; |
| 398 | } |
| 399 | |
| 400 | exit: |
| 401 | mutex_unlock(&dac33->mutex); |
| 402 | return ret; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 403 | } |
| 404 | |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 405 | static int playback_event(struct snd_soc_dapm_widget *w, |
| 406 | struct snd_kcontrol *kcontrol, int event) |
| 407 | { |
| 408 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(w->codec); |
| 409 | |
| 410 | switch (event) { |
| 411 | case SND_SOC_DAPM_PRE_PMU: |
| 412 | if (likely(dac33->substream)) { |
| 413 | dac33_calculate_times(dac33->substream); |
| 414 | dac33_prepare_chip(dac33->substream); |
| 415 | } |
| 416 | break; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 421 | static int dac33_get_nsample(struct snd_kcontrol *kcontrol, |
| 422 | struct snd_ctl_elem_value *ucontrol) |
| 423 | { |
| 424 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 425 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 426 | |
| 427 | ucontrol->value.integer.value[0] = dac33->nsample; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int dac33_set_nsample(struct snd_kcontrol *kcontrol, |
| 433 | struct snd_ctl_elem_value *ucontrol) |
| 434 | { |
| 435 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 436 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 437 | int ret = 0; |
| 438 | |
| 439 | if (dac33->nsample == ucontrol->value.integer.value[0]) |
| 440 | return 0; |
| 441 | |
| 442 | if (ucontrol->value.integer.value[0] < dac33->nsample_min || |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 443 | ucontrol->value.integer.value[0] > dac33->nsample_max) { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 444 | ret = -EINVAL; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 445 | } else { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 446 | dac33->nsample = ucontrol->value.integer.value[0]; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 447 | /* Re calculate the burst time */ |
| 448 | dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate, |
| 449 | dac33->nsample); |
| 450 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 455 | static int dac33_get_uthr(struct snd_kcontrol *kcontrol, |
| 456 | struct snd_ctl_elem_value *ucontrol) |
| 457 | { |
| 458 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 459 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
| 460 | |
| 461 | ucontrol->value.integer.value[0] = dac33->uthr; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int dac33_set_uthr(struct snd_kcontrol *kcontrol, |
| 467 | struct snd_ctl_elem_value *ucontrol) |
| 468 | { |
| 469 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 470 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
| 471 | int ret = 0; |
| 472 | |
| 473 | if (dac33->substream) |
| 474 | return -EBUSY; |
| 475 | |
| 476 | if (dac33->uthr == ucontrol->value.integer.value[0]) |
| 477 | return 0; |
| 478 | |
| 479 | if (ucontrol->value.integer.value[0] < (MODE7_LTHR + 10) || |
| 480 | ucontrol->value.integer.value[0] > MODE7_UTHR) |
| 481 | ret = -EINVAL; |
| 482 | else |
| 483 | dac33->uthr = ucontrol->value.integer.value[0]; |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 488 | static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 489 | struct snd_ctl_elem_value *ucontrol) |
| 490 | { |
| 491 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 492 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 493 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 494 | ucontrol->value.integer.value[0] = dac33->fifo_mode; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 499 | static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 500 | struct snd_ctl_elem_value *ucontrol) |
| 501 | { |
| 502 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 503 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 504 | int ret = 0; |
| 505 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 506 | if (dac33->fifo_mode == ucontrol->value.integer.value[0]) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 507 | return 0; |
| 508 | /* Do not allow changes while stream is running*/ |
| 509 | if (codec->active) |
| 510 | return -EPERM; |
| 511 | |
| 512 | if (ucontrol->value.integer.value[0] < 0 || |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 513 | ucontrol->value.integer.value[0] >= DAC33_FIFO_LAST_MODE) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 514 | ret = -EINVAL; |
| 515 | else |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 516 | dac33->fifo_mode = ucontrol->value.integer.value[0]; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 521 | /* Codec operation modes */ |
| 522 | static const char *dac33_fifo_mode_texts[] = { |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 523 | "Bypass", "Mode 1", "Mode 7" |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | static const struct soc_enum dac33_fifo_mode_enum = |
| 527 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(dac33_fifo_mode_texts), |
| 528 | dac33_fifo_mode_texts); |
| 529 | |
Peter Ujfalusi | cf4bb698 | 2010-10-13 11:56:28 +0300 | [diff] [blame] | 530 | /* L/R Line Output Gain */ |
| 531 | static const char *lr_lineout_gain_texts[] = { |
| 532 | "Line -12dB DAC 0dB", "Line -6dB DAC 6dB", |
| 533 | "Line 0dB DAC 12dB", "Line 6dB DAC 18dB", |
| 534 | }; |
| 535 | |
| 536 | static const struct soc_enum l_lineout_gain_enum = |
| 537 | SOC_ENUM_SINGLE(DAC33_LDAC_PWR_CTRL, 0, |
| 538 | ARRAY_SIZE(lr_lineout_gain_texts), |
| 539 | lr_lineout_gain_texts); |
| 540 | |
| 541 | static const struct soc_enum r_lineout_gain_enum = |
| 542 | SOC_ENUM_SINGLE(DAC33_RDAC_PWR_CTRL, 0, |
| 543 | ARRAY_SIZE(lr_lineout_gain_texts), |
| 544 | lr_lineout_gain_texts); |
| 545 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 546 | /* |
| 547 | * DACL/R digital volume control: |
| 548 | * from 0 dB to -63.5 in 0.5 dB steps |
| 549 | * Need to be inverted later on: |
| 550 | * 0x00 == 0 dB |
| 551 | * 0x7f == -63.5 dB |
| 552 | */ |
| 553 | static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0); |
| 554 | |
| 555 | static const struct snd_kcontrol_new dac33_snd_controls[] = { |
| 556 | SOC_DOUBLE_R_TLV("DAC Digital Playback Volume", |
| 557 | DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, |
| 558 | 0, 0x7f, 1, dac_digivol_tlv), |
| 559 | SOC_DOUBLE_R("DAC Digital Playback Switch", |
| 560 | DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1), |
| 561 | SOC_DOUBLE_R("Line to Line Out Volume", |
| 562 | DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1), |
Peter Ujfalusi | cf4bb698 | 2010-10-13 11:56:28 +0300 | [diff] [blame] | 563 | SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum), |
| 564 | SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum), |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 565 | }; |
| 566 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 567 | static const struct snd_kcontrol_new dac33_mode_snd_controls[] = { |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 568 | SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum, |
| 569 | dac33_get_fifo_mode, dac33_set_fifo_mode), |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 570 | }; |
| 571 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 572 | static const struct snd_kcontrol_new dac33_fifo_snd_controls[] = { |
| 573 | SOC_SINGLE_EXT("nSample", 0, 0, 5900, 0, |
| 574 | dac33_get_nsample, dac33_set_nsample), |
| 575 | SOC_SINGLE_EXT("UTHR", 0, 0, MODE7_UTHR, 0, |
| 576 | dac33_get_uthr, dac33_set_uthr), |
| 577 | }; |
| 578 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 579 | /* Analog bypass */ |
| 580 | static const struct snd_kcontrol_new dac33_dapm_abypassl_control = |
| 581 | SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1); |
| 582 | |
| 583 | static const struct snd_kcontrol_new dac33_dapm_abypassr_control = |
| 584 | SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1); |
| 585 | |
| 586 | static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = { |
| 587 | SND_SOC_DAPM_OUTPUT("LEFT_LO"), |
| 588 | SND_SOC_DAPM_OUTPUT("RIGHT_LO"), |
| 589 | |
| 590 | SND_SOC_DAPM_INPUT("LINEL"), |
| 591 | SND_SOC_DAPM_INPUT("LINER"), |
| 592 | |
Peter Ujfalusi | 76eac39 | 2010-12-08 16:04:33 +0200 | [diff] [blame^] | 593 | SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0), |
| 594 | SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0), |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 595 | |
| 596 | /* Analog bypass */ |
| 597 | SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0, |
| 598 | &dac33_dapm_abypassl_control), |
| 599 | SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0, |
| 600 | &dac33_dapm_abypassr_control), |
| 601 | |
Peter Ujfalusi | 9e87186 | 2010-12-08 16:04:32 +0200 | [diff] [blame] | 602 | SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amplifier", |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 603 | DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0), |
Peter Ujfalusi | 9e87186 | 2010-12-08 16:04:32 +0200 | [diff] [blame] | 604 | SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amplifier", |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 605 | DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0), |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 606 | |
Peter Ujfalusi | 76eac39 | 2010-12-08 16:04:33 +0200 | [diff] [blame^] | 607 | SND_SOC_DAPM_SUPPLY("Left DAC Power", |
| 608 | DAC33_LDAC_PWR_CTRL, 2, 0, NULL, 0), |
| 609 | SND_SOC_DAPM_SUPPLY("Right DAC Power", |
| 610 | DAC33_RDAC_PWR_CTRL, 2, 0, NULL, 0), |
| 611 | |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 612 | SND_SOC_DAPM_PRE("Prepare Playback", playback_event), |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | static const struct snd_soc_dapm_route audio_map[] = { |
| 616 | /* Analog bypass */ |
| 617 | {"Analog Left Bypass", "Switch", "LINEL"}, |
| 618 | {"Analog Right Bypass", "Switch", "LINER"}, |
| 619 | |
Peter Ujfalusi | 9e87186 | 2010-12-08 16:04:32 +0200 | [diff] [blame] | 620 | {"Output Left Amplifier", NULL, "DACL"}, |
| 621 | {"Output Right Amplifier", NULL, "DACR"}, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 622 | |
Peter Ujfalusi | 9e87186 | 2010-12-08 16:04:32 +0200 | [diff] [blame] | 623 | {"Output Left Amplifier", NULL, "Analog Left Bypass"}, |
| 624 | {"Output Right Amplifier", NULL, "Analog Right Bypass"}, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 625 | |
Peter Ujfalusi | 76eac39 | 2010-12-08 16:04:33 +0200 | [diff] [blame^] | 626 | {"Output Left Amplifier", NULL, "Left DAC Power"}, |
| 627 | {"Output Right Amplifier", NULL, "Right DAC Power"}, |
| 628 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 629 | /* output */ |
Peter Ujfalusi | 9e87186 | 2010-12-08 16:04:32 +0200 | [diff] [blame] | 630 | {"LEFT_LO", NULL, "Output Left Amplifier"}, |
| 631 | {"RIGHT_LO", NULL, "Output Right Amplifier"}, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static int dac33_add_widgets(struct snd_soc_codec *codec) |
| 635 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 636 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 637 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 638 | snd_soc_dapm_new_controls(dapm, dac33_dapm_widgets, |
| 639 | ARRAY_SIZE(dac33_dapm_widgets)); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 640 | /* set up audio path interconnects */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 641 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int dac33_set_bias_level(struct snd_soc_codec *codec, |
| 647 | enum snd_soc_bias_level level) |
| 648 | { |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 649 | int ret; |
| 650 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 651 | switch (level) { |
| 652 | case SND_SOC_BIAS_ON: |
Peter Ujfalusi | 3e20234 | 2010-11-30 14:31:46 +0200 | [diff] [blame] | 653 | if (!dac33->substream) |
| 654 | dac33_soft_power(codec, 1); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 655 | break; |
| 656 | case SND_SOC_BIAS_PREPARE: |
| 657 | break; |
| 658 | case SND_SOC_BIAS_STANDBY: |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 659 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 660 | /* Coming from OFF, switch on the codec */ |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 661 | ret = dac33_hard_power(codec, 1); |
| 662 | if (ret != 0) |
| 663 | return ret; |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 664 | |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 665 | dac33_init_chip(codec); |
| 666 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 667 | break; |
| 668 | case SND_SOC_BIAS_OFF: |
Peter Ujfalusi | 2d4cdd6 | 2010-05-17 14:21:46 +0300 | [diff] [blame] | 669 | /* Do not power off, when the codec is already off */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 670 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) |
Peter Ujfalusi | 2d4cdd6 | 2010-05-17 14:21:46 +0300 | [diff] [blame] | 671 | return 0; |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 672 | ret = dac33_hard_power(codec, 0); |
| 673 | if (ret != 0) |
| 674 | return ret; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 675 | break; |
| 676 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 677 | codec->dapm.bias_level = level; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 682 | static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33) |
| 683 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 684 | struct snd_soc_codec *codec = dac33->codec; |
Peter Ujfalusi | 84eae18 | 2010-10-22 15:11:20 +0300 | [diff] [blame] | 685 | unsigned int delay; |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 686 | |
| 687 | switch (dac33->fifo_mode) { |
| 688 | case DAC33_FIFO_MODE1: |
| 689 | dac33_write16(codec, DAC33_NSAMPLE_MSB, |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 690 | DAC33_THRREG(dac33->nsample)); |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 691 | |
| 692 | /* Take the timestamps */ |
| 693 | spin_lock_irq(&dac33->lock); |
| 694 | dac33->t_stamp2 = ktime_to_us(ktime_get()); |
| 695 | dac33->t_stamp1 = dac33->t_stamp2; |
| 696 | spin_unlock_irq(&dac33->lock); |
| 697 | |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 698 | dac33_write16(codec, DAC33_PREFILL_MSB, |
| 699 | DAC33_THRREG(dac33->alarm_threshold)); |
Peter Ujfalusi | f4d5932 | 2010-04-23 10:09:57 +0300 | [diff] [blame] | 700 | /* Enable Alarm Threshold IRQ with a delay */ |
Peter Ujfalusi | 84eae18 | 2010-10-22 15:11:20 +0300 | [diff] [blame] | 701 | delay = SAMPLES_TO_US(dac33->burst_rate, |
| 702 | dac33->alarm_threshold) + 1000; |
| 703 | usleep_range(delay, delay + 500); |
Peter Ujfalusi | f4d5932 | 2010-04-23 10:09:57 +0300 | [diff] [blame] | 704 | dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT); |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 705 | break; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 706 | case DAC33_FIFO_MODE7: |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 707 | /* Take the timestamp */ |
| 708 | spin_lock_irq(&dac33->lock); |
| 709 | dac33->t_stamp1 = ktime_to_us(ktime_get()); |
| 710 | /* Move back the timestamp with drain time */ |
| 711 | dac33->t_stamp1 -= dac33->mode7_us_to_lthr; |
| 712 | spin_unlock_irq(&dac33->lock); |
| 713 | |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 714 | dac33_write16(codec, DAC33_PREFILL_MSB, |
Peter Ujfalusi | 4260393 | 2010-04-23 10:09:59 +0300 | [diff] [blame] | 715 | DAC33_THRREG(MODE7_LTHR)); |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 716 | |
| 717 | /* Enable Upper Threshold IRQ */ |
| 718 | dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MUT); |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 719 | break; |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 720 | default: |
| 721 | dev_warn(codec->dev, "Unhandled FIFO mode: %d\n", |
| 722 | dac33->fifo_mode); |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33) |
| 728 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 729 | struct snd_soc_codec *codec = dac33->codec; |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 730 | |
| 731 | switch (dac33->fifo_mode) { |
| 732 | case DAC33_FIFO_MODE1: |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 733 | /* Take the timestamp */ |
| 734 | spin_lock_irq(&dac33->lock); |
| 735 | dac33->t_stamp2 = ktime_to_us(ktime_get()); |
| 736 | spin_unlock_irq(&dac33->lock); |
| 737 | |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 738 | dac33_write16(codec, DAC33_NSAMPLE_MSB, |
| 739 | DAC33_THRREG(dac33->nsample)); |
| 740 | break; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 741 | case DAC33_FIFO_MODE7: |
| 742 | /* At the moment we are not using interrupts in mode7 */ |
| 743 | break; |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 744 | default: |
| 745 | dev_warn(codec->dev, "Unhandled FIFO mode: %d\n", |
| 746 | dac33->fifo_mode); |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 751 | static void dac33_work(struct work_struct *work) |
| 752 | { |
| 753 | struct snd_soc_codec *codec; |
| 754 | struct tlv320dac33_priv *dac33; |
| 755 | u8 reg; |
| 756 | |
| 757 | dac33 = container_of(work, struct tlv320dac33_priv, work); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 758 | codec = dac33->codec; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 759 | |
| 760 | mutex_lock(&dac33->mutex); |
| 761 | switch (dac33->state) { |
| 762 | case DAC33_PREFILL: |
| 763 | dac33->state = DAC33_PLAYBACK; |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 764 | dac33_prefill_handler(dac33); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 765 | break; |
| 766 | case DAC33_PLAYBACK: |
Peter Ujfalusi | d4f102d | 2009-12-31 10:30:20 +0200 | [diff] [blame] | 767 | dac33_playback_handler(dac33); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 768 | break; |
| 769 | case DAC33_IDLE: |
| 770 | break; |
| 771 | case DAC33_FLUSH: |
| 772 | dac33->state = DAC33_IDLE; |
| 773 | /* Mask all interrupts from dac33 */ |
| 774 | dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0); |
| 775 | |
| 776 | /* flush fifo */ |
| 777 | reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A); |
| 778 | reg |= DAC33_FIFOFLUSH; |
| 779 | dac33_write(codec, DAC33_FIFO_CTRL_A, reg); |
| 780 | break; |
| 781 | } |
| 782 | mutex_unlock(&dac33->mutex); |
| 783 | } |
| 784 | |
| 785 | static irqreturn_t dac33_interrupt_handler(int irq, void *dev) |
| 786 | { |
| 787 | struct snd_soc_codec *codec = dev; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 788 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 789 | |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 790 | spin_lock(&dac33->lock); |
| 791 | dac33->t_stamp1 = ktime_to_us(ktime_get()); |
| 792 | spin_unlock(&dac33->lock); |
| 793 | |
| 794 | /* Do not schedule the workqueue in Mode7 */ |
| 795 | if (dac33->fifo_mode != DAC33_FIFO_MODE7) |
| 796 | queue_work(dac33->dac33_wq, &dac33->work); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 797 | |
| 798 | return IRQ_HANDLED; |
| 799 | } |
| 800 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 801 | static void dac33_oscwait(struct snd_soc_codec *codec) |
| 802 | { |
Peter Ujfalusi | 84eae18 | 2010-10-22 15:11:20 +0300 | [diff] [blame] | 803 | int timeout = 60; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 804 | u8 reg; |
| 805 | |
| 806 | do { |
Peter Ujfalusi | 84eae18 | 2010-10-22 15:11:20 +0300 | [diff] [blame] | 807 | usleep_range(1000, 2000); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 808 | dac33_read(codec, DAC33_INT_OSC_STATUS, ®); |
| 809 | } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--); |
| 810 | if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) |
| 811 | dev_err(codec->dev, |
| 812 | "internal oscillator calibration failed\n"); |
| 813 | } |
| 814 | |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 815 | static int dac33_startup(struct snd_pcm_substream *substream, |
| 816 | struct snd_soc_dai *dai) |
| 817 | { |
| 818 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 819 | struct snd_soc_codec *codec = rtd->codec; |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 820 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
| 821 | |
| 822 | /* Stream started, save the substream pointer */ |
| 823 | dac33->substream = substream; |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static void dac33_shutdown(struct snd_pcm_substream *substream, |
| 829 | struct snd_soc_dai *dai) |
| 830 | { |
| 831 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 832 | struct snd_soc_codec *codec = rtd->codec; |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 833 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
| 834 | |
| 835 | dac33->substream = NULL; |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 836 | |
| 837 | /* Reset the nSample restrictions */ |
| 838 | dac33->nsample_min = 0; |
| 839 | dac33->nsample_max = NSAMPLE_MAX; |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 840 | } |
| 841 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 842 | static int dac33_hw_params(struct snd_pcm_substream *substream, |
| 843 | struct snd_pcm_hw_params *params, |
| 844 | struct snd_soc_dai *dai) |
| 845 | { |
| 846 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 847 | struct snd_soc_codec *codec = rtd->codec; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 848 | |
| 849 | /* Check parameters for validity */ |
| 850 | switch (params_rate(params)) { |
| 851 | case 44100: |
| 852 | case 48000: |
| 853 | break; |
| 854 | default: |
| 855 | dev_err(codec->dev, "unsupported rate %d\n", |
| 856 | params_rate(params)); |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | |
| 860 | switch (params_format(params)) { |
| 861 | case SNDRV_PCM_FORMAT_S16_LE: |
| 862 | break; |
| 863 | default: |
| 864 | dev_err(codec->dev, "unsupported format %d\n", |
| 865 | params_format(params)); |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | #define CALC_OSCSET(rate, refclk) ( \ |
Peter Ujfalusi | 7833ae0 | 2010-02-16 13:23:16 +0200 | [diff] [blame] | 873 | ((((rate * 10000) / refclk) * 4096) + 7000) / 10000) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 874 | #define CALC_RATIOSET(rate, refclk) ( \ |
| 875 | ((((refclk * 100000) / rate) * 16384) + 50000) / 100000) |
| 876 | |
| 877 | /* |
| 878 | * tlv320dac33 is strict on the sequence of the register writes, if the register |
| 879 | * writes happens in different order, than dac33 might end up in unknown state. |
| 880 | * Use the known, working sequence of register writes to initialize the dac33. |
| 881 | */ |
| 882 | static int dac33_prepare_chip(struct snd_pcm_substream *substream) |
| 883 | { |
| 884 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 885 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 886 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 887 | unsigned int oscset, ratioset, pwr_ctrl, reg_tmp; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 888 | u8 aictrl_a, aictrl_b, fifoctrl_a; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 889 | |
| 890 | switch (substream->runtime->rate) { |
| 891 | case 44100: |
| 892 | case 48000: |
| 893 | oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk); |
| 894 | ratioset = CALC_RATIOSET(substream->runtime->rate, |
| 895 | dac33->refclk); |
| 896 | break; |
| 897 | default: |
| 898 | dev_err(codec->dev, "unsupported rate %d\n", |
| 899 | substream->runtime->rate); |
| 900 | return -EINVAL; |
| 901 | } |
| 902 | |
| 903 | |
| 904 | aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A); |
| 905 | aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK); |
Peter Ujfalusi | e5e878c | 2010-02-16 13:23:15 +0200 | [diff] [blame] | 906 | /* Read FIFO control A, and clear FIFO flush bit */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 907 | fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A); |
Peter Ujfalusi | e5e878c | 2010-02-16 13:23:15 +0200 | [diff] [blame] | 908 | fifoctrl_a &= ~DAC33_FIFOFLUSH; |
| 909 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 910 | fifoctrl_a &= ~DAC33_WIDTH; |
| 911 | switch (substream->runtime->format) { |
| 912 | case SNDRV_PCM_FORMAT_S16_LE: |
| 913 | aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16); |
| 914 | fifoctrl_a |= DAC33_WIDTH; |
| 915 | break; |
| 916 | default: |
| 917 | dev_err(codec->dev, "unsupported format %d\n", |
| 918 | substream->runtime->format); |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | mutex_lock(&dac33->mutex); |
Peter Ujfalusi | ad05c03 | 2010-04-30 14:59:36 +0300 | [diff] [blame] | 923 | |
| 924 | if (!dac33->chip_power) { |
| 925 | /* |
| 926 | * Chip is not powered yet. |
| 927 | * Do the init in the dac33_set_bias_level later. |
| 928 | */ |
| 929 | mutex_unlock(&dac33->mutex); |
| 930 | return 0; |
| 931 | } |
| 932 | |
Peter Ujfalusi | c3746a0 | 2010-03-11 16:26:21 +0200 | [diff] [blame] | 933 | dac33_soft_power(codec, 0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 934 | dac33_soft_power(codec, 1); |
| 935 | |
| 936 | reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL); |
| 937 | dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp); |
| 938 | |
| 939 | /* Write registers 0x08 and 0x09 (MSB, LSB) */ |
| 940 | dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset); |
| 941 | |
| 942 | /* calib time: 128 is a nice number ;) */ |
| 943 | dac33_write(codec, DAC33_CALIB_TIME, 128); |
| 944 | |
| 945 | /* adjustment treshold & step */ |
| 946 | dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) | |
| 947 | DAC33_ADJSTEP(1)); |
| 948 | |
| 949 | /* div=4 / gain=1 / div */ |
| 950 | dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4)); |
| 951 | |
| 952 | pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL); |
| 953 | pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB; |
| 954 | dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl); |
| 955 | |
| 956 | dac33_oscwait(codec); |
| 957 | |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 958 | if (dac33->fifo_mode) { |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 959 | /* Generic for all FIFO modes */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 960 | /* 50-51 : ASRC Control registers */ |
Peter Ujfalusi | fdb6b1e | 2010-03-19 11:10:20 +0200 | [diff] [blame] | 961 | dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1)); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 962 | dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */ |
| 963 | |
| 964 | /* Write registers 0x34 and 0x35 (MSB, LSB) */ |
| 965 | dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset); |
| 966 | |
| 967 | /* Set interrupts to high active */ |
| 968 | dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 969 | } else { |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 970 | /* FIFO bypass mode */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 971 | /* 50-51 : ASRC Control registers */ |
| 972 | dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP); |
| 973 | dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */ |
| 974 | } |
| 975 | |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 976 | /* Interrupt behaviour configuration */ |
| 977 | switch (dac33->fifo_mode) { |
| 978 | case DAC33_FIFO_MODE1: |
| 979 | dac33_write(codec, DAC33_FIFO_IRQ_MODE_B, |
| 980 | DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL)); |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 981 | break; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 982 | case DAC33_FIFO_MODE7: |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 983 | dac33_write(codec, DAC33_FIFO_IRQ_MODE_A, |
| 984 | DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL)); |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 985 | break; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 986 | default: |
| 987 | /* in FIFO bypass mode, the interrupts are not used */ |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B); |
| 992 | |
| 993 | switch (dac33->fifo_mode) { |
| 994 | case DAC33_FIFO_MODE1: |
| 995 | /* |
| 996 | * For mode1: |
| 997 | * Disable the FIFO bypass (Enable the use of FIFO) |
| 998 | * Select nSample mode |
| 999 | * BCLK is only running when data is needed by DAC33 |
| 1000 | */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1001 | fifoctrl_a &= ~DAC33_FBYPAS; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1002 | fifoctrl_a &= ~DAC33_FAUTO; |
Peter Ujfalusi | eeb309a | 2010-03-11 16:26:22 +0200 | [diff] [blame] | 1003 | if (dac33->keep_bclk) |
| 1004 | aictrl_b |= DAC33_BCLKON; |
| 1005 | else |
| 1006 | aictrl_b &= ~DAC33_BCLKON; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1007 | break; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 1008 | case DAC33_FIFO_MODE7: |
| 1009 | /* |
| 1010 | * For mode1: |
| 1011 | * Disable the FIFO bypass (Enable the use of FIFO) |
| 1012 | * Select Threshold mode |
| 1013 | * BCLK is only running when data is needed by DAC33 |
| 1014 | */ |
| 1015 | fifoctrl_a &= ~DAC33_FBYPAS; |
| 1016 | fifoctrl_a |= DAC33_FAUTO; |
Peter Ujfalusi | eeb309a | 2010-03-11 16:26:22 +0200 | [diff] [blame] | 1017 | if (dac33->keep_bclk) |
| 1018 | aictrl_b |= DAC33_BCLKON; |
| 1019 | else |
| 1020 | aictrl_b &= ~DAC33_BCLKON; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 1021 | break; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1022 | default: |
| 1023 | /* |
| 1024 | * For FIFO bypass mode: |
| 1025 | * Enable the FIFO bypass (Disable the FIFO use) |
| 1026 | * Set the BCLK as continous |
| 1027 | */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1028 | fifoctrl_a |= DAC33_FBYPAS; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1029 | aictrl_b |= DAC33_BCLKON; |
| 1030 | break; |
| 1031 | } |
| 1032 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1033 | dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1034 | dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a); |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1035 | dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1036 | |
Peter Ujfalusi | 6aceabb | 2010-01-20 09:39:36 +0200 | [diff] [blame] | 1037 | /* |
| 1038 | * BCLK divide ratio |
| 1039 | * 0: 1.5 |
| 1040 | * 1: 1 |
| 1041 | * 2: 2 |
| 1042 | * ... |
| 1043 | * 254: 254 |
| 1044 | * 255: 255 |
| 1045 | */ |
Peter Ujfalusi | 6cd6ced | 2010-01-20 09:39:35 +0200 | [diff] [blame] | 1046 | if (dac33->fifo_mode) |
Peter Ujfalusi | 6aceabb | 2010-01-20 09:39:36 +0200 | [diff] [blame] | 1047 | dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, |
| 1048 | dac33->burst_bclkdiv); |
Peter Ujfalusi | 6cd6ced | 2010-01-20 09:39:35 +0200 | [diff] [blame] | 1049 | else |
| 1050 | dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32); |
| 1051 | |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1052 | switch (dac33->fifo_mode) { |
| 1053 | case DAC33_FIFO_MODE1: |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1054 | dac33_write16(codec, DAC33_ATHR_MSB, |
| 1055 | DAC33_THRREG(dac33->alarm_threshold)); |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1056 | break; |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 1057 | case DAC33_FIFO_MODE7: |
| 1058 | /* |
| 1059 | * Configure the threshold levels, and leave 10 sample space |
| 1060 | * at the bottom, and also at the top of the FIFO |
| 1061 | */ |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1062 | dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr)); |
Peter Ujfalusi | 4260393 | 2010-04-23 10:09:59 +0300 | [diff] [blame] | 1063 | dac33_write16(codec, DAC33_LTHR_MSB, DAC33_THRREG(MODE7_LTHR)); |
Peter Ujfalusi | 28e05d9 | 2009-12-31 10:30:22 +0200 | [diff] [blame] | 1064 | break; |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1065 | default: |
Peter Ujfalusi | aec242d | 2009-12-31 10:30:21 +0200 | [diff] [blame] | 1066 | break; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | mutex_unlock(&dac33->mutex); |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static void dac33_calculate_times(struct snd_pcm_substream *substream) |
| 1075 | { |
| 1076 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1077 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 1078 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 1079 | unsigned int period_size = substream->runtime->period_size; |
| 1080 | unsigned int rate = substream->runtime->rate; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1081 | unsigned int nsample_limit; |
| 1082 | |
Peter Ujfalusi | 55abb59 | 2010-04-23 10:09:58 +0300 | [diff] [blame] | 1083 | /* In bypass mode we don't need to calculate */ |
| 1084 | if (!dac33->fifo_mode) |
| 1085 | return; |
| 1086 | |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1087 | switch (dac33->fifo_mode) { |
| 1088 | case DAC33_FIFO_MODE1: |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 1089 | /* Number of samples under i2c latency */ |
| 1090 | dac33->alarm_threshold = US_TO_SAMPLES(rate, |
| 1091 | dac33->mode1_latency); |
Peter Ujfalusi | 1bc13b2 | 2010-10-29 09:49:37 +0300 | [diff] [blame] | 1092 | nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - |
| 1093 | dac33->alarm_threshold; |
| 1094 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1095 | if (dac33->auto_fifo_config) { |
| 1096 | if (period_size <= dac33->alarm_threshold) |
| 1097 | /* |
| 1098 | * Configure nSamaple to number of periods, |
| 1099 | * which covers the latency requironment. |
| 1100 | */ |
| 1101 | dac33->nsample = period_size * |
| 1102 | ((dac33->alarm_threshold / period_size) + |
| 1103 | (dac33->alarm_threshold % period_size ? |
| 1104 | 1 : 0)); |
Peter Ujfalusi | 1bc13b2 | 2010-10-29 09:49:37 +0300 | [diff] [blame] | 1105 | else if (period_size > nsample_limit) |
| 1106 | dac33->nsample = nsample_limit; |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1107 | else |
| 1108 | dac33->nsample = period_size; |
| 1109 | } else { |
| 1110 | /* nSample time shall not be shorter than i2c latency */ |
| 1111 | dac33->nsample_min = dac33->alarm_threshold; |
| 1112 | /* |
| 1113 | * nSample should not be bigger than alsa buffer minus |
| 1114 | * size of one period to avoid overruns |
| 1115 | */ |
| 1116 | dac33->nsample_max = substream->runtime->buffer_size - |
| 1117 | period_size; |
Peter Ujfalusi | 1bc13b2 | 2010-10-29 09:49:37 +0300 | [diff] [blame] | 1118 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1119 | if (dac33->nsample_max > nsample_limit) |
| 1120 | dac33->nsample_max = nsample_limit; |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 1121 | |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1122 | /* Correct the nSample if it is outside of the ranges */ |
| 1123 | if (dac33->nsample < dac33->nsample_min) |
| 1124 | dac33->nsample = dac33->nsample_min; |
| 1125 | if (dac33->nsample > dac33->nsample_max) |
| 1126 | dac33->nsample = dac33->nsample_max; |
| 1127 | } |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 1128 | |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1129 | dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate, |
| 1130 | dac33->nsample); |
| 1131 | dac33->t_stamp1 = 0; |
| 1132 | dac33->t_stamp2 = 0; |
| 1133 | break; |
| 1134 | case DAC33_FIFO_MODE7: |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1135 | if (dac33->auto_fifo_config) { |
| 1136 | dac33->uthr = UTHR_FROM_PERIOD_SIZE( |
| 1137 | period_size, |
| 1138 | rate, |
| 1139 | dac33->burst_rate) + 9; |
| 1140 | if (dac33->uthr > MODE7_UTHR) |
| 1141 | dac33->uthr = MODE7_UTHR; |
| 1142 | if (dac33->uthr < (MODE7_LTHR + 10)) |
| 1143 | dac33->uthr = (MODE7_LTHR + 10); |
| 1144 | } |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1145 | dac33->mode7_us_to_lthr = |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1146 | SAMPLES_TO_US(substream->runtime->rate, |
| 1147 | dac33->uthr - MODE7_LTHR + 1); |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1148 | dac33->t_stamp1 = 0; |
| 1149 | break; |
| 1150 | default: |
| 1151 | break; |
| 1152 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1153 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd, |
| 1157 | struct snd_soc_dai *dai) |
| 1158 | { |
| 1159 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1160 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 1161 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1162 | int ret = 0; |
| 1163 | |
| 1164 | switch (cmd) { |
| 1165 | case SNDRV_PCM_TRIGGER_START: |
| 1166 | case SNDRV_PCM_TRIGGER_RESUME: |
| 1167 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 1168 | if (dac33->fifo_mode) { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1169 | dac33->state = DAC33_PREFILL; |
| 1170 | queue_work(dac33->dac33_wq, &dac33->work); |
| 1171 | } |
| 1172 | break; |
| 1173 | case SNDRV_PCM_TRIGGER_STOP: |
| 1174 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1175 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 1176 | if (dac33->fifo_mode) { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1177 | dac33->state = DAC33_FLUSH; |
| 1178 | queue_work(dac33->dac33_wq, &dac33->work); |
| 1179 | } |
| 1180 | break; |
| 1181 | default: |
| 1182 | ret = -EINVAL; |
| 1183 | } |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1188 | static snd_pcm_sframes_t dac33_dai_delay( |
| 1189 | struct snd_pcm_substream *substream, |
| 1190 | struct snd_soc_dai *dai) |
| 1191 | { |
| 1192 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1193 | struct snd_soc_codec *codec = rtd->codec; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1194 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
| 1195 | unsigned long long t0, t1, t_now; |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1196 | unsigned int time_delta, uthr; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1197 | int samples_out, samples_in, samples; |
| 1198 | snd_pcm_sframes_t delay = 0; |
| 1199 | |
| 1200 | switch (dac33->fifo_mode) { |
| 1201 | case DAC33_FIFO_BYPASS: |
| 1202 | break; |
| 1203 | case DAC33_FIFO_MODE1: |
| 1204 | spin_lock(&dac33->lock); |
| 1205 | t0 = dac33->t_stamp1; |
| 1206 | t1 = dac33->t_stamp2; |
| 1207 | spin_unlock(&dac33->lock); |
| 1208 | t_now = ktime_to_us(ktime_get()); |
| 1209 | |
| 1210 | /* We have not started to fill the FIFO yet, delay is 0 */ |
| 1211 | if (!t1) |
| 1212 | goto out; |
| 1213 | |
| 1214 | if (t0 > t1) { |
| 1215 | /* |
| 1216 | * Phase 1: |
| 1217 | * After Alarm threshold, and before nSample write |
| 1218 | */ |
| 1219 | time_delta = t_now - t0; |
| 1220 | samples_out = time_delta ? US_TO_SAMPLES( |
| 1221 | substream->runtime->rate, |
| 1222 | time_delta) : 0; |
| 1223 | |
| 1224 | if (likely(dac33->alarm_threshold > samples_out)) |
| 1225 | delay = dac33->alarm_threshold - samples_out; |
| 1226 | else |
| 1227 | delay = 0; |
| 1228 | } else if ((t_now - t1) <= dac33->mode1_us_burst) { |
| 1229 | /* |
| 1230 | * Phase 2: |
| 1231 | * After nSample write (during burst operation) |
| 1232 | */ |
| 1233 | time_delta = t_now - t0; |
| 1234 | samples_out = time_delta ? US_TO_SAMPLES( |
| 1235 | substream->runtime->rate, |
| 1236 | time_delta) : 0; |
| 1237 | |
| 1238 | time_delta = t_now - t1; |
| 1239 | samples_in = time_delta ? US_TO_SAMPLES( |
| 1240 | dac33->burst_rate, |
| 1241 | time_delta) : 0; |
| 1242 | |
| 1243 | samples = dac33->alarm_threshold; |
| 1244 | samples += (samples_in - samples_out); |
| 1245 | |
| 1246 | if (likely(samples > 0)) |
| 1247 | delay = samples; |
| 1248 | else |
| 1249 | delay = 0; |
| 1250 | } else { |
| 1251 | /* |
| 1252 | * Phase 3: |
| 1253 | * After burst operation, before next alarm threshold |
| 1254 | */ |
| 1255 | time_delta = t_now - t0; |
| 1256 | samples_out = time_delta ? US_TO_SAMPLES( |
| 1257 | substream->runtime->rate, |
| 1258 | time_delta) : 0; |
| 1259 | |
| 1260 | samples_in = dac33->nsample; |
| 1261 | samples = dac33->alarm_threshold; |
| 1262 | samples += (samples_in - samples_out); |
| 1263 | |
| 1264 | if (likely(samples > 0)) |
| 1265 | delay = samples > DAC33_BUFFER_SIZE_SAMPLES ? |
| 1266 | DAC33_BUFFER_SIZE_SAMPLES : samples; |
| 1267 | else |
| 1268 | delay = 0; |
| 1269 | } |
| 1270 | break; |
| 1271 | case DAC33_FIFO_MODE7: |
| 1272 | spin_lock(&dac33->lock); |
| 1273 | t0 = dac33->t_stamp1; |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1274 | uthr = dac33->uthr; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1275 | spin_unlock(&dac33->lock); |
| 1276 | t_now = ktime_to_us(ktime_get()); |
| 1277 | |
| 1278 | /* We have not started to fill the FIFO yet, delay is 0 */ |
| 1279 | if (!t0) |
| 1280 | goto out; |
| 1281 | |
| 1282 | if (t_now <= t0) { |
| 1283 | /* |
| 1284 | * Either the timestamps are messed or equal. Report |
| 1285 | * maximum delay |
| 1286 | */ |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1287 | delay = uthr; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1288 | goto out; |
| 1289 | } |
| 1290 | |
| 1291 | time_delta = t_now - t0; |
| 1292 | if (time_delta <= dac33->mode7_us_to_lthr) { |
| 1293 | /* |
| 1294 | * Phase 1: |
| 1295 | * After burst (draining phase) |
| 1296 | */ |
| 1297 | samples_out = US_TO_SAMPLES( |
| 1298 | substream->runtime->rate, |
| 1299 | time_delta); |
| 1300 | |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1301 | if (likely(uthr > samples_out)) |
| 1302 | delay = uthr - samples_out; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1303 | else |
| 1304 | delay = 0; |
| 1305 | } else { |
| 1306 | /* |
| 1307 | * Phase 2: |
| 1308 | * During burst operation |
| 1309 | */ |
| 1310 | time_delta = time_delta - dac33->mode7_us_to_lthr; |
| 1311 | |
| 1312 | samples_out = US_TO_SAMPLES( |
| 1313 | substream->runtime->rate, |
| 1314 | time_delta); |
| 1315 | samples_in = US_TO_SAMPLES( |
| 1316 | dac33->burst_rate, |
| 1317 | time_delta); |
| 1318 | delay = MODE7_LTHR + samples_in - samples_out; |
| 1319 | |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1320 | if (unlikely(delay > uthr)) |
| 1321 | delay = uthr; |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1322 | } |
| 1323 | break; |
| 1324 | default: |
| 1325 | dev_warn(codec->dev, "Unhandled FIFO mode: %d\n", |
| 1326 | dac33->fifo_mode); |
| 1327 | break; |
| 1328 | } |
| 1329 | out: |
| 1330 | return delay; |
| 1331 | } |
| 1332 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1333 | static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 1334 | int clk_id, unsigned int freq, int dir) |
| 1335 | { |
| 1336 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 1337 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1338 | u8 ioc_reg, asrcb_reg; |
| 1339 | |
| 1340 | ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL); |
| 1341 | asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B); |
| 1342 | switch (clk_id) { |
| 1343 | case TLV320DAC33_MCLK: |
| 1344 | ioc_reg |= DAC33_REFSEL; |
| 1345 | asrcb_reg |= DAC33_SRCREFSEL; |
| 1346 | break; |
| 1347 | case TLV320DAC33_SLEEPCLK: |
| 1348 | ioc_reg &= ~DAC33_REFSEL; |
| 1349 | asrcb_reg &= ~DAC33_SRCREFSEL; |
| 1350 | break; |
| 1351 | default: |
| 1352 | dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id); |
| 1353 | break; |
| 1354 | } |
| 1355 | dac33->refclk = freq; |
| 1356 | |
| 1357 | dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg); |
| 1358 | dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg); |
| 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 1364 | unsigned int fmt) |
| 1365 | { |
| 1366 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 1367 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1368 | u8 aictrl_a, aictrl_b; |
| 1369 | |
| 1370 | aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A); |
| 1371 | aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B); |
| 1372 | /* set master/slave audio interface */ |
| 1373 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 1374 | case SND_SOC_DAIFMT_CBM_CFM: |
| 1375 | /* Codec Master */ |
| 1376 | aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK); |
| 1377 | break; |
| 1378 | case SND_SOC_DAIFMT_CBS_CFS: |
| 1379 | /* Codec Slave */ |
Peter Ujfalusi | adcb8bc | 2009-12-31 10:30:23 +0200 | [diff] [blame] | 1380 | if (dac33->fifo_mode) { |
| 1381 | dev_err(codec->dev, "FIFO mode requires master mode\n"); |
| 1382 | return -EINVAL; |
| 1383 | } else |
| 1384 | aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1385 | break; |
| 1386 | default: |
| 1387 | return -EINVAL; |
| 1388 | } |
| 1389 | |
| 1390 | aictrl_a &= ~DAC33_AFMT_MASK; |
| 1391 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 1392 | case SND_SOC_DAIFMT_I2S: |
| 1393 | aictrl_a |= DAC33_AFMT_I2S; |
| 1394 | break; |
| 1395 | case SND_SOC_DAIFMT_DSP_A: |
| 1396 | aictrl_a |= DAC33_AFMT_DSP; |
| 1397 | aictrl_b &= ~DAC33_DATA_DELAY_MASK; |
Peter Ujfalusi | 44f497b | 2010-03-19 11:10:19 +0200 | [diff] [blame] | 1398 | aictrl_b |= DAC33_DATA_DELAY(0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1399 | break; |
| 1400 | case SND_SOC_DAIFMT_RIGHT_J: |
| 1401 | aictrl_a |= DAC33_AFMT_RIGHT_J; |
| 1402 | break; |
| 1403 | case SND_SOC_DAIFMT_LEFT_J: |
| 1404 | aictrl_a |= DAC33_AFMT_LEFT_J; |
| 1405 | break; |
| 1406 | default: |
| 1407 | dev_err(codec->dev, "Unsupported format (%u)\n", |
| 1408 | fmt & SND_SOC_DAIFMT_FORMAT_MASK); |
| 1409 | return -EINVAL; |
| 1410 | } |
| 1411 | |
| 1412 | dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a); |
| 1413 | dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b); |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1418 | static int dac33_soc_probe(struct snd_soc_codec *codec) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1419 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1420 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1421 | int ret = 0; |
| 1422 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1423 | codec->control_data = dac33->control_data; |
| 1424 | codec->hw_write = (hw_write_t) i2c_master_send; |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 1425 | codec->dapm.idle_bias_off = 1; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1426 | dac33->codec = codec; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1427 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1428 | /* Read the tlv320dac33 ID registers */ |
| 1429 | ret = dac33_hard_power(codec, 1); |
| 1430 | if (ret != 0) { |
| 1431 | dev_err(codec->dev, "Failed to power up codec: %d\n", ret); |
| 1432 | goto err_power; |
| 1433 | } |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 1434 | ret = dac33_read_id(codec); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1435 | dac33_hard_power(codec, 0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1436 | |
Peter Ujfalusi | 911a0f0 | 2010-10-26 11:45:59 +0300 | [diff] [blame] | 1437 | if (ret < 0) { |
| 1438 | dev_err(codec->dev, "Failed to read chip ID: %d\n", ret); |
| 1439 | ret = -ENODEV; |
| 1440 | goto err_power; |
| 1441 | } |
| 1442 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1443 | /* Check if the IRQ number is valid and request it */ |
| 1444 | if (dac33->irq >= 0) { |
| 1445 | ret = request_irq(dac33->irq, dac33_interrupt_handler, |
| 1446 | IRQF_TRIGGER_RISING | IRQF_DISABLED, |
| 1447 | codec->name, codec); |
| 1448 | if (ret < 0) { |
| 1449 | dev_err(codec->dev, "Could not request IRQ%d (%d)\n", |
| 1450 | dac33->irq, ret); |
| 1451 | dac33->irq = -1; |
| 1452 | } |
| 1453 | if (dac33->irq != -1) { |
| 1454 | /* Setup work queue */ |
| 1455 | dac33->dac33_wq = |
| 1456 | create_singlethread_workqueue("tlv320dac33"); |
| 1457 | if (dac33->dac33_wq == NULL) { |
| 1458 | free_irq(dac33->irq, codec); |
| 1459 | return -ENOMEM; |
| 1460 | } |
| 1461 | |
| 1462 | INIT_WORK(&dac33->work, dac33_work); |
| 1463 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | snd_soc_add_controls(codec, dac33_snd_controls, |
| 1467 | ARRAY_SIZE(dac33_snd_controls)); |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1468 | /* Only add the FIFO controls, if we have valid IRQ number */ |
| 1469 | if (dac33->irq >= 0) { |
| 1470 | snd_soc_add_controls(codec, dac33_mode_snd_controls, |
| 1471 | ARRAY_SIZE(dac33_mode_snd_controls)); |
| 1472 | /* FIFO usage controls only, if autoio config is not selected */ |
| 1473 | if (!dac33->auto_fifo_config) |
| 1474 | snd_soc_add_controls(codec, dac33_fifo_snd_controls, |
| 1475 | ARRAY_SIZE(dac33_fifo_snd_controls)); |
| 1476 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1477 | dac33_add_widgets(codec); |
| 1478 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1479 | err_power: |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1480 | return ret; |
| 1481 | } |
| 1482 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1483 | static int dac33_soc_remove(struct snd_soc_codec *codec) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1484 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1485 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1486 | |
| 1487 | dac33_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 1488 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1489 | if (dac33->irq >= 0) { |
| 1490 | free_irq(dac33->irq, dac33->codec); |
| 1491 | destroy_workqueue(dac33->dac33_wq); |
| 1492 | } |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1493 | return 0; |
| 1494 | } |
| 1495 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1496 | static int dac33_soc_suspend(struct snd_soc_codec *codec, pm_message_t state) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1497 | { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1498 | dac33_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1503 | static int dac33_soc_resume(struct snd_soc_codec *codec) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1504 | { |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1505 | dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1510 | static struct snd_soc_codec_driver soc_codec_dev_tlv320dac33 = { |
| 1511 | .read = dac33_read_reg_cache, |
| 1512 | .write = dac33_write_locked, |
| 1513 | .set_bias_level = dac33_set_bias_level, |
| 1514 | .reg_cache_size = ARRAY_SIZE(dac33_reg), |
| 1515 | .reg_word_size = sizeof(u8), |
| 1516 | .reg_cache_default = dac33_reg, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1517 | .probe = dac33_soc_probe, |
| 1518 | .remove = dac33_soc_remove, |
| 1519 | .suspend = dac33_soc_suspend, |
| 1520 | .resume = dac33_soc_resume, |
| 1521 | }; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1522 | |
| 1523 | #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \ |
| 1524 | SNDRV_PCM_RATE_48000) |
| 1525 | #define DAC33_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 1526 | |
| 1527 | static struct snd_soc_dai_ops dac33_dai_ops = { |
Peter Ujfalusi | 0b61d2b | 2010-04-30 14:59:35 +0300 | [diff] [blame] | 1528 | .startup = dac33_startup, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1529 | .shutdown = dac33_shutdown, |
| 1530 | .hw_params = dac33_hw_params, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1531 | .trigger = dac33_pcm_trigger, |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1532 | .delay = dac33_dai_delay, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1533 | .set_sysclk = dac33_set_dai_sysclk, |
| 1534 | .set_fmt = dac33_set_dai_fmt, |
| 1535 | }; |
| 1536 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1537 | static struct snd_soc_dai_driver dac33_dai = { |
| 1538 | .name = "tlv320dac33-hifi", |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1539 | .playback = { |
| 1540 | .stream_name = "Playback", |
| 1541 | .channels_min = 2, |
| 1542 | .channels_max = 2, |
| 1543 | .rates = DAC33_RATES, |
| 1544 | .formats = DAC33_FORMATS,}, |
| 1545 | .ops = &dac33_dai_ops, |
| 1546 | }; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1547 | |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 1548 | static int __devinit dac33_i2c_probe(struct i2c_client *client, |
| 1549 | const struct i2c_device_id *id) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1550 | { |
| 1551 | struct tlv320dac33_platform_data *pdata; |
| 1552 | struct tlv320dac33_priv *dac33; |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1553 | int ret, i; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1554 | |
| 1555 | if (client->dev.platform_data == NULL) { |
| 1556 | dev_err(&client->dev, "Platform data not set\n"); |
| 1557 | return -ENODEV; |
| 1558 | } |
| 1559 | pdata = client->dev.platform_data; |
| 1560 | |
| 1561 | dac33 = kzalloc(sizeof(struct tlv320dac33_priv), GFP_KERNEL); |
| 1562 | if (dac33 == NULL) |
| 1563 | return -ENOMEM; |
| 1564 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1565 | dac33->control_data = client; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1566 | mutex_init(&dac33->mutex); |
Peter Ujfalusi | f57d2cf | 2010-04-23 10:10:01 +0300 | [diff] [blame] | 1567 | spin_lock_init(&dac33->lock); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1568 | |
| 1569 | i2c_set_clientdata(client, dac33); |
| 1570 | |
| 1571 | dac33->power_gpio = pdata->power_gpio; |
Peter Ujfalusi | 6aceabb | 2010-01-20 09:39:36 +0200 | [diff] [blame] | 1572 | dac33->burst_bclkdiv = pdata->burst_bclkdiv; |
Peter Ujfalusi | 76f47127 | 2010-04-23 10:10:00 +0300 | [diff] [blame] | 1573 | /* Pre calculate the burst rate */ |
| 1574 | dac33->burst_rate = BURST_BASEFREQ_HZ / dac33->burst_bclkdiv / 32; |
Peter Ujfalusi | eeb309a | 2010-03-11 16:26:22 +0200 | [diff] [blame] | 1575 | dac33->keep_bclk = pdata->keep_bclk; |
Peter Ujfalusi | a577b31 | 2010-07-28 15:26:55 +0300 | [diff] [blame] | 1576 | dac33->auto_fifo_config = pdata->auto_fifo_config; |
Peter Ujfalusi | f430a27 | 2010-07-28 15:26:54 +0300 | [diff] [blame] | 1577 | dac33->mode1_latency = pdata->mode1_latency; |
| 1578 | if (!dac33->mode1_latency) |
| 1579 | dac33->mode1_latency = 10000; /* 10ms */ |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1580 | dac33->irq = client->irq; |
| 1581 | dac33->nsample = NSAMPLE_MAX; |
Peter Ujfalusi | 55abb59 | 2010-04-23 10:09:58 +0300 | [diff] [blame] | 1582 | dac33->nsample_max = NSAMPLE_MAX; |
Peter Ujfalusi | 9d7db2b | 2010-06-07 10:50:39 +0300 | [diff] [blame] | 1583 | dac33->uthr = MODE7_UTHR; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1584 | /* Disable FIFO use by default */ |
Peter Ujfalusi | 7427b4b | 2009-12-31 10:30:19 +0200 | [diff] [blame] | 1585 | dac33->fifo_mode = DAC33_FIFO_BYPASS; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1586 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1587 | /* Check if the reset GPIO number is valid and request it */ |
| 1588 | if (dac33->power_gpio >= 0) { |
| 1589 | ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset"); |
| 1590 | if (ret < 0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1591 | dev_err(&client->dev, |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1592 | "Failed to request reset GPIO (%d)\n", |
| 1593 | dac33->power_gpio); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1594 | goto err_gpio; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1595 | } |
| 1596 | gpio_direction_output(dac33->power_gpio, 0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1597 | } |
| 1598 | |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1599 | for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++) |
| 1600 | dac33->supplies[i].supply = dac33_supply_names[i]; |
| 1601 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1602 | ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(dac33->supplies), |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1603 | dac33->supplies); |
| 1604 | |
| 1605 | if (ret != 0) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1606 | dev_err(&client->dev, "Failed to request supplies: %d\n", ret); |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1607 | goto err_get; |
| 1608 | } |
| 1609 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1610 | ret = snd_soc_register_codec(&client->dev, |
| 1611 | &soc_codec_dev_tlv320dac33, &dac33_dai, 1); |
| 1612 | if (ret < 0) |
| 1613 | goto err_register; |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1614 | |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1615 | return ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1616 | err_register: |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1617 | regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies); |
| 1618 | err_get: |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1619 | if (dac33->power_gpio >= 0) |
| 1620 | gpio_free(dac33->power_gpio); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1621 | err_gpio: |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1622 | kfree(dac33); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 1626 | static int __devexit dac33_i2c_remove(struct i2c_client *client) |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1627 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1628 | struct tlv320dac33_priv *dac33 = i2c_get_clientdata(client); |
Peter Ujfalusi | 239fe55 | 2010-04-30 14:59:34 +0300 | [diff] [blame] | 1629 | |
| 1630 | if (unlikely(dac33->chip_power)) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1631 | dac33_hard_power(dac33->codec, 0); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1632 | |
| 1633 | if (dac33->power_gpio >= 0) |
| 1634 | gpio_free(dac33->power_gpio); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1635 | |
Ilkka Koskinen | 3a7aaed | 2009-12-04 13:49:10 +0200 | [diff] [blame] | 1636 | regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies); |
| 1637 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1638 | snd_soc_unregister_codec(&client->dev); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1639 | kfree(dac33); |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | static const struct i2c_device_id tlv320dac33_i2c_id[] = { |
| 1645 | { |
| 1646 | .name = "tlv320dac33", |
| 1647 | .driver_data = 0, |
| 1648 | }, |
| 1649 | { }, |
| 1650 | }; |
| 1651 | |
| 1652 | static struct i2c_driver tlv320dac33_i2c_driver = { |
| 1653 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1654 | .name = "tlv320dac33-codec", |
Peter Ujfalusi | c8bf93f | 2009-10-15 09:03:56 +0300 | [diff] [blame] | 1655 | .owner = THIS_MODULE, |
| 1656 | }, |
| 1657 | .probe = dac33_i2c_probe, |
| 1658 | .remove = __devexit_p(dac33_i2c_remove), |
| 1659 | .id_table = tlv320dac33_i2c_id, |
| 1660 | }; |
| 1661 | |
| 1662 | static int __init dac33_module_init(void) |
| 1663 | { |
| 1664 | int r; |
| 1665 | r = i2c_add_driver(&tlv320dac33_i2c_driver); |
| 1666 | if (r < 0) { |
| 1667 | printk(KERN_ERR "DAC33: driver registration failed\n"); |
| 1668 | return r; |
| 1669 | } |
| 1670 | return 0; |
| 1671 | } |
| 1672 | module_init(dac33_module_init); |
| 1673 | |
| 1674 | static void __exit dac33_module_exit(void) |
| 1675 | { |
| 1676 | i2c_del_driver(&tlv320dac33_i2c_driver); |
| 1677 | } |
| 1678 | module_exit(dac33_module_exit); |
| 1679 | |
| 1680 | |
| 1681 | MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver"); |
| 1682 | MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>"); |
| 1683 | MODULE_LICENSE("GPL"); |