jpeg-core.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122
  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-v4l2.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. case SJPEG_EXYNOS5420:
  603. if (ctx->subsampling > 3)
  604. return V4L2_JPEG_CHROMA_SUBSAMPLING_411;
  605. return exynos3250_decoded_subsampling[ctx->subsampling];
  606. case SJPEG_EXYNOS4:
  607. case SJPEG_EXYNOS5433:
  608. if (ctx->subsampling > 2)
  609. return V4L2_JPEG_CHROMA_SUBSAMPLING_420;
  610. return exynos4x12_decoded_subsampling[ctx->subsampling];
  611. default:
  612. return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  613. }
  614. }
  615. static inline void s5p_jpeg_set_qtbl(void __iomem *regs,
  616. const unsigned char *qtbl,
  617. unsigned long tab, int len)
  618. {
  619. int i;
  620. for (i = 0; i < len; i++)
  621. writel((unsigned int)qtbl[i], regs + tab + (i * 0x04));
  622. }
  623. static inline void s5p_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  624. {
  625. /* this driver fills quantisation table 0 with data for luma */
  626. s5p_jpeg_set_qtbl(regs, qtbl_luminance[quality],
  627. S5P_JPG_QTBL_CONTENT(0),
  628. ARRAY_SIZE(qtbl_luminance[quality]));
  629. }
  630. static inline void s5p_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  631. {
  632. /* this driver fills quantisation table 1 with data for chroma */
  633. s5p_jpeg_set_qtbl(regs, qtbl_chrominance[quality],
  634. S5P_JPG_QTBL_CONTENT(1),
  635. ARRAY_SIZE(qtbl_chrominance[quality]));
  636. }
  637. static inline void s5p_jpeg_set_htbl(void __iomem *regs,
  638. const unsigned char *htbl,
  639. unsigned long tab, int len)
  640. {
  641. int i;
  642. for (i = 0; i < len; i++)
  643. writel((unsigned int)htbl[i], regs + tab + (i * 0x04));
  644. }
  645. static inline void s5p_jpeg_set_hdctbl(void __iomem *regs)
  646. {
  647. /* this driver fills table 0 for this component */
  648. s5p_jpeg_set_htbl(regs, hdctbl0, S5P_JPG_HDCTBL(0),
  649. ARRAY_SIZE(hdctbl0));
  650. }
  651. static inline void s5p_jpeg_set_hdctblg(void __iomem *regs)
  652. {
  653. /* this driver fills table 0 for this component */
  654. s5p_jpeg_set_htbl(regs, hdctblg0, S5P_JPG_HDCTBLG(0),
  655. ARRAY_SIZE(hdctblg0));
  656. }
  657. static inline void s5p_jpeg_set_hactbl(void __iomem *regs)
  658. {
  659. /* this driver fills table 0 for this component */
  660. s5p_jpeg_set_htbl(regs, hactbl0, S5P_JPG_HACTBL(0),
  661. ARRAY_SIZE(hactbl0));
  662. }
  663. static inline void s5p_jpeg_set_hactblg(void __iomem *regs)
  664. {
  665. /* this driver fills table 0 for this component */
  666. s5p_jpeg_set_htbl(regs, hactblg0, S5P_JPG_HACTBLG(0),
  667. ARRAY_SIZE(hactblg0));
  668. }
  669. static inline void exynos4_jpeg_set_tbl(void __iomem *regs,
  670. const unsigned char *tbl,
  671. unsigned long tab, int len)
  672. {
  673. int i;
  674. unsigned int dword;
  675. for (i = 0; i < len; i += 4) {
  676. dword = tbl[i] |
  677. (tbl[i + 1] << 8) |
  678. (tbl[i + 2] << 16) |
  679. (tbl[i + 3] << 24);
  680. writel(dword, regs + tab + i);
  681. }
  682. }
  683. static inline void exynos4_jpeg_set_qtbl_lum(void __iomem *regs, int quality)
  684. {
  685. /* this driver fills quantisation table 0 with data for luma */
  686. exynos4_jpeg_set_tbl(regs, qtbl_luminance[quality],
  687. EXYNOS4_QTBL_CONTENT(0),
  688. ARRAY_SIZE(qtbl_luminance[quality]));
  689. }
  690. static inline void exynos4_jpeg_set_qtbl_chr(void __iomem *regs, int quality)
  691. {
  692. /* this driver fills quantisation table 1 with data for chroma */
  693. exynos4_jpeg_set_tbl(regs, qtbl_chrominance[quality],
  694. EXYNOS4_QTBL_CONTENT(1),
  695. ARRAY_SIZE(qtbl_chrominance[quality]));
  696. }
  697. static void exynos4_jpeg_set_huff_tbl(void __iomem *base)
  698. {
  699. exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCLL,
  700. ARRAY_SIZE(hdctbl0));
  701. exynos4_jpeg_set_tbl(base, hdctbl0, EXYNOS4_HUFF_TBL_HDCCL,
  702. ARRAY_SIZE(hdctbl0));
  703. exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCLV,
  704. ARRAY_SIZE(hdctblg0));
  705. exynos4_jpeg_set_tbl(base, hdctblg0, EXYNOS4_HUFF_TBL_HDCCV,
  706. ARRAY_SIZE(hdctblg0));
  707. exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACLL,
  708. ARRAY_SIZE(hactbl0));
  709. exynos4_jpeg_set_tbl(base, hactbl0, EXYNOS4_HUFF_TBL_HACCL,
  710. ARRAY_SIZE(hactbl0));
  711. exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACLV,
  712. ARRAY_SIZE(hactblg0));
  713. exynos4_jpeg_set_tbl(base, hactblg0, EXYNOS4_HUFF_TBL_HACCV,
  714. ARRAY_SIZE(hactblg0));
  715. }
  716. static inline int __exynos4_huff_tbl(int class, int id, bool lenval)
  717. {
  718. /*
  719. * class: 0 - DC, 1 - AC
  720. * id: 0 - Y, 1 - Cb/Cr
  721. */
  722. if (class) {
  723. if (id)
  724. return lenval ? EXYNOS4_HUFF_TBL_HACCL :
  725. EXYNOS4_HUFF_TBL_HACCV;
  726. return lenval ? EXYNOS4_HUFF_TBL_HACLL : EXYNOS4_HUFF_TBL_HACLV;
  727. }
  728. /* class == 0 */
  729. if (id)
  730. return lenval ? EXYNOS4_HUFF_TBL_HDCCL : EXYNOS4_HUFF_TBL_HDCCV;
  731. return lenval ? EXYNOS4_HUFF_TBL_HDCLL : EXYNOS4_HUFF_TBL_HDCLV;
  732. }
  733. static inline int exynos4_huff_tbl_len(int class, int id)
  734. {
  735. return __exynos4_huff_tbl(class, id, true);
  736. }
  737. static inline int exynos4_huff_tbl_val(int class, int id)
  738. {
  739. return __exynos4_huff_tbl(class, id, false);
  740. }
  741. static int get_byte(struct s5p_jpeg_buffer *buf);
  742. static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word);
  743. static void skip(struct s5p_jpeg_buffer *buf, long len);
  744. static void exynos4_jpeg_parse_decode_h_tbl(struct s5p_jpeg_ctx *ctx)
  745. {
  746. struct s5p_jpeg *jpeg = ctx->jpeg;
  747. struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  748. struct s5p_jpeg_buffer jpeg_buffer;
  749. unsigned int word;
  750. int c, x, components;
  751. jpeg_buffer.size = 2; /* Ls */
  752. jpeg_buffer.data =
  753. (unsigned long)vb2_plane_vaddr(vb, 0) + ctx->out_q.sos + 2;
  754. jpeg_buffer.curr = 0;
  755. word = 0;
  756. if (get_word_be(&jpeg_buffer, &word))
  757. return;
  758. jpeg_buffer.size = (long)word - 2;
  759. jpeg_buffer.data += 2;
  760. jpeg_buffer.curr = 0;
  761. components = get_byte(&jpeg_buffer);
  762. if (components == -1)
  763. return;
  764. while (components--) {
  765. c = get_byte(&jpeg_buffer);
  766. if (c == -1)
  767. return;
  768. x = get_byte(&jpeg_buffer);
  769. if (x == -1)
  770. return;
  771. exynos4_jpeg_select_dec_h_tbl(jpeg->regs, c,
  772. (((x >> 4) & 0x1) << 1) | (x & 0x1));
  773. }
  774. }
  775. static void exynos4_jpeg_parse_huff_tbl(struct s5p_jpeg_ctx *ctx)
  776. {
  777. struct s5p_jpeg *jpeg = ctx->jpeg;
  778. struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  779. struct s5p_jpeg_buffer jpeg_buffer;
  780. unsigned int word;
  781. int c, i, n, j;
  782. for (j = 0; j < ctx->out_q.dht.n; ++j) {
  783. jpeg_buffer.size = ctx->out_q.dht.len[j];
  784. jpeg_buffer.data = (unsigned long)vb2_plane_vaddr(vb, 0) +
  785. ctx->out_q.dht.marker[j];
  786. jpeg_buffer.curr = 0;
  787. word = 0;
  788. while (jpeg_buffer.curr < jpeg_buffer.size) {
  789. char id, class;
  790. c = get_byte(&jpeg_buffer);
  791. if (c == -1)
  792. return;
  793. id = c & 0xf;
  794. class = (c >> 4) & 0xf;
  795. n = 0;
  796. for (i = 0; i < 16; ++i) {
  797. c = get_byte(&jpeg_buffer);
  798. if (c == -1)
  799. return;
  800. word |= c << ((i % 4) * 8);
  801. if ((i + 1) % 4 == 0) {
  802. writel(word, jpeg->regs +
  803. exynos4_huff_tbl_len(class, id) +
  804. (i / 4) * 4);
  805. word = 0;
  806. }
  807. n += c;
  808. }
  809. word = 0;
  810. for (i = 0; i < n; ++i) {
  811. c = get_byte(&jpeg_buffer);
  812. if (c == -1)
  813. return;
  814. word |= c << ((i % 4) * 8);
  815. if ((i + 1) % 4 == 0) {
  816. writel(word, jpeg->regs +
  817. exynos4_huff_tbl_val(class, id) +
  818. (i / 4) * 4);
  819. word = 0;
  820. }
  821. }
  822. if (i % 4) {
  823. writel(word, jpeg->regs +
  824. exynos4_huff_tbl_val(class, id) + (i / 4) * 4);
  825. }
  826. word = 0;
  827. }
  828. }
  829. }
  830. static void exynos4_jpeg_parse_decode_q_tbl(struct s5p_jpeg_ctx *ctx)
  831. {
  832. struct s5p_jpeg *jpeg = ctx->jpeg;
  833. struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  834. struct s5p_jpeg_buffer jpeg_buffer;
  835. int c, x, components;
  836. jpeg_buffer.size = ctx->out_q.sof_len;
  837. jpeg_buffer.data =
  838. (unsigned long)vb2_plane_vaddr(vb, 0) + ctx->out_q.sof;
  839. jpeg_buffer.curr = 0;
  840. skip(&jpeg_buffer, 5); /* P, Y, X */
  841. components = get_byte(&jpeg_buffer);
  842. if (components == -1)
  843. return;
  844. exynos4_jpeg_set_dec_components(jpeg->regs, components);
  845. while (components--) {
  846. c = get_byte(&jpeg_buffer);
  847. if (c == -1)
  848. return;
  849. skip(&jpeg_buffer, 1);
  850. x = get_byte(&jpeg_buffer);
  851. if (x == -1)
  852. return;
  853. exynos4_jpeg_select_dec_q_tbl(jpeg->regs, c, x);
  854. }
  855. }
  856. static void exynos4_jpeg_parse_q_tbl(struct s5p_jpeg_ctx *ctx)
  857. {
  858. struct s5p_jpeg *jpeg = ctx->jpeg;
  859. struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  860. struct s5p_jpeg_buffer jpeg_buffer;
  861. unsigned int word;
  862. int c, i, j;
  863. for (j = 0; j < ctx->out_q.dqt.n; ++j) {
  864. jpeg_buffer.size = ctx->out_q.dqt.len[j];
  865. jpeg_buffer.data = (unsigned long)vb2_plane_vaddr(vb, 0) +
  866. ctx->out_q.dqt.marker[j];
  867. jpeg_buffer.curr = 0;
  868. word = 0;
  869. while (jpeg_buffer.size - jpeg_buffer.curr >= 65) {
  870. char id;
  871. c = get_byte(&jpeg_buffer);
  872. if (c == -1)
  873. return;
  874. id = c & 0xf;
  875. /* nonzero means extended mode - not supported */
  876. if ((c >> 4) & 0xf)
  877. return;
  878. for (i = 0; i < 64; ++i) {
  879. c = get_byte(&jpeg_buffer);
  880. if (c == -1)
  881. return;
  882. word |= c << ((i % 4) * 8);
  883. if ((i + 1) % 4 == 0) {
  884. writel(word, jpeg->regs +
  885. EXYNOS4_QTBL_CONTENT(id) + (i / 4) * 4);
  886. word = 0;
  887. }
  888. }
  889. word = 0;
  890. }
  891. }
  892. }
  893. /*
  894. * ============================================================================
  895. * Device file operations
  896. * ============================================================================
  897. */
  898. static int queue_init(void *priv, struct vb2_queue *src_vq,
  899. struct vb2_queue *dst_vq);
  900. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
  901. __u32 pixelformat, unsigned int fmt_type);
  902. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx);
  903. static int s5p_jpeg_open(struct file *file)
  904. {
  905. struct s5p_jpeg *jpeg = video_drvdata(file);
  906. struct video_device *vfd = video_devdata(file);
  907. struct s5p_jpeg_ctx *ctx;
  908. struct s5p_jpeg_fmt *out_fmt, *cap_fmt;
  909. int ret = 0;
  910. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  911. if (!ctx)
  912. return -ENOMEM;
  913. if (mutex_lock_interruptible(&jpeg->lock)) {
  914. ret = -ERESTARTSYS;
  915. goto free;
  916. }
  917. v4l2_fh_init(&ctx->fh, vfd);
  918. /* Use separate control handler per file handle */
  919. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  920. file->private_data = &ctx->fh;
  921. v4l2_fh_add(&ctx->fh);
  922. ctx->jpeg = jpeg;
  923. if (vfd == jpeg->vfd_encoder) {
  924. ctx->mode = S5P_JPEG_ENCODE;
  925. out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_RGB565,
  926. FMT_TYPE_OUTPUT);
  927. cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
  928. FMT_TYPE_CAPTURE);
  929. } else {
  930. ctx->mode = S5P_JPEG_DECODE;
  931. out_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
  932. FMT_TYPE_OUTPUT);
  933. cap_fmt = s5p_jpeg_find_format(ctx, V4L2_PIX_FMT_YUYV,
  934. FMT_TYPE_CAPTURE);
  935. ctx->scale_factor = EXYNOS3250_DEC_SCALE_FACTOR_8_8;
  936. }
  937. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
  938. if (IS_ERR(ctx->fh.m2m_ctx)) {
  939. ret = PTR_ERR(ctx->fh.m2m_ctx);
  940. goto error;
  941. }
  942. ctx->out_q.fmt = out_fmt;
  943. ctx->cap_q.fmt = cap_fmt;
  944. ret = s5p_jpeg_controls_create(ctx);
  945. if (ret < 0)
  946. goto error;
  947. mutex_unlock(&jpeg->lock);
  948. return 0;
  949. error:
  950. v4l2_fh_del(&ctx->fh);
  951. v4l2_fh_exit(&ctx->fh);
  952. mutex_unlock(&jpeg->lock);
  953. free:
  954. kfree(ctx);
  955. return ret;
  956. }
  957. static int s5p_jpeg_release(struct file *file)
  958. {
  959. struct s5p_jpeg *jpeg = video_drvdata(file);
  960. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  961. mutex_lock(&jpeg->lock);
  962. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  963. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  964. v4l2_fh_del(&ctx->fh);
  965. v4l2_fh_exit(&ctx->fh);
  966. kfree(ctx);
  967. mutex_unlock(&jpeg->lock);
  968. return 0;
  969. }
  970. static const struct v4l2_file_operations s5p_jpeg_fops = {
  971. .owner = THIS_MODULE,
  972. .open = s5p_jpeg_open,
  973. .release = s5p_jpeg_release,
  974. .poll = v4l2_m2m_fop_poll,
  975. .unlocked_ioctl = video_ioctl2,
  976. .mmap = v4l2_m2m_fop_mmap,
  977. };
  978. /*
  979. * ============================================================================
  980. * video ioctl operations
  981. * ============================================================================
  982. */
  983. static int get_byte(struct s5p_jpeg_buffer *buf)
  984. {
  985. if (buf->curr >= buf->size)
  986. return -1;
  987. return ((unsigned char *)buf->data)[buf->curr++];
  988. }
  989. static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
  990. {
  991. unsigned int temp;
  992. int byte;
  993. byte = get_byte(buf);
  994. if (byte == -1)
  995. return -1;
  996. temp = byte << 8;
  997. byte = get_byte(buf);
  998. if (byte == -1)
  999. return -1;
  1000. *word = (unsigned int)byte | temp;
  1001. return 0;
  1002. }
  1003. static void skip(struct s5p_jpeg_buffer *buf, long len)
  1004. {
  1005. if (len <= 0)
  1006. return;
  1007. while (len--)
  1008. get_byte(buf);
  1009. }
  1010. static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
  1011. unsigned long buffer, unsigned long size,
  1012. struct s5p_jpeg_ctx *ctx)
  1013. {
  1014. int c, components = 0, notfound, n_dht = 0, n_dqt = 0;
  1015. unsigned int height = 0, width = 0, word, subsampling = 0;
  1016. unsigned int sos = 0, sof = 0, sof_len = 0;
  1017. unsigned int dht[S5P_JPEG_MAX_MARKER], dht_len[S5P_JPEG_MAX_MARKER];
  1018. unsigned int dqt[S5P_JPEG_MAX_MARKER], dqt_len[S5P_JPEG_MAX_MARKER];
  1019. long length;
  1020. struct s5p_jpeg_buffer jpeg_buffer;
  1021. jpeg_buffer.size = size;
  1022. jpeg_buffer.data = buffer;
  1023. jpeg_buffer.curr = 0;
  1024. notfound = 1;
  1025. while (notfound || !sos) {
  1026. c = get_byte(&jpeg_buffer);
  1027. if (c == -1)
  1028. return false;
  1029. if (c != 0xff)
  1030. continue;
  1031. do
  1032. c = get_byte(&jpeg_buffer);
  1033. while (c == 0xff);
  1034. if (c == -1)
  1035. return false;
  1036. if (c == 0)
  1037. continue;
  1038. length = 0;
  1039. switch (c) {
  1040. /* SOF0: baseline JPEG */
  1041. case SOF0:
  1042. if (get_word_be(&jpeg_buffer, &word))
  1043. break;
  1044. length = (long)word - 2;
  1045. if (!length)
  1046. return false;
  1047. sof = jpeg_buffer.curr; /* after 0xffc0 */
  1048. sof_len = length;
  1049. if (get_byte(&jpeg_buffer) == -1)
  1050. break;
  1051. if (get_word_be(&jpeg_buffer, &height))
  1052. break;
  1053. if (get_word_be(&jpeg_buffer, &width))
  1054. break;
  1055. components = get_byte(&jpeg_buffer);
  1056. if (components == -1)
  1057. break;
  1058. if (components == 1) {
  1059. subsampling = 0x33;
  1060. } else {
  1061. skip(&jpeg_buffer, 1);
  1062. subsampling = get_byte(&jpeg_buffer);
  1063. skip(&jpeg_buffer, 1);
  1064. }
  1065. if (components > 3)
  1066. return false;
  1067. skip(&jpeg_buffer, components * 2);
  1068. notfound = 0;
  1069. break;
  1070. case DQT:
  1071. if (get_word_be(&jpeg_buffer, &word))
  1072. break;
  1073. length = (long)word - 2;
  1074. if (!length)
  1075. return false;
  1076. if (n_dqt >= S5P_JPEG_MAX_MARKER)
  1077. return false;
  1078. dqt[n_dqt] = jpeg_buffer.curr; /* after 0xffdb */
  1079. dqt_len[n_dqt++] = length;
  1080. skip(&jpeg_buffer, length);
  1081. break;
  1082. case DHT:
  1083. if (get_word_be(&jpeg_buffer, &word))
  1084. break;
  1085. length = (long)word - 2;
  1086. if (!length)
  1087. return false;
  1088. if (n_dht >= S5P_JPEG_MAX_MARKER)
  1089. return false;
  1090. dht[n_dht] = jpeg_buffer.curr; /* after 0xffc4 */
  1091. dht_len[n_dht++] = length;
  1092. skip(&jpeg_buffer, length);
  1093. break;
  1094. case SOS:
  1095. sos = jpeg_buffer.curr - 2; /* 0xffda */
  1096. break;
  1097. /* skip payload-less markers */
  1098. case RST ... RST + 7:
  1099. case SOI:
  1100. case EOI:
  1101. case TEM:
  1102. break;
  1103. /* skip uninteresting payload markers */
  1104. default:
  1105. if (get_word_be(&jpeg_buffer, &word))
  1106. break;
  1107. length = (long)word - 2;
  1108. skip(&jpeg_buffer, length);
  1109. break;
  1110. }
  1111. }
  1112. result->w = width;
  1113. result->h = height;
  1114. result->sos = sos;
  1115. result->dht.n = n_dht;
  1116. while (n_dht--) {
  1117. result->dht.marker[n_dht] = dht[n_dht];
  1118. result->dht.len[n_dht] = dht_len[n_dht];
  1119. }
  1120. result->dqt.n = n_dqt;
  1121. while (n_dqt--) {
  1122. result->dqt.marker[n_dqt] = dqt[n_dqt];
  1123. result->dqt.len[n_dqt] = dqt_len[n_dqt];
  1124. }
  1125. result->sof = sof;
  1126. result->sof_len = sof_len;
  1127. result->size = result->components = components;
  1128. switch (subsampling) {
  1129. case 0x11:
  1130. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444;
  1131. break;
  1132. case 0x21:
  1133. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422;
  1134. break;
  1135. case 0x22:
  1136. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420;
  1137. break;
  1138. case 0x33:
  1139. ctx->subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY;
  1140. break;
  1141. default:
  1142. return false;
  1143. }
  1144. return !notfound && sos;
  1145. }
  1146. static int s5p_jpeg_querycap(struct file *file, void *priv,
  1147. struct v4l2_capability *cap)
  1148. {
  1149. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1150. if (ctx->mode == S5P_JPEG_ENCODE) {
  1151. strlcpy(cap->driver, S5P_JPEG_M2M_NAME,
  1152. sizeof(cap->driver));
  1153. strlcpy(cap->card, S5P_JPEG_M2M_NAME " encoder",
  1154. sizeof(cap->card));
  1155. } else {
  1156. strlcpy(cap->driver, S5P_JPEG_M2M_NAME,
  1157. sizeof(cap->driver));
  1158. strlcpy(cap->card, S5P_JPEG_M2M_NAME " decoder",
  1159. sizeof(cap->card));
  1160. }
  1161. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  1162. dev_name(ctx->jpeg->dev));
  1163. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
  1164. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1165. return 0;
  1166. }
  1167. static int enum_fmt(struct s5p_jpeg_fmt *sjpeg_formats, int n,
  1168. struct v4l2_fmtdesc *f, u32 type)
  1169. {
  1170. int i, num = 0;
  1171. for (i = 0; i < n; ++i) {
  1172. if (sjpeg_formats[i].flags & type) {
  1173. /* index-th format of type type found ? */
  1174. if (num == f->index)
  1175. break;
  1176. /* Correct type but haven't reached our index yet,
  1177. * just increment per-type index
  1178. */
  1179. ++num;
  1180. }
  1181. }
  1182. /* Format not found */
  1183. if (i >= n)
  1184. return -EINVAL;
  1185. strlcpy(f->description, sjpeg_formats[i].name, sizeof(f->description));
  1186. f->pixelformat = sjpeg_formats[i].fourcc;
  1187. return 0;
  1188. }
  1189. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  1190. struct v4l2_fmtdesc *f)
  1191. {
  1192. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1193. if (ctx->mode == S5P_JPEG_ENCODE)
  1194. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  1195. SJPEG_FMT_FLAG_ENC_CAPTURE);
  1196. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  1197. SJPEG_FMT_FLAG_DEC_CAPTURE);
  1198. }
  1199. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  1200. struct v4l2_fmtdesc *f)
  1201. {
  1202. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1203. if (ctx->mode == S5P_JPEG_ENCODE)
  1204. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  1205. SJPEG_FMT_FLAG_ENC_OUTPUT);
  1206. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  1207. SJPEG_FMT_FLAG_DEC_OUTPUT);
  1208. }
  1209. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  1210. enum v4l2_buf_type type)
  1211. {
  1212. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1213. return &ctx->out_q;
  1214. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1215. return &ctx->cap_q;
  1216. return NULL;
  1217. }
  1218. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1219. {
  1220. struct vb2_queue *vq;
  1221. struct s5p_jpeg_q_data *q_data = NULL;
  1222. struct v4l2_pix_format *pix = &f->fmt.pix;
  1223. struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
  1224. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  1225. if (!vq)
  1226. return -EINVAL;
  1227. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1228. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  1229. return -EINVAL;
  1230. q_data = get_q_data(ct, f->type);
  1231. BUG_ON(q_data == NULL);
  1232. pix->width = q_data->w;
  1233. pix->height = q_data->h;
  1234. pix->field = V4L2_FIELD_NONE;
  1235. pix->pixelformat = q_data->fmt->fourcc;
  1236. pix->bytesperline = 0;
  1237. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  1238. u32 bpl = q_data->w;
  1239. if (q_data->fmt->colplanes == 1)
  1240. bpl = (bpl * q_data->fmt->depth) >> 3;
  1241. pix->bytesperline = bpl;
  1242. }
  1243. pix->sizeimage = q_data->size;
  1244. return 0;
  1245. }
  1246. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
  1247. u32 pixelformat, unsigned int fmt_type)
  1248. {
  1249. unsigned int k, fmt_flag;
  1250. if (ctx->mode == S5P_JPEG_ENCODE)
  1251. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  1252. SJPEG_FMT_FLAG_ENC_OUTPUT :
  1253. SJPEG_FMT_FLAG_ENC_CAPTURE;
  1254. else
  1255. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  1256. SJPEG_FMT_FLAG_DEC_OUTPUT :
  1257. SJPEG_FMT_FLAG_DEC_CAPTURE;
  1258. for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
  1259. struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
  1260. if (fmt->fourcc == pixelformat &&
  1261. fmt->flags & fmt_flag &&
  1262. fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
  1263. return fmt;
  1264. }
  1265. }
  1266. return NULL;
  1267. }
  1268. static void jpeg_bound_align_image(struct s5p_jpeg_ctx *ctx,
  1269. u32 *w, unsigned int wmin, unsigned int wmax,
  1270. unsigned int walign,
  1271. u32 *h, unsigned int hmin, unsigned int hmax,
  1272. unsigned int halign)
  1273. {
  1274. int width, height, w_step, h_step;
  1275. width = *w;
  1276. height = *h;
  1277. w_step = 1 << walign;
  1278. h_step = 1 << halign;
  1279. if (ctx->jpeg->variant->hw3250_compat) {
  1280. /*
  1281. * Rightmost and bottommost pixels are cropped by the
  1282. * Exynos3250/compatible JPEG IP for RGB formats, for the
  1283. * specific width and height values respectively. This
  1284. * assignment will result in v4l_bound_align_image returning
  1285. * dimensions reduced by 1 for the aforementioned cases.
  1286. */
  1287. if (w_step == 4 && ((width & 3) == 1)) {
  1288. wmax = width;
  1289. hmax = height;
  1290. }
  1291. }
  1292. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  1293. if (*w < width && (*w + w_step) < wmax)
  1294. *w += w_step;
  1295. if (*h < height && (*h + h_step) < hmax)
  1296. *h += h_step;
  1297. }
  1298. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  1299. struct s5p_jpeg_ctx *ctx, int q_type)
  1300. {
  1301. struct v4l2_pix_format *pix = &f->fmt.pix;
  1302. if (pix->field == V4L2_FIELD_ANY)
  1303. pix->field = V4L2_FIELD_NONE;
  1304. else if (pix->field != V4L2_FIELD_NONE)
  1305. return -EINVAL;
  1306. /* V4L2 specification suggests the driver corrects the format struct
  1307. * if any of the dimensions is unsupported
  1308. */
  1309. if (q_type == FMT_TYPE_OUTPUT)
  1310. jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
  1311. S5P_JPEG_MAX_WIDTH, 0,
  1312. &pix->height, S5P_JPEG_MIN_HEIGHT,
  1313. S5P_JPEG_MAX_HEIGHT, 0);
  1314. else
  1315. jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
  1316. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  1317. &pix->height, S5P_JPEG_MIN_HEIGHT,
  1318. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  1319. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  1320. if (pix->sizeimage <= 0)
  1321. pix->sizeimage = PAGE_SIZE;
  1322. pix->bytesperline = 0;
  1323. } else {
  1324. u32 bpl = pix->bytesperline;
  1325. if (fmt->colplanes > 1 && bpl < pix->width)
  1326. bpl = pix->width; /* planar */
  1327. if (fmt->colplanes == 1 && /* packed */
  1328. (bpl << 3) / fmt->depth < pix->width)
  1329. bpl = (pix->width * fmt->depth) >> 3;
  1330. pix->bytesperline = bpl;
  1331. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  1332. }
  1333. return 0;
  1334. }
  1335. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  1336. struct v4l2_format *f)
  1337. {
  1338. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1339. struct v4l2_pix_format *pix = &f->fmt.pix;
  1340. struct s5p_jpeg_fmt *fmt;
  1341. int ret;
  1342. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  1343. FMT_TYPE_CAPTURE);
  1344. if (!fmt) {
  1345. v4l2_err(&ctx->jpeg->v4l2_dev,
  1346. "Fourcc format (0x%08x) invalid.\n",
  1347. f->fmt.pix.pixelformat);
  1348. return -EINVAL;
  1349. }
  1350. if (!ctx->jpeg->variant->hw_ex4_compat || ctx->mode != S5P_JPEG_DECODE)
  1351. goto exit;
  1352. /*
  1353. * The exynos4x12 device requires resulting YUV image
  1354. * subsampling not to be lower than the input jpeg subsampling.
  1355. * If this requirement is not met then downgrade the requested
  1356. * capture format to the one with subsampling equal to the input jpeg.
  1357. */
  1358. if ((fmt->flags & SJPEG_FMT_NON_RGB) &&
  1359. (fmt->subsampling < ctx->subsampling)) {
  1360. ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
  1361. fmt->fourcc,
  1362. &pix->pixelformat,
  1363. ctx);
  1364. if (ret < 0)
  1365. pix->pixelformat = V4L2_PIX_FMT_GREY;
  1366. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  1367. FMT_TYPE_CAPTURE);
  1368. }
  1369. /*
  1370. * Decompression of a JPEG file with 4:2:0 subsampling and odd
  1371. * width to the YUV 4:2:0 compliant formats produces a raw image
  1372. * with broken luma component. Adjust capture format to RGB565
  1373. * in such a case.
  1374. */
  1375. if (ctx->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
  1376. (ctx->out_q.w & 1) &&
  1377. (pix->pixelformat == V4L2_PIX_FMT_NV12 ||
  1378. pix->pixelformat == V4L2_PIX_FMT_NV21 ||
  1379. pix->pixelformat == V4L2_PIX_FMT_YUV420)) {
  1380. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1381. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  1382. FMT_TYPE_CAPTURE);
  1383. }
  1384. exit:
  1385. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
  1386. }
  1387. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  1388. struct v4l2_format *f)
  1389. {
  1390. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1391. struct s5p_jpeg_fmt *fmt;
  1392. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  1393. FMT_TYPE_OUTPUT);
  1394. if (!fmt) {
  1395. v4l2_err(&ctx->jpeg->v4l2_dev,
  1396. "Fourcc format (0x%08x) invalid.\n",
  1397. f->fmt.pix.pixelformat);
  1398. return -EINVAL;
  1399. }
  1400. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
  1401. }
  1402. static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
  1403. struct v4l2_format *f,
  1404. int fmt_depth)
  1405. {
  1406. struct v4l2_pix_format *pix = &f->fmt.pix;
  1407. u32 pix_fmt = f->fmt.pix.pixelformat;
  1408. int w = pix->width, h = pix->height, wh_align;
  1409. int padding = 0;
  1410. if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
  1411. pix_fmt == V4L2_PIX_FMT_RGB565 ||
  1412. pix_fmt == V4L2_PIX_FMT_NV24 ||
  1413. pix_fmt == V4L2_PIX_FMT_NV42 ||
  1414. pix_fmt == V4L2_PIX_FMT_NV12 ||
  1415. pix_fmt == V4L2_PIX_FMT_NV21 ||
  1416. pix_fmt == V4L2_PIX_FMT_YUV420)
  1417. wh_align = 4;
  1418. else
  1419. wh_align = 1;
  1420. jpeg_bound_align_image(ctx, &w, S5P_JPEG_MIN_WIDTH,
  1421. S5P_JPEG_MAX_WIDTH, wh_align,
  1422. &h, S5P_JPEG_MIN_HEIGHT,
  1423. S5P_JPEG_MAX_HEIGHT, wh_align);
  1424. if (ctx->jpeg->variant->version == SJPEG_EXYNOS4)
  1425. padding = PAGE_SIZE;
  1426. return (w * h * fmt_depth >> 3) + padding;
  1427. }
  1428. static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
  1429. struct v4l2_rect *r);
  1430. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  1431. {
  1432. struct vb2_queue *vq;
  1433. struct s5p_jpeg_q_data *q_data = NULL;
  1434. struct v4l2_pix_format *pix = &f->fmt.pix;
  1435. struct v4l2_ctrl *ctrl_subs;
  1436. struct v4l2_rect scale_rect;
  1437. unsigned int f_type;
  1438. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  1439. if (!vq)
  1440. return -EINVAL;
  1441. q_data = get_q_data(ct, f->type);
  1442. BUG_ON(q_data == NULL);
  1443. if (vb2_is_busy(vq)) {
  1444. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  1445. return -EBUSY;
  1446. }
  1447. f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
  1448. FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
  1449. q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
  1450. q_data->w = pix->width;
  1451. q_data->h = pix->height;
  1452. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  1453. /*
  1454. * During encoding Exynos4x12 SoCs access wider memory area
  1455. * than it results from Image_x and Image_y values written to
  1456. * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
  1457. * page fault calculate proper buffer size in such a case.
  1458. */
  1459. if (ct->jpeg->variant->hw_ex4_compat &&
  1460. f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
  1461. q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
  1462. f,
  1463. q_data->fmt->depth);
  1464. else
  1465. q_data->size = q_data->w * q_data->h *
  1466. q_data->fmt->depth >> 3;
  1467. } else {
  1468. q_data->size = pix->sizeimage;
  1469. }
  1470. if (f_type == FMT_TYPE_OUTPUT) {
  1471. ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
  1472. V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
  1473. if (ctrl_subs)
  1474. v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
  1475. ct->crop_altered = false;
  1476. }
  1477. /*
  1478. * For decoding init crop_rect with capture buffer dimmensions which
  1479. * contain aligned dimensions of the input JPEG image and do it only
  1480. * if crop rectangle hasn't been altered by the user space e.g. with
  1481. * S_SELECTION ioctl. For encoding assign output buffer dimensions.
  1482. */
  1483. if (!ct->crop_altered &&
  1484. ((ct->mode == S5P_JPEG_DECODE && f_type == FMT_TYPE_CAPTURE) ||
  1485. (ct->mode == S5P_JPEG_ENCODE && f_type == FMT_TYPE_OUTPUT))) {
  1486. ct->crop_rect.width = pix->width;
  1487. ct->crop_rect.height = pix->height;
  1488. }
  1489. /*
  1490. * Prevent downscaling to YUV420 format by more than 2
  1491. * for Exynos3250/compatible SoC as it produces broken raw image
  1492. * in such cases.
  1493. */
  1494. if (ct->mode == S5P_JPEG_DECODE &&
  1495. f_type == FMT_TYPE_CAPTURE &&
  1496. ct->jpeg->variant->hw3250_compat &&
  1497. pix->pixelformat == V4L2_PIX_FMT_YUV420 &&
  1498. ct->scale_factor > 2) {
  1499. scale_rect.width = ct->out_q.w / 2;
  1500. scale_rect.height = ct->out_q.h / 2;
  1501. exynos3250_jpeg_try_downscale(ct, &scale_rect);
  1502. }
  1503. return 0;
  1504. }
  1505. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  1506. struct v4l2_format *f)
  1507. {
  1508. int ret;
  1509. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  1510. if (ret)
  1511. return ret;
  1512. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1513. }
  1514. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  1515. struct v4l2_format *f)
  1516. {
  1517. int ret;
  1518. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  1519. if (ret)
  1520. return ret;
  1521. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1522. }
  1523. static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
  1524. struct v4l2_rect *r)
  1525. {
  1526. int w_ratio, h_ratio, scale_factor, cur_ratio, i;
  1527. w_ratio = ctx->out_q.w / r->width;
  1528. h_ratio = ctx->out_q.h / r->height;
  1529. scale_factor = w_ratio > h_ratio ? w_ratio : h_ratio;
  1530. scale_factor = clamp_val(scale_factor, 1, 8);
  1531. /* Align scale ratio to the nearest power of 2 */
  1532. for (i = 0; i <= 3; ++i) {
  1533. cur_ratio = 1 << i;
  1534. if (scale_factor <= cur_ratio) {
  1535. ctx->scale_factor = cur_ratio;
  1536. break;
  1537. }
  1538. }
  1539. r->width = round_down(ctx->out_q.w / ctx->scale_factor, 2);
  1540. r->height = round_down(ctx->out_q.h / ctx->scale_factor, 2);
  1541. ctx->crop_rect.width = r->width;
  1542. ctx->crop_rect.height = r->height;
  1543. ctx->crop_rect.left = 0;
  1544. ctx->crop_rect.top = 0;
  1545. ctx->crop_altered = true;
  1546. return 0;
  1547. }
  1548. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  1549. static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  1550. {
  1551. if (a->left < b->left || a->top < b->top)
  1552. return 0;
  1553. if (a->left + a->width > b->left + b->width)
  1554. return 0;
  1555. if (a->top + a->height > b->top + b->height)
  1556. return 0;
  1557. return 1;
  1558. }
  1559. static int exynos3250_jpeg_try_crop(struct s5p_jpeg_ctx *ctx,
  1560. struct v4l2_rect *r)
  1561. {
  1562. struct v4l2_rect base_rect;
  1563. int w_step, h_step;
  1564. switch (ctx->cap_q.fmt->fourcc) {
  1565. case V4L2_PIX_FMT_NV12:
  1566. case V4L2_PIX_FMT_NV21:
  1567. w_step = 1;
  1568. h_step = 2;
  1569. break;
  1570. case V4L2_PIX_FMT_YUV420:
  1571. w_step = 2;
  1572. h_step = 2;
  1573. break;
  1574. default:
  1575. w_step = 1;
  1576. h_step = 1;
  1577. break;
  1578. }
  1579. base_rect.top = 0;
  1580. base_rect.left = 0;
  1581. base_rect.width = ctx->out_q.w;
  1582. base_rect.height = ctx->out_q.h;
  1583. r->width = round_down(r->width, w_step);
  1584. r->height = round_down(r->height, h_step);
  1585. r->left = round_down(r->left, 2);
  1586. r->top = round_down(r->top, 2);
  1587. if (!enclosed_rectangle(r, &base_rect))
  1588. return -EINVAL;
  1589. ctx->crop_rect.left = r->left;
  1590. ctx->crop_rect.top = r->top;
  1591. ctx->crop_rect.width = r->width;
  1592. ctx->crop_rect.height = r->height;
  1593. ctx->crop_altered = true;
  1594. return 0;
  1595. }
  1596. /*
  1597. * V4L2 controls
  1598. */
  1599. static int s5p_jpeg_g_selection(struct file *file, void *priv,
  1600. struct v4l2_selection *s)
  1601. {
  1602. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1603. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  1604. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1605. return -EINVAL;
  1606. /* For JPEG blob active == default == bounds */
  1607. switch (s->target) {
  1608. case V4L2_SEL_TGT_CROP:
  1609. case V4L2_SEL_TGT_CROP_BOUNDS:
  1610. case V4L2_SEL_TGT_CROP_DEFAULT:
  1611. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1612. s->r.width = ctx->out_q.w;
  1613. s->r.height = ctx->out_q.h;
  1614. s->r.left = 0;
  1615. s->r.top = 0;
  1616. break;
  1617. case V4L2_SEL_TGT_COMPOSE:
  1618. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1619. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1620. s->r.width = ctx->crop_rect.width;
  1621. s->r.height = ctx->crop_rect.height;
  1622. s->r.left = ctx->crop_rect.left;
  1623. s->r.top = ctx->crop_rect.top;
  1624. break;
  1625. default:
  1626. return -EINVAL;
  1627. }
  1628. return 0;
  1629. }
  1630. /*
  1631. * V4L2 controls
  1632. */
  1633. static int s5p_jpeg_s_selection(struct file *file, void *fh,
  1634. struct v4l2_selection *s)
  1635. {
  1636. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  1637. struct v4l2_rect *rect = &s->r;
  1638. int ret = -EINVAL;
  1639. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1640. return -EINVAL;
  1641. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  1642. if (ctx->mode != S5P_JPEG_DECODE)
  1643. return -EINVAL;
  1644. if (ctx->jpeg->variant->hw3250_compat)
  1645. ret = exynos3250_jpeg_try_downscale(ctx, rect);
  1646. } else if (s->target == V4L2_SEL_TGT_CROP) {
  1647. if (ctx->mode != S5P_JPEG_ENCODE)
  1648. return -EINVAL;
  1649. if (ctx->jpeg->variant->hw3250_compat)
  1650. ret = exynos3250_jpeg_try_crop(ctx, rect);
  1651. }
  1652. return ret;
  1653. }
  1654. static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1655. {
  1656. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1657. struct s5p_jpeg *jpeg = ctx->jpeg;
  1658. unsigned long flags;
  1659. switch (ctrl->id) {
  1660. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1661. spin_lock_irqsave(&jpeg->slock, flags);
  1662. ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
  1663. spin_unlock_irqrestore(&jpeg->slock, flags);
  1664. break;
  1665. }
  1666. return 0;
  1667. }
  1668. static int s5p_jpeg_adjust_subs_ctrl(struct s5p_jpeg_ctx *ctx, int *ctrl_val)
  1669. {
  1670. switch (ctx->jpeg->variant->version) {
  1671. case SJPEG_S5P:
  1672. return 0;
  1673. case SJPEG_EXYNOS3250:
  1674. case SJPEG_EXYNOS5420:
  1675. /*
  1676. * The exynos3250/compatible device can produce JPEG image only
  1677. * of 4:4:4 subsampling when given RGB32 source image.
  1678. */
  1679. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
  1680. *ctrl_val = 0;
  1681. break;
  1682. case SJPEG_EXYNOS4:
  1683. /*
  1684. * The exynos4x12 device requires input raw image fourcc
  1685. * to be V4L2_PIX_FMT_GREY if gray jpeg format
  1686. * is to be set.
  1687. */
  1688. if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
  1689. *ctrl_val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY)
  1690. return -EINVAL;
  1691. break;
  1692. }
  1693. /*
  1694. * The exynos4x12 and exynos3250/compatible devices require resulting
  1695. * jpeg subsampling not to be lower than the input raw image
  1696. * subsampling.
  1697. */
  1698. if (ctx->out_q.fmt->subsampling > *ctrl_val)
  1699. *ctrl_val = ctx->out_q.fmt->subsampling;
  1700. return 0;
  1701. }
  1702. static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
  1703. {
  1704. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1705. unsigned long flags;
  1706. int ret = 0;
  1707. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1708. if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING)
  1709. ret = s5p_jpeg_adjust_subs_ctrl(ctx, &ctrl->val);
  1710. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1711. return ret;
  1712. }
  1713. static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
  1714. {
  1715. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1716. unsigned long flags;
  1717. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1718. switch (ctrl->id) {
  1719. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1720. ctx->compr_quality = ctrl->val;
  1721. break;
  1722. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1723. ctx->restart_interval = ctrl->val;
  1724. break;
  1725. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1726. ctx->subsampling = ctrl->val;
  1727. break;
  1728. }
  1729. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1730. return 0;
  1731. }
  1732. static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
  1733. .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
  1734. .try_ctrl = s5p_jpeg_try_ctrl,
  1735. .s_ctrl = s5p_jpeg_s_ctrl,
  1736. };
  1737. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
  1738. {
  1739. unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
  1740. struct v4l2_ctrl *ctrl;
  1741. int ret;
  1742. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  1743. if (ctx->mode == S5P_JPEG_ENCODE) {
  1744. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1745. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  1746. 0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
  1747. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1748. V4L2_CID_JPEG_RESTART_INTERVAL,
  1749. 0, 3, 0xffff, 0);
  1750. if (ctx->jpeg->variant->version == SJPEG_S5P)
  1751. mask = ~0x06; /* 422, 420 */
  1752. }
  1753. ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1754. V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
  1755. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
  1756. V4L2_JPEG_CHROMA_SUBSAMPLING_422);
  1757. if (ctx->ctrl_handler.error) {
  1758. ret = ctx->ctrl_handler.error;
  1759. goto error_free;
  1760. }
  1761. if (ctx->mode == S5P_JPEG_DECODE)
  1762. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  1763. V4L2_CTRL_FLAG_READ_ONLY;
  1764. ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1765. if (ret < 0)
  1766. goto error_free;
  1767. return ret;
  1768. error_free:
  1769. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1770. return ret;
  1771. }
  1772. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  1773. .vidioc_querycap = s5p_jpeg_querycap,
  1774. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  1775. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  1776. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  1777. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  1778. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  1779. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  1780. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  1781. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  1782. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1783. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1784. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1785. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1786. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1787. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1788. .vidioc_g_selection = s5p_jpeg_g_selection,
  1789. .vidioc_s_selection = s5p_jpeg_s_selection,
  1790. };
  1791. /*
  1792. * ============================================================================
  1793. * mem2mem callbacks
  1794. * ============================================================================
  1795. */
  1796. static void s5p_jpeg_device_run(void *priv)
  1797. {
  1798. struct s5p_jpeg_ctx *ctx = priv;
  1799. struct s5p_jpeg *jpeg = ctx->jpeg;
  1800. struct vb2_buffer *src_buf, *dst_buf;
  1801. unsigned long src_addr, dst_addr, flags;
  1802. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1803. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1804. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1805. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  1806. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  1807. s5p_jpeg_reset(jpeg->regs);
  1808. s5p_jpeg_poweron(jpeg->regs);
  1809. s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
  1810. if (ctx->mode == S5P_JPEG_ENCODE) {
  1811. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  1812. s5p_jpeg_input_raw_mode(jpeg->regs,
  1813. S5P_JPEG_RAW_IN_565);
  1814. else
  1815. s5p_jpeg_input_raw_mode(jpeg->regs,
  1816. S5P_JPEG_RAW_IN_422);
  1817. s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  1818. s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
  1819. s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
  1820. s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
  1821. s5p_jpeg_imgadr(jpeg->regs, src_addr);
  1822. s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
  1823. /* ultimately comes from sizeimage from userspace */
  1824. s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  1825. /* JPEG RGB to YCbCr conversion matrix */
  1826. s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  1827. s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  1828. s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  1829. s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  1830. s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  1831. s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  1832. s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  1833. s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  1834. s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  1835. /*
  1836. * JPEG IP allows storing 4 quantization tables
  1837. * We fill table 0 for luma and table 1 for chroma
  1838. */
  1839. s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1840. s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1841. /* use table 0 for Y */
  1842. s5p_jpeg_qtbl(jpeg->regs, 1, 0);
  1843. /* use table 1 for Cb and Cr*/
  1844. s5p_jpeg_qtbl(jpeg->regs, 2, 1);
  1845. s5p_jpeg_qtbl(jpeg->regs, 3, 1);
  1846. /* Y, Cb, Cr use Huffman table 0 */
  1847. s5p_jpeg_htbl_ac(jpeg->regs, 1);
  1848. s5p_jpeg_htbl_dc(jpeg->regs, 1);
  1849. s5p_jpeg_htbl_ac(jpeg->regs, 2);
  1850. s5p_jpeg_htbl_dc(jpeg->regs, 2);
  1851. s5p_jpeg_htbl_ac(jpeg->regs, 3);
  1852. s5p_jpeg_htbl_dc(jpeg->regs, 3);
  1853. } else { /* S5P_JPEG_DECODE */
  1854. s5p_jpeg_rst_int_enable(jpeg->regs, true);
  1855. s5p_jpeg_data_num_int_enable(jpeg->regs, true);
  1856. s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  1857. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  1858. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  1859. else
  1860. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
  1861. s5p_jpeg_jpgadr(jpeg->regs, src_addr);
  1862. s5p_jpeg_imgadr(jpeg->regs, dst_addr);
  1863. }
  1864. s5p_jpeg_start(jpeg->regs);
  1865. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1866. }
  1867. static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
  1868. {
  1869. struct s5p_jpeg *jpeg = ctx->jpeg;
  1870. struct s5p_jpeg_fmt *fmt;
  1871. struct vb2_buffer *vb;
  1872. struct s5p_jpeg_addr jpeg_addr = {};
  1873. u32 pix_size, padding_bytes = 0;
  1874. jpeg_addr.cb = 0;
  1875. jpeg_addr.cr = 0;
  1876. pix_size = ctx->cap_q.w * ctx->cap_q.h;
  1877. if (ctx->mode == S5P_JPEG_ENCODE) {
  1878. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1879. fmt = ctx->out_q.fmt;
  1880. if (ctx->out_q.w % 2 && fmt->h_align > 0)
  1881. padding_bytes = ctx->out_q.h;
  1882. } else {
  1883. fmt = ctx->cap_q.fmt;
  1884. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1885. }
  1886. jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
  1887. if (fmt->colplanes == 2) {
  1888. jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
  1889. } else if (fmt->colplanes == 3) {
  1890. jpeg_addr.cb = jpeg_addr.y + pix_size;
  1891. if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
  1892. jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
  1893. else
  1894. jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
  1895. }
  1896. exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
  1897. }
  1898. static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
  1899. {
  1900. struct s5p_jpeg *jpeg = ctx->jpeg;
  1901. struct vb2_buffer *vb;
  1902. unsigned int jpeg_addr = 0;
  1903. if (ctx->mode == S5P_JPEG_ENCODE)
  1904. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1905. else
  1906. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1907. jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  1908. if (jpeg->variant->version == SJPEG_EXYNOS5433 &&
  1909. ctx->mode == S5P_JPEG_DECODE)
  1910. jpeg_addr += ctx->out_q.sos;
  1911. exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
  1912. }
  1913. static inline void exynos4_jpeg_set_img_fmt(void __iomem *base,
  1914. unsigned int img_fmt)
  1915. {
  1916. __exynos4_jpeg_set_img_fmt(base, img_fmt, SJPEG_EXYNOS4);
  1917. }
  1918. static inline void exynos5433_jpeg_set_img_fmt(void __iomem *base,
  1919. unsigned int img_fmt)
  1920. {
  1921. __exynos4_jpeg_set_img_fmt(base, img_fmt, SJPEG_EXYNOS5433);
  1922. }
  1923. static inline void exynos4_jpeg_set_enc_out_fmt(void __iomem *base,
  1924. unsigned int out_fmt)
  1925. {
  1926. __exynos4_jpeg_set_enc_out_fmt(base, out_fmt, SJPEG_EXYNOS4);
  1927. }
  1928. static inline void exynos5433_jpeg_set_enc_out_fmt(void __iomem *base,
  1929. unsigned int out_fmt)
  1930. {
  1931. __exynos4_jpeg_set_enc_out_fmt(base, out_fmt, SJPEG_EXYNOS5433);
  1932. }
  1933. static void exynos4_jpeg_device_run(void *priv)
  1934. {
  1935. struct s5p_jpeg_ctx *ctx = priv;
  1936. struct s5p_jpeg *jpeg = ctx->jpeg;
  1937. unsigned int bitstream_size;
  1938. unsigned long flags;
  1939. spin_lock_irqsave(&jpeg->slock, flags);
  1940. if (ctx->mode == S5P_JPEG_ENCODE) {
  1941. exynos4_jpeg_sw_reset(jpeg->regs);
  1942. exynos4_jpeg_set_interrupt(jpeg->regs, jpeg->variant->version);
  1943. exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
  1944. exynos4_jpeg_set_huff_tbl(jpeg->regs);
  1945. /*
  1946. * JPEG IP allows storing 4 quantization tables
  1947. * We fill table 0 for luma and table 1 for chroma
  1948. */
  1949. exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1950. exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1951. exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
  1952. ctx->compr_quality);
  1953. exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
  1954. ctx->cap_q.h);
  1955. if (ctx->jpeg->variant->version == SJPEG_EXYNOS4) {
  1956. exynos4_jpeg_set_enc_out_fmt(jpeg->regs,
  1957. ctx->subsampling);
  1958. exynos4_jpeg_set_img_fmt(jpeg->regs,
  1959. ctx->out_q.fmt->fourcc);
  1960. } else {
  1961. exynos5433_jpeg_set_enc_out_fmt(jpeg->regs,
  1962. ctx->subsampling);
  1963. exynos5433_jpeg_set_img_fmt(jpeg->regs,
  1964. ctx->out_q.fmt->fourcc);
  1965. }
  1966. exynos4_jpeg_set_img_addr(ctx);
  1967. exynos4_jpeg_set_jpeg_addr(ctx);
  1968. exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
  1969. ctx->out_q.fmt->fourcc);
  1970. } else {
  1971. exynos4_jpeg_sw_reset(jpeg->regs);
  1972. exynos4_jpeg_set_interrupt(jpeg->regs,
  1973. jpeg->variant->version);
  1974. exynos4_jpeg_set_img_addr(ctx);
  1975. exynos4_jpeg_set_jpeg_addr(ctx);
  1976. if (jpeg->variant->version == SJPEG_EXYNOS5433) {
  1977. exynos4_jpeg_parse_huff_tbl(ctx);
  1978. exynos4_jpeg_parse_decode_h_tbl(ctx);
  1979. exynos4_jpeg_parse_q_tbl(ctx);
  1980. exynos4_jpeg_parse_decode_q_tbl(ctx);
  1981. exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
  1982. exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
  1983. ctx->cap_q.h);
  1984. exynos5433_jpeg_set_enc_out_fmt(jpeg->regs,
  1985. ctx->subsampling);
  1986. exynos5433_jpeg_set_img_fmt(jpeg->regs,
  1987. ctx->cap_q.fmt->fourcc);
  1988. bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 16);
  1989. } else {
  1990. exynos4_jpeg_set_img_fmt(jpeg->regs,
  1991. ctx->cap_q.fmt->fourcc);
  1992. bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
  1993. }
  1994. exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
  1995. }
  1996. exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
  1997. spin_unlock_irqrestore(&jpeg->slock, flags);
  1998. }
  1999. static void exynos3250_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
  2000. {
  2001. struct s5p_jpeg *jpeg = ctx->jpeg;
  2002. struct s5p_jpeg_fmt *fmt;
  2003. struct vb2_buffer *vb;
  2004. struct s5p_jpeg_addr jpeg_addr = {};
  2005. u32 pix_size;
  2006. pix_size = ctx->cap_q.w * ctx->cap_q.h;
  2007. if (ctx->mode == S5P_JPEG_ENCODE) {
  2008. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2009. fmt = ctx->out_q.fmt;
  2010. } else {
  2011. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2012. fmt = ctx->cap_q.fmt;
  2013. }
  2014. jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
  2015. if (fmt->colplanes == 2) {
  2016. jpeg_addr.cb = jpeg_addr.y + pix_size;
  2017. } else if (fmt->colplanes == 3) {
  2018. jpeg_addr.cb = jpeg_addr.y + pix_size;
  2019. if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
  2020. jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
  2021. else
  2022. jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
  2023. }
  2024. exynos3250_jpeg_imgadr(jpeg->regs, &jpeg_addr);
  2025. }
  2026. static void exynos3250_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
  2027. {
  2028. struct s5p_jpeg *jpeg = ctx->jpeg;
  2029. struct vb2_buffer *vb;
  2030. unsigned int jpeg_addr = 0;
  2031. if (ctx->mode == S5P_JPEG_ENCODE)
  2032. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2033. else
  2034. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2035. jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  2036. exynos3250_jpeg_jpgadr(jpeg->regs, jpeg_addr);
  2037. }
  2038. static void exynos3250_jpeg_device_run(void *priv)
  2039. {
  2040. struct s5p_jpeg_ctx *ctx = priv;
  2041. struct s5p_jpeg *jpeg = ctx->jpeg;
  2042. unsigned long flags;
  2043. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  2044. exynos3250_jpeg_reset(jpeg->regs);
  2045. exynos3250_jpeg_set_dma_num(jpeg->regs);
  2046. exynos3250_jpeg_poweron(jpeg->regs);
  2047. exynos3250_jpeg_clk_set(jpeg->regs);
  2048. exynos3250_jpeg_proc_mode(jpeg->regs, ctx->mode);
  2049. if (ctx->mode == S5P_JPEG_ENCODE) {
  2050. exynos3250_jpeg_input_raw_fmt(jpeg->regs,
  2051. ctx->out_q.fmt->fourcc);
  2052. exynos3250_jpeg_dri(jpeg->regs, ctx->restart_interval);
  2053. /*
  2054. * JPEG IP allows storing 4 quantization tables
  2055. * We fill table 0 for luma and table 1 for chroma
  2056. */
  2057. s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  2058. s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  2059. /* use table 0 for Y */
  2060. exynos3250_jpeg_qtbl(jpeg->regs, 1, 0);
  2061. /* use table 1 for Cb and Cr*/
  2062. exynos3250_jpeg_qtbl(jpeg->regs, 2, 1);
  2063. exynos3250_jpeg_qtbl(jpeg->regs, 3, 1);
  2064. /*
  2065. * Some SoCs require setting Huffman tables before each run
  2066. */
  2067. if (jpeg->variant->htbl_reinit) {
  2068. s5p_jpeg_set_hdctbl(jpeg->regs);
  2069. s5p_jpeg_set_hdctblg(jpeg->regs);
  2070. s5p_jpeg_set_hactbl(jpeg->regs);
  2071. s5p_jpeg_set_hactblg(jpeg->regs);
  2072. }
  2073. /* Y, Cb, Cr use Huffman table 0 */
  2074. exynos3250_jpeg_htbl_ac(jpeg->regs, 1);
  2075. exynos3250_jpeg_htbl_dc(jpeg->regs, 1);
  2076. exynos3250_jpeg_htbl_ac(jpeg->regs, 2);
  2077. exynos3250_jpeg_htbl_dc(jpeg->regs, 2);
  2078. exynos3250_jpeg_htbl_ac(jpeg->regs, 3);
  2079. exynos3250_jpeg_htbl_dc(jpeg->regs, 3);
  2080. exynos3250_jpeg_set_x(jpeg->regs, ctx->crop_rect.width);
  2081. exynos3250_jpeg_set_y(jpeg->regs, ctx->crop_rect.height);
  2082. exynos3250_jpeg_stride(jpeg->regs, ctx->out_q.fmt->fourcc,
  2083. ctx->out_q.w);
  2084. exynos3250_jpeg_offset(jpeg->regs, ctx->crop_rect.left,
  2085. ctx->crop_rect.top);
  2086. exynos3250_jpeg_set_img_addr(ctx);
  2087. exynos3250_jpeg_set_jpeg_addr(ctx);
  2088. exynos3250_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  2089. /* ultimately comes from sizeimage from userspace */
  2090. exynos3250_jpeg_enc_stream_bound(jpeg->regs, ctx->cap_q.size);
  2091. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565 ||
  2092. ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565X ||
  2093. ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
  2094. exynos3250_jpeg_set_y16(jpeg->regs, true);
  2095. } else {
  2096. exynos3250_jpeg_set_img_addr(ctx);
  2097. exynos3250_jpeg_set_jpeg_addr(ctx);
  2098. exynos3250_jpeg_stride(jpeg->regs, ctx->cap_q.fmt->fourcc,
  2099. ctx->cap_q.w);
  2100. exynos3250_jpeg_offset(jpeg->regs, 0, 0);
  2101. exynos3250_jpeg_dec_scaling_ratio(jpeg->regs,
  2102. ctx->scale_factor);
  2103. exynos3250_jpeg_dec_stream_size(jpeg->regs, ctx->out_q.size);
  2104. exynos3250_jpeg_output_raw_fmt(jpeg->regs,
  2105. ctx->cap_q.fmt->fourcc);
  2106. }
  2107. exynos3250_jpeg_interrupts_enable(jpeg->regs);
  2108. /* JPEG RGB to YCbCr conversion matrix */
  2109. exynos3250_jpeg_coef(jpeg->regs, ctx->mode);
  2110. exynos3250_jpeg_set_timer(jpeg->regs, EXYNOS3250_IRQ_TIMEOUT);
  2111. jpeg->irq_status = 0;
  2112. exynos3250_jpeg_start(jpeg->regs);
  2113. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  2114. }
  2115. static int s5p_jpeg_job_ready(void *priv)
  2116. {
  2117. struct s5p_jpeg_ctx *ctx = priv;
  2118. if (ctx->mode == S5P_JPEG_DECODE)
  2119. return ctx->hdr_parsed;
  2120. return 1;
  2121. }
  2122. static void s5p_jpeg_job_abort(void *priv)
  2123. {
  2124. }
  2125. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  2126. .device_run = s5p_jpeg_device_run,
  2127. .job_ready = s5p_jpeg_job_ready,
  2128. .job_abort = s5p_jpeg_job_abort,
  2129. };
  2130. static struct v4l2_m2m_ops exynos3250_jpeg_m2m_ops = {
  2131. .device_run = exynos3250_jpeg_device_run,
  2132. .job_ready = s5p_jpeg_job_ready,
  2133. .job_abort = s5p_jpeg_job_abort,
  2134. };
  2135. static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
  2136. .device_run = exynos4_jpeg_device_run,
  2137. .job_ready = s5p_jpeg_job_ready,
  2138. .job_abort = s5p_jpeg_job_abort,
  2139. };
  2140. /*
  2141. * ============================================================================
  2142. * Queue operations
  2143. * ============================================================================
  2144. */
  2145. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  2146. unsigned int *nbuffers, unsigned int *nplanes,
  2147. unsigned int sizes[], struct device *alloc_devs[])
  2148. {
  2149. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  2150. struct s5p_jpeg_q_data *q_data = NULL;
  2151. unsigned int size, count = *nbuffers;
  2152. q_data = get_q_data(ctx, vq->type);
  2153. BUG_ON(q_data == NULL);
  2154. size = q_data->size;
  2155. /*
  2156. * header is parsed during decoding and parsed information stored
  2157. * in the context so we do not allow another buffer to overwrite it
  2158. */
  2159. if (ctx->mode == S5P_JPEG_DECODE)
  2160. count = 1;
  2161. *nbuffers = count;
  2162. *nplanes = 1;
  2163. sizes[0] = size;
  2164. return 0;
  2165. }
  2166. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  2167. {
  2168. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  2169. struct s5p_jpeg_q_data *q_data = NULL;
  2170. q_data = get_q_data(ctx, vb->vb2_queue->type);
  2171. BUG_ON(q_data == NULL);
  2172. if (vb2_plane_size(vb, 0) < q_data->size) {
  2173. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  2174. __func__, vb2_plane_size(vb, 0),
  2175. (long)q_data->size);
  2176. return -EINVAL;
  2177. }
  2178. vb2_set_plane_payload(vb, 0, q_data->size);
  2179. return 0;
  2180. }
  2181. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  2182. {
  2183. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  2184. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  2185. if (ctx->mode == S5P_JPEG_DECODE &&
  2186. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  2187. struct s5p_jpeg_q_data tmp, *q_data;
  2188. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  2189. (unsigned long)vb2_plane_vaddr(vb, 0),
  2190. min((unsigned long)ctx->out_q.size,
  2191. vb2_get_plane_payload(vb, 0)), ctx);
  2192. if (!ctx->hdr_parsed) {
  2193. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  2194. return;
  2195. }
  2196. q_data = &ctx->out_q;
  2197. q_data->w = tmp.w;
  2198. q_data->h = tmp.h;
  2199. q_data->sos = tmp.sos;
  2200. memcpy(q_data->dht.marker, tmp.dht.marker,
  2201. sizeof(tmp.dht.marker));
  2202. memcpy(q_data->dht.len, tmp.dht.len, sizeof(tmp.dht.len));
  2203. q_data->dht.n = tmp.dht.n;
  2204. memcpy(q_data->dqt.marker, tmp.dqt.marker,
  2205. sizeof(tmp.dqt.marker));
  2206. memcpy(q_data->dqt.len, tmp.dqt.len, sizeof(tmp.dqt.len));
  2207. q_data->dqt.n = tmp.dqt.n;
  2208. q_data->sof = tmp.sof;
  2209. q_data->sof_len = tmp.sof_len;
  2210. q_data = &ctx->cap_q;
  2211. q_data->w = tmp.w;
  2212. q_data->h = tmp.h;
  2213. }
  2214. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  2215. }
  2216. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  2217. {
  2218. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  2219. int ret;
  2220. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  2221. return ret > 0 ? 0 : ret;
  2222. }
  2223. static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
  2224. {
  2225. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  2226. pm_runtime_put(ctx->jpeg->dev);
  2227. }
  2228. static const struct vb2_ops s5p_jpeg_qops = {
  2229. .queue_setup = s5p_jpeg_queue_setup,
  2230. .buf_prepare = s5p_jpeg_buf_prepare,
  2231. .buf_queue = s5p_jpeg_buf_queue,
  2232. .wait_prepare = vb2_ops_wait_prepare,
  2233. .wait_finish = vb2_ops_wait_finish,
  2234. .start_streaming = s5p_jpeg_start_streaming,
  2235. .stop_streaming = s5p_jpeg_stop_streaming,
  2236. };
  2237. static int queue_init(void *priv, struct vb2_queue *src_vq,
  2238. struct vb2_queue *dst_vq)
  2239. {
  2240. struct s5p_jpeg_ctx *ctx = priv;
  2241. int ret;
  2242. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2243. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  2244. src_vq->drv_priv = ctx;
  2245. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  2246. src_vq->ops = &s5p_jpeg_qops;
  2247. src_vq->mem_ops = &vb2_dma_contig_memops;
  2248. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2249. src_vq->lock = &ctx->jpeg->lock;
  2250. src_vq->dev = ctx->jpeg->dev;
  2251. ret = vb2_queue_init(src_vq);
  2252. if (ret)
  2253. return ret;
  2254. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2255. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  2256. dst_vq->drv_priv = ctx;
  2257. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  2258. dst_vq->ops = &s5p_jpeg_qops;
  2259. dst_vq->mem_ops = &vb2_dma_contig_memops;
  2260. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2261. dst_vq->lock = &ctx->jpeg->lock;
  2262. dst_vq->dev = ctx->jpeg->dev;
  2263. return vb2_queue_init(dst_vq);
  2264. }
  2265. /*
  2266. * ============================================================================
  2267. * ISR
  2268. * ============================================================================
  2269. */
  2270. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  2271. {
  2272. struct s5p_jpeg *jpeg = dev_id;
  2273. struct s5p_jpeg_ctx *curr_ctx;
  2274. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  2275. unsigned long payload_size = 0;
  2276. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  2277. bool enc_jpeg_too_large = false;
  2278. bool timer_elapsed = false;
  2279. bool op_completed = false;
  2280. spin_lock(&jpeg->slock);
  2281. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  2282. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  2283. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  2284. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  2285. enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
  2286. timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
  2287. op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
  2288. if (curr_ctx->mode == S5P_JPEG_DECODE)
  2289. op_completed = op_completed &&
  2290. s5p_jpeg_stream_stat_ok(jpeg->regs);
  2291. if (enc_jpeg_too_large) {
  2292. state = VB2_BUF_STATE_ERROR;
  2293. s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
  2294. } else if (timer_elapsed) {
  2295. state = VB2_BUF_STATE_ERROR;
  2296. s5p_jpeg_clear_timer_stat(jpeg->regs);
  2297. } else if (!op_completed) {
  2298. state = VB2_BUF_STATE_ERROR;
  2299. } else {
  2300. payload_size = s5p_jpeg_compressed_size(jpeg->regs);
  2301. }
  2302. dst_buf->timecode = src_buf->timecode;
  2303. dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
  2304. dst_buf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  2305. dst_buf->flags |=
  2306. src_buf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  2307. v4l2_m2m_buf_done(src_buf, state);
  2308. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  2309. vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload_size);
  2310. v4l2_m2m_buf_done(dst_buf, state);
  2311. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2312. curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
  2313. spin_unlock(&jpeg->slock);
  2314. s5p_jpeg_clear_int(jpeg->regs);
  2315. return IRQ_HANDLED;
  2316. }
  2317. static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
  2318. {
  2319. unsigned int int_status;
  2320. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  2321. struct s5p_jpeg *jpeg = priv;
  2322. struct s5p_jpeg_ctx *curr_ctx;
  2323. unsigned long payload_size = 0;
  2324. spin_lock(&jpeg->slock);
  2325. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  2326. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  2327. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  2328. int_status = exynos4_jpeg_get_int_status(jpeg->regs);
  2329. if (int_status) {
  2330. switch (int_status & 0x1f) {
  2331. case 0x1:
  2332. jpeg->irq_ret = ERR_PROT;
  2333. break;
  2334. case 0x2:
  2335. jpeg->irq_ret = OK_ENC_OR_DEC;
  2336. break;
  2337. case 0x4:
  2338. jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
  2339. break;
  2340. case 0x8:
  2341. jpeg->irq_ret = ERR_MULTI_SCAN;
  2342. break;
  2343. case 0x10:
  2344. jpeg->irq_ret = ERR_FRAME;
  2345. break;
  2346. default:
  2347. jpeg->irq_ret = ERR_UNKNOWN;
  2348. break;
  2349. }
  2350. } else {
  2351. jpeg->irq_ret = ERR_UNKNOWN;
  2352. }
  2353. if (jpeg->irq_ret == OK_ENC_OR_DEC) {
  2354. if (curr_ctx->mode == S5P_JPEG_ENCODE) {
  2355. payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
  2356. vb2_set_plane_payload(&dst_vb->vb2_buf,
  2357. 0, payload_size);
  2358. }
  2359. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  2360. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  2361. } else {
  2362. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
  2363. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
  2364. }
  2365. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2366. if (jpeg->variant->version == SJPEG_EXYNOS4)
  2367. curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
  2368. spin_unlock(&jpeg->slock);
  2369. return IRQ_HANDLED;
  2370. }
  2371. static irqreturn_t exynos3250_jpeg_irq(int irq, void *dev_id)
  2372. {
  2373. struct s5p_jpeg *jpeg = dev_id;
  2374. struct s5p_jpeg_ctx *curr_ctx;
  2375. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  2376. unsigned long payload_size = 0;
  2377. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  2378. bool interrupt_timeout = false;
  2379. u32 irq_status;
  2380. spin_lock(&jpeg->slock);
  2381. irq_status = exynos3250_jpeg_get_timer_status(jpeg->regs);
  2382. if (irq_status & EXYNOS3250_TIMER_INT_STAT) {
  2383. exynos3250_jpeg_clear_timer_status(jpeg->regs);
  2384. interrupt_timeout = true;
  2385. dev_err(jpeg->dev, "Interrupt timeout occurred.\n");
  2386. }
  2387. irq_status = exynos3250_jpeg_get_int_status(jpeg->regs);
  2388. exynos3250_jpeg_clear_int_status(jpeg->regs, irq_status);
  2389. jpeg->irq_status |= irq_status;
  2390. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  2391. if (!curr_ctx)
  2392. goto exit_unlock;
  2393. if ((irq_status & EXYNOS3250_HEADER_STAT) &&
  2394. (curr_ctx->mode == S5P_JPEG_DECODE)) {
  2395. exynos3250_jpeg_rstart(jpeg->regs);
  2396. goto exit_unlock;
  2397. }
  2398. if (jpeg->irq_status & (EXYNOS3250_JPEG_DONE |
  2399. EXYNOS3250_WDMA_DONE |
  2400. EXYNOS3250_RDMA_DONE |
  2401. EXYNOS3250_RESULT_STAT))
  2402. payload_size = exynos3250_jpeg_compressed_size(jpeg->regs);
  2403. else if (interrupt_timeout)
  2404. state = VB2_BUF_STATE_ERROR;
  2405. else
  2406. goto exit_unlock;
  2407. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  2408. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  2409. dst_buf->timecode = src_buf->timecode;
  2410. dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
  2411. v4l2_m2m_buf_done(src_buf, state);
  2412. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  2413. vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload_size);
  2414. v4l2_m2m_buf_done(dst_buf, state);
  2415. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2416. curr_ctx->subsampling =
  2417. exynos3250_jpeg_get_subsampling_mode(jpeg->regs);
  2418. exit_unlock:
  2419. spin_unlock(&jpeg->slock);
  2420. return IRQ_HANDLED;
  2421. }
  2422. static void *jpeg_get_drv_data(struct device *dev);
  2423. /*
  2424. * ============================================================================
  2425. * Driver basic infrastructure
  2426. * ============================================================================
  2427. */
  2428. static int s5p_jpeg_probe(struct platform_device *pdev)
  2429. {
  2430. struct s5p_jpeg *jpeg;
  2431. struct resource *res;
  2432. int i, ret;
  2433. /* JPEG IP abstraction struct */
  2434. jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
  2435. if (!jpeg)
  2436. return -ENOMEM;
  2437. jpeg->variant = jpeg_get_drv_data(&pdev->dev);
  2438. mutex_init(&jpeg->lock);
  2439. spin_lock_init(&jpeg->slock);
  2440. jpeg->dev = &pdev->dev;
  2441. /* memory-mapped registers */
  2442. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2443. jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
  2444. if (IS_ERR(jpeg->regs))
  2445. return PTR_ERR(jpeg->regs);
  2446. /* interrupt service routine registration */
  2447. jpeg->irq = ret = platform_get_irq(pdev, 0);
  2448. if (ret < 0) {
  2449. dev_err(&pdev->dev, "cannot find IRQ\n");
  2450. return ret;
  2451. }
  2452. ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
  2453. 0, dev_name(&pdev->dev), jpeg);
  2454. if (ret) {
  2455. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  2456. return ret;
  2457. }
  2458. /* clocks */
  2459. for (i = 0; i < jpeg->variant->num_clocks; i++) {
  2460. jpeg->clocks[i] = devm_clk_get(&pdev->dev,
  2461. jpeg->variant->clk_names[i]);
  2462. if (IS_ERR(jpeg->clocks[i])) {
  2463. dev_err(&pdev->dev, "failed to get clock: %s\n",
  2464. jpeg->variant->clk_names[i]);
  2465. return PTR_ERR(jpeg->clocks[i]);
  2466. }
  2467. }
  2468. /* v4l2 device */
  2469. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  2470. if (ret) {
  2471. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  2472. return ret;
  2473. }
  2474. /* mem2mem device */
  2475. jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
  2476. if (IS_ERR(jpeg->m2m_dev)) {
  2477. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  2478. ret = PTR_ERR(jpeg->m2m_dev);
  2479. goto device_register_rollback;
  2480. }
  2481. vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
  2482. /* JPEG encoder /dev/videoX node */
  2483. jpeg->vfd_encoder = video_device_alloc();
  2484. if (!jpeg->vfd_encoder) {
  2485. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  2486. ret = -ENOMEM;
  2487. goto m2m_init_rollback;
  2488. }
  2489. snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
  2490. "%s-enc", S5P_JPEG_M2M_NAME);
  2491. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  2492. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  2493. jpeg->vfd_encoder->minor = -1;
  2494. jpeg->vfd_encoder->release = video_device_release;
  2495. jpeg->vfd_encoder->lock = &jpeg->lock;
  2496. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  2497. jpeg->vfd_encoder->vfl_dir = VFL_DIR_M2M;
  2498. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  2499. if (ret) {
  2500. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  2501. video_device_release(jpeg->vfd_encoder);
  2502. goto m2m_init_rollback;
  2503. }
  2504. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  2505. v4l2_info(&jpeg->v4l2_dev,
  2506. "encoder device registered as /dev/video%d\n",
  2507. jpeg->vfd_encoder->num);
  2508. /* JPEG decoder /dev/videoX node */
  2509. jpeg->vfd_decoder = video_device_alloc();
  2510. if (!jpeg->vfd_decoder) {
  2511. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  2512. ret = -ENOMEM;
  2513. goto enc_vdev_register_rollback;
  2514. }
  2515. snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
  2516. "%s-dec", S5P_JPEG_M2M_NAME);
  2517. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  2518. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  2519. jpeg->vfd_decoder->minor = -1;
  2520. jpeg->vfd_decoder->release = video_device_release;
  2521. jpeg->vfd_decoder->lock = &jpeg->lock;
  2522. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  2523. jpeg->vfd_decoder->vfl_dir = VFL_DIR_M2M;
  2524. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  2525. if (ret) {
  2526. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  2527. video_device_release(jpeg->vfd_decoder);
  2528. goto enc_vdev_register_rollback;
  2529. }
  2530. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  2531. v4l2_info(&jpeg->v4l2_dev,
  2532. "decoder device registered as /dev/video%d\n",
  2533. jpeg->vfd_decoder->num);
  2534. /* final statements & power management */
  2535. platform_set_drvdata(pdev, jpeg);
  2536. pm_runtime_enable(&pdev->dev);
  2537. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  2538. return 0;
  2539. enc_vdev_register_rollback:
  2540. video_unregister_device(jpeg->vfd_encoder);
  2541. m2m_init_rollback:
  2542. v4l2_m2m_release(jpeg->m2m_dev);
  2543. device_register_rollback:
  2544. v4l2_device_unregister(&jpeg->v4l2_dev);
  2545. return ret;
  2546. }
  2547. static int s5p_jpeg_remove(struct platform_device *pdev)
  2548. {
  2549. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  2550. int i;
  2551. pm_runtime_disable(jpeg->dev);
  2552. video_unregister_device(jpeg->vfd_decoder);
  2553. video_unregister_device(jpeg->vfd_encoder);
  2554. vb2_dma_contig_clear_max_seg_size(&pdev->dev);
  2555. v4l2_m2m_release(jpeg->m2m_dev);
  2556. v4l2_device_unregister(&jpeg->v4l2_dev);
  2557. if (!pm_runtime_status_suspended(&pdev->dev)) {
  2558. for (i = jpeg->variant->num_clocks - 1; i >= 0; i--)
  2559. clk_disable_unprepare(jpeg->clocks[i]);
  2560. }
  2561. return 0;
  2562. }
  2563. #ifdef CONFIG_PM
  2564. static int s5p_jpeg_runtime_suspend(struct device *dev)
  2565. {
  2566. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  2567. int i;
  2568. for (i = jpeg->variant->num_clocks - 1; i >= 0; i--)
  2569. clk_disable_unprepare(jpeg->clocks[i]);
  2570. return 0;
  2571. }
  2572. static int s5p_jpeg_runtime_resume(struct device *dev)
  2573. {
  2574. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  2575. unsigned long flags;
  2576. int i, ret;
  2577. for (i = 0; i < jpeg->variant->num_clocks; i++) {
  2578. ret = clk_prepare_enable(jpeg->clocks[i]);
  2579. if (ret) {
  2580. while (--i > 0)
  2581. clk_disable_unprepare(jpeg->clocks[i]);
  2582. return ret;
  2583. }
  2584. }
  2585. spin_lock_irqsave(&jpeg->slock, flags);
  2586. /*
  2587. * JPEG IP allows storing two Huffman tables for each component.
  2588. * We fill table 0 for each component and do this here only
  2589. * for S5PC210 and Exynos3250 SoCs. Exynos4x12 and Exynos542x SoC
  2590. * require programming their Huffman tables each time the encoding
  2591. * process is initialized, and thus it is accomplished in the
  2592. * device_run callback of m2m_ops.
  2593. */
  2594. if (!jpeg->variant->htbl_reinit) {
  2595. s5p_jpeg_set_hdctbl(jpeg->regs);
  2596. s5p_jpeg_set_hdctblg(jpeg->regs);
  2597. s5p_jpeg_set_hactbl(jpeg->regs);
  2598. s5p_jpeg_set_hactblg(jpeg->regs);
  2599. }
  2600. spin_unlock_irqrestore(&jpeg->slock, flags);
  2601. return 0;
  2602. }
  2603. #endif /* CONFIG_PM */
  2604. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  2605. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  2606. pm_runtime_force_resume)
  2607. SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume,
  2608. NULL)
  2609. };
  2610. static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
  2611. .version = SJPEG_S5P,
  2612. .jpeg_irq = s5p_jpeg_irq,
  2613. .m2m_ops = &s5p_jpeg_m2m_ops,
  2614. .fmt_ver_flag = SJPEG_FMT_FLAG_S5P,
  2615. .clk_names = {"jpeg"},
  2616. .num_clocks = 1,
  2617. };
  2618. static struct s5p_jpeg_variant exynos3250_jpeg_drvdata = {
  2619. .version = SJPEG_EXYNOS3250,
  2620. .jpeg_irq = exynos3250_jpeg_irq,
  2621. .m2m_ops = &exynos3250_jpeg_m2m_ops,
  2622. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS3250,
  2623. .hw3250_compat = 1,
  2624. .clk_names = {"jpeg", "sclk"},
  2625. .num_clocks = 2,
  2626. };
  2627. static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
  2628. .version = SJPEG_EXYNOS4,
  2629. .jpeg_irq = exynos4_jpeg_irq,
  2630. .m2m_ops = &exynos4_jpeg_m2m_ops,
  2631. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
  2632. .htbl_reinit = 1,
  2633. .clk_names = {"jpeg"},
  2634. .num_clocks = 1,
  2635. .hw_ex4_compat = 1,
  2636. };
  2637. static struct s5p_jpeg_variant exynos5420_jpeg_drvdata = {
  2638. .version = SJPEG_EXYNOS5420,
  2639. .jpeg_irq = exynos3250_jpeg_irq, /* intentionally 3250 */
  2640. .m2m_ops = &exynos3250_jpeg_m2m_ops, /* intentionally 3250 */
  2641. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS3250, /* intentionally 3250 */
  2642. .hw3250_compat = 1,
  2643. .htbl_reinit = 1,
  2644. .clk_names = {"jpeg"},
  2645. .num_clocks = 1,
  2646. };
  2647. static struct s5p_jpeg_variant exynos5433_jpeg_drvdata = {
  2648. .version = SJPEG_EXYNOS5433,
  2649. .jpeg_irq = exynos4_jpeg_irq,
  2650. .m2m_ops = &exynos4_jpeg_m2m_ops,
  2651. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
  2652. .htbl_reinit = 1,
  2653. .clk_names = {"pclk", "aclk", "aclk_xiu", "sclk"},
  2654. .num_clocks = 4,
  2655. .hw_ex4_compat = 1,
  2656. };
  2657. static const struct of_device_id samsung_jpeg_match[] = {
  2658. {
  2659. .compatible = "samsung,s5pv210-jpeg",
  2660. .data = &s5p_jpeg_drvdata,
  2661. }, {
  2662. .compatible = "samsung,exynos3250-jpeg",
  2663. .data = &exynos3250_jpeg_drvdata,
  2664. }, {
  2665. .compatible = "samsung,exynos4210-jpeg",
  2666. .data = &exynos4_jpeg_drvdata,
  2667. }, {
  2668. .compatible = "samsung,exynos4212-jpeg",
  2669. .data = &exynos4_jpeg_drvdata,
  2670. }, {
  2671. .compatible = "samsung,exynos5420-jpeg",
  2672. .data = &exynos5420_jpeg_drvdata,
  2673. }, {
  2674. .compatible = "samsung,exynos5433-jpeg",
  2675. .data = &exynos5433_jpeg_drvdata,
  2676. },
  2677. {},
  2678. };
  2679. MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
  2680. static void *jpeg_get_drv_data(struct device *dev)
  2681. {
  2682. struct s5p_jpeg_variant *driver_data = NULL;
  2683. const struct of_device_id *match;
  2684. if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
  2685. return &s5p_jpeg_drvdata;
  2686. match = of_match_node(samsung_jpeg_match, dev->of_node);
  2687. if (match)
  2688. driver_data = (struct s5p_jpeg_variant *)match->data;
  2689. return driver_data;
  2690. }
  2691. static struct platform_driver s5p_jpeg_driver = {
  2692. .probe = s5p_jpeg_probe,
  2693. .remove = s5p_jpeg_remove,
  2694. .driver = {
  2695. .of_match_table = of_match_ptr(samsung_jpeg_match),
  2696. .name = S5P_JPEG_M2M_NAME,
  2697. .pm = &s5p_jpeg_pm_ops,
  2698. },
  2699. };
  2700. module_platform_driver(s5p_jpeg_driver);
  2701. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  2702. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  2703. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  2704. MODULE_LICENSE("GPL");