blob: b67961dd2a128bbdf4e3c4dc33054ebd77f696d9 [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
53static struct snd_soc_codec *tlv320dac33_codec;
54
55enum dac33_state {
56 DAC33_IDLE = 0,
57 DAC33_PREFILL,
58 DAC33_PLAYBACK,
59 DAC33_FLUSH,
60};
61
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020062enum dac33_fifo_modes {
63 DAC33_FIFO_BYPASS = 0,
64 DAC33_FIFO_MODE1,
65 DAC33_FIFO_LAST_MODE,
66};
67
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020068#define DAC33_NUM_SUPPLIES 3
69static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = {
70 "AVDD",
71 "DVDD",
72 "IOVDD",
73};
74
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030075struct tlv320dac33_priv {
76 struct mutex mutex;
77 struct workqueue_struct *dac33_wq;
78 struct work_struct work;
79 struct snd_soc_codec codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +020080 struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES];
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030081 int power_gpio;
82 int chip_power;
83 int irq;
84 unsigned int refclk;
85
86 unsigned int alarm_threshold; /* set to be half of LATENCY_TIME_MS */
87 unsigned int nsample_min; /* nsample should not be lower than
88 * this */
89 unsigned int nsample_max; /* nsample should not be higher than
90 * this */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +020091 enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +030092 unsigned int nsample; /* burst read amount from host */
93
94 enum dac33_state state;
95};
96
97static const u8 dac33_reg[DAC33_CACHEREGNUM] = {
980x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
990x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
1000x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
1010x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
1020x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
1030x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
1040x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
1050x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
1060x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
1070x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
1080x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
1090x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
1100x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
1110x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
1120x00, 0x00, /* 0x38 - 0x39 */
113/* Registers 0x3a - 0x3f are reserved */
114 0x00, 0x00, /* 0x3a - 0x3b */
1150x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */
116
1170x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
1180x00, 0x80, /* 0x44 - 0x45 */
119/* Registers 0x46 - 0x47 are reserved */
120 0x80, 0x80, /* 0x46 - 0x47 */
121
1220x80, 0x00, 0x00, /* 0x48 - 0x4a */
123/* Registers 0x4b - 0x7c are reserved */
124 0x00, /* 0x4b */
1250x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
1260x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
1270x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
1280x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
1290x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
1300x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
1310x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
1320x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
1330x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
1340x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
1350x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
1360x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
1370x00, /* 0x7c */
138
139 0xda, 0x33, 0x03, /* 0x7d - 0x7f */
140};
141
142/* Register read and write */
143static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec *codec,
144 unsigned reg)
145{
146 u8 *cache = codec->reg_cache;
147 if (reg >= DAC33_CACHEREGNUM)
148 return 0;
149
150 return cache[reg];
151}
152
153static inline void dac33_write_reg_cache(struct snd_soc_codec *codec,
154 u8 reg, u8 value)
155{
156 u8 *cache = codec->reg_cache;
157 if (reg >= DAC33_CACHEREGNUM)
158 return;
159
160 cache[reg] = value;
161}
162
163static int dac33_read(struct snd_soc_codec *codec, unsigned int reg,
164 u8 *value)
165{
166 struct tlv320dac33_priv *dac33 = codec->private_data;
167 int val;
168
169 *value = reg & 0xff;
170
171 /* If powered off, return the cached value */
172 if (dac33->chip_power) {
173 val = i2c_smbus_read_byte_data(codec->control_data, value[0]);
174 if (val < 0) {
175 dev_err(codec->dev, "Read failed (%d)\n", val);
176 value[0] = dac33_read_reg_cache(codec, reg);
177 } else {
178 value[0] = val;
179 dac33_write_reg_cache(codec, reg, val);
180 }
181 } else {
182 value[0] = dac33_read_reg_cache(codec, reg);
183 }
184
185 return 0;
186}
187
188static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
189 unsigned int value)
190{
191 struct tlv320dac33_priv *dac33 = codec->private_data;
192 u8 data[2];
193 int ret = 0;
194
195 /*
196 * data is
197 * D15..D8 dac33 register offset
198 * D7...D0 register data
199 */
200 data[0] = reg & 0xff;
201 data[1] = value & 0xff;
202
203 dac33_write_reg_cache(codec, data[0], data[1]);
204 if (dac33->chip_power) {
205 ret = codec->hw_write(codec->control_data, data, 2);
206 if (ret != 2)
207 dev_err(codec->dev, "Write failed (%d)\n", ret);
208 else
209 ret = 0;
210 }
211
212 return ret;
213}
214
215static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
216 unsigned int value)
217{
218 struct tlv320dac33_priv *dac33 = codec->private_data;
219 int ret;
220
221 mutex_lock(&dac33->mutex);
222 ret = dac33_write(codec, reg, value);
223 mutex_unlock(&dac33->mutex);
224
225 return ret;
226}
227
228#define DAC33_I2C_ADDR_AUTOINC 0x80
229static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg,
230 unsigned int value)
231{
232 struct tlv320dac33_priv *dac33 = codec->private_data;
233 u8 data[3];
234 int ret = 0;
235
236 /*
237 * data is
238 * D23..D16 dac33 register offset
239 * D15..D8 register data MSB
240 * D7...D0 register data LSB
241 */
242 data[0] = reg & 0xff;
243 data[1] = (value >> 8) & 0xff;
244 data[2] = value & 0xff;
245
246 dac33_write_reg_cache(codec, data[0], data[1]);
247 dac33_write_reg_cache(codec, data[0] + 1, data[2]);
248
249 if (dac33->chip_power) {
250 /* We need to set autoincrement mode for 16 bit writes */
251 data[0] |= DAC33_I2C_ADDR_AUTOINC;
252 ret = codec->hw_write(codec->control_data, data, 3);
253 if (ret != 3)
254 dev_err(codec->dev, "Write failed (%d)\n", ret);
255 else
256 ret = 0;
257 }
258
259 return ret;
260}
261
262static void dac33_restore_regs(struct snd_soc_codec *codec)
263{
264 struct tlv320dac33_priv *dac33 = codec->private_data;
265 u8 *cache = codec->reg_cache;
266 u8 data[2];
267 int i, ret;
268
269 if (!dac33->chip_power)
270 return;
271
272 for (i = DAC33_PWR_CTRL; i <= DAC33_INTP_CTRL_B; i++) {
273 data[0] = i;
274 data[1] = cache[i];
275 /* Skip the read only registers */
276 if ((i >= DAC33_INT_OSC_STATUS &&
277 i <= DAC33_INT_OSC_FREQ_RAT_READ_B) ||
278 (i >= DAC33_FIFO_WPTR_MSB && i <= DAC33_FIFO_IRQ_FLAG) ||
279 i == DAC33_DAC_STATUS_FLAGS ||
280 i == DAC33_SRC_EST_REF_CLK_RATIO_A ||
281 i == DAC33_SRC_EST_REF_CLK_RATIO_B)
282 continue;
283 ret = codec->hw_write(codec->control_data, data, 2);
284 if (ret != 2)
285 dev_err(codec->dev, "Write failed (%d)\n", ret);
286 }
287 for (i = DAC33_LDAC_PWR_CTRL; i <= DAC33_LINEL_TO_LLO_VOL; i++) {
288 data[0] = i;
289 data[1] = cache[i];
290 ret = codec->hw_write(codec->control_data, data, 2);
291 if (ret != 2)
292 dev_err(codec->dev, "Write failed (%d)\n", ret);
293 }
294 for (i = DAC33_LINER_TO_RLO_VOL; i <= DAC33_OSC_TRIM; i++) {
295 data[0] = i;
296 data[1] = cache[i];
297 ret = codec->hw_write(codec->control_data, data, 2);
298 if (ret != 2)
299 dev_err(codec->dev, "Write failed (%d)\n", ret);
300 }
301}
302
303static inline void dac33_soft_power(struct snd_soc_codec *codec, int power)
304{
305 u8 reg;
306
307 reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
308 if (power)
309 reg |= DAC33_PDNALLB;
310 else
311 reg &= ~DAC33_PDNALLB;
312 dac33_write(codec, DAC33_PWR_CTRL, reg);
313}
314
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200315static int dac33_hard_power(struct snd_soc_codec *codec, int power)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300316{
317 struct tlv320dac33_priv *dac33 = codec->private_data;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200318 int ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300319
320 mutex_lock(&dac33->mutex);
321 if (power) {
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200322 ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
323 dac33->supplies);
324 if (ret != 0) {
325 dev_err(codec->dev,
326 "Failed to enable supplies: %d\n", ret);
327 goto exit;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300328 }
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200329
330 if (dac33->power_gpio >= 0)
331 gpio_set_value(dac33->power_gpio, 1);
332
333 dac33->chip_power = 1;
334
335 /* Restore registers */
336 dac33_restore_regs(codec);
337
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300338 dac33_soft_power(codec, 1);
339 } else {
340 dac33_soft_power(codec, 0);
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200341 if (dac33->power_gpio >= 0)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300342 gpio_set_value(dac33->power_gpio, 0);
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300343
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200344 ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies),
345 dac33->supplies);
346 if (ret != 0) {
347 dev_err(codec->dev,
348 "Failed to disable supplies: %d\n", ret);
349 goto exit;
350 }
351
352 dac33->chip_power = 0;
353 }
354
355exit:
356 mutex_unlock(&dac33->mutex);
357 return ret;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300358}
359
360static int dac33_get_nsample(struct snd_kcontrol *kcontrol,
361 struct snd_ctl_elem_value *ucontrol)
362{
363 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
364 struct tlv320dac33_priv *dac33 = codec->private_data;
365
366 ucontrol->value.integer.value[0] = dac33->nsample;
367
368 return 0;
369}
370
371static int dac33_set_nsample(struct snd_kcontrol *kcontrol,
372 struct snd_ctl_elem_value *ucontrol)
373{
374 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
375 struct tlv320dac33_priv *dac33 = codec->private_data;
376 int ret = 0;
377
378 if (dac33->nsample == ucontrol->value.integer.value[0])
379 return 0;
380
381 if (ucontrol->value.integer.value[0] < dac33->nsample_min ||
382 ucontrol->value.integer.value[0] > dac33->nsample_max)
383 ret = -EINVAL;
384 else
385 dac33->nsample = ucontrol->value.integer.value[0];
386
387 return ret;
388}
389
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200390static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300391 struct snd_ctl_elem_value *ucontrol)
392{
393 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
394 struct tlv320dac33_priv *dac33 = codec->private_data;
395
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200396 ucontrol->value.integer.value[0] = dac33->fifo_mode;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300397
398 return 0;
399}
400
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200401static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol,
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300402 struct snd_ctl_elem_value *ucontrol)
403{
404 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
405 struct tlv320dac33_priv *dac33 = codec->private_data;
406 int ret = 0;
407
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200408 if (dac33->fifo_mode == ucontrol->value.integer.value[0])
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300409 return 0;
410 /* Do not allow changes while stream is running*/
411 if (codec->active)
412 return -EPERM;
413
414 if (ucontrol->value.integer.value[0] < 0 ||
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200415 ucontrol->value.integer.value[0] >= DAC33_FIFO_LAST_MODE)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300416 ret = -EINVAL;
417 else
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200418 dac33->fifo_mode = ucontrol->value.integer.value[0];
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300419
420 return ret;
421}
422
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200423/* Codec operation modes */
424static const char *dac33_fifo_mode_texts[] = {
425 "Bypass", "Mode 1"
426};
427
428static const struct soc_enum dac33_fifo_mode_enum =
429 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(dac33_fifo_mode_texts),
430 dac33_fifo_mode_texts);
431
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300432/*
433 * DACL/R digital volume control:
434 * from 0 dB to -63.5 in 0.5 dB steps
435 * Need to be inverted later on:
436 * 0x00 == 0 dB
437 * 0x7f == -63.5 dB
438 */
439static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0);
440
441static const struct snd_kcontrol_new dac33_snd_controls[] = {
442 SOC_DOUBLE_R_TLV("DAC Digital Playback Volume",
443 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL,
444 0, 0x7f, 1, dac_digivol_tlv),
445 SOC_DOUBLE_R("DAC Digital Playback Switch",
446 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1),
447 SOC_DOUBLE_R("Line to Line Out Volume",
448 DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1),
449};
450
451static const struct snd_kcontrol_new dac33_nsample_snd_controls[] = {
452 SOC_SINGLE_EXT("nSample", 0, 0, 5900, 0,
453 dac33_get_nsample, dac33_set_nsample),
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200454 SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum,
455 dac33_get_fifo_mode, dac33_set_fifo_mode),
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300456};
457
458/* Analog bypass */
459static const struct snd_kcontrol_new dac33_dapm_abypassl_control =
460 SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1);
461
462static const struct snd_kcontrol_new dac33_dapm_abypassr_control =
463 SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1);
464
465static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = {
466 SND_SOC_DAPM_OUTPUT("LEFT_LO"),
467 SND_SOC_DAPM_OUTPUT("RIGHT_LO"),
468
469 SND_SOC_DAPM_INPUT("LINEL"),
470 SND_SOC_DAPM_INPUT("LINER"),
471
472 SND_SOC_DAPM_DAC("DACL", "Left Playback", DAC33_LDAC_PWR_CTRL, 2, 0),
473 SND_SOC_DAPM_DAC("DACR", "Right Playback", DAC33_RDAC_PWR_CTRL, 2, 0),
474
475 /* Analog bypass */
476 SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0,
477 &dac33_dapm_abypassl_control),
478 SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0,
479 &dac33_dapm_abypassr_control),
480
481 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amp Power",
482 DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0),
483 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amp Power",
484 DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0),
485};
486
487static const struct snd_soc_dapm_route audio_map[] = {
488 /* Analog bypass */
489 {"Analog Left Bypass", "Switch", "LINEL"},
490 {"Analog Right Bypass", "Switch", "LINER"},
491
492 {"Output Left Amp Power", NULL, "DACL"},
493 {"Output Right Amp Power", NULL, "DACR"},
494
495 {"Output Left Amp Power", NULL, "Analog Left Bypass"},
496 {"Output Right Amp Power", NULL, "Analog Right Bypass"},
497
498 /* output */
499 {"LEFT_LO", NULL, "Output Left Amp Power"},
500 {"RIGHT_LO", NULL, "Output Right Amp Power"},
501};
502
503static int dac33_add_widgets(struct snd_soc_codec *codec)
504{
505 snd_soc_dapm_new_controls(codec, dac33_dapm_widgets,
506 ARRAY_SIZE(dac33_dapm_widgets));
507
508 /* set up audio path interconnects */
509 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300510
511 return 0;
512}
513
514static int dac33_set_bias_level(struct snd_soc_codec *codec,
515 enum snd_soc_bias_level level)
516{
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200517 int ret;
518
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300519 switch (level) {
520 case SND_SOC_BIAS_ON:
521 dac33_soft_power(codec, 1);
522 break;
523 case SND_SOC_BIAS_PREPARE:
524 break;
525 case SND_SOC_BIAS_STANDBY:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200526 if (codec->bias_level == SND_SOC_BIAS_OFF) {
527 ret = dac33_hard_power(codec, 1);
528 if (ret != 0)
529 return ret;
530 }
531
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300532 dac33_soft_power(codec, 0);
533 break;
534 case SND_SOC_BIAS_OFF:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +0200535 ret = dac33_hard_power(codec, 0);
536 if (ret != 0)
537 return ret;
538
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300539 break;
540 }
541 codec->bias_level = level;
542
543 return 0;
544}
545
546static void dac33_work(struct work_struct *work)
547{
548 struct snd_soc_codec *codec;
549 struct tlv320dac33_priv *dac33;
550 u8 reg;
551
552 dac33 = container_of(work, struct tlv320dac33_priv, work);
553 codec = &dac33->codec;
554
555 mutex_lock(&dac33->mutex);
556 switch (dac33->state) {
557 case DAC33_PREFILL:
558 dac33->state = DAC33_PLAYBACK;
559 dac33_write16(codec, DAC33_NSAMPLE_MSB,
560 DAC33_THRREG(dac33->nsample));
561 dac33_write16(codec, DAC33_PREFILL_MSB,
562 DAC33_THRREG(dac33->alarm_threshold));
563 break;
564 case DAC33_PLAYBACK:
565 dac33_write16(codec, DAC33_NSAMPLE_MSB,
566 DAC33_THRREG(dac33->nsample));
567 break;
568 case DAC33_IDLE:
569 break;
570 case DAC33_FLUSH:
571 dac33->state = DAC33_IDLE;
572 /* Mask all interrupts from dac33 */
573 dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);
574
575 /* flush fifo */
576 reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
577 reg |= DAC33_FIFOFLUSH;
578 dac33_write(codec, DAC33_FIFO_CTRL_A, reg);
579 break;
580 }
581 mutex_unlock(&dac33->mutex);
582}
583
584static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
585{
586 struct snd_soc_codec *codec = dev;
587 struct tlv320dac33_priv *dac33 = codec->private_data;
588
589 queue_work(dac33->dac33_wq, &dac33->work);
590
591 return IRQ_HANDLED;
592}
593
594static void dac33_shutdown(struct snd_pcm_substream *substream,
595 struct snd_soc_dai *dai)
596{
597 struct snd_soc_pcm_runtime *rtd = substream->private_data;
598 struct snd_soc_device *socdev = rtd->socdev;
599 struct snd_soc_codec *codec = socdev->card->codec;
600 struct tlv320dac33_priv *dac33 = codec->private_data;
601 unsigned int pwr_ctrl;
602
603 /* Stop pending workqueue */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200604 if (dac33->fifo_mode)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300605 cancel_work_sync(&dac33->work);
606
607 mutex_lock(&dac33->mutex);
608 pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
609 pwr_ctrl &= ~(DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB);
610 dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);
611 mutex_unlock(&dac33->mutex);
612}
613
614static void dac33_oscwait(struct snd_soc_codec *codec)
615{
616 int timeout = 20;
617 u8 reg;
618
619 do {
620 msleep(1);
621 dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
622 } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--);
623 if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL)
624 dev_err(codec->dev,
625 "internal oscillator calibration failed\n");
626}
627
628static int dac33_hw_params(struct snd_pcm_substream *substream,
629 struct snd_pcm_hw_params *params,
630 struct snd_soc_dai *dai)
631{
632 struct snd_soc_pcm_runtime *rtd = substream->private_data;
633 struct snd_soc_device *socdev = rtd->socdev;
634 struct snd_soc_codec *codec = socdev->card->codec;
635
636 /* Check parameters for validity */
637 switch (params_rate(params)) {
638 case 44100:
639 case 48000:
640 break;
641 default:
642 dev_err(codec->dev, "unsupported rate %d\n",
643 params_rate(params));
644 return -EINVAL;
645 }
646
647 switch (params_format(params)) {
648 case SNDRV_PCM_FORMAT_S16_LE:
649 break;
650 default:
651 dev_err(codec->dev, "unsupported format %d\n",
652 params_format(params));
653 return -EINVAL;
654 }
655
656 return 0;
657}
658
659#define CALC_OSCSET(rate, refclk) ( \
660 ((((rate * 10000) / refclk) * 4096) + 5000) / 10000)
661#define CALC_RATIOSET(rate, refclk) ( \
662 ((((refclk * 100000) / rate) * 16384) + 50000) / 100000)
663
664/*
665 * tlv320dac33 is strict on the sequence of the register writes, if the register
666 * writes happens in different order, than dac33 might end up in unknown state.
667 * Use the known, working sequence of register writes to initialize the dac33.
668 */
669static int dac33_prepare_chip(struct snd_pcm_substream *substream)
670{
671 struct snd_soc_pcm_runtime *rtd = substream->private_data;
672 struct snd_soc_device *socdev = rtd->socdev;
673 struct snd_soc_codec *codec = socdev->card->codec;
674 struct tlv320dac33_priv *dac33 = codec->private_data;
675 unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
676 u8 aictrl_a, fifoctrl_a;
677
678 switch (substream->runtime->rate) {
679 case 44100:
680 case 48000:
681 oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk);
682 ratioset = CALC_RATIOSET(substream->runtime->rate,
683 dac33->refclk);
684 break;
685 default:
686 dev_err(codec->dev, "unsupported rate %d\n",
687 substream->runtime->rate);
688 return -EINVAL;
689 }
690
691
692 aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
693 aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK);
694 fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
695 fifoctrl_a &= ~DAC33_WIDTH;
696 switch (substream->runtime->format) {
697 case SNDRV_PCM_FORMAT_S16_LE:
698 aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16);
699 fifoctrl_a |= DAC33_WIDTH;
700 break;
701 default:
702 dev_err(codec->dev, "unsupported format %d\n",
703 substream->runtime->format);
704 return -EINVAL;
705 }
706
707 mutex_lock(&dac33->mutex);
708 dac33_soft_power(codec, 1);
709
710 reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
711 dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp);
712
713 /* Write registers 0x08 and 0x09 (MSB, LSB) */
714 dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset);
715
716 /* calib time: 128 is a nice number ;) */
717 dac33_write(codec, DAC33_CALIB_TIME, 128);
718
719 /* adjustment treshold & step */
720 dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) |
721 DAC33_ADJSTEP(1));
722
723 /* div=4 / gain=1 / div */
724 dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4));
725
726 pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
727 pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB;
728 dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);
729
730 dac33_oscwait(codec);
731
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200732 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300733 /* 50-51 : ASRC Control registers */
734 dac33_write(codec, DAC33_ASRC_CTRL_A, (1 << 4)); /* div=2 */
735 dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */
736
737 /* Write registers 0x34 and 0x35 (MSB, LSB) */
738 dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset);
739
740 /* Set interrupts to high active */
741 dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH);
742
743 dac33_write(codec, DAC33_FIFO_IRQ_MODE_B,
744 DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL));
745 dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
746 } else {
747 /* 50-51 : ASRC Control registers */
748 dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP);
749 dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */
750 }
751
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200752 if (dac33->fifo_mode)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300753 fifoctrl_a &= ~DAC33_FBYPAS;
754 else
755 fifoctrl_a |= DAC33_FBYPAS;
756 dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
757
758 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
759 reg_tmp = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200760 if (dac33->fifo_mode)
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300761 reg_tmp &= ~DAC33_BCLKON;
762 else
763 reg_tmp |= DAC33_BCLKON;
764 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, reg_tmp);
765
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200766 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300767 /* 20: BCLK divide ratio */
768 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 3);
769
770 dac33_write16(codec, DAC33_ATHR_MSB,
771 DAC33_THRREG(dac33->alarm_threshold));
772 } else {
773 dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
774 }
775
776 mutex_unlock(&dac33->mutex);
777
778 return 0;
779}
780
781static void dac33_calculate_times(struct snd_pcm_substream *substream)
782{
783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
784 struct snd_soc_device *socdev = rtd->socdev;
785 struct snd_soc_codec *codec = socdev->card->codec;
786 struct tlv320dac33_priv *dac33 = codec->private_data;
787 unsigned int nsample_limit;
788
789 /* Number of samples (16bit, stereo) in one period */
790 dac33->nsample_min = snd_pcm_lib_period_bytes(substream) / 4;
791
792 /* Number of samples (16bit, stereo) in ALSA buffer */
793 dac33->nsample_max = snd_pcm_lib_buffer_bytes(substream) / 4;
794 /* Subtract one period from the total */
795 dac33->nsample_max -= dac33->nsample_min;
796
797 /* Number of samples for LATENCY_TIME_MS / 2 */
798 dac33->alarm_threshold = substream->runtime->rate /
799 (1000 / (LATENCY_TIME_MS / 2));
800
801 /* Find and fix up the lowest nsmaple limit */
802 nsample_limit = substream->runtime->rate / (1000 / LATENCY_TIME_MS);
803
804 if (dac33->nsample_min < nsample_limit)
805 dac33->nsample_min = nsample_limit;
806
807 if (dac33->nsample < dac33->nsample_min)
808 dac33->nsample = dac33->nsample_min;
809
810 /*
811 * Find and fix up the highest nsmaple limit
812 * In order to not overflow the DAC33 buffer substract the
813 * alarm_threshold value from the size of the DAC33 buffer
814 */
815 nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - dac33->alarm_threshold;
816
817 if (dac33->nsample_max > nsample_limit)
818 dac33->nsample_max = nsample_limit;
819
820 if (dac33->nsample > dac33->nsample_max)
821 dac33->nsample = dac33->nsample_max;
822}
823
824static int dac33_pcm_prepare(struct snd_pcm_substream *substream,
825 struct snd_soc_dai *dai)
826{
827 dac33_calculate_times(substream);
828 dac33_prepare_chip(substream);
829
830 return 0;
831}
832
833static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
834 struct snd_soc_dai *dai)
835{
836 struct snd_soc_pcm_runtime *rtd = substream->private_data;
837 struct snd_soc_device *socdev = rtd->socdev;
838 struct snd_soc_codec *codec = socdev->card->codec;
839 struct tlv320dac33_priv *dac33 = codec->private_data;
840 int ret = 0;
841
842 switch (cmd) {
843 case SNDRV_PCM_TRIGGER_START:
844 case SNDRV_PCM_TRIGGER_RESUME:
845 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200846 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300847 dac33->state = DAC33_PREFILL;
848 queue_work(dac33->dac33_wq, &dac33->work);
849 }
850 break;
851 case SNDRV_PCM_TRIGGER_STOP:
852 case SNDRV_PCM_TRIGGER_SUSPEND:
853 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +0200854 if (dac33->fifo_mode) {
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +0300855 dac33->state = DAC33_FLUSH;
856 queue_work(dac33->dac33_wq, &dac33->work);
857 }
858 break;
859 default:
860 ret = -EINVAL;
861 }
862
863 return ret;
864}
865
866static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai,
867 int clk_id, unsigned int freq, int dir)
868{
869 struct snd_soc_codec *codec = codec_dai->codec;
870 struct tlv320dac33_priv *dac33 = codec->private_data;
871 u8 ioc_reg, asrcb_reg;
872
873 ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
874 asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B);
875 switch (clk_id) {
876 case TLV320DAC33_MCLK:
877 ioc_reg |= DAC33_REFSEL;
878 asrcb_reg |= DAC33_SRCREFSEL;
879 break;
880 case TLV320DAC33_SLEEPCLK:
881 ioc_reg &= ~DAC33_REFSEL;
882 asrcb_reg &= ~DAC33_SRCREFSEL;
883 break;
884 default:
885 dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id);
886 break;
887 }
888 dac33->refclk = freq;
889
890 dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg);
891 dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg);
892
893 return 0;
894}
895
896static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai,
897 unsigned int fmt)
898{
899 struct snd_soc_codec *codec = codec_dai->codec;
900 u8 aictrl_a, aictrl_b;
901
902 aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
903 aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
904 /* set master/slave audio interface */
905 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
906 case SND_SOC_DAIFMT_CBM_CFM:
907 /* Codec Master */
908 aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK);
909 break;
910 case SND_SOC_DAIFMT_CBS_CFS:
911 /* Codec Slave */
912 aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK);
913 break;
914 default:
915 return -EINVAL;
916 }
917
918 aictrl_a &= ~DAC33_AFMT_MASK;
919 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
920 case SND_SOC_DAIFMT_I2S:
921 aictrl_a |= DAC33_AFMT_I2S;
922 break;
923 case SND_SOC_DAIFMT_DSP_A:
924 aictrl_a |= DAC33_AFMT_DSP;
925 aictrl_b &= ~DAC33_DATA_DELAY_MASK;
926 aictrl_b |= DAC33_DATA_DELAY(1); /* 1 bit delay */
927 break;
928 case SND_SOC_DAIFMT_DSP_B:
929 aictrl_a |= DAC33_AFMT_DSP;
930 aictrl_b &= ~DAC33_DATA_DELAY_MASK; /* No delay */
931 break;
932 case SND_SOC_DAIFMT_RIGHT_J:
933 aictrl_a |= DAC33_AFMT_RIGHT_J;
934 break;
935 case SND_SOC_DAIFMT_LEFT_J:
936 aictrl_a |= DAC33_AFMT_LEFT_J;
937 break;
938 default:
939 dev_err(codec->dev, "Unsupported format (%u)\n",
940 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
941 return -EINVAL;
942 }
943
944 dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
945 dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
946
947 return 0;
948}
949
950static void dac33_init_chip(struct snd_soc_codec *codec)
951{
952 /* 44-46: DAC Control Registers */
953 /* A : DAC sample rate Fsref/1.5 */
954 dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(1));
955 /* B : DAC src=normal, not muted */
956 dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT |
957 DAC33_DACSRCL_LEFT);
958 /* C : (defaults) */
959 dac33_write(codec, DAC33_DAC_CTRL_C, 0x00);
960
961 /* 64-65 : L&R DAC power control
962 Line In -> OUT 1V/V Gain, DAC -> OUT 4V/V Gain*/
963 dac33_write(codec, DAC33_LDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
964 dac33_write(codec, DAC33_RDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
965
966 /* 73 : volume soft stepping control,
967 clock source = internal osc (?) */
968 dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);
969
970 /* 66 : LOP/LOM Modes */
971 dac33_write(codec, DAC33_OUT_AMP_CM_CTRL, 0xff);
972
973 /* 68 : LOM inverted from LOP */
974 dac33_write(codec, DAC33_OUT_AMP_CTRL, (3<<2));
975
976 dac33_write(codec, DAC33_PWR_CTRL, DAC33_PDNALLB);
977}
978
979static int dac33_soc_probe(struct platform_device *pdev)
980{
981 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
982 struct snd_soc_codec *codec;
983 struct tlv320dac33_priv *dac33;
984 int ret = 0;
985
986 BUG_ON(!tlv320dac33_codec);
987
988 codec = tlv320dac33_codec;
989 socdev->card->codec = codec;
990 dac33 = codec->private_data;
991
992 /* Power up the codec */
993 dac33_hard_power(codec, 1);
994 /* Set default configuration */
995 dac33_init_chip(codec);
996
997 /* register pcms */
998 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
999 if (ret < 0) {
1000 dev_err(codec->dev, "failed to create pcms\n");
1001 goto pcm_err;
1002 }
1003
1004 snd_soc_add_controls(codec, dac33_snd_controls,
1005 ARRAY_SIZE(dac33_snd_controls));
1006 /* Only add the nSample controls, if we have valid IRQ number */
1007 if (dac33->irq >= 0)
1008 snd_soc_add_controls(codec, dac33_nsample_snd_controls,
1009 ARRAY_SIZE(dac33_nsample_snd_controls));
1010
1011 dac33_add_widgets(codec);
1012
1013 /* power on device */
1014 dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1015
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001016 /* Bias level configuration has enabled regulator an extra time */
1017 regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1018
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001019 return 0;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001020
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001021pcm_err:
1022 dac33_hard_power(codec, 0);
1023 return ret;
1024}
1025
1026static int dac33_soc_remove(struct platform_device *pdev)
1027{
1028 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1029 struct snd_soc_codec *codec = socdev->card->codec;
1030
1031 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1032
1033 snd_soc_free_pcms(socdev);
1034 snd_soc_dapm_free(socdev);
1035
1036 return 0;
1037}
1038
1039static int dac33_soc_suspend(struct platform_device *pdev, pm_message_t state)
1040{
1041 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1042 struct snd_soc_codec *codec = socdev->card->codec;
1043
1044 dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
1045
1046 return 0;
1047}
1048
1049static int dac33_soc_resume(struct platform_device *pdev)
1050{
1051 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1052 struct snd_soc_codec *codec = socdev->card->codec;
1053
1054 dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1055 dac33_set_bias_level(codec, codec->suspend_bias_level);
1056
1057 return 0;
1058}
1059
1060struct snd_soc_codec_device soc_codec_dev_tlv320dac33 = {
1061 .probe = dac33_soc_probe,
1062 .remove = dac33_soc_remove,
1063 .suspend = dac33_soc_suspend,
1064 .resume = dac33_soc_resume,
1065};
1066EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320dac33);
1067
1068#define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
1069 SNDRV_PCM_RATE_48000)
1070#define DAC33_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1071
1072static struct snd_soc_dai_ops dac33_dai_ops = {
1073 .shutdown = dac33_shutdown,
1074 .hw_params = dac33_hw_params,
1075 .prepare = dac33_pcm_prepare,
1076 .trigger = dac33_pcm_trigger,
1077 .set_sysclk = dac33_set_dai_sysclk,
1078 .set_fmt = dac33_set_dai_fmt,
1079};
1080
1081struct snd_soc_dai dac33_dai = {
1082 .name = "tlv320dac33",
1083 .playback = {
1084 .stream_name = "Playback",
1085 .channels_min = 2,
1086 .channels_max = 2,
1087 .rates = DAC33_RATES,
1088 .formats = DAC33_FORMATS,},
1089 .ops = &dac33_dai_ops,
1090};
1091EXPORT_SYMBOL_GPL(dac33_dai);
1092
1093static int dac33_i2c_probe(struct i2c_client *client,
1094 const struct i2c_device_id *id)
1095{
1096 struct tlv320dac33_platform_data *pdata;
1097 struct tlv320dac33_priv *dac33;
1098 struct snd_soc_codec *codec;
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001099 int ret, i;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001100
1101 if (client->dev.platform_data == NULL) {
1102 dev_err(&client->dev, "Platform data not set\n");
1103 return -ENODEV;
1104 }
1105 pdata = client->dev.platform_data;
1106
1107 dac33 = kzalloc(sizeof(struct tlv320dac33_priv), GFP_KERNEL);
1108 if (dac33 == NULL)
1109 return -ENOMEM;
1110
1111 codec = &dac33->codec;
1112 codec->private_data = dac33;
1113 codec->control_data = client;
1114
1115 mutex_init(&codec->mutex);
1116 mutex_init(&dac33->mutex);
1117 INIT_LIST_HEAD(&codec->dapm_widgets);
1118 INIT_LIST_HEAD(&codec->dapm_paths);
1119
1120 codec->name = "tlv320dac33";
1121 codec->owner = THIS_MODULE;
1122 codec->read = dac33_read_reg_cache;
1123 codec->write = dac33_write_locked;
1124 codec->hw_write = (hw_write_t) i2c_master_send;
1125 codec->bias_level = SND_SOC_BIAS_OFF;
1126 codec->set_bias_level = dac33_set_bias_level;
1127 codec->dai = &dac33_dai;
1128 codec->num_dai = 1;
1129 codec->reg_cache_size = ARRAY_SIZE(dac33_reg);
1130 codec->reg_cache = kmemdup(dac33_reg, ARRAY_SIZE(dac33_reg),
1131 GFP_KERNEL);
1132 if (codec->reg_cache == NULL) {
1133 ret = -ENOMEM;
1134 goto error_reg;
1135 }
1136
1137 i2c_set_clientdata(client, dac33);
1138
1139 dac33->power_gpio = pdata->power_gpio;
1140 dac33->irq = client->irq;
1141 dac33->nsample = NSAMPLE_MAX;
1142 /* Disable FIFO use by default */
Peter Ujfalusi7427b4b2009-12-31 10:30:19 +02001143 dac33->fifo_mode = DAC33_FIFO_BYPASS;
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001144
1145 tlv320dac33_codec = codec;
1146
1147 codec->dev = &client->dev;
1148 dac33_dai.dev = codec->dev;
1149
1150 /* Check if the reset GPIO number is valid and request it */
1151 if (dac33->power_gpio >= 0) {
1152 ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset");
1153 if (ret < 0) {
1154 dev_err(codec->dev,
1155 "Failed to request reset GPIO (%d)\n",
1156 dac33->power_gpio);
1157 snd_soc_unregister_dai(&dac33_dai);
1158 snd_soc_unregister_codec(codec);
1159 goto error_gpio;
1160 }
1161 gpio_direction_output(dac33->power_gpio, 0);
1162 } else {
1163 dac33->chip_power = 1;
1164 }
1165
1166 /* Check if the IRQ number is valid and request it */
1167 if (dac33->irq >= 0) {
1168 ret = request_irq(dac33->irq, dac33_interrupt_handler,
1169 IRQF_TRIGGER_RISING | IRQF_DISABLED,
1170 codec->name, codec);
1171 if (ret < 0) {
1172 dev_err(codec->dev, "Could not request IRQ%d (%d)\n",
1173 dac33->irq, ret);
1174 dac33->irq = -1;
1175 }
1176 if (dac33->irq != -1) {
1177 /* Setup work queue */
Peter Ujfalusi74ea23a2009-11-26 13:55:11 +02001178 dac33->dac33_wq =
1179 create_singlethread_workqueue("tlv320dac33");
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001180 if (dac33->dac33_wq == NULL) {
1181 free_irq(dac33->irq, &dac33->codec);
1182 ret = -ENOMEM;
1183 goto error_wq;
1184 }
1185
1186 INIT_WORK(&dac33->work, dac33_work);
1187 }
1188 }
1189
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001190 for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
1191 dac33->supplies[i].supply = dac33_supply_names[i];
1192
1193 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(dac33->supplies),
1194 dac33->supplies);
1195
1196 if (ret != 0) {
1197 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
1198 goto err_get;
1199 }
1200
1201 ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
1202 dac33->supplies);
1203 if (ret != 0) {
1204 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
1205 goto err_enable;
1206 }
1207
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001208 ret = snd_soc_register_codec(codec);
1209 if (ret != 0) {
1210 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1211 goto error_codec;
1212 }
1213
1214 ret = snd_soc_register_dai(&dac33_dai);
1215 if (ret != 0) {
1216 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
1217 snd_soc_unregister_codec(codec);
1218 goto error_codec;
1219 }
1220
1221 /* Shut down the codec for now */
1222 dac33_hard_power(codec, 0);
1223
1224 return ret;
1225
1226error_codec:
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001227 regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1228err_enable:
1229 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1230err_get:
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001231 if (dac33->irq >= 0) {
1232 free_irq(dac33->irq, &dac33->codec);
1233 destroy_workqueue(dac33->dac33_wq);
1234 }
1235error_wq:
1236 if (dac33->power_gpio >= 0)
1237 gpio_free(dac33->power_gpio);
1238error_gpio:
1239 kfree(codec->reg_cache);
1240error_reg:
1241 tlv320dac33_codec = NULL;
1242 kfree(dac33);
1243
1244 return ret;
1245}
1246
1247static int dac33_i2c_remove(struct i2c_client *client)
1248{
1249 struct tlv320dac33_priv *dac33;
1250
1251 dac33 = i2c_get_clientdata(client);
1252 dac33_hard_power(&dac33->codec, 0);
1253
1254 if (dac33->power_gpio >= 0)
1255 gpio_free(dac33->power_gpio);
1256 if (dac33->irq >= 0)
1257 free_irq(dac33->irq, &dac33->codec);
1258
Ilkka Koskinen3a7aaed2009-12-04 13:49:10 +02001259 regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
1260
Peter Ujfalusic8bf93f2009-10-15 09:03:56 +03001261 destroy_workqueue(dac33->dac33_wq);
1262 snd_soc_unregister_dai(&dac33_dai);
1263 snd_soc_unregister_codec(&dac33->codec);
1264 kfree(dac33->codec.reg_cache);
1265 kfree(dac33);
1266 tlv320dac33_codec = NULL;
1267
1268 return 0;
1269}
1270
1271static const struct i2c_device_id tlv320dac33_i2c_id[] = {
1272 {
1273 .name = "tlv320dac33",
1274 .driver_data = 0,
1275 },
1276 { },
1277};
1278
1279static struct i2c_driver tlv320dac33_i2c_driver = {
1280 .driver = {
1281 .name = "tlv320dac33",
1282 .owner = THIS_MODULE,
1283 },
1284 .probe = dac33_i2c_probe,
1285 .remove = __devexit_p(dac33_i2c_remove),
1286 .id_table = tlv320dac33_i2c_id,
1287};
1288
1289static int __init dac33_module_init(void)
1290{
1291 int r;
1292 r = i2c_add_driver(&tlv320dac33_i2c_driver);
1293 if (r < 0) {
1294 printk(KERN_ERR "DAC33: driver registration failed\n");
1295 return r;
1296 }
1297 return 0;
1298}
1299module_init(dac33_module_init);
1300
1301static void __exit dac33_module_exit(void)
1302{
1303 i2c_del_driver(&tlv320dac33_i2c_driver);
1304}
1305module_exit(dac33_module_exit);
1306
1307
1308MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
1309MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1310MODULE_LICENSE("GPL");