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