jpeg-core.c 82 KB

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