sd8563_timer_acts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Copyright (c) 2024 Wingcool Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief SD8563 Timer driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. //#include <stdbool.h>
  14. #include <init.h>
  15. #include <irq.h>
  16. #include <drivers/adc.h>
  17. #include <drivers/input/input_dev.h>
  18. #include <sys/util.h>
  19. #include <sys/byteorder.h>
  20. #include <board.h>
  21. #include <soc_pmu.h>
  22. #include <logging/log.h>
  23. #include <device.h>
  24. #include <drivers/gpio.h>
  25. #include <soc.h>
  26. #include <string.h>
  27. #include <drivers/i2c.h>
  28. //#include <board_cfg.h>
  29. #include <drivers/uart.h>
  30. LOG_MODULE_REGISTER(sd8563, CONFIG_SYS_LOG_INPUT_DEV_LEVEL);
  31. #define rtc_slaver_addr (0xA2 >> 1)// 0x51
  32. //#ifndef CONFIG_MERGE_WORK_Q
  33. //#define CONFIG_USED_TP_WORK_QUEUE 0
  34. //#endif
  35. #ifdef CONFIG_USED_TP_WORK_QUEUE
  36. #define CONFIG_TIMER_WORK_Q_STACK_SIZE 1280
  37. struct k_work_q timer_drv_q;
  38. K_THREAD_STACK_DEFINE(timer_work_q_stack, CONFIG_TIMER_WORK_Q_STACK_SIZE);
  39. #endif
  40. struct acts_timer_data {
  41. input_notify_t notify;
  42. const struct device *i2c_dev;
  43. const struct device *gpio_dev;
  44. const struct device *this_dev;
  45. struct gpio_callback key_gpio_cb;
  46. struct k_work init_timer;
  47. bool inited;
  48. #ifdef CONFIG_PM_DEVICE
  49. uint32_t pm_state;
  50. #endif
  51. };
  52. uint16_t timer_crc[2] __attribute((used)) = {0};
  53. uint8_t read_time_data[7] = {0};
  54. static struct acts_timer_data timer_acts_ddata;
  55. static int _sd8563_close_write_protection(const struct device *i2c_dev);
  56. static void _sd8563_open_write_protection(const struct device *i2c_dev);
  57. static void _sd8563_read_time(const struct device *i2c_dev);
  58. static void _sd8563_set_time(const struct device *i2c_dev,
  59. uint8_t set_hour,
  60. uint8_t set_minute,
  61. uint8_t set_week,
  62. uint16_t set_year,
  63. uint8_t set_month,
  64. uint8_t set_day);
  65. extern void uart2_poll_out_ch(int c);
  66. extern uint8_t bySetHour;
  67. extern uint8_t bySetMinute;
  68. extern uint8_t bySetWeekday;
  69. extern uint16_t wSetYear;
  70. extern uint8_t bySetMonth;
  71. extern uint8_t bySetDay;
  72. #include <drivers/hrtimer.h>
  73. #if 1
  74. static struct hrtimer g_rtc_ht_read;
  75. static void timer_acts_handler(struct k_work *work)
  76. {
  77. static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  78. if ((bySetHour != 0xff) || (wSetYear != 0xff))
  79. {
  80. hrtimer_stop(&g_rtc_ht_read);
  81. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  82. {
  83. _sd8563_set_time(external_rtc->i2c_dev, bySetHour, bySetMinute, bySetWeekday, wSetYear, bySetMonth, bySetDay);
  84. _sd8563_open_write_protection(external_rtc->i2c_dev);
  85. }
  86. bySetHour = 0xff;
  87. bySetMinute = 0xff;
  88. bySetWeekday = 0xff;
  89. wSetYear = 0xff;
  90. bySetMonth = 0xff;
  91. bySetDay = 0xff;
  92. hrtimer_restart(&g_rtc_ht_read);
  93. return;
  94. }
  95. _sd8563_read_time(external_rtc->i2c_dev); //不在ISR中完成,防止中断嵌套
  96. }
  97. K_WORK_DEFINE(timer_acts, timer_acts_handler);
  98. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  99. {
  100. //static int t;
  101. //static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  102. //printk("%d ---htimer--\n", t++);
  103. //_sd8563_read_time(external_rtc->i2c_dev);
  104. k_work_submit(&timer_acts); //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
  105. }
  106. static void htimer_read(unsigned int ms)
  107. {
  108. hrtimer_init(&g_rtc_ht_read, htimer_fun, NULL);
  109. hrtimer_start(&g_rtc_ht_read, 1000*ms, 1000*ms);
  110. }
  111. #endif
  112. static void _sd8563_open_write_protection(const struct device *i2c_dev)
  113. {
  114. #if 1
  115. static uint8_t write_cmd[2] = {0};
  116. static uint8_t read_cmd[7] = {0};
  117. int ret = 0;
  118. printk("_sd8563_read_write_protection\n");
  119. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  120. if (ret != 0)
  121. {
  122. printk("i2c_write_read ERR\n");
  123. }
  124. printk("CTR1 = %d\n", read_cmd[0]);
  125. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  126. {
  127. printk("write_protection has been opened\n");
  128. return;
  129. }
  130. //open_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b10101、b01010、b10111
  131. write_cmd[0] = 0x0E;
  132. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  133. if (ret != 0)
  134. {
  135. printk("i2c_write_read ERR\n");
  136. }
  137. read_cmd[0] = (read_cmd[0] & 0x83);
  138. //bit6~bit2 write b0000
  139. write_cmd[1] = read_cmd[0];
  140. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  141. if (ret != 0)
  142. {
  143. printk("step1 i2c write ERR\n");
  144. return;
  145. }
  146. //bit6~bit2 write b10101
  147. write_cmd[1] = read_cmd[0] | 0x54;
  148. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  149. if (ret != 0)
  150. {
  151. printk("step2 i2c write ERR\n");
  152. return;
  153. }
  154. //bit6~bit2 write b01010
  155. write_cmd[1] = read_cmd[0] | 0x28;
  156. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  157. if (ret != 0)
  158. {
  159. printk("step3 i2c write ERR\n");
  160. return;
  161. }
  162. //bit6~bit2 write b10111
  163. write_cmd[1] = read_cmd[0] | 0x5C;
  164. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  165. if (ret != 0)
  166. {
  167. printk("step4 i2c write ERR\n");
  168. return;
  169. }
  170. k_msleep(1);
  171. write_cmd[0] = 0; //CTR1
  172. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  173. if (ret != 0)
  174. {
  175. printk("i2c_write_read ERR\n");
  176. }
  177. printk("CTR1 = %d\n", read_cmd[0]);
  178. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  179. {
  180. printk("write_protection has been opened\n");
  181. }
  182. printk("_sd8563_open_write_protection exit\n");
  183. #endif
  184. }
  185. static int _sd8563_close_write_protection(const struct device *i2c_dev)
  186. {
  187. #if 1
  188. static uint8_t write_cmd[2] = {0};
  189. static uint8_t read_cmd[7] = {0};
  190. int ret = 0;
  191. printk("_sd8563_read_write_protection\n");
  192. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  193. if (ret != 0)
  194. {
  195. printk("i2c_write_read ERR\n");
  196. return 0;
  197. }
  198. printk("CTR1 = %d\n", read_cmd[0]);
  199. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  200. {
  201. printk("write_protection has been closed\n");
  202. return 1;
  203. }
  204. //close_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b11100、b00011、b01110
  205. write_cmd[0] = 0x0E;
  206. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  207. if (ret != 0)
  208. {
  209. printk("i2c_write_read ERR\n");
  210. return 0;
  211. }
  212. read_cmd[0] = (read_cmd[0] & 0x83);
  213. //bit6~bit2 write b0000
  214. write_cmd[1] = read_cmd[0];
  215. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  216. if (ret != 0)
  217. {
  218. printk("step1 i2c write ERR\n");
  219. return 0;
  220. }
  221. //bit6~bit2 write b11100
  222. write_cmd[1] = read_cmd[0] | 0x70;
  223. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  224. if (ret != 0)
  225. {
  226. printk("step2 i2c write ERR\n");
  227. return 0;
  228. }
  229. //bit6~bit2 write b00011
  230. write_cmd[1] = read_cmd[0] | 0x0C;
  231. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  232. if (ret != 0)
  233. {
  234. printk("step3 i2c write ERR\n");
  235. return 0;
  236. }
  237. //bit6~bit2 write b01110
  238. write_cmd[1] = read_cmd[0] | 0x38;
  239. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  240. if (ret != 0)
  241. {
  242. printk("step4 i2c write ERR\n");
  243. return 0;
  244. }
  245. k_msleep(1);
  246. write_cmd[0] = 0; //CTR1
  247. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  248. if (ret != 0)
  249. {
  250. printk("i2c_write_read ERR\n");
  251. return 0;
  252. }
  253. printk("CTR1 = %d\n", read_cmd[0]);
  254. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  255. {
  256. printk("write_protection has been closed\n");
  257. return 1;
  258. }
  259. printk("_sd8563_close_write_protection exit\n");
  260. return 0;
  261. #else
  262. return 1;
  263. #endif
  264. }
  265. static void _sd8563_set_time(const struct device *i2c_dev,
  266. uint8_t set_hour,
  267. uint8_t set_minute,
  268. uint8_t set_week,
  269. uint16_t set_year,
  270. uint8_t set_month,
  271. uint8_t set_day)
  272. {
  273. #if 1
  274. static uint8_t write_cmd[8] = {0};
  275. static uint8_t read_cmd[7] = {0};
  276. bool power_on_set_time_data = false;
  277. int ret = 0;
  278. printk("_sd8563_set_time start\n");
  279. write_cmd[0] = 0x02; //sec
  280. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  281. if (ret != 0)
  282. {
  283. printk("i2c_write_read ERR\n");
  284. return;
  285. }
  286. if ((set_hour == 0xFF) && (set_year == 0xFF)) //power on
  287. {
  288. printk("read_cmd[0] = %d\n", read_cmd[0]);
  289. if ((read_cmd[0] & 0x80) == 0) //bit7:0SF/
  290. {
  291. printk("bit7:0SF is 0,The time has been set\n");
  292. return;
  293. }
  294. power_on_set_time_data = true;
  295. }
  296. _sd8563_read_time(i2c_dev); //read time
  297. printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  298. read_time_data[6], read_time_data[5], read_time_data[4], read_time_data[3], read_time_data[2], read_time_data[1], read_time_data[0]);
  299. if(set_hour != 0xFF)
  300. {
  301. read_time_data[0] = 0;
  302. read_time_data[1] = set_minute;//(set_minute / 10) * 16 + set_minute % 10; //DEC TO BCD CODE
  303. read_time_data[2] = set_hour;//(set_hour / 10) * 16 + set_hour % 10; //DEC TO BCD CODE
  304. read_time_data[4] = set_week % 7;//0:Sun. 1:Mon. 2:Tue. 3:Wed. 4:Thu. 5:Fri. 6:Sat.
  305. }
  306. else if (set_year != 0xFF)
  307. {
  308. read_time_data[3] = set_day;//(set_day / 10) * 16 + set_day % 10; //DEC TO BCD CODE
  309. read_time_data[5] = set_month;//(set_month / 10) * 16 + set_month % 10; //DEC TO BCD CODE
  310. read_time_data[6] = set_year;//(set_year / 10) * 16 + set_year % 10; //DEC TO BCD CODE
  311. }
  312. if (power_on_set_time_data == true)
  313. {
  314. //BCD code
  315. write_cmd[1] = 0; //sec
  316. write_cmd[2] = 0x35; //min
  317. write_cmd[3] = 0x11; //hour
  318. write_cmd[4] = 0x12; //day
  319. write_cmd[5] = 0x06; //week
  320. write_cmd[6] = 0x10; //mon
  321. write_cmd[7] = 0x24; //year
  322. printk("power on set time and date\n");
  323. }
  324. else
  325. {
  326. //BCD code
  327. for (uint8_t i = 0; i < 7; i++)
  328. {
  329. write_cmd[i + 1] = (read_time_data[i] / 10) * 16 + read_time_data[i] % 10;
  330. }
  331. //write_cmd[1] = read_time_data[0]; //sec
  332. //write_cmd[2] = read_time_data[1]; //min
  333. //write_cmd[3] = read_time_data[2]; //hour
  334. //write_cmd[4] = read_time_data[3]; //day
  335. //write_cmd[5] = read_time_data[4]; //week
  336. //write_cmd[6] = read_time_data[5]; //mon
  337. //write_cmd[7] = read_time_data[6]; //year
  338. }
  339. ret = i2c_write(i2c_dev, write_cmd, 8, rtc_slaver_addr);
  340. if (ret != 0)
  341. {
  342. printk("i2c write ERR\n");
  343. return;
  344. }
  345. k_msleep(1);
  346. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 7);
  347. if (ret != 0)
  348. {
  349. printk("i2c_write_read ERR\n");
  350. return;
  351. }
  352. printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  353. read_cmd[6], read_cmd[5], read_cmd[4], read_cmd[3], read_cmd[2], read_cmd[1], read_cmd[0]);
  354. printk("_sd8563_set_time exit\n");
  355. #endif
  356. }
  357. static void _sd8563_read_time(const struct device *i2c_dev)
  358. {
  359. #if 1
  360. uint8_t i, check_sum = 0x52;
  361. static uint8_t write_cmd[1] = {0x02};
  362. //static uint8_t read_time_data[7] = {0};
  363. int ret = 0;
  364. //printk("_sd8563_read_time\n");
  365. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_time_data, 7);
  366. //ret = i2c_burst_read(i2c_dev, rtc_slaver_addr, 0x02, read_cmd, 3);
  367. //ret = i2c_read(i2c_dev, read_time_data, 7, rtc_slaver_addr);
  368. if (ret != 0)
  369. {
  370. printk("i2c_write_read ERR\n");
  371. }
  372. read_time_data[0] = read_time_data[0] & 0x7F; //bit7:0SF/
  373. read_time_data[5] = read_time_data[5] & 0x7F; //bit7:C/century
  374. //uart2 send data start ==============================================//
  375. uart2_poll_out_ch(0x5A); //报文表头
  376. uart2_poll_out_ch(0x54);
  377. for (i = 0; i < 7; i++)
  378. {
  379. read_time_data[i] = (read_time_data[i] / 16) * 10 + read_time_data[i] % 16; //DEC TO BCD CODE
  380. check_sum += read_time_data[i];
  381. uart2_poll_out_ch(read_time_data[i]);
  382. }
  383. uart2_poll_out_ch(check_sum); //checksum
  384. //uart2 send data end ==============================================//
  385. //printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  386. // read_time_data[6], read_time_data[5], read_time_data[4], read_time_data[3], read_time_data[2], read_time_data[1], read_time_data[0]);
  387. #endif
  388. }
  389. static void _sd8563_init_work(struct k_work *work)
  390. {
  391. struct acts_timer_data *external_rtc = &timer_acts_ddata;
  392. printk("sd8563 init work\n");
  393. external_rtc->inited = true;
  394. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  395. {
  396. //k_msleep(2);
  397. _sd8563_set_time(external_rtc->i2c_dev, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
  398. //k_msleep(2);
  399. _sd8563_open_write_protection(external_rtc->i2c_dev);
  400. }
  401. #if 0
  402. uint8_t i;
  403. for (i=0; i < 20; i++)
  404. {
  405. k_msleep(1000);
  406. _sd8563_read_time(external_rtc->i2c_dev);
  407. }
  408. #endif
  409. htimer_read(1000); //1000ms = 1s
  410. printk("sd8563 init work exit\n");
  411. }
  412. static int _sd8563_acts_init(const struct device *dev)
  413. {
  414. struct acts_timer_data *external_rtc = dev->data;
  415. printk("sd8563 acts init\n");
  416. #if 1
  417. external_rtc->this_dev = (struct device *)dev;
  418. external_rtc->i2c_dev = (struct device *)device_get_binding(CONFIG_SD8563_I2C_NAME);
  419. if (!external_rtc->i2c_dev) {
  420. printk("can not access right i2c device\n");
  421. return -1;
  422. }
  423. external_rtc->inited = false;
  424. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  425. #ifdef CONFIG_USED_TP_WORK_QUEUE
  426. k_work_queue_start(&timer_drv_q, timer_work_q_stack, K_THREAD_STACK_SIZEOF(timer_work_q_stack), 7, NULL);
  427. k_work_submit_to_queue(&timer_drv_q, &external_rtc->init_timer);
  428. #else
  429. k_work_submit(&external_rtc->init_timer);
  430. #endif
  431. #endif
  432. printk("sd8563 acts init exit\n");
  433. return 0;
  434. }
  435. #ifdef CONFIG_PM_DEVICE
  436. static void _sd8563_suspend(const struct device *dev)
  437. {
  438. //struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  439. printk("sd8563 suspend\n");
  440. hrtimer_stop(&g_rtc_ht_read);
  441. }
  442. static void _sd8563_resume(const struct device *dev)
  443. {
  444. struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  445. external_rtc->i2c_dev = (struct device *)device_get_binding(CONFIG_SD8563_I2C_NAME);
  446. if (!external_rtc->i2c_dev) {
  447. printk("can not access right i2c device\n");
  448. return;
  449. }
  450. external_rtc->inited = false;
  451. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  452. printk("sd8563 resume\n");
  453. #ifdef CONFIG_USED_TP_WORK_QUEUE
  454. k_work_submit_to_queue(&tp_drv_q, &external_rtc->init_timer);
  455. #else
  456. k_work_submit(&external_rtc->init_timer);
  457. #endif
  458. }
  459. static int _sd8563_pm_control(const struct device *dev, enum pm_device_action action)
  460. {
  461. int ret = 0;
  462. //printk("sd8563 pm control\n");
  463. switch (action) {
  464. case PM_DEVICE_ACTION_SUSPEND:
  465. break;
  466. case PM_DEVICE_ACTION_RESUME:
  467. break;
  468. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  469. _sd8563_suspend(dev);
  470. break;
  471. case PM_DEVICE_ACTION_LATE_RESUME:
  472. _sd8563_resume(dev);
  473. break;
  474. default:
  475. break;
  476. }
  477. return ret;
  478. }
  479. #else /* CONFIG_PM_DEVICE */
  480. static int _sd8563_pm_control(const struct device *dev, uint32_t ctrl_command,
  481. void *context, device_pm_cb cb, void *arg)
  482. {
  483. }
  484. #endif
  485. #if IS_ENABLED(CONFIG_SD8563)
  486. DEVICE_DEFINE(sd8563, CONFIG_SD8563_DEV_NAME, _sd8563_acts_init,
  487. _sd8563_pm_control, &timer_acts_ddata, NULL, POST_KERNEL,
  488. 50, NULL);
  489. #endif