coda-common.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875
  1. /*
  2. * Coda multi-standard codec IP
  3. *
  4. * Copyright (C) 2012 Vista Silicon S.L.
  5. * Javier Martin, <javier.martin@vista-silicon.com>
  6. * Xavier Duret
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/firmware.h>
  17. #include <linux/gcd.h>
  18. #include <linux/genalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/irq.h>
  22. #include <linux/kfifo.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/slab.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/of.h>
  30. #include <linux/platform_data/media/coda.h>
  31. #include <linux/reset.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-event.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-mem2mem.h>
  37. #include <media/videobuf2-v4l2.h>
  38. #include <media/videobuf2-dma-contig.h>
  39. #include <media/videobuf2-vmalloc.h>
  40. #include "coda.h"
  41. #include "imx-vdoa.h"
  42. #define CODA_NAME "coda"
  43. #define CODADX6_MAX_INSTANCES 4
  44. #define CODA_MAX_FORMATS 4
  45. #define CODA_ISRAM_SIZE (2048 * 2)
  46. #define MIN_W 176
  47. #define MIN_H 144
  48. #define S_ALIGN 1 /* multiple of 2 */
  49. #define W_ALIGN 1 /* multiple of 2 */
  50. #define H_ALIGN 1 /* multiple of 2 */
  51. #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
  52. int coda_debug;
  53. module_param(coda_debug, int, 0644);
  54. MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
  55. static int disable_tiling;
  56. module_param(disable_tiling, int, 0644);
  57. MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
  58. static int disable_vdoa;
  59. module_param(disable_vdoa, int, 0644);
  60. MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
  61. static int enable_bwb = 0;
  62. module_param(enable_bwb, int, 0644);
  63. MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
  64. void coda_write(struct coda_dev *dev, u32 data, u32 reg)
  65. {
  66. v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
  67. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  68. writel(data, dev->regs_base + reg);
  69. }
  70. unsigned int coda_read(struct coda_dev *dev, u32 reg)
  71. {
  72. u32 data;
  73. data = readl(dev->regs_base + reg);
  74. v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
  75. "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
  76. return data;
  77. }
  78. void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
  79. struct vb2_v4l2_buffer *buf, unsigned int reg_y)
  80. {
  81. u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
  82. u32 base_cb, base_cr;
  83. switch (q_data->fourcc) {
  84. case V4L2_PIX_FMT_YUYV:
  85. /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
  86. case V4L2_PIX_FMT_NV12:
  87. case V4L2_PIX_FMT_YUV420:
  88. default:
  89. base_cb = base_y + q_data->bytesperline * q_data->height;
  90. base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
  91. break;
  92. case V4L2_PIX_FMT_YVU420:
  93. /* Switch Cb and Cr for YVU420 format */
  94. base_cr = base_y + q_data->bytesperline * q_data->height;
  95. base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
  96. break;
  97. case V4L2_PIX_FMT_YUV422P:
  98. base_cb = base_y + q_data->bytesperline * q_data->height;
  99. base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
  100. }
  101. coda_write(ctx->dev, base_y, reg_y);
  102. coda_write(ctx->dev, base_cb, reg_y + 4);
  103. coda_write(ctx->dev, base_cr, reg_y + 8);
  104. }
  105. #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
  106. { mode, src_fourcc, dst_fourcc, max_w, max_h }
  107. /*
  108. * Arrays of codecs supported by each given version of Coda:
  109. * i.MX27 -> codadx6
  110. * i.MX51 -> codahx4
  111. * i.MX53 -> coda7
  112. * i.MX6 -> coda960
  113. * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
  114. */
  115. static const struct coda_codec codadx6_codecs[] = {
  116. CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  117. CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
  118. };
  119. static const struct coda_codec codahx4_codecs[] = {
  120. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
  121. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  122. CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  123. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
  124. };
  125. static const struct coda_codec coda7_codecs[] = {
  126. CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
  127. CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
  128. CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
  129. CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  130. CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  131. CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  132. CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
  133. };
  134. static const struct coda_codec coda9_codecs[] = {
  135. CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
  136. CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
  137. CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
  138. CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
  139. CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
  140. };
  141. struct coda_video_device {
  142. const char *name;
  143. enum coda_inst_type type;
  144. const struct coda_context_ops *ops;
  145. bool direct;
  146. u32 src_formats[CODA_MAX_FORMATS];
  147. u32 dst_formats[CODA_MAX_FORMATS];
  148. };
  149. static const struct coda_video_device coda_bit_encoder = {
  150. .name = "coda-encoder",
  151. .type = CODA_INST_ENCODER,
  152. .ops = &coda_bit_encode_ops,
  153. .src_formats = {
  154. V4L2_PIX_FMT_NV12,
  155. V4L2_PIX_FMT_YUV420,
  156. V4L2_PIX_FMT_YVU420,
  157. },
  158. .dst_formats = {
  159. V4L2_PIX_FMT_H264,
  160. V4L2_PIX_FMT_MPEG4,
  161. },
  162. };
  163. static const struct coda_video_device coda_bit_jpeg_encoder = {
  164. .name = "coda-jpeg-encoder",
  165. .type = CODA_INST_ENCODER,
  166. .ops = &coda_bit_encode_ops,
  167. .src_formats = {
  168. V4L2_PIX_FMT_NV12,
  169. V4L2_PIX_FMT_YUV420,
  170. V4L2_PIX_FMT_YVU420,
  171. V4L2_PIX_FMT_YUV422P,
  172. },
  173. .dst_formats = {
  174. V4L2_PIX_FMT_JPEG,
  175. },
  176. };
  177. static const struct coda_video_device coda_bit_decoder = {
  178. .name = "coda-decoder",
  179. .type = CODA_INST_DECODER,
  180. .ops = &coda_bit_decode_ops,
  181. .src_formats = {
  182. V4L2_PIX_FMT_H264,
  183. V4L2_PIX_FMT_MPEG2,
  184. V4L2_PIX_FMT_MPEG4,
  185. },
  186. .dst_formats = {
  187. V4L2_PIX_FMT_NV12,
  188. V4L2_PIX_FMT_YUV420,
  189. V4L2_PIX_FMT_YVU420,
  190. /*
  191. * If V4L2_PIX_FMT_YUYV should be default,
  192. * set_default_params() must be adjusted.
  193. */
  194. V4L2_PIX_FMT_YUYV,
  195. },
  196. };
  197. static const struct coda_video_device coda_bit_jpeg_decoder = {
  198. .name = "coda-jpeg-decoder",
  199. .type = CODA_INST_DECODER,
  200. .ops = &coda_bit_decode_ops,
  201. .src_formats = {
  202. V4L2_PIX_FMT_JPEG,
  203. },
  204. .dst_formats = {
  205. V4L2_PIX_FMT_NV12,
  206. V4L2_PIX_FMT_YUV420,
  207. V4L2_PIX_FMT_YVU420,
  208. V4L2_PIX_FMT_YUV422P,
  209. },
  210. };
  211. static const struct coda_video_device *codadx6_video_devices[] = {
  212. &coda_bit_encoder,
  213. };
  214. static const struct coda_video_device *codahx4_video_devices[] = {
  215. &coda_bit_encoder,
  216. &coda_bit_decoder,
  217. };
  218. static const struct coda_video_device *coda7_video_devices[] = {
  219. &coda_bit_jpeg_encoder,
  220. &coda_bit_jpeg_decoder,
  221. &coda_bit_encoder,
  222. &coda_bit_decoder,
  223. };
  224. static const struct coda_video_device *coda9_video_devices[] = {
  225. &coda_bit_encoder,
  226. &coda_bit_decoder,
  227. };
  228. /*
  229. * Normalize all supported YUV 4:2:0 formats to the value used in the codec
  230. * tables.
  231. */
  232. static u32 coda_format_normalize_yuv(u32 fourcc)
  233. {
  234. switch (fourcc) {
  235. case V4L2_PIX_FMT_NV12:
  236. case V4L2_PIX_FMT_YUV420:
  237. case V4L2_PIX_FMT_YVU420:
  238. case V4L2_PIX_FMT_YUV422P:
  239. case V4L2_PIX_FMT_YUYV:
  240. return V4L2_PIX_FMT_YUV420;
  241. default:
  242. return fourcc;
  243. }
  244. }
  245. static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
  246. int src_fourcc, int dst_fourcc)
  247. {
  248. const struct coda_codec *codecs = dev->devtype->codecs;
  249. int num_codecs = dev->devtype->num_codecs;
  250. int k;
  251. src_fourcc = coda_format_normalize_yuv(src_fourcc);
  252. dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
  253. if (src_fourcc == dst_fourcc)
  254. return NULL;
  255. for (k = 0; k < num_codecs; k++) {
  256. if (codecs[k].src_fourcc == src_fourcc &&
  257. codecs[k].dst_fourcc == dst_fourcc)
  258. break;
  259. }
  260. if (k == num_codecs)
  261. return NULL;
  262. return &codecs[k];
  263. }
  264. static void coda_get_max_dimensions(struct coda_dev *dev,
  265. const struct coda_codec *codec,
  266. int *max_w, int *max_h)
  267. {
  268. const struct coda_codec *codecs = dev->devtype->codecs;
  269. int num_codecs = dev->devtype->num_codecs;
  270. unsigned int w, h;
  271. int k;
  272. if (codec) {
  273. w = codec->max_w;
  274. h = codec->max_h;
  275. } else {
  276. for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
  277. w = max(w, codecs[k].max_w);
  278. h = max(h, codecs[k].max_h);
  279. }
  280. }
  281. if (max_w)
  282. *max_w = w;
  283. if (max_h)
  284. *max_h = h;
  285. }
  286. static const struct coda_video_device *to_coda_video_device(struct video_device
  287. *vdev)
  288. {
  289. struct coda_dev *dev = video_get_drvdata(vdev);
  290. unsigned int i = vdev - dev->vfd;
  291. if (i >= dev->devtype->num_vdevs)
  292. return NULL;
  293. return dev->devtype->vdevs[i];
  294. }
  295. const char *coda_product_name(int product)
  296. {
  297. static char buf[9];
  298. switch (product) {
  299. case CODA_DX6:
  300. return "CodaDx6";
  301. case CODA_HX4:
  302. return "CodaHx4";
  303. case CODA_7541:
  304. return "CODA7541";
  305. case CODA_960:
  306. return "CODA960";
  307. default:
  308. snprintf(buf, sizeof(buf), "(0x%04x)", product);
  309. return buf;
  310. }
  311. }
  312. static struct vdoa_data *coda_get_vdoa_data(void)
  313. {
  314. struct device_node *vdoa_node;
  315. struct platform_device *vdoa_pdev;
  316. struct vdoa_data *vdoa_data = NULL;
  317. vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
  318. if (!vdoa_node)
  319. return NULL;
  320. vdoa_pdev = of_find_device_by_node(vdoa_node);
  321. if (!vdoa_pdev)
  322. goto out;
  323. vdoa_data = platform_get_drvdata(vdoa_pdev);
  324. if (!vdoa_data)
  325. vdoa_data = ERR_PTR(-EPROBE_DEFER);
  326. out:
  327. if (vdoa_node)
  328. of_node_put(vdoa_node);
  329. return vdoa_data;
  330. }
  331. /*
  332. * V4L2 ioctl() operations.
  333. */
  334. static int coda_querycap(struct file *file, void *priv,
  335. struct v4l2_capability *cap)
  336. {
  337. struct coda_ctx *ctx = fh_to_ctx(priv);
  338. strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
  339. strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
  340. sizeof(cap->card));
  341. strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
  342. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  343. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  344. return 0;
  345. }
  346. static int coda_enum_fmt(struct file *file, void *priv,
  347. struct v4l2_fmtdesc *f)
  348. {
  349. struct video_device *vdev = video_devdata(file);
  350. const struct coda_video_device *cvd = to_coda_video_device(vdev);
  351. struct coda_ctx *ctx = fh_to_ctx(priv);
  352. const u32 *formats;
  353. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  354. formats = cvd->src_formats;
  355. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  356. formats = cvd->dst_formats;
  357. else
  358. return -EINVAL;
  359. if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
  360. return -EINVAL;
  361. /* Skip YUYV if the vdoa is not available */
  362. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  363. formats[f->index] == V4L2_PIX_FMT_YUYV)
  364. return -EINVAL;
  365. f->pixelformat = formats[f->index];
  366. return 0;
  367. }
  368. static int coda_g_fmt(struct file *file, void *priv,
  369. struct v4l2_format *f)
  370. {
  371. struct coda_q_data *q_data;
  372. struct coda_ctx *ctx = fh_to_ctx(priv);
  373. q_data = get_q_data(ctx, f->type);
  374. if (!q_data)
  375. return -EINVAL;
  376. f->fmt.pix.field = V4L2_FIELD_NONE;
  377. f->fmt.pix.pixelformat = q_data->fourcc;
  378. f->fmt.pix.width = q_data->width;
  379. f->fmt.pix.height = q_data->height;
  380. f->fmt.pix.bytesperline = q_data->bytesperline;
  381. f->fmt.pix.sizeimage = q_data->sizeimage;
  382. f->fmt.pix.colorspace = ctx->colorspace;
  383. f->fmt.pix.xfer_func = ctx->xfer_func;
  384. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  385. f->fmt.pix.quantization = ctx->quantization;
  386. return 0;
  387. }
  388. static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
  389. {
  390. struct coda_q_data *q_data;
  391. const u32 *formats;
  392. int i;
  393. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  394. formats = ctx->cvd->src_formats;
  395. else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  396. formats = ctx->cvd->dst_formats;
  397. else
  398. return -EINVAL;
  399. for (i = 0; i < CODA_MAX_FORMATS; i++) {
  400. /* Skip YUYV if the vdoa is not available */
  401. if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  402. formats[i] == V4L2_PIX_FMT_YUYV)
  403. continue;
  404. if (formats[i] == f->fmt.pix.pixelformat) {
  405. f->fmt.pix.pixelformat = formats[i];
  406. return 0;
  407. }
  408. }
  409. /* Fall back to currently set pixelformat */
  410. q_data = get_q_data(ctx, f->type);
  411. f->fmt.pix.pixelformat = q_data->fourcc;
  412. return 0;
  413. }
  414. static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
  415. bool *use_vdoa)
  416. {
  417. int err;
  418. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  419. return -EINVAL;
  420. if (!use_vdoa)
  421. return -EINVAL;
  422. if (!ctx->vdoa) {
  423. *use_vdoa = false;
  424. return 0;
  425. }
  426. err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
  427. f->fmt.pix.height, f->fmt.pix.pixelformat);
  428. if (err) {
  429. *use_vdoa = false;
  430. return 0;
  431. }
  432. *use_vdoa = true;
  433. return 0;
  434. }
  435. static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
  436. u32 width, u32 height)
  437. {
  438. /*
  439. * This is a rough estimate for sensible compressed buffer
  440. * sizes (between 1 and 16 bits per pixel). This could be
  441. * improved by better format specific worst case estimates.
  442. */
  443. return round_up(clamp(sizeimage, width * height / 8,
  444. width * height * 2), PAGE_SIZE);
  445. }
  446. static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
  447. struct v4l2_format *f)
  448. {
  449. struct coda_dev *dev = ctx->dev;
  450. unsigned int max_w, max_h;
  451. enum v4l2_field field;
  452. field = f->fmt.pix.field;
  453. if (field == V4L2_FIELD_ANY)
  454. field = V4L2_FIELD_NONE;
  455. else if (V4L2_FIELD_NONE != field)
  456. return -EINVAL;
  457. /* V4L2 specification suggests the driver corrects the format struct
  458. * if any of the dimensions is unsupported */
  459. f->fmt.pix.field = field;
  460. coda_get_max_dimensions(dev, codec, &max_w, &max_h);
  461. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
  462. &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
  463. S_ALIGN);
  464. switch (f->fmt.pix.pixelformat) {
  465. case V4L2_PIX_FMT_NV12:
  466. case V4L2_PIX_FMT_YUV420:
  467. case V4L2_PIX_FMT_YVU420:
  468. /*
  469. * Frame stride must be at least multiple of 8,
  470. * but multiple of 16 for h.264 or JPEG 4:2:x
  471. */
  472. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  473. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  474. f->fmt.pix.height * 3 / 2;
  475. break;
  476. case V4L2_PIX_FMT_YUYV:
  477. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  478. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  479. f->fmt.pix.height;
  480. break;
  481. case V4L2_PIX_FMT_YUV422P:
  482. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  483. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  484. f->fmt.pix.height * 2;
  485. break;
  486. case V4L2_PIX_FMT_JPEG:
  487. case V4L2_PIX_FMT_H264:
  488. case V4L2_PIX_FMT_MPEG4:
  489. case V4L2_PIX_FMT_MPEG2:
  490. f->fmt.pix.bytesperline = 0;
  491. f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
  492. f->fmt.pix.sizeimage,
  493. f->fmt.pix.width,
  494. f->fmt.pix.height);
  495. break;
  496. default:
  497. BUG();
  498. }
  499. return 0;
  500. }
  501. static int coda_try_fmt_vid_cap(struct file *file, void *priv,
  502. struct v4l2_format *f)
  503. {
  504. struct coda_ctx *ctx = fh_to_ctx(priv);
  505. const struct coda_q_data *q_data_src;
  506. const struct coda_codec *codec;
  507. struct vb2_queue *src_vq;
  508. int ret;
  509. bool use_vdoa;
  510. ret = coda_try_pixelformat(ctx, f);
  511. if (ret < 0)
  512. return ret;
  513. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  514. /*
  515. * If the source format is already fixed, only allow the same output
  516. * resolution
  517. */
  518. src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  519. if (vb2_is_streaming(src_vq)) {
  520. f->fmt.pix.width = q_data_src->width;
  521. f->fmt.pix.height = q_data_src->height;
  522. }
  523. f->fmt.pix.colorspace = ctx->colorspace;
  524. f->fmt.pix.xfer_func = ctx->xfer_func;
  525. f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
  526. f->fmt.pix.quantization = ctx->quantization;
  527. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  528. codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  529. f->fmt.pix.pixelformat);
  530. if (!codec)
  531. return -EINVAL;
  532. ret = coda_try_fmt(ctx, codec, f);
  533. if (ret < 0)
  534. return ret;
  535. /* The h.264 decoder only returns complete 16x16 macroblocks */
  536. if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
  537. f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
  538. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
  539. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  540. f->fmt.pix.height * 3 / 2;
  541. ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
  542. if (ret < 0)
  543. return ret;
  544. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
  545. if (!use_vdoa)
  546. return -EINVAL;
  547. f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
  548. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
  549. f->fmt.pix.height;
  550. }
  551. }
  552. return 0;
  553. }
  554. static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
  555. {
  556. enum v4l2_colorspace colorspace;
  557. if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
  558. colorspace = V4L2_COLORSPACE_JPEG;
  559. else if (fmt->width <= 720 && fmt->height <= 576)
  560. colorspace = V4L2_COLORSPACE_SMPTE170M;
  561. else
  562. colorspace = V4L2_COLORSPACE_REC709;
  563. fmt->colorspace = colorspace;
  564. fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  565. fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  566. fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  567. }
  568. static int coda_try_fmt_vid_out(struct file *file, void *priv,
  569. struct v4l2_format *f)
  570. {
  571. struct coda_ctx *ctx = fh_to_ctx(priv);
  572. struct coda_dev *dev = ctx->dev;
  573. const struct coda_q_data *q_data_dst;
  574. const struct coda_codec *codec;
  575. int ret;
  576. ret = coda_try_pixelformat(ctx, f);
  577. if (ret < 0)
  578. return ret;
  579. if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
  580. coda_set_default_colorspace(&f->fmt.pix);
  581. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  582. codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
  583. return coda_try_fmt(ctx, codec, f);
  584. }
  585. static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
  586. struct v4l2_rect *r)
  587. {
  588. struct coda_q_data *q_data;
  589. struct vb2_queue *vq;
  590. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  591. if (!vq)
  592. return -EINVAL;
  593. q_data = get_q_data(ctx, f->type);
  594. if (!q_data)
  595. return -EINVAL;
  596. if (vb2_is_busy(vq)) {
  597. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  598. return -EBUSY;
  599. }
  600. q_data->fourcc = f->fmt.pix.pixelformat;
  601. q_data->width = f->fmt.pix.width;
  602. q_data->height = f->fmt.pix.height;
  603. q_data->bytesperline = f->fmt.pix.bytesperline;
  604. q_data->sizeimage = f->fmt.pix.sizeimage;
  605. if (r) {
  606. q_data->rect = *r;
  607. } else {
  608. q_data->rect.left = 0;
  609. q_data->rect.top = 0;
  610. q_data->rect.width = f->fmt.pix.width;
  611. q_data->rect.height = f->fmt.pix.height;
  612. }
  613. switch (f->fmt.pix.pixelformat) {
  614. case V4L2_PIX_FMT_YUYV:
  615. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  616. break;
  617. case V4L2_PIX_FMT_NV12:
  618. if (!disable_tiling) {
  619. ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
  620. break;
  621. }
  622. /* else fall through */
  623. case V4L2_PIX_FMT_YUV420:
  624. case V4L2_PIX_FMT_YVU420:
  625. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  626. break;
  627. default:
  628. break;
  629. }
  630. if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
  631. !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
  632. ctx->use_vdoa)
  633. vdoa_context_configure(ctx->vdoa,
  634. round_up(f->fmt.pix.width, 16),
  635. f->fmt.pix.height,
  636. f->fmt.pix.pixelformat);
  637. else
  638. ctx->use_vdoa = false;
  639. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  640. "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
  641. f->type, q_data->width, q_data->height,
  642. (char *)&q_data->fourcc,
  643. (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
  644. return 0;
  645. }
  646. static int coda_s_fmt_vid_cap(struct file *file, void *priv,
  647. struct v4l2_format *f)
  648. {
  649. struct coda_ctx *ctx = fh_to_ctx(priv);
  650. struct coda_q_data *q_data_src;
  651. struct v4l2_rect r;
  652. int ret;
  653. ret = coda_try_fmt_vid_cap(file, priv, f);
  654. if (ret)
  655. return ret;
  656. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  657. r.left = 0;
  658. r.top = 0;
  659. r.width = q_data_src->width;
  660. r.height = q_data_src->height;
  661. ret = coda_s_fmt(ctx, f, &r);
  662. if (ret)
  663. return ret;
  664. if (ctx->inst_type != CODA_INST_ENCODER)
  665. return 0;
  666. ctx->colorspace = f->fmt.pix.colorspace;
  667. ctx->xfer_func = f->fmt.pix.xfer_func;
  668. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  669. ctx->quantization = f->fmt.pix.quantization;
  670. return 0;
  671. }
  672. static int coda_s_fmt_vid_out(struct file *file, void *priv,
  673. struct v4l2_format *f)
  674. {
  675. struct coda_ctx *ctx = fh_to_ctx(priv);
  676. struct v4l2_format f_cap;
  677. struct vb2_queue *dst_vq;
  678. int ret;
  679. ret = coda_try_fmt_vid_out(file, priv, f);
  680. if (ret)
  681. return ret;
  682. ret = coda_s_fmt(ctx, f, NULL);
  683. if (ret)
  684. return ret;
  685. if (ctx->inst_type != CODA_INST_DECODER)
  686. return 0;
  687. ctx->colorspace = f->fmt.pix.colorspace;
  688. ctx->xfer_func = f->fmt.pix.xfer_func;
  689. ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
  690. ctx->quantization = f->fmt.pix.quantization;
  691. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  692. if (!dst_vq)
  693. return -EINVAL;
  694. /*
  695. * Setting the capture queue format is not possible while the capture
  696. * queue is still busy. This is not an error, but the user will have to
  697. * make sure themselves that the capture format is set correctly before
  698. * starting the output queue again.
  699. */
  700. if (vb2_is_busy(dst_vq))
  701. return 0;
  702. memset(&f_cap, 0, sizeof(f_cap));
  703. f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  704. coda_g_fmt(file, priv, &f_cap);
  705. f_cap.fmt.pix.width = f->fmt.pix.width;
  706. f_cap.fmt.pix.height = f->fmt.pix.height;
  707. return coda_s_fmt_vid_cap(file, priv, &f_cap);
  708. }
  709. static int coda_reqbufs(struct file *file, void *priv,
  710. struct v4l2_requestbuffers *rb)
  711. {
  712. struct coda_ctx *ctx = fh_to_ctx(priv);
  713. int ret;
  714. ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
  715. if (ret)
  716. return ret;
  717. /*
  718. * Allow to allocate instance specific per-context buffers, such as
  719. * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
  720. */
  721. if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
  722. return ctx->ops->reqbufs(ctx, rb);
  723. return 0;
  724. }
  725. static int coda_qbuf(struct file *file, void *priv,
  726. struct v4l2_buffer *buf)
  727. {
  728. struct coda_ctx *ctx = fh_to_ctx(priv);
  729. return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  730. }
  731. static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
  732. struct vb2_v4l2_buffer *buf)
  733. {
  734. return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
  735. (buf->sequence == (ctx->qsequence - 1)));
  736. }
  737. void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
  738. enum vb2_buffer_state state)
  739. {
  740. const struct v4l2_event eos_event = {
  741. .type = V4L2_EVENT_EOS
  742. };
  743. if (coda_buf_is_end_of_stream(ctx, buf)) {
  744. buf->flags |= V4L2_BUF_FLAG_LAST;
  745. v4l2_event_queue_fh(&ctx->fh, &eos_event);
  746. }
  747. v4l2_m2m_buf_done(buf, state);
  748. }
  749. static int coda_g_selection(struct file *file, void *fh,
  750. struct v4l2_selection *s)
  751. {
  752. struct coda_ctx *ctx = fh_to_ctx(fh);
  753. struct coda_q_data *q_data;
  754. struct v4l2_rect r, *rsel;
  755. q_data = get_q_data(ctx, s->type);
  756. if (!q_data)
  757. return -EINVAL;
  758. r.left = 0;
  759. r.top = 0;
  760. r.width = q_data->width;
  761. r.height = q_data->height;
  762. rsel = &q_data->rect;
  763. switch (s->target) {
  764. case V4L2_SEL_TGT_CROP_DEFAULT:
  765. case V4L2_SEL_TGT_CROP_BOUNDS:
  766. rsel = &r;
  767. /* fallthrough */
  768. case V4L2_SEL_TGT_CROP:
  769. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  770. return -EINVAL;
  771. break;
  772. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  773. case V4L2_SEL_TGT_COMPOSE_PADDED:
  774. rsel = &r;
  775. /* fallthrough */
  776. case V4L2_SEL_TGT_COMPOSE:
  777. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  778. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  779. return -EINVAL;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. s->r = *rsel;
  785. return 0;
  786. }
  787. static int coda_s_selection(struct file *file, void *fh,
  788. struct v4l2_selection *s)
  789. {
  790. struct coda_ctx *ctx = fh_to_ctx(fh);
  791. struct coda_q_data *q_data;
  792. if (ctx->inst_type == CODA_INST_ENCODER &&
  793. s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  794. s->target == V4L2_SEL_TGT_CROP) {
  795. q_data = get_q_data(ctx, s->type);
  796. if (!q_data)
  797. return -EINVAL;
  798. s->r.left = 0;
  799. s->r.top = 0;
  800. s->r.width = clamp(s->r.width, 2U, q_data->width);
  801. s->r.height = clamp(s->r.height, 2U, q_data->height);
  802. if (s->flags & V4L2_SEL_FLAG_LE) {
  803. s->r.width = round_up(s->r.width, 2);
  804. s->r.height = round_up(s->r.height, 2);
  805. } else {
  806. s->r.width = round_down(s->r.width, 2);
  807. s->r.height = round_down(s->r.height, 2);
  808. }
  809. q_data->rect = s->r;
  810. return 0;
  811. }
  812. return coda_g_selection(file, fh, s);
  813. }
  814. static int coda_try_encoder_cmd(struct file *file, void *fh,
  815. struct v4l2_encoder_cmd *ec)
  816. {
  817. if (ec->cmd != V4L2_ENC_CMD_STOP)
  818. return -EINVAL;
  819. if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
  820. return -EINVAL;
  821. return 0;
  822. }
  823. static int coda_encoder_cmd(struct file *file, void *fh,
  824. struct v4l2_encoder_cmd *ec)
  825. {
  826. struct coda_ctx *ctx = fh_to_ctx(fh);
  827. struct vb2_queue *dst_vq;
  828. int ret;
  829. ret = coda_try_encoder_cmd(file, fh, ec);
  830. if (ret < 0)
  831. return ret;
  832. /* Ignore encoder stop command silently in decoder context */
  833. if (ctx->inst_type != CODA_INST_ENCODER)
  834. return 0;
  835. /* Set the stream-end flag on this context */
  836. ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
  837. flush_work(&ctx->pic_run_work);
  838. /* If there is no buffer in flight, wake up */
  839. if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
  840. dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  841. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  842. dst_vq->last_buffer_dequeued = true;
  843. wake_up(&dst_vq->done_wq);
  844. }
  845. return 0;
  846. }
  847. static int coda_try_decoder_cmd(struct file *file, void *fh,
  848. struct v4l2_decoder_cmd *dc)
  849. {
  850. if (dc->cmd != V4L2_DEC_CMD_STOP)
  851. return -EINVAL;
  852. if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
  853. return -EINVAL;
  854. if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
  855. return -EINVAL;
  856. return 0;
  857. }
  858. static int coda_decoder_cmd(struct file *file, void *fh,
  859. struct v4l2_decoder_cmd *dc)
  860. {
  861. struct coda_ctx *ctx = fh_to_ctx(fh);
  862. int ret;
  863. ret = coda_try_decoder_cmd(file, fh, dc);
  864. if (ret < 0)
  865. return ret;
  866. /* Ignore decoder stop command silently in encoder context */
  867. if (ctx->inst_type != CODA_INST_DECODER)
  868. return 0;
  869. /* Set the stream-end flag on this context */
  870. coda_bit_stream_end_flag(ctx);
  871. ctx->hold = false;
  872. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  873. return 0;
  874. }
  875. static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  876. {
  877. struct coda_ctx *ctx = fh_to_ctx(fh);
  878. struct v4l2_fract *tpf;
  879. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  880. return -EINVAL;
  881. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  882. tpf = &a->parm.output.timeperframe;
  883. tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
  884. tpf->numerator = 1 + (ctx->params.framerate >>
  885. CODA_FRATE_DIV_OFFSET);
  886. return 0;
  887. }
  888. /*
  889. * Approximate timeperframe v4l2_fract with values that can be written
  890. * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
  891. */
  892. static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
  893. {
  894. struct v4l2_fract s = *timeperframe;
  895. struct v4l2_fract f0;
  896. struct v4l2_fract f1 = { 1, 0 };
  897. struct v4l2_fract f2 = { 0, 1 };
  898. unsigned int i, div, s_denominator;
  899. /* Lower bound is 1/65535 */
  900. if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
  901. timeperframe->numerator = 1;
  902. timeperframe->denominator = 65535;
  903. return;
  904. }
  905. /* Upper bound is 65536/1, map everything above to infinity */
  906. if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
  907. timeperframe->numerator = 1;
  908. timeperframe->denominator = 0;
  909. return;
  910. }
  911. /* Reduce fraction to lowest terms */
  912. div = gcd(s.numerator, s.denominator);
  913. if (div > 1) {
  914. s.numerator /= div;
  915. s.denominator /= div;
  916. }
  917. if (s.numerator <= 65536 && s.denominator < 65536) {
  918. *timeperframe = s;
  919. return;
  920. }
  921. /* Find successive convergents from continued fraction expansion */
  922. while (f2.numerator <= 65536 && f2.denominator < 65536) {
  923. f0 = f1;
  924. f1 = f2;
  925. /* Stop when f2 exactly equals timeperframe */
  926. if (s.numerator == 0)
  927. break;
  928. i = s.denominator / s.numerator;
  929. f2.numerator = f0.numerator + i * f1.numerator;
  930. f2.denominator = f0.denominator + i * f2.denominator;
  931. s_denominator = s.numerator;
  932. s.numerator = s.denominator % s.numerator;
  933. s.denominator = s_denominator;
  934. }
  935. *timeperframe = f1;
  936. }
  937. static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
  938. {
  939. return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
  940. timeperframe->denominator;
  941. }
  942. static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  943. {
  944. struct coda_ctx *ctx = fh_to_ctx(fh);
  945. struct v4l2_fract *tpf;
  946. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  947. return -EINVAL;
  948. tpf = &a->parm.output.timeperframe;
  949. coda_approximate_timeperframe(tpf);
  950. ctx->params.framerate = coda_timeperframe_to_frate(tpf);
  951. return 0;
  952. }
  953. static int coda_subscribe_event(struct v4l2_fh *fh,
  954. const struct v4l2_event_subscription *sub)
  955. {
  956. switch (sub->type) {
  957. case V4L2_EVENT_EOS:
  958. return v4l2_event_subscribe(fh, sub, 0, NULL);
  959. default:
  960. return v4l2_ctrl_subscribe_event(fh, sub);
  961. }
  962. }
  963. static const struct v4l2_ioctl_ops coda_ioctl_ops = {
  964. .vidioc_querycap = coda_querycap,
  965. .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
  966. .vidioc_g_fmt_vid_cap = coda_g_fmt,
  967. .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
  968. .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
  969. .vidioc_enum_fmt_vid_out = coda_enum_fmt,
  970. .vidioc_g_fmt_vid_out = coda_g_fmt,
  971. .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
  972. .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
  973. .vidioc_reqbufs = coda_reqbufs,
  974. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  975. .vidioc_qbuf = coda_qbuf,
  976. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  977. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  978. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  979. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  980. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  981. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  982. .vidioc_g_selection = coda_g_selection,
  983. .vidioc_s_selection = coda_s_selection,
  984. .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
  985. .vidioc_encoder_cmd = coda_encoder_cmd,
  986. .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
  987. .vidioc_decoder_cmd = coda_decoder_cmd,
  988. .vidioc_g_parm = coda_g_parm,
  989. .vidioc_s_parm = coda_s_parm,
  990. .vidioc_subscribe_event = coda_subscribe_event,
  991. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  992. };
  993. /*
  994. * Mem-to-mem operations.
  995. */
  996. static void coda_device_run(void *m2m_priv)
  997. {
  998. struct coda_ctx *ctx = m2m_priv;
  999. struct coda_dev *dev = ctx->dev;
  1000. queue_work(dev->workqueue, &ctx->pic_run_work);
  1001. }
  1002. static void coda_pic_run_work(struct work_struct *work)
  1003. {
  1004. struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
  1005. struct coda_dev *dev = ctx->dev;
  1006. int ret;
  1007. mutex_lock(&ctx->buffer_mutex);
  1008. mutex_lock(&dev->coda_mutex);
  1009. ret = ctx->ops->prepare_run(ctx);
  1010. if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
  1011. mutex_unlock(&dev->coda_mutex);
  1012. mutex_unlock(&ctx->buffer_mutex);
  1013. /* job_finish scheduled by prepare_decode */
  1014. return;
  1015. }
  1016. if (!wait_for_completion_timeout(&ctx->completion,
  1017. msecs_to_jiffies(1000))) {
  1018. dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
  1019. ctx->hold = true;
  1020. coda_hw_reset(ctx);
  1021. if (ctx->ops->run_timeout)
  1022. ctx->ops->run_timeout(ctx);
  1023. } else if (!ctx->aborting) {
  1024. ctx->ops->finish_run(ctx);
  1025. }
  1026. if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
  1027. ctx->ops->seq_end_work)
  1028. queue_work(dev->workqueue, &ctx->seq_end_work);
  1029. mutex_unlock(&dev->coda_mutex);
  1030. mutex_unlock(&ctx->buffer_mutex);
  1031. v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
  1032. }
  1033. static int coda_job_ready(void *m2m_priv)
  1034. {
  1035. struct coda_ctx *ctx = m2m_priv;
  1036. int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
  1037. /*
  1038. * For both 'P' and 'key' frame cases 1 picture
  1039. * and 1 frame are needed. In the decoder case,
  1040. * the compressed frame can be in the bitstream.
  1041. */
  1042. if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
  1043. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1044. "not ready: not enough video buffers.\n");
  1045. return 0;
  1046. }
  1047. if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
  1048. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1049. "not ready: not enough video capture buffers.\n");
  1050. return 0;
  1051. }
  1052. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1053. bool stream_end = ctx->bit_stream_param &
  1054. CODA_BIT_STREAM_END_FLAG;
  1055. int num_metas = ctx->num_metas;
  1056. unsigned int count;
  1057. count = hweight32(ctx->frm_dis_flg);
  1058. if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
  1059. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1060. "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
  1061. ctx->idx, count, ctx->num_internal_frames,
  1062. ctx->frm_dis_flg);
  1063. return 0;
  1064. }
  1065. if (ctx->hold && !src_bufs) {
  1066. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1067. "%d: not ready: on hold for more buffers.\n",
  1068. ctx->idx);
  1069. return 0;
  1070. }
  1071. if (!stream_end && (num_metas + src_bufs) < 2) {
  1072. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1073. "%d: not ready: need 2 buffers available (%d, %d)\n",
  1074. ctx->idx, num_metas, src_bufs);
  1075. return 0;
  1076. }
  1077. if (!src_bufs && !stream_end &&
  1078. (coda_get_bitstream_payload(ctx) < 512)) {
  1079. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1080. "%d: not ready: not enough bitstream data (%d).\n",
  1081. ctx->idx, coda_get_bitstream_payload(ctx));
  1082. return 0;
  1083. }
  1084. }
  1085. if (ctx->aborting) {
  1086. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1087. "not ready: aborting\n");
  1088. return 0;
  1089. }
  1090. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1091. "job ready\n");
  1092. return 1;
  1093. }
  1094. static void coda_job_abort(void *priv)
  1095. {
  1096. struct coda_ctx *ctx = priv;
  1097. ctx->aborting = 1;
  1098. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1099. "Aborting task\n");
  1100. }
  1101. static const struct v4l2_m2m_ops coda_m2m_ops = {
  1102. .device_run = coda_device_run,
  1103. .job_ready = coda_job_ready,
  1104. .job_abort = coda_job_abort,
  1105. };
  1106. static void set_default_params(struct coda_ctx *ctx)
  1107. {
  1108. unsigned int max_w, max_h, usize, csize;
  1109. ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
  1110. ctx->cvd->dst_formats[0]);
  1111. max_w = min(ctx->codec->max_w, 1920U);
  1112. max_h = min(ctx->codec->max_h, 1088U);
  1113. usize = max_w * max_h * 3 / 2;
  1114. csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
  1115. ctx->params.codec_mode = ctx->codec->mode;
  1116. if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
  1117. ctx->colorspace = V4L2_COLORSPACE_JPEG;
  1118. else
  1119. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1120. ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1121. ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1122. ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
  1123. ctx->params.framerate = 30;
  1124. /* Default formats for output and input queues */
  1125. ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
  1126. ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
  1127. ctx->q_data[V4L2_M2M_SRC].width = max_w;
  1128. ctx->q_data[V4L2_M2M_SRC].height = max_h;
  1129. ctx->q_data[V4L2_M2M_DST].width = max_w;
  1130. ctx->q_data[V4L2_M2M_DST].height = max_h;
  1131. if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
  1132. ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
  1133. ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
  1134. ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
  1135. ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
  1136. } else {
  1137. ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
  1138. ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
  1139. ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
  1140. ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
  1141. }
  1142. ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
  1143. ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
  1144. ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
  1145. ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
  1146. /*
  1147. * Since the RBC2AXI logic only supports a single chroma plane,
  1148. * macroblock tiling only works for to NV12 pixel format.
  1149. */
  1150. ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
  1151. }
  1152. /*
  1153. * Queue operations
  1154. */
  1155. static int coda_queue_setup(struct vb2_queue *vq,
  1156. unsigned int *nbuffers, unsigned int *nplanes,
  1157. unsigned int sizes[], struct device *alloc_devs[])
  1158. {
  1159. struct coda_ctx *ctx = vb2_get_drv_priv(vq);
  1160. struct coda_q_data *q_data;
  1161. unsigned int size;
  1162. q_data = get_q_data(ctx, vq->type);
  1163. size = q_data->sizeimage;
  1164. *nplanes = 1;
  1165. sizes[0] = size;
  1166. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1167. "get %d buffer(s) of size %d each.\n", *nbuffers, size);
  1168. return 0;
  1169. }
  1170. static int coda_buf_prepare(struct vb2_buffer *vb)
  1171. {
  1172. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1173. struct coda_q_data *q_data;
  1174. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1175. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  1176. v4l2_warn(&ctx->dev->v4l2_dev,
  1177. "%s data will not fit into plane (%lu < %lu)\n",
  1178. __func__, vb2_plane_size(vb, 0),
  1179. (long)q_data->sizeimage);
  1180. return -EINVAL;
  1181. }
  1182. return 0;
  1183. }
  1184. static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
  1185. {
  1186. if (!ctrl)
  1187. return;
  1188. v4l2_ctrl_lock(ctrl);
  1189. /*
  1190. * Extend the control range if the parsed stream contains a known but
  1191. * unsupported value or level.
  1192. */
  1193. if (value > ctrl->maximum) {
  1194. __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
  1195. ctrl->menu_skip_mask & ~(1 << value),
  1196. ctrl->default_value);
  1197. } else if (value < ctrl->minimum) {
  1198. __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
  1199. ctrl->menu_skip_mask & ~(1 << value),
  1200. ctrl->default_value);
  1201. }
  1202. __v4l2_ctrl_s_ctrl(ctrl, value);
  1203. v4l2_ctrl_unlock(ctrl);
  1204. }
  1205. static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx)
  1206. {
  1207. const char * const *profile_names;
  1208. int profile;
  1209. profile = coda_h264_profile(ctx->params.h264_profile_idc);
  1210. if (profile < 0) {
  1211. v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n",
  1212. ctx->params.h264_profile_idc);
  1213. return;
  1214. }
  1215. coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile);
  1216. profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
  1217. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Profile: %s\n",
  1218. profile_names[profile]);
  1219. }
  1220. static void coda_update_h264_level_ctrl(struct coda_ctx *ctx)
  1221. {
  1222. const char * const *level_names;
  1223. int level;
  1224. level = coda_h264_level(ctx->params.h264_level_idc);
  1225. if (level < 0) {
  1226. v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n",
  1227. ctx->params.h264_level_idc);
  1228. return;
  1229. }
  1230. coda_update_menu_ctrl(ctx->h264_level_ctrl, level);
  1231. level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
  1232. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Level: %s\n",
  1233. level_names[level]);
  1234. }
  1235. static void coda_buf_queue(struct vb2_buffer *vb)
  1236. {
  1237. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1238. struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1239. struct vb2_queue *vq = vb->vb2_queue;
  1240. struct coda_q_data *q_data;
  1241. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1242. /*
  1243. * In the decoder case, immediately try to copy the buffer into the
  1244. * bitstream ringbuffer and mark it as ready to be dequeued.
  1245. */
  1246. if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1247. /*
  1248. * For backwards compatibility, queuing an empty buffer marks
  1249. * the stream end
  1250. */
  1251. if (vb2_get_plane_payload(vb, 0) == 0)
  1252. coda_bit_stream_end_flag(ctx);
  1253. if (q_data->fourcc == V4L2_PIX_FMT_H264) {
  1254. /*
  1255. * Unless already done, try to obtain profile_idc and
  1256. * level_idc from the SPS header. This allows to decide
  1257. * whether to enable reordering during sequence
  1258. * initialization.
  1259. */
  1260. if (!ctx->params.h264_profile_idc) {
  1261. coda_sps_parse_profile(ctx, vb);
  1262. coda_update_h264_profile_ctrl(ctx);
  1263. coda_update_h264_level_ctrl(ctx);
  1264. }
  1265. }
  1266. mutex_lock(&ctx->bitstream_mutex);
  1267. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1268. if (vb2_is_streaming(vb->vb2_queue))
  1269. /* This set buf->sequence = ctx->qsequence++ */
  1270. coda_fill_bitstream(ctx, NULL);
  1271. mutex_unlock(&ctx->bitstream_mutex);
  1272. } else {
  1273. if (ctx->inst_type == CODA_INST_ENCODER &&
  1274. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1275. vbuf->sequence = ctx->qsequence++;
  1276. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1277. }
  1278. }
  1279. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  1280. size_t size, const char *name, struct dentry *parent)
  1281. {
  1282. buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
  1283. GFP_KERNEL);
  1284. if (!buf->vaddr) {
  1285. v4l2_err(&dev->v4l2_dev,
  1286. "Failed to allocate %s buffer of size %zu\n",
  1287. name, size);
  1288. return -ENOMEM;
  1289. }
  1290. buf->size = size;
  1291. if (name && parent) {
  1292. buf->blob.data = buf->vaddr;
  1293. buf->blob.size = size;
  1294. buf->dentry = debugfs_create_blob(name, 0644, parent,
  1295. &buf->blob);
  1296. if (!buf->dentry)
  1297. dev_warn(&dev->plat_dev->dev,
  1298. "failed to create debugfs entry %s\n", name);
  1299. }
  1300. return 0;
  1301. }
  1302. void coda_free_aux_buf(struct coda_dev *dev,
  1303. struct coda_aux_buf *buf)
  1304. {
  1305. if (buf->vaddr) {
  1306. dma_free_coherent(&dev->plat_dev->dev, buf->size,
  1307. buf->vaddr, buf->paddr);
  1308. buf->vaddr = NULL;
  1309. buf->size = 0;
  1310. debugfs_remove(buf->dentry);
  1311. buf->dentry = NULL;
  1312. }
  1313. }
  1314. static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
  1315. {
  1316. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1317. struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
  1318. struct coda_q_data *q_data_src, *q_data_dst;
  1319. struct v4l2_m2m_buffer *m2m_buf, *tmp;
  1320. struct vb2_v4l2_buffer *buf;
  1321. struct list_head list;
  1322. int ret = 0;
  1323. if (count < 1)
  1324. return -EINVAL;
  1325. INIT_LIST_HEAD(&list);
  1326. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1327. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1328. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
  1329. /* copy the buffers that were queued before streamon */
  1330. mutex_lock(&ctx->bitstream_mutex);
  1331. coda_fill_bitstream(ctx, &list);
  1332. mutex_unlock(&ctx->bitstream_mutex);
  1333. if (coda_get_bitstream_payload(ctx) < 512) {
  1334. ret = -EINVAL;
  1335. goto err;
  1336. }
  1337. }
  1338. ctx->streamon_out = 1;
  1339. } else {
  1340. ctx->streamon_cap = 1;
  1341. }
  1342. /* Don't start the coda unless both queues are on */
  1343. if (!(ctx->streamon_out && ctx->streamon_cap))
  1344. goto out;
  1345. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1346. if ((q_data_src->rect.width != q_data_dst->width &&
  1347. round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
  1348. (q_data_src->rect.height != q_data_dst->height &&
  1349. round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
  1350. v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
  1351. q_data_src->rect.width, q_data_src->rect.height,
  1352. q_data_dst->width, q_data_dst->height);
  1353. ret = -EINVAL;
  1354. goto err;
  1355. }
  1356. /* Allow BIT decoder device_run with no new buffers queued */
  1357. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1358. v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
  1359. ctx->gopcounter = ctx->params.gop_size - 1;
  1360. ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
  1361. q_data_dst->fourcc);
  1362. if (!ctx->codec) {
  1363. v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
  1364. ret = -EINVAL;
  1365. goto err;
  1366. }
  1367. if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
  1368. ctx->params.gop_size = 1;
  1369. ctx->gopcounter = ctx->params.gop_size - 1;
  1370. ret = ctx->ops->start_streaming(ctx);
  1371. if (ctx->inst_type == CODA_INST_DECODER) {
  1372. if (ret == -EAGAIN)
  1373. goto out;
  1374. }
  1375. if (ret < 0)
  1376. goto err;
  1377. out:
  1378. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1379. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1380. list_del(&m2m_buf->list);
  1381. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
  1382. }
  1383. }
  1384. return 0;
  1385. err:
  1386. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1387. list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
  1388. list_del(&m2m_buf->list);
  1389. v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
  1390. }
  1391. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1392. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1393. } else {
  1394. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1395. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
  1396. }
  1397. return ret;
  1398. }
  1399. static void coda_stop_streaming(struct vb2_queue *q)
  1400. {
  1401. struct coda_ctx *ctx = vb2_get_drv_priv(q);
  1402. struct coda_dev *dev = ctx->dev;
  1403. struct vb2_v4l2_buffer *buf;
  1404. unsigned long flags;
  1405. bool stop;
  1406. stop = ctx->streamon_out && ctx->streamon_cap;
  1407. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1408. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1409. "%s: output\n", __func__);
  1410. ctx->streamon_out = 0;
  1411. coda_bit_stream_end_flag(ctx);
  1412. ctx->qsequence = 0;
  1413. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
  1414. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1415. } else {
  1416. v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
  1417. "%s: capture\n", __func__);
  1418. ctx->streamon_cap = 0;
  1419. ctx->osequence = 0;
  1420. ctx->sequence_offset = 0;
  1421. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
  1422. v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
  1423. }
  1424. if (stop) {
  1425. struct coda_buffer_meta *meta;
  1426. if (ctx->ops->seq_end_work) {
  1427. queue_work(dev->workqueue, &ctx->seq_end_work);
  1428. flush_work(&ctx->seq_end_work);
  1429. }
  1430. spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
  1431. while (!list_empty(&ctx->buffer_meta_list)) {
  1432. meta = list_first_entry(&ctx->buffer_meta_list,
  1433. struct coda_buffer_meta, list);
  1434. list_del(&meta->list);
  1435. kfree(meta);
  1436. }
  1437. ctx->num_metas = 0;
  1438. spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
  1439. kfifo_init(&ctx->bitstream_fifo,
  1440. ctx->bitstream.vaddr, ctx->bitstream.size);
  1441. ctx->runcounter = 0;
  1442. ctx->aborting = 0;
  1443. ctx->hold = false;
  1444. }
  1445. if (!ctx->streamon_out && !ctx->streamon_cap)
  1446. ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
  1447. }
  1448. static const struct vb2_ops coda_qops = {
  1449. .queue_setup = coda_queue_setup,
  1450. .buf_prepare = coda_buf_prepare,
  1451. .buf_queue = coda_buf_queue,
  1452. .start_streaming = coda_start_streaming,
  1453. .stop_streaming = coda_stop_streaming,
  1454. .wait_prepare = vb2_ops_wait_prepare,
  1455. .wait_finish = vb2_ops_wait_finish,
  1456. };
  1457. static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
  1458. {
  1459. struct coda_ctx *ctx =
  1460. container_of(ctrl->handler, struct coda_ctx, ctrls);
  1461. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1462. "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
  1463. switch (ctrl->id) {
  1464. case V4L2_CID_HFLIP:
  1465. if (ctrl->val)
  1466. ctx->params.rot_mode |= CODA_MIR_HOR;
  1467. else
  1468. ctx->params.rot_mode &= ~CODA_MIR_HOR;
  1469. break;
  1470. case V4L2_CID_VFLIP:
  1471. if (ctrl->val)
  1472. ctx->params.rot_mode |= CODA_MIR_VER;
  1473. else
  1474. ctx->params.rot_mode &= ~CODA_MIR_VER;
  1475. break;
  1476. case V4L2_CID_MPEG_VIDEO_BITRATE:
  1477. ctx->params.bitrate = ctrl->val / 1000;
  1478. break;
  1479. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1480. ctx->params.gop_size = ctrl->val;
  1481. break;
  1482. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
  1483. ctx->params.h264_intra_qp = ctrl->val;
  1484. break;
  1485. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
  1486. ctx->params.h264_inter_qp = ctrl->val;
  1487. break;
  1488. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  1489. ctx->params.h264_min_qp = ctrl->val;
  1490. break;
  1491. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
  1492. ctx->params.h264_max_qp = ctrl->val;
  1493. break;
  1494. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
  1495. ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
  1496. break;
  1497. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
  1498. ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
  1499. break;
  1500. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  1501. ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
  1502. break;
  1503. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  1504. /* TODO: switch between baseline and constrained baseline */
  1505. if (ctx->inst_type == CODA_INST_ENCODER)
  1506. ctx->params.h264_profile_idc = 66;
  1507. break;
  1508. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  1509. /* nothing to do, this is set by the encoder */
  1510. break;
  1511. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
  1512. ctx->params.mpeg4_intra_qp = ctrl->val;
  1513. break;
  1514. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
  1515. ctx->params.mpeg4_inter_qp = ctrl->val;
  1516. break;
  1517. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1518. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1519. /* nothing to do, these are fixed */
  1520. break;
  1521. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  1522. ctx->params.slice_mode = ctrl->val;
  1523. break;
  1524. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
  1525. ctx->params.slice_max_mb = ctrl->val;
  1526. break;
  1527. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
  1528. ctx->params.slice_max_bits = ctrl->val * 8;
  1529. break;
  1530. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  1531. break;
  1532. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
  1533. ctx->params.intra_refresh = ctrl->val;
  1534. break;
  1535. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  1536. ctx->params.force_ipicture = true;
  1537. break;
  1538. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1539. coda_set_jpeg_compression_quality(ctx, ctrl->val);
  1540. break;
  1541. case V4L2_CID_JPEG_RESTART_INTERVAL:
  1542. ctx->params.jpeg_restart_interval = ctrl->val;
  1543. break;
  1544. case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
  1545. ctx->params.vbv_delay = ctrl->val;
  1546. break;
  1547. case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
  1548. ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
  1549. break;
  1550. default:
  1551. v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
  1552. "Invalid control, id=%d, val=%d\n",
  1553. ctrl->id, ctrl->val);
  1554. return -EINVAL;
  1555. }
  1556. return 0;
  1557. }
  1558. static const struct v4l2_ctrl_ops coda_ctrl_ops = {
  1559. .s_ctrl = coda_s_ctrl,
  1560. };
  1561. static void coda_encode_ctrls(struct coda_ctx *ctx)
  1562. {
  1563. int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
  1564. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1565. V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
  1566. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1567. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
  1568. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1569. V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
  1570. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1571. V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
  1572. if (ctx->dev->devtype->product != CODA_960) {
  1573. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1574. V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
  1575. }
  1576. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1577. V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
  1578. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1579. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
  1580. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1581. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
  1582. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1583. V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
  1584. V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
  1585. 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
  1586. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1587. V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  1588. V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
  1589. V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
  1590. if (ctx->dev->devtype->product == CODA_HX4 ||
  1591. ctx->dev->devtype->product == CODA_7541) {
  1592. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1593. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  1594. V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
  1595. ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  1596. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  1597. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
  1598. V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
  1599. }
  1600. if (ctx->dev->devtype->product == CODA_960) {
  1601. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1602. V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  1603. V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  1604. ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  1605. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  1606. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  1607. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  1608. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  1609. V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
  1610. }
  1611. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1612. V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
  1613. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1614. V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
  1615. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1616. V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
  1617. V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
  1618. V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
  1619. if (ctx->dev->devtype->product == CODA_HX4 ||
  1620. ctx->dev->devtype->product == CODA_7541 ||
  1621. ctx->dev->devtype->product == CODA_960) {
  1622. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1623. V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
  1624. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
  1625. ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
  1626. V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
  1627. }
  1628. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1629. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
  1630. V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
  1631. V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
  1632. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1633. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
  1634. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1635. V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
  1636. 500);
  1637. v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
  1638. V4L2_CID_MPEG_VIDEO_HEADER_MODE,
  1639. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
  1640. (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
  1641. V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
  1642. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1643. V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
  1644. 1920 * 1088 / 256, 1, 0);
  1645. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1646. V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
  1647. /*
  1648. * The maximum VBV size value is 0x7fffffff bits,
  1649. * one bit less than 262144 KiB
  1650. */
  1651. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1652. V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
  1653. }
  1654. static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
  1655. {
  1656. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1657. V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
  1658. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1659. V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
  1660. }
  1661. static void coda_decode_ctrls(struct coda_ctx *ctx)
  1662. {
  1663. u64 mask;
  1664. u8 max;
  1665. ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  1666. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  1667. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  1668. ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  1669. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  1670. (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  1671. V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
  1672. if (ctx->h264_profile_ctrl)
  1673. ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1674. if (ctx->dev->devtype->product == CODA_HX4 ||
  1675. ctx->dev->devtype->product == CODA_7541) {
  1676. max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  1677. mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  1678. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  1679. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  1680. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  1681. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0));
  1682. } else if (ctx->dev->devtype->product == CODA_960) {
  1683. max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
  1684. mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  1685. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  1686. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  1687. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  1688. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
  1689. (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1));
  1690. } else {
  1691. return;
  1692. }
  1693. ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
  1694. &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, mask,
  1695. max);
  1696. if (ctx->h264_level_ctrl)
  1697. ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1698. }
  1699. static int coda_ctrls_setup(struct coda_ctx *ctx)
  1700. {
  1701. v4l2_ctrl_handler_init(&ctx->ctrls, 2);
  1702. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1703. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1704. v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
  1705. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1706. if (ctx->inst_type == CODA_INST_ENCODER) {
  1707. if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
  1708. coda_jpeg_encode_ctrls(ctx);
  1709. else
  1710. coda_encode_ctrls(ctx);
  1711. } else {
  1712. if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
  1713. coda_decode_ctrls(ctx);
  1714. }
  1715. if (ctx->ctrls.error) {
  1716. v4l2_err(&ctx->dev->v4l2_dev,
  1717. "control initialization error (%d)",
  1718. ctx->ctrls.error);
  1719. return -EINVAL;
  1720. }
  1721. return v4l2_ctrl_handler_setup(&ctx->ctrls);
  1722. }
  1723. static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
  1724. {
  1725. vq->drv_priv = ctx;
  1726. vq->ops = &coda_qops;
  1727. vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1728. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1729. vq->lock = &ctx->dev->dev_mutex;
  1730. /* One way to indicate end-of-stream for coda is to set the
  1731. * bytesused == 0. However by default videobuf2 handles bytesused
  1732. * equal to 0 as a special case and changes its value to the size
  1733. * of the buffer. Set the allow_zero_bytesused flag, so
  1734. * that videobuf2 will keep the value of bytesused intact.
  1735. */
  1736. vq->allow_zero_bytesused = 1;
  1737. /*
  1738. * We might be fine with no buffers on some of the queues, but that
  1739. * would need to be reflected in job_ready(). Currently we expect all
  1740. * queues to have at least one buffer queued.
  1741. */
  1742. vq->min_buffers_needed = 1;
  1743. vq->dev = &ctx->dev->plat_dev->dev;
  1744. return vb2_queue_init(vq);
  1745. }
  1746. int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1747. struct vb2_queue *dst_vq)
  1748. {
  1749. int ret;
  1750. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1751. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1752. src_vq->mem_ops = &vb2_dma_contig_memops;
  1753. ret = coda_queue_init(priv, src_vq);
  1754. if (ret)
  1755. return ret;
  1756. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1757. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1758. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1759. return coda_queue_init(priv, dst_vq);
  1760. }
  1761. int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
  1762. struct vb2_queue *dst_vq)
  1763. {
  1764. int ret;
  1765. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1766. src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
  1767. src_vq->mem_ops = &vb2_vmalloc_memops;
  1768. ret = coda_queue_init(priv, src_vq);
  1769. if (ret)
  1770. return ret;
  1771. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1772. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  1773. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1774. return coda_queue_init(priv, dst_vq);
  1775. }
  1776. static int coda_next_free_instance(struct coda_dev *dev)
  1777. {
  1778. int idx = ffz(dev->instance_mask);
  1779. if ((idx < 0) ||
  1780. (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
  1781. return -EBUSY;
  1782. return idx;
  1783. }
  1784. /*
  1785. * File operations
  1786. */
  1787. static int coda_open(struct file *file)
  1788. {
  1789. struct video_device *vdev = video_devdata(file);
  1790. struct coda_dev *dev = video_get_drvdata(vdev);
  1791. struct coda_ctx *ctx = NULL;
  1792. char *name;
  1793. int ret;
  1794. int idx;
  1795. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1796. if (!ctx)
  1797. return -ENOMEM;
  1798. idx = coda_next_free_instance(dev);
  1799. if (idx < 0) {
  1800. ret = idx;
  1801. goto err_coda_max;
  1802. }
  1803. set_bit(idx, &dev->instance_mask);
  1804. name = kasprintf(GFP_KERNEL, "context%d", idx);
  1805. if (!name) {
  1806. ret = -ENOMEM;
  1807. goto err_coda_name_init;
  1808. }
  1809. ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
  1810. kfree(name);
  1811. ctx->cvd = to_coda_video_device(vdev);
  1812. ctx->inst_type = ctx->cvd->type;
  1813. ctx->ops = ctx->cvd->ops;
  1814. ctx->use_bit = !ctx->cvd->direct;
  1815. init_completion(&ctx->completion);
  1816. INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
  1817. if (ctx->ops->seq_end_work)
  1818. INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
  1819. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1820. file->private_data = &ctx->fh;
  1821. v4l2_fh_add(&ctx->fh);
  1822. ctx->dev = dev;
  1823. ctx->idx = idx;
  1824. switch (dev->devtype->product) {
  1825. case CODA_960:
  1826. /*
  1827. * Enabling the BWB when decoding can hang the firmware with
  1828. * certain streams. The issue was tracked as ENGR00293425 by
  1829. * Freescale. As a workaround, disable BWB for all decoders.
  1830. * The enable_bwb module parameter allows to override this.
  1831. */
  1832. if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
  1833. ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
  1834. /* fallthrough */
  1835. case CODA_HX4:
  1836. case CODA_7541:
  1837. ctx->reg_idx = 0;
  1838. break;
  1839. default:
  1840. ctx->reg_idx = idx;
  1841. }
  1842. if (ctx->dev->vdoa && !disable_vdoa) {
  1843. ctx->vdoa = vdoa_context_create(dev->vdoa);
  1844. if (!ctx->vdoa)
  1845. v4l2_warn(&dev->v4l2_dev,
  1846. "Failed to create vdoa context: not using vdoa");
  1847. }
  1848. ctx->use_vdoa = false;
  1849. /* Power up and upload firmware if necessary */
  1850. ret = pm_runtime_get_sync(&dev->plat_dev->dev);
  1851. if (ret < 0) {
  1852. v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
  1853. goto err_pm_get;
  1854. }
  1855. ret = clk_prepare_enable(dev->clk_per);
  1856. if (ret)
  1857. goto err_clk_per;
  1858. ret = clk_prepare_enable(dev->clk_ahb);
  1859. if (ret)
  1860. goto err_clk_ahb;
  1861. set_default_params(ctx);
  1862. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  1863. ctx->ops->queue_init);
  1864. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1865. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1866. v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
  1867. __func__, ret);
  1868. goto err_ctx_init;
  1869. }
  1870. ret = coda_ctrls_setup(ctx);
  1871. if (ret) {
  1872. v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
  1873. goto err_ctrls_setup;
  1874. }
  1875. ctx->fh.ctrl_handler = &ctx->ctrls;
  1876. mutex_init(&ctx->bitstream_mutex);
  1877. mutex_init(&ctx->buffer_mutex);
  1878. INIT_LIST_HEAD(&ctx->buffer_meta_list);
  1879. spin_lock_init(&ctx->buffer_meta_lock);
  1880. mutex_lock(&dev->dev_mutex);
  1881. list_add(&ctx->list, &dev->instances);
  1882. mutex_unlock(&dev->dev_mutex);
  1883. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
  1884. ctx->idx, ctx);
  1885. return 0;
  1886. err_ctrls_setup:
  1887. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1888. err_ctx_init:
  1889. clk_disable_unprepare(dev->clk_ahb);
  1890. err_clk_ahb:
  1891. clk_disable_unprepare(dev->clk_per);
  1892. err_clk_per:
  1893. pm_runtime_put_sync(&dev->plat_dev->dev);
  1894. err_pm_get:
  1895. v4l2_fh_del(&ctx->fh);
  1896. v4l2_fh_exit(&ctx->fh);
  1897. clear_bit(ctx->idx, &dev->instance_mask);
  1898. err_coda_name_init:
  1899. err_coda_max:
  1900. kfree(ctx);
  1901. return ret;
  1902. }
  1903. static int coda_release(struct file *file)
  1904. {
  1905. struct coda_dev *dev = video_drvdata(file);
  1906. struct coda_ctx *ctx = fh_to_ctx(file->private_data);
  1907. v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
  1908. ctx);
  1909. if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
  1910. coda_bit_stream_end_flag(ctx);
  1911. /* If this instance is running, call .job_abort and wait for it to end */
  1912. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1913. if (ctx->vdoa)
  1914. vdoa_context_destroy(ctx->vdoa);
  1915. /* In case the instance was not running, we still need to call SEQ_END */
  1916. if (ctx->ops->seq_end_work) {
  1917. queue_work(dev->workqueue, &ctx->seq_end_work);
  1918. flush_work(&ctx->seq_end_work);
  1919. }
  1920. mutex_lock(&dev->dev_mutex);
  1921. list_del(&ctx->list);
  1922. mutex_unlock(&dev->dev_mutex);
  1923. if (ctx->dev->devtype->product == CODA_DX6)
  1924. coda_free_aux_buf(dev, &ctx->workbuf);
  1925. v4l2_ctrl_handler_free(&ctx->ctrls);
  1926. clk_disable_unprepare(dev->clk_ahb);
  1927. clk_disable_unprepare(dev->clk_per);
  1928. pm_runtime_put_sync(&dev->plat_dev->dev);
  1929. v4l2_fh_del(&ctx->fh);
  1930. v4l2_fh_exit(&ctx->fh);
  1931. clear_bit(ctx->idx, &dev->instance_mask);
  1932. if (ctx->ops->release)
  1933. ctx->ops->release(ctx);
  1934. debugfs_remove_recursive(ctx->debugfs_entry);
  1935. kfree(ctx);
  1936. return 0;
  1937. }
  1938. static const struct v4l2_file_operations coda_fops = {
  1939. .owner = THIS_MODULE,
  1940. .open = coda_open,
  1941. .release = coda_release,
  1942. .poll = v4l2_m2m_fop_poll,
  1943. .unlocked_ioctl = video_ioctl2,
  1944. .mmap = v4l2_m2m_fop_mmap,
  1945. };
  1946. static int coda_hw_init(struct coda_dev *dev)
  1947. {
  1948. u32 data;
  1949. u16 *p;
  1950. int i, ret;
  1951. ret = clk_prepare_enable(dev->clk_per);
  1952. if (ret)
  1953. goto err_clk_per;
  1954. ret = clk_prepare_enable(dev->clk_ahb);
  1955. if (ret)
  1956. goto err_clk_ahb;
  1957. reset_control_reset(dev->rstc);
  1958. /*
  1959. * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
  1960. * The 16-bit chars in the code buffer are in memory access
  1961. * order, re-sort them to CODA order for register download.
  1962. * Data in this SRAM survives a reboot.
  1963. */
  1964. p = (u16 *)dev->codebuf.vaddr;
  1965. if (dev->devtype->product == CODA_DX6) {
  1966. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1967. data = CODA_DOWN_ADDRESS_SET(i) |
  1968. CODA_DOWN_DATA_SET(p[i ^ 1]);
  1969. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1970. }
  1971. } else {
  1972. for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
  1973. data = CODA_DOWN_ADDRESS_SET(i) |
  1974. CODA_DOWN_DATA_SET(p[round_down(i, 4) +
  1975. 3 - (i % 4)]);
  1976. coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
  1977. }
  1978. }
  1979. /* Clear registers */
  1980. for (i = 0; i < 64; i++)
  1981. coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
  1982. /* Tell the BIT where to find everything it needs */
  1983. if (dev->devtype->product == CODA_960 ||
  1984. dev->devtype->product == CODA_7541 ||
  1985. dev->devtype->product == CODA_HX4) {
  1986. coda_write(dev, dev->tempbuf.paddr,
  1987. CODA_REG_BIT_TEMP_BUF_ADDR);
  1988. coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
  1989. } else {
  1990. coda_write(dev, dev->workbuf.paddr,
  1991. CODA_REG_BIT_WORK_BUF_ADDR);
  1992. }
  1993. coda_write(dev, dev->codebuf.paddr,
  1994. CODA_REG_BIT_CODE_BUF_ADDR);
  1995. coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
  1996. /* Set default values */
  1997. switch (dev->devtype->product) {
  1998. case CODA_DX6:
  1999. coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
  2000. CODA_REG_BIT_STREAM_CTRL);
  2001. break;
  2002. default:
  2003. coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
  2004. CODA_REG_BIT_STREAM_CTRL);
  2005. }
  2006. if (dev->devtype->product == CODA_960)
  2007. coda_write(dev, CODA9_FRAME_ENABLE_BWB,
  2008. CODA_REG_BIT_FRAME_MEM_CTRL);
  2009. else
  2010. coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
  2011. if (dev->devtype->product != CODA_DX6)
  2012. coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
  2013. coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
  2014. CODA_REG_BIT_INT_ENABLE);
  2015. /* Reset VPU and start processor */
  2016. data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
  2017. data |= CODA_REG_RESET_ENABLE;
  2018. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2019. udelay(10);
  2020. data &= ~CODA_REG_RESET_ENABLE;
  2021. coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
  2022. coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
  2023. clk_disable_unprepare(dev->clk_ahb);
  2024. clk_disable_unprepare(dev->clk_per);
  2025. return 0;
  2026. err_clk_ahb:
  2027. clk_disable_unprepare(dev->clk_per);
  2028. err_clk_per:
  2029. return ret;
  2030. }
  2031. static int coda_register_device(struct coda_dev *dev, int i)
  2032. {
  2033. struct video_device *vfd = &dev->vfd[i];
  2034. if (i >= dev->devtype->num_vdevs)
  2035. return -EINVAL;
  2036. strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
  2037. vfd->fops = &coda_fops;
  2038. vfd->ioctl_ops = &coda_ioctl_ops;
  2039. vfd->release = video_device_release_empty,
  2040. vfd->lock = &dev->dev_mutex;
  2041. vfd->v4l2_dev = &dev->v4l2_dev;
  2042. vfd->vfl_dir = VFL_DIR_M2M;
  2043. video_set_drvdata(vfd, dev);
  2044. /* Not applicable, use the selection API instead */
  2045. v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
  2046. v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
  2047. v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
  2048. return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  2049. }
  2050. static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
  2051. size_t size)
  2052. {
  2053. u32 *src = (u32 *)buf;
  2054. /* Check if the firmware has a 16-byte Freescale header, skip it */
  2055. if (buf[0] == 'M' && buf[1] == 'X')
  2056. src += 4;
  2057. /*
  2058. * Check whether the firmware is in native order or pre-reordered for
  2059. * memory access. The first instruction opcode always is 0xe40e.
  2060. */
  2061. if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
  2062. u32 *dst = dev->codebuf.vaddr;
  2063. int i;
  2064. /* Firmware in native order, reorder while copying */
  2065. if (dev->devtype->product == CODA_DX6) {
  2066. for (i = 0; i < (size - 16) / 4; i++)
  2067. dst[i] = (src[i] << 16) | (src[i] >> 16);
  2068. } else {
  2069. for (i = 0; i < (size - 16) / 4; i += 2) {
  2070. dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
  2071. dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
  2072. }
  2073. }
  2074. } else {
  2075. /* Copy the already reordered firmware image */
  2076. memcpy(dev->codebuf.vaddr, src, size);
  2077. }
  2078. }
  2079. static void coda_fw_callback(const struct firmware *fw, void *context);
  2080. static int coda_firmware_request(struct coda_dev *dev)
  2081. {
  2082. char *fw;
  2083. if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
  2084. return -EINVAL;
  2085. fw = dev->devtype->firmware[dev->firmware];
  2086. dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
  2087. coda_product_name(dev->devtype->product));
  2088. return request_firmware_nowait(THIS_MODULE, true, fw,
  2089. &dev->plat_dev->dev, GFP_KERNEL, dev,
  2090. coda_fw_callback);
  2091. }
  2092. static void coda_fw_callback(const struct firmware *fw, void *context)
  2093. {
  2094. struct coda_dev *dev = context;
  2095. struct platform_device *pdev = dev->plat_dev;
  2096. int i, ret;
  2097. if (!fw) {
  2098. dev->firmware++;
  2099. ret = coda_firmware_request(dev);
  2100. if (ret < 0) {
  2101. v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
  2102. goto put_pm;
  2103. }
  2104. return;
  2105. }
  2106. if (dev->firmware > 0) {
  2107. /*
  2108. * Since we can't suppress warnings for failed asynchronous
  2109. * firmware requests, report that the fallback firmware was
  2110. * found.
  2111. */
  2112. dev_info(&pdev->dev, "Using fallback firmware %s\n",
  2113. dev->devtype->firmware[dev->firmware]);
  2114. }
  2115. /* allocate auxiliary per-device code buffer for the BIT processor */
  2116. ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
  2117. dev->debugfs_root);
  2118. if (ret < 0)
  2119. goto put_pm;
  2120. coda_copy_firmware(dev, fw->data, fw->size);
  2121. release_firmware(fw);
  2122. ret = coda_hw_init(dev);
  2123. if (ret < 0) {
  2124. v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
  2125. goto put_pm;
  2126. }
  2127. ret = coda_check_firmware(dev);
  2128. if (ret < 0)
  2129. goto put_pm;
  2130. dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
  2131. if (IS_ERR(dev->m2m_dev)) {
  2132. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  2133. goto put_pm;
  2134. }
  2135. for (i = 0; i < dev->devtype->num_vdevs; i++) {
  2136. ret = coda_register_device(dev, i);
  2137. if (ret) {
  2138. v4l2_err(&dev->v4l2_dev,
  2139. "Failed to register %s video device: %d\n",
  2140. dev->devtype->vdevs[i]->name, ret);
  2141. goto rel_vfd;
  2142. }
  2143. }
  2144. v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
  2145. dev->vfd[0].num, dev->vfd[i - 1].num);
  2146. pm_runtime_put_sync(&pdev->dev);
  2147. return;
  2148. rel_vfd:
  2149. while (--i >= 0)
  2150. video_unregister_device(&dev->vfd[i]);
  2151. v4l2_m2m_release(dev->m2m_dev);
  2152. put_pm:
  2153. pm_runtime_put_sync(&pdev->dev);
  2154. }
  2155. enum coda_platform {
  2156. CODA_IMX27,
  2157. CODA_IMX51,
  2158. CODA_IMX53,
  2159. CODA_IMX6Q,
  2160. CODA_IMX6DL,
  2161. };
  2162. static const struct coda_devtype coda_devdata[] = {
  2163. [CODA_IMX27] = {
  2164. .firmware = {
  2165. "vpu_fw_imx27_TO2.bin",
  2166. "vpu/vpu_fw_imx27_TO2.bin",
  2167. "v4l-codadx6-imx27.bin"
  2168. },
  2169. .product = CODA_DX6,
  2170. .codecs = codadx6_codecs,
  2171. .num_codecs = ARRAY_SIZE(codadx6_codecs),
  2172. .vdevs = codadx6_video_devices,
  2173. .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
  2174. .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
  2175. .iram_size = 0xb000,
  2176. },
  2177. [CODA_IMX51] = {
  2178. .firmware = {
  2179. "vpu_fw_imx51.bin",
  2180. "vpu/vpu_fw_imx51.bin",
  2181. "v4l-codahx4-imx51.bin"
  2182. },
  2183. .product = CODA_HX4,
  2184. .codecs = codahx4_codecs,
  2185. .num_codecs = ARRAY_SIZE(codahx4_codecs),
  2186. .vdevs = codahx4_video_devices,
  2187. .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
  2188. .workbuf_size = 128 * 1024,
  2189. .tempbuf_size = 304 * 1024,
  2190. .iram_size = 0x14000,
  2191. },
  2192. [CODA_IMX53] = {
  2193. .firmware = {
  2194. "vpu_fw_imx53.bin",
  2195. "vpu/vpu_fw_imx53.bin",
  2196. "v4l-coda7541-imx53.bin"
  2197. },
  2198. .product = CODA_7541,
  2199. .codecs = coda7_codecs,
  2200. .num_codecs = ARRAY_SIZE(coda7_codecs),
  2201. .vdevs = coda7_video_devices,
  2202. .num_vdevs = ARRAY_SIZE(coda7_video_devices),
  2203. .workbuf_size = 128 * 1024,
  2204. .tempbuf_size = 304 * 1024,
  2205. .iram_size = 0x14000,
  2206. },
  2207. [CODA_IMX6Q] = {
  2208. .firmware = {
  2209. "vpu_fw_imx6q.bin",
  2210. "vpu/vpu_fw_imx6q.bin",
  2211. "v4l-coda960-imx6q.bin"
  2212. },
  2213. .product = CODA_960,
  2214. .codecs = coda9_codecs,
  2215. .num_codecs = ARRAY_SIZE(coda9_codecs),
  2216. .vdevs = coda9_video_devices,
  2217. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  2218. .workbuf_size = 80 * 1024,
  2219. .tempbuf_size = 204 * 1024,
  2220. .iram_size = 0x21000,
  2221. },
  2222. [CODA_IMX6DL] = {
  2223. .firmware = {
  2224. "vpu_fw_imx6d.bin",
  2225. "vpu/vpu_fw_imx6d.bin",
  2226. "v4l-coda960-imx6dl.bin"
  2227. },
  2228. .product = CODA_960,
  2229. .codecs = coda9_codecs,
  2230. .num_codecs = ARRAY_SIZE(coda9_codecs),
  2231. .vdevs = coda9_video_devices,
  2232. .num_vdevs = ARRAY_SIZE(coda9_video_devices),
  2233. .workbuf_size = 80 * 1024,
  2234. .tempbuf_size = 204 * 1024,
  2235. .iram_size = 0x1f000, /* leave 4k for suspend code */
  2236. },
  2237. };
  2238. static const struct platform_device_id coda_platform_ids[] = {
  2239. { .name = "coda-imx27", .driver_data = CODA_IMX27 },
  2240. { /* sentinel */ }
  2241. };
  2242. MODULE_DEVICE_TABLE(platform, coda_platform_ids);
  2243. #ifdef CONFIG_OF
  2244. static const struct of_device_id coda_dt_ids[] = {
  2245. { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
  2246. { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
  2247. { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
  2248. { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
  2249. { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
  2250. { /* sentinel */ }
  2251. };
  2252. MODULE_DEVICE_TABLE(of, coda_dt_ids);
  2253. #endif
  2254. static int coda_probe(struct platform_device *pdev)
  2255. {
  2256. const struct of_device_id *of_id =
  2257. of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
  2258. const struct platform_device_id *pdev_id;
  2259. struct coda_platform_data *pdata = pdev->dev.platform_data;
  2260. struct device_node *np = pdev->dev.of_node;
  2261. struct gen_pool *pool;
  2262. struct coda_dev *dev;
  2263. struct resource *res;
  2264. int ret, irq;
  2265. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  2266. if (!dev)
  2267. return -ENOMEM;
  2268. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  2269. if (of_id)
  2270. dev->devtype = of_id->data;
  2271. else if (pdev_id)
  2272. dev->devtype = &coda_devdata[pdev_id->driver_data];
  2273. else
  2274. return -EINVAL;
  2275. spin_lock_init(&dev->irqlock);
  2276. INIT_LIST_HEAD(&dev->instances);
  2277. dev->plat_dev = pdev;
  2278. dev->clk_per = devm_clk_get(&pdev->dev, "per");
  2279. if (IS_ERR(dev->clk_per)) {
  2280. dev_err(&pdev->dev, "Could not get per clock\n");
  2281. return PTR_ERR(dev->clk_per);
  2282. }
  2283. dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2284. if (IS_ERR(dev->clk_ahb)) {
  2285. dev_err(&pdev->dev, "Could not get ahb clock\n");
  2286. return PTR_ERR(dev->clk_ahb);
  2287. }
  2288. /* Get memory for physical registers */
  2289. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2290. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  2291. if (IS_ERR(dev->regs_base))
  2292. return PTR_ERR(dev->regs_base);
  2293. /* IRQ */
  2294. irq = platform_get_irq_byname(pdev, "bit");
  2295. if (irq < 0)
  2296. irq = platform_get_irq(pdev, 0);
  2297. if (irq < 0) {
  2298. dev_err(&pdev->dev, "failed to get irq resource\n");
  2299. return irq;
  2300. }
  2301. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
  2302. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  2303. if (ret < 0) {
  2304. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  2305. return ret;
  2306. }
  2307. dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
  2308. NULL);
  2309. if (IS_ERR(dev->rstc)) {
  2310. ret = PTR_ERR(dev->rstc);
  2311. dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
  2312. return ret;
  2313. }
  2314. /* Get IRAM pool from device tree or platform data */
  2315. pool = of_gen_pool_get(np, "iram", 0);
  2316. if (!pool && pdata)
  2317. pool = gen_pool_get(pdata->iram_dev, NULL);
  2318. if (!pool) {
  2319. dev_err(&pdev->dev, "iram pool not available\n");
  2320. return -ENOMEM;
  2321. }
  2322. dev->iram_pool = pool;
  2323. /* Get vdoa_data if supported by the platform */
  2324. dev->vdoa = coda_get_vdoa_data();
  2325. if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
  2326. return -EPROBE_DEFER;
  2327. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2328. if (ret)
  2329. return ret;
  2330. mutex_init(&dev->dev_mutex);
  2331. mutex_init(&dev->coda_mutex);
  2332. dev->debugfs_root = debugfs_create_dir("coda", NULL);
  2333. if (!dev->debugfs_root)
  2334. dev_warn(&pdev->dev, "failed to create debugfs root\n");
  2335. /* allocate auxiliary per-device buffers for the BIT processor */
  2336. if (dev->devtype->product == CODA_DX6) {
  2337. ret = coda_alloc_aux_buf(dev, &dev->workbuf,
  2338. dev->devtype->workbuf_size, "workbuf",
  2339. dev->debugfs_root);
  2340. if (ret < 0)
  2341. goto err_v4l2_register;
  2342. }
  2343. if (dev->devtype->tempbuf_size) {
  2344. ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
  2345. dev->devtype->tempbuf_size, "tempbuf",
  2346. dev->debugfs_root);
  2347. if (ret < 0)
  2348. goto err_v4l2_register;
  2349. }
  2350. dev->iram.size = dev->devtype->iram_size;
  2351. dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
  2352. &dev->iram.paddr);
  2353. if (!dev->iram.vaddr) {
  2354. dev_warn(&pdev->dev, "unable to alloc iram\n");
  2355. } else {
  2356. memset(dev->iram.vaddr, 0, dev->iram.size);
  2357. dev->iram.blob.data = dev->iram.vaddr;
  2358. dev->iram.blob.size = dev->iram.size;
  2359. dev->iram.dentry = debugfs_create_blob("iram", 0644,
  2360. dev->debugfs_root,
  2361. &dev->iram.blob);
  2362. }
  2363. dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  2364. if (!dev->workqueue) {
  2365. dev_err(&pdev->dev, "unable to alloc workqueue\n");
  2366. ret = -ENOMEM;
  2367. goto err_v4l2_register;
  2368. }
  2369. platform_set_drvdata(pdev, dev);
  2370. /*
  2371. * Start activated so we can directly call coda_hw_init in
  2372. * coda_fw_callback regardless of whether CONFIG_PM is
  2373. * enabled or whether the device is associated with a PM domain.
  2374. */
  2375. pm_runtime_get_noresume(&pdev->dev);
  2376. pm_runtime_set_active(&pdev->dev);
  2377. pm_runtime_enable(&pdev->dev);
  2378. ret = coda_firmware_request(dev);
  2379. if (ret)
  2380. goto err_alloc_workqueue;
  2381. return 0;
  2382. err_alloc_workqueue:
  2383. destroy_workqueue(dev->workqueue);
  2384. err_v4l2_register:
  2385. v4l2_device_unregister(&dev->v4l2_dev);
  2386. return ret;
  2387. }
  2388. static int coda_remove(struct platform_device *pdev)
  2389. {
  2390. struct coda_dev *dev = platform_get_drvdata(pdev);
  2391. int i;
  2392. for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
  2393. if (video_get_drvdata(&dev->vfd[i]))
  2394. video_unregister_device(&dev->vfd[i]);
  2395. }
  2396. if (dev->m2m_dev)
  2397. v4l2_m2m_release(dev->m2m_dev);
  2398. pm_runtime_disable(&pdev->dev);
  2399. v4l2_device_unregister(&dev->v4l2_dev);
  2400. destroy_workqueue(dev->workqueue);
  2401. if (dev->iram.vaddr)
  2402. gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
  2403. dev->iram.size);
  2404. coda_free_aux_buf(dev, &dev->codebuf);
  2405. coda_free_aux_buf(dev, &dev->tempbuf);
  2406. coda_free_aux_buf(dev, &dev->workbuf);
  2407. debugfs_remove_recursive(dev->debugfs_root);
  2408. return 0;
  2409. }
  2410. #ifdef CONFIG_PM
  2411. static int coda_runtime_resume(struct device *dev)
  2412. {
  2413. struct coda_dev *cdev = dev_get_drvdata(dev);
  2414. int ret = 0;
  2415. if (dev->pm_domain && cdev->codebuf.vaddr) {
  2416. ret = coda_hw_init(cdev);
  2417. if (ret)
  2418. v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
  2419. }
  2420. return ret;
  2421. }
  2422. #endif
  2423. static const struct dev_pm_ops coda_pm_ops = {
  2424. SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
  2425. };
  2426. static struct platform_driver coda_driver = {
  2427. .probe = coda_probe,
  2428. .remove = coda_remove,
  2429. .driver = {
  2430. .name = CODA_NAME,
  2431. .of_match_table = of_match_ptr(coda_dt_ids),
  2432. .pm = &coda_pm_ops,
  2433. },
  2434. .id_table = coda_platform_ids,
  2435. };
  2436. module_platform_driver(coda_driver);
  2437. MODULE_LICENSE("GPL");
  2438. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  2439. MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");