blob: 60b83b48246712a9155a43a97b16a85b528fa33b [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>
Kuninori Morimotoa300de32010-07-01 14:23:45 +090031#include <sound/tlv.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090032
33#include "ak4642.h"
34
35#define AK4642_VERSION "0.0.1"
36
37#define PW_MGMT1 0x00
38#define PW_MGMT2 0x01
39#define SG_SL1 0x02
40#define SG_SL2 0x03
41#define MD_CTL1 0x04
42#define MD_CTL2 0x05
43#define TIMER 0x06
44#define ALC_CTL1 0x07
45#define ALC_CTL2 0x08
46#define L_IVC 0x09
47#define L_DVC 0x0a
48#define ALC_CTL3 0x0b
49#define R_IVC 0x0c
50#define R_DVC 0x0d
51#define MD_CTL3 0x0e
52#define MD_CTL4 0x0f
53#define PW_MGMT3 0x10
54#define DF_S 0x11
55#define FIL3_0 0x12
56#define FIL3_1 0x13
57#define FIL3_2 0x14
58#define FIL3_3 0x15
59#define EQ_0 0x16
60#define EQ_1 0x17
61#define EQ_2 0x18
62#define EQ_3 0x19
63#define EQ_4 0x1a
64#define EQ_5 0x1b
65#define FIL1_0 0x1c
66#define FIL1_1 0x1d
67#define FIL1_2 0x1e
68#define FIL1_3 0x1f
69#define PW_MGMT4 0x20
70#define MD_CTL5 0x21
71#define LO_MS 0x22
72#define HP_MS 0x23
73#define SPK_MS 0x24
74
75#define AK4642_CACHEREGNUM 0x25
76
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090077/* PW_MGMT2 */
78#define HPMTN (1 << 6)
79#define PMHPL (1 << 5)
80#define PMHPR (1 << 4)
81#define MS (1 << 3) /* master/slave select */
82#define MCKO (1 << 1)
83#define PMPLL (1 << 0)
84
85#define PMHP_MASK (PMHPL | PMHPR)
86#define PMHP PMHP_MASK
87
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +090088/* MD_CTL1 */
89#define PLL3 (1 << 7)
90#define PLL2 (1 << 6)
91#define PLL1 (1 << 5)
92#define PLL0 (1 << 4)
93#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
94
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090095#define BCKO_MASK (1 << 3)
96#define BCKO_64 BCKO_MASK
97
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +090098/* MD_CTL2 */
99#define FS0 (1 << 0)
100#define FS1 (1 << 1)
101#define FS2 (1 << 2)
102#define FS3 (1 << 5)
103#define FS_MASK (FS0 | FS1 | FS2 | FS3)
104
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900105struct snd_soc_codec_device soc_codec_dev_ak4642;
106
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900107/*
108 * Playback Volume (table 39)
109 *
110 * max : 0x00 : +12.0 dB
111 * ( 0.5 dB step )
112 * min : 0xFE : -115.0 dB
113 * mute: 0xFF
114 */
115static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
116
117static const struct snd_kcontrol_new ak4642_snd_controls[] = {
118
119 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
120 0, 0xFF, 1, out_tlv),
121};
122
123
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900124/* codec private data */
125struct ak4642_priv {
126 struct snd_soc_codec codec;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900127};
128
129static struct snd_soc_codec *ak4642_codec;
130
131/*
132 * ak4642 register cache
133 */
134static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
135 0x0000, 0x0000, 0x0001, 0x0000,
136 0x0002, 0x0000, 0x0000, 0x0000,
137 0x00e1, 0x00e1, 0x0018, 0x0000,
138 0x00e1, 0x0018, 0x0011, 0x0008,
139 0x0000, 0x0000, 0x0000, 0x0000,
140 0x0000, 0x0000, 0x0000, 0x0000,
141 0x0000, 0x0000, 0x0000, 0x0000,
142 0x0000, 0x0000, 0x0000, 0x0000,
143 0x0000, 0x0000, 0x0000, 0x0000,
144 0x0000,
145};
146
147/*
148 * read ak4642 register cache
149 */
150static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
151 unsigned int reg)
152{
153 u16 *cache = codec->reg_cache;
154 if (reg >= AK4642_CACHEREGNUM)
155 return -1;
156 return cache[reg];
157}
158
159/*
160 * write ak4642 register cache
161 */
162static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
163 u16 reg, unsigned int value)
164{
165 u16 *cache = codec->reg_cache;
166 if (reg >= AK4642_CACHEREGNUM)
167 return;
168
169 cache[reg] = value;
170}
171
172/*
173 * write to the AK4642 register space
174 */
175static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
176 unsigned int value)
177{
178 u8 data[2];
179
180 /* data is
181 * D15..D8 AK4642 register offset
182 * D7...D0 register data
183 */
184 data[0] = reg & 0xff;
185 data[1] = value & 0xff;
186
187 if (codec->hw_write(codec->control_data, data, 2) == 2) {
188 ak4642_write_reg_cache(codec, reg, value);
189 return 0;
190 } else
191 return -EIO;
192}
193
194static int ak4642_sync(struct snd_soc_codec *codec)
195{
196 u16 *cache = codec->reg_cache;
197 int i, r = 0;
198
199 for (i = 0; i < AK4642_CACHEREGNUM; i++)
200 r |= ak4642_write(codec, i, cache[i]);
201
202 return r;
203};
204
205static int ak4642_dai_startup(struct snd_pcm_substream *substream,
206 struct snd_soc_dai *dai)
207{
208 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
209 struct snd_soc_codec *codec = dai->codec;
210
211 if (is_play) {
212 /*
213 * start headphone output
214 *
215 * PLL, Master Mode
216 * Audio I/F Format :MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900217 * Bass Boost Level : Middle
218 *
219 * This operation came from example code of
220 * "ASAHI KASEI AK4642" (japanese) manual p97.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900221 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900222 ak4642_write(codec, 0x0f, 0x09);
223 ak4642_write(codec, 0x0e, 0x19);
224 ak4642_write(codec, 0x09, 0x91);
225 ak4642_write(codec, 0x0c, 0x91);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900226 ak4642_write(codec, 0x00, 0x64);
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900227 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
228 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900229 } else {
230 /*
231 * start stereo input
232 *
233 * PLL Master Mode
234 * Audio I/F Format:MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900235 * Pre MIC AMP:+20dB
236 * MIC Power On
237 * ALC setting:Refer to Table 35
238 * ALC bit=“1”
239 *
240 * This operation came from example code of
241 * "ASAHI KASEI AK4642" (japanese) manual p94.
242 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900243 ak4642_write(codec, 0x02, 0x05);
244 ak4642_write(codec, 0x06, 0x3c);
245 ak4642_write(codec, 0x08, 0xe1);
246 ak4642_write(codec, 0x0b, 0x00);
247 ak4642_write(codec, 0x07, 0x21);
248 ak4642_write(codec, 0x00, 0x41);
249 ak4642_write(codec, 0x10, 0x01);
250 }
251
252 return 0;
253}
254
255static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
256 struct snd_soc_dai *dai)
257{
258 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
259 struct snd_soc_codec *codec = dai->codec;
260
261 if (is_play) {
262 /* stop headphone output */
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900263 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
264 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900265 ak4642_write(codec, 0x00, 0x40);
266 ak4642_write(codec, 0x0e, 0x11);
267 ak4642_write(codec, 0x0f, 0x08);
268 } else {
269 /* stop stereo input */
270 ak4642_write(codec, 0x00, 0x40);
271 ak4642_write(codec, 0x10, 0x00);
272 ak4642_write(codec, 0x07, 0x01);
273 }
274}
275
276static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
277 int clk_id, unsigned int freq, int dir)
278{
279 struct snd_soc_codec *codec = codec_dai->codec;
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900280 u8 pll;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900281
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900282 switch (freq) {
283 case 11289600:
284 pll = PLL2;
285 break;
286 case 12288000:
287 pll = PLL2 | PLL0;
288 break;
289 case 12000000:
290 pll = PLL2 | PLL1;
291 break;
292 case 24000000:
293 pll = PLL2 | PLL1 | PLL0;
294 break;
295 case 13500000:
296 pll = PLL3 | PLL2;
297 break;
298 case 27000000:
299 pll = PLL3 | PLL2 | PLL0;
300 break;
301 default:
302 return -EINVAL;
303 }
304 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
305
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900306 return 0;
307}
308
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900309static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
310{
311 struct snd_soc_codec *codec = dai->codec;
312 u8 data;
313 u8 bcko;
314
315 data = MCKO | PMPLL; /* use MCKO */
316 bcko = 0;
317
318 /* set master/slave audio interface */
319 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
320 case SND_SOC_DAIFMT_CBM_CFM:
321 data |= MS;
322 bcko = BCKO_64;
323 break;
324 case SND_SOC_DAIFMT_CBS_CFS:
325 break;
326 default:
327 return -EINVAL;
328 }
329 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
330 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
331
332 return 0;
333}
334
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900335static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *params,
337 struct snd_soc_dai *dai)
338{
339 struct snd_soc_codec *codec = dai->codec;
340 u8 rate;
341
342 switch (params_rate(params)) {
343 case 7350:
344 rate = FS2;
345 break;
346 case 8000:
347 rate = 0;
348 break;
349 case 11025:
350 rate = FS2 | FS0;
351 break;
352 case 12000:
353 rate = FS0;
354 break;
355 case 14700:
356 rate = FS2 | FS1;
357 break;
358 case 16000:
359 rate = FS1;
360 break;
361 case 22050:
362 rate = FS2 | FS1 | FS0;
363 break;
364 case 24000:
365 rate = FS1 | FS0;
366 break;
367 case 29400:
368 rate = FS3 | FS2 | FS1;
369 break;
370 case 32000:
371 rate = FS3 | FS1;
372 break;
373 case 44100:
374 rate = FS3 | FS2 | FS1 | FS0;
375 break;
376 case 48000:
377 rate = FS3 | FS1 | FS0;
378 break;
379 default:
380 return -EINVAL;
381 break;
382 }
383 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
384
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900385 return 0;
386}
387
388static struct snd_soc_dai_ops ak4642_dai_ops = {
389 .startup = ak4642_dai_startup,
390 .shutdown = ak4642_dai_shutdown,
391 .set_sysclk = ak4642_dai_set_sysclk,
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900392 .set_fmt = ak4642_dai_set_fmt,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900393 .hw_params = ak4642_dai_hw_params,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900394};
395
396struct snd_soc_dai ak4642_dai = {
397 .name = "AK4642",
398 .playback = {
399 .stream_name = "Playback",
400 .channels_min = 1,
401 .channels_max = 2,
402 .rates = SNDRV_PCM_RATE_8000_48000,
403 .formats = SNDRV_PCM_FMTBIT_S16_LE },
404 .capture = {
405 .stream_name = "Capture",
406 .channels_min = 1,
407 .channels_max = 2,
408 .rates = SNDRV_PCM_RATE_8000_48000,
409 .formats = SNDRV_PCM_FMTBIT_S16_LE },
410 .ops = &ak4642_dai_ops,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900411 .symmetric_rates = 1,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900412};
413EXPORT_SYMBOL_GPL(ak4642_dai);
414
415static int ak4642_resume(struct platform_device *pdev)
416{
417 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
418 struct snd_soc_codec *codec = socdev->card->codec;
419
420 ak4642_sync(codec);
421 return 0;
422}
423
424/*
425 * initialise the AK4642 driver
426 * register the mixer and dsp interfaces with the kernel
427 */
428static int ak4642_init(struct ak4642_priv *ak4642)
429{
430 struct snd_soc_codec *codec = &ak4642->codec;
431 int ret = 0;
432
433 if (ak4642_codec) {
434 dev_err(codec->dev, "Another ak4642 is registered\n");
435 return -EINVAL;
436 }
437
438 mutex_init(&codec->mutex);
439 INIT_LIST_HEAD(&codec->dapm_widgets);
440 INIT_LIST_HEAD(&codec->dapm_paths);
441
Mark Brownb2c812e2010-04-14 15:35:19 +0900442 snd_soc_codec_set_drvdata(codec, ak4642);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900443 codec->name = "AK4642";
444 codec->owner = THIS_MODULE;
445 codec->read = ak4642_read_reg_cache;
446 codec->write = ak4642_write;
447 codec->dai = &ak4642_dai;
448 codec->num_dai = 1;
449 codec->hw_write = (hw_write_t)i2c_master_send;
450 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
451 codec->reg_cache = kmemdup(ak4642_reg,
452 sizeof(ak4642_reg), GFP_KERNEL);
453
454 if (!codec->reg_cache)
455 return -ENOMEM;
456
457 ak4642_dai.dev = codec->dev;
458 ak4642_codec = codec;
459
460 ret = snd_soc_register_codec(codec);
461 if (ret) {
462 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
463 goto reg_cache_err;
464 }
465
466 ret = snd_soc_register_dai(&ak4642_dai);
467 if (ret) {
468 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
469 snd_soc_unregister_codec(codec);
470 goto reg_cache_err;
471 }
472
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900473 return ret;
474
475reg_cache_err:
476 kfree(codec->reg_cache);
477 codec->reg_cache = NULL;
478
479 return ret;
480}
481
482#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
483static int ak4642_i2c_probe(struct i2c_client *i2c,
484 const struct i2c_device_id *id)
485{
486 struct ak4642_priv *ak4642;
487 struct snd_soc_codec *codec;
488 int ret;
489
490 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
491 if (!ak4642)
492 return -ENOMEM;
493
494 codec = &ak4642->codec;
495 codec->dev = &i2c->dev;
496
497 i2c_set_clientdata(i2c, ak4642);
498 codec->control_data = i2c;
499
500 ret = ak4642_init(ak4642);
501 if (ret < 0)
502 printk(KERN_ERR "failed to initialise AK4642\n");
503
504 return ret;
505}
506
507static int ak4642_i2c_remove(struct i2c_client *client)
508{
509 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
510
511 snd_soc_unregister_dai(&ak4642_dai);
512 snd_soc_unregister_codec(&ak4642->codec);
513 kfree(ak4642->codec.reg_cache);
514 kfree(ak4642);
515 ak4642_codec = NULL;
516
517 return 0;
518}
519
520static const struct i2c_device_id ak4642_i2c_id[] = {
521 { "ak4642", 0 },
522 { "ak4643", 0 },
523 { }
524};
525MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
526
527static struct i2c_driver ak4642_i2c_driver = {
528 .driver = {
529 .name = "AK4642 I2C Codec",
530 .owner = THIS_MODULE,
531 },
532 .probe = ak4642_i2c_probe,
533 .remove = ak4642_i2c_remove,
534 .id_table = ak4642_i2c_id,
535};
536
537#endif
538
539static int ak4642_probe(struct platform_device *pdev)
540{
541 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
542 int ret;
543
544 if (!ak4642_codec) {
545 dev_err(&pdev->dev, "Codec device not registered\n");
546 return -ENODEV;
547 }
548
549 socdev->card->codec = ak4642_codec;
550
551 /* register pcms */
552 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
553 if (ret < 0) {
554 printk(KERN_ERR "ak4642: failed to create pcms\n");
555 goto pcm_err;
556 }
557
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900558 snd_soc_add_controls(ak4642_codec, ak4642_snd_controls,
559 ARRAY_SIZE(ak4642_snd_controls));
560
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900561 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
562 return ret;
563
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900564pcm_err:
565 return ret;
566
567}
568
569/* power down chip */
570static int ak4642_remove(struct platform_device *pdev)
571{
572 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
573
574 snd_soc_free_pcms(socdev);
575 snd_soc_dapm_free(socdev);
576
577 return 0;
578}
579
580struct snd_soc_codec_device soc_codec_dev_ak4642 = {
581 .probe = ak4642_probe,
582 .remove = ak4642_remove,
583 .resume = ak4642_resume,
584};
585EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
586
587static int __init ak4642_modinit(void)
588{
Kuninori Morimoto1cf86f62009-12-15 15:54:21 +0900589 int ret = 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900590#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
591 ret = i2c_add_driver(&ak4642_i2c_driver);
592#endif
593 return ret;
594
595}
596module_init(ak4642_modinit);
597
598static void __exit ak4642_exit(void)
599{
600#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
601 i2c_del_driver(&ak4642_i2c_driver);
602#endif
603
604}
605module_exit(ak4642_exit);
606
607MODULE_DESCRIPTION("Soc AK4642 driver");
608MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
609MODULE_LICENSE("GPL");