include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / codecs / da7210.c
CommitLineData
98615454
KM
1/*
2 * DA7210 ALSA Soc codec driver
3 *
4 * Copyright (c) 2009 Dialog Semiconductor
5 * Written by David Chen <Dajun.chen@diasemi.com>
6 *
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/pm.h>
24#include <linux/i2c.h>
25#include <linux/platform_device.h>
5a0e3ad6 26#include <linux/slab.h>
98615454
KM
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
31#include <sound/soc-dapm.h>
32#include <sound/tlv.h>
33#include <sound/initval.h>
34#include <asm/div64.h>
35
36#include "da7210.h"
37
38/* DA7210 register space */
39#define DA7210_STATUS 0x02
40#define DA7210_STARTUP1 0x03
41#define DA7210_MIC_L 0x07
42#define DA7210_MIC_R 0x08
43#define DA7210_INMIX_L 0x0D
44#define DA7210_INMIX_R 0x0E
45#define DA7210_ADC_HPF 0x0F
46#define DA7210_ADC 0x10
47#define DA7210_DAC_HPF 0x14
48#define DA7210_DAC_L 0x15
49#define DA7210_DAC_R 0x16
50#define DA7210_DAC_SEL 0x17
51#define DA7210_OUTMIX_L 0x1C
52#define DA7210_OUTMIX_R 0x1D
53#define DA7210_HP_L_VOL 0x21
54#define DA7210_HP_R_VOL 0x22
55#define DA7210_HP_CFG 0x23
56#define DA7210_DAI_SRC_SEL 0x25
57#define DA7210_DAI_CFG1 0x26
58#define DA7210_DAI_CFG3 0x28
59#define DA7210_PLL_DIV3 0x2B
60#define DA7210_PLL 0x2C
61
62/* STARTUP1 bit fields */
63#define DA7210_SC_MST_EN (1 << 0)
64
65/* MIC_L bit fields */
66#define DA7210_MICBIAS_EN (1 << 6)
67#define DA7210_MIC_L_EN (1 << 7)
68
69/* MIC_R bit fields */
70#define DA7210_MIC_R_EN (1 << 7)
71
72/* INMIX_L bit fields */
73#define DA7210_IN_L_EN (1 << 7)
74
75/* INMIX_R bit fields */
76#define DA7210_IN_R_EN (1 << 7)
77
78/* ADC_HPF bit fields */
79#define DA7210_ADC_VOICE_EN (1 << 7)
80
81/* ADC bit fields */
82#define DA7210_ADC_L_EN (1 << 3)
83#define DA7210_ADC_R_EN (1 << 7)
84
c2151433
MB
85/* DAC_HPF fields */
86#define DA7210_DAC_VOICE_EN (1 << 7)
87
98615454
KM
88/* DAC_SEL bit fields */
89#define DA7210_DAC_L_SRC_DAI_L (4 << 0)
90#define DA7210_DAC_L_EN (1 << 3)
91#define DA7210_DAC_R_SRC_DAI_R (5 << 4)
92#define DA7210_DAC_R_EN (1 << 7)
93
94/* OUTMIX_L bit fields */
95#define DA7210_OUT_L_EN (1 << 7)
96
97/* OUTMIX_R bit fields */
98#define DA7210_OUT_R_EN (1 << 7)
99
100/* HP_CFG bit fields */
101#define DA7210_HP_2CAP_MODE (1 << 1)
102#define DA7210_HP_SENSE_EN (1 << 2)
103#define DA7210_HP_L_EN (1 << 3)
104#define DA7210_HP_MODE (1 << 6)
105#define DA7210_HP_R_EN (1 << 7)
106
107/* DAI_SRC_SEL bit fields */
108#define DA7210_DAI_OUT_L_SRC (6 << 0)
109#define DA7210_DAI_OUT_R_SRC (7 << 4)
110
111/* DAI_CFG1 bit fields */
112#define DA7210_DAI_WORD_S16_LE (0 << 0)
113#define DA7210_DAI_WORD_S24_LE (2 << 0)
114#define DA7210_DAI_FLEN_64BIT (1 << 2)
115#define DA7210_DAI_MODE_MASTER (1 << 7)
116
117/* DAI_CFG3 bit fields */
118#define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
119#define DA7210_DAI_OE (1 << 3)
120#define DA7210_DAI_EN (1 << 7)
121
122/*PLL_DIV3 bit fields */
123#define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
124#define DA7210_PLL_BYP (1 << 6)
125
126/* PLL bit fields */
127#define DA7210_PLL_FS_48000 (11 << 0)
128
129#define DA7210_VERSION "0.0.1"
130
131/* Codec private data */
132struct da7210_priv {
133 struct snd_soc_codec codec;
134};
135
136static struct snd_soc_codec *da7210_codec;
137
138/*
139 * Register cache
140 */
141static const u8 da7210_reg[] = {
142 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
144 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
145 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
146 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
147 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
148 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
149 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
158 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
159 0x00, /* R88 */
160};
161
162/*
163 * Read da7210 register cache
164 */
165static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg)
166{
167 u8 *cache = codec->reg_cache;
168 BUG_ON(reg > ARRAY_SIZE(da7210_reg));
169 return cache[reg];
170}
171
172/*
173 * Write to the da7210 register space
174 */
175static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value)
176{
177 u8 *cache = codec->reg_cache;
178 u8 data[2];
179
180 BUG_ON(codec->volatile_register);
181
182 data[0] = reg & 0xff;
183 data[1] = value & 0xff;
184
185 if (reg >= codec->reg_cache_size)
186 return -EIO;
187
188 if (2 != codec->hw_write(codec->control_data, data, 2))
189 return -EIO;
190
191 cache[reg] = value;
192 return 0;
193}
194
195/*
196 * Read from the da7210 register space.
197 */
198static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg)
199{
200 if (DA7210_STATUS == reg)
201 return i2c_smbus_read_byte_data(codec->control_data, reg);
202
203 return da7210_read_reg_cache(codec, reg);
204}
205
206static int da7210_startup(struct snd_pcm_substream *substream,
207 struct snd_soc_dai *dai)
208{
209 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
210 struct snd_soc_codec *codec = dai->codec;
211
212 if (is_play) {
213 /* PlayBack Volume 40 */
214 snd_soc_update_bits(codec, DA7210_HP_L_VOL, 0x3F, 40);
215 snd_soc_update_bits(codec, DA7210_HP_R_VOL, 0x3F, 40);
216
217 /* Enable Out */
218 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
219 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
220
221 } else {
222 /* Volume 7 */
223 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
224 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
225
226 /* Enable Mic */
227 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
228 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
229 }
230
231 return 0;
232}
233
234/*
235 * Set PCM DAI word length.
236 */
237static int da7210_hw_params(struct snd_pcm_substream *substream,
238 struct snd_pcm_hw_params *params,
239 struct snd_soc_dai *dai)
240{
241 struct snd_soc_pcm_runtime *rtd = substream->private_data;
242 struct snd_soc_device *socdev = rtd->socdev;
243 struct snd_soc_codec *codec = socdev->card->codec;
244 u32 dai_cfg1;
245 u32 reg, mask;
246
247 /* set DAI source to Left and Right ADC */
248 da7210_write(codec, DA7210_DAI_SRC_SEL,
249 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
250
251 /* Enable DAI */
252 da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
253
254 dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1);
255
256 switch (params_format(params)) {
257 case SNDRV_PCM_FORMAT_S16_LE:
258 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
259 break;
260 case SNDRV_PCM_FORMAT_S24_LE:
261 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
262 break;
263 default:
264 return -EINVAL;
265 }
266
267 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
268
269 /* FIXME
270 *
271 * It support 48K only now
272 */
273 switch (params_rate(params)) {
274 case 48000:
275 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
276 reg = DA7210_DAC_HPF;
277 mask = DA7210_DAC_VOICE_EN;
278 } else {
279 reg = DA7210_ADC_HPF;
280 mask = DA7210_ADC_VOICE_EN;
281 }
282 break;
283 default:
284 return -EINVAL;
285 }
286
287 snd_soc_update_bits(codec, reg, mask, 0);
288
289 return 0;
290}
291
292/*
293 * Set DAI mode and Format
294 */
295static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
296{
297 struct snd_soc_codec *codec = codec_dai->codec;
298 u32 dai_cfg1;
299 u32 dai_cfg3;
300
301 dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1);
302 dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3);
303
304 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
305 case SND_SOC_DAIFMT_CBM_CFM:
306 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
307 break;
308 default:
309 return -EINVAL;
310 }
311
312 /* FIXME
313 *
314 * It support I2S only now
315 */
316 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
317 case SND_SOC_DAIFMT_I2S:
318 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
319 break;
320 default:
321 return -EINVAL;
322 }
323
324 /* FIXME
325 *
326 * It support 64bit data transmission only now
327 */
328 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
329
330 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
331 da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3);
332
333 return 0;
334}
335
336#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
337
338/* DAI operations */
339static struct snd_soc_dai_ops da7210_dai_ops = {
340 .startup = da7210_startup,
341 .hw_params = da7210_hw_params,
342 .set_fmt = da7210_set_dai_fmt,
343};
344
345struct snd_soc_dai da7210_dai = {
346 .name = "DA7210 IIS",
347 .id = 0,
348 /* playback capabilities */
349 .playback = {
350 .stream_name = "Playback",
351 .channels_min = 1,
352 .channels_max = 2,
353 .rates = SNDRV_PCM_RATE_8000_96000,
354 .formats = DA7210_FORMATS,
355 },
356 /* capture capabilities */
357 .capture = {
358 .stream_name = "Capture",
359 .channels_min = 1,
360 .channels_max = 2,
361 .rates = SNDRV_PCM_RATE_8000_96000,
362 .formats = DA7210_FORMATS,
363 },
364 .ops = &da7210_dai_ops,
365};
366EXPORT_SYMBOL_GPL(da7210_dai);
367
368/*
369 * Initialize the DA7210 driver
370 * register the mixer and dsp interfaces with the kernel
371 */
372static int da7210_init(struct da7210_priv *da7210)
373{
374 struct snd_soc_codec *codec = &da7210->codec;
375 int ret = 0;
376
377 if (da7210_codec) {
378 dev_err(codec->dev, "Another da7210 is registered\n");
379 return -EINVAL;
380 }
381
382 mutex_init(&codec->mutex);
383 INIT_LIST_HEAD(&codec->dapm_widgets);
384 INIT_LIST_HEAD(&codec->dapm_paths);
385
386 codec->private_data = da7210;
387 codec->name = "DA7210";
388 codec->owner = THIS_MODULE;
389 codec->read = da7210_read;
390 codec->write = da7210_write;
391 codec->dai = &da7210_dai;
392 codec->num_dai = 1;
393 codec->hw_write = (hw_write_t)i2c_master_send;
394 codec->reg_cache_size = ARRAY_SIZE(da7210_reg);
395 codec->reg_cache = kmemdup(da7210_reg,
396 sizeof(da7210_reg), GFP_KERNEL);
397
398 if (!codec->reg_cache)
399 return -ENOMEM;
400
401 da7210_dai.dev = codec->dev;
402 da7210_codec = codec;
403
404 ret = snd_soc_register_codec(codec);
405 if (ret) {
406 dev_err(codec->dev, "Failed to register CODEC: %d\n", ret);
407 goto init_err;
408 }
409
410 ret = snd_soc_register_dai(&da7210_dai);
411 if (ret) {
412 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
413 goto init_err;
414 }
415
416 /* FIXME
417 *
418 * This driver use fixed value here
419 */
420
421 /*
422 * ADC settings
423 */
424
425 /* Enable Left & Right MIC PGA and Mic Bias */
426 da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
427 da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
428
429 /* Enable Left and Right input PGA */
430 da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
431 da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
432
433 /* Enable Left and Right ADC */
434 da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
435
436 /*
437 * DAC settings
438 */
439
440 /* Enable Left and Right DAC */
441 da7210_write(codec, DA7210_DAC_SEL,
442 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
443 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
444
445 /* Enable Left and Right out PGA */
446 da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
447 da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
448
449 /* Enable Left and Right HeadPhone PGA */
450 da7210_write(codec, DA7210_HP_CFG,
451 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
452 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
453
454 /* Diable PLL and bypass it */
455 da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
456
457 /* Bypass PLL and set MCLK freq rang to 10-20MHz */
458 da7210_write(codec, DA7210_PLL_DIV3,
459 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
460
461 /* Activate all enabled subsystem */
462 da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
463
464 return ret;
465
466init_err:
467 kfree(codec->reg_cache);
468 codec->reg_cache = NULL;
469
470 return ret;
471
472}
473
474#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
735fe4cf
MB
475static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
476 const struct i2c_device_id *id)
98615454
KM
477{
478 struct da7210_priv *da7210;
479 struct snd_soc_codec *codec;
480 int ret;
481
482 da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
483 if (!da7210)
484 return -ENOMEM;
485
486 codec = &da7210->codec;
487 codec->dev = &i2c->dev;
488
489 i2c_set_clientdata(i2c, da7210);
490 codec->control_data = i2c;
491
492 ret = da7210_init(da7210);
493 if (ret < 0)
494 pr_err("Failed to initialise da7210 audio codec\n");
495
496 return ret;
497}
498
735fe4cf 499static int __devexit da7210_i2c_remove(struct i2c_client *client)
98615454
KM
500{
501 struct da7210_priv *da7210 = i2c_get_clientdata(client);
502
503 snd_soc_unregister_dai(&da7210_dai);
504 kfree(da7210->codec.reg_cache);
505 kfree(da7210);
506 da7210_codec = NULL;
507
508 return 0;
509}
510
511static const struct i2c_device_id da7210_i2c_id[] = {
512 { "da7210", 0 },
513 { }
514};
515MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
516
517/* I2C codec control layer */
518static struct i2c_driver da7210_i2c_driver = {
519 .driver = {
520 .name = "DA7210 I2C Codec",
521 .owner = THIS_MODULE,
522 },
523 .probe = da7210_i2c_probe,
524 .remove = __devexit_p(da7210_i2c_remove),
525 .id_table = da7210_i2c_id,
526};
527#endif
528
529static int da7210_probe(struct platform_device *pdev)
530{
531 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
532 struct snd_soc_codec *codec;
533 int ret;
534
535 if (!da7210_codec) {
536 dev_err(&pdev->dev, "Codec device not registered\n");
537 return -ENODEV;
538 }
539
540 socdev->card->codec = da7210_codec;
541 codec = da7210_codec;
542
543 /* Register pcms */
544 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
545 if (ret < 0)
546 goto pcm_err;
547
548 dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
549
550pcm_err:
551 return ret;
552}
553
554static int da7210_remove(struct platform_device *pdev)
555{
556 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
557
558 snd_soc_free_pcms(socdev);
559 snd_soc_dapm_free(socdev);
560
561 return 0;
562}
563
564struct snd_soc_codec_device soc_codec_dev_da7210 = {
565 .probe = da7210_probe,
566 .remove = da7210_remove,
567};
568EXPORT_SYMBOL_GPL(soc_codec_dev_da7210);
569
570static int __init da7210_modinit(void)
571{
572 int ret = 0;
573#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
574 ret = i2c_add_driver(&da7210_i2c_driver);
575#endif
576 return ret;
577}
578module_init(da7210_modinit);
579
580static void __exit da7210_exit(void)
581{
582#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
583 i2c_del_driver(&da7210_i2c_driver);
584#endif
585}
586module_exit(da7210_exit);
587
588MODULE_DESCRIPTION("ASoC DA7210 driver");
589MODULE_AUTHOR("David Chen, Kuninori Morimoto");
590MODULE_LICENSE("GPL");