stk1135.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Syntek STK1135 subdriver
  3. *
  4. * Copyright (c) 2013 Ondrej Zary
  5. *
  6. * Based on Syntekdriver (stk11xx) by Nicolas VIVIEN:
  7. * http://syntekdriver.sourceforge.net
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define MODULE_NAME "stk1135"
  25. #include "gspca.h"
  26. #include "stk1135.h"
  27. MODULE_AUTHOR("Ondrej Zary");
  28. MODULE_DESCRIPTION("Syntek STK1135 USB Camera Driver");
  29. MODULE_LICENSE("GPL");
  30. /* specific webcam descriptor */
  31. struct sd {
  32. struct gspca_dev gspca_dev; /* !! must be the first item */
  33. u8 pkt_seq;
  34. u8 sensor_page;
  35. bool flip_status;
  36. u8 flip_debounce;
  37. struct v4l2_ctrl *hflip;
  38. struct v4l2_ctrl *vflip;
  39. };
  40. static const struct v4l2_pix_format stk1135_modes[] = {
  41. {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  42. .bytesperline = 160,
  43. .sizeimage = 160 * 120,
  44. .colorspace = V4L2_COLORSPACE_SRGB},
  45. {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  46. .bytesperline = 176,
  47. .sizeimage = 176 * 144,
  48. .colorspace = V4L2_COLORSPACE_SRGB},
  49. {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  50. .bytesperline = 320,
  51. .sizeimage = 320 * 240,
  52. .colorspace = V4L2_COLORSPACE_SRGB},
  53. {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  54. .bytesperline = 352,
  55. .sizeimage = 352 * 288,
  56. .colorspace = V4L2_COLORSPACE_SRGB},
  57. {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  58. .bytesperline = 640,
  59. .sizeimage = 640 * 480,
  60. .colorspace = V4L2_COLORSPACE_SRGB},
  61. {720, 576, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  62. .bytesperline = 720,
  63. .sizeimage = 720 * 576,
  64. .colorspace = V4L2_COLORSPACE_SRGB},
  65. {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  66. .bytesperline = 800,
  67. .sizeimage = 800 * 600,
  68. .colorspace = V4L2_COLORSPACE_SRGB},
  69. {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  70. .bytesperline = 1024,
  71. .sizeimage = 1024 * 768,
  72. .colorspace = V4L2_COLORSPACE_SRGB},
  73. {1280, 1024, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  74. .bytesperline = 1280,
  75. .sizeimage = 1280 * 1024,
  76. .colorspace = V4L2_COLORSPACE_SRGB},
  77. };
  78. /* -- read a register -- */
  79. static u8 reg_r(struct gspca_dev *gspca_dev, u16 index)
  80. {
  81. struct usb_device *dev = gspca_dev->dev;
  82. int ret;
  83. if (gspca_dev->usb_err < 0)
  84. return 0;
  85. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  86. 0x00,
  87. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  88. 0x00,
  89. index,
  90. gspca_dev->usb_buf, 1,
  91. 500);
  92. PDEBUG(D_USBI, "reg_r 0x%x=0x%02x", index, gspca_dev->usb_buf[0]);
  93. if (ret < 0) {
  94. pr_err("reg_r 0x%x err %d\n", index, ret);
  95. gspca_dev->usb_err = ret;
  96. return 0;
  97. }
  98. return gspca_dev->usb_buf[0];
  99. }
  100. /* -- write a register -- */
  101. static void reg_w(struct gspca_dev *gspca_dev, u16 index, u8 val)
  102. {
  103. int ret;
  104. struct usb_device *dev = gspca_dev->dev;
  105. if (gspca_dev->usb_err < 0)
  106. return;
  107. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  108. 0x01,
  109. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  110. val,
  111. index,
  112. NULL,
  113. 0,
  114. 500);
  115. PDEBUG(D_USBO, "reg_w 0x%x:=0x%02x", index, val);
  116. if (ret < 0) {
  117. pr_err("reg_w 0x%x err %d\n", index, ret);
  118. gspca_dev->usb_err = ret;
  119. }
  120. }
  121. static void reg_w_mask(struct gspca_dev *gspca_dev, u16 index, u8 val, u8 mask)
  122. {
  123. val = (reg_r(gspca_dev, index) & ~mask) | (val & mask);
  124. reg_w(gspca_dev, index, val);
  125. }
  126. /* this function is called at probe time */
  127. static int sd_config(struct gspca_dev *gspca_dev,
  128. const struct usb_device_id *id)
  129. {
  130. gspca_dev->cam.cam_mode = stk1135_modes;
  131. gspca_dev->cam.nmodes = ARRAY_SIZE(stk1135_modes);
  132. return 0;
  133. }
  134. static int stk1135_serial_wait_ready(struct gspca_dev *gspca_dev)
  135. {
  136. int i = 0;
  137. u8 val;
  138. do {
  139. val = reg_r(gspca_dev, STK1135_REG_SICTL + 1);
  140. if (i++ > 500) { /* maximum retry count */
  141. pr_err("serial bus timeout: status=0x%02x\n", val);
  142. return -1;
  143. }
  144. /* repeat if BUSY or WRITE/READ not finished */
  145. } while ((val & 0x10) || !(val & 0x05));
  146. return 0;
  147. }
  148. static u8 sensor_read_8(struct gspca_dev *gspca_dev, u8 addr)
  149. {
  150. reg_w(gspca_dev, STK1135_REG_SBUSR, addr);
  151. /* begin read */
  152. reg_w(gspca_dev, STK1135_REG_SICTL, 0x20);
  153. /* wait until finished */
  154. if (stk1135_serial_wait_ready(gspca_dev)) {
  155. pr_err("Sensor read failed\n");
  156. return 0;
  157. }
  158. return reg_r(gspca_dev, STK1135_REG_SBUSR + 1);
  159. }
  160. static u16 sensor_read_16(struct gspca_dev *gspca_dev, u8 addr)
  161. {
  162. return (sensor_read_8(gspca_dev, addr) << 8) |
  163. sensor_read_8(gspca_dev, 0xf1);
  164. }
  165. static void sensor_write_8(struct gspca_dev *gspca_dev, u8 addr, u8 data)
  166. {
  167. /* load address and data registers */
  168. reg_w(gspca_dev, STK1135_REG_SBUSW, addr);
  169. reg_w(gspca_dev, STK1135_REG_SBUSW + 1, data);
  170. /* begin write */
  171. reg_w(gspca_dev, STK1135_REG_SICTL, 0x01);
  172. /* wait until finished */
  173. if (stk1135_serial_wait_ready(gspca_dev)) {
  174. pr_err("Sensor write failed\n");
  175. return;
  176. }
  177. }
  178. static void sensor_write_16(struct gspca_dev *gspca_dev, u8 addr, u16 data)
  179. {
  180. sensor_write_8(gspca_dev, addr, data >> 8);
  181. sensor_write_8(gspca_dev, 0xf1, data & 0xff);
  182. }
  183. static void sensor_set_page(struct gspca_dev *gspca_dev, u8 page)
  184. {
  185. struct sd *sd = (struct sd *) gspca_dev;
  186. if (page != sd->sensor_page) {
  187. sensor_write_16(gspca_dev, 0xf0, page);
  188. sd->sensor_page = page;
  189. }
  190. }
  191. static u16 sensor_read(struct gspca_dev *gspca_dev, u16 reg)
  192. {
  193. sensor_set_page(gspca_dev, reg >> 8);
  194. return sensor_read_16(gspca_dev, reg & 0xff);
  195. }
  196. static void sensor_write(struct gspca_dev *gspca_dev, u16 reg, u16 val)
  197. {
  198. sensor_set_page(gspca_dev, reg >> 8);
  199. sensor_write_16(gspca_dev, reg & 0xff, val);
  200. }
  201. static void sensor_write_mask(struct gspca_dev *gspca_dev,
  202. u16 reg, u16 val, u16 mask)
  203. {
  204. val = (sensor_read(gspca_dev, reg) & ~mask) | (val & mask);
  205. sensor_write(gspca_dev, reg, val);
  206. }
  207. struct sensor_val {
  208. u16 reg;
  209. u16 val;
  210. };
  211. /* configure MT9M112 sensor */
  212. static void stk1135_configure_mt9m112(struct gspca_dev *gspca_dev)
  213. {
  214. static const struct sensor_val cfg[] = {
  215. /* restart&reset, chip enable, reserved */
  216. { 0x00d, 0x000b }, { 0x00d, 0x0008 }, { 0x035, 0x0022 },
  217. /* mode ctl: AWB on, AE both, clip aper corr, defect corr, AE */
  218. { 0x106, 0x700e },
  219. { 0x2dd, 0x18e0 }, /* B-R thresholds, */
  220. /* AWB */
  221. { 0x21f, 0x0180 }, /* Cb and Cr limits */
  222. { 0x220, 0xc814 }, { 0x221, 0x8080 }, /* lum limits, RGB gain */
  223. { 0x222, 0xa078 }, { 0x223, 0xa078 }, /* R, B limit */
  224. { 0x224, 0x5f20 }, { 0x228, 0xea02 }, /* mtx adj lim, adv ctl */
  225. { 0x229, 0x867a }, /* wide gates */
  226. /* Color correction */
  227. /* imager gains base, delta, delta signs */
  228. { 0x25e, 0x594c }, { 0x25f, 0x4d51 }, { 0x260, 0x0002 },
  229. /* AWB adv ctl 2, gain offs */
  230. { 0x2ef, 0x0008 }, { 0x2f2, 0x0000 },
  231. /* base matrix signs, scale K1-5, K6-9 */
  232. { 0x202, 0x00ee }, { 0x203, 0x3923 }, { 0x204, 0x0724 },
  233. /* base matrix coef */
  234. { 0x209, 0x00cd }, { 0x20a, 0x0093 }, { 0x20b, 0x0004 },/*K1-3*/
  235. { 0x20c, 0x005c }, { 0x20d, 0x00d9 }, { 0x20e, 0x0053 },/*K4-6*/
  236. { 0x20f, 0x0008 }, { 0x210, 0x0091 }, { 0x211, 0x00cf },/*K7-9*/
  237. { 0x215, 0x0000 }, /* delta mtx signs */
  238. /* delta matrix coef */
  239. { 0x216, 0x0000 }, { 0x217, 0x0000 }, { 0x218, 0x0000 },/*D1-3*/
  240. { 0x219, 0x0000 }, { 0x21a, 0x0000 }, { 0x21b, 0x0000 },/*D4-6*/
  241. { 0x21c, 0x0000 }, { 0x21d, 0x0000 }, { 0x21e, 0x0000 },/*D7-9*/
  242. /* enable & disable manual WB to apply color corr. settings */
  243. { 0x106, 0xf00e }, { 0x106, 0x700e },
  244. /* Lens shading correction */
  245. { 0x180, 0x0007 }, /* control */
  246. /* vertical knee 0, 2+1, 4+3 */
  247. { 0x181, 0xde13 }, { 0x182, 0xebe2 }, { 0x183, 0x00f6 }, /* R */
  248. { 0x184, 0xe114 }, { 0x185, 0xeadd }, { 0x186, 0xfdf6 }, /* G */
  249. { 0x187, 0xe511 }, { 0x188, 0xede6 }, { 0x189, 0xfbf7 }, /* B */
  250. /* horizontal knee 0, 2+1, 4+3, 5 */
  251. { 0x18a, 0xd613 }, { 0x18b, 0xedec }, /* R .. */
  252. { 0x18c, 0xf9f2 }, { 0x18d, 0x0000 }, /* .. R */
  253. { 0x18e, 0xd815 }, { 0x18f, 0xe9ea }, /* G .. */
  254. { 0x190, 0xf9f1 }, { 0x191, 0x0002 }, /* .. G */
  255. { 0x192, 0xde10 }, { 0x193, 0xefef }, /* B .. */
  256. { 0x194, 0xfbf4 }, { 0x195, 0x0002 }, /* .. B */
  257. /* vertical knee 6+5, 8+7 */
  258. { 0x1b6, 0x0e06 }, { 0x1b7, 0x2713 }, /* R */
  259. { 0x1b8, 0x1106 }, { 0x1b9, 0x2713 }, /* G */
  260. { 0x1ba, 0x0c03 }, { 0x1bb, 0x2a0f }, /* B */
  261. /* horizontal knee 7+6, 9+8, 10 */
  262. { 0x1bc, 0x1208 }, { 0x1bd, 0x1a16 }, { 0x1be, 0x0022 }, /* R */
  263. { 0x1bf, 0x150a }, { 0x1c0, 0x1c1a }, { 0x1c1, 0x002d }, /* G */
  264. { 0x1c2, 0x1109 }, { 0x1c3, 0x1414 }, { 0x1c4, 0x002a }, /* B */
  265. { 0x106, 0x740e }, /* enable lens shading correction */
  266. /* Gamma correction - context A */
  267. { 0x153, 0x0b03 }, { 0x154, 0x4722 }, { 0x155, 0xac82 },
  268. { 0x156, 0xdac7 }, { 0x157, 0xf5e9 }, { 0x158, 0xff00 },
  269. /* Gamma correction - context B */
  270. { 0x1dc, 0x0b03 }, { 0x1dd, 0x4722 }, { 0x1de, 0xac82 },
  271. { 0x1df, 0xdac7 }, { 0x1e0, 0xf5e9 }, { 0x1e1, 0xff00 },
  272. /* output format: RGB, invert output pixclock, output bayer */
  273. { 0x13a, 0x4300 }, { 0x19b, 0x4300 }, /* for context A, B */
  274. { 0x108, 0x0180 }, /* format control - enable bayer row flip */
  275. { 0x22f, 0xd100 }, { 0x29c, 0xd100 }, /* AE A, B */
  276. /* default prg conf, prg ctl - by 0x2d2, prg advance - PA1 */
  277. { 0x2d2, 0x0000 }, { 0x2cc, 0x0004 }, { 0x2cb, 0x0001 },
  278. { 0x22e, 0x0c3c }, { 0x267, 0x1010 }, /* AE tgt ctl, gain lim */
  279. /* PLL */
  280. { 0x065, 0xa000 }, /* clk ctl - enable PLL (clear bit 14) */
  281. { 0x066, 0x2003 }, { 0x067, 0x0501 }, /* PLL M=128, N=3, P=1 */
  282. { 0x065, 0x2000 }, /* disable PLL bypass (clear bit 15) */
  283. { 0x005, 0x01b8 }, { 0x007, 0x00d8 }, /* horiz blanking B, A */
  284. /* AE line size, shutter delay limit */
  285. { 0x239, 0x06c0 }, { 0x23b, 0x040e }, /* for context A */
  286. { 0x23a, 0x06c0 }, { 0x23c, 0x0564 }, /* for context B */
  287. /* shutter width basis 60Hz, 50Hz */
  288. { 0x257, 0x0208 }, { 0x258, 0x0271 }, /* for context A */
  289. { 0x259, 0x0209 }, { 0x25a, 0x0271 }, /* for context B */
  290. { 0x25c, 0x120d }, { 0x25d, 0x1712 }, /* flicker 60Hz, 50Hz */
  291. { 0x264, 0x5e1c }, /* reserved */
  292. /* flicker, AE gain limits, gain zone limits */
  293. { 0x25b, 0x0003 }, { 0x236, 0x7810 }, { 0x237, 0x8304 },
  294. { 0x008, 0x0021 }, /* vert blanking A */
  295. };
  296. int i;
  297. u16 width, height;
  298. for (i = 0; i < ARRAY_SIZE(cfg); i++)
  299. sensor_write(gspca_dev, cfg[i].reg, cfg[i].val);
  300. /* set output size */
  301. width = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].width;
  302. height = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].height;
  303. if (width <= 640) { /* use context A (half readout speed by default) */
  304. sensor_write(gspca_dev, 0x1a7, width);
  305. sensor_write(gspca_dev, 0x1aa, height);
  306. /* set read mode context A */
  307. sensor_write(gspca_dev, 0x0c8, 0x0000);
  308. /* set resize, read mode, vblank, hblank context A */
  309. sensor_write(gspca_dev, 0x2c8, 0x0000);
  310. } else { /* use context B (full readout speed by default) */
  311. sensor_write(gspca_dev, 0x1a1, width);
  312. sensor_write(gspca_dev, 0x1a4, height);
  313. /* set read mode context B */
  314. sensor_write(gspca_dev, 0x0c8, 0x0008);
  315. /* set resize, read mode, vblank, hblank context B */
  316. sensor_write(gspca_dev, 0x2c8, 0x040b);
  317. }
  318. }
  319. static void stk1135_configure_clock(struct gspca_dev *gspca_dev)
  320. {
  321. /* configure SCLKOUT */
  322. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x12);
  323. /* set 1 clock per pixel */
  324. /* and positive edge clocked pulse high when pixel counter = 0 */
  325. reg_w(gspca_dev, STK1135_REG_TCP1 + 0, 0x41);
  326. reg_w(gspca_dev, STK1135_REG_TCP1 + 1, 0x00);
  327. reg_w(gspca_dev, STK1135_REG_TCP1 + 2, 0x00);
  328. reg_w(gspca_dev, STK1135_REG_TCP1 + 3, 0x00);
  329. /* enable CLKOUT for sensor */
  330. reg_w(gspca_dev, STK1135_REG_SENSO + 0, 0x10);
  331. /* disable STOP clock */
  332. reg_w(gspca_dev, STK1135_REG_SENSO + 1, 0x00);
  333. /* set lower 8 bits of PLL feedback divider */
  334. reg_w(gspca_dev, STK1135_REG_SENSO + 3, 0x07);
  335. /* set other PLL parameters */
  336. reg_w(gspca_dev, STK1135_REG_PLLFD, 0x06);
  337. /* enable timing generator */
  338. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x80);
  339. /* enable PLL */
  340. reg_w(gspca_dev, STK1135_REG_SENSO + 2, 0x04);
  341. /* set serial interface clock divider (30MHz/0x1f*16+2) = 60240 kHz) */
  342. reg_w(gspca_dev, STK1135_REG_SICTL + 2, 0x1f);
  343. }
  344. static void stk1135_camera_disable(struct gspca_dev *gspca_dev)
  345. {
  346. /* set capture end Y position to 0 */
  347. reg_w(gspca_dev, STK1135_REG_CIEPO + 2, 0x00);
  348. reg_w(gspca_dev, STK1135_REG_CIEPO + 3, 0x00);
  349. /* disable capture */
  350. reg_w_mask(gspca_dev, STK1135_REG_SCTRL, 0x00, 0x80);
  351. /* enable sensor standby and diasble chip enable */
  352. sensor_write_mask(gspca_dev, 0x00d, 0x0004, 0x000c);
  353. /* disable PLL */
  354. reg_w_mask(gspca_dev, STK1135_REG_SENSO + 2, 0x00, 0x01);
  355. /* disable timing generator */
  356. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x00);
  357. /* enable STOP clock */
  358. reg_w(gspca_dev, STK1135_REG_SENSO + 1, 0x20);
  359. /* disable CLKOUT for sensor */
  360. reg_w(gspca_dev, STK1135_REG_SENSO, 0x00);
  361. /* disable sensor (GPIO5) and enable GPIO0,3,6 (?) - sensor standby? */
  362. reg_w(gspca_dev, STK1135_REG_GCTRL, 0x49);
  363. }
  364. /* this function is called at probe and resume time */
  365. static int sd_init(struct gspca_dev *gspca_dev)
  366. {
  367. u16 sensor_id;
  368. char *sensor_name;
  369. struct sd *sd = (struct sd *) gspca_dev;
  370. /* set GPIO3,4,5,6 direction to output */
  371. reg_w(gspca_dev, STK1135_REG_GCTRL + 2, 0x78);
  372. /* enable sensor (GPIO5) */
  373. reg_w(gspca_dev, STK1135_REG_GCTRL, (1 << 5));
  374. /* disable ROM interface */
  375. reg_w(gspca_dev, STK1135_REG_GCTRL + 3, 0x80);
  376. /* enable interrupts from GPIO8 (flip sensor) and GPIO9 (???) */
  377. reg_w(gspca_dev, STK1135_REG_ICTRL + 1, 0x00);
  378. reg_w(gspca_dev, STK1135_REG_ICTRL + 3, 0x03);
  379. /* enable remote wakeup from GPIO9 (???) */
  380. reg_w(gspca_dev, STK1135_REG_RMCTL + 1, 0x00);
  381. reg_w(gspca_dev, STK1135_REG_RMCTL + 3, 0x02);
  382. /* reset serial interface */
  383. reg_w(gspca_dev, STK1135_REG_SICTL, 0x80);
  384. reg_w(gspca_dev, STK1135_REG_SICTL, 0x00);
  385. /* set sensor address */
  386. reg_w(gspca_dev, STK1135_REG_SICTL + 3, 0xba);
  387. /* disable alt 2-wire serial interface */
  388. reg_w(gspca_dev, STK1135_REG_ASIC + 3, 0x00);
  389. stk1135_configure_clock(gspca_dev);
  390. /* read sensor ID */
  391. sd->sensor_page = 0xff;
  392. sensor_id = sensor_read(gspca_dev, 0x000);
  393. switch (sensor_id) {
  394. case 0x148c:
  395. sensor_name = "MT9M112";
  396. break;
  397. default:
  398. sensor_name = "unknown";
  399. }
  400. pr_info("Detected sensor type %s (0x%x)\n", sensor_name, sensor_id);
  401. stk1135_camera_disable(gspca_dev);
  402. return gspca_dev->usb_err;
  403. }
  404. /* -- start the camera -- */
  405. static int sd_start(struct gspca_dev *gspca_dev)
  406. {
  407. struct sd *sd = (struct sd *) gspca_dev;
  408. u16 width, height;
  409. /* enable sensor (GPIO5) */
  410. reg_w(gspca_dev, STK1135_REG_GCTRL, (1 << 5));
  411. stk1135_configure_clock(gspca_dev);
  412. /* set capture start position X = 0, Y = 0 */
  413. reg_w(gspca_dev, STK1135_REG_CISPO + 0, 0x00);
  414. reg_w(gspca_dev, STK1135_REG_CISPO + 1, 0x00);
  415. reg_w(gspca_dev, STK1135_REG_CISPO + 2, 0x00);
  416. reg_w(gspca_dev, STK1135_REG_CISPO + 3, 0x00);
  417. /* set capture end position */
  418. width = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].width;
  419. height = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].height;
  420. reg_w(gspca_dev, STK1135_REG_CIEPO + 0, width & 0xff);
  421. reg_w(gspca_dev, STK1135_REG_CIEPO + 1, width >> 8);
  422. reg_w(gspca_dev, STK1135_REG_CIEPO + 2, height & 0xff);
  423. reg_w(gspca_dev, STK1135_REG_CIEPO + 3, height >> 8);
  424. /* set 8-bit mode */
  425. reg_w(gspca_dev, STK1135_REG_SCTRL, 0x20);
  426. stk1135_configure_mt9m112(gspca_dev);
  427. /* enable capture */
  428. reg_w_mask(gspca_dev, STK1135_REG_SCTRL, 0x80, 0x80);
  429. if (gspca_dev->usb_err >= 0)
  430. PDEBUG(D_STREAM, "camera started alt: 0x%02x",
  431. gspca_dev->alt);
  432. sd->pkt_seq = 0;
  433. return gspca_dev->usb_err;
  434. }
  435. static void sd_stopN(struct gspca_dev *gspca_dev)
  436. {
  437. struct usb_device *dev = gspca_dev->dev;
  438. usb_set_interface(dev, gspca_dev->iface, 0);
  439. stk1135_camera_disable(gspca_dev);
  440. PDEBUG(D_STREAM, "camera stopped");
  441. }
  442. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  443. u8 *data, /* isoc packet */
  444. int len) /* iso packet length */
  445. {
  446. struct sd *sd = (struct sd *) gspca_dev;
  447. int skip = sizeof(struct stk1135_pkt_header);
  448. bool flip;
  449. enum gspca_packet_type pkt_type = INTER_PACKET;
  450. struct stk1135_pkt_header *hdr = (void *)data;
  451. u8 seq;
  452. if (len < 4) {
  453. PDEBUG(D_PACK, "received short packet (less than 4 bytes)");
  454. return;
  455. }
  456. /* GPIO 8 is flip sensor (1 = normal position, 0 = flipped to back) */
  457. flip = !(le16_to_cpu(hdr->gpio) & (1 << 8));
  458. /* it's a switch, needs software debounce */
  459. if (sd->flip_status != flip)
  460. sd->flip_debounce++;
  461. else
  462. sd->flip_debounce = 0;
  463. /* check sequence number (not present in new frame packets) */
  464. if (!(hdr->flags & STK1135_HDR_FRAME_START)) {
  465. seq = hdr->seq & STK1135_HDR_SEQ_MASK;
  466. if (seq != sd->pkt_seq) {
  467. PDEBUG(D_PACK, "received out-of-sequence packet");
  468. /* resync sequence and discard packet */
  469. sd->pkt_seq = seq;
  470. gspca_dev->last_packet_type = DISCARD_PACKET;
  471. return;
  472. }
  473. }
  474. sd->pkt_seq++;
  475. if (sd->pkt_seq > STK1135_HDR_SEQ_MASK)
  476. sd->pkt_seq = 0;
  477. if (len == sizeof(struct stk1135_pkt_header))
  478. return;
  479. if (hdr->flags & STK1135_HDR_FRAME_START) { /* new frame */
  480. skip = 8; /* the header is longer */
  481. gspca_frame_add(gspca_dev, LAST_PACKET, data, 0);
  482. pkt_type = FIRST_PACKET;
  483. }
  484. gspca_frame_add(gspca_dev, pkt_type, data + skip, len - skip);
  485. }
  486. static void sethflip(struct gspca_dev *gspca_dev, s32 val)
  487. {
  488. struct sd *sd = (struct sd *) gspca_dev;
  489. if (sd->flip_status)
  490. val = !val;
  491. sensor_write_mask(gspca_dev, 0x020, val ? 0x0002 : 0x0000 , 0x0002);
  492. }
  493. static void setvflip(struct gspca_dev *gspca_dev, s32 val)
  494. {
  495. struct sd *sd = (struct sd *) gspca_dev;
  496. if (sd->flip_status)
  497. val = !val;
  498. sensor_write_mask(gspca_dev, 0x020, val ? 0x0001 : 0x0000 , 0x0001);
  499. }
  500. static void stk1135_dq_callback(struct gspca_dev *gspca_dev)
  501. {
  502. struct sd *sd = (struct sd *) gspca_dev;
  503. if (sd->flip_debounce > 100) {
  504. sd->flip_status = !sd->flip_status;
  505. sethflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip));
  506. setvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->vflip));
  507. }
  508. }
  509. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  510. {
  511. struct gspca_dev *gspca_dev =
  512. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  513. gspca_dev->usb_err = 0;
  514. if (!gspca_dev->streaming)
  515. return 0;
  516. switch (ctrl->id) {
  517. case V4L2_CID_HFLIP:
  518. sethflip(gspca_dev, ctrl->val);
  519. break;
  520. case V4L2_CID_VFLIP:
  521. setvflip(gspca_dev, ctrl->val);
  522. break;
  523. }
  524. return gspca_dev->usb_err;
  525. }
  526. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  527. .s_ctrl = sd_s_ctrl,
  528. };
  529. static int sd_init_controls(struct gspca_dev *gspca_dev)
  530. {
  531. struct sd *sd = (struct sd *) gspca_dev;
  532. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  533. gspca_dev->vdev.ctrl_handler = hdl;
  534. v4l2_ctrl_handler_init(hdl, 2);
  535. sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  536. V4L2_CID_HFLIP, 0, 1, 1, 0);
  537. sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  538. V4L2_CID_VFLIP, 0, 1, 1, 0);
  539. if (hdl->error) {
  540. pr_err("Could not initialize controls\n");
  541. return hdl->error;
  542. }
  543. return 0;
  544. }
  545. /* sub-driver description */
  546. static const struct sd_desc sd_desc = {
  547. .name = MODULE_NAME,
  548. .config = sd_config,
  549. .init = sd_init,
  550. .init_controls = sd_init_controls,
  551. .start = sd_start,
  552. .stopN = sd_stopN,
  553. .pkt_scan = sd_pkt_scan,
  554. .dq_callback = stk1135_dq_callback,
  555. };
  556. /* -- module initialisation -- */
  557. static const struct usb_device_id device_table[] = {
  558. {USB_DEVICE(0x174f, 0x6a31)}, /* ASUS laptop, MT9M112 sensor */
  559. {}
  560. };
  561. MODULE_DEVICE_TABLE(usb, device_table);
  562. /* -- device connect -- */
  563. static int sd_probe(struct usb_interface *intf,
  564. const struct usb_device_id *id)
  565. {
  566. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  567. THIS_MODULE);
  568. }
  569. static struct usb_driver sd_driver = {
  570. .name = MODULE_NAME,
  571. .id_table = device_table,
  572. .probe = sd_probe,
  573. .disconnect = gspca_disconnect,
  574. #ifdef CONFIG_PM
  575. .suspend = gspca_suspend,
  576. .resume = gspca_resume,
  577. .reset_resume = gspca_resume,
  578. #endif
  579. };
  580. module_usb_driver(sd_driver);