Advertisement
shivashanka908290

Untitled

Nov 26th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.52 KB | None | 0 0
  1. /*
  2. * Driver for the OV5645 camera sensor.
  3. *
  4. * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
  5. * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
  6. * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  7. *
  8. * Based on:
  9. * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
  10. * https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
  11. * media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
  12. * - the OV5640 driver posted on linux-media:
  13. * https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
  14. */
  15.  
  16. /*
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21.  
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. */
  27.  
  28. #include <linux/bitops.h>
  29. #include <linux/clk.h>
  30. #include <linux/delay.h>
  31. #include <linux/device.h>
  32. #include <linux/gpio/consumer.h>
  33. #include <linux/i2c.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/mutex.h>
  37. #include <linux/of.h>
  38. #include <linux/of_graph.h>
  39. #include <linux/regulator/consumer.h>
  40. #include <linux/slab.h>
  41. #include <linux/types.h>
  42. #include <media/v4l2-ctrls.h>
  43. #include <media/v4l2-fwnode.h>
  44. #include <media/v4l2-subdev.h>
  45.  
  46. /* -> Enable this for ov5645 devkit test
  47. * -> Disable this macro for lattice board testing
  48. */
  49. //#define OV5645
  50.  
  51. #define DBG
  52. #ifdef DBG
  53. #define dbg_printk(fmt, args...) printk("Mist Naneye drv:" fmt, ##args)
  54. #else
  55. #define dbg_printk(fmt, args...) while(0)
  56. #endif
  57.  
  58. static DEFINE_MUTEX(ov5645_lock);
  59.  
  60. #define OV5645_VOLTAGE_ANALOG 2800000
  61. #define OV5645_VOLTAGE_DIGITAL_CORE 1500000
  62. #define OV5645_VOLTAGE_DIGITAL_IO 1800000
  63.  
  64. #define OV5645_SYSTEM_CTRL0 0x3008
  65. #define OV5645_SYSTEM_CTRL0_START 0x02
  66. #define OV5645_SYSTEM_CTRL0_STOP 0x42
  67. #define OV5645_CHIP_ID_HIGH 0x300a
  68. #define OV5645_CHIP_ID_HIGH_BYTE 0x56
  69. #define OV5645_CHIP_ID_LOW 0x300b
  70. #define OV5645_CHIP_ID_LOW_BYTE 0x45
  71. #define OV5645_AWB_MANUAL_CONTROL 0x3406
  72. #define OV5645_AWB_MANUAL_ENABLE BIT(0)
  73. #define OV5645_AEC_PK_MANUAL 0x3503
  74. #define OV5645_AEC_MANUAL_ENABLE BIT(0)
  75. #define OV5645_AGC_MANUAL_ENABLE BIT(1)
  76. #define OV5645_TIMING_TC_REG20 0x3820
  77. #define OV5645_SENSOR_VFLIP BIT(1)
  78. #define OV5645_ISP_VFLIP BIT(2)
  79. #define OV5645_TIMING_TC_REG21 0x3821
  80. #define OV5645_SENSOR_MIRROR BIT(1)
  81. #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
  82. #define OV5645_TEST_PATTERN_MASK 0x3
  83. #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
  84. #define OV5645_TEST_PATTERN_ENABLE BIT(7)
  85. #define OV5645_SDE_SAT_U 0x5583
  86. #define OV5645_SDE_SAT_V 0x5584
  87.  
  88. struct reg_value {
  89. u16 reg;
  90. u8 val;
  91. };
  92.  
  93. struct ov5645_mode_info {
  94. u32 width;
  95. u32 height;
  96. const struct reg_value *data;
  97. u32 data_size;
  98. u32 pixel_clock;
  99. u32 link_freq;
  100. };
  101.  
  102. struct ov5645 {
  103. struct i2c_client *i2c_client;
  104. struct device *dev;
  105. struct v4l2_subdev sd;
  106. struct media_pad pad;
  107. struct v4l2_fwnode_endpoint ep;
  108. struct v4l2_mbus_framefmt fmt;
  109. struct v4l2_rect crop;
  110. struct clk *xclk;
  111.  
  112. struct regulator *io_regulator;
  113. struct regulator *core_regulator;
  114. struct regulator *analog_regulator;
  115.  
  116. const struct ov5645_mode_info *current_mode;
  117.  
  118. struct v4l2_ctrl_handler ctrls;
  119. struct v4l2_ctrl *pixel_clock;
  120. struct v4l2_ctrl *link_freq;
  121.  
  122. /* Cached register values */
  123. u8 aec_pk_manual;
  124. u8 timing_tc_reg20;
  125. u8 timing_tc_reg21;
  126.  
  127. struct mutex power_lock; /* lock to protect power state */
  128. int power_count;
  129.  
  130. struct gpio_desc *enable_gpio;
  131. struct gpio_desc *rst_gpio;
  132. };
  133.  
  134. static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
  135. {
  136. return container_of(sd, struct ov5645, sd);
  137. }
  138.  
  139. static const struct reg_value ov5645_global_init_setting[] = {
  140.  
  141. { 0x5805, 0x3f },
  142. { 0x5806, 0x0b },
  143. { 0x5807, 0x06 },
  144. { 0x5808, 0x04 },
  145. { 0x5809, 0x04 },
  146. { 0x580a, 0x06 },
  147. { 0x580b, 0x0b },
  148. { 0x580c, 0x09 },
  149. { 0x580d, 0x03 },
  150. { 0x580e, 0x00 },
  151. { 0x580f, 0x00 },
  152. { 0x5810, 0x03 },
  153. { 0x5811, 0x08 },
  154. { 0x5812, 0x0a },
  155. { 0x5813, 0x03 },
  156. { 0x5814, 0x00 },
  157. { 0x5815, 0x00 },
  158. { 0x5816, 0x04 },
  159. { 0x5817, 0x09 },
  160. { 0x5818, 0x0f },
  161. { 0x5819, 0x08 },
  162. { 0x581a, 0x06 },
  163. { 0x581b, 0x06 },
  164. { 0x581c, 0x08 },
  165. { 0x581d, 0x0c },
  166. { 0x581e, 0x3f },
  167. { 0x581f, 0x1e },
  168. { 0x5820, 0x12 },
  169. { 0x5821, 0x13 },
  170. { 0x5822, 0x21 },
  171. { 0x5823, 0x3f },
  172. { 0x5824, 0x68 },
  173. { 0x5825, 0x28 },
  174. { 0x5826, 0x2c },
  175. { 0x5827, 0x28 },
  176. { 0x5828, 0x08 },
  177. { 0x5829, 0x48 },
  178. { 0x582a, 0x64 },
  179. { 0x582b, 0x62 },
  180. { 0x582c, 0x64 },
  181. { 0x582d, 0x28 },
  182. { 0x582e, 0x46 },
  183. { 0x582f, 0x62 },
  184. { 0x5830, 0x60 },
  185. { 0x5831, 0x62 },
  186. { 0x5832, 0x26 },
  187. { 0x5833, 0x48 },
  188. { 0x5834, 0x66 },
  189. { 0x5835, 0x44 },
  190. { 0x5836, 0x64 },
  191. { 0x5837, 0x28 },
  192. { 0x5838, 0x66 },
  193. { 0x5839, 0x48 },
  194. { 0x583a, 0x2c },
  195. { 0x583b, 0x28 },
  196. { 0x583c, 0x26 },
  197. { 0x583d, 0xae },
  198. { 0x5025, 0x00 },
  199. { 0x3a0f, 0x30 },
  200. { 0x3a10, 0x28 },
  201. { 0x3a1b, 0x30 },
  202. { 0x3a1e, 0x26 },
  203. { 0x3a11, 0x60 },
  204. { 0x3a1f, 0x14 },
  205. { 0x0601, 0x02 },
  206. { 0x3008, 0x42 },
  207. { 0x3008, 0x02 }
  208. };
  209.  
  210. static const struct reg_value ov5645_setting_sxga[] = {
  211. 05 },
  212. { 0x3809, 0x00 },
  213. { 0x380a, 0x03 },
  214. { 0x380b, 0xc0 },
  215. { 0x380c, 0x07 },
  216. { 0x380d, 0x68 },
  217. { 0x380e, 0x03 },
  218. { 0x380f, 0xd8 },
  219. { 0x3813, 0x06 },
  220. { 0x3814, 0x31 },
  221. { 0x3815, 0x31 },
  222. };
  223.  
  224. static const struct reg_value ov5645_setting_1080p[] = {
  225.  
  226. { 0x3808, 0x07 },
  227. { 0x3809, 0x80 },
  228. { 0x380a, 0x04 },
  229. { 0x380b, 0x38 },
  230. { 0x380c, 0x09 },
  231. { 0x380d, 0xc4 },
  232. { 0x380e, 0x04 },
  233. { 0x380f, 0x60 },
  234. { 0x3813, 0x04 },
  235. { 0x3814, 0x11 },
  236. { 0x3815, 0x11 },
  237. { 0x3820, 0x47 },
  238. { 0x4514, 0x88 },
  239. { 0x3a02, 0x04 },
  240. { 0x3a03, 0x60 },
  241. { 0x3a08, 0x01 },
  242. { 0x3a09, 0x50 },
  243. { 0x3a0a, 0x01 },
  244.  
  245. { 0x4837, 0x0b }
  246. };
  247.  
  248. static const struct reg_value ov5645_setting_full[] = {
  249. { 0x3612, 0xab },
  250. { 0x3614, 0x50 },
  251. { 0x3618, 0x04 },
  252. { 0x3034, 0x18 },
  253. { 0x3035, 0x11 },
  254. { 0x3036, 0x54 },
  255. { 0x3600, 0x08 },
  256. { 0x3601, 0x33 },
  257. { 0x3708, 0x63 },
  258. { 0x370c, 0xc0 },
  259. { 0x3800, 0x00 },
  260. { 0x3801, 0x00 },
  261. { 0x3802, 0x00 },
  262. { 0x3803, 0x00 },
  263. { 0x3804, 0x0a },
  264. { 0x3805, 0x3f },
  265. { 0x3806, 0x07 },
  266. { 0x3807, 0x9f },
  267. { 0x3808, 0x0a },
  268. { 0x3809, 0x20 },
  269. { 0x380a, 0x07 },
  270. { 0x380b, 0x98 },
  271. { 0x380c, 0x0b },
  272. { 0x380d, 0x1c },
  273. { 0x380e, 0x07 },
  274. { 0x380f, 0xb0 },
  275. { 0x3813, 0x06 },
  276. { 0x3814, 0x11 },
  277. { 0x3815, 0x11 },
  278. { 0x3820, 0x47 },
  279. { 0x4514, 0x88 },
  280. { 0x3a02, 0x07 },
  281. { 0x3a03, 0xb0 },
  282. { 0x3a08, 0x01 },
  283. { 0x3a09, 0x27 },
  284. { 0x3a0a, 0x00 },
  285. { 0x3a0b, 0xf6 },
  286. { 0x3a0e, 0x06 },
  287. { 0x3a0d, 0x08 },
  288. { 0x3a14, 0x07 },
  289. { 0x3a15, 0xb0 },
  290. { 0x3a18, 0x01 },
  291. { 0x4004, 0x06 },
  292. { 0x4005, 0x18 },
  293. { 0x4300, 0x32},//0xf8 },
  294. { 0x4837, 0x0b },
  295. { 0x4202, 0x00 }
  296. };
  297.  
  298.  
  299. static const struct reg_value ov5645_setting_320_240[] = {
  300. { 0x3612, 0xab },
  301. { 0x3614, 0x50 },
  302. { 0x3618, 0x04 },
  303. x20 },
  304. { 0x380a, 0x07 },
  305. { 0x380b, 0x98 },
  306. { 0x380c, 0x0b },
  307. { 0x380d, 0x1c },
  308. { 0x380e, 0x07 },
  309. { 0x380f, 0xb0 },
  310. { 0x3813, 0x06 },
  311. { 0x3814, 0x11 },
  312. { 0x3815, 0x11 },
  313. { 0x3820, 0x47 },
  314. { 0x4514, 0x88 },
  315. {
  316. { 0x4837, 0x0b },
  317. { 0x4202, 0x00 }
  318. };
  319.  
  320.  
  321.  
  322.  
  323. static const s64 link_freq[] = {
  324. 224000000,
  325. 336000000,
  326. 320000000
  327. };
  328.  
  329. static const struct ov5645_mode_info ov5645_mode_info_data[] = {
  330. //pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample
  331.  
  332. {
  333. .width = 320,
  334. .height = 320,
  335. //.data = ov5645_setting_320_240,
  336. //.data_size = ARRAY_SIZE(ov5645_setting_full),
  337. .pixel_clock = 80000000,
  338. .link_freq = 2 /* an index in link_freq[] */
  339.  
  340. },
  341. {
  342. .width = 1280,
  343. .height = 960,
  344. .data = ov5645_setting_sxga,
  345. .data_size = ARRAY_SIZE(ov5645_setting_sxga),
  346. .pixel_clock = 112000000,
  347. .link_freq = 0 /* an index in link_freq[] */
  348. },
  349. {
  350. .width = 1920,
  351. .height = 1080,
  352. .data = ov5645_setting_1080p,
  353. .data_size = ARRAY_SIZE(ov5645_setting_1080p),
  354. .pixel_clock = 168000000,
  355. .link_freq = 1 /* an index in link_freq[] */
  356. },
  357. {
  358. .width = 2592,
  359. .height = 1944,
  360. .data = ov5645_setting_full,
  361. .data_size = ARRAY_SIZE(ov5645_setting_full),
  362. .pixel_clock = 168000000,
  363. .link_freq = 1 /* an index in link_freq[] */
  364. },
  365. };
  366.  
  367. static int ov5645_regulators_enable(struct ov5645 *ov5645)
  368. {
  369. int ret;
  370.  
  371. ret = regulator_enable(ov5645->io_regulator);
  372. if (ret < 0) {
  373. dev_err(ov5645->dev, "set io voltage failed\n");
  374. return ret;
  375. }
  376.  
  377. return 0;
  378.  
  379. err_disable_analog:
  380. regulator_disable(ov5645->analog_regulator);
  381. err_disable_io:
  382. regulator_disable(ov5645->io_regulator);
  383.  
  384. return ret;
  385. }
  386.  
  387. static void ov5645_regulators_disable(struct ov5645 *ov5645)
  388. {
  389. int ret;
  390.  
  391. ret = regulator_disable(ov5645->core_regulator);
  392. if (ret < 0)
  393. dev_err(ov5645->dev, "core regulator disable failed\n");
  394. }
  395.  
  396. static int ov5645_write_reg_to(struct ov5645 *ov5645, u16 reg, u8 val,
  397. u16 i2c_addr)
  398. {
  399. u8 regbuf[3] = {
  400. reg >> 8,
  401. reg & 0xff,
  402. val
  403. };
  404. struct i2c_msg msgs = {
  405. .addr = i2c_addr,
  406. .flags = 0,
  407. .len = 3,
  408. .buf = regbuf
  409. };
  410. int ret;
  411.  
  412. ret = i2c_transfer(ov5645->i2c_client->adapter, &msgs, 1);
  413. if (ret < 0) {
  414. dev_err(ov5645->dev,
  415. "%s: write reg error %d on addr 0x%x: reg=0x%x, val=0x%x\n",
  416. __func__, ret, i2c_addr, reg, val);
  417. return ret;
  418. }
  419.  
  420. return 0;
  421. }
  422.  
  423. static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
  424. {
  425. u8 regbuf[3];
  426. int ret;
  427.  
  428. regbuf[0] = reg >> 8;
  429. regbuf[1] = reg & 0xff;
  430. regbuf[2] = val;
  431.  
  432. ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
  433. if (ret < 0) {
  434. dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
  435. __func__, ret, reg, val);
  436. return ret;
  437. }
  438.  
  439. return 0;
  440. }
  441.  
  442. static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
  443. {
  444. u8 regbuf[2];
  445. int ret;
  446.  
  447. regbuf[0] = reg >> 8;
  448. regbuf[1] = reg & 0xff;
  449.  
  450. ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
  451. if (ret < 0) {
  452. dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
  453. __func__, ret, reg);
  454. return ret;
  455. }
  456.  
  457. ret = i2c_master_recv(ov5645->i2c_client, val, 1);
  458. if (ret < 0) {
  459. dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
  460. __func__, ret, reg);
  461. return ret;
  462. }
  463.  
  464. return 0;
  465. }
  466.  
  467. static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
  468. {
  469. u8 val = ov5645->aec_pk_manual;
  470. int ret;
  471.  
  472. if (mode == V4L2_EXPOSURE_AUTO)
  473. val &= ~OV5645_AEC_MANUAL_ENABLE;
  474. else /* V4L2_EXPOSURE_MANUAL */
  475. val |= OV5645_AEC_MANUAL_ENABLE;
  476.  
  477. ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
  478. if (!ret)
  479. ov5645->aec_pk_manual = val;
  480.  
  481. return ret;
  482. }
  483.  
  484. static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
  485. {
  486. u8 val = ov5645->aec_pk_manual;
  487. int ret;
  488.  
  489. if (enable)
  490. val &= ~OV5645_AGC_MANUAL_ENABLE;
  491. else
  492. val |= OV5645_AGC_MANUAL_ENABLE;
  493.  
  494. ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
  495. if (!ret)
  496. ov5645->aec_pk_manual = val;
  497.  
  498. return ret;
  499. }
  500.  
  501. static int ov5645_set_register_array(struct ov5645 *ov5645,
  502. const struct reg_value *settings,
  503. unsigned int num_settings)
  504. {
  505. unsigned int i;
  506. int ret;
  507.  
  508. for (i = 0; i < num_settings; ++i, ++settings) {
  509. ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
  510. if (ret < 0)
  511. return ret;
  512. }
  513.  
  514. return 0;
  515. }
  516.  
  517. static int ov5645_set_power_on(struct ov5645 *ov5645)
  518. {
  519. int ret;
  520.  
  521. ret = ov5645_regulators_enable(ov5645);
  522. if (ret < 0) {
  523. return ret;
  524. }
  525.  
  526. ret = clk_prepare_enable(ov5645->xclk);
  527. if (ret < 0) {
  528. dev_err(ov5645->dev, "clk prepare enable failed\n");
  529. ov5645_regulators_disable(ov5645);
  530. return ret;
  531. }
  532.  
  533. usleep_range(5000, 15000);
  534. gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
  535.  
  536. usleep_range(1000, 2000);
  537. gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
  538.  
  539. msleep(20);
  540.  
  541. return 0;
  542. }
  543.  
  544. static void ov5645_set_power_off(struct ov5645 *ov5645)
  545. {
  546. gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
  547. gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
  548. clk_disable_unprepare(ov5645->xclk);
  549. ov5645_regulators_disable(ov5645);
  550. }
  551.  
  552. static int ov5645_s_power(struct v4l2_subdev *sd, int on)
  553. {
  554. dbg_printk("---- start of %s ---- \n", __func__);
  555.  
  556. #ifdef OV5645
  557. struct ov5645 *ov5645 = to_ov5645(sd);
  558. int ret = 0;
  559.  
  560. mutex_lock(&ov5645->power_lock);
  561.  
  562. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  563. * update the power state.
  564. */
  565. if (ov5645->power_count == !on) {
  566. if (on) {
  567. mutex_lock(&ov5645_lock);
  568.  
  569. ret = ov5645_set_power_on(ov5645);
  570. if (ret < 0) {
  571. mutex_unlock(&ov5645_lock);
  572. goto exit;
  573. }
  574.  
  575. ret = ov5645_write_reg_to(ov5645, 0x3100,
  576. ov5645->i2c_client->addr << 1, 0x3c);
  577. if (ret < 0) {
  578. dev_err(ov5645->dev,
  579. "could not change i2c address\n");
  580. ov5645_set_power_off(ov5645);
  581. mutex_unlock(&ov5645_lock);
  582. goto exit;
  583. }
  584.  
  585. mutex_unlock(&ov5645_lock);
  586.  
  587. ret = ov5645_set_register_array(ov5645,
  588. ov5645_global_init_setting,
  589. ARRAY_SIZE(ov5645_global_init_setting));
  590. if (ret < 0) {
  591. dev_err(ov5645->dev,
  592. "could not set init registers\n");
  593. ov5645_set_power_off(ov5645);
  594. goto exit;
  595. }
  596.  
  597. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  598. OV5645_SYSTEM_CTRL0_STOP);
  599. if (ret < 0) {
  600. ov5645_set_power_off(ov5645);
  601. goto exit;
  602. }
  603. } else {
  604. ov5645_set_power_off(ov5645);
  605. }
  606. }
  607.  
  608. /* Update the power count. */
  609. ov5645->power_count += on ? 1 : -1;
  610. WARN_ON(ov5645->power_count < 0);
  611.  
  612. exit:
  613. mutex_unlock(&ov5645->power_lock);
  614.  
  615. return ret;
  616. #endif
  617.  
  618. dbg_printk("---- end of %s ---- \n", __func__);
  619. }
  620.  
  621. static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
  622. {
  623. u32 reg_value = (value * 0x10) + 0x40;
  624. int ret;
  625.  
  626. ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
  627. if (ret < 0)
  628. return ret;
  629.  
  630. return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
  631. }
  632.  
  633. static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
  634. {
  635. u8 val = ov5645->timing_tc_reg21;
  636. int ret;
  637.  
  638. if (value == 0)
  639. val &= ~(OV5645_SENSOR_MIRROR);
  640. else
  641. val |= (OV5645_SENSOR_MIRROR);
  642.  
  643. ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
  644. if (!ret)
  645. ov5645->timing_tc_reg21 = val;
  646.  
  647. return ret;
  648. }
  649.  
  650. static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
  651. {
  652. u8 val = ov5645->timing_tc_reg20;
  653. int ret;
  654.  
  655. if (value == 0)
  656. val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
  657. else
  658. val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
  659.  
  660. ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
  661. if (!ret)
  662. ov5645->timing_tc_reg20 = val;
  663.  
  664. return ret;
  665. }
  666.  
  667. static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
  668. {
  669. u8 val = 0;
  670.  
  671. if (value) {
  672. val = OV5645_SET_TEST_PATTERN(value - 1);
  673. val |= OV5645_TEST_PATTERN_ENABLE;
  674. }
  675.  
  676. return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
  677. }
  678.  
  679. static const char * const ov5645_test_pattern_menu[] = {
  680. "Disabled",
  681. "Vertical Color Bars",
  682. "Pseudo-Random Data",
  683. "Color Square",
  684. "Black Image",
  685. };
  686.  
  687. static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
  688. {
  689. u8 val = 0;
  690.  
  691. if (!enable_auto)
  692. val = OV5645_AWB_MANUAL_ENABLE;
  693.  
  694. return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
  695. }
  696.  
  697. static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
  698. {
  699. struct ov5645 *ov5645 = container_of(ctrl->handler,
  700. struct ov5645, ctrls);
  701. int ret;
  702.  
  703. mutex_lock(&ov5645->power_lock);
  704. if (!ov5645->power_count) {
  705. mutex_unlock(&ov5645->power_lock);
  706. return 0;
  707. }
  708. case V4L2_CID_VFLIP:
  709. ret = ov5645_set_vflip(ov5645, ctrl->val);
  710. break;
  711. default:
  712. ret = -EINVAL;
  713. break;
  714. }
  715.  
  716. mutex_unlock(&ov5645->power_lock);
  717.  
  718. return ret;
  719. }
  720.  
  721. static struct v4l2_ctrl_ops ov5645_ctrl_ops = {
  722. .s_ctrl = ov5645_s_ctrl,
  723. };
  724.  
  725. static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
  726. struct v4l2_subdev_pad_config *cfg,
  727. struct v4l2_subdev_mbus_code_enum *code)
  728. {
  729. if (code->index > 0)
  730. return -EINVAL;
  731.  
  732. code->code = MEDIA_BUS_FMT_UYVY8_2X8; //MEDIA_BUS_FMT_SBGGR8_1X8;
  733.  
  734. return 0;
  735. }
  736.  
  737. static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
  738. struct v4l2_subdev_pad_config *cfg,
  739. struct v4l2_subdev_frame_size_enum *fse)
  740. {
  741. if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)// MEDIA_BUS_FMT_SBGGR8_1X8)
  742. return -EINVAL;
  743.  
  744. if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
  745. return -EINVAL;
  746.  
  747. fse->min_width = ov5645_mode_info_data[fse->index].width;
  748. fse->max_width = ov5645_mode_info_data[fse->index].width;
  749. fse->min_height = ov5645_mode_info_data[fse->index].height;
  750. fse->max_height = ov5645_mode_info_data[fse->index].height;
  751.  
  752. return 0;
  753. }
  754.  
  755. static struct v4l2_mbus_framefmt *
  756. __ov5645_get_pad_format(struct ov5645 *ov5645,
  757. struct v4l2_subdev_pad_config *cfg,
  758. unsigned int pad,
  759. enum v4l2_subdev_format_whence which)
  760. {
  761. switch (which) {
  762. case V4L2_SUBDEV_FORMAT_TRY:
  763. return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
  764. case V4L2_SUBDEV_FORMAT_ACTIVE:
  765. return &ov5645->fmt;
  766. default:
  767. return NULL;
  768. }
  769. }
  770.  
  771. static int ov5645_get_format(struct v4l2_subdev *sd,
  772. struct v4l2_subdev_pad_config *cfg,
  773. struct v4l2_subdev_format *format)
  774. {
  775. struct ov5645 *ov5645 = to_ov5645(sd);
  776.  
  777. format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
  778. format->which);
  779. return 0;
  780. }
  781.  
  782. static struct v4l2_rect *
  783. __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
  784. unsigned int pad, enum v4l2_subdev_format_whence which)
  785. {
  786. switch (which) {
  787. case V4L2_SUBDEV_FORMAT_TRY:
  788. return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
  789. case V4L2_SUBDEV_FORMAT_ACTIVE:
  790. return &ov5645->crop;
  791. default:
  792. return NULL;
  793. }
  794. }
  795.  
  796. static const struct ov5645_mode_info *
  797. ov5645_find_nearest_mode(unsigned int width, unsigned int height)
  798. {
  799. unsigned int max_dist_match = (unsigned int) -1;
  800. int i, n = 0;
  801.  
  802. for (i = 0; i < ARRAY_SIZE(ov5645_mode_info_data); i++) {
  803. unsigned int dist = min(width, ov5645_mode_info_data[i].width)
  804. * min(height, ov5645_mode_info_data[i].height);
  805.  
  806. dist = ov5645_mode_info_data[i].width *
  807. ov5645_mode_info_data[i].height
  808. + width * height - 2 * dist;
  809.  
  810. if (dist < max_dist_match) {
  811. n = i;
  812. max_dist_match = dist;
  813. }
  814. }
  815.  
  816. printk(" &ov5645_mode_info_data[0] = %p\n", &ov5645_mode_info_data[0]);
  817. printk(" &ov5645_mode_info_data[1] = %p\n", &ov5645_mode_info_data[1]);
  818. printk(" &ov5645_mode_info_data[2] = %p\n", &ov5645_mode_info_data[2]);
  819. printk(" &ov5645_mode_info_data[n] = %p\n", &ov5645_mode_info_data[n]);
  820.  
  821. return &ov5645_mode_info_data[n];
  822. }
  823.  
  824. static int ov5645_set_format(struct v4l2_subdev *sd,
  825. struct v4l2_subdev_pad_config *cfg,
  826. struct v4l2_subdev_format *format)
  827. {
  828. dbg_printk("---- start of %s ---- \n", __func__);
  829.  
  830. struct ov5645 *ov5645 = to_ov5645(sd);
  831. struct v4l2_mbus_framefmt *__format;
  832. struct v4l2_rect *__crop;
  833. const struct ov5645_mode_info *new_mode;
  834. int ret;
  835.  
  836. __crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
  837. format->which);
  838.  
  839. new_mode = ov5645_find_nearest_mode(format->format.width,
  840. format->format.height);
  841. __crop->width = new_mode->width;
  842. __crop->height = new_mode->height;
  843.  
  844. printk("--------width = %d-----\n", __crop->width);
  845. printk("--------heigth = %d-----\n", __crop->height);
  846. printk("--------pixel clock = %d ---\n", new_mode->pixel_clock);
  847. printk("--------link freq = %d ---\n", new_mode->link_freq);
  848.  
  849. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  850. ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
  851. new_mode->pixel_clock);
  852. if (ret < 0)
  853. return ret;
  854.  
  855. ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
  856. new_mode->link_freq);
  857. if (ret < 0)
  858. return ret;
  859.  
  860. ov5645->current_mode = new_mode;
  861. }
  862.  
  863. __format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
  864. format->which);
  865. __format->width = __crop->width;
  866. __format->height = __crop->height;
  867. printk("-----2nd---width = %d-----\n", __crop->width);
  868. printk("-----2nd---heigth = %d-----\n", __crop->height);
  869.  
  870. __format->code = MEDIA_BUS_FMT_UYVY8_2X8; //MEDIA_BUS_FMT_SBGGR8_1X8;
  871. __format->field = V4L2_FIELD_NONE;
  872. __format->colorspace = V4L2_COLORSPACE_SRGB;
  873.  
  874. format->format = *__format;
  875.  
  876. dbg_printk("---- end of %s ---- \n", __func__);
  877. return 0;
  878. }
  879.  
  880. static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
  881. struct v4l2_subdev_pad_config *cfg)
  882. {
  883. dbg_printk("---- start of %s ---- \n", __func__);
  884. struct v4l2_subdev_format fmt = { 0 };
  885.  
  886. fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  887. fmt.format.width = 320;//1920;
  888. fmt.format.height = 320;//1080;
  889. //#ifdef OV5645
  890. ov5645_set_format(subdev, cfg, &fmt);
  891. //#endif
  892.  
  893. dbg_printk("---- end of %s ---- \n", __func__);
  894. return 0;
  895. }
  896.  
  897. static int ov5645_get_selection(struct v4l2_subdev *sd,
  898. struct v4l2_subdev_pad_config *cfg,
  899. struct v4l2_subdev_selection *sel)
  900. {
  901. dbg_printk("---- start of %s ---- \n", __func__);
  902. struct ov5645 *ov5645 = to_ov5645(sd);
  903.  
  904. if (sel->target != V4L2_SEL_TGT_CROP)
  905. return -EINVAL;
  906.  
  907. sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
  908. sel->which);
  909. dbg_printk("---- end of %s ---- \n", __func__);
  910. return 0;
  911. }
  912.  
  913. static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
  914. {
  915. dbg_printk("---- start of %s ---- \n", __func__);
  916.  
  917. #ifdef OV5645
  918. struct ov5645 *ov5645 = to_ov5645(subdev);
  919. int ret;
  920.  
  921. if (enable) {
  922. ret = ov5645_set_register_array(ov5645,
  923. ov5645->current_mode->data,
  924. ov5645->current_mode->data_size);
  925. if (ret < 0) {
  926. dev_err(ov5645->dev, "could not set mode %dx%d\n",
  927. ov5645->current_mode->width,
  928. ov5645->current_mode->height);
  929. return ret;
  930. }
  931. ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
  932. if (ret < 0) {
  933. dev_err(ov5645->dev, "could not sync v4l2 controls\n");
  934. return ret;
  935. }
  936. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  937. OV5645_SYSTEM_CTRL0_START);
  938. if (ret < 0)
  939. return ret;
  940. } else {
  941. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  942. OV5645_SYSTEM_CTRL0_STOP);
  943. if (ret < 0)
  944. return ret;
  945. }
  946.  
  947. #endif
  948.  
  949. dbg_printk("---- end of %s ---- \n", __func__);
  950. return 0;
  951. }
  952.  
  953. static const struct v4l2_subdev_core_ops ov5645_core_ops = {
  954. .s_power = ov5645_s_power,
  955. };
  956.  
  957. static const struct v4l2_subdev_video_ops ov5645_video_ops = {
  958. .s_stream = ov5645_s_stream,
  959. };
  960.  
  961. static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
  962. .init_cfg = ov5645_entity_init_cfg,
  963. .enum_mbus_code = ov5645_enum_mbus_code,
  964. .enum_frame_size = ov5645_enum_frame_size,
  965. .get_fmt = ov5645_get_format,
  966. .set_fmt = ov5645_set_format,
  967. .get_selection = ov5645_get_selection,
  968. };
  969.  
  970. static const struct v4l2_subdev_ops ov5645_subdev_ops = {
  971. .core = &ov5645_core_ops,
  972. .video = &ov5645_video_ops,
  973. .pad = &ov5645_subdev_pad_ops,
  974. };
  975.  
  976. static int ov5645_probe(struct i2c_client *client,
  977. const struct i2c_device_id *id)
  978. {
  979. dbg_printk("---- start of %s ---- \n", __func__);
  980. struct device *dev = &client->dev;
  981. struct device_node *endpoint;
  982. struct ov5645 *ov5645;
  983. u8 chip_id_high, chip_id_low;
  984. u32 xclk_freq;
  985. int ret;
  986.  
  987. ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
  988. if (!ov5645)
  989. return -ENOMEM;
  990.  
  991. ov5645->i2c_client = client;
  992. ov5645->dev = dev;
  993.  
  994. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  995. if (!endpoint) {
  996. dev_err(dev, "endpoint node not found\n");
  997. return -EINVAL;
  998. }
  999.  
  1000. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
  1001. &ov5645->ep);
  1002.  
  1003.  
  1004.  
  1005.  
  1006. of_node_put(endpoint);
  1007.  
  1008. if (ret < 0) {
  1009. dev_err(dev, "parsing endpoint node failed\n");
  1010. return ret;
  1011. }
  1012.  
  1013. if (ov5645->ep.bus_type != V4L2_MBUS_CSI2) {
  1014. dev_err(dev, "invalid bus type, must be CSI2\n");
  1015. return -EINVAL;
  1016. }
  1017.  
  1018. /* get system clock (xclk) */
  1019. ov5645->xclk = devm_clk_get(dev, "xclk");
  1020. if (IS_ERR(ov5645->xclk)) {
  1021. dev_err(dev, "could not get xclk");
  1022. return PTR_ERR(ov5645->xclk);
  1023. }
  1024.  
  1025. ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
  1026. if (ret) {
  1027. dev_err(dev, "could not get xclk frequency\n");
  1028. return ret;
  1029. }
  1030.  
  1031. #ifdef OV5645
  1032. /* external clock must be 24MHz, allow 1% tolerance */
  1033. if (xclk_freq < 23760000 || xclk_freq > 24240000) {
  1034. dev_err(dev, "external clock frequency %u is not supported\n",
  1035. xclk_freq);
  1036. return -EINVAL;
  1037. }
  1038. #endif
  1039.  
  1040. ret = clk_set_rate(ov5645->xclk, xclk_freq);
  1041. if (ret) {
  1042. dev_err(dev, "could not set xclk frequency\n");
  1043. return ret;
  1044. }
  1045.  
  1046. ov5645->io_regulator = devm_regulator_get(dev, "vdddo");
  1047. if (IS_ERR(ov5645->io_regulator)) {
  1048. dev_err(dev, "cannot get io regulator\n");
  1049. return PTR_ERR(ov5645->io_regulator);
  1050. }
  1051.  
  1052. ret = regulator_set_voltage(ov5645->io_regulator,
  1053. OV5645_VOLTAGE_DIGITAL_IO,
  1054. OV5645_VOLTAGE_DIGITAL_IO);
  1055. if (ret < 0) {
  1056. dev_err(dev, "cannot set io voltage\n");
  1057. return ret;
  1058. }
  1059.  
  1060. ov5645->core_regulator = devm_regulator_get(dev, "vddd");
  1061. if (IS_ERR(ov5645->core_regulator)) {
  1062. dev_err(dev, "cannot get core regulator\n");
  1063. return PTR_ERR(ov5645->core_regulator);
  1064. }
  1065.  
  1066. ret = regulator_set_voltage(ov5645->core_regulator,
  1067. OV5645_VOLTAGE_DIGITAL_CORE,
  1068. OV5645_VOLTAGE_DIGITAL_CORE);
  1069. if (ret < 0) {
  1070. dev_err(dev, "cannot set core voltage\n");
  1071. return ret;
  1072. }
  1073.  
  1074. ov5645->analog_regulator = devm_regulator_get(dev, "vdda");
  1075. if (IS_ERR(ov5645->analog_regulator)) {
  1076. dev_err(dev, "cannot get analog regulator\n");
  1077. return PTR_ERR(ov5645->analog_regulator);
  1078. }
  1079.  
  1080. ret = regulator_set_voltage(ov5645->analog_regulator,
  1081. OV5645_VOLTAGE_ANALOG,
  1082. OV5645_VOLTAGE_ANALOG);
  1083. if (ret < 0) {
  1084. dev_err(dev, "cannot set analog voltage\n");
  1085. return ret;
  1086. }
  1087.  
  1088. ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
  1089. if (IS_ERR(ov5645->enable_gpio)) {
  1090. dev_err(dev, "cannot get enable gpio\n");
  1091. return PTR_ERR(ov5645->enable_gpio);
  1092. }
  1093.  
  1094. ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1095. if (IS_ERR(ov5645->rst_gpio)) {
  1096. dev_err(dev, "cannot get reset gpio\n");
  1097. return PTR_ERR(ov5645->rst_gpio);
  1098. }
  1099.  
  1100. mutex_init(&ov5645->power_lock);
  1101.  
  1102. v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
  1103. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1104. V4L2_CID_SATURATION, -4, 4, 1, 0);
  1105. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1106. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1107. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1108. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1109. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1110. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1111. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1112. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  1113. v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
  1114. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
  1115. 0, V4L2_EXPOSURE_AUTO);
  1116. v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
  1117. V4L2_CID_TEST_PATTERN,
  1118. ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
  1119. 0, 0, ov5645_test_pattern_menu);
  1120. ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
  1121. &ov5645_ctrl_ops,
  1122. V4L2_CID_PIXEL_RATE,
  1123. 1, INT_MAX, 1, 1);
  1124. ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
  1125. &ov5645_ctrl_ops,
  1126. V4L2_CID_LINK_FREQ,
  1127. ARRAY_SIZE(link_freq) - 1,
  1128. 0, link_freq);
  1129. if (ov5645->link_freq)
  1130. ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1131.  
  1132. ov5645->sd.ctrl_handler = &ov5645->ctrls;
  1133.  
  1134. if (ov5645->ctrls.error) {
  1135. dev_err(dev, "%s: control initialization error %d\n",
  1136. __func__, ov5645->ctrls.error);
  1137. ret = ov5645->ctrls.error;
  1138. goto free_ctrl;
  1139. }
  1140.  
  1141. v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
  1142. ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1143. ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
  1144. ov5645->sd.dev = &client->dev;
  1145. ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1146.  
  1147. ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
  1148. if (ret < 0) {
  1149. dev_err(dev, "could not register media entity\n");
  1150. goto free_ctrl;
  1151. }
  1152.  
  1153. //FPGA I2C core read/write operation
  1154. /* for ( ix = 0; ix < 0xf; i+=2) {
  1155. ret = ov5645_read_reg(ov5645, i, &xval);
  1156. printk("---xval = %x ---\n", xval);
  1157. }
  1158. */
  1159.  
  1160.  
  1161. #ifdef OV5645
  1162. ret = ov5645_s_power(&ov5645->sd, true);
  1163. if (ret < 0) {
  1164. dev_err(dev, "could not power up OV5645\n");
  1165. goto free_entity;
  1166. }
  1167.  
  1168.  
  1169. ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
  1170. if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
  1171. dev_err(dev, "could not read ID high\n");
  1172. ret = -ENODEV;
  1173. goto power_down;
  1174. }
  1175. ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
  1176. if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
  1177. dev_err(dev, "could not read ID low\n");
  1178. ret = -ENODEV;
  1179. goto power_down;
  1180. }
  1181.  
  1182. dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
  1183.  
  1184. ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
  1185. &ov5645->aec_pk_manual);
  1186. if (ret < 0) {
  1187. dev_err(dev, "could not read AEC/AGC mode\n");
  1188. ret = -ENODEV;
  1189. goto power_down;
  1190. }
  1191.  
  1192. ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
  1193. &ov5645->timing_tc_reg20);
  1194. if (ret < 0) {
  1195. dev_err(dev, "could not read vflip value\n");
  1196. ret = -ENODEV;
  1197. goto power_down;
  1198. }
  1199.  
  1200. ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
  1201. &ov5645->timing_tc_reg21);
  1202. if (ret < 0) {
  1203. dev_err(dev, "could not read hflip value\n");
  1204. ret = -ENODEV;
  1205. goto power_down;
  1206. }
  1207.  
  1208. ov5645_s_power(&ov5645->sd, false);
  1209.  
  1210. #endif
  1211. ret = v4l2_async_register_subdev(&ov5645->sd);
  1212. if (ret < 0) {
  1213. dev_err(dev, "could not register v4l2 device\n");
  1214. goto free_entity;
  1215. }
  1216.  
  1217. //#ifdef OV5645
  1218. ov5645_entity_init_cfg(&ov5645->sd, NULL);
  1219. //#endif
  1220. return 0;
  1221.  
  1222. power_down:
  1223. ov5645_s_power(&ov5645->sd, false);
  1224. free_entity:
  1225. media_entity_cleanup(&ov5645->sd.entity);
  1226. free_ctrl:
  1227. v4l2_ctrl_handler_free(&ov5645->ctrls);
  1228. mutex_destroy(&ov5645->power_lock);
  1229.  
  1230. dbg_printk("---- End of %s ---- \n", __func__);
  1231. return ret;
  1232. }
  1233.  
  1234. static int ov5645_remove(struct i2c_client *client)
  1235. {
  1236. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1237. struct ov5645 *ov5645 = to_ov5645(sd);
  1238.  
  1239. v4l2_async_unregister_subdev(&ov5645->sd);
  1240. media_entity_cleanup(&ov5645->sd.entity);
  1241. v4l2_ctrl_handler_free(&ov5645->ctrls);
  1242. mutex_destroy(&ov5645->power_lock);
  1243.  
  1244. return 0;
  1245. }
  1246.  
  1247. static const struct i2c_device_id ov5645_id[] = {
  1248. { "ov5645", 0 },
  1249. {}
  1250. };
  1251. MODULE_DEVICE_TABLE(i2c, ov5645_id);
  1252.  
  1253. static const struct of_device_id ov5645_of_match[] = {
  1254. { .compatible = "ovti,ov5645" },
  1255. { /* sentinel */ }
  1256. };
  1257. MODULE_DEVICE_TABLE(of, ov5645_of_match);
  1258.  
  1259. static struct i2c_driver ov5645_i2c_driver = {
  1260. .driver = {
  1261. .of_match_table = of_match_ptr(ov5645_of_match),
  1262. .name = "ov5645",
  1263. },
  1264. .probe = ov5645_probe,
  1265. .remove = ov5645_remove,
  1266. .id_table = ov5645_id,
  1267. };
  1268.  
  1269. module_i2c_driver(ov5645_i2c_driver);
  1270.  
  1271. MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
  1272. MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
  1273. MODULE_LICENSE("GPL v2");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement