jpegdec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * AMLOGIC HW JPEG decoder driver.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the named License,
  7. * or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
  17. *
  18. * Author: Tim Yao <timyao@amlogic.com>
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/device.h>
  28. #include <linux/mm.h>
  29. #include <linux/major.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mutex.h>
  33. #include <linux/amports/jpegdec.h>
  34. #include <linux/amports/canvas.h>
  35. #include <asm/uaccess.h>
  36. #include <mach/am_regs.h>
  37. #include <plat/io.h>
  38. #include "amvdec.h"
  39. #include "jpegdec_mc.h"
  40. #include "streambuf.h"
  41. #include "vdec_reg.h"
  42. #define DEVICE_NAME "amjpegdec"
  43. #define DRIVER_NAME "amjpegdec"
  44. #define MODULE_NAME "amjpegdec"
  45. //#define DEBUG
  46. #define JPEGDEC_OUTPUT_CANVAS_Y 58U
  47. #define JPEGDEC_OUTPUT_CANVAS_U 59U
  48. #define JPEGDEC_OUTPUT_CANVAS_V 60U
  49. #define JPEGDEC_OUTPUT_CANVAS \
  50. ((JPEGDEC_OUTPUT_CANVAS_V << 16) | (JPEGDEC_OUTPUT_CANVAS_U << 8) | (JPEGDEC_OUTPUT_CANVAS_Y))
  51. #define PSCALE_CANVAS_Y 61U
  52. #define PSCALE_CANVAS_U 62U
  53. #define PSCALE_CANVAS_V 63U
  54. #define PSCALE_CANVAS \
  55. ((PSCALE_CANVAS_V << 16) | (PSCALE_CANVAS_U << 8) | (PSCALE_CANVAS_Y))
  56. #define JPEG_DECODE_START AV_SCRATCH_0
  57. #define JPEG_INFO AV_SCRATCH_0
  58. #define JPEG_PIC_WIDTH AV_SCRATCH_1
  59. #define JPEG_PIC_HEIGHT AV_SCRATCH_2
  60. #define JPEG_DECODE_PARAMETER AV_SCRATCH_3
  61. #define JPEG_SCREEN_OFFSET_Y AV_SCRATCH_4
  62. #define JPEG_SCREEN_OFFSET_X AV_SCRATCH_5
  63. #define JPEG_MCU_CROP_HSTART PSCALE_PICI_W
  64. #define JPEG_MCU_CROP_HEND PSCALE_PICI_H
  65. #define JPEG_MCU_CROP_VSTART PSCALE_PICO_W
  66. #define JPEG_MCU_CROP_VEND PSCALE_PICO_H
  67. #ifdef DEBUG
  68. #define pr_dbg(fmt, args...) printk(KERN_DEBUG "amjpegdec: " fmt, ## args)
  69. #else
  70. #define pr_dbg(fmt, args...)
  71. #endif
  72. #define pr_error(fmt, args...) printk(KERN_ERR "amjpegdec: " fmt, ## args)
  73. typedef struct {
  74. jpegdec_config_t conf;
  75. jpegdec_info_t info;
  76. unsigned state;
  77. } jpegdec_t;
  78. static struct class *amjpegdec_class;
  79. static struct device *amjpegdec_dev;
  80. static const char jpegdec_id[] = "amjpegdec";
  81. static DEFINE_MUTEX(jpegdec_module_mutex);
  82. static jpegdec_t *dec = NULL;
  83. static unsigned long pbufAddr;
  84. static unsigned long pbufSize;
  85. static jpegdec_mem_info_t jegdec_mem_info;
  86. static irqreturn_t jpegdec_isr(int irq, void *dev_id)
  87. {
  88. WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
  89. if ((dec->state & JPEGDEC_STAT_INFO_READY) == 0) {
  90. dec->info.width = READ_VREG(JPEG_PIC_WIDTH);
  91. dec->info.height = READ_VREG(JPEG_PIC_HEIGHT);
  92. dec->info.comp_num = READ_VREG(JPEG_INFO) >> 1;
  93. pr_dbg("ucode report picture size %dx%d, comp_num %d\n",
  94. dec->info.width, dec->info.height, dec->info.comp_num);
  95. if ((dec->info.comp_num != 1) && (dec->info.comp_num != 3)) {
  96. dec->state |= JPEGDEC_STAT_INFO_READY | JPEGDEC_STAT_UNSUPPORT;
  97. } else {
  98. dec->state |= JPEGDEC_STAT_INFO_READY | JPEGDEC_STAT_WAIT_DECCONFIG;
  99. }
  100. } else {
  101. unsigned r = READ_VREG(JPEG_DECODE_START);
  102. dec->state &= ~JPEGDEC_STAT_WAIT_DATA;
  103. if (r == 2) {
  104. pr_dbg("ucode report decoding finished\n");
  105. dec->state |= JPEGDEC_STAT_DONE;
  106. } else if ((r == 4) || (r == 6)) {
  107. dec->state |= JPEGDEC_STAT_ERROR;
  108. } else {
  109. pr_error("jpegdec_isr ucode unknow status 0x%x\n", r);
  110. }
  111. }
  112. return IRQ_HANDLED;
  113. }
  114. static int _init_dec(jpegdec_t *d)
  115. {
  116. int r;
  117. amvdec_enable();
  118. #ifdef CONFIG_ARCH_MESON6
  119. WRITE_VREG(DOS_SW_RESET0, (1<<11));
  120. WRITE_VREG(DOS_SW_RESET0, 0);
  121. #else
  122. WRITE_MPEG_REG(RESET0_REGISTER, RESET_VCPU);
  123. #endif
  124. WRITE_VREG(ASSIST_AMR1_INT0, 0x1);
  125. WRITE_VREG(ASSIST_AMR1_INT1, 0xf);
  126. WRITE_VREG(ASSIST_AMR1_INT2, 0x8);
  127. WRITE_VREG(ASSIST_AMR1_INT3, 0xa);
  128. WRITE_VREG(ASSIST_AMR1_INT4, 0x3);
  129. WRITE_VREG(ASSIST_AMR1_INT5, 0x9);
  130. WRITE_VREG(ASSIST_AMR1_INT6, 0x4);
  131. #ifdef CONFIG_ARCH_MESON6
  132. WRITE_VREG(DOS_SW_RESET0, (1<<11) | (1<<7) | (1<<6));
  133. WRITE_VREG(DOS_SW_RESET0, 0);
  134. #else
  135. WRITE_MPEG_REG(RESET0_REGISTER, RESET_VCPU | RESET_IQIDCT | RESET_MC);
  136. #endif
  137. if (amvdec_loadmc(jpegdec_mc) < 0) {
  138. amvdec_disable();
  139. pr_error("jpegdec ucode loading failed.\n");
  140. return -EBUSY;
  141. }
  142. #ifdef CONFIG_ARCH_MESON6
  143. WRITE_VREG(DOS_SW_RESET0, (1<<11) | (1<<10) | (1<<7) | (1<<6));
  144. WRITE_VREG(DOS_SW_RESET0, 0);
  145. #else
  146. WRITE_MPEG_REG(RESET0_REGISTER, RESET_VCPU | RESET_IQIDCT | RESET_MC);
  147. WRITE_MPEG_REG(RESET2_REGISTER, RESET_PSCALE);
  148. #endif
  149. WRITE_VREG(PSCALE_RST, 0x7);
  150. WRITE_VREG(PSCALE_RST, 0x0);
  151. WRITE_VREG(JPEG_PIC_WIDTH, 0);
  152. WRITE_VREG(JPEG_PIC_HEIGHT, 0);
  153. WRITE_VREG(ASSIST_MBOX1_MASK, 1);
  154. WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
  155. r = request_irq(INT_MAILBOX_1A, jpegdec_isr,
  156. IRQF_SHARED, "jpegdec-irq", (void *)jpegdec_id);
  157. if (r) {
  158. amvdec_disable();
  159. pr_error("jpegdec irq register error.\n");
  160. return -ENOENT;
  161. }
  162. d->state = JPEGDEC_STAT_WAIT_DATA | JPEGDEC_STAT_WAIT_INFOCONFIG;
  163. return 0;
  164. }
  165. static void _init_scaler(unsigned horz_step, unsigned vert_step)
  166. {
  167. int i;
  168. /* 2 point bilinear */
  169. static const unsigned filt_coef[] = {
  170. 0x00800000, 0x007e0200, 0x007c0400, 0x007a0600,
  171. 0x00780800, 0x00760a00, 0x00740c00, 0x00720e00,
  172. 0x00701000, 0x006e1200, 0x006c1400, 0x006a1600,
  173. 0x00681800, 0x00661a00, 0x00641c00, 0x00621e00,
  174. 0x00602000, 0x005e2200, 0x005c2400, 0x005a2600,
  175. 0x00582800, 0x00562a00, 0x00542c00, 0x00522e00,
  176. 0x00503000, 0x004e3200, 0x004c3400, 0x004a3600,
  177. 0x00483800, 0x00463a00, 0x00443c00, 0x00423e00,
  178. 0x00404000
  179. };
  180. WRITE_VREG(PSCALE_CTRL, 0xc000);
  181. /* write filter coefs */
  182. WRITE_VREG(PSCALE_BMEM_ADDR, 0);
  183. for (i = 0; i < 33; i++) {
  184. WRITE_VREG(PSCALE_BMEM_DAT, 0);
  185. WRITE_VREG(PSCALE_BMEM_DAT, filt_coef[i]);
  186. }
  187. #define BM_HORZ_Y_PHASE_STEP_OFFSET (36*2+1)
  188. #define BM_HORZ_C_PHASE_STEP_OFFSET (40*2+1)
  189. #define BM_VERT_Y_PHASE_STEP_OFFSET (38*2+1)
  190. #define BM_VERT_C_PHASE_STEP_OFFSET (42*2+1)
  191. #define BM_HORZ_Y_INI_INFO_OFFSET (37*2)
  192. #define BM_HORZ_C_INI_INFO_OFFSET (41*2)
  193. #define BM_VERT_Y_INI_INFO_OFFSET (39*2)
  194. #define BM_VERT_C_INI_INFO_OFFSET (43*2)
  195. /* Y horizontal initial info */
  196. WRITE_VREG(PSCALE_BMEM_ADDR, BM_HORZ_Y_INI_INFO_OFFSET);
  197. WRITE_VREG(PSCALE_BMEM_DAT, 0x0008);
  198. WRITE_VREG(PSCALE_BMEM_DAT, 0x60000000);
  199. /* C horizontal initial info */
  200. WRITE_VREG(PSCALE_BMEM_ADDR, BM_HORZ_C_INI_INFO_OFFSET);
  201. WRITE_VREG(PSCALE_BMEM_DAT, 0x0008);
  202. WRITE_VREG(PSCALE_BMEM_DAT, 0x60000000);
  203. /* Y vertical initial info */
  204. WRITE_VREG(PSCALE_BMEM_ADDR, BM_VERT_Y_INI_INFO_OFFSET);
  205. WRITE_VREG(PSCALE_BMEM_DAT, 0x0008);
  206. WRITE_VREG(PSCALE_BMEM_DAT, 0x60000000);
  207. /* C vertical initial info */
  208. WRITE_VREG(PSCALE_BMEM_ADDR, BM_VERT_C_INI_INFO_OFFSET);
  209. WRITE_VREG(PSCALE_BMEM_DAT, 0x0008);
  210. WRITE_VREG(PSCALE_BMEM_DAT, 0x60000000);
  211. /* Y horizontal phase step */
  212. WRITE_VREG(PSCALE_BMEM_ADDR, BM_HORZ_Y_PHASE_STEP_OFFSET);
  213. WRITE_VREG(PSCALE_BMEM_DAT, horz_step);
  214. /* C horizontal phase step */
  215. WRITE_VREG(PSCALE_BMEM_ADDR, BM_HORZ_C_PHASE_STEP_OFFSET);
  216. WRITE_VREG(PSCALE_BMEM_DAT, horz_step);
  217. /* Y vertical phase step */
  218. WRITE_VREG(PSCALE_BMEM_ADDR, BM_VERT_Y_PHASE_STEP_OFFSET);
  219. WRITE_VREG(PSCALE_BMEM_DAT, vert_step);
  220. /* C vertical phase step */
  221. WRITE_VREG(PSCALE_BMEM_ADDR, BM_VERT_C_PHASE_STEP_OFFSET);
  222. WRITE_VREG(PSCALE_BMEM_DAT, vert_step);
  223. }
  224. static void _dec_run(void)
  225. {
  226. static unsigned filt0_table[9] = {0, 0, 1, 1, 2, 2, 2, 2, 2};
  227. static unsigned addr_mode_tab[4] = {0x0, 0xd, 0xa, 0x7};
  228. unsigned filt0_horz_ratio2, filt0_vert_ratio2;
  229. unsigned filt0_horz_ratio, filt0_vert_ratio;
  230. unsigned horz_step, vert_step;
  231. unsigned r, cmd = 0;
  232. unsigned pscaleCanvasWidth = (dec->conf.angle & 1) ? dec->conf.dec_h : dec->conf.dec_w;
  233. pscaleCanvasWidth = (pscaleCanvasWidth + 63) & (~63);
  234. canvas_config(JPEGDEC_OUTPUT_CANVAS_Y,
  235. dec->conf.addr_y,
  236. dec->conf.canvas_width,
  237. dec->conf.dec_h,
  238. CANVAS_ADDR_NOWRAP,
  239. CANVAS_BLKMODE_LINEAR);
  240. canvas_config(JPEGDEC_OUTPUT_CANVAS_U,
  241. dec->conf.addr_u,
  242. dec->conf.canvas_width / 2,
  243. dec->conf.dec_h / 2,
  244. CANVAS_ADDR_NOWRAP,
  245. CANVAS_BLKMODE_LINEAR);
  246. canvas_config(JPEGDEC_OUTPUT_CANVAS_V,
  247. dec->conf.addr_v,
  248. dec->conf.canvas_width / 2,
  249. dec->conf.dec_h / 2,
  250. CANVAS_ADDR_NOWRAP,
  251. CANVAS_BLKMODE_LINEAR);
  252. canvas_config(PSCALE_CANVAS_Y,
  253. pbufAddr,
  254. pscaleCanvasWidth,
  255. 128,
  256. CANVAS_ADDR_NOWRAP,
  257. CANVAS_BLKMODE_32X32);
  258. canvas_config(PSCALE_CANVAS_U,
  259. pbufAddr + ((pscaleCanvasWidth + 7) & ~7) * 128,
  260. pscaleCanvasWidth / 2,
  261. 128,
  262. CANVAS_ADDR_NOWRAP,
  263. CANVAS_BLKMODE_32X32);
  264. canvas_config(PSCALE_CANVAS_V,
  265. pbufAddr + ((pscaleCanvasWidth + 7) & ~7) * 128 + ((pscaleCanvasWidth / 2 + 7) & ~7) * 128,
  266. pscaleCanvasWidth / 2,
  267. 128,
  268. CANVAS_ADDR_NOWRAP,
  269. CANVAS_BLKMODE_32X32);
  270. WRITE_VREG(JPEG_SCREEN_OFFSET_X, dec->conf.dec_x);
  271. WRITE_VREG(JPEG_SCREEN_OFFSET_Y, dec->conf.dec_y);
  272. WRITE_VREG(PSCALE_PICO_SHIFT_XY, 0);
  273. WRITE_VREG(JPEG_MCU_CROP_HSTART, 0);
  274. WRITE_VREG(JPEG_MCU_CROP_HEND, ((dec->info.width + 15) >> 4) - 1);
  275. WRITE_VREG(JPEG_MCU_CROP_VSTART, 0);
  276. WRITE_VREG(JPEG_MCU_CROP_VEND, ((dec->info.height + 15) >> 4) - 1);
  277. if ((dec->conf.angle & 1) == 0) {
  278. r = dec->info.width / dec->conf.dec_w;
  279. filt0_horz_ratio = filt0_table[r];
  280. filt0_horz_ratio2 = filt0_table[min((unsigned)(dec->info.width / dec->conf.dec_w) >> (2 + r), 8U)];
  281. horz_step = (dec->info.width << (16 - filt0_horz_ratio - filt0_horz_ratio2)) / dec->conf.dec_w;
  282. r = dec->info.height / dec->conf.dec_h;
  283. filt0_vert_ratio = filt0_table[r];
  284. filt0_vert_ratio2 = filt0_table[min((dec->info.height / dec->conf.dec_h) >> (2 + r), 8U)];
  285. vert_step = (dec->info.height << (16 - filt0_vert_ratio - filt0_vert_ratio2)) / dec->conf.dec_h;
  286. WRITE_VREG(JPEG_PIC_WIDTH, dec->conf.dec_w);
  287. WRITE_VREG(JPEG_PIC_HEIGHT, dec->conf.dec_h);
  288. } else {
  289. r = dec->info.width / dec->conf.dec_h;
  290. filt0_horz_ratio = filt0_table[r];
  291. filt0_horz_ratio2 = filt0_table[min((unsigned)(dec->info.width / dec->conf.dec_h) >> (2 + r), 8U)];
  292. horz_step = (dec->info.width << (16 - filt0_horz_ratio - filt0_horz_ratio2)) / dec->conf.dec_h;
  293. r = dec->info.height / dec->conf.dec_w;
  294. filt0_vert_ratio = filt0_table[r];
  295. filt0_vert_ratio2 = filt0_table[min((dec->info.height / dec->conf.dec_w) >> (2 + r), 8U)];
  296. vert_step = (dec->info.height << (16 - filt0_vert_ratio - filt0_vert_ratio2)) / dec->conf.dec_w;
  297. WRITE_VREG(JPEG_PIC_WIDTH, dec->conf.dec_h);
  298. WRITE_VREG(JPEG_PIC_HEIGHT, dec->conf.dec_w);
  299. }
  300. _init_scaler(horz_step, vert_step);
  301. WRITE_VREG(JPEG_DECODE_PARAMETER,
  302. filt0_horz_ratio2 << 14 | //round1
  303. filt0_vert_ratio2 << 12 | //round1
  304. filt0_horz_ratio << 10 | //round0
  305. filt0_vert_ratio << 8 | //round0
  306. addr_mode_tab[dec->conf.angle & 3] << 4);
  307. WRITE_VREG(PSCALE_RBUF_START_BLKX, 0);
  308. WRITE_VREG(PSCALE_RBUF_START_BLKY, 0);
  309. WRITE_VREG(PSCALE_CANVAS_WR_ADDR, JPEGDEC_OUTPUT_CANVAS);
  310. WRITE_VREG(PSCALE_CANVAS_RD_ADDR, PSCALE_CANVAS);
  311. if (dec->conf.opt & JPEGDEC_OPT_FULLRANGE) {
  312. cmd |= 1 << 5;
  313. }
  314. if (dec->conf.opt & JPEGDEC_OPT_THUMBNAIL_ONLY) {
  315. cmd |= 1 << 7;
  316. }
  317. pr_dbg("Enable decoding\n");
  318. WRITE_VREG(JPEG_DECODE_START, cmd | 1);
  319. }
  320. static int amjpegdec_open(struct inode *inode, struct file *file)
  321. {
  322. int r = 0;
  323. mutex_lock(&jpegdec_module_mutex);
  324. if (dec != NULL) {
  325. r = -EBUSY;
  326. } else if ((dec = (jpegdec_t *)kcalloc(1, sizeof(jpegdec_t), GFP_KERNEL)) == NULL) {
  327. r = -ENOMEM;
  328. }
  329. mutex_unlock(&jpegdec_module_mutex);
  330. if (r == 0) {
  331. if ((r = _init_dec(dec)) < 0) {
  332. kfree(dec);
  333. dec = NULL;
  334. } else {
  335. file->private_data = dec;
  336. }
  337. }
  338. return r;
  339. }
  340. static int amjpegdec_release(struct inode *inode, struct file *file)
  341. {
  342. if (dec) {
  343. free_irq(INT_MAILBOX_1A, (void *)jpegdec_id);
  344. kfree(dec);
  345. dec = NULL;
  346. }
  347. amvdec_disable();
  348. return 0;
  349. }
  350. static long amjpegdec_ioctl(struct file *file,
  351. unsigned int cmd, ulong arg)
  352. {
  353. int r = 0;
  354. switch (cmd) {
  355. case JPEGDEC_IOC_INFOCONFIG:
  356. if (dec->state & JPEGDEC_STAT_WAIT_INFOCONFIG) {
  357. pr_dbg("amjpegdec_ioctl: JPEGDEC_IOC_INFOCONFIG\n");
  358. dec->conf.opt |= arg & (JPEGDEC_OPT_THUMBNAIL_ONLY | JPEGDEC_OPT_THUMBNAIL_PREFERED);
  359. WRITE_VREG(JPEG_DECODE_START,
  360. (dec->conf.opt & JPEGDEC_OPT_THUMBNAIL_PREFERED) ? 1 : 0);
  361. dec->state &= ~JPEGDEC_STAT_WAIT_INFOCONFIG;
  362. dec->state |= JPEGDEC_STAT_WAIT_DATA;
  363. amvdec_start();
  364. } else {
  365. r = -EPERM;
  366. }
  367. break;
  368. case JPEGDEC_IOC_DECCONFIG:
  369. if (dec->state & JPEGDEC_STAT_WAIT_DECCONFIG) {
  370. if (copy_from_user(&dec->conf, (void *)arg, sizeof(jpegdec_config_t))) {
  371. return -EFAULT;
  372. }
  373. pr_dbg("amjpegdec_ioctl: JPEGDEC_IOC_DECCONFIG, target (%d-%d-%d-%d)\n",
  374. dec->conf.dec_x, dec->conf.dec_y, dec->conf.dec_w, dec->conf.dec_h);
  375. pr_dbg("planes (0x%lx-0x%lx-0x%lx), pbufAddr=0x%lx\n",
  376. dec->conf.addr_y, dec->conf.addr_u, dec->conf.addr_v, pbufAddr);
  377. if ((dec->conf.angle & 1) == 0) {
  378. if ((dec->conf.dec_w > dec->info.width) ||
  379. (dec->conf.dec_h > dec->info.height)) {
  380. return -EPERM;
  381. }
  382. } else {
  383. if ((dec->conf.dec_w > dec->info.height) ||
  384. (dec->conf.dec_h > dec->info.width)) {
  385. return -EPERM;
  386. }
  387. }
  388. dec->state &= ~JPEGDEC_STAT_WAIT_DECCONFIG;
  389. _dec_run();
  390. } else {
  391. r = -EPERM;
  392. }
  393. break;
  394. case JPEGDEC_IOC_INFO:
  395. if (dec->state & JPEGDEC_STAT_INFO_READY) {
  396. pr_dbg("amjpegdec_ioctl: JPEGDEC_IOC_INFO, %dx%d\n",
  397. dec->info.width, dec->info.height);
  398. if (copy_to_user((void *)arg, &dec->info, sizeof(jpegdec_info_t))) {
  399. return -EFAULT;
  400. }
  401. } else {
  402. r = -EAGAIN;
  403. }
  404. break;
  405. case JPEGDEC_IOC_STAT:
  406. return dec->state;
  407. case JPEGDEC_G_MEM_INFO:
  408. if (copy_from_user(&jegdec_mem_info, (void __user *)arg, sizeof(jpegdec_mem_info_t))) {
  409. r = -EFAULT;
  410. } else {
  411. unsigned pscaleCanvasbwidth = (jegdec_mem_info.angle & 1) ? jegdec_mem_info.dec_h : jegdec_mem_info.dec_w;
  412. pscaleCanvasbwidth = (pscaleCanvasbwidth + 63) & (~63);
  413. pscaleCanvasbwidth = pscaleCanvasbwidth * 2 * 128;
  414. jegdec_mem_info.canv_addr = pscaleCanvasbwidth + pbufAddr;
  415. jegdec_mem_info.canv_len = pbufSize - pscaleCanvasbwidth;
  416. if (copy_to_user((void __user *)arg, &jegdec_mem_info, sizeof(jpegdec_mem_info_t))) {
  417. r = -EFAULT;
  418. }
  419. }
  420. break;
  421. default:
  422. return -ENOIOCTLCMD;
  423. }
  424. return r;
  425. }
  426. static int mmap(struct file *filp, struct vm_area_struct *vma)
  427. {
  428. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  429. unsigned vm_size = vma->vm_end - vma->vm_start;
  430. if (vm_size == 0) {
  431. return -EAGAIN;
  432. }
  433. //printk("mmap:%x\n",vm_size);
  434. off += jegdec_mem_info.canv_addr;
  435. vma->vm_flags |= VM_RESERVED | VM_IO;
  436. if (remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  437. vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
  438. printk("set_cached: failed remap_pfn_range\n");
  439. return -EAGAIN;
  440. }
  441. return 0;
  442. }
  443. const static struct file_operations amjpegdec_fops = {
  444. .owner = THIS_MODULE,
  445. .open = amjpegdec_open,
  446. .mmap = mmap,
  447. .release = amjpegdec_release,
  448. .unlocked_ioctl = amjpegdec_ioctl,
  449. };
  450. int AMHWJPEGDEC_MAJOR = 0;
  451. static int amjpegdec_probe(struct platform_device *pdev)
  452. {
  453. int r;
  454. struct resource *s;
  455. AMHWJPEGDEC_MAJOR = 0;
  456. r = register_chrdev(AMHWJPEGDEC_MAJOR, "amjpegdev", &amjpegdec_fops);
  457. if (r < 0) {
  458. pr_error("Can't register major for amjpegdec device\n");
  459. return r;
  460. }
  461. AMHWJPEGDEC_MAJOR = r;
  462. amjpegdec_class = class_create(THIS_MODULE, DEVICE_NAME);
  463. amjpegdec_dev = device_create(amjpegdec_class, NULL,
  464. MKDEV(AMHWJPEGDEC_MAJOR, 0), NULL,
  465. DEVICE_NAME);
  466. s = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  467. pbufAddr = s->start;
  468. pbufSize = s->end - s->start + 1;
  469. return 0;
  470. }
  471. static int amjpegdec_remove(struct platform_device *pdev)
  472. {
  473. device_destroy(amjpegdec_class, MKDEV(AMHWJPEGDEC_MAJOR, 0));
  474. class_destroy(amjpegdec_class);
  475. unregister_chrdev(AMHWJPEGDEC_MAJOR, DEVICE_NAME);
  476. return 0;
  477. }
  478. static struct platform_driver amjpegdec_driver = {
  479. .probe = amjpegdec_probe,
  480. .remove = amjpegdec_remove,
  481. .driver = {
  482. .name = "amjpegdec",
  483. }
  484. };
  485. static int __init amjpegdec_init(void)
  486. {
  487. if (platform_driver_register(&amjpegdec_driver)) {
  488. pr_error("failed to register amjpegdec module\n");
  489. return -ENODEV;
  490. }
  491. return 0;
  492. }
  493. static void __exit amjpegdec_exit(void)
  494. {
  495. platform_driver_unregister(&amjpegdec_driver);
  496. return ;
  497. }
  498. module_init(amjpegdec_init);
  499. module_exit(amjpegdec_exit);
  500. MODULE_DESCRIPTION("AMLOGIC HW JPEG decoder driver");
  501. MODULE_LICENSE("GPL");
  502. MODULE_AUTHOR("Tim Yao <timyao@amlogic.com>");