max98095.c 14 KB

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