rcar_jpu.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Mikhail Ulyanov
  4. * Copyright (C) 2014-2015 Cogent Embedded, Inc. <source@cogentembedded.com>
  5. * Copyright (C) 2014-2015 Renesas Electronics Corporation
  6. *
  7. * This is based on the drivers/media/platform/s5p-jpeg driver by
  8. * Andrzej Pietrasiewicz and Jacek Anaszewski.
  9. * Some portions of code inspired by VSP1 driver by Laurent Pinchart.
  10. *
  11. * TODO in order of priority:
  12. * 1) Rotation
  13. * 2) Cropping
  14. * 3) V4L2_CID_JPEG_ACTIVE_MARKER
  15. */
  16. #include <asm/unaligned.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/string.h>
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/v4l2-fh.h>
  34. #include <media/v4l2-mem2mem.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/videobuf2-v4l2.h>
  37. #include <media/videobuf2-dma-contig.h>
  38. #define DRV_NAME "rcar_jpu"
  39. /*
  40. * Align JPEG header end to cache line to make sure we will not have any issues
  41. * with cache; additionally to requerment (33.3.27 R01UH0501EJ0100 Rev.1.00)
  42. */
  43. #define JPU_JPEG_HDR_SIZE (ALIGN(0x258, L1_CACHE_BYTES))
  44. #define JPU_JPEG_MAX_BYTES_PER_PIXEL 2 /* 16 bit precision format */
  45. #define JPU_JPEG_MIN_SIZE 25 /* SOI + SOF + EOI */
  46. #define JPU_JPEG_QTBL_SIZE 0x40
  47. #define JPU_JPEG_HDCTBL_SIZE 0x1c
  48. #define JPU_JPEG_HACTBL_SIZE 0xb2
  49. #define JPU_JPEG_HEIGHT_OFFSET 0x91
  50. #define JPU_JPEG_WIDTH_OFFSET 0x93
  51. #define JPU_JPEG_SUBS_OFFSET 0x97
  52. #define JPU_JPEG_QTBL_LUM_OFFSET 0x07
  53. #define JPU_JPEG_QTBL_CHR_OFFSET 0x4c
  54. #define JPU_JPEG_HDCTBL_LUM_OFFSET 0xa4
  55. #define JPU_JPEG_HACTBL_LUM_OFFSET 0xc5
  56. #define JPU_JPEG_HDCTBL_CHR_OFFSET 0x17c
  57. #define JPU_JPEG_HACTBL_CHR_OFFSET 0x19d
  58. #define JPU_JPEG_PADDING_OFFSET 0x24f
  59. #define JPU_JPEG_LUM 0x00
  60. #define JPU_JPEG_CHR 0x01
  61. #define JPU_JPEG_DC 0x00
  62. #define JPU_JPEG_AC 0x10
  63. #define JPU_JPEG_422 0x21
  64. #define JPU_JPEG_420 0x22
  65. #define JPU_JPEG_DEFAULT_422_PIX_FMT V4L2_PIX_FMT_NV16M
  66. #define JPU_JPEG_DEFAULT_420_PIX_FMT V4L2_PIX_FMT_NV12M
  67. /* JPEG markers */
  68. #define TEM 0x01
  69. #define SOF0 0xc0
  70. #define RST 0xd0
  71. #define SOI 0xd8
  72. #define EOI 0xd9
  73. #define DHP 0xde
  74. #define DHT 0xc4
  75. #define COM 0xfe
  76. #define DQT 0xdb
  77. #define DRI 0xdd
  78. #define APP0 0xe0
  79. #define JPU_RESET_TIMEOUT 100 /* ms */
  80. #define JPU_JOB_TIMEOUT 300 /* ms */
  81. #define JPU_MAX_QUALITY 4
  82. #define JPU_WIDTH_MIN 16
  83. #define JPU_HEIGHT_MIN 16
  84. #define JPU_WIDTH_MAX 4096
  85. #define JPU_HEIGHT_MAX 4096
  86. #define JPU_MEMALIGN 8
  87. /* Flags that indicate a format can be used for capture/output */
  88. #define JPU_FMT_TYPE_OUTPUT 0
  89. #define JPU_FMT_TYPE_CAPTURE 1
  90. #define JPU_ENC_CAPTURE (1 << 0)
  91. #define JPU_ENC_OUTPUT (1 << 1)
  92. #define JPU_DEC_CAPTURE (1 << 2)
  93. #define JPU_DEC_OUTPUT (1 << 3)
  94. /*
  95. * JPEG registers and bits
  96. */
  97. /* JPEG code mode register */
  98. #define JCMOD 0x00
  99. #define JCMOD_PCTR (1 << 7)
  100. #define JCMOD_MSKIP_ENABLE (1 << 5)
  101. #define JCMOD_DSP_ENC (0 << 3)
  102. #define JCMOD_DSP_DEC (1 << 3)
  103. #define JCMOD_REDU (7 << 0)
  104. #define JCMOD_REDU_422 (1 << 0)
  105. #define JCMOD_REDU_420 (2 << 0)
  106. /* JPEG code command register */
  107. #define JCCMD 0x04
  108. #define JCCMD_SRST (1 << 12)
  109. #define JCCMD_JEND (1 << 2)
  110. #define JCCMD_JSRT (1 << 0)
  111. /* JPEG code quantanization table number register */
  112. #define JCQTN 0x0c
  113. #define JCQTN_SHIFT(t) (((t) - 1) << 1)
  114. /* JPEG code Huffman table number register */
  115. #define JCHTN 0x10
  116. #define JCHTN_AC_SHIFT(t) (((t) << 1) - 1)
  117. #define JCHTN_DC_SHIFT(t) (((t) - 1) << 1)
  118. #define JCVSZU 0x1c /* JPEG code vertical size upper register */
  119. #define JCVSZD 0x20 /* JPEG code vertical size lower register */
  120. #define JCHSZU 0x24 /* JPEG code horizontal size upper register */
  121. #define JCHSZD 0x28 /* JPEG code horizontal size lower register */
  122. #define JCSZ_MASK 0xff /* JPEG code h/v size register contains only 1 byte*/
  123. #define JCDTCU 0x2c /* JPEG code data count upper register */
  124. #define JCDTCM 0x30 /* JPEG code data count middle register */
  125. #define JCDTCD 0x34 /* JPEG code data count lower register */
  126. /* JPEG interrupt enable register */
  127. #define JINTE 0x38
  128. #define JINTE_ERR (7 << 5) /* INT5 + INT6 + INT7 */
  129. #define JINTE_TRANSF_COMPL (1 << 10)
  130. /* JPEG interrupt status register */
  131. #define JINTS 0x3c
  132. #define JINTS_MASK 0x7c68
  133. #define JINTS_ERR (1 << 5)
  134. #define JINTS_PROCESS_COMPL (1 << 6)
  135. #define JINTS_TRANSF_COMPL (1 << 10)
  136. #define JCDERR 0x40 /* JPEG code decode error register */
  137. #define JCDERR_MASK 0xf /* JPEG code decode error register mask*/
  138. /* JPEG interface encoding */
  139. #define JIFECNT 0x70
  140. #define JIFECNT_INFT_422 0
  141. #define JIFECNT_INFT_420 1
  142. #define JIFECNT_SWAP_WB (3 << 4) /* to JPU */
  143. #define JIFESYA1 0x74 /* encode source Y address register 1 */
  144. #define JIFESCA1 0x78 /* encode source C address register 1 */
  145. #define JIFESYA2 0x7c /* encode source Y address register 2 */
  146. #define JIFESCA2 0x80 /* encode source C address register 2 */
  147. #define JIFESMW 0x84 /* encode source memory width register */
  148. #define JIFESVSZ 0x88 /* encode source vertical size register */
  149. #define JIFESHSZ 0x8c /* encode source horizontal size register */
  150. #define JIFEDA1 0x90 /* encode destination address register 1 */
  151. #define JIFEDA2 0x94 /* encode destination address register 2 */
  152. /* JPEG decoding control register */
  153. #define JIFDCNT 0xa0
  154. #define JIFDCNT_SWAP_WB (3 << 1) /* from JPU */
  155. #define JIFDSA1 0xa4 /* decode source address register 1 */
  156. #define JIFDDMW 0xb0 /* decode destination memory width register */
  157. #define JIFDDVSZ 0xb4 /* decode destination vert. size register */
  158. #define JIFDDHSZ 0xb8 /* decode destination horiz. size register */
  159. #define JIFDDYA1 0xbc /* decode destination Y address register 1 */
  160. #define JIFDDCA1 0xc0 /* decode destination C address register 1 */
  161. #define JCQTBL(n) (0x10000 + (n) * 0x40) /* quantization tables regs */
  162. #define JCHTBD(n) (0x10100 + (n) * 0x100) /* Huffman table DC regs */
  163. #define JCHTBA(n) (0x10120 + (n) * 0x100) /* Huffman table AC regs */
  164. /**
  165. * struct jpu - JPEG IP abstraction
  166. * @mutex: the mutex protecting this structure
  167. * @lock: spinlock protecting the device contexts
  168. * @v4l2_dev: v4l2 device for mem2mem mode
  169. * @vfd_encoder: video device node for encoder mem2mem mode
  170. * @vfd_decoder: video device node for decoder mem2mem mode
  171. * @m2m_dev: v4l2 mem2mem device data
  172. * @curr: pointer to current context
  173. * @regs: JPEG IP registers mapping
  174. * @irq: JPEG IP irq
  175. * @clk: JPEG IP clock
  176. * @dev: JPEG IP struct device
  177. * @ref_count: reference counter
  178. */
  179. struct jpu {
  180. struct mutex mutex;
  181. spinlock_t lock;
  182. struct v4l2_device v4l2_dev;
  183. struct video_device vfd_encoder;
  184. struct video_device vfd_decoder;
  185. struct v4l2_m2m_dev *m2m_dev;
  186. struct jpu_ctx *curr;
  187. void __iomem *regs;
  188. unsigned int irq;
  189. struct clk *clk;
  190. struct device *dev;
  191. int ref_count;
  192. };
  193. /**
  194. * struct jpu_buffer - driver's specific video buffer
  195. * @buf: m2m buffer
  196. * @compr_quality: destination image quality in compression mode
  197. * @subsampling: source image subsampling in decompression mode
  198. */
  199. struct jpu_buffer {
  200. struct v4l2_m2m_buffer buf;
  201. unsigned short compr_quality;
  202. unsigned char subsampling;
  203. };
  204. /**
  205. * struct jpu_fmt - driver's internal format data
  206. * @fourcc: the fourcc code, 0 if not applicable
  207. * @colorspace: the colorspace specifier
  208. * @bpp: number of bits per pixel per plane
  209. * @h_align: horizontal alignment order (align to 2^h_align)
  210. * @v_align: vertical alignment order (align to 2^v_align)
  211. * @subsampling: (horizontal:4 | vertical:4) subsampling factor
  212. * @num_planes: number of planes
  213. * @types: types of queue this format is applicable to
  214. */
  215. struct jpu_fmt {
  216. u32 fourcc;
  217. u32 colorspace;
  218. u8 bpp[2];
  219. u8 h_align;
  220. u8 v_align;
  221. u8 subsampling;
  222. u8 num_planes;
  223. u16 types;
  224. };
  225. /**
  226. * struct jpu_q_data - parameters of one queue
  227. * @fmtinfo: driver-specific format of this queue
  228. * @format: multiplanar format of this queue
  229. * @sequence: sequence number
  230. */
  231. struct jpu_q_data {
  232. struct jpu_fmt *fmtinfo;
  233. struct v4l2_pix_format_mplane format;
  234. unsigned int sequence;
  235. };
  236. /**
  237. * struct jpu_ctx - the device context data
  238. * @jpu: JPEG IP device for this context
  239. * @encoder: compression (encode) operation or decompression (decode)
  240. * @compr_quality: destination image quality in compression (encode) mode
  241. * @out_q: source (output) queue information
  242. * @cap_q: destination (capture) queue information
  243. * @fh: file handler
  244. * @ctrl_handler: controls handler
  245. */
  246. struct jpu_ctx {
  247. struct jpu *jpu;
  248. bool encoder;
  249. unsigned short compr_quality;
  250. struct jpu_q_data out_q;
  251. struct jpu_q_data cap_q;
  252. struct v4l2_fh fh;
  253. struct v4l2_ctrl_handler ctrl_handler;
  254. };
  255. /**
  256. * jpeg_buffer - description of memory containing input JPEG data
  257. * @end: end position in the buffer
  258. * @curr: current position in the buffer
  259. */
  260. struct jpeg_buffer {
  261. void *end;
  262. void *curr;
  263. };
  264. static struct jpu_fmt jpu_formats[] = {
  265. { V4L2_PIX_FMT_JPEG, V4L2_COLORSPACE_JPEG,
  266. {0, 0}, 0, 0, 0, 1, JPU_ENC_CAPTURE | JPU_DEC_OUTPUT },
  267. { V4L2_PIX_FMT_NV16M, V4L2_COLORSPACE_SRGB,
  268. {8, 8}, 2, 2, JPU_JPEG_422, 2, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
  269. { V4L2_PIX_FMT_NV12M, V4L2_COLORSPACE_SRGB,
  270. {8, 4}, 2, 2, JPU_JPEG_420, 2, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
  271. { V4L2_PIX_FMT_NV16, V4L2_COLORSPACE_SRGB,
  272. {16, 0}, 2, 2, JPU_JPEG_422, 1, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
  273. { V4L2_PIX_FMT_NV12, V4L2_COLORSPACE_SRGB,
  274. {12, 0}, 2, 2, JPU_JPEG_420, 1, JPU_ENC_OUTPUT | JPU_DEC_CAPTURE },
  275. };
  276. static const u8 zigzag[] = {
  277. 0x03, 0x02, 0x0b, 0x13, 0x0a, 0x01, 0x00, 0x09,
  278. 0x12, 0x1b, 0x23, 0x1a, 0x11, 0x08, 0x07, 0x06,
  279. 0x0f, 0x10, 0x19, 0x22, 0x2b, 0x33, 0x2a, 0x21,
  280. 0x18, 0x17, 0x0e, 0x05, 0x04, 0x0d, 0x16, 0x1f,
  281. 0x20, 0x29, 0x32, 0x3b, 0x3a, 0x31, 0x28, 0x27,
  282. 0x1e, 0x15, 0x0e, 0x14, 0x10, 0x26, 0x2f, 0x30,
  283. 0x39, 0x38, 0x37, 0x2e, 0x25, 0x1c, 0x24, 0x2b,
  284. 0x36, 0x3f, 0x3e, 0x35, 0x2c, 0x34, 0x3d, 0x3c
  285. };
  286. #define QTBL_SIZE (ALIGN(JPU_JPEG_QTBL_SIZE, \
  287. sizeof(unsigned int)) / sizeof(unsigned int))
  288. #define HDCTBL_SIZE (ALIGN(JPU_JPEG_HDCTBL_SIZE, \
  289. sizeof(unsigned int)) / sizeof(unsigned int))
  290. #define HACTBL_SIZE (ALIGN(JPU_JPEG_HACTBL_SIZE, \
  291. sizeof(unsigned int)) / sizeof(unsigned int))
  292. /*
  293. * Start of image; Quantization tables
  294. * SOF0 (17 bytes payload) is Baseline DCT - Sample precision, height, width,
  295. * Number of image components, (Ci:8 - Hi:4 - Vi:4 - Tq:8) * 3 - Y,Cb,Cr;
  296. * Huffman tables; Padding with 0xff (33.3.27 R01UH0501EJ0100 Rev.1.00)
  297. */
  298. #define JPU_JPEG_HDR_BLOB { \
  299. 0xff, SOI, 0xff, DQT, 0x00, JPU_JPEG_QTBL_SIZE + 0x3, JPU_JPEG_LUM, \
  300. [JPU_JPEG_QTBL_LUM_OFFSET ... \
  301. JPU_JPEG_QTBL_LUM_OFFSET + JPU_JPEG_QTBL_SIZE - 1] = 0x00, \
  302. 0xff, DQT, 0x00, JPU_JPEG_QTBL_SIZE + 0x3, JPU_JPEG_CHR, \
  303. [JPU_JPEG_QTBL_CHR_OFFSET ... JPU_JPEG_QTBL_CHR_OFFSET + \
  304. JPU_JPEG_QTBL_SIZE - 1] = 0x00, 0xff, SOF0, 0x00, 0x11, 0x08, \
  305. [JPU_JPEG_HEIGHT_OFFSET ... JPU_JPEG_HEIGHT_OFFSET + 1] = 0x00, \
  306. [JPU_JPEG_WIDTH_OFFSET ... JPU_JPEG_WIDTH_OFFSET + 1] = 0x00, \
  307. 0x03, 0x01, [JPU_JPEG_SUBS_OFFSET] = 0x00, JPU_JPEG_LUM, \
  308. 0x02, 0x11, JPU_JPEG_CHR, 0x03, 0x11, JPU_JPEG_CHR, \
  309. 0xff, DHT, 0x00, JPU_JPEG_HDCTBL_SIZE + 0x3, JPU_JPEG_LUM|JPU_JPEG_DC, \
  310. [JPU_JPEG_HDCTBL_LUM_OFFSET ... \
  311. JPU_JPEG_HDCTBL_LUM_OFFSET + JPU_JPEG_HDCTBL_SIZE - 1] = 0x00, \
  312. 0xff, DHT, 0x00, JPU_JPEG_HACTBL_SIZE + 0x3, JPU_JPEG_LUM|JPU_JPEG_AC, \
  313. [JPU_JPEG_HACTBL_LUM_OFFSET ... \
  314. JPU_JPEG_HACTBL_LUM_OFFSET + JPU_JPEG_HACTBL_SIZE - 1] = 0x00, \
  315. 0xff, DHT, 0x00, JPU_JPEG_HDCTBL_SIZE + 0x3, JPU_JPEG_CHR|JPU_JPEG_DC, \
  316. [JPU_JPEG_HDCTBL_CHR_OFFSET ... \
  317. JPU_JPEG_HDCTBL_CHR_OFFSET + JPU_JPEG_HDCTBL_SIZE - 1] = 0x00, \
  318. 0xff, DHT, 0x00, JPU_JPEG_HACTBL_SIZE + 0x3, JPU_JPEG_CHR|JPU_JPEG_AC, \
  319. [JPU_JPEG_HACTBL_CHR_OFFSET ... \
  320. JPU_JPEG_HACTBL_CHR_OFFSET + JPU_JPEG_HACTBL_SIZE - 1] = 0x00, \
  321. [JPU_JPEG_PADDING_OFFSET ... JPU_JPEG_HDR_SIZE - 1] = 0xff \
  322. }
  323. static unsigned char jpeg_hdrs[JPU_MAX_QUALITY][JPU_JPEG_HDR_SIZE] = {
  324. [0 ... JPU_MAX_QUALITY - 1] = JPU_JPEG_HDR_BLOB
  325. };
  326. static const unsigned int qtbl_lum[JPU_MAX_QUALITY][QTBL_SIZE] = {
  327. {
  328. 0x14101927, 0x322e3e44, 0x10121726, 0x26354144,
  329. 0x19171f26, 0x35414444, 0x27262635, 0x41444444,
  330. 0x32263541, 0x44444444, 0x2e354144, 0x44444444,
  331. 0x3e414444, 0x44444444, 0x44444444, 0x44444444
  332. },
  333. {
  334. 0x100b0b10, 0x171b1f1e, 0x0b0c0c0f, 0x1417171e,
  335. 0x0b0c0d10, 0x171a232f, 0x100f1017, 0x1a252f40,
  336. 0x1714171a, 0x27334040, 0x1b171a25, 0x33404040,
  337. 0x1f17232f, 0x40404040, 0x1e1e2f40, 0x40404040
  338. },
  339. {
  340. 0x0c08080c, 0x11151817, 0x0809090b, 0x0f131217,
  341. 0x08090a0c, 0x13141b24, 0x0c0b0c15, 0x141c2435,
  342. 0x110f1314, 0x1e27333b, 0x1513141c, 0x27333b3b,
  343. 0x18121b24, 0x333b3b3b, 0x17172435, 0x3b3b3b3b
  344. },
  345. {
  346. 0x08060608, 0x0c0e1011, 0x06060608, 0x0a0d0c0f,
  347. 0x06060708, 0x0d0e1218, 0x0808080e, 0x0d131823,
  348. 0x0c0a0d0d, 0x141a2227, 0x0e0d0e13, 0x1a222727,
  349. 0x100c1318, 0x22272727, 0x110f1823, 0x27272727
  350. }
  351. };
  352. static const unsigned int qtbl_chr[JPU_MAX_QUALITY][QTBL_SIZE] = {
  353. {
  354. 0x15192026, 0x36444444, 0x191c1826, 0x36444444,
  355. 0x2018202b, 0x42444444, 0x26262b35, 0x44444444,
  356. 0x36424444, 0x44444444, 0x44444444, 0x44444444,
  357. 0x44444444, 0x44444444, 0x44444444, 0x44444444
  358. },
  359. {
  360. 0x110f1115, 0x141a2630, 0x0f131211, 0x141a232b,
  361. 0x11121416, 0x1a1e2e35, 0x1511161c, 0x1e273540,
  362. 0x14141a1e, 0x27304040, 0x1a1a1e27, 0x303f4040,
  363. 0x26232e35, 0x40404040, 0x302b3540, 0x40404040
  364. },
  365. {
  366. 0x0d0b0d10, 0x14141d25, 0x0b0e0e0e, 0x10141a20,
  367. 0x0d0e0f11, 0x14172328, 0x100e1115, 0x171e2832,
  368. 0x14101417, 0x1e25323b, 0x1414171e, 0x25303b3b,
  369. 0x1d1a2328, 0x323b3b3b, 0x25202832, 0x3b3b3b3b
  370. },
  371. {
  372. 0x0908090b, 0x0e111318, 0x080a090b, 0x0e0d1116,
  373. 0x09090d0e, 0x0d0f171a, 0x0b0b0e0e, 0x0f141a21,
  374. 0x0e0e0d0f, 0x14182127, 0x110d0f14, 0x18202727,
  375. 0x1311171a, 0x21272727, 0x18161a21, 0x27272727
  376. }
  377. };
  378. static const unsigned int hdctbl_lum[HDCTBL_SIZE] = {
  379. 0x00010501, 0x01010101, 0x01000000, 0x00000000,
  380. 0x00010203, 0x04050607, 0x08090a0b
  381. };
  382. static const unsigned int hdctbl_chr[HDCTBL_SIZE] = {
  383. 0x00010501, 0x01010101, 0x01000000, 0x00000000,
  384. 0x00010203, 0x04050607, 0x08090a0b
  385. };
  386. static const unsigned int hactbl_lum[HACTBL_SIZE] = {
  387. 0x00020103, 0x03020403, 0x05050404, 0x0000017d, 0x01020300, 0x04110512,
  388. 0x21314106, 0x13516107, 0x22711432, 0x8191a108, 0x2342b1c1, 0x1552d1f0,
  389. 0x24336272, 0x82090a16, 0x1718191a, 0x25262728, 0x292a3435, 0x36373839,
  390. 0x3a434445, 0x46474849, 0x4a535455, 0x56575859, 0x5a636465, 0x66676869,
  391. 0x6a737475, 0x76777879, 0x7a838485, 0x86878889, 0x8a929394, 0x95969798,
  392. 0x999aa2a3, 0xa4a5a6a7, 0xa8a9aab2, 0xb3b4b5b6, 0xb7b8b9ba, 0xc2c3c4c5,
  393. 0xc6c7c8c9, 0xcad2d3d4, 0xd5d6d7d8, 0xd9dae1e2, 0xe3e4e5e6, 0xe7e8e9ea,
  394. 0xf1f2f3f4, 0xf5f6f7f8, 0xf9fa0000
  395. };
  396. static const unsigned int hactbl_chr[HACTBL_SIZE] = {
  397. 0x00020103, 0x03020403, 0x05050404, 0x0000017d, 0x01020300, 0x04110512,
  398. 0x21314106, 0x13516107, 0x22711432, 0x8191a108, 0x2342b1c1, 0x1552d1f0,
  399. 0x24336272, 0x82090a16, 0x1718191a, 0x25262728, 0x292a3435, 0x36373839,
  400. 0x3a434445, 0x46474849, 0x4a535455, 0x56575859, 0x5a636465, 0x66676869,
  401. 0x6a737475, 0x76777879, 0x7a838485, 0x86878889, 0x8a929394, 0x95969798,
  402. 0x999aa2a3, 0xa4a5a6a7, 0xa8a9aab2, 0xb3b4b5b6, 0xb7b8b9ba, 0xc2c3c4c5,
  403. 0xc6c7c8c9, 0xcad2d3d4, 0xd5d6d7d8, 0xd9dae1e2, 0xe3e4e5e6, 0xe7e8e9ea,
  404. 0xf1f2f3f4, 0xf5f6f7f8, 0xf9fa0000
  405. };
  406. static const char *error_to_text[16] = {
  407. "Normal",
  408. "SOI not detected",
  409. "SOF1 to SOFF detected",
  410. "Subsampling not detected",
  411. "SOF accuracy error",
  412. "DQT accuracy error",
  413. "Component error 1",
  414. "Component error 2",
  415. "SOF0, DQT, and DHT not detected when SOS detected",
  416. "SOS not detected",
  417. "EOI not detected",
  418. "Restart interval data number error detected",
  419. "Image size error",
  420. "Last MCU data number error",
  421. "Block data number error",
  422. "Unknown"
  423. };
  424. static struct jpu_buffer *vb2_to_jpu_buffer(struct vb2_v4l2_buffer *vb)
  425. {
  426. struct v4l2_m2m_buffer *b =
  427. container_of(vb, struct v4l2_m2m_buffer, vb);
  428. return container_of(b, struct jpu_buffer, buf);
  429. }
  430. static u32 jpu_read(struct jpu *jpu, unsigned int reg)
  431. {
  432. return ioread32(jpu->regs + reg);
  433. }
  434. static void jpu_write(struct jpu *jpu, u32 val, unsigned int reg)
  435. {
  436. iowrite32(val, jpu->regs + reg);
  437. }
  438. static struct jpu_ctx *ctrl_to_ctx(struct v4l2_ctrl *c)
  439. {
  440. return container_of(c->handler, struct jpu_ctx, ctrl_handler);
  441. }
  442. static struct jpu_ctx *fh_to_ctx(struct v4l2_fh *fh)
  443. {
  444. return container_of(fh, struct jpu_ctx, fh);
  445. }
  446. static void jpu_set_tbl(struct jpu *jpu, u32 reg, const unsigned int *tbl,
  447. unsigned int len) {
  448. unsigned int i;
  449. for (i = 0; i < len; i++)
  450. jpu_write(jpu, tbl[i], reg + (i << 2));
  451. }
  452. static void jpu_set_qtbl(struct jpu *jpu, unsigned short quality)
  453. {
  454. jpu_set_tbl(jpu, JCQTBL(0), qtbl_lum[quality], QTBL_SIZE);
  455. jpu_set_tbl(jpu, JCQTBL(1), qtbl_chr[quality], QTBL_SIZE);
  456. }
  457. static void jpu_set_htbl(struct jpu *jpu)
  458. {
  459. jpu_set_tbl(jpu, JCHTBD(0), hdctbl_lum, HDCTBL_SIZE);
  460. jpu_set_tbl(jpu, JCHTBA(0), hactbl_lum, HACTBL_SIZE);
  461. jpu_set_tbl(jpu, JCHTBD(1), hdctbl_chr, HDCTBL_SIZE);
  462. jpu_set_tbl(jpu, JCHTBA(1), hactbl_chr, HACTBL_SIZE);
  463. }
  464. static int jpu_wait_reset(struct jpu *jpu)
  465. {
  466. unsigned long timeout;
  467. timeout = jiffies + msecs_to_jiffies(JPU_RESET_TIMEOUT);
  468. while (jpu_read(jpu, JCCMD) & JCCMD_SRST) {
  469. if (time_after(jiffies, timeout)) {
  470. dev_err(jpu->dev, "timed out in reset\n");
  471. return -ETIMEDOUT;
  472. }
  473. schedule();
  474. }
  475. return 0;
  476. }
  477. static int jpu_reset(struct jpu *jpu)
  478. {
  479. jpu_write(jpu, JCCMD_SRST, JCCMD);
  480. return jpu_wait_reset(jpu);
  481. }
  482. /*
  483. * ============================================================================
  484. * video ioctl operations
  485. * ============================================================================
  486. */
  487. static void put_qtbl(u8 *p, const u8 *qtbl)
  488. {
  489. unsigned int i;
  490. for (i = 0; i < ARRAY_SIZE(zigzag); i++)
  491. p[i] = *(qtbl + zigzag[i]);
  492. }
  493. static void put_htbl(u8 *p, const u8 *htbl, unsigned int len)
  494. {
  495. unsigned int i, j;
  496. for (i = 0; i < len; i += 4)
  497. for (j = 0; j < 4 && (i + j) < len; ++j)
  498. p[i + j] = htbl[i + 3 - j];
  499. }
  500. static void jpu_generate_hdr(unsigned short quality, unsigned char *p)
  501. {
  502. put_qtbl(p + JPU_JPEG_QTBL_LUM_OFFSET, (const u8 *)qtbl_lum[quality]);
  503. put_qtbl(p + JPU_JPEG_QTBL_CHR_OFFSET, (const u8 *)qtbl_chr[quality]);
  504. put_htbl(p + JPU_JPEG_HDCTBL_LUM_OFFSET, (const u8 *)hdctbl_lum,
  505. JPU_JPEG_HDCTBL_SIZE);
  506. put_htbl(p + JPU_JPEG_HACTBL_LUM_OFFSET, (const u8 *)hactbl_lum,
  507. JPU_JPEG_HACTBL_SIZE);
  508. put_htbl(p + JPU_JPEG_HDCTBL_CHR_OFFSET, (const u8 *)hdctbl_chr,
  509. JPU_JPEG_HDCTBL_SIZE);
  510. put_htbl(p + JPU_JPEG_HACTBL_CHR_OFFSET, (const u8 *)hactbl_chr,
  511. JPU_JPEG_HACTBL_SIZE);
  512. }
  513. static int get_byte(struct jpeg_buffer *buf)
  514. {
  515. if (buf->curr >= buf->end)
  516. return -1;
  517. return *(u8 *)buf->curr++;
  518. }
  519. static int get_word_be(struct jpeg_buffer *buf, unsigned int *word)
  520. {
  521. if (buf->end - buf->curr < 2)
  522. return -1;
  523. *word = get_unaligned_be16(buf->curr);
  524. buf->curr += 2;
  525. return 0;
  526. }
  527. static void skip(struct jpeg_buffer *buf, unsigned long len)
  528. {
  529. buf->curr += min((unsigned long)(buf->end - buf->curr), len);
  530. }
  531. static u8 jpu_parse_hdr(void *buffer, unsigned long size, unsigned int *width,
  532. unsigned int *height)
  533. {
  534. struct jpeg_buffer jpeg_buffer;
  535. unsigned int word;
  536. bool soi = false;
  537. jpeg_buffer.end = buffer + size;
  538. jpeg_buffer.curr = buffer;
  539. /*
  540. * basic size check and EOI - we don't want to let JPU cross
  541. * buffer bounds in any case. Hope it's stopping by EOI.
  542. */
  543. if (size < JPU_JPEG_MIN_SIZE || *(u8 *)(buffer + size - 1) != EOI)
  544. return 0;
  545. for (;;) {
  546. int c;
  547. /* skip preceding filler bytes */
  548. do
  549. c = get_byte(&jpeg_buffer);
  550. while (c == 0xff || c == 0);
  551. if (!soi && c == SOI) {
  552. soi = true;
  553. continue;
  554. } else if (soi != (c != SOI))
  555. return 0;
  556. switch (c) {
  557. case SOF0: /* SOF0: baseline JPEG */
  558. skip(&jpeg_buffer, 3); /* segment length and bpp */
  559. if (get_word_be(&jpeg_buffer, height) ||
  560. get_word_be(&jpeg_buffer, width) ||
  561. get_byte(&jpeg_buffer) != 3) /* YCbCr only */
  562. return 0;
  563. skip(&jpeg_buffer, 1);
  564. return get_byte(&jpeg_buffer);
  565. case DHT:
  566. case DQT:
  567. case COM:
  568. case DRI:
  569. case APP0 ... APP0 + 0x0f:
  570. if (get_word_be(&jpeg_buffer, &word))
  571. return 0;
  572. skip(&jpeg_buffer, (long)word - 2);
  573. case 0:
  574. break;
  575. default:
  576. return 0;
  577. }
  578. }
  579. return 0;
  580. }
  581. static int jpu_querycap(struct file *file, void *priv,
  582. struct v4l2_capability *cap)
  583. {
  584. struct jpu_ctx *ctx = fh_to_ctx(priv);
  585. if (ctx->encoder)
  586. strscpy(cap->card, DRV_NAME " encoder", sizeof(cap->card));
  587. else
  588. strscpy(cap->card, DRV_NAME " decoder", sizeof(cap->card));
  589. strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  590. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  591. dev_name(ctx->jpu->dev));
  592. memset(cap->reserved, 0, sizeof(cap->reserved));
  593. return 0;
  594. }
  595. static struct jpu_fmt *jpu_find_format(bool encoder, u32 pixelformat,
  596. unsigned int fmt_type)
  597. {
  598. unsigned int i, fmt_flag;
  599. if (encoder)
  600. fmt_flag = fmt_type == JPU_FMT_TYPE_OUTPUT ? JPU_ENC_OUTPUT :
  601. JPU_ENC_CAPTURE;
  602. else
  603. fmt_flag = fmt_type == JPU_FMT_TYPE_OUTPUT ? JPU_DEC_OUTPUT :
  604. JPU_DEC_CAPTURE;
  605. for (i = 0; i < ARRAY_SIZE(jpu_formats); i++) {
  606. struct jpu_fmt *fmt = &jpu_formats[i];
  607. if (fmt->fourcc == pixelformat && fmt->types & fmt_flag)
  608. return fmt;
  609. }
  610. return NULL;
  611. }
  612. static int jpu_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  613. {
  614. unsigned int i, num = 0;
  615. for (i = 0; i < ARRAY_SIZE(jpu_formats); ++i) {
  616. if (jpu_formats[i].types & type) {
  617. if (num == f->index)
  618. break;
  619. ++num;
  620. }
  621. }
  622. if (i >= ARRAY_SIZE(jpu_formats))
  623. return -EINVAL;
  624. f->pixelformat = jpu_formats[i].fourcc;
  625. return 0;
  626. }
  627. static int jpu_enum_fmt_cap(struct file *file, void *priv,
  628. struct v4l2_fmtdesc *f)
  629. {
  630. struct jpu_ctx *ctx = fh_to_ctx(priv);
  631. return jpu_enum_fmt(f, ctx->encoder ? JPU_ENC_CAPTURE :
  632. JPU_DEC_CAPTURE);
  633. }
  634. static int jpu_enum_fmt_out(struct file *file, void *priv,
  635. struct v4l2_fmtdesc *f)
  636. {
  637. struct jpu_ctx *ctx = fh_to_ctx(priv);
  638. return jpu_enum_fmt(f, ctx->encoder ? JPU_ENC_OUTPUT : JPU_DEC_OUTPUT);
  639. }
  640. static struct jpu_q_data *jpu_get_q_data(struct jpu_ctx *ctx,
  641. enum v4l2_buf_type type)
  642. {
  643. if (V4L2_TYPE_IS_OUTPUT(type))
  644. return &ctx->out_q;
  645. else
  646. return &ctx->cap_q;
  647. }
  648. static void jpu_bound_align_image(u32 *w, unsigned int w_min,
  649. unsigned int w_max, unsigned int w_align,
  650. u32 *h, unsigned int h_min,
  651. unsigned int h_max, unsigned int h_align)
  652. {
  653. unsigned int width, height, w_step, h_step;
  654. width = *w;
  655. height = *h;
  656. w_step = 1U << w_align;
  657. h_step = 1U << h_align;
  658. v4l_bound_align_image(w, w_min, w_max, w_align, h, h_min, h_max,
  659. h_align, 3);
  660. if (*w < width && *w + w_step < w_max)
  661. *w += w_step;
  662. if (*h < height && *h + h_step < h_max)
  663. *h += h_step;
  664. }
  665. static int __jpu_try_fmt(struct jpu_ctx *ctx, struct jpu_fmt **fmtinfo,
  666. struct v4l2_pix_format_mplane *pix,
  667. enum v4l2_buf_type type)
  668. {
  669. struct jpu_fmt *fmt;
  670. unsigned int f_type, w, h;
  671. f_type = V4L2_TYPE_IS_OUTPUT(type) ? JPU_FMT_TYPE_OUTPUT :
  672. JPU_FMT_TYPE_CAPTURE;
  673. fmt = jpu_find_format(ctx->encoder, pix->pixelformat, f_type);
  674. if (!fmt) {
  675. unsigned int pixelformat;
  676. dev_dbg(ctx->jpu->dev, "unknown format; set default format\n");
  677. if (ctx->encoder)
  678. pixelformat = f_type == JPU_FMT_TYPE_OUTPUT ?
  679. V4L2_PIX_FMT_NV16M : V4L2_PIX_FMT_JPEG;
  680. else
  681. pixelformat = f_type == JPU_FMT_TYPE_CAPTURE ?
  682. V4L2_PIX_FMT_NV16M : V4L2_PIX_FMT_JPEG;
  683. fmt = jpu_find_format(ctx->encoder, pixelformat, f_type);
  684. }
  685. pix->pixelformat = fmt->fourcc;
  686. pix->colorspace = fmt->colorspace;
  687. pix->field = V4L2_FIELD_NONE;
  688. pix->num_planes = fmt->num_planes;
  689. memset(pix->reserved, 0, sizeof(pix->reserved));
  690. jpu_bound_align_image(&pix->width, JPU_WIDTH_MIN, JPU_WIDTH_MAX,
  691. fmt->h_align, &pix->height, JPU_HEIGHT_MIN,
  692. JPU_HEIGHT_MAX, fmt->v_align);
  693. w = pix->width;
  694. h = pix->height;
  695. if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
  696. /* ignore userspaces's sizeimage for encoding */
  697. if (pix->plane_fmt[0].sizeimage <= 0 || ctx->encoder)
  698. pix->plane_fmt[0].sizeimage = JPU_JPEG_HDR_SIZE +
  699. (JPU_JPEG_MAX_BYTES_PER_PIXEL * w * h);
  700. pix->plane_fmt[0].bytesperline = 0;
  701. memset(pix->plane_fmt[0].reserved, 0,
  702. sizeof(pix->plane_fmt[0].reserved));
  703. } else {
  704. unsigned int i, bpl = 0;
  705. for (i = 0; i < pix->num_planes; ++i)
  706. bpl = max(bpl, pix->plane_fmt[i].bytesperline);
  707. bpl = clamp_t(unsigned int, bpl, w, JPU_WIDTH_MAX);
  708. bpl = round_up(bpl, JPU_MEMALIGN);
  709. for (i = 0; i < pix->num_planes; ++i) {
  710. pix->plane_fmt[i].bytesperline = bpl;
  711. pix->plane_fmt[i].sizeimage = bpl * h * fmt->bpp[i] / 8;
  712. memset(pix->plane_fmt[i].reserved, 0,
  713. sizeof(pix->plane_fmt[i].reserved));
  714. }
  715. }
  716. if (fmtinfo)
  717. *fmtinfo = fmt;
  718. return 0;
  719. }
  720. static int jpu_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  721. {
  722. struct jpu_ctx *ctx = fh_to_ctx(priv);
  723. if (!v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type))
  724. return -EINVAL;
  725. return __jpu_try_fmt(ctx, NULL, &f->fmt.pix_mp, f->type);
  726. }
  727. static int jpu_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  728. {
  729. struct vb2_queue *vq;
  730. struct jpu_ctx *ctx = fh_to_ctx(priv);
  731. struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  732. struct jpu_fmt *fmtinfo;
  733. struct jpu_q_data *q_data;
  734. int ret;
  735. vq = v4l2_m2m_get_vq(m2m_ctx, f->type);
  736. if (!vq)
  737. return -EINVAL;
  738. if (vb2_is_busy(vq)) {
  739. v4l2_err(&ctx->jpu->v4l2_dev, "%s queue busy\n", __func__);
  740. return -EBUSY;
  741. }
  742. ret = __jpu_try_fmt(ctx, &fmtinfo, &f->fmt.pix_mp, f->type);
  743. if (ret < 0)
  744. return ret;
  745. q_data = jpu_get_q_data(ctx, f->type);
  746. q_data->format = f->fmt.pix_mp;
  747. q_data->fmtinfo = fmtinfo;
  748. return 0;
  749. }
  750. static int jpu_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  751. {
  752. struct jpu_q_data *q_data;
  753. struct jpu_ctx *ctx = fh_to_ctx(priv);
  754. if (!v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type))
  755. return -EINVAL;
  756. q_data = jpu_get_q_data(ctx, f->type);
  757. f->fmt.pix_mp = q_data->format;
  758. return 0;
  759. }
  760. /*
  761. * V4L2 controls
  762. */
  763. static int jpu_s_ctrl(struct v4l2_ctrl *ctrl)
  764. {
  765. struct jpu_ctx *ctx = ctrl_to_ctx(ctrl);
  766. unsigned long flags;
  767. spin_lock_irqsave(&ctx->jpu->lock, flags);
  768. if (ctrl->id == V4L2_CID_JPEG_COMPRESSION_QUALITY)
  769. ctx->compr_quality = ctrl->val;
  770. spin_unlock_irqrestore(&ctx->jpu->lock, flags);
  771. return 0;
  772. }
  773. static const struct v4l2_ctrl_ops jpu_ctrl_ops = {
  774. .s_ctrl = jpu_s_ctrl,
  775. };
  776. static int jpu_streamon(struct file *file, void *priv, enum v4l2_buf_type type)
  777. {
  778. struct jpu_ctx *ctx = fh_to_ctx(priv);
  779. struct jpu_q_data *src_q_data, *dst_q_data, *orig, adj, *ref;
  780. enum v4l2_buf_type adj_type;
  781. src_q_data = jpu_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  782. dst_q_data = jpu_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  783. if (ctx->encoder) {
  784. adj = *src_q_data;
  785. orig = src_q_data;
  786. ref = dst_q_data;
  787. adj_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  788. } else {
  789. adj = *dst_q_data;
  790. orig = dst_q_data;
  791. ref = src_q_data;
  792. adj_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  793. }
  794. adj.format.width = ref->format.width;
  795. adj.format.height = ref->format.height;
  796. __jpu_try_fmt(ctx, NULL, &adj.format, adj_type);
  797. if (adj.format.width != orig->format.width ||
  798. adj.format.height != orig->format.height) {
  799. dev_err(ctx->jpu->dev, "src and dst formats do not match.\n");
  800. /* maybe we can return -EPIPE here? */
  801. return -EINVAL;
  802. }
  803. return v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, type);
  804. }
  805. static const struct v4l2_ioctl_ops jpu_ioctl_ops = {
  806. .vidioc_querycap = jpu_querycap,
  807. .vidioc_enum_fmt_vid_cap = jpu_enum_fmt_cap,
  808. .vidioc_enum_fmt_vid_out = jpu_enum_fmt_out,
  809. .vidioc_g_fmt_vid_cap_mplane = jpu_g_fmt,
  810. .vidioc_g_fmt_vid_out_mplane = jpu_g_fmt,
  811. .vidioc_try_fmt_vid_cap_mplane = jpu_try_fmt,
  812. .vidioc_try_fmt_vid_out_mplane = jpu_try_fmt,
  813. .vidioc_s_fmt_vid_cap_mplane = jpu_s_fmt,
  814. .vidioc_s_fmt_vid_out_mplane = jpu_s_fmt,
  815. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  816. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  817. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  818. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  819. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  820. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  821. .vidioc_streamon = jpu_streamon,
  822. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  823. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  824. .vidioc_unsubscribe_event = v4l2_event_unsubscribe
  825. };
  826. static int jpu_controls_create(struct jpu_ctx *ctx)
  827. {
  828. struct v4l2_ctrl *ctrl;
  829. int ret;
  830. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1);
  831. ctrl = v4l2_ctrl_new_std(&ctx->ctrl_handler, &jpu_ctrl_ops,
  832. V4L2_CID_JPEG_COMPRESSION_QUALITY,
  833. 0, JPU_MAX_QUALITY - 1, 1, 0);
  834. if (ctx->ctrl_handler.error) {
  835. ret = ctx->ctrl_handler.error;
  836. goto error_free;
  837. }
  838. if (!ctx->encoder)
  839. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE |
  840. V4L2_CTRL_FLAG_READ_ONLY;
  841. ret = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  842. if (ret < 0)
  843. goto error_free;
  844. return 0;
  845. error_free:
  846. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  847. return ret;
  848. }
  849. /*
  850. * ============================================================================
  851. * Queue operations
  852. * ============================================================================
  853. */
  854. static int jpu_queue_setup(struct vb2_queue *vq,
  855. unsigned int *nbuffers, unsigned int *nplanes,
  856. unsigned int sizes[], struct device *alloc_devs[])
  857. {
  858. struct jpu_ctx *ctx = vb2_get_drv_priv(vq);
  859. struct jpu_q_data *q_data;
  860. unsigned int i;
  861. q_data = jpu_get_q_data(ctx, vq->type);
  862. if (*nplanes) {
  863. if (*nplanes != q_data->format.num_planes)
  864. return -EINVAL;
  865. for (i = 0; i < *nplanes; i++) {
  866. unsigned int q_size = q_data->format.plane_fmt[i].sizeimage;
  867. if (sizes[i] < q_size)
  868. return -EINVAL;
  869. }
  870. return 0;
  871. }
  872. *nplanes = q_data->format.num_planes;
  873. for (i = 0; i < *nplanes; i++)
  874. sizes[i] = q_data->format.plane_fmt[i].sizeimage;
  875. return 0;
  876. }
  877. static int jpu_buf_prepare(struct vb2_buffer *vb)
  878. {
  879. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  880. struct jpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  881. struct jpu_q_data *q_data;
  882. unsigned int i;
  883. q_data = jpu_get_q_data(ctx, vb->vb2_queue->type);
  884. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  885. if (vbuf->field == V4L2_FIELD_ANY)
  886. vbuf->field = V4L2_FIELD_NONE;
  887. if (vbuf->field != V4L2_FIELD_NONE) {
  888. dev_err(ctx->jpu->dev, "%s field isn't supported\n",
  889. __func__);
  890. return -EINVAL;
  891. }
  892. }
  893. for (i = 0; i < q_data->format.num_planes; i++) {
  894. unsigned long size = q_data->format.plane_fmt[i].sizeimage;
  895. if (vb2_plane_size(vb, i) < size) {
  896. dev_err(ctx->jpu->dev,
  897. "%s: data will not fit into plane (%lu < %lu)\n",
  898. __func__, vb2_plane_size(vb, i), size);
  899. return -EINVAL;
  900. }
  901. /* decoder capture queue */
  902. if (!ctx->encoder && !V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
  903. vb2_set_plane_payload(vb, i, size);
  904. }
  905. return 0;
  906. }
  907. static void jpu_buf_queue(struct vb2_buffer *vb)
  908. {
  909. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  910. struct jpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  911. if (!ctx->encoder && V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  912. struct jpu_buffer *jpu_buf = vb2_to_jpu_buffer(vbuf);
  913. struct jpu_q_data *q_data, adjust;
  914. void *buffer = vb2_plane_vaddr(vb, 0);
  915. unsigned long buf_size = vb2_get_plane_payload(vb, 0);
  916. unsigned int width, height;
  917. u8 subsampling = jpu_parse_hdr(buffer, buf_size, &width,
  918. &height);
  919. /* check if JPEG data basic parsing was successful */
  920. if (subsampling != JPU_JPEG_422 && subsampling != JPU_JPEG_420)
  921. goto format_error;
  922. q_data = &ctx->out_q;
  923. adjust = *q_data;
  924. adjust.format.width = width;
  925. adjust.format.height = height;
  926. __jpu_try_fmt(ctx, &adjust.fmtinfo, &adjust.format,
  927. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  928. if (adjust.format.width != q_data->format.width ||
  929. adjust.format.height != q_data->format.height)
  930. goto format_error;
  931. /*
  932. * keep subsampling in buffer to check it
  933. * for compatibility in device_run
  934. */
  935. jpu_buf->subsampling = subsampling;
  936. }
  937. if (ctx->fh.m2m_ctx)
  938. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  939. return;
  940. format_error:
  941. dev_err(ctx->jpu->dev, "incompatible or corrupted JPEG data\n");
  942. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  943. }
  944. static void jpu_buf_finish(struct vb2_buffer *vb)
  945. {
  946. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  947. struct jpu_buffer *jpu_buf = vb2_to_jpu_buffer(vbuf);
  948. struct jpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  949. struct jpu_q_data *q_data = &ctx->out_q;
  950. enum v4l2_buf_type type = vb->vb2_queue->type;
  951. u8 *buffer;
  952. if (vb->state == VB2_BUF_STATE_DONE)
  953. vbuf->sequence = jpu_get_q_data(ctx, type)->sequence++;
  954. if (!ctx->encoder || vb->state != VB2_BUF_STATE_DONE ||
  955. V4L2_TYPE_IS_OUTPUT(type))
  956. return;
  957. buffer = vb2_plane_vaddr(vb, 0);
  958. memcpy(buffer, jpeg_hdrs[jpu_buf->compr_quality], JPU_JPEG_HDR_SIZE);
  959. *(__be16 *)(buffer + JPU_JPEG_HEIGHT_OFFSET) =
  960. cpu_to_be16(q_data->format.height);
  961. *(__be16 *)(buffer + JPU_JPEG_WIDTH_OFFSET) =
  962. cpu_to_be16(q_data->format.width);
  963. *(buffer + JPU_JPEG_SUBS_OFFSET) = q_data->fmtinfo->subsampling;
  964. }
  965. static int jpu_start_streaming(struct vb2_queue *vq, unsigned count)
  966. {
  967. struct jpu_ctx *ctx = vb2_get_drv_priv(vq);
  968. struct jpu_q_data *q_data = jpu_get_q_data(ctx, vq->type);
  969. q_data->sequence = 0;
  970. return 0;
  971. }
  972. static void jpu_stop_streaming(struct vb2_queue *vq)
  973. {
  974. struct jpu_ctx *ctx = vb2_get_drv_priv(vq);
  975. struct vb2_v4l2_buffer *vb;
  976. unsigned long flags;
  977. for (;;) {
  978. if (V4L2_TYPE_IS_OUTPUT(vq->type))
  979. vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  980. else
  981. vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  982. if (vb == NULL)
  983. return;
  984. spin_lock_irqsave(&ctx->jpu->lock, flags);
  985. v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
  986. spin_unlock_irqrestore(&ctx->jpu->lock, flags);
  987. }
  988. }
  989. static const struct vb2_ops jpu_qops = {
  990. .queue_setup = jpu_queue_setup,
  991. .buf_prepare = jpu_buf_prepare,
  992. .buf_queue = jpu_buf_queue,
  993. .buf_finish = jpu_buf_finish,
  994. .start_streaming = jpu_start_streaming,
  995. .stop_streaming = jpu_stop_streaming,
  996. .wait_prepare = vb2_ops_wait_prepare,
  997. .wait_finish = vb2_ops_wait_finish,
  998. };
  999. static int jpu_queue_init(void *priv, struct vb2_queue *src_vq,
  1000. struct vb2_queue *dst_vq)
  1001. {
  1002. struct jpu_ctx *ctx = priv;
  1003. int ret;
  1004. memset(src_vq, 0, sizeof(*src_vq));
  1005. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1006. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1007. src_vq->drv_priv = ctx;
  1008. src_vq->buf_struct_size = sizeof(struct jpu_buffer);
  1009. src_vq->ops = &jpu_qops;
  1010. src_vq->mem_ops = &vb2_dma_contig_memops;
  1011. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1012. src_vq->lock = &ctx->jpu->mutex;
  1013. src_vq->dev = ctx->jpu->v4l2_dev.dev;
  1014. ret = vb2_queue_init(src_vq);
  1015. if (ret)
  1016. return ret;
  1017. memset(dst_vq, 0, sizeof(*dst_vq));
  1018. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1019. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1020. dst_vq->drv_priv = ctx;
  1021. dst_vq->buf_struct_size = sizeof(struct jpu_buffer);
  1022. dst_vq->ops = &jpu_qops;
  1023. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1024. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1025. dst_vq->lock = &ctx->jpu->mutex;
  1026. dst_vq->dev = ctx->jpu->v4l2_dev.dev;
  1027. return vb2_queue_init(dst_vq);
  1028. }
  1029. /*
  1030. * ============================================================================
  1031. * Device file operations
  1032. * ============================================================================
  1033. */
  1034. static int jpu_open(struct file *file)
  1035. {
  1036. struct jpu *jpu = video_drvdata(file);
  1037. struct video_device *vfd = video_devdata(file);
  1038. struct jpu_ctx *ctx;
  1039. int ret;
  1040. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1041. if (!ctx)
  1042. return -ENOMEM;
  1043. v4l2_fh_init(&ctx->fh, vfd);
  1044. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  1045. file->private_data = &ctx->fh;
  1046. v4l2_fh_add(&ctx->fh);
  1047. ctx->jpu = jpu;
  1048. ctx->encoder = vfd == &jpu->vfd_encoder;
  1049. __jpu_try_fmt(ctx, &ctx->out_q.fmtinfo, &ctx->out_q.format,
  1050. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1051. __jpu_try_fmt(ctx, &ctx->cap_q.fmtinfo, &ctx->cap_q.format,
  1052. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  1053. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpu->m2m_dev, ctx, jpu_queue_init);
  1054. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1055. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1056. goto v4l_prepare_rollback;
  1057. }
  1058. ret = jpu_controls_create(ctx);
  1059. if (ret < 0)
  1060. goto v4l_prepare_rollback;
  1061. if (mutex_lock_interruptible(&jpu->mutex)) {
  1062. ret = -ERESTARTSYS;
  1063. goto v4l_prepare_rollback;
  1064. }
  1065. if (jpu->ref_count == 0) {
  1066. ret = clk_prepare_enable(jpu->clk);
  1067. if (ret < 0)
  1068. goto device_prepare_rollback;
  1069. /* ...issue software reset */
  1070. ret = jpu_reset(jpu);
  1071. if (ret)
  1072. goto jpu_reset_rollback;
  1073. }
  1074. jpu->ref_count++;
  1075. mutex_unlock(&jpu->mutex);
  1076. return 0;
  1077. jpu_reset_rollback:
  1078. clk_disable_unprepare(jpu->clk);
  1079. device_prepare_rollback:
  1080. mutex_unlock(&jpu->mutex);
  1081. v4l_prepare_rollback:
  1082. v4l2_fh_del(&ctx->fh);
  1083. v4l2_fh_exit(&ctx->fh);
  1084. kfree(ctx);
  1085. return ret;
  1086. }
  1087. static int jpu_release(struct file *file)
  1088. {
  1089. struct jpu *jpu = video_drvdata(file);
  1090. struct jpu_ctx *ctx = fh_to_ctx(file->private_data);
  1091. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1092. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1093. v4l2_fh_del(&ctx->fh);
  1094. v4l2_fh_exit(&ctx->fh);
  1095. kfree(ctx);
  1096. mutex_lock(&jpu->mutex);
  1097. if (--jpu->ref_count == 0)
  1098. clk_disable_unprepare(jpu->clk);
  1099. mutex_unlock(&jpu->mutex);
  1100. return 0;
  1101. }
  1102. static const struct v4l2_file_operations jpu_fops = {
  1103. .owner = THIS_MODULE,
  1104. .open = jpu_open,
  1105. .release = jpu_release,
  1106. .unlocked_ioctl = video_ioctl2,
  1107. .poll = v4l2_m2m_fop_poll,
  1108. .mmap = v4l2_m2m_fop_mmap,
  1109. };
  1110. /*
  1111. * ============================================================================
  1112. * mem2mem callbacks
  1113. * ============================================================================
  1114. */
  1115. static void jpu_cleanup(struct jpu_ctx *ctx, bool reset)
  1116. {
  1117. /* remove current buffers and finish job */
  1118. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1119. unsigned long flags;
  1120. spin_lock_irqsave(&ctx->jpu->lock, flags);
  1121. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1122. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1123. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
  1124. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  1125. /* ...and give it a chance on next run */
  1126. if (reset)
  1127. jpu_write(ctx->jpu, JCCMD_SRST, JCCMD);
  1128. spin_unlock_irqrestore(&ctx->jpu->lock, flags);
  1129. v4l2_m2m_job_finish(ctx->jpu->m2m_dev, ctx->fh.m2m_ctx);
  1130. }
  1131. static void jpu_device_run(void *priv)
  1132. {
  1133. struct jpu_ctx *ctx = priv;
  1134. struct jpu *jpu = ctx->jpu;
  1135. struct jpu_buffer *jpu_buf;
  1136. struct jpu_q_data *q_data;
  1137. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1138. unsigned int w, h, bpl;
  1139. unsigned char num_planes, subsampling;
  1140. unsigned long flags;
  1141. /* ...wait until module reset completes; we have mutex locked here */
  1142. if (jpu_wait_reset(jpu)) {
  1143. jpu_cleanup(ctx, true);
  1144. return;
  1145. }
  1146. spin_lock_irqsave(&ctx->jpu->lock, flags);
  1147. jpu->curr = ctx;
  1148. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1149. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1150. if (ctx->encoder) {
  1151. jpu_buf = vb2_to_jpu_buffer(dst_buf);
  1152. q_data = &ctx->out_q;
  1153. } else {
  1154. jpu_buf = vb2_to_jpu_buffer(src_buf);
  1155. q_data = &ctx->cap_q;
  1156. }
  1157. w = q_data->format.width;
  1158. h = q_data->format.height;
  1159. bpl = q_data->format.plane_fmt[0].bytesperline;
  1160. num_planes = q_data->fmtinfo->num_planes;
  1161. subsampling = q_data->fmtinfo->subsampling;
  1162. if (ctx->encoder) {
  1163. unsigned long src_1_addr, src_2_addr, dst_addr;
  1164. unsigned int redu, inft;
  1165. dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
  1166. src_1_addr =
  1167. vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  1168. if (num_planes > 1)
  1169. src_2_addr = vb2_dma_contig_plane_dma_addr(
  1170. &src_buf->vb2_buf, 1);
  1171. else
  1172. src_2_addr = src_1_addr + w * h;
  1173. jpu_buf->compr_quality = ctx->compr_quality;
  1174. if (subsampling == JPU_JPEG_420) {
  1175. redu = JCMOD_REDU_420;
  1176. inft = JIFECNT_INFT_420;
  1177. } else {
  1178. redu = JCMOD_REDU_422;
  1179. inft = JIFECNT_INFT_422;
  1180. }
  1181. /* only no marker mode works for encoding */
  1182. jpu_write(jpu, JCMOD_DSP_ENC | JCMOD_PCTR | redu |
  1183. JCMOD_MSKIP_ENABLE, JCMOD);
  1184. jpu_write(jpu, JIFECNT_SWAP_WB | inft, JIFECNT);
  1185. jpu_write(jpu, JIFDCNT_SWAP_WB, JIFDCNT);
  1186. jpu_write(jpu, JINTE_TRANSF_COMPL, JINTE);
  1187. /* Y and C components source addresses */
  1188. jpu_write(jpu, src_1_addr, JIFESYA1);
  1189. jpu_write(jpu, src_2_addr, JIFESCA1);
  1190. /* memory width */
  1191. jpu_write(jpu, bpl, JIFESMW);
  1192. jpu_write(jpu, (w >> 8) & JCSZ_MASK, JCHSZU);
  1193. jpu_write(jpu, w & JCSZ_MASK, JCHSZD);
  1194. jpu_write(jpu, (h >> 8) & JCSZ_MASK, JCVSZU);
  1195. jpu_write(jpu, h & JCSZ_MASK, JCVSZD);
  1196. jpu_write(jpu, w, JIFESHSZ);
  1197. jpu_write(jpu, h, JIFESVSZ);
  1198. jpu_write(jpu, dst_addr + JPU_JPEG_HDR_SIZE, JIFEDA1);
  1199. jpu_write(jpu, 0 << JCQTN_SHIFT(1) | 1 << JCQTN_SHIFT(2) |
  1200. 1 << JCQTN_SHIFT(3), JCQTN);
  1201. jpu_write(jpu, 0 << JCHTN_AC_SHIFT(1) | 0 << JCHTN_DC_SHIFT(1) |
  1202. 1 << JCHTN_AC_SHIFT(2) | 1 << JCHTN_DC_SHIFT(2) |
  1203. 1 << JCHTN_AC_SHIFT(3) | 1 << JCHTN_DC_SHIFT(3),
  1204. JCHTN);
  1205. jpu_set_qtbl(jpu, ctx->compr_quality);
  1206. jpu_set_htbl(jpu);
  1207. } else {
  1208. unsigned long src_addr, dst_1_addr, dst_2_addr;
  1209. if (jpu_buf->subsampling != subsampling) {
  1210. dev_err(ctx->jpu->dev,
  1211. "src and dst formats do not match.\n");
  1212. spin_unlock_irqrestore(&ctx->jpu->lock, flags);
  1213. jpu_cleanup(ctx, false);
  1214. return;
  1215. }
  1216. src_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  1217. dst_1_addr =
  1218. vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
  1219. if (q_data->fmtinfo->num_planes > 1)
  1220. dst_2_addr = vb2_dma_contig_plane_dma_addr(
  1221. &dst_buf->vb2_buf, 1);
  1222. else
  1223. dst_2_addr = dst_1_addr + w * h;
  1224. /* ...set up decoder operation */
  1225. jpu_write(jpu, JCMOD_DSP_DEC | JCMOD_PCTR, JCMOD);
  1226. jpu_write(jpu, JIFECNT_SWAP_WB, JIFECNT);
  1227. jpu_write(jpu, JIFDCNT_SWAP_WB, JIFDCNT);
  1228. /* ...enable interrupts on transfer completion and d-g error */
  1229. jpu_write(jpu, JINTE_TRANSF_COMPL | JINTE_ERR, JINTE);
  1230. /* ...set source/destination addresses of encoded data */
  1231. jpu_write(jpu, src_addr, JIFDSA1);
  1232. jpu_write(jpu, dst_1_addr, JIFDDYA1);
  1233. jpu_write(jpu, dst_2_addr, JIFDDCA1);
  1234. jpu_write(jpu, bpl, JIFDDMW);
  1235. }
  1236. /* ...start encoder/decoder operation */
  1237. jpu_write(jpu, JCCMD_JSRT, JCCMD);
  1238. spin_unlock_irqrestore(&ctx->jpu->lock, flags);
  1239. }
  1240. static const struct v4l2_m2m_ops jpu_m2m_ops = {
  1241. .device_run = jpu_device_run,
  1242. };
  1243. /*
  1244. * ============================================================================
  1245. * IRQ handler
  1246. * ============================================================================
  1247. */
  1248. static irqreturn_t jpu_irq_handler(int irq, void *dev_id)
  1249. {
  1250. struct jpu *jpu = dev_id;
  1251. struct jpu_ctx *curr_ctx;
  1252. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1253. unsigned int int_status;
  1254. int_status = jpu_read(jpu, JINTS);
  1255. /* ...spurious interrupt */
  1256. if (!((JINTS_TRANSF_COMPL | JINTS_PROCESS_COMPL | JINTS_ERR) &
  1257. int_status))
  1258. return IRQ_NONE;
  1259. /* ...clear interrupts */
  1260. jpu_write(jpu, ~(int_status & JINTS_MASK), JINTS);
  1261. if (int_status & (JINTS_ERR | JINTS_PROCESS_COMPL))
  1262. jpu_write(jpu, JCCMD_JEND, JCCMD);
  1263. spin_lock(&jpu->lock);
  1264. if ((int_status & JINTS_PROCESS_COMPL) &&
  1265. !(int_status & JINTS_TRANSF_COMPL))
  1266. goto handled;
  1267. curr_ctx = v4l2_m2m_get_curr_priv(jpu->m2m_dev);
  1268. if (!curr_ctx) {
  1269. /* ...instance is not running */
  1270. dev_err(jpu->dev, "no active context for m2m\n");
  1271. goto handled;
  1272. }
  1273. src_buf = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  1274. dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  1275. if (int_status & JINTS_TRANSF_COMPL) {
  1276. if (curr_ctx->encoder) {
  1277. unsigned long payload_size = jpu_read(jpu, JCDTCU) << 16
  1278. | jpu_read(jpu, JCDTCM) << 8
  1279. | jpu_read(jpu, JCDTCD);
  1280. vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
  1281. payload_size + JPU_JPEG_HDR_SIZE);
  1282. }
  1283. dst_buf->field = src_buf->field;
  1284. dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
  1285. if (src_buf->flags & V4L2_BUF_FLAG_TIMECODE)
  1286. dst_buf->timecode = src_buf->timecode;
  1287. dst_buf->flags = src_buf->flags &
  1288. (V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_KEYFRAME |
  1289. V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME |
  1290. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  1291. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  1292. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  1293. } else if (int_status & JINTS_ERR) {
  1294. unsigned char error = jpu_read(jpu, JCDERR) & JCDERR_MASK;
  1295. dev_dbg(jpu->dev, "processing error: %#X: %s\n", error,
  1296. error_to_text[error]);
  1297. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
  1298. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  1299. }
  1300. jpu->curr = NULL;
  1301. /* ...reset JPU after completion */
  1302. jpu_write(jpu, JCCMD_SRST, JCCMD);
  1303. spin_unlock(&jpu->lock);
  1304. v4l2_m2m_job_finish(jpu->m2m_dev, curr_ctx->fh.m2m_ctx);
  1305. return IRQ_HANDLED;
  1306. handled:
  1307. spin_unlock(&jpu->lock);
  1308. return IRQ_HANDLED;
  1309. }
  1310. /*
  1311. * ============================================================================
  1312. * Driver basic infrastructure
  1313. * ============================================================================
  1314. */
  1315. static const struct of_device_id jpu_dt_ids[] = {
  1316. { .compatible = "renesas,jpu-r8a7790" }, /* H2 */
  1317. { .compatible = "renesas,jpu-r8a7791" }, /* M2-W */
  1318. { .compatible = "renesas,jpu-r8a7792" }, /* V2H */
  1319. { .compatible = "renesas,jpu-r8a7793" }, /* M2-N */
  1320. { .compatible = "renesas,rcar-gen2-jpu" },
  1321. { },
  1322. };
  1323. MODULE_DEVICE_TABLE(of, jpu_dt_ids);
  1324. static int jpu_probe(struct platform_device *pdev)
  1325. {
  1326. struct jpu *jpu;
  1327. struct resource *res;
  1328. int ret;
  1329. unsigned int i;
  1330. jpu = devm_kzalloc(&pdev->dev, sizeof(*jpu), GFP_KERNEL);
  1331. if (!jpu)
  1332. return -ENOMEM;
  1333. mutex_init(&jpu->mutex);
  1334. spin_lock_init(&jpu->lock);
  1335. jpu->dev = &pdev->dev;
  1336. /* memory-mapped registers */
  1337. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1338. jpu->regs = devm_ioremap_resource(&pdev->dev, res);
  1339. if (IS_ERR(jpu->regs))
  1340. return PTR_ERR(jpu->regs);
  1341. /* interrupt service routine registration */
  1342. jpu->irq = ret = platform_get_irq(pdev, 0);
  1343. if (ret < 0) {
  1344. dev_err(&pdev->dev, "cannot find IRQ\n");
  1345. return ret;
  1346. }
  1347. ret = devm_request_irq(&pdev->dev, jpu->irq, jpu_irq_handler, 0,
  1348. dev_name(&pdev->dev), jpu);
  1349. if (ret) {
  1350. dev_err(&pdev->dev, "cannot claim IRQ %d\n", jpu->irq);
  1351. return ret;
  1352. }
  1353. /* clocks */
  1354. jpu->clk = devm_clk_get(&pdev->dev, NULL);
  1355. if (IS_ERR(jpu->clk)) {
  1356. dev_err(&pdev->dev, "cannot get clock\n");
  1357. return PTR_ERR(jpu->clk);
  1358. }
  1359. /* v4l2 device */
  1360. ret = v4l2_device_register(&pdev->dev, &jpu->v4l2_dev);
  1361. if (ret) {
  1362. dev_err(&pdev->dev, "Failed to register v4l2 device\n");
  1363. return ret;
  1364. }
  1365. /* mem2mem device */
  1366. jpu->m2m_dev = v4l2_m2m_init(&jpu_m2m_ops);
  1367. if (IS_ERR(jpu->m2m_dev)) {
  1368. v4l2_err(&jpu->v4l2_dev, "Failed to init mem2mem device\n");
  1369. ret = PTR_ERR(jpu->m2m_dev);
  1370. goto device_register_rollback;
  1371. }
  1372. /* fill in qantization and Huffman tables for encoder */
  1373. for (i = 0; i < JPU_MAX_QUALITY; i++)
  1374. jpu_generate_hdr(i, (unsigned char *)jpeg_hdrs[i]);
  1375. strscpy(jpu->vfd_encoder.name, DRV_NAME, sizeof(jpu->vfd_encoder.name));
  1376. jpu->vfd_encoder.fops = &jpu_fops;
  1377. jpu->vfd_encoder.ioctl_ops = &jpu_ioctl_ops;
  1378. jpu->vfd_encoder.minor = -1;
  1379. jpu->vfd_encoder.release = video_device_release_empty;
  1380. jpu->vfd_encoder.lock = &jpu->mutex;
  1381. jpu->vfd_encoder.v4l2_dev = &jpu->v4l2_dev;
  1382. jpu->vfd_encoder.vfl_dir = VFL_DIR_M2M;
  1383. jpu->vfd_encoder.device_caps = V4L2_CAP_STREAMING |
  1384. V4L2_CAP_VIDEO_M2M_MPLANE;
  1385. ret = video_register_device(&jpu->vfd_encoder, VFL_TYPE_GRABBER, -1);
  1386. if (ret) {
  1387. v4l2_err(&jpu->v4l2_dev, "Failed to register video device\n");
  1388. goto m2m_init_rollback;
  1389. }
  1390. video_set_drvdata(&jpu->vfd_encoder, jpu);
  1391. strscpy(jpu->vfd_decoder.name, DRV_NAME, sizeof(jpu->vfd_decoder.name));
  1392. jpu->vfd_decoder.fops = &jpu_fops;
  1393. jpu->vfd_decoder.ioctl_ops = &jpu_ioctl_ops;
  1394. jpu->vfd_decoder.minor = -1;
  1395. jpu->vfd_decoder.release = video_device_release_empty;
  1396. jpu->vfd_decoder.lock = &jpu->mutex;
  1397. jpu->vfd_decoder.v4l2_dev = &jpu->v4l2_dev;
  1398. jpu->vfd_decoder.vfl_dir = VFL_DIR_M2M;
  1399. jpu->vfd_decoder.device_caps = V4L2_CAP_STREAMING |
  1400. V4L2_CAP_VIDEO_M2M_MPLANE;
  1401. ret = video_register_device(&jpu->vfd_decoder, VFL_TYPE_GRABBER, -1);
  1402. if (ret) {
  1403. v4l2_err(&jpu->v4l2_dev, "Failed to register video device\n");
  1404. goto enc_vdev_register_rollback;
  1405. }
  1406. video_set_drvdata(&jpu->vfd_decoder, jpu);
  1407. platform_set_drvdata(pdev, jpu);
  1408. v4l2_info(&jpu->v4l2_dev, "encoder device registered as /dev/video%d\n",
  1409. jpu->vfd_encoder.num);
  1410. v4l2_info(&jpu->v4l2_dev, "decoder device registered as /dev/video%d\n",
  1411. jpu->vfd_decoder.num);
  1412. return 0;
  1413. enc_vdev_register_rollback:
  1414. video_unregister_device(&jpu->vfd_encoder);
  1415. m2m_init_rollback:
  1416. v4l2_m2m_release(jpu->m2m_dev);
  1417. device_register_rollback:
  1418. v4l2_device_unregister(&jpu->v4l2_dev);
  1419. return ret;
  1420. }
  1421. static int jpu_remove(struct platform_device *pdev)
  1422. {
  1423. struct jpu *jpu = platform_get_drvdata(pdev);
  1424. video_unregister_device(&jpu->vfd_decoder);
  1425. video_unregister_device(&jpu->vfd_encoder);
  1426. v4l2_m2m_release(jpu->m2m_dev);
  1427. v4l2_device_unregister(&jpu->v4l2_dev);
  1428. return 0;
  1429. }
  1430. #ifdef CONFIG_PM_SLEEP
  1431. static int jpu_suspend(struct device *dev)
  1432. {
  1433. struct jpu *jpu = dev_get_drvdata(dev);
  1434. if (jpu->ref_count == 0)
  1435. return 0;
  1436. clk_disable_unprepare(jpu->clk);
  1437. return 0;
  1438. }
  1439. static int jpu_resume(struct device *dev)
  1440. {
  1441. struct jpu *jpu = dev_get_drvdata(dev);
  1442. if (jpu->ref_count == 0)
  1443. return 0;
  1444. clk_prepare_enable(jpu->clk);
  1445. return 0;
  1446. }
  1447. #endif
  1448. static const struct dev_pm_ops jpu_pm_ops = {
  1449. SET_SYSTEM_SLEEP_PM_OPS(jpu_suspend, jpu_resume)
  1450. };
  1451. static struct platform_driver jpu_driver = {
  1452. .probe = jpu_probe,
  1453. .remove = jpu_remove,
  1454. .driver = {
  1455. .of_match_table = jpu_dt_ids,
  1456. .name = DRV_NAME,
  1457. .pm = &jpu_pm_ops,
  1458. },
  1459. };
  1460. module_platform_driver(jpu_driver);
  1461. MODULE_ALIAS("platform:" DRV_NAME);
  1462. MODULE_AUTHOR("Mikhail Ulianov <mikhail.ulyanov@cogentembedded.com>");
  1463. MODULE_DESCRIPTION("Renesas R-Car JPEG processing unit driver");
  1464. MODULE_LICENSE("GPL v2");