drv_pmic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-18 thread-liu the first version
  9. */
  10. #include <board.h>
  11. #if defined(BSP_USING_PMIC)
  12. #include <drv_pmic.h>
  13. #include <string.h>
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.pmic"
  16. #include <drv_log.h>
  17. #define I2C_NAME "i2c3"
  18. static struct rt_i2c_bus_device *pmic_dev = RT_NULL;
  19. /* i2c read reg */
  20. static rt_err_t read_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t len, rt_uint8_t *buf)
  21. {
  22. struct rt_i2c_msg msg[2] = {0, 0};
  23. RT_ASSERT(bus != RT_NULL);
  24. msg[0].addr = STPMU1_I2C_ADDRESS; /* Slave address */
  25. msg[0].flags = RT_I2C_WR; /* Write flag */
  26. msg[0].buf = &reg; /* Slave register address */
  27. msg[0].len = 1; /* Number of bytes sent */
  28. msg[1].addr = STPMU1_I2C_ADDRESS;
  29. msg[1].flags = RT_I2C_RD;
  30. msg[1].len = len;
  31. msg[1].buf = buf;
  32. if (rt_i2c_transfer(bus, msg, 2) == 2)
  33. {
  34. return RT_EOK;
  35. }
  36. return RT_ERROR;
  37. }
  38. /* i2c write reg */
  39. static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t data)
  40. {
  41. rt_uint8_t buf[2];
  42. struct rt_i2c_msg msgs;
  43. RT_ASSERT(bus != RT_NULL);
  44. buf[0] = reg; //cmd
  45. buf[1] = data;
  46. msgs.addr = STPMU1_I2C_ADDRESS;
  47. msgs.flags = RT_I2C_WR;
  48. msgs.buf = buf;
  49. msgs.len = 2;
  50. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  51. {
  52. return RT_EOK;
  53. }
  54. return RT_ERROR;
  55. }
  56. /* register direct access */
  57. static rt_err_t stpmu1_read_reg(uint8_t register_id)
  58. {
  59. rt_err_t status = RT_EOK;
  60. uint8_t result = 0;
  61. status = read_reg(pmic_dev, register_id, 1, &result);
  62. /* Check the communication status */
  63. if(status != RT_EOK)
  64. {
  65. Error_Handler();
  66. }
  67. return result;
  68. }
  69. static void stpmu1_write_reg(uint8_t register_id, uint8_t value)
  70. {
  71. uint32_t status = RT_EOK;
  72. uint8_t readval = 0;
  73. status = write_reg(pmic_dev, register_id, (rt_uint8_t)value);
  74. /* Check the communication status */
  75. if(status != RT_EOK)
  76. {
  77. Error_Handler();
  78. }
  79. /* verify register content */
  80. if ((register_id != WATCHDOG_CONTROL_REG) && (register_id <= 0x40))
  81. {
  82. readval = stpmu1_read_reg(register_id);
  83. if (readval != value)
  84. {
  85. Error_Handler();
  86. }
  87. }
  88. }
  89. /** PMIC init */
  90. static uint32_t BSP_PMIC_MspInit(void)
  91. {
  92. GPIO_InitTypeDef GPIO_InitStruct = {0};
  93. __HAL_RCC_GPIOA_CLK_ENABLE();
  94. GPIO_InitStruct.Pin = GPIO_PIN_0;
  95. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  96. GPIO_InitStruct.Pull = GPIO_PULLUP;
  97. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  98. GPIO_InitStruct.Alternate = 0 ;
  99. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  100. HAL_NVIC_SetPriority(EXTI0_IRQn, 0x03, 0x00);
  101. HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  102. return RT_EOK;
  103. }
  104. static uint32_t BSP_PMIC_MspDeInit(void)
  105. {
  106. __HAL_RCC_GPIOA_CLK_DISABLE();
  107. HAL_NVIC_DisableIRQ(EXTI0_IRQn);
  108. HAL_GPIO_DeInit(GPIOA,GPIO_PIN_0);
  109. return RT_EOK;
  110. }
  111. uint16_t buck1_voltage_table[] = {
  112. 600,
  113. 625,
  114. 650,
  115. 675,
  116. 700,
  117. 725,
  118. 750,
  119. 775,
  120. 800,
  121. 825,
  122. 850,
  123. 875,
  124. 900,
  125. 925,
  126. 950,
  127. 975,
  128. 1000,
  129. 1025,
  130. 1050,
  131. 1075,
  132. 1100,
  133. 1125,
  134. 1150,
  135. 1175,
  136. 1200,
  137. 1225,
  138. 1250,
  139. 1275,
  140. 1300,
  141. 1325,
  142. 1350,
  143. 1350,// 31 1,35
  144. };
  145. uint16_t buck2_voltage_table[] = {
  146. 1000, // 1
  147. 1000, //
  148. 1000, // 1
  149. 1000, // 1
  150. 1000, // 1
  151. 1000, // 1
  152. 1000, // 1
  153. 1000, // 1
  154. 1000, // 1
  155. 1000, // 1
  156. 1000, // 1
  157. 1000, // 1
  158. 1000, // 1
  159. 1000, // 1
  160. 1000, // 1
  161. 1000, // 1
  162. 1000, // 1
  163. 1000, // 1
  164. 1050, // 1,05
  165. 1050, // 1,05
  166. 1100, // 1,1
  167. 1100, // 1,1
  168. 1150, // 1,15
  169. 1150, // 1,15
  170. 1200, // 1,2
  171. 1200, // 1,2
  172. 1250, // 1,25
  173. 1250, // 1,25
  174. 1300, // 1,3
  175. 1300, // 1,3
  176. 1350, // 1,35
  177. 1350, // 1,35
  178. 1400, // 1,4
  179. 1400, // 1,4
  180. 1450, // 1,45
  181. 1450, // 1,45
  182. 1500, // 1,5
  183. };
  184. uint16_t buck3_voltage_table[] = {
  185. 1000, // 1
  186. 1000, // 1
  187. 1000, // 1
  188. 1000, // 1
  189. 1000, // 1
  190. 1000, // 1
  191. 1000, // 1
  192. 1000, // 1
  193. 1000, // 1
  194. 1000, // 1
  195. 1000, // 1
  196. 1000, // 1
  197. 1000, // 1
  198. 1000, // 1
  199. 1000, // 1
  200. 1000, // 1
  201. 1000, // 1
  202. 1000, // 1
  203. 1000, // 1
  204. 1000, // 1
  205. 1100, // 1,1
  206. 1100, // 1,1
  207. 1100, // 1,1
  208. 1100, // 1,1
  209. 1200, // 1,2
  210. 1200, // 1,2
  211. 1200, // 1,2
  212. 1200, // 1,2
  213. 1300, // 1,3
  214. 1300, // 1,3
  215. 1300, // 1,3
  216. 1300, // 1,3
  217. 1400, // 1,4
  218. 1400, // 1,4
  219. 1400, // 1,4
  220. 1400, // 1,4
  221. 1500, // 1,5
  222. 1600, // 1,6
  223. 1700, // 1,7
  224. 1800, // 1,8
  225. 1900, // 1,9
  226. 2000, // 2
  227. 2100, // 2,1
  228. 2200, // 2,2
  229. 2300, // 2,3
  230. 2400, // 2,4
  231. 2500, // 2,5
  232. 2600, // 2,6
  233. 2700, // 2,7
  234. 2800, // 2,8
  235. 2900, // 2,9
  236. 3000, // 3
  237. 3100, // 3,1
  238. 3200, // 3,2
  239. 3300, // 3,3
  240. 3400, // 3,4
  241. };
  242. uint16_t buck4_voltage_table[] = {
  243. 600,
  244. 625,
  245. 650,
  246. 675,
  247. 700,
  248. 725,
  249. 750,
  250. 775,
  251. 800,
  252. 825,
  253. 850,
  254. 875,
  255. 900,
  256. 925,
  257. 950,
  258. 975,
  259. 1000,
  260. 1025,
  261. 1050,
  262. 1075,
  263. 1100,
  264. 1125,
  265. 1150,
  266. 1175,
  267. 1200,
  268. 1225,
  269. 1250,
  270. 1275,
  271. 1300,
  272. 1300,
  273. 1350,
  274. 1350,// 31 1,35
  275. 1400,// 32 1,40
  276. 1400,// 33 1,40
  277. 1450,// 34 1,45
  278. 1450,// 35 1,45
  279. 1500,// 36 1,5
  280. 1600,// 37 1,6
  281. 1700,// 38 1,7
  282. 1800,// 39 1,8
  283. 1900,// 40 1,9
  284. 2000,// 41 2,0
  285. 2100,// 42 2,1
  286. 2200,// 43 2,2
  287. 2300,// 44 2,3
  288. 2400,// 45 2,4
  289. 2500,// 46 2,5
  290. 2600,// 47 2,6
  291. 2700,// 48 2,7
  292. 2800,// 49 2,8
  293. 2900,// 50 2,9
  294. 3000,// 51 3,0
  295. 3100,// 52 3,1
  296. 3200,// 53 3,2
  297. 3300,// 54 3,3
  298. 3400,// 55 3,4
  299. 3500,// 56 3,5
  300. 3600,// 57 3,6
  301. 3700,// 58 3,7
  302. 3800,// 59 3,8
  303. 3900,// 60 3,9
  304. };
  305. uint16_t ldo1_voltage_table[] = {
  306. 1700, // 1,7
  307. 1700, // 1,7
  308. 1700, // 1,7
  309. 1700, // 1,7
  310. 1700, // 1,7
  311. 1700, // 1,7
  312. 1700, // 1,7
  313. 1700, // 1,7
  314. 1700, // 1,7
  315. 1800, // 1,8
  316. 1900, // 1,9
  317. 2000, // 2
  318. 2100, // 2,1
  319. 2200, // 2,2
  320. 2300, // 2,3
  321. 2400, // 2,4
  322. 2500, // 2,5
  323. 2600, // 2,6
  324. 2700, // 2,7
  325. 2800, // 2,8
  326. 2900, // 2,9
  327. 3000, // 3
  328. 3100, // 3,1
  329. 3200, // 3,2
  330. 3300, // 3,3
  331. };
  332. uint16_t ldo2_voltage_table[] = {
  333. 1700, // 1,7
  334. 1700, // 1,7
  335. 1700, // 1,7
  336. 1700, // 1,7
  337. 1700, // 1,7
  338. 1700, // 1,7
  339. 1700, // 1,7
  340. 1700, // 1,7
  341. 1700, // 1,7
  342. 1800, // 1,8
  343. 1900, // 1,9
  344. 2000, // 2
  345. 2100, // 2,1
  346. 2200, // 2,2
  347. 2300, // 2,3
  348. 2400, // 2,4
  349. 2500, // 2,5
  350. 2600, // 2,6
  351. 2700, // 2,7
  352. 2800, // 2,8
  353. 2900, // 2,9
  354. 3000, // 3
  355. 3100, // 3,1
  356. 3200, // 3,2
  357. 3300, // 3,3
  358. };
  359. uint16_t ldo3_voltage_table[] = {
  360. 1700, // 1,7
  361. 1700, // 1,7
  362. 1700, // 1,7
  363. 1700, // 1,7
  364. 1700, // 1,7
  365. 1700, // 1,7
  366. 1700, // 1,7
  367. 1700, // 1,7
  368. 1700, // 1,7
  369. 1800, // 1,8
  370. 1900, // 1,9
  371. 2000, // 2
  372. 2100, // 2,1
  373. 2200, // 2,2
  374. 2300, // 2,3
  375. 2400, // 2,4
  376. 2500, // 2,5
  377. 2600, // 2,6
  378. 2700, // 2,7
  379. 2800, // 2,8
  380. 2900, // 2,9
  381. 3000, // 3
  382. 3100, // 3,1
  383. 3200, // 3,2
  384. 3300, // 3,3
  385. 3300, // 3,3
  386. 3300, // 3,3
  387. 3300, // 3,3
  388. 3300, // 3,3
  389. 3300, // 3,3
  390. 3300, // 3,3
  391. 0xFFFF, // VREFDDR
  392. };
  393. uint16_t ldo5_voltage_table[] = {
  394. 1700, // 1,7
  395. 1700, // 1,7
  396. 1700, // 1,7
  397. 1700, // 1,7
  398. 1700, // 1,7
  399. 1700, // 1,7
  400. 1700, // 1,7
  401. 1700, // 1,7
  402. 1700, // 1,7
  403. 1800, // 1,8
  404. 1900, // 1,9
  405. 2000, // 2
  406. 2100, // 2,1
  407. 2200, // 2,2
  408. 2300, // 2,3
  409. 2400, // 2,4
  410. 2500, // 2,5
  411. 2600, // 2,6
  412. 2700, // 2,7
  413. 2800, // 2,8
  414. 2900, // 2,9
  415. 3000, // 3
  416. 3100, // 3,1
  417. 3200, // 3,2
  418. 3300, // 3,3
  419. 3400, // 3,4
  420. 3500, // 3,5
  421. 3600, // 3,6
  422. 3700, // 3,7
  423. 3800, // 3,8
  424. 3900, // 3,9
  425. };
  426. uint16_t ldo6_voltage_table[] = {
  427. 900, // 0,9
  428. 1000, // 1,0
  429. 1100, // 1,1
  430. 1200, // 1,2
  431. 1300, // 1,3
  432. 1400, // 1,4
  433. 1500, // 1,5
  434. 1600, // 1,6
  435. 1700, // 1,7
  436. 1800, // 1,8
  437. 1900, // 1,9
  438. 2000, // 2
  439. 2100, // 2,1
  440. 2200, // 2,2
  441. 2300, // 2,3
  442. 2400, // 2,4
  443. 2500, // 2,5
  444. 2600, // 2,6
  445. 2700, // 2,7
  446. 2800, // 2,8
  447. 2900, // 2,9
  448. 3000, // 3
  449. 3100, // 3,1
  450. 3200, // 3,2
  451. 3300, // 3,3
  452. };
  453. uint16_t ldo4_voltage_table[] = {
  454. 3300, // 3,3
  455. };
  456. uint16_t vref_ddr_voltage_table[] = {
  457. 3300, // 3,3
  458. };
  459. /*
  460. Table of Regulators in PMIC SoC
  461. */
  462. static regul_struct regulators_table[] = {
  463. {
  464. .id = STPMU1_BUCK1,
  465. .voltage_table = buck1_voltage_table,
  466. .voltage_table_size = ARRAY_SIZE(buck1_voltage_table),
  467. .control_reg = BUCK1_CONTROL_REG,
  468. .low_power_reg = BUCK1_PWRCTRL_REG,
  469. .rank = OTP_RANK_BUCK1,
  470. },
  471. {
  472. .id = STPMU1_BUCK2,
  473. .voltage_table = buck2_voltage_table,
  474. .voltage_table_size = ARRAY_SIZE(buck2_voltage_table),
  475. .control_reg = BUCK2_CONTROL_REG,
  476. .low_power_reg = BUCK2_PWRCTRL_REG,
  477. .rank = OTP_RANK_BUCK2,
  478. },
  479. {
  480. .id = STPMU1_BUCK3,
  481. .voltage_table = buck3_voltage_table,
  482. .voltage_table_size = ARRAY_SIZE(buck3_voltage_table),
  483. .control_reg = BUCK3_CONTROL_REG,
  484. .low_power_reg = BUCK3_PWRCTRL_REG,
  485. .rank = OTP_RANK_BUCK3,
  486. },
  487. {
  488. .id = STPMU1_BUCK4,
  489. .voltage_table = buck4_voltage_table,
  490. .voltage_table_size = ARRAY_SIZE(buck4_voltage_table),
  491. .control_reg = BUCK4_CONTROL_REG,
  492. .low_power_reg = BUCK4_PWRCTRL_REG,
  493. .rank = OTP_RANK_BUCK4,
  494. },
  495. {
  496. .id = STPMU1_LDO1,
  497. .voltage_table = ldo1_voltage_table,
  498. .voltage_table_size = ARRAY_SIZE(ldo1_voltage_table),
  499. .control_reg = LDO1_CONTROL_REG,
  500. .low_power_reg = LDO1_PWRCTRL_REG,
  501. .rank = OTP_RANK_LDO1,
  502. },
  503. {
  504. .id = STPMU1_LDO2,
  505. .voltage_table = ldo2_voltage_table,
  506. .voltage_table_size = ARRAY_SIZE(ldo2_voltage_table),
  507. .control_reg = LDO2_CONTROL_REG,
  508. .low_power_reg = LDO2_PWRCTRL_REG,
  509. .rank = OTP_RANK_LDO2,
  510. },
  511. {
  512. .id = STPMU1_LDO3,
  513. .voltage_table = ldo3_voltage_table,
  514. .voltage_table_size = ARRAY_SIZE(ldo3_voltage_table),
  515. .control_reg = LDO3_CONTROL_REG,
  516. .low_power_reg = LDO3_PWRCTRL_REG,
  517. .rank = OTP_RANK_LDO3,
  518. },
  519. {
  520. .id = STPMU1_LDO4,
  521. .voltage_table = ldo4_voltage_table,
  522. .voltage_table_size = ARRAY_SIZE(ldo4_voltage_table),
  523. .control_reg = LDO4_CONTROL_REG,
  524. .low_power_reg = LDO4_PWRCTRL_REG,
  525. .rank = OTP_RANK_LDO4,
  526. },
  527. {
  528. .id = STPMU1_LDO5,
  529. .voltage_table = ldo5_voltage_table ,
  530. .voltage_table_size = ARRAY_SIZE(ldo5_voltage_table),
  531. .control_reg = LDO5_CONTROL_REG,
  532. .low_power_reg = LDO5_PWRCTRL_REG,
  533. .rank = OTP_RANK_LDO5,
  534. },
  535. {
  536. .id = STPMU1_LDO6,
  537. .voltage_table = ldo6_voltage_table ,
  538. .voltage_table_size = ARRAY_SIZE(ldo6_voltage_table),
  539. .control_reg = LDO6_CONTROL_REG,
  540. .low_power_reg = LDO6_PWRCTRL_REG,
  541. .rank = OTP_RANK_LDO6,
  542. },
  543. {
  544. .id = STPMU1_VREFDDR,
  545. .voltage_table = vref_ddr_voltage_table ,
  546. .voltage_table_size = ARRAY_SIZE(vref_ddr_voltage_table),
  547. .control_reg = VREF_DDR_CONTROL_REG,
  548. .low_power_reg = VREF_DDR_PWRCTRL_REG,
  549. .rank = OTP_RANK_VREFDDR,
  550. },
  551. };
  552. #define MAX_REGUL ARRAY_SIZE(regulators_table)
  553. static regul_struct *STPMU1_Get_Regulator_Data(PMIC_RegulId_TypeDef id)
  554. {
  555. uint8_t i;
  556. for (i = 0 ; i < MAX_REGUL ; i++ )
  557. {
  558. if (id == regulators_table[i].id)
  559. {
  560. return &regulators_table[i];
  561. }
  562. }
  563. /* id not found */
  564. Error_Handler();
  565. return NULL;
  566. }
  567. static uint8_t STPMU1_Voltage_Find_Index(PMIC_RegulId_TypeDef id, uint16_t milivolts)
  568. {
  569. regul_struct *regul = STPMU1_Get_Regulator_Data(id);
  570. uint8_t i;
  571. for ( i = 0 ; i < regul->voltage_table_size ; i++)
  572. {
  573. if ( regul->voltage_table[i] == milivolts )
  574. {
  575. LOG_D("idx:%d for %dmV\n\r", (int)i, (int)milivolts);
  576. return i;
  577. }
  578. }
  579. /* voltage not found */
  580. Error_Handler();
  581. return 0;
  582. }
  583. void STPMU1_Enable_Interrupt(PMIC_IRQn IRQn)
  584. {
  585. uint8_t irq_reg , irq_reg_value ;
  586. if (IRQn >= IRQ_NR)
  587. {
  588. return ;
  589. }
  590. /* IRQ register is IRQ Number divided by 8 */
  591. irq_reg = IRQn >> 3 ;
  592. /* value to be set in IRQ register corresponds to BIT(7-N) where N is the Interrupt id modulo 8 */
  593. irq_reg_value = 1 << ( 7 - ( IRQn%8 ) );
  594. /* Clear previous event stored in latch */
  595. stpmu1_write_reg(ITCLEARLATCH1_REG+irq_reg, irq_reg_value );
  596. /* Clear relevant mask to enable interrupt */
  597. stpmu1_write_reg(ITCLEARMASK1_REG+irq_reg, irq_reg_value );
  598. }
  599. void STPMU1_Disable_Interrupt(PMIC_IRQn IRQn)
  600. {
  601. uint8_t irq_reg , irq_reg_value ;
  602. if (IRQn >= IRQ_NR)
  603. {
  604. return ;
  605. }
  606. /* IRQ register is IRQ Number divided by 8 */
  607. irq_reg = IRQn >> 3 ;
  608. /* value to be set in IRQ register corresponds to BIT(7-N) where N is the Interrupt id modulo 8 */
  609. irq_reg_value = 1 << ( 7 - ( IRQn%8 ) );
  610. /* Clear previous event stored in latch */
  611. stpmu1_write_reg(ITCLEARLATCH1_REG+irq_reg, irq_reg_value );
  612. /* Set relevant mask to disable interrupt */
  613. stpmu1_write_reg(ITSETMASK1_REG+irq_reg, irq_reg_value );
  614. }
  615. void BSP_PMIC_INTn_Callback(PMIC_IRQn IRQn)
  616. {
  617. switch (IRQn)
  618. {
  619. case IT_PONKEY_F:
  620. LOG_I("IT_PONKEY_F");
  621. break;
  622. case IT_PONKEY_R:
  623. LOG_I("IT_PONKEY_R");
  624. break;
  625. case IT_WAKEUP_F:
  626. LOG_I("IT_WAKEUP_F");
  627. break;
  628. case IT_WAKEUP_R:
  629. LOG_I("IT_WAKEUP_R");
  630. break;
  631. case IT_VBUS_OTG_F:
  632. LOG_I("IT_VBUS_OTG_F");
  633. break;
  634. case IT_SWOUT_F:
  635. LOG_I("IT_SWOUT_F");
  636. break;
  637. case IT_TWARN_R:
  638. LOG_I("IT_TWARN_R");
  639. break;
  640. case IT_TWARN_F:
  641. LOG_I("IT_TWARN_F");
  642. break;
  643. default:
  644. LOG_I("%d",IRQn);
  645. break;
  646. }
  647. LOG_I(" Interrupt received\n\r");
  648. }
  649. void STPMU1_INTn_Callback(PMIC_IRQn IRQn)
  650. {
  651. BSP_PMIC_INTn_Callback(IRQn);
  652. }
  653. void STPMU1_IrqHandler(void)
  654. {
  655. uint8_t irq_reg,mask,latch_events,i;
  656. for (irq_reg = 0 ; irq_reg < STM32_PMIC_NUM_IRQ_REGS ; irq_reg++)
  657. {
  658. /* Get latch events & active mask from register */
  659. mask = stpmu1_read_reg(ITMASK1_REG+irq_reg);
  660. latch_events = stpmu1_read_reg(ITLATCH1_REG+irq_reg) & ~mask ;
  661. /* Go through all bits for each register */
  662. for (i = 0 ; i < 8 ; i++ )
  663. {
  664. if ( latch_events & ( 1 << i ) )
  665. {
  666. /* Callback with parameter computes as "PMIC Interrupt" enum */
  667. STPMU1_INTn_Callback( (PMIC_IRQn )(irq_reg*8 + (7-i)));
  668. }
  669. }
  670. /* Clear events in appropriate register for the event with mask set */
  671. stpmu1_write_reg(ITCLEARLATCH1_REG+irq_reg, latch_events );
  672. }
  673. }
  674. static void STPMU1_Register_Update(uint8_t register_id, uint8_t value, uint8_t mask)
  675. {
  676. uint8_t initial_value ;
  677. initial_value = stpmu1_read_reg(register_id);
  678. /* Clear bits to update */
  679. initial_value &= ~mask;
  680. /* Update appropriate bits*/
  681. initial_value |= ( value & mask );
  682. /* Send new value on I2C Bus */
  683. stpmu1_write_reg(register_id, initial_value);
  684. }
  685. static void STPMU1_Regulator_Enable(PMIC_RegulId_TypeDef id)
  686. {
  687. regul_struct *regul = STPMU1_Get_Regulator_Data(id);
  688. STPMU1_Register_Update(regul->control_reg,BIT(0),BIT(0));
  689. }
  690. static void STPMU1_Regulator_Voltage_Set(PMIC_RegulId_TypeDef id,uint16_t milivolts)
  691. {
  692. uint8_t voltage_index = STPMU1_Voltage_Find_Index(id,milivolts);
  693. regul_struct *regul = STPMU1_Get_Regulator_Data(id);
  694. STPMU1_Register_Update(regul->control_reg, voltage_index<<2 , 0xFC );
  695. }
  696. void BSP_PMIC_INTn_IRQHandler(void)
  697. {
  698. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  699. STPMU1_IrqHandler();
  700. }
  701. static rt_err_t rt_hw_pmic_init_register(void)
  702. {
  703. stpmu1_write_reg(MAIN_CONTROL_REG, 0x04);
  704. stpmu1_write_reg(VIN_CONTROL_REG, 0xc0);
  705. stpmu1_write_reg(USB_CONTROL_REG, 0x30);
  706. stpmu1_write_reg(MASK_RESET_BUCK_REG, 0x04);
  707. stpmu1_write_reg(MASK_RESET_LDO_REG, 0x00);
  708. stpmu1_write_reg(MASK_RANK_BUCK_REG, 0x00);
  709. stpmu1_write_reg(MASK_RANK_LDO_REG, 0x00);
  710. stpmu1_write_reg(BUCK_PULL_DOWN_REG, 0x00);
  711. stpmu1_write_reg(LDO14_PULL_DOWN_REG, 0x00);
  712. stpmu1_write_reg(LDO56_PULL_DOWN_REG, 0x00);
  713. stpmu1_write_reg(BUCK_ICC_TURNOFF_REG, 0x30);
  714. stpmu1_write_reg(LDO_ICC_TURNOFF_REG, 0x3b);
  715. /* vddcore */
  716. STPMU1_Regulator_Voltage_Set(STPMU1_BUCK1, 1200);
  717. STPMU1_Regulator_Enable(STPMU1_BUCK1);
  718. /* vddddr */
  719. STPMU1_Regulator_Voltage_Set(STPMU1_BUCK2, 1350);
  720. STPMU1_Regulator_Enable(STPMU1_BUCK2);
  721. /* vdd */
  722. STPMU1_Regulator_Voltage_Set(STPMU1_BUCK3, 3300);
  723. STPMU1_Regulator_Enable(STPMU1_BUCK3);
  724. /* 3v3 */
  725. STPMU1_Regulator_Voltage_Set(STPMU1_BUCK4, 3300);
  726. STPMU1_Regulator_Enable(STPMU1_BUCK4);
  727. /* vdda */
  728. STPMU1_Regulator_Voltage_Set(STPMU1_LDO1, 2900);
  729. STPMU1_Regulator_Enable(STPMU1_LDO1);
  730. /* 2v8 */
  731. STPMU1_Regulator_Voltage_Set(STPMU1_LDO2, 2800);
  732. STPMU1_Regulator_Enable(STPMU1_LDO2);
  733. /* vtt_ddr lod3 mode buck2/2 */
  734. STPMU1_Regulator_Voltage_Set(STPMU1_LDO3, 0xFFFF);
  735. STPMU1_Regulator_Enable(STPMU1_LDO3);
  736. /* vdd_usb */
  737. STPMU1_Regulator_Voltage_Set(STPMU1_LDO4, 3300);
  738. STPMU1_Regulator_Enable(STPMU1_LDO4);
  739. /* vdd_sd */
  740. STPMU1_Regulator_Voltage_Set(STPMU1_LDO5, 2900);
  741. STPMU1_Regulator_Enable(STPMU1_LDO5);
  742. /* 1v8 */
  743. STPMU1_Regulator_Voltage_Set(STPMU1_LDO6, 1800);
  744. STPMU1_Regulator_Enable(STPMU1_LDO6);
  745. STPMU1_Regulator_Enable(STPMU1_VREFDDR);
  746. return RT_EOK;
  747. }
  748. static rt_err_t rt_hw_pmic_init(const char *bus_name)
  749. {
  750. PMIC_IRQn irq;
  751. pmic_dev = rt_i2c_bus_device_find(bus_name);
  752. if (pmic_dev == RT_NULL)
  753. {
  754. LOG_E("%s bus not found\n", bus_name);
  755. return -RT_ERROR;
  756. }
  757. if (stpmu1_read_reg(VERSION_STATUS_REG) != PMIC_VERSION_ID)
  758. {
  759. return -RT_EIO;
  760. }
  761. STPMU1_Enable_Interrupt(IT_PONKEY_R);
  762. STPMU1_Enable_Interrupt(IT_PONKEY_F);
  763. /* enable all irqs */
  764. for (irq = IT_SWOUT_R; irq < IRQ_NR; irq++)
  765. {
  766. STPMU1_Enable_Interrupt(irq);
  767. }
  768. return RT_EOK;
  769. }
  770. static rt_err_t rt_hw_pmic_deinit(void)
  771. {
  772. BSP_PMIC_MspDeInit();
  773. return RT_EOK;
  774. }
  775. static int pmic_init(void)
  776. {
  777. rt_err_t result = RT_EOK;
  778. if (IS_ENGINEERING_BOOT_MODE())
  779. {
  780. BSP_PMIC_MspInit();
  781. result = rt_hw_pmic_init(I2C_NAME);
  782. if(result != RT_EOK)
  783. {
  784. LOG_D("stpmic init failed: %02x", result);
  785. rt_hw_pmic_deinit();
  786. return RT_ERROR;
  787. }
  788. rt_hw_pmic_init_register();
  789. }
  790. LOG_I("stpmic init success!");
  791. return RT_EOK;
  792. }
  793. INIT_PREV_EXPORT(pmic_init);
  794. #endif