Merge branch 'fixes-for-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/coolone...
[linux/fpc-iii.git] / sound / soc / codecs / sta529.c
blob0c225cd569d231007b92c51bc7c8cf4baf02b9dc
1 /*
2 * ASoC codec driver for spear platform
4 * sound/soc/codecs/sta529.c -- spear ALSA Soc codec driver
6 * Copyright (C) 2012 ST Microelectronics
7 * Rajeev Kumar <rajeev-dlh.kumar@st.com>
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/clk.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/pm.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/initval.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/tlv.h>
32 /* STA529 Register offsets */
33 #define STA529_FFXCFG0 0x00
34 #define STA529_FFXCFG1 0x01
35 #define STA529_MVOL 0x02
36 #define STA529_LVOL 0x03
37 #define STA529_RVOL 0x04
38 #define STA529_TTF0 0x05
39 #define STA529_TTF1 0x06
40 #define STA529_TTP0 0x07
41 #define STA529_TTP1 0x08
42 #define STA529_S2PCFG0 0x0A
43 #define STA529_S2PCFG1 0x0B
44 #define STA529_P2SCFG0 0x0C
45 #define STA529_P2SCFG1 0x0D
46 #define STA529_PLLCFG0 0x14
47 #define STA529_PLLCFG1 0x15
48 #define STA529_PLLCFG2 0x16
49 #define STA529_PLLCFG3 0x17
50 #define STA529_PLLPFE 0x18
51 #define STA529_PLLST 0x19
52 #define STA529_ADCCFG 0x1E /*mic_select*/
53 #define STA529_CKOCFG 0x1F
54 #define STA529_MISC 0x20
55 #define STA529_PADST0 0x21
56 #define STA529_PADST1 0x22
57 #define STA529_FFXST 0x23
58 #define STA529_PWMIN1 0x2D
59 #define STA529_PWMIN2 0x2E
60 #define STA529_POWST 0x32
62 #define STA529_MAX_REGISTER 0x32
64 #define STA529_RATES (SNDRV_PCM_RATE_8000 | \
65 SNDRV_PCM_RATE_11025 | \
66 SNDRV_PCM_RATE_16000 | \
67 SNDRV_PCM_RATE_22050 | \
68 SNDRV_PCM_RATE_32000 | \
69 SNDRV_PCM_RATE_44100 | \
70 SNDRV_PCM_RATE_48000)
72 #define STA529_FORMAT (SNDRV_PCM_FMTBIT_S16_LE | \
73 SNDRV_PCM_FMTBIT_S24_LE | \
74 SNDRV_PCM_FMTBIT_S32_LE)
75 #define S2PC_VALUE 0x98
76 #define CLOCK_OUT 0x60
77 #define LEFT_J_DATA_FORMAT 0x10
78 #define I2S_DATA_FORMAT 0x12
79 #define RIGHT_J_DATA_FORMAT 0x14
80 #define CODEC_MUTE_VAL 0x80
82 #define POWER_CNTLMSAK 0x40
83 #define POWER_STDBY 0x40
84 #define FFX_MASK 0x80
85 #define FFX_OFF 0x80
86 #define POWER_UP 0x00
87 #define FFX_CLK_ENB 0x01
88 #define FFX_CLK_DIS 0x00
89 #define FFX_CLK_MSK 0x01
90 #define PLAY_FREQ_RANGE_MSK 0x70
91 #define CAP_FREQ_RANGE_MSK 0x0C
92 #define PDATA_LEN_MSK 0xC0
93 #define BCLK_TO_FS_MSK 0x30
94 #define AUDIO_MUTE_MSK 0x80
96 static const struct reg_default sta529_reg_defaults[] = {
97 { 0, 0x35 }, /* R0 - FFX Configuration reg 0 */
98 { 1, 0xc8 }, /* R1 - FFX Configuration reg 1 */
99 { 2, 0x50 }, /* R2 - Master Volume */
100 { 3, 0x00 }, /* R3 - Left Volume */
101 { 4, 0x00 }, /* R4 - Right Volume */
102 { 10, 0xb2 }, /* R10 - S2P Config Reg 0 */
103 { 11, 0x41 }, /* R11 - S2P Config Reg 1 */
104 { 12, 0x92 }, /* R12 - P2S Config Reg 0 */
105 { 13, 0x41 }, /* R13 - P2S Config Reg 1 */
106 { 30, 0xd2 }, /* R30 - ADC Config Reg */
107 { 31, 0x40 }, /* R31 - clock Out Reg */
108 { 32, 0x21 }, /* R32 - Misc Register */
111 struct sta529 {
112 struct regmap *regmap;
115 static bool sta529_readable(struct device *dev, unsigned int reg)
117 switch (reg) {
119 case STA529_FFXCFG0:
120 case STA529_FFXCFG1:
121 case STA529_MVOL:
122 case STA529_LVOL:
123 case STA529_RVOL:
124 case STA529_S2PCFG0:
125 case STA529_S2PCFG1:
126 case STA529_P2SCFG0:
127 case STA529_P2SCFG1:
128 case STA529_ADCCFG:
129 case STA529_CKOCFG:
130 case STA529_MISC:
131 return true;
132 default:
133 return false;
138 static const char *pwm_mode_text[] = { "Binary", "Headphone", "Ternary",
139 "Phase-shift"};
141 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -9150, 50, 0);
142 static const DECLARE_TLV_DB_SCALE(master_vol_tlv, -12750, 50, 0);
143 static const SOC_ENUM_SINGLE_DECL(pwm_src, STA529_FFXCFG1, 4, pwm_mode_text);
145 static const struct snd_kcontrol_new sta529_snd_controls[] = {
146 SOC_DOUBLE_R_TLV("Digital Playback Volume", STA529_LVOL, STA529_RVOL, 0,
147 127, 0, out_gain_tlv),
148 SOC_SINGLE_TLV("Master Playback Volume", STA529_MVOL, 0, 127, 1,
149 master_vol_tlv),
150 SOC_ENUM("PWM Select", pwm_src),
153 static int sta529_set_bias_level(struct snd_soc_codec *codec, enum
154 snd_soc_bias_level level)
156 struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
158 switch (level) {
159 case SND_SOC_BIAS_ON:
160 case SND_SOC_BIAS_PREPARE:
161 snd_soc_update_bits(codec, STA529_FFXCFG0, POWER_CNTLMSAK,
162 POWER_UP);
163 snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
164 FFX_CLK_ENB);
165 break;
166 case SND_SOC_BIAS_STANDBY:
167 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
168 regcache_sync(sta529->regmap);
169 snd_soc_update_bits(codec, STA529_FFXCFG0,
170 POWER_CNTLMSAK, POWER_STDBY);
171 /* Making FFX output to zero */
172 snd_soc_update_bits(codec, STA529_FFXCFG0, FFX_MASK,
173 FFX_OFF);
174 snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
175 FFX_CLK_DIS);
176 break;
177 case SND_SOC_BIAS_OFF:
178 break;
182 * store the label for powers down audio subsystem for suspend.This is
183 * used by soc core layer
185 codec->dapm.bias_level = level;
187 return 0;
191 static int sta529_hw_params(struct snd_pcm_substream *substream,
192 struct snd_pcm_hw_params *params,
193 struct snd_soc_dai *dai)
195 struct snd_soc_pcm_runtime *rtd = substream->private_data;
196 struct snd_soc_codec *codec = rtd->codec;
197 int pdata, play_freq_val, record_freq_val;
198 int bclk_to_fs_ratio;
200 switch (params_format(params)) {
201 case SNDRV_PCM_FORMAT_S16_LE:
202 pdata = 1;
203 bclk_to_fs_ratio = 0;
204 break;
205 case SNDRV_PCM_FORMAT_S24_LE:
206 pdata = 2;
207 bclk_to_fs_ratio = 1;
208 break;
209 case SNDRV_PCM_FORMAT_S32_LE:
210 pdata = 3;
211 bclk_to_fs_ratio = 2;
212 break;
213 default:
214 dev_err(codec->dev, "Unsupported format\n");
215 return -EINVAL;
218 switch (params_rate(params)) {
219 case 8000:
220 case 11025:
221 play_freq_val = 0;
222 record_freq_val = 2;
223 break;
224 case 16000:
225 case 22050:
226 play_freq_val = 1;
227 record_freq_val = 0;
228 break;
230 case 32000:
231 case 44100:
232 case 48000:
233 play_freq_val = 2;
234 record_freq_val = 0;
235 break;
236 default:
237 dev_err(codec->dev, "Unsupported rate\n");
238 return -EINVAL;
241 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
242 snd_soc_update_bits(codec, STA529_S2PCFG1, PDATA_LEN_MSK,
243 pdata << 6);
244 snd_soc_update_bits(codec, STA529_S2PCFG1, BCLK_TO_FS_MSK,
245 bclk_to_fs_ratio << 4);
246 snd_soc_update_bits(codec, STA529_MISC, PLAY_FREQ_RANGE_MSK,
247 play_freq_val << 4);
248 } else {
249 snd_soc_update_bits(codec, STA529_P2SCFG1, PDATA_LEN_MSK,
250 pdata << 6);
251 snd_soc_update_bits(codec, STA529_P2SCFG1, BCLK_TO_FS_MSK,
252 bclk_to_fs_ratio << 4);
253 snd_soc_update_bits(codec, STA529_MISC, CAP_FREQ_RANGE_MSK,
254 record_freq_val << 2);
257 return 0;
260 static int sta529_mute(struct snd_soc_dai *dai, int mute)
262 u8 val = 0;
264 if (mute)
265 val |= CODEC_MUTE_VAL;
267 snd_soc_update_bits(dai->codec, STA529_FFXCFG0, AUDIO_MUTE_MSK, val);
269 return 0;
272 static int sta529_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
274 struct snd_soc_codec *codec = codec_dai->codec;
275 u8 mode = 0;
277 /* interface format */
278 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
279 case SND_SOC_DAIFMT_LEFT_J:
280 mode = LEFT_J_DATA_FORMAT;
281 break;
282 case SND_SOC_DAIFMT_I2S:
283 mode = I2S_DATA_FORMAT;
284 break;
285 case SND_SOC_DAIFMT_RIGHT_J:
286 mode = RIGHT_J_DATA_FORMAT;
287 break;
288 default:
289 return -EINVAL;
292 snd_soc_update_bits(codec, STA529_S2PCFG0, 0x0D, mode);
294 return 0;
297 static const struct snd_soc_dai_ops sta529_dai_ops = {
298 .hw_params = sta529_hw_params,
299 .set_fmt = sta529_set_dai_fmt,
300 .digital_mute = sta529_mute,
303 static struct snd_soc_dai_driver sta529_dai = {
304 .name = "sta529-audio",
305 .playback = {
306 .stream_name = "Playback",
307 .channels_min = 2,
308 .channels_max = 2,
309 .rates = STA529_RATES,
310 .formats = STA529_FORMAT,
312 .capture = {
313 .stream_name = "Capture",
314 .channels_min = 2,
315 .channels_max = 2,
316 .rates = STA529_RATES,
317 .formats = STA529_FORMAT,
319 .ops = &sta529_dai_ops,
322 static int sta529_probe(struct snd_soc_codec *codec)
324 struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
325 int ret;
327 codec->control_data = sta529->regmap;
328 ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
330 if (ret < 0) {
331 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
332 return ret;
334 sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
336 return 0;
339 /* power down chip */
340 static int sta529_remove(struct snd_soc_codec *codec)
342 sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
344 return 0;
347 static int sta529_suspend(struct snd_soc_codec *codec)
349 sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
351 return 0;
354 static int sta529_resume(struct snd_soc_codec *codec)
356 sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
358 return 0;
361 struct snd_soc_codec_driver sta529_codec_driver = {
362 .probe = sta529_probe,
363 .remove = sta529_remove,
364 .set_bias_level = sta529_set_bias_level,
365 .suspend = sta529_suspend,
366 .resume = sta529_resume,
367 .controls = sta529_snd_controls,
368 .num_controls = ARRAY_SIZE(sta529_snd_controls),
371 static const struct regmap_config sta529_regmap = {
372 .reg_bits = 8,
373 .val_bits = 8,
375 .max_register = STA529_MAX_REGISTER,
376 .readable_reg = sta529_readable,
378 .cache_type = REGCACHE_RBTREE,
379 .reg_defaults = sta529_reg_defaults,
380 .num_reg_defaults = ARRAY_SIZE(sta529_reg_defaults),
383 static __devinit int sta529_i2c_probe(struct i2c_client *i2c,
384 const struct i2c_device_id *id)
386 struct sta529 *sta529;
387 int ret;
389 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
390 return -EINVAL;
392 sta529 = devm_kzalloc(&i2c->dev, sizeof(struct sta529), GFP_KERNEL);
393 if (sta529 == NULL) {
394 dev_err(&i2c->dev, "Can not allocate memory\n");
395 return -ENOMEM;
398 sta529->regmap = devm_regmap_init_i2c(i2c, &sta529_regmap);
399 if (IS_ERR(sta529->regmap)) {
400 ret = PTR_ERR(sta529->regmap);
401 dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
402 return ret;
405 i2c_set_clientdata(i2c, sta529);
407 ret = snd_soc_register_codec(&i2c->dev,
408 &sta529_codec_driver, &sta529_dai, 1);
409 if (ret != 0)
410 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
412 return ret;
415 static int __devexit sta529_i2c_remove(struct i2c_client *client)
417 snd_soc_unregister_codec(&client->dev);
419 return 0;
422 static const struct i2c_device_id sta529_i2c_id[] = {
423 { "sta529", 0 },
426 MODULE_DEVICE_TABLE(i2c, sta529_i2c_id);
428 static struct i2c_driver sta529_i2c_driver = {
429 .driver = {
430 .name = "sta529",
431 .owner = THIS_MODULE,
433 .probe = sta529_i2c_probe,
434 .remove = __devexit_p(sta529_i2c_remove),
435 .id_table = sta529_i2c_id,
438 module_i2c_driver(sta529_i2c_driver);
440 MODULE_DESCRIPTION("ASoC STA529 codec driver");
441 MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
442 MODULE_LICENSE("GPL");