tv8532.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Quickcam cameras initialization data
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define MODULE_NAME "tv8532"
  18. #include "gspca.h"
  19. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  20. MODULE_DESCRIPTION("TV8532 USB Camera Driver");
  21. MODULE_LICENSE("GPL");
  22. /* specific webcam descriptor */
  23. struct sd {
  24. struct gspca_dev gspca_dev; /* !! must be the first item */
  25. __u8 packet;
  26. };
  27. static const struct v4l2_pix_format sif_mode[] = {
  28. {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  29. .bytesperline = 176,
  30. .sizeimage = 176 * 144,
  31. .colorspace = V4L2_COLORSPACE_SRGB,
  32. .priv = 1},
  33. {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  34. .bytesperline = 352,
  35. .sizeimage = 352 * 288,
  36. .colorspace = V4L2_COLORSPACE_SRGB,
  37. .priv = 0},
  38. };
  39. /* TV-8532A (ICM532A) registers (LE) */
  40. #define R00_PART_CONTROL 0x00
  41. #define LATENT_CHANGE 0x80
  42. #define EXPO_CHANGE 0x04
  43. #define R01_TIMING_CONTROL_LOW 0x01
  44. #define CMD_EEprom_Open 0x30
  45. #define CMD_EEprom_Close 0x29
  46. #define R03_TABLE_ADDR 0x03
  47. #define R04_WTRAM_DATA_L 0x04
  48. #define R05_WTRAM_DATA_M 0x05
  49. #define R06_WTRAM_DATA_H 0x06
  50. #define R07_TABLE_LEN 0x07
  51. #define R08_RAM_WRITE_ACTION 0x08
  52. #define R0C_AD_WIDTHL 0x0c
  53. #define R0D_AD_WIDTHH 0x0d
  54. #define R0E_AD_HEIGHTL 0x0e
  55. #define R0F_AD_HEIGHTH 0x0f
  56. #define R10_AD_COL_BEGINL 0x10
  57. #define R11_AD_COL_BEGINH 0x11
  58. #define MIRROR 0x04 /* [10] */
  59. #define R14_AD_ROW_BEGINL 0x14
  60. #define R15_AD_ROWBEGINH 0x15
  61. #define R1C_AD_EXPOSE_TIMEL 0x1c
  62. #define R20_GAIN_G1L 0x20
  63. #define R21_GAIN_G1H 0x21
  64. #define R22_GAIN_RL 0x22
  65. #define R23_GAIN_RH 0x23
  66. #define R24_GAIN_BL 0x24
  67. #define R25_GAIN_BH 0x25
  68. #define R26_GAIN_G2L 0x26
  69. #define R27_GAIN_G2H 0x27
  70. #define R28_QUANT 0x28
  71. #define R29_LINE 0x29
  72. #define R2C_POLARITY 0x2c
  73. #define R2D_POINT 0x2d
  74. #define R2E_POINTH 0x2e
  75. #define R2F_POINTB 0x2f
  76. #define R30_POINTBH 0x30
  77. #define R31_UPD 0x31
  78. #define R2A_HIGH_BUDGET 0x2a
  79. #define R2B_LOW_BUDGET 0x2b
  80. #define R34_VID 0x34
  81. #define R35_VIDH 0x35
  82. #define R36_PID 0x36
  83. #define R37_PIDH 0x37
  84. #define R39_Test1 0x39 /* GPIO */
  85. #define R3B_Test3 0x3b /* GPIO */
  86. #define R83_AD_IDH 0x83
  87. #define R91_AD_SLOPEREG 0x91
  88. #define R94_AD_BITCONTROL 0x94
  89. static const u8 eeprom_data[][3] = {
  90. /* dataH dataM dataL */
  91. {0x01, 0x00, 0x01},
  92. {0x01, 0x80, 0x11},
  93. {0x05, 0x00, 0x14},
  94. {0x05, 0x00, 0x1c},
  95. {0x0d, 0x00, 0x1e},
  96. {0x05, 0x00, 0x1f},
  97. {0x05, 0x05, 0x19},
  98. {0x05, 0x01, 0x1b},
  99. {0x05, 0x09, 0x1e},
  100. {0x0d, 0x89, 0x2e},
  101. {0x05, 0x89, 0x2f},
  102. {0x05, 0x0d, 0xd9},
  103. {0x05, 0x09, 0xf1},
  104. };
  105. /* write 1 byte */
  106. static void reg_w1(struct gspca_dev *gspca_dev,
  107. __u16 index, __u8 value)
  108. {
  109. gspca_dev->usb_buf[0] = value;
  110. usb_control_msg(gspca_dev->dev,
  111. usb_sndctrlpipe(gspca_dev->dev, 0),
  112. 0x02,
  113. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  114. 0, /* value */
  115. index, gspca_dev->usb_buf, 1, 500);
  116. }
  117. /* write 2 bytes */
  118. static void reg_w2(struct gspca_dev *gspca_dev,
  119. u16 index, u16 value)
  120. {
  121. gspca_dev->usb_buf[0] = value;
  122. gspca_dev->usb_buf[1] = value >> 8;
  123. usb_control_msg(gspca_dev->dev,
  124. usb_sndctrlpipe(gspca_dev->dev, 0),
  125. 0x02,
  126. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  127. 0, /* value */
  128. index, gspca_dev->usb_buf, 2, 500);
  129. }
  130. static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
  131. {
  132. int i;
  133. reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Open);
  134. for (i = 0; i < ARRAY_SIZE(eeprom_data); i++) {
  135. reg_w1(gspca_dev, R03_TABLE_ADDR, i);
  136. reg_w1(gspca_dev, R04_WTRAM_DATA_L, eeprom_data[i][2]);
  137. reg_w1(gspca_dev, R05_WTRAM_DATA_M, eeprom_data[i][1]);
  138. reg_w1(gspca_dev, R06_WTRAM_DATA_H, eeprom_data[i][0]);
  139. reg_w1(gspca_dev, R08_RAM_WRITE_ACTION, 0);
  140. }
  141. reg_w1(gspca_dev, R07_TABLE_LEN, i);
  142. reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Close);
  143. }
  144. /* this function is called at probe time */
  145. static int sd_config(struct gspca_dev *gspca_dev,
  146. const struct usb_device_id *id)
  147. {
  148. struct cam *cam;
  149. cam = &gspca_dev->cam;
  150. cam->cam_mode = sif_mode;
  151. cam->nmodes = ARRAY_SIZE(sif_mode);
  152. return 0;
  153. }
  154. static void tv_8532_setReg(struct gspca_dev *gspca_dev)
  155. {
  156. reg_w1(gspca_dev, R3B_Test3, 0x0a); /* Test0Sel = 10 */
  157. /******************************************************/
  158. reg_w1(gspca_dev, R0E_AD_HEIGHTL, 0x90);
  159. reg_w1(gspca_dev, R0F_AD_HEIGHTH, 0x01);
  160. reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, 0x018f);
  161. reg_w1(gspca_dev, R10_AD_COL_BEGINL, 0x44);
  162. /* begin active line */
  163. reg_w1(gspca_dev, R11_AD_COL_BEGINH, 0x00);
  164. /* mirror and digital gain */
  165. reg_w1(gspca_dev, R14_AD_ROW_BEGINL, 0x0a);
  166. reg_w1(gspca_dev, R94_AD_BITCONTROL, 0x02);
  167. reg_w1(gspca_dev, R91_AD_SLOPEREG, 0x00);
  168. reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE);
  169. /* = 0x84 */
  170. }
  171. /* this function is called at probe and resume time */
  172. static int sd_init(struct gspca_dev *gspca_dev)
  173. {
  174. tv_8532WriteEEprom(gspca_dev);
  175. return 0;
  176. }
  177. static void setexposure(struct gspca_dev *gspca_dev, s32 val)
  178. {
  179. reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, val);
  180. reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE);
  181. /* 0x84 */
  182. }
  183. static void setgain(struct gspca_dev *gspca_dev, s32 val)
  184. {
  185. reg_w2(gspca_dev, R20_GAIN_G1L, val);
  186. reg_w2(gspca_dev, R22_GAIN_RL, val);
  187. reg_w2(gspca_dev, R24_GAIN_BL, val);
  188. reg_w2(gspca_dev, R26_GAIN_G2L, val);
  189. }
  190. /* -- start the camera -- */
  191. static int sd_start(struct gspca_dev *gspca_dev)
  192. {
  193. struct sd *sd = (struct sd *) gspca_dev;
  194. reg_w1(gspca_dev, R0C_AD_WIDTHL, 0xe8); /* 0x20; 0x0c */
  195. reg_w1(gspca_dev, R0D_AD_WIDTHH, 0x03);
  196. /************************************************/
  197. reg_w1(gspca_dev, R28_QUANT, 0x90);
  198. /* 0x72 compressed mode 0x28 */
  199. if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  200. /* 176x144 */
  201. reg_w1(gspca_dev, R29_LINE, 0x41);
  202. /* CIF - 2 lines/packet */
  203. } else {
  204. /* 352x288 */
  205. reg_w1(gspca_dev, R29_LINE, 0x81);
  206. /* CIF - 2 lines/packet */
  207. }
  208. /************************************************/
  209. reg_w1(gspca_dev, R2C_POLARITY, 0x10); /* slow clock */
  210. reg_w1(gspca_dev, R2D_POINT, 0x14);
  211. reg_w1(gspca_dev, R2E_POINTH, 0x01);
  212. reg_w1(gspca_dev, R2F_POINTB, 0x12);
  213. reg_w1(gspca_dev, R30_POINTBH, 0x01);
  214. tv_8532_setReg(gspca_dev);
  215. /************************************************/
  216. reg_w1(gspca_dev, R31_UPD, 0x01); /* update registers */
  217. msleep(200);
  218. reg_w1(gspca_dev, R31_UPD, 0x00); /* end update */
  219. gspca_dev->empty_packet = 0; /* check the empty packets */
  220. sd->packet = 0; /* ignore the first packets */
  221. return 0;
  222. }
  223. static void sd_stopN(struct gspca_dev *gspca_dev)
  224. {
  225. reg_w1(gspca_dev, R3B_Test3, 0x0b); /* Test0Sel = 11 = GPIO */
  226. }
  227. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  228. u8 *data, /* isoc packet */
  229. int len) /* iso packet length */
  230. {
  231. struct sd *sd = (struct sd *) gspca_dev;
  232. int packet_type0, packet_type1;
  233. packet_type0 = packet_type1 = INTER_PACKET;
  234. if (gspca_dev->empty_packet) {
  235. gspca_dev->empty_packet = 0;
  236. sd->packet = gspca_dev->pixfmt.height / 2;
  237. packet_type0 = FIRST_PACKET;
  238. } else if (sd->packet == 0)
  239. return; /* 2 more lines in 352x288 ! */
  240. sd->packet--;
  241. if (sd->packet == 0)
  242. packet_type1 = LAST_PACKET;
  243. /* each packet contains:
  244. * - header 2 bytes
  245. * - RGRG line
  246. * - 4 bytes
  247. * - GBGB line
  248. * - 4 bytes
  249. */
  250. gspca_frame_add(gspca_dev, packet_type0,
  251. data + 2, gspca_dev->pixfmt.width);
  252. gspca_frame_add(gspca_dev, packet_type1,
  253. data + gspca_dev->pixfmt.width + 5,
  254. gspca_dev->pixfmt.width);
  255. }
  256. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  257. {
  258. struct gspca_dev *gspca_dev =
  259. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  260. gspca_dev->usb_err = 0;
  261. if (!gspca_dev->streaming)
  262. return 0;
  263. switch (ctrl->id) {
  264. case V4L2_CID_EXPOSURE:
  265. setexposure(gspca_dev, ctrl->val);
  266. break;
  267. case V4L2_CID_GAIN:
  268. setgain(gspca_dev, ctrl->val);
  269. break;
  270. }
  271. return gspca_dev->usb_err;
  272. }
  273. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  274. .s_ctrl = sd_s_ctrl,
  275. };
  276. static int sd_init_controls(struct gspca_dev *gspca_dev)
  277. {
  278. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  279. gspca_dev->vdev.ctrl_handler = hdl;
  280. v4l2_ctrl_handler_init(hdl, 2);
  281. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  282. V4L2_CID_EXPOSURE, 0, 0x18f, 1, 0x18f);
  283. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  284. V4L2_CID_GAIN, 0, 0x7ff, 1, 0x100);
  285. if (hdl->error) {
  286. pr_err("Could not initialize controls\n");
  287. return hdl->error;
  288. }
  289. return 0;
  290. }
  291. /* sub-driver description */
  292. static const struct sd_desc sd_desc = {
  293. .name = MODULE_NAME,
  294. .config = sd_config,
  295. .init = sd_init,
  296. .init_controls = sd_init_controls,
  297. .start = sd_start,
  298. .stopN = sd_stopN,
  299. .pkt_scan = sd_pkt_scan,
  300. };
  301. /* -- module initialisation -- */
  302. static const struct usb_device_id device_table[] = {
  303. {USB_DEVICE(0x046d, 0x0920)},
  304. {USB_DEVICE(0x046d, 0x0921)},
  305. {USB_DEVICE(0x0545, 0x808b)},
  306. {USB_DEVICE(0x0545, 0x8333)},
  307. {USB_DEVICE(0x0923, 0x010f)},
  308. {}
  309. };
  310. MODULE_DEVICE_TABLE(usb, device_table);
  311. /* -- device connect -- */
  312. static int sd_probe(struct usb_interface *intf,
  313. const struct usb_device_id *id)
  314. {
  315. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  316. THIS_MODULE);
  317. }
  318. static struct usb_driver sd_driver = {
  319. .name = MODULE_NAME,
  320. .id_table = device_table,
  321. .probe = sd_probe,
  322. .disconnect = gspca_disconnect,
  323. #ifdef CONFIG_PM
  324. .suspend = gspca_suspend,
  325. .resume = gspca_resume,
  326. .reset_resume = gspca_resume,
  327. #endif
  328. };
  329. module_usb_driver(sd_driver);