imx283.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * V4L2 Support for the IMX283
  4. *
  5. * Diagonal 15.86 mm (Type 1) CMOS Image Sensor with Square Pixel for Color
  6. * Cameras.
  7. *
  8. * Copyright (C) 2024 Ideas on Board Oy.
  9. *
  10. * Based on Sony IMX283 driver prepared by Will Whang
  11. *
  12. * Based on Sony imx477 camera driver
  13. * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
  14. */
  15. #include <linux/array_size.h>
  16. #include <linux/bitops.h>
  17. #include <linux/container_of.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/i2c.h>
  23. #include <linux/minmax.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/property.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/types.h>
  30. #include <linux/units.h>
  31. #include <media/v4l2-cci.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-fwnode.h>
  35. #include <media/v4l2-mediabus.h>
  36. /* Chip ID */
  37. #define IMX283_REG_CHIP_ID CCI_REG8(0x3000)
  38. #define IMX283_CHIP_ID 0x0b // Default power on state
  39. #define IMX283_REG_STANDBY CCI_REG8(0x3000)
  40. #define IMX283_ACTIVE 0
  41. #define IMX283_STANDBY BIT(0)
  42. #define IMX283_STBLOGIC BIT(1)
  43. #define IMX283_STBMIPI BIT(2)
  44. #define IMX283_STBDV BIT(3)
  45. #define IMX283_SLEEP BIT(4)
  46. #define IMX283_REG_CLAMP CCI_REG8(0x3001)
  47. #define IMX283_CLPSQRST BIT(4)
  48. #define IMX283_REG_PLSTMG08 CCI_REG8(0x3003)
  49. #define IMX283_PLSTMG08_VAL 0x77
  50. #define IMX283_REG_MDSEL1 CCI_REG8(0x3004)
  51. #define IMX283_REG_MDSEL2 CCI_REG8(0x3005)
  52. #define IMX283_REG_MDSEL3 CCI_REG8(0x3006)
  53. #define IMX283_MDSEL3_VCROP_EN BIT(5)
  54. #define IMX283_REG_MDSEL4 CCI_REG8(0x3007)
  55. #define IMX283_MDSEL4_VCROP_EN (BIT(4) | BIT(6))
  56. #define IMX283_REG_SVR CCI_REG16_LE(0x3009)
  57. #define IMX283_REG_HTRIMMING CCI_REG8(0x300b)
  58. #define IMX283_MDVREV BIT(0) /* VFLIP */
  59. #define IMX283_HTRIMMING_EN BIT(4)
  60. #define IMX283_REG_VWINPOS CCI_REG16_LE(0x300f)
  61. #define IMX283_REG_VWIDCUT CCI_REG16_LE(0x3011)
  62. #define IMX283_REG_MDSEL7 CCI_REG16_LE(0x3013)
  63. /* CSI Clock Configuration */
  64. #define IMX283_REG_TCLKPOST CCI_REG8(0x3018)
  65. #define IMX283_REG_THSPREPARE CCI_REG8(0x301a)
  66. #define IMX283_REG_THSZERO CCI_REG8(0x301c)
  67. #define IMX283_REG_THSTRAIL CCI_REG8(0x301e)
  68. #define IMX283_REG_TCLKTRAIL CCI_REG8(0x3020)
  69. #define IMX283_REG_TCLKPREPARE CCI_REG8(0x3022)
  70. #define IMX283_REG_TCLKZERO CCI_REG16_LE(0x3024)
  71. #define IMX283_REG_TLPX CCI_REG8(0x3026)
  72. #define IMX283_REG_THSEXIT CCI_REG8(0x3028)
  73. #define IMX283_REG_TCLKPRE CCI_REG8(0x302a)
  74. #define IMX283_REG_SYSMODE CCI_REG8(0x3104)
  75. #define IMX283_REG_Y_OUT_SIZE CCI_REG16_LE(0x302f)
  76. #define IMX283_REG_WRITE_VSIZE CCI_REG16_LE(0x3031)
  77. #define IMX283_REG_OB_SIZE_V CCI_REG8(0x3033)
  78. /* HMAX internal HBLANK */
  79. #define IMX283_REG_HMAX CCI_REG16_LE(0x3036)
  80. #define IMX283_HMAX_MAX (BIT(16) - 1)
  81. /* VMAX internal VBLANK */
  82. #define IMX283_REG_VMAX CCI_REG24_LE(0x3038)
  83. #define IMX283_VMAX_MAX (BIT(16) - 1)
  84. /* SHR internal */
  85. #define IMX283_REG_SHR CCI_REG16_LE(0x303b)
  86. #define IMX283_SHR_MIN 11
  87. /*
  88. * Analog gain control
  89. * Gain [dB] = -20log{(2048 - value [10:0]) /2048}
  90. * Range: 0dB to approximately +27dB
  91. */
  92. #define IMX283_REG_ANALOG_GAIN CCI_REG16_LE(0x3042)
  93. #define IMX283_ANA_GAIN_MIN 0
  94. #define IMX283_ANA_GAIN_MAX 1957
  95. #define IMX283_ANA_GAIN_STEP 1
  96. #define IMX283_ANA_GAIN_DEFAULT 0x0
  97. /*
  98. * Digital gain control
  99. * Gain [dB] = value * 6
  100. * Range: 0dB to +18db
  101. */
  102. #define IMX283_REG_DIGITAL_GAIN CCI_REG8(0x3044)
  103. #define IMX283_DGTL_GAIN_MIN 0
  104. #define IMX283_DGTL_GAIN_MAX 3
  105. #define IMX283_DGTL_GAIN_DEFAULT 0
  106. #define IMX283_DGTL_GAIN_STEP 1
  107. #define IMX283_REG_HTRIMMING_START CCI_REG16_LE(0x3058)
  108. #define IMX283_REG_HTRIMMING_END CCI_REG16_LE(0x305a)
  109. #define IMX283_REG_MDSEL18 CCI_REG16_LE(0x30f6)
  110. /* Master Mode Operation Control */
  111. #define IMX283_REG_XMSTA CCI_REG8(0x3105)
  112. #define IMX283_XMSTA BIT(0)
  113. #define IMX283_REG_SYNCDRV CCI_REG8(0x3107)
  114. #define IMX283_SYNCDRV_XHS_XVS (0xa0 | 0x02)
  115. #define IMX283_SYNCDRV_HIZ (0xa0 | 0x03)
  116. /* PLL Standby */
  117. #define IMX283_REG_STBPL CCI_REG8(0x320b)
  118. #define IMX283_STBPL_NORMAL 0x00
  119. #define IMX283_STBPL_STANDBY 0x03
  120. /* Input Frequency Setting */
  121. #define IMX283_REG_PLRD1 CCI_REG8(0x36c1)
  122. #define IMX283_REG_PLRD2 CCI_REG16_LE(0x36c2)
  123. #define IMX283_REG_PLRD3 CCI_REG8(0x36f7)
  124. #define IMX283_REG_PLRD4 CCI_REG8(0x36f8)
  125. #define IMX283_REG_PLSTMG02 CCI_REG8(0x36aa)
  126. #define IMX283_PLSTMG02_VAL 0x00
  127. #define IMX283_REG_EBD_X_OUT_SIZE CCI_REG16_LE(0x3a54)
  128. /* Test pattern generator */
  129. #define IMX283_REG_TPG_CTRL CCI_REG8(0x3156)
  130. #define IMX283_TPG_CTRL_CLKEN BIT(0)
  131. #define IMX283_TPG_CTRL_PATEN BIT(4)
  132. #define IMX283_REG_TPG_PAT CCI_REG8(0x3157)
  133. #define IMX283_TPG_PAT_ALL_000 0x00
  134. #define IMX283_TPG_PAT_ALL_FFF 0x01
  135. #define IMX283_TPG_PAT_ALL_555 0x02
  136. #define IMX283_TPG_PAT_ALL_AAA 0x03
  137. #define IMX283_TPG_PAT_H_COLOR_BARS 0x0a
  138. #define IMX283_TPG_PAT_V_COLOR_BARS 0x0b
  139. /* Exposure control */
  140. #define IMX283_EXPOSURE_MIN 52
  141. #define IMX283_EXPOSURE_STEP 1
  142. #define IMX283_EXPOSURE_DEFAULT 1000
  143. #define IMX283_EXPOSURE_MAX 49865
  144. #define IMAGE_PAD 0
  145. #define IMX283_XCLR_MIN_DELAY_US (1 * USEC_PER_MSEC)
  146. #define IMX283_XCLR_DELAY_RANGE_US (1 * USEC_PER_MSEC)
  147. /* IMX283 native and active pixel array size. */
  148. static const struct v4l2_rect imx283_native_area = {
  149. .top = 0,
  150. .left = 0,
  151. .width = 5592,
  152. .height = 3710,
  153. };
  154. static const struct v4l2_rect imx283_active_area = {
  155. .top = 40,
  156. .left = 108,
  157. .width = 5472,
  158. .height = 3648,
  159. };
  160. struct imx283_reg_list {
  161. unsigned int num_of_regs;
  162. const struct cci_reg_sequence *regs;
  163. };
  164. /* Mode : resolution and related config values */
  165. struct imx283_mode {
  166. unsigned int mode;
  167. /* Bits per pixel */
  168. unsigned int bpp;
  169. /* Frame width */
  170. unsigned int width;
  171. /* Frame height */
  172. unsigned int height;
  173. /*
  174. * Minimum horizontal timing in pixel-units
  175. *
  176. * Note that HMAX is written in 72MHz units, and the datasheet assumes a
  177. * 720MHz link frequency. Convert datasheet values with the following:
  178. *
  179. * For 12 bpp modes (480Mbps) convert with:
  180. * hmax = [hmax in 72MHz units] * 480 / 72
  181. *
  182. * For 10 bpp modes (576Mbps) convert with:
  183. * hmax = [hmax in 72MHz units] * 576 / 72
  184. */
  185. u32 min_hmax;
  186. /* minimum V-timing in lines */
  187. u32 min_vmax;
  188. /* default H-timing */
  189. u32 default_hmax;
  190. /* default V-timing */
  191. u32 default_vmax;
  192. /* minimum SHR */
  193. u32 min_shr;
  194. /*
  195. * Per-mode vertical crop constants used to calculate values
  196. * of IMX283REG_WIDCUT and IMX283_REG_VWINPOS.
  197. */
  198. u32 veff;
  199. u32 vst;
  200. u32 vct;
  201. /* Horizontal and vertical binning ratio */
  202. u8 hbin_ratio;
  203. u8 vbin_ratio;
  204. /* Optical Blanking */
  205. u32 horizontal_ob;
  206. u32 vertical_ob;
  207. /* Analog crop rectangle. */
  208. struct v4l2_rect crop;
  209. };
  210. struct imx283_input_frequency {
  211. unsigned int mhz;
  212. unsigned int reg_count;
  213. struct cci_reg_sequence regs[4];
  214. };
  215. static const struct imx283_input_frequency imx283_frequencies[] = {
  216. {
  217. .mhz = 6 * HZ_PER_MHZ,
  218. .reg_count = 4,
  219. .regs = {
  220. { IMX283_REG_PLRD1, 0x00 },
  221. { IMX283_REG_PLRD2, 0x00f0 },
  222. { IMX283_REG_PLRD3, 0x00 },
  223. { IMX283_REG_PLRD4, 0xc0 },
  224. },
  225. },
  226. {
  227. .mhz = 12 * HZ_PER_MHZ,
  228. .reg_count = 4,
  229. .regs = {
  230. { IMX283_REG_PLRD1, 0x01 },
  231. { IMX283_REG_PLRD2, 0x00f0 },
  232. { IMX283_REG_PLRD3, 0x01 },
  233. { IMX283_REG_PLRD4, 0xc0 },
  234. },
  235. },
  236. {
  237. .mhz = 18 * HZ_PER_MHZ,
  238. .reg_count = 4,
  239. .regs = {
  240. { IMX283_REG_PLRD1, 0x01 },
  241. { IMX283_REG_PLRD2, 0x00a0 },
  242. { IMX283_REG_PLRD3, 0x01 },
  243. { IMX283_REG_PLRD4, 0x80 },
  244. },
  245. },
  246. {
  247. .mhz = 24 * HZ_PER_MHZ,
  248. .reg_count = 4,
  249. .regs = {
  250. { IMX283_REG_PLRD1, 0x02 },
  251. { IMX283_REG_PLRD2, 0x00f0 },
  252. { IMX283_REG_PLRD3, 0x02 },
  253. { IMX283_REG_PLRD4, 0xc0 },
  254. },
  255. },
  256. };
  257. enum imx283_modes {
  258. IMX283_MODE_0,
  259. IMX283_MODE_1,
  260. IMX283_MODE_1A,
  261. IMX283_MODE_1S,
  262. IMX283_MODE_2,
  263. IMX283_MODE_2A,
  264. IMX283_MODE_3,
  265. IMX283_MODE_4,
  266. IMX283_MODE_5,
  267. IMX283_MODE_6,
  268. };
  269. struct imx283_readout_mode {
  270. u8 mdsel1;
  271. u8 mdsel2;
  272. u8 mdsel3;
  273. u8 mdsel4;
  274. };
  275. static const struct imx283_readout_mode imx283_readout_modes[] = {
  276. /* All pixel scan modes */
  277. [IMX283_MODE_0] = { 0x04, 0x03, 0x10, 0x00 }, /* 12 bit */
  278. [IMX283_MODE_1] = { 0x04, 0x01, 0x00, 0x00 }, /* 10 bit */
  279. [IMX283_MODE_1A] = { 0x04, 0x01, 0x20, 0x50 }, /* 10 bit */
  280. [IMX283_MODE_1S] = { 0x04, 0x41, 0x20, 0x50 }, /* 10 bit */
  281. /* Horizontal / Vertical 2/2-line binning */
  282. [IMX283_MODE_2] = { 0x0d, 0x11, 0x50, 0x00 }, /* 12 bit */
  283. [IMX283_MODE_2A] = { 0x0d, 0x11, 0x70, 0x50 }, /* 12 bit */
  284. /* Horizontal / Vertical 3/3-line binning */
  285. [IMX283_MODE_3] = { 0x1e, 0x18, 0x10, 0x00 }, /* 12 bit */
  286. /* Vertical 2/9 subsampling, horizontal 3 binning cropping */
  287. [IMX283_MODE_4] = { 0x29, 0x18, 0x30, 0x50 }, /* 12 bit */
  288. /* Vertical 2/19 subsampling binning, horizontal 3 binning */
  289. [IMX283_MODE_5] = { 0x2d, 0x18, 0x10, 0x00 }, /* 12 bit */
  290. /* Vertical 2 binning horizontal 2/4, subsampling 16:9 cropping */
  291. [IMX283_MODE_6] = { 0x18, 0x21, 0x00, 0x09 }, /* 10 bit */
  292. /*
  293. * New modes should make sure the offset period is complied.
  294. * See imx283_exposure() for reference.
  295. */
  296. };
  297. static const struct cci_reg_sequence mipi_data_rate_1440Mbps[] = {
  298. /* The default register settings provide the 1440Mbps rate */
  299. { CCI_REG8(0x36c5), 0x00 }, /* Undocumented */
  300. { CCI_REG8(0x3ac4), 0x00 }, /* Undocumented */
  301. { IMX283_REG_STBPL, 0x00 },
  302. { IMX283_REG_TCLKPOST, 0xa7 },
  303. { IMX283_REG_THSPREPARE, 0x6f },
  304. { IMX283_REG_THSZERO, 0x9f },
  305. { IMX283_REG_THSTRAIL, 0x5f },
  306. { IMX283_REG_TCLKTRAIL, 0x5f },
  307. { IMX283_REG_TCLKPREPARE, 0x6f },
  308. { IMX283_REG_TCLKZERO, 0x017f },
  309. { IMX283_REG_TLPX, 0x4f },
  310. { IMX283_REG_THSEXIT, 0x47 },
  311. { IMX283_REG_TCLKPRE, 0x07 },
  312. { IMX283_REG_SYSMODE, 0x02 },
  313. };
  314. static const struct cci_reg_sequence mipi_data_rate_720Mbps[] = {
  315. /* Undocumented Additions "For 720MBps" Setting */
  316. { CCI_REG8(0x36c5), 0x01 }, /* Undocumented */
  317. { CCI_REG8(0x3ac4), 0x01 }, /* Undocumented */
  318. { IMX283_REG_STBPL, 0x00 },
  319. { IMX283_REG_TCLKPOST, 0x77 },
  320. { IMX283_REG_THSPREPARE, 0x37 },
  321. { IMX283_REG_THSZERO, 0x67 },
  322. { IMX283_REG_THSTRAIL, 0x37 },
  323. { IMX283_REG_TCLKTRAIL, 0x37 },
  324. { IMX283_REG_TCLKPREPARE, 0x37 },
  325. { IMX283_REG_TCLKZERO, 0xdf },
  326. { IMX283_REG_TLPX, 0x2f },
  327. { IMX283_REG_THSEXIT, 0x47 },
  328. { IMX283_REG_TCLKPRE, 0x0f },
  329. { IMX283_REG_SYSMODE, 0x02 },
  330. };
  331. static const s64 link_frequencies[] = {
  332. 720 * HZ_PER_MHZ, /* 1440 Mbps lane data rate */
  333. 360 * HZ_PER_MHZ, /* 720 Mbps data lane rate */
  334. };
  335. static const struct imx283_reg_list link_freq_reglist[] = {
  336. { /* 720 MHz */
  337. .num_of_regs = ARRAY_SIZE(mipi_data_rate_1440Mbps),
  338. .regs = mipi_data_rate_1440Mbps,
  339. },
  340. { /* 360 MHz */
  341. .num_of_regs = ARRAY_SIZE(mipi_data_rate_720Mbps),
  342. .regs = mipi_data_rate_720Mbps,
  343. },
  344. };
  345. /* Mode configs */
  346. static const struct imx283_mode supported_modes_12bit[] = {
  347. {
  348. /* 20MPix 21.40 fps readout mode 0 */
  349. .mode = IMX283_MODE_0,
  350. .bpp = 12,
  351. .width = 5472,
  352. .height = 3648,
  353. .min_hmax = 5914, /* 887 @ 480MHz/72MHz */
  354. .min_vmax = 3793, /* Lines */
  355. .veff = 3694,
  356. .vst = 0,
  357. .vct = 0,
  358. .hbin_ratio = 1,
  359. .vbin_ratio = 1,
  360. /* 20.00 FPS */
  361. .default_hmax = 6000, /* 900 @ 480MHz/72MHz */
  362. .default_vmax = 4000,
  363. .min_shr = 11,
  364. .horizontal_ob = 96,
  365. .vertical_ob = 16,
  366. .crop = {
  367. .top = 40,
  368. .left = 108,
  369. .width = 5472,
  370. .height = 3648,
  371. },
  372. },
  373. {
  374. /*
  375. * Readout mode 2 : 2/2 binned mode (2736x1824)
  376. */
  377. .mode = IMX283_MODE_2,
  378. .bpp = 12,
  379. .width = 2736,
  380. .height = 1824,
  381. .min_hmax = 2414, /* Pixels (362 * 480MHz/72MHz + padding) */
  382. .min_vmax = 3840, /* Lines */
  383. /* 50.00 FPS */
  384. .default_hmax = 2500, /* 375 @ 480MHz/72Mhz */
  385. .default_vmax = 3840,
  386. .veff = 1824,
  387. .vst = 0,
  388. .vct = 0,
  389. .hbin_ratio = 2,
  390. .vbin_ratio = 2,
  391. .min_shr = 12,
  392. .horizontal_ob = 48,
  393. .vertical_ob = 4,
  394. .crop = {
  395. .top = 40,
  396. .left = 108,
  397. .width = 5472,
  398. .height = 3648,
  399. },
  400. },
  401. {
  402. /*
  403. * Readout mode 3 : 3/3 binned mode (1824x1216)
  404. */
  405. .mode = IMX283_MODE_3,
  406. .bpp = 12,
  407. .width = 1824,
  408. .height = 1216,
  409. .min_hmax = 1894, /* Pixels (284 * 480MHz/72MHz + padding) */
  410. .min_vmax = 4200, /* Lines */
  411. /* 60.00 fps */
  412. .default_hmax = 1900, /* 285 @ 480MHz/72Mhz */
  413. .default_vmax = 4200,
  414. .veff = 1234,
  415. .vst = 0,
  416. .vct = 0,
  417. .hbin_ratio = 3,
  418. .vbin_ratio = 3,
  419. .min_shr = 16,
  420. .horizontal_ob = 32,
  421. .vertical_ob = 4,
  422. .crop = {
  423. .top = 40,
  424. .left = 108,
  425. .width = 5472,
  426. .height = 3648,
  427. },
  428. },
  429. };
  430. static const struct imx283_mode supported_modes_10bit[] = {
  431. {
  432. /* 20MPix 25.48 fps readout mode 1 */
  433. .mode = IMX283_MODE_1,
  434. .bpp = 10,
  435. .width = 5472,
  436. .height = 3648,
  437. .min_hmax = 5960, /* 745 @ 576MHz / 72MHz */
  438. .min_vmax = 3793,
  439. /* 25.00 FPS */
  440. .default_hmax = 6000, /* 750 @ 576MHz / 72MHz */
  441. .default_vmax = 3840,
  442. .min_shr = 10,
  443. .horizontal_ob = 96,
  444. .vertical_ob = 16,
  445. .crop = {
  446. .top = 40,
  447. .left = 108,
  448. .width = 5472,
  449. .height = 3648,
  450. },
  451. },
  452. };
  453. static const u32 imx283_mbus_codes[] = {
  454. MEDIA_BUS_FMT_SRGGB12_1X12,
  455. MEDIA_BUS_FMT_SRGGB10_1X10,
  456. };
  457. /* regulator supplies */
  458. static const char *const imx283_supply_name[] = {
  459. "vadd", /* Analog (2.9V) supply */
  460. "vdd1", /* Supply Voltage 2 (1.8V) supply */
  461. "vdd2", /* Supply Voltage 3 (1.2V) supply */
  462. };
  463. struct imx283 {
  464. struct device *dev;
  465. struct regmap *cci;
  466. const struct imx283_input_frequency *freq;
  467. struct v4l2_subdev sd;
  468. struct media_pad pad;
  469. struct clk *xclk;
  470. struct gpio_desc *reset_gpio;
  471. struct regulator_bulk_data supplies[ARRAY_SIZE(imx283_supply_name)];
  472. /* V4L2 Controls */
  473. struct v4l2_ctrl_handler ctrl_handler;
  474. struct v4l2_ctrl *exposure;
  475. struct v4l2_ctrl *vblank;
  476. struct v4l2_ctrl *hblank;
  477. struct v4l2_ctrl *vflip;
  478. unsigned long link_freq_bitmap;
  479. u16 hmax;
  480. u32 vmax;
  481. };
  482. static inline struct imx283 *to_imx283(struct v4l2_subdev *sd)
  483. {
  484. return container_of_const(sd, struct imx283, sd);
  485. }
  486. static inline void get_mode_table(unsigned int code,
  487. const struct imx283_mode **mode_list,
  488. unsigned int *num_modes)
  489. {
  490. switch (code) {
  491. case MEDIA_BUS_FMT_SRGGB12_1X12:
  492. case MEDIA_BUS_FMT_SGRBG12_1X12:
  493. case MEDIA_BUS_FMT_SGBRG12_1X12:
  494. case MEDIA_BUS_FMT_SBGGR12_1X12:
  495. *mode_list = supported_modes_12bit;
  496. *num_modes = ARRAY_SIZE(supported_modes_12bit);
  497. break;
  498. case MEDIA_BUS_FMT_SRGGB10_1X10:
  499. case MEDIA_BUS_FMT_SGRBG10_1X10:
  500. case MEDIA_BUS_FMT_SGBRG10_1X10:
  501. case MEDIA_BUS_FMT_SBGGR10_1X10:
  502. *mode_list = supported_modes_10bit;
  503. *num_modes = ARRAY_SIZE(supported_modes_10bit);
  504. break;
  505. default:
  506. *mode_list = NULL;
  507. *num_modes = 0;
  508. break;
  509. }
  510. }
  511. /* Calculate the Pixel Rate based on the current mode */
  512. static u64 imx283_pixel_rate(struct imx283 *imx283,
  513. const struct imx283_mode *mode)
  514. {
  515. u64 link_frequency = link_frequencies[__ffs(imx283->link_freq_bitmap)];
  516. unsigned int bpp = mode->bpp;
  517. const unsigned int ddr = 2; /* Double Data Rate */
  518. const unsigned int lanes = 4; /* Only 4 lane support */
  519. u64 numerator = link_frequency * ddr * lanes;
  520. do_div(numerator, bpp);
  521. return numerator;
  522. }
  523. /* Convert from a variable pixel_rate to 72 MHz clock cycles */
  524. static u64 imx283_internal_clock(unsigned int pixel_rate, unsigned int pixels)
  525. {
  526. /*
  527. * Determine the following operation without overflow:
  528. * pixels = 72 Mhz / pixel_rate
  529. *
  530. * The internal clock at 72MHz and Pixel Rate (between 240 and 576MHz)
  531. * can easily overflow this calculation, so pre-divide to simplify.
  532. */
  533. const u32 iclk_pre = 72;
  534. const u32 pclk_pre = pixel_rate / HZ_PER_MHZ;
  535. u64 numerator = pixels * iclk_pre;
  536. do_div(numerator, pclk_pre);
  537. return numerator;
  538. }
  539. /* Internal clock (72MHz) to Pixel Rate clock (Variable) */
  540. static u64 imx283_iclk_to_pix(unsigned int pixel_rate, unsigned int cycles)
  541. {
  542. /*
  543. * Determine the following operation without overflow:
  544. * cycles * pixel_rate / 72 MHz
  545. *
  546. * The internal clock at 72MHz and Pixel Rate (between 240 and 576MHz)
  547. * can easily overflow this calculation, so pre-divide to simplify.
  548. */
  549. const u32 iclk_pre = 72;
  550. const u32 pclk_pre = pixel_rate / HZ_PER_MHZ;
  551. u64 numerator = cycles * pclk_pre;
  552. do_div(numerator, iclk_pre);
  553. return numerator;
  554. }
  555. /* Determine the exposure based on current hmax, vmax and a given SHR */
  556. static u32 imx283_exposure(struct imx283 *imx283,
  557. const struct imx283_mode *mode, u64 shr)
  558. {
  559. u32 svr = 0; /* SVR feature is not currently supported */
  560. u32 offset;
  561. u64 numerator;
  562. /* Number of clocks per internal offset period */
  563. offset = mode->mode == IMX283_MODE_0 ? 209 : 157;
  564. numerator = (imx283->vmax * (svr + 1) - shr) * imx283->hmax + offset;
  565. do_div(numerator, imx283->hmax);
  566. return clamp(numerator, 0, U32_MAX);
  567. }
  568. static void imx283_exposure_limits(struct imx283 *imx283,
  569. const struct imx283_mode *mode,
  570. s64 *min_exposure, s64 *max_exposure)
  571. {
  572. u32 svr = 0; /* SVR feature is not currently supported */
  573. u64 min_shr = mode->min_shr;
  574. /* Global Shutter is not supported */
  575. u64 max_shr = (svr + 1) * imx283->vmax - 4;
  576. max_shr = min(max_shr, BIT(16) - 1);
  577. *min_exposure = imx283_exposure(imx283, mode, max_shr);
  578. *max_exposure = imx283_exposure(imx283, mode, min_shr);
  579. }
  580. /*
  581. * Integration Time [s] = [ {VMAX x (SVR + 1) – (SHR)} x HMAX + offset ]
  582. * / [ 72 x 10^6 ]
  583. */
  584. static u32 imx283_shr(struct imx283 *imx283, const struct imx283_mode *mode,
  585. u32 exposure)
  586. {
  587. u32 svr = 0; /* SVR feature is not currently supported */
  588. u32 offset;
  589. u64 temp;
  590. /* Number of clocks per internal offset period */
  591. offset = mode->mode == IMX283_MODE_0 ? 209 : 157;
  592. temp = ((u64)exposure * imx283->hmax - offset);
  593. do_div(temp, imx283->hmax);
  594. return (imx283->vmax * (svr + 1) - temp);
  595. }
  596. static const char * const imx283_tpg_menu[] = {
  597. "Disabled",
  598. "All 000h",
  599. "All FFFh",
  600. "All 555h",
  601. "All AAAh",
  602. "Horizontal color bars",
  603. "Vertical color bars",
  604. };
  605. static const int imx283_tpg_val[] = {
  606. IMX283_TPG_PAT_ALL_000,
  607. IMX283_TPG_PAT_ALL_000,
  608. IMX283_TPG_PAT_ALL_FFF,
  609. IMX283_TPG_PAT_ALL_555,
  610. IMX283_TPG_PAT_ALL_AAA,
  611. IMX283_TPG_PAT_H_COLOR_BARS,
  612. IMX283_TPG_PAT_V_COLOR_BARS,
  613. };
  614. static int imx283_update_test_pattern(struct imx283 *imx283, u32 pattern_index)
  615. {
  616. int ret;
  617. if (pattern_index >= ARRAY_SIZE(imx283_tpg_val))
  618. return -EINVAL;
  619. if (!pattern_index)
  620. return cci_write(imx283->cci, IMX283_REG_TPG_CTRL, 0x00, NULL);
  621. ret = cci_write(imx283->cci, IMX283_REG_TPG_PAT,
  622. imx283_tpg_val[pattern_index], NULL);
  623. if (ret)
  624. return ret;
  625. return cci_write(imx283->cci, IMX283_REG_TPG_CTRL,
  626. IMX283_TPG_CTRL_CLKEN | IMX283_TPG_CTRL_PATEN, NULL);
  627. }
  628. static int imx283_set_ctrl(struct v4l2_ctrl *ctrl)
  629. {
  630. struct imx283 *imx283 = container_of(ctrl->handler, struct imx283,
  631. ctrl_handler);
  632. const struct imx283_mode *mode;
  633. struct v4l2_mbus_framefmt *fmt;
  634. const struct imx283_mode *mode_list;
  635. struct v4l2_subdev_state *state;
  636. unsigned int num_modes;
  637. u64 shr, pixel_rate;
  638. int ret = 0;
  639. state = v4l2_subdev_get_locked_active_state(&imx283->sd);
  640. fmt = v4l2_subdev_state_get_format(state, 0);
  641. get_mode_table(fmt->code, &mode_list, &num_modes);
  642. mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
  643. fmt->width, fmt->height);
  644. /*
  645. * The VBLANK control may change the limits of usable exposure, so check
  646. * and adjust if necessary.
  647. */
  648. if (ctrl->id == V4L2_CID_VBLANK) {
  649. /* Honour the VBLANK limits when setting exposure. */
  650. s64 current_exposure, max_exposure, min_exposure;
  651. imx283->vmax = mode->height + ctrl->val;
  652. imx283_exposure_limits(imx283, mode,
  653. &min_exposure, &max_exposure);
  654. current_exposure = imx283->exposure->val;
  655. current_exposure = clamp(current_exposure, min_exposure,
  656. max_exposure);
  657. __v4l2_ctrl_modify_range(imx283->exposure, min_exposure,
  658. max_exposure, 1, current_exposure);
  659. }
  660. /*
  661. * Applying V4L2 control value only happens
  662. * when power is up for streaming
  663. */
  664. if (!pm_runtime_get_if_active(imx283->dev))
  665. return 0;
  666. switch (ctrl->id) {
  667. case V4L2_CID_EXPOSURE:
  668. shr = imx283_shr(imx283, mode, ctrl->val);
  669. dev_dbg(imx283->dev, "V4L2_CID_EXPOSURE : %d - SHR: %lld\n",
  670. ctrl->val, shr);
  671. ret = cci_write(imx283->cci, IMX283_REG_SHR, shr, NULL);
  672. break;
  673. case V4L2_CID_HBLANK:
  674. pixel_rate = imx283_pixel_rate(imx283, mode);
  675. imx283->hmax = imx283_internal_clock(pixel_rate, mode->width + ctrl->val);
  676. dev_dbg(imx283->dev, "V4L2_CID_HBLANK : %d HMAX : %u\n",
  677. ctrl->val, imx283->hmax);
  678. ret = cci_write(imx283->cci, IMX283_REG_HMAX, imx283->hmax, NULL);
  679. break;
  680. case V4L2_CID_VBLANK:
  681. imx283->vmax = mode->height + ctrl->val;
  682. dev_dbg(imx283->dev, "V4L2_CID_VBLANK : %d VMAX : %u\n",
  683. ctrl->val, imx283->vmax);
  684. ret = cci_write(imx283->cci, IMX283_REG_VMAX, imx283->vmax, NULL);
  685. break;
  686. case V4L2_CID_ANALOGUE_GAIN:
  687. ret = cci_write(imx283->cci, IMX283_REG_ANALOG_GAIN, ctrl->val, NULL);
  688. break;
  689. case V4L2_CID_DIGITAL_GAIN:
  690. ret = cci_write(imx283->cci, IMX283_REG_DIGITAL_GAIN, ctrl->val, NULL);
  691. break;
  692. case V4L2_CID_VFLIP:
  693. /*
  694. * VFLIP is managed by BIT(0) of IMX283_REG_HTRIMMING address, hence
  695. * both need to be set simultaneously.
  696. */
  697. if (ctrl->val) {
  698. cci_write(imx283->cci, IMX283_REG_HTRIMMING,
  699. IMX283_HTRIMMING_EN | IMX283_MDVREV, &ret);
  700. } else {
  701. cci_write(imx283->cci, IMX283_REG_HTRIMMING,
  702. IMX283_HTRIMMING_EN, &ret);
  703. }
  704. break;
  705. case V4L2_CID_TEST_PATTERN:
  706. ret = imx283_update_test_pattern(imx283, ctrl->val);
  707. break;
  708. default:
  709. dev_err(imx283->dev, "ctrl(id:0x%x, val:0x%x) is not handled\n",
  710. ctrl->id, ctrl->val);
  711. break;
  712. }
  713. pm_runtime_put(imx283->dev);
  714. return ret;
  715. }
  716. static const struct v4l2_ctrl_ops imx283_ctrl_ops = {
  717. .s_ctrl = imx283_set_ctrl,
  718. };
  719. static int imx283_enum_mbus_code(struct v4l2_subdev *sd,
  720. struct v4l2_subdev_state *sd_state,
  721. struct v4l2_subdev_mbus_code_enum *code)
  722. {
  723. if (code->index >= ARRAY_SIZE(imx283_mbus_codes))
  724. return -EINVAL;
  725. code->code = imx283_mbus_codes[code->index];
  726. return 0;
  727. }
  728. static int imx283_enum_frame_size(struct v4l2_subdev *sd,
  729. struct v4l2_subdev_state *sd_state,
  730. struct v4l2_subdev_frame_size_enum *fse)
  731. {
  732. const struct imx283_mode *mode_list;
  733. unsigned int num_modes;
  734. get_mode_table(fse->code, &mode_list, &num_modes);
  735. if (fse->index >= num_modes)
  736. return -EINVAL;
  737. fse->min_width = mode_list[fse->index].width;
  738. fse->max_width = fse->min_width;
  739. fse->min_height = mode_list[fse->index].height;
  740. fse->max_height = fse->min_height;
  741. return 0;
  742. }
  743. static void imx283_update_image_pad_format(struct imx283 *imx283,
  744. const struct imx283_mode *mode,
  745. struct v4l2_mbus_framefmt *format)
  746. {
  747. format->width = mode->width;
  748. format->height = mode->height;
  749. format->field = V4L2_FIELD_NONE;
  750. format->colorspace = V4L2_COLORSPACE_RAW;
  751. format->ycbcr_enc = V4L2_YCBCR_ENC_601;
  752. format->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  753. format->xfer_func = V4L2_XFER_FUNC_NONE;
  754. }
  755. static int imx283_init_state(struct v4l2_subdev *sd,
  756. struct v4l2_subdev_state *state)
  757. {
  758. struct imx283 *imx283 = to_imx283(sd);
  759. struct v4l2_mbus_framefmt *format;
  760. const struct imx283_mode *mode;
  761. struct v4l2_rect *crop;
  762. /* Initialize try_fmt */
  763. format = v4l2_subdev_state_get_format(state, IMAGE_PAD);
  764. mode = &supported_modes_12bit[0];
  765. format->code = MEDIA_BUS_FMT_SRGGB12_1X12;
  766. imx283_update_image_pad_format(imx283, mode, format);
  767. /* Initialize crop rectangle to mode default */
  768. crop = v4l2_subdev_state_get_crop(state, IMAGE_PAD);
  769. *crop = mode->crop;
  770. return 0;
  771. }
  772. static void imx283_set_framing_limits(struct imx283 *imx283,
  773. const struct imx283_mode *mode)
  774. {
  775. u64 pixel_rate = imx283_pixel_rate(imx283, mode);
  776. u64 min_hblank, max_hblank, def_hblank;
  777. /* Initialise hmax and vmax for exposure calculations */
  778. imx283->hmax = imx283_internal_clock(pixel_rate, mode->default_hmax);
  779. imx283->vmax = mode->default_vmax;
  780. /*
  781. * Horizontal Blanking
  782. * Convert the HMAX_MAX (72MHz) to Pixel rate values for HBLANK_MAX
  783. */
  784. min_hblank = mode->min_hmax - mode->width;
  785. max_hblank = imx283_iclk_to_pix(pixel_rate, IMX283_HMAX_MAX) - mode->width;
  786. def_hblank = mode->default_hmax - mode->width;
  787. __v4l2_ctrl_modify_range(imx283->hblank, min_hblank, max_hblank, 1,
  788. def_hblank);
  789. __v4l2_ctrl_s_ctrl(imx283->hblank, def_hblank);
  790. /* Vertical Blanking */
  791. __v4l2_ctrl_modify_range(imx283->vblank, mode->min_vmax - mode->height,
  792. IMX283_VMAX_MAX - mode->height, 1,
  793. mode->default_vmax - mode->height);
  794. __v4l2_ctrl_s_ctrl(imx283->vblank, mode->default_vmax - mode->height);
  795. }
  796. static int imx283_set_pad_format(struct v4l2_subdev *sd,
  797. struct v4l2_subdev_state *sd_state,
  798. struct v4l2_subdev_format *fmt)
  799. {
  800. struct v4l2_mbus_framefmt *format;
  801. const struct imx283_mode *mode;
  802. struct imx283 *imx283 = to_imx283(sd);
  803. const struct imx283_mode *mode_list;
  804. unsigned int num_modes;
  805. get_mode_table(fmt->format.code, &mode_list, &num_modes);
  806. mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
  807. fmt->format.width, fmt->format.height);
  808. fmt->format.width = mode->width;
  809. fmt->format.height = mode->height;
  810. fmt->format.field = V4L2_FIELD_NONE;
  811. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  812. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601;
  813. fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  814. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  815. format = v4l2_subdev_state_get_format(sd_state, 0);
  816. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  817. imx283_set_framing_limits(imx283, mode);
  818. *format = fmt->format;
  819. return 0;
  820. }
  821. static int imx283_standby_cancel(struct imx283 *imx283)
  822. {
  823. unsigned int link_freq_idx;
  824. int ret = 0;
  825. cci_write(imx283->cci, IMX283_REG_STANDBY,
  826. IMX283_STBLOGIC | IMX283_STBDV, &ret);
  827. /* Configure PLL clocks based on the xclk */
  828. cci_multi_reg_write(imx283->cci, imx283->freq->regs,
  829. imx283->freq->reg_count, &ret);
  830. dev_dbg(imx283->dev, "Using clk freq %ld MHz",
  831. imx283->freq->mhz / HZ_PER_MHZ);
  832. /* Initialise communication */
  833. cci_write(imx283->cci, IMX283_REG_PLSTMG08, IMX283_PLSTMG08_VAL, &ret);
  834. cci_write(imx283->cci, IMX283_REG_PLSTMG02, IMX283_PLSTMG02_VAL, &ret);
  835. /* Enable PLL */
  836. cci_write(imx283->cci, IMX283_REG_STBPL, IMX283_STBPL_NORMAL, &ret);
  837. /* Configure the MIPI link speed */
  838. link_freq_idx = __ffs(imx283->link_freq_bitmap);
  839. cci_multi_reg_write(imx283->cci, link_freq_reglist[link_freq_idx].regs,
  840. link_freq_reglist[link_freq_idx].num_of_regs,
  841. &ret);
  842. /* 1st Stabilisation period of 1 ms or more */
  843. usleep_range(1000, 2000);
  844. /* Activate */
  845. cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_ACTIVE, &ret);
  846. /* 2nd Stabilisation period of 19ms or more */
  847. usleep_range(19000, 20000);
  848. cci_write(imx283->cci, IMX283_REG_CLAMP, IMX283_CLPSQRST, &ret);
  849. cci_write(imx283->cci, IMX283_REG_XMSTA, 0, &ret);
  850. cci_write(imx283->cci, IMX283_REG_SYNCDRV, IMX283_SYNCDRV_XHS_XVS, &ret);
  851. return ret;
  852. }
  853. /* Start streaming */
  854. static int imx283_start_streaming(struct imx283 *imx283,
  855. struct v4l2_subdev_state *state)
  856. {
  857. const struct imx283_readout_mode *readout;
  858. const struct imx283_mode *mode;
  859. const struct v4l2_mbus_framefmt *fmt;
  860. const struct imx283_mode *mode_list;
  861. unsigned int num_modes;
  862. u32 v_widcut;
  863. s32 v_pos;
  864. u32 write_v_size;
  865. u32 y_out_size;
  866. int ret = 0;
  867. fmt = v4l2_subdev_state_get_format(state, 0);
  868. get_mode_table(fmt->code, &mode_list, &num_modes);
  869. mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
  870. fmt->width, fmt->height);
  871. ret = imx283_standby_cancel(imx283);
  872. if (ret) {
  873. dev_err(imx283->dev, "failed to cancel standby\n");
  874. return ret;
  875. }
  876. /*
  877. * Set the readout mode registers.
  878. * MDSEL3 and MDSEL4 are updated to enable Arbitrary Vertical Cropping.
  879. */
  880. readout = &imx283_readout_modes[mode->mode];
  881. cci_write(imx283->cci, IMX283_REG_MDSEL1, readout->mdsel1, &ret);
  882. cci_write(imx283->cci, IMX283_REG_MDSEL2, readout->mdsel2, &ret);
  883. cci_write(imx283->cci, IMX283_REG_MDSEL3,
  884. readout->mdsel3 | IMX283_MDSEL3_VCROP_EN, &ret);
  885. cci_write(imx283->cci, IMX283_REG_MDSEL4,
  886. readout->mdsel4 | IMX283_MDSEL4_VCROP_EN, &ret);
  887. /* Mode 1S specific entries from the Readout Drive Mode Tables */
  888. if (mode->mode == IMX283_MODE_1S) {
  889. cci_write(imx283->cci, IMX283_REG_MDSEL7, 0x01, &ret);
  890. cci_write(imx283->cci, IMX283_REG_MDSEL18, 0x1098, &ret);
  891. }
  892. if (ret) {
  893. dev_err(imx283->dev, "failed to set readout\n");
  894. return ret;
  895. }
  896. /* Initialise SVR. Unsupported for now - Always 0 */
  897. cci_write(imx283->cci, IMX283_REG_SVR, 0x00, &ret);
  898. dev_dbg(imx283->dev, "Mode: Size %d x %d\n", mode->width, mode->height);
  899. dev_dbg(imx283->dev, "Analogue Crop (in the mode) (%d,%d)/%ux%u\n",
  900. mode->crop.left,
  901. mode->crop.top,
  902. mode->crop.width,
  903. mode->crop.height);
  904. y_out_size = mode->crop.height / mode->vbin_ratio;
  905. write_v_size = y_out_size + mode->vertical_ob;
  906. /*
  907. * cropping start position = (VWINPOS – Vst) × 2
  908. * cropping width = Veff – (VWIDCUT – Vct) × 2
  909. */
  910. v_pos = imx283->vflip->val ?
  911. ((-mode->crop.top / mode->vbin_ratio) / 2) + mode->vst :
  912. ((mode->crop.top / mode->vbin_ratio) / 2) + mode->vst;
  913. v_widcut = ((mode->veff - y_out_size) / 2) + mode->vct;
  914. cci_write(imx283->cci, IMX283_REG_Y_OUT_SIZE, y_out_size, &ret);
  915. cci_write(imx283->cci, IMX283_REG_WRITE_VSIZE, write_v_size, &ret);
  916. cci_write(imx283->cci, IMX283_REG_VWIDCUT, v_widcut, &ret);
  917. cci_write(imx283->cci, IMX283_REG_VWINPOS, v_pos, &ret);
  918. cci_write(imx283->cci, IMX283_REG_OB_SIZE_V, mode->vertical_ob, &ret);
  919. /* TODO: Validate mode->crop is fully contained within imx283_native_area */
  920. cci_write(imx283->cci, IMX283_REG_HTRIMMING_START, mode->crop.left, &ret);
  921. cci_write(imx283->cci, IMX283_REG_HTRIMMING_END,
  922. mode->crop.left + mode->crop.width, &ret);
  923. /* Disable embedded data */
  924. cci_write(imx283->cci, IMX283_REG_EBD_X_OUT_SIZE, 0, &ret);
  925. /* Apply customized values from controls (HMAX/VMAX/SHR) */
  926. ret = __v4l2_ctrl_handler_setup(imx283->sd.ctrl_handler);
  927. return ret;
  928. }
  929. static int imx283_enable_streams(struct v4l2_subdev *sd,
  930. struct v4l2_subdev_state *state, u32 pad,
  931. u64 streams_mask)
  932. {
  933. struct imx283 *imx283 = to_imx283(sd);
  934. int ret;
  935. if (pad != IMAGE_PAD)
  936. return -EINVAL;
  937. ret = pm_runtime_get_sync(imx283->dev);
  938. if (ret < 0) {
  939. pm_runtime_put_noidle(imx283->dev);
  940. return ret;
  941. }
  942. ret = imx283_start_streaming(imx283, state);
  943. if (ret)
  944. goto err_rpm_put;
  945. return 0;
  946. err_rpm_put:
  947. pm_runtime_put_autosuspend(imx283->dev);
  948. return ret;
  949. }
  950. static int imx283_disable_streams(struct v4l2_subdev *sd,
  951. struct v4l2_subdev_state *state, u32 pad,
  952. u64 streams_mask)
  953. {
  954. struct imx283 *imx283 = to_imx283(sd);
  955. int ret;
  956. if (pad != IMAGE_PAD)
  957. return -EINVAL;
  958. ret = cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_STBLOGIC, NULL);
  959. if (ret)
  960. dev_err(imx283->dev, "Failed to stop stream\n");
  961. pm_runtime_put_autosuspend(imx283->dev);
  962. return ret;
  963. }
  964. /* Power/clock management functions */
  965. static int imx283_power_on(struct device *dev)
  966. {
  967. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  968. struct imx283 *imx283 = to_imx283(sd);
  969. int ret;
  970. ret = regulator_bulk_enable(ARRAY_SIZE(imx283_supply_name),
  971. imx283->supplies);
  972. if (ret) {
  973. dev_err(imx283->dev, "failed to enable regulators\n");
  974. return ret;
  975. }
  976. ret = clk_prepare_enable(imx283->xclk);
  977. if (ret) {
  978. dev_err(imx283->dev, "failed to enable clock\n");
  979. goto reg_off;
  980. }
  981. gpiod_set_value_cansleep(imx283->reset_gpio, 0);
  982. usleep_range(IMX283_XCLR_MIN_DELAY_US,
  983. IMX283_XCLR_MIN_DELAY_US + IMX283_XCLR_DELAY_RANGE_US);
  984. return 0;
  985. reg_off:
  986. regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies);
  987. return ret;
  988. }
  989. static int imx283_power_off(struct device *dev)
  990. {
  991. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  992. struct imx283 *imx283 = to_imx283(sd);
  993. gpiod_set_value_cansleep(imx283->reset_gpio, 1);
  994. regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies);
  995. clk_disable_unprepare(imx283->xclk);
  996. return 0;
  997. }
  998. static int imx283_get_regulators(struct imx283 *imx283)
  999. {
  1000. unsigned int i;
  1001. for (i = 0; i < ARRAY_SIZE(imx283_supply_name); i++)
  1002. imx283->supplies[i].supply = imx283_supply_name[i];
  1003. return devm_regulator_bulk_get(imx283->dev,
  1004. ARRAY_SIZE(imx283_supply_name),
  1005. imx283->supplies);
  1006. }
  1007. /* Verify chip ID */
  1008. static int imx283_identify_module(struct imx283 *imx283)
  1009. {
  1010. int ret;
  1011. u64 val;
  1012. ret = cci_read(imx283->cci, IMX283_REG_CHIP_ID, &val, NULL);
  1013. if (ret) {
  1014. dev_err(imx283->dev, "failed to read chip id %x, with error %d\n",
  1015. IMX283_CHIP_ID, ret);
  1016. return ret;
  1017. }
  1018. if (val != IMX283_CHIP_ID) {
  1019. dev_err(imx283->dev, "chip id mismatch: %x!=%llx\n",
  1020. IMX283_CHIP_ID, val);
  1021. return -EIO;
  1022. }
  1023. return 0;
  1024. }
  1025. static int imx283_get_selection(struct v4l2_subdev *sd,
  1026. struct v4l2_subdev_state *sd_state,
  1027. struct v4l2_subdev_selection *sel)
  1028. {
  1029. switch (sel->target) {
  1030. case V4L2_SEL_TGT_CROP: {
  1031. sel->r = *v4l2_subdev_state_get_crop(sd_state, 0);
  1032. return 0;
  1033. }
  1034. case V4L2_SEL_TGT_NATIVE_SIZE:
  1035. sel->r = imx283_native_area;
  1036. return 0;
  1037. case V4L2_SEL_TGT_CROP_DEFAULT:
  1038. case V4L2_SEL_TGT_CROP_BOUNDS:
  1039. sel->r = imx283_active_area;
  1040. return 0;
  1041. default:
  1042. return -EINVAL;
  1043. }
  1044. }
  1045. static const struct v4l2_subdev_video_ops imx283_video_ops = {
  1046. .s_stream = v4l2_subdev_s_stream_helper,
  1047. };
  1048. static const struct v4l2_subdev_pad_ops imx283_pad_ops = {
  1049. .enum_mbus_code = imx283_enum_mbus_code,
  1050. .get_fmt = v4l2_subdev_get_fmt,
  1051. .set_fmt = imx283_set_pad_format,
  1052. .get_selection = imx283_get_selection,
  1053. .enum_frame_size = imx283_enum_frame_size,
  1054. .enable_streams = imx283_enable_streams,
  1055. .disable_streams = imx283_disable_streams,
  1056. };
  1057. static const struct v4l2_subdev_internal_ops imx283_internal_ops = {
  1058. .init_state = imx283_init_state,
  1059. };
  1060. static const struct v4l2_subdev_ops imx283_subdev_ops = {
  1061. .video = &imx283_video_ops,
  1062. .pad = &imx283_pad_ops,
  1063. };
  1064. /* Initialize control handlers */
  1065. static int imx283_init_controls(struct imx283 *imx283)
  1066. {
  1067. struct v4l2_ctrl_handler *ctrl_hdlr;
  1068. struct v4l2_fwnode_device_properties props;
  1069. struct v4l2_ctrl *link_freq;
  1070. const struct imx283_mode *mode = &supported_modes_12bit[0];
  1071. u64 min_hblank, max_hblank, def_hblank;
  1072. u64 pixel_rate;
  1073. int ret;
  1074. ctrl_hdlr = &imx283->ctrl_handler;
  1075. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
  1076. if (ret)
  1077. return ret;
  1078. /*
  1079. * Create the controls here, but mode specific limits are setup
  1080. * in the imx283_set_framing_limits() call below.
  1081. */
  1082. /* By default, PIXEL_RATE is read only */
  1083. pixel_rate = imx283_pixel_rate(imx283, mode);
  1084. v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
  1085. V4L2_CID_PIXEL_RATE, pixel_rate,
  1086. pixel_rate, 1, pixel_rate);
  1087. link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx283_ctrl_ops,
  1088. V4L2_CID_LINK_FREQ,
  1089. __fls(imx283->link_freq_bitmap),
  1090. __ffs(imx283->link_freq_bitmap),
  1091. link_frequencies);
  1092. if (link_freq)
  1093. link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1094. /* Initialise vblank/hblank/exposure based on the current mode. */
  1095. imx283->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
  1096. V4L2_CID_VBLANK,
  1097. mode->min_vmax - mode->height,
  1098. IMX283_VMAX_MAX, 1,
  1099. mode->default_vmax - mode->height);
  1100. min_hblank = mode->min_hmax - mode->width;
  1101. max_hblank = imx283_iclk_to_pix(pixel_rate, IMX283_HMAX_MAX) - mode->width;
  1102. def_hblank = mode->default_hmax - mode->width;
  1103. imx283->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
  1104. V4L2_CID_HBLANK, min_hblank, max_hblank,
  1105. 1, def_hblank);
  1106. imx283->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
  1107. V4L2_CID_EXPOSURE,
  1108. IMX283_EXPOSURE_MIN,
  1109. IMX283_EXPOSURE_MAX,
  1110. IMX283_EXPOSURE_STEP,
  1111. IMX283_EXPOSURE_DEFAULT);
  1112. v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  1113. IMX283_ANA_GAIN_MIN, IMX283_ANA_GAIN_MAX,
  1114. IMX283_ANA_GAIN_STEP, IMX283_ANA_GAIN_DEFAULT);
  1115. v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  1116. IMX283_DGTL_GAIN_MIN, IMX283_DGTL_GAIN_MAX,
  1117. IMX283_DGTL_GAIN_STEP, IMX283_DGTL_GAIN_DEFAULT);
  1118. imx283->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_VFLIP,
  1119. 0, 1, 1, 0);
  1120. if (imx283->vflip)
  1121. imx283->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  1122. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx283_ctrl_ops,
  1123. V4L2_CID_TEST_PATTERN,
  1124. ARRAY_SIZE(imx283_tpg_menu) - 1,
  1125. 0, 0, imx283_tpg_menu);
  1126. if (ctrl_hdlr->error) {
  1127. ret = ctrl_hdlr->error;
  1128. dev_err(imx283->dev, "control init failed (%d)\n", ret);
  1129. goto error;
  1130. }
  1131. ret = v4l2_fwnode_device_parse(imx283->dev, &props);
  1132. if (ret)
  1133. goto error;
  1134. ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx283_ctrl_ops,
  1135. &props);
  1136. if (ret)
  1137. goto error;
  1138. imx283->sd.ctrl_handler = ctrl_hdlr;
  1139. mutex_lock(imx283->ctrl_handler.lock);
  1140. /* Setup exposure and frame/line length limits. */
  1141. imx283_set_framing_limits(imx283, mode);
  1142. mutex_unlock(imx283->ctrl_handler.lock);
  1143. return 0;
  1144. error:
  1145. v4l2_ctrl_handler_free(ctrl_hdlr);
  1146. return ret;
  1147. }
  1148. static int imx283_parse_endpoint(struct imx283 *imx283)
  1149. {
  1150. struct fwnode_handle *fwnode;
  1151. struct v4l2_fwnode_endpoint bus_cfg = {
  1152. .bus_type = V4L2_MBUS_CSI2_DPHY
  1153. };
  1154. struct fwnode_handle *ep;
  1155. int ret;
  1156. fwnode = dev_fwnode(imx283->dev);
  1157. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1158. if (!ep) {
  1159. dev_err(imx283->dev, "Failed to get next endpoint\n");
  1160. return -ENXIO;
  1161. }
  1162. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1163. fwnode_handle_put(ep);
  1164. if (ret)
  1165. return ret;
  1166. if (bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
  1167. dev_err(imx283->dev,
  1168. "number of CSI2 data lanes %d is not supported\n",
  1169. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1170. ret = -EINVAL;
  1171. goto done_endpoint_free;
  1172. }
  1173. ret = v4l2_link_freq_to_bitmap(imx283->dev, bus_cfg.link_frequencies,
  1174. bus_cfg.nr_of_link_frequencies,
  1175. link_frequencies, ARRAY_SIZE(link_frequencies),
  1176. &imx283->link_freq_bitmap);
  1177. done_endpoint_free:
  1178. v4l2_fwnode_endpoint_free(&bus_cfg);
  1179. return ret;
  1180. };
  1181. static int imx283_probe(struct i2c_client *client)
  1182. {
  1183. struct imx283 *imx283;
  1184. unsigned int i;
  1185. unsigned int xclk_freq;
  1186. int ret;
  1187. imx283 = devm_kzalloc(&client->dev, sizeof(*imx283), GFP_KERNEL);
  1188. if (!imx283)
  1189. return -ENOMEM;
  1190. imx283->dev = &client->dev;
  1191. v4l2_i2c_subdev_init(&imx283->sd, client, &imx283_subdev_ops);
  1192. imx283->cci = devm_cci_regmap_init_i2c(client, 16);
  1193. if (IS_ERR(imx283->cci)) {
  1194. ret = PTR_ERR(imx283->cci);
  1195. dev_err(imx283->dev, "failed to initialize CCI: %d\n", ret);
  1196. return ret;
  1197. }
  1198. /* Get system clock (xclk) */
  1199. imx283->xclk = devm_v4l2_sensor_clk_get(imx283->dev, NULL);
  1200. if (IS_ERR(imx283->xclk))
  1201. return dev_err_probe(imx283->dev, PTR_ERR(imx283->xclk),
  1202. "failed to get xclk\n");
  1203. xclk_freq = clk_get_rate(imx283->xclk);
  1204. for (i = 0; i < ARRAY_SIZE(imx283_frequencies); i++) {
  1205. if (xclk_freq == imx283_frequencies[i].mhz) {
  1206. imx283->freq = &imx283_frequencies[i];
  1207. break;
  1208. }
  1209. }
  1210. if (!imx283->freq) {
  1211. dev_err(imx283->dev, "xclk frequency unsupported: %d Hz\n", xclk_freq);
  1212. return -EINVAL;
  1213. }
  1214. ret = imx283_get_regulators(imx283);
  1215. if (ret) {
  1216. return dev_err_probe(imx283->dev, ret,
  1217. "failed to get regulators\n");
  1218. }
  1219. ret = imx283_parse_endpoint(imx283);
  1220. if (ret) {
  1221. dev_err(imx283->dev, "failed to parse endpoint configuration\n");
  1222. return ret;
  1223. }
  1224. /* Request optional enable pin */
  1225. imx283->reset_gpio = devm_gpiod_get_optional(imx283->dev, "reset",
  1226. GPIOD_OUT_LOW);
  1227. if (IS_ERR(imx283->reset_gpio))
  1228. return dev_err_probe(imx283->dev, PTR_ERR(imx283->reset_gpio),
  1229. "failed to get reset GPIO\n");
  1230. /*
  1231. * The sensor must be powered for imx283_identify_module()
  1232. * to be able to read the CHIP_ID register
  1233. */
  1234. ret = imx283_power_on(imx283->dev);
  1235. if (ret)
  1236. return ret;
  1237. ret = imx283_identify_module(imx283);
  1238. if (ret)
  1239. goto error_power_off;
  1240. /*
  1241. * Enable runtime PM with autosuspend. As the device has been powered
  1242. * manually, mark it as active, and increase the usage count without
  1243. * resuming the device.
  1244. */
  1245. pm_runtime_set_active(imx283->dev);
  1246. pm_runtime_get_noresume(imx283->dev);
  1247. pm_runtime_enable(imx283->dev);
  1248. pm_runtime_set_autosuspend_delay(imx283->dev, 1000);
  1249. pm_runtime_use_autosuspend(imx283->dev);
  1250. /* This needs the pm runtime to be registered. */
  1251. ret = imx283_init_controls(imx283);
  1252. if (ret)
  1253. goto error_pm;
  1254. /* Initialize subdev */
  1255. imx283->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1256. imx283->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1257. imx283->sd.internal_ops = &imx283_internal_ops;
  1258. /* Initialize source pads */
  1259. imx283->pad.flags = MEDIA_PAD_FL_SOURCE;
  1260. ret = media_entity_pads_init(&imx283->sd.entity, 1, &imx283->pad);
  1261. if (ret) {
  1262. dev_err(imx283->dev, "failed to init entity pads: %d\n", ret);
  1263. goto error_handler_free;
  1264. }
  1265. imx283->sd.state_lock = imx283->ctrl_handler.lock;
  1266. ret = v4l2_subdev_init_finalize(&imx283->sd);
  1267. if (ret < 0) {
  1268. dev_err(imx283->dev, "subdev init error: %d\n", ret);
  1269. goto error_media_entity;
  1270. }
  1271. ret = v4l2_async_register_subdev_sensor(&imx283->sd);
  1272. if (ret < 0) {
  1273. dev_err(imx283->dev, "failed to register sensor sub-device: %d\n", ret);
  1274. goto error_subdev_cleanup;
  1275. }
  1276. /*
  1277. * Decrease the PM usage count. The device will get suspended after the
  1278. * autosuspend delay, turning the power off.
  1279. */
  1280. pm_runtime_put_autosuspend(imx283->dev);
  1281. return 0;
  1282. error_subdev_cleanup:
  1283. v4l2_subdev_cleanup(&imx283->sd);
  1284. error_media_entity:
  1285. media_entity_cleanup(&imx283->sd.entity);
  1286. error_handler_free:
  1287. v4l2_ctrl_handler_free(imx283->sd.ctrl_handler);
  1288. error_pm:
  1289. pm_runtime_disable(imx283->dev);
  1290. pm_runtime_set_suspended(imx283->dev);
  1291. error_power_off:
  1292. imx283_power_off(imx283->dev);
  1293. return ret;
  1294. }
  1295. static void imx283_remove(struct i2c_client *client)
  1296. {
  1297. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1298. struct imx283 *imx283 = to_imx283(sd);
  1299. v4l2_async_unregister_subdev(sd);
  1300. v4l2_subdev_cleanup(&imx283->sd);
  1301. media_entity_cleanup(&sd->entity);
  1302. v4l2_ctrl_handler_free(imx283->sd.ctrl_handler);
  1303. pm_runtime_disable(imx283->dev);
  1304. if (!pm_runtime_status_suspended(imx283->dev))
  1305. imx283_power_off(imx283->dev);
  1306. pm_runtime_set_suspended(imx283->dev);
  1307. }
  1308. static DEFINE_RUNTIME_DEV_PM_OPS(imx283_pm_ops, imx283_power_off,
  1309. imx283_power_on, NULL);
  1310. static const struct of_device_id imx283_dt_ids[] = {
  1311. { .compatible = "sony,imx283" },
  1312. { /* sentinel */ }
  1313. };
  1314. MODULE_DEVICE_TABLE(of, imx283_dt_ids);
  1315. static struct i2c_driver imx283_i2c_driver = {
  1316. .driver = {
  1317. .name = "imx283",
  1318. .pm = pm_ptr(&imx283_pm_ops),
  1319. .of_match_table = imx283_dt_ids,
  1320. },
  1321. .probe = imx283_probe,
  1322. .remove = imx283_remove,
  1323. };
  1324. module_i2c_driver(imx283_i2c_driver);
  1325. MODULE_AUTHOR("Will Whang <will@willwhang.com>");
  1326. MODULE_AUTHOR("Kieran Bingham <kieran.bingham@ideasonboard.com>");
  1327. MODULE_AUTHOR("Umang Jain <umang.jain@ideasonboard.com>");
  1328. MODULE_DESCRIPTION("Sony IMX283 Sensor Driver");
  1329. MODULE_LICENSE("GPL");