jpeg-core.c 85 KB

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