2 * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
7 * Based on wm8731.c by Richard Purdie
8 * Based on ak4535.c by Richard Purdie
9 * Based on wm8753.c by Liam Girdwood
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.
18 * This is very simple driver.
19 * It can use headphone output / stereo input only
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <linux/of_device.h>
30 #include <linux/module.h>
31 #include <linux/regmap.h>
32 #include <sound/soc.h>
33 #include <sound/initval.h>
34 #include <sound/tlv.h>
75 #define PMVCM (1 << 6) /* VCOM Power Management */
76 #define PMMIN (1 << 5) /* MIN Input Power Management */
77 #define PMDAC (1 << 2) /* DAC Power Management */
78 #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
81 #define HPMTN (1 << 6)
82 #define PMHPL (1 << 5)
83 #define PMHPR (1 << 4)
84 #define MS (1 << 3) /* master/slave select */
86 #define PMPLL (1 << 0)
88 #define PMHP_MASK (PMHPL | PMHPR)
89 #define PMHP PMHP_MASK
92 #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
95 #define MINS (1 << 6) /* Switch from MIN to Speaker */
96 #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
97 #define PMMP (1 << 2) /* MPWR pin Power Management */
98 #define MGAIN0 (1 << 0) /* MIC amp gain*/
101 #define LOPS (1 << 6) /* Stero Line-out Power Save Mode */
104 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
105 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
108 #define ALC (1 << 5) /* ALC Enable */
109 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
112 #define PLL3 (1 << 7)
113 #define PLL2 (1 << 6)
114 #define PLL1 (1 << 5)
115 #define PLL0 (1 << 4)
116 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
118 #define BCKO_MASK (1 << 3)
119 #define BCKO_64 BCKO_MASK
121 #define DIF_MASK (3 << 0)
123 #define RIGHT_J (1 << 0)
124 #define LEFT_J (2 << 0)
132 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
135 #define BST1 (1 << 3)
138 #define DACH (1 << 0)
140 struct ak4642_drvdata
{
141 const struct regmap_config
*regmap_config
;
142 int extended_frequencies
;
146 const struct ak4642_drvdata
*drvdata
;
150 * Playback Volume (table 39)
152 * max : 0x00 : +12.0 dB
154 * min : 0xFE : -115.0 dB
157 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11550, 50, 1);
159 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
161 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
162 0, 0xFF, 1, out_tlv
),
163 SOC_SINGLE("ALC Capture Switch", ALC_CTL1
, 5, 1, 0),
164 SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1
, 4, 1, 1),
167 static const struct snd_kcontrol_new ak4642_headphone_control
=
168 SOC_DAPM_SINGLE("Switch", PW_MGMT2
, 6, 1, 0);
170 static const struct snd_kcontrol_new ak4642_lout_mixer_controls
[] = {
171 SOC_DAPM_SINGLE("DACL", SG_SL1
, 4, 1, 0),
175 static int ak4642_lout_event(struct snd_soc_dapm_widget
*w
,
176 struct snd_kcontrol
*kcontrol
, int event
)
178 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
181 case SND_SOC_DAPM_PRE_PMD
:
182 case SND_SOC_DAPM_PRE_PMU
:
183 /* Power save mode ON */
184 snd_soc_update_bits(codec
, SG_SL2
, LOPS
, LOPS
);
186 case SND_SOC_DAPM_POST_PMU
:
187 case SND_SOC_DAPM_POST_PMD
:
188 /* Power save mode OFF */
190 snd_soc_update_bits(codec
, SG_SL2
, LOPS
, 0);
197 static const struct snd_soc_dapm_widget ak4642_dapm_widgets
[] = {
200 SND_SOC_DAPM_OUTPUT("HPOUTL"),
201 SND_SOC_DAPM_OUTPUT("HPOUTR"),
202 SND_SOC_DAPM_OUTPUT("LINEOUT"),
204 SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2
, 5, 0, NULL
, 0),
205 SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2
, 4, 0, NULL
, 0),
206 SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM
, 0, 0,
207 &ak4642_headphone_control
),
209 SND_SOC_DAPM_PGA("DACH", MD_CTL4
, 0, 0, NULL
, 0),
211 SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1
, 3, 0,
212 &ak4642_lout_mixer_controls
[0],
213 ARRAY_SIZE(ak4642_lout_mixer_controls
),
215 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
216 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMD
),
219 SND_SOC_DAPM_DAC("DAC", NULL
, PW_MGMT1
, 2, 0),
222 static const struct snd_soc_dapm_route ak4642_intercon
[] = {
225 {"HPOUTL", NULL
, "HPL Out"},
226 {"HPOUTR", NULL
, "HPR Out"},
227 {"LINEOUT", NULL
, "LINEOUT Mixer"},
229 {"HPL Out", NULL
, "Headphone Enable"},
230 {"HPR Out", NULL
, "Headphone Enable"},
232 {"Headphone Enable", "Switch", "DACH"},
234 {"DACH", NULL
, "DAC"},
236 {"LINEOUT Mixer", "DACL", "DAC"},
238 { "DAC", NULL
, "Playback" },
242 * ak4642 register cache
244 static const struct reg_default ak4642_reg
[] = {
245 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
246 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
247 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
248 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
249 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
250 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
251 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
252 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
253 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
257 static const struct reg_default ak4648_reg
[] = {
258 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
259 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
260 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
261 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
262 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
263 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
264 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
265 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
266 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
267 { 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
270 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
271 struct snd_soc_dai
*dai
)
273 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
274 struct snd_soc_codec
*codec
= dai
->codec
;
278 * start headphone output
281 * Audio I/F Format :MSB justified (ADC & DAC)
282 * Bass Boost Level : Middle
284 * This operation came from example code of
285 * "ASAHI KASEI AK4642" (japanese) manual p97.
287 snd_soc_write(codec
, L_IVC
, 0x91); /* volume */
288 snd_soc_write(codec
, R_IVC
, 0x91); /* volume */
294 * Audio I/F Format:MSB justified (ADC & DAC)
297 * ALC setting:Refer to Table 35
300 * This operation came from example code of
301 * "ASAHI KASEI AK4642" (japanese) manual p94.
303 snd_soc_update_bits(codec
, SG_SL1
, PMMP
| MGAIN0
, PMMP
| MGAIN0
);
304 snd_soc_write(codec
, TIMER
, ZTM(0x3) | WTM(0x3));
305 snd_soc_write(codec
, ALC_CTL1
, ALC
| LMTH0
);
306 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, PMADL
);
307 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, PMADR
);
313 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
314 struct snd_soc_dai
*dai
)
316 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
317 struct snd_soc_codec
*codec
= dai
->codec
;
321 /* stop stereo input */
322 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, 0);
323 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, 0);
324 snd_soc_update_bits(codec
, ALC_CTL1
, ALC
, 0);
328 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
329 int clk_id
, unsigned int freq
, int dir
)
331 struct snd_soc_codec
*codec
= codec_dai
->codec
;
332 struct ak4642_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
334 int extended_freq
= 0;
347 pll
= PLL2
| PLL1
| PLL0
;
353 pll
= PLL3
| PLL2
| PLL0
;
360 pll
= PLL3
| PLL2
| PLL1
;
364 pll
= PLL3
| PLL2
| PLL1
| PLL0
;
371 if (extended_freq
&& !priv
->drvdata
->extended_frequencies
)
374 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
379 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
381 struct snd_soc_codec
*codec
= dai
->codec
;
385 data
= MCKO
| PMPLL
; /* use MCKO */
388 /* set master/slave audio interface */
389 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
390 case SND_SOC_DAIFMT_CBM_CFM
:
394 case SND_SOC_DAIFMT_CBS_CFS
:
399 snd_soc_update_bits(codec
, PW_MGMT2
, MS
| MCKO
| PMPLL
, data
);
400 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
404 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
405 case SND_SOC_DAIFMT_LEFT_J
:
408 case SND_SOC_DAIFMT_I2S
:
412 * Please add RIGHT_J / DSP support here
417 snd_soc_update_bits(codec
, MD_CTL1
, DIF_MASK
, data
);
422 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
423 struct snd_pcm_hw_params
*params
,
424 struct snd_soc_dai
*dai
)
426 struct snd_soc_codec
*codec
= dai
->codec
;
429 switch (params_rate(params
)) {
449 rate
= FS2
| FS1
| FS0
;
455 rate
= FS3
| FS2
| FS1
;
461 rate
= FS3
| FS2
| FS1
| FS0
;
464 rate
= FS3
| FS1
| FS0
;
469 snd_soc_update_bits(codec
, MD_CTL2
, FS_MASK
, rate
);
474 static int ak4642_set_bias_level(struct snd_soc_codec
*codec
,
475 enum snd_soc_bias_level level
)
478 case SND_SOC_BIAS_OFF
:
479 snd_soc_write(codec
, PW_MGMT1
, 0x00);
482 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
, PMVCM
);
485 codec
->dapm
.bias_level
= level
;
490 static const struct snd_soc_dai_ops ak4642_dai_ops
= {
491 .startup
= ak4642_dai_startup
,
492 .shutdown
= ak4642_dai_shutdown
,
493 .set_sysclk
= ak4642_dai_set_sysclk
,
494 .set_fmt
= ak4642_dai_set_fmt
,
495 .hw_params
= ak4642_dai_hw_params
,
498 static struct snd_soc_dai_driver ak4642_dai
= {
499 .name
= "ak4642-hifi",
501 .stream_name
= "Playback",
504 .rates
= SNDRV_PCM_RATE_8000_48000
,
505 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
507 .stream_name
= "Capture",
510 .rates
= SNDRV_PCM_RATE_8000_48000
,
511 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
512 .ops
= &ak4642_dai_ops
,
513 .symmetric_rates
= 1,
516 static int ak4642_resume(struct snd_soc_codec
*codec
)
518 struct regmap
*regmap
= dev_get_regmap(codec
->dev
, NULL
);
520 regcache_mark_dirty(regmap
);
521 regcache_sync(regmap
);
525 static struct snd_soc_codec_driver soc_codec_dev_ak4642
= {
526 .resume
= ak4642_resume
,
527 .set_bias_level
= ak4642_set_bias_level
,
528 .controls
= ak4642_snd_controls
,
529 .num_controls
= ARRAY_SIZE(ak4642_snd_controls
),
530 .dapm_widgets
= ak4642_dapm_widgets
,
531 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
532 .dapm_routes
= ak4642_intercon
,
533 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
536 static const struct regmap_config ak4642_regmap
= {
539 .max_register
= ARRAY_SIZE(ak4642_reg
) + 1,
540 .reg_defaults
= ak4642_reg
,
541 .num_reg_defaults
= ARRAY_SIZE(ak4642_reg
),
544 static const struct regmap_config ak4648_regmap
= {
547 .max_register
= ARRAY_SIZE(ak4648_reg
) + 1,
548 .reg_defaults
= ak4648_reg
,
549 .num_reg_defaults
= ARRAY_SIZE(ak4648_reg
),
552 static const struct ak4642_drvdata ak4642_drvdata
= {
553 .regmap_config
= &ak4642_regmap
,
556 static const struct ak4642_drvdata ak4643_drvdata
= {
557 .regmap_config
= &ak4642_regmap
,
560 static const struct ak4642_drvdata ak4648_drvdata
= {
561 .regmap_config
= &ak4648_regmap
,
562 .extended_frequencies
= 1,
565 static const struct of_device_id ak4642_of_match
[];
566 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
567 const struct i2c_device_id
*id
)
569 struct device_node
*np
= i2c
->dev
.of_node
;
570 const struct ak4642_drvdata
*drvdata
= NULL
;
571 struct regmap
*regmap
;
572 struct ak4642_priv
*priv
;
575 const struct of_device_id
*of_id
;
577 of_id
= of_match_device(ak4642_of_match
, &i2c
->dev
);
579 drvdata
= of_id
->data
;
581 drvdata
= (const struct ak4642_drvdata
*)id
->driver_data
;
585 dev_err(&i2c
->dev
, "Unknown device type\n");
589 priv
= devm_kzalloc(&i2c
->dev
, sizeof(*priv
), GFP_KERNEL
);
593 priv
->drvdata
= drvdata
;
595 i2c_set_clientdata(i2c
, priv
);
597 regmap
= devm_regmap_init_i2c(i2c
, drvdata
->regmap_config
);
599 return PTR_ERR(regmap
);
601 return snd_soc_register_codec(&i2c
->dev
,
602 &soc_codec_dev_ak4642
, &ak4642_dai
, 1);
605 static int ak4642_i2c_remove(struct i2c_client
*client
)
607 snd_soc_unregister_codec(&client
->dev
);
611 static const struct of_device_id ak4642_of_match
[] = {
612 { .compatible
= "asahi-kasei,ak4642", .data
= &ak4642_drvdata
},
613 { .compatible
= "asahi-kasei,ak4643", .data
= &ak4643_drvdata
},
614 { .compatible
= "asahi-kasei,ak4648", .data
= &ak4648_drvdata
},
617 MODULE_DEVICE_TABLE(of
, ak4642_of_match
);
619 static const struct i2c_device_id ak4642_i2c_id
[] = {
620 { "ak4642", (kernel_ulong_t
)&ak4642_drvdata
},
621 { "ak4643", (kernel_ulong_t
)&ak4643_drvdata
},
622 { "ak4648", (kernel_ulong_t
)&ak4648_drvdata
},
625 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
627 static struct i2c_driver ak4642_i2c_driver
= {
629 .name
= "ak4642-codec",
630 .owner
= THIS_MODULE
,
631 .of_match_table
= ak4642_of_match
,
633 .probe
= ak4642_i2c_probe
,
634 .remove
= ak4642_i2c_remove
,
635 .id_table
= ak4642_i2c_id
,
638 module_i2c_driver(ak4642_i2c_driver
);
640 MODULE_DESCRIPTION("Soc AK4642 driver");
641 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
642 MODULE_LICENSE("GPL");