950-0281-media-i2c-Add-driver-for-Sony-IMX477-sensor.patch 54 KB

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