ov2680.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Omnivision OV2680 CMOS Image Sensor driver
  4. *
  5. * Copyright (C) 2018 Linaro Ltd
  6. *
  7. * Based on OV5640 Sensor Driver
  8. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  9. * Copyright (C) 2014-2017 Mentor Graphics Inc.
  10. *
  11. */
  12. #include <asm/unaligned.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-subdev.h>
  25. #define OV2680_XVCLK_VALUE 24000000
  26. #define OV2680_CHIP_ID 0x2680
  27. #define OV2680_REG_STREAM_CTRL 0x0100
  28. #define OV2680_REG_SOFT_RESET 0x0103
  29. #define OV2680_REG_CHIP_ID_HIGH 0x300a
  30. #define OV2680_REG_CHIP_ID_LOW 0x300b
  31. #define OV2680_REG_R_MANUAL 0x3503
  32. #define OV2680_REG_GAIN_PK 0x350a
  33. #define OV2680_REG_EXPOSURE_PK_HIGH 0x3500
  34. #define OV2680_REG_TIMING_HTS 0x380c
  35. #define OV2680_REG_TIMING_VTS 0x380e
  36. #define OV2680_REG_FORMAT1 0x3820
  37. #define OV2680_REG_FORMAT2 0x3821
  38. #define OV2680_REG_ISP_CTRL00 0x5080
  39. #define OV2680_FRAME_RATE 30
  40. #define OV2680_REG_VALUE_8BIT 1
  41. #define OV2680_REG_VALUE_16BIT 2
  42. #define OV2680_REG_VALUE_24BIT 3
  43. #define OV2680_WIDTH_MAX 1600
  44. #define OV2680_HEIGHT_MAX 1200
  45. #define OV2680_DEFAULT_WIDTH 800
  46. #define OV2680_DEFAULT_HEIGHT 600
  47. enum ov2680_mode_id {
  48. OV2680_MODE_QUXGA_800_600,
  49. OV2680_MODE_720P_1280_720,
  50. OV2680_MODE_UXGA_1600_1200,
  51. OV2680_MODE_MAX,
  52. };
  53. struct reg_value {
  54. u16 reg_addr;
  55. u8 val;
  56. };
  57. static const char * const ov2680_supply_name[] = {
  58. "DOVDD",
  59. "DVDD",
  60. "AVDD",
  61. };
  62. #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
  63. struct ov2680_mode_info {
  64. const char *name;
  65. enum ov2680_mode_id id;
  66. u32 width;
  67. u32 height;
  68. const struct reg_value *reg_data;
  69. u32 reg_data_size;
  70. };
  71. struct ov2680_ctrls {
  72. struct v4l2_ctrl_handler handler;
  73. struct v4l2_ctrl *exposure;
  74. struct v4l2_ctrl *gain;
  75. struct v4l2_ctrl *hflip;
  76. struct v4l2_ctrl *vflip;
  77. struct v4l2_ctrl *test_pattern;
  78. };
  79. struct ov2680_dev {
  80. struct i2c_client *i2c_client;
  81. struct v4l2_subdev sd;
  82. struct media_pad pad;
  83. struct clk *xvclk;
  84. u32 xvclk_freq;
  85. struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
  86. struct gpio_desc *reset_gpio;
  87. struct mutex lock; /* protect members */
  88. bool mode_pending_changes;
  89. bool is_enabled;
  90. bool is_streaming;
  91. struct ov2680_ctrls ctrls;
  92. struct v4l2_mbus_framefmt fmt;
  93. struct v4l2_fract frame_interval;
  94. const struct ov2680_mode_info *current_mode;
  95. };
  96. static const char * const test_pattern_menu[] = {
  97. "Disabled",
  98. "Color Bars",
  99. "Random Data",
  100. "Square",
  101. "Black Image",
  102. };
  103. static const int ov2680_hv_flip_bayer_order[] = {
  104. MEDIA_BUS_FMT_SBGGR10_1X10,
  105. MEDIA_BUS_FMT_SGRBG10_1X10,
  106. MEDIA_BUS_FMT_SGBRG10_1X10,
  107. MEDIA_BUS_FMT_SRGGB10_1X10,
  108. };
  109. static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
  110. {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
  111. {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
  112. {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
  113. {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
  114. {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
  115. {0x3503, 0x03},
  116. };
  117. static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
  118. {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
  119. {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
  120. {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
  121. {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
  122. };
  123. static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
  124. {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
  125. {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
  126. {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
  127. {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
  128. {0x4008, 0x00}, {0x4837, 0x18}
  129. };
  130. static const struct ov2680_mode_info ov2680_mode_init_data = {
  131. "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
  132. ov2680_setting_30fps_QUXGA_800_600,
  133. ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
  134. };
  135. static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
  136. {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
  137. 800, 600, ov2680_setting_30fps_QUXGA_800_600,
  138. ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
  139. {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
  140. 1280, 720, ov2680_setting_30fps_720P_1280_720,
  141. ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
  142. {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
  143. 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
  144. ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
  145. };
  146. static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
  147. {
  148. return container_of(sd, struct ov2680_dev, sd);
  149. }
  150. static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
  151. {
  152. return &sensor->i2c_client->dev;
  153. }
  154. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  155. {
  156. return &container_of(ctrl->handler, struct ov2680_dev,
  157. ctrls.handler)->sd;
  158. }
  159. static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
  160. unsigned int len, u32 val)
  161. {
  162. struct i2c_client *client = sensor->i2c_client;
  163. u8 buf[6];
  164. int ret;
  165. if (len > 4)
  166. return -EINVAL;
  167. put_unaligned_be16(reg, buf);
  168. put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  169. ret = i2c_master_send(client, buf, len + 2);
  170. if (ret != len + 2) {
  171. dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
  172. return -EIO;
  173. }
  174. return 0;
  175. }
  176. #define ov2680_write_reg(s, r, v) \
  177. __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
  178. #define ov2680_write_reg16(s, r, v) \
  179. __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
  180. #define ov2680_write_reg24(s, r, v) \
  181. __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
  182. static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
  183. unsigned int len, u32 *val)
  184. {
  185. struct i2c_client *client = sensor->i2c_client;
  186. struct i2c_msg msgs[2];
  187. u8 addr_buf[2] = { reg >> 8, reg & 0xff };
  188. u8 data_buf[4] = { 0, };
  189. int ret;
  190. if (len > 4)
  191. return -EINVAL;
  192. msgs[0].addr = client->addr;
  193. msgs[0].flags = 0;
  194. msgs[0].len = ARRAY_SIZE(addr_buf);
  195. msgs[0].buf = addr_buf;
  196. msgs[1].addr = client->addr;
  197. msgs[1].flags = I2C_M_RD;
  198. msgs[1].len = len;
  199. msgs[1].buf = &data_buf[4 - len];
  200. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  201. if (ret != ARRAY_SIZE(msgs)) {
  202. dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
  203. return -EIO;
  204. }
  205. *val = get_unaligned_be32(data_buf);
  206. return 0;
  207. }
  208. #define ov2680_read_reg(s, r, v) \
  209. __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
  210. #define ov2680_read_reg16(s, r, v) \
  211. __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
  212. #define ov2680_read_reg24(s, r, v) \
  213. __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
  214. static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
  215. {
  216. u32 readval;
  217. int ret;
  218. ret = ov2680_read_reg(sensor, reg, &readval);
  219. if (ret < 0)
  220. return ret;
  221. readval &= ~mask;
  222. val &= mask;
  223. val |= readval;
  224. return ov2680_write_reg(sensor, reg, val);
  225. }
  226. static int ov2680_load_regs(struct ov2680_dev *sensor,
  227. const struct ov2680_mode_info *mode)
  228. {
  229. const struct reg_value *regs = mode->reg_data;
  230. unsigned int i;
  231. int ret = 0;
  232. u16 reg_addr;
  233. u8 val;
  234. for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
  235. reg_addr = regs->reg_addr;
  236. val = regs->val;
  237. ret = ov2680_write_reg(sensor, reg_addr, val);
  238. if (ret)
  239. break;
  240. }
  241. return ret;
  242. }
  243. static void ov2680_power_up(struct ov2680_dev *sensor)
  244. {
  245. if (!sensor->reset_gpio)
  246. return;
  247. gpiod_set_value(sensor->reset_gpio, 0);
  248. usleep_range(5000, 10000);
  249. }
  250. static void ov2680_power_down(struct ov2680_dev *sensor)
  251. {
  252. if (!sensor->reset_gpio)
  253. return;
  254. gpiod_set_value(sensor->reset_gpio, 1);
  255. usleep_range(5000, 10000);
  256. }
  257. static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
  258. struct v4l2_mbus_framefmt *fmt)
  259. {
  260. int hv_flip = 0;
  261. if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
  262. hv_flip += 1;
  263. if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
  264. hv_flip += 2;
  265. fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
  266. }
  267. static void ov2680_fill_format(struct ov2680_dev *sensor,
  268. struct v4l2_mbus_framefmt *fmt,
  269. unsigned int width, unsigned int height)
  270. {
  271. memset(fmt, 0, sizeof(*fmt));
  272. fmt->width = width;
  273. fmt->height = height;
  274. fmt->field = V4L2_FIELD_NONE;
  275. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  276. ov2680_set_bayer_order(sensor, fmt);
  277. }
  278. static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
  279. {
  280. int ret;
  281. if (sensor->is_streaming)
  282. return -EBUSY;
  283. ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1,
  284. BIT(2), val ? BIT(2) : 0);
  285. if (ret < 0)
  286. return ret;
  287. ov2680_set_bayer_order(sensor, &sensor->fmt);
  288. return 0;
  289. }
  290. static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
  291. {
  292. int ret;
  293. if (sensor->is_streaming)
  294. return -EBUSY;
  295. ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2,
  296. BIT(2), val ? BIT(2) : 0);
  297. if (ret < 0)
  298. return ret;
  299. ov2680_set_bayer_order(sensor, &sensor->fmt);
  300. return 0;
  301. }
  302. static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
  303. {
  304. int ret;
  305. if (!value)
  306. return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
  307. ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
  308. if (ret < 0)
  309. return ret;
  310. ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
  311. if (ret < 0)
  312. return ret;
  313. return 0;
  314. }
  315. static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
  316. {
  317. return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
  318. }
  319. static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
  320. {
  321. return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
  322. exp << 4);
  323. }
  324. static int ov2680_stream_enable(struct ov2680_dev *sensor)
  325. {
  326. return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
  327. }
  328. static int ov2680_stream_disable(struct ov2680_dev *sensor)
  329. {
  330. return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
  331. }
  332. static int ov2680_mode_set(struct ov2680_dev *sensor)
  333. {
  334. int ret;
  335. ret = ov2680_load_regs(sensor, sensor->current_mode);
  336. if (ret < 0)
  337. return ret;
  338. /* Restore value of all ctrls */
  339. ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
  340. if (ret < 0)
  341. return ret;
  342. sensor->mode_pending_changes = false;
  343. return 0;
  344. }
  345. static int ov2680_mode_restore(struct ov2680_dev *sensor)
  346. {
  347. int ret;
  348. ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
  349. if (ret < 0)
  350. return ret;
  351. return ov2680_mode_set(sensor);
  352. }
  353. static int ov2680_power_off(struct ov2680_dev *sensor)
  354. {
  355. if (!sensor->is_enabled)
  356. return 0;
  357. clk_disable_unprepare(sensor->xvclk);
  358. ov2680_power_down(sensor);
  359. regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
  360. sensor->is_enabled = false;
  361. return 0;
  362. }
  363. static int ov2680_power_on(struct ov2680_dev *sensor)
  364. {
  365. struct device *dev = ov2680_to_dev(sensor);
  366. int ret;
  367. if (sensor->is_enabled)
  368. return 0;
  369. ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
  370. if (ret < 0) {
  371. dev_err(dev, "failed to enable regulators: %d\n", ret);
  372. return ret;
  373. }
  374. if (!sensor->reset_gpio) {
  375. ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
  376. if (ret != 0) {
  377. dev_err(dev, "sensor soft reset failed\n");
  378. goto err_disable_regulators;
  379. }
  380. usleep_range(1000, 2000);
  381. } else {
  382. ov2680_power_down(sensor);
  383. ov2680_power_up(sensor);
  384. }
  385. ret = clk_prepare_enable(sensor->xvclk);
  386. if (ret < 0)
  387. goto err_disable_regulators;
  388. sensor->is_enabled = true;
  389. /* Set clock lane into LP-11 state */
  390. ov2680_stream_enable(sensor);
  391. usleep_range(1000, 2000);
  392. ov2680_stream_disable(sensor);
  393. return 0;
  394. err_disable_regulators:
  395. regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
  396. return ret;
  397. }
  398. static int ov2680_s_power(struct v4l2_subdev *sd, int on)
  399. {
  400. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  401. int ret = 0;
  402. mutex_lock(&sensor->lock);
  403. if (on)
  404. ret = ov2680_power_on(sensor);
  405. else
  406. ret = ov2680_power_off(sensor);
  407. if (on && ret == 0)
  408. ret = ov2680_mode_restore(sensor);
  409. mutex_unlock(&sensor->lock);
  410. return ret;
  411. }
  412. static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
  413. struct v4l2_subdev_frame_interval *fi)
  414. {
  415. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  416. mutex_lock(&sensor->lock);
  417. fi->interval = sensor->frame_interval;
  418. mutex_unlock(&sensor->lock);
  419. return 0;
  420. }
  421. static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
  422. {
  423. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  424. int ret = 0;
  425. mutex_lock(&sensor->lock);
  426. if (sensor->is_streaming == !!enable)
  427. goto unlock;
  428. if (enable && sensor->mode_pending_changes) {
  429. ret = ov2680_mode_set(sensor);
  430. if (ret < 0)
  431. goto unlock;
  432. }
  433. if (enable)
  434. ret = ov2680_stream_enable(sensor);
  435. else
  436. ret = ov2680_stream_disable(sensor);
  437. sensor->is_streaming = !!enable;
  438. unlock:
  439. mutex_unlock(&sensor->lock);
  440. return ret;
  441. }
  442. static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
  443. struct v4l2_subdev_state *sd_state,
  444. struct v4l2_subdev_mbus_code_enum *code)
  445. {
  446. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  447. if (code->pad != 0 || code->index != 0)
  448. return -EINVAL;
  449. code->code = sensor->fmt.code;
  450. return 0;
  451. }
  452. static int ov2680_get_fmt(struct v4l2_subdev *sd,
  453. struct v4l2_subdev_state *sd_state,
  454. struct v4l2_subdev_format *format)
  455. {
  456. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  457. struct v4l2_mbus_framefmt *fmt = NULL;
  458. if (format->pad != 0)
  459. return -EINVAL;
  460. mutex_lock(&sensor->lock);
  461. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  462. fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
  463. format->pad);
  464. } else {
  465. fmt = &sensor->fmt;
  466. }
  467. format->format = *fmt;
  468. mutex_unlock(&sensor->lock);
  469. return 0;
  470. }
  471. static int ov2680_set_fmt(struct v4l2_subdev *sd,
  472. struct v4l2_subdev_state *sd_state,
  473. struct v4l2_subdev_format *format)
  474. {
  475. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  476. struct v4l2_mbus_framefmt *try_fmt;
  477. const struct ov2680_mode_info *mode;
  478. int ret = 0;
  479. if (format->pad != 0)
  480. return -EINVAL;
  481. mode = v4l2_find_nearest_size(ov2680_mode_data,
  482. ARRAY_SIZE(ov2680_mode_data),
  483. width, height,
  484. format->format.width,
  485. format->format.height);
  486. if (!mode)
  487. return -EINVAL;
  488. ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
  489. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  490. try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
  491. *try_fmt = format->format;
  492. return 0;
  493. }
  494. mutex_lock(&sensor->lock);
  495. if (sensor->is_streaming) {
  496. ret = -EBUSY;
  497. goto unlock;
  498. }
  499. sensor->current_mode = mode;
  500. sensor->fmt = format->format;
  501. sensor->mode_pending_changes = true;
  502. unlock:
  503. mutex_unlock(&sensor->lock);
  504. return ret;
  505. }
  506. static int ov2680_init_cfg(struct v4l2_subdev *sd,
  507. struct v4l2_subdev_state *sd_state)
  508. {
  509. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  510. ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
  511. OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
  512. return 0;
  513. }
  514. static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
  515. struct v4l2_subdev_state *sd_state,
  516. struct v4l2_subdev_frame_size_enum *fse)
  517. {
  518. int index = fse->index;
  519. if (index >= OV2680_MODE_MAX || index < 0)
  520. return -EINVAL;
  521. fse->min_width = ov2680_mode_data[index].width;
  522. fse->min_height = ov2680_mode_data[index].height;
  523. fse->max_width = ov2680_mode_data[index].width;
  524. fse->max_height = ov2680_mode_data[index].height;
  525. return 0;
  526. }
  527. static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
  528. struct v4l2_subdev_state *sd_state,
  529. struct v4l2_subdev_frame_interval_enum *fie)
  530. {
  531. struct v4l2_fract tpf;
  532. if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
  533. fie->height > OV2680_HEIGHT_MAX ||
  534. fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
  535. return -EINVAL;
  536. tpf.denominator = OV2680_FRAME_RATE;
  537. tpf.numerator = 1;
  538. fie->interval = tpf;
  539. return 0;
  540. }
  541. static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
  542. {
  543. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  544. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  545. if (!sensor->is_enabled)
  546. return 0;
  547. switch (ctrl->id) {
  548. case V4L2_CID_GAIN:
  549. return ov2680_gain_set(sensor, ctrl->val);
  550. case V4L2_CID_EXPOSURE:
  551. return ov2680_exposure_set(sensor, ctrl->val);
  552. case V4L2_CID_VFLIP:
  553. return ov2680_set_vflip(sensor, ctrl->val);
  554. case V4L2_CID_HFLIP:
  555. return ov2680_set_hflip(sensor, ctrl->val);
  556. case V4L2_CID_TEST_PATTERN:
  557. return ov2680_test_pattern_set(sensor, ctrl->val);
  558. default:
  559. break;
  560. }
  561. return -EINVAL;
  562. }
  563. static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
  564. .s_ctrl = ov2680_s_ctrl,
  565. };
  566. static const struct v4l2_subdev_core_ops ov2680_core_ops = {
  567. .s_power = ov2680_s_power,
  568. };
  569. static const struct v4l2_subdev_video_ops ov2680_video_ops = {
  570. .g_frame_interval = ov2680_s_g_frame_interval,
  571. .s_frame_interval = ov2680_s_g_frame_interval,
  572. .s_stream = ov2680_s_stream,
  573. };
  574. static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
  575. .init_cfg = ov2680_init_cfg,
  576. .enum_mbus_code = ov2680_enum_mbus_code,
  577. .get_fmt = ov2680_get_fmt,
  578. .set_fmt = ov2680_set_fmt,
  579. .enum_frame_size = ov2680_enum_frame_size,
  580. .enum_frame_interval = ov2680_enum_frame_interval,
  581. };
  582. static const struct v4l2_subdev_ops ov2680_subdev_ops = {
  583. .core = &ov2680_core_ops,
  584. .video = &ov2680_video_ops,
  585. .pad = &ov2680_pad_ops,
  586. };
  587. static int ov2680_mode_init(struct ov2680_dev *sensor)
  588. {
  589. const struct ov2680_mode_info *init_mode;
  590. /* set initial mode */
  591. ov2680_fill_format(sensor, &sensor->fmt,
  592. OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
  593. sensor->frame_interval.denominator = OV2680_FRAME_RATE;
  594. sensor->frame_interval.numerator = 1;
  595. init_mode = &ov2680_mode_init_data;
  596. sensor->current_mode = init_mode;
  597. sensor->mode_pending_changes = true;
  598. return 0;
  599. }
  600. static int ov2680_v4l2_register(struct ov2680_dev *sensor)
  601. {
  602. const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
  603. struct ov2680_ctrls *ctrls = &sensor->ctrls;
  604. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  605. int ret = 0;
  606. v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
  607. &ov2680_subdev_ops);
  608. sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  609. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  610. sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  611. ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
  612. if (ret < 0)
  613. return ret;
  614. v4l2_ctrl_handler_init(hdl, 5);
  615. hdl->lock = &sensor->lock;
  616. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  617. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  618. ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
  619. &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
  620. ARRAY_SIZE(test_pattern_menu) - 1,
  621. 0, 0, test_pattern_menu);
  622. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  623. 0, 32767, 1, 0);
  624. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
  625. if (hdl->error) {
  626. ret = hdl->error;
  627. goto cleanup_entity;
  628. }
  629. ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  630. ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  631. sensor->sd.ctrl_handler = hdl;
  632. ret = v4l2_async_register_subdev(&sensor->sd);
  633. if (ret < 0)
  634. goto cleanup_entity;
  635. return 0;
  636. cleanup_entity:
  637. media_entity_cleanup(&sensor->sd.entity);
  638. v4l2_ctrl_handler_free(hdl);
  639. return ret;
  640. }
  641. static int ov2680_get_regulators(struct ov2680_dev *sensor)
  642. {
  643. int i;
  644. for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
  645. sensor->supplies[i].supply = ov2680_supply_name[i];
  646. return devm_regulator_bulk_get(&sensor->i2c_client->dev,
  647. OV2680_NUM_SUPPLIES,
  648. sensor->supplies);
  649. }
  650. static int ov2680_check_id(struct ov2680_dev *sensor)
  651. {
  652. struct device *dev = ov2680_to_dev(sensor);
  653. u32 chip_id;
  654. int ret;
  655. ov2680_power_on(sensor);
  656. ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
  657. if (ret < 0) {
  658. dev_err(dev, "failed to read chip id high\n");
  659. return -ENODEV;
  660. }
  661. if (chip_id != OV2680_CHIP_ID) {
  662. dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
  663. chip_id, OV2680_CHIP_ID);
  664. return -ENODEV;
  665. }
  666. return 0;
  667. }
  668. static int ov2680_parse_dt(struct ov2680_dev *sensor)
  669. {
  670. struct device *dev = ov2680_to_dev(sensor);
  671. int ret;
  672. sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  673. GPIOD_OUT_HIGH);
  674. ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
  675. if (ret < 0) {
  676. dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
  677. return ret;
  678. }
  679. sensor->xvclk = devm_clk_get(dev, "xvclk");
  680. if (IS_ERR(sensor->xvclk)) {
  681. dev_err(dev, "xvclk clock missing or invalid\n");
  682. return PTR_ERR(sensor->xvclk);
  683. }
  684. sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
  685. if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
  686. dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
  687. sensor->xvclk_freq, OV2680_XVCLK_VALUE);
  688. return -EINVAL;
  689. }
  690. return 0;
  691. }
  692. static int ov2680_probe(struct i2c_client *client)
  693. {
  694. struct device *dev = &client->dev;
  695. struct ov2680_dev *sensor;
  696. int ret;
  697. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  698. if (!sensor)
  699. return -ENOMEM;
  700. sensor->i2c_client = client;
  701. ret = ov2680_parse_dt(sensor);
  702. if (ret < 0)
  703. return -EINVAL;
  704. ret = ov2680_mode_init(sensor);
  705. if (ret < 0)
  706. return ret;
  707. ret = ov2680_get_regulators(sensor);
  708. if (ret < 0) {
  709. dev_err(dev, "failed to get regulators\n");
  710. return ret;
  711. }
  712. mutex_init(&sensor->lock);
  713. ret = ov2680_check_id(sensor);
  714. if (ret < 0)
  715. goto lock_destroy;
  716. ret = ov2680_v4l2_register(sensor);
  717. if (ret < 0)
  718. goto lock_destroy;
  719. dev_info(dev, "ov2680 init correctly\n");
  720. return 0;
  721. lock_destroy:
  722. dev_err(dev, "ov2680 init fail: %d\n", ret);
  723. mutex_destroy(&sensor->lock);
  724. return ret;
  725. }
  726. static void ov2680_remove(struct i2c_client *client)
  727. {
  728. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  729. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  730. v4l2_async_unregister_subdev(&sensor->sd);
  731. mutex_destroy(&sensor->lock);
  732. media_entity_cleanup(&sensor->sd.entity);
  733. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  734. }
  735. static int __maybe_unused ov2680_suspend(struct device *dev)
  736. {
  737. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  738. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  739. if (sensor->is_streaming)
  740. ov2680_stream_disable(sensor);
  741. return 0;
  742. }
  743. static int __maybe_unused ov2680_resume(struct device *dev)
  744. {
  745. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  746. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  747. int ret;
  748. if (sensor->is_streaming) {
  749. ret = ov2680_stream_enable(sensor);
  750. if (ret < 0)
  751. goto stream_disable;
  752. }
  753. return 0;
  754. stream_disable:
  755. ov2680_stream_disable(sensor);
  756. sensor->is_streaming = false;
  757. return ret;
  758. }
  759. static const struct dev_pm_ops ov2680_pm_ops = {
  760. SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
  761. };
  762. static const struct of_device_id ov2680_dt_ids[] = {
  763. { .compatible = "ovti,ov2680" },
  764. { /* sentinel */ },
  765. };
  766. MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
  767. static struct i2c_driver ov2680_i2c_driver = {
  768. .driver = {
  769. .name = "ov2680",
  770. .pm = &ov2680_pm_ops,
  771. .of_match_table = of_match_ptr(ov2680_dt_ids),
  772. },
  773. .probe_new = ov2680_probe,
  774. .remove = ov2680_remove,
  775. };
  776. module_i2c_driver(ov2680_i2c_driver);
  777. MODULE_AUTHOR("Rui Miguel Silva <[email protected]>");
  778. MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
  779. MODULE_LICENSE("GPL v2");