sn9c102_hv7131d.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /***************************************************************************
  2. * Plug-in for HV7131D 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 "sn9c102_sensor.h"
  22. static int hv7131d_init(struct sn9c102_device* cam)
  23. {
  24. int err = 0;
  25. err += sn9c102_write_reg(cam, 0x00, 0x10);
  26. err += sn9c102_write_reg(cam, 0x00, 0x11);
  27. err += sn9c102_write_reg(cam, 0x00, 0x14);
  28. err += sn9c102_write_reg(cam, 0x60, 0x17);
  29. err += sn9c102_write_reg(cam, 0x0e, 0x18);
  30. err += sn9c102_write_reg(cam, 0xf2, 0x19);
  31. err += sn9c102_i2c_write(cam, 0x01, 0x04);
  32. err += sn9c102_i2c_write(cam, 0x02, 0x00);
  33. err += sn9c102_i2c_write(cam, 0x28, 0x00);
  34. return err;
  35. }
  36. static int hv7131d_get_ctrl(struct sn9c102_device* cam,
  37. struct v4l2_control* ctrl)
  38. {
  39. switch (ctrl->id) {
  40. case V4L2_CID_EXPOSURE:
  41. {
  42. int r1 = sn9c102_i2c_read(cam, 0x26),
  43. r2 = sn9c102_i2c_read(cam, 0x27);
  44. if (r1 < 0 || r2 < 0)
  45. return -EIO;
  46. ctrl->value = (r1 << 8) | (r2 & 0xff);
  47. }
  48. return 0;
  49. case V4L2_CID_RED_BALANCE:
  50. if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0)
  51. return -EIO;
  52. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  53. return 0;
  54. case V4L2_CID_BLUE_BALANCE:
  55. if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0)
  56. return -EIO;
  57. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  58. return 0;
  59. case SN9C102_V4L2_CID_GREEN_BALANCE:
  60. if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0)
  61. return -EIO;
  62. ctrl->value = 0x3f - (ctrl->value & 0x3f);
  63. return 0;
  64. case SN9C102_V4L2_CID_RESET_LEVEL:
  65. if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0)
  66. return -EIO;
  67. ctrl->value &= 0x3f;
  68. return 0;
  69. case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
  70. if ((ctrl->value = sn9c102_i2c_read(cam, 0x34)) < 0)
  71. return -EIO;
  72. ctrl->value &= 0x07;
  73. return 0;
  74. default:
  75. return -EINVAL;
  76. }
  77. }
  78. static int hv7131d_set_ctrl(struct sn9c102_device* cam,
  79. const struct v4l2_control* ctrl)
  80. {
  81. int err = 0;
  82. switch (ctrl->id) {
  83. case V4L2_CID_EXPOSURE:
  84. err += sn9c102_i2c_write(cam, 0x26, ctrl->value >> 8);
  85. err += sn9c102_i2c_write(cam, 0x27, ctrl->value & 0xff);
  86. break;
  87. case V4L2_CID_RED_BALANCE:
  88. err += sn9c102_i2c_write(cam, 0x31, 0x3f - ctrl->value);
  89. break;
  90. case V4L2_CID_BLUE_BALANCE:
  91. err += sn9c102_i2c_write(cam, 0x33, 0x3f - ctrl->value);
  92. break;
  93. case SN9C102_V4L2_CID_GREEN_BALANCE:
  94. err += sn9c102_i2c_write(cam, 0x32, 0x3f - ctrl->value);
  95. break;
  96. case SN9C102_V4L2_CID_RESET_LEVEL:
  97. err += sn9c102_i2c_write(cam, 0x30, ctrl->value);
  98. break;
  99. case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
  100. err += sn9c102_i2c_write(cam, 0x34, ctrl->value);
  101. break;
  102. default:
  103. return -EINVAL;
  104. }
  105. return err ? -EIO : 0;
  106. }
  107. static int hv7131d_set_crop(struct sn9c102_device* cam,
  108. const struct v4l2_rect* rect)
  109. {
  110. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  111. int err = 0;
  112. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 2,
  113. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
  114. err += sn9c102_write_reg(cam, h_start, 0x12);
  115. err += sn9c102_write_reg(cam, v_start, 0x13);
  116. return err;
  117. }
  118. static int hv7131d_set_pix_format(struct sn9c102_device* cam,
  119. const struct v4l2_pix_format* pix)
  120. {
  121. int err = 0;
  122. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  123. err += sn9c102_write_reg(cam, 0x42, 0x19);
  124. else
  125. err += sn9c102_write_reg(cam, 0xf2, 0x19);
  126. return err;
  127. }
  128. static struct sn9c102_sensor hv7131d = {
  129. .name = "HV7131D",
  130. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  131. .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
  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. }