irqchip/zevio: Use irq_data_get_chip_type() helper
[linux/fpc-iii.git] / sound / soc / codecs / sgtl5000.c
blobf540f82b1f271ec4833d9ea43aa7f4567c0df712
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/log2.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/of_device.h>
25 #include <sound/core.h>
26 #include <sound/tlv.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/initval.h>
33 #include "sgtl5000.h"
35 #define SGTL5000_DAP_REG_OFFSET 0x0100
36 #define SGTL5000_MAX_REG_OFFSET 0x013A
38 /* default value of sgtl5000 registers */
39 static const struct reg_default sgtl5000_reg_defaults[] = {
40 { SGTL5000_CHIP_DIG_POWER, 0x0000 },
41 { SGTL5000_CHIP_CLK_CTRL, 0x0008 },
42 { SGTL5000_CHIP_I2S_CTRL, 0x0010 },
43 { SGTL5000_CHIP_SSS_CTRL, 0x0010 },
44 { SGTL5000_CHIP_ADCDAC_CTRL, 0x020c },
45 { SGTL5000_CHIP_DAC_VOL, 0x3c3c },
46 { SGTL5000_CHIP_PAD_STRENGTH, 0x015f },
47 { SGTL5000_CHIP_ANA_ADC_CTRL, 0x0000 },
48 { SGTL5000_CHIP_ANA_HP_CTRL, 0x1818 },
49 { SGTL5000_CHIP_ANA_CTRL, 0x0111 },
50 { SGTL5000_CHIP_LINREG_CTRL, 0x0000 },
51 { SGTL5000_CHIP_REF_CTRL, 0x0000 },
52 { SGTL5000_CHIP_MIC_CTRL, 0x0000 },
53 { SGTL5000_CHIP_LINE_OUT_CTRL, 0x0000 },
54 { SGTL5000_CHIP_LINE_OUT_VOL, 0x0404 },
55 { SGTL5000_CHIP_ANA_POWER, 0x7060 },
56 { SGTL5000_CHIP_PLL_CTRL, 0x5000 },
57 { SGTL5000_CHIP_CLK_TOP_CTRL, 0x0000 },
58 { SGTL5000_CHIP_ANA_STATUS, 0x0000 },
59 { SGTL5000_CHIP_SHORT_CTRL, 0x0000 },
60 { SGTL5000_CHIP_ANA_TEST2, 0x0000 },
61 { SGTL5000_DAP_CTRL, 0x0000 },
62 { SGTL5000_DAP_PEQ, 0x0000 },
63 { SGTL5000_DAP_BASS_ENHANCE, 0x0040 },
64 { SGTL5000_DAP_BASS_ENHANCE_CTRL, 0x051f },
65 { SGTL5000_DAP_AUDIO_EQ, 0x0000 },
66 { SGTL5000_DAP_SURROUND, 0x0040 },
67 { SGTL5000_DAP_EQ_BASS_BAND0, 0x002f },
68 { SGTL5000_DAP_EQ_BASS_BAND1, 0x002f },
69 { SGTL5000_DAP_EQ_BASS_BAND2, 0x002f },
70 { SGTL5000_DAP_EQ_BASS_BAND3, 0x002f },
71 { SGTL5000_DAP_EQ_BASS_BAND4, 0x002f },
72 { SGTL5000_DAP_MAIN_CHAN, 0x8000 },
73 { SGTL5000_DAP_MIX_CHAN, 0x0000 },
74 { SGTL5000_DAP_AVC_CTRL, 0x0510 },
75 { SGTL5000_DAP_AVC_THRESHOLD, 0x1473 },
76 { SGTL5000_DAP_AVC_ATTACK, 0x0028 },
77 { SGTL5000_DAP_AVC_DECAY, 0x0050 },
80 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
81 enum sgtl5000_regulator_supplies {
82 VDDA,
83 VDDIO,
84 VDDD,
85 SGTL5000_SUPPLY_NUM
88 /* vddd is optional supply */
89 static const char *supply_names[SGTL5000_SUPPLY_NUM] = {
90 "VDDA",
91 "VDDIO",
92 "VDDD"
95 #define LDO_CONSUMER_NAME "VDDD_LDO"
96 #define LDO_VOLTAGE 1200000
98 static struct regulator_consumer_supply ldo_consumer[] = {
99 REGULATOR_SUPPLY(LDO_CONSUMER_NAME, NULL),
102 static struct regulator_init_data ldo_init_data = {
103 .constraints = {
104 .min_uV = 1200000,
105 .max_uV = 1200000,
106 .valid_modes_mask = REGULATOR_MODE_NORMAL,
107 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
109 .num_consumer_supplies = 1,
110 .consumer_supplies = &ldo_consumer[0],
114 * sgtl5000 internal ldo regulator,
115 * enabled when VDDD not provided
117 struct ldo_regulator {
118 struct regulator_desc desc;
119 struct regulator_dev *dev;
120 int voltage;
121 void *codec_data;
122 bool enabled;
125 enum sgtl5000_micbias_resistor {
126 SGTL5000_MICBIAS_OFF = 0,
127 SGTL5000_MICBIAS_2K = 2,
128 SGTL5000_MICBIAS_4K = 4,
129 SGTL5000_MICBIAS_8K = 8,
132 /* sgtl5000 private structure in codec */
133 struct sgtl5000_priv {
134 int sysclk; /* sysclk rate */
135 int master; /* i2s master or not */
136 int fmt; /* i2s data format */
137 struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM];
138 struct ldo_regulator *ldo;
139 struct regmap *regmap;
140 struct clk *mclk;
141 int revision;
142 u8 micbias_resistor;
143 u8 micbias_voltage;
147 * mic_bias power on/off share the same register bits with
148 * output impedance of mic bias, when power on mic bias, we
149 * need reclaim it to impedance value.
150 * 0x0 = Powered off
151 * 0x1 = 2Kohm
152 * 0x2 = 4Kohm
153 * 0x3 = 8Kohm
155 static int mic_bias_event(struct snd_soc_dapm_widget *w,
156 struct snd_kcontrol *kcontrol, int event)
158 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
159 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
161 switch (event) {
162 case SND_SOC_DAPM_POST_PMU:
163 /* change mic bias resistor */
164 snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
165 SGTL5000_BIAS_R_MASK,
166 sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
167 break;
169 case SND_SOC_DAPM_PRE_PMD:
170 snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
171 SGTL5000_BIAS_R_MASK, 0);
172 break;
174 return 0;
178 * As manual described, ADC/DAC only works when VAG powerup,
179 * So enabled VAG before ADC/DAC up.
180 * In power down case, we need wait 400ms when vag fully ramped down.
182 static int power_vag_event(struct snd_soc_dapm_widget *w,
183 struct snd_kcontrol *kcontrol, int event)
185 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
186 const u32 mask = SGTL5000_DAC_POWERUP | SGTL5000_ADC_POWERUP;
188 switch (event) {
189 case SND_SOC_DAPM_POST_PMU:
190 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
191 SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
192 break;
194 case SND_SOC_DAPM_PRE_PMD:
196 * Don't clear VAG_POWERUP, when both DAC and ADC are
197 * operational to prevent inadvertently starving the
198 * other one of them.
200 if ((snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER) &
201 mask) != mask) {
202 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
203 SGTL5000_VAG_POWERUP, 0);
204 msleep(400);
206 break;
207 default:
208 break;
211 return 0;
214 /* input sources for ADC */
215 static const char *adc_mux_text[] = {
216 "MIC_IN", "LINE_IN"
219 static SOC_ENUM_SINGLE_DECL(adc_enum,
220 SGTL5000_CHIP_ANA_CTRL, 2,
221 adc_mux_text);
223 static const struct snd_kcontrol_new adc_mux =
224 SOC_DAPM_ENUM("Capture Mux", adc_enum);
226 /* input sources for DAC */
227 static const char *dac_mux_text[] = {
228 "DAC", "LINE_IN"
231 static SOC_ENUM_SINGLE_DECL(dac_enum,
232 SGTL5000_CHIP_ANA_CTRL, 6,
233 dac_mux_text);
235 static const struct snd_kcontrol_new dac_mux =
236 SOC_DAPM_ENUM("Headphone Mux", dac_enum);
238 static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = {
239 SND_SOC_DAPM_INPUT("LINE_IN"),
240 SND_SOC_DAPM_INPUT("MIC_IN"),
242 SND_SOC_DAPM_OUTPUT("HP_OUT"),
243 SND_SOC_DAPM_OUTPUT("LINE_OUT"),
245 SND_SOC_DAPM_SUPPLY("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0,
246 mic_bias_event,
247 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
249 SND_SOC_DAPM_PGA("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0),
250 SND_SOC_DAPM_PGA("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0),
252 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux),
253 SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &dac_mux),
255 /* aif for i2s input */
256 SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
257 0, SGTL5000_CHIP_DIG_POWER,
258 0, 0),
260 /* aif for i2s output */
261 SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
262 0, SGTL5000_CHIP_DIG_POWER,
263 1, 0),
265 SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0),
266 SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0),
268 SND_SOC_DAPM_PRE("VAG_POWER_PRE", power_vag_event),
269 SND_SOC_DAPM_POST("VAG_POWER_POST", power_vag_event),
272 /* routes for sgtl5000 */
273 static const struct snd_soc_dapm_route sgtl5000_dapm_routes[] = {
274 {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
275 {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
277 {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */
278 {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */
280 {"DAC", NULL, "AIFIN"}, /* i2s-->dac,skip audio mux */
281 {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
282 {"LO", NULL, "DAC"}, /* dac --> line_out */
284 {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
285 {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
287 {"LINE_OUT", NULL, "LO"},
288 {"HP_OUT", NULL, "HP"},
291 /* custom function to fetch info of PCM playback volume */
292 static int dac_info_volsw(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_info *uinfo)
295 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
296 uinfo->count = 2;
297 uinfo->value.integer.min = 0;
298 uinfo->value.integer.max = 0xfc - 0x3c;
299 return 0;
303 * custom function to get of PCM playback volume
305 * dac volume register
306 * 15-------------8-7--------------0
307 * | R channel vol | L channel vol |
308 * -------------------------------
310 * PCM volume with 0.5017 dB steps from 0 to -90 dB
312 * register values map to dB
313 * 0x3B and less = Reserved
314 * 0x3C = 0 dB
315 * 0x3D = -0.5 dB
316 * 0xF0 = -90 dB
317 * 0xFC and greater = Muted
319 * register value map to userspace value
321 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
322 * ------------------------------
323 * userspace value 0xc0 0
325 static int dac_get_volsw(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
328 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
329 int reg;
330 int l;
331 int r;
333 reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
335 /* get left channel volume */
336 l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
338 /* get right channel volume */
339 r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
341 /* make sure value fall in (0x3c,0xfc) */
342 l = clamp(l, 0x3c, 0xfc);
343 r = clamp(r, 0x3c, 0xfc);
345 /* invert it and map to userspace value */
346 l = 0xfc - l;
347 r = 0xfc - r;
349 ucontrol->value.integer.value[0] = l;
350 ucontrol->value.integer.value[1] = r;
352 return 0;
356 * custom function to put of PCM playback volume
358 * dac volume register
359 * 15-------------8-7--------------0
360 * | R channel vol | L channel vol |
361 * -------------------------------
363 * PCM volume with 0.5017 dB steps from 0 to -90 dB
365 * register values map to dB
366 * 0x3B and less = Reserved
367 * 0x3C = 0 dB
368 * 0x3D = -0.5 dB
369 * 0xF0 = -90 dB
370 * 0xFC and greater = Muted
372 * userspace value map to register value
374 * userspace value 0xc0 0
375 * ------------------------------
376 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
378 static int dac_put_volsw(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
381 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
382 int reg;
383 int l;
384 int r;
386 l = ucontrol->value.integer.value[0];
387 r = ucontrol->value.integer.value[1];
389 /* make sure userspace volume fall in (0, 0xfc-0x3c) */
390 l = clamp(l, 0, 0xfc - 0x3c);
391 r = clamp(r, 0, 0xfc - 0x3c);
393 /* invert it, get the value can be set to register */
394 l = 0xfc - l;
395 r = 0xfc - r;
397 /* shift to get the register value */
398 reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
399 r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
401 snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg);
403 return 0;
406 static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
408 /* tlv for mic gain, 0db 20db 30db 40db */
409 static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
410 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
411 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0)
414 /* tlv for hp volume, -51.5db to 12.0db, step .5db */
415 static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
417 static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
418 /* SOC_DOUBLE_S8_TLV with invert */
420 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
421 .name = "PCM Playback Volume",
422 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
423 SNDRV_CTL_ELEM_ACCESS_READWRITE,
424 .info = dac_info_volsw,
425 .get = dac_get_volsw,
426 .put = dac_put_volsw,
429 SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
430 SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
431 SGTL5000_CHIP_ANA_ADC_CTRL,
432 8, 1, 0, capture_6db_attenuate),
433 SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
435 SOC_DOUBLE_TLV("Headphone Playback Volume",
436 SGTL5000_CHIP_ANA_HP_CTRL,
437 0, 8,
438 0x7f, 1,
439 headphone_volume),
440 SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
441 5, 1, 0),
443 SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
444 0, 3, 0, mic_gain_tlv),
447 /* mute the codec used by alsa core */
448 static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute)
450 struct snd_soc_codec *codec = codec_dai->codec;
451 u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT;
453 snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
454 adcdac_ctrl, mute ? adcdac_ctrl : 0);
456 return 0;
459 /* set codec format */
460 static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
462 struct snd_soc_codec *codec = codec_dai->codec;
463 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
464 u16 i2sctl = 0;
466 sgtl5000->master = 0;
468 * i2s clock and frame master setting.
469 * ONLY support:
470 * - clock and frame slave,
471 * - clock and frame master
473 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
474 case SND_SOC_DAIFMT_CBS_CFS:
475 break;
476 case SND_SOC_DAIFMT_CBM_CFM:
477 i2sctl |= SGTL5000_I2S_MASTER;
478 sgtl5000->master = 1;
479 break;
480 default:
481 return -EINVAL;
484 /* setting i2s data format */
485 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
486 case SND_SOC_DAIFMT_DSP_A:
487 i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
488 break;
489 case SND_SOC_DAIFMT_DSP_B:
490 i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
491 i2sctl |= SGTL5000_I2S_LRALIGN;
492 break;
493 case SND_SOC_DAIFMT_I2S:
494 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
495 break;
496 case SND_SOC_DAIFMT_RIGHT_J:
497 i2sctl |= SGTL5000_I2S_MODE_RJ << SGTL5000_I2S_MODE_SHIFT;
498 i2sctl |= SGTL5000_I2S_LRPOL;
499 break;
500 case SND_SOC_DAIFMT_LEFT_J:
501 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
502 i2sctl |= SGTL5000_I2S_LRALIGN;
503 break;
504 default:
505 return -EINVAL;
508 sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
510 /* Clock inversion */
511 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
512 case SND_SOC_DAIFMT_NB_NF:
513 break;
514 case SND_SOC_DAIFMT_IB_NF:
515 i2sctl |= SGTL5000_I2S_SCLK_INV;
516 break;
517 default:
518 return -EINVAL;
521 snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl);
523 return 0;
526 /* set codec sysclk */
527 static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
528 int clk_id, unsigned int freq, int dir)
530 struct snd_soc_codec *codec = codec_dai->codec;
531 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
533 switch (clk_id) {
534 case SGTL5000_SYSCLK:
535 sgtl5000->sysclk = freq;
536 break;
537 default:
538 return -EINVAL;
541 return 0;
545 * set clock according to i2s frame clock,
546 * sgtl5000 provides 2 clock sources:
547 * 1. sys_mclk: sample freq can only be configured to
548 * 1/256, 1/384, 1/512 of sys_mclk.
549 * 2. pll: can derive any audio clocks.
551 * clock setting rules:
552 * 1. in slave mode, only sys_mclk can be used
553 * 2. as constraint by sys_mclk, sample freq should be set to 32 kHz, 44.1 kHz
554 * and above.
555 * 3. usage of sys_mclk is preferred over pll to save power.
557 static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
559 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
560 int clk_ctl = 0;
561 int sys_fs; /* sample freq */
564 * sample freq should be divided by frame clock,
565 * if frame clock is lower than 44.1 kHz, sample freq should be set to
566 * 32 kHz or 44.1 kHz.
568 switch (frame_rate) {
569 case 8000:
570 case 16000:
571 sys_fs = 32000;
572 break;
573 case 11025:
574 case 22050:
575 sys_fs = 44100;
576 break;
577 default:
578 sys_fs = frame_rate;
579 break;
582 /* set divided factor of frame clock */
583 switch (sys_fs / frame_rate) {
584 case 4:
585 clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
586 break;
587 case 2:
588 clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
589 break;
590 case 1:
591 clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
592 break;
593 default:
594 return -EINVAL;
597 /* set the sys_fs according to frame rate */
598 switch (sys_fs) {
599 case 32000:
600 clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
601 break;
602 case 44100:
603 clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
604 break;
605 case 48000:
606 clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
607 break;
608 case 96000:
609 clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
610 break;
611 default:
612 dev_err(codec->dev, "frame rate %d not supported\n",
613 frame_rate);
614 return -EINVAL;
618 * calculate the divider of mclk/sample_freq,
619 * factor of freq = 96 kHz can only be 256, since mclk is in the range
620 * of 8 MHz - 27 MHz
622 switch (sgtl5000->sysclk / frame_rate) {
623 case 256:
624 clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
625 SGTL5000_MCLK_FREQ_SHIFT;
626 break;
627 case 384:
628 clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
629 SGTL5000_MCLK_FREQ_SHIFT;
630 break;
631 case 512:
632 clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
633 SGTL5000_MCLK_FREQ_SHIFT;
634 break;
635 default:
636 /* if mclk does not satisfy the divider, use pll */
637 if (sgtl5000->master) {
638 clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
639 SGTL5000_MCLK_FREQ_SHIFT;
640 } else {
641 dev_err(codec->dev,
642 "PLL not supported in slave mode\n");
643 dev_err(codec->dev, "%d ratio is not supported. "
644 "SYS_MCLK needs to be 256, 384 or 512 * fs\n",
645 sgtl5000->sysclk / frame_rate);
646 return -EINVAL;
650 /* if using pll, please check manual 6.4.2 for detail */
651 if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
652 u64 out, t;
653 int div2;
654 int pll_ctl;
655 unsigned int in, int_div, frac_div;
657 if (sgtl5000->sysclk > 17000000) {
658 div2 = 1;
659 in = sgtl5000->sysclk / 2;
660 } else {
661 div2 = 0;
662 in = sgtl5000->sysclk;
664 if (sys_fs == 44100)
665 out = 180633600;
666 else
667 out = 196608000;
668 t = do_div(out, in);
669 int_div = out;
670 t *= 2048;
671 do_div(t, in);
672 frac_div = t;
673 pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
674 frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
676 snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
677 if (div2)
678 snd_soc_update_bits(codec,
679 SGTL5000_CHIP_CLK_TOP_CTRL,
680 SGTL5000_INPUT_FREQ_DIV2,
681 SGTL5000_INPUT_FREQ_DIV2);
682 else
683 snd_soc_update_bits(codec,
684 SGTL5000_CHIP_CLK_TOP_CTRL,
685 SGTL5000_INPUT_FREQ_DIV2,
688 /* power up pll */
689 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
690 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
691 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
693 /* if using pll, clk_ctrl must be set after pll power up */
694 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
695 } else {
696 /* otherwise, clk_ctrl must be set before pll power down */
697 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
699 /* power down pll */
700 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
701 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
705 return 0;
709 * Set PCM DAI bit size and sample rate.
710 * input: params_rate, params_fmt
712 static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
713 struct snd_pcm_hw_params *params,
714 struct snd_soc_dai *dai)
716 struct snd_soc_codec *codec = dai->codec;
717 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
718 int channels = params_channels(params);
719 int i2s_ctl = 0;
720 int stereo;
721 int ret;
723 /* sysclk should already set */
724 if (!sgtl5000->sysclk) {
725 dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
726 return -EFAULT;
729 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
730 stereo = SGTL5000_DAC_STEREO;
731 else
732 stereo = SGTL5000_ADC_STEREO;
734 /* set mono to save power */
735 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
736 channels == 1 ? 0 : stereo);
738 /* set codec clock base on lrclk */
739 ret = sgtl5000_set_clock(codec, params_rate(params));
740 if (ret)
741 return ret;
743 /* set i2s data format */
744 switch (params_width(params)) {
745 case 16:
746 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
747 return -EINVAL;
748 i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
749 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
750 SGTL5000_I2S_SCLKFREQ_SHIFT;
751 break;
752 case 20:
753 i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
754 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
755 SGTL5000_I2S_SCLKFREQ_SHIFT;
756 break;
757 case 24:
758 i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
759 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
760 SGTL5000_I2S_SCLKFREQ_SHIFT;
761 break;
762 case 32:
763 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
764 return -EINVAL;
765 i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
766 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
767 SGTL5000_I2S_SCLKFREQ_SHIFT;
768 break;
769 default:
770 return -EINVAL;
773 snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL,
774 SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK,
775 i2s_ctl);
777 return 0;
780 #ifdef CONFIG_REGULATOR
781 static int ldo_regulator_is_enabled(struct regulator_dev *dev)
783 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
785 return ldo->enabled;
788 static int ldo_regulator_enable(struct regulator_dev *dev)
790 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
791 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
792 int reg;
794 if (ldo_regulator_is_enabled(dev))
795 return 0;
797 /* set regulator value firstly */
798 reg = (1600 - ldo->voltage / 1000) / 50;
799 reg = clamp(reg, 0x0, 0xf);
801 /* amend the voltage value, unit: uV */
802 ldo->voltage = (1600 - reg * 50) * 1000;
804 /* set voltage to register */
805 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
806 SGTL5000_LINREG_VDDD_MASK, reg);
808 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
809 SGTL5000_LINEREG_D_POWERUP,
810 SGTL5000_LINEREG_D_POWERUP);
812 /* when internal ldo is enabled, simple digital power can be disabled */
813 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
814 SGTL5000_LINREG_SIMPLE_POWERUP,
817 ldo->enabled = 1;
818 return 0;
821 static int ldo_regulator_disable(struct regulator_dev *dev)
823 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
824 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
826 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
827 SGTL5000_LINEREG_D_POWERUP,
830 /* clear voltage info */
831 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
832 SGTL5000_LINREG_VDDD_MASK, 0);
834 ldo->enabled = 0;
836 return 0;
839 static int ldo_regulator_get_voltage(struct regulator_dev *dev)
841 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
843 return ldo->voltage;
846 static struct regulator_ops ldo_regulator_ops = {
847 .is_enabled = ldo_regulator_is_enabled,
848 .enable = ldo_regulator_enable,
849 .disable = ldo_regulator_disable,
850 .get_voltage = ldo_regulator_get_voltage,
853 static int ldo_regulator_register(struct snd_soc_codec *codec,
854 struct regulator_init_data *init_data,
855 int voltage)
857 struct ldo_regulator *ldo;
858 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
859 struct regulator_config config = { };
861 ldo = kzalloc(sizeof(struct ldo_regulator), GFP_KERNEL);
863 if (!ldo)
864 return -ENOMEM;
866 ldo->desc.name = kstrdup(dev_name(codec->dev), GFP_KERNEL);
867 if (!ldo->desc.name) {
868 kfree(ldo);
869 dev_err(codec->dev, "failed to allocate decs name memory\n");
870 return -ENOMEM;
873 ldo->desc.type = REGULATOR_VOLTAGE;
874 ldo->desc.owner = THIS_MODULE;
875 ldo->desc.ops = &ldo_regulator_ops;
876 ldo->desc.n_voltages = 1;
878 ldo->codec_data = codec;
879 ldo->voltage = voltage;
881 config.dev = codec->dev;
882 config.driver_data = ldo;
883 config.init_data = init_data;
885 ldo->dev = regulator_register(&ldo->desc, &config);
886 if (IS_ERR(ldo->dev)) {
887 int ret = PTR_ERR(ldo->dev);
889 dev_err(codec->dev, "failed to register regulator\n");
890 kfree(ldo->desc.name);
891 kfree(ldo);
893 return ret;
895 sgtl5000->ldo = ldo;
897 return 0;
900 static int ldo_regulator_remove(struct snd_soc_codec *codec)
902 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
903 struct ldo_regulator *ldo = sgtl5000->ldo;
905 if (!ldo)
906 return 0;
908 regulator_unregister(ldo->dev);
909 kfree(ldo->desc.name);
910 kfree(ldo);
912 return 0;
914 #else
915 static int ldo_regulator_register(struct snd_soc_codec *codec,
916 struct regulator_init_data *init_data,
917 int voltage)
919 dev_err(codec->dev, "this setup needs regulator support in the kernel\n");
920 return -EINVAL;
923 static int ldo_regulator_remove(struct snd_soc_codec *codec)
925 return 0;
927 #endif
930 * set dac bias
931 * common state changes:
932 * startup:
933 * off --> standby --> prepare --> on
934 * standby --> prepare --> on
936 * stop:
937 * on --> prepare --> standby
939 static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
940 enum snd_soc_bias_level level)
942 int ret;
943 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
945 switch (level) {
946 case SND_SOC_BIAS_ON:
947 case SND_SOC_BIAS_PREPARE:
948 break;
949 case SND_SOC_BIAS_STANDBY:
950 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
951 ret = regulator_bulk_enable(
952 ARRAY_SIZE(sgtl5000->supplies),
953 sgtl5000->supplies);
954 if (ret)
955 return ret;
956 udelay(10);
958 regcache_cache_only(sgtl5000->regmap, false);
960 ret = regcache_sync(sgtl5000->regmap);
961 if (ret != 0) {
962 dev_err(codec->dev,
963 "Failed to restore cache: %d\n", ret);
965 regcache_cache_only(sgtl5000->regmap, true);
966 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
967 sgtl5000->supplies);
969 return ret;
973 break;
974 case SND_SOC_BIAS_OFF:
975 regcache_cache_only(sgtl5000->regmap, true);
976 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
977 sgtl5000->supplies);
978 break;
981 return 0;
984 #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
985 SNDRV_PCM_FMTBIT_S20_3LE |\
986 SNDRV_PCM_FMTBIT_S24_LE |\
987 SNDRV_PCM_FMTBIT_S32_LE)
989 static const struct snd_soc_dai_ops sgtl5000_ops = {
990 .hw_params = sgtl5000_pcm_hw_params,
991 .digital_mute = sgtl5000_digital_mute,
992 .set_fmt = sgtl5000_set_dai_fmt,
993 .set_sysclk = sgtl5000_set_dai_sysclk,
996 static struct snd_soc_dai_driver sgtl5000_dai = {
997 .name = "sgtl5000",
998 .playback = {
999 .stream_name = "Playback",
1000 .channels_min = 1,
1001 .channels_max = 2,
1003 * only support 8~48K + 96K,
1004 * TODO modify hw_param to support more
1006 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
1007 .formats = SGTL5000_FORMATS,
1009 .capture = {
1010 .stream_name = "Capture",
1011 .channels_min = 1,
1012 .channels_max = 2,
1013 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
1014 .formats = SGTL5000_FORMATS,
1016 .ops = &sgtl5000_ops,
1017 .symmetric_rates = 1,
1020 static bool sgtl5000_volatile(struct device *dev, unsigned int reg)
1022 switch (reg) {
1023 case SGTL5000_CHIP_ID:
1024 case SGTL5000_CHIP_ADCDAC_CTRL:
1025 case SGTL5000_CHIP_ANA_STATUS:
1026 return true;
1029 return false;
1032 static bool sgtl5000_readable(struct device *dev, unsigned int reg)
1034 switch (reg) {
1035 case SGTL5000_CHIP_ID:
1036 case SGTL5000_CHIP_DIG_POWER:
1037 case SGTL5000_CHIP_CLK_CTRL:
1038 case SGTL5000_CHIP_I2S_CTRL:
1039 case SGTL5000_CHIP_SSS_CTRL:
1040 case SGTL5000_CHIP_ADCDAC_CTRL:
1041 case SGTL5000_CHIP_DAC_VOL:
1042 case SGTL5000_CHIP_PAD_STRENGTH:
1043 case SGTL5000_CHIP_ANA_ADC_CTRL:
1044 case SGTL5000_CHIP_ANA_HP_CTRL:
1045 case SGTL5000_CHIP_ANA_CTRL:
1046 case SGTL5000_CHIP_LINREG_CTRL:
1047 case SGTL5000_CHIP_REF_CTRL:
1048 case SGTL5000_CHIP_MIC_CTRL:
1049 case SGTL5000_CHIP_LINE_OUT_CTRL:
1050 case SGTL5000_CHIP_LINE_OUT_VOL:
1051 case SGTL5000_CHIP_ANA_POWER:
1052 case SGTL5000_CHIP_PLL_CTRL:
1053 case SGTL5000_CHIP_CLK_TOP_CTRL:
1054 case SGTL5000_CHIP_ANA_STATUS:
1055 case SGTL5000_CHIP_SHORT_CTRL:
1056 case SGTL5000_CHIP_ANA_TEST2:
1057 case SGTL5000_DAP_CTRL:
1058 case SGTL5000_DAP_PEQ:
1059 case SGTL5000_DAP_BASS_ENHANCE:
1060 case SGTL5000_DAP_BASS_ENHANCE_CTRL:
1061 case SGTL5000_DAP_AUDIO_EQ:
1062 case SGTL5000_DAP_SURROUND:
1063 case SGTL5000_DAP_FLT_COEF_ACCESS:
1064 case SGTL5000_DAP_COEF_WR_B0_MSB:
1065 case SGTL5000_DAP_COEF_WR_B0_LSB:
1066 case SGTL5000_DAP_EQ_BASS_BAND0:
1067 case SGTL5000_DAP_EQ_BASS_BAND1:
1068 case SGTL5000_DAP_EQ_BASS_BAND2:
1069 case SGTL5000_DAP_EQ_BASS_BAND3:
1070 case SGTL5000_DAP_EQ_BASS_BAND4:
1071 case SGTL5000_DAP_MAIN_CHAN:
1072 case SGTL5000_DAP_MIX_CHAN:
1073 case SGTL5000_DAP_AVC_CTRL:
1074 case SGTL5000_DAP_AVC_THRESHOLD:
1075 case SGTL5000_DAP_AVC_ATTACK:
1076 case SGTL5000_DAP_AVC_DECAY:
1077 case SGTL5000_DAP_COEF_WR_B1_MSB:
1078 case SGTL5000_DAP_COEF_WR_B1_LSB:
1079 case SGTL5000_DAP_COEF_WR_B2_MSB:
1080 case SGTL5000_DAP_COEF_WR_B2_LSB:
1081 case SGTL5000_DAP_COEF_WR_A1_MSB:
1082 case SGTL5000_DAP_COEF_WR_A1_LSB:
1083 case SGTL5000_DAP_COEF_WR_A2_MSB:
1084 case SGTL5000_DAP_COEF_WR_A2_LSB:
1085 return true;
1087 default:
1088 return false;
1093 * This precalculated table contains all (vag_val * 100 / lo_calcntrl) results
1094 * to select an appropriate lo_vol_* in SGTL5000_CHIP_LINE_OUT_VOL
1095 * The calculatation was done for all possible register values which
1096 * is the array index and the following formula: 10^((idx−15)/40) * 100
1098 static const u8 vol_quot_table[] = {
1099 42, 45, 47, 50, 53, 56, 60, 63,
1100 67, 71, 75, 79, 84, 89, 94, 100,
1101 106, 112, 119, 126, 133, 141, 150, 158,
1102 168, 178, 188, 200, 211, 224, 237, 251
1106 * sgtl5000 has 3 internal power supplies:
1107 * 1. VAG, normally set to vdda/2
1108 * 2. charge pump, set to different value
1109 * according to voltage of vdda and vddio
1110 * 3. line out VAG, normally set to vddio/2
1112 * and should be set according to:
1113 * 1. vddd provided by external or not
1114 * 2. vdda and vddio voltage value. > 3.1v or not
1115 * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
1117 static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
1119 int vddd;
1120 int vdda;
1121 int vddio;
1122 u16 ana_pwr;
1123 u16 lreg_ctrl;
1124 int vag;
1125 int lo_vag;
1126 int vol_quot;
1127 int lo_vol;
1128 size_t i;
1129 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1131 vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
1132 vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
1133 vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer);
1135 vdda = vdda / 1000;
1136 vddio = vddio / 1000;
1137 vddd = vddd / 1000;
1139 if (vdda <= 0 || vddio <= 0 || vddd < 0) {
1140 dev_err(codec->dev, "regulator voltage not set correctly\n");
1142 return -EINVAL;
1145 /* according to datasheet, maximum voltage of supplies */
1146 if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
1147 dev_err(codec->dev,
1148 "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
1149 vdda, vddio, vddd);
1151 return -EINVAL;
1154 /* reset value */
1155 ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
1156 ana_pwr |= SGTL5000_DAC_STEREO |
1157 SGTL5000_ADC_STEREO |
1158 SGTL5000_REFTOP_POWERUP;
1159 lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
1161 if (vddio < 3100 && vdda < 3100) {
1162 /* enable internal oscillator used for charge pump */
1163 snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
1164 SGTL5000_INT_OSC_EN,
1165 SGTL5000_INT_OSC_EN);
1166 /* Enable VDDC charge pump */
1167 ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
1168 } else if (vddio >= 3100 && vdda >= 3100) {
1169 ana_pwr &= ~SGTL5000_VDDC_CHRGPMP_POWERUP;
1170 /* VDDC use VDDIO rail */
1171 lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
1172 lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
1173 SGTL5000_VDDC_MAN_ASSN_SHIFT;
1176 snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
1178 snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
1180 /* set voltage to register */
1181 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
1182 SGTL5000_LINREG_VDDD_MASK, 0x8);
1185 * if vddd linear reg has been enabled,
1186 * simple digital supply should be clear to get
1187 * proper VDDD voltage.
1189 if (ana_pwr & SGTL5000_LINEREG_D_POWERUP)
1190 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1191 SGTL5000_LINREG_SIMPLE_POWERUP,
1193 else
1194 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1195 SGTL5000_LINREG_SIMPLE_POWERUP |
1196 SGTL5000_STARTUP_POWERUP,
1200 * set ADC/DAC VAG to vdda / 2,
1201 * should stay in range (0.8v, 1.575v)
1203 vag = vdda / 2;
1204 if (vag <= SGTL5000_ANA_GND_BASE)
1205 vag = 0;
1206 else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
1207 (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
1208 vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
1209 else
1210 vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
1212 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1213 SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
1215 /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
1216 lo_vag = vddio / 2;
1217 if (lo_vag <= SGTL5000_LINE_OUT_GND_BASE)
1218 lo_vag = 0;
1219 else if (lo_vag >= SGTL5000_LINE_OUT_GND_BASE +
1220 SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
1221 lo_vag = SGTL5000_LINE_OUT_GND_MAX;
1222 else
1223 lo_vag = (lo_vag - SGTL5000_LINE_OUT_GND_BASE) /
1224 SGTL5000_LINE_OUT_GND_STP;
1226 snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
1227 SGTL5000_LINE_OUT_CURRENT_MASK |
1228 SGTL5000_LINE_OUT_GND_MASK,
1229 lo_vag << SGTL5000_LINE_OUT_GND_SHIFT |
1230 SGTL5000_LINE_OUT_CURRENT_360u <<
1231 SGTL5000_LINE_OUT_CURRENT_SHIFT);
1234 * Set lineout output level in range (0..31)
1235 * the same value is used for right and left channel
1237 * Searching for a suitable index solving this formula:
1238 * idx = 40 * log10(vag_val / lo_cagcntrl) + 15
1240 vol_quot = (vag * 100) / lo_vag;
1241 lo_vol = 0;
1242 for (i = 0; i < ARRAY_SIZE(vol_quot_table); i++) {
1243 if (vol_quot >= vol_quot_table[i])
1244 lo_vol = i;
1245 else
1246 break;
1249 snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_VOL,
1250 SGTL5000_LINE_OUT_VOL_RIGHT_MASK |
1251 SGTL5000_LINE_OUT_VOL_LEFT_MASK,
1252 lo_vol << SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT |
1253 lo_vol << SGTL5000_LINE_OUT_VOL_LEFT_SHIFT);
1255 return 0;
1258 static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec *codec)
1260 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1261 int ret;
1263 /* set internal ldo to 1.2v */
1264 ret = ldo_regulator_register(codec, &ldo_init_data, LDO_VOLTAGE);
1265 if (ret) {
1266 dev_err(codec->dev,
1267 "Failed to register vddd internal supplies: %d\n", ret);
1268 return ret;
1271 sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
1273 dev_info(codec->dev, "Using internal LDO instead of VDDD\n");
1274 return 0;
1277 static int sgtl5000_enable_regulators(struct snd_soc_codec *codec)
1279 int ret;
1280 int i;
1281 int external_vddd = 0;
1282 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1283 struct regulator *vddd;
1285 for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
1286 sgtl5000->supplies[i].supply = supply_names[i];
1288 /* External VDDD only works before revision 0x11 */
1289 if (sgtl5000->revision < 0x11) {
1290 vddd = regulator_get_optional(codec->dev, "VDDD");
1291 if (IS_ERR(vddd)) {
1292 /* See if it's just not registered yet */
1293 if (PTR_ERR(vddd) == -EPROBE_DEFER)
1294 return -EPROBE_DEFER;
1295 } else {
1296 external_vddd = 1;
1297 regulator_put(vddd);
1301 if (!external_vddd) {
1302 ret = sgtl5000_replace_vddd_with_ldo(codec);
1303 if (ret)
1304 return ret;
1307 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
1308 sgtl5000->supplies);
1309 if (ret)
1310 goto err_ldo_remove;
1312 ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
1313 sgtl5000->supplies);
1314 if (ret)
1315 goto err_regulator_free;
1317 /* wait for all power rails bring up */
1318 udelay(10);
1320 return 0;
1322 err_regulator_free:
1323 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1324 sgtl5000->supplies);
1325 err_ldo_remove:
1326 if (!external_vddd)
1327 ldo_regulator_remove(codec);
1328 return ret;
1332 static int sgtl5000_probe(struct snd_soc_codec *codec)
1334 int ret;
1335 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1337 ret = sgtl5000_enable_regulators(codec);
1338 if (ret)
1339 return ret;
1341 /* power up sgtl5000 */
1342 ret = sgtl5000_set_power_regs(codec);
1343 if (ret)
1344 goto err;
1346 /* enable small pop, introduce 400ms delay in turning off */
1347 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1348 SGTL5000_SMALL_POP, 1);
1350 /* disable short cut detector */
1351 snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
1354 * set i2s as default input of sound switch
1355 * TODO: add sound switch to control and dapm widge.
1357 snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
1358 SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
1359 snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
1360 SGTL5000_ADC_EN | SGTL5000_DAC_EN);
1362 /* enable dac volume ramp by default */
1363 snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
1364 SGTL5000_DAC_VOL_RAMP_EN |
1365 SGTL5000_DAC_MUTE_RIGHT |
1366 SGTL5000_DAC_MUTE_LEFT);
1368 snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
1370 snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
1371 SGTL5000_HP_ZCD_EN |
1372 SGTL5000_ADC_ZCD_EN);
1374 snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
1375 SGTL5000_BIAS_R_MASK,
1376 sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
1378 snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
1379 SGTL5000_BIAS_VOLT_MASK,
1380 sgtl5000->micbias_voltage << SGTL5000_BIAS_VOLT_SHIFT);
1382 * disable DAP
1383 * TODO:
1384 * Enable DAP in kcontrol and dapm.
1386 snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
1388 return 0;
1390 err:
1391 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1392 sgtl5000->supplies);
1393 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1394 sgtl5000->supplies);
1395 ldo_regulator_remove(codec);
1397 return ret;
1400 static int sgtl5000_remove(struct snd_soc_codec *codec)
1402 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1404 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1405 sgtl5000->supplies);
1406 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1407 sgtl5000->supplies);
1408 ldo_regulator_remove(codec);
1410 return 0;
1413 static struct snd_soc_codec_driver sgtl5000_driver = {
1414 .probe = sgtl5000_probe,
1415 .remove = sgtl5000_remove,
1416 .set_bias_level = sgtl5000_set_bias_level,
1417 .suspend_bias_off = true,
1418 .controls = sgtl5000_snd_controls,
1419 .num_controls = ARRAY_SIZE(sgtl5000_snd_controls),
1420 .dapm_widgets = sgtl5000_dapm_widgets,
1421 .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets),
1422 .dapm_routes = sgtl5000_dapm_routes,
1423 .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes),
1426 static const struct regmap_config sgtl5000_regmap = {
1427 .reg_bits = 16,
1428 .val_bits = 16,
1429 .reg_stride = 2,
1431 .max_register = SGTL5000_MAX_REG_OFFSET,
1432 .volatile_reg = sgtl5000_volatile,
1433 .readable_reg = sgtl5000_readable,
1435 .cache_type = REGCACHE_RBTREE,
1436 .reg_defaults = sgtl5000_reg_defaults,
1437 .num_reg_defaults = ARRAY_SIZE(sgtl5000_reg_defaults),
1441 * Write all the default values from sgtl5000_reg_defaults[] array into the
1442 * sgtl5000 registers, to make sure we always start with the sane registers
1443 * values as stated in the datasheet.
1445 * Since sgtl5000 does not have a reset line, nor a reset command in software,
1446 * we follow this approach to guarantee we always start from the default values
1447 * and avoid problems like, not being able to probe after an audio playback
1448 * followed by a system reset or a 'reboot' command in Linux
1450 static int sgtl5000_fill_defaults(struct sgtl5000_priv *sgtl5000)
1452 int i, ret, val, index;
1454 for (i = 0; i < ARRAY_SIZE(sgtl5000_reg_defaults); i++) {
1455 val = sgtl5000_reg_defaults[i].def;
1456 index = sgtl5000_reg_defaults[i].reg;
1457 ret = regmap_write(sgtl5000->regmap, index, val);
1458 if (ret)
1459 return ret;
1462 return 0;
1465 static int sgtl5000_i2c_probe(struct i2c_client *client,
1466 const struct i2c_device_id *id)
1468 struct sgtl5000_priv *sgtl5000;
1469 int ret, reg, rev;
1470 struct device_node *np = client->dev.of_node;
1471 u32 value;
1473 sgtl5000 = devm_kzalloc(&client->dev, sizeof(*sgtl5000), GFP_KERNEL);
1474 if (!sgtl5000)
1475 return -ENOMEM;
1477 sgtl5000->regmap = devm_regmap_init_i2c(client, &sgtl5000_regmap);
1478 if (IS_ERR(sgtl5000->regmap)) {
1479 ret = PTR_ERR(sgtl5000->regmap);
1480 dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
1481 return ret;
1484 sgtl5000->mclk = devm_clk_get(&client->dev, NULL);
1485 if (IS_ERR(sgtl5000->mclk)) {
1486 ret = PTR_ERR(sgtl5000->mclk);
1487 dev_err(&client->dev, "Failed to get mclock: %d\n", ret);
1488 /* Defer the probe to see if the clk will be provided later */
1489 if (ret == -ENOENT)
1490 return -EPROBE_DEFER;
1491 return ret;
1494 ret = clk_prepare_enable(sgtl5000->mclk);
1495 if (ret)
1496 return ret;
1498 /* Need 8 clocks before I2C accesses */
1499 udelay(1);
1501 /* read chip information */
1502 ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ID, &reg);
1503 if (ret)
1504 goto disable_clk;
1506 if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
1507 SGTL5000_PARTID_PART_ID) {
1508 dev_err(&client->dev,
1509 "Device with ID register %x is not a sgtl5000\n", reg);
1510 ret = -ENODEV;
1511 goto disable_clk;
1514 rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
1515 dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev);
1516 sgtl5000->revision = rev;
1518 if (np) {
1519 if (!of_property_read_u32(np,
1520 "micbias-resistor-k-ohms", &value)) {
1521 switch (value) {
1522 case SGTL5000_MICBIAS_OFF:
1523 sgtl5000->micbias_resistor = 0;
1524 break;
1525 case SGTL5000_MICBIAS_2K:
1526 sgtl5000->micbias_resistor = 1;
1527 break;
1528 case SGTL5000_MICBIAS_4K:
1529 sgtl5000->micbias_resistor = 2;
1530 break;
1531 case SGTL5000_MICBIAS_8K:
1532 sgtl5000->micbias_resistor = 3;
1533 break;
1534 default:
1535 sgtl5000->micbias_resistor = 2;
1536 dev_err(&client->dev,
1537 "Unsuitable MicBias resistor\n");
1539 } else {
1540 /* default is 4Kohms */
1541 sgtl5000->micbias_resistor = 2;
1543 if (!of_property_read_u32(np,
1544 "micbias-voltage-m-volts", &value)) {
1545 /* 1250mV => 0 */
1546 /* steps of 250mV */
1547 if ((value >= 1250) && (value <= 3000))
1548 sgtl5000->micbias_voltage = (value / 250) - 5;
1549 else {
1550 sgtl5000->micbias_voltage = 0;
1551 dev_err(&client->dev,
1552 "Unsuitable MicBias voltage\n");
1554 } else {
1555 sgtl5000->micbias_voltage = 0;
1559 i2c_set_clientdata(client, sgtl5000);
1561 /* Ensure sgtl5000 will start with sane register values */
1562 ret = sgtl5000_fill_defaults(sgtl5000);
1563 if (ret)
1564 goto disable_clk;
1566 ret = snd_soc_register_codec(&client->dev,
1567 &sgtl5000_driver, &sgtl5000_dai, 1);
1568 if (ret)
1569 goto disable_clk;
1571 return 0;
1573 disable_clk:
1574 clk_disable_unprepare(sgtl5000->mclk);
1575 return ret;
1578 static int sgtl5000_i2c_remove(struct i2c_client *client)
1580 struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
1582 snd_soc_unregister_codec(&client->dev);
1583 clk_disable_unprepare(sgtl5000->mclk);
1584 return 0;
1587 static const struct i2c_device_id sgtl5000_id[] = {
1588 {"sgtl5000", 0},
1592 MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
1594 static const struct of_device_id sgtl5000_dt_ids[] = {
1595 { .compatible = "fsl,sgtl5000", },
1596 { /* sentinel */ }
1598 MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids);
1600 static struct i2c_driver sgtl5000_i2c_driver = {
1601 .driver = {
1602 .name = "sgtl5000",
1603 .of_match_table = sgtl5000_dt_ids,
1605 .probe = sgtl5000_i2c_probe,
1606 .remove = sgtl5000_i2c_remove,
1607 .id_table = sgtl5000_id,
1610 module_i2c_driver(sgtl5000_i2c_driver);
1612 MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
1613 MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>");
1614 MODULE_LICENSE("GPL");