m5602_ov7660.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Driver for the ov7660 sensor
  3. *
  4. * Copyright (C) 2009 Erik Andrén
  5. * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "m5602_ov7660.h"
  20. static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl);
  21. static void ov7660_dump_registers(struct sd *sd);
  22. static const unsigned char preinit_ov7660[][4] = {
  23. {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
  24. {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
  25. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  26. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  27. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  28. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
  29. {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
  30. {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
  31. {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
  32. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  33. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  34. {SENSOR, OV7660_OFON, 0x0c},
  35. {SENSOR, OV7660_COM2, 0x11},
  36. {SENSOR, OV7660_COM7, 0x05},
  37. {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
  38. {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
  39. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  40. {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
  41. {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
  42. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
  43. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  44. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  45. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  46. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  47. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  48. {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
  49. {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
  50. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  51. {BRIDGE, M5602_XB_GPIO_EN_L, 0x00}
  52. };
  53. static const unsigned char init_ov7660[][4] = {
  54. {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
  55. {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
  56. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  57. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  58. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  59. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
  60. {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
  61. {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
  62. {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
  63. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  64. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  65. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  66. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  67. {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
  68. {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
  69. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  70. {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
  71. {SENSOR, OV7660_COM7, 0x80},
  72. {SENSOR, OV7660_CLKRC, 0x80},
  73. {SENSOR, OV7660_COM9, 0x4c},
  74. {SENSOR, OV7660_OFON, 0x43},
  75. {SENSOR, OV7660_COM12, 0x28},
  76. {SENSOR, OV7660_COM8, 0x00},
  77. {SENSOR, OV7660_COM10, 0x40},
  78. {SENSOR, OV7660_HSTART, 0x0c},
  79. {SENSOR, OV7660_HSTOP, 0x61},
  80. {SENSOR, OV7660_HREF, 0xa4},
  81. {SENSOR, OV7660_PSHFT, 0x0b},
  82. {SENSOR, OV7660_VSTART, 0x01},
  83. {SENSOR, OV7660_VSTOP, 0x7a},
  84. {SENSOR, OV7660_VSTOP, 0x00},
  85. {SENSOR, OV7660_COM7, 0x05},
  86. {SENSOR, OV7660_COM6, 0x42},
  87. {SENSOR, OV7660_BBIAS, 0x94},
  88. {SENSOR, OV7660_GbBIAS, 0x94},
  89. {SENSOR, OV7660_RSVD29, 0x94},
  90. {SENSOR, OV7660_RBIAS, 0x94},
  91. {SENSOR, OV7660_COM1, 0x00},
  92. {SENSOR, OV7660_AECH, 0x00},
  93. {SENSOR, OV7660_AECHH, 0x00},
  94. {SENSOR, OV7660_ADC, 0x05},
  95. {SENSOR, OV7660_COM13, 0x00},
  96. {SENSOR, OV7660_RSVDA1, 0x23},
  97. {SENSOR, OV7660_TSLB, 0x0d},
  98. {SENSOR, OV7660_HV, 0x80},
  99. {SENSOR, OV7660_LCC1, 0x00},
  100. {SENSOR, OV7660_LCC2, 0x00},
  101. {SENSOR, OV7660_LCC3, 0x10},
  102. {SENSOR, OV7660_LCC4, 0x40},
  103. {SENSOR, OV7660_LCC5, 0x01},
  104. {SENSOR, OV7660_AECH, 0x20},
  105. {SENSOR, OV7660_COM1, 0x00},
  106. {SENSOR, OV7660_OFON, 0x0c},
  107. {SENSOR, OV7660_COM2, 0x11},
  108. {SENSOR, OV7660_COM7, 0x05},
  109. {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
  110. {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
  111. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  112. {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
  113. {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
  114. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
  115. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  116. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  117. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  118. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  119. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  120. {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
  121. {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
  122. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  123. {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
  124. {SENSOR, OV7660_AECH, 0x5f},
  125. {SENSOR, OV7660_COM1, 0x03},
  126. {SENSOR, OV7660_OFON, 0x0c},
  127. {SENSOR, OV7660_COM2, 0x11},
  128. {SENSOR, OV7660_COM7, 0x05},
  129. {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
  130. {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
  131. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  132. {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
  133. {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
  134. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
  135. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  136. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  137. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  138. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  139. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  140. {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
  141. {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
  142. {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
  143. {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
  144. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06},
  145. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  146. {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
  147. {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
  148. {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81},
  149. {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
  150. {BRIDGE, M5602_XB_SIG_INI, 0x01},
  151. {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
  152. {BRIDGE, M5602_XB_VSYNC_PARA, 0x08},
  153. {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
  154. {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
  155. {BRIDGE, M5602_XB_VSYNC_PARA, 0x01},
  156. {BRIDGE, M5602_XB_VSYNC_PARA, 0xec},
  157. {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
  158. {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
  159. {BRIDGE, M5602_XB_SIG_INI, 0x00},
  160. {BRIDGE, M5602_XB_SIG_INI, 0x02},
  161. {BRIDGE, M5602_XB_HSYNC_PARA, 0x00},
  162. {BRIDGE, M5602_XB_HSYNC_PARA, 0x27},
  163. {BRIDGE, M5602_XB_HSYNC_PARA, 0x02},
  164. {BRIDGE, M5602_XB_HSYNC_PARA, 0xa7},
  165. {BRIDGE, M5602_XB_SIG_INI, 0x00},
  166. {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
  167. {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
  168. };
  169. static struct v4l2_pix_format ov7660_modes[] = {
  170. {
  171. 640,
  172. 480,
  173. V4L2_PIX_FMT_SBGGR8,
  174. V4L2_FIELD_NONE,
  175. .sizeimage =
  176. 640 * 480,
  177. .bytesperline = 640,
  178. .colorspace = V4L2_COLORSPACE_SRGB,
  179. .priv = 0
  180. }
  181. };
  182. static const struct v4l2_ctrl_ops ov7660_ctrl_ops = {
  183. .s_ctrl = ov7660_s_ctrl,
  184. };
  185. int ov7660_probe(struct sd *sd)
  186. {
  187. int err = 0, i;
  188. u8 prod_id = 0, ver_id = 0;
  189. if (force_sensor) {
  190. if (force_sensor == OV7660_SENSOR) {
  191. pr_info("Forcing an %s sensor\n", ov7660.name);
  192. goto sensor_found;
  193. }
  194. /* If we want to force another sensor,
  195. don't try to probe this one */
  196. return -ENODEV;
  197. }
  198. /* Do the preinit */
  199. for (i = 0; i < ARRAY_SIZE(preinit_ov7660) && !err; i++) {
  200. u8 data[2];
  201. if (preinit_ov7660[i][0] == BRIDGE) {
  202. err = m5602_write_bridge(sd,
  203. preinit_ov7660[i][1],
  204. preinit_ov7660[i][2]);
  205. } else {
  206. data[0] = preinit_ov7660[i][2];
  207. err = m5602_write_sensor(sd,
  208. preinit_ov7660[i][1], data, 1);
  209. }
  210. }
  211. if (err < 0)
  212. return err;
  213. if (m5602_read_sensor(sd, OV7660_PID, &prod_id, 1))
  214. return -ENODEV;
  215. if (m5602_read_sensor(sd, OV7660_VER, &ver_id, 1))
  216. return -ENODEV;
  217. pr_info("Sensor reported 0x%x%x\n", prod_id, ver_id);
  218. if ((prod_id == 0x76) && (ver_id == 0x60)) {
  219. pr_info("Detected a ov7660 sensor\n");
  220. goto sensor_found;
  221. }
  222. return -ENODEV;
  223. sensor_found:
  224. sd->gspca_dev.cam.cam_mode = ov7660_modes;
  225. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(ov7660_modes);
  226. return 0;
  227. }
  228. int ov7660_init(struct sd *sd)
  229. {
  230. int i, err;
  231. /* Init the sensor */
  232. for (i = 0; i < ARRAY_SIZE(init_ov7660); i++) {
  233. u8 data[2];
  234. if (init_ov7660[i][0] == BRIDGE) {
  235. err = m5602_write_bridge(sd,
  236. init_ov7660[i][1],
  237. init_ov7660[i][2]);
  238. } else {
  239. data[0] = init_ov7660[i][2];
  240. err = m5602_write_sensor(sd,
  241. init_ov7660[i][1], data, 1);
  242. }
  243. if (err < 0)
  244. return err;
  245. }
  246. if (dump_sensor)
  247. ov7660_dump_registers(sd);
  248. return 0;
  249. }
  250. int ov7660_init_controls(struct sd *sd)
  251. {
  252. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  253. sd->gspca_dev.vdev.ctrl_handler = hdl;
  254. v4l2_ctrl_handler_init(hdl, 6);
  255. v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
  256. 0, 1, 1, 1);
  257. v4l2_ctrl_new_std_menu(hdl, &ov7660_ctrl_ops,
  258. V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_AUTO);
  259. sd->autogain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops,
  260. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  261. sd->gain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_GAIN, 0,
  262. 255, 1, OV7660_DEFAULT_GAIN);
  263. sd->hflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_HFLIP,
  264. 0, 1, 1, 0);
  265. sd->vflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_VFLIP,
  266. 0, 1, 1, 0);
  267. if (hdl->error) {
  268. pr_err("Could not initialize controls\n");
  269. return hdl->error;
  270. }
  271. v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, false);
  272. v4l2_ctrl_cluster(2, &sd->hflip);
  273. return 0;
  274. }
  275. int ov7660_start(struct sd *sd)
  276. {
  277. return 0;
  278. }
  279. int ov7660_stop(struct sd *sd)
  280. {
  281. return 0;
  282. }
  283. void ov7660_disconnect(struct sd *sd)
  284. {
  285. ov7660_stop(sd);
  286. sd->sensor = NULL;
  287. }
  288. static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  289. {
  290. int err;
  291. u8 i2c_data = val;
  292. struct sd *sd = (struct sd *) gspca_dev;
  293. PDEBUG(D_CONF, "Setting gain to %d", val);
  294. err = m5602_write_sensor(sd, OV7660_GAIN, &i2c_data, 1);
  295. return err;
  296. }
  297. static int ov7660_set_auto_white_balance(struct gspca_dev *gspca_dev,
  298. __s32 val)
  299. {
  300. int err;
  301. u8 i2c_data;
  302. struct sd *sd = (struct sd *) gspca_dev;
  303. PDEBUG(D_CONF, "Set auto white balance to %d", val);
  304. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  305. if (err < 0)
  306. return err;
  307. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  308. err = m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  309. return err;
  310. }
  311. static int ov7660_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  312. {
  313. int err;
  314. u8 i2c_data;
  315. struct sd *sd = (struct sd *) gspca_dev;
  316. PDEBUG(D_CONF, "Set auto gain control to %d", val);
  317. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  318. if (err < 0)
  319. return err;
  320. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  321. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  322. }
  323. static int ov7660_set_auto_exposure(struct gspca_dev *gspca_dev,
  324. __s32 val)
  325. {
  326. int err;
  327. u8 i2c_data;
  328. struct sd *sd = (struct sd *) gspca_dev;
  329. PDEBUG(D_CONF, "Set auto exposure control to %d", val);
  330. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  331. if (err < 0)
  332. return err;
  333. val = (val == V4L2_EXPOSURE_AUTO);
  334. i2c_data = ((i2c_data & 0xfe) | ((val & 0x01) << 0));
  335. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  336. }
  337. static int ov7660_set_hvflip(struct gspca_dev *gspca_dev)
  338. {
  339. int err;
  340. u8 i2c_data;
  341. struct sd *sd = (struct sd *) gspca_dev;
  342. PDEBUG(D_CONF, "Set hvflip to %d, %d", sd->hflip->val, sd->vflip->val);
  343. i2c_data = (sd->hflip->val << 5) | (sd->vflip->val << 4);
  344. err = m5602_write_sensor(sd, OV7660_MVFP, &i2c_data, 1);
  345. return err;
  346. }
  347. static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl)
  348. {
  349. struct gspca_dev *gspca_dev =
  350. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  351. struct sd *sd = (struct sd *) gspca_dev;
  352. int err;
  353. if (!gspca_dev->streaming)
  354. return 0;
  355. switch (ctrl->id) {
  356. case V4L2_CID_AUTO_WHITE_BALANCE:
  357. err = ov7660_set_auto_white_balance(gspca_dev, ctrl->val);
  358. break;
  359. case V4L2_CID_EXPOSURE_AUTO:
  360. err = ov7660_set_auto_exposure(gspca_dev, ctrl->val);
  361. break;
  362. case V4L2_CID_AUTOGAIN:
  363. err = ov7660_set_auto_gain(gspca_dev, ctrl->val);
  364. if (err || ctrl->val)
  365. return err;
  366. err = ov7660_set_gain(gspca_dev, sd->gain->val);
  367. break;
  368. case V4L2_CID_HFLIP:
  369. err = ov7660_set_hvflip(gspca_dev);
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. return err;
  375. }
  376. static void ov7660_dump_registers(struct sd *sd)
  377. {
  378. int address;
  379. pr_info("Dumping the ov7660 register state\n");
  380. for (address = 0; address < 0xa9; address++) {
  381. u8 value;
  382. m5602_read_sensor(sd, address, &value, 1);
  383. pr_info("register 0x%x contains 0x%x\n", address, value);
  384. }
  385. pr_info("ov7660 register state dump complete\n");
  386. pr_info("Probing for which registers that are read/write\n");
  387. for (address = 0; address < 0xff; address++) {
  388. u8 old_value, ctrl_value;
  389. u8 test_value[2] = {0xff, 0xff};
  390. m5602_read_sensor(sd, address, &old_value, 1);
  391. m5602_write_sensor(sd, address, test_value, 1);
  392. m5602_read_sensor(sd, address, &ctrl_value, 1);
  393. if (ctrl_value == test_value[0])
  394. pr_info("register 0x%x is writeable\n", address);
  395. else
  396. pr_info("register 0x%x is read only\n", address);
  397. /* Restore original value */
  398. m5602_write_sensor(sd, address, &old_value, 1);
  399. }
  400. }