1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701 |
- From 227eddefe9cb07dd1d160f0015b0d17d66cd3cf8 Mon Sep 17 00:00:00 2001
- From: Naushir Patuck <[email protected]>
- Date: Thu, 7 May 2020 15:50:54 +0100
- Subject: [PATCH] media: i2c: imx477: Support for the Sony IMX477
- sensor
- dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
- Add YAML device tree binding for IMX477 CMOS image sensor.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: Add driver for Sony IMX477 sensor
- Adds a driver for the 12MPix Sony IMX477 CSI2 sensor.
- Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
- currently only supports 2 lanes.
- The following Bayer modes are currently available:
- 4056x3040 12-bit @ 10fps
- 2028x1520 12-bit (binned) @ 40fps
- 2028x1050 12-bit (cropped/binned) @ 50fps
- 1012x760 10-bit (scaled) @ 120 fps
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Add support for adaptive frame control
- Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
- automatically adjust the sensor frame length based on exposure time,
- allowing variable frame rates and longer exposures.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Return correct result on sensor id verification
- The test should return -EIO if the register read id does not match
- the expected sensor id.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Parse and register properties
- Parse device properties and register controls for them using the V4L2
- fwnode properties helpers.
- Signed-off-by: Laurent Pinchart <[email protected]>
- media: i2c: imx477: Selection compliance fixes
- To comply with the intended usage of the V4L2 selection target when
- used to retrieve a sensor image properties, adjust the rectangles
- returned by the imx477 driver.
- The top/left crop coordinates of the TGT_CROP rectangle were set to
- (0, 0) instead of (8, 16) which is the offset from the larger physical
- pixel array rectangle. This was also a mismatch with the default values
- crop rectangle value, so this is corrected. Found with v4l2-compliance.
- While at it, add V4L2_SEL_TGT_CROP_BOUNDS support: CROP_DEFAULT and
- CROP_BOUNDS have the same size as the non-active pixels are not readable
- using the selection API. Found with v4l2-compliance.
- This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
- the imx219 sensor.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Remove auto frame length adjusting
- The V4L2_CID_EXPOSURE_AUTO_PRIORITY was used to let the sensor control
- frame length (effectively framerate) based on the requested exposure
- time requested. Remove this feature as it is never used, and goes
- against how V4L2 likes to handle exposure and vblank controls.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Add very long exposure control to the driver
- Add support for very long exposures by using the exposure multiplier
- register. Userland does not need to pass any additional controls to
- enable long exposures, it simply requests a larger vblank to extend the
- exposure control range appropriately.
- Currently, since hblank is fixed, a maximum of approximately 124 seconds
- of exposure time can be used. In a future change, hblank could also be
- controlled in userland to give over 200 seconds of exposure time.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Fix crop height for 2028x1080 mode
- The crop height for this mode was set at 2600 lines, it should be 2160
- lines instead.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Replace existing 1012x760 mode
- The existing 1012x760 120 fps mode has significant IQ problem using
- the internal sensor scaler. Replace this mode with a 1332x990 120 fps
- mode instead. This new mode has a smaller field of view, but does not
- suffer from the bad IQ of the original mode.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
- The only field in this struct that is used is the format code, so
- replace the struct with this single field.
- Save the format code in imx477_set_pad_format() when setting up a new
- mode so that imx477_get_pad_format() performs the right lookup.
- Otherwise, this caused a bug where the mode lookup occurred on the
- 12-bit table rather than the 10-bit table.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Remove unused function parameter
- The struct imx477 *ctrl parameter is not used in the function
- imx477_adjust_exposure_range(), so remove it.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Fix for long exposure limit calculations
- Do not scale IMX477_EXPOSURE_OFFSET with the long exposure factor during
- the limit calculations. This allows larger exposure times, and does seem to be
- what the sensor is doing internally.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Extend driver to support imx378 sensor
- The imx378 sensor is almost identical to the imx477 and can be
- supported as a "compatible" sensor with just a few extra register
- writes.
- Signed-off-by: David Plowman <[email protected]>
- media: i2c: imx477: Fix framerates for 1332x990 mode
- The imx477 driver's line length for this mode had not been updated to
- the value supplied to us by the sensor manufacturer. With this
- correction the sensor delivers the framerates that are expected.
- Signed-off-by: David Plowman <[email protected]>
- media: i2c: imx477: Allow control of on-sensor DPC
- A module parameter "dpc_enable" is added to allow the control of the
- sensor's on-board DPC (Defective Pixel Correction) function.
- This is a global setting to be configured before using the sensor;
- there is no intention that this would ever be changed on-the-fly.
- Signed-off-by: David Plowman <[email protected]>
- media: i2c: imx477: Sensor should report RAW color space
- Tested on Raspberry Pi running libcamera.
- Signed-off-by: David Plowman <[email protected]>
- media: i2c: imx477: Add vsync trigger_mode parameter
- trigger_mode == 0 (default) => no effect / no registers written
- trigger_mode == 1 => source
- trigger_mode == 2 => sink
- This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
- Signed-off-by: Jonas Jacob <[email protected]>
- media: i2c: Update imx477 Kconfig entry
- Bring the IMX477 Kconfig declaration in line with upstream entries.
- Signed-off-by: Phil Elwell <[email protected]>
- media: i2c: imx477: Correct minimum exposure lines
- The minimum number of exposure lines value (IMX477_EXPOSURE_MIN) was
- previously 20 but this is not correct. The datasheet is not completely
- explicit, however the new value of 4 has been tested with all the
- sensor modes supported by this driver, and matches the lowest exposure
- value of 114us that could be achieved wtih Raspberry Pi's legacy
- firmware driver.
- Signed-off-by: David Plowman <[email protected]>
- media: i2c: imx477: Allow dynamic horizontal blanking control
- Currently, the V4L2_CID_HBLANK control is marked as read-only. Remove this
- restriction and allow userland to modify the control if needed.
- Set the maximum limit of the line length to 0xfff0.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Reset hblank on mode switch
- Reset the hblank control to the minimum value on every mode switch. This is to
- account for userland instances that do not yet control hblank, otherwise it
- gets set to a non-optimal value.
- Signed-off-by: Naushir Patuck <[email protected]>
- media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
- On a mode change, only call imx477_set_framing_limits() to adjust the hblank
- and vblank limits if the new mode is different from the existing mode. This
- preserves any manual control values the user might have set.
- Signed-off-by: Naushir Patuck <[email protected]>
- ---
- .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
- MAINTAINERS | 8 +
- drivers/media/i2c/Kconfig | 11 +
- drivers/media/i2c/Makefile | 1 +
- drivers/media/i2c/imx477.c | 2308 +++++++++++++++++
- 5 files changed, 2441 insertions(+)
- create mode 100644 Documentation/devicetree/bindings/media/i2c/imx477.yaml
- create mode 100644 drivers/media/i2c/imx477.c
- --- /dev/null
- +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
- @@ -0,0 +1,113 @@
- +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
- +%YAML 1.2
- +---
- +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
- +$schema: http://devicetree.org/meta-schemas/core.yaml#
- +
- +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
- +
- +maintainers:
- + - Naushir Patuck <[email protected]>
- +
- +description: |-
- + The Sony IMX477 is a 1/2.3-inch CMOS active pixel digital image sensor
- + with an active array size of 4056H x 3040V. It is programmable through
- + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
- + Image data is sent through MIPI CSI-2, which is configured as either 2 or
- + 4 data lanes.
- +
- +properties:
- + compatible:
- + const: sony,imx477
- +
- + reg:
- + description: I2C device address
- + maxItems: 1
- +
- + clocks:
- + maxItems: 1
- +
- + VDIG-supply:
- + description:
- + Digital I/O voltage supply, 1.05 volts
- +
- + VANA-supply:
- + description:
- + Analog voltage supply, 2.8 volts
- +
- + VDDL-supply:
- + description:
- + Digital core voltage supply, 1.8 volts
- +
- + reset-gpios:
- + description: |-
- + Reference to the GPIO connected to the xclr pin, if any.
- + Must be released (set high) after all all supplies and INCK are applied.
- +
- + # See ../video-interfaces.txt for more details
- + port:
- + type: object
- + properties:
- + endpoint:
- + type: object
- + properties:
- + data-lanes:
- + description: |-
- + The sensor supports either two-lane, or four-lane operation.
- + For two-lane operation the property must be set to <1 2>.
- + items:
- + - const: 1
- + - const: 2
- +
- + clock-noncontinuous:
- + type: boolean
- + description: |-
- + MIPI CSI-2 clock is non-continuous if this property is present,
- + otherwise it's continuous.
- +
- + link-frequencies:
- + allOf:
- + - $ref: /schemas/types.yaml#/definitions/uint64-array
- + description:
- + Allowed data bus frequencies.
- +
- + required:
- + - link-frequencies
- +
- +required:
- + - compatible
- + - reg
- + - clocks
- + - VANA-supply
- + - VDIG-supply
- + - VDDL-supply
- + - port
- +
- +additionalProperties: false
- +
- +examples:
- + - |
- + i2c0 {
- + #address-cells = <1>;
- + #size-cells = <0>;
- +
- + imx477: sensor@10 {
- + compatible = "sony,imx477";
- + reg = <0x1a>;
- + clocks = <&imx477_clk>;
- + VANA-supply = <&imx477_vana>; /* 2.8v */
- + VDIG-supply = <&imx477_vdig>; /* 1.05v */
- + VDDL-supply = <&imx477_vddl>; /* 1.8v */
- +
- + port {
- + imx477_0: endpoint {
- + remote-endpoint = <&csi1_ep>;
- + data-lanes = <1 2>;
- + clock-noncontinuous;
- + link-frequencies = /bits/ 64 <450000000>;
- + };
- + };
- + };
- + };
- +
- +...
- --- a/MAINTAINERS
- +++ b/MAINTAINERS
- @@ -19279,6 +19279,14 @@ T: git git://linuxtv.org/media_tree.git
- F: Documentation/devicetree/bindings/media/i2c/sony,imx412.yaml
- F: drivers/media/i2c/imx412.c
-
- +SONY IMX477 SENSOR DRIVER
- +M: Raspberry Pi Kernel Maintenance <[email protected]>
- +L: [email protected]
- +S: Maintained
- +T: git git://linuxtv.org/media_tree.git
- +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
- +F: drivers/media/i2c/imx477.c
- +
- SONY MEMORYSTICK SUBSYSTEM
- M: Maxim Levitsky <[email protected]>
- M: Alex Dubov <[email protected]>
- --- a/drivers/media/i2c/Kconfig
- +++ b/drivers/media/i2c/Kconfig
- @@ -1030,6 +1030,17 @@ config VIDEO_UDA1342
- To compile this driver as a module, choose M here: the
- module will be called uda1342.
-
- +config VIDEO_IMX477
- + tristate "Sony IMX477 sensor support"
- + depends on I2C && VIDEO_DEV
- + select VIDEO_V4L2_SUBDEV_API
- + help
- + This is a Video4Linux2 sensor driver for the Sony
- + IMX477 camera. Also supports the Sony IMX378.
- +
- + To compile this driver as a module, choose M here: the
- + module will be called imx477.
- +
- config VIDEO_VP27SMPX
- tristate "Panasonic VP27's internal MPX"
- depends on VIDEO_DEV && I2C
- --- a/drivers/media/i2c/Makefile
- +++ b/drivers/media/i2c/Makefile
- @@ -48,6 +48,7 @@ obj-$(CONFIG_VIDEO_IMX334) += imx334.o
- obj-$(CONFIG_VIDEO_IMX335) += imx335.o
- obj-$(CONFIG_VIDEO_IMX355) += imx355.o
- obj-$(CONFIG_VIDEO_IMX412) += imx412.o
- +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
- obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
- obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
- obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
- --- /dev/null
- +++ b/drivers/media/i2c/imx477.c
- @@ -0,0 +1,2308 @@
- +// SPDX-License-Identifier: GPL-2.0
- +/*
- + * A V4L2 driver for Sony IMX477 cameras.
- + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
- + *
- + * Based on Sony imx219 camera driver
- + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
- + */
- +#include <asm/unaligned.h>
- +#include <linux/clk.h>
- +#include <linux/delay.h>
- +#include <linux/gpio/consumer.h>
- +#include <linux/i2c.h>
- +#include <linux/module.h>
- +#include <linux/of_device.h>
- +#include <linux/pm_runtime.h>
- +#include <linux/regulator/consumer.h>
- +#include <media/v4l2-ctrls.h>
- +#include <media/v4l2-device.h>
- +#include <media/v4l2-event.h>
- +#include <media/v4l2-fwnode.h>
- +#include <media/v4l2-mediabus.h>
- +
- +static int dpc_enable = 1;
- +module_param(dpc_enable, int, 0644);
- +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
- +
- +static int trigger_mode;
- +module_param(trigger_mode, int, 0644);
- +MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
- +
- +#define IMX477_REG_VALUE_08BIT 1
- +#define IMX477_REG_VALUE_16BIT 2
- +
- +/* Chip ID */
- +#define IMX477_REG_CHIP_ID 0x0016
- +#define IMX477_CHIP_ID 0x0477
- +#define IMX378_CHIP_ID 0x0378
- +
- +#define IMX477_REG_MODE_SELECT 0x0100
- +#define IMX477_MODE_STANDBY 0x00
- +#define IMX477_MODE_STREAMING 0x01
- +
- +#define IMX477_REG_ORIENTATION 0x101
- +
- +#define IMX477_XCLK_FREQ 24000000
- +
- +#define IMX477_DEFAULT_LINK_FREQ 450000000
- +
- +/* Pixel rate is fixed at 840MHz for all the modes */
- +#define IMX477_PIXEL_RATE 840000000
- +
- +/* V_TIMING internal */
- +#define IMX477_REG_FRAME_LENGTH 0x0340
- +#define IMX477_FRAME_LENGTH_MAX 0xffdc
- +
- +/* H_TIMING internal */
- +#define IMX477_REG_LINE_LENGTH 0x0342
- +#define IMX477_LINE_LENGTH_MAX 0xfff0
- +
- +/* Long exposure multiplier */
- +#define IMX477_LONG_EXP_SHIFT_MAX 7
- +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
- +
- +/* Exposure control */
- +#define IMX477_REG_EXPOSURE 0x0202
- +#define IMX477_EXPOSURE_OFFSET 22
- +#define IMX477_EXPOSURE_MIN 4
- +#define IMX477_EXPOSURE_STEP 1
- +#define IMX477_EXPOSURE_DEFAULT 0x640
- +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
- + IMX477_EXPOSURE_OFFSET)
- +
- +/* Analog gain control */
- +#define IMX477_REG_ANALOG_GAIN 0x0204
- +#define IMX477_ANA_GAIN_MIN 0
- +#define IMX477_ANA_GAIN_MAX 978
- +#define IMX477_ANA_GAIN_STEP 1
- +#define IMX477_ANA_GAIN_DEFAULT 0x0
- +
- +/* Digital gain control */
- +#define IMX477_REG_DIGITAL_GAIN 0x020e
- +#define IMX477_DGTL_GAIN_MIN 0x0100
- +#define IMX477_DGTL_GAIN_MAX 0xffff
- +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
- +#define IMX477_DGTL_GAIN_STEP 1
- +
- +/* Test Pattern Control */
- +#define IMX477_REG_TEST_PATTERN 0x0600
- +#define IMX477_TEST_PATTERN_DISABLE 0
- +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
- +#define IMX477_TEST_PATTERN_COLOR_BARS 2
- +#define IMX477_TEST_PATTERN_GREY_COLOR 3
- +#define IMX477_TEST_PATTERN_PN9 4
- +
- +/* Test pattern colour components */
- +#define IMX477_REG_TEST_PATTERN_R 0x0602
- +#define IMX477_REG_TEST_PATTERN_GR 0x0604
- +#define IMX477_REG_TEST_PATTERN_B 0x0606
- +#define IMX477_REG_TEST_PATTERN_GB 0x0608
- +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
- +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
- +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
- +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
- +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
- +#define IMX477_TEST_PATTERN_B_DEFAULT 0
- +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
- +
- +/* Trigger mode */
- +#define IMX477_REG_MC_MODE 0x3f0b
- +#define IMX477_REG_MS_SEL 0x3041
- +#define IMX477_REG_XVS_IO_CTRL 0x3040
- +#define IMX477_REG_EXTOUT_EN 0x4b81
- +
- +/* Embedded metadata stream structure */
- +#define IMX477_EMBEDDED_LINE_WIDTH 16384
- +#define IMX477_NUM_EMBEDDED_LINES 1
- +
- +enum pad_types {
- + IMAGE_PAD,
- + METADATA_PAD,
- + NUM_PADS
- +};
- +
- +/* IMX477 native and active pixel array size. */
- +#define IMX477_NATIVE_WIDTH 4072U
- +#define IMX477_NATIVE_HEIGHT 3176U
- +#define IMX477_PIXEL_ARRAY_LEFT 8U
- +#define IMX477_PIXEL_ARRAY_TOP 16U
- +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
- +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
- +
- +struct imx477_reg {
- + u16 address;
- + u8 val;
- +};
- +
- +struct imx477_reg_list {
- + unsigned int num_of_regs;
- + const struct imx477_reg *regs;
- +};
- +
- +/* Mode : resolution and related config&values */
- +struct imx477_mode {
- + /* Frame width */
- + unsigned int width;
- +
- + /* Frame height */
- + unsigned int height;
- +
- + /* H-timing in pixels */
- + unsigned int line_length_pix;
- +
- + /* Analog crop rectangle. */
- + struct v4l2_rect crop;
- +
- + /* Highest possible framerate. */
- + struct v4l2_fract timeperframe_min;
- +
- + /* Default framerate. */
- + struct v4l2_fract timeperframe_default;
- +
- + /* Default register values */
- + struct imx477_reg_list reg_list;
- +};
- +
- +static const struct imx477_reg mode_common_regs[] = {
- + {0x0136, 0x18},
- + {0x0137, 0x00},
- + {0xe000, 0x00},
- + {0xe07a, 0x01},
- + {0x0808, 0x02},
- + {0x4ae9, 0x18},
- + {0x4aea, 0x08},
- + {0xf61c, 0x04},
- + {0xf61e, 0x04},
- + {0x4ae9, 0x21},
- + {0x4aea, 0x80},
- + {0x38a8, 0x1f},
- + {0x38a9, 0xff},
- + {0x38aa, 0x1f},
- + {0x38ab, 0xff},
- + {0x55d4, 0x00},
- + {0x55d5, 0x00},
- + {0x55d6, 0x07},
- + {0x55d7, 0xff},
- + {0x55e8, 0x07},
- + {0x55e9, 0xff},
- + {0x55ea, 0x00},
- + {0x55eb, 0x00},
- + {0x574c, 0x07},
- + {0x574d, 0xff},
- + {0x574e, 0x00},
- + {0x574f, 0x00},
- + {0x5754, 0x00},
- + {0x5755, 0x00},
- + {0x5756, 0x07},
- + {0x5757, 0xff},
- + {0x5973, 0x04},
- + {0x5974, 0x01},
- + {0x5d13, 0xc3},
- + {0x5d14, 0x58},
- + {0x5d15, 0xa3},
- + {0x5d16, 0x1d},
- + {0x5d17, 0x65},
- + {0x5d18, 0x8c},
- + {0x5d1a, 0x06},
- + {0x5d1b, 0xa9},
- + {0x5d1c, 0x45},
- + {0x5d1d, 0x3a},
- + {0x5d1e, 0xab},
- + {0x5d1f, 0x15},
- + {0x5d21, 0x0e},
- + {0x5d22, 0x52},
- + {0x5d23, 0xaa},
- + {0x5d24, 0x7d},
- + {0x5d25, 0x57},
- + {0x5d26, 0xa8},
- + {0x5d37, 0x5a},
- + {0x5d38, 0x5a},
- + {0x5d77, 0x7f},
- + {0x7b75, 0x0e},
- + {0x7b76, 0x0b},
- + {0x7b77, 0x08},
- + {0x7b78, 0x0a},
- + {0x7b79, 0x47},
- + {0x7b7c, 0x00},
- + {0x7b7d, 0x00},
- + {0x8d1f, 0x00},
- + {0x8d27, 0x00},
- + {0x9004, 0x03},
- + {0x9200, 0x50},
- + {0x9201, 0x6c},
- + {0x9202, 0x71},
- + {0x9203, 0x00},
- + {0x9204, 0x71},
- + {0x9205, 0x01},
- + {0x9371, 0x6a},
- + {0x9373, 0x6a},
- + {0x9375, 0x64},
- + {0x991a, 0x00},
- + {0x996b, 0x8c},
- + {0x996c, 0x64},
- + {0x996d, 0x50},
- + {0x9a4c, 0x0d},
- + {0x9a4d, 0x0d},
- + {0xa001, 0x0a},
- + {0xa003, 0x0a},
- + {0xa005, 0x0a},
- + {0xa006, 0x01},
- + {0xa007, 0xc0},
- + {0xa009, 0xc0},
- + {0x3d8a, 0x01},
- + {0x4421, 0x04},
- + {0x7b3b, 0x01},
- + {0x7b4c, 0x00},
- + {0x9905, 0x00},
- + {0x9907, 0x00},
- + {0x9909, 0x00},
- + {0x990b, 0x00},
- + {0x9944, 0x3c},
- + {0x9947, 0x3c},
- + {0x994a, 0x8c},
- + {0x994b, 0x50},
- + {0x994c, 0x1b},
- + {0x994d, 0x8c},
- + {0x994e, 0x50},
- + {0x994f, 0x1b},
- + {0x9950, 0x8c},
- + {0x9951, 0x1b},
- + {0x9952, 0x0a},
- + {0x9953, 0x8c},
- + {0x9954, 0x1b},
- + {0x9955, 0x0a},
- + {0x9a13, 0x04},
- + {0x9a14, 0x04},
- + {0x9a19, 0x00},
- + {0x9a1c, 0x04},
- + {0x9a1d, 0x04},
- + {0x9a26, 0x05},
- + {0x9a27, 0x05},
- + {0x9a2c, 0x01},
- + {0x9a2d, 0x03},
- + {0x9a2f, 0x05},
- + {0x9a30, 0x05},
- + {0x9a41, 0x00},
- + {0x9a46, 0x00},
- + {0x9a47, 0x00},
- + {0x9c17, 0x35},
- + {0x9c1d, 0x31},
- + {0x9c29, 0x50},
- + {0x9c3b, 0x2f},
- + {0x9c41, 0x6b},
- + {0x9c47, 0x2d},
- + {0x9c4d, 0x40},
- + {0x9c6b, 0x00},
- + {0x9c71, 0xc8},
- + {0x9c73, 0x32},
- + {0x9c75, 0x04},
- + {0x9c7d, 0x2d},
- + {0x9c83, 0x40},
- + {0x9c94, 0x3f},
- + {0x9c95, 0x3f},
- + {0x9c96, 0x3f},
- + {0x9c97, 0x00},
- + {0x9c98, 0x00},
- + {0x9c99, 0x00},
- + {0x9c9a, 0x3f},
- + {0x9c9b, 0x3f},
- + {0x9c9c, 0x3f},
- + {0x9ca0, 0x0f},
- + {0x9ca1, 0x0f},
- + {0x9ca2, 0x0f},
- + {0x9ca3, 0x00},
- + {0x9ca4, 0x00},
- + {0x9ca5, 0x00},
- + {0x9ca6, 0x1e},
- + {0x9ca7, 0x1e},
- + {0x9ca8, 0x1e},
- + {0x9ca9, 0x00},
- + {0x9caa, 0x00},
- + {0x9cab, 0x00},
- + {0x9cac, 0x09},
- + {0x9cad, 0x09},
- + {0x9cae, 0x09},
- + {0x9cbd, 0x50},
- + {0x9cbf, 0x50},
- + {0x9cc1, 0x50},
- + {0x9cc3, 0x40},
- + {0x9cc5, 0x40},
- + {0x9cc7, 0x40},
- + {0x9cc9, 0x0a},
- + {0x9ccb, 0x0a},
- + {0x9ccd, 0x0a},
- + {0x9d17, 0x35},
- + {0x9d1d, 0x31},
- + {0x9d29, 0x50},
- + {0x9d3b, 0x2f},
- + {0x9d41, 0x6b},
- + {0x9d47, 0x42},
- + {0x9d4d, 0x5a},
- + {0x9d6b, 0x00},
- + {0x9d71, 0xc8},
- + {0x9d73, 0x32},
- + {0x9d75, 0x04},
- + {0x9d7d, 0x42},
- + {0x9d83, 0x5a},
- + {0x9d94, 0x3f},
- + {0x9d95, 0x3f},
- + {0x9d96, 0x3f},
- + {0x9d97, 0x00},
- + {0x9d98, 0x00},
- + {0x9d99, 0x00},
- + {0x9d9a, 0x3f},
- + {0x9d9b, 0x3f},
- + {0x9d9c, 0x3f},
- + {0x9d9d, 0x1f},
- + {0x9d9e, 0x1f},
- + {0x9d9f, 0x1f},
- + {0x9da0, 0x0f},
- + {0x9da1, 0x0f},
- + {0x9da2, 0x0f},
- + {0x9da3, 0x00},
- + {0x9da4, 0x00},
- + {0x9da5, 0x00},
- + {0x9da6, 0x1e},
- + {0x9da7, 0x1e},
- + {0x9da8, 0x1e},
- + {0x9da9, 0x00},
- + {0x9daa, 0x00},
- + {0x9dab, 0x00},
- + {0x9dac, 0x09},
- + {0x9dad, 0x09},
- + {0x9dae, 0x09},
- + {0x9dc9, 0x0a},
- + {0x9dcb, 0x0a},
- + {0x9dcd, 0x0a},
- + {0x9e17, 0x35},
- + {0x9e1d, 0x31},
- + {0x9e29, 0x50},
- + {0x9e3b, 0x2f},
- + {0x9e41, 0x6b},
- + {0x9e47, 0x2d},
- + {0x9e4d, 0x40},
- + {0x9e6b, 0x00},
- + {0x9e71, 0xc8},
- + {0x9e73, 0x32},
- + {0x9e75, 0x04},
- + {0x9e94, 0x0f},
- + {0x9e95, 0x0f},
- + {0x9e96, 0x0f},
- + {0x9e97, 0x00},
- + {0x9e98, 0x00},
- + {0x9e99, 0x00},
- + {0x9ea0, 0x0f},
- + {0x9ea1, 0x0f},
- + {0x9ea2, 0x0f},
- + {0x9ea3, 0x00},
- + {0x9ea4, 0x00},
- + {0x9ea5, 0x00},
- + {0x9ea6, 0x3f},
- + {0x9ea7, 0x3f},
- + {0x9ea8, 0x3f},
- + {0x9ea9, 0x00},
- + {0x9eaa, 0x00},
- + {0x9eab, 0x00},
- + {0x9eac, 0x09},
- + {0x9ead, 0x09},
- + {0x9eae, 0x09},
- + {0x9ec9, 0x0a},
- + {0x9ecb, 0x0a},
- + {0x9ecd, 0x0a},
- + {0x9f17, 0x35},
- + {0x9f1d, 0x31},
- + {0x9f29, 0x50},
- + {0x9f3b, 0x2f},
- + {0x9f41, 0x6b},
- + {0x9f47, 0x42},
- + {0x9f4d, 0x5a},
- + {0x9f6b, 0x00},
- + {0x9f71, 0xc8},
- + {0x9f73, 0x32},
- + {0x9f75, 0x04},
- + {0x9f94, 0x0f},
- + {0x9f95, 0x0f},
- + {0x9f96, 0x0f},
- + {0x9f97, 0x00},
- + {0x9f98, 0x00},
- + {0x9f99, 0x00},
- + {0x9f9a, 0x2f},
- + {0x9f9b, 0x2f},
- + {0x9f9c, 0x2f},
- + {0x9f9d, 0x00},
- + {0x9f9e, 0x00},
- + {0x9f9f, 0x00},
- + {0x9fa0, 0x0f},
- + {0x9fa1, 0x0f},
- + {0x9fa2, 0x0f},
- + {0x9fa3, 0x00},
- + {0x9fa4, 0x00},
- + {0x9fa5, 0x00},
- + {0x9fa6, 0x1e},
- + {0x9fa7, 0x1e},
- + {0x9fa8, 0x1e},
- + {0x9fa9, 0x00},
- + {0x9faa, 0x00},
- + {0x9fab, 0x00},
- + {0x9fac, 0x09},
- + {0x9fad, 0x09},
- + {0x9fae, 0x09},
- + {0x9fc9, 0x0a},
- + {0x9fcb, 0x0a},
- + {0x9fcd, 0x0a},
- + {0xa14b, 0xff},
- + {0xa151, 0x0c},
- + {0xa153, 0x50},
- + {0xa155, 0x02},
- + {0xa157, 0x00},
- + {0xa1ad, 0xff},
- + {0xa1b3, 0x0c},
- + {0xa1b5, 0x50},
- + {0xa1b9, 0x00},
- + {0xa24b, 0xff},
- + {0xa257, 0x00},
- + {0xa2ad, 0xff},
- + {0xa2b9, 0x00},
- + {0xb21f, 0x04},
- + {0xb35c, 0x00},
- + {0xb35e, 0x08},
- + {0x0112, 0x0c},
- + {0x0113, 0x0c},
- + {0x0114, 0x01},
- + {0x0350, 0x00},
- + {0xbcf1, 0x02},
- + {0x3ff9, 0x01},
- +};
- +
- +/* 12 mpix 10fps */
- +static const struct imx477_reg mode_4056x3040_regs[] = {
- + {0x0342, 0x5d},
- + {0x0343, 0xc0},
- + {0x0344, 0x00},
- + {0x0345, 0x00},
- + {0x0346, 0x00},
- + {0x0347, 0x00},
- + {0x0348, 0x0f},
- + {0x0349, 0xd7},
- + {0x034a, 0x0b},
- + {0x034b, 0xdf},
- + {0x00e3, 0x00},
- + {0x00e4, 0x00},
- + {0x00fc, 0x0a},
- + {0x00fd, 0x0a},
- + {0x00fe, 0x0a},
- + {0x00ff, 0x0a},
- + {0x0220, 0x00},
- + {0x0221, 0x11},
- + {0x0381, 0x01},
- + {0x0383, 0x01},
- + {0x0385, 0x01},
- + {0x0387, 0x01},
- + {0x0900, 0x00},
- + {0x0901, 0x11},
- + {0x0902, 0x02},
- + {0x3140, 0x02},
- + {0x3c00, 0x00},
- + {0x3c01, 0x03},
- + {0x3c02, 0xa2},
- + {0x3f0d, 0x01},
- + {0x5748, 0x07},
- + {0x5749, 0xff},
- + {0x574a, 0x00},
- + {0x574b, 0x00},
- + {0x7b75, 0x0a},
- + {0x7b76, 0x0c},
- + {0x7b77, 0x07},
- + {0x7b78, 0x06},
- + {0x7b79, 0x3c},
- + {0x7b53, 0x01},
- + {0x9369, 0x5a},
- + {0x936b, 0x55},
- + {0x936d, 0x28},
- + {0x9304, 0x00},
- + {0x9305, 0x00},
- + {0x9e9a, 0x2f},
- + {0x9e9b, 0x2f},
- + {0x9e9c, 0x2f},
- + {0x9e9d, 0x00},
- + {0x9e9e, 0x00},
- + {0x9e9f, 0x00},
- + {0xa2a9, 0x60},
- + {0xa2b7, 0x00},
- + {0x0401, 0x00},
- + {0x0404, 0x00},
- + {0x0405, 0x10},
- + {0x0408, 0x00},
- + {0x0409, 0x00},
- + {0x040a, 0x00},
- + {0x040b, 0x00},
- + {0x040c, 0x0f},
- + {0x040d, 0xd8},
- + {0x040e, 0x0b},
- + {0x040f, 0xe0},
- + {0x034c, 0x0f},
- + {0x034d, 0xd8},
- + {0x034e, 0x0b},
- + {0x034f, 0xe0},
- + {0x0301, 0x05},
- + {0x0303, 0x02},
- + {0x0305, 0x04},
- + {0x0306, 0x01},
- + {0x0307, 0x5e},
- + {0x0309, 0x0c},
- + {0x030b, 0x02},
- + {0x030d, 0x02},
- + {0x030e, 0x00},
- + {0x030f, 0x96},
- + {0x0310, 0x01},
- + {0x0820, 0x07},
- + {0x0821, 0x08},
- + {0x0822, 0x00},
- + {0x0823, 0x00},
- + {0x080a, 0x00},
- + {0x080b, 0x7f},
- + {0x080c, 0x00},
- + {0x080d, 0x4f},
- + {0x080e, 0x00},
- + {0x080f, 0x77},
- + {0x0810, 0x00},
- + {0x0811, 0x5f},
- + {0x0812, 0x00},
- + {0x0813, 0x57},
- + {0x0814, 0x00},
- + {0x0815, 0x4f},
- + {0x0816, 0x01},
- + {0x0817, 0x27},
- + {0x0818, 0x00},
- + {0x0819, 0x3f},
- + {0xe04c, 0x00},
- + {0xe04d, 0x7f},
- + {0xe04e, 0x00},
- + {0xe04f, 0x1f},
- + {0x3e20, 0x01},
- + {0x3e37, 0x00},
- + {0x3f50, 0x00},
- + {0x3f56, 0x02},
- + {0x3f57, 0xae},
- +};
- +
- +/* 2x2 binned. 40fps */
- +static const struct imx477_reg mode_2028x1520_regs[] = {
- + {0x0342, 0x31},
- + {0x0343, 0xc4},
- + {0x0344, 0x00},
- + {0x0345, 0x00},
- + {0x0346, 0x00},
- + {0x0347, 0x00},
- + {0x0348, 0x0f},
- + {0x0349, 0xd7},
- + {0x034a, 0x0b},
- + {0x034b, 0xdf},
- + {0x0220, 0x00},
- + {0x0221, 0x11},
- + {0x0381, 0x01},
- + {0x0383, 0x01},
- + {0x0385, 0x01},
- + {0x0387, 0x01},
- + {0x0900, 0x01},
- + {0x0901, 0x12},
- + {0x0902, 0x02},
- + {0x3140, 0x02},
- + {0x3c00, 0x00},
- + {0x3c01, 0x03},
- + {0x3c02, 0xa2},
- + {0x3f0d, 0x01},
- + {0x5748, 0x07},
- + {0x5749, 0xff},
- + {0x574a, 0x00},
- + {0x574b, 0x00},
- + {0x7b53, 0x01},
- + {0x9369, 0x73},
- + {0x936b, 0x64},
- + {0x936d, 0x5f},
- + {0x9304, 0x00},
- + {0x9305, 0x00},
- + {0x9e9a, 0x2f},
- + {0x9e9b, 0x2f},
- + {0x9e9c, 0x2f},
- + {0x9e9d, 0x00},
- + {0x9e9e, 0x00},
- + {0x9e9f, 0x00},
- + {0xa2a9, 0x60},
- + {0xa2b7, 0x00},
- + {0x0401, 0x01},
- + {0x0404, 0x00},
- + {0x0405, 0x20},
- + {0x0408, 0x00},
- + {0x0409, 0x00},
- + {0x040a, 0x00},
- + {0x040b, 0x00},
- + {0x040c, 0x0f},
- + {0x040d, 0xd8},
- + {0x040e, 0x0b},
- + {0x040f, 0xe0},
- + {0x034c, 0x07},
- + {0x034d, 0xec},
- + {0x034e, 0x05},
- + {0x034f, 0xf0},
- + {0x0301, 0x05},
- + {0x0303, 0x02},
- + {0x0305, 0x04},
- + {0x0306, 0x01},
- + {0x0307, 0x5e},
- + {0x0309, 0x0c},
- + {0x030b, 0x02},
- + {0x030d, 0x02},
- + {0x030e, 0x00},
- + {0x030f, 0x96},
- + {0x0310, 0x01},
- + {0x0820, 0x07},
- + {0x0821, 0x08},
- + {0x0822, 0x00},
- + {0x0823, 0x00},
- + {0x080a, 0x00},
- + {0x080b, 0x7f},
- + {0x080c, 0x00},
- + {0x080d, 0x4f},
- + {0x080e, 0x00},
- + {0x080f, 0x77},
- + {0x0810, 0x00},
- + {0x0811, 0x5f},
- + {0x0812, 0x00},
- + {0x0813, 0x57},
- + {0x0814, 0x00},
- + {0x0815, 0x4f},
- + {0x0816, 0x01},
- + {0x0817, 0x27},
- + {0x0818, 0x00},
- + {0x0819, 0x3f},
- + {0xe04c, 0x00},
- + {0xe04d, 0x7f},
- + {0xe04e, 0x00},
- + {0xe04f, 0x1f},
- + {0x3e20, 0x01},
- + {0x3e37, 0x00},
- + {0x3f50, 0x00},
- + {0x3f56, 0x01},
- + {0x3f57, 0x6c},
- +};
- +
- +/* 1080p cropped mode */
- +static const struct imx477_reg mode_2028x1080_regs[] = {
- + {0x0342, 0x31},
- + {0x0343, 0xc4},
- + {0x0344, 0x00},
- + {0x0345, 0x00},
- + {0x0346, 0x01},
- + {0x0347, 0xb8},
- + {0x0348, 0x0f},
- + {0x0349, 0xd7},
- + {0x034a, 0x0a},
- + {0x034b, 0x27},
- + {0x0220, 0x00},
- + {0x0221, 0x11},
- + {0x0381, 0x01},
- + {0x0383, 0x01},
- + {0x0385, 0x01},
- + {0x0387, 0x01},
- + {0x0900, 0x01},
- + {0x0901, 0x12},
- + {0x0902, 0x02},
- + {0x3140, 0x02},
- + {0x3c00, 0x00},
- + {0x3c01, 0x03},
- + {0x3c02, 0xa2},
- + {0x3f0d, 0x01},
- + {0x5748, 0x07},
- + {0x5749, 0xff},
- + {0x574a, 0x00},
- + {0x574b, 0x00},
- + {0x7b53, 0x01},
- + {0x9369, 0x73},
- + {0x936b, 0x64},
- + {0x936d, 0x5f},
- + {0x9304, 0x00},
- + {0x9305, 0x00},
- + {0x9e9a, 0x2f},
- + {0x9e9b, 0x2f},
- + {0x9e9c, 0x2f},
- + {0x9e9d, 0x00},
- + {0x9e9e, 0x00},
- + {0x9e9f, 0x00},
- + {0xa2a9, 0x60},
- + {0xa2b7, 0x00},
- + {0x0401, 0x01},
- + {0x0404, 0x00},
- + {0x0405, 0x20},
- + {0x0408, 0x00},
- + {0x0409, 0x00},
- + {0x040a, 0x00},
- + {0x040b, 0x00},
- + {0x040c, 0x0f},
- + {0x040d, 0xd8},
- + {0x040e, 0x04},
- + {0x040f, 0x38},
- + {0x034c, 0x07},
- + {0x034d, 0xec},
- + {0x034e, 0x04},
- + {0x034f, 0x38},
- + {0x0301, 0x05},
- + {0x0303, 0x02},
- + {0x0305, 0x04},
- + {0x0306, 0x01},
- + {0x0307, 0x5e},
- + {0x0309, 0x0c},
- + {0x030b, 0x02},
- + {0x030d, 0x02},
- + {0x030e, 0x00},
- + {0x030f, 0x96},
- + {0x0310, 0x01},
- + {0x0820, 0x07},
- + {0x0821, 0x08},
- + {0x0822, 0x00},
- + {0x0823, 0x00},
- + {0x080a, 0x00},
- + {0x080b, 0x7f},
- + {0x080c, 0x00},
- + {0x080d, 0x4f},
- + {0x080e, 0x00},
- + {0x080f, 0x77},
- + {0x0810, 0x00},
- + {0x0811, 0x5f},
- + {0x0812, 0x00},
- + {0x0813, 0x57},
- + {0x0814, 0x00},
- + {0x0815, 0x4f},
- + {0x0816, 0x01},
- + {0x0817, 0x27},
- + {0x0818, 0x00},
- + {0x0819, 0x3f},
- + {0xe04c, 0x00},
- + {0xe04d, 0x7f},
- + {0xe04e, 0x00},
- + {0xe04f, 0x1f},
- + {0x3e20, 0x01},
- + {0x3e37, 0x00},
- + {0x3f50, 0x00},
- + {0x3f56, 0x01},
- + {0x3f57, 0x6c},
- +};
- +
- +/* 4x4 binned. 120fps */
- +static const struct imx477_reg mode_1332x990_regs[] = {
- + {0x420b, 0x01},
- + {0x990c, 0x00},
- + {0x990d, 0x08},
- + {0x9956, 0x8c},
- + {0x9957, 0x64},
- + {0x9958, 0x50},
- + {0x9a48, 0x06},
- + {0x9a49, 0x06},
- + {0x9a4a, 0x06},
- + {0x9a4b, 0x06},
- + {0x9a4c, 0x06},
- + {0x9a4d, 0x06},
- + {0x0112, 0x0a},
- + {0x0113, 0x0a},
- + {0x0114, 0x01},
- + {0x0342, 0x1a},
- + {0x0343, 0x08},
- + {0x0340, 0x04},
- + {0x0341, 0x1a},
- + {0x0344, 0x00},
- + {0x0345, 0x00},
- + {0x0346, 0x02},
- + {0x0347, 0x10},
- + {0x0348, 0x0f},
- + {0x0349, 0xd7},
- + {0x034a, 0x09},
- + {0x034b, 0xcf},
- + {0x00e3, 0x00},
- + {0x00e4, 0x00},
- + {0x00fc, 0x0a},
- + {0x00fd, 0x0a},
- + {0x00fe, 0x0a},
- + {0x00ff, 0x0a},
- + {0xe013, 0x00},
- + {0x0220, 0x00},
- + {0x0221, 0x11},
- + {0x0381, 0x01},
- + {0x0383, 0x01},
- + {0x0385, 0x01},
- + {0x0387, 0x01},
- + {0x0900, 0x01},
- + {0x0901, 0x22},
- + {0x0902, 0x02},
- + {0x3140, 0x02},
- + {0x3c00, 0x00},
- + {0x3c01, 0x01},
- + {0x3c02, 0x9c},
- + {0x3f0d, 0x00},
- + {0x5748, 0x00},
- + {0x5749, 0x00},
- + {0x574a, 0x00},
- + {0x574b, 0xa4},
- + {0x7b75, 0x0e},
- + {0x7b76, 0x09},
- + {0x7b77, 0x08},
- + {0x7b78, 0x06},
- + {0x7b79, 0x34},
- + {0x7b53, 0x00},
- + {0x9369, 0x73},
- + {0x936b, 0x64},
- + {0x936d, 0x5f},
- + {0x9304, 0x03},
- + {0x9305, 0x80},
- + {0x9e9a, 0x2f},
- + {0x9e9b, 0x2f},
- + {0x9e9c, 0x2f},
- + {0x9e9d, 0x00},
- + {0x9e9e, 0x00},
- + {0x9e9f, 0x00},
- + {0xa2a9, 0x27},
- + {0xa2b7, 0x03},
- + {0x0401, 0x00},
- + {0x0404, 0x00},
- + {0x0405, 0x10},
- + {0x0408, 0x01},
- + {0x0409, 0x5c},
- + {0x040a, 0x00},
- + {0x040b, 0x00},
- + {0x040c, 0x05},
- + {0x040d, 0x34},
- + {0x040e, 0x03},
- + {0x040f, 0xde},
- + {0x034c, 0x05},
- + {0x034d, 0x34},
- + {0x034e, 0x03},
- + {0x034f, 0xde},
- + {0x0301, 0x05},
- + {0x0303, 0x02},
- + {0x0305, 0x02},
- + {0x0306, 0x00},
- + {0x0307, 0xaf},
- + {0x0309, 0x0a},
- + {0x030b, 0x02},
- + {0x030d, 0x02},
- + {0x030e, 0x00},
- + {0x030f, 0x96},
- + {0x0310, 0x01},
- + {0x0820, 0x07},
- + {0x0821, 0x08},
- + {0x0822, 0x00},
- + {0x0823, 0x00},
- + {0x080a, 0x00},
- + {0x080b, 0x7f},
- + {0x080c, 0x00},
- + {0x080d, 0x4f},
- + {0x080e, 0x00},
- + {0x080f, 0x77},
- + {0x0810, 0x00},
- + {0x0811, 0x5f},
- + {0x0812, 0x00},
- + {0x0813, 0x57},
- + {0x0814, 0x00},
- + {0x0815, 0x4f},
- + {0x0816, 0x01},
- + {0x0817, 0x27},
- + {0x0818, 0x00},
- + {0x0819, 0x3f},
- + {0xe04c, 0x00},
- + {0xe04d, 0x5f},
- + {0xe04e, 0x00},
- + {0xe04f, 0x1f},
- + {0x3e20, 0x01},
- + {0x3e37, 0x00},
- + {0x3f50, 0x00},
- + {0x3f56, 0x00},
- + {0x3f57, 0xbf},
- +};
- +
- +/* Mode configs */
- +static const struct imx477_mode supported_modes_12bit[] = {
- + {
- + /* 12MPix 10fps mode */
- + .width = 4056,
- + .height = 3040,
- + .line_length_pix = 0x5dc0,
- + .crop = {
- + .left = IMX477_PIXEL_ARRAY_LEFT,
- + .top = IMX477_PIXEL_ARRAY_TOP,
- + .width = 4056,
- + .height = 3040,
- + },
- + .timeperframe_min = {
- + .numerator = 100,
- + .denominator = 1000
- + },
- + .timeperframe_default = {
- + .numerator = 100,
- + .denominator = 1000
- + },
- + .reg_list = {
- + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
- + .regs = mode_4056x3040_regs,
- + },
- + },
- + {
- + /* 2x2 binned 40fps mode */
- + .width = 2028,
- + .height = 1520,
- + .line_length_pix = 0x31c4,
- + .crop = {
- + .left = IMX477_PIXEL_ARRAY_LEFT,
- + .top = IMX477_PIXEL_ARRAY_TOP,
- + .width = 4056,
- + .height = 3040,
- + },
- + .timeperframe_min = {
- + .numerator = 100,
- + .denominator = 4000
- + },
- + .timeperframe_default = {
- + .numerator = 100,
- + .denominator = 3000
- + },
- + .reg_list = {
- + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
- + .regs = mode_2028x1520_regs,
- + },
- + },
- + {
- + /* 1080p 50fps cropped mode */
- + .width = 2028,
- + .height = 1080,
- + .line_length_pix = 0x31c4,
- + .crop = {
- + .left = IMX477_PIXEL_ARRAY_LEFT,
- + .top = IMX477_PIXEL_ARRAY_TOP + 440,
- + .width = 4056,
- + .height = 2160,
- + },
- + .timeperframe_min = {
- + .numerator = 100,
- + .denominator = 5000
- + },
- + .timeperframe_default = {
- + .numerator = 100,
- + .denominator = 3000
- + },
- + .reg_list = {
- + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
- + .regs = mode_2028x1080_regs,
- + },
- + }
- +};
- +
- +static const struct imx477_mode supported_modes_10bit[] = {
- + {
- + /* 120fps. 2x2 binned and cropped */
- + .width = 1332,
- + .height = 990,
- + .line_length_pix = 6664,
- + .crop = {
- + /*
- + * FIXME: the analog crop rectangle is actually
- + * programmed with a horizontal displacement of 0
- + * pixels, not 4. It gets shrunk after going through
- + * the scaler. Move this information to the compose
- + * rectangle once the driver is expanded to represent
- + * its processing blocks with multiple subdevs.
- + */
- + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
- + .top = IMX477_PIXEL_ARRAY_TOP + 528,
- + .width = 2664,
- + .height = 1980,
- + },
- + .timeperframe_min = {
- + .numerator = 100,
- + .denominator = 12000
- + },
- + .timeperframe_default = {
- + .numerator = 100,
- + .denominator = 12000
- + },
- + .reg_list = {
- + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
- + .regs = mode_1332x990_regs,
- + }
- + }
- +};
- +
- +/*
- + * The supported formats.
- + * This table MUST contain 4 entries per format, to cover the various flip
- + * combinations in the order
- + * - no flip
- + * - h flip
- + * - v flip
- + * - h&v flips
- + */
- +static const u32 codes[] = {
- + /* 12-bit modes. */
- + MEDIA_BUS_FMT_SRGGB12_1X12,
- + MEDIA_BUS_FMT_SGRBG12_1X12,
- + MEDIA_BUS_FMT_SGBRG12_1X12,
- + MEDIA_BUS_FMT_SBGGR12_1X12,
- + /* 10-bit modes. */
- + MEDIA_BUS_FMT_SRGGB10_1X10,
- + MEDIA_BUS_FMT_SGRBG10_1X10,
- + MEDIA_BUS_FMT_SGBRG10_1X10,
- + MEDIA_BUS_FMT_SBGGR10_1X10,
- +};
- +
- +static const char * const imx477_test_pattern_menu[] = {
- + "Disabled",
- + "Color Bars",
- + "Solid Color",
- + "Grey Color Bars",
- + "PN9"
- +};
- +
- +static const int imx477_test_pattern_val[] = {
- + IMX477_TEST_PATTERN_DISABLE,
- + IMX477_TEST_PATTERN_COLOR_BARS,
- + IMX477_TEST_PATTERN_SOLID_COLOR,
- + IMX477_TEST_PATTERN_GREY_COLOR,
- + IMX477_TEST_PATTERN_PN9,
- +};
- +
- +/* regulator supplies */
- +static const char * const imx477_supply_name[] = {
- + /* Supplies can be enabled in any order */
- + "VANA", /* Analog (2.8V) supply */
- + "VDIG", /* Digital Core (1.05V) supply */
- + "VDDL", /* IF (1.8V) supply */
- +};
- +
- +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
- +
- +/*
- + * Initialisation delay between XCLR low->high and the moment when the sensor
- + * can start capture (i.e. can leave software standby), given by T7 in the
- + * datasheet is 8ms. This does include I2C setup time as well.
- + *
- + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
- + * in the datasheet) is much smaller - 600us.
- + */
- +#define IMX477_XCLR_MIN_DELAY_US 8000
- +#define IMX477_XCLR_DELAY_RANGE_US 1000
- +
- +struct imx477_compatible_data {
- + unsigned int chip_id;
- + struct imx477_reg_list extra_regs;
- +};
- +
- +struct imx477 {
- + struct v4l2_subdev sd;
- + struct media_pad pad[NUM_PADS];
- +
- + unsigned int fmt_code;
- +
- + struct clk *xclk;
- + u32 xclk_freq;
- +
- + struct gpio_desc *reset_gpio;
- + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
- +
- + struct v4l2_ctrl_handler ctrl_handler;
- + /* V4L2 Controls */
- + struct v4l2_ctrl *pixel_rate;
- + struct v4l2_ctrl *exposure;
- + struct v4l2_ctrl *vflip;
- + struct v4l2_ctrl *hflip;
- + struct v4l2_ctrl *vblank;
- + struct v4l2_ctrl *hblank;
- +
- + /* Current mode */
- + const struct imx477_mode *mode;
- +
- + /*
- + * Mutex for serialized access:
- + * Protect sensor module set pad format and start/stop streaming safely.
- + */
- + struct mutex mutex;
- +
- + /* Streaming on/off */
- + bool streaming;
- +
- + /* Rewrite common registers on stream on? */
- + bool common_regs_written;
- +
- + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
- + unsigned int long_exp_shift;
- +
- + /* Any extra information related to different compatible sensors */
- + const struct imx477_compatible_data *compatible_data;
- +};
- +
- +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
- +{
- + return container_of(_sd, struct imx477, sd);
- +}
- +
- +static inline void get_mode_table(unsigned int code,
- + const struct imx477_mode **mode_list,
- + unsigned int *num_modes)
- +{
- + switch (code) {
- + /* 12-bit */
- + case MEDIA_BUS_FMT_SRGGB12_1X12:
- + case MEDIA_BUS_FMT_SGRBG12_1X12:
- + case MEDIA_BUS_FMT_SGBRG12_1X12:
- + case MEDIA_BUS_FMT_SBGGR12_1X12:
- + *mode_list = supported_modes_12bit;
- + *num_modes = ARRAY_SIZE(supported_modes_12bit);
- + break;
- + /* 10-bit */
- + case MEDIA_BUS_FMT_SRGGB10_1X10:
- + case MEDIA_BUS_FMT_SGRBG10_1X10:
- + case MEDIA_BUS_FMT_SGBRG10_1X10:
- + case MEDIA_BUS_FMT_SBGGR10_1X10:
- + *mode_list = supported_modes_10bit;
- + *num_modes = ARRAY_SIZE(supported_modes_10bit);
- + break;
- + default:
- + *mode_list = NULL;
- + *num_modes = 0;
- + }
- +}
- +
- +/* Read registers up to 2 at a time */
- +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + struct i2c_msg msgs[2];
- + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
- + u8 data_buf[4] = { 0, };
- + int ret;
- +
- + if (len > 4)
- + return -EINVAL;
- +
- + /* Write register address */
- + msgs[0].addr = client->addr;
- + msgs[0].flags = 0;
- + msgs[0].len = ARRAY_SIZE(addr_buf);
- + msgs[0].buf = addr_buf;
- +
- + /* Read data from register */
- + msgs[1].addr = client->addr;
- + msgs[1].flags = I2C_M_RD;
- + msgs[1].len = len;
- + msgs[1].buf = &data_buf[4 - len];
- +
- + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
- + if (ret != ARRAY_SIZE(msgs))
- + return -EIO;
- +
- + *val = get_unaligned_be32(data_buf);
- +
- + return 0;
- +}
- +
- +/* Write registers up to 2 at a time */
- +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + u8 buf[6];
- +
- + if (len > 4)
- + return -EINVAL;
- +
- + put_unaligned_be16(reg, buf);
- + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
- + if (i2c_master_send(client, buf, len + 2) != len + 2)
- + return -EIO;
- +
- + return 0;
- +}
- +
- +/* Write a list of registers */
- +static int imx477_write_regs(struct imx477 *imx477,
- + const struct imx477_reg *regs, u32 len)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + unsigned int i;
- + int ret;
- +
- + for (i = 0; i < len; i++) {
- + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
- + if (ret) {
- + dev_err_ratelimited(&client->dev,
- + "Failed to write reg 0x%4.4x. error = %d\n",
- + regs[i].address, ret);
- +
- + return ret;
- + }
- + }
- +
- + return 0;
- +}
- +
- +/* Get bayer order based on flip setting. */
- +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
- +{
- + unsigned int i;
- +
- + lockdep_assert_held(&imx477->mutex);
- +
- + for (i = 0; i < ARRAY_SIZE(codes); i++)
- + if (codes[i] == code)
- + break;
- +
- + if (i >= ARRAY_SIZE(codes))
- + i = 0;
- +
- + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
- + (imx477->hflip->val ? 1 : 0);
- +
- + return codes[i];
- +}
- +
- +static void imx477_set_default_format(struct imx477 *imx477)
- +{
- + /* Set default mode to max resolution */
- + imx477->mode = &supported_modes_12bit[0];
- + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
- +}
- +
- +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
- +{
- + struct imx477 *imx477 = to_imx477(sd);
- + struct v4l2_mbus_framefmt *try_fmt_img =
- + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
- + struct v4l2_mbus_framefmt *try_fmt_meta =
- + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
- + struct v4l2_rect *try_crop;
- +
- + mutex_lock(&imx477->mutex);
- +
- + /* Initialize try_fmt for the image pad */
- + try_fmt_img->width = supported_modes_12bit[0].width;
- + try_fmt_img->height = supported_modes_12bit[0].height;
- + try_fmt_img->code = imx477_get_format_code(imx477,
- + MEDIA_BUS_FMT_SRGGB12_1X12);
- + try_fmt_img->field = V4L2_FIELD_NONE;
- +
- + /* Initialize try_fmt for the embedded metadata pad */
- + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
- + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
- + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
- + try_fmt_meta->field = V4L2_FIELD_NONE;
- +
- + /* Initialize try_crop */
- + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
- + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
- + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
- + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
- + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
- +
- + mutex_unlock(&imx477->mutex);
- +
- + return 0;
- +}
- +
- +static void imx477_adjust_exposure_range(struct imx477 *imx477)
- +{
- + int exposure_max, exposure_def;
- +
- + /* Honour the VBLANK limits when setting exposure. */
- + exposure_max = imx477->mode->height + imx477->vblank->val -
- + IMX477_EXPOSURE_OFFSET;
- + exposure_def = min(exposure_max, imx477->exposure->val);
- + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
- + exposure_max, imx477->exposure->step,
- + exposure_def);
- +}
- +
- +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
- +{
- + int ret = 0;
- +
- + imx477->long_exp_shift = 0;
- +
- + while (val > IMX477_FRAME_LENGTH_MAX) {
- + imx477->long_exp_shift++;
- + val >>= 1;
- + }
- +
- + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
- + IMX477_REG_VALUE_16BIT, val);
- + if (ret)
- + return ret;
- +
- + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
- + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
- +}
- +
- +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
- +{
- + struct imx477 *imx477 =
- + container_of(ctrl->handler, struct imx477, ctrl_handler);
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + int ret = 0;
- +
- + /*
- + * The VBLANK control may change the limits of usable exposure, so check
- + * and adjust if necessary.
- + */
- + if (ctrl->id == V4L2_CID_VBLANK)
- + imx477_adjust_exposure_range(imx477);
- +
- + /*
- + * Applying V4L2 control value only happens
- + * when power is up for streaming
- + */
- + if (pm_runtime_get_if_in_use(&client->dev) == 0)
- + return 0;
- +
- + switch (ctrl->id) {
- + case V4L2_CID_ANALOGUE_GAIN:
- + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_EXPOSURE:
- + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
- + IMX477_REG_VALUE_16BIT, ctrl->val >>
- + imx477->long_exp_shift);
- + break;
- + case V4L2_CID_DIGITAL_GAIN:
- + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_TEST_PATTERN:
- + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
- + IMX477_REG_VALUE_16BIT,
- + imx477_test_pattern_val[ctrl->val]);
- + break;
- + case V4L2_CID_TEST_PATTERN_RED:
- + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_TEST_PATTERN_GREENR:
- + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_TEST_PATTERN_BLUE:
- + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_TEST_PATTERN_GREENB:
- + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
- + IMX477_REG_VALUE_16BIT, ctrl->val);
- + break;
- + case V4L2_CID_HFLIP:
- + case V4L2_CID_VFLIP:
- + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
- + imx477->hflip->val |
- + imx477->vflip->val << 1);
- + break;
- + case V4L2_CID_VBLANK:
- + ret = imx477_set_frame_length(imx477,
- + imx477->mode->height + ctrl->val);
- + break;
- + case V4L2_CID_HBLANK:
- + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
- + imx477->mode->width + ctrl->val);
- + break;
- + default:
- + dev_info(&client->dev,
- + "ctrl(id:0x%x,val:0x%x) is not handled\n",
- + ctrl->id, ctrl->val);
- + ret = -EINVAL;
- + break;
- + }
- +
- + pm_runtime_put(&client->dev);
- +
- + return ret;
- +}
- +
- +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
- + .s_ctrl = imx477_set_ctrl,
- +};
- +
- +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
- + struct v4l2_subdev_pad_config *cfg,
- + struct v4l2_subdev_mbus_code_enum *code)
- +{
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + if (code->pad >= NUM_PADS)
- + return -EINVAL;
- +
- + if (code->pad == IMAGE_PAD) {
- + if (code->index >= (ARRAY_SIZE(codes) / 4))
- + return -EINVAL;
- +
- + code->code = imx477_get_format_code(imx477,
- + codes[code->index * 4]);
- + } else {
- + if (code->index > 0)
- + return -EINVAL;
- +
- + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
- + }
- +
- + return 0;
- +}
- +
- +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
- + struct v4l2_subdev_pad_config *cfg,
- + struct v4l2_subdev_frame_size_enum *fse)
- +{
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + if (fse->pad >= NUM_PADS)
- + return -EINVAL;
- +
- + if (fse->pad == IMAGE_PAD) {
- + const struct imx477_mode *mode_list;
- + unsigned int num_modes;
- +
- + get_mode_table(fse->code, &mode_list, &num_modes);
- +
- + if (fse->index >= num_modes)
- + return -EINVAL;
- +
- + if (fse->code != imx477_get_format_code(imx477, fse->code))
- + return -EINVAL;
- +
- + fse->min_width = mode_list[fse->index].width;
- + fse->max_width = fse->min_width;
- + fse->min_height = mode_list[fse->index].height;
- + fse->max_height = fse->min_height;
- + } else {
- + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
- + return -EINVAL;
- +
- + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
- + fse->max_width = fse->min_width;
- + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
- + fse->max_height = fse->min_height;
- + }
- +
- + return 0;
- +}
- +
- +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
- +{
- + fmt->colorspace = V4L2_COLORSPACE_RAW;
- + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
- + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
- + fmt->colorspace,
- + fmt->ycbcr_enc);
- + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
- +}
- +
- +static void imx477_update_image_pad_format(struct imx477 *imx477,
- + const struct imx477_mode *mode,
- + struct v4l2_subdev_format *fmt)
- +{
- + fmt->format.width = mode->width;
- + fmt->format.height = mode->height;
- + fmt->format.field = V4L2_FIELD_NONE;
- + imx477_reset_colorspace(&fmt->format);
- +}
- +
- +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
- +{
- + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
- + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
- + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
- + fmt->format.field = V4L2_FIELD_NONE;
- +}
- +
- +static int imx477_get_pad_format(struct v4l2_subdev *sd,
- + struct v4l2_subdev_pad_config *cfg,
- + struct v4l2_subdev_format *fmt)
- +{
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + if (fmt->pad >= NUM_PADS)
- + return -EINVAL;
- +
- + mutex_lock(&imx477->mutex);
- +
- + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
- + struct v4l2_mbus_framefmt *try_fmt =
- + v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
- + /* update the code which could change due to vflip or hflip: */
- + try_fmt->code = fmt->pad == IMAGE_PAD ?
- + imx477_get_format_code(imx477, try_fmt->code) :
- + MEDIA_BUS_FMT_SENSOR_DATA;
- + fmt->format = *try_fmt;
- + } else {
- + if (fmt->pad == IMAGE_PAD) {
- + imx477_update_image_pad_format(imx477, imx477->mode,
- + fmt);
- + fmt->format.code =
- + imx477_get_format_code(imx477, imx477->fmt_code);
- + } else {
- + imx477_update_metadata_pad_format(fmt);
- + }
- + }
- +
- + mutex_unlock(&imx477->mutex);
- + return 0;
- +}
- +
- +static
- +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
- + const struct v4l2_fract *timeperframe)
- +{
- + u64 frame_length;
- +
- + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
- + do_div(frame_length,
- + (u64)timeperframe->denominator * mode->line_length_pix);
- +
- + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
- + frame_length = IMX477_FRAME_LENGTH_MAX;
- +
- + return max_t(unsigned int, frame_length, mode->height);
- +}
- +
- +static void imx477_set_framing_limits(struct imx477 *imx477)
- +{
- + unsigned int frm_length_min, frm_length_default, hblank_min;
- + const struct imx477_mode *mode = imx477->mode;
- +
- + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
- + frm_length_default =
- + imx477_get_frame_length(mode, &mode->timeperframe_default);
- +
- + /* Default to no long exposure multiplier. */
- + imx477->long_exp_shift = 0;
- +
- + /* Update limits and set FPS to default */
- + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
- + ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
- + IMX477_FRAME_LENGTH_MAX) - mode->height,
- + 1, frm_length_default - mode->height);
- +
- + /* Setting this will adjust the exposure limits as well. */
- + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
- +
- + hblank_min = mode->line_length_pix - mode->width;
- + __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
- + IMX477_LINE_LENGTH_MAX, 1, hblank_min);
- + __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
- +}
- +
- +static int imx477_set_pad_format(struct v4l2_subdev *sd,
- + struct v4l2_subdev_pad_config *cfg,
- + struct v4l2_subdev_format *fmt)
- +{
- + struct v4l2_mbus_framefmt *framefmt;
- + const struct imx477_mode *mode;
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + if (fmt->pad >= NUM_PADS)
- + return -EINVAL;
- +
- + mutex_lock(&imx477->mutex);
- +
- + if (fmt->pad == IMAGE_PAD) {
- + const struct imx477_mode *mode_list;
- + unsigned int num_modes;
- +
- + /* Bayer order varies with flips */
- + fmt->format.code = imx477_get_format_code(imx477,
- + fmt->format.code);
- +
- + get_mode_table(fmt->format.code, &mode_list, &num_modes);
- +
- + mode = v4l2_find_nearest_size(mode_list,
- + num_modes,
- + width, height,
- + fmt->format.width,
- + fmt->format.height);
- + imx477_update_image_pad_format(imx477, mode, fmt);
- + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
- + framefmt = v4l2_subdev_get_try_format(sd, cfg,
- + fmt->pad);
- + *framefmt = fmt->format;
- + } else if (imx477->mode != mode) {
- + imx477->mode = mode;
- + imx477->fmt_code = fmt->format.code;
- + imx477_set_framing_limits(imx477);
- + }
- + } else {
- + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
- + framefmt = v4l2_subdev_get_try_format(sd, cfg,
- + fmt->pad);
- + *framefmt = fmt->format;
- + } else {
- + /* Only one embedded data mode is supported */
- + imx477_update_metadata_pad_format(fmt);
- + }
- + }
- +
- + mutex_unlock(&imx477->mutex);
- +
- + return 0;
- +}
- +
- +static const struct v4l2_rect *
- +__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
- + unsigned int pad, enum v4l2_subdev_format_whence which)
- +{
- + switch (which) {
- + case V4L2_SUBDEV_FORMAT_TRY:
- + return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
- + case V4L2_SUBDEV_FORMAT_ACTIVE:
- + return &imx477->mode->crop;
- + }
- +
- + return NULL;
- +}
- +
- +static int imx477_get_selection(struct v4l2_subdev *sd,
- + struct v4l2_subdev_pad_config *cfg,
- + struct v4l2_subdev_selection *sel)
- +{
- + switch (sel->target) {
- + case V4L2_SEL_TGT_CROP: {
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + mutex_lock(&imx477->mutex);
- + sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
- + sel->which);
- + mutex_unlock(&imx477->mutex);
- +
- + return 0;
- + }
- +
- + case V4L2_SEL_TGT_NATIVE_SIZE:
- + sel->r.left = 0;
- + sel->r.top = 0;
- + sel->r.width = IMX477_NATIVE_WIDTH;
- + sel->r.height = IMX477_NATIVE_HEIGHT;
- +
- + return 0;
- +
- + case V4L2_SEL_TGT_CROP_DEFAULT:
- + case V4L2_SEL_TGT_CROP_BOUNDS:
- + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
- + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
- + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
- + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
- +
- + return 0;
- + }
- +
- + return -EINVAL;
- +}
- +
- +/* Start streaming */
- +static int imx477_start_streaming(struct imx477 *imx477)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + const struct imx477_reg_list *reg_list;
- + const struct imx477_reg_list *extra_regs;
- + int ret;
- +
- + if (!imx477->common_regs_written) {
- + ret = imx477_write_regs(imx477, mode_common_regs,
- + ARRAY_SIZE(mode_common_regs));
- + if (!ret) {
- + extra_regs = &imx477->compatible_data->extra_regs;
- + ret = imx477_write_regs(imx477, extra_regs->regs,
- + extra_regs->num_of_regs);
- + }
- +
- + if (ret) {
- + dev_err(&client->dev, "%s failed to set common settings\n",
- + __func__);
- + return ret;
- + }
- + imx477->common_regs_written = true;
- + }
- +
- + /* Apply default values of current mode */
- + reg_list = &imx477->mode->reg_list;
- + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
- + if (ret) {
- + dev_err(&client->dev, "%s failed to set mode\n", __func__);
- + return ret;
- + }
- +
- + /* Set on-sensor DPC. */
- + imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
- + imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
- +
- + /* Set vsync trigger mode */
- + if (trigger_mode != 0) {
- + /* trigger_mode == 1 for source, 2 for sink */
- + const u32 val = (trigger_mode == 1) ? 1 : 0;
- +
- + imx477_write_reg(imx477, IMX477_REG_MC_MODE,
- + IMX477_REG_VALUE_08BIT, 1);
- + imx477_write_reg(imx477, IMX477_REG_MS_SEL,
- + IMX477_REG_VALUE_08BIT, val);
- + imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
- + IMX477_REG_VALUE_08BIT, val);
- + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
- + IMX477_REG_VALUE_08BIT, val);
- + }
- +
- + /* Apply customized values from user */
- + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
- + if (ret)
- + return ret;
- +
- + /* set stream on register */
- + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
- + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
- +}
- +
- +/* Stop streaming */
- +static void imx477_stop_streaming(struct imx477 *imx477)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + int ret;
- +
- + /* set stream off register */
- + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
- + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
- + if (ret)
- + dev_err(&client->dev, "%s failed to set stream\n", __func__);
- +}
- +
- +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
- +{
- + struct imx477 *imx477 = to_imx477(sd);
- + struct i2c_client *client = v4l2_get_subdevdata(sd);
- + int ret = 0;
- +
- + mutex_lock(&imx477->mutex);
- + if (imx477->streaming == enable) {
- + mutex_unlock(&imx477->mutex);
- + return 0;
- + }
- +
- + if (enable) {
- + ret = pm_runtime_get_sync(&client->dev);
- + if (ret < 0) {
- + pm_runtime_put_noidle(&client->dev);
- + goto err_unlock;
- + }
- +
- + /*
- + * Apply default & customized values
- + * and then start streaming.
- + */
- + ret = imx477_start_streaming(imx477);
- + if (ret)
- + goto err_rpm_put;
- + } else {
- + imx477_stop_streaming(imx477);
- + pm_runtime_put(&client->dev);
- + }
- +
- + imx477->streaming = enable;
- +
- + /* vflip and hflip cannot change during streaming */
- + __v4l2_ctrl_grab(imx477->vflip, enable);
- + __v4l2_ctrl_grab(imx477->hflip, enable);
- +
- + mutex_unlock(&imx477->mutex);
- +
- + return ret;
- +
- +err_rpm_put:
- + pm_runtime_put(&client->dev);
- +err_unlock:
- + mutex_unlock(&imx477->mutex);
- +
- + return ret;
- +}
- +
- +/* Power/clock management functions */
- +static int imx477_power_on(struct device *dev)
- +{
- + struct i2c_client *client = to_i2c_client(dev);
- + struct v4l2_subdev *sd = i2c_get_clientdata(client);
- + struct imx477 *imx477 = to_imx477(sd);
- + int ret;
- +
- + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
- + imx477->supplies);
- + if (ret) {
- + dev_err(&client->dev, "%s: failed to enable regulators\n",
- + __func__);
- + return ret;
- + }
- +
- + ret = clk_prepare_enable(imx477->xclk);
- + if (ret) {
- + dev_err(&client->dev, "%s: failed to enable clock\n",
- + __func__);
- + goto reg_off;
- + }
- +
- + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
- + usleep_range(IMX477_XCLR_MIN_DELAY_US,
- + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
- +
- + return 0;
- +
- +reg_off:
- + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
- + return ret;
- +}
- +
- +static int imx477_power_off(struct device *dev)
- +{
- + struct i2c_client *client = to_i2c_client(dev);
- + struct v4l2_subdev *sd = i2c_get_clientdata(client);
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
- + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
- + clk_disable_unprepare(imx477->xclk);
- +
- + /* Force reprogramming of the common registers when powered up again. */
- + imx477->common_regs_written = false;
- +
- + return 0;
- +}
- +
- +static int __maybe_unused imx477_suspend(struct device *dev)
- +{
- + struct i2c_client *client = to_i2c_client(dev);
- + struct v4l2_subdev *sd = i2c_get_clientdata(client);
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + if (imx477->streaming)
- + imx477_stop_streaming(imx477);
- +
- + return 0;
- +}
- +
- +static int __maybe_unused imx477_resume(struct device *dev)
- +{
- + struct i2c_client *client = to_i2c_client(dev);
- + struct v4l2_subdev *sd = i2c_get_clientdata(client);
- + struct imx477 *imx477 = to_imx477(sd);
- + int ret;
- +
- + if (imx477->streaming) {
- + ret = imx477_start_streaming(imx477);
- + if (ret)
- + goto error;
- + }
- +
- + return 0;
- +
- +error:
- + imx477_stop_streaming(imx477);
- + imx477->streaming = 0;
- + return ret;
- +}
- +
- +static int imx477_get_regulators(struct imx477 *imx477)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + unsigned int i;
- +
- + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
- + imx477->supplies[i].supply = imx477_supply_name[i];
- +
- + return devm_regulator_bulk_get(&client->dev,
- + IMX477_NUM_SUPPLIES,
- + imx477->supplies);
- +}
- +
- +/* Verify chip ID */
- +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
- +{
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + int ret;
- + u32 val;
- +
- + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
- + IMX477_REG_VALUE_16BIT, &val);
- + if (ret) {
- + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
- + expected_id, ret);
- + return ret;
- + }
- +
- + if (val != expected_id) {
- + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
- + expected_id, val);
- + return -EIO;
- + }
- +
- + dev_info(&client->dev, "Device found is imx%x\n", val);
- +
- + return 0;
- +}
- +
- +static const struct v4l2_subdev_core_ops imx477_core_ops = {
- + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
- + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
- +};
- +
- +static const struct v4l2_subdev_video_ops imx477_video_ops = {
- + .s_stream = imx477_set_stream,
- +};
- +
- +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
- + .enum_mbus_code = imx477_enum_mbus_code,
- + .get_fmt = imx477_get_pad_format,
- + .set_fmt = imx477_set_pad_format,
- + .get_selection = imx477_get_selection,
- + .enum_frame_size = imx477_enum_frame_size,
- +};
- +
- +static const struct v4l2_subdev_ops imx477_subdev_ops = {
- + .core = &imx477_core_ops,
- + .video = &imx477_video_ops,
- + .pad = &imx477_pad_ops,
- +};
- +
- +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
- + .open = imx477_open,
- +};
- +
- +/* Initialize control handlers */
- +static int imx477_init_controls(struct imx477 *imx477)
- +{
- + struct v4l2_ctrl_handler *ctrl_hdlr;
- + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
- + struct v4l2_fwnode_device_properties props;
- + unsigned int i;
- + int ret;
- +
- + ctrl_hdlr = &imx477->ctrl_handler;
- + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
- + if (ret)
- + return ret;
- +
- + mutex_init(&imx477->mutex);
- + ctrl_hdlr->lock = &imx477->mutex;
- +
- + /* By default, PIXEL_RATE is read only */
- + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_PIXEL_RATE,
- + IMX477_PIXEL_RATE,
- + IMX477_PIXEL_RATE, 1,
- + IMX477_PIXEL_RATE);
- +
- + /*
- + * Create the controls here, but mode specific limits are setup
- + * in the imx477_set_framing_limits() call below.
- + */
- + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
- + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
- +
- + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_EXPOSURE,
- + IMX477_EXPOSURE_MIN,
- + IMX477_EXPOSURE_MAX,
- + IMX477_EXPOSURE_STEP,
- + IMX477_EXPOSURE_DEFAULT);
- +
- + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
- + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
- + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
- +
- + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
- + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
- + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
- +
- + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_HFLIP, 0, 1, 1, 0);
- + if (imx477->hflip)
- + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
- +
- + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_VFLIP, 0, 1, 1, 0);
- + if (imx477->vflip)
- + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
- +
- + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_TEST_PATTERN,
- + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
- + 0, 0, imx477_test_pattern_menu);
- + for (i = 0; i < 4; i++) {
- + /*
- + * The assumption is that
- + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
- + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
- + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
- + */
- + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
- + V4L2_CID_TEST_PATTERN_RED + i,
- + IMX477_TEST_PATTERN_COLOUR_MIN,
- + IMX477_TEST_PATTERN_COLOUR_MAX,
- + IMX477_TEST_PATTERN_COLOUR_STEP,
- + IMX477_TEST_PATTERN_COLOUR_MAX);
- + /* The "Solid color" pattern is white by default */
- + }
- +
- + if (ctrl_hdlr->error) {
- + ret = ctrl_hdlr->error;
- + dev_err(&client->dev, "%s control init failed (%d)\n",
- + __func__, ret);
- + goto error;
- + }
- +
- + ret = v4l2_fwnode_device_parse(&client->dev, &props);
- + if (ret)
- + goto error;
- +
- + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
- + &props);
- + if (ret)
- + goto error;
- +
- + imx477->sd.ctrl_handler = ctrl_hdlr;
- +
- + /* Setup exposure and frame/line length limits. */
- + imx477_set_framing_limits(imx477);
- +
- + return 0;
- +
- +error:
- + v4l2_ctrl_handler_free(ctrl_hdlr);
- + mutex_destroy(&imx477->mutex);
- +
- + return ret;
- +}
- +
- +static void imx477_free_controls(struct imx477 *imx477)
- +{
- + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
- + mutex_destroy(&imx477->mutex);
- +}
- +
- +static int imx477_check_hwcfg(struct device *dev)
- +{
- + struct fwnode_handle *endpoint;
- + struct v4l2_fwnode_endpoint ep_cfg = {
- + .bus_type = V4L2_MBUS_CSI2_DPHY
- + };
- + int ret = -EINVAL;
- +
- + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
- + if (!endpoint) {
- + dev_err(dev, "endpoint node not found\n");
- + return -EINVAL;
- + }
- +
- + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
- + dev_err(dev, "could not parse endpoint\n");
- + goto error_out;
- + }
- +
- + /* Check the number of MIPI CSI2 data lanes */
- + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
- + dev_err(dev, "only 2 data lanes are currently supported\n");
- + goto error_out;
- + }
- +
- + /* Check the link frequency set in device tree */
- + if (!ep_cfg.nr_of_link_frequencies) {
- + dev_err(dev, "link-frequency property not found in DT\n");
- + goto error_out;
- + }
- +
- + if (ep_cfg.nr_of_link_frequencies != 1 ||
- + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
- + dev_err(dev, "Link frequency not supported: %lld\n",
- + ep_cfg.link_frequencies[0]);
- + goto error_out;
- + }
- +
- + ret = 0;
- +
- +error_out:
- + v4l2_fwnode_endpoint_free(&ep_cfg);
- + fwnode_handle_put(endpoint);
- +
- + return ret;
- +}
- +
- +static const struct imx477_compatible_data imx477_compatible = {
- + .chip_id = IMX477_CHIP_ID,
- + .extra_regs = {
- + .num_of_regs = 0,
- + .regs = NULL
- + }
- +};
- +
- +static const struct imx477_reg imx378_regs[] = {
- + {0x3e35, 0x01},
- + {0x4421, 0x08},
- + {0x3ff9, 0x00},
- +};
- +
- +static const struct imx477_compatible_data imx378_compatible = {
- + .chip_id = IMX378_CHIP_ID,
- + .extra_regs = {
- + .num_of_regs = ARRAY_SIZE(imx378_regs),
- + .regs = imx378_regs
- + }
- +};
- +
- +static const struct of_device_id imx477_dt_ids[] = {
- + { .compatible = "sony,imx477", .data = &imx477_compatible },
- + { .compatible = "sony,imx378", .data = &imx378_compatible },
- + { /* sentinel */ }
- +};
- +
- +static int imx477_probe(struct i2c_client *client)
- +{
- + struct device *dev = &client->dev;
- + struct imx477 *imx477;
- + const struct of_device_id *match;
- + int ret;
- +
- + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
- + if (!imx477)
- + return -ENOMEM;
- +
- + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
- +
- + match = of_match_device(imx477_dt_ids, dev);
- + if (!match)
- + return -ENODEV;
- + imx477->compatible_data =
- + (const struct imx477_compatible_data *)match->data;
- +
- + /* Check the hardware configuration in device tree */
- + if (imx477_check_hwcfg(dev))
- + return -EINVAL;
- +
- + /* Get system clock (xclk) */
- + imx477->xclk = devm_clk_get(dev, NULL);
- + if (IS_ERR(imx477->xclk)) {
- + dev_err(dev, "failed to get xclk\n");
- + return PTR_ERR(imx477->xclk);
- + }
- +
- + imx477->xclk_freq = clk_get_rate(imx477->xclk);
- + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
- + dev_err(dev, "xclk frequency not supported: %d Hz\n",
- + imx477->xclk_freq);
- + return -EINVAL;
- + }
- +
- + ret = imx477_get_regulators(imx477);
- + if (ret) {
- + dev_err(dev, "failed to get regulators\n");
- + return ret;
- + }
- +
- + /* Request optional enable pin */
- + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
- + GPIOD_OUT_HIGH);
- +
- + /*
- + * The sensor must be powered for imx477_identify_module()
- + * to be able to read the CHIP_ID register
- + */
- + ret = imx477_power_on(dev);
- + if (ret)
- + return ret;
- +
- + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
- + if (ret)
- + goto error_power_off;
- +
- + /* Initialize default format */
- + imx477_set_default_format(imx477);
- +
- + /* Enable runtime PM and turn off the device */
- + pm_runtime_set_active(dev);
- + pm_runtime_enable(dev);
- + pm_runtime_idle(dev);
- +
- + /* This needs the pm runtime to be registered. */
- + ret = imx477_init_controls(imx477);
- + if (ret)
- + goto error_power_off;
- +
- + /* Initialize subdev */
- + imx477->sd.internal_ops = &imx477_internal_ops;
- + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
- + V4L2_SUBDEV_FL_HAS_EVENTS;
- + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
- +
- + /* Initialize source pads */
- + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
- + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
- +
- + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
- + if (ret) {
- + dev_err(dev, "failed to init entity pads: %d\n", ret);
- + goto error_handler_free;
- + }
- +
- + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
- + if (ret < 0) {
- + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
- + goto error_media_entity;
- + }
- +
- + return 0;
- +
- +error_media_entity:
- + media_entity_cleanup(&imx477->sd.entity);
- +
- +error_handler_free:
- + imx477_free_controls(imx477);
- +
- +error_power_off:
- + pm_runtime_disable(&client->dev);
- + pm_runtime_set_suspended(&client->dev);
- + imx477_power_off(&client->dev);
- +
- + return ret;
- +}
- +
- +static void imx477_remove(struct i2c_client *client)
- +{
- + struct v4l2_subdev *sd = i2c_get_clientdata(client);
- + struct imx477 *imx477 = to_imx477(sd);
- +
- + v4l2_async_unregister_subdev(sd);
- + media_entity_cleanup(&sd->entity);
- + imx477_free_controls(imx477);
- +
- + pm_runtime_disable(&client->dev);
- + if (!pm_runtime_status_suspended(&client->dev))
- + imx477_power_off(&client->dev);
- + pm_runtime_set_suspended(&client->dev);
- +}
- +
- +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
- +
- +static const struct dev_pm_ops imx477_pm_ops = {
- + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
- + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
- +};
- +
- +static struct i2c_driver imx477_i2c_driver = {
- + .driver = {
- + .name = "imx477",
- + .of_match_table = imx477_dt_ids,
- + .pm = &imx477_pm_ops,
- + },
- + .probe_new = imx477_probe,
- + .remove = imx477_remove,
- +};
- +
- +module_i2c_driver(imx477_i2c_driver);
- +
- +MODULE_AUTHOR("Naushir Patuck <[email protected]>");
- +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
- +MODULE_LICENSE("GPL v2");
|