m5602_s5k83a.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Driver for the s5k83a sensor
  3. *
  4. * Copyright (C) 2008 Erik Andrén
  5. * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kthread.h>
  20. #include "m5602_s5k83a.h"
  21. static int s5k83a_s_ctrl(struct v4l2_ctrl *ctrl);
  22. static const struct v4l2_ctrl_ops s5k83a_ctrl_ops = {
  23. .s_ctrl = s5k83a_s_ctrl,
  24. };
  25. static struct v4l2_pix_format s5k83a_modes[] = {
  26. {
  27. 640,
  28. 480,
  29. V4L2_PIX_FMT_SBGGR8,
  30. V4L2_FIELD_NONE,
  31. .sizeimage =
  32. 640 * 480,
  33. .bytesperline = 640,
  34. .colorspace = V4L2_COLORSPACE_SRGB,
  35. .priv = 0
  36. }
  37. };
  38. static void s5k83a_dump_registers(struct sd *sd);
  39. static int s5k83a_get_rotation(struct sd *sd, u8 *reg_data);
  40. static int s5k83a_set_led_indication(struct sd *sd, u8 val);
  41. static int s5k83a_set_flip_real(struct gspca_dev *gspca_dev,
  42. __s32 vflip, __s32 hflip);
  43. int s5k83a_probe(struct sd *sd)
  44. {
  45. u8 prod_id = 0, ver_id = 0;
  46. int i, err = 0;
  47. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  48. if (force_sensor) {
  49. if (force_sensor == S5K83A_SENSOR) {
  50. pr_info("Forcing a %s sensor\n", s5k83a.name);
  51. goto sensor_found;
  52. }
  53. /* If we want to force another sensor, don't try to probe this
  54. * one */
  55. return -ENODEV;
  56. }
  57. PDEBUG(D_PROBE, "Probing for a s5k83a sensor");
  58. /* Preinit the sensor */
  59. for (i = 0; i < ARRAY_SIZE(preinit_s5k83a) && !err; i++) {
  60. u8 data[2] = {preinit_s5k83a[i][2], preinit_s5k83a[i][3]};
  61. if (preinit_s5k83a[i][0] == SENSOR)
  62. err = m5602_write_sensor(sd, preinit_s5k83a[i][1],
  63. data, 2);
  64. else
  65. err = m5602_write_bridge(sd, preinit_s5k83a[i][1],
  66. data[0]);
  67. }
  68. /* We don't know what register (if any) that contain the product id
  69. * Just pick the first addresses that seem to produce the same results
  70. * on multiple machines */
  71. if (m5602_read_sensor(sd, 0x00, &prod_id, 1))
  72. return -ENODEV;
  73. if (m5602_read_sensor(sd, 0x01, &ver_id, 1))
  74. return -ENODEV;
  75. if ((prod_id == 0xff) || (ver_id == 0xff))
  76. return -ENODEV;
  77. else
  78. pr_info("Detected a s5k83a sensor\n");
  79. sensor_found:
  80. sd->gspca_dev.cam.cam_mode = s5k83a_modes;
  81. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(s5k83a_modes);
  82. /* null the pointer! thread is't running now */
  83. sd->rotation_thread = NULL;
  84. return 0;
  85. }
  86. int s5k83a_init(struct sd *sd)
  87. {
  88. int i, err = 0;
  89. for (i = 0; i < ARRAY_SIZE(init_s5k83a) && !err; i++) {
  90. u8 data[2] = {0x00, 0x00};
  91. switch (init_s5k83a[i][0]) {
  92. case BRIDGE:
  93. err = m5602_write_bridge(sd,
  94. init_s5k83a[i][1],
  95. init_s5k83a[i][2]);
  96. break;
  97. case SENSOR:
  98. data[0] = init_s5k83a[i][2];
  99. err = m5602_write_sensor(sd,
  100. init_s5k83a[i][1], data, 1);
  101. break;
  102. case SENSOR_LONG:
  103. data[0] = init_s5k83a[i][2];
  104. data[1] = init_s5k83a[i][3];
  105. err = m5602_write_sensor(sd,
  106. init_s5k83a[i][1], data, 2);
  107. break;
  108. default:
  109. pr_info("Invalid stream command, exiting init\n");
  110. return -EINVAL;
  111. }
  112. }
  113. if (dump_sensor)
  114. s5k83a_dump_registers(sd);
  115. return err;
  116. }
  117. int s5k83a_init_controls(struct sd *sd)
  118. {
  119. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  120. sd->gspca_dev.vdev.ctrl_handler = hdl;
  121. v4l2_ctrl_handler_init(hdl, 6);
  122. v4l2_ctrl_new_std(hdl, &s5k83a_ctrl_ops, V4L2_CID_BRIGHTNESS,
  123. 0, 255, 1, S5K83A_DEFAULT_BRIGHTNESS);
  124. v4l2_ctrl_new_std(hdl, &s5k83a_ctrl_ops, V4L2_CID_EXPOSURE,
  125. 0, S5K83A_MAXIMUM_EXPOSURE, 1,
  126. S5K83A_DEFAULT_EXPOSURE);
  127. v4l2_ctrl_new_std(hdl, &s5k83a_ctrl_ops, V4L2_CID_GAIN,
  128. 0, 255, 1, S5K83A_DEFAULT_GAIN);
  129. sd->hflip = v4l2_ctrl_new_std(hdl, &s5k83a_ctrl_ops, V4L2_CID_HFLIP,
  130. 0, 1, 1, 0);
  131. sd->vflip = v4l2_ctrl_new_std(hdl, &s5k83a_ctrl_ops, V4L2_CID_VFLIP,
  132. 0, 1, 1, 0);
  133. if (hdl->error) {
  134. pr_err("Could not initialize controls\n");
  135. return hdl->error;
  136. }
  137. v4l2_ctrl_cluster(2, &sd->hflip);
  138. return 0;
  139. }
  140. static int rotation_thread_function(void *data)
  141. {
  142. struct sd *sd = (struct sd *) data;
  143. u8 reg, previous_rotation = 0;
  144. __s32 vflip, hflip;
  145. set_current_state(TASK_INTERRUPTIBLE);
  146. while (!schedule_timeout(msecs_to_jiffies(100))) {
  147. if (mutex_lock_interruptible(&sd->gspca_dev.usb_lock))
  148. break;
  149. s5k83a_get_rotation(sd, &reg);
  150. if (previous_rotation != reg) {
  151. previous_rotation = reg;
  152. pr_info("Camera was flipped\n");
  153. hflip = sd->hflip->val;
  154. vflip = sd->vflip->val;
  155. if (reg) {
  156. vflip = !vflip;
  157. hflip = !hflip;
  158. }
  159. s5k83a_set_flip_real((struct gspca_dev *) sd,
  160. vflip, hflip);
  161. }
  162. mutex_unlock(&sd->gspca_dev.usb_lock);
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. }
  165. /* return to "front" flip */
  166. if (previous_rotation) {
  167. hflip = sd->hflip->val;
  168. vflip = sd->vflip->val;
  169. s5k83a_set_flip_real((struct gspca_dev *) sd, vflip, hflip);
  170. }
  171. sd->rotation_thread = NULL;
  172. return 0;
  173. }
  174. int s5k83a_start(struct sd *sd)
  175. {
  176. int i, err = 0;
  177. /* Create another thread, polling the GPIO ports of the camera to check
  178. if it got rotated. This is how the windows driver does it so we have
  179. to assume that there is no better way of accomplishing this */
  180. sd->rotation_thread = kthread_create(rotation_thread_function,
  181. sd, "rotation thread");
  182. wake_up_process(sd->rotation_thread);
  183. /* Preinit the sensor */
  184. for (i = 0; i < ARRAY_SIZE(start_s5k83a) && !err; i++) {
  185. u8 data[2] = {start_s5k83a[i][2], start_s5k83a[i][3]};
  186. if (start_s5k83a[i][0] == SENSOR)
  187. err = m5602_write_sensor(sd, start_s5k83a[i][1],
  188. data, 2);
  189. else
  190. err = m5602_write_bridge(sd, start_s5k83a[i][1],
  191. data[0]);
  192. }
  193. if (err < 0)
  194. return err;
  195. return s5k83a_set_led_indication(sd, 1);
  196. }
  197. int s5k83a_stop(struct sd *sd)
  198. {
  199. if (sd->rotation_thread)
  200. kthread_stop(sd->rotation_thread);
  201. return s5k83a_set_led_indication(sd, 0);
  202. }
  203. void s5k83a_disconnect(struct sd *sd)
  204. {
  205. s5k83a_stop(sd);
  206. sd->sensor = NULL;
  207. }
  208. static int s5k83a_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  209. {
  210. int err;
  211. u8 data[2];
  212. struct sd *sd = (struct sd *) gspca_dev;
  213. data[0] = 0x00;
  214. data[1] = 0x20;
  215. err = m5602_write_sensor(sd, 0x14, data, 2);
  216. if (err < 0)
  217. return err;
  218. data[0] = 0x01;
  219. data[1] = 0x00;
  220. err = m5602_write_sensor(sd, 0x0d, data, 2);
  221. if (err < 0)
  222. return err;
  223. /* FIXME: This is not sane, we need to figure out the composition
  224. of these registers */
  225. data[0] = val >> 3; /* gain, high 5 bits */
  226. data[1] = val >> 1; /* gain, high 7 bits */
  227. err = m5602_write_sensor(sd, S5K83A_GAIN, data, 2);
  228. return err;
  229. }
  230. static int s5k83a_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  231. {
  232. int err;
  233. u8 data[1];
  234. struct sd *sd = (struct sd *) gspca_dev;
  235. data[0] = val;
  236. err = m5602_write_sensor(sd, S5K83A_BRIGHTNESS, data, 1);
  237. return err;
  238. }
  239. static int s5k83a_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  240. {
  241. int err;
  242. u8 data[2];
  243. struct sd *sd = (struct sd *) gspca_dev;
  244. data[0] = 0;
  245. data[1] = val;
  246. err = m5602_write_sensor(sd, S5K83A_EXPOSURE, data, 2);
  247. return err;
  248. }
  249. static int s5k83a_set_flip_real(struct gspca_dev *gspca_dev,
  250. __s32 vflip, __s32 hflip)
  251. {
  252. int err;
  253. u8 data[1];
  254. struct sd *sd = (struct sd *) gspca_dev;
  255. data[0] = 0x05;
  256. err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
  257. if (err < 0)
  258. return err;
  259. /* six bit is vflip, seven is hflip */
  260. data[0] = S5K83A_FLIP_MASK;
  261. data[0] = (vflip) ? data[0] | 0x40 : data[0];
  262. data[0] = (hflip) ? data[0] | 0x80 : data[0];
  263. err = m5602_write_sensor(sd, S5K83A_FLIP, data, 1);
  264. if (err < 0)
  265. return err;
  266. data[0] = (vflip) ? 0x0b : 0x0a;
  267. err = m5602_write_sensor(sd, S5K83A_VFLIP_TUNE, data, 1);
  268. if (err < 0)
  269. return err;
  270. data[0] = (hflip) ? 0x0a : 0x0b;
  271. err = m5602_write_sensor(sd, S5K83A_HFLIP_TUNE, data, 1);
  272. return err;
  273. }
  274. static int s5k83a_set_hvflip(struct gspca_dev *gspca_dev)
  275. {
  276. int err;
  277. u8 reg;
  278. struct sd *sd = (struct sd *) gspca_dev;
  279. int hflip = sd->hflip->val;
  280. int vflip = sd->vflip->val;
  281. err = s5k83a_get_rotation(sd, &reg);
  282. if (err < 0)
  283. return err;
  284. if (reg) {
  285. hflip = !hflip;
  286. vflip = !vflip;
  287. }
  288. err = s5k83a_set_flip_real(gspca_dev, vflip, hflip);
  289. return err;
  290. }
  291. static int s5k83a_s_ctrl(struct v4l2_ctrl *ctrl)
  292. {
  293. struct gspca_dev *gspca_dev =
  294. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  295. int err;
  296. if (!gspca_dev->streaming)
  297. return 0;
  298. switch (ctrl->id) {
  299. case V4L2_CID_BRIGHTNESS:
  300. err = s5k83a_set_brightness(gspca_dev, ctrl->val);
  301. break;
  302. case V4L2_CID_EXPOSURE:
  303. err = s5k83a_set_exposure(gspca_dev, ctrl->val);
  304. break;
  305. case V4L2_CID_GAIN:
  306. err = s5k83a_set_gain(gspca_dev, ctrl->val);
  307. break;
  308. case V4L2_CID_HFLIP:
  309. err = s5k83a_set_hvflip(gspca_dev);
  310. break;
  311. default:
  312. return -EINVAL;
  313. }
  314. return err;
  315. }
  316. static int s5k83a_set_led_indication(struct sd *sd, u8 val)
  317. {
  318. int err = 0;
  319. u8 data[1];
  320. err = m5602_read_bridge(sd, M5602_XB_GPIO_DAT, data);
  321. if (err < 0)
  322. return err;
  323. if (val)
  324. data[0] = data[0] | S5K83A_GPIO_LED_MASK;
  325. else
  326. data[0] = data[0] & ~S5K83A_GPIO_LED_MASK;
  327. err = m5602_write_bridge(sd, M5602_XB_GPIO_DAT, data[0]);
  328. return err;
  329. }
  330. /* Get camera rotation on Acer notebooks */
  331. static int s5k83a_get_rotation(struct sd *sd, u8 *reg_data)
  332. {
  333. int err = m5602_read_bridge(sd, M5602_XB_GPIO_DAT, reg_data);
  334. *reg_data = (*reg_data & S5K83A_GPIO_ROTATION_MASK) ? 0 : 1;
  335. return err;
  336. }
  337. static void s5k83a_dump_registers(struct sd *sd)
  338. {
  339. int address;
  340. u8 page, old_page;
  341. m5602_read_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
  342. for (page = 0; page < 16; page++) {
  343. m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
  344. pr_info("Dumping the s5k83a register state for page 0x%x\n",
  345. page);
  346. for (address = 0; address <= 0xff; address++) {
  347. u8 val = 0;
  348. m5602_read_sensor(sd, address, &val, 1);
  349. pr_info("register 0x%x contains 0x%x\n", address, val);
  350. }
  351. }
  352. pr_info("s5k83a register state dump complete\n");
  353. for (page = 0; page < 16; page++) {
  354. m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
  355. pr_info("Probing for which registers that are read/write for page 0x%x\n",
  356. page);
  357. for (address = 0; address <= 0xff; address++) {
  358. u8 old_val, ctrl_val, test_val = 0xff;
  359. m5602_read_sensor(sd, address, &old_val, 1);
  360. m5602_write_sensor(sd, address, &test_val, 1);
  361. m5602_read_sensor(sd, address, &ctrl_val, 1);
  362. if (ctrl_val == test_val)
  363. pr_info("register 0x%x is writeable\n",
  364. address);
  365. else
  366. pr_info("register 0x%x is read only\n",
  367. address);
  368. /* Restore original val */
  369. m5602_write_sensor(sd, address, &old_val, 1);
  370. }
  371. }
  372. pr_info("Read/write register probing complete\n");
  373. m5602_write_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
  374. }