blob: 8d56811c730611a6015b5e04252dba96473b96c8 [file] [log] [blame]
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +09001/*
2 * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on wm8731.c by Richard Purdie
8 * Based on ak4535.c by Richard Purdie
9 * Based on wm8753.c by Liam Girdwood
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16/* ** CAUTION **
17 *
18 * This is very simple driver.
19 * It can use headphone output / stereo input only
20 *
21 * AK4642 is not tested.
22 * AK4643 is tested.
23 */
24
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090025#include <linux/delay.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090026#include <linux/i2c.h>
27#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090029#include <sound/soc-dapm.h>
30#include <sound/initval.h>
31
32#include "ak4642.h"
33
34#define AK4642_VERSION "0.0.1"
35
36#define PW_MGMT1 0x00
37#define PW_MGMT2 0x01
38#define SG_SL1 0x02
39#define SG_SL2 0x03
40#define MD_CTL1 0x04
41#define MD_CTL2 0x05
42#define TIMER 0x06
43#define ALC_CTL1 0x07
44#define ALC_CTL2 0x08
45#define L_IVC 0x09
46#define L_DVC 0x0a
47#define ALC_CTL3 0x0b
48#define R_IVC 0x0c
49#define R_DVC 0x0d
50#define MD_CTL3 0x0e
51#define MD_CTL4 0x0f
52#define PW_MGMT3 0x10
53#define DF_S 0x11
54#define FIL3_0 0x12
55#define FIL3_1 0x13
56#define FIL3_2 0x14
57#define FIL3_3 0x15
58#define EQ_0 0x16
59#define EQ_1 0x17
60#define EQ_2 0x18
61#define EQ_3 0x19
62#define EQ_4 0x1a
63#define EQ_5 0x1b
64#define FIL1_0 0x1c
65#define FIL1_1 0x1d
66#define FIL1_2 0x1e
67#define FIL1_3 0x1f
68#define PW_MGMT4 0x20
69#define MD_CTL5 0x21
70#define LO_MS 0x22
71#define HP_MS 0x23
72#define SPK_MS 0x24
73
74#define AK4642_CACHEREGNUM 0x25
75
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090076/* PW_MGMT2 */
77#define HPMTN (1 << 6)
78#define PMHPL (1 << 5)
79#define PMHPR (1 << 4)
80#define MS (1 << 3) /* master/slave select */
81#define MCKO (1 << 1)
82#define PMPLL (1 << 0)
83
84#define PMHP_MASK (PMHPL | PMHPR)
85#define PMHP PMHP_MASK
86
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +090087/* MD_CTL1 */
88#define PLL3 (1 << 7)
89#define PLL2 (1 << 6)
90#define PLL1 (1 << 5)
91#define PLL0 (1 << 4)
92#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
93
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090094#define BCKO_MASK (1 << 3)
95#define BCKO_64 BCKO_MASK
96
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +090097/* MD_CTL2 */
98#define FS0 (1 << 0)
99#define FS1 (1 << 1)
100#define FS2 (1 << 2)
101#define FS3 (1 << 5)
102#define FS_MASK (FS0 | FS1 | FS2 | FS3)
103
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900104struct snd_soc_codec_device soc_codec_dev_ak4642;
105
106/* codec private data */
107struct ak4642_priv {
108 struct snd_soc_codec codec;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900109};
110
111static struct snd_soc_codec *ak4642_codec;
112
113/*
114 * ak4642 register cache
115 */
116static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
117 0x0000, 0x0000, 0x0001, 0x0000,
118 0x0002, 0x0000, 0x0000, 0x0000,
119 0x00e1, 0x00e1, 0x0018, 0x0000,
120 0x00e1, 0x0018, 0x0011, 0x0008,
121 0x0000, 0x0000, 0x0000, 0x0000,
122 0x0000, 0x0000, 0x0000, 0x0000,
123 0x0000, 0x0000, 0x0000, 0x0000,
124 0x0000, 0x0000, 0x0000, 0x0000,
125 0x0000, 0x0000, 0x0000, 0x0000,
126 0x0000,
127};
128
129/*
130 * read ak4642 register cache
131 */
132static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
133 unsigned int reg)
134{
135 u16 *cache = codec->reg_cache;
136 if (reg >= AK4642_CACHEREGNUM)
137 return -1;
138 return cache[reg];
139}
140
141/*
142 * write ak4642 register cache
143 */
144static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
145 u16 reg, unsigned int value)
146{
147 u16 *cache = codec->reg_cache;
148 if (reg >= AK4642_CACHEREGNUM)
149 return;
150
151 cache[reg] = value;
152}
153
154/*
155 * write to the AK4642 register space
156 */
157static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
158 unsigned int value)
159{
160 u8 data[2];
161
162 /* data is
163 * D15..D8 AK4642 register offset
164 * D7...D0 register data
165 */
166 data[0] = reg & 0xff;
167 data[1] = value & 0xff;
168
169 if (codec->hw_write(codec->control_data, data, 2) == 2) {
170 ak4642_write_reg_cache(codec, reg, value);
171 return 0;
172 } else
173 return -EIO;
174}
175
176static int ak4642_sync(struct snd_soc_codec *codec)
177{
178 u16 *cache = codec->reg_cache;
179 int i, r = 0;
180
181 for (i = 0; i < AK4642_CACHEREGNUM; i++)
182 r |= ak4642_write(codec, i, cache[i]);
183
184 return r;
185};
186
187static int ak4642_dai_startup(struct snd_pcm_substream *substream,
188 struct snd_soc_dai *dai)
189{
190 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
191 struct snd_soc_codec *codec = dai->codec;
192
193 if (is_play) {
194 /*
195 * start headphone output
196 *
197 * PLL, Master Mode
198 * Audio I/F Format :MSB justified (ADC & DAC)
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900199 * Digital Volume: -8dB
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900200 * Bass Boost Level : Middle
201 *
202 * This operation came from example code of
203 * "ASAHI KASEI AK4642" (japanese) manual p97.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900204 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900205 ak4642_write(codec, 0x0f, 0x09);
206 ak4642_write(codec, 0x0e, 0x19);
207 ak4642_write(codec, 0x09, 0x91);
208 ak4642_write(codec, 0x0c, 0x91);
209 ak4642_write(codec, 0x0a, 0x28);
210 ak4642_write(codec, 0x0d, 0x28);
211 ak4642_write(codec, 0x00, 0x64);
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900212 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
213 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900214 } else {
215 /*
216 * start stereo input
217 *
218 * PLL Master Mode
219 * Audio I/F Format:MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900220 * Pre MIC AMP:+20dB
221 * MIC Power On
222 * ALC setting:Refer to Table 35
223 * ALC bit=“1”
224 *
225 * This operation came from example code of
226 * "ASAHI KASEI AK4642" (japanese) manual p94.
227 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900228 ak4642_write(codec, 0x02, 0x05);
229 ak4642_write(codec, 0x06, 0x3c);
230 ak4642_write(codec, 0x08, 0xe1);
231 ak4642_write(codec, 0x0b, 0x00);
232 ak4642_write(codec, 0x07, 0x21);
233 ak4642_write(codec, 0x00, 0x41);
234 ak4642_write(codec, 0x10, 0x01);
235 }
236
237 return 0;
238}
239
240static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
241 struct snd_soc_dai *dai)
242{
243 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
244 struct snd_soc_codec *codec = dai->codec;
245
246 if (is_play) {
247 /* stop headphone output */
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900248 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
249 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900250 ak4642_write(codec, 0x00, 0x40);
251 ak4642_write(codec, 0x0e, 0x11);
252 ak4642_write(codec, 0x0f, 0x08);
253 } else {
254 /* stop stereo input */
255 ak4642_write(codec, 0x00, 0x40);
256 ak4642_write(codec, 0x10, 0x00);
257 ak4642_write(codec, 0x07, 0x01);
258 }
259}
260
261static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
262 int clk_id, unsigned int freq, int dir)
263{
264 struct snd_soc_codec *codec = codec_dai->codec;
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900265 u8 pll;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900266
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900267 switch (freq) {
268 case 11289600:
269 pll = PLL2;
270 break;
271 case 12288000:
272 pll = PLL2 | PLL0;
273 break;
274 case 12000000:
275 pll = PLL2 | PLL1;
276 break;
277 case 24000000:
278 pll = PLL2 | PLL1 | PLL0;
279 break;
280 case 13500000:
281 pll = PLL3 | PLL2;
282 break;
283 case 27000000:
284 pll = PLL3 | PLL2 | PLL0;
285 break;
286 default:
287 return -EINVAL;
288 }
289 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
290
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900291 return 0;
292}
293
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900294static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
295{
296 struct snd_soc_codec *codec = dai->codec;
297 u8 data;
298 u8 bcko;
299
300 data = MCKO | PMPLL; /* use MCKO */
301 bcko = 0;
302
303 /* set master/slave audio interface */
304 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
305 case SND_SOC_DAIFMT_CBM_CFM:
306 data |= MS;
307 bcko = BCKO_64;
308 break;
309 case SND_SOC_DAIFMT_CBS_CFS:
310 break;
311 default:
312 return -EINVAL;
313 }
314 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
315 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
316
317 return 0;
318}
319
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900320static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *params,
322 struct snd_soc_dai *dai)
323{
324 struct snd_soc_codec *codec = dai->codec;
325 u8 rate;
326
327 switch (params_rate(params)) {
328 case 7350:
329 rate = FS2;
330 break;
331 case 8000:
332 rate = 0;
333 break;
334 case 11025:
335 rate = FS2 | FS0;
336 break;
337 case 12000:
338 rate = FS0;
339 break;
340 case 14700:
341 rate = FS2 | FS1;
342 break;
343 case 16000:
344 rate = FS1;
345 break;
346 case 22050:
347 rate = FS2 | FS1 | FS0;
348 break;
349 case 24000:
350 rate = FS1 | FS0;
351 break;
352 case 29400:
353 rate = FS3 | FS2 | FS1;
354 break;
355 case 32000:
356 rate = FS3 | FS1;
357 break;
358 case 44100:
359 rate = FS3 | FS2 | FS1 | FS0;
360 break;
361 case 48000:
362 rate = FS3 | FS1 | FS0;
363 break;
364 default:
365 return -EINVAL;
366 break;
367 }
368 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
369
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900370 return 0;
371}
372
373static struct snd_soc_dai_ops ak4642_dai_ops = {
374 .startup = ak4642_dai_startup,
375 .shutdown = ak4642_dai_shutdown,
376 .set_sysclk = ak4642_dai_set_sysclk,
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900377 .set_fmt = ak4642_dai_set_fmt,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900378 .hw_params = ak4642_dai_hw_params,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900379};
380
381struct snd_soc_dai ak4642_dai = {
382 .name = "AK4642",
383 .playback = {
384 .stream_name = "Playback",
385 .channels_min = 1,
386 .channels_max = 2,
387 .rates = SNDRV_PCM_RATE_8000_48000,
388 .formats = SNDRV_PCM_FMTBIT_S16_LE },
389 .capture = {
390 .stream_name = "Capture",
391 .channels_min = 1,
392 .channels_max = 2,
393 .rates = SNDRV_PCM_RATE_8000_48000,
394 .formats = SNDRV_PCM_FMTBIT_S16_LE },
395 .ops = &ak4642_dai_ops,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900396 .symmetric_rates = 1,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900397};
398EXPORT_SYMBOL_GPL(ak4642_dai);
399
400static int ak4642_resume(struct platform_device *pdev)
401{
402 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
403 struct snd_soc_codec *codec = socdev->card->codec;
404
405 ak4642_sync(codec);
406 return 0;
407}
408
409/*
410 * initialise the AK4642 driver
411 * register the mixer and dsp interfaces with the kernel
412 */
413static int ak4642_init(struct ak4642_priv *ak4642)
414{
415 struct snd_soc_codec *codec = &ak4642->codec;
416 int ret = 0;
417
418 if (ak4642_codec) {
419 dev_err(codec->dev, "Another ak4642 is registered\n");
420 return -EINVAL;
421 }
422
423 mutex_init(&codec->mutex);
424 INIT_LIST_HEAD(&codec->dapm_widgets);
425 INIT_LIST_HEAD(&codec->dapm_paths);
426
Mark Brownb2c812e2010-04-14 15:35:19 +0900427 snd_soc_codec_set_drvdata(codec, ak4642);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900428 codec->name = "AK4642";
429 codec->owner = THIS_MODULE;
430 codec->read = ak4642_read_reg_cache;
431 codec->write = ak4642_write;
432 codec->dai = &ak4642_dai;
433 codec->num_dai = 1;
434 codec->hw_write = (hw_write_t)i2c_master_send;
435 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
436 codec->reg_cache = kmemdup(ak4642_reg,
437 sizeof(ak4642_reg), GFP_KERNEL);
438
439 if (!codec->reg_cache)
440 return -ENOMEM;
441
442 ak4642_dai.dev = codec->dev;
443 ak4642_codec = codec;
444
445 ret = snd_soc_register_codec(codec);
446 if (ret) {
447 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
448 goto reg_cache_err;
449 }
450
451 ret = snd_soc_register_dai(&ak4642_dai);
452 if (ret) {
453 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
454 snd_soc_unregister_codec(codec);
455 goto reg_cache_err;
456 }
457
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900458 return ret;
459
460reg_cache_err:
461 kfree(codec->reg_cache);
462 codec->reg_cache = NULL;
463
464 return ret;
465}
466
467#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
468static int ak4642_i2c_probe(struct i2c_client *i2c,
469 const struct i2c_device_id *id)
470{
471 struct ak4642_priv *ak4642;
472 struct snd_soc_codec *codec;
473 int ret;
474
475 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
476 if (!ak4642)
477 return -ENOMEM;
478
479 codec = &ak4642->codec;
480 codec->dev = &i2c->dev;
481
482 i2c_set_clientdata(i2c, ak4642);
483 codec->control_data = i2c;
484
485 ret = ak4642_init(ak4642);
486 if (ret < 0)
487 printk(KERN_ERR "failed to initialise AK4642\n");
488
489 return ret;
490}
491
492static int ak4642_i2c_remove(struct i2c_client *client)
493{
494 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
495
496 snd_soc_unregister_dai(&ak4642_dai);
497 snd_soc_unregister_codec(&ak4642->codec);
498 kfree(ak4642->codec.reg_cache);
499 kfree(ak4642);
500 ak4642_codec = NULL;
501
502 return 0;
503}
504
505static const struct i2c_device_id ak4642_i2c_id[] = {
506 { "ak4642", 0 },
507 { "ak4643", 0 },
508 { }
509};
510MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
511
512static struct i2c_driver ak4642_i2c_driver = {
513 .driver = {
514 .name = "AK4642 I2C Codec",
515 .owner = THIS_MODULE,
516 },
517 .probe = ak4642_i2c_probe,
518 .remove = ak4642_i2c_remove,
519 .id_table = ak4642_i2c_id,
520};
521
522#endif
523
524static int ak4642_probe(struct platform_device *pdev)
525{
526 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
527 int ret;
528
529 if (!ak4642_codec) {
530 dev_err(&pdev->dev, "Codec device not registered\n");
531 return -ENODEV;
532 }
533
534 socdev->card->codec = ak4642_codec;
535
536 /* register pcms */
537 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
538 if (ret < 0) {
539 printk(KERN_ERR "ak4642: failed to create pcms\n");
540 goto pcm_err;
541 }
542
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900543 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
544 return ret;
545
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900546pcm_err:
547 return ret;
548
549}
550
551/* power down chip */
552static int ak4642_remove(struct platform_device *pdev)
553{
554 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
555
556 snd_soc_free_pcms(socdev);
557 snd_soc_dapm_free(socdev);
558
559 return 0;
560}
561
562struct snd_soc_codec_device soc_codec_dev_ak4642 = {
563 .probe = ak4642_probe,
564 .remove = ak4642_remove,
565 .resume = ak4642_resume,
566};
567EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
568
569static int __init ak4642_modinit(void)
570{
Kuninori Morimoto1cf86f62009-12-15 15:54:21 +0900571 int ret = 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900572#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
573 ret = i2c_add_driver(&ak4642_i2c_driver);
574#endif
575 return ret;
576
577}
578module_init(ak4642_modinit);
579
580static void __exit ak4642_exit(void)
581{
582#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
583 i2c_del_driver(&ak4642_i2c_driver);
584#endif
585
586}
587module_exit(ak4642_exit);
588
589MODULE_DESCRIPTION("Soc AK4642 driver");
590MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
591MODULE_LICENSE("GPL");