ov1063x.c 25 KB

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