blob: cee0f991240841694a030b0d08e2c5202999b9c3 [file] [log] [blame]
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001/*
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 Koskinen3a7aaed2009-12-04 13:49:10 +020033#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030039#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 Ujfalusi42603932010-04-23 10:09:59 +030051#define MODE7_LTHR 10
52#define MODE7_UTHR (DAC33_BUFFER_SIZE_SAMPLES - 10)
53
Peter Ujfalusi76f471272010-04-23 10:10:00 +030054#define BURST_BASEFREQ_HZ 49152000
55
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +030056#define SAMPLES_TO_US(rate, samples) \
57 (1000000000 / ((rate * 1000) / samples))
58
59#define US_TO_SAMPLES(rate, us) \
Peter Ujfalusid54e1f42010-10-29 14:07:25 +030060 (rate / (1000000 / (us < 1000000 ? us : 1000000)))
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +030061
Peter Ujfalusia577b312010-07-28 15:26:55 +030062#define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \
63 ((samples * 5000) / ((burstrate * 5000) / (burstrate - playrate)))
64
Peter Ujfalusiad05c032010-04-30 14:59:36 +030065static void dac33_calculate_times(struct snd_pcm_substream *substream);
66static int dac33_prepare_chip(struct snd_pcm_substream *substream);
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +030067
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030068enum dac33_state {
69 DAC33_IDLE = 0,
70 DAC33_PREFILL,
71 DAC33_PLAYBACK,
72 DAC33_FLUSH,
73};
74
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020075enum dac33_fifo_modes {
76 DAC33_FIFO_BYPASS = 0,
77 DAC33_FIFO_MODE1,
Peter Ujfalusi28e05d92009-12-31 10:30:22 +020078 DAC33_FIFO_MODE7,
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020079 DAC33_FIFO_LAST_MODE,
80};
81
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020082#define DAC33_NUM_SUPPLIES 3
83static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = {
84 "AVDD",
85 "DVDD",
86 "IOVDD",
87};
88
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030089struct tlv320dac33_priv {
90 struct mutex mutex;
91 struct workqueue_struct *dac33_wq;
92 struct work_struct work;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000093 struct snd_soc_codec *codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020094 struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES];
Peter Ujfalusi0b61d2b2010-04-30 14:59:35 +030095 struct snd_pcm_substream *substream;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030096 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 Ujfalusi7427b4b2009-12-31 10:30:19 +0200106 enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300107 unsigned int nsample; /* burst read amount from host */
Peter Ujfalusif430a272010-07-28 15:26:54 +0300108 int mode1_latency; /* latency caused by the i2c writes in
109 * us */
Peter Ujfalusia577b312010-07-28 15:26:55 +0300110 int auto_fifo_config; /* Configure the FIFO based on the
111 * period size */
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +0200112 u8 burst_bclkdiv; /* BCLK divider value in burst mode */
Peter Ujfalusi76f471272010-04-23 10:10:00 +0300113 unsigned int burst_rate; /* Interface speed in Burst modes */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300114
Peter Ujfalusieeb309a2010-03-11 16:26:22 +0200115 int keep_bclk; /* Keep the BCLK continuously running
116 * in FIFO modes */
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300117 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300124
Peter Ujfalusi9d7db2b2010-06-07 10:50:39 +0300125 unsigned int uthr;
126
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300127 enum dac33_state state;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000128 enum snd_soc_control_type control_type;
129 void *control_data;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300130};
131
132static const u8 dac33_reg[DAC33_CACHEREGNUM] = {
1330x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
1340x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
1350x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
1360x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
1370x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
1380x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
1390x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
1400x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
1410x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
1420x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
1430x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
1440x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
1450x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
1460x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
1470x00, 0x00, /* 0x38 - 0x39 */
148/* Registers 0x3a - 0x3f are reserved */
149 0x00, 0x00, /* 0x3a - 0x3b */
1500x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */
151
1520x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
1530x00, 0x80, /* 0x44 - 0x45 */
154/* Registers 0x46 - 0x47 are reserved */
155 0x80, 0x80, /* 0x46 - 0x47 */
156
1570x80, 0x00, 0x00, /* 0x48 - 0x4a */
158/* Registers 0x4b - 0x7c are reserved */
159 0x00, /* 0x4b */
1600x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
1610x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
1620x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
1630x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
1640x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
1650x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
1660x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
1670x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
1680x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
1690x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
1700x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
1710x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
1720x00, /* 0x7c */
173
174 0xda, 0x33, 0x03, /* 0x7d - 0x7f */
175};
176
177/* Register read and write */
178static 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
188static 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
198static int dac33_read(struct snd_soc_codec *codec, unsigned int reg,
199 u8 *value)
200{
Mark Brownb2c812e2010-04-14 15:35:19 +0900201 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi911a0f02010-10-26 11:45:59 +0300202 int val, ret = 0;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300203
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 Ujfalusi911a0f02010-10-26 11:45:59 +0300212 ret = val;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300213 } 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 Ujfalusi911a0f02010-10-26 11:45:59 +0300221 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300222}
223
224static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
225 unsigned int value)
226{
Mark Brownb2c812e2010-04-14 15:35:19 +0900227 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300228 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
251static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
252 unsigned int value)
253{
Mark Brownb2c812e2010-04-14 15:35:19 +0900254 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300255 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
265static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg,
266 unsigned int value)
267{
Mark Brownb2c812e2010-04-14 15:35:19 +0900268 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300269 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 Ujfalusief909d62010-04-30 14:59:33 +0300298static void dac33_init_chip(struct snd_soc_codec *codec)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300299{
Mark Brownb2c812e2010-04-14 15:35:19 +0900300 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300301
Peter Ujfalusief909d62010-04-30 14:59:33 +0300302 if (unlikely(!dac33->chip_power))
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300303 return;
304
Peter Ujfalusief909d62010-04-30 14:59:33 +0300305 /* 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 Ujfalusief909d62010-04-30 14:59:33 +0300314 /* 73 : volume soft stepping control,
315 clock source = internal osc (?) */
316 dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);
317
Peter Ujfalusief909d62010-04-30 14:59:33 +0300318 /* 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300328}
329
Peter Ujfalusi911a0f02010-10-26 11:45:59 +0300330static inline int dac33_read_id(struct snd_soc_codec *codec)
Peter Ujfalusi239fe552010-04-30 14:59:34 +0300331{
Peter Ujfalusi911a0f02010-10-26 11:45:59 +0300332 int i, ret = 0;
Peter Ujfalusi239fe552010-04-30 14:59:34 +0300333 u8 reg;
334
Peter Ujfalusi911a0f02010-10-26 11:45:59 +0300335 for (i = 0; i < 3; i++) {
336 ret = dac33_read(codec, DAC33_DEVICE_ID_MSB + i, &reg);
337 if (ret < 0)
338 break;
339 }
340
341 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300342}
343
344static 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 Ujfalusic3746a02010-03-11 16:26:21 +0200352 reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB |
353 DAC33_DACRPDNB | DAC33_DACLPDNB);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300354 dac33_write(codec, DAC33_PWR_CTRL, reg);
355}
356
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200357static int dac33_hard_power(struct snd_soc_codec *codec, int power)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300358{
Mark Brownb2c812e2010-04-14 15:35:19 +0900359 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300360 int ret = 0;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300361
362 mutex_lock(&dac33->mutex);
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300363
364 /* Safety check */
365 if (unlikely(power == dac33->chip_power)) {
Felipe Balbi7fd1d742010-05-17 14:21:45 +0300366 dev_dbg(codec->dev, "Trying to set the same power state: %s\n",
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300367 power ? "ON" : "OFF");
368 goto exit;
369 }
370
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300371 if (power) {
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200372 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300378 }
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200379
380 if (dac33->power_gpio >= 0)
381 gpio_set_value(dac33->power_gpio, 1);
382
383 dac33->chip_power = 1;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300384 } else {
385 dac33_soft_power(codec, 0);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200386 if (dac33->power_gpio >= 0)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300387 gpio_set_value(dac33->power_gpio, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300388
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200389 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
400exit:
401 mutex_unlock(&dac33->mutex);
402 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300403}
404
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300405static 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300421static 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 Brownb2c812e2010-04-14 15:35:19 +0900425 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300426
427 ucontrol->value.integer.value[0] = dac33->nsample;
428
429 return 0;
430}
431
432static 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 Brownb2c812e2010-04-14 15:35:19 +0900436 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300437 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 Ujfalusif57d2cf2010-04-23 10:10:01 +0300443 ucontrol->value.integer.value[0] > dac33->nsample_max) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300444 ret = -EINVAL;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300445 } else {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300446 dac33->nsample = ucontrol->value.integer.value[0];
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300447 /* Re calculate the burst time */
448 dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate,
449 dac33->nsample);
450 }
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300451
452 return ret;
453}
454
Peter Ujfalusi9d7db2b2010-06-07 10:50:39 +0300455static 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
466static 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 Ujfalusi7427b4b2009-12-31 10:30:19 +0200488static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300489 struct snd_ctl_elem_value *ucontrol)
490{
491 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900492 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300493
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200494 ucontrol->value.integer.value[0] = dac33->fifo_mode;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300495
496 return 0;
497}
498
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200499static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300500 struct snd_ctl_elem_value *ucontrol)
501{
502 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900503 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300504 int ret = 0;
505
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200506 if (dac33->fifo_mode == ucontrol->value.integer.value[0])
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300507 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 Ujfalusi7427b4b2009-12-31 10:30:19 +0200513 ucontrol->value.integer.value[0] >= DAC33_FIFO_LAST_MODE)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300514 ret = -EINVAL;
515 else
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200516 dac33->fifo_mode = ucontrol->value.integer.value[0];
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300517
518 return ret;
519}
520
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200521/* Codec operation modes */
522static const char *dac33_fifo_mode_texts[] = {
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200523 "Bypass", "Mode 1", "Mode 7"
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200524};
525
526static 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 Ujfalusicf4bb6982010-10-13 11:56:28 +0300530/* L/R Line Output Gain */
531static 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
536static 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
541static 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300546/*
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 */
553static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0);
554
555static 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 Ujfalusicf4bb6982010-10-13 11:56:28 +0300563 SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum),
564 SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum),
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300565};
566
Peter Ujfalusia577b312010-07-28 15:26:55 +0300567static const struct snd_kcontrol_new dac33_mode_snd_controls[] = {
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200568 SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum,
569 dac33_get_fifo_mode, dac33_set_fifo_mode),
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300570};
571
Peter Ujfalusia577b312010-07-28 15:26:55 +0300572static 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300579/* Analog bypass */
580static const struct snd_kcontrol_new dac33_dapm_abypassl_control =
581 SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1);
582
583static const struct snd_kcontrol_new dac33_dapm_abypassr_control =
584 SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1);
585
586static 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 Ujfalusi76eac392010-12-08 16:04:33 +0200593 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300595
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 Ujfalusi9e871862010-12-08 16:04:32 +0200602 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amplifier",
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300603 DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0),
Peter Ujfalusi9e871862010-12-08 16:04:32 +0200604 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amplifier",
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300605 DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0),
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300606
Peter Ujfalusi76eac392010-12-08 16:04:33 +0200607 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 Ujfalusiad05c032010-04-30 14:59:36 +0300612 SND_SOC_DAPM_PRE("Prepare Playback", playback_event),
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300613};
614
615static 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 Ujfalusi9e871862010-12-08 16:04:32 +0200620 {"Output Left Amplifier", NULL, "DACL"},
621 {"Output Right Amplifier", NULL, "DACR"},
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300622
Peter Ujfalusi9e871862010-12-08 16:04:32 +0200623 {"Output Left Amplifier", NULL, "Analog Left Bypass"},
624 {"Output Right Amplifier", NULL, "Analog Right Bypass"},
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300625
Peter Ujfalusi76eac392010-12-08 16:04:33 +0200626 {"Output Left Amplifier", NULL, "Left DAC Power"},
627 {"Output Right Amplifier", NULL, "Right DAC Power"},
628
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300629 /* output */
Peter Ujfalusi9e871862010-12-08 16:04:32 +0200630 {"LEFT_LO", NULL, "Output Left Amplifier"},
631 {"RIGHT_LO", NULL, "Output Right Amplifier"},
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300632};
633
634static int dac33_add_widgets(struct snd_soc_codec *codec)
635{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200636 struct snd_soc_dapm_context *dapm = &codec->dapm;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300637
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200638 snd_soc_dapm_new_controls(dapm, dac33_dapm_widgets,
639 ARRAY_SIZE(dac33_dapm_widgets));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300640 /* set up audio path interconnects */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200641 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300642
643 return 0;
644}
645
646static int dac33_set_bias_level(struct snd_soc_codec *codec,
647 enum snd_soc_bias_level level)
648{
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200649 int ret;
650
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300651 switch (level) {
652 case SND_SOC_BIAS_ON:
Peter Ujfalusi3e202342010-11-30 14:31:46 +0200653 if (!dac33->substream)
654 dac33_soft_power(codec, 1);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300655 break;
656 case SND_SOC_BIAS_PREPARE:
657 break;
658 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200659 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300660 /* Coming from OFF, switch on the codec */
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200661 ret = dac33_hard_power(codec, 1);
662 if (ret != 0)
663 return ret;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200664
Peter Ujfalusiad05c032010-04-30 14:59:36 +0300665 dac33_init_chip(codec);
666 }
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300667 break;
668 case SND_SOC_BIAS_OFF:
Peter Ujfalusi2d4cdd62010-05-17 14:21:46 +0300669 /* Do not power off, when the codec is already off */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200670 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
Peter Ujfalusi2d4cdd62010-05-17 14:21:46 +0300671 return 0;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200672 ret = dac33_hard_power(codec, 0);
673 if (ret != 0)
674 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300675 break;
676 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200677 codec->dapm.bias_level = level;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300678
679 return 0;
680}
681
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200682static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33)
683{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 struct snd_soc_codec *codec = dac33->codec;
Peter Ujfalusi84eae182010-10-22 15:11:20 +0300685 unsigned int delay;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200686
687 switch (dac33->fifo_mode) {
688 case DAC33_FIFO_MODE1:
689 dac33_write16(codec, DAC33_NSAMPLE_MSB,
Peter Ujfalusif430a272010-07-28 15:26:54 +0300690 DAC33_THRREG(dac33->nsample));
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300691
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 Ujfalusid4f102d2009-12-31 10:30:20 +0200698 dac33_write16(codec, DAC33_PREFILL_MSB,
699 DAC33_THRREG(dac33->alarm_threshold));
Peter Ujfalusif4d59322010-04-23 10:09:57 +0300700 /* Enable Alarm Threshold IRQ with a delay */
Peter Ujfalusi84eae182010-10-22 15:11:20 +0300701 delay = SAMPLES_TO_US(dac33->burst_rate,
702 dac33->alarm_threshold) + 1000;
703 usleep_range(delay, delay + 500);
Peter Ujfalusif4d59322010-04-23 10:09:57 +0300704 dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200705 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200706 case DAC33_FIFO_MODE7:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300707 /* 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 Ujfalusi28e05d92009-12-31 10:30:22 +0200714 dac33_write16(codec, DAC33_PREFILL_MSB,
Peter Ujfalusi42603932010-04-23 10:09:59 +0300715 DAC33_THRREG(MODE7_LTHR));
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300716
717 /* Enable Upper Threshold IRQ */
718 dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MUT);
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200719 break;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200720 default:
721 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
722 dac33->fifo_mode);
723 break;
724 }
725}
726
727static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33)
728{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 struct snd_soc_codec *codec = dac33->codec;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200730
731 switch (dac33->fifo_mode) {
732 case DAC33_FIFO_MODE1:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300733 /* 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 Ujfalusid4f102d2009-12-31 10:30:20 +0200738 dac33_write16(codec, DAC33_NSAMPLE_MSB,
739 DAC33_THRREG(dac33->nsample));
740 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200741 case DAC33_FIFO_MODE7:
742 /* At the moment we are not using interrupts in mode7 */
743 break;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200744 default:
745 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
746 dac33->fifo_mode);
747 break;
748 }
749}
750
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300751static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 codec = dac33->codec;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300759
760 mutex_lock(&dac33->mutex);
761 switch (dac33->state) {
762 case DAC33_PREFILL:
763 dac33->state = DAC33_PLAYBACK;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200764 dac33_prefill_handler(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300765 break;
766 case DAC33_PLAYBACK:
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200767 dac33_playback_handler(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300768 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
785static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
786{
787 struct snd_soc_codec *codec = dev;
Mark Brownb2c812e2010-04-14 15:35:19 +0900788 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300789
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300790 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300797
798 return IRQ_HANDLED;
799}
800
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300801static void dac33_oscwait(struct snd_soc_codec *codec)
802{
Peter Ujfalusi84eae182010-10-22 15:11:20 +0300803 int timeout = 60;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300804 u8 reg;
805
806 do {
Peter Ujfalusi84eae182010-10-22 15:11:20 +0300807 usleep_range(1000, 2000);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300808 dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
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 Ujfalusi0b61d2b2010-04-30 14:59:35 +0300815static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusi0b61d2b2010-04-30 14:59:35 +0300820 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
828static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000832 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusi0b61d2b2010-04-30 14:59:35 +0300833 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
834
835 dac33->substream = NULL;
Peter Ujfalusif430a272010-07-28 15:26:54 +0300836
837 /* Reset the nSample restrictions */
838 dac33->nsample_min = 0;
839 dac33->nsample_max = NSAMPLE_MAX;
Peter Ujfalusi0b61d2b2010-04-30 14:59:35 +0300840}
841
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300842static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300848
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 Ujfalusi7833ae02010-02-16 13:23:16 +0200873 ((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300874#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 */
882static int dac33_prepare_chip(struct snd_pcm_substream *substream)
883{
884 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000885 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900886 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300887 unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200888 u8 aictrl_a, aictrl_b, fifoctrl_a;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300889
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 Ujfalusie5e878c2010-02-16 13:23:15 +0200906 /* Read FIFO control A, and clear FIFO flush bit */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300907 fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
Peter Ujfalusie5e878c2010-02-16 13:23:15 +0200908 fifoctrl_a &= ~DAC33_FIFOFLUSH;
909
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300910 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 Ujfalusiad05c032010-04-30 14:59:36 +0300923
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 Ujfalusic3746a02010-03-11 16:26:21 +0200933 dac33_soft_power(codec, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300934 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 Ujfalusi7427b4b2009-12-31 10:30:19 +0200958 if (dac33->fifo_mode) {
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200959 /* Generic for all FIFO modes */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300960 /* 50-51 : ASRC Control registers */
Peter Ujfalusifdb6b1e2010-03-19 11:10:20 +0200961 dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300962 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 Ujfalusic8bf93f2009-10-15 09:03:56 +0300969 } else {
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200970 /* FIFO bypass mode */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300971 /* 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 Ujfalusiaec242d2009-12-31 10:30:21 +0200976 /* 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 Ujfalusiaec242d2009-12-31 10:30:21 +0200981 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200982 case DAC33_FIFO_MODE7:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300983 dac33_write(codec, DAC33_FIFO_IRQ_MODE_A,
984 DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL));
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200985 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200986 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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001001 fifoctrl_a &= ~DAC33_FBYPAS;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001002 fifoctrl_a &= ~DAC33_FAUTO;
Peter Ujfalusieeb309a2010-03-11 16:26:22 +02001003 if (dac33->keep_bclk)
1004 aictrl_b |= DAC33_BCLKON;
1005 else
1006 aictrl_b &= ~DAC33_BCLKON;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001007 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +02001008 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 Ujfalusieeb309a2010-03-11 16:26:22 +02001017 if (dac33->keep_bclk)
1018 aictrl_b |= DAC33_BCLKON;
1019 else
1020 aictrl_b &= ~DAC33_BCLKON;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +02001021 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001022 default:
1023 /*
1024 * For FIFO bypass mode:
1025 * Enable the FIFO bypass (Disable the FIFO use)
1026 * Set the BCLK as continous
1027 */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001028 fifoctrl_a |= DAC33_FBYPAS;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001029 aictrl_b |= DAC33_BCLKON;
1030 break;
1031 }
1032
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001033 dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001034 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001035 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001036
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +02001037 /*
1038 * BCLK divide ratio
1039 * 0: 1.5
1040 * 1: 1
1041 * 2: 2
1042 * ...
1043 * 254: 254
1044 * 255: 255
1045 */
Peter Ujfalusi6cd6ced2010-01-20 09:39:35 +02001046 if (dac33->fifo_mode)
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +02001047 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C,
1048 dac33->burst_bclkdiv);
Peter Ujfalusi6cd6ced2010-01-20 09:39:35 +02001049 else
1050 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
1051
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001052 switch (dac33->fifo_mode) {
1053 case DAC33_FIFO_MODE1:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001054 dac33_write16(codec, DAC33_ATHR_MSB,
1055 DAC33_THRREG(dac33->alarm_threshold));
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001056 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +02001057 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 Ujfalusi9d7db2b2010-06-07 10:50:39 +03001062 dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr));
Peter Ujfalusi42603932010-04-23 10:09:59 +03001063 dac33_write16(codec, DAC33_LTHR_MSB, DAC33_THRREG(MODE7_LTHR));
Peter Ujfalusi28e05d92009-12-31 10:30:22 +02001064 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001065 default:
Peter Ujfalusiaec242d2009-12-31 10:30:21 +02001066 break;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001067 }
1068
1069 mutex_unlock(&dac33->mutex);
1070
1071 return 0;
1072}
1073
1074static void dac33_calculate_times(struct snd_pcm_substream *substream)
1075{
1076 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001077 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001078 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusif430a272010-07-28 15:26:54 +03001079 unsigned int period_size = substream->runtime->period_size;
1080 unsigned int rate = substream->runtime->rate;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001081 unsigned int nsample_limit;
1082
Peter Ujfalusi55abb592010-04-23 10:09:58 +03001083 /* In bypass mode we don't need to calculate */
1084 if (!dac33->fifo_mode)
1085 return;
1086
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001087 switch (dac33->fifo_mode) {
1088 case DAC33_FIFO_MODE1:
Peter Ujfalusif430a272010-07-28 15:26:54 +03001089 /* Number of samples under i2c latency */
1090 dac33->alarm_threshold = US_TO_SAMPLES(rate,
1091 dac33->mode1_latency);
Peter Ujfalusi1bc13b22010-10-29 09:49:37 +03001092 nsample_limit = DAC33_BUFFER_SIZE_SAMPLES -
1093 dac33->alarm_threshold;
1094
Peter Ujfalusia577b312010-07-28 15:26:55 +03001095 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 Ujfalusi1bc13b22010-10-29 09:49:37 +03001105 else if (period_size > nsample_limit)
1106 dac33->nsample = nsample_limit;
Peter Ujfalusia577b312010-07-28 15:26:55 +03001107 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 Ujfalusi1bc13b22010-10-29 09:49:37 +03001118
Peter Ujfalusia577b312010-07-28 15:26:55 +03001119 if (dac33->nsample_max > nsample_limit)
1120 dac33->nsample_max = nsample_limit;
Peter Ujfalusif430a272010-07-28 15:26:54 +03001121
Peter Ujfalusia577b312010-07-28 15:26:55 +03001122 /* 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 Ujfalusif430a272010-07-28 15:26:54 +03001128
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001129 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 Ujfalusia577b312010-07-28 15:26:55 +03001135 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 Ujfalusif57d2cf2010-04-23 10:10:01 +03001145 dac33->mode7_us_to_lthr =
Peter Ujfalusi9d7db2b2010-06-07 10:50:39 +03001146 SAMPLES_TO_US(substream->runtime->rate,
1147 dac33->uthr - MODE7_LTHR + 1);
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001148 dac33->t_stamp1 = 0;
1149 break;
1150 default:
1151 break;
1152 }
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001153
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001154}
1155
1156static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001161 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001162 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 Ujfalusi7427b4b2009-12-31 10:30:19 +02001168 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001169 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 Ujfalusi7427b4b2009-12-31 10:30:19 +02001176 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001177 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 Ujfalusif57d2cf2010-04-23 10:10:01 +03001188static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001193 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001194 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1195 unsigned long long t0, t1, t_now;
Peter Ujfalusi9d7db2b2010-06-07 10:50:39 +03001196 unsigned int time_delta, uthr;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001197 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 Ujfalusi9d7db2b2010-06-07 10:50:39 +03001274 uthr = dac33->uthr;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001275 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 Ujfalusi9d7db2b2010-06-07 10:50:39 +03001287 delay = uthr;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001288 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 Ujfalusi9d7db2b2010-06-07 10:50:39 +03001301 if (likely(uthr > samples_out))
1302 delay = uthr - samples_out;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001303 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 Ujfalusi9d7db2b2010-06-07 10:50:39 +03001320 if (unlikely(delay > uthr))
1321 delay = uthr;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001322 }
1323 break;
1324 default:
1325 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
1326 dac33->fifo_mode);
1327 break;
1328 }
1329out:
1330 return delay;
1331}
1332
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001333static 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 Brownb2c812e2010-04-14 15:35:19 +09001337 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001338 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
1363static 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 Brownb2c812e2010-04-14 15:35:19 +09001367 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001368 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 Ujfalusiadcb8bc2009-12-31 10:30:23 +02001380 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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001385 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 Ujfalusi44f497b2010-03-19 11:10:19 +02001398 aictrl_b |= DAC33_DATA_DELAY(0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001399 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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001418static int dac33_soc_probe(struct snd_soc_codec *codec)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001421 int ret = 0;
1422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 codec->control_data = dac33->control_data;
1424 codec->hw_write = (hw_write_t) i2c_master_send;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001425 codec->dapm.idle_bias_off = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426 dac33->codec = codec;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001427
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428 /* 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 Ujfalusi911a0f02010-10-26 11:45:59 +03001434 ret = dac33_read_id(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435 dac33_hard_power(codec, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001436
Peter Ujfalusi911a0f02010-10-26 11:45:59 +03001437 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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001443 /* 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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001464 }
1465
1466 snd_soc_add_controls(codec, dac33_snd_controls,
1467 ARRAY_SIZE(dac33_snd_controls));
Peter Ujfalusia577b312010-07-28 15:26:55 +03001468 /* 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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001477 dac33_add_widgets(codec);
1478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001479err_power:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001480 return ret;
1481}
1482
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483static int dac33_soc_remove(struct snd_soc_codec *codec)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001484{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001486
1487 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 if (dac33->irq >= 0) {
1490 free_irq(dac33->irq, dac33->codec);
1491 destroy_workqueue(dac33->dac33_wq);
1492 }
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001493 return 0;
1494}
1495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496static int dac33_soc_suspend(struct snd_soc_codec *codec, pm_message_t state)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001497{
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001498 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1499
1500 return 0;
1501}
1502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503static int dac33_soc_resume(struct snd_soc_codec *codec)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001504{
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001505 dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001506
1507 return 0;
1508}
1509
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510static 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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001517 .probe = dac33_soc_probe,
1518 .remove = dac33_soc_remove,
1519 .suspend = dac33_soc_suspend,
1520 .resume = dac33_soc_resume,
1521};
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001522
1523#define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
1524 SNDRV_PCM_RATE_48000)
1525#define DAC33_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1526
1527static struct snd_soc_dai_ops dac33_dai_ops = {
Peter Ujfalusi0b61d2b2010-04-30 14:59:35 +03001528 .startup = dac33_startup,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001529 .shutdown = dac33_shutdown,
1530 .hw_params = dac33_hw_params,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001531 .trigger = dac33_pcm_trigger,
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001532 .delay = dac33_dai_delay,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001533 .set_sysclk = dac33_set_dai_sysclk,
1534 .set_fmt = dac33_set_dai_fmt,
1535};
1536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537static struct snd_soc_dai_driver dac33_dai = {
1538 .name = "tlv320dac33-hifi",
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001539 .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 Ujfalusic8bf93f2009-10-15 09:03:56 +03001547
Mark Brown735fe4c2010-01-12 14:13:00 +00001548static int __devinit dac33_i2c_probe(struct i2c_client *client,
1549 const struct i2c_device_id *id)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001550{
1551 struct tlv320dac33_platform_data *pdata;
1552 struct tlv320dac33_priv *dac33;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001553 int ret, i;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001554
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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001565 dac33->control_data = client;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001566 mutex_init(&dac33->mutex);
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001567 spin_lock_init(&dac33->lock);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001568
1569 i2c_set_clientdata(client, dac33);
1570
1571 dac33->power_gpio = pdata->power_gpio;
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +02001572 dac33->burst_bclkdiv = pdata->burst_bclkdiv;
Peter Ujfalusi76f471272010-04-23 10:10:00 +03001573 /* Pre calculate the burst rate */
1574 dac33->burst_rate = BURST_BASEFREQ_HZ / dac33->burst_bclkdiv / 32;
Peter Ujfalusieeb309a2010-03-11 16:26:22 +02001575 dac33->keep_bclk = pdata->keep_bclk;
Peter Ujfalusia577b312010-07-28 15:26:55 +03001576 dac33->auto_fifo_config = pdata->auto_fifo_config;
Peter Ujfalusif430a272010-07-28 15:26:54 +03001577 dac33->mode1_latency = pdata->mode1_latency;
1578 if (!dac33->mode1_latency)
1579 dac33->mode1_latency = 10000; /* 10ms */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001580 dac33->irq = client->irq;
1581 dac33->nsample = NSAMPLE_MAX;
Peter Ujfalusi55abb592010-04-23 10:09:58 +03001582 dac33->nsample_max = NSAMPLE_MAX;
Peter Ujfalusi9d7db2b2010-06-07 10:50:39 +03001583 dac33->uthr = MODE7_UTHR;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001584 /* Disable FIFO use by default */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +02001585 dac33->fifo_mode = DAC33_FIFO_BYPASS;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001586
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001587 /* 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 Girdwoodf0fba2a2010-03-17 20:15:21 +00001591 dev_err(&client->dev,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001592 "Failed to request reset GPIO (%d)\n",
1593 dac33->power_gpio);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594 goto err_gpio;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001595 }
1596 gpio_direction_output(dac33->power_gpio, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001597 }
1598
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001599 for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
1600 dac33->supplies[i].supply = dac33_supply_names[i];
1601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602 ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(dac33->supplies),
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001603 dac33->supplies);
1604
1605 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 dev_err(&client->dev, "Failed to request supplies: %d\n", ret);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001607 goto err_get;
1608 }
1609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 ret = snd_soc_register_codec(&client->dev,
1611 &soc_codec_dev_tlv320dac33, &dac33_dai, 1);
1612 if (ret < 0)
1613 goto err_register;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001614
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001615 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001616err_register:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001617 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1618err_get:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001619 if (dac33->power_gpio >= 0)
1620 gpio_free(dac33->power_gpio);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621err_gpio:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001622 kfree(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001623 return ret;
1624}
1625
Mark Brown735fe4c2010-01-12 14:13:00 +00001626static int __devexit dac33_i2c_remove(struct i2c_client *client)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001627{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628 struct tlv320dac33_priv *dac33 = i2c_get_clientdata(client);
Peter Ujfalusi239fe552010-04-30 14:59:34 +03001629
1630 if (unlikely(dac33->chip_power))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001631 dac33_hard_power(dac33->codec, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001632
1633 if (dac33->power_gpio >= 0)
1634 gpio_free(dac33->power_gpio);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001635
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001636 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 snd_soc_unregister_codec(&client->dev);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001639 kfree(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001640
1641 return 0;
1642}
1643
1644static const struct i2c_device_id tlv320dac33_i2c_id[] = {
1645 {
1646 .name = "tlv320dac33",
1647 .driver_data = 0,
1648 },
1649 { },
1650};
1651
1652static struct i2c_driver tlv320dac33_i2c_driver = {
1653 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 .name = "tlv320dac33-codec",
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001655 .owner = THIS_MODULE,
1656 },
1657 .probe = dac33_i2c_probe,
1658 .remove = __devexit_p(dac33_i2c_remove),
1659 .id_table = tlv320dac33_i2c_id,
1660};
1661
1662static 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}
1672module_init(dac33_module_init);
1673
1674static void __exit dac33_module_exit(void)
1675{
1676 i2c_del_driver(&tlv320dac33_i2c_driver);
1677}
1678module_exit(dac33_module_exit);
1679
1680
1681MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
1682MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1683MODULE_LICENSE("GPL");