jpeg-core.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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, unsigned int *nbuffers,
  840. unsigned int *nplanes, unsigned int sizes[],
  841. void *alloc_ctxs[])
  842. {
  843. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  844. struct s5p_jpeg_q_data *q_data = NULL;
  845. unsigned int size, count = *nbuffers;
  846. q_data = get_q_data(ctx, vq->type);
  847. BUG_ON(q_data == NULL);
  848. size = q_data->size;
  849. /*
  850. * header is parsed during decoding and parsed information stored
  851. * in the context so we do not allow another buffer to overwrite it
  852. */
  853. if (ctx->mode == S5P_JPEG_DECODE)
  854. count = 1;
  855. *nbuffers = count;
  856. *nplanes = 1;
  857. sizes[0] = size;
  858. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  859. return 0;
  860. }
  861. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  862. {
  863. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  864. struct s5p_jpeg_q_data *q_data = NULL;
  865. q_data = get_q_data(ctx, vb->vb2_queue->type);
  866. BUG_ON(q_data == NULL);
  867. if (vb2_plane_size(vb, 0) < q_data->size) {
  868. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  869. __func__, vb2_plane_size(vb, 0),
  870. (long)q_data->size);
  871. return -EINVAL;
  872. }
  873. vb2_set_plane_payload(vb, 0, q_data->size);
  874. return 0;
  875. }
  876. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  877. {
  878. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  879. if (ctx->mode == S5P_JPEG_DECODE &&
  880. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  881. struct s5p_jpeg_q_data tmp, *q_data;
  882. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  883. (unsigned long)vb2_plane_vaddr(vb, 0),
  884. min((unsigned long)ctx->out_q.size,
  885. vb2_get_plane_payload(vb, 0)));
  886. if (!ctx->hdr_parsed) {
  887. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  888. return;
  889. }
  890. q_data = &ctx->out_q;
  891. q_data->w = tmp.w;
  892. q_data->h = tmp.h;
  893. q_data = &ctx->cap_q;
  894. q_data->w = tmp.w;
  895. q_data->h = tmp.h;
  896. jpeg_bound_align_image(&q_data->w, S5P_JPEG_MIN_WIDTH,
  897. S5P_JPEG_MAX_WIDTH, q_data->fmt->h_align,
  898. &q_data->h, S5P_JPEG_MIN_HEIGHT,
  899. S5P_JPEG_MAX_HEIGHT, q_data->fmt->v_align
  900. );
  901. q_data->size = q_data->w * q_data->h * q_data->fmt->depth >> 3;
  902. }
  903. if (ctx->m2m_ctx)
  904. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  905. }
  906. static void s5p_jpeg_wait_prepare(struct vb2_queue *vq)
  907. {
  908. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  909. mutex_unlock(&ctx->jpeg->lock);
  910. }
  911. static void s5p_jpeg_wait_finish(struct vb2_queue *vq)
  912. {
  913. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  914. mutex_lock(&ctx->jpeg->lock);
  915. }
  916. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  917. {
  918. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  919. int ret;
  920. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  921. return ret > 0 ? 0 : ret;
  922. }
  923. static int s5p_jpeg_stop_streaming(struct vb2_queue *q)
  924. {
  925. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  926. pm_runtime_put(ctx->jpeg->dev);
  927. return 0;
  928. }
  929. static struct vb2_ops s5p_jpeg_qops = {
  930. .queue_setup = s5p_jpeg_queue_setup,
  931. .buf_prepare = s5p_jpeg_buf_prepare,
  932. .buf_queue = s5p_jpeg_buf_queue,
  933. .wait_prepare = s5p_jpeg_wait_prepare,
  934. .wait_finish = s5p_jpeg_wait_finish,
  935. .start_streaming = s5p_jpeg_start_streaming,
  936. .stop_streaming = s5p_jpeg_stop_streaming,
  937. };
  938. static int queue_init(void *priv, struct vb2_queue *src_vq,
  939. struct vb2_queue *dst_vq)
  940. {
  941. struct s5p_jpeg_ctx *ctx = priv;
  942. int ret;
  943. memset(src_vq, 0, sizeof(*src_vq));
  944. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  945. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  946. src_vq->drv_priv = ctx;
  947. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  948. src_vq->ops = &s5p_jpeg_qops;
  949. src_vq->mem_ops = &vb2_dma_contig_memops;
  950. ret = vb2_queue_init(src_vq);
  951. if (ret)
  952. return ret;
  953. memset(dst_vq, 0, sizeof(*dst_vq));
  954. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  955. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  956. dst_vq->drv_priv = ctx;
  957. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  958. dst_vq->ops = &s5p_jpeg_qops;
  959. dst_vq->mem_ops = &vb2_dma_contig_memops;
  960. return vb2_queue_init(dst_vq);
  961. }
  962. /*
  963. * ============================================================================
  964. * ISR
  965. * ============================================================================
  966. */
  967. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  968. {
  969. struct s5p_jpeg *jpeg = dev_id;
  970. struct s5p_jpeg_ctx *curr_ctx;
  971. struct vb2_buffer *src_buf, *dst_buf;
  972. unsigned long payload_size = 0;
  973. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  974. bool enc_jpeg_too_large = false;
  975. bool timer_elapsed = false;
  976. bool op_completed = false;
  977. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  978. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
  979. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
  980. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  981. enc_jpeg_too_large = jpeg_enc_stream_stat(jpeg->regs);
  982. timer_elapsed = jpeg_timer_stat(jpeg->regs);
  983. op_completed = jpeg_result_stat_ok(jpeg->regs);
  984. if (curr_ctx->mode == S5P_JPEG_DECODE)
  985. op_completed = op_completed && jpeg_stream_stat_ok(jpeg->regs);
  986. if (enc_jpeg_too_large) {
  987. state = VB2_BUF_STATE_ERROR;
  988. jpeg_clear_enc_stream_stat(jpeg->regs);
  989. } else if (timer_elapsed) {
  990. state = VB2_BUF_STATE_ERROR;
  991. jpeg_clear_timer_stat(jpeg->regs);
  992. } else if (!op_completed) {
  993. state = VB2_BUF_STATE_ERROR;
  994. } else {
  995. payload_size = jpeg_compressed_size(jpeg->regs);
  996. }
  997. v4l2_m2m_buf_done(src_buf, state);
  998. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  999. vb2_set_plane_payload(dst_buf, 0, payload_size);
  1000. v4l2_m2m_buf_done(dst_buf, state);
  1001. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->m2m_ctx);
  1002. jpeg_clear_int(jpeg->regs);
  1003. return IRQ_HANDLED;
  1004. }
  1005. /*
  1006. * ============================================================================
  1007. * Driver basic infrastructure
  1008. * ============================================================================
  1009. */
  1010. static int s5p_jpeg_probe(struct platform_device *pdev)
  1011. {
  1012. struct s5p_jpeg *jpeg;
  1013. struct resource *res;
  1014. int ret;
  1015. /* JPEG IP abstraction struct */
  1016. jpeg = kzalloc(sizeof(struct s5p_jpeg), GFP_KERNEL);
  1017. if (!jpeg)
  1018. return -ENOMEM;
  1019. mutex_init(&jpeg->lock);
  1020. jpeg->dev = &pdev->dev;
  1021. /* memory-mapped registers */
  1022. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1023. if (!res) {
  1024. dev_err(&pdev->dev, "cannot find IO resource\n");
  1025. ret = -ENOENT;
  1026. goto jpeg_alloc_rollback;
  1027. }
  1028. jpeg->ioarea = request_mem_region(res->start, resource_size(res),
  1029. pdev->name);
  1030. if (!jpeg->ioarea) {
  1031. dev_err(&pdev->dev, "cannot request IO\n");
  1032. ret = -ENXIO;
  1033. goto jpeg_alloc_rollback;
  1034. }
  1035. jpeg->regs = ioremap(res->start, resource_size(res));
  1036. if (!jpeg->regs) {
  1037. dev_err(&pdev->dev, "cannot map IO\n");
  1038. ret = -ENXIO;
  1039. goto mem_region_rollback;
  1040. }
  1041. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  1042. jpeg->regs, jpeg->ioarea, res);
  1043. /* interrupt service routine registration */
  1044. jpeg->irq = ret = platform_get_irq(pdev, 0);
  1045. if (ret < 0) {
  1046. dev_err(&pdev->dev, "cannot find IRQ\n");
  1047. goto ioremap_rollback;
  1048. }
  1049. ret = request_irq(jpeg->irq, s5p_jpeg_irq, 0,
  1050. dev_name(&pdev->dev), jpeg);
  1051. if (ret) {
  1052. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  1053. goto ioremap_rollback;
  1054. }
  1055. /* clocks */
  1056. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  1057. if (IS_ERR(jpeg->clk)) {
  1058. dev_err(&pdev->dev, "cannot get clock\n");
  1059. ret = PTR_ERR(jpeg->clk);
  1060. goto request_irq_rollback;
  1061. }
  1062. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  1063. clk_enable(jpeg->clk);
  1064. /* v4l2 device */
  1065. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  1066. if (ret) {
  1067. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1068. goto clk_get_rollback;
  1069. }
  1070. /* mem2mem device */
  1071. jpeg->m2m_dev = v4l2_m2m_init(&s5p_jpeg_m2m_ops);
  1072. if (IS_ERR(jpeg->m2m_dev)) {
  1073. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  1074. ret = PTR_ERR(jpeg->m2m_dev);
  1075. goto device_register_rollback;
  1076. }
  1077. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1078. if (IS_ERR(jpeg->alloc_ctx)) {
  1079. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  1080. ret = PTR_ERR(jpeg->alloc_ctx);
  1081. goto m2m_init_rollback;
  1082. }
  1083. /* JPEG encoder /dev/videoX node */
  1084. jpeg->vfd_encoder = video_device_alloc();
  1085. if (!jpeg->vfd_encoder) {
  1086. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1087. ret = -ENOMEM;
  1088. goto vb2_allocator_rollback;
  1089. }
  1090. strlcpy(jpeg->vfd_encoder->name, S5P_JPEG_M2M_NAME,
  1091. sizeof(jpeg->vfd_encoder->name));
  1092. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  1093. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1094. jpeg->vfd_encoder->minor = -1;
  1095. jpeg->vfd_encoder->release = video_device_release;
  1096. jpeg->vfd_encoder->lock = &jpeg->lock;
  1097. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  1098. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1099. if (ret) {
  1100. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1101. goto enc_vdev_alloc_rollback;
  1102. }
  1103. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  1104. v4l2_info(&jpeg->v4l2_dev,
  1105. "encoder device registered as /dev/video%d\n",
  1106. jpeg->vfd_encoder->num);
  1107. /* JPEG decoder /dev/videoX node */
  1108. jpeg->vfd_decoder = video_device_alloc();
  1109. if (!jpeg->vfd_decoder) {
  1110. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1111. ret = -ENOMEM;
  1112. goto enc_vdev_register_rollback;
  1113. }
  1114. strlcpy(jpeg->vfd_decoder->name, S5P_JPEG_M2M_NAME,
  1115. sizeof(jpeg->vfd_decoder->name));
  1116. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  1117. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1118. jpeg->vfd_decoder->minor = -1;
  1119. jpeg->vfd_decoder->release = video_device_release;
  1120. jpeg->vfd_decoder->lock = &jpeg->lock;
  1121. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  1122. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1123. if (ret) {
  1124. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1125. goto dec_vdev_alloc_rollback;
  1126. }
  1127. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  1128. v4l2_info(&jpeg->v4l2_dev,
  1129. "decoder device registered as /dev/video%d\n",
  1130. jpeg->vfd_decoder->num);
  1131. /* final statements & power management */
  1132. platform_set_drvdata(pdev, jpeg);
  1133. pm_runtime_enable(&pdev->dev);
  1134. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  1135. return 0;
  1136. dec_vdev_alloc_rollback:
  1137. video_device_release(jpeg->vfd_decoder);
  1138. enc_vdev_register_rollback:
  1139. video_unregister_device(jpeg->vfd_encoder);
  1140. enc_vdev_alloc_rollback:
  1141. video_device_release(jpeg->vfd_encoder);
  1142. vb2_allocator_rollback:
  1143. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1144. m2m_init_rollback:
  1145. v4l2_m2m_release(jpeg->m2m_dev);
  1146. device_register_rollback:
  1147. v4l2_device_unregister(&jpeg->v4l2_dev);
  1148. clk_get_rollback:
  1149. clk_disable(jpeg->clk);
  1150. clk_put(jpeg->clk);
  1151. request_irq_rollback:
  1152. free_irq(jpeg->irq, jpeg);
  1153. ioremap_rollback:
  1154. iounmap(jpeg->regs);
  1155. mem_region_rollback:
  1156. release_resource(jpeg->ioarea);
  1157. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1158. jpeg_alloc_rollback:
  1159. kfree(jpeg);
  1160. return ret;
  1161. }
  1162. static int s5p_jpeg_remove(struct platform_device *pdev)
  1163. {
  1164. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  1165. pm_runtime_disable(jpeg->dev);
  1166. video_unregister_device(jpeg->vfd_decoder);
  1167. video_device_release(jpeg->vfd_decoder);
  1168. video_unregister_device(jpeg->vfd_encoder);
  1169. video_device_release(jpeg->vfd_encoder);
  1170. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1171. v4l2_m2m_release(jpeg->m2m_dev);
  1172. v4l2_device_unregister(&jpeg->v4l2_dev);
  1173. clk_disable(jpeg->clk);
  1174. clk_put(jpeg->clk);
  1175. free_irq(jpeg->irq, jpeg);
  1176. iounmap(jpeg->regs);
  1177. release_resource(jpeg->ioarea);
  1178. release_mem_region(jpeg->ioarea->start, resource_size(jpeg->ioarea));
  1179. kfree(jpeg);
  1180. return 0;
  1181. }
  1182. static int s5p_jpeg_runtime_suspend(struct device *dev)
  1183. {
  1184. return 0;
  1185. }
  1186. static int s5p_jpeg_runtime_resume(struct device *dev)
  1187. {
  1188. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1189. /*
  1190. * JPEG IP allows storing two Huffman tables for each component
  1191. * We fill table 0 for each component
  1192. */
  1193. jpeg_set_hdctbl(jpeg->regs);
  1194. jpeg_set_hdctblg(jpeg->regs);
  1195. jpeg_set_hactbl(jpeg->regs);
  1196. jpeg_set_hactblg(jpeg->regs);
  1197. return 0;
  1198. }
  1199. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  1200. .runtime_suspend = s5p_jpeg_runtime_suspend,
  1201. .runtime_resume = s5p_jpeg_runtime_resume,
  1202. };
  1203. static struct platform_driver s5p_jpeg_driver = {
  1204. .probe = s5p_jpeg_probe,
  1205. .remove = s5p_jpeg_remove,
  1206. .driver = {
  1207. .owner = THIS_MODULE,
  1208. .name = S5P_JPEG_M2M_NAME,
  1209. .pm = &s5p_jpeg_pm_ops,
  1210. },
  1211. };
  1212. static int __init
  1213. s5p_jpeg_register(void)
  1214. {
  1215. int ret;
  1216. pr_info("S5P JPEG V4L2 Driver, (c) 2011 Samsung Electronics\n");
  1217. ret = platform_driver_register(&s5p_jpeg_driver);
  1218. if (ret)
  1219. pr_err("%s: failed to register jpeg driver\n", __func__);
  1220. return ret;
  1221. }
  1222. static void __exit
  1223. s5p_jpeg_unregister(void)
  1224. {
  1225. platform_driver_unregister(&s5p_jpeg_driver);
  1226. }
  1227. module_init(s5p_jpeg_register);
  1228. module_exit(s5p_jpeg_unregister);
  1229. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  1230. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  1231. MODULE_LICENSE("GPL");