stv06xx_st6422.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Support for the sensor part which is integrated (I think) into the
  3. * st6422 stv06xx alike bridge, as its integrated there are no i2c writes
  4. * but instead direct bridge writes.
  5. *
  6. * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com>
  7. *
  8. * Strongly based on qc-usb-messenger, which is:
  9. * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
  10. * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
  11. * Copyright (c) 2002, 2003 Tuukka Toivonen
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include "stv06xx_st6422.h"
  30. static struct v4l2_pix_format st6422_mode[] = {
  31. /* Note we actually get 124 lines of data, of which we skip the 4st
  32. 4 as they are garbage */
  33. {
  34. 162,
  35. 120,
  36. V4L2_PIX_FMT_SGRBG8,
  37. V4L2_FIELD_NONE,
  38. .sizeimage = 162 * 120,
  39. .bytesperline = 162,
  40. .colorspace = V4L2_COLORSPACE_SRGB,
  41. .priv = 1
  42. },
  43. /* Note we actually get 248 lines of data, of which we skip the 4st
  44. 4 as they are garbage, and we tell the app it only gets the
  45. first 240 of the 244 lines it actually gets, so that it ignores
  46. the last 4. */
  47. {
  48. 324,
  49. 240,
  50. V4L2_PIX_FMT_SGRBG8,
  51. V4L2_FIELD_NONE,
  52. .sizeimage = 324 * 244,
  53. .bytesperline = 324,
  54. .colorspace = V4L2_COLORSPACE_SRGB,
  55. .priv = 0
  56. },
  57. };
  58. /* V4L2 controls supported by the driver */
  59. static int setbrightness(struct sd *sd, s32 val);
  60. static int setcontrast(struct sd *sd, s32 val);
  61. static int setgain(struct sd *sd, u8 gain);
  62. static int setexposure(struct sd *sd, s16 expo);
  63. static int st6422_s_ctrl(struct v4l2_ctrl *ctrl)
  64. {
  65. struct gspca_dev *gspca_dev =
  66. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  67. struct sd *sd = (struct sd *)gspca_dev;
  68. int err = -EINVAL;
  69. switch (ctrl->id) {
  70. case V4L2_CID_BRIGHTNESS:
  71. err = setbrightness(sd, ctrl->val);
  72. break;
  73. case V4L2_CID_CONTRAST:
  74. err = setcontrast(sd, ctrl->val);
  75. break;
  76. case V4L2_CID_GAIN:
  77. err = setgain(sd, ctrl->val);
  78. break;
  79. case V4L2_CID_EXPOSURE:
  80. err = setexposure(sd, ctrl->val);
  81. break;
  82. }
  83. /* commit settings */
  84. if (err >= 0)
  85. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  86. sd->gspca_dev.usb_err = err;
  87. return err;
  88. }
  89. static const struct v4l2_ctrl_ops st6422_ctrl_ops = {
  90. .s_ctrl = st6422_s_ctrl,
  91. };
  92. static int st6422_init_controls(struct sd *sd)
  93. {
  94. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  95. v4l2_ctrl_handler_init(hdl, 4);
  96. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  97. V4L2_CID_BRIGHTNESS, 0, 31, 1, 3);
  98. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  99. V4L2_CID_CONTRAST, 0, 15, 1, 11);
  100. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  101. V4L2_CID_EXPOSURE, 0, 1023, 1, 256);
  102. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  103. V4L2_CID_GAIN, 0, 255, 1, 64);
  104. return hdl->error;
  105. }
  106. static int st6422_probe(struct sd *sd)
  107. {
  108. if (sd->bridge != BRIDGE_ST6422)
  109. return -ENODEV;
  110. pr_info("st6422 sensor detected\n");
  111. sd->gspca_dev.cam.cam_mode = st6422_mode;
  112. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
  113. return 0;
  114. }
  115. static int st6422_init(struct sd *sd)
  116. {
  117. int err = 0, i;
  118. const u16 st6422_bridge_init[][2] = {
  119. { STV_ISO_ENABLE, 0x00 }, /* disable capture */
  120. { 0x1436, 0x00 },
  121. { 0x1432, 0x03 }, /* 0x00-0x1F brightness */
  122. { 0x143a, 0xf9 }, /* 0x00-0x0F contrast */
  123. { 0x0509, 0x38 }, /* R */
  124. { 0x050a, 0x38 }, /* G */
  125. { 0x050b, 0x38 }, /* B */
  126. { 0x050c, 0x2a },
  127. { 0x050d, 0x01 },
  128. { 0x1431, 0x00 }, /* 0x00-0x07 ??? */
  129. { 0x1433, 0x34 }, /* 160x120, 0x00-0x01 night filter */
  130. { 0x1438, 0x18 }, /* 640x480 */
  131. /* 18 bayes */
  132. /* 10 compressed? */
  133. { 0x1439, 0x00 },
  134. /* anti-noise? 0xa2 gives a perfect image */
  135. { 0x143b, 0x05 },
  136. { 0x143c, 0x00 }, /* 0x00-0x01 - ??? */
  137. /* shutter time 0x0000-0x03FF */
  138. /* low value give good picures on moving objects (but requires much light) */
  139. /* high value gives good picures in darkness (but tends to be overexposed) */
  140. { 0x143e, 0x01 },
  141. { 0x143d, 0x00 },
  142. { 0x1442, 0xe2 },
  143. /* write: 1x1x xxxx */
  144. /* read: 1x1x xxxx */
  145. /* bit 5 == button pressed and hold if 0 */
  146. /* write 0xe2,0xea */
  147. /* 0x144a */
  148. /* 0x00 init */
  149. /* bit 7 == button has been pressed, but not handled */
  150. /* interrupt */
  151. /* if(urb->iso_frame_desc[i].status == 0x80) { */
  152. /* if(urb->iso_frame_desc[i].status == 0x88) { */
  153. { 0x1500, 0xd0 },
  154. { 0x1500, 0xd0 },
  155. { 0x1500, 0x50 }, /* 0x00 - 0xFF 0x80 == compr ? */
  156. { 0x1501, 0xaf },
  157. /* high val-> light area gets darker */
  158. /* low val -> light area gets lighter */
  159. { 0x1502, 0xc2 },
  160. /* high val-> light area gets darker */
  161. /* low val -> light area gets lighter */
  162. { 0x1503, 0x45 },
  163. /* high val-> light area gets darker */
  164. /* low val -> light area gets lighter */
  165. { 0x1505, 0x02 },
  166. /* 2 : 324x248 80352 bytes */
  167. /* 7 : 248x162 40176 bytes */
  168. /* c+f: 162*124 20088 bytes */
  169. { 0x150e, 0x8e },
  170. { 0x150f, 0x37 },
  171. { 0x15c0, 0x00 },
  172. { 0x15c3, 0x08 }, /* 0x04/0x14 ... test pictures ??? */
  173. { 0x143f, 0x01 }, /* commit settings */
  174. };
  175. for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
  176. err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
  177. st6422_bridge_init[i][1]);
  178. }
  179. return err;
  180. }
  181. static int setbrightness(struct sd *sd, s32 val)
  182. {
  183. /* val goes from 0 -> 31 */
  184. return stv06xx_write_bridge(sd, 0x1432, val);
  185. }
  186. static int setcontrast(struct sd *sd, s32 val)
  187. {
  188. /* Val goes from 0 -> 15 */
  189. return stv06xx_write_bridge(sd, 0x143a, val | 0xf0);
  190. }
  191. static int setgain(struct sd *sd, u8 gain)
  192. {
  193. int err;
  194. /* Set red, green, blue, gain */
  195. err = stv06xx_write_bridge(sd, 0x0509, gain);
  196. if (err < 0)
  197. return err;
  198. err = stv06xx_write_bridge(sd, 0x050a, gain);
  199. if (err < 0)
  200. return err;
  201. err = stv06xx_write_bridge(sd, 0x050b, gain);
  202. if (err < 0)
  203. return err;
  204. /* 2 mystery writes */
  205. err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
  206. if (err < 0)
  207. return err;
  208. return stv06xx_write_bridge(sd, 0x050d, 0x01);
  209. }
  210. static int setexposure(struct sd *sd, s16 expo)
  211. {
  212. int err;
  213. err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
  214. if (err < 0)
  215. return err;
  216. return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
  217. }
  218. static int st6422_start(struct sd *sd)
  219. {
  220. int err;
  221. struct cam *cam = &sd->gspca_dev.cam;
  222. if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
  223. err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
  224. else
  225. err = stv06xx_write_bridge(sd, 0x1505, 0x02);
  226. if (err < 0)
  227. return err;
  228. /* commit settings */
  229. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  230. return (err < 0) ? err : 0;
  231. }
  232. static int st6422_stop(struct sd *sd)
  233. {
  234. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  235. PDEBUG(D_STREAM, "Halting stream");
  236. return 0;
  237. }