wm8994.c 21 KB

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