jpeg-core.c 39 KB

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