stv06xx_st6422.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
  66. int err = -EINVAL;
  67. switch (ctrl->id) {
  68. case V4L2_CID_BRIGHTNESS:
  69. err = setbrightness(sd, ctrl->val);
  70. break;
  71. case V4L2_CID_CONTRAST:
  72. err = setcontrast(sd, ctrl->val);
  73. break;
  74. case V4L2_CID_GAIN:
  75. err = setgain(sd, ctrl->val);
  76. break;
  77. case V4L2_CID_EXPOSURE:
  78. err = setexposure(sd, ctrl->val);
  79. break;
  80. }
  81. /* commit settings */
  82. if (err >= 0)
  83. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  84. sd->gspca_dev.usb_err = err;
  85. return err;
  86. }
  87. static const struct v4l2_ctrl_ops st6422_ctrl_ops = {
  88. .s_ctrl = st6422_s_ctrl,
  89. };
  90. static int st6422_init_controls(struct sd *sd)
  91. {
  92. struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
  93. v4l2_ctrl_handler_init(hdl, 4);
  94. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  95. V4L2_CID_BRIGHTNESS, 0, 31, 1, 3);
  96. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  97. V4L2_CID_CONTRAST, 0, 15, 1, 11);
  98. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  99. V4L2_CID_EXPOSURE, 0, 1023, 1, 256);
  100. v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
  101. V4L2_CID_GAIN, 0, 255, 1, 64);
  102. return hdl->error;
  103. }
  104. static int st6422_probe(struct sd *sd)
  105. {
  106. if (sd->bridge != BRIDGE_ST6422)
  107. return -ENODEV;
  108. pr_info("st6422 sensor detected\n");
  109. sd->gspca_dev.cam.cam_mode = st6422_mode;
  110. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
  111. return 0;
  112. }
  113. static int st6422_init(struct sd *sd)
  114. {
  115. int err = 0, i;
  116. const u16 st6422_bridge_init[][2] = {
  117. { STV_ISO_ENABLE, 0x00 }, /* disable capture */
  118. { 0x1436, 0x00 },
  119. { 0x1432, 0x03 }, /* 0x00-0x1F brightness */
  120. { 0x143a, 0xf9 }, /* 0x00-0x0F contrast */
  121. { 0x0509, 0x38 }, /* R */
  122. { 0x050a, 0x38 }, /* G */
  123. { 0x050b, 0x38 }, /* B */
  124. { 0x050c, 0x2a },
  125. { 0x050d, 0x01 },
  126. { 0x1431, 0x00 }, /* 0x00-0x07 ??? */
  127. { 0x1433, 0x34 }, /* 160x120, 0x00-0x01 night filter */
  128. { 0x1438, 0x18 }, /* 640x480 */
  129. /* 18 bayes */
  130. /* 10 compressed? */
  131. { 0x1439, 0x00 },
  132. /* anti-noise? 0xa2 gives a perfect image */
  133. { 0x143b, 0x05 },
  134. { 0x143c, 0x00 }, /* 0x00-0x01 - ??? */
  135. /* shutter time 0x0000-0x03FF */
  136. /* low value give good picures on moving objects (but requires much light) */
  137. /* high value gives good picures in darkness (but tends to be overexposed) */
  138. { 0x143e, 0x01 },
  139. { 0x143d, 0x00 },
  140. { 0x1442, 0xe2 },
  141. /* write: 1x1x xxxx */
  142. /* read: 1x1x xxxx */
  143. /* bit 5 == button pressed and hold if 0 */
  144. /* write 0xe2,0xea */
  145. /* 0x144a */
  146. /* 0x00 init */
  147. /* bit 7 == button has been pressed, but not handled */
  148. /* interrupt */
  149. /* if(urb->iso_frame_desc[i].status == 0x80) { */
  150. /* if(urb->iso_frame_desc[i].status == 0x88) { */
  151. { 0x1500, 0xd0 },
  152. { 0x1500, 0xd0 },
  153. { 0x1500, 0x50 }, /* 0x00 - 0xFF 0x80 == compr ? */
  154. { 0x1501, 0xaf },
  155. /* high val-> light area gets darker */
  156. /* low val -> light area gets lighter */
  157. { 0x1502, 0xc2 },
  158. /* high val-> light area gets darker */
  159. /* low val -> light area gets lighter */
  160. { 0x1503, 0x45 },
  161. /* high val-> light area gets darker */
  162. /* low val -> light area gets lighter */
  163. { 0x1505, 0x02 },
  164. /* 2 : 324x248 80352 bytes */
  165. /* 7 : 248x162 40176 bytes */
  166. /* c+f: 162*124 20088 bytes */
  167. { 0x150e, 0x8e },
  168. { 0x150f, 0x37 },
  169. { 0x15c0, 0x00 },
  170. { 0x15c3, 0x08 }, /* 0x04/0x14 ... test pictures ??? */
  171. { 0x143f, 0x01 }, /* commit settings */
  172. };
  173. for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
  174. err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
  175. st6422_bridge_init[i][1]);
  176. }
  177. return err;
  178. }
  179. static int setbrightness(struct sd *sd, s32 val)
  180. {
  181. /* val goes from 0 -> 31 */
  182. return stv06xx_write_bridge(sd, 0x1432, val);
  183. }
  184. static int setcontrast(struct sd *sd, s32 val)
  185. {
  186. /* Val goes from 0 -> 15 */
  187. return stv06xx_write_bridge(sd, 0x143a, val | 0xf0);
  188. }
  189. static int setgain(struct sd *sd, u8 gain)
  190. {
  191. int err;
  192. /* Set red, green, blue, gain */
  193. err = stv06xx_write_bridge(sd, 0x0509, gain);
  194. if (err < 0)
  195. return err;
  196. err = stv06xx_write_bridge(sd, 0x050a, gain);
  197. if (err < 0)
  198. return err;
  199. err = stv06xx_write_bridge(sd, 0x050b, gain);
  200. if (err < 0)
  201. return err;
  202. /* 2 mystery writes */
  203. err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
  204. if (err < 0)
  205. return err;
  206. return stv06xx_write_bridge(sd, 0x050d, 0x01);
  207. }
  208. static int setexposure(struct sd *sd, s16 expo)
  209. {
  210. int err;
  211. err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
  212. if (err < 0)
  213. return err;
  214. return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
  215. }
  216. static int st6422_start(struct sd *sd)
  217. {
  218. int err;
  219. struct cam *cam = &sd->gspca_dev.cam;
  220. if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
  221. err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
  222. else
  223. err = stv06xx_write_bridge(sd, 0x1505, 0x02);
  224. if (err < 0)
  225. return err;
  226. /* commit settings */
  227. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  228. return (err < 0) ? err : 0;
  229. }
  230. static int st6422_stop(struct sd *sd)
  231. {
  232. PDEBUG(D_STREAM, "Halting stream");
  233. return 0;
  234. }