ov2680.c 26 KB

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