sn9c102_pas106b.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /***************************************************************************
  2. * Plug-in for PAS106B image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2004-2007 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 int pas106b_init(struct sn9c102_device* cam)
  24. {
  25. int err = 0;
  26. err += sn9c102_write_reg(cam, 0x00, 0x10);
  27. err += sn9c102_write_reg(cam, 0x00, 0x11);
  28. err += sn9c102_write_reg(cam, 0x00, 0x14);
  29. err += sn9c102_write_reg(cam, 0x20, 0x17);
  30. err += sn9c102_write_reg(cam, 0x20, 0x19);
  31. err += sn9c102_write_reg(cam, 0x09, 0x18);
  32. err += sn9c102_i2c_write(cam, 0x02, 0x0c);
  33. err += sn9c102_i2c_write(cam, 0x05, 0x5a);
  34. err += sn9c102_i2c_write(cam, 0x06, 0x88);
  35. err += sn9c102_i2c_write(cam, 0x07, 0x80);
  36. err += sn9c102_i2c_write(cam, 0x10, 0x06);
  37. err += sn9c102_i2c_write(cam, 0x11, 0x06);
  38. err += sn9c102_i2c_write(cam, 0x12, 0x00);
  39. err += sn9c102_i2c_write(cam, 0x14, 0x02);
  40. err += sn9c102_i2c_write(cam, 0x13, 0x01);
  41. msleep(400);
  42. return err;
  43. }
  44. static int pas106b_get_ctrl(struct sn9c102_device* cam,
  45. struct v4l2_control* ctrl)
  46. {
  47. switch (ctrl->id) {
  48. case V4L2_CID_EXPOSURE:
  49. {
  50. int r1 = sn9c102_i2c_read(cam, 0x03),
  51. r2 = sn9c102_i2c_read(cam, 0x04);
  52. if (r1 < 0 || r2 < 0)
  53. return -EIO;
  54. ctrl->value = (r1 << 4) | (r2 & 0x0f);
  55. }
  56. return 0;
  57. case V4L2_CID_RED_BALANCE:
  58. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
  59. return -EIO;
  60. ctrl->value &= 0x1f;
  61. return 0;
  62. case V4L2_CID_BLUE_BALANCE:
  63. if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
  64. return -EIO;
  65. ctrl->value &= 0x1f;
  66. return 0;
  67. case V4L2_CID_GAIN:
  68. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0e)) < 0)
  69. return -EIO;
  70. ctrl->value &= 0x1f;
  71. return 0;
  72. case V4L2_CID_CONTRAST:
  73. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0f)) < 0)
  74. return -EIO;
  75. ctrl->value &= 0x07;
  76. return 0;
  77. case SN9C102_V4L2_CID_GREEN_BALANCE:
  78. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0a)) < 0)
  79. return -EIO;
  80. ctrl->value = (ctrl->value & 0x1f) << 1;
  81. return 0;
  82. case SN9C102_V4L2_CID_DAC_MAGNITUDE:
  83. if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
  84. return -EIO;
  85. ctrl->value &= 0xf8;
  86. return 0;
  87. default:
  88. return -EINVAL;
  89. }
  90. }
  91. static int pas106b_set_ctrl(struct sn9c102_device* cam,
  92. const struct v4l2_control* ctrl)
  93. {
  94. int err = 0;
  95. switch (ctrl->id) {
  96. case V4L2_CID_EXPOSURE:
  97. err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4);
  98. err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f);
  99. break;
  100. case V4L2_CID_RED_BALANCE:
  101. err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
  102. break;
  103. case V4L2_CID_BLUE_BALANCE:
  104. err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
  105. break;
  106. case V4L2_CID_GAIN:
  107. err += sn9c102_i2c_write(cam, 0x0e, ctrl->value);
  108. break;
  109. case V4L2_CID_CONTRAST:
  110. err += sn9c102_i2c_write(cam, 0x0f, ctrl->value);
  111. break;
  112. case SN9C102_V4L2_CID_GREEN_BALANCE:
  113. err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1);
  114. err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1);
  115. break;
  116. case SN9C102_V4L2_CID_DAC_MAGNITUDE:
  117. err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3);
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. err += sn9c102_i2c_write(cam, 0x13, 0x01);
  123. return err ? -EIO : 0;
  124. }
  125. static int pas106b_set_crop(struct sn9c102_device* cam,
  126. const struct v4l2_rect* rect)
  127. {
  128. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  129. int err = 0;
  130. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
  131. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
  132. err += sn9c102_write_reg(cam, h_start, 0x12);
  133. err += sn9c102_write_reg(cam, v_start, 0x13);
  134. return err;
  135. }
  136. static int pas106b_set_pix_format(struct sn9c102_device* cam,
  137. const struct v4l2_pix_format* pix)
  138. {
  139. int err = 0;
  140. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  141. err += sn9c102_write_reg(cam, 0x2c, 0x17);
  142. else
  143. err += sn9c102_write_reg(cam, 0x20, 0x17);
  144. return err;
  145. }
  146. static struct sn9c102_sensor pas106b = {
  147. .name = "PAS106B",
  148. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  149. .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
  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. }