jpeg_hw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /****************************************************************************
  7. * Included Files
  8. ****************************************************************************/
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <soc.h>
  12. #include <spicache.h>
  13. #include "jpeg_hw.h"
  14. #include <logging/log.h>
  15. #include <flash/spi_flash.h>
  16. #include <tracing/tracing.h>
  17. /****************************************************************************
  18. * Pre-processor Definitions
  19. ****************************************************************************/
  20. #define OUTPUT_TEMP_BUFFER_SIZE (4 * 1024)
  21. //#define CONFIG_BLOCK_MODE 1
  22. #ifdef CONFIG_BLOCK_MODE
  23. #define CONFIG_BLOCK_SIZE 1024
  24. #endif
  25. LOG_MODULE_REGISTER(jpeg_hw_dev, LOG_LEVEL_INF);
  26. #if CONFIG_JPEG_HW_INPUT_SDMA_CHAN < 1 || CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN > 3 \
  27. || CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN < 1 || CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN > 3
  28. # error "CONFIG_JPEG_HW_SDMA_CHAN must in range [1, 3]."
  29. #endif
  30. // sdma config
  31. #define SDMA ((SDMA_Type *)SDMA_REG_BASE)
  32. #define JPEG_INPUT_CHAN (&(SDMA->CHAN_CTL[CONFIG_JPEG_HW_INPUT_SDMA_CHAN]))
  33. #define JPEG_INPUT_LINE (&(SDMA->LINE_CTL[CONFIG_JPEG_HW_INPUT_SDMA_CHAN]))
  34. #define JPEG_OUTPUT_CHAN (&(SDMA->CHAN_CTL[CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN]))
  35. #define JPEG_OUTPUT_LINE (&(SDMA->LINE_CTL[CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN]))
  36. #define JPEG_COUPLE_CHAN (&(SDMA->CHAN_CTL[CONFIG_JPEG_HW_COUPLE_SDMA_CHAN]))
  37. #define JPEG_COUPLE_LINE (&(SDMA->LINE_CTL[CONFIG_JPEG_HW_COUPLE_SDMA_CHAN]))
  38. #define __SDMA_IRQ_ID(n) (IRQ_ID_SDMA##n)
  39. #define _SDMA_IRQ_ID(n) __SDMA_IRQ_ID(n)
  40. #define JPEG_INPUT_IRQ_ID _SDMA_IRQ_ID(CONFIG_JPEG_HW_INPUT_SDMA_CHAN)
  41. #define JPEG_OUTPUT_IRQ_ID _SDMA_IRQ_ID(CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN)
  42. #define JPEG_OUTPUT_IRQ_HF_PENDDING (1 << (CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN + 16))
  43. #define JPEG_OUTPUT_IRQ_TC_PENDDING (1 << CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN)
  44. // jpeg hw config
  45. #define JPEG_HW ((JPEG_HW_Type *)JPEG_REG_BASE)
  46. /****************************************************************************
  47. * Private Types
  48. ****************************************************************************/
  49. struct jpeg_hw_instance {
  50. jpeg_hw_instance_callback_t callback;
  51. void *user_data;
  52. };
  53. struct jpeg_data {
  54. uint8_t is_busy : 1;
  55. uint8_t is_locked : 1;
  56. uint8_t is_decoding : 1;
  57. struct jpeg_info_t *cfg_info;
  58. struct jpeg_hw_instance instance;
  59. struct k_sem wait_sem;
  60. struct k_mutex mutex;
  61. };
  62. /****************************************************************************
  63. * Private Function Prototypes
  64. ****************************************************************************/
  65. static int _jpeg_hw_poll_unlock(const struct device *dev, int timeout_ms);
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. #ifndef CONFIG_SOC_NO_PSRAM
  70. __in_section_unique(jpeg.bss.temp_buffer)
  71. uint8_t jpeg_sram_temp_buffer[OUTPUT_TEMP_BUFFER_SIZE];
  72. #endif
  73. static struct jpeg_data jpeg_hw_drv_data;
  74. /****************************************************************************
  75. * Private Functions
  76. ****************************************************************************/
  77. static void _jpeg_hw_dump(void)
  78. {
  79. printk("CMU_DEVCLKEN0: 0x%x\n", sys_read32(CMU_DEVCLKEN0));
  80. printk("CMU_MEMCLKEN0: 0x%x\n", sys_read32(CMU_MEMCLKEN0));
  81. printk("CMU_MEMCLKEN1: 0x%x\n", sys_read32(CMU_MEMCLKEN1));
  82. printk("CMU_MEMCLKSRC0: 0x%x\n", sys_read32(CMU_MEMCLKSRC0));
  83. printk("CMU_MEMCLKSRC1: 0x%x\n", sys_read32(CMU_MEMCLKSRC1));
  84. printk("COREPLL_CTL: 0x%x\n", sys_read32(COREPLL_CTL));
  85. printk("CMU_SYSCLK: 0x%x\n", sys_read32(CMU_SYSCLK));
  86. printk("CMU_JPEGCLK: 0x%x\n", sys_read32(CMU_JPEGCLK));
  87. for (int i = 0; i < 23; i++) {
  88. printk("reg[%d] : 0x%x \n", i + 1, *(uint32_t *)(JPEG_REG_BASE + i * 4));
  89. }
  90. printk("SDMA->IP: 0x%x \n", SDMA->IP);
  91. printk("SDMA->IE: 0x%x \n", SDMA->IE);
  92. printk("SDMA->COUPLE_CONFIG: 0x%x \n", SDMA->COUPLE_CONFIG);
  93. printk("SDMA->COUPLE_BUF_ADDR: 0x%x \n", SDMA->COUPLE_BUF_ADDR);
  94. printk("SDMA->COUPLE_BUF_SIZE: 0x%x \n", SDMA->COUPLE_BUF_SIZE);
  95. printk("SDMA->COUPLE_WRITER_POINTER: 0x%x \n", SDMA->COUPLE_WRITER_POINTER);
  96. printk("SDMA->COUPLE_READ_POINTER: 0x%x \n", SDMA->COUPLE_READ_POINTER);
  97. printk("JPEG_INPUT_CHAN->CTL: 0x%x \n", JPEG_INPUT_CHAN->CTL);
  98. printk("JPEG_INPUT_CHAN->SADDR: 0x%x \n", JPEG_INPUT_CHAN->SADDR);
  99. printk("JPEG_INPUT_CHAN->DADDR: 0x%x \n", JPEG_INPUT_CHAN->DADDR);
  100. printk("JPEG_INPUT_CHAN->BC: 0x%x \n", JPEG_INPUT_CHAN->BC);
  101. printk("JPEG_INPUT_CHAN->RC: 0x%x \n", JPEG_INPUT_CHAN->RC);
  102. printk("JPEG_OUTPUT_CHAN->CTL: 0x%x \n", JPEG_OUTPUT_CHAN->CTL);
  103. printk("JPEG_OUTPUT_CHAN->SADDR: 0x%x \n", JPEG_OUTPUT_CHAN->SADDR);
  104. printk("JPEG_OUTPUT_CHAN->DADDR: 0x%x \n", JPEG_OUTPUT_CHAN->DADDR);
  105. printk("JPEG_OUTPUT_CHAN->BC: 0x%x \n", JPEG_OUTPUT_CHAN->BC);
  106. printk("JPEG_OUTPUT_CHAN->RC: 0x%x \n", JPEG_OUTPUT_CHAN->RC);
  107. printk("JPEG_OUTPUT_LINE->LENGTH: 0x%x \n", JPEG_OUTPUT_LINE->LENGTH);
  108. printk("JPEG_OUTPUT_LINE->COUNT: 0x%x \n", JPEG_OUTPUT_LINE->COUNT);
  109. printk("JPEG_OUTPUT_LINE->SSTRIDE: 0x%x \n", JPEG_OUTPUT_LINE->SSTRIDE);
  110. printk("JPEG_OUTPUT_LINE->DSTRIDE: 0x%x \n", JPEG_OUTPUT_LINE->DSTRIDE);
  111. printk("JPEG_COUPLE_CHAN->CTL: 0x%x \n", JPEG_COUPLE_CHAN->CTL);
  112. printk("JPEG_COUPLE_CHAN->SADDR: 0x%x \n", JPEG_COUPLE_CHAN->SADDR);
  113. printk("JPEG_COUPLE_CHAN->DADDR: 0x%x \n", JPEG_COUPLE_CHAN->DADDR);
  114. printk("JPEG_COUPLE_CHAN->BC: 0x%x \n", JPEG_COUPLE_CHAN->BC);
  115. printk("JPEG_COUPLE_CHAN->RC: 0x%x \n", JPEG_COUPLE_CHAN->RC);
  116. printk("JPEG_COUPLE_LINE->LENGTH: 0x%x \n", JPEG_COUPLE_LINE->LENGTH);
  117. printk("JPEG_COUPLE_LINE->COUNT: 0x%x \n", JPEG_COUPLE_LINE->COUNT);
  118. printk("JPEG_COUPLE_LINE->SSTRIDE: 0x%x \n", JPEG_COUPLE_LINE->SSTRIDE);
  119. printk("JPEG_COUPLE_LINE->DSTRIDE: 0x%x \n", JPEG_COUPLE_LINE->DSTRIDE);
  120. }
  121. static int _jpeg_hw_open(const struct device *dev)
  122. {
  123. struct jpeg_data *data = dev->data;
  124. int ret = -EBUSY;
  125. k_mutex_lock(&data->mutex, K_FOREVER);
  126. if (!data->is_busy) {
  127. data->is_busy = 1;
  128. ret = 0;
  129. }
  130. k_mutex_unlock(&data->mutex);
  131. k_sem_init(&data->wait_sem, 0, 1);
  132. acts_reset_peripheral_assert(RESET_ID_JPEG);
  133. acts_clock_peripheral_enable(CLOCK_ID_JPEG);
  134. acts_reset_peripheral_deassert(RESET_ID_JPEG);
  135. return ret;
  136. }
  137. //config JPEG AC VLC code part1 (reg11)
  138. static void _jpeg_set_ac_vlc_part1(struct jpeg_info_t *cfg_info)
  139. {
  140. uint32_t val = 0;
  141. uint32_t shiftval = 0;
  142. for (uint8_t i = 0; i < 6; i++) {
  143. val |= (uint32_t)(cfg_info->AC_TAB0[i] << shiftval);
  144. shiftval += (i + 2);
  145. }
  146. JPEG_HW->JPEG_AC_VLC[0] = val;
  147. }
  148. /* config JPEG AC VLC code part2 (reg12) */
  149. static void _jpeg_set_ac_vlc_part2(struct jpeg_info_t *cfg_info)
  150. {
  151. uint32_t val = 0;
  152. uint32_t shiftval = 0;
  153. for(uint8_t i = 6; i < 10; i++) {
  154. val |= (uint32_t)(cfg_info->AC_TAB0[i] << shiftval);
  155. shiftval += 8;
  156. }
  157. JPEG_HW->JPEG_AC_VLC[1] = val;
  158. }
  159. /* config JPEG AC VLC code part3 (reg13) */
  160. static void _jpeg_set_ac_vlc_part3(struct jpeg_info_t *cfg_info)
  161. {
  162. uint32_t val = 0;
  163. uint32_t shiftval = 0;
  164. for(uint8_t i = 10; i < 14; i++) {
  165. val |= (uint32_t)(cfg_info->AC_TAB0[i] << shiftval);
  166. shiftval += 8;
  167. }
  168. JPEG_HW->JPEG_AC_VLC[2] = val;
  169. }
  170. /* config JPEG AC VLC code part4 (reg14) */
  171. static void _jpeg_set_ac_vlc_part4(struct jpeg_info_t *cfg_info)
  172. {
  173. uint32_t val = 0;
  174. uint32_t shiftval = 0;
  175. uint8_t i;
  176. for(i = 14; i < 16; i++) {
  177. val |= (uint32_t)(cfg_info->AC_TAB0[i] << shiftval);
  178. shiftval += 8;
  179. }
  180. for(i = 0; i < 4; i++) {
  181. val |= (uint32_t)(cfg_info->AC_TAB1[i] << shiftval);
  182. shiftval += (i + 2);
  183. }
  184. JPEG_HW->JPEG_AC_VLC[3] = val;
  185. }
  186. /* config JPEG AC VLC code part5 (reg15) */
  187. static void _jpeg_set_ac_vlc_part5(struct jpeg_info_t *cfg_info)
  188. {
  189. uint32_t val = 0;
  190. uint32_t shiftval = 0;
  191. for(uint8_t i = 4; i < 8; i++) {
  192. val |= (uint32_t)(cfg_info->AC_TAB1[i] << shiftval);
  193. if (i == 6)
  194. shiftval += 8;
  195. else
  196. shiftval += (i + 2);
  197. }
  198. JPEG_HW->JPEG_AC_VLC[4] = val;
  199. }
  200. /* config JPEG AC VLC code part6 (reg16) */
  201. static void _jpeg_set_ac_vlc_part6(struct jpeg_info_t *cfg_info)
  202. {
  203. uint32_t val = 0;
  204. uint32_t shiftval = 0;
  205. for(uint8_t i = 8; i < 12; i++) {
  206. val |= (uint32_t)(cfg_info->AC_TAB1[i] << shiftval);
  207. shiftval += 8;
  208. }
  209. JPEG_HW->JPEG_AC_VLC[5] = val;
  210. }
  211. /* config JPEG AC VLC code part7 (reg17) */
  212. static void _jpeg_set_ac_vlc_part7(struct jpeg_info_t *cfg_info)
  213. {
  214. uint32_t val = 0;
  215. uint32_t shiftval = 0;
  216. for(uint8_t i = 12; i < 16; i++) {
  217. val |= (uint32_t)(cfg_info->AC_TAB1[i] << shiftval);
  218. shiftval += 8;
  219. }
  220. JPEG_HW->JPEG_AC_VLC[6] = val;
  221. }
  222. /* config JPEG DC VLC code part1 (reg18) */
  223. static void _jpeg_set_dc_vlc_part1(struct jpeg_info_t *cfg_info)
  224. {
  225. uint32_t val = 0;
  226. uint32_t shiftval = 0;
  227. for(uint8_t i = 0; i < 8; i++) {
  228. val |= (uint32_t)(cfg_info->DC_TAB0[i] << shiftval);
  229. if (i < 2)
  230. shiftval += (i + 2);
  231. else
  232. shiftval += 4;
  233. }
  234. JPEG_HW->JPEG_DC_VLC[0] = val;
  235. }
  236. /* config JPEG DC VLC code part2 (reg19) */
  237. static void _jpeg_set_dc_vlc_part2(struct jpeg_info_t *cfg_info)
  238. {
  239. uint32_t val = 0;
  240. uint32_t shiftval = 0;
  241. for(uint8_t i = 8; i < 16; i++) {
  242. val |= (uint32_t)(cfg_info->DC_TAB0[i] << shiftval);
  243. shiftval += 4;
  244. }
  245. JPEG_HW->JPEG_DC_VLC[1] = val;
  246. }
  247. /* config JPEG DC VLC code part3 (reg20) */
  248. static void _jpeg_set_dc_vlc_part3(struct jpeg_info_t *cfg_info)
  249. {
  250. uint32_t val = 0;
  251. uint32_t shiftval = 0;
  252. for(uint8_t i = 0; i < 8; i++) {
  253. val |= (uint32_t)(cfg_info->DC_TAB1[i] << shiftval);
  254. if (i < 2)
  255. shiftval += (i + 2);
  256. else
  257. shiftval += 4;
  258. }
  259. JPEG_HW->JPEG_DC_VLC[2] = val;
  260. }
  261. /* config JPEG DC VLC code part4 (reg21) */
  262. static void _jpeg_set_dc_vlc_part4(struct jpeg_info_t *cfg_info)
  263. {
  264. uint32_t val = 0;
  265. uint32_t shiftval = 0;
  266. for(uint8_t i = 8; i < 16; i++) {
  267. val |= (uint32_t)(cfg_info->DC_TAB1[i] << shiftval);
  268. shiftval += 4;
  269. }
  270. JPEG_HW->JPEG_DC_VLC[3] = val;
  271. }
  272. static void _jpeg_input_config(struct jpeg_info_t *cfg_info)
  273. {
  274. uint8_t * stream = (uint8_t *)cfg_info->stream_addr + cfg_info->stream_offset;
  275. SDMA->IE |= (1<<SDMAIE_SDMA2TCIE);
  276. JPEG_INPUT_CHAN->CTL = (0 << SDMA2CTL_YDAM) | (1 << SDMA2CTL_DSTSL)
  277. | (0 << SDMA2CTL_YSAM) | (0 << SDMA2CTL_SRCSL);
  278. JPEG_INPUT_CHAN->SADDR = buf_is_nor(stream) ?
  279. (uint32_t)cache_to_uncache_by_master(stream, SPI0_CACHE_MASTER_SDMA) :
  280. (uint32_t)cache_to_uncache(stream);
  281. JPEG_INPUT_CHAN->DADDR = 0;//JPG
  282. #ifdef CONFIG_BLOCK_MODE
  283. JPEG_INPUT_CHAN->BC = CONFIG_BLOCK_SIZE;
  284. #else
  285. JPEG_INPUT_CHAN->BC = cfg_info->stream_size + 512;
  286. #endif
  287. cfg_info->stream_offset += JPEG_INPUT_CHAN->BC;
  288. }
  289. static void _jpeg_output_config(struct jpeg_info_t *cfg_info)
  290. {
  291. uint8_t bytes_per_pix = 0;
  292. uint16_t crop_width = cfg_info->window_w;
  293. uint16_t crop_height = cfg_info->window_h;
  294. uint32_t out_bmp_addr = (uint32_t)cache_to_uncache(cfg_info->output_bmp);
  295. // RGB888
  296. if (cfg_info->output_format == 0) {
  297. bytes_per_pix = 3;
  298. } else {
  299. bytes_per_pix = 2;
  300. }
  301. SDMA->IE |= (1 << SDMAIE_SDMA3TCIE);
  302. JPEG_OUTPUT_CHAN->CTL = (1 << SDMA3CTL_STRME)
  303. | (cfg_info->output_format << SDMA3CTL_JPEG_OUT_FORMAT)
  304. | (0 << SDMA3CTL_YDAM) | (0 << SDMA3CTL_DSTSL)
  305. | (0 << SDMA3CTL_YSAM) | (1 << SDMA3CTL_SRCSL);
  306. JPEG_OUTPUT_CHAN->SADDR = 0;//JPG
  307. JPEG_OUTPUT_CHAN->DADDR = out_bmp_addr;
  308. //config output window (swreg6)(swreg7)(swreg8)
  309. JPEG_HW->JPEG_WINDOW_START = cfg_info->window_y << 16 | cfg_info->window_x;
  310. JPEG_HW->JPEG_WINDOW_SIZE = cfg_info->window_h << 16 | cfg_info->window_w;
  311. JPEG_OUTPUT_CHAN->BC = crop_width * crop_height * bytes_per_pix;
  312. JPEG_OUTPUT_LINE->LENGTH = crop_width * bytes_per_pix;
  313. JPEG_OUTPUT_LINE->COUNT = crop_height;
  314. JPEG_OUTPUT_LINE->SSTRIDE = 0;
  315. JPEG_OUTPUT_LINE->DSTRIDE = cfg_info->output_stride * bytes_per_pix;
  316. #ifndef CONFIG_SOC_NO_PSRAM
  317. //psram addr
  318. if (buf_is_psram_un(out_bmp_addr)) {
  319. JPEG_OUTPUT_CHAN->DADDR = (uint32_t)&jpeg_sram_temp_buffer;
  320. JPEG_COUPLE_CHAN->CTL = (1 << SDMA3CTL_STRME);
  321. JPEG_COUPLE_CHAN->SADDR = (uint32_t)&jpeg_sram_temp_buffer;
  322. JPEG_COUPLE_CHAN->DADDR = out_bmp_addr;
  323. JPEG_COUPLE_CHAN->BC = crop_width * crop_height * bytes_per_pix;
  324. JPEG_COUPLE_LINE->LENGTH = crop_width * bytes_per_pix;
  325. JPEG_COUPLE_LINE->COUNT = crop_height;
  326. JPEG_COUPLE_LINE->SSTRIDE = crop_width * bytes_per_pix;
  327. JPEG_COUPLE_LINE->DSTRIDE = cfg_info->output_stride * bytes_per_pix;
  328. SDMA->COUPLE_CONFIG = (0x1f << 2) | CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN;
  329. SDMA->COUPLE_BUF_ADDR = (uint32_t)&jpeg_sram_temp_buffer;
  330. SDMA->COUPLE_BUF_SIZE = sizeof(jpeg_sram_temp_buffer);
  331. }
  332. #else
  333. SDMA->COUPLE_CONFIG = 0;
  334. #endif
  335. }
  336. static int _jpeg_hw_config(const struct device *dev, struct jpeg_info_t *cfg_info)
  337. {
  338. struct jpeg_data *data = dev->data;
  339. int ret = 0;
  340. int bitstreamoffset =0;
  341. int stream_start_bit = 0;
  342. int image_block_w = 0;
  343. int image_block_h = 0;
  344. if (!cfg_info || cfg_info->yuv_mode != YUV420) {
  345. LOG_ERR("yuv_mode %d not support\n",cfg_info ? cfg_info->yuv_mode : 0);
  346. return -EINVAL;
  347. }
  348. data->cfg_info = cfg_info;
  349. // size must align to 16 bytes block
  350. image_block_w= (cfg_info->image_w + 15) / 16 * 16;
  351. image_block_h = (cfg_info->image_h + 15) / 16 * 16;
  352. //config image w & h (swreg2)
  353. JPEG_HW->JPEG_CONFIG = ((image_block_w >> 4) << 23)
  354. | ((image_block_h >> 4) << 14);
  355. //config yuv to rgb mode bt709
  356. JPEG_HW->JPEG_CONFIG |= (1 << 8);
  357. if (cfg_info->output_format == 0) {
  358. //RB SWAP , BGR888
  359. //JPEG_HW->JPEG_CONFIG |= (1 << 9);
  360. } else {
  361. // RGB 565
  362. JPEG_HW->JPEG_CONFIG &= (~(1 << 9));
  363. }
  364. //config RGB_FIFO_DRQ_MODE
  365. JPEG_HW->JPEG_CONFIG |= (cfg_info->ds_mode << 7);
  366. //config Amount of qtabs
  367. JPEG_HW->JPEG_CONFIG |= (cfg_info->amountOfQTables << 5);
  368. //config yuv mode ,only support yuv 420 ,no need config
  369. //JPEG_HW->JPEG_CONFIG |= (cfg_info->yuv_mode << 2);
  370. // need check (swreg3)
  371. JPEG_HW->JPEG_BITSTREAM = (bitstreamoffset << 14)
  372. | (511 << 5) | (stream_start_bit << 0);
  373. //config stream size (swreg5)
  374. #ifdef CONFIG_BLOCK_MODE
  375. JPEG_HW->JPEG_STREAM_SIZE = CONFIG_BLOCK_SIZE - 1;
  376. #else
  377. JPEG_HW->JPEG_STREAM_SIZE = cfg_info->stream_size + 1024 - 1;
  378. #endif
  379. //not support scale ,no need config
  380. //JPEG_HW->JPEG_OUTPUT
  381. //config image resulation (swreg9)
  382. JPEG_HW->JPEG_RESOLUTION = ((cfg_info->image_h % 16) % 2 ?
  383. cfg_info->image_h + 1 : cfg_info->image_h) << 13 | cfg_info->image_w;
  384. //config Huffman table select (swreg10)
  385. JPEG_HW->JPEG_HUFF_TABLE = (cfg_info->scan.Ta[2] << 3)
  386. | (cfg_info->scan.Ta[1] << 2)
  387. | (cfg_info->scan.Td[2] << 1)
  388. | (cfg_info->scan.Td[1] << 0);
  389. _jpeg_set_ac_vlc_part1(cfg_info);
  390. _jpeg_set_ac_vlc_part2(cfg_info);
  391. _jpeg_set_ac_vlc_part3(cfg_info);
  392. _jpeg_set_ac_vlc_part4(cfg_info);
  393. _jpeg_set_ac_vlc_part5(cfg_info);
  394. _jpeg_set_ac_vlc_part6(cfg_info);
  395. _jpeg_set_ac_vlc_part7(cfg_info);
  396. _jpeg_set_dc_vlc_part1(cfg_info);
  397. _jpeg_set_dc_vlc_part2(cfg_info);
  398. _jpeg_set_dc_vlc_part3(cfg_info);
  399. _jpeg_set_dc_vlc_part4(cfg_info);
  400. //switch jpeg mem clk to jpeg
  401. sys_write32(sys_read32(CMU_MEMCLKSRC1) | (0x1f << 24),CMU_MEMCLKSRC1);
  402. _jpeg_input_config(cfg_info);
  403. _jpeg_output_config(cfg_info);
  404. //_jpeg_hw_dump();
  405. return ret;
  406. }
  407. static int _jpeg_hw_decode(const struct device *dev)
  408. {
  409. struct jpeg_data *data = dev->data;
  410. k_mutex_lock(&data->mutex, K_FOREVER);
  411. #ifdef CONFIG_SPI_FLASH_ACTS
  412. struct jpeg_info_t *cfg_info = data->cfg_info;
  413. if(buf_is_nor(cfg_info->stream_addr) && !data->is_locked) {
  414. spi0_nor_xip_lock();
  415. data->is_locked = 1;
  416. }
  417. #endif
  418. data->is_decoding = 1;
  419. k_mutex_unlock(&data->mutex);
  420. k_sem_reset(&data->wait_sem);
  421. sys_trace_u32(SYS_TRACE_ID_IMG_HW_DECODE, 0);
  422. // start output sdma
  423. JPEG_OUTPUT_CHAN->START = 1;
  424. // start input sdma
  425. JPEG_INPUT_CHAN->START = 1;
  426. SDMA->COUPLE_START = 1;
  427. //start jpeg hw, enable timout and decoder interrupt (swreg0)
  428. JPEG_HW->JPEG_ENABLE |= 0x05;
  429. return 0;
  430. }
  431. static int _jpeg_hw_close(const struct device *dev)
  432. {
  433. struct jpeg_data *data = dev->data;
  434. int ret = -EINVAL;
  435. k_mutex_lock(&data->mutex, K_FOREVER);
  436. data->instance.callback = NULL;
  437. data->instance.user_data = NULL;
  438. data->cfg_info = NULL;
  439. data->is_busy = 0;
  440. k_mutex_unlock(&data->mutex);
  441. // reset sdma when jpeg close
  442. JPEG_OUTPUT_CHAN->START = 0;
  443. JPEG_INPUT_CHAN->START = 0;
  444. SDMA->COUPLE_START = 0;
  445. JPEG_HW->JPEG_ENABLE = 0x00;
  446. //switch jpeg mem clk to mcu
  447. sys_write32(sys_read32(CMU_MEMCLKSRC1) & (~(0x1f << 24)),CMU_MEMCLKSRC1);
  448. acts_clock_peripheral_disable(CLOCK_ID_JPEG);
  449. return ret;
  450. }
  451. static int _jpeg_hw_poll_unlock(const struct device *dev, int timeout_ms)
  452. {
  453. struct jpeg_data *data = dev->data;
  454. if (data->is_decoding) {
  455. k_timeout_t timeout = (timeout_ms >= 0) ? K_MSEC(timeout_ms) : K_FOREVER;
  456. return k_sem_take(&data->wait_sem, timeout);
  457. } else {
  458. data->is_decoding = 0;
  459. return 0;
  460. }
  461. }
  462. static int _jpeg_hw_poll(const struct device *dev, int timeout_ms)
  463. {
  464. struct jpeg_data *data = dev->data;
  465. #ifdef CONFIG_SPI_FLASH_ACTS
  466. struct jpeg_info_t *cfg_info = data->cfg_info;
  467. #endif
  468. int ret;
  469. k_mutex_lock(&data->mutex, K_FOREVER);
  470. ret = _jpeg_hw_poll_unlock(dev, timeout_ms);
  471. #ifdef CONFIG_SPI_FLASH_ACTS
  472. if(buf_is_nor(cfg_info->stream_addr) && data->is_locked) {
  473. spi0_nor_xip_unlock();
  474. data->is_locked = 0;
  475. }
  476. #endif
  477. k_mutex_unlock(&data->mutex);
  478. return ret;
  479. }
  480. static int _jpeg_hw_register_callback(const struct device *dev,
  481. jpeg_hw_instance_callback_t callback,
  482. void *user_data)
  483. {
  484. struct jpeg_data *data = dev->data;
  485. int ret = -EINVAL;
  486. k_mutex_lock(&data->mutex, K_FOREVER);
  487. if (data->is_busy) {
  488. data->instance.user_data = user_data;
  489. data->instance.callback = callback;
  490. ret = 0;
  491. }
  492. k_mutex_unlock(&data->mutex);
  493. return ret;
  494. }
  495. static void _jpeg_hw_get_capabilities(const struct device *dev,
  496. struct jpeg_hw_capabilities *capabilities)
  497. {
  498. capabilities->min_width = 48;
  499. capabilities->min_height = 48;
  500. capabilities->max_width = 480;
  501. capabilities->max_height = 480;
  502. }
  503. static void _jpeg_hw_isr(const void *arg)
  504. {
  505. const struct device *dev = arg;
  506. struct jpeg_data *data = dev->data;
  507. int dec_status = 0;
  508. #ifdef CONFIG_BLOCK_MODE
  509. int block_len = CONFIG_BLOCK_SIZE;
  510. struct jpeg_info_t *cfg_info = data->cfg_info;
  511. #endif
  512. /*first stop jpeg when error avoid interrupt can't clear*/
  513. JPEG_HW->JPEG_ENABLE = 0;
  514. if((JPEG_HW->JPEG_INTERRUPT & 0x02) != 0){
  515. sys_trace_end_call(SYS_TRACE_ID_IMG_HW_DECODE);
  516. data->is_decoding = 0;
  517. k_sem_give(&data->wait_sem);
  518. dec_status = DECODE_FRAME_FINISHED;
  519. }
  520. #ifdef CONFIG_BLOCK_MODE
  521. else if((JPEG_HW->JPEG_INTERRUPT & 0x4) != 0) {
  522. if(cfg_info->stream_offset < cfg_info->stream_size) {
  523. if(cfg_info->stream_offset + block_len > cfg_info->stream_size) {
  524. block_len = cfg_info->stream_size - cfg_info->stream_offset;
  525. }
  526. JPEG_INPUT_CHAN->SADDR += JPEG_INPUT_CHAN->BC;
  527. JPEG_INPUT_CHAN->BC = block_len;
  528. JPEG_HW->JPEG_STREAM_SIZE = block_len - 1;
  529. cfg_info->stream_offset += block_len;
  530. dec_status = DECODE_BLOCK_FINISHED;
  531. } else {
  532. LOG_ERR("error: %x\n",JPEG_HW->JPEG_INTERRUPT);
  533. }
  534. }
  535. #endif
  536. else if((JPEG_HW->JPEG_INTERRUPT & 0xEC) != 0) {
  537. sys_trace_end_call(SYS_TRACE_ID_IMG_HW_DECODE);
  538. LOG_ERR("error: %x\n",JPEG_HW->JPEG_INTERRUPT);
  539. _jpeg_hw_dump();
  540. data->is_decoding = 0;
  541. k_sem_give(&data->wait_sem);
  542. dec_status = DECODE_DRROR;
  543. }
  544. struct jpeg_hw_instance *instance = &data->instance;
  545. if (instance->callback) {
  546. instance->callback(dec_status, instance->user_data);
  547. }
  548. JPEG_HW->JPEG_INTERRUPT = 0;
  549. }
  550. #if 0
  551. static void jpeg_output_isr(const void *arg)
  552. {
  553. int dec_status = 0;
  554. const struct device *dev = arg;
  555. struct jpeg_data *data = dev->data;
  556. if(SDMA->IP & JPEG_OUTPUT_IRQ_TC_PENDDING) {
  557. // reset sdma when jpeg close
  558. JPEG_OUTPUT_CHAN->START = 0;
  559. JPEG_INPUT_CHAN->START = 0;
  560. SDMA->COUPLE_START = 0;
  561. SDMA->IP |= JPEG_OUTPUT_IRQ_TC_PENDDING;
  562. dec_status = DECODE_BLOCK_FINISHED;
  563. k_sem_give(&data->wait_sem);
  564. struct jpeg_hw_instance *instance = &data->instance;
  565. if (instance->callback) {
  566. instance->callback(dec_status, instance->user_data);
  567. }
  568. }
  569. }
  570. #endif
  571. DEVICE_DECLARE(jpeg_hw);
  572. static int _jpeg_hw_init(const struct device *dev)
  573. {
  574. struct jpeg_data *data = dev->data;
  575. k_mutex_init(&data->mutex);
  576. SDMA->IE |= BIT(CONFIG_JPEG_HW_INPUT_SDMA_CHAN)
  577. | BIT(CONFIG_JPEG_HW_OUTPUT_SDMA_CHAN);
  578. IRQ_CONNECT(IRQ_ID_JPEG, 0, _jpeg_hw_isr, DEVICE_GET(jpeg_hw), 0);
  579. irq_enable(IRQ_ID_JPEG);
  580. sys_write32(sys_read32(CMU_MEMCLKEN1) | (0x1f << 24), CMU_MEMCLKEN1);
  581. //IRQ_CONNECT(JPEG_OUTPUT_IRQ_ID, 0, jpeg_output_isr, DEVICE_GET(jpeg_hw), 0);
  582. //irq_enable(JPEG_OUTPUT_IRQ_ID);
  583. clk_set_rate(CLOCK_ID_JPEG, KHZ(CONFIG_JPEG_CLOCK_KHZ));
  584. data->is_locked = 0;
  585. return 0;
  586. }
  587. #ifdef CONFIG_PM_DEVICE
  588. static int _jpeg_hw_pm_control(const struct device *dev, enum pm_device_action action)
  589. {
  590. int ret = 0;
  591. switch (action) {
  592. case PM_DEVICE_ACTION_SUSPEND:
  593. case PM_DEVICE_ACTION_FORCE_SUSPEND:
  594. case PM_DEVICE_ACTION_TURN_OFF:
  595. break;
  596. case PM_DEVICE_ACTION_RESUME:
  597. default:
  598. break;
  599. }
  600. return ret;
  601. }
  602. #endif /* CONFIG_PM_DEVICE */
  603. static const struct jpeg_hw_driver_api jpeg_hw_drv_api = {
  604. .open = _jpeg_hw_open,
  605. .close = _jpeg_hw_close,
  606. .config = _jpeg_hw_config,
  607. .get_capabilities = _jpeg_hw_get_capabilities,
  608. .register_callback = _jpeg_hw_register_callback,
  609. .decode = _jpeg_hw_decode,
  610. .poll = _jpeg_hw_poll,
  611. };
  612. /****************************************************************************
  613. * Public Functions
  614. ****************************************************************************/
  615. #if IS_ENABLED(CONFIG_JPEG_HW_DEV)
  616. DEVICE_DEFINE(jpeg_hw, CONFIG_JPEG_HW_DEV_NAME, &_jpeg_hw_init,
  617. _jpeg_hw_pm_control, &jpeg_hw_drv_data, NULL, POST_KERNEL,
  618. CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &jpeg_hw_drv_api);
  619. #endif