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.
12 #include <asm/arch/clk.h>
13 #include <asm/arch/cpu.h>
14 #include <asm/arch/power.h>
29 struct max98095_priv {
30 enum max98095_type devtype;
36 static struct sound_codec_info g_codec_info;
37 struct max98095_priv g_max98095_info;
38 unsigned int g_max98095_i2c_dev_addr;
40 /* Index 0 is reserved. */
41 int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
45 * Writes value to a device register through i2c
47 * @param reg reg number to be write
48 * @param data data to be writen to the above registor
50 * @return int value 1 for change, 0 for no change or negative error code.
52 static int max98095_i2c_write(unsigned int reg, unsigned char data)
54 debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n",
56 return i2c_write(g_max98095_i2c_dev_addr, reg, 1, &data, 1);
60 * Read a value from a device register through i2c
62 * @param reg reg number to be read
63 * @param data address of read data to be stored
65 * @return int value 0 for success, -1 in case of error.
67 static unsigned int max98095_i2c_read(unsigned int reg, unsigned char *data)
71 ret = i2c_read(g_max98095_i2c_dev_addr, reg, 1, data, 1);
73 debug("%s: Error while reading register %#04x\n",
82 * update device register bits through i2c
84 * @param reg codec register
85 * @param mask register mask
86 * @param value new value
88 * @return int value 0 for success, non-zero error code.
90 static int max98095_update_bits(unsigned int reg, unsigned char mask,
94 unsigned char old, new;
96 if (max98095_i2c_read(reg, &old) != 0)
98 new = (old & ~mask) | (value & mask);
99 change = (old != new) ? 1 : 0;
101 ret = max98095_i2c_write(reg, new);
109 * codec mclk clock divider coefficients based on sampling rate
111 * @param rate sampling rate
112 * @param value address of indexvalue to be stored
114 * @return 0 for success or negative error code.
116 static int rate_value(int rate, u8 *value)
120 for (i = 1; i < ARRAY_SIZE(rate_table); i++) {
121 if (rate_table[i] >= rate) {
132 * Sets hw params for max98095
134 * @param max98095 max98095 information pointer
135 * @param rate Sampling rate
136 * @param bits_per_sample Bits per sample
138 * @return -1 for error and 0 Success.
140 static int max98095_hw_params(struct max98095_priv *max98095,
141 enum en_max_audio_interface aif_id,
142 unsigned int rate, unsigned int bits_per_sample)
146 unsigned short M98095_DAI_CLKMODE;
147 unsigned short M98095_DAI_FORMAT;
148 unsigned short M98095_DAI_FILTERS;
150 if (aif_id == AIF1) {
151 M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE;
152 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
153 M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS;
155 M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE;
156 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
157 M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS;
160 switch (bits_per_sample) {
162 error = max98095_update_bits(M98095_DAI_FORMAT,
166 error = max98095_update_bits(M98095_DAI_FORMAT,
167 M98095_DAI_WS, M98095_DAI_WS);
170 debug("%s: Illegal bits per sample %d.\n",
171 __func__, bits_per_sample);
175 if (rate_value(rate, ®val)) {
176 debug("%s: Failed to set sample rate to %d.\n",
180 max98095->rate = rate;
182 error |= max98095_update_bits(M98095_DAI_CLKMODE,
183 M98095_CLKMODE_MASK, regval);
185 /* Update sample rate mode */
187 error |= max98095_update_bits(M98095_DAI_FILTERS,
190 error |= max98095_update_bits(M98095_DAI_FILTERS,
191 M98095_DAI_DHF, M98095_DAI_DHF);
194 debug("%s: Error setting hardware params.\n", __func__);
202 * Configures Audio interface system clock for the given frequency
204 * @param max98095 max98095 information
205 * @param freq Sampling frequency in Hz
207 * @return -1 for error and 0 success.
209 static int max98095_set_sysclk(struct max98095_priv *max98095,
214 /* Requested clock frequency is already setup */
215 if (freq == max98095->sysclk)
218 /* Setup clocks for slave mode, and using the PLL
219 * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
220 * 0x02 (when master clk is 20MHz to 40MHz)..
221 * 0x03 (when master clk is 40MHz to 60MHz)..
223 if ((freq >= 10000000) && (freq < 20000000)) {
224 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x10);
225 } else if ((freq >= 20000000) && (freq < 40000000)) {
226 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x20);
227 } else if ((freq >= 40000000) && (freq < 60000000)) {
228 error = max98095_i2c_write(M98095_026_SYS_CLK, 0x30);
230 debug("%s: Invalid master clock frequency\n", __func__);
234 debug("%s: Clock at %uHz\n", __func__, freq);
239 max98095->sysclk = freq;
244 * Sets Max98095 I2S format
246 * @param max98095 max98095 information
247 * @param fmt i2S format - supports a subset of the options defined
250 * @return -1 for error and 0 Success.
252 static int max98095_set_fmt(struct max98095_priv *max98095, int fmt,
253 enum en_max_audio_interface aif_id)
257 unsigned short M98095_DAI_CLKCFG_HI;
258 unsigned short M98095_DAI_CLKCFG_LO;
259 unsigned short M98095_DAI_FORMAT;
260 unsigned short M98095_DAI_CLOCK;
262 if (fmt == max98095->fmt)
267 if (aif_id == AIF1) {
268 M98095_DAI_CLKCFG_HI = M98095_028_DAI1_CLKCFG_HI;
269 M98095_DAI_CLKCFG_LO = M98095_029_DAI1_CLKCFG_LO;
270 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
271 M98095_DAI_CLOCK = M98095_02B_DAI1_CLOCK;
273 M98095_DAI_CLKCFG_HI = M98095_032_DAI2_CLKCFG_HI;
274 M98095_DAI_CLKCFG_LO = M98095_033_DAI2_CLKCFG_LO;
275 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
276 M98095_DAI_CLOCK = M98095_035_DAI2_CLOCK;
279 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
280 case SND_SOC_DAIFMT_CBS_CFS:
282 error |= max98095_i2c_write(M98095_DAI_CLKCFG_HI,
284 error |= max98095_i2c_write(M98095_DAI_CLKCFG_LO,
287 case SND_SOC_DAIFMT_CBM_CFM:
288 /* Set to master mode */
289 regval |= M98095_DAI_MAS;
291 case SND_SOC_DAIFMT_CBS_CFM:
292 case SND_SOC_DAIFMT_CBM_CFS:
294 debug("%s: Clock mode unsupported\n", __func__);
298 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
299 case SND_SOC_DAIFMT_I2S:
300 regval |= M98095_DAI_DLY;
302 case SND_SOC_DAIFMT_LEFT_J:
305 debug("%s: Unrecognized format.\n", __func__);
309 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
310 case SND_SOC_DAIFMT_NB_NF:
312 case SND_SOC_DAIFMT_NB_IF:
313 regval |= M98095_DAI_WCI;
315 case SND_SOC_DAIFMT_IB_NF:
316 regval |= M98095_DAI_BCI;
318 case SND_SOC_DAIFMT_IB_IF:
319 regval |= M98095_DAI_BCI | M98095_DAI_WCI;
322 debug("%s: Unrecognized inversion settings.\n", __func__);
326 error |= max98095_update_bits(M98095_DAI_FORMAT,
327 M98095_DAI_MAS | M98095_DAI_DLY |
328 M98095_DAI_BCI | M98095_DAI_WCI,
331 error |= max98095_i2c_write(M98095_DAI_CLOCK,
335 debug("%s: Error setting i2s format.\n", __func__);
343 * resets the audio codec
345 * @return -1 for error and 0 success.
347 static int max98095_reset(void)
352 * Gracefully reset the DSP core and the codec hardware in a proper
355 ret = max98095_i2c_write(M98095_00F_HOST_CFG, 0);
357 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
361 ret = max98095_i2c_write(M98095_097_PWR_SYS, 0);
363 debug("%s: Failed to reset codec: %d\n", __func__, ret);
368 * Reset to hardware default for registers, as there is not a soft
369 * reset hardware control register.
371 for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
372 ret = max98095_i2c_write(i, 0);
374 debug("%s: Failed to reset: %d\n", __func__, ret);
383 * Intialise max98095 codec device
385 * @param max98095 max98095 information
387 * @returns -1 for error and 0 Success.
389 static int max98095_device_init(struct max98095_priv *max98095,
390 enum en_max_audio_interface aif_id)
395 /* reset the codec, the DSP core, and disable all interrupts */
396 error = max98095_reset();
402 /* initialize private data */
403 max98095->sysclk = -1U;
404 max98095->rate = -1U;
407 error = max98095_i2c_read(M98095_0FF_REV_ID, &id);
409 debug("%s: Failure reading hardware revision: %d\n",
413 debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
415 error |= max98095_i2c_write(M98095_097_PWR_SYS, M98095_PWRSV);
418 * initialize registers to hardware default configuring audio
422 error |= max98095_i2c_write(M98095_048_MIX_DAC_LR,
423 M98095_DAI1L_TO_DACL |
424 M98095_DAI1R_TO_DACR);
426 error |= max98095_i2c_write(M98095_048_MIX_DAC_LR,
427 M98095_DAI2M_TO_DACL |
428 M98095_DAI2M_TO_DACR);
430 error |= max98095_i2c_write(M98095_092_PWR_EN_OUT,
431 M98095_SPK_SPREADSPECTRUM);
432 error |= max98095_i2c_write(M98095_04E_CFG_HP, M98095_HPNORMAL);
434 error |= max98095_i2c_write(M98095_02C_DAI1_IOCFG,
435 M98095_S1NORMAL | M98095_SDATA);
437 error |= max98095_i2c_write(M98095_036_DAI2_IOCFG,
438 M98095_S2NORMAL | M98095_SDATA);
440 /* take the codec out of the shut down */
441 error |= max98095_update_bits(M98095_097_PWR_SYS, M98095_SHDNRUN,
443 /* route DACL and DACR output to HO and Spekers */
444 error |= max98095_i2c_write(M98095_050_MIX_SPK_LEFT, 0x01); /* DACL */
445 error |= max98095_i2c_write(M98095_051_MIX_SPK_RIGHT, 0x01);/* DACR */
446 error |= max98095_i2c_write(M98095_04C_MIX_HP_LEFT, 0x01); /* DACL */
447 error |= max98095_i2c_write(M98095_04D_MIX_HP_RIGHT, 0x01); /* DACR */
450 error |= max98095_i2c_write(M98095_091_PWR_EN_OUT, 0xF3);
453 error |= max98095_i2c_write(M98095_064_LVL_HP_L, 15);
454 error |= max98095_i2c_write(M98095_065_LVL_HP_R, 15);
455 error |= max98095_i2c_write(M98095_067_LVL_SPK_L, 16);
456 error |= max98095_i2c_write(M98095_068_LVL_SPK_R, 16);
459 error |= max98095_i2c_write(M98095_093_BIAS_CTRL, 0x30);
461 error |= max98095_i2c_write(M98095_096_PWR_DAC_CK, 0x01);
463 error |= max98095_i2c_write(M98095_096_PWR_DAC_CK, 0x07);
472 static int max98095_do_init(struct sound_codec_info *pcodec_info,
473 enum en_max_audio_interface aif_id,
474 int sampling_rate, int mclk_freq,
479 /* Enable codec clock */
482 /* shift the device address by 1 for 7 bit addressing */
483 g_max98095_i2c_dev_addr = pcodec_info->i2c_dev_addr >> 1;
485 if (pcodec_info->codec_type == CODEC_MAX_98095) {
486 g_max98095_info.devtype = MAX98095;
488 debug("%s: Codec id [%d] not defined\n", __func__,
489 pcodec_info->codec_type);
493 ret = max98095_device_init(&g_max98095_info, aif_id);
495 debug("%s: max98095 codec chip init failed\n", __func__);
499 ret = max98095_set_sysclk(&g_max98095_info, mclk_freq);
501 debug("%s: max98095 codec set sys clock failed\n", __func__);
505 ret = max98095_hw_params(&g_max98095_info, aif_id, sampling_rate,
509 ret = max98095_set_fmt(&g_max98095_info,
511 SND_SOC_DAIFMT_NB_NF |
512 SND_SOC_DAIFMT_CBS_CFS,
519 static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
523 #ifdef CONFIG_OF_CONTROL
524 enum fdt_compat_id compat;
528 /* Get the node from FDT for codec */
529 node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC);
531 debug("EXYNOS_SOUND: No node for codec in device tree\n");
532 debug("node = %d\n", node);
536 parent = fdt_parent_offset(blob, node);
538 debug("%s: Cannot find node parent\n", __func__);
542 compat = fdtdec_lookup(blob, parent);
544 case COMPAT_SAMSUNG_S3C2440_I2C:
545 pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent);
546 error |= pcodec_info->i2c_bus;
547 debug("i2c bus = %d\n", pcodec_info->i2c_bus);
548 pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node,
550 error |= pcodec_info->i2c_dev_addr;
551 debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr);
554 debug("%s: Unknown compat id %d\n", __func__, compat);
558 pcodec_info->i2c_bus = AUDIO_I2C_BUS;
559 pcodec_info->i2c_dev_addr = AUDIO_I2C_REG;
560 debug("i2c dev addr = %d\n", pcodec_info->i2c_dev_addr);
562 pcodec_info->codec_type = CODEC_MAX_98095;
564 debug("fail to get max98095 codec node properties\n");
571 /* max98095 Device Initialisation */
572 int max98095_init(const void *blob, enum en_max_audio_interface aif_id,
573 int sampling_rate, int mclk_freq,
577 int old_bus = i2c_get_bus_num();
578 struct sound_codec_info *pcodec_info = &g_codec_info;
580 if (get_max98095_codec_values(pcodec_info, blob) < 0) {
581 debug("FDT Codec values failed\n");
585 i2c_set_bus_num(pcodec_info->i2c_bus);
586 ret = max98095_do_init(pcodec_info, aif_id, sampling_rate, mclk_freq,
588 i2c_set_bus_num(old_bus);