rt5677.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #define LOG_CATEGORY UCLASS_SOUND
  6. #include <common.h>
  7. #include <audio_codec.h>
  8. #include <dm.h>
  9. #include <i2c.h>
  10. #include "rt5677.h"
  11. #include <log.h>
  12. struct rt5677_priv {
  13. struct udevice *dev;
  14. };
  15. /* RT5677 has 256 8-bit register addresses, and 16-bit register data */
  16. struct rt5677_init_reg {
  17. u8 reg;
  18. u16 val;
  19. };
  20. static struct rt5677_init_reg init_list[] = {
  21. {RT5677_LOUT1, 0x0800},
  22. {RT5677_SIDETONE_CTRL, 0x0000},
  23. {RT5677_STO1_ADC_DIG_VOL, 0x3F3F},
  24. {RT5677_DAC1_DIG_VOL, 0x9090},
  25. {RT5677_STO2_ADC_MIXER, 0xA441},
  26. {RT5677_STO1_ADC_MIXER, 0x5480},
  27. {RT5677_STO1_DAC_MIXER, 0x8A8A},
  28. {RT5677_PWR_DIG1, 0x9800}, /* Power up I2S1 */
  29. {RT5677_PWR_ANLG1, 0xE9D5},
  30. {RT5677_PWR_ANLG2, 0x2CC0},
  31. {RT5677_PWR_DSP2, 0x0C00},
  32. {RT5677_I2S2_SDP, 0x0000},
  33. {RT5677_CLK_TREE_CTRL1, 0x1111},
  34. {RT5677_PLL1_CTRL1, 0x0000},
  35. {RT5677_PLL1_CTRL2, 0x0000},
  36. {RT5677_DIG_MISC, 0x0029},
  37. {RT5677_GEN_CTRL1, 0x00FF},
  38. {RT5677_GPIO_CTRL2, 0x0020},
  39. {RT5677_PWR_DIG2, 0x9024}, /* Power on ADC Stereo Filters */
  40. {RT5677_PDM_OUT_CTRL, 0x0088}, /* Unmute PDM, set stereo1 DAC */
  41. {RT5677_PDM_DATA_CTRL1, 0x0001}, /* Sysclk to PDM filter divider 2 */
  42. };
  43. /**
  44. * rt5677_i2c_read() - Read a 16-bit register
  45. *
  46. * @priv: Private driver data
  47. * @reg: Register number to read
  48. * @returns data read or -ve on error
  49. */
  50. static int rt5677_i2c_read(struct rt5677_priv *priv, uint reg)
  51. {
  52. u8 buf[2];
  53. int ret;
  54. ret = dm_i2c_read(priv->dev, reg, buf, sizeof(u16));
  55. if (ret)
  56. return ret;
  57. return buf[0] << 8 | buf[1];
  58. }
  59. /**
  60. * rt5677_i2c_write() - Write a 16-bit register
  61. *
  62. * @priv: Private driver data
  63. * @reg: Register number to read
  64. * @data: Data to write
  65. * @returns 0 if OK, -ve on error
  66. */
  67. static int rt5677_i2c_write(struct rt5677_priv *priv, uint reg, uint data)
  68. {
  69. u8 buf[2];
  70. buf[0] = (data >> 8) & 0xff;
  71. buf[1] = data & 0xff;
  72. return dm_i2c_write(priv->dev, reg, buf, sizeof(u16));
  73. }
  74. /**
  75. * rt5677_bic_or() - Set and clear bits of a codec register
  76. *
  77. * @priv: Private driver data
  78. * @reg: Register number to update
  79. * @bic: Mask of bits to clear
  80. * @set: Mask of bits to set
  81. * @returns 0 if OK, -ve on error
  82. *
  83. */
  84. static int rt5677_bic_or(struct rt5677_priv *priv, uint reg, uint bic,
  85. uint set)
  86. {
  87. uint old, new_value;
  88. int ret;
  89. old = rt5677_i2c_read(priv, reg);
  90. if (old < 0)
  91. return old;
  92. new_value = (old & ~bic) | (set & bic);
  93. if (old != new_value) {
  94. ret = rt5677_i2c_write(priv, reg, new_value);
  95. if (ret)
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. /**
  101. * rt5677_reg_init() - Initialise codec regs w/static/base values
  102. *
  103. * @priv: Private driver data
  104. * @returns 0 if OK, -ve on error
  105. */
  106. static int rt5677_reg_init(struct rt5677_priv *priv)
  107. {
  108. int ret;
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(init_list); i++) {
  111. ret = rt5677_i2c_write(priv, init_list[i].reg, init_list[i].val);
  112. if (ret)
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. #ifdef DEBUG
  118. static void debug_dump_5677_regs(struct rt5677_priv *priv, int swap)
  119. {
  120. uint i, reg_word;
  121. /* Show all 16-bit codec regs */
  122. for (i = 0; i < RT5677_REG_CNT; i++) {
  123. if (i % 8 == 0)
  124. log_debug("\nMX%02x: ", i);
  125. rt5677_i2c_read(priv, (u8)i, &reg_word);
  126. if (swap)
  127. log_debug("%04x ", swap_bytes16(reg_word));
  128. else
  129. log_debug("%04x ", reg_word);
  130. }
  131. log_debug("\n");
  132. /* Show all 16-bit 'private' codec regs */
  133. for (i = 0; i < RT5677_PR_REG_CNT; i++) {
  134. if (i % 8 == 0)
  135. log_debug("\nPR%02x: ", i);
  136. rt5677_i2c_write(priv, RT5677_PRIV_INDEX, i);
  137. rt5677_i2c_read(priv, RT5677_PRIV_DATA, &reg_word);
  138. if (swap)
  139. log_debug("%04x ", swap_bytes16(reg_word));
  140. else
  141. log_debug("%04x ", reg_word);
  142. }
  143. log_debug("\n");
  144. }
  145. #endif /* DEBUG */
  146. static int rt5677_hw_params(struct rt5677_priv *priv, uint bits_per_sample)
  147. {
  148. int ret;
  149. switch (bits_per_sample) {
  150. case 16:
  151. ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DL_MASK,
  152. 0);
  153. if (ret) {
  154. log_debug("Error updating I2S1 Interface Ctrl reg\n");
  155. return 1;
  156. }
  157. break;
  158. default:
  159. log_err("Illegal bits per sample %d\n", bits_per_sample);
  160. return -EINVAL;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * rt5677_set_fmt() - set rt5677 I2S format
  166. *
  167. * @priv: Private driver data
  168. * @returns 0 if OK, -ve on error
  169. */
  170. static int rt5677_set_fmt(struct rt5677_priv *priv)
  171. {
  172. int ret = 0;
  173. /*
  174. * Set format here: Assumes I2S, NB_NF, CBS_CFS
  175. *
  176. * CBS_CFS (Codec Bit Slave/Codec Frame Slave)
  177. */
  178. ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_MS_MASK,
  179. RT5677_I2S_MS_S);
  180. /* NB_NF (Normal Bit/Normal Frame) */
  181. ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_BP_MASK,
  182. RT5677_I2S_BP_NOR);
  183. /* I2S mode */
  184. ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DF_MASK,
  185. RT5677_I2S_DF_I2S);
  186. /* A44: I2S2 (going to speaker amp) is master */
  187. ret |= rt5677_bic_or(priv, RT5677_I2S2_SDP, RT5677_I2S_MS_MASK,
  188. RT5677_I2S_MS_M);
  189. if (ret) {
  190. log_err("Error updating I2S1 Interface Ctrl reg\n");
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * rt5677_reset() - reset the audio codec
  197. *
  198. * @priv: Private driver data
  199. * @returns 0 if OK, -ve on error
  200. */
  201. static int rt5677_reset(struct rt5677_priv *priv)
  202. {
  203. int ret;
  204. /* Reset the codec registers to their defaults */
  205. ret = rt5677_i2c_write(priv, RT5677_RESET, RT5677_SW_RESET);
  206. if (ret) {
  207. log_err("Error resetting codec\n");
  208. return ret;
  209. }
  210. return 0;
  211. }
  212. /**
  213. * Initialise rt5677 codec device
  214. *
  215. * @priv: Private driver data
  216. * @returns 0 if OK, -ve on error
  217. */
  218. int rt5677_device_init(struct rt5677_priv *priv)
  219. {
  220. int ret;
  221. /* Read status reg */
  222. ret = rt5677_i2c_read(priv, RT5677_RESET);
  223. if (ret < 0)
  224. return ret;
  225. log_debug("reg 00h, Software Reset & Status = 0x%04x\n", ret);
  226. /* Reset the codec/regs */
  227. ret = rt5677_reset(priv);
  228. if (ret)
  229. return ret;
  230. ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID1);
  231. if (ret < 0) {
  232. log_err("Error reading vendor ID\n");
  233. return 1;
  234. }
  235. log_debug("Hardware ID: %0xX\n", ret);
  236. ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID2);
  237. if (ret < 0) {
  238. log_err("Error reading vendor rev\n");
  239. return 1;
  240. }
  241. log_debug("Hardware revision: %04x\n", ret);
  242. return 0;
  243. }
  244. static int rt5677_set_params(struct udevice *dev, int interface, int rate,
  245. int mclk_freq, int bits_per_sample,
  246. uint channels)
  247. {
  248. struct rt5677_priv *priv = dev_get_priv(dev);
  249. int ret;
  250. /* Initialise codec regs w/static/base values, same as Linux driver */
  251. ret = rt5677_reg_init(priv);
  252. if (ret)
  253. return ret;
  254. ret = rt5677_hw_params(priv, bits_per_sample);
  255. if (ret)
  256. return ret;
  257. ret = rt5677_set_fmt(priv);
  258. if (ret)
  259. return ret;
  260. return 0;
  261. }
  262. static int rt5677_probe(struct udevice *dev)
  263. {
  264. struct rt5677_priv *priv = dev_get_priv(dev);
  265. priv->dev = dev;
  266. return rt5677_device_init(priv);
  267. }
  268. static const struct audio_codec_ops rt5677_ops = {
  269. .set_params = rt5677_set_params,
  270. };
  271. static const struct udevice_id rt5677_ids[] = {
  272. { .compatible = "realtek,rt5677" },
  273. { }
  274. };
  275. U_BOOT_DRIVER(rt5677_drv) = {
  276. .name = "rt5677",
  277. .id = UCLASS_AUDIO_CODEC,
  278. .of_match = rt5677_ids,
  279. .ops = &rt5677_ops,
  280. .probe = rt5677_probe,
  281. .priv_auto_alloc_size = sizeof(struct rt5677_priv),
  282. };