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