blob: d7313f9ad75484e93a92b27c70bced874ec1c92e [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 <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
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000027struct max98095_priv {
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000028 unsigned int sysclk;
29 unsigned int rate;
30 unsigned int fmt;
Simon Glass77c04342018-12-03 04:37:30 -070031 int i2c_addr;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000032};
33
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000034/* Index 0 is reserved. */
35int 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 *
Simon Glassfd7d6972018-12-03 04:37:25 -070041 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000042 * @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 */
Simon Glassfd7d6972018-12-03 04:37:25 -070047static int max98095_i2c_write(struct max98095_priv *priv, unsigned int reg,
48 unsigned char data)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000049{
50 debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +053051 __func__, reg, data);
Simon Glass77c04342018-12-03 04:37:30 -070052 return i2c_write(priv->i2c_addr, reg, 1, &data, 1);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000053}
54
55/*
56 * Read a value from a device register through i2c
57 *
Simon Glassfd7d6972018-12-03 04:37:25 -070058 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000059 * @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 */
Simon Glassfd7d6972018-12-03 04:37:25 -070064static unsigned int max98095_i2c_read(struct max98095_priv *priv,
65 unsigned int reg, unsigned char *data)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000066{
67 int ret;
68
Simon Glass77c04342018-12-03 04:37:30 -070069 ret = i2c_read(priv->i2c_addr, reg, 1, data, 1);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000070 if (ret != 0) {
71 debug("%s: Error while reading register %#04x\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +053072 __func__, reg);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000073 return -1;
74 }
75
76 return 0;
77}
78
79/*
80 * update device register bits through i2c
81 *
Simon Glassfd7d6972018-12-03 04:37:25 -070082 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000083 * @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 */
Simon Glassfd7d6972018-12-03 04:37:25 -070089static int max98095_bic_or(struct max98095_priv *priv, unsigned int reg,
90 unsigned char mask, unsigned char value)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000091{
92 int change, ret = 0;
93 unsigned char old, new;
94
Simon Glassfd7d6972018-12-03 04:37:25 -070095 if (max98095_i2c_read(priv, reg, &old) != 0)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +000096 return -1;
97 new = (old & ~mask) | (value & mask);
98 change = (old != new) ? 1 : 0;
99 if (change)
Simon Glassfd7d6972018-12-03 04:37:25 -0700100 ret = max98095_i2c_write(priv, reg, new);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000101 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 */
115static 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 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700133 * @param priv max98095 information pointer
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000134 * @param rate Sampling rate
135 * @param bits_per_sample Bits per sample
136 *
137 * @return -1 for error and 0 Success.
138 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700139static int max98095_hw_params(struct max98095_priv *priv,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530140 enum en_max_audio_interface aif_id,
141 unsigned int rate, unsigned int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000142{
143 u8 regval;
144 int error;
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530145 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 }
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000158
159 switch (bits_per_sample) {
160 case 16:
Simon Glassfd7d6972018-12-03 04:37:25 -0700161 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
162 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000163 break;
164 case 24:
Simon Glassfd7d6972018-12-03 04:37:25 -0700165 error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS,
166 M98095_DAI_WS);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000167 break;
168 default:
169 debug("%s: Illegal bits per sample %d.\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530170 __func__, bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000171 return -1;
172 }
173
174 if (rate_value(rate, &regval)) {
175 debug("%s: Failed to set sample rate to %d.\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530176 __func__, rate);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000177 return -1;
178 }
Simon Glassfd7d6972018-12-03 04:37:25 -0700179 priv->rate = rate;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000180
Simon Glassfd7d6972018-12-03 04:37:25 -0700181 error |= max98095_bic_or(priv, M98095_DAI_CLKMODE, M98095_CLKMODE_MASK,
182 regval);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000183
184 /* Update sample rate mode */
185 if (rate < 50000)
Simon Glassfd7d6972018-12-03 04:37:25 -0700186 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
187 M98095_DAI_DHF, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000188 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700189 error |= max98095_bic_or(priv, M98095_DAI_FILTERS,
190 M98095_DAI_DHF, M98095_DAI_DHF);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000191
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 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700203 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000204 * @param freq Sampling frequency in Hz
205 *
206 * @return -1 for error and 0 success.
207 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700208static int max98095_set_sysclk(struct max98095_priv *priv, unsigned int freq)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000209{
210 int error = 0;
211
212 /* Requested clock frequency is already setup */
Simon Glassfd7d6972018-12-03 04:37:25 -0700213 if (freq == priv->sysclk)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000214 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)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700222 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x10);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000223 } else if ((freq >= 20000000) && (freq < 40000000)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700224 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x20);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000225 } else if ((freq >= 40000000) && (freq < 60000000)) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700226 error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x30);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000227 } 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
Simon Glassfd7d6972018-12-03 04:37:25 -0700237 priv->sysclk = freq;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000238 return 0;
239}
240
241/*
242 * Sets Max98095 I2S format
243 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700244 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000245 * @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 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700250static int max98095_set_fmt(struct max98095_priv *priv, int fmt,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530251 enum en_max_audio_interface aif_id)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000252{
253 u8 regval = 0;
254 int error = 0;
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530255 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;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000259
Simon Glassfd7d6972018-12-03 04:37:25 -0700260 if (fmt == priv->fmt)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000261 return 0;
262
Simon Glassfd7d6972018-12-03 04:37:25 -0700263 priv->fmt = fmt;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000264
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530265 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
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000277 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
278 case SND_SOC_DAIFMT_CBS_CFS:
279 /* Slave mode PLL */
Simon Glassfd7d6972018-12-03 04:37:25 -0700280 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_HI, 0x80);
281 error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_LO, 0x00);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000282 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
Simon Glassfd7d6972018-12-03 04:37:25 -0700322 error |= max98095_bic_or(priv, M98095_DAI_FORMAT,
323 M98095_DAI_MAS | M98095_DAI_DLY |
324 M98095_DAI_BCI | M98095_DAI_WCI, regval);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000325
Simon Glassfd7d6972018-12-03 04:37:25 -0700326 error |= max98095_i2c_write(priv, M98095_DAI_CLOCK, M98095_DAI_BSEL64);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000327
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 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700339 * @param priv Private data for driver
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000340 * @return -1 for error and 0 success.
341 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700342static int max98095_reset(struct max98095_priv *priv)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000343{
344 int i, ret;
345
346 /*
347 * Gracefully reset the DSP core and the codec hardware in a proper
348 * sequence.
349 */
Simon Glassfd7d6972018-12-03 04:37:25 -0700350 ret = max98095_i2c_write(priv, M98095_00F_HOST_CFG, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000351 if (ret != 0) {
352 debug("%s: Failed to reset DSP: %d\n", __func__, ret);
353 return ret;
354 }
355
Simon Glassfd7d6972018-12-03 04:37:25 -0700356 ret = max98095_i2c_write(priv, M98095_097_PWR_SYS, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000357 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++) {
Simon Glassfd7d6972018-12-03 04:37:25 -0700367 ret = max98095_i2c_write(priv, i, 0);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000368 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 *
Simon Glassfd7d6972018-12-03 04:37:25 -0700380 * @param priv max98095 information
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000381 *
382 * @returns -1 for error and 0 Success.
383 */
Simon Glasse1458f62018-12-03 04:37:28 -0700384static int max98095_device_init(struct max98095_priv *priv)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000385{
386 unsigned char id;
387 int error = 0;
388
Simon Glasse1458f62018-12-03 04:37:28 -0700389 /* Enable codec clock */
390 set_xclkout();
391
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000392 /* reset the codec, the DSP core, and disable all interrupts */
Simon Glassfd7d6972018-12-03 04:37:25 -0700393 error = max98095_reset(priv);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000394 if (error != 0) {
395 debug("Reset\n");
396 return error;
397 }
398
399 /* initialize private data */
Simon Glassfd7d6972018-12-03 04:37:25 -0700400 priv->sysclk = -1U;
401 priv->rate = -1U;
402 priv->fmt = -1U;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000403
Simon Glassfd7d6972018-12-03 04:37:25 -0700404 error = max98095_i2c_read(priv, M98095_0FF_REV_ID, &id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000405 if (error < 0) {
406 debug("%s: Failure reading hardware revision: %d\n",
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530407 __func__, id);
Simon Glasse1458f62018-12-03 04:37:28 -0700408 return error;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000409 }
410 debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
411
Simon Glasse1458f62018-12-03 04:37:28 -0700412 return 0;
413}
414
415static 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);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000421
422 /*
423 * initialize registers to hardware default configuring audio
424 * interface2 to DAC
425 */
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530426 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700427 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530428 M98095_DAI1L_TO_DACL |
429 M98095_DAI1R_TO_DACR);
430 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700431 error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530432 M98095_DAI2M_TO_DACL |
433 M98095_DAI2M_TO_DACR);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000434
Simon Glassfd7d6972018-12-03 04:37:25 -0700435 error |= max98095_i2c_write(priv, M98095_092_PWR_EN_OUT,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530436 M98095_SPK_SPREADSPECTRUM);
Simon Glassfd7d6972018-12-03 04:37:25 -0700437 error |= max98095_i2c_write(priv, M98095_04E_CFG_HP, M98095_HPNORMAL);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530438 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700439 error |= max98095_i2c_write(priv, M98095_02C_DAI1_IOCFG,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530440 M98095_S1NORMAL | M98095_SDATA);
441 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700442 error |= max98095_i2c_write(priv, M98095_036_DAI2_IOCFG,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530443 M98095_S2NORMAL | M98095_SDATA);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000444
445 /* take the codec out of the shut down */
Simon Glassfd7d6972018-12-03 04:37:25 -0700446 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);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000456
457 /* power Enable */
Simon Glassfd7d6972018-12-03 04:37:25 -0700458 error |= max98095_i2c_write(priv, M98095_091_PWR_EN_OUT, 0xF3);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000459
460 /* set Volume */
Simon Glassfd7d6972018-12-03 04:37:25 -0700461 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);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000465
466 /* Enable DAIs */
Simon Glassfd7d6972018-12-03 04:37:25 -0700467 error |= max98095_i2c_write(priv, M98095_093_BIAS_CTRL, 0x30);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530468 if (aif_id == AIF1)
Simon Glassfd7d6972018-12-03 04:37:25 -0700469 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x01);
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530470 else
Simon Glassfd7d6972018-12-03 04:37:25 -0700471 error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x07);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000472
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000473 if (error < 0)
474 return -1;
475
476 return 0;
477}
478
Simon Glass860b11c2018-12-03 04:37:32 -0700479static int max98095_do_init(struct max98095_priv *priv,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530480 enum en_max_audio_interface aif_id,
481 int sampling_rate, int mclk_freq,
482 int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000483{
484 int ret = 0;
485
Simon Glass860b11c2018-12-03 04:37:32 -0700486 ret = max98095_setup_interface(priv, aif_id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000487 if (ret < 0) {
488 debug("%s: max98095 codec chip init failed\n", __func__);
489 return ret;
490 }
491
Simon Glass860b11c2018-12-03 04:37:32 -0700492 ret = max98095_set_sysclk(priv, mclk_freq);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000493 if (ret < 0) {
494 debug("%s: max98095 codec set sys clock failed\n", __func__);
495 return ret;
496 }
497
Simon Glass860b11c2018-12-03 04:37:32 -0700498 ret = max98095_hw_params(priv, aif_id, sampling_rate,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530499 bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000500
501 if (ret == 0) {
Simon Glass860b11c2018-12-03 04:37:32 -0700502 ret = max98095_set_fmt(priv, SND_SOC_DAIFMT_I2S |
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530503 SND_SOC_DAIFMT_NB_NF |
504 SND_SOC_DAIFMT_CBS_CFS,
505 aif_id);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000506 }
507
508 return ret;
509}
510
511static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
512 const void *blob)
513{
514 int error = 0;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000515 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 }
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000548 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 */
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530557int max98095_init(const void *blob, enum en_max_audio_interface aif_id,
558 int sampling_rate, int mclk_freq,
559 int bits_per_sample)
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000560{
561 int ret;
562 int old_bus = i2c_get_bus_num();
Simon Glass860b11c2018-12-03 04:37:32 -0700563 struct sound_codec_info pcodec_info;
564 struct max98095_priv max98095_info;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000565
Simon Glass860b11c2018-12-03 04:37:32 -0700566 if (get_max98095_codec_values(&pcodec_info, blob) < 0) {
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000567 debug("FDT Codec values failed\n");
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530568 return -1;
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000569 }
570
Simon Glass860b11c2018-12-03 04:37:32 -0700571 i2c_set_bus_num(pcodec_info.i2c_bus);
Simon Glasse1458f62018-12-03 04:37:28 -0700572
573 /* shift the device address by 1 for 7 bit addressing */
Simon Glass860b11c2018-12-03 04:37:32 -0700574 max98095_info.i2c_addr = pcodec_info.i2c_dev_addr >> 1;
575 ret = max98095_device_init(&max98095_info);
Simon Glasse1458f62018-12-03 04:37:28 -0700576 if (ret < 0) {
577 debug("%s: max98095 codec chip init failed\n", __func__);
578 return ret;
579 }
580
Simon Glass860b11c2018-12-03 04:37:32 -0700581 ret = max98095_do_init(&max98095_info, aif_id, sampling_rate, mclk_freq,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530582 bits_per_sample);
Rajeshwari Shindecd93e9b2013-02-14 19:46:12 +0000583 i2c_set_bus_num(old_bus);
584
585 return ret;
586}