sound: Add a driver for max98088
[oweals/u-boot.git] / drivers / sound / max98088.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * max98088.c -- MAX98088 ALSA SoC Audio driver
4  *
5  * Copyright 2010 Maxim Integrated Products
6  *
7  * Modified for U-Boot by Chih-Chung Chang (chihchung@chromium.org),
8  * following the changes made in max98095.c
9  */
10
11 #include <common.h>
12 #include <audio_codec.h>
13 #include <div64.h>
14 #include <dm.h>
15 #include <i2c.h>
16 #include <i2s.h>
17 #include <sound.h>
18 #include <asm/gpio.h>
19 #include <asm/io.h>
20 #include <asm/arch/clk.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/power.h>
23 #include "maxim_codec.h"
24 #include "max98088.h"
25
26 /* codec mclk clock divider coefficients. Index 0 is reserved. */
27 static const int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000,
28                                  44100, 48000, 88200, 96000};
29
30 /*
31  * codec mclk clock divider coefficients based on sampling rate
32  *
33  * @param rate sampling rate
34  * @param value address of indexvalue to be stored
35  *
36  * @return      0 for success or negative error code.
37  */
38 static int rate_value(int rate, u8 *value)
39 {
40         int i;
41
42         for (i = 1; i < ARRAY_SIZE(rate_table); i++) {
43                 if (rate_table[i] >= rate) {
44                         *value = i;
45                         return 0;
46                 }
47         }
48         *value = 1;
49
50         return -EINVAL;
51 }
52
53 /*
54  * Sets hw params for max98088
55  *
56  * @priv: max98088 information pointer
57  * @rate: Sampling rate
58  * @bits_per_sample: Bits per sample
59  *
60  * @return -EIO for error, 0 for success.
61  */
62 int max98088_hw_params(struct maxim_priv *priv, unsigned int rate,
63                        unsigned int bits_per_sample)
64 {
65         int error;
66         u8 regval;
67
68         switch (bits_per_sample) {
69         case 16:
70                 error = maxim_bic_or(priv, M98088_REG_DAI1_FORMAT,
71                                      M98088_DAI_WS, 0);
72                 break;
73         case 24:
74                 error = maxim_bic_or(priv, M98088_REG_DAI1_FORMAT,
75                                      M98088_DAI_WS, M98088_DAI_WS);
76                 break;
77         default:
78                 debug("%s: Illegal bits per sample %d.\n",
79                       __func__, bits_per_sample);
80                 return -EINVAL;
81         }
82
83         error |= maxim_bic_or(priv, M98088_REG_PWR_SYS, M98088_SHDNRUN, 0);
84
85         if (rate_value(rate, &regval)) {
86                 debug("%s: Failed to set sample rate to %d.\n",
87                       __func__, rate);
88                 return -EIO;
89         }
90
91         error |= maxim_bic_or(priv, M98088_REG_DAI1_CLKMODE,
92                               M98088_CLKMODE_MASK, regval << 4);
93         priv->rate = rate;
94
95         /* Update sample rate mode */
96         if (rate < 50000)
97                 error |= maxim_bic_or(priv, M98088_REG_DAI1_FILTERS,
98                                       M98088_DAI_DHF, 0);
99         else
100                 error |= maxim_bic_or(priv, M98088_REG_DAI1_FILTERS,
101                                       M98088_DAI_DHF, M98088_DAI_DHF);
102
103         error |= maxim_bic_or(priv, M98088_REG_PWR_SYS, M98088_SHDNRUN,
104                               M98088_SHDNRUN);
105
106         if (error < 0) {
107                 debug("%s: Error setting hardware params.\n", __func__);
108                 return -EIO;
109         }
110         priv->rate = rate;
111
112         return 0;
113 }
114
115 /*
116  * Configures Audio interface system clock for the given frequency
117  *
118  * @priv: max98088 information
119  * @freq: Sampling frequency in Hz
120  *
121  * @return -EIO for error, 0 for success.
122  */
123 int max98088_set_sysclk(struct maxim_priv *priv, unsigned int freq)
124 {
125         int error = 0;
126         u8 pwr;
127
128         /* Requested clock frequency is already setup */
129         if (freq == priv->sysclk)
130                 return 0;
131
132         /*
133          * Setup clocks for slave mode, and using the PLL
134          * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
135          *         0x02 (when master clk is 20MHz to 30MHz)..
136          */
137         if (freq >= 10000000 && freq < 20000000) {
138                 error = maxim_i2c_write(priv, M98088_REG_SYS_CLK, 0x10);
139         } else if ((freq >= 20000000) && (freq < 30000000)) {
140                 error = maxim_i2c_write(priv, M98088_REG_SYS_CLK, 0x20);
141         } else {
142                 debug("%s: Invalid master clock frequency\n", __func__);
143                 return -EIO;
144         }
145
146         error |= maxim_i2c_read(priv, M98088_REG_PWR_SYS, &pwr);
147         if (pwr & M98088_SHDNRUN) {
148                 error |= maxim_bic_or(priv, M98088_REG_PWR_SYS,
149                                       M98088_SHDNRUN, 0);
150                 error |= maxim_bic_or(priv, M98088_REG_PWR_SYS,
151                                       M98088_SHDNRUN, M98088_SHDNRUN);
152         }
153
154         debug("%s: Clock at %uHz\n", __func__, freq);
155         if (error < 0)
156                 return -EIO;
157
158         priv->sysclk = freq;
159
160         return 0;
161 }
162
163 /*
164  * Sets Max98090 I2S format
165  *
166  * @priv: max98088 information
167  * @fmt: i2S format - supports a subset of the options defined in i2s.h.
168  *
169  * @return -EIO for error, 0 for success.
170  */
171 int max98088_set_fmt(struct maxim_priv *priv, int fmt)
172 {
173         u8 reg15val;
174         u8 reg14val = 0;
175         int error = 0;
176
177         if (fmt == priv->fmt)
178                 return 0;
179
180         priv->fmt = fmt;
181
182         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
183         case SND_SOC_DAIFMT_CBS_CFS:
184                 /* Slave mode PLL */
185                 error |= maxim_i2c_write(priv, M98088_REG_DAI1_CLKCFG_HI,
186                                             0x80);
187                 error |= maxim_i2c_write(priv, M98088_REG_DAI1_CLKCFG_LO,
188                                             0x00);
189                 break;
190         case SND_SOC_DAIFMT_CBM_CFM:
191                 /* Set to master mode */
192                 reg14val |= M98088_DAI_MAS;
193                 break;
194         case SND_SOC_DAIFMT_CBS_CFM:
195         case SND_SOC_DAIFMT_CBM_CFS:
196         default:
197                 debug("%s: Clock mode unsupported\n", __func__);
198                 return -EINVAL;
199         }
200
201         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
202         case SND_SOC_DAIFMT_I2S:
203                 reg14val |= M98088_DAI_DLY;
204                 break;
205         case SND_SOC_DAIFMT_LEFT_J:
206                 break;
207         default:
208                 debug("%s: Unrecognized format.\n", __func__);
209                 return -EINVAL;
210         }
211
212         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
213         case SND_SOC_DAIFMT_NB_NF:
214                 break;
215         case SND_SOC_DAIFMT_NB_IF:
216                 reg14val |= M98088_DAI_WCI;
217                 break;
218         case SND_SOC_DAIFMT_IB_NF:
219                 reg14val |= M98088_DAI_BCI;
220                 break;
221         case SND_SOC_DAIFMT_IB_IF:
222                 reg14val |= M98088_DAI_BCI | M98088_DAI_WCI;
223                 break;
224         default:
225                 debug("%s: Unrecognized inversion settings.\n",  __func__);
226                 return -EINVAL;
227         }
228
229         error |= maxim_bic_or(priv, M98088_REG_DAI1_FORMAT,
230                               M98088_DAI_MAS | M98088_DAI_DLY | M98088_DAI_BCI |
231                               M98088_DAI_WCI, reg14val);
232         reg15val = M98088_DAI_BSEL64;
233         error |= maxim_i2c_write(priv, M98088_REG_DAI1_CLOCK, reg15val);
234
235         if (error < 0) {
236                 debug("%s: Error setting i2s format.\n", __func__);
237                 return -EIO;
238         }
239
240         return 0;
241 }
242
243 /*
244  * max98088_reset() - reset the audio codec
245  *
246  * @priv: max98088 information
247  * @return -EIO for error, 0 for success.
248  */
249 static int max98088_reset(struct maxim_priv *priv)
250 {
251         int ret, i;
252         u8 val;
253
254         /*
255          * Reset to hardware default for registers, as there is not a soft
256          * reset hardware control register.
257          */
258         for (i = M98088_REG_IRQ_ENABLE; i <= M98088_REG_PWR_SYS; i++) {
259                 switch (i) {
260                 case M98088_REG_BIAS_CNTL:
261                         val = 0xf0;
262                         break;
263                 case M98088_REG_DAC_BIAS2:
264                         val = 0x0f;
265                         break;
266                 default:
267                         val = 0;
268                 }
269                 ret = maxim_i2c_write(priv, i, val);
270                 if (ret < 0) {
271                         debug("%s: Failed to reset: %d\n", __func__, ret);
272                         return ret;
273                 }
274         }
275
276         return 0;
277 }
278
279 /**
280  * max98088_device_init() - Initialise max98088 codec device
281  *
282  * @priv: max98088 information
283  *
284  * @return -EIO for error, 0 for success.
285  */
286 static int max98088_device_init(struct maxim_priv *priv)
287 {
288         unsigned char id;
289         int error = 0;
290
291         /* Enable codec clock */
292         set_xclkout();
293
294         /* reset the codec, the DSP core, and disable all interrupts */
295         error = max98088_reset(priv);
296         if (error != 0) {
297                 debug("Reset\n");
298                 return error;
299         }
300
301         /* initialize private data */
302         priv->sysclk = -1U;
303         priv->rate = -1U;
304         priv->fmt = -1U;
305
306         error = maxim_i2c_read(priv, M98088_REG_REV_ID, &id);
307         if (error < 0) {
308                 debug("%s: Failure reading hardware revision: %d\n",
309                       __func__, id);
310                 return -EIO;
311         }
312         debug("%s: Hardware revision: %d\n", __func__, id);
313
314         return 0;
315 }
316
317 static int max98088_setup_interface(struct maxim_priv *priv)
318 {
319         int error;
320
321         /* Reading interrupt status to clear them */
322         error = maxim_i2c_write(priv, M98088_REG_PWR_SYS, M98088_PWRSV);
323         error |= maxim_i2c_write(priv, M98088_REG_IRQ_ENABLE, 0x00);
324
325         /*
326          * initialize registers to hardware default configuring audio
327          * interface2 to DAI1
328          */
329         error |= maxim_i2c_write(priv, M98088_REG_MIX_DAC,
330                                  M98088_DAI1L_TO_DACL | M98088_DAI1R_TO_DACR);
331         error |= maxim_i2c_write(priv, M98088_REG_BIAS_CNTL, 0xF0);
332         error |= maxim_i2c_write(priv, M98088_REG_DAC_BIAS2, 0x0F);
333         error |= maxim_i2c_write(priv, M98088_REG_DAI1_IOCFG,
334                                  M98088_S2NORMAL | M98088_SDATA);
335
336         /*
337          * route DACL and DACR output to headphone and speakers
338          * Ordering: DACL, DACR, DACL, DACR
339          */
340         error |= maxim_i2c_write(priv, M98088_REG_MIX_SPK_LEFT, 1);
341         error |= maxim_i2c_write(priv, M98088_REG_MIX_SPK_RIGHT, 1);
342         error |= maxim_i2c_write(priv, M98088_REG_MIX_HP_LEFT, 1);
343         error |= maxim_i2c_write(priv, M98088_REG_MIX_HP_RIGHT, 1);
344
345         /* set volume: -12db */
346         error |= maxim_i2c_write(priv, M98088_REG_LVL_SPK_L, 0x0f);
347         error |= maxim_i2c_write(priv, M98088_REG_LVL_SPK_R, 0x0f);
348
349         /* set volume: -22db */
350         error |= maxim_i2c_write(priv, M98088_REG_LVL_HP_L, 0x0d);
351         error |= maxim_i2c_write(priv, M98088_REG_LVL_HP_R, 0x0d);
352
353         /* power enable */
354         error |= maxim_i2c_write(priv, M98088_REG_PWR_EN_OUT,
355                                  M98088_HPLEN | M98088_HPREN | M98088_SPLEN |
356                                  M98088_SPREN | M98088_DALEN | M98088_DAREN);
357         if (error < 0)
358                 return -EIO;
359
360         return 0;
361 }
362
363 static int max98088_do_init(struct maxim_priv *priv, int sampling_rate,
364                             int mclk_freq, int bits_per_sample)
365 {
366         int ret = 0;
367
368         ret = max98088_setup_interface(priv);
369         if (ret < 0) {
370                 debug("%s: max98088 setup interface failed\n", __func__);
371                 return ret;
372         }
373
374         ret = max98088_set_sysclk(priv, mclk_freq);
375         if (ret < 0) {
376                 debug("%s: max98088 codec set sys clock failed\n", __func__);
377                 return ret;
378         }
379
380         ret = max98088_hw_params(priv, sampling_rate, bits_per_sample);
381
382         if (ret == 0) {
383                 ret = max98088_set_fmt(priv, SND_SOC_DAIFMT_I2S |
384                                        SND_SOC_DAIFMT_NB_NF |
385                                        SND_SOC_DAIFMT_CBS_CFS);
386         }
387
388         return ret;
389 }
390
391 static int max98088_set_params(struct udevice *dev, int interface, int rate,
392                                int mclk_freq, int bits_per_sample,
393                                uint channels)
394 {
395         struct maxim_priv *priv = dev_get_priv(dev);
396
397         return max98088_do_init(priv, rate, mclk_freq, bits_per_sample);
398 }
399
400 static int max98088_probe(struct udevice *dev)
401 {
402         struct maxim_priv *priv = dev_get_priv(dev);
403         int ret;
404
405         priv->dev = dev;
406         ret = max98088_device_init(priv);
407         if (ret < 0) {
408                 debug("%s: max98088 codec chip init failed\n", __func__);
409                 return ret;
410         }
411
412         return 0;
413 }
414
415 static const struct audio_codec_ops max98088_ops = {
416         .set_params     = max98088_set_params,
417 };
418
419 static const struct udevice_id max98088_ids[] = {
420         { .compatible = "maxim,max98088" },
421         { }
422 };
423
424 U_BOOT_DRIVER(max98088) = {
425         .name           = "max98088",
426         .id             = UCLASS_AUDIO_CODEC,
427         .of_match       = max98088_ids,
428         .probe          = max98088_probe,
429         .ops            = &max98088_ops,
430         .priv_auto_alloc_size   = sizeof(struct maxim_priv),
431 };