adc_acts_leopard.c 33 KB

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