ov2680.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. sensor->is_enabled = true;
  447. /* Set clock lane into LP-11 state */
  448. ov2680_stream_enable(sensor);
  449. usleep_range(1000, 2000);
  450. ov2680_stream_disable(sensor);
  451. return 0;
  452. }
  453. static int ov2680_s_power(struct v4l2_subdev *sd, int on)
  454. {
  455. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  456. int ret = 0;
  457. mutex_lock(&sensor->lock);
  458. if (on)
  459. ret = ov2680_power_on(sensor);
  460. else
  461. ret = ov2680_power_off(sensor);
  462. mutex_unlock(&sensor->lock);
  463. if (on && ret == 0) {
  464. ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
  465. if (ret < 0)
  466. return ret;
  467. ret = ov2680_mode_restore(sensor);
  468. }
  469. return ret;
  470. }
  471. static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
  472. struct v4l2_subdev_frame_interval *fi)
  473. {
  474. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  475. mutex_lock(&sensor->lock);
  476. fi->interval = sensor->frame_interval;
  477. mutex_unlock(&sensor->lock);
  478. return 0;
  479. }
  480. static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
  481. {
  482. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  483. int ret = 0;
  484. mutex_lock(&sensor->lock);
  485. if (sensor->is_streaming == !!enable)
  486. goto unlock;
  487. if (enable && sensor->mode_pending_changes) {
  488. ret = ov2680_mode_set(sensor);
  489. if (ret < 0)
  490. goto unlock;
  491. }
  492. if (enable)
  493. ret = ov2680_stream_enable(sensor);
  494. else
  495. ret = ov2680_stream_disable(sensor);
  496. sensor->is_streaming = !!enable;
  497. unlock:
  498. mutex_unlock(&sensor->lock);
  499. return ret;
  500. }
  501. static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
  502. struct v4l2_subdev_pad_config *cfg,
  503. struct v4l2_subdev_mbus_code_enum *code)
  504. {
  505. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  506. if (code->pad != 0 || code->index != 0)
  507. return -EINVAL;
  508. code->code = sensor->fmt.code;
  509. return 0;
  510. }
  511. static int ov2680_get_fmt(struct v4l2_subdev *sd,
  512. struct v4l2_subdev_pad_config *cfg,
  513. struct v4l2_subdev_format *format)
  514. {
  515. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  516. struct v4l2_mbus_framefmt *fmt = NULL;
  517. int ret = 0;
  518. if (format->pad != 0)
  519. return -EINVAL;
  520. mutex_lock(&sensor->lock);
  521. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  522. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  523. fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, format->pad);
  524. #else
  525. ret = -ENOTTY;
  526. #endif
  527. } else {
  528. fmt = &sensor->fmt;
  529. }
  530. if (fmt)
  531. format->format = *fmt;
  532. mutex_unlock(&sensor->lock);
  533. return ret;
  534. }
  535. static int ov2680_set_fmt(struct v4l2_subdev *sd,
  536. struct v4l2_subdev_pad_config *cfg,
  537. struct v4l2_subdev_format *format)
  538. {
  539. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  540. struct v4l2_mbus_framefmt *fmt = &format->format;
  541. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  542. struct v4l2_mbus_framefmt *try_fmt;
  543. #endif
  544. const struct ov2680_mode_info *mode;
  545. int ret = 0;
  546. if (format->pad != 0)
  547. return -EINVAL;
  548. mutex_lock(&sensor->lock);
  549. if (sensor->is_streaming) {
  550. ret = -EBUSY;
  551. goto unlock;
  552. }
  553. mode = v4l2_find_nearest_size(ov2680_mode_data,
  554. ARRAY_SIZE(ov2680_mode_data), width,
  555. height, fmt->width, fmt->height);
  556. if (!mode) {
  557. ret = -EINVAL;
  558. goto unlock;
  559. }
  560. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  561. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  562. try_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  563. format->format = *try_fmt;
  564. #else
  565. ret = -ENOTTY;
  566. #endif
  567. goto unlock;
  568. }
  569. fmt->width = mode->width;
  570. fmt->height = mode->height;
  571. fmt->code = sensor->fmt.code;
  572. fmt->colorspace = sensor->fmt.colorspace;
  573. sensor->current_mode = mode;
  574. sensor->fmt = format->format;
  575. sensor->mode_pending_changes = true;
  576. unlock:
  577. mutex_unlock(&sensor->lock);
  578. return ret;
  579. }
  580. static int ov2680_init_cfg(struct v4l2_subdev *sd,
  581. struct v4l2_subdev_pad_config *cfg)
  582. {
  583. struct v4l2_subdev_format fmt = {
  584. .which = cfg ? V4L2_SUBDEV_FORMAT_TRY
  585. : V4L2_SUBDEV_FORMAT_ACTIVE,
  586. .format = {
  587. .width = 800,
  588. .height = 600,
  589. }
  590. };
  591. return ov2680_set_fmt(sd, cfg, &fmt);
  592. }
  593. static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
  594. struct v4l2_subdev_pad_config *cfg,
  595. struct v4l2_subdev_frame_size_enum *fse)
  596. {
  597. int index = fse->index;
  598. if (index >= OV2680_MODE_MAX || index < 0)
  599. return -EINVAL;
  600. fse->min_width = ov2680_mode_data[index].width;
  601. fse->min_height = ov2680_mode_data[index].height;
  602. fse->max_width = ov2680_mode_data[index].width;
  603. fse->max_height = ov2680_mode_data[index].height;
  604. return 0;
  605. }
  606. static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
  607. struct v4l2_subdev_pad_config *cfg,
  608. struct v4l2_subdev_frame_interval_enum *fie)
  609. {
  610. struct v4l2_fract tpf;
  611. if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
  612. fie->height > OV2680_HEIGHT_MAX ||
  613. fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
  614. return -EINVAL;
  615. tpf.denominator = OV2680_FRAME_RATE;
  616. tpf.numerator = 1;
  617. fie->interval = tpf;
  618. return 0;
  619. }
  620. static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  621. {
  622. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  623. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  624. struct ov2680_ctrls *ctrls = &sensor->ctrls;
  625. int val;
  626. if (!sensor->is_enabled)
  627. return 0;
  628. switch (ctrl->id) {
  629. case V4L2_CID_GAIN:
  630. val = ov2680_gain_get(sensor);
  631. if (val < 0)
  632. return val;
  633. ctrls->gain->val = val;
  634. break;
  635. case V4L2_CID_EXPOSURE:
  636. val = ov2680_exposure_get(sensor);
  637. if (val < 0)
  638. return val;
  639. ctrls->exposure->val = val;
  640. break;
  641. }
  642. return 0;
  643. }
  644. static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
  645. {
  646. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  647. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  648. struct ov2680_ctrls *ctrls = &sensor->ctrls;
  649. if (!sensor->is_enabled)
  650. return 0;
  651. switch (ctrl->id) {
  652. case V4L2_CID_AUTOGAIN:
  653. return ov2680_gain_set(sensor, !!ctrl->val);
  654. case V4L2_CID_GAIN:
  655. return ov2680_gain_set(sensor, !!ctrls->auto_gain->val);
  656. case V4L2_CID_EXPOSURE_AUTO:
  657. return ov2680_exposure_set(sensor, !!ctrl->val);
  658. case V4L2_CID_EXPOSURE:
  659. return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val);
  660. case V4L2_CID_VFLIP:
  661. if (sensor->is_streaming)
  662. return -EBUSY;
  663. if (ctrl->val)
  664. return ov2680_vflip_enable(sensor);
  665. else
  666. return ov2680_vflip_disable(sensor);
  667. case V4L2_CID_HFLIP:
  668. if (sensor->is_streaming)
  669. return -EBUSY;
  670. if (ctrl->val)
  671. return ov2680_hflip_enable(sensor);
  672. else
  673. return ov2680_hflip_disable(sensor);
  674. case V4L2_CID_TEST_PATTERN:
  675. return ov2680_test_pattern_set(sensor, ctrl->val);
  676. default:
  677. break;
  678. }
  679. return -EINVAL;
  680. }
  681. static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
  682. .g_volatile_ctrl = ov2680_g_volatile_ctrl,
  683. .s_ctrl = ov2680_s_ctrl,
  684. };
  685. static const struct v4l2_subdev_core_ops ov2680_core_ops = {
  686. .s_power = ov2680_s_power,
  687. };
  688. static const struct v4l2_subdev_video_ops ov2680_video_ops = {
  689. .g_frame_interval = ov2680_s_g_frame_interval,
  690. .s_frame_interval = ov2680_s_g_frame_interval,
  691. .s_stream = ov2680_s_stream,
  692. };
  693. static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
  694. .init_cfg = ov2680_init_cfg,
  695. .enum_mbus_code = ov2680_enum_mbus_code,
  696. .get_fmt = ov2680_get_fmt,
  697. .set_fmt = ov2680_set_fmt,
  698. .enum_frame_size = ov2680_enum_frame_size,
  699. .enum_frame_interval = ov2680_enum_frame_interval,
  700. };
  701. static const struct v4l2_subdev_ops ov2680_subdev_ops = {
  702. .core = &ov2680_core_ops,
  703. .video = &ov2680_video_ops,
  704. .pad = &ov2680_pad_ops,
  705. };
  706. static int ov2680_mode_init(struct ov2680_dev *sensor)
  707. {
  708. const struct ov2680_mode_info *init_mode;
  709. /* set initial mode */
  710. sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  711. sensor->fmt.width = 800;
  712. sensor->fmt.height = 600;
  713. sensor->fmt.field = V4L2_FIELD_NONE;
  714. sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  715. sensor->frame_interval.denominator = OV2680_FRAME_RATE;
  716. sensor->frame_interval.numerator = 1;
  717. init_mode = &ov2680_mode_init_data;
  718. sensor->current_mode = init_mode;
  719. sensor->mode_pending_changes = true;
  720. return 0;
  721. }
  722. static int ov2680_v4l2_init(struct ov2680_dev *sensor)
  723. {
  724. const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
  725. struct ov2680_ctrls *ctrls = &sensor->ctrls;
  726. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  727. int ret = 0;
  728. v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
  729. &ov2680_subdev_ops);
  730. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  731. sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  732. #endif
  733. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  734. sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  735. ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
  736. if (ret < 0)
  737. return ret;
  738. v4l2_ctrl_handler_init(hdl, 7);
  739. hdl->lock = &sensor->lock;
  740. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  741. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  742. ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
  743. &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
  744. ARRAY_SIZE(test_pattern_menu) - 1,
  745. 0, 0, test_pattern_menu);
  746. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  747. V4L2_CID_EXPOSURE_AUTO,
  748. V4L2_EXPOSURE_MANUAL, 0,
  749. V4L2_EXPOSURE_AUTO);
  750. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  751. 0, 32767, 1, 0);
  752. ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
  753. 0, 1, 1, 1);
  754. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
  755. if (hdl->error) {
  756. ret = hdl->error;
  757. goto cleanup_entity;
  758. }
  759. ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  760. ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  761. v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
  762. v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
  763. sensor->sd.ctrl_handler = hdl;
  764. ret = v4l2_async_register_subdev(&sensor->sd);
  765. if (ret < 0)
  766. goto cleanup_entity;
  767. return 0;
  768. cleanup_entity:
  769. media_entity_cleanup(&sensor->sd.entity);
  770. v4l2_ctrl_handler_free(hdl);
  771. return ret;
  772. }
  773. static int ov2680_get_regulators(struct ov2680_dev *sensor)
  774. {
  775. int i;
  776. for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
  777. sensor->supplies[i].supply = ov2680_supply_name[i];
  778. return devm_regulator_bulk_get(&sensor->i2c_client->dev,
  779. OV2680_NUM_SUPPLIES,
  780. sensor->supplies);
  781. }
  782. static int ov2680_check_id(struct ov2680_dev *sensor)
  783. {
  784. struct device *dev = ov2680_to_dev(sensor);
  785. u32 chip_id;
  786. int ret;
  787. ov2680_power_on(sensor);
  788. ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
  789. if (ret < 0) {
  790. dev_err(dev, "failed to read chip id high\n");
  791. return -ENODEV;
  792. }
  793. if (chip_id != OV2680_CHIP_ID) {
  794. dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
  795. chip_id, OV2680_CHIP_ID);
  796. return -ENODEV;
  797. }
  798. return 0;
  799. }
  800. static int ov2860_parse_dt(struct ov2680_dev *sensor)
  801. {
  802. struct device *dev = ov2680_to_dev(sensor);
  803. int ret;
  804. sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  805. GPIOD_OUT_HIGH);
  806. ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
  807. if (ret < 0) {
  808. dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
  809. return ret;
  810. }
  811. sensor->xvclk = devm_clk_get(dev, "xvclk");
  812. if (IS_ERR(sensor->xvclk)) {
  813. dev_err(dev, "xvclk clock missing or invalid\n");
  814. return PTR_ERR(sensor->xvclk);
  815. }
  816. sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
  817. if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
  818. dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
  819. sensor->xvclk_freq, OV2680_XVCLK_VALUE);
  820. return -EINVAL;
  821. }
  822. return 0;
  823. }
  824. static int ov2680_probe(struct i2c_client *client)
  825. {
  826. struct device *dev = &client->dev;
  827. struct ov2680_dev *sensor;
  828. int ret;
  829. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  830. if (!sensor)
  831. return -ENOMEM;
  832. sensor->i2c_client = client;
  833. ret = ov2860_parse_dt(sensor);
  834. if (ret < 0)
  835. return -EINVAL;
  836. ret = ov2680_mode_init(sensor);
  837. if (ret < 0)
  838. return ret;
  839. ret = ov2680_get_regulators(sensor);
  840. if (ret < 0) {
  841. dev_err(dev, "failed to get regulators\n");
  842. return ret;
  843. }
  844. mutex_init(&sensor->lock);
  845. ret = ov2680_check_id(sensor);
  846. if (ret < 0)
  847. goto lock_destroy;
  848. ret = ov2680_v4l2_init(sensor);
  849. if (ret < 0)
  850. goto lock_destroy;
  851. dev_info(dev, "ov2680 init correctly\n");
  852. return 0;
  853. lock_destroy:
  854. dev_err(dev, "ov2680 init fail: %d\n", ret);
  855. mutex_destroy(&sensor->lock);
  856. return ret;
  857. }
  858. static int ov2680_remove(struct i2c_client *client)
  859. {
  860. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  861. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  862. v4l2_async_unregister_subdev(&sensor->sd);
  863. mutex_destroy(&sensor->lock);
  864. media_entity_cleanup(&sensor->sd.entity);
  865. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  866. return 0;
  867. }
  868. static int __maybe_unused ov2680_suspend(struct device *dev)
  869. {
  870. struct i2c_client *client = to_i2c_client(dev);
  871. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  872. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  873. if (sensor->is_streaming)
  874. ov2680_stream_disable(sensor);
  875. return 0;
  876. }
  877. static int __maybe_unused ov2680_resume(struct device *dev)
  878. {
  879. struct i2c_client *client = to_i2c_client(dev);
  880. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  881. struct ov2680_dev *sensor = to_ov2680_dev(sd);
  882. int ret;
  883. if (sensor->is_streaming) {
  884. ret = ov2680_stream_enable(sensor);
  885. if (ret < 0)
  886. goto stream_disable;
  887. }
  888. return 0;
  889. stream_disable:
  890. ov2680_stream_disable(sensor);
  891. sensor->is_streaming = false;
  892. return ret;
  893. }
  894. static const struct dev_pm_ops ov2680_pm_ops = {
  895. SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
  896. };
  897. static const struct of_device_id ov2680_dt_ids[] = {
  898. { .compatible = "ovti,ov2680" },
  899. { /* sentinel */ },
  900. };
  901. MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
  902. static struct i2c_driver ov2680_i2c_driver = {
  903. .driver = {
  904. .name = "ov2680",
  905. .pm = &ov2680_pm_ops,
  906. .of_match_table = of_match_ptr(ov2680_dt_ids),
  907. },
  908. .probe_new = ov2680_probe,
  909. .remove = ov2680_remove,
  910. };
  911. module_i2c_driver(ov2680_i2c_driver);
  912. MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
  913. MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
  914. MODULE_LICENSE("GPL v2");