ov5647.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * A V4L2 driver for OmniVision OV5647 cameras.
  3. *
  4. * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
  5. * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * Based on Omnivision OV7670 Camera Driver
  8. * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
  9. *
  10. * Copyright (C) 2016, Synopsys, Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation version 2.
  15. *
  16. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  17. * kind, whether express or implied; without even the implied warranty
  18. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/videodev2.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-image-sizes.h>
  31. #include <media/v4l2-mediabus.h>
  32. #include <media/v4l2-of.h>
  33. #define SENSOR_NAME "ov5647"
  34. #define OV5647_SW_RESET 0x0103
  35. #define OV5647_REG_CHIPID_H 0x300A
  36. #define OV5647_REG_CHIPID_L 0x300B
  37. #define REG_TERM 0xfffe
  38. #define VAL_TERM 0xfe
  39. #define REG_DLY 0xffff
  40. #define OV5647_ROW_START 0x01
  41. #define OV5647_ROW_START_MIN 0
  42. #define OV5647_ROW_START_MAX 2004
  43. #define OV5647_ROW_START_DEF 54
  44. #define OV5647_COLUMN_START 0x02
  45. #define OV5647_COLUMN_START_MIN 0
  46. #define OV5647_COLUMN_START_MAX 2750
  47. #define OV5647_COLUMN_START_DEF 16
  48. #define OV5647_WINDOW_HEIGHT 0x03
  49. #define OV5647_WINDOW_HEIGHT_MIN 2
  50. #define OV5647_WINDOW_HEIGHT_MAX 2006
  51. #define OV5647_WINDOW_HEIGHT_DEF 1944
  52. #define OV5647_WINDOW_WIDTH 0x04
  53. #define OV5647_WINDOW_WIDTH_MIN 2
  54. #define OV5647_WINDOW_WIDTH_MAX 2752
  55. #define OV5647_WINDOW_WIDTH_DEF 2592
  56. struct regval_list {
  57. u16 addr;
  58. u8 data;
  59. };
  60. struct ov5647 {
  61. struct v4l2_subdev sd;
  62. struct media_pad pad;
  63. struct mutex lock;
  64. struct v4l2_mbus_framefmt format;
  65. unsigned int width;
  66. unsigned int height;
  67. int power_count;
  68. struct clk *xclk;
  69. };
  70. static inline struct ov5647 *to_state(struct v4l2_subdev *sd)
  71. {
  72. return container_of(sd, struct ov5647, sd);
  73. }
  74. static struct regval_list sensor_oe_disable_regs[] = {
  75. {0x3000, 0x00},
  76. {0x3001, 0x00},
  77. {0x3002, 0x00},
  78. };
  79. static struct regval_list sensor_oe_enable_regs[] = {
  80. {0x3000, 0x0f},
  81. {0x3001, 0xff},
  82. {0x3002, 0xe4},
  83. };
  84. static struct regval_list ov5647_640x480[] = {
  85. {0x0100, 0x00},
  86. {0x0103, 0x01},
  87. {0x3034, 0x08},
  88. {0x3035, 0x21},
  89. {0x3036, 0x46},
  90. {0x303c, 0x11},
  91. {0x3106, 0xf5},
  92. {0x3821, 0x07},
  93. {0x3820, 0x41},
  94. {0x3827, 0xec},
  95. {0x370c, 0x0f},
  96. {0x3612, 0x59},
  97. {0x3618, 0x00},
  98. {0x5000, 0x06},
  99. {0x5001, 0x01},
  100. {0x5002, 0x41},
  101. {0x5003, 0x08},
  102. {0x5a00, 0x08},
  103. {0x3000, 0x00},
  104. {0x3001, 0x00},
  105. {0x3002, 0x00},
  106. {0x3016, 0x08},
  107. {0x3017, 0xe0},
  108. {0x3018, 0x44},
  109. {0x301c, 0xf8},
  110. {0x301d, 0xf0},
  111. {0x3a18, 0x00},
  112. {0x3a19, 0xf8},
  113. {0x3c01, 0x80},
  114. {0x3b07, 0x0c},
  115. {0x380c, 0x07},
  116. {0x380d, 0x68},
  117. {0x380e, 0x03},
  118. {0x380f, 0xd8},
  119. {0x3814, 0x31},
  120. {0x3815, 0x31},
  121. {0x3708, 0x64},
  122. {0x3709, 0x52},
  123. {0x3808, 0x02},
  124. {0x3809, 0x80},
  125. {0x380a, 0x01},
  126. {0x380b, 0xE0},
  127. {0x3801, 0x00},
  128. {0x3802, 0x00},
  129. {0x3803, 0x00},
  130. {0x3804, 0x0a},
  131. {0x3805, 0x3f},
  132. {0x3806, 0x07},
  133. {0x3807, 0xa1},
  134. {0x3811, 0x08},
  135. {0x3813, 0x02},
  136. {0x3630, 0x2e},
  137. {0x3632, 0xe2},
  138. {0x3633, 0x23},
  139. {0x3634, 0x44},
  140. {0x3636, 0x06},
  141. {0x3620, 0x64},
  142. {0x3621, 0xe0},
  143. {0x3600, 0x37},
  144. {0x3704, 0xa0},
  145. {0x3703, 0x5a},
  146. {0x3715, 0x78},
  147. {0x3717, 0x01},
  148. {0x3731, 0x02},
  149. {0x370b, 0x60},
  150. {0x3705, 0x1a},
  151. {0x3f05, 0x02},
  152. {0x3f06, 0x10},
  153. {0x3f01, 0x0a},
  154. {0x3a08, 0x01},
  155. {0x3a09, 0x27},
  156. {0x3a0a, 0x00},
  157. {0x3a0b, 0xf6},
  158. {0x3a0d, 0x04},
  159. {0x3a0e, 0x03},
  160. {0x3a0f, 0x58},
  161. {0x3a10, 0x50},
  162. {0x3a1b, 0x58},
  163. {0x3a1e, 0x50},
  164. {0x3a11, 0x60},
  165. {0x3a1f, 0x28},
  166. {0x4001, 0x02},
  167. {0x4004, 0x02},
  168. {0x4000, 0x09},
  169. {0x4837, 0x24},
  170. {0x4050, 0x6e},
  171. {0x4051, 0x8f},
  172. {0x0100, 0x01},
  173. };
  174. static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
  175. {
  176. int ret;
  177. unsigned char data[3] = { reg >> 8, reg & 0xff, val};
  178. struct i2c_client *client = v4l2_get_subdevdata(sd);
  179. ret = i2c_master_send(client, data, 3);
  180. if (ret < 0)
  181. dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
  182. __func__, reg);
  183. return ret;
  184. }
  185. static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
  186. {
  187. int ret;
  188. unsigned char data_w[2] = { reg >> 8, reg & 0xff };
  189. struct i2c_client *client = v4l2_get_subdevdata(sd);
  190. ret = i2c_master_send(client, data_w, 2);
  191. if (ret < 0) {
  192. dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
  193. __func__, reg);
  194. return ret;
  195. }
  196. ret = i2c_master_recv(client, val, 1);
  197. if (ret < 0)
  198. dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
  199. __func__, reg);
  200. return ret;
  201. }
  202. static int ov5647_write_array(struct v4l2_subdev *sd,
  203. struct regval_list *regs, int array_size)
  204. {
  205. int i, ret;
  206. for (i = 0; i < array_size; i++) {
  207. ret = ov5647_write(sd, regs[i].addr, regs[i].data);
  208. if (ret < 0)
  209. return ret;
  210. }
  211. return 0;
  212. }
  213. static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
  214. {
  215. u8 channel_id;
  216. int ret;
  217. ret = ov5647_read(sd, 0x4814, &channel_id);
  218. if (ret < 0)
  219. return ret;
  220. channel_id &= ~(3 << 6);
  221. return ov5647_write(sd, 0x4814, channel_id | (channel << 6));
  222. }
  223. static int ov5647_stream_on(struct v4l2_subdev *sd)
  224. {
  225. int ret;
  226. ret = ov5647_write(sd, 0x4202, 0x00);
  227. if (ret < 0)
  228. return ret;
  229. return ov5647_write(sd, 0x300D, 0x00);
  230. }
  231. static int ov5647_stream_off(struct v4l2_subdev *sd)
  232. {
  233. int ret;
  234. ret = ov5647_write(sd, 0x4202, 0x0f);
  235. if (ret < 0)
  236. return ret;
  237. return ov5647_write(sd, 0x300D, 0x01);
  238. }
  239. static int set_sw_standby(struct v4l2_subdev *sd, bool standby)
  240. {
  241. int ret;
  242. u8 rdval;
  243. ret = ov5647_read(sd, 0x0100, &rdval);
  244. if (ret < 0)
  245. return ret;
  246. if (standby)
  247. rdval &= ~0x01;
  248. else
  249. rdval |= 0x01;
  250. return ov5647_write(sd, 0x0100, rdval);
  251. }
  252. static int __sensor_init(struct v4l2_subdev *sd)
  253. {
  254. int ret;
  255. u8 resetval, rdval;
  256. struct i2c_client *client = v4l2_get_subdevdata(sd);
  257. ret = ov5647_read(sd, 0x0100, &rdval);
  258. if (ret < 0)
  259. return ret;
  260. ret = ov5647_write_array(sd, ov5647_640x480,
  261. ARRAY_SIZE(ov5647_640x480));
  262. if (ret < 0) {
  263. dev_err(&client->dev, "write sensor default regs error\n");
  264. return ret;
  265. }
  266. ret = ov5647_set_virtual_channel(sd, 0);
  267. if (ret < 0)
  268. return ret;
  269. ret = ov5647_read(sd, 0x0100, &resetval);
  270. if (ret < 0)
  271. return ret;
  272. if (!(resetval & 0x01)) {
  273. dev_err(&client->dev, "Device was in SW standby");
  274. ret = ov5647_write(sd, 0x0100, 0x01);
  275. if (ret < 0)
  276. return ret;
  277. }
  278. return ov5647_write(sd, 0x4800, 0x04);
  279. }
  280. static int ov5647_sensor_power(struct v4l2_subdev *sd, int on)
  281. {
  282. int ret = 0;
  283. struct ov5647 *ov5647 = to_state(sd);
  284. struct i2c_client *client = v4l2_get_subdevdata(sd);
  285. mutex_lock(&ov5647->lock);
  286. if (on && !ov5647->power_count) {
  287. dev_dbg(&client->dev, "OV5647 power on\n");
  288. ret = clk_prepare_enable(ov5647->xclk);
  289. if (ret < 0) {
  290. dev_err(&client->dev, "clk prepare enable failed\n");
  291. goto out;
  292. }
  293. ret = ov5647_write_array(sd, sensor_oe_enable_regs,
  294. ARRAY_SIZE(sensor_oe_enable_regs));
  295. if (ret < 0) {
  296. clk_disable_unprepare(ov5647->xclk);
  297. dev_err(&client->dev,
  298. "write sensor_oe_enable_regs error\n");
  299. goto out;
  300. }
  301. ret = __sensor_init(sd);
  302. if (ret < 0) {
  303. clk_disable_unprepare(ov5647->xclk);
  304. dev_err(&client->dev,
  305. "Camera not available, check Power\n");
  306. goto out;
  307. }
  308. } else if (!on && ov5647->power_count == 1) {
  309. dev_dbg(&client->dev, "OV5647 power off\n");
  310. ret = ov5647_write_array(sd, sensor_oe_disable_regs,
  311. ARRAY_SIZE(sensor_oe_disable_regs));
  312. if (ret < 0)
  313. dev_dbg(&client->dev, "disable oe failed\n");
  314. ret = set_sw_standby(sd, true);
  315. if (ret < 0)
  316. dev_dbg(&client->dev, "soft stby failed\n");
  317. clk_disable_unprepare(ov5647->xclk);
  318. }
  319. /* Update the power count. */
  320. ov5647->power_count += on ? 1 : -1;
  321. WARN_ON(ov5647->power_count < 0);
  322. out:
  323. mutex_unlock(&ov5647->lock);
  324. return ret;
  325. }
  326. #ifdef CONFIG_VIDEO_ADV_DEBUG
  327. static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
  328. struct v4l2_dbg_register *reg)
  329. {
  330. u8 val;
  331. int ret;
  332. ret = ov5647_read(sd, reg->reg & 0xff, &val);
  333. if (ret < 0)
  334. return ret;
  335. reg->val = val;
  336. reg->size = 1;
  337. return 0;
  338. }
  339. static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
  340. const struct v4l2_dbg_register *reg)
  341. {
  342. return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
  343. }
  344. #endif
  345. /**
  346. * @short Subdev core operations registration
  347. */
  348. static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
  349. .s_power = ov5647_sensor_power,
  350. #ifdef CONFIG_VIDEO_ADV_DEBUG
  351. .g_register = ov5647_sensor_get_register,
  352. .s_register = ov5647_sensor_set_register,
  353. #endif
  354. };
  355. static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
  356. {
  357. if (enable)
  358. return ov5647_stream_on(sd);
  359. else
  360. return ov5647_stream_off(sd);
  361. }
  362. static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
  363. .s_stream = ov5647_s_stream,
  364. };
  365. static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
  366. struct v4l2_subdev_pad_config *cfg,
  367. struct v4l2_subdev_mbus_code_enum *code)
  368. {
  369. if (code->index > 0)
  370. return -EINVAL;
  371. code->code = MEDIA_BUS_FMT_SBGGR8_1X8;
  372. return 0;
  373. }
  374. static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
  375. .enum_mbus_code = ov5647_enum_mbus_code,
  376. };
  377. static const struct v4l2_subdev_ops ov5647_subdev_ops = {
  378. .core = &ov5647_subdev_core_ops,
  379. .video = &ov5647_subdev_video_ops,
  380. .pad = &ov5647_subdev_pad_ops,
  381. };
  382. static int ov5647_detect(struct v4l2_subdev *sd)
  383. {
  384. u8 read;
  385. int ret;
  386. struct i2c_client *client = v4l2_get_subdevdata(sd);
  387. ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
  388. if (ret < 0)
  389. return ret;
  390. ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
  391. if (ret < 0)
  392. return ret;
  393. if (read != 0x56) {
  394. dev_err(&client->dev, "ID High expected 0x56 got %x", read);
  395. return -ENODEV;
  396. }
  397. ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
  398. if (ret < 0)
  399. return ret;
  400. if (read != 0x47) {
  401. dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
  402. return -ENODEV;
  403. }
  404. return ov5647_write(sd, OV5647_SW_RESET, 0x00);
  405. }
  406. static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  407. {
  408. struct v4l2_mbus_framefmt *format =
  409. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  410. struct v4l2_rect *crop =
  411. v4l2_subdev_get_try_crop(sd, fh->pad, 0);
  412. crop->left = OV5647_COLUMN_START_DEF;
  413. crop->top = OV5647_ROW_START_DEF;
  414. crop->width = OV5647_WINDOW_WIDTH_DEF;
  415. crop->height = OV5647_WINDOW_HEIGHT_DEF;
  416. format->code = MEDIA_BUS_FMT_SBGGR8_1X8;
  417. format->width = OV5647_WINDOW_WIDTH_DEF;
  418. format->height = OV5647_WINDOW_HEIGHT_DEF;
  419. format->field = V4L2_FIELD_NONE;
  420. format->colorspace = V4L2_COLORSPACE_SRGB;
  421. return 0;
  422. }
  423. static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
  424. .open = ov5647_open,
  425. };
  426. static int ov5647_parse_dt(struct device_node *np)
  427. {
  428. struct v4l2_of_endpoint bus_cfg;
  429. struct device_node *ep;
  430. int ret;
  431. ep = of_graph_get_next_endpoint(np, NULL);
  432. if (!ep)
  433. return -EINVAL;
  434. ret = v4l2_of_parse_endpoint(ep, &bus_cfg);
  435. of_node_put(ep);
  436. return ret;
  437. }
  438. static int ov5647_probe(struct i2c_client *client,
  439. const struct i2c_device_id *id)
  440. {
  441. struct device *dev = &client->dev;
  442. struct ov5647 *sensor;
  443. int ret;
  444. struct v4l2_subdev *sd;
  445. struct device_node *np = client->dev.of_node;
  446. u32 xclk_freq;
  447. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  448. if (!sensor)
  449. return -ENOMEM;
  450. if (IS_ENABLED(CONFIG_OF) && np) {
  451. ret = ov5647_parse_dt(np);
  452. if (ret) {
  453. dev_err(dev, "DT parsing error: %d\n", ret);
  454. return ret;
  455. }
  456. }
  457. /* get system clock (xclk) */
  458. sensor->xclk = devm_clk_get(dev, NULL);
  459. if (IS_ERR(sensor->xclk)) {
  460. dev_err(dev, "could not get xclk");
  461. return PTR_ERR(sensor->xclk);
  462. }
  463. xclk_freq = clk_get_rate(sensor->xclk);
  464. if (xclk_freq != 25000000) {
  465. dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
  466. return -EINVAL;
  467. }
  468. mutex_init(&sensor->lock);
  469. sd = &sensor->sd;
  470. v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
  471. sensor->sd.internal_ops = &ov5647_subdev_internal_ops;
  472. sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  473. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  474. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  475. ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
  476. if (ret < 0)
  477. goto mutex_remove;
  478. ret = ov5647_detect(sd);
  479. if (ret < 0)
  480. goto error;
  481. ret = v4l2_async_register_subdev(sd);
  482. if (ret < 0)
  483. goto error;
  484. dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
  485. return 0;
  486. error:
  487. media_entity_cleanup(&sd->entity);
  488. mutex_remove:
  489. mutex_destroy(&sensor->lock);
  490. return ret;
  491. }
  492. static int ov5647_remove(struct i2c_client *client)
  493. {
  494. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  495. struct ov5647 *ov5647 = to_state(sd);
  496. v4l2_async_unregister_subdev(&ov5647->sd);
  497. media_entity_cleanup(&ov5647->sd.entity);
  498. v4l2_device_unregister_subdev(sd);
  499. mutex_destroy(&ov5647->lock);
  500. return 0;
  501. }
  502. static const struct i2c_device_id ov5647_id[] = {
  503. { "ov5647", 0 },
  504. { }
  505. };
  506. MODULE_DEVICE_TABLE(i2c, ov5647_id);
  507. #if IS_ENABLED(CONFIG_OF)
  508. static const struct of_device_id ov5647_of_match[] = {
  509. { .compatible = "ovti,ov5647" },
  510. { /* sentinel */ },
  511. };
  512. MODULE_DEVICE_TABLE(of, ov5647_of_match);
  513. #endif
  514. static struct i2c_driver ov5647_driver = {
  515. .driver = {
  516. .of_match_table = of_match_ptr(ov5647_of_match),
  517. .name = SENSOR_NAME,
  518. },
  519. .probe = ov5647_probe,
  520. .remove = ov5647_remove,
  521. .id_table = ov5647_id,
  522. };
  523. module_i2c_driver(ov5647_driver);
  524. MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
  525. MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
  526. MODULE_LICENSE("GPL v2");