sn9c102_hv7131d.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /***************************************************************************
  2. * Plug-in for HV7131D image sensor connected to the SN9C10x PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2004-2005 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 "sn9c102_sensor.h"
  22. static struct sn9c102_sensor hv7131d;
  23. static int hv7131d_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, 0x60, 0x17);
  30. err += sn9c102_write_reg(cam, 0x0e, 0x18);
  31. err += sn9c102_write_reg(cam, 0xf2, 0x19);
  32. err += sn9c102_i2c_write(cam, 0x01, 0x04);
  33. err += sn9c102_i2c_write(cam, 0x02, 0x00);
  34. err += sn9c102_i2c_write(cam, 0x28, 0x00);
  35. return err;
  36. }
  37. static int hv7131d_get_ctrl(struct sn9c102_device* cam,
  38. struct v4l2_control* ctrl)
  39. {
  40. switch (ctrl->id) {
  41. case V4L2_CID_EXPOSURE:
  42. {
  43. int r1 = sn9c102_i2c_read(cam, 0x26),
  44. r2 = sn9c102_i2c_read(cam, 0x27);
  45. if (r1 < 0 || r2 < 0)
  46. return -EIO;
  47. ctrl->value = (r1 << 8) | (r2 & 0xff);
  48. }
  49. return 0;
  50. case V4L2_CID_RED_BALANCE:
  51. if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0)
  52. return -EIO;
  53. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  54. return 0;
  55. case V4L2_CID_BLUE_BALANCE:
  56. if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0)
  57. return -EIO;
  58. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  59. return 0;
  60. case SN9C102_V4L2_CID_GREEN_BALANCE:
  61. if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0)
  62. return -EIO;
  63. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  64. return 0;
  65. case SN9C102_V4L2_CID_RESET_LEVEL:
  66. if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0)
  67. return -EIO;
  68. ctrl->value &= 0x3f;
  69. return 0;
  70. case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
  71. if ((ctrl->value = sn9c102_i2c_read(cam, 0x34)) < 0)
  72. return -EIO;
  73. ctrl->value &= 0x07;
  74. return 0;
  75. default:
  76. return -EINVAL;
  77. }
  78. }
  79. static int hv7131d_set_ctrl(struct sn9c102_device* cam,
  80. const struct v4l2_control* ctrl)
  81. {
  82. int err = 0;
  83. switch (ctrl->id) {
  84. case V4L2_CID_EXPOSURE:
  85. err += sn9c102_i2c_write(cam, 0x26, ctrl->value >> 8);
  86. err += sn9c102_i2c_write(cam, 0x27, ctrl->value & 0xff);
  87. break;
  88. case V4L2_CID_RED_BALANCE:
  89. err += sn9c102_i2c_write(cam, 0x31, 0x3f - ctrl->value);
  90. break;
  91. case V4L2_CID_BLUE_BALANCE:
  92. err += sn9c102_i2c_write(cam, 0x33, 0x3f - ctrl->value);
  93. break;
  94. case SN9C102_V4L2_CID_GREEN_BALANCE:
  95. err += sn9c102_i2c_write(cam, 0x32, 0x3f - ctrl->value);
  96. break;
  97. case SN9C102_V4L2_CID_RESET_LEVEL:
  98. err += sn9c102_i2c_write(cam, 0x30, ctrl->value);
  99. break;
  100. case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
  101. err += sn9c102_i2c_write(cam, 0x34, ctrl->value);
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. return err ? -EIO : 0;
  107. }
  108. static int hv7131d_set_crop(struct sn9c102_device* cam,
  109. const struct v4l2_rect* rect)
  110. {
  111. struct sn9c102_sensor* s = &hv7131d;
  112. int err = 0;
  113. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 2,
  114. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
  115. err += sn9c102_write_reg(cam, h_start, 0x12);
  116. err += sn9c102_write_reg(cam, v_start, 0x13);
  117. return err;
  118. }
  119. static int hv7131d_set_pix_format(struct sn9c102_device* cam,
  120. const struct v4l2_pix_format* pix)
  121. {
  122. int err = 0;
  123. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  124. err += sn9c102_write_reg(cam, 0x42, 0x19);
  125. else
  126. err += sn9c102_write_reg(cam, 0xf2, 0x19);
  127. return err;
  128. }
  129. static struct sn9c102_sensor hv7131d = {
  130. .name = "HV7131D",
  131. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  132. .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
  133. .frequency = SN9C102_I2C_100KHZ,
  134. .interface = SN9C102_I2C_2WIRES,
  135. .i2c_slave_id = 0x11,
  136. .init = &hv7131d_init,
  137. .qctrl = {
  138. {
  139. .id = V4L2_CID_EXPOSURE,
  140. .type = V4L2_CTRL_TYPE_INTEGER,
  141. .name = "exposure",
  142. .minimum = 0x0250,
  143. .maximum = 0xffff,
  144. .step = 0x0001,
  145. .default_value = 0x0250,
  146. .flags = 0,
  147. },
  148. {
  149. .id = V4L2_CID_RED_BALANCE,
  150. .type = V4L2_CTRL_TYPE_INTEGER,
  151. .name = "red balance",
  152. .minimum = 0x00,
  153. .maximum = 0x3f,
  154. .step = 0x01,
  155. .default_value = 0x00,
  156. .flags = 0,
  157. },
  158. {
  159. .id = V4L2_CID_BLUE_BALANCE,
  160. .type = V4L2_CTRL_TYPE_INTEGER,
  161. .name = "blue balance",
  162. .minimum = 0x00,
  163. .maximum = 0x3f,
  164. .step = 0x01,
  165. .default_value = 0x20,
  166. .flags = 0,
  167. },
  168. {
  169. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  170. .type = V4L2_CTRL_TYPE_INTEGER,
  171. .name = "green balance",
  172. .minimum = 0x00,
  173. .maximum = 0x3f,
  174. .step = 0x01,
  175. .default_value = 0x1e,
  176. .flags = 0,
  177. },
  178. {
  179. .id = SN9C102_V4L2_CID_RESET_LEVEL,
  180. .type = V4L2_CTRL_TYPE_INTEGER,
  181. .name = "reset level",
  182. .minimum = 0x19,
  183. .maximum = 0x3f,
  184. .step = 0x01,
  185. .default_value = 0x30,
  186. .flags = 0,
  187. },
  188. {
  189. .id = SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE,
  190. .type = V4L2_CTRL_TYPE_INTEGER,
  191. .name = "pixel bias voltage",
  192. .minimum = 0x00,
  193. .maximum = 0x07,
  194. .step = 0x01,
  195. .default_value = 0x02,
  196. .flags = 0,
  197. },
  198. },
  199. .get_ctrl = &hv7131d_get_ctrl,
  200. .set_ctrl = &hv7131d_set_ctrl,
  201. .cropcap = {
  202. .bounds = {
  203. .left = 0,
  204. .top = 0,
  205. .width = 640,
  206. .height = 480,
  207. },
  208. .defrect = {
  209. .left = 0,
  210. .top = 0,
  211. .width = 640,
  212. .height = 480,
  213. },
  214. },
  215. .set_crop = &hv7131d_set_crop,
  216. .pix_format = {
  217. .width = 640,
  218. .height = 480,
  219. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  220. .priv = 8,
  221. },
  222. .set_pix_format = &hv7131d_set_pix_format
  223. };
  224. int sn9c102_probe_hv7131d(struct sn9c102_device* cam)
  225. {
  226. int r0 = 0, r1 = 0, err = 0;
  227. err += sn9c102_write_reg(cam, 0x01, 0x01);
  228. err += sn9c102_write_reg(cam, 0x00, 0x01);
  229. err += sn9c102_write_reg(cam, 0x28, 0x17);
  230. if (err)
  231. return -EIO;
  232. r0 = sn9c102_i2c_try_read(cam, &hv7131d, 0x00);
  233. r1 = sn9c102_i2c_try_read(cam, &hv7131d, 0x01);
  234. if (r0 < 0 || r1 < 0)
  235. return -EIO;
  236. if (r0 != 0x00 && r1 != 0x04)
  237. return -ENODEV;
  238. sn9c102_attach_sensor(cam, &hv7131d);
  239. return 0;
  240. }