sn9c102_pas202bcb.c 8.0 KB

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