pa_aw87390.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief aw87390 audio implementation
  9. */
  10. /*
  11. * Features
  12. */
  13. #include <kernel.h>
  14. #include <device.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <soc.h>
  18. #include <board_cfg.h>
  19. #include <logging/log.h>
  20. #include <drivers/i2c.h>
  21. #include <stdlib.h>
  22. #include "../phy_audio_common.h"
  23. LOG_MODULE_REGISTER(aw87390_audio, CONFIG_LOG_DEFAULT_LEVEL);
  24. #define AW87390_SLAVE_ADDR 0x58
  25. #define USE_ADP_MODE 0
  26. #define I2C_DEV_NAME "I2C_1"
  27. typedef struct
  28. {
  29. uint8_t addr;
  30. uint8_t val;
  31. }iic_set_t;
  32. static const iic_set_t AW87390_register_setting[] =
  33. {
  34. #if(USE_ADP_MODE == 1)
  35. {0x67,0x23},
  36. #else
  37. {0x67,0x03},
  38. #endif
  39. {0x01,0x03},
  40. {0x02,0x07},
  41. {0x02,0x00},
  42. #if(USE_ADP_MODE == 1)
  43. {0x02,0x1C},
  44. #else
  45. {0x02,0x0C},
  46. #endif
  47. //{0x03,0x08}, //8V
  48. {0x03,0x02}, //6.5V
  49. {0x04,0x05},
  50. //{0x05,0x08}, //12dB
  51. {0x05,0x0C}, //18dB
  52. //{0x05,0x12}, //27dB
  53. //{0x06,0x07}, //0.8W@8ohm
  54. {0x06,0x04}, //0.5W@8ohm
  55. {0x07,0x4E},
  56. //{0x08,0x06}, //1.6W
  57. //{0x08,0x08}, //2.0W
  58. //{0x08,0x04}, //1.2W
  59. {0x08,0x02}, //0.8W
  60. //{0x08,0x01}, //0.6W
  61. {0x09,0x08},
  62. #if(USE_ADP_MODE == 1)
  63. {0x0A,0x00},
  64. #else
  65. {0x0A,0x3A},
  66. #endif
  67. {0x61,0xB3},
  68. {0x62,0x24},
  69. {0x63,0x05},
  70. #if(USE_ADP_MODE == 1)
  71. {0x64,0x24},
  72. {0x65,0x15},
  73. #else
  74. {0x64,0x48},
  75. {0x65,0x17},
  76. #endif
  77. {0x79,0x7A},
  78. {0x7A,0x6C},
  79. {0x78,0x80},
  80. {0x66,0x38},
  81. {0x76,0x00},
  82. {0x78,0x00},
  83. {0x68,0x1B},
  84. {0x69,0x5B},
  85. #if(USE_ADP_MODE == 1)
  86. {0x70,0x1C},
  87. {0x71,0x00},
  88. {0x72,0xFE},
  89. #else
  90. {0x70,0x1D},
  91. {0x71,0x10},
  92. {0x72,0xB4},
  93. #endif
  94. {0x73,0x4F},
  95. {0x74,0x24},
  96. {0x75,0x02},
  97. {0x01,0x07},
  98. #if(USE_ADP_MODE == 1)
  99. {0xFF,0x50},
  100. #else
  101. {0xFF,0x00},
  102. #endif
  103. };
  104. static uint8_t drv_aw87390_regs_init(const struct device *dev)
  105. {
  106. uint8_t buf[2] = {0};
  107. uint8_t num = ARRAY_SIZE(AW87390_register_setting);
  108. const struct device *const iic_dev_1 = device_get_binding(I2C_DEV_NAME);
  109. if (!iic_dev_1) {
  110. LOG_ERR("iic device not found!");
  111. return -1;
  112. }else{
  113. LOG_INF("iic device has been found!\n");
  114. }
  115. for(uint8_t i = 0; i< num; i++)
  116. {
  117. buf[0] = AW87390_register_setting[i].addr;
  118. buf[1] = AW87390_register_setting[i].val;
  119. i2c_write_async(iic_dev_1, buf, 2, AW87390_SLAVE_ADDR,NULL,NULL);
  120. k_msleep(1);
  121. }
  122. LOG_INF("drv_aw87390_regs_init successfully\n");
  123. return 0;
  124. }
  125. static int aw87390_enable(struct device *dev, void *param)
  126. {
  127. uint8_t buf[2] = {0x1,0x7};//enable pa
  128. const struct device *const iic_dev_1 = device_get_binding(I2C_DEV_NAME);
  129. if (!iic_dev_1) {
  130. LOG_ERR("iic device not found!");
  131. return -1;
  132. }else{
  133. LOG_DBG("iic device has been found!\n");
  134. }
  135. i2c_write_async(iic_dev_1, buf, 2, AW87390_SLAVE_ADDR,NULL,NULL);
  136. k_msleep(1);
  137. LOG_INF("aw87390_enable\n");
  138. return 0;
  139. }
  140. static int aw87390_disable(struct device *dev, void *param)
  141. {
  142. uint8_t buf[2] = {0x1,0x3};//disable pa
  143. const struct device *const iic_dev_1 = device_get_binding(I2C_DEV_NAME);
  144. if (!iic_dev_1) {
  145. LOG_ERR("iic device not found!");
  146. return -1;
  147. }else{
  148. LOG_DBG("iic device has been found!\n");
  149. }
  150. i2c_write_async(iic_dev_1, buf, 2, AW87390_SLAVE_ADDR,NULL,NULL);
  151. k_msleep(1);
  152. LOG_INF("aw87390_disable\n");
  153. return 0;
  154. }
  155. static int aw87390_audioioctl(struct device *dev, uint32_t cmd, void *param)
  156. {
  157. LOG_INF("aw87390_audioioctl,cmd:%d,param:%p\n",cmd,param);
  158. return 0;
  159. }
  160. const struct phy_audio_driver_api aw87390_drv_api = {
  161. .audio_enable = aw87390_enable,
  162. .audio_disable = aw87390_disable,
  163. .audio_ioctl = aw87390_audioioctl,
  164. };
  165. /* aw87390_out_init initialization */
  166. static int aw87390_out_init(const struct device *dev)
  167. {
  168. uint8_t ret;
  169. ret = drv_aw87390_regs_init(NULL);
  170. if(ret == 0)
  171. LOG_INF("aw87390_out init successfully\n");
  172. else
  173. LOG_ERR("aw87390_out init failed\n");
  174. return ret;
  175. }
  176. static int cmd_config_aw87390(const struct shell *shell, size_t argc, char **argv)
  177. {
  178. uint16_t i;
  179. /*read start addr */
  180. uint8_t r_start_add[1] = {0x00};
  181. uint8_t r_str_data[12] = {0x00};
  182. /*write start addr and data */
  183. uint8_t w_reg_s[2] = {0x00, 0x00};
  184. uint8_t w_reg_addr = 0x00;
  185. uint8_t w_reg_data = 0x00;
  186. if(argc == 3){
  187. w_reg_addr = atoi(argv[1]);
  188. w_reg_data = atoi(argv[2]);
  189. printk("w_reg_addr:%0x\n", w_reg_addr);
  190. printk("w_reg_data:%0x\n", w_reg_data);
  191. }
  192. w_reg_s[0] = w_reg_addr;
  193. w_reg_s[1] = w_reg_data;
  194. const struct device *const iic_dev_1 = device_get_binding(I2C_DEV_NAME);
  195. if (!iic_dev_1) {
  196. printk("iic device not found!");
  197. return 0;
  198. }else
  199. {
  200. printk("iic device has been found!\n");
  201. }
  202. if(argc == 3)
  203. i2c_write_async(iic_dev_1, w_reg_s, 2, AW87390_SLAVE_ADDR, NULL, NULL);
  204. else
  205. printk("iput addr and data\n");
  206. i2c_write_async(iic_dev_1, r_start_add, 1, AW87390_SLAVE_ADDR, NULL, NULL);
  207. i2c_read_async(iic_dev_1, r_str_data, 12, AW87390_SLAVE_ADDR, NULL, NULL);
  208. k_msleep(1000);
  209. for(i = 0; i < 12; i++)
  210. {
  211. printk("r_str_data%d=====:%0x\n",i,r_str_data[i]);
  212. }
  213. return 0;
  214. }
  215. DEVICE_DEFINE(aw87390_audio, "aw87390_out", aw87390_out_init, NULL,
  216. NULL, NULL,
  217. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, &aw87390_drv_api);
  218. SHELL_CMD_REGISTER(write_aw87390, NULL, "dbg aw87390 commands", cmd_config_aw87390);