blob: 6a98dac04bba48c39520287c98efb46ddfc9dc75 [file] [log] [blame]
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +00001/*
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 */
Simon Glass1d9af1f2017-05-30 21:47:09 -060012
13#include <common.h>
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000014#include <div64.h>
15#include <fdtdec.h>
16#include <i2c.h>
17#include <sound.h>
Simon Glassf2222362018-12-03 04:37:34 -070018#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>
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000023#include "i2s.h"
24#include "max98095.h"
25
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000026struct max98095_priv {
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000027 unsigned int sysclk;
28 unsigned int rate;
29 unsigned int fmt;
Simon Glass77c04342018-12-03 04:37:30 -070030 int i2c_addr;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000031};
32
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000033/* Index 0 is reserved. */
34int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
35 88200, 96000};
36
37/*
38 * Writes value to a device register through i2c
39 *
Simon Glassfd7d6972018-12-03 04:37:25 -070040 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000041 * @param reg reg number to be write
42 * @param data data to be writen to the above registor
43 *
44 * @return int value 1 for change, 0 for no change or negative error code.
45 */
Simon Glassfd7d6972018-12-03 04:37:25 -070046static int max98095_i2c_write(struct max98095_priv *priv, unsigned int reg,
47 unsigned char data)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000048{
49 debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +053050 __func__, reg, data);
Simon Glass77c04342018-12-03 04:37:30 -070051 return i2c_write(priv->i2c_addr, reg, 1, &data, 1);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000052}
53
54/*
55 * Read a value from a device register through i2c
56 *
Simon Glassfd7d6972018-12-03 04:37:25 -070057 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000058 * @param reg reg number to be read
59 * @param data address of read data to be stored
60 *
61 * @return int value 0 for success, -1 in case of error.
62 */
Simon Glassfd7d6972018-12-03 04:37:25 -070063static unsigned int max98095_i2c_read(struct max98095_priv *priv,
64 unsigned int reg, unsigned char *data)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000065{
66 int ret;
67
Simon Glass77c04342018-12-03 04:37:30 -070068 ret = i2c_read(priv->i2c_addr, reg, 1, data, 1);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000069 if (ret != 0) {
70 debug("%s: Error while reading register %#04x\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +053071 __func__, reg);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000072 return -1;
73 }
74
75 return 0;
76}
77
78/*
79 * update device register bits through i2c
80 *
Simon Glassfd7d6972018-12-03 04:37:25 -070081 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000082 * @param reg codec register
83 * @param mask register mask
84 * @param value new value
85 *
86 * @return int value 0 for success, non-zero error code.
87 */
Simon Glassfd7d6972018-12-03 04:37:25 -070088static int max98095_bic_or(struct max98095_priv *priv, unsigned int reg,
89 unsigned char mask, unsigned char value)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000090{
91 int change, ret = 0;
92 unsigned char old, new;
93
Simon Glassfd7d6972018-12-03 04:37:25 -070094 if (max98095_i2c_read(priv, reg, &old) != 0)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000095 return -1;
96 new = (old & ~mask) | (value & mask);
97 change = (old != new) ? 1 : 0;
98 if (change)
Simon Glassfd7d6972018-12-03 04:37:25 -070099 ret = max98095_i2c_write(priv, reg, new);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000100 if (ret < 0)
101 return ret;
102
103 return change;
104}
105
106/*
107 * codec mclk clock divider coefficients based on sampling rate
108 *
109 * @param rate sampling rate
110 * @param value address of indexvalue to be stored
111 *
112 * @return 0 for success or negative error code.
113 */
114static int rate_value(int rate, u8 *value)
115{
116 int i;
117
118 for (i = 1; i < ARRAY_SIZE(rate_table); i++) {
119 if (rate_table[i] >= rate) {
120 *value = i;
121 return 0;
122 }
123 }
124 *value = 1;
125
126 return -1;
127}
128
129/*
130 * Sets hw params for max98095
131 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700132 * @param priv max98095 information pointer
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000133 * @param rate Sampling rate
134 * @param bits_per_sample Bits per sample
135 *
136 * @return -1 for error and 0 Success.
137 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700138static int max98095_hw_params(struct max98095_priv *priv,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530139 enum en_max_audio_interface aif_id,
140 unsigned int rate, unsigned int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000141{
142 u8 regval;
143 int error;
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530144 unsigned short M98095_DAI_CLKMODE;
145 unsigned short M98095_DAI_FORMAT;
146 unsigned short M98095_DAI_FILTERS;
147
148 if (aif_id == AIF1) {
149 M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE;
150 M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT;
151 M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS;
152 } else {
153 M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE;
154 M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT;
155 M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS;
156 }
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000157
158 switch (bits_per_sample) {
159 case 16:
Simon Glassfd7d6972018-12-03 04:37:25 -0700160 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
161 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000162 break;
163 case 24:
Simon Glassfd7d6972018-12-03 04:37:25 -0700164 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
165 M98095_DAI_WS);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000166 break;
167 default:
168 debug("%s: Illegal bits per sample %d.\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530169 __func__, bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000170 return -1;
171 }
172
173 if (rate_value(rate, &regval)) {
174 debug("%s: Failed to set sample rate to %d.\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530175 __func__, rate);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000176 return -1;
177 }
Simon Glassfd7d6972018-12-03 04:37:25 -0700178 priv->rate = rate;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000179
Simon Glassfd7d6972018-12-03 04:37:25 -0700180 error |= max98095_bic_or(priv, M98095_DAI_CLKMODE, M98095_CLKMODE_MASK,
181 regval);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000182
183 /* Update sample rate mode */
184 if (rate < 50000)
Simon Glassfd7d6972018-12-03 04:37:25 -0700185 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
186 M98095_DAI_DHF, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000187 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700188 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
189 M98095_DAI_DHF, M98095_DAI_DHF);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000190
191 if (error < 0) {
192 debug("%s: Error setting hardware params.\n", __func__);
193 return -1;
194 }
195
196 return 0;
197}
198
199/*
200 * Configures Audio interface system clock for the given frequency
201 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700202 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000203 * @param freq Sampling frequency in Hz
204 *
205 * @return -1 for error and 0 success.
206 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700207static int max98095_set_sysclk(struct max98095_priv *priv, unsigned int freq)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000208{
209 int error = 0;
210
211 /* Requested clock frequency is already setup */
Simon Glassfd7d6972018-12-03 04:37:25 -0700212 if (freq == priv->sysclk)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000213 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)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700221 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x10);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000222 } else if ((freq >= 20000000) && (freq < 40000000)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700223 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x20);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000224 } else if ((freq >= 40000000) && (freq < 60000000)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700225 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x30);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000226 } 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
Simon Glassfd7d6972018-12-03 04:37:25 -0700236 priv->sysclk = freq;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000237 return 0;
238}
239
240/*
241 * Sets Max98095 I2S format
242 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700243 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000244 * @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 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700249static int max98095_set_fmt(struct max98095_priv *priv, int fmt,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530250 enum en_max_audio_interface aif_id)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000251{
252 u8 regval = 0;
253 int error = 0;
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530254 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;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000258
Simon Glassfd7d6972018-12-03 04:37:25 -0700259 if (fmt == priv->fmt)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000260 return 0;
261
Simon Glassfd7d6972018-12-03 04:37:25 -0700262 priv->fmt = fmt;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000263
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530264 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
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000276 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
277 case SND_SOC_DAIFMT_CBS_CFS:
278 /* Slave mode PLL */
Simon Glassfd7d6972018-12-03 04:37:25 -0700279 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_HI, 0x80);
280 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_LO, 0x00);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000281 break;
282 case SND_SOC_DAIFMT_CBM_CFM:
283 /* Set to master mode */
284 regval |= M98095_DAI_MAS;
285 break;
286 case SND_SOC_DAIFMT_CBS_CFM:
287 case SND_SOC_DAIFMT_CBM_CFS:
288 default:
289 debug("%s: Clock mode unsupported\n", __func__);
290 return -1;
291 }
292
293 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
294 case SND_SOC_DAIFMT_I2S:
295 regval |= M98095_DAI_DLY;
296 break;
297 case SND_SOC_DAIFMT_LEFT_J:
298 break;
299 default:
300 debug("%s: Unrecognized format.\n", __func__);
301 return -1;
302 }
303
304 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
305 case SND_SOC_DAIFMT_NB_NF:
306 break;
307 case SND_SOC_DAIFMT_NB_IF:
308 regval |= M98095_DAI_WCI;
309 break;
310 case SND_SOC_DAIFMT_IB_NF:
311 regval |= M98095_DAI_BCI;
312 break;
313 case SND_SOC_DAIFMT_IB_IF:
314 regval |= M98095_DAI_BCI | M98095_DAI_WCI;
315 break;
316 default:
317 debug("%s: Unrecognized inversion settings.\n", __func__);
318 return -1;
319 }
320
Simon Glassfd7d6972018-12-03 04:37:25 -0700321 error |= max98095_bic_or(priv, M98095_DAI_FORMAT,
322 M98095_DAI_MAS | M98095_DAI_DLY |
323 M98095_DAI_BCI | M98095_DAI_WCI, regval);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000324
Simon Glassfd7d6972018-12-03 04:37:25 -0700325 error |= max98095_i2c_write(priv, M98095_DAI_CLOCK, M98095_DAI_BSEL64);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000326
327 if (error < 0) {
328 debug("%s: Error setting i2s format.\n", __func__);
329 return -1;
330 }
331
332 return 0;
333}
334
335/*
336 * resets the audio codec
337 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700338 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000339 * @return -1 for error and 0 success.
340 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700341static int max98095_reset(struct max98095_priv *priv)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000342{
343 int i, ret;
344
345 /*
346 * Gracefully reset the DSP core and the codec hardware in a proper
347 * sequence.
348 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700349 ret = max98095_i2c_write(priv, M98095_00F_HOST_CFG, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000350 if (ret != 0) {
351 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
352 return ret;
353 }
354
Simon Glassfd7d6972018-12-03 04:37:25 -0700355 ret = max98095_i2c_write(priv, M98095_097_PWR_SYS, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000356 if (ret != 0) {
357 debug("%s: Failed to reset codec: %d\n", __func__, ret);
358 return ret;
359 }
360
361 /*
362 * Reset to hardware default for registers, as there is not a soft
363 * reset hardware control register.
364 */
365 for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700366 ret = max98095_i2c_write(priv, i, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000367 if (ret < 0) {
368 debug("%s: Failed to reset: %d\n", __func__, ret);
369 return ret;
370 }
371 }
372
373 return 0;
374}
375
376/*
377 * Intialise max98095 codec device
378 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700379 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000380 *
381 * @returns -1 for error and 0 Success.
382 */
Simon Glasse1458f62018-12-03 04:37:28 -0700383static int max98095_device_init(struct max98095_priv *priv)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000384{
385 unsigned char id;
386 int error = 0;
387
Simon Glasse1458f62018-12-03 04:37:28 -0700388 /* Enable codec clock */
389 set_xclkout();
390
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000391 /* reset the codec, the DSP core, and disable all interrupts */
Simon Glassfd7d6972018-12-03 04:37:25 -0700392 error = max98095_reset(priv);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000393 if (error != 0) {
394 debug("Reset\n");
395 return error;
396 }
397
398 /* initialize private data */
Simon Glassfd7d6972018-12-03 04:37:25 -0700399 priv->sysclk = -1U;
400 priv->rate = -1U;
401 priv->fmt = -1U;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000402
Simon Glassfd7d6972018-12-03 04:37:25 -0700403 error = max98095_i2c_read(priv, M98095_0FF_REV_ID, &id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000404 if (error < 0) {
405 debug("%s: Failure reading hardware revision: %d\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530406 __func__, id);
Simon Glasse1458f62018-12-03 04:37:28 -0700407 return error;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000408 }
409 debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
410
Simon Glasse1458f62018-12-03 04:37:28 -0700411 return 0;
412}
413
414static int max98095_setup_interface(struct max98095_priv *priv,
415 enum en_max_audio_interface aif_id)
416{
417 int error;
418
419 error = max98095_i2c_write(priv, M98095_097_PWR_SYS, M98095_PWRSV);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000420
421 /*
422 * initialize registers to hardware default configuring audio
423 * interface2 to DAC
424 */
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530425 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700426 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530427 M98095_DAI1L_TO_DACL |
428 M98095_DAI1R_TO_DACR);
429 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700430 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530431 M98095_DAI2M_TO_DACL |
432 M98095_DAI2M_TO_DACR);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000433
Simon Glassfd7d6972018-12-03 04:37:25 -0700434 error |= max98095_i2c_write(priv, M98095_092_PWR_EN_OUT,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530435 M98095_SPK_SPREADSPECTRUM);
Simon Glassfd7d6972018-12-03 04:37:25 -0700436 error |= max98095_i2c_write(priv, M98095_04E_CFG_HP, M98095_HPNORMAL);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530437 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700438 error |= max98095_i2c_write(priv, M98095_02C_DAI1_IOCFG,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530439 M98095_S1NORMAL | M98095_SDATA);
440 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700441 error |= max98095_i2c_write(priv, M98095_036_DAI2_IOCFG,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530442 M98095_S2NORMAL | M98095_SDATA);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000443
444 /* take the codec out of the shut down */
Simon Glassfd7d6972018-12-03 04:37:25 -0700445 error |= max98095_bic_or(priv, M98095_097_PWR_SYS, M98095_SHDNRUN,
446 M98095_SHDNRUN);
447 /*
448 * route DACL and DACR output to HO and Speakers
449 * Ordering: DACL, DACR, DACL, DACR
450 */
451 error |= max98095_i2c_write(priv, M98095_050_MIX_SPK_LEFT, 0x01);
452 error |= max98095_i2c_write(priv, M98095_051_MIX_SPK_RIGHT, 0x01);
453 error |= max98095_i2c_write(priv, M98095_04C_MIX_HP_LEFT, 0x01);
454 error |= max98095_i2c_write(priv, M98095_04D_MIX_HP_RIGHT, 0x01);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000455
456 /* power Enable */
Simon Glassfd7d6972018-12-03 04:37:25 -0700457 error |= max98095_i2c_write(priv, M98095_091_PWR_EN_OUT, 0xF3);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000458
459 /* set Volume */
Simon Glassfd7d6972018-12-03 04:37:25 -0700460 error |= max98095_i2c_write(priv, M98095_064_LVL_HP_L, 15);
461 error |= max98095_i2c_write(priv, M98095_065_LVL_HP_R, 15);
462 error |= max98095_i2c_write(priv, M98095_067_LVL_SPK_L, 16);
463 error |= max98095_i2c_write(priv, M98095_068_LVL_SPK_R, 16);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000464
465 /* Enable DAIs */
Simon Glassfd7d6972018-12-03 04:37:25 -0700466 error |= max98095_i2c_write(priv, M98095_093_BIAS_CTRL, 0x30);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530467 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700468 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x01);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530469 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700470 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x07);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000471
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000472 if (error < 0)
473 return -1;
474
475 return 0;
476}
477
Simon Glass860b11c2018-12-03 04:37:32 -0700478static int max98095_do_init(struct max98095_priv *priv,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530479 enum en_max_audio_interface aif_id,
480 int sampling_rate, int mclk_freq,
481 int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000482{
483 int ret = 0;
484
Simon Glass860b11c2018-12-03 04:37:32 -0700485 ret = max98095_setup_interface(priv, aif_id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000486 if (ret < 0) {
487 debug("%s: max98095 codec chip init failed\n", __func__);
488 return ret;
489 }
490
Simon Glass860b11c2018-12-03 04:37:32 -0700491 ret = max98095_set_sysclk(priv, mclk_freq);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000492 if (ret < 0) {
493 debug("%s: max98095 codec set sys clock failed\n", __func__);
494 return ret;
495 }
496
Simon Glass860b11c2018-12-03 04:37:32 -0700497 ret = max98095_hw_params(priv, aif_id, sampling_rate,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530498 bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000499
500 if (ret == 0) {
Simon Glass860b11c2018-12-03 04:37:32 -0700501 ret = max98095_set_fmt(priv, SND_SOC_DAIFMT_I2S |
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530502 SND_SOC_DAIFMT_NB_NF |
503 SND_SOC_DAIFMT_CBS_CFS,
504 aif_id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000505 }
506
507 return ret;
508}
509
510static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
511 const void *blob)
512{
513 int error = 0;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000514 enum fdt_compat_id compat;
515 int node;
516 int parent;
517
518 /* Get the node from FDT for codec */
519 node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC);
520 if (node <= 0) {
521 debug("EXYNOS_SOUND: No node for codec in device tree\n");
522 debug("node = %d\n", node);
523 return -1;
524 }
525
526 parent = fdt_parent_offset(blob, node);
527 if (parent < 0) {
528 debug("%s: Cannot find node parent\n", __func__);
529 return -1;
530 }
531
532 compat = fdtdec_lookup(blob, parent);
533 switch (compat) {
534 case COMPAT_SAMSUNG_S3C2440_I2C:
535 pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent);
536 error |= pcodec_info->i2c_bus;
537 debug("i2c bus = %d\n", pcodec_info->i2c_bus);
538 pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node,
539 "reg", 0);
540 error |= pcodec_info->i2c_dev_addr;
541 debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr);
542 break;
543 default:
544 debug("%s: Unknown compat id %d\n", __func__, compat);
545 return -1;
546 }
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000547 if (error == -1) {
548 debug("fail to get max98095 codec node properties\n");
549 return -1;
550 }
551
552 return 0;
553}
554
555/* max98095 Device Initialisation */
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530556int max98095_init(const void *blob, enum en_max_audio_interface aif_id,
557 int sampling_rate, int mclk_freq,
558 int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000559{
560 int ret;
561 int old_bus = i2c_get_bus_num();
Simon Glass860b11c2018-12-03 04:37:32 -0700562 struct sound_codec_info pcodec_info;
563 struct max98095_priv max98095_info;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000564
Simon Glass860b11c2018-12-03 04:37:32 -0700565 if (get_max98095_codec_values(&pcodec_info, blob) < 0) {
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000566 debug("FDT Codec values failed\n");
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530567 return -1;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000568 }
569
Simon Glass860b11c2018-12-03 04:37:32 -0700570 i2c_set_bus_num(pcodec_info.i2c_bus);
Simon Glasse1458f62018-12-03 04:37:28 -0700571
572 /* shift the device address by 1 for 7 bit addressing */
Simon Glass860b11c2018-12-03 04:37:32 -0700573 max98095_info.i2c_addr = pcodec_info.i2c_dev_addr >> 1;
574 ret = max98095_device_init(&max98095_info);
Simon Glasse1458f62018-12-03 04:37:28 -0700575 if (ret < 0) {
576 debug("%s: max98095 codec chip init failed\n", __func__);
577 return ret;
578 }
579
Simon Glass860b11c2018-12-03 04:37:32 -0700580 ret = max98095_do_init(&max98095_info, aif_id, sampling_rate, mclk_freq,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530581 bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000582 i2c_set_bus_num(old_bus);
583
584 return ret;
585}