wm8994.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics
  3. * R. Chandrasekar <rcsekar@samsung.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <asm/arch/clk.h>
  24. #include <asm/arch/cpu.h>
  25. #include <asm/gpio.h>
  26. #include <asm/io.h>
  27. #include <common.h>
  28. #include <div64.h>
  29. #include <i2c.h>
  30. #include <i2s.h>
  31. #include <sound.h>
  32. #include "wm8994.h"
  33. #include "wm8994_registers.h"
  34. /* defines for wm8994 system clock selection */
  35. #define SEL_MCLK1 0x00
  36. #define SEL_MCLK2 0x08
  37. #define SEL_FLL1 0x10
  38. #define SEL_FLL2 0x18
  39. /* fll config to configure fll */
  40. struct wm8994_fll_config {
  41. int src; /* Source */
  42. int in; /* Input frequency in Hz */
  43. int out; /* output frequency in Hz */
  44. };
  45. /* codec private data */
  46. struct wm8994_priv {
  47. enum wm8994_type type; /* codec type of wolfson */
  48. int revision; /* Revision */
  49. int sysclk[WM8994_MAX_AIF]; /* System clock frequency in Hz */
  50. int mclk[WM8994_MAX_AIF]; /* master clock frequency in Hz */
  51. int aifclk[WM8994_MAX_AIF]; /* audio interface clock in Hz */
  52. struct wm8994_fll_config fll[2]; /* fll config to configure fll */
  53. };
  54. /* wm 8994 supported sampling rate values */
  55. static unsigned int src_rate[] = {
  56. 8000, 11025, 12000, 16000, 22050, 24000,
  57. 32000, 44100, 48000, 88200, 96000
  58. };
  59. /* op clock divisions */
  60. static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
  61. /* lr clock frame size ratio */
  62. static int fs_ratios[] = {
  63. 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
  64. };
  65. /* bit clock divisors */
  66. static int bclk_divs[] = {
  67. 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
  68. 640, 880, 960, 1280, 1760, 1920
  69. };
  70. static struct wm8994_priv g_wm8994_info;
  71. static unsigned char g_wm8994_i2c_dev_addr;
  72. /*
  73. * Initialise I2C for wm 8994
  74. *
  75. * @param bus no i2c bus number in which wm8994 is connected
  76. */
  77. static void wm8994_i2c_init(int bus_no)
  78. {
  79. i2c_set_bus_num(bus_no);
  80. }
  81. /*
  82. * Writes value to a device register through i2c
  83. *
  84. * @param reg reg number to be write
  85. * @param data data to be writen to the above registor
  86. *
  87. * @return int value 1 for change, 0 for no change or negative error code.
  88. */
  89. static int wm8994_i2c_write(unsigned int reg, unsigned short data)
  90. {
  91. unsigned char val[2];
  92. val[0] = (unsigned char)((data >> 8) & 0xff);
  93. val[1] = (unsigned char)(data & 0xff);
  94. debug("Write Addr : 0x%04X, Data : 0x%04X\n", reg, data);
  95. return i2c_write(g_wm8994_i2c_dev_addr, reg, 2, val, 2);
  96. }
  97. /*
  98. * Read a value from a device register through i2c
  99. *
  100. * @param reg reg number to be read
  101. * @param data address of read data to be stored
  102. *
  103. * @return int value 0 for success, -1 in case of error.
  104. */
  105. static unsigned int wm8994_i2c_read(unsigned int reg , unsigned short *data)
  106. {
  107. unsigned char val[2];
  108. int ret;
  109. ret = i2c_read(g_wm8994_i2c_dev_addr, reg, 2, val, 2);
  110. if (ret != 0) {
  111. debug("%s: Error while reading register %#04x\n",
  112. __func__, reg);
  113. return -1;
  114. }
  115. *data = val[0];
  116. *data <<= 8;
  117. *data |= val[1];
  118. return 0;
  119. }
  120. /*
  121. * update device register bits through i2c
  122. *
  123. * @param reg codec register
  124. * @param mask register mask
  125. * @param value new value
  126. *
  127. * @return int value 1 if change in the register value,
  128. * 0 for no change or negative error code.
  129. */
  130. static int wm8994_update_bits(unsigned int reg, unsigned short mask,
  131. unsigned short value)
  132. {
  133. int change , ret = 0;
  134. unsigned short old, new;
  135. if (wm8994_i2c_read(reg, &old) != 0)
  136. return -1;
  137. new = (old & ~mask) | (value & mask);
  138. change = (old != new) ? 1 : 0;
  139. if (change)
  140. ret = wm8994_i2c_write(reg, new);
  141. if (ret < 0)
  142. return ret;
  143. return change;
  144. }
  145. /*
  146. * Sets i2s set format
  147. *
  148. * @param aif_id Interface ID
  149. * @param fmt i2S format
  150. *
  151. * @return -1 for error and 0 Success.
  152. */
  153. int wm8994_set_fmt(int aif_id, unsigned int fmt)
  154. {
  155. int ms_reg;
  156. int aif_reg;
  157. int ms = 0;
  158. int aif = 0;
  159. int aif_clk = 0;
  160. int error = 0;
  161. switch (aif_id) {
  162. case 1:
  163. ms_reg = WM8994_AIF1_MASTER_SLAVE;
  164. aif_reg = WM8994_AIF1_CONTROL_1;
  165. aif_clk = WM8994_AIF1_CLOCKING_1;
  166. break;
  167. case 2:
  168. ms_reg = WM8994_AIF2_MASTER_SLAVE;
  169. aif_reg = WM8994_AIF2_CONTROL_1;
  170. aif_clk = WM8994_AIF2_CLOCKING_1;
  171. break;
  172. default:
  173. debug("%s: Invalid audio interface selection\n", __func__);
  174. return -1;
  175. }
  176. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  177. case SND_SOC_DAIFMT_CBS_CFS:
  178. break;
  179. case SND_SOC_DAIFMT_CBM_CFM:
  180. ms = WM8994_AIF1_MSTR;
  181. break;
  182. default:
  183. debug("%s: Invalid i2s master selection\n", __func__);
  184. return -1;
  185. }
  186. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  187. case SND_SOC_DAIFMT_DSP_B:
  188. aif |= WM8994_AIF1_LRCLK_INV;
  189. case SND_SOC_DAIFMT_DSP_A:
  190. aif |= 0x18;
  191. break;
  192. case SND_SOC_DAIFMT_I2S:
  193. aif |= 0x10;
  194. break;
  195. case SND_SOC_DAIFMT_RIGHT_J:
  196. break;
  197. case SND_SOC_DAIFMT_LEFT_J:
  198. aif |= 0x8;
  199. break;
  200. default:
  201. debug("%s: Invalid i2s format selection\n", __func__);
  202. return -1;
  203. }
  204. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  205. case SND_SOC_DAIFMT_DSP_A:
  206. case SND_SOC_DAIFMT_DSP_B:
  207. /* frame inversion not valid for DSP modes */
  208. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  209. case SND_SOC_DAIFMT_NB_NF:
  210. break;
  211. case SND_SOC_DAIFMT_IB_NF:
  212. aif |= WM8994_AIF1_BCLK_INV;
  213. break;
  214. default:
  215. debug("%s: Invalid i2s frame inverse selection\n",
  216. __func__);
  217. return -1;
  218. }
  219. break;
  220. case SND_SOC_DAIFMT_I2S:
  221. case SND_SOC_DAIFMT_RIGHT_J:
  222. case SND_SOC_DAIFMT_LEFT_J:
  223. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  224. case SND_SOC_DAIFMT_NB_NF:
  225. break;
  226. case SND_SOC_DAIFMT_IB_IF:
  227. aif |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
  228. break;
  229. case SND_SOC_DAIFMT_IB_NF:
  230. aif |= WM8994_AIF1_BCLK_INV;
  231. break;
  232. case SND_SOC_DAIFMT_NB_IF:
  233. aif |= WM8994_AIF1_LRCLK_INV;
  234. break;
  235. default:
  236. debug("%s: Invalid i2s clock polarity selection\n",
  237. __func__);
  238. return -1;
  239. }
  240. break;
  241. default:
  242. debug("%s: Invalid i2s format selection\n", __func__);
  243. return -1;
  244. }
  245. error = wm8994_update_bits(aif_reg, WM8994_AIF1_BCLK_INV |
  246. WM8994_AIF1_LRCLK_INV_MASK | WM8994_AIF1_FMT_MASK, aif);
  247. error |= wm8994_update_bits(ms_reg, WM8994_AIF1_MSTR_MASK, ms);
  248. error |= wm8994_update_bits(aif_clk, WM8994_AIF1CLK_ENA_MASK,
  249. WM8994_AIF1CLK_ENA);
  250. if (error < 0) {
  251. debug("%s: codec register access error\n", __func__);
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. /*
  257. * Sets hw params FOR WM8994
  258. *
  259. * @param wm8994 wm8994 information pointer
  260. * @param aif_id Audio interface ID
  261. * @param sampling_rate Sampling rate
  262. * @param bits_per_sample Bits per sample
  263. * @param Channels Channels in the given audio input
  264. *
  265. * @return -1 for error and 0 Success.
  266. */
  267. static int wm8994_hw_params(struct wm8994_priv *wm8994, int aif_id,
  268. unsigned int sampling_rate, unsigned int bits_per_sample,
  269. unsigned int channels)
  270. {
  271. int aif1_reg;
  272. int aif2_reg;
  273. int bclk_reg;
  274. int bclk = 0;
  275. int rate_reg;
  276. int aif1 = 0;
  277. int aif2 = 0;
  278. int rate_val = 0;
  279. int id = aif_id - 1;
  280. int i, cur_val, best_val, bclk_rate, best;
  281. unsigned short reg_data;
  282. int ret = 0;
  283. switch (aif_id) {
  284. case 1:
  285. aif1_reg = WM8994_AIF1_CONTROL_1;
  286. aif2_reg = WM8994_AIF1_CONTROL_2;
  287. bclk_reg = WM8994_AIF1_BCLK;
  288. rate_reg = WM8994_AIF1_RATE;
  289. break;
  290. case 2:
  291. aif1_reg = WM8994_AIF2_CONTROL_1;
  292. aif2_reg = WM8994_AIF2_CONTROL_2;
  293. bclk_reg = WM8994_AIF2_BCLK;
  294. rate_reg = WM8994_AIF2_RATE;
  295. break;
  296. default:
  297. return -1;
  298. }
  299. bclk_rate = sampling_rate * 32;
  300. switch (bits_per_sample) {
  301. case 16:
  302. bclk_rate *= 16;
  303. break;
  304. case 20:
  305. bclk_rate *= 20;
  306. aif1 |= 0x20;
  307. break;
  308. case 24:
  309. bclk_rate *= 24;
  310. aif1 |= 0x40;
  311. break;
  312. case 32:
  313. bclk_rate *= 32;
  314. aif1 |= 0x60;
  315. break;
  316. default:
  317. return -1;
  318. }
  319. /* Try to find an appropriate sample rate; look for an exact match. */
  320. for (i = 0; i < ARRAY_SIZE(src_rate); i++)
  321. if (src_rate[i] == sampling_rate)
  322. break;
  323. if (i == ARRAY_SIZE(src_rate)) {
  324. debug("%s: Could not get the best matching samplingrate\n",
  325. __func__);
  326. return -1;
  327. }
  328. rate_val |= i << WM8994_AIF1_SR_SHIFT;
  329. /* AIFCLK/fs ratio; look for a close match in either direction */
  330. best = 0;
  331. best_val = abs((fs_ratios[0] * sampling_rate)
  332. - wm8994->aifclk[id]);
  333. for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
  334. cur_val = abs((fs_ratios[i] * sampling_rate)
  335. - wm8994->aifclk[id]);
  336. if (cur_val >= best_val)
  337. continue;
  338. best = i;
  339. best_val = cur_val;
  340. }
  341. rate_val |= best;
  342. /*
  343. * We may not get quite the right frequency if using
  344. * approximate clocks so look for the closest match that is
  345. * higher than the target (we need to ensure that there enough
  346. * BCLKs to clock out the samples).
  347. */
  348. best = 0;
  349. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  350. cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
  351. if (cur_val < 0) /* BCLK table is sorted */
  352. break;
  353. best = i;
  354. }
  355. if (i == ARRAY_SIZE(bclk_divs)) {
  356. debug("%s: Could not get the best matching bclk division\n",
  357. __func__);
  358. return -1;
  359. }
  360. bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
  361. bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
  362. if (wm8994_i2c_read(aif1_reg, &reg_data) != 0) {
  363. debug("%s: AIF1 register read Failed\n", __func__);
  364. return -1;
  365. }
  366. if ((channels == 1) && ((reg_data & 0x18) == 0x18))
  367. aif2 |= WM8994_AIF1_MONO;
  368. if (wm8994->aifclk[id] == 0) {
  369. debug("%s:Audio interface clock not set\n", __func__);
  370. return -1;
  371. }
  372. ret = wm8994_update_bits(aif1_reg, WM8994_AIF1_WL_MASK, aif1);
  373. ret |= wm8994_update_bits(aif2_reg, WM8994_AIF1_MONO, aif2);
  374. ret |= wm8994_update_bits(bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
  375. ret |= wm8994_update_bits(rate_reg, WM8994_AIF1_SR_MASK |
  376. WM8994_AIF1CLK_RATE_MASK, rate_val);
  377. debug("rate vale = %x , bclk val= %x\n", rate_val, bclk);
  378. if (ret < 0) {
  379. debug("%s: codec register access error\n", __func__);
  380. return -1;
  381. }
  382. return 0;
  383. }
  384. /*
  385. * Configures Audio interface Clock
  386. *
  387. * @param wm8994 wm8994 information pointer
  388. * @param aif Audio Interface ID
  389. *
  390. * @return -1 for error and 0 Success.
  391. */
  392. static int configure_aif_clock(struct wm8994_priv *wm8994, int aif)
  393. {
  394. int rate;
  395. int reg1 = 0;
  396. int offset;
  397. int ret;
  398. /* AIF(1/0) register adress offset calculated */
  399. if (aif)
  400. offset = 4;
  401. else
  402. offset = 0;
  403. switch (wm8994->sysclk[aif]) {
  404. case WM8994_SYSCLK_MCLK1:
  405. reg1 |= SEL_MCLK1;
  406. rate = wm8994->mclk[0];
  407. break;
  408. case WM8994_SYSCLK_MCLK2:
  409. reg1 |= SEL_MCLK2;
  410. rate = wm8994->mclk[1];
  411. break;
  412. case WM8994_SYSCLK_FLL1:
  413. reg1 |= SEL_FLL1;
  414. rate = wm8994->fll[0].out;
  415. break;
  416. case WM8994_SYSCLK_FLL2:
  417. reg1 |= SEL_FLL2;
  418. rate = wm8994->fll[1].out;
  419. break;
  420. default:
  421. debug("%s: Invalid input clock selection [%d]\n",
  422. __func__, wm8994->sysclk[aif]);
  423. return -1;
  424. }
  425. /* if input clock frequenct is more than 135Mhz then divide */
  426. if (rate >= WM8994_MAX_INPUT_CLK_FREQ) {
  427. rate /= 2;
  428. reg1 |= WM8994_AIF1CLK_DIV;
  429. }
  430. wm8994->aifclk[aif] = rate;
  431. ret = wm8994_update_bits(WM8994_AIF1_CLOCKING_1 + offset,
  432. WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
  433. reg1);
  434. ret |= wm8994_update_bits(WM8994_CLOCKING_1,
  435. WM8994_SYSCLK_SRC | WM8994_AIF2DSPCLK_ENA_MASK |
  436. WM8994_SYSDSPCLK_ENA_MASK, WM8994_SYSCLK_SRC |
  437. WM8994_AIF2DSPCLK_ENA | WM8994_SYSDSPCLK_ENA);
  438. if (ret < 0) {
  439. debug("%s: codec register access error\n", __func__);
  440. return -1;
  441. }
  442. return 0;
  443. }
  444. /*
  445. * Configures Audio interface for the given frequency
  446. *
  447. * @param wm8994 wm8994 information
  448. * @param aif_id Audio Interface
  449. * @param clk_id Input Clock ID
  450. * @param freq Sampling frequency in Hz
  451. *
  452. * @return -1 for error and 0 success.
  453. */
  454. static int wm8994_set_sysclk(struct wm8994_priv *wm8994, int aif_id,
  455. int clk_id, unsigned int freq)
  456. {
  457. int i;
  458. int ret = 0;
  459. wm8994->sysclk[aif_id - 1] = clk_id;
  460. switch (clk_id) {
  461. case WM8994_SYSCLK_MCLK1:
  462. wm8994->mclk[0] = freq;
  463. if (aif_id == 2) {
  464. ret = wm8994_update_bits(WM8994_AIF1_CLOCKING_2 ,
  465. WM8994_AIF2DAC_DIV_MASK , 0);
  466. }
  467. break;
  468. case WM8994_SYSCLK_MCLK2:
  469. /* TODO: Set GPIO AF */
  470. wm8994->mclk[1] = freq;
  471. break;
  472. case WM8994_SYSCLK_FLL1:
  473. case WM8994_SYSCLK_FLL2:
  474. break;
  475. case WM8994_SYSCLK_OPCLK:
  476. /*
  477. * Special case - a division (times 10) is given and
  478. * no effect on main clocking.
  479. */
  480. if (freq) {
  481. for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
  482. if (opclk_divs[i] == freq)
  483. break;
  484. if (i == ARRAY_SIZE(opclk_divs)) {
  485. debug("%s frequency divisor not found\n",
  486. __func__);
  487. return -1;
  488. }
  489. ret = wm8994_update_bits(WM8994_CLOCKING_2,
  490. WM8994_OPCLK_DIV_MASK, i);
  491. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_2,
  492. WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
  493. } else {
  494. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_2,
  495. WM8994_OPCLK_ENA, 0);
  496. }
  497. default:
  498. debug("%s Invalid input clock selection [%d]\n",
  499. __func__, clk_id);
  500. return -1;
  501. }
  502. ret |= configure_aif_clock(wm8994, aif_id - 1);
  503. if (ret < 0) {
  504. debug("%s: codec register access error\n", __func__);
  505. return -1;
  506. }
  507. return 0;
  508. }
  509. /*
  510. * Initializes Volume for AIF2 to HP path
  511. *
  512. * @returns -1 for error and 0 Success.
  513. *
  514. */
  515. static int wm8994_init_volume_aif2_dac1(void)
  516. {
  517. int ret;
  518. /* Unmute AIF2DAC */
  519. ret = wm8994_update_bits(WM8994_AIF2_DAC_FILTERS_1,
  520. WM8994_AIF2DAC_MUTE_MASK, 0);
  521. ret |= wm8994_update_bits(WM8994_AIF2_DAC_LEFT_VOLUME,
  522. WM8994_AIF2DAC_VU_MASK | WM8994_AIF2DACL_VOL_MASK,
  523. WM8994_AIF2DAC_VU | 0xff);
  524. ret |= wm8994_update_bits(WM8994_AIF2_DAC_RIGHT_VOLUME,
  525. WM8994_AIF2DAC_VU_MASK | WM8994_AIF2DACR_VOL_MASK,
  526. WM8994_AIF2DAC_VU | 0xff);
  527. ret |= wm8994_update_bits(WM8994_DAC1_LEFT_VOLUME,
  528. WM8994_DAC1_VU_MASK | WM8994_DAC1L_VOL_MASK |
  529. WM8994_DAC1L_MUTE_MASK, WM8994_DAC1_VU | 0xc0);
  530. ret |= wm8994_update_bits(WM8994_DAC1_RIGHT_VOLUME,
  531. WM8994_DAC1_VU_MASK | WM8994_DAC1R_VOL_MASK |
  532. WM8994_DAC1R_MUTE_MASK, WM8994_DAC1_VU | 0xc0);
  533. /* Head Phone Volume */
  534. ret |= wm8994_i2c_write(WM8994_LEFT_OUTPUT_VOLUME, 0x12D);
  535. ret |= wm8994_i2c_write(WM8994_RIGHT_OUTPUT_VOLUME, 0x12D);
  536. if (ret < 0) {
  537. debug("%s: codec register access error\n", __func__);
  538. return -1;
  539. }
  540. return 0;
  541. }
  542. /*
  543. * Intialise wm8994 codec device
  544. *
  545. * @param wm8994 wm8994 information
  546. *
  547. * @returns -1 for error and 0 Success.
  548. */
  549. static int wm8994_device_init(struct wm8994_priv *wm8994)
  550. {
  551. const char *devname;
  552. unsigned short reg_data;
  553. int ret;
  554. wm8994_i2c_write(WM8994_SOFTWARE_RESET, WM8994_SW_RESET);/* Reset */
  555. ret = wm8994_i2c_read(WM8994_SOFTWARE_RESET, &reg_data);
  556. if (ret < 0) {
  557. debug("Failed to read ID register\n");
  558. goto err;
  559. }
  560. if (reg_data == WM8994_ID) {
  561. devname = "WM8994";
  562. debug("Device registered as type %d\n", wm8994->type);
  563. wm8994->type = WM8994;
  564. } else {
  565. debug("Device is not a WM8994, ID is %x\n", ret);
  566. ret = -1;
  567. goto err;
  568. }
  569. ret = wm8994_i2c_read(WM8994_CHIP_REVISION, &reg_data);
  570. if (ret < 0) {
  571. debug("Failed to read revision register: %d\n", ret);
  572. goto err;
  573. }
  574. wm8994->revision = reg_data;
  575. debug("%s revision %c\n", devname, 'A' + wm8994->revision);
  576. /* VMID Selection */
  577. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_1,
  578. WM8994_VMID_SEL_MASK | WM8994_BIAS_ENA_MASK, 0x3);
  579. /* Charge Pump Enable */
  580. ret |= wm8994_update_bits(WM8994_CHARGE_PUMP_1, WM8994_CP_ENA_MASK,
  581. WM8994_CP_ENA);
  582. /* Head Phone Power Enable */
  583. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_1,
  584. WM8994_HPOUT1L_ENA_MASK, WM8994_HPOUT1L_ENA);
  585. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_1,
  586. WM8994_HPOUT1R_ENA_MASK, WM8994_HPOUT1R_ENA);
  587. /* Power enable for AIF2 and DAC1 */
  588. ret |= wm8994_update_bits(WM8994_POWER_MANAGEMENT_5,
  589. WM8994_AIF2DACL_ENA_MASK | WM8994_AIF2DACR_ENA_MASK |
  590. WM8994_DAC1L_ENA_MASK | WM8994_DAC1R_ENA_MASK,
  591. WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA | WM8994_DAC1L_ENA |
  592. WM8994_DAC1R_ENA);
  593. /* Head Phone Initialisation */
  594. ret |= wm8994_update_bits(WM8994_ANALOGUE_HP_1,
  595. WM8994_HPOUT1L_DLY_MASK | WM8994_HPOUT1R_DLY_MASK,
  596. WM8994_HPOUT1L_DLY | WM8994_HPOUT1R_DLY);
  597. ret |= wm8994_update_bits(WM8994_DC_SERVO_1,
  598. WM8994_DCS_ENA_CHAN_0_MASK |
  599. WM8994_DCS_ENA_CHAN_1_MASK , WM8994_DCS_ENA_CHAN_0 |
  600. WM8994_DCS_ENA_CHAN_1);
  601. ret |= wm8994_update_bits(WM8994_ANALOGUE_HP_1,
  602. WM8994_HPOUT1L_DLY_MASK |
  603. WM8994_HPOUT1R_DLY_MASK | WM8994_HPOUT1L_OUTP_MASK |
  604. WM8994_HPOUT1R_OUTP_MASK |
  605. WM8994_HPOUT1L_RMV_SHORT_MASK |
  606. WM8994_HPOUT1R_RMV_SHORT_MASK, WM8994_HPOUT1L_DLY |
  607. WM8994_HPOUT1R_DLY | WM8994_HPOUT1L_OUTP |
  608. WM8994_HPOUT1R_OUTP | WM8994_HPOUT1L_RMV_SHORT |
  609. WM8994_HPOUT1R_RMV_SHORT);
  610. /* MIXER Config DAC1 to HP */
  611. ret |= wm8994_update_bits(WM8994_OUTPUT_MIXER_1,
  612. WM8994_DAC1L_TO_HPOUT1L_MASK, WM8994_DAC1L_TO_HPOUT1L);
  613. ret |= wm8994_update_bits(WM8994_OUTPUT_MIXER_2,
  614. WM8994_DAC1R_TO_HPOUT1R_MASK, WM8994_DAC1R_TO_HPOUT1R);
  615. /* Routing AIF2 to DAC1 */
  616. ret |= wm8994_update_bits(WM8994_DAC1_LEFT_MIXER_ROUTING,
  617. WM8994_AIF2DACL_TO_DAC1L_MASK,
  618. WM8994_AIF2DACL_TO_DAC1L);
  619. ret |= wm8994_update_bits(WM8994_DAC1_RIGHT_MIXER_ROUTING,
  620. WM8994_AIF2DACR_TO_DAC1R_MASK,
  621. WM8994_AIF2DACR_TO_DAC1R);
  622. /* GPIO Settings for AIF2 */
  623. /* B CLK */
  624. ret |= wm8994_update_bits(WM8994_GPIO_3, WM8994_GPIO_DIR_MASK |
  625. WM8994_GPIO_FUNCTION_MASK ,
  626. WM8994_GPIO_DIR_OUTPUT |
  627. WM8994_GPIO_FUNCTION_I2S_CLK);
  628. /* LR CLK */
  629. ret |= wm8994_update_bits(WM8994_GPIO_4, WM8994_GPIO_DIR_MASK |
  630. WM8994_GPIO_FUNCTION_MASK,
  631. WM8994_GPIO_DIR_OUTPUT |
  632. WM8994_GPIO_FUNCTION_I2S_CLK);
  633. /* DATA */
  634. ret |= wm8994_update_bits(WM8994_GPIO_5, WM8994_GPIO_DIR_MASK |
  635. WM8994_GPIO_FUNCTION_MASK,
  636. WM8994_GPIO_DIR_OUTPUT |
  637. WM8994_GPIO_FUNCTION_I2S_CLK);
  638. ret |= wm8994_init_volume_aif2_dac1();
  639. if (ret < 0)
  640. goto err;
  641. debug("%s: Codec chip init ok\n", __func__);
  642. return 0;
  643. err:
  644. debug("%s: Codec chip init error\n", __func__);
  645. return -1;
  646. }
  647. /*wm8994 Device Initialisation */
  648. int wm8994_init(struct sound_codec_info *pcodec_info,
  649. enum en_audio_interface aif_id,
  650. int sampling_rate, int mclk_freq,
  651. int bits_per_sample, unsigned int channels)
  652. {
  653. int ret = 0;
  654. /* shift the device address by 1 for 7 bit addressing */
  655. g_wm8994_i2c_dev_addr = pcodec_info->i2c_dev_addr;
  656. wm8994_i2c_init(pcodec_info->i2c_bus);
  657. if (pcodec_info->codec_type == CODEC_WM_8994)
  658. g_wm8994_info.type = WM8994;
  659. else {
  660. debug("%s: Codec id [%d] not defined\n", __func__,
  661. pcodec_info->codec_type);
  662. return -1;
  663. }
  664. ret = wm8994_device_init(&g_wm8994_info);
  665. if (ret < 0) {
  666. debug("%s: wm8994 codec chip init failed\n", __func__);
  667. return ret;
  668. }
  669. ret = wm8994_set_sysclk(&g_wm8994_info, aif_id, WM8994_SYSCLK_MCLK1,
  670. mclk_freq);
  671. if (ret < 0) {
  672. debug("%s: wm8994 codec set sys clock failed\n", __func__);
  673. return ret;
  674. }
  675. ret = wm8994_hw_params(&g_wm8994_info, aif_id, sampling_rate,
  676. bits_per_sample, channels);
  677. if (ret == 0) {
  678. ret = wm8994_set_fmt(aif_id, SND_SOC_DAIFMT_I2S |
  679. SND_SOC_DAIFMT_NB_NF |
  680. SND_SOC_DAIFMT_CBS_CFS);
  681. }
  682. return ret;
  683. }