imx335.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sony imx335 Camera Sensor Driver
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. */
  7. #include <linux/unaligned.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/regmap.h>
  14. #include <media/v4l2-cci.h>
  15. #include <media/v4l2-ctrls.h>
  16. #include <media/v4l2-fwnode.h>
  17. #include <media/v4l2-subdev.h>
  18. /* Streaming Mode */
  19. #define IMX335_REG_MODE_SELECT CCI_REG8(0x3000)
  20. #define IMX335_MODE_STANDBY 0x01
  21. #define IMX335_MODE_STREAMING 0x00
  22. /* Group hold register */
  23. #define IMX335_REG_HOLD CCI_REG8(0x3001)
  24. #define IMX335_REG_MASTER_MODE CCI_REG8(0x3002)
  25. #define IMX335_REG_BCWAIT_TIME CCI_REG8(0x300c)
  26. #define IMX335_REG_CPWAIT_TIME CCI_REG8(0x300d)
  27. #define IMX335_REG_WINMODE CCI_REG8(0x3018)
  28. #define IMX335_REG_HTRIMMING_START CCI_REG16_LE(0x302c)
  29. #define IMX335_REG_HNUM CCI_REG16_LE(0x302e)
  30. /* Lines per frame */
  31. #define IMX335_REG_VMAX CCI_REG24_LE(0x3030)
  32. #define IMX335_REG_HMAX CCI_REG16_LE(0x3034)
  33. #define IMX335_REG_OPB_SIZE_V CCI_REG8(0x304c)
  34. #define IMX335_REG_ADBIT CCI_REG8(0x3050)
  35. #define IMX335_REG_Y_OUT_SIZE CCI_REG16_LE(0x3056)
  36. #define IMX335_REG_SHUTTER CCI_REG24_LE(0x3058)
  37. #define IMX335_EXPOSURE_MIN 1
  38. #define IMX335_SHUTTER_MIN 9
  39. #define IMX335_SHUTTER_MIN_BINNED 17
  40. #define IMX335_EXPOSURE_STEP 1
  41. #define IMX335_EXPOSURE_DEFAULT 0x0648
  42. #define IMX335_REG_AREA2_WIDTH_1 CCI_REG16_LE(0x3072)
  43. #define IMX335_REG_AREA3_ST_ADR_1 CCI_REG16_LE(0x3074)
  44. #define IMX335_REG_AREA3_WIDTH_1 CCI_REG16_LE(0x3076)
  45. /* Analog and Digital gain control */
  46. #define IMX335_REG_GAIN CCI_REG8(0x30e8)
  47. #define IMX335_AGAIN_MIN 0
  48. #define IMX335_AGAIN_MAX 100
  49. #define IMX335_AGAIN_STEP 1
  50. #define IMX335_AGAIN_DEFAULT 0
  51. /* Vertical flip */
  52. #define IMX335_REG_VREVERSE CCI_REG8(0x304f)
  53. #define IMX335_REG_TPG_TESTCLKEN CCI_REG8(0x3148)
  54. #define IMX335_REG_INCLKSEL1 CCI_REG16_LE(0x314c)
  55. #define IMX335_REG_INCLKSEL2 CCI_REG8(0x315a)
  56. #define IMX335_REG_INCLKSEL3 CCI_REG8(0x3168)
  57. #define IMX335_REG_INCLKSEL4 CCI_REG8(0x316a)
  58. #define IMX335_REG_MDBIT CCI_REG8(0x319d)
  59. #define IMX335_REG_SYSMODE CCI_REG8(0x319e)
  60. #define IMX335_REG_XVS_XHS_DRV CCI_REG8(0x31a1)
  61. /* Test pattern generator */
  62. #define IMX335_REG_TPG_DIG_CLP_MODE CCI_REG8(0x3280)
  63. #define IMX335_REG_TPG_EN_DUOUT CCI_REG8(0x329c)
  64. #define IMX335_REG_TPG CCI_REG8(0x329e)
  65. #define IMX335_TPG_ALL_000 0
  66. #define IMX335_TPG_ALL_FFF 1
  67. #define IMX335_TPG_ALL_555 2
  68. #define IMX335_TPG_ALL_AAA 3
  69. #define IMX335_TPG_TOG_555_AAA 4
  70. #define IMX335_TPG_TOG_AAA_555 5
  71. #define IMX335_TPG_TOG_000_555 6
  72. #define IMX335_TPG_TOG_555_000 7
  73. #define IMX335_TPG_TOG_000_FFF 8
  74. #define IMX335_TPG_TOG_FFF_000 9
  75. #define IMX335_TPG_H_COLOR_BARS 10
  76. #define IMX335_TPG_V_COLOR_BARS 11
  77. #define IMX335_REG_TPG_COLORWIDTH CCI_REG8(0x32a0)
  78. #define IMX335_REG_BLKLEVEL CCI_REG16_LE(0x3302)
  79. #define IMX335_REG_WRJ_OPEN CCI_REG8(0x336c)
  80. #define IMX335_REG_ADBIT1 CCI_REG16_LE(0x341c)
  81. /* Chip ID */
  82. #define IMX335_REG_ID CCI_REG8(0x3912)
  83. #define IMX335_ID 0x00
  84. /* Data Lanes */
  85. #define IMX335_REG_LANEMODE CCI_REG8(0x3a01)
  86. #define IMX335_2LANE 1
  87. #define IMX335_4LANE 3
  88. #define IMX335_REG_TCLKPOST CCI_REG16_LE(0x3a18)
  89. #define IMX335_REG_TCLKPREPARE CCI_REG16_LE(0x3a1a)
  90. #define IMX335_REG_TCLK_TRAIL CCI_REG16_LE(0x3a1c)
  91. #define IMX335_REG_TCLK_ZERO CCI_REG16_LE(0x3a1e)
  92. #define IMX335_REG_THS_PREPARE CCI_REG16_LE(0x3a20)
  93. #define IMX335_REG_THS_ZERO CCI_REG16_LE(0x3a22)
  94. #define IMX335_REG_THS_TRAIL CCI_REG16_LE(0x3a24)
  95. #define IMX335_REG_THS_EXIT CCI_REG16_LE(0x3a26)
  96. #define IMX335_REG_TPLX CCI_REG16_LE(0x3a28)
  97. /* Input clock rate */
  98. #define IMX335_INCLK_RATE 24000000
  99. /* CSI2 HW configuration */
  100. #define IMX335_LINK_FREQ_594MHz 594000000LL
  101. #define IMX335_LINK_FREQ_445MHz 445500000LL
  102. #define IMX335_NUM_DATA_LANES 4
  103. /* IMX335 native and active pixel array size. */
  104. static const struct v4l2_rect imx335_native_area = {
  105. .top = 0,
  106. .left = 0,
  107. .width = 2696,
  108. .height = 2044,
  109. };
  110. static const struct v4l2_rect imx335_active_area = {
  111. .top = 50,
  112. .left = 36,
  113. .width = 2624,
  114. .height = 1944,
  115. };
  116. /**
  117. * struct imx335_reg_list - imx335 sensor register list
  118. * @num_of_regs: Number of registers in the list
  119. * @regs: Pointer to register list
  120. */
  121. struct imx335_reg_list {
  122. u32 num_of_regs;
  123. const struct cci_reg_sequence *regs;
  124. };
  125. static const char * const imx335_supply_name[] = {
  126. "avdd", /* Analog (2.9V) supply */
  127. "ovdd", /* Digital I/O (1.8V) supply */
  128. "dvdd", /* Digital Core (1.2V) supply */
  129. };
  130. enum imx335_scan_mode {
  131. IMX335_ALL_PIXEL,
  132. IMX335_2_2_BINNING,
  133. };
  134. /**
  135. * struct imx335_mode - imx335 sensor mode structure
  136. * @scan_mode: Configuration scan mode (All pixel / 2x2Binning)
  137. * @width: Frame width
  138. * @height: Frame height
  139. * @code: Format code
  140. * @hblank: Horizontal blanking in lines
  141. * @vblank: Vertical blanking in lines
  142. * @vblank_min: Minimum vertical blanking in lines
  143. * @vblank_max: Maximum vertical blanking in lines
  144. * @pclk: Sensor pixel clock
  145. * @reg_list: Register list for sensor mode
  146. * @vflip_normal: Register list vflip (normal readout)
  147. * @vflip_inverted: Register list vflip (inverted readout)
  148. */
  149. struct imx335_mode {
  150. enum imx335_scan_mode scan_mode;
  151. u32 width;
  152. u32 height;
  153. u32 code;
  154. u32 hblank;
  155. u32 vblank;
  156. u32 vblank_min;
  157. u32 vblank_max;
  158. u64 pclk;
  159. struct imx335_reg_list reg_list;
  160. struct imx335_reg_list vflip_normal;
  161. struct imx335_reg_list vflip_inverted;
  162. };
  163. /**
  164. * struct imx335 - imx335 sensor device structure
  165. * @dev: Pointer to generic device
  166. * @client: Pointer to i2c client
  167. * @sd: V4L2 sub-device
  168. * @pad: Media pad. Only one pad supported
  169. * @reset_gpio: Sensor reset gpio
  170. * @supplies: Regulator supplies to handle power control
  171. * @cci: CCI register map
  172. * @inclk: Sensor input clock
  173. * @ctrl_handler: V4L2 control handler
  174. * @link_freq_ctrl: Pointer to link frequency control
  175. * @pclk_ctrl: Pointer to pixel clock control
  176. * @hblank_ctrl: Pointer to horizontal blanking control
  177. * @vblank_ctrl: Pointer to vertical blanking control
  178. * @vflip: Pointer to vertical flip control
  179. * @exp_ctrl: Pointer to exposure control
  180. * @again_ctrl: Pointer to analog gain control
  181. * @vblank: Vertical blanking in lines
  182. * @lane_mode: Mode for number of connected data lanes
  183. * @cur_mode: Pointer to current selected sensor mode
  184. * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  185. * @cur_mbus_code: Currently selected media bus format code
  186. */
  187. struct imx335 {
  188. struct device *dev;
  189. struct i2c_client *client;
  190. struct v4l2_subdev sd;
  191. struct media_pad pad;
  192. struct gpio_desc *reset_gpio;
  193. struct regulator_bulk_data supplies[ARRAY_SIZE(imx335_supply_name)];
  194. struct regmap *cci;
  195. struct clk *inclk;
  196. struct v4l2_ctrl_handler ctrl_handler;
  197. struct v4l2_ctrl *link_freq_ctrl;
  198. struct v4l2_ctrl *pclk_ctrl;
  199. struct v4l2_ctrl *hblank_ctrl;
  200. struct v4l2_ctrl *vblank_ctrl;
  201. struct v4l2_ctrl *vflip;
  202. struct {
  203. struct v4l2_ctrl *exp_ctrl;
  204. struct v4l2_ctrl *again_ctrl;
  205. };
  206. u32 vblank;
  207. u32 lane_mode;
  208. const struct imx335_mode *cur_mode;
  209. unsigned long link_freq_bitmap;
  210. u32 cur_mbus_code;
  211. };
  212. static const char * const imx335_tpg_menu[] = {
  213. "Disabled",
  214. "All 000h",
  215. "All FFFh",
  216. "All 555h",
  217. "All AAAh",
  218. "Toggle 555/AAAh",
  219. "Toggle AAA/555h",
  220. "Toggle 000/555h",
  221. "Toggle 555/000h",
  222. "Toggle 000/FFFh",
  223. "Toggle FFF/000h",
  224. "Horizontal color bars",
  225. "Vertical color bars",
  226. };
  227. static const int imx335_tpg_val[] = {
  228. IMX335_TPG_ALL_000,
  229. IMX335_TPG_ALL_000,
  230. IMX335_TPG_ALL_FFF,
  231. IMX335_TPG_ALL_555,
  232. IMX335_TPG_ALL_AAA,
  233. IMX335_TPG_TOG_555_AAA,
  234. IMX335_TPG_TOG_AAA_555,
  235. IMX335_TPG_TOG_000_555,
  236. IMX335_TPG_TOG_555_000,
  237. IMX335_TPG_TOG_000_FFF,
  238. IMX335_TPG_TOG_FFF_000,
  239. IMX335_TPG_H_COLOR_BARS,
  240. IMX335_TPG_V_COLOR_BARS,
  241. };
  242. /* Sensor mode registers */
  243. static const struct cci_reg_sequence mode_2592x1944_regs[] = {
  244. { IMX335_REG_MODE_SELECT, IMX335_MODE_STANDBY },
  245. { IMX335_REG_MASTER_MODE, 0x00 },
  246. { IMX335_REG_WINMODE, 0x04 },
  247. { IMX335_REG_HMAX, 550 },
  248. { IMX335_REG_HTRIMMING_START, 48 },
  249. { IMX335_REG_HNUM, 2592 },
  250. { IMX335_REG_Y_OUT_SIZE, 1944 },
  251. { IMX335_REG_AREA2_WIDTH_1, 40 },
  252. { IMX335_REG_AREA3_WIDTH_1, 3928 },
  253. { IMX335_REG_OPB_SIZE_V, 0 },
  254. { IMX335_REG_XVS_XHS_DRV, 0x00 },
  255. };
  256. static const struct cci_reg_sequence mode_1312x972_regs[] = {
  257. { IMX335_REG_MODE_SELECT, IMX335_MODE_STANDBY },
  258. { IMX335_REG_MASTER_MODE, 0x00 },
  259. { IMX335_REG_WINMODE, 0x01 },
  260. { IMX335_REG_HMAX, 275 },
  261. { IMX335_REG_HTRIMMING_START, 48 },
  262. { IMX335_REG_HNUM, 2600 },
  263. { IMX335_REG_Y_OUT_SIZE, 972 },
  264. { IMX335_REG_AREA2_WIDTH_1, 48 },
  265. { IMX335_REG_AREA3_WIDTH_1, 3936 },
  266. { IMX335_REG_OPB_SIZE_V, 0 },
  267. { IMX335_REG_XVS_XHS_DRV, 0x00 },
  268. { CCI_REG8(0x3300), 1 }, /* TCYCLE */
  269. { CCI_REG8(0x3199), 0x30 }, /* HADD/VADD */
  270. };
  271. static const struct cci_reg_sequence imx335_common_regs[] = {
  272. { CCI_REG8(0x3288), 0x21 },
  273. { CCI_REG8(0x328a), 0x02 },
  274. { CCI_REG8(0x3414), 0x05 },
  275. { CCI_REG8(0x3416), 0x18 },
  276. { CCI_REG8(0x3648), 0x01 },
  277. { CCI_REG8(0x364a), 0x04 },
  278. { CCI_REG8(0x364c), 0x04 },
  279. { CCI_REG8(0x3678), 0x01 },
  280. { CCI_REG8(0x367c), 0x31 },
  281. { CCI_REG8(0x367e), 0x31 },
  282. { CCI_REG8(0x3706), 0x10 },
  283. { CCI_REG8(0x3708), 0x03 },
  284. { CCI_REG8(0x3714), 0x02 },
  285. { CCI_REG8(0x3715), 0x02 },
  286. { CCI_REG8(0x3716), 0x01 },
  287. { CCI_REG8(0x3717), 0x03 },
  288. { CCI_REG8(0x371c), 0x3d },
  289. { CCI_REG8(0x371d), 0x3f },
  290. { CCI_REG8(0x372c), 0x00 },
  291. { CCI_REG8(0x372d), 0x00 },
  292. { CCI_REG8(0x372e), 0x46 },
  293. { CCI_REG8(0x372f), 0x00 },
  294. { CCI_REG8(0x3730), 0x89 },
  295. { CCI_REG8(0x3731), 0x00 },
  296. { CCI_REG8(0x3732), 0x08 },
  297. { CCI_REG8(0x3733), 0x01 },
  298. { CCI_REG8(0x3734), 0xfe },
  299. { CCI_REG8(0x3735), 0x05 },
  300. { CCI_REG8(0x3740), 0x02 },
  301. { CCI_REG8(0x375d), 0x00 },
  302. { CCI_REG8(0x375e), 0x00 },
  303. { CCI_REG8(0x375f), 0x11 },
  304. { CCI_REG8(0x3760), 0x01 },
  305. { CCI_REG8(0x3768), 0x1b },
  306. { CCI_REG8(0x3769), 0x1b },
  307. { CCI_REG8(0x376a), 0x1b },
  308. { CCI_REG8(0x376b), 0x1b },
  309. { CCI_REG8(0x376c), 0x1a },
  310. { CCI_REG8(0x376d), 0x17 },
  311. { CCI_REG8(0x376e), 0x0f },
  312. { CCI_REG8(0x3776), 0x00 },
  313. { CCI_REG8(0x3777), 0x00 },
  314. { CCI_REG8(0x3778), 0x46 },
  315. { CCI_REG8(0x3779), 0x00 },
  316. { CCI_REG8(0x377a), 0x89 },
  317. { CCI_REG8(0x377b), 0x00 },
  318. { CCI_REG8(0x377c), 0x08 },
  319. { CCI_REG8(0x377d), 0x01 },
  320. { CCI_REG8(0x377e), 0x23 },
  321. { CCI_REG8(0x377f), 0x02 },
  322. { CCI_REG8(0x3780), 0xd9 },
  323. { CCI_REG8(0x3781), 0x03 },
  324. { CCI_REG8(0x3782), 0xf5 },
  325. { CCI_REG8(0x3783), 0x06 },
  326. { CCI_REG8(0x3784), 0xa5 },
  327. { CCI_REG8(0x3788), 0x0f },
  328. { CCI_REG8(0x378a), 0xd9 },
  329. { CCI_REG8(0x378b), 0x03 },
  330. { CCI_REG8(0x378c), 0xeb },
  331. { CCI_REG8(0x378d), 0x05 },
  332. { CCI_REG8(0x378e), 0x87 },
  333. { CCI_REG8(0x378f), 0x06 },
  334. { CCI_REG8(0x3790), 0xf5 },
  335. { CCI_REG8(0x3792), 0x43 },
  336. { CCI_REG8(0x3794), 0x7a },
  337. { CCI_REG8(0x3796), 0xa1 },
  338. { CCI_REG8(0x37b0), 0x36 },
  339. { CCI_REG8(0x3a00), 0x00 },
  340. };
  341. static const struct cci_reg_sequence mode_2592x1944_vflip_normal[] = {
  342. { IMX335_REG_AREA3_ST_ADR_1, 176 },
  343. /* Undocumented V-Flip related registers on Page 55 of datasheet. */
  344. { CCI_REG8(0x3081), 0x02, },
  345. { CCI_REG8(0x3083), 0x02, },
  346. { CCI_REG16_LE(0x30b6), 0x00 },
  347. { CCI_REG16_LE(0x3116), 0x08 },
  348. };
  349. static const struct cci_reg_sequence mode_2592x1944_vflip_inverted[] = {
  350. { IMX335_REG_AREA3_ST_ADR_1, 4112 },
  351. /* Undocumented V-Flip related registers on Page 55 of datasheet. */
  352. { CCI_REG8(0x3081), 0xfe, },
  353. { CCI_REG8(0x3083), 0xfe, },
  354. { CCI_REG16_LE(0x30b6), 0x1fa },
  355. { CCI_REG16_LE(0x3116), 0x002 },
  356. };
  357. static const struct cci_reg_sequence mode_1312x972_vflip_normal[] = {
  358. { IMX335_REG_AREA3_ST_ADR_1, 176 },
  359. /* Undocumented */
  360. { CCI_REG8(0x3078), 0x04 },
  361. { CCI_REG8(0x3079), 0xfd },
  362. { CCI_REG8(0x307a), 0x04 },
  363. { CCI_REG8(0x307b), 0xfe },
  364. { CCI_REG8(0x307c), 0x04 },
  365. { CCI_REG8(0x307d), 0xfb },
  366. { CCI_REG8(0x307e), 0x04 },
  367. { CCI_REG8(0x307f), 0x02 },
  368. { CCI_REG8(0x3080), 0x04 },
  369. { CCI_REG8(0x3081), 0xfd },
  370. { CCI_REG8(0x3082), 0x04 },
  371. { CCI_REG8(0x3083), 0xfe },
  372. { CCI_REG8(0x3084), 0x04 },
  373. { CCI_REG8(0x3085), 0xfb },
  374. { CCI_REG8(0x3086), 0x04 },
  375. { CCI_REG8(0x3087), 0x02 },
  376. { CCI_REG8(0x30a4), 0x77 },
  377. { CCI_REG8(0x30a8), 0x20 },
  378. { CCI_REG8(0x30a9), 0x00 },
  379. { CCI_REG8(0x30ac), 0x08 },
  380. { CCI_REG8(0x30ad), 0x08 },
  381. { CCI_REG8(0x30b0), 0x20 },
  382. { CCI_REG8(0x30b1), 0x00 },
  383. { CCI_REG8(0x30b4), 0x10 },
  384. { CCI_REG8(0x30b5), 0x10 },
  385. { CCI_REG16_LE(0x30b6), 0x00 },
  386. { CCI_REG16_LE(0x3112), 0x10 },
  387. { CCI_REG16_LE(0x3116), 0x10 },
  388. };
  389. static const struct cci_reg_sequence mode_1312x972_vflip_inverted[] = {
  390. { IMX335_REG_AREA3_ST_ADR_1, 4112 },
  391. /* Undocumented */
  392. { CCI_REG8(0x3078), 0x04 },
  393. { CCI_REG8(0x3079), 0xfd },
  394. { CCI_REG8(0x307a), 0x04 },
  395. { CCI_REG8(0x307b), 0xfe },
  396. { CCI_REG8(0x307c), 0x04 },
  397. { CCI_REG8(0x307d), 0xfb },
  398. { CCI_REG8(0x307e), 0x04 },
  399. { CCI_REG8(0x307f), 0x02 },
  400. { CCI_REG8(0x3080), 0xfc },
  401. { CCI_REG8(0x3081), 0x05 },
  402. { CCI_REG8(0x3082), 0xfc },
  403. { CCI_REG8(0x3083), 0x02 },
  404. { CCI_REG8(0x3084), 0xfc },
  405. { CCI_REG8(0x3085), 0x03 },
  406. { CCI_REG8(0x3086), 0xfc },
  407. { CCI_REG8(0x3087), 0xfe },
  408. { CCI_REG8(0x30a4), 0x77 },
  409. { CCI_REG8(0x30a8), 0x20 },
  410. { CCI_REG8(0x30a9), 0x00 },
  411. { CCI_REG8(0x30ac), 0x08 },
  412. { CCI_REG8(0x30ad), 0x78 },
  413. { CCI_REG8(0x30b0), 0x20 },
  414. { CCI_REG8(0x30b1), 0x00 },
  415. { CCI_REG8(0x30b4), 0x10 },
  416. { CCI_REG8(0x30b5), 0x70 },
  417. { CCI_REG16_LE(0x30b6), 0x01f2 },
  418. { CCI_REG16_LE(0x3112), 0x10 },
  419. { CCI_REG16_LE(0x3116), 0x02 },
  420. };
  421. static const struct cci_reg_sequence mipi_data_rate_1188Mbps[] = {
  422. { IMX335_REG_BCWAIT_TIME, 0x3b },
  423. { IMX335_REG_CPWAIT_TIME, 0x2a },
  424. { IMX335_REG_INCLKSEL1, 0x00c6 },
  425. { IMX335_REG_INCLKSEL2, 0x02 },
  426. { IMX335_REG_INCLKSEL3, 0xa0 },
  427. { IMX335_REG_INCLKSEL4, 0x7e },
  428. { IMX335_REG_SYSMODE, 0x01 },
  429. { IMX335_REG_TCLKPOST, 0x8f },
  430. { IMX335_REG_TCLKPREPARE, 0x4f },
  431. { IMX335_REG_TCLK_TRAIL, 0x47 },
  432. { IMX335_REG_TCLK_ZERO, 0x0137 },
  433. { IMX335_REG_THS_PREPARE, 0x4f },
  434. { IMX335_REG_THS_ZERO, 0x87 },
  435. { IMX335_REG_THS_TRAIL, 0x4f },
  436. { IMX335_REG_THS_EXIT, 0x7f },
  437. { IMX335_REG_TPLX, 0x3f },
  438. };
  439. static const struct cci_reg_sequence mipi_data_rate_891Mbps[] = {
  440. { IMX335_REG_BCWAIT_TIME, 0x3b },
  441. { IMX335_REG_CPWAIT_TIME, 0x2a },
  442. { IMX335_REG_INCLKSEL1, 0x0129 },
  443. { IMX335_REG_INCLKSEL2, 0x06 },
  444. { IMX335_REG_INCLKSEL3, 0xa0 },
  445. { IMX335_REG_INCLKSEL4, 0x7e },
  446. { IMX335_REG_SYSMODE, 0x02 },
  447. { IMX335_REG_TCLKPOST, 0x7f },
  448. { IMX335_REG_TCLKPREPARE, 0x37 },
  449. { IMX335_REG_TCLK_TRAIL, 0x37 },
  450. { IMX335_REG_TCLK_ZERO, 0xf7 },
  451. { IMX335_REG_THS_PREPARE, 0x3f },
  452. { IMX335_REG_THS_ZERO, 0x6f },
  453. { IMX335_REG_THS_TRAIL, 0x3f },
  454. { IMX335_REG_THS_EXIT, 0x5f },
  455. { IMX335_REG_TPLX, 0x2f },
  456. };
  457. static const s64 link_freq[] = {
  458. /* Corresponds to 1188Mbps data lane rate */
  459. IMX335_LINK_FREQ_594MHz,
  460. /* Corresponds to 891Mbps data lane rate */
  461. IMX335_LINK_FREQ_445MHz,
  462. };
  463. static const struct imx335_reg_list link_freq_reglist[] = {
  464. {
  465. .num_of_regs = ARRAY_SIZE(mipi_data_rate_1188Mbps),
  466. .regs = mipi_data_rate_1188Mbps,
  467. },
  468. {
  469. .num_of_regs = ARRAY_SIZE(mipi_data_rate_891Mbps),
  470. .regs = mipi_data_rate_891Mbps,
  471. },
  472. };
  473. static const u32 imx335_mbus_codes[] = {
  474. MEDIA_BUS_FMT_SRGGB12_1X12,
  475. MEDIA_BUS_FMT_SRGGB10_1X10,
  476. };
  477. /* Supported sensor mode configurations */
  478. static const struct imx335_mode supported_modes[] = {
  479. {
  480. .scan_mode = IMX335_ALL_PIXEL,
  481. .width = 2592,
  482. .height = 1944,
  483. .hblank = 342,
  484. .vblank = 2556,
  485. .vblank_min = 2556,
  486. .vblank_max = 133060,
  487. .pclk = 396000000,
  488. .reg_list = {
  489. .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
  490. .regs = mode_2592x1944_regs,
  491. },
  492. .vflip_normal = {
  493. .num_of_regs = ARRAY_SIZE(mode_2592x1944_vflip_normal),
  494. .regs = mode_2592x1944_vflip_normal,
  495. },
  496. .vflip_inverted = {
  497. .num_of_regs = ARRAY_SIZE(mode_2592x1944_vflip_inverted),
  498. .regs = mode_2592x1944_vflip_inverted,
  499. }
  500. }, {
  501. .scan_mode = IMX335_2_2_BINNING,
  502. .width = 1312,
  503. .height = 972,
  504. .hblank = 155,
  505. .vblank = 3528,
  506. .vblank_min = 3528,
  507. .vblank_max = 133060,
  508. .pclk = 396000000,
  509. .reg_list = {
  510. .num_of_regs = ARRAY_SIZE(mode_1312x972_regs),
  511. .regs = mode_1312x972_regs,
  512. },
  513. .vflip_normal = {
  514. .num_of_regs = ARRAY_SIZE(mode_1312x972_vflip_normal),
  515. .regs = mode_1312x972_vflip_normal,
  516. },
  517. .vflip_inverted = {
  518. .num_of_regs = ARRAY_SIZE(mode_1312x972_vflip_inverted),
  519. .regs = mode_1312x972_vflip_inverted,
  520. },
  521. },
  522. };
  523. /**
  524. * to_imx335() - imx335 V4L2 sub-device to imx335 device.
  525. * @subdev: pointer to imx335 V4L2 sub-device
  526. *
  527. * Return: pointer to imx335 device
  528. */
  529. static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev)
  530. {
  531. return container_of(subdev, struct imx335, sd);
  532. }
  533. /**
  534. * imx335_update_controls() - Update control ranges based on streaming mode
  535. * @imx335: pointer to imx335 device
  536. * @mode: pointer to imx335_mode sensor mode
  537. *
  538. * Return: 0 if successful, error code otherwise.
  539. */
  540. static int imx335_update_controls(struct imx335 *imx335,
  541. const struct imx335_mode *mode)
  542. {
  543. int ret;
  544. ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl,
  545. __ffs(imx335->link_freq_bitmap));
  546. if (ret)
  547. return ret;
  548. ret = __v4l2_ctrl_modify_range(imx335->hblank_ctrl, mode->hblank,
  549. mode->hblank, 1, mode->hblank);
  550. if (ret)
  551. return ret;
  552. return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min,
  553. mode->vblank_max, 1, mode->vblank);
  554. }
  555. /**
  556. * imx335_update_exp_gain() - Set updated exposure and gain
  557. * @imx335: pointer to imx335 device
  558. * @exposure: updated exposure value
  559. * @gain: updated analog gain value
  560. *
  561. * Return: 0 if successful, error code otherwise.
  562. */
  563. static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain)
  564. {
  565. u32 lpfr, shutter;
  566. int ret_hold;
  567. int ret = 0;
  568. lpfr = imx335->vblank + imx335->cur_mode->height;
  569. shutter = lpfr - exposure;
  570. dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u\n",
  571. exposure, gain, shutter, lpfr);
  572. cci_write(imx335->cci, IMX335_REG_HOLD, 1, &ret);
  573. cci_write(imx335->cci, IMX335_REG_VMAX, lpfr, &ret);
  574. cci_write(imx335->cci, IMX335_REG_SHUTTER, shutter, &ret);
  575. cci_write(imx335->cci, IMX335_REG_GAIN, gain, &ret);
  576. /*
  577. * Unconditionally attempt to release the hold, but track the
  578. * error if the unhold itself fails.
  579. */
  580. ret_hold = cci_write(imx335->cci, IMX335_REG_HOLD, 0, NULL);
  581. if (ret_hold)
  582. ret = ret_hold;
  583. return ret;
  584. }
  585. static int imx335_update_vertical_flip(struct imx335 *imx335, u32 vflip)
  586. {
  587. const struct imx335_reg_list * const vflip_regs =
  588. vflip ? &imx335->cur_mode->vflip_inverted :
  589. &imx335->cur_mode->vflip_normal;
  590. int ret = 0;
  591. cci_multi_reg_write(imx335->cci, vflip_regs->regs,
  592. vflip_regs->num_of_regs, &ret);
  593. return cci_write(imx335->cci, IMX335_REG_VREVERSE, vflip, &ret);
  594. }
  595. static int imx335_update_test_pattern(struct imx335 *imx335, u32 pattern_index)
  596. {
  597. int ret = 0;
  598. if (pattern_index >= ARRAY_SIZE(imx335_tpg_val))
  599. return -EINVAL;
  600. if (pattern_index) {
  601. const struct cci_reg_sequence tpg_enable_regs[] = {
  602. { IMX335_REG_TPG_TESTCLKEN, 0x10 },
  603. { IMX335_REG_TPG_DIG_CLP_MODE, 0x00 },
  604. { IMX335_REG_TPG_EN_DUOUT, 0x01 },
  605. { IMX335_REG_TPG_COLORWIDTH, 0x11 },
  606. { IMX335_REG_BLKLEVEL, 0x00 },
  607. { IMX335_REG_WRJ_OPEN, 0x00 },
  608. };
  609. cci_write(imx335->cci, IMX335_REG_TPG,
  610. imx335_tpg_val[pattern_index], &ret);
  611. cci_multi_reg_write(imx335->cci, tpg_enable_regs,
  612. ARRAY_SIZE(tpg_enable_regs), &ret);
  613. } else {
  614. const struct cci_reg_sequence tpg_disable_regs[] = {
  615. { IMX335_REG_TPG_TESTCLKEN, 0x00 },
  616. { IMX335_REG_TPG_DIG_CLP_MODE, 0x01 },
  617. { IMX335_REG_TPG_EN_DUOUT, 0x00 },
  618. { IMX335_REG_TPG_COLORWIDTH, 0x10 },
  619. { IMX335_REG_BLKLEVEL, 0x32 },
  620. { IMX335_REG_WRJ_OPEN, 0x01 },
  621. };
  622. cci_multi_reg_write(imx335->cci, tpg_disable_regs,
  623. ARRAY_SIZE(tpg_disable_regs), &ret);
  624. }
  625. return ret;
  626. }
  627. /**
  628. * imx335_set_ctrl() - Set subdevice control
  629. * @ctrl: pointer to v4l2_ctrl structure
  630. *
  631. * Supported controls:
  632. * - V4L2_CID_VBLANK
  633. * - cluster controls:
  634. * - V4L2_CID_ANALOGUE_GAIN
  635. * - V4L2_CID_EXPOSURE
  636. *
  637. * Return: 0 if successful, error code otherwise.
  638. */
  639. static int imx335_set_ctrl(struct v4l2_ctrl *ctrl)
  640. {
  641. struct imx335 *imx335 =
  642. container_of(ctrl->handler, struct imx335, ctrl_handler);
  643. u32 analog_gain;
  644. u32 exposure;
  645. int ret;
  646. /* Propagate change of current control to all related controls */
  647. if (ctrl->id == V4L2_CID_VBLANK) {
  648. u32 shutter_min = IMX335_SHUTTER_MIN;
  649. u32 lpfr;
  650. imx335->vblank = imx335->vblank_ctrl->val;
  651. lpfr = imx335->vblank + imx335->cur_mode->height;
  652. dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u\n",
  653. imx335->vblank, lpfr);
  654. if (imx335->cur_mode->scan_mode == IMX335_2_2_BINNING)
  655. shutter_min = IMX335_SHUTTER_MIN_BINNED;
  656. ret = __v4l2_ctrl_modify_range(imx335->exp_ctrl,
  657. IMX335_EXPOSURE_MIN,
  658. lpfr - shutter_min, 1,
  659. IMX335_EXPOSURE_DEFAULT);
  660. if (ret)
  661. return ret;
  662. }
  663. /*
  664. * Applying V4L2 control value only happens
  665. * when power is up for streaming.
  666. */
  667. if (pm_runtime_get_if_in_use(imx335->dev) == 0)
  668. return 0;
  669. switch (ctrl->id) {
  670. case V4L2_CID_VBLANK:
  671. exposure = imx335->exp_ctrl->val;
  672. analog_gain = imx335->again_ctrl->val;
  673. ret = imx335_update_exp_gain(imx335, exposure, analog_gain);
  674. break;
  675. case V4L2_CID_EXPOSURE:
  676. exposure = ctrl->val;
  677. analog_gain = imx335->again_ctrl->val;
  678. dev_dbg(imx335->dev, "Received exp %u, analog gain %u\n",
  679. exposure, analog_gain);
  680. ret = imx335_update_exp_gain(imx335, exposure, analog_gain);
  681. break;
  682. case V4L2_CID_VFLIP:
  683. ret = imx335_update_vertical_flip(imx335, ctrl->val);
  684. break;
  685. case V4L2_CID_TEST_PATTERN:
  686. ret = imx335_update_test_pattern(imx335, ctrl->val);
  687. break;
  688. default:
  689. dev_err(imx335->dev, "Invalid control %d\n", ctrl->id);
  690. ret = -EINVAL;
  691. }
  692. pm_runtime_put(imx335->dev);
  693. return ret;
  694. }
  695. /* V4l2 subdevice control ops*/
  696. static const struct v4l2_ctrl_ops imx335_ctrl_ops = {
  697. .s_ctrl = imx335_set_ctrl,
  698. };
  699. static int imx335_get_format_code(struct imx335 *imx335, u32 code)
  700. {
  701. unsigned int i;
  702. for (i = 0; i < ARRAY_SIZE(imx335_mbus_codes); i++) {
  703. if (imx335_mbus_codes[i] == code)
  704. return imx335_mbus_codes[i];
  705. }
  706. return imx335_mbus_codes[0];
  707. }
  708. /**
  709. * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  710. * @sd: pointer to imx335 V4L2 sub-device structure
  711. * @sd_state: V4L2 sub-device configuration
  712. * @code: V4L2 sub-device code enumeration need to be filled
  713. *
  714. * Return: 0 if successful, error code otherwise.
  715. */
  716. static int imx335_enum_mbus_code(struct v4l2_subdev *sd,
  717. struct v4l2_subdev_state *sd_state,
  718. struct v4l2_subdev_mbus_code_enum *code)
  719. {
  720. if (code->index >= ARRAY_SIZE(imx335_mbus_codes))
  721. return -EINVAL;
  722. code->code = imx335_mbus_codes[code->index];
  723. return 0;
  724. }
  725. /**
  726. * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  727. * @sd: pointer to imx335 V4L2 sub-device structure
  728. * @sd_state: V4L2 sub-device configuration
  729. * @fsize: V4L2 sub-device size enumeration need to be filled
  730. *
  731. * Return: 0 if successful, error code otherwise.
  732. */
  733. static int imx335_enum_frame_size(struct v4l2_subdev *sd,
  734. struct v4l2_subdev_state *sd_state,
  735. struct v4l2_subdev_frame_size_enum *fsize)
  736. {
  737. struct imx335 *imx335 = to_imx335(sd);
  738. u32 code;
  739. if (fsize->index >= ARRAY_SIZE(supported_modes))
  740. return -EINVAL;
  741. code = imx335_get_format_code(imx335, fsize->code);
  742. if (fsize->code != code)
  743. return -EINVAL;
  744. fsize->min_width = supported_modes[fsize->index].width;
  745. fsize->max_width = fsize->min_width;
  746. fsize->min_height = supported_modes[fsize->index].height;
  747. fsize->max_height = fsize->min_height;
  748. return 0;
  749. }
  750. /**
  751. * imx335_fill_pad_format() - Fill subdevice pad format
  752. * from selected sensor mode
  753. * @imx335: pointer to imx335 device
  754. * @mode: pointer to imx335_mode sensor mode
  755. * @fmt: V4L2 sub-device format need to be filled
  756. */
  757. static void imx335_fill_pad_format(struct imx335 *imx335,
  758. const struct imx335_mode *mode,
  759. struct v4l2_subdev_format *fmt)
  760. {
  761. fmt->format.width = mode->width;
  762. fmt->format.height = mode->height;
  763. fmt->format.code = imx335->cur_mbus_code;
  764. fmt->format.field = V4L2_FIELD_NONE;
  765. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  766. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  767. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  768. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  769. }
  770. /**
  771. * imx335_set_pad_format() - Set subdevice pad format
  772. * @sd: pointer to imx335 V4L2 sub-device structure
  773. * @sd_state: V4L2 sub-device configuration
  774. * @fmt: V4L2 sub-device format need to be set
  775. *
  776. * Return: 0 if successful, error code otherwise.
  777. */
  778. static int imx335_set_pad_format(struct v4l2_subdev *sd,
  779. struct v4l2_subdev_state *sd_state,
  780. struct v4l2_subdev_format *fmt)
  781. {
  782. struct imx335 *imx335 = to_imx335(sd);
  783. struct v4l2_mbus_framefmt *format;
  784. const struct imx335_mode *mode;
  785. struct v4l2_rect *crop;
  786. int i, ret = 0;
  787. mode = v4l2_find_nearest_size(supported_modes,
  788. ARRAY_SIZE(supported_modes),
  789. width, height,
  790. fmt->format.width, fmt->format.height);
  791. for (i = 0; i < ARRAY_SIZE(imx335_mbus_codes); i++) {
  792. if (imx335_mbus_codes[i] == fmt->format.code)
  793. imx335->cur_mbus_code = imx335_mbus_codes[i];
  794. }
  795. imx335_fill_pad_format(imx335, mode, fmt);
  796. format = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  797. *format = fmt->format;
  798. crop = v4l2_subdev_state_get_crop(sd_state, fmt->pad);
  799. crop->width = fmt->format.width;
  800. crop->height = fmt->format.height;
  801. if (mode->scan_mode == IMX335_2_2_BINNING) {
  802. crop->width *= 2;
  803. crop->height *= 2;
  804. }
  805. crop->left = (imx335_native_area.width - crop->width) / 2;
  806. crop->top = (imx335_native_area.height - crop->height) / 2;
  807. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  808. ret = imx335_update_controls(imx335, mode);
  809. if (!ret)
  810. imx335->cur_mode = mode;
  811. }
  812. return ret;
  813. }
  814. /**
  815. * imx335_init_state() - Initialize sub-device state
  816. * @sd: pointer to imx335 V4L2 sub-device structure
  817. * @sd_state: V4L2 sub-device configuration
  818. *
  819. * Return: 0 if successful, error code otherwise.
  820. */
  821. static int imx335_init_state(struct v4l2_subdev *sd,
  822. struct v4l2_subdev_state *sd_state)
  823. {
  824. struct imx335 *imx335 = to_imx335(sd);
  825. struct v4l2_subdev_format fmt = { 0 };
  826. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  827. imx335_fill_pad_format(imx335, &supported_modes[0], &fmt);
  828. __v4l2_ctrl_modify_range(imx335->link_freq_ctrl, 0,
  829. __fls(imx335->link_freq_bitmap),
  830. ~(imx335->link_freq_bitmap),
  831. __ffs(imx335->link_freq_bitmap));
  832. return imx335_set_pad_format(sd, sd_state, &fmt);
  833. }
  834. /**
  835. * imx335_get_selection() - Selection API
  836. * @sd: pointer to imx335 V4L2 sub-device structure
  837. * @sd_state: V4L2 sub-device configuration
  838. * @sel: V4L2 selection info
  839. *
  840. * Return: 0 if successful, error code otherwise.
  841. */
  842. static int imx335_get_selection(struct v4l2_subdev *sd,
  843. struct v4l2_subdev_state *sd_state,
  844. struct v4l2_subdev_selection *sel)
  845. {
  846. switch (sel->target) {
  847. case V4L2_SEL_TGT_CROP:
  848. sel->r = *v4l2_subdev_state_get_crop(sd_state, 0);
  849. return 0;
  850. case V4L2_SEL_TGT_NATIVE_SIZE:
  851. sel->r = imx335_native_area;
  852. return 0;
  853. case V4L2_SEL_TGT_CROP_DEFAULT:
  854. case V4L2_SEL_TGT_CROP_BOUNDS:
  855. sel->r = imx335_active_area;
  856. return 0;
  857. }
  858. return -EINVAL;
  859. }
  860. static int imx335_set_framefmt(struct imx335 *imx335)
  861. {
  862. /*
  863. * In the all-pixel scan mode the AD conversion shall match the output
  864. * bit width requested.
  865. *
  866. * However, when 2/2 binning is enabled, the AD conversion is always
  867. * 10-bit, so we ensure ADBIT is clear and ADBIT1 is assigned 0x1ff.
  868. * That's as much as the documentation gives us...
  869. */
  870. int ret = 0;
  871. u8 bpp = imx335->cur_mbus_code == MEDIA_BUS_FMT_SRGGB10_1X10 ? 10 : 12;
  872. u8 ad_conv = bpp;
  873. /* Start with the output mode */
  874. cci_write(imx335->cci, IMX335_REG_MDBIT, bpp == 12, &ret);
  875. /* Enforce 10 bit AD on binning modes */
  876. if (imx335->cur_mode->scan_mode == IMX335_2_2_BINNING)
  877. ad_conv = 10;
  878. /* AD Conversion configuration */
  879. if (ad_conv == 10) {
  880. cci_write(imx335->cci, IMX335_REG_ADBIT, 0x00, &ret);
  881. cci_write(imx335->cci, IMX335_REG_ADBIT1, 0x1ff, &ret);
  882. } else { /* 12 bit AD Conversion */
  883. cci_write(imx335->cci, IMX335_REG_ADBIT, 0x01, &ret);
  884. cci_write(imx335->cci, IMX335_REG_ADBIT1, 0x47, &ret);
  885. }
  886. return ret;
  887. }
  888. /**
  889. * imx335_enable_streams() - Enable sensor streams
  890. * @sd: V4L2 subdevice
  891. * @state: V4L2 subdevice state
  892. * @pad: The pad to enable
  893. * @streams_mask: Bitmask of streams to enable
  894. *
  895. * Return: 0 if successful, error code otherwise.
  896. */
  897. static int imx335_enable_streams(struct v4l2_subdev *sd,
  898. struct v4l2_subdev_state *state, u32 pad,
  899. u64 streams_mask)
  900. {
  901. struct imx335 *imx335 = to_imx335(sd);
  902. const struct imx335_reg_list *reg_list;
  903. int ret;
  904. ret = pm_runtime_resume_and_get(imx335->dev);
  905. if (ret < 0)
  906. return ret;
  907. /* Setup PLL */
  908. reg_list = &link_freq_reglist[__ffs(imx335->link_freq_bitmap)];
  909. ret = cci_multi_reg_write(imx335->cci, reg_list->regs,
  910. reg_list->num_of_regs, NULL);
  911. if (ret) {
  912. dev_err(imx335->dev, "%s failed to set plls\n", __func__);
  913. goto err_rpm_put;
  914. }
  915. /* Write sensor mode registers */
  916. reg_list = &imx335->cur_mode->reg_list;
  917. ret = cci_multi_reg_write(imx335->cci, reg_list->regs,
  918. reg_list->num_of_regs, NULL);
  919. if (ret) {
  920. dev_err(imx335->dev, "fail to write initial registers\n");
  921. goto err_rpm_put;
  922. }
  923. /* Write sensor common registers */
  924. ret = cci_multi_reg_write(imx335->cci, imx335_common_regs,
  925. ARRAY_SIZE(imx335_common_regs), NULL);
  926. if (ret) {
  927. dev_err(imx335->dev, "fail to write initial registers\n");
  928. goto err_rpm_put;
  929. }
  930. ret = imx335_set_framefmt(imx335);
  931. if (ret) {
  932. dev_err(imx335->dev, "%s failed to set frame format: %d\n",
  933. __func__, ret);
  934. goto err_rpm_put;
  935. }
  936. /* Configure lanes */
  937. ret = cci_write(imx335->cci, IMX335_REG_LANEMODE,
  938. imx335->lane_mode, NULL);
  939. if (ret)
  940. goto err_rpm_put;
  941. /* Setup handler will write actual exposure and gain */
  942. ret = __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler);
  943. if (ret) {
  944. dev_err(imx335->dev, "fail to setup handler\n");
  945. goto err_rpm_put;
  946. }
  947. /* Start streaming */
  948. ret = cci_write(imx335->cci, IMX335_REG_MODE_SELECT,
  949. IMX335_MODE_STREAMING, NULL);
  950. if (ret) {
  951. dev_err(imx335->dev, "fail to start streaming\n");
  952. goto err_rpm_put;
  953. }
  954. /* Initial regulator stabilization period */
  955. usleep_range(18000, 20000);
  956. return 0;
  957. err_rpm_put:
  958. pm_runtime_put(imx335->dev);
  959. return ret;
  960. }
  961. /**
  962. * imx335_disable_streams() - Disable sensor streams
  963. * @sd: V4L2 subdevice
  964. * @state: V4L2 subdevice state
  965. * @pad: The pad to disable
  966. * @streams_mask: Bitmask of streams to disable
  967. *
  968. * Return: 0 if successful, error code otherwise.
  969. */
  970. static int imx335_disable_streams(struct v4l2_subdev *sd,
  971. struct v4l2_subdev_state *state, u32 pad,
  972. u64 streams_mask)
  973. {
  974. struct imx335 *imx335 = to_imx335(sd);
  975. int ret;
  976. ret = cci_write(imx335->cci, IMX335_REG_MODE_SELECT,
  977. IMX335_MODE_STANDBY, NULL);
  978. pm_runtime_put(imx335->dev);
  979. return ret;
  980. }
  981. /**
  982. * imx335_detect() - Detect imx335 sensor
  983. * @imx335: pointer to imx335 device
  984. *
  985. * Return: 0 if successful, -EIO if sensor id does not match
  986. */
  987. static int imx335_detect(struct imx335 *imx335)
  988. {
  989. int ret;
  990. u64 val;
  991. ret = cci_read(imx335->cci, IMX335_REG_ID, &val, NULL);
  992. if (ret)
  993. return ret;
  994. if (val != IMX335_ID) {
  995. dev_err(imx335->dev, "chip id mismatch: %x!=%llx\n",
  996. IMX335_ID, val);
  997. return -ENXIO;
  998. }
  999. return 0;
  1000. }
  1001. /**
  1002. * imx335_parse_hw_config() - Parse HW configuration and check if supported
  1003. * @imx335: pointer to imx335 device
  1004. *
  1005. * Return: 0 if successful, error code otherwise.
  1006. */
  1007. static int imx335_parse_hw_config(struct imx335 *imx335)
  1008. {
  1009. struct fwnode_handle *fwnode = dev_fwnode(imx335->dev);
  1010. struct v4l2_fwnode_endpoint bus_cfg = {
  1011. .bus_type = V4L2_MBUS_CSI2_DPHY
  1012. };
  1013. struct fwnode_handle *ep;
  1014. unsigned long rate;
  1015. unsigned int i;
  1016. int ret;
  1017. if (!fwnode)
  1018. return -ENXIO;
  1019. /* Request optional reset pin */
  1020. imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset",
  1021. GPIOD_OUT_HIGH);
  1022. if (IS_ERR(imx335->reset_gpio)) {
  1023. dev_err(imx335->dev, "failed to get reset gpio %pe\n",
  1024. imx335->reset_gpio);
  1025. return PTR_ERR(imx335->reset_gpio);
  1026. }
  1027. for (i = 0; i < ARRAY_SIZE(imx335_supply_name); i++)
  1028. imx335->supplies[i].supply = imx335_supply_name[i];
  1029. ret = devm_regulator_bulk_get(imx335->dev,
  1030. ARRAY_SIZE(imx335_supply_name),
  1031. imx335->supplies);
  1032. if (ret) {
  1033. dev_err(imx335->dev, "Failed to get regulators\n");
  1034. return ret;
  1035. }
  1036. /* Get sensor input clock */
  1037. imx335->inclk = devm_v4l2_sensor_clk_get(imx335->dev, NULL);
  1038. if (IS_ERR(imx335->inclk))
  1039. return dev_err_probe(imx335->dev, PTR_ERR(imx335->inclk),
  1040. "could not get inclk\n");
  1041. rate = clk_get_rate(imx335->inclk);
  1042. if (rate != IMX335_INCLK_RATE) {
  1043. dev_err(imx335->dev, "inclk frequency mismatch\n");
  1044. return -EINVAL;
  1045. }
  1046. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1047. if (!ep) {
  1048. dev_err(imx335->dev, "Failed to get next endpoint\n");
  1049. return -ENXIO;
  1050. }
  1051. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1052. fwnode_handle_put(ep);
  1053. if (ret)
  1054. return ret;
  1055. switch (bus_cfg.bus.mipi_csi2.num_data_lanes) {
  1056. case 2:
  1057. imx335->lane_mode = IMX335_2LANE;
  1058. break;
  1059. case 4:
  1060. imx335->lane_mode = IMX335_4LANE;
  1061. break;
  1062. default:
  1063. dev_err(imx335->dev,
  1064. "number of CSI2 data lanes %d is not supported\n",
  1065. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1066. ret = -EINVAL;
  1067. goto done_endpoint_free;
  1068. }
  1069. ret = v4l2_link_freq_to_bitmap(imx335->dev, bus_cfg.link_frequencies,
  1070. bus_cfg.nr_of_link_frequencies,
  1071. link_freq, ARRAY_SIZE(link_freq),
  1072. &imx335->link_freq_bitmap);
  1073. done_endpoint_free:
  1074. v4l2_fwnode_endpoint_free(&bus_cfg);
  1075. return ret;
  1076. }
  1077. /* V4l2 subdevice ops */
  1078. static const struct v4l2_subdev_video_ops imx335_video_ops = {
  1079. .s_stream = v4l2_subdev_s_stream_helper,
  1080. };
  1081. static const struct v4l2_subdev_pad_ops imx335_pad_ops = {
  1082. .enum_mbus_code = imx335_enum_mbus_code,
  1083. .enum_frame_size = imx335_enum_frame_size,
  1084. .get_selection = imx335_get_selection,
  1085. .set_selection = imx335_get_selection,
  1086. .get_fmt = v4l2_subdev_get_fmt,
  1087. .set_fmt = imx335_set_pad_format,
  1088. .enable_streams = imx335_enable_streams,
  1089. .disable_streams = imx335_disable_streams,
  1090. };
  1091. static const struct v4l2_subdev_ops imx335_subdev_ops = {
  1092. .video = &imx335_video_ops,
  1093. .pad = &imx335_pad_ops,
  1094. };
  1095. static const struct v4l2_subdev_internal_ops imx335_internal_ops = {
  1096. .init_state = imx335_init_state,
  1097. };
  1098. /**
  1099. * imx335_power_on() - Sensor power on sequence
  1100. * @dev: pointer to i2c device
  1101. *
  1102. * Return: 0 if successful, error code otherwise.
  1103. */
  1104. static int imx335_power_on(struct device *dev)
  1105. {
  1106. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1107. struct imx335 *imx335 = to_imx335(sd);
  1108. int ret;
  1109. ret = regulator_bulk_enable(ARRAY_SIZE(imx335_supply_name),
  1110. imx335->supplies);
  1111. if (ret) {
  1112. dev_err(dev, "%s: failed to enable regulators\n",
  1113. __func__);
  1114. return ret;
  1115. }
  1116. usleep_range(500, 550); /* Tlow */
  1117. gpiod_set_value_cansleep(imx335->reset_gpio, 0);
  1118. ret = clk_prepare_enable(imx335->inclk);
  1119. if (ret) {
  1120. dev_err(imx335->dev, "fail to enable inclk\n");
  1121. goto error_reset;
  1122. }
  1123. usleep_range(20, 22); /* T4 */
  1124. return 0;
  1125. error_reset:
  1126. gpiod_set_value_cansleep(imx335->reset_gpio, 1);
  1127. regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies);
  1128. return ret;
  1129. }
  1130. /**
  1131. * imx335_power_off() - Sensor power off sequence
  1132. * @dev: pointer to i2c device
  1133. *
  1134. * Return: 0 if successful, error code otherwise.
  1135. */
  1136. static int imx335_power_off(struct device *dev)
  1137. {
  1138. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1139. struct imx335 *imx335 = to_imx335(sd);
  1140. gpiod_set_value_cansleep(imx335->reset_gpio, 1);
  1141. clk_disable_unprepare(imx335->inclk);
  1142. regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies);
  1143. return 0;
  1144. }
  1145. /**
  1146. * imx335_init_controls() - Initialize sensor subdevice controls
  1147. * @imx335: pointer to imx335 device
  1148. *
  1149. * Return: 0 if successful, error code otherwise.
  1150. */
  1151. static int imx335_init_controls(struct imx335 *imx335)
  1152. {
  1153. struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler;
  1154. const struct imx335_mode *mode = imx335->cur_mode;
  1155. struct v4l2_fwnode_device_properties props;
  1156. u32 lpfr, shutter_min;
  1157. int ret;
  1158. ret = v4l2_fwnode_device_parse(imx335->dev, &props);
  1159. if (ret)
  1160. return ret;
  1161. /* v4l2_fwnode_device_properties can add two more controls */
  1162. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
  1163. if (ret)
  1164. return ret;
  1165. /* Initialize exposure and gain */
  1166. lpfr = mode->vblank + mode->height;
  1167. shutter_min = IMX335_SHUTTER_MIN;
  1168. if (mode->scan_mode == IMX335_2_2_BINNING)
  1169. shutter_min = IMX335_SHUTTER_MIN_BINNED;
  1170. imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1171. &imx335_ctrl_ops,
  1172. V4L2_CID_EXPOSURE,
  1173. IMX335_EXPOSURE_MIN,
  1174. lpfr - shutter_min,
  1175. IMX335_EXPOSURE_STEP,
  1176. IMX335_EXPOSURE_DEFAULT);
  1177. /*
  1178. * The sensor has an analog gain and a digital gain, both controlled
  1179. * through a single gain value, expressed in 0.3dB increments. Values
  1180. * from 0.0dB (0) to 30.0dB (100) apply analog gain only, higher values
  1181. * up to 72.0dB (240) add further digital gain. Limit the range to
  1182. * analog gain only, support for digital gain can be added separately
  1183. * if needed.
  1184. */
  1185. imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1186. &imx335_ctrl_ops,
  1187. V4L2_CID_ANALOGUE_GAIN,
  1188. IMX335_AGAIN_MIN,
  1189. IMX335_AGAIN_MAX,
  1190. IMX335_AGAIN_STEP,
  1191. IMX335_AGAIN_DEFAULT);
  1192. v4l2_ctrl_cluster(2, &imx335->exp_ctrl);
  1193. imx335->vflip = v4l2_ctrl_new_std(ctrl_hdlr,
  1194. &imx335_ctrl_ops,
  1195. V4L2_CID_VFLIP,
  1196. 0, 1, 1, 0);
  1197. if (imx335->vflip)
  1198. imx335->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  1199. imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1200. &imx335_ctrl_ops,
  1201. V4L2_CID_VBLANK,
  1202. mode->vblank_min,
  1203. mode->vblank_max,
  1204. 1, mode->vblank);
  1205. v4l2_ctrl_new_std_menu_items(ctrl_hdlr,
  1206. &imx335_ctrl_ops,
  1207. V4L2_CID_TEST_PATTERN,
  1208. ARRAY_SIZE(imx335_tpg_menu) - 1,
  1209. 0, 0, imx335_tpg_menu);
  1210. /* Read only controls */
  1211. imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1212. &imx335_ctrl_ops,
  1213. V4L2_CID_PIXEL_RATE,
  1214. mode->pclk, mode->pclk,
  1215. 1, mode->pclk);
  1216. imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  1217. &imx335_ctrl_ops,
  1218. V4L2_CID_LINK_FREQ,
  1219. __fls(imx335->link_freq_bitmap),
  1220. __ffs(imx335->link_freq_bitmap),
  1221. link_freq);
  1222. if (imx335->link_freq_ctrl)
  1223. imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1224. imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1225. &imx335_ctrl_ops,
  1226. V4L2_CID_HBLANK,
  1227. mode->hblank,
  1228. mode->hblank,
  1229. 1, mode->hblank);
  1230. if (imx335->hblank_ctrl)
  1231. imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1232. v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx335_ctrl_ops, &props);
  1233. if (ctrl_hdlr->error) {
  1234. dev_err(imx335->dev, "control init failed: %d\n",
  1235. ctrl_hdlr->error);
  1236. v4l2_ctrl_handler_free(ctrl_hdlr);
  1237. return ctrl_hdlr->error;
  1238. }
  1239. imx335->sd.ctrl_handler = ctrl_hdlr;
  1240. return 0;
  1241. }
  1242. /**
  1243. * imx335_probe() - I2C client device binding
  1244. * @client: pointer to i2c client device
  1245. *
  1246. * Return: 0 if successful, error code otherwise.
  1247. */
  1248. static int imx335_probe(struct i2c_client *client)
  1249. {
  1250. struct imx335 *imx335;
  1251. int ret;
  1252. imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL);
  1253. if (!imx335)
  1254. return -ENOMEM;
  1255. imx335->dev = &client->dev;
  1256. imx335->cci = devm_cci_regmap_init_i2c(client, 16);
  1257. if (IS_ERR(imx335->cci)) {
  1258. dev_err(imx335->dev, "Unable to initialize I2C\n");
  1259. return -ENODEV;
  1260. }
  1261. /* Initialize subdev */
  1262. v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops);
  1263. imx335->sd.internal_ops = &imx335_internal_ops;
  1264. ret = imx335_parse_hw_config(imx335);
  1265. if (ret) {
  1266. dev_err(imx335->dev, "HW configuration is not supported\n");
  1267. return ret;
  1268. }
  1269. ret = imx335_power_on(imx335->dev);
  1270. if (ret) {
  1271. dev_err(imx335->dev, "failed to power-on the sensor\n");
  1272. return ret;
  1273. }
  1274. /* Check module identity */
  1275. ret = imx335_detect(imx335);
  1276. if (ret) {
  1277. dev_err(imx335->dev, "failed to find sensor: %d\n", ret);
  1278. goto error_power_off;
  1279. }
  1280. /* Set default mode to max resolution */
  1281. imx335->cur_mode = &supported_modes[0];
  1282. imx335->cur_mbus_code = imx335_mbus_codes[0];
  1283. imx335->vblank = imx335->cur_mode->vblank;
  1284. ret = imx335_init_controls(imx335);
  1285. if (ret) {
  1286. dev_err(imx335->dev, "failed to init controls: %d\n", ret);
  1287. goto error_power_off;
  1288. }
  1289. /* Initialize subdev */
  1290. imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1291. imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1292. /* Initialize source pad */
  1293. imx335->pad.flags = MEDIA_PAD_FL_SOURCE;
  1294. ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad);
  1295. if (ret) {
  1296. dev_err(imx335->dev, "failed to init entity pads: %d\n", ret);
  1297. goto error_handler_free;
  1298. }
  1299. imx335->sd.state_lock = imx335->ctrl_handler.lock;
  1300. ret = v4l2_subdev_init_finalize(&imx335->sd);
  1301. if (ret < 0) {
  1302. dev_err(imx335->dev, "subdev init error\n");
  1303. goto error_media_entity;
  1304. }
  1305. ret = v4l2_async_register_subdev_sensor(&imx335->sd);
  1306. if (ret < 0) {
  1307. dev_err(imx335->dev,
  1308. "failed to register async subdev: %d\n", ret);
  1309. goto error_subdev_cleanup;
  1310. }
  1311. pm_runtime_set_active(imx335->dev);
  1312. pm_runtime_enable(imx335->dev);
  1313. pm_runtime_idle(imx335->dev);
  1314. return 0;
  1315. error_subdev_cleanup:
  1316. v4l2_subdev_cleanup(&imx335->sd);
  1317. error_media_entity:
  1318. media_entity_cleanup(&imx335->sd.entity);
  1319. error_handler_free:
  1320. v4l2_ctrl_handler_free(imx335->sd.ctrl_handler);
  1321. error_power_off:
  1322. imx335_power_off(imx335->dev);
  1323. return ret;
  1324. }
  1325. /**
  1326. * imx335_remove() - I2C client device unbinding
  1327. * @client: pointer to I2C client device
  1328. *
  1329. * Return: 0 if successful, error code otherwise.
  1330. */
  1331. static void imx335_remove(struct i2c_client *client)
  1332. {
  1333. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1334. v4l2_async_unregister_subdev(sd);
  1335. v4l2_subdev_cleanup(sd);
  1336. media_entity_cleanup(&sd->entity);
  1337. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1338. pm_runtime_disable(&client->dev);
  1339. if (!pm_runtime_status_suspended(&client->dev))
  1340. imx335_power_off(&client->dev);
  1341. pm_runtime_set_suspended(&client->dev);
  1342. }
  1343. static const struct dev_pm_ops imx335_pm_ops = {
  1344. SET_RUNTIME_PM_OPS(imx335_power_off, imx335_power_on, NULL)
  1345. };
  1346. static const struct of_device_id imx335_of_match[] = {
  1347. { .compatible = "sony,imx335" },
  1348. { }
  1349. };
  1350. MODULE_DEVICE_TABLE(of, imx335_of_match);
  1351. static struct i2c_driver imx335_driver = {
  1352. .probe = imx335_probe,
  1353. .remove = imx335_remove,
  1354. .driver = {
  1355. .name = "imx335",
  1356. .pm = &imx335_pm_ops,
  1357. .of_match_table = imx335_of_match,
  1358. },
  1359. };
  1360. module_i2c_driver(imx335_driver);
  1361. MODULE_DESCRIPTION("Sony imx335 sensor driver");
  1362. MODULE_LICENSE("GPL");