ov1063x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * OmniVision OV1063X Camera Driver
  3. *
  4. * Based on the original driver written by Phil Edworthy.
  5. * Copyright (C) 2013 Phil Edworthy
  6. * Copyright (C) 2013 Renesas Electronics
  7. *
  8. * This driver has been tested at QVGA, VGA and 720p, and 1280x800 at up to
  9. * 30fps and it should work at any resolution in between and any frame rate
  10. * up to 30fps.
  11. *
  12. * FIXME:
  13. * Horizontal flip (mirroring) does not work correctly. The image is flipped,
  14. * but the colors are wrong.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. *
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/regmap.h>
  29. #include <linux/slab.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/gpio/consumer.h>
  34. #include <linux/of_device.h>
  35. #include <linux/v4l2-mediabus.h>
  36. #include <media/media-entity.h>
  37. #include <media/v4l2-ctrls.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-event.h>
  40. #include <media/v4l2-image-sizes.h>
  41. #include <media/v4l2-subdev.h>
  42. #include <media/v4l2-mediabus.h>
  43. #include <media/v4l2-common.h>
  44. #include <media/v4l2-of.h>
  45. #include "ov1063x_regs.h"
  46. /* Register definitions */
  47. #define OV1063X_VFLIP 0x381c
  48. #define OV1063X_VFLIP_ON (0x3 << 6)
  49. #define OV1063X_VFLIP_SUBSAMPLE 0x1
  50. #define OV1063X_HMIRROR 0x381d
  51. #define OV1063X_HMIRROR_ON 0x3
  52. #define OV1063X_PID 0x300a
  53. #define OV1063X_VER 0x300b
  54. #define OV1063X_FORMAT_CTRL00 0x4300
  55. #define OV1063X_FORMAT_YUYV 0x38
  56. #define OV1063X_FORMAT_YYYU 0x39
  57. #define OV1063X_FORMAT_UYVY 0x3A
  58. #define OV1063X_FORMAT_VYUY 0x3B
  59. /* IDs */
  60. #define OV10633_VERSION_REG 0xa630
  61. #define OV10635_VERSION_REG 0xa635
  62. #define OV1063X_VERSION(pid, ver) (((pid) << 8) | ((ver) & 0xff))
  63. enum ov1063x_model {
  64. SENSOR_OV10633,
  65. SENSOR_OV10635,
  66. };
  67. #define OV1063X_SENSOR_WIDTH 1312
  68. #define OV1063X_SENSOR_HEIGHT 814
  69. #define OV1063X_MAX_WIDTH 1280
  70. #define OV1063X_MAX_HEIGHT 800
  71. #define MAX_NUM_GPIOS 20
  72. struct ov1063x_color_format {
  73. u32 code;
  74. u32 colorspace;
  75. };
  76. struct ov1063x_framesize {
  77. u16 width;
  78. u16 height;
  79. };
  80. struct ov1063x_priv {
  81. struct v4l2_subdev subdev;
  82. struct v4l2_async_subdev asd;
  83. struct v4l2_ctrl_handler hdl;
  84. int model;
  85. int revision;
  86. int xvclk_rate;
  87. /* Protects the struct fields below */
  88. struct mutex lock;
  89. int fps_numerator;
  90. int fps_denominator;
  91. struct v4l2_mbus_framefmt format;
  92. int width;
  93. int height;
  94. struct gpio mux_gpios[MAX_NUM_GPIOS];
  95. int num_gpios;
  96. /* Sensor reference clock */
  97. struct clk *xvclk;
  98. bool power;
  99. /* GPIOs */
  100. struct gpio_desc *reset_gpio;
  101. struct gpio_desc *powerdown_gpio;
  102. struct v4l2_ctrl *colorbar;
  103. };
  104. static int ov1063x_init_gpios(struct i2c_client *client);
  105. static const struct ov1063x_framesize ov1063x_framesizes[] = {
  106. {
  107. .width = 1280,
  108. .height = 800,
  109. }, {
  110. .width = 1280,
  111. .height = 720,
  112. }, {
  113. .width = 752,
  114. .height = 480,
  115. }, {
  116. .width = 640,
  117. .height = 480,
  118. }, {
  119. .width = 600,
  120. .height = 400,
  121. }, {
  122. .width = 352,
  123. .height = 288,
  124. }, {
  125. .width = 320,
  126. .height = 240,
  127. },
  128. };
  129. /*
  130. * supported color format list
  131. */
  132. static const struct ov1063x_color_format ov1063x_cfmts[] = {
  133. {
  134. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  135. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  136. },
  137. {
  138. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  139. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  140. },
  141. {
  142. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  143. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  144. },
  145. {
  146. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  147. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  148. },
  149. {
  150. .code = MEDIA_BUS_FMT_YUYV10_2X10,
  151. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  152. },
  153. };
  154. static struct ov1063x_priv *to_ov1063x(const struct i2c_client *client)
  155. {
  156. return container_of(i2c_get_clientdata(client), struct ov1063x_priv,
  157. subdev);
  158. }
  159. /* read a register */
  160. static int ov1063x_reg_read(struct i2c_client *client, u16 reg, u8 *val)
  161. {
  162. int ret;
  163. u8 data[2] = { reg >> 8, reg & 0xff };
  164. struct i2c_msg msg = {
  165. .addr = client->addr,
  166. .flags = 0,
  167. .len = 2,
  168. .buf = data,
  169. };
  170. ret = i2c_transfer(client->adapter, &msg, 1);
  171. if (ret < 0)
  172. goto err;
  173. msg.flags = I2C_M_RD;
  174. msg.len = 1;
  175. msg.buf = &data[0];
  176. ret = i2c_transfer(client->adapter, &msg, 1);
  177. if (ret < 0)
  178. goto err;
  179. *val = data[0];
  180. return 0;
  181. err:
  182. dev_err(&client->dev, "Failed reading register 0x%04x!\n", reg);
  183. return ret;
  184. }
  185. /* write a register */
  186. static int ov1063x_reg_write(struct i2c_client *client, u16 reg, u8 val)
  187. {
  188. int ret;
  189. u8 data[3] = { reg >> 8, reg & 0xff, val };
  190. struct i2c_msg msg = {
  191. .addr = client->addr,
  192. .flags = 0,
  193. .len = 3,
  194. .buf = data,
  195. };
  196. ret = i2c_transfer(client->adapter, &msg, 1);
  197. if (ret < 0) {
  198. dev_err(&client->dev, "Failed writing register 0x%04x!\n", reg);
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. static int ov1063x_reg_write16(struct i2c_client *client, u16 reg, u16 val)
  204. {
  205. int ret;
  206. ret = ov1063x_reg_write(client, reg, val >> 8);
  207. if (ret)
  208. return ret;
  209. ret = ov1063x_reg_write(client, reg + 1, val & 0xff);
  210. if (ret)
  211. return ret;
  212. return 0;
  213. }
  214. /* Read a register, alter its bits, write it back */
  215. static int ov1063x_reg_rmw(struct i2c_client *client, u16 reg, u8 set, u8 unset)
  216. {
  217. u8 val;
  218. int ret;
  219. ret = ov1063x_reg_read(client, reg, &val);
  220. if (ret) {
  221. dev_err(&client->dev,
  222. "[Read]-Modify-Write of register %04x failed!\n", reg);
  223. return ret;
  224. }
  225. val |= set;
  226. val &= ~unset;
  227. ret = ov1063x_reg_write(client, reg, val);
  228. if (ret)
  229. dev_err(&client->dev,
  230. "Read-Modify-[Write] of register %04x failed!\n", reg);
  231. return ret;
  232. }
  233. /* Start/Stop streaming from the device */
  234. static int ov1063x_s_stream(struct v4l2_subdev *sd, int enable)
  235. {
  236. struct i2c_client *client = v4l2_get_subdevdata(sd);
  237. int ret;
  238. ret = ov1063x_init_gpios(client);
  239. if (ret) {
  240. dev_err(&client->dev, "Failed to request gpios");
  241. return ret;
  242. }
  243. ov1063x_reg_write(client, 0x0100, enable);
  244. if (enable)
  245. ov1063x_reg_write(client, 0x301c, 0xf0);
  246. else
  247. ov1063x_reg_write(client, 0x301c, 0x70);
  248. return 0;
  249. }
  250. static int ov1063x_set_regs(struct i2c_client *client,
  251. const struct ov1063x_reg *regs, int nr_regs);
  252. /* Set status of additional camera capabilities */
  253. static int ov1063x_s_ctrl(struct v4l2_ctrl *ctrl)
  254. {
  255. struct ov1063x_priv *priv = container_of(ctrl->handler,
  256. struct ov1063x_priv, hdl);
  257. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  258. int n_regs;
  259. switch (ctrl->id) {
  260. case V4L2_CID_VFLIP:
  261. if (ctrl->val)
  262. return ov1063x_reg_rmw(client, OV1063X_VFLIP,
  263. OV1063X_VFLIP_ON, 0);
  264. else
  265. return ov1063x_reg_rmw(client, OV1063X_VFLIP,
  266. 0, OV1063X_VFLIP_ON);
  267. break;
  268. case V4L2_CID_HFLIP:
  269. if (ctrl->val)
  270. return ov1063x_reg_rmw(client, OV1063X_HMIRROR,
  271. OV1063X_HMIRROR_ON, 0);
  272. else
  273. return ov1063x_reg_rmw(client, OV1063X_HMIRROR,
  274. 0, OV1063X_HMIRROR_ON);
  275. break;
  276. case V4L2_CID_TEST_PATTERN:
  277. if (ctrl->val) {
  278. n_regs = ARRAY_SIZE(ov1063x_regs_colorbar_enable);
  279. return ov1063x_set_regs(client,
  280. ov1063x_regs_colorbar_enable,
  281. n_regs);
  282. } else {
  283. n_regs = ARRAY_SIZE(ov1063x_regs_colorbar_disable);
  284. return ov1063x_set_regs(client,
  285. ov1063x_regs_colorbar_disable,
  286. n_regs);
  287. }
  288. break;
  289. }
  290. return -EINVAL;
  291. }
  292. /*
  293. * Get the best pixel clock (pclk) that meets minimum hts/vts requirements.
  294. * xvclk_rate => pre-divider => clk1 => multiplier => clk2 => post-divider
  295. * => pclk
  296. * We try all valid combinations of settings for the 3 blocks to get the pixel
  297. * clock, and from that calculate the actual hts/vts to use. The vts is
  298. * extended so as to achieve the required frame rate. The function also returns
  299. * the PLL register contents needed to set the pixel clock.
  300. */
  301. static int ov1063x_get_pclk(int xvclk_rate, int *htsmin, int *vtsmin,
  302. int fps_numerator, int fps_denominator,
  303. u8 *r3003, u8 *r3004)
  304. {
  305. int pre_divs[] = { 2, 3, 4, 6, 8, 10, 12, 14 };
  306. int pclk;
  307. int best_pclk = INT_MAX;
  308. int best_hts = 0;
  309. int i, j, k;
  310. int best_i = 0, best_j = 0, best_k = 0;
  311. int clk1, clk2;
  312. int hts;
  313. /* Pre-div, reg 0x3004, bits 6:4 */
  314. for (i = 0; i < ARRAY_SIZE(pre_divs); i++) {
  315. clk1 = (xvclk_rate / pre_divs[i]) * 2;
  316. if ((clk1 < 3000000) || (clk1 > 27000000))
  317. continue;
  318. /* Mult = reg 0x3003, bits 5:0 */
  319. for (j = 1; j < 32; j++) {
  320. clk2 = (clk1 * j);
  321. if ((clk2 < 200000000) || (clk2 > 500000000))
  322. continue;
  323. /* Post-div, reg 0x3004, bits 2:0 */
  324. for (k = 0; k < 8; k++) {
  325. pclk = clk2 / (2 * (k + 1));
  326. if (pclk > 96000000)
  327. continue;
  328. hts = *htsmin + 200 + pclk / 300000;
  329. /* 2 clock cycles for every YUV422 pixel */
  330. if (pclk < (((hts * *vtsmin) / fps_denominator)
  331. * fps_numerator * 2))
  332. continue;
  333. if (pclk < best_pclk) {
  334. best_pclk = pclk;
  335. best_hts = hts;
  336. best_i = i;
  337. best_j = j;
  338. best_k = k;
  339. }
  340. }
  341. }
  342. }
  343. /* register contents */
  344. *r3003 = (u8)best_j;
  345. *r3004 = ((u8)best_i << 4) | (u8)best_k;
  346. /* Did we get a valid PCLK? */
  347. if (best_pclk == INT_MAX)
  348. return -1;
  349. *htsmin = best_hts;
  350. /* Adjust vts to get as close to the desired frame rate as we can */
  351. *vtsmin = best_pclk / ((best_hts / fps_denominator) *
  352. fps_numerator * 2);
  353. return best_pclk;
  354. }
  355. static int ov1063x_set_regs(struct i2c_client *client,
  356. const struct ov1063x_reg *regs, int nr_regs)
  357. {
  358. int i, ret;
  359. u8 val;
  360. for (i = 0; i < nr_regs; i++) {
  361. if (regs[i].reg == 0x300c) {
  362. val = ((client->addr * 2) | 0x1);
  363. ret = ov1063x_reg_write(client, regs[i].reg, val);
  364. if (ret)
  365. return ret;
  366. } else {
  367. ret = ov1063x_reg_write(client, regs[i].reg,
  368. regs[i].val);
  369. if (ret)
  370. return ret;
  371. }
  372. }
  373. return 0;
  374. }
  375. /* Setup registers according to resolution and color encoding */
  376. static int ov1063x_set_params(struct i2c_client *client, u32 width, u32 height)
  377. {
  378. struct ov1063x_priv *priv = to_ov1063x(client);
  379. int ret = -EINVAL;
  380. int pclk;
  381. int hts, vts;
  382. u8 r3003, r3004, r4300;
  383. int tmp;
  384. u32 height_pre_subsample;
  385. u32 width_pre_subsample;
  386. u8 horiz_crop_mode;
  387. int nr_isp_pixels;
  388. int vert_sub_sample = 0;
  389. int horiz_sub_sample = 0;
  390. int sensor_width;
  391. int tmp_array_size;
  392. if ((width > OV1063X_MAX_WIDTH) || (height > OV1063X_MAX_HEIGHT))
  393. return ret;
  394. priv->width = width;
  395. priv->height = height;
  396. /* Vertical sub-sampling? */
  397. height_pre_subsample = priv->height;
  398. if (priv->height <= 400) {
  399. vert_sub_sample = 1;
  400. height_pre_subsample <<= 1;
  401. }
  402. /* Horizontal sub-sampling? */
  403. width_pre_subsample = priv->width;
  404. if (priv->width <= 640) {
  405. horiz_sub_sample = 1;
  406. width_pre_subsample <<= 1;
  407. }
  408. /* Horizontal cropping */
  409. if (width_pre_subsample > 768) {
  410. sensor_width = OV1063X_SENSOR_WIDTH;
  411. horiz_crop_mode = 0x63;
  412. } else if (width_pre_subsample > 656) {
  413. sensor_width = 768;
  414. horiz_crop_mode = 0x6b;
  415. } else {
  416. sensor_width = 656;
  417. horiz_crop_mode = 0x73;
  418. }
  419. /* minimum values for hts and vts */
  420. hts = sensor_width;
  421. vts = height_pre_subsample + 50;
  422. dev_dbg(&client->dev, "fps=(%d/%d), hts=%d, vts=%d\n",
  423. priv->fps_numerator, priv->fps_denominator, hts, vts);
  424. /* Get the best PCLK & adjust hts,vts accordingly */
  425. pclk = ov1063x_get_pclk(priv->xvclk_rate, &hts, &vts,
  426. priv->fps_numerator, priv->fps_denominator,
  427. &r3003, &r3004);
  428. if (pclk < 0)
  429. return ret;
  430. dev_dbg(&client->dev, "pclk=%d, hts=%d, vts=%d\n", pclk, hts, vts);
  431. dev_dbg(&client->dev, "r3003=0x%X r3004=0x%X\n", r3003, r3004);
  432. /* Disable ISP & program all registers that we might modify */
  433. ret = ov1063x_set_regs(client, ov1063x_regs_change_mode,
  434. ARRAY_SIZE(ov1063x_regs_change_mode));
  435. if (ret)
  436. return ret;
  437. /* Set to 1280x720 */
  438. ret = ov1063x_reg_write(client, 0x380f, 0x80);
  439. if (ret)
  440. return ret;
  441. /* Set PLL */
  442. ret = ov1063x_reg_write(client, 0x3003, r3003);
  443. if (ret)
  444. return ret;
  445. ret = ov1063x_reg_write(client, 0x3004, r3004);
  446. if (ret)
  447. return ret;
  448. /* Set HSYNC */
  449. ret = ov1063x_reg_write(client, 0x4700, 0x00);
  450. if (ret)
  451. return ret;
  452. switch (priv->format.code) {
  453. case MEDIA_BUS_FMT_UYVY8_2X8:
  454. r4300 = OV1063X_FORMAT_UYVY;
  455. break;
  456. case MEDIA_BUS_FMT_VYUY8_2X8:
  457. r4300 = OV1063X_FORMAT_VYUY;
  458. break;
  459. case MEDIA_BUS_FMT_YUYV8_2X8:
  460. r4300 = OV1063X_FORMAT_YUYV;
  461. break;
  462. case MEDIA_BUS_FMT_YVYU8_2X8:
  463. r4300 = OV1063X_FORMAT_YYYU;
  464. break;
  465. default:
  466. r4300 = OV1063X_FORMAT_UYVY;
  467. break;
  468. }
  469. /* Set format to UYVY */
  470. ret = ov1063x_reg_write(client, OV1063X_FORMAT_CTRL00, r4300);
  471. if (ret)
  472. return ret;
  473. dev_dbg(&client->dev, "r4300=0x%X\n", r4300);
  474. /* Set output to 8-bit yuv */
  475. ret = ov1063x_reg_write(client, 0x4605, 0x08);
  476. if (ret)
  477. return ret;
  478. /* Horizontal cropping */
  479. ret = ov1063x_reg_write(client, 0x3621, horiz_crop_mode);
  480. if (ret)
  481. return ret;
  482. ret = ov1063x_reg_write(client, 0x3702, (pclk + 1500000) / 3000000);
  483. if (ret)
  484. return ret;
  485. ret = ov1063x_reg_write(client, 0x3703, (pclk + 666666) / 1333333);
  486. if (ret)
  487. return ret;
  488. ret = ov1063x_reg_write(client, 0x3704, (pclk + 961500) / 1923000);
  489. if (ret)
  490. return ret;
  491. /* Vertical cropping */
  492. tmp = ((OV1063X_SENSOR_HEIGHT - height_pre_subsample) / 2) & ~0x1;
  493. ret = ov1063x_reg_write16(client, 0x3802, tmp);
  494. if (ret)
  495. return ret;
  496. tmp = tmp + height_pre_subsample + 3;
  497. ret = ov1063x_reg_write16(client, 0x3806, tmp);
  498. if (ret)
  499. return ret;
  500. dev_dbg(&client->dev, "width x height = %x x %x\n",
  501. priv->width, priv->height);
  502. /* Output size */
  503. ret = ov1063x_reg_write16(client, 0x3808, priv->width);
  504. if (ret)
  505. return ret;
  506. ret = ov1063x_reg_write16(client, 0x380a, priv->height);
  507. if (ret)
  508. return ret;
  509. dev_dbg(&client->dev, "hts x vts = %x x %x\n", hts, vts);
  510. ret = ov1063x_reg_write16(client, 0x380c, hts);
  511. if (ret)
  512. return ret;
  513. ret = ov1063x_reg_write16(client, 0x380e, vts);
  514. if (ret)
  515. return ret;
  516. if (vert_sub_sample) {
  517. ret = ov1063x_reg_rmw(client, OV1063X_VFLIP,
  518. OV1063X_VFLIP_SUBSAMPLE, 0);
  519. if (ret)
  520. return ret;
  521. tmp_array_size = ARRAY_SIZE(ov1063x_regs_vert_sub_sample);
  522. ret = ov1063x_set_regs(client, ov1063x_regs_vert_sub_sample,
  523. tmp_array_size);
  524. if (ret)
  525. return ret;
  526. }
  527. ret = ov1063x_reg_write16(client, 0x4606, 2 * hts);
  528. if (ret)
  529. return ret;
  530. ret = ov1063x_reg_write16(client, 0x460a,
  531. 2 * (hts - width_pre_subsample));
  532. if (ret)
  533. return ret;
  534. tmp = (vts - 8) * 16;
  535. ret = ov1063x_reg_write16(client, 0xc488, tmp);
  536. if (ret)
  537. return ret;
  538. ret = ov1063x_reg_write16(client, 0xc48a, tmp);
  539. if (ret)
  540. return ret;
  541. nr_isp_pixels = sensor_width * (priv->height + 4);
  542. ret = ov1063x_reg_write16(client, 0xc4cc, nr_isp_pixels / 256);
  543. if (ret)
  544. return ret;
  545. ret = ov1063x_reg_write16(client, 0xc4ce, nr_isp_pixels / 256);
  546. if (ret)
  547. return ret;
  548. ret = ov1063x_reg_write16(client, 0xc512, nr_isp_pixels / 16);
  549. if (ret)
  550. return ret;
  551. /* Horizontal sub-sampling */
  552. if (horiz_sub_sample) {
  553. ret = ov1063x_reg_write(client, 0x5005, 0x9);
  554. if (ret)
  555. return ret;
  556. ret = ov1063x_reg_write(client, 0x3007, 0x2);
  557. if (ret)
  558. return ret;
  559. }
  560. ret = ov1063x_reg_write16(client, 0xc518, vts);
  561. if (ret)
  562. return ret;
  563. ret = ov1063x_reg_write16(client, 0xc51a, hts);
  564. if (ret)
  565. return ret;
  566. /* Enable ISP blocks */
  567. ret = ov1063x_set_regs(client, ov1063x_regs_enable,
  568. ARRAY_SIZE(ov1063x_regs_enable));
  569. if (ret)
  570. return ret;
  571. return 0;
  572. }
  573. /*
  574. * V4L2 subdev video and pad level operations
  575. */
  576. static void ov1063x_get_default_format(struct v4l2_mbus_framefmt *mf)
  577. {
  578. mf->width = ov1063x_framesizes[0].width;
  579. mf->height = ov1063x_framesizes[0].height;
  580. mf->colorspace = ov1063x_cfmts[0].colorspace;
  581. mf->code = ov1063x_cfmts[0].code;
  582. mf->field = V4L2_FIELD_NONE;
  583. }
  584. static int ov1063x_get_fmt(struct v4l2_subdev *sd,
  585. struct v4l2_subdev_pad_config *cfg,
  586. struct v4l2_subdev_format *fmt)
  587. {
  588. struct i2c_client *client = v4l2_get_subdevdata(sd);
  589. struct ov1063x_priv *priv = to_ov1063x(client);
  590. struct v4l2_mbus_framefmt *mf;
  591. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  592. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  593. mutex_lock(&priv->lock);
  594. fmt->format = *mf;
  595. mutex_unlock(&priv->lock);
  596. return 0;
  597. }
  598. mutex_lock(&priv->lock);
  599. fmt->format = priv->format;
  600. mutex_unlock(&priv->lock);
  601. return 0;
  602. }
  603. static void __ov1063x_try_frame_size(struct v4l2_mbus_framefmt *mf)
  604. {
  605. const struct ov1063x_framesize *fsize = &ov1063x_framesizes[0];
  606. const struct ov1063x_framesize *match = NULL;
  607. int i = ARRAY_SIZE(ov1063x_framesizes);
  608. unsigned int min_err = UINT_MAX;
  609. while (i--) {
  610. int err = abs(fsize->width - mf->width)
  611. + abs(fsize->height - mf->height);
  612. if (err < min_err) {
  613. min_err = err;
  614. match = fsize;
  615. }
  616. fsize++;
  617. }
  618. if (!match)
  619. match = &ov1063x_framesizes[0];
  620. mf->width = match->width;
  621. mf->height = match->height;
  622. }
  623. static int ov1063x_set_fmt(struct v4l2_subdev *sd,
  624. struct v4l2_subdev_pad_config *cfg,
  625. struct v4l2_subdev_format *fmt)
  626. {
  627. struct i2c_client *client = v4l2_get_subdevdata(sd);
  628. unsigned int index = ARRAY_SIZE(ov1063x_cfmts);
  629. struct ov1063x_priv *priv = to_ov1063x(client);
  630. struct v4l2_mbus_framefmt *mf = &fmt->format;
  631. int ret = 0;
  632. __ov1063x_try_frame_size(mf);
  633. while (--index >= 0)
  634. if (ov1063x_cfmts[index].code == mf->code)
  635. break;
  636. if (index < 0)
  637. return -EINVAL;
  638. mf->colorspace = ov1063x_cfmts[index].colorspace;
  639. mf->code = ov1063x_cfmts[index].code;
  640. mf->field = V4L2_FIELD_NONE;
  641. mutex_lock(&priv->lock);
  642. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  643. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  644. *mf = fmt->format;
  645. } else {
  646. priv->format = fmt->format;
  647. ret = ov1063x_set_params(client, mf->width, mf->height);
  648. }
  649. mutex_unlock(&priv->lock);
  650. return ret;
  651. }
  652. static int ov1063x_enum_mbus_code(struct v4l2_subdev *sd,
  653. struct v4l2_subdev_pad_config *cfg,
  654. struct v4l2_subdev_mbus_code_enum *code)
  655. {
  656. if (code->index >= ARRAY_SIZE(ov1063x_cfmts))
  657. return -EINVAL;
  658. code->code = ov1063x_cfmts[code->index].code;
  659. return 0;
  660. }
  661. static int ov1063x_enum_frame_sizes(struct v4l2_subdev *sd,
  662. struct v4l2_subdev_pad_config *cfg,
  663. struct v4l2_subdev_frame_size_enum *fse)
  664. {
  665. int i = ARRAY_SIZE(ov1063x_cfmts);
  666. if (fse->index >= ARRAY_SIZE(ov1063x_framesizes))
  667. return -EINVAL;
  668. while (--i)
  669. if (ov1063x_cfmts[i].code == fse->code)
  670. break;
  671. fse->code = ov1063x_cfmts[i].code;
  672. fse->min_width = ov1063x_framesizes[fse->index].width;
  673. fse->max_width = fse->min_width;
  674. fse->max_height = ov1063x_framesizes[fse->index].height;
  675. fse->min_height = fse->max_height;
  676. return 0;
  677. }
  678. static int ov1063x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  679. {
  680. struct i2c_client *client = v4l2_get_subdevdata(sd);
  681. struct ov1063x_priv *priv = to_ov1063x(client);
  682. struct v4l2_captureparm *cp = &parms->parm.capture;
  683. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  684. return -EINVAL;
  685. memset(cp, 0, sizeof(struct v4l2_captureparm));
  686. cp->capability = V4L2_CAP_TIMEPERFRAME;
  687. cp->timeperframe.denominator = priv->fps_numerator;
  688. cp->timeperframe.numerator = priv->fps_denominator;
  689. return 0;
  690. }
  691. static int ov1063x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  692. {
  693. struct i2c_client *client = v4l2_get_subdevdata(sd);
  694. struct ov1063x_priv *priv = to_ov1063x(client);
  695. struct v4l2_captureparm *cp = &parms->parm.capture;
  696. int ret;
  697. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  698. return -EINVAL;
  699. if (cp->extendedmode != 0)
  700. return -EINVAL;
  701. /* FIXME Check we can handle the requested framerate */
  702. priv->fps_denominator = cp->timeperframe.numerator;
  703. priv->fps_numerator = cp->timeperframe.denominator;
  704. ret = ov1063x_set_params(client, priv->width, priv->height);
  705. if (ret < 0)
  706. return ret;
  707. return 0;
  708. }
  709. static int ov1063x_init_gpios(struct i2c_client *client)
  710. {
  711. struct ov1063x_priv *priv = to_ov1063x(client);
  712. int ret = 0;
  713. ret = gpio_request_array(priv->mux_gpios, priv->num_gpios);
  714. if (ret)
  715. goto done;
  716. gpio_free_array(priv->mux_gpios, priv->num_gpios);
  717. done:
  718. return ret;
  719. }
  720. static int ov1063x_init_cam_gpios(struct i2c_client *client)
  721. {
  722. struct ov1063x_priv *priv = to_ov1063x(client);
  723. struct gpio_desc *gpio;
  724. gpio = devm_gpiod_get_optional(&client->dev, "reset",
  725. GPIOD_OUT_LOW);
  726. if (IS_ERR(gpio))
  727. return PTR_ERR(gpio);
  728. priv->reset_gpio = gpio;
  729. gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  730. GPIOD_OUT_LOW);
  731. if (IS_ERR(gpio))
  732. return PTR_ERR(gpio);
  733. priv->powerdown_gpio = gpio;
  734. return 0;
  735. }
  736. static void ov1063x_set_power(struct i2c_client *client, bool on)
  737. {
  738. struct ov1063x_priv *priv = to_ov1063x(client);
  739. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  740. if (priv->power == on)
  741. return;
  742. if (on) {
  743. if (priv->powerdown_gpio) {
  744. gpiod_set_value_cansleep(priv->powerdown_gpio, 1);
  745. usleep_range(1000, 1200);
  746. }
  747. if (priv->reset_gpio) {
  748. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  749. usleep_range(250000, 260000);
  750. }
  751. } else {
  752. if (priv->powerdown_gpio)
  753. gpiod_set_value_cansleep(priv->powerdown_gpio, 0);
  754. if (priv->reset_gpio)
  755. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  756. }
  757. priv->power = on;
  758. }
  759. static int ov1063x_video_probe(struct i2c_client *client)
  760. {
  761. struct ov1063x_priv *priv = to_ov1063x(client);
  762. u8 pid, ver;
  763. u16 id;
  764. int ret;
  765. ov1063x_set_power(client, true);
  766. ret = ov1063x_set_regs(client, ov1063x_regs_default,
  767. ARRAY_SIZE(ov1063x_regs_default));
  768. if (ret)
  769. return ret;
  770. usleep_range(500, 510);
  771. /* check and show product ID and manufacturer ID */
  772. ret = ov1063x_reg_read(client, OV1063X_PID, &pid);
  773. if (ret)
  774. return ret;
  775. id = pid << 8;
  776. ret = ov1063x_reg_read(client, OV1063X_VER, &ver);
  777. if (ret)
  778. return ret;
  779. id |= ver;
  780. if (OV1063X_VERSION(pid, ver) == OV10635_VERSION_REG) {
  781. priv->model = SENSOR_OV10635;
  782. priv->revision = 1;
  783. } else if (OV1063X_VERSION(pid, ver) == OV10633_VERSION_REG) {
  784. priv->model = SENSOR_OV10633;
  785. priv->revision = 1;
  786. } else {
  787. dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
  788. return -ENODEV;
  789. }
  790. dev_info(&client->dev, "ov1063x Product ID %x Manufacturer ID %x\n",
  791. pid, ver);
  792. /* Program all the 'standard' registers */
  793. return v4l2_ctrl_handler_setup(&priv->hdl);
  794. }
  795. /*
  796. * V4L2 subdev internal operations
  797. */
  798. static int ov1063x_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  799. {
  800. struct i2c_client *client = v4l2_get_subdevdata(sd);
  801. struct v4l2_mbus_framefmt *mf;
  802. dev_dbg(&client->dev, "%s:\n", __func__);
  803. mf = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  804. ov1063x_get_default_format(mf);
  805. return 0;
  806. }
  807. static const struct v4l2_ctrl_ops ov1063x_ctrl_ops = {
  808. .s_ctrl = ov1063x_s_ctrl,
  809. };
  810. static const char * const ov1063x_test_pattern_menu[] = {
  811. "Disabled",
  812. "Vertical Color Bars",
  813. };
  814. static const struct v4l2_subdev_video_ops ov1063x_subdev_video_ops = {
  815. .s_stream = ov1063x_s_stream,
  816. .g_parm = ov1063x_g_parm,
  817. .s_parm = ov1063x_s_parm,
  818. };
  819. static const struct v4l2_subdev_internal_ops ov1063x_sd_internal_ops = {
  820. .open = ov1063x_open,
  821. };
  822. static const struct v4l2_subdev_core_ops ov1063x_subdev_core_ops = {
  823. .log_status = v4l2_ctrl_subdev_log_status,
  824. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  825. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  826. };
  827. static const struct v4l2_subdev_pad_ops ov1063x_subdev_pad_ops = {
  828. .enum_mbus_code = ov1063x_enum_mbus_code,
  829. .enum_frame_size = ov1063x_enum_frame_sizes,
  830. .get_fmt = ov1063x_get_fmt,
  831. .set_fmt = ov1063x_set_fmt,
  832. };
  833. static struct v4l2_subdev_ops ov1063x_subdev_ops = {
  834. .core = &ov1063x_subdev_core_ops,
  835. .video = &ov1063x_subdev_video_ops,
  836. .pad = &ov1063x_subdev_pad_ops,
  837. };
  838. /*
  839. * i2c_driver function
  840. */
  841. static int ov1063x_of_probe(struct i2c_client *client,
  842. struct device_node *node)
  843. {
  844. struct ov1063x_priv *priv = to_ov1063x(client);
  845. struct gpio *gpios = &priv->mux_gpios[0];
  846. unsigned int flags;
  847. int i, gpio;
  848. /*
  849. * Iterate over all the gpios in the device tree
  850. * ENOENT is returned when trying to access last + 1 gpio
  851. */
  852. for (i = 0; i < MAX_NUM_GPIOS; i++) {
  853. gpio = of_get_named_gpio_flags(node, "mux-gpios", i, &flags);
  854. if (gpio_is_valid(gpio)) {
  855. gpios[i].gpio = gpio;
  856. gpios[i].flags = (flags & OF_GPIO_ACTIVE_LOW) ?
  857. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH;
  858. gpios[i].label = client->name;
  859. } else {
  860. if (gpio == -ENOENT)
  861. break;
  862. return gpio;
  863. }
  864. }
  865. priv->num_gpios = i;
  866. return 0;
  867. }
  868. static int ov1063x_probe(struct i2c_client *client,
  869. const struct i2c_device_id *did)
  870. {
  871. struct device_node *node = client->dev.of_node;
  872. struct ov1063x_priv *priv;
  873. struct v4l2_subdev *sd;
  874. struct clk *clk;
  875. int ret = 0;
  876. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  877. if (!priv)
  878. return -ENOMEM;
  879. i2c_set_clientdata(client, priv);
  880. clk = devm_clk_get(&client->dev, "xvclk");
  881. if (IS_ERR(clk)) {
  882. dev_err(&client->dev, "xvclk reference is missing!\n");
  883. ret = PTR_ERR(clk);
  884. goto err;
  885. }
  886. priv->xvclk = clk;
  887. priv->xvclk_rate = clk_get_rate(clk);
  888. dev_dbg(&client->dev, "xvclk_rate: %d (Hz)\n", priv->xvclk_rate);
  889. if (priv->xvclk_rate < 6000000 ||
  890. priv->xvclk_rate > 27000000) {
  891. ret = -EINVAL;
  892. goto err;
  893. }
  894. ret = clk_prepare_enable(priv->xvclk);
  895. if (ret < 0)
  896. goto err;
  897. ret = ov1063x_of_probe(client, node);
  898. if (ret)
  899. goto err;
  900. /* Default framerate */
  901. priv->fps_numerator = 30;
  902. priv->fps_denominator = 1;
  903. ov1063x_get_default_format(&priv->format);
  904. priv->width = priv->format.width;
  905. priv->height = priv->format.height;
  906. sd = &priv->subdev;
  907. v4l2_i2c_subdev_init(sd, client, &ov1063x_subdev_ops);
  908. sd->internal_ops = &ov1063x_sd_internal_ops;
  909. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  910. V4L2_SUBDEV_FL_HAS_EVENTS;
  911. v4l2_ctrl_handler_init(&priv->hdl, 3);
  912. v4l2_ctrl_new_std(&priv->hdl, &ov1063x_ctrl_ops,
  913. V4L2_CID_VFLIP, 0, 1, 1, 0);
  914. v4l2_ctrl_new_std(&priv->hdl, &ov1063x_ctrl_ops,
  915. V4L2_CID_HFLIP, 0, 1, 1, 0);
  916. priv->colorbar = v4l2_ctrl_new_std_menu_items(&priv->hdl, &ov1063x_ctrl_ops,
  917. V4L2_CID_TEST_PATTERN,
  918. ARRAY_SIZE(ov1063x_test_pattern_menu) - 1,
  919. 0, 0, ov1063x_test_pattern_menu);
  920. priv->subdev.ctrl_handler = &priv->hdl;
  921. if (priv->hdl.error) {
  922. ret = priv->hdl.error;
  923. goto err;
  924. }
  925. mutex_init(&priv->lock);
  926. ret = ov1063x_init_cam_gpios(client);
  927. if (ret) {
  928. dev_err(&client->dev, "Failed to request cam gpios");
  929. goto err;
  930. }
  931. ret = ov1063x_init_gpios(client);
  932. if (ret) {
  933. dev_err(&client->dev, "Failed to request mux gpios");
  934. goto err;
  935. }
  936. ret = ov1063x_video_probe(client);
  937. if (ret) {
  938. v4l2_ctrl_handler_free(&priv->hdl);
  939. goto err;
  940. }
  941. sd->dev = &client->dev;
  942. ret = v4l2_async_register_subdev(sd);
  943. dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
  944. pm_runtime_enable(&client->dev);
  945. return 0;
  946. err:
  947. clk_disable_unprepare(priv->xvclk);
  948. return ret;
  949. }
  950. static int ov1063x_remove(struct i2c_client *client)
  951. {
  952. struct ov1063x_priv *priv = i2c_get_clientdata(client);
  953. v4l2_device_unregister_subdev(&priv->subdev);
  954. v4l2_ctrl_handler_free(&priv->hdl);
  955. ov1063x_set_power(client, false);
  956. clk_disable_unprepare(priv->xvclk);
  957. pm_runtime_disable(&client->dev);
  958. return 0;
  959. }
  960. static const struct i2c_device_id ov1063x_id[] = {
  961. { "ov10635", 0 },
  962. { "ov10633", 0 },
  963. { }
  964. };
  965. MODULE_DEVICE_TABLE(i2c, ov1063x_id);
  966. #if defined(CONFIG_OF)
  967. static const struct of_device_id ov1063x_dt_id[] = {
  968. {
  969. .compatible = "ovti,ov10635", .data = "ov10635"
  970. },
  971. {
  972. .compatible = "ovti,ov10633", .data = "ov10633"
  973. },
  974. {
  975. }
  976. };
  977. #endif
  978. static struct i2c_driver ov1063x_i2c_driver = {
  979. .driver = {
  980. .name = "ov1063x",
  981. .of_match_table = of_match_ptr(ov1063x_dt_id),
  982. },
  983. .probe = ov1063x_probe,
  984. .remove = ov1063x_remove,
  985. .id_table = ov1063x_id,
  986. };
  987. module_i2c_driver(ov1063x_i2c_driver);
  988. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV1063X");
  989. MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
  990. MODULE_LICENSE("GPL v2");