aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/sgtl5000.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/sgtl5000.c')
-rw-r--r--sound/soc/codecs/sgtl5000.c1513
1 files changed, 1513 insertions, 0 deletions
diff --git a/sound/soc/codecs/sgtl5000.c b/sound/soc/codecs/sgtl5000.c
new file mode 100644
index 000000000000..1f7217f703ee
--- /dev/null
+++ b/sound/soc/codecs/sgtl5000.c
@@ -0,0 +1,1513 @@
1/*
2 * sgtl5000.c -- SGTL5000 ALSA SoC Audio driver
3 *
4 * Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
5 *
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 */
10
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>
30
31#include "sgtl5000.h"
32
33#define SGTL5000_DAP_REG_OFFSET 0x0100
34#define SGTL5000_MAX_REG_OFFSET 0x013A
35
36/* default value of sgtl5000 registers except DAP */
37static const u16 sgtl5000_regs[SGTL5000_MAX_REG_OFFSET >> 1] = {
38 0xa011, /* 0x0000, CHIP_ID. 11 stand for revison 17 */
39 0x0000, /* 0x0002, CHIP_DIG_POWER. */
40 0x0008, /* 0x0004, CHIP_CKL_CTRL */
41 0x0010, /* 0x0006, CHIP_I2S_CTRL */
42 0x0000, /* 0x0008, reserved */
43 0x0008, /* 0x000A, CHIP_SSS_CTRL */
44 0x0000, /* 0x000C, reserved */
45 0x020c, /* 0x000E, CHIP_ADCDAC_CTRL */
46 0x3c3c, /* 0x0010, CHIP_DAC_VOL */
47 0x0000, /* 0x0012, reserved */
48 0x015f, /* 0x0014, CHIP_PAD_STRENGTH */
49 0x0000, /* 0x0016, reserved */
50 0x0000, /* 0x0018, reserved */
51 0x0000, /* 0x001A, reserved */
52 0x0000, /* 0x001E, reserved */
53 0x0000, /* 0x0020, CHIP_ANA_ADC_CTRL */
54 0x1818, /* 0x0022, CHIP_ANA_HP_CTRL */
55 0x0111, /* 0x0024, CHIP_ANN_CTRL */
56 0x0000, /* 0x0026, CHIP_LINREG_CTRL */
57 0x0000, /* 0x0028, CHIP_REF_CTRL */
58 0x0000, /* 0x002A, CHIP_MIC_CTRL */
59 0x0000, /* 0x002C, CHIP_LINE_OUT_CTRL */
60 0x0404, /* 0x002E, CHIP_LINE_OUT_VOL */
61 0x7060, /* 0x0030, CHIP_ANA_POWER */
62 0x5000, /* 0x0032, CHIP_PLL_CTRL */
63 0x0000, /* 0x0034, CHIP_CLK_TOP_CTRL */
64 0x0000, /* 0x0036, CHIP_ANA_STATUS */
65 0x0000, /* 0x0038, reserved */
66 0x0000, /* 0x003A, CHIP_ANA_TEST2 */
67 0x0000, /* 0x003C, CHIP_SHORT_CTRL */
68 0x0000, /* reserved */
69};
70
71/* default value of dap registers */
72static const u16 sgtl5000_dap_regs[] = {
73 0x0000, /* 0x0100, DAP_CONTROL */
74 0x0000, /* 0x0102, DAP_PEQ */
75 0x0040, /* 0x0104, DAP_BASS_ENHANCE */
76 0x051f, /* 0x0106, DAP_BASS_ENHANCE_CTRL */
77 0x0000, /* 0x0108, DAP_AUDIO_EQ */
78 0x0040, /* 0x010A, DAP_SGTL_SURROUND */
79 0x0000, /* 0x010C, DAP_FILTER_COEF_ACCESS */
80 0x0000, /* 0x010E, DAP_COEF_WR_B0_MSB */
81 0x0000, /* 0x0110, DAP_COEF_WR_B0_LSB */
82 0x0000, /* 0x0112, reserved */
83 0x0000, /* 0x0114, reserved */
84 0x002f, /* 0x0116, DAP_AUDIO_EQ_BASS_BAND0 */
85 0x002f, /* 0x0118, DAP_AUDIO_EQ_BAND0 */
86 0x002f, /* 0x011A, DAP_AUDIO_EQ_BAND2 */
87 0x002f, /* 0x011C, DAP_AUDIO_EQ_BAND3 */
88 0x002f, /* 0x011E, DAP_AUDIO_EQ_TREBLE_BAND4 */
89 0x8000, /* 0x0120, DAP_MAIN_CHAN */
90 0x0000, /* 0x0122, DAP_MIX_CHAN */
91 0x0510, /* 0x0124, DAP_AVC_CTRL */
92 0x1473, /* 0x0126, DAP_AVC_THRESHOLD */
93 0x0028, /* 0x0128, DAP_AVC_ATTACK */
94 0x0050, /* 0x012A, DAP_AVC_DECAY */
95 0x0000, /* 0x012C, DAP_COEF_WR_B1_MSB */
96 0x0000, /* 0x012E, DAP_COEF_WR_B1_LSB */
97 0x0000, /* 0x0130, DAP_COEF_WR_B2_MSB */
98 0x0000, /* 0x0132, DAP_COEF_WR_B2_LSB */
99 0x0000, /* 0x0134, DAP_COEF_WR_A1_MSB */
100 0x0000, /* 0x0136, DAP_COEF_WR_A1_LSB */
101 0x0000, /* 0x0138, DAP_COEF_WR_A2_MSB */
102 0x0000, /* 0x013A, DAP_COEF_WR_A2_LSB */
103};
104
105/* regulator supplies for sgtl5000, VDDD is an optional external supply */
106enum sgtl5000_regulator_supplies {
107 VDDA,
108 VDDIO,
109 VDDD,
110 SGTL5000_SUPPLY_NUM
111};
112
113/* vddd is optional supply */
114static const char *supply_names[SGTL5000_SUPPLY_NUM] = {
115 "VDDA",
116 "VDDIO",
117 "VDDD"
118};
119
120#define LDO_CONSUMER_NAME "VDDD_LDO"
121#define LDO_VOLTAGE 1200000
122
123static struct regulator_consumer_supply ldo_consumer[] = {
124 REGULATOR_SUPPLY(LDO_CONSUMER_NAME, NULL),
125};
126
127static struct regulator_init_data ldo_init_data = {
128 .constraints = {
129 .min_uV = 850000,
130 .max_uV = 1600000,
131 .valid_modes_mask = REGULATOR_MODE_NORMAL,
132 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
133 },
134 .num_consumer_supplies = 1,
135 .consumer_supplies = &ldo_consumer[0],
136};
137
138/*
139 * sgtl5000 internal ldo regulator,
140 * enabled when VDDD not provided
141 */
142struct ldo_regulator {
143 struct regulator_desc desc;
144 struct regulator_dev *dev;
145 int voltage;
146 void *codec_data;
147 bool enabled;
148};
149
150/* sgtl5000 private structure in codec */
151struct sgtl5000_priv {
152 int sysclk; /* sysclk rate */
153 int master; /* i2s master or not */
154 int fmt; /* i2s data format */
155 struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM];
156 struct ldo_regulator *ldo;
157};
158
159/*
160 * mic_bias power on/off share the same register bits with
161 * output impedance of mic bias, when power on mic bias, we
162 * need reclaim it to impedance value.
163 * 0x0 = Powered off
164 * 0x1 = 2Kohm
165 * 0x2 = 4Kohm
166 * 0x3 = 8Kohm
167 */
168static int mic_bias_event(struct snd_soc_dapm_widget *w,
169 struct snd_kcontrol *kcontrol, int event)
170{
171 switch (event) {
172 case SND_SOC_DAPM_POST_PMU:
173 /* change mic bias resistor to 4Kohm */
174 snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
175 SGTL5000_BIAS_R_4k, SGTL5000_BIAS_R_4k);
176 break;
177
178 case SND_SOC_DAPM_PRE_PMD:
179 /*
180 * SGTL5000_BIAS_R_8k as mask to clean the two bits
181 * of mic bias and output impedance
182 */
183 snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
184 SGTL5000_BIAS_R_8k, 0);
185 break;
186 }
187 return 0;
188}
189
190/*
191 * using codec assist to small pop, hp_powerup or lineout_powerup
192 * should stay setting until vag_powerup is fully ramped down,
193 * vag fully ramped down require 400ms.
194 */
195static int small_pop_event(struct snd_soc_dapm_widget *w,
196 struct snd_kcontrol *kcontrol, int event)
197{
198 switch (event) {
199 case SND_SOC_DAPM_PRE_PMU:
200 snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
201 SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
202 break;
203
204 case SND_SOC_DAPM_PRE_PMD:
205 snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
206 SGTL5000_VAG_POWERUP, 0);
207 msleep(400);
208 break;
209 default:
210 break;
211 }
212
213 return 0;
214}
215
216/* input sources for ADC */
217static const char *adc_mux_text[] = {
218 "MIC_IN", "LINE_IN"
219};
220
221static const struct soc_enum adc_enum =
222SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL, 2, 2, adc_mux_text);
223
224static const struct snd_kcontrol_new adc_mux =
225SOC_DAPM_ENUM("Capture Mux", adc_enum);
226
227/* input sources for DAC */
228static const char *dac_mux_text[] = {
229 "DAC", "LINE_IN"
230};
231
232static const struct soc_enum dac_enum =
233SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL, 6, 2, dac_mux_text);
234
235static const struct snd_kcontrol_new dac_mux =
236SOC_DAPM_ENUM("Headphone Mux", dac_enum);
237
238static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = {
239 SND_SOC_DAPM_INPUT("LINE_IN"),
240 SND_SOC_DAPM_INPUT("MIC_IN"),
241
242 SND_SOC_DAPM_OUTPUT("HP_OUT"),
243 SND_SOC_DAPM_OUTPUT("LINE_OUT"),
244
245 SND_SOC_DAPM_MICBIAS_E("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0,
246 mic_bias_event,
247 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
248
249 SND_SOC_DAPM_PGA_E("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0,
250 small_pop_event,
251 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
252 SND_SOC_DAPM_PGA_E("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0,
253 small_pop_event,
254 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
255
256 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux),
257 SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &dac_mux),
258
259 /* aif for i2s input */
260 SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
261 0, SGTL5000_CHIP_DIG_POWER,
262 0, 0),
263
264 /* aif for i2s output */
265 SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
266 0, SGTL5000_CHIP_DIG_POWER,
267 1, 0),
268
269 SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0),
270
271 SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0),
272};
273
274/* routes for sgtl5000 */
275static const struct snd_soc_dapm_route audio_map[] = {
276 {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
277 {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
278
279 {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */
280 {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */
281
282 {"DAC", NULL, "AIFIN"}, /* i2s-->dac,skip audio mux */
283 {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
284 {"LO", NULL, "DAC"}, /* dac --> line_out */
285
286 {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
287 {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
288
289 {"LINE_OUT", NULL, "LO"},
290 {"HP_OUT", NULL, "HP"},
291};
292
293/* custom function to fetch info of PCM playback volume */
294static int dac_info_volsw(struct snd_kcontrol *kcontrol,
295 struct snd_ctl_elem_info *uinfo)
296{
297 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
298 uinfo->count = 2;
299 uinfo->value.integer.min = 0;
300 uinfo->value.integer.max = 0xfc - 0x3c;
301 return 0;
302}
303
304/*
305 * custom function to get of PCM playback volume
306 *
307 * dac volume register
308 * 15-------------8-7--------------0
309 * | R channel vol | L channel vol |
310 * -------------------------------
311 *
312 * PCM volume with 0.5017 dB steps from 0 to -90 dB
313 *
314 * register values map to dB
315 * 0x3B and less = Reserved
316 * 0x3C = 0 dB
317 * 0x3D = -0.5 dB
318 * 0xF0 = -90 dB
319 * 0xFC and greater = Muted
320 *
321 * register value map to userspace value
322 *
323 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
324 * ------------------------------
325 * userspace value 0xc0 0
326 */
327static int dac_get_volsw(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
329{
330 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
331 int reg;
332 int l;
333 int r;
334
335 reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
336
337 /* get left channel volume */
338 l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
339
340 /* get right channel volume */
341 r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
342
343 /* make sure value fall in (0x3c,0xfc) */
344 l = clamp(l, 0x3c, 0xfc);
345 r = clamp(r, 0x3c, 0xfc);
346
347 /* invert it and map to userspace value */
348 l = 0xfc - l;
349 r = 0xfc - r;
350
351 ucontrol->value.integer.value[0] = l;
352 ucontrol->value.integer.value[1] = r;
353
354 return 0;
355}
356
357/*
358 * custom function to put of PCM playback volume
359 *
360 * dac volume register
361 * 15-------------8-7--------------0
362 * | R channel vol | L channel vol |
363 * -------------------------------
364 *
365 * PCM volume with 0.5017 dB steps from 0 to -90 dB
366 *
367 * register values map to dB
368 * 0x3B and less = Reserved
369 * 0x3C = 0 dB
370 * 0x3D = -0.5 dB
371 * 0xF0 = -90 dB
372 * 0xFC and greater = Muted
373 *
374 * userspace value map to register value
375 *
376 * userspace value 0xc0 0
377 * ------------------------------
378 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
379 */
380static int dac_put_volsw(struct snd_kcontrol *kcontrol,
381 struct snd_ctl_elem_value *ucontrol)
382{
383 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
384 int reg;
385 int l;
386 int r;
387
388 l = ucontrol->value.integer.value[0];
389 r = ucontrol->value.integer.value[1];
390
391 /* make sure userspace volume fall in (0, 0xfc-0x3c) */
392 l = clamp(l, 0, 0xfc - 0x3c);
393 r = clamp(r, 0, 0xfc - 0x3c);
394
395 /* invert it, get the value can be set to register */
396 l = 0xfc - l;
397 r = 0xfc - r;
398
399 /* shift to get the register value */
400 reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
401 r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
402
403 snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg);
404
405 return 0;
406}
407
408static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
409
410/* tlv for mic gain, 0db 20db 30db 40db */
411static const unsigned int mic_gain_tlv[] = {
412 TLV_DB_RANGE_HEAD(4),
413 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
414 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
415};
416
417/* tlv for hp volume, -51.5db to 12.0db, step .5db */
418static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
419
420static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
421 /* SOC_DOUBLE_S8_TLV with invert */
422 {
423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
424 .name = "PCM Playback Volume",
425 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
426 SNDRV_CTL_ELEM_ACCESS_READWRITE,
427 .info = dac_info_volsw,
428 .get = dac_get_volsw,
429 .put = dac_put_volsw,
430 },
431
432 SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
433 SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
434 SGTL5000_CHIP_ANA_ADC_CTRL,
435 8, 2, 0, capture_6db_attenuate),
436 SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
437
438 SOC_DOUBLE_TLV("Headphone Playback Volume",
439 SGTL5000_CHIP_ANA_HP_CTRL,
440 0, 8,
441 0x7f, 1,
442 headphone_volume),
443 SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
444 5, 1, 0),
445
446 SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
447 0, 4, 0, mic_gain_tlv),
448};
449
450/* mute the codec used by alsa core */
451static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute)
452{
453 struct snd_soc_codec *codec = codec_dai->codec;
454 u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT;
455
456 snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
457 adcdac_ctrl, mute ? adcdac_ctrl : 0);
458
459 return 0;
460}
461
462/* set codec format */
463static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
464{
465 struct snd_soc_codec *codec = codec_dai->codec;
466 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
467 u16 i2sctl = 0;
468
469 sgtl5000->master = 0;
470 /*
471 * i2s clock and frame master setting.
472 * ONLY support:
473 * - clock and frame slave,
474 * - clock and frame master
475 */
476 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
477 case SND_SOC_DAIFMT_CBS_CFS:
478 break;
479 case SND_SOC_DAIFMT_CBM_CFM:
480 i2sctl |= SGTL5000_I2S_MASTER;
481 sgtl5000->master = 1;
482 break;
483 default:
484 return -EINVAL;
485 }
486
487 /* setting i2s data format */
488 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
489 case SND_SOC_DAIFMT_DSP_A:
490 i2sctl |= SGTL5000_I2S_MODE_PCM;
491 break;
492 case SND_SOC_DAIFMT_DSP_B:
493 i2sctl |= SGTL5000_I2S_MODE_PCM;
494 i2sctl |= SGTL5000_I2S_LRALIGN;
495 break;
496 case SND_SOC_DAIFMT_I2S:
497 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
498 break;
499 case SND_SOC_DAIFMT_RIGHT_J:
500 i2sctl |= SGTL5000_I2S_MODE_RJ;
501 i2sctl |= SGTL5000_I2S_LRPOL;
502 break;
503 case SND_SOC_DAIFMT_LEFT_J:
504 i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
505 i2sctl |= SGTL5000_I2S_LRALIGN;
506 break;
507 default:
508 return -EINVAL;
509 }
510
511 sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
512
513 /* Clock inversion */
514 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
515 case SND_SOC_DAIFMT_NB_NF:
516 break;
517 case SND_SOC_DAIFMT_IB_NF:
518 i2sctl |= SGTL5000_I2S_SCLK_INV;
519 break;
520 default:
521 return -EINVAL;
522 }
523
524 snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl);
525
526 return 0;
527}
528
529/* set codec sysclk */
530static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
531 int clk_id, unsigned int freq, int dir)
532{
533 struct snd_soc_codec *codec = codec_dai->codec;
534 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
535
536 switch (clk_id) {
537 case SGTL5000_SYSCLK:
538 sgtl5000->sysclk = freq;
539 break;
540 default:
541 return -EINVAL;
542 }
543
544 return 0;
545}
546
547/*
548 * set clock according to i2s frame clock,
549 * sgtl5000 provide 2 clock sources.
550 * 1. sys_mclk. sample freq can only configure to
551 * 1/256, 1/384, 1/512 of sys_mclk.
552 * 2. pll. can derive any audio clocks.
553 *
554 * clock setting rules:
555 * 1. in slave mode, only sys_mclk can use.
556 * 2. as constraint by sys_mclk, sample freq should
557 * set to 32k, 44.1k and above.
558 * 3. using sys_mclk prefer to pll to save power.
559 */
560static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
561{
562 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
563 int clk_ctl = 0;
564 int sys_fs; /* sample freq */
565
566 /*
567 * sample freq should be divided by frame clock,
568 * if frame clock lower than 44.1khz, sample feq should set to
569 * 32khz or 44.1khz.
570 */
571 switch (frame_rate) {
572 case 8000:
573 case 16000:
574 sys_fs = 32000;
575 break;
576 case 11025:
577 case 22050:
578 sys_fs = 44100;
579 break;
580 default:
581 sys_fs = frame_rate;
582 break;
583 }
584
585 /* set divided factor of frame clock */
586 switch (sys_fs / frame_rate) {
587 case 4:
588 clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
589 break;
590 case 2:
591 clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
592 break;
593 case 1:
594 clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
595 break;
596 default:
597 return -EINVAL;
598 }
599
600 /* set the sys_fs according to frame rate */
601 switch (sys_fs) {
602 case 32000:
603 clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
604 break;
605 case 44100:
606 clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
607 break;
608 case 48000:
609 clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
610 break;
611 case 96000:
612 clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
613 break;
614 default:
615 dev_err(codec->dev, "frame rate %d not supported\n",
616 frame_rate);
617 return -EINVAL;
618 }
619
620 /*
621 * calculate the divider of mclk/sample_freq,
622 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
623 */
624 switch (sgtl5000->sysclk / sys_fs) {
625 case 256:
626 clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
627 SGTL5000_MCLK_FREQ_SHIFT;
628 break;
629 case 384:
630 clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
631 SGTL5000_MCLK_FREQ_SHIFT;
632 break;
633 case 512:
634 clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
635 SGTL5000_MCLK_FREQ_SHIFT;
636 break;
637 default:
638 /* if mclk not satisify the divider, use pll */
639 if (sgtl5000->master) {
640 clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
641 SGTL5000_MCLK_FREQ_SHIFT;
642 } else {
643 dev_err(codec->dev,
644 "PLL not supported in slave mode\n");
645 return -EINVAL;
646 }
647 }
648
649 /* if using pll, please check manual 6.4.2 for detail */
650 if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
651 u64 out, t;
652 int div2;
653 int pll_ctl;
654 unsigned int in, int_div, frac_div;
655
656 if (sgtl5000->sysclk > 17000000) {
657 div2 = 1;
658 in = sgtl5000->sysclk / 2;
659 } else {
660 div2 = 0;
661 in = sgtl5000->sysclk;
662 }
663 if (sys_fs == 44100)
664 out = 180633600;
665 else
666 out = 196608000;
667 t = do_div(out, in);
668 int_div = out;
669 t *= 2048;
670 do_div(t, in);
671 frac_div = t;
672 pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
673 frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
674
675 snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
676 if (div2)
677 snd_soc_update_bits(codec,
678 SGTL5000_CHIP_CLK_TOP_CTRL,
679 SGTL5000_INPUT_FREQ_DIV2,
680 SGTL5000_INPUT_FREQ_DIV2);
681 else
682 snd_soc_update_bits(codec,
683 SGTL5000_CHIP_CLK_TOP_CTRL,
684 SGTL5000_INPUT_FREQ_DIV2,
685 0);
686
687 /* power up pll */
688 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
689 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
690 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
691 } else {
692 /* power down pll */
693 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
694 SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
695 0);
696 }
697
698 /* if using pll, clk_ctrl must be set after pll power up */
699 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
700
701 return 0;
702}
703
704/*
705 * Set PCM DAI bit size and sample rate.
706 * input: params_rate, params_fmt
707 */
708static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
709 struct snd_pcm_hw_params *params,
710 struct snd_soc_dai *dai)
711{
712 struct snd_soc_pcm_runtime *rtd = substream->private_data;
713 struct snd_soc_codec *codec = rtd->codec;
714 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
715 int channels = params_channels(params);
716 int i2s_ctl = 0;
717 int stereo;
718 int ret;
719
720 /* sysclk should already set */
721 if (!sgtl5000->sysclk) {
722 dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
723 return -EFAULT;
724 }
725
726 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
727 stereo = SGTL5000_DAC_STEREO;
728 else
729 stereo = SGTL5000_ADC_STEREO;
730
731 /* set mono to save power */
732 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
733 channels == 1 ? 0 : stereo);
734
735 /* set codec clock base on lrclk */
736 ret = sgtl5000_set_clock(codec, params_rate(params));
737 if (ret)
738 return ret;
739
740 /* set i2s data format */
741 switch (params_format(params)) {
742 case SNDRV_PCM_FORMAT_S16_LE:
743 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
744 return -EINVAL;
745 i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
746 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
747 SGTL5000_I2S_SCLKFREQ_SHIFT;
748 break;
749 case SNDRV_PCM_FORMAT_S20_3LE:
750 i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
751 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
752 SGTL5000_I2S_SCLKFREQ_SHIFT;
753 break;
754 case SNDRV_PCM_FORMAT_S24_LE:
755 i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
756 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
757 SGTL5000_I2S_SCLKFREQ_SHIFT;
758 break;
759 case SNDRV_PCM_FORMAT_S32_LE:
760 if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
761 return -EINVAL;
762 i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
763 i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
764 SGTL5000_I2S_SCLKFREQ_SHIFT;
765 break;
766 default:
767 return -EINVAL;
768 }
769
770 snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL, i2s_ctl, i2s_ctl);
771
772 return 0;
773}
774
775static int ldo_regulator_is_enabled(struct regulator_dev *dev)
776{
777 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
778
779 return ldo->enabled;
780}
781
782static int ldo_regulator_enable(struct regulator_dev *dev)
783{
784 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
785 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
786 int reg;
787
788 if (ldo_regulator_is_enabled(dev))
789 return 0;
790
791 /* set regulator value firstly */
792 reg = (1600 - ldo->voltage / 1000) / 50;
793 reg = clamp(reg, 0x0, 0xf);
794
795 /* amend the voltage value, unit: uV */
796 ldo->voltage = (1600 - reg * 50) * 1000;
797
798 /* set voltage to register */
799 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
800 (0x1 << 4) - 1, reg);
801
802 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
803 SGTL5000_LINEREG_D_POWERUP,
804 SGTL5000_LINEREG_D_POWERUP);
805
806 /* when internal ldo enabled, simple digital power can be disabled */
807 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
808 SGTL5000_LINREG_SIMPLE_POWERUP,
809 0);
810
811 ldo->enabled = 1;
812 return 0;
813}
814
815static int ldo_regulator_disable(struct regulator_dev *dev)
816{
817 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
818 struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
819
820 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
821 SGTL5000_LINEREG_D_POWERUP,
822 0);
823
824 /* clear voltage info */
825 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
826 (0x1 << 4) - 1, 0);
827
828 ldo->enabled = 0;
829
830 return 0;
831}
832
833static int ldo_regulator_get_voltage(struct regulator_dev *dev)
834{
835 struct ldo_regulator *ldo = rdev_get_drvdata(dev);
836
837 return ldo->voltage;
838}
839
840static struct regulator_ops ldo_regulator_ops = {
841 .is_enabled = ldo_regulator_is_enabled,
842 .enable = ldo_regulator_enable,
843 .disable = ldo_regulator_disable,
844 .get_voltage = ldo_regulator_get_voltage,
845};
846
847static int ldo_regulator_register(struct snd_soc_codec *codec,
848 struct regulator_init_data *init_data,
849 int voltage)
850{
851 struct ldo_regulator *ldo;
852
853 ldo = kzalloc(sizeof(struct ldo_regulator), GFP_KERNEL);
854
855 if (!ldo) {
856 dev_err(codec->dev, "failed to allocate ldo_regulator\n");
857 return -ENOMEM;
858 }
859
860 ldo->desc.name = kstrdup(dev_name(codec->dev), GFP_KERNEL);
861 if (!ldo->desc.name) {
862 kfree(ldo);
863 dev_err(codec->dev, "failed to allocate decs name memory\n");
864 return -ENOMEM;
865 }
866
867 ldo->desc.type = REGULATOR_VOLTAGE;
868 ldo->desc.owner = THIS_MODULE;
869 ldo->desc.ops = &ldo_regulator_ops;
870 ldo->desc.n_voltages = 1;
871
872 ldo->codec_data = codec;
873 ldo->voltage = voltage;
874
875 ldo->dev = regulator_register(&ldo->desc, codec->dev,
876 init_data, ldo);
877 if (IS_ERR(ldo->dev)) {
878 int ret = PTR_ERR(ldo->dev);
879
880 dev_err(codec->dev, "failed to register regulator\n");
881 kfree(ldo->desc.name);
882 kfree(ldo);
883
884 return ret;
885 }
886
887 return 0;
888}
889
890static int ldo_regulator_remove(struct snd_soc_codec *codec)
891{
892 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
893 struct ldo_regulator *ldo = sgtl5000->ldo;
894
895 if (!ldo)
896 return 0;
897
898 regulator_unregister(ldo->dev);
899 kfree(ldo->desc.name);
900 kfree(ldo);
901
902 return 0;
903}
904
905/*
906 * set dac bias
907 * common state changes:
908 * startup:
909 * off --> standby --> prepare --> on
910 * standby --> prepare --> on
911 *
912 * stop:
913 * on --> prepare --> standby
914 */
915static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
916 enum snd_soc_bias_level level)
917{
918 int ret;
919 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
920
921 switch (level) {
922 case SND_SOC_BIAS_ON:
923 case SND_SOC_BIAS_PREPARE:
924 break;
925 case SND_SOC_BIAS_STANDBY:
926 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
927 ret = regulator_bulk_enable(
928 ARRAY_SIZE(sgtl5000->supplies),
929 sgtl5000->supplies);
930 if (ret)
931 return ret;
932 udelay(10);
933 }
934
935 break;
936 case SND_SOC_BIAS_OFF:
937 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
938 sgtl5000->supplies);
939 break;
940 }
941
942 codec->dapm.bias_level = level;
943 return 0;
944}
945
946#define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
947 SNDRV_PCM_FMTBIT_S20_3LE |\
948 SNDRV_PCM_FMTBIT_S24_LE |\
949 SNDRV_PCM_FMTBIT_S32_LE)
950
951static struct snd_soc_dai_ops sgtl5000_ops = {
952 .hw_params = sgtl5000_pcm_hw_params,
953 .digital_mute = sgtl5000_digital_mute,
954 .set_fmt = sgtl5000_set_dai_fmt,
955 .set_sysclk = sgtl5000_set_dai_sysclk,
956};
957
958static struct snd_soc_dai_driver sgtl5000_dai = {
959 .name = "sgtl5000",
960 .playback = {
961 .stream_name = "Playback",
962 .channels_min = 1,
963 .channels_max = 2,
964 /*
965 * only support 8~48K + 96K,
966 * TODO modify hw_param to support more
967 */
968 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
969 .formats = SGTL5000_FORMATS,
970 },
971 .capture = {
972 .stream_name = "Capture",
973 .channels_min = 1,
974 .channels_max = 2,
975 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
976 .formats = SGTL5000_FORMATS,
977 },
978 .ops = &sgtl5000_ops,
979 .symmetric_rates = 1,
980};
981
982static int sgtl5000_volatile_register(struct snd_soc_codec *codec,
983 unsigned int reg)
984{
985 switch (reg) {
986 case SGTL5000_CHIP_ID:
987 case SGTL5000_CHIP_ADCDAC_CTRL:
988 case SGTL5000_CHIP_ANA_STATUS:
989 return 1;
990 }
991
992 return 0;
993}
994
995#ifdef CONFIG_SUSPEND
996static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
997{
998 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
999
1000 return 0;
1001}
1002
1003/*
1004 * restore all sgtl5000 registers,
1005 * since a big hole between dap and regular registers,
1006 * we will restore them respectively.
1007 */
1008static int sgtl5000_restore_regs(struct snd_soc_codec *codec)
1009{
1010 u16 *cache = codec->reg_cache;
1011 int i;
1012 int regular_regs = SGTL5000_CHIP_SHORT_CTRL >> 1;
1013
1014 /* restore regular registers */
1015 for (i = 0; i < regular_regs; i++) {
1016 int reg = i << 1;
1017
1018 /* this regs depends on the others */
1019 if (reg == SGTL5000_CHIP_ANA_POWER ||
1020 reg == SGTL5000_CHIP_CLK_CTRL ||
1021 reg == SGTL5000_CHIP_LINREG_CTRL ||
1022 reg == SGTL5000_CHIP_LINE_OUT_CTRL ||
1023 reg == SGTL5000_CHIP_CLK_CTRL)
1024 continue;
1025
1026 snd_soc_write(codec, reg, cache[i]);
1027 }
1028
1029 /* restore dap registers */
1030 for (i = SGTL5000_DAP_REG_OFFSET >> 1;
1031 i < SGTL5000_MAX_REG_OFFSET >> 1; i++) {
1032 int reg = i << 1;
1033
1034 snd_soc_write(codec, reg, cache[i]);
1035 }
1036
1037 /*
1038 * restore power and other regs according
1039 * to set_power() and set_clock()
1040 */
1041 snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL,
1042 cache[SGTL5000_CHIP_LINREG_CTRL >> 1]);
1043
1044 snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER,
1045 cache[SGTL5000_CHIP_ANA_POWER >> 1]);
1046
1047 snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL,
1048 cache[SGTL5000_CHIP_CLK_CTRL >> 1]);
1049
1050 snd_soc_write(codec, SGTL5000_CHIP_REF_CTRL,
1051 cache[SGTL5000_CHIP_REF_CTRL >> 1]);
1052
1053 snd_soc_write(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
1054 cache[SGTL5000_CHIP_LINE_OUT_CTRL >> 1]);
1055 return 0;
1056}
1057
1058static int sgtl5000_resume(struct snd_soc_codec *codec)
1059{
1060 /* Bring the codec back up to standby to enable regulators */
1061 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1062
1063 /* Restore registers by cached in memory */
1064 sgtl5000_restore_regs(codec);
1065 return 0;
1066}
1067#else
1068#define sgtl5000_suspend NULL
1069#define sgtl5000_resume NULL
1070#endif /* CONFIG_SUSPEND */
1071
1072/*
1073 * sgtl5000 has 3 internal power supplies:
1074 * 1. VAG, normally set to vdda/2
1075 * 2. chargepump, set to different value
1076 * according to voltage of vdda and vddio
1077 * 3. line out VAG, normally set to vddio/2
1078 *
1079 * and should be set according to:
1080 * 1. vddd provided by external or not
1081 * 2. vdda and vddio voltage value. > 3.1v or not
1082 * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
1083 */
1084static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
1085{
1086 int vddd;
1087 int vdda;
1088 int vddio;
1089 u16 ana_pwr;
1090 u16 lreg_ctrl;
1091 int vag;
1092 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1093
1094 vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
1095 vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
1096 vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer);
1097
1098 vdda = vdda / 1000;
1099 vddio = vddio / 1000;
1100 vddd = vddd / 1000;
1101
1102 if (vdda <= 0 || vddio <= 0 || vddd < 0) {
1103 dev_err(codec->dev, "regulator voltage not set correctly\n");
1104
1105 return -EINVAL;
1106 }
1107
1108 /* according to datasheet, maximum voltage of supplies */
1109 if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
1110 dev_err(codec->dev,
1111 "exceed max voltage vdda %dmv vddio %dma vddd %dma\n",
1112 vdda, vddio, vddd);
1113
1114 return -EINVAL;
1115 }
1116
1117 /* reset value */
1118 ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
1119 ana_pwr |= SGTL5000_DAC_STEREO |
1120 SGTL5000_ADC_STEREO |
1121 SGTL5000_REFTOP_POWERUP;
1122 lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
1123
1124 if (vddio < 3100 && vdda < 3100) {
1125 /* enable internal oscillator used for charge pump */
1126 snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
1127 SGTL5000_INT_OSC_EN,
1128 SGTL5000_INT_OSC_EN);
1129 /* Enable VDDC charge pump */
1130 ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
1131 } else if (vddio >= 3100 && vdda >= 3100) {
1132 /*
1133 * if vddio and vddd > 3.1v,
1134 * charge pump should be clean before set ana_pwr
1135 */
1136 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1137 SGTL5000_VDDC_CHRGPMP_POWERUP, 0);
1138
1139 /* VDDC use VDDIO rail */
1140 lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
1141 lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
1142 SGTL5000_VDDC_MAN_ASSN_SHIFT;
1143 }
1144
1145 snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
1146
1147 snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
1148
1149 /* set voltage to register */
1150 snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
1151 (0x1 << 4) - 1, 0x8);
1152
1153 /*
1154 * if vddd linear reg has been enabled,
1155 * simple digital supply should be clear to get
1156 * proper VDDD voltage.
1157 */
1158 if (ana_pwr & SGTL5000_LINEREG_D_POWERUP)
1159 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1160 SGTL5000_LINREG_SIMPLE_POWERUP,
1161 0);
1162 else
1163 snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
1164 SGTL5000_LINREG_SIMPLE_POWERUP |
1165 SGTL5000_STARTUP_POWERUP,
1166 0);
1167
1168 /*
1169 * set ADC/DAC VAG to vdda / 2,
1170 * should stay in range (0.8v, 1.575v)
1171 */
1172 vag = vdda / 2;
1173 if (vag <= SGTL5000_ANA_GND_BASE)
1174 vag = 0;
1175 else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
1176 (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
1177 vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
1178 else
1179 vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
1180
1181 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1182 vag << SGTL5000_ANA_GND_SHIFT,
1183 vag << SGTL5000_ANA_GND_SHIFT);
1184
1185 /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
1186 vag = vddio / 2;
1187 if (vag <= SGTL5000_LINE_OUT_GND_BASE)
1188 vag = 0;
1189 else if (vag >= SGTL5000_LINE_OUT_GND_BASE +
1190 SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
1191 vag = SGTL5000_LINE_OUT_GND_MAX;
1192 else
1193 vag = (vag - SGTL5000_LINE_OUT_GND_BASE) /
1194 SGTL5000_LINE_OUT_GND_STP;
1195
1196 snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
1197 vag << SGTL5000_LINE_OUT_GND_SHIFT |
1198 SGTL5000_LINE_OUT_CURRENT_360u <<
1199 SGTL5000_LINE_OUT_CURRENT_SHIFT,
1200 vag << SGTL5000_LINE_OUT_GND_SHIFT |
1201 SGTL5000_LINE_OUT_CURRENT_360u <<
1202 SGTL5000_LINE_OUT_CURRENT_SHIFT);
1203
1204 return 0;
1205}
1206
1207static int sgtl5000_enable_regulators(struct snd_soc_codec *codec)
1208{
1209 u16 reg;
1210 int ret;
1211 int rev;
1212 int i;
1213 int external_vddd = 0;
1214 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1215
1216 for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
1217 sgtl5000->supplies[i].supply = supply_names[i];
1218
1219 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
1220 sgtl5000->supplies);
1221 if (!ret)
1222 external_vddd = 1;
1223 else {
1224 /* set internal ldo to 1.2v */
1225 int voltage = LDO_VOLTAGE;
1226
1227 ret = ldo_regulator_register(codec, &ldo_init_data, voltage);
1228 if (ret) {
1229 dev_err(codec->dev,
1230 "Failed to register vddd internal supplies: %d\n",
1231 ret);
1232 return ret;
1233 }
1234
1235 sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
1236
1237 ret = regulator_bulk_get(codec->dev,
1238 ARRAY_SIZE(sgtl5000->supplies),
1239 sgtl5000->supplies);
1240
1241 if (ret) {
1242 ldo_regulator_remove(codec);
1243 dev_err(codec->dev,
1244 "Failed to request supplies: %d\n", ret);
1245
1246 return ret;
1247 }
1248 }
1249
1250 ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
1251 sgtl5000->supplies);
1252 if (ret)
1253 goto err_regulator_free;
1254
1255 /* wait for all power rails bring up */
1256 udelay(10);
1257
1258 /* read chip information */
1259 reg = snd_soc_read(codec, SGTL5000_CHIP_ID);
1260 if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
1261 SGTL5000_PARTID_PART_ID) {
1262 dev_err(codec->dev,
1263 "Device with ID register %x is not a sgtl5000\n", reg);
1264 ret = -ENODEV;
1265 goto err_regulator_disable;
1266 }
1267
1268 rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
1269 dev_info(codec->dev, "sgtl5000 revision %d\n", rev);
1270
1271 /*
1272 * workaround for revision 0x11 and later,
1273 * roll back to use internal LDO
1274 */
1275 if (external_vddd && rev >= 0x11) {
1276 int voltage = LDO_VOLTAGE;
1277 /* disable all regulator first */
1278 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1279 sgtl5000->supplies);
1280 /* free VDDD regulator */
1281 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1282 sgtl5000->supplies);
1283
1284 ret = ldo_regulator_register(codec, &ldo_init_data, voltage);
1285 if (ret)
1286 return ret;
1287
1288 sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
1289
1290 ret = regulator_bulk_get(codec->dev,
1291 ARRAY_SIZE(sgtl5000->supplies),
1292 sgtl5000->supplies);
1293 if (ret) {
1294 ldo_regulator_remove(codec);
1295 dev_err(codec->dev,
1296 "Failed to request supplies: %d\n", ret);
1297
1298 return ret;
1299 }
1300
1301 ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
1302 sgtl5000->supplies);
1303 if (ret)
1304 goto err_regulator_free;
1305
1306 /* wait for all power rails bring up */
1307 udelay(10);
1308 }
1309
1310 return 0;
1311
1312err_regulator_disable:
1313 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1314 sgtl5000->supplies);
1315err_regulator_free:
1316 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1317 sgtl5000->supplies);
1318 if (external_vddd)
1319 ldo_regulator_remove(codec);
1320 return ret;
1321
1322}
1323
1324static int sgtl5000_probe(struct snd_soc_codec *codec)
1325{
1326 int ret;
1327 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1328
1329 /* setup i2c data ops */
1330 ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
1331 if (ret < 0) {
1332 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1333 return ret;
1334 }
1335
1336 ret = sgtl5000_enable_regulators(codec);
1337 if (ret)
1338 return ret;
1339
1340 /* power up sgtl5000 */
1341 ret = sgtl5000_set_power_regs(codec);
1342 if (ret)
1343 goto err;
1344
1345 /* enable small pop, introduce 400ms delay in turning off */
1346 snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
1347 SGTL5000_SMALL_POP,
1348 SGTL5000_SMALL_POP);
1349
1350 /* disable short cut detector */
1351 snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
1352
1353 /*
1354 * set i2s as default input of sound switch
1355 * TODO: add sound switch to control and dapm widge.
1356 */
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);
1361
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);
1367
1368 snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
1369
1370 snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
1371 SGTL5000_HP_ZCD_EN |
1372 SGTL5000_ADC_ZCD_EN);
1373
1374 snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 0);
1375
1376 /*
1377 * disable DAP
1378 * TODO:
1379 * Enable DAP in kcontrol and dapm.
1380 */
1381 snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
1382
1383 /* leading to standby state */
1384 ret = sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1385 if (ret)
1386 goto err;
1387
1388 snd_soc_add_controls(codec, sgtl5000_snd_controls,
1389 ARRAY_SIZE(sgtl5000_snd_controls));
1390
1391 snd_soc_dapm_new_controls(&codec->dapm, sgtl5000_dapm_widgets,
1392 ARRAY_SIZE(sgtl5000_dapm_widgets));
1393
1394 snd_soc_dapm_add_routes(&codec->dapm, audio_map,
1395 ARRAY_SIZE(audio_map));
1396
1397 snd_soc_dapm_new_widgets(&codec->dapm);
1398
1399 return 0;
1400
1401err:
1402 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1403 sgtl5000->supplies);
1404 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1405 sgtl5000->supplies);
1406 ldo_regulator_remove(codec);
1407
1408 return ret;
1409}
1410
1411static int sgtl5000_remove(struct snd_soc_codec *codec)
1412{
1413 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1414
1415 sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
1416
1417 regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
1418 sgtl5000->supplies);
1419 regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
1420 sgtl5000->supplies);
1421 ldo_regulator_remove(codec);
1422
1423 return 0;
1424}
1425
1426static struct snd_soc_codec_driver sgtl5000_driver = {
1427 .probe = sgtl5000_probe,
1428 .remove = sgtl5000_remove,
1429 .suspend = sgtl5000_suspend,
1430 .resume = sgtl5000_resume,
1431 .set_bias_level = sgtl5000_set_bias_level,
1432 .reg_cache_size = ARRAY_SIZE(sgtl5000_regs),
1433 .reg_word_size = sizeof(u16),
1434 .reg_cache_step = 2,
1435 .reg_cache_default = sgtl5000_regs,
1436 .volatile_register = sgtl5000_volatile_register,
1437};
1438
1439static __devinit int sgtl5000_i2c_probe(struct i2c_client *client,
1440 const struct i2c_device_id *id)
1441{
1442 struct sgtl5000_priv *sgtl5000;
1443 int ret;
1444
1445 sgtl5000 = kzalloc(sizeof(struct sgtl5000_priv), GFP_KERNEL);
1446 if (!sgtl5000)
1447 return -ENOMEM;
1448
1449 /*
1450 * copy DAP default values to default value array.
1451 * sgtl5000 register space has a big hole, merge it
1452 * at init phase makes life easy.
1453 * FIXME: should we drop 'const' of sgtl5000_regs?
1454 */
1455 memcpy((void *)(&sgtl5000_regs[0] + (SGTL5000_DAP_REG_OFFSET >> 1)),
1456 sgtl5000_dap_regs,
1457 SGTL5000_MAX_REG_OFFSET - SGTL5000_DAP_REG_OFFSET);
1458
1459 i2c_set_clientdata(client, sgtl5000);
1460
1461 ret = snd_soc_register_codec(&client->dev,
1462 &sgtl5000_driver, &sgtl5000_dai, 1);
1463 if (ret) {
1464 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1465 kfree(sgtl5000);
1466 return ret;
1467 }
1468
1469 return 0;
1470}
1471
1472static __devexit int sgtl5000_i2c_remove(struct i2c_client *client)
1473{
1474 struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
1475
1476 snd_soc_unregister_codec(&client->dev);
1477
1478 kfree(sgtl5000);
1479 return 0;
1480}
1481
1482static const struct i2c_device_id sgtl5000_id[] = {
1483 {"sgtl5000", 0},
1484 {},
1485};
1486
1487MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
1488
1489static struct i2c_driver sgtl5000_i2c_driver = {
1490 .driver = {
1491 .name = "sgtl5000",
1492 .owner = THIS_MODULE,
1493 },
1494 .probe = sgtl5000_i2c_probe,
1495 .remove = __devexit_p(sgtl5000_i2c_remove),
1496 .id_table = sgtl5000_id,
1497};
1498
1499static int __init sgtl5000_modinit(void)
1500{
1501 return i2c_add_driver(&sgtl5000_i2c_driver);
1502}
1503module_init(sgtl5000_modinit);
1504
1505static void __exit sgtl5000_exit(void)
1506{
1507 i2c_del_driver(&sgtl5000_i2c_driver);
1508}
1509module_exit(sgtl5000_exit);
1510
1511MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
1512MODULE_AUTHOR("Zeng Zhaoming <zhaoming.zeng@freescale.com>");
1513MODULE_LICENSE("GPL");