wm8994.c 22 KB

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