es8156.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include "FreeRTOS.h"
  5. #include "chip.h"
  6. #include "board.h"
  7. #include "soc-dai.h"
  8. #if AUDIO_CODEC_DAC_IC == AUDIO_CODEC_DAC_ES8156
  9. /* Reset Control */
  10. #define ES8156_RESET_REG00 0x00
  11. /* Clock Managerment */
  12. #define ES8156_MAINCLOCK_CTL_REG01 0x01
  13. #define ES8156_SCLK_MODE_REG02 0x02
  14. #define ES8156_LRCLK_DIV_H_REG03 0x03
  15. #define ES8156_LRCLK_DIV_L_REG04 0x04
  16. #define ES8156_SCLK_DIV_REG05 0x05
  17. #define ES8156_NFS_CONFIG_REG06 0x06
  18. #define ES8156_MISC_CONTROL1_REG07 0x07
  19. #define ES8156_CLOCK_ON_OFF_REG08 0x08
  20. #define ES8156_MISC_CONTROL2_REG09 0x09
  21. #define ES8156_TIME_CONTROL1_REG0A 0x0a
  22. #define ES8156_TIME_CONTROL2_REG0B 0x0b
  23. /* System Control */
  24. #define ES8156_CHIP_STATUS_REG0C 0x0c
  25. #define ES8156_P2S_CONTROL_REG0D 0x0d
  26. #define ES8156_DAC_OSR_COUNTER_REG10 0x10
  27. /* SDP Control */
  28. #define ES8156_DAC_SDP_REG11 0x11
  29. #define ES8156_AUTOMUTE_SET_REG12 0x12
  30. #define ES8156_DAC_MUTE_REG13 0x13
  31. #define ES8156_VOLUME_CONTROL_REG14 0x14
  32. /* ALC Control */
  33. #define ES8156_ALC_CONFIG1_REG15 0x15
  34. #define ES8156_ALC_CONFIG2_REG16 0x16
  35. #define ES8156_ALC_CONFIG3_REG17 0x17
  36. #define ES8156_MISC_CONTROL3_REG18 0x18
  37. #define ES8156_EQ_CONTROL1_REG19 0x19
  38. #define ES8156_EQ_CONTROL2_REG1A 0x1a
  39. /* Analog System Control */
  40. #define ES8156_ANALOG_SYS1_REG20 0x20
  41. #define ES8156_ANALOG_SYS2_REG21 0x21
  42. #define ES8156_ANALOG_SYS3_REG22 0x22
  43. #define ES8156_ANALOG_SYS4_REG23 0x23
  44. #define ES8156_ANALOG_LP_REG24 0x24
  45. #define ES8156_ANALOG_SYS5_REG25 0x25
  46. /* Chip Information */
  47. #define ES8156_I2C_PAGESEL_REGFC 0xFC
  48. #define ES8156_CHIPID1_REGFD 0xFD
  49. #define ES8156_CHIPID0_REGFE 0xFE
  50. #define ES8156_CHIP_VERSION_REGFF 0xFF
  51. /* Slave device address */
  52. #define ES8156_SLAVE_ADDR 0x09
  53. typedef struct es8156_private_data {
  54. struct i2c_adapter *adap;
  55. unsigned short slave_addr;
  56. int rates;
  57. int channels;
  58. int bits;
  59. int master;
  60. int mclk;
  61. int fmt;
  62. int mute;
  63. } es8156_client;
  64. #if 1
  65. static es8156_client *g_es8156_client = NULL;
  66. /**
  67. * es8156_i2c_read - read data from a register of the i2c slave device.
  68. *
  69. * @adap: i2c device.
  70. * @reg: the register to read from.
  71. * @buf: raw write data buffer.
  72. * @len: length of the buffer to write
  73. */
  74. static int es8156_i2c_read(es8156_client *client, uint8_t reg)
  75. {
  76. struct i2c_msg msgs[2];
  77. uint8_t wbuf[2];
  78. uint8_t rbuf = 0;
  79. int ret;
  80. int i;
  81. wbuf[0] = reg;
  82. msgs[0].flags = 0;
  83. msgs[0].addr = client->slave_addr;
  84. msgs[0].len = 1;
  85. msgs[0].buf = wbuf;
  86. msgs[1].flags = I2C_M_RD;
  87. msgs[1].addr = client->slave_addr;
  88. msgs[1].len = 1;
  89. msgs[1].buf = &rbuf;
  90. for(i=0; i<5; i++)
  91. {
  92. ret = i2c_transfer(client->adap, msgs, 2);
  93. if(ret == 2)
  94. break;
  95. }
  96. return (ret==ARRAY_SIZE(msgs)) ? rbuf : -EIO;
  97. }
  98. /**
  99. * es8156_i2c_write - write data to a register of the i2c slave device.
  100. *
  101. * @adap: i2c device.
  102. * @reg: the register to write to.
  103. * @buf: raw data buffer to write.
  104. * @len: length of the buffer to write
  105. */
  106. static int es8156_i2c_write(es8156_client *client, uint8_t reg, uint8_t val)
  107. {
  108. struct i2c_msg msg;
  109. uint8_t addr_buf[2];
  110. int ret;
  111. int i;
  112. addr_buf[0] = reg;
  113. addr_buf[1] = val;
  114. msg.flags = 0;
  115. msg.addr = client->slave_addr;
  116. msg.buf = addr_buf;
  117. msg.len = 2;
  118. for(i=0; i<5; i++)
  119. {
  120. ret = i2c_transfer(client->adap, &msg, 1);
  121. if(ret == 1)
  122. break;
  123. }
  124. printf("reg[0x%x], val:0x%x, read:0x%x\n", reg, val, es8156_i2c_read(client, reg));
  125. return (ret != 1 ? -EIO : 0);
  126. }
  127. int es8156_i2c_write_bits(es8156_client *client, u8 reg, u8 mask, u8 offset, u8 value)
  128. {
  129. int val = es8156_i2c_read(client, reg);
  130. if(val < 0)
  131. {
  132. printf("%s read failed, reg:0x%x, val:0x%x\n", __func__, reg, val);
  133. return -1;
  134. }
  135. val &= ~(mask << offset);
  136. val |= (value << offset);
  137. if(es8156_i2c_write(client, reg, val))
  138. {
  139. printf("%s write failed, reg:0x%x, val:0x%x\n", __func__, reg, val);
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. static int es8156_reset(es8156_client *client)
  145. {
  146. es8156_i2c_write(client, ES8156_RESET_REG00, 0x1c);
  147. mdelay(5);
  148. es8156_i2c_write(client, ES8156_RESET_REG00, 0x03);
  149. return 0;
  150. }
  151. static int es8156_probe(struct i2c_adapter *adap)
  152. {
  153. es8156_client *client;
  154. client = pvPortMalloc(sizeof(es8156_client));
  155. struct codec_register_inf codec_register;
  156. struct snd_soc_dai_ops *ops = NULL;
  157. if (!client)
  158. {
  159. printf("%s, pvPortMalloc fail.\n", __func__);
  160. return -ENOMEM;
  161. }
  162. memset(client, 0, sizeof(es8156_client));
  163. ops = (struct snd_soc_dai_ops *)pvPortMalloc(sizeof(struct snd_soc_dai_ops));
  164. if (!ops) {
  165. vPortFree((void *)client);
  166. printf("%s, pvPortMalloc fail.\n", __func__);
  167. return -ENOMEM;
  168. }
  169. memset(ops, 0, sizeof(struct snd_soc_dai_ops));
  170. g_es8156_client = client;
  171. client->adap = adap;
  172. client->slave_addr = ES8156_SLAVE_ADDR;
  173. #if 0 //set default value;
  174. client->rates = 44100;
  175. client->channels = 2;
  176. client->bits = 16;
  177. client->mute = 0;
  178. client->master = 1;
  179. client->mclk = client->rates * 256;
  180. client->fmt = SND_SOC_DAIFMT_I2S; //interface type.
  181. #endif
  182. if(ops)
  183. {
  184. //es8156 Free Driver(免驱).
  185. ops->init = NULL;
  186. ops->hw_params = NULL;
  187. ops->startup = NULL;
  188. ops->set_fmt = NULL;
  189. ops->set_mute = NULL;
  190. ops->set_mclk = NULL;
  191. }
  192. codec_register.name = "ES8156";
  193. codec_register.codec_type = TYPE_DECODER;
  194. codec_register.ops = ops;
  195. codec_register.codec_prvdata = NULL;
  196. audio_codec_register(codec_register);
  197. // es8156_reset(client);
  198. return 0;
  199. }
  200. #endif
  201. int es8156_init(void)
  202. {
  203. #if 1 //es8156 no need driver.
  204. struct i2c_adapter *adap = NULL;
  205. if (!(adap = i2c_open("i2c0"))) {
  206. printf("%s, open i2c0 fail.\n", __func__);
  207. return -1;
  208. }
  209. es8156_probe(adap);
  210. #endif
  211. return 0;
  212. }
  213. #endif