imx219.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A V4L2 driver for Sony IMX219 cameras.
  4. * Copyright (C) 2019, Raspberry Pi (Trading) Ltd
  5. *
  6. * Based on Sony imx258 camera driver
  7. * Copyright (C) 2018 Intel Corporation
  8. *
  9. * DT / fwnode changes, and regulator / GPIO control taken from imx214 driver
  10. * Copyright 2018 Qtechnology A/S
  11. *
  12. * Flip handling taken from the Sony IMX319 driver.
  13. * Copyright (C) 2018 Intel Corporation
  14. *
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/minmax.h>
  21. #include <linux/module.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <media/v4l2-cci.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include <media/v4l2-mediabus.h>
  29. /* Chip ID */
  30. #define IMX219_REG_CHIP_ID CCI_REG16(0x0000)
  31. #define IMX219_CHIP_ID 0x0219
  32. #define IMX219_REG_MODE_SELECT CCI_REG8(0x0100)
  33. #define IMX219_MODE_STANDBY 0x00
  34. #define IMX219_MODE_STREAMING 0x01
  35. #define IMX219_REG_CSI_LANE_MODE CCI_REG8(0x0114)
  36. #define IMX219_CSI_2_LANE_MODE 0x01
  37. #define IMX219_CSI_4_LANE_MODE 0x03
  38. #define IMX219_REG_DPHY_CTRL CCI_REG8(0x0128)
  39. #define IMX219_DPHY_CTRL_TIMING_AUTO 0
  40. #define IMX219_DPHY_CTRL_TIMING_MANUAL 1
  41. #define IMX219_REG_EXCK_FREQ CCI_REG16(0x012a)
  42. #define IMX219_EXCK_FREQ(n) ((n) * 256) /* n expressed in MHz */
  43. /* Analog gain control */
  44. #define IMX219_REG_ANALOG_GAIN CCI_REG8(0x0157)
  45. #define IMX219_ANA_GAIN_MIN 0
  46. #define IMX219_ANA_GAIN_MAX 232
  47. #define IMX219_ANA_GAIN_STEP 1
  48. #define IMX219_ANA_GAIN_DEFAULT 0x0
  49. /* Digital gain control */
  50. #define IMX219_REG_DIGITAL_GAIN CCI_REG16(0x0158)
  51. #define IMX219_DGTL_GAIN_MIN 0x0100
  52. #define IMX219_DGTL_GAIN_MAX 0x0fff
  53. #define IMX219_DGTL_GAIN_DEFAULT 0x0100
  54. #define IMX219_DGTL_GAIN_STEP 1
  55. /* Exposure control */
  56. #define IMX219_REG_EXPOSURE CCI_REG16(0x015a)
  57. #define IMX219_EXPOSURE_MIN 4
  58. #define IMX219_EXPOSURE_STEP 1
  59. #define IMX219_EXPOSURE_DEFAULT 0x640
  60. #define IMX219_EXPOSURE_MAX 65535
  61. #define IMX219_EXPOSURE_OFFSET 4
  62. /* V_TIMING internal */
  63. #define IMX219_REG_FRM_LENGTH_A CCI_REG16(0x0160)
  64. #define IMX219_FLL_MAX 0xffff
  65. #define IMX219_VBLANK_MIN 32
  66. #define IMX219_REG_LINE_LENGTH_A CCI_REG16(0x0162)
  67. #define IMX219_LLP_MIN 0x0d78
  68. #define IMX219_BINNED_LLP_MIN 0x0de8
  69. #define IMX219_LLP_MAX 0x7ff0
  70. #define IMX219_REG_X_ADD_STA_A CCI_REG16(0x0164)
  71. #define IMX219_REG_X_ADD_END_A CCI_REG16(0x0166)
  72. #define IMX219_REG_Y_ADD_STA_A CCI_REG16(0x0168)
  73. #define IMX219_REG_Y_ADD_END_A CCI_REG16(0x016a)
  74. #define IMX219_REG_X_OUTPUT_SIZE CCI_REG16(0x016c)
  75. #define IMX219_REG_Y_OUTPUT_SIZE CCI_REG16(0x016e)
  76. #define IMX219_REG_X_ODD_INC_A CCI_REG8(0x0170)
  77. #define IMX219_REG_Y_ODD_INC_A CCI_REG8(0x0171)
  78. #define IMX219_REG_ORIENTATION CCI_REG8(0x0172)
  79. /* Binning Mode */
  80. #define IMX219_REG_BINNING_MODE_H CCI_REG8(0x0174)
  81. #define IMX219_REG_BINNING_MODE_V CCI_REG8(0x0175)
  82. #define IMX219_BINNING_NONE 0x00
  83. #define IMX219_BINNING_X2 0x01
  84. #define IMX219_BINNING_X2_ANALOG 0x03
  85. #define IMX219_REG_CSI_DATA_FORMAT_A CCI_REG16(0x018c)
  86. /* PLL Settings */
  87. #define IMX219_REG_VTPXCK_DIV CCI_REG8(0x0301)
  88. #define IMX219_REG_VTSYCK_DIV CCI_REG8(0x0303)
  89. #define IMX219_REG_PREPLLCK_VT_DIV CCI_REG8(0x0304)
  90. #define IMX219_REG_PREPLLCK_OP_DIV CCI_REG8(0x0305)
  91. #define IMX219_REG_PLL_VT_MPY CCI_REG16(0x0306)
  92. #define IMX219_REG_OPPXCK_DIV CCI_REG8(0x0309)
  93. #define IMX219_REG_OPSYCK_DIV CCI_REG8(0x030b)
  94. #define IMX219_REG_PLL_OP_MPY CCI_REG16(0x030c)
  95. /* Test Pattern Control */
  96. #define IMX219_REG_TEST_PATTERN CCI_REG16(0x0600)
  97. #define IMX219_TEST_PATTERN_DISABLE 0
  98. #define IMX219_TEST_PATTERN_SOLID_COLOR 1
  99. #define IMX219_TEST_PATTERN_COLOR_BARS 2
  100. #define IMX219_TEST_PATTERN_GREY_COLOR 3
  101. #define IMX219_TEST_PATTERN_PN9 4
  102. /* Test pattern colour components */
  103. #define IMX219_REG_TESTP_RED CCI_REG16(0x0602)
  104. #define IMX219_REG_TESTP_GREENR CCI_REG16(0x0604)
  105. #define IMX219_REG_TESTP_BLUE CCI_REG16(0x0606)
  106. #define IMX219_REG_TESTP_GREENB CCI_REG16(0x0608)
  107. #define IMX219_TESTP_COLOUR_MIN 0
  108. #define IMX219_TESTP_COLOUR_MAX 0x03ff
  109. #define IMX219_TESTP_COLOUR_STEP 1
  110. #define IMX219_REG_TP_WINDOW_WIDTH CCI_REG16(0x0624)
  111. #define IMX219_REG_TP_WINDOW_HEIGHT CCI_REG16(0x0626)
  112. /* External clock frequency is 24.0M */
  113. #define IMX219_XCLK_FREQ 24000000
  114. /* Pixel rate is fixed for all the modes */
  115. #define IMX219_PIXEL_RATE 182400000
  116. #define IMX219_PIXEL_RATE_4LANE 281600000
  117. #define IMX219_DEFAULT_LINK_FREQ 456000000
  118. #define IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED 363000000
  119. #define IMX219_DEFAULT_LINK_FREQ_4LANE 364000000
  120. /* IMX219 native and active pixel array size. */
  121. #define IMX219_NATIVE_WIDTH 3296U
  122. #define IMX219_NATIVE_HEIGHT 2480U
  123. #define IMX219_PIXEL_ARRAY_LEFT 8U
  124. #define IMX219_PIXEL_ARRAY_TOP 8U
  125. #define IMX219_PIXEL_ARRAY_WIDTH 3280U
  126. #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
  127. /* Mode : resolution and related config&values */
  128. struct imx219_mode {
  129. /* Frame width */
  130. unsigned int width;
  131. /* Frame height */
  132. unsigned int height;
  133. /* V-timing */
  134. unsigned int fll_def;
  135. };
  136. static const struct cci_reg_sequence imx219_common_regs[] = {
  137. { IMX219_REG_MODE_SELECT, 0x00 }, /* Mode Select */
  138. /* To Access Addresses 3000-5fff, send the following commands */
  139. { CCI_REG8(0x30eb), 0x05 },
  140. { CCI_REG8(0x30eb), 0x0c },
  141. { CCI_REG8(0x300a), 0xff },
  142. { CCI_REG8(0x300b), 0xff },
  143. { CCI_REG8(0x30eb), 0x05 },
  144. { CCI_REG8(0x30eb), 0x09 },
  145. /* Undocumented registers */
  146. { CCI_REG8(0x455e), 0x00 },
  147. { CCI_REG8(0x471e), 0x4b },
  148. { CCI_REG8(0x4767), 0x0f },
  149. { CCI_REG8(0x4750), 0x14 },
  150. { CCI_REG8(0x4540), 0x00 },
  151. { CCI_REG8(0x47b4), 0x14 },
  152. { CCI_REG8(0x4713), 0x30 },
  153. { CCI_REG8(0x478b), 0x10 },
  154. { CCI_REG8(0x478f), 0x10 },
  155. { CCI_REG8(0x4793), 0x10 },
  156. { CCI_REG8(0x4797), 0x0e },
  157. { CCI_REG8(0x479b), 0x0e },
  158. /* Frame Bank Register Group "A" */
  159. { IMX219_REG_X_ODD_INC_A, 1 },
  160. { IMX219_REG_Y_ODD_INC_A, 1 },
  161. /* Output setup registers */
  162. { IMX219_REG_DPHY_CTRL, IMX219_DPHY_CTRL_TIMING_AUTO },
  163. { IMX219_REG_EXCK_FREQ, IMX219_EXCK_FREQ(IMX219_XCLK_FREQ / 1000000) },
  164. };
  165. static const struct cci_reg_sequence imx219_2lane_regs[] = {
  166. /* PLL Clock Table */
  167. { IMX219_REG_VTPXCK_DIV, 5 },
  168. { IMX219_REG_VTSYCK_DIV, 1 },
  169. { IMX219_REG_PREPLLCK_VT_DIV, 3 }, /* 0x03 = AUTO set */
  170. { IMX219_REG_PREPLLCK_OP_DIV, 3 }, /* 0x03 = AUTO set */
  171. { IMX219_REG_PLL_VT_MPY, 57 },
  172. { IMX219_REG_OPSYCK_DIV, 1 },
  173. { IMX219_REG_PLL_OP_MPY, 114 },
  174. /* 2-Lane CSI Mode */
  175. { IMX219_REG_CSI_LANE_MODE, IMX219_CSI_2_LANE_MODE },
  176. };
  177. static const struct cci_reg_sequence imx219_4lane_regs[] = {
  178. /* PLL Clock Table */
  179. { IMX219_REG_VTPXCK_DIV, 5 },
  180. { IMX219_REG_VTSYCK_DIV, 1 },
  181. { IMX219_REG_PREPLLCK_VT_DIV, 3 }, /* 0x03 = AUTO set */
  182. { IMX219_REG_PREPLLCK_OP_DIV, 3 }, /* 0x03 = AUTO set */
  183. { IMX219_REG_PLL_VT_MPY, 88 },
  184. { IMX219_REG_OPSYCK_DIV, 1 },
  185. { IMX219_REG_PLL_OP_MPY, 91 },
  186. /* 4-Lane CSI Mode */
  187. { IMX219_REG_CSI_LANE_MODE, IMX219_CSI_4_LANE_MODE },
  188. };
  189. static const s64 imx219_link_freq_menu[] = {
  190. IMX219_DEFAULT_LINK_FREQ,
  191. };
  192. static const s64 imx219_link_freq_4lane_menu[] = {
  193. IMX219_DEFAULT_LINK_FREQ_4LANE,
  194. /*
  195. * This will never be advertised to userspace, but will be used for
  196. * v4l2_link_freq_to_bitmap
  197. */
  198. IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED,
  199. };
  200. static const char * const imx219_test_pattern_menu[] = {
  201. "Disabled",
  202. "Color Bars",
  203. "Solid Color",
  204. "Grey Color Bars",
  205. "PN9"
  206. };
  207. static const int imx219_test_pattern_val[] = {
  208. IMX219_TEST_PATTERN_DISABLE,
  209. IMX219_TEST_PATTERN_COLOR_BARS,
  210. IMX219_TEST_PATTERN_SOLID_COLOR,
  211. IMX219_TEST_PATTERN_GREY_COLOR,
  212. IMX219_TEST_PATTERN_PN9,
  213. };
  214. /* regulator supplies */
  215. static const char * const imx219_supply_name[] = {
  216. /* Supplies can be enabled in any order */
  217. "VANA", /* Analog (2.8V) supply */
  218. "VDIG", /* Digital Core (1.8V) supply */
  219. "VDDL", /* IF (1.2V) supply */
  220. };
  221. #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
  222. /*
  223. * The supported formats.
  224. * This table MUST contain 4 entries per format, to cover the various flip
  225. * combinations in the order
  226. * - no flip
  227. * - h flip
  228. * - v flip
  229. * - h&v flips
  230. */
  231. static const u32 imx219_mbus_formats[] = {
  232. MEDIA_BUS_FMT_SRGGB10_1X10,
  233. MEDIA_BUS_FMT_SGRBG10_1X10,
  234. MEDIA_BUS_FMT_SGBRG10_1X10,
  235. MEDIA_BUS_FMT_SBGGR10_1X10,
  236. MEDIA_BUS_FMT_SRGGB8_1X8,
  237. MEDIA_BUS_FMT_SGRBG8_1X8,
  238. MEDIA_BUS_FMT_SGBRG8_1X8,
  239. MEDIA_BUS_FMT_SBGGR8_1X8,
  240. };
  241. /*
  242. * Initialisation delay between XCLR low->high and the moment when the sensor
  243. * can start capture (i.e. can leave software stanby) must be not less than:
  244. * t4 + max(t5, t6 + <time to initialize the sensor register over I2C>)
  245. * where
  246. * t4 is fixed, and is max 200uS,
  247. * t5 is fixed, and is 6000uS,
  248. * t6 depends on the sensor external clock, and is max 32000 clock periods.
  249. * As per sensor datasheet, the external clock must be from 6MHz to 27MHz.
  250. * So for any acceptable external clock t6 is always within the range of
  251. * 1185 to 5333 uS, and is always less than t5.
  252. * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then
  253. * initialize the sensor over I2C, and then exit the software standby.
  254. *
  255. * This start-up time can be optimized a bit more, if we start the writes
  256. * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor
  257. * initialization over I2C may complete before (t4+t5) expires, and we must
  258. * ensure that capture is not started before (t4+t5).
  259. *
  260. * This delay doesn't account for the power supply startup time. If needed,
  261. * this should be taken care of via the regulator framework. E.g. in the
  262. * case of DT for regulator-fixed one should define the startup-delay-us
  263. * property.
  264. */
  265. #define IMX219_XCLR_MIN_DELAY_US 6200
  266. #define IMX219_XCLR_DELAY_RANGE_US 1000
  267. /* Mode configs */
  268. static const struct imx219_mode supported_modes[] = {
  269. {
  270. /* 8MPix 15fps mode */
  271. .width = 3280,
  272. .height = 2464,
  273. .fll_def = 3526,
  274. },
  275. {
  276. /* 1080P 30fps cropped */
  277. .width = 1920,
  278. .height = 1080,
  279. .fll_def = 1763,
  280. },
  281. {
  282. /* 2x2 binned 60fps mode */
  283. .width = 1640,
  284. .height = 1232,
  285. .fll_def = 1707,
  286. },
  287. {
  288. /* 640x480 60fps mode */
  289. .width = 640,
  290. .height = 480,
  291. .fll_def = 1707,
  292. },
  293. };
  294. struct imx219 {
  295. struct v4l2_subdev sd;
  296. struct media_pad pad;
  297. struct regmap *regmap;
  298. struct clk *xclk; /* system clock to IMX219 */
  299. u32 xclk_freq;
  300. struct gpio_desc *reset_gpio;
  301. struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES];
  302. struct v4l2_ctrl_handler ctrl_handler;
  303. /* V4L2 Controls */
  304. struct v4l2_ctrl *pixel_rate;
  305. struct v4l2_ctrl *link_freq;
  306. struct v4l2_ctrl *exposure;
  307. struct v4l2_ctrl *vflip;
  308. struct v4l2_ctrl *hflip;
  309. struct v4l2_ctrl *vblank;
  310. struct v4l2_ctrl *hblank;
  311. /* Two or Four lanes */
  312. u8 lanes;
  313. };
  314. static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd)
  315. {
  316. return container_of(_sd, struct imx219, sd);
  317. }
  318. /* Get bayer order based on flip setting. */
  319. static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
  320. {
  321. unsigned int i;
  322. for (i = 0; i < ARRAY_SIZE(imx219_mbus_formats); i++)
  323. if (imx219_mbus_formats[i] == code)
  324. break;
  325. if (i >= ARRAY_SIZE(imx219_mbus_formats))
  326. i = 0;
  327. i = (i & ~3) | (imx219->vflip->val ? 2 : 0) |
  328. (imx219->hflip->val ? 1 : 0);
  329. return imx219_mbus_formats[i];
  330. }
  331. static u32 imx219_get_format_bpp(const struct v4l2_mbus_framefmt *format)
  332. {
  333. switch (format->code) {
  334. case MEDIA_BUS_FMT_SRGGB8_1X8:
  335. case MEDIA_BUS_FMT_SGRBG8_1X8:
  336. case MEDIA_BUS_FMT_SGBRG8_1X8:
  337. case MEDIA_BUS_FMT_SBGGR8_1X8:
  338. return 8;
  339. case MEDIA_BUS_FMT_SRGGB10_1X10:
  340. case MEDIA_BUS_FMT_SGRBG10_1X10:
  341. case MEDIA_BUS_FMT_SGBRG10_1X10:
  342. case MEDIA_BUS_FMT_SBGGR10_1X10:
  343. default:
  344. return 10;
  345. }
  346. }
  347. static void imx219_get_binning(struct v4l2_subdev_state *state, u8 *bin_h,
  348. u8 *bin_v)
  349. {
  350. const struct v4l2_mbus_framefmt *format =
  351. v4l2_subdev_state_get_format(state, 0);
  352. const struct v4l2_rect *crop = v4l2_subdev_state_get_crop(state, 0);
  353. u32 hbin = crop->width / format->width;
  354. u32 vbin = crop->height / format->height;
  355. if (hbin == 2 && vbin == 2) {
  356. *bin_h = IMX219_BINNING_X2_ANALOG;
  357. *bin_v = IMX219_BINNING_X2_ANALOG;
  358. } else {
  359. *bin_h = IMX219_BINNING_NONE;
  360. *bin_v = IMX219_BINNING_NONE;
  361. }
  362. }
  363. static inline u32 imx219_get_rate_factor(struct v4l2_subdev_state *state)
  364. {
  365. u8 bin_h, bin_v;
  366. imx219_get_binning(state, &bin_h, &bin_v);
  367. return (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ? 2 : 1;
  368. }
  369. /* -----------------------------------------------------------------------------
  370. * Controls
  371. */
  372. static int imx219_set_ctrl(struct v4l2_ctrl *ctrl)
  373. {
  374. struct imx219 *imx219 =
  375. container_of(ctrl->handler, struct imx219, ctrl_handler);
  376. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  377. const struct v4l2_mbus_framefmt *format;
  378. struct v4l2_subdev_state *state;
  379. u32 rate_factor;
  380. int ret = 0;
  381. state = v4l2_subdev_get_locked_active_state(&imx219->sd);
  382. format = v4l2_subdev_state_get_format(state, 0);
  383. rate_factor = imx219_get_rate_factor(state);
  384. if (ctrl->id == V4L2_CID_VBLANK) {
  385. int exposure_max, exposure_def;
  386. /* Update max exposure while meeting expected vblanking */
  387. exposure_max = format->height + ctrl->val - IMX219_EXPOSURE_OFFSET;
  388. exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
  389. exposure_max : IMX219_EXPOSURE_DEFAULT;
  390. ret = __v4l2_ctrl_modify_range(imx219->exposure,
  391. imx219->exposure->minimum,
  392. exposure_max,
  393. imx219->exposure->step,
  394. exposure_def);
  395. if (ret)
  396. return ret;
  397. }
  398. /*
  399. * Applying V4L2 control value only happens
  400. * when power is up for streaming
  401. */
  402. if (pm_runtime_get_if_in_use(&client->dev) == 0)
  403. return 0;
  404. switch (ctrl->id) {
  405. case V4L2_CID_ANALOGUE_GAIN:
  406. cci_write(imx219->regmap, IMX219_REG_ANALOG_GAIN,
  407. ctrl->val, &ret);
  408. break;
  409. case V4L2_CID_EXPOSURE:
  410. cci_write(imx219->regmap, IMX219_REG_EXPOSURE,
  411. ctrl->val / rate_factor, &ret);
  412. break;
  413. case V4L2_CID_DIGITAL_GAIN:
  414. cci_write(imx219->regmap, IMX219_REG_DIGITAL_GAIN,
  415. ctrl->val, &ret);
  416. break;
  417. case V4L2_CID_TEST_PATTERN:
  418. cci_write(imx219->regmap, IMX219_REG_TEST_PATTERN,
  419. imx219_test_pattern_val[ctrl->val], &ret);
  420. break;
  421. case V4L2_CID_HFLIP:
  422. case V4L2_CID_VFLIP:
  423. cci_write(imx219->regmap, IMX219_REG_ORIENTATION,
  424. imx219->hflip->val | imx219->vflip->val << 1, &ret);
  425. break;
  426. case V4L2_CID_VBLANK:
  427. cci_write(imx219->regmap, IMX219_REG_FRM_LENGTH_A,
  428. (format->height + ctrl->val) / rate_factor, &ret);
  429. break;
  430. case V4L2_CID_HBLANK:
  431. cci_write(imx219->regmap, IMX219_REG_LINE_LENGTH_A,
  432. format->width + ctrl->val, &ret);
  433. break;
  434. case V4L2_CID_TEST_PATTERN_RED:
  435. cci_write(imx219->regmap, IMX219_REG_TESTP_RED,
  436. ctrl->val, &ret);
  437. break;
  438. case V4L2_CID_TEST_PATTERN_GREENR:
  439. cci_write(imx219->regmap, IMX219_REG_TESTP_GREENR,
  440. ctrl->val, &ret);
  441. break;
  442. case V4L2_CID_TEST_PATTERN_BLUE:
  443. cci_write(imx219->regmap, IMX219_REG_TESTP_BLUE,
  444. ctrl->val, &ret);
  445. break;
  446. case V4L2_CID_TEST_PATTERN_GREENB:
  447. cci_write(imx219->regmap, IMX219_REG_TESTP_GREENB,
  448. ctrl->val, &ret);
  449. break;
  450. default:
  451. dev_info(&client->dev,
  452. "ctrl(id:0x%x,val:0x%x) is not handled\n",
  453. ctrl->id, ctrl->val);
  454. ret = -EINVAL;
  455. break;
  456. }
  457. pm_runtime_put(&client->dev);
  458. return ret;
  459. }
  460. static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
  461. .s_ctrl = imx219_set_ctrl,
  462. };
  463. static unsigned long imx219_get_pixel_rate(struct imx219 *imx219)
  464. {
  465. return (imx219->lanes == 2) ? IMX219_PIXEL_RATE : IMX219_PIXEL_RATE_4LANE;
  466. }
  467. /* Initialize control handlers */
  468. static int imx219_init_controls(struct imx219 *imx219)
  469. {
  470. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  471. const struct imx219_mode *mode = &supported_modes[0];
  472. struct v4l2_ctrl_handler *ctrl_hdlr;
  473. struct v4l2_fwnode_device_properties props;
  474. int exposure_max, exposure_def;
  475. int i, ret;
  476. ctrl_hdlr = &imx219->ctrl_handler;
  477. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
  478. if (ret)
  479. return ret;
  480. /* By default, PIXEL_RATE is read only */
  481. imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  482. V4L2_CID_PIXEL_RATE,
  483. imx219_get_pixel_rate(imx219),
  484. imx219_get_pixel_rate(imx219), 1,
  485. imx219_get_pixel_rate(imx219));
  486. imx219->link_freq =
  487. v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx219_ctrl_ops,
  488. V4L2_CID_LINK_FREQ,
  489. ARRAY_SIZE(imx219_link_freq_menu) - 1, 0,
  490. (imx219->lanes == 2) ? imx219_link_freq_menu :
  491. imx219_link_freq_4lane_menu);
  492. if (imx219->link_freq)
  493. imx219->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  494. /* Initial blanking and exposure. Limits are updated during set_fmt */
  495. imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  496. V4L2_CID_VBLANK, IMX219_VBLANK_MIN,
  497. IMX219_FLL_MAX - mode->height, 1,
  498. mode->fll_def - mode->height);
  499. imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  500. V4L2_CID_HBLANK,
  501. IMX219_LLP_MIN - mode->width,
  502. IMX219_LLP_MAX - mode->width, 1,
  503. IMX219_LLP_MIN - mode->width);
  504. exposure_max = mode->fll_def - IMX219_EXPOSURE_OFFSET;
  505. exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
  506. exposure_max : IMX219_EXPOSURE_DEFAULT;
  507. imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  508. V4L2_CID_EXPOSURE,
  509. IMX219_EXPOSURE_MIN, exposure_max,
  510. IMX219_EXPOSURE_STEP,
  511. exposure_def);
  512. v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  513. IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX,
  514. IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT);
  515. v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  516. IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX,
  517. IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT);
  518. imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  519. V4L2_CID_HFLIP, 0, 1, 1, 0);
  520. if (imx219->hflip)
  521. imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  522. imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  523. V4L2_CID_VFLIP, 0, 1, 1, 0);
  524. if (imx219->vflip)
  525. imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  526. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops,
  527. V4L2_CID_TEST_PATTERN,
  528. ARRAY_SIZE(imx219_test_pattern_menu) - 1,
  529. 0, 0, imx219_test_pattern_menu);
  530. for (i = 0; i < 4; i++) {
  531. /*
  532. * The assumption is that
  533. * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
  534. * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
  535. * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
  536. */
  537. v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
  538. V4L2_CID_TEST_PATTERN_RED + i,
  539. IMX219_TESTP_COLOUR_MIN,
  540. IMX219_TESTP_COLOUR_MAX,
  541. IMX219_TESTP_COLOUR_STEP,
  542. IMX219_TESTP_COLOUR_MAX);
  543. /* The "Solid color" pattern is white by default */
  544. }
  545. if (ctrl_hdlr->error) {
  546. ret = ctrl_hdlr->error;
  547. dev_err_probe(&client->dev, ret, "Control init failed\n");
  548. goto error;
  549. }
  550. ret = v4l2_fwnode_device_parse(&client->dev, &props);
  551. if (ret)
  552. goto error;
  553. ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx219_ctrl_ops,
  554. &props);
  555. if (ret)
  556. goto error;
  557. imx219->sd.ctrl_handler = ctrl_hdlr;
  558. return 0;
  559. error:
  560. v4l2_ctrl_handler_free(ctrl_hdlr);
  561. return ret;
  562. }
  563. static void imx219_free_controls(struct imx219 *imx219)
  564. {
  565. v4l2_ctrl_handler_free(imx219->sd.ctrl_handler);
  566. }
  567. /* -----------------------------------------------------------------------------
  568. * Subdev operations
  569. */
  570. static int imx219_set_framefmt(struct imx219 *imx219,
  571. struct v4l2_subdev_state *state)
  572. {
  573. const struct v4l2_mbus_framefmt *format;
  574. const struct v4l2_rect *crop;
  575. u8 bin_h, bin_v;
  576. u32 bpp;
  577. int ret = 0;
  578. format = v4l2_subdev_state_get_format(state, 0);
  579. crop = v4l2_subdev_state_get_crop(state, 0);
  580. bpp = imx219_get_format_bpp(format);
  581. cci_write(imx219->regmap, IMX219_REG_X_ADD_STA_A,
  582. crop->left - IMX219_PIXEL_ARRAY_LEFT, &ret);
  583. cci_write(imx219->regmap, IMX219_REG_X_ADD_END_A,
  584. crop->left - IMX219_PIXEL_ARRAY_LEFT + crop->width - 1, &ret);
  585. cci_write(imx219->regmap, IMX219_REG_Y_ADD_STA_A,
  586. crop->top - IMX219_PIXEL_ARRAY_TOP, &ret);
  587. cci_write(imx219->regmap, IMX219_REG_Y_ADD_END_A,
  588. crop->top - IMX219_PIXEL_ARRAY_TOP + crop->height - 1, &ret);
  589. imx219_get_binning(state, &bin_h, &bin_v);
  590. cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_H, bin_h, &ret);
  591. cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_V, bin_v, &ret);
  592. cci_write(imx219->regmap, IMX219_REG_X_OUTPUT_SIZE,
  593. format->width, &ret);
  594. cci_write(imx219->regmap, IMX219_REG_Y_OUTPUT_SIZE,
  595. format->height, &ret);
  596. cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_WIDTH,
  597. format->width, &ret);
  598. cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_HEIGHT,
  599. format->height, &ret);
  600. cci_write(imx219->regmap, IMX219_REG_CSI_DATA_FORMAT_A,
  601. (bpp << 8) | bpp, &ret);
  602. cci_write(imx219->regmap, IMX219_REG_OPPXCK_DIV, bpp, &ret);
  603. return ret;
  604. }
  605. static int imx219_configure_lanes(struct imx219 *imx219)
  606. {
  607. /* Write the appropriate PLL settings for the number of MIPI lanes */
  608. return cci_multi_reg_write(imx219->regmap,
  609. imx219->lanes == 2 ? imx219_2lane_regs : imx219_4lane_regs,
  610. imx219->lanes == 2 ? ARRAY_SIZE(imx219_2lane_regs) :
  611. ARRAY_SIZE(imx219_4lane_regs), NULL);
  612. };
  613. static int imx219_enable_streams(struct v4l2_subdev *sd,
  614. struct v4l2_subdev_state *state, u32 pad,
  615. u64 streams_mask)
  616. {
  617. struct imx219 *imx219 = to_imx219(sd);
  618. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  619. int ret;
  620. ret = pm_runtime_resume_and_get(&client->dev);
  621. if (ret < 0)
  622. return ret;
  623. /* Send all registers that are common to all modes */
  624. ret = cci_multi_reg_write(imx219->regmap, imx219_common_regs,
  625. ARRAY_SIZE(imx219_common_regs), NULL);
  626. if (ret) {
  627. dev_err(&client->dev, "%s failed to send mfg header\n", __func__);
  628. goto err_rpm_put;
  629. }
  630. /* Configure two or four Lane mode */
  631. ret = imx219_configure_lanes(imx219);
  632. if (ret) {
  633. dev_err(&client->dev, "%s failed to configure lanes\n", __func__);
  634. goto err_rpm_put;
  635. }
  636. /* Apply format and crop settings. */
  637. ret = imx219_set_framefmt(imx219, state);
  638. if (ret) {
  639. dev_err(&client->dev, "%s failed to set frame format: %d\n",
  640. __func__, ret);
  641. goto err_rpm_put;
  642. }
  643. /* Apply customized values from user */
  644. ret = __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler);
  645. if (ret)
  646. goto err_rpm_put;
  647. /* set stream on register */
  648. ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
  649. IMX219_MODE_STREAMING, NULL);
  650. if (ret)
  651. goto err_rpm_put;
  652. /* vflip and hflip cannot change during streaming */
  653. __v4l2_ctrl_grab(imx219->vflip, true);
  654. __v4l2_ctrl_grab(imx219->hflip, true);
  655. return 0;
  656. err_rpm_put:
  657. pm_runtime_put_autosuspend(&client->dev);
  658. return ret;
  659. }
  660. static int imx219_disable_streams(struct v4l2_subdev *sd,
  661. struct v4l2_subdev_state *state, u32 pad,
  662. u64 streams_mask)
  663. {
  664. struct imx219 *imx219 = to_imx219(sd);
  665. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  666. int ret;
  667. /* set stream off register */
  668. ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
  669. IMX219_MODE_STANDBY, NULL);
  670. if (ret)
  671. dev_err(&client->dev, "%s failed to set stream\n", __func__);
  672. __v4l2_ctrl_grab(imx219->vflip, false);
  673. __v4l2_ctrl_grab(imx219->hflip, false);
  674. pm_runtime_put_autosuspend(&client->dev);
  675. return ret;
  676. }
  677. static int imx219_enum_mbus_code(struct v4l2_subdev *sd,
  678. struct v4l2_subdev_state *state,
  679. struct v4l2_subdev_mbus_code_enum *code)
  680. {
  681. struct imx219 *imx219 = to_imx219(sd);
  682. if (code->index >= (ARRAY_SIZE(imx219_mbus_formats) / 4))
  683. return -EINVAL;
  684. code->code = imx219_get_format_code(imx219, imx219_mbus_formats[code->index * 4]);
  685. return 0;
  686. }
  687. static int imx219_enum_frame_size(struct v4l2_subdev *sd,
  688. struct v4l2_subdev_state *state,
  689. struct v4l2_subdev_frame_size_enum *fse)
  690. {
  691. struct imx219 *imx219 = to_imx219(sd);
  692. u32 code;
  693. if (fse->index >= ARRAY_SIZE(supported_modes))
  694. return -EINVAL;
  695. code = imx219_get_format_code(imx219, fse->code);
  696. if (fse->code != code)
  697. return -EINVAL;
  698. fse->min_width = supported_modes[fse->index].width;
  699. fse->max_width = fse->min_width;
  700. fse->min_height = supported_modes[fse->index].height;
  701. fse->max_height = fse->min_height;
  702. return 0;
  703. }
  704. static int imx219_set_pad_format(struct v4l2_subdev *sd,
  705. struct v4l2_subdev_state *state,
  706. struct v4l2_subdev_format *fmt)
  707. {
  708. struct imx219 *imx219 = to_imx219(sd);
  709. const struct imx219_mode *mode;
  710. struct v4l2_mbus_framefmt *format;
  711. struct v4l2_rect *crop;
  712. u8 bin_h, bin_v, binning;
  713. u32 prev_line_len;
  714. int ret;
  715. format = v4l2_subdev_state_get_format(state, 0);
  716. prev_line_len = format->width + imx219->hblank->val;
  717. /*
  718. * Adjust the requested format to match the closest mode. The Bayer
  719. * order varies with flips.
  720. */
  721. mode = v4l2_find_nearest_size(supported_modes,
  722. ARRAY_SIZE(supported_modes),
  723. width, height,
  724. fmt->format.width, fmt->format.height);
  725. fmt->format.code = imx219_get_format_code(imx219, fmt->format.code);
  726. fmt->format.width = mode->width;
  727. fmt->format.height = mode->height;
  728. fmt->format.field = V4L2_FIELD_NONE;
  729. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  730. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601;
  731. fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  732. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  733. *format = fmt->format;
  734. /*
  735. * Use binning to maximize the crop rectangle size, and centre it in the
  736. * sensor.
  737. */
  738. bin_h = min(IMX219_PIXEL_ARRAY_WIDTH / format->width, 2U);
  739. bin_v = min(IMX219_PIXEL_ARRAY_HEIGHT / format->height, 2U);
  740. /* Ensure bin_h and bin_v are same to avoid 1:2 or 2:1 stretching */
  741. binning = min(bin_h, bin_v);
  742. crop = v4l2_subdev_state_get_crop(state, 0);
  743. crop->width = format->width * binning;
  744. crop->height = format->height * binning;
  745. crop->left = (IMX219_NATIVE_WIDTH - crop->width) / 2;
  746. crop->top = (IMX219_NATIVE_HEIGHT - crop->height) / 2;
  747. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  748. int exposure_max;
  749. int exposure_def;
  750. int hblank, llp_min;
  751. int pixel_rate;
  752. /* Update limits and set FPS to default */
  753. ret = __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
  754. IMX219_FLL_MAX - mode->height, 1,
  755. mode->fll_def - mode->height);
  756. if (ret)
  757. return ret;
  758. ret = __v4l2_ctrl_s_ctrl(imx219->vblank,
  759. mode->fll_def - mode->height);
  760. if (ret)
  761. return ret;
  762. /* Update max exposure while meeting expected vblanking */
  763. exposure_max = mode->fll_def - IMX219_EXPOSURE_OFFSET;
  764. exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
  765. exposure_max : IMX219_EXPOSURE_DEFAULT;
  766. ret = __v4l2_ctrl_modify_range(imx219->exposure,
  767. imx219->exposure->minimum,
  768. exposure_max,
  769. imx219->exposure->step,
  770. exposure_def);
  771. if (ret)
  772. return ret;
  773. /*
  774. * With analog binning the default minimum line length of 3448
  775. * can cause artefacts with RAW10 formats, because the ADC
  776. * operates on two lines together. So we switch to a higher
  777. * minimum of 3560.
  778. */
  779. imx219_get_binning(state, &bin_h, &bin_v);
  780. llp_min = (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ?
  781. IMX219_BINNED_LLP_MIN : IMX219_LLP_MIN;
  782. ret = __v4l2_ctrl_modify_range(imx219->hblank,
  783. llp_min - mode->width,
  784. IMX219_LLP_MAX - mode->width, 1,
  785. llp_min - mode->width);
  786. if (ret)
  787. return ret;
  788. /*
  789. * Retain PPL setting from previous mode so that the
  790. * line time does not change on a mode change.
  791. * Limits have to be recomputed as the controls define
  792. * the blanking only, so PPL values need to have the
  793. * mode width subtracted.
  794. */
  795. hblank = prev_line_len - mode->width;
  796. ret = __v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
  797. if (ret)
  798. return ret;
  799. /* Scale the pixel rate based on the mode specific factor */
  800. pixel_rate = imx219_get_pixel_rate(imx219) *
  801. imx219_get_rate_factor(state);
  802. ret = __v4l2_ctrl_modify_range(imx219->pixel_rate, pixel_rate,
  803. pixel_rate, 1, pixel_rate);
  804. if (ret)
  805. return ret;
  806. }
  807. return 0;
  808. }
  809. static int imx219_get_selection(struct v4l2_subdev *sd,
  810. struct v4l2_subdev_state *state,
  811. struct v4l2_subdev_selection *sel)
  812. {
  813. switch (sel->target) {
  814. case V4L2_SEL_TGT_CROP:
  815. sel->r = *v4l2_subdev_state_get_crop(state, 0);
  816. return 0;
  817. case V4L2_SEL_TGT_NATIVE_SIZE:
  818. sel->r.top = 0;
  819. sel->r.left = 0;
  820. sel->r.width = IMX219_NATIVE_WIDTH;
  821. sel->r.height = IMX219_NATIVE_HEIGHT;
  822. return 0;
  823. case V4L2_SEL_TGT_CROP_DEFAULT:
  824. case V4L2_SEL_TGT_CROP_BOUNDS:
  825. sel->r.top = IMX219_PIXEL_ARRAY_TOP;
  826. sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
  827. sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
  828. sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
  829. return 0;
  830. }
  831. return -EINVAL;
  832. }
  833. static int imx219_init_state(struct v4l2_subdev *sd,
  834. struct v4l2_subdev_state *state)
  835. {
  836. struct v4l2_subdev_format fmt = {
  837. .which = V4L2_SUBDEV_FORMAT_TRY,
  838. .pad = 0,
  839. .format = {
  840. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  841. .width = supported_modes[0].width,
  842. .height = supported_modes[0].height,
  843. },
  844. };
  845. return imx219_set_pad_format(sd, state, &fmt);
  846. }
  847. static const struct v4l2_subdev_video_ops imx219_video_ops = {
  848. .s_stream = v4l2_subdev_s_stream_helper,
  849. };
  850. static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
  851. .enum_mbus_code = imx219_enum_mbus_code,
  852. .get_fmt = v4l2_subdev_get_fmt,
  853. .set_fmt = imx219_set_pad_format,
  854. .get_selection = imx219_get_selection,
  855. .enum_frame_size = imx219_enum_frame_size,
  856. .enable_streams = imx219_enable_streams,
  857. .disable_streams = imx219_disable_streams,
  858. };
  859. static const struct v4l2_subdev_ops imx219_subdev_ops = {
  860. .video = &imx219_video_ops,
  861. .pad = &imx219_pad_ops,
  862. };
  863. static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
  864. .init_state = imx219_init_state,
  865. };
  866. /* -----------------------------------------------------------------------------
  867. * Power management
  868. */
  869. static int imx219_power_on(struct device *dev)
  870. {
  871. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  872. struct imx219 *imx219 = to_imx219(sd);
  873. int ret;
  874. ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
  875. imx219->supplies);
  876. if (ret) {
  877. dev_err(dev, "%s: failed to enable regulators\n",
  878. __func__);
  879. return ret;
  880. }
  881. ret = clk_prepare_enable(imx219->xclk);
  882. if (ret) {
  883. dev_err(dev, "%s: failed to enable clock\n",
  884. __func__);
  885. goto reg_off;
  886. }
  887. /*
  888. * Note: Misinterpretation of reset assertion - do not re-use this code.
  889. * XCLR pin is using incorrect (for reset signal) logical level.
  890. */
  891. gpiod_set_value_cansleep(imx219->reset_gpio, 1);
  892. usleep_range(IMX219_XCLR_MIN_DELAY_US,
  893. IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US);
  894. return 0;
  895. reg_off:
  896. regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
  897. return ret;
  898. }
  899. static int imx219_power_off(struct device *dev)
  900. {
  901. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  902. struct imx219 *imx219 = to_imx219(sd);
  903. gpiod_set_value_cansleep(imx219->reset_gpio, 0);
  904. regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
  905. clk_disable_unprepare(imx219->xclk);
  906. return 0;
  907. }
  908. /* -----------------------------------------------------------------------------
  909. * Probe & remove
  910. */
  911. static int imx219_get_regulators(struct imx219 *imx219)
  912. {
  913. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  914. unsigned int i;
  915. for (i = 0; i < IMX219_NUM_SUPPLIES; i++)
  916. imx219->supplies[i].supply = imx219_supply_name[i];
  917. return devm_regulator_bulk_get(&client->dev,
  918. IMX219_NUM_SUPPLIES,
  919. imx219->supplies);
  920. }
  921. /* Verify chip ID */
  922. static int imx219_identify_module(struct imx219 *imx219)
  923. {
  924. struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
  925. int ret;
  926. u64 val;
  927. ret = cci_read(imx219->regmap, IMX219_REG_CHIP_ID, &val, NULL);
  928. if (ret)
  929. return dev_err_probe(&client->dev, ret,
  930. "failed to read chip id %x\n",
  931. IMX219_CHIP_ID);
  932. if (val != IMX219_CHIP_ID)
  933. return dev_err_probe(&client->dev, -EIO,
  934. "chip id mismatch: %x!=%llx\n",
  935. IMX219_CHIP_ID, val);
  936. return 0;
  937. }
  938. static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
  939. {
  940. struct fwnode_handle *endpoint;
  941. struct v4l2_fwnode_endpoint ep_cfg = {
  942. .bus_type = V4L2_MBUS_CSI2_DPHY
  943. };
  944. unsigned long link_freq_bitmap;
  945. int ret = -EINVAL;
  946. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  947. if (!endpoint)
  948. return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
  949. if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
  950. dev_err_probe(dev, -EINVAL, "could not parse endpoint\n");
  951. goto error_out;
  952. }
  953. /* Check the number of MIPI CSI2 data lanes */
  954. if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2 &&
  955. ep_cfg.bus.mipi_csi2.num_data_lanes != 4) {
  956. dev_err_probe(dev, -EINVAL,
  957. "only 2 or 4 data lanes are currently supported\n");
  958. goto error_out;
  959. }
  960. imx219->lanes = ep_cfg.bus.mipi_csi2.num_data_lanes;
  961. /* Check the link frequency set in device tree */
  962. switch (imx219->lanes) {
  963. case 2:
  964. ret = v4l2_link_freq_to_bitmap(dev,
  965. ep_cfg.link_frequencies,
  966. ep_cfg.nr_of_link_frequencies,
  967. imx219_link_freq_menu,
  968. ARRAY_SIZE(imx219_link_freq_menu),
  969. &link_freq_bitmap);
  970. break;
  971. case 4:
  972. ret = v4l2_link_freq_to_bitmap(dev,
  973. ep_cfg.link_frequencies,
  974. ep_cfg.nr_of_link_frequencies,
  975. imx219_link_freq_4lane_menu,
  976. ARRAY_SIZE(imx219_link_freq_4lane_menu),
  977. &link_freq_bitmap);
  978. if (!ret && (link_freq_bitmap & BIT(1))) {
  979. dev_warn(dev, "Link frequency of %d not supported, but has been incorrectly advertised previously\n",
  980. IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED);
  981. dev_warn(dev, "Using link frequency of %d\n",
  982. IMX219_DEFAULT_LINK_FREQ_4LANE);
  983. link_freq_bitmap |= BIT(0);
  984. }
  985. break;
  986. }
  987. if (ret || !(link_freq_bitmap & BIT(0))) {
  988. ret = -EINVAL;
  989. dev_err_probe(dev, -EINVAL,
  990. "Link frequency not supported: %lld\n",
  991. ep_cfg.link_frequencies[0]);
  992. }
  993. error_out:
  994. v4l2_fwnode_endpoint_free(&ep_cfg);
  995. fwnode_handle_put(endpoint);
  996. return ret;
  997. }
  998. static int imx219_probe(struct i2c_client *client)
  999. {
  1000. struct device *dev = &client->dev;
  1001. struct imx219 *imx219;
  1002. int ret;
  1003. imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL);
  1004. if (!imx219)
  1005. return -ENOMEM;
  1006. v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops);
  1007. imx219->sd.internal_ops = &imx219_internal_ops;
  1008. /* Check the hardware configuration in device tree */
  1009. if (imx219_check_hwcfg(dev, imx219))
  1010. return -EINVAL;
  1011. imx219->regmap = devm_cci_regmap_init_i2c(client, 16);
  1012. if (IS_ERR(imx219->regmap))
  1013. return dev_err_probe(dev, PTR_ERR(imx219->regmap),
  1014. "failed to initialize CCI\n");
  1015. /* Get system clock (xclk) */
  1016. imx219->xclk = devm_v4l2_sensor_clk_get(dev, NULL);
  1017. if (IS_ERR(imx219->xclk))
  1018. return dev_err_probe(dev, PTR_ERR(imx219->xclk),
  1019. "failed to get xclk\n");
  1020. imx219->xclk_freq = clk_get_rate(imx219->xclk);
  1021. if (imx219->xclk_freq != IMX219_XCLK_FREQ)
  1022. return dev_err_probe(dev, -EINVAL,
  1023. "xclk frequency not supported: %d Hz\n",
  1024. imx219->xclk_freq);
  1025. ret = imx219_get_regulators(imx219);
  1026. if (ret)
  1027. return dev_err_probe(dev, ret, "failed to get regulators\n");
  1028. /* Request optional enable pin */
  1029. imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1030. GPIOD_OUT_HIGH);
  1031. /*
  1032. * The sensor must be powered for imx219_identify_module()
  1033. * to be able to read the CHIP_ID register
  1034. */
  1035. ret = imx219_power_on(dev);
  1036. if (ret)
  1037. return ret;
  1038. ret = imx219_identify_module(imx219);
  1039. if (ret)
  1040. goto error_power_off;
  1041. /*
  1042. * Sensor doesn't enter LP-11 state upon power up until and unless
  1043. * streaming is started, so upon power up switch the modes to:
  1044. * streaming -> standby
  1045. */
  1046. ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
  1047. IMX219_MODE_STREAMING, NULL);
  1048. if (ret < 0)
  1049. goto error_power_off;
  1050. usleep_range(100, 110);
  1051. /* put sensor back to standby mode */
  1052. ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
  1053. IMX219_MODE_STANDBY, NULL);
  1054. if (ret < 0)
  1055. goto error_power_off;
  1056. usleep_range(100, 110);
  1057. ret = imx219_init_controls(imx219);
  1058. if (ret)
  1059. goto error_power_off;
  1060. /* Initialize subdev */
  1061. imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1062. imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1063. /* Initialize source pad */
  1064. imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
  1065. ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
  1066. if (ret) {
  1067. dev_err_probe(dev, ret, "failed to init entity pads\n");
  1068. goto error_handler_free;
  1069. }
  1070. imx219->sd.state_lock = imx219->ctrl_handler.lock;
  1071. ret = v4l2_subdev_init_finalize(&imx219->sd);
  1072. if (ret < 0) {
  1073. dev_err_probe(dev, ret, "subdev init error\n");
  1074. goto error_media_entity;
  1075. }
  1076. pm_runtime_set_active(dev);
  1077. pm_runtime_enable(dev);
  1078. ret = v4l2_async_register_subdev_sensor(&imx219->sd);
  1079. if (ret < 0) {
  1080. dev_err_probe(dev, ret,
  1081. "failed to register sensor sub-device\n");
  1082. goto error_subdev_cleanup;
  1083. }
  1084. pm_runtime_idle(dev);
  1085. pm_runtime_set_autosuspend_delay(dev, 1000);
  1086. pm_runtime_use_autosuspend(dev);
  1087. return 0;
  1088. error_subdev_cleanup:
  1089. v4l2_subdev_cleanup(&imx219->sd);
  1090. pm_runtime_disable(dev);
  1091. pm_runtime_set_suspended(dev);
  1092. error_media_entity:
  1093. media_entity_cleanup(&imx219->sd.entity);
  1094. error_handler_free:
  1095. imx219_free_controls(imx219);
  1096. error_power_off:
  1097. imx219_power_off(dev);
  1098. return ret;
  1099. }
  1100. static void imx219_remove(struct i2c_client *client)
  1101. {
  1102. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1103. struct imx219 *imx219 = to_imx219(sd);
  1104. v4l2_async_unregister_subdev(sd);
  1105. v4l2_subdev_cleanup(sd);
  1106. media_entity_cleanup(&sd->entity);
  1107. imx219_free_controls(imx219);
  1108. pm_runtime_disable(&client->dev);
  1109. if (!pm_runtime_status_suspended(&client->dev)) {
  1110. imx219_power_off(&client->dev);
  1111. pm_runtime_set_suspended(&client->dev);
  1112. }
  1113. }
  1114. static const struct of_device_id imx219_dt_ids[] = {
  1115. { .compatible = "sony,imx219" },
  1116. { /* sentinel */ }
  1117. };
  1118. MODULE_DEVICE_TABLE(of, imx219_dt_ids);
  1119. static const struct dev_pm_ops imx219_pm_ops = {
  1120. SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL)
  1121. };
  1122. static struct i2c_driver imx219_i2c_driver = {
  1123. .driver = {
  1124. .name = "imx219",
  1125. .of_match_table = imx219_dt_ids,
  1126. .pm = &imx219_pm_ops,
  1127. },
  1128. .probe = imx219_probe,
  1129. .remove = imx219_remove,
  1130. };
  1131. module_i2c_driver(imx219_i2c_driver);
  1132. MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
  1133. MODULE_DESCRIPTION("Sony IMX219 sensor driver");
  1134. MODULE_LICENSE("GPL v2");