jpeg_drv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Arkmicro 1668 jpeg driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/version.h>
  8. #include <linux/kernel.h>
  9. #include <linux/kthread.h>
  10. #include <linux/cdev.h>
  11. #include <linux/device.h>
  12. #include <linux/fs.h>
  13. #include <linux/io.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/wait.h>
  16. #include <linux/fb.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/delay.h>
  19. #include <linux/completion.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/poll.h>
  23. #include "ark_jpeg_io.h"
  24. #include "jpeg.h"
  25. #include "jpeg_priv.h"
  26. extern int ark_bootanimation_display_init(int width, int height, unsigned int addr);
  27. extern int ark_bootanimation_display_uninit(void);
  28. extern int ark_bootanimation_set_display_addr(unsigned int addr);
  29. struct platform_device *ark_jpeg_platform_device = NULL;
  30. struct decode_buffer {
  31. unsigned decode_kernel_phys_base;
  32. unsigned decode_size;
  33. };
  34. #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
  35. typedef struct {
  36. unsigned int magic;
  37. int hasBootlogo;
  38. int bootlogoDisplayTime;
  39. int aniCount;
  40. int aniWidth;
  41. int aniHeight;
  42. int aniFps;
  43. int aniDelayHideTime;
  44. int reserved[4];
  45. } BANIHEADER;
  46. static void animation_jpeg_decode(struct ark_jpeg_context *context, unsigned int width, unsigned int height,
  47. unsigned int src_addr, unsigned int dest_addr)
  48. {
  49. unsigned int val;
  50. val = readl(context->mmio_base + JPEG_CTRL);
  51. writel(val | (3 << 0), context->mmio_base + JPEG_CTRL);
  52. writel(val & ~(3 << 0), context->mmio_base + JPEG_CTRL);
  53. writel((1 << 8) | (1 << 3), context->mmio_base + JPEG_1);
  54. writel(0x28000738, context->mmio_base + JPEG_CTRL);
  55. writel(0xff, context->mmio_base + JPEG_COUNT);
  56. writel(0x2f, context->mmio_base + JPEG_INTMASK);
  57. writel(src_addr, context->mmio_base + JPEG_DEC_RD_BASE_ADDR);
  58. writel(dest_addr, context->mmio_base + JPEG_WRSTA);
  59. writel(dest_addr + width * height * 2, context->mmio_base + JPEG_WREND);
  60. writel(0, context->mmio_base + JPEG_WRSTA1);
  61. writel(0, context->mmio_base + JPEG_WREND1);
  62. writel(0, context->mmio_base + JPEG_WRSTA2);
  63. writel(0, context->mmio_base + JPEG_WREND2);
  64. writel(readl(context->mmio_base + JPEG_CTRL) | (1 << 13), context->mmio_base + JPEG_CTRL);
  65. writel(1, context->mmio_base + JPEG_0);
  66. writel(readl(context->mmio_base + JPEG_START) | (1 << 31), context->mmio_base + JPEG_START);
  67. }
  68. //extern int ark_carback_get_status(void);
  69. static void animation_timer_handler(struct timer_list *t)
  70. {
  71. static unsigned int frame = 0;
  72. static int timeout_count = 0;
  73. struct ark_jpeg_context *context = from_timer(context, t, animation_timer);
  74. BANIHEADER *header = (BANIHEADER *) context->animation_data_virtaddr;
  75. unsigned int size =
  76. *(unsigned int *)(context->animation_data_virtaddr + context->animation_file_phyaddr -
  77. context->animation_data_phyaddr);
  78. if (timeout_count > 50) {
  79. context->animation_end = true;
  80. }
  81. if (context->animation_end) {
  82. if (context->animation_display_phyaddr)
  83. dma_free_coherent(context->dev, context->animation_display_size,
  84. (void *)context->animation_display_virtaddr,
  85. context->animation_display_phyaddr);
  86. ark_bootanimation_display_uninit();
  87. return;
  88. }
  89. if (frame > 0) {
  90. if (!context->animation_dec_finish) {
  91. timeout_count++;
  92. mod_timer(&context->animation_timer, jiffies + msecs_to_jiffies(10));
  93. return;
  94. }
  95. timeout_count = 0;
  96. }
  97. if (frame == header->aniCount + header->hasBootlogo ? 1 : 0) {
  98. if (!context->animation_dec_finish) {
  99. timeout_count++;
  100. mod_timer(&context->animation_timer, jiffies + msecs_to_jiffies(10));
  101. return;
  102. }
  103. timeout_count = 0;
  104. context->animation_end = true;
  105. if (header->aniCount > 0) {
  106. mod_timer(&context->animation_timer, jiffies + msecs_to_jiffies(header->aniDelayHideTime));
  107. }
  108. return;
  109. }
  110. frame++;
  111. context->animation_dec_finish = false;
  112. animation_jpeg_decode(context, header->aniWidth, header->aniHeight, context->animation_file_phyaddr + 4,
  113. context->animation_display_phyaddr + header->aniWidth * header->aniHeight * 2 *
  114. context->animation_display_index);
  115. context->animation_file_phyaddr += size;
  116. if (header->hasBootlogo && frame == 1)
  117. mod_timer(&context->animation_timer, jiffies + msecs_to_jiffies(header->bootlogoDisplayTime));
  118. else
  119. mod_timer(&context->animation_timer, jiffies + msecs_to_jiffies(1000 / header->aniFps));
  120. }
  121. /* This is the first function being called when opening the device
  122. * driver. It identifies the driver-specific data structure of the device
  123. * being opened and stores the start pointer of the data structure in the
  124. * private_data field of the file structure, i.e. filp->private_data,
  125. * for easier access in the future.
  126. *
  127. * Arguments:
  128. * inode : kernel data structure that represents the device file of the
  129. * driver on disk
  130. * filp : a pointer to the file structure that is created by the kernel
  131. * to represent the file opened for the device driver
  132. *
  133. * Return:
  134. * 0
  135. */
  136. static int ark_jpeg_open(struct inode *inode, struct file *filp)
  137. {
  138. struct ark_jpeg_device *dev;
  139. struct ark_jpeg_context *context;
  140. dev = container_of(inode->i_cdev, struct ark_jpeg_device, cdev);
  141. context = &dev->context;
  142. filp->private_data = dev;
  143. return 0;
  144. }
  145. /* This function is invoked when the associated file structure is being
  146. * released.
  147. *
  148. * Arguments:
  149. * inode : kernel data structure that represents the device file of the
  150. * driver on disk
  151. * filp : a pointer to the file structure that is created by the kernel
  152. * to represent the file opened for the device driver
  153. *
  154. * Return:
  155. * 0
  156. */
  157. static int ark_jpeg_release(struct inode *inode, struct file *filp)
  158. {
  159. struct ark_jpeg_device *dev;
  160. dev = container_of(inode->i_cdev, struct ark_jpeg_device, cdev);
  161. /* NOTE: intend not to do anything here */
  162. return 0;
  163. }
  164. /* This function is invoked to handle ioctl system calls and implement
  165. * the requested ioctl command. It validates the ioctl command and performs
  166. * the requested device control function. If the command number does not
  167. * match a valid operation, it returns -ENOTTY. Otherwise, it returns 0
  168. *
  169. * Arguments:
  170. * inode : kernel data structure that represents the device file of the
  171. * driver on disk
  172. * filp : a pointer to the file structure that is created by the kernel
  173. * to represent the file opened for the device driver
  174. *
  175. * Return:
  176. * 0
  177. */
  178. static long ark_jpeg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  179. {
  180. struct ark_jpeg_device *jpeg =
  181. (struct ark_jpeg_device *)file->private_data;
  182. struct ark_jpeg_context *context = &jpeg->context;
  183. switch (cmd) {
  184. case ARKJPEG_SET_DECODE_OPT:
  185. {
  186. JPEG_DECODE_OPT input_arg;
  187. //printk("ARKJPEG_SET_DECODE_OPT [%s][%d]\n", __func__, __LINE__);
  188. if (arg == (unsigned long)NULL) {
  189. printk("%s %d: null arg error\n",
  190. __FUNCTION__, __LINE__);
  191. return -EINVAL;
  192. }
  193. if (copy_from_user(&input_arg, (void*)arg, sizeof(input_arg))) {
  194. printk("%s %d: copy_from_user error\n",
  195. __FUNCTION__, __LINE__);
  196. return -EFAULT;
  197. }
  198. context->scaler_mode = input_arg.ScalerMode;
  199. context->zoom_mode = input_arg.ZoomMode;
  200. context->rotate_angle = input_arg.RotateAngle;
  201. context->dst_width = input_arg.dwDestWidth;
  202. context->dst_height = input_arg.dwDestHeight;
  203. context->format = input_arg.format;
  204. context->repeat_src_height = input_arg.RepeatdwSrcHeight;
  205. context->repeat_src_width= input_arg.RepeatdwSrcWidth;
  206. }
  207. break;
  208. case ARKJPEG_SET_JPG_SIZE:
  209. {
  210. unsigned int input_arg;
  211. //printk("ARKJPEG_SET_JPG_SIZE [%s][%d]\n", __func__, __LINE__);
  212. if (arg == (unsigned long)NULL) {
  213. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  214. __FUNCTION__, __LINE__);
  215. return -EINVAL;
  216. }
  217. if (copy_from_user(&input_arg, (void*)arg, sizeof(input_arg))) {
  218. ARKJPEG_DBGPRTK("%s %d: copy_from_user error\n",
  219. __FUNCTION__, __LINE__);
  220. return -EFAULT;
  221. }
  222. context->file_size = input_arg;
  223. }
  224. break;
  225. case ARKJPEG_DECODE:
  226. //printk("ARKJPEG_DECODE [%s][%d]\n", __func__, __LINE__);
  227. complete(&context->decstart_completion);
  228. break;
  229. case ARKJPEG_BREAK_DECODE:
  230. //printk("ARKJPEG_BREAK_DECODE [%s][%d]\n", __func__, __LINE__);
  231. context->break_decode = true;
  232. break;
  233. case ARKJPEG_GET_DECSTATUS:
  234. {
  235. JPEG_DEC_STATUS output_arg;
  236. //printk("ARKJPEG_GET_DECSTATUS [%s][%d]\n", __func__, __LINE__);
  237. if (arg == (unsigned long)NULL) {
  238. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  239. __FUNCTION__, __LINE__);
  240. return -EINVAL;
  241. }
  242. output_arg = context->decode_status;
  243. if (copy_to_user((void*)arg, &output_arg, sizeof(output_arg))) {
  244. ARKJPEG_DBGPRTK("%s %d: copy_to_user error\n",
  245. __FUNCTION__, __LINE__);
  246. return -EFAULT;
  247. }
  248. }
  249. break;
  250. case ARKJPEG_SET_BUFFER:
  251. {
  252. struct jpeg_buffer buffer;
  253. if (arg == (unsigned long)NULL) {
  254. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  255. __FUNCTION__, __LINE__);
  256. return -EINVAL;
  257. }
  258. if (copy_from_user(&buffer, (void*)arg, sizeof(buffer))) {
  259. ARKJPEG_DBGPRTK("%s %d: copy_to_user error\n",
  260. __FUNCTION__, __LINE__);
  261. return -EFAULT;
  262. }
  263. context->buf_size = buffer.file_size;
  264. context->buf_base_virt = phys_to_virt(buffer.file_base_phys);
  265. context->buf_base_phys = buffer.file_base_phys;
  266. context->decode_buf_size = buffer.decode_size;
  267. context->decode_buf_base_phys = buffer.decode_base_phys;
  268. context->decode_buf_base_virt = phys_to_virt(buffer.decode_base_phys);
  269. }
  270. break;
  271. case ARKJPEG_GET_BUFFER:
  272. break;
  273. case ARKJPEG_GET_DECODEINFO:
  274. {
  275. JPEG_DECODE_INFO output_arg;
  276. //printk("ARKJPEG_GET_DECODEINFO [%s][%d]", __func__, __LINE__);
  277. if (arg == (unsigned long)NULL) {
  278. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  279. __FUNCTION__, __LINE__);
  280. return -EINVAL;
  281. }
  282. output_arg.DecResult = context->decode_result;
  283. output_arg.dwDecSize = context->decode_size;
  284. output_arg.dwSrcWidth = context->src_width;
  285. output_arg.dwSrcHeight = context->src_height;
  286. output_arg.dwOutWidth = context->out_width;
  287. output_arg.dwOutHeight = context->out_height;
  288. output_arg.RepeatdwSrcHeight = context->repeat_src_height;
  289. output_arg.RepeatdwSrcWidth = context->repeat_src_width;
  290. if (copy_to_user((void*)arg, &output_arg, sizeof(output_arg))) {
  291. ARKJPEG_DBGPRTK("%s %d: copy_to_user error\n",
  292. __FUNCTION__, __LINE__);
  293. return -EFAULT;
  294. }
  295. }
  296. break;
  297. case ARKJPEG_GET_APIEVENT:
  298. {
  299. int ret;
  300. //printk("ARKJPEG_GET_APIEVENT [%s][%d]\n", __func__, __LINE__);
  301. if (arg == (unsigned long)NULL) {
  302. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  303. __FUNCTION__, __LINE__);
  304. return -EINVAL;
  305. }
  306. ret = wait_for_completion_timeout(&context->api_completion, msecs_to_jiffies(JPEG_API_TIMEOUT));
  307. if(ret == 0)
  308. {
  309. ARKJPEG_DBGPRTK("%s %d: wait api_completion timeout\n",
  310. __FUNCTION__, __LINE__);
  311. return -EFAULT;
  312. }
  313. if (copy_to_user((void*)arg, &context->api_info, sizeof(context->api_info))) {
  314. ARKJPEG_DBGPRTK("%s %d: copy_to_user error\n",
  315. __FUNCTION__, __LINE__);
  316. return -EFAULT;
  317. }
  318. }
  319. break;
  320. case ARKJPEG_SET_APIDONEEVENT:
  321. {
  322. //printk("ARKJPEG_SET_APIDONEEVENT [%s][%d]\n", __func__, __LINE__);
  323. if (arg == (unsigned long)NULL) {
  324. ARKJPEG_DBGPRTK("%s %d: null arg error\n",
  325. __FUNCTION__, __LINE__);
  326. return -EINVAL;
  327. }
  328. if (copy_from_user(&context->api_retinfo, (void*)arg, sizeof(context->api_retinfo))) {
  329. ARKJPEG_DBGPRTK("%s %d: copy_from_user error\n",
  330. __FUNCTION__, __LINE__);
  331. return -EFAULT;
  332. }
  333. complete(&context->apidone_completion);
  334. }
  335. break;
  336. default:
  337. ARKJPEG_DBGPRTK("%s %d: undefined cmd (0x%2X)\n",
  338. __FUNCTION__, __LINE__, cmd);
  339. return -EINVAL;
  340. }
  341. return 0;
  342. }
  343. static struct file_operations ark_jpeg_fops = {
  344. .owner = THIS_MODULE,
  345. .open = ark_jpeg_open,
  346. .unlocked_ioctl = ark_jpeg_ioctl,
  347. .release = ark_jpeg_release,
  348. };
  349. static int jpeg_decode_thread(void *data)
  350. {
  351. struct ark_jpeg_context *context = (struct ark_jpeg_context *)data;
  352. while (1) {
  353. wait_for_completion(&context->decstart_completion);
  354. ARKJPEG_DBGPRTK("jpeg_decode_thread\n");
  355. JpegPicDec();
  356. context->api_info.EventType = DEC_OVER;
  357. context->api_info.DecResult = context->decode_result;
  358. complete(&context->api_completion);
  359. }
  360. return 0;
  361. }
  362. static void jpeg_int_work(struct work_struct *work)
  363. {
  364. struct ark_jpeg_context *context = container_of(work, struct ark_jpeg_context, jpeg_work);
  365. BANIHEADER *header = (BANIHEADER *) context->animation_data_virtaddr;
  366. //printk(KERN_ALERT "jpeg_int_work 0x%x\n", context->intr_status);
  367. if (!context->animation_end) {
  368. if (context->intr_status & 0x4) {
  369. if (!context->animation_initdisplay) {
  370. ark_bootanimation_display_init(header->aniWidth, header->aniHeight,
  371. context->animation_display_phyaddr);
  372. context->animation_initdisplay = true;
  373. } else {
  374. ark_bootanimation_set_display_addr(context->animation_display_phyaddr +
  375. header->aniWidth * header->aniHeight * 2 * context->animation_display_index);
  376. context->animation_display_index = !context->animation_display_index;
  377. }
  378. context->animation_dec_finish = true;
  379. } else if (context->intr_status & 0x1) {
  380. int dec_width = (readl(context->mmio_base + JPEG_1) >> 16) & 0x0000ffff;
  381. int dec_height = (readl(context->mmio_base + JPEG_3) >> 16) & 0x0000ffff;
  382. if (dec_width == header->aniWidth && dec_height == header->aniHeight) {
  383. JpegContinueWork();
  384. } else {
  385. printk(KERN_ALERT "decode boot animation jpeg error.\n");
  386. context->animation_dec_finish = true;
  387. }
  388. }
  389. return;
  390. }
  391. if (context->intr_status & 0x4) {
  392. if (JpegFrameIntHandler())
  393. context->decode_result = DEC_SUCCESS;
  394. complete(&context->decdone_completion);
  395. } else if (context->intr_status & 0x20) {
  396. if (!JpegBufferIntHandler())
  397. complete(&context->decdone_completion);
  398. else
  399. JpegContinueWork();
  400. } else if (context->intr_status & 0x1) {
  401. if (!JpegHeaderIntHandler())
  402. complete(&context->decdone_completion);
  403. else
  404. JpegContinueWork();
  405. } else if (context->intr_status & 0x2) {
  406. if (!JpegBlockIntHandler())
  407. complete(&context->decdone_completion);
  408. else
  409. JpegContinueWork();
  410. } else {
  411. printk(KERN_ALERT "JPG:: Jpeg error int!\n");
  412. }
  413. }
  414. /* This function is invoked when the device module is loaded into the
  415. * kernel. It allocates system resources for constructing driver control
  416. * data structures and initializes them accordingly.
  417. */
  418. static int ark_jpeg_probe(struct platform_device *pdev)
  419. {
  420. struct ark_jpeg_device *jpeg;
  421. struct resource *res;
  422. void __iomem *regs;
  423. dev_t dev;
  424. int error = 0;
  425. jpeg = devm_kzalloc(&pdev->dev, sizeof(struct ark_jpeg_device), GFP_KERNEL);
  426. if (jpeg == NULL) {
  427. dev_err(&pdev->dev, "%s %d: failed to allocate memory\n", __FUNCTION__, __LINE__);
  428. return -ENOMEM;
  429. }
  430. jpeg->driver_name = "ark_jpeg_drv";
  431. jpeg->name = "ark_jpeg";
  432. jpeg->major = 0; /* if 0, let system choose */
  433. jpeg->minor_start = 0;
  434. jpeg->minor_num = 1; /* one dev only */
  435. jpeg->num = 1;
  436. /* register char device */
  437. if (!jpeg->major) {
  438. error = alloc_chrdev_region(&dev, jpeg->minor_start, jpeg->num, jpeg->name);
  439. if (!error) {
  440. jpeg->major = MAJOR(dev);
  441. jpeg->minor_start = MINOR(dev);
  442. }
  443. } else {
  444. dev = MKDEV(jpeg->major, jpeg->minor_start);
  445. error = register_chrdev_region(dev, jpeg->num, (char *)jpeg->name);
  446. }
  447. if (error < 0) {
  448. dev_err(&pdev->dev, "%s %d: register driver error\n", __FUNCTION__, __LINE__);
  449. goto err_driver_register;
  450. }
  451. /* associate the file operations */
  452. cdev_init(&jpeg->cdev, &ark_jpeg_fops);
  453. jpeg->cdev.owner = THIS_MODULE; //driver->owner;
  454. jpeg->cdev.ops = &ark_jpeg_fops;
  455. error = cdev_add(&jpeg->cdev, dev, jpeg->num);
  456. if (error) {
  457. dev_err(&pdev->dev, "%s %d: cdev add error\n", __FUNCTION__, __LINE__);
  458. goto err_cdev_add;
  459. }
  460. jpeg->jpeg_class = class_create(THIS_MODULE, "ark_jpeg_class");
  461. if (IS_ERR(jpeg->jpeg_class)) {
  462. dev_err(&pdev->dev, "Err: failed in creating ark jpeg class.\n");
  463. jpeg->jpeg_class = NULL;
  464. goto err_cdev_add;
  465. }
  466. jpeg->jpeg_device = device_create(jpeg->jpeg_class, NULL, dev, NULL, "ark_jpeg");
  467. if (IS_ERR(jpeg->jpeg_device)) {
  468. dev_err(&pdev->dev, "Err: failed in creating ark jpeg device.\n");
  469. jpeg->jpeg_device = NULL;
  470. goto err_cdev_add;
  471. }
  472. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  473. if (IS_ERR(res)) {
  474. error = PTR_ERR(res);
  475. goto err_mem_res_req;
  476. }
  477. regs = devm_ioremap_resource(&pdev->dev, res);
  478. if (IS_ERR(regs)) {
  479. error = PTR_ERR(regs);
  480. goto err_mem_res_req;
  481. }
  482. jpeg->context.mmio_base = regs;
  483. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  484. if (IS_ERR(res)) {
  485. error = PTR_ERR(res);
  486. goto err_mem_res_req;
  487. }
  488. jpeg->context.animation_data_phyaddr = res->start;
  489. jpeg->context.animation_data_virtaddr =
  490. (unsigned int)ioremap(jpeg->context.animation_data_phyaddr, resource_size(res));
  491. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  492. if (!IS_ERR(res)) {
  493. regs = devm_ioremap_resource(&pdev->dev, res);
  494. if (IS_ERR(regs)) {
  495. error = PTR_ERR(regs);
  496. goto err_mem_res_req;
  497. }
  498. jpeg->context.ps_mmio_base = regs;
  499. }
  500. /* initialize hardware and data structure */
  501. error = ark_jpeg_dev_init(&jpeg->context);
  502. if (error != 0) {
  503. printk(KERN_ERR "%s %d: dev init err\n", __FUNCTION__, __LINE__);
  504. goto err_init;
  505. }
  506. jpeg->context.irq = platform_get_irq(pdev, 0);
  507. if (jpeg->context.irq < 0) {
  508. dev_err(&pdev->dev, "%s %d: can't get irq resource.\n", __FUNCTION__, __LINE__);
  509. goto err_irq;
  510. }
  511. error = devm_request_irq(&pdev->dev, jpeg->context.irq, ark_jpeg_intr_handler, IRQF_SHARED, //SA_SHIRQ,
  512. "jpeg", jpeg);
  513. if (error) {
  514. dev_err(&pdev->dev, "%s %d: can't get assigned irq %d, error %d\n", __FUNCTION__, __LINE__,
  515. jpeg->context.irq, error);
  516. goto err_irq;
  517. }
  518. jpeg->context.psirq = platform_get_irq(pdev, 1);
  519. if (jpeg->context.psirq > 0) {
  520. error = devm_request_irq(&pdev->dev, jpeg->context.psirq, ark_prescale_intr_handler, IRQF_SHARED, //SA_SHIRQ,
  521. "prescale", jpeg);
  522. if (error) {
  523. dev_err(&pdev->dev, "%s %d: can't get assigned prescale irq %d, error %d\n", __FUNCTION__, __LINE__,
  524. jpeg->context.psirq, error);
  525. goto err_irq;
  526. }
  527. }
  528. if (jpeg->context.ps_mmio_base && jpeg->context.psirq > 0) {
  529. writel(3, jpeg->context.ps_mmio_base + PRESCALE_INT_CLR);
  530. writel(3, jpeg->context.ps_mmio_base + PRESCALE_INT_MASK);
  531. init_completion(&jpeg->context.psblockint_completion);
  532. init_completion(&jpeg->context.psframeint_completion);
  533. }
  534. jpeg->context.dev = &pdev->dev;
  535. init_completion(&jpeg->context.decstart_completion);
  536. init_completion(&jpeg->context.api_completion);
  537. init_completion(&jpeg->context.apidone_completion);
  538. init_completion(&jpeg->context.decdone_completion);
  539. jpeg->context.wrok_queue = create_singlethread_workqueue("jpeg_queue");
  540. if (!jpeg->context.wrok_queue) {
  541. printk(KERN_ERR "%s %d: , create_singlethread_workqueue fail.\n", __FUNCTION__, __LINE__);
  542. }
  543. INIT_WORK(&jpeg->context.jpeg_work, jpeg_int_work);
  544. kthread_run(jpeg_decode_thread, &jpeg->context, "jpeg_decode");
  545. if (jpeg->context.animation_data_virtaddr) {
  546. BANIHEADER *header = (BANIHEADER *) jpeg->context.animation_data_virtaddr;
  547. if (header->magic == MKTAG('B', 'A', 'N', 'I')) {
  548. jpeg->context.animation_file_phyaddr =
  549. jpeg->context.animation_data_phyaddr + sizeof(BANIHEADER);
  550. jpeg->context.animation_display_size = header->aniWidth * header->aniHeight * 2 * 2;
  551. jpeg->context.animation_display_virtaddr =
  552. (unsigned int)dma_alloc_coherent(&pdev->dev, jpeg->context.animation_display_size,
  553. &jpeg->context.animation_display_phyaddr, GFP_KERNEL);
  554. if (!jpeg->context.animation_display_virtaddr) {
  555. dev_err(&pdev->dev, "alloc animation display buffer failed.\n");
  556. jpeg->context.animation_end = true;
  557. } else {
  558. jpeg->context.animation_end = false;
  559. jpeg->context.animation_dec_finish = false;
  560. jpeg->context.animation_initdisplay = false;
  561. timer_setup(&jpeg->context.animation_timer, animation_timer_handler, 0);
  562. jpeg->context.animation_timer.expires = jiffies + 1;
  563. add_timer(&jpeg->context.animation_timer);
  564. }
  565. } else {
  566. jpeg->context.animation_end = true;
  567. }
  568. } else
  569. jpeg->context.animation_end = true;
  570. platform_set_drvdata(pdev, jpeg);
  571. return 0;
  572. err_irq:
  573. err_init:
  574. err_mem_res_req:
  575. cdev_del(&jpeg->cdev);
  576. err_cdev_add:
  577. if (jpeg->context.animation_data_virtaddr)
  578. iounmap((void *)jpeg->context.animation_data_virtaddr);
  579. if (jpeg->jpeg_class) {
  580. if (jpeg->jpeg_device) {
  581. device_destroy(jpeg->jpeg_class, dev);
  582. }
  583. class_destroy(jpeg->jpeg_class);
  584. }
  585. unregister_chrdev_region(dev, jpeg->num);
  586. err_driver_register:
  587. return error;
  588. }
  589. /* This function is invoked when the device module is removed from the
  590. * kernel. It releases all resources to the system.
  591. */
  592. static int ark_jpeg_remove(struct platform_device *pdev)
  593. {
  594. struct ark_jpeg_device *jpeg;
  595. dev_t dev;
  596. jpeg = platform_get_drvdata(pdev);
  597. if (jpeg == NULL)
  598. return -ENODEV;
  599. dev = MKDEV(jpeg->major, jpeg->minor_start);
  600. if (jpeg->context.wrok_queue)
  601. destroy_workqueue(jpeg->context.wrok_queue);
  602. cdev_del(&jpeg->cdev);
  603. if (jpeg->jpeg_class) {
  604. if (jpeg->jpeg_device) {
  605. device_destroy(jpeg->jpeg_class, dev);
  606. }
  607. class_destroy(jpeg->jpeg_class);
  608. }
  609. unregister_chrdev_region(dev, jpeg->num);
  610. return 0;
  611. }
  612. static const struct of_device_id jpeg_of_match[] = {
  613. {.compatible = "arkmicro,ark-jpeg",},
  614. {}
  615. };
  616. MODULE_DEVICE_TABLE(of, jpeg_of_match);
  617. static struct platform_driver ark_jpeg_driver = {
  618. .driver = {
  619. .name = "ark-jpeg",
  620. .of_match_table = of_match_ptr(jpeg_of_match),
  621. },
  622. .probe = ark_jpeg_probe,
  623. .remove = ark_jpeg_remove,
  624. };
  625. //module_platform_driver(ark_jpeg_driver);
  626. static int __init ark1668_jpeg_init(void)
  627. {
  628. int ret;
  629. ret = platform_driver_register(&ark_jpeg_driver);
  630. if (ret != 0) {
  631. printk(KERN_ERR "%s %d: failed to register ark1668_lcdfb_driver\n",
  632. __FUNCTION__, __LINE__);
  633. }
  634. return ret;
  635. }
  636. device_initcall(ark1668_jpeg_init);
  637. MODULE_AUTHOR("Sim");
  638. MODULE_DESCRIPTION("ArkMicro 1668 Jpeg Driver");
  639. MODULE_LICENSE("GPL v2");