tv8532.c 9.8 KB

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