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