blob: dab127603ff6e7b107b9ab594b2c75c99bbabef4 [file] [log] [blame]
Kuninori Morimotob0757062015-09-15 08:26:36 +00001/*
2 * ak4613.c -- Asahi Kasei ALSA Soc Audio driver
3 *
4 * Copyright (C) 2015 Renesas Electronics Corporation
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * Based on ak4642.c by Kuninori Morimoto
8 * Based on wm8731.c by Richard Purdie
9 * Based on ak4535.c by Richard Purdie
10 * Based on wm8753.c by Liam Girdwood
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/clk.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/of_device.h>
21#include <linux/module.h>
22#include <linux/regmap.h>
23#include <sound/soc.h>
24#include <sound/pcm_params.h>
Kuninori Morimotoe3a4d952015-09-18 00:52:44 +000025#include <sound/tlv.h>
Kuninori Morimotob0757062015-09-15 08:26:36 +000026
27#define PW_MGMT1 0x00 /* Power Management 1 */
28#define PW_MGMT2 0x01 /* Power Management 2 */
29#define PW_MGMT3 0x02 /* Power Management 3 */
30#define CTRL1 0x03 /* Control 1 */
31#define CTRL2 0x04 /* Control 2 */
32#define DEMP1 0x05 /* De-emphasis1 */
33#define DEMP2 0x06 /* De-emphasis2 */
34#define OFD 0x07 /* Overflow Detect */
35#define ZRD 0x08 /* Zero Detect */
36#define ICTRL 0x09 /* Input Control */
37#define OCTRL 0x0a /* Output Control */
38#define LOUT1 0x0b /* LOUT1 Volume Control */
39#define ROUT1 0x0c /* ROUT1 Volume Control */
40#define LOUT2 0x0d /* LOUT2 Volume Control */
41#define ROUT2 0x0e /* ROUT2 Volume Control */
42#define LOUT3 0x0f /* LOUT3 Volume Control */
43#define ROUT3 0x10 /* ROUT3 Volume Control */
44#define LOUT4 0x11 /* LOUT4 Volume Control */
45#define ROUT4 0x12 /* ROUT4 Volume Control */
46#define LOUT5 0x13 /* LOUT5 Volume Control */
47#define ROUT5 0x14 /* ROUT5 Volume Control */
48#define LOUT6 0x15 /* LOUT6 Volume Control */
49#define ROUT6 0x16 /* ROUT6 Volume Control */
50
51/* PW_MGMT1 */
52#define RSTN BIT(0)
53#define PMDAC BIT(1)
54#define PMADC BIT(2)
55#define PMVR BIT(3)
56
57/* PW_MGMT2 */
58#define PMAD_ALL 0x7
59
60/* PW_MGMT3 */
61#define PMDA_ALL 0x3f
62
63/* CTRL1 */
64#define DIF0 BIT(3)
65#define DIF1 BIT(4)
66#define DIF2 BIT(5)
67#define TDM0 BIT(6)
68#define TDM1 BIT(7)
69#define NO_FMT (0xff)
70#define FMT_MASK (0xf8)
71
72/* CTRL2 */
73#define DFS_NORMAL_SPEED (0 << 2)
74#define DFS_DOUBLE_SPEED (1 << 2)
75#define DFS_QUAD_SPEED (2 << 2)
76
Kuninori Morimotob0757062015-09-15 08:26:36 +000077struct ak4613_formats {
78 unsigned int width;
79 unsigned int fmt;
80};
81
82struct ak4613_interface {
83 struct ak4613_formats capture;
84 struct ak4613_formats playback;
85};
86
Kuninori Morimoto35299f12015-11-16 06:01:29 +000087struct ak4613_priv {
88 struct mutex lock;
89 const struct ak4613_interface *iface;
90
91 unsigned int fmt;
92 u8 oc;
93 u8 ic;
94 int cnt;
95};
96
Kuninori Morimotoe3a4d952015-09-18 00:52:44 +000097/*
98 * Playback Volume
99 *
100 * max : 0x00 : 0 dB
101 * ( 0.5 dB step )
102 * min : 0xFE : -127.0 dB
103 * mute: 0xFF
104 */
105static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
106
107static const struct snd_kcontrol_new ak4613_snd_controls[] = {
108 SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
109 0, 0xFF, 1, out_tlv),
110 SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
111 0, 0xFF, 1, out_tlv),
112 SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
113 0, 0xFF, 1, out_tlv),
114 SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
115 0, 0xFF, 1, out_tlv),
116 SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
117 0, 0xFF, 1, out_tlv),
118 SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
119 0, 0xFF, 1, out_tlv),
120};
121
Kuninori Morimotob0757062015-09-15 08:26:36 +0000122static const struct reg_default ak4613_reg[] = {
123 { 0x0, 0x0f }, { 0x1, 0x07 }, { 0x2, 0x3f }, { 0x3, 0x20 },
124 { 0x4, 0x20 }, { 0x5, 0x55 }, { 0x6, 0x05 }, { 0x7, 0x07 },
125 { 0x8, 0x0f }, { 0x9, 0x07 }, { 0xa, 0x3f }, { 0xb, 0x00 },
126 { 0xc, 0x00 }, { 0xd, 0x00 }, { 0xe, 0x00 }, { 0xf, 0x00 },
127 { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
128 { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
129};
130
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000131#define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
Kuninori Morimotob0757062015-09-15 08:26:36 +0000132#define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
133static const struct ak4613_interface ak4613_iface[] = {
134 /* capture */ /* playback */
135 [0] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(16, RIGHT_J) },
136 [1] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(20, RIGHT_J) },
137 [2] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, RIGHT_J) },
138 [3] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, LEFT_J) },
139 [4] = { AUDIO_IFACE(24, I2S), AUDIO_IFACE(24, I2S) },
140};
141
142static const struct regmap_config ak4613_regmap_cfg = {
143 .reg_bits = 8,
144 .val_bits = 8,
145 .max_register = 0x16,
146 .reg_defaults = ak4613_reg,
147 .num_reg_defaults = ARRAY_SIZE(ak4613_reg),
148};
149
150static const struct of_device_id ak4613_of_match[] = {
151 { .compatible = "asahi-kasei,ak4613", .data = &ak4613_regmap_cfg },
152 {},
153};
154MODULE_DEVICE_TABLE(of, ak4613_of_match);
155
156static const struct i2c_device_id ak4613_i2c_id[] = {
157 { "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
158 { }
159};
160MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
161
162static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
163
164 /* Outputs */
165 SND_SOC_DAPM_OUTPUT("LOUT1"),
166 SND_SOC_DAPM_OUTPUT("LOUT2"),
167 SND_SOC_DAPM_OUTPUT("LOUT3"),
168 SND_SOC_DAPM_OUTPUT("LOUT4"),
169 SND_SOC_DAPM_OUTPUT("LOUT5"),
170 SND_SOC_DAPM_OUTPUT("LOUT6"),
171
172 SND_SOC_DAPM_OUTPUT("ROUT1"),
173 SND_SOC_DAPM_OUTPUT("ROUT2"),
174 SND_SOC_DAPM_OUTPUT("ROUT3"),
175 SND_SOC_DAPM_OUTPUT("ROUT4"),
176 SND_SOC_DAPM_OUTPUT("ROUT5"),
177 SND_SOC_DAPM_OUTPUT("ROUT6"),
178
179 /* Inputs */
180 SND_SOC_DAPM_INPUT("LIN1"),
181 SND_SOC_DAPM_INPUT("LIN2"),
182
183 SND_SOC_DAPM_INPUT("RIN1"),
184 SND_SOC_DAPM_INPUT("RIN2"),
185
186 /* DAC */
187 SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
188 SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
189 SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
190 SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
191 SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
192 SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
193
194 /* ADC */
195 SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
196 SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
197};
198
199static const struct snd_soc_dapm_route ak4613_intercon[] = {
200 {"LOUT1", NULL, "DAC1"},
201 {"LOUT2", NULL, "DAC2"},
202 {"LOUT3", NULL, "DAC3"},
203 {"LOUT4", NULL, "DAC4"},
204 {"LOUT5", NULL, "DAC5"},
205 {"LOUT6", NULL, "DAC6"},
206
207 {"ROUT1", NULL, "DAC1"},
208 {"ROUT2", NULL, "DAC2"},
209 {"ROUT3", NULL, "DAC3"},
210 {"ROUT4", NULL, "DAC4"},
211 {"ROUT5", NULL, "DAC5"},
212 {"ROUT6", NULL, "DAC6"},
213
214 {"DAC1", NULL, "Playback"},
215 {"DAC2", NULL, "Playback"},
216 {"DAC3", NULL, "Playback"},
217 {"DAC4", NULL, "Playback"},
218 {"DAC5", NULL, "Playback"},
219 {"DAC6", NULL, "Playback"},
220
221 {"Capture", NULL, "ADC1"},
222 {"Capture", NULL, "ADC2"},
223
224 {"ADC1", NULL, "LIN1"},
225 {"ADC2", NULL, "LIN2"},
226
227 {"ADC1", NULL, "RIN1"},
228 {"ADC2", NULL, "RIN2"},
229};
230
231static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
232 struct snd_soc_dai *dai)
233{
234 struct snd_soc_codec *codec = dai->codec;
235 struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
236 struct device *dev = codec->dev;
237
238 mutex_lock(&priv->lock);
239 priv->cnt--;
240 if (priv->cnt < 0) {
241 dev_err(dev, "unexpected counter error\n");
242 priv->cnt = 0;
243 }
244 if (!priv->cnt)
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000245 priv->iface = NULL;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000246 mutex_unlock(&priv->lock);
247}
248
249static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
250{
251 struct snd_soc_codec *codec = dai->codec;
252 struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
253
254 fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
255
256 switch (fmt) {
257 case SND_SOC_DAIFMT_RIGHT_J:
258 case SND_SOC_DAIFMT_LEFT_J:
259 case SND_SOC_DAIFMT_I2S:
260 priv->fmt = fmt;
261
262 break;
263 default:
264 return -EINVAL;
265 }
266
267 return 0;
268}
269
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000270static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
271 int is_play,
272 unsigned int fmt, unsigned int width)
273{
274 const struct ak4613_formats *fmts;
275
276 fmts = (is_play) ? &iface->playback : &iface->capture;
277
278 if (fmts->fmt != fmt)
279 return false;
280
281 if (fmt == SND_SOC_DAIFMT_RIGHT_J) {
282 if (fmts->width != width)
283 return false;
284 } else {
285 if (fmts->width < width)
286 return false;
287 }
288
289 return true;
290}
291
Kuninori Morimotob0757062015-09-15 08:26:36 +0000292static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
293 struct snd_pcm_hw_params *params,
294 struct snd_soc_dai *dai)
295{
296 struct snd_soc_codec *codec = dai->codec;
297 struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000298 const struct ak4613_interface *iface;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000299 struct device *dev = codec->dev;
300 unsigned int width = params_width(params);
301 unsigned int fmt = priv->fmt;
302 unsigned int rate;
303 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
304 int i, ret;
305 u8 fmt_ctrl, ctrl2;
306
307 rate = params_rate(params);
308 switch (rate) {
309 case 32000:
310 case 44100:
311 case 48000:
312 ctrl2 = DFS_NORMAL_SPEED;
313 break;
314 case 88200:
315 case 96000:
316 ctrl2 = DFS_DOUBLE_SPEED;
317 break;
318 case 176400:
319 case 192000:
320 ctrl2 = DFS_QUAD_SPEED;
321 break;
322 default:
323 return -EINVAL;
324 }
325
326 /*
327 * FIXME
328 *
329 * It doesn't support TDM at this point
330 */
331 fmt_ctrl = NO_FMT;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000332 ret = -EINVAL;
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000333 iface = NULL;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000334
335 mutex_lock(&priv->lock);
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000336 if (priv->iface) {
337 if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
338 iface = priv->iface;
339 } else {
340 for (i = ARRAY_SIZE(ak4613_iface); i >= 0; i--) {
341 if (!ak4613_dai_fmt_matching(ak4613_iface + i,
342 is_play,
343 fmt, width))
344 continue;
345 iface = ak4613_iface + i;
346 break;
347 }
348 }
349
350 if ((priv->iface == NULL) ||
351 (priv->iface == iface)) {
352 priv->iface = iface;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000353 priv->cnt++;
354 ret = 0;
355 }
356 mutex_unlock(&priv->lock);
357
358 if (ret < 0)
359 goto hw_params_end;
360
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000361 fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);
362
Kuninori Morimotob0757062015-09-15 08:26:36 +0000363 snd_soc_update_bits(codec, CTRL1, FMT_MASK, fmt_ctrl);
364 snd_soc_write(codec, CTRL2, ctrl2);
365
Kuninori Morimotoa3af0c62015-11-16 04:51:21 +0000366 snd_soc_write(codec, ICTRL, priv->ic);
367 snd_soc_write(codec, OCTRL, priv->oc);
368
Kuninori Morimotob0757062015-09-15 08:26:36 +0000369hw_params_end:
370 if (ret < 0)
371 dev_warn(dev, "unsupported data width/format combination\n");
372
373 return ret;
374}
375
376static int ak4613_set_bias_level(struct snd_soc_codec *codec,
377 enum snd_soc_bias_level level)
378{
379 u8 mgmt1 = 0;
380
381 switch (level) {
382 case SND_SOC_BIAS_ON:
383 mgmt1 |= RSTN;
384 /* fall through */
385 case SND_SOC_BIAS_PREPARE:
386 mgmt1 |= PMADC | PMDAC;
387 /* fall through */
388 case SND_SOC_BIAS_STANDBY:
389 mgmt1 |= PMVR;
390 /* fall through */
391 case SND_SOC_BIAS_OFF:
392 default:
393 break;
394 }
395
396 snd_soc_write(codec, PW_MGMT1, mgmt1);
397
398 return 0;
399}
400
401static const struct snd_soc_dai_ops ak4613_dai_ops = {
402 .shutdown = ak4613_dai_shutdown,
403 .set_fmt = ak4613_dai_set_fmt,
404 .hw_params = ak4613_dai_hw_params,
405};
406
407#define AK4613_PCM_RATE (SNDRV_PCM_RATE_32000 |\
408 SNDRV_PCM_RATE_44100 |\
409 SNDRV_PCM_RATE_48000 |\
410 SNDRV_PCM_RATE_64000 |\
411 SNDRV_PCM_RATE_88200 |\
412 SNDRV_PCM_RATE_96000 |\
413 SNDRV_PCM_RATE_176400 |\
414 SNDRV_PCM_RATE_192000)
415#define AK4613_PCM_FMTBIT (SNDRV_PCM_FMTBIT_S16_LE |\
416 SNDRV_PCM_FMTBIT_S24_LE)
417
418static struct snd_soc_dai_driver ak4613_dai = {
419 .name = "ak4613-hifi",
420 .playback = {
421 .stream_name = "Playback",
422 .channels_min = 2,
423 .channels_max = 2,
424 .rates = AK4613_PCM_RATE,
425 .formats = AK4613_PCM_FMTBIT,
426 },
427 .capture = {
428 .stream_name = "Capture",
429 .channels_min = 2,
430 .channels_max = 2,
431 .rates = AK4613_PCM_RATE,
432 .formats = AK4613_PCM_FMTBIT,
433 },
434 .ops = &ak4613_dai_ops,
435 .symmetric_rates = 1,
436};
437
438static int ak4613_resume(struct snd_soc_codec *codec)
439{
440 struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
441
442 regcache_mark_dirty(regmap);
443 return regcache_sync(regmap);
444}
445
446static struct snd_soc_codec_driver soc_codec_dev_ak4613 = {
447 .resume = ak4613_resume,
448 .set_bias_level = ak4613_set_bias_level,
Kuninori Morimotoe3a4d952015-09-18 00:52:44 +0000449 .controls = ak4613_snd_controls,
450 .num_controls = ARRAY_SIZE(ak4613_snd_controls),
Kuninori Morimotob0757062015-09-15 08:26:36 +0000451 .dapm_widgets = ak4613_dapm_widgets,
452 .num_dapm_widgets = ARRAY_SIZE(ak4613_dapm_widgets),
453 .dapm_routes = ak4613_intercon,
454 .num_dapm_routes = ARRAY_SIZE(ak4613_intercon),
455};
456
Kuninori Morimotoa3af0c62015-11-16 04:51:21 +0000457static void ak4613_parse_of(struct ak4613_priv *priv,
458 struct device *dev)
459{
460 struct device_node *np = dev->of_node;
461 char prop[32];
462 int i;
463
464 /* Input 1 - 2 */
465 for (i = 0; i < 2; i++) {
466 snprintf(prop, sizeof(prop), "ak4613,in%d-single-end", i + 1);
467 if (!of_get_property(np, prop, NULL))
468 priv->ic |= 1 << i;
469 }
470
471 /* Output 1 - 6 */
472 for (i = 0; i < 6; i++) {
473 snprintf(prop, sizeof(prop), "ak4613,out%d-single-end", i + 1);
474 if (!of_get_property(np, prop, NULL))
475 priv->oc |= 1 << i;
476 }
477}
478
Kuninori Morimotob0757062015-09-15 08:26:36 +0000479static int ak4613_i2c_probe(struct i2c_client *i2c,
480 const struct i2c_device_id *id)
481{
482 struct device *dev = &i2c->dev;
483 struct device_node *np = dev->of_node;
484 const struct regmap_config *regmap_cfg;
485 struct regmap *regmap;
486 struct ak4613_priv *priv;
487
488 regmap_cfg = NULL;
489 if (np) {
490 const struct of_device_id *of_id;
491
492 of_id = of_match_device(ak4613_of_match, dev);
493 if (of_id)
494 regmap_cfg = of_id->data;
495 } else {
496 regmap_cfg = (const struct regmap_config *)id->driver_data;
497 }
498
499 if (!regmap_cfg)
500 return -EINVAL;
501
502 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
503 if (!priv)
504 return -ENOMEM;
505
Kuninori Morimotoa3af0c62015-11-16 04:51:21 +0000506 ak4613_parse_of(priv, dev);
507
Kuninori Morimoto35299f12015-11-16 06:01:29 +0000508 priv->iface = NULL;
Kuninori Morimotob0757062015-09-15 08:26:36 +0000509 priv->cnt = 0;
510
511 mutex_init(&priv->lock);
512
513 i2c_set_clientdata(i2c, priv);
514
515 regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
516 if (IS_ERR(regmap))
517 return PTR_ERR(regmap);
518
519 return snd_soc_register_codec(dev, &soc_codec_dev_ak4613,
520 &ak4613_dai, 1);
521}
522
523static int ak4613_i2c_remove(struct i2c_client *client)
524{
525 snd_soc_unregister_codec(&client->dev);
526 return 0;
527}
528
529static struct i2c_driver ak4613_i2c_driver = {
530 .driver = {
531 .name = "ak4613-codec",
532 .owner = THIS_MODULE,
533 .of_match_table = ak4613_of_match,
534 },
535 .probe = ak4613_i2c_probe,
536 .remove = ak4613_i2c_remove,
537 .id_table = ak4613_i2c_id,
538};
539
540module_i2c_driver(ak4613_i2c_driver);
541
542MODULE_DESCRIPTION("Soc AK4613 driver");
543MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
544MODULE_LICENSE("GPL v2");