jpeg-core.c 73 KB

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