wm8994.c 22 KB

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