jpeg-core.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /* linux/drivers/media/video/s5p-jpeg/jpeg-core.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/gfp.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/string.h>
  24. #include <media/v4l2-mem2mem.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/videobuf2-core.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "jpeg-core.h"
  29. #include "jpeg-hw.h"
  30. static struct s5p_jpeg_fmt formats_enc[] = {
  31. {
  32. .name = "JPEG JFIF",
  33. .fourcc = V4L2_PIX_FMT_JPEG,
  34. .colplanes = 1,
  35. .types = MEM2MEM_CAPTURE,
  36. },
  37. {
  38. .name = "YUV 4:2:2 packed, YCbYCr",
  39. .fourcc = V4L2_PIX_FMT_YUYV,
  40. .depth = 16,
  41. .colplanes = 1,
  42. .types = MEM2MEM_OUTPUT,
  43. },
  44. {
  45. .name = "RGB565",
  46. .fourcc = V4L2_PIX_FMT_RGB565,
  47. .depth = 16,
  48. .colplanes = 1,
  49. .types = MEM2MEM_OUTPUT,
  50. },
  51. };
  52. #define NUM_FORMATS_ENC ARRAY_SIZE(formats_enc)
  53. static struct s5p_jpeg_fmt formats_dec[] = {
  54. {
  55. .name = "YUV 4:2:0 planar, YCbCr",
  56. .fourcc = V4L2_PIX_FMT_YUV420,
  57. .depth = 12,
  58. .colplanes = 3,
  59. .h_align = 4,
  60. .v_align = 4,
  61. .types = MEM2MEM_CAPTURE,
  62. },
  63. {
  64. .name = "YUV 4:2:2 packed, YCbYCr",
  65. .fourcc = V4L2_PIX_FMT_YUYV,
  66. .depth = 16,
  67. .colplanes = 1,
  68. .h_align = 4,
  69. .v_align = 3,
  70. .types = MEM2MEM_CAPTURE,
  71. },
  72. {
  73. .name = "JPEG JFIF",
  74. .fourcc = V4L2_PIX_FMT_JPEG,
  75. .colplanes = 1,
  76. .types = MEM2MEM_OUTPUT,
  77. },
  78. };
  79. #define NUM_FORMATS_DEC ARRAY_SIZE(formats_dec)
  80. static const unsigned char qtbl_luminance[4][64] = {
  81. {/* level 1 - high quality */
  82. 8, 6, 6, 8, 12, 14, 16, 17,
  83. 6, 6, 6, 8, 10, 13, 12, 15,
  84. 6, 6, 7, 8, 13, 14, 18, 24,
  85. 8, 8, 8, 14, 13, 19, 24, 35,
  86. 12, 10, 13, 13, 20, 26, 34, 39,
  87. 14, 13, 14, 19, 26, 34, 39, 39,
  88. 16, 12, 18, 24, 34, 39, 39, 39,
  89. 17, 15, 24, 35, 39, 39, 39, 39
  90. },
  91. {/* level 2 */
  92. 12, 8, 8, 12, 17, 21, 24, 23,
  93. 8, 9, 9, 11, 15, 19, 18, 23,
  94. 8, 9, 10, 12, 19, 20, 27, 36,
  95. 12, 11, 12, 21, 20, 28, 36, 53,
  96. 17, 15, 19, 20, 30, 39, 51, 59,
  97. 21, 19, 20, 28, 39, 51, 59, 59,
  98. 24, 18, 27, 36, 51, 59, 59, 59,
  99. 23, 23, 36, 53, 59, 59, 59, 59
  100. },
  101. {/* level 3 */
  102. 16, 11, 11, 16, 23, 27, 31, 30,
  103. 11, 12, 12, 15, 20, 23, 23, 30,
  104. 11, 12, 13, 16, 23, 26, 35, 47,
  105. 16, 15, 16, 23, 26, 37, 47, 64,
  106. 23, 20, 23, 26, 39, 51, 64, 64,
  107. 27, 23, 26, 37, 51, 64, 64, 64,
  108. 31, 23, 35, 47, 64, 64, 64, 64,
  109. 30, 30, 47, 64, 64, 64, 64, 64
  110. },
  111. {/*level 4 - low quality */
  112. 20, 16, 25, 39, 50, 46, 62, 68,
  113. 16, 18, 23, 38, 38, 53, 65, 68,
  114. 25, 23, 31, 38, 53, 65, 68, 68,
  115. 39, 38, 38, 53, 65, 68, 68, 68,
  116. 50, 38, 53, 65, 68, 68, 68, 68,
  117. 46, 53, 65, 68, 68, 68, 68, 68,
  118. 62, 65, 68, 68, 68, 68, 68, 68,
  119. 68, 68, 68, 68, 68, 68, 68, 68
  120. }
  121. };
  122. static const unsigned char qtbl_chrominance[4][64] = {
  123. {/* level 1 - high quality */
  124. 9, 8, 9, 11, 14, 17, 19, 24,
  125. 8, 10, 9, 11, 14, 13, 17, 22,
  126. 9, 9, 13, 14, 13, 15, 23, 26,
  127. 11, 11, 14, 14, 15, 20, 26, 33,
  128. 14, 14, 13, 15, 20, 24, 33, 39,
  129. 17, 13, 15, 20, 24, 32, 39, 39,
  130. 19, 17, 23, 26, 33, 39, 39, 39,
  131. 24, 22, 26, 33, 39, 39, 39, 39
  132. },
  133. {/* level 2 */
  134. 13, 11, 13, 16, 20, 20, 29, 37,
  135. 11, 14, 14, 14, 16, 20, 26, 32,
  136. 13, 14, 15, 17, 20, 23, 35, 40,
  137. 16, 14, 17, 21, 23, 30, 40, 50,
  138. 20, 16, 20, 23, 30, 37, 50, 59,
  139. 20, 20, 23, 30, 37, 48, 59, 59,
  140. 29, 26, 35, 40, 50, 59, 59, 59,
  141. 37, 32, 40, 50, 59, 59, 59, 59
  142. },
  143. {/* level 3 */
  144. 17, 15, 17, 21, 20, 26, 38, 48,
  145. 15, 19, 18, 17, 20, 26, 35, 43,
  146. 17, 18, 20, 22, 26, 30, 46, 53,
  147. 21, 17, 22, 28, 30, 39, 53, 64,
  148. 20, 20, 26, 30, 39, 48, 64, 64,
  149. 26, 26, 30, 39, 48, 63, 64, 64,
  150. 38, 35, 46, 53, 64, 64, 64, 64,
  151. 48, 43, 53, 64, 64, 64, 64, 64
  152. },
  153. {/*level 4 - low quality */
  154. 21, 25, 32, 38, 54, 68, 68, 68,
  155. 25, 28, 24, 38, 54, 68, 68, 68,
  156. 32, 24, 32, 43, 66, 68, 68, 68,
  157. 38, 38, 43, 53, 68, 68, 68, 68,
  158. 54, 54, 66, 68, 68, 68, 68, 68,
  159. 68, 68, 68, 68, 68, 68, 68, 68,
  160. 68, 68, 68, 68, 68, 68, 68, 68,
  161. 68, 68, 68, 68, 68, 68, 68, 68
  162. }
  163. };
  164. static const unsigned char hdctbl0[16] = {
  165. 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
  166. };
  167. static const unsigned char hdctblg0[12] = {
  168. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
  169. };
  170. static const unsigned char hactbl0[16] = {
  171. 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
  172. };
  173. static const unsigned char hactblg0[162] = {
  174. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  175. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  176. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  177. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  178. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  179. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  180. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  181. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  182. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  183. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  184. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  185. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  186. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  187. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  188. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  189. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  190. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  191. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  192. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  193. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  194. 0xf9, 0xfa
  195. };
  196. static inline struct s5p_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
  197. {
  198. return container_of(c->handler, struct s5p_jpeg_ctx, ctrl_handler);
  199. }
  200. static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
  201. {
  202. return container_of(fh, struct s5p_jpeg_ctx, fh);
  203. }
  204. static inline void jpeg_set_qtbl(void __iomem *regs, const unsigned char *qtbl,
  205. unsigned long tab, int len)
  206. {
  207. int i;
  208. for (i = 0; i < len; i++)
  209. writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
  210. }
  211. static inline void jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  212. {
  213. /* this driver fills quantisation table 0 with data for luma */
  214. jpeg_set_qtbl(regs, qtbl_luminance[quality], S5P_JPG_QTBL_CONTENT(0),
  215. ARRAY_SIZE(qtbl_luminance[quality]));
  216. }
  217. static inline void jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  218. {
  219. /* this driver fills quantisation table 1 with data for chroma */
  220. jpeg_set_qtbl(regs, qtbl_chrominance[quality], S5P_JPG_QTBL_CONTENT(1),
  221. ARRAY_SIZE(qtbl_chrominance[quality]));
  222. }
  223. static inline void jpeg_set_htbl(void __iomem *regs, const unsigned char *htbl,
  224. unsigned long tab, int len)
  225. {
  226. int i;
  227. for (i = 0; i < len; i++)
  228. writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
  229. }
  230. static inline void jpeg_set_hdctbl(void __iomem *regs)
  231. {
  232. /* this driver fills table 0 for this component */
  233. jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0), ARRAY_SIZE(hdctbl0));
  234. }
  235. static inline void jpeg_set_hdctblg(void __iomem *regs)
  236. {
  237. /* this driver fills table 0 for this component */
  238. jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0), ARRAY_SIZE(hdctblg0));
  239. }
  240. static inline void jpeg_set_hactbl(void __iomem *regs)
  241. {
  242. /* this driver fills table 0 for this component */
  243. jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0), ARRAY_SIZE(hactbl0));
  244. }
  245. static inline void jpeg_set_hactblg(void __iomem *regs)
  246. {
  247. /* this driver fills table 0 for this component */
  248. jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0), ARRAY_SIZE(hactblg0));
  249. }
  250. /*
  251. * ============================================================================
  252. * Device file operations
  253. * ============================================================================
  254. */
  255. static int queue_init(void *priv, struct vb2_queue *src_vq,
  256. struct vb2_queue *dst_vq);
  257. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(unsigned int mode,
  258. __u32 pixelformat);
  259. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
  260. static int s5p_jpeg_open(struct file *file)
  261. {
  262. struct s5p_jpeg *jpeg = video_drvdata(file);
  263. struct video_device *vfd = video_devdata(file);
  264. struct s5p_jpeg_ctx *ctx;
  265. struct s5p_jpeg_fmt *out_fmt;
  266. int ret = 0;
  267. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  268. if (!ctx)
  269. return -ENOMEM;
  270. v4l2_fh_init(&ctx->fh, vfd);
  271. /* Use separate control handler per file handle */
  272. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  273. file->private_data = &ctx->fh;
  274. v4l2_fh_add(&ctx->fh);
  275. ctx->jpeg = jpeg;
  276. if (vfd == jpeg->vfd_encoder) {
  277. ctx->mode = S5P_JPEG_ENCODE;
  278. out_fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_RGB565);
  279. } else {
  280. ctx->mode = S5P_JPEG_DECODE;
  281. out_fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_JPEG);
  282. }
  283. ret = s5p_jpeg_controls_create(ctx);
  284. if (ret < 0)
  285. goto error;
  286. ctx->m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
  287. if (IS_ERR(ctx->m2m_ctx)) {
  288. ret = PTR_ERR(ctx->m2m_ctx);
  289. goto error;
  290. }
  291. ctx->out_q.fmt = out_fmt;
  292. ctx->cap_q.fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_YUYV);
  293. return 0;
  294. error:
  295. v4l2_fh_del(&ctx->fh);
  296. v4l2_fh_exit(&ctx->fh);
  297. kfree(ctx);
  298. return ret;
  299. }
  300. static int s5p_jpeg_release(struct file *file)
  301. {
  302. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  303. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  304. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  305. v4l2_fh_del(&ctx->fh);
  306. v4l2_fh_exit(&ctx->fh);
  307. kfree(ctx);
  308. return 0;
  309. }
  310. static unsigned int s5p_jpeg_poll(struct file *file,
  311. struct poll_table_struct *wait)
  312. {
  313. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  314. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  315. }
  316. static int s5p_jpeg_mmap(struct file *file, struct vm_area_struct *vma)
  317. {
  318. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  319. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  320. }
  321. static const struct v4l2_file_operations s5p_jpeg_fops = {
  322. .owner = THIS_MODULE,
  323. .open = s5p_jpeg_open,
  324. .release = s5p_jpeg_release,
  325. .poll = s5p_jpeg_poll,
  326. .unlocked_ioctl = video_ioctl2,
  327. .mmap = s5p_jpeg_mmap,
  328. };
  329. /*
  330. * ============================================================================
  331. * video ioctl operations
  332. * ============================================================================
  333. */
  334. static int get_byte(struct s5p_jpeg_buffer *buf)
  335. {
  336. if (buf->curr >= buf->size)
  337. return -1;
  338. return ((unsigned char *)buf->data)[buf->curr++];
  339. }
  340. static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
  341. {
  342. unsigned int temp;
  343. int byte;
  344. byte = get_byte(buf);
  345. if (byte == -1)
  346. return -1;
  347. temp = byte << 8;
  348. byte = get_byte(buf);
  349. if (byte == -1)
  350. return -1;
  351. *word = (unsigned int)byte | temp;
  352. return 0;
  353. }
  354. static void skip(struct s5p_jpeg_buffer *buf, long len)
  355. {
  356. if (len <= 0)
  357. return;
  358. while (len--)
  359. get_byte(buf);
  360. }
  361. static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
  362. unsigned long buffer, unsigned long size)
  363. {
  364. int c, components, notfound;
  365. unsigned int height, width, word;
  366. long length;
  367. struct s5p_jpeg_buffer jpeg_buffer;
  368. jpeg_buffer.size = size;
  369. jpeg_buffer.data = buffer;
  370. jpeg_buffer.curr = 0;
  371. notfound = 1;
  372. while (notfound) {
  373. c = get_byte(&jpeg_buffer);
  374. if (c == -1)
  375. break;
  376. if (c != 0xff)
  377. continue;
  378. do
  379. c = get_byte(&jpeg_buffer);
  380. while (c == 0xff);
  381. if (c == -1)
  382. break;
  383. if (c == 0)
  384. continue;
  385. length = 0;
  386. switch (c) {
  387. /* SOF0: baseline JPEG */
  388. case SOF0:
  389. if (get_word_be(&jpeg_buffer, &word))
  390. break;
  391. if (get_byte(&jpeg_buffer) == -1)
  392. break;
  393. if (get_word_be(&jpeg_buffer, &height))
  394. break;
  395. if (get_word_be(&jpeg_buffer, &width))
  396. break;
  397. components = get_byte(&jpeg_buffer);
  398. if (components == -1)
  399. break;
  400. notfound = 0;
  401. skip(&jpeg_buffer, components * 3);
  402. break;
  403. /* skip payload-less markers */
  404. case RST ... RST + 7:
  405. case SOI:
  406. case EOI:
  407. case TEM:
  408. break;
  409. /* skip uninteresting payload markers */
  410. default:
  411. if (get_word_be(&jpeg_buffer, &word))
  412. break;
  413. length = (long)word - 2;
  414. skip(&jpeg_buffer, length);
  415. break;
  416. }
  417. }
  418. result->w = width;
  419. result->h = height;
  420. result->size = components;
  421. return !notfound;
  422. }
  423. static int s5p_jpeg_querycap(struct file *file, void *priv,
  424. struct v4l2_capability *cap)
  425. {
  426. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  427. if (ctx->mode == S5P_JPEG_ENCODE) {
  428. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
  429. sizeof(cap->driver));
  430. strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
  431. sizeof(cap->card));
  432. } else {
  433. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " decoder",
  434. sizeof(cap->driver));
  435. strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
  436. sizeof(cap->card));
  437. }
  438. cap->bus_info[0] = 0;
  439. cap->capabilities = V4L2_CAP_STREAMING |
  440. V4L2_CAP_VIDEO_CAPTURE |
  441. V4L2_CAP_VIDEO_OUTPUT;
  442. return 0;
  443. }
  444. static int enum_fmt(struct s5p_jpeg_fmt *formats, int n,
  445. struct v4l2_fmtdesc *f, u32 type)
  446. {
  447. int i, num = 0;
  448. for (i = 0; i < n; ++i) {
  449. if (formats[i].types & type) {
  450. /* index-th format of type type found ? */
  451. if (num == f->index)
  452. break;
  453. /* Correct type but haven't reached our index yet,
  454. * just increment per-type index */
  455. ++num;
  456. }
  457. }
  458. /* Format not found */
  459. if (i >= n)
  460. return -EINVAL;
  461. strlcpy(f->description, formats[i].name, sizeof(f->description));
  462. f->pixelformat = formats[i].fourcc;
  463. return 0;
  464. }
  465. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  466. struct v4l2_fmtdesc *f)
  467. {
  468. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  469. if (ctx->mode == S5P_JPEG_ENCODE)
  470. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  471. MEM2MEM_CAPTURE);
  472. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_CAPTURE);
  473. }
  474. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  475. struct v4l2_fmtdesc *f)
  476. {
  477. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  478. if (ctx->mode == S5P_JPEG_ENCODE)
  479. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  480. MEM2MEM_OUTPUT);
  481. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_OUTPUT);
  482. }
  483. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  484. enum v4l2_buf_type type)
  485. {
  486. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  487. return &ctx->out_q;
  488. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  489. return &ctx->cap_q;
  490. return NULL;
  491. }
  492. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  493. {
  494. struct vb2_queue *vq;
  495. struct s5p_jpeg_q_data *q_data = NULL;
  496. struct v4l2_pix_format *pix = &f->fmt.pix;
  497. struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
  498. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  499. if (!vq)
  500. return -EINVAL;
  501. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  502. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  503. return -EINVAL;
  504. q_data = get_q_data(ct, f->type);
  505. BUG_ON(q_data == NULL);
  506. pix->width = q_data->w;
  507. pix->height = q_data->h;
  508. pix->field = V4L2_FIELD_NONE;
  509. pix->pixelformat = q_data->fmt->fourcc;
  510. pix->bytesperline = 0;
  511. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  512. u32 bpl = q_data->w;
  513. if (q_data->fmt->colplanes == 1)
  514. bpl = (bpl * q_data->fmt->depth) >> 3;
  515. pix->bytesperline = bpl;
  516. }
  517. pix->sizeimage = q_data->size;
  518. return 0;
  519. }
  520. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(unsigned int mode,
  521. u32 pixelformat)
  522. {
  523. unsigned int k;
  524. struct s5p_jpeg_fmt *formats;
  525. int n;
  526. if (mode == S5P_JPEG_ENCODE) {
  527. formats = formats_enc;
  528. n = NUM_FORMATS_ENC;
  529. } else {
  530. formats = formats_dec;
  531. n = NUM_FORMATS_DEC;
  532. }
  533. for (k = 0; k < n; k++) {
  534. struct s5p_jpeg_fmt *fmt = &formats[k];
  535. if (fmt->fourcc == pixelformat)
  536. return fmt;
  537. }
  538. return NULL;
  539. }
  540. static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  541. unsigned int walign,
  542. u32 *h, unsigned int hmin, unsigned int hmax,
  543. unsigned int halign)
  544. {
  545. int width, height, w_step, h_step;
  546. width = *w;
  547. height = *h;
  548. w_step = 1 << walign;
  549. h_step = 1 << halign;
  550. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  551. if (*w < width && (*w + w_step) < wmax)
  552. *w += w_step;
  553. if (*h < height && (*h + h_step) < hmax)
  554. *h += h_step;
  555. }
  556. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  557. struct s5p_jpeg_ctx *ctx, int q_type)
  558. {
  559. struct v4l2_pix_format *pix = &f->fmt.pix;
  560. if (pix->field == V4L2_FIELD_ANY)
  561. pix->field = V4L2_FIELD_NONE;
  562. else if (pix->field != V4L2_FIELD_NONE)
  563. return -EINVAL;
  564. /* V4L2 specification suggests the driver corrects the format struct
  565. * if any of the dimensions is unsupported */
  566. if (q_type == MEM2MEM_OUTPUT)
  567. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  568. S5P_JPEG_MAX_WIDTH, 0,
  569. &pix->height, S5P_JPEG_MIN_HEIGHT,
  570. S5P_JPEG_MAX_HEIGHT, 0);
  571. else
  572. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  573. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  574. &pix->height, S5P_JPEG_MIN_HEIGHT,
  575. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  576. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  577. if (pix->sizeimage <= 0)
  578. pix->sizeimage = PAGE_SIZE;
  579. pix->bytesperline = 0;
  580. } else {
  581. u32 bpl = pix->bytesperline;
  582. if (fmt->colplanes > 1 && bpl < pix->width)
  583. bpl = pix->width; /* planar */
  584. if (fmt->colplanes == 1 && /* packed */
  585. (bpl << 3) * fmt->depth < pix->width)
  586. bpl = (pix->width * fmt->depth) >> 3;
  587. pix->bytesperline = bpl;
  588. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  589. }
  590. return 0;
  591. }
  592. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  593. struct v4l2_format *f)
  594. {
  595. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  596. struct s5p_jpeg_fmt *fmt;
  597. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  598. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  599. v4l2_err(&ctx->jpeg->v4l2_dev,
  600. "Fourcc format (0x%08x) invalid.\n",
  601. f->fmt.pix.pixelformat);
  602. return -EINVAL;
  603. }
  604. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_CAPTURE);
  605. }
  606. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  607. struct v4l2_format *f)
  608. {
  609. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  610. struct s5p_jpeg_fmt *fmt;
  611. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  612. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  613. v4l2_err(&ctx->jpeg->v4l2_dev,
  614. "Fourcc format (0x%08x) invalid.\n",
  615. f->fmt.pix.pixelformat);
  616. return -EINVAL;
  617. }
  618. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_OUTPUT);
  619. }
  620. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  621. {
  622. struct vb2_queue *vq;
  623. struct s5p_jpeg_q_data *q_data = NULL;
  624. struct v4l2_pix_format *pix = &f->fmt.pix;
  625. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  626. if (!vq)
  627. return -EINVAL;
  628. q_data = get_q_data(ct, f->type);
  629. BUG_ON(q_data == NULL);
  630. if (vb2_is_busy(vq)) {
  631. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  632. return -EBUSY;
  633. }
  634. q_data->fmt = s5p_jpeg_find_format(ct->mode, pix->pixelformat);
  635. q_data->w = pix->width;
  636. q_data->h = pix->height;
  637. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG)
  638. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  639. else
  640. q_data->size = pix->sizeimage;
  641. return 0;
  642. }
  643. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  644. struct v4l2_format *f)
  645. {
  646. int ret;
  647. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  648. if (ret)
  649. return ret;
  650. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  651. }
  652. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  653. struct v4l2_format *f)
  654. {
  655. int ret;
  656. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  657. if (ret)
  658. return ret;
  659. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  660. }
  661. static int s5p_jpeg_reqbufs(struct file *file, void *priv,
  662. struct v4l2_requestbuffers *reqbufs)
  663. {
  664. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  665. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  666. }
  667. static int s5p_jpeg_querybuf(struct file *file, void *priv,
  668. struct v4l2_buffer *buf)
  669. {
  670. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  671. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  672. }
  673. static int s5p_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  674. {
  675. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  676. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  677. }
  678. static int s5p_jpeg_dqbuf(struct file *file, void *priv,
  679. struct v4l2_buffer *buf)
  680. {
  681. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  682. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  683. }
  684. static int s5p_jpeg_streamon(struct file *file, void *priv,
  685. enum v4l2_buf_type type)
  686. {
  687. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  688. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  689. }
  690. static int s5p_jpeg_streamoff(struct file *file, void *priv,
  691. enum v4l2_buf_type type)
  692. {
  693. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  694. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  695. }
  696. int s5p_jpeg_g_selection(struct file *file, void *priv,
  697. struct v4l2_selection *s)
  698. {
  699. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  700. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  701. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  702. return -EINVAL;
  703. /* For JPEG blob active == default == bounds */
  704. switch (s->target) {
  705. case V4L2_SEL_TGT_CROP_ACTIVE:
  706. case V4L2_SEL_TGT_CROP_BOUNDS:
  707. case V4L2_SEL_TGT_CROP_DEFAULT:
  708. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  709. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  710. s->r.width = ctx->out_q.w;
  711. s->r.height = ctx->out_q.h;
  712. break;
  713. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  714. case V4L2_SEL_TGT_COMPOSE_PADDED:
  715. s->r.width = ctx->cap_q.w;
  716. s->r.height = ctx->cap_q.h;
  717. break;
  718. default:
  719. return -EINVAL;
  720. }
  721. s->r.left = 0;
  722. s->r.top = 0;
  723. return 0;
  724. }
  725. /*
  726. * V4L2 controls
  727. */
  728. static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  729. {
  730. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  731. struct s5p_jpeg *jpeg = ctx->jpeg;
  732. unsigned long flags;
  733. switch (ctrl->id) {
  734. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  735. spin_lock_irqsave(&jpeg->slock, flags);
  736. WARN_ON(ctx->subsampling > S5P_SUBSAMPLING_MODE_GRAY);
  737. if (ctx->subsampling > 2)
  738. ctrl->val = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  739. else
  740. ctrl->val = ctx->subsampling;
  741. spin_unlock_irqrestore(&jpeg->slock, flags);
  742. break;
  743. }
  744. return 0;
  745. }
  746. static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
  747. {
  748. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  749. unsigned long flags;
  750. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  751. switch (ctrl->id) {
  752. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  753. ctx->compr_quality = S5P_JPEG_COMPR_QUAL_WORST - ctrl->val;
  754. break;
  755. case V4L2_CID_JPEG_RESTART_INTERVAL:
  756. ctx->restart_interval = ctrl->val;
  757. break;
  758. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  759. ctx->subsampling = ctrl->val;
  760. break;
  761. }
  762. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  763. return 0;
  764. }
  765. static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
  766. .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
  767. .s_ctrl = s5p_jpeg_s_ctrl,
  768. };
  769. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
  770. {
  771. unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
  772. struct v4l2_ctrl *ctrl;
  773. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  774. if (ctx->mode == S5P_JPEG_ENCODE) {
  775. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  776. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  777. 0, 3, 1, 3);
  778. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  779. V4L2_CID_JPEG_RESTART_INTERVAL,
  780. 0, 0xffff, 1, 0);
  781. mask = ~0x06; /* 422, 420 */
  782. }
  783. ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  784. V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
  785. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
  786. V4L2_JPEG_CHROMA_SUBSAMPLING_422);
  787. if (ctx->ctrl_handler.error)
  788. return ctx->ctrl_handler.error;
  789. if (ctx->mode == S5P_JPEG_DECODE)
  790. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  791. V4L2_CTRL_FLAG_READ_ONLY;
  792. return 0;
  793. }
  794. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  795. .vidioc_querycap = s5p_jpeg_querycap,
  796. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  797. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  798. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  799. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  800. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  801. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  802. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  803. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  804. .vidioc_reqbufs = s5p_jpeg_reqbufs,
  805. .vidioc_querybuf = s5p_jpeg_querybuf,
  806. .vidioc_qbuf = s5p_jpeg_qbuf,
  807. .vidioc_dqbuf = s5p_jpeg_dqbuf,
  808. .vidioc_streamon = s5p_jpeg_streamon,
  809. .vidioc_streamoff = s5p_jpeg_streamoff,
  810. .vidioc_g_selection = s5p_jpeg_g_selection,
  811. };
  812. /*
  813. * ============================================================================
  814. * mem2mem callbacks
  815. * ============================================================================
  816. */
  817. static void s5p_jpeg_device_run(void *priv)
  818. {
  819. struct s5p_jpeg_ctx *ctx = priv;
  820. struct s5p_jpeg *jpeg = ctx->jpeg;
  821. struct vb2_buffer *src_buf, *dst_buf;
  822. unsigned long src_addr, dst_addr;
  823. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  824. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  825. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  826. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  827. jpeg_reset(jpeg->regs);
  828. jpeg_poweron(jpeg->regs);
  829. jpeg_proc_mode(jpeg->regs, ctx->mode);
  830. if (ctx->mode == S5P_JPEG_ENCODE) {
  831. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  832. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_565);
  833. else
  834. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_422);
  835. jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  836. jpeg_dri(jpeg->regs, ctx->restart_interval);
  837. jpeg_x(jpeg->regs, ctx->out_q.w);
  838. jpeg_y(jpeg->regs, ctx->out_q.h);
  839. jpeg_imgadr(jpeg->regs, src_addr);
  840. jpeg_jpgadr(jpeg->regs, dst_addr);
  841. /* ultimately comes from sizeimage from userspace */
  842. jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  843. /* JPEG RGB to YCbCr conversion matrix */
  844. jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  845. jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  846. jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  847. jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  848. jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  849. jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  850. jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  851. jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  852. jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  853. /*
  854. * JPEG IP allows storing 4 quantization tables
  855. * We fill table 0 for luma and table 1 for chroma
  856. */
  857. jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  858. jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  859. /* use table 0 for Y */
  860. jpeg_qtbl(jpeg->regs, 1, 0);
  861. /* use table 1 for Cb and Cr*/
  862. jpeg_qtbl(jpeg->regs, 2, 1);
  863. jpeg_qtbl(jpeg->regs, 3, 1);
  864. /* Y, Cb, Cr use Huffman table 0 */
  865. jpeg_htbl_ac(jpeg->regs, 1);
  866. jpeg_htbl_dc(jpeg->regs, 1);
  867. jpeg_htbl_ac(jpeg->regs, 2);
  868. jpeg_htbl_dc(jpeg->regs, 2);
  869. jpeg_htbl_ac(jpeg->regs, 3);
  870. jpeg_htbl_dc(jpeg->regs, 3);
  871. } else { /* S5P_JPEG_DECODE */
  872. jpeg_rst_int_enable(jpeg->regs, true);
  873. jpeg_data_num_int_enable(jpeg->regs, true);
  874. jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  875. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  876. jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  877. else
  878. jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
  879. jpeg_jpgadr(jpeg->regs, src_addr);
  880. jpeg_imgadr(jpeg->regs, dst_addr);
  881. }
  882. jpeg_start(jpeg->regs);
  883. }
  884. static int s5p_jpeg_job_ready(void *priv)
  885. {
  886. struct s5p_jpeg_ctx *ctx = priv;
  887. if (ctx->mode == S5P_JPEG_DECODE)
  888. return ctx->hdr_parsed;
  889. return 1;
  890. }
  891. static void s5p_jpeg_job_abort(void *priv)
  892. {
  893. }
  894. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  895. .device_run = s5p_jpeg_device_run,
  896. .job_ready = s5p_jpeg_job_ready,
  897. .job_abort = s5p_jpeg_job_abort,
  898. };
  899. /*
  900. * ============================================================================
  901. * Queue operations
  902. * ============================================================================
  903. */
  904. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  905. const struct v4l2_format *fmt,
  906. unsigned int *nbuffers, unsigned int *nplanes,
  907. unsigned int sizes[], void *alloc_ctxs[])
  908. {
  909. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  910. struct s5p_jpeg_q_data *q_data = NULL;
  911. unsigned int size, count = *nbuffers;
  912. q_data = get_q_data(ctx, vq->type);
  913. BUG_ON(q_data == NULL);
  914. size = q_data->size;
  915. /*
  916. * header is parsed during decoding and parsed information stored
  917. * in the context so we do not allow another buffer to overwrite it
  918. */
  919. if (ctx->mode == S5P_JPEG_DECODE)
  920. count = 1;
  921. *nbuffers = count;
  922. *nplanes = 1;
  923. sizes[0] = size;
  924. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  925. return 0;
  926. }
  927. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  928. {
  929. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  930. struct s5p_jpeg_q_data *q_data = NULL;
  931. q_data = get_q_data(ctx, vb->vb2_queue->type);
  932. BUG_ON(q_data == NULL);
  933. if (vb2_plane_size(vb, 0) < q_data->size) {
  934. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  935. __func__, vb2_plane_size(vb, 0),
  936. (long)q_data->size);
  937. return -EINVAL;
  938. }
  939. vb2_set_plane_payload(vb, 0, q_data->size);
  940. return 0;
  941. }
  942. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  943. {
  944. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  945. if (ctx->mode == S5P_JPEG_DECODE &&
  946. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  947. struct s5p_jpeg_q_data tmp, *q_data;
  948. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  949. (unsigned long)vb2_plane_vaddr(vb, 0),
  950. min((unsigned long)ctx->out_q.size,
  951. vb2_get_plane_payload(vb, 0)));
  952. if (!ctx->hdr_parsed) {
  953. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  954. return;
  955. }
  956. q_data = &ctx->out_q;
  957. q_data->w = tmp.w;
  958. q_data->h = tmp.h;
  959. q_data = &ctx->cap_q;
  960. q_data->w = tmp.w;
  961. q_data->h = tmp.h;
  962. jpeg_bound_align_image(&q_data->w, S5P_JPEG_MIN_WIDTH,
  963. S5P_JPEG_MAX_WIDTH, q_data->fmt->h_align,
  964. &q_data->h, S5P_JPEG_MIN_HEIGHT,
  965. S5P_JPEG_MAX_HEIGHT, q_data->fmt->v_align
  966. );
  967. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  968. }
  969. if (ctx->m2m_ctx)
  970. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  971. }
  972. static void s5p_jpeg_wait_prepare(struct vb2_queue *vq)
  973. {
  974. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  975. mutex_unlock(&ctx->jpeg->lock);
  976. }
  977. static void s5p_jpeg_wait_finish(struct vb2_queue *vq)
  978. {
  979. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  980. mutex_lock(&ctx->jpeg->lock);
  981. }
  982. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  983. {
  984. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  985. int ret;
  986. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  987. return ret > 0 ? 0 : ret;
  988. }
  989. static int s5p_jpeg_stop_streaming(struct vb2_queue *q)
  990. {
  991. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  992. pm_runtime_put(ctx->jpeg->dev);
  993. return 0;
  994. }
  995. static struct vb2_ops s5p_jpeg_qops = {
  996. .queue_setup = s5p_jpeg_queue_setup,
  997. .buf_prepare = s5p_jpeg_buf_prepare,
  998. .buf_queue = s5p_jpeg_buf_queue,
  999. .wait_prepare = s5p_jpeg_wait_prepare,
  1000. .wait_finish = s5p_jpeg_wait_finish,
  1001. .start_streaming = s5p_jpeg_start_streaming,
  1002. .stop_streaming = s5p_jpeg_stop_streaming,
  1003. };
  1004. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1005. struct vb2_queue *dst_vq)
  1006. {
  1007. struct s5p_jpeg_ctx *ctx = priv;
  1008. int ret;
  1009. memset(src_vq, 0, sizeof(*src_vq));
  1010. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1011. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1012. src_vq->drv_priv = ctx;
  1013. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1014. src_vq->ops = &s5p_jpeg_qops;
  1015. src_vq->mem_ops = &vb2_dma_contig_memops;
  1016. ret = vb2_queue_init(src_vq);
  1017. if (ret)
  1018. return ret;
  1019. memset(dst_vq, 0, sizeof(*dst_vq));
  1020. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1021. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1022. dst_vq->drv_priv = ctx;
  1023. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1024. dst_vq->ops = &s5p_jpeg_qops;
  1025. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1026. return vb2_queue_init(dst_vq);
  1027. }
  1028. /*
  1029. * ============================================================================
  1030. * ISR
  1031. * ============================================================================
  1032. */
  1033. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  1034. {
  1035. struct s5p_jpeg *jpeg = dev_id;
  1036. struct s5p_jpeg_ctx *curr_ctx;
  1037. struct vb2_buffer *src_buf, *dst_buf;
  1038. unsigned long payload_size = 0;
  1039. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  1040. bool enc_jpeg_too_large = false;
  1041. bool timer_elapsed = false;
  1042. bool op_completed = false;
  1043. spin_lock(&jpeg->slock);
  1044. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  1045. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  1046. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  1047. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1048. enc_jpeg_too_large = jpeg_enc_stream_stat(jpeg->regs);
  1049. timer_elapsed = jpeg_timer_stat(jpeg->regs);
  1050. op_completed = jpeg_result_stat_ok(jpeg->regs);
  1051. if (curr_ctx->mode == S5P_JPEG_DECODE)
  1052. op_completed = op_completed && jpeg_stream_stat_ok(jpeg->regs);
  1053. if (enc_jpeg_too_large) {
  1054. state = VB2_BUF_STATE_ERROR;
  1055. jpeg_clear_enc_stream_stat(jpeg->regs);
  1056. } else if (timer_elapsed) {
  1057. state = VB2_BUF_STATE_ERROR;
  1058. jpeg_clear_timer_stat(jpeg->regs);
  1059. } else if (!op_completed) {
  1060. state = VB2_BUF_STATE_ERROR;
  1061. } else {
  1062. payload_size = jpeg_compressed_size(jpeg->regs);
  1063. }
  1064. v4l2_m2m_buf_done(src_buf, state);
  1065. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1066. vb2_set_plane_payload(dst_buf, 0, payload_size);
  1067. v4l2_m2m_buf_done(dst_buf, state);
  1068. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->m2m_ctx);
  1069. curr_ctx->subsampling = jpeg_get_subsampling_mode(jpeg->regs);
  1070. spin_unlock(&jpeg->slock);
  1071. jpeg_clear_int(jpeg->regs);
  1072. return IRQ_HANDLED;
  1073. }
  1074. /*
  1075. * ============================================================================
  1076. * Driver basic infrastructure
  1077. * ============================================================================
  1078. */
  1079. static int s5p_jpeg_probe(struct platform_device *pdev)
  1080. {
  1081. struct s5p_jpeg *jpeg;
  1082. struct resource *res;
  1083. int ret;
  1084. /* JPEG IP abstraction struct */
  1085. jpeg = kzalloc(sizeof(struct s5p_jpeg), GFP_KERNEL);
  1086. if (!jpeg)
  1087. return -ENOMEM;
  1088. mutex_init(&jpeg->lock);
  1089. spin_lock_init(&jpeg->slock);
  1090. jpeg->dev = &pdev->dev;
  1091. /* memory-mapped registers */
  1092. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1093. if (!res) {
  1094. dev_err(&pdev->dev, "cannot find IO resource\n");
  1095. ret = -ENOENT;
  1096. goto jpeg_alloc_rollback;
  1097. }
  1098. jpeg->ioarea = request_mem_region(res->start, resource_size(res),
  1099. pdev->name);
  1100. if (!jpeg->ioarea) {
  1101. dev_err(&pdev->dev, "cannot request IO\n");
  1102. ret = -ENXIO;
  1103. goto jpeg_alloc_rollback;
  1104. }
  1105. jpeg->regs = ioremap(res->start, resource_size(res));
  1106. if (!jpeg->regs) {
  1107. dev_err(&pdev->dev, "cannot map IO\n");
  1108. ret = -ENXIO;
  1109. goto mem_region_rollback;
  1110. }
  1111. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  1112. jpeg->regs, jpeg->ioarea, res);
  1113. /* interrupt service routine registration */
  1114. jpeg->irq = ret = platform_get_irq(pdev, 0);
  1115. if (ret < 0) {
  1116. dev_err(&pdev->dev, "cannot find IRQ\n");
  1117. goto ioremap_rollback;
  1118. }
  1119. ret = request_irq(jpeg->irq, s5p_jpeg_irq, 0,
  1120. dev_name(&pdev->dev), jpeg);
  1121. if (ret) {
  1122. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  1123. goto ioremap_rollback;
  1124. }
  1125. /* clocks */
  1126. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  1127. if (IS_ERR(jpeg->clk)) {
  1128. dev_err(&pdev->dev, "cannot get clock\n");
  1129. ret = PTR_ERR(jpeg->clk);
  1130. goto request_irq_rollback;
  1131. }
  1132. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  1133. clk_enable(jpeg->clk);
  1134. /* v4l2 device */
  1135. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  1136. if (ret) {
  1137. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1138. goto clk_get_rollback;
  1139. }
  1140. /* mem2mem device */
  1141. jpeg->m2m_dev = v4l2_m2m_init(&s5p_jpeg_m2m_ops);
  1142. if (IS_ERR(jpeg->m2m_dev)) {
  1143. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  1144. ret = PTR_ERR(jpeg->m2m_dev);
  1145. goto device_register_rollback;
  1146. }
  1147. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1148. if (IS_ERR(jpeg->alloc_ctx)) {
  1149. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  1150. ret = PTR_ERR(jpeg->alloc_ctx);
  1151. goto m2m_init_rollback;
  1152. }
  1153. /* JPEG encoder /dev/videoX node */
  1154. jpeg->vfd_encoder = video_device_alloc();
  1155. if (!jpeg->vfd_encoder) {
  1156. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1157. ret = -ENOMEM;
  1158. goto vb2_allocator_rollback;
  1159. }
  1160. strlcpy(jpeg->vfd_encoder->name, S5P_JPEG_M2M_NAME,
  1161. sizeof(jpeg->vfd_encoder->name));
  1162. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  1163. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1164. jpeg->vfd_encoder->minor = -1;
  1165. jpeg->vfd_encoder->release = video_device_release;
  1166. jpeg->vfd_encoder->lock = &jpeg->lock;
  1167. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  1168. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1169. if (ret) {
  1170. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1171. goto enc_vdev_alloc_rollback;
  1172. }
  1173. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  1174. v4l2_info(&jpeg->v4l2_dev,
  1175. "encoder device registered as /dev/video%d\n",
  1176. jpeg->vfd_encoder->num);
  1177. /* JPEG decoder /dev/videoX node */
  1178. jpeg->vfd_decoder = video_device_alloc();
  1179. if (!jpeg->vfd_decoder) {
  1180. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1181. ret = -ENOMEM;
  1182. goto enc_vdev_register_rollback;
  1183. }
  1184. strlcpy(jpeg->vfd_decoder->name, S5P_JPEG_M2M_NAME,
  1185. sizeof(jpeg->vfd_decoder->name));
  1186. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  1187. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1188. jpeg->vfd_decoder->minor = -1;
  1189. jpeg->vfd_decoder->release = video_device_release;
  1190. jpeg->vfd_decoder->lock = &jpeg->lock;
  1191. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  1192. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1193. if (ret) {
  1194. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1195. goto dec_vdev_alloc_rollback;
  1196. }
  1197. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  1198. v4l2_info(&jpeg->v4l2_dev,
  1199. "decoder device registered as /dev/video%d\n",
  1200. jpeg->vfd_decoder->num);
  1201. /* final statements & power management */
  1202. platform_set_drvdata(pdev, jpeg);
  1203. pm_runtime_enable(&pdev->dev);
  1204. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  1205. return 0;
  1206. dec_vdev_alloc_rollback:
  1207. video_device_release(jpeg->vfd_decoder);
  1208. enc_vdev_register_rollback:
  1209. video_unregister_device(jpeg->vfd_encoder);
  1210. enc_vdev_alloc_rollback:
  1211. video_device_release(jpeg->vfd_encoder);
  1212. vb2_allocator_rollback:
  1213. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1214. m2m_init_rollback:
  1215. v4l2_m2m_release(jpeg->m2m_dev);
  1216. device_register_rollback:
  1217. v4l2_device_unregister(&jpeg->v4l2_dev);
  1218. clk_get_rollback:
  1219. clk_disable(jpeg->clk);
  1220. clk_put(jpeg->clk);
  1221. request_irq_rollback:
  1222. free_irq(jpeg->irq, jpeg);
  1223. ioremap_rollback:
  1224. iounmap(jpeg->regs);
  1225. mem_region_rollback:
  1226. release_resource(jpeg->ioarea);
  1227. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1228. jpeg_alloc_rollback:
  1229. kfree(jpeg);
  1230. return ret;
  1231. }
  1232. static int s5p_jpeg_remove(struct platform_device *pdev)
  1233. {
  1234. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  1235. pm_runtime_disable(jpeg->dev);
  1236. video_unregister_device(jpeg->vfd_decoder);
  1237. video_device_release(jpeg->vfd_decoder);
  1238. video_unregister_device(jpeg->vfd_encoder);
  1239. video_device_release(jpeg->vfd_encoder);
  1240. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1241. v4l2_m2m_release(jpeg->m2m_dev);
  1242. v4l2_device_unregister(&jpeg->v4l2_dev);
  1243. clk_disable(jpeg->clk);
  1244. clk_put(jpeg->clk);
  1245. free_irq(jpeg->irq, jpeg);
  1246. iounmap(jpeg->regs);
  1247. release_resource(jpeg->ioarea);
  1248. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1249. kfree(jpeg);
  1250. return 0;
  1251. }
  1252. static int s5p_jpeg_runtime_suspend(struct device *dev)
  1253. {
  1254. return 0;
  1255. }
  1256. static int s5p_jpeg_runtime_resume(struct device *dev)
  1257. {
  1258. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1259. /*
  1260. * JPEG IP allows storing two Huffman tables for each component
  1261. * We fill table 0 for each component
  1262. */
  1263. jpeg_set_hdctbl(jpeg->regs);
  1264. jpeg_set_hdctblg(jpeg->regs);
  1265. jpeg_set_hactbl(jpeg->regs);
  1266. jpeg_set_hactblg(jpeg->regs);
  1267. return 0;
  1268. }
  1269. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  1270. .runtime_suspend = s5p_jpeg_runtime_suspend,
  1271. .runtime_resume = s5p_jpeg_runtime_resume,
  1272. };
  1273. static struct platform_driver s5p_jpeg_driver = {
  1274. .probe = s5p_jpeg_probe,
  1275. .remove = s5p_jpeg_remove,
  1276. .driver = {
  1277. .owner = THIS_MODULE,
  1278. .name = S5P_JPEG_M2M_NAME,
  1279. .pm = &s5p_jpeg_pm_ops,
  1280. },
  1281. };
  1282. static int __init
  1283. s5p_jpeg_register(void)
  1284. {
  1285. int ret;
  1286. pr_info("S5P JPEG V4L2 Driver, (c) 2011 Samsung Electronics\n");
  1287. ret = platform_driver_register(&s5p_jpeg_driver);
  1288. if (ret)
  1289. pr_err("%s: failed to register jpeg driver\n", __func__);
  1290. return ret;
  1291. }
  1292. static void __exit
  1293. s5p_jpeg_unregister(void)
  1294. {
  1295. platform_driver_unregister(&s5p_jpeg_driver);
  1296. }
  1297. module_init(s5p_jpeg_register);
  1298. module_exit(s5p_jpeg_unregister);
  1299. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  1300. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  1301. MODULE_LICENSE("GPL");