stv06xx_st6422.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /* controls */
  31. enum e_ctrl {
  32. BRIGHTNESS,
  33. CONTRAST,
  34. GAIN,
  35. EXPOSURE,
  36. NCTRLS /* number of controls */
  37. };
  38. /* sensor settings */
  39. struct st6422_settings {
  40. struct gspca_ctrl ctrls[NCTRLS];
  41. };
  42. static struct v4l2_pix_format st6422_mode[] = {
  43. /* Note we actually get 124 lines of data, of which we skip the 4st
  44. 4 as they are garbage */
  45. {
  46. 162,
  47. 120,
  48. V4L2_PIX_FMT_SGRBG8,
  49. V4L2_FIELD_NONE,
  50. .sizeimage = 162 * 120,
  51. .bytesperline = 162,
  52. .colorspace = V4L2_COLORSPACE_SRGB,
  53. .priv = 1
  54. },
  55. /* Note we actually get 248 lines of data, of which we skip the 4st
  56. 4 as they are garbage, and we tell the app it only gets the
  57. first 240 of the 244 lines it actually gets, so that it ignores
  58. the last 4. */
  59. {
  60. 324,
  61. 240,
  62. V4L2_PIX_FMT_SGRBG8,
  63. V4L2_FIELD_NONE,
  64. .sizeimage = 324 * 244,
  65. .bytesperline = 324,
  66. .colorspace = V4L2_COLORSPACE_SRGB,
  67. .priv = 0
  68. },
  69. };
  70. /* V4L2 controls supported by the driver */
  71. static void st6422_set_brightness(struct gspca_dev *gspca_dev);
  72. static void st6422_set_contrast(struct gspca_dev *gspca_dev);
  73. static void st6422_set_gain(struct gspca_dev *gspca_dev);
  74. static void st6422_set_exposure(struct gspca_dev *gspca_dev);
  75. static const struct ctrl st6422_ctrl[NCTRLS] = {
  76. [BRIGHTNESS] = {
  77. {
  78. .id = V4L2_CID_BRIGHTNESS,
  79. .type = V4L2_CTRL_TYPE_INTEGER,
  80. .name = "Brightness",
  81. .minimum = 0,
  82. .maximum = 31,
  83. .step = 1,
  84. .default_value = 3
  85. },
  86. .set_control = st6422_set_brightness
  87. },
  88. [CONTRAST] = {
  89. {
  90. .id = V4L2_CID_CONTRAST,
  91. .type = V4L2_CTRL_TYPE_INTEGER,
  92. .name = "Contrast",
  93. .minimum = 0,
  94. .maximum = 15,
  95. .step = 1,
  96. .default_value = 11
  97. },
  98. .set_control = st6422_set_contrast
  99. },
  100. [GAIN] = {
  101. {
  102. .id = V4L2_CID_GAIN,
  103. .type = V4L2_CTRL_TYPE_INTEGER,
  104. .name = "Gain",
  105. .minimum = 0,
  106. .maximum = 255,
  107. .step = 1,
  108. .default_value = 64
  109. },
  110. .set_control = st6422_set_gain
  111. },
  112. [EXPOSURE] = {
  113. {
  114. .id = V4L2_CID_EXPOSURE,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .name = "Exposure",
  117. .minimum = 0,
  118. #define EXPOSURE_MAX 1023
  119. .maximum = EXPOSURE_MAX,
  120. .step = 1,
  121. .default_value = 256
  122. },
  123. .set_control = st6422_set_exposure
  124. },
  125. };
  126. static int st6422_probe(struct sd *sd)
  127. {
  128. struct st6422_settings *sensor_settings;
  129. if (sd->bridge != BRIDGE_ST6422)
  130. return -ENODEV;
  131. pr_info("st6422 sensor detected\n");
  132. sensor_settings = kmalloc(sizeof *sensor_settings, GFP_KERNEL);
  133. if (!sensor_settings)
  134. return -ENOMEM;
  135. sd->gspca_dev.cam.cam_mode = st6422_mode;
  136. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
  137. sd->gspca_dev.cam.ctrls = sensor_settings->ctrls;
  138. sd->desc.ctrls = st6422_ctrl;
  139. sd->desc.nctrls = ARRAY_SIZE(st6422_ctrl);
  140. sd->sensor_priv = sensor_settings;
  141. return 0;
  142. }
  143. static int st6422_init(struct sd *sd)
  144. {
  145. int err = 0, i;
  146. const u16 st6422_bridge_init[][2] = {
  147. { STV_ISO_ENABLE, 0x00 }, /* disable capture */
  148. { 0x1436, 0x00 },
  149. { 0x1432, 0x03 }, /* 0x00-0x1F brightness */
  150. { 0x143a, 0xf9 }, /* 0x00-0x0F contrast */
  151. { 0x0509, 0x38 }, /* R */
  152. { 0x050a, 0x38 }, /* G */
  153. { 0x050b, 0x38 }, /* B */
  154. { 0x050c, 0x2a },
  155. { 0x050d, 0x01 },
  156. { 0x1431, 0x00 }, /* 0x00-0x07 ??? */
  157. { 0x1433, 0x34 }, /* 160x120, 0x00-0x01 night filter */
  158. { 0x1438, 0x18 }, /* 640x480 */
  159. /* 18 bayes */
  160. /* 10 compressed? */
  161. { 0x1439, 0x00 },
  162. /* anti-noise? 0xa2 gives a perfect image */
  163. { 0x143b, 0x05 },
  164. { 0x143c, 0x00 }, /* 0x00-0x01 - ??? */
  165. /* shutter time 0x0000-0x03FF */
  166. /* low value give good picures on moving objects (but requires much light) */
  167. /* high value gives good picures in darkness (but tends to be overexposed) */
  168. { 0x143e, 0x01 },
  169. { 0x143d, 0x00 },
  170. { 0x1442, 0xe2 },
  171. /* write: 1x1x xxxx */
  172. /* read: 1x1x xxxx */
  173. /* bit 5 == button pressed and hold if 0 */
  174. /* write 0xe2,0xea */
  175. /* 0x144a */
  176. /* 0x00 init */
  177. /* bit 7 == button has been pressed, but not handled */
  178. /* interrupt */
  179. /* if(urb->iso_frame_desc[i].status == 0x80) { */
  180. /* if(urb->iso_frame_desc[i].status == 0x88) { */
  181. { 0x1500, 0xd0 },
  182. { 0x1500, 0xd0 },
  183. { 0x1500, 0x50 }, /* 0x00 - 0xFF 0x80 == compr ? */
  184. { 0x1501, 0xaf },
  185. /* high val-> light area gets darker */
  186. /* low val -> light area gets lighter */
  187. { 0x1502, 0xc2 },
  188. /* high val-> light area gets darker */
  189. /* low val -> light area gets lighter */
  190. { 0x1503, 0x45 },
  191. /* high val-> light area gets darker */
  192. /* low val -> light area gets lighter */
  193. { 0x1505, 0x02 },
  194. /* 2 : 324x248 80352 bytes */
  195. /* 7 : 248x162 40176 bytes */
  196. /* c+f: 162*124 20088 bytes */
  197. { 0x150e, 0x8e },
  198. { 0x150f, 0x37 },
  199. { 0x15c0, 0x00 },
  200. { 0x15c3, 0x08 }, /* 0x04/0x14 ... test pictures ??? */
  201. { 0x143f, 0x01 }, /* commit settings */
  202. };
  203. for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
  204. err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
  205. st6422_bridge_init[i][1]);
  206. }
  207. return err;
  208. }
  209. static void st6422_disconnect(struct sd *sd)
  210. {
  211. sd->sensor = NULL;
  212. kfree(sd->sensor_priv);
  213. }
  214. static int setbrightness(struct sd *sd)
  215. {
  216. struct st6422_settings *sensor_settings = sd->sensor_priv;
  217. /* val goes from 0 -> 31 */
  218. return stv06xx_write_bridge(sd, 0x1432,
  219. sensor_settings->ctrls[BRIGHTNESS].val);
  220. }
  221. static int setcontrast(struct sd *sd)
  222. {
  223. struct st6422_settings *sensor_settings = sd->sensor_priv;
  224. /* Val goes from 0 -> 15 */
  225. return stv06xx_write_bridge(sd, 0x143a,
  226. sensor_settings->ctrls[CONTRAST].val | 0xf0);
  227. }
  228. static int setgain(struct sd *sd)
  229. {
  230. struct st6422_settings *sensor_settings = sd->sensor_priv;
  231. u8 gain;
  232. int err;
  233. gain = sensor_settings->ctrls[GAIN].val;
  234. /* Set red, green, blue, gain */
  235. err = stv06xx_write_bridge(sd, 0x0509, gain);
  236. if (err < 0)
  237. return err;
  238. err = stv06xx_write_bridge(sd, 0x050a, gain);
  239. if (err < 0)
  240. return err;
  241. err = stv06xx_write_bridge(sd, 0x050b, gain);
  242. if (err < 0)
  243. return err;
  244. /* 2 mystery writes */
  245. err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
  246. if (err < 0)
  247. return err;
  248. return stv06xx_write_bridge(sd, 0x050d, 0x01);
  249. }
  250. static int setexposure(struct sd *sd)
  251. {
  252. struct st6422_settings *sensor_settings = sd->sensor_priv;
  253. u16 expo;
  254. int err;
  255. expo = sensor_settings->ctrls[EXPOSURE].val;
  256. err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
  257. if (err < 0)
  258. return err;
  259. return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
  260. }
  261. static int st6422_start(struct sd *sd)
  262. {
  263. int err;
  264. struct cam *cam = &sd->gspca_dev.cam;
  265. if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
  266. err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
  267. else
  268. err = stv06xx_write_bridge(sd, 0x1505, 0x02);
  269. if (err < 0)
  270. return err;
  271. err = setbrightness(sd);
  272. if (err < 0)
  273. return err;
  274. err = setcontrast(sd);
  275. if (err < 0)
  276. return err;
  277. err = setexposure(sd);
  278. if (err < 0)
  279. return err;
  280. err = setgain(sd);
  281. if (err < 0)
  282. return err;
  283. /* commit settings */
  284. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  285. return (err < 0) ? err : 0;
  286. }
  287. static int st6422_stop(struct sd *sd)
  288. {
  289. PDEBUG(D_STREAM, "Halting stream");
  290. return 0;
  291. }
  292. static void st6422_set_brightness(struct gspca_dev *gspca_dev)
  293. {
  294. int err;
  295. struct sd *sd = (struct sd *) gspca_dev;
  296. err = setbrightness(sd);
  297. /* commit settings */
  298. if (err >= 0)
  299. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  300. gspca_dev->usb_err = err;
  301. }
  302. static void st6422_set_contrast(struct gspca_dev *gspca_dev)
  303. {
  304. int err;
  305. struct sd *sd = (struct sd *) gspca_dev;
  306. err = setcontrast(sd);
  307. /* commit settings */
  308. if (err >= 0)
  309. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  310. gspca_dev->usb_err = err;
  311. }
  312. static void st6422_set_gain(struct gspca_dev *gspca_dev)
  313. {
  314. int err;
  315. struct sd *sd = (struct sd *) gspca_dev;
  316. err = setgain(sd);
  317. /* commit settings */
  318. if (err >= 0)
  319. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  320. gspca_dev->usb_err = err;
  321. }
  322. static void st6422_set_exposure(struct gspca_dev *gspca_dev)
  323. {
  324. int err;
  325. struct sd *sd = (struct sd *) gspca_dev;
  326. err = setexposure(sd);
  327. /* commit settings */
  328. if (err >= 0)
  329. err = stv06xx_write_bridge(sd, 0x143f, 0x01);
  330. gspca_dev->usb_err = err;
  331. }