blob: d5bd4cae73a12723357117fca6134389cd98d503 [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
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/i2c.h>
31#include <linux/platform_device.h>
32#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
36#include <sound/soc-dapm.h>
37#include <sound/initval.h>
38
39#include "ak4642.h"
40
41#define AK4642_VERSION "0.0.1"
42
43#define PW_MGMT1 0x00
44#define PW_MGMT2 0x01
45#define SG_SL1 0x02
46#define SG_SL2 0x03
47#define MD_CTL1 0x04
48#define MD_CTL2 0x05
49#define TIMER 0x06
50#define ALC_CTL1 0x07
51#define ALC_CTL2 0x08
52#define L_IVC 0x09
53#define L_DVC 0x0a
54#define ALC_CTL3 0x0b
55#define R_IVC 0x0c
56#define R_DVC 0x0d
57#define MD_CTL3 0x0e
58#define MD_CTL4 0x0f
59#define PW_MGMT3 0x10
60#define DF_S 0x11
61#define FIL3_0 0x12
62#define FIL3_1 0x13
63#define FIL3_2 0x14
64#define FIL3_3 0x15
65#define EQ_0 0x16
66#define EQ_1 0x17
67#define EQ_2 0x18
68#define EQ_3 0x19
69#define EQ_4 0x1a
70#define EQ_5 0x1b
71#define FIL1_0 0x1c
72#define FIL1_1 0x1d
73#define FIL1_2 0x1e
74#define FIL1_3 0x1f
75#define PW_MGMT4 0x20
76#define MD_CTL5 0x21
77#define LO_MS 0x22
78#define HP_MS 0x23
79#define SPK_MS 0x24
80
81#define AK4642_CACHEREGNUM 0x25
82
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +090083/* MD_CTL1 */
84#define PLL3 (1 << 7)
85#define PLL2 (1 << 6)
86#define PLL1 (1 << 5)
87#define PLL0 (1 << 4)
88#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
89
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090090struct snd_soc_codec_device soc_codec_dev_ak4642;
91
92/* codec private data */
93struct ak4642_priv {
94 struct snd_soc_codec codec;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090095};
96
97static struct snd_soc_codec *ak4642_codec;
98
99/*
100 * ak4642 register cache
101 */
102static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
103 0x0000, 0x0000, 0x0001, 0x0000,
104 0x0002, 0x0000, 0x0000, 0x0000,
105 0x00e1, 0x00e1, 0x0018, 0x0000,
106 0x00e1, 0x0018, 0x0011, 0x0008,
107 0x0000, 0x0000, 0x0000, 0x0000,
108 0x0000, 0x0000, 0x0000, 0x0000,
109 0x0000, 0x0000, 0x0000, 0x0000,
110 0x0000, 0x0000, 0x0000, 0x0000,
111 0x0000, 0x0000, 0x0000, 0x0000,
112 0x0000,
113};
114
115/*
116 * read ak4642 register cache
117 */
118static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
119 unsigned int reg)
120{
121 u16 *cache = codec->reg_cache;
122 if (reg >= AK4642_CACHEREGNUM)
123 return -1;
124 return cache[reg];
125}
126
127/*
128 * write ak4642 register cache
129 */
130static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
131 u16 reg, unsigned int value)
132{
133 u16 *cache = codec->reg_cache;
134 if (reg >= AK4642_CACHEREGNUM)
135 return;
136
137 cache[reg] = value;
138}
139
140/*
141 * write to the AK4642 register space
142 */
143static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
144 unsigned int value)
145{
146 u8 data[2];
147
148 /* data is
149 * D15..D8 AK4642 register offset
150 * D7...D0 register data
151 */
152 data[0] = reg & 0xff;
153 data[1] = value & 0xff;
154
155 if (codec->hw_write(codec->control_data, data, 2) == 2) {
156 ak4642_write_reg_cache(codec, reg, value);
157 return 0;
158 } else
159 return -EIO;
160}
161
162static int ak4642_sync(struct snd_soc_codec *codec)
163{
164 u16 *cache = codec->reg_cache;
165 int i, r = 0;
166
167 for (i = 0; i < AK4642_CACHEREGNUM; i++)
168 r |= ak4642_write(codec, i, cache[i]);
169
170 return r;
171};
172
173static int ak4642_dai_startup(struct snd_pcm_substream *substream,
174 struct snd_soc_dai *dai)
175{
176 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
177 struct snd_soc_codec *codec = dai->codec;
178
179 if (is_play) {
180 /*
181 * start headphone output
182 *
183 * PLL, Master Mode
184 * Audio I/F Format :MSB justified (ADC & DAC)
185 * Sampling Frequency: 44.1kHz
186 * Digital Volume: −8dB
187 * Bass Boost Level : Middle
188 *
189 * This operation came from example code of
190 * "ASAHI KASEI AK4642" (japanese) manual p97.
191 *
192 * Example code use 0x39, 0x79 value for 0x01 address,
193 * But we need MCKO (0x02) bit now
194 */
195 ak4642_write(codec, 0x05, 0x27);
196 ak4642_write(codec, 0x0f, 0x09);
197 ak4642_write(codec, 0x0e, 0x19);
198 ak4642_write(codec, 0x09, 0x91);
199 ak4642_write(codec, 0x0c, 0x91);
200 ak4642_write(codec, 0x0a, 0x28);
201 ak4642_write(codec, 0x0d, 0x28);
202 ak4642_write(codec, 0x00, 0x64);
203 ak4642_write(codec, 0x01, 0x3b); /* + MCKO bit */
204 ak4642_write(codec, 0x01, 0x7b); /* + MCKO bit */
205 } else {
206 /*
207 * start stereo input
208 *
209 * PLL Master Mode
210 * Audio I/F Format:MSB justified (ADC & DAC)
211 * Sampling Frequency:44.1kHz
212 * Pre MIC AMP:+20dB
213 * MIC Power On
214 * ALC setting:Refer to Table 35
215 * ALC bit=“1”
216 *
217 * This operation came from example code of
218 * "ASAHI KASEI AK4642" (japanese) manual p94.
219 */
220 ak4642_write(codec, 0x05, 0x27);
221 ak4642_write(codec, 0x02, 0x05);
222 ak4642_write(codec, 0x06, 0x3c);
223 ak4642_write(codec, 0x08, 0xe1);
224 ak4642_write(codec, 0x0b, 0x00);
225 ak4642_write(codec, 0x07, 0x21);
226 ak4642_write(codec, 0x00, 0x41);
227 ak4642_write(codec, 0x10, 0x01);
228 }
229
230 return 0;
231}
232
233static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
234 struct snd_soc_dai *dai)
235{
236 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
237 struct snd_soc_codec *codec = dai->codec;
238
239 if (is_play) {
240 /* stop headphone output */
241 ak4642_write(codec, 0x01, 0x3b);
242 ak4642_write(codec, 0x01, 0x0b);
243 ak4642_write(codec, 0x00, 0x40);
244 ak4642_write(codec, 0x0e, 0x11);
245 ak4642_write(codec, 0x0f, 0x08);
246 } else {
247 /* stop stereo input */
248 ak4642_write(codec, 0x00, 0x40);
249 ak4642_write(codec, 0x10, 0x00);
250 ak4642_write(codec, 0x07, 0x01);
251 }
252}
253
254static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
255 int clk_id, unsigned int freq, int dir)
256{
257 struct snd_soc_codec *codec = codec_dai->codec;
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900258 u8 pll;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900259
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900260 switch (freq) {
261 case 11289600:
262 pll = PLL2;
263 break;
264 case 12288000:
265 pll = PLL2 | PLL0;
266 break;
267 case 12000000:
268 pll = PLL2 | PLL1;
269 break;
270 case 24000000:
271 pll = PLL2 | PLL1 | PLL0;
272 break;
273 case 13500000:
274 pll = PLL3 | PLL2;
275 break;
276 case 27000000:
277 pll = PLL3 | PLL2 | PLL0;
278 break;
279 default:
280 return -EINVAL;
281 }
282 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
283
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900284 return 0;
285}
286
287static struct snd_soc_dai_ops ak4642_dai_ops = {
288 .startup = ak4642_dai_startup,
289 .shutdown = ak4642_dai_shutdown,
290 .set_sysclk = ak4642_dai_set_sysclk,
291};
292
293struct snd_soc_dai ak4642_dai = {
294 .name = "AK4642",
295 .playback = {
296 .stream_name = "Playback",
297 .channels_min = 1,
298 .channels_max = 2,
299 .rates = SNDRV_PCM_RATE_8000_48000,
300 .formats = SNDRV_PCM_FMTBIT_S16_LE },
301 .capture = {
302 .stream_name = "Capture",
303 .channels_min = 1,
304 .channels_max = 2,
305 .rates = SNDRV_PCM_RATE_8000_48000,
306 .formats = SNDRV_PCM_FMTBIT_S16_LE },
307 .ops = &ak4642_dai_ops,
308};
309EXPORT_SYMBOL_GPL(ak4642_dai);
310
311static int ak4642_resume(struct platform_device *pdev)
312{
313 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
314 struct snd_soc_codec *codec = socdev->card->codec;
315
316 ak4642_sync(codec);
317 return 0;
318}
319
320/*
321 * initialise the AK4642 driver
322 * register the mixer and dsp interfaces with the kernel
323 */
324static int ak4642_init(struct ak4642_priv *ak4642)
325{
326 struct snd_soc_codec *codec = &ak4642->codec;
327 int ret = 0;
328
329 if (ak4642_codec) {
330 dev_err(codec->dev, "Another ak4642 is registered\n");
331 return -EINVAL;
332 }
333
334 mutex_init(&codec->mutex);
335 INIT_LIST_HEAD(&codec->dapm_widgets);
336 INIT_LIST_HEAD(&codec->dapm_paths);
337
338 codec->private_data = ak4642;
339 codec->name = "AK4642";
340 codec->owner = THIS_MODULE;
341 codec->read = ak4642_read_reg_cache;
342 codec->write = ak4642_write;
343 codec->dai = &ak4642_dai;
344 codec->num_dai = 1;
345 codec->hw_write = (hw_write_t)i2c_master_send;
346 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
347 codec->reg_cache = kmemdup(ak4642_reg,
348 sizeof(ak4642_reg), GFP_KERNEL);
349
350 if (!codec->reg_cache)
351 return -ENOMEM;
352
353 ak4642_dai.dev = codec->dev;
354 ak4642_codec = codec;
355
356 ret = snd_soc_register_codec(codec);
357 if (ret) {
358 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
359 goto reg_cache_err;
360 }
361
362 ret = snd_soc_register_dai(&ak4642_dai);
363 if (ret) {
364 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
365 snd_soc_unregister_codec(codec);
366 goto reg_cache_err;
367 }
368
369 /*
370 * clock setting
371 *
372 * Audio I/F Format: MSB justified (ADC & DAC)
373 * BICK frequency at Master Mode: 64fs
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900374 * MCKO: Enable
375 * Sampling Frequency: 44.1kHz
376 *
377 * This operation came from example code of
378 * "ASAHI KASEI AK4642" (japanese) manual p89.
379 *
380 * please fix-me
381 */
382 ak4642_write(codec, 0x01, 0x08);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900383 ak4642_write(codec, 0x05, 0x27);
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900384 ak4642_write(codec, 0x04, 0x0a);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900385
386 return ret;
387
388reg_cache_err:
389 kfree(codec->reg_cache);
390 codec->reg_cache = NULL;
391
392 return ret;
393}
394
395#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
396static int ak4642_i2c_probe(struct i2c_client *i2c,
397 const struct i2c_device_id *id)
398{
399 struct ak4642_priv *ak4642;
400 struct snd_soc_codec *codec;
401 int ret;
402
403 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
404 if (!ak4642)
405 return -ENOMEM;
406
407 codec = &ak4642->codec;
408 codec->dev = &i2c->dev;
409
410 i2c_set_clientdata(i2c, ak4642);
411 codec->control_data = i2c;
412
413 ret = ak4642_init(ak4642);
414 if (ret < 0)
415 printk(KERN_ERR "failed to initialise AK4642\n");
416
417 return ret;
418}
419
420static int ak4642_i2c_remove(struct i2c_client *client)
421{
422 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
423
424 snd_soc_unregister_dai(&ak4642_dai);
425 snd_soc_unregister_codec(&ak4642->codec);
426 kfree(ak4642->codec.reg_cache);
427 kfree(ak4642);
428 ak4642_codec = NULL;
429
430 return 0;
431}
432
433static const struct i2c_device_id ak4642_i2c_id[] = {
434 { "ak4642", 0 },
435 { "ak4643", 0 },
436 { }
437};
438MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
439
440static struct i2c_driver ak4642_i2c_driver = {
441 .driver = {
442 .name = "AK4642 I2C Codec",
443 .owner = THIS_MODULE,
444 },
445 .probe = ak4642_i2c_probe,
446 .remove = ak4642_i2c_remove,
447 .id_table = ak4642_i2c_id,
448};
449
450#endif
451
452static int ak4642_probe(struct platform_device *pdev)
453{
454 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
455 int ret;
456
457 if (!ak4642_codec) {
458 dev_err(&pdev->dev, "Codec device not registered\n");
459 return -ENODEV;
460 }
461
462 socdev->card->codec = ak4642_codec;
463
464 /* register pcms */
465 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
466 if (ret < 0) {
467 printk(KERN_ERR "ak4642: failed to create pcms\n");
468 goto pcm_err;
469 }
470
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900471 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
472 return ret;
473
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900474pcm_err:
475 return ret;
476
477}
478
479/* power down chip */
480static int ak4642_remove(struct platform_device *pdev)
481{
482 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
483
484 snd_soc_free_pcms(socdev);
485 snd_soc_dapm_free(socdev);
486
487 return 0;
488}
489
490struct snd_soc_codec_device soc_codec_dev_ak4642 = {
491 .probe = ak4642_probe,
492 .remove = ak4642_remove,
493 .resume = ak4642_resume,
494};
495EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
496
497static int __init ak4642_modinit(void)
498{
Kuninori Morimoto1cf86f62009-12-15 15:54:21 +0900499 int ret = 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900500#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
501 ret = i2c_add_driver(&ak4642_i2c_driver);
502#endif
503 return ret;
504
505}
506module_init(ak4642_modinit);
507
508static void __exit ak4642_exit(void)
509{
510#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
511 i2c_del_driver(&ak4642_i2c_driver);
512#endif
513
514}
515module_exit(ak4642_exit);
516
517MODULE_DESCRIPTION("Soc AK4642 driver");
518MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
519MODULE_LICENSE("GPL");