dm: sound: max98095: Drop g_codec_info and g_max98095_info
[oweals/u-boot.git] / drivers / sound / max98095.c
1 /*
2  * max98095.c -- MAX98095 ALSA SoC Audio driver
3  *
4  * Copyright 2011 Maxim Integrated Products
5  *
6  * Modified for uboot by R. Chandrasekar (rcsekar@samsung.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <common.h>
14 #include <asm/arch/clk.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/power.h>
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <common.h>
20 #include <div64.h>
21 #include <fdtdec.h>
22 #include <i2c.h>
23 #include <sound.h>
24 #include "i2s.h"
25 #include "max98095.h"
26
27 struct max98095_priv {
28         unsigned int sysclk;
29         unsigned int rate;
30         unsigned int fmt;
31         int i2c_addr;
32 };
33
34 /* Index 0 is reserved. */
35 int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
36                 88200, 96000};
37
38 /*
39  * Writes value to a device register through i2c
40  *
41  * @param priv  Private data for driver
42  * @param reg   reg number to be write
43  * @param data  data to be writen to the above registor
44  *
45  * @return      int value 1 for change, 0 for no change or negative error code.
46  */
47 static int max98095_i2c_write(struct max98095_priv *priv, unsigned int reg,
48                               unsigned char data)
49 {
50         debug("%s: Write Addr : 0x%02X, Data :  0x%02X\n",
51               __func__, reg, data);
52         return i2c_write(priv->i2c_addr, reg, 1, &data, 1);
53 }
54
55 /*
56  * Read a value from a device register through i2c
57  *
58  * @param priv  Private data for driver
59  * @param reg   reg number to be read
60  * @param data  address of read data to be stored
61  *
62  * @return      int value 0 for success, -1 in case of error.
63  */
64 static unsigned int max98095_i2c_read(struct max98095_priv *priv,
65                                       unsigned int reg, unsigned char *data)
66 {
67         int ret;
68
69         ret = i2c_read(priv->i2c_addr, reg, 1, data, 1);
70         if (ret != 0) {
71                 debug("%s: Error while reading register %#04x\n",
72                       __func__, reg);
73                 return -1;
74         }
75
76         return 0;
77 }
78
79 /*
80  * update device register bits through i2c
81  *
82  * @param priv  Private data for driver
83  * @param reg   codec register
84  * @param mask  register mask
85  * @param value new value
86  *
87  * @return int value 0 for success, non-zero error code.
88  */
89 static int max98095_bic_or(struct max98095_priv *priv, unsigned int reg,
90                            unsigned char mask, unsigned char value)
91 {
92         int change, ret = 0;
93         unsigned char old, new;
94
95         if (max98095_i2c_read(priv, reg, &old) != 0)
96                 return -1;
97         new = (old & ~mask) | (value & mask);
98         change  = (old != new) ? 1 : 0;
99         if (change)
100                 ret = max98095_i2c_write(priv, reg, new);
101         if (ret < 0)
102                 return ret;
103
104         return change;
105 }
106
107 /*
108  * codec mclk clock divider coefficients based on sampling rate
109  *
110  * @param rate sampling rate
111  * @param value address of indexvalue to be stored
112  *
113  * @return      0 for success or negative error code.
114  */
115 static int rate_value(int rate, u8 *value)
116 {
117         int i;
118
119         for (i = 1; i < ARRAY_SIZE(rate_table); i++) {
120                 if (rate_table[i] >= rate) {
121                         *value = i;
122                         return 0;
123                 }
124         }
125         *value = 1;
126
127         return -1;
128 }
129
130 /*
131  * Sets hw params for max98095
132  *
133  * @param priv          max98095 information pointer
134  * @param rate          Sampling rate
135  * @param bits_per_sample       Bits per sample
136  *
137  * @return -1 for error  and 0  Success.
138  */
139 static int max98095_hw_params(struct max98095_priv *priv,
140                               enum en_max_audio_interface aif_id,
141                               unsigned int rate, unsigned int bits_per_sample)
142 {
143         u8 regval;
144         int error;
145         unsigned short M98095_DAI_CLKMODE;
146         unsigned short M98095_DAI_FORMAT;
147         unsigned short M98095_DAI_FILTERS;
148
149         if (aif_id == AIF1) {
150                 M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE;
151                 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
152                 M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS;
153         } else {
154                 M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE;
155                 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
156                 M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS;
157         }
158
159         switch (bits_per_sample) {
160         case 16:
161                 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
162                                         0);
163                 break;
164         case 24:
165                 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
166                                         M98095_DAI_WS);
167                 break;
168         default:
169                 debug("%s: Illegal bits per sample %d.\n",
170                       __func__, bits_per_sample);
171                 return -1;
172         }
173
174         if (rate_value(rate, &regval)) {
175                 debug("%s: Failed to set sample rate to %d.\n",
176                       __func__, rate);
177                 return -1;
178         }
179         priv->rate = rate;
180
181         error |= max98095_bic_or(priv, M98095_DAI_CLKMODE, M98095_CLKMODE_MASK,
182                                  regval);
183
184         /* Update sample rate mode */
185         if (rate < 50000)
186                 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
187                                          M98095_DAI_DHF, 0);
188         else
189                 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
190                                          M98095_DAI_DHF, M98095_DAI_DHF);
191
192         if (error < 0) {
193                 debug("%s: Error setting hardware params.\n", __func__);
194                 return -1;
195         }
196
197         return 0;
198 }
199
200 /*
201  * Configures Audio interface system clock for the given frequency
202  *
203  * @param priv          max98095 information
204  * @param freq          Sampling frequency in Hz
205  *
206  * @return -1 for error and 0 success.
207  */
208 static int max98095_set_sysclk(struct max98095_priv *priv, unsigned int freq)
209 {
210         int error = 0;
211
212         /* Requested clock frequency is already setup */
213         if (freq == priv->sysclk)
214                 return 0;
215
216         /* Setup clocks for slave mode, and using the PLL
217          * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
218          *      0x02 (when master clk is 20MHz to 40MHz)..
219          *      0x03 (when master clk is 40MHz to 60MHz)..
220          */
221         if ((freq >= 10000000) && (freq < 20000000)) {
222                 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x10);
223         } else if ((freq >= 20000000) && (freq < 40000000)) {
224                 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x20);
225         } else if ((freq >= 40000000) && (freq < 60000000)) {
226                 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x30);
227         } else {
228                 debug("%s: Invalid master clock frequency\n", __func__);
229                 return -1;
230         }
231
232         debug("%s: Clock at %uHz\n", __func__, freq);
233
234         if (error < 0)
235                 return -1;
236
237         priv->sysclk = freq;
238         return 0;
239 }
240
241 /*
242  * Sets Max98095 I2S format
243  *
244  * @param priv          max98095 information
245  * @param fmt           i2S format - supports a subset of the options defined
246  *                      in i2s.h.
247  *
248  * @return -1 for error and 0  Success.
249  */
250 static int max98095_set_fmt(struct max98095_priv *priv, int fmt,
251                             enum en_max_audio_interface aif_id)
252 {
253         u8 regval = 0;
254         int error = 0;
255         unsigned short M98095_DAI_CLKCFG_HI;
256         unsigned short M98095_DAI_CLKCFG_LO;
257         unsigned short M98095_DAI_FORMAT;
258         unsigned short M98095_DAI_CLOCK;
259
260         if (fmt == priv->fmt)
261                 return 0;
262
263         priv->fmt = fmt;
264
265         if (aif_id == AIF1) {
266                 M98095_DAI_CLKCFG_HI = M98095_028_DAI1_CLKCFG_HI;
267                 M98095_DAI_CLKCFG_LO = M98095_029_DAI1_CLKCFG_LO;
268                 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
269                 M98095_DAI_CLOCK = M98095_02B_DAI1_CLOCK;
270         } else {
271                 M98095_DAI_CLKCFG_HI = M98095_032_DAI2_CLKCFG_HI;
272                 M98095_DAI_CLKCFG_LO = M98095_033_DAI2_CLKCFG_LO;
273                 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
274                 M98095_DAI_CLOCK = M98095_035_DAI2_CLOCK;
275         }
276
277         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
278         case SND_SOC_DAIFMT_CBS_CFS:
279                 /* Slave mode PLL */
280                 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_HI, 0x80);
281                 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_LO, 0x00);
282                 break;
283         case SND_SOC_DAIFMT_CBM_CFM:
284                 /* Set to master mode */
285                 regval |= M98095_DAI_MAS;
286                 break;
287         case SND_SOC_DAIFMT_CBS_CFM:
288         case SND_SOC_DAIFMT_CBM_CFS:
289         default:
290                 debug("%s: Clock mode unsupported\n", __func__);
291                 return -1;
292         }
293
294         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
295         case SND_SOC_DAIFMT_I2S:
296                 regval |= M98095_DAI_DLY;
297                 break;
298         case SND_SOC_DAIFMT_LEFT_J:
299                 break;
300         default:
301                 debug("%s: Unrecognized format.\n", __func__);
302                 return -1;
303         }
304
305         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
306         case SND_SOC_DAIFMT_NB_NF:
307                 break;
308         case SND_SOC_DAIFMT_NB_IF:
309                 regval |= M98095_DAI_WCI;
310                 break;
311         case SND_SOC_DAIFMT_IB_NF:
312                 regval |= M98095_DAI_BCI;
313                 break;
314         case SND_SOC_DAIFMT_IB_IF:
315                 regval |= M98095_DAI_BCI | M98095_DAI_WCI;
316                 break;
317         default:
318                 debug("%s: Unrecognized inversion settings.\n", __func__);
319                 return -1;
320         }
321
322         error |= max98095_bic_or(priv, M98095_DAI_FORMAT,
323                                  M98095_DAI_MAS | M98095_DAI_DLY |
324                                  M98095_DAI_BCI | M98095_DAI_WCI, regval);
325
326         error |= max98095_i2c_write(priv, M98095_DAI_CLOCK, M98095_DAI_BSEL64);
327
328         if (error < 0) {
329                 debug("%s: Error setting i2s format.\n", __func__);
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 /*
337  * resets the audio codec
338  *
339  * @param priv  Private data for driver
340  * @return -1 for error and 0 success.
341  */
342 static int max98095_reset(struct max98095_priv *priv)
343 {
344         int i, ret;
345
346         /*
347          * Gracefully reset the DSP core and the codec hardware in a proper
348          * sequence.
349          */
350         ret = max98095_i2c_write(priv, M98095_00F_HOST_CFG, 0);
351         if (ret != 0) {
352                 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
353                 return ret;
354         }
355
356         ret = max98095_i2c_write(priv, M98095_097_PWR_SYS, 0);
357         if (ret != 0) {
358                 debug("%s: Failed to reset codec: %d\n", __func__, ret);
359                 return ret;
360         }
361
362         /*
363          * Reset to hardware default for registers, as there is not a soft
364          * reset hardware control register.
365          */
366         for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
367                 ret = max98095_i2c_write(priv, i, 0);
368                 if (ret < 0) {
369                         debug("%s: Failed to reset: %d\n", __func__, ret);
370                         return ret;
371                 }
372         }
373
374         return 0;
375 }
376
377 /*
378  * Intialise max98095 codec device
379  *
380  * @param priv          max98095 information
381  *
382  * @returns -1 for error  and 0 Success.
383  */
384 static int max98095_device_init(struct max98095_priv *priv)
385 {
386         unsigned char id;
387         int error = 0;
388
389         /* Enable codec clock */
390         set_xclkout();
391
392         /* reset the codec, the DSP core, and disable all interrupts */
393         error = max98095_reset(priv);
394         if (error != 0) {
395                 debug("Reset\n");
396                 return error;
397         }
398
399         /* initialize private data */
400         priv->sysclk = -1U;
401         priv->rate = -1U;
402         priv->fmt = -1U;
403
404         error = max98095_i2c_read(priv, M98095_0FF_REV_ID, &id);
405         if (error < 0) {
406                 debug("%s: Failure reading hardware revision: %d\n",
407                       __func__, id);
408                 return error;
409         }
410         debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
411
412         return 0;
413 }
414
415 static int max98095_setup_interface(struct max98095_priv *priv,
416                                     enum en_max_audio_interface aif_id)
417 {
418         int error;
419
420         error = max98095_i2c_write(priv, M98095_097_PWR_SYS, M98095_PWRSV);
421
422         /*
423          * initialize registers to hardware default configuring audio
424          * interface2 to DAC
425          */
426         if (aif_id == AIF1)
427                 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
428                                             M98095_DAI1L_TO_DACL |
429                                             M98095_DAI1R_TO_DACR);
430         else
431                 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
432                                             M98095_DAI2M_TO_DACL |
433                                             M98095_DAI2M_TO_DACR);
434
435         error |= max98095_i2c_write(priv, M98095_092_PWR_EN_OUT,
436                                     M98095_SPK_SPREADSPECTRUM);
437         error |= max98095_i2c_write(priv, M98095_04E_CFG_HP, M98095_HPNORMAL);
438         if (aif_id == AIF1)
439                 error |= max98095_i2c_write(priv, M98095_02C_DAI1_IOCFG,
440                                             M98095_S1NORMAL | M98095_SDATA);
441         else
442                 error |= max98095_i2c_write(priv, M98095_036_DAI2_IOCFG,
443                                             M98095_S2NORMAL | M98095_SDATA);
444
445         /* take the codec out of the shut down */
446         error |= max98095_bic_or(priv, M98095_097_PWR_SYS, M98095_SHDNRUN,
447                                  M98095_SHDNRUN);
448         /*
449          * route DACL and DACR output to HO and Speakers
450          * Ordering: DACL, DACR, DACL, DACR
451          */
452         error |= max98095_i2c_write(priv, M98095_050_MIX_SPK_LEFT, 0x01);
453         error |= max98095_i2c_write(priv, M98095_051_MIX_SPK_RIGHT, 0x01);
454         error |= max98095_i2c_write(priv, M98095_04C_MIX_HP_LEFT, 0x01);
455         error |= max98095_i2c_write(priv, M98095_04D_MIX_HP_RIGHT, 0x01);
456
457         /* power Enable */
458         error |= max98095_i2c_write(priv, M98095_091_PWR_EN_OUT, 0xF3);
459
460         /* set Volume */
461         error |= max98095_i2c_write(priv, M98095_064_LVL_HP_L, 15);
462         error |= max98095_i2c_write(priv, M98095_065_LVL_HP_R, 15);
463         error |= max98095_i2c_write(priv, M98095_067_LVL_SPK_L, 16);
464         error |= max98095_i2c_write(priv, M98095_068_LVL_SPK_R, 16);
465
466         /* Enable DAIs */
467         error |= max98095_i2c_write(priv, M98095_093_BIAS_CTRL, 0x30);
468         if (aif_id == AIF1)
469                 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x01);
470         else
471                 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x07);
472
473         if (error < 0)
474                 return -1;
475
476         return 0;
477 }
478
479 static int max98095_do_init(struct max98095_priv *priv,
480                             enum en_max_audio_interface aif_id,
481                             int sampling_rate, int mclk_freq,
482                             int bits_per_sample)
483 {
484         int ret = 0;
485
486         ret = max98095_setup_interface(priv, aif_id);
487         if (ret < 0) {
488                 debug("%s: max98095 codec chip init failed\n", __func__);
489                 return ret;
490         }
491
492         ret = max98095_set_sysclk(priv, mclk_freq);
493         if (ret < 0) {
494                 debug("%s: max98095 codec set sys clock failed\n", __func__);
495                 return ret;
496         }
497
498         ret = max98095_hw_params(priv, aif_id, sampling_rate,
499                                  bits_per_sample);
500
501         if (ret == 0) {
502                 ret = max98095_set_fmt(priv, SND_SOC_DAIFMT_I2S |
503                                        SND_SOC_DAIFMT_NB_NF |
504                                        SND_SOC_DAIFMT_CBS_CFS,
505                                        aif_id);
506         }
507
508         return ret;
509 }
510
511 static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
512                                 const void *blob)
513 {
514         int error = 0;
515         enum fdt_compat_id compat;
516         int node;
517         int parent;
518
519         /* Get the node from FDT for codec */
520         node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC);
521         if (node <= 0) {
522                 debug("EXYNOS_SOUND: No node for codec in device tree\n");
523                 debug("node = %d\n", node);
524                 return -1;
525         }
526
527         parent = fdt_parent_offset(blob, node);
528         if (parent < 0) {
529                 debug("%s: Cannot find node parent\n", __func__);
530                 return -1;
531         }
532
533         compat = fdtdec_lookup(blob, parent);
534         switch (compat) {
535         case COMPAT_SAMSUNG_S3C2440_I2C:
536                 pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent);
537                 error |= pcodec_info->i2c_bus;
538                 debug("i2c bus = %d\n", pcodec_info->i2c_bus);
539                 pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node,
540                                                         "reg", 0);
541                 error |= pcodec_info->i2c_dev_addr;
542                 debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr);
543                 break;
544         default:
545                 debug("%s: Unknown compat id %d\n", __func__, compat);
546                 return -1;
547         }
548         if (error == -1) {
549                 debug("fail to get max98095 codec node properties\n");
550                 return -1;
551         }
552
553         return 0;
554 }
555
556 /* max98095 Device Initialisation */
557 int max98095_init(const void *blob, enum en_max_audio_interface aif_id,
558                   int sampling_rate, int mclk_freq,
559                   int bits_per_sample)
560 {
561         int ret;
562         int old_bus = i2c_get_bus_num();
563         struct sound_codec_info pcodec_info;
564         struct max98095_priv max98095_info;
565
566         if (get_max98095_codec_values(&pcodec_info, blob) < 0) {
567                 debug("FDT Codec values failed\n");
568                 return -1;
569         }
570
571         i2c_set_bus_num(pcodec_info.i2c_bus);
572
573         /* shift the device address by 1 for 7 bit addressing */
574         max98095_info.i2c_addr = pcodec_info.i2c_dev_addr >> 1;
575         ret = max98095_device_init(&max98095_info);
576         if (ret < 0) {
577                 debug("%s: max98095 codec chip init failed\n", __func__);
578                 return ret;
579         }
580
581         ret = max98095_do_init(&max98095_info, aif_id, sampling_rate, mclk_freq,
582                                bits_per_sample);
583         i2c_set_bus_num(old_bus);
584
585         return ret;
586 }