es7243e.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include "FreeRTOS.h"
  6. #include "chip.h"
  7. #include "board.h"
  8. #include "soc-dai.h"
  9. #if AUDIO_CODEC_ADC_IC == AUDIO_CODEC_ADC_ES7243E
  10. /* Slave device address */
  11. #define ES7243E_SLAVE_ADDR 0x10
  12. typedef enum adc_data_select {
  13. ADC_OUTPUT_L_R,
  14. ADC_OUTPUT_L_L,
  15. ADC_OUTPUT_R_R,
  16. ADC_OUTPUT_R_L
  17. } adc_data_sel;
  18. typedef enum adc_polarity_invert {
  19. ADC_INV_NORMAL,
  20. ADC_R_INV,
  21. ADC_L_INV,
  22. ADC_LR_INV
  23. } adc_pol_inv;
  24. struct _coeff_div {
  25. u32 mclk; //mclk frequency
  26. u32 sr_rate; //sample rate
  27. u8 osr; //adc over sample rate
  28. u8 prediv_premulti; //adcclk and dacclk divider
  29. u8 cf_dsp_div; //adclrck divider and daclrck divider
  30. u8 scale;
  31. u8 lrckdiv_h;
  32. u8 lrckdiv_l;
  33. u8 bclkdiv; //sclk divider
  34. };
  35. typedef struct _adc_private {
  36. adc_data_sel adc_data_sel;
  37. adc_pol_inv adc_inv;
  38. } adc_private;
  39. typedef struct es7243e_private_data {
  40. struct i2c_adapter *adap;
  41. unsigned short slave_addr;
  42. int rates;
  43. int channels;
  44. int bits;
  45. int master;
  46. int mclk;
  47. int fmt;
  48. int mute;
  49. adc_private adc_priv;
  50. } es7243e_client;
  51. static int es7243e_mute(void *codec_prvdata, struct snd_pcm_substream *substream, int mute);
  52. static es7243e_client *g_es7243e_client = NULL;
  53. /**
  54. * es7243e_i2c_read - read data from a register of the i2c slave device.
  55. *
  56. * @adap: i2c device.
  57. * @reg: the register to read from.
  58. * @buf: raw write data buffer.
  59. * @len: length of the buffer to write
  60. */
  61. static int es7243e_i2c_read(es7243e_client *client, uint8_t reg)
  62. {
  63. struct i2c_msg msgs[2];
  64. uint8_t wbuf[2];
  65. uint8_t rbuf = 0;
  66. int ret;
  67. int i;
  68. wbuf[0] = reg;
  69. msgs[0].flags = 0;
  70. msgs[0].addr = client->slave_addr;
  71. msgs[0].len = 1;
  72. msgs[0].buf = wbuf;
  73. msgs[1].flags = I2C_M_RD;
  74. msgs[1].addr = client->slave_addr;
  75. msgs[1].len = 1;
  76. msgs[1].buf = &rbuf;
  77. for(i=0; i<5; i++)
  78. {
  79. ret = i2c_transfer(client->adap, msgs, 2);
  80. if(ret == 2)
  81. break;
  82. }
  83. return (ret==ARRAY_SIZE(msgs)) ? rbuf : -EIO;
  84. }
  85. /**
  86. * es7243e_i2c_write - write data to a register of the i2c slave device.
  87. *
  88. * @adap: i2c device.
  89. * @reg: the register to write to.
  90. * @buf: raw data buffer to write.
  91. * @len: length of the buffer to write
  92. */
  93. static int es7243e_i2c_write(es7243e_client *client, uint8_t reg, uint8_t val)
  94. {
  95. struct i2c_msg msg;
  96. uint8_t addr_buf[2];
  97. int ret;
  98. int i;
  99. addr_buf[0] = reg;
  100. addr_buf[1] = val;
  101. msg.flags = 0;
  102. msg.addr = client->slave_addr;
  103. msg.buf = addr_buf;
  104. msg.len = 2;
  105. for(i=0; i<5; i++)
  106. {
  107. ret = i2c_transfer(client->adap, &msg, 1);
  108. if(ret == 1)
  109. break;
  110. }
  111. //printf("reg[0x%x], val:0x%x, read:0x%x\n", reg, val, es7243e_i2c_read(client, reg));
  112. return (ret != 1 ? -EIO : 0);
  113. }
  114. int es7243e_i2c_write_bits(es7243e_client *client, u8 reg, u8 mask, u8 offset, u8 val)
  115. {
  116. int value = es7243e_i2c_read(client, reg);
  117. if(value < 0)
  118. {
  119. printf("%s read failed, reg:0x%x, val:0x%x\n", __func__, reg, value);
  120. return -1;
  121. }
  122. value &= ~(mask << offset);
  123. value |= (val << offset);
  124. if(es7243e_i2c_write(client, reg, value&0xFF))
  125. {
  126. printf("%s write failed, reg:0x%x, val:0x%x\n", __func__, reg, value);
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. /* codec mclk clock divider coefficients */
  132. static const struct _coeff_div coeff_div[] = {
  133. //mclk lrck, osr, pre, div ,scale, lrckdiv_h, lrckdiv_l, bclkdiv
  134. /* 24.576MHZ */
  135. {24576000, 8000 , 0x20, 0x50, 0x00, 0x00, 0x0b, 0xff, 0x2f},
  136. {24576000, 12000, 0x20, 0x30, 0x00, 0x00, 0x07, 0xff, 0x1f},
  137. {24576000, 16000, 0x20, 0x20, 0x00, 0x00, 0x05, 0xff, 0x17},
  138. {24576000, 24000, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  139. {24576000, 32000, 0x20, 0x21, 0x00, 0x00, 0x02, 0xff, 0x0b},
  140. {24576000, 48000, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  141. /* 12.288MHZ */
  142. {12288000, 8000 , 0x20, 0x20, 0x00, 0x00, 0x05, 0xff, 0x17},
  143. {12288000, 12000, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  144. {12288000, 16000, 0x20, 0x21, 0x00, 0x00, 0x02, 0xff, 0x0b},
  145. {12288000, 24000, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  146. {12288000, 32000, 0x20, 0x22, 0x00, 0x00, 0x01, 0x7f, 0x05},
  147. {12288000, 48000, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  148. /* 6.144MHZ */
  149. {6144000 , 8000 , 0x20, 0x21, 0x00, 0x00, 0x02, 0xff, 0x0b},
  150. {6144000 , 12000, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  151. {6144000 , 16000, 0x20, 0x22, 0x00, 0x00, 0x01, 0x7f, 0x05},
  152. {6144000 , 24000, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  153. {6144000 , 32000, 0x20, 0x23, 0x00, 0x00, 0x00, 0xbf, 0x02},
  154. {6144000 , 48000, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  155. /* 3.072MHZ */
  156. {3072000 , 8000 , 0x20, 0x22, 0x00, 0x00, 0x01, 0x7f, 0x05},
  157. {3072000 , 12000, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  158. {3072000 , 16000, 0x20, 0x23, 0x00, 0x00, 0x00, 0xbf, 0x02},
  159. {3072000 , 24000, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  160. {3072000 , 32000, 0x10, 0x03, 0x20, 0x04, 0x00, 0x5f, 0x02},
  161. {3072000 , 48000, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  162. /* 1.536MHZ */
  163. {1536000 , 8000 , 0x20, 0x23, 0x00, 0x00, 0x00, 0xbf, 0x02},
  164. {1536000 , 12000, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  165. {1536000 , 16000, 0x10, 0x03, 0x20, 0x04, 0x00, 0x5f, 0x00},
  166. {1536000 , 24000, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  167. /* 32.768MHZ */
  168. {32768000, 8000 , 0x20, 0x70, 0x00, 0x00, 0x0f, 0xff, 0x3f},
  169. {32768000, 16000, 0x20, 0x30, 0x00, 0x00, 0x07, 0xff, 0x1f},
  170. {32768000, 32000, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  171. /* 16.384MHZ */
  172. {16384000, 8000 , 0x20, 0x30, 0x00, 0x00, 0x07, 0xff, 0x1f},
  173. {16384000, 16000, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  174. {16384000, 32000, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  175. /* 8.192MHZ */
  176. {8192000 , 8000 , 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  177. {8192000 , 16000, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  178. {8192000 , 32000, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  179. /* 4.096MHZ */
  180. {4096000 , 8000 , 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  181. {4096000 , 16000, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  182. {4096000 , 32000, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  183. /* 2.048MHZ */
  184. {2048000 , 8000 , 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  185. {2048000 , 16000, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  186. {2048000 , 32000, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  187. /* 1.024MHZ */
  188. {1024000 , 8000 , 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  189. {1024000 , 16000, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  190. /* 22.5792MHz */
  191. {22579200, 11025, 0x20, 0x30, 0x00, 0x00, 0x07, 0xff, 0x1f},
  192. {22579200, 22050, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  193. {22579200, 44100, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  194. /* 11.2896MHz */
  195. {11289600, 11025, 0x20, 0x10, 0x00, 0x00, 0x03, 0xff, 0x0f},
  196. {11289600, 22050, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  197. {11289600, 44100, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  198. /* 5.6448MHz */
  199. {56448000, 11025, 0x20, 0x00, 0x00, 0x00, 0x01, 0xff, 0x07},
  200. {56448000, 22050, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  201. {56448000, 44100, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  202. /* 2.8224MHz */
  203. {28224000, 11025, 0x20, 0x01, 0x00, 0x00, 0x00, 0xff, 0x03},
  204. {28224000, 22050, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  205. {28224000, 44100, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  206. /* 1.4112MHz */
  207. {14112000, 11025, 0x20, 0x02, 0x00, 0x00, 0x00, 0x7f, 0x01},
  208. {14112000, 22050, 0x20, 0x03, 0x00, 0x00, 0x00, 0x3f, 0x00},
  209. };
  210. static inline int get_coeff(int mclk, int rate)
  211. {
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  214. if (coeff_div[i].sr_rate == rate && coeff_div[i].mclk == mclk) {
  215. printf("%s() get_coeff find index:%d\n", __func__, i);
  216. return i;
  217. }
  218. }
  219. printf("%s() get_coeff not find\n", __func__);
  220. return -EINVAL;
  221. }
  222. static int es7243e_pcm_hw_params(es7243e_client *client)
  223. {
  224. u8 regv;
  225. int coeff;
  226. #if 1
  227. coeff = get_coeff(client->mclk, client->rates);
  228. if(coeff < 0)
  229. {
  230. printf("Unable to configure sample rate %dHz with %dHz MCLK", client->rates, client->mclk);
  231. return coeff;
  232. }
  233. /*
  234. * set clock parameters
  235. */
  236. //printf("%s():coeff = %d\n",__func__, coeff);
  237. if(coeff >= 0)
  238. {
  239. es7243e_i2c_write(client, 0x03, coeff_div[coeff].osr);
  240. es7243e_i2c_write(client, 0x04, coeff_div[coeff].prediv_premulti);
  241. es7243e_i2c_write(client, 0x05, coeff_div[coeff].cf_dsp_div);
  242. es7243e_i2c_write_bits(client, 0x0D, 0x07, 0, coeff_div[coeff].scale);
  243. es7243e_i2c_write(client, 0x03, coeff_div[coeff].osr);
  244. es7243e_i2c_write_bits(client, 0x07, 0x0F, 0, coeff_div[coeff].lrckdiv_h & 0x0f);
  245. es7243e_i2c_write(client, 0x06, coeff_div[coeff].bclkdiv);
  246. //es7243e_i2c_write(client, 0x06, 0x07);//BCLK:1.536
  247. }
  248. #endif
  249. #if 1//add-
  250. regv = es7243e_i2c_read(client, 0x0B);
  251. regv &= 0xe3;
  252. switch (client->bits)
  253. {
  254. case 16:
  255. regv |= 0x0c;
  256. break;
  257. case 20:
  258. regv |= 0x04;
  259. break;
  260. case 24:
  261. break;
  262. case 32:
  263. regv |= 0x10;
  264. break;
  265. default:
  266. regv |= 0x0c;
  267. break;
  268. }
  269. es7243e_i2c_write(client, 0x0B, regv);
  270. /*
  271. * unmute SDP for MT8516 and MT8167 platform
  272. */
  273. msleep(500);
  274. es7243e_mute(NULL, NULL, 1);
  275. #endif
  276. return 0;
  277. }
  278. static int es7243e_set_bias_level(es7243e_client *client, enum snd_soc_bias_level level)
  279. {
  280. u8 regv;
  281. switch (level) {
  282. case SND_SOC_BIAS_ON:
  283. printf("%s on\n", __func__);
  284. break;
  285. case SND_SOC_BIAS_PREPARE:
  286. printf("%s prepare\n", __func__);
  287. break;
  288. case SND_SOC_BIAS_STANDBY:
  289. printf("%s standby\n", __func__);
  290. /*
  291. * enable clock
  292. */
  293. regv = es7243e_i2c_read(client, 0x01);
  294. regv |= 0x0A;
  295. es7243e_i2c_write(client, 0x01, regv);
  296. es7243e_i2c_write(client, 0x16, 0x00); //power up analog
  297. /*
  298. * enable mic input 1
  299. */
  300. regv = es7243e_i2c_read(client, 0x20);
  301. regv |= 0x10;
  302. es7243e_i2c_write(client, 0x20, regv);
  303. /*
  304. * enable mic input 2
  305. */
  306. regv = es7243e_i2c_read(client, 0x21);
  307. regv |= 0x10;
  308. es7243e_i2c_write(client, 0x21, regv);
  309. msleep(100);
  310. es7243e_mute(NULL, NULL, 0);
  311. break;
  312. case SND_SOC_BIAS_OFF:
  313. printf("%s off\n", __func__);
  314. es7243e_mute(NULL, NULL, 1);
  315. /*
  316. * disable mic input 1
  317. */
  318. regv = es7243e_i2c_read(client, 0x20);
  319. regv &= 0xef;
  320. es7243e_i2c_write(client, 0x20, regv);
  321. /*
  322. * disable mic input 2
  323. */
  324. regv = es7243e_i2c_read(client, 0x21);
  325. regv &= 0xef;
  326. es7243e_i2c_write(client, 0x21, regv);
  327. es7243e_i2c_write(client, 0x16, 0xff); //power down analog
  328. /*
  329. * disable clock
  330. */
  331. regv = es7243e_i2c_read(client, 0x01);
  332. regv &= 0xf5;
  333. es7243e_i2c_write(client, 0x01, regv);
  334. break;
  335. }
  336. return 0;
  337. }
  338. static int es7243e_init_regs(es7243e_client *client)
  339. {
  340. if(client->master)
  341. {
  342. //ES7243e master mode
  343. es7243e_i2c_write(client, 0x01, 0x3A);
  344. es7243e_i2c_write(client, 0x00, 0x80);
  345. es7243e_i2c_write(client, 0x04, 0x02);
  346. es7243e_i2c_write(client, 0x04, 0x01);
  347. es7243e_i2c_write(client, 0xF9, 0x01);
  348. es7243e_i2c_write(client, 0x00, 0x1E);
  349. es7243e_i2c_write(client, 0x01, 0x00);
  350. es7243e_i2c_write(client, 0x02, 0x00); //default:0x00 (0x01 :BCLK invert)
  351. es7243e_i2c_write(client, 0x03, 0x20);
  352. //es7243e_i2c_write(client, 0x0D, 0x40);//default:0x00 (settable:0x40 0x80)
  353. es7243e_i2c_write_bits(client, 0x0D, 0x0f, 4, (client->adc_priv.adc_data_sel<<2) | client->adc_priv.adc_inv);
  354. es7243e_i2c_write(client, 0xF9, 0x00);
  355. es7243e_i2c_write(client, 0x04, 0x02);
  356. es7243e_i2c_write(client, 0x04, 0x01);
  357. es7243e_i2c_write(client, 0x05, 0x00);
  358. //es7243e_i2c_write(client, 0x06, 0x07);//test: set BCLK 1.536M
  359. es7243e_i2c_write(client, 0x07, 0x00);//default:0x00 0x02
  360. es7243e_i2c_write(client, 0x08, 0xFF);//default:0xFF
  361. es7243e_i2c_write(client, 0x09, 0xC5);
  362. es7243e_i2c_write(client, 0x0A, 0x81);
  363. es7243e_i2c_write(client, 0x0B, 0x0C);//default:0x00(24bit) 0x0c(16 bit) add:0x0d 20210722
  364. es7243e_i2c_write(client, 0x0E, 0xBF);
  365. es7243e_i2c_write(client, 0x0F, 0x80);
  366. es7243e_i2c_write(client, 0x14, 0x0C);
  367. es7243e_i2c_write(client, 0x15, 0x0C);
  368. es7243e_i2c_write(client, 0x17, 0x02);
  369. es7243e_i2c_write(client, 0x18, 0x26);
  370. es7243e_i2c_write(client, 0x19, 0x77);
  371. es7243e_i2c_write(client, 0x1A, 0xF4);
  372. es7243e_i2c_write(client, 0x1B, 0x66);
  373. es7243e_i2c_write(client, 0x1C, 0x44);
  374. es7243e_i2c_write(client, 0x1E, 0x00);
  375. es7243e_i2c_write(client, 0x1F, 0x0C);
  376. es7243e_i2c_write(client, 0x20, 0x1c);//default:0x19 Analog gain(min:0x01 max:0x1e)
  377. es7243e_i2c_write(client, 0x21, 0x1c);//default:0x19 Analog gain(min:0x01 max:0x1e)
  378. es7243e_i2c_write(client, 0x00, 0xC0);
  379. es7243e_i2c_write(client, 0x01, 0x3A);
  380. es7243e_i2c_write(client, 0x16, 0x3F);
  381. es7243e_i2c_write(client, 0x16, 0x00);
  382. es7243e_i2c_write(client, 0x13, 0x3f);//add
  383. es7243e_i2c_write(client, 0x0f, 0x40);//add
  384. }
  385. else
  386. {
  387. //ES7243e slave mode
  388. es7243e_i2c_write(client, 0x01, 0x3A);
  389. es7243e_i2c_write(client, 0x00, 0x80);
  390. es7243e_i2c_write(client, 0x04, 0x02);
  391. es7243e_i2c_write(client, 0x04, 0x01);
  392. es7243e_i2c_write(client, 0xF9, 0x01);
  393. es7243e_i2c_write(client, 0x00, 0x1E);
  394. es7243e_i2c_write(client, 0x01, 0x00);
  395. es7243e_i2c_write(client, 0x02, 0x00);
  396. es7243e_i2c_write(client, 0x03, 0x20);
  397. //es7243e_i2c_write(client, 0x0D, 0x00);
  398. es7243e_i2c_write_bits(client, 0x0D, 0x0f, 4, (client->adc_priv.adc_data_sel<<2) | client->adc_priv.adc_inv);
  399. es7243e_i2c_write(client, 0xF9, 0x00);
  400. es7243e_i2c_write(client, 0x04, 0x02);
  401. es7243e_i2c_write(client, 0x04, 0x01);
  402. es7243e_i2c_write(client, 0x05, 0x00);
  403. es7243e_i2c_write(client, 0x07, 0x00);//add
  404. es7243e_i2c_write(client, 0x09, 0xC5);
  405. es7243e_i2c_write(client, 0x0A, 0x81);
  406. es7243e_i2c_write(client, 0x0b, 0x00);
  407. es7243e_i2c_write(client, 0x0E, 0xBF);
  408. es7243e_i2c_write(client, 0x0F, 0x80);
  409. es7243e_i2c_write(client, 0x14, 0x0C);
  410. es7243e_i2c_write(client, 0x15, 0x0C);
  411. es7243e_i2c_write(client, 0x17, 0x02);//add
  412. es7243e_i2c_write(client, 0x18, 0x26);
  413. es7243e_i2c_write(client, 0x19, 0x77);
  414. es7243e_i2c_write(client, 0x1A, 0xF4);
  415. es7243e_i2c_write(client, 0x1B, 0x66);
  416. es7243e_i2c_write(client, 0x1C, 0x44);
  417. es7243e_i2c_write(client, 0x1E, 0x00);
  418. es7243e_i2c_write(client, 0x1F, 0x0C);
  419. es7243e_i2c_write(client, 0x20, 0x19);
  420. es7243e_i2c_write(client, 0x21, 0x19);
  421. es7243e_i2c_write(client, 0x00, 0x80);
  422. es7243e_i2c_write(client, 0x01, 0x3A);
  423. es7243e_i2c_write(client, 0x16, 0x3F);
  424. es7243e_i2c_write(client, 0x16, 0x00);
  425. }
  426. return 0;
  427. }
  428. static int es7243e_set_fmt(void *codec_prvdata, unsigned int fmt)
  429. {
  430. u8 iface = 0;
  431. u8 adciface = 0;
  432. u8 clksel = 0;
  433. es7243e_client *client = g_es7243e_client;
  434. if(!client)
  435. {
  436. printf("%s(), Invalid client\n", __func__);
  437. return -1;
  438. }
  439. adciface = es7243e_i2c_read(client, 0x0B); //get interface format
  440. iface = es7243e_i2c_read(client, 0x00); //get spd interface
  441. clksel = es7243e_i2c_read(client, 0x02); //get spd interface
  442. /* set master/slave audio interface */
  443. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK)
  444. {
  445. case SND_SOC_DAIFMT_CBM_CFM: // MASTER MODE
  446. printf("Enter into %s(), I2S master\n", __func__);
  447. iface |= 0x40;
  448. break;
  449. case SND_SOC_DAIFMT_CBS_CFS: // SLAVE MODE
  450. printf("Enter into %s(), I2S slave\n", __func__);
  451. iface &= 0xbf;
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. /* interface format */
  457. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK)
  458. {
  459. case SND_SOC_DAIFMT_I2S:
  460. printf("Enter into %s(), I2S format \n", __func__);
  461. adciface &= 0xFC;
  462. break;
  463. case SND_SOC_DAIFMT_RIGHT_J:
  464. return -EINVAL;
  465. case SND_SOC_DAIFMT_LEFT_J:
  466. printf("Enter into %s(), LJ format \n", __func__);
  467. adciface &= 0xFC;
  468. adciface |= 0x01;
  469. break;
  470. case SND_SOC_DAIFMT_DSP_A:
  471. printf("Enter into %s(), DSP-A format \n", __func__);
  472. adciface &= 0xDC;
  473. adciface |= 0x03;
  474. break;
  475. case SND_SOC_DAIFMT_DSP_B:
  476. printf("Enter into %s(), DSP-B format \n", __func__);
  477. adciface &= 0xDC;
  478. adciface |= 0x23;
  479. break;
  480. default:
  481. return -EINVAL;
  482. }
  483. /* clock inversion */
  484. switch (fmt & SND_SOC_DAIFMT_INV_MASK)
  485. {
  486. case SND_SOC_DAIFMT_NB_NF:
  487. printf("Enter into %s(), BCLK noinvert, MCLK noinvert \n", __func__);
  488. adciface &= 0xdf;
  489. clksel &= 0xfe;
  490. break;
  491. case SND_SOC_DAIFMT_IB_IF:
  492. printf("Enter into %s(), BCLK invert, MCLK invert \n", __func__);
  493. adciface |= 0x20;
  494. clksel |= 0x01;
  495. break;
  496. case SND_SOC_DAIFMT_IB_NF:
  497. printf("Enter into %s(), BCLK invert, MCLK noinvert \n", __func__);
  498. adciface &= 0xdf;
  499. clksel |= 0x01;
  500. break;
  501. case SND_SOC_DAIFMT_NB_IF:
  502. printf("Enter into %s(), BCLK noinvert, MCLK invert \n", __func__);
  503. adciface |= 0x20;
  504. clksel &= 0xfe;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. es7243e_i2c_write(client, 0x00, iface);
  510. es7243e_i2c_write(client, 0x02, clksel);
  511. es7243e_i2c_write(client, 0x0B, adciface);
  512. return 0;
  513. }
  514. static int es7243e_mute(void *codec_prvdata, struct snd_pcm_substream *substream, int mute)
  515. {
  516. es7243e_client *client = g_es7243e_client;
  517. if(!client)
  518. {
  519. printf("%s(), Invalid client\n", __func__);
  520. return -1;
  521. }
  522. printf("%s %d\n", __func__, mute);
  523. if (mute)
  524. {
  525. es7243e_i2c_write_bits(client, 0x0B, 0x3, 6, 0x3);
  526. client->mute = 1;
  527. }
  528. else
  529. {
  530. es7243e_i2c_write_bits(client, 0x0B, 0x3, 6, 0x0);
  531. client->mute = 0;
  532. }
  533. return 0;
  534. }
  535. static int es7243e_set_mclk(void *codec_prvdata, int freq, int dir)
  536. {
  537. return 0;
  538. }
  539. static int es7243e_hw_params(void *codec_prvdata, struct snd_pcm_substream *substream, struct snd_soc_hw_params *params)
  540. {
  541. es7243e_client *client = g_es7243e_client;
  542. if(!client)
  543. {
  544. printf("%s(), Invalid client\n", __func__);
  545. return -1;
  546. }
  547. if(!params)
  548. {
  549. printf("%s(), Invalid params\n", __func__);
  550. return -1;
  551. }
  552. client->rates = params->rates;
  553. client->channels = params->channels;
  554. client->bits = params->bits;
  555. client->mclk = client->rates * 256;
  556. es7243e_set_bias_level(client, SND_SOC_BIAS_OFF);
  557. es7243e_pcm_hw_params(client);
  558. return 0;
  559. }
  560. static int es7243e_startup(void *codec_prvdata, struct snd_pcm_substream *substream, int start)
  561. {
  562. es7243e_client *client = g_es7243e_client;
  563. if(!client)
  564. {
  565. printf("%s(), Invalid client\n", __func__);
  566. return -1;
  567. }
  568. if(start)
  569. es7243e_set_bias_level(client, SND_SOC_BIAS_STANDBY);
  570. else
  571. es7243e_set_bias_level(client, SND_SOC_BIAS_OFF);
  572. return 0;
  573. }
  574. static int es7243e_hw_init(void *codec_prvdata, int master)
  575. {
  576. es7243e_client *client = g_es7243e_client;
  577. if(!client)
  578. {
  579. printf("%s(), Invalid client\n", __func__);
  580. return -1;
  581. }
  582. client->master = !!master;
  583. client->fmt = SND_SOC_DAIFMT_I2S; //interface type.
  584. client->fmt |= SND_SOC_DAIFMT_NB_NF; //normal bclk + normal frame.
  585. client->fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  586. if(client->master)
  587. client->fmt |= SND_SOC_DAIFMT_CBM_CFM; //master
  588. else
  589. client->fmt |= SND_SOC_DAIFMT_CBS_CFS; //slave
  590. client->adc_priv.adc_data_sel = ADC_OUTPUT_L_R;
  591. client->adc_priv.adc_inv = ADC_INV_NORMAL;
  592. es7243e_init_regs(client);
  593. es7243e_set_fmt(NULL, client->fmt);
  594. return 0;
  595. }
  596. static int es7243e_reset(es7243e_client *client)
  597. {
  598. return 0;
  599. }
  600. static int es7243e_probe(struct i2c_adapter *adap)
  601. {
  602. es7243e_client *client;
  603. struct codec_register_inf codec_register;
  604. struct snd_soc_dai_ops *ops = NULL;
  605. client = pvPortMalloc(sizeof(es7243e_client));
  606. if (!client) {
  607. printf("%s, pvPortMalloc fail.\n", __func__);
  608. return -ENOMEM;
  609. }
  610. memset(client, 0, sizeof(es7243e_client));
  611. ops = (struct snd_soc_dai_ops *)pvPortMalloc(sizeof(struct snd_soc_dai_ops));
  612. if (!ops) {
  613. vPortFree((void *)client);
  614. printf("%s, pvPortMalloc fail.\n", __func__);
  615. return -ENOMEM;
  616. }
  617. memset(ops, 0, sizeof(struct snd_soc_dai_ops));
  618. g_es7243e_client = client;
  619. client->adap = adap;
  620. client->slave_addr = ES7243E_SLAVE_ADDR;
  621. ops->init = es7243e_hw_init;
  622. ops->hw_params = es7243e_hw_params;
  623. ops->startup = es7243e_startup;
  624. ops->set_fmt = es7243e_set_fmt;
  625. ops->set_mute = es7243e_mute;
  626. ops->set_mclk = es7243e_set_mclk;
  627. codec_register.name = "ES7243E";
  628. codec_register.codec_type = TYPE_ENCODER;
  629. codec_register.ops = ops;
  630. codec_register.codec_prvdata = NULL;
  631. audio_codec_register(codec_register);
  632. es7243e_reset(client);
  633. es7243e_mute(NULL, NULL, 1);
  634. return 0;
  635. }
  636. void es7243e_init(void)
  637. {
  638. struct i2c_adapter *adap = NULL;
  639. if (!(adap = i2c_open("i2c0"))) {
  640. printf("%s, open i2c0 fail.\n", __func__);
  641. return ;
  642. }
  643. es7243e_probe(adap);
  644. }
  645. #endif