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