jpeg-core.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  1. /* linux/drivers/media/platform/s5p-jpeg/jpeg-core.c
  2. *
  3. * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  7. * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/gfp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/string.h>
  26. #include <media/v4l2-mem2mem.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/videobuf2-core.h>
  29. #include <media/videobuf2-dma-contig.h>
  30. #include "jpeg-core.h"
  31. #include "jpeg-hw-s5p.h"
  32. #include "jpeg-hw-exynos4.h"
  33. #include "jpeg-regs.h"
  34. static struct s5p_jpeg_fmt sjpeg_formats[] = {
  35. {
  36. .name = "JPEG JFIF",
  37. .fourcc = V4L2_PIX_FMT_JPEG,
  38. .flags = SJPEG_FMT_FLAG_ENC_CAPTURE |
  39. SJPEG_FMT_FLAG_DEC_OUTPUT |
  40. SJPEG_FMT_FLAG_S5P |
  41. SJPEG_FMT_FLAG_EXYNOS4,
  42. },
  43. {
  44. .name = "YUV 4:2:2 packed, YCbYCr",
  45. .fourcc = V4L2_PIX_FMT_YUYV,
  46. .depth = 16,
  47. .colplanes = 1,
  48. .h_align = 4,
  49. .v_align = 3,
  50. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  51. SJPEG_FMT_FLAG_DEC_CAPTURE |
  52. SJPEG_FMT_FLAG_S5P |
  53. SJPEG_FMT_NON_RGB,
  54. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  55. },
  56. {
  57. .name = "YUV 4:2:2 packed, YCbYCr",
  58. .fourcc = V4L2_PIX_FMT_YUYV,
  59. .depth = 16,
  60. .colplanes = 1,
  61. .h_align = 1,
  62. .v_align = 0,
  63. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  64. SJPEG_FMT_FLAG_DEC_CAPTURE |
  65. SJPEG_FMT_FLAG_EXYNOS4 |
  66. SJPEG_FMT_NON_RGB,
  67. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  68. },
  69. {
  70. .name = "YUV 4:2:2 packed, YCrYCb",
  71. .fourcc = V4L2_PIX_FMT_YVYU,
  72. .depth = 16,
  73. .colplanes = 1,
  74. .h_align = 1,
  75. .v_align = 0,
  76. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  77. SJPEG_FMT_FLAG_DEC_CAPTURE |
  78. SJPEG_FMT_FLAG_EXYNOS4 |
  79. SJPEG_FMT_NON_RGB,
  80. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  81. },
  82. {
  83. .name = "RGB565",
  84. .fourcc = V4L2_PIX_FMT_RGB565,
  85. .depth = 16,
  86. .colplanes = 1,
  87. .h_align = 0,
  88. .v_align = 0,
  89. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  90. SJPEG_FMT_FLAG_DEC_CAPTURE |
  91. SJPEG_FMT_FLAG_EXYNOS4 |
  92. SJPEG_FMT_RGB,
  93. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  94. },
  95. {
  96. .name = "RGB565",
  97. .fourcc = V4L2_PIX_FMT_RGB565,
  98. .depth = 16,
  99. .colplanes = 1,
  100. .h_align = 0,
  101. .v_align = 0,
  102. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  103. SJPEG_FMT_FLAG_S5P |
  104. SJPEG_FMT_RGB,
  105. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  106. },
  107. {
  108. .name = "ARGB8888, 32 bpp",
  109. .fourcc = V4L2_PIX_FMT_RGB32,
  110. .depth = 32,
  111. .colplanes = 1,
  112. .h_align = 0,
  113. .v_align = 0,
  114. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  115. SJPEG_FMT_FLAG_DEC_CAPTURE |
  116. SJPEG_FMT_FLAG_EXYNOS4 |
  117. SJPEG_FMT_RGB,
  118. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  119. },
  120. {
  121. .name = "YUV 4:4:4 planar, Y/CbCr",
  122. .fourcc = V4L2_PIX_FMT_NV24,
  123. .depth = 24,
  124. .colplanes = 2,
  125. .h_align = 0,
  126. .v_align = 0,
  127. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  128. SJPEG_FMT_FLAG_DEC_CAPTURE |
  129. SJPEG_FMT_FLAG_EXYNOS4 |
  130. SJPEG_FMT_NON_RGB,
  131. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  132. },
  133. {
  134. .name = "YUV 4:4:4 planar, Y/CrCb",
  135. .fourcc = V4L2_PIX_FMT_NV42,
  136. .depth = 24,
  137. .colplanes = 2,
  138. .h_align = 0,
  139. .v_align = 0,
  140. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  141. SJPEG_FMT_FLAG_DEC_CAPTURE |
  142. SJPEG_FMT_FLAG_EXYNOS4 |
  143. SJPEG_FMT_NON_RGB,
  144. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  145. },
  146. {
  147. .name = "YUV 4:2:2 planar, Y/CrCb",
  148. .fourcc = V4L2_PIX_FMT_NV61,
  149. .depth = 16,
  150. .colplanes = 2,
  151. .h_align = 1,
  152. .v_align = 0,
  153. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  154. SJPEG_FMT_FLAG_DEC_CAPTURE |
  155. SJPEG_FMT_FLAG_EXYNOS4 |
  156. SJPEG_FMT_NON_RGB,
  157. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  158. },
  159. {
  160. .name = "YUV 4:2:2 planar, Y/CbCr",
  161. .fourcc = V4L2_PIX_FMT_NV16,
  162. .depth = 16,
  163. .colplanes = 2,
  164. .h_align = 1,
  165. .v_align = 0,
  166. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  167. SJPEG_FMT_FLAG_DEC_CAPTURE |
  168. SJPEG_FMT_FLAG_EXYNOS4 |
  169. SJPEG_FMT_NON_RGB,
  170. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  171. },
  172. {
  173. .name = "YUV 4:2:0 planar, Y/CbCr",
  174. .fourcc = V4L2_PIX_FMT_NV12,
  175. .depth = 12,
  176. .colplanes = 2,
  177. .h_align = 1,
  178. .v_align = 1,
  179. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  180. SJPEG_FMT_FLAG_DEC_CAPTURE |
  181. SJPEG_FMT_FLAG_EXYNOS4 |
  182. SJPEG_FMT_NON_RGB,
  183. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
  184. },
  185. {
  186. .name = "YUV 4:2:0 planar, Y/CbCr",
  187. .fourcc = V4L2_PIX_FMT_NV12,
  188. .depth = 12,
  189. .colplanes = 2,
  190. .h_align = 4,
  191. .v_align = 4,
  192. .flags = SJPEG_FMT_FLAG_DEC_CAPTURE |
  193. SJPEG_FMT_FLAG_S5P |
  194. SJPEG_FMT_NON_RGB,
  195. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
  196. },
  197. {
  198. .name = "YUV 4:2:0 planar, Y/CrCb",
  199. .fourcc = V4L2_PIX_FMT_NV21,
  200. .depth = 12,
  201. .colplanes = 2,
  202. .h_align = 1,
  203. .v_align = 1,
  204. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  205. SJPEG_FMT_FLAG_DEC_CAPTURE |
  206. SJPEG_FMT_FLAG_EXYNOS4 |
  207. SJPEG_FMT_NON_RGB,
  208. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
  209. },
  210. {
  211. .name = "YUV 4:2:0 contiguous 3-planar, Y/Cb/Cr",
  212. .fourcc = V4L2_PIX_FMT_YUV420,
  213. .depth = 12,
  214. .colplanes = 3,
  215. .h_align = 1,
  216. .v_align = 1,
  217. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  218. SJPEG_FMT_FLAG_DEC_CAPTURE |
  219. SJPEG_FMT_FLAG_EXYNOS4 |
  220. SJPEG_FMT_NON_RGB,
  221. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
  222. },
  223. {
  224. .name = "Gray",
  225. .fourcc = V4L2_PIX_FMT_GREY,
  226. .depth = 8,
  227. .colplanes = 1,
  228. .flags = SJPEG_FMT_FLAG_ENC_OUTPUT |
  229. SJPEG_FMT_FLAG_DEC_CAPTURE |
  230. SJPEG_FMT_FLAG_EXYNOS4 |
  231. SJPEG_FMT_NON_RGB,
  232. .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
  233. },
  234. };
  235. #define SJPEG_NUM_FORMATS ARRAY_SIZE(sjpeg_formats)
  236. static const unsigned char qtbl_luminance[4][64] = {
  237. {/*level 0 - high compression quality */
  238. 20, 16, 25, 39, 50, 46, 62, 68,
  239. 16, 18, 23, 38, 38, 53, 65, 68,
  240. 25, 23, 31, 38, 53, 65, 68, 68,
  241. 39, 38, 38, 53, 65, 68, 68, 68,
  242. 50, 38, 53, 65, 68, 68, 68, 68,
  243. 46, 53, 65, 68, 68, 68, 68, 68,
  244. 62, 65, 68, 68, 68, 68, 68, 68,
  245. 68, 68, 68, 68, 68, 68, 68, 68
  246. },
  247. {/* level 1 */
  248. 16, 11, 11, 16, 23, 27, 31, 30,
  249. 11, 12, 12, 15, 20, 23, 23, 30,
  250. 11, 12, 13, 16, 23, 26, 35, 47,
  251. 16, 15, 16, 23, 26, 37, 47, 64,
  252. 23, 20, 23, 26, 39, 51, 64, 64,
  253. 27, 23, 26, 37, 51, 64, 64, 64,
  254. 31, 23, 35, 47, 64, 64, 64, 64,
  255. 30, 30, 47, 64, 64, 64, 64, 64
  256. },
  257. {/* level 2 */
  258. 12, 8, 8, 12, 17, 21, 24, 23,
  259. 8, 9, 9, 11, 15, 19, 18, 23,
  260. 8, 9, 10, 12, 19, 20, 27, 36,
  261. 12, 11, 12, 21, 20, 28, 36, 53,
  262. 17, 15, 19, 20, 30, 39, 51, 59,
  263. 21, 19, 20, 28, 39, 51, 59, 59,
  264. 24, 18, 27, 36, 51, 59, 59, 59,
  265. 23, 23, 36, 53, 59, 59, 59, 59
  266. },
  267. {/* level 3 - low compression quality */
  268. 8, 6, 6, 8, 12, 14, 16, 17,
  269. 6, 6, 6, 8, 10, 13, 12, 15,
  270. 6, 6, 7, 8, 13, 14, 18, 24,
  271. 8, 8, 8, 14, 13, 19, 24, 35,
  272. 12, 10, 13, 13, 20, 26, 34, 39,
  273. 14, 13, 14, 19, 26, 34, 39, 39,
  274. 16, 12, 18, 24, 34, 39, 39, 39,
  275. 17, 15, 24, 35, 39, 39, 39, 39
  276. }
  277. };
  278. static const unsigned char qtbl_chrominance[4][64] = {
  279. {/*level 0 - high compression quality */
  280. 21, 25, 32, 38, 54, 68, 68, 68,
  281. 25, 28, 24, 38, 54, 68, 68, 68,
  282. 32, 24, 32, 43, 66, 68, 68, 68,
  283. 38, 38, 43, 53, 68, 68, 68, 68,
  284. 54, 54, 66, 68, 68, 68, 68, 68,
  285. 68, 68, 68, 68, 68, 68, 68, 68,
  286. 68, 68, 68, 68, 68, 68, 68, 68,
  287. 68, 68, 68, 68, 68, 68, 68, 68
  288. },
  289. {/* level 1 */
  290. 17, 15, 17, 21, 20, 26, 38, 48,
  291. 15, 19, 18, 17, 20, 26, 35, 43,
  292. 17, 18, 20, 22, 26, 30, 46, 53,
  293. 21, 17, 22, 28, 30, 39, 53, 64,
  294. 20, 20, 26, 30, 39, 48, 64, 64,
  295. 26, 26, 30, 39, 48, 63, 64, 64,
  296. 38, 35, 46, 53, 64, 64, 64, 64,
  297. 48, 43, 53, 64, 64, 64, 64, 64
  298. },
  299. {/* level 2 */
  300. 13, 11, 13, 16, 20, 20, 29, 37,
  301. 11, 14, 14, 14, 16, 20, 26, 32,
  302. 13, 14, 15, 17, 20, 23, 35, 40,
  303. 16, 14, 17, 21, 23, 30, 40, 50,
  304. 20, 16, 20, 23, 30, 37, 50, 59,
  305. 20, 20, 23, 30, 37, 48, 59, 59,
  306. 29, 26, 35, 40, 50, 59, 59, 59,
  307. 37, 32, 40, 50, 59, 59, 59, 59
  308. },
  309. {/* level 3 - low compression quality */
  310. 9, 8, 9, 11, 14, 17, 19, 24,
  311. 8, 10, 9, 11, 14, 13, 17, 22,
  312. 9, 9, 13, 14, 13, 15, 23, 26,
  313. 11, 11, 14, 14, 15, 20, 26, 33,
  314. 14, 14, 13, 15, 20, 24, 33, 39,
  315. 17, 13, 15, 20, 24, 32, 39, 39,
  316. 19, 17, 23, 26, 33, 39, 39, 39,
  317. 24, 22, 26, 33, 39, 39, 39, 39
  318. }
  319. };
  320. static const unsigned char hdctbl0[16] = {
  321. 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
  322. };
  323. static const unsigned char hdctblg0[12] = {
  324. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
  325. };
  326. static const unsigned char hactbl0[16] = {
  327. 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
  328. };
  329. static const unsigned char hactblg0[162] = {
  330. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  331. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  332. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  333. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  334. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  335. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  336. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  337. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  338. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  339. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  340. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  341. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  342. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  343. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  344. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  345. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  346. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  347. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  348. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  349. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  350. 0xf9, 0xfa
  351. };
  352. /*
  353. * Fourcc downgrade schema lookup tables for 422 and 420
  354. * chroma subsampling - fourcc on each position maps on the
  355. * fourcc from the table fourcc_to_dwngrd_schema_id which allows
  356. * to get the most suitable fourcc counterpart for the given
  357. * downgraded subsampling property.
  358. */
  359. static const u32 subs422_fourcc_dwngrd_schema[] = {
  360. V4L2_PIX_FMT_NV16,
  361. V4L2_PIX_FMT_NV61,
  362. };
  363. static const u32 subs420_fourcc_dwngrd_schema[] = {
  364. V4L2_PIX_FMT_NV12,
  365. V4L2_PIX_FMT_NV21,
  366. V4L2_PIX_FMT_NV12,
  367. V4L2_PIX_FMT_NV21,
  368. V4L2_PIX_FMT_NV12,
  369. V4L2_PIX_FMT_NV21,
  370. V4L2_PIX_FMT_GREY,
  371. V4L2_PIX_FMT_GREY,
  372. V4L2_PIX_FMT_GREY,
  373. V4L2_PIX_FMT_GREY,
  374. };
  375. /*
  376. * Lookup table for translation of a fourcc to the position
  377. * of its downgraded counterpart in the *fourcc_dwngrd_schema
  378. * tables.
  379. */
  380. static const u32 fourcc_to_dwngrd_schema_id[] = {
  381. V4L2_PIX_FMT_NV24,
  382. V4L2_PIX_FMT_NV42,
  383. V4L2_PIX_FMT_NV16,
  384. V4L2_PIX_FMT_NV61,
  385. V4L2_PIX_FMT_YUYV,
  386. V4L2_PIX_FMT_YVYU,
  387. V4L2_PIX_FMT_NV12,
  388. V4L2_PIX_FMT_NV21,
  389. V4L2_PIX_FMT_YUV420,
  390. V4L2_PIX_FMT_GREY,
  391. };
  392. static int s5p_jpeg_get_dwngrd_sch_id_by_fourcc(u32 fourcc)
  393. {
  394. int i;
  395. for (i = 0; i < ARRAY_SIZE(fourcc_to_dwngrd_schema_id); ++i) {
  396. if (fourcc_to_dwngrd_schema_id[i] == fourcc)
  397. return i;
  398. }
  399. return -EINVAL;
  400. }
  401. static int s5p_jpeg_adjust_fourcc_to_subsampling(
  402. enum v4l2_jpeg_chroma_subsampling subs,
  403. u32 in_fourcc,
  404. u32 *out_fourcc,
  405. struct s5p_jpeg_ctx *ctx)
  406. {
  407. int dwngrd_sch_id;
  408. if (ctx->subsampling != V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
  409. dwngrd_sch_id =
  410. s5p_jpeg_get_dwngrd_sch_id_by_fourcc(in_fourcc);
  411. if (dwngrd_sch_id < 0)
  412. return -EINVAL;
  413. }
  414. switch (ctx->subsampling) {
  415. case V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY:
  416. *out_fourcc = V4L2_PIX_FMT_GREY;
  417. break;
  418. case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
  419. if (dwngrd_sch_id >
  420. ARRAY_SIZE(subs420_fourcc_dwngrd_schema) - 1)
  421. return -EINVAL;
  422. *out_fourcc = subs420_fourcc_dwngrd_schema[dwngrd_sch_id];
  423. break;
  424. case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
  425. if (dwngrd_sch_id >
  426. ARRAY_SIZE(subs422_fourcc_dwngrd_schema) - 1)
  427. return -EINVAL;
  428. *out_fourcc = subs422_fourcc_dwngrd_schema[dwngrd_sch_id];
  429. break;
  430. default:
  431. *out_fourcc = V4L2_PIX_FMT_GREY;
  432. break;
  433. }
  434. return 0;
  435. }
  436. static int exynos4x12_decoded_subsampling[] = {
  437. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
  438. V4L2_JPEG_CHROMA_SUBSAMPLING_444,
  439. V4L2_JPEG_CHROMA_SUBSAMPLING_422,
  440. V4L2_JPEG_CHROMA_SUBSAMPLING_420,
  441. };
  442. static inline struct s5p_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
  443. {
  444. return container_of(c->handler, struct s5p_jpeg_ctx, ctrl_handler);
  445. }
  446. static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
  447. {
  448. return container_of(fh, struct s5p_jpeg_ctx, fh);
  449. }
  450. static int s5p_jpeg_to_user_subsampling(struct s5p_jpeg_ctx *ctx)
  451. {
  452. WARN_ON(ctx->subsampling > 3);
  453. if (ctx->jpeg->variant->version == SJPEG_S5P) {
  454. if (ctx->subsampling > 2)
  455. return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  456. return ctx->subsampling;
  457. } else {
  458. if (ctx->subsampling > 2)
  459. return V4L2_JPEG_CHROMA_SUBSAMPLING_420;
  460. return exynos4x12_decoded_subsampling[ctx->subsampling];
  461. }
  462. }
  463. static inline void s5p_jpeg_set_qtbl(void __iomem *regs,
  464. const unsigned char *qtbl,
  465. unsigned long tab, int len)
  466. {
  467. int i;
  468. for (i = 0; i < len; i++)
  469. writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
  470. }
  471. static inline void s5p_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  472. {
  473. /* this driver fills quantisation table 0 with data for luma */
  474. s5p_jpeg_set_qtbl(regs, qtbl_luminance[quality],
  475. S5P_JPG_QTBL_CONTENT(0),
  476. ARRAY_SIZE(qtbl_luminance[quality]));
  477. }
  478. static inline void s5p_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  479. {
  480. /* this driver fills quantisation table 1 with data for chroma */
  481. s5p_jpeg_set_qtbl(regs, qtbl_chrominance[quality],
  482. S5P_JPG_QTBL_CONTENT(1),
  483. ARRAY_SIZE(qtbl_chrominance[quality]));
  484. }
  485. static inline void s5p_jpeg_set_htbl(void __iomem *regs,
  486. const unsigned char *htbl,
  487. unsigned long tab, int len)
  488. {
  489. int i;
  490. for (i = 0; i < len; i++)
  491. writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
  492. }
  493. static inline void s5p_jpeg_set_hdctbl(void __iomem *regs)
  494. {
  495. /* this driver fills table 0 for this component */
  496. s5p_jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0),
  497. ARRAY_SIZE(hdctbl0));
  498. }
  499. static inline void s5p_jpeg_set_hdctblg(void __iomem *regs)
  500. {
  501. /* this driver fills table 0 for this component */
  502. s5p_jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0),
  503. ARRAY_SIZE(hdctblg0));
  504. }
  505. static inline void s5p_jpeg_set_hactbl(void __iomem *regs)
  506. {
  507. /* this driver fills table 0 for this component */
  508. s5p_jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0),
  509. ARRAY_SIZE(hactbl0));
  510. }
  511. static inline void s5p_jpeg_set_hactblg(void __iomem *regs)
  512. {
  513. /* this driver fills table 0 for this component */
  514. s5p_jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0),
  515. ARRAY_SIZE(hactblg0));
  516. }
  517. static inline void exynos4_jpeg_set_tbl(void __iomem *regs,
  518. const unsigned char *tbl,
  519. unsigned long tab, int len)
  520. {
  521. int i;
  522. unsigned int dword;
  523. for (i = 0; i < len; i += 4) {
  524. dword = tbl[i] |
  525. (tbl[i + 1] << 8) |
  526. (tbl[i + 2] << 16) |
  527. (tbl[i + 3] << 24);
  528. writel(dword, regs + tab + i);
  529. }
  530. }
  531. static inline void exynos4_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  532. {
  533. /* this driver fills quantisation table 0 with data for luma */
  534. exynos4_jpeg_set_tbl(regs, qtbl_luminance[quality],
  535. EXYNOS4_QTBL_CONTENT(0),
  536. ARRAY_SIZE(qtbl_luminance[quality]));
  537. }
  538. static inline void exynos4_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  539. {
  540. /* this driver fills quantisation table 1 with data for chroma */
  541. exynos4_jpeg_set_tbl(regs, qtbl_chrominance[quality],
  542. EXYNOS4_QTBL_CONTENT(1),
  543. ARRAY_SIZE(qtbl_chrominance[quality]));
  544. }
  545. void exynos4_jpeg_set_huff_tbl(void __iomem *base)
  546. {
  547. exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCLL,
  548. ARRAY_SIZE(hdctbl0));
  549. exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCCL,
  550. ARRAY_SIZE(hdctbl0));
  551. exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCLV,
  552. ARRAY_SIZE(hdctblg0));
  553. exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCCV,
  554. ARRAY_SIZE(hdctblg0));
  555. exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACLL,
  556. ARRAY_SIZE(hactbl0));
  557. exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACCL,
  558. ARRAY_SIZE(hactbl0));
  559. exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACLV,
  560. ARRAY_SIZE(hactblg0));
  561. exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACCV,
  562. ARRAY_SIZE(hactblg0));
  563. }
  564. /*
  565. * ============================================================================
  566. * Device file operations
  567. * ============================================================================
  568. */
  569. static int queue_init(void *priv, struct vb2_queue *src_vq,
  570. struct vb2_queue *dst_vq);
  571. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
  572. __u32 pixelformat, unsigned int fmt_type);
  573. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
  574. static int s5p_jpeg_open(struct file *file)
  575. {
  576. struct s5p_jpeg *jpeg = video_drvdata(file);
  577. struct video_device *vfd = video_devdata(file);
  578. struct s5p_jpeg_ctx *ctx;
  579. struct s5p_jpeg_fmt *out_fmt, *cap_fmt;
  580. int ret = 0;
  581. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  582. if (!ctx)
  583. return -ENOMEM;
  584. if (mutex_lock_interruptible(&jpeg->lock)) {
  585. ret = -ERESTARTSYS;
  586. goto free;
  587. }
  588. v4l2_fh_init(&ctx->fh, vfd);
  589. /* Use separate control handler per file handle */
  590. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  591. file->private_data = &ctx->fh;
  592. v4l2_fh_add(&ctx->fh);
  593. ctx->jpeg = jpeg;
  594. if (vfd == jpeg->vfd_encoder) {
  595. ctx->mode = S5P_JPEG_ENCODE;
  596. out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_RGB565,
  597. FMT_TYPE_OUTPUT);
  598. cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
  599. FMT_TYPE_CAPTURE);
  600. } else {
  601. ctx->mode = S5P_JPEG_DECODE;
  602. out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
  603. FMT_TYPE_OUTPUT);
  604. cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
  605. FMT_TYPE_CAPTURE);
  606. }
  607. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
  608. if (IS_ERR(ctx->fh.m2m_ctx)) {
  609. ret = PTR_ERR(ctx->fh.m2m_ctx);
  610. goto error;
  611. }
  612. ctx->out_q.fmt = out_fmt;
  613. ctx->cap_q.fmt = cap_fmt;
  614. ret = s5p_jpeg_controls_create(ctx);
  615. if (ret < 0)
  616. goto error;
  617. mutex_unlock(&jpeg->lock);
  618. return 0;
  619. error:
  620. v4l2_fh_del(&ctx->fh);
  621. v4l2_fh_exit(&ctx->fh);
  622. mutex_unlock(&jpeg->lock);
  623. free:
  624. kfree(ctx);
  625. return ret;
  626. }
  627. static int s5p_jpeg_release(struct file *file)
  628. {
  629. struct s5p_jpeg *jpeg = video_drvdata(file);
  630. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  631. mutex_lock(&jpeg->lock);
  632. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  633. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  634. v4l2_fh_del(&ctx->fh);
  635. v4l2_fh_exit(&ctx->fh);
  636. kfree(ctx);
  637. mutex_unlock(&jpeg->lock);
  638. return 0;
  639. }
  640. static const struct v4l2_file_operations s5p_jpeg_fops = {
  641. .owner = THIS_MODULE,
  642. .open = s5p_jpeg_open,
  643. .release = s5p_jpeg_release,
  644. .poll = v4l2_m2m_fop_poll,
  645. .unlocked_ioctl = video_ioctl2,
  646. .mmap = v4l2_m2m_fop_mmap,
  647. };
  648. /*
  649. * ============================================================================
  650. * video ioctl operations
  651. * ============================================================================
  652. */
  653. static int get_byte(struct s5p_jpeg_buffer *buf)
  654. {
  655. if (buf->curr >= buf->size)
  656. return -1;
  657. return ((unsigned char *)buf->data)[buf->curr++];
  658. }
  659. static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
  660. {
  661. unsigned int temp;
  662. int byte;
  663. byte = get_byte(buf);
  664. if (byte == -1)
  665. return -1;
  666. temp = byte << 8;
  667. byte = get_byte(buf);
  668. if (byte == -1)
  669. return -1;
  670. *word = (unsigned int)byte | temp;
  671. return 0;
  672. }
  673. static void skip(struct s5p_jpeg_buffer *buf, long len)
  674. {
  675. if (len <= 0)
  676. return;
  677. while (len--)
  678. get_byte(buf);
  679. }
  680. static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
  681. unsigned long buffer, unsigned long size,
  682. struct s5p_jpeg_ctx *ctx)
  683. {
  684. int c, components, notfound;
  685. unsigned int height, width, word, subsampling = 0;
  686. long length;
  687. struct s5p_jpeg_buffer jpeg_buffer;
  688. jpeg_buffer.size = size;
  689. jpeg_buffer.data = buffer;
  690. jpeg_buffer.curr = 0;
  691. notfound = 1;
  692. while (notfound) {
  693. c = get_byte(&jpeg_buffer);
  694. if (c == -1)
  695. break;
  696. if (c != 0xff)
  697. continue;
  698. do
  699. c = get_byte(&jpeg_buffer);
  700. while (c == 0xff);
  701. if (c == -1)
  702. break;
  703. if (c == 0)
  704. continue;
  705. length = 0;
  706. switch (c) {
  707. /* SOF0: baseline JPEG */
  708. case SOF0:
  709. if (get_word_be(&jpeg_buffer, &word))
  710. break;
  711. if (get_byte(&jpeg_buffer) == -1)
  712. break;
  713. if (get_word_be(&jpeg_buffer, &height))
  714. break;
  715. if (get_word_be(&jpeg_buffer, &width))
  716. break;
  717. components = get_byte(&jpeg_buffer);
  718. if (components == -1)
  719. break;
  720. notfound = 0;
  721. if (components == 1) {
  722. subsampling = 0x33;
  723. } else {
  724. skip(&jpeg_buffer, 1);
  725. subsampling = get_byte(&jpeg_buffer);
  726. skip(&jpeg_buffer, 1);
  727. }
  728. skip(&jpeg_buffer, components * 2);
  729. break;
  730. /* skip payload-less markers */
  731. case RST ... RST + 7:
  732. case SOI:
  733. case EOI:
  734. case TEM:
  735. break;
  736. /* skip uninteresting payload markers */
  737. default:
  738. if (get_word_be(&jpeg_buffer, &word))
  739. break;
  740. length = (long)word - 2;
  741. skip(&jpeg_buffer, length);
  742. break;
  743. }
  744. }
  745. result->w = width;
  746. result->h = height;
  747. result->size = components;
  748. switch (subsampling) {
  749. case 0x11:
  750. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444;
  751. break;
  752. case 0x21:
  753. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422;
  754. break;
  755. case 0x22:
  756. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420;
  757. break;
  758. case 0x33:
  759. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  760. break;
  761. default:
  762. return false;
  763. }
  764. return !notfound;
  765. }
  766. static int s5p_jpeg_querycap(struct file *file, void *priv,
  767. struct v4l2_capability *cap)
  768. {
  769. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  770. if (ctx->mode == S5P_JPEG_ENCODE) {
  771. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
  772. sizeof(cap->driver));
  773. strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
  774. sizeof(cap->card));
  775. } else {
  776. strlcpy(cap->driver, S5P_JPEG_M2M_NAME " decoder",
  777. sizeof(cap->driver));
  778. strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
  779. sizeof(cap->card));
  780. }
  781. cap->bus_info[0] = 0;
  782. /*
  783. * This is only a mem-to-mem video device. The capture and output
  784. * device capability flags are left only for backward compatibility
  785. * and are scheduled for removal.
  786. */
  787. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
  788. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
  789. return 0;
  790. }
  791. static int enum_fmt(struct s5p_jpeg_fmt *sjpeg_formats, int n,
  792. struct v4l2_fmtdesc *f, u32 type)
  793. {
  794. int i, num = 0;
  795. for (i = 0; i < n; ++i) {
  796. if (sjpeg_formats[i].flags & type) {
  797. /* index-th format of type type found ? */
  798. if (num == f->index)
  799. break;
  800. /* Correct type but haven't reached our index yet,
  801. * just increment per-type index */
  802. ++num;
  803. }
  804. }
  805. /* Format not found */
  806. if (i >= n)
  807. return -EINVAL;
  808. strlcpy(f->description, sjpeg_formats[i].name, sizeof(f->description));
  809. f->pixelformat = sjpeg_formats[i].fourcc;
  810. return 0;
  811. }
  812. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  813. struct v4l2_fmtdesc *f)
  814. {
  815. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  816. if (ctx->mode == S5P_JPEG_ENCODE)
  817. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  818. SJPEG_FMT_FLAG_ENC_CAPTURE);
  819. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  820. SJPEG_FMT_FLAG_DEC_CAPTURE);
  821. }
  822. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  823. struct v4l2_fmtdesc *f)
  824. {
  825. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  826. if (ctx->mode == S5P_JPEG_ENCODE)
  827. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  828. SJPEG_FMT_FLAG_ENC_OUTPUT);
  829. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  830. SJPEG_FMT_FLAG_DEC_OUTPUT);
  831. }
  832. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  833. enum v4l2_buf_type type)
  834. {
  835. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  836. return &ctx->out_q;
  837. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  838. return &ctx->cap_q;
  839. return NULL;
  840. }
  841. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  842. {
  843. struct vb2_queue *vq;
  844. struct s5p_jpeg_q_data *q_data = NULL;
  845. struct v4l2_pix_format *pix = &f->fmt.pix;
  846. struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
  847. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  848. if (!vq)
  849. return -EINVAL;
  850. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  851. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  852. return -EINVAL;
  853. q_data = get_q_data(ct, f->type);
  854. BUG_ON(q_data == NULL);
  855. pix->width = q_data->w;
  856. pix->height = q_data->h;
  857. pix->field = V4L2_FIELD_NONE;
  858. pix->pixelformat = q_data->fmt->fourcc;
  859. pix->bytesperline = 0;
  860. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  861. u32 bpl = q_data->w;
  862. if (q_data->fmt->colplanes == 1)
  863. bpl = (bpl * q_data->fmt->depth) >> 3;
  864. pix->bytesperline = bpl;
  865. }
  866. pix->sizeimage = q_data->size;
  867. return 0;
  868. }
  869. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
  870. u32 pixelformat, unsigned int fmt_type)
  871. {
  872. unsigned int k, fmt_flag;
  873. if (ctx->mode == S5P_JPEG_ENCODE)
  874. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  875. SJPEG_FMT_FLAG_ENC_OUTPUT :
  876. SJPEG_FMT_FLAG_ENC_CAPTURE;
  877. else
  878. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  879. SJPEG_FMT_FLAG_DEC_OUTPUT :
  880. SJPEG_FMT_FLAG_DEC_CAPTURE;
  881. for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
  882. struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
  883. if (fmt->fourcc == pixelformat &&
  884. fmt->flags & fmt_flag &&
  885. fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
  886. return fmt;
  887. }
  888. }
  889. return NULL;
  890. }
  891. static void jpeg_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  892. unsigned int walign,
  893. u32 *h, unsigned int hmin, unsigned int hmax,
  894. unsigned int halign)
  895. {
  896. int width, height, w_step, h_step;
  897. width = *w;
  898. height = *h;
  899. w_step = 1 << walign;
  900. h_step = 1 << halign;
  901. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  902. if (*w < width && (*w + w_step) < wmax)
  903. *w += w_step;
  904. if (*h < height && (*h + h_step) < hmax)
  905. *h += h_step;
  906. }
  907. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  908. struct s5p_jpeg_ctx *ctx, int q_type)
  909. {
  910. struct v4l2_pix_format *pix = &f->fmt.pix;
  911. if (pix->field == V4L2_FIELD_ANY)
  912. pix->field = V4L2_FIELD_NONE;
  913. else if (pix->field != V4L2_FIELD_NONE)
  914. return -EINVAL;
  915. /* V4L2 specification suggests the driver corrects the format struct
  916. * if any of the dimensions is unsupported */
  917. if (q_type == FMT_TYPE_OUTPUT)
  918. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  919. S5P_JPEG_MAX_WIDTH, 0,
  920. &pix->height, S5P_JPEG_MIN_HEIGHT,
  921. S5P_JPEG_MAX_HEIGHT, 0);
  922. else
  923. jpeg_bound_align_image(&pix->width, S5P_JPEG_MIN_WIDTH,
  924. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  925. &pix->height, S5P_JPEG_MIN_HEIGHT,
  926. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  927. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  928. if (pix->sizeimage <= 0)
  929. pix->sizeimage = PAGE_SIZE;
  930. pix->bytesperline = 0;
  931. } else {
  932. u32 bpl = pix->bytesperline;
  933. if (fmt->colplanes > 1 && bpl < pix->width)
  934. bpl = pix->width; /* planar */
  935. if (fmt->colplanes == 1 && /* packed */
  936. (bpl << 3) / fmt->depth < pix->width)
  937. bpl = (pix->width * fmt->depth) >> 3;
  938. pix->bytesperline = bpl;
  939. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  940. }
  941. return 0;
  942. }
  943. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  944. struct v4l2_format *f)
  945. {
  946. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  947. struct v4l2_pix_format *pix = &f->fmt.pix;
  948. struct s5p_jpeg_fmt *fmt;
  949. int ret;
  950. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  951. FMT_TYPE_CAPTURE);
  952. if (!fmt) {
  953. v4l2_err(&ctx->jpeg->v4l2_dev,
  954. "Fourcc format (0x%08x) invalid.\n",
  955. f->fmt.pix.pixelformat);
  956. return -EINVAL;
  957. }
  958. if ((ctx->jpeg->variant->version != SJPEG_EXYNOS4) ||
  959. (ctx->mode != S5P_JPEG_DECODE))
  960. goto exit;
  961. /*
  962. * The exynos4x12 device requires resulting YUV image
  963. * subsampling not to be lower than the input jpeg subsampling.
  964. * If this requirement is not met then downgrade the requested
  965. * capture format to the one with subsampling equal to the input jpeg.
  966. */
  967. if ((fmt->flags & SJPEG_FMT_NON_RGB) &&
  968. (fmt->subsampling < ctx->subsampling)) {
  969. ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
  970. fmt->fourcc,
  971. &pix->pixelformat,
  972. ctx);
  973. if (ret < 0)
  974. pix->pixelformat = V4L2_PIX_FMT_GREY;
  975. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  976. FMT_TYPE_CAPTURE);
  977. }
  978. /*
  979. * Decompression of a JPEG file with 4:2:0 subsampling and odd
  980. * width to the YUV 4:2:0 compliant formats produces a raw image
  981. * with broken luma component. Adjust capture format to RGB565
  982. * in such a case.
  983. */
  984. if (ctx->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
  985. (ctx->out_q.w & 1) &&
  986. (pix->pixelformat == V4L2_PIX_FMT_NV12 ||
  987. pix->pixelformat == V4L2_PIX_FMT_NV21 ||
  988. pix->pixelformat == V4L2_PIX_FMT_YUV420)) {
  989. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  990. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  991. FMT_TYPE_CAPTURE);
  992. }
  993. exit:
  994. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
  995. }
  996. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  997. struct v4l2_format *f)
  998. {
  999. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1000. struct s5p_jpeg_fmt *fmt;
  1001. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  1002. FMT_TYPE_OUTPUT);
  1003. if (!fmt) {
  1004. v4l2_err(&ctx->jpeg->v4l2_dev,
  1005. "Fourcc format (0x%08x) invalid.\n",
  1006. f->fmt.pix.pixelformat);
  1007. return -EINVAL;
  1008. }
  1009. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
  1010. }
  1011. static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
  1012. struct v4l2_format *f,
  1013. int fmt_depth)
  1014. {
  1015. struct v4l2_pix_format *pix = &f->fmt.pix;
  1016. u32 pix_fmt = f->fmt.pix.pixelformat;
  1017. int w = pix->width, h = pix->height, wh_align;
  1018. if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
  1019. pix_fmt == V4L2_PIX_FMT_NV24 ||
  1020. pix_fmt == V4L2_PIX_FMT_NV42 ||
  1021. pix_fmt == V4L2_PIX_FMT_NV12 ||
  1022. pix_fmt == V4L2_PIX_FMT_NV21 ||
  1023. pix_fmt == V4L2_PIX_FMT_YUV420)
  1024. wh_align = 4;
  1025. else
  1026. wh_align = 1;
  1027. jpeg_bound_align_image(&w, S5P_JPEG_MIN_WIDTH,
  1028. S5P_JPEG_MAX_WIDTH, wh_align,
  1029. &h, S5P_JPEG_MIN_HEIGHT,
  1030. S5P_JPEG_MAX_HEIGHT, wh_align);
  1031. return w * h * fmt_depth >> 3;
  1032. }
  1033. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  1034. {
  1035. struct vb2_queue *vq;
  1036. struct s5p_jpeg_q_data *q_data = NULL;
  1037. struct v4l2_pix_format *pix = &f->fmt.pix;
  1038. struct v4l2_ctrl *ctrl_subs;
  1039. unsigned int f_type;
  1040. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  1041. if (!vq)
  1042. return -EINVAL;
  1043. q_data = get_q_data(ct, f->type);
  1044. BUG_ON(q_data == NULL);
  1045. if (vb2_is_busy(vq)) {
  1046. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  1047. return -EBUSY;
  1048. }
  1049. f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
  1050. FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
  1051. q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
  1052. q_data->w = pix->width;
  1053. q_data->h = pix->height;
  1054. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  1055. /*
  1056. * During encoding Exynos4x12 SoCs access wider memory area
  1057. * than it results from Image_x and Image_y values written to
  1058. * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
  1059. * page fault calculate proper buffer size in such a case.
  1060. */
  1061. if (ct->jpeg->variant->version == SJPEG_EXYNOS4 &&
  1062. f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
  1063. q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
  1064. f,
  1065. q_data->fmt->depth);
  1066. else
  1067. q_data->size = q_data->w * q_data->h *
  1068. q_data->fmt->depth >> 3;
  1069. } else {
  1070. q_data->size = pix->sizeimage;
  1071. }
  1072. if (f_type == FMT_TYPE_OUTPUT) {
  1073. ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
  1074. V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
  1075. if (ctrl_subs)
  1076. v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
  1077. }
  1078. return 0;
  1079. }
  1080. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  1081. struct v4l2_format *f)
  1082. {
  1083. int ret;
  1084. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  1085. if (ret)
  1086. return ret;
  1087. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1088. }
  1089. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  1090. struct v4l2_format *f)
  1091. {
  1092. int ret;
  1093. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  1094. if (ret)
  1095. return ret;
  1096. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1097. }
  1098. static int s5p_jpeg_g_selection(struct file *file, void *priv,
  1099. struct v4l2_selection *s)
  1100. {
  1101. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1102. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  1103. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1104. return -EINVAL;
  1105. /* For JPEG blob active == default == bounds */
  1106. switch (s->target) {
  1107. case V4L2_SEL_TGT_CROP:
  1108. case V4L2_SEL_TGT_CROP_BOUNDS:
  1109. case V4L2_SEL_TGT_CROP_DEFAULT:
  1110. case V4L2_SEL_TGT_COMPOSE:
  1111. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1112. s->r.width = ctx->out_q.w;
  1113. s->r.height = ctx->out_q.h;
  1114. break;
  1115. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1116. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1117. s->r.width = ctx->cap_q.w;
  1118. s->r.height = ctx->cap_q.h;
  1119. break;
  1120. default:
  1121. return -EINVAL;
  1122. }
  1123. s->r.left = 0;
  1124. s->r.top = 0;
  1125. return 0;
  1126. }
  1127. /*
  1128. * V4L2 controls
  1129. */
  1130. static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1131. {
  1132. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1133. struct s5p_jpeg *jpeg = ctx->jpeg;
  1134. unsigned long flags;
  1135. switch (ctrl->id) {
  1136. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1137. spin_lock_irqsave(&jpeg->slock, flags);
  1138. ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
  1139. spin_unlock_irqrestore(&jpeg->slock, flags);
  1140. break;
  1141. }
  1142. return 0;
  1143. }
  1144. static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
  1145. {
  1146. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1147. unsigned long flags;
  1148. int ret = 0;
  1149. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1150. if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING) {
  1151. if (ctx->jpeg->variant->version == SJPEG_S5P)
  1152. goto error_free;
  1153. /*
  1154. * The exynos4x12 device requires input raw image fourcc
  1155. * to be V4L2_PIX_FMT_GREY if gray jpeg format
  1156. * is to be set.
  1157. */
  1158. if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
  1159. ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY) {
  1160. ret = -EINVAL;
  1161. goto error_free;
  1162. }
  1163. /*
  1164. * The exynos4x12 device requires resulting jpeg subsampling
  1165. * not to be lower than the input raw image subsampling.
  1166. */
  1167. if (ctx->out_q.fmt->subsampling > ctrl->val)
  1168. ctrl->val = ctx->out_q.fmt->subsampling;
  1169. }
  1170. error_free:
  1171. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1172. return ret;
  1173. }
  1174. static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
  1175. {
  1176. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1177. unsigned long flags;
  1178. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1179. switch (ctrl->id) {
  1180. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1181. ctx->compr_quality = ctrl->val;
  1182. break;
  1183. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1184. ctx->restart_interval = ctrl->val;
  1185. break;
  1186. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1187. ctx->subsampling = ctrl->val;
  1188. break;
  1189. }
  1190. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1191. return 0;
  1192. }
  1193. static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
  1194. .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
  1195. .try_ctrl = s5p_jpeg_try_ctrl,
  1196. .s_ctrl = s5p_jpeg_s_ctrl,
  1197. };
  1198. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
  1199. {
  1200. unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
  1201. struct v4l2_ctrl *ctrl;
  1202. int ret;
  1203. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  1204. if (ctx->mode == S5P_JPEG_ENCODE) {
  1205. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1206. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  1207. 0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
  1208. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1209. V4L2_CID_JPEG_RESTART_INTERVAL,
  1210. 0, 3, 0xffff, 0);
  1211. if (ctx->jpeg->variant->version == SJPEG_S5P)
  1212. mask = ~0x06; /* 422, 420 */
  1213. }
  1214. ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1215. V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
  1216. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
  1217. V4L2_JPEG_CHROMA_SUBSAMPLING_422);
  1218. if (ctx->ctrl_handler.error) {
  1219. ret = ctx->ctrl_handler.error;
  1220. goto error_free;
  1221. }
  1222. if (ctx->mode == S5P_JPEG_DECODE)
  1223. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  1224. V4L2_CTRL_FLAG_READ_ONLY;
  1225. ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1226. if (ret < 0)
  1227. goto error_free;
  1228. return ret;
  1229. error_free:
  1230. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1231. return ret;
  1232. }
  1233. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  1234. .vidioc_querycap = s5p_jpeg_querycap,
  1235. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  1236. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  1237. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  1238. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  1239. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  1240. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  1241. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  1242. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  1243. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1244. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1245. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1246. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1247. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1248. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1249. .vidioc_g_selection = s5p_jpeg_g_selection,
  1250. };
  1251. /*
  1252. * ============================================================================
  1253. * mem2mem callbacks
  1254. * ============================================================================
  1255. */
  1256. static void s5p_jpeg_device_run(void *priv)
  1257. {
  1258. struct s5p_jpeg_ctx *ctx = priv;
  1259. struct s5p_jpeg *jpeg = ctx->jpeg;
  1260. struct vb2_buffer *src_buf, *dst_buf;
  1261. unsigned long src_addr, dst_addr, flags;
  1262. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1263. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1264. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1265. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  1266. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  1267. s5p_jpeg_reset(jpeg->regs);
  1268. s5p_jpeg_poweron(jpeg->regs);
  1269. s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
  1270. if (ctx->mode == S5P_JPEG_ENCODE) {
  1271. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  1272. s5p_jpeg_input_raw_mode(jpeg->regs,
  1273. S5P_JPEG_RAW_IN_565);
  1274. else
  1275. s5p_jpeg_input_raw_mode(jpeg->regs,
  1276. S5P_JPEG_RAW_IN_422);
  1277. s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  1278. s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
  1279. s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
  1280. s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
  1281. s5p_jpeg_imgadr(jpeg->regs, src_addr);
  1282. s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
  1283. /* ultimately comes from sizeimage from userspace */
  1284. s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  1285. /* JPEG RGB to YCbCr conversion matrix */
  1286. s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  1287. s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  1288. s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  1289. s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  1290. s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  1291. s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  1292. s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  1293. s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  1294. s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  1295. /*
  1296. * JPEG IP allows storing 4 quantization tables
  1297. * We fill table 0 for luma and table 1 for chroma
  1298. */
  1299. s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1300. s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1301. /* use table 0 for Y */
  1302. s5p_jpeg_qtbl(jpeg->regs, 1, 0);
  1303. /* use table 1 for Cb and Cr*/
  1304. s5p_jpeg_qtbl(jpeg->regs, 2, 1);
  1305. s5p_jpeg_qtbl(jpeg->regs, 3, 1);
  1306. /* Y, Cb, Cr use Huffman table 0 */
  1307. s5p_jpeg_htbl_ac(jpeg->regs, 1);
  1308. s5p_jpeg_htbl_dc(jpeg->regs, 1);
  1309. s5p_jpeg_htbl_ac(jpeg->regs, 2);
  1310. s5p_jpeg_htbl_dc(jpeg->regs, 2);
  1311. s5p_jpeg_htbl_ac(jpeg->regs, 3);
  1312. s5p_jpeg_htbl_dc(jpeg->regs, 3);
  1313. } else { /* S5P_JPEG_DECODE */
  1314. s5p_jpeg_rst_int_enable(jpeg->regs, true);
  1315. s5p_jpeg_data_num_int_enable(jpeg->regs, true);
  1316. s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  1317. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  1318. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  1319. else
  1320. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
  1321. s5p_jpeg_jpgadr(jpeg->regs, src_addr);
  1322. s5p_jpeg_imgadr(jpeg->regs, dst_addr);
  1323. }
  1324. s5p_jpeg_start(jpeg->regs);
  1325. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1326. }
  1327. static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
  1328. {
  1329. struct s5p_jpeg *jpeg = ctx->jpeg;
  1330. struct s5p_jpeg_fmt *fmt;
  1331. struct vb2_buffer *vb;
  1332. struct s5p_jpeg_addr jpeg_addr;
  1333. u32 pix_size, padding_bytes = 0;
  1334. pix_size = ctx->cap_q.w * ctx->cap_q.h;
  1335. if (ctx->mode == S5P_JPEG_ENCODE) {
  1336. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1337. fmt = ctx->out_q.fmt;
  1338. if (ctx->out_q.w % 2 && fmt->h_align > 0)
  1339. padding_bytes = ctx->out_q.h;
  1340. } else {
  1341. fmt = ctx->cap_q.fmt;
  1342. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1343. }
  1344. jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
  1345. if (fmt->colplanes == 2) {
  1346. jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
  1347. } else if (fmt->colplanes == 3) {
  1348. jpeg_addr.cb = jpeg_addr.y + pix_size;
  1349. if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
  1350. jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
  1351. else
  1352. jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
  1353. }
  1354. exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
  1355. }
  1356. static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
  1357. {
  1358. struct s5p_jpeg *jpeg = ctx->jpeg;
  1359. struct vb2_buffer *vb;
  1360. unsigned int jpeg_addr = 0;
  1361. if (ctx->mode == S5P_JPEG_ENCODE)
  1362. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1363. else
  1364. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1365. jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  1366. exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
  1367. }
  1368. static void exynos4_jpeg_device_run(void *priv)
  1369. {
  1370. struct s5p_jpeg_ctx *ctx = priv;
  1371. struct s5p_jpeg *jpeg = ctx->jpeg;
  1372. unsigned int bitstream_size;
  1373. unsigned long flags;
  1374. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1375. if (ctx->mode == S5P_JPEG_ENCODE) {
  1376. exynos4_jpeg_sw_reset(jpeg->regs);
  1377. exynos4_jpeg_set_interrupt(jpeg->regs);
  1378. exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
  1379. exynos4_jpeg_set_huff_tbl(jpeg->regs);
  1380. /*
  1381. * JPEG IP allows storing 4 quantization tables
  1382. * We fill table 0 for luma and table 1 for chroma
  1383. */
  1384. exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1385. exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1386. exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
  1387. ctx->compr_quality);
  1388. exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
  1389. ctx->cap_q.h);
  1390. exynos4_jpeg_set_enc_out_fmt(jpeg->regs, ctx->subsampling);
  1391. exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->out_q.fmt->fourcc);
  1392. exynos4_jpeg_set_img_addr(ctx);
  1393. exynos4_jpeg_set_jpeg_addr(ctx);
  1394. exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
  1395. ctx->out_q.fmt->fourcc);
  1396. } else {
  1397. exynos4_jpeg_sw_reset(jpeg->regs);
  1398. exynos4_jpeg_set_interrupt(jpeg->regs);
  1399. exynos4_jpeg_set_img_addr(ctx);
  1400. exynos4_jpeg_set_jpeg_addr(ctx);
  1401. exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->cap_q.fmt->fourcc);
  1402. bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
  1403. exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
  1404. }
  1405. exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
  1406. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1407. }
  1408. static int s5p_jpeg_job_ready(void *priv)
  1409. {
  1410. struct s5p_jpeg_ctx *ctx = priv;
  1411. if (ctx->mode == S5P_JPEG_DECODE)
  1412. return ctx->hdr_parsed;
  1413. return 1;
  1414. }
  1415. static void s5p_jpeg_job_abort(void *priv)
  1416. {
  1417. }
  1418. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  1419. .device_run = s5p_jpeg_device_run,
  1420. .job_ready = s5p_jpeg_job_ready,
  1421. .job_abort = s5p_jpeg_job_abort,
  1422. }
  1423. ;
  1424. static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
  1425. .device_run = exynos4_jpeg_device_run,
  1426. .job_ready = s5p_jpeg_job_ready,
  1427. .job_abort = s5p_jpeg_job_abort,
  1428. };
  1429. /*
  1430. * ============================================================================
  1431. * Queue operations
  1432. * ============================================================================
  1433. */
  1434. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  1435. const struct v4l2_format *fmt,
  1436. unsigned int *nbuffers, unsigned int *nplanes,
  1437. unsigned int sizes[], void *alloc_ctxs[])
  1438. {
  1439. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  1440. struct s5p_jpeg_q_data *q_data = NULL;
  1441. unsigned int size, count = *nbuffers;
  1442. q_data = get_q_data(ctx, vq->type);
  1443. BUG_ON(q_data == NULL);
  1444. size = q_data->size;
  1445. /*
  1446. * header is parsed during decoding and parsed information stored
  1447. * in the context so we do not allow another buffer to overwrite it
  1448. */
  1449. if (ctx->mode == S5P_JPEG_DECODE)
  1450. count = 1;
  1451. *nbuffers = count;
  1452. *nplanes = 1;
  1453. sizes[0] = size;
  1454. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  1455. return 0;
  1456. }
  1457. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  1458. {
  1459. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1460. struct s5p_jpeg_q_data *q_data = NULL;
  1461. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1462. BUG_ON(q_data == NULL);
  1463. if (vb2_plane_size(vb, 0) < q_data->size) {
  1464. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  1465. __func__, vb2_plane_size(vb, 0),
  1466. (long)q_data->size);
  1467. return -EINVAL;
  1468. }
  1469. vb2_set_plane_payload(vb, 0, q_data->size);
  1470. return 0;
  1471. }
  1472. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  1473. {
  1474. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1475. if (ctx->mode == S5P_JPEG_DECODE &&
  1476. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1477. struct s5p_jpeg_q_data tmp, *q_data;
  1478. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  1479. (unsigned long)vb2_plane_vaddr(vb, 0),
  1480. min((unsigned long)ctx->out_q.size,
  1481. vb2_get_plane_payload(vb, 0)), ctx);
  1482. if (!ctx->hdr_parsed) {
  1483. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1484. return;
  1485. }
  1486. q_data = &ctx->out_q;
  1487. q_data->w = tmp.w;
  1488. q_data->h = tmp.h;
  1489. q_data = &ctx->cap_q;
  1490. q_data->w = tmp.w;
  1491. q_data->h = tmp.h;
  1492. }
  1493. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  1494. }
  1495. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  1496. {
  1497. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  1498. int ret;
  1499. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  1500. return ret > 0 ? 0 : ret;
  1501. }
  1502. static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
  1503. {
  1504. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  1505. pm_runtime_put(ctx->jpeg->dev);
  1506. }
  1507. static struct vb2_ops s5p_jpeg_qops = {
  1508. .queue_setup = s5p_jpeg_queue_setup,
  1509. .buf_prepare = s5p_jpeg_buf_prepare,
  1510. .buf_queue = s5p_jpeg_buf_queue,
  1511. .wait_prepare = vb2_ops_wait_prepare,
  1512. .wait_finish = vb2_ops_wait_finish,
  1513. .start_streaming = s5p_jpeg_start_streaming,
  1514. .stop_streaming = s5p_jpeg_stop_streaming,
  1515. };
  1516. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1517. struct vb2_queue *dst_vq)
  1518. {
  1519. struct s5p_jpeg_ctx *ctx = priv;
  1520. int ret;
  1521. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1522. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1523. src_vq->drv_priv = ctx;
  1524. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1525. src_vq->ops = &s5p_jpeg_qops;
  1526. src_vq->mem_ops = &vb2_dma_contig_memops;
  1527. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1528. src_vq->lock = &ctx->jpeg->lock;
  1529. ret = vb2_queue_init(src_vq);
  1530. if (ret)
  1531. return ret;
  1532. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1533. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1534. dst_vq->drv_priv = ctx;
  1535. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1536. dst_vq->ops = &s5p_jpeg_qops;
  1537. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1538. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1539. dst_vq->lock = &ctx->jpeg->lock;
  1540. return vb2_queue_init(dst_vq);
  1541. }
  1542. /*
  1543. * ============================================================================
  1544. * ISR
  1545. * ============================================================================
  1546. */
  1547. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  1548. {
  1549. struct s5p_jpeg *jpeg = dev_id;
  1550. struct s5p_jpeg_ctx *curr_ctx;
  1551. struct vb2_buffer *src_buf, *dst_buf;
  1552. unsigned long payload_size = 0;
  1553. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  1554. bool enc_jpeg_too_large = false;
  1555. bool timer_elapsed = false;
  1556. bool op_completed = false;
  1557. spin_lock(&jpeg->slock);
  1558. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  1559. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  1560. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  1561. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1562. enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
  1563. timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
  1564. op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
  1565. if (curr_ctx->mode == S5P_JPEG_DECODE)
  1566. op_completed = op_completed &&
  1567. s5p_jpeg_stream_stat_ok(jpeg->regs);
  1568. if (enc_jpeg_too_large) {
  1569. state = VB2_BUF_STATE_ERROR;
  1570. s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
  1571. } else if (timer_elapsed) {
  1572. state = VB2_BUF_STATE_ERROR;
  1573. s5p_jpeg_clear_timer_stat(jpeg->regs);
  1574. } else if (!op_completed) {
  1575. state = VB2_BUF_STATE_ERROR;
  1576. } else {
  1577. payload_size = s5p_jpeg_compressed_size(jpeg->regs);
  1578. }
  1579. dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
  1580. dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
  1581. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  1582. dst_buf->v4l2_buf.flags |=
  1583. src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  1584. v4l2_m2m_buf_done(src_buf, state);
  1585. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1586. vb2_set_plane_payload(dst_buf, 0, payload_size);
  1587. v4l2_m2m_buf_done(dst_buf, state);
  1588. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  1589. curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
  1590. spin_unlock(&jpeg->slock);
  1591. s5p_jpeg_clear_int(jpeg->regs);
  1592. return IRQ_HANDLED;
  1593. }
  1594. static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
  1595. {
  1596. unsigned int int_status;
  1597. struct vb2_buffer *src_vb, *dst_vb;
  1598. struct s5p_jpeg *jpeg = priv;
  1599. struct s5p_jpeg_ctx *curr_ctx;
  1600. unsigned long payload_size = 0;
  1601. spin_lock(&jpeg->slock);
  1602. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  1603. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  1604. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  1605. int_status = exynos4_jpeg_get_int_status(jpeg->regs);
  1606. if (int_status) {
  1607. switch (int_status & 0x1f) {
  1608. case 0x1:
  1609. jpeg->irq_ret = ERR_PROT;
  1610. break;
  1611. case 0x2:
  1612. jpeg->irq_ret = OK_ENC_OR_DEC;
  1613. break;
  1614. case 0x4:
  1615. jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
  1616. break;
  1617. case 0x8:
  1618. jpeg->irq_ret = ERR_MULTI_SCAN;
  1619. break;
  1620. case 0x10:
  1621. jpeg->irq_ret = ERR_FRAME;
  1622. break;
  1623. default:
  1624. jpeg->irq_ret = ERR_UNKNOWN;
  1625. break;
  1626. }
  1627. } else {
  1628. jpeg->irq_ret = ERR_UNKNOWN;
  1629. }
  1630. if (jpeg->irq_ret == OK_ENC_OR_DEC) {
  1631. if (curr_ctx->mode == S5P_JPEG_ENCODE) {
  1632. payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
  1633. vb2_set_plane_payload(dst_vb, 0, payload_size);
  1634. }
  1635. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  1636. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  1637. } else {
  1638. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
  1639. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
  1640. }
  1641. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  1642. curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
  1643. spin_unlock(&jpeg->slock);
  1644. return IRQ_HANDLED;
  1645. }
  1646. static void *jpeg_get_drv_data(struct device *dev);
  1647. /*
  1648. * ============================================================================
  1649. * Driver basic infrastructure
  1650. * ============================================================================
  1651. */
  1652. static int s5p_jpeg_probe(struct platform_device *pdev)
  1653. {
  1654. struct s5p_jpeg *jpeg;
  1655. struct resource *res;
  1656. int ret;
  1657. /* JPEG IP abstraction struct */
  1658. jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
  1659. if (!jpeg)
  1660. return -ENOMEM;
  1661. jpeg->variant = jpeg_get_drv_data(&pdev->dev);
  1662. mutex_init(&jpeg->lock);
  1663. spin_lock_init(&jpeg->slock);
  1664. jpeg->dev = &pdev->dev;
  1665. /* memory-mapped registers */
  1666. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1667. jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
  1668. if (IS_ERR(jpeg->regs))
  1669. return PTR_ERR(jpeg->regs);
  1670. /* interrupt service routine registration */
  1671. jpeg->irq = ret = platform_get_irq(pdev, 0);
  1672. if (ret < 0) {
  1673. dev_err(&pdev->dev, "cannot find IRQ\n");
  1674. return ret;
  1675. }
  1676. ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
  1677. 0, dev_name(&pdev->dev), jpeg);
  1678. if (ret) {
  1679. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  1680. return ret;
  1681. }
  1682. /* clocks */
  1683. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  1684. if (IS_ERR(jpeg->clk)) {
  1685. dev_err(&pdev->dev, "cannot get clock\n");
  1686. ret = PTR_ERR(jpeg->clk);
  1687. return ret;
  1688. }
  1689. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  1690. /* v4l2 device */
  1691. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  1692. if (ret) {
  1693. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1694. goto clk_get_rollback;
  1695. }
  1696. /* mem2mem device */
  1697. jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
  1698. if (IS_ERR(jpeg->m2m_dev)) {
  1699. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  1700. ret = PTR_ERR(jpeg->m2m_dev);
  1701. goto device_register_rollback;
  1702. }
  1703. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1704. if (IS_ERR(jpeg->alloc_ctx)) {
  1705. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  1706. ret = PTR_ERR(jpeg->alloc_ctx);
  1707. goto m2m_init_rollback;
  1708. }
  1709. /* JPEG encoder /dev/videoX node */
  1710. jpeg->vfd_encoder = video_device_alloc();
  1711. if (!jpeg->vfd_encoder) {
  1712. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1713. ret = -ENOMEM;
  1714. goto vb2_allocator_rollback;
  1715. }
  1716. snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
  1717. "%s-enc", S5P_JPEG_M2M_NAME);
  1718. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  1719. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1720. jpeg->vfd_encoder->minor = -1;
  1721. jpeg->vfd_encoder->release = video_device_release;
  1722. jpeg->vfd_encoder->lock = &jpeg->lock;
  1723. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  1724. jpeg->vfd_encoder->vfl_dir = VFL_DIR_M2M;
  1725. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1726. if (ret) {
  1727. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1728. goto enc_vdev_alloc_rollback;
  1729. }
  1730. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  1731. v4l2_info(&jpeg->v4l2_dev,
  1732. "encoder device registered as /dev/video%d\n",
  1733. jpeg->vfd_encoder->num);
  1734. /* JPEG decoder /dev/videoX node */
  1735. jpeg->vfd_decoder = video_device_alloc();
  1736. if (!jpeg->vfd_decoder) {
  1737. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  1738. ret = -ENOMEM;
  1739. goto enc_vdev_register_rollback;
  1740. }
  1741. snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
  1742. "%s-dec", S5P_JPEG_M2M_NAME);
  1743. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  1744. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  1745. jpeg->vfd_decoder->minor = -1;
  1746. jpeg->vfd_decoder->release = video_device_release;
  1747. jpeg->vfd_decoder->lock = &jpeg->lock;
  1748. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  1749. jpeg->vfd_decoder->vfl_dir = VFL_DIR_M2M;
  1750. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1751. if (ret) {
  1752. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  1753. goto dec_vdev_alloc_rollback;
  1754. }
  1755. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  1756. v4l2_info(&jpeg->v4l2_dev,
  1757. "decoder device registered as /dev/video%d\n",
  1758. jpeg->vfd_decoder->num);
  1759. /* final statements & power management */
  1760. platform_set_drvdata(pdev, jpeg);
  1761. pm_runtime_enable(&pdev->dev);
  1762. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  1763. return 0;
  1764. dec_vdev_alloc_rollback:
  1765. video_device_release(jpeg->vfd_decoder);
  1766. enc_vdev_register_rollback:
  1767. video_unregister_device(jpeg->vfd_encoder);
  1768. enc_vdev_alloc_rollback:
  1769. video_device_release(jpeg->vfd_encoder);
  1770. vb2_allocator_rollback:
  1771. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1772. m2m_init_rollback:
  1773. v4l2_m2m_release(jpeg->m2m_dev);
  1774. device_register_rollback:
  1775. v4l2_device_unregister(&jpeg->v4l2_dev);
  1776. clk_get_rollback:
  1777. clk_put(jpeg->clk);
  1778. return ret;
  1779. }
  1780. static int s5p_jpeg_remove(struct platform_device *pdev)
  1781. {
  1782. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  1783. pm_runtime_disable(jpeg->dev);
  1784. video_unregister_device(jpeg->vfd_decoder);
  1785. video_device_release(jpeg->vfd_decoder);
  1786. video_unregister_device(jpeg->vfd_encoder);
  1787. video_device_release(jpeg->vfd_encoder);
  1788. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  1789. v4l2_m2m_release(jpeg->m2m_dev);
  1790. v4l2_device_unregister(&jpeg->v4l2_dev);
  1791. if (!pm_runtime_status_suspended(&pdev->dev))
  1792. clk_disable_unprepare(jpeg->clk);
  1793. clk_put(jpeg->clk);
  1794. return 0;
  1795. }
  1796. static int s5p_jpeg_runtime_suspend(struct device *dev)
  1797. {
  1798. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1799. clk_disable_unprepare(jpeg->clk);
  1800. return 0;
  1801. }
  1802. static int s5p_jpeg_runtime_resume(struct device *dev)
  1803. {
  1804. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  1805. unsigned long flags;
  1806. int ret;
  1807. ret = clk_prepare_enable(jpeg->clk);
  1808. if (ret < 0)
  1809. return ret;
  1810. spin_lock_irqsave(&jpeg->slock, flags);
  1811. /*
  1812. * JPEG IP allows storing two Huffman tables for each component
  1813. * We fill table 0 for each component and do this here only
  1814. * for S5PC210 device as Exynos4x12 requires programming its
  1815. * Huffman tables each time the encoding process is initialized.
  1816. */
  1817. if (jpeg->variant->version == SJPEG_S5P) {
  1818. s5p_jpeg_set_hdctbl(jpeg->regs);
  1819. s5p_jpeg_set_hdctblg(jpeg->regs);
  1820. s5p_jpeg_set_hactbl(jpeg->regs);
  1821. s5p_jpeg_set_hactblg(jpeg->regs);
  1822. }
  1823. spin_unlock_irqrestore(&jpeg->slock, flags);
  1824. return 0;
  1825. }
  1826. static int s5p_jpeg_suspend(struct device *dev)
  1827. {
  1828. if (pm_runtime_suspended(dev))
  1829. return 0;
  1830. return s5p_jpeg_runtime_suspend(dev);
  1831. }
  1832. static int s5p_jpeg_resume(struct device *dev)
  1833. {
  1834. if (pm_runtime_suspended(dev))
  1835. return 0;
  1836. return s5p_jpeg_runtime_resume(dev);
  1837. }
  1838. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  1839. SET_SYSTEM_SLEEP_PM_OPS(s5p_jpeg_suspend, s5p_jpeg_resume)
  1840. SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume, NULL)
  1841. };
  1842. static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
  1843. .version = SJPEG_S5P,
  1844. .jpeg_irq = s5p_jpeg_irq,
  1845. .m2m_ops = &s5p_jpeg_m2m_ops,
  1846. .fmt_ver_flag = SJPEG_FMT_FLAG_S5P,
  1847. };
  1848. static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
  1849. .version = SJPEG_EXYNOS4,
  1850. .jpeg_irq = exynos4_jpeg_irq,
  1851. .m2m_ops = &exynos4_jpeg_m2m_ops,
  1852. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
  1853. };
  1854. static const struct of_device_id samsung_jpeg_match[] = {
  1855. {
  1856. .compatible = "samsung,s5pv210-jpeg",
  1857. .data = &s5p_jpeg_drvdata,
  1858. }, {
  1859. .compatible = "samsung,exynos4210-jpeg",
  1860. .data = &s5p_jpeg_drvdata,
  1861. }, {
  1862. .compatible = "samsung,exynos4212-jpeg",
  1863. .data = &exynos4_jpeg_drvdata,
  1864. },
  1865. {},
  1866. };
  1867. MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
  1868. static void *jpeg_get_drv_data(struct device *dev)
  1869. {
  1870. struct s5p_jpeg_variant *driver_data = NULL;
  1871. const struct of_device_id *match;
  1872. if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
  1873. return &s5p_jpeg_drvdata;
  1874. match = of_match_node(samsung_jpeg_match, dev->of_node);
  1875. if (match)
  1876. driver_data = (struct s5p_jpeg_variant *)match->data;
  1877. return driver_data;
  1878. }
  1879. static struct platform_driver s5p_jpeg_driver = {
  1880. .probe = s5p_jpeg_probe,
  1881. .remove = s5p_jpeg_remove,
  1882. .driver = {
  1883. .of_match_table = of_match_ptr(samsung_jpeg_match),
  1884. .owner = THIS_MODULE,
  1885. .name = S5P_JPEG_M2M_NAME,
  1886. .pm = &s5p_jpeg_pm_ops,
  1887. },
  1888. };
  1889. module_platform_driver(s5p_jpeg_driver);
  1890. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  1891. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  1892. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  1893. MODULE_LICENSE("GPL");