imx412.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sony imx412 Camera Sensor Driver
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. */
  7. #include <asm/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/regulator/consumer.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include <media/v4l2-fwnode.h>
  16. #include <media/v4l2-subdev.h>
  17. /* Streaming Mode */
  18. #define IMX412_REG_MODE_SELECT 0x0100
  19. #define IMX412_MODE_STANDBY 0x00
  20. #define IMX412_MODE_STREAMING 0x01
  21. /* Lines per frame */
  22. #define IMX412_REG_LPFR 0x0340
  23. /* Chip ID */
  24. #define IMX412_REG_ID 0x0016
  25. #define IMX412_ID 0x577
  26. /* Exposure control */
  27. #define IMX412_REG_EXPOSURE_CIT 0x0202
  28. #define IMX412_EXPOSURE_MIN 8
  29. #define IMX412_EXPOSURE_OFFSET 22
  30. #define IMX412_EXPOSURE_STEP 1
  31. #define IMX412_EXPOSURE_DEFAULT 0x0648
  32. /* Analog gain control */
  33. #define IMX412_REG_AGAIN 0x0204
  34. #define IMX412_AGAIN_MIN 0
  35. #define IMX412_AGAIN_MAX 978
  36. #define IMX412_AGAIN_STEP 1
  37. #define IMX412_AGAIN_DEFAULT 0
  38. /* Group hold register */
  39. #define IMX412_REG_HOLD 0x0104
  40. /* Input clock rate */
  41. #define IMX412_INCLK_RATE 24000000
  42. /* CSI2 HW configuration */
  43. #define IMX412_LINK_FREQ 600000000
  44. #define IMX412_NUM_DATA_LANES 4
  45. #define IMX412_REG_MIN 0x00
  46. #define IMX412_REG_MAX 0xffff
  47. /**
  48. * struct imx412_reg - imx412 sensor register
  49. * @address: Register address
  50. * @val: Register value
  51. */
  52. struct imx412_reg {
  53. u16 address;
  54. u8 val;
  55. };
  56. /**
  57. * struct imx412_reg_list - imx412 sensor register list
  58. * @num_of_regs: Number of registers in the list
  59. * @regs: Pointer to register list
  60. */
  61. struct imx412_reg_list {
  62. u32 num_of_regs;
  63. const struct imx412_reg *regs;
  64. };
  65. /**
  66. * struct imx412_mode - imx412 sensor mode structure
  67. * @width: Frame width
  68. * @height: Frame height
  69. * @code: Format code
  70. * @hblank: Horizontal blanking in lines
  71. * @vblank: Vertical blanking in lines
  72. * @vblank_min: Minimum vertical blanking in lines
  73. * @vblank_max: Maximum vertical blanking in lines
  74. * @pclk: Sensor pixel clock
  75. * @link_freq_idx: Link frequency index
  76. * @reg_list: Register list for sensor mode
  77. */
  78. struct imx412_mode {
  79. u32 width;
  80. u32 height;
  81. u32 code;
  82. u32 hblank;
  83. u32 vblank;
  84. u32 vblank_min;
  85. u32 vblank_max;
  86. u64 pclk;
  87. u32 link_freq_idx;
  88. struct imx412_reg_list reg_list;
  89. };
  90. static const char * const imx412_supply_names[] = {
  91. "dovdd", /* Digital I/O power */
  92. "avdd", /* Analog power */
  93. "dvdd", /* Digital core power */
  94. };
  95. /**
  96. * struct imx412 - imx412 sensor device structure
  97. * @dev: Pointer to generic device
  98. * @client: Pointer to i2c client
  99. * @sd: V4L2 sub-device
  100. * @pad: Media pad. Only one pad supported
  101. * @reset_gpio: Sensor reset gpio
  102. * @inclk: Sensor input clock
  103. * @supplies: Regulator supplies
  104. * @ctrl_handler: V4L2 control handler
  105. * @link_freq_ctrl: Pointer to link frequency control
  106. * @pclk_ctrl: Pointer to pixel clock control
  107. * @hblank_ctrl: Pointer to horizontal blanking control
  108. * @vblank_ctrl: Pointer to vertical blanking control
  109. * @exp_ctrl: Pointer to exposure control
  110. * @again_ctrl: Pointer to analog gain control
  111. * @vblank: Vertical blanking in lines
  112. * @cur_mode: Pointer to current selected sensor mode
  113. * @mutex: Mutex for serializing sensor controls
  114. * @streaming: Flag indicating streaming state
  115. */
  116. struct imx412 {
  117. struct device *dev;
  118. struct i2c_client *client;
  119. struct v4l2_subdev sd;
  120. struct media_pad pad;
  121. struct gpio_desc *reset_gpio;
  122. struct clk *inclk;
  123. struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
  124. struct v4l2_ctrl_handler ctrl_handler;
  125. struct v4l2_ctrl *link_freq_ctrl;
  126. struct v4l2_ctrl *pclk_ctrl;
  127. struct v4l2_ctrl *hblank_ctrl;
  128. struct v4l2_ctrl *vblank_ctrl;
  129. struct {
  130. struct v4l2_ctrl *exp_ctrl;
  131. struct v4l2_ctrl *again_ctrl;
  132. };
  133. u32 vblank;
  134. const struct imx412_mode *cur_mode;
  135. struct mutex mutex;
  136. bool streaming;
  137. };
  138. static const s64 link_freq[] = {
  139. IMX412_LINK_FREQ,
  140. };
  141. /* Sensor mode registers */
  142. static const struct imx412_reg mode_4056x3040_regs[] = {
  143. {0x0136, 0x18},
  144. {0x0137, 0x00},
  145. {0x3c7e, 0x08},
  146. {0x3c7f, 0x02},
  147. {0x38a8, 0x1f},
  148. {0x38a9, 0xff},
  149. {0x38aa, 0x1f},
  150. {0x38ab, 0xff},
  151. {0x55d4, 0x00},
  152. {0x55d5, 0x00},
  153. {0x55d6, 0x07},
  154. {0x55d7, 0xff},
  155. {0x55e8, 0x07},
  156. {0x55e9, 0xff},
  157. {0x55ea, 0x00},
  158. {0x55eb, 0x00},
  159. {0x575c, 0x07},
  160. {0x575d, 0xff},
  161. {0x575e, 0x00},
  162. {0x575f, 0x00},
  163. {0x5764, 0x00},
  164. {0x5765, 0x00},
  165. {0x5766, 0x07},
  166. {0x5767, 0xff},
  167. {0x5974, 0x04},
  168. {0x5975, 0x01},
  169. {0x5f10, 0x09},
  170. {0x5f11, 0x92},
  171. {0x5f12, 0x32},
  172. {0x5f13, 0x72},
  173. {0x5f14, 0x16},
  174. {0x5f15, 0xba},
  175. {0x5f17, 0x13},
  176. {0x5f18, 0x24},
  177. {0x5f19, 0x60},
  178. {0x5f1a, 0xe3},
  179. {0x5f1b, 0xad},
  180. {0x5f1c, 0x74},
  181. {0x5f2d, 0x25},
  182. {0x5f5c, 0xd0},
  183. {0x6a22, 0x00},
  184. {0x6a23, 0x1d},
  185. {0x7ba8, 0x00},
  186. {0x7ba9, 0x00},
  187. {0x886b, 0x00},
  188. {0x9002, 0x0a},
  189. {0x9004, 0x1a},
  190. {0x9214, 0x93},
  191. {0x9215, 0x69},
  192. {0x9216, 0x93},
  193. {0x9217, 0x6b},
  194. {0x9218, 0x93},
  195. {0x9219, 0x6d},
  196. {0x921a, 0x57},
  197. {0x921b, 0x58},
  198. {0x921c, 0x57},
  199. {0x921d, 0x59},
  200. {0x921e, 0x57},
  201. {0x921f, 0x5a},
  202. {0x9220, 0x57},
  203. {0x9221, 0x5b},
  204. {0x9222, 0x93},
  205. {0x9223, 0x02},
  206. {0x9224, 0x93},
  207. {0x9225, 0x03},
  208. {0x9226, 0x93},
  209. {0x9227, 0x04},
  210. {0x9228, 0x93},
  211. {0x9229, 0x05},
  212. {0x922a, 0x98},
  213. {0x922b, 0x21},
  214. {0x922c, 0xb2},
  215. {0x922d, 0xdb},
  216. {0x922e, 0xb2},
  217. {0x922f, 0xdc},
  218. {0x9230, 0xb2},
  219. {0x9231, 0xdd},
  220. {0x9232, 0xe2},
  221. {0x9233, 0xe1},
  222. {0x9234, 0xb2},
  223. {0x9235, 0xe2},
  224. {0x9236, 0xb2},
  225. {0x9237, 0xe3},
  226. {0x9238, 0xb7},
  227. {0x9239, 0xb9},
  228. {0x923a, 0xb7},
  229. {0x923b, 0xbb},
  230. {0x923c, 0xb7},
  231. {0x923d, 0xbc},
  232. {0x923e, 0xb7},
  233. {0x923f, 0xc5},
  234. {0x9240, 0xb7},
  235. {0x9241, 0xc7},
  236. {0x9242, 0xb7},
  237. {0x9243, 0xc9},
  238. {0x9244, 0x98},
  239. {0x9245, 0x56},
  240. {0x9246, 0x98},
  241. {0x9247, 0x55},
  242. {0x9380, 0x00},
  243. {0x9381, 0x62},
  244. {0x9382, 0x00},
  245. {0x9383, 0x56},
  246. {0x9384, 0x00},
  247. {0x9385, 0x52},
  248. {0x9388, 0x00},
  249. {0x9389, 0x55},
  250. {0x938a, 0x00},
  251. {0x938b, 0x55},
  252. {0x938c, 0x00},
  253. {0x938d, 0x41},
  254. {0x5078, 0x01},
  255. {0x0112, 0x0a},
  256. {0x0113, 0x0a},
  257. {0x0114, 0x03},
  258. {0x0342, 0x11},
  259. {0x0343, 0xa0},
  260. {0x0340, 0x0d},
  261. {0x0341, 0xda},
  262. {0x3210, 0x00},
  263. {0x0344, 0x00},
  264. {0x0345, 0x00},
  265. {0x0346, 0x00},
  266. {0x0347, 0x00},
  267. {0x0348, 0x0f},
  268. {0x0349, 0xd7},
  269. {0x034a, 0x0b},
  270. {0x034b, 0xdf},
  271. {0x00e3, 0x00},
  272. {0x00e4, 0x00},
  273. {0x00e5, 0x01},
  274. {0x00fc, 0x0a},
  275. {0x00fd, 0x0a},
  276. {0x00fe, 0x0a},
  277. {0x00ff, 0x0a},
  278. {0xe013, 0x00},
  279. {0x0220, 0x00},
  280. {0x0221, 0x11},
  281. {0x0381, 0x01},
  282. {0x0383, 0x01},
  283. {0x0385, 0x01},
  284. {0x0387, 0x01},
  285. {0x0900, 0x00},
  286. {0x0901, 0x11},
  287. {0x0902, 0x00},
  288. {0x3140, 0x02},
  289. {0x3241, 0x11},
  290. {0x3250, 0x03},
  291. {0x3e10, 0x00},
  292. {0x3e11, 0x00},
  293. {0x3f0d, 0x00},
  294. {0x3f42, 0x00},
  295. {0x3f43, 0x00},
  296. {0x0401, 0x00},
  297. {0x0404, 0x00},
  298. {0x0405, 0x10},
  299. {0x0408, 0x00},
  300. {0x0409, 0x00},
  301. {0x040a, 0x00},
  302. {0x040b, 0x00},
  303. {0x040c, 0x0f},
  304. {0x040d, 0xd8},
  305. {0x040e, 0x0b},
  306. {0x040f, 0xe0},
  307. {0x034c, 0x0f},
  308. {0x034d, 0xd8},
  309. {0x034e, 0x0b},
  310. {0x034f, 0xe0},
  311. {0x0301, 0x05},
  312. {0x0303, 0x02},
  313. {0x0305, 0x04},
  314. {0x0306, 0x00},
  315. {0x0307, 0xc8},
  316. {0x0309, 0x0a},
  317. {0x030b, 0x01},
  318. {0x030d, 0x02},
  319. {0x030e, 0x01},
  320. {0x030f, 0x5e},
  321. {0x0310, 0x00},
  322. {0x0820, 0x12},
  323. {0x0821, 0xc0},
  324. {0x0822, 0x00},
  325. {0x0823, 0x00},
  326. {0x3e20, 0x01},
  327. {0x3e37, 0x00},
  328. {0x3f50, 0x00},
  329. {0x3f56, 0x00},
  330. {0x3f57, 0xe2},
  331. {0x3c0a, 0x5a},
  332. {0x3c0b, 0x55},
  333. {0x3c0c, 0x28},
  334. {0x3c0d, 0x07},
  335. {0x3c0e, 0xff},
  336. {0x3c0f, 0x00},
  337. {0x3c10, 0x00},
  338. {0x3c11, 0x02},
  339. {0x3c12, 0x00},
  340. {0x3c13, 0x03},
  341. {0x3c14, 0x00},
  342. {0x3c15, 0x00},
  343. {0x3c16, 0x0c},
  344. {0x3c17, 0x0c},
  345. {0x3c18, 0x0c},
  346. {0x3c19, 0x0a},
  347. {0x3c1a, 0x0a},
  348. {0x3c1b, 0x0a},
  349. {0x3c1c, 0x00},
  350. {0x3c1d, 0x00},
  351. {0x3c1e, 0x00},
  352. {0x3c1f, 0x00},
  353. {0x3c20, 0x00},
  354. {0x3c21, 0x00},
  355. {0x3c22, 0x3f},
  356. {0x3c23, 0x0a},
  357. {0x3e35, 0x01},
  358. {0x3f4a, 0x03},
  359. {0x3f4b, 0xbf},
  360. {0x3f26, 0x00},
  361. {0x0202, 0x0d},
  362. {0x0203, 0xc4},
  363. {0x0204, 0x00},
  364. {0x0205, 0x00},
  365. {0x020e, 0x01},
  366. {0x020f, 0x00},
  367. {0x0210, 0x01},
  368. {0x0211, 0x00},
  369. {0x0212, 0x01},
  370. {0x0213, 0x00},
  371. {0x0214, 0x01},
  372. {0x0215, 0x00},
  373. {0xbcf1, 0x00},
  374. };
  375. /* Supported sensor mode configurations */
  376. static const struct imx412_mode supported_mode = {
  377. .width = 4056,
  378. .height = 3040,
  379. .hblank = 456,
  380. .vblank = 506,
  381. .vblank_min = 506,
  382. .vblank_max = 32420,
  383. .pclk = 480000000,
  384. .link_freq_idx = 0,
  385. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  386. .reg_list = {
  387. .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
  388. .regs = mode_4056x3040_regs,
  389. },
  390. };
  391. /**
  392. * to_imx412() - imx412 V4L2 sub-device to imx412 device.
  393. * @subdev: pointer to imx412 V4L2 sub-device
  394. *
  395. * Return: pointer to imx412 device
  396. */
  397. static inline struct imx412 *to_imx412(struct v4l2_subdev *subdev)
  398. {
  399. return container_of(subdev, struct imx412, sd);
  400. }
  401. /**
  402. * imx412_read_reg() - Read registers.
  403. * @imx412: pointer to imx412 device
  404. * @reg: register address
  405. * @len: length of bytes to read. Max supported bytes is 4
  406. * @val: pointer to register value to be filled.
  407. *
  408. * Return: 0 if successful, error code otherwise.
  409. */
  410. static int imx412_read_reg(struct imx412 *imx412, u16 reg, u32 len, u32 *val)
  411. {
  412. struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
  413. struct i2c_msg msgs[2] = {0};
  414. u8 addr_buf[2] = {0};
  415. u8 data_buf[4] = {0};
  416. int ret;
  417. if (WARN_ON(len > 4))
  418. return -EINVAL;
  419. put_unaligned_be16(reg, addr_buf);
  420. /* Write register address */
  421. msgs[0].addr = client->addr;
  422. msgs[0].flags = 0;
  423. msgs[0].len = ARRAY_SIZE(addr_buf);
  424. msgs[0].buf = addr_buf;
  425. /* Read data from register */
  426. msgs[1].addr = client->addr;
  427. msgs[1].flags = I2C_M_RD;
  428. msgs[1].len = len;
  429. msgs[1].buf = &data_buf[4 - len];
  430. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  431. if (ret != ARRAY_SIZE(msgs))
  432. return -EIO;
  433. *val = get_unaligned_be32(data_buf);
  434. return 0;
  435. }
  436. /**
  437. * imx412_write_reg() - Write register
  438. * @imx412: pointer to imx412 device
  439. * @reg: register address
  440. * @len: length of bytes. Max supported bytes is 4
  441. * @val: register value
  442. *
  443. * Return: 0 if successful, error code otherwise.
  444. */
  445. static int imx412_write_reg(struct imx412 *imx412, u16 reg, u32 len, u32 val)
  446. {
  447. struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
  448. u8 buf[6] = {0};
  449. if (WARN_ON(len > 4))
  450. return -EINVAL;
  451. put_unaligned_be16(reg, buf);
  452. put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  453. if (i2c_master_send(client, buf, len + 2) != len + 2)
  454. return -EIO;
  455. return 0;
  456. }
  457. /**
  458. * imx412_write_regs() - Write a list of registers
  459. * @imx412: pointer to imx412 device
  460. * @regs: list of registers to be written
  461. * @len: length of registers array
  462. *
  463. * Return: 0 if successful, error code otherwise.
  464. */
  465. static int imx412_write_regs(struct imx412 *imx412,
  466. const struct imx412_reg *regs, u32 len)
  467. {
  468. unsigned int i;
  469. int ret;
  470. for (i = 0; i < len; i++) {
  471. ret = imx412_write_reg(imx412, regs[i].address, 1, regs[i].val);
  472. if (ret)
  473. return ret;
  474. }
  475. return 0;
  476. }
  477. /**
  478. * imx412_update_controls() - Update control ranges based on streaming mode
  479. * @imx412: pointer to imx412 device
  480. * @mode: pointer to imx412_mode sensor mode
  481. *
  482. * Return: 0 if successful, error code otherwise.
  483. */
  484. static int imx412_update_controls(struct imx412 *imx412,
  485. const struct imx412_mode *mode)
  486. {
  487. int ret;
  488. ret = __v4l2_ctrl_s_ctrl(imx412->link_freq_ctrl, mode->link_freq_idx);
  489. if (ret)
  490. return ret;
  491. ret = __v4l2_ctrl_s_ctrl(imx412->hblank_ctrl, mode->hblank);
  492. if (ret)
  493. return ret;
  494. return __v4l2_ctrl_modify_range(imx412->vblank_ctrl, mode->vblank_min,
  495. mode->vblank_max, 1, mode->vblank);
  496. }
  497. /**
  498. * imx412_update_exp_gain() - Set updated exposure and gain
  499. * @imx412: pointer to imx412 device
  500. * @exposure: updated exposure value
  501. * @gain: updated analog gain value
  502. *
  503. * Return: 0 if successful, error code otherwise.
  504. */
  505. static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain)
  506. {
  507. u32 lpfr, shutter;
  508. int ret;
  509. lpfr = imx412->vblank + imx412->cur_mode->height;
  510. shutter = lpfr - exposure;
  511. dev_dbg(imx412->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u",
  512. exposure, gain, shutter, lpfr);
  513. ret = imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 1);
  514. if (ret)
  515. return ret;
  516. ret = imx412_write_reg(imx412, IMX412_REG_LPFR, 2, lpfr);
  517. if (ret)
  518. goto error_release_group_hold;
  519. ret = imx412_write_reg(imx412, IMX412_REG_EXPOSURE_CIT, 2, shutter);
  520. if (ret)
  521. goto error_release_group_hold;
  522. ret = imx412_write_reg(imx412, IMX412_REG_AGAIN, 2, gain);
  523. error_release_group_hold:
  524. imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 0);
  525. return ret;
  526. }
  527. /**
  528. * imx412_set_ctrl() - Set subdevice control
  529. * @ctrl: pointer to v4l2_ctrl structure
  530. *
  531. * Supported controls:
  532. * - V4L2_CID_VBLANK
  533. * - cluster controls:
  534. * - V4L2_CID_ANALOGUE_GAIN
  535. * - V4L2_CID_EXPOSURE
  536. *
  537. * Return: 0 if successful, error code otherwise.
  538. */
  539. static int imx412_set_ctrl(struct v4l2_ctrl *ctrl)
  540. {
  541. struct imx412 *imx412 =
  542. container_of(ctrl->handler, struct imx412, ctrl_handler);
  543. u32 analog_gain;
  544. u32 exposure;
  545. int ret;
  546. switch (ctrl->id) {
  547. case V4L2_CID_VBLANK:
  548. imx412->vblank = imx412->vblank_ctrl->val;
  549. dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u",
  550. imx412->vblank,
  551. imx412->vblank + imx412->cur_mode->height);
  552. ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl,
  553. IMX412_EXPOSURE_MIN,
  554. imx412->vblank +
  555. imx412->cur_mode->height -
  556. IMX412_EXPOSURE_OFFSET,
  557. 1, IMX412_EXPOSURE_DEFAULT);
  558. break;
  559. case V4L2_CID_EXPOSURE:
  560. /* Set controls only if sensor is in power on state */
  561. if (!pm_runtime_get_if_in_use(imx412->dev))
  562. return 0;
  563. exposure = ctrl->val;
  564. analog_gain = imx412->again_ctrl->val;
  565. dev_dbg(imx412->dev, "Received exp %u, analog gain %u",
  566. exposure, analog_gain);
  567. ret = imx412_update_exp_gain(imx412, exposure, analog_gain);
  568. pm_runtime_put(imx412->dev);
  569. break;
  570. default:
  571. dev_err(imx412->dev, "Invalid control %d", ctrl->id);
  572. ret = -EINVAL;
  573. }
  574. return ret;
  575. }
  576. /* V4l2 subdevice control ops*/
  577. static const struct v4l2_ctrl_ops imx412_ctrl_ops = {
  578. .s_ctrl = imx412_set_ctrl,
  579. };
  580. /**
  581. * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  582. * @sd: pointer to imx412 V4L2 sub-device structure
  583. * @sd_state: V4L2 sub-device configuration
  584. * @code: V4L2 sub-device code enumeration need to be filled
  585. *
  586. * Return: 0 if successful, error code otherwise.
  587. */
  588. static int imx412_enum_mbus_code(struct v4l2_subdev *sd,
  589. struct v4l2_subdev_state *sd_state,
  590. struct v4l2_subdev_mbus_code_enum *code)
  591. {
  592. if (code->index > 0)
  593. return -EINVAL;
  594. code->code = supported_mode.code;
  595. return 0;
  596. }
  597. /**
  598. * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  599. * @sd: pointer to imx412 V4L2 sub-device structure
  600. * @sd_state: V4L2 sub-device configuration
  601. * @fsize: V4L2 sub-device size enumeration need to be filled
  602. *
  603. * Return: 0 if successful, error code otherwise.
  604. */
  605. static int imx412_enum_frame_size(struct v4l2_subdev *sd,
  606. struct v4l2_subdev_state *sd_state,
  607. struct v4l2_subdev_frame_size_enum *fsize)
  608. {
  609. if (fsize->index > 0)
  610. return -EINVAL;
  611. if (fsize->code != supported_mode.code)
  612. return -EINVAL;
  613. fsize->min_width = supported_mode.width;
  614. fsize->max_width = fsize->min_width;
  615. fsize->min_height = supported_mode.height;
  616. fsize->max_height = fsize->min_height;
  617. return 0;
  618. }
  619. /**
  620. * imx412_fill_pad_format() - Fill subdevice pad format
  621. * from selected sensor mode
  622. * @imx412: pointer to imx412 device
  623. * @mode: pointer to imx412_mode sensor mode
  624. * @fmt: V4L2 sub-device format need to be filled
  625. */
  626. static void imx412_fill_pad_format(struct imx412 *imx412,
  627. const struct imx412_mode *mode,
  628. struct v4l2_subdev_format *fmt)
  629. {
  630. fmt->format.width = mode->width;
  631. fmt->format.height = mode->height;
  632. fmt->format.code = mode->code;
  633. fmt->format.field = V4L2_FIELD_NONE;
  634. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  635. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  636. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  637. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  638. }
  639. /**
  640. * imx412_get_pad_format() - Get subdevice pad format
  641. * @sd: pointer to imx412 V4L2 sub-device structure
  642. * @sd_state: V4L2 sub-device configuration
  643. * @fmt: V4L2 sub-device format need to be set
  644. *
  645. * Return: 0 if successful, error code otherwise.
  646. */
  647. static int imx412_get_pad_format(struct v4l2_subdev *sd,
  648. struct v4l2_subdev_state *sd_state,
  649. struct v4l2_subdev_format *fmt)
  650. {
  651. struct imx412 *imx412 = to_imx412(sd);
  652. mutex_lock(&imx412->mutex);
  653. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  654. struct v4l2_mbus_framefmt *framefmt;
  655. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  656. fmt->format = *framefmt;
  657. } else {
  658. imx412_fill_pad_format(imx412, imx412->cur_mode, fmt);
  659. }
  660. mutex_unlock(&imx412->mutex);
  661. return 0;
  662. }
  663. /**
  664. * imx412_set_pad_format() - Set subdevice pad format
  665. * @sd: pointer to imx412 V4L2 sub-device structure
  666. * @sd_state: V4L2 sub-device configuration
  667. * @fmt: V4L2 sub-device format need to be set
  668. *
  669. * Return: 0 if successful, error code otherwise.
  670. */
  671. static int imx412_set_pad_format(struct v4l2_subdev *sd,
  672. struct v4l2_subdev_state *sd_state,
  673. struct v4l2_subdev_format *fmt)
  674. {
  675. struct imx412 *imx412 = to_imx412(sd);
  676. const struct imx412_mode *mode;
  677. int ret = 0;
  678. mutex_lock(&imx412->mutex);
  679. mode = &supported_mode;
  680. imx412_fill_pad_format(imx412, mode, fmt);
  681. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  682. struct v4l2_mbus_framefmt *framefmt;
  683. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  684. *framefmt = fmt->format;
  685. } else {
  686. ret = imx412_update_controls(imx412, mode);
  687. if (!ret)
  688. imx412->cur_mode = mode;
  689. }
  690. mutex_unlock(&imx412->mutex);
  691. return ret;
  692. }
  693. /**
  694. * imx412_init_pad_cfg() - Initialize sub-device pad configuration
  695. * @sd: pointer to imx412 V4L2 sub-device structure
  696. * @sd_state: V4L2 sub-device configuration
  697. *
  698. * Return: 0 if successful, error code otherwise.
  699. */
  700. static int imx412_init_pad_cfg(struct v4l2_subdev *sd,
  701. struct v4l2_subdev_state *sd_state)
  702. {
  703. struct imx412 *imx412 = to_imx412(sd);
  704. struct v4l2_subdev_format fmt = { 0 };
  705. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  706. imx412_fill_pad_format(imx412, &supported_mode, &fmt);
  707. return imx412_set_pad_format(sd, sd_state, &fmt);
  708. }
  709. /**
  710. * imx412_start_streaming() - Start sensor stream
  711. * @imx412: pointer to imx412 device
  712. *
  713. * Return: 0 if successful, error code otherwise.
  714. */
  715. static int imx412_start_streaming(struct imx412 *imx412)
  716. {
  717. const struct imx412_reg_list *reg_list;
  718. int ret;
  719. /* Write sensor mode registers */
  720. reg_list = &imx412->cur_mode->reg_list;
  721. ret = imx412_write_regs(imx412, reg_list->regs,
  722. reg_list->num_of_regs);
  723. if (ret) {
  724. dev_err(imx412->dev, "fail to write initial registers");
  725. return ret;
  726. }
  727. /* Setup handler will write actual exposure and gain */
  728. ret = __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler);
  729. if (ret) {
  730. dev_err(imx412->dev, "fail to setup handler");
  731. return ret;
  732. }
  733. /* Delay is required before streaming*/
  734. usleep_range(7400, 8000);
  735. /* Start streaming */
  736. ret = imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
  737. 1, IMX412_MODE_STREAMING);
  738. if (ret) {
  739. dev_err(imx412->dev, "fail to start streaming");
  740. return ret;
  741. }
  742. return 0;
  743. }
  744. /**
  745. * imx412_stop_streaming() - Stop sensor stream
  746. * @imx412: pointer to imx412 device
  747. *
  748. * Return: 0 if successful, error code otherwise.
  749. */
  750. static int imx412_stop_streaming(struct imx412 *imx412)
  751. {
  752. return imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
  753. 1, IMX412_MODE_STANDBY);
  754. }
  755. /**
  756. * imx412_set_stream() - Enable sensor streaming
  757. * @sd: pointer to imx412 subdevice
  758. * @enable: set to enable sensor streaming
  759. *
  760. * Return: 0 if successful, error code otherwise.
  761. */
  762. static int imx412_set_stream(struct v4l2_subdev *sd, int enable)
  763. {
  764. struct imx412 *imx412 = to_imx412(sd);
  765. int ret;
  766. mutex_lock(&imx412->mutex);
  767. if (imx412->streaming == enable) {
  768. mutex_unlock(&imx412->mutex);
  769. return 0;
  770. }
  771. if (enable) {
  772. ret = pm_runtime_resume_and_get(imx412->dev);
  773. if (ret)
  774. goto error_unlock;
  775. ret = imx412_start_streaming(imx412);
  776. if (ret)
  777. goto error_power_off;
  778. } else {
  779. imx412_stop_streaming(imx412);
  780. pm_runtime_put(imx412->dev);
  781. }
  782. imx412->streaming = enable;
  783. mutex_unlock(&imx412->mutex);
  784. return 0;
  785. error_power_off:
  786. pm_runtime_put(imx412->dev);
  787. error_unlock:
  788. mutex_unlock(&imx412->mutex);
  789. return ret;
  790. }
  791. /**
  792. * imx412_detect() - Detect imx412 sensor
  793. * @imx412: pointer to imx412 device
  794. *
  795. * Return: 0 if successful, -EIO if sensor id does not match
  796. */
  797. static int imx412_detect(struct imx412 *imx412)
  798. {
  799. int ret;
  800. u32 val;
  801. ret = imx412_read_reg(imx412, IMX412_REG_ID, 2, &val);
  802. if (ret)
  803. return ret;
  804. if (val != IMX412_ID) {
  805. dev_err(imx412->dev, "chip id mismatch: %x!=%x",
  806. IMX412_ID, val);
  807. return -ENXIO;
  808. }
  809. return 0;
  810. }
  811. /**
  812. * imx412_parse_hw_config() - Parse HW configuration and check if supported
  813. * @imx412: pointer to imx412 device
  814. *
  815. * Return: 0 if successful, error code otherwise.
  816. */
  817. static int imx412_parse_hw_config(struct imx412 *imx412)
  818. {
  819. struct fwnode_handle *fwnode = dev_fwnode(imx412->dev);
  820. struct v4l2_fwnode_endpoint bus_cfg = {
  821. .bus_type = V4L2_MBUS_CSI2_DPHY
  822. };
  823. struct fwnode_handle *ep;
  824. unsigned long rate;
  825. unsigned int i;
  826. int ret;
  827. if (!fwnode)
  828. return -ENXIO;
  829. /* Request optional reset pin */
  830. imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset",
  831. GPIOD_OUT_LOW);
  832. if (IS_ERR(imx412->reset_gpio)) {
  833. dev_err(imx412->dev, "failed to get reset gpio %ld",
  834. PTR_ERR(imx412->reset_gpio));
  835. return PTR_ERR(imx412->reset_gpio);
  836. }
  837. /* Get sensor input clock */
  838. imx412->inclk = devm_clk_get(imx412->dev, NULL);
  839. if (IS_ERR(imx412->inclk)) {
  840. dev_err(imx412->dev, "could not get inclk");
  841. return PTR_ERR(imx412->inclk);
  842. }
  843. rate = clk_get_rate(imx412->inclk);
  844. if (rate != IMX412_INCLK_RATE) {
  845. dev_err(imx412->dev, "inclk frequency mismatch");
  846. return -EINVAL;
  847. }
  848. /* Get optional DT defined regulators */
  849. for (i = 0; i < ARRAY_SIZE(imx412_supply_names); i++)
  850. imx412->supplies[i].supply = imx412_supply_names[i];
  851. ret = devm_regulator_bulk_get(imx412->dev,
  852. ARRAY_SIZE(imx412_supply_names),
  853. imx412->supplies);
  854. if (ret)
  855. return ret;
  856. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  857. if (!ep)
  858. return -ENXIO;
  859. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  860. fwnode_handle_put(ep);
  861. if (ret)
  862. return ret;
  863. if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX412_NUM_DATA_LANES) {
  864. dev_err(imx412->dev,
  865. "number of CSI2 data lanes %d is not supported",
  866. bus_cfg.bus.mipi_csi2.num_data_lanes);
  867. ret = -EINVAL;
  868. goto done_endpoint_free;
  869. }
  870. if (!bus_cfg.nr_of_link_frequencies) {
  871. dev_err(imx412->dev, "no link frequencies defined");
  872. ret = -EINVAL;
  873. goto done_endpoint_free;
  874. }
  875. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  876. if (bus_cfg.link_frequencies[i] == IMX412_LINK_FREQ)
  877. goto done_endpoint_free;
  878. ret = -EINVAL;
  879. done_endpoint_free:
  880. v4l2_fwnode_endpoint_free(&bus_cfg);
  881. return ret;
  882. }
  883. /* V4l2 subdevice ops */
  884. static const struct v4l2_subdev_video_ops imx412_video_ops = {
  885. .s_stream = imx412_set_stream,
  886. };
  887. static const struct v4l2_subdev_pad_ops imx412_pad_ops = {
  888. .init_cfg = imx412_init_pad_cfg,
  889. .enum_mbus_code = imx412_enum_mbus_code,
  890. .enum_frame_size = imx412_enum_frame_size,
  891. .get_fmt = imx412_get_pad_format,
  892. .set_fmt = imx412_set_pad_format,
  893. };
  894. static const struct v4l2_subdev_ops imx412_subdev_ops = {
  895. .video = &imx412_video_ops,
  896. .pad = &imx412_pad_ops,
  897. };
  898. /**
  899. * imx412_power_on() - Sensor power on sequence
  900. * @dev: pointer to i2c device
  901. *
  902. * Return: 0 if successful, error code otherwise.
  903. */
  904. static int imx412_power_on(struct device *dev)
  905. {
  906. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  907. struct imx412 *imx412 = to_imx412(sd);
  908. int ret;
  909. ret = regulator_bulk_enable(ARRAY_SIZE(imx412_supply_names),
  910. imx412->supplies);
  911. if (ret < 0) {
  912. dev_err(dev, "failed to enable regulators\n");
  913. return ret;
  914. }
  915. gpiod_set_value_cansleep(imx412->reset_gpio, 0);
  916. ret = clk_prepare_enable(imx412->inclk);
  917. if (ret) {
  918. dev_err(imx412->dev, "fail to enable inclk");
  919. goto error_reset;
  920. }
  921. usleep_range(1000, 1200);
  922. return 0;
  923. error_reset:
  924. gpiod_set_value_cansleep(imx412->reset_gpio, 1);
  925. regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
  926. imx412->supplies);
  927. return ret;
  928. }
  929. /**
  930. * imx412_power_off() - Sensor power off sequence
  931. * @dev: pointer to i2c device
  932. *
  933. * Return: 0 if successful, error code otherwise.
  934. */
  935. static int imx412_power_off(struct device *dev)
  936. {
  937. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  938. struct imx412 *imx412 = to_imx412(sd);
  939. clk_disable_unprepare(imx412->inclk);
  940. gpiod_set_value_cansleep(imx412->reset_gpio, 1);
  941. regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
  942. imx412->supplies);
  943. return 0;
  944. }
  945. /**
  946. * imx412_init_controls() - Initialize sensor subdevice controls
  947. * @imx412: pointer to imx412 device
  948. *
  949. * Return: 0 if successful, error code otherwise.
  950. */
  951. static int imx412_init_controls(struct imx412 *imx412)
  952. {
  953. struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler;
  954. const struct imx412_mode *mode = imx412->cur_mode;
  955. u32 lpfr;
  956. int ret;
  957. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
  958. if (ret)
  959. return ret;
  960. /* Serialize controls with sensor device */
  961. ctrl_hdlr->lock = &imx412->mutex;
  962. /* Initialize exposure and gain */
  963. lpfr = mode->vblank + mode->height;
  964. imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  965. &imx412_ctrl_ops,
  966. V4L2_CID_EXPOSURE,
  967. IMX412_EXPOSURE_MIN,
  968. lpfr - IMX412_EXPOSURE_OFFSET,
  969. IMX412_EXPOSURE_STEP,
  970. IMX412_EXPOSURE_DEFAULT);
  971. imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  972. &imx412_ctrl_ops,
  973. V4L2_CID_ANALOGUE_GAIN,
  974. IMX412_AGAIN_MIN,
  975. IMX412_AGAIN_MAX,
  976. IMX412_AGAIN_STEP,
  977. IMX412_AGAIN_DEFAULT);
  978. v4l2_ctrl_cluster(2, &imx412->exp_ctrl);
  979. imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  980. &imx412_ctrl_ops,
  981. V4L2_CID_VBLANK,
  982. mode->vblank_min,
  983. mode->vblank_max,
  984. 1, mode->vblank);
  985. /* Read only controls */
  986. imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  987. &imx412_ctrl_ops,
  988. V4L2_CID_PIXEL_RATE,
  989. mode->pclk, mode->pclk,
  990. 1, mode->pclk);
  991. imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  992. &imx412_ctrl_ops,
  993. V4L2_CID_LINK_FREQ,
  994. ARRAY_SIZE(link_freq) -
  995. 1,
  996. mode->link_freq_idx,
  997. link_freq);
  998. if (imx412->link_freq_ctrl)
  999. imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1000. imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1001. &imx412_ctrl_ops,
  1002. V4L2_CID_HBLANK,
  1003. IMX412_REG_MIN,
  1004. IMX412_REG_MAX,
  1005. 1, mode->hblank);
  1006. if (imx412->hblank_ctrl)
  1007. imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1008. if (ctrl_hdlr->error) {
  1009. dev_err(imx412->dev, "control init failed: %d",
  1010. ctrl_hdlr->error);
  1011. v4l2_ctrl_handler_free(ctrl_hdlr);
  1012. return ctrl_hdlr->error;
  1013. }
  1014. imx412->sd.ctrl_handler = ctrl_hdlr;
  1015. return 0;
  1016. }
  1017. /**
  1018. * imx412_probe() - I2C client device binding
  1019. * @client: pointer to i2c client device
  1020. *
  1021. * Return: 0 if successful, error code otherwise.
  1022. */
  1023. static int imx412_probe(struct i2c_client *client)
  1024. {
  1025. struct imx412 *imx412;
  1026. int ret;
  1027. imx412 = devm_kzalloc(&client->dev, sizeof(*imx412), GFP_KERNEL);
  1028. if (!imx412)
  1029. return -ENOMEM;
  1030. imx412->dev = &client->dev;
  1031. /* Initialize subdev */
  1032. v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops);
  1033. ret = imx412_parse_hw_config(imx412);
  1034. if (ret) {
  1035. dev_err(imx412->dev, "HW configuration is not supported");
  1036. return ret;
  1037. }
  1038. mutex_init(&imx412->mutex);
  1039. ret = imx412_power_on(imx412->dev);
  1040. if (ret) {
  1041. dev_err(imx412->dev, "failed to power-on the sensor");
  1042. goto error_mutex_destroy;
  1043. }
  1044. /* Check module identity */
  1045. ret = imx412_detect(imx412);
  1046. if (ret) {
  1047. dev_err(imx412->dev, "failed to find sensor: %d", ret);
  1048. goto error_power_off;
  1049. }
  1050. /* Set default mode to max resolution */
  1051. imx412->cur_mode = &supported_mode;
  1052. imx412->vblank = imx412->cur_mode->vblank;
  1053. ret = imx412_init_controls(imx412);
  1054. if (ret) {
  1055. dev_err(imx412->dev, "failed to init controls: %d", ret);
  1056. goto error_power_off;
  1057. }
  1058. /* Initialize subdev */
  1059. imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1060. imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1061. /* Initialize source pad */
  1062. imx412->pad.flags = MEDIA_PAD_FL_SOURCE;
  1063. ret = media_entity_pads_init(&imx412->sd.entity, 1, &imx412->pad);
  1064. if (ret) {
  1065. dev_err(imx412->dev, "failed to init entity pads: %d", ret);
  1066. goto error_handler_free;
  1067. }
  1068. ret = v4l2_async_register_subdev_sensor(&imx412->sd);
  1069. if (ret < 0) {
  1070. dev_err(imx412->dev,
  1071. "failed to register async subdev: %d", ret);
  1072. goto error_media_entity;
  1073. }
  1074. pm_runtime_set_active(imx412->dev);
  1075. pm_runtime_enable(imx412->dev);
  1076. pm_runtime_idle(imx412->dev);
  1077. return 0;
  1078. error_media_entity:
  1079. media_entity_cleanup(&imx412->sd.entity);
  1080. error_handler_free:
  1081. v4l2_ctrl_handler_free(imx412->sd.ctrl_handler);
  1082. error_power_off:
  1083. imx412_power_off(imx412->dev);
  1084. error_mutex_destroy:
  1085. mutex_destroy(&imx412->mutex);
  1086. return ret;
  1087. }
  1088. /**
  1089. * imx412_remove() - I2C client device unbinding
  1090. * @client: pointer to I2C client device
  1091. *
  1092. * Return: 0 if successful, error code otherwise.
  1093. */
  1094. static void imx412_remove(struct i2c_client *client)
  1095. {
  1096. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1097. struct imx412 *imx412 = to_imx412(sd);
  1098. v4l2_async_unregister_subdev(sd);
  1099. media_entity_cleanup(&sd->entity);
  1100. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1101. pm_runtime_disable(&client->dev);
  1102. if (!pm_runtime_status_suspended(&client->dev))
  1103. imx412_power_off(&client->dev);
  1104. pm_runtime_set_suspended(&client->dev);
  1105. mutex_destroy(&imx412->mutex);
  1106. }
  1107. static const struct dev_pm_ops imx412_pm_ops = {
  1108. SET_RUNTIME_PM_OPS(imx412_power_off, imx412_power_on, NULL)
  1109. };
  1110. static const struct of_device_id imx412_of_match[] = {
  1111. { .compatible = "sony,imx412" },
  1112. { }
  1113. };
  1114. MODULE_DEVICE_TABLE(of, imx412_of_match);
  1115. static struct i2c_driver imx412_driver = {
  1116. .probe_new = imx412_probe,
  1117. .remove = imx412_remove,
  1118. .driver = {
  1119. .name = "imx412",
  1120. .pm = &imx412_pm_ops,
  1121. .of_match_table = imx412_of_match,
  1122. },
  1123. };
  1124. module_i2c_driver(imx412_driver);
  1125. MODULE_DESCRIPTION("Sony imx412 sensor driver");
  1126. MODULE_LICENSE("GPL");