jpeg-core.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  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. /*
  934. * This is only a mem-to-mem video device. The capture and output
  935. * device capability flags are left only for backward compatibility
  936. * and are scheduled for removal.
  937. */
  938. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
  939. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
  940. return 0;
  941. }
  942. static int enum_fmt(struct s5p_jpeg_fmt *sjpeg_formats, int n,
  943. struct v4l2_fmtdesc *f, u32 type)
  944. {
  945. int i, num = 0;
  946. for (i = 0; i < n; ++i) {
  947. if (sjpeg_formats[i].flags & type) {
  948. /* index-th format of type type found ? */
  949. if (num == f->index)
  950. break;
  951. /* Correct type but haven't reached our index yet,
  952. * just increment per-type index */
  953. ++num;
  954. }
  955. }
  956. /* Format not found */
  957. if (i >= n)
  958. return -EINVAL;
  959. strlcpy(f->description, sjpeg_formats[i].name, sizeof(f->description));
  960. f->pixelformat = sjpeg_formats[i].fourcc;
  961. return 0;
  962. }
  963. static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
  964. struct v4l2_fmtdesc *f)
  965. {
  966. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  967. if (ctx->mode == S5P_JPEG_ENCODE)
  968. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  969. SJPEG_FMT_FLAG_ENC_CAPTURE);
  970. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  971. SJPEG_FMT_FLAG_DEC_CAPTURE);
  972. }
  973. static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
  974. struct v4l2_fmtdesc *f)
  975. {
  976. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  977. if (ctx->mode == S5P_JPEG_ENCODE)
  978. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  979. SJPEG_FMT_FLAG_ENC_OUTPUT);
  980. return enum_fmt(sjpeg_formats, SJPEG_NUM_FORMATS, f,
  981. SJPEG_FMT_FLAG_DEC_OUTPUT);
  982. }
  983. static struct s5p_jpeg_q_data *get_q_data(struct s5p_jpeg_ctx *ctx,
  984. enum v4l2_buf_type type)
  985. {
  986. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  987. return &ctx->out_q;
  988. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  989. return &ctx->cap_q;
  990. return NULL;
  991. }
  992. static int s5p_jpeg_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  993. {
  994. struct vb2_queue *vq;
  995. struct s5p_jpeg_q_data *q_data = NULL;
  996. struct v4l2_pix_format *pix = &f->fmt.pix;
  997. struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
  998. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  999. if (!vq)
  1000. return -EINVAL;
  1001. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1002. ct->mode == S5P_JPEG_DECODE && !ct->hdr_parsed)
  1003. return -EINVAL;
  1004. q_data = get_q_data(ct, f->type);
  1005. BUG_ON(q_data == NULL);
  1006. pix->width = q_data->w;
  1007. pix->height = q_data->h;
  1008. pix->field = V4L2_FIELD_NONE;
  1009. pix->pixelformat = q_data->fmt->fourcc;
  1010. pix->bytesperline = 0;
  1011. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  1012. u32 bpl = q_data->w;
  1013. if (q_data->fmt->colplanes == 1)
  1014. bpl = (bpl * q_data->fmt->depth) >> 3;
  1015. pix->bytesperline = bpl;
  1016. }
  1017. pix->sizeimage = q_data->size;
  1018. return 0;
  1019. }
  1020. static struct s5p_jpeg_fmt *s5p_jpeg_find_format(struct s5p_jpeg_ctx *ctx,
  1021. u32 pixelformat, unsigned int fmt_type)
  1022. {
  1023. unsigned int k, fmt_flag;
  1024. if (ctx->mode == S5P_JPEG_ENCODE)
  1025. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  1026. SJPEG_FMT_FLAG_ENC_OUTPUT :
  1027. SJPEG_FMT_FLAG_ENC_CAPTURE;
  1028. else
  1029. fmt_flag = (fmt_type == FMT_TYPE_OUTPUT) ?
  1030. SJPEG_FMT_FLAG_DEC_OUTPUT :
  1031. SJPEG_FMT_FLAG_DEC_CAPTURE;
  1032. for (k = 0; k < ARRAY_SIZE(sjpeg_formats); k++) {
  1033. struct s5p_jpeg_fmt *fmt = &sjpeg_formats[k];
  1034. if (fmt->fourcc == pixelformat &&
  1035. fmt->flags & fmt_flag &&
  1036. fmt->flags & ctx->jpeg->variant->fmt_ver_flag) {
  1037. return fmt;
  1038. }
  1039. }
  1040. return NULL;
  1041. }
  1042. static void jpeg_bound_align_image(struct s5p_jpeg_ctx *ctx,
  1043. u32 *w, unsigned int wmin, unsigned int wmax,
  1044. unsigned int walign,
  1045. u32 *h, unsigned int hmin, unsigned int hmax,
  1046. unsigned int halign)
  1047. {
  1048. int width, height, w_step, h_step;
  1049. width = *w;
  1050. height = *h;
  1051. w_step = 1 << walign;
  1052. h_step = 1 << halign;
  1053. if (ctx->jpeg->variant->version == SJPEG_EXYNOS3250) {
  1054. /*
  1055. * Rightmost and bottommost pixels are cropped by the
  1056. * Exynos3250 JPEG IP for RGB formats, for the specific
  1057. * width and height values respectively. This assignment
  1058. * will result in v4l_bound_align_image returning dimensions
  1059. * reduced by 1 for the aforementioned cases.
  1060. */
  1061. if (w_step == 4 && ((width & 3) == 1)) {
  1062. wmax = width;
  1063. hmax = height;
  1064. }
  1065. }
  1066. v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
  1067. if (*w < width && (*w + w_step) < wmax)
  1068. *w += w_step;
  1069. if (*h < height && (*h + h_step) < hmax)
  1070. *h += h_step;
  1071. }
  1072. static int vidioc_try_fmt(struct v4l2_format *f, struct s5p_jpeg_fmt *fmt,
  1073. struct s5p_jpeg_ctx *ctx, int q_type)
  1074. {
  1075. struct v4l2_pix_format *pix = &f->fmt.pix;
  1076. if (pix->field == V4L2_FIELD_ANY)
  1077. pix->field = V4L2_FIELD_NONE;
  1078. else if (pix->field != V4L2_FIELD_NONE)
  1079. return -EINVAL;
  1080. /* V4L2 specification suggests the driver corrects the format struct
  1081. * if any of the dimensions is unsupported */
  1082. if (q_type == FMT_TYPE_OUTPUT)
  1083. jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
  1084. S5P_JPEG_MAX_WIDTH, 0,
  1085. &pix->height, S5P_JPEG_MIN_HEIGHT,
  1086. S5P_JPEG_MAX_HEIGHT, 0);
  1087. else
  1088. jpeg_bound_align_image(ctx, &pix->width, S5P_JPEG_MIN_WIDTH,
  1089. S5P_JPEG_MAX_WIDTH, fmt->h_align,
  1090. &pix->height, S5P_JPEG_MIN_HEIGHT,
  1091. S5P_JPEG_MAX_HEIGHT, fmt->v_align);
  1092. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  1093. if (pix->sizeimage <= 0)
  1094. pix->sizeimage = PAGE_SIZE;
  1095. pix->bytesperline = 0;
  1096. } else {
  1097. u32 bpl = pix->bytesperline;
  1098. if (fmt->colplanes > 1 && bpl < pix->width)
  1099. bpl = pix->width; /* planar */
  1100. if (fmt->colplanes == 1 && /* packed */
  1101. (bpl << 3) / fmt->depth < pix->width)
  1102. bpl = (pix->width * fmt->depth) >> 3;
  1103. pix->bytesperline = bpl;
  1104. pix->sizeimage = (pix->width * pix->height * fmt->depth) >> 3;
  1105. }
  1106. return 0;
  1107. }
  1108. static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
  1109. struct v4l2_format *f)
  1110. {
  1111. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1112. struct v4l2_pix_format *pix = &f->fmt.pix;
  1113. struct s5p_jpeg_fmt *fmt;
  1114. int ret;
  1115. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  1116. FMT_TYPE_CAPTURE);
  1117. if (!fmt) {
  1118. v4l2_err(&ctx->jpeg->v4l2_dev,
  1119. "Fourcc format (0x%08x) invalid.\n",
  1120. f->fmt.pix.pixelformat);
  1121. return -EINVAL;
  1122. }
  1123. if ((ctx->jpeg->variant->version != SJPEG_EXYNOS4) ||
  1124. (ctx->mode != S5P_JPEG_DECODE))
  1125. goto exit;
  1126. /*
  1127. * The exynos4x12 device requires resulting YUV image
  1128. * subsampling not to be lower than the input jpeg subsampling.
  1129. * If this requirement is not met then downgrade the requested
  1130. * capture format to the one with subsampling equal to the input jpeg.
  1131. */
  1132. if ((fmt->flags & SJPEG_FMT_NON_RGB) &&
  1133. (fmt->subsampling < ctx->subsampling)) {
  1134. ret = s5p_jpeg_adjust_fourcc_to_subsampling(ctx->subsampling,
  1135. fmt->fourcc,
  1136. &pix->pixelformat,
  1137. ctx);
  1138. if (ret < 0)
  1139. pix->pixelformat = V4L2_PIX_FMT_GREY;
  1140. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  1141. FMT_TYPE_CAPTURE);
  1142. }
  1143. /*
  1144. * Decompression of a JPEG file with 4:2:0 subsampling and odd
  1145. * width to the YUV 4:2:0 compliant formats produces a raw image
  1146. * with broken luma component. Adjust capture format to RGB565
  1147. * in such a case.
  1148. */
  1149. if (ctx->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
  1150. (ctx->out_q.w & 1) &&
  1151. (pix->pixelformat == V4L2_PIX_FMT_NV12 ||
  1152. pix->pixelformat == V4L2_PIX_FMT_NV21 ||
  1153. pix->pixelformat == V4L2_PIX_FMT_YUV420)) {
  1154. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1155. fmt = s5p_jpeg_find_format(ctx, pix->pixelformat,
  1156. FMT_TYPE_CAPTURE);
  1157. }
  1158. exit:
  1159. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_CAPTURE);
  1160. }
  1161. static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
  1162. struct v4l2_format *f)
  1163. {
  1164. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1165. struct s5p_jpeg_fmt *fmt;
  1166. fmt = s5p_jpeg_find_format(ctx, f->fmt.pix.pixelformat,
  1167. FMT_TYPE_OUTPUT);
  1168. if (!fmt) {
  1169. v4l2_err(&ctx->jpeg->v4l2_dev,
  1170. "Fourcc format (0x%08x) invalid.\n",
  1171. f->fmt.pix.pixelformat);
  1172. return -EINVAL;
  1173. }
  1174. return vidioc_try_fmt(f, fmt, ctx, FMT_TYPE_OUTPUT);
  1175. }
  1176. static int exynos4_jpeg_get_output_buffer_size(struct s5p_jpeg_ctx *ctx,
  1177. struct v4l2_format *f,
  1178. int fmt_depth)
  1179. {
  1180. struct v4l2_pix_format *pix = &f->fmt.pix;
  1181. u32 pix_fmt = f->fmt.pix.pixelformat;
  1182. int w = pix->width, h = pix->height, wh_align;
  1183. if (pix_fmt == V4L2_PIX_FMT_RGB32 ||
  1184. pix_fmt == V4L2_PIX_FMT_NV24 ||
  1185. pix_fmt == V4L2_PIX_FMT_NV42 ||
  1186. pix_fmt == V4L2_PIX_FMT_NV12 ||
  1187. pix_fmt == V4L2_PIX_FMT_NV21 ||
  1188. pix_fmt == V4L2_PIX_FMT_YUV420)
  1189. wh_align = 4;
  1190. else
  1191. wh_align = 1;
  1192. jpeg_bound_align_image(ctx, &w, S5P_JPEG_MIN_WIDTH,
  1193. S5P_JPEG_MAX_WIDTH, wh_align,
  1194. &h, S5P_JPEG_MIN_HEIGHT,
  1195. S5P_JPEG_MAX_HEIGHT, wh_align);
  1196. return w * h * fmt_depth >> 3;
  1197. }
  1198. static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
  1199. struct v4l2_rect *r);
  1200. static int s5p_jpeg_s_fmt(struct s5p_jpeg_ctx *ct, struct v4l2_format *f)
  1201. {
  1202. struct vb2_queue *vq;
  1203. struct s5p_jpeg_q_data *q_data = NULL;
  1204. struct v4l2_pix_format *pix = &f->fmt.pix;
  1205. struct v4l2_ctrl *ctrl_subs;
  1206. struct v4l2_rect scale_rect;
  1207. unsigned int f_type;
  1208. vq = v4l2_m2m_get_vq(ct->fh.m2m_ctx, f->type);
  1209. if (!vq)
  1210. return -EINVAL;
  1211. q_data = get_q_data(ct, f->type);
  1212. BUG_ON(q_data == NULL);
  1213. if (vb2_is_busy(vq)) {
  1214. v4l2_err(&ct->jpeg->v4l2_dev, "%s queue busy\n", __func__);
  1215. return -EBUSY;
  1216. }
  1217. f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
  1218. FMT_TYPE_OUTPUT : FMT_TYPE_CAPTURE;
  1219. q_data->fmt = s5p_jpeg_find_format(ct, pix->pixelformat, f_type);
  1220. q_data->w = pix->width;
  1221. q_data->h = pix->height;
  1222. if (q_data->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
  1223. /*
  1224. * During encoding Exynos4x12 SoCs access wider memory area
  1225. * than it results from Image_x and Image_y values written to
  1226. * the JPEG_IMAGE_SIZE register. In order to avoid sysmmu
  1227. * page fault calculate proper buffer size in such a case.
  1228. */
  1229. if (ct->jpeg->variant->version == SJPEG_EXYNOS4 &&
  1230. f_type == FMT_TYPE_OUTPUT && ct->mode == S5P_JPEG_ENCODE)
  1231. q_data->size = exynos4_jpeg_get_output_buffer_size(ct,
  1232. f,
  1233. q_data->fmt->depth);
  1234. else
  1235. q_data->size = q_data->w * q_data->h *
  1236. q_data->fmt->depth >> 3;
  1237. } else {
  1238. q_data->size = pix->sizeimage;
  1239. }
  1240. if (f_type == FMT_TYPE_OUTPUT) {
  1241. ctrl_subs = v4l2_ctrl_find(&ct->ctrl_handler,
  1242. V4L2_CID_JPEG_CHROMA_SUBSAMPLING);
  1243. if (ctrl_subs)
  1244. v4l2_ctrl_s_ctrl(ctrl_subs, q_data->fmt->subsampling);
  1245. ct->crop_altered = false;
  1246. }
  1247. /*
  1248. * For decoding init crop_rect with capture buffer dimmensions which
  1249. * contain aligned dimensions of the input JPEG image and do it only
  1250. * if crop rectangle hasn't been altered by the user space e.g. with
  1251. * S_SELECTION ioctl. For encoding assign output buffer dimensions.
  1252. */
  1253. if (!ct->crop_altered &&
  1254. ((ct->mode == S5P_JPEG_DECODE && f_type == FMT_TYPE_CAPTURE) ||
  1255. (ct->mode == S5P_JPEG_ENCODE && f_type == FMT_TYPE_OUTPUT))) {
  1256. ct->crop_rect.width = pix->width;
  1257. ct->crop_rect.height = pix->height;
  1258. }
  1259. /*
  1260. * Prevent downscaling to YUV420 format by more than 2
  1261. * for Exynos3250 SoC as it produces broken raw image
  1262. * in such cases.
  1263. */
  1264. if (ct->mode == S5P_JPEG_DECODE &&
  1265. f_type == FMT_TYPE_CAPTURE &&
  1266. ct->jpeg->variant->version == SJPEG_EXYNOS3250 &&
  1267. pix->pixelformat == V4L2_PIX_FMT_YUV420 &&
  1268. ct->scale_factor > 2) {
  1269. scale_rect.width = ct->out_q.w / 2;
  1270. scale_rect.height = ct->out_q.h / 2;
  1271. exynos3250_jpeg_try_downscale(ct, &scale_rect);
  1272. }
  1273. return 0;
  1274. }
  1275. static int s5p_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
  1276. struct v4l2_format *f)
  1277. {
  1278. int ret;
  1279. ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
  1280. if (ret)
  1281. return ret;
  1282. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1283. }
  1284. static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
  1285. struct v4l2_format *f)
  1286. {
  1287. int ret;
  1288. ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
  1289. if (ret)
  1290. return ret;
  1291. return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
  1292. }
  1293. static int exynos3250_jpeg_try_downscale(struct s5p_jpeg_ctx *ctx,
  1294. struct v4l2_rect *r)
  1295. {
  1296. int w_ratio, h_ratio, scale_factor, cur_ratio, i;
  1297. w_ratio = ctx->out_q.w / r->width;
  1298. h_ratio = ctx->out_q.h / r->height;
  1299. scale_factor = w_ratio > h_ratio ? w_ratio : h_ratio;
  1300. scale_factor = clamp_val(scale_factor, 1, 8);
  1301. /* Align scale ratio to the nearest power of 2 */
  1302. for (i = 0; i <= 3; ++i) {
  1303. cur_ratio = 1 << i;
  1304. if (scale_factor <= cur_ratio) {
  1305. ctx->scale_factor = cur_ratio;
  1306. break;
  1307. }
  1308. }
  1309. r->width = round_down(ctx->out_q.w / ctx->scale_factor, 2);
  1310. r->height = round_down(ctx->out_q.h / ctx->scale_factor, 2);
  1311. ctx->crop_rect.width = r->width;
  1312. ctx->crop_rect.height = r->height;
  1313. ctx->crop_rect.left = 0;
  1314. ctx->crop_rect.top = 0;
  1315. ctx->crop_altered = true;
  1316. return 0;
  1317. }
  1318. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  1319. static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  1320. {
  1321. if (a->left < b->left || a->top < b->top)
  1322. return 0;
  1323. if (a->left + a->width > b->left + b->width)
  1324. return 0;
  1325. if (a->top + a->height > b->top + b->height)
  1326. return 0;
  1327. return 1;
  1328. }
  1329. static int exynos3250_jpeg_try_crop(struct s5p_jpeg_ctx *ctx,
  1330. struct v4l2_rect *r)
  1331. {
  1332. struct v4l2_rect base_rect;
  1333. int w_step, h_step;
  1334. switch (ctx->cap_q.fmt->fourcc) {
  1335. case V4L2_PIX_FMT_NV12:
  1336. case V4L2_PIX_FMT_NV21:
  1337. w_step = 1;
  1338. h_step = 2;
  1339. break;
  1340. case V4L2_PIX_FMT_YUV420:
  1341. w_step = 2;
  1342. h_step = 2;
  1343. break;
  1344. default:
  1345. w_step = 1;
  1346. h_step = 1;
  1347. break;
  1348. }
  1349. base_rect.top = 0;
  1350. base_rect.left = 0;
  1351. base_rect.width = ctx->out_q.w;
  1352. base_rect.height = ctx->out_q.h;
  1353. r->width = round_down(r->width, w_step);
  1354. r->height = round_down(r->height, h_step);
  1355. r->left = round_down(r->left, 2);
  1356. r->top = round_down(r->top, 2);
  1357. if (!enclosed_rectangle(r, &base_rect))
  1358. return -EINVAL;
  1359. ctx->crop_rect.left = r->left;
  1360. ctx->crop_rect.top = r->top;
  1361. ctx->crop_rect.width = r->width;
  1362. ctx->crop_rect.height = r->height;
  1363. ctx->crop_altered = true;
  1364. return 0;
  1365. }
  1366. /*
  1367. * V4L2 controls
  1368. */
  1369. static int s5p_jpeg_g_selection(struct file *file, void *priv,
  1370. struct v4l2_selection *s)
  1371. {
  1372. struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
  1373. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  1374. s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1375. return -EINVAL;
  1376. /* For JPEG blob active == default == bounds */
  1377. switch (s->target) {
  1378. case V4L2_SEL_TGT_CROP:
  1379. case V4L2_SEL_TGT_CROP_BOUNDS:
  1380. case V4L2_SEL_TGT_CROP_DEFAULT:
  1381. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1382. s->r.width = ctx->out_q.w;
  1383. s->r.height = ctx->out_q.h;
  1384. s->r.left = 0;
  1385. s->r.top = 0;
  1386. break;
  1387. case V4L2_SEL_TGT_COMPOSE:
  1388. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1389. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1390. s->r.width = ctx->crop_rect.width;
  1391. s->r.height = ctx->crop_rect.height;
  1392. s->r.left = ctx->crop_rect.left;
  1393. s->r.top = ctx->crop_rect.top;
  1394. break;
  1395. default:
  1396. return -EINVAL;
  1397. }
  1398. return 0;
  1399. }
  1400. /*
  1401. * V4L2 controls
  1402. */
  1403. static int s5p_jpeg_s_selection(struct file *file, void *fh,
  1404. struct v4l2_selection *s)
  1405. {
  1406. struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
  1407. struct v4l2_rect *rect = &s->r;
  1408. int ret = -EINVAL;
  1409. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1410. return -EINVAL;
  1411. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  1412. if (ctx->mode != S5P_JPEG_DECODE)
  1413. return -EINVAL;
  1414. if (ctx->jpeg->variant->version == SJPEG_EXYNOS3250)
  1415. ret = exynos3250_jpeg_try_downscale(ctx, rect);
  1416. } else if (s->target == V4L2_SEL_TGT_CROP) {
  1417. if (ctx->mode != S5P_JPEG_ENCODE)
  1418. return -EINVAL;
  1419. if (ctx->jpeg->variant->version == SJPEG_EXYNOS3250)
  1420. ret = exynos3250_jpeg_try_crop(ctx, rect);
  1421. }
  1422. return ret;
  1423. }
  1424. static int s5p_jpeg_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1425. {
  1426. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1427. struct s5p_jpeg *jpeg = ctx->jpeg;
  1428. unsigned long flags;
  1429. switch (ctrl->id) {
  1430. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1431. spin_lock_irqsave(&jpeg->slock, flags);
  1432. ctrl->val = s5p_jpeg_to_user_subsampling(ctx);
  1433. spin_unlock_irqrestore(&jpeg->slock, flags);
  1434. break;
  1435. }
  1436. return 0;
  1437. }
  1438. static int s5p_jpeg_adjust_subs_ctrl(struct s5p_jpeg_ctx *ctx, int *ctrl_val)
  1439. {
  1440. switch (ctx->jpeg->variant->version) {
  1441. case SJPEG_S5P:
  1442. return 0;
  1443. case SJPEG_EXYNOS3250:
  1444. /*
  1445. * The exynos3250 device can produce JPEG image only
  1446. * of 4:4:4 subsampling when given RGB32 source image.
  1447. */
  1448. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
  1449. *ctrl_val = 0;
  1450. break;
  1451. case SJPEG_EXYNOS4:
  1452. /*
  1453. * The exynos4x12 device requires input raw image fourcc
  1454. * to be V4L2_PIX_FMT_GREY if gray jpeg format
  1455. * is to be set.
  1456. */
  1457. if (ctx->out_q.fmt->fourcc != V4L2_PIX_FMT_GREY &&
  1458. *ctrl_val == V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY)
  1459. return -EINVAL;
  1460. break;
  1461. }
  1462. /*
  1463. * The exynos4x12 and exynos3250 devices require resulting
  1464. * jpeg subsampling not to be lower than the input raw image
  1465. * subsampling.
  1466. */
  1467. if (ctx->out_q.fmt->subsampling > *ctrl_val)
  1468. *ctrl_val = ctx->out_q.fmt->subsampling;
  1469. return 0;
  1470. }
  1471. static int s5p_jpeg_try_ctrl(struct v4l2_ctrl *ctrl)
  1472. {
  1473. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1474. unsigned long flags;
  1475. int ret = 0;
  1476. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1477. if (ctrl->id == V4L2_CID_JPEG_CHROMA_SUBSAMPLING)
  1478. ret = s5p_jpeg_adjust_subs_ctrl(ctx, &ctrl->val);
  1479. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1480. return ret;
  1481. }
  1482. static int s5p_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
  1483. {
  1484. struct s5p_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
  1485. unsigned long flags;
  1486. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1487. switch (ctrl->id) {
  1488. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1489. ctx->compr_quality = ctrl->val;
  1490. break;
  1491. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1492. ctx->restart_interval = ctrl->val;
  1493. break;
  1494. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1495. ctx->subsampling = ctrl->val;
  1496. break;
  1497. }
  1498. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1499. return 0;
  1500. }
  1501. static const struct v4l2_ctrl_ops s5p_jpeg_ctrl_ops = {
  1502. .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
  1503. .try_ctrl = s5p_jpeg_try_ctrl,
  1504. .s_ctrl = s5p_jpeg_s_ctrl,
  1505. };
  1506. static int s5p_jpeg_controls_create(struct s5p_jpeg_ctx *ctx)
  1507. {
  1508. unsigned int mask = ~0x27; /* 444, 422, 420, GRAY */
  1509. struct v4l2_ctrl *ctrl;
  1510. int ret;
  1511. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  1512. if (ctx->mode == S5P_JPEG_ENCODE) {
  1513. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1514. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  1515. 0, 3, 1, S5P_JPEG_COMPR_QUAL_WORST);
  1516. v4l2_ctrl_new_std(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1517. V4L2_CID_JPEG_RESTART_INTERVAL,
  1518. 0, 3, 0xffff, 0);
  1519. if (ctx->jpeg->variant->version == SJPEG_S5P)
  1520. mask = ~0x06; /* 422, 420 */
  1521. }
  1522. ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &s5p_jpeg_ctrl_ops,
  1523. V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
  1524. V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, mask,
  1525. V4L2_JPEG_CHROMA_SUBSAMPLING_422);
  1526. if (ctx->ctrl_handler.error) {
  1527. ret = ctx->ctrl_handler.error;
  1528. goto error_free;
  1529. }
  1530. if (ctx->mode == S5P_JPEG_DECODE)
  1531. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  1532. V4L2_CTRL_FLAG_READ_ONLY;
  1533. ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  1534. if (ret < 0)
  1535. goto error_free;
  1536. return ret;
  1537. error_free:
  1538. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1539. return ret;
  1540. }
  1541. static const struct v4l2_ioctl_ops s5p_jpeg_ioctl_ops = {
  1542. .vidioc_querycap = s5p_jpeg_querycap,
  1543. .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
  1544. .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
  1545. .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
  1546. .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
  1547. .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
  1548. .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
  1549. .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
  1550. .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
  1551. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1552. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1553. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1554. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1555. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1556. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1557. .vidioc_g_selection = s5p_jpeg_g_selection,
  1558. .vidioc_s_selection = s5p_jpeg_s_selection,
  1559. };
  1560. /*
  1561. * ============================================================================
  1562. * mem2mem callbacks
  1563. * ============================================================================
  1564. */
  1565. static void s5p_jpeg_device_run(void *priv)
  1566. {
  1567. struct s5p_jpeg_ctx *ctx = priv;
  1568. struct s5p_jpeg *jpeg = ctx->jpeg;
  1569. struct vb2_buffer *src_buf, *dst_buf;
  1570. unsigned long src_addr, dst_addr, flags;
  1571. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1572. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1573. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1574. src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  1575. dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  1576. s5p_jpeg_reset(jpeg->regs);
  1577. s5p_jpeg_poweron(jpeg->regs);
  1578. s5p_jpeg_proc_mode(jpeg->regs, ctx->mode);
  1579. if (ctx->mode == S5P_JPEG_ENCODE) {
  1580. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565)
  1581. s5p_jpeg_input_raw_mode(jpeg->regs,
  1582. S5P_JPEG_RAW_IN_565);
  1583. else
  1584. s5p_jpeg_input_raw_mode(jpeg->regs,
  1585. S5P_JPEG_RAW_IN_422);
  1586. s5p_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  1587. s5p_jpeg_dri(jpeg->regs, ctx->restart_interval);
  1588. s5p_jpeg_x(jpeg->regs, ctx->out_q.w);
  1589. s5p_jpeg_y(jpeg->regs, ctx->out_q.h);
  1590. s5p_jpeg_imgadr(jpeg->regs, src_addr);
  1591. s5p_jpeg_jpgadr(jpeg->regs, dst_addr);
  1592. /* ultimately comes from sizeimage from userspace */
  1593. s5p_jpeg_enc_stream_int(jpeg->regs, ctx->cap_q.size);
  1594. /* JPEG RGB to YCbCr conversion matrix */
  1595. s5p_jpeg_coef(jpeg->regs, 1, 1, S5P_JPEG_COEF11);
  1596. s5p_jpeg_coef(jpeg->regs, 1, 2, S5P_JPEG_COEF12);
  1597. s5p_jpeg_coef(jpeg->regs, 1, 3, S5P_JPEG_COEF13);
  1598. s5p_jpeg_coef(jpeg->regs, 2, 1, S5P_JPEG_COEF21);
  1599. s5p_jpeg_coef(jpeg->regs, 2, 2, S5P_JPEG_COEF22);
  1600. s5p_jpeg_coef(jpeg->regs, 2, 3, S5P_JPEG_COEF23);
  1601. s5p_jpeg_coef(jpeg->regs, 3, 1, S5P_JPEG_COEF31);
  1602. s5p_jpeg_coef(jpeg->regs, 3, 2, S5P_JPEG_COEF32);
  1603. s5p_jpeg_coef(jpeg->regs, 3, 3, S5P_JPEG_COEF33);
  1604. /*
  1605. * JPEG IP allows storing 4 quantization tables
  1606. * We fill table 0 for luma and table 1 for chroma
  1607. */
  1608. s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1609. s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1610. /* use table 0 for Y */
  1611. s5p_jpeg_qtbl(jpeg->regs, 1, 0);
  1612. /* use table 1 for Cb and Cr*/
  1613. s5p_jpeg_qtbl(jpeg->regs, 2, 1);
  1614. s5p_jpeg_qtbl(jpeg->regs, 3, 1);
  1615. /* Y, Cb, Cr use Huffman table 0 */
  1616. s5p_jpeg_htbl_ac(jpeg->regs, 1);
  1617. s5p_jpeg_htbl_dc(jpeg->regs, 1);
  1618. s5p_jpeg_htbl_ac(jpeg->regs, 2);
  1619. s5p_jpeg_htbl_dc(jpeg->regs, 2);
  1620. s5p_jpeg_htbl_ac(jpeg->regs, 3);
  1621. s5p_jpeg_htbl_dc(jpeg->regs, 3);
  1622. } else { /* S5P_JPEG_DECODE */
  1623. s5p_jpeg_rst_int_enable(jpeg->regs, true);
  1624. s5p_jpeg_data_num_int_enable(jpeg->regs, true);
  1625. s5p_jpeg_final_mcu_num_int_enable(jpeg->regs, true);
  1626. if (ctx->cap_q.fmt->fourcc == V4L2_PIX_FMT_YUYV)
  1627. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_422);
  1628. else
  1629. s5p_jpeg_outform_raw(jpeg->regs, S5P_JPEG_RAW_OUT_420);
  1630. s5p_jpeg_jpgadr(jpeg->regs, src_addr);
  1631. s5p_jpeg_imgadr(jpeg->regs, dst_addr);
  1632. }
  1633. s5p_jpeg_start(jpeg->regs);
  1634. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1635. }
  1636. static void exynos4_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
  1637. {
  1638. struct s5p_jpeg *jpeg = ctx->jpeg;
  1639. struct s5p_jpeg_fmt *fmt;
  1640. struct vb2_buffer *vb;
  1641. struct s5p_jpeg_addr jpeg_addr;
  1642. u32 pix_size, padding_bytes = 0;
  1643. pix_size = ctx->cap_q.w * ctx->cap_q.h;
  1644. if (ctx->mode == S5P_JPEG_ENCODE) {
  1645. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1646. fmt = ctx->out_q.fmt;
  1647. if (ctx->out_q.w % 2 && fmt->h_align > 0)
  1648. padding_bytes = ctx->out_q.h;
  1649. } else {
  1650. fmt = ctx->cap_q.fmt;
  1651. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1652. }
  1653. jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
  1654. if (fmt->colplanes == 2) {
  1655. jpeg_addr.cb = jpeg_addr.y + pix_size - padding_bytes;
  1656. } else if (fmt->colplanes == 3) {
  1657. jpeg_addr.cb = jpeg_addr.y + pix_size;
  1658. if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
  1659. jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
  1660. else
  1661. jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
  1662. }
  1663. exynos4_jpeg_set_frame_buf_address(jpeg->regs, &jpeg_addr);
  1664. }
  1665. static void exynos4_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
  1666. {
  1667. struct s5p_jpeg *jpeg = ctx->jpeg;
  1668. struct vb2_buffer *vb;
  1669. unsigned int jpeg_addr = 0;
  1670. if (ctx->mode == S5P_JPEG_ENCODE)
  1671. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1672. else
  1673. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1674. jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  1675. exynos4_jpeg_set_stream_buf_address(jpeg->regs, jpeg_addr);
  1676. }
  1677. static void exynos4_jpeg_device_run(void *priv)
  1678. {
  1679. struct s5p_jpeg_ctx *ctx = priv;
  1680. struct s5p_jpeg *jpeg = ctx->jpeg;
  1681. unsigned int bitstream_size;
  1682. unsigned long flags;
  1683. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1684. if (ctx->mode == S5P_JPEG_ENCODE) {
  1685. exynos4_jpeg_sw_reset(jpeg->regs);
  1686. exynos4_jpeg_set_interrupt(jpeg->regs);
  1687. exynos4_jpeg_set_huf_table_enable(jpeg->regs, 1);
  1688. exynos4_jpeg_set_huff_tbl(jpeg->regs);
  1689. /*
  1690. * JPEG IP allows storing 4 quantization tables
  1691. * We fill table 0 for luma and table 1 for chroma
  1692. */
  1693. exynos4_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1694. exynos4_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1695. exynos4_jpeg_set_encode_tbl_select(jpeg->regs,
  1696. ctx->compr_quality);
  1697. exynos4_jpeg_set_stream_size(jpeg->regs, ctx->cap_q.w,
  1698. ctx->cap_q.h);
  1699. exynos4_jpeg_set_enc_out_fmt(jpeg->regs, ctx->subsampling);
  1700. exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->out_q.fmt->fourcc);
  1701. exynos4_jpeg_set_img_addr(ctx);
  1702. exynos4_jpeg_set_jpeg_addr(ctx);
  1703. exynos4_jpeg_set_encode_hoff_cnt(jpeg->regs,
  1704. ctx->out_q.fmt->fourcc);
  1705. } else {
  1706. exynos4_jpeg_sw_reset(jpeg->regs);
  1707. exynos4_jpeg_set_interrupt(jpeg->regs);
  1708. exynos4_jpeg_set_img_addr(ctx);
  1709. exynos4_jpeg_set_jpeg_addr(ctx);
  1710. exynos4_jpeg_set_img_fmt(jpeg->regs, ctx->cap_q.fmt->fourcc);
  1711. bitstream_size = DIV_ROUND_UP(ctx->out_q.size, 32);
  1712. exynos4_jpeg_set_dec_bitstream_size(jpeg->regs, bitstream_size);
  1713. }
  1714. exynos4_jpeg_set_enc_dec_mode(jpeg->regs, ctx->mode);
  1715. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1716. }
  1717. static void exynos3250_jpeg_set_img_addr(struct s5p_jpeg_ctx *ctx)
  1718. {
  1719. struct s5p_jpeg *jpeg = ctx->jpeg;
  1720. struct s5p_jpeg_fmt *fmt;
  1721. struct vb2_buffer *vb;
  1722. struct s5p_jpeg_addr jpeg_addr;
  1723. u32 pix_size;
  1724. pix_size = ctx->cap_q.w * ctx->cap_q.h;
  1725. if (ctx->mode == S5P_JPEG_ENCODE) {
  1726. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1727. fmt = ctx->out_q.fmt;
  1728. } else {
  1729. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1730. fmt = ctx->cap_q.fmt;
  1731. }
  1732. jpeg_addr.y = vb2_dma_contig_plane_dma_addr(vb, 0);
  1733. if (fmt->colplanes == 2) {
  1734. jpeg_addr.cb = jpeg_addr.y + pix_size;
  1735. } else if (fmt->colplanes == 3) {
  1736. jpeg_addr.cb = jpeg_addr.y + pix_size;
  1737. if (fmt->fourcc == V4L2_PIX_FMT_YUV420)
  1738. jpeg_addr.cr = jpeg_addr.cb + pix_size / 4;
  1739. else
  1740. jpeg_addr.cr = jpeg_addr.cb + pix_size / 2;
  1741. }
  1742. exynos3250_jpeg_imgadr(jpeg->regs, &jpeg_addr);
  1743. }
  1744. static void exynos3250_jpeg_set_jpeg_addr(struct s5p_jpeg_ctx *ctx)
  1745. {
  1746. struct s5p_jpeg *jpeg = ctx->jpeg;
  1747. struct vb2_buffer *vb;
  1748. unsigned int jpeg_addr = 0;
  1749. if (ctx->mode == S5P_JPEG_ENCODE)
  1750. vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1751. else
  1752. vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1753. jpeg_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  1754. exynos3250_jpeg_jpgadr(jpeg->regs, jpeg_addr);
  1755. }
  1756. static void exynos3250_jpeg_device_run(void *priv)
  1757. {
  1758. struct s5p_jpeg_ctx *ctx = priv;
  1759. struct s5p_jpeg *jpeg = ctx->jpeg;
  1760. unsigned long flags;
  1761. spin_lock_irqsave(&ctx->jpeg->slock, flags);
  1762. exynos3250_jpeg_reset(jpeg->regs);
  1763. exynos3250_jpeg_set_dma_num(jpeg->regs);
  1764. exynos3250_jpeg_poweron(jpeg->regs);
  1765. exynos3250_jpeg_clk_set(jpeg->regs);
  1766. exynos3250_jpeg_proc_mode(jpeg->regs, ctx->mode);
  1767. if (ctx->mode == S5P_JPEG_ENCODE) {
  1768. exynos3250_jpeg_input_raw_fmt(jpeg->regs,
  1769. ctx->out_q.fmt->fourcc);
  1770. exynos3250_jpeg_dri(jpeg->regs, ctx->restart_interval);
  1771. /*
  1772. * JPEG IP allows storing 4 quantization tables
  1773. * We fill table 0 for luma and table 1 for chroma
  1774. */
  1775. s5p_jpeg_set_qtbl_lum(jpeg->regs, ctx->compr_quality);
  1776. s5p_jpeg_set_qtbl_chr(jpeg->regs, ctx->compr_quality);
  1777. /* use table 0 for Y */
  1778. exynos3250_jpeg_qtbl(jpeg->regs, 1, 0);
  1779. /* use table 1 for Cb and Cr*/
  1780. exynos3250_jpeg_qtbl(jpeg->regs, 2, 1);
  1781. exynos3250_jpeg_qtbl(jpeg->regs, 3, 1);
  1782. /* Y, Cb, Cr use Huffman table 0 */
  1783. exynos3250_jpeg_htbl_ac(jpeg->regs, 1);
  1784. exynos3250_jpeg_htbl_dc(jpeg->regs, 1);
  1785. exynos3250_jpeg_htbl_ac(jpeg->regs, 2);
  1786. exynos3250_jpeg_htbl_dc(jpeg->regs, 2);
  1787. exynos3250_jpeg_htbl_ac(jpeg->regs, 3);
  1788. exynos3250_jpeg_htbl_dc(jpeg->regs, 3);
  1789. exynos3250_jpeg_set_x(jpeg->regs, ctx->crop_rect.width);
  1790. exynos3250_jpeg_set_y(jpeg->regs, ctx->crop_rect.height);
  1791. exynos3250_jpeg_stride(jpeg->regs, ctx->out_q.fmt->fourcc,
  1792. ctx->out_q.w);
  1793. exynos3250_jpeg_offset(jpeg->regs, ctx->crop_rect.left,
  1794. ctx->crop_rect.top);
  1795. exynos3250_jpeg_set_img_addr(ctx);
  1796. exynos3250_jpeg_set_jpeg_addr(ctx);
  1797. exynos3250_jpeg_subsampling_mode(jpeg->regs, ctx->subsampling);
  1798. /* ultimately comes from sizeimage from userspace */
  1799. exynos3250_jpeg_enc_stream_bound(jpeg->regs, ctx->cap_q.size);
  1800. if (ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565 ||
  1801. ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB565X ||
  1802. ctx->out_q.fmt->fourcc == V4L2_PIX_FMT_RGB32)
  1803. exynos3250_jpeg_set_y16(jpeg->regs, true);
  1804. } else {
  1805. exynos3250_jpeg_set_img_addr(ctx);
  1806. exynos3250_jpeg_set_jpeg_addr(ctx);
  1807. exynos3250_jpeg_stride(jpeg->regs, ctx->cap_q.fmt->fourcc,
  1808. ctx->cap_q.w);
  1809. exynos3250_jpeg_offset(jpeg->regs, 0, 0);
  1810. exynos3250_jpeg_dec_scaling_ratio(jpeg->regs,
  1811. ctx->scale_factor);
  1812. exynos3250_jpeg_dec_stream_size(jpeg->regs, ctx->out_q.size);
  1813. exynos3250_jpeg_output_raw_fmt(jpeg->regs,
  1814. ctx->cap_q.fmt->fourcc);
  1815. }
  1816. exynos3250_jpeg_interrupts_enable(jpeg->regs);
  1817. /* JPEG RGB to YCbCr conversion matrix */
  1818. exynos3250_jpeg_coef(jpeg->regs, ctx->mode);
  1819. exynos3250_jpeg_set_timer(jpeg->regs, EXYNOS3250_IRQ_TIMEOUT);
  1820. jpeg->irq_status = 0;
  1821. exynos3250_jpeg_start(jpeg->regs);
  1822. spin_unlock_irqrestore(&ctx->jpeg->slock, flags);
  1823. }
  1824. static int s5p_jpeg_job_ready(void *priv)
  1825. {
  1826. struct s5p_jpeg_ctx *ctx = priv;
  1827. if (ctx->mode == S5P_JPEG_DECODE)
  1828. return ctx->hdr_parsed;
  1829. return 1;
  1830. }
  1831. static void s5p_jpeg_job_abort(void *priv)
  1832. {
  1833. }
  1834. static struct v4l2_m2m_ops s5p_jpeg_m2m_ops = {
  1835. .device_run = s5p_jpeg_device_run,
  1836. .job_ready = s5p_jpeg_job_ready,
  1837. .job_abort = s5p_jpeg_job_abort,
  1838. };
  1839. static struct v4l2_m2m_ops exynos3250_jpeg_m2m_ops = {
  1840. .device_run = exynos3250_jpeg_device_run,
  1841. .job_ready = s5p_jpeg_job_ready,
  1842. .job_abort = s5p_jpeg_job_abort,
  1843. };
  1844. static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
  1845. .device_run = exynos4_jpeg_device_run,
  1846. .job_ready = s5p_jpeg_job_ready,
  1847. .job_abort = s5p_jpeg_job_abort,
  1848. };
  1849. /*
  1850. * ============================================================================
  1851. * Queue operations
  1852. * ============================================================================
  1853. */
  1854. static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
  1855. const struct v4l2_format *fmt,
  1856. unsigned int *nbuffers, unsigned int *nplanes,
  1857. unsigned int sizes[], void *alloc_ctxs[])
  1858. {
  1859. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
  1860. struct s5p_jpeg_q_data *q_data = NULL;
  1861. unsigned int size, count = *nbuffers;
  1862. q_data = get_q_data(ctx, vq->type);
  1863. BUG_ON(q_data == NULL);
  1864. size = q_data->size;
  1865. /*
  1866. * header is parsed during decoding and parsed information stored
  1867. * in the context so we do not allow another buffer to overwrite it
  1868. */
  1869. if (ctx->mode == S5P_JPEG_DECODE)
  1870. count = 1;
  1871. *nbuffers = count;
  1872. *nplanes = 1;
  1873. sizes[0] = size;
  1874. alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
  1875. return 0;
  1876. }
  1877. static int s5p_jpeg_buf_prepare(struct vb2_buffer *vb)
  1878. {
  1879. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1880. struct s5p_jpeg_q_data *q_data = NULL;
  1881. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1882. BUG_ON(q_data == NULL);
  1883. if (vb2_plane_size(vb, 0) < q_data->size) {
  1884. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  1885. __func__, vb2_plane_size(vb, 0),
  1886. (long)q_data->size);
  1887. return -EINVAL;
  1888. }
  1889. vb2_set_plane_payload(vb, 0, q_data->size);
  1890. return 0;
  1891. }
  1892. static void s5p_jpeg_buf_queue(struct vb2_buffer *vb)
  1893. {
  1894. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1895. if (ctx->mode == S5P_JPEG_DECODE &&
  1896. vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1897. struct s5p_jpeg_q_data tmp, *q_data;
  1898. ctx->hdr_parsed = s5p_jpeg_parse_hdr(&tmp,
  1899. (unsigned long)vb2_plane_vaddr(vb, 0),
  1900. min((unsigned long)ctx->out_q.size,
  1901. vb2_get_plane_payload(vb, 0)), ctx);
  1902. if (!ctx->hdr_parsed) {
  1903. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1904. return;
  1905. }
  1906. q_data = &ctx->out_q;
  1907. q_data->w = tmp.w;
  1908. q_data->h = tmp.h;
  1909. q_data = &ctx->cap_q;
  1910. q_data->w = tmp.w;
  1911. q_data->h = tmp.h;
  1912. }
  1913. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
  1914. }
  1915. static int s5p_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
  1916. {
  1917. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  1918. int ret;
  1919. ret = pm_runtime_get_sync(ctx->jpeg->dev);
  1920. return ret > 0 ? 0 : ret;
  1921. }
  1922. static void s5p_jpeg_stop_streaming(struct vb2_queue *q)
  1923. {
  1924. struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(q);
  1925. pm_runtime_put(ctx->jpeg->dev);
  1926. }
  1927. static struct vb2_ops s5p_jpeg_qops = {
  1928. .queue_setup = s5p_jpeg_queue_setup,
  1929. .buf_prepare = s5p_jpeg_buf_prepare,
  1930. .buf_queue = s5p_jpeg_buf_queue,
  1931. .wait_prepare = vb2_ops_wait_prepare,
  1932. .wait_finish = vb2_ops_wait_finish,
  1933. .start_streaming = s5p_jpeg_start_streaming,
  1934. .stop_streaming = s5p_jpeg_stop_streaming,
  1935. };
  1936. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1937. struct vb2_queue *dst_vq)
  1938. {
  1939. struct s5p_jpeg_ctx *ctx = priv;
  1940. int ret;
  1941. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1942. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1943. src_vq->drv_priv = ctx;
  1944. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1945. src_vq->ops = &s5p_jpeg_qops;
  1946. src_vq->mem_ops = &vb2_dma_contig_memops;
  1947. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1948. src_vq->lock = &ctx->jpeg->lock;
  1949. ret = vb2_queue_init(src_vq);
  1950. if (ret)
  1951. return ret;
  1952. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1953. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1954. dst_vq->drv_priv = ctx;
  1955. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1956. dst_vq->ops = &s5p_jpeg_qops;
  1957. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1958. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1959. dst_vq->lock = &ctx->jpeg->lock;
  1960. return vb2_queue_init(dst_vq);
  1961. }
  1962. /*
  1963. * ============================================================================
  1964. * ISR
  1965. * ============================================================================
  1966. */
  1967. static irqreturn_t s5p_jpeg_irq(int irq, void *dev_id)
  1968. {
  1969. struct s5p_jpeg *jpeg = dev_id;
  1970. struct s5p_jpeg_ctx *curr_ctx;
  1971. struct vb2_buffer *src_buf, *dst_buf;
  1972. unsigned long payload_size = 0;
  1973. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  1974. bool enc_jpeg_too_large = false;
  1975. bool timer_elapsed = false;
  1976. bool op_completed = false;
  1977. spin_lock(&jpeg->slock);
  1978. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  1979. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  1980. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  1981. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  1982. enc_jpeg_too_large = s5p_jpeg_enc_stream_stat(jpeg->regs);
  1983. timer_elapsed = s5p_jpeg_timer_stat(jpeg->regs);
  1984. op_completed = s5p_jpeg_result_stat_ok(jpeg->regs);
  1985. if (curr_ctx->mode == S5P_JPEG_DECODE)
  1986. op_completed = op_completed &&
  1987. s5p_jpeg_stream_stat_ok(jpeg->regs);
  1988. if (enc_jpeg_too_large) {
  1989. state = VB2_BUF_STATE_ERROR;
  1990. s5p_jpeg_clear_enc_stream_stat(jpeg->regs);
  1991. } else if (timer_elapsed) {
  1992. state = VB2_BUF_STATE_ERROR;
  1993. s5p_jpeg_clear_timer_stat(jpeg->regs);
  1994. } else if (!op_completed) {
  1995. state = VB2_BUF_STATE_ERROR;
  1996. } else {
  1997. payload_size = s5p_jpeg_compressed_size(jpeg->regs);
  1998. }
  1999. dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
  2000. dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
  2001. dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  2002. dst_buf->v4l2_buf.flags |=
  2003. src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  2004. v4l2_m2m_buf_done(src_buf, state);
  2005. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  2006. vb2_set_plane_payload(dst_buf, 0, payload_size);
  2007. v4l2_m2m_buf_done(dst_buf, state);
  2008. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2009. curr_ctx->subsampling = s5p_jpeg_get_subsampling_mode(jpeg->regs);
  2010. spin_unlock(&jpeg->slock);
  2011. s5p_jpeg_clear_int(jpeg->regs);
  2012. return IRQ_HANDLED;
  2013. }
  2014. static irqreturn_t exynos4_jpeg_irq(int irq, void *priv)
  2015. {
  2016. unsigned int int_status;
  2017. struct vb2_buffer *src_vb, *dst_vb;
  2018. struct s5p_jpeg *jpeg = priv;
  2019. struct s5p_jpeg_ctx *curr_ctx;
  2020. unsigned long payload_size = 0;
  2021. spin_lock(&jpeg->slock);
  2022. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  2023. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  2024. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  2025. int_status = exynos4_jpeg_get_int_status(jpeg->regs);
  2026. if (int_status) {
  2027. switch (int_status & 0x1f) {
  2028. case 0x1:
  2029. jpeg->irq_ret = ERR_PROT;
  2030. break;
  2031. case 0x2:
  2032. jpeg->irq_ret = OK_ENC_OR_DEC;
  2033. break;
  2034. case 0x4:
  2035. jpeg->irq_ret = ERR_DEC_INVALID_FORMAT;
  2036. break;
  2037. case 0x8:
  2038. jpeg->irq_ret = ERR_MULTI_SCAN;
  2039. break;
  2040. case 0x10:
  2041. jpeg->irq_ret = ERR_FRAME;
  2042. break;
  2043. default:
  2044. jpeg->irq_ret = ERR_UNKNOWN;
  2045. break;
  2046. }
  2047. } else {
  2048. jpeg->irq_ret = ERR_UNKNOWN;
  2049. }
  2050. if (jpeg->irq_ret == OK_ENC_OR_DEC) {
  2051. if (curr_ctx->mode == S5P_JPEG_ENCODE) {
  2052. payload_size = exynos4_jpeg_get_stream_size(jpeg->regs);
  2053. vb2_set_plane_payload(dst_vb, 0, payload_size);
  2054. }
  2055. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  2056. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  2057. } else {
  2058. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
  2059. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
  2060. }
  2061. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2062. curr_ctx->subsampling = exynos4_jpeg_get_frame_fmt(jpeg->regs);
  2063. spin_unlock(&jpeg->slock);
  2064. return IRQ_HANDLED;
  2065. }
  2066. static irqreturn_t exynos3250_jpeg_irq(int irq, void *dev_id)
  2067. {
  2068. struct s5p_jpeg *jpeg = dev_id;
  2069. struct s5p_jpeg_ctx *curr_ctx;
  2070. struct vb2_buffer *src_buf, *dst_buf;
  2071. unsigned long payload_size = 0;
  2072. enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
  2073. bool interrupt_timeout = false;
  2074. u32 irq_status;
  2075. spin_lock(&jpeg->slock);
  2076. irq_status = exynos3250_jpeg_get_timer_status(jpeg->regs);
  2077. if (irq_status & EXYNOS3250_TIMER_INT_STAT) {
  2078. exynos3250_jpeg_clear_timer_status(jpeg->regs);
  2079. interrupt_timeout = true;
  2080. dev_err(jpeg->dev, "Interrupt timeout occurred.\n");
  2081. }
  2082. irq_status = exynos3250_jpeg_get_int_status(jpeg->regs);
  2083. exynos3250_jpeg_clear_int_status(jpeg->regs, irq_status);
  2084. jpeg->irq_status |= irq_status;
  2085. curr_ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
  2086. if (!curr_ctx)
  2087. goto exit_unlock;
  2088. if ((irq_status & EXYNOS3250_HEADER_STAT) &&
  2089. (curr_ctx->mode == S5P_JPEG_DECODE)) {
  2090. exynos3250_jpeg_rstart(jpeg->regs);
  2091. goto exit_unlock;
  2092. }
  2093. if (jpeg->irq_status & (EXYNOS3250_JPEG_DONE |
  2094. EXYNOS3250_WDMA_DONE |
  2095. EXYNOS3250_RDMA_DONE |
  2096. EXYNOS3250_RESULT_STAT))
  2097. payload_size = exynos3250_jpeg_compressed_size(jpeg->regs);
  2098. else if (interrupt_timeout)
  2099. state = VB2_BUF_STATE_ERROR;
  2100. else
  2101. goto exit_unlock;
  2102. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  2103. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  2104. dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
  2105. dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
  2106. v4l2_m2m_buf_done(src_buf, state);
  2107. if (curr_ctx->mode == S5P_JPEG_ENCODE)
  2108. vb2_set_plane_payload(dst_buf, 0, payload_size);
  2109. v4l2_m2m_buf_done(dst_buf, state);
  2110. v4l2_m2m_job_finish(jpeg->m2m_dev, curr_ctx->fh.m2m_ctx);
  2111. curr_ctx->subsampling =
  2112. exynos3250_jpeg_get_subsampling_mode(jpeg->regs);
  2113. exit_unlock:
  2114. spin_unlock(&jpeg->slock);
  2115. return IRQ_HANDLED;
  2116. }
  2117. static void *jpeg_get_drv_data(struct device *dev);
  2118. /*
  2119. * ============================================================================
  2120. * Driver basic infrastructure
  2121. * ============================================================================
  2122. */
  2123. static int s5p_jpeg_probe(struct platform_device *pdev)
  2124. {
  2125. struct s5p_jpeg *jpeg;
  2126. struct resource *res;
  2127. int ret;
  2128. /* JPEG IP abstraction struct */
  2129. jpeg = devm_kzalloc(&pdev->dev, sizeof(struct s5p_jpeg), GFP_KERNEL);
  2130. if (!jpeg)
  2131. return -ENOMEM;
  2132. jpeg->variant = jpeg_get_drv_data(&pdev->dev);
  2133. mutex_init(&jpeg->lock);
  2134. spin_lock_init(&jpeg->slock);
  2135. jpeg->dev = &pdev->dev;
  2136. /* memory-mapped registers */
  2137. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2138. jpeg->regs = devm_ioremap_resource(&pdev->dev, res);
  2139. if (IS_ERR(jpeg->regs))
  2140. return PTR_ERR(jpeg->regs);
  2141. /* interrupt service routine registration */
  2142. jpeg->irq = ret = platform_get_irq(pdev, 0);
  2143. if (ret < 0) {
  2144. dev_err(&pdev->dev, "cannot find IRQ\n");
  2145. return ret;
  2146. }
  2147. ret = devm_request_irq(&pdev->dev, jpeg->irq, jpeg->variant->jpeg_irq,
  2148. 0, dev_name(&pdev->dev), jpeg);
  2149. if (ret) {
  2150. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpeg->irq);
  2151. return ret;
  2152. }
  2153. /* clocks */
  2154. jpeg->clk = clk_get(&pdev->dev, "jpeg");
  2155. if (IS_ERR(jpeg->clk)) {
  2156. dev_err(&pdev->dev, "cannot get clock\n");
  2157. ret = PTR_ERR(jpeg->clk);
  2158. return ret;
  2159. }
  2160. dev_dbg(&pdev->dev, "clock source %p\n", jpeg->clk);
  2161. jpeg->sclk = clk_get(&pdev->dev, "sclk");
  2162. if (IS_ERR(jpeg->sclk))
  2163. dev_info(&pdev->dev, "sclk clock not available\n");
  2164. /* v4l2 device */
  2165. ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
  2166. if (ret) {
  2167. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  2168. goto clk_get_rollback;
  2169. }
  2170. /* mem2mem device */
  2171. jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
  2172. if (IS_ERR(jpeg->m2m_dev)) {
  2173. v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
  2174. ret = PTR_ERR(jpeg->m2m_dev);
  2175. goto device_register_rollback;
  2176. }
  2177. jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  2178. if (IS_ERR(jpeg->alloc_ctx)) {
  2179. v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
  2180. ret = PTR_ERR(jpeg->alloc_ctx);
  2181. goto m2m_init_rollback;
  2182. }
  2183. /* JPEG encoder /dev/videoX node */
  2184. jpeg->vfd_encoder = video_device_alloc();
  2185. if (!jpeg->vfd_encoder) {
  2186. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  2187. ret = -ENOMEM;
  2188. goto vb2_allocator_rollback;
  2189. }
  2190. snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
  2191. "%s-enc", S5P_JPEG_M2M_NAME);
  2192. jpeg->vfd_encoder->fops = &s5p_jpeg_fops;
  2193. jpeg->vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  2194. jpeg->vfd_encoder->minor = -1;
  2195. jpeg->vfd_encoder->release = video_device_release;
  2196. jpeg->vfd_encoder->lock = &jpeg->lock;
  2197. jpeg->vfd_encoder->v4l2_dev = &jpeg->v4l2_dev;
  2198. jpeg->vfd_encoder->vfl_dir = VFL_DIR_M2M;
  2199. ret = video_register_device(jpeg->vfd_encoder, VFL_TYPE_GRABBER, -1);
  2200. if (ret) {
  2201. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  2202. goto enc_vdev_alloc_rollback;
  2203. }
  2204. video_set_drvdata(jpeg->vfd_encoder, jpeg);
  2205. v4l2_info(&jpeg->v4l2_dev,
  2206. "encoder device registered as /dev/video%d\n",
  2207. jpeg->vfd_encoder->num);
  2208. /* JPEG decoder /dev/videoX node */
  2209. jpeg->vfd_decoder = video_device_alloc();
  2210. if (!jpeg->vfd_decoder) {
  2211. v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
  2212. ret = -ENOMEM;
  2213. goto enc_vdev_register_rollback;
  2214. }
  2215. snprintf(jpeg->vfd_decoder->name, sizeof(jpeg->vfd_decoder->name),
  2216. "%s-dec", S5P_JPEG_M2M_NAME);
  2217. jpeg->vfd_decoder->fops = &s5p_jpeg_fops;
  2218. jpeg->vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
  2219. jpeg->vfd_decoder->minor = -1;
  2220. jpeg->vfd_decoder->release = video_device_release;
  2221. jpeg->vfd_decoder->lock = &jpeg->lock;
  2222. jpeg->vfd_decoder->v4l2_dev = &jpeg->v4l2_dev;
  2223. jpeg->vfd_decoder->vfl_dir = VFL_DIR_M2M;
  2224. ret = video_register_device(jpeg->vfd_decoder, VFL_TYPE_GRABBER, -1);
  2225. if (ret) {
  2226. v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
  2227. goto dec_vdev_alloc_rollback;
  2228. }
  2229. video_set_drvdata(jpeg->vfd_decoder, jpeg);
  2230. v4l2_info(&jpeg->v4l2_dev,
  2231. "decoder device registered as /dev/video%d\n",
  2232. jpeg->vfd_decoder->num);
  2233. /* final statements & power management */
  2234. platform_set_drvdata(pdev, jpeg);
  2235. pm_runtime_enable(&pdev->dev);
  2236. v4l2_info(&jpeg->v4l2_dev, "Samsung S5P JPEG codec\n");
  2237. return 0;
  2238. dec_vdev_alloc_rollback:
  2239. video_device_release(jpeg->vfd_decoder);
  2240. enc_vdev_register_rollback:
  2241. video_unregister_device(jpeg->vfd_encoder);
  2242. enc_vdev_alloc_rollback:
  2243. video_device_release(jpeg->vfd_encoder);
  2244. vb2_allocator_rollback:
  2245. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  2246. m2m_init_rollback:
  2247. v4l2_m2m_release(jpeg->m2m_dev);
  2248. device_register_rollback:
  2249. v4l2_device_unregister(&jpeg->v4l2_dev);
  2250. clk_get_rollback:
  2251. clk_put(jpeg->clk);
  2252. if (!IS_ERR(jpeg->sclk))
  2253. clk_put(jpeg->sclk);
  2254. return ret;
  2255. }
  2256. static int s5p_jpeg_remove(struct platform_device *pdev)
  2257. {
  2258. struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
  2259. pm_runtime_disable(jpeg->dev);
  2260. video_unregister_device(jpeg->vfd_decoder);
  2261. video_device_release(jpeg->vfd_decoder);
  2262. video_unregister_device(jpeg->vfd_encoder);
  2263. video_device_release(jpeg->vfd_encoder);
  2264. vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
  2265. v4l2_m2m_release(jpeg->m2m_dev);
  2266. v4l2_device_unregister(&jpeg->v4l2_dev);
  2267. if (!pm_runtime_status_suspended(&pdev->dev)) {
  2268. clk_disable_unprepare(jpeg->clk);
  2269. if (!IS_ERR(jpeg->sclk))
  2270. clk_disable_unprepare(jpeg->sclk);
  2271. }
  2272. clk_put(jpeg->clk);
  2273. if (!IS_ERR(jpeg->sclk))
  2274. clk_put(jpeg->sclk);
  2275. return 0;
  2276. }
  2277. #if defined(CONFIG_PM_RUNTIME) || defined(CONFIG_PM_SLEEP)
  2278. static int s5p_jpeg_runtime_suspend(struct device *dev)
  2279. {
  2280. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  2281. clk_disable_unprepare(jpeg->clk);
  2282. if (!IS_ERR(jpeg->sclk))
  2283. clk_disable_unprepare(jpeg->sclk);
  2284. return 0;
  2285. }
  2286. static int s5p_jpeg_runtime_resume(struct device *dev)
  2287. {
  2288. struct s5p_jpeg *jpeg = dev_get_drvdata(dev);
  2289. unsigned long flags;
  2290. int ret;
  2291. ret = clk_prepare_enable(jpeg->clk);
  2292. if (ret < 0)
  2293. return ret;
  2294. if (!IS_ERR(jpeg->sclk)) {
  2295. ret = clk_prepare_enable(jpeg->sclk);
  2296. if (ret < 0)
  2297. return ret;
  2298. }
  2299. spin_lock_irqsave(&jpeg->slock, flags);
  2300. /*
  2301. * JPEG IP allows storing two Huffman tables for each component.
  2302. * We fill table 0 for each component and do this here only
  2303. * for S5PC210 and Exynos3250 SoCs. Exynos4x12 SoC requires
  2304. * programming its Huffman tables each time the encoding process
  2305. * is initialized, and thus it is accomplished in the device_run
  2306. * callback of m2m_ops.
  2307. */
  2308. if (jpeg->variant->version == SJPEG_S5P ||
  2309. jpeg->variant->version == SJPEG_EXYNOS3250) {
  2310. s5p_jpeg_set_hdctbl(jpeg->regs);
  2311. s5p_jpeg_set_hdctblg(jpeg->regs);
  2312. s5p_jpeg_set_hactbl(jpeg->regs);
  2313. s5p_jpeg_set_hactblg(jpeg->regs);
  2314. }
  2315. spin_unlock_irqrestore(&jpeg->slock, flags);
  2316. return 0;
  2317. }
  2318. #endif /* CONFIG_PM_RUNTIME || CONFIG_PM_SLEEP */
  2319. #ifdef CONFIG_PM_SLEEP
  2320. static int s5p_jpeg_suspend(struct device *dev)
  2321. {
  2322. if (pm_runtime_suspended(dev))
  2323. return 0;
  2324. return s5p_jpeg_runtime_suspend(dev);
  2325. }
  2326. static int s5p_jpeg_resume(struct device *dev)
  2327. {
  2328. if (pm_runtime_suspended(dev))
  2329. return 0;
  2330. return s5p_jpeg_runtime_resume(dev);
  2331. }
  2332. #endif
  2333. static const struct dev_pm_ops s5p_jpeg_pm_ops = {
  2334. SET_SYSTEM_SLEEP_PM_OPS(s5p_jpeg_suspend, s5p_jpeg_resume)
  2335. SET_RUNTIME_PM_OPS(s5p_jpeg_runtime_suspend, s5p_jpeg_runtime_resume, NULL)
  2336. };
  2337. static struct s5p_jpeg_variant s5p_jpeg_drvdata = {
  2338. .version = SJPEG_S5P,
  2339. .jpeg_irq = s5p_jpeg_irq,
  2340. .m2m_ops = &s5p_jpeg_m2m_ops,
  2341. .fmt_ver_flag = SJPEG_FMT_FLAG_S5P,
  2342. };
  2343. static struct s5p_jpeg_variant exynos3250_jpeg_drvdata = {
  2344. .version = SJPEG_EXYNOS3250,
  2345. .jpeg_irq = exynos3250_jpeg_irq,
  2346. .m2m_ops = &exynos3250_jpeg_m2m_ops,
  2347. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS3250,
  2348. };
  2349. static struct s5p_jpeg_variant exynos4_jpeg_drvdata = {
  2350. .version = SJPEG_EXYNOS4,
  2351. .jpeg_irq = exynos4_jpeg_irq,
  2352. .m2m_ops = &exynos4_jpeg_m2m_ops,
  2353. .fmt_ver_flag = SJPEG_FMT_FLAG_EXYNOS4,
  2354. };
  2355. static const struct of_device_id samsung_jpeg_match[] = {
  2356. {
  2357. .compatible = "samsung,s5pv210-jpeg",
  2358. .data = &s5p_jpeg_drvdata,
  2359. }, {
  2360. .compatible = "samsung,exynos3250-jpeg",
  2361. .data = &exynos3250_jpeg_drvdata,
  2362. }, {
  2363. .compatible = "samsung,exynos4210-jpeg",
  2364. .data = &exynos4_jpeg_drvdata,
  2365. }, {
  2366. .compatible = "samsung,exynos4212-jpeg",
  2367. .data = &exynos4_jpeg_drvdata,
  2368. },
  2369. {},
  2370. };
  2371. MODULE_DEVICE_TABLE(of, samsung_jpeg_match);
  2372. static void *jpeg_get_drv_data(struct device *dev)
  2373. {
  2374. struct s5p_jpeg_variant *driver_data = NULL;
  2375. const struct of_device_id *match;
  2376. if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
  2377. return &s5p_jpeg_drvdata;
  2378. match = of_match_node(samsung_jpeg_match, dev->of_node);
  2379. if (match)
  2380. driver_data = (struct s5p_jpeg_variant *)match->data;
  2381. return driver_data;
  2382. }
  2383. static struct platform_driver s5p_jpeg_driver = {
  2384. .probe = s5p_jpeg_probe,
  2385. .remove = s5p_jpeg_remove,
  2386. .driver = {
  2387. .of_match_table = of_match_ptr(samsung_jpeg_match),
  2388. .owner = THIS_MODULE,
  2389. .name = S5P_JPEG_M2M_NAME,
  2390. .pm = &s5p_jpeg_pm_ops,
  2391. },
  2392. };
  2393. module_platform_driver(s5p_jpeg_driver);
  2394. MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@samsung.com>");
  2395. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  2396. MODULE_DESCRIPTION("Samsung JPEG codec driver");
  2397. MODULE_LICENSE("GPL");