sn9c102_pas106b.c 8.1 KB

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