jpeg_drv.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. #include <linux/kernel.h>
  2. #include <linux/mm.h>
  3. #include <linux/mm_types.h>
  4. #include <linux/module.h>
  5. #include <generated/autoconf.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/cdev.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/delay.h>
  11. #include <linux/ioport.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/device.h>
  15. #include <linux/fs.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/wait.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/param.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. /* #include <linux/xlog.h> */
  24. #include <linux/io.h>
  25. #include <cmdq_core.h>
  26. /* ============================================================ */
  27. /* #include <linux/uaccess.h> */
  28. /* #include <linux/module.h> */
  29. /* #include <linux/fs.h> */
  30. /* #include <linux/platform_device.h> */
  31. /* #include <linux/cdev.h> */
  32. #include <linux/interrupt.h>
  33. /* #include <asm/io.h> */
  34. #include <linux/sched.h>
  35. #include <linux/wait.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/delay.h>
  38. /* #include <linux/earlysuspend.h> */
  39. /* #include <linux/mm.h> */
  40. #include <linux/vmalloc.h>
  41. #include <linux/dma-mapping.h>
  42. /* #include <linux/slab.h> */
  43. /* #include <linux/gfp.h> */
  44. /* #include <linux/aee.h> */
  45. #include <linux/timer.h>
  46. /* #include <linux/disp_assert_layer.h> */
  47. /* #include <linux/xlog.h> */
  48. /* #include <linux/fs.h> */
  49. /* Arch dependent files */
  50. /* #include <asm/mach/map.h> */
  51. /* #include <mach/mt6577_pll.h> */
  52. /* #include <mach/mt_irq.h> */
  53. /* #include <mach/irqs.h> */
  54. #ifdef CONFIG_MTK_CLKMGR
  55. #include <mach/mt_clkmgr.h>
  56. #endif /* defined(CONFIG_MTK_LEGACY) */
  57. /* #include <asm/tcm.h> */
  58. #include <asm/cacheflush.h>
  59. /* #include <asm/system.h> */
  60. /* #include <linux/mm.h> */
  61. #include <linux/pagemap.h>
  62. #ifndef FPGA_VERSION
  63. /* #include <mach/mt_boot.h> */
  64. #endif
  65. #include <cmdq_record.h>
  66. #ifndef JPEG_DEV
  67. #include <linux/proc_fs.h>
  68. #endif
  69. #ifdef CONFIG_OF
  70. /* device tree */
  71. #include <linux/of.h>
  72. #include <linux/of_irq.h>
  73. #include <linux/of_address.h>
  74. #include <linux/io.h>
  75. #include <linux/of_device.h>
  76. #endif
  77. #ifdef JPEG_PM_DOMAIN_ENABLE
  78. /*#include <linux/pm_runtime.h>*/
  79. #include "mt_smi.h"
  80. #endif
  81. /* ========================================================== */
  82. #include "jpeg_drv.h"
  83. #include "jpeg_drv_common.h"
  84. #include "jpeg_cmdq.h"
  85. /* #define USE_SYSRAM */
  86. #define JPEG_DEVNAME "mtk_jpeg"
  87. #define JDEC_DEVNAME "mtk_jdec"
  88. #define TABLE_SIZE 4096
  89. #define JPEG_DEC_PROCESS 0x1
  90. #define JPEG_ENC_PROCESS 0x2
  91. /* #define FPGA_VERSION */
  92. #include "jpeg_drv_reg.h"
  93. /* -------------------------------------------------------------------------- */
  94. /* */
  95. /* -------------------------------------------------------------------------- */
  96. #ifdef JPEG_DEV
  97. /* device and driver */
  98. static dev_t jenc_devno;
  99. static struct cdev *jenc_cdev;
  100. static struct class *jenc_class;
  101. #ifdef JPEG_PM_DOMAIN_ENABLE
  102. static dev_t jdec_devno;
  103. static struct cdev *jdec_cdev;
  104. static struct class *jdec_class;
  105. #endif
  106. #endif
  107. static struct JpegDeviceStruct gJpegqDev;
  108. #ifdef CONFIG_OF
  109. static struct JpegDeviceStruct *gJpegqDevs;
  110. static int nrJpegDevs;
  111. static const struct of_device_id jenc_of_ids[] = {
  112. {.compatible = "mediatek,jpgenc",},
  113. {}
  114. };
  115. #ifdef JPEG_PM_DOMAIN_ENABLE
  116. static const struct of_device_id jdec_of_ids[] = {
  117. {.compatible = "mediatek,jpgdec",},
  118. {}
  119. };
  120. #endif
  121. #endif
  122. #ifndef CONFIG_MTK_CLKMGR
  123. static struct JpegClk gJpegClk;
  124. #endif
  125. /* decoder */
  126. #ifdef JPEG_DEC_DRIVER
  127. static wait_queue_head_t dec_wait_queue;
  128. static spinlock_t jpeg_dec_lock;
  129. static int dec_status;
  130. #endif
  131. #ifdef JPEG_PM_DOMAIN_ENABLE
  132. struct platform_device *pjdec_dev;
  133. struct platform_device *pjenc_dev;
  134. #endif
  135. /* encoder */
  136. static wait_queue_head_t enc_wait_queue;
  137. static spinlock_t jpeg_enc_lock;
  138. static int enc_status;
  139. /* ========================================== */
  140. /* CMDQ */
  141. /* static cmdqRecStruct jpegCMDQ_handle; */
  142. #ifdef JPEG_DEC_DRIVER
  143. /* static cmdqRecHandle jpegCMDQ_handle; */
  144. #endif
  145. /* -------------------------------------------------------------------------- */
  146. /* JPEG Common Function */
  147. /* -------------------------------------------------------------------------- */
  148. #ifdef FPGA_VERSION
  149. void jpeg_drv_dec_power_on(void)
  150. {
  151. JPEG_MSG("JPEG Decoder Power On\n");
  152. }
  153. void jpeg_drv_dec_power_off(void)
  154. {
  155. JPEG_MSG("JPEG Decoder Power Off\n");
  156. }
  157. void jpeg_drv_enc_power_on(void)
  158. {
  159. #ifdef FPGA_VERSION
  160. IMG_REG_WRITE((0), JPEG_EARLY_MM_BASE);
  161. JPEG_MSG("JPEG Encoder RESET_MM_BASE!!\n");
  162. #endif
  163. JPEG_MSG("JPEG Encoder Power On\n");
  164. }
  165. void jpeg_drv_enc_power_off(void)
  166. {
  167. JPEG_MSG("JPEG Encoder Power Off\n");
  168. }
  169. #else
  170. static irqreturn_t jpeg_drv_enc_isr(int irq, void *dev_id)
  171. {
  172. /* JPEG_MSG("JPEG Encoder Interrupt\n"); */
  173. if (irq == gJpegqDev.encIrqId) {
  174. if (jpeg_isr_enc_lisr() == 0)
  175. wake_up_interruptible(&enc_wait_queue);
  176. }
  177. return IRQ_HANDLED;
  178. }
  179. #ifdef JPEG_DEC_DRIVER
  180. static irqreturn_t jpeg_drv_dec_isr(int irq, void *dev_id)
  181. {
  182. /* JPEG_MSG("JPEG Decoder Interrupt\n"); */
  183. if (irq == gJpegqDev.decIrqId) {
  184. /* mt65xx_irq_mask(MT6575_JPEG_CODEC_IRQ_ID); */
  185. if (jpeg_isr_dec_lisr() == 0)
  186. wake_up_interruptible(&dec_wait_queue);
  187. }
  188. return IRQ_HANDLED;
  189. }
  190. void jpeg_drv_dec_power_on(void)
  191. {
  192. #ifdef CONFIG_MTK_CLKMGR
  193. enable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  194. enable_clock(MT_CG_VENC_LARB, "JPEG");
  195. enable_clock(MT_CG_VENC_JPGDEC, "JPEG");
  196. #else
  197. #ifdef JPEG_PM_DOMAIN_ENABLE
  198. mtk_smi_larb_clock_on(3, true);
  199. #else
  200. if (clk_prepare_enable(gJpegClk.clk_disp_mtcmos))
  201. JPEG_ERR("enable disp_mtcmos clk fail!");
  202. if (clk_prepare_enable(gJpegClk.clk_venc_mtcmos))
  203. JPEG_ERR("enable venc_mtcmos clk fail!");
  204. if (clk_prepare_enable(gJpegClk.clk_disp_smi))
  205. JPEG_ERR("enable smi clk fail!");
  206. if (clk_prepare_enable(gJpegClk.clk_venc_larb))
  207. JPEG_ERR("enable larb clk fail!");
  208. #endif
  209. if (clk_prepare_enable(gJpegClk.clk_venc_jpgDec))
  210. JPEG_ERR("enable jpgDec clk fail!");
  211. #endif
  212. }
  213. void jpeg_drv_dec_power_off(void)
  214. {
  215. #ifdef CONFIG_MTK_CLKMGR
  216. disable_clock(MT_CG_VENC_JPGDEC, "JPEG");
  217. disable_clock(MT_CG_VENC_LARB, "JPEG");
  218. disable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  219. #else
  220. clk_disable_unprepare(gJpegClk.clk_venc_jpgDec);
  221. #ifdef JPEG_PM_DOMAIN_ENABLE
  222. mtk_smi_larb_clock_off(3, true);
  223. #else
  224. clk_disable_unprepare(gJpegClk.clk_venc_larb);
  225. clk_disable_unprepare(gJpegClk.clk_disp_smi);
  226. clk_disable_unprepare(gJpegClk.clk_venc_mtcmos);
  227. clk_disable_unprepare(gJpegClk.clk_disp_mtcmos);
  228. #endif
  229. #endif
  230. }
  231. #endif
  232. void jpeg_drv_enc_power_on(void)
  233. {
  234. #ifdef CONFIG_MTK_CLKMGR
  235. /* REG_JPEG_MM_REG_MASK = 0; */
  236. enable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  237. #ifdef CONFIG_ARCH_MT6735M
  238. enable_clock(MT_CG_IMAGE_JPGENC, "JPEG");
  239. #else
  240. enable_clock(MT_CG_VENC_LARB, "JPEG");
  241. enable_clock(MT_CG_VENC_JPGENC, "JPEG");
  242. #endif
  243. #else
  244. #ifdef JPEG_PM_DOMAIN_ENABLE
  245. mtk_smi_larb_clock_on(3, true);
  246. #else
  247. if (clk_prepare_enable(gJpegClk.clk_disp_mtcmos))
  248. JPEG_ERR("enable disp_mtcmos clk fail!");
  249. if (clk_prepare_enable(gJpegClk.clk_venc_mtcmos))
  250. JPEG_ERR("enable venc_mtcmos clk fail!");
  251. if (clk_prepare_enable(gJpegClk.clk_disp_smi))
  252. JPEG_ERR("enable smi clk fail!");
  253. #ifndef CONFIG_ARCH_MT6735M
  254. if (clk_prepare_enable(gJpegClk.clk_venc_larb))
  255. JPEG_ERR("enable larb clk fail!");
  256. #endif
  257. #endif
  258. if (clk_prepare_enable(gJpegClk.clk_venc_jpgEnc))
  259. JPEG_ERR("enable jpgEnc clk fail!");
  260. #endif
  261. }
  262. void jpeg_drv_enc_power_off(void)
  263. {
  264. #ifdef CONFIG_MTK_CLKMGR
  265. #ifdef CONFIG_ARCH_MT6735M
  266. disable_clock(MT_CG_IMAGE_JPGENC, "JPEG");
  267. #else
  268. disable_clock(MT_CG_VENC_JPGENC, "JPEG");
  269. disable_clock(MT_CG_VENC_LARB, "JPEG");
  270. #endif
  271. disable_clock(MT_CG_DISP0_SMI_COMMON, "JPEG");
  272. #else
  273. clk_disable_unprepare(gJpegClk.clk_venc_jpgEnc);
  274. #ifdef JPEG_PM_DOMAIN_ENABLE
  275. mtk_smi_larb_clock_off(3, true);
  276. #else
  277. #ifndef CONFIG_ARCH_MT6735M
  278. clk_disable_unprepare(gJpegClk.clk_venc_larb);
  279. #endif
  280. clk_disable_unprepare(gJpegClk.clk_disp_smi);
  281. clk_disable_unprepare(gJpegClk.clk_venc_mtcmos);
  282. clk_disable_unprepare(gJpegClk.clk_disp_mtcmos);
  283. #endif
  284. #endif
  285. }
  286. #endif
  287. #ifdef JPEG_DEC_DRIVER
  288. static int jpeg_drv_dec_init(void)
  289. {
  290. int retValue;
  291. spin_lock(&jpeg_dec_lock);
  292. if (dec_status != 0) {
  293. JPEG_WRN("JPEG Decoder is busy\n");
  294. retValue = -EBUSY;
  295. } else {
  296. dec_status = 1;
  297. retValue = 0;
  298. }
  299. spin_unlock(&jpeg_dec_lock);
  300. if (retValue == 0) {
  301. jpeg_drv_dec_power_on();
  302. jpeg_drv_dec_verify_state_and_reset();
  303. }
  304. return retValue;
  305. }
  306. static void jpeg_drv_dec_deinit(void)
  307. {
  308. if (dec_status != 0) {
  309. spin_lock(&jpeg_dec_lock);
  310. dec_status = 0;
  311. spin_unlock(&jpeg_dec_lock);
  312. jpeg_drv_dec_reset();
  313. jpeg_drv_dec_power_off();
  314. }
  315. }
  316. #endif
  317. static int jpeg_drv_enc_init(void)
  318. {
  319. int retValue;
  320. spin_lock(&jpeg_enc_lock);
  321. if (enc_status != 0) {
  322. JPEG_WRN("JPEG Encoder is busy\n");
  323. retValue = -EBUSY;
  324. } else {
  325. enc_status = 1;
  326. retValue = 0;
  327. }
  328. spin_unlock(&jpeg_enc_lock);
  329. if (retValue == 0) {
  330. jpeg_drv_enc_power_on();
  331. jpeg_drv_enc_verify_state_and_reset();
  332. }
  333. return retValue;
  334. }
  335. static void jpeg_drv_enc_deinit(void)
  336. {
  337. if (enc_status != 0) {
  338. spin_lock(&jpeg_enc_lock);
  339. enc_status = 0;
  340. spin_unlock(&jpeg_enc_lock);
  341. jpeg_drv_enc_reset();
  342. jpeg_drv_enc_power_off();
  343. }
  344. }
  345. /* -------------------------------------------------------------------------- */
  346. /* JPEG REG DUMP FUNCTION */
  347. /* -------------------------------------------------------------------------- */
  348. void jpeg_reg_dump(void)
  349. {
  350. unsigned int reg_value = 0;
  351. unsigned int index = 0;
  352. JPEG_WRN("JPEG REG:\n ********************\n");
  353. for (index = 0; index < 0x168; index += 4) {
  354. /* reg_value = ioread32(JPEG_DEC_BASE + index); */
  355. IMG_REG_READ(reg_value, JPEG_DEC_BASE + index);
  356. JPEG_WRN("+0x%x 0x%x\n", index, reg_value);
  357. }
  358. }
  359. /* -------------------------------------------------------------------------- */
  360. /* JPEG DECODER IOCTRL FUNCTION */
  361. /* -------------------------------------------------------------------------- */
  362. #ifdef JPEG_DEC_DRIVER
  363. static int jpeg_dec_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
  364. {
  365. unsigned int *pStatus;
  366. unsigned int decResult;
  367. long timeout_jiff;
  368. JPEG_DEC_DRV_IN dec_params;
  369. JPEG_DEC_CONFIG_ROW dec_row_params;
  370. /* JPEG_DEC_CONFIG_CMDQ cfg_cmdq_params; */
  371. unsigned int irq_st = 0;
  372. /* unsigned int timeout = 0x1FFFFF; */
  373. JPEG_DEC_DRV_OUT outParams;
  374. pStatus = (unsigned int *)file->private_data;
  375. if (NULL == pStatus) {
  376. JPEG_MSG
  377. ("[JPEGDRV]JPEG Decoder: Private data is null in flush operation. SOME THING WRONG??\n");
  378. return -EFAULT;
  379. }
  380. switch (cmd) {
  381. /* initial and reset JPEG encoder */
  382. case JPEG_DEC_IOCTL_INIT: /* OT:OK */
  383. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Init!!\n");*/
  384. if (jpeg_drv_dec_init() == 0)
  385. *pStatus = JPEG_DEC_PROCESS;
  386. break;
  387. case JPEG_DEC_IOCTL_RESET: /* OT:OK */
  388. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Reset!!\n");
  389. jpeg_drv_dec_reset();
  390. break;
  391. case JPEG_DEC_IOCTL_CONFIG:
  392. /* JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Configration!!\n"); */
  393. if (*pStatus != JPEG_DEC_PROCESS) {
  394. JPEG_MSG
  395. ("[JPEGDRV]Permission Denied! This process can not access decoder\n");
  396. return -EFAULT;
  397. }
  398. if (dec_status == 0) {
  399. JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
  400. *pStatus = 0;
  401. return -EFAULT;
  402. }
  403. if (copy_from_user(&dec_params, (void *)arg, sizeof(JPEG_DEC_DRV_IN))) {
  404. JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
  405. return -EFAULT;
  406. }
  407. /* _jpeg_dec_dump_reg_en = dec_params.regDecDumpEn; */
  408. if (dec_params.decodeMode == JPEG_DEC_MODE_MCU_ROW)
  409. _jpeg_dec_mode = 1;
  410. else
  411. _jpeg_dec_mode = 0;
  412. if (jpeg_drv_dec_set_config_data(&dec_params) < 0)
  413. return -EFAULT;
  414. break;
  415. case JPEG_DEC_IOCTL_FLUSH_CMDQ:
  416. #if 0 /* currently no use */
  417. JPEG_MSG("[JPEGDRV]enter JPEG BUILD CMDQ !!\n");
  418. if (*pStatus != JPEG_DEC_PROCESS) {
  419. JPEG_MSG
  420. ("[JPEGDRV]Permission Denied! This process can not access decoder\n");
  421. return -EFAULT;
  422. }
  423. if (dec_status == 0) {
  424. JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
  425. *pStatus = 0;
  426. return -EFAULT;
  427. }
  428. if (copy_from_user(&cfg_cmdq_params, (void *)arg, sizeof(JPEG_DEC_CONFIG_CMDQ))) {
  429. JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
  430. return -EFAULT;
  431. }
  432. JPEG_MSG("[JPEGDRV]JPEG CREATE CMDQ !!\n");
  433. cmdqRecCreate(CMDQ_SCENARIO_JPEG_DEC, &jpegCMDQ_handle);
  434. cmdqRecWait(jpegCMDQ_handle, CMDQ_EVENT_JPEG_DEC_EOF);
  435. for (i = 0; i < cfg_cmdq_params.goNum; i++) {
  436. JPEG_MSG("[JPEGDRV]JPEG gen CMDQ %x %x %x %x !!\n",
  437. cfg_cmdq_params.pauseMCUidx[i], cfg_cmdq_params.decRowBuf0[i],
  438. cfg_cmdq_params.decRowBuf1[i], cfg_cmdq_params.decRowBuf2[i]);
  439. cmdqRecWrite(jpegCMDQ_handle,
  440. 0x18004170 /*REG_ADDR_JPGDEC_PAUSE_MCU_NUM */ ,
  441. cfg_cmdq_params.pauseMCUidx[i] - 1, 0xFFFFFFFF);
  442. cmdqRecWrite(jpegCMDQ_handle, 0x18004140 /*REG_ADDR_JPGDEC_DEST_ADDR0_Y */ ,
  443. cfg_cmdq_params.decRowBuf0[i], 0xFFFFFFFF);
  444. cmdqRecWrite(jpegCMDQ_handle, 0x18004144 /*REG_ADDR_JPGDEC_DEST_ADDR0_U */ ,
  445. cfg_cmdq_params.decRowBuf1[i], 0xFFFFFFFF);
  446. cmdqRecWrite(jpegCMDQ_handle, 0x18004148 /*REG_ADDR_JPGDEC_DEST_ADDR0_V */ ,
  447. cfg_cmdq_params.decRowBuf2[i], 0xFFFFFFFF);
  448. JPEG_MSG("[JPEGDRV]JPEG gen CMDQ go!!\n");
  449. /* trigger */
  450. cmdqRecWrite(jpegCMDQ_handle,
  451. 0x18004274 /*REG_ADDR_JPGDEC_INTERRUPT_STATUS */ ,
  452. BIT_INQST_MASK_PAUSE, 0xFFFFFFFF);
  453. JPEG_MSG("[JPEGDRV]JPEG gen CMDQ wait!!\n");
  454. /* wait frame done */
  455. cmdqRecWait(jpegCMDQ_handle, CMDQ_EVENT_JPEG_DEC_EOF);
  456. /* cmdqRecPoll(jpegCMDQ_handle, 0x18004274 */
  457. /* , BIT_INQST_MASK_PAUSE, 0x0010); */
  458. }
  459. JPEG_MSG("[JPEGDRV]JPEG flush CMDQ start!!\n");
  460. cmdqRecFlush(jpegCMDQ_handle);
  461. JPEG_MSG("[JPEGDRV]JPEG flush CMDQ end!!\n");
  462. cmdqRecDestroy(jpegCMDQ_handle);
  463. JPEG_MSG("[JPEGDRV]JPEG destroy CMDQ end!!\n");
  464. #endif
  465. break;
  466. case JPEG_DEC_IOCTL_RESUME:
  467. if (*pStatus != JPEG_DEC_PROCESS) {
  468. JPEG_MSG
  469. ("[JPEGDRV]Permission Denied! This process can not access decoder\n");
  470. return -EFAULT;
  471. }
  472. if (dec_status == 0) {
  473. JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
  474. *pStatus = 0;
  475. return -EFAULT;
  476. }
  477. if (copy_from_user(&dec_row_params, (void *)arg, sizeof(JPEG_DEC_CONFIG_ROW))) {
  478. JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
  479. return -EFAULT;
  480. }
  481. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Resume, [%d] %x %x %x !!\n",
  482. dec_row_params.pauseMCU - 1, dec_row_params.decRowBuf[0],
  483. dec_row_params.decRowBuf[1], dec_row_params.decRowBuf[2]);
  484. jpeg_drv_dec_set_dst_bank0(dec_row_params.decRowBuf[0], dec_row_params.decRowBuf[1],
  485. dec_row_params.decRowBuf[2]);
  486. jpeg_drv_dec_set_pause_mcu_idx(dec_row_params.pauseMCU - 1);
  487. /* lock CPU to ensure irq is enabled after trigger HW */
  488. spin_lock(&jpeg_dec_lock);
  489. jpeg_drv_dec_resume(BIT_INQST_MASK_PAUSE);
  490. spin_unlock(&jpeg_dec_lock);
  491. break;
  492. case JPEG_DEC_IOCTL_START: /* OT:OK */
  493. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Start!!\n");*/
  494. jpeg_drv_dec_start();
  495. break;
  496. case JPEG_DEC_IOCTL_WAIT:
  497. if (*pStatus != JPEG_DEC_PROCESS) {
  498. JPEG_WRN("Permission Denied! This process can not access decoder");
  499. return -EFAULT;
  500. }
  501. if (dec_status == 0) {
  502. JPEG_WRN("Decoder status is available, HOW COULD THIS HAPPEN ??");
  503. *pStatus = 0;
  504. return -EFAULT;
  505. }
  506. if (copy_from_user(&outParams, (void *)arg, sizeof(JPEG_DEC_DRV_OUT))) {
  507. JPEG_WRN("JPEG Decoder : Copy from user error\n");
  508. return -EFAULT;
  509. }
  510. /* set timeout */
  511. timeout_jiff = outParams.timeout * HZ / 1000;
  512. /* JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Wait Resume Time Jiffies : %ld\n", timeout_jiff); */
  513. #ifdef FPGA_VERSION
  514. JPEG_MSG("[JPEGDRV]Polling JPEG Status");
  515. do {
  516. _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS;
  517. } while (_jpeg_dec_int_status == 0);
  518. #else
  519. /* if(outParams.timeout >= 5000){ */
  520. /* */
  521. /* JPEG_MSG("Polling JPEG Status"); */
  522. /* do */
  523. /* { */
  524. /* _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS; */
  525. /* timeout--; */
  526. /* } while(_jpeg_dec_int_status == 0 && timeout != 0); */
  527. /* if(timeout == 0) JPEG_MSG("Polling JPEG Status TIMEOUT!!\n"); */
  528. /* }else */
  529. if (jpeg_isr_dec_lisr() < 0) {
  530. /* JPEG_MSG("wait JPEG irq\n"); */
  531. wait_event_interruptible_timeout(dec_wait_queue, _jpeg_dec_int_status,
  532. timeout_jiff);
  533. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Done!!\n");*/
  534. } else {
  535. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Already Done!!\n");*/
  536. }
  537. #endif
  538. decResult = jpeg_drv_dec_get_result();
  539. /* jpeg_drv_dec_dump_key_reg(); */
  540. if (decResult >= 2) {
  541. JPEG_MSG("[JPEGDRV]Decode Result : %d, status %x!\n", decResult,
  542. _jpeg_dec_int_status);
  543. jpeg_drv_dec_dump_key_reg();
  544. /* jpeg_drv_dec_dump_reg(); */
  545. jpeg_drv_dec_warm_reset();
  546. }
  547. irq_st = _jpeg_dec_int_status;
  548. decResult = decResult | (irq_st << 8);
  549. _jpeg_dec_int_status = 0;
  550. if (copy_to_user(outParams.result, &decResult, sizeof(unsigned int))) {
  551. JPEG_WRN("JPEG Decoder : Copy to user error (result)\n");
  552. return -EFAULT;
  553. }
  554. break;
  555. case JPEG_DEC_IOCTL_BREAK:
  556. if (jpeg_drv_dec_break() < 0)
  557. return -EFAULT;
  558. break;
  559. case JPEG_DEC_IOCTL_DUMP_REG:
  560. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder DUMP REGISTER !!\n");
  561. jpeg_drv_dec_dump_reg();
  562. break;
  563. case JPEG_DEC_IOCTL_DEINIT:
  564. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Deinit !!\n");*/
  565. /* copy input parameters */
  566. if (*pStatus != JPEG_DEC_PROCESS) {
  567. JPEG_ERR("Permission Denied! This process can not access encoder");
  568. return -EFAULT;
  569. }
  570. if (dec_status == 0) {
  571. JPEG_ERR("Encoder status is available, HOW COULD THIS HAPPEN ??");
  572. *pStatus = 0;
  573. return -EFAULT;
  574. }
  575. jpeg_drv_dec_deinit();
  576. *pStatus = 0;
  577. break;
  578. default:
  579. JPEG_ERR("JPEG DEC IOCTL NO THIS COMMAND\n");
  580. break;
  581. }
  582. return 0;
  583. }
  584. #endif /* JPEG_DEC_DRIVER */
  585. static int jpeg_enc_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
  586. {
  587. int retValue;
  588. /* unsigned int decResult; */
  589. long timeout_jiff;
  590. unsigned int file_size, enc_result_code;
  591. /* unsigned int _jpeg_enc_int_status; */
  592. unsigned int jpeg_enc_wait_timeout = 0;
  593. unsigned int cycle_count;
  594. unsigned int ret;
  595. unsigned int *pStatus;
  596. /* JpegDrvEncParam cfgEnc; */
  597. JPEG_ENC_DRV_IN cfgEnc;
  598. JPEG_ENC_DRV_OUT enc_result;
  599. pStatus = (unsigned int *)file->private_data;
  600. if (NULL == pStatus) {
  601. JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
  602. return -EFAULT;
  603. }
  604. switch (cmd) {
  605. case JPEG_ENC_IOCTL_RW_REG:
  606. /* jpeg_drv_enc_rw_reg(); */
  607. break;
  608. /* initial and reset JPEG encoder */
  609. case JPEG_ENC_IOCTL_INIT:
  610. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Init!!\n");*/
  611. retValue = jpeg_drv_enc_init();
  612. if (retValue == 0)
  613. *pStatus = JPEG_ENC_PROCESS;
  614. return retValue;
  615. break;
  616. case JPEG_ENC_IOCTL_WARM_RESET:
  617. JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Warm Reset\n");
  618. enc_result_code = jpeg_drv_enc_warm_reset();
  619. if (0 == enc_result_code)
  620. return -EFAULT;
  621. break;
  622. /* configure the register */
  623. case JPEG_ENC_IOCTL_CONFIG:
  624. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Configure Hardware\n");*/
  625. if (*pStatus != JPEG_ENC_PROCESS) {
  626. JPEG_WRN("Permission Denied! This process can not access encoder");
  627. return -EFAULT;
  628. }
  629. if (enc_status == 0) {
  630. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  631. *pStatus = 0;
  632. return -EFAULT;
  633. }
  634. /* copy input parameters */
  635. if (copy_from_user(&cfgEnc, (void *)arg, sizeof(JPEG_ENC_DRV_IN))) {
  636. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy from user error\n");
  637. return -EFAULT;
  638. }
  639. /* 0. reset */
  640. jpeg_drv_enc_reset();
  641. /* 1. set src config */
  642. /* memset(&src_cfg, 0, sizeof(JpegDrvEncSrcCfg)); */
  643. /* src_cfg.luma_addr = cfgEnc.srcBufferAddr; */
  644. /* if (cfgEnc.encFormat == NV12 || cfgEnc.encFormat == NV21) */
  645. /* { */
  646. /* unsigned int srcChromaAddr = cfgEnc.srcChromaAddr; */
  647. /* srcChromaAddr = TO_CEIL(srcChromaAddr, 128); //((srcChromaAddr+127)&~127); */
  648. /* src_cfg.chroma_addr = srcChromaAddr; */
  649. /* } */
  650. /* */
  651. /* src_cfg.width = cfgEnc.encWidth; */
  652. /* src_cfg.height = cfgEnc.encHeight; */
  653. /* src_cfg.yuv_format = cfgEnc.encFormat; */
  654. /* 1. set src config */
  655. JPEG_MSG("[JPEGDRV]SRC_IMG: %x %x, DU:%x, fmt:%x!!\n", cfgEnc.encWidth,
  656. cfgEnc.encHeight, cfgEnc.totalEncDU, cfgEnc.encFormat);
  657. ret =
  658. jpeg_drv_enc_set_src_image(cfgEnc.encWidth, cfgEnc.encHeight, cfgEnc.encFormat,
  659. cfgEnc.totalEncDU);
  660. if (ret == 0) {
  661. JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce image failed\n");
  662. return -EFAULT;
  663. }
  664. /* 2. set src buffer info */
  665. JPEG_MSG("[JPEGDRV]SRC_BUF: addr %x, %x, stride %x, %x!!\n", cfgEnc.srcBufferAddr,
  666. cfgEnc.srcChromaAddr, cfgEnc.imgStride, cfgEnc.memStride);
  667. ret =
  668. jpeg_drv_enc_set_src_buf(cfgEnc.encFormat, cfgEnc.imgStride, cfgEnc.memStride,
  669. cfgEnc.srcBufferAddr, cfgEnc.srcChromaAddr);
  670. if (ret == 0) {
  671. JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce buffer failed\n");
  672. return -EFAULT;
  673. }
  674. /* if (0 == jpeg_drv_enc_src_cfg(src_cfg)) */
  675. /* { */
  676. /* JPEG_MSG("JPEG Encoder src cfg failed\n"); */
  677. /* return -EFAULT; */
  678. /* } */
  679. /* 3. set dst buffer info */
  680. JPEG_MSG("[JPEGDRV]DST_BUF: addr:%x, size:%x, ofs:%x, mask:%x!!\n",
  681. cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize, cfgEnc.dstBufAddrOffset,
  682. cfgEnc.dstBufAddrOffsetMask);
  683. ret =
  684. jpeg_drv_enc_set_dst_buff(cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize,
  685. cfgEnc.dstBufAddrOffset, cfgEnc.dstBufAddrOffsetMask);
  686. if (ret == 0) {
  687. JPEG_MSG("[JPEGDRV]JPEG Encoder set dst buffer failed\n");
  688. return -EFAULT;
  689. }
  690. /* memset(&dst_cfg, 0, sizeof(JpegDrvEncDstCfg)); */
  691. /* */
  692. /* dst_cfg.dst_addr = cfgEnc.dstBufferAddr; */
  693. /* dst_cfg.dst_size = cfgEnc.dstBufferSize; */
  694. /* dst_cfg.exif_en = cfgEnc.enableEXIF; */
  695. /* */
  696. /* */
  697. /* if (0 == jpeg_drv_enc_dst_buff(dst_cfg)) */
  698. /* return -EFAULT; */
  699. /* 4 .set ctrl config */
  700. JPEG_MSG("[JPEGDRV]ENC_CFG: exif:%d, q:%d, DRI:%d !!\n", cfgEnc.enableEXIF,
  701. cfgEnc.encQuality, cfgEnc.restartInterval);
  702. jpeg_drv_enc_ctrl_cfg(cfgEnc.enableEXIF, cfgEnc.encQuality, cfgEnc.restartInterval);
  703. /* memset(&ctrl_cfg, 0, sizeof(JpegDrvEncCtrlCfg)); */
  704. /* */
  705. /* ctrl_cfg.quality = cfgEnc.encQuality; */
  706. /* ctrl_cfg.gmc_disable = cfgEnc.disableGMC; */
  707. /* ctrl_cfg.restart_interval = cfgEnc.restartInterval; */
  708. /* */
  709. break;
  710. case JPEG_ENC_IOCTL_START:
  711. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Start!!\n");*/
  712. if (*pStatus != JPEG_ENC_PROCESS) {
  713. JPEG_WRN("Permission Denied! This process can not access encoder");
  714. return -EFAULT;
  715. }
  716. if (enc_status == 0) {
  717. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  718. *pStatus = 0;
  719. return -EFAULT;
  720. }
  721. jpeg_drv_enc_start();
  722. break;
  723. case JPEG_ENC_IOCTL_WAIT:
  724. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Wait!!\n");*/
  725. if (*pStatus != JPEG_ENC_PROCESS) {
  726. JPEG_WRN("Permission Denied! This process can not access encoder");
  727. return -EFAULT;
  728. }
  729. if (enc_status == 0) {
  730. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  731. *pStatus = 0;
  732. return -EFAULT;
  733. }
  734. if (copy_from_user(&enc_result, (void *)arg, sizeof(JPEG_ENC_DRV_OUT))) {
  735. JPEG_WRN("JPEG Encoder : Copy from user error\n");
  736. return -EFAULT;
  737. }
  738. /* TODO: ENC_DONE in REG_JPEG_ENC_INTERRUPT_STATUS need to set to 0 after read. */
  739. jpeg_enc_wait_timeout = 0xFFFFFF;
  740. #ifdef FPGA_VERSION
  741. do {
  742. _jpeg_enc_int_status = REG_JPEG_ENC_INTERRUPT_STATUS;
  743. jpeg_enc_wait_timeout--;
  744. } while (_jpeg_enc_int_status == 0 && jpeg_enc_wait_timeout > 0);
  745. if (jpeg_enc_wait_timeout == 0)
  746. JPEG_MSG("JPEG Encoder timeout\n");
  747. ret = jpeg_drv_enc_get_result(&file_size);
  748. JPEG_MSG("Result : %d, Size : %u, addres : 0x%x\n", ret, file_size,
  749. ioread32(JPEG_ENC_BASE + 0x120));
  750. if (_jpeg_enc_int_status != 1)
  751. jpeg_drv_enc_dump_reg();
  752. #else
  753. /* set timeout */
  754. timeout_jiff = enc_result.timeout * HZ / 1000;
  755. JPEG_MSG("[JPEGDRV]JPEG Encoder Time Jiffies : %ld\n", timeout_jiff);
  756. if (jpeg_isr_enc_lisr() < 0) {
  757. wait_event_interruptible_timeout(enc_wait_queue, _jpeg_enc_int_status,
  758. timeout_jiff);
  759. JPEG_MSG("[JPEGDRV]JPEG Encoder Wait done !!\n");
  760. } else {
  761. JPEG_MSG("[JPEGDRV]JPEG Encoder already done !!\n");
  762. }
  763. ret = jpeg_drv_enc_get_result(&file_size);
  764. JPEG_MSG("[JPEGDRV]Result : %d, Size : %u!!\n", ret, file_size);
  765. if (ret != 0) {
  766. jpeg_drv_enc_dump_reg();
  767. jpeg_drv_enc_warm_reset();
  768. }
  769. #endif
  770. cycle_count = jpeg_drv_enc_get_cycle_count();
  771. if (copy_to_user(enc_result.fileSize, &file_size, sizeof(unsigned int))) {
  772. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (file size)\n");
  773. return -EFAULT;
  774. }
  775. if (copy_to_user(enc_result.result, &ret, sizeof(unsigned int))) {
  776. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (status)\n");
  777. return -EFAULT;
  778. }
  779. if (copy_to_user(enc_result.cycleCount, &cycle_count, sizeof(unsigned int))) {
  780. JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (cycle)\n");
  781. return -EFAULT;
  782. }
  783. break;
  784. case JPEG_ENC_IOCTL_DEINIT:
  785. /*JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Deinit!!\n");*/
  786. /* copy input parameters */
  787. if (*pStatus != JPEG_ENC_PROCESS) {
  788. JPEG_WRN("Permission Denied! This process can not access encoder");
  789. return -EFAULT;
  790. }
  791. if (enc_status == 0) {
  792. JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
  793. *pStatus = 0;
  794. return -EFAULT;
  795. }
  796. jpeg_drv_enc_deinit();
  797. *pStatus = 0;
  798. break;
  799. case JPEG_ENC_IOCTL_DUMP_REG:
  800. jpeg_drv_enc_dump_reg();
  801. break;
  802. default:
  803. JPEG_MSG("[JPEGDRV]JPEG ENC IOCTL NO THIS COMMAND\n");
  804. }
  805. return 0;
  806. }
  807. /* -------------------------------------------------------------------------- */
  808. /* */
  809. /* -------------------------------------------------------------------------- */
  810. #ifdef CONFIG_COMPAT
  811. static int compat_get_jpeg_dec_ioctl_wait_data(compat_JPEG_DEC_DRV_OUT __user *data32,
  812. JPEG_DEC_DRV_OUT __user *data) {
  813. compat_long_t timeout;
  814. compat_uptr_t result;
  815. int err;
  816. err = get_user(timeout, &data32->timeout);
  817. err |= put_user(timeout, &data->timeout);
  818. err |= get_user(result, &data32->result);
  819. err |= put_user(compat_ptr(result), &data->result);
  820. return err;
  821. }
  822. static int compat_put_jpeg_dec_ioctl_wait_data(compat_JPEG_DEC_DRV_OUT __user *data32,
  823. JPEG_DEC_DRV_OUT __user *data) {
  824. compat_long_t timeout;
  825. /* compat_uptr_t result; */
  826. int err;
  827. err = get_user(timeout, &data->timeout);
  828. err |= put_user(timeout, &data32->timeout);
  829. /* err |= get_user(result, &data->result); */
  830. /* err |= put_user(result, &data32->result); */
  831. return err;
  832. }
  833. static int compat_get_jpeg_dec_ioctl_chksum_data(compat_JpegDrvDecResult __user *data32,
  834. JpegDrvDecResult __user *data) {
  835. compat_uptr_t pChksum;
  836. int err;
  837. err = get_user(pChksum, &data32->pChksum);
  838. err |= put_user(compat_ptr(pChksum), &data->pChksum);
  839. return err;
  840. }
  841. static int compat_put_jpeg_dec_ioctl_chksum_data(compat_JpegDrvDecResult __user *data32,
  842. JpegDrvDecResult __user *data) {
  843. /* compat_uptr_t pChksum; */
  844. /* int err; */
  845. /* err = get_user(pChksum, &data->pChksum); */
  846. /* err |= put_user(pChksum, &data32->pChksum); */
  847. return 0;
  848. }
  849. static int compat_get_jpeg_enc_ioctl_wait_data(compat_JPEG_ENC_DRV_OUT __user *data32,
  850. JPEG_ENC_DRV_OUT __user *data) {
  851. compat_long_t timeout;
  852. compat_uptr_t fileSize;
  853. compat_uptr_t result;
  854. compat_uptr_t cycleCount;
  855. int err;
  856. err = get_user(timeout, &data32->timeout);
  857. err |= put_user(timeout, &data->timeout);
  858. err |= get_user(fileSize, &data32->fileSize);
  859. err |= put_user(compat_ptr(fileSize), &data->fileSize);
  860. err |= get_user(result, &data32->result);
  861. err |= put_user(compat_ptr(result), &data->result);
  862. err |= get_user(cycleCount, &data32->cycleCount);
  863. err |= put_user(compat_ptr(cycleCount), &data->cycleCount);
  864. return err;
  865. }
  866. static int compat_put_jpeg_enc_ioctl_wait_data(compat_JPEG_ENC_DRV_OUT __user *data32,
  867. JPEG_ENC_DRV_OUT __user *data) {
  868. compat_long_t timeout;
  869. /* compat_uptr_t fileSize; */
  870. /* compat_uptr_t result; */
  871. /* compat_uptr_t cycleCount; */
  872. int err;
  873. err = get_user(timeout, &data->timeout);
  874. err |= put_user(timeout, &data32->timeout);
  875. /* err |= get_user(fileSize, &data->fileSize); */
  876. /* err |= put_user(fileSize, &data32->fileSize); */
  877. /* err |= get_user(result, &data->result); */
  878. /* err |= put_user(result, &data32->result); */
  879. /* err |= get_user(cycleCount, &data->cycleCount); */
  880. /* err |= put_user(cycleCount, &data32->cycleCount); */
  881. return err;
  882. }
  883. static long compat_jpeg_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  884. {
  885. long ret;
  886. if (!filp->f_op || !filp->f_op->unlocked_ioctl)
  887. return -ENOTTY;
  888. switch (cmd) {
  889. case COMPAT_JPEG_DEC_IOCTL_WAIT:
  890. {
  891. compat_JPEG_DEC_DRV_OUT __user *data32;
  892. JPEG_DEC_DRV_OUT __user *data;
  893. int err;
  894. data32 = compat_ptr(arg);
  895. data = compat_alloc_user_space(sizeof(*data));
  896. if (data == NULL)
  897. return -EFAULT;
  898. err = compat_get_jpeg_dec_ioctl_wait_data(data32, data);
  899. if (err)
  900. return err;
  901. ret =
  902. filp->f_op->unlocked_ioctl(filp, JPEG_DEC_IOCTL_WAIT,
  903. (unsigned long)data);
  904. err = compat_put_jpeg_dec_ioctl_wait_data(data32, data);
  905. return ret ? ret : err;
  906. } case COMPAT_JPEG_DEC_IOCTL_CHKSUM: {
  907. compat_JpegDrvDecResult __user *data32;
  908. JpegDrvDecResult __user *data;
  909. int err;
  910. data32 = compat_ptr(arg);
  911. data = compat_alloc_user_space(sizeof(*data));
  912. if (data == NULL)
  913. return -EFAULT;
  914. err = compat_get_jpeg_dec_ioctl_chksum_data(data32, data);
  915. if (err)
  916. return err;
  917. ret =
  918. filp->f_op->unlocked_ioctl(filp, JPEG_DEC_IOCTL_CHKSUM,
  919. (unsigned long)data);
  920. err = compat_put_jpeg_dec_ioctl_chksum_data(data32, data);
  921. return ret ? ret : err;
  922. }
  923. case COMPAT_JPEG_ENC_IOCTL_WAIT:
  924. {
  925. compat_JPEG_ENC_DRV_OUT __user *data32;
  926. JPEG_ENC_DRV_OUT __user *data;
  927. int err;
  928. data32 = compat_ptr(arg);
  929. data = compat_alloc_user_space(sizeof(*data));
  930. if (data == NULL)
  931. return -EFAULT;
  932. err = compat_get_jpeg_enc_ioctl_wait_data(data32, data);
  933. if (err)
  934. return err;
  935. ret =
  936. filp->f_op->unlocked_ioctl(filp, JPEG_ENC_IOCTL_WAIT,
  937. (unsigned long)data);
  938. err = compat_put_jpeg_enc_ioctl_wait_data(data32, data);
  939. return ret ? ret : err;
  940. }
  941. case JPEG_DEC_IOCTL_INIT:
  942. case JPEG_DEC_IOCTL_START:
  943. case JPEG_DEC_IOCTL_DEINIT:
  944. case JPEG_DEC_IOCTL_DUMP_REG:
  945. case JPEG_ENC_IOCTL_INIT:
  946. case JPEG_ENC_IOCTL_DEINIT:
  947. case JPEG_ENC_IOCTL_START:
  948. return filp->f_op->unlocked_ioctl(filp, cmd, arg);
  949. case JPEG_DEC_IOCTL_CONFIG:
  950. case JPEG_DEC_IOCTL_RESUME:
  951. case JPEG_DEC_IOCTL_FLUSH_CMDQ:
  952. case JPEG_ENC_IOCTL_CONFIG:
  953. return filp->f_op->unlocked_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  954. default:
  955. return -ENOIOCTLCMD;
  956. }
  957. }
  958. #endif
  959. /* static int jpeg_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) */
  960. static long jpeg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  961. {
  962. switch (cmd) {
  963. #ifdef JPEG_DEC_DRIVER
  964. case JPEG_DEC_IOCTL_INIT:
  965. case JPEG_DEC_IOCTL_CONFIG:
  966. case JPEG_DEC_IOCTL_START:
  967. case JPEG_DEC_IOCTL_RESUME:
  968. case JPEG_DEC_IOCTL_WAIT:
  969. case JPEG_DEC_IOCTL_DEINIT:
  970. case JPEG_DEC_IOCTL_DUMP_REG:
  971. case JPEG_DEC_IOCTL_FLUSH_CMDQ:
  972. return jpeg_dec_ioctl(cmd, arg, file);
  973. #endif
  974. case JPEG_ENC_IOCTL_INIT:
  975. case JPEG_ENC_IOCTL_CONFIG:
  976. case JPEG_ENC_IOCTL_WAIT:
  977. case JPEG_ENC_IOCTL_DEINIT:
  978. case JPEG_ENC_IOCTL_START:
  979. return jpeg_enc_ioctl(cmd, arg, file);
  980. default:
  981. break;
  982. }
  983. return -EINVAL;
  984. }
  985. static int jpeg_open(struct inode *inode, struct file *file)
  986. {
  987. unsigned int *pStatus;
  988. /* Allocate and initialize private data */
  989. file->private_data = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
  990. if (NULL == file->private_data) {
  991. JPEG_WRN("Not enough entry for JPEG open operation\n");
  992. return -ENOMEM;
  993. }
  994. pStatus = (unsigned int *)file->private_data;
  995. *pStatus = 0;
  996. return 0;
  997. }
  998. static ssize_t jpeg_read(struct file *file, char __user *data, size_t len, loff_t *ppos)
  999. {
  1000. JPEG_MSG("jpeg driver read\n");
  1001. return 0;
  1002. }
  1003. static int jpeg_release(struct inode *inode, struct file *file)
  1004. {
  1005. if (NULL != file->private_data) {
  1006. kfree(file->private_data);
  1007. file->private_data = NULL;
  1008. }
  1009. return 0;
  1010. }
  1011. static int jpeg_flush(struct file *a_pstFile, fl_owner_t a_id)
  1012. {
  1013. unsigned int *pStatus;
  1014. pStatus = (unsigned int *)a_pstFile->private_data;
  1015. if (NULL == pStatus) {
  1016. JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
  1017. return -EFAULT;
  1018. }
  1019. if (*pStatus == JPEG_ENC_PROCESS) {
  1020. if (enc_status != 0) {
  1021. JPEG_WRN("Error! Enable error handling for jpeg encoder");
  1022. jpeg_drv_enc_deinit();
  1023. }
  1024. }
  1025. #ifdef JPEG_DEC_DRIVER
  1026. else if (*pStatus == JPEG_DEC_PROCESS) {
  1027. if (dec_status != 0) {
  1028. JPEG_WRN("Error! Enable error handling for jpeg decoder");
  1029. jpeg_drv_dec_deinit();
  1030. }
  1031. }
  1032. #endif
  1033. return 0;
  1034. }
  1035. /* Kernel interface */
  1036. static struct file_operations const jpeg_fops = {
  1037. .owner = THIS_MODULE,
  1038. /* .ioctl = jpeg_ioctl, */
  1039. .unlocked_ioctl = jpeg_unlocked_ioctl,
  1040. #ifdef CONFIG_COMPAT
  1041. .compat_ioctl = compat_jpeg_ioctl,
  1042. #endif
  1043. .open = jpeg_open,
  1044. .release = jpeg_release,
  1045. .flush = jpeg_flush,
  1046. .read = jpeg_read,
  1047. };
  1048. const long jpeg_dev_get_encoder_base_VA(void)
  1049. {
  1050. return gJpegqDev.encRegBaseVA;
  1051. }
  1052. const long jpeg_dev_get_decoder_base_VA(void)
  1053. {
  1054. return gJpegqDev.decRegBaseVA;
  1055. }
  1056. static int jpeg_probe(struct platform_device *pdev)
  1057. {
  1058. #ifdef CONFIG_OF
  1059. int new_count;
  1060. struct JpegDeviceStruct *jpegDev;
  1061. struct device_node *node = NULL;
  1062. new_count = nrJpegDevs + 1;
  1063. gJpegqDevs = krealloc(gJpegqDevs, sizeof(struct JpegDeviceStruct) * new_count, GFP_KERNEL);
  1064. if (!gJpegqDevs) {
  1065. dev_err(&pdev->dev, "Unable to allocate cam_isp_devs\n");
  1066. return -ENOMEM;
  1067. }
  1068. jpegDev = &(gJpegqDevs[nrJpegDevs]);
  1069. jpegDev->pDev = &pdev->dev;
  1070. memset(&gJpegqDev, 0x0, sizeof(JpegDeviceStruct));
  1071. node = of_find_compatible_node(NULL, NULL, "mediatek,jpgenc");
  1072. jpegDev->encRegBaseVA = (unsigned long)of_iomap(node, 0);
  1073. jpegDev->encIrqId = irq_of_parse_and_map(node, 0);
  1074. #ifdef CONFIG_MTK_CLKMGR
  1075. #else
  1076. #ifdef JPEG_PM_DOMAIN_ENABLE
  1077. pjenc_dev = pdev;
  1078. #else
  1079. /* venc-mtcmos lead to disp power scpsys SCP_SYS_DISP */
  1080. gJpegClk.clk_disp_mtcmos = of_clk_get_by_name(node, "disp-mtcmos");
  1081. if (IS_ERR(gJpegClk.clk_disp_mtcmos))
  1082. JPEG_ERR("get dispMTCMOS clk error!");
  1083. /* venc-mtcmos lead to venc power scpsys SCP_SYS_VEN */
  1084. gJpegClk.clk_venc_mtcmos = of_clk_get_by_name(node, "venc-mtcmos");
  1085. if (IS_ERR(gJpegClk.clk_venc_mtcmos))
  1086. JPEG_ERR("get vencMTCMOS clk error!");
  1087. gJpegClk.clk_disp_smi = of_clk_get_by_name(node, "disp-smi");
  1088. if (IS_ERR(gJpegClk.clk_disp_smi))
  1089. JPEG_ERR("get dispMI clk error!");
  1090. gJpegClk.clk_venc_larb = of_clk_get_by_name(node, "venc-larb");
  1091. if (IS_ERR(gJpegClk.clk_venc_larb))
  1092. JPEG_ERR("get venc-larb clk error!");
  1093. #endif
  1094. gJpegClk.clk_venc_jpgEnc = of_clk_get_by_name(node, "venc-jpgenc");
  1095. if (IS_ERR(gJpegClk.clk_venc_jpgEnc))
  1096. JPEG_ERR("get jpgEnc clk error!");
  1097. #endif
  1098. node = of_find_compatible_node(NULL, NULL, "mediatek,jpgdec");
  1099. jpegDev->decRegBaseVA = (unsigned long)of_iomap(node, 0);
  1100. jpegDev->decIrqId = irq_of_parse_and_map(node, 0);
  1101. #ifdef CONFIG_MTK_CLKMGR
  1102. #else
  1103. gJpegClk.clk_venc_jpgDec = of_clk_get_by_name(node, "venc-jpgdec");
  1104. if (IS_ERR(gJpegClk.clk_venc_jpgDec))
  1105. JPEG_ERR("get jpgDec clk error!");
  1106. #endif
  1107. gJpegqDev = *jpegDev;
  1108. #else
  1109. gJpegqDev.encRegBaseVA = (0L | 0xF7003000);
  1110. gJpegqDev.decRegBaseVA = (0L | 0xF7004000);
  1111. gJpegqDev.encIrqId = JPGENC_IRQ_BIT_ID;
  1112. gJpegqDev.decIrqId = JPGDEC_IRQ_BIT_ID;
  1113. gJpegqDev.pDev = &pdev->dev;
  1114. #endif
  1115. {
  1116. #ifdef JPEG_DEV
  1117. int ret;
  1118. struct class_device *class_dev = NULL;
  1119. JPEG_MSG("-------------jpeg driver probe-------\n");
  1120. ret = alloc_chrdev_region(&jenc_devno, 0, 1, JPEG_DEVNAME);
  1121. if (ret)
  1122. JPEG_ERR("Error: Can't Get Major number for JPEG Device\n");
  1123. else
  1124. JPEG_MSG("Get JPEG Device Major number (%d)\n", jenc_devno);
  1125. jenc_cdev = cdev_alloc();
  1126. jenc_cdev->owner = THIS_MODULE;
  1127. jenc_cdev->ops = &jpeg_fops;
  1128. ret = cdev_add(jenc_cdev, jenc_devno, 1);
  1129. jenc_class = class_create(THIS_MODULE, JPEG_DEVNAME);
  1130. class_dev =
  1131. (struct class_device *)device_create(jenc_class, NULL, jenc_devno, NULL, JPEG_DEVNAME);
  1132. #else
  1133. proc_create("mtk_jpeg", 0, NULL, &jpeg_fops);
  1134. #endif
  1135. }
  1136. spin_lock_init(&jpeg_enc_lock);
  1137. /* initial codec, register codec ISR */
  1138. enc_status = 0;
  1139. _jpeg_enc_int_status = 0;
  1140. #ifdef JPEG_DEC_DRIVER
  1141. spin_lock_init(&jpeg_dec_lock);
  1142. dec_status = 0;
  1143. _jpeg_dec_int_status = 0;
  1144. _jpeg_dec_mode = 0;
  1145. #endif
  1146. #ifndef FPGA_VERSION
  1147. #ifdef JPEG_DEC_DRIVER
  1148. init_waitqueue_head(&dec_wait_queue);
  1149. #endif
  1150. init_waitqueue_head(&enc_wait_queue);
  1151. /* mt6575_irq_set_sens(MT6575_JPEG_CODEC_IRQ_ID, MT65xx_LEVEL_SENSITIVE); */
  1152. /* mt6575_irq_set_polarity(MT6575_JPEG_CODEC_IRQ_ID, MT65xx_POLARITY_LOW); */
  1153. /* mt6575_irq_unmask(MT6575_JPEG_CODEC_IRQ_ID); */
  1154. JPEG_MSG("request JPEG Encoder IRQ\n");
  1155. enable_irq(gJpegqDev.encIrqId);
  1156. if (request_irq(gJpegqDev.encIrqId, jpeg_drv_enc_isr, IRQF_TRIGGER_LOW, "jpeg_enc_driver", NULL))
  1157. JPEG_ERR("JPEG ENC Driver request irq failed\n");
  1158. #ifdef JPEG_DEC_DRIVER
  1159. enable_irq(gJpegqDev.decIrqId);
  1160. JPEG_MSG("request JPEG Decoder IRQ\n");
  1161. if (request_irq(gJpegqDev.decIrqId, jpeg_drv_dec_isr, IRQF_TRIGGER_FALLING, "jpeg_dec_driver", NULL))
  1162. JPEG_ERR("JPEG DEC Driver request irq failed\n");
  1163. #endif
  1164. #endif
  1165. JPEG_MSG("JPEG Probe Done\n");
  1166. #ifdef JPEG_DEV
  1167. /* NOT_REFERENCED(class_dev); */
  1168. #endif
  1169. return 0;
  1170. }
  1171. static int jpeg_remove(struct platform_device *pdev)
  1172. {
  1173. JPEG_MSG("JPEG Codec remove\n");
  1174. /* unregister_chrdev(JPEGDEC_MAJOR, JPEGDEC_DEVNAME); */
  1175. #ifndef FPGA_VERSION
  1176. free_irq(gJpegqDev.encIrqId, NULL);
  1177. #ifdef JPEG_DEC_DRIVER
  1178. free_irq(gJpegqDev.decIrqId, NULL);
  1179. #endif
  1180. #endif
  1181. JPEG_MSG("Done\n");
  1182. return 0;
  1183. }
  1184. static void jpeg_shutdown(struct platform_device *pdev)
  1185. {
  1186. JPEG_MSG("JPEG Codec shutdown\n");
  1187. /* Nothing yet */
  1188. }
  1189. /* PM suspend */
  1190. static int jpeg_suspend(struct platform_device *pdev, pm_message_t mesg)
  1191. {
  1192. #ifdef JPEG_DEC_DRIVER
  1193. jpeg_drv_dec_deinit();
  1194. #endif
  1195. jpeg_drv_enc_deinit();
  1196. return 0;
  1197. }
  1198. /* PM resume */
  1199. static int jpeg_resume(struct platform_device *pdev)
  1200. {
  1201. return 0;
  1202. }
  1203. static int jpeg_pm_suspend(struct device *pDevice)
  1204. {
  1205. struct platform_device *pdev = to_platform_device(pDevice);
  1206. BUG_ON(pdev == NULL);
  1207. return jpeg_suspend(pdev, PMSG_SUSPEND);
  1208. }
  1209. static int jpeg_pm_resume(struct device *pDevice)
  1210. {
  1211. struct platform_device *pdev = to_platform_device(pDevice);
  1212. BUG_ON(pdev == NULL);
  1213. return jpeg_resume(pdev);
  1214. }
  1215. static int jpeg_pm_restore_noirq(struct device *pDevice)
  1216. {
  1217. return 0;
  1218. }
  1219. static struct dev_pm_ops const jpeg_pm_ops = {
  1220. .suspend = jpeg_pm_suspend,
  1221. .resume = jpeg_pm_resume,
  1222. .freeze = NULL,
  1223. .thaw = NULL,
  1224. .poweroff = NULL,
  1225. .restore = NULL,
  1226. .restore_noirq = jpeg_pm_restore_noirq,
  1227. };
  1228. static struct platform_driver jpeg_driver = {
  1229. .probe = jpeg_probe,
  1230. .remove = jpeg_remove,
  1231. .shutdown = jpeg_shutdown,
  1232. .suspend = jpeg_suspend,
  1233. .resume = jpeg_resume,
  1234. .driver = {
  1235. .name = JPEG_DEVNAME,
  1236. .pm = &jpeg_pm_ops,
  1237. #ifdef CONFIG_OF
  1238. .of_match_table = jenc_of_ids,
  1239. #endif
  1240. },
  1241. };
  1242. static void jpeg_device_release(struct device *dev)
  1243. {
  1244. /* Nothing to release? */
  1245. }
  1246. static u64 jpegdec_dmamask = ~(u32) 0;
  1247. static struct platform_device jpeg_device = {
  1248. .name = JPEG_DEVNAME,
  1249. .id = 0,
  1250. .dev = {
  1251. .release = jpeg_device_release,
  1252. .dma_mask = &jpegdec_dmamask,
  1253. .coherent_dma_mask = 0xffffffff,
  1254. },
  1255. .num_resources = 0,
  1256. };
  1257. #if 0 /*def JPEG_PM_DOMAIN_ENABLE*/
  1258. /* Kernel interface */
  1259. static struct file_operations const jdec_fops = {
  1260. .owner = THIS_MODULE,
  1261. /* .ioctl = jpeg_ioctl, */
  1262. .unlocked_ioctl = NULL,
  1263. #ifdef CONFIG_COMPAT
  1264. .compat_ioctl = NULL,
  1265. #endif
  1266. .open = NULL,
  1267. .release = NULL,
  1268. .flush = NULL,
  1269. .read = NULL,
  1270. };
  1271. static int jdec_probe(struct platform_device *pdev)
  1272. {
  1273. #ifdef JPEG_DEV
  1274. int ret;
  1275. struct class_device *class_dev = NULL;
  1276. #endif
  1277. JPEG_MSG("+jdec_probe\n");
  1278. pjdec_dev = pdev;
  1279. #ifdef JPEG_DEV
  1280. ret = alloc_chrdev_region(&jdec_devno, 0, 1, JDEC_DEVNAME);
  1281. if (ret)
  1282. JPEG_MSG("Error: Can't Get Major number for %s Device\n", JDEC_DEVNAME);
  1283. else
  1284. JPEG_MSG("Get %s Device Major number (%d)\n", JDEC_DEVNAME, jdec_devno);
  1285. jdec_cdev = cdev_alloc();
  1286. jdec_cdev->owner = THIS_MODULE;
  1287. jdec_cdev->ops = NULL;
  1288. ret = cdev_add(jdec_cdev, jdec_devno, 1);
  1289. jdec_class = class_create(THIS_MODULE, JDEC_DEVNAME);
  1290. class_dev =
  1291. (struct class_device *)device_create(jdec_class, NULL, jdec_devno, NULL, JDEC_DEVNAME);
  1292. #else
  1293. proc_create(JDEC_DEVNAME, 0, NULL, &jdec_fops);
  1294. #endif
  1295. /* venc_power_on(); */
  1296. return 0;
  1297. }
  1298. static struct platform_driver jdec_driver = {
  1299. .probe = jdec_probe,
  1300. /*.remove = vcodec_venc_remove, */
  1301. .driver = {
  1302. .name = JDEC_DEVNAME,
  1303. .owner = THIS_MODULE,
  1304. .of_match_table = jdec_of_ids,
  1305. }
  1306. };
  1307. #endif
  1308. static int __init jpeg_init(void)
  1309. {
  1310. int ret;
  1311. JPEG_MSG("JPEG Codec initialize\n");
  1312. #if 0
  1313. JPEG_MSG("Register the JPEG Codec device\n");
  1314. if (platform_device_register(&jpeg_device)) {
  1315. JPEG_ERR("failed to register jpeg codec device\n");
  1316. ret = -ENODEV;
  1317. return ret;
  1318. }
  1319. #endif
  1320. JPEG_MSG("Register the JPEG Codec driver\n");
  1321. if (platform_driver_register(&jpeg_driver)) {
  1322. JPEG_ERR("failed to register jpeg codec driver\n");
  1323. platform_device_unregister(&jpeg_device);
  1324. ret = -ENODEV;
  1325. return ret;
  1326. }
  1327. #if 0 /*def JPEG_PM_DOMAIN_ENABLE*/
  1328. if (platform_driver_register(&jdec_driver)) {
  1329. JPEG_ERR("failed to register jdec_driver codec driver\n");
  1330. platform_device_unregister(pjdec_dev);
  1331. ret = -ENODEV;
  1332. return ret;
  1333. }
  1334. #endif
  1335. cmdqCoreRegisterCB(CMDQ_GROUP_JPEG,
  1336. cmdqJpegClockOn, cmdqJpegDumpInfo, cmdqJpegResetEng, cmdqJpegClockOff);
  1337. return 0;
  1338. }
  1339. static void __exit jpeg_exit(void)
  1340. {
  1341. JPEG_MSG("jpeg_exit +\n");
  1342. #ifdef JPEG_DEV
  1343. cdev_del(jenc_cdev);
  1344. unregister_chrdev_region(jenc_devno, 1);
  1345. device_destroy(jenc_class, jenc_devno);
  1346. class_destroy(jenc_class);
  1347. #else
  1348. remove_proc_entry("mtk_jpeg", NULL);
  1349. #endif
  1350. cmdqCoreRegisterCB(CMDQ_GROUP_JPEG, NULL, NULL, NULL, NULL);
  1351. /* JPEG_MSG("Unregistering driver\n"); */
  1352. platform_driver_unregister(&jpeg_driver);
  1353. platform_device_unregister(&jpeg_device);
  1354. #ifdef JPEG_PM_DOMAIN_ENABLE
  1355. #ifdef JPEG_DEV
  1356. /*cdev_del(jdec_cdev);
  1357. unregister_chrdev_region(jdec_devno, 1);
  1358. device_destroy(jdec_class, jdec_devno);
  1359. class_destroy(jdec_class);*/
  1360. #else
  1361. remove_proc_entry("mtk_jenc", NULL);
  1362. #endif
  1363. /*platform_driver_unregister(&jdec_driver);*/
  1364. platform_device_unregister(pjenc_dev);
  1365. JPEG_MSG("jpeg_exit jdec remove\n");
  1366. #endif
  1367. JPEG_MSG("jpeg_exit -\n");
  1368. }
  1369. module_init(jpeg_init);
  1370. module_exit(jpeg_exit);
  1371. MODULE_AUTHOR("Hao-Ting Huang <otis.huang@mediatek.com>");
  1372. MODULE_DESCRIPTION("MT6582 JPEG Codec Driver");
  1373. MODULE_LICENSE("GPL");