es7210_config.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * ALSA SoC ES7210 adc driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.^M
  7. *
  8. * Notes:
  9. * ES7210 is a 4-ch ADC of Everest
  10. */
  11. static int es7210_read(u8 reg, u8 * rt_value, struct i2c_client *client)
  12. {
  13. int ret;
  14. u8 read_cmd[3] = { 0 };
  15. u8 cmd_len = 0;
  16. read_cmd[0] = reg;
  17. cmd_len = 1;
  18. if (client->adapter == NULL)
  19. printk("es7210_read client->adapter==NULL\n");
  20. ret = i2c_master_send(client, read_cmd, cmd_len);
  21. if (ret != cmd_len) {
  22. printk("es7210_read error1\n");
  23. return -1;
  24. }
  25. ret = i2c_master_recv(client, rt_value, 1);
  26. if (ret != 1) {
  27. printk("es7210_read error2, ret = %d.\n", ret);
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. static int es7210_write(u8 reg, unsigned char value, struct i2c_client *client)
  33. {
  34. int ret = 0;
  35. u8 write_cmd[2] = { 0 };
  36. write_cmd[0] = reg;
  37. write_cmd[1] = value;
  38. ret = i2c_master_send(client, write_cmd, 2);
  39. if (ret != 2) {
  40. printk("es7210_write error->[REG-0x%02x,val-0x%02x]\n",
  41. reg, value);
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. static int es7210_update_bits(u8 reg, u8 mask, u8 value,
  47. struct i2c_client *client)
  48. {
  49. u8 val_old, val_new;
  50. es7210_read(reg, &val_old, client);
  51. val_new = (val_old & ~mask) | (value & mask);
  52. if (val_new != val_old) {
  53. es7210_write(reg, val_new, client);
  54. }
  55. return 0;
  56. }
  57. static int es7210_multi_chips_write(u8 reg, unsigned char value)
  58. {
  59. u8 i;
  60. for (i = 0; i < ADC_DEV_MAXNUM; i++) {
  61. es7210_write(reg, value, i2c_clt1[i]);
  62. }
  63. return 0;
  64. }
  65. static int es7210_multi_chips_update_bits(u8 reg, u8 mask, u8 value)
  66. {
  67. u8 i;
  68. for (i = 0; i < ADC_DEV_MAXNUM; i++) {
  69. es7210_update_bits(reg, mask, value, i2c_clt1[i]);
  70. }
  71. return 0;
  72. }
  73. /*
  74. * to initialize es7210 for tdm mode
  75. */
  76. static void es7210_tdm_init_codec(u8 mode)
  77. {
  78. int cnt, channel;
  79. for (cnt = 0;
  80. cnt < sizeof(es7210_tdm_reg_common_cfg1) /
  81. sizeof(es7210_tdm_reg_common_cfg1[0]); cnt++) {
  82. es7210_multi_chips_write(es7210_tdm_reg_common_cfg1
  83. [cnt].reg_addr,
  84. es7210_tdm_reg_common_cfg1
  85. [cnt].reg_v);
  86. }
  87. switch (mode) {
  88. case ES7210_TDM_1LRCK_DSPA:
  89. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  90. es7210_write(ES7210_SDP_CFG1_REG11,
  91. 0x63, i2c_clt1[cnt]);
  92. }
  93. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  94. es7210_write(ES7210_SDP_CFG2_REG12,
  95. 0x01, i2c_clt1[cnt]);
  96. }
  97. break;
  98. case ES7210_TDM_1LRCK_DSPB:
  99. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  100. es7210_write(ES7210_SDP_CFG1_REG11,
  101. 0x73, i2c_clt1[cnt]);
  102. }
  103. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  104. es7210_write(ES7210_SDP_CFG2_REG12,
  105. 0x01, i2c_clt1[cnt]);
  106. }
  107. break;
  108. case ES7210_TDM_1LRCK_I2S:
  109. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  110. es7210_write(ES7210_SDP_CFG1_REG11,
  111. 0x60, i2c_clt1[cnt]);
  112. }
  113. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  114. es7210_write(ES7210_SDP_CFG2_REG12,
  115. 0x02, i2c_clt1[cnt]);
  116. }
  117. break;
  118. case ES7210_TDM_1LRCK_LJ:
  119. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  120. es7210_write(ES7210_SDP_CFG1_REG11,
  121. 0x61, i2c_clt1[cnt]);
  122. }
  123. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  124. es7210_write(ES7210_SDP_CFG2_REG12,
  125. 0x02, i2c_clt1[cnt]);
  126. }
  127. break;
  128. case ES7210_TDM_NLRCK_DSPA:
  129. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  130. es7210_write(ES7210_SDP_CFG1_REG11,
  131. 0x63, i2c_clt1[cnt]);
  132. }
  133. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  134. if (cnt == 0) {
  135. es7210_write(ES7210_SDP_CFG2_REG12,
  136. 0x07, i2c_clt1[cnt]);
  137. } else {
  138. es7210_write(ES7210_SDP_CFG2_REG12,
  139. 0x03, i2c_clt1[cnt]);
  140. }
  141. }
  142. break;
  143. case ES7210_TDM_NLRCK_DSPB:
  144. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  145. es7210_write(ES7210_SDP_CFG1_REG11,
  146. 0x73, i2c_clt1[cnt]);
  147. }
  148. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  149. if (cnt == 0) {
  150. es7210_write(ES7210_SDP_CFG2_REG12,
  151. 0x07, i2c_clt1[cnt]);
  152. } else {
  153. es7210_write(ES7210_SDP_CFG2_REG12,
  154. 0x03, i2c_clt1[cnt]);
  155. }
  156. }
  157. break;
  158. case ES7210_TDM_NLRCK_I2S:
  159. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  160. es7210_write(ES7210_SDP_CFG1_REG11,
  161. 0x60, i2c_clt1[cnt]);
  162. }
  163. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  164. if (cnt == 0) {
  165. es7210_write(ES7210_SDP_CFG2_REG12,
  166. 0x07, i2c_clt1[cnt]);
  167. } else {
  168. es7210_write(ES7210_SDP_CFG2_REG12,
  169. 0x03, i2c_clt1[cnt]);
  170. }
  171. }
  172. break;
  173. case ES7210_TDM_NLRCK_LJ:
  174. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  175. es7210_write(ES7210_SDP_CFG1_REG11,
  176. 0x61, i2c_clt1[cnt]);
  177. }
  178. for (cnt = 0; cnt < ADC_DEV_MAXNUM; cnt++) {
  179. if (cnt == 0) {
  180. es7210_write(ES7210_SDP_CFG2_REG12,
  181. 0x07, i2c_clt1[cnt]);
  182. } else {
  183. es7210_write(ES7210_SDP_CFG2_REG12,
  184. 0x03, i2c_clt1[cnt]);
  185. }
  186. }
  187. break;
  188. case ES7210_NORMAL_I2S:
  189. es7210_multi_chips_write(ES7210_SDP_CFG1_REG11, 0x60);
  190. es7210_multi_chips_write(ES7210_SDP_CFG2_REG12, 0x00);
  191. break;
  192. case ES7210_NORMAL_LJ:
  193. es7210_multi_chips_write(ES7210_SDP_CFG1_REG11, 0x61);
  194. es7210_multi_chips_write(ES7210_SDP_CFG2_REG12, 0x00);
  195. break;
  196. case ES7210_NORMAL_DSPA:
  197. es7210_multi_chips_write(ES7210_SDP_CFG1_REG11, 0x63);
  198. es7210_multi_chips_write(ES7210_SDP_CFG2_REG12, 0x00);
  199. break;
  200. case ES7210_NORMAL_DSPB:
  201. es7210_multi_chips_write(ES7210_SDP_CFG1_REG11, 0x73);
  202. es7210_multi_chips_write(ES7210_SDP_CFG2_REG12, 0x00);
  203. break;
  204. default:
  205. es7210_multi_chips_write(ES7210_SDP_CFG1_REG11, 0x60);
  206. es7210_multi_chips_write(ES7210_SDP_CFG2_REG12, 0x00);
  207. break;
  208. }
  209. for (cnt = 0;
  210. cnt < sizeof(es7210_tdm_reg_common_cfg2) /
  211. sizeof(es7210_tdm_reg_common_cfg2[0]); cnt++) {
  212. es7210_multi_chips_write(es7210_tdm_reg_common_cfg2
  213. [cnt].reg_addr,
  214. es7210_tdm_reg_common_cfg2
  215. [cnt].reg_v);
  216. }
  217. switch (mode) {
  218. case ES7210_TDM_1LRCK_DSPA:
  219. case ES7210_TDM_1LRCK_DSPB:
  220. case ES7210_TDM_1LRCK_I2S:
  221. case ES7210_TDM_1LRCK_LJ:
  222. es7210_multi_chips_write(ES7210_MCLK_CTL_REG02, 0xc1);
  223. break;
  224. case ES7210_TDM_NLRCK_DSPA:
  225. case ES7210_TDM_NLRCK_DSPB:
  226. case ES7210_TDM_NLRCK_I2S:
  227. case ES7210_TDM_NLRCK_LJ:
  228. channel = ES7210_CHANNELS_MAX / 2;
  229. channel &= 0x0f;
  230. channel = channel << 4;
  231. printk("ADC Analg channel register = 0x%2x\n", channel);
  232. es7210_multi_chips_write(ES7210_MODE_CFG_REG08, channel);
  233. es7210_multi_chips_write(ES7210_MCLK_CTL_REG02, 0x01);
  234. break;
  235. default:
  236. es7210_multi_chips_write(ES7210_MCLK_CTL_REG02, 0x41);
  237. break;
  238. }
  239. for (cnt = 0; cnt < sizeof(es7210_tdm_reg_common_cfg3) /
  240. sizeof(es7210_tdm_reg_common_cfg3[0]); cnt++) {
  241. es7210_multi_chips_write(es7210_tdm_reg_common_cfg3
  242. [cnt].reg_addr,
  243. es7210_tdm_reg_common_cfg3
  244. [cnt].reg_v);
  245. }
  246. /*
  247. * Mute All ADC
  248. */
  249. es7210_multi_chips_update_bits(ES7210_ADC34_MUTE_REG14, 0x03, 0x03);
  250. es7210_multi_chips_update_bits(ES7210_ADC12_MUTE_REG15, 0x03, 0x03);
  251. /*
  252. * Set Direct DB Gain
  253. */
  254. es7210_multi_chips_update_bits(ES7210_ALC_COM_CFG2_REG1A, 0x03, 0x00);
  255. es7210_multi_chips_write(ES7210_ALC1_MAX_GAIN_REG1E, 0xBF);
  256. es7210_multi_chips_write(ES7210_ALC2_MAX_GAIN_REG1D, 0xBF);
  257. es7210_multi_chips_write(ES7210_ALC3_MAX_GAIN_REG1C, 0xBF);
  258. es7210_multi_chips_write(ES7210_ALC4_MAX_GAIN_REG1B, 0xBF);
  259. }