jpeg-core.c 73 KB

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