sgm832a_acts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2024 Wingcool Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief SGM832A Timer driver for Actions SoC
  9. * typec_num: 0 for typec0, input/output; 1 for typec1, input
  10. */
  11. #include <errno.h>
  12. #include <kernel.h>
  13. #include <string.h>
  14. //#include <stdbool.h>
  15. #include <init.h>
  16. #include <irq.h>
  17. #include <drivers/adc.h>
  18. #include <drivers/input/input_dev.h>
  19. #include <sys/util.h>
  20. #include <sys/byteorder.h>
  21. #include <board.h>
  22. #include <soc_pmu.h>
  23. #include <logging/log.h>
  24. #include <device.h>
  25. #include <drivers/gpio.h>
  26. #include <soc.h>
  27. #include <string.h>
  28. #include <drivers/i2c.h>
  29. //#include <board_cfg.h>
  30. #include <drivers/uart.h>
  31. LOG_MODULE_REGISTER(sgm832a, CONFIG_SYS_LOG_INPUT_DEV_LEVEL);
  32. #define sgm832_typec0_slaver_addr (0x8A >> 1)
  33. #define sgm832_typec1_slaver_addr (0x82 >> 1)
  34. //sgm832 Register Address
  35. #define CONFIGURATION_REGISTER 0x00
  36. #define SHUNT_VOLTAGE_REGISTER 0x01
  37. #define BUS_VOLTAGE_REGISTER 0x02
  38. #define POWER_REGISTER 0x03
  39. #define CURRENT_REGISTER 0x04
  40. #define CALIBRATION_REGISTER 0x05
  41. #define MASK_ENABLE_REGISTER 0x06
  42. #define ALERT_LIMIT_REGISTER 0x07
  43. #define MANUFACTURER_ID_REGISTER 0xFE
  44. #define DIE_ID_REGISTER 0xFF
  45. //#ifndef CONFIG_MERGE_WORK_Q
  46. //#define CONFIG_USED_TP_WORK_QUEUE 0
  47. //#endif
  48. #ifdef CONFIG_USED_TP_WORK_QUEUE
  49. #define CONFIG_TIMER_WORK_Q_STACK_SIZE 1280
  50. struct k_work_q timer_drv_q;
  51. K_THREAD_STACK_DEFINE(timer_work_q_stack, CONFIG_TIMER_WORK_Q_STACK_SIZE);
  52. #endif
  53. struct acts_sgm_data {
  54. input_notify_t notify;
  55. const struct device *i2c_dev;
  56. const struct device *gpio_dev;
  57. const struct device *this_dev;
  58. struct gpio_callback key_gpio_cb;
  59. struct k_work init_timer;
  60. bool inited;
  61. #ifdef CONFIG_PM_DEVICE
  62. uint32_t pm_state;
  63. #endif
  64. };
  65. uint16_t sgm_crc[2] __attribute((used)) = {0};
  66. float f_ShuntVoltage[2] = {0.0}, f_BusVoltage[2] = {0.0}, f_Power[2] = {0.0}, f_Current[2] = {0.0};
  67. static struct acts_sgm_data sgm_acts_ddata;
  68. static void sgm832_typec_get_data(const struct device *i2c_dev, uint16_t sgm832_typec_slaver_addr, uint8_t typec_num);
  69. //static void sgm832_typec1_get_data(const struct device *i2c_dev);
  70. extern void uart2_poll_out_ch(int c);
  71. #include <drivers/hrtimer.h>
  72. #if 1
  73. static struct hrtimer g_sgm_ht_read;
  74. static void sgm832_acts_handler(struct k_work *work)
  75. {
  76. static struct acts_sgm_data *power_consumption = &sgm_acts_ddata;
  77. sgm832_typec_get_data(power_consumption->i2c_dev, sgm832_typec0_slaver_addr, 0); //不在ISR中完成,防止中断嵌套
  78. sgm832_typec_get_data(power_consumption->i2c_dev, sgm832_typec1_slaver_addr, 1); //不在ISR中完成,防止中断嵌套
  79. //sgm832_typec1_get_data(power_consumption->i2c_dev); //不在ISR中完成,防止中断嵌套
  80. }
  81. K_WORK_DEFINE(sgm832_acts, sgm832_acts_handler);
  82. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  83. {
  84. //static int t;
  85. //printk("%d ---htimer--\n", t++);
  86. k_work_submit(&sgm832_acts); //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
  87. }
  88. static void htimer_read(unsigned int ms)
  89. {
  90. hrtimer_init(&g_sgm_ht_read, htimer_fun, NULL);
  91. hrtimer_start(&g_sgm_ht_read, 1000*ms, 1000*ms);
  92. }
  93. #endif
  94. static void sgm832_typec_get_data(const struct device *i2c_dev, uint16_t sgm832_typec_slaver_addr, uint8_t typec_num)
  95. {
  96. #if 1
  97. //uint8_t i;
  98. //float f_ShuntVoltage = 0.0, f_BusVoltage = 0.0, f_Power = 0.0, f_Current = 0.0;
  99. //uint16_t u16_ShuntVoltage;
  100. static uint8_t write_cmd[10] = {CONFIGURATION_REGISTER, SHUNT_VOLTAGE_REGISTER,
  101. BUS_VOLTAGE_REGISTER, POWER_REGISTER,
  102. CURRENT_REGISTER, CALIBRATION_REGISTER,
  103. MASK_ENABLE_REGISTER, ALERT_LIMIT_REGISTER,
  104. MANUFACTURER_ID_REGISTER, DIE_ID_REGISTER};
  105. static uint16_t read_data[1] = {0};
  106. int ret = 0;
  107. printk("sgm832 typec%d get data\n", typec_num);
  108. //00H Configuartion Register
  109. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, write_cmd, 1, read_data, 2);
  110. if (ret != 0)
  111. {
  112. printk("sgm832 typec%d i2c_write_read Configuartion Register ERR\n", typec_num);
  113. }
  114. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  115. printk("ConfigReg: 0x%04x\n", read_data[0]);
  116. //01H Shunt Voltage Register
  117. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[1], 1, read_data, 2);
  118. if (ret == 0)
  119. {
  120. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  121. if (read_data[0] & 0x8000) {
  122. // Sign-extend for negative ShuntVoltage
  123. read_data[0] = ~read_data[0] + 1;
  124. }
  125. f_ShuntVoltage[typec_num] = read_data[0] * 2.5 * 0.001;
  126. }
  127. else
  128. {
  129. printk("sgm832 typec%d i2c_write_read Shunt_Voltage ERR\n", typec_num);
  130. }
  131. printk("Shunt_Voltage: %.4f mV\n", f_ShuntVoltage[typec_num]);
  132. //printk("ConfigReg: 0x%04x, ShuntVoltage: %.4f mV, BusVoltage: %.3f V, Power: 0x%04x, Current: 0x%04x, Calibration: 0x%04x, MaskEnable: 0x%04x, AlertLimit: 0x%04x\n",
  133. // read_data[0], f_ShuntVoltage, f_BusVoltage, read_data[3], read_data[4], read_data[5], read_data[6], read_data[7]);
  134. //02H Bus Voltage Register
  135. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[2], 1, read_data, 2);
  136. if (ret != 0)
  137. {
  138. printk("sgm832 typec%d i2c_write_read Bus Voltage Register ERR\n", typec_num);
  139. }
  140. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  141. f_BusVoltage[typec_num] = (float)read_data[0] * 1.25 * 0.001;
  142. printk("Bus_Voltage: %.3f V\n", f_BusVoltage[typec_num]);
  143. //03H Power Register
  144. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[3], 1, read_data, 2);
  145. if (ret != 0)
  146. {
  147. printk("sgm832 typec%d i2c_write_read Power Register ERR\n", typec_num);
  148. }
  149. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  150. f_Power[typec_num] = (float)read_data[0] * 225 * 0.001; //W
  151. printk("Power: %.3f mW\n", f_Power[typec_num]);
  152. //04H Current Register
  153. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[4], 1, read_data, 2);
  154. if (ret == 0)
  155. {
  156. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  157. if (read_data[0] & 0x8000) {
  158. // Sign-extend for negative Current
  159. read_data[0] = ~read_data[0] + 1;
  160. }
  161. f_Current[typec_num] = read_data[0] * 0.00009; //A
  162. }
  163. else
  164. {
  165. printk("sgm832 typec%d i2c_write_read Current Register ERR\n", typec_num);
  166. }
  167. printk("Current data: 0x%04x\n", read_data[0]);
  168. printk("Current: %.3f mA\n", f_Current[typec_num]);
  169. //05H Calibration Register
  170. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[5], 1, read_data, 2);
  171. if (ret != 0)
  172. {
  173. printk("sgm832 typec%d i2c_write_read Calibration Register ERR\n", typec_num);
  174. }
  175. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  176. printk("Calibration: 0x%04x\n", read_data[0]);
  177. //06H Mask/Enable Register
  178. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[6], 1, read_data, 2);
  179. if (ret != 0)
  180. {
  181. printk("sgm832 typec%d i2c_write_read Mask/Enable Register ERR\n", typec_num);
  182. }
  183. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  184. //printk("Mask/Enable: 0x%04x\n", read_data[0]);
  185. //07H AlertLimit Register
  186. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[7], 1, read_data, 2);
  187. if (ret != 0)
  188. {
  189. printk("sgm832 typec%d i2c_write_read AlertLimit Register ERR\n", typec_num);
  190. }
  191. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  192. //printk("AlertLimit: 0x%04x\n", read_data[0]);
  193. //FEH Manufacturer ID Register
  194. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[8], 1, read_data, 2);
  195. if (ret != 0)
  196. {
  197. printk("sgm832 typec%d i2c_write_read Manufacturer ID Register ERR\n", typec_num);
  198. }
  199. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  200. //printk("Manufacturer_ID: 0x%04x\n", read_data[0]);
  201. //FFH Die ID Register
  202. ret = i2c_write_read(i2c_dev, sgm832_typec_slaver_addr, &write_cmd[9], 1, read_data, 2);
  203. if (ret != 0)
  204. {
  205. printk("sgm832 typec%d i2c_write_read Die ID Register ERR\n", typec_num);
  206. }
  207. read_data[0] = ((read_data[0] & 0xFF00) >> 8) + ((read_data[0] & 0x00FF) << 8);
  208. //printk("Die_ID: 0x%04x\n", read_data[0]);
  209. #endif
  210. }
  211. #if 0
  212. static void sgm832_typec1_get_shuntvoltage(const struct device *i2c_dev)
  213. {
  214. #if 1
  215. //uint8_t i;
  216. float f_ShuntVoltage = 0.0, f_BusVoltage = 0.0;
  217. //uint16_t u16_ShuntVoltage;
  218. static uint8_t write_cmd[2] = {CONFIGURATION_REGISTER, MANUFACTURER_ID_REGISTER};
  219. static uint16_t read_data[8] = {0};
  220. int ret = 0;
  221. printk("sgm832 typec1 get data\n");
  222. ret = i2c_write_read(i2c_dev, sgm832_typec1_slaver_addr, write_cmd, 1, read_data, sizeof(read_data));
  223. //ret = i2c_burst_read(i2c_dev, sgm832_typec1_slaver_addr, CONFIGURATION_REGISTER, read_data, sizeof(read_data));
  224. //ret = i2c_read(i2c_dev, read_data, 7, sgm832_typec1_slaver_addr);
  225. if (ret == 0)
  226. {
  227. if (read_data[1] & 0x8000) {
  228. // Sign-extend for negative ShuntVoltage
  229. read_data[1] = ~read_data[1] + 1;
  230. //printf( "ShuntVoltage: 0x%04x\n", u16_ShuntVoltage);
  231. f_ShuntVoltage = read_data[1] * 2.5 * 0.001;
  232. }
  233. else {
  234. f_ShuntVoltage = read_data[1] * 2.5 * 0.001;
  235. }
  236. //read_data[2] = ((read_data[2] & 0xFF00) >> 8) + ((read_data[2] & 0x00FF) << 8);
  237. f_BusVoltage = (float)read_data[2] * 1.25 * 0.001;
  238. //uart2_poll_out_ch(read_data[i]); //uart2 send data
  239. }
  240. else
  241. {
  242. printk("sgm832 typec1 i2c_write_read ERR\n");
  243. }
  244. printk("ConfigReg: 0x%04x, ShuntVoltage: %.4f mV, BusVoltage: %.3f V, Power: 0x%04x, Current: 0x%04x, Calibration: 0x%04x, MaskEnable: 0x%04x, AlertLimit: 0x%04x\n",
  245. read_data[0], f_ShuntVoltage, f_BusVoltage, read_data[3], read_data[4], read_data[5], read_data[6], read_data[7]);
  246. ret = i2c_write_read(i2c_dev, sgm832_typec1_slaver_addr, &write_cmd[1], 1, read_data, 4);
  247. //ret = i2c_burst_read(i2c_dev, sgm832_typec1_slaver_addr, CONFIGURATION_REGISTER, read_data, sizeof(read_data));
  248. //ret = i2c_read(i2c_dev, read_data, 7, sgm832_typec1_slaver_addr);
  249. if (ret == 0)
  250. {
  251. }
  252. else
  253. {
  254. printk("sgm832 typec1 i2c_write_read ERR\n");
  255. }
  256. printk("ManufacturerID: 0x%04x, DieID: 0x%04x\n",
  257. read_data[0], read_data[1]);
  258. #endif
  259. }
  260. #endif
  261. static void sgm832_typec_calibration(const struct device *i2c_dev, uint16_t sgm832_typec_slaver_addr, uint8_t typec_num)
  262. {
  263. //static uint8_t configurate_write_data[3] = {CONFIGURATION_REGISTER, 0x47, 0x6F};
  264. static uint8_t calibrate_write_data0[3] = {CALIBRATION_REGISTER, 0x07, 0xcd}; //typec0, input/output
  265. static uint8_t calibrate_write_data1[3] = {CALIBRATION_REGISTER, 0x07, 0xcd}; //typec1, input
  266. int ret = 0;
  267. //ret = i2c_write(i2c_dev, configurate_write_data, 3, sgm832_typec_slaver_addr);
  268. //if (ret != 0)
  269. //{
  270. // printk("i2c write sgm832_typec%d Configuration Register ERR\n", typec_num);
  271. // return;
  272. //}
  273. switch(typec_num)
  274. {
  275. case 0:
  276. ret = i2c_write(i2c_dev, calibrate_write_data0, 3, sgm832_typec_slaver_addr);
  277. break;
  278. case 1:
  279. ret = i2c_write(i2c_dev, calibrate_write_data1, 3, sgm832_typec_slaver_addr);
  280. break;
  281. default:
  282. break;
  283. }
  284. if (ret != 0)
  285. {
  286. printk("i2c write sgm832_typec%d Calibration Register ERR\n", typec_num);
  287. }
  288. }
  289. static void _sgm832a_init_work(struct k_work *work)
  290. {
  291. struct acts_sgm_data *power_consumption = &sgm_acts_ddata;
  292. printk("sgm832a init work\n");
  293. power_consumption->inited = true;
  294. //write Calibration Register
  295. sgm832_typec_calibration(power_consumption->i2c_dev, sgm832_typec0_slaver_addr, 0);
  296. sgm832_typec_calibration(power_consumption->i2c_dev, sgm832_typec1_slaver_addr, 1);
  297. htimer_read(500); //1000ms = 1s
  298. }
  299. static int _sgm832a_acts_init(const struct device *dev)
  300. {
  301. struct acts_sgm_data *power_consumption = dev->data;
  302. printk("sgm832a acts init\n");
  303. #if 1
  304. power_consumption->this_dev = (struct device *)dev;
  305. power_consumption->i2c_dev = (struct device *)device_get_binding(CONFIG_SGM832A_I2C_NAME);
  306. if (!power_consumption->i2c_dev) {
  307. printk("can not access right i2c device\n");
  308. return -1;
  309. }
  310. power_consumption->inited = false;
  311. k_work_init(&power_consumption->init_timer, _sgm832a_init_work);
  312. #ifdef CONFIG_USED_TP_WORK_QUEUE
  313. k_work_queue_start(&timer_drv_q, timer_work_q_stack, K_THREAD_STACK_SIZEOF(timer_work_q_stack), 7, NULL);
  314. k_work_submit_to_queue(&timer_drv_q, &power_consumption->init_timer);
  315. #else
  316. k_work_submit(&power_consumption->init_timer);
  317. #endif
  318. #endif
  319. printk("sgm832a acts init exit\n");
  320. return 0;
  321. }
  322. #ifdef CONFIG_PM_DEVICE
  323. static void _sgm832a_suspend(const struct device *dev)
  324. {
  325. //struct acts_sgm_data *power_consumption = (struct acts_sgm_data *)dev->data;
  326. printk("sgm832a suspend\n");
  327. hrtimer_stop(&g_sgm_ht_read);
  328. }
  329. static void _sgm832a_resume(const struct device *dev)
  330. {
  331. struct acts_sgm_data *power_consumption = (struct acts_sgm_data *)dev->data;
  332. power_consumption->i2c_dev = (struct device *)device_get_binding(CONFIG_SGM832A_I2C_NAME);
  333. if (!power_consumption->i2c_dev) {
  334. printk("can not access right i2c device\n");
  335. return;
  336. }
  337. power_consumption->inited = false;
  338. k_work_init(&power_consumption->init_timer, _sgm832a_init_work);
  339. printk("sgm832a resume\n");
  340. #ifdef CONFIG_USED_TP_WORK_QUEUE
  341. k_work_submit_to_queue(&tp_drv_q, &power_consumption->init_timer);
  342. #else
  343. k_work_submit(&power_consumption->init_timer);
  344. #endif
  345. }
  346. static int _sgm832a_pm_control(const struct device *dev, enum pm_device_action action)
  347. {
  348. int ret = 0;
  349. //printk("sgm832a pm control\n");
  350. switch (action) {
  351. case PM_DEVICE_ACTION_SUSPEND:
  352. break;
  353. case PM_DEVICE_ACTION_RESUME:
  354. break;
  355. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  356. _sgm832a_suspend(dev);
  357. break;
  358. case PM_DEVICE_ACTION_LATE_RESUME:
  359. _sgm832a_resume(dev);
  360. break;
  361. default:
  362. break;
  363. }
  364. return ret;
  365. }
  366. #else /* CONFIG_PM_DEVICE */
  367. static int _sgm832a_pm_control(const struct device *dev, uint32_t ctrl_command,
  368. void *context, device_pm_cb cb, void *arg)
  369. {
  370. }
  371. #endif
  372. #if IS_ENABLED(CONFIG_SGM832A)
  373. DEVICE_DEFINE(sgm832a, CONFIG_SGM832A_DEV_NAME, _sgm832a_acts_init,
  374. _sgm832a_pm_control, &sgm_acts_ddata, NULL, POST_KERNEL,
  375. 50, NULL);
  376. #endif