sn9c102_pas106b.c 7.9 KB

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