sn9c102_hv7131r.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /***************************************************************************
  2. * Plug-in for HV7131R image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 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 hv7131r_init(struct sn9c102_device* cam)
  24. {
  25. int err = 0;
  26. switch (sn9c102_get_bridge(cam)) {
  27. case BRIDGE_SN9C103:
  28. err = sn9c102_write_const_regs(cam, {0x00, 0x03}, {0x1a, 0x04},
  29. {0x20, 0x05}, {0x20, 0x06},
  30. {0x03, 0x10}, {0x00, 0x14},
  31. {0x60, 0x17}, {0x0a, 0x18},
  32. {0xf0, 0x19}, {0x1d, 0x1a},
  33. {0x10, 0x1b}, {0x02, 0x1c},
  34. {0x03, 0x1d}, {0x0f, 0x1e},
  35. {0x0c, 0x1f}, {0x00, 0x20},
  36. {0x10, 0x21}, {0x20, 0x22},
  37. {0x30, 0x23}, {0x40, 0x24},
  38. {0x50, 0x25}, {0x60, 0x26},
  39. {0x70, 0x27}, {0x80, 0x28},
  40. {0x90, 0x29}, {0xa0, 0x2a},
  41. {0xb0, 0x2b}, {0xc0, 0x2c},
  42. {0xd0, 0x2d}, {0xe0, 0x2e},
  43. {0xf0, 0x2f}, {0xff, 0x30});
  44. break;
  45. case BRIDGE_SN9C105:
  46. case BRIDGE_SN9C120:
  47. err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02},
  48. {0x00, 0x03}, {0x1a, 0x04},
  49. {0x44, 0x05}, {0x3e, 0x06},
  50. {0x1a, 0x07}, {0x03, 0x10},
  51. {0x08, 0x14}, {0xa3, 0x17},
  52. {0x4b, 0x18}, {0x00, 0x19},
  53. {0x1d, 0x1a}, {0x10, 0x1b},
  54. {0x02, 0x1c}, {0x03, 0x1d},
  55. {0x0f, 0x1e}, {0x0c, 0x1f},
  56. {0x00, 0x20}, {0x29, 0x21},
  57. {0x40, 0x22}, {0x54, 0x23},
  58. {0x66, 0x24}, {0x76, 0x25},
  59. {0x85, 0x26}, {0x94, 0x27},
  60. {0xa1, 0x28}, {0xae, 0x29},
  61. {0xbb, 0x2a}, {0xc7, 0x2b},
  62. {0xd3, 0x2c}, {0xde, 0x2d},
  63. {0xea, 0x2e}, {0xf4, 0x2f},
  64. {0xff, 0x30}, {0x00, 0x3F},
  65. {0xC7, 0x40}, {0x01, 0x41},
  66. {0x44, 0x42}, {0x00, 0x43},
  67. {0x44, 0x44}, {0x00, 0x45},
  68. {0x44, 0x46}, {0x00, 0x47},
  69. {0xC7, 0x48}, {0x01, 0x49},
  70. {0xC7, 0x4A}, {0x01, 0x4B},
  71. {0xC7, 0x4C}, {0x01, 0x4D},
  72. {0x44, 0x4E}, {0x00, 0x4F},
  73. {0x44, 0x50}, {0x00, 0x51},
  74. {0x44, 0x52}, {0x00, 0x53},
  75. {0xC7, 0x54}, {0x01, 0x55},
  76. {0xC7, 0x56}, {0x01, 0x57},
  77. {0xC7, 0x58}, {0x01, 0x59},
  78. {0x44, 0x5A}, {0x00, 0x5B},
  79. {0x44, 0x5C}, {0x00, 0x5D},
  80. {0x44, 0x5E}, {0x00, 0x5F},
  81. {0xC7, 0x60}, {0x01, 0x61},
  82. {0xC7, 0x62}, {0x01, 0x63},
  83. {0xC7, 0x64}, {0x01, 0x65},
  84. {0x44, 0x66}, {0x00, 0x67},
  85. {0x44, 0x68}, {0x00, 0x69},
  86. {0x44, 0x6A}, {0x00, 0x6B},
  87. {0xC7, 0x6C}, {0x01, 0x6D},
  88. {0xC7, 0x6E}, {0x01, 0x6F},
  89. {0xC7, 0x70}, {0x01, 0x71},
  90. {0x44, 0x72}, {0x00, 0x73},
  91. {0x44, 0x74}, {0x00, 0x75},
  92. {0x44, 0x76}, {0x00, 0x77},
  93. {0xC7, 0x78}, {0x01, 0x79},
  94. {0xC7, 0x7A}, {0x01, 0x7B},
  95. {0xC7, 0x7C}, {0x01, 0x7D},
  96. {0x44, 0x7E}, {0x00, 0x7F},
  97. {0x14, 0x84}, {0x00, 0x85},
  98. {0x27, 0x86}, {0x00, 0x87},
  99. {0x07, 0x88}, {0x00, 0x89},
  100. {0xEC, 0x8A}, {0x0f, 0x8B},
  101. {0xD8, 0x8C}, {0x0f, 0x8D},
  102. {0x3D, 0x8E}, {0x00, 0x8F},
  103. {0x3D, 0x90}, {0x00, 0x91},
  104. {0xCD, 0x92}, {0x0f, 0x93},
  105. {0xf7, 0x94}, {0x0f, 0x95},
  106. {0x0C, 0x96}, {0x00, 0x97},
  107. {0x00, 0x98}, {0x66, 0x99},
  108. {0x05, 0x9A}, {0x00, 0x9B},
  109. {0x04, 0x9C}, {0x00, 0x9D},
  110. {0x08, 0x9E}, {0x00, 0x9F},
  111. {0x2D, 0xC0}, {0x2D, 0xC1},
  112. {0x3A, 0xC2}, {0x05, 0xC3},
  113. {0x04, 0xC4}, {0x3F, 0xC5},
  114. {0x00, 0xC6}, {0x00, 0xC7},
  115. {0x50, 0xC8}, {0x3C, 0xC9},
  116. {0x28, 0xCA}, {0xD8, 0xCB},
  117. {0x14, 0xCC}, {0xEC, 0xCD},
  118. {0x32, 0xCE}, {0xDD, 0xCF},
  119. {0x32, 0xD0}, {0xDD, 0xD1},
  120. {0x6A, 0xD2}, {0x50, 0xD3},
  121. {0x00, 0xD4}, {0x00, 0xD5},
  122. {0x00, 0xD6});
  123. break;
  124. default:
  125. break;
  126. }
  127. err += sn9c102_i2c_write(cam, 0x20, 0x00);
  128. err += sn9c102_i2c_write(cam, 0x21, 0xd6);
  129. err += sn9c102_i2c_write(cam, 0x25, 0x06);
  130. return err;
  131. }
  132. static int hv7131r_get_ctrl(struct sn9c102_device* cam,
  133. struct v4l2_control* ctrl)
  134. {
  135. switch (ctrl->id) {
  136. case V4L2_CID_GAIN:
  137. if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0)
  138. return -EIO;
  139. return 0;
  140. case V4L2_CID_RED_BALANCE:
  141. if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0)
  142. return -EIO;
  143. ctrl->value = ctrl->value & 0x3f;
  144. return 0;
  145. case V4L2_CID_BLUE_BALANCE:
  146. if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0)
  147. return -EIO;
  148. ctrl->value = ctrl->value & 0x3f;
  149. return 0;
  150. case SN9C102_V4L2_CID_GREEN_BALANCE:
  151. if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0)
  152. return -EIO;
  153. ctrl->value = ctrl->value & 0x3f;
  154. return 0;
  155. case V4L2_CID_BLACK_LEVEL:
  156. if ((ctrl->value = sn9c102_i2c_read(cam, 0x01)) < 0)
  157. return -EIO;
  158. ctrl->value = (ctrl->value & 0x08) ? 1 : 0;
  159. return 0;
  160. default:
  161. return -EINVAL;
  162. }
  163. }
  164. static int hv7131r_set_ctrl(struct sn9c102_device* cam,
  165. const struct v4l2_control* ctrl)
  166. {
  167. int err = 0;
  168. switch (ctrl->id) {
  169. case V4L2_CID_GAIN:
  170. err += sn9c102_i2c_write(cam, 0x30, ctrl->value);
  171. break;
  172. case V4L2_CID_RED_BALANCE:
  173. err += sn9c102_i2c_write(cam, 0x31, ctrl->value);
  174. break;
  175. case V4L2_CID_BLUE_BALANCE:
  176. err += sn9c102_i2c_write(cam, 0x33, ctrl->value);
  177. break;
  178. case SN9C102_V4L2_CID_GREEN_BALANCE:
  179. err += sn9c102_i2c_write(cam, 0x32, ctrl->value);
  180. break;
  181. case V4L2_CID_BLACK_LEVEL:
  182. {
  183. int r = sn9c102_i2c_read(cam, 0x01);
  184. if (r < 0)
  185. return -EIO;
  186. err += sn9c102_i2c_write(cam, 0x01,
  187. (ctrl->value<<3) | (r&0xf7));
  188. }
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. return err ? -EIO : 0;
  194. }
  195. static int hv7131r_set_crop(struct sn9c102_device* cam,
  196. const struct v4l2_rect* rect)
  197. {
  198. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  199. int err = 0;
  200. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1,
  201. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
  202. err += sn9c102_write_reg(cam, h_start, 0x12);
  203. err += sn9c102_write_reg(cam, v_start, 0x13);
  204. return err;
  205. }
  206. static int hv7131r_set_pix_format(struct sn9c102_device* cam,
  207. const struct v4l2_pix_format* pix)
  208. {
  209. int err = 0;
  210. switch (sn9c102_get_bridge(cam)) {
  211. case BRIDGE_SN9C103:
  212. if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
  213. err += sn9c102_write_reg(cam, 0xa0, 0x19);
  214. err += sn9c102_i2c_write(cam, 0x01, 0x04);
  215. } else {
  216. err += sn9c102_write_reg(cam, 0x30, 0x19);
  217. err += sn9c102_i2c_write(cam, 0x01, 0x04);
  218. }
  219. break;
  220. case BRIDGE_SN9C105:
  221. case BRIDGE_SN9C120:
  222. if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
  223. err += sn9c102_write_reg(cam, 0xa5, 0x17);
  224. err += sn9c102_i2c_write(cam, 0x01, 0x24);
  225. } else {
  226. err += sn9c102_write_reg(cam, 0xa3, 0x17);
  227. err += sn9c102_i2c_write(cam, 0x01, 0x04);
  228. }
  229. break;
  230. default:
  231. break;
  232. }
  233. return err;
  234. }
  235. static const struct sn9c102_sensor hv7131r = {
  236. .name = "HV7131R",
  237. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  238. .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120,
  239. .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
  240. .frequency = SN9C102_I2C_100KHZ,
  241. .interface = SN9C102_I2C_2WIRES,
  242. .i2c_slave_id = 0x11,
  243. .init = &hv7131r_init,
  244. .qctrl = {
  245. {
  246. .id = V4L2_CID_GAIN,
  247. .type = V4L2_CTRL_TYPE_INTEGER,
  248. .name = "global gain",
  249. .minimum = 0x00,
  250. .maximum = 0xff,
  251. .step = 0x01,
  252. .default_value = 0x40,
  253. .flags = 0,
  254. },
  255. {
  256. .id = V4L2_CID_RED_BALANCE,
  257. .type = V4L2_CTRL_TYPE_INTEGER,
  258. .name = "red balance",
  259. .minimum = 0x00,
  260. .maximum = 0x3f,
  261. .step = 0x01,
  262. .default_value = 0x08,
  263. .flags = 0,
  264. },
  265. {
  266. .id = V4L2_CID_BLUE_BALANCE,
  267. .type = V4L2_CTRL_TYPE_INTEGER,
  268. .name = "blue balance",
  269. .minimum = 0x00,
  270. .maximum = 0x3f,
  271. .step = 0x01,
  272. .default_value = 0x1a,
  273. .flags = 0,
  274. },
  275. {
  276. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  277. .type = V4L2_CTRL_TYPE_INTEGER,
  278. .name = "green balance",
  279. .minimum = 0x00,
  280. .maximum = 0x3f,
  281. .step = 0x01,
  282. .default_value = 0x2f,
  283. .flags = 0,
  284. },
  285. {
  286. .id = V4L2_CID_BLACK_LEVEL,
  287. .type = V4L2_CTRL_TYPE_BOOLEAN,
  288. .name = "auto black level compensation",
  289. .minimum = 0x00,
  290. .maximum = 0x01,
  291. .step = 0x01,
  292. .default_value = 0x00,
  293. .flags = 0,
  294. },
  295. },
  296. .get_ctrl = &hv7131r_get_ctrl,
  297. .set_ctrl = &hv7131r_set_ctrl,
  298. .cropcap = {
  299. .bounds = {
  300. .left = 0,
  301. .top = 0,
  302. .width = 640,
  303. .height = 480,
  304. },
  305. .defrect = {
  306. .left = 0,
  307. .top = 0,
  308. .width = 640,
  309. .height = 480,
  310. },
  311. },
  312. .set_crop = &hv7131r_set_crop,
  313. .pix_format = {
  314. .width = 640,
  315. .height = 480,
  316. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  317. .priv = 8,
  318. },
  319. .set_pix_format = &hv7131r_set_pix_format
  320. };
  321. int sn9c102_probe_hv7131r(struct sn9c102_device* cam)
  322. {
  323. int devid, err;
  324. err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x44, 0x02},
  325. {0x34, 0x01}, {0x20, 0x17},
  326. {0x34, 0x01}, {0x46, 0x01});
  327. devid = sn9c102_i2c_try_read(cam, &hv7131r, 0x00);
  328. if (err || devid < 0)
  329. return -EIO;
  330. if (devid != 0x02)
  331. return -ENODEV;
  332. sn9c102_attach_sensor(cam, &hv7131r);
  333. return 0;
  334. }