]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - sound/soc/codecs/ak4642.c
ASoC: multi-component - ASoC Multi-Component Support
[mirror_ubuntu-jammy-kernel.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/delay.h>
26 #include <linux/i2c.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32
33 #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
75 /* 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
86 /* 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
93 #define BCKO_MASK (1 << 3)
94 #define BCKO_64 BCKO_MASK
95
96 /* 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
103
104 /*
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 */
112 static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
113
114 static 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
121 /* codec private data */
122 struct ak4642_priv {
123 unsigned int sysclk;
124 enum snd_soc_control_type control_type;
125 void *control_data;
126 };
127
128 /*
129 * ak4642 register cache
130 */
131 static 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 */
147 static 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 */
159 static 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 */
172 static 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
191 static 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
202 static 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)
214 * Bass Boost Level : Middle
215 *
216 * This operation came from example code of
217 * "ASAHI KASEI AK4642" (japanese) manual p97.
218 */
219 ak4642_write(codec, 0x0f, 0x09);
220 ak4642_write(codec, 0x0e, 0x19);
221 ak4642_write(codec, 0x09, 0x91);
222 ak4642_write(codec, 0x0c, 0x91);
223 ak4642_write(codec, 0x00, 0x64);
224 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
225 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
226 } else {
227 /*
228 * start stereo input
229 *
230 * PLL Master Mode
231 * Audio I/F Format:MSB justified (ADC & DAC)
232 * 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 */
240 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
252 static 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 */
260 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
261 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
262 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
273 static 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;
277 u8 pll;
278
279 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
303 return 0;
304 }
305
306 static 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
332 static 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
382 return 0;
383 }
384
385 static 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,
389 .set_fmt = ak4642_dai_set_fmt,
390 .hw_params = ak4642_dai_hw_params,
391 };
392
393 static struct snd_soc_dai_driver ak4642_dai = {
394 .name = "ak4642-hifi",
395 .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,
408 .symmetric_rates = 1,
409 };
410
411 static int ak4642_resume(struct snd_soc_codec *codec)
412 {
413 ak4642_sync(codec);
414 return 0;
415 }
416
417
418 static int ak4642_probe(struct snd_soc_codec *codec)
419 {
420 struct ak4642_priv *ak4642 = snd_soc_codec_get_drvdata(codec);
421
422 dev_info(codec->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
423
424 codec->hw_write = (hw_write_t)i2c_master_send;
425 codec->control_data = ak4642->control_data;
426
427
428 return 0;
429 }
430
431 static 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
441 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
442 static __devinit int ak4642_i2c_probe(struct i2c_client *i2c,
443 const struct i2c_device_id *id)
444 {
445 struct ak4642_priv *ak4642;
446 int ret;
447
448 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
449 if (ak4642 == NULL)
450 return -ENOMEM;
451
452 i2c_set_clientdata(i2c, ak4642);
453 ak4642->control_data = i2c;
454 ak4642->control_type = SND_SOC_I2C;
455
456 ret = snd_soc_register_codec(&i2c->dev,
457 &soc_codec_dev_ak4642, &ak4642_dai, 1);
458 if (ret < 0)
459 kfree(ak4642);
460 return ret;
461 }
462
463 static __devexit int ak4642_i2c_remove(struct i2c_client *client)
464 {
465 snd_soc_unregister_codec(&client->dev);
466 kfree(i2c_get_clientdata(client));
467 return 0;
468 }
469
470 static const struct i2c_device_id ak4642_i2c_id[] = {
471 { "ak4642", 0 },
472 { "ak4643", 0 },
473 { }
474 };
475 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
476
477 static struct i2c_driver ak4642_i2c_driver = {
478 .driver = {
479 .name = "ak4642-codec",
480 .owner = THIS_MODULE,
481 },
482 .probe = ak4642_i2c_probe,
483 .remove = __devexit_p(ak4642_i2c_remove),
484 .id_table = ak4642_i2c_id,
485 };
486 #endif
487
488 static int __init ak4642_modinit(void)
489 {
490 int ret = 0;
491 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
492 ret = i2c_add_driver(&ak4642_i2c_driver);
493 #endif
494 return ret;
495
496 }
497 module_init(ak4642_modinit);
498
499 static 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 }
506 module_exit(ak4642_exit);
507
508 MODULE_DESCRIPTION("Soc AK4642 driver");
509 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
510 MODULE_LICENSE("GPL");