ov2685.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ov2685 driver
  4. *
  5. * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/sysfs.h>
  16. #include <media/media-entity.h>
  17. #include <media/v4l2-async.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-subdev.h>
  20. #define CHIP_ID 0x2685
  21. #define OV2685_REG_CHIP_ID 0x300a
  22. #define OV2685_XVCLK_FREQ 24000000
  23. #define REG_SC_CTRL_MODE 0x0100
  24. #define SC_CTRL_MODE_STANDBY 0x0
  25. #define SC_CTRL_MODE_STREAMING BIT(0)
  26. #define OV2685_REG_EXPOSURE 0x3500
  27. #define OV2685_EXPOSURE_MIN 4
  28. #define OV2685_EXPOSURE_STEP 1
  29. #define OV2685_REG_VTS 0x380e
  30. #define OV2685_VTS_MAX 0x7fff
  31. #define OV2685_REG_GAIN 0x350a
  32. #define OV2685_GAIN_MIN 0
  33. #define OV2685_GAIN_MAX 0x07ff
  34. #define OV2685_GAIN_STEP 0x1
  35. #define OV2685_GAIN_DEFAULT 0x0036
  36. #define OV2685_REG_TEST_PATTERN 0x5080
  37. #define OV2685_TEST_PATTERN_DISABLED 0x00
  38. #define OV2685_TEST_PATTERN_COLOR_BAR 0x80
  39. #define OV2685_TEST_PATTERN_RANDOM 0x81
  40. #define OV2685_TEST_PATTERN_COLOR_BAR_FADE 0x88
  41. #define OV2685_TEST_PATTERN_BW_SQUARE 0x92
  42. #define OV2685_TEST_PATTERN_COLOR_SQUARE 0x82
  43. #define REG_NULL 0xFFFF
  44. #define OV2685_REG_VALUE_08BIT 1
  45. #define OV2685_REG_VALUE_16BIT 2
  46. #define OV2685_REG_VALUE_24BIT 3
  47. #define OV2685_LANES 1
  48. #define OV2685_BITS_PER_SAMPLE 10
  49. static const char * const ov2685_supply_names[] = {
  50. "avdd", /* Analog power */
  51. "dovdd", /* Digital I/O power */
  52. "dvdd", /* Digital core power */
  53. };
  54. #define OV2685_NUM_SUPPLIES ARRAY_SIZE(ov2685_supply_names)
  55. struct regval {
  56. u16 addr;
  57. u8 val;
  58. };
  59. struct ov2685_mode {
  60. u32 width;
  61. u32 height;
  62. u32 exp_def;
  63. u32 hts_def;
  64. u32 vts_def;
  65. const struct regval *reg_list;
  66. };
  67. struct ov2685 {
  68. struct i2c_client *client;
  69. struct clk *xvclk;
  70. struct gpio_desc *reset_gpio;
  71. struct regulator_bulk_data supplies[OV2685_NUM_SUPPLIES];
  72. bool streaming;
  73. struct mutex mutex;
  74. struct v4l2_subdev subdev;
  75. struct media_pad pad;
  76. struct v4l2_ctrl *anal_gain;
  77. struct v4l2_ctrl *exposure;
  78. struct v4l2_ctrl *hblank;
  79. struct v4l2_ctrl *vblank;
  80. struct v4l2_ctrl *test_pattern;
  81. struct v4l2_ctrl_handler ctrl_handler;
  82. const struct ov2685_mode *cur_mode;
  83. };
  84. #define to_ov2685(sd) container_of(sd, struct ov2685, subdev)
  85. /* PLL settings bases on 24M xvclk */
  86. static struct regval ov2685_1600x1200_regs[] = {
  87. {0x0103, 0x01},
  88. {0x0100, 0x00},
  89. {0x3002, 0x00},
  90. {0x3016, 0x1c},
  91. {0x3018, 0x44},
  92. {0x301d, 0xf0},
  93. {0x3020, 0x00},
  94. {0x3082, 0x37},
  95. {0x3083, 0x03},
  96. {0x3084, 0x09},
  97. {0x3085, 0x04},
  98. {0x3086, 0x00},
  99. {0x3087, 0x00},
  100. {0x3501, 0x4e},
  101. {0x3502, 0xe0},
  102. {0x3503, 0x27},
  103. {0x350b, 0x36},
  104. {0x3600, 0xb4},
  105. {0x3603, 0x35},
  106. {0x3604, 0x24},
  107. {0x3605, 0x00},
  108. {0x3620, 0x24},
  109. {0x3621, 0x34},
  110. {0x3622, 0x03},
  111. {0x3628, 0x10},
  112. {0x3705, 0x3c},
  113. {0x370a, 0x21},
  114. {0x370c, 0x50},
  115. {0x370d, 0xc0},
  116. {0x3717, 0x58},
  117. {0x3718, 0x80},
  118. {0x3720, 0x00},
  119. {0x3721, 0x09},
  120. {0x3722, 0x06},
  121. {0x3723, 0x59},
  122. {0x3738, 0x99},
  123. {0x3781, 0x80},
  124. {0x3784, 0x0c},
  125. {0x3789, 0x60},
  126. {0x3800, 0x00},
  127. {0x3801, 0x00},
  128. {0x3802, 0x00},
  129. {0x3803, 0x00},
  130. {0x3804, 0x06},
  131. {0x3805, 0x4f},
  132. {0x3806, 0x04},
  133. {0x3807, 0xbf},
  134. {0x3808, 0x06},
  135. {0x3809, 0x40},
  136. {0x380a, 0x04},
  137. {0x380b, 0xb0},
  138. {0x380c, 0x06},
  139. {0x380d, 0xa4},
  140. {0x380e, 0x05},
  141. {0x380f, 0x0e},
  142. {0x3810, 0x00},
  143. {0x3811, 0x08},
  144. {0x3812, 0x00},
  145. {0x3813, 0x08},
  146. {0x3814, 0x11},
  147. {0x3815, 0x11},
  148. {0x3819, 0x04},
  149. {0x3820, 0xc0},
  150. {0x3821, 0x00},
  151. {0x3a06, 0x01},
  152. {0x3a07, 0x84},
  153. {0x3a08, 0x01},
  154. {0x3a09, 0x43},
  155. {0x3a0a, 0x24},
  156. {0x3a0b, 0x60},
  157. {0x3a0c, 0x28},
  158. {0x3a0d, 0x60},
  159. {0x3a0e, 0x04},
  160. {0x3a0f, 0x8c},
  161. {0x3a10, 0x05},
  162. {0x3a11, 0x0c},
  163. {0x4000, 0x81},
  164. {0x4001, 0x40},
  165. {0x4008, 0x02},
  166. {0x4009, 0x09},
  167. {0x4300, 0x00},
  168. {0x430e, 0x00},
  169. {0x4602, 0x02},
  170. {0x481b, 0x40},
  171. {0x481f, 0x40},
  172. {0x4837, 0x18},
  173. {0x5000, 0x1f},
  174. {0x5001, 0x05},
  175. {0x5002, 0x30},
  176. {0x5003, 0x04},
  177. {0x5004, 0x00},
  178. {0x5005, 0x0c},
  179. {0x5280, 0x15},
  180. {0x5281, 0x06},
  181. {0x5282, 0x06},
  182. {0x5283, 0x08},
  183. {0x5284, 0x1c},
  184. {0x5285, 0x1c},
  185. {0x5286, 0x20},
  186. {0x5287, 0x10},
  187. {REG_NULL, 0x00}
  188. };
  189. #define OV2685_LINK_FREQ_330MHZ 330000000
  190. static const s64 link_freq_menu_items[] = {
  191. OV2685_LINK_FREQ_330MHZ
  192. };
  193. static const char * const ov2685_test_pattern_menu[] = {
  194. "Disabled",
  195. "Color Bar",
  196. "Color Bar FADE",
  197. "Random Data",
  198. "Black White Square",
  199. "Color Square"
  200. };
  201. static const int ov2685_test_pattern_val[] = {
  202. OV2685_TEST_PATTERN_DISABLED,
  203. OV2685_TEST_PATTERN_COLOR_BAR,
  204. OV2685_TEST_PATTERN_COLOR_BAR_FADE,
  205. OV2685_TEST_PATTERN_RANDOM,
  206. OV2685_TEST_PATTERN_BW_SQUARE,
  207. OV2685_TEST_PATTERN_COLOR_SQUARE,
  208. };
  209. static const struct ov2685_mode supported_modes[] = {
  210. {
  211. .width = 1600,
  212. .height = 1200,
  213. .exp_def = 0x04ee,
  214. .hts_def = 0x06a4,
  215. .vts_def = 0x050e,
  216. .reg_list = ov2685_1600x1200_regs,
  217. },
  218. };
  219. /* Write registers up to 4 at a time */
  220. static int ov2685_write_reg(struct i2c_client *client, u16 reg,
  221. u32 len, u32 val)
  222. {
  223. u32 val_i, buf_i;
  224. u8 buf[6];
  225. u8 *val_p;
  226. __be32 val_be;
  227. if (len > 4)
  228. return -EINVAL;
  229. buf[0] = reg >> 8;
  230. buf[1] = reg & 0xff;
  231. val_be = cpu_to_be32(val);
  232. val_p = (u8 *)&val_be;
  233. buf_i = 2;
  234. val_i = 4 - len;
  235. while (val_i < 4)
  236. buf[buf_i++] = val_p[val_i++];
  237. if (i2c_master_send(client, buf, len + 2) != len + 2)
  238. return -EIO;
  239. return 0;
  240. }
  241. static int ov2685_write_array(struct i2c_client *client,
  242. const struct regval *regs)
  243. {
  244. int ret = 0;
  245. u32 i;
  246. for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
  247. ret = ov2685_write_reg(client, regs[i].addr,
  248. OV2685_REG_VALUE_08BIT, regs[i].val);
  249. return ret;
  250. }
  251. /* Read registers up to 4 at a time */
  252. static int ov2685_read_reg(struct i2c_client *client, u16 reg,
  253. u32 len, u32 *val)
  254. {
  255. struct i2c_msg msgs[2];
  256. u8 *data_be_p;
  257. __be32 data_be = 0;
  258. __be16 reg_addr_be = cpu_to_be16(reg);
  259. int ret;
  260. if (len > 4)
  261. return -EINVAL;
  262. data_be_p = (u8 *)&data_be;
  263. /* Write register address */
  264. msgs[0].addr = client->addr;
  265. msgs[0].flags = 0;
  266. msgs[0].len = 2;
  267. msgs[0].buf = (u8 *)&reg_addr_be;
  268. /* Read data from register */
  269. msgs[1].addr = client->addr;
  270. msgs[1].flags = I2C_M_RD;
  271. msgs[1].len = len;
  272. msgs[1].buf = &data_be_p[4 - len];
  273. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  274. if (ret != ARRAY_SIZE(msgs))
  275. return -EIO;
  276. *val = be32_to_cpu(data_be);
  277. return 0;
  278. }
  279. static void ov2685_fill_fmt(const struct ov2685_mode *mode,
  280. struct v4l2_mbus_framefmt *fmt)
  281. {
  282. fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  283. fmt->width = mode->width;
  284. fmt->height = mode->height;
  285. fmt->field = V4L2_FIELD_NONE;
  286. }
  287. static int ov2685_set_fmt(struct v4l2_subdev *sd,
  288. struct v4l2_subdev_state *sd_state,
  289. struct v4l2_subdev_format *fmt)
  290. {
  291. struct ov2685 *ov2685 = to_ov2685(sd);
  292. struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
  293. /* only one mode supported for now */
  294. ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
  295. return 0;
  296. }
  297. static int ov2685_get_fmt(struct v4l2_subdev *sd,
  298. struct v4l2_subdev_state *sd_state,
  299. struct v4l2_subdev_format *fmt)
  300. {
  301. struct ov2685 *ov2685 = to_ov2685(sd);
  302. struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
  303. ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
  304. return 0;
  305. }
  306. static int ov2685_enum_mbus_code(struct v4l2_subdev *sd,
  307. struct v4l2_subdev_state *sd_state,
  308. struct v4l2_subdev_mbus_code_enum *code)
  309. {
  310. if (code->index >= ARRAY_SIZE(supported_modes))
  311. return -EINVAL;
  312. code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  313. return 0;
  314. }
  315. static int ov2685_enum_frame_sizes(struct v4l2_subdev *sd,
  316. struct v4l2_subdev_state *sd_state,
  317. struct v4l2_subdev_frame_size_enum *fse)
  318. {
  319. int index = fse->index;
  320. if (index >= ARRAY_SIZE(supported_modes))
  321. return -EINVAL;
  322. fse->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  323. fse->min_width = supported_modes[index].width;
  324. fse->max_width = supported_modes[index].width;
  325. fse->max_height = supported_modes[index].height;
  326. fse->min_height = supported_modes[index].height;
  327. return 0;
  328. }
  329. /* Calculate the delay in us by clock rate and clock cycles */
  330. static inline u32 ov2685_cal_delay(u32 cycles)
  331. {
  332. return DIV_ROUND_UP(cycles, OV2685_XVCLK_FREQ / 1000 / 1000);
  333. }
  334. static int __ov2685_power_on(struct ov2685 *ov2685)
  335. {
  336. int ret;
  337. u32 delay_us;
  338. struct device *dev = &ov2685->client->dev;
  339. ret = clk_prepare_enable(ov2685->xvclk);
  340. if (ret < 0) {
  341. dev_err(dev, "Failed to enable xvclk\n");
  342. return ret;
  343. }
  344. gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
  345. ret = regulator_bulk_enable(OV2685_NUM_SUPPLIES, ov2685->supplies);
  346. if (ret < 0) {
  347. dev_err(dev, "Failed to enable regulators\n");
  348. goto disable_clk;
  349. }
  350. /* The minimum delay between power supplies and reset rising can be 0 */
  351. gpiod_set_value_cansleep(ov2685->reset_gpio, 0);
  352. /* 8192 xvclk cycles prior to the first SCCB transaction */
  353. delay_us = ov2685_cal_delay(8192);
  354. usleep_range(delay_us, delay_us * 2);
  355. /* HACK: ov2685 would output messy data after reset(R0103),
  356. * writing register before .s_stream() as a workaround
  357. */
  358. ret = ov2685_write_array(ov2685->client, ov2685->cur_mode->reg_list);
  359. if (ret)
  360. goto disable_supplies;
  361. return 0;
  362. disable_supplies:
  363. regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
  364. disable_clk:
  365. clk_disable_unprepare(ov2685->xvclk);
  366. return ret;
  367. }
  368. static void __ov2685_power_off(struct ov2685 *ov2685)
  369. {
  370. /* 512 xvclk cycles after the last SCCB transaction or MIPI frame end */
  371. u32 delay_us = ov2685_cal_delay(512);
  372. usleep_range(delay_us, delay_us * 2);
  373. clk_disable_unprepare(ov2685->xvclk);
  374. gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
  375. regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
  376. }
  377. static int ov2685_s_stream(struct v4l2_subdev *sd, int on)
  378. {
  379. struct ov2685 *ov2685 = to_ov2685(sd);
  380. struct i2c_client *client = ov2685->client;
  381. int ret = 0;
  382. mutex_lock(&ov2685->mutex);
  383. on = !!on;
  384. if (on == ov2685->streaming)
  385. goto unlock_and_return;
  386. if (on) {
  387. ret = pm_runtime_resume_and_get(&ov2685->client->dev);
  388. if (ret < 0)
  389. goto unlock_and_return;
  390. ret = __v4l2_ctrl_handler_setup(&ov2685->ctrl_handler);
  391. if (ret) {
  392. pm_runtime_put(&client->dev);
  393. goto unlock_and_return;
  394. }
  395. ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
  396. OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
  397. if (ret) {
  398. pm_runtime_put(&client->dev);
  399. goto unlock_and_return;
  400. }
  401. } else {
  402. ov2685_write_reg(client, REG_SC_CTRL_MODE,
  403. OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
  404. pm_runtime_put(&ov2685->client->dev);
  405. }
  406. ov2685->streaming = on;
  407. unlock_and_return:
  408. mutex_unlock(&ov2685->mutex);
  409. return ret;
  410. }
  411. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  412. static int ov2685_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  413. {
  414. struct ov2685 *ov2685 = to_ov2685(sd);
  415. struct v4l2_mbus_framefmt *try_fmt;
  416. mutex_lock(&ov2685->mutex);
  417. try_fmt = v4l2_subdev_get_try_format(sd, fh->state, 0);
  418. /* Initialize try_fmt */
  419. ov2685_fill_fmt(&supported_modes[0], try_fmt);
  420. mutex_unlock(&ov2685->mutex);
  421. return 0;
  422. }
  423. #endif
  424. static int __maybe_unused ov2685_runtime_resume(struct device *dev)
  425. {
  426. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  427. struct ov2685 *ov2685 = to_ov2685(sd);
  428. return __ov2685_power_on(ov2685);
  429. }
  430. static int __maybe_unused ov2685_runtime_suspend(struct device *dev)
  431. {
  432. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  433. struct ov2685 *ov2685 = to_ov2685(sd);
  434. __ov2685_power_off(ov2685);
  435. return 0;
  436. }
  437. static const struct dev_pm_ops ov2685_pm_ops = {
  438. SET_RUNTIME_PM_OPS(ov2685_runtime_suspend,
  439. ov2685_runtime_resume, NULL)
  440. };
  441. static int ov2685_set_ctrl(struct v4l2_ctrl *ctrl)
  442. {
  443. struct ov2685 *ov2685 = container_of(ctrl->handler,
  444. struct ov2685, ctrl_handler);
  445. struct i2c_client *client = ov2685->client;
  446. s64 max_expo;
  447. int ret;
  448. /* Propagate change of current control to all related controls */
  449. switch (ctrl->id) {
  450. case V4L2_CID_VBLANK:
  451. /* Update max exposure while meeting expected vblanking */
  452. max_expo = ov2685->cur_mode->height + ctrl->val - 4;
  453. __v4l2_ctrl_modify_range(ov2685->exposure,
  454. ov2685->exposure->minimum, max_expo,
  455. ov2685->exposure->step,
  456. ov2685->exposure->default_value);
  457. break;
  458. }
  459. if (!pm_runtime_get_if_in_use(&client->dev))
  460. return 0;
  461. switch (ctrl->id) {
  462. case V4L2_CID_EXPOSURE:
  463. ret = ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
  464. OV2685_REG_VALUE_24BIT, ctrl->val << 4);
  465. break;
  466. case V4L2_CID_ANALOGUE_GAIN:
  467. ret = ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
  468. OV2685_REG_VALUE_16BIT, ctrl->val);
  469. break;
  470. case V4L2_CID_VBLANK:
  471. ret = ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
  472. OV2685_REG_VALUE_16BIT,
  473. ctrl->val + ov2685->cur_mode->height);
  474. break;
  475. case V4L2_CID_TEST_PATTERN:
  476. ret = ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
  477. OV2685_REG_VALUE_08BIT,
  478. ov2685_test_pattern_val[ctrl->val]);
  479. break;
  480. default:
  481. dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
  482. __func__, ctrl->id, ctrl->val);
  483. ret = -EINVAL;
  484. break;
  485. }
  486. pm_runtime_put(&client->dev);
  487. return ret;
  488. }
  489. static const struct v4l2_subdev_video_ops ov2685_video_ops = {
  490. .s_stream = ov2685_s_stream,
  491. };
  492. static const struct v4l2_subdev_pad_ops ov2685_pad_ops = {
  493. .enum_mbus_code = ov2685_enum_mbus_code,
  494. .enum_frame_size = ov2685_enum_frame_sizes,
  495. .get_fmt = ov2685_get_fmt,
  496. .set_fmt = ov2685_set_fmt,
  497. };
  498. static const struct v4l2_subdev_ops ov2685_subdev_ops = {
  499. .video = &ov2685_video_ops,
  500. .pad = &ov2685_pad_ops,
  501. };
  502. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  503. static const struct v4l2_subdev_internal_ops ov2685_internal_ops = {
  504. .open = ov2685_open,
  505. };
  506. #endif
  507. static const struct v4l2_ctrl_ops ov2685_ctrl_ops = {
  508. .s_ctrl = ov2685_set_ctrl,
  509. };
  510. static int ov2685_initialize_controls(struct ov2685 *ov2685)
  511. {
  512. const struct ov2685_mode *mode;
  513. struct v4l2_ctrl_handler *handler;
  514. struct v4l2_ctrl *ctrl;
  515. u64 exposure_max;
  516. u32 pixel_rate, h_blank;
  517. int ret;
  518. handler = &ov2685->ctrl_handler;
  519. mode = ov2685->cur_mode;
  520. ret = v4l2_ctrl_handler_init(handler, 8);
  521. if (ret)
  522. return ret;
  523. handler->lock = &ov2685->mutex;
  524. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
  525. 0, 0, link_freq_menu_items);
  526. if (ctrl)
  527. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  528. pixel_rate = (link_freq_menu_items[0] * 2 * OV2685_LANES) /
  529. OV2685_BITS_PER_SAMPLE;
  530. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
  531. 0, pixel_rate, 1, pixel_rate);
  532. h_blank = mode->hts_def - mode->width;
  533. ov2685->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
  534. h_blank, h_blank, 1, h_blank);
  535. if (ov2685->hblank)
  536. ov2685->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  537. ov2685->vblank = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  538. V4L2_CID_VBLANK, mode->vts_def - mode->height,
  539. OV2685_VTS_MAX - mode->height, 1,
  540. mode->vts_def - mode->height);
  541. exposure_max = mode->vts_def - 4;
  542. ov2685->exposure = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  543. V4L2_CID_EXPOSURE, OV2685_EXPOSURE_MIN,
  544. exposure_max, OV2685_EXPOSURE_STEP,
  545. mode->exp_def);
  546. ov2685->anal_gain = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  547. V4L2_CID_ANALOGUE_GAIN, OV2685_GAIN_MIN,
  548. OV2685_GAIN_MAX, OV2685_GAIN_STEP,
  549. OV2685_GAIN_DEFAULT);
  550. ov2685->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  551. &ov2685_ctrl_ops, V4L2_CID_TEST_PATTERN,
  552. ARRAY_SIZE(ov2685_test_pattern_menu) - 1,
  553. 0, 0, ov2685_test_pattern_menu);
  554. if (handler->error) {
  555. ret = handler->error;
  556. dev_err(&ov2685->client->dev,
  557. "Failed to init controls(%d)\n", ret);
  558. goto err_free_handler;
  559. }
  560. ov2685->subdev.ctrl_handler = handler;
  561. return 0;
  562. err_free_handler:
  563. v4l2_ctrl_handler_free(handler);
  564. return ret;
  565. }
  566. static int ov2685_check_sensor_id(struct ov2685 *ov2685,
  567. struct i2c_client *client)
  568. {
  569. struct device *dev = &ov2685->client->dev;
  570. int ret;
  571. u32 id = 0;
  572. ret = ov2685_read_reg(client, OV2685_REG_CHIP_ID,
  573. OV2685_REG_VALUE_16BIT, &id);
  574. if (id != CHIP_ID) {
  575. dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
  576. return ret;
  577. }
  578. dev_info(dev, "Detected OV%04x sensor\n", CHIP_ID);
  579. return 0;
  580. }
  581. static int ov2685_configure_regulators(struct ov2685 *ov2685)
  582. {
  583. int i;
  584. for (i = 0; i < OV2685_NUM_SUPPLIES; i++)
  585. ov2685->supplies[i].supply = ov2685_supply_names[i];
  586. return devm_regulator_bulk_get(&ov2685->client->dev,
  587. OV2685_NUM_SUPPLIES,
  588. ov2685->supplies);
  589. }
  590. static int ov2685_probe(struct i2c_client *client,
  591. const struct i2c_device_id *id)
  592. {
  593. struct device *dev = &client->dev;
  594. struct ov2685 *ov2685;
  595. int ret;
  596. ov2685 = devm_kzalloc(dev, sizeof(*ov2685), GFP_KERNEL);
  597. if (!ov2685)
  598. return -ENOMEM;
  599. ov2685->client = client;
  600. ov2685->cur_mode = &supported_modes[0];
  601. ov2685->xvclk = devm_clk_get(dev, "xvclk");
  602. if (IS_ERR(ov2685->xvclk)) {
  603. dev_err(dev, "Failed to get xvclk\n");
  604. return -EINVAL;
  605. }
  606. ret = clk_set_rate(ov2685->xvclk, OV2685_XVCLK_FREQ);
  607. if (ret < 0) {
  608. dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
  609. return ret;
  610. }
  611. if (clk_get_rate(ov2685->xvclk) != OV2685_XVCLK_FREQ)
  612. dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
  613. ov2685->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  614. if (IS_ERR(ov2685->reset_gpio)) {
  615. dev_err(dev, "Failed to get reset-gpios\n");
  616. return -EINVAL;
  617. }
  618. ret = ov2685_configure_regulators(ov2685);
  619. if (ret) {
  620. dev_err(dev, "Failed to get power regulators\n");
  621. return ret;
  622. }
  623. mutex_init(&ov2685->mutex);
  624. v4l2_i2c_subdev_init(&ov2685->subdev, client, &ov2685_subdev_ops);
  625. ret = ov2685_initialize_controls(ov2685);
  626. if (ret)
  627. goto err_destroy_mutex;
  628. ret = __ov2685_power_on(ov2685);
  629. if (ret)
  630. goto err_free_handler;
  631. ret = ov2685_check_sensor_id(ov2685, client);
  632. if (ret)
  633. goto err_power_off;
  634. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  635. ov2685->subdev.internal_ops = &ov2685_internal_ops;
  636. ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  637. #endif
  638. #if defined(CONFIG_MEDIA_CONTROLLER)
  639. ov2685->pad.flags = MEDIA_PAD_FL_SOURCE;
  640. ov2685->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  641. ret = media_entity_pads_init(&ov2685->subdev.entity, 1, &ov2685->pad);
  642. if (ret < 0)
  643. goto err_power_off;
  644. #endif
  645. ret = v4l2_async_register_subdev(&ov2685->subdev);
  646. if (ret) {
  647. dev_err(dev, "v4l2 async register subdev failed\n");
  648. goto err_clean_entity;
  649. }
  650. pm_runtime_set_active(dev);
  651. pm_runtime_enable(dev);
  652. pm_runtime_idle(dev);
  653. return 0;
  654. err_clean_entity:
  655. #if defined(CONFIG_MEDIA_CONTROLLER)
  656. media_entity_cleanup(&ov2685->subdev.entity);
  657. #endif
  658. err_power_off:
  659. __ov2685_power_off(ov2685);
  660. err_free_handler:
  661. v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
  662. err_destroy_mutex:
  663. mutex_destroy(&ov2685->mutex);
  664. return ret;
  665. }
  666. static void ov2685_remove(struct i2c_client *client)
  667. {
  668. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  669. struct ov2685 *ov2685 = to_ov2685(sd);
  670. v4l2_async_unregister_subdev(sd);
  671. #if defined(CONFIG_MEDIA_CONTROLLER)
  672. media_entity_cleanup(&sd->entity);
  673. #endif
  674. v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
  675. mutex_destroy(&ov2685->mutex);
  676. pm_runtime_disable(&client->dev);
  677. if (!pm_runtime_status_suspended(&client->dev))
  678. __ov2685_power_off(ov2685);
  679. pm_runtime_set_suspended(&client->dev);
  680. }
  681. #if IS_ENABLED(CONFIG_OF)
  682. static const struct of_device_id ov2685_of_match[] = {
  683. { .compatible = "ovti,ov2685" },
  684. {},
  685. };
  686. MODULE_DEVICE_TABLE(of, ov2685_of_match);
  687. #endif
  688. static struct i2c_driver ov2685_i2c_driver = {
  689. .driver = {
  690. .name = "ov2685",
  691. .pm = &ov2685_pm_ops,
  692. .of_match_table = of_match_ptr(ov2685_of_match),
  693. },
  694. .probe = &ov2685_probe,
  695. .remove = &ov2685_remove,
  696. };
  697. module_i2c_driver(ov2685_i2c_driver);
  698. MODULE_DESCRIPTION("OmniVision ov2685 sensor driver");
  699. MODULE_LICENSE("GPL v2");