blob: 9944721a055c42a0f883846e64acae962316c598 [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>
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030034#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
38#include <sound/soc-dapm.h>
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
51#define LATENCY_TIME_MS 20
52
Peter Ujfalusi42603932010-04-23 10:09:59 +030053#define MODE7_LTHR 10
54#define MODE7_UTHR (DAC33_BUFFER_SIZE_SAMPLES - 10)
55
Peter Ujfalusi76f471272010-04-23 10:10:00 +030056#define BURST_BASEFREQ_HZ 49152000
57
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +030058#define SAMPLES_TO_US(rate, samples) \
59 (1000000000 / ((rate * 1000) / samples))
60
61#define US_TO_SAMPLES(rate, us) \
62 (rate / (1000000 / us))
63
64
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030065static struct snd_soc_codec *tlv320dac33_codec;
66
67enum dac33_state {
68 DAC33_IDLE = 0,
69 DAC33_PREFILL,
70 DAC33_PLAYBACK,
71 DAC33_FLUSH,
72};
73
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020074enum dac33_fifo_modes {
75 DAC33_FIFO_BYPASS = 0,
76 DAC33_FIFO_MODE1,
Peter Ujfalusi28e05d92009-12-31 10:30:22 +020077 DAC33_FIFO_MODE7,
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020078 DAC33_FIFO_LAST_MODE,
79};
80
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020081#define DAC33_NUM_SUPPLIES 3
82static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = {
83 "AVDD",
84 "DVDD",
85 "IOVDD",
86};
87
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030088struct tlv320dac33_priv {
89 struct mutex mutex;
90 struct workqueue_struct *dac33_wq;
91 struct work_struct work;
92 struct snd_soc_codec codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020093 struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES];
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030094 int power_gpio;
95 int chip_power;
96 int irq;
97 unsigned int refclk;
98
99 unsigned int alarm_threshold; /* set to be half of LATENCY_TIME_MS */
100 unsigned int nsample_min; /* nsample should not be lower than
101 * this */
102 unsigned int nsample_max; /* nsample should not be higher than
103 * this */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200104 enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300105 unsigned int nsample; /* burst read amount from host */
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +0200106 u8 burst_bclkdiv; /* BCLK divider value in burst mode */
Peter Ujfalusi76f471272010-04-23 10:10:00 +0300107 unsigned int burst_rate; /* Interface speed in Burst modes */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300108
Peter Ujfalusieeb309a2010-03-11 16:26:22 +0200109 int keep_bclk; /* Keep the BCLK continuously running
110 * in FIFO modes */
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300111 spinlock_t lock;
112 unsigned long long t_stamp1; /* Time stamp for FIFO modes to */
113 unsigned long long t_stamp2; /* calculate the FIFO caused delay */
114
115 unsigned int mode1_us_burst; /* Time to burst read n number of
116 * samples */
117 unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */
118
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300119 enum dac33_state state;
120};
121
122static const u8 dac33_reg[DAC33_CACHEREGNUM] = {
1230x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
1240x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
1250x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
1260x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
1270x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
1280x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
1290x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
1300x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
1310x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
1320x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
1330x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
1340x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
1350x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
1360x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
1370x00, 0x00, /* 0x38 - 0x39 */
138/* Registers 0x3a - 0x3f are reserved */
139 0x00, 0x00, /* 0x3a - 0x3b */
1400x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */
141
1420x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
1430x00, 0x80, /* 0x44 - 0x45 */
144/* Registers 0x46 - 0x47 are reserved */
145 0x80, 0x80, /* 0x46 - 0x47 */
146
1470x80, 0x00, 0x00, /* 0x48 - 0x4a */
148/* Registers 0x4b - 0x7c are reserved */
149 0x00, /* 0x4b */
1500x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
1510x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
1520x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
1530x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
1540x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
1550x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
1560x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
1570x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
1580x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
1590x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
1600x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
1610x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
1620x00, /* 0x7c */
163
164 0xda, 0x33, 0x03, /* 0x7d - 0x7f */
165};
166
167/* Register read and write */
168static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec *codec,
169 unsigned reg)
170{
171 u8 *cache = codec->reg_cache;
172 if (reg >= DAC33_CACHEREGNUM)
173 return 0;
174
175 return cache[reg];
176}
177
178static inline void dac33_write_reg_cache(struct snd_soc_codec *codec,
179 u8 reg, u8 value)
180{
181 u8 *cache = codec->reg_cache;
182 if (reg >= DAC33_CACHEREGNUM)
183 return;
184
185 cache[reg] = value;
186}
187
188static int dac33_read(struct snd_soc_codec *codec, unsigned int reg,
189 u8 *value)
190{
Mark Brownb2c812e2010-04-14 15:35:19 +0900191 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300192 int val;
193
194 *value = reg & 0xff;
195
196 /* If powered off, return the cached value */
197 if (dac33->chip_power) {
198 val = i2c_smbus_read_byte_data(codec->control_data, value[0]);
199 if (val < 0) {
200 dev_err(codec->dev, "Read failed (%d)\n", val);
201 value[0] = dac33_read_reg_cache(codec, reg);
202 } else {
203 value[0] = val;
204 dac33_write_reg_cache(codec, reg, val);
205 }
206 } else {
207 value[0] = dac33_read_reg_cache(codec, reg);
208 }
209
210 return 0;
211}
212
213static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
214 unsigned int value)
215{
Mark Brownb2c812e2010-04-14 15:35:19 +0900216 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300217 u8 data[2];
218 int ret = 0;
219
220 /*
221 * data is
222 * D15..D8 dac33 register offset
223 * D7...D0 register data
224 */
225 data[0] = reg & 0xff;
226 data[1] = value & 0xff;
227
228 dac33_write_reg_cache(codec, data[0], data[1]);
229 if (dac33->chip_power) {
230 ret = codec->hw_write(codec->control_data, data, 2);
231 if (ret != 2)
232 dev_err(codec->dev, "Write failed (%d)\n", ret);
233 else
234 ret = 0;
235 }
236
237 return ret;
238}
239
240static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
241 unsigned int value)
242{
Mark Brownb2c812e2010-04-14 15:35:19 +0900243 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300244 int ret;
245
246 mutex_lock(&dac33->mutex);
247 ret = dac33_write(codec, reg, value);
248 mutex_unlock(&dac33->mutex);
249
250 return ret;
251}
252
253#define DAC33_I2C_ADDR_AUTOINC 0x80
254static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg,
255 unsigned int value)
256{
Mark Brownb2c812e2010-04-14 15:35:19 +0900257 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300258 u8 data[3];
259 int ret = 0;
260
261 /*
262 * data is
263 * D23..D16 dac33 register offset
264 * D15..D8 register data MSB
265 * D7...D0 register data LSB
266 */
267 data[0] = reg & 0xff;
268 data[1] = (value >> 8) & 0xff;
269 data[2] = value & 0xff;
270
271 dac33_write_reg_cache(codec, data[0], data[1]);
272 dac33_write_reg_cache(codec, data[0] + 1, data[2]);
273
274 if (dac33->chip_power) {
275 /* We need to set autoincrement mode for 16 bit writes */
276 data[0] |= DAC33_I2C_ADDR_AUTOINC;
277 ret = codec->hw_write(codec->control_data, data, 3);
278 if (ret != 3)
279 dev_err(codec->dev, "Write failed (%d)\n", ret);
280 else
281 ret = 0;
282 }
283
284 return ret;
285}
286
Peter Ujfalusief909d62010-04-30 14:59:33 +0300287static void dac33_init_chip(struct snd_soc_codec *codec)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300288{
Mark Brownb2c812e2010-04-14 15:35:19 +0900289 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300290
Peter Ujfalusief909d62010-04-30 14:59:33 +0300291 if (unlikely(!dac33->chip_power))
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300292 return;
293
Peter Ujfalusief909d62010-04-30 14:59:33 +0300294 /* 44-46: DAC Control Registers */
295 /* A : DAC sample rate Fsref/1.5 */
296 dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(0));
297 /* B : DAC src=normal, not muted */
298 dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT |
299 DAC33_DACSRCL_LEFT);
300 /* C : (defaults) */
301 dac33_write(codec, DAC33_DAC_CTRL_C, 0x00);
302
303 /* 64-65 : L&R DAC power control
304 Line In -> OUT 1V/V Gain, DAC -> OUT 4V/V Gain*/
305 dac33_write(codec, DAC33_LDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
306 dac33_write(codec, DAC33_RDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
307
308 /* 73 : volume soft stepping control,
309 clock source = internal osc (?) */
310 dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);
311
312 /* 66 : LOP/LOM Modes */
313 dac33_write(codec, DAC33_OUT_AMP_CM_CTRL, 0xff);
314
315 /* 68 : LOM inverted from LOP */
316 dac33_write(codec, DAC33_OUT_AMP_CTRL, (3<<2));
317
318 dac33_write(codec, DAC33_PWR_CTRL, DAC33_PDNALLB);
319
320 /* Restore only selected registers (gains mostly) */
321 dac33_write(codec, DAC33_LDAC_DIG_VOL_CTRL,
322 dac33_read_reg_cache(codec, DAC33_LDAC_DIG_VOL_CTRL));
323 dac33_write(codec, DAC33_RDAC_DIG_VOL_CTRL,
324 dac33_read_reg_cache(codec, DAC33_RDAC_DIG_VOL_CTRL));
325
326 dac33_write(codec, DAC33_LINEL_TO_LLO_VOL,
327 dac33_read_reg_cache(codec, DAC33_LINEL_TO_LLO_VOL));
328 dac33_write(codec, DAC33_LINER_TO_RLO_VOL,
329 dac33_read_reg_cache(codec, DAC33_LINER_TO_RLO_VOL));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300330}
331
Peter Ujfalusi239fe552010-04-30 14:59:34 +0300332static inline void dac33_read_id(struct snd_soc_codec *codec)
333{
334 u8 reg;
335
336 dac33_read(codec, DAC33_DEVICE_ID_MSB, &reg);
337 dac33_read(codec, DAC33_DEVICE_ID_LSB, &reg);
338 dac33_read(codec, DAC33_DEVICE_REV_ID, &reg);
339}
340
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300341static inline void dac33_soft_power(struct snd_soc_codec *codec, int power)
342{
343 u8 reg;
344
345 reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
346 if (power)
347 reg |= DAC33_PDNALLB;
348 else
Peter Ujfalusic3746a02010-03-11 16:26:21 +0200349 reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB |
350 DAC33_DACRPDNB | DAC33_DACLPDNB);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300351 dac33_write(codec, DAC33_PWR_CTRL, reg);
352}
353
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200354static int dac33_hard_power(struct snd_soc_codec *codec, int power)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300355{
Mark Brownb2c812e2010-04-14 15:35:19 +0900356 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200357 int ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300358
359 mutex_lock(&dac33->mutex);
360 if (power) {
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200361 ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
362 dac33->supplies);
363 if (ret != 0) {
364 dev_err(codec->dev,
365 "Failed to enable supplies: %d\n", ret);
366 goto exit;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300367 }
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200368
369 if (dac33->power_gpio >= 0)
370 gpio_set_value(dac33->power_gpio, 1);
371
372 dac33->chip_power = 1;
373
Peter Ujfalusief909d62010-04-30 14:59:33 +0300374 dac33_init_chip(codec);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200375
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300376 dac33_soft_power(codec, 1);
377 } else {
378 dac33_soft_power(codec, 0);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200379 if (dac33->power_gpio >= 0)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300380 gpio_set_value(dac33->power_gpio, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300381
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200382 ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies),
383 dac33->supplies);
384 if (ret != 0) {
385 dev_err(codec->dev,
386 "Failed to disable supplies: %d\n", ret);
387 goto exit;
388 }
389
390 dac33->chip_power = 0;
391 }
392
393exit:
394 mutex_unlock(&dac33->mutex);
395 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300396}
397
398static int dac33_get_nsample(struct snd_kcontrol *kcontrol,
399 struct snd_ctl_elem_value *ucontrol)
400{
401 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900402 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300403
404 ucontrol->value.integer.value[0] = dac33->nsample;
405
406 return 0;
407}
408
409static int dac33_set_nsample(struct snd_kcontrol *kcontrol,
410 struct snd_ctl_elem_value *ucontrol)
411{
412 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900413 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300414 int ret = 0;
415
416 if (dac33->nsample == ucontrol->value.integer.value[0])
417 return 0;
418
419 if (ucontrol->value.integer.value[0] < dac33->nsample_min ||
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300420 ucontrol->value.integer.value[0] > dac33->nsample_max) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300421 ret = -EINVAL;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300422 } else {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300423 dac33->nsample = ucontrol->value.integer.value[0];
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300424 /* Re calculate the burst time */
425 dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate,
426 dac33->nsample);
427 }
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300428
429 return ret;
430}
431
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200432static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300433 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
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200438 ucontrol->value.integer.value[0] = dac33->fifo_mode;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300439
440 return 0;
441}
442
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200443static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300444 struct snd_ctl_elem_value *ucontrol)
445{
446 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900447 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300448 int ret = 0;
449
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200450 if (dac33->fifo_mode == ucontrol->value.integer.value[0])
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300451 return 0;
452 /* Do not allow changes while stream is running*/
453 if (codec->active)
454 return -EPERM;
455
456 if (ucontrol->value.integer.value[0] < 0 ||
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200457 ucontrol->value.integer.value[0] >= DAC33_FIFO_LAST_MODE)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300458 ret = -EINVAL;
459 else
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200460 dac33->fifo_mode = ucontrol->value.integer.value[0];
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300461
462 return ret;
463}
464
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200465/* Codec operation modes */
466static const char *dac33_fifo_mode_texts[] = {
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200467 "Bypass", "Mode 1", "Mode 7"
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200468};
469
470static const struct soc_enum dac33_fifo_mode_enum =
471 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(dac33_fifo_mode_texts),
472 dac33_fifo_mode_texts);
473
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300474/*
475 * DACL/R digital volume control:
476 * from 0 dB to -63.5 in 0.5 dB steps
477 * Need to be inverted later on:
478 * 0x00 == 0 dB
479 * 0x7f == -63.5 dB
480 */
481static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0);
482
483static const struct snd_kcontrol_new dac33_snd_controls[] = {
484 SOC_DOUBLE_R_TLV("DAC Digital Playback Volume",
485 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL,
486 0, 0x7f, 1, dac_digivol_tlv),
487 SOC_DOUBLE_R("DAC Digital Playback Switch",
488 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1),
489 SOC_DOUBLE_R("Line to Line Out Volume",
490 DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1),
491};
492
493static const struct snd_kcontrol_new dac33_nsample_snd_controls[] = {
494 SOC_SINGLE_EXT("nSample", 0, 0, 5900, 0,
495 dac33_get_nsample, dac33_set_nsample),
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200496 SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum,
497 dac33_get_fifo_mode, dac33_set_fifo_mode),
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300498};
499
500/* Analog bypass */
501static const struct snd_kcontrol_new dac33_dapm_abypassl_control =
502 SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1);
503
504static const struct snd_kcontrol_new dac33_dapm_abypassr_control =
505 SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1);
506
507static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = {
508 SND_SOC_DAPM_OUTPUT("LEFT_LO"),
509 SND_SOC_DAPM_OUTPUT("RIGHT_LO"),
510
511 SND_SOC_DAPM_INPUT("LINEL"),
512 SND_SOC_DAPM_INPUT("LINER"),
513
514 SND_SOC_DAPM_DAC("DACL", "Left Playback", DAC33_LDAC_PWR_CTRL, 2, 0),
515 SND_SOC_DAPM_DAC("DACR", "Right Playback", DAC33_RDAC_PWR_CTRL, 2, 0),
516
517 /* Analog bypass */
518 SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0,
519 &dac33_dapm_abypassl_control),
520 SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0,
521 &dac33_dapm_abypassr_control),
522
523 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amp Power",
524 DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0),
525 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amp Power",
526 DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0),
527};
528
529static const struct snd_soc_dapm_route audio_map[] = {
530 /* Analog bypass */
531 {"Analog Left Bypass", "Switch", "LINEL"},
532 {"Analog Right Bypass", "Switch", "LINER"},
533
534 {"Output Left Amp Power", NULL, "DACL"},
535 {"Output Right Amp Power", NULL, "DACR"},
536
537 {"Output Left Amp Power", NULL, "Analog Left Bypass"},
538 {"Output Right Amp Power", NULL, "Analog Right Bypass"},
539
540 /* output */
541 {"LEFT_LO", NULL, "Output Left Amp Power"},
542 {"RIGHT_LO", NULL, "Output Right Amp Power"},
543};
544
545static int dac33_add_widgets(struct snd_soc_codec *codec)
546{
547 snd_soc_dapm_new_controls(codec, dac33_dapm_widgets,
548 ARRAY_SIZE(dac33_dapm_widgets));
549
550 /* set up audio path interconnects */
551 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300552
553 return 0;
554}
555
556static int dac33_set_bias_level(struct snd_soc_codec *codec,
557 enum snd_soc_bias_level level)
558{
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200559 int ret;
560
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300561 switch (level) {
562 case SND_SOC_BIAS_ON:
563 dac33_soft_power(codec, 1);
564 break;
565 case SND_SOC_BIAS_PREPARE:
566 break;
567 case SND_SOC_BIAS_STANDBY:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200568 if (codec->bias_level == SND_SOC_BIAS_OFF) {
569 ret = dac33_hard_power(codec, 1);
570 if (ret != 0)
571 return ret;
572 }
573
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300574 dac33_soft_power(codec, 0);
575 break;
576 case SND_SOC_BIAS_OFF:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200577 ret = dac33_hard_power(codec, 0);
578 if (ret != 0)
579 return ret;
580
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300581 break;
582 }
583 codec->bias_level = level;
584
585 return 0;
586}
587
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200588static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33)
589{
590 struct snd_soc_codec *codec;
591
592 codec = &dac33->codec;
593
594 switch (dac33->fifo_mode) {
595 case DAC33_FIFO_MODE1:
596 dac33_write16(codec, DAC33_NSAMPLE_MSB,
Peter Ujfalusif4d59322010-04-23 10:09:57 +0300597 DAC33_THRREG(dac33->nsample + dac33->alarm_threshold));
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300598
599 /* Take the timestamps */
600 spin_lock_irq(&dac33->lock);
601 dac33->t_stamp2 = ktime_to_us(ktime_get());
602 dac33->t_stamp1 = dac33->t_stamp2;
603 spin_unlock_irq(&dac33->lock);
604
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200605 dac33_write16(codec, DAC33_PREFILL_MSB,
606 DAC33_THRREG(dac33->alarm_threshold));
Peter Ujfalusif4d59322010-04-23 10:09:57 +0300607 /* Enable Alarm Threshold IRQ with a delay */
608 udelay(SAMPLES_TO_US(dac33->burst_rate,
609 dac33->alarm_threshold));
610 dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200611 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200612 case DAC33_FIFO_MODE7:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300613 /* Take the timestamp */
614 spin_lock_irq(&dac33->lock);
615 dac33->t_stamp1 = ktime_to_us(ktime_get());
616 /* Move back the timestamp with drain time */
617 dac33->t_stamp1 -= dac33->mode7_us_to_lthr;
618 spin_unlock_irq(&dac33->lock);
619
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200620 dac33_write16(codec, DAC33_PREFILL_MSB,
Peter Ujfalusi42603932010-04-23 10:09:59 +0300621 DAC33_THRREG(MODE7_LTHR));
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300622
623 /* Enable Upper Threshold IRQ */
624 dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MUT);
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200625 break;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200626 default:
627 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
628 dac33->fifo_mode);
629 break;
630 }
631}
632
633static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33)
634{
635 struct snd_soc_codec *codec;
636
637 codec = &dac33->codec;
638
639 switch (dac33->fifo_mode) {
640 case DAC33_FIFO_MODE1:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300641 /* Take the timestamp */
642 spin_lock_irq(&dac33->lock);
643 dac33->t_stamp2 = ktime_to_us(ktime_get());
644 spin_unlock_irq(&dac33->lock);
645
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200646 dac33_write16(codec, DAC33_NSAMPLE_MSB,
647 DAC33_THRREG(dac33->nsample));
648 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200649 case DAC33_FIFO_MODE7:
650 /* At the moment we are not using interrupts in mode7 */
651 break;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200652 default:
653 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
654 dac33->fifo_mode);
655 break;
656 }
657}
658
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300659static void dac33_work(struct work_struct *work)
660{
661 struct snd_soc_codec *codec;
662 struct tlv320dac33_priv *dac33;
663 u8 reg;
664
665 dac33 = container_of(work, struct tlv320dac33_priv, work);
666 codec = &dac33->codec;
667
668 mutex_lock(&dac33->mutex);
669 switch (dac33->state) {
670 case DAC33_PREFILL:
671 dac33->state = DAC33_PLAYBACK;
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200672 dac33_prefill_handler(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300673 break;
674 case DAC33_PLAYBACK:
Peter Ujfalusid4f102d2009-12-31 10:30:20 +0200675 dac33_playback_handler(dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300676 break;
677 case DAC33_IDLE:
678 break;
679 case DAC33_FLUSH:
680 dac33->state = DAC33_IDLE;
681 /* Mask all interrupts from dac33 */
682 dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);
683
684 /* flush fifo */
685 reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
686 reg |= DAC33_FIFOFLUSH;
687 dac33_write(codec, DAC33_FIFO_CTRL_A, reg);
688 break;
689 }
690 mutex_unlock(&dac33->mutex);
691}
692
693static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
694{
695 struct snd_soc_codec *codec = dev;
Mark Brownb2c812e2010-04-14 15:35:19 +0900696 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300697
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300698 spin_lock(&dac33->lock);
699 dac33->t_stamp1 = ktime_to_us(ktime_get());
700 spin_unlock(&dac33->lock);
701
702 /* Do not schedule the workqueue in Mode7 */
703 if (dac33->fifo_mode != DAC33_FIFO_MODE7)
704 queue_work(dac33->dac33_wq, &dac33->work);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300705
706 return IRQ_HANDLED;
707}
708
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300709static void dac33_oscwait(struct snd_soc_codec *codec)
710{
711 int timeout = 20;
712 u8 reg;
713
714 do {
715 msleep(1);
716 dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
717 } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--);
718 if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL)
719 dev_err(codec->dev,
720 "internal oscillator calibration failed\n");
721}
722
723static int dac33_hw_params(struct snd_pcm_substream *substream,
724 struct snd_pcm_hw_params *params,
725 struct snd_soc_dai *dai)
726{
727 struct snd_soc_pcm_runtime *rtd = substream->private_data;
728 struct snd_soc_device *socdev = rtd->socdev;
729 struct snd_soc_codec *codec = socdev->card->codec;
730
731 /* Check parameters for validity */
732 switch (params_rate(params)) {
733 case 44100:
734 case 48000:
735 break;
736 default:
737 dev_err(codec->dev, "unsupported rate %d\n",
738 params_rate(params));
739 return -EINVAL;
740 }
741
742 switch (params_format(params)) {
743 case SNDRV_PCM_FORMAT_S16_LE:
744 break;
745 default:
746 dev_err(codec->dev, "unsupported format %d\n",
747 params_format(params));
748 return -EINVAL;
749 }
750
751 return 0;
752}
753
754#define CALC_OSCSET(rate, refclk) ( \
Peter Ujfalusi7833ae02010-02-16 13:23:16 +0200755 ((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300756#define CALC_RATIOSET(rate, refclk) ( \
757 ((((refclk * 100000) / rate) * 16384) + 50000) / 100000)
758
759/*
760 * tlv320dac33 is strict on the sequence of the register writes, if the register
761 * writes happens in different order, than dac33 might end up in unknown state.
762 * Use the known, working sequence of register writes to initialize the dac33.
763 */
764static int dac33_prepare_chip(struct snd_pcm_substream *substream)
765{
766 struct snd_soc_pcm_runtime *rtd = substream->private_data;
767 struct snd_soc_device *socdev = rtd->socdev;
768 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900769 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300770 unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200771 u8 aictrl_a, aictrl_b, fifoctrl_a;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300772
773 switch (substream->runtime->rate) {
774 case 44100:
775 case 48000:
776 oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk);
777 ratioset = CALC_RATIOSET(substream->runtime->rate,
778 dac33->refclk);
779 break;
780 default:
781 dev_err(codec->dev, "unsupported rate %d\n",
782 substream->runtime->rate);
783 return -EINVAL;
784 }
785
786
787 aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
788 aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK);
Peter Ujfalusie5e878c2010-02-16 13:23:15 +0200789 /* Read FIFO control A, and clear FIFO flush bit */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300790 fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
Peter Ujfalusie5e878c2010-02-16 13:23:15 +0200791 fifoctrl_a &= ~DAC33_FIFOFLUSH;
792
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300793 fifoctrl_a &= ~DAC33_WIDTH;
794 switch (substream->runtime->format) {
795 case SNDRV_PCM_FORMAT_S16_LE:
796 aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16);
797 fifoctrl_a |= DAC33_WIDTH;
798 break;
799 default:
800 dev_err(codec->dev, "unsupported format %d\n",
801 substream->runtime->format);
802 return -EINVAL;
803 }
804
805 mutex_lock(&dac33->mutex);
Peter Ujfalusic3746a02010-03-11 16:26:21 +0200806 dac33_soft_power(codec, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300807 dac33_soft_power(codec, 1);
808
809 reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
810 dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp);
811
812 /* Write registers 0x08 and 0x09 (MSB, LSB) */
813 dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset);
814
815 /* calib time: 128 is a nice number ;) */
816 dac33_write(codec, DAC33_CALIB_TIME, 128);
817
818 /* adjustment treshold & step */
819 dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) |
820 DAC33_ADJSTEP(1));
821
822 /* div=4 / gain=1 / div */
823 dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4));
824
825 pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
826 pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB;
827 dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);
828
829 dac33_oscwait(codec);
830
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200831 if (dac33->fifo_mode) {
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200832 /* Generic for all FIFO modes */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300833 /* 50-51 : ASRC Control registers */
Peter Ujfalusifdb6b1e2010-03-19 11:10:20 +0200834 dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300835 dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */
836
837 /* Write registers 0x34 and 0x35 (MSB, LSB) */
838 dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset);
839
840 /* Set interrupts to high active */
841 dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300842 } else {
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200843 /* FIFO bypass mode */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300844 /* 50-51 : ASRC Control registers */
845 dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP);
846 dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */
847 }
848
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200849 /* Interrupt behaviour configuration */
850 switch (dac33->fifo_mode) {
851 case DAC33_FIFO_MODE1:
852 dac33_write(codec, DAC33_FIFO_IRQ_MODE_B,
853 DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL));
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200854 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200855 case DAC33_FIFO_MODE7:
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300856 dac33_write(codec, DAC33_FIFO_IRQ_MODE_A,
857 DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL));
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200858 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200859 default:
860 /* in FIFO bypass mode, the interrupts are not used */
861 break;
862 }
863
864 aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
865
866 switch (dac33->fifo_mode) {
867 case DAC33_FIFO_MODE1:
868 /*
869 * For mode1:
870 * Disable the FIFO bypass (Enable the use of FIFO)
871 * Select nSample mode
872 * BCLK is only running when data is needed by DAC33
873 */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300874 fifoctrl_a &= ~DAC33_FBYPAS;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200875 fifoctrl_a &= ~DAC33_FAUTO;
Peter Ujfalusieeb309a2010-03-11 16:26:22 +0200876 if (dac33->keep_bclk)
877 aictrl_b |= DAC33_BCLKON;
878 else
879 aictrl_b &= ~DAC33_BCLKON;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200880 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200881 case DAC33_FIFO_MODE7:
882 /*
883 * For mode1:
884 * Disable the FIFO bypass (Enable the use of FIFO)
885 * Select Threshold mode
886 * BCLK is only running when data is needed by DAC33
887 */
888 fifoctrl_a &= ~DAC33_FBYPAS;
889 fifoctrl_a |= DAC33_FAUTO;
Peter Ujfalusieeb309a2010-03-11 16:26:22 +0200890 if (dac33->keep_bclk)
891 aictrl_b |= DAC33_BCLKON;
892 else
893 aictrl_b &= ~DAC33_BCLKON;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200894 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200895 default:
896 /*
897 * For FIFO bypass mode:
898 * Enable the FIFO bypass (Disable the FIFO use)
899 * Set the BCLK as continous
900 */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300901 fifoctrl_a |= DAC33_FBYPAS;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200902 aictrl_b |= DAC33_BCLKON;
903 break;
904 }
905
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300906 dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300907 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200908 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300909
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +0200910 /*
911 * BCLK divide ratio
912 * 0: 1.5
913 * 1: 1
914 * 2: 2
915 * ...
916 * 254: 254
917 * 255: 255
918 */
Peter Ujfalusi6cd6ced2010-01-20 09:39:35 +0200919 if (dac33->fifo_mode)
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +0200920 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C,
921 dac33->burst_bclkdiv);
Peter Ujfalusi6cd6ced2010-01-20 09:39:35 +0200922 else
923 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
924
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200925 switch (dac33->fifo_mode) {
926 case DAC33_FIFO_MODE1:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300927 dac33_write16(codec, DAC33_ATHR_MSB,
928 DAC33_THRREG(dac33->alarm_threshold));
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200929 break;
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200930 case DAC33_FIFO_MODE7:
931 /*
932 * Configure the threshold levels, and leave 10 sample space
933 * at the bottom, and also at the top of the FIFO
934 */
Peter Ujfalusi42603932010-04-23 10:09:59 +0300935 dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(MODE7_UTHR));
936 dac33_write16(codec, DAC33_LTHR_MSB, DAC33_THRREG(MODE7_LTHR));
Peter Ujfalusi28e05d92009-12-31 10:30:22 +0200937 break;
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200938 default:
Peter Ujfalusiaec242d2009-12-31 10:30:21 +0200939 break;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300940 }
941
942 mutex_unlock(&dac33->mutex);
943
944 return 0;
945}
946
947static void dac33_calculate_times(struct snd_pcm_substream *substream)
948{
949 struct snd_soc_pcm_runtime *rtd = substream->private_data;
950 struct snd_soc_device *socdev = rtd->socdev;
951 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900952 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300953 unsigned int nsample_limit;
954
Peter Ujfalusi55abb592010-04-23 10:09:58 +0300955 /* In bypass mode we don't need to calculate */
956 if (!dac33->fifo_mode)
957 return;
958
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300959 /* Number of samples (16bit, stereo) in one period */
960 dac33->nsample_min = snd_pcm_lib_period_bytes(substream) / 4;
961
962 /* Number of samples (16bit, stereo) in ALSA buffer */
963 dac33->nsample_max = snd_pcm_lib_buffer_bytes(substream) / 4;
964 /* Subtract one period from the total */
965 dac33->nsample_max -= dac33->nsample_min;
966
967 /* Number of samples for LATENCY_TIME_MS / 2 */
968 dac33->alarm_threshold = substream->runtime->rate /
969 (1000 / (LATENCY_TIME_MS / 2));
970
971 /* Find and fix up the lowest nsmaple limit */
972 nsample_limit = substream->runtime->rate / (1000 / LATENCY_TIME_MS);
973
974 if (dac33->nsample_min < nsample_limit)
975 dac33->nsample_min = nsample_limit;
976
977 if (dac33->nsample < dac33->nsample_min)
978 dac33->nsample = dac33->nsample_min;
979
980 /*
981 * Find and fix up the highest nsmaple limit
982 * In order to not overflow the DAC33 buffer substract the
983 * alarm_threshold value from the size of the DAC33 buffer
984 */
985 nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - dac33->alarm_threshold;
986
987 if (dac33->nsample_max > nsample_limit)
988 dac33->nsample_max = nsample_limit;
989
990 if (dac33->nsample > dac33->nsample_max)
991 dac33->nsample = dac33->nsample_max;
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +0300992
993 switch (dac33->fifo_mode) {
994 case DAC33_FIFO_MODE1:
995 dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate,
996 dac33->nsample);
997 dac33->t_stamp1 = 0;
998 dac33->t_stamp2 = 0;
999 break;
1000 case DAC33_FIFO_MODE7:
1001 dac33->mode7_us_to_lthr =
1002 SAMPLES_TO_US(substream->runtime->rate,
1003 MODE7_UTHR - MODE7_LTHR + 1);
1004 dac33->t_stamp1 = 0;
1005 break;
1006 default:
1007 break;
1008 }
1009
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001010}
1011
1012static int dac33_pcm_prepare(struct snd_pcm_substream *substream,
1013 struct snd_soc_dai *dai)
1014{
1015 dac33_calculate_times(substream);
1016 dac33_prepare_chip(substream);
1017
1018 return 0;
1019}
1020
1021static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
1022 struct snd_soc_dai *dai)
1023{
1024 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1025 struct snd_soc_device *socdev = rtd->socdev;
1026 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001027 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001028 int ret = 0;
1029
1030 switch (cmd) {
1031 case SNDRV_PCM_TRIGGER_START:
1032 case SNDRV_PCM_TRIGGER_RESUME:
1033 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +02001034 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001035 dac33->state = DAC33_PREFILL;
1036 queue_work(dac33->dac33_wq, &dac33->work);
1037 }
1038 break;
1039 case SNDRV_PCM_TRIGGER_STOP:
1040 case SNDRV_PCM_TRIGGER_SUSPEND:
1041 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +02001042 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001043 dac33->state = DAC33_FLUSH;
1044 queue_work(dac33->dac33_wq, &dac33->work);
1045 }
1046 break;
1047 default:
1048 ret = -EINVAL;
1049 }
1050
1051 return ret;
1052}
1053
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001054static snd_pcm_sframes_t dac33_dai_delay(
1055 struct snd_pcm_substream *substream,
1056 struct snd_soc_dai *dai)
1057{
1058 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1059 struct snd_soc_device *socdev = rtd->socdev;
1060 struct snd_soc_codec *codec = socdev->card->codec;
1061 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1062 unsigned long long t0, t1, t_now;
1063 unsigned int time_delta;
1064 int samples_out, samples_in, samples;
1065 snd_pcm_sframes_t delay = 0;
1066
1067 switch (dac33->fifo_mode) {
1068 case DAC33_FIFO_BYPASS:
1069 break;
1070 case DAC33_FIFO_MODE1:
1071 spin_lock(&dac33->lock);
1072 t0 = dac33->t_stamp1;
1073 t1 = dac33->t_stamp2;
1074 spin_unlock(&dac33->lock);
1075 t_now = ktime_to_us(ktime_get());
1076
1077 /* We have not started to fill the FIFO yet, delay is 0 */
1078 if (!t1)
1079 goto out;
1080
1081 if (t0 > t1) {
1082 /*
1083 * Phase 1:
1084 * After Alarm threshold, and before nSample write
1085 */
1086 time_delta = t_now - t0;
1087 samples_out = time_delta ? US_TO_SAMPLES(
1088 substream->runtime->rate,
1089 time_delta) : 0;
1090
1091 if (likely(dac33->alarm_threshold > samples_out))
1092 delay = dac33->alarm_threshold - samples_out;
1093 else
1094 delay = 0;
1095 } else if ((t_now - t1) <= dac33->mode1_us_burst) {
1096 /*
1097 * Phase 2:
1098 * After nSample write (during burst operation)
1099 */
1100 time_delta = t_now - t0;
1101 samples_out = time_delta ? US_TO_SAMPLES(
1102 substream->runtime->rate,
1103 time_delta) : 0;
1104
1105 time_delta = t_now - t1;
1106 samples_in = time_delta ? US_TO_SAMPLES(
1107 dac33->burst_rate,
1108 time_delta) : 0;
1109
1110 samples = dac33->alarm_threshold;
1111 samples += (samples_in - samples_out);
1112
1113 if (likely(samples > 0))
1114 delay = samples;
1115 else
1116 delay = 0;
1117 } else {
1118 /*
1119 * Phase 3:
1120 * After burst operation, before next alarm threshold
1121 */
1122 time_delta = t_now - t0;
1123 samples_out = time_delta ? US_TO_SAMPLES(
1124 substream->runtime->rate,
1125 time_delta) : 0;
1126
1127 samples_in = dac33->nsample;
1128 samples = dac33->alarm_threshold;
1129 samples += (samples_in - samples_out);
1130
1131 if (likely(samples > 0))
1132 delay = samples > DAC33_BUFFER_SIZE_SAMPLES ?
1133 DAC33_BUFFER_SIZE_SAMPLES : samples;
1134 else
1135 delay = 0;
1136 }
1137 break;
1138 case DAC33_FIFO_MODE7:
1139 spin_lock(&dac33->lock);
1140 t0 = dac33->t_stamp1;
1141 spin_unlock(&dac33->lock);
1142 t_now = ktime_to_us(ktime_get());
1143
1144 /* We have not started to fill the FIFO yet, delay is 0 */
1145 if (!t0)
1146 goto out;
1147
1148 if (t_now <= t0) {
1149 /*
1150 * Either the timestamps are messed or equal. Report
1151 * maximum delay
1152 */
1153 delay = MODE7_UTHR;
1154 goto out;
1155 }
1156
1157 time_delta = t_now - t0;
1158 if (time_delta <= dac33->mode7_us_to_lthr) {
1159 /*
1160 * Phase 1:
1161 * After burst (draining phase)
1162 */
1163 samples_out = US_TO_SAMPLES(
1164 substream->runtime->rate,
1165 time_delta);
1166
1167 if (likely(MODE7_UTHR > samples_out))
1168 delay = MODE7_UTHR - samples_out;
1169 else
1170 delay = 0;
1171 } else {
1172 /*
1173 * Phase 2:
1174 * During burst operation
1175 */
1176 time_delta = time_delta - dac33->mode7_us_to_lthr;
1177
1178 samples_out = US_TO_SAMPLES(
1179 substream->runtime->rate,
1180 time_delta);
1181 samples_in = US_TO_SAMPLES(
1182 dac33->burst_rate,
1183 time_delta);
1184 delay = MODE7_LTHR + samples_in - samples_out;
1185
1186 if (unlikely(delay > MODE7_UTHR))
1187 delay = MODE7_UTHR;
1188 }
1189 break;
1190 default:
1191 dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
1192 dac33->fifo_mode);
1193 break;
1194 }
1195out:
1196 return delay;
1197}
1198
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001199static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1200 int clk_id, unsigned int freq, int dir)
1201{
1202 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001203 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001204 u8 ioc_reg, asrcb_reg;
1205
1206 ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
1207 asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B);
1208 switch (clk_id) {
1209 case TLV320DAC33_MCLK:
1210 ioc_reg |= DAC33_REFSEL;
1211 asrcb_reg |= DAC33_SRCREFSEL;
1212 break;
1213 case TLV320DAC33_SLEEPCLK:
1214 ioc_reg &= ~DAC33_REFSEL;
1215 asrcb_reg &= ~DAC33_SRCREFSEL;
1216 break;
1217 default:
1218 dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id);
1219 break;
1220 }
1221 dac33->refclk = freq;
1222
1223 dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg);
1224 dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg);
1225
1226 return 0;
1227}
1228
1229static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai,
1230 unsigned int fmt)
1231{
1232 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001233 struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001234 u8 aictrl_a, aictrl_b;
1235
1236 aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
1237 aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
1238 /* set master/slave audio interface */
1239 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1240 case SND_SOC_DAIFMT_CBM_CFM:
1241 /* Codec Master */
1242 aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK);
1243 break;
1244 case SND_SOC_DAIFMT_CBS_CFS:
1245 /* Codec Slave */
Peter Ujfalusiadcb8bc2009-12-31 10:30:23 +02001246 if (dac33->fifo_mode) {
1247 dev_err(codec->dev, "FIFO mode requires master mode\n");
1248 return -EINVAL;
1249 } else
1250 aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001251 break;
1252 default:
1253 return -EINVAL;
1254 }
1255
1256 aictrl_a &= ~DAC33_AFMT_MASK;
1257 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1258 case SND_SOC_DAIFMT_I2S:
1259 aictrl_a |= DAC33_AFMT_I2S;
1260 break;
1261 case SND_SOC_DAIFMT_DSP_A:
1262 aictrl_a |= DAC33_AFMT_DSP;
1263 aictrl_b &= ~DAC33_DATA_DELAY_MASK;
Peter Ujfalusi44f497b2010-03-19 11:10:19 +02001264 aictrl_b |= DAC33_DATA_DELAY(0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001265 break;
1266 case SND_SOC_DAIFMT_RIGHT_J:
1267 aictrl_a |= DAC33_AFMT_RIGHT_J;
1268 break;
1269 case SND_SOC_DAIFMT_LEFT_J:
1270 aictrl_a |= DAC33_AFMT_LEFT_J;
1271 break;
1272 default:
1273 dev_err(codec->dev, "Unsupported format (%u)\n",
1274 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1275 return -EINVAL;
1276 }
1277
1278 dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
1279 dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
1280
1281 return 0;
1282}
1283
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001284static int dac33_soc_probe(struct platform_device *pdev)
1285{
1286 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1287 struct snd_soc_codec *codec;
1288 struct tlv320dac33_priv *dac33;
1289 int ret = 0;
1290
1291 BUG_ON(!tlv320dac33_codec);
1292
1293 codec = tlv320dac33_codec;
1294 socdev->card->codec = codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001295 dac33 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001296
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001297 /* register pcms */
1298 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1299 if (ret < 0) {
1300 dev_err(codec->dev, "failed to create pcms\n");
1301 goto pcm_err;
1302 }
1303
1304 snd_soc_add_controls(codec, dac33_snd_controls,
1305 ARRAY_SIZE(dac33_snd_controls));
1306 /* Only add the nSample controls, if we have valid IRQ number */
1307 if (dac33->irq >= 0)
1308 snd_soc_add_controls(codec, dac33_nsample_snd_controls,
1309 ARRAY_SIZE(dac33_nsample_snd_controls));
1310
1311 dac33_add_widgets(codec);
1312
1313 /* power on device */
1314 dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1315
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001316 return 0;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001317
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001318pcm_err:
1319 dac33_hard_power(codec, 0);
1320 return ret;
1321}
1322
1323static int dac33_soc_remove(struct platform_device *pdev)
1324{
1325 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1326 struct snd_soc_codec *codec = socdev->card->codec;
1327
1328 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1329
1330 snd_soc_free_pcms(socdev);
1331 snd_soc_dapm_free(socdev);
1332
1333 return 0;
1334}
1335
1336static int dac33_soc_suspend(struct platform_device *pdev, pm_message_t state)
1337{
1338 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1339 struct snd_soc_codec *codec = socdev->card->codec;
1340
1341 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1342
1343 return 0;
1344}
1345
1346static int dac33_soc_resume(struct platform_device *pdev)
1347{
1348 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1349 struct snd_soc_codec *codec = socdev->card->codec;
1350
1351 dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1352 dac33_set_bias_level(codec, codec->suspend_bias_level);
1353
1354 return 0;
1355}
1356
1357struct snd_soc_codec_device soc_codec_dev_tlv320dac33 = {
1358 .probe = dac33_soc_probe,
1359 .remove = dac33_soc_remove,
1360 .suspend = dac33_soc_suspend,
1361 .resume = dac33_soc_resume,
1362};
1363EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320dac33);
1364
1365#define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
1366 SNDRV_PCM_RATE_48000)
1367#define DAC33_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1368
1369static struct snd_soc_dai_ops dac33_dai_ops = {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001370 .hw_params = dac33_hw_params,
1371 .prepare = dac33_pcm_prepare,
1372 .trigger = dac33_pcm_trigger,
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001373 .delay = dac33_dai_delay,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001374 .set_sysclk = dac33_set_dai_sysclk,
1375 .set_fmt = dac33_set_dai_fmt,
1376};
1377
1378struct snd_soc_dai dac33_dai = {
1379 .name = "tlv320dac33",
1380 .playback = {
1381 .stream_name = "Playback",
1382 .channels_min = 2,
1383 .channels_max = 2,
1384 .rates = DAC33_RATES,
1385 .formats = DAC33_FORMATS,},
1386 .ops = &dac33_dai_ops,
1387};
1388EXPORT_SYMBOL_GPL(dac33_dai);
1389
Mark Brown735fe4c2010-01-12 14:13:00 +00001390static int __devinit dac33_i2c_probe(struct i2c_client *client,
1391 const struct i2c_device_id *id)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001392{
1393 struct tlv320dac33_platform_data *pdata;
1394 struct tlv320dac33_priv *dac33;
1395 struct snd_soc_codec *codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001396 int ret, i;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001397
1398 if (client->dev.platform_data == NULL) {
1399 dev_err(&client->dev, "Platform data not set\n");
1400 return -ENODEV;
1401 }
1402 pdata = client->dev.platform_data;
1403
1404 dac33 = kzalloc(sizeof(struct tlv320dac33_priv), GFP_KERNEL);
1405 if (dac33 == NULL)
1406 return -ENOMEM;
1407
1408 codec = &dac33->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001409 snd_soc_codec_set_drvdata(codec, dac33);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001410 codec->control_data = client;
1411
1412 mutex_init(&codec->mutex);
1413 mutex_init(&dac33->mutex);
Peter Ujfalusif57d2cf2010-04-23 10:10:01 +03001414 spin_lock_init(&dac33->lock);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001415 INIT_LIST_HEAD(&codec->dapm_widgets);
1416 INIT_LIST_HEAD(&codec->dapm_paths);
1417
1418 codec->name = "tlv320dac33";
1419 codec->owner = THIS_MODULE;
1420 codec->read = dac33_read_reg_cache;
1421 codec->write = dac33_write_locked;
1422 codec->hw_write = (hw_write_t) i2c_master_send;
1423 codec->bias_level = SND_SOC_BIAS_OFF;
1424 codec->set_bias_level = dac33_set_bias_level;
1425 codec->dai = &dac33_dai;
1426 codec->num_dai = 1;
1427 codec->reg_cache_size = ARRAY_SIZE(dac33_reg);
1428 codec->reg_cache = kmemdup(dac33_reg, ARRAY_SIZE(dac33_reg),
1429 GFP_KERNEL);
1430 if (codec->reg_cache == NULL) {
1431 ret = -ENOMEM;
1432 goto error_reg;
1433 }
1434
1435 i2c_set_clientdata(client, dac33);
1436
1437 dac33->power_gpio = pdata->power_gpio;
Peter Ujfalusi6aceabb2010-01-20 09:39:36 +02001438 dac33->burst_bclkdiv = pdata->burst_bclkdiv;
Peter Ujfalusi76f471272010-04-23 10:10:00 +03001439 /* Pre calculate the burst rate */
1440 dac33->burst_rate = BURST_BASEFREQ_HZ / dac33->burst_bclkdiv / 32;
Peter Ujfalusieeb309a2010-03-11 16:26:22 +02001441 dac33->keep_bclk = pdata->keep_bclk;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001442 dac33->irq = client->irq;
1443 dac33->nsample = NSAMPLE_MAX;
Peter Ujfalusi55abb592010-04-23 10:09:58 +03001444 dac33->nsample_max = NSAMPLE_MAX;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001445 /* Disable FIFO use by default */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +02001446 dac33->fifo_mode = DAC33_FIFO_BYPASS;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001447
1448 tlv320dac33_codec = codec;
1449
1450 codec->dev = &client->dev;
1451 dac33_dai.dev = codec->dev;
1452
1453 /* Check if the reset GPIO number is valid and request it */
1454 if (dac33->power_gpio >= 0) {
1455 ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset");
1456 if (ret < 0) {
1457 dev_err(codec->dev,
1458 "Failed to request reset GPIO (%d)\n",
1459 dac33->power_gpio);
1460 snd_soc_unregister_dai(&dac33_dai);
1461 snd_soc_unregister_codec(codec);
1462 goto error_gpio;
1463 }
1464 gpio_direction_output(dac33->power_gpio, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001465 }
1466
1467 /* Check if the IRQ number is valid and request it */
1468 if (dac33->irq >= 0) {
1469 ret = request_irq(dac33->irq, dac33_interrupt_handler,
1470 IRQF_TRIGGER_RISING | IRQF_DISABLED,
1471 codec->name, codec);
1472 if (ret < 0) {
1473 dev_err(codec->dev, "Could not request IRQ%d (%d)\n",
1474 dac33->irq, ret);
1475 dac33->irq = -1;
1476 }
1477 if (dac33->irq != -1) {
1478 /* Setup work queue */
Peter Ujfalusi74ea23a2009-11-26 13:55:11 +02001479 dac33->dac33_wq =
1480 create_singlethread_workqueue("tlv320dac33");
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001481 if (dac33->dac33_wq == NULL) {
1482 free_irq(dac33->irq, &dac33->codec);
1483 ret = -ENOMEM;
1484 goto error_wq;
1485 }
1486
1487 INIT_WORK(&dac33->work, dac33_work);
1488 }
1489 }
1490
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001491 for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
1492 dac33->supplies[i].supply = dac33_supply_names[i];
1493
1494 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(dac33->supplies),
1495 dac33->supplies);
1496
1497 if (ret != 0) {
1498 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
1499 goto err_get;
1500 }
1501
Peter Ujfalusi239fe552010-04-30 14:59:34 +03001502 /* Read the tlv320dac33 ID registers */
1503 ret = dac33_hard_power(codec, 1);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001504 if (ret != 0) {
Peter Ujfalusi239fe552010-04-30 14:59:34 +03001505 dev_err(codec->dev, "Failed to power up codec: %d\n", ret);
1506 goto error_codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001507 }
Peter Ujfalusi239fe552010-04-30 14:59:34 +03001508 dac33_read_id(codec);
1509 dac33_hard_power(codec, 0);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001510
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001511 ret = snd_soc_register_codec(codec);
1512 if (ret != 0) {
1513 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1514 goto error_codec;
1515 }
1516
1517 ret = snd_soc_register_dai(&dac33_dai);
1518 if (ret != 0) {
1519 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
1520 snd_soc_unregister_codec(codec);
1521 goto error_codec;
1522 }
1523
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001524 return ret;
1525
1526error_codec:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001527 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1528err_get:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001529 if (dac33->irq >= 0) {
1530 free_irq(dac33->irq, &dac33->codec);
1531 destroy_workqueue(dac33->dac33_wq);
1532 }
1533error_wq:
1534 if (dac33->power_gpio >= 0)
1535 gpio_free(dac33->power_gpio);
1536error_gpio:
1537 kfree(codec->reg_cache);
1538error_reg:
1539 tlv320dac33_codec = NULL;
1540 kfree(dac33);
1541
1542 return ret;
1543}
1544
Mark Brown735fe4c2010-01-12 14:13:00 +00001545static int __devexit dac33_i2c_remove(struct i2c_client *client)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001546{
1547 struct tlv320dac33_priv *dac33;
1548
1549 dac33 = i2c_get_clientdata(client);
Peter Ujfalusi239fe552010-04-30 14:59:34 +03001550
1551 if (unlikely(dac33->chip_power))
1552 dac33_hard_power(&dac33->codec, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001553
1554 if (dac33->power_gpio >= 0)
1555 gpio_free(dac33->power_gpio);
1556 if (dac33->irq >= 0)
1557 free_irq(dac33->irq, &dac33->codec);
1558
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001559 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1560
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001561 destroy_workqueue(dac33->dac33_wq);
1562 snd_soc_unregister_dai(&dac33_dai);
1563 snd_soc_unregister_codec(&dac33->codec);
1564 kfree(dac33->codec.reg_cache);
1565 kfree(dac33);
1566 tlv320dac33_codec = NULL;
1567
1568 return 0;
1569}
1570
1571static const struct i2c_device_id tlv320dac33_i2c_id[] = {
1572 {
1573 .name = "tlv320dac33",
1574 .driver_data = 0,
1575 },
1576 { },
1577};
1578
1579static struct i2c_driver tlv320dac33_i2c_driver = {
1580 .driver = {
1581 .name = "tlv320dac33",
1582 .owner = THIS_MODULE,
1583 },
1584 .probe = dac33_i2c_probe,
1585 .remove = __devexit_p(dac33_i2c_remove),
1586 .id_table = tlv320dac33_i2c_id,
1587};
1588
1589static int __init dac33_module_init(void)
1590{
1591 int r;
1592 r = i2c_add_driver(&tlv320dac33_i2c_driver);
1593 if (r < 0) {
1594 printk(KERN_ERR "DAC33: driver registration failed\n");
1595 return r;
1596 }
1597 return 0;
1598}
1599module_init(dac33_module_init);
1600
1601static void __exit dac33_module_exit(void)
1602{
1603 i2c_del_driver(&tlv320dac33_i2c_driver);
1604}
1605module_exit(dac33_module_exit);
1606
1607
1608MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
1609MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1610MODULE_LICENSE("GPL");