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