jpeg-core.c 73 KB

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