1 // SPDX-License-Identifier: GPL-2.0+
3 * max98090.c -- MAX98090 ALSA SoC Audio driver
5 * Copyright 2011 Maxim Integrated Products
9 #include <audio_codec.h>
17 #include <linux/delay.h>
18 #include "maxim_codec.h"
22 * Sets hw params for max98090
24 * @priv: max98090 information pointer
25 * @rate: Sampling rate
26 * @bits_per_sample: Bits per sample
28 * @return -EIO for error, 0 for success.
30 int max98090_hw_params(struct maxim_priv *priv, unsigned int rate,
31 unsigned int bits_per_sample)
36 switch (bits_per_sample) {
38 maxim_i2c_read(priv, M98090_REG_INTERFACE_FORMAT, &value);
39 error = maxim_bic_or(priv, M98090_REG_INTERFACE_FORMAT,
41 maxim_i2c_read(priv, M98090_REG_INTERFACE_FORMAT, &value);
44 debug("%s: Illegal bits per sample %d.\n",
45 __func__, bits_per_sample);
49 /* Update filter mode */
51 error |= maxim_bic_or(priv, M98090_REG_FILTER_CONFIG,
54 error |= maxim_bic_or(priv, M98090_REG_FILTER_CONFIG,
55 M98090_MODE_MASK, M98090_MODE_MASK);
57 /* Update sample rate mode */
59 error |= maxim_bic_or(priv, M98090_REG_FILTER_CONFIG,
62 error |= maxim_bic_or(priv, M98090_REG_FILTER_CONFIG,
63 M98090_DHF_MASK, M98090_DHF_MASK);
66 debug("%s: Error setting hardware params.\n", __func__);
75 * Configures Audio interface system clock for the given frequency
77 * @priv: max98090 information
78 * @freq: Sampling frequency in Hz
80 * @return -EIO for error, 0 for success.
82 int max98090_set_sysclk(struct maxim_priv *priv, unsigned int freq)
86 /* Requested clock frequency is already setup */
87 if (freq == priv->sysclk)
90 /* Setup clocks for slave mode, and using the PLL
91 * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
92 * 0x02 (when master clk is 20MHz to 40MHz)..
93 * 0x03 (when master clk is 40MHz to 60MHz)..
95 if (freq >= 10000000 && freq < 20000000) {
96 error = maxim_i2c_write(priv, M98090_REG_SYSTEM_CLOCK,
98 } else if (freq >= 20000000 && freq < 40000000) {
99 error = maxim_i2c_write(priv, M98090_REG_SYSTEM_CLOCK,
101 } else if (freq >= 40000000 && freq < 60000000) {
102 error = maxim_i2c_write(priv, M98090_REG_SYSTEM_CLOCK,
105 debug("%s: Invalid master clock frequency\n", __func__);
109 debug("%s: Clock at %uHz\n", __func__, freq);
120 * Sets Max98090 I2S format
122 * @priv: max98090 information
123 * @fmt: i2S format - supports a subset of the options defined in i2s.h.
125 * @return -EIO for error, 0 for success.
127 int max98090_set_fmt(struct maxim_priv *priv, int fmt)
132 if (fmt == priv->fmt)
137 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
138 case SND_SOC_DAIFMT_CBS_CFS:
139 /* Set to slave mode PLL - MAS mode off */
140 error |= maxim_i2c_write(priv, M98090_REG_CLOCK_RATIO_NI_MSB,
142 error |= maxim_i2c_write(priv, M98090_REG_CLOCK_RATIO_NI_LSB,
144 error |= maxim_bic_or(priv, M98090_REG_CLOCK_MODE,
145 M98090_USE_M1_MASK, 0);
147 case SND_SOC_DAIFMT_CBM_CFM:
148 /* Set to master mode */
149 debug("Master mode not supported\n");
151 case SND_SOC_DAIFMT_CBS_CFM:
152 case SND_SOC_DAIFMT_CBM_CFS:
154 debug("%s: Clock mode unsupported\n", __func__);
158 error |= maxim_i2c_write(priv, M98090_REG_MASTER_MODE, regval);
161 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
162 case SND_SOC_DAIFMT_I2S:
163 regval |= M98090_DLY_MASK;
165 case SND_SOC_DAIFMT_LEFT_J:
167 case SND_SOC_DAIFMT_RIGHT_J:
168 regval |= M98090_RJ_MASK;
170 case SND_SOC_DAIFMT_DSP_A:
171 /* Not supported mode */
173 debug("%s: Unrecognized format.\n", __func__);
177 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
178 case SND_SOC_DAIFMT_NB_NF:
180 case SND_SOC_DAIFMT_NB_IF:
181 regval |= M98090_WCI_MASK;
183 case SND_SOC_DAIFMT_IB_NF:
184 regval |= M98090_BCI_MASK;
186 case SND_SOC_DAIFMT_IB_IF:
187 regval |= M98090_BCI_MASK | M98090_WCI_MASK;
190 debug("%s: Unrecognized inversion settings.\n", __func__);
194 error |= maxim_i2c_write(priv, M98090_REG_INTERFACE_FORMAT, regval);
197 debug("%s: Error setting i2s format.\n", __func__);
205 * resets the audio codec
207 * @priv: max98090 information
208 * @return -EIO for error, 0 for success.
210 static int max98090_reset(struct maxim_priv *priv)
215 * Gracefully reset the DSP core and the codec hardware in a proper
218 ret = maxim_i2c_write(priv, M98090_REG_SOFTWARE_RESET,
219 M98090_SWRESET_MASK);
221 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
230 * Initialise max98090 codec device
232 * @priv: max98090 information
234 * @return -EIO for error, 0 for success.
236 int max98090_device_init(struct maxim_priv *priv)
241 /* reset the codec, the DSP core, and disable all interrupts */
242 error = max98090_reset(priv);
248 /* initialize private data */
253 error = maxim_i2c_read(priv, M98090_REG_REVISION_ID, &id);
255 debug("%s: Failure reading hardware revision: %d\n",
259 debug("%s: Hardware revision: %d\n", __func__, id);
264 static int max98090_setup_interface(struct maxim_priv *priv)
269 /* Reading interrupt status to clear them */
270 error = maxim_i2c_read(priv, M98090_REG_DEVICE_STATUS, &id);
272 error |= maxim_i2c_write(priv, M98090_REG_DAC_CONTROL,
274 error |= maxim_i2c_write(priv, M98090_REG_BIAS_CONTROL,
275 M98090_VCM_MODE_MASK);
277 error |= maxim_i2c_write(priv, M98090_REG_LEFT_SPK_MIXER, 0x1);
278 error |= maxim_i2c_write(priv, M98090_REG_RIGHT_SPK_MIXER, 0x2);
280 error |= maxim_i2c_write(priv, M98090_REG_LEFT_SPK_VOLUME, 0x25);
281 error |= maxim_i2c_write(priv, M98090_REG_RIGHT_SPK_VOLUME, 0x25);
283 error |= maxim_i2c_write(priv, M98090_REG_CLOCK_RATIO_NI_MSB, 0x0);
284 error |= maxim_i2c_write(priv, M98090_REG_CLOCK_RATIO_NI_LSB, 0x0);
285 error |= maxim_i2c_write(priv, M98090_REG_MASTER_MODE, 0x0);
286 error |= maxim_i2c_write(priv, M98090_REG_INTERFACE_FORMAT, 0x0);
287 error |= maxim_i2c_write(priv, M98090_REG_IO_CONFIGURATION,
289 error |= maxim_i2c_write(priv, M98090_REG_DEVICE_SHUTDOWN,
291 error |= maxim_i2c_write(priv, M98090_REG_OUTPUT_ENABLE,
292 M98090_HPREN_MASK | M98090_HPLEN_MASK |
293 M98090_SPREN_MASK | M98090_SPLEN_MASK |
294 M98090_DAREN_MASK | M98090_DALEN_MASK);
295 error |= maxim_i2c_write(priv, M98090_REG_IO_CONFIGURATION,
296 M98090_SDOEN_MASK | M98090_SDIEN_MASK);
304 static int max98090_do_init(struct maxim_priv *priv, int sampling_rate,
305 int mclk_freq, int bits_per_sample)
309 ret = max98090_setup_interface(priv);
311 debug("%s: max98090 setup interface failed\n", __func__);
315 ret = max98090_set_sysclk(priv, mclk_freq);
317 debug("%s: max98090 codec set sys clock failed\n", __func__);
321 ret = max98090_hw_params(priv, sampling_rate, bits_per_sample);
324 ret = max98090_set_fmt(priv, SND_SOC_DAIFMT_I2S |
325 SND_SOC_DAIFMT_NB_NF |
326 SND_SOC_DAIFMT_CBS_CFS);
332 static int max98090_set_params(struct udevice *dev, int interface, int rate,
333 int mclk_freq, int bits_per_sample,
336 struct maxim_priv *priv = dev_get_priv(dev);
338 return max98090_do_init(priv, rate, mclk_freq, bits_per_sample);
341 static int max98090_probe(struct udevice *dev)
343 struct maxim_priv *priv = dev_get_priv(dev);
347 ret = max98090_device_init(priv);
349 debug("%s: max98090 codec chip init failed\n", __func__);
356 static const struct audio_codec_ops max98090_ops = {
357 .set_params = max98090_set_params,
360 static const struct udevice_id max98090_ids[] = {
361 { .compatible = "maxim,max98090" },
365 U_BOOT_DRIVER(max98090) = {
367 .id = UCLASS_AUDIO_CODEC,
368 .of_match = max98090_ids,
369 .probe = max98090_probe,
370 .ops = &max98090_ops,
371 .priv_auto = sizeof(struct maxim_priv),