sn9c102_pas202bcb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /***************************************************************************
  2. * Plug-in for PAS202BCB image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2004 by Carlos Eduardo Medaglia Dyonisio *
  6. * <medaglia@undl.org.br> *
  7. * *
  8. * Support for SN9C103, DAC Magnitude, exposure and green gain controls *
  9. * added by Luca Risolia <luca.risolia@studio.unibo.it> *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the Free Software *
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  24. ***************************************************************************/
  25. #include <linux/delay.h>
  26. #include "sn9c102_sensor.h"
  27. #include "sn9c102_devtable.h"
  28. static int pas202bcb_init(struct sn9c102_device* cam)
  29. {
  30. int err = 0;
  31. switch (sn9c102_get_bridge(cam)) {
  32. case BRIDGE_SN9C101:
  33. case BRIDGE_SN9C102:
  34. err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
  35. {0x00, 0x14}, {0x20, 0x17},
  36. {0x30, 0x19}, {0x09, 0x18});
  37. break;
  38. case BRIDGE_SN9C103:
  39. err = sn9c102_write_const_regs(cam, {0x00, 0x02}, {0x00, 0x03},
  40. {0x1a, 0x04}, {0x20, 0x05},
  41. {0x20, 0x06}, {0x20, 0x07},
  42. {0x00, 0x10}, {0x00, 0x11},
  43. {0x00, 0x14}, {0x20, 0x17},
  44. {0x30, 0x19}, {0x09, 0x18},
  45. {0x02, 0x1c}, {0x03, 0x1d},
  46. {0x0f, 0x1e}, {0x0c, 0x1f},
  47. {0x00, 0x20}, {0x10, 0x21},
  48. {0x20, 0x22}, {0x30, 0x23},
  49. {0x40, 0x24}, {0x50, 0x25},
  50. {0x60, 0x26}, {0x70, 0x27},
  51. {0x80, 0x28}, {0x90, 0x29},
  52. {0xa0, 0x2a}, {0xb0, 0x2b},
  53. {0xc0, 0x2c}, {0xd0, 0x2d},
  54. {0xe0, 0x2e}, {0xf0, 0x2f},
  55. {0xff, 0x30});
  56. break;
  57. default:
  58. break;
  59. }
  60. err += sn9c102_i2c_write(cam, 0x02, 0x14);
  61. err += sn9c102_i2c_write(cam, 0x03, 0x40);
  62. err += sn9c102_i2c_write(cam, 0x0d, 0x2c);
  63. err += sn9c102_i2c_write(cam, 0x0e, 0x01);
  64. err += sn9c102_i2c_write(cam, 0x0f, 0xa9);
  65. err += sn9c102_i2c_write(cam, 0x10, 0x08);
  66. err += sn9c102_i2c_write(cam, 0x13, 0x63);
  67. err += sn9c102_i2c_write(cam, 0x15, 0x70);
  68. err += sn9c102_i2c_write(cam, 0x11, 0x01);
  69. msleep(400);
  70. return err;
  71. }
  72. static int pas202bcb_get_ctrl(struct sn9c102_device* cam,
  73. struct v4l2_control* ctrl)
  74. {
  75. switch (ctrl->id) {
  76. case V4L2_CID_EXPOSURE:
  77. {
  78. int r1 = sn9c102_i2c_read(cam, 0x04),
  79. r2 = sn9c102_i2c_read(cam, 0x05);
  80. if (r1 < 0 || r2 < 0)
  81. return -EIO;
  82. ctrl->value = (r1 << 6) | (r2 & 0x3f);
  83. }
  84. return 0;
  85. case V4L2_CID_RED_BALANCE:
  86. if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
  87. return -EIO;
  88. ctrl->value &= 0x0f;
  89. return 0;
  90. case V4L2_CID_BLUE_BALANCE:
  91. if ((ctrl->value = sn9c102_i2c_read(cam, 0x07)) < 0)
  92. return -EIO;
  93. ctrl->value &= 0x0f;
  94. return 0;
  95. case V4L2_CID_GAIN:
  96. if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0)
  97. return -EIO;
  98. ctrl->value &= 0x1f;
  99. return 0;
  100. case SN9C102_V4L2_CID_GREEN_BALANCE:
  101. if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
  102. return -EIO;
  103. ctrl->value &= 0x0f;
  104. return 0;
  105. case SN9C102_V4L2_CID_DAC_MAGNITUDE:
  106. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
  107. return -EIO;
  108. return 0;
  109. default:
  110. return -EINVAL;
  111. }
  112. }
  113. static int pas202bcb_set_pix_format(struct sn9c102_device* cam,
  114. const struct v4l2_pix_format* pix)
  115. {
  116. int err = 0;
  117. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  118. err += sn9c102_write_reg(cam, 0x28, 0x17);
  119. else
  120. err += sn9c102_write_reg(cam, 0x20, 0x17);
  121. return err;
  122. }
  123. static int pas202bcb_set_ctrl(struct sn9c102_device* cam,
  124. const struct v4l2_control* ctrl)
  125. {
  126. int err = 0;
  127. switch (ctrl->id) {
  128. case V4L2_CID_EXPOSURE:
  129. err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6);
  130. err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f);
  131. break;
  132. case V4L2_CID_RED_BALANCE:
  133. err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
  134. break;
  135. case V4L2_CID_BLUE_BALANCE:
  136. err += sn9c102_i2c_write(cam, 0x07, ctrl->value);
  137. break;
  138. case V4L2_CID_GAIN:
  139. err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
  140. break;
  141. case SN9C102_V4L2_CID_GREEN_BALANCE:
  142. err += sn9c102_i2c_write(cam, 0x08, ctrl->value);
  143. break;
  144. case SN9C102_V4L2_CID_DAC_MAGNITUDE:
  145. err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
  146. break;
  147. default:
  148. return -EINVAL;
  149. }
  150. err += sn9c102_i2c_write(cam, 0x11, 0x01);
  151. return err ? -EIO : 0;
  152. }
  153. static int pas202bcb_set_crop(struct sn9c102_device* cam,
  154. const struct v4l2_rect* rect)
  155. {
  156. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  157. int err = 0;
  158. u8 h_start = 0,
  159. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
  160. switch (sn9c102_get_bridge(cam)) {
  161. case BRIDGE_SN9C101:
  162. case BRIDGE_SN9C102:
  163. h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4;
  164. break;
  165. case BRIDGE_SN9C103:
  166. h_start = (u8)(rect->left - s->cropcap.bounds.left) + 3;
  167. break;
  168. default:
  169. break;
  170. }
  171. err += sn9c102_write_reg(cam, h_start, 0x12);
  172. err += sn9c102_write_reg(cam, v_start, 0x13);
  173. return err;
  174. }
  175. static const struct sn9c102_sensor pas202bcb = {
  176. .name = "PAS202BCB",
  177. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  178. .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103,
  179. .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
  180. .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
  181. .interface = SN9C102_I2C_2WIRES,
  182. .i2c_slave_id = 0x40,
  183. .init = &pas202bcb_init,
  184. .qctrl = {
  185. {
  186. .id = V4L2_CID_EXPOSURE,
  187. .type = V4L2_CTRL_TYPE_INTEGER,
  188. .name = "exposure",
  189. .minimum = 0x01e5,
  190. .maximum = 0x3fff,
  191. .step = 0x0001,
  192. .default_value = 0x01e5,
  193. .flags = 0,
  194. },
  195. {
  196. .id = V4L2_CID_GAIN,
  197. .type = V4L2_CTRL_TYPE_INTEGER,
  198. .name = "global gain",
  199. .minimum = 0x00,
  200. .maximum = 0x1f,
  201. .step = 0x01,
  202. .default_value = 0x0b,
  203. .flags = 0,
  204. },
  205. {
  206. .id = V4L2_CID_RED_BALANCE,
  207. .type = V4L2_CTRL_TYPE_INTEGER,
  208. .name = "red balance",
  209. .minimum = 0x00,
  210. .maximum = 0x0f,
  211. .step = 0x01,
  212. .default_value = 0x00,
  213. .flags = 0,
  214. },
  215. {
  216. .id = V4L2_CID_BLUE_BALANCE,
  217. .type = V4L2_CTRL_TYPE_INTEGER,
  218. .name = "blue balance",
  219. .minimum = 0x00,
  220. .maximum = 0x0f,
  221. .step = 0x01,
  222. .default_value = 0x05,
  223. .flags = 0,
  224. },
  225. {
  226. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  227. .type = V4L2_CTRL_TYPE_INTEGER,
  228. .name = "green balance",
  229. .minimum = 0x00,
  230. .maximum = 0x0f,
  231. .step = 0x01,
  232. .default_value = 0x00,
  233. .flags = 0,
  234. },
  235. {
  236. .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
  237. .type = V4L2_CTRL_TYPE_INTEGER,
  238. .name = "DAC magnitude",
  239. .minimum = 0x00,
  240. .maximum = 0xff,
  241. .step = 0x01,
  242. .default_value = 0x04,
  243. .flags = 0,
  244. },
  245. },
  246. .get_ctrl = &pas202bcb_get_ctrl,
  247. .set_ctrl = &pas202bcb_set_ctrl,
  248. .cropcap = {
  249. .bounds = {
  250. .left = 0,
  251. .top = 0,
  252. .width = 640,
  253. .height = 480,
  254. },
  255. .defrect = {
  256. .left = 0,
  257. .top = 0,
  258. .width = 640,
  259. .height = 480,
  260. },
  261. },
  262. .set_crop = &pas202bcb_set_crop,
  263. .pix_format = {
  264. .width = 640,
  265. .height = 480,
  266. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  267. .priv = 8,
  268. },
  269. .set_pix_format = &pas202bcb_set_pix_format
  270. };
  271. int sn9c102_probe_pas202bcb(struct sn9c102_device* cam)
  272. {
  273. int r0 = 0, r1 = 0, err = 0;
  274. unsigned int pid = 0;
  275. /*
  276. * Minimal initialization to enable the I2C communication
  277. * NOTE: do NOT change the values!
  278. */
  279. switch (sn9c102_get_bridge(cam)) {
  280. case BRIDGE_SN9C101:
  281. case BRIDGE_SN9C102:
  282. err = sn9c102_write_const_regs(cam,
  283. {0x01, 0x01}, /* power down */
  284. {0x40, 0x01}, /* power on */
  285. {0x28, 0x17});/* clock 24 MHz */
  286. break;
  287. case BRIDGE_SN9C103: /* do _not_ change anything! */
  288. err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x44, 0x01},
  289. {0x44, 0x02}, {0x29, 0x17});
  290. break;
  291. default:
  292. break;
  293. }
  294. r0 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x00);
  295. r1 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x01);
  296. if (err || r0 < 0 || r1 < 0)
  297. return -EIO;
  298. pid = (r0 << 4) | ((r1 & 0xf0) >> 4);
  299. if (pid != 0x017)
  300. return -ENODEV;
  301. sn9c102_attach_sensor(cam, &pas202bcb);
  302. return 0;
  303. }