jpeg-core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. /*
  440. * This is only a mem-to-mem video device. The capture and output
  441. * device capability flags are left only for backward compatibility
  442. * and are scheduled for removal.
  443. */
  444. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
  445. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
  446. return 0;
  447. }
  448. static int enum_fmt(struct s5p_jpeg_fmt *formats, int n,
  449. struct v4l2_fmtdesc *f, u32 type)
  450. {
  451. int i, num = 0;
  452. for (i = 0; i < n; ++i) {
  453. if (formats[i].types & type) {
  454. /* index-th format of type type found ? */
  455. if (num == f->index)
  456. break;
  457. /* Correct type but haven't reached our index yet,
  458. * just increment per-type index */
  459. ++num;
  460. }
  461. }
  462. /* Format not found */
  463. if (i >= n)
  464. return -EINVAL;
  465. strlcpy(f->description, formats[i].name, sizeof(f->description));
  466. f->pixelformat = formats[i].fourcc;
  467. return 0;
  468. }
  469. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  470. struct v4l2_fmtdesc *f)
  471. {
  472. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  473. if (ctx->mode == S5P_JPEG_ENCODE)
  474. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  475. MEM2MEM_CAPTURE);
  476. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_CAPTURE);
  477. }
  478. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  479. struct v4l2_fmtdesc *f)
  480. {
  481. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  482. if (ctx->mode == S5P_JPEG_ENCODE)
  483. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  484. MEM2MEM_OUTPUT);
  485. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_OUTPUT);
  486. }
  487. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  488. enum v4l2_buf_type type)
  489. {
  490. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  491. return &ctx->out_q;
  492. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  493. return &ctx->cap_q;
  494. return NULL;
  495. }
  496. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  497. {
  498. struct vb2_queue *vq;
  499. struct s5p_jpeg_q_data *q_data = NULL;
  500. struct v4l2_pix_format *pix = &f->fmt.pix;
  501. struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
  502. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  503. if (!vq)
  504. return -EINVAL;
  505. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  506. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  507. return -EINVAL;
  508. q_data = get_q_data(ct, f->type);
  509. BUG_ON(q_data == NULL);
  510. pix->width = q_data->w;
  511. pix->height = q_data->h;
  512. pix->field = V4L2_FIELD_NONE;
  513. pix->pixelformat = q_data->fmt->fourcc;
  514. pix->bytesperline = 0;
  515. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  516. u32 bpl = q_data->w;
  517. if (q_data->fmt->colplanes == 1)
  518. bpl = (bpl * q_data->fmt->depth) >> 3;
  519. pix->bytesperline = bpl;
  520. }
  521. pix->sizeimage = q_data->size;
  522. return 0;
  523. }
  524. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(unsigned int mode,
  525. u32 pixelformat)
  526. {
  527. unsigned int k;
  528. struct s5p_jpeg_fmt *formats;
  529. int n;
  530. if (mode == S5P_JPEG_ENCODE) {
  531. formats = formats_enc;
  532. n = NUM_FORMATS_ENC;
  533. } else {
  534. formats = formats_dec;
  535. n = NUM_FORMATS_DEC;
  536. }
  537. for (k = 0; k < n; k++) {
  538. struct s5p_jpeg_fmt *fmt = &formats[k];
  539. if (fmt->fourcc == pixelformat)
  540. return fmt;
  541. }
  542. return NULL;
  543. }
  544. static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  545. unsigned int walign,
  546. u32 *h, unsigned int hmin, unsigned int hmax,
  547. unsigned int halign)
  548. {
  549. int width, height, w_step, h_step;
  550. width = *w;
  551. height = *h;
  552. w_step = 1 << walign;
  553. h_step = 1 << halign;
  554. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  555. if (*w < width && (*w + w_step) < wmax)
  556. *w += w_step;
  557. if (*h < height && (*h + h_step) < hmax)
  558. *h += h_step;
  559. }
  560. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  561. struct s5p_jpeg_ctx *ctx, int q_type)
  562. {
  563. struct v4l2_pix_format *pix = &f->fmt.pix;
  564. if (pix->field == V4L2_FIELD_ANY)
  565. pix->field = V4L2_FIELD_NONE;
  566. else if (pix->field != V4L2_FIELD_NONE)
  567. return -EINVAL;
  568. /* V4L2 specification suggests the driver corrects the format struct
  569. * if any of the dimensions is unsupported */
  570. if (q_type == MEM2MEM_OUTPUT)
  571. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  572. S5P_JPEG_MAX_WIDTH, 0,
  573. &pix->height, S5P_JPEG_MIN_HEIGHT,
  574. S5P_JPEG_MAX_HEIGHT, 0);
  575. else
  576. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  577. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  578. &pix->height, S5P_JPEG_MIN_HEIGHT,
  579. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  580. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  581. if (pix->sizeimage <= 0)
  582. pix->sizeimage = PAGE_SIZE;
  583. pix->bytesperline = 0;
  584. } else {
  585. u32 bpl = pix->bytesperline;
  586. if (fmt->colplanes > 1 && bpl < pix->width)
  587. bpl = pix->width; /* planar */
  588. if (fmt->colplanes == 1 && /* packed */
  589. (bpl << 3) * fmt->depth < pix->width)
  590. bpl = (pix->width * fmt->depth) >> 3;
  591. pix->bytesperline = bpl;
  592. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  593. }
  594. return 0;
  595. }
  596. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  597. struct v4l2_format *f)
  598. {
  599. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  600. struct s5p_jpeg_fmt *fmt;
  601. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  602. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  603. v4l2_err(&ctx->jpeg->v4l2_dev,
  604. "Fourcc format (0x%08x) invalid.\n",
  605. f->fmt.pix.pixelformat);
  606. return -EINVAL;
  607. }
  608. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_CAPTURE);
  609. }
  610. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  611. struct v4l2_format *f)
  612. {
  613. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  614. struct s5p_jpeg_fmt *fmt;
  615. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  616. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  617. v4l2_err(&ctx->jpeg->v4l2_dev,
  618. "Fourcc format (0x%08x) invalid.\n",
  619. f->fmt.pix.pixelformat);
  620. return -EINVAL;
  621. }
  622. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_OUTPUT);
  623. }
  624. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  625. {
  626. struct vb2_queue *vq;
  627. struct s5p_jpeg_q_data *q_data = NULL;
  628. struct v4l2_pix_format *pix = &f->fmt.pix;
  629. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  630. if (!vq)
  631. return -EINVAL;
  632. q_data = get_q_data(ct, f->type);
  633. BUG_ON(q_data == NULL);
  634. if (vb2_is_busy(vq)) {
  635. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  636. return -EBUSY;
  637. }
  638. q_data->fmt = s5p_jpeg_find_format(ct->mode, pix->pixelformat);
  639. q_data->w = pix->width;
  640. q_data->h = pix->height;
  641. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG)
  642. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  643. else
  644. q_data->size = pix->sizeimage;
  645. return 0;
  646. }
  647. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  648. struct v4l2_format *f)
  649. {
  650. int ret;
  651. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  652. if (ret)
  653. return ret;
  654. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  655. }
  656. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  657. struct v4l2_format *f)
  658. {
  659. int ret;
  660. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  661. if (ret)
  662. return ret;
  663. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  664. }
  665. static int s5p_jpeg_reqbufs(struct file *file, void *priv,
  666. struct v4l2_requestbuffers *reqbufs)
  667. {
  668. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  669. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  670. }
  671. static int s5p_jpeg_querybuf(struct file *file, void *priv,
  672. struct v4l2_buffer *buf)
  673. {
  674. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  675. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  676. }
  677. static int s5p_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  678. {
  679. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  680. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  681. }
  682. static int s5p_jpeg_dqbuf(struct file *file, void *priv,
  683. struct v4l2_buffer *buf)
  684. {
  685. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  686. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  687. }
  688. static int s5p_jpeg_streamon(struct file *file, void *priv,
  689. enum v4l2_buf_type type)
  690. {
  691. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  692. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  693. }
  694. static int s5p_jpeg_streamoff(struct file *file, void *priv,
  695. enum v4l2_buf_type type)
  696. {
  697. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  698. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  699. }
  700. static int s5p_jpeg_g_selection(struct file *file, void *priv,
  701. struct v4l2_selection *s)
  702. {
  703. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  704. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  705. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  706. return -EINVAL;
  707. /* For JPEG blob active == default == bounds */
  708. switch (s->target) {
  709. case V4L2_SEL_TGT_CROP:
  710. case V4L2_SEL_TGT_CROP_BOUNDS:
  711. case V4L2_SEL_TGT_CROP_DEFAULT:
  712. case V4L2_SEL_TGT_COMPOSE:
  713. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  714. s->r.width = ctx->out_q.w;
  715. s->r.height = ctx->out_q.h;
  716. break;
  717. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  718. case V4L2_SEL_TGT_COMPOSE_PADDED:
  719. s->r.width = ctx->cap_q.w;
  720. s->r.height = ctx->cap_q.h;
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. s->r.left = 0;
  726. s->r.top = 0;
  727. return 0;
  728. }
  729. /*
  730. * V4L2 controls
  731. */
  732. static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  733. {
  734. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  735. struct s5p_jpeg *jpeg = ctx->jpeg;
  736. unsigned long flags;
  737. switch (ctrl->id) {
  738. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  739. spin_lock_irqsave(&jpeg->slock, flags);
  740. WARN_ON(ctx->subsampling > S5P_SUBSAMPLING_MODE_GRAY);
  741. if (ctx->subsampling > 2)
  742. ctrl->val = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  743. else
  744. ctrl->val = ctx->subsampling;
  745. spin_unlock_irqrestore(&jpeg->slock, flags);
  746. break;
  747. }
  748. return 0;
  749. }
  750. static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
  751. {
  752. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  753. unsigned long flags;
  754. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  755. switch (ctrl->id) {
  756. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  757. ctx->compr_quality = S5P_JPEG_COMPR_QUAL_WORST - ctrl->val;
  758. break;
  759. case V4L2_CID_JPEG_RESTART_INTERVAL:
  760. ctx->restart_interval = ctrl->val;
  761. break;
  762. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  763. ctx->subsampling = ctrl->val;
  764. break;
  765. }
  766. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  767. return 0;
  768. }
  769. static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
  770. .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
  771. .s_ctrl = s5p_jpeg_s_ctrl,
  772. };
  773. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
  774. {
  775. unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
  776. struct v4l2_ctrl *ctrl;
  777. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  778. if (ctx->mode == S5P_JPEG_ENCODE) {
  779. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  780. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  781. 0, 3, 1, 3);
  782. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  783. V4L2_CID_JPEG_RESTART_INTERVAL,
  784. 0, 3, 0xffff, 0);
  785. mask = ~0x06; /* 422, 420 */
  786. }
  787. ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  788. V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
  789. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
  790. V4L2_JPEG_CHROMA_SUBSAMPLING_422);
  791. if (ctx->ctrl_handler.error)
  792. return ctx->ctrl_handler.error;
  793. if (ctx->mode == S5P_JPEG_DECODE)
  794. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  795. V4L2_CTRL_FLAG_READ_ONLY;
  796. return 0;
  797. }
  798. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  799. .vidioc_querycap = s5p_jpeg_querycap,
  800. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  801. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  802. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  803. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  804. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  805. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  806. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  807. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  808. .vidioc_reqbufs = s5p_jpeg_reqbufs,
  809. .vidioc_querybuf = s5p_jpeg_querybuf,
  810. .vidioc_qbuf = s5p_jpeg_qbuf,
  811. .vidioc_dqbuf = s5p_jpeg_dqbuf,
  812. .vidioc_streamon = s5p_jpeg_streamon,
  813. .vidioc_streamoff = s5p_jpeg_streamoff,
  814. .vidioc_g_selection = s5p_jpeg_g_selection,
  815. };
  816. /*
  817. * ============================================================================
  818. * mem2mem callbacks
  819. * ============================================================================
  820. */
  821. static void s5p_jpeg_device_run(void *priv)
  822. {
  823. struct s5p_jpeg_ctx *ctx = priv;
  824. struct s5p_jpeg *jpeg = ctx->jpeg;
  825. struct vb2_buffer *src_buf, *dst_buf;
  826. unsigned long src_addr, dst_addr;
  827. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  828. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  829. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  830. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  831. jpeg_reset(jpeg->regs);
  832. jpeg_poweron(jpeg->regs);
  833. jpeg_proc_mode(jpeg->regs, ctx->mode);
  834. if (ctx->mode == S5P_JPEG_ENCODE) {
  835. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  836. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_565);
  837. else
  838. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_422);
  839. jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  840. jpeg_dri(jpeg->regs, ctx->restart_interval);
  841. jpeg_x(jpeg->regs, ctx->out_q.w);
  842. jpeg_y(jpeg->regs, ctx->out_q.h);
  843. jpeg_imgadr(jpeg->regs, src_addr);
  844. jpeg_jpgadr(jpeg->regs, dst_addr);
  845. /* ultimately comes from sizeimage from userspace */
  846. jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  847. /* JPEG RGB to YCbCr conversion matrix */
  848. jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  849. jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  850. jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  851. jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  852. jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  853. jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  854. jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  855. jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  856. jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  857. /*
  858. * JPEG IP allows storing 4 quantization tables
  859. * We fill table 0 for luma and table 1 for chroma
  860. */
  861. jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  862. jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  863. /* use table 0 for Y */
  864. jpeg_qtbl(jpeg->regs, 1, 0);
  865. /* use table 1 for Cb and Cr*/
  866. jpeg_qtbl(jpeg->regs, 2, 1);
  867. jpeg_qtbl(jpeg->regs, 3, 1);
  868. /* Y, Cb, Cr use Huffman table 0 */
  869. jpeg_htbl_ac(jpeg->regs, 1);
  870. jpeg_htbl_dc(jpeg->regs, 1);
  871. jpeg_htbl_ac(jpeg->regs, 2);
  872. jpeg_htbl_dc(jpeg->regs, 2);
  873. jpeg_htbl_ac(jpeg->regs, 3);
  874. jpeg_htbl_dc(jpeg->regs, 3);
  875. } else { /* S5P_JPEG_DECODE */
  876. jpeg_rst_int_enable(jpeg->regs, true);
  877. jpeg_data_num_int_enable(jpeg->regs, true);
  878. jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  879. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  880. jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  881. else
  882. jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
  883. jpeg_jpgadr(jpeg->regs, src_addr);
  884. jpeg_imgadr(jpeg->regs, dst_addr);
  885. }
  886. jpeg_start(jpeg->regs);
  887. }
  888. static int s5p_jpeg_job_ready(void *priv)
  889. {
  890. struct s5p_jpeg_ctx *ctx = priv;
  891. if (ctx->mode == S5P_JPEG_DECODE)
  892. return ctx->hdr_parsed;
  893. return 1;
  894. }
  895. static void s5p_jpeg_job_abort(void *priv)
  896. {
  897. }
  898. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  899. .device_run = s5p_jpeg_device_run,
  900. .job_ready = s5p_jpeg_job_ready,
  901. .job_abort = s5p_jpeg_job_abort,
  902. };
  903. /*
  904. * ============================================================================
  905. * Queue operations
  906. * ============================================================================
  907. */
  908. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  909. const struct v4l2_format *fmt,
  910. unsigned int *nbuffers, unsigned int *nplanes,
  911. unsigned int sizes[], void *alloc_ctxs[])
  912. {
  913. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  914. struct s5p_jpeg_q_data *q_data = NULL;
  915. unsigned int size, count = *nbuffers;
  916. q_data = get_q_data(ctx, vq->type);
  917. BUG_ON(q_data == NULL);
  918. size = q_data->size;
  919. /*
  920. * header is parsed during decoding and parsed information stored
  921. * in the context so we do not allow another buffer to overwrite it
  922. */
  923. if (ctx->mode == S5P_JPEG_DECODE)
  924. count = 1;
  925. *nbuffers = count;
  926. *nplanes = 1;
  927. sizes[0] = size;
  928. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  929. return 0;
  930. }
  931. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  932. {
  933. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  934. struct s5p_jpeg_q_data *q_data = NULL;
  935. q_data = get_q_data(ctx, vb->vb2_queue->type);
  936. BUG_ON(q_data == NULL);
  937. if (vb2_plane_size(vb, 0) < q_data->size) {
  938. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  939. __func__, vb2_plane_size(vb, 0),
  940. (long)q_data->size);
  941. return -EINVAL;
  942. }
  943. vb2_set_plane_payload(vb, 0, q_data->size);
  944. return 0;
  945. }
  946. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  947. {
  948. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  949. if (ctx->mode == S5P_JPEG_DECODE &&
  950. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  951. struct s5p_jpeg_q_data tmp, *q_data;
  952. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  953. (unsigned long)vb2_plane_vaddr(vb, 0),
  954. min((unsigned long)ctx->out_q.size,
  955. vb2_get_plane_payload(vb, 0)));
  956. if (!ctx->hdr_parsed) {
  957. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  958. return;
  959. }
  960. q_data = &ctx->out_q;
  961. q_data->w = tmp.w;
  962. q_data->h = tmp.h;
  963. q_data = &ctx->cap_q;
  964. q_data->w = tmp.w;
  965. q_data->h = tmp.h;
  966. jpeg_bound_align_image(&q_data->w, S5P_JPEG_MIN_WIDTH,
  967. S5P_JPEG_MAX_WIDTH, q_data->fmt->h_align,
  968. &q_data->h, S5P_JPEG_MIN_HEIGHT,
  969. S5P_JPEG_MAX_HEIGHT, q_data->fmt->v_align
  970. );
  971. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  972. }
  973. if (ctx->m2m_ctx)
  974. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  975. }
  976. static void s5p_jpeg_wait_prepare(struct vb2_queue *vq)
  977. {
  978. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  979. mutex_unlock(&ctx->jpeg->lock);
  980. }
  981. static void s5p_jpeg_wait_finish(struct vb2_queue *vq)
  982. {
  983. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  984. mutex_lock(&ctx->jpeg->lock);
  985. }
  986. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  987. {
  988. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  989. int ret;
  990. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  991. return ret > 0 ? 0 : ret;
  992. }
  993. static int s5p_jpeg_stop_streaming(struct vb2_queue *q)
  994. {
  995. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  996. pm_runtime_put(ctx->jpeg->dev);
  997. return 0;
  998. }
  999. static struct vb2_ops s5p_jpeg_qops = {
  1000. .queue_setup = s5p_jpeg_queue_setup,
  1001. .buf_prepare = s5p_jpeg_buf_prepare,
  1002. .buf_queue = s5p_jpeg_buf_queue,
  1003. .wait_prepare = s5p_jpeg_wait_prepare,
  1004. .wait_finish = s5p_jpeg_wait_finish,
  1005. .start_streaming = s5p_jpeg_start_streaming,
  1006. .stop_streaming = s5p_jpeg_stop_streaming,
  1007. };
  1008. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1009. struct vb2_queue *dst_vq)
  1010. {
  1011. struct s5p_jpeg_ctx *ctx = priv;
  1012. int ret;
  1013. memset(src_vq, 0, sizeof(*src_vq));
  1014. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1015. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1016. src_vq->drv_priv = ctx;
  1017. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1018. src_vq->ops = &s5p_jpeg_qops;
  1019. src_vq->mem_ops = &vb2_dma_contig_memops;
  1020. ret = vb2_queue_init(src_vq);
  1021. if (ret)
  1022. return ret;
  1023. memset(dst_vq, 0, sizeof(*dst_vq));
  1024. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1025. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1026. dst_vq->drv_priv = ctx;
  1027. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1028. dst_vq->ops = &s5p_jpeg_qops;
  1029. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1030. return vb2_queue_init(dst_vq);
  1031. }
  1032. /*
  1033. * ============================================================================
  1034. * ISR
  1035. * ============================================================================
  1036. */
  1037. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  1038. {
  1039. struct s5p_jpeg *jpeg = dev_id;
  1040. struct s5p_jpeg_ctx *curr_ctx;
  1041. struct vb2_buffer *src_buf, *dst_buf;
  1042. unsigned long payload_size = 0;
  1043. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  1044. bool enc_jpeg_too_large = false;
  1045. bool timer_elapsed = false;
  1046. bool op_completed = false;
  1047. spin_lock(&jpeg->slock);
  1048. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  1049. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  1050. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  1051. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1052. enc_jpeg_too_large = jpeg_enc_stream_stat(jpeg->regs);
  1053. timer_elapsed = jpeg_timer_stat(jpeg->regs);
  1054. op_completed = jpeg_result_stat_ok(jpeg->regs);
  1055. if (curr_ctx->mode == S5P_JPEG_DECODE)
  1056. op_completed = op_completed && jpeg_stream_stat_ok(jpeg->regs);
  1057. if (enc_jpeg_too_large) {
  1058. state = VB2_BUF_STATE_ERROR;
  1059. jpeg_clear_enc_stream_stat(jpeg->regs);
  1060. } else if (timer_elapsed) {
  1061. state = VB2_BUF_STATE_ERROR;
  1062. jpeg_clear_timer_stat(jpeg->regs);
  1063. } else if (!op_completed) {
  1064. state = VB2_BUF_STATE_ERROR;
  1065. } else {
  1066. payload_size = jpeg_compressed_size(jpeg->regs);
  1067. }
  1068. v4l2_m2m_buf_done(src_buf, state);
  1069. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1070. vb2_set_plane_payload(dst_buf, 0, payload_size);
  1071. v4l2_m2m_buf_done(dst_buf, state);
  1072. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->m2m_ctx);
  1073. curr_ctx->subsampling = jpeg_get_subsampling_mode(jpeg->regs);
  1074. spin_unlock(&jpeg->slock);
  1075. jpeg_clear_int(jpeg->regs);
  1076. return IRQ_HANDLED;
  1077. }
  1078. /*
  1079. * ============================================================================
  1080. * Driver basic infrastructure
  1081. * ============================================================================
  1082. */
  1083. static int s5p_jpeg_probe(struct platform_device *pdev)
  1084. {
  1085. struct s5p_jpeg *jpeg;
  1086. struct resource *res;
  1087. int ret;
  1088. /* JPEG IP abstraction struct */
  1089. jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
  1090. if (!jpeg)
  1091. return -ENOMEM;
  1092. mutex_init(&jpeg->lock);
  1093. spin_lock_init(&jpeg->slock);
  1094. jpeg->dev = &pdev->dev;
  1095. /* memory-mapped registers */
  1096. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1097. jpeg->regs = devm_request_and_ioremap(&pdev->dev, res);
  1098. if (jpeg->regs == NULL) {
  1099. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1100. return -ENOENT;
  1101. }
  1102. /* interrupt service routine registration */
  1103. jpeg->irq = ret = platform_get_irq(pdev, 0);
  1104. if (ret < 0) {
  1105. dev_err(&pdev->dev, "cannot find IRQ\n");
  1106. return ret;
  1107. }
  1108. ret = devm_request_irq(&pdev->dev, jpeg->irq, s5p_jpeg_irq, 0,
  1109. dev_name(&pdev->dev), jpeg);
  1110. if (ret) {
  1111. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  1112. return ret;
  1113. }
  1114. /* clocks */
  1115. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  1116. if (IS_ERR(jpeg->clk)) {
  1117. dev_err(&pdev->dev, "cannot get clock\n");
  1118. ret = PTR_ERR(jpeg->clk);
  1119. return ret;
  1120. }
  1121. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  1122. clk_enable(jpeg->clk);
  1123. /* v4l2 device */
  1124. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  1125. if (ret) {
  1126. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1127. goto clk_get_rollback;
  1128. }
  1129. /* mem2mem device */
  1130. jpeg->m2m_dev = v4l2_m2m_init(&s5p_jpeg_m2m_ops);
  1131. if (IS_ERR(jpeg->m2m_dev)) {
  1132. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  1133. ret = PTR_ERR(jpeg->m2m_dev);
  1134. goto device_register_rollback;
  1135. }
  1136. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1137. if (IS_ERR(jpeg->alloc_ctx)) {
  1138. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  1139. ret = PTR_ERR(jpeg->alloc_ctx);
  1140. goto m2m_init_rollback;
  1141. }
  1142. /* JPEG encoder /dev/videoX node */
  1143. jpeg->vfd_encoder = video_device_alloc();
  1144. if (!jpeg->vfd_encoder) {
  1145. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1146. ret = -ENOMEM;
  1147. goto vb2_allocator_rollback;
  1148. }
  1149. strlcpy(jpeg->vfd_encoder->name, S5P_JPEG_M2M_NAME,
  1150. sizeof(jpeg->vfd_encoder->name));
  1151. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  1152. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1153. jpeg->vfd_encoder->minor = -1;
  1154. jpeg->vfd_encoder->release = video_device_release;
  1155. jpeg->vfd_encoder->lock = &jpeg->lock;
  1156. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  1157. /* Locking in file operations other than ioctl should be done
  1158. by the driver, not the V4L2 core.
  1159. This driver needs auditing so that this flag can be removed. */
  1160. set_bit(V4L2_FL_LOCK_ALL_FOPS, &jpeg->vfd_encoder->flags);
  1161. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1162. if (ret) {
  1163. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1164. goto enc_vdev_alloc_rollback;
  1165. }
  1166. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  1167. v4l2_info(&jpeg->v4l2_dev,
  1168. "encoder device registered as /dev/video%d\n",
  1169. jpeg->vfd_encoder->num);
  1170. /* JPEG decoder /dev/videoX node */
  1171. jpeg->vfd_decoder = video_device_alloc();
  1172. if (!jpeg->vfd_decoder) {
  1173. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1174. ret = -ENOMEM;
  1175. goto enc_vdev_register_rollback;
  1176. }
  1177. strlcpy(jpeg->vfd_decoder->name, S5P_JPEG_M2M_NAME,
  1178. sizeof(jpeg->vfd_decoder->name));
  1179. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  1180. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1181. jpeg->vfd_decoder->minor = -1;
  1182. jpeg->vfd_decoder->release = video_device_release;
  1183. jpeg->vfd_decoder->lock = &jpeg->lock;
  1184. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  1185. /* Locking in file operations other than ioctl should be done by the driver,
  1186. not the V4L2 core.
  1187. This driver needs auditing so that this flag can be removed. */
  1188. set_bit(V4L2_FL_LOCK_ALL_FOPS, &jpeg->vfd_decoder->flags);
  1189. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1190. if (ret) {
  1191. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1192. goto dec_vdev_alloc_rollback;
  1193. }
  1194. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  1195. v4l2_info(&jpeg->v4l2_dev,
  1196. "decoder device registered as /dev/video%d\n",
  1197. jpeg->vfd_decoder->num);
  1198. /* final statements & power management */
  1199. platform_set_drvdata(pdev, jpeg);
  1200. pm_runtime_enable(&pdev->dev);
  1201. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  1202. return 0;
  1203. dec_vdev_alloc_rollback:
  1204. video_device_release(jpeg->vfd_decoder);
  1205. enc_vdev_register_rollback:
  1206. video_unregister_device(jpeg->vfd_encoder);
  1207. enc_vdev_alloc_rollback:
  1208. video_device_release(jpeg->vfd_encoder);
  1209. vb2_allocator_rollback:
  1210. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1211. m2m_init_rollback:
  1212. v4l2_m2m_release(jpeg->m2m_dev);
  1213. device_register_rollback:
  1214. v4l2_device_unregister(&jpeg->v4l2_dev);
  1215. clk_get_rollback:
  1216. clk_disable(jpeg->clk);
  1217. clk_put(jpeg->clk);
  1218. return ret;
  1219. }
  1220. static int s5p_jpeg_remove(struct platform_device *pdev)
  1221. {
  1222. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  1223. pm_runtime_disable(jpeg->dev);
  1224. video_unregister_device(jpeg->vfd_decoder);
  1225. video_device_release(jpeg->vfd_decoder);
  1226. video_unregister_device(jpeg->vfd_encoder);
  1227. video_device_release(jpeg->vfd_encoder);
  1228. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1229. v4l2_m2m_release(jpeg->m2m_dev);
  1230. v4l2_device_unregister(&jpeg->v4l2_dev);
  1231. clk_disable(jpeg->clk);
  1232. clk_put(jpeg->clk);
  1233. return 0;
  1234. }
  1235. static int s5p_jpeg_runtime_suspend(struct device *dev)
  1236. {
  1237. return 0;
  1238. }
  1239. static int s5p_jpeg_runtime_resume(struct device *dev)
  1240. {
  1241. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1242. /*
  1243. * JPEG IP allows storing two Huffman tables for each component
  1244. * We fill table 0 for each component
  1245. */
  1246. jpeg_set_hdctbl(jpeg->regs);
  1247. jpeg_set_hdctblg(jpeg->regs);
  1248. jpeg_set_hactbl(jpeg->regs);
  1249. jpeg_set_hactblg(jpeg->regs);
  1250. return 0;
  1251. }
  1252. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  1253. .runtime_suspend = s5p_jpeg_runtime_suspend,
  1254. .runtime_resume = s5p_jpeg_runtime_resume,
  1255. };
  1256. static struct platform_driver s5p_jpeg_driver = {
  1257. .probe = s5p_jpeg_probe,
  1258. .remove = s5p_jpeg_remove,
  1259. .driver = {
  1260. .owner = THIS_MODULE,
  1261. .name = S5P_JPEG_M2M_NAME,
  1262. .pm = &s5p_jpeg_pm_ops,
  1263. },
  1264. };
  1265. module_platform_driver(s5p_jpeg_driver);
  1266. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  1267. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  1268. MODULE_LICENSE("GPL");