max98095.c 14 KB

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