blob: 31b35e967398064d87b1e239d374cdf0810b7638 [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
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090033#define AK4642_VERSION "0.0.1"
34
35#define PW_MGMT1 0x00
36#define PW_MGMT2 0x01
37#define SG_SL1 0x02
38#define SG_SL2 0x03
39#define MD_CTL1 0x04
40#define MD_CTL2 0x05
41#define TIMER 0x06
42#define ALC_CTL1 0x07
43#define ALC_CTL2 0x08
44#define L_IVC 0x09
45#define L_DVC 0x0a
46#define ALC_CTL3 0x0b
47#define R_IVC 0x0c
48#define R_DVC 0x0d
49#define MD_CTL3 0x0e
50#define MD_CTL4 0x0f
51#define PW_MGMT3 0x10
52#define DF_S 0x11
53#define FIL3_0 0x12
54#define FIL3_1 0x13
55#define FIL3_2 0x14
56#define FIL3_3 0x15
57#define EQ_0 0x16
58#define EQ_1 0x17
59#define EQ_2 0x18
60#define EQ_3 0x19
61#define EQ_4 0x1a
62#define EQ_5 0x1b
63#define FIL1_0 0x1c
64#define FIL1_1 0x1d
65#define FIL1_2 0x1e
66#define FIL1_3 0x1f
67#define PW_MGMT4 0x20
68#define MD_CTL5 0x21
69#define LO_MS 0x22
70#define HP_MS 0x23
71#define SPK_MS 0x24
72
73#define AK4642_CACHEREGNUM 0x25
74
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090075/* PW_MGMT2 */
76#define HPMTN (1 << 6)
77#define PMHPL (1 << 5)
78#define PMHPR (1 << 4)
79#define MS (1 << 3) /* master/slave select */
80#define MCKO (1 << 1)
81#define PMPLL (1 << 0)
82
83#define PMHP_MASK (PMHPL | PMHPR)
84#define PMHP PMHP_MASK
85
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +090086/* MD_CTL1 */
87#define PLL3 (1 << 7)
88#define PLL2 (1 << 6)
89#define PLL1 (1 << 5)
90#define PLL0 (1 << 4)
91#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
92
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090093#define BCKO_MASK (1 << 3)
94#define BCKO_64 BCKO_MASK
95
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +090096/* MD_CTL2 */
97#define FS0 (1 << 0)
98#define FS1 (1 << 1)
99#define FS2 (1 << 2)
100#define FS3 (1 << 5)
101#define FS_MASK (FS0 | FS1 | FS2 | FS3)
102
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900103
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900104/*
105 * Playback Volume (table 39)
106 *
107 * max : 0x00 : +12.0 dB
108 * ( 0.5 dB step )
109 * min : 0xFE : -115.0 dB
110 * mute: 0xFF
111 */
112static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
113
114static const struct snd_kcontrol_new ak4642_snd_controls[] = {
115
116 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
117 0, 0xFF, 1, out_tlv),
118};
119
120
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900121/* codec private data */
122struct ak4642_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000123 unsigned int sysclk;
124 enum snd_soc_control_type control_type;
125 void *control_data;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900126};
127
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900128/*
129 * ak4642 register cache
130 */
131static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
132 0x0000, 0x0000, 0x0001, 0x0000,
133 0x0002, 0x0000, 0x0000, 0x0000,
134 0x00e1, 0x00e1, 0x0018, 0x0000,
135 0x00e1, 0x0018, 0x0011, 0x0008,
136 0x0000, 0x0000, 0x0000, 0x0000,
137 0x0000, 0x0000, 0x0000, 0x0000,
138 0x0000, 0x0000, 0x0000, 0x0000,
139 0x0000, 0x0000, 0x0000, 0x0000,
140 0x0000, 0x0000, 0x0000, 0x0000,
141 0x0000,
142};
143
144/*
145 * read ak4642 register cache
146 */
147static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
148 unsigned int reg)
149{
150 u16 *cache = codec->reg_cache;
151 if (reg >= AK4642_CACHEREGNUM)
152 return -1;
153 return cache[reg];
154}
155
156/*
157 * write ak4642 register cache
158 */
159static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
160 u16 reg, unsigned int value)
161{
162 u16 *cache = codec->reg_cache;
163 if (reg >= AK4642_CACHEREGNUM)
164 return;
165
166 cache[reg] = value;
167}
168
169/*
170 * write to the AK4642 register space
171 */
172static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
173 unsigned int value)
174{
175 u8 data[2];
176
177 /* data is
178 * D15..D8 AK4642 register offset
179 * D7...D0 register data
180 */
181 data[0] = reg & 0xff;
182 data[1] = value & 0xff;
183
184 if (codec->hw_write(codec->control_data, data, 2) == 2) {
185 ak4642_write_reg_cache(codec, reg, value);
186 return 0;
187 } else
188 return -EIO;
189}
190
191static int ak4642_sync(struct snd_soc_codec *codec)
192{
193 u16 *cache = codec->reg_cache;
194 int i, r = 0;
195
196 for (i = 0; i < AK4642_CACHEREGNUM; i++)
197 r |= ak4642_write(codec, i, cache[i]);
198
199 return r;
200};
201
202static int ak4642_dai_startup(struct snd_pcm_substream *substream,
203 struct snd_soc_dai *dai)
204{
205 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
206 struct snd_soc_codec *codec = dai->codec;
207
208 if (is_play) {
209 /*
210 * start headphone output
211 *
212 * PLL, Master Mode
213 * Audio I/F Format :MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900214 * Bass Boost Level : Middle
215 *
216 * This operation came from example code of
217 * "ASAHI KASEI AK4642" (japanese) manual p97.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900218 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900219 ak4642_write(codec, 0x0f, 0x09);
220 ak4642_write(codec, 0x0e, 0x19);
221 ak4642_write(codec, 0x09, 0x91);
222 ak4642_write(codec, 0x0c, 0x91);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900223 ak4642_write(codec, 0x00, 0x64);
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900224 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
225 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900226 } else {
227 /*
228 * start stereo input
229 *
230 * PLL Master Mode
231 * Audio I/F Format:MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900232 * Pre MIC AMP:+20dB
233 * MIC Power On
234 * ALC setting:Refer to Table 35
235 * ALC bit=“1”
236 *
237 * This operation came from example code of
238 * "ASAHI KASEI AK4642" (japanese) manual p94.
239 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900240 ak4642_write(codec, 0x02, 0x05);
241 ak4642_write(codec, 0x06, 0x3c);
242 ak4642_write(codec, 0x08, 0xe1);
243 ak4642_write(codec, 0x0b, 0x00);
244 ak4642_write(codec, 0x07, 0x21);
245 ak4642_write(codec, 0x00, 0x41);
246 ak4642_write(codec, 0x10, 0x01);
247 }
248
249 return 0;
250}
251
252static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
253 struct snd_soc_dai *dai)
254{
255 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
256 struct snd_soc_codec *codec = dai->codec;
257
258 if (is_play) {
259 /* stop headphone output */
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900260 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
261 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900262 ak4642_write(codec, 0x00, 0x40);
263 ak4642_write(codec, 0x0e, 0x11);
264 ak4642_write(codec, 0x0f, 0x08);
265 } else {
266 /* stop stereo input */
267 ak4642_write(codec, 0x00, 0x40);
268 ak4642_write(codec, 0x10, 0x00);
269 ak4642_write(codec, 0x07, 0x01);
270 }
271}
272
273static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
274 int clk_id, unsigned int freq, int dir)
275{
276 struct snd_soc_codec *codec = codec_dai->codec;
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900277 u8 pll;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900278
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900279 switch (freq) {
280 case 11289600:
281 pll = PLL2;
282 break;
283 case 12288000:
284 pll = PLL2 | PLL0;
285 break;
286 case 12000000:
287 pll = PLL2 | PLL1;
288 break;
289 case 24000000:
290 pll = PLL2 | PLL1 | PLL0;
291 break;
292 case 13500000:
293 pll = PLL3 | PLL2;
294 break;
295 case 27000000:
296 pll = PLL3 | PLL2 | PLL0;
297 break;
298 default:
299 return -EINVAL;
300 }
301 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
302
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900303 return 0;
304}
305
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900306static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
307{
308 struct snd_soc_codec *codec = dai->codec;
309 u8 data;
310 u8 bcko;
311
312 data = MCKO | PMPLL; /* use MCKO */
313 bcko = 0;
314
315 /* set master/slave audio interface */
316 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
317 case SND_SOC_DAIFMT_CBM_CFM:
318 data |= MS;
319 bcko = BCKO_64;
320 break;
321 case SND_SOC_DAIFMT_CBS_CFS:
322 break;
323 default:
324 return -EINVAL;
325 }
326 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
327 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
328
329 return 0;
330}
331
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900332static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *params,
334 struct snd_soc_dai *dai)
335{
336 struct snd_soc_codec *codec = dai->codec;
337 u8 rate;
338
339 switch (params_rate(params)) {
340 case 7350:
341 rate = FS2;
342 break;
343 case 8000:
344 rate = 0;
345 break;
346 case 11025:
347 rate = FS2 | FS0;
348 break;
349 case 12000:
350 rate = FS0;
351 break;
352 case 14700:
353 rate = FS2 | FS1;
354 break;
355 case 16000:
356 rate = FS1;
357 break;
358 case 22050:
359 rate = FS2 | FS1 | FS0;
360 break;
361 case 24000:
362 rate = FS1 | FS0;
363 break;
364 case 29400:
365 rate = FS3 | FS2 | FS1;
366 break;
367 case 32000:
368 rate = FS3 | FS1;
369 break;
370 case 44100:
371 rate = FS3 | FS2 | FS1 | FS0;
372 break;
373 case 48000:
374 rate = FS3 | FS1 | FS0;
375 break;
376 default:
377 return -EINVAL;
378 break;
379 }
380 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
381
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900382 return 0;
383}
384
385static struct snd_soc_dai_ops ak4642_dai_ops = {
386 .startup = ak4642_dai_startup,
387 .shutdown = ak4642_dai_shutdown,
388 .set_sysclk = ak4642_dai_set_sysclk,
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900389 .set_fmt = ak4642_dai_set_fmt,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900390 .hw_params = ak4642_dai_hw_params,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900391};
392
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000393static struct snd_soc_dai_driver ak4642_dai = {
394 .name = "ak4642-hifi",
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900395 .playback = {
396 .stream_name = "Playback",
397 .channels_min = 1,
398 .channels_max = 2,
399 .rates = SNDRV_PCM_RATE_8000_48000,
400 .formats = SNDRV_PCM_FMTBIT_S16_LE },
401 .capture = {
402 .stream_name = "Capture",
403 .channels_min = 1,
404 .channels_max = 2,
405 .rates = SNDRV_PCM_RATE_8000_48000,
406 .formats = SNDRV_PCM_FMTBIT_S16_LE },
407 .ops = &ak4642_dai_ops,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900408 .symmetric_rates = 1,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900409};
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900410
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000411static int ak4642_resume(struct snd_soc_codec *codec)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900412{
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900413 ak4642_sync(codec);
414 return 0;
415}
416
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000417
418static int ak4642_probe(struct snd_soc_codec *codec)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 struct ak4642_priv *ak4642 = snd_soc_codec_get_drvdata(codec);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900421
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000422 dev_info(codec->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900423
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900424 codec->hw_write = (hw_write_t)i2c_master_send;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 codec->control_data = ak4642->control_data;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900426
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900427
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 return 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900429}
430
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431static struct snd_soc_codec_driver soc_codec_dev_ak4642 = {
432 .probe = ak4642_probe,
433 .resume = ak4642_resume,
434 .read = ak4642_read_reg_cache,
435 .write = ak4642_write,
436 .reg_cache_size = ARRAY_SIZE(ak4642_reg),
437 .reg_word_size = sizeof(u8),
438 .reg_cache_default = ak4642_reg,
439};
440
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900441#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442static __devinit int ak4642_i2c_probe(struct i2c_client *i2c,
443 const struct i2c_device_id *id)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900444{
445 struct ak4642_priv *ak4642;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900446 int ret;
447
448 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 if (ak4642 == NULL)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900450 return -ENOMEM;
451
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900452 i2c_set_clientdata(i2c, ak4642);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453 ak4642->control_data = i2c;
454 ak4642->control_type = SND_SOC_I2C;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900455
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 ret = snd_soc_register_codec(&i2c->dev,
457 &soc_codec_dev_ak4642, &ak4642_dai, 1);
458 if (ret < 0)
Axel Lin7bcaad92010-07-23 05:53:44 +0000459 kfree(ak4642);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900460 return ret;
461}
462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463static __devexit int ak4642_i2c_remove(struct i2c_client *client)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900464{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 snd_soc_unregister_codec(&client->dev);
466 kfree(i2c_get_clientdata(client));
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900467 return 0;
468}
469
470static const struct i2c_device_id ak4642_i2c_id[] = {
471 { "ak4642", 0 },
472 { "ak4643", 0 },
473 { }
474};
475MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
476
477static struct i2c_driver ak4642_i2c_driver = {
478 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479 .name = "ak4642-codec",
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900480 .owner = THIS_MODULE,
481 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482 .probe = ak4642_i2c_probe,
483 .remove = __devexit_p(ak4642_i2c_remove),
484 .id_table = ak4642_i2c_id,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900485};
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900486#endif
487
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900488static int __init ak4642_modinit(void)
489{
Kuninori Morimoto1cf86f62009-12-15 15:54:21 +0900490 int ret = 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900491#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
492 ret = i2c_add_driver(&ak4642_i2c_driver);
493#endif
494 return ret;
495
496}
497module_init(ak4642_modinit);
498
499static void __exit ak4642_exit(void)
500{
501#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
502 i2c_del_driver(&ak4642_i2c_driver);
503#endif
504
505}
506module_exit(ak4642_exit);
507
508MODULE_DESCRIPTION("Soc AK4642 driver");
509MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
510MODULE_LICENSE("GPL");