max98095.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. unsigned int rate, unsigned int bits_per_sample)
  127. {
  128. u8 regval;
  129. int error;
  130. switch (bits_per_sample) {
  131. case 16:
  132. error = max98095_update_bits(M98095_034_DAI2_FORMAT,
  133. M98095_DAI_WS, 0);
  134. break;
  135. case 24:
  136. error = max98095_update_bits(M98095_034_DAI2_FORMAT,
  137. M98095_DAI_WS, M98095_DAI_WS);
  138. break;
  139. default:
  140. debug("%s: Illegal bits per sample %d.\n",
  141. __func__, bits_per_sample);
  142. return -1;
  143. }
  144. if (rate_value(rate, &regval)) {
  145. debug("%s: Failed to set sample rate to %d.\n",
  146. __func__, rate);
  147. return -1;
  148. }
  149. max98095->rate = rate;
  150. error |= max98095_update_bits(M98095_031_DAI2_CLKMODE,
  151. M98095_CLKMODE_MASK, regval);
  152. /* Update sample rate mode */
  153. if (rate < 50000)
  154. error |= max98095_update_bits(M98095_038_DAI2_FILTERS,
  155. M98095_DAI_DHF, 0);
  156. else
  157. error |= max98095_update_bits(M98095_038_DAI2_FILTERS,
  158. M98095_DAI_DHF, M98095_DAI_DHF);
  159. if (error < 0) {
  160. debug("%s: Error setting hardware params.\n", __func__);
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. /*
  166. * Configures Audio interface system clock for the given frequency
  167. *
  168. * @param max98095 max98095 information
  169. * @param freq Sampling frequency in Hz
  170. *
  171. * @return -1 for error and 0 success.
  172. */
  173. static int max98095_set_sysclk(struct max98095_priv *max98095,
  174. unsigned int freq)
  175. {
  176. int error = 0;
  177. /* Requested clock frequency is already setup */
  178. if (freq == max98095->sysclk)
  179. return 0;
  180. /* Setup clocks for slave mode, and using the PLL
  181. * PSCLK = 0x01 (when master clk is 10MHz to 20MHz)
  182. * 0x02 (when master clk is 20MHz to 40MHz)..
  183. * 0x03 (when master clk is 40MHz to 60MHz)..
  184. */
  185. if ((freq >= 10000000) && (freq < 20000000)) {
  186. error = max98095_i2c_write(M98095_026_SYS_CLK, 0x10);
  187. } else if ((freq >= 20000000) && (freq < 40000000)) {
  188. error = max98095_i2c_write(M98095_026_SYS_CLK, 0x20);
  189. } else if ((freq >= 40000000) && (freq < 60000000)) {
  190. error = max98095_i2c_write(M98095_026_SYS_CLK, 0x30);
  191. } else {
  192. debug("%s: Invalid master clock frequency\n", __func__);
  193. return -1;
  194. }
  195. debug("%s: Clock at %uHz\n", __func__, freq);
  196. if (error < 0)
  197. return -1;
  198. max98095->sysclk = freq;
  199. return 0;
  200. }
  201. /*
  202. * Sets Max98095 I2S format
  203. *
  204. * @param max98095 max98095 information
  205. * @param fmt i2S format - supports a subset of the options defined
  206. * in i2s.h.
  207. *
  208. * @return -1 for error and 0 Success.
  209. */
  210. static int max98095_set_fmt(struct max98095_priv *max98095, int fmt)
  211. {
  212. u8 regval = 0;
  213. int error = 0;
  214. if (fmt == max98095->fmt)
  215. return 0;
  216. max98095->fmt = fmt;
  217. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  218. case SND_SOC_DAIFMT_CBS_CFS:
  219. /* Slave mode PLL */
  220. error |= max98095_i2c_write(M98095_032_DAI2_CLKCFG_HI,
  221. 0x80);
  222. error |= max98095_i2c_write(M98095_033_DAI2_CLKCFG_LO,
  223. 0x00);
  224. break;
  225. case SND_SOC_DAIFMT_CBM_CFM:
  226. /* Set to master mode */
  227. regval |= M98095_DAI_MAS;
  228. break;
  229. case SND_SOC_DAIFMT_CBS_CFM:
  230. case SND_SOC_DAIFMT_CBM_CFS:
  231. default:
  232. debug("%s: Clock mode unsupported\n", __func__);
  233. return -1;
  234. }
  235. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  236. case SND_SOC_DAIFMT_I2S:
  237. regval |= M98095_DAI_DLY;
  238. break;
  239. case SND_SOC_DAIFMT_LEFT_J:
  240. break;
  241. default:
  242. debug("%s: Unrecognized format.\n", __func__);
  243. return -1;
  244. }
  245. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  246. case SND_SOC_DAIFMT_NB_NF:
  247. break;
  248. case SND_SOC_DAIFMT_NB_IF:
  249. regval |= M98095_DAI_WCI;
  250. break;
  251. case SND_SOC_DAIFMT_IB_NF:
  252. regval |= M98095_DAI_BCI;
  253. break;
  254. case SND_SOC_DAIFMT_IB_IF:
  255. regval |= M98095_DAI_BCI | M98095_DAI_WCI;
  256. break;
  257. default:
  258. debug("%s: Unrecognized inversion settings.\n", __func__);
  259. return -1;
  260. }
  261. error |= max98095_update_bits(M98095_034_DAI2_FORMAT,
  262. M98095_DAI_MAS | M98095_DAI_DLY | M98095_DAI_BCI |
  263. M98095_DAI_WCI, regval);
  264. error |= max98095_i2c_write(M98095_035_DAI2_CLOCK,
  265. M98095_DAI_BSEL64);
  266. if (error < 0) {
  267. debug("%s: Error setting i2s format.\n", __func__);
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. /*
  273. * resets the audio codec
  274. *
  275. * @return -1 for error and 0 success.
  276. */
  277. static int max98095_reset(void)
  278. {
  279. int i, ret;
  280. /*
  281. * Gracefully reset the DSP core and the codec hardware in a proper
  282. * sequence.
  283. */
  284. ret = max98095_i2c_write(M98095_00F_HOST_CFG, 0);
  285. if (ret != 0) {
  286. debug("%s: Failed to reset DSP: %d\n", __func__, ret);
  287. return ret;
  288. }
  289. ret = max98095_i2c_write(M98095_097_PWR_SYS, 0);
  290. if (ret != 0) {
  291. debug("%s: Failed to reset codec: %d\n", __func__, ret);
  292. return ret;
  293. }
  294. /*
  295. * Reset to hardware default for registers, as there is not a soft
  296. * reset hardware control register.
  297. */
  298. for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) {
  299. ret = max98095_i2c_write(i, 0);
  300. if (ret < 0) {
  301. debug("%s: Failed to reset: %d\n", __func__, ret);
  302. return ret;
  303. }
  304. }
  305. return 0;
  306. }
  307. /*
  308. * Intialise max98095 codec device
  309. *
  310. * @param max98095 max98095 information
  311. *
  312. * @returns -1 for error and 0 Success.
  313. */
  314. static int max98095_device_init(struct max98095_priv *max98095)
  315. {
  316. unsigned char id;
  317. int error = 0;
  318. /* reset the codec, the DSP core, and disable all interrupts */
  319. error = max98095_reset();
  320. if (error != 0) {
  321. debug("Reset\n");
  322. return error;
  323. }
  324. /* initialize private data */
  325. max98095->sysclk = -1U;
  326. max98095->rate = -1U;
  327. max98095->fmt = -1U;
  328. error = max98095_i2c_read(M98095_0FF_REV_ID, &id);
  329. if (error < 0) {
  330. debug("%s: Failure reading hardware revision: %d\n",
  331. __func__, id);
  332. goto err_access;
  333. }
  334. debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A');
  335. error |= max98095_i2c_write(M98095_097_PWR_SYS, M98095_PWRSV);
  336. /*
  337. * initialize registers to hardware default configuring audio
  338. * interface2 to DAC
  339. */
  340. error |= max98095_i2c_write(M98095_048_MIX_DAC_LR,
  341. M98095_DAI2M_TO_DACL|M98095_DAI2M_TO_DACR);
  342. error |= max98095_i2c_write(M98095_092_PWR_EN_OUT,
  343. M98095_SPK_SPREADSPECTRUM);
  344. error |= max98095_i2c_write(M98095_045_CFG_DSP, M98095_DSPNORMAL);
  345. error |= max98095_i2c_write(M98095_04E_CFG_HP, M98095_HPNORMAL);
  346. error |= max98095_i2c_write(M98095_02C_DAI1_IOCFG,
  347. M98095_S1NORMAL|M98095_SDATA);
  348. error |= max98095_i2c_write(M98095_036_DAI2_IOCFG,
  349. M98095_S2NORMAL|M98095_SDATA);
  350. error |= max98095_i2c_write(M98095_040_DAI3_IOCFG,
  351. M98095_S3NORMAL|M98095_SDATA);
  352. /* take the codec out of the shut down */
  353. error |= max98095_update_bits(M98095_097_PWR_SYS, M98095_SHDNRUN,
  354. M98095_SHDNRUN);
  355. /* route DACL and DACR output to HO and Spekers */
  356. error |= max98095_i2c_write(M98095_050_MIX_SPK_LEFT, 0x01); /* DACL */
  357. error |= max98095_i2c_write(M98095_051_MIX_SPK_RIGHT, 0x01);/* DACR */
  358. error |= max98095_i2c_write(M98095_04C_MIX_HP_LEFT, 0x01); /* DACL */
  359. error |= max98095_i2c_write(M98095_04D_MIX_HP_RIGHT, 0x01); /* DACR */
  360. /* power Enable */
  361. error |= max98095_i2c_write(M98095_091_PWR_EN_OUT, 0xF3);
  362. /* set Volume */
  363. error |= max98095_i2c_write(M98095_064_LVL_HP_L, 15);
  364. error |= max98095_i2c_write(M98095_065_LVL_HP_R, 15);
  365. error |= max98095_i2c_write(M98095_067_LVL_SPK_L, 16);
  366. error |= max98095_i2c_write(M98095_068_LVL_SPK_R, 16);
  367. /* Enable DAIs */
  368. error |= max98095_i2c_write(M98095_093_BIAS_CTRL, 0x30);
  369. error |= max98095_i2c_write(M98095_096_PWR_DAC_CK, 0x07);
  370. err_access:
  371. if (error < 0)
  372. return -1;
  373. return 0;
  374. }
  375. static int max98095_do_init(struct sound_codec_info *pcodec_info,
  376. int sampling_rate, int mclk_freq,
  377. int bits_per_sample)
  378. {
  379. int ret = 0;
  380. /* Enable codec clock */
  381. set_xclkout();
  382. /* shift the device address by 1 for 7 bit addressing */
  383. g_max98095_i2c_dev_addr = pcodec_info->i2c_dev_addr >> 1;
  384. if (pcodec_info->codec_type == CODEC_MAX_98095)
  385. g_max98095_info.devtype = MAX98095;
  386. else {
  387. debug("%s: Codec id [%d] not defined\n", __func__,
  388. pcodec_info->codec_type);
  389. return -1;
  390. }
  391. ret = max98095_device_init(&g_max98095_info);
  392. if (ret < 0) {
  393. debug("%s: max98095 codec chip init failed\n", __func__);
  394. return ret;
  395. }
  396. ret = max98095_set_sysclk(&g_max98095_info, mclk_freq);
  397. if (ret < 0) {
  398. debug("%s: max98095 codec set sys clock failed\n", __func__);
  399. return ret;
  400. }
  401. ret = max98095_hw_params(&g_max98095_info, sampling_rate,
  402. bits_per_sample);
  403. if (ret == 0) {
  404. ret = max98095_set_fmt(&g_max98095_info,
  405. SND_SOC_DAIFMT_I2S |
  406. SND_SOC_DAIFMT_NB_NF |
  407. SND_SOC_DAIFMT_CBS_CFS);
  408. }
  409. return ret;
  410. }
  411. static int get_max98095_codec_values(struct sound_codec_info *pcodec_info,
  412. const void *blob)
  413. {
  414. int error = 0;
  415. #ifdef CONFIG_OF_CONTROL
  416. enum fdt_compat_id compat;
  417. int node;
  418. int parent;
  419. /* Get the node from FDT for codec */
  420. node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC);
  421. if (node <= 0) {
  422. debug("EXYNOS_SOUND: No node for codec in device tree\n");
  423. debug("node = %d\n", node);
  424. return -1;
  425. }
  426. parent = fdt_parent_offset(blob, node);
  427. if (parent < 0) {
  428. debug("%s: Cannot find node parent\n", __func__);
  429. return -1;
  430. }
  431. compat = fdtdec_lookup(blob, parent);
  432. switch (compat) {
  433. case COMPAT_SAMSUNG_S3C2440_I2C:
  434. pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent);
  435. error |= pcodec_info->i2c_bus;
  436. debug("i2c bus = %d\n", pcodec_info->i2c_bus);
  437. pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node,
  438. "reg", 0);
  439. error |= pcodec_info->i2c_dev_addr;
  440. debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr);
  441. break;
  442. default:
  443. debug("%s: Unknown compat id %d\n", __func__, compat);
  444. return -1;
  445. }
  446. #else
  447. pcodec_info->i2c_bus = AUDIO_I2C_BUS;
  448. pcodec_info->i2c_dev_addr = AUDIO_I2C_REG;
  449. debug("i2c dev addr = %d\n", pcodec_info->i2c_dev_addr);
  450. #endif
  451. pcodec_info->codec_type = CODEC_MAX_98095;
  452. if (error == -1) {
  453. debug("fail to get max98095 codec node properties\n");
  454. return -1;
  455. }
  456. return 0;
  457. }
  458. /* max98095 Device Initialisation */
  459. int max98095_init(const void *blob, int sampling_rate, int mclk_freq,
  460. int bits_per_sample)
  461. {
  462. int ret;
  463. int old_bus = i2c_get_bus_num();
  464. struct sound_codec_info *pcodec_info = &g_codec_info;
  465. if (get_max98095_codec_values(pcodec_info, blob) < 0) {
  466. debug("FDT Codec values failed\n");
  467. return -1;
  468. }
  469. i2c_set_bus_num(pcodec_info->i2c_bus);
  470. ret = max98095_do_init(pcodec_info, sampling_rate, mclk_freq,
  471. bits_per_sample);
  472. i2c_set_bus_num(old_bus);
  473. return ret;
  474. }