sn9c102_hv7131r.c 10 KB

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