ASoC: ak4642: Add set_fmt function for snd_soc_dai_ops
[deliverable/linux.git] / sound / soc / codecs / ak4642.c
CommitLineData
a3a83d9a
KM
1/*
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
0643ce8f
KM
83/* PW_MGMT2 */
84#define HPMTN (1 << 6)
85#define PMHPL (1 << 5)
86#define PMHPR (1 << 4)
87#define MS (1 << 3) /* master/slave select */
88#define MCKO (1 << 1)
89#define PMPLL (1 << 0)
90
91#define PMHP_MASK (PMHPL | PMHPR)
92#define PMHP PMHP_MASK
93
4b6316b4
KM
94/* MD_CTL1 */
95#define PLL3 (1 << 7)
96#define PLL2 (1 << 6)
97#define PLL1 (1 << 5)
98#define PLL0 (1 << 4)
99#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
100
0643ce8f
KM
101#define BCKO_MASK (1 << 3)
102#define BCKO_64 BCKO_MASK
103
a3a83d9a
KM
104struct snd_soc_codec_device soc_codec_dev_ak4642;
105
106/* codec private data */
107struct ak4642_priv {
108 struct snd_soc_codec codec;
a3a83d9a
KM
109};
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)
199 * Sampling Frequency: 44.1kHz
200 * Digital Volume: −8dB
201 * Bass Boost Level : Middle
202 *
203 * This operation came from example code of
204 * "ASAHI KASEI AK4642" (japanese) manual p97.
a3a83d9a
KM
205 */
206 ak4642_write(codec, 0x05, 0x27);
207 ak4642_write(codec, 0x0f, 0x09);
208 ak4642_write(codec, 0x0e, 0x19);
209 ak4642_write(codec, 0x09, 0x91);
210 ak4642_write(codec, 0x0c, 0x91);
211 ak4642_write(codec, 0x0a, 0x28);
212 ak4642_write(codec, 0x0d, 0x28);
213 ak4642_write(codec, 0x00, 0x64);
0643ce8f
KM
214 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
215 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
a3a83d9a
KM
216 } else {
217 /*
218 * start stereo input
219 *
220 * PLL Master Mode
221 * Audio I/F Format:MSB justified (ADC & DAC)
222 * Sampling Frequency:44.1kHz
223 * Pre MIC AMP:+20dB
224 * MIC Power On
225 * ALC setting:Refer to Table 35
226 * ALC bit=“1”
227 *
228 * This operation came from example code of
229 * "ASAHI KASEI AK4642" (japanese) manual p94.
230 */
231 ak4642_write(codec, 0x05, 0x27);
232 ak4642_write(codec, 0x02, 0x05);
233 ak4642_write(codec, 0x06, 0x3c);
234 ak4642_write(codec, 0x08, 0xe1);
235 ak4642_write(codec, 0x0b, 0x00);
236 ak4642_write(codec, 0x07, 0x21);
237 ak4642_write(codec, 0x00, 0x41);
238 ak4642_write(codec, 0x10, 0x01);
239 }
240
241 return 0;
242}
243
244static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
245 struct snd_soc_dai *dai)
246{
247 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
248 struct snd_soc_codec *codec = dai->codec;
249
250 if (is_play) {
251 /* stop headphone output */
0643ce8f
KM
252 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
253 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
a3a83d9a
KM
254 ak4642_write(codec, 0x00, 0x40);
255 ak4642_write(codec, 0x0e, 0x11);
256 ak4642_write(codec, 0x0f, 0x08);
257 } else {
258 /* stop stereo input */
259 ak4642_write(codec, 0x00, 0x40);
260 ak4642_write(codec, 0x10, 0x00);
261 ak4642_write(codec, 0x07, 0x01);
262 }
263}
264
265static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
266 int clk_id, unsigned int freq, int dir)
267{
268 struct snd_soc_codec *codec = codec_dai->codec;
4b6316b4
KM
269 u8 pll;
270
271 switch (freq) {
272 case 11289600:
273 pll = PLL2;
274 break;
275 case 12288000:
276 pll = PLL2 | PLL0;
277 break;
278 case 12000000:
279 pll = PLL2 | PLL1;
280 break;
281 case 24000000:
282 pll = PLL2 | PLL1 | PLL0;
283 break;
284 case 13500000:
285 pll = PLL3 | PLL2;
286 break;
287 case 27000000:
288 pll = PLL3 | PLL2 | PLL0;
289 break;
290 default:
291 return -EINVAL;
292 }
293 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
a3a83d9a 294
a3a83d9a
KM
295 return 0;
296}
297
0643ce8f
KM
298static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
299{
300 struct snd_soc_codec *codec = dai->codec;
301 u8 data;
302 u8 bcko;
303
304 data = MCKO | PMPLL; /* use MCKO */
305 bcko = 0;
306
307 /* set master/slave audio interface */
308 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
309 case SND_SOC_DAIFMT_CBM_CFM:
310 data |= MS;
311 bcko = BCKO_64;
312 break;
313 case SND_SOC_DAIFMT_CBS_CFS:
314 break;
315 default:
316 return -EINVAL;
317 }
318 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
319 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
320
321 return 0;
322}
323
a3a83d9a
KM
324static struct snd_soc_dai_ops ak4642_dai_ops = {
325 .startup = ak4642_dai_startup,
326 .shutdown = ak4642_dai_shutdown,
327 .set_sysclk = ak4642_dai_set_sysclk,
0643ce8f 328 .set_fmt = ak4642_dai_set_fmt,
a3a83d9a
KM
329};
330
331struct snd_soc_dai ak4642_dai = {
332 .name = "AK4642",
333 .playback = {
334 .stream_name = "Playback",
335 .channels_min = 1,
336 .channels_max = 2,
337 .rates = SNDRV_PCM_RATE_8000_48000,
338 .formats = SNDRV_PCM_FMTBIT_S16_LE },
339 .capture = {
340 .stream_name = "Capture",
341 .channels_min = 1,
342 .channels_max = 2,
343 .rates = SNDRV_PCM_RATE_8000_48000,
344 .formats = SNDRV_PCM_FMTBIT_S16_LE },
345 .ops = &ak4642_dai_ops,
346};
347EXPORT_SYMBOL_GPL(ak4642_dai);
348
349static int ak4642_resume(struct platform_device *pdev)
350{
351 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
352 struct snd_soc_codec *codec = socdev->card->codec;
353
354 ak4642_sync(codec);
355 return 0;
356}
357
358/*
359 * initialise the AK4642 driver
360 * register the mixer and dsp interfaces with the kernel
361 */
362static int ak4642_init(struct ak4642_priv *ak4642)
363{
364 struct snd_soc_codec *codec = &ak4642->codec;
365 int ret = 0;
366
367 if (ak4642_codec) {
368 dev_err(codec->dev, "Another ak4642 is registered\n");
369 return -EINVAL;
370 }
371
372 mutex_init(&codec->mutex);
373 INIT_LIST_HEAD(&codec->dapm_widgets);
374 INIT_LIST_HEAD(&codec->dapm_paths);
375
376 codec->private_data = ak4642;
377 codec->name = "AK4642";
378 codec->owner = THIS_MODULE;
379 codec->read = ak4642_read_reg_cache;
380 codec->write = ak4642_write;
381 codec->dai = &ak4642_dai;
382 codec->num_dai = 1;
383 codec->hw_write = (hw_write_t)i2c_master_send;
384 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
385 codec->reg_cache = kmemdup(ak4642_reg,
386 sizeof(ak4642_reg), GFP_KERNEL);
387
388 if (!codec->reg_cache)
389 return -ENOMEM;
390
391 ak4642_dai.dev = codec->dev;
392 ak4642_codec = codec;
393
394 ret = snd_soc_register_codec(codec);
395 if (ret) {
396 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
397 goto reg_cache_err;
398 }
399
400 ret = snd_soc_register_dai(&ak4642_dai);
401 if (ret) {
402 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
403 snd_soc_unregister_codec(codec);
404 goto reg_cache_err;
405 }
406
a3a83d9a
KM
407 return ret;
408
409reg_cache_err:
410 kfree(codec->reg_cache);
411 codec->reg_cache = NULL;
412
413 return ret;
414}
415
416#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
417static int ak4642_i2c_probe(struct i2c_client *i2c,
418 const struct i2c_device_id *id)
419{
420 struct ak4642_priv *ak4642;
421 struct snd_soc_codec *codec;
422 int ret;
423
424 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
425 if (!ak4642)
426 return -ENOMEM;
427
428 codec = &ak4642->codec;
429 codec->dev = &i2c->dev;
430
431 i2c_set_clientdata(i2c, ak4642);
432 codec->control_data = i2c;
433
434 ret = ak4642_init(ak4642);
435 if (ret < 0)
436 printk(KERN_ERR "failed to initialise AK4642\n");
437
438 return ret;
439}
440
441static int ak4642_i2c_remove(struct i2c_client *client)
442{
443 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
444
445 snd_soc_unregister_dai(&ak4642_dai);
446 snd_soc_unregister_codec(&ak4642->codec);
447 kfree(ak4642->codec.reg_cache);
448 kfree(ak4642);
449 ak4642_codec = NULL;
450
451 return 0;
452}
453
454static const struct i2c_device_id ak4642_i2c_id[] = {
455 { "ak4642", 0 },
456 { "ak4643", 0 },
457 { }
458};
459MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
460
461static struct i2c_driver ak4642_i2c_driver = {
462 .driver = {
463 .name = "AK4642 I2C Codec",
464 .owner = THIS_MODULE,
465 },
466 .probe = ak4642_i2c_probe,
467 .remove = ak4642_i2c_remove,
468 .id_table = ak4642_i2c_id,
469};
470
471#endif
472
473static int ak4642_probe(struct platform_device *pdev)
474{
475 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
476 int ret;
477
478 if (!ak4642_codec) {
479 dev_err(&pdev->dev, "Codec device not registered\n");
480 return -ENODEV;
481 }
482
483 socdev->card->codec = ak4642_codec;
484
485 /* register pcms */
486 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
487 if (ret < 0) {
488 printk(KERN_ERR "ak4642: failed to create pcms\n");
489 goto pcm_err;
490 }
491
a3a83d9a
KM
492 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
493 return ret;
494
a3a83d9a
KM
495pcm_err:
496 return ret;
497
498}
499
500/* power down chip */
501static int ak4642_remove(struct platform_device *pdev)
502{
503 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
504
505 snd_soc_free_pcms(socdev);
506 snd_soc_dapm_free(socdev);
507
508 return 0;
509}
510
511struct snd_soc_codec_device soc_codec_dev_ak4642 = {
512 .probe = ak4642_probe,
513 .remove = ak4642_remove,
514 .resume = ak4642_resume,
515};
516EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
517
518static int __init ak4642_modinit(void)
519{
1cf86f6f 520 int ret = 0;
a3a83d9a
KM
521#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
522 ret = i2c_add_driver(&ak4642_i2c_driver);
523#endif
524 return ret;
525
526}
527module_init(ak4642_modinit);
528
529static void __exit ak4642_exit(void)
530{
531#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
532 i2c_del_driver(&ak4642_i2c_driver);
533#endif
534
535}
536module_exit(ak4642_exit);
537
538MODULE_DESCRIPTION("Soc AK4642 driver");
539MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
540MODULE_LICENSE("GPL");
This page took 0.054312 seconds and 5 git commands to generate.