1 // SPDX-License-Identifier: GPL-2.0 //
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/version.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>
19 struct reg_size_table
{
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 },
36 static int mt6660_get_reg_size(uint32_t addr
)
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
;
47 static int mt6660_reg_write(void *context
, unsigned int reg
, unsigned int val
)
49 struct mt6660_chip
*chip
= context
;
50 int size
= mt6660_get_reg_size(reg
);
54 for (i
= 0; i
< size
; i
++)
55 reg_data
[size
- i
- 1] = (val
>> (8 * i
)) & 0xff;
57 ret
= i2c_smbus_write_i2c_block_data(chip
->i2c
, reg
, size
, reg_data
);
61 static int mt6660_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
63 struct mt6660_chip
*chip
= context
;
64 int size
= mt6660_get_reg_size(reg
);
69 ret
= i2c_smbus_read_i2c_block_data(chip
->i2c
, reg
, size
, data
);
72 for (i
= 0; i
< size
; i
++) {
80 static const struct regmap_config mt6660_regmap_config
= {
83 .reg_write
= mt6660_reg_write
,
84 .reg_read
= mt6660_reg_read
,
87 static int mt6660_codec_dac_event(struct snd_soc_dapm_widget
*w
,
88 struct snd_kcontrol
*kcontrol
, int event
)
90 if (event
== SND_SOC_DAPM_POST_PMU
)
91 usleep_range(1000, 1100);
95 static int mt6660_codec_classd_event(struct snd_soc_dapm_widget
*w
,
96 struct snd_kcontrol
*kcontrol
, int event
)
98 struct snd_soc_component
*component
=
99 snd_soc_dapm_to_component(w
->dapm
);
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);
110 dev_err(component
->dev
, "config mode adaptive fail\n");
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);
119 dev_err(component
->dev
,
120 "enable voltage sensing fail\n");
123 dev_dbg(component
->dev
, "Amp on\n");
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);
131 dev_err(component
->dev
,
132 "disable voltage sensing fail\n");
135 /* pop-noise improvement 1 */
136 ret
= snd_soc_component_update_bits(component
,
137 MT6660_REG_RESV10
, 0x10, 0x10);
139 dev_err(component
->dev
,
140 "pop-noise improvement 1 fail\n");
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);
151 dev_err(component
->dev
,
152 "pop-noise improvement 2 fail\n");
155 /* config to off mode */
156 ret
= snd_soc_component_update_bits(component
,
157 MT6660_REG_BST_CTRL
, 0x03, 0x00);
159 dev_err(component
->dev
, "config mode off fail\n");
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"),
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" },
190 static int mt6660_component_get_volsw(struct snd_kcontrol
*kcontrol
,
191 struct snd_ctl_elem_value
*ucontrol
)
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
);
198 ucontrol
->value
.integer
.value
[0] = chip
->chip_rev
& 0x0f;
202 static const DECLARE_TLV_DB_SCALE(vol_ctl_tlv
, -1155, 5, 0);
204 static const struct snd_kcontrol_new mt6660_component_snd_controls
[] = {
205 SOC_SINGLE_TLV("Digital Volume", MT6660_REG_VOL_CTRL
, 0, 255,
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
),
222 static int _mt6660_chip_power_on(struct mt6660_chip
*chip
, int on_off
)
224 return regmap_write_bits(chip
->regmap
, MT6660_REG_SYSTEM_CTRL
,
225 0x01, on_off
? 0x00 : 0x01);
228 static int mt6660_component_probe(struct snd_soc_component
*component
)
230 struct mt6660_chip
*chip
= snd_soc_component_get_drvdata(component
);
232 dev_dbg(component
->dev
, "%s\n", __func__
);
233 snd_soc_component_init_regmap(component
, chip
->regmap
);
238 static void mt6660_component_remove(struct snd_soc_component
*component
)
240 dev_dbg(component
->dev
, "%s\n", __func__
);
241 snd_soc_component_exit_regmap(component
);
244 static const struct snd_soc_component_driver mt6660_component_driver
= {
245 .probe
= mt6660_component_probe
,
246 .remove
= mt6660_component_remove
,
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
),
255 .idle_bias_on
= false, /* idle_bias_off = true */
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
)
261 int word_len
= params_physical_width(hw_params
);
262 int aud_bit
= params_width(hw_params
);
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");
291 ret
= snd_soc_component_update_bits(dai
->component
,
292 MT6660_REG_SERIAL_CFG1
, 0xc0, (reg_data
<< 6));
294 dev_err(dai
->dev
, "config aud bit fail\n");
297 ret
= snd_soc_component_update_bits(dai
->component
,
298 MT6660_REG_TDM_CFG3
, 0x3f0, word_len
<< 4);
300 dev_err(dai
->dev
, "config word len fail\n");
303 dev_dbg(dai
->dev
, "%s: --\n", __func__
);
307 static const struct snd_soc_dai_ops mt6660_component_aif_ops
= {
308 .hw_params
= mt6660_component_aif_hw_params
,
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)
319 static struct snd_soc_dai_driver mt6660_codec_dai
= {
320 .name
= "mt6660-aif",
322 .stream_name
= "aif_playback",
326 .formats
= STUB_FORMATS
,
329 .stream_name
= "aif_capture",
333 .formats
= STUB_FORMATS
,
336 .symmetric_rates
= 1,
337 .symmetric_channels
= 1,
338 .symmetric_samplebits
= 1,
340 .ops
= &mt6660_component_aif_ops
,
343 static int _mt6660_chip_id_check(struct mt6660_chip
*chip
)
348 ret
= regmap_read(chip
->regmap
, MT6660_REG_DEVID
, &val
);
352 if (val
!= 0x00e0 && val
!= 0x01e0) {
353 dev_err(chip
->dev
, "%s id(%x) not match\n", __func__
, val
);
359 static int _mt6660_chip_sw_reset(struct mt6660_chip
*chip
)
363 /* turn on main pll first, then trigger reset */
364 ret
= regmap_write(chip
->regmap
, MT6660_REG_SYSTEM_CTRL
, 0x00);
367 ret
= regmap_write(chip
->regmap
, MT6660_REG_SYSTEM_CTRL
, 0x80);
374 static int _mt6660_read_chip_revision(struct mt6660_chip
*chip
)
379 ret
= regmap_read(chip
->regmap
, MT6660_REG_DEVID
, &val
);
381 dev_err(chip
->dev
, "get chip revision fail\n");
384 chip
->chip_rev
= val
&0xff;
385 dev_info(chip
->dev
, "%s chip_rev = %x\n", __func__
, chip
->chip_rev
);
389 static int mt6660_i2c_probe(struct i2c_client
*client
,
390 const struct i2c_device_id
*id
)
392 struct mt6660_chip
*chip
= NULL
;
395 dev_dbg(&client
->dev
, "%s\n", __func__
);
396 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
400 chip
->dev
= &client
->dev
;
401 mutex_init(&chip
->io_lock
);
402 i2c_set_clientdata(client
, chip
);
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
);
412 /* chip reset first */
413 ret
= _mt6660_chip_sw_reset(chip
);
415 dev_err(chip
->dev
, "chip reset fail\n");
419 ret
= _mt6660_chip_power_on(chip
, 1);
421 dev_err(chip
->dev
, "chip power on 2 fail\n");
424 /* chip devid check */
425 ret
= _mt6660_chip_id_check(chip
);
427 dev_err(chip
->dev
, "chip id check fail\n");
430 /* chip revision get */
431 ret
= _mt6660_read_chip_revision(chip
);
433 dev_err(chip
->dev
, "read chip revision fail\n");
436 pm_runtime_set_active(chip
->dev
);
437 pm_runtime_enable(chip
->dev
);
439 ret
= devm_snd_soc_register_component(chip
->dev
,
440 &mt6660_component_driver
,
441 &mt6660_codec_dai
, 1);
444 _mt6660_chip_power_on(chip
, 0);
445 mutex_destroy(&chip
->io_lock
);
449 static int mt6660_i2c_remove(struct i2c_client
*client
)
451 struct mt6660_chip
*chip
= i2c_get_clientdata(client
);
453 pm_runtime_disable(chip
->dev
);
454 pm_runtime_set_suspended(chip
->dev
);
455 mutex_destroy(&chip
->io_lock
);
459 static int __maybe_unused
mt6660_i2c_runtime_suspend(struct device
*dev
)
461 struct mt6660_chip
*chip
= dev_get_drvdata(dev
);
463 dev_dbg(dev
, "enter low power mode\n");
464 return regmap_update_bits(chip
->regmap
,
465 MT6660_REG_SYSTEM_CTRL
, 0x01, 0x01);
468 static int __maybe_unused
mt6660_i2c_runtime_resume(struct device
*dev
)
470 struct mt6660_chip
*chip
= dev_get_drvdata(dev
);
472 dev_dbg(dev
, "exit low power mode\n");
473 return regmap_update_bits(chip
->regmap
,
474 MT6660_REG_SYSTEM_CTRL
, 0x01, 0x00);
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
)
482 static const struct of_device_id __maybe_unused mt6660_of_id
[] = {
483 { .compatible
= "mediatek,mt6660",},
486 MODULE_DEVICE_TABLE(of
, mt6660_of_id
);
488 static const struct i2c_device_id mt6660_i2c_id
[] = {
492 MODULE_DEVICE_TABLE(i2c
, mt6660_i2c_id
);
494 static struct i2c_driver mt6660_i2c_driver
= {
497 .of_match_table
= of_match_ptr(mt6660_of_id
),
498 .pm
= &mt6660_dev_pm_ops
,
500 .probe
= mt6660_i2c_probe
,
501 .remove
= mt6660_i2c_remove
,
502 .id_table
= mt6660_i2c_id
,
504 module_i2c_driver(mt6660_i2c_driver
);
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");