950-0195-media-i2c-imx477-Support-for-the-Sony-IMX477-sensor.patch 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  1. From 227eddefe9cb07dd1d160f0015b0d17d66cd3cf8 Mon Sep 17 00:00:00 2001
  2. From: Naushir Patuck <[email protected]>
  3. Date: Thu, 7 May 2020 15:50:54 +0100
  4. Subject: [PATCH] media: i2c: imx477: Support for the Sony IMX477
  5. sensor
  6. dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
  7. Add YAML device tree binding for IMX477 CMOS image sensor.
  8. Signed-off-by: Naushir Patuck <[email protected]>
  9. media: i2c: Add driver for Sony IMX477 sensor
  10. Adds a driver for the 12MPix Sony IMX477 CSI2 sensor.
  11. Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
  12. currently only supports 2 lanes.
  13. The following Bayer modes are currently available:
  14. 4056x3040 12-bit @ 10fps
  15. 2028x1520 12-bit (binned) @ 40fps
  16. 2028x1050 12-bit (cropped/binned) @ 50fps
  17. 1012x760 10-bit (scaled) @ 120 fps
  18. Signed-off-by: Naushir Patuck <[email protected]>
  19. media: i2c: imx477: Add support for adaptive frame control
  20. Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
  21. automatically adjust the sensor frame length based on exposure time,
  22. allowing variable frame rates and longer exposures.
  23. Signed-off-by: Naushir Patuck <[email protected]>
  24. media: i2c: imx477: Return correct result on sensor id verification
  25. The test should return -EIO if the register read id does not match
  26. the expected sensor id.
  27. Signed-off-by: Naushir Patuck <[email protected]>
  28. media: i2c: imx477: Parse and register properties
  29. Parse device properties and register controls for them using the V4L2
  30. fwnode properties helpers.
  31. Signed-off-by: Laurent Pinchart <[email protected]>
  32. media: i2c: imx477: Selection compliance fixes
  33. To comply with the intended usage of the V4L2 selection target when
  34. used to retrieve a sensor image properties, adjust the rectangles
  35. returned by the imx477 driver.
  36. The top/left crop coordinates of the TGT_CROP rectangle were set to
  37. (0, 0) instead of (8, 16) which is the offset from the larger physical
  38. pixel array rectangle. This was also a mismatch with the default values
  39. crop rectangle value, so this is corrected. Found with v4l2-compliance.
  40. While at it, add V4L2_SEL_TGT_CROP_BOUNDS support: CROP_DEFAULT and
  41. CROP_BOUNDS have the same size as the non-active pixels are not readable
  42. using the selection API. Found with v4l2-compliance.
  43. This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
  44. the imx219 sensor.
  45. Signed-off-by: Naushir Patuck <[email protected]>
  46. media: i2c: imx477: Remove auto frame length adjusting
  47. The V4L2_CID_EXPOSURE_AUTO_PRIORITY was used to let the sensor control
  48. frame length (effectively framerate) based on the requested exposure
  49. time requested. Remove this feature as it is never used, and goes
  50. against how V4L2 likes to handle exposure and vblank controls.
  51. Signed-off-by: Naushir Patuck <[email protected]>
  52. media: i2c: imx477: Add very long exposure control to the driver
  53. Add support for very long exposures by using the exposure multiplier
  54. register. Userland does not need to pass any additional controls to
  55. enable long exposures, it simply requests a larger vblank to extend the
  56. exposure control range appropriately.
  57. Currently, since hblank is fixed, a maximum of approximately 124 seconds
  58. of exposure time can be used. In a future change, hblank could also be
  59. controlled in userland to give over 200 seconds of exposure time.
  60. Signed-off-by: Naushir Patuck <[email protected]>
  61. media: i2c: imx477: Fix crop height for 2028x1080 mode
  62. The crop height for this mode was set at 2600 lines, it should be 2160
  63. lines instead.
  64. Signed-off-by: Naushir Patuck <[email protected]>
  65. media: i2c: imx477: Replace existing 1012x760 mode
  66. The existing 1012x760 120 fps mode has significant IQ problem using
  67. the internal sensor scaler. Replace this mode with a 1332x990 120 fps
  68. mode instead. This new mode has a smaller field of view, but does not
  69. suffer from the bad IQ of the original mode.
  70. Signed-off-by: Naushir Patuck <[email protected]>
  71. media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
  72. The only field in this struct that is used is the format code, so
  73. replace the struct with this single field.
  74. Save the format code in imx477_set_pad_format() when setting up a new
  75. mode so that imx477_get_pad_format() performs the right lookup.
  76. Otherwise, this caused a bug where the mode lookup occurred on the
  77. 12-bit table rather than the 10-bit table.
  78. Signed-off-by: Naushir Patuck <[email protected]>
  79. media: i2c: imx477: Remove unused function parameter
  80. The struct imx477 *ctrl parameter is not used in the function
  81. imx477_adjust_exposure_range(), so remove it.
  82. Signed-off-by: Naushir Patuck <[email protected]>
  83. media: i2c: imx477: Fix for long exposure limit calculations
  84. Do not scale IMX477_EXPOSURE_OFFSET with the long exposure factor during
  85. the limit calculations. This allows larger exposure times, and does seem to be
  86. what the sensor is doing internally.
  87. Signed-off-by: Naushir Patuck <[email protected]>
  88. media: i2c: imx477: Extend driver to support imx378 sensor
  89. The imx378 sensor is almost identical to the imx477 and can be
  90. supported as a "compatible" sensor with just a few extra register
  91. writes.
  92. Signed-off-by: David Plowman <[email protected]>
  93. media: i2c: imx477: Fix framerates for 1332x990 mode
  94. The imx477 driver's line length for this mode had not been updated to
  95. the value supplied to us by the sensor manufacturer. With this
  96. correction the sensor delivers the framerates that are expected.
  97. Signed-off-by: David Plowman <[email protected]>
  98. media: i2c: imx477: Allow control of on-sensor DPC
  99. A module parameter "dpc_enable" is added to allow the control of the
  100. sensor's on-board DPC (Defective Pixel Correction) function.
  101. This is a global setting to be configured before using the sensor;
  102. there is no intention that this would ever be changed on-the-fly.
  103. Signed-off-by: David Plowman <[email protected]>
  104. media: i2c: imx477: Sensor should report RAW color space
  105. Tested on Raspberry Pi running libcamera.
  106. Signed-off-by: David Plowman <[email protected]>
  107. media: i2c: imx477: Add vsync trigger_mode parameter
  108. trigger_mode == 0 (default) => no effect / no registers written
  109. trigger_mode == 1 => source
  110. trigger_mode == 2 => sink
  111. This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
  112. Signed-off-by: Jonas Jacob <[email protected]>
  113. media: i2c: Update imx477 Kconfig entry
  114. Bring the IMX477 Kconfig declaration in line with upstream entries.
  115. Signed-off-by: Phil Elwell <[email protected]>
  116. media: i2c: imx477: Correct minimum exposure lines
  117. The minimum number of exposure lines value (IMX477_EXPOSURE_MIN) was
  118. previously 20 but this is not correct. The datasheet is not completely
  119. explicit, however the new value of 4 has been tested with all the
  120. sensor modes supported by this driver, and matches the lowest exposure
  121. value of 114us that could be achieved wtih Raspberry Pi's legacy
  122. firmware driver.
  123. Signed-off-by: David Plowman <[email protected]>
  124. media: i2c: imx477: Allow dynamic horizontal blanking control
  125. Currently, the V4L2_CID_HBLANK control is marked as read-only. Remove this
  126. restriction and allow userland to modify the control if needed.
  127. Set the maximum limit of the line length to 0xfff0.
  128. Signed-off-by: Naushir Patuck <[email protected]>
  129. media: i2c: imx477: Reset hblank on mode switch
  130. Reset the hblank control to the minimum value on every mode switch. This is to
  131. account for userland instances that do not yet control hblank, otherwise it
  132. gets set to a non-optimal value.
  133. Signed-off-by: Naushir Patuck <[email protected]>
  134. media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
  135. On a mode change, only call imx477_set_framing_limits() to adjust the hblank
  136. and vblank limits if the new mode is different from the existing mode. This
  137. preserves any manual control values the user might have set.
  138. Signed-off-by: Naushir Patuck <[email protected]>
  139. ---
  140. .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
  141. MAINTAINERS | 8 +
  142. drivers/media/i2c/Kconfig | 11 +
  143. drivers/media/i2c/Makefile | 1 +
  144. drivers/media/i2c/imx477.c | 2308 +++++++++++++++++
  145. 5 files changed, 2441 insertions(+)
  146. create mode 100644 Documentation/devicetree/bindings/media/i2c/imx477.yaml
  147. create mode 100644 drivers/media/i2c/imx477.c
  148. --- /dev/null
  149. +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
  150. @@ -0,0 +1,113 @@
  151. +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  152. +%YAML 1.2
  153. +---
  154. +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
  155. +$schema: http://devicetree.org/meta-schemas/core.yaml#
  156. +
  157. +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
  158. +
  159. +maintainers:
  160. + - Naushir Patuck <[email protected]>
  161. +
  162. +description: |-
  163. + The Sony IMX477 is a 1/2.3-inch CMOS active pixel digital image sensor
  164. + with an active array size of 4056H x 3040V. It is programmable through
  165. + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
  166. + Image data is sent through MIPI CSI-2, which is configured as either 2 or
  167. + 4 data lanes.
  168. +
  169. +properties:
  170. + compatible:
  171. + const: sony,imx477
  172. +
  173. + reg:
  174. + description: I2C device address
  175. + maxItems: 1
  176. +
  177. + clocks:
  178. + maxItems: 1
  179. +
  180. + VDIG-supply:
  181. + description:
  182. + Digital I/O voltage supply, 1.05 volts
  183. +
  184. + VANA-supply:
  185. + description:
  186. + Analog voltage supply, 2.8 volts
  187. +
  188. + VDDL-supply:
  189. + description:
  190. + Digital core voltage supply, 1.8 volts
  191. +
  192. + reset-gpios:
  193. + description: |-
  194. + Reference to the GPIO connected to the xclr pin, if any.
  195. + Must be released (set high) after all all supplies and INCK are applied.
  196. +
  197. + # See ../video-interfaces.txt for more details
  198. + port:
  199. + type: object
  200. + properties:
  201. + endpoint:
  202. + type: object
  203. + properties:
  204. + data-lanes:
  205. + description: |-
  206. + The sensor supports either two-lane, or four-lane operation.
  207. + For two-lane operation the property must be set to <1 2>.
  208. + items:
  209. + - const: 1
  210. + - const: 2
  211. +
  212. + clock-noncontinuous:
  213. + type: boolean
  214. + description: |-
  215. + MIPI CSI-2 clock is non-continuous if this property is present,
  216. + otherwise it's continuous.
  217. +
  218. + link-frequencies:
  219. + allOf:
  220. + - $ref: /schemas/types.yaml#/definitions/uint64-array
  221. + description:
  222. + Allowed data bus frequencies.
  223. +
  224. + required:
  225. + - link-frequencies
  226. +
  227. +required:
  228. + - compatible
  229. + - reg
  230. + - clocks
  231. + - VANA-supply
  232. + - VDIG-supply
  233. + - VDDL-supply
  234. + - port
  235. +
  236. +additionalProperties: false
  237. +
  238. +examples:
  239. + - |
  240. + i2c0 {
  241. + #address-cells = <1>;
  242. + #size-cells = <0>;
  243. +
  244. + imx477: sensor@10 {
  245. + compatible = "sony,imx477";
  246. + reg = <0x1a>;
  247. + clocks = <&imx477_clk>;
  248. + VANA-supply = <&imx477_vana>; /* 2.8v */
  249. + VDIG-supply = <&imx477_vdig>; /* 1.05v */
  250. + VDDL-supply = <&imx477_vddl>; /* 1.8v */
  251. +
  252. + port {
  253. + imx477_0: endpoint {
  254. + remote-endpoint = <&csi1_ep>;
  255. + data-lanes = <1 2>;
  256. + clock-noncontinuous;
  257. + link-frequencies = /bits/ 64 <450000000>;
  258. + };
  259. + };
  260. + };
  261. + };
  262. +
  263. +...
  264. --- a/MAINTAINERS
  265. +++ b/MAINTAINERS
  266. @@ -19279,6 +19279,14 @@ T: git git://linuxtv.org/media_tree.git
  267. F: Documentation/devicetree/bindings/media/i2c/sony,imx412.yaml
  268. F: drivers/media/i2c/imx412.c
  269. +SONY IMX477 SENSOR DRIVER
  270. +M: Raspberry Pi Kernel Maintenance <[email protected]>
  271. +L: [email protected]
  272. +S: Maintained
  273. +T: git git://linuxtv.org/media_tree.git
  274. +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
  275. +F: drivers/media/i2c/imx477.c
  276. +
  277. SONY MEMORYSTICK SUBSYSTEM
  278. M: Maxim Levitsky <[email protected]>
  279. M: Alex Dubov <[email protected]>
  280. --- a/drivers/media/i2c/Kconfig
  281. +++ b/drivers/media/i2c/Kconfig
  282. @@ -1030,6 +1030,17 @@ config VIDEO_UDA1342
  283. To compile this driver as a module, choose M here: the
  284. module will be called uda1342.
  285. +config VIDEO_IMX477
  286. + tristate "Sony IMX477 sensor support"
  287. + depends on I2C && VIDEO_DEV
  288. + select VIDEO_V4L2_SUBDEV_API
  289. + help
  290. + This is a Video4Linux2 sensor driver for the Sony
  291. + IMX477 camera. Also supports the Sony IMX378.
  292. +
  293. + To compile this driver as a module, choose M here: the
  294. + module will be called imx477.
  295. +
  296. config VIDEO_VP27SMPX
  297. tristate "Panasonic VP27's internal MPX"
  298. depends on VIDEO_DEV && I2C
  299. --- a/drivers/media/i2c/Makefile
  300. +++ b/drivers/media/i2c/Makefile
  301. @@ -48,6 +48,7 @@ obj-$(CONFIG_VIDEO_IMX334) += imx334.o
  302. obj-$(CONFIG_VIDEO_IMX335) += imx335.o
  303. obj-$(CONFIG_VIDEO_IMX355) += imx355.o
  304. obj-$(CONFIG_VIDEO_IMX412) += imx412.o
  305. +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
  306. obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
  307. obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
  308. obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
  309. --- /dev/null
  310. +++ b/drivers/media/i2c/imx477.c
  311. @@ -0,0 +1,2308 @@
  312. +// SPDX-License-Identifier: GPL-2.0
  313. +/*
  314. + * A V4L2 driver for Sony IMX477 cameras.
  315. + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
  316. + *
  317. + * Based on Sony imx219 camera driver
  318. + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
  319. + */
  320. +#include <asm/unaligned.h>
  321. +#include <linux/clk.h>
  322. +#include <linux/delay.h>
  323. +#include <linux/gpio/consumer.h>
  324. +#include <linux/i2c.h>
  325. +#include <linux/module.h>
  326. +#include <linux/of_device.h>
  327. +#include <linux/pm_runtime.h>
  328. +#include <linux/regulator/consumer.h>
  329. +#include <media/v4l2-ctrls.h>
  330. +#include <media/v4l2-device.h>
  331. +#include <media/v4l2-event.h>
  332. +#include <media/v4l2-fwnode.h>
  333. +#include <media/v4l2-mediabus.h>
  334. +
  335. +static int dpc_enable = 1;
  336. +module_param(dpc_enable, int, 0644);
  337. +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
  338. +
  339. +static int trigger_mode;
  340. +module_param(trigger_mode, int, 0644);
  341. +MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
  342. +
  343. +#define IMX477_REG_VALUE_08BIT 1
  344. +#define IMX477_REG_VALUE_16BIT 2
  345. +
  346. +/* Chip ID */
  347. +#define IMX477_REG_CHIP_ID 0x0016
  348. +#define IMX477_CHIP_ID 0x0477
  349. +#define IMX378_CHIP_ID 0x0378
  350. +
  351. +#define IMX477_REG_MODE_SELECT 0x0100
  352. +#define IMX477_MODE_STANDBY 0x00
  353. +#define IMX477_MODE_STREAMING 0x01
  354. +
  355. +#define IMX477_REG_ORIENTATION 0x101
  356. +
  357. +#define IMX477_XCLK_FREQ 24000000
  358. +
  359. +#define IMX477_DEFAULT_LINK_FREQ 450000000
  360. +
  361. +/* Pixel rate is fixed at 840MHz for all the modes */
  362. +#define IMX477_PIXEL_RATE 840000000
  363. +
  364. +/* V_TIMING internal */
  365. +#define IMX477_REG_FRAME_LENGTH 0x0340
  366. +#define IMX477_FRAME_LENGTH_MAX 0xffdc
  367. +
  368. +/* H_TIMING internal */
  369. +#define IMX477_REG_LINE_LENGTH 0x0342
  370. +#define IMX477_LINE_LENGTH_MAX 0xfff0
  371. +
  372. +/* Long exposure multiplier */
  373. +#define IMX477_LONG_EXP_SHIFT_MAX 7
  374. +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
  375. +
  376. +/* Exposure control */
  377. +#define IMX477_REG_EXPOSURE 0x0202
  378. +#define IMX477_EXPOSURE_OFFSET 22
  379. +#define IMX477_EXPOSURE_MIN 4
  380. +#define IMX477_EXPOSURE_STEP 1
  381. +#define IMX477_EXPOSURE_DEFAULT 0x640
  382. +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
  383. + IMX477_EXPOSURE_OFFSET)
  384. +
  385. +/* Analog gain control */
  386. +#define IMX477_REG_ANALOG_GAIN 0x0204
  387. +#define IMX477_ANA_GAIN_MIN 0
  388. +#define IMX477_ANA_GAIN_MAX 978
  389. +#define IMX477_ANA_GAIN_STEP 1
  390. +#define IMX477_ANA_GAIN_DEFAULT 0x0
  391. +
  392. +/* Digital gain control */
  393. +#define IMX477_REG_DIGITAL_GAIN 0x020e
  394. +#define IMX477_DGTL_GAIN_MIN 0x0100
  395. +#define IMX477_DGTL_GAIN_MAX 0xffff
  396. +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
  397. +#define IMX477_DGTL_GAIN_STEP 1
  398. +
  399. +/* Test Pattern Control */
  400. +#define IMX477_REG_TEST_PATTERN 0x0600
  401. +#define IMX477_TEST_PATTERN_DISABLE 0
  402. +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
  403. +#define IMX477_TEST_PATTERN_COLOR_BARS 2
  404. +#define IMX477_TEST_PATTERN_GREY_COLOR 3
  405. +#define IMX477_TEST_PATTERN_PN9 4
  406. +
  407. +/* Test pattern colour components */
  408. +#define IMX477_REG_TEST_PATTERN_R 0x0602
  409. +#define IMX477_REG_TEST_PATTERN_GR 0x0604
  410. +#define IMX477_REG_TEST_PATTERN_B 0x0606
  411. +#define IMX477_REG_TEST_PATTERN_GB 0x0608
  412. +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
  413. +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
  414. +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
  415. +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
  416. +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
  417. +#define IMX477_TEST_PATTERN_B_DEFAULT 0
  418. +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
  419. +
  420. +/* Trigger mode */
  421. +#define IMX477_REG_MC_MODE 0x3f0b
  422. +#define IMX477_REG_MS_SEL 0x3041
  423. +#define IMX477_REG_XVS_IO_CTRL 0x3040
  424. +#define IMX477_REG_EXTOUT_EN 0x4b81
  425. +
  426. +/* Embedded metadata stream structure */
  427. +#define IMX477_EMBEDDED_LINE_WIDTH 16384
  428. +#define IMX477_NUM_EMBEDDED_LINES 1
  429. +
  430. +enum pad_types {
  431. + IMAGE_PAD,
  432. + METADATA_PAD,
  433. + NUM_PADS
  434. +};
  435. +
  436. +/* IMX477 native and active pixel array size. */
  437. +#define IMX477_NATIVE_WIDTH 4072U
  438. +#define IMX477_NATIVE_HEIGHT 3176U
  439. +#define IMX477_PIXEL_ARRAY_LEFT 8U
  440. +#define IMX477_PIXEL_ARRAY_TOP 16U
  441. +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
  442. +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
  443. +
  444. +struct imx477_reg {
  445. + u16 address;
  446. + u8 val;
  447. +};
  448. +
  449. +struct imx477_reg_list {
  450. + unsigned int num_of_regs;
  451. + const struct imx477_reg *regs;
  452. +};
  453. +
  454. +/* Mode : resolution and related config&values */
  455. +struct imx477_mode {
  456. + /* Frame width */
  457. + unsigned int width;
  458. +
  459. + /* Frame height */
  460. + unsigned int height;
  461. +
  462. + /* H-timing in pixels */
  463. + unsigned int line_length_pix;
  464. +
  465. + /* Analog crop rectangle. */
  466. + struct v4l2_rect crop;
  467. +
  468. + /* Highest possible framerate. */
  469. + struct v4l2_fract timeperframe_min;
  470. +
  471. + /* Default framerate. */
  472. + struct v4l2_fract timeperframe_default;
  473. +
  474. + /* Default register values */
  475. + struct imx477_reg_list reg_list;
  476. +};
  477. +
  478. +static const struct imx477_reg mode_common_regs[] = {
  479. + {0x0136, 0x18},
  480. + {0x0137, 0x00},
  481. + {0xe000, 0x00},
  482. + {0xe07a, 0x01},
  483. + {0x0808, 0x02},
  484. + {0x4ae9, 0x18},
  485. + {0x4aea, 0x08},
  486. + {0xf61c, 0x04},
  487. + {0xf61e, 0x04},
  488. + {0x4ae9, 0x21},
  489. + {0x4aea, 0x80},
  490. + {0x38a8, 0x1f},
  491. + {0x38a9, 0xff},
  492. + {0x38aa, 0x1f},
  493. + {0x38ab, 0xff},
  494. + {0x55d4, 0x00},
  495. + {0x55d5, 0x00},
  496. + {0x55d6, 0x07},
  497. + {0x55d7, 0xff},
  498. + {0x55e8, 0x07},
  499. + {0x55e9, 0xff},
  500. + {0x55ea, 0x00},
  501. + {0x55eb, 0x00},
  502. + {0x574c, 0x07},
  503. + {0x574d, 0xff},
  504. + {0x574e, 0x00},
  505. + {0x574f, 0x00},
  506. + {0x5754, 0x00},
  507. + {0x5755, 0x00},
  508. + {0x5756, 0x07},
  509. + {0x5757, 0xff},
  510. + {0x5973, 0x04},
  511. + {0x5974, 0x01},
  512. + {0x5d13, 0xc3},
  513. + {0x5d14, 0x58},
  514. + {0x5d15, 0xa3},
  515. + {0x5d16, 0x1d},
  516. + {0x5d17, 0x65},
  517. + {0x5d18, 0x8c},
  518. + {0x5d1a, 0x06},
  519. + {0x5d1b, 0xa9},
  520. + {0x5d1c, 0x45},
  521. + {0x5d1d, 0x3a},
  522. + {0x5d1e, 0xab},
  523. + {0x5d1f, 0x15},
  524. + {0x5d21, 0x0e},
  525. + {0x5d22, 0x52},
  526. + {0x5d23, 0xaa},
  527. + {0x5d24, 0x7d},
  528. + {0x5d25, 0x57},
  529. + {0x5d26, 0xa8},
  530. + {0x5d37, 0x5a},
  531. + {0x5d38, 0x5a},
  532. + {0x5d77, 0x7f},
  533. + {0x7b75, 0x0e},
  534. + {0x7b76, 0x0b},
  535. + {0x7b77, 0x08},
  536. + {0x7b78, 0x0a},
  537. + {0x7b79, 0x47},
  538. + {0x7b7c, 0x00},
  539. + {0x7b7d, 0x00},
  540. + {0x8d1f, 0x00},
  541. + {0x8d27, 0x00},
  542. + {0x9004, 0x03},
  543. + {0x9200, 0x50},
  544. + {0x9201, 0x6c},
  545. + {0x9202, 0x71},
  546. + {0x9203, 0x00},
  547. + {0x9204, 0x71},
  548. + {0x9205, 0x01},
  549. + {0x9371, 0x6a},
  550. + {0x9373, 0x6a},
  551. + {0x9375, 0x64},
  552. + {0x991a, 0x00},
  553. + {0x996b, 0x8c},
  554. + {0x996c, 0x64},
  555. + {0x996d, 0x50},
  556. + {0x9a4c, 0x0d},
  557. + {0x9a4d, 0x0d},
  558. + {0xa001, 0x0a},
  559. + {0xa003, 0x0a},
  560. + {0xa005, 0x0a},
  561. + {0xa006, 0x01},
  562. + {0xa007, 0xc0},
  563. + {0xa009, 0xc0},
  564. + {0x3d8a, 0x01},
  565. + {0x4421, 0x04},
  566. + {0x7b3b, 0x01},
  567. + {0x7b4c, 0x00},
  568. + {0x9905, 0x00},
  569. + {0x9907, 0x00},
  570. + {0x9909, 0x00},
  571. + {0x990b, 0x00},
  572. + {0x9944, 0x3c},
  573. + {0x9947, 0x3c},
  574. + {0x994a, 0x8c},
  575. + {0x994b, 0x50},
  576. + {0x994c, 0x1b},
  577. + {0x994d, 0x8c},
  578. + {0x994e, 0x50},
  579. + {0x994f, 0x1b},
  580. + {0x9950, 0x8c},
  581. + {0x9951, 0x1b},
  582. + {0x9952, 0x0a},
  583. + {0x9953, 0x8c},
  584. + {0x9954, 0x1b},
  585. + {0x9955, 0x0a},
  586. + {0x9a13, 0x04},
  587. + {0x9a14, 0x04},
  588. + {0x9a19, 0x00},
  589. + {0x9a1c, 0x04},
  590. + {0x9a1d, 0x04},
  591. + {0x9a26, 0x05},
  592. + {0x9a27, 0x05},
  593. + {0x9a2c, 0x01},
  594. + {0x9a2d, 0x03},
  595. + {0x9a2f, 0x05},
  596. + {0x9a30, 0x05},
  597. + {0x9a41, 0x00},
  598. + {0x9a46, 0x00},
  599. + {0x9a47, 0x00},
  600. + {0x9c17, 0x35},
  601. + {0x9c1d, 0x31},
  602. + {0x9c29, 0x50},
  603. + {0x9c3b, 0x2f},
  604. + {0x9c41, 0x6b},
  605. + {0x9c47, 0x2d},
  606. + {0x9c4d, 0x40},
  607. + {0x9c6b, 0x00},
  608. + {0x9c71, 0xc8},
  609. + {0x9c73, 0x32},
  610. + {0x9c75, 0x04},
  611. + {0x9c7d, 0x2d},
  612. + {0x9c83, 0x40},
  613. + {0x9c94, 0x3f},
  614. + {0x9c95, 0x3f},
  615. + {0x9c96, 0x3f},
  616. + {0x9c97, 0x00},
  617. + {0x9c98, 0x00},
  618. + {0x9c99, 0x00},
  619. + {0x9c9a, 0x3f},
  620. + {0x9c9b, 0x3f},
  621. + {0x9c9c, 0x3f},
  622. + {0x9ca0, 0x0f},
  623. + {0x9ca1, 0x0f},
  624. + {0x9ca2, 0x0f},
  625. + {0x9ca3, 0x00},
  626. + {0x9ca4, 0x00},
  627. + {0x9ca5, 0x00},
  628. + {0x9ca6, 0x1e},
  629. + {0x9ca7, 0x1e},
  630. + {0x9ca8, 0x1e},
  631. + {0x9ca9, 0x00},
  632. + {0x9caa, 0x00},
  633. + {0x9cab, 0x00},
  634. + {0x9cac, 0x09},
  635. + {0x9cad, 0x09},
  636. + {0x9cae, 0x09},
  637. + {0x9cbd, 0x50},
  638. + {0x9cbf, 0x50},
  639. + {0x9cc1, 0x50},
  640. + {0x9cc3, 0x40},
  641. + {0x9cc5, 0x40},
  642. + {0x9cc7, 0x40},
  643. + {0x9cc9, 0x0a},
  644. + {0x9ccb, 0x0a},
  645. + {0x9ccd, 0x0a},
  646. + {0x9d17, 0x35},
  647. + {0x9d1d, 0x31},
  648. + {0x9d29, 0x50},
  649. + {0x9d3b, 0x2f},
  650. + {0x9d41, 0x6b},
  651. + {0x9d47, 0x42},
  652. + {0x9d4d, 0x5a},
  653. + {0x9d6b, 0x00},
  654. + {0x9d71, 0xc8},
  655. + {0x9d73, 0x32},
  656. + {0x9d75, 0x04},
  657. + {0x9d7d, 0x42},
  658. + {0x9d83, 0x5a},
  659. + {0x9d94, 0x3f},
  660. + {0x9d95, 0x3f},
  661. + {0x9d96, 0x3f},
  662. + {0x9d97, 0x00},
  663. + {0x9d98, 0x00},
  664. + {0x9d99, 0x00},
  665. + {0x9d9a, 0x3f},
  666. + {0x9d9b, 0x3f},
  667. + {0x9d9c, 0x3f},
  668. + {0x9d9d, 0x1f},
  669. + {0x9d9e, 0x1f},
  670. + {0x9d9f, 0x1f},
  671. + {0x9da0, 0x0f},
  672. + {0x9da1, 0x0f},
  673. + {0x9da2, 0x0f},
  674. + {0x9da3, 0x00},
  675. + {0x9da4, 0x00},
  676. + {0x9da5, 0x00},
  677. + {0x9da6, 0x1e},
  678. + {0x9da7, 0x1e},
  679. + {0x9da8, 0x1e},
  680. + {0x9da9, 0x00},
  681. + {0x9daa, 0x00},
  682. + {0x9dab, 0x00},
  683. + {0x9dac, 0x09},
  684. + {0x9dad, 0x09},
  685. + {0x9dae, 0x09},
  686. + {0x9dc9, 0x0a},
  687. + {0x9dcb, 0x0a},
  688. + {0x9dcd, 0x0a},
  689. + {0x9e17, 0x35},
  690. + {0x9e1d, 0x31},
  691. + {0x9e29, 0x50},
  692. + {0x9e3b, 0x2f},
  693. + {0x9e41, 0x6b},
  694. + {0x9e47, 0x2d},
  695. + {0x9e4d, 0x40},
  696. + {0x9e6b, 0x00},
  697. + {0x9e71, 0xc8},
  698. + {0x9e73, 0x32},
  699. + {0x9e75, 0x04},
  700. + {0x9e94, 0x0f},
  701. + {0x9e95, 0x0f},
  702. + {0x9e96, 0x0f},
  703. + {0x9e97, 0x00},
  704. + {0x9e98, 0x00},
  705. + {0x9e99, 0x00},
  706. + {0x9ea0, 0x0f},
  707. + {0x9ea1, 0x0f},
  708. + {0x9ea2, 0x0f},
  709. + {0x9ea3, 0x00},
  710. + {0x9ea4, 0x00},
  711. + {0x9ea5, 0x00},
  712. + {0x9ea6, 0x3f},
  713. + {0x9ea7, 0x3f},
  714. + {0x9ea8, 0x3f},
  715. + {0x9ea9, 0x00},
  716. + {0x9eaa, 0x00},
  717. + {0x9eab, 0x00},
  718. + {0x9eac, 0x09},
  719. + {0x9ead, 0x09},
  720. + {0x9eae, 0x09},
  721. + {0x9ec9, 0x0a},
  722. + {0x9ecb, 0x0a},
  723. + {0x9ecd, 0x0a},
  724. + {0x9f17, 0x35},
  725. + {0x9f1d, 0x31},
  726. + {0x9f29, 0x50},
  727. + {0x9f3b, 0x2f},
  728. + {0x9f41, 0x6b},
  729. + {0x9f47, 0x42},
  730. + {0x9f4d, 0x5a},
  731. + {0x9f6b, 0x00},
  732. + {0x9f71, 0xc8},
  733. + {0x9f73, 0x32},
  734. + {0x9f75, 0x04},
  735. + {0x9f94, 0x0f},
  736. + {0x9f95, 0x0f},
  737. + {0x9f96, 0x0f},
  738. + {0x9f97, 0x00},
  739. + {0x9f98, 0x00},
  740. + {0x9f99, 0x00},
  741. + {0x9f9a, 0x2f},
  742. + {0x9f9b, 0x2f},
  743. + {0x9f9c, 0x2f},
  744. + {0x9f9d, 0x00},
  745. + {0x9f9e, 0x00},
  746. + {0x9f9f, 0x00},
  747. + {0x9fa0, 0x0f},
  748. + {0x9fa1, 0x0f},
  749. + {0x9fa2, 0x0f},
  750. + {0x9fa3, 0x00},
  751. + {0x9fa4, 0x00},
  752. + {0x9fa5, 0x00},
  753. + {0x9fa6, 0x1e},
  754. + {0x9fa7, 0x1e},
  755. + {0x9fa8, 0x1e},
  756. + {0x9fa9, 0x00},
  757. + {0x9faa, 0x00},
  758. + {0x9fab, 0x00},
  759. + {0x9fac, 0x09},
  760. + {0x9fad, 0x09},
  761. + {0x9fae, 0x09},
  762. + {0x9fc9, 0x0a},
  763. + {0x9fcb, 0x0a},
  764. + {0x9fcd, 0x0a},
  765. + {0xa14b, 0xff},
  766. + {0xa151, 0x0c},
  767. + {0xa153, 0x50},
  768. + {0xa155, 0x02},
  769. + {0xa157, 0x00},
  770. + {0xa1ad, 0xff},
  771. + {0xa1b3, 0x0c},
  772. + {0xa1b5, 0x50},
  773. + {0xa1b9, 0x00},
  774. + {0xa24b, 0xff},
  775. + {0xa257, 0x00},
  776. + {0xa2ad, 0xff},
  777. + {0xa2b9, 0x00},
  778. + {0xb21f, 0x04},
  779. + {0xb35c, 0x00},
  780. + {0xb35e, 0x08},
  781. + {0x0112, 0x0c},
  782. + {0x0113, 0x0c},
  783. + {0x0114, 0x01},
  784. + {0x0350, 0x00},
  785. + {0xbcf1, 0x02},
  786. + {0x3ff9, 0x01},
  787. +};
  788. +
  789. +/* 12 mpix 10fps */
  790. +static const struct imx477_reg mode_4056x3040_regs[] = {
  791. + {0x0342, 0x5d},
  792. + {0x0343, 0xc0},
  793. + {0x0344, 0x00},
  794. + {0x0345, 0x00},
  795. + {0x0346, 0x00},
  796. + {0x0347, 0x00},
  797. + {0x0348, 0x0f},
  798. + {0x0349, 0xd7},
  799. + {0x034a, 0x0b},
  800. + {0x034b, 0xdf},
  801. + {0x00e3, 0x00},
  802. + {0x00e4, 0x00},
  803. + {0x00fc, 0x0a},
  804. + {0x00fd, 0x0a},
  805. + {0x00fe, 0x0a},
  806. + {0x00ff, 0x0a},
  807. + {0x0220, 0x00},
  808. + {0x0221, 0x11},
  809. + {0x0381, 0x01},
  810. + {0x0383, 0x01},
  811. + {0x0385, 0x01},
  812. + {0x0387, 0x01},
  813. + {0x0900, 0x00},
  814. + {0x0901, 0x11},
  815. + {0x0902, 0x02},
  816. + {0x3140, 0x02},
  817. + {0x3c00, 0x00},
  818. + {0x3c01, 0x03},
  819. + {0x3c02, 0xa2},
  820. + {0x3f0d, 0x01},
  821. + {0x5748, 0x07},
  822. + {0x5749, 0xff},
  823. + {0x574a, 0x00},
  824. + {0x574b, 0x00},
  825. + {0x7b75, 0x0a},
  826. + {0x7b76, 0x0c},
  827. + {0x7b77, 0x07},
  828. + {0x7b78, 0x06},
  829. + {0x7b79, 0x3c},
  830. + {0x7b53, 0x01},
  831. + {0x9369, 0x5a},
  832. + {0x936b, 0x55},
  833. + {0x936d, 0x28},
  834. + {0x9304, 0x00},
  835. + {0x9305, 0x00},
  836. + {0x9e9a, 0x2f},
  837. + {0x9e9b, 0x2f},
  838. + {0x9e9c, 0x2f},
  839. + {0x9e9d, 0x00},
  840. + {0x9e9e, 0x00},
  841. + {0x9e9f, 0x00},
  842. + {0xa2a9, 0x60},
  843. + {0xa2b7, 0x00},
  844. + {0x0401, 0x00},
  845. + {0x0404, 0x00},
  846. + {0x0405, 0x10},
  847. + {0x0408, 0x00},
  848. + {0x0409, 0x00},
  849. + {0x040a, 0x00},
  850. + {0x040b, 0x00},
  851. + {0x040c, 0x0f},
  852. + {0x040d, 0xd8},
  853. + {0x040e, 0x0b},
  854. + {0x040f, 0xe0},
  855. + {0x034c, 0x0f},
  856. + {0x034d, 0xd8},
  857. + {0x034e, 0x0b},
  858. + {0x034f, 0xe0},
  859. + {0x0301, 0x05},
  860. + {0x0303, 0x02},
  861. + {0x0305, 0x04},
  862. + {0x0306, 0x01},
  863. + {0x0307, 0x5e},
  864. + {0x0309, 0x0c},
  865. + {0x030b, 0x02},
  866. + {0x030d, 0x02},
  867. + {0x030e, 0x00},
  868. + {0x030f, 0x96},
  869. + {0x0310, 0x01},
  870. + {0x0820, 0x07},
  871. + {0x0821, 0x08},
  872. + {0x0822, 0x00},
  873. + {0x0823, 0x00},
  874. + {0x080a, 0x00},
  875. + {0x080b, 0x7f},
  876. + {0x080c, 0x00},
  877. + {0x080d, 0x4f},
  878. + {0x080e, 0x00},
  879. + {0x080f, 0x77},
  880. + {0x0810, 0x00},
  881. + {0x0811, 0x5f},
  882. + {0x0812, 0x00},
  883. + {0x0813, 0x57},
  884. + {0x0814, 0x00},
  885. + {0x0815, 0x4f},
  886. + {0x0816, 0x01},
  887. + {0x0817, 0x27},
  888. + {0x0818, 0x00},
  889. + {0x0819, 0x3f},
  890. + {0xe04c, 0x00},
  891. + {0xe04d, 0x7f},
  892. + {0xe04e, 0x00},
  893. + {0xe04f, 0x1f},
  894. + {0x3e20, 0x01},
  895. + {0x3e37, 0x00},
  896. + {0x3f50, 0x00},
  897. + {0x3f56, 0x02},
  898. + {0x3f57, 0xae},
  899. +};
  900. +
  901. +/* 2x2 binned. 40fps */
  902. +static const struct imx477_reg mode_2028x1520_regs[] = {
  903. + {0x0342, 0x31},
  904. + {0x0343, 0xc4},
  905. + {0x0344, 0x00},
  906. + {0x0345, 0x00},
  907. + {0x0346, 0x00},
  908. + {0x0347, 0x00},
  909. + {0x0348, 0x0f},
  910. + {0x0349, 0xd7},
  911. + {0x034a, 0x0b},
  912. + {0x034b, 0xdf},
  913. + {0x0220, 0x00},
  914. + {0x0221, 0x11},
  915. + {0x0381, 0x01},
  916. + {0x0383, 0x01},
  917. + {0x0385, 0x01},
  918. + {0x0387, 0x01},
  919. + {0x0900, 0x01},
  920. + {0x0901, 0x12},
  921. + {0x0902, 0x02},
  922. + {0x3140, 0x02},
  923. + {0x3c00, 0x00},
  924. + {0x3c01, 0x03},
  925. + {0x3c02, 0xa2},
  926. + {0x3f0d, 0x01},
  927. + {0x5748, 0x07},
  928. + {0x5749, 0xff},
  929. + {0x574a, 0x00},
  930. + {0x574b, 0x00},
  931. + {0x7b53, 0x01},
  932. + {0x9369, 0x73},
  933. + {0x936b, 0x64},
  934. + {0x936d, 0x5f},
  935. + {0x9304, 0x00},
  936. + {0x9305, 0x00},
  937. + {0x9e9a, 0x2f},
  938. + {0x9e9b, 0x2f},
  939. + {0x9e9c, 0x2f},
  940. + {0x9e9d, 0x00},
  941. + {0x9e9e, 0x00},
  942. + {0x9e9f, 0x00},
  943. + {0xa2a9, 0x60},
  944. + {0xa2b7, 0x00},
  945. + {0x0401, 0x01},
  946. + {0x0404, 0x00},
  947. + {0x0405, 0x20},
  948. + {0x0408, 0x00},
  949. + {0x0409, 0x00},
  950. + {0x040a, 0x00},
  951. + {0x040b, 0x00},
  952. + {0x040c, 0x0f},
  953. + {0x040d, 0xd8},
  954. + {0x040e, 0x0b},
  955. + {0x040f, 0xe0},
  956. + {0x034c, 0x07},
  957. + {0x034d, 0xec},
  958. + {0x034e, 0x05},
  959. + {0x034f, 0xf0},
  960. + {0x0301, 0x05},
  961. + {0x0303, 0x02},
  962. + {0x0305, 0x04},
  963. + {0x0306, 0x01},
  964. + {0x0307, 0x5e},
  965. + {0x0309, 0x0c},
  966. + {0x030b, 0x02},
  967. + {0x030d, 0x02},
  968. + {0x030e, 0x00},
  969. + {0x030f, 0x96},
  970. + {0x0310, 0x01},
  971. + {0x0820, 0x07},
  972. + {0x0821, 0x08},
  973. + {0x0822, 0x00},
  974. + {0x0823, 0x00},
  975. + {0x080a, 0x00},
  976. + {0x080b, 0x7f},
  977. + {0x080c, 0x00},
  978. + {0x080d, 0x4f},
  979. + {0x080e, 0x00},
  980. + {0x080f, 0x77},
  981. + {0x0810, 0x00},
  982. + {0x0811, 0x5f},
  983. + {0x0812, 0x00},
  984. + {0x0813, 0x57},
  985. + {0x0814, 0x00},
  986. + {0x0815, 0x4f},
  987. + {0x0816, 0x01},
  988. + {0x0817, 0x27},
  989. + {0x0818, 0x00},
  990. + {0x0819, 0x3f},
  991. + {0xe04c, 0x00},
  992. + {0xe04d, 0x7f},
  993. + {0xe04e, 0x00},
  994. + {0xe04f, 0x1f},
  995. + {0x3e20, 0x01},
  996. + {0x3e37, 0x00},
  997. + {0x3f50, 0x00},
  998. + {0x3f56, 0x01},
  999. + {0x3f57, 0x6c},
  1000. +};
  1001. +
  1002. +/* 1080p cropped mode */
  1003. +static const struct imx477_reg mode_2028x1080_regs[] = {
  1004. + {0x0342, 0x31},
  1005. + {0x0343, 0xc4},
  1006. + {0x0344, 0x00},
  1007. + {0x0345, 0x00},
  1008. + {0x0346, 0x01},
  1009. + {0x0347, 0xb8},
  1010. + {0x0348, 0x0f},
  1011. + {0x0349, 0xd7},
  1012. + {0x034a, 0x0a},
  1013. + {0x034b, 0x27},
  1014. + {0x0220, 0x00},
  1015. + {0x0221, 0x11},
  1016. + {0x0381, 0x01},
  1017. + {0x0383, 0x01},
  1018. + {0x0385, 0x01},
  1019. + {0x0387, 0x01},
  1020. + {0x0900, 0x01},
  1021. + {0x0901, 0x12},
  1022. + {0x0902, 0x02},
  1023. + {0x3140, 0x02},
  1024. + {0x3c00, 0x00},
  1025. + {0x3c01, 0x03},
  1026. + {0x3c02, 0xa2},
  1027. + {0x3f0d, 0x01},
  1028. + {0x5748, 0x07},
  1029. + {0x5749, 0xff},
  1030. + {0x574a, 0x00},
  1031. + {0x574b, 0x00},
  1032. + {0x7b53, 0x01},
  1033. + {0x9369, 0x73},
  1034. + {0x936b, 0x64},
  1035. + {0x936d, 0x5f},
  1036. + {0x9304, 0x00},
  1037. + {0x9305, 0x00},
  1038. + {0x9e9a, 0x2f},
  1039. + {0x9e9b, 0x2f},
  1040. + {0x9e9c, 0x2f},
  1041. + {0x9e9d, 0x00},
  1042. + {0x9e9e, 0x00},
  1043. + {0x9e9f, 0x00},
  1044. + {0xa2a9, 0x60},
  1045. + {0xa2b7, 0x00},
  1046. + {0x0401, 0x01},
  1047. + {0x0404, 0x00},
  1048. + {0x0405, 0x20},
  1049. + {0x0408, 0x00},
  1050. + {0x0409, 0x00},
  1051. + {0x040a, 0x00},
  1052. + {0x040b, 0x00},
  1053. + {0x040c, 0x0f},
  1054. + {0x040d, 0xd8},
  1055. + {0x040e, 0x04},
  1056. + {0x040f, 0x38},
  1057. + {0x034c, 0x07},
  1058. + {0x034d, 0xec},
  1059. + {0x034e, 0x04},
  1060. + {0x034f, 0x38},
  1061. + {0x0301, 0x05},
  1062. + {0x0303, 0x02},
  1063. + {0x0305, 0x04},
  1064. + {0x0306, 0x01},
  1065. + {0x0307, 0x5e},
  1066. + {0x0309, 0x0c},
  1067. + {0x030b, 0x02},
  1068. + {0x030d, 0x02},
  1069. + {0x030e, 0x00},
  1070. + {0x030f, 0x96},
  1071. + {0x0310, 0x01},
  1072. + {0x0820, 0x07},
  1073. + {0x0821, 0x08},
  1074. + {0x0822, 0x00},
  1075. + {0x0823, 0x00},
  1076. + {0x080a, 0x00},
  1077. + {0x080b, 0x7f},
  1078. + {0x080c, 0x00},
  1079. + {0x080d, 0x4f},
  1080. + {0x080e, 0x00},
  1081. + {0x080f, 0x77},
  1082. + {0x0810, 0x00},
  1083. + {0x0811, 0x5f},
  1084. + {0x0812, 0x00},
  1085. + {0x0813, 0x57},
  1086. + {0x0814, 0x00},
  1087. + {0x0815, 0x4f},
  1088. + {0x0816, 0x01},
  1089. + {0x0817, 0x27},
  1090. + {0x0818, 0x00},
  1091. + {0x0819, 0x3f},
  1092. + {0xe04c, 0x00},
  1093. + {0xe04d, 0x7f},
  1094. + {0xe04e, 0x00},
  1095. + {0xe04f, 0x1f},
  1096. + {0x3e20, 0x01},
  1097. + {0x3e37, 0x00},
  1098. + {0x3f50, 0x00},
  1099. + {0x3f56, 0x01},
  1100. + {0x3f57, 0x6c},
  1101. +};
  1102. +
  1103. +/* 4x4 binned. 120fps */
  1104. +static const struct imx477_reg mode_1332x990_regs[] = {
  1105. + {0x420b, 0x01},
  1106. + {0x990c, 0x00},
  1107. + {0x990d, 0x08},
  1108. + {0x9956, 0x8c},
  1109. + {0x9957, 0x64},
  1110. + {0x9958, 0x50},
  1111. + {0x9a48, 0x06},
  1112. + {0x9a49, 0x06},
  1113. + {0x9a4a, 0x06},
  1114. + {0x9a4b, 0x06},
  1115. + {0x9a4c, 0x06},
  1116. + {0x9a4d, 0x06},
  1117. + {0x0112, 0x0a},
  1118. + {0x0113, 0x0a},
  1119. + {0x0114, 0x01},
  1120. + {0x0342, 0x1a},
  1121. + {0x0343, 0x08},
  1122. + {0x0340, 0x04},
  1123. + {0x0341, 0x1a},
  1124. + {0x0344, 0x00},
  1125. + {0x0345, 0x00},
  1126. + {0x0346, 0x02},
  1127. + {0x0347, 0x10},
  1128. + {0x0348, 0x0f},
  1129. + {0x0349, 0xd7},
  1130. + {0x034a, 0x09},
  1131. + {0x034b, 0xcf},
  1132. + {0x00e3, 0x00},
  1133. + {0x00e4, 0x00},
  1134. + {0x00fc, 0x0a},
  1135. + {0x00fd, 0x0a},
  1136. + {0x00fe, 0x0a},
  1137. + {0x00ff, 0x0a},
  1138. + {0xe013, 0x00},
  1139. + {0x0220, 0x00},
  1140. + {0x0221, 0x11},
  1141. + {0x0381, 0x01},
  1142. + {0x0383, 0x01},
  1143. + {0x0385, 0x01},
  1144. + {0x0387, 0x01},
  1145. + {0x0900, 0x01},
  1146. + {0x0901, 0x22},
  1147. + {0x0902, 0x02},
  1148. + {0x3140, 0x02},
  1149. + {0x3c00, 0x00},
  1150. + {0x3c01, 0x01},
  1151. + {0x3c02, 0x9c},
  1152. + {0x3f0d, 0x00},
  1153. + {0x5748, 0x00},
  1154. + {0x5749, 0x00},
  1155. + {0x574a, 0x00},
  1156. + {0x574b, 0xa4},
  1157. + {0x7b75, 0x0e},
  1158. + {0x7b76, 0x09},
  1159. + {0x7b77, 0x08},
  1160. + {0x7b78, 0x06},
  1161. + {0x7b79, 0x34},
  1162. + {0x7b53, 0x00},
  1163. + {0x9369, 0x73},
  1164. + {0x936b, 0x64},
  1165. + {0x936d, 0x5f},
  1166. + {0x9304, 0x03},
  1167. + {0x9305, 0x80},
  1168. + {0x9e9a, 0x2f},
  1169. + {0x9e9b, 0x2f},
  1170. + {0x9e9c, 0x2f},
  1171. + {0x9e9d, 0x00},
  1172. + {0x9e9e, 0x00},
  1173. + {0x9e9f, 0x00},
  1174. + {0xa2a9, 0x27},
  1175. + {0xa2b7, 0x03},
  1176. + {0x0401, 0x00},
  1177. + {0x0404, 0x00},
  1178. + {0x0405, 0x10},
  1179. + {0x0408, 0x01},
  1180. + {0x0409, 0x5c},
  1181. + {0x040a, 0x00},
  1182. + {0x040b, 0x00},
  1183. + {0x040c, 0x05},
  1184. + {0x040d, 0x34},
  1185. + {0x040e, 0x03},
  1186. + {0x040f, 0xde},
  1187. + {0x034c, 0x05},
  1188. + {0x034d, 0x34},
  1189. + {0x034e, 0x03},
  1190. + {0x034f, 0xde},
  1191. + {0x0301, 0x05},
  1192. + {0x0303, 0x02},
  1193. + {0x0305, 0x02},
  1194. + {0x0306, 0x00},
  1195. + {0x0307, 0xaf},
  1196. + {0x0309, 0x0a},
  1197. + {0x030b, 0x02},
  1198. + {0x030d, 0x02},
  1199. + {0x030e, 0x00},
  1200. + {0x030f, 0x96},
  1201. + {0x0310, 0x01},
  1202. + {0x0820, 0x07},
  1203. + {0x0821, 0x08},
  1204. + {0x0822, 0x00},
  1205. + {0x0823, 0x00},
  1206. + {0x080a, 0x00},
  1207. + {0x080b, 0x7f},
  1208. + {0x080c, 0x00},
  1209. + {0x080d, 0x4f},
  1210. + {0x080e, 0x00},
  1211. + {0x080f, 0x77},
  1212. + {0x0810, 0x00},
  1213. + {0x0811, 0x5f},
  1214. + {0x0812, 0x00},
  1215. + {0x0813, 0x57},
  1216. + {0x0814, 0x00},
  1217. + {0x0815, 0x4f},
  1218. + {0x0816, 0x01},
  1219. + {0x0817, 0x27},
  1220. + {0x0818, 0x00},
  1221. + {0x0819, 0x3f},
  1222. + {0xe04c, 0x00},
  1223. + {0xe04d, 0x5f},
  1224. + {0xe04e, 0x00},
  1225. + {0xe04f, 0x1f},
  1226. + {0x3e20, 0x01},
  1227. + {0x3e37, 0x00},
  1228. + {0x3f50, 0x00},
  1229. + {0x3f56, 0x00},
  1230. + {0x3f57, 0xbf},
  1231. +};
  1232. +
  1233. +/* Mode configs */
  1234. +static const struct imx477_mode supported_modes_12bit[] = {
  1235. + {
  1236. + /* 12MPix 10fps mode */
  1237. + .width = 4056,
  1238. + .height = 3040,
  1239. + .line_length_pix = 0x5dc0,
  1240. + .crop = {
  1241. + .left = IMX477_PIXEL_ARRAY_LEFT,
  1242. + .top = IMX477_PIXEL_ARRAY_TOP,
  1243. + .width = 4056,
  1244. + .height = 3040,
  1245. + },
  1246. + .timeperframe_min = {
  1247. + .numerator = 100,
  1248. + .denominator = 1000
  1249. + },
  1250. + .timeperframe_default = {
  1251. + .numerator = 100,
  1252. + .denominator = 1000
  1253. + },
  1254. + .reg_list = {
  1255. + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
  1256. + .regs = mode_4056x3040_regs,
  1257. + },
  1258. + },
  1259. + {
  1260. + /* 2x2 binned 40fps mode */
  1261. + .width = 2028,
  1262. + .height = 1520,
  1263. + .line_length_pix = 0x31c4,
  1264. + .crop = {
  1265. + .left = IMX477_PIXEL_ARRAY_LEFT,
  1266. + .top = IMX477_PIXEL_ARRAY_TOP,
  1267. + .width = 4056,
  1268. + .height = 3040,
  1269. + },
  1270. + .timeperframe_min = {
  1271. + .numerator = 100,
  1272. + .denominator = 4000
  1273. + },
  1274. + .timeperframe_default = {
  1275. + .numerator = 100,
  1276. + .denominator = 3000
  1277. + },
  1278. + .reg_list = {
  1279. + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
  1280. + .regs = mode_2028x1520_regs,
  1281. + },
  1282. + },
  1283. + {
  1284. + /* 1080p 50fps cropped mode */
  1285. + .width = 2028,
  1286. + .height = 1080,
  1287. + .line_length_pix = 0x31c4,
  1288. + .crop = {
  1289. + .left = IMX477_PIXEL_ARRAY_LEFT,
  1290. + .top = IMX477_PIXEL_ARRAY_TOP + 440,
  1291. + .width = 4056,
  1292. + .height = 2160,
  1293. + },
  1294. + .timeperframe_min = {
  1295. + .numerator = 100,
  1296. + .denominator = 5000
  1297. + },
  1298. + .timeperframe_default = {
  1299. + .numerator = 100,
  1300. + .denominator = 3000
  1301. + },
  1302. + .reg_list = {
  1303. + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
  1304. + .regs = mode_2028x1080_regs,
  1305. + },
  1306. + }
  1307. +};
  1308. +
  1309. +static const struct imx477_mode supported_modes_10bit[] = {
  1310. + {
  1311. + /* 120fps. 2x2 binned and cropped */
  1312. + .width = 1332,
  1313. + .height = 990,
  1314. + .line_length_pix = 6664,
  1315. + .crop = {
  1316. + /*
  1317. + * FIXME: the analog crop rectangle is actually
  1318. + * programmed with a horizontal displacement of 0
  1319. + * pixels, not 4. It gets shrunk after going through
  1320. + * the scaler. Move this information to the compose
  1321. + * rectangle once the driver is expanded to represent
  1322. + * its processing blocks with multiple subdevs.
  1323. + */
  1324. + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
  1325. + .top = IMX477_PIXEL_ARRAY_TOP + 528,
  1326. + .width = 2664,
  1327. + .height = 1980,
  1328. + },
  1329. + .timeperframe_min = {
  1330. + .numerator = 100,
  1331. + .denominator = 12000
  1332. + },
  1333. + .timeperframe_default = {
  1334. + .numerator = 100,
  1335. + .denominator = 12000
  1336. + },
  1337. + .reg_list = {
  1338. + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
  1339. + .regs = mode_1332x990_regs,
  1340. + }
  1341. + }
  1342. +};
  1343. +
  1344. +/*
  1345. + * The supported formats.
  1346. + * This table MUST contain 4 entries per format, to cover the various flip
  1347. + * combinations in the order
  1348. + * - no flip
  1349. + * - h flip
  1350. + * - v flip
  1351. + * - h&v flips
  1352. + */
  1353. +static const u32 codes[] = {
  1354. + /* 12-bit modes. */
  1355. + MEDIA_BUS_FMT_SRGGB12_1X12,
  1356. + MEDIA_BUS_FMT_SGRBG12_1X12,
  1357. + MEDIA_BUS_FMT_SGBRG12_1X12,
  1358. + MEDIA_BUS_FMT_SBGGR12_1X12,
  1359. + /* 10-bit modes. */
  1360. + MEDIA_BUS_FMT_SRGGB10_1X10,
  1361. + MEDIA_BUS_FMT_SGRBG10_1X10,
  1362. + MEDIA_BUS_FMT_SGBRG10_1X10,
  1363. + MEDIA_BUS_FMT_SBGGR10_1X10,
  1364. +};
  1365. +
  1366. +static const char * const imx477_test_pattern_menu[] = {
  1367. + "Disabled",
  1368. + "Color Bars",
  1369. + "Solid Color",
  1370. + "Grey Color Bars",
  1371. + "PN9"
  1372. +};
  1373. +
  1374. +static const int imx477_test_pattern_val[] = {
  1375. + IMX477_TEST_PATTERN_DISABLE,
  1376. + IMX477_TEST_PATTERN_COLOR_BARS,
  1377. + IMX477_TEST_PATTERN_SOLID_COLOR,
  1378. + IMX477_TEST_PATTERN_GREY_COLOR,
  1379. + IMX477_TEST_PATTERN_PN9,
  1380. +};
  1381. +
  1382. +/* regulator supplies */
  1383. +static const char * const imx477_supply_name[] = {
  1384. + /* Supplies can be enabled in any order */
  1385. + "VANA", /* Analog (2.8V) supply */
  1386. + "VDIG", /* Digital Core (1.05V) supply */
  1387. + "VDDL", /* IF (1.8V) supply */
  1388. +};
  1389. +
  1390. +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
  1391. +
  1392. +/*
  1393. + * Initialisation delay between XCLR low->high and the moment when the sensor
  1394. + * can start capture (i.e. can leave software standby), given by T7 in the
  1395. + * datasheet is 8ms. This does include I2C setup time as well.
  1396. + *
  1397. + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
  1398. + * in the datasheet) is much smaller - 600us.
  1399. + */
  1400. +#define IMX477_XCLR_MIN_DELAY_US 8000
  1401. +#define IMX477_XCLR_DELAY_RANGE_US 1000
  1402. +
  1403. +struct imx477_compatible_data {
  1404. + unsigned int chip_id;
  1405. + struct imx477_reg_list extra_regs;
  1406. +};
  1407. +
  1408. +struct imx477 {
  1409. + struct v4l2_subdev sd;
  1410. + struct media_pad pad[NUM_PADS];
  1411. +
  1412. + unsigned int fmt_code;
  1413. +
  1414. + struct clk *xclk;
  1415. + u32 xclk_freq;
  1416. +
  1417. + struct gpio_desc *reset_gpio;
  1418. + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
  1419. +
  1420. + struct v4l2_ctrl_handler ctrl_handler;
  1421. + /* V4L2 Controls */
  1422. + struct v4l2_ctrl *pixel_rate;
  1423. + struct v4l2_ctrl *exposure;
  1424. + struct v4l2_ctrl *vflip;
  1425. + struct v4l2_ctrl *hflip;
  1426. + struct v4l2_ctrl *vblank;
  1427. + struct v4l2_ctrl *hblank;
  1428. +
  1429. + /* Current mode */
  1430. + const struct imx477_mode *mode;
  1431. +
  1432. + /*
  1433. + * Mutex for serialized access:
  1434. + * Protect sensor module set pad format and start/stop streaming safely.
  1435. + */
  1436. + struct mutex mutex;
  1437. +
  1438. + /* Streaming on/off */
  1439. + bool streaming;
  1440. +
  1441. + /* Rewrite common registers on stream on? */
  1442. + bool common_regs_written;
  1443. +
  1444. + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
  1445. + unsigned int long_exp_shift;
  1446. +
  1447. + /* Any extra information related to different compatible sensors */
  1448. + const struct imx477_compatible_data *compatible_data;
  1449. +};
  1450. +
  1451. +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
  1452. +{
  1453. + return container_of(_sd, struct imx477, sd);
  1454. +}
  1455. +
  1456. +static inline void get_mode_table(unsigned int code,
  1457. + const struct imx477_mode **mode_list,
  1458. + unsigned int *num_modes)
  1459. +{
  1460. + switch (code) {
  1461. + /* 12-bit */
  1462. + case MEDIA_BUS_FMT_SRGGB12_1X12:
  1463. + case MEDIA_BUS_FMT_SGRBG12_1X12:
  1464. + case MEDIA_BUS_FMT_SGBRG12_1X12:
  1465. + case MEDIA_BUS_FMT_SBGGR12_1X12:
  1466. + *mode_list = supported_modes_12bit;
  1467. + *num_modes = ARRAY_SIZE(supported_modes_12bit);
  1468. + break;
  1469. + /* 10-bit */
  1470. + case MEDIA_BUS_FMT_SRGGB10_1X10:
  1471. + case MEDIA_BUS_FMT_SGRBG10_1X10:
  1472. + case MEDIA_BUS_FMT_SGBRG10_1X10:
  1473. + case MEDIA_BUS_FMT_SBGGR10_1X10:
  1474. + *mode_list = supported_modes_10bit;
  1475. + *num_modes = ARRAY_SIZE(supported_modes_10bit);
  1476. + break;
  1477. + default:
  1478. + *mode_list = NULL;
  1479. + *num_modes = 0;
  1480. + }
  1481. +}
  1482. +
  1483. +/* Read registers up to 2 at a time */
  1484. +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
  1485. +{
  1486. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  1487. + struct i2c_msg msgs[2];
  1488. + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
  1489. + u8 data_buf[4] = { 0, };
  1490. + int ret;
  1491. +
  1492. + if (len > 4)
  1493. + return -EINVAL;
  1494. +
  1495. + /* Write register address */
  1496. + msgs[0].addr = client->addr;
  1497. + msgs[0].flags = 0;
  1498. + msgs[0].len = ARRAY_SIZE(addr_buf);
  1499. + msgs[0].buf = addr_buf;
  1500. +
  1501. + /* Read data from register */
  1502. + msgs[1].addr = client->addr;
  1503. + msgs[1].flags = I2C_M_RD;
  1504. + msgs[1].len = len;
  1505. + msgs[1].buf = &data_buf[4 - len];
  1506. +
  1507. + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  1508. + if (ret != ARRAY_SIZE(msgs))
  1509. + return -EIO;
  1510. +
  1511. + *val = get_unaligned_be32(data_buf);
  1512. +
  1513. + return 0;
  1514. +}
  1515. +
  1516. +/* Write registers up to 2 at a time */
  1517. +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
  1518. +{
  1519. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  1520. + u8 buf[6];
  1521. +
  1522. + if (len > 4)
  1523. + return -EINVAL;
  1524. +
  1525. + put_unaligned_be16(reg, buf);
  1526. + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  1527. + if (i2c_master_send(client, buf, len + 2) != len + 2)
  1528. + return -EIO;
  1529. +
  1530. + return 0;
  1531. +}
  1532. +
  1533. +/* Write a list of registers */
  1534. +static int imx477_write_regs(struct imx477 *imx477,
  1535. + const struct imx477_reg *regs, u32 len)
  1536. +{
  1537. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  1538. + unsigned int i;
  1539. + int ret;
  1540. +
  1541. + for (i = 0; i < len; i++) {
  1542. + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
  1543. + if (ret) {
  1544. + dev_err_ratelimited(&client->dev,
  1545. + "Failed to write reg 0x%4.4x. error = %d\n",
  1546. + regs[i].address, ret);
  1547. +
  1548. + return ret;
  1549. + }
  1550. + }
  1551. +
  1552. + return 0;
  1553. +}
  1554. +
  1555. +/* Get bayer order based on flip setting. */
  1556. +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
  1557. +{
  1558. + unsigned int i;
  1559. +
  1560. + lockdep_assert_held(&imx477->mutex);
  1561. +
  1562. + for (i = 0; i < ARRAY_SIZE(codes); i++)
  1563. + if (codes[i] == code)
  1564. + break;
  1565. +
  1566. + if (i >= ARRAY_SIZE(codes))
  1567. + i = 0;
  1568. +
  1569. + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
  1570. + (imx477->hflip->val ? 1 : 0);
  1571. +
  1572. + return codes[i];
  1573. +}
  1574. +
  1575. +static void imx477_set_default_format(struct imx477 *imx477)
  1576. +{
  1577. + /* Set default mode to max resolution */
  1578. + imx477->mode = &supported_modes_12bit[0];
  1579. + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
  1580. +}
  1581. +
  1582. +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1583. +{
  1584. + struct imx477 *imx477 = to_imx477(sd);
  1585. + struct v4l2_mbus_framefmt *try_fmt_img =
  1586. + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
  1587. + struct v4l2_mbus_framefmt *try_fmt_meta =
  1588. + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
  1589. + struct v4l2_rect *try_crop;
  1590. +
  1591. + mutex_lock(&imx477->mutex);
  1592. +
  1593. + /* Initialize try_fmt for the image pad */
  1594. + try_fmt_img->width = supported_modes_12bit[0].width;
  1595. + try_fmt_img->height = supported_modes_12bit[0].height;
  1596. + try_fmt_img->code = imx477_get_format_code(imx477,
  1597. + MEDIA_BUS_FMT_SRGGB12_1X12);
  1598. + try_fmt_img->field = V4L2_FIELD_NONE;
  1599. +
  1600. + /* Initialize try_fmt for the embedded metadata pad */
  1601. + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
  1602. + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
  1603. + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
  1604. + try_fmt_meta->field = V4L2_FIELD_NONE;
  1605. +
  1606. + /* Initialize try_crop */
  1607. + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
  1608. + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
  1609. + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
  1610. + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
  1611. + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
  1612. +
  1613. + mutex_unlock(&imx477->mutex);
  1614. +
  1615. + return 0;
  1616. +}
  1617. +
  1618. +static void imx477_adjust_exposure_range(struct imx477 *imx477)
  1619. +{
  1620. + int exposure_max, exposure_def;
  1621. +
  1622. + /* Honour the VBLANK limits when setting exposure. */
  1623. + exposure_max = imx477->mode->height + imx477->vblank->val -
  1624. + IMX477_EXPOSURE_OFFSET;
  1625. + exposure_def = min(exposure_max, imx477->exposure->val);
  1626. + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
  1627. + exposure_max, imx477->exposure->step,
  1628. + exposure_def);
  1629. +}
  1630. +
  1631. +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
  1632. +{
  1633. + int ret = 0;
  1634. +
  1635. + imx477->long_exp_shift = 0;
  1636. +
  1637. + while (val > IMX477_FRAME_LENGTH_MAX) {
  1638. + imx477->long_exp_shift++;
  1639. + val >>= 1;
  1640. + }
  1641. +
  1642. + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
  1643. + IMX477_REG_VALUE_16BIT, val);
  1644. + if (ret)
  1645. + return ret;
  1646. +
  1647. + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
  1648. + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
  1649. +}
  1650. +
  1651. +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
  1652. +{
  1653. + struct imx477 *imx477 =
  1654. + container_of(ctrl->handler, struct imx477, ctrl_handler);
  1655. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  1656. + int ret = 0;
  1657. +
  1658. + /*
  1659. + * The VBLANK control may change the limits of usable exposure, so check
  1660. + * and adjust if necessary.
  1661. + */
  1662. + if (ctrl->id == V4L2_CID_VBLANK)
  1663. + imx477_adjust_exposure_range(imx477);
  1664. +
  1665. + /*
  1666. + * Applying V4L2 control value only happens
  1667. + * when power is up for streaming
  1668. + */
  1669. + if (pm_runtime_get_if_in_use(&client->dev) == 0)
  1670. + return 0;
  1671. +
  1672. + switch (ctrl->id) {
  1673. + case V4L2_CID_ANALOGUE_GAIN:
  1674. + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
  1675. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1676. + break;
  1677. + case V4L2_CID_EXPOSURE:
  1678. + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
  1679. + IMX477_REG_VALUE_16BIT, ctrl->val >>
  1680. + imx477->long_exp_shift);
  1681. + break;
  1682. + case V4L2_CID_DIGITAL_GAIN:
  1683. + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
  1684. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1685. + break;
  1686. + case V4L2_CID_TEST_PATTERN:
  1687. + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
  1688. + IMX477_REG_VALUE_16BIT,
  1689. + imx477_test_pattern_val[ctrl->val]);
  1690. + break;
  1691. + case V4L2_CID_TEST_PATTERN_RED:
  1692. + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
  1693. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1694. + break;
  1695. + case V4L2_CID_TEST_PATTERN_GREENR:
  1696. + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
  1697. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1698. + break;
  1699. + case V4L2_CID_TEST_PATTERN_BLUE:
  1700. + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
  1701. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1702. + break;
  1703. + case V4L2_CID_TEST_PATTERN_GREENB:
  1704. + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
  1705. + IMX477_REG_VALUE_16BIT, ctrl->val);
  1706. + break;
  1707. + case V4L2_CID_HFLIP:
  1708. + case V4L2_CID_VFLIP:
  1709. + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
  1710. + imx477->hflip->val |
  1711. + imx477->vflip->val << 1);
  1712. + break;
  1713. + case V4L2_CID_VBLANK:
  1714. + ret = imx477_set_frame_length(imx477,
  1715. + imx477->mode->height + ctrl->val);
  1716. + break;
  1717. + case V4L2_CID_HBLANK:
  1718. + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
  1719. + imx477->mode->width + ctrl->val);
  1720. + break;
  1721. + default:
  1722. + dev_info(&client->dev,
  1723. + "ctrl(id:0x%x,val:0x%x) is not handled\n",
  1724. + ctrl->id, ctrl->val);
  1725. + ret = -EINVAL;
  1726. + break;
  1727. + }
  1728. +
  1729. + pm_runtime_put(&client->dev);
  1730. +
  1731. + return ret;
  1732. +}
  1733. +
  1734. +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
  1735. + .s_ctrl = imx477_set_ctrl,
  1736. +};
  1737. +
  1738. +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
  1739. + struct v4l2_subdev_pad_config *cfg,
  1740. + struct v4l2_subdev_mbus_code_enum *code)
  1741. +{
  1742. + struct imx477 *imx477 = to_imx477(sd);
  1743. +
  1744. + if (code->pad >= NUM_PADS)
  1745. + return -EINVAL;
  1746. +
  1747. + if (code->pad == IMAGE_PAD) {
  1748. + if (code->index >= (ARRAY_SIZE(codes) / 4))
  1749. + return -EINVAL;
  1750. +
  1751. + code->code = imx477_get_format_code(imx477,
  1752. + codes[code->index * 4]);
  1753. + } else {
  1754. + if (code->index > 0)
  1755. + return -EINVAL;
  1756. +
  1757. + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
  1758. + }
  1759. +
  1760. + return 0;
  1761. +}
  1762. +
  1763. +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
  1764. + struct v4l2_subdev_pad_config *cfg,
  1765. + struct v4l2_subdev_frame_size_enum *fse)
  1766. +{
  1767. + struct imx477 *imx477 = to_imx477(sd);
  1768. +
  1769. + if (fse->pad >= NUM_PADS)
  1770. + return -EINVAL;
  1771. +
  1772. + if (fse->pad == IMAGE_PAD) {
  1773. + const struct imx477_mode *mode_list;
  1774. + unsigned int num_modes;
  1775. +
  1776. + get_mode_table(fse->code, &mode_list, &num_modes);
  1777. +
  1778. + if (fse->index >= num_modes)
  1779. + return -EINVAL;
  1780. +
  1781. + if (fse->code != imx477_get_format_code(imx477, fse->code))
  1782. + return -EINVAL;
  1783. +
  1784. + fse->min_width = mode_list[fse->index].width;
  1785. + fse->max_width = fse->min_width;
  1786. + fse->min_height = mode_list[fse->index].height;
  1787. + fse->max_height = fse->min_height;
  1788. + } else {
  1789. + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
  1790. + return -EINVAL;
  1791. +
  1792. + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
  1793. + fse->max_width = fse->min_width;
  1794. + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
  1795. + fse->max_height = fse->min_height;
  1796. + }
  1797. +
  1798. + return 0;
  1799. +}
  1800. +
  1801. +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
  1802. +{
  1803. + fmt->colorspace = V4L2_COLORSPACE_RAW;
  1804. + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  1805. + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  1806. + fmt->colorspace,
  1807. + fmt->ycbcr_enc);
  1808. + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  1809. +}
  1810. +
  1811. +static void imx477_update_image_pad_format(struct imx477 *imx477,
  1812. + const struct imx477_mode *mode,
  1813. + struct v4l2_subdev_format *fmt)
  1814. +{
  1815. + fmt->format.width = mode->width;
  1816. + fmt->format.height = mode->height;
  1817. + fmt->format.field = V4L2_FIELD_NONE;
  1818. + imx477_reset_colorspace(&fmt->format);
  1819. +}
  1820. +
  1821. +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
  1822. +{
  1823. + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
  1824. + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
  1825. + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
  1826. + fmt->format.field = V4L2_FIELD_NONE;
  1827. +}
  1828. +
  1829. +static int imx477_get_pad_format(struct v4l2_subdev *sd,
  1830. + struct v4l2_subdev_pad_config *cfg,
  1831. + struct v4l2_subdev_format *fmt)
  1832. +{
  1833. + struct imx477 *imx477 = to_imx477(sd);
  1834. +
  1835. + if (fmt->pad >= NUM_PADS)
  1836. + return -EINVAL;
  1837. +
  1838. + mutex_lock(&imx477->mutex);
  1839. +
  1840. + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1841. + struct v4l2_mbus_framefmt *try_fmt =
  1842. + v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
  1843. + /* update the code which could change due to vflip or hflip: */
  1844. + try_fmt->code = fmt->pad == IMAGE_PAD ?
  1845. + imx477_get_format_code(imx477, try_fmt->code) :
  1846. + MEDIA_BUS_FMT_SENSOR_DATA;
  1847. + fmt->format = *try_fmt;
  1848. + } else {
  1849. + if (fmt->pad == IMAGE_PAD) {
  1850. + imx477_update_image_pad_format(imx477, imx477->mode,
  1851. + fmt);
  1852. + fmt->format.code =
  1853. + imx477_get_format_code(imx477, imx477->fmt_code);
  1854. + } else {
  1855. + imx477_update_metadata_pad_format(fmt);
  1856. + }
  1857. + }
  1858. +
  1859. + mutex_unlock(&imx477->mutex);
  1860. + return 0;
  1861. +}
  1862. +
  1863. +static
  1864. +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
  1865. + const struct v4l2_fract *timeperframe)
  1866. +{
  1867. + u64 frame_length;
  1868. +
  1869. + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
  1870. + do_div(frame_length,
  1871. + (u64)timeperframe->denominator * mode->line_length_pix);
  1872. +
  1873. + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
  1874. + frame_length = IMX477_FRAME_LENGTH_MAX;
  1875. +
  1876. + return max_t(unsigned int, frame_length, mode->height);
  1877. +}
  1878. +
  1879. +static void imx477_set_framing_limits(struct imx477 *imx477)
  1880. +{
  1881. + unsigned int frm_length_min, frm_length_default, hblank_min;
  1882. + const struct imx477_mode *mode = imx477->mode;
  1883. +
  1884. + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
  1885. + frm_length_default =
  1886. + imx477_get_frame_length(mode, &mode->timeperframe_default);
  1887. +
  1888. + /* Default to no long exposure multiplier. */
  1889. + imx477->long_exp_shift = 0;
  1890. +
  1891. + /* Update limits and set FPS to default */
  1892. + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
  1893. + ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
  1894. + IMX477_FRAME_LENGTH_MAX) - mode->height,
  1895. + 1, frm_length_default - mode->height);
  1896. +
  1897. + /* Setting this will adjust the exposure limits as well. */
  1898. + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
  1899. +
  1900. + hblank_min = mode->line_length_pix - mode->width;
  1901. + __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
  1902. + IMX477_LINE_LENGTH_MAX, 1, hblank_min);
  1903. + __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
  1904. +}
  1905. +
  1906. +static int imx477_set_pad_format(struct v4l2_subdev *sd,
  1907. + struct v4l2_subdev_pad_config *cfg,
  1908. + struct v4l2_subdev_format *fmt)
  1909. +{
  1910. + struct v4l2_mbus_framefmt *framefmt;
  1911. + const struct imx477_mode *mode;
  1912. + struct imx477 *imx477 = to_imx477(sd);
  1913. +
  1914. + if (fmt->pad >= NUM_PADS)
  1915. + return -EINVAL;
  1916. +
  1917. + mutex_lock(&imx477->mutex);
  1918. +
  1919. + if (fmt->pad == IMAGE_PAD) {
  1920. + const struct imx477_mode *mode_list;
  1921. + unsigned int num_modes;
  1922. +
  1923. + /* Bayer order varies with flips */
  1924. + fmt->format.code = imx477_get_format_code(imx477,
  1925. + fmt->format.code);
  1926. +
  1927. + get_mode_table(fmt->format.code, &mode_list, &num_modes);
  1928. +
  1929. + mode = v4l2_find_nearest_size(mode_list,
  1930. + num_modes,
  1931. + width, height,
  1932. + fmt->format.width,
  1933. + fmt->format.height);
  1934. + imx477_update_image_pad_format(imx477, mode, fmt);
  1935. + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1936. + framefmt = v4l2_subdev_get_try_format(sd, cfg,
  1937. + fmt->pad);
  1938. + *framefmt = fmt->format;
  1939. + } else if (imx477->mode != mode) {
  1940. + imx477->mode = mode;
  1941. + imx477->fmt_code = fmt->format.code;
  1942. + imx477_set_framing_limits(imx477);
  1943. + }
  1944. + } else {
  1945. + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1946. + framefmt = v4l2_subdev_get_try_format(sd, cfg,
  1947. + fmt->pad);
  1948. + *framefmt = fmt->format;
  1949. + } else {
  1950. + /* Only one embedded data mode is supported */
  1951. + imx477_update_metadata_pad_format(fmt);
  1952. + }
  1953. + }
  1954. +
  1955. + mutex_unlock(&imx477->mutex);
  1956. +
  1957. + return 0;
  1958. +}
  1959. +
  1960. +static const struct v4l2_rect *
  1961. +__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
  1962. + unsigned int pad, enum v4l2_subdev_format_whence which)
  1963. +{
  1964. + switch (which) {
  1965. + case V4L2_SUBDEV_FORMAT_TRY:
  1966. + return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
  1967. + case V4L2_SUBDEV_FORMAT_ACTIVE:
  1968. + return &imx477->mode->crop;
  1969. + }
  1970. +
  1971. + return NULL;
  1972. +}
  1973. +
  1974. +static int imx477_get_selection(struct v4l2_subdev *sd,
  1975. + struct v4l2_subdev_pad_config *cfg,
  1976. + struct v4l2_subdev_selection *sel)
  1977. +{
  1978. + switch (sel->target) {
  1979. + case V4L2_SEL_TGT_CROP: {
  1980. + struct imx477 *imx477 = to_imx477(sd);
  1981. +
  1982. + mutex_lock(&imx477->mutex);
  1983. + sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
  1984. + sel->which);
  1985. + mutex_unlock(&imx477->mutex);
  1986. +
  1987. + return 0;
  1988. + }
  1989. +
  1990. + case V4L2_SEL_TGT_NATIVE_SIZE:
  1991. + sel->r.left = 0;
  1992. + sel->r.top = 0;
  1993. + sel->r.width = IMX477_NATIVE_WIDTH;
  1994. + sel->r.height = IMX477_NATIVE_HEIGHT;
  1995. +
  1996. + return 0;
  1997. +
  1998. + case V4L2_SEL_TGT_CROP_DEFAULT:
  1999. + case V4L2_SEL_TGT_CROP_BOUNDS:
  2000. + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
  2001. + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
  2002. + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
  2003. + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
  2004. +
  2005. + return 0;
  2006. + }
  2007. +
  2008. + return -EINVAL;
  2009. +}
  2010. +
  2011. +/* Start streaming */
  2012. +static int imx477_start_streaming(struct imx477 *imx477)
  2013. +{
  2014. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  2015. + const struct imx477_reg_list *reg_list;
  2016. + const struct imx477_reg_list *extra_regs;
  2017. + int ret;
  2018. +
  2019. + if (!imx477->common_regs_written) {
  2020. + ret = imx477_write_regs(imx477, mode_common_regs,
  2021. + ARRAY_SIZE(mode_common_regs));
  2022. + if (!ret) {
  2023. + extra_regs = &imx477->compatible_data->extra_regs;
  2024. + ret = imx477_write_regs(imx477, extra_regs->regs,
  2025. + extra_regs->num_of_regs);
  2026. + }
  2027. +
  2028. + if (ret) {
  2029. + dev_err(&client->dev, "%s failed to set common settings\n",
  2030. + __func__);
  2031. + return ret;
  2032. + }
  2033. + imx477->common_regs_written = true;
  2034. + }
  2035. +
  2036. + /* Apply default values of current mode */
  2037. + reg_list = &imx477->mode->reg_list;
  2038. + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
  2039. + if (ret) {
  2040. + dev_err(&client->dev, "%s failed to set mode\n", __func__);
  2041. + return ret;
  2042. + }
  2043. +
  2044. + /* Set on-sensor DPC. */
  2045. + imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
  2046. + imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
  2047. +
  2048. + /* Set vsync trigger mode */
  2049. + if (trigger_mode != 0) {
  2050. + /* trigger_mode == 1 for source, 2 for sink */
  2051. + const u32 val = (trigger_mode == 1) ? 1 : 0;
  2052. +
  2053. + imx477_write_reg(imx477, IMX477_REG_MC_MODE,
  2054. + IMX477_REG_VALUE_08BIT, 1);
  2055. + imx477_write_reg(imx477, IMX477_REG_MS_SEL,
  2056. + IMX477_REG_VALUE_08BIT, val);
  2057. + imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
  2058. + IMX477_REG_VALUE_08BIT, val);
  2059. + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
  2060. + IMX477_REG_VALUE_08BIT, val);
  2061. + }
  2062. +
  2063. + /* Apply customized values from user */
  2064. + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
  2065. + if (ret)
  2066. + return ret;
  2067. +
  2068. + /* set stream on register */
  2069. + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
  2070. + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
  2071. +}
  2072. +
  2073. +/* Stop streaming */
  2074. +static void imx477_stop_streaming(struct imx477 *imx477)
  2075. +{
  2076. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  2077. + int ret;
  2078. +
  2079. + /* set stream off register */
  2080. + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
  2081. + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
  2082. + if (ret)
  2083. + dev_err(&client->dev, "%s failed to set stream\n", __func__);
  2084. +}
  2085. +
  2086. +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
  2087. +{
  2088. + struct imx477 *imx477 = to_imx477(sd);
  2089. + struct i2c_client *client = v4l2_get_subdevdata(sd);
  2090. + int ret = 0;
  2091. +
  2092. + mutex_lock(&imx477->mutex);
  2093. + if (imx477->streaming == enable) {
  2094. + mutex_unlock(&imx477->mutex);
  2095. + return 0;
  2096. + }
  2097. +
  2098. + if (enable) {
  2099. + ret = pm_runtime_get_sync(&client->dev);
  2100. + if (ret < 0) {
  2101. + pm_runtime_put_noidle(&client->dev);
  2102. + goto err_unlock;
  2103. + }
  2104. +
  2105. + /*
  2106. + * Apply default & customized values
  2107. + * and then start streaming.
  2108. + */
  2109. + ret = imx477_start_streaming(imx477);
  2110. + if (ret)
  2111. + goto err_rpm_put;
  2112. + } else {
  2113. + imx477_stop_streaming(imx477);
  2114. + pm_runtime_put(&client->dev);
  2115. + }
  2116. +
  2117. + imx477->streaming = enable;
  2118. +
  2119. + /* vflip and hflip cannot change during streaming */
  2120. + __v4l2_ctrl_grab(imx477->vflip, enable);
  2121. + __v4l2_ctrl_grab(imx477->hflip, enable);
  2122. +
  2123. + mutex_unlock(&imx477->mutex);
  2124. +
  2125. + return ret;
  2126. +
  2127. +err_rpm_put:
  2128. + pm_runtime_put(&client->dev);
  2129. +err_unlock:
  2130. + mutex_unlock(&imx477->mutex);
  2131. +
  2132. + return ret;
  2133. +}
  2134. +
  2135. +/* Power/clock management functions */
  2136. +static int imx477_power_on(struct device *dev)
  2137. +{
  2138. + struct i2c_client *client = to_i2c_client(dev);
  2139. + struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2140. + struct imx477 *imx477 = to_imx477(sd);
  2141. + int ret;
  2142. +
  2143. + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
  2144. + imx477->supplies);
  2145. + if (ret) {
  2146. + dev_err(&client->dev, "%s: failed to enable regulators\n",
  2147. + __func__);
  2148. + return ret;
  2149. + }
  2150. +
  2151. + ret = clk_prepare_enable(imx477->xclk);
  2152. + if (ret) {
  2153. + dev_err(&client->dev, "%s: failed to enable clock\n",
  2154. + __func__);
  2155. + goto reg_off;
  2156. + }
  2157. +
  2158. + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
  2159. + usleep_range(IMX477_XCLR_MIN_DELAY_US,
  2160. + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
  2161. +
  2162. + return 0;
  2163. +
  2164. +reg_off:
  2165. + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
  2166. + return ret;
  2167. +}
  2168. +
  2169. +static int imx477_power_off(struct device *dev)
  2170. +{
  2171. + struct i2c_client *client = to_i2c_client(dev);
  2172. + struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2173. + struct imx477 *imx477 = to_imx477(sd);
  2174. +
  2175. + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
  2176. + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
  2177. + clk_disable_unprepare(imx477->xclk);
  2178. +
  2179. + /* Force reprogramming of the common registers when powered up again. */
  2180. + imx477->common_regs_written = false;
  2181. +
  2182. + return 0;
  2183. +}
  2184. +
  2185. +static int __maybe_unused imx477_suspend(struct device *dev)
  2186. +{
  2187. + struct i2c_client *client = to_i2c_client(dev);
  2188. + struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2189. + struct imx477 *imx477 = to_imx477(sd);
  2190. +
  2191. + if (imx477->streaming)
  2192. + imx477_stop_streaming(imx477);
  2193. +
  2194. + return 0;
  2195. +}
  2196. +
  2197. +static int __maybe_unused imx477_resume(struct device *dev)
  2198. +{
  2199. + struct i2c_client *client = to_i2c_client(dev);
  2200. + struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2201. + struct imx477 *imx477 = to_imx477(sd);
  2202. + int ret;
  2203. +
  2204. + if (imx477->streaming) {
  2205. + ret = imx477_start_streaming(imx477);
  2206. + if (ret)
  2207. + goto error;
  2208. + }
  2209. +
  2210. + return 0;
  2211. +
  2212. +error:
  2213. + imx477_stop_streaming(imx477);
  2214. + imx477->streaming = 0;
  2215. + return ret;
  2216. +}
  2217. +
  2218. +static int imx477_get_regulators(struct imx477 *imx477)
  2219. +{
  2220. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  2221. + unsigned int i;
  2222. +
  2223. + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
  2224. + imx477->supplies[i].supply = imx477_supply_name[i];
  2225. +
  2226. + return devm_regulator_bulk_get(&client->dev,
  2227. + IMX477_NUM_SUPPLIES,
  2228. + imx477->supplies);
  2229. +}
  2230. +
  2231. +/* Verify chip ID */
  2232. +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
  2233. +{
  2234. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  2235. + int ret;
  2236. + u32 val;
  2237. +
  2238. + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
  2239. + IMX477_REG_VALUE_16BIT, &val);
  2240. + if (ret) {
  2241. + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
  2242. + expected_id, ret);
  2243. + return ret;
  2244. + }
  2245. +
  2246. + if (val != expected_id) {
  2247. + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
  2248. + expected_id, val);
  2249. + return -EIO;
  2250. + }
  2251. +
  2252. + dev_info(&client->dev, "Device found is imx%x\n", val);
  2253. +
  2254. + return 0;
  2255. +}
  2256. +
  2257. +static const struct v4l2_subdev_core_ops imx477_core_ops = {
  2258. + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  2259. + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  2260. +};
  2261. +
  2262. +static const struct v4l2_subdev_video_ops imx477_video_ops = {
  2263. + .s_stream = imx477_set_stream,
  2264. +};
  2265. +
  2266. +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
  2267. + .enum_mbus_code = imx477_enum_mbus_code,
  2268. + .get_fmt = imx477_get_pad_format,
  2269. + .set_fmt = imx477_set_pad_format,
  2270. + .get_selection = imx477_get_selection,
  2271. + .enum_frame_size = imx477_enum_frame_size,
  2272. +};
  2273. +
  2274. +static const struct v4l2_subdev_ops imx477_subdev_ops = {
  2275. + .core = &imx477_core_ops,
  2276. + .video = &imx477_video_ops,
  2277. + .pad = &imx477_pad_ops,
  2278. +};
  2279. +
  2280. +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
  2281. + .open = imx477_open,
  2282. +};
  2283. +
  2284. +/* Initialize control handlers */
  2285. +static int imx477_init_controls(struct imx477 *imx477)
  2286. +{
  2287. + struct v4l2_ctrl_handler *ctrl_hdlr;
  2288. + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
  2289. + struct v4l2_fwnode_device_properties props;
  2290. + unsigned int i;
  2291. + int ret;
  2292. +
  2293. + ctrl_hdlr = &imx477->ctrl_handler;
  2294. + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
  2295. + if (ret)
  2296. + return ret;
  2297. +
  2298. + mutex_init(&imx477->mutex);
  2299. + ctrl_hdlr->lock = &imx477->mutex;
  2300. +
  2301. + /* By default, PIXEL_RATE is read only */
  2302. + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2303. + V4L2_CID_PIXEL_RATE,
  2304. + IMX477_PIXEL_RATE,
  2305. + IMX477_PIXEL_RATE, 1,
  2306. + IMX477_PIXEL_RATE);
  2307. +
  2308. + /*
  2309. + * Create the controls here, but mode specific limits are setup
  2310. + * in the imx477_set_framing_limits() call below.
  2311. + */
  2312. + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2313. + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
  2314. + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2315. + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
  2316. +
  2317. + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2318. + V4L2_CID_EXPOSURE,
  2319. + IMX477_EXPOSURE_MIN,
  2320. + IMX477_EXPOSURE_MAX,
  2321. + IMX477_EXPOSURE_STEP,
  2322. + IMX477_EXPOSURE_DEFAULT);
  2323. +
  2324. + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  2325. + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
  2326. + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
  2327. +
  2328. + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  2329. + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
  2330. + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
  2331. +
  2332. + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2333. + V4L2_CID_HFLIP, 0, 1, 1, 0);
  2334. + if (imx477->hflip)
  2335. + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  2336. +
  2337. + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2338. + V4L2_CID_VFLIP, 0, 1, 1, 0);
  2339. + if (imx477->vflip)
  2340. + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  2341. +
  2342. + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
  2343. + V4L2_CID_TEST_PATTERN,
  2344. + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
  2345. + 0, 0, imx477_test_pattern_menu);
  2346. + for (i = 0; i < 4; i++) {
  2347. + /*
  2348. + * The assumption is that
  2349. + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
  2350. + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
  2351. + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
  2352. + */
  2353. + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
  2354. + V4L2_CID_TEST_PATTERN_RED + i,
  2355. + IMX477_TEST_PATTERN_COLOUR_MIN,
  2356. + IMX477_TEST_PATTERN_COLOUR_MAX,
  2357. + IMX477_TEST_PATTERN_COLOUR_STEP,
  2358. + IMX477_TEST_PATTERN_COLOUR_MAX);
  2359. + /* The "Solid color" pattern is white by default */
  2360. + }
  2361. +
  2362. + if (ctrl_hdlr->error) {
  2363. + ret = ctrl_hdlr->error;
  2364. + dev_err(&client->dev, "%s control init failed (%d)\n",
  2365. + __func__, ret);
  2366. + goto error;
  2367. + }
  2368. +
  2369. + ret = v4l2_fwnode_device_parse(&client->dev, &props);
  2370. + if (ret)
  2371. + goto error;
  2372. +
  2373. + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
  2374. + &props);
  2375. + if (ret)
  2376. + goto error;
  2377. +
  2378. + imx477->sd.ctrl_handler = ctrl_hdlr;
  2379. +
  2380. + /* Setup exposure and frame/line length limits. */
  2381. + imx477_set_framing_limits(imx477);
  2382. +
  2383. + return 0;
  2384. +
  2385. +error:
  2386. + v4l2_ctrl_handler_free(ctrl_hdlr);
  2387. + mutex_destroy(&imx477->mutex);
  2388. +
  2389. + return ret;
  2390. +}
  2391. +
  2392. +static void imx477_free_controls(struct imx477 *imx477)
  2393. +{
  2394. + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
  2395. + mutex_destroy(&imx477->mutex);
  2396. +}
  2397. +
  2398. +static int imx477_check_hwcfg(struct device *dev)
  2399. +{
  2400. + struct fwnode_handle *endpoint;
  2401. + struct v4l2_fwnode_endpoint ep_cfg = {
  2402. + .bus_type = V4L2_MBUS_CSI2_DPHY
  2403. + };
  2404. + int ret = -EINVAL;
  2405. +
  2406. + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  2407. + if (!endpoint) {
  2408. + dev_err(dev, "endpoint node not found\n");
  2409. + return -EINVAL;
  2410. + }
  2411. +
  2412. + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
  2413. + dev_err(dev, "could not parse endpoint\n");
  2414. + goto error_out;
  2415. + }
  2416. +
  2417. + /* Check the number of MIPI CSI2 data lanes */
  2418. + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
  2419. + dev_err(dev, "only 2 data lanes are currently supported\n");
  2420. + goto error_out;
  2421. + }
  2422. +
  2423. + /* Check the link frequency set in device tree */
  2424. + if (!ep_cfg.nr_of_link_frequencies) {
  2425. + dev_err(dev, "link-frequency property not found in DT\n");
  2426. + goto error_out;
  2427. + }
  2428. +
  2429. + if (ep_cfg.nr_of_link_frequencies != 1 ||
  2430. + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
  2431. + dev_err(dev, "Link frequency not supported: %lld\n",
  2432. + ep_cfg.link_frequencies[0]);
  2433. + goto error_out;
  2434. + }
  2435. +
  2436. + ret = 0;
  2437. +
  2438. +error_out:
  2439. + v4l2_fwnode_endpoint_free(&ep_cfg);
  2440. + fwnode_handle_put(endpoint);
  2441. +
  2442. + return ret;
  2443. +}
  2444. +
  2445. +static const struct imx477_compatible_data imx477_compatible = {
  2446. + .chip_id = IMX477_CHIP_ID,
  2447. + .extra_regs = {
  2448. + .num_of_regs = 0,
  2449. + .regs = NULL
  2450. + }
  2451. +};
  2452. +
  2453. +static const struct imx477_reg imx378_regs[] = {
  2454. + {0x3e35, 0x01},
  2455. + {0x4421, 0x08},
  2456. + {0x3ff9, 0x00},
  2457. +};
  2458. +
  2459. +static const struct imx477_compatible_data imx378_compatible = {
  2460. + .chip_id = IMX378_CHIP_ID,
  2461. + .extra_regs = {
  2462. + .num_of_regs = ARRAY_SIZE(imx378_regs),
  2463. + .regs = imx378_regs
  2464. + }
  2465. +};
  2466. +
  2467. +static const struct of_device_id imx477_dt_ids[] = {
  2468. + { .compatible = "sony,imx477", .data = &imx477_compatible },
  2469. + { .compatible = "sony,imx378", .data = &imx378_compatible },
  2470. + { /* sentinel */ }
  2471. +};
  2472. +
  2473. +static int imx477_probe(struct i2c_client *client)
  2474. +{
  2475. + struct device *dev = &client->dev;
  2476. + struct imx477 *imx477;
  2477. + const struct of_device_id *match;
  2478. + int ret;
  2479. +
  2480. + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
  2481. + if (!imx477)
  2482. + return -ENOMEM;
  2483. +
  2484. + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
  2485. +
  2486. + match = of_match_device(imx477_dt_ids, dev);
  2487. + if (!match)
  2488. + return -ENODEV;
  2489. + imx477->compatible_data =
  2490. + (const struct imx477_compatible_data *)match->data;
  2491. +
  2492. + /* Check the hardware configuration in device tree */
  2493. + if (imx477_check_hwcfg(dev))
  2494. + return -EINVAL;
  2495. +
  2496. + /* Get system clock (xclk) */
  2497. + imx477->xclk = devm_clk_get(dev, NULL);
  2498. + if (IS_ERR(imx477->xclk)) {
  2499. + dev_err(dev, "failed to get xclk\n");
  2500. + return PTR_ERR(imx477->xclk);
  2501. + }
  2502. +
  2503. + imx477->xclk_freq = clk_get_rate(imx477->xclk);
  2504. + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
  2505. + dev_err(dev, "xclk frequency not supported: %d Hz\n",
  2506. + imx477->xclk_freq);
  2507. + return -EINVAL;
  2508. + }
  2509. +
  2510. + ret = imx477_get_regulators(imx477);
  2511. + if (ret) {
  2512. + dev_err(dev, "failed to get regulators\n");
  2513. + return ret;
  2514. + }
  2515. +
  2516. + /* Request optional enable pin */
  2517. + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  2518. + GPIOD_OUT_HIGH);
  2519. +
  2520. + /*
  2521. + * The sensor must be powered for imx477_identify_module()
  2522. + * to be able to read the CHIP_ID register
  2523. + */
  2524. + ret = imx477_power_on(dev);
  2525. + if (ret)
  2526. + return ret;
  2527. +
  2528. + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
  2529. + if (ret)
  2530. + goto error_power_off;
  2531. +
  2532. + /* Initialize default format */
  2533. + imx477_set_default_format(imx477);
  2534. +
  2535. + /* Enable runtime PM and turn off the device */
  2536. + pm_runtime_set_active(dev);
  2537. + pm_runtime_enable(dev);
  2538. + pm_runtime_idle(dev);
  2539. +
  2540. + /* This needs the pm runtime to be registered. */
  2541. + ret = imx477_init_controls(imx477);
  2542. + if (ret)
  2543. + goto error_power_off;
  2544. +
  2545. + /* Initialize subdev */
  2546. + imx477->sd.internal_ops = &imx477_internal_ops;
  2547. + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  2548. + V4L2_SUBDEV_FL_HAS_EVENTS;
  2549. + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  2550. +
  2551. + /* Initialize source pads */
  2552. + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
  2553. + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
  2554. +
  2555. + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
  2556. + if (ret) {
  2557. + dev_err(dev, "failed to init entity pads: %d\n", ret);
  2558. + goto error_handler_free;
  2559. + }
  2560. +
  2561. + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
  2562. + if (ret < 0) {
  2563. + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
  2564. + goto error_media_entity;
  2565. + }
  2566. +
  2567. + return 0;
  2568. +
  2569. +error_media_entity:
  2570. + media_entity_cleanup(&imx477->sd.entity);
  2571. +
  2572. +error_handler_free:
  2573. + imx477_free_controls(imx477);
  2574. +
  2575. +error_power_off:
  2576. + pm_runtime_disable(&client->dev);
  2577. + pm_runtime_set_suspended(&client->dev);
  2578. + imx477_power_off(&client->dev);
  2579. +
  2580. + return ret;
  2581. +}
  2582. +
  2583. +static void imx477_remove(struct i2c_client *client)
  2584. +{
  2585. + struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2586. + struct imx477 *imx477 = to_imx477(sd);
  2587. +
  2588. + v4l2_async_unregister_subdev(sd);
  2589. + media_entity_cleanup(&sd->entity);
  2590. + imx477_free_controls(imx477);
  2591. +
  2592. + pm_runtime_disable(&client->dev);
  2593. + if (!pm_runtime_status_suspended(&client->dev))
  2594. + imx477_power_off(&client->dev);
  2595. + pm_runtime_set_suspended(&client->dev);
  2596. +}
  2597. +
  2598. +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
  2599. +
  2600. +static const struct dev_pm_ops imx477_pm_ops = {
  2601. + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
  2602. + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
  2603. +};
  2604. +
  2605. +static struct i2c_driver imx477_i2c_driver = {
  2606. + .driver = {
  2607. + .name = "imx477",
  2608. + .of_match_table = imx477_dt_ids,
  2609. + .pm = &imx477_pm_ops,
  2610. + },
  2611. + .probe_new = imx477_probe,
  2612. + .remove = imx477_remove,
  2613. +};
  2614. +
  2615. +module_i2c_driver(imx477_i2c_driver);
  2616. +
  2617. +MODULE_AUTHOR("Naushir Patuck <[email protected]>");
  2618. +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
  2619. +MODULE_LICENSE("GPL v2");