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