sn9c102_hv7131d.c 6.9 KB

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