xref: /linux/sound/soc/codecs/ak4613.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ak4613.c  --  Asahi Kasei ALSA Soc Audio driver
4 //
5 // Copyright (C) 2015 Renesas Electronics Corporation
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on ak4642.c by Kuninori Morimoto
9 // Based on wm8731.c by Richard Purdie
10 // Based on ak4535.c by Richard Purdie
11 // Based on wm8753.c by Liam Girdwood
12 
13 /*
14  *		+-------+
15  *		|AK4613	|
16  *	SDTO1 <-|	|
17  *		|	|
18  *	SDTI1 ->|	|
19  *	SDTI2 ->|	|
20  *	SDTI3 ->|	|
21  *		+-------+
22  *
23  *	  +---+
24  * clk	  |   |___________________________________________...
25  *
26  * [TDM512]
27  * SDTO1  [L1][R1][L2][R2]
28  * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4][L5][R5][L6][R6]
29  *
30  * [TDM256]
31  * SDTO1  [L1][R1][L2][R2]
32  * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4]
33  * SDTI2  [L5][R5][L6][R6]
34  *
35  * [TDM128]
36  * SDTO1  [L1][R1][L2][R2]
37  * SDTI1  [L1][R1][L2][R2]
38  * SDTI2  [L3][R3][L4][R4]
39  * SDTI3  [L5][R5][L6][R6]
40  *
41  * [STEREO]
42  *	Playback  2ch : SDTI1
43  *	Capture   2ch : SDTO1
44  *
45  * [TDM512]
46  *	Playback 12ch : SDTI1
47  *	Capture   4ch : SDTO1
48  *
49  * [TDM256]
50  *	Playback 12ch : SDTI1 + SDTI2
51  *	Playback  8ch : SDTI1
52  *	Capture   4ch : SDTO1
53  *
54  * [TDM128]
55  *	Playback 12ch : SDTI1 + SDTI2 + SDTI3
56  *	Playback  8ch : SDTI1 + SDTI2
57  *	Playback  4ch : SDTI1
58  *	Capture   4ch : SDTO1
59  *
60  *
61  * !!! NOTE !!!
62  *
63  * Renesas is the only user of ak4613 on upstream so far,
64  * but the chip connection is like below.
65  * Thus, Renesas can't test all connection case.
66  * Tested TDM is very limited.
67  *
68  * +-----+	+-----------+
69  * | SoC |	|  AK4613   |
70  * |     |<-----|SDTO1	 IN1|<-- Mic
71  * |     |	|	 IN2|
72  * |     |	|	    |
73  * |     |----->|SDTI1	OUT1|--> Headphone
74  * +-----+	|SDTI2	OUT2|
75  *		|SDTI3	OUT3|
76  *		|	OUT4|
77  *		|	OUT5|
78  *		|	OUT6|
79  *		+-----------+
80  *
81  * Renesas SoC can handle [2,  6,8]    channels.
82  * Ak4613      can handle [2,4,  8,12] channels.
83  *
84  * Because of above HW connection and available channels number,
85  * Renesas could test are ...
86  *
87  *	[STEREO] Playback  2ch : SDTI1
88  *		 Capture   2ch : SDTO1
89  *	[TDM256] Playback  8ch : SDTI1 (*)
90  *
91  * (*) it used 8ch data between SoC <-> AK4613 on TDM256 mode,
92  *     but could confirm is only first 2ch because only 1
93  *     Headphone is connected.
94  *
95  * see
96  *	AK4613_ENABLE_TDM_TEST
97  */
98 #include <linux/cleanup.h>
99 #include <linux/clk.h>
100 #include <linux/delay.h>
101 #include <linux/i2c.h>
102 #include <linux/slab.h>
103 #include <linux/of.h>
104 #include <linux/of_graph.h>
105 #include <linux/module.h>
106 #include <linux/regmap.h>
107 #include <sound/soc.h>
108 #include <sound/pcm_params.h>
109 #include <sound/tlv.h>
110 
111 #define PW_MGMT1	0x00 /* Power Management 1 */
112 #define PW_MGMT2	0x01 /* Power Management 2 */
113 #define PW_MGMT3	0x02 /* Power Management 3 */
114 #define CTRL1		0x03 /* Control 1 */
115 #define CTRL2		0x04 /* Control 2 */
116 #define DEMP1		0x05 /* De-emphasis1 */
117 #define DEMP2		0x06 /* De-emphasis2 */
118 #define OFD		0x07 /* Overflow Detect */
119 #define ZRD		0x08 /* Zero Detect */
120 #define ICTRL		0x09 /* Input Control */
121 #define OCTRL		0x0a /* Output Control */
122 #define LOUT1		0x0b /* LOUT1 Volume Control */
123 #define ROUT1		0x0c /* ROUT1 Volume Control */
124 #define LOUT2		0x0d /* LOUT2 Volume Control */
125 #define ROUT2		0x0e /* ROUT2 Volume Control */
126 #define LOUT3		0x0f /* LOUT3 Volume Control */
127 #define ROUT3		0x10 /* ROUT3 Volume Control */
128 #define LOUT4		0x11 /* LOUT4 Volume Control */
129 #define ROUT4		0x12 /* ROUT4 Volume Control */
130 #define LOUT5		0x13 /* LOUT5 Volume Control */
131 #define ROUT5		0x14 /* ROUT5 Volume Control */
132 #define LOUT6		0x15 /* LOUT6 Volume Control */
133 #define ROUT6		0x16 /* ROUT6 Volume Control */
134 
135 /* PW_MGMT1 */
136 #define RSTN		BIT(0)
137 #define PMDAC		BIT(1)
138 #define PMADC		BIT(2)
139 #define PMVR		BIT(3)
140 
141 /* PW_MGMT2 */
142 #define PMAD_ALL	0x7
143 
144 /* PW_MGMT3 */
145 #define PMDA_ALL	0x3f
146 
147 /* CTRL1 */
148 #define DIF0		BIT(3)
149 #define DIF1		BIT(4)
150 #define DIF2		BIT(5)
151 #define TDM0		BIT(6)
152 #define TDM1		BIT(7)
153 #define NO_FMT		(0xff)
154 #define FMT_MASK	(0xf8)
155 
156 /* CTRL2 */
157 #define DFS_MASK		(3 << 2)
158 #define DFS_NORMAL_SPEED	(0 << 2)
159 #define DFS_DOUBLE_SPEED	(1 << 2)
160 #define DFS_QUAD_SPEED		(2 << 2)
161 
162 /* ICTRL */
163 #define ICTRL_MASK	(0x3)
164 
165 /* OCTRL */
166 #define OCTRL_MASK	(0x3F)
167 
168 /*
169  * configs
170  *
171  * 0x000000BA
172  *
173  * B : AK4613_CONFIG_SDTI_x
174  * A : AK4613_CONFIG_MODE_x
175  */
176 #define AK4613_CONFIG_SET(priv, x)	 priv->configs |= AK4613_CONFIG_##x
177 #define AK4613_CONFIG_GET(priv, x)	(priv->configs &  AK4613_CONFIG_##x##_MASK)
178 
179 /*
180  * AK4613_CONFIG_SDTI_x
181  *
182  * It indicates how many SDTIx is connected.
183  */
184 #define AK4613_CONFIG_SDTI_MASK		(0xF << 4)
185 #define AK4613_CONFIG_SDTI(x)		(((x) & 0xF) << 4)
186 #define AK4613_CONFIG_SDTI_set(priv, x)	  AK4613_CONFIG_SET(priv, SDTI(x))
187 #define AK4613_CONFIG_SDTI_get(priv)	((AK4613_CONFIG_GET(priv, SDTI) >> 4) & 0xF)
188 
189 /*
190  * AK4613_CONFIG_MODE_x
191  *
192  * Same as Ctrl1 :: TDM1/TDM0
193  * No shift is requested
194  * see
195  *	AK4613_CTRL1_TO_MODE()
196  *	Table 11/12/13/14
197  */
198 #define AK4613_CONFIG_MODE_MASK		(0xF)
199 #define AK4613_CONFIG_MODE_STEREO	(0x0)
200 #define AK4613_CONFIG_MODE_TDM512	(0x1)
201 #define AK4613_CONFIG_MODE_TDM256	(0x2)
202 #define AK4613_CONFIG_MODE_TDM128	(0x3)
203 
204 /*
205  * !!!! FIXME !!!!
206  *
207  * Because of testable HW limitation, TDM256 8ch TDM was only tested.
208  * This driver uses AK4613_ENABLE_TDM_TEST instead of new DT property so far.
209  * Don't hesitate to update driver, you don't need to care compatible
210  * with Renesas.
211  *
212  * #define AK4613_ENABLE_TDM_TEST
213  */
214 
215 struct ak4613_interface {
216 	unsigned int width;
217 	unsigned int fmt;
218 	u8 dif;
219 };
220 
221 struct ak4613_priv {
222 	struct mutex lock;
223 	struct snd_pcm_hw_constraint_list constraint_rates;
224 	struct snd_pcm_hw_constraint_list constraint_channels;
225 	struct work_struct dummy_write_work;
226 	struct snd_soc_component *component;
227 	unsigned int rate;
228 	unsigned int sysclk;
229 
230 	unsigned int fmt;
231 	unsigned int configs;
232 	int cnt;
233 	u8 ctrl1;
234 	u8 oc;
235 	u8 ic;
236 };
237 
238 /*
239  * Playback Volume
240  *
241  * max : 0x00 : 0 dB
242  *       ( 0.5 dB step )
243  * min : 0xFE : -127.0 dB
244  * mute: 0xFF
245  */
246 static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
247 
248 static const struct snd_kcontrol_new ak4613_snd_controls[] = {
249 	SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
250 			 0, 0xFF, 1, out_tlv),
251 	SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
252 			 0, 0xFF, 1, out_tlv),
253 	SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
254 			 0, 0xFF, 1, out_tlv),
255 	SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
256 			 0, 0xFF, 1, out_tlv),
257 	SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
258 			 0, 0xFF, 1, out_tlv),
259 	SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
260 			 0, 0xFF, 1, out_tlv),
261 };
262 
263 static const struct reg_default ak4613_reg[] = {
264 	{ 0x0,  0x0f }, { 0x1,  0x07 }, { 0x2,  0x3f }, { 0x3,  0x20 },
265 	{ 0x4,  0x20 }, { 0x5,  0x55 }, { 0x6,  0x05 }, { 0x7,  0x07 },
266 	{ 0x8,  0x0f }, { 0x9,  0x07 }, { 0xa,  0x3f }, { 0xb,  0x00 },
267 	{ 0xc,  0x00 }, { 0xd,  0x00 }, { 0xe,  0x00 }, { 0xf,  0x00 },
268 	{ 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
269 	{ 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
270 };
271 
272 /*
273  * CTRL1 register
274  * see
275  *	Table 11/12/13/14
276  */
277 #define AUDIO_IFACE(_dif, _width, _fmt)		\
278 	{					\
279 		.dif	= _dif,			\
280 		.width	= _width,		\
281 		.fmt	= SND_SOC_DAIFMT_##_fmt,\
282 	}
283 static const struct ak4613_interface ak4613_iface[] = {
284 	/* It doesn't support asymmetric format */
285 
286 	AUDIO_IFACE(0x03, 24, LEFT_J),
287 	AUDIO_IFACE(0x04, 24, I2S),
288 };
289 #define AK4613_CTRL1_TO_MODE(priv)	((priv)->ctrl1 >> 6) /* AK4613_CONFIG_MODE_x */
290 
291 static const struct regmap_config ak4613_regmap_cfg = {
292 	.reg_bits		= 8,
293 	.val_bits		= 8,
294 	.max_register		= 0x16,
295 	.reg_defaults		= ak4613_reg,
296 	.num_reg_defaults	= ARRAY_SIZE(ak4613_reg),
297 	.cache_type		= REGCACHE_RBTREE,
298 };
299 
300 static const struct of_device_id ak4613_of_match[] = {
301 	{ .compatible = "asahi-kasei,ak4613",	.data = &ak4613_regmap_cfg },
302 	{},
303 };
304 MODULE_DEVICE_TABLE(of, ak4613_of_match);
305 
306 static const struct i2c_device_id ak4613_i2c_id[] = {
307 	{ .name = "ak4613", .driver_data = (kernel_ulong_t)&ak4613_regmap_cfg },
308 	{ }
309 };
310 MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
311 
312 static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
313 
314 	/* Outputs */
315 	SND_SOC_DAPM_OUTPUT("LOUT1"),
316 	SND_SOC_DAPM_OUTPUT("LOUT2"),
317 	SND_SOC_DAPM_OUTPUT("LOUT3"),
318 	SND_SOC_DAPM_OUTPUT("LOUT4"),
319 	SND_SOC_DAPM_OUTPUT("LOUT5"),
320 	SND_SOC_DAPM_OUTPUT("LOUT6"),
321 
322 	SND_SOC_DAPM_OUTPUT("ROUT1"),
323 	SND_SOC_DAPM_OUTPUT("ROUT2"),
324 	SND_SOC_DAPM_OUTPUT("ROUT3"),
325 	SND_SOC_DAPM_OUTPUT("ROUT4"),
326 	SND_SOC_DAPM_OUTPUT("ROUT5"),
327 	SND_SOC_DAPM_OUTPUT("ROUT6"),
328 
329 	/* Inputs */
330 	SND_SOC_DAPM_INPUT("LIN1"),
331 	SND_SOC_DAPM_INPUT("LIN2"),
332 
333 	SND_SOC_DAPM_INPUT("RIN1"),
334 	SND_SOC_DAPM_INPUT("RIN2"),
335 
336 	/* DAC */
337 	SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
338 	SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
339 	SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
340 	SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
341 	SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
342 	SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
343 
344 	/* ADC */
345 	SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
346 	SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
347 };
348 
349 static const struct snd_soc_dapm_route ak4613_intercon[] = {
350 	{"LOUT1", NULL, "DAC1"},
351 	{"LOUT2", NULL, "DAC2"},
352 	{"LOUT3", NULL, "DAC3"},
353 	{"LOUT4", NULL, "DAC4"},
354 	{"LOUT5", NULL, "DAC5"},
355 	{"LOUT6", NULL, "DAC6"},
356 
357 	{"ROUT1", NULL, "DAC1"},
358 	{"ROUT2", NULL, "DAC2"},
359 	{"ROUT3", NULL, "DAC3"},
360 	{"ROUT4", NULL, "DAC4"},
361 	{"ROUT5", NULL, "DAC5"},
362 	{"ROUT6", NULL, "DAC6"},
363 
364 	{"DAC1", NULL, "Playback"},
365 	{"DAC2", NULL, "Playback"},
366 	{"DAC3", NULL, "Playback"},
367 	{"DAC4", NULL, "Playback"},
368 	{"DAC5", NULL, "Playback"},
369 	{"DAC6", NULL, "Playback"},
370 
371 	{"Capture", NULL, "ADC1"},
372 	{"Capture", NULL, "ADC2"},
373 
374 	{"ADC1", NULL, "LIN1"},
375 	{"ADC2", NULL, "LIN2"},
376 
377 	{"ADC1", NULL, "RIN1"},
378 	{"ADC2", NULL, "RIN2"},
379 };
380 
381 static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
382 			       struct snd_soc_dai *dai)
383 {
384 	struct snd_soc_component *component = dai->component;
385 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
386 	struct device *dev = component->dev;
387 
388 	guard(mutex)(&priv->lock);
389 	priv->cnt--;
390 	if (priv->cnt < 0) {
391 		dev_err(dev, "unexpected counter error\n");
392 		priv->cnt = 0;
393 	}
394 	if (!priv->cnt)
395 		priv->ctrl1 = 0;
396 }
397 
398 static void ak4613_hw_constraints(struct ak4613_priv *priv,
399 				  struct snd_pcm_substream *substream)
400 {
401 	struct snd_pcm_runtime *runtime = substream->runtime;
402 	static const unsigned int ak4613_rates[] = {
403 		 32000,
404 		 44100,
405 		 48000,
406 		 64000,
407 		 88200,
408 		 96000,
409 		176400,
410 		192000,
411 	};
412 #define AK4613_CHANNEL_2	 0
413 #define AK4613_CHANNEL_4	 1
414 #define AK4613_CHANNEL_8	 2
415 #define AK4613_CHANNEL_12	 3
416 #define AK4613_CHANNEL_NONE	-1
417 	static const unsigned int ak4613_channels[] = {
418 		[AK4613_CHANNEL_2]  =  2,
419 		[AK4613_CHANNEL_4]  =  4,
420 		[AK4613_CHANNEL_8]  =  8,
421 		[AK4613_CHANNEL_12] = 12,
422 	};
423 #define MODE_MAX 4
424 #define SDTx_MAX 4
425 #define MASK(x) (1 << AK4613_CHANNEL_##x)
426 	static const int mask_list[MODE_MAX][SDTx_MAX] = {
427 		/* 				SDTO	 SDTIx1    SDTIx2		SDTIx3 */
428 		[AK4613_CONFIG_MODE_STEREO] = { MASK(2), MASK(2),  MASK(2),		MASK(2)},
429 		[AK4613_CONFIG_MODE_TDM512] = { MASK(4), MASK(12), MASK(12),		MASK(12)},
430 		[AK4613_CONFIG_MODE_TDM256] = { MASK(4), MASK(8),  MASK(8)|MASK(12),	MASK(8)|MASK(12)},
431 		[AK4613_CONFIG_MODE_TDM128] = { MASK(4), MASK(4),  MASK(4)|MASK(8),	MASK(4)|MASK(8)|MASK(12)},
432 	};
433 	struct snd_pcm_hw_constraint_list *constraint;
434 	unsigned int mask;
435 	unsigned int mode;
436 	unsigned int fs;
437 	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
438 	int sdti_num;
439 	int i;
440 
441 	constraint		= &priv->constraint_rates;
442 	constraint->list	= ak4613_rates;
443 	constraint->mask	= 0;
444 	constraint->count	= 0;
445 
446 	/*
447 	 * Slave Mode
448 	 *	Normal: [32kHz, 48kHz] : 256fs,384fs or 512fs
449 	 *	Double: [64kHz, 96kHz] : 256fs
450 	 *	Quad  : [128kHz,192kHz]: 128fs
451 	 *
452 	 * Master mode
453 	 *	Normal: [32kHz, 48kHz] : 256fs or 512fs
454 	 *	Double: [64kHz, 96kHz] : 256fs
455 	 *	Quad  : [128kHz,192kHz]: 128fs
456 	*/
457 	for (i = 0; i < ARRAY_SIZE(ak4613_rates); i++) {
458 		/* minimum fs on each range */
459 		fs = (ak4613_rates[i] <= 96000) ? 256 : 128;
460 
461 		if (priv->sysclk >= ak4613_rates[i] * fs)
462 			constraint->count = i + 1;
463 	}
464 
465 	snd_pcm_hw_constraint_list(runtime, 0,
466 				SNDRV_PCM_HW_PARAM_RATE, constraint);
467 
468 
469 	sdti_num = AK4613_CONFIG_SDTI_get(priv);
470 	if (WARN_ON(sdti_num >= SDTx_MAX))
471 		return;
472 
473 	if (priv->cnt) {
474 		/*
475 		 * If it was already working,
476 		 * the constraint is same as working mode.
477 		 */
478 		mode = AK4613_CTRL1_TO_MODE(priv);
479 		mask = 0; /* no default */
480 	} else {
481 		/*
482 		 * It is not yet working,
483 		 * the constraint is based on board configs.
484 		 * STEREO mask is default
485 		 */
486 		mode = AK4613_CONFIG_GET(priv, MODE);
487 		mask = mask_list[AK4613_CONFIG_MODE_STEREO][is_play * sdti_num];
488 	}
489 
490 	if (WARN_ON(mode >= MODE_MAX))
491 		return;
492 
493 	/* add each mode mask */
494 	mask |= mask_list[mode][is_play * sdti_num];
495 
496 	constraint		= &priv->constraint_channels;
497 	constraint->list	= ak4613_channels;
498 	constraint->mask	= mask;
499 	constraint->count	= sizeof(ak4613_channels);
500 	snd_pcm_hw_constraint_list(runtime, 0,
501 				   SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
502 }
503 
504 static int ak4613_dai_startup(struct snd_pcm_substream *substream,
505 			      struct snd_soc_dai *dai)
506 {
507 	struct snd_soc_component *component = dai->component;
508 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
509 
510 	guard(mutex)(&priv->lock);
511 	ak4613_hw_constraints(priv, substream);
512 	priv->cnt++;
513 
514 	return 0;
515 }
516 
517 static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
518 				 int clk_id, unsigned int freq, int dir)
519 {
520 	struct snd_soc_component *component = codec_dai->component;
521 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
522 
523 	priv->sysclk = freq;
524 
525 	return 0;
526 }
527 
528 static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int format)
529 {
530 	struct snd_soc_component *component = dai->component;
531 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
532 	unsigned int fmt;
533 
534 	fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
535 	switch (fmt) {
536 	case SND_SOC_DAIFMT_LEFT_J:
537 	case SND_SOC_DAIFMT_I2S:
538 		priv->fmt = fmt;
539 		break;
540 	default:
541 		return -EINVAL;
542 	}
543 
544 	fmt = format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
545 	switch (fmt) {
546 	case SND_SOC_DAIFMT_CBC_CFC:
547 		break;
548 	default:
549 		/*
550 		 * SUPPORTME
551 		 *
552 		 * "clock provider" is not yet supperted
553 		 */
554 		return -EINVAL;
555 	}
556 
557 	return 0;
558 }
559 
560 static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
561 				struct snd_pcm_hw_params *params,
562 				struct snd_soc_dai *dai)
563 {
564 	struct snd_soc_component *component = dai->component;
565 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
566 	struct device *dev = component->dev;
567 	unsigned int width = params_width(params);
568 	unsigned int fmt = priv->fmt;
569 	unsigned int rate;
570 	int i, ret;
571 	u8 ctrl2;
572 
573 	rate = params_rate(params);
574 	switch (rate) {
575 	case 32000:
576 	case 44100:
577 	case 48000:
578 		ctrl2 = DFS_NORMAL_SPEED;
579 		break;
580 	case 64000:
581 	case 88200:
582 	case 96000:
583 		ctrl2 = DFS_DOUBLE_SPEED;
584 		break;
585 	case 176400:
586 	case 192000:
587 		ctrl2 = DFS_QUAD_SPEED;
588 		break;
589 	default:
590 		return -EINVAL;
591 	}
592 	priv->rate = rate;
593 
594 	/*
595 	 * FIXME
596 	 *
597 	 * It doesn't have full TDM suppert yet
598 	 */
599 	ret = -EINVAL;
600 
601 	scoped_guard(mutex, &priv->lock) {
602 		if (priv->cnt > 1) {
603 			/*
604 			 * If it was already working, use current priv->ctrl1
605 			 */
606 			ret = 0;
607 		} else {
608 			/*
609 			 * It is not yet working,
610 			 */
611 			unsigned int channel = params_channels(params);
612 			u8 tdm;
613 
614 			/* STEREO or TDM */
615 			if (channel == 2)
616 				tdm = AK4613_CONFIG_MODE_STEREO;
617 			else
618 				tdm = AK4613_CONFIG_GET(priv, MODE);
619 
620 			for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
621 				const struct ak4613_interface *iface = ak4613_iface + i;
622 
623 				if (iface->fmt == fmt && iface->width == width) {
624 					/*
625 					 * Ctrl1
626 					 * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0  |
627 					 * |TDM1|TDM0|DIF2|DIF1|DIF0|ATS1|ATS0|SMUTE|
628 					 *  <  tdm  > < iface->dif >
629 					 */
630 					priv->ctrl1 = (tdm << 6) | (iface->dif << 3);
631 					ret = 0;
632 					break;
633 				}
634 			}
635 		}
636 	}
637 
638 	if (ret < 0) {
639 		dev_warn(dev, "unsupported data width/format combination\n");
640 		return ret;
641 	}
642 
643 	snd_soc_component_update_bits(component, CTRL1, FMT_MASK, priv->ctrl1);
644 	snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);
645 
646 	snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
647 	snd_soc_component_update_bits(component, OCTRL, OCTRL_MASK, priv->oc);
648 
649 	return ret;
650 }
651 
652 static int ak4613_set_bias_level(struct snd_soc_component *component,
653 				 enum snd_soc_bias_level level)
654 {
655 	u8 mgmt1 = 0;
656 
657 	switch (level) {
658 	case SND_SOC_BIAS_ON:
659 		mgmt1 |= RSTN;
660 		fallthrough;
661 	case SND_SOC_BIAS_PREPARE:
662 		mgmt1 |= PMADC | PMDAC;
663 		fallthrough;
664 	case SND_SOC_BIAS_STANDBY:
665 		mgmt1 |= PMVR;
666 		fallthrough;
667 	case SND_SOC_BIAS_OFF:
668 	default:
669 		break;
670 	}
671 
672 	snd_soc_component_write(component, PW_MGMT1, mgmt1);
673 
674 	return 0;
675 }
676 
677 static void ak4613_dummy_write(struct work_struct *work)
678 {
679 	struct ak4613_priv *priv = container_of(work,
680 						struct ak4613_priv,
681 						dummy_write_work);
682 	struct snd_soc_component *component = priv->component;
683 	unsigned int mgmt1;
684 	unsigned int mgmt3;
685 
686 	/*
687 	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
688 	 *
689 	 * Note
690 	 *
691 	 * To avoid extra delay, we want to avoid preemption here,
692 	 * but we can't. Because it uses I2C access which is using IRQ
693 	 * and sleep. Thus, delay might be more than 5 LR clocks
694 	 * see also
695 	 *	ak4613_dai_trigger()
696 	 */
697 	udelay(5000000 / priv->rate);
698 
699 	mgmt1 = snd_soc_component_read(component, PW_MGMT1);
700 	mgmt3 = snd_soc_component_read(component, PW_MGMT3);
701 
702 	snd_soc_component_write(component, PW_MGMT1, mgmt1);
703 	snd_soc_component_write(component, PW_MGMT3, mgmt3);
704 }
705 
706 static int ak4613_dai_trigger(struct snd_pcm_substream *substream, int cmd,
707 			      struct snd_soc_dai *dai)
708 {
709 	struct snd_soc_component *component = dai->component;
710 	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
711 
712 	/*
713 	 * FIXME
714 	 *
715 	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
716 	 * from Power Down Release. Otherwise, Playback volume will be 0dB.
717 	 * To avoid complex multiple delay/dummy_write method from
718 	 * ak4613_set_bias_level() / SND_SOC_DAPM_DAC_E("DACx", ...),
719 	 * call it once here.
720 	 *
721 	 * But, unfortunately, we can't "write" here because here is atomic
722 	 * context (It uses I2C access for writing).
723 	 * Thus, use schedule_work() to switching to normal context
724 	 * immediately.
725 	 *
726 	 * Note
727 	 *
728 	 * Calling ak4613_dummy_write() function might be delayed.
729 	 * In such case, ak4613 volume might be temporarily 0dB when
730 	 * beggining of playback.
731 	 * see also
732 	 *	ak4613_dummy_write()
733 	 */
734 
735 	if ((cmd != SNDRV_PCM_TRIGGER_START) &&
736 	    (cmd != SNDRV_PCM_TRIGGER_RESUME))
737 		return 0;
738 
739 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
740 		return  0;
741 
742 	priv->component = component;
743 	schedule_work(&priv->dummy_write_work);
744 
745 	return 0;
746 }
747 
748 static const u64 ak4613_dai_formats =
749 	SND_SOC_POSSIBLE_DAIFMT_I2S	|
750 	SND_SOC_POSSIBLE_DAIFMT_LEFT_J;
751 
752 static const struct snd_soc_dai_ops ak4613_dai_ops = {
753 	.startup	= ak4613_dai_startup,
754 	.shutdown	= ak4613_dai_shutdown,
755 	.set_sysclk	= ak4613_dai_set_sysclk,
756 	.set_fmt	= ak4613_dai_set_fmt,
757 	.trigger	= ak4613_dai_trigger,
758 	.hw_params	= ak4613_dai_hw_params,
759 	.auto_selectable_formats	= &ak4613_dai_formats,
760 	.num_auto_selectable_formats	= 1,
761 };
762 
763 #define AK4613_PCM_RATE		(SNDRV_PCM_RATE_32000  |\
764 				 SNDRV_PCM_RATE_44100  |\
765 				 SNDRV_PCM_RATE_48000  |\
766 				 SNDRV_PCM_RATE_64000  |\
767 				 SNDRV_PCM_RATE_88200  |\
768 				 SNDRV_PCM_RATE_96000  |\
769 				 SNDRV_PCM_RATE_176400 |\
770 				 SNDRV_PCM_RATE_192000)
771 #define AK4613_PCM_FMTBIT	(SNDRV_PCM_FMTBIT_S24_LE)
772 
773 static struct snd_soc_dai_driver ak4613_dai = {
774 	.name = "ak4613-hifi",
775 	.playback = {
776 		.stream_name	= "Playback",
777 		.channels_min	= 2,
778 		.channels_max	= 12,
779 		.rates		= AK4613_PCM_RATE,
780 		.formats	= AK4613_PCM_FMTBIT,
781 	},
782 	.capture = {
783 		.stream_name	= "Capture",
784 		.channels_min	= 2,
785 		.channels_max	= 4,
786 		.rates		= AK4613_PCM_RATE,
787 		.formats	= AK4613_PCM_FMTBIT,
788 	},
789 	.ops = &ak4613_dai_ops,
790 	.symmetric_rate = 1,
791 };
792 
793 static int ak4613_suspend(struct snd_soc_component *component)
794 {
795 	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
796 
797 	regcache_cache_only(regmap, true);
798 	regcache_mark_dirty(regmap);
799 	return 0;
800 }
801 
802 static int ak4613_resume(struct snd_soc_component *component)
803 {
804 	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
805 
806 	regcache_cache_only(regmap, false);
807 	return regcache_sync(regmap);
808 }
809 
810 static const struct snd_soc_component_driver soc_component_dev_ak4613 = {
811 	.suspend		= ak4613_suspend,
812 	.resume			= ak4613_resume,
813 	.set_bias_level		= ak4613_set_bias_level,
814 	.controls		= ak4613_snd_controls,
815 	.num_controls		= ARRAY_SIZE(ak4613_snd_controls),
816 	.dapm_widgets		= ak4613_dapm_widgets,
817 	.num_dapm_widgets	= ARRAY_SIZE(ak4613_dapm_widgets),
818 	.dapm_routes		= ak4613_intercon,
819 	.num_dapm_routes	= ARRAY_SIZE(ak4613_intercon),
820 	.idle_bias_on		= 1,
821 	.endianness		= 1,
822 };
823 
824 static void ak4613_parse_of(struct ak4613_priv *priv,
825 			    struct device *dev)
826 {
827 	struct device_node *np = dev->of_node;
828 	char prop[32];
829 	int sdti_num;
830 	int i;
831 
832 	/* Input 1 - 2 */
833 	for (i = 0; i < 2; i++) {
834 		snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
835 		if (!of_property_read_bool(np, prop))
836 			priv->ic |= 1 << i;
837 	}
838 
839 	/* Output 1 - 6 */
840 	for (i = 0; i < 6; i++) {
841 		snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
842 		if (!of_property_read_bool(np, prop))
843 			priv->oc |= 1 << i;
844 	}
845 
846 	/*
847 	 * enable TDM256 test
848 	 *
849 	 * !!! FIXME !!!
850 	 *
851 	 * It should be configured by DT or other way
852 	 * if it was full supported.
853 	 * But it is using ifdef style for now for test
854 	 * purpose.
855 	 */
856 #if defined(AK4613_ENABLE_TDM_TEST)
857 	AK4613_CONFIG_SET(priv, MODE_TDM256);
858 #endif
859 
860 	/*
861 	 * connected STDI
862 	 * TDM support is assuming it is probed via Audio-Graph-Card style here.
863 	 * Default is SDTIx1 if it was probed via Simple-Audio-Card for now.
864 	 */
865 	sdti_num = of_graph_get_endpoint_count(np);
866 	if ((sdti_num >= SDTx_MAX) || (sdti_num < 1))
867 		sdti_num = 1;
868 
869 	AK4613_CONFIG_SDTI_set(priv, sdti_num);
870 }
871 
872 static int ak4613_i2c_probe(struct i2c_client *i2c)
873 {
874 	struct device *dev = &i2c->dev;
875 	const struct regmap_config *regmap_cfg;
876 	struct regmap *regmap;
877 	struct ak4613_priv *priv;
878 
879 	regmap_cfg = i2c_get_match_data(i2c);
880 	if (!regmap_cfg)
881 		return -EINVAL;
882 
883 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
884 	if (!priv)
885 		return -ENOMEM;
886 
887 	ak4613_parse_of(priv, dev);
888 
889 	priv->ctrl1		= 0;
890 	priv->cnt		= 0;
891 	priv->sysclk		= 0;
892 	INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);
893 
894 	mutex_init(&priv->lock);
895 
896 	i2c_set_clientdata(i2c, priv);
897 
898 	regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
899 	if (IS_ERR(regmap))
900 		return PTR_ERR(regmap);
901 
902 	return devm_snd_soc_register_component(dev, &soc_component_dev_ak4613,
903 				      &ak4613_dai, 1);
904 }
905 
906 static struct i2c_driver ak4613_i2c_driver = {
907 	.driver = {
908 		.name = "ak4613-codec",
909 		.of_match_table = ak4613_of_match,
910 	},
911 	.probe		= ak4613_i2c_probe,
912 	.id_table	= ak4613_i2c_id,
913 };
914 
915 module_i2c_driver(ak4613_i2c_driver);
916 
917 MODULE_DESCRIPTION("Soc AK4613 driver");
918 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
919 MODULE_LICENSE("GPL v2");
920