xref: /openbmc/linux/sound/soc/codecs/mt6660.c (revision 249592bf)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 // Copyright (c) 2019 MediaTek Inc.
4 
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/err.h>
8 #include <linux/i2c.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/delay.h>
11 #include <sound/soc.h>
12 #include <sound/tlv.h>
13 #include <sound/pcm_params.h>
14 
15 #include "mt6660.h"
16 
17 struct reg_size_table {
18 	u32 addr;
19 	u8 size;
20 };
21 
22 static const struct reg_size_table mt6660_reg_size_table[] = {
23 	{ MT6660_REG_HPF1_COEF, 4 },
24 	{ MT6660_REG_HPF2_COEF, 4 },
25 	{ MT6660_REG_TDM_CFG3, 2 },
26 	{ MT6660_REG_RESV17, 2 },
27 	{ MT6660_REG_RESV23, 2 },
28 	{ MT6660_REG_SIGMAX, 2 },
29 	{ MT6660_REG_DEVID, 2 },
30 	{ MT6660_REG_HCLIP_CTRL, 2 },
31 	{ MT6660_REG_DA_GAIN, 2 },
32 };
33 
34 static int mt6660_get_reg_size(uint32_t addr)
35 {
36 	int i;
37 
38 	for (i = 0; i < ARRAY_SIZE(mt6660_reg_size_table); i++) {
39 		if (mt6660_reg_size_table[i].addr == addr)
40 			return mt6660_reg_size_table[i].size;
41 	}
42 	return 1;
43 }
44 
45 static int mt6660_reg_write(void *context, unsigned int reg, unsigned int val)
46 {
47 	struct mt6660_chip *chip = context;
48 	int size = mt6660_get_reg_size(reg);
49 	u8 reg_data[4];
50 	int i, ret;
51 
52 	for (i = 0; i < size; i++)
53 		reg_data[size - i - 1] = (val >> (8 * i)) & 0xff;
54 
55 	ret = i2c_smbus_write_i2c_block_data(chip->i2c, reg, size, reg_data);
56 	return ret;
57 }
58 
59 static int mt6660_reg_read(void *context, unsigned int reg, unsigned int *val)
60 {
61 	struct mt6660_chip *chip = context;
62 	int size = mt6660_get_reg_size(reg);
63 	int i, ret;
64 	u8 data[4];
65 	u32 reg_data = 0;
66 
67 	ret = i2c_smbus_read_i2c_block_data(chip->i2c, reg, size, data);
68 	if (ret < 0)
69 		return ret;
70 	for (i = 0; i < size; i++) {
71 		reg_data <<= 8;
72 		reg_data |= data[i];
73 	}
74 	*val = reg_data;
75 	return 0;
76 }
77 
78 static const struct regmap_config mt6660_regmap_config = {
79 	.reg_bits = 8,
80 	.val_bits = 32,
81 	.reg_write = mt6660_reg_write,
82 	.reg_read = mt6660_reg_read,
83 };
84 
85 static int mt6660_codec_dac_event(struct snd_soc_dapm_widget *w,
86 	struct snd_kcontrol *kcontrol, int event)
87 {
88 	if (event == SND_SOC_DAPM_POST_PMU)
89 		usleep_range(1000, 1100);
90 	return 0;
91 }
92 
93 static int mt6660_codec_classd_event(struct snd_soc_dapm_widget *w,
94 	struct snd_kcontrol *kcontrol, int event)
95 {
96 	struct snd_soc_component *component =
97 		snd_soc_dapm_to_component(w->dapm);
98 	int ret;
99 
100 	switch (event) {
101 	case SND_SOC_DAPM_PRE_PMU:
102 		dev_dbg(component->dev,
103 			"%s: before classd turn on\n", __func__);
104 		/* config to adaptive mode */
105 		ret = snd_soc_component_update_bits(component,
106 			MT6660_REG_BST_CTRL, 0x03, 0x03);
107 		if (ret < 0) {
108 			dev_err(component->dev, "config mode adaptive fail\n");
109 			return ret;
110 		}
111 		break;
112 	case SND_SOC_DAPM_POST_PMU:
113 		/* voltage sensing enable */
114 		ret = snd_soc_component_update_bits(component,
115 			MT6660_REG_RESV7, 0x04, 0x04);
116 		if (ret < 0) {
117 			dev_err(component->dev,
118 				"enable voltage sensing fail\n");
119 			return ret;
120 		}
121 		dev_dbg(component->dev, "Amp on\n");
122 		break;
123 	case SND_SOC_DAPM_PRE_PMD:
124 		dev_dbg(component->dev, "Amp off\n");
125 		/* voltage sensing disable */
126 		ret = snd_soc_component_update_bits(component,
127 			MT6660_REG_RESV7, 0x04, 0x00);
128 		if (ret < 0) {
129 			dev_err(component->dev,
130 				"disable voltage sensing fail\n");
131 			return ret;
132 		}
133 		/* pop-noise improvement 1 */
134 		ret = snd_soc_component_update_bits(component,
135 			MT6660_REG_RESV10, 0x10, 0x10);
136 		if (ret < 0) {
137 			dev_err(component->dev,
138 				"pop-noise improvement 1 fail\n");
139 			return ret;
140 		}
141 		break;
142 	case SND_SOC_DAPM_POST_PMD:
143 		dev_dbg(component->dev,
144 			"%s: after classd turn off\n", __func__);
145 		/* pop-noise improvement 2 */
146 		ret = snd_soc_component_update_bits(component,
147 			MT6660_REG_RESV10, 0x10, 0x00);
148 		if (ret < 0) {
149 			dev_err(component->dev,
150 				"pop-noise improvement 2 fail\n");
151 			return ret;
152 		}
153 		/* config to off mode */
154 		ret = snd_soc_component_update_bits(component,
155 			MT6660_REG_BST_CTRL, 0x03, 0x00);
156 		if (ret < 0) {
157 			dev_err(component->dev, "config mode off fail\n");
158 			return ret;
159 		}
160 		break;
161 	}
162 	return 0;
163 }
164 
165 static const struct snd_soc_dapm_widget mt6660_component_dapm_widgets[] = {
166 	SND_SOC_DAPM_DAC_E("DAC", NULL, MT6660_REG_PLL_CFG1,
167 		0, 1, mt6660_codec_dac_event, SND_SOC_DAPM_POST_PMU),
168 	SND_SOC_DAPM_ADC("VI ADC", NULL, SND_SOC_NOPM, 0, 0),
169 	SND_SOC_DAPM_PGA("PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
170 	SND_SOC_DAPM_OUT_DRV_E("ClassD", MT6660_REG_SYSTEM_CTRL, 2, 0,
171 			       NULL, 0, mt6660_codec_classd_event,
172 			       SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
173 			       SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
174 	SND_SOC_DAPM_OUTPUT("OUTP"),
175 	SND_SOC_DAPM_OUTPUT("OUTN"),
176 };
177 
178 static const struct snd_soc_dapm_route mt6660_component_dapm_routes[] = {
179 	{ "DAC", NULL, "aif_playback" },
180 	{ "PGA", NULL, "DAC" },
181 	{ "ClassD", NULL, "PGA" },
182 	{ "OUTP", NULL, "ClassD" },
183 	{ "OUTN", NULL, "ClassD" },
184 	{ "VI ADC", NULL, "ClassD" },
185 	{ "aif_capture", NULL, "VI ADC" },
186 };
187 
188 static int mt6660_component_get_volsw(struct snd_kcontrol *kcontrol,
189 				  struct snd_ctl_elem_value *ucontrol)
190 {
191 	struct snd_soc_component *component =
192 		snd_soc_kcontrol_component(kcontrol);
193 	struct mt6660_chip *chip = (struct mt6660_chip *)
194 		snd_soc_component_get_drvdata(component);
195 
196 	ucontrol->value.integer.value[0] = chip->chip_rev & 0x0f;
197 	return 0;
198 }
199 
200 static const DECLARE_TLV_DB_SCALE(vol_ctl_tlv, -1155, 5, 0);
201 
202 static const struct snd_kcontrol_new mt6660_component_snd_controls[] = {
203 	SOC_SINGLE_TLV("Digital Volume", MT6660_REG_VOL_CTRL, 0, 255,
204 			   1, vol_ctl_tlv),
205 	SOC_SINGLE("Hard Clip Switch", MT6660_REG_HCLIP_CTRL, 8, 1, 0),
206 	SOC_SINGLE("Clip Switch", MT6660_REG_SPS_CTRL, 0, 1, 0),
207 	SOC_SINGLE("Boost Mode", MT6660_REG_BST_CTRL, 0, 3, 0),
208 	SOC_SINGLE("DRE Switch", MT6660_REG_DRE_CTRL, 0, 1, 0),
209 	SOC_SINGLE("DC Protect Switch",	MT6660_REG_DC_PROTECT_CTRL, 3, 1, 0),
210 	SOC_SINGLE("Data Output Left Channel Selection",
211 		   MT6660_REG_DATAO_SEL, 3, 7, 0),
212 	SOC_SINGLE("Data Output Right Channel Selection",
213 		   MT6660_REG_DATAO_SEL, 0, 7, 0),
214 	SOC_SINGLE_EXT("T0 SEL", MT6660_REG_CALI_T0, 0, 7, 0,
215 		       snd_soc_get_volsw, NULL),
216 	SOC_SINGLE_EXT("Chip Rev", MT6660_REG_DEVID, 8, 15, 0,
217 		       mt6660_component_get_volsw, NULL),
218 };
219 
220 static int _mt6660_chip_power_on(struct mt6660_chip *chip, int on_off)
221 {
222 	return regmap_write_bits(chip->regmap, MT6660_REG_SYSTEM_CTRL,
223 				 0x01, on_off ? 0x00 : 0x01);
224 }
225 
226 struct reg_table {
227 	uint32_t addr;
228 	uint32_t mask;
229 	uint32_t val;
230 };
231 
232 static const struct reg_table mt6660_setting_table[] = {
233 	{ 0x20, 0x80, 0x00 },
234 	{ 0x30, 0x01, 0x00 },
235 	{ 0x50, 0x1c, 0x04 },
236 	{ 0xB1, 0x0c, 0x00 },
237 	{ 0xD3, 0x03, 0x03 },
238 	{ 0xE0, 0x01, 0x00 },
239 	{ 0x98, 0x44, 0x04 },
240 	{ 0xB9, 0xff, 0x82 },
241 	{ 0xB7, 0x7777, 0x7273 },
242 	{ 0xB6, 0x07, 0x03 },
243 	{ 0x6B, 0xe0, 0x20 },
244 	{ 0x07, 0xff, 0x70 },
245 	{ 0xBB, 0xff, 0x20 },
246 	{ 0x69, 0xff, 0x40 },
247 	{ 0xBD, 0xffff, 0x17f8 },
248 	{ 0x70, 0xff, 0x15 },
249 	{ 0x7C, 0xff, 0x00 },
250 	{ 0x46, 0xff, 0x1d },
251 	{ 0x1A, 0xffffffff, 0x7fdb7ffe },
252 	{ 0x1B, 0xffffffff, 0x7fdb7ffe },
253 	{ 0x51, 0xff, 0x58 },
254 	{ 0xA2, 0xff, 0xce },
255 	{ 0x33, 0xffff, 0x7fff },
256 	{ 0x4C, 0xffff, 0x0116 },
257 	{ 0x16, 0x1800, 0x0800 },
258 	{ 0x68, 0x1f, 0x07 },
259 };
260 
261 static int mt6660_component_setting(struct snd_soc_component *component)
262 {
263 	struct mt6660_chip *chip = snd_soc_component_get_drvdata(component);
264 	int ret = 0;
265 	size_t i = 0;
266 
267 	ret = _mt6660_chip_power_on(chip, 1);
268 	if (ret < 0) {
269 		dev_err(component->dev, "%s chip power on failed\n", __func__);
270 		return ret;
271 	}
272 
273 	for (i = 0; i < ARRAY_SIZE(mt6660_setting_table); i++) {
274 		ret = snd_soc_component_update_bits(component,
275 				mt6660_setting_table[i].addr,
276 				mt6660_setting_table[i].mask,
277 				mt6660_setting_table[i].val);
278 		if (ret < 0) {
279 			dev_err(component->dev, "%s update 0x%02x failed\n",
280 				__func__, mt6660_setting_table[i].addr);
281 			return ret;
282 		}
283 	}
284 
285 	ret = _mt6660_chip_power_on(chip, 0);
286 	if (ret < 0) {
287 		dev_err(component->dev, "%s chip power off failed\n", __func__);
288 		return ret;
289 	}
290 
291 	return 0;
292 }
293 
294 static int mt6660_component_probe(struct snd_soc_component *component)
295 {
296 	struct mt6660_chip *chip = snd_soc_component_get_drvdata(component);
297 	int ret;
298 
299 	dev_dbg(component->dev, "%s\n", __func__);
300 	snd_soc_component_init_regmap(component, chip->regmap);
301 
302 	ret = mt6660_component_setting(component);
303 	if (ret < 0)
304 		dev_err(chip->dev, "mt6660 component setting failed\n");
305 
306 	return ret;
307 }
308 
309 static void mt6660_component_remove(struct snd_soc_component *component)
310 {
311 	dev_dbg(component->dev, "%s\n", __func__);
312 	snd_soc_component_exit_regmap(component);
313 }
314 
315 static const struct snd_soc_component_driver mt6660_component_driver = {
316 	.probe = mt6660_component_probe,
317 	.remove = mt6660_component_remove,
318 
319 	.controls = mt6660_component_snd_controls,
320 	.num_controls = ARRAY_SIZE(mt6660_component_snd_controls),
321 	.dapm_widgets = mt6660_component_dapm_widgets,
322 	.num_dapm_widgets = ARRAY_SIZE(mt6660_component_dapm_widgets),
323 	.dapm_routes = mt6660_component_dapm_routes,
324 	.num_dapm_routes = ARRAY_SIZE(mt6660_component_dapm_routes),
325 
326 	.idle_bias_on = false, /* idle_bias_off = true */
327 };
328 
329 static int mt6660_component_aif_hw_params(struct snd_pcm_substream *substream,
330 	struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
331 {
332 	int word_len = params_physical_width(hw_params);
333 	int aud_bit = params_width(hw_params);
334 	u16 reg_data = 0;
335 	int ret;
336 
337 	dev_dbg(dai->dev, "%s: ++\n", __func__);
338 	dev_dbg(dai->dev, "format: 0x%08x\n", params_format(hw_params));
339 	dev_dbg(dai->dev, "rate: 0x%08x\n", params_rate(hw_params));
340 	dev_dbg(dai->dev, "word_len: %d, aud_bit: %d\n", word_len, aud_bit);
341 	if (word_len > 32 || word_len < 16) {
342 		dev_err(dai->dev, "not supported word length\n");
343 		return -ENOTSUPP;
344 	}
345 	switch (aud_bit) {
346 	case 16:
347 		reg_data = 3;
348 		break;
349 	case 18:
350 		reg_data = 2;
351 		break;
352 	case 20:
353 		reg_data = 1;
354 		break;
355 	case 24:
356 	case 32:
357 		reg_data = 0;
358 		break;
359 	default:
360 		return -ENOTSUPP;
361 	}
362 	ret = snd_soc_component_update_bits(dai->component,
363 		MT6660_REG_SERIAL_CFG1, 0xc0, (reg_data << 6));
364 	if (ret < 0) {
365 		dev_err(dai->dev, "config aud bit fail\n");
366 		return ret;
367 	}
368 	ret = snd_soc_component_update_bits(dai->component,
369 		MT6660_REG_TDM_CFG3, 0x3f0, word_len << 4);
370 	if (ret < 0) {
371 		dev_err(dai->dev, "config word len fail\n");
372 		return ret;
373 	}
374 	dev_dbg(dai->dev, "%s: --\n", __func__);
375 	return 0;
376 }
377 
378 static const struct snd_soc_dai_ops mt6660_component_aif_ops = {
379 	.hw_params = mt6660_component_aif_hw_params,
380 };
381 
382 #define STUB_RATES	SNDRV_PCM_RATE_8000_192000
383 #define STUB_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
384 			SNDRV_PCM_FMTBIT_U16_LE | \
385 			SNDRV_PCM_FMTBIT_S24_LE | \
386 			SNDRV_PCM_FMTBIT_U24_LE | \
387 			SNDRV_PCM_FMTBIT_S32_LE | \
388 			SNDRV_PCM_FMTBIT_U32_LE)
389 
390 static struct snd_soc_dai_driver mt6660_codec_dai = {
391 	.name = "mt6660-aif",
392 	.playback = {
393 		.stream_name	= "aif_playback",
394 		.channels_min	= 1,
395 		.channels_max	= 2,
396 		.rates		= STUB_RATES,
397 		.formats	= STUB_FORMATS,
398 	},
399 	.capture = {
400 		.stream_name	= "aif_capture",
401 		.channels_min	= 1,
402 		.channels_max	= 2,
403 		.rates = STUB_RATES,
404 		.formats = STUB_FORMATS,
405 	},
406 	/* dai properties */
407 	.symmetric_rate = 1,
408 	.symmetric_channels = 1,
409 	.symmetric_sample_bits = 1,
410 	/* dai operations */
411 	.ops = &mt6660_component_aif_ops,
412 };
413 
414 static int _mt6660_chip_id_check(struct mt6660_chip *chip)
415 {
416 	int ret;
417 	unsigned int val;
418 
419 	ret = regmap_read(chip->regmap, MT6660_REG_DEVID, &val);
420 	if (ret < 0)
421 		return ret;
422 	val &= 0x0ff0;
423 	if (val != 0x00e0 && val != 0x01e0) {
424 		dev_err(chip->dev, "%s id(%x) not match\n", __func__, val);
425 		return -ENODEV;
426 	}
427 	return 0;
428 }
429 
430 static int _mt6660_chip_sw_reset(struct mt6660_chip *chip)
431 {
432 	int ret;
433 
434 	/* turn on main pll first, then trigger reset */
435 	ret = regmap_write(chip->regmap, MT6660_REG_SYSTEM_CTRL, 0x00);
436 	if (ret < 0)
437 		return ret;
438 	ret = regmap_write(chip->regmap, MT6660_REG_SYSTEM_CTRL, 0x80);
439 	if (ret < 0)
440 		return ret;
441 	msleep(30);
442 	return 0;
443 }
444 
445 static int _mt6660_read_chip_revision(struct mt6660_chip *chip)
446 {
447 	int ret;
448 	unsigned int val;
449 
450 	ret = regmap_read(chip->regmap, MT6660_REG_DEVID, &val);
451 	if (ret < 0) {
452 		dev_err(chip->dev, "get chip revision fail\n");
453 		return ret;
454 	}
455 	chip->chip_rev = val&0xff;
456 	dev_info(chip->dev, "%s chip_rev = %x\n", __func__, chip->chip_rev);
457 	return 0;
458 }
459 
460 static int mt6660_i2c_probe(struct i2c_client *client,
461 			    const struct i2c_device_id *id)
462 {
463 	struct mt6660_chip *chip = NULL;
464 	int ret;
465 
466 	dev_dbg(&client->dev, "%s\n", __func__);
467 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
468 	if (!chip)
469 		return -ENOMEM;
470 	chip->i2c = client;
471 	chip->dev = &client->dev;
472 	mutex_init(&chip->io_lock);
473 	i2c_set_clientdata(client, chip);
474 
475 	chip->regmap = devm_regmap_init(&client->dev,
476 		NULL, chip, &mt6660_regmap_config);
477 	if (IS_ERR(chip->regmap)) {
478 		ret = PTR_ERR(chip->regmap);
479 		dev_err(&client->dev, "failed to initialise regmap: %d\n", ret);
480 		return ret;
481 	}
482 
483 	/* chip reset first */
484 	ret = _mt6660_chip_sw_reset(chip);
485 	if (ret < 0) {
486 		dev_err(chip->dev, "chip reset fail\n");
487 		goto probe_fail;
488 	}
489 	/* chip power on */
490 	ret = _mt6660_chip_power_on(chip, 1);
491 	if (ret < 0) {
492 		dev_err(chip->dev, "chip power on 2 fail\n");
493 		goto probe_fail;
494 	}
495 	/* chip devid check */
496 	ret = _mt6660_chip_id_check(chip);
497 	if (ret < 0) {
498 		dev_err(chip->dev, "chip id check fail\n");
499 		goto probe_fail;
500 	}
501 	/* chip revision get */
502 	ret = _mt6660_read_chip_revision(chip);
503 	if (ret < 0) {
504 		dev_err(chip->dev, "read chip revision fail\n");
505 		goto probe_fail;
506 	}
507 	pm_runtime_set_active(chip->dev);
508 	pm_runtime_enable(chip->dev);
509 
510 	ret = devm_snd_soc_register_component(chip->dev,
511 					       &mt6660_component_driver,
512 					       &mt6660_codec_dai, 1);
513 	return ret;
514 probe_fail:
515 	_mt6660_chip_power_on(chip, 0);
516 	mutex_destroy(&chip->io_lock);
517 	return ret;
518 }
519 
520 static int mt6660_i2c_remove(struct i2c_client *client)
521 {
522 	struct mt6660_chip *chip = i2c_get_clientdata(client);
523 
524 	pm_runtime_disable(chip->dev);
525 	pm_runtime_set_suspended(chip->dev);
526 	mutex_destroy(&chip->io_lock);
527 	return 0;
528 }
529 
530 static int __maybe_unused mt6660_i2c_runtime_suspend(struct device *dev)
531 {
532 	struct mt6660_chip *chip = dev_get_drvdata(dev);
533 
534 	dev_dbg(dev, "enter low power mode\n");
535 	return regmap_update_bits(chip->regmap,
536 		MT6660_REG_SYSTEM_CTRL, 0x01, 0x01);
537 }
538 
539 static int __maybe_unused mt6660_i2c_runtime_resume(struct device *dev)
540 {
541 	struct mt6660_chip *chip = dev_get_drvdata(dev);
542 
543 	dev_dbg(dev, "exit low power mode\n");
544 	return regmap_update_bits(chip->regmap,
545 		MT6660_REG_SYSTEM_CTRL, 0x01, 0x00);
546 }
547 
548 static const struct dev_pm_ops mt6660_dev_pm_ops = {
549 	SET_RUNTIME_PM_OPS(mt6660_i2c_runtime_suspend,
550 			   mt6660_i2c_runtime_resume, NULL)
551 };
552 
553 static const struct of_device_id __maybe_unused mt6660_of_id[] = {
554 	{ .compatible = "mediatek,mt6660",},
555 	{},
556 };
557 MODULE_DEVICE_TABLE(of, mt6660_of_id);
558 
559 static const struct i2c_device_id mt6660_i2c_id[] = {
560 	{"mt6660", 0 },
561 	{},
562 };
563 MODULE_DEVICE_TABLE(i2c, mt6660_i2c_id);
564 
565 static struct i2c_driver mt6660_i2c_driver = {
566 	.driver = {
567 		.name = "mt6660",
568 		.of_match_table = of_match_ptr(mt6660_of_id),
569 		.pm = &mt6660_dev_pm_ops,
570 	},
571 	.probe = mt6660_i2c_probe,
572 	.remove = mt6660_i2c_remove,
573 	.id_table = mt6660_i2c_id,
574 };
575 module_i2c_driver(mt6660_i2c_driver);
576 
577 MODULE_AUTHOR("Jeff Chang <jeff_chang@richtek.com>");
578 MODULE_DESCRIPTION("MT6660 SPKAMP Driver");
579 MODULE_LICENSE("GPL");
580 MODULE_VERSION("1.0.8_G");
581