GNU Linux-libre 4.4.284-gnu1
[releases.git] / sound / soc / codecs / ak4613.c
1 /*
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>
25 #include <sound/tlv.h>
26
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
77 struct ak4613_priv {
78         struct mutex lock;
79
80         unsigned int fmt;
81         u8 fmt_ctrl;
82         int cnt;
83 };
84
85 struct ak4613_formats {
86         unsigned int width;
87         unsigned int fmt;
88 };
89
90 struct ak4613_interface {
91         struct ak4613_formats capture;
92         struct ak4613_formats playback;
93 };
94
95 /*
96  * Playback Volume
97  *
98  * max : 0x00 : 0 dB
99  *       ( 0.5 dB step )
100  * min : 0xFE : -127.0 dB
101  * mute: 0xFF
102  */
103 static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
104
105 static const struct snd_kcontrol_new ak4613_snd_controls[] = {
106         SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
107                          0, 0xFF, 1, out_tlv),
108         SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
109                          0, 0xFF, 1, out_tlv),
110         SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
111                          0, 0xFF, 1, out_tlv),
112         SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
113                          0, 0xFF, 1, out_tlv),
114         SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
115                          0, 0xFF, 1, out_tlv),
116         SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
117                          0, 0xFF, 1, out_tlv),
118 };
119
120 static const struct reg_default ak4613_reg[] = {
121         { 0x0,  0x0f }, { 0x1,  0x07 }, { 0x2,  0x3f }, { 0x3,  0x20 },
122         { 0x4,  0x20 }, { 0x5,  0x55 }, { 0x6,  0x05 }, { 0x7,  0x07 },
123         { 0x8,  0x0f }, { 0x9,  0x07 }, { 0xa,  0x3f }, { 0xb,  0x00 },
124         { 0xc,  0x00 }, { 0xd,  0x00 }, { 0xe,  0x00 }, { 0xf,  0x00 },
125         { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
126         { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
127 };
128
129 #define AUDIO_IFACE_IDX_TO_VAL(i) (i << 3)
130 #define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
131 static const struct ak4613_interface ak4613_iface[] = {
132         /* capture */                           /* playback */
133         [0] = { AUDIO_IFACE(24, LEFT_J),        AUDIO_IFACE(16, RIGHT_J) },
134         [1] = { AUDIO_IFACE(24, LEFT_J),        AUDIO_IFACE(20, RIGHT_J) },
135         [2] = { AUDIO_IFACE(24, LEFT_J),        AUDIO_IFACE(24, RIGHT_J) },
136         [3] = { AUDIO_IFACE(24, LEFT_J),        AUDIO_IFACE(24, LEFT_J) },
137         [4] = { AUDIO_IFACE(24, I2S),           AUDIO_IFACE(24, I2S) },
138 };
139
140 static const struct regmap_config ak4613_regmap_cfg = {
141         .reg_bits               = 8,
142         .val_bits               = 8,
143         .max_register           = 0x16,
144         .reg_defaults           = ak4613_reg,
145         .num_reg_defaults       = ARRAY_SIZE(ak4613_reg),
146         .cache_type             = REGCACHE_RBTREE,
147 };
148
149 static const struct of_device_id ak4613_of_match[] = {
150         { .compatible = "asahi-kasei,ak4613",   .data = &ak4613_regmap_cfg },
151         {},
152 };
153 MODULE_DEVICE_TABLE(of, ak4613_of_match);
154
155 static const struct i2c_device_id ak4613_i2c_id[] = {
156         { "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
157         { }
158 };
159 MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
160
161 static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
162
163         /* Outputs */
164         SND_SOC_DAPM_OUTPUT("LOUT1"),
165         SND_SOC_DAPM_OUTPUT("LOUT2"),
166         SND_SOC_DAPM_OUTPUT("LOUT3"),
167         SND_SOC_DAPM_OUTPUT("LOUT4"),
168         SND_SOC_DAPM_OUTPUT("LOUT5"),
169         SND_SOC_DAPM_OUTPUT("LOUT6"),
170
171         SND_SOC_DAPM_OUTPUT("ROUT1"),
172         SND_SOC_DAPM_OUTPUT("ROUT2"),
173         SND_SOC_DAPM_OUTPUT("ROUT3"),
174         SND_SOC_DAPM_OUTPUT("ROUT4"),
175         SND_SOC_DAPM_OUTPUT("ROUT5"),
176         SND_SOC_DAPM_OUTPUT("ROUT6"),
177
178         /* Inputs */
179         SND_SOC_DAPM_INPUT("LIN1"),
180         SND_SOC_DAPM_INPUT("LIN2"),
181
182         SND_SOC_DAPM_INPUT("RIN1"),
183         SND_SOC_DAPM_INPUT("RIN2"),
184
185         /* DAC */
186         SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
187         SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
188         SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
189         SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
190         SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
191         SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
192
193         /* ADC */
194         SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
195         SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
196 };
197
198 static const struct snd_soc_dapm_route ak4613_intercon[] = {
199         {"LOUT1", NULL, "DAC1"},
200         {"LOUT2", NULL, "DAC2"},
201         {"LOUT3", NULL, "DAC3"},
202         {"LOUT4", NULL, "DAC4"},
203         {"LOUT5", NULL, "DAC5"},
204         {"LOUT6", NULL, "DAC6"},
205
206         {"ROUT1", NULL, "DAC1"},
207         {"ROUT2", NULL, "DAC2"},
208         {"ROUT3", NULL, "DAC3"},
209         {"ROUT4", NULL, "DAC4"},
210         {"ROUT5", NULL, "DAC5"},
211         {"ROUT6", NULL, "DAC6"},
212
213         {"DAC1", NULL, "Playback"},
214         {"DAC2", NULL, "Playback"},
215         {"DAC3", NULL, "Playback"},
216         {"DAC4", NULL, "Playback"},
217         {"DAC5", NULL, "Playback"},
218         {"DAC6", NULL, "Playback"},
219
220         {"Capture", NULL, "ADC1"},
221         {"Capture", NULL, "ADC2"},
222
223         {"ADC1", NULL, "LIN1"},
224         {"ADC2", NULL, "LIN2"},
225
226         {"ADC1", NULL, "RIN1"},
227         {"ADC2", NULL, "RIN2"},
228 };
229
230 static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
231                                struct snd_soc_dai *dai)
232 {
233         struct snd_soc_codec *codec = dai->codec;
234         struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
235         struct device *dev = codec->dev;
236
237         mutex_lock(&priv->lock);
238         priv->cnt--;
239         if (priv->cnt < 0) {
240                 dev_err(dev, "unexpected counter error\n");
241                 priv->cnt = 0;
242         }
243         if (!priv->cnt)
244                 priv->fmt_ctrl = NO_FMT;
245         mutex_unlock(&priv->lock);
246 }
247
248 static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
249 {
250         struct snd_soc_codec *codec = dai->codec;
251         struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
252
253         fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
254
255         switch (fmt) {
256         case SND_SOC_DAIFMT_RIGHT_J:
257         case SND_SOC_DAIFMT_LEFT_J:
258         case SND_SOC_DAIFMT_I2S:
259                 priv->fmt = fmt;
260
261                 break;
262         default:
263                 return -EINVAL;
264         }
265
266         return 0;
267 }
268
269 static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
270                                 struct snd_pcm_hw_params *params,
271                                 struct snd_soc_dai *dai)
272 {
273         struct snd_soc_codec *codec = dai->codec;
274         struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
275         const struct ak4613_formats *fmts;
276         struct device *dev = codec->dev;
277         unsigned int width = params_width(params);
278         unsigned int fmt = priv->fmt;
279         unsigned int rate;
280         int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
281         int i, ret;
282         u8 fmt_ctrl, ctrl2;
283
284         rate = params_rate(params);
285         switch (rate) {
286         case 32000:
287         case 44100:
288         case 48000:
289                 ctrl2 = DFS_NORMAL_SPEED;
290                 break;
291         case 88200:
292         case 96000:
293                 ctrl2 = DFS_DOUBLE_SPEED;
294                 break;
295         case 176400:
296         case 192000:
297                 ctrl2 = DFS_QUAD_SPEED;
298                 break;
299         default:
300                 return -EINVAL;
301         }
302
303         /*
304          * FIXME
305          *
306          * It doesn't support TDM at this point
307          */
308         fmt_ctrl = NO_FMT;
309         for (i = 0; i < ARRAY_SIZE(ak4613_iface); i++) {
310                 fmts = (is_play) ?      &ak4613_iface[i].playback :
311                                         &ak4613_iface[i].capture;
312
313                 if (fmts->fmt != fmt)
314                         continue;
315
316                 if (fmt == SND_SOC_DAIFMT_RIGHT_J) {
317                         if (fmts->width != width)
318                                 continue;
319                 } else {
320                         if (fmts->width < width)
321                                 continue;
322                 }
323
324                 fmt_ctrl = AUDIO_IFACE_IDX_TO_VAL(i);
325                 break;
326         }
327
328         ret = -EINVAL;
329         if (fmt_ctrl == NO_FMT)
330                 goto hw_params_end;
331
332         mutex_lock(&priv->lock);
333         if ((priv->fmt_ctrl == NO_FMT) ||
334             (priv->fmt_ctrl == fmt_ctrl)) {
335                 priv->fmt_ctrl = fmt_ctrl;
336                 priv->cnt++;
337                 ret = 0;
338         }
339         mutex_unlock(&priv->lock);
340
341         if (ret < 0)
342                 goto hw_params_end;
343
344         snd_soc_update_bits(codec, CTRL1, FMT_MASK, fmt_ctrl);
345         snd_soc_write(codec, CTRL2, ctrl2);
346
347 hw_params_end:
348         if (ret < 0)
349                 dev_warn(dev, "unsupported data width/format combination\n");
350
351         return ret;
352 }
353
354 static int ak4613_set_bias_level(struct snd_soc_codec *codec,
355                                  enum snd_soc_bias_level level)
356 {
357         u8 mgmt1 = 0;
358
359         switch (level) {
360         case SND_SOC_BIAS_ON:
361                 mgmt1 |= RSTN;
362                 /* fall through */
363         case SND_SOC_BIAS_PREPARE:
364                 mgmt1 |= PMADC | PMDAC;
365                 /* fall through */
366         case SND_SOC_BIAS_STANDBY:
367                 mgmt1 |= PMVR;
368                 /* fall through */
369         case SND_SOC_BIAS_OFF:
370         default:
371                 break;
372         }
373
374         snd_soc_write(codec, PW_MGMT1, mgmt1);
375
376         return 0;
377 }
378
379 static const struct snd_soc_dai_ops ak4613_dai_ops = {
380         .shutdown       = ak4613_dai_shutdown,
381         .set_fmt        = ak4613_dai_set_fmt,
382         .hw_params      = ak4613_dai_hw_params,
383 };
384
385 #define AK4613_PCM_RATE         (SNDRV_PCM_RATE_32000  |\
386                                  SNDRV_PCM_RATE_44100  |\
387                                  SNDRV_PCM_RATE_48000  |\
388                                  SNDRV_PCM_RATE_64000  |\
389                                  SNDRV_PCM_RATE_88200  |\
390                                  SNDRV_PCM_RATE_96000  |\
391                                  SNDRV_PCM_RATE_176400 |\
392                                  SNDRV_PCM_RATE_192000)
393 #define AK4613_PCM_FMTBIT       (SNDRV_PCM_FMTBIT_S16_LE |\
394                                  SNDRV_PCM_FMTBIT_S24_LE)
395
396 static struct snd_soc_dai_driver ak4613_dai = {
397         .name = "ak4613-hifi",
398         .playback = {
399                 .stream_name    = "Playback",
400                 .channels_min   = 2,
401                 .channels_max   = 2,
402                 .rates          = AK4613_PCM_RATE,
403                 .formats        = AK4613_PCM_FMTBIT,
404         },
405         .capture = {
406                 .stream_name    = "Capture",
407                 .channels_min   = 2,
408                 .channels_max   = 2,
409                 .rates          = AK4613_PCM_RATE,
410                 .formats        = AK4613_PCM_FMTBIT,
411         },
412         .ops = &ak4613_dai_ops,
413         .symmetric_rates = 1,
414 };
415
416 static int ak4613_resume(struct snd_soc_codec *codec)
417 {
418         struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
419
420         regcache_mark_dirty(regmap);
421         return regcache_sync(regmap);
422 }
423
424 static struct snd_soc_codec_driver soc_codec_dev_ak4613 = {
425         .resume                 = ak4613_resume,
426         .set_bias_level         = ak4613_set_bias_level,
427         .controls               = ak4613_snd_controls,
428         .num_controls           = ARRAY_SIZE(ak4613_snd_controls),
429         .dapm_widgets           = ak4613_dapm_widgets,
430         .num_dapm_widgets       = ARRAY_SIZE(ak4613_dapm_widgets),
431         .dapm_routes            = ak4613_intercon,
432         .num_dapm_routes        = ARRAY_SIZE(ak4613_intercon),
433 };
434
435 static int ak4613_i2c_probe(struct i2c_client *i2c,
436                             const struct i2c_device_id *id)
437 {
438         struct device *dev = &i2c->dev;
439         struct device_node *np = dev->of_node;
440         const struct regmap_config *regmap_cfg;
441         struct regmap *regmap;
442         struct ak4613_priv *priv;
443
444         regmap_cfg = NULL;
445         if (np) {
446                 const struct of_device_id *of_id;
447
448                 of_id = of_match_device(ak4613_of_match, dev);
449                 if (of_id)
450                         regmap_cfg = of_id->data;
451         } else {
452                 regmap_cfg = (const struct regmap_config *)id->driver_data;
453         }
454
455         if (!regmap_cfg)
456                 return -EINVAL;
457
458         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
459         if (!priv)
460                 return -ENOMEM;
461
462         priv->fmt_ctrl          = NO_FMT;
463         priv->cnt               = 0;
464
465         mutex_init(&priv->lock);
466
467         i2c_set_clientdata(i2c, priv);
468
469         regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
470         if (IS_ERR(regmap))
471                 return PTR_ERR(regmap);
472
473         return snd_soc_register_codec(dev, &soc_codec_dev_ak4613,
474                                       &ak4613_dai, 1);
475 }
476
477 static int ak4613_i2c_remove(struct i2c_client *client)
478 {
479         snd_soc_unregister_codec(&client->dev);
480         return 0;
481 }
482
483 static struct i2c_driver ak4613_i2c_driver = {
484         .driver = {
485                 .name = "ak4613-codec",
486                 .owner = THIS_MODULE,
487                 .of_match_table = ak4613_of_match,
488         },
489         .probe          = ak4613_i2c_probe,
490         .remove         = ak4613_i2c_remove,
491         .id_table       = ak4613_i2c_id,
492 };
493
494 module_i2c_driver(ak4613_i2c_driver);
495
496 MODULE_DESCRIPTION("Soc AK4613 driver");
497 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
498 MODULE_LICENSE("GPL v2");