x86: Fix S4 regression
[linux-btrfs-devel.git] / sound / soc / codecs / sgtl5000.c
blob7e4066e131e64ac95ed0eee06bb470a4ee23a6f2
1 /*
2 * sgtl5000.c -- SGTL5000 ALSA SoC Audio driver
4 * Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/pm.h>
17 #include <linux/i2c.h>
18 #include <linux/clk.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/consumer.h>
23 #include <sound/core.h>
24 #include <sound/tlv.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
31 #include "sgtl5000.h"
33 #define SGTL5000_DAP_REG_OFFSET 0x0100
34 #define SGTL5000_MAX_REG_OFFSET 0x013A
36 /* default value of sgtl5000 registers */
37 static const u16 sgtl5000_regs[SGTL5000_MAX_REG_OFFSET] = {
38 [SGTL5000_CHIP_CLK_CTRL] = 0x0008,
39 [SGTL5000_CHIP_I2S_CTRL] = 0x0010,
40 [SGTL5000_CHIP_SSS_CTRL] = 0x0008,
41 [SGTL5000_CHIP_DAC_VOL] = 0x3c3c,
42 [SGTL5000_CHIP_PAD_STRENGTH] = 0x015f,
43 [SGTL5000_CHIP_ANA_HP_CTRL] = 0x1818,
44 [SGTL5000_CHIP_ANA_CTRL] = 0x0111,
45 [SGTL5000_CHIP_LINE_OUT_VOL] = 0x0404,
46 [SGTL5000_CHIP_ANA_POWER] = 0x7060,
47 [SGTL5000_CHIP_PLL_CTRL] = 0x5000,
48 [SGTL5000_DAP_BASS_ENHANCE] = 0x0040,
49 [SGTL5000_DAP_BASS_ENHANCE_CTRL] = 0x051f,
50 [SGTL5000_DAP_SURROUND] = 0x0040,
51 [SGTL5000_DAP_EQ_BASS_BAND0] = 0x002f,
52 [SGTL5000_DAP_EQ_BASS_BAND1] = 0x002f,
53 [SGTL5000_DAP_EQ_BASS_BAND2] = 0x002f,
54 [SGTL5000_DAP_EQ_BASS_BAND3] = 0x002f,
55 [SGTL5000_DAP_EQ_BASS_BAND4] = 0x002f,
56 [SGTL5000_DAP_MAIN_CHAN] = 0x8000,
57 [SGTL5000_DAP_AVC_CTRL] = 0x0510,
58 [SGTL5000_DAP_AVC_THRESHOLD] = 0x1473,
59 [SGTL5000_DAP_AVC_ATTACK] = 0x0028,
60 [SGTL5000_DAP_AVC_DECAY] = 0x0050,
63 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
64 enum sgtl5000_regulator_supplies {
65 VDDA,
66 VDDIO,
67 VDDD,
68 SGTL5000_SUPPLY_NUM
71 /* vddd is optional supply */
72 static const char *supply_names[SGTL5000_SUPPLY_NUM] = {
73 "VDDA",
74 "VDDIO",
75 "VDDD"
78 #define LDO_CONSUMER_NAME "VDDD_LDO"
79 #define LDO_VOLTAGE 1200000
81 static struct regulator_consumer_supply ldo_consumer[] = {
82 REGULATOR_SUPPLY(LDO_CONSUMER_NAME, NULL),
85 static struct regulator_init_data ldo_init_data = {
86 .constraints = {
87 .min_uV = 850000,
88 .max_uV = 1600000,
89 .valid_modes_mask = REGULATOR_MODE_NORMAL,
90 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
92 .num_consumer_supplies = 1,
93 .consumer_supplies = &ldo_consumer[0],
97 * sgtl5000 internal ldo regulator,
98 * enabled when VDDD not provided
100 struct ldo_regulator {
101 struct regulator_desc desc;
102 struct regulator_dev *dev;
103 int voltage;
104 void *codec_data;
105 bool enabled;
108 /* sgtl5000 private structure in codec */
109 struct sgtl5000_priv {
110 int sysclk; /* sysclk rate */
111 int master; /* i2s master or not */
112 int fmt; /* i2s data format */
113 struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM];
114 struct ldo_regulator *ldo;
118 * mic_bias power on/off share the same register bits with
119 * output impedance of mic bias, when power on mic bias, we
120 * need reclaim it to impedance value.
121 * 0x0 = Powered off
122 * 0x1 = 2Kohm
123 * 0x2 = 4Kohm
124 * 0x3 = 8Kohm
126 static int mic_bias_event(struct snd_soc_dapm_widget *w,
127 struct snd_kcontrol *kcontrol, int event)
129 switch (event) {
130 case SND_SOC_DAPM_POST_PMU:
131 /* change mic bias resistor to 4Kohm */
132 snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
133 SGTL5000_BIAS_R_4k, SGTL5000_BIAS_R_4k);
134 break;
136 case SND_SOC_DAPM_PRE_PMD:
138 * SGTL5000_BIAS_R_8k as mask to clean the two bits
139 * of mic bias and output impedance
141 snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
142 SGTL5000_BIAS_R_8k, 0);
143 break;
145 return 0;
149 * using codec assist to small pop, hp_powerup or lineout_powerup
150 * should stay setting until vag_powerup is fully ramped down,
151 * vag fully ramped down require 400ms.
153 static int small_pop_event(struct snd_soc_dapm_widget *w,
154 struct snd_kcontrol *kcontrol, int event)
156 switch (event) {
157 case SND_SOC_DAPM_PRE_PMU:
158 snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
159 SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
160 break;
162 case SND_SOC_DAPM_PRE_PMD:
163 snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
164 SGTL5000_VAG_POWERUP, 0);
165 msleep(400);
166 break;
167 default:
168 break;
171 return 0;
174 /* input sources for ADC */
175 static const char *adc_mux_text[] = {
176 "MIC_IN", "LINE_IN"
179 static const struct soc_enum adc_enum =
180 SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL, 2, 2, adc_mux_text);
182 static const struct snd_kcontrol_new adc_mux =
183 SOC_DAPM_ENUM("Capture Mux", adc_enum);
185 /* input sources for DAC */
186 static const char *dac_mux_text[] = {
187 "DAC", "LINE_IN"
190 static const struct soc_enum dac_enum =
191 SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL, 6, 2, dac_mux_text);
193 static const struct snd_kcontrol_new dac_mux =
194 SOC_DAPM_ENUM("Headphone Mux", dac_enum);
196 static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = {
197 SND_SOC_DAPM_INPUT("LINE_IN"),
198 SND_SOC_DAPM_INPUT("MIC_IN"),
200 SND_SOC_DAPM_OUTPUT("HP_OUT"),
201 SND_SOC_DAPM_OUTPUT("LINE_OUT"),
203 SND_SOC_DAPM_MICBIAS_E("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0,
204 mic_bias_event,
205 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
207 SND_SOC_DAPM_PGA_E("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0,
208 small_pop_event,
209 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
210 SND_SOC_DAPM_PGA_E("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0,
211 small_pop_event,
212 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
214 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux),
215 SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &dac_mux),
217 /* aif for i2s input */
218 SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
219 0, SGTL5000_CHIP_DIG_POWER,
220 0, 0),
222 /* aif for i2s output */
223 SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
224 0, SGTL5000_CHIP_DIG_POWER,
225 1, 0),
227 SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0),
229 SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0),
232 /* routes for sgtl5000 */
233 static const struct snd_soc_dapm_route audio_map[] = {
234 {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
235 {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
237 {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */
238 {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */
240 {"DAC", NULL, "AIFIN"}, /* i2s-->dac,skip audio mux */
241 {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
242 {"LO", NULL, "DAC"}, /* dac --> line_out */
244 {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
245 {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
247 {"LINE_OUT", NULL, "LO"},
248 {"HP_OUT", NULL, "HP"},
251 /* custom function to fetch info of PCM playback volume */
252 static int dac_info_volsw(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_info *uinfo)
255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
256 uinfo->count = 2;
257 uinfo->value.integer.min = 0;
258 uinfo->value.integer.max = 0xfc - 0x3c;
259 return 0;
263 * custom function to get of PCM playback volume
265 * dac volume register
266 * 15-------------8-7--------------0
267 * | R channel vol | L channel vol |
268 * -------------------------------
270 * PCM volume with 0.5017 dB steps from 0 to -90 dB
272 * register values map to dB
273 * 0x3B and less = Reserved
274 * 0x3C = 0 dB
275 * 0x3D = -0.5 dB
276 * 0xF0 = -90 dB
277 * 0xFC and greater = Muted
279 * register value map to userspace value
281 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
282 * ------------------------------
283 * userspace value 0xc0 0
285 static int dac_get_volsw(struct snd_kcontrol *kcontrol,
286 struct snd_ctl_elem_value *ucontrol)
288 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
289 int reg;
290 int l;
291 int r;
293 reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
295 /* get left channel volume */
296 l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
298 /* get right channel volume */
299 r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
301 /* make sure value fall in (0x3c,0xfc) */
302 l = clamp(l, 0x3c, 0xfc);
303 r = clamp(r, 0x3c, 0xfc);
305 /* invert it and map to userspace value */
306 l = 0xfc - l;
307 r = 0xfc - r;
309 ucontrol->value.integer.value[0] = l;
310 ucontrol->value.integer.value[1] = r;
312 return 0;
316 * custom function to put of PCM playback volume
318 * dac volume register
319 * 15-------------8-7--------------0
320 * | R channel vol | L channel vol |
321 * -------------------------------
323 * PCM volume with 0.5017 dB steps from 0 to -90 dB
325 * register values map to dB
326 * 0x3B and less = Reserved
327 * 0x3C = 0 dB
328 * 0x3D = -0.5 dB
329 * 0xF0 = -90 dB
330 * 0xFC and greater = Muted
332 * userspace value map to register value
334 * userspace value 0xc0 0
335 * ------------------------------
336 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
338 static int dac_put_volsw(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
341 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
342 int reg;
343 int l;
344 int r;
346 l = ucontrol->value.integer.value[0];
347 r = ucontrol->value.integer.value[1];
349 /* make sure userspace volume fall in (0, 0xfc-0x3c) */
350 l = clamp(l, 0, 0xfc - 0x3c);
351 r = clamp(r, 0, 0xfc - 0x3c);
353 /* invert it, get the value can be set to register */
354 l = 0xfc - l;
355 r = 0xfc - r;
357 /* shift to get the register value */
358 reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
359 r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
361 snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg);
363 return 0;
366 static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
368 /* tlv for mic gain, 0db 20db 30db 40db */
369 static const unsigned int mic_gain_tlv[] = {
370 TLV_DB_RANGE_HEAD(4),
371 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
372 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
375 /* tlv for hp volume, -51.5db to 12.0db, step .5db */
376 static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
378 static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
379 /* SOC_DOUBLE_S8_TLV with invert */
381 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
382 .name = "PCM Playback Volume",
383 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
384 SNDRV_CTL_ELEM_ACCESS_READWRITE,
385 .info = dac_info_volsw,
386 .get = dac_get_volsw,
387 .put = dac_put_volsw,
390 SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
391 SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
392 SGTL5000_CHIP_ANA_ADC_CTRL,
393 8, 2, 0, capture_6db_attenuate),
394 SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
396 SOC_DOUBLE_TLV("Headphone Playback Volume",
397 SGTL5000_CHIP_ANA_HP_CTRL,
398 0, 8,
399 0x7f, 1,
400 headphone_volume),
401 SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
402 5, 1, 0),
404 SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
405 0, 4, 0, mic_gain_tlv),
408 /* mute the codec used by alsa core */
409 static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute)
411 struct snd_soc_codec *codec = codec_dai->codec;
412 u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT;
414 snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
415 adcdac_ctrl, mute ? adcdac_ctrl : 0);
417 return 0;
420 /* set codec format */
421 static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
423 struct snd_soc_codec *codec = codec_dai->codec;
424 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
425 u16 i2sctl = 0;
427 sgtl5000->master = 0;
429 * i2s clock and frame master setting.
430 * ONLY support:
431 * - clock and frame slave,
432 * - clock and frame master
434 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
435 case SND_SOC_DAIFMT_CBS_CFS:
436 break;
437 case SND_SOC_DAIFMT_CBM_CFM:
438 i2sctl |= SGTL5000_I2S_MASTER;
439 sgtl5000->master = 1;
440 break;
441 default:
442 return -EINVAL;
445 /* setting i2s data format */
446 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
447 case SND_SOC_DAIFMT_DSP_A:
448 i2sctl |= SGTL5000_I2S_MODE_PCM;
449 break;
450 case SND_SOC_DAIFMT_DSP_B:
451 i2sctl |= SGTL5000_I2S_MODE_PCM;
452 i2sctl |= SGTL5000_I2S_LRALIGN;
453 break;
454 case SND_SOC_DAIFMT_I2S:
455 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
456 break;
457 case SND_SOC_DAIFMT_RIGHT_J:
458 i2sctl |= SGTL5000_I2S_MODE_RJ;
459 i2sctl |= SGTL5000_I2S_LRPOL;
460 break;
461 case SND_SOC_DAIFMT_LEFT_J:
462 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
463 i2sctl |= SGTL5000_I2S_LRALIGN;
464 break;
465 default:
466 return -EINVAL;
469 sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
471 /* Clock inversion */
472 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
473 case SND_SOC_DAIFMT_NB_NF:
474 break;
475 case SND_SOC_DAIFMT_IB_NF:
476 i2sctl |= SGTL5000_I2S_SCLK_INV;
477 break;
478 default:
479 return -EINVAL;
482 snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl);
484 return 0;
487 /* set codec sysclk */
488 static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
489 int clk_id, unsigned int freq, int dir)
491 struct snd_soc_codec *codec = codec_dai->codec;
492 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
494 switch (clk_id) {
495 case SGTL5000_SYSCLK:
496 sgtl5000->sysclk = freq;
497 break;
498 default:
499 return -EINVAL;
502 return 0;
506 * set clock according to i2s frame clock,
507 * sgtl5000 provide 2 clock sources.
508 * 1. sys_mclk. sample freq can only configure to
509 * 1/256, 1/384, 1/512 of sys_mclk.
510 * 2. pll. can derive any audio clocks.
512 * clock setting rules:
513 * 1. in slave mode, only sys_mclk can use.
514 * 2. as constraint by sys_mclk, sample freq should
515 * set to 32k, 44.1k and above.
516 * 3. using sys_mclk prefer to pll to save power.
518 static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
520 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
521 int clk_ctl = 0;
522 int sys_fs; /* sample freq */
525 * sample freq should be divided by frame clock,
526 * if frame clock lower than 44.1khz, sample feq should set to
527 * 32khz or 44.1khz.
529 switch (frame_rate) {
530 case 8000:
531 case 16000:
532 sys_fs = 32000;
533 break;
534 case 11025:
535 case 22050:
536 sys_fs = 44100;
537 break;
538 default:
539 sys_fs = frame_rate;
540 break;
543 /* set divided factor of frame clock */
544 switch (sys_fs / frame_rate) {
545 case 4:
546 clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
547 break;
548 case 2:
549 clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
550 break;
551 case 1:
552 clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
553 break;
554 default:
555 return -EINVAL;
558 /* set the sys_fs according to frame rate */
559 switch (sys_fs) {
560 case 32000:
561 clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
562 break;
563 case 44100:
564 clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
565 break;
566 case 48000:
567 clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
568 break;
569 case 96000:
570 clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
571 break;
572 default:
573 dev_err(codec->dev, "frame rate %d not supported\n",
574 frame_rate);
575 return -EINVAL;
579 * calculate the divider of mclk/sample_freq,
580 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
582 switch (sgtl5000->sysclk / sys_fs) {
583 case 256:
584 clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
585 SGTL5000_MCLK_FREQ_SHIFT;
586 break;
587 case 384:
588 clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
589 SGTL5000_MCLK_FREQ_SHIFT;
590 break;
591 case 512:
592 clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
593 SGTL5000_MCLK_FREQ_SHIFT;
594 break;
595 default:
596 /* if mclk not satisify the divider, use pll */
597 if (sgtl5000->master) {
598 clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
599 SGTL5000_MCLK_FREQ_SHIFT;
600 } else {
601 dev_err(codec->dev,
602 "PLL not supported in slave mode\n");
603 return -EINVAL;
607 /* if using pll, please check manual 6.4.2 for detail */
608 if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
609 u64 out, t;
610 int div2;
611 int pll_ctl;
612 unsigned int in, int_div, frac_div;
614 if (sgtl5000->sysclk > 17000000) {
615 div2 = 1;
616 in = sgtl5000->sysclk / 2;
617 } else {
618 div2 = 0;
619 in = sgtl5000->sysclk;
621 if (sys_fs == 44100)
622 out = 180633600;
623 else
624 out = 196608000;
625 t = do_div(out, in);
626 int_div = out;
627 t *= 2048;
628 do_div(t, in);
629 frac_div = t;
630 pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
631 frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
633 snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
634 if (div2)
635 snd_soc_update_bits(codec,
636 SGTL5000_CHIP_CLK_TOP_CTRL,
637 SGTL5000_INPUT_FREQ_DIV2,
638 SGTL5000_INPUT_FREQ_DIV2);
639 else
640 snd_soc_update_bits(codec,
641 SGTL5000_CHIP_CLK_TOP_CTRL,
642 SGTL5000_INPUT_FREQ_DIV2,
645 /* power up pll */
646 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
647 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
648 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
649 } else {
650 /* power down pll */
651 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
652 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
656 /* if using pll, clk_ctrl must be set after pll power up */
657 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
659 return 0;
663 * Set PCM DAI bit size and sample rate.
664 * input: params_rate, params_fmt
666 static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
667 struct snd_pcm_hw_params *params,
668 struct snd_soc_dai *dai)
670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
671 struct snd_soc_codec *codec = rtd->codec;
672 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
673 int channels = params_channels(params);
674 int i2s_ctl = 0;
675 int stereo;
676 int ret;
678 /* sysclk should already set */
679 if (!sgtl5000->sysclk) {
680 dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
681 return -EFAULT;
684 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
685 stereo = SGTL5000_DAC_STEREO;
686 else
687 stereo = SGTL5000_ADC_STEREO;
689 /* set mono to save power */
690 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
691 channels == 1 ? 0 : stereo);
693 /* set codec clock base on lrclk */
694 ret = sgtl5000_set_clock(codec, params_rate(params));
695 if (ret)
696 return ret;
698 /* set i2s data format */
699 switch (params_format(params)) {
700 case SNDRV_PCM_FORMAT_S16_LE:
701 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
702 return -EINVAL;
703 i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
704 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
705 SGTL5000_I2S_SCLKFREQ_SHIFT;
706 break;
707 case SNDRV_PCM_FORMAT_S20_3LE:
708 i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
709 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
710 SGTL5000_I2S_SCLKFREQ_SHIFT;
711 break;
712 case SNDRV_PCM_FORMAT_S24_LE:
713 i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
714 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
715 SGTL5000_I2S_SCLKFREQ_SHIFT;
716 break;
717 case SNDRV_PCM_FORMAT_S32_LE:
718 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
719 return -EINVAL;
720 i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
721 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
722 SGTL5000_I2S_SCLKFREQ_SHIFT;
723 break;
724 default:
725 return -EINVAL;
728 snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL, i2s_ctl, i2s_ctl);
730 return 0;
733 #ifdef CONFIG_REGULATOR
734 static int ldo_regulator_is_enabled(struct regulator_dev *dev)
736 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
738 return ldo->enabled;
741 static int ldo_regulator_enable(struct regulator_dev *dev)
743 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
744 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
745 int reg;
747 if (ldo_regulator_is_enabled(dev))
748 return 0;
750 /* set regulator value firstly */
751 reg = (1600 - ldo->voltage / 1000) / 50;
752 reg = clamp(reg, 0x0, 0xf);
754 /* amend the voltage value, unit: uV */
755 ldo->voltage = (1600 - reg * 50) * 1000;
757 /* set voltage to register */
758 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
759 (0x1 << 4) - 1, reg);
761 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
762 SGTL5000_LINEREG_D_POWERUP,
763 SGTL5000_LINEREG_D_POWERUP);
765 /* when internal ldo enabled, simple digital power can be disabled */
766 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
767 SGTL5000_LINREG_SIMPLE_POWERUP,
770 ldo->enabled = 1;
771 return 0;
774 static int ldo_regulator_disable(struct regulator_dev *dev)
776 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
777 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
779 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
780 SGTL5000_LINEREG_D_POWERUP,
783 /* clear voltage info */
784 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
785 (0x1 << 4) - 1, 0);
787 ldo->enabled = 0;
789 return 0;
792 static int ldo_regulator_get_voltage(struct regulator_dev *dev)
794 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
796 return ldo->voltage;
799 static struct regulator_ops ldo_regulator_ops = {
800 .is_enabled = ldo_regulator_is_enabled,
801 .enable = ldo_regulator_enable,
802 .disable = ldo_regulator_disable,
803 .get_voltage = ldo_regulator_get_voltage,
806 static int ldo_regulator_register(struct snd_soc_codec *codec,
807 struct regulator_init_data *init_data,
808 int voltage)
810 struct ldo_regulator *ldo;
812 ldo = kzalloc(sizeof(struct ldo_regulator), GFP_KERNEL);
814 if (!ldo) {
815 dev_err(codec->dev, "failed to allocate ldo_regulator\n");
816 return -ENOMEM;
819 ldo->desc.name = kstrdup(dev_name(codec->dev), GFP_KERNEL);
820 if (!ldo->desc.name) {
821 kfree(ldo);
822 dev_err(codec->dev, "failed to allocate decs name memory\n");
823 return -ENOMEM;
826 ldo->desc.type = REGULATOR_VOLTAGE;
827 ldo->desc.owner = THIS_MODULE;
828 ldo->desc.ops = &ldo_regulator_ops;
829 ldo->desc.n_voltages = 1;
831 ldo->codec_data = codec;
832 ldo->voltage = voltage;
834 ldo->dev = regulator_register(&ldo->desc, codec->dev,
835 init_data, ldo);
836 if (IS_ERR(ldo->dev)) {
837 int ret = PTR_ERR(ldo->dev);
839 dev_err(codec->dev, "failed to register regulator\n");
840 kfree(ldo->desc.name);
841 kfree(ldo);
843 return ret;
846 return 0;
849 static int ldo_regulator_remove(struct snd_soc_codec *codec)
851 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
852 struct ldo_regulator *ldo = sgtl5000->ldo;
854 if (!ldo)
855 return 0;
857 regulator_unregister(ldo->dev);
858 kfree(ldo->desc.name);
859 kfree(ldo);
861 return 0;
863 #else
864 static int ldo_regulator_register(struct snd_soc_codec *codec,
865 struct regulator_init_data *init_data,
866 int voltage)
868 dev_err(codec->dev, "this setup needs regulator support in the kernel\n");
869 return -EINVAL;
872 static int ldo_regulator_remove(struct snd_soc_codec *codec)
874 return 0;
876 #endif
879 * set dac bias
880 * common state changes:
881 * startup:
882 * off --> standby --> prepare --> on
883 * standby --> prepare --> on
885 * stop:
886 * on --> prepare --> standby
888 static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
889 enum snd_soc_bias_level level)
891 int ret;
892 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
894 switch (level) {
895 case SND_SOC_BIAS_ON:
896 case SND_SOC_BIAS_PREPARE:
897 break;
898 case SND_SOC_BIAS_STANDBY:
899 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
900 ret = regulator_bulk_enable(
901 ARRAY_SIZE(sgtl5000->supplies),
902 sgtl5000->supplies);
903 if (ret)
904 return ret;
905 udelay(10);
908 break;
909 case SND_SOC_BIAS_OFF:
910 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
911 sgtl5000->supplies);
912 break;
915 codec->dapm.bias_level = level;
916 return 0;
919 #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
920 SNDRV_PCM_FMTBIT_S20_3LE |\
921 SNDRV_PCM_FMTBIT_S24_LE |\
922 SNDRV_PCM_FMTBIT_S32_LE)
924 static struct snd_soc_dai_ops sgtl5000_ops = {
925 .hw_params = sgtl5000_pcm_hw_params,
926 .digital_mute = sgtl5000_digital_mute,
927 .set_fmt = sgtl5000_set_dai_fmt,
928 .set_sysclk = sgtl5000_set_dai_sysclk,
931 static struct snd_soc_dai_driver sgtl5000_dai = {
932 .name = "sgtl5000",
933 .playback = {
934 .stream_name = "Playback",
935 .channels_min = 1,
936 .channels_max = 2,
938 * only support 8~48K + 96K,
939 * TODO modify hw_param to support more
941 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
942 .formats = SGTL5000_FORMATS,
944 .capture = {
945 .stream_name = "Capture",
946 .channels_min = 1,
947 .channels_max = 2,
948 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
949 .formats = SGTL5000_FORMATS,
951 .ops = &sgtl5000_ops,
952 .symmetric_rates = 1,
955 static int sgtl5000_volatile_register(struct snd_soc_codec *codec,
956 unsigned int reg)
958 switch (reg) {
959 case SGTL5000_CHIP_ID:
960 case SGTL5000_CHIP_ADCDAC_CTRL:
961 case SGTL5000_CHIP_ANA_STATUS:
962 return 1;
965 return 0;
968 #ifdef CONFIG_SUSPEND
969 static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
971 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
973 return 0;
977 * restore all sgtl5000 registers,
978 * since a big hole between dap and regular registers,
979 * we will restore them respectively.
981 static int sgtl5000_restore_regs(struct snd_soc_codec *codec)
983 u16 *cache = codec->reg_cache;
984 u16 reg;
986 /* restore regular registers */
987 for (reg = 0; reg <= SGTL5000_CHIP_SHORT_CTRL; reg += 2) {
989 /* this regs depends on the others */
990 if (reg == SGTL5000_CHIP_ANA_POWER ||
991 reg == SGTL5000_CHIP_CLK_CTRL ||
992 reg == SGTL5000_CHIP_LINREG_CTRL ||
993 reg == SGTL5000_CHIP_LINE_OUT_CTRL ||
994 reg == SGTL5000_CHIP_CLK_CTRL)
995 continue;
997 snd_soc_write(codec, reg, cache[reg]);
1000 /* restore dap registers */
1001 for (reg = SGTL5000_DAP_REG_OFFSET; reg < SGTL5000_MAX_REG_OFFSET; reg += 2)
1002 snd_soc_write(codec, reg, cache[reg]);
1005 * restore power and other regs according
1006 * to set_power() and set_clock()
1008 snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL,
1009 cache[SGTL5000_CHIP_LINREG_CTRL]);
1011 snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER,
1012 cache[SGTL5000_CHIP_ANA_POWER]);
1014 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL,
1015 cache[SGTL5000_CHIP_CLK_CTRL]);
1017 snd_soc_write(codec, SGTL5000_CHIP_REF_CTRL,
1018 cache[SGTL5000_CHIP_REF_CTRL]);
1020 snd_soc_write(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
1021 cache[SGTL5000_CHIP_LINE_OUT_CTRL]);
1022 return 0;
1025 static int sgtl5000_resume(struct snd_soc_codec *codec)
1027 /* Bring the codec back up to standby to enable regulators */
1028 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1030 /* Restore registers by cached in memory */
1031 sgtl5000_restore_regs(codec);
1032 return 0;
1034 #else
1035 #define sgtl5000_suspend NULL
1036 #define sgtl5000_resume NULL
1037 #endif /* CONFIG_SUSPEND */
1040 * sgtl5000 has 3 internal power supplies:
1041 * 1. VAG, normally set to vdda/2
1042 * 2. chargepump, set to different value
1043 * according to voltage of vdda and vddio
1044 * 3. line out VAG, normally set to vddio/2
1046 * and should be set according to:
1047 * 1. vddd provided by external or not
1048 * 2. vdda and vddio voltage value. > 3.1v or not
1049 * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
1051 static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
1053 int vddd;
1054 int vdda;
1055 int vddio;
1056 u16 ana_pwr;
1057 u16 lreg_ctrl;
1058 int vag;
1059 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1061 vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
1062 vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
1063 vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer);
1065 vdda = vdda / 1000;
1066 vddio = vddio / 1000;
1067 vddd = vddd / 1000;
1069 if (vdda <= 0 || vddio <= 0 || vddd < 0) {
1070 dev_err(codec->dev, "regulator voltage not set correctly\n");
1072 return -EINVAL;
1075 /* according to datasheet, maximum voltage of supplies */
1076 if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
1077 dev_err(codec->dev,
1078 "exceed max voltage vdda %dmv vddio %dma vddd %dma\n",
1079 vdda, vddio, vddd);
1081 return -EINVAL;
1084 /* reset value */
1085 ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
1086 ana_pwr |= SGTL5000_DAC_STEREO |
1087 SGTL5000_ADC_STEREO |
1088 SGTL5000_REFTOP_POWERUP;
1089 lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
1091 if (vddio < 3100 && vdda < 3100) {
1092 /* enable internal oscillator used for charge pump */
1093 snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
1094 SGTL5000_INT_OSC_EN,
1095 SGTL5000_INT_OSC_EN);
1096 /* Enable VDDC charge pump */
1097 ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
1098 } else if (vddio >= 3100 && vdda >= 3100) {
1100 * if vddio and vddd > 3.1v,
1101 * charge pump should be clean before set ana_pwr
1103 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1104 SGTL5000_VDDC_CHRGPMP_POWERUP, 0);
1106 /* VDDC use VDDIO rail */
1107 lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
1108 lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
1109 SGTL5000_VDDC_MAN_ASSN_SHIFT;
1112 snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
1114 snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
1116 /* set voltage to register */
1117 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
1118 (0x1 << 4) - 1, 0x8);
1121 * if vddd linear reg has been enabled,
1122 * simple digital supply should be clear to get
1123 * proper VDDD voltage.
1125 if (ana_pwr & SGTL5000_LINEREG_D_POWERUP)
1126 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1127 SGTL5000_LINREG_SIMPLE_POWERUP,
1129 else
1130 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1131 SGTL5000_LINREG_SIMPLE_POWERUP |
1132 SGTL5000_STARTUP_POWERUP,
1136 * set ADC/DAC VAG to vdda / 2,
1137 * should stay in range (0.8v, 1.575v)
1139 vag = vdda / 2;
1140 if (vag <= SGTL5000_ANA_GND_BASE)
1141 vag = 0;
1142 else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
1143 (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
1144 vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
1145 else
1146 vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
1148 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1149 vag << SGTL5000_ANA_GND_SHIFT,
1150 vag << SGTL5000_ANA_GND_SHIFT);
1152 /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
1153 vag = vddio / 2;
1154 if (vag <= SGTL5000_LINE_OUT_GND_BASE)
1155 vag = 0;
1156 else if (vag >= SGTL5000_LINE_OUT_GND_BASE +
1157 SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
1158 vag = SGTL5000_LINE_OUT_GND_MAX;
1159 else
1160 vag = (vag - SGTL5000_LINE_OUT_GND_BASE) /
1161 SGTL5000_LINE_OUT_GND_STP;
1163 snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
1164 vag << SGTL5000_LINE_OUT_GND_SHIFT |
1165 SGTL5000_LINE_OUT_CURRENT_360u <<
1166 SGTL5000_LINE_OUT_CURRENT_SHIFT,
1167 vag << SGTL5000_LINE_OUT_GND_SHIFT |
1168 SGTL5000_LINE_OUT_CURRENT_360u <<
1169 SGTL5000_LINE_OUT_CURRENT_SHIFT);
1171 return 0;
1174 static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec *codec)
1176 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1177 int ret;
1179 /* set internal ldo to 1.2v */
1180 ret = ldo_regulator_register(codec, &ldo_init_data, LDO_VOLTAGE);
1181 if (ret) {
1182 dev_err(codec->dev,
1183 "Failed to register vddd internal supplies: %d\n", ret);
1184 return ret;
1187 sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
1189 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
1190 sgtl5000->supplies);
1192 if (ret) {
1193 ldo_regulator_remove(codec);
1194 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
1195 return ret;
1198 dev_info(codec->dev, "Using internal LDO instead of VDDD\n");
1199 return 0;
1202 static int sgtl5000_enable_regulators(struct snd_soc_codec *codec)
1204 u16 reg;
1205 int ret;
1206 int rev;
1207 int i;
1208 int external_vddd = 0;
1209 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1211 for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
1212 sgtl5000->supplies[i].supply = supply_names[i];
1214 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
1215 sgtl5000->supplies);
1216 if (!ret)
1217 external_vddd = 1;
1218 else {
1219 ret = sgtl5000_replace_vddd_with_ldo(codec);
1220 if (ret)
1221 return ret;
1224 ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
1225 sgtl5000->supplies);
1226 if (ret)
1227 goto err_regulator_free;
1229 /* wait for all power rails bring up */
1230 udelay(10);
1232 /* read chip information */
1233 reg = snd_soc_read(codec, SGTL5000_CHIP_ID);
1234 if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
1235 SGTL5000_PARTID_PART_ID) {
1236 dev_err(codec->dev,
1237 "Device with ID register %x is not a sgtl5000\n", reg);
1238 ret = -ENODEV;
1239 goto err_regulator_disable;
1242 rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
1243 dev_info(codec->dev, "sgtl5000 revision %d\n", rev);
1246 * workaround for revision 0x11 and later,
1247 * roll back to use internal LDO
1249 if (external_vddd && rev >= 0x11) {
1250 /* disable all regulator first */
1251 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1252 sgtl5000->supplies);
1253 /* free VDDD regulator */
1254 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1255 sgtl5000->supplies);
1257 ret = sgtl5000_replace_vddd_with_ldo(codec);
1258 if (ret)
1259 return ret;
1261 ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
1262 sgtl5000->supplies);
1263 if (ret)
1264 goto err_regulator_free;
1266 /* wait for all power rails bring up */
1267 udelay(10);
1270 return 0;
1272 err_regulator_disable:
1273 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1274 sgtl5000->supplies);
1275 err_regulator_free:
1276 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1277 sgtl5000->supplies);
1278 if (external_vddd)
1279 ldo_regulator_remove(codec);
1280 return ret;
1284 static int sgtl5000_probe(struct snd_soc_codec *codec)
1286 int ret;
1287 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1289 /* setup i2c data ops */
1290 ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
1291 if (ret < 0) {
1292 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1293 return ret;
1296 ret = sgtl5000_enable_regulators(codec);
1297 if (ret)
1298 return ret;
1300 /* power up sgtl5000 */
1301 ret = sgtl5000_set_power_regs(codec);
1302 if (ret)
1303 goto err;
1305 /* enable small pop, introduce 400ms delay in turning off */
1306 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1307 SGTL5000_SMALL_POP,
1308 SGTL5000_SMALL_POP);
1310 /* disable short cut detector */
1311 snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
1314 * set i2s as default input of sound switch
1315 * TODO: add sound switch to control and dapm widge.
1317 snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
1318 SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
1319 snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
1320 SGTL5000_ADC_EN | SGTL5000_DAC_EN);
1322 /* enable dac volume ramp by default */
1323 snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
1324 SGTL5000_DAC_VOL_RAMP_EN |
1325 SGTL5000_DAC_MUTE_RIGHT |
1326 SGTL5000_DAC_MUTE_LEFT);
1328 snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
1330 snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
1331 SGTL5000_HP_ZCD_EN |
1332 SGTL5000_ADC_ZCD_EN);
1334 snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 0);
1337 * disable DAP
1338 * TODO:
1339 * Enable DAP in kcontrol and dapm.
1341 snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
1343 /* leading to standby state */
1344 ret = sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1345 if (ret)
1346 goto err;
1348 snd_soc_add_controls(codec, sgtl5000_snd_controls,
1349 ARRAY_SIZE(sgtl5000_snd_controls));
1351 snd_soc_dapm_new_controls(&codec->dapm, sgtl5000_dapm_widgets,
1352 ARRAY_SIZE(sgtl5000_dapm_widgets));
1354 snd_soc_dapm_add_routes(&codec->dapm, audio_map,
1355 ARRAY_SIZE(audio_map));
1357 snd_soc_dapm_new_widgets(&codec->dapm);
1359 return 0;
1361 err:
1362 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1363 sgtl5000->supplies);
1364 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1365 sgtl5000->supplies);
1366 ldo_regulator_remove(codec);
1368 return ret;
1371 static int sgtl5000_remove(struct snd_soc_codec *codec)
1373 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1375 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
1377 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1378 sgtl5000->supplies);
1379 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1380 sgtl5000->supplies);
1381 ldo_regulator_remove(codec);
1383 return 0;
1386 static struct snd_soc_codec_driver sgtl5000_driver = {
1387 .probe = sgtl5000_probe,
1388 .remove = sgtl5000_remove,
1389 .suspend = sgtl5000_suspend,
1390 .resume = sgtl5000_resume,
1391 .set_bias_level = sgtl5000_set_bias_level,
1392 .reg_cache_size = ARRAY_SIZE(sgtl5000_regs),
1393 .reg_word_size = sizeof(u16),
1394 .reg_cache_step = 2,
1395 .reg_cache_default = sgtl5000_regs,
1396 .volatile_register = sgtl5000_volatile_register,
1399 static __devinit int sgtl5000_i2c_probe(struct i2c_client *client,
1400 const struct i2c_device_id *id)
1402 struct sgtl5000_priv *sgtl5000;
1403 int ret;
1405 sgtl5000 = kzalloc(sizeof(struct sgtl5000_priv), GFP_KERNEL);
1406 if (!sgtl5000)
1407 return -ENOMEM;
1409 i2c_set_clientdata(client, sgtl5000);
1411 ret = snd_soc_register_codec(&client->dev,
1412 &sgtl5000_driver, &sgtl5000_dai, 1);
1413 if (ret) {
1414 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1415 kfree(sgtl5000);
1416 return ret;
1419 return 0;
1422 static __devexit int sgtl5000_i2c_remove(struct i2c_client *client)
1424 struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
1426 snd_soc_unregister_codec(&client->dev);
1428 kfree(sgtl5000);
1429 return 0;
1432 static const struct i2c_device_id sgtl5000_id[] = {
1433 {"sgtl5000", 0},
1437 MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
1439 static struct i2c_driver sgtl5000_i2c_driver = {
1440 .driver = {
1441 .name = "sgtl5000",
1442 .owner = THIS_MODULE,
1444 .probe = sgtl5000_i2c_probe,
1445 .remove = __devexit_p(sgtl5000_i2c_remove),
1446 .id_table = sgtl5000_id,
1449 static int __init sgtl5000_modinit(void)
1451 return i2c_add_driver(&sgtl5000_i2c_driver);
1453 module_init(sgtl5000_modinit);
1455 static void __exit sgtl5000_exit(void)
1457 i2c_del_driver(&sgtl5000_i2c_driver);
1459 module_exit(sgtl5000_exit);
1461 MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
1462 MODULE_AUTHOR("Zeng Zhaoming <zhaoming.zeng@freescale.com>");
1463 MODULE_LICENSE("GPL");