jpeg-core.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 void jpeg_set_qtbl(void __iomem *regs, const unsigned char *qtbl,
  198. unsigned long tab, int len)
  199. {
  200. int i;
  201. for (i = 0; i < len; i++)
  202. writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
  203. }
  204. static inline void jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  205. {
  206. /* this driver fills quantisation table 0 with data for luma */
  207. jpeg_set_qtbl(regs, qtbl_luminance[quality], S5P_JPG_QTBL_CONTENT(0),
  208. ARRAY_SIZE(qtbl_luminance[quality]));
  209. }
  210. static inline void jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  211. {
  212. /* this driver fills quantisation table 1 with data for chroma */
  213. jpeg_set_qtbl(regs, qtbl_chrominance[quality], S5P_JPG_QTBL_CONTENT(1),
  214. ARRAY_SIZE(qtbl_chrominance[quality]));
  215. }
  216. static inline void jpeg_set_htbl(void __iomem *regs, const unsigned char *htbl,
  217. unsigned long tab, int len)
  218. {
  219. int i;
  220. for (i = 0; i < len; i++)
  221. writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
  222. }
  223. static inline void jpeg_set_hdctbl(void __iomem *regs)
  224. {
  225. /* this driver fills table 0 for this component */
  226. jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0), ARRAY_SIZE(hdctbl0));
  227. }
  228. static inline void jpeg_set_hdctblg(void __iomem *regs)
  229. {
  230. /* this driver fills table 0 for this component */
  231. jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0), ARRAY_SIZE(hdctblg0));
  232. }
  233. static inline void jpeg_set_hactbl(void __iomem *regs)
  234. {
  235. /* this driver fills table 0 for this component */
  236. jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0), ARRAY_SIZE(hactbl0));
  237. }
  238. static inline void jpeg_set_hactblg(void __iomem *regs)
  239. {
  240. /* this driver fills table 0 for this component */
  241. jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0), ARRAY_SIZE(hactblg0));
  242. }
  243. /*
  244. * ============================================================================
  245. * Device file operations
  246. * ============================================================================
  247. */
  248. static int queue_init(void *priv, struct vb2_queue *src_vq,
  249. struct vb2_queue *dst_vq);
  250. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(unsigned int mode,
  251. __u32 pixelformat);
  252. static int s5p_jpeg_open(struct file *file)
  253. {
  254. struct s5p_jpeg *jpeg = video_drvdata(file);
  255. struct video_device *vfd = video_devdata(file);
  256. struct s5p_jpeg_ctx *ctx;
  257. struct s5p_jpeg_fmt *out_fmt;
  258. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  259. if (!ctx)
  260. return -ENOMEM;
  261. file->private_data = ctx;
  262. ctx->jpeg = jpeg;
  263. if (vfd == jpeg->vfd_encoder) {
  264. ctx->mode = S5P_JPEG_ENCODE;
  265. out_fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_RGB565);
  266. } else {
  267. ctx->mode = S5P_JPEG_DECODE;
  268. out_fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_JPEG);
  269. }
  270. ctx->m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
  271. if (IS_ERR(ctx->m2m_ctx)) {
  272. int err = PTR_ERR(ctx->m2m_ctx);
  273. kfree(ctx);
  274. return err;
  275. }
  276. ctx->out_q.fmt = out_fmt;
  277. ctx->cap_q.fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_YUYV);
  278. return 0;
  279. }
  280. static int s5p_jpeg_release(struct file *file)
  281. {
  282. struct s5p_jpeg_ctx *ctx = file->private_data;
  283. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  284. kfree(ctx);
  285. return 0;
  286. }
  287. static unsigned int s5p_jpeg_poll(struct file *file,
  288. struct poll_table_struct *wait)
  289. {
  290. struct s5p_jpeg_ctx *ctx = file->private_data;
  291. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  292. }
  293. static int s5p_jpeg_mmap(struct file *file, struct vm_area_struct *vma)
  294. {
  295. struct s5p_jpeg_ctx *ctx = file->private_data;
  296. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  297. }
  298. static const struct v4l2_file_operations s5p_jpeg_fops = {
  299. .owner = THIS_MODULE,
  300. .open = s5p_jpeg_open,
  301. .release = s5p_jpeg_release,
  302. .poll = s5p_jpeg_poll,
  303. .unlocked_ioctl = video_ioctl2,
  304. .mmap = s5p_jpeg_mmap,
  305. };
  306. /*
  307. * ============================================================================
  308. * video ioctl operations
  309. * ============================================================================
  310. */
  311. static int get_byte(struct s5p_jpeg_buffer *buf)
  312. {
  313. if (buf->curr >= buf->size)
  314. return -1;
  315. return ((unsigned char *)buf->data)[buf->curr++];
  316. }
  317. static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
  318. {
  319. unsigned int temp;
  320. int byte;
  321. byte = get_byte(buf);
  322. if (byte == -1)
  323. return -1;
  324. temp = byte << 8;
  325. byte = get_byte(buf);
  326. if (byte == -1)
  327. return -1;
  328. *word = (unsigned int)byte | temp;
  329. return 0;
  330. }
  331. static void skip(struct s5p_jpeg_buffer *buf, long len)
  332. {
  333. if (len <= 0)
  334. return;
  335. while (len--)
  336. get_byte(buf);
  337. }
  338. static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
  339. unsigned long buffer, unsigned long size)
  340. {
  341. int c, components, notfound;
  342. unsigned int height, width, word;
  343. long length;
  344. struct s5p_jpeg_buffer jpeg_buffer;
  345. jpeg_buffer.size = size;
  346. jpeg_buffer.data = buffer;
  347. jpeg_buffer.curr = 0;
  348. notfound = 1;
  349. while (notfound) {
  350. c = get_byte(&jpeg_buffer);
  351. if (c == -1)
  352. break;
  353. if (c != 0xff)
  354. continue;
  355. do
  356. c = get_byte(&jpeg_buffer);
  357. while (c == 0xff);
  358. if (c == -1)
  359. break;
  360. if (c == 0)
  361. continue;
  362. length = 0;
  363. switch (c) {
  364. /* SOF0: baseline JPEG */
  365. case SOF0:
  366. if (get_word_be(&jpeg_buffer, &word))
  367. break;
  368. if (get_byte(&jpeg_buffer) == -1)
  369. break;
  370. if (get_word_be(&jpeg_buffer, &height))
  371. break;
  372. if (get_word_be(&jpeg_buffer, &width))
  373. break;
  374. components = get_byte(&jpeg_buffer);
  375. if (components == -1)
  376. break;
  377. notfound = 0;
  378. skip(&jpeg_buffer, components * 3);
  379. break;
  380. /* skip payload-less markers */
  381. case RST ... RST + 7:
  382. case SOI:
  383. case EOI:
  384. case TEM:
  385. break;
  386. /* skip uninteresting payload markers */
  387. default:
  388. if (get_word_be(&jpeg_buffer, &word))
  389. break;
  390. length = (long)word - 2;
  391. skip(&jpeg_buffer, length);
  392. break;
  393. }
  394. }
  395. result->w = width;
  396. result->h = height;
  397. result->size = components;
  398. return !notfound;
  399. }
  400. static int s5p_jpeg_querycap(struct file *file, void *priv,
  401. struct v4l2_capability *cap)
  402. {
  403. struct s5p_jpeg_ctx *ctx = priv;
  404. if (ctx->mode == S5P_JPEG_ENCODE) {
  405. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
  406. sizeof(cap->driver));
  407. strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
  408. sizeof(cap->card));
  409. } else {
  410. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " decoder",
  411. sizeof(cap->driver));
  412. strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
  413. sizeof(cap->card));
  414. }
  415. cap->bus_info[0] = 0;
  416. cap->capabilities = V4L2_CAP_STREAMING |
  417. V4L2_CAP_VIDEO_CAPTURE |
  418. V4L2_CAP_VIDEO_OUTPUT;
  419. return 0;
  420. }
  421. static int enum_fmt(struct s5p_jpeg_fmt *formats, int n,
  422. struct v4l2_fmtdesc *f, u32 type)
  423. {
  424. int i, num = 0;
  425. for (i = 0; i < n; ++i) {
  426. if (formats[i].types & type) {
  427. /* index-th format of type type found ? */
  428. if (num == f->index)
  429. break;
  430. /* Correct type but haven't reached our index yet,
  431. * just increment per-type index */
  432. ++num;
  433. }
  434. }
  435. /* Format not found */
  436. if (i >= n)
  437. return -EINVAL;
  438. strlcpy(f->description, formats[i].name, sizeof(f->description));
  439. f->pixelformat = formats[i].fourcc;
  440. return 0;
  441. }
  442. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  443. struct v4l2_fmtdesc *f)
  444. {
  445. struct s5p_jpeg_ctx *ctx;
  446. ctx = priv;
  447. if (ctx->mode == S5P_JPEG_ENCODE)
  448. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  449. MEM2MEM_CAPTURE);
  450. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_CAPTURE);
  451. }
  452. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  453. struct v4l2_fmtdesc *f)
  454. {
  455. struct s5p_jpeg_ctx *ctx;
  456. ctx = priv;
  457. if (ctx->mode == S5P_JPEG_ENCODE)
  458. return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
  459. MEM2MEM_OUTPUT);
  460. return enum_fmt(formats_dec, NUM_FORMATS_DEC, f, MEM2MEM_OUTPUT);
  461. }
  462. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  463. enum v4l2_buf_type type)
  464. {
  465. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  466. return &ctx->out_q;
  467. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  468. return &ctx->cap_q;
  469. return NULL;
  470. }
  471. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  472. {
  473. struct vb2_queue *vq;
  474. struct s5p_jpeg_q_data *q_data = NULL;
  475. struct v4l2_pix_format *pix = &f->fmt.pix;
  476. struct s5p_jpeg_ctx *ct = priv;
  477. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  478. if (!vq)
  479. return -EINVAL;
  480. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  481. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  482. return -EINVAL;
  483. q_data = get_q_data(ct, f->type);
  484. BUG_ON(q_data == NULL);
  485. pix->width = q_data->w;
  486. pix->height = q_data->h;
  487. pix->field = V4L2_FIELD_NONE;
  488. pix->pixelformat = q_data->fmt->fourcc;
  489. pix->bytesperline = 0;
  490. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  491. u32 bpl = q_data->w;
  492. if (q_data->fmt->colplanes == 1)
  493. bpl = (bpl * q_data->fmt->depth) >> 3;
  494. pix->bytesperline = bpl;
  495. }
  496. pix->sizeimage = q_data->size;
  497. return 0;
  498. }
  499. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(unsigned int mode,
  500. u32 pixelformat)
  501. {
  502. unsigned int k;
  503. struct s5p_jpeg_fmt *formats;
  504. int n;
  505. if (mode == S5P_JPEG_ENCODE) {
  506. formats = formats_enc;
  507. n = NUM_FORMATS_ENC;
  508. } else {
  509. formats = formats_dec;
  510. n = NUM_FORMATS_DEC;
  511. }
  512. for (k = 0; k < n; k++) {
  513. struct s5p_jpeg_fmt *fmt = &formats[k];
  514. if (fmt->fourcc == pixelformat)
  515. return fmt;
  516. }
  517. return NULL;
  518. }
  519. static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  520. unsigned int walign,
  521. u32 *h, unsigned int hmin, unsigned int hmax,
  522. unsigned int halign)
  523. {
  524. int width, height, w_step, h_step;
  525. width = *w;
  526. height = *h;
  527. w_step = 1 << walign;
  528. h_step = 1 << halign;
  529. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  530. if (*w < width && (*w + w_step) < wmax)
  531. *w += w_step;
  532. if (*h < height && (*h + h_step) < hmax)
  533. *h += h_step;
  534. }
  535. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  536. struct s5p_jpeg_ctx *ctx, int q_type)
  537. {
  538. struct v4l2_pix_format *pix = &f->fmt.pix;
  539. if (pix->field == V4L2_FIELD_ANY)
  540. pix->field = V4L2_FIELD_NONE;
  541. else if (pix->field != V4L2_FIELD_NONE)
  542. return -EINVAL;
  543. /* V4L2 specification suggests the driver corrects the format struct
  544. * if any of the dimensions is unsupported */
  545. if (q_type == MEM2MEM_OUTPUT)
  546. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  547. S5P_JPEG_MAX_WIDTH, 0,
  548. &pix->height, S5P_JPEG_MIN_HEIGHT,
  549. S5P_JPEG_MAX_HEIGHT, 0);
  550. else
  551. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  552. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  553. &pix->height, S5P_JPEG_MIN_HEIGHT,
  554. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  555. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  556. if (pix->sizeimage <= 0)
  557. pix->sizeimage = PAGE_SIZE;
  558. pix->bytesperline = 0;
  559. } else {
  560. u32 bpl = pix->bytesperline;
  561. if (fmt->colplanes > 1 && bpl < pix->width)
  562. bpl = pix->width; /* planar */
  563. if (fmt->colplanes == 1 && /* packed */
  564. (bpl << 3) * fmt->depth < pix->width)
  565. bpl = (pix->width * fmt->depth) >> 3;
  566. pix->bytesperline = bpl;
  567. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  568. }
  569. return 0;
  570. }
  571. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  572. struct v4l2_format *f)
  573. {
  574. struct s5p_jpeg_fmt *fmt;
  575. struct s5p_jpeg_ctx *ctx = priv;
  576. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  577. if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
  578. v4l2_err(&ctx->jpeg->v4l2_dev,
  579. "Fourcc format (0x%08x) invalid.\n",
  580. f->fmt.pix.pixelformat);
  581. return -EINVAL;
  582. }
  583. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_CAPTURE);
  584. }
  585. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  586. struct v4l2_format *f)
  587. {
  588. struct s5p_jpeg_fmt *fmt;
  589. struct s5p_jpeg_ctx *ctx = priv;
  590. fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
  591. if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
  592. v4l2_err(&ctx->jpeg->v4l2_dev,
  593. "Fourcc format (0x%08x) invalid.\n",
  594. f->fmt.pix.pixelformat);
  595. return -EINVAL;
  596. }
  597. return vidioc_try_fmt(f, fmt, ctx, MEM2MEM_OUTPUT);
  598. }
  599. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  600. {
  601. struct vb2_queue *vq;
  602. struct s5p_jpeg_q_data *q_data = NULL;
  603. struct v4l2_pix_format *pix = &f->fmt.pix;
  604. vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
  605. if (!vq)
  606. return -EINVAL;
  607. q_data = get_q_data(ct, f->type);
  608. BUG_ON(q_data == NULL);
  609. if (vb2_is_busy(vq)) {
  610. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  611. return -EBUSY;
  612. }
  613. q_data->fmt = s5p_jpeg_find_format(ct->mode, pix->pixelformat);
  614. q_data->w = pix->width;
  615. q_data->h = pix->height;
  616. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG)
  617. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  618. else
  619. q_data->size = pix->sizeimage;
  620. return 0;
  621. }
  622. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  623. struct v4l2_format *f)
  624. {
  625. int ret;
  626. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  627. if (ret)
  628. return ret;
  629. return s5p_jpeg_s_fmt(priv, f);
  630. }
  631. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  632. struct v4l2_format *f)
  633. {
  634. int ret;
  635. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  636. if (ret)
  637. return ret;
  638. return s5p_jpeg_s_fmt(priv, f);
  639. }
  640. static int s5p_jpeg_reqbufs(struct file *file, void *priv,
  641. struct v4l2_requestbuffers *reqbufs)
  642. {
  643. struct s5p_jpeg_ctx *ctx = priv;
  644. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  645. }
  646. static int s5p_jpeg_querybuf(struct file *file, void *priv,
  647. struct v4l2_buffer *buf)
  648. {
  649. struct s5p_jpeg_ctx *ctx = priv;
  650. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  651. }
  652. static int s5p_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  653. {
  654. struct s5p_jpeg_ctx *ctx = priv;
  655. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  656. }
  657. static int s5p_jpeg_dqbuf(struct file *file, void *priv,
  658. struct v4l2_buffer *buf)
  659. {
  660. struct s5p_jpeg_ctx *ctx = priv;
  661. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  662. }
  663. static int s5p_jpeg_streamon(struct file *file, void *priv,
  664. enum v4l2_buf_type type)
  665. {
  666. struct s5p_jpeg_ctx *ctx = priv;
  667. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  668. }
  669. static int s5p_jpeg_streamoff(struct file *file, void *priv,
  670. enum v4l2_buf_type type)
  671. {
  672. struct s5p_jpeg_ctx *ctx = priv;
  673. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  674. }
  675. int s5p_jpeg_g_selection(struct file *file, void *priv,
  676. struct v4l2_selection *s)
  677. {
  678. struct s5p_jpeg_ctx *ctx = priv;
  679. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  680. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  681. return -EINVAL;
  682. /* For JPEG blob active == default == bounds */
  683. switch (s->target) {
  684. case V4L2_SEL_TGT_CROP_ACTIVE:
  685. case V4L2_SEL_TGT_CROP_BOUNDS:
  686. case V4L2_SEL_TGT_CROP_DEFAULT:
  687. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  688. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  689. s->r.width = ctx->out_q.w;
  690. s->r.height = ctx->out_q.h;
  691. break;
  692. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  693. case V4L2_SEL_TGT_COMPOSE_PADDED:
  694. s->r.width = ctx->cap_q.w;
  695. s->r.height = ctx->cap_q.h;
  696. break;
  697. default:
  698. return -EINVAL;
  699. }
  700. s->r.left = 0;
  701. s->r.top = 0;
  702. return 0;
  703. }
  704. static int s5p_jpeg_g_jpegcomp(struct file *file, void *priv,
  705. struct v4l2_jpegcompression *compr)
  706. {
  707. struct s5p_jpeg_ctx *ctx = priv;
  708. if (ctx->mode == S5P_JPEG_DECODE)
  709. return -ENOTTY;
  710. memset(compr, 0, sizeof(*compr));
  711. compr->quality = ctx->compr_quality;
  712. return 0;
  713. }
  714. static int s5p_jpeg_s_jpegcomp(struct file *file, void *priv,
  715. struct v4l2_jpegcompression *compr)
  716. {
  717. struct s5p_jpeg_ctx *ctx = priv;
  718. if (ctx->mode == S5P_JPEG_DECODE)
  719. return -ENOTTY;
  720. compr->quality = clamp(compr->quality, S5P_JPEG_COMPR_QUAL_BEST,
  721. S5P_JPEG_COMPR_QUAL_WORST);
  722. ctx->compr_quality = S5P_JPEG_COMPR_QUAL_WORST - compr->quality;
  723. return 0;
  724. }
  725. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  726. .vidioc_querycap = s5p_jpeg_querycap,
  727. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  728. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  729. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  730. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  731. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  732. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  733. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  734. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  735. .vidioc_reqbufs = s5p_jpeg_reqbufs,
  736. .vidioc_querybuf = s5p_jpeg_querybuf,
  737. .vidioc_qbuf = s5p_jpeg_qbuf,
  738. .vidioc_dqbuf = s5p_jpeg_dqbuf,
  739. .vidioc_streamon = s5p_jpeg_streamon,
  740. .vidioc_streamoff = s5p_jpeg_streamoff,
  741. .vidioc_g_selection = s5p_jpeg_g_selection,
  742. .vidioc_g_jpegcomp = s5p_jpeg_g_jpegcomp,
  743. .vidioc_s_jpegcomp = s5p_jpeg_s_jpegcomp,
  744. };
  745. /*
  746. * ============================================================================
  747. * mem2mem callbacks
  748. * ============================================================================
  749. */
  750. static void s5p_jpeg_device_run(void *priv)
  751. {
  752. struct s5p_jpeg_ctx *ctx = priv;
  753. struct s5p_jpeg *jpeg = ctx->jpeg;
  754. struct vb2_buffer *src_buf, *dst_buf;
  755. unsigned long src_addr, dst_addr;
  756. src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  757. dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  758. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  759. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  760. jpeg_reset(jpeg->regs);
  761. jpeg_poweron(jpeg->regs);
  762. jpeg_proc_mode(jpeg->regs, ctx->mode);
  763. if (ctx->mode == S5P_JPEG_ENCODE) {
  764. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  765. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_565);
  766. else
  767. jpeg_input_raw_mode(jpeg->regs, S5P_JPEG_RAW_IN_422);
  768. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  769. jpeg_subsampling_mode(jpeg->regs,
  770. S5P_JPEG_SUBSAMPLING_422);
  771. else
  772. jpeg_subsampling_mode(jpeg->regs,
  773. S5P_JPEG_SUBSAMPLING_420);
  774. jpeg_dri(jpeg->regs, 0);
  775. jpeg_x(jpeg->regs, ctx->out_q.w);
  776. jpeg_y(jpeg->regs, ctx->out_q.h);
  777. jpeg_imgadr(jpeg->regs, src_addr);
  778. jpeg_jpgadr(jpeg->regs, dst_addr);
  779. /* ultimately comes from sizeimage from userspace */
  780. jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  781. /* JPEG RGB to YCbCr conversion matrix */
  782. jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  783. jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  784. jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  785. jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  786. jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  787. jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  788. jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  789. jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  790. jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  791. /*
  792. * JPEG IP allows storing 4 quantization tables
  793. * We fill table 0 for luma and table 1 for chroma
  794. */
  795. jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  796. jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  797. /* use table 0 for Y */
  798. jpeg_qtbl(jpeg->regs, 1, 0);
  799. /* use table 1 for Cb and Cr*/
  800. jpeg_qtbl(jpeg->regs, 2, 1);
  801. jpeg_qtbl(jpeg->regs, 3, 1);
  802. /* Y, Cb, Cr use Huffman table 0 */
  803. jpeg_htbl_ac(jpeg->regs, 1);
  804. jpeg_htbl_dc(jpeg->regs, 1);
  805. jpeg_htbl_ac(jpeg->regs, 2);
  806. jpeg_htbl_dc(jpeg->regs, 2);
  807. jpeg_htbl_ac(jpeg->regs, 3);
  808. jpeg_htbl_dc(jpeg->regs, 3);
  809. } else {
  810. jpeg_rst_int_enable(jpeg->regs, true);
  811. jpeg_data_num_int_enable(jpeg->regs, true);
  812. jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  813. jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  814. jpeg_jpgadr(jpeg->regs, src_addr);
  815. jpeg_imgadr(jpeg->regs, dst_addr);
  816. }
  817. jpeg_start(jpeg->regs);
  818. }
  819. static int s5p_jpeg_job_ready(void *priv)
  820. {
  821. struct s5p_jpeg_ctx *ctx = priv;
  822. if (ctx->mode == S5P_JPEG_DECODE)
  823. return ctx->hdr_parsed;
  824. return 1;
  825. }
  826. static void s5p_jpeg_job_abort(void *priv)
  827. {
  828. }
  829. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  830. .device_run = s5p_jpeg_device_run,
  831. .job_ready = s5p_jpeg_job_ready,
  832. .job_abort = s5p_jpeg_job_abort,
  833. };
  834. /*
  835. * ============================================================================
  836. * Queue operations
  837. * ============================================================================
  838. */
  839. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  840. const struct v4l2_format *fmt,
  841. unsigned int *nbuffers, unsigned int *nplanes,
  842. unsigned int sizes[], void *alloc_ctxs[])
  843. {
  844. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  845. struct s5p_jpeg_q_data *q_data = NULL;
  846. unsigned int size, count = *nbuffers;
  847. q_data = get_q_data(ctx, vq->type);
  848. BUG_ON(q_data == NULL);
  849. size = q_data->size;
  850. /*
  851. * header is parsed during decoding and parsed information stored
  852. * in the context so we do not allow another buffer to overwrite it
  853. */
  854. if (ctx->mode == S5P_JPEG_DECODE)
  855. count = 1;
  856. *nbuffers = count;
  857. *nplanes = 1;
  858. sizes[0] = size;
  859. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  860. return 0;
  861. }
  862. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  863. {
  864. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  865. struct s5p_jpeg_q_data *q_data = NULL;
  866. q_data = get_q_data(ctx, vb->vb2_queue->type);
  867. BUG_ON(q_data == NULL);
  868. if (vb2_plane_size(vb, 0) < q_data->size) {
  869. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  870. __func__, vb2_plane_size(vb, 0),
  871. (long)q_data->size);
  872. return -EINVAL;
  873. }
  874. vb2_set_plane_payload(vb, 0, q_data->size);
  875. return 0;
  876. }
  877. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  878. {
  879. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  880. if (ctx->mode == S5P_JPEG_DECODE &&
  881. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  882. struct s5p_jpeg_q_data tmp, *q_data;
  883. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  884. (unsigned long)vb2_plane_vaddr(vb, 0),
  885. min((unsigned long)ctx->out_q.size,
  886. vb2_get_plane_payload(vb, 0)));
  887. if (!ctx->hdr_parsed) {
  888. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  889. return;
  890. }
  891. q_data = &ctx->out_q;
  892. q_data->w = tmp.w;
  893. q_data->h = tmp.h;
  894. q_data = &ctx->cap_q;
  895. q_data->w = tmp.w;
  896. q_data->h = tmp.h;
  897. jpeg_bound_align_image(&q_data->w, S5P_JPEG_MIN_WIDTH,
  898. S5P_JPEG_MAX_WIDTH, q_data->fmt->h_align,
  899. &q_data->h, S5P_JPEG_MIN_HEIGHT,
  900. S5P_JPEG_MAX_HEIGHT, q_data->fmt->v_align
  901. );
  902. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  903. }
  904. if (ctx->m2m_ctx)
  905. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  906. }
  907. static void s5p_jpeg_wait_prepare(struct vb2_queue *vq)
  908. {
  909. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  910. mutex_unlock(&ctx->jpeg->lock);
  911. }
  912. static void s5p_jpeg_wait_finish(struct vb2_queue *vq)
  913. {
  914. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  915. mutex_lock(&ctx->jpeg->lock);
  916. }
  917. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  918. {
  919. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  920. int ret;
  921. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  922. return ret > 0 ? 0 : ret;
  923. }
  924. static int s5p_jpeg_stop_streaming(struct vb2_queue *q)
  925. {
  926. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  927. pm_runtime_put(ctx->jpeg->dev);
  928. return 0;
  929. }
  930. static struct vb2_ops s5p_jpeg_qops = {
  931. .queue_setup = s5p_jpeg_queue_setup,
  932. .buf_prepare = s5p_jpeg_buf_prepare,
  933. .buf_queue = s5p_jpeg_buf_queue,
  934. .wait_prepare = s5p_jpeg_wait_prepare,
  935. .wait_finish = s5p_jpeg_wait_finish,
  936. .start_streaming = s5p_jpeg_start_streaming,
  937. .stop_streaming = s5p_jpeg_stop_streaming,
  938. };
  939. static int queue_init(void *priv, struct vb2_queue *src_vq,
  940. struct vb2_queue *dst_vq)
  941. {
  942. struct s5p_jpeg_ctx *ctx = priv;
  943. int ret;
  944. memset(src_vq, 0, sizeof(*src_vq));
  945. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  946. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  947. src_vq->drv_priv = ctx;
  948. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  949. src_vq->ops = &s5p_jpeg_qops;
  950. src_vq->mem_ops = &vb2_dma_contig_memops;
  951. ret = vb2_queue_init(src_vq);
  952. if (ret)
  953. return ret;
  954. memset(dst_vq, 0, sizeof(*dst_vq));
  955. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  956. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  957. dst_vq->drv_priv = ctx;
  958. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  959. dst_vq->ops = &s5p_jpeg_qops;
  960. dst_vq->mem_ops = &vb2_dma_contig_memops;
  961. return vb2_queue_init(dst_vq);
  962. }
  963. /*
  964. * ============================================================================
  965. * ISR
  966. * ============================================================================
  967. */
  968. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  969. {
  970. struct s5p_jpeg *jpeg = dev_id;
  971. struct s5p_jpeg_ctx *curr_ctx;
  972. struct vb2_buffer *src_buf, *dst_buf;
  973. unsigned long payload_size = 0;
  974. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  975. bool enc_jpeg_too_large = false;
  976. bool timer_elapsed = false;
  977. bool op_completed = false;
  978. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  979. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  980. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  981. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  982. enc_jpeg_too_large = jpeg_enc_stream_stat(jpeg->regs);
  983. timer_elapsed = jpeg_timer_stat(jpeg->regs);
  984. op_completed = jpeg_result_stat_ok(jpeg->regs);
  985. if (curr_ctx->mode == S5P_JPEG_DECODE)
  986. op_completed = op_completed && jpeg_stream_stat_ok(jpeg->regs);
  987. if (enc_jpeg_too_large) {
  988. state = VB2_BUF_STATE_ERROR;
  989. jpeg_clear_enc_stream_stat(jpeg->regs);
  990. } else if (timer_elapsed) {
  991. state = VB2_BUF_STATE_ERROR;
  992. jpeg_clear_timer_stat(jpeg->regs);
  993. } else if (!op_completed) {
  994. state = VB2_BUF_STATE_ERROR;
  995. } else {
  996. payload_size = jpeg_compressed_size(jpeg->regs);
  997. }
  998. v4l2_m2m_buf_done(src_buf, state);
  999. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1000. vb2_set_plane_payload(dst_buf, 0, payload_size);
  1001. v4l2_m2m_buf_done(dst_buf, state);
  1002. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->m2m_ctx);
  1003. jpeg_clear_int(jpeg->regs);
  1004. return IRQ_HANDLED;
  1005. }
  1006. /*
  1007. * ============================================================================
  1008. * Driver basic infrastructure
  1009. * ============================================================================
  1010. */
  1011. static int s5p_jpeg_probe(struct platform_device *pdev)
  1012. {
  1013. struct s5p_jpeg *jpeg;
  1014. struct resource *res;
  1015. int ret;
  1016. /* JPEG IP abstraction struct */
  1017. jpeg = kzalloc(sizeof(struct s5p_jpeg), GFP_KERNEL);
  1018. if (!jpeg)
  1019. return -ENOMEM;
  1020. mutex_init(&jpeg->lock);
  1021. jpeg->dev = &pdev->dev;
  1022. /* memory-mapped registers */
  1023. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1024. if (!res) {
  1025. dev_err(&pdev->dev, "cannot find IO resource\n");
  1026. ret = -ENOENT;
  1027. goto jpeg_alloc_rollback;
  1028. }
  1029. jpeg->ioarea = request_mem_region(res->start, resource_size(res),
  1030. pdev->name);
  1031. if (!jpeg->ioarea) {
  1032. dev_err(&pdev->dev, "cannot request IO\n");
  1033. ret = -ENXIO;
  1034. goto jpeg_alloc_rollback;
  1035. }
  1036. jpeg->regs = ioremap(res->start, resource_size(res));
  1037. if (!jpeg->regs) {
  1038. dev_err(&pdev->dev, "cannot map IO\n");
  1039. ret = -ENXIO;
  1040. goto mem_region_rollback;
  1041. }
  1042. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  1043. jpeg->regs, jpeg->ioarea, res);
  1044. /* interrupt service routine registration */
  1045. jpeg->irq = ret = platform_get_irq(pdev, 0);
  1046. if (ret < 0) {
  1047. dev_err(&pdev->dev, "cannot find IRQ\n");
  1048. goto ioremap_rollback;
  1049. }
  1050. ret = request_irq(jpeg->irq, s5p_jpeg_irq, 0,
  1051. dev_name(&pdev->dev), jpeg);
  1052. if (ret) {
  1053. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  1054. goto ioremap_rollback;
  1055. }
  1056. /* clocks */
  1057. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  1058. if (IS_ERR(jpeg->clk)) {
  1059. dev_err(&pdev->dev, "cannot get clock\n");
  1060. ret = PTR_ERR(jpeg->clk);
  1061. goto request_irq_rollback;
  1062. }
  1063. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  1064. clk_enable(jpeg->clk);
  1065. /* v4l2 device */
  1066. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  1067. if (ret) {
  1068. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1069. goto clk_get_rollback;
  1070. }
  1071. /* mem2mem device */
  1072. jpeg->m2m_dev = v4l2_m2m_init(&s5p_jpeg_m2m_ops);
  1073. if (IS_ERR(jpeg->m2m_dev)) {
  1074. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  1075. ret = PTR_ERR(jpeg->m2m_dev);
  1076. goto device_register_rollback;
  1077. }
  1078. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1079. if (IS_ERR(jpeg->alloc_ctx)) {
  1080. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  1081. ret = PTR_ERR(jpeg->alloc_ctx);
  1082. goto m2m_init_rollback;
  1083. }
  1084. /* JPEG encoder /dev/videoX node */
  1085. jpeg->vfd_encoder = video_device_alloc();
  1086. if (!jpeg->vfd_encoder) {
  1087. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1088. ret = -ENOMEM;
  1089. goto vb2_allocator_rollback;
  1090. }
  1091. strlcpy(jpeg->vfd_encoder->name, S5P_JPEG_M2M_NAME,
  1092. sizeof(jpeg->vfd_encoder->name));
  1093. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  1094. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1095. jpeg->vfd_encoder->minor = -1;
  1096. jpeg->vfd_encoder->release = video_device_release;
  1097. jpeg->vfd_encoder->lock = &jpeg->lock;
  1098. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  1099. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1100. if (ret) {
  1101. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1102. goto enc_vdev_alloc_rollback;
  1103. }
  1104. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  1105. v4l2_info(&jpeg->v4l2_dev,
  1106. "encoder device registered as /dev/video%d\n",
  1107. jpeg->vfd_encoder->num);
  1108. /* JPEG decoder /dev/videoX node */
  1109. jpeg->vfd_decoder = video_device_alloc();
  1110. if (!jpeg->vfd_decoder) {
  1111. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1112. ret = -ENOMEM;
  1113. goto enc_vdev_register_rollback;
  1114. }
  1115. strlcpy(jpeg->vfd_decoder->name, S5P_JPEG_M2M_NAME,
  1116. sizeof(jpeg->vfd_decoder->name));
  1117. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  1118. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1119. jpeg->vfd_decoder->minor = -1;
  1120. jpeg->vfd_decoder->release = video_device_release;
  1121. jpeg->vfd_decoder->lock = &jpeg->lock;
  1122. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  1123. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1124. if (ret) {
  1125. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1126. goto dec_vdev_alloc_rollback;
  1127. }
  1128. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  1129. v4l2_info(&jpeg->v4l2_dev,
  1130. "decoder device registered as /dev/video%d\n",
  1131. jpeg->vfd_decoder->num);
  1132. /* final statements & power management */
  1133. platform_set_drvdata(pdev, jpeg);
  1134. pm_runtime_enable(&pdev->dev);
  1135. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  1136. return 0;
  1137. dec_vdev_alloc_rollback:
  1138. video_device_release(jpeg->vfd_decoder);
  1139. enc_vdev_register_rollback:
  1140. video_unregister_device(jpeg->vfd_encoder);
  1141. enc_vdev_alloc_rollback:
  1142. video_device_release(jpeg->vfd_encoder);
  1143. vb2_allocator_rollback:
  1144. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1145. m2m_init_rollback:
  1146. v4l2_m2m_release(jpeg->m2m_dev);
  1147. device_register_rollback:
  1148. v4l2_device_unregister(&jpeg->v4l2_dev);
  1149. clk_get_rollback:
  1150. clk_disable(jpeg->clk);
  1151. clk_put(jpeg->clk);
  1152. request_irq_rollback:
  1153. free_irq(jpeg->irq, jpeg);
  1154. ioremap_rollback:
  1155. iounmap(jpeg->regs);
  1156. mem_region_rollback:
  1157. release_resource(jpeg->ioarea);
  1158. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1159. jpeg_alloc_rollback:
  1160. kfree(jpeg);
  1161. return ret;
  1162. }
  1163. static int s5p_jpeg_remove(struct platform_device *pdev)
  1164. {
  1165. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  1166. pm_runtime_disable(jpeg->dev);
  1167. video_unregister_device(jpeg->vfd_decoder);
  1168. video_device_release(jpeg->vfd_decoder);
  1169. video_unregister_device(jpeg->vfd_encoder);
  1170. video_device_release(jpeg->vfd_encoder);
  1171. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1172. v4l2_m2m_release(jpeg->m2m_dev);
  1173. v4l2_device_unregister(&jpeg->v4l2_dev);
  1174. clk_disable(jpeg->clk);
  1175. clk_put(jpeg->clk);
  1176. free_irq(jpeg->irq, jpeg);
  1177. iounmap(jpeg->regs);
  1178. release_resource(jpeg->ioarea);
  1179. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1180. kfree(jpeg);
  1181. return 0;
  1182. }
  1183. static int s5p_jpeg_runtime_suspend(struct device *dev)
  1184. {
  1185. return 0;
  1186. }
  1187. static int s5p_jpeg_runtime_resume(struct device *dev)
  1188. {
  1189. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1190. /*
  1191. * JPEG IP allows storing two Huffman tables for each component
  1192. * We fill table 0 for each component
  1193. */
  1194. jpeg_set_hdctbl(jpeg->regs);
  1195. jpeg_set_hdctblg(jpeg->regs);
  1196. jpeg_set_hactbl(jpeg->regs);
  1197. jpeg_set_hactblg(jpeg->regs);
  1198. return 0;
  1199. }
  1200. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  1201. .runtime_suspend = s5p_jpeg_runtime_suspend,
  1202. .runtime_resume = s5p_jpeg_runtime_resume,
  1203. };
  1204. static struct platform_driver s5p_jpeg_driver = {
  1205. .probe = s5p_jpeg_probe,
  1206. .remove = s5p_jpeg_remove,
  1207. .driver = {
  1208. .owner = THIS_MODULE,
  1209. .name = S5P_JPEG_M2M_NAME,
  1210. .pm = &s5p_jpeg_pm_ops,
  1211. },
  1212. };
  1213. static int __init
  1214. s5p_jpeg_register(void)
  1215. {
  1216. int ret;
  1217. pr_info("S5P JPEG V4L2 Driver, (c) 2011 Samsung Electronics\n");
  1218. ret = platform_driver_register(&s5p_jpeg_driver);
  1219. if (ret)
  1220. pr_err("%s: failed to register jpeg driver\n", __func__);
  1221. return ret;
  1222. }
  1223. static void __exit
  1224. s5p_jpeg_unregister(void)
  1225. {
  1226. platform_driver_unregister(&s5p_jpeg_driver);
  1227. }
  1228. module_init(s5p_jpeg_register);
  1229. module_exit(s5p_jpeg_unregister);
  1230. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  1231. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  1232. MODULE_LICENSE("GPL");