sn9c102_hv7131d.c 6.9 KB

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