2 * max98095.c -- MAX98095 ALSA SoC Audio driver
4 * Copyright 2011 Maxim Integrated Products
6 * Modified for uboot by R. Chandrasekar (rcsekar@samsung.com)
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.
14 #include <asm/arch/clk.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/power.h>
31 struct max98095_priv {
32 enum max98095_type devtype;
38 static struct sound_codec_info g_codec_info;
39 struct max98095_priv g_max98095_info;
40 unsigned int g_max98095_i2c_dev_addr;
42 /* Index 0 is reserved. */
43 int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
47 * Writes value to a device register through i2c
49 * @param reg reg number to be write
50 * @param data data to be writen to the above registor
52 * @return int value 1 for change, 0 for no change or negative error code.
54 static int max98095_i2c_write(unsigned int reg, unsigned char data)
56 debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n",
58 return i2c_write(g_max98095_i2c_dev_addr, reg, 1, &data, 1);
62 * Read a value from a device register through i2c
64 * @param reg reg number to be read
65 * @param data address of read data to be stored
67 * @return int value 0 for success, -1 in case of error.
69 static unsigned int max98095_i2c_read(unsigned int reg, unsigned char *data)
73 ret = i2c_read(g_max98095_i2c_dev_addr, reg, 1, data, 1);
75 debug("%s: Error while reading register %#04x\n",
84 * update device register bits through i2c
86 * @param reg codec register
87 * @param mask register mask
88 * @param value new value
90 * @return int value 0 for success, non-zero error code.
92 static int max98095_update_bits(unsigned int reg, unsigned char mask,
96 unsigned char old, new;
98 if (max98095_i2c_read(reg, &old) != 0)
100 new = (old & ~mask) | (value & mask);
101 change = (old != new) ? 1 : 0;
103 ret = max98095_i2c_write(reg, new);
111 * codec mclk clock divider coefficients based on sampling rate
113 * @param rate sampling rate
114 * @param value address of indexvalue to be stored
116 * @return 0 for success or negative error code.
118 static int rate_value(int rate, u8 *value)
122 for (i = 1; i < ARRAY_SIZE(rate_table); i++) {
123 if (rate_table[i] >= rate) {
134 * Sets hw params for max98095
136 * @param max98095 max98095 information pointer
137 * @param rate Sampling rate
138 * @param bits_per_sample Bits per sample
140 * @return -1 for error and 0 Success.
142 static int max98095_hw_params(struct max98095_priv *max98095,
143 enum en_max_audio_interface aif_id,
144 unsigned int rate, unsigned int bits_per_sample)
148 unsigned short M98095_DAI_CLKMODE;
149 unsigned short M98095_DAI_FORMAT;
150 unsigned short M98095_DAI_FILTERS;
152 if (aif_id == AIF1) {
153 M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE;
154 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
155 M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS;
157 M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE;
158 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
159 M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS;
162 switch (bits_per_sample) {
164 error = max98095_update_bits(M98095_DAI_FORMAT,
168 error = max98095_update_bits(M98095_DAI_FORMAT,
169 M98095_DAI_WS, M98095_DAI_WS);
172 debug("%s: Illegal bits per sample %d.\n",
173 __func__, bits_per_sample);
177 if (rate_value(rate, ®val)) {
178 debug("%s: Failed to set sample rate to %d.\n",
182 max98095->rate = rate;
184 error |= max98095_update_bits(M98095_DAI_CLKMODE,
185 M98095_CLKMODE_MASK, regval);
187 /* Update sample rate mode */
189 error |= max98095_update_bits(M98095_DAI_FILTERS,
192 error |= max98095_update_bits(M98095_DAI_FILTERS,
193 M98095_DAI_DHF, M98095_DAI_DHF);
196 debug("%s: Error setting hardware params.\n", __func__);
204 * Configures Audio interface system clock for the given frequency
206 * @param max98095 max98095 information
207 * @param freq Sampling frequency in Hz
209 * @return -1 for error and 0 success.
211 static int max98095_set_sysclk(struct max98095_priv *max98095,
216 /* Requested clock frequency is already setup */
217 if (freq == max98095->sysclk)
220 /* Setup clocks for slave mode, and using the PLL
221 * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
222 * 0x02 (when master clk is 20MHz to 40MHz)..
223 * 0x03 (when master clk is 40MHz to 60MHz)..
225 if ((freq >= 10000000) && (freq < 20000000)) {
226 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x10);
227 } else if ((freq >= 20000000) && (freq < 40000000)) {
228 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x20);
229 } else if ((freq >= 40000000) && (freq < 60000000)) {
230 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x30);
232 debug("%s: Invalid master clock frequency\n", __func__);
236 debug("%s: Clock at %uHz\n", __func__, freq);
241 max98095->sysclk = freq;
246 * Sets Max98095 I2S format
248 * @param max98095 max98095 information
249 * @param fmt i2S format - supports a subset of the options defined
252 * @return -1 for error and 0 Success.
254 static int max98095_set_fmt(struct max98095_priv *max98095, int fmt,
255 enum en_max_audio_interface aif_id)
259 unsigned short M98095_DAI_CLKCFG_HI;
260 unsigned short M98095_DAI_CLKCFG_LO;
261 unsigned short M98095_DAI_FORMAT;
262 unsigned short M98095_DAI_CLOCK;
264 if (fmt == max98095->fmt)
269 if (aif_id == AIF1) {
270 M98095_DAI_CLKCFG_HI = M98095_028_DAI1_CLKCFG_HI;
271 M98095_DAI_CLKCFG_LO = M98095_029_DAI1_CLKCFG_LO;
272 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
273 M98095_DAI_CLOCK = M98095_02B_DAI1_CLOCK;
275 M98095_DAI_CLKCFG_HI = M98095_032_DAI2_CLKCFG_HI;
276 M98095_DAI_CLKCFG_LO = M98095_033_DAI2_CLKCFG_LO;
277 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
278 M98095_DAI_CLOCK = M98095_035_DAI2_CLOCK;
281 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
282 case SND_SOC_DAIFMT_CBS_CFS:
284 error |= max98095_i2c_write(M98095_DAI_CLKCFG_HI,
286 error |= max98095_i2c_write(M98095_DAI_CLKCFG_LO,
289 case SND_SOC_DAIFMT_CBM_CFM:
290 /* Set to master mode */
291 regval |= M98095_DAI_MAS;
293 case SND_SOC_DAIFMT_CBS_CFM:
294 case SND_SOC_DAIFMT_CBM_CFS:
296 debug("%s: Clock mode unsupported\n", __func__);
300 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
301 case SND_SOC_DAIFMT_I2S:
302 regval |= M98095_DAI_DLY;
304 case SND_SOC_DAIFMT_LEFT_J:
307 debug("%s: Unrecognized format.\n", __func__);
311 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
312 case SND_SOC_DAIFMT_NB_NF:
314 case SND_SOC_DAIFMT_NB_IF:
315 regval |= M98095_DAI_WCI;
317 case SND_SOC_DAIFMT_IB_NF:
318 regval |= M98095_DAI_BCI;
320 case SND_SOC_DAIFMT_IB_IF:
321 regval |= M98095_DAI_BCI | M98095_DAI_WCI;
324 debug("%s: Unrecognized inversion settings.\n", __func__);
328 error |= max98095_update_bits(M98095_DAI_FORMAT,
329 M98095_DAI_MAS | M98095_DAI_DLY |
330 M98095_DAI_BCI | M98095_DAI_WCI,
333 error |= max98095_i2c_write(M98095_DAI_CLOCK,
337 debug("%s: Error setting i2s format.\n", __func__);
345 * resets the audio codec
347 * @return -1 for error and 0 success.
349 static int max98095_reset(void)
354 * Gracefully reset the DSP core and the codec hardware in a proper
357 ret = max98095_i2c_write(M98095_00F_HOST_CFG, 0);
359 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
363 ret = max98095_i2c_write(M98095_097_PWR_SYS, 0);
365 debug("%s: Failed to reset codec: %d\n", __func__, ret);
370 * Reset to hardware default for registers, as there is not a soft
371 * reset hardware control register.
373 for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
374 ret = max98095_i2c_write(i, 0);
376 debug("%s: Failed to reset: %d\n", __func__, ret);
385 * Intialise max98095 codec device
387 * @param max98095 max98095 information
389 * @returns -1 for error and 0 Success.
391 static int max98095_device_init(struct max98095_priv *max98095,
392 enum en_max_audio_interface aif_id)
397 /* reset the codec, the DSP core, and disable all interrupts */
398 error = max98095_reset();
404 /* initialize private data */
405 max98095->sysclk = -1U;
406 max98095->rate = -1U;
409 error = max98095_i2c_read(M98095_0FF_REV_ID, &id);
411 debug("%s: Failure reading hardware revision: %d\n",
415 debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
417 error |= max98095_i2c_write(M98095_097_PWR_SYS, M98095_PWRSV);
420 * initialize registers to hardware default configuring audio
424 error |= max98095_i2c_write(M98095_048_MIX_DAC_LR,
425 M98095_DAI1L_TO_DACL |
426 M98095_DAI1R_TO_DACR);
428 error |= max98095_i2c_write(M98095_048_MIX_DAC_LR,
429 M98095_DAI2M_TO_DACL |
430 M98095_DAI2M_TO_DACR);
432 error |= max98095_i2c_write(M98095_092_PWR_EN_OUT,
433 M98095_SPK_SPREADSPECTRUM);
434 error |= max98095_i2c_write(M98095_04E_CFG_HP, M98095_HPNORMAL);
436 error |= max98095_i2c_write(M98095_02C_DAI1_IOCFG,
437 M98095_S1NORMAL | M98095_SDATA);
439 error |= max98095_i2c_write(M98095_036_DAI2_IOCFG,
440 M98095_S2NORMAL | M98095_SDATA);
442 /* take the codec out of the shut down */
443 error |= max98095_update_bits(M98095_097_PWR_SYS, M98095_SHDNRUN,
445 /* route DACL and DACR output to HO and Spekers */
446 error |= max98095_i2c_write(M98095_050_MIX_SPK_LEFT, 0x01); /* DACL */
447 error |= max98095_i2c_write(M98095_051_MIX_SPK_RIGHT, 0x01);/* DACR */
448 error |= max98095_i2c_write(M98095_04C_MIX_HP_LEFT, 0x01); /* DACL */
449 error |= max98095_i2c_write(M98095_04D_MIX_HP_RIGHT, 0x01); /* DACR */
452 error |= max98095_i2c_write(M98095_091_PWR_EN_OUT, 0xF3);
455 error |= max98095_i2c_write(M98095_064_LVL_HP_L, 15);
456 error |= max98095_i2c_write(M98095_065_LVL_HP_R, 15);
457 error |= max98095_i2c_write(M98095_067_LVL_SPK_L, 16);
458 error |= max98095_i2c_write(M98095_068_LVL_SPK_R, 16);
461 error |= max98095_i2c_write(M98095_093_BIAS_CTRL, 0x30);
463 error |= max98095_i2c_write(M98095_096_PWR_DAC_CK, 0x01);
465 error |= max98095_i2c_write(M98095_096_PWR_DAC_CK, 0x07);
474 static int max98095_do_init(struct sound_codec_info *pcodec_info,
475 enum en_max_audio_interface aif_id,
476 int sampling_rate, int mclk_freq,
481 /* Enable codec clock */
484 /* shift the device address by 1 for 7 bit addressing */
485 g_max98095_i2c_dev_addr = pcodec_info->i2c_dev_addr >> 1;
487 if (pcodec_info->codec_type == CODEC_MAX_98095) {
488 g_max98095_info.devtype = MAX98095;
490 debug("%s: Codec id [%d] not defined\n", __func__,
491 pcodec_info->codec_type);
495 ret = max98095_device_init(&g_max98095_info, aif_id);
497 debug("%s: max98095 codec chip init failed\n", __func__);
501 ret = max98095_set_sysclk(&g_max98095_info, mclk_freq);
503 debug("%s: max98095 codec set sys clock failed\n", __func__);
507 ret = max98095_hw_params(&g_max98095_info, aif_id, sampling_rate,
511 ret = max98095_set_fmt(&g_max98095_info,
513 SND_SOC_DAIFMT_NB_NF |
514 SND_SOC_DAIFMT_CBS_CFS,
521 static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
525 #if CONFIG_IS_ENABLED(OF_CONTROL)
526 enum fdt_compat_id compat;
530 /* Get the node from FDT for codec */
531 node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC);
533 debug("EXYNOS_SOUND: No node for codec in device tree\n");
534 debug("node = %d\n", node);
538 parent = fdt_parent_offset(blob, node);
540 debug("%s: Cannot find node parent\n", __func__);
544 compat = fdtdec_lookup(blob, parent);
546 case COMPAT_SAMSUNG_S3C2440_I2C:
547 pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent);
548 error |= pcodec_info->i2c_bus;
549 debug("i2c bus = %d\n", pcodec_info->i2c_bus);
550 pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node,
552 error |= pcodec_info->i2c_dev_addr;
553 debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr);
556 debug("%s: Unknown compat id %d\n", __func__, compat);
560 pcodec_info->i2c_bus = AUDIO_I2C_BUS;
561 pcodec_info->i2c_dev_addr = AUDIO_I2C_REG;
562 debug("i2c dev addr = %d\n", pcodec_info->i2c_dev_addr);
564 pcodec_info->codec_type = CODEC_MAX_98095;
566 debug("fail to get max98095 codec node properties\n");
573 /* max98095 Device Initialisation */
574 int max98095_init(const void *blob, enum en_max_audio_interface aif_id,
575 int sampling_rate, int mclk_freq,
579 int old_bus = i2c_get_bus_num();
580 struct sound_codec_info *pcodec_info = &g_codec_info;
582 if (get_max98095_codec_values(pcodec_info, blob) < 0) {
583 debug("FDT Codec values failed\n");
587 i2c_set_bus_num(pcodec_info->i2c_bus);
588 ret = max98095_do_init(pcodec_info, aif_id, sampling_rate, mclk_freq,
590 i2c_set_bus_num(old_bus);