adc_acts_lark.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * Copyright (c) 2021 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Actions PMU ADC implementation
  9. */
  10. #include <kernel.h>
  11. #include <device.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <soc.h>
  15. #include <soc_atp.h>
  16. #include <board_cfg.h>
  17. #include <sys/util.h>
  18. #include <sys/byteorder.h>
  19. #include <drivers/adc.h>
  20. #include <logging/log.h>
  21. LOG_MODULE_REGISTER(pmuadc0, CONFIG_ADC_LOG_LEVEL);
  22. /***************************************************************************************************
  23. * PMUADC_CTL
  24. */
  25. #define PMUADC_CTL_REG_SENSOR_EFUSE_SHIFT (26)
  26. #define PMUADC_CTL_REG_SENSOR_EFUSE_MASK (0x3F << PMUADC_CTL_REG_SENSOR_EFUSE_SHIFT)
  27. #define PMUADC_CTL_EN_TEST_SEN_CURR BIT(25)
  28. #define PMUADC_CTL_PMUADC_EN BIT(23)
  29. #define PMUADC_CTL_REG_IBIAS_BUF_SHIFT (21)
  30. #define PMUADC_CTL_REG_IBIAS_BUF_MASK (0x3 << PMUADC_CTL_REG_IBIAS_BUF_SHIFT)
  31. #define PMUADC_CTL_REG_IBIAS_BUF(x) ((x) << PMUADC_CTL_REG_IBIAS_BUF_SHIFT)
  32. #define PMUADC_CTL_REG_IBIAS_ADC_SHIFT (19)
  33. #define PMUADC_CTL_REG_IBIAS_ADC_MASK (0x3 << PMUADC_CTL_REG_IBIAS_ADC_SHIFT)
  34. #define PMUADC_CTL_REG_IBIAS_ADC(x) ((x) << PMUADC_CTL_REG_IBIAS_ADC_SHIFT)
  35. #define PMUADC_CTL_TEST_SARAD BIT(18)
  36. #define PMUADC_CTL_LRADC7_SCAL BIT(17)
  37. #define PMUADC_CTL_LRADC6_SCAL BIT(16)
  38. #define PMUADC_CTL_LRADC5_SCAL BIT(15)
  39. #define PMUADC_CTL_LRADC4_SCAL BIT(14)
  40. #define PMUADC_CTL_LRADC3_SCAL BIT(13)
  41. #define PMUADC_CTL_LRADC7_CHEN BIT(11)
  42. #define PMUADC_CTL_LRADC6_CHEN BIT(10)
  43. #define PMUADC_CTL_LRADC5_CHEN BIT(9)
  44. #define PMUADC_CTL_LRADC4_CHEN BIT(8)
  45. #define PMUADC_CTL_LRADC3_CHEN BIT(7)
  46. #define PMUADC_CTL_LRADC2_CHEN BIT(6)
  47. #define PMUADC_CTL_LRADC1_CHEN BIT(5)
  48. #define PMUADC_CTL_SVCC_CHEN BIT(4)
  49. #define PMUADC_CTL_SENSOR_CHEN BIT(3)
  50. #define PMUADC_CTL_DC5V_CHEN BIT(2)
  51. #define PMUADC_CTL_BATV_CHEN BIT(1)
  52. #define PMUADC_CTL_CHARGI_CHEN BIT(0)
  53. /***************************************************************************************************
  54. * PMUADC_INTMASK
  55. */
  56. #define PMUADC_INTMASK_LRADC7_INTEN BIT(11)
  57. #define PMUADC_INTMASK_LRADC6_INTEN BIT(10)
  58. #define PMUADC_INTMASK_LRADC5_INTEN BIT(9)
  59. #define PMUADC_INTMASK_LRADC4_INTEN BIT(8)
  60. #define PMUADC_INTMASK_LRADC3_INTEN BIT(7)
  61. #define PMUADC_INTMASK_LRADC2_INTEN BIT(6)
  62. #define PMUADC_INTMASK_LRADC1_INTEN BIT(5)
  63. #define PMUADC_INTMASK_SVCC_INTEN BIT(4)
  64. #define PMUADC_INTMASK_SENSOR_INTEN BIT(3)
  65. #define PMUADC_INTMASK_DC5V_INTEN BIT(2)
  66. #define PMUADC_INTMASK_BATV_INTEN BIT(1)
  67. #define PMUADC_INTMASK_CHARGI_INTEN BIT(0)
  68. /***************************************************************************************************
  69. * PMUADC_PD
  70. */
  71. #define PMUADC_PD_LRADC7_PD BIT(11)
  72. #define PMUADC_PD_LRADC6_PD BIT(10)
  73. #define PMUADC_PD_LRADC5_PD BIT(9)
  74. #define PMUADC_PD_LRADC4_PD BIT(8)
  75. #define PMUADC_PD_LRADC3_PD BIT(7)
  76. #define PMUADC_PD_LRADC2_PD BIT(6)
  77. #define PMUADC_PD_LRADC1_PD BIT(5)
  78. #define PMUADC_PD_SVCC_PD BIT(4)
  79. #define PMUADC_PD_SENSOR_PD BIT(3)
  80. #define PMUADC_PD_DC5V_PD BIT(2)
  81. #define PMUADC_PD_BATV_PD BIT(1)
  82. #define PMUADC_PD_CHARGI_PD BIT(0)
  83. /***************************************************************************************************
  84. * CHARGI_DATA
  85. */
  86. #define CHARGI_DATA_CHARGI_SHIFT (0)
  87. #define CHARGI_DATA_CHARGI_MASK (0xFFF << CHARGI_DATA_CHARGI_SHIFT)
  88. /***************************************************************************************************
  89. * BATADC_DATA
  90. */
  91. #define BATADC_DATA_BATV_SHIFT (0)
  92. #define BATADC_DATA_BATV_MASK (0x3FFF << BATADC_DATA_BATV_SHIFT)
  93. /***************************************************************************************************
  94. * DC5VADC_DATA
  95. */
  96. #define DC5VADC_DATA_DC5V_SHIFT (0)
  97. #define DC5VADC_DATA_DC5V_MASK (0xFFF << DC5VADC_DATA_DC5V_SHIFT)
  98. /***************************************************************************************************
  99. * SENSADC_DATA
  100. */
  101. #define SENSADC_DATA_SENSOR_SHIFT (0)
  102. #define SENSADC_DATA_SENSOR_MASK (0xFFF << SENSADC_DATA_SENSOR_SHIFT)
  103. /***************************************************************************************************
  104. * SVCCADC_DATA
  105. */
  106. #define SVCCADC_DATA_SVCC_SHIFT (0)
  107. #define SVCCADC_DATA_SVCC_MASK (0xFFF << SVCCADC_DATA_SVCC_SHIFT)
  108. /***************************************************************************************************
  109. * LRADC_DATA
  110. */
  111. #define LRADC_DATA_LRADC_SHIFT (0)
  112. #define LRADC_DATA_LRADC_MASK (0x3FFF << LRADC_DATA_LRADC_SHIFT)
  113. /***************************************************************************************************
  114. * PMUADCDIG_CTL
  115. */
  116. #define PMUADCDIG_CTL_LRADC7_AVG_SHIFT (22)
  117. #define PMUADCDIG_CTL_LRADC7_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC7_AVG_SHIFT)
  118. #define PMUADCDIG_CTL_LRADC7_AVG(x) ((x) << PMUADCDIG_CTL_LRADC7_AVG_SHIFT)
  119. #define PMUADCDIG_CTL_LRADC6_AVG_SHIFT (20)
  120. #define PMUADCDIG_CTL_LRADC6_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC6_AVG_SHIFT)
  121. #define PMUADCDIG_CTL_LRADC6_AVG(x) ((x) << PMUADCDIG_CTL_LRADC6_AVG_SHIFT)
  122. #define PMUADCDIG_CTL_LRADC5_AVG_SHIFT (18)
  123. #define PMUADCDIG_CTL_LRADC5_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC5_AVG_SHIFT)
  124. #define PMUADCDIG_CTL_LRADC5_AVG(x) ((x) << PMUADCDIG_CTL_LRADC5_AVG_SHIFT)
  125. #define PMUADCDIG_CTL_LRADC4_AVG_SHIFT (16)
  126. #define PMUADCDIG_CTL_LRADC4_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC4_AVG_SHIFT)
  127. #define PMUADCDIG_CTL_LRADC4_AVG(x) ((x) << PMUADCDIG_CTL_LRADC4_AVG_SHIFT)
  128. #define PMUADCDIG_CTL_LRADC3_AVG_SHIFT (14)
  129. #define PMUADCDIG_CTL_LRADC3_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC3_AVG_SHIFT)
  130. #define PMUADCDIG_CTL_LRADC3_AVG(x) ((x) << PMUADCDIG_CTL_LRADC3_AVG_SHIFT)
  131. #define PMUADCDIG_CTL_LRADC2_AVG_SHIFT (12)
  132. #define PMUADCDIG_CTL_LRADC2_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC2_AVG_SHIFT)
  133. #define PMUADCDIG_CTL_LRADC2_AVG(x) ((x) << PMUADCDIG_CTL_LRADC2_AVG_SHIFT)
  134. #define PMUADCDIG_CTL_LRADC1_AVG_SHIFT (10)
  135. #define PMUADCDIG_CTL_LRADC1_AVG_MASK (0x3 << PMUADCDIG_CTL_LRADC1_AVG_SHIFT)
  136. #define PMUADCDIG_CTL_LRADC1_AVG(x) ((x) << PMUADCDIG_CTL_LRADC1_AVG_SHIFT)
  137. #define PMUADCDIG_CTL_BAT_AVG_SHIFT (8)
  138. #define PMUADCDIG_CTL_BAT_AVG_MASK (0x3 << PMUADCDIG_CTL_BAT_AVG_SHIFT)
  139. #define PMUADCDIG_CTL_BAT_AVG(x) ((x) << PMUADCDIG_CTL_BAT_AVG_SHIFT)
  140. #define PMUADCDIG_CTL_ADCFIFOSEL BIT(7)
  141. #define PMUADCDIG_CTL_ADC_DRQEN BIT(6)
  142. #define PMUADCDIG_CTL_ADCFIFOLV_SHIFT (4)
  143. #define PMUADCDIG_CTL_ADCFIFOLV_MASK (0x3 << PMUADCDIG_CTL_ADCFIFOLV_SHIFT)
  144. #define PMUADCDIG_CTL_ADCFIFOLV(x) ((x) << PMUADCDIG_CTL_ADCFIFOLV_SHIFT)
  145. #define PMUADCDIG_CTL_ADCFIFOEM BIT(3)
  146. #define PMUADCDIG_CTL_ADCFIFOFU BIT(2)
  147. #define PMUADCDIG_CTL_ADCFIFOER BIT(1)
  148. #define PMUADCDIG_CTL_ADCFIFOEN BIT(0)
  149. #define PMUADC_MAX_CHANNEL_ID (11) /* MAX valid channel ID */
  150. #define PMUADC_WAIT_TIMEOUT_MS (50) /* Timeout to wait for PMU ADC sampling */
  151. #define PMUADC_WAIT_OVER_SAMPLE_US (3000) //(1000)
  152. #if (CONFIG_PMUADC_LRADC1_AVG == 0)
  153. #define LRADCx_PENDING(ch, n) \
  154. if ((ch) & PMUADC_PD_LRADC##n##_PD) { \
  155. data->mdata.v.lradc##n##_voltage = pmuadc_reg->lradc##n##_data & LRADC_DATA_LRADC_MASK; \
  156. data->channels &= ~(BIT(n + 4)); \
  157. LOG_DBG("New LRADC%d voltage: 0x%x", n, data->mdata.v.lradc##n##_voltage); \
  158. }
  159. #else
  160. #define LRADCx_PENDING(ch, n) \
  161. if ((ch) & PMUADC_PD_LRADC##n##_PD) { \
  162. ret = pmuadc_wait_sample_complete(dev, ch); \
  163. if (ret) \
  164. return ret; \
  165. data->mdata.v.lradc##n##_voltage = pmuadc_reg->lradc##n##_data & LRADC_DATA_LRADC_MASK; \
  166. data->channels &= ~(BIT(n + 4)); \
  167. LOG_DBG("New LRADC%d voltage: 0x%x", n, data->mdata.v.lradc##n##_voltage); \
  168. }
  169. #endif
  170. #define LRADCx_PENDING_ALL(x) \
  171. { \
  172. LRADCx_PENDING(x, 1); \
  173. LRADCx_PENDING(x, 2); \
  174. LRADCx_PENDING(x, 3); \
  175. LRADCx_PENDING(x, 4); \
  176. LRADCx_PENDING(x, 5); \
  177. LRADCx_PENDING(x, 6); \
  178. LRADCx_PENDING(x, 7); \
  179. }
  180. /*
  181. * @struct acts_pmu_adc
  182. * @brief Actions PMU ADC controller hardware register
  183. */
  184. struct acts_pmu_adc {
  185. volatile uint32_t ctl; /* PMUADC control */
  186. volatile uint32_t intmask; /* PMUADC sample interrupt set */
  187. volatile uint32_t pending; /* PMUADC sample pending */
  188. volatile uint32_t dig_ctl; /* PMUADC digital control */
  189. volatile uint32_t charge_data; /* Measure charging current */
  190. volatile uint32_t bat_data; /* Measure battery voltage */
  191. volatile uint32_t dc5v_data; /* Measure DC5V voltage */
  192. volatile uint32_t sensor_data; /* IC temperature sensor */
  193. volatile uint32_t svcc_data; /* Measure SVCC voltage */
  194. volatile uint32_t lradc1_data; /* Measure LRADC1 IO voltage */
  195. volatile uint32_t lradc2_data; /* Measure LRADC2 IO voltage */
  196. volatile uint32_t lradc3_data; /* Measure LRADC3 IO voltage */
  197. volatile uint32_t lradc4_data; /* Measure LRADC4 IO voltage */
  198. volatile uint32_t lradc5_data; /* Measure LRADC5 IO voltage */
  199. volatile uint32_t lradc6_data; /* Measure LRADC6 IO voltage */
  200. volatile uint32_t lradc7_data; /* Measure LRADC7 IO voltage */
  201. volatile uint32_t fifo_data; /* PMUADC FIFO data */
  202. };
  203. /**
  204. * struct pmuadc_measure_data
  205. * @brief The measure result data from PMU ADC sampling.
  206. */
  207. union pmuadc_measure_data {
  208. uint16_t data[PMUADC_MAX_CHANNEL_ID + 1];
  209. struct {
  210. uint16_t charging_current;
  211. uint16_t battery_voltage;
  212. //uint16_t charging_current;
  213. uint16_t dc5v_voltage;
  214. uint16_t sensor_temperature;
  215. uint16_t svcc_voltage;
  216. uint16_t lradc1_voltage;
  217. uint16_t lradc2_voltage;
  218. uint16_t lradc3_voltage;
  219. uint16_t lradc4_voltage;
  220. uint16_t lradc5_voltage;
  221. uint16_t lradc6_voltage;
  222. uint16_t lradc7_voltage;
  223. } v;
  224. };
  225. /**
  226. * struct pmuadc_drv_data
  227. * @brief The meta data which related to Actions PMU ADC.
  228. */
  229. struct pmuadc_drv_data {
  230. uint16_t channel_bitmap;
  231. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  232. struct k_sem completion; /* ADC sample synchronization completion semaphare */
  233. #endif
  234. struct k_sem lock; /* ADC read lock */
  235. union pmuadc_measure_data mdata; /* measuared data */
  236. uint16_t channels; /* active channels */
  237. uint16_t sample_cnt; /* sample counter */
  238. };
  239. /**
  240. * struct pmuadc_config_data
  241. * @brief The hardware data that related to Actions PMU ADC
  242. */
  243. struct pmuadc_config_data {
  244. uint32_t reg_base; /* PMU ADC controller register base address */
  245. uint8_t clk_id; /* LRADC devclk id */
  246. uint8_t clk_src; /* LRADC clock source */
  247. uint8_t clk_div; /* LRADC clock divisor */
  248. uint8_t debounce; /* PMU ADC debounce as the first sample data is not correct */
  249. void (*irq_config)(void); /* IRQ configuration function */
  250. };
  251. /* @brief get the base address of PMU ADC register */
  252. static inline struct acts_pmu_adc *get_pmuadc_reg_base(const struct device *dev)
  253. {
  254. const struct pmuadc_config_data *cfg = dev->config;
  255. return (struct acts_pmu_adc *)cfg->reg_base;
  256. }
  257. /* @brief dump pmu dac controller register */
  258. void pmudac_dump_register(const struct device *dev)
  259. {
  260. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  261. LOG_INF("** pmudac contoller regster **");
  262. LOG_INF(" BASE: %08x", (uint32_t)pmuadc_reg);
  263. LOG_INF(" CTL: %08x", pmuadc_reg->ctl);
  264. LOG_INF(" INTMASK: %08x", pmuadc_reg->intmask);
  265. LOG_INF(" PENDING: %08x", pmuadc_reg->pending);
  266. LOG_INF(" DIG_CTL: %08x", pmuadc_reg->dig_ctl);
  267. LOG_INF(" CHARGE_DATA: %08x", pmuadc_reg->charge_data);
  268. LOG_INF(" BAT_DATA: %08x", pmuadc_reg->bat_data);
  269. LOG_INF(" DC5V_DATA: %08x", pmuadc_reg->dc5v_data);
  270. LOG_INF(" SENSOR_DATA: %08x", pmuadc_reg->sensor_data);
  271. LOG_INF(" SVCC_DATA: %08x", pmuadc_reg->svcc_data);
  272. LOG_INF(" LRADC1_DATA: %08x", pmuadc_reg->lradc1_data);
  273. LOG_INF(" LRADC2_DATA: %08x", pmuadc_reg->lradc2_data);
  274. LOG_INF(" LRADC3_DATA: %08x", pmuadc_reg->lradc3_data);
  275. LOG_INF(" LRADC4_DATA: %08x", pmuadc_reg->lradc4_data);
  276. LOG_INF(" LRADC5_DATA: %08x", pmuadc_reg->lradc5_data);
  277. LOG_INF(" LRADC6_DATA: %08x", pmuadc_reg->lradc6_data);
  278. LOG_INF(" LRADC7_DATA: %08x", pmuadc_reg->lradc7_data);
  279. LOG_INF(" FIFO_DATA: %08x", pmuadc_reg->fifo_data);
  280. LOG_INF(" CMU_LRADCCLK: %08x", sys_read32(CMU_LRADCCLK));
  281. }
  282. static s16_t adc_sensor_offset, adc_dc5v_offset, adc_bat_offset;
  283. /* @brief PMU ADC channel lock */
  284. static inline void pmuadc_lock(const struct device *dev)
  285. {
  286. struct pmuadc_drv_data *data = dev->data;
  287. k_sem_take(&data->lock, K_FOREVER);
  288. }
  289. /* @brief PMU ADC channel unlock */
  290. static inline void pmuadc_unlock(const struct device *dev)
  291. {
  292. struct pmuadc_drv_data *data = dev->data;
  293. k_sem_give(&data->lock);
  294. }
  295. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  296. /* @brief wait for PMU ADC sample IRQ completion */
  297. static inline int pmuadc_wait_for_completion(const struct device *dev)
  298. {
  299. struct pmuadc_drv_data *data = dev->data;
  300. return k_sem_take(&data->completion, K_MSEC(PMUADC_WAIT_TIMEOUT_MS));
  301. }
  302. /* @brief signal that PMU ADC sample data completly */
  303. static inline void pmuadc_complete(const struct device *dev)
  304. {
  305. struct pmuadc_drv_data *data = dev->data;
  306. k_sem_give(&data->completion);
  307. }
  308. #endif
  309. /* @brief check the buffer size */
  310. static int check_buffer_size(const struct adc_sequence *sequence,
  311. uint8_t active_channels)
  312. {
  313. uint32_t needed_buffer_size;
  314. needed_buffer_size = active_channels * sizeof(uint16_t);
  315. if (sequence->buffer_size < needed_buffer_size) {
  316. LOG_ERR("Provided buffer is too small (%u/%u)",
  317. sequence->buffer_size, needed_buffer_size);
  318. return -ENOMEM;
  319. }
  320. return 0;
  321. }
  322. /* @brief validate the selected PMU ADC channels */
  323. static int pmuadc_check_channels(const struct device *dev,
  324. const struct adc_sequence *sequence)
  325. {
  326. struct pmuadc_drv_data *data = dev->data;
  327. uint32_t channels = sequence->channels;
  328. uint8_t i, active_channels = 0;
  329. if (!channels) {
  330. LOG_ERR("null channels");
  331. return -EINVAL;
  332. }
  333. for (i = 0; i <= PMUADC_MAX_CHANNEL_ID; i++) {
  334. if (channels & BIT(i)) {
  335. if (!(data->channel_bitmap & BIT(i))) {
  336. LOG_ERR("ADC channel@%d has not setuped yet", i);
  337. return -ENXIO;
  338. } else {
  339. ++active_channels;
  340. }
  341. }
  342. }
  343. return check_buffer_size(sequence, active_channels);
  344. }
  345. /* @brief enable specified PMU ADC channels to sample data */
  346. static int pmuadc_enable_channels(const struct device *dev, uint16_t channels)
  347. {
  348. struct pmuadc_drv_data *data = dev->data;
  349. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  350. uint8_t i;
  351. uint16_t ctl = 0;
  352. data->channels = channels;
  353. data->sample_cnt = 0;
  354. LOG_DBG("Active channels:0x%x", data->channels);
  355. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  356. /* If enable new channels, need to disable PMUADC and then enable */
  357. if (pmuadc_reg->ctl & PMUADC_CTL_PMUADC_EN) {
  358. pmuadc_reg->ctl &= ~PMUADC_CTL_PMUADC_EN;
  359. k_busy_wait(300);
  360. }
  361. #endif
  362. /* enable PMU ADC channles */
  363. for (i = 0; i <= PMUADC_MAX_CHANNEL_ID; i++) {
  364. if (channels & BIT(i))
  365. ctl |= BIT(i);
  366. }
  367. pmuadc_reg->ctl |= ctl;
  368. /* enable PMU ADC function */
  369. pmuadc_reg->ctl |= PMUADC_CTL_PMUADC_EN;
  370. pmuadc_reg->pending = pmuadc_reg->pending;
  371. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  372. uint16_t intmask = 0;
  373. /* enable channels interrupts */
  374. for (i = 0; i <= PMUADC_MAX_CHANNEL_ID; i++) {
  375. if (channels & BIT(i)) {
  376. intmask |= BIT(i);
  377. }
  378. }
  379. pmuadc_reg->intmask = intmask;
  380. #endif
  381. return 0;
  382. }
  383. /* @brief disable all PMU ADC channels */
  384. static int pmuadc_disable_channels(const struct device *dev)
  385. {
  386. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  387. /* disable all ADC channels */
  388. pmuadc_reg->intmask = 0;
  389. pmuadc_reg->pending = pmuadc_reg->pending;
  390. pmuadc_reg->ctl &= ~(PMUADC_CTL_PMUADC_EN | 0x7FF);
  391. k_busy_wait(300);
  392. return 0;
  393. }
  394. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  395. static int pmuadc_wait_sample_complete(const struct device *dev, uint8_t ch_index)
  396. {
  397. uint32_t timestamp = k_cycle_get_32();
  398. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  399. while(!(ch_index & pmuadc_reg->pending)) {
  400. if (k_cyc_to_us_floor32(k_cycle_get_32() - timestamp)
  401. > PMUADC_WAIT_OVER_SAMPLE_US) {
  402. LOG_ERR("failed to get ADC channel:%d PD:0x%x CTL:0x%x", ch_index, pmuadc_reg->pending, pmuadc_reg->ctl);
  403. return -ETIMEDOUT;
  404. }
  405. }
  406. LOG_DBG("pmuadc wait channel:%d use %dus", ch_index,
  407. k_cyc_to_us_floor32(k_cycle_get_32() - timestamp));
  408. return 0;
  409. }
  410. #endif
  411. /* @brief Implementation of the ADC driver API function: adc_channel_setup. */
  412. static int pmuadc_channel_setup(const struct device *dev,
  413. const struct adc_channel_cfg *channel_cfg)
  414. {
  415. struct pmuadc_drv_data *data = dev->data;
  416. uint8_t channel_id = channel_cfg->channel_id;
  417. if (channel_id > PMUADC_MAX_CHANNEL_ID) {
  418. LOG_ERR("Invalid channel id %d", channel_id);
  419. return -EINVAL;
  420. }
  421. pmuadc_lock(dev);
  422. if (!(data->channel_bitmap & BIT(channel_id))) {
  423. data->channel_bitmap |= BIT(channel_id);
  424. LOG_INF("Enable PMU ADC channel@%d", channel_id);
  425. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  426. pmuadc_enable_channels(dev, BIT(channel_id));
  427. #if (CONFIG_PMUADC_BAT_WAIT_AVG_COMPLETE == 0) && (CONFIG_PMUADC_BAT_AVG_CNT == 1)
  428. /* Ignore the first BAT Voltage sample data */
  429. if (channel_id == PMUADC_ID_BATV)
  430. pmuadc_wait_sample_complete(dev, BIT(PMUADC_ID_BATV));
  431. #endif
  432. k_busy_wait(300);
  433. #endif
  434. }
  435. pmuadc_unlock(dev);
  436. return 0;
  437. }
  438. static u16_t pmu_adc_cal(int32_t adcval, int32_t offset)
  439. {
  440. adcval = adcval + offset;
  441. return (u16_t)adcval;
  442. }
  443. /* @brief start to read the PMU ADC measure data */
  444. static int pmuadc_start_read(const struct device *dev,
  445. const struct adc_sequence *sequence)
  446. {
  447. struct pmuadc_drv_data *data = dev->data;
  448. int ret = 0;
  449. uint32_t channels = sequence->channels;
  450. uint8_t i;
  451. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  452. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  453. /* clear all channels pending */
  454. pmuadc_reg->pending = pmuadc_reg->pending;
  455. if (channels & PMUADC_PD_BATV_PD) {
  456. #if (CONFIG_PMUADC_BAT_WAIT_AVG_COMPLETE == 1) && (CONFIG_PMUADC_BAT_AVG_CNT == 1)
  457. ret = pmuadc_wait_sample_complete(dev, BIT(PMUADC_ID_BATV));
  458. if (ret)
  459. return ret;
  460. #endif
  461. data->mdata.v.battery_voltage = pmuadc_reg->bat_data & BATADC_DATA_BATV_MASK;
  462. data->mdata.v.battery_voltage = pmu_adc_cal(data->mdata.v.battery_voltage, adc_bat_offset);
  463. }
  464. if (channels & PMUADC_PD_CHARGI_PD)
  465. data->mdata.v.charging_current = pmuadc_reg->charge_data & CHARGI_DATA_CHARGI_MASK;
  466. if (channels & PMUADC_PD_DC5V_PD){
  467. data->mdata.v.dc5v_voltage = pmuadc_reg->dc5v_data & DC5VADC_DATA_DC5V_MASK;
  468. data->mdata.v.dc5v_voltage = pmu_adc_cal(data->mdata.v.dc5v_voltage, adc_dc5v_offset);
  469. }
  470. if (channels & PMUADC_PD_SENSOR_PD){
  471. data->mdata.v.sensor_temperature = pmuadc_reg->sensor_data & SENSADC_DATA_SENSOR_MASK;
  472. data->mdata.v.sensor_temperature = pmu_adc_cal(data->mdata.v.sensor_temperature, adc_sensor_offset);
  473. }
  474. if (channels & PMUADC_PD_SVCC_PD)
  475. data->mdata.v.svcc_voltage = pmuadc_reg->svcc_data & SVCCADC_DATA_SVCC_MASK;
  476. LRADCx_PENDING_ALL(channels);
  477. #else
  478. //uint32_t timestamp = k_cycle_get_32();
  479. ret = pmuadc_enable_channels(dev, channels);
  480. if (ret)
  481. return ret;
  482. ret = pmuadc_wait_for_completion(dev);
  483. /*LOG_DBG("pmuadc wait channel:%d use %dus", channels,
  484. k_cyc_to_us_floor32(k_cycle_get_32() - timestamp));*/
  485. #endif /* CONFIG_ADC_ACTS_ALWAYS_ON */
  486. if (!ret) {
  487. for (i = 0; i <= PMUADC_MAX_CHANNEL_ID; i++) {
  488. if (channels & BIT(i)) {
  489. sys_put_le16(data->mdata.data[i], sequence->buffer);
  490. }
  491. }
  492. }
  493. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  494. pmuadc_disable_channels(dev);
  495. #endif
  496. return ret;
  497. }
  498. /* @brief Implementation of the ADC driver API function: adc_read. */
  499. static int pmuadc_read(const struct device *dev,
  500. const struct adc_sequence *sequence)
  501. {
  502. int ret;
  503. ret = pmuadc_check_channels(dev, sequence);
  504. if (ret)
  505. return ret;
  506. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  507. pmuadc_lock(dev);
  508. #endif
  509. ret = pmuadc_start_read(dev, sequence);
  510. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  511. pmuadc_unlock(dev);
  512. #endif
  513. return ret;
  514. }
  515. #ifdef CONFIG_ADC_ASYNC
  516. /* @brief Implementation of the ADC driver API function: adc_read_sync. */
  517. static int pmuadc_read_async(const struct device *dev,
  518. const struct adc_sequence *sequence,
  519. struct k_poll_signal *async)
  520. {
  521. return -ENOTSUP;
  522. }
  523. #endif /* CONFIG_ADC_ASYNC */
  524. static const struct adc_driver_api pmuadc_driver_api = {
  525. .channel_setup = pmuadc_channel_setup,
  526. .read = pmuadc_read,
  527. #ifdef CONFIG_ADC_ASYNC
  528. .read_async = pmuadc_read_async,
  529. #endif
  530. };
  531. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  532. static void pmuadc_isr(void *arg)
  533. {
  534. struct device *dev = (struct device *)arg;
  535. struct pmuadc_drv_data *data = dev->data;
  536. const struct pmuadc_config_data *cfg = dev->config;
  537. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  538. uint32_t pending = pmuadc_reg->pending;
  539. uint32_t val;
  540. LOG_DBG("ctl:0x%x mask:0x%x pending:0x%x channels:0x%x",
  541. pmuadc_reg->ctl, pmuadc_reg->intmask, pending, data->channels);
  542. if (pending & PMUADC_PD_CHARGI_PD) {
  543. val = pmuadc_reg->charge_data & CHARGI_DATA_CHARGI_MASK;
  544. data->mdata.v.charging_current = val;
  545. data->channels &= ~BIT(0);
  546. LOG_DBG("New charging current: 0x%x", val);
  547. }
  548. if (pending & PMUADC_PD_BATV_PD) {
  549. val = pmuadc_reg->bat_data & BATADC_DATA_BATV_MASK;
  550. data->mdata.v.battery_voltage = pmu_adc_cal(val, adc_bat_offset);
  551. data->channels &= ~BIT(1);
  552. LOG_DBG("New battery voltage: 0x%x", val);
  553. }
  554. if (pending & PMUADC_PD_DC5V_PD) {
  555. val = pmuadc_reg->dc5v_data & DC5VADC_DATA_DC5V_MASK;
  556. data->mdata.v.dc5v_voltage = pmu_adc_cal(val, adc_dc5v_offset);
  557. data->channels &= ~BIT(2);
  558. LOG_DBG("New DC5V voltage: 0x%x", val);
  559. }
  560. if (pending & PMUADC_PD_SENSOR_PD) {
  561. val = pmuadc_reg->sensor_data & SENSADC_DATA_SENSOR_MASK;
  562. data->mdata.v.sensor_temperature = pmu_adc_cal(val, adc_sensor_offset);;
  563. data->channels &= ~BIT(3);
  564. LOG_DBG("New sensor temperature: 0x%x", val);
  565. }
  566. if (pending & PMUADC_PD_SVCC_PD) {
  567. val = pmuadc_reg->svcc_data & SVCCADC_DATA_SVCC_MASK;
  568. data->mdata.v.svcc_voltage = val;
  569. data->channels &= ~BIT(4);
  570. LOG_DBG("New SVCC voltage: 0x%x", val);
  571. }
  572. LRADCx_PENDING_ALL(pending);
  573. pmuadc_reg->pending = pending;
  574. if ((!data->channels) && (++data->sample_cnt > cfg->debounce)) {
  575. LOG_DBG("complete");
  576. pmuadc_reg->intmask = 0;
  577. pmuadc_complete(dev);
  578. }
  579. }
  580. #endif
  581. /* @brief set the LRADC clock source and divisor */
  582. static int pmuadc_clk_source_set(const struct device *dev)
  583. {
  584. const struct pmuadc_config_data *cfg = dev->config;
  585. sys_write32(((cfg->clk_src & 0x7) << 4) | ((cfg->clk_div & 0x3)), CMU_LRADCCLK);
  586. LOG_DBG("LRADCCLK:0x%08x", sys_read32(CMU_LRADCCLK));
  587. return 0;
  588. }
  589. /* @brief ADC core current bias setting */
  590. static int pmuadc_bias_setting(const struct device *dev)
  591. {
  592. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  593. uint32_t reg = pmuadc_reg->ctl;
  594. reg &= ~PMUADC_CTL_REG_IBIAS_BUF_MASK;
  595. reg |= PMUADC_CTL_REG_IBIAS_BUF(CONFIG_PMUADC_IBIAS_BUF_SEL);
  596. reg &= ~PMUADC_CTL_REG_IBIAS_ADC_MASK;
  597. reg |= PMUADC_CTL_REG_IBIAS_ADC(CONFIG_PMUADC_IBIAS_ADC_SEL);
  598. pmuadc_reg->ctl = reg;
  599. return 0;
  600. }
  601. /* @brief ADC channels over sampling times setting */
  602. static int pmuadc_digital_setting(const struct device *dev)
  603. {
  604. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  605. uint32_t reg = pmuadc_reg->dig_ctl;
  606. reg &= ~PMUADCDIG_CTL_BAT_AVG_MASK;
  607. reg |= PMUADCDIG_CTL_BAT_AVG(CONFIG_PMUADC_BAT_AVG_CNT);
  608. reg &= ~PMUADCDIG_CTL_LRADC1_AVG_MASK;
  609. reg |= PMUADCDIG_CTL_LRADC1_AVG(CONFIG_PMUADC_LRADC1_AVG);
  610. pmuadc_reg->dig_ctl = reg;
  611. return 0;
  612. }
  613. static void adc_cal_init(void)
  614. {
  615. unsigned int offset;
  616. adc_bat_offset = 0;
  617. adc_sensor_offset = 0;
  618. adc_dc5v_offset = 0;
  619. if (!soc_atp_get_pmu_calib(4, &offset)){
  620. LOG_DBG("get batadc cal=0x%x\n", offset);
  621. if(offset & 0x10) //转换成adc 校准值
  622. adc_bat_offset = -(0x20-offset)*16;
  623. else
  624. adc_bat_offset = offset*16;
  625. }
  626. if (!soc_atp_get_pmu_calib(5, &offset)){ //offset校准按摄氏度,补码 单位0.1度
  627. LOG_DBG("get sensoradc cal=0x%x\n", offset);
  628. if(offset & 0x10)
  629. adc_sensor_offset = -(0x20-offset)*10;
  630. else
  631. adc_sensor_offset = offset*10;
  632. /*把校准值转成adc 值*/
  633. adc_sensor_offset = -adc_sensor_offset*1000/1745;
  634. }
  635. if (!soc_atp_get_pmu_calib(7, &offset)){
  636. LOG_DBG("get dv5v cal=0x%x\n", offset);
  637. if(offset & 0x10)
  638. adc_dc5v_offset = -(0x20-offset)*4;
  639. else
  640. adc_dc5v_offset = offset*4;
  641. }
  642. LOG_DBG("adc:bat=%d,sensor=%d,dc5v=%d\n", adc_bat_offset, adc_sensor_offset, adc_dc5v_offset);
  643. }
  644. /* @brief PMU ADC initialization */
  645. static int pmuadc_init(const struct device *dev)
  646. {
  647. const struct pmuadc_config_data *cfg = dev->config;
  648. struct pmuadc_drv_data *data = dev->data;
  649. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(dev);
  650. adc_cal_init();
  651. /* configure the LRADC clock source */
  652. pmuadc_clk_source_set(dev);
  653. acts_clock_peripheral_enable(cfg->clk_id);
  654. /* disable all ADC channels */
  655. pmuadc_reg->intmask = 0;
  656. pmuadc_reg->pending = pmuadc_reg->pending;
  657. pmuadc_reg->ctl &= ~0x3FFFFFF;
  658. pmuadc_bias_setting(dev);
  659. pmuadc_digital_setting(dev);
  660. k_busy_wait(300);
  661. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  662. k_sem_init(&data->completion, 0, 1);
  663. #endif
  664. k_sem_init(&data->lock, 1, 1);
  665. if (cfg->irq_config)
  666. cfg->irq_config();
  667. return 0;
  668. }
  669. #ifdef CONFIG_PM_DEVICE
  670. int adc_pm_control(const struct device *device, enum pm_device_action action)
  671. {
  672. int ret;
  673. const struct pmuadc_config_data *cfg = device->config;
  674. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  675. static uint32_t pmuadc_ctl_bak;
  676. struct acts_pmu_adc *pmuadc_reg = get_pmuadc_reg_base(device);
  677. #endif
  678. switch (action) {
  679. case PM_DEVICE_ACTION_RESUME:
  680. LOG_INF("adc wakeup\n");
  681. acts_clock_peripheral_enable(cfg->clk_id);
  682. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  683. pmuadc_reg->ctl = (pmuadc_ctl_bak & ~PMUADC_CTL_PMUADC_EN);
  684. LOG_DBG("%d PMUADC_CTL:0x%x", __LINE__, pmuadc_reg->ctl);
  685. k_busy_wait(300);
  686. pmuadc_reg->ctl |= PMUADC_CTL_PMUADC_EN;
  687. LOG_DBG("%d PMUADC_CTL:0x%x", __LINE__, pmuadc_reg->ctl);
  688. k_busy_wait(300);
  689. #else
  690. irq_enable(IRQ_ID_LRADC);
  691. #endif
  692. break;
  693. case PM_DEVICE_ACTION_SUSPEND:
  694. LOG_INF("adc sleep\n");
  695. #ifdef CONFIG_ADC_ACTS_ALWAYS_ON
  696. pmuadc_ctl_bak = pmuadc_reg->ctl;
  697. pmuadc_disable_channels(device);
  698. acts_clock_peripheral_disable(cfg->clk_id);
  699. #else
  700. irq_disable(IRQ_ID_LRADC);
  701. #endif
  702. break;
  703. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  704. break;
  705. case PM_DEVICE_ACTION_LATE_RESUME:
  706. break;
  707. default:
  708. ret = -EINVAL;
  709. }
  710. return 0;
  711. }
  712. #else
  713. #define adc_pm_control NULL
  714. #endif
  715. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  716. static void pmuadc_irq_config(void);
  717. #endif
  718. /* pmu adc driver data */
  719. static struct pmuadc_drv_data pmuadc_drv_data0;
  720. /* pmu adc config data */
  721. static const struct pmuadc_config_data pmuadc_config_data0 = {
  722. .reg_base = PMUADC_CTL,
  723. .clk_id = CLOCK_ID_LRADC,
  724. .clk_src = CONFIG_PMUADC_CLOCK_SOURCE,
  725. .clk_div = CONFIG_PMUADC_CLOCK_DIV,
  726. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  727. .debounce = CONFIG_PMUADC_DEBOUNCE,
  728. .irq_config = pmuadc_irq_config,
  729. #endif
  730. };
  731. DEVICE_DEFINE(pmuadc0, CONFIG_PMUADC_NAME, pmuadc_init, adc_pm_control,
  732. &pmuadc_drv_data0, &pmuadc_config_data0,
  733. PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, &pmuadc_driver_api);
  734. #ifndef CONFIG_ADC_ACTS_ALWAYS_ON
  735. static void pmuadc_irq_config(void)
  736. {
  737. IRQ_CONNECT(IRQ_ID_LRADC, CONFIG_PMUADC_IRQ_PRI,
  738. pmuadc_isr,
  739. DEVICE_GET(pmuadc0), 0);
  740. irq_enable(IRQ_ID_LRADC);
  741. }
  742. #endif