ov2685.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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_pad_config *cfg,
  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_pad_config *cfg,
  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_pad_config *cfg,
  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_pad_config *cfg,
  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_get_sync(&ov2685->client->dev);
  388. if (ret < 0) {
  389. pm_runtime_put_noidle(&client->dev);
  390. goto unlock_and_return;
  391. }
  392. ret = __v4l2_ctrl_handler_setup(&ov2685->ctrl_handler);
  393. if (ret) {
  394. pm_runtime_put(&client->dev);
  395. goto unlock_and_return;
  396. }
  397. ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
  398. OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
  399. if (ret) {
  400. pm_runtime_put(&client->dev);
  401. goto unlock_and_return;
  402. }
  403. } else {
  404. ov2685_write_reg(client, REG_SC_CTRL_MODE,
  405. OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
  406. pm_runtime_put(&ov2685->client->dev);
  407. }
  408. ov2685->streaming = on;
  409. unlock_and_return:
  410. mutex_unlock(&ov2685->mutex);
  411. return ret;
  412. }
  413. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  414. static int ov2685_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  415. {
  416. struct ov2685 *ov2685 = to_ov2685(sd);
  417. struct v4l2_mbus_framefmt *try_fmt;
  418. mutex_lock(&ov2685->mutex);
  419. try_fmt = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  420. /* Initialize try_fmt */
  421. ov2685_fill_fmt(&supported_modes[0], try_fmt);
  422. mutex_unlock(&ov2685->mutex);
  423. return 0;
  424. }
  425. #endif
  426. static int __maybe_unused ov2685_runtime_resume(struct device *dev)
  427. {
  428. struct i2c_client *client = to_i2c_client(dev);
  429. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  430. struct ov2685 *ov2685 = to_ov2685(sd);
  431. return __ov2685_power_on(ov2685);
  432. }
  433. static int __maybe_unused ov2685_runtime_suspend(struct device *dev)
  434. {
  435. struct i2c_client *client = to_i2c_client(dev);
  436. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  437. struct ov2685 *ov2685 = to_ov2685(sd);
  438. __ov2685_power_off(ov2685);
  439. return 0;
  440. }
  441. static const struct dev_pm_ops ov2685_pm_ops = {
  442. SET_RUNTIME_PM_OPS(ov2685_runtime_suspend,
  443. ov2685_runtime_resume, NULL)
  444. };
  445. static int ov2685_set_ctrl(struct v4l2_ctrl *ctrl)
  446. {
  447. struct ov2685 *ov2685 = container_of(ctrl->handler,
  448. struct ov2685, ctrl_handler);
  449. struct i2c_client *client = ov2685->client;
  450. s64 max_expo;
  451. int ret;
  452. /* Propagate change of current control to all related controls */
  453. switch (ctrl->id) {
  454. case V4L2_CID_VBLANK:
  455. /* Update max exposure while meeting expected vblanking */
  456. max_expo = ov2685->cur_mode->height + ctrl->val - 4;
  457. __v4l2_ctrl_modify_range(ov2685->exposure,
  458. ov2685->exposure->minimum, max_expo,
  459. ov2685->exposure->step,
  460. ov2685->exposure->default_value);
  461. break;
  462. }
  463. if (!pm_runtime_get_if_in_use(&client->dev))
  464. return 0;
  465. switch (ctrl->id) {
  466. case V4L2_CID_EXPOSURE:
  467. ret = ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
  468. OV2685_REG_VALUE_24BIT, ctrl->val << 4);
  469. break;
  470. case V4L2_CID_ANALOGUE_GAIN:
  471. ret = ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
  472. OV2685_REG_VALUE_16BIT, ctrl->val);
  473. break;
  474. case V4L2_CID_VBLANK:
  475. ret = ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
  476. OV2685_REG_VALUE_16BIT,
  477. ctrl->val + ov2685->cur_mode->height);
  478. break;
  479. case V4L2_CID_TEST_PATTERN:
  480. ret = ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
  481. OV2685_REG_VALUE_08BIT,
  482. ov2685_test_pattern_val[ctrl->val]);
  483. break;
  484. default:
  485. dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
  486. __func__, ctrl->id, ctrl->val);
  487. ret = -EINVAL;
  488. break;
  489. };
  490. pm_runtime_put(&client->dev);
  491. return ret;
  492. }
  493. static const struct v4l2_subdev_video_ops ov2685_video_ops = {
  494. .s_stream = ov2685_s_stream,
  495. };
  496. static const struct v4l2_subdev_pad_ops ov2685_pad_ops = {
  497. .enum_mbus_code = ov2685_enum_mbus_code,
  498. .enum_frame_size = ov2685_enum_frame_sizes,
  499. .get_fmt = ov2685_get_fmt,
  500. .set_fmt = ov2685_set_fmt,
  501. };
  502. static const struct v4l2_subdev_ops ov2685_subdev_ops = {
  503. .video = &ov2685_video_ops,
  504. .pad = &ov2685_pad_ops,
  505. };
  506. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  507. static const struct v4l2_subdev_internal_ops ov2685_internal_ops = {
  508. .open = ov2685_open,
  509. };
  510. #endif
  511. static const struct v4l2_ctrl_ops ov2685_ctrl_ops = {
  512. .s_ctrl = ov2685_set_ctrl,
  513. };
  514. static int ov2685_initialize_controls(struct ov2685 *ov2685)
  515. {
  516. const struct ov2685_mode *mode;
  517. struct v4l2_ctrl_handler *handler;
  518. struct v4l2_ctrl *ctrl;
  519. u64 exposure_max;
  520. u32 pixel_rate, h_blank;
  521. int ret;
  522. handler = &ov2685->ctrl_handler;
  523. mode = ov2685->cur_mode;
  524. ret = v4l2_ctrl_handler_init(handler, 8);
  525. if (ret)
  526. return ret;
  527. handler->lock = &ov2685->mutex;
  528. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
  529. 0, 0, link_freq_menu_items);
  530. if (ctrl)
  531. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  532. pixel_rate = (link_freq_menu_items[0] * 2 * OV2685_LANES) /
  533. OV2685_BITS_PER_SAMPLE;
  534. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
  535. 0, pixel_rate, 1, pixel_rate);
  536. h_blank = mode->hts_def - mode->width;
  537. ov2685->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
  538. h_blank, h_blank, 1, h_blank);
  539. if (ov2685->hblank)
  540. ov2685->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  541. ov2685->vblank = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  542. V4L2_CID_VBLANK, mode->vts_def - mode->height,
  543. OV2685_VTS_MAX - mode->height, 1,
  544. mode->vts_def - mode->height);
  545. exposure_max = mode->vts_def - 4;
  546. ov2685->exposure = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  547. V4L2_CID_EXPOSURE, OV2685_EXPOSURE_MIN,
  548. exposure_max, OV2685_EXPOSURE_STEP,
  549. mode->exp_def);
  550. ov2685->anal_gain = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
  551. V4L2_CID_ANALOGUE_GAIN, OV2685_GAIN_MIN,
  552. OV2685_GAIN_MAX, OV2685_GAIN_STEP,
  553. OV2685_GAIN_DEFAULT);
  554. ov2685->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  555. &ov2685_ctrl_ops, V4L2_CID_TEST_PATTERN,
  556. ARRAY_SIZE(ov2685_test_pattern_menu) - 1,
  557. 0, 0, ov2685_test_pattern_menu);
  558. if (handler->error) {
  559. ret = handler->error;
  560. dev_err(&ov2685->client->dev,
  561. "Failed to init controls(%d)\n", ret);
  562. goto err_free_handler;
  563. }
  564. ov2685->subdev.ctrl_handler = handler;
  565. return 0;
  566. err_free_handler:
  567. v4l2_ctrl_handler_free(handler);
  568. return ret;
  569. }
  570. static int ov2685_check_sensor_id(struct ov2685 *ov2685,
  571. struct i2c_client *client)
  572. {
  573. struct device *dev = &ov2685->client->dev;
  574. int ret;
  575. u32 id = 0;
  576. ret = ov2685_read_reg(client, OV2685_REG_CHIP_ID,
  577. OV2685_REG_VALUE_16BIT, &id);
  578. if (id != CHIP_ID) {
  579. dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
  580. return ret;
  581. }
  582. dev_info(dev, "Detected OV%04x sensor\n", CHIP_ID);
  583. return 0;
  584. }
  585. static int ov2685_configure_regulators(struct ov2685 *ov2685)
  586. {
  587. int i;
  588. for (i = 0; i < OV2685_NUM_SUPPLIES; i++)
  589. ov2685->supplies[i].supply = ov2685_supply_names[i];
  590. return devm_regulator_bulk_get(&ov2685->client->dev,
  591. OV2685_NUM_SUPPLIES,
  592. ov2685->supplies);
  593. }
  594. static int ov2685_probe(struct i2c_client *client,
  595. const struct i2c_device_id *id)
  596. {
  597. struct device *dev = &client->dev;
  598. struct ov2685 *ov2685;
  599. int ret;
  600. ov2685 = devm_kzalloc(dev, sizeof(*ov2685), GFP_KERNEL);
  601. if (!ov2685)
  602. return -ENOMEM;
  603. ov2685->client = client;
  604. ov2685->cur_mode = &supported_modes[0];
  605. ov2685->xvclk = devm_clk_get(dev, "xvclk");
  606. if (IS_ERR(ov2685->xvclk)) {
  607. dev_err(dev, "Failed to get xvclk\n");
  608. return -EINVAL;
  609. }
  610. ret = clk_set_rate(ov2685->xvclk, OV2685_XVCLK_FREQ);
  611. if (ret < 0) {
  612. dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
  613. return ret;
  614. }
  615. if (clk_get_rate(ov2685->xvclk) != OV2685_XVCLK_FREQ)
  616. dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
  617. ov2685->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  618. if (IS_ERR(ov2685->reset_gpio)) {
  619. dev_err(dev, "Failed to get reset-gpios\n");
  620. return -EINVAL;
  621. }
  622. ret = ov2685_configure_regulators(ov2685);
  623. if (ret) {
  624. dev_err(dev, "Failed to get power regulators\n");
  625. return ret;
  626. }
  627. mutex_init(&ov2685->mutex);
  628. v4l2_i2c_subdev_init(&ov2685->subdev, client, &ov2685_subdev_ops);
  629. ret = ov2685_initialize_controls(ov2685);
  630. if (ret)
  631. goto err_destroy_mutex;
  632. ret = __ov2685_power_on(ov2685);
  633. if (ret)
  634. goto err_free_handler;
  635. ret = ov2685_check_sensor_id(ov2685, client);
  636. if (ret)
  637. goto err_power_off;
  638. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  639. ov2685->subdev.internal_ops = &ov2685_internal_ops;
  640. ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  641. #endif
  642. #if defined(CONFIG_MEDIA_CONTROLLER)
  643. ov2685->pad.flags = MEDIA_PAD_FL_SOURCE;
  644. ov2685->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  645. ret = media_entity_pads_init(&ov2685->subdev.entity, 1, &ov2685->pad);
  646. if (ret < 0)
  647. goto err_power_off;
  648. #endif
  649. ret = v4l2_async_register_subdev(&ov2685->subdev);
  650. if (ret) {
  651. dev_err(dev, "v4l2 async register subdev failed\n");
  652. goto err_clean_entity;
  653. }
  654. pm_runtime_set_active(dev);
  655. pm_runtime_enable(dev);
  656. pm_runtime_idle(dev);
  657. return 0;
  658. err_clean_entity:
  659. #if defined(CONFIG_MEDIA_CONTROLLER)
  660. media_entity_cleanup(&ov2685->subdev.entity);
  661. #endif
  662. err_power_off:
  663. __ov2685_power_off(ov2685);
  664. err_free_handler:
  665. v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
  666. err_destroy_mutex:
  667. mutex_destroy(&ov2685->mutex);
  668. return ret;
  669. }
  670. static int ov2685_remove(struct i2c_client *client)
  671. {
  672. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  673. struct ov2685 *ov2685 = to_ov2685(sd);
  674. v4l2_async_unregister_subdev(sd);
  675. #if defined(CONFIG_MEDIA_CONTROLLER)
  676. media_entity_cleanup(&sd->entity);
  677. #endif
  678. v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
  679. mutex_destroy(&ov2685->mutex);
  680. pm_runtime_disable(&client->dev);
  681. if (!pm_runtime_status_suspended(&client->dev))
  682. __ov2685_power_off(ov2685);
  683. pm_runtime_set_suspended(&client->dev);
  684. return 0;
  685. }
  686. #if IS_ENABLED(CONFIG_OF)
  687. static const struct of_device_id ov2685_of_match[] = {
  688. { .compatible = "ovti,ov2685" },
  689. {},
  690. };
  691. MODULE_DEVICE_TABLE(of, ov2685_of_match);
  692. #endif
  693. static struct i2c_driver ov2685_i2c_driver = {
  694. .driver = {
  695. .name = "ov2685",
  696. .pm = &ov2685_pm_ops,
  697. .of_match_table = of_match_ptr(ov2685_of_match),
  698. },
  699. .probe = &ov2685_probe,
  700. .remove = &ov2685_remove,
  701. };
  702. module_i2c_driver(ov2685_i2c_driver);
  703. MODULE_DESCRIPTION("OmniVision ov2685 sensor driver");
  704. MODULE_LICENSE("GPL v2");