950-0210-media-bcm2835-unicam-Driver-for-CCP2-CSI2-camera-int.patch 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  1. From 8f4208f42c896b71abe54b697cd044f0af490e67 Mon Sep 17 00:00:00 2001
  2. From: Naushir Patuck <[email protected]>
  3. Date: Wed, 1 Apr 2020 08:39:49 +0100
  4. Subject: [PATCH] media: bcm2835-unicam: Driver for CCP2/CSI2 camera
  5. interface
  6. Add driver for the Unicam camera receiver block on
  7. BCM283x processors.
  8. This commit is made up of a series of changes cherry-picked from the
  9. rpi-4.19.y branch.
  10. Signed-off-by: Dave Stevenson <[email protected]>
  11. Signed-off-by: Naushir Patuck <[email protected]>
  12. ---
  13. MAINTAINERS | 2 +-
  14. drivers/media/platform/Kconfig | 1 +
  15. drivers/media/platform/Makefile | 2 +
  16. drivers/media/platform/bcm2835/Kconfig | 14 +
  17. drivers/media/platform/bcm2835/Makefile | 3 +
  18. .../media/platform/bcm2835/bcm2835-unicam.c | 2369 +++++++++++++++++
  19. .../media/platform/bcm2835/vc4-regs-unicam.h | 253 ++
  20. 7 files changed, 2643 insertions(+), 1 deletion(-)
  21. create mode 100644 drivers/media/platform/bcm2835/Kconfig
  22. create mode 100644 drivers/media/platform/bcm2835/Makefile
  23. create mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
  24. create mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
  25. --- a/MAINTAINERS
  26. +++ b/MAINTAINERS
  27. @@ -3563,7 +3563,7 @@ F: Documentation/devicetree/bindings/med
  28. F: drivers/staging/media/rpivid
  29. BROADCOM BCM2835 CAMERA DRIVER
  30. -M: Dave Stevenson <[email protected]>
  31. +M: Raspberry Pi Kernel Maintenance <[email protected]>
  32. L: [email protected]
  33. S: Maintained
  34. F: drivers/media/platform/bcm2835/
  35. --- a/drivers/media/platform/Kconfig
  36. +++ b/drivers/media/platform/Kconfig
  37. @@ -170,6 +170,7 @@ source "drivers/media/platform/am437x/Kc
  38. source "drivers/media/platform/xilinx/Kconfig"
  39. source "drivers/media/platform/rcar-vin/Kconfig"
  40. source "drivers/media/platform/atmel/Kconfig"
  41. +source "drivers/media/platform/bcm2835/Kconfig"
  42. source "drivers/media/platform/sunxi/Kconfig"
  43. config VIDEO_TI_CAL
  44. --- a/drivers/media/platform/Makefile
  45. +++ b/drivers/media/platform/Makefile
  46. @@ -83,6 +83,8 @@ obj-$(CONFIG_VIDEO_QCOM_CAMSS) += qcom/
  47. obj-$(CONFIG_VIDEO_QCOM_VENUS) += qcom/venus/
  48. +obj-y += bcm2835/
  49. +
  50. obj-y += sunxi/
  51. obj-$(CONFIG_VIDEO_MESON_GE2D) += meson/ge2d/
  52. --- /dev/null
  53. +++ b/drivers/media/platform/bcm2835/Kconfig
  54. @@ -0,0 +1,14 @@
  55. +# Broadcom VideoCore4 V4L2 camera support
  56. +
  57. +config VIDEO_BCM2835_UNICAM
  58. + tristate "Broadcom BCM2835 Unicam video capture driver"
  59. + depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER
  60. + depends on ARCH_BCM2835 || COMPILE_TEST
  61. + select VIDEOBUF2_DMA_CONTIG
  62. + select V4L2_FWNODE
  63. + help
  64. + Say Y here to enable V4L2 subdevice for CSI2 receiver.
  65. + This is a V4L2 subdevice that interfaces directly to the VC4 peripheral.
  66. +
  67. + To compile this driver as a module, choose M here. The module
  68. + will be called bcm2835-unicam.
  69. --- /dev/null
  70. +++ b/drivers/media/platform/bcm2835/Makefile
  71. @@ -0,0 +1,3 @@
  72. +# Makefile for BCM2835 Unicam driver
  73. +
  74. +obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
  75. --- /dev/null
  76. +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
  77. @@ -0,0 +1,2369 @@
  78. +// SPDX-License-Identifier: GPL-2.0-only
  79. +/*
  80. + * BCM2835 Unicam Capture Driver
  81. + *
  82. + * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
  83. + *
  84. + * Dave Stevenson <[email protected]>
  85. + *
  86. + * Based on TI am437x driver by
  87. + * Benoit Parrot <[email protected]>
  88. + * Lad, Prabhakar <[email protected]>
  89. + *
  90. + * and TI CAL camera interface driver by
  91. + * Benoit Parrot <[email protected]>
  92. + *
  93. + *
  94. + * There are two camera drivers in the kernel for BCM283x - this one
  95. + * and bcm2835-camera (currently in staging).
  96. + *
  97. + * This driver directly controls the Unicam peripheral - there is no
  98. + * involvement with the VideoCore firmware. Unicam receives CSI-2 or
  99. + * CCP2 data and writes it into SDRAM.
  100. + * The only potential processing options are to repack Bayer data into an
  101. + * alternate format, and applying windowing.
  102. + * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
  103. + * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
  104. + * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
  105. + * formats where the relevant formats are defined, and will automatically
  106. + * configure the repacking as required.
  107. + * Support for windowing may be added later.
  108. + *
  109. + * It should be possible to connect this driver to any sensor with a
  110. + * suitable output interface and V4L2 subdevice driver.
  111. + *
  112. + * bcm2835-camera uses the VideoCore firmware to control the sensor,
  113. + * Unicam, ISP, and all tuner control loops. Fully processed frames are
  114. + * delivered to the driver by the firmware. It only has sensor drivers
  115. + * for Omnivision OV5647, and Sony IMX219 sensors.
  116. + *
  117. + * The two drivers are mutually exclusive for the same Unicam instance.
  118. + * The VideoCore firmware checks the device tree configuration during boot.
  119. + * If it finds device tree nodes called csi0 or csi1 it will block the
  120. + * firmware from accessing the peripheral, and bcm2835-camera will
  121. + * not be able to stream data.
  122. + */
  123. +
  124. +#include <linux/clk.h>
  125. +#include <linux/delay.h>
  126. +#include <linux/device.h>
  127. +#include <linux/err.h>
  128. +#include <linux/init.h>
  129. +#include <linux/interrupt.h>
  130. +#include <linux/io.h>
  131. +#include <linux/module.h>
  132. +#include <linux/of_device.h>
  133. +#include <linux/of_graph.h>
  134. +#include <linux/pinctrl/consumer.h>
  135. +#include <linux/platform_device.h>
  136. +#include <linux/pm_runtime.h>
  137. +#include <linux/slab.h>
  138. +#include <linux/uaccess.h>
  139. +#include <linux/videodev2.h>
  140. +
  141. +#include <media/v4l2-common.h>
  142. +#include <media/v4l2-ctrls.h>
  143. +#include <media/v4l2-dev.h>
  144. +#include <media/v4l2-device.h>
  145. +#include <media/v4l2-dv-timings.h>
  146. +#include <media/v4l2-event.h>
  147. +#include <media/v4l2-ioctl.h>
  148. +#include <media/v4l2-fwnode.h>
  149. +#include <media/videobuf2-dma-contig.h>
  150. +
  151. +#include "vc4-regs-unicam.h"
  152. +
  153. +#define UNICAM_MODULE_NAME "unicam"
  154. +#define UNICAM_VERSION "0.1.0"
  155. +
  156. +static int debug;
  157. +module_param(debug, int, 0644);
  158. +MODULE_PARM_DESC(debug, "Debug level 0-3");
  159. +
  160. +#define unicam_dbg(level, dev, fmt, arg...) \
  161. + v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
  162. +#define unicam_info(dev, fmt, arg...) \
  163. + v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
  164. +#define unicam_err(dev, fmt, arg...) \
  165. + v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
  166. +
  167. +/* To protect against a dodgy sensor driver never returning an error from
  168. + * enum_mbus_code, set a maximum index value to be used.
  169. + */
  170. +#define MAX_ENUM_MBUS_CODE 128
  171. +
  172. +/*
  173. + * Stride is a 16 bit register, but also has to be a multiple of 32.
  174. + */
  175. +#define BPL_ALIGNMENT 32
  176. +#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
  177. +/*
  178. + * Max width is therefore determined by the max stride divided by
  179. + * the number of bits per pixel. Take 32bpp as a
  180. + * worst case.
  181. + * No imposed limit on the height, so adopt a square image for want
  182. + * of anything better.
  183. + */
  184. +#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
  185. +#define MAX_HEIGHT MAX_WIDTH
  186. +/* Define a nominal minimum image size */
  187. +#define MIN_WIDTH 16
  188. +#define MIN_HEIGHT 16
  189. +
  190. +/*
  191. + * struct unicam_fmt - Unicam media bus format information
  192. + * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
  193. + * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
  194. + * out to 16bpp. 0 if n/a.
  195. + * @code: V4L2 media bus format code.
  196. + * @depth: Bits per pixel as delivered from the source.
  197. + * @csi_dt: CSI data type.
  198. + * @check_variants: Flag to denote that there are multiple mediabus formats
  199. + * still in the list that could match this V4L2 format.
  200. + */
  201. +struct unicam_fmt {
  202. + u32 fourcc;
  203. + u32 repacked_fourcc;
  204. + u32 code;
  205. + u8 depth;
  206. + u8 csi_dt;
  207. + u8 check_variants;
  208. +};
  209. +
  210. +static const struct unicam_fmt formats[] = {
  211. + /* YUV Formats */
  212. + {
  213. + .fourcc = V4L2_PIX_FMT_YUYV,
  214. + .code = MEDIA_BUS_FMT_YUYV8_2X8,
  215. + .depth = 16,
  216. + .csi_dt = 0x1e,
  217. + .check_variants = 1,
  218. + }, {
  219. + .fourcc = V4L2_PIX_FMT_UYVY,
  220. + .code = MEDIA_BUS_FMT_UYVY8_2X8,
  221. + .depth = 16,
  222. + .csi_dt = 0x1e,
  223. + .check_variants = 1,
  224. + }, {
  225. + .fourcc = V4L2_PIX_FMT_YVYU,
  226. + .code = MEDIA_BUS_FMT_YVYU8_2X8,
  227. + .depth = 16,
  228. + .csi_dt = 0x1e,
  229. + .check_variants = 1,
  230. + }, {
  231. + .fourcc = V4L2_PIX_FMT_VYUY,
  232. + .code = MEDIA_BUS_FMT_VYUY8_2X8,
  233. + .depth = 16,
  234. + .csi_dt = 0x1e,
  235. + .check_variants = 1,
  236. + }, {
  237. + .fourcc = V4L2_PIX_FMT_YUYV,
  238. + .code = MEDIA_BUS_FMT_YUYV8_1X16,
  239. + .depth = 16,
  240. + .csi_dt = 0x1e,
  241. + }, {
  242. + .fourcc = V4L2_PIX_FMT_UYVY,
  243. + .code = MEDIA_BUS_FMT_UYVY8_1X16,
  244. + .depth = 16,
  245. + .csi_dt = 0x1e,
  246. + }, {
  247. + .fourcc = V4L2_PIX_FMT_YVYU,
  248. + .code = MEDIA_BUS_FMT_YVYU8_1X16,
  249. + .depth = 16,
  250. + .csi_dt = 0x1e,
  251. + }, {
  252. + .fourcc = V4L2_PIX_FMT_VYUY,
  253. + .code = MEDIA_BUS_FMT_VYUY8_1X16,
  254. + .depth = 16,
  255. + .csi_dt = 0x1e,
  256. + }, {
  257. + /* RGB Formats */
  258. + .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  259. + .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  260. + .depth = 16,
  261. + .csi_dt = 0x22,
  262. + }, {
  263. + .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  264. + .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  265. + .depth = 16,
  266. + .csi_dt = 0x22
  267. + }, {
  268. + .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
  269. + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  270. + .depth = 16,
  271. + .csi_dt = 0x21,
  272. + }, {
  273. + .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
  274. + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
  275. + .depth = 16,
  276. + .csi_dt = 0x21,
  277. + }, {
  278. + .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  279. + .code = MEDIA_BUS_FMT_RGB888_1X24,
  280. + .depth = 24,
  281. + .csi_dt = 0x24,
  282. + }, {
  283. + .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  284. + .code = MEDIA_BUS_FMT_BGR888_1X24,
  285. + .depth = 24,
  286. + .csi_dt = 0x24,
  287. + }, {
  288. + .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
  289. + .code = MEDIA_BUS_FMT_ARGB8888_1X32,
  290. + .depth = 32,
  291. + .csi_dt = 0x0,
  292. + }, {
  293. + /* Bayer Formats */
  294. + .fourcc = V4L2_PIX_FMT_SBGGR8,
  295. + .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  296. + .depth = 8,
  297. + .csi_dt = 0x2a,
  298. + }, {
  299. + .fourcc = V4L2_PIX_FMT_SGBRG8,
  300. + .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  301. + .depth = 8,
  302. + .csi_dt = 0x2a,
  303. + }, {
  304. + .fourcc = V4L2_PIX_FMT_SGRBG8,
  305. + .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  306. + .depth = 8,
  307. + .csi_dt = 0x2a,
  308. + }, {
  309. + .fourcc = V4L2_PIX_FMT_SRGGB8,
  310. + .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  311. + .depth = 8,
  312. + .csi_dt = 0x2a,
  313. + }, {
  314. + .fourcc = V4L2_PIX_FMT_SBGGR10P,
  315. + .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
  316. + .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  317. + .depth = 10,
  318. + .csi_dt = 0x2b,
  319. + }, {
  320. + .fourcc = V4L2_PIX_FMT_SGBRG10P,
  321. + .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
  322. + .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  323. + .depth = 10,
  324. + .csi_dt = 0x2b,
  325. + }, {
  326. + .fourcc = V4L2_PIX_FMT_SGRBG10P,
  327. + .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
  328. + .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  329. + .depth = 10,
  330. + .csi_dt = 0x2b,
  331. + }, {
  332. + .fourcc = V4L2_PIX_FMT_SRGGB10P,
  333. + .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
  334. + .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  335. + .depth = 10,
  336. + .csi_dt = 0x2b,
  337. + }, {
  338. + .fourcc = V4L2_PIX_FMT_SBGGR12P,
  339. + .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
  340. + .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  341. + .depth = 12,
  342. + .csi_dt = 0x2c,
  343. + }, {
  344. + .fourcc = V4L2_PIX_FMT_SGBRG12P,
  345. + .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
  346. + .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  347. + .depth = 12,
  348. + .csi_dt = 0x2c,
  349. + }, {
  350. + .fourcc = V4L2_PIX_FMT_SGRBG12P,
  351. + .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
  352. + .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  353. + .depth = 12,
  354. + .csi_dt = 0x2c,
  355. + }, {
  356. + .fourcc = V4L2_PIX_FMT_SRGGB12P,
  357. + .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
  358. + .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  359. + .depth = 12,
  360. + .csi_dt = 0x2c,
  361. + }, {
  362. + .fourcc = V4L2_PIX_FMT_SBGGR14P,
  363. + .code = MEDIA_BUS_FMT_SBGGR14_1X14,
  364. + .depth = 14,
  365. + .csi_dt = 0x2d,
  366. + }, {
  367. + .fourcc = V4L2_PIX_FMT_SGBRG14P,
  368. + .code = MEDIA_BUS_FMT_SGBRG14_1X14,
  369. + .depth = 14,
  370. + .csi_dt = 0x2d,
  371. + }, {
  372. + .fourcc = V4L2_PIX_FMT_SGRBG14P,
  373. + .code = MEDIA_BUS_FMT_SGRBG14_1X14,
  374. + .depth = 14,
  375. + .csi_dt = 0x2d,
  376. + }, {
  377. + .fourcc = V4L2_PIX_FMT_SRGGB14P,
  378. + .code = MEDIA_BUS_FMT_SRGGB14_1X14,
  379. + .depth = 14,
  380. + .csi_dt = 0x2d,
  381. + }, {
  382. + /*
  383. + * 16 bit Bayer formats could be supported, but there is no CSI2
  384. + * data_type defined for raw 16, and no sensors that produce it at
  385. + * present.
  386. + */
  387. +
  388. + /* Greyscale formats */
  389. + .fourcc = V4L2_PIX_FMT_GREY,
  390. + .code = MEDIA_BUS_FMT_Y8_1X8,
  391. + .depth = 8,
  392. + .csi_dt = 0x2a,
  393. + }, {
  394. + .fourcc = V4L2_PIX_FMT_Y10P,
  395. + .repacked_fourcc = V4L2_PIX_FMT_Y10,
  396. + .code = MEDIA_BUS_FMT_Y10_1X10,
  397. + .depth = 10,
  398. + .csi_dt = 0x2b,
  399. + }, {
  400. + /* NB There is no packed V4L2 fourcc for this format. */
  401. + .repacked_fourcc = V4L2_PIX_FMT_Y12,
  402. + .code = MEDIA_BUS_FMT_Y12_1X12,
  403. + .depth = 12,
  404. + .csi_dt = 0x2c,
  405. + },
  406. +};
  407. +
  408. +struct unicam_dmaqueue {
  409. + struct list_head active;
  410. +};
  411. +
  412. +struct unicam_buffer {
  413. + struct vb2_v4l2_buffer vb;
  414. + struct list_head list;
  415. +};
  416. +
  417. +struct unicam_cfg {
  418. + /* peripheral base address */
  419. + void __iomem *base;
  420. + /* clock gating base address */
  421. + void __iomem *clk_gate_base;
  422. +};
  423. +
  424. +#define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
  425. +
  426. +struct unicam_device {
  427. + /* V4l2 specific parameters */
  428. + /* Identifies video device for this channel */
  429. + struct video_device video_dev;
  430. + struct v4l2_ctrl_handler ctrl_handler;
  431. +
  432. + struct v4l2_fwnode_endpoint endpoint;
  433. +
  434. + struct v4l2_async_subdev asd;
  435. +
  436. + /* unicam cfg */
  437. + struct unicam_cfg cfg;
  438. + /* clock handle */
  439. + struct clk *clock;
  440. + /* V4l2 device */
  441. + struct v4l2_device v4l2_dev;
  442. + struct media_device mdev;
  443. + struct media_pad pad;
  444. +
  445. + /* parent device */
  446. + struct platform_device *pdev;
  447. + /* subdevice async Notifier */
  448. + struct v4l2_async_notifier notifier;
  449. + unsigned int sequence;
  450. +
  451. + /* ptr to sub device */
  452. + struct v4l2_subdev *sensor;
  453. + /* Pad config for the sensor */
  454. + struct v4l2_subdev_pad_config *sensor_config;
  455. + /* current input at the sub device */
  456. + int current_input;
  457. +
  458. + /* Pointer pointing to current v4l2_buffer */
  459. + struct unicam_buffer *cur_frm;
  460. + /* Pointer pointing to next v4l2_buffer */
  461. + struct unicam_buffer *next_frm;
  462. +
  463. + /* video capture */
  464. + const struct unicam_fmt *fmt;
  465. + /* Used to store current pixel format */
  466. + struct v4l2_format v_fmt;
  467. + /* Used to store current mbus frame format */
  468. + struct v4l2_mbus_framefmt m_fmt;
  469. +
  470. + unsigned int virtual_channel;
  471. + enum v4l2_mbus_type bus_type;
  472. + /*
  473. + * Stores bus.mipi_csi2.flags for CSI2 sensors, or
  474. + * bus.mipi_csi1.strobe for CCP2.
  475. + */
  476. + unsigned int bus_flags;
  477. + unsigned int max_data_lanes;
  478. + unsigned int active_data_lanes;
  479. +
  480. + struct v4l2_rect crop;
  481. +
  482. + /* Currently selected input on subdev */
  483. + int input;
  484. +
  485. + /* Buffer queue used in video-buf */
  486. + struct vb2_queue buffer_queue;
  487. + /* Queue of filled frames */
  488. + struct unicam_dmaqueue dma_queue;
  489. + /* IRQ lock for DMA queue */
  490. + spinlock_t dma_queue_lock;
  491. + /* lock used to access this structure */
  492. + struct mutex lock;
  493. + /* Flag to denote that we are processing buffers */
  494. + int streaming;
  495. +};
  496. +
  497. +/* Hardware access */
  498. +#define clk_write(dev, val) writel((val) | 0x5a000000, (dev)->clk_gate_base)
  499. +#define clk_read(dev) readl((dev)->clk_gate_base)
  500. +
  501. +#define reg_read(dev, offset) readl((dev)->base + (offset))
  502. +#define reg_write(dev, offset, val) writel(val, (dev)->base + (offset))
  503. +
  504. +#define reg_read_field(dev, offset, mask) get_field(reg_read((dev), (offset), \
  505. + mask))
  506. +
  507. +static inline int get_field(u32 value, u32 mask)
  508. +{
  509. + return (value & mask) >> __ffs(mask);
  510. +}
  511. +
  512. +static inline void set_field(u32 *valp, u32 field, u32 mask)
  513. +{
  514. + u32 val = *valp;
  515. +
  516. + val &= ~mask;
  517. + val |= (field << __ffs(mask)) & mask;
  518. + *valp = val;
  519. +}
  520. +
  521. +static inline void reg_write_field(struct unicam_cfg *dev, u32 offset,
  522. + u32 field, u32 mask)
  523. +{
  524. + u32 val = reg_read((dev), (offset));
  525. +
  526. + set_field(&val, field, mask);
  527. + reg_write((dev), (offset), val);
  528. +}
  529. +
  530. +/* Power management functions */
  531. +static inline int unicam_runtime_get(struct unicam_device *dev)
  532. +{
  533. + return pm_runtime_get_sync(&dev->pdev->dev);
  534. +}
  535. +
  536. +static inline void unicam_runtime_put(struct unicam_device *dev)
  537. +{
  538. + pm_runtime_put_sync(&dev->pdev->dev);
  539. +}
  540. +
  541. +/* Format setup functions */
  542. +static const struct unicam_fmt *find_format_by_code(u32 code)
  543. +{
  544. + unsigned int i;
  545. +
  546. + for (i = 0; i < ARRAY_SIZE(formats); i++) {
  547. + if (formats[i].code == code)
  548. + return &formats[i];
  549. + }
  550. +
  551. + return NULL;
  552. +}
  553. +
  554. +static int check_mbus_format(struct unicam_device *dev,
  555. + const struct unicam_fmt *format)
  556. +{
  557. + struct v4l2_subdev_mbus_code_enum mbus_code;
  558. + int ret = 0;
  559. + int i;
  560. +
  561. + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
  562. + memset(&mbus_code, 0, sizeof(mbus_code));
  563. + mbus_code.index = i;
  564. + mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  565. +
  566. + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
  567. + NULL, &mbus_code);
  568. +
  569. + if (!ret && mbus_code.code == format->code)
  570. + return 1;
  571. + }
  572. +
  573. + return 0;
  574. +}
  575. +
  576. +static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
  577. + u32 pixelformat)
  578. +{
  579. + unsigned int i;
  580. +
  581. + for (i = 0; i < ARRAY_SIZE(formats); i++) {
  582. + if (formats[i].fourcc == pixelformat ||
  583. + formats[i].repacked_fourcc == pixelformat) {
  584. + if (formats[i].check_variants &&
  585. + !check_mbus_format(dev, &formats[i]))
  586. + continue;
  587. + return &formats[i];
  588. + }
  589. + }
  590. +
  591. + return NULL;
  592. +}
  593. +
  594. +static inline unsigned int bytes_per_line(u32 width,
  595. + const struct unicam_fmt *fmt,
  596. + u32 v4l2_fourcc)
  597. +{
  598. + if (v4l2_fourcc == fmt->repacked_fourcc)
  599. + /* Repacking always goes to 16bpp */
  600. + return ALIGN(width << 1, BPL_ALIGNMENT);
  601. + else
  602. + return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
  603. +}
  604. +
  605. +static int __subdev_get_format(struct unicam_device *dev,
  606. + struct v4l2_mbus_framefmt *fmt)
  607. +{
  608. + struct v4l2_subdev_format sd_fmt = {
  609. + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  610. + };
  611. + int ret;
  612. +
  613. + ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
  614. + &sd_fmt);
  615. + if (ret < 0)
  616. + return ret;
  617. +
  618. + *fmt = sd_fmt.format;
  619. +
  620. + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
  621. + fmt->width, fmt->height, fmt->code);
  622. +
  623. + return 0;
  624. +}
  625. +
  626. +static int __subdev_set_format(struct unicam_device *dev,
  627. + struct v4l2_mbus_framefmt *fmt)
  628. +{
  629. + struct v4l2_subdev_format sd_fmt = {
  630. + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  631. + };
  632. + int ret;
  633. +
  634. + sd_fmt.format = *fmt;
  635. +
  636. + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
  637. + &sd_fmt);
  638. + if (ret < 0)
  639. + return ret;
  640. +
  641. + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
  642. + fmt->width, fmt->height, fmt->code);
  643. +
  644. + return 0;
  645. +}
  646. +
  647. +static int unicam_calc_format_size_bpl(struct unicam_device *dev,
  648. + const struct unicam_fmt *fmt,
  649. + struct v4l2_format *f)
  650. +{
  651. + unsigned int min_bytesperline;
  652. +
  653. + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
  654. + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
  655. + 0);
  656. +
  657. + min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
  658. + f->fmt.pix.pixelformat);
  659. +
  660. + if (f->fmt.pix.bytesperline > min_bytesperline &&
  661. + f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
  662. + f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
  663. + BPL_ALIGNMENT);
  664. + else
  665. + f->fmt.pix.bytesperline = min_bytesperline;
  666. +
  667. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  668. +
  669. + unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
  670. + __func__,
  671. + f->fmt.pix.pixelformat,
  672. + f->fmt.pix.width, f->fmt.pix.height,
  673. + f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
  674. +
  675. + return 0;
  676. +}
  677. +
  678. +static int unicam_reset_format(struct unicam_device *dev)
  679. +{
  680. + struct v4l2_mbus_framefmt mbus_fmt;
  681. + int ret;
  682. +
  683. + ret = __subdev_get_format(dev, &mbus_fmt);
  684. + if (ret) {
  685. + unicam_err(dev, "Failed to get_format - ret %d\n", ret);
  686. + return ret;
  687. + }
  688. +
  689. + if (mbus_fmt.code != dev->fmt->code) {
  690. + unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
  691. + dev->fmt->code, mbus_fmt.code);
  692. + return ret;
  693. + }
  694. +
  695. + v4l2_fill_pix_format(&dev->v_fmt.fmt.pix, &mbus_fmt);
  696. + dev->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  697. +
  698. + unicam_calc_format_size_bpl(dev, dev->fmt, &dev->v_fmt);
  699. +
  700. + dev->m_fmt = mbus_fmt;
  701. +
  702. + return 0;
  703. +}
  704. +
  705. +static void unicam_wr_dma_addr(struct unicam_device *dev, dma_addr_t dmaaddr)
  706. +{
  707. + /*
  708. + * dmaaddr should be a 32-bit address with the top two bits set to 0x3
  709. + * to signify uncached access through the Videocore memory controller.
  710. + */
  711. + BUG_ON((dmaaddr >> 30) != 0x3);
  712. +
  713. + reg_write(&dev->cfg, UNICAM_IBSA0, dmaaddr);
  714. + reg_write(&dev->cfg, UNICAM_IBEA0,
  715. + dmaaddr + dev->v_fmt.fmt.pix.sizeimage);
  716. +}
  717. +
  718. +static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
  719. +{
  720. + dma_addr_t start_addr, cur_addr;
  721. + unsigned int stride = dev->v_fmt.fmt.pix.bytesperline;
  722. + struct unicam_buffer *frm = dev->cur_frm;
  723. +
  724. + if (!frm)
  725. + return 0;
  726. +
  727. + start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
  728. + cur_addr = reg_read(&dev->cfg, UNICAM_IBWP);
  729. + return (unsigned int)(cur_addr - start_addr) / stride;
  730. +}
  731. +
  732. +static inline void unicam_schedule_next_buffer(struct unicam_device *dev)
  733. +{
  734. + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
  735. + struct unicam_buffer *buf;
  736. + dma_addr_t addr;
  737. +
  738. + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
  739. + dev->next_frm = buf;
  740. + list_del(&buf->list);
  741. +
  742. + addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  743. + unicam_wr_dma_addr(dev, addr);
  744. +}
  745. +
  746. +static inline void unicam_process_buffer_complete(struct unicam_device *dev)
  747. +{
  748. + dev->cur_frm->vb.field = dev->m_fmt.field;
  749. + dev->cur_frm->vb.sequence = dev->sequence++;
  750. +
  751. + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  752. + dev->cur_frm = dev->next_frm;
  753. +}
  754. +
  755. +/*
  756. + * unicam_isr : ISR handler for unicam capture
  757. + * @irq: irq number
  758. + * @dev_id: dev_id ptr
  759. + *
  760. + * It changes status of the captured buffer, takes next buffer from the queue
  761. + * and sets its address in unicam registers
  762. + */
  763. +static irqreturn_t unicam_isr(int irq, void *dev)
  764. +{
  765. + struct unicam_device *unicam = (struct unicam_device *)dev;
  766. + struct unicam_cfg *cfg = &unicam->cfg;
  767. + struct unicam_dmaqueue *dma_q = &unicam->dma_queue;
  768. + unsigned int lines_done = unicam_get_lines_done(dev);
  769. + unsigned int sequence = unicam->sequence;
  770. + int ista, sta;
  771. +
  772. + /*
  773. + * Don't service interrupts if not streaming.
  774. + * Avoids issues if the VPU should enable the
  775. + * peripheral without the kernel knowing (that
  776. + * shouldn't happen, but causes issues if it does).
  777. + */
  778. + if (!unicam->streaming)
  779. + return IRQ_HANDLED;
  780. +
  781. + sta = reg_read(cfg, UNICAM_STA);
  782. + /* Write value back to clear the interrupts */
  783. + reg_write(cfg, UNICAM_STA, sta);
  784. +
  785. + ista = reg_read(cfg, UNICAM_ISTA);
  786. + /* Write value back to clear the interrupts */
  787. + reg_write(cfg, UNICAM_ISTA, ista);
  788. +
  789. + unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
  790. + ista, sta, sequence, lines_done);
  791. +
  792. + if (!(sta && (UNICAM_IS | UNICAM_PI0)))
  793. + return IRQ_HANDLED;
  794. +
  795. + if (ista & UNICAM_FSI) {
  796. + /*
  797. + * Timestamp is to be when the first data byte was captured,
  798. + * aka frame start.
  799. + */
  800. + if (unicam->cur_frm)
  801. + unicam->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  802. + }
  803. + if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
  804. + /*
  805. + * Ensure we have swapped buffers already as we can't
  806. + * stop the peripheral. Overwrite the frame we've just
  807. + * captured instead.
  808. + */
  809. + if (unicam->cur_frm && unicam->cur_frm != unicam->next_frm)
  810. + unicam_process_buffer_complete(unicam);
  811. + }
  812. +
  813. + /* Cannot swap buffer at frame end, there may be a race condition
  814. + * where the HW does not actually swap it if the new frame has
  815. + * already started.
  816. + */
  817. + if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
  818. + spin_lock(&unicam->dma_queue_lock);
  819. + if (!list_empty(&dma_q->active) &&
  820. + unicam->cur_frm == unicam->next_frm)
  821. + unicam_schedule_next_buffer(unicam);
  822. + spin_unlock(&unicam->dma_queue_lock);
  823. + }
  824. +
  825. + if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
  826. + /* Switch out of trigger mode if selected */
  827. + reg_write_field(&unicam->cfg, UNICAM_ICTL, 1, UNICAM_TFC);
  828. + reg_write_field(&unicam->cfg, UNICAM_ICTL, 0, UNICAM_FCM);
  829. + }
  830. + return IRQ_HANDLED;
  831. +}
  832. +
  833. +static int unicam_querycap(struct file *file, void *priv,
  834. + struct v4l2_capability *cap)
  835. +{
  836. + struct unicam_device *dev = video_drvdata(file);
  837. +
  838. + strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
  839. + strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
  840. +
  841. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  842. + "platform:%s", dev->v4l2_dev.name);
  843. +
  844. + return 0;
  845. +}
  846. +
  847. +static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
  848. + struct v4l2_fmtdesc *f)
  849. +{
  850. + struct unicam_device *dev = video_drvdata(file);
  851. + struct v4l2_subdev_mbus_code_enum mbus_code;
  852. + const struct unicam_fmt *fmt = NULL;
  853. + int index = 0;
  854. + int ret = 0;
  855. + int i;
  856. +
  857. + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
  858. + memset(&mbus_code, 0, sizeof(mbus_code));
  859. + mbus_code.index = i;
  860. +
  861. + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
  862. + NULL, &mbus_code);
  863. + if (ret < 0) {
  864. + unicam_dbg(2, dev,
  865. + "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
  866. + i, ret);
  867. + return -EINVAL;
  868. + }
  869. +
  870. + fmt = find_format_by_code(mbus_code.code);
  871. + if (fmt) {
  872. + if (fmt->fourcc) {
  873. + if (index == f->index) {
  874. + f->pixelformat = fmt->fourcc;
  875. + break;
  876. + }
  877. + index++;
  878. + }
  879. + if (fmt->repacked_fourcc) {
  880. + if (index == f->index) {
  881. + f->pixelformat = fmt->repacked_fourcc;
  882. + break;
  883. + }
  884. + index++;
  885. + }
  886. + }
  887. + }
  888. +
  889. + return 0;
  890. +}
  891. +
  892. +static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
  893. + struct v4l2_format *f)
  894. +{
  895. + struct unicam_device *dev = video_drvdata(file);
  896. +
  897. + *f = dev->v_fmt;
  898. +
  899. + return 0;
  900. +}
  901. +
  902. +static
  903. +const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
  904. +{
  905. + struct v4l2_subdev_mbus_code_enum mbus_code;
  906. + const struct unicam_fmt *fmt = NULL;
  907. + int ret;
  908. + int j;
  909. +
  910. + for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
  911. + memset(&mbus_code, 0, sizeof(mbus_code));
  912. + mbus_code.index = j;
  913. + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
  914. + &mbus_code);
  915. + if (ret < 0) {
  916. + unicam_dbg(2, dev,
  917. + "subdev->enum_mbus_code idx %d returned %d - continue\n",
  918. + j, ret);
  919. + continue;
  920. + }
  921. +
  922. + unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %d\n",
  923. + dev->sensor->name, mbus_code.code, j);
  924. +
  925. + fmt = find_format_by_code(mbus_code.code);
  926. + unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
  927. + mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
  928. + fmt ? fmt->csi_dt : 0);
  929. + if (fmt)
  930. + return fmt;
  931. + }
  932. +
  933. + return NULL;
  934. +}
  935. +
  936. +static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
  937. + struct v4l2_format *f)
  938. +{
  939. + struct unicam_device *dev = video_drvdata(file);
  940. + struct v4l2_subdev_format sd_fmt = {
  941. + .which = V4L2_SUBDEV_FORMAT_TRY,
  942. + };
  943. + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
  944. + const struct unicam_fmt *fmt;
  945. + int ret;
  946. +
  947. + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
  948. + if (!fmt) {
  949. + /* Pixel format not supported by unicam. Choose the first
  950. + * supported format, and let the sensor choose something else.
  951. + */
  952. + unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
  953. + f->fmt.pix.pixelformat);
  954. +
  955. + fmt = &formats[0];
  956. + f->fmt.pix.pixelformat = fmt->fourcc;
  957. + }
  958. +
  959. + v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
  960. + /*
  961. + * No support for receiving interlaced video, so never
  962. + * request it from the sensor subdev.
  963. + */
  964. + mbus_fmt->field = V4L2_FIELD_NONE;
  965. +
  966. + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
  967. + &sd_fmt);
  968. + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
  969. + return ret;
  970. +
  971. + if (mbus_fmt->field != V4L2_FIELD_NONE)
  972. + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
  973. +
  974. + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
  975. + if (mbus_fmt->code != fmt->code) {
  976. + /* Sensor has returned an alternate format */
  977. + fmt = find_format_by_code(mbus_fmt->code);
  978. + if (!fmt) {
  979. + /* The alternate format is one unicam can't support.
  980. + * Find the first format that is supported by both, and
  981. + * then set that.
  982. + */
  983. + fmt = get_first_supported_format(dev);
  984. + mbus_fmt->code = fmt->code;
  985. +
  986. + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
  987. + dev->sensor_config, &sd_fmt);
  988. + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
  989. + return ret;
  990. +
  991. + if (mbus_fmt->field != V4L2_FIELD_NONE)
  992. + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
  993. +
  994. + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
  995. +
  996. + if (mbus_fmt->code != fmt->code) {
  997. + /* We've set a format that the sensor reports
  998. + * as being supported, but it refuses to set it.
  999. + * Not much else we can do.
  1000. + * Assume that the sensor driver may accept the
  1001. + * format when it is set (rather than tried).
  1002. + */
  1003. + unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
  1004. + }
  1005. + }
  1006. +
  1007. + if (fmt->fourcc)
  1008. + f->fmt.pix.pixelformat = fmt->fourcc;
  1009. + else
  1010. + f->fmt.pix.pixelformat = fmt->repacked_fourcc;
  1011. + }
  1012. +
  1013. + return unicam_calc_format_size_bpl(dev, fmt, f);
  1014. +}
  1015. +
  1016. +static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
  1017. + struct v4l2_format *f)
  1018. +{
  1019. + struct unicam_device *dev = video_drvdata(file);
  1020. + struct vb2_queue *q = &dev->buffer_queue;
  1021. + struct v4l2_mbus_framefmt mbus_fmt = {0};
  1022. + const struct unicam_fmt *fmt;
  1023. + int ret;
  1024. +
  1025. + if (vb2_is_busy(q))
  1026. + return -EBUSY;
  1027. +
  1028. + ret = unicam_try_fmt_vid_cap(file, priv, f);
  1029. + if (ret < 0)
  1030. + return ret;
  1031. +
  1032. + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
  1033. + if (!fmt) {
  1034. + /* Unknown pixel format - adopt a default.
  1035. + * This shouldn't happen as try_fmt should have resolved any
  1036. + * issues first.
  1037. + */
  1038. + fmt = get_first_supported_format(dev);
  1039. + if (!fmt)
  1040. + /* It shouldn't be possible to get here with no
  1041. + * supported formats
  1042. + */
  1043. + return -EINVAL;
  1044. + f->fmt.pix.pixelformat = fmt->fourcc;
  1045. + return -EINVAL;
  1046. + }
  1047. +
  1048. + v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
  1049. +
  1050. + ret = __subdev_set_format(dev, &mbus_fmt);
  1051. + if (ret) {
  1052. + unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
  1053. + __func__, ret);
  1054. + return ret;
  1055. + }
  1056. +
  1057. + /* Just double check nothing has gone wrong */
  1058. + if (mbus_fmt.code != fmt->code) {
  1059. + unicam_dbg(3, dev,
  1060. + "%s subdev changed format on us, this should not happen\n",
  1061. + __func__);
  1062. + return -EINVAL;
  1063. + }
  1064. +
  1065. + dev->fmt = fmt;
  1066. + dev->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
  1067. + dev->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
  1068. + unicam_reset_format(dev);
  1069. +
  1070. + unicam_dbg(3, dev, "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
  1071. + __func__, dev->v_fmt.fmt.pix.width,
  1072. + dev->v_fmt.fmt.pix.height, mbus_fmt.code,
  1073. + dev->v_fmt.fmt.pix.pixelformat);
  1074. +
  1075. + *f = dev->v_fmt;
  1076. +
  1077. + return 0;
  1078. +}
  1079. +
  1080. +static int unicam_queue_setup(struct vb2_queue *vq,
  1081. + unsigned int *nbuffers,
  1082. + unsigned int *nplanes,
  1083. + unsigned int sizes[],
  1084. + struct device *alloc_devs[])
  1085. +{
  1086. + struct unicam_device *dev = vb2_get_drv_priv(vq);
  1087. + unsigned int size = dev->v_fmt.fmt.pix.sizeimage;
  1088. +
  1089. + if (vq->num_buffers + *nbuffers < 3)
  1090. + *nbuffers = 3 - vq->num_buffers;
  1091. +
  1092. + if (*nplanes) {
  1093. + if (sizes[0] < size) {
  1094. + unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
  1095. + size);
  1096. + return -EINVAL;
  1097. + }
  1098. + size = sizes[0];
  1099. + }
  1100. +
  1101. + *nplanes = 1;
  1102. + sizes[0] = size;
  1103. +
  1104. + return 0;
  1105. +}
  1106. +
  1107. +static int unicam_buffer_prepare(struct vb2_buffer *vb)
  1108. +{
  1109. + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
  1110. + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
  1111. + vb.vb2_buf);
  1112. + unsigned long size;
  1113. +
  1114. + if (WARN_ON(!dev->fmt))
  1115. + return -EINVAL;
  1116. +
  1117. + size = dev->v_fmt.fmt.pix.sizeimage;
  1118. + if (vb2_plane_size(vb, 0) < size) {
  1119. + unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
  1120. + vb2_plane_size(vb, 0), size);
  1121. + return -EINVAL;
  1122. + }
  1123. +
  1124. + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  1125. + return 0;
  1126. +}
  1127. +
  1128. +static void unicam_buffer_queue(struct vb2_buffer *vb)
  1129. +{
  1130. + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
  1131. + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
  1132. + vb.vb2_buf);
  1133. + struct unicam_dmaqueue *dma_queue = &dev->dma_queue;
  1134. + unsigned long flags = 0;
  1135. +
  1136. + spin_lock_irqsave(&dev->dma_queue_lock, flags);
  1137. + list_add_tail(&buf->list, &dma_queue->active);
  1138. + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
  1139. +}
  1140. +
  1141. +static void unicam_set_packing_config(struct unicam_device *dev)
  1142. +{
  1143. + int pack, unpack;
  1144. + u32 val;
  1145. +
  1146. + if (dev->v_fmt.fmt.pix.pixelformat == dev->fmt->fourcc) {
  1147. + unpack = UNICAM_PUM_NONE;
  1148. + pack = UNICAM_PPM_NONE;
  1149. + } else {
  1150. + switch (dev->fmt->depth) {
  1151. + case 8:
  1152. + unpack = UNICAM_PUM_UNPACK8;
  1153. + break;
  1154. + case 10:
  1155. + unpack = UNICAM_PUM_UNPACK10;
  1156. + break;
  1157. + case 12:
  1158. + unpack = UNICAM_PUM_UNPACK12;
  1159. + break;
  1160. + case 14:
  1161. + unpack = UNICAM_PUM_UNPACK14;
  1162. + break;
  1163. + case 16:
  1164. + unpack = UNICAM_PUM_UNPACK16;
  1165. + break;
  1166. + default:
  1167. + unpack = UNICAM_PUM_NONE;
  1168. + break;
  1169. + }
  1170. +
  1171. + /* Repacking is always to 16bpp */
  1172. + pack = UNICAM_PPM_PACK16;
  1173. + }
  1174. +
  1175. + val = 0;
  1176. + set_field(&val, unpack, UNICAM_PUM_MASK);
  1177. + set_field(&val, pack, UNICAM_PPM_MASK);
  1178. + reg_write(&dev->cfg, UNICAM_IPIPE, val);
  1179. +}
  1180. +
  1181. +static void unicam_cfg_image_id(struct unicam_device *dev)
  1182. +{
  1183. + struct unicam_cfg *cfg = &dev->cfg;
  1184. +
  1185. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
  1186. + /* CSI2 mode */
  1187. + reg_write(cfg, UNICAM_IDI0,
  1188. + (dev->virtual_channel << 6) | dev->fmt->csi_dt);
  1189. + } else {
  1190. + /* CCP2 mode */
  1191. + reg_write(cfg, UNICAM_IDI0, (0x80 | dev->fmt->csi_dt));
  1192. + }
  1193. +}
  1194. +
  1195. +static void unicam_start_rx(struct unicam_device *dev, unsigned long addr)
  1196. +{
  1197. + struct unicam_cfg *cfg = &dev->cfg;
  1198. + int line_int_freq = dev->v_fmt.fmt.pix.height >> 2;
  1199. + unsigned int i;
  1200. + u32 val;
  1201. +
  1202. + if (line_int_freq < 128)
  1203. + line_int_freq = 128;
  1204. +
  1205. + /* Enable lane clocks */
  1206. + val = 1;
  1207. + for (i = 0; i < dev->active_data_lanes; i++)
  1208. + val = val << 2 | 1;
  1209. + clk_write(cfg, val);
  1210. +
  1211. + /* Basic init */
  1212. + reg_write(cfg, UNICAM_CTRL, UNICAM_MEM);
  1213. +
  1214. + /* Enable analogue control, and leave in reset. */
  1215. + val = UNICAM_AR;
  1216. + set_field(&val, 7, UNICAM_CTATADJ_MASK);
  1217. + set_field(&val, 7, UNICAM_PTATADJ_MASK);
  1218. + reg_write(cfg, UNICAM_ANA, val);
  1219. + usleep_range(1000, 2000);
  1220. +
  1221. + /* Come out of reset */
  1222. + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_AR);
  1223. +
  1224. + /* Peripheral reset */
  1225. + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
  1226. + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
  1227. +
  1228. + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
  1229. +
  1230. + /* Enable Rx control. */
  1231. + val = reg_read(cfg, UNICAM_CTRL);
  1232. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
  1233. + set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
  1234. + set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
  1235. + } else {
  1236. + set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
  1237. + set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
  1238. + }
  1239. + /* Packet framer timeout */
  1240. + set_field(&val, 0xf, UNICAM_PFT_MASK);
  1241. + set_field(&val, 128, UNICAM_OET_MASK);
  1242. + reg_write(cfg, UNICAM_CTRL, val);
  1243. +
  1244. + reg_write(cfg, UNICAM_IHWIN, 0);
  1245. + reg_write(cfg, UNICAM_IVWIN, 0);
  1246. +
  1247. + /* AXI bus access QoS setup */
  1248. + val = reg_read(&dev->cfg, UNICAM_PRI);
  1249. + set_field(&val, 0, UNICAM_BL_MASK);
  1250. + set_field(&val, 0, UNICAM_BS_MASK);
  1251. + set_field(&val, 0xe, UNICAM_PP_MASK);
  1252. + set_field(&val, 8, UNICAM_NP_MASK);
  1253. + set_field(&val, 2, UNICAM_PT_MASK);
  1254. + set_field(&val, 1, UNICAM_PE);
  1255. + reg_write(cfg, UNICAM_PRI, val);
  1256. +
  1257. + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_DDL);
  1258. +
  1259. + /* Always start in trigger frame capture mode (UNICAM_FCM set) */
  1260. + val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM;
  1261. + set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
  1262. + reg_write(cfg, UNICAM_ICTL, val);
  1263. + reg_write(cfg, UNICAM_STA, UNICAM_STA_MASK_ALL);
  1264. + reg_write(cfg, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
  1265. +
  1266. + /* tclk_term_en */
  1267. + reg_write_field(cfg, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
  1268. + /* tclk_settle */
  1269. + reg_write_field(cfg, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
  1270. + /* td_term_en */
  1271. + reg_write_field(cfg, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
  1272. + /* ths_settle */
  1273. + reg_write_field(cfg, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
  1274. + /* trx_enable */
  1275. + reg_write_field(cfg, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
  1276. +
  1277. + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_SOE);
  1278. +
  1279. + /* Packet compare setup - required to avoid missing frame ends */
  1280. + val = 0;
  1281. + set_field(&val, 1, UNICAM_PCE);
  1282. + set_field(&val, 1, UNICAM_GI);
  1283. + set_field(&val, 1, UNICAM_CPH);
  1284. + set_field(&val, 0, UNICAM_PCVC_MASK);
  1285. + set_field(&val, 1, UNICAM_PCDT_MASK);
  1286. + reg_write(cfg, UNICAM_CMP0, val);
  1287. +
  1288. + /* Enable clock lane and set up terminations */
  1289. + val = 0;
  1290. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
  1291. + /* CSI2 */
  1292. + set_field(&val, 1, UNICAM_CLE);
  1293. + set_field(&val, 1, UNICAM_CLLPE);
  1294. + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
  1295. + set_field(&val, 1, UNICAM_CLTRE);
  1296. + set_field(&val, 1, UNICAM_CLHSE);
  1297. + }
  1298. + } else {
  1299. + /* CCP2 */
  1300. + set_field(&val, 1, UNICAM_CLE);
  1301. + set_field(&val, 1, UNICAM_CLHSE);
  1302. + set_field(&val, 1, UNICAM_CLTRE);
  1303. + }
  1304. + reg_write(cfg, UNICAM_CLK, val);
  1305. +
  1306. + /*
  1307. + * Enable required data lanes with appropriate terminations.
  1308. + * The same value needs to be written to UNICAM_DATn registers for
  1309. + * the active lanes, and 0 for inactive ones.
  1310. + */
  1311. + val = 0;
  1312. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
  1313. + /* CSI2 */
  1314. + set_field(&val, 1, UNICAM_DLE);
  1315. + set_field(&val, 1, UNICAM_DLLPE);
  1316. + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
  1317. + set_field(&val, 1, UNICAM_DLTRE);
  1318. + set_field(&val, 1, UNICAM_DLHSE);
  1319. + }
  1320. + } else {
  1321. + /* CCP2 */
  1322. + set_field(&val, 1, UNICAM_DLE);
  1323. + set_field(&val, 1, UNICAM_DLHSE);
  1324. + set_field(&val, 1, UNICAM_DLTRE);
  1325. + }
  1326. + reg_write(cfg, UNICAM_DAT0, val);
  1327. +
  1328. + if (dev->active_data_lanes == 1)
  1329. + val = 0;
  1330. + reg_write(cfg, UNICAM_DAT1, val);
  1331. +
  1332. + if (dev->max_data_lanes > 2) {
  1333. + /*
  1334. + * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
  1335. + * instance supports more than 2 data lanes.
  1336. + */
  1337. + if (dev->active_data_lanes == 2)
  1338. + val = 0;
  1339. + reg_write(cfg, UNICAM_DAT2, val);
  1340. +
  1341. + if (dev->active_data_lanes == 3)
  1342. + val = 0;
  1343. + reg_write(cfg, UNICAM_DAT3, val);
  1344. + }
  1345. +
  1346. + reg_write(&dev->cfg, UNICAM_IBLS, dev->v_fmt.fmt.pix.bytesperline);
  1347. + unicam_wr_dma_addr(dev, addr);
  1348. + unicam_set_packing_config(dev);
  1349. + unicam_cfg_image_id(dev);
  1350. +
  1351. + /* Disabled embedded data */
  1352. + val = 0;
  1353. + set_field(&val, 0, UNICAM_EDL_MASK);
  1354. + reg_write(cfg, UNICAM_DCS, val);
  1355. +
  1356. + val = reg_read(cfg, UNICAM_MISC);
  1357. + set_field(&val, 1, UNICAM_FL0);
  1358. + set_field(&val, 1, UNICAM_FL1);
  1359. + reg_write(cfg, UNICAM_MISC, val);
  1360. +
  1361. + /* Enable peripheral */
  1362. + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPE);
  1363. +
  1364. + /* Load image pointers */
  1365. + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
  1366. +
  1367. + /*
  1368. + * Enable trigger only for the first frame to
  1369. + * sync correctly to the FS from the source.
  1370. + */
  1371. + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_TFC);
  1372. +}
  1373. +
  1374. +static void unicam_disable(struct unicam_device *dev)
  1375. +{
  1376. + struct unicam_cfg *cfg = &dev->cfg;
  1377. +
  1378. + /* Analogue lane control disable */
  1379. + reg_write_field(cfg, UNICAM_ANA, 1, UNICAM_DDL);
  1380. +
  1381. + /* Stop the output engine */
  1382. + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_SOE);
  1383. +
  1384. + /* Disable the data lanes. */
  1385. + reg_write(cfg, UNICAM_DAT0, 0);
  1386. + reg_write(cfg, UNICAM_DAT1, 0);
  1387. +
  1388. + if (dev->max_data_lanes > 2) {
  1389. + reg_write(cfg, UNICAM_DAT2, 0);
  1390. + reg_write(cfg, UNICAM_DAT3, 0);
  1391. + }
  1392. +
  1393. + /* Peripheral reset */
  1394. + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
  1395. + usleep_range(50, 100);
  1396. + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
  1397. +
  1398. + /* Disable peripheral */
  1399. + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
  1400. +
  1401. + /* Disable all lane clocks */
  1402. + clk_write(cfg, 0);
  1403. +}
  1404. +
  1405. +static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
  1406. +{
  1407. + struct unicam_device *dev = vb2_get_drv_priv(vq);
  1408. + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
  1409. + struct unicam_buffer *buf, *tmp;
  1410. + unsigned long addr = 0;
  1411. + unsigned long flags;
  1412. + int ret;
  1413. +
  1414. + spin_lock_irqsave(&dev->dma_queue_lock, flags);
  1415. + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
  1416. + dev->cur_frm = buf;
  1417. + dev->next_frm = buf;
  1418. + list_del(&buf->list);
  1419. + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
  1420. +
  1421. + addr = vb2_dma_contig_plane_dma_addr(&dev->cur_frm->vb.vb2_buf, 0);
  1422. + dev->sequence = 0;
  1423. +
  1424. + ret = unicam_runtime_get(dev);
  1425. + if (ret < 0) {
  1426. + unicam_dbg(3, dev, "unicam_runtime_get failed\n");
  1427. + goto err_release_buffers;
  1428. + }
  1429. +
  1430. + dev->active_data_lanes = dev->max_data_lanes;
  1431. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY &&
  1432. + v4l2_subdev_has_op(dev->sensor, video, g_mbus_config)) {
  1433. + struct v4l2_mbus_config mbus_config;
  1434. +
  1435. + ret = v4l2_subdev_call(dev->sensor, video, g_mbus_config,
  1436. + &mbus_config);
  1437. + if (ret < 0) {
  1438. + unicam_dbg(3, dev, "g_mbus_config failed\n");
  1439. + goto err_pm_put;
  1440. + }
  1441. +
  1442. + dev->active_data_lanes =
  1443. + (mbus_config.flags & V4L2_MBUS_CSI2_LANE_MASK) >>
  1444. + __ffs(V4L2_MBUS_CSI2_LANE_MASK);
  1445. + if (!dev->active_data_lanes)
  1446. + dev->active_data_lanes = dev->max_data_lanes;
  1447. + }
  1448. + if (dev->active_data_lanes > dev->max_data_lanes) {
  1449. + unicam_err(dev, "Device has requested %u data lanes, which is >%u configured in DT\n",
  1450. + dev->active_data_lanes, dev->max_data_lanes);
  1451. + ret = -EINVAL;
  1452. + goto err_pm_put;
  1453. + }
  1454. +
  1455. + unicam_dbg(1, dev, "Running with %u data lanes\n",
  1456. + dev->active_data_lanes);
  1457. +
  1458. + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
  1459. + if (ret) {
  1460. + unicam_err(dev, "failed to set up clock\n");
  1461. + goto err_pm_put;
  1462. + }
  1463. +
  1464. + ret = clk_prepare_enable(dev->clock);
  1465. + if (ret) {
  1466. + unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
  1467. + goto err_pm_put;
  1468. + }
  1469. + dev->streaming = 1;
  1470. +
  1471. + unicam_start_rx(dev, addr);
  1472. +
  1473. + ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
  1474. + if (ret < 0) {
  1475. + unicam_err(dev, "stream on failed in subdev\n");
  1476. + goto err_disable_unicam;
  1477. + }
  1478. +
  1479. + return 0;
  1480. +
  1481. +err_disable_unicam:
  1482. + unicam_disable(dev);
  1483. + clk_disable_unprepare(dev->clock);
  1484. +err_pm_put:
  1485. + unicam_runtime_put(dev);
  1486. +err_release_buffers:
  1487. + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1488. + list_del(&buf->list);
  1489. + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  1490. + }
  1491. + if (dev->cur_frm != dev->next_frm)
  1492. + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
  1493. + VB2_BUF_STATE_QUEUED);
  1494. + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  1495. + dev->next_frm = NULL;
  1496. + dev->cur_frm = NULL;
  1497. +
  1498. + return ret;
  1499. +}
  1500. +
  1501. +static void unicam_stop_streaming(struct vb2_queue *vq)
  1502. +{
  1503. + struct unicam_device *dev = vb2_get_drv_priv(vq);
  1504. + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
  1505. + struct unicam_buffer *buf, *tmp;
  1506. + unsigned long flags;
  1507. +
  1508. + if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
  1509. + unicam_err(dev, "stream off failed in subdev\n");
  1510. +
  1511. + unicam_disable(dev);
  1512. +
  1513. + /* Release all active buffers */
  1514. + spin_lock_irqsave(&dev->dma_queue_lock, flags);
  1515. + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
  1516. + list_del(&buf->list);
  1517. + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1518. + }
  1519. +
  1520. + if (dev->cur_frm == dev->next_frm) {
  1521. + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1522. + } else {
  1523. + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1524. + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
  1525. + VB2_BUF_STATE_ERROR);
  1526. + }
  1527. + dev->cur_frm = NULL;
  1528. + dev->next_frm = NULL;
  1529. + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
  1530. +
  1531. + clk_disable_unprepare(dev->clock);
  1532. + unicam_runtime_put(dev);
  1533. +}
  1534. +
  1535. +static int unicam_enum_input(struct file *file, void *priv,
  1536. + struct v4l2_input *inp)
  1537. +{
  1538. + struct unicam_device *dev = video_drvdata(file);
  1539. +
  1540. + if (inp->index != 0)
  1541. + return -EINVAL;
  1542. +
  1543. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  1544. + if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
  1545. + inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  1546. + inp->std = 0;
  1547. + } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
  1548. + inp->capabilities = V4L2_IN_CAP_STD;
  1549. + if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
  1550. + < 0)
  1551. + inp->std = V4L2_STD_ALL;
  1552. + } else {
  1553. + inp->capabilities = 0;
  1554. + inp->std = 0;
  1555. + }
  1556. + sprintf(inp->name, "Camera 0");
  1557. + return 0;
  1558. +}
  1559. +
  1560. +static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
  1561. +{
  1562. + *i = 0;
  1563. +
  1564. + return 0;
  1565. +}
  1566. +
  1567. +static int unicam_s_input(struct file *file, void *priv, unsigned int i)
  1568. +{
  1569. + /*
  1570. + * FIXME: Ideally we would like to be able to query the source
  1571. + * subdevice for information over the input connectors it supports,
  1572. + * and map that through in to a call to video_ops->s_routing.
  1573. + * There is no infrastructure support for defining that within
  1574. + * devicetree at present. Until that is implemented we can't
  1575. + * map a user physical connector number to s_routing input number.
  1576. + */
  1577. + if (i > 0)
  1578. + return -EINVAL;
  1579. +
  1580. + return 0;
  1581. +}
  1582. +
  1583. +static int unicam_querystd(struct file *file, void *priv,
  1584. + v4l2_std_id *std)
  1585. +{
  1586. + struct unicam_device *dev = video_drvdata(file);
  1587. +
  1588. + return v4l2_subdev_call(dev->sensor, video, querystd, std);
  1589. +}
  1590. +
  1591. +static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
  1592. +{
  1593. + struct unicam_device *dev = video_drvdata(file);
  1594. +
  1595. + return v4l2_subdev_call(dev->sensor, video, g_std, std);
  1596. +}
  1597. +
  1598. +static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
  1599. +{
  1600. + struct unicam_device *dev = video_drvdata(file);
  1601. + int ret;
  1602. + v4l2_std_id current_std;
  1603. +
  1604. + ret = v4l2_subdev_call(dev->sensor, video, g_std, &current_std);
  1605. + if (ret)
  1606. + return ret;
  1607. +
  1608. + if (std == current_std)
  1609. + return 0;
  1610. +
  1611. + if (vb2_is_busy(&dev->buffer_queue))
  1612. + return -EBUSY;
  1613. +
  1614. + ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
  1615. +
  1616. + /* Force recomputation of bytesperline */
  1617. + dev->v_fmt.fmt.pix.bytesperline = 0;
  1618. +
  1619. + unicam_reset_format(dev);
  1620. +
  1621. + return ret;
  1622. +}
  1623. +
  1624. +static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
  1625. +{
  1626. + struct unicam_device *dev = video_drvdata(file);
  1627. +
  1628. + return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
  1629. +}
  1630. +
  1631. +static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
  1632. +{
  1633. + struct unicam_device *dev = video_drvdata(file);
  1634. +
  1635. + return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
  1636. +}
  1637. +
  1638. +static int unicam_enum_framesizes(struct file *file, void *priv,
  1639. + struct v4l2_frmsizeenum *fsize)
  1640. +{
  1641. + struct unicam_device *dev = video_drvdata(file);
  1642. + const struct unicam_fmt *fmt;
  1643. + struct v4l2_subdev_frame_size_enum fse;
  1644. + int ret;
  1645. +
  1646. + /* check for valid format */
  1647. + fmt = find_format_by_pix(dev, fsize->pixel_format);
  1648. + if (!fmt) {
  1649. + unicam_dbg(3, dev, "Invalid pixel code: %x\n",
  1650. + fsize->pixel_format);
  1651. + return -EINVAL;
  1652. + }
  1653. +
  1654. + fse.index = fsize->index;
  1655. + fse.pad = 0;
  1656. + fse.code = fmt->code;
  1657. +
  1658. + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
  1659. + if (ret)
  1660. + return ret;
  1661. +
  1662. + unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
  1663. + __func__, fse.index, fse.code, fse.min_width, fse.max_width,
  1664. + fse.min_height, fse.max_height);
  1665. +
  1666. + fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1667. + fsize->discrete.width = fse.max_width;
  1668. + fsize->discrete.height = fse.max_height;
  1669. +
  1670. + return 0;
  1671. +}
  1672. +
  1673. +static int unicam_enum_frameintervals(struct file *file, void *priv,
  1674. + struct v4l2_frmivalenum *fival)
  1675. +{
  1676. + struct unicam_device *dev = video_drvdata(file);
  1677. + const struct unicam_fmt *fmt;
  1678. + struct v4l2_subdev_frame_interval_enum fie = {
  1679. + .index = fival->index,
  1680. + .width = fival->width,
  1681. + .height = fival->height,
  1682. + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1683. + };
  1684. + int ret;
  1685. +
  1686. + fmt = find_format_by_pix(dev, fival->pixel_format);
  1687. + if (!fmt)
  1688. + return -EINVAL;
  1689. +
  1690. + fie.code = fmt->code;
  1691. + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
  1692. + NULL, &fie);
  1693. + if (ret)
  1694. + return ret;
  1695. +
  1696. + fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1697. + fival->discrete = fie.interval;
  1698. +
  1699. + return 0;
  1700. +}
  1701. +
  1702. +static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1703. +{
  1704. + struct unicam_device *dev = video_drvdata(file);
  1705. +
  1706. + return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
  1707. +}
  1708. +
  1709. +static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1710. +{
  1711. + struct unicam_device *dev = video_drvdata(file);
  1712. +
  1713. + return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
  1714. +}
  1715. +
  1716. +static int unicam_g_dv_timings(struct file *file, void *priv,
  1717. + struct v4l2_dv_timings *timings)
  1718. +{
  1719. + struct unicam_device *dev = video_drvdata(file);
  1720. +
  1721. + return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
  1722. +}
  1723. +
  1724. +static int unicam_s_dv_timings(struct file *file, void *priv,
  1725. + struct v4l2_dv_timings *timings)
  1726. +{
  1727. + struct unicam_device *dev = video_drvdata(file);
  1728. + struct v4l2_dv_timings current_timings;
  1729. + int ret;
  1730. +
  1731. + ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
  1732. + &current_timings);
  1733. +
  1734. + if (v4l2_match_dv_timings(timings, &current_timings, 0, false))
  1735. + return 0;
  1736. +
  1737. + if (vb2_is_busy(&dev->buffer_queue))
  1738. + return -EBUSY;
  1739. +
  1740. + ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
  1741. +
  1742. + /* Force recomputation of bytesperline */
  1743. + dev->v_fmt.fmt.pix.bytesperline = 0;
  1744. +
  1745. + unicam_reset_format(dev);
  1746. +
  1747. + return ret;
  1748. +}
  1749. +
  1750. +static int unicam_query_dv_timings(struct file *file, void *priv,
  1751. + struct v4l2_dv_timings *timings)
  1752. +{
  1753. + struct unicam_device *dev = video_drvdata(file);
  1754. +
  1755. + return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
  1756. +}
  1757. +
  1758. +static int unicam_enum_dv_timings(struct file *file, void *priv,
  1759. + struct v4l2_enum_dv_timings *timings)
  1760. +{
  1761. + struct unicam_device *dev = video_drvdata(file);
  1762. +
  1763. + return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
  1764. +}
  1765. +
  1766. +static int unicam_dv_timings_cap(struct file *file, void *priv,
  1767. + struct v4l2_dv_timings_cap *cap)
  1768. +{
  1769. + struct unicam_device *dev = video_drvdata(file);
  1770. +
  1771. + return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
  1772. +}
  1773. +
  1774. +static int unicam_subscribe_event(struct v4l2_fh *fh,
  1775. + const struct v4l2_event_subscription *sub)
  1776. +{
  1777. + switch (sub->type) {
  1778. + case V4L2_EVENT_SOURCE_CHANGE:
  1779. + return v4l2_event_subscribe(fh, sub, 4, NULL);
  1780. + }
  1781. +
  1782. + return v4l2_ctrl_subscribe_event(fh, sub);
  1783. +}
  1784. +
  1785. +static int unicam_log_status(struct file *file, void *fh)
  1786. +{
  1787. + struct unicam_device *dev = video_drvdata(file);
  1788. + struct unicam_cfg *cfg = &dev->cfg;
  1789. + u32 reg;
  1790. +
  1791. + /* status for sub devices */
  1792. + v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
  1793. +
  1794. + unicam_info(dev, "-----Receiver status-----\n");
  1795. + unicam_info(dev, "V4L2 width/height: %ux%u\n",
  1796. + dev->v_fmt.fmt.pix.width, dev->v_fmt.fmt.pix.height);
  1797. + unicam_info(dev, "Mediabus format: %08x\n", dev->fmt->code);
  1798. + unicam_info(dev, "V4L2 format: %08x\n",
  1799. + dev->v_fmt.fmt.pix.pixelformat);
  1800. + reg = reg_read(&dev->cfg, UNICAM_IPIPE);
  1801. + unicam_info(dev, "Unpacking/packing: %u / %u\n",
  1802. + get_field(reg, UNICAM_PUM_MASK),
  1803. + get_field(reg, UNICAM_PPM_MASK));
  1804. + unicam_info(dev, "----Live data----\n");
  1805. + unicam_info(dev, "Programmed stride: %4u\n",
  1806. + reg_read(cfg, UNICAM_IBLS));
  1807. + unicam_info(dev, "Detected resolution: %ux%u\n",
  1808. + reg_read(cfg, UNICAM_IHSTA),
  1809. + reg_read(cfg, UNICAM_IVSTA));
  1810. + unicam_info(dev, "Write pointer: %08x\n",
  1811. + reg_read(cfg, UNICAM_IBWP));
  1812. +
  1813. + return 0;
  1814. +}
  1815. +
  1816. +static void unicam_notify(struct v4l2_subdev *sd,
  1817. + unsigned int notification, void *arg)
  1818. +{
  1819. + struct unicam_device *dev =
  1820. + container_of(sd->v4l2_dev, struct unicam_device, v4l2_dev);
  1821. +
  1822. + switch (notification) {
  1823. + case V4L2_DEVICE_NOTIFY_EVENT:
  1824. + v4l2_event_queue(&dev->video_dev, arg);
  1825. + break;
  1826. + default:
  1827. + break;
  1828. + }
  1829. +}
  1830. +
  1831. +static const struct vb2_ops unicam_video_qops = {
  1832. + .wait_prepare = vb2_ops_wait_prepare,
  1833. + .wait_finish = vb2_ops_wait_finish,
  1834. + .queue_setup = unicam_queue_setup,
  1835. + .buf_prepare = unicam_buffer_prepare,
  1836. + .buf_queue = unicam_buffer_queue,
  1837. + .start_streaming = unicam_start_streaming,
  1838. + .stop_streaming = unicam_stop_streaming,
  1839. +};
  1840. +
  1841. +/*
  1842. + * unicam_open : This function is based on the v4l2_fh_open helper function.
  1843. + * It has been augmented to handle sensor subdevice power management,
  1844. + */
  1845. +static int unicam_open(struct file *file)
  1846. +{
  1847. + struct unicam_device *dev = video_drvdata(file);
  1848. + int ret;
  1849. +
  1850. + mutex_lock(&dev->lock);
  1851. +
  1852. + ret = v4l2_fh_open(file);
  1853. + if (ret) {
  1854. + unicam_err(dev, "v4l2_fh_open failed\n");
  1855. + goto unlock;
  1856. + }
  1857. +
  1858. + if (!v4l2_fh_is_singular_file(file))
  1859. + goto unlock;
  1860. +
  1861. + ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
  1862. + if (ret < 0 && ret != -ENOIOCTLCMD) {
  1863. + v4l2_fh_release(file);
  1864. + goto unlock;
  1865. + }
  1866. +
  1867. + ret = 0;
  1868. +
  1869. +unlock:
  1870. + mutex_unlock(&dev->lock);
  1871. + return ret;
  1872. +}
  1873. +
  1874. +static int unicam_release(struct file *file)
  1875. +{
  1876. + struct unicam_device *dev = video_drvdata(file);
  1877. + struct v4l2_subdev *sd = dev->sensor;
  1878. + bool fh_singular;
  1879. + int ret;
  1880. +
  1881. + mutex_lock(&dev->lock);
  1882. +
  1883. + fh_singular = v4l2_fh_is_singular_file(file);
  1884. +
  1885. + ret = _vb2_fop_release(file, NULL);
  1886. +
  1887. + if (fh_singular)
  1888. + v4l2_subdev_call(sd, core, s_power, 0);
  1889. +
  1890. + mutex_unlock(&dev->lock);
  1891. +
  1892. + return ret;
  1893. +}
  1894. +
  1895. +/* unicam capture driver file operations */
  1896. +static const struct v4l2_file_operations unicam_fops = {
  1897. + .owner = THIS_MODULE,
  1898. + .open = unicam_open,
  1899. + .release = unicam_release,
  1900. + .read = vb2_fop_read,
  1901. + .poll = vb2_fop_poll,
  1902. + .unlocked_ioctl = video_ioctl2,
  1903. + .mmap = vb2_fop_mmap,
  1904. +};
  1905. +
  1906. +/* unicam capture ioctl operations */
  1907. +static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
  1908. + .vidioc_querycap = unicam_querycap,
  1909. + .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
  1910. + .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
  1911. + .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
  1912. + .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
  1913. +
  1914. + .vidioc_enum_input = unicam_enum_input,
  1915. + .vidioc_g_input = unicam_g_input,
  1916. + .vidioc_s_input = unicam_s_input,
  1917. +
  1918. + .vidioc_querystd = unicam_querystd,
  1919. + .vidioc_s_std = unicam_s_std,
  1920. + .vidioc_g_std = unicam_g_std,
  1921. +
  1922. + .vidioc_g_edid = unicam_g_edid,
  1923. + .vidioc_s_edid = unicam_s_edid,
  1924. +
  1925. + .vidioc_enum_framesizes = unicam_enum_framesizes,
  1926. + .vidioc_enum_frameintervals = unicam_enum_frameintervals,
  1927. +
  1928. + .vidioc_g_parm = unicam_g_parm,
  1929. + .vidioc_s_parm = unicam_s_parm,
  1930. +
  1931. + .vidioc_s_dv_timings = unicam_s_dv_timings,
  1932. + .vidioc_g_dv_timings = unicam_g_dv_timings,
  1933. + .vidioc_query_dv_timings = unicam_query_dv_timings,
  1934. + .vidioc_enum_dv_timings = unicam_enum_dv_timings,
  1935. + .vidioc_dv_timings_cap = unicam_dv_timings_cap,
  1936. +
  1937. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1938. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1939. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1940. + .vidioc_querybuf = vb2_ioctl_querybuf,
  1941. + .vidioc_qbuf = vb2_ioctl_qbuf,
  1942. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1943. + .vidioc_expbuf = vb2_ioctl_expbuf,
  1944. + .vidioc_streamon = vb2_ioctl_streamon,
  1945. + .vidioc_streamoff = vb2_ioctl_streamoff,
  1946. +
  1947. + .vidioc_log_status = unicam_log_status,
  1948. + .vidioc_subscribe_event = unicam_subscribe_event,
  1949. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1950. +};
  1951. +
  1952. +static int
  1953. +unicam_async_bound(struct v4l2_async_notifier *notifier,
  1954. + struct v4l2_subdev *subdev,
  1955. + struct v4l2_async_subdev *asd)
  1956. +{
  1957. + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
  1958. + struct unicam_device, v4l2_dev);
  1959. +
  1960. + if (unicam->sensor) {
  1961. + unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
  1962. + subdev->name);
  1963. + return 0;
  1964. + }
  1965. +
  1966. + unicam->sensor = subdev;
  1967. + unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
  1968. +
  1969. + return 0;
  1970. +}
  1971. +
  1972. +static int unicam_probe_complete(struct unicam_device *unicam)
  1973. +{
  1974. + struct video_device *vdev;
  1975. + struct vb2_queue *q;
  1976. + struct v4l2_mbus_framefmt mbus_fmt = {0};
  1977. + const struct unicam_fmt *fmt;
  1978. + int ret;
  1979. +
  1980. + v4l2_set_subdev_hostdata(unicam->sensor, unicam);
  1981. +
  1982. + unicam->v4l2_dev.notify = unicam_notify;
  1983. +
  1984. + unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
  1985. + if (!unicam->sensor_config)
  1986. + return -ENOMEM;
  1987. +
  1988. + ret = __subdev_get_format(unicam, &mbus_fmt);
  1989. + if (ret) {
  1990. + unicam_err(unicam, "Failed to get_format - ret %d\n", ret);
  1991. + return ret;
  1992. + }
  1993. +
  1994. + fmt = find_format_by_code(mbus_fmt.code);
  1995. + if (!fmt) {
  1996. + /* Find the first format that the sensor and unicam both
  1997. + * support
  1998. + */
  1999. + fmt = get_first_supported_format(unicam);
  2000. +
  2001. + if (!fmt)
  2002. + /* No compatible formats */
  2003. + return -EINVAL;
  2004. +
  2005. + mbus_fmt.code = fmt->code;
  2006. + ret = __subdev_set_format(unicam, &mbus_fmt);
  2007. + if (ret)
  2008. + return -EINVAL;
  2009. + }
  2010. + if (mbus_fmt.field != V4L2_FIELD_NONE) {
  2011. + /* Interlaced not supported - disable it now. */
  2012. + mbus_fmt.field = V4L2_FIELD_NONE;
  2013. + ret = __subdev_set_format(unicam, &mbus_fmt);
  2014. + if (ret)
  2015. + return -EINVAL;
  2016. + }
  2017. +
  2018. + unicam->fmt = fmt;
  2019. + if (fmt->fourcc)
  2020. + unicam->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
  2021. + else
  2022. + unicam->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
  2023. +
  2024. + /* Read current subdev format */
  2025. + unicam_reset_format(unicam);
  2026. +
  2027. + if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
  2028. + v4l2_std_id tvnorms;
  2029. +
  2030. + if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
  2031. + g_tvnorms)))
  2032. + /*
  2033. + * Subdevice should not advertise s_std but not
  2034. + * g_tvnorms
  2035. + */
  2036. + return -EINVAL;
  2037. +
  2038. + ret = v4l2_subdev_call(unicam->sensor, video,
  2039. + g_tvnorms, &tvnorms);
  2040. + if (WARN_ON(ret))
  2041. + return -EINVAL;
  2042. + unicam->video_dev.tvnorms |= tvnorms;
  2043. + }
  2044. +
  2045. + spin_lock_init(&unicam->dma_queue_lock);
  2046. + mutex_init(&unicam->lock);
  2047. +
  2048. + /* Add controls from the subdevice */
  2049. + ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
  2050. + unicam->sensor->ctrl_handler, NULL, true);
  2051. + if (ret < 0)
  2052. + return ret;
  2053. +
  2054. + q = &unicam->buffer_queue;
  2055. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2056. + q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  2057. + q->drv_priv = unicam;
  2058. + q->ops = &unicam_video_qops;
  2059. + q->mem_ops = &vb2_dma_contig_memops;
  2060. + q->buf_struct_size = sizeof(struct unicam_buffer);
  2061. + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  2062. + q->lock = &unicam->lock;
  2063. + q->min_buffers_needed = 2;
  2064. + q->dev = &unicam->pdev->dev;
  2065. +
  2066. + ret = vb2_queue_init(q);
  2067. + if (ret) {
  2068. + unicam_err(unicam, "vb2_queue_init() failed\n");
  2069. + return ret;
  2070. + }
  2071. +
  2072. + INIT_LIST_HEAD(&unicam->dma_queue.active);
  2073. +
  2074. + vdev = &unicam->video_dev;
  2075. + strlcpy(vdev->name, UNICAM_MODULE_NAME, sizeof(vdev->name));
  2076. + vdev->release = video_device_release_empty;
  2077. + vdev->fops = &unicam_fops;
  2078. + vdev->ioctl_ops = &unicam_ioctl_ops;
  2079. + vdev->v4l2_dev = &unicam->v4l2_dev;
  2080. + vdev->vfl_dir = VFL_DIR_RX;
  2081. + vdev->queue = q;
  2082. + vdev->lock = &unicam->lock;
  2083. + vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  2084. + V4L2_CAP_READWRITE;
  2085. +
  2086. + /* If the source has no controls then remove our ctrl handler. */
  2087. + if (list_empty(&unicam->ctrl_handler.ctrls))
  2088. + unicam->v4l2_dev.ctrl_handler = NULL;
  2089. +
  2090. + video_set_drvdata(vdev, unicam);
  2091. + vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
  2092. +
  2093. + if (!v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
  2094. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_STD);
  2095. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_STD);
  2096. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUMSTD);
  2097. + }
  2098. + if (!v4l2_subdev_has_op(unicam->sensor, video, querystd))
  2099. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERYSTD);
  2100. + if (!v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
  2101. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_EDID);
  2102. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_EDID);
  2103. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_DV_TIMINGS_CAP);
  2104. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_DV_TIMINGS);
  2105. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_DV_TIMINGS);
  2106. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_DV_TIMINGS);
  2107. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERY_DV_TIMINGS);
  2108. + }
  2109. + if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
  2110. + v4l2_disable_ioctl(&unicam->video_dev,
  2111. + VIDIOC_ENUM_FRAMEINTERVALS);
  2112. + if (!v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
  2113. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_PARM);
  2114. + if (!v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
  2115. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_PARM);
  2116. +
  2117. + if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
  2118. + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_FRAMESIZES);
  2119. +
  2120. + ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  2121. + if (ret) {
  2122. + unicam_err(unicam, "Unable to register video device.\n");
  2123. + return ret;
  2124. + }
  2125. +
  2126. + ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
  2127. + if (ret) {
  2128. + unicam_err(unicam,
  2129. + "Unable to register subdev nodes.\n");
  2130. + video_unregister_device(&unicam->video_dev);
  2131. + return ret;
  2132. + }
  2133. +
  2134. + ret = media_create_pad_link(&unicam->sensor->entity, 0,
  2135. + &unicam->video_dev.entity, 0,
  2136. + MEDIA_LNK_FL_ENABLED |
  2137. + MEDIA_LNK_FL_IMMUTABLE);
  2138. + if (ret) {
  2139. + unicam_err(unicam, "Unable to create pad links.\n");
  2140. + video_unregister_device(&unicam->video_dev);
  2141. + return ret;
  2142. + }
  2143. +
  2144. + return 0;
  2145. +}
  2146. +
  2147. +static int unicam_async_complete(struct v4l2_async_notifier *notifier)
  2148. +{
  2149. + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
  2150. + struct unicam_device, v4l2_dev);
  2151. +
  2152. + return unicam_probe_complete(unicam);
  2153. +}
  2154. +
  2155. +static const struct v4l2_async_notifier_operations unicam_async_ops = {
  2156. + .bound = unicam_async_bound,
  2157. + .complete = unicam_async_complete,
  2158. +};
  2159. +
  2160. +static int of_unicam_connect_subdevs(struct unicam_device *dev)
  2161. +{
  2162. + struct platform_device *pdev = dev->pdev;
  2163. + struct device_node *parent, *ep_node = NULL, *remote_ep = NULL,
  2164. + *sensor_node = NULL;
  2165. + struct v4l2_fwnode_endpoint *ep;
  2166. + struct v4l2_async_subdev *asd;
  2167. + unsigned int peripheral_data_lanes;
  2168. + int ret = -EINVAL;
  2169. + unsigned int lane;
  2170. +
  2171. + parent = pdev->dev.of_node;
  2172. +
  2173. + asd = &dev->asd;
  2174. + ep = &dev->endpoint;
  2175. +
  2176. + ep_node = of_graph_get_next_endpoint(parent, NULL);
  2177. + if (!ep_node) {
  2178. + unicam_dbg(3, dev, "can't get next endpoint\n");
  2179. + goto cleanup_exit;
  2180. + }
  2181. +
  2182. + unicam_dbg(3, dev, "ep_node is %s\n", ep_node->name);
  2183. +
  2184. + v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), ep);
  2185. +
  2186. + for (lane = 0; lane < ep->bus.mipi_csi2.num_data_lanes; lane++) {
  2187. + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
  2188. + unicam_err(dev, "Local endpoint - data lane reordering not supported\n");
  2189. + goto cleanup_exit;
  2190. + }
  2191. + }
  2192. +
  2193. + peripheral_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
  2194. +
  2195. + sensor_node = of_graph_get_remote_port_parent(ep_node);
  2196. + if (!sensor_node) {
  2197. + unicam_dbg(3, dev, "can't get remote parent\n");
  2198. + goto cleanup_exit;
  2199. + }
  2200. + unicam_dbg(3, dev, "sensor_node is %s\n", sensor_node->name);
  2201. + asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  2202. + asd->match.fwnode = of_fwnode_handle(sensor_node);
  2203. +
  2204. + remote_ep = of_graph_get_remote_endpoint(ep_node);
  2205. + if (!remote_ep) {
  2206. + unicam_dbg(3, dev, "can't get remote-endpoint\n");
  2207. + goto cleanup_exit;
  2208. + }
  2209. + unicam_dbg(3, dev, "remote_ep is %s\n", remote_ep->name);
  2210. + v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), ep);
  2211. + unicam_dbg(3, dev, "parsed remote_ep to endpoint. nr_of_link_frequencies %u, bus_type %u\n",
  2212. + ep->nr_of_link_frequencies, ep->bus_type);
  2213. +
  2214. + switch (ep->bus_type) {
  2215. + case V4L2_MBUS_CSI2_DPHY:
  2216. + if (ep->bus.mipi_csi2.num_data_lanes >
  2217. + peripheral_data_lanes) {
  2218. + unicam_err(dev, "Subdevice %s wants too many data lanes (%u > %u)\n",
  2219. + sensor_node->name,
  2220. + ep->bus.mipi_csi2.num_data_lanes,
  2221. + peripheral_data_lanes);
  2222. + goto cleanup_exit;
  2223. + }
  2224. + for (lane = 0;
  2225. + lane < ep->bus.mipi_csi2.num_data_lanes;
  2226. + lane++) {
  2227. + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
  2228. + unicam_err(dev, "Subdevice %s - incompatible data lane config\n",
  2229. + sensor_node->name);
  2230. + goto cleanup_exit;
  2231. + }
  2232. + }
  2233. + dev->max_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
  2234. + dev->bus_flags = ep->bus.mipi_csi2.flags;
  2235. + break;
  2236. + case V4L2_MBUS_CCP2:
  2237. + if (ep->bus.mipi_csi1.clock_lane != 0 ||
  2238. + ep->bus.mipi_csi1.data_lane != 1) {
  2239. + unicam_err(dev, "Subdevice %s incompatible lane config\n",
  2240. + sensor_node->name);
  2241. + goto cleanup_exit;
  2242. + }
  2243. + dev->max_data_lanes = 1;
  2244. + dev->bus_flags = ep->bus.mipi_csi1.strobe;
  2245. + break;
  2246. + default:
  2247. + /* Unsupported bus type */
  2248. + unicam_err(dev, "sub-device %s is not a CSI2 or CCP2 device %d\n",
  2249. + sensor_node->name, ep->bus_type);
  2250. + goto cleanup_exit;
  2251. + }
  2252. +
  2253. + /* Store bus type - CSI2 or CCP2 */
  2254. + dev->bus_type = ep->bus_type;
  2255. + unicam_dbg(3, dev, "bus_type is %d\n", dev->bus_type);
  2256. +
  2257. + /* Store Virtual Channel number */
  2258. + dev->virtual_channel = ep->base.id;
  2259. +
  2260. + unicam_dbg(3, dev, "v4l2-endpoint: %s\n",
  2261. + dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI2" : "CCP2");
  2262. + unicam_dbg(3, dev, "Virtual Channel=%d\n", dev->virtual_channel);
  2263. + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY)
  2264. + unicam_dbg(3, dev, "flags=0x%08x\n", ep->bus.mipi_csi2.flags);
  2265. + unicam_dbg(3, dev, "num_data_lanes=%d\n", dev->max_data_lanes);
  2266. +
  2267. + unicam_dbg(1, dev, "found sub-device %s\n", sensor_node->name);
  2268. +
  2269. + v4l2_async_notifier_init(&dev->notifier);
  2270. +
  2271. + ret = v4l2_async_notifier_add_subdev(&dev->notifier, asd);
  2272. + if (ret) {
  2273. + unicam_err(dev, "Error adding subdevice - ret %d\n", ret);
  2274. + goto cleanup_exit;
  2275. + }
  2276. +
  2277. + dev->notifier.ops = &unicam_async_ops;
  2278. + ret = v4l2_async_notifier_register(&dev->v4l2_dev,
  2279. + &dev->notifier);
  2280. + if (ret) {
  2281. + unicam_err(dev, "Error registering async notifier - ret %d\n",
  2282. + ret);
  2283. + ret = -EINVAL;
  2284. + }
  2285. +
  2286. +cleanup_exit:
  2287. + if (remote_ep)
  2288. + of_node_put(remote_ep);
  2289. + if (sensor_node)
  2290. + of_node_put(sensor_node);
  2291. + if (ep_node)
  2292. + of_node_put(ep_node);
  2293. +
  2294. + return ret;
  2295. +}
  2296. +
  2297. +static int unicam_probe(struct platform_device *pdev)
  2298. +{
  2299. + struct unicam_cfg *unicam_cfg;
  2300. + struct unicam_device *unicam;
  2301. + struct v4l2_ctrl_handler *hdl;
  2302. + struct resource *res;
  2303. + int ret;
  2304. +
  2305. + unicam = devm_kzalloc(&pdev->dev, sizeof(*unicam), GFP_KERNEL);
  2306. + if (!unicam)
  2307. + return -ENOMEM;
  2308. +
  2309. + unicam->pdev = pdev;
  2310. + unicam_cfg = &unicam->cfg;
  2311. +
  2312. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2313. + unicam_cfg->base = devm_ioremap_resource(&pdev->dev, res);
  2314. + if (IS_ERR(unicam_cfg->base)) {
  2315. + unicam_err(unicam, "Failed to get main io block\n");
  2316. + return PTR_ERR(unicam_cfg->base);
  2317. + }
  2318. +
  2319. + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2320. + unicam_cfg->clk_gate_base = devm_ioremap_resource(&pdev->dev, res);
  2321. + if (IS_ERR(unicam_cfg->clk_gate_base)) {
  2322. + unicam_err(unicam, "Failed to get 2nd io block\n");
  2323. + return PTR_ERR(unicam_cfg->clk_gate_base);
  2324. + }
  2325. +
  2326. + unicam->clock = devm_clk_get(&pdev->dev, "lp");
  2327. + if (IS_ERR(unicam->clock)) {
  2328. + unicam_err(unicam, "Failed to get clock\n");
  2329. + return PTR_ERR(unicam->clock);
  2330. + }
  2331. +
  2332. + ret = platform_get_irq(pdev, 0);
  2333. + if (ret <= 0) {
  2334. + dev_err(&pdev->dev, "No IRQ resource\n");
  2335. + return -ENODEV;
  2336. + }
  2337. +
  2338. + ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
  2339. + "unicam_capture0", unicam);
  2340. + if (ret) {
  2341. + dev_err(&pdev->dev, "Unable to request interrupt\n");
  2342. + return -EINVAL;
  2343. + }
  2344. +
  2345. + unicam->mdev.dev = &pdev->dev;
  2346. + strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
  2347. + sizeof(unicam->mdev.model));
  2348. + strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
  2349. + snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
  2350. + "platform:%s %s",
  2351. + pdev->dev.driver->name, dev_name(&pdev->dev));
  2352. + unicam->mdev.hw_revision = 1;
  2353. +
  2354. + media_entity_pads_init(&unicam->video_dev.entity, 1, &unicam->pad);
  2355. + media_device_init(&unicam->mdev);
  2356. +
  2357. + unicam->v4l2_dev.mdev = &unicam->mdev;
  2358. +
  2359. + ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
  2360. + if (ret) {
  2361. + unicam_err(unicam,
  2362. + "Unable to register v4l2 device.\n");
  2363. + goto media_cleanup;
  2364. + }
  2365. +
  2366. + ret = media_device_register(&unicam->mdev);
  2367. + if (ret < 0) {
  2368. + unicam_err(unicam,
  2369. + "Unable to register media-controller device.\n");
  2370. + goto probe_out_v4l2_unregister;
  2371. + }
  2372. +
  2373. + /* Reserve space for the controls */
  2374. + hdl = &unicam->ctrl_handler;
  2375. + ret = v4l2_ctrl_handler_init(hdl, 16);
  2376. + if (ret < 0)
  2377. + goto media_unregister;
  2378. + unicam->v4l2_dev.ctrl_handler = hdl;
  2379. +
  2380. + /* set the driver data in platform device */
  2381. + platform_set_drvdata(pdev, unicam);
  2382. +
  2383. + ret = of_unicam_connect_subdevs(unicam);
  2384. + if (ret) {
  2385. + dev_err(&pdev->dev, "Failed to connect subdevs\n");
  2386. + goto free_hdl;
  2387. + }
  2388. +
  2389. + /* Enable the block power domain */
  2390. + pm_runtime_enable(&pdev->dev);
  2391. +
  2392. + return 0;
  2393. +
  2394. +free_hdl:
  2395. + v4l2_ctrl_handler_free(hdl);
  2396. +media_unregister:
  2397. + media_device_unregister(&unicam->mdev);
  2398. +probe_out_v4l2_unregister:
  2399. + v4l2_device_unregister(&unicam->v4l2_dev);
  2400. +media_cleanup:
  2401. + media_device_cleanup(&unicam->mdev);
  2402. +
  2403. + return ret;
  2404. +}
  2405. +
  2406. +static int unicam_remove(struct platform_device *pdev)
  2407. +{
  2408. + struct unicam_device *unicam = platform_get_drvdata(pdev);
  2409. +
  2410. + unicam_dbg(2, unicam, "%s\n", __func__);
  2411. +
  2412. + pm_runtime_disable(&pdev->dev);
  2413. +
  2414. + v4l2_async_notifier_unregister(&unicam->notifier);
  2415. + v4l2_ctrl_handler_free(&unicam->ctrl_handler);
  2416. + v4l2_device_unregister(&unicam->v4l2_dev);
  2417. + video_unregister_device(&unicam->video_dev);
  2418. + if (unicam->sensor_config)
  2419. + v4l2_subdev_free_pad_config(unicam->sensor_config);
  2420. + media_device_unregister(&unicam->mdev);
  2421. + media_device_cleanup(&unicam->mdev);
  2422. +
  2423. + return 0;
  2424. +}
  2425. +
  2426. +static const struct of_device_id unicam_of_match[] = {
  2427. + { .compatible = "brcm,bcm2835-unicam", },
  2428. + { /* sentinel */ },
  2429. +};
  2430. +MODULE_DEVICE_TABLE(of, unicam_of_match);
  2431. +
  2432. +static struct platform_driver unicam_driver = {
  2433. + .probe = unicam_probe,
  2434. + .remove = unicam_remove,
  2435. + .driver = {
  2436. + .name = UNICAM_MODULE_NAME,
  2437. + .of_match_table = of_match_ptr(unicam_of_match),
  2438. + },
  2439. +};
  2440. +
  2441. +module_platform_driver(unicam_driver);
  2442. +
  2443. +MODULE_AUTHOR("Dave Stevenson <[email protected]>");
  2444. +MODULE_DESCRIPTION("BCM2835 Unicam driver");
  2445. +MODULE_LICENSE("GPL");
  2446. +MODULE_VERSION(UNICAM_VERSION);
  2447. --- /dev/null
  2448. +++ b/drivers/media/platform/bcm2835/vc4-regs-unicam.h
  2449. @@ -0,0 +1,253 @@
  2450. +/* SPDX-License-Identifier: GPL-2.0-only */
  2451. +
  2452. +/*
  2453. + * Copyright (C) 2017-2020 Raspberry Pi Trading.
  2454. + * Dave Stevenson <[email protected]>
  2455. + */
  2456. +
  2457. +#ifndef VC4_REGS_UNICAM_H
  2458. +#define VC4_REGS_UNICAM_H
  2459. +
  2460. +/*
  2461. + * The following values are taken from files found within the code drop
  2462. + * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
  2463. + * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
  2464. + * They have been modified to be only the register offset.
  2465. + */
  2466. +#define UNICAM_CTRL 0x000
  2467. +#define UNICAM_STA 0x004
  2468. +#define UNICAM_ANA 0x008
  2469. +#define UNICAM_PRI 0x00c
  2470. +#define UNICAM_CLK 0x010
  2471. +#define UNICAM_CLT 0x014
  2472. +#define UNICAM_DAT0 0x018
  2473. +#define UNICAM_DAT1 0x01c
  2474. +#define UNICAM_DAT2 0x020
  2475. +#define UNICAM_DAT3 0x024
  2476. +#define UNICAM_DLT 0x028
  2477. +#define UNICAM_CMP0 0x02c
  2478. +#define UNICAM_CMP1 0x030
  2479. +#define UNICAM_CAP0 0x034
  2480. +#define UNICAM_CAP1 0x038
  2481. +#define UNICAM_ICTL 0x100
  2482. +#define UNICAM_ISTA 0x104
  2483. +#define UNICAM_IDI0 0x108
  2484. +#define UNICAM_IPIPE 0x10c
  2485. +#define UNICAM_IBSA0 0x110
  2486. +#define UNICAM_IBEA0 0x114
  2487. +#define UNICAM_IBLS 0x118
  2488. +#define UNICAM_IBWP 0x11c
  2489. +#define UNICAM_IHWIN 0x120
  2490. +#define UNICAM_IHSTA 0x124
  2491. +#define UNICAM_IVWIN 0x128
  2492. +#define UNICAM_IVSTA 0x12c
  2493. +#define UNICAM_ICC 0x130
  2494. +#define UNICAM_ICS 0x134
  2495. +#define UNICAM_IDC 0x138
  2496. +#define UNICAM_IDPO 0x13c
  2497. +#define UNICAM_IDCA 0x140
  2498. +#define UNICAM_IDCD 0x144
  2499. +#define UNICAM_IDS 0x148
  2500. +#define UNICAM_DCS 0x200
  2501. +#define UNICAM_DBSA0 0x204
  2502. +#define UNICAM_DBEA0 0x208
  2503. +#define UNICAM_DBWP 0x20c
  2504. +#define UNICAM_DBCTL 0x300
  2505. +#define UNICAM_IBSA1 0x304
  2506. +#define UNICAM_IBEA1 0x308
  2507. +#define UNICAM_IDI1 0x30c
  2508. +#define UNICAM_DBSA1 0x310
  2509. +#define UNICAM_DBEA1 0x314
  2510. +#define UNICAM_MISC 0x400
  2511. +
  2512. +/*
  2513. + * The following bitmasks are from the kernel released by Broadcom
  2514. + * for Android - https://android.googlesource.com/kernel/bcm/
  2515. + * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
  2516. + * Unicam block as BCM2835, as defined in eg
  2517. + * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
  2518. + * Values reworked to use the kernel BIT and GENMASK macros.
  2519. + *
  2520. + * Some of the bit mnenomics have been amended to match the datasheet.
  2521. + */
  2522. +/* UNICAM_CTRL Register */
  2523. +#define UNICAM_CPE BIT(0)
  2524. +#define UNICAM_MEM BIT(1)
  2525. +#define UNICAM_CPR BIT(2)
  2526. +#define UNICAM_CPM_MASK GENMASK(3, 3)
  2527. +#define UNICAM_CPM_CSI2 0
  2528. +#define UNICAM_CPM_CCP2 1
  2529. +#define UNICAM_SOE BIT(4)
  2530. +#define UNICAM_DCM_MASK GENMASK(5, 5)
  2531. +#define UNICAM_DCM_STROBE 0
  2532. +#define UNICAM_DCM_DATA 1
  2533. +#define UNICAM_SLS BIT(6)
  2534. +#define UNICAM_PFT_MASK GENMASK(11, 8)
  2535. +#define UNICAM_OET_MASK GENMASK(20, 12)
  2536. +
  2537. +/* UNICAM_STA Register */
  2538. +#define UNICAM_SYN BIT(0)
  2539. +#define UNICAM_CS BIT(1)
  2540. +#define UNICAM_SBE BIT(2)
  2541. +#define UNICAM_PBE BIT(3)
  2542. +#define UNICAM_HOE BIT(4)
  2543. +#define UNICAM_PLE BIT(5)
  2544. +#define UNICAM_SSC BIT(6)
  2545. +#define UNICAM_CRCE BIT(7)
  2546. +#define UNICAM_OES BIT(8)
  2547. +#define UNICAM_IFO BIT(9)
  2548. +#define UNICAM_OFO BIT(10)
  2549. +#define UNICAM_BFO BIT(11)
  2550. +#define UNICAM_DL BIT(12)
  2551. +#define UNICAM_PS BIT(13)
  2552. +#define UNICAM_IS BIT(14)
  2553. +#define UNICAM_PI0 BIT(15)
  2554. +#define UNICAM_PI1 BIT(16)
  2555. +#define UNICAM_FSI_S BIT(17)
  2556. +#define UNICAM_FEI_S BIT(18)
  2557. +#define UNICAM_LCI_S BIT(19)
  2558. +#define UNICAM_BUF0_RDY BIT(20)
  2559. +#define UNICAM_BUF0_NO BIT(21)
  2560. +#define UNICAM_BUF1_RDY BIT(22)
  2561. +#define UNICAM_BUF1_NO BIT(23)
  2562. +#define UNICAM_DI BIT(24)
  2563. +
  2564. +#define UNICAM_STA_MASK_ALL \
  2565. + (UNICAM_DL + \
  2566. + UNICAM_SBE + \
  2567. + UNICAM_PBE + \
  2568. + UNICAM_HOE + \
  2569. + UNICAM_PLE + \
  2570. + UNICAM_SSC + \
  2571. + UNICAM_CRCE + \
  2572. + UNICAM_IFO + \
  2573. + UNICAM_OFO + \
  2574. + UNICAM_PS + \
  2575. + UNICAM_PI0 + \
  2576. + UNICAM_PI1)
  2577. +
  2578. +/* UNICAM_ANA Register */
  2579. +#define UNICAM_APD BIT(0)
  2580. +#define UNICAM_BPD BIT(1)
  2581. +#define UNICAM_AR BIT(2)
  2582. +#define UNICAM_DDL BIT(3)
  2583. +#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
  2584. +#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
  2585. +
  2586. +/* UNICAM_PRI Register */
  2587. +#define UNICAM_PE BIT(0)
  2588. +#define UNICAM_PT_MASK GENMASK(2, 1)
  2589. +#define UNICAM_NP_MASK GENMASK(7, 4)
  2590. +#define UNICAM_PP_MASK GENMASK(11, 8)
  2591. +#define UNICAM_BS_MASK GENMASK(15, 12)
  2592. +#define UNICAM_BL_MASK GENMASK(17, 16)
  2593. +
  2594. +/* UNICAM_CLK Register */
  2595. +#define UNICAM_CLE BIT(0)
  2596. +#define UNICAM_CLPD BIT(1)
  2597. +#define UNICAM_CLLPE BIT(2)
  2598. +#define UNICAM_CLHSE BIT(3)
  2599. +#define UNICAM_CLTRE BIT(4)
  2600. +#define UNICAM_CLAC_MASK GENMASK(8, 5)
  2601. +#define UNICAM_CLSTE BIT(29)
  2602. +
  2603. +/* UNICAM_CLT Register */
  2604. +#define UNICAM_CLT1_MASK GENMASK(7, 0)
  2605. +#define UNICAM_CLT2_MASK GENMASK(15, 8)
  2606. +
  2607. +/* UNICAM_DATn Registers */
  2608. +#define UNICAM_DLE BIT(0)
  2609. +#define UNICAM_DLPD BIT(1)
  2610. +#define UNICAM_DLLPE BIT(2)
  2611. +#define UNICAM_DLHSE BIT(3)
  2612. +#define UNICAM_DLTRE BIT(4)
  2613. +#define UNICAM_DLSM BIT(5)
  2614. +#define UNICAM_DLFO BIT(28)
  2615. +#define UNICAM_DLSTE BIT(29)
  2616. +
  2617. +#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
  2618. +
  2619. +/* UNICAM_DLT Register */
  2620. +#define UNICAM_DLT1_MASK GENMASK(7, 0)
  2621. +#define UNICAM_DLT2_MASK GENMASK(15, 8)
  2622. +#define UNICAM_DLT3_MASK GENMASK(23, 16)
  2623. +
  2624. +/* UNICAM_ICTL Register */
  2625. +#define UNICAM_FSIE BIT(0)
  2626. +#define UNICAM_FEIE BIT(1)
  2627. +#define UNICAM_IBOB BIT(2)
  2628. +#define UNICAM_FCM BIT(3)
  2629. +#define UNICAM_TFC BIT(4)
  2630. +#define UNICAM_LIP_MASK GENMASK(6, 5)
  2631. +#define UNICAM_LCIE_MASK GENMASK(28, 16)
  2632. +
  2633. +/* UNICAM_IDI0/1 Register */
  2634. +#define UNICAM_ID0_MASK GENMASK(7, 0)
  2635. +#define UNICAM_ID1_MASK GENMASK(15, 8)
  2636. +#define UNICAM_ID2_MASK GENMASK(23, 16)
  2637. +#define UNICAM_ID3_MASK GENMASK(31, 24)
  2638. +
  2639. +/* UNICAM_ISTA Register */
  2640. +#define UNICAM_FSI BIT(0)
  2641. +#define UNICAM_FEI BIT(1)
  2642. +#define UNICAM_LCI BIT(2)
  2643. +
  2644. +#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
  2645. +
  2646. +/* UNICAM_IPIPE Register */
  2647. +#define UNICAM_PUM_MASK GENMASK(2, 0)
  2648. + /* Unpacking modes */
  2649. + #define UNICAM_PUM_NONE 0
  2650. + #define UNICAM_PUM_UNPACK6 1
  2651. + #define UNICAM_PUM_UNPACK7 2
  2652. + #define UNICAM_PUM_UNPACK8 3
  2653. + #define UNICAM_PUM_UNPACK10 4
  2654. + #define UNICAM_PUM_UNPACK12 5
  2655. + #define UNICAM_PUM_UNPACK14 6
  2656. + #define UNICAM_PUM_UNPACK16 7
  2657. +#define UNICAM_DDM_MASK GENMASK(6, 3)
  2658. +#define UNICAM_PPM_MASK GENMASK(9, 7)
  2659. + /* Packing modes */
  2660. + #define UNICAM_PPM_NONE 0
  2661. + #define UNICAM_PPM_PACK8 1
  2662. + #define UNICAM_PPM_PACK10 2
  2663. + #define UNICAM_PPM_PACK12 3
  2664. + #define UNICAM_PPM_PACK14 4
  2665. + #define UNICAM_PPM_PACK16 5
  2666. +#define UNICAM_DEM_MASK GENMASK(11, 10)
  2667. +#define UNICAM_DEBL_MASK GENMASK(14, 12)
  2668. +#define UNICAM_ICM_MASK GENMASK(16, 15)
  2669. +#define UNICAM_IDM_MASK GENMASK(17, 17)
  2670. +
  2671. +/* UNICAM_ICC Register */
  2672. +#define UNICAM_ICFL_MASK GENMASK(4, 0)
  2673. +#define UNICAM_ICFH_MASK GENMASK(9, 5)
  2674. +#define UNICAM_ICST_MASK GENMASK(12, 10)
  2675. +#define UNICAM_ICLT_MASK GENMASK(15, 13)
  2676. +#define UNICAM_ICLL_MASK GENMASK(31, 16)
  2677. +
  2678. +/* UNICAM_DCS Register */
  2679. +#define UNICAM_DIE BIT(0)
  2680. +#define UNICAM_DIM BIT(1)
  2681. +#define UNICAM_DBOB BIT(3)
  2682. +#define UNICAM_FDE BIT(4)
  2683. +#define UNICAM_LDP BIT(5)
  2684. +#define UNICAM_EDL_MASK GENMASK(15, 8)
  2685. +
  2686. +/* UNICAM_DBCTL Register */
  2687. +#define UNICAM_DBEN BIT(0)
  2688. +#define UNICAM_BUF0_IE BIT(1)
  2689. +#define UNICAM_BUF1_IE BIT(2)
  2690. +
  2691. +/* UNICAM_CMP[0,1] register */
  2692. +#define UNICAM_PCE BIT(31)
  2693. +#define UNICAM_GI BIT(9)
  2694. +#define UNICAM_CPH BIT(8)
  2695. +#define UNICAM_PCVC_MASK GENMASK(7, 6)
  2696. +#define UNICAM_PCDT_MASK GENMASK(5, 0)
  2697. +
  2698. +/* UNICAM_MISC register */
  2699. +#define UNICAM_FL0 BIT(6)
  2700. +#define UNICAM_FL1 BIT(9)
  2701. +
  2702. +#endif