bcm2835-unicam.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BCM283x / BCM271x Unicam Capture Driver
  4. *
  5. * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
  6. * Copyright (C) 2024 - Ideas on Board
  7. *
  8. * Dave Stevenson <dave.stevenson@raspberrypi.com>
  9. *
  10. * Based on TI am437x driver by
  11. * Benoit Parrot <bparrot@ti.com>
  12. * Lad, Prabhakar <prabhakar.csengg@gmail.com>
  13. *
  14. * and TI CAL camera interface driver by
  15. * Benoit Parrot <bparrot@ti.com>
  16. *
  17. *
  18. * There are two camera drivers in the kernel for BCM283x - this one and
  19. * bcm2835-camera (currently in staging).
  20. *
  21. * This driver directly controls the Unicam peripheral - there is no
  22. * involvement with the VideoCore firmware. Unicam receives CSI-2 or CCP2 data
  23. * and writes it into SDRAM. The only potential processing options are to
  24. * repack Bayer data into an alternate format, and applying windowing. The
  25. * repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P to
  26. * V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12, but
  27. * not generically up to V4L2_PIX_FMT_Sxxxx16. Support for windowing may be
  28. * added later.
  29. *
  30. * It should be possible to connect this driver to any sensor with a suitable
  31. * output interface and V4L2 subdevice driver.
  32. */
  33. #include <linux/clk.h>
  34. #include <linux/delay.h>
  35. #include <linux/device.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/err.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/io.h>
  40. #include <linux/module.h>
  41. #include <linux/of.h>
  42. #include <linux/of_device.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/slab.h>
  46. #include <linux/videodev2.h>
  47. #include <media/mipi-csi2.h>
  48. #include <media/v4l2-async.h>
  49. #include <media/v4l2-common.h>
  50. #include <media/v4l2-dev.h>
  51. #include <media/v4l2-device.h>
  52. #include <media/v4l2-event.h>
  53. #include <media/v4l2-ioctl.h>
  54. #include <media/v4l2-fwnode.h>
  55. #include <media/v4l2-mc.h>
  56. #include <media/v4l2-subdev.h>
  57. #include <media/videobuf2-dma-contig.h>
  58. #include "bcm2835-unicam-regs.h"
  59. #define UNICAM_MODULE_NAME "unicam"
  60. /*
  61. * Unicam must request a minimum of 250Mhz from the VPU clock.
  62. * Otherwise the input FIFOs overrun and cause image corruption.
  63. */
  64. #define UNICAM_MIN_VPU_CLOCK_RATE (250 * 1000 * 1000)
  65. /* Unicam has an internal DMA alignment constraint of 16 bytes for each line. */
  66. #define UNICAM_DMA_BPL_ALIGNMENT 16
  67. /*
  68. * The image stride is stored in a 16 bit register, and needs to be aligned to
  69. * the DMA constraint. As the ISP in the same SoC has a 32 bytes alignment
  70. * constraint on its input, set the image stride alignment to 32 bytes here as
  71. * well to avoid incompatible configurations.
  72. */
  73. #define UNICAM_IMAGE_BPL_ALIGNMENT 32
  74. #define UNICAM_IMAGE_MAX_BPL ((1U << 16) - UNICAM_IMAGE_BPL_ALIGNMENT)
  75. /*
  76. * Max width is therefore determined by the max stride divided by the number of
  77. * bits per pixel. Take 32bpp as a worst case. No imposed limit on the height,
  78. * so adopt a square image for want of anything better.
  79. */
  80. #define UNICAM_IMAGE_MIN_WIDTH 16
  81. #define UNICAM_IMAGE_MIN_HEIGHT 16
  82. #define UNICAM_IMAGE_MAX_WIDTH (UNICAM_IMAGE_MAX_BPL / 4)
  83. #define UNICAM_IMAGE_MAX_HEIGHT UNICAM_IMAGE_MAX_WIDTH
  84. /*
  85. * There's no intrinsic limits on the width and height for embedded data. Use
  86. * the same maximum values as for the image, to avoid overflows in the image
  87. * size computation.
  88. */
  89. #define UNICAM_META_MIN_WIDTH 1
  90. #define UNICAM_META_MIN_HEIGHT 1
  91. #define UNICAM_META_MAX_WIDTH UNICAM_IMAGE_MAX_WIDTH
  92. #define UNICAM_META_MAX_HEIGHT UNICAM_IMAGE_MAX_HEIGHT
  93. /*
  94. * Size of the dummy buffer. Can be any size really, but the DMA
  95. * allocation works in units of page sizes.
  96. */
  97. #define UNICAM_DUMMY_BUF_SIZE PAGE_SIZE
  98. enum unicam_pad {
  99. UNICAM_SD_PAD_SINK,
  100. UNICAM_SD_PAD_SOURCE_IMAGE,
  101. UNICAM_SD_PAD_SOURCE_METADATA,
  102. UNICAM_SD_NUM_PADS
  103. };
  104. enum unicam_node_type {
  105. UNICAM_IMAGE_NODE,
  106. UNICAM_METADATA_NODE,
  107. UNICAM_MAX_NODES
  108. };
  109. /*
  110. * struct unicam_format_info - Unicam media bus format information
  111. * @fourcc: V4L2 pixel format FCC identifier. 0 if n/a.
  112. * @unpacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
  113. * out to 16bpp. 0 if n/a.
  114. * @code: V4L2 media bus format code.
  115. * @depth: Bits per pixel as delivered from the source.
  116. * @csi_dt: CSI data type.
  117. * @unpack: PUM value when unpacking to @unpacked_fourcc
  118. */
  119. struct unicam_format_info {
  120. u32 fourcc;
  121. u32 unpacked_fourcc;
  122. u32 code;
  123. u8 depth;
  124. u8 csi_dt;
  125. u8 unpack;
  126. };
  127. struct unicam_buffer {
  128. struct vb2_v4l2_buffer vb;
  129. struct list_head list;
  130. dma_addr_t dma_addr;
  131. unsigned int size;
  132. };
  133. static inline struct unicam_buffer *to_unicam_buffer(struct vb2_buffer *vb)
  134. {
  135. return container_of(vb, struct unicam_buffer, vb.vb2_buf);
  136. }
  137. struct unicam_node {
  138. bool registered;
  139. unsigned int id;
  140. /* Pointer to the current v4l2_buffer */
  141. struct unicam_buffer *cur_frm;
  142. /* Pointer to the next v4l2_buffer */
  143. struct unicam_buffer *next_frm;
  144. /* Used to store current pixel format */
  145. struct v4l2_format fmt;
  146. /* Buffer queue used in video-buf */
  147. struct vb2_queue buffer_queue;
  148. /* Queue of filled frames */
  149. struct list_head dma_queue;
  150. /* IRQ lock for DMA queue */
  151. spinlock_t dma_queue_lock;
  152. /* Identifies video device for this channel */
  153. struct video_device video_dev;
  154. /* Pointer to the parent handle */
  155. struct unicam_device *dev;
  156. struct media_pad pad;
  157. /*
  158. * Dummy buffer intended to be used by unicam
  159. * if we have no other queued buffers to swap to.
  160. */
  161. struct unicam_buffer dummy_buf;
  162. void *dummy_buf_cpu_addr;
  163. };
  164. struct unicam_device {
  165. struct kref kref;
  166. /* peripheral base address */
  167. void __iomem *base;
  168. /* clock gating base address */
  169. void __iomem *clk_gate_base;
  170. /* lp clock handle */
  171. struct clk *clock;
  172. /* vpu clock handle */
  173. struct clk *vpu_clock;
  174. /* V4l2 device */
  175. struct v4l2_device v4l2_dev;
  176. struct media_device mdev;
  177. /* parent device */
  178. struct device *dev;
  179. /* subdevice async notifier */
  180. struct v4l2_async_notifier notifier;
  181. unsigned int sequence;
  182. /* Sensor node */
  183. struct {
  184. struct v4l2_subdev *subdev;
  185. struct media_pad *pad;
  186. } sensor;
  187. /* Internal subdev */
  188. struct {
  189. struct v4l2_subdev sd;
  190. struct media_pad pads[UNICAM_SD_NUM_PADS];
  191. unsigned int enabled_streams;
  192. } subdev;
  193. enum v4l2_mbus_type bus_type;
  194. /*
  195. * Stores bus.mipi_csi2.flags for CSI2 sensors, or
  196. * bus.mipi_csi1.strobe for CCP2.
  197. */
  198. unsigned int bus_flags;
  199. unsigned int max_data_lanes;
  200. struct {
  201. struct media_pipeline pipe;
  202. unsigned int num_data_lanes;
  203. unsigned int nodes;
  204. } pipe;
  205. /* Lock used for the video devices of both nodes */
  206. struct mutex lock;
  207. struct unicam_node node[UNICAM_MAX_NODES];
  208. };
  209. static inline struct unicam_device *
  210. notifier_to_unicam_device(struct v4l2_async_notifier *notifier)
  211. {
  212. return container_of(notifier, struct unicam_device, notifier);
  213. }
  214. static inline struct unicam_device *
  215. sd_to_unicam_device(struct v4l2_subdev *sd)
  216. {
  217. return container_of(sd, struct unicam_device, subdev.sd);
  218. }
  219. static void unicam_release(struct kref *kref)
  220. {
  221. struct unicam_device *unicam =
  222. container_of(kref, struct unicam_device, kref);
  223. if (unicam->mdev.dev)
  224. media_device_cleanup(&unicam->mdev);
  225. mutex_destroy(&unicam->lock);
  226. kfree(unicam);
  227. }
  228. static struct unicam_device *unicam_get(struct unicam_device *unicam)
  229. {
  230. kref_get(&unicam->kref);
  231. return unicam;
  232. }
  233. static void unicam_put(struct unicam_device *unicam)
  234. {
  235. kref_put(&unicam->kref, unicam_release);
  236. }
  237. /* -----------------------------------------------------------------------------
  238. * Misc helper functions
  239. */
  240. static inline bool unicam_sd_pad_is_source(u32 pad)
  241. {
  242. /* Camera RX has 1 sink pad, and N source pads */
  243. return pad != UNICAM_SD_PAD_SINK;
  244. }
  245. static inline bool is_metadata_node(struct unicam_node *node)
  246. {
  247. return node->video_dev.device_caps & V4L2_CAP_META_CAPTURE;
  248. }
  249. static inline bool is_image_node(struct unicam_node *node)
  250. {
  251. return node->video_dev.device_caps & V4L2_CAP_VIDEO_CAPTURE;
  252. }
  253. /* -----------------------------------------------------------------------------
  254. * Format data table and helper functions
  255. */
  256. static const struct v4l2_mbus_framefmt unicam_default_image_format = {
  257. .width = 640,
  258. .height = 480,
  259. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  260. .field = V4L2_FIELD_NONE,
  261. .colorspace = V4L2_COLORSPACE_SRGB,
  262. .ycbcr_enc = V4L2_YCBCR_ENC_601,
  263. .quantization = V4L2_QUANTIZATION_LIM_RANGE,
  264. .xfer_func = V4L2_XFER_FUNC_SRGB,
  265. .flags = 0,
  266. };
  267. static const struct v4l2_mbus_framefmt unicam_default_meta_format = {
  268. .width = 640,
  269. .height = 2,
  270. .code = MEDIA_BUS_FMT_META_8,
  271. .field = V4L2_FIELD_NONE,
  272. };
  273. static const struct unicam_format_info unicam_image_formats[] = {
  274. /* YUV Formats */
  275. {
  276. .fourcc = V4L2_PIX_FMT_YUYV,
  277. .code = MEDIA_BUS_FMT_YUYV8_1X16,
  278. .depth = 16,
  279. .csi_dt = MIPI_CSI2_DT_YUV422_8B,
  280. }, {
  281. .fourcc = V4L2_PIX_FMT_UYVY,
  282. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  283. .depth = 16,
  284. .csi_dt = MIPI_CSI2_DT_YUV422_8B,
  285. }, {
  286. .fourcc = V4L2_PIX_FMT_YVYU,
  287. .code = MEDIA_BUS_FMT_YVYU8_1X16,
  288. .depth = 16,
  289. .csi_dt = MIPI_CSI2_DT_YUV422_8B,
  290. }, {
  291. .fourcc = V4L2_PIX_FMT_VYUY,
  292. .code = MEDIA_BUS_FMT_VYUY8_1X16,
  293. .depth = 16,
  294. .csi_dt = MIPI_CSI2_DT_YUV422_8B,
  295. }, {
  296. /* RGB Formats */
  297. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  298. .code = MEDIA_BUS_FMT_RGB565_1X16,
  299. .depth = 16,
  300. .csi_dt = MIPI_CSI2_DT_RGB565,
  301. }, {
  302. .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  303. .code = MEDIA_BUS_FMT_RGB888_1X24,
  304. .depth = 24,
  305. .csi_dt = MIPI_CSI2_DT_RGB888,
  306. }, {
  307. .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  308. .code = MEDIA_BUS_FMT_BGR888_1X24,
  309. .depth = 24,
  310. .csi_dt = MIPI_CSI2_DT_RGB888,
  311. }, {
  312. /* Bayer Formats */
  313. .fourcc = V4L2_PIX_FMT_SBGGR8,
  314. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  315. .depth = 8,
  316. .csi_dt = MIPI_CSI2_DT_RAW8,
  317. }, {
  318. .fourcc = V4L2_PIX_FMT_SGBRG8,
  319. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  320. .depth = 8,
  321. .csi_dt = MIPI_CSI2_DT_RAW8,
  322. }, {
  323. .fourcc = V4L2_PIX_FMT_SGRBG8,
  324. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  325. .depth = 8,
  326. .csi_dt = MIPI_CSI2_DT_RAW8,
  327. }, {
  328. .fourcc = V4L2_PIX_FMT_SRGGB8,
  329. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  330. .depth = 8,
  331. .csi_dt = MIPI_CSI2_DT_RAW8,
  332. }, {
  333. .fourcc = V4L2_PIX_FMT_SBGGR10P,
  334. .unpacked_fourcc = V4L2_PIX_FMT_SBGGR10,
  335. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  336. .depth = 10,
  337. .csi_dt = MIPI_CSI2_DT_RAW10,
  338. .unpack = UNICAM_PUM_UNPACK10,
  339. }, {
  340. .fourcc = V4L2_PIX_FMT_SGBRG10P,
  341. .unpacked_fourcc = V4L2_PIX_FMT_SGBRG10,
  342. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  343. .depth = 10,
  344. .csi_dt = MIPI_CSI2_DT_RAW10,
  345. .unpack = UNICAM_PUM_UNPACK10,
  346. }, {
  347. .fourcc = V4L2_PIX_FMT_SGRBG10P,
  348. .unpacked_fourcc = V4L2_PIX_FMT_SGRBG10,
  349. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  350. .depth = 10,
  351. .csi_dt = MIPI_CSI2_DT_RAW10,
  352. .unpack = UNICAM_PUM_UNPACK10,
  353. }, {
  354. .fourcc = V4L2_PIX_FMT_SRGGB10P,
  355. .unpacked_fourcc = V4L2_PIX_FMT_SRGGB10,
  356. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  357. .depth = 10,
  358. .csi_dt = MIPI_CSI2_DT_RAW10,
  359. .unpack = UNICAM_PUM_UNPACK10,
  360. }, {
  361. .fourcc = V4L2_PIX_FMT_SBGGR12P,
  362. .unpacked_fourcc = V4L2_PIX_FMT_SBGGR12,
  363. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  364. .depth = 12,
  365. .csi_dt = MIPI_CSI2_DT_RAW12,
  366. .unpack = UNICAM_PUM_UNPACK12,
  367. }, {
  368. .fourcc = V4L2_PIX_FMT_SGBRG12P,
  369. .unpacked_fourcc = V4L2_PIX_FMT_SGBRG12,
  370. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  371. .depth = 12,
  372. .csi_dt = MIPI_CSI2_DT_RAW12,
  373. .unpack = UNICAM_PUM_UNPACK12,
  374. }, {
  375. .fourcc = V4L2_PIX_FMT_SGRBG12P,
  376. .unpacked_fourcc = V4L2_PIX_FMT_SGRBG12,
  377. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  378. .depth = 12,
  379. .csi_dt = MIPI_CSI2_DT_RAW12,
  380. .unpack = UNICAM_PUM_UNPACK12,
  381. }, {
  382. .fourcc = V4L2_PIX_FMT_SRGGB12P,
  383. .unpacked_fourcc = V4L2_PIX_FMT_SRGGB12,
  384. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  385. .depth = 12,
  386. .csi_dt = MIPI_CSI2_DT_RAW12,
  387. .unpack = UNICAM_PUM_UNPACK12,
  388. }, {
  389. .fourcc = V4L2_PIX_FMT_SBGGR14P,
  390. .unpacked_fourcc = V4L2_PIX_FMT_SBGGR14,
  391. .code = MEDIA_BUS_FMT_SBGGR14_1X14,
  392. .depth = 14,
  393. .csi_dt = MIPI_CSI2_DT_RAW14,
  394. .unpack = UNICAM_PUM_UNPACK14,
  395. }, {
  396. .fourcc = V4L2_PIX_FMT_SGBRG14P,
  397. .unpacked_fourcc = V4L2_PIX_FMT_SGBRG14,
  398. .code = MEDIA_BUS_FMT_SGBRG14_1X14,
  399. .depth = 14,
  400. .csi_dt = MIPI_CSI2_DT_RAW14,
  401. .unpack = UNICAM_PUM_UNPACK14,
  402. }, {
  403. .fourcc = V4L2_PIX_FMT_SGRBG14P,
  404. .unpacked_fourcc = V4L2_PIX_FMT_SGRBG14,
  405. .code = MEDIA_BUS_FMT_SGRBG14_1X14,
  406. .depth = 14,
  407. .csi_dt = MIPI_CSI2_DT_RAW14,
  408. .unpack = UNICAM_PUM_UNPACK14,
  409. }, {
  410. .fourcc = V4L2_PIX_FMT_SRGGB14P,
  411. .unpacked_fourcc = V4L2_PIX_FMT_SRGGB14,
  412. .code = MEDIA_BUS_FMT_SRGGB14_1X14,
  413. .depth = 14,
  414. .csi_dt = MIPI_CSI2_DT_RAW14,
  415. .unpack = UNICAM_PUM_UNPACK14,
  416. }, {
  417. /* 16 bit Bayer formats could be supported. */
  418. /* Greyscale formats */
  419. .fourcc = V4L2_PIX_FMT_GREY,
  420. .code = MEDIA_BUS_FMT_Y8_1X8,
  421. .depth = 8,
  422. .csi_dt = MIPI_CSI2_DT_RAW8,
  423. }, {
  424. .fourcc = V4L2_PIX_FMT_Y10P,
  425. .unpacked_fourcc = V4L2_PIX_FMT_Y10,
  426. .code = MEDIA_BUS_FMT_Y10_1X10,
  427. .depth = 10,
  428. .csi_dt = MIPI_CSI2_DT_RAW10,
  429. .unpack = UNICAM_PUM_UNPACK10,
  430. }, {
  431. .fourcc = V4L2_PIX_FMT_Y12P,
  432. .unpacked_fourcc = V4L2_PIX_FMT_Y12,
  433. .code = MEDIA_BUS_FMT_Y12_1X12,
  434. .depth = 12,
  435. .csi_dt = MIPI_CSI2_DT_RAW12,
  436. .unpack = UNICAM_PUM_UNPACK12,
  437. }, {
  438. .fourcc = V4L2_PIX_FMT_Y14P,
  439. .unpacked_fourcc = V4L2_PIX_FMT_Y14,
  440. .code = MEDIA_BUS_FMT_Y14_1X14,
  441. .depth = 14,
  442. .csi_dt = MIPI_CSI2_DT_RAW14,
  443. .unpack = UNICAM_PUM_UNPACK14,
  444. },
  445. };
  446. static const struct unicam_format_info unicam_meta_formats[] = {
  447. {
  448. .fourcc = V4L2_META_FMT_GENERIC_8,
  449. .code = MEDIA_BUS_FMT_META_8,
  450. .depth = 8,
  451. }, {
  452. .fourcc = V4L2_META_FMT_GENERIC_CSI2_10,
  453. .code = MEDIA_BUS_FMT_META_10,
  454. .depth = 10,
  455. }, {
  456. .fourcc = V4L2_META_FMT_GENERIC_CSI2_12,
  457. .code = MEDIA_BUS_FMT_META_12,
  458. .depth = 12,
  459. }, {
  460. .fourcc = V4L2_META_FMT_GENERIC_CSI2_14,
  461. .code = MEDIA_BUS_FMT_META_14,
  462. .depth = 14,
  463. },
  464. };
  465. /* Format setup functions */
  466. static const struct unicam_format_info *
  467. unicam_find_format_by_code(u32 code, u32 pad)
  468. {
  469. const struct unicam_format_info *formats;
  470. unsigned int num_formats;
  471. unsigned int i;
  472. if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) {
  473. formats = unicam_image_formats;
  474. num_formats = ARRAY_SIZE(unicam_image_formats);
  475. } else {
  476. formats = unicam_meta_formats;
  477. num_formats = ARRAY_SIZE(unicam_meta_formats);
  478. }
  479. for (i = 0; i < num_formats; i++) {
  480. if (formats[i].code == code)
  481. return &formats[i];
  482. }
  483. return NULL;
  484. }
  485. static const struct unicam_format_info *
  486. unicam_find_format_by_fourcc(u32 fourcc, u32 pad)
  487. {
  488. const struct unicam_format_info *formats;
  489. unsigned int num_formats;
  490. unsigned int i;
  491. if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) {
  492. formats = unicam_image_formats;
  493. num_formats = ARRAY_SIZE(unicam_image_formats);
  494. } else {
  495. formats = unicam_meta_formats;
  496. num_formats = ARRAY_SIZE(unicam_meta_formats);
  497. }
  498. for (i = 0; i < num_formats; ++i) {
  499. if (formats[i].fourcc == fourcc)
  500. return &formats[i];
  501. }
  502. return NULL;
  503. }
  504. static void unicam_calc_image_size_bpl(struct unicam_device *unicam,
  505. const struct unicam_format_info *fmtinfo,
  506. struct v4l2_pix_format *pix)
  507. {
  508. u32 min_bpl;
  509. v4l_bound_align_image(&pix->width, UNICAM_IMAGE_MIN_WIDTH,
  510. UNICAM_IMAGE_MAX_WIDTH, 2,
  511. &pix->height, UNICAM_IMAGE_MIN_HEIGHT,
  512. UNICAM_IMAGE_MAX_HEIGHT, 0, 0);
  513. /* Unpacking always goes to 16bpp */
  514. if (pix->pixelformat == fmtinfo->unpacked_fourcc)
  515. min_bpl = pix->width * 2;
  516. else
  517. min_bpl = pix->width * fmtinfo->depth / 8;
  518. min_bpl = ALIGN(min_bpl, UNICAM_IMAGE_BPL_ALIGNMENT);
  519. pix->bytesperline = ALIGN(pix->bytesperline, UNICAM_IMAGE_BPL_ALIGNMENT);
  520. pix->bytesperline = clamp_t(unsigned int, pix->bytesperline, min_bpl,
  521. UNICAM_IMAGE_MAX_BPL);
  522. pix->sizeimage = pix->height * pix->bytesperline;
  523. }
  524. static void unicam_calc_meta_size_bpl(struct unicam_device *unicam,
  525. const struct unicam_format_info *fmtinfo,
  526. struct v4l2_meta_format *meta)
  527. {
  528. v4l_bound_align_image(&meta->width, UNICAM_META_MIN_WIDTH,
  529. UNICAM_META_MAX_WIDTH, 0,
  530. &meta->height, UNICAM_META_MIN_HEIGHT,
  531. UNICAM_META_MAX_HEIGHT, 0, 0);
  532. meta->bytesperline = ALIGN(meta->width * fmtinfo->depth / 8,
  533. UNICAM_DMA_BPL_ALIGNMENT);
  534. meta->buffersize = meta->height * meta->bytesperline;
  535. }
  536. /* -----------------------------------------------------------------------------
  537. * Hardware handling
  538. */
  539. static inline void unicam_clk_write(struct unicam_device *unicam, u32 val)
  540. {
  541. /* Pass the CM_PASSWORD along with the value. */
  542. writel(val | 0x5a000000, unicam->clk_gate_base);
  543. }
  544. static inline u32 unicam_reg_read(struct unicam_device *unicam, u32 offset)
  545. {
  546. return readl(unicam->base + offset);
  547. }
  548. static inline void unicam_reg_write(struct unicam_device *unicam, u32 offset, u32 val)
  549. {
  550. writel(val, unicam->base + offset);
  551. }
  552. static inline int unicam_get_field(u32 value, u32 mask)
  553. {
  554. return (value & mask) >> __ffs(mask);
  555. }
  556. static inline void unicam_set_field(u32 *valp, u32 field, u32 mask)
  557. {
  558. u32 val = *valp;
  559. val &= ~mask;
  560. val |= (field << __ffs(mask)) & mask;
  561. *valp = val;
  562. }
  563. static inline void unicam_reg_write_field(struct unicam_device *unicam, u32 offset,
  564. u32 field, u32 mask)
  565. {
  566. u32 val = unicam_reg_read(unicam, offset);
  567. unicam_set_field(&val, field, mask);
  568. unicam_reg_write(unicam, offset, val);
  569. }
  570. static void unicam_wr_dma_addr(struct unicam_node *node,
  571. struct unicam_buffer *buf)
  572. {
  573. dma_addr_t endaddr = buf->dma_addr + buf->size;
  574. if (node->id == UNICAM_IMAGE_NODE) {
  575. unicam_reg_write(node->dev, UNICAM_IBSA0, buf->dma_addr);
  576. unicam_reg_write(node->dev, UNICAM_IBEA0, endaddr);
  577. } else {
  578. unicam_reg_write(node->dev, UNICAM_DBSA0, buf->dma_addr);
  579. unicam_reg_write(node->dev, UNICAM_DBEA0, endaddr);
  580. }
  581. }
  582. static unsigned int unicam_get_lines_done(struct unicam_device *unicam)
  583. {
  584. struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE];
  585. unsigned int stride = node->fmt.fmt.pix.bytesperline;
  586. struct unicam_buffer *frm = node->cur_frm;
  587. dma_addr_t cur_addr;
  588. if (!frm)
  589. return 0;
  590. cur_addr = unicam_reg_read(unicam, UNICAM_IBWP);
  591. return (unsigned int)(cur_addr - frm->dma_addr) / stride;
  592. }
  593. static void unicam_schedule_next_buffer(struct unicam_node *node)
  594. {
  595. struct unicam_buffer *buf;
  596. buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list);
  597. node->next_frm = buf;
  598. list_del(&buf->list);
  599. unicam_wr_dma_addr(node, buf);
  600. }
  601. static void unicam_schedule_dummy_buffer(struct unicam_node *node)
  602. {
  603. int node_id = is_image_node(node) ? UNICAM_IMAGE_NODE : UNICAM_METADATA_NODE;
  604. dev_dbg(node->dev->dev, "Scheduling dummy buffer for node %d\n", node_id);
  605. unicam_wr_dma_addr(node, &node->dummy_buf);
  606. node->next_frm = NULL;
  607. }
  608. static void unicam_process_buffer_complete(struct unicam_node *node,
  609. unsigned int sequence)
  610. {
  611. node->cur_frm->vb.field = node->fmt.fmt.pix.field;
  612. node->cur_frm->vb.sequence = sequence;
  613. vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  614. }
  615. static void unicam_queue_event_sof(struct unicam_device *unicam)
  616. {
  617. struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE];
  618. struct v4l2_event event = {
  619. .type = V4L2_EVENT_FRAME_SYNC,
  620. .u.frame_sync.frame_sequence = unicam->sequence,
  621. };
  622. v4l2_event_queue(&node->video_dev, &event);
  623. }
  624. static irqreturn_t unicam_isr(int irq, void *dev)
  625. {
  626. struct unicam_device *unicam = dev;
  627. unsigned int lines_done = unicam_get_lines_done(dev);
  628. unsigned int sequence = unicam->sequence;
  629. unsigned int i;
  630. u32 ista, sta;
  631. bool fe;
  632. u64 ts;
  633. sta = unicam_reg_read(unicam, UNICAM_STA);
  634. /* Write value back to clear the interrupts */
  635. unicam_reg_write(unicam, UNICAM_STA, sta);
  636. ista = unicam_reg_read(unicam, UNICAM_ISTA);
  637. /* Write value back to clear the interrupts */
  638. unicam_reg_write(unicam, UNICAM_ISTA, ista);
  639. dev_dbg(unicam->dev, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d\n",
  640. ista, sta, sequence, lines_done);
  641. if (!(sta & (UNICAM_IS | UNICAM_PI0)))
  642. return IRQ_HANDLED;
  643. /*
  644. * Look for either the Frame End interrupt or the Packet Capture status
  645. * to signal a frame end.
  646. */
  647. fe = ista & UNICAM_FEI || sta & UNICAM_PI0;
  648. /*
  649. * We must run the frame end handler first. If we have a valid next_frm
  650. * and we get a simultaneout FE + FS interrupt, running the FS handler
  651. * first would null out the next_frm ptr and we would have lost the
  652. * buffer forever.
  653. */
  654. if (fe) {
  655. /*
  656. * Ensure we have swapped buffers already as we can't
  657. * stop the peripheral. If no buffer is available, use a
  658. * dummy buffer to dump out frames until we get a new buffer
  659. * to use.
  660. */
  661. for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
  662. struct unicam_node *node = &unicam->node[i];
  663. if (!vb2_start_streaming_called(&node->buffer_queue))
  664. continue;
  665. /*
  666. * If cur_frm == next_frm, it means we have not had
  667. * a chance to swap buffers, likely due to having
  668. * multiple interrupts occurring simultaneously (like FE
  669. * + FS + LS). In this case, we cannot signal the buffer
  670. * as complete, as the HW will reuse that buffer.
  671. */
  672. if (node->cur_frm && node->cur_frm != node->next_frm)
  673. unicam_process_buffer_complete(node, sequence);
  674. node->cur_frm = node->next_frm;
  675. }
  676. unicam->sequence++;
  677. }
  678. if (ista & UNICAM_FSI) {
  679. /*
  680. * Timestamp is to be when the first data byte was captured,
  681. * aka frame start.
  682. */
  683. ts = ktime_get_ns();
  684. for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
  685. struct unicam_node *node = &unicam->node[i];
  686. if (!vb2_start_streaming_called(&node->buffer_queue))
  687. continue;
  688. if (node->cur_frm)
  689. node->cur_frm->vb.vb2_buf.timestamp = ts;
  690. else
  691. dev_dbg(unicam->v4l2_dev.dev,
  692. "ISR: [%d] Dropping frame, buffer not available at FS\n",
  693. i);
  694. /*
  695. * Set the next frame output to go to a dummy frame
  696. * if we have not managed to obtain another frame
  697. * from the queue.
  698. */
  699. unicam_schedule_dummy_buffer(node);
  700. }
  701. unicam_queue_event_sof(unicam);
  702. }
  703. /*
  704. * Cannot swap buffer at frame end, there may be a race condition
  705. * where the HW does not actually swap it if the new frame has
  706. * already started.
  707. */
  708. if (ista & (UNICAM_FSI | UNICAM_LCI) && !fe) {
  709. for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
  710. struct unicam_node *node = &unicam->node[i];
  711. if (!vb2_start_streaming_called(&node->buffer_queue))
  712. continue;
  713. spin_lock(&node->dma_queue_lock);
  714. if (!list_empty(&node->dma_queue) && !node->next_frm)
  715. unicam_schedule_next_buffer(node);
  716. spin_unlock(&node->dma_queue_lock);
  717. }
  718. }
  719. return IRQ_HANDLED;
  720. }
  721. static void unicam_set_packing_config(struct unicam_device *unicam,
  722. const struct unicam_format_info *fmtinfo)
  723. {
  724. struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE];
  725. u32 pack, unpack;
  726. u32 val;
  727. if (node->fmt.fmt.pix.pixelformat == fmtinfo->fourcc) {
  728. unpack = UNICAM_PUM_NONE;
  729. pack = UNICAM_PPM_NONE;
  730. } else {
  731. unpack = fmtinfo->unpack;
  732. /* Repacking is always to 16bpp */
  733. pack = UNICAM_PPM_PACK16;
  734. }
  735. val = 0;
  736. unicam_set_field(&val, unpack, UNICAM_PUM_MASK);
  737. unicam_set_field(&val, pack, UNICAM_PPM_MASK);
  738. unicam_reg_write(unicam, UNICAM_IPIPE, val);
  739. }
  740. static void unicam_cfg_image_id(struct unicam_device *unicam, u8 vc, u8 dt)
  741. {
  742. if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) {
  743. /* CSI2 mode */
  744. unicam_reg_write(unicam, UNICAM_IDI0, (vc << 6) | dt);
  745. } else {
  746. /* CCP2 mode */
  747. unicam_reg_write(unicam, UNICAM_IDI0, 0x80 | dt);
  748. }
  749. }
  750. static void unicam_enable_ed(struct unicam_device *unicam)
  751. {
  752. u32 val = unicam_reg_read(unicam, UNICAM_DCS);
  753. unicam_set_field(&val, 2, UNICAM_EDL_MASK);
  754. /* Do not wrap at the end of the embedded data buffer */
  755. unicam_set_field(&val, 0, UNICAM_DBOB);
  756. unicam_reg_write(unicam, UNICAM_DCS, val);
  757. }
  758. static int unicam_get_image_vc_dt(struct unicam_device *unicam,
  759. struct v4l2_subdev_state *state,
  760. u8 *vc, u8 *dt)
  761. {
  762. struct v4l2_mbus_frame_desc fd;
  763. u32 stream;
  764. int ret;
  765. ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
  766. UNICAM_SD_PAD_SOURCE_IMAGE,
  767. 0, NULL, &stream);
  768. if (ret)
  769. return ret;
  770. ret = v4l2_subdev_call(unicam->sensor.subdev, pad, get_frame_desc,
  771. unicam->sensor.pad->index, &fd);
  772. if (ret)
  773. return ret;
  774. /* Only CSI-2 supports DTs. */
  775. if (fd.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
  776. return -EINVAL;
  777. for (unsigned int i = 0; i < fd.num_entries; ++i) {
  778. const struct v4l2_mbus_frame_desc_entry *fde = &fd.entry[i];
  779. if (fde->stream == stream) {
  780. *vc = fde->bus.csi2.vc;
  781. *dt = fde->bus.csi2.dt;
  782. return 0;
  783. }
  784. }
  785. return -EINVAL;
  786. }
  787. static void unicam_start_rx(struct unicam_device *unicam,
  788. struct v4l2_subdev_state *state)
  789. {
  790. struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE];
  791. const struct unicam_format_info *fmtinfo;
  792. const struct v4l2_mbus_framefmt *fmt;
  793. unsigned int line_int_freq;
  794. u8 vc, dt;
  795. u32 val;
  796. int ret;
  797. fmt = v4l2_subdev_state_get_format(state, UNICAM_SD_PAD_SOURCE_IMAGE, 0);
  798. fmtinfo = unicam_find_format_by_code(fmt->code,
  799. UNICAM_SD_PAD_SOURCE_IMAGE);
  800. if (WARN_ON(!fmtinfo))
  801. return;
  802. /*
  803. * Enable lane clocks. The register is structured as follows:
  804. *
  805. * [9:8] - DAT3
  806. * [7:6] - DAT2
  807. * [5:4] - DAT1
  808. * [3:2] - DAT0
  809. * [1:0] - CLK
  810. *
  811. * Enabled lane must be set to b01, and disabled lanes to b00. The clock
  812. * lane is always enabled.
  813. */
  814. val = 0x155 & GENMASK(unicam->pipe.num_data_lanes * 2 + 1, 0);
  815. unicam_clk_write(unicam, val);
  816. /* Basic init */
  817. unicam_reg_write(unicam, UNICAM_CTRL, UNICAM_MEM);
  818. /* Enable analogue control, and leave in reset. */
  819. val = UNICAM_AR;
  820. unicam_set_field(&val, 7, UNICAM_CTATADJ_MASK);
  821. unicam_set_field(&val, 7, UNICAM_PTATADJ_MASK);
  822. unicam_reg_write(unicam, UNICAM_ANA, val);
  823. usleep_range(1000, 2000);
  824. /* Come out of reset */
  825. unicam_reg_write_field(unicam, UNICAM_ANA, 0, UNICAM_AR);
  826. /* Peripheral reset */
  827. unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPR);
  828. unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPR);
  829. unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPE);
  830. /* Enable Rx control. */
  831. val = unicam_reg_read(unicam, UNICAM_CTRL);
  832. if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) {
  833. unicam_set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
  834. unicam_set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
  835. } else {
  836. unicam_set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
  837. unicam_set_field(&val, unicam->bus_flags, UNICAM_DCM_MASK);
  838. }
  839. /* Packet framer timeout */
  840. unicam_set_field(&val, 0xf, UNICAM_PFT_MASK);
  841. unicam_set_field(&val, 128, UNICAM_OET_MASK);
  842. unicam_reg_write(unicam, UNICAM_CTRL, val);
  843. unicam_reg_write(unicam, UNICAM_IHWIN, 0);
  844. unicam_reg_write(unicam, UNICAM_IVWIN, 0);
  845. /* AXI bus access QoS setup */
  846. val = unicam_reg_read(unicam, UNICAM_PRI);
  847. unicam_set_field(&val, 0, UNICAM_BL_MASK);
  848. unicam_set_field(&val, 0, UNICAM_BS_MASK);
  849. unicam_set_field(&val, 0xe, UNICAM_PP_MASK);
  850. unicam_set_field(&val, 8, UNICAM_NP_MASK);
  851. unicam_set_field(&val, 2, UNICAM_PT_MASK);
  852. unicam_set_field(&val, 1, UNICAM_PE);
  853. unicam_reg_write(unicam, UNICAM_PRI, val);
  854. unicam_reg_write_field(unicam, UNICAM_ANA, 0, UNICAM_DDL);
  855. val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_IBOB;
  856. line_int_freq = max(fmt->height >> 2, 128);
  857. unicam_set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
  858. unicam_reg_write(unicam, UNICAM_ICTL, val);
  859. unicam_reg_write(unicam, UNICAM_STA, UNICAM_STA_MASK_ALL);
  860. unicam_reg_write(unicam, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
  861. /* tclk_term_en */
  862. unicam_reg_write_field(unicam, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
  863. /* tclk_settle */
  864. unicam_reg_write_field(unicam, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
  865. /* td_term_en */
  866. unicam_reg_write_field(unicam, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
  867. /* ths_settle */
  868. unicam_reg_write_field(unicam, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
  869. /* trx_enable */
  870. unicam_reg_write_field(unicam, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
  871. unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_SOE);
  872. /* Packet compare setup - required to avoid missing frame ends */
  873. val = 0;
  874. unicam_set_field(&val, 1, UNICAM_PCE);
  875. unicam_set_field(&val, 1, UNICAM_GI);
  876. unicam_set_field(&val, 1, UNICAM_CPH);
  877. unicam_set_field(&val, 0, UNICAM_PCVC_MASK);
  878. unicam_set_field(&val, 1, UNICAM_PCDT_MASK);
  879. unicam_reg_write(unicam, UNICAM_CMP0, val);
  880. /* Enable clock lane and set up terminations */
  881. val = 0;
  882. if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) {
  883. /* CSI2 */
  884. unicam_set_field(&val, 1, UNICAM_CLE);
  885. unicam_set_field(&val, 1, UNICAM_CLLPE);
  886. if (!(unicam->bus_flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK)) {
  887. unicam_set_field(&val, 1, UNICAM_CLTRE);
  888. unicam_set_field(&val, 1, UNICAM_CLHSE);
  889. }
  890. } else {
  891. /* CCP2 */
  892. unicam_set_field(&val, 1, UNICAM_CLE);
  893. unicam_set_field(&val, 1, UNICAM_CLHSE);
  894. unicam_set_field(&val, 1, UNICAM_CLTRE);
  895. }
  896. unicam_reg_write(unicam, UNICAM_CLK, val);
  897. /*
  898. * Enable required data lanes with appropriate terminations.
  899. * The same value needs to be written to UNICAM_DATn registers for
  900. * the active lanes, and 0 for inactive ones.
  901. */
  902. val = 0;
  903. if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) {
  904. /* CSI2 */
  905. unicam_set_field(&val, 1, UNICAM_DLE);
  906. unicam_set_field(&val, 1, UNICAM_DLLPE);
  907. if (!(unicam->bus_flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK)) {
  908. unicam_set_field(&val, 1, UNICAM_DLTRE);
  909. unicam_set_field(&val, 1, UNICAM_DLHSE);
  910. }
  911. } else {
  912. /* CCP2 */
  913. unicam_set_field(&val, 1, UNICAM_DLE);
  914. unicam_set_field(&val, 1, UNICAM_DLHSE);
  915. unicam_set_field(&val, 1, UNICAM_DLTRE);
  916. }
  917. unicam_reg_write(unicam, UNICAM_DAT0, val);
  918. if (unicam->pipe.num_data_lanes == 1)
  919. val = 0;
  920. unicam_reg_write(unicam, UNICAM_DAT1, val);
  921. if (unicam->max_data_lanes > 2) {
  922. /*
  923. * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
  924. * instance supports more than 2 data lanes.
  925. */
  926. if (unicam->pipe.num_data_lanes == 2)
  927. val = 0;
  928. unicam_reg_write(unicam, UNICAM_DAT2, val);
  929. if (unicam->pipe.num_data_lanes == 3)
  930. val = 0;
  931. unicam_reg_write(unicam, UNICAM_DAT3, val);
  932. }
  933. unicam_reg_write(unicam, UNICAM_IBLS,
  934. node->fmt.fmt.pix.bytesperline);
  935. unicam_wr_dma_addr(node, node->cur_frm);
  936. unicam_set_packing_config(unicam, fmtinfo);
  937. ret = unicam_get_image_vc_dt(unicam, state, &vc, &dt);
  938. if (ret) {
  939. /*
  940. * If the source doesn't support frame descriptors, default to
  941. * VC 0 and use the DT corresponding to the format.
  942. */
  943. vc = 0;
  944. dt = fmtinfo->csi_dt;
  945. }
  946. unicam_cfg_image_id(unicam, vc, dt);
  947. val = unicam_reg_read(unicam, UNICAM_MISC);
  948. unicam_set_field(&val, 1, UNICAM_FL0);
  949. unicam_set_field(&val, 1, UNICAM_FL1);
  950. unicam_reg_write(unicam, UNICAM_MISC, val);
  951. /* Enable peripheral */
  952. unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPE);
  953. /* Load image pointers */
  954. unicam_reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
  955. /*
  956. * Enable trigger only for the first frame to
  957. * sync correctly to the FS from the source.
  958. */
  959. unicam_reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_TFC);
  960. }
  961. static void unicam_start_metadata(struct unicam_device *unicam)
  962. {
  963. struct unicam_node *node = &unicam->node[UNICAM_METADATA_NODE];
  964. unicam_enable_ed(unicam);
  965. unicam_wr_dma_addr(node, node->cur_frm);
  966. unicam_reg_write_field(unicam, UNICAM_DCS, 1, UNICAM_LDP);
  967. }
  968. static void unicam_disable(struct unicam_device *unicam)
  969. {
  970. /* Analogue lane control disable */
  971. unicam_reg_write_field(unicam, UNICAM_ANA, 1, UNICAM_DDL);
  972. /* Stop the output engine */
  973. unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_SOE);
  974. /* Disable the data lanes. */
  975. unicam_reg_write(unicam, UNICAM_DAT0, 0);
  976. unicam_reg_write(unicam, UNICAM_DAT1, 0);
  977. if (unicam->max_data_lanes > 2) {
  978. unicam_reg_write(unicam, UNICAM_DAT2, 0);
  979. unicam_reg_write(unicam, UNICAM_DAT3, 0);
  980. }
  981. /* Peripheral reset */
  982. unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPR);
  983. usleep_range(50, 100);
  984. unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPR);
  985. /* Disable peripheral */
  986. unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPE);
  987. /* Clear ED setup */
  988. unicam_reg_write(unicam, UNICAM_DCS, 0);
  989. /* Disable all lane clocks */
  990. unicam_clk_write(unicam, 0);
  991. }
  992. /* -----------------------------------------------------------------------------
  993. * V4L2 subdev operations
  994. */
  995. static int __unicam_subdev_set_routing(struct v4l2_subdev *sd,
  996. struct v4l2_subdev_state *state,
  997. struct v4l2_subdev_krouting *routing)
  998. {
  999. struct v4l2_subdev_route *route;
  1000. int ret;
  1001. ret = v4l2_subdev_routing_validate(sd, routing,
  1002. V4L2_SUBDEV_ROUTING_ONLY_1_TO_1);
  1003. if (ret)
  1004. return ret;
  1005. ret = v4l2_subdev_set_routing(sd, state, routing);
  1006. if (ret)
  1007. return ret;
  1008. for_each_active_route(&state->routing, route) {
  1009. const struct v4l2_mbus_framefmt *def_fmt;
  1010. struct v4l2_mbus_framefmt *fmt;
  1011. if (route->source_pad == UNICAM_SD_PAD_SOURCE_IMAGE)
  1012. def_fmt = &unicam_default_image_format;
  1013. else
  1014. def_fmt = &unicam_default_meta_format;
  1015. fmt = v4l2_subdev_state_get_format(state, route->sink_pad,
  1016. route->sink_stream);
  1017. *fmt = *def_fmt;
  1018. fmt = v4l2_subdev_state_get_format(state, route->source_pad,
  1019. route->source_stream);
  1020. *fmt = *def_fmt;
  1021. }
  1022. return 0;
  1023. }
  1024. static int unicam_subdev_init_state(struct v4l2_subdev *sd,
  1025. struct v4l2_subdev_state *state)
  1026. {
  1027. struct v4l2_subdev_route routes[] = {
  1028. {
  1029. .sink_pad = UNICAM_SD_PAD_SINK,
  1030. .sink_stream = 0,
  1031. .source_pad = UNICAM_SD_PAD_SOURCE_IMAGE,
  1032. .source_stream = 0,
  1033. .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
  1034. },
  1035. };
  1036. struct v4l2_subdev_krouting routing = {
  1037. .len_routes = ARRAY_SIZE(routes),
  1038. .num_routes = ARRAY_SIZE(routes),
  1039. .routes = routes,
  1040. };
  1041. /* Initialize routing to single route to the fist source pad. */
  1042. return __unicam_subdev_set_routing(sd, state, &routing);
  1043. }
  1044. static int unicam_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  1045. struct v4l2_subdev_state *state,
  1046. struct v4l2_subdev_mbus_code_enum *code)
  1047. {
  1048. u32 pad, stream;
  1049. int ret;
  1050. ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
  1051. code->pad, code->stream,
  1052. &pad, &stream);
  1053. if (ret)
  1054. return ret;
  1055. if (unicam_sd_pad_is_source(code->pad)) {
  1056. /* No transcoding, source and sink codes must match. */
  1057. const struct v4l2_mbus_framefmt *fmt;
  1058. fmt = v4l2_subdev_state_get_format(state, pad, stream);
  1059. if (!fmt)
  1060. return -EINVAL;
  1061. if (code->index > 0)
  1062. return -EINVAL;
  1063. code->code = fmt->code;
  1064. } else {
  1065. const struct unicam_format_info *formats;
  1066. unsigned int num_formats;
  1067. if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) {
  1068. formats = unicam_image_formats;
  1069. num_formats = ARRAY_SIZE(unicam_image_formats);
  1070. } else {
  1071. formats = unicam_meta_formats;
  1072. num_formats = ARRAY_SIZE(unicam_meta_formats);
  1073. }
  1074. if (code->index >= num_formats)
  1075. return -EINVAL;
  1076. code->code = formats[code->index].code;
  1077. }
  1078. return 0;
  1079. }
  1080. static int unicam_subdev_enum_frame_size(struct v4l2_subdev *sd,
  1081. struct v4l2_subdev_state *state,
  1082. struct v4l2_subdev_frame_size_enum *fse)
  1083. {
  1084. u32 pad, stream;
  1085. int ret;
  1086. if (fse->index > 0)
  1087. return -EINVAL;
  1088. ret = v4l2_subdev_routing_find_opposite_end(&state->routing, fse->pad,
  1089. fse->stream, &pad,
  1090. &stream);
  1091. if (ret)
  1092. return ret;
  1093. if (unicam_sd_pad_is_source(fse->pad)) {
  1094. /* No transcoding, source and sink formats must match. */
  1095. const struct v4l2_mbus_framefmt *fmt;
  1096. fmt = v4l2_subdev_state_get_format(state, pad, stream);
  1097. if (!fmt)
  1098. return -EINVAL;
  1099. if (fse->code != fmt->code)
  1100. return -EINVAL;
  1101. fse->min_width = fmt->width;
  1102. fse->max_width = fmt->width;
  1103. fse->min_height = fmt->height;
  1104. fse->max_height = fmt->height;
  1105. } else {
  1106. const struct unicam_format_info *fmtinfo;
  1107. fmtinfo = unicam_find_format_by_code(fse->code, pad);
  1108. if (!fmtinfo)
  1109. return -EINVAL;
  1110. if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) {
  1111. fse->min_width = UNICAM_IMAGE_MIN_WIDTH;
  1112. fse->max_width = UNICAM_IMAGE_MAX_WIDTH;
  1113. fse->min_height = UNICAM_IMAGE_MIN_HEIGHT;
  1114. fse->max_height = UNICAM_IMAGE_MAX_HEIGHT;
  1115. } else {
  1116. fse->min_width = UNICAM_META_MIN_WIDTH;
  1117. fse->max_width = UNICAM_META_MAX_WIDTH;
  1118. fse->min_height = UNICAM_META_MIN_HEIGHT;
  1119. fse->max_height = UNICAM_META_MAX_HEIGHT;
  1120. }
  1121. }
  1122. return 0;
  1123. }
  1124. static int unicam_subdev_set_format(struct v4l2_subdev *sd,
  1125. struct v4l2_subdev_state *state,
  1126. struct v4l2_subdev_format *format)
  1127. {
  1128. struct unicam_device *unicam = sd_to_unicam_device(sd);
  1129. struct v4l2_mbus_framefmt *sink_format, *source_format;
  1130. const struct unicam_format_info *fmtinfo;
  1131. u32 source_pad, source_stream;
  1132. int ret;
  1133. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
  1134. unicam->subdev.enabled_streams)
  1135. return -EBUSY;
  1136. /* No transcoding, source and sink formats must match. */
  1137. if (unicam_sd_pad_is_source(format->pad))
  1138. return v4l2_subdev_get_fmt(sd, state, format);
  1139. /*
  1140. * Allowed formats for the stream on the sink pad depend on what source
  1141. * pad the stream is routed to. Find the corresponding source pad and
  1142. * use it to validate the media bus code.
  1143. */
  1144. ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
  1145. format->pad, format->stream,
  1146. &source_pad, &source_stream);
  1147. if (ret)
  1148. return ret;
  1149. fmtinfo = unicam_find_format_by_code(format->format.code, source_pad);
  1150. if (!fmtinfo) {
  1151. fmtinfo = source_pad == UNICAM_SD_PAD_SOURCE_IMAGE
  1152. ? &unicam_image_formats[0] : &unicam_meta_formats[0];
  1153. format->format.code = fmtinfo->code;
  1154. }
  1155. if (source_pad == UNICAM_SD_PAD_SOURCE_IMAGE) {
  1156. format->format.width = clamp_t(unsigned int,
  1157. format->format.width,
  1158. UNICAM_IMAGE_MIN_WIDTH,
  1159. UNICAM_IMAGE_MAX_WIDTH);
  1160. format->format.height = clamp_t(unsigned int,
  1161. format->format.height,
  1162. UNICAM_IMAGE_MIN_HEIGHT,
  1163. UNICAM_IMAGE_MAX_HEIGHT);
  1164. format->format.field = V4L2_FIELD_NONE;
  1165. } else {
  1166. format->format.width = clamp_t(unsigned int,
  1167. format->format.width,
  1168. UNICAM_META_MIN_WIDTH,
  1169. UNICAM_META_MAX_WIDTH);
  1170. format->format.height = clamp_t(unsigned int,
  1171. format->format.height,
  1172. UNICAM_META_MIN_HEIGHT,
  1173. UNICAM_META_MAX_HEIGHT);
  1174. format->format.field = V4L2_FIELD_NONE;
  1175. /* Colorspace don't apply to metadata. */
  1176. format->format.colorspace = 0;
  1177. format->format.ycbcr_enc = 0;
  1178. format->format.quantization = 0;
  1179. format->format.xfer_func = 0;
  1180. }
  1181. sink_format = v4l2_subdev_state_get_format(state, format->pad,
  1182. format->stream);
  1183. source_format = v4l2_subdev_state_get_format(state, source_pad,
  1184. source_stream);
  1185. *sink_format = format->format;
  1186. *source_format = format->format;
  1187. return 0;
  1188. }
  1189. static int unicam_subdev_set_routing(struct v4l2_subdev *sd,
  1190. struct v4l2_subdev_state *state,
  1191. enum v4l2_subdev_format_whence which,
  1192. struct v4l2_subdev_krouting *routing)
  1193. {
  1194. struct unicam_device *unicam = sd_to_unicam_device(sd);
  1195. if (which == V4L2_SUBDEV_FORMAT_ACTIVE && unicam->subdev.enabled_streams)
  1196. return -EBUSY;
  1197. return __unicam_subdev_set_routing(sd, state, routing);
  1198. }
  1199. static int unicam_sd_enable_streams(struct v4l2_subdev *sd,
  1200. struct v4l2_subdev_state *state, u32 pad,
  1201. u64 streams_mask)
  1202. {
  1203. struct unicam_device *unicam = sd_to_unicam_device(sd);
  1204. u32 other_pad, other_stream;
  1205. int ret;
  1206. if (!unicam->subdev.enabled_streams) {
  1207. /* Configure and start Unicam. */
  1208. unicam->sequence = 0;
  1209. if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE))
  1210. unicam_start_metadata(unicam);
  1211. unicam_start_rx(unicam, state);
  1212. }
  1213. ret = v4l2_subdev_routing_find_opposite_end(&state->routing, pad, 0,
  1214. &other_pad, &other_stream);
  1215. if (ret)
  1216. return ret;
  1217. ret = v4l2_subdev_enable_streams(unicam->sensor.subdev,
  1218. unicam->sensor.pad->index,
  1219. BIT(other_stream));
  1220. if (ret) {
  1221. dev_err(unicam->dev, "stream on failed in subdev\n");
  1222. return ret;
  1223. }
  1224. unicam->subdev.enabled_streams |= BIT(other_stream);
  1225. return 0;
  1226. }
  1227. static int unicam_sd_disable_streams(struct v4l2_subdev *sd,
  1228. struct v4l2_subdev_state *state, u32 pad,
  1229. u64 streams_mask)
  1230. {
  1231. struct unicam_device *unicam = sd_to_unicam_device(sd);
  1232. u32 other_pad, other_stream;
  1233. int ret;
  1234. ret = v4l2_subdev_routing_find_opposite_end(&state->routing, pad, 0,
  1235. &other_pad, &other_stream);
  1236. if (ret)
  1237. return ret;
  1238. v4l2_subdev_disable_streams(unicam->sensor.subdev,
  1239. unicam->sensor.pad->index,
  1240. BIT(other_stream));
  1241. unicam->subdev.enabled_streams &= ~BIT(other_stream);
  1242. if (!unicam->subdev.enabled_streams)
  1243. unicam_disable(unicam);
  1244. return 0;
  1245. }
  1246. static const struct v4l2_subdev_pad_ops unicam_subdev_pad_ops = {
  1247. .enum_mbus_code = unicam_subdev_enum_mbus_code,
  1248. .enum_frame_size = unicam_subdev_enum_frame_size,
  1249. .get_fmt = v4l2_subdev_get_fmt,
  1250. .set_fmt = unicam_subdev_set_format,
  1251. .set_routing = unicam_subdev_set_routing,
  1252. .enable_streams = unicam_sd_enable_streams,
  1253. .disable_streams = unicam_sd_disable_streams,
  1254. };
  1255. static const struct v4l2_subdev_ops unicam_subdev_ops = {
  1256. .pad = &unicam_subdev_pad_ops,
  1257. };
  1258. static const struct v4l2_subdev_internal_ops unicam_subdev_internal_ops = {
  1259. .init_state = unicam_subdev_init_state,
  1260. };
  1261. static const struct media_entity_operations unicam_subdev_media_ops = {
  1262. .link_validate = v4l2_subdev_link_validate,
  1263. .has_pad_interdep = v4l2_subdev_has_pad_interdep,
  1264. };
  1265. static int unicam_subdev_init(struct unicam_device *unicam)
  1266. {
  1267. struct v4l2_subdev *sd = &unicam->subdev.sd;
  1268. int ret;
  1269. v4l2_subdev_init(sd, &unicam_subdev_ops);
  1270. sd->internal_ops = &unicam_subdev_internal_ops;
  1271. v4l2_set_subdevdata(sd, unicam);
  1272. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1273. sd->entity.ops = &unicam_subdev_media_ops;
  1274. sd->dev = unicam->dev;
  1275. sd->owner = THIS_MODULE;
  1276. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS;
  1277. strscpy(sd->name, "unicam", sizeof(sd->name));
  1278. unicam->subdev.pads[UNICAM_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1279. unicam->subdev.pads[UNICAM_SD_PAD_SOURCE_IMAGE].flags = MEDIA_PAD_FL_SOURCE;
  1280. unicam->subdev.pads[UNICAM_SD_PAD_SOURCE_METADATA].flags = MEDIA_PAD_FL_SOURCE;
  1281. ret = media_entity_pads_init(&sd->entity, ARRAY_SIZE(unicam->subdev.pads),
  1282. unicam->subdev.pads);
  1283. if (ret) {
  1284. dev_err(unicam->dev, "Failed to initialize media entity: %d\n",
  1285. ret);
  1286. return ret;
  1287. }
  1288. ret = v4l2_subdev_init_finalize(sd);
  1289. if (ret) {
  1290. dev_err(unicam->dev, "Failed to initialize subdev: %d\n", ret);
  1291. goto err_entity;
  1292. }
  1293. ret = v4l2_device_register_subdev(&unicam->v4l2_dev, sd);
  1294. if (ret) {
  1295. dev_err(unicam->dev, "Failed to register subdev: %d\n", ret);
  1296. goto err_subdev;
  1297. }
  1298. return 0;
  1299. err_subdev:
  1300. v4l2_subdev_cleanup(sd);
  1301. err_entity:
  1302. media_entity_cleanup(&sd->entity);
  1303. return ret;
  1304. }
  1305. static void unicam_subdev_cleanup(struct unicam_device *unicam)
  1306. {
  1307. v4l2_subdev_cleanup(&unicam->subdev.sd);
  1308. media_entity_cleanup(&unicam->subdev.sd.entity);
  1309. }
  1310. /* -----------------------------------------------------------------------------
  1311. * Videobuf2 queue operations
  1312. */
  1313. static int unicam_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  1314. unsigned int *nplanes, unsigned int sizes[],
  1315. struct device *alloc_devs[])
  1316. {
  1317. struct unicam_node *node = vb2_get_drv_priv(vq);
  1318. u32 size = is_image_node(node) ? node->fmt.fmt.pix.sizeimage
  1319. : node->fmt.fmt.meta.buffersize;
  1320. if (*nplanes) {
  1321. if (sizes[0] < size) {
  1322. dev_dbg(node->dev->dev, "sizes[0] %i < size %u\n",
  1323. sizes[0], size);
  1324. return -EINVAL;
  1325. }
  1326. size = sizes[0];
  1327. }
  1328. *nplanes = 1;
  1329. sizes[0] = size;
  1330. return 0;
  1331. }
  1332. static int unicam_buffer_prepare(struct vb2_buffer *vb)
  1333. {
  1334. struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
  1335. struct unicam_buffer *buf = to_unicam_buffer(vb);
  1336. u32 size = is_image_node(node) ? node->fmt.fmt.pix.sizeimage
  1337. : node->fmt.fmt.meta.buffersize;
  1338. if (vb2_plane_size(vb, 0) < size) {
  1339. dev_dbg(node->dev->dev,
  1340. "data will not fit into plane (%lu < %u)\n",
  1341. vb2_plane_size(vb, 0), size);
  1342. return -EINVAL;
  1343. }
  1344. buf->dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  1345. buf->size = size;
  1346. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  1347. return 0;
  1348. }
  1349. static void unicam_return_buffers(struct unicam_node *node,
  1350. enum vb2_buffer_state state)
  1351. {
  1352. struct unicam_buffer *buf, *tmp;
  1353. list_for_each_entry_safe(buf, tmp, &node->dma_queue, list) {
  1354. list_del(&buf->list);
  1355. vb2_buffer_done(&buf->vb.vb2_buf, state);
  1356. }
  1357. if (node->cur_frm)
  1358. vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
  1359. state);
  1360. if (node->next_frm && node->cur_frm != node->next_frm)
  1361. vb2_buffer_done(&node->next_frm->vb.vb2_buf,
  1362. state);
  1363. node->cur_frm = NULL;
  1364. node->next_frm = NULL;
  1365. }
  1366. static int unicam_num_data_lanes(struct unicam_device *unicam)
  1367. {
  1368. struct v4l2_mbus_config mbus_config = { 0 };
  1369. unsigned int num_data_lanes;
  1370. int ret;
  1371. if (unicam->bus_type != V4L2_MBUS_CSI2_DPHY)
  1372. return unicam->max_data_lanes;
  1373. ret = v4l2_subdev_call(unicam->sensor.subdev, pad, get_mbus_config,
  1374. unicam->sensor.pad->index, &mbus_config);
  1375. if (ret == -ENOIOCTLCMD)
  1376. return unicam->max_data_lanes;
  1377. if (ret < 0) {
  1378. dev_err(unicam->dev, "Failed to get mbus config: %d\n", ret);
  1379. return ret;
  1380. }
  1381. num_data_lanes = mbus_config.bus.mipi_csi2.num_data_lanes;
  1382. if (num_data_lanes != 1 && num_data_lanes != 2 && num_data_lanes != 4) {
  1383. dev_err(unicam->dev,
  1384. "Device %s has requested %u data lanes, invalid\n",
  1385. unicam->sensor.subdev->name, num_data_lanes);
  1386. return -EINVAL;
  1387. }
  1388. if (num_data_lanes > unicam->max_data_lanes) {
  1389. dev_err(unicam->dev,
  1390. "Device %s has requested %u data lanes, >%u configured in DT\n",
  1391. unicam->sensor.subdev->name, num_data_lanes,
  1392. unicam->max_data_lanes);
  1393. return -EINVAL;
  1394. }
  1395. return num_data_lanes;
  1396. }
  1397. static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
  1398. {
  1399. struct unicam_node *node = vb2_get_drv_priv(vq);
  1400. struct unicam_device *unicam = node->dev;
  1401. struct unicam_buffer *buf;
  1402. struct media_pipeline_pad_iter iter;
  1403. struct media_pad *pad;
  1404. unsigned long flags;
  1405. int ret;
  1406. dev_dbg(unicam->dev, "Starting stream on %s device\n",
  1407. is_metadata_node(node) ? "metadata" : "image");
  1408. /*
  1409. * Start the pipeline. This validates all links, and populates the
  1410. * pipeline structure.
  1411. */
  1412. ret = video_device_pipeline_start(&node->video_dev, &unicam->pipe.pipe);
  1413. if (ret < 0) {
  1414. dev_dbg(unicam->dev, "Failed to start media pipeline: %d\n", ret);
  1415. goto err_buffers;
  1416. }
  1417. /*
  1418. * Determine which video nodes are included in the pipeline, and get the
  1419. * number of data lanes.
  1420. */
  1421. if (unicam->pipe.pipe.start_count == 1) {
  1422. unicam->pipe.nodes = 0;
  1423. media_pipeline_for_each_pad(&unicam->pipe.pipe, &iter, pad) {
  1424. if (pad->entity != &unicam->subdev.sd.entity)
  1425. continue;
  1426. if (pad->index == UNICAM_SD_PAD_SOURCE_IMAGE)
  1427. unicam->pipe.nodes |= BIT(UNICAM_IMAGE_NODE);
  1428. else if (pad->index == UNICAM_SD_PAD_SOURCE_METADATA)
  1429. unicam->pipe.nodes |= BIT(UNICAM_METADATA_NODE);
  1430. }
  1431. if (!(unicam->pipe.nodes & BIT(UNICAM_IMAGE_NODE))) {
  1432. dev_dbg(unicam->dev,
  1433. "Pipeline does not include image node\n");
  1434. ret = -EPIPE;
  1435. goto err_pipeline;
  1436. }
  1437. ret = unicam_num_data_lanes(unicam);
  1438. if (ret < 0)
  1439. goto err_pipeline;
  1440. unicam->pipe.num_data_lanes = ret;
  1441. dev_dbg(unicam->dev, "Running with %u data lanes, nodes %u\n",
  1442. unicam->pipe.num_data_lanes, unicam->pipe.nodes);
  1443. }
  1444. /* Arm the node with the first buffer from the DMA queue. */
  1445. spin_lock_irqsave(&node->dma_queue_lock, flags);
  1446. buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list);
  1447. node->cur_frm = buf;
  1448. node->next_frm = buf;
  1449. list_del(&buf->list);
  1450. spin_unlock_irqrestore(&node->dma_queue_lock, flags);
  1451. /*
  1452. * Wait for all the video devices in the pipeline to have been started
  1453. * before starting the hardware. In the general case, this would
  1454. * prevent capturing multiple streams independently. However, the
  1455. * Unicam DMA engines are not generic, they have been designed to
  1456. * capture image data and embedded data from the same camera sensor.
  1457. * Not only does the main use case not benefit from independent
  1458. * capture, it requires proper synchronization of the streams at start
  1459. * time.
  1460. */
  1461. if (unicam->pipe.pipe.start_count < hweight32(unicam->pipe.nodes))
  1462. return 0;
  1463. ret = pm_runtime_resume_and_get(unicam->dev);
  1464. if (ret < 0) {
  1465. dev_err(unicam->dev, "PM runtime resume failed: %d\n", ret);
  1466. goto err_pipeline;
  1467. }
  1468. /* Enable the streams on the source. */
  1469. ret = v4l2_subdev_enable_streams(&unicam->subdev.sd,
  1470. UNICAM_SD_PAD_SOURCE_IMAGE,
  1471. BIT(0));
  1472. if (ret < 0) {
  1473. dev_err(unicam->dev, "stream on failed in subdev\n");
  1474. goto err_pm_put;
  1475. }
  1476. if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE)) {
  1477. ret = v4l2_subdev_enable_streams(&unicam->subdev.sd,
  1478. UNICAM_SD_PAD_SOURCE_METADATA,
  1479. BIT(0));
  1480. if (ret < 0) {
  1481. dev_err(unicam->dev, "stream on failed in subdev\n");
  1482. goto err_disable_streams;
  1483. }
  1484. }
  1485. return 0;
  1486. err_disable_streams:
  1487. v4l2_subdev_disable_streams(&unicam->subdev.sd,
  1488. UNICAM_SD_PAD_SOURCE_IMAGE, BIT(0));
  1489. err_pm_put:
  1490. pm_runtime_put_sync(unicam->dev);
  1491. err_pipeline:
  1492. video_device_pipeline_stop(&node->video_dev);
  1493. err_buffers:
  1494. unicam_return_buffers(node, VB2_BUF_STATE_QUEUED);
  1495. return ret;
  1496. }
  1497. static void unicam_stop_streaming(struct vb2_queue *vq)
  1498. {
  1499. struct unicam_node *node = vb2_get_drv_priv(vq);
  1500. struct unicam_device *unicam = node->dev;
  1501. /* Stop the hardware when the first video device gets stopped. */
  1502. if (unicam->pipe.pipe.start_count == hweight32(unicam->pipe.nodes)) {
  1503. if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE))
  1504. v4l2_subdev_disable_streams(&unicam->subdev.sd,
  1505. UNICAM_SD_PAD_SOURCE_METADATA,
  1506. BIT(0));
  1507. v4l2_subdev_disable_streams(&unicam->subdev.sd,
  1508. UNICAM_SD_PAD_SOURCE_IMAGE,
  1509. BIT(0));
  1510. pm_runtime_put(unicam->dev);
  1511. }
  1512. video_device_pipeline_stop(&node->video_dev);
  1513. /* Clear all queued buffers for the node */
  1514. unicam_return_buffers(node, VB2_BUF_STATE_ERROR);
  1515. }
  1516. static void unicam_buffer_queue(struct vb2_buffer *vb)
  1517. {
  1518. struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
  1519. struct unicam_buffer *buf = to_unicam_buffer(vb);
  1520. spin_lock_irq(&node->dma_queue_lock);
  1521. list_add_tail(&buf->list, &node->dma_queue);
  1522. spin_unlock_irq(&node->dma_queue_lock);
  1523. }
  1524. static const struct vb2_ops unicam_video_qops = {
  1525. .queue_setup = unicam_queue_setup,
  1526. .wait_prepare = vb2_ops_wait_prepare,
  1527. .wait_finish = vb2_ops_wait_finish,
  1528. .buf_prepare = unicam_buffer_prepare,
  1529. .start_streaming = unicam_start_streaming,
  1530. .stop_streaming = unicam_stop_streaming,
  1531. .buf_queue = unicam_buffer_queue,
  1532. };
  1533. /* -----------------------------------------------------------------------------
  1534. * V4L2 video device operations
  1535. */
  1536. static int unicam_querycap(struct file *file, void *priv,
  1537. struct v4l2_capability *cap)
  1538. {
  1539. strscpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
  1540. strscpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
  1541. cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_META_CAPTURE;
  1542. return 0;
  1543. }
  1544. static int unicam_enum_fmt_vid(struct file *file, void *priv,
  1545. struct v4l2_fmtdesc *f)
  1546. {
  1547. unsigned int index;
  1548. unsigned int i;
  1549. for (i = 0, index = 0; i < ARRAY_SIZE(unicam_image_formats); i++) {
  1550. if (f->mbus_code && unicam_image_formats[i].code != f->mbus_code)
  1551. continue;
  1552. if (index == f->index) {
  1553. f->pixelformat = unicam_image_formats[i].fourcc;
  1554. return 0;
  1555. }
  1556. index++;
  1557. if (!unicam_image_formats[i].unpacked_fourcc)
  1558. continue;
  1559. if (index == f->index) {
  1560. f->pixelformat = unicam_image_formats[i].unpacked_fourcc;
  1561. return 0;
  1562. }
  1563. index++;
  1564. }
  1565. return -EINVAL;
  1566. }
  1567. static int unicam_g_fmt_vid(struct file *file, void *priv,
  1568. struct v4l2_format *f)
  1569. {
  1570. struct unicam_node *node = video_drvdata(file);
  1571. *f = node->fmt;
  1572. return 0;
  1573. }
  1574. static void __unicam_try_fmt_vid(struct unicam_node *node,
  1575. struct v4l2_pix_format *pix)
  1576. {
  1577. const struct unicam_format_info *fmtinfo;
  1578. /*
  1579. * Default to the first format if the requested pixel format code isn't
  1580. * supported.
  1581. */
  1582. fmtinfo = unicam_find_format_by_fourcc(pix->pixelformat,
  1583. UNICAM_SD_PAD_SOURCE_IMAGE);
  1584. if (!fmtinfo) {
  1585. fmtinfo = &unicam_image_formats[0];
  1586. pix->pixelformat = fmtinfo->fourcc;
  1587. }
  1588. unicam_calc_image_size_bpl(node->dev, fmtinfo, pix);
  1589. if (pix->field == V4L2_FIELD_ANY)
  1590. pix->field = V4L2_FIELD_NONE;
  1591. }
  1592. static int unicam_try_fmt_vid(struct file *file, void *priv,
  1593. struct v4l2_format *f)
  1594. {
  1595. struct unicam_node *node = video_drvdata(file);
  1596. __unicam_try_fmt_vid(node, &f->fmt.pix);
  1597. return 0;
  1598. }
  1599. static int unicam_s_fmt_vid(struct file *file, void *priv,
  1600. struct v4l2_format *f)
  1601. {
  1602. struct unicam_node *node = video_drvdata(file);
  1603. if (vb2_is_busy(&node->buffer_queue))
  1604. return -EBUSY;
  1605. __unicam_try_fmt_vid(node, &f->fmt.pix);
  1606. node->fmt = *f;
  1607. return 0;
  1608. }
  1609. static int unicam_enum_fmt_meta(struct file *file, void *priv,
  1610. struct v4l2_fmtdesc *f)
  1611. {
  1612. unsigned int i, index;
  1613. for (i = 0, index = 0; i < ARRAY_SIZE(unicam_meta_formats); i++) {
  1614. if (f->mbus_code && unicam_meta_formats[i].code != f->mbus_code)
  1615. continue;
  1616. if (index == f->index) {
  1617. f->pixelformat = unicam_meta_formats[i].fourcc;
  1618. f->type = V4L2_BUF_TYPE_META_CAPTURE;
  1619. f->flags = V4L2_FMT_FLAG_META_LINE_BASED;
  1620. return 0;
  1621. }
  1622. index++;
  1623. }
  1624. return -EINVAL;
  1625. }
  1626. static int unicam_g_fmt_meta(struct file *file, void *priv,
  1627. struct v4l2_format *f)
  1628. {
  1629. struct unicam_node *node = video_drvdata(file);
  1630. f->fmt.meta = node->fmt.fmt.meta;
  1631. return 0;
  1632. }
  1633. static const struct unicam_format_info *
  1634. __unicam_try_fmt_meta(struct unicam_node *node, struct v4l2_meta_format *meta)
  1635. {
  1636. const struct unicam_format_info *fmtinfo;
  1637. /*
  1638. * Default to the first format if the requested pixel format code isn't
  1639. * supported.
  1640. */
  1641. fmtinfo = unicam_find_format_by_fourcc(meta->dataformat,
  1642. UNICAM_SD_PAD_SOURCE_METADATA);
  1643. if (!fmtinfo) {
  1644. fmtinfo = &unicam_meta_formats[0];
  1645. meta->dataformat = fmtinfo->fourcc;
  1646. }
  1647. unicam_calc_meta_size_bpl(node->dev, fmtinfo, meta);
  1648. return fmtinfo;
  1649. }
  1650. static int unicam_try_fmt_meta(struct file *file, void *priv,
  1651. struct v4l2_format *f)
  1652. {
  1653. struct unicam_node *node = video_drvdata(file);
  1654. __unicam_try_fmt_meta(node, &f->fmt.meta);
  1655. return 0;
  1656. }
  1657. static int unicam_s_fmt_meta(struct file *file, void *priv,
  1658. struct v4l2_format *f)
  1659. {
  1660. struct unicam_node *node = video_drvdata(file);
  1661. if (vb2_is_busy(&node->buffer_queue))
  1662. return -EBUSY;
  1663. __unicam_try_fmt_meta(node, &f->fmt.meta);
  1664. node->fmt = *f;
  1665. return 0;
  1666. }
  1667. static int unicam_enum_framesizes(struct file *file, void *fh,
  1668. struct v4l2_frmsizeenum *fsize)
  1669. {
  1670. struct unicam_node *node = video_drvdata(file);
  1671. int ret = -EINVAL;
  1672. if (fsize->index > 0)
  1673. return ret;
  1674. if (is_image_node(node)) {
  1675. if (!unicam_find_format_by_fourcc(fsize->pixel_format,
  1676. UNICAM_SD_PAD_SOURCE_IMAGE))
  1677. return ret;
  1678. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1679. fsize->stepwise.min_width = UNICAM_IMAGE_MIN_WIDTH;
  1680. fsize->stepwise.max_width = UNICAM_IMAGE_MAX_WIDTH;
  1681. fsize->stepwise.step_width = 1;
  1682. fsize->stepwise.min_height = UNICAM_IMAGE_MIN_HEIGHT;
  1683. fsize->stepwise.max_height = UNICAM_IMAGE_MAX_HEIGHT;
  1684. fsize->stepwise.step_height = 1;
  1685. } else {
  1686. if (!unicam_find_format_by_fourcc(fsize->pixel_format,
  1687. UNICAM_SD_PAD_SOURCE_METADATA))
  1688. return ret;
  1689. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1690. fsize->stepwise.min_width = UNICAM_META_MIN_WIDTH;
  1691. fsize->stepwise.max_width = UNICAM_META_MAX_WIDTH;
  1692. fsize->stepwise.step_width = 1;
  1693. fsize->stepwise.min_height = UNICAM_META_MIN_HEIGHT;
  1694. fsize->stepwise.max_height = UNICAM_META_MAX_HEIGHT;
  1695. fsize->stepwise.step_height = 1;
  1696. }
  1697. return 0;
  1698. }
  1699. static int unicam_log_status(struct file *file, void *fh)
  1700. {
  1701. struct unicam_node *node = video_drvdata(file);
  1702. struct unicam_device *unicam = node->dev;
  1703. u32 reg;
  1704. /* status for sub devices */
  1705. v4l2_device_call_all(&unicam->v4l2_dev, 0, core, log_status);
  1706. dev_info(unicam->dev, "-----Receiver status-----\n");
  1707. dev_info(unicam->dev, "V4L2 width/height: %ux%u\n",
  1708. node->fmt.fmt.pix.width, node->fmt.fmt.pix.height);
  1709. dev_info(unicam->dev, "V4L2 format: %08x\n",
  1710. node->fmt.fmt.pix.pixelformat);
  1711. reg = unicam_reg_read(unicam, UNICAM_IPIPE);
  1712. dev_info(unicam->dev, "Unpacking/packing: %u / %u\n",
  1713. unicam_get_field(reg, UNICAM_PUM_MASK),
  1714. unicam_get_field(reg, UNICAM_PPM_MASK));
  1715. dev_info(unicam->dev, "----Live data----\n");
  1716. dev_info(unicam->dev, "Programmed stride: %4u\n",
  1717. unicam_reg_read(unicam, UNICAM_IBLS));
  1718. dev_info(unicam->dev, "Detected resolution: %ux%u\n",
  1719. unicam_reg_read(unicam, UNICAM_IHSTA),
  1720. unicam_reg_read(unicam, UNICAM_IVSTA));
  1721. dev_info(unicam->dev, "Write pointer: %08x\n",
  1722. unicam_reg_read(unicam, UNICAM_IBWP));
  1723. return 0;
  1724. }
  1725. static int unicam_subscribe_event(struct v4l2_fh *fh,
  1726. const struct v4l2_event_subscription *sub)
  1727. {
  1728. switch (sub->type) {
  1729. case V4L2_EVENT_FRAME_SYNC:
  1730. return v4l2_event_subscribe(fh, sub, 2, NULL);
  1731. default:
  1732. return -EINVAL;
  1733. }
  1734. }
  1735. static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
  1736. .vidioc_querycap = unicam_querycap,
  1737. .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid,
  1738. .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid,
  1739. .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid,
  1740. .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid,
  1741. .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta,
  1742. .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta,
  1743. .vidioc_try_fmt_meta_cap = unicam_try_fmt_meta,
  1744. .vidioc_s_fmt_meta_cap = unicam_s_fmt_meta,
  1745. .vidioc_enum_framesizes = unicam_enum_framesizes,
  1746. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1747. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1748. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1749. .vidioc_querybuf = vb2_ioctl_querybuf,
  1750. .vidioc_qbuf = vb2_ioctl_qbuf,
  1751. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1752. .vidioc_expbuf = vb2_ioctl_expbuf,
  1753. .vidioc_streamon = vb2_ioctl_streamon,
  1754. .vidioc_streamoff = vb2_ioctl_streamoff,
  1755. .vidioc_log_status = unicam_log_status,
  1756. .vidioc_subscribe_event = unicam_subscribe_event,
  1757. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1758. };
  1759. /* unicam capture driver file operations */
  1760. static const struct v4l2_file_operations unicam_fops = {
  1761. .owner = THIS_MODULE,
  1762. .open = v4l2_fh_open,
  1763. .release = vb2_fop_release,
  1764. .poll = vb2_fop_poll,
  1765. .unlocked_ioctl = video_ioctl2,
  1766. .mmap = vb2_fop_mmap,
  1767. };
  1768. static int unicam_video_link_validate(struct media_link *link)
  1769. {
  1770. struct video_device *vdev =
  1771. media_entity_to_video_device(link->sink->entity);
  1772. struct v4l2_subdev *sd =
  1773. media_entity_to_v4l2_subdev(link->source->entity);
  1774. struct unicam_node *node = video_get_drvdata(vdev);
  1775. const u32 pad = is_image_node(node) ? UNICAM_SD_PAD_SOURCE_IMAGE
  1776. : UNICAM_SD_PAD_SOURCE_METADATA;
  1777. const struct v4l2_mbus_framefmt *format;
  1778. struct v4l2_subdev_state *state;
  1779. int ret = 0;
  1780. state = v4l2_subdev_lock_and_get_active_state(sd);
  1781. format = v4l2_subdev_state_get_format(state, pad, 0);
  1782. if (!format) {
  1783. ret = -EINVAL;
  1784. goto out;
  1785. }
  1786. if (is_image_node(node)) {
  1787. const struct v4l2_pix_format *fmt = &node->fmt.fmt.pix;
  1788. const struct unicam_format_info *fmtinfo;
  1789. fmtinfo = unicam_find_format_by_fourcc(fmt->pixelformat,
  1790. UNICAM_SD_PAD_SOURCE_IMAGE);
  1791. if (WARN_ON(!fmtinfo)) {
  1792. ret = -EPIPE;
  1793. goto out;
  1794. }
  1795. if (fmtinfo->code != format->code ||
  1796. fmt->height != format->height ||
  1797. fmt->width != format->width ||
  1798. fmt->field != format->field) {
  1799. dev_dbg(node->dev->dev,
  1800. "image: (%u x %u) 0x%08x %s != (%u x %u) 0x%08x %s\n",
  1801. fmt->width, fmt->height, fmtinfo->code,
  1802. v4l2_field_names[fmt->field],
  1803. format->width, format->height, format->code,
  1804. v4l2_field_names[format->field]);
  1805. ret = -EPIPE;
  1806. }
  1807. } else {
  1808. const struct v4l2_meta_format *fmt = &node->fmt.fmt.meta;
  1809. const struct unicam_format_info *fmtinfo;
  1810. fmtinfo = unicam_find_format_by_fourcc(fmt->dataformat,
  1811. UNICAM_SD_PAD_SOURCE_METADATA);
  1812. if (WARN_ON(!fmtinfo)) {
  1813. ret = -EPIPE;
  1814. goto out;
  1815. }
  1816. if (fmtinfo->code != format->code ||
  1817. fmt->height != format->height ||
  1818. fmt->width != format->width) {
  1819. dev_dbg(node->dev->dev,
  1820. "meta: (%u x %u) 0x%04x != (%u x %u) 0x%04x\n",
  1821. fmt->width, fmt->height, fmtinfo->code,
  1822. format->width, format->height, format->code);
  1823. ret = -EPIPE;
  1824. }
  1825. }
  1826. out:
  1827. v4l2_subdev_unlock_state(state);
  1828. return ret;
  1829. }
  1830. static const struct media_entity_operations unicam_video_media_ops = {
  1831. .link_validate = unicam_video_link_validate,
  1832. };
  1833. static void unicam_node_release(struct video_device *vdev)
  1834. {
  1835. struct unicam_node *node = video_get_drvdata(vdev);
  1836. unicam_put(node->dev);
  1837. }
  1838. static void unicam_set_default_format(struct unicam_node *node)
  1839. {
  1840. if (is_image_node(node)) {
  1841. struct v4l2_pix_format *fmt = &node->fmt.fmt.pix;
  1842. const struct unicam_format_info *fmtinfo =
  1843. &unicam_image_formats[0];
  1844. node->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1845. v4l2_fill_pix_format(fmt, &unicam_default_image_format);
  1846. fmt->pixelformat = fmtinfo->fourcc;
  1847. unicam_calc_image_size_bpl(node->dev, fmtinfo, fmt);
  1848. } else {
  1849. struct v4l2_meta_format *fmt = &node->fmt.fmt.meta;
  1850. const struct unicam_format_info *fmtinfo =
  1851. &unicam_meta_formats[0];
  1852. node->fmt.type = V4L2_BUF_TYPE_META_CAPTURE;
  1853. fmt->dataformat = fmtinfo->fourcc;
  1854. fmt->width = unicam_default_meta_format.width;
  1855. fmt->height = unicam_default_meta_format.height;
  1856. unicam_calc_meta_size_bpl(node->dev, fmtinfo, fmt);
  1857. }
  1858. }
  1859. static int unicam_register_node(struct unicam_device *unicam,
  1860. enum unicam_node_type type)
  1861. {
  1862. const u32 pad_index = type == UNICAM_IMAGE_NODE
  1863. ? UNICAM_SD_PAD_SOURCE_IMAGE
  1864. : UNICAM_SD_PAD_SOURCE_METADATA;
  1865. struct unicam_node *node = &unicam->node[type];
  1866. struct video_device *vdev = &node->video_dev;
  1867. struct vb2_queue *q = &node->buffer_queue;
  1868. int ret;
  1869. node->dev = unicam_get(unicam);
  1870. node->id = type;
  1871. spin_lock_init(&node->dma_queue_lock);
  1872. INIT_LIST_HEAD(&node->dma_queue);
  1873. /* Initialize the videobuf2 queue. */
  1874. q->type = type == UNICAM_IMAGE_NODE ? V4L2_BUF_TYPE_VIDEO_CAPTURE
  1875. : V4L2_BUF_TYPE_META_CAPTURE;
  1876. q->io_modes = VB2_MMAP | VB2_DMABUF;
  1877. q->drv_priv = node;
  1878. q->ops = &unicam_video_qops;
  1879. q->mem_ops = &vb2_dma_contig_memops;
  1880. q->buf_struct_size = sizeof(struct unicam_buffer);
  1881. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1882. q->lock = &unicam->lock;
  1883. q->min_queued_buffers = 1;
  1884. q->dev = unicam->dev;
  1885. ret = vb2_queue_init(q);
  1886. if (ret) {
  1887. dev_err(unicam->dev, "vb2_queue_init() failed\n");
  1888. goto err_unicam_put;
  1889. }
  1890. /* Initialize the video device. */
  1891. vdev->release = unicam_node_release;
  1892. vdev->fops = &unicam_fops;
  1893. vdev->ioctl_ops = &unicam_ioctl_ops;
  1894. vdev->v4l2_dev = &unicam->v4l2_dev;
  1895. vdev->vfl_dir = VFL_DIR_RX;
  1896. vdev->queue = q;
  1897. vdev->lock = &unicam->lock;
  1898. vdev->device_caps = type == UNICAM_IMAGE_NODE
  1899. ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_META_CAPTURE;
  1900. vdev->device_caps |= V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
  1901. vdev->entity.ops = &unicam_video_media_ops;
  1902. snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME,
  1903. type == UNICAM_IMAGE_NODE ? "image" : "embedded");
  1904. video_set_drvdata(vdev, node);
  1905. if (type == UNICAM_IMAGE_NODE)
  1906. vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
  1907. node->pad.flags = MEDIA_PAD_FL_SINK;
  1908. ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
  1909. if (ret)
  1910. goto err_unicam_put;
  1911. node->dummy_buf.size = UNICAM_DUMMY_BUF_SIZE;
  1912. node->dummy_buf_cpu_addr = dma_alloc_coherent(unicam->dev,
  1913. node->dummy_buf.size,
  1914. &node->dummy_buf.dma_addr,
  1915. GFP_KERNEL);
  1916. if (!node->dummy_buf_cpu_addr) {
  1917. dev_err(unicam->dev, "Unable to allocate dummy buffer.\n");
  1918. ret = -ENOMEM;
  1919. goto err_entity_cleanup;
  1920. }
  1921. unicam_set_default_format(node);
  1922. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  1923. if (ret) {
  1924. dev_err(unicam->dev, "Unable to register video device %s\n",
  1925. vdev->name);
  1926. goto err_dma_free;
  1927. }
  1928. node->registered = true;
  1929. ret = media_create_pad_link(&unicam->subdev.sd.entity,
  1930. pad_index,
  1931. &node->video_dev.entity,
  1932. 0,
  1933. MEDIA_LNK_FL_ENABLED |
  1934. MEDIA_LNK_FL_IMMUTABLE);
  1935. if (ret) {
  1936. /*
  1937. * No need for cleanup, the caller will unregister the
  1938. * video device, which will drop the reference on the
  1939. * device and trigger the cleanup.
  1940. */
  1941. dev_err(unicam->dev, "Unable to create pad link for %s\n",
  1942. unicam->sensor.subdev->name);
  1943. return ret;
  1944. }
  1945. return 0;
  1946. err_dma_free:
  1947. dma_free_coherent(unicam->dev, node->dummy_buf.size,
  1948. node->dummy_buf_cpu_addr,
  1949. node->dummy_buf.dma_addr);
  1950. err_entity_cleanup:
  1951. media_entity_cleanup(&vdev->entity);
  1952. err_unicam_put:
  1953. unicam_put(unicam);
  1954. return ret;
  1955. }
  1956. static void unicam_unregister_nodes(struct unicam_device *unicam)
  1957. {
  1958. unsigned int i;
  1959. for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
  1960. struct unicam_node *node = &unicam->node[i];
  1961. if (node->registered) {
  1962. vb2_video_unregister_device(&node->video_dev);
  1963. node->registered = false;
  1964. }
  1965. if (node->dummy_buf_cpu_addr)
  1966. dma_free_coherent(unicam->dev, node->dummy_buf.size,
  1967. node->dummy_buf_cpu_addr,
  1968. node->dummy_buf.dma_addr);
  1969. }
  1970. }
  1971. /* -----------------------------------------------------------------------------
  1972. * Power management
  1973. */
  1974. static int unicam_runtime_resume(struct device *dev)
  1975. {
  1976. struct unicam_device *unicam = dev_get_drvdata(dev);
  1977. int ret;
  1978. ret = clk_set_min_rate(unicam->vpu_clock, UNICAM_MIN_VPU_CLOCK_RATE);
  1979. if (ret) {
  1980. dev_err(unicam->dev, "failed to set up VPU clock\n");
  1981. return ret;
  1982. }
  1983. ret = clk_prepare_enable(unicam->vpu_clock);
  1984. if (ret) {
  1985. dev_err(unicam->dev, "Failed to enable VPU clock: %d\n", ret);
  1986. goto err_vpu_clock;
  1987. }
  1988. ret = clk_set_rate(unicam->clock, 100 * 1000 * 1000);
  1989. if (ret) {
  1990. dev_err(unicam->dev, "failed to set up CSI clock\n");
  1991. goto err_vpu_prepare;
  1992. }
  1993. ret = clk_prepare_enable(unicam->clock);
  1994. if (ret) {
  1995. dev_err(unicam->dev, "Failed to enable CSI clock: %d\n", ret);
  1996. goto err_vpu_prepare;
  1997. }
  1998. return 0;
  1999. err_vpu_prepare:
  2000. clk_disable_unprepare(unicam->vpu_clock);
  2001. err_vpu_clock:
  2002. if (clk_set_min_rate(unicam->vpu_clock, 0))
  2003. dev_err(unicam->dev, "failed to reset the VPU clock\n");
  2004. return ret;
  2005. }
  2006. static int unicam_runtime_suspend(struct device *dev)
  2007. {
  2008. struct unicam_device *unicam = dev_get_drvdata(dev);
  2009. clk_disable_unprepare(unicam->clock);
  2010. if (clk_set_min_rate(unicam->vpu_clock, 0))
  2011. dev_err(unicam->dev, "failed to reset the VPU clock\n");
  2012. clk_disable_unprepare(unicam->vpu_clock);
  2013. return 0;
  2014. }
  2015. static const struct dev_pm_ops unicam_pm_ops = {
  2016. RUNTIME_PM_OPS(unicam_runtime_suspend, unicam_runtime_resume, NULL)
  2017. };
  2018. /* -----------------------------------------------------------------------------
  2019. * V4L2 async notifier
  2020. */
  2021. static int unicam_async_bound(struct v4l2_async_notifier *notifier,
  2022. struct v4l2_subdev *subdev,
  2023. struct v4l2_async_connection *asc)
  2024. {
  2025. struct unicam_device *unicam = notifier_to_unicam_device(notifier);
  2026. struct media_pad *sink = &unicam->subdev.pads[UNICAM_SD_PAD_SINK];
  2027. struct media_pad *source;
  2028. int ret;
  2029. dev_dbg(unicam->dev, "Using sensor %s for capture\n",
  2030. subdev->name);
  2031. ret = v4l2_create_fwnode_links_to_pad(subdev, sink, MEDIA_LNK_FL_ENABLED |
  2032. MEDIA_LNK_FL_IMMUTABLE);
  2033. if (ret)
  2034. return ret;
  2035. source = media_pad_remote_pad_unique(sink);
  2036. if (IS_ERR(source)) {
  2037. dev_err(unicam->dev, "No connected sensor pad\n");
  2038. return PTR_ERR(source);
  2039. }
  2040. unicam->sensor.subdev = subdev;
  2041. unicam->sensor.pad = source;
  2042. return 0;
  2043. }
  2044. static int unicam_async_complete(struct v4l2_async_notifier *notifier)
  2045. {
  2046. struct unicam_device *unicam = notifier_to_unicam_device(notifier);
  2047. int ret;
  2048. ret = unicam_register_node(unicam, UNICAM_IMAGE_NODE);
  2049. if (ret) {
  2050. dev_err(unicam->dev, "Unable to register image video device.\n");
  2051. goto unregister;
  2052. }
  2053. ret = unicam_register_node(unicam, UNICAM_METADATA_NODE);
  2054. if (ret) {
  2055. dev_err(unicam->dev, "Unable to register metadata video device.\n");
  2056. goto unregister;
  2057. }
  2058. ret = v4l2_device_register_subdev_nodes(&unicam->v4l2_dev);
  2059. if (ret) {
  2060. dev_err(unicam->dev, "Unable to register subdev nodes.\n");
  2061. goto unregister;
  2062. }
  2063. return 0;
  2064. unregister:
  2065. unicam_unregister_nodes(unicam);
  2066. unicam_put(unicam);
  2067. return ret;
  2068. }
  2069. static const struct v4l2_async_notifier_operations unicam_async_ops = {
  2070. .bound = unicam_async_bound,
  2071. .complete = unicam_async_complete,
  2072. };
  2073. static int unicam_async_nf_init(struct unicam_device *unicam)
  2074. {
  2075. struct v4l2_fwnode_endpoint ep = { };
  2076. struct fwnode_handle *ep_handle;
  2077. struct v4l2_async_connection *asc;
  2078. int ret;
  2079. ret = of_property_read_u32(unicam->dev->of_node, "brcm,num-data-lanes",
  2080. &unicam->max_data_lanes);
  2081. if (ret < 0) {
  2082. dev_err(unicam->dev, "Missing %s DT property\n",
  2083. "brcm,num-data-lanes");
  2084. return -EINVAL;
  2085. }
  2086. /* Get and parse the local endpoint. */
  2087. ep_handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(unicam->dev), 0, 0,
  2088. FWNODE_GRAPH_ENDPOINT_NEXT);
  2089. if (!ep_handle) {
  2090. dev_err(unicam->dev, "No endpoint found\n");
  2091. return -ENODEV;
  2092. }
  2093. ret = v4l2_fwnode_endpoint_parse(ep_handle, &ep);
  2094. if (ret) {
  2095. dev_err(unicam->dev, "Failed to parse endpoint: %d\n", ret);
  2096. goto error;
  2097. }
  2098. unicam->bus_type = ep.bus_type;
  2099. switch (ep.bus_type) {
  2100. case V4L2_MBUS_CSI2_DPHY: {
  2101. unsigned int num_data_lanes = ep.bus.mipi_csi2.num_data_lanes;
  2102. if (num_data_lanes != 1 && num_data_lanes != 2 &&
  2103. num_data_lanes != 4) {
  2104. dev_err(unicam->dev, "%u data lanes not supported\n",
  2105. num_data_lanes);
  2106. ret = -EINVAL;
  2107. goto error;
  2108. }
  2109. if (num_data_lanes > unicam->max_data_lanes) {
  2110. dev_err(unicam->dev,
  2111. "Endpoint uses %u data lanes when %u are supported\n",
  2112. num_data_lanes, unicam->max_data_lanes);
  2113. ret = -EINVAL;
  2114. goto error;
  2115. }
  2116. unicam->max_data_lanes = num_data_lanes;
  2117. unicam->bus_flags = ep.bus.mipi_csi2.flags;
  2118. break;
  2119. }
  2120. case V4L2_MBUS_CCP2:
  2121. unicam->max_data_lanes = 1;
  2122. unicam->bus_flags = ep.bus.mipi_csi1.strobe;
  2123. break;
  2124. default:
  2125. /* Unsupported bus type */
  2126. dev_err(unicam->dev, "Unsupported bus type %u\n", ep.bus_type);
  2127. ret = -EINVAL;
  2128. goto error;
  2129. }
  2130. /* Initialize and register the async notifier. */
  2131. v4l2_async_nf_init(&unicam->notifier, &unicam->v4l2_dev);
  2132. asc = v4l2_async_nf_add_fwnode_remote(&unicam->notifier, ep_handle,
  2133. struct v4l2_async_connection);
  2134. fwnode_handle_put(ep_handle);
  2135. ep_handle = NULL;
  2136. if (IS_ERR(asc)) {
  2137. ret = PTR_ERR(asc);
  2138. dev_err(unicam->dev, "Failed to add entry to notifier: %d\n",
  2139. ret);
  2140. goto error;
  2141. }
  2142. unicam->notifier.ops = &unicam_async_ops;
  2143. ret = v4l2_async_nf_register(&unicam->notifier);
  2144. if (ret) {
  2145. dev_err(unicam->dev, "Error registering device notifier: %d\n",
  2146. ret);
  2147. goto error;
  2148. }
  2149. return 0;
  2150. error:
  2151. fwnode_handle_put(ep_handle);
  2152. return ret;
  2153. }
  2154. /* -----------------------------------------------------------------------------
  2155. * Probe & remove
  2156. */
  2157. static int unicam_media_init(struct unicam_device *unicam)
  2158. {
  2159. int ret;
  2160. unicam->mdev.dev = unicam->dev;
  2161. strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
  2162. sizeof(unicam->mdev.model));
  2163. unicam->mdev.hw_revision = 0;
  2164. media_device_init(&unicam->mdev);
  2165. unicam->v4l2_dev.mdev = &unicam->mdev;
  2166. ret = v4l2_device_register(unicam->dev, &unicam->v4l2_dev);
  2167. if (ret < 0) {
  2168. dev_err(unicam->dev, "Unable to register v4l2 device\n");
  2169. goto err_media_cleanup;
  2170. }
  2171. ret = media_device_register(&unicam->mdev);
  2172. if (ret < 0) {
  2173. dev_err(unicam->dev,
  2174. "Unable to register media-controller device\n");
  2175. goto err_v4l2_unregister;
  2176. }
  2177. return 0;
  2178. err_v4l2_unregister:
  2179. v4l2_device_unregister(&unicam->v4l2_dev);
  2180. err_media_cleanup:
  2181. media_device_cleanup(&unicam->mdev);
  2182. return ret;
  2183. }
  2184. static int unicam_probe(struct platform_device *pdev)
  2185. {
  2186. struct unicam_device *unicam;
  2187. int ret;
  2188. unicam = kzalloc(sizeof(*unicam), GFP_KERNEL);
  2189. if (!unicam)
  2190. return -ENOMEM;
  2191. kref_init(&unicam->kref);
  2192. mutex_init(&unicam->lock);
  2193. unicam->dev = &pdev->dev;
  2194. platform_set_drvdata(pdev, unicam);
  2195. unicam->base = devm_platform_ioremap_resource_byname(pdev, "unicam");
  2196. if (IS_ERR(unicam->base)) {
  2197. ret = PTR_ERR(unicam->base);
  2198. goto err_unicam_put;
  2199. }
  2200. unicam->clk_gate_base = devm_platform_ioremap_resource_byname(pdev, "cmi");
  2201. if (IS_ERR(unicam->clk_gate_base)) {
  2202. ret = PTR_ERR(unicam->clk_gate_base);
  2203. goto err_unicam_put;
  2204. }
  2205. unicam->clock = devm_clk_get(&pdev->dev, "lp");
  2206. if (IS_ERR(unicam->clock)) {
  2207. dev_err(unicam->dev, "Failed to get lp clock\n");
  2208. ret = PTR_ERR(unicam->clock);
  2209. goto err_unicam_put;
  2210. }
  2211. unicam->vpu_clock = devm_clk_get(&pdev->dev, "vpu");
  2212. if (IS_ERR(unicam->vpu_clock)) {
  2213. dev_err(unicam->dev, "Failed to get vpu clock\n");
  2214. ret = PTR_ERR(unicam->vpu_clock);
  2215. goto err_unicam_put;
  2216. }
  2217. ret = platform_get_irq(pdev, 0);
  2218. if (ret < 0)
  2219. goto err_unicam_put;
  2220. ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
  2221. "unicam_capture0", unicam);
  2222. if (ret) {
  2223. dev_err(&pdev->dev, "Unable to request interrupt\n");
  2224. goto err_unicam_put;
  2225. }
  2226. /* Enable the block power domain. */
  2227. pm_runtime_enable(&pdev->dev);
  2228. ret = unicam_media_init(unicam);
  2229. if (ret)
  2230. goto err_pm_runtime;
  2231. ret = unicam_subdev_init(unicam);
  2232. if (ret)
  2233. goto err_media_unregister;
  2234. ret = unicam_async_nf_init(unicam);
  2235. if (ret)
  2236. goto err_subdev_unregister;
  2237. return 0;
  2238. err_subdev_unregister:
  2239. unicam_subdev_cleanup(unicam);
  2240. err_media_unregister:
  2241. media_device_unregister(&unicam->mdev);
  2242. err_pm_runtime:
  2243. pm_runtime_disable(&pdev->dev);
  2244. err_unicam_put:
  2245. unicam_put(unicam);
  2246. return ret;
  2247. }
  2248. static void unicam_remove(struct platform_device *pdev)
  2249. {
  2250. struct unicam_device *unicam = platform_get_drvdata(pdev);
  2251. unicam_unregister_nodes(unicam);
  2252. v4l2_device_unregister(&unicam->v4l2_dev);
  2253. media_device_unregister(&unicam->mdev);
  2254. v4l2_async_nf_unregister(&unicam->notifier);
  2255. unicam_subdev_cleanup(unicam);
  2256. unicam_put(unicam);
  2257. pm_runtime_disable(&pdev->dev);
  2258. }
  2259. static const struct of_device_id unicam_of_match[] = {
  2260. { .compatible = "brcm,bcm2835-unicam", },
  2261. { /* sentinel */ },
  2262. };
  2263. MODULE_DEVICE_TABLE(of, unicam_of_match);
  2264. static struct platform_driver unicam_driver = {
  2265. .probe = unicam_probe,
  2266. .remove_new = unicam_remove,
  2267. .driver = {
  2268. .name = UNICAM_MODULE_NAME,
  2269. .pm = pm_ptr(&unicam_pm_ops),
  2270. .of_match_table = unicam_of_match,
  2271. },
  2272. };
  2273. module_platform_driver(unicam_driver);
  2274. MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
  2275. MODULE_DESCRIPTION("BCM2835 Unicam driver");
  2276. MODULE_LICENSE("GPL");