ov9282.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OmniVision ov9282 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 <media/v4l2-ctrls.h>
  14. #include <media/v4l2-fwnode.h>
  15. #include <media/v4l2-subdev.h>
  16. /* Streaming Mode */
  17. #define OV9282_REG_MODE_SELECT 0x0100
  18. #define OV9282_MODE_STANDBY 0x00
  19. #define OV9282_MODE_STREAMING 0x01
  20. /* Lines per frame */
  21. #define OV9282_REG_LPFR 0x380e
  22. /* Chip ID */
  23. #define OV9282_REG_ID 0x300a
  24. #define OV9282_ID 0x9281
  25. /* Exposure control */
  26. #define OV9282_REG_EXPOSURE 0x3500
  27. #define OV9282_EXPOSURE_MIN 1
  28. #define OV9282_EXPOSURE_OFFSET 12
  29. #define OV9282_EXPOSURE_STEP 1
  30. #define OV9282_EXPOSURE_DEFAULT 0x0282
  31. /* Analog gain control */
  32. #define OV9282_REG_AGAIN 0x3509
  33. #define OV9282_AGAIN_MIN 0x10
  34. #define OV9282_AGAIN_MAX 0xff
  35. #define OV9282_AGAIN_STEP 1
  36. #define OV9282_AGAIN_DEFAULT 0x10
  37. /* Group hold register */
  38. #define OV9282_REG_HOLD 0x3308
  39. /* Input clock rate */
  40. #define OV9282_INCLK_RATE 24000000
  41. /* CSI2 HW configuration */
  42. #define OV9282_LINK_FREQ 400000000
  43. #define OV9282_NUM_DATA_LANES 2
  44. #define OV9282_REG_MIN 0x00
  45. #define OV9282_REG_MAX 0xfffff
  46. /**
  47. * struct ov9282_reg - ov9282 sensor register
  48. * @address: Register address
  49. * @val: Register value
  50. */
  51. struct ov9282_reg {
  52. u16 address;
  53. u8 val;
  54. };
  55. /**
  56. * struct ov9282_reg_list - ov9282 sensor register list
  57. * @num_of_regs: Number of registers in the list
  58. * @regs: Pointer to register list
  59. */
  60. struct ov9282_reg_list {
  61. u32 num_of_regs;
  62. const struct ov9282_reg *regs;
  63. };
  64. /**
  65. * struct ov9282_mode - ov9282 sensor mode structure
  66. * @width: Frame width
  67. * @height: Frame height
  68. * @code: Format code
  69. * @hblank: Horizontal blanking in lines
  70. * @vblank: Vertical blanking in lines
  71. * @vblank_min: Minimum vertical blanking in lines
  72. * @vblank_max: Maximum vertical blanking in lines
  73. * @pclk: Sensor pixel clock
  74. * @link_freq_idx: Link frequency index
  75. * @reg_list: Register list for sensor mode
  76. */
  77. struct ov9282_mode {
  78. u32 width;
  79. u32 height;
  80. u32 code;
  81. u32 hblank;
  82. u32 vblank;
  83. u32 vblank_min;
  84. u32 vblank_max;
  85. u64 pclk;
  86. u32 link_freq_idx;
  87. struct ov9282_reg_list reg_list;
  88. };
  89. /**
  90. * struct ov9282 - ov9282 sensor device structure
  91. * @dev: Pointer to generic device
  92. * @client: Pointer to i2c client
  93. * @sd: V4L2 sub-device
  94. * @pad: Media pad. Only one pad supported
  95. * @reset_gpio: Sensor reset gpio
  96. * @inclk: Sensor input clock
  97. * @ctrl_handler: V4L2 control handler
  98. * @link_freq_ctrl: Pointer to link frequency control
  99. * @pclk_ctrl: Pointer to pixel clock control
  100. * @hblank_ctrl: Pointer to horizontal blanking control
  101. * @vblank_ctrl: Pointer to vertical blanking control
  102. * @exp_ctrl: Pointer to exposure control
  103. * @again_ctrl: Pointer to analog gain control
  104. * @vblank: Vertical blanking in lines
  105. * @cur_mode: Pointer to current selected sensor mode
  106. * @mutex: Mutex for serializing sensor controls
  107. * @streaming: Flag indicating streaming state
  108. */
  109. struct ov9282 {
  110. struct device *dev;
  111. struct i2c_client *client;
  112. struct v4l2_subdev sd;
  113. struct media_pad pad;
  114. struct gpio_desc *reset_gpio;
  115. struct clk *inclk;
  116. struct v4l2_ctrl_handler ctrl_handler;
  117. struct v4l2_ctrl *link_freq_ctrl;
  118. struct v4l2_ctrl *pclk_ctrl;
  119. struct v4l2_ctrl *hblank_ctrl;
  120. struct v4l2_ctrl *vblank_ctrl;
  121. struct {
  122. struct v4l2_ctrl *exp_ctrl;
  123. struct v4l2_ctrl *again_ctrl;
  124. };
  125. u32 vblank;
  126. const struct ov9282_mode *cur_mode;
  127. struct mutex mutex;
  128. bool streaming;
  129. };
  130. static const s64 link_freq[] = {
  131. OV9282_LINK_FREQ,
  132. };
  133. /* Sensor mode registers */
  134. static const struct ov9282_reg mode_1280x720_regs[] = {
  135. {0x0302, 0x32},
  136. {0x030d, 0x50},
  137. {0x030e, 0x02},
  138. {0x3001, 0x00},
  139. {0x3004, 0x00},
  140. {0x3005, 0x00},
  141. {0x3006, 0x04},
  142. {0x3011, 0x0a},
  143. {0x3013, 0x18},
  144. {0x301c, 0xf0},
  145. {0x3022, 0x01},
  146. {0x3030, 0x10},
  147. {0x3039, 0x32},
  148. {0x303a, 0x00},
  149. {0x3500, 0x00},
  150. {0x3501, 0x5f},
  151. {0x3502, 0x1e},
  152. {0x3503, 0x08},
  153. {0x3505, 0x8c},
  154. {0x3507, 0x03},
  155. {0x3508, 0x00},
  156. {0x3509, 0x10},
  157. {0x3610, 0x80},
  158. {0x3611, 0xa0},
  159. {0x3620, 0x6e},
  160. {0x3632, 0x56},
  161. {0x3633, 0x78},
  162. {0x3666, 0x00},
  163. {0x366f, 0x5a},
  164. {0x3680, 0x84},
  165. {0x3712, 0x80},
  166. {0x372d, 0x22},
  167. {0x3731, 0x80},
  168. {0x3732, 0x30},
  169. {0x3778, 0x00},
  170. {0x377d, 0x22},
  171. {0x3788, 0x02},
  172. {0x3789, 0xa4},
  173. {0x378a, 0x00},
  174. {0x378b, 0x4a},
  175. {0x3799, 0x20},
  176. {0x3800, 0x00},
  177. {0x3801, 0x00},
  178. {0x3802, 0x00},
  179. {0x3803, 0x00},
  180. {0x3804, 0x05},
  181. {0x3805, 0x0f},
  182. {0x3806, 0x02},
  183. {0x3807, 0xdf},
  184. {0x3808, 0x05},
  185. {0x3809, 0x00},
  186. {0x380a, 0x02},
  187. {0x380b, 0xd0},
  188. {0x380c, 0x05},
  189. {0x380d, 0xfa},
  190. {0x380e, 0x06},
  191. {0x380f, 0xce},
  192. {0x3810, 0x00},
  193. {0x3811, 0x08},
  194. {0x3812, 0x00},
  195. {0x3813, 0x08},
  196. {0x3814, 0x11},
  197. {0x3815, 0x11},
  198. {0x3820, 0x3c},
  199. {0x3821, 0x84},
  200. {0x3881, 0x42},
  201. {0x38a8, 0x02},
  202. {0x38a9, 0x80},
  203. {0x38b1, 0x00},
  204. {0x38c4, 0x00},
  205. {0x38c5, 0xc0},
  206. {0x38c6, 0x04},
  207. {0x38c7, 0x80},
  208. {0x3920, 0xff},
  209. {0x4003, 0x40},
  210. {0x4008, 0x02},
  211. {0x4009, 0x05},
  212. {0x400c, 0x00},
  213. {0x400d, 0x03},
  214. {0x4010, 0x40},
  215. {0x4043, 0x40},
  216. {0x4307, 0x30},
  217. {0x4317, 0x00},
  218. {0x4501, 0x00},
  219. {0x4507, 0x00},
  220. {0x4509, 0x80},
  221. {0x450a, 0x08},
  222. {0x4601, 0x04},
  223. {0x470f, 0x00},
  224. {0x4f07, 0x00},
  225. {0x4800, 0x20},
  226. {0x5000, 0x9f},
  227. {0x5001, 0x00},
  228. {0x5e00, 0x00},
  229. {0x5d00, 0x07},
  230. {0x5d01, 0x00},
  231. {0x0101, 0x01},
  232. {0x1000, 0x03},
  233. {0x5a08, 0x84},
  234. };
  235. /* Supported sensor mode configurations */
  236. static const struct ov9282_mode supported_mode = {
  237. .width = 1280,
  238. .height = 720,
  239. .hblank = 250,
  240. .vblank = 1022,
  241. .vblank_min = 151,
  242. .vblank_max = 51540,
  243. .pclk = 160000000,
  244. .link_freq_idx = 0,
  245. .code = MEDIA_BUS_FMT_Y10_1X10,
  246. .reg_list = {
  247. .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
  248. .regs = mode_1280x720_regs,
  249. },
  250. };
  251. /**
  252. * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
  253. * @subdev: pointer to ov9282 V4L2 sub-device
  254. *
  255. * Return: pointer to ov9282 device
  256. */
  257. static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
  258. {
  259. return container_of(subdev, struct ov9282, sd);
  260. }
  261. /**
  262. * ov9282_read_reg() - Read registers.
  263. * @ov9282: pointer to ov9282 device
  264. * @reg: register address
  265. * @len: length of bytes to read. Max supported bytes is 4
  266. * @val: pointer to register value to be filled.
  267. *
  268. * Return: 0 if successful, error code otherwise.
  269. */
  270. static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
  271. {
  272. struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
  273. struct i2c_msg msgs[2] = {0};
  274. u8 addr_buf[2] = {0};
  275. u8 data_buf[4] = {0};
  276. int ret;
  277. if (WARN_ON(len > 4))
  278. return -EINVAL;
  279. put_unaligned_be16(reg, addr_buf);
  280. /* Write register address */
  281. msgs[0].addr = client->addr;
  282. msgs[0].flags = 0;
  283. msgs[0].len = ARRAY_SIZE(addr_buf);
  284. msgs[0].buf = addr_buf;
  285. /* Read data from register */
  286. msgs[1].addr = client->addr;
  287. msgs[1].flags = I2C_M_RD;
  288. msgs[1].len = len;
  289. msgs[1].buf = &data_buf[4 - len];
  290. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  291. if (ret != ARRAY_SIZE(msgs))
  292. return -EIO;
  293. *val = get_unaligned_be32(data_buf);
  294. return 0;
  295. }
  296. /**
  297. * ov9282_write_reg() - Write register
  298. * @ov9282: pointer to ov9282 device
  299. * @reg: register address
  300. * @len: length of bytes. Max supported bytes is 4
  301. * @val: register value
  302. *
  303. * Return: 0 if successful, error code otherwise.
  304. */
  305. static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
  306. {
  307. struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
  308. u8 buf[6] = {0};
  309. if (WARN_ON(len > 4))
  310. return -EINVAL;
  311. put_unaligned_be16(reg, buf);
  312. put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  313. if (i2c_master_send(client, buf, len + 2) != len + 2)
  314. return -EIO;
  315. return 0;
  316. }
  317. /**
  318. * ov9282_write_regs() - Write a list of registers
  319. * @ov9282: pointer to ov9282 device
  320. * @regs: list of registers to be written
  321. * @len: length of registers array
  322. *
  323. * Return: 0 if successful, error code otherwise.
  324. */
  325. static int ov9282_write_regs(struct ov9282 *ov9282,
  326. const struct ov9282_reg *regs, u32 len)
  327. {
  328. unsigned int i;
  329. int ret;
  330. for (i = 0; i < len; i++) {
  331. ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
  332. if (ret)
  333. return ret;
  334. }
  335. return 0;
  336. }
  337. /**
  338. * ov9282_update_controls() - Update control ranges based on streaming mode
  339. * @ov9282: pointer to ov9282 device
  340. * @mode: pointer to ov9282_mode sensor mode
  341. *
  342. * Return: 0 if successful, error code otherwise.
  343. */
  344. static int ov9282_update_controls(struct ov9282 *ov9282,
  345. const struct ov9282_mode *mode)
  346. {
  347. int ret;
  348. ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
  349. if (ret)
  350. return ret;
  351. ret = __v4l2_ctrl_s_ctrl(ov9282->hblank_ctrl, mode->hblank);
  352. if (ret)
  353. return ret;
  354. return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
  355. mode->vblank_max, 1, mode->vblank);
  356. }
  357. /**
  358. * ov9282_update_exp_gain() - Set updated exposure and gain
  359. * @ov9282: pointer to ov9282 device
  360. * @exposure: updated exposure value
  361. * @gain: updated analog gain value
  362. *
  363. * Return: 0 if successful, error code otherwise.
  364. */
  365. static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
  366. {
  367. u32 lpfr;
  368. int ret;
  369. lpfr = ov9282->vblank + ov9282->cur_mode->height;
  370. dev_dbg(ov9282->dev, "Set exp %u, analog gain %u, lpfr %u",
  371. exposure, gain, lpfr);
  372. ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
  373. if (ret)
  374. return ret;
  375. ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
  376. if (ret)
  377. goto error_release_group_hold;
  378. ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
  379. if (ret)
  380. goto error_release_group_hold;
  381. ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
  382. error_release_group_hold:
  383. ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
  384. return ret;
  385. }
  386. /**
  387. * ov9282_set_ctrl() - Set subdevice control
  388. * @ctrl: pointer to v4l2_ctrl structure
  389. *
  390. * Supported controls:
  391. * - V4L2_CID_VBLANK
  392. * - cluster controls:
  393. * - V4L2_CID_ANALOGUE_GAIN
  394. * - V4L2_CID_EXPOSURE
  395. *
  396. * Return: 0 if successful, error code otherwise.
  397. */
  398. static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
  399. {
  400. struct ov9282 *ov9282 =
  401. container_of(ctrl->handler, struct ov9282, ctrl_handler);
  402. u32 analog_gain;
  403. u32 exposure;
  404. int ret;
  405. switch (ctrl->id) {
  406. case V4L2_CID_VBLANK:
  407. ov9282->vblank = ov9282->vblank_ctrl->val;
  408. dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
  409. ov9282->vblank,
  410. ov9282->vblank + ov9282->cur_mode->height);
  411. ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
  412. OV9282_EXPOSURE_MIN,
  413. ov9282->vblank +
  414. ov9282->cur_mode->height -
  415. OV9282_EXPOSURE_OFFSET,
  416. 1, OV9282_EXPOSURE_DEFAULT);
  417. break;
  418. case V4L2_CID_EXPOSURE:
  419. /* Set controls only if sensor is in power on state */
  420. if (!pm_runtime_get_if_in_use(ov9282->dev))
  421. return 0;
  422. exposure = ctrl->val;
  423. analog_gain = ov9282->again_ctrl->val;
  424. dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
  425. exposure, analog_gain);
  426. ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
  427. pm_runtime_put(ov9282->dev);
  428. break;
  429. default:
  430. dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
  431. ret = -EINVAL;
  432. }
  433. return ret;
  434. }
  435. /* V4l2 subdevice control ops*/
  436. static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
  437. .s_ctrl = ov9282_set_ctrl,
  438. };
  439. /**
  440. * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  441. * @sd: pointer to ov9282 V4L2 sub-device structure
  442. * @sd_state: V4L2 sub-device configuration
  443. * @code: V4L2 sub-device code enumeration need to be filled
  444. *
  445. * Return: 0 if successful, error code otherwise.
  446. */
  447. static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
  448. struct v4l2_subdev_state *sd_state,
  449. struct v4l2_subdev_mbus_code_enum *code)
  450. {
  451. if (code->index > 0)
  452. return -EINVAL;
  453. code->code = supported_mode.code;
  454. return 0;
  455. }
  456. /**
  457. * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  458. * @sd: pointer to ov9282 V4L2 sub-device structure
  459. * @sd_state: V4L2 sub-device configuration
  460. * @fsize: V4L2 sub-device size enumeration need to be filled
  461. *
  462. * Return: 0 if successful, error code otherwise.
  463. */
  464. static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
  465. struct v4l2_subdev_state *sd_state,
  466. struct v4l2_subdev_frame_size_enum *fsize)
  467. {
  468. if (fsize->index > 0)
  469. return -EINVAL;
  470. if (fsize->code != supported_mode.code)
  471. return -EINVAL;
  472. fsize->min_width = supported_mode.width;
  473. fsize->max_width = fsize->min_width;
  474. fsize->min_height = supported_mode.height;
  475. fsize->max_height = fsize->min_height;
  476. return 0;
  477. }
  478. /**
  479. * ov9282_fill_pad_format() - Fill subdevice pad format
  480. * from selected sensor mode
  481. * @ov9282: pointer to ov9282 device
  482. * @mode: pointer to ov9282_mode sensor mode
  483. * @fmt: V4L2 sub-device format need to be filled
  484. */
  485. static void ov9282_fill_pad_format(struct ov9282 *ov9282,
  486. const struct ov9282_mode *mode,
  487. struct v4l2_subdev_format *fmt)
  488. {
  489. fmt->format.width = mode->width;
  490. fmt->format.height = mode->height;
  491. fmt->format.code = mode->code;
  492. fmt->format.field = V4L2_FIELD_NONE;
  493. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  494. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  495. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  496. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  497. }
  498. /**
  499. * ov9282_get_pad_format() - Get subdevice pad format
  500. * @sd: pointer to ov9282 V4L2 sub-device structure
  501. * @sd_state: V4L2 sub-device configuration
  502. * @fmt: V4L2 sub-device format need to be set
  503. *
  504. * Return: 0 if successful, error code otherwise.
  505. */
  506. static int ov9282_get_pad_format(struct v4l2_subdev *sd,
  507. struct v4l2_subdev_state *sd_state,
  508. struct v4l2_subdev_format *fmt)
  509. {
  510. struct ov9282 *ov9282 = to_ov9282(sd);
  511. mutex_lock(&ov9282->mutex);
  512. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  513. struct v4l2_mbus_framefmt *framefmt;
  514. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  515. fmt->format = *framefmt;
  516. } else {
  517. ov9282_fill_pad_format(ov9282, ov9282->cur_mode, fmt);
  518. }
  519. mutex_unlock(&ov9282->mutex);
  520. return 0;
  521. }
  522. /**
  523. * ov9282_set_pad_format() - Set subdevice pad format
  524. * @sd: pointer to ov9282 V4L2 sub-device structure
  525. * @sd_state: V4L2 sub-device configuration
  526. * @fmt: V4L2 sub-device format need to be set
  527. *
  528. * Return: 0 if successful, error code otherwise.
  529. */
  530. static int ov9282_set_pad_format(struct v4l2_subdev *sd,
  531. struct v4l2_subdev_state *sd_state,
  532. struct v4l2_subdev_format *fmt)
  533. {
  534. struct ov9282 *ov9282 = to_ov9282(sd);
  535. const struct ov9282_mode *mode;
  536. int ret = 0;
  537. mutex_lock(&ov9282->mutex);
  538. mode = &supported_mode;
  539. ov9282_fill_pad_format(ov9282, mode, fmt);
  540. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  541. struct v4l2_mbus_framefmt *framefmt;
  542. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  543. *framefmt = fmt->format;
  544. } else {
  545. ret = ov9282_update_controls(ov9282, mode);
  546. if (!ret)
  547. ov9282->cur_mode = mode;
  548. }
  549. mutex_unlock(&ov9282->mutex);
  550. return ret;
  551. }
  552. /**
  553. * ov9282_init_pad_cfg() - Initialize sub-device pad configuration
  554. * @sd: pointer to ov9282 V4L2 sub-device structure
  555. * @sd_state: V4L2 sub-device configuration
  556. *
  557. * Return: 0 if successful, error code otherwise.
  558. */
  559. static int ov9282_init_pad_cfg(struct v4l2_subdev *sd,
  560. struct v4l2_subdev_state *sd_state)
  561. {
  562. struct ov9282 *ov9282 = to_ov9282(sd);
  563. struct v4l2_subdev_format fmt = { 0 };
  564. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  565. ov9282_fill_pad_format(ov9282, &supported_mode, &fmt);
  566. return ov9282_set_pad_format(sd, sd_state, &fmt);
  567. }
  568. /**
  569. * ov9282_start_streaming() - Start sensor stream
  570. * @ov9282: pointer to ov9282 device
  571. *
  572. * Return: 0 if successful, error code otherwise.
  573. */
  574. static int ov9282_start_streaming(struct ov9282 *ov9282)
  575. {
  576. const struct ov9282_reg_list *reg_list;
  577. int ret;
  578. /* Write sensor mode registers */
  579. reg_list = &ov9282->cur_mode->reg_list;
  580. ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
  581. if (ret) {
  582. dev_err(ov9282->dev, "fail to write initial registers");
  583. return ret;
  584. }
  585. /* Setup handler will write actual exposure and gain */
  586. ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
  587. if (ret) {
  588. dev_err(ov9282->dev, "fail to setup handler");
  589. return ret;
  590. }
  591. /* Start streaming */
  592. ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
  593. 1, OV9282_MODE_STREAMING);
  594. if (ret) {
  595. dev_err(ov9282->dev, "fail to start streaming");
  596. return ret;
  597. }
  598. return 0;
  599. }
  600. /**
  601. * ov9282_stop_streaming() - Stop sensor stream
  602. * @ov9282: pointer to ov9282 device
  603. *
  604. * Return: 0 if successful, error code otherwise.
  605. */
  606. static int ov9282_stop_streaming(struct ov9282 *ov9282)
  607. {
  608. return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
  609. 1, OV9282_MODE_STANDBY);
  610. }
  611. /**
  612. * ov9282_set_stream() - Enable sensor streaming
  613. * @sd: pointer to ov9282 subdevice
  614. * @enable: set to enable sensor streaming
  615. *
  616. * Return: 0 if successful, error code otherwise.
  617. */
  618. static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
  619. {
  620. struct ov9282 *ov9282 = to_ov9282(sd);
  621. int ret;
  622. mutex_lock(&ov9282->mutex);
  623. if (ov9282->streaming == enable) {
  624. mutex_unlock(&ov9282->mutex);
  625. return 0;
  626. }
  627. if (enable) {
  628. ret = pm_runtime_resume_and_get(ov9282->dev);
  629. if (ret)
  630. goto error_unlock;
  631. ret = ov9282_start_streaming(ov9282);
  632. if (ret)
  633. goto error_power_off;
  634. } else {
  635. ov9282_stop_streaming(ov9282);
  636. pm_runtime_put(ov9282->dev);
  637. }
  638. ov9282->streaming = enable;
  639. mutex_unlock(&ov9282->mutex);
  640. return 0;
  641. error_power_off:
  642. pm_runtime_put(ov9282->dev);
  643. error_unlock:
  644. mutex_unlock(&ov9282->mutex);
  645. return ret;
  646. }
  647. /**
  648. * ov9282_detect() - Detect ov9282 sensor
  649. * @ov9282: pointer to ov9282 device
  650. *
  651. * Return: 0 if successful, -EIO if sensor id does not match
  652. */
  653. static int ov9282_detect(struct ov9282 *ov9282)
  654. {
  655. int ret;
  656. u32 val;
  657. ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val);
  658. if (ret)
  659. return ret;
  660. if (val != OV9282_ID) {
  661. dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
  662. OV9282_ID, val);
  663. return -ENXIO;
  664. }
  665. return 0;
  666. }
  667. /**
  668. * ov9282_parse_hw_config() - Parse HW configuration and check if supported
  669. * @ov9282: pointer to ov9282 device
  670. *
  671. * Return: 0 if successful, error code otherwise.
  672. */
  673. static int ov9282_parse_hw_config(struct ov9282 *ov9282)
  674. {
  675. struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
  676. struct v4l2_fwnode_endpoint bus_cfg = {
  677. .bus_type = V4L2_MBUS_CSI2_DPHY
  678. };
  679. struct fwnode_handle *ep;
  680. unsigned long rate;
  681. unsigned int i;
  682. int ret;
  683. if (!fwnode)
  684. return -ENXIO;
  685. /* Request optional reset pin */
  686. ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
  687. GPIOD_OUT_LOW);
  688. if (IS_ERR(ov9282->reset_gpio)) {
  689. dev_err(ov9282->dev, "failed to get reset gpio %ld",
  690. PTR_ERR(ov9282->reset_gpio));
  691. return PTR_ERR(ov9282->reset_gpio);
  692. }
  693. /* Get sensor input clock */
  694. ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
  695. if (IS_ERR(ov9282->inclk)) {
  696. dev_err(ov9282->dev, "could not get inclk");
  697. return PTR_ERR(ov9282->inclk);
  698. }
  699. rate = clk_get_rate(ov9282->inclk);
  700. if (rate != OV9282_INCLK_RATE) {
  701. dev_err(ov9282->dev, "inclk frequency mismatch");
  702. return -EINVAL;
  703. }
  704. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  705. if (!ep)
  706. return -ENXIO;
  707. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  708. fwnode_handle_put(ep);
  709. if (ret)
  710. return ret;
  711. if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
  712. dev_err(ov9282->dev,
  713. "number of CSI2 data lanes %d is not supported",
  714. bus_cfg.bus.mipi_csi2.num_data_lanes);
  715. ret = -EINVAL;
  716. goto done_endpoint_free;
  717. }
  718. if (!bus_cfg.nr_of_link_frequencies) {
  719. dev_err(ov9282->dev, "no link frequencies defined");
  720. ret = -EINVAL;
  721. goto done_endpoint_free;
  722. }
  723. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  724. if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
  725. goto done_endpoint_free;
  726. ret = -EINVAL;
  727. done_endpoint_free:
  728. v4l2_fwnode_endpoint_free(&bus_cfg);
  729. return ret;
  730. }
  731. /* V4l2 subdevice ops */
  732. static const struct v4l2_subdev_video_ops ov9282_video_ops = {
  733. .s_stream = ov9282_set_stream,
  734. };
  735. static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
  736. .init_cfg = ov9282_init_pad_cfg,
  737. .enum_mbus_code = ov9282_enum_mbus_code,
  738. .enum_frame_size = ov9282_enum_frame_size,
  739. .get_fmt = ov9282_get_pad_format,
  740. .set_fmt = ov9282_set_pad_format,
  741. };
  742. static const struct v4l2_subdev_ops ov9282_subdev_ops = {
  743. .video = &ov9282_video_ops,
  744. .pad = &ov9282_pad_ops,
  745. };
  746. /**
  747. * ov9282_power_on() - Sensor power on sequence
  748. * @dev: pointer to i2c device
  749. *
  750. * Return: 0 if successful, error code otherwise.
  751. */
  752. static int ov9282_power_on(struct device *dev)
  753. {
  754. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  755. struct ov9282 *ov9282 = to_ov9282(sd);
  756. int ret;
  757. usleep_range(400, 600);
  758. gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
  759. ret = clk_prepare_enable(ov9282->inclk);
  760. if (ret) {
  761. dev_err(ov9282->dev, "fail to enable inclk");
  762. goto error_reset;
  763. }
  764. usleep_range(400, 600);
  765. return 0;
  766. error_reset:
  767. gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
  768. return ret;
  769. }
  770. /**
  771. * ov9282_power_off() - Sensor power off sequence
  772. * @dev: pointer to i2c device
  773. *
  774. * Return: 0 if successful, error code otherwise.
  775. */
  776. static int ov9282_power_off(struct device *dev)
  777. {
  778. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  779. struct ov9282 *ov9282 = to_ov9282(sd);
  780. gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
  781. clk_disable_unprepare(ov9282->inclk);
  782. return 0;
  783. }
  784. /**
  785. * ov9282_init_controls() - Initialize sensor subdevice controls
  786. * @ov9282: pointer to ov9282 device
  787. *
  788. * Return: 0 if successful, error code otherwise.
  789. */
  790. static int ov9282_init_controls(struct ov9282 *ov9282)
  791. {
  792. struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
  793. const struct ov9282_mode *mode = ov9282->cur_mode;
  794. u32 lpfr;
  795. int ret;
  796. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
  797. if (ret)
  798. return ret;
  799. /* Serialize controls with sensor device */
  800. ctrl_hdlr->lock = &ov9282->mutex;
  801. /* Initialize exposure and gain */
  802. lpfr = mode->vblank + mode->height;
  803. ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  804. &ov9282_ctrl_ops,
  805. V4L2_CID_EXPOSURE,
  806. OV9282_EXPOSURE_MIN,
  807. lpfr - OV9282_EXPOSURE_OFFSET,
  808. OV9282_EXPOSURE_STEP,
  809. OV9282_EXPOSURE_DEFAULT);
  810. ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  811. &ov9282_ctrl_ops,
  812. V4L2_CID_ANALOGUE_GAIN,
  813. OV9282_AGAIN_MIN,
  814. OV9282_AGAIN_MAX,
  815. OV9282_AGAIN_STEP,
  816. OV9282_AGAIN_DEFAULT);
  817. v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
  818. ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  819. &ov9282_ctrl_ops,
  820. V4L2_CID_VBLANK,
  821. mode->vblank_min,
  822. mode->vblank_max,
  823. 1, mode->vblank);
  824. /* Read only controls */
  825. ov9282->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  826. &ov9282_ctrl_ops,
  827. V4L2_CID_PIXEL_RATE,
  828. mode->pclk, mode->pclk,
  829. 1, mode->pclk);
  830. ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  831. &ov9282_ctrl_ops,
  832. V4L2_CID_LINK_FREQ,
  833. ARRAY_SIZE(link_freq) -
  834. 1,
  835. mode->link_freq_idx,
  836. link_freq);
  837. if (ov9282->link_freq_ctrl)
  838. ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  839. ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  840. &ov9282_ctrl_ops,
  841. V4L2_CID_HBLANK,
  842. OV9282_REG_MIN,
  843. OV9282_REG_MAX,
  844. 1, mode->hblank);
  845. if (ov9282->hblank_ctrl)
  846. ov9282->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  847. if (ctrl_hdlr->error) {
  848. dev_err(ov9282->dev, "control init failed: %d",
  849. ctrl_hdlr->error);
  850. v4l2_ctrl_handler_free(ctrl_hdlr);
  851. return ctrl_hdlr->error;
  852. }
  853. ov9282->sd.ctrl_handler = ctrl_hdlr;
  854. return 0;
  855. }
  856. /**
  857. * ov9282_probe() - I2C client device binding
  858. * @client: pointer to i2c client device
  859. *
  860. * Return: 0 if successful, error code otherwise.
  861. */
  862. static int ov9282_probe(struct i2c_client *client)
  863. {
  864. struct ov9282 *ov9282;
  865. int ret;
  866. ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
  867. if (!ov9282)
  868. return -ENOMEM;
  869. ov9282->dev = &client->dev;
  870. /* Initialize subdev */
  871. v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
  872. ret = ov9282_parse_hw_config(ov9282);
  873. if (ret) {
  874. dev_err(ov9282->dev, "HW configuration is not supported");
  875. return ret;
  876. }
  877. mutex_init(&ov9282->mutex);
  878. ret = ov9282_power_on(ov9282->dev);
  879. if (ret) {
  880. dev_err(ov9282->dev, "failed to power-on the sensor");
  881. goto error_mutex_destroy;
  882. }
  883. /* Check module identity */
  884. ret = ov9282_detect(ov9282);
  885. if (ret) {
  886. dev_err(ov9282->dev, "failed to find sensor: %d", ret);
  887. goto error_power_off;
  888. }
  889. /* Set default mode to max resolution */
  890. ov9282->cur_mode = &supported_mode;
  891. ov9282->vblank = ov9282->cur_mode->vblank;
  892. ret = ov9282_init_controls(ov9282);
  893. if (ret) {
  894. dev_err(ov9282->dev, "failed to init controls: %d", ret);
  895. goto error_power_off;
  896. }
  897. /* Initialize subdev */
  898. ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  899. ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  900. /* Initialize source pad */
  901. ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
  902. ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
  903. if (ret) {
  904. dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
  905. goto error_handler_free;
  906. }
  907. ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
  908. if (ret < 0) {
  909. dev_err(ov9282->dev,
  910. "failed to register async subdev: %d", ret);
  911. goto error_media_entity;
  912. }
  913. pm_runtime_set_active(ov9282->dev);
  914. pm_runtime_enable(ov9282->dev);
  915. pm_runtime_idle(ov9282->dev);
  916. return 0;
  917. error_media_entity:
  918. media_entity_cleanup(&ov9282->sd.entity);
  919. error_handler_free:
  920. v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
  921. error_power_off:
  922. ov9282_power_off(ov9282->dev);
  923. error_mutex_destroy:
  924. mutex_destroy(&ov9282->mutex);
  925. return ret;
  926. }
  927. /**
  928. * ov9282_remove() - I2C client device unbinding
  929. * @client: pointer to I2C client device
  930. *
  931. * Return: 0 if successful, error code otherwise.
  932. */
  933. static void ov9282_remove(struct i2c_client *client)
  934. {
  935. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  936. struct ov9282 *ov9282 = to_ov9282(sd);
  937. v4l2_async_unregister_subdev(sd);
  938. media_entity_cleanup(&sd->entity);
  939. v4l2_ctrl_handler_free(sd->ctrl_handler);
  940. pm_runtime_disable(&client->dev);
  941. if (!pm_runtime_status_suspended(&client->dev))
  942. ov9282_power_off(&client->dev);
  943. pm_runtime_set_suspended(&client->dev);
  944. mutex_destroy(&ov9282->mutex);
  945. }
  946. static const struct dev_pm_ops ov9282_pm_ops = {
  947. SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
  948. };
  949. static const struct of_device_id ov9282_of_match[] = {
  950. { .compatible = "ovti,ov9282" },
  951. { }
  952. };
  953. MODULE_DEVICE_TABLE(of, ov9282_of_match);
  954. static struct i2c_driver ov9282_driver = {
  955. .probe_new = ov9282_probe,
  956. .remove = ov9282_remove,
  957. .driver = {
  958. .name = "ov9282",
  959. .pm = &ov9282_pm_ops,
  960. .of_match_table = ov9282_of_match,
  961. },
  962. };
  963. module_i2c_driver(ov9282_driver);
  964. MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
  965. MODULE_LICENSE("GPL");