tv8532.c 11 KB

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