jpeg_drv.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. #define MT6582_FPGA
  2. /* #define FPGA_VERSION */
  3. #include <linux/kernel.h>
  4. #include <linux/mm.h>
  5. #include <linux/mm_types.h>
  6. #include <linux/module.h>
  7. #include <generated/autoconf.h>
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/cdev.h>
  11. #include <linux/kdev_t.h>
  12. #include <linux/delay.h>
  13. #include <linux/ioport.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/device.h>
  17. #include <linux/fs.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/wait.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/param.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/xlog.h>
  26. #include <asm/io.h>
  27. #include <mach/irqs.h>
  28. #include <mach/mt_reg_base.h>
  29. /* #include <mach/mt6575_pll.h> */
  30. #include <mach/mt_irq.h>
  31. #include <mach/irqs.h>
  32. #include <mach/mt_clkmgr.h>
  33. #include <mach/sync_write.h>
  34. /* ============================================================ */
  35. /* #include <linux/uaccess.h> */
  36. /* #include <linux/module.h> */
  37. /* #include <linux/fs.h> */
  38. /* #include <linux/platform_device.h> */
  39. /* #include <linux/cdev.h> */
  40. #include <linux/interrupt.h>
  41. /* #include <asm/io.h> */
  42. #include <linux/sched.h>
  43. #include <linux/wait.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/delay.h>
  46. #include <linux/earlysuspend.h>
  47. /* #include <linux/mm.h> */
  48. #include <linux/vmalloc.h>
  49. #include <linux/dma-mapping.h>
  50. /* #include <linux/slab.h> */
  51. /* #include <linux/gfp.h> */
  52. #include <linux/aee.h>
  53. #include <linux/timer.h>
  54. #include <linux/disp_assert_layer.h>
  55. /* #include <linux/xlog.h> */
  56. /* #include <linux/fs.h> */
  57. /* Arch dependent files */
  58. #include <asm/mach/map.h>
  59. #include <mach/sync_write.h>
  60. /* #include <mach/mt6577_pll.h> */
  61. #include <mach/mt_irq.h>
  62. #include <mach/mt_clkmgr.h>
  63. #include <mach/irqs.h>
  64. /* #include <asm/tcm.h> */
  65. #include <asm/cacheflush.h>
  66. #include <asm/system.h>
  67. /* #include <linux/mm.h> */
  68. #include <linux/pagemap.h>
  69. #ifndef FPGA_VERSION
  70. #include <mach/mt_boot.h>
  71. #endif
  72. /* ========================================================== */
  73. #include "jpeg_drv.h"
  74. #include "jpeg_drv_6589_common.h"
  75. /* #include <asm/tcm.h> */
  76. /* #define USE_SYSRAM */
  77. #define JPEG_MSG pr_debug
  78. #define JPEG_WRN pr_debug
  79. #define JPEG_ERR pr_debug
  80. #define JPEG_DEVNAME "mtk_jpeg"
  81. #define TABLE_SIZE 4096
  82. #define JPEG_DEC_PROCESS 0x1
  83. #define JPEG_ENC_PROCESS 0x2
  84. /* #define FPGA_VERSION */
  85. #include "jpeg_drv_6589_reg.h"
  86. /* -------------------------------------------------------------------------- */
  87. /* */
  88. /* -------------------------------------------------------------------------- */
  89. /* device and driver */
  90. static dev_t jpeg_devno;
  91. static struct cdev *jpeg_cdev;
  92. static struct class *jpeg_class;
  93. /* decoder */
  94. static wait_queue_head_t dec_wait_queue;
  95. static spinlock_t jpeg_dec_lock;
  96. static int dec_status;
  97. /* static unsigned int table_buffer_pa; */
  98. /* static unsigned char *table_buffer_va; */
  99. /* static unsigned char *dec_src_va; */
  100. /* static unsigned int dec_src_pa; */
  101. /* static unsigned int dec_src_size; */
  102. /* encoder */
  103. static wait_queue_head_t enc_wait_queue;
  104. static spinlock_t jpeg_enc_lock;
  105. static int enc_status;
  106. /* static unsigned char *dstBufferVA; */
  107. /* static unsigned char *dstUserVA; */
  108. /* static unsigned int dstBufferPA; */
  109. /* static unsigned int dstBufferSize; */
  110. /* -------------------------------------------------------------------------- */
  111. /* JPEG Common Function */
  112. /* -------------------------------------------------------------------------- */
  113. #ifdef FPGA_VERSION
  114. void jpeg_drv_dec_power_on(void)
  115. {
  116. JPEG_MSG("JPEG Decoder Power On\n");
  117. }
  118. void jpeg_drv_dec_power_off(void)
  119. {
  120. JPEG_MSG("JPEG Decoder Power Off\n");
  121. }
  122. void jpeg_drv_enc_power_on(void)
  123. {
  124. #ifdef FPGA_VERSION
  125. IMG_REG_WRITE((0), JPEG_EARLY_MM_BASE);
  126. JPEG_MSG("JPEG Encoder RESET_MM_BASE!!\n");
  127. #endif
  128. JPEG_MSG("JPEG Encoder Power On\n");
  129. }
  130. void jpeg_drv_enc_power_off(void)
  131. {
  132. JPEG_MSG("JPEG Encoder Power Off\n");
  133. }
  134. #else
  135. static irqreturn_t jpeg_drv_enc_isr(int irq, void *dev_id)
  136. {
  137. /* JPEG_MSG("JPEG Encoder Interrupt\n"); */
  138. if (irq == MT6582_JPEG_ENC_IRQ_ID) {
  139. /* mt65xx_irq_mask(MT6575_JPEG_CODEC_IRQ_ID); */
  140. #if 0
  141. if (jpeg_isr_dec_lisr() == 0)
  142. wake_up_interruptible(&dec_wait_queue);
  143. #endif
  144. if (jpeg_isr_enc_lisr() == 0)
  145. wake_up_interruptible(&enc_wait_queue);
  146. /* mt65xx_irq_unmask(MT6575_JPEG_CODEC_IRQ_ID); */
  147. }
  148. return IRQ_HANDLED;
  149. }
  150. #ifdef JPEG_DEC_DRIVER
  151. static irqreturn_t jpeg_drv_dec_isr(int irq, void *dev_id)
  152. {
  153. /* JPEG_MSG("JPEG Decoder Interrupt\n"); */
  154. /* jpeg_reg_dump(); */
  155. if (irq == MT6589_JPEG_DEC_IRQ_ID) {
  156. /* mt65xx_irq_mask(MT6575_JPEG_CODEC_IRQ_ID); */
  157. if (jpeg_isr_dec_lisr() == 0)
  158. wake_up_interruptible(&dec_wait_queue);
  159. #if 0
  160. if (jpeg_isr_enc_lisr() == 0)
  161. wake_up_interruptible(&enc_wait_queue);
  162. #endif
  163. /* mt65xx_irq_unmask(MT6575_JPEG_CODEC_IRQ_ID); */
  164. }
  165. return IRQ_HANDLED;
  166. }
  167. #endif
  168. void jpeg_drv_dec_power_on(void)
  169. {
  170. /* REG_JPEG_MM_REG_MASK = 0; */
  171. #ifndef FPGA_VERSION
  172. /* enable_clock(MT_CG_IMAGE_JPGD_SMI,"JPEG"); */
  173. /* enable_clock(MT_CG_IMAGE_JPGD_JPG,"JPEG"); */
  174. #endif
  175. #ifdef FOR_COMPILE
  176. BOOL ret;
  177. ret = enable_clock(MT65XX_PDN_MM_JPEG_DEC, "JPEG");
  178. NOT_REFERENCED(ret);
  179. #endif
  180. }
  181. void jpeg_drv_dec_power_off(void)
  182. {
  183. #ifndef FPGA_VERSION
  184. /* disable_clock(MT_CG_IMAGE_JPGD_SMI,"JPEG"); */
  185. /* disable_clock(MT_CG_IMAGE_JPGD_JPG,"JPEG"); */
  186. #endif
  187. #ifdef FOR_COMPILE
  188. BOOL ret;
  189. ret = disable_clock(MT65XX_PDN_MM_JPEG_DEC, "JPEG");
  190. NOT_REFERENCED(ret);
  191. #endif
  192. }
  193. void jpeg_drv_enc_power_on(void)
  194. {
  195. #ifndef FPGA_VERSION
  196. /* REG_JPEG_MM_REG_MASK = 0; */
  197. enable_clock(MT_CG_IMAGE_LARB2_SMI, "JPEG");
  198. enable_clock(MT_CG_IMAGE_VENC_JPENC, "JPEG");
  199. enable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  200. #endif
  201. #ifdef FOR_COMPILE
  202. BOOL ret;
  203. ret = enable_clock(MT65XX_PDN_MM_JPEG_ENC, "JPEG");
  204. NOT_REFERENCED(ret);
  205. #endif
  206. }
  207. void jpeg_drv_enc_power_off(void)
  208. {
  209. #ifndef FPGA_VERSION
  210. disable_clock(MT_CG_IMAGE_LARB2_SMI, "JPEG");
  211. disable_clock(MT_CG_IMAGE_VENC_JPENC, "JPEG");
  212. disable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  213. #endif
  214. #ifdef FOR_COMPILE
  215. BOOL ret;
  216. ret = disable_clock(MT65XX_PDN_MM_JPEG_ENC, "JPEG");
  217. NOT_REFERENCED(ret);
  218. #endif
  219. }
  220. #endif
  221. static int jpeg_drv_dec_init(void)
  222. {
  223. int retValue;
  224. spin_lock(&jpeg_dec_lock);
  225. if (dec_status != 0) {
  226. JPEG_WRN("JPEG Decoder is busy\n");
  227. retValue = -EBUSY;
  228. } else {
  229. dec_status = 1;
  230. retValue = 0;
  231. }
  232. spin_unlock(&jpeg_dec_lock);
  233. if (retValue == 0) {
  234. jpeg_drv_dec_power_on();
  235. jpeg_drv_dec_reset();
  236. }
  237. return retValue;
  238. }
  239. static void jpeg_drv_dec_deinit(void)
  240. {
  241. if (dec_status != 0) {
  242. spin_lock(&jpeg_dec_lock);
  243. dec_status = 0;
  244. spin_unlock(&jpeg_dec_lock);
  245. jpeg_drv_dec_reset();
  246. jpeg_drv_dec_power_off();
  247. }
  248. }
  249. static int jpeg_drv_enc_init(void)
  250. {
  251. int retValue;
  252. spin_lock(&jpeg_enc_lock);
  253. if (enc_status != 0) {
  254. JPEG_WRN("JPEG Encoder is busy\n");
  255. retValue = -EBUSY;
  256. } else {
  257. enc_status = 1;
  258. retValue = 0;
  259. }
  260. spin_unlock(&jpeg_enc_lock);
  261. if (retValue == 0) {
  262. jpeg_drv_enc_power_on();
  263. jpeg_drv_enc_reset();
  264. }
  265. return retValue;
  266. }
  267. static void jpeg_drv_enc_deinit(void)
  268. {
  269. if (enc_status != 0) {
  270. spin_lock(&jpeg_enc_lock);
  271. enc_status = 0;
  272. spin_unlock(&jpeg_enc_lock);
  273. jpeg_drv_enc_reset();
  274. jpeg_drv_enc_power_off();
  275. }
  276. }
  277. /* -------------------------------------------------------------------------- */
  278. /* JPEG REG DUMP FUNCTION */
  279. /* -------------------------------------------------------------------------- */
  280. void jpeg_reg_dump(void)
  281. {
  282. unsigned int reg_value = 0;
  283. unsigned int index = 0;
  284. JPEG_WRN("JPEG REG:\n ********************\n");
  285. for (index = 0; index < 0x168; index += 4) {
  286. /* reg_value = ioread32(JPEG_DEC_BASE + index); */
  287. IMG_REG_READ(reg_value, JPEG_DEC_BASE + index);
  288. JPEG_WRN("+0x%x 0x%x\n", index, reg_value);
  289. }
  290. }
  291. /* -------------------------------------------------------------------------- */
  292. /* JPEG DECODER IOCTRL FUNCTION */
  293. /* -------------------------------------------------------------------------- */
  294. static int jpeg_dec_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
  295. {
  296. unsigned int *pStatus;
  297. unsigned int decResult;
  298. long timeout_jiff;
  299. JPEG_DEC_DRV_IN dec_params;
  300. JPEG_DEC_CONFIG_ROW dec_row_params;
  301. unsigned int irq_st = 0;
  302. /* unsigned int timeout = 0x1FFFFF; */
  303. JPEG_DEC_DRV_OUT outParams;
  304. pStatus = (unsigned int *)file->private_data;
  305. if (NULL == pStatus) {
  306. JPEG_MSG
  307. ("[JPEGDRV]JPEG Decoder: Private data is null in flush operation. SOME THING WRONG??\n");
  308. return -EFAULT;
  309. }
  310. switch (cmd) {
  311. /* initial and reset JPEG encoder */
  312. case JPEG_DEC_IOCTL_INIT: /* OT:OK */
  313. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Init!!\n");
  314. if (jpeg_drv_dec_init() == 0)
  315. *pStatus = JPEG_DEC_PROCESS;
  316. break;
  317. case JPEG_DEC_IOCTL_RESET: /* OT:OK */
  318. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Reset!!\n");
  319. jpeg_drv_dec_reset();
  320. break;
  321. case JPEG_DEC_IOCTL_CONFIG:
  322. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Configration!!\n");
  323. if (*pStatus != JPEG_DEC_PROCESS) {
  324. JPEG_MSG
  325. ("[JPEGDRV]Permission Denied! This process can not access decoder\n");
  326. return -EFAULT;
  327. }
  328. if (dec_status == 0) {
  329. JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
  330. *pStatus = 0;
  331. return -EFAULT;
  332. }
  333. if (copy_from_user(&dec_params, (void *)arg, sizeof(JPEG_DEC_DRV_IN))) {
  334. JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
  335. return -EFAULT;
  336. }
  337. /* _jpeg_dec_dump_reg_en = dec_params.regDecDumpEn; */
  338. if (dec_params.decodeMode == JPEG_DEC_MODE_MCU_ROW)
  339. _jpeg_dec_mode = 1;
  340. else
  341. _jpeg_dec_mode = 0;
  342. if (jpeg_drv_dec_set_config_data(&dec_params) < 0)
  343. return -EFAULT;
  344. break;
  345. case JPEG_DEC_IOCTL_RESUME:
  346. if (*pStatus != JPEG_DEC_PROCESS) {
  347. JPEG_MSG
  348. ("[JPEGDRV]Permission Denied! This process can not access decoder\n");
  349. return -EFAULT;
  350. }
  351. if (dec_status == 0) {
  352. JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
  353. *pStatus = 0;
  354. return -EFAULT;
  355. }
  356. if (copy_from_user(&dec_row_params, (void *)arg, sizeof(JPEG_DEC_CONFIG_ROW))) {
  357. JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
  358. return -EFAULT;
  359. }
  360. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Resume, [%d] %x %x %x !!\n",
  361. dec_row_params.pauseMCU - 1, dec_row_params.decRowBuf[0],
  362. dec_row_params.decRowBuf[1], dec_row_params.decRowBuf[2]);
  363. jpeg_drv_dec_set_dst_bank0(dec_row_params.decRowBuf[0], dec_row_params.decRowBuf[1],
  364. dec_row_params.decRowBuf[2]);
  365. jpeg_drv_dec_set_pause_mcu_idx(dec_row_params.pauseMCU - 1);
  366. jpeg_drv_dec_resume(BIT_INQST_MASK_PAUSE);
  367. break;
  368. case JPEG_DEC_IOCTL_START: /* OT:OK */
  369. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Start!!\n");
  370. jpeg_drv_dec_start();
  371. break;
  372. case JPEG_DEC_IOCTL_WAIT:
  373. if (*pStatus != JPEG_DEC_PROCESS) {
  374. JPEG_WRN("Permission Denied! This process can not access decoder");
  375. return -EFAULT;
  376. }
  377. if (dec_status == 0) {
  378. JPEG_WRN("Decoder status is available, HOW COULD THIS HAPPEN ??");
  379. *pStatus = 0;
  380. return -EFAULT;
  381. }
  382. if (copy_from_user(&outParams, (void *)arg, sizeof(JPEG_DEC_DRV_OUT))) {
  383. JPEG_WRN("JPEG Decoder : Copy from user error\n");
  384. return -EFAULT;
  385. }
  386. /* set timeout */
  387. timeout_jiff = outParams.timeout * HZ / 1000;
  388. /* JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Wait Resume Time Jiffies : %ld\n", timeout_jiff); */
  389. #ifdef FPGA_VERSION
  390. /* #if 1 */
  391. JPEG_MSG("[JPEGDRV]Polling JPEG Status");
  392. do {
  393. _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS;
  394. } while (_jpeg_dec_int_status == 0);
  395. #else
  396. /* if(outParams.timeout >= 5000){ */
  397. /* */
  398. /* JPEG_MSG("Polling JPEG Status"); */
  399. /* do */
  400. /* { */
  401. /* _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS; */
  402. /* timeout--; */
  403. /* } while(_jpeg_dec_int_status == 0 && timeout != 0); */
  404. /* if(timeout == 0) JPEG_MSG("Polling JPEG Status TIMEOUT!!\n"); */
  405. /* }else */
  406. if (jpeg_isr_dec_lisr() < 0) {
  407. /* JPEG_MSG("wait JPEG irq\n"); */
  408. wait_event_interruptible_timeout(dec_wait_queue, _jpeg_dec_int_status,
  409. timeout_jiff);
  410. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Done!!\n");
  411. } else {
  412. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Already Done!!\n");
  413. }
  414. #endif
  415. decResult = jpeg_drv_dec_get_result();
  416. /* jpeg_drv_dec_dump_key_reg(); */
  417. if (decResult >= 2) {
  418. JPEG_MSG("[JPEGDRV]Decode Result : %d, status %x!\n", decResult,
  419. _jpeg_dec_int_status);
  420. jpeg_drv_dec_dump_key_reg();
  421. /* jpeg_drv_dec_dump_reg(); */
  422. jpeg_drv_dec_reset();
  423. }
  424. irq_st = _jpeg_dec_int_status;
  425. decResult = decResult | (irq_st << 8);
  426. _jpeg_dec_int_status = 0;
  427. if (copy_to_user(outParams.result, &decResult, sizeof(unsigned int))) {
  428. JPEG_WRN("JPEG Decoder : Copy to user error (result)\n");
  429. return -EFAULT;
  430. }
  431. break;
  432. case JPEG_DEC_IOCTL_BREAK:
  433. if (jpeg_drv_dec_break() < 0)
  434. return -EFAULT;
  435. break;
  436. case JPEG_DEC_IOCTL_DUMP_REG:
  437. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder DUMP REGISTER !!\n");
  438. jpeg_drv_dec_dump_reg();
  439. break;
  440. case JPEG_DEC_IOCTL_DEINIT:
  441. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Deinit !!\n");
  442. /* copy input parameters */
  443. if (*pStatus != JPEG_DEC_PROCESS) {
  444. JPEG_ERR("Permission Denied! This process can not access encoder");
  445. return -EFAULT;
  446. }
  447. if (dec_status == 0) {
  448. JPEG_ERR("Encoder status is available, HOW COULD THIS HAPPEN ??");
  449. *pStatus = 0;
  450. return -EFAULT;
  451. }
  452. jpeg_drv_dec_deinit();
  453. *pStatus = 0;
  454. break;
  455. #ifdef FOR_COMPILE
  456. case JPEG_DEC_IOCTL_RW_REG: /* OT:OK */
  457. jpeg_drv_dec_rw_reg();
  458. break;
  459. #endif
  460. default:
  461. JPEG_ERR("JPEG DEC IOCTL NO THIS COMMAND\n");
  462. break;
  463. }
  464. return 0;
  465. }
  466. static int jpeg_enc_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
  467. {
  468. int retValue;
  469. /* unsigned int decResult; */
  470. long timeout_jiff;
  471. unsigned int file_size, enc_result_code;
  472. /* unsigned int _jpeg_enc_int_status; */
  473. unsigned int jpeg_enc_wait_timeout = 0;
  474. unsigned int cycle_count;
  475. unsigned int ret;
  476. unsigned int *pStatus;
  477. /* JpegDrvEncParam cfgEnc; */
  478. JPEG_ENC_DRV_IN cfgEnc;
  479. JpegDrvEncResult enc_result;
  480. /* JpegDrvEncSrcCfg src_cfg; */
  481. /* JpegDrvEncDstCfg dst_cfg; */
  482. /* JpegDrvEncCtrlCfg ctrl_cfg; */
  483. pStatus = (unsigned int *)file->private_data;
  484. if (NULL == pStatus) {
  485. JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
  486. return -EFAULT;
  487. }
  488. switch (cmd) {
  489. case JPEG_ENC_IOCTL_RW_REG:
  490. /* jpeg_drv_enc_rw_reg(); */
  491. break;
  492. /* initial and reset JPEG encoder */
  493. case JPEG_ENC_IOCTL_INIT:
  494. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Init!!\n");
  495. retValue = jpeg_drv_enc_init();
  496. if (retValue == 0)
  497. *pStatus = JPEG_ENC_PROCESS;
  498. return retValue;
  499. break;
  500. case JPEG_ENC_IOCTL_WARM_RESET:
  501. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Warm Reset\n");
  502. enc_result_code = jpeg_drv_enc_warm_reset();
  503. if (0 == enc_result_code)
  504. return -EFAULT;
  505. break;
  506. /* configure the register */
  507. case JPEG_ENC_IOCTL_CONFIG:
  508. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Configure Hardware\n");
  509. if (*pStatus != JPEG_ENC_PROCESS) {
  510. JPEG_WRN("Permission Denied! This process can not access encoder");
  511. return -EFAULT;
  512. }
  513. if (enc_status == 0) {
  514. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  515. *pStatus = 0;
  516. return -EFAULT;
  517. }
  518. /* copy input parameters */
  519. if (copy_from_user(&cfgEnc, (void *)arg, sizeof(JPEG_ENC_DRV_IN))) {
  520. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy from user error\n");
  521. return -EFAULT;
  522. }
  523. /* 0. reset */
  524. jpeg_drv_enc_reset();
  525. /* 1. set src config */
  526. /* memset(&src_cfg, 0, sizeof(JpegDrvEncSrcCfg)); */
  527. /* src_cfg.luma_addr = cfgEnc.srcBufferAddr; */
  528. /* if (cfgEnc.encFormat == NV12 || cfgEnc.encFormat == NV21) */
  529. /* { */
  530. /* unsigned int srcChromaAddr = cfgEnc.srcChromaAddr; */
  531. /* srcChromaAddr = TO_CEIL(srcChromaAddr, 128); //((srcChromaAddr+127)&~127); */
  532. /* src_cfg.chroma_addr = srcChromaAddr; */
  533. /* } */
  534. /* */
  535. /* src_cfg.width = cfgEnc.encWidth; */
  536. /* src_cfg.height = cfgEnc.encHeight; */
  537. /* src_cfg.yuv_format = cfgEnc.encFormat; */
  538. /* 1. set src config */
  539. JPEG_MSG("[JPEGDRV]SRC_IMG: %x %x, DU:%x, fmt:%x!!\n", cfgEnc.encWidth,
  540. cfgEnc.encHeight, cfgEnc.totalEncDU, cfgEnc.encFormat);
  541. ret =
  542. jpeg_drv_enc_set_src_image(cfgEnc.encWidth, cfgEnc.encHeight, cfgEnc.encFormat,
  543. cfgEnc.totalEncDU);
  544. if (ret == 0) {
  545. JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce image failed\n");
  546. return -EFAULT;
  547. }
  548. /* 2. set src buffer info */
  549. JPEG_MSG("[JPEGDRV]SRC_BUF: addr %x, %x, stride %x, %x!!\n", cfgEnc.srcBufferAddr,
  550. cfgEnc.srcChromaAddr, cfgEnc.imgStride, cfgEnc.memStride);
  551. ret =
  552. jpeg_drv_enc_set_src_buf(cfgEnc.encFormat, cfgEnc.imgStride, cfgEnc.memStride,
  553. cfgEnc.srcBufferAddr, cfgEnc.srcChromaAddr);
  554. if (ret == 0) {
  555. JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce buffer failed\n");
  556. return -EFAULT;
  557. }
  558. /* if (0 == jpeg_drv_enc_src_cfg(src_cfg)) */
  559. /* { */
  560. /* JPEG_MSG("JPEG Encoder src cfg failed\n"); */
  561. /* return -EFAULT; */
  562. /* } */
  563. /* 3. set dst buffer info */
  564. JPEG_MSG("[JPEGDRV]DST_BUF: addr:%x, size:%x, ofs:%x, mask:%x!!\n",
  565. cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize, cfgEnc.dstBufAddrOffset,
  566. cfgEnc.dstBufAddrOffsetMask);
  567. ret =
  568. jpeg_drv_enc_set_dst_buff(cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize,
  569. cfgEnc.dstBufAddrOffset, cfgEnc.dstBufAddrOffsetMask);
  570. if (ret == 0) {
  571. JPEG_MSG("[JPEGDRV]JPEG Encoder set dst buffer failed\n");
  572. return -EFAULT;
  573. }
  574. /* memset(&dst_cfg, 0, sizeof(JpegDrvEncDstCfg)); */
  575. /* */
  576. /* dst_cfg.dst_addr = cfgEnc.dstBufferAddr; */
  577. /* dst_cfg.dst_size = cfgEnc.dstBufferSize; */
  578. /* dst_cfg.exif_en = cfgEnc.enableEXIF; */
  579. /* */
  580. /* */
  581. /* if (0 == jpeg_drv_enc_dst_buff(dst_cfg)) */
  582. /* return -EFAULT; */
  583. /* 4 .set ctrl config */
  584. JPEG_MSG("[JPEGDRV]ENC_CFG: exif:%d, q:%d, DRI:%d !!\n", cfgEnc.enableEXIF,
  585. cfgEnc.encQuality, cfgEnc.restartInterval);
  586. jpeg_drv_enc_ctrl_cfg(cfgEnc.enableEXIF, cfgEnc.encQuality, cfgEnc.restartInterval);
  587. /* memset(&ctrl_cfg, 0, sizeof(JpegDrvEncCtrlCfg)); */
  588. /* */
  589. /* ctrl_cfg.quality = cfgEnc.encQuality; */
  590. /* ctrl_cfg.gmc_disable = cfgEnc.disableGMC; */
  591. /* ctrl_cfg.restart_interval = cfgEnc.restartInterval; */
  592. /* */
  593. break;
  594. case JPEG_ENC_IOCTL_START:
  595. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Start!!\n");
  596. if (*pStatus != JPEG_ENC_PROCESS) {
  597. JPEG_WRN("Permission Denied! This process can not access encoder");
  598. return -EFAULT;
  599. }
  600. if (enc_status == 0) {
  601. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  602. *pStatus = 0;
  603. return -EFAULT;
  604. }
  605. jpeg_drv_enc_start();
  606. break;
  607. case JPEG_ENC_IOCTL_WAIT:
  608. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Wait!!\n");
  609. if (*pStatus != JPEG_ENC_PROCESS) {
  610. JPEG_WRN("Permission Denied! This process can not access encoder");
  611. return -EFAULT;
  612. }
  613. if (enc_status == 0) {
  614. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  615. *pStatus = 0;
  616. return -EFAULT;
  617. }
  618. if (copy_from_user(&enc_result, (void *)arg, sizeof(JpegDrvEncResult))) {
  619. JPEG_WRN("JPEG Encoder : Copy from user error\n");
  620. return -EFAULT;
  621. }
  622. /* TODO: ENC_DONE in REG_JPEG_ENC_INTERRUPT_STATUS need to set to 0 after read. */
  623. jpeg_enc_wait_timeout = 0xFFFFFF;
  624. #ifdef FPGA_VERSION
  625. do {
  626. _jpeg_enc_int_status = REG_JPEG_ENC_INTERRUPT_STATUS;
  627. jpeg_enc_wait_timeout--;
  628. } while (_jpeg_enc_int_status == 0 && jpeg_enc_wait_timeout > 0);
  629. if (jpeg_enc_wait_timeout == 0)
  630. JPEG_MSG("JPEG Encoder timeout\n");
  631. ret = jpeg_drv_enc_get_result(&file_size);
  632. JPEG_MSG("Result : %d, Size : %u, addres : 0x%x\n", ret, file_size,
  633. ioread32(JPEG_ENC_BASE + 0x120));
  634. if (_jpeg_enc_int_status != 1)
  635. jpeg_drv_enc_dump_reg();
  636. #else
  637. /* set timeout */
  638. timeout_jiff = enc_result.timeout * HZ / 1000;
  639. JPEG_MSG("[JPEGDRV]JPEG Encoder Time Jiffies : %ld\n", timeout_jiff);
  640. if (jpeg_isr_enc_lisr() < 0) {
  641. wait_event_interruptible_timeout(enc_wait_queue, _jpeg_enc_int_status,
  642. timeout_jiff);
  643. JPEG_MSG("[JPEGDRV]JPEG Encoder Wait done !!\n");
  644. } else {
  645. JPEG_MSG("[JPEGDRV]JPEG Encoder already done !!\n");
  646. }
  647. ret = jpeg_drv_enc_get_result(&file_size);
  648. JPEG_MSG("[JPEGDRV]Result : %d, Size : %u!!\n", ret, file_size);
  649. if (ret != 0)
  650. jpeg_drv_enc_dump_reg();
  651. #endif
  652. cycle_count = jpeg_drv_enc_get_cycle_count();
  653. if (copy_to_user(enc_result.pFileSize, &file_size, sizeof(unsigned int))) {
  654. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (file size)\n");
  655. return -EFAULT;
  656. }
  657. if (copy_to_user(enc_result.pResult, &ret, sizeof(unsigned int))) {
  658. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (status)\n");
  659. return -EFAULT;
  660. }
  661. if (copy_to_user(enc_result.pCycleCount, &cycle_count, sizeof(unsigned int))) {
  662. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (cycle)\n");
  663. return -EFAULT;
  664. }
  665. break;
  666. case JPEG_ENC_IOCTL_DEINIT:
  667. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Deinit!!\n");
  668. /* copy input parameters */
  669. if (*pStatus != JPEG_ENC_PROCESS) {
  670. JPEG_WRN("Permission Denied! This process can not access encoder");
  671. return -EFAULT;
  672. }
  673. if (enc_status == 0) {
  674. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  675. *pStatus = 0;
  676. return -EFAULT;
  677. }
  678. jpeg_drv_enc_deinit();
  679. *pStatus = 0;
  680. break;
  681. case JPEG_ENC_IOCTL_DUMP_REG:
  682. jpeg_drv_enc_dump_reg();
  683. break;
  684. default:
  685. JPEG_MSG("[JPEGDRV]JPEG ENC IOCTL NO THIS COMMAND\n");
  686. }
  687. return 0;
  688. }
  689. /* -------------------------------------------------------------------------- */
  690. /* */
  691. /* -------------------------------------------------------------------------- */
  692. /* static int jpeg_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) */
  693. static long jpeg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  694. {
  695. switch (cmd) {
  696. case JPEG_DEC_IOCTL_INIT:
  697. case JPEG_DEC_IOCTL_CONFIG:
  698. case JPEG_DEC_IOCTL_START:
  699. /* case JPEG_DEC_IOCTL_RANGE: */
  700. case JPEG_DEC_IOCTL_RESUME:
  701. case JPEG_DEC_IOCTL_WAIT:
  702. /* case JPEG_DEC_IOCTL_COPY: */
  703. case JPEG_DEC_IOCTL_DEINIT:
  704. case JPEG_DEC_IOCTL_DUMP_REG:
  705. return jpeg_dec_ioctl(cmd, arg, file);
  706. case JPEG_ENC_IOCTL_INIT:
  707. case JPEG_ENC_IOCTL_CONFIG:
  708. case JPEG_ENC_IOCTL_WAIT:
  709. case JPEG_ENC_IOCTL_DEINIT:
  710. case JPEG_ENC_IOCTL_START:
  711. return jpeg_enc_ioctl(cmd, arg, file);
  712. default:
  713. break;
  714. }
  715. return -EINVAL;
  716. }
  717. static int jpeg_open(struct inode *inode, struct file *file)
  718. {
  719. unsigned int *pStatus;
  720. /* Allocate and initialize private data */
  721. file->private_data = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
  722. if (NULL == file->private_data) {
  723. JPEG_WRN("Not enough entry for JPEG open operation\n");
  724. return -ENOMEM;
  725. }
  726. pStatus = (unsigned int *)file->private_data;
  727. *pStatus = 0;
  728. return 0;
  729. }
  730. static ssize_t jpeg_read(struct file *file, char __user *data, size_t len, loff_t *ppos)
  731. {
  732. JPEG_MSG("jpeg driver read\n");
  733. return 0;
  734. }
  735. static int jpeg_release(struct inode *inode, struct file *file)
  736. {
  737. if (NULL != file->private_data) {
  738. kfree(file->private_data);
  739. file->private_data = NULL;
  740. }
  741. return 0;
  742. }
  743. static int jpeg_flush(struct file *a_pstFile, fl_owner_t a_id)
  744. {
  745. unsigned int *pStatus;
  746. pStatus = (unsigned int *)a_pstFile->private_data;
  747. if (NULL == pStatus) {
  748. JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
  749. return -EFAULT;
  750. }
  751. if (*pStatus == JPEG_ENC_PROCESS) {
  752. if (enc_status != 0) {
  753. JPEG_WRN("Error! Enable error handling for jpeg encoder");
  754. jpeg_drv_enc_deinit();
  755. }
  756. }
  757. #ifdef JPEG_DEC_DRIVER
  758. else if (*pStatus == JPEG_DEC_PROCESS) {
  759. if (dec_status != 0) {
  760. JPEG_WRN("Error! Enable error handling for jpeg decoder");
  761. jpeg_drv_dec_deinit();
  762. }
  763. }
  764. #endif
  765. return 0;
  766. }
  767. /* Kernel interface */
  768. static struct const file_operations jpeg_fops = {
  769. .owner = THIS_MODULE,
  770. /* .ioctl = jpeg_ioctl, */
  771. .unlocked_ioctl = jpeg_unlocked_ioctl,
  772. .open = jpeg_open,
  773. .release = jpeg_release,
  774. .flush = jpeg_flush,
  775. .read = jpeg_read,
  776. };
  777. static int jpeg_probe(struct platform_device *pdev)
  778. {
  779. struct class_device;
  780. int ret;
  781. struct class_device *class_dev = NULL;
  782. JPEG_MSG("-------------jpeg driver probe-------\n");
  783. ret = alloc_chrdev_region(&jpeg_devno, 0, 1, JPEG_DEVNAME);
  784. if (ret)
  785. JPEG_ERR("Error: Can't Get Major number for JPEG Device\n");
  786. else
  787. JPEG_MSG("Get JPEG Device Major number (%d)\n", jpeg_devno);
  788. jpeg_cdev = cdev_alloc();
  789. jpeg_cdev->owner = THIS_MODULE;
  790. jpeg_cdev->ops = &jpeg_fops;
  791. ret = cdev_add(jpeg_cdev, jpeg_devno, 1);
  792. jpeg_class = class_create(THIS_MODULE, JPEG_DEVNAME);
  793. class_dev =
  794. (struct class_device *)device_create(jpeg_class, NULL, jpeg_devno, NULL, JPEG_DEVNAME);
  795. spin_lock_init(&jpeg_dec_lock);
  796. spin_lock_init(&jpeg_enc_lock);
  797. /* initial codec, register codec ISR */
  798. dec_status = 0;
  799. enc_status = 0;
  800. _jpeg_dec_int_status = 0;
  801. _jpeg_enc_int_status = 0;
  802. _jpeg_dec_mode = 0;
  803. #ifndef FPGA_VERSION
  804. #ifdef JPEG_DEC_DRIVER
  805. init_waitqueue_head(&dec_wait_queue);
  806. #endif
  807. init_waitqueue_head(&enc_wait_queue);
  808. /* mt6575_irq_set_sens(MT6575_JPEG_CODEC_IRQ_ID, MT65xx_LEVEL_SENSITIVE); */
  809. /* mt6575_irq_set_polarity(MT6575_JPEG_CODEC_IRQ_ID, MT65xx_POLARITY_LOW); */
  810. /* mt6575_irq_unmask(MT6575_JPEG_CODEC_IRQ_ID); */
  811. JPEG_MSG("request JPEG Encoder IRQ\n");
  812. enable_irq(MT6582_JPEG_ENC_IRQ_ID);
  813. if (request_irq
  814. (MT6582_JPEG_ENC_IRQ_ID, jpeg_drv_enc_isr, IRQF_TRIGGER_LOW, "jpeg_enc_driver", NULL)) {
  815. JPEG_ERR("JPEG ENC Driver request irq failed\n");
  816. }
  817. #ifdef JPEG_DEC_DRIVER
  818. enable_irq(MT6589_JPEG_DEC_IRQ_ID);
  819. JPEG_MSG("request JPEG Decoder IRQ\n");
  820. /* if(request_irq(MT6589_JPEG_DEC_IRQ_ID, jpeg_drv_dec_isr, IRQF_TRIGGER_LOW, "jpeg_dec_driver" , NULL)) */
  821. if (request_irq
  822. (MT6589_JPEG_DEC_IRQ_ID, jpeg_drv_dec_isr, IRQF_TRIGGER_FALLING, "jpeg_dec_driver",
  823. NULL)) {
  824. JPEG_ERR("JPEG DEC Driver request irq failed\n");
  825. }
  826. #endif
  827. #endif
  828. JPEG_MSG("JPEG Probe Done\n");
  829. NOT_REFERENCED(class_dev);
  830. return 0;
  831. }
  832. static int jpeg_remove(struct platform_device *pdev)
  833. {
  834. JPEG_MSG("JPEG Codec remove\n");
  835. /* unregister_chrdev(JPEGDEC_MAJOR, JPEGDEC_DEVNAME); */
  836. #ifndef FPGA_VERSION
  837. free_irq(MT6582_JPEG_ENC_IRQ_ID, NULL);
  838. #ifdef JPEG_DEC_DRIVER
  839. free_irq(MT6589_JPEG_DEC_IRQ_ID, NULL);
  840. #endif
  841. #endif
  842. JPEG_MSG("Done\n");
  843. return 0;
  844. }
  845. static void jpeg_shutdown(struct platform_device *pdev)
  846. {
  847. JPEG_MSG("JPEG Codec shutdown\n");
  848. /* Nothing yet */
  849. }
  850. /* PM suspend */
  851. static int jpeg_suspend(struct platform_device *pdev, pm_message_t mesg)
  852. {
  853. #ifdef JPEG_DEC_DRIVER
  854. jpeg_drv_dec_deinit();
  855. #endif
  856. jpeg_drv_enc_deinit();
  857. return 0;
  858. }
  859. /* PM resume */
  860. static int jpeg_resume(struct platform_device *pdev)
  861. {
  862. return 0;
  863. }
  864. static struct platform_driver jpeg_driver = {
  865. .probe = jpeg_probe,
  866. .remove = jpeg_remove,
  867. .shutdown = jpeg_shutdown,
  868. .suspend = jpeg_suspend,
  869. .resume = jpeg_resume,
  870. .driver = {
  871. .name = JPEG_DEVNAME,
  872. },
  873. };
  874. static void jpeg_device_release(struct device *dev)
  875. {
  876. /* Nothing to release? */
  877. }
  878. static u64 jpegdec_dmamask = ~(u32) 0;
  879. static struct platform_device jpeg_device = {
  880. .name = JPEG_DEVNAME,
  881. .id = 0,
  882. .dev = {
  883. .release = jpeg_device_release,
  884. .dma_mask = &jpegdec_dmamask,
  885. .coherent_dma_mask = 0xffffffff,
  886. },
  887. .num_resources = 0,
  888. };
  889. static int __init jpeg_init(void)
  890. {
  891. int ret;
  892. JPEG_MSG("JPEG Codec initialize\n");
  893. JPEG_MSG("Register the JPEG Codec device\n");
  894. if (platform_device_register(&jpeg_device)) {
  895. JPEG_ERR("failed to register jpeg codec device\n");
  896. ret = -ENODEV;
  897. return ret;
  898. }
  899. JPEG_MSG("Register the JPEG Codec driver\n");
  900. if (platform_driver_register(&jpeg_driver)) {
  901. JPEG_ERR("failed to register jpeg codec driver\n");
  902. platform_device_unregister(&jpeg_device);
  903. ret = -ENODEV;
  904. return ret;
  905. }
  906. return 0;
  907. }
  908. static void __exit jpeg_exit(void)
  909. {
  910. cdev_del(jpeg_cdev);
  911. unregister_chrdev_region(jpeg_devno, 1);
  912. /* JPEG_MSG("Unregistering driver\n"); */
  913. platform_driver_unregister(&jpeg_driver);
  914. platform_device_unregister(&jpeg_device);
  915. device_destroy(jpeg_class, jpeg_devno);
  916. class_destroy(jpeg_class);
  917. JPEG_MSG("Done\n");
  918. }
  919. module_init(jpeg_init);
  920. module_exit(jpeg_exit);
  921. MODULE_AUTHOR("Hao-Ting Huang <otis.huang@mediatek.com>");
  922. MODULE_DESCRIPTION("MT6582 JPEG Codec Driver");
  923. MODULE_LICENSE("GPL");