jpeg-core.c 39 KB

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