m5602_s5k4aa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Driver for the s5k4aa 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 "m5602_s5k4aa.h"
  20. static int s5k4aa_s_ctrl(struct v4l2_ctrl *ctrl);
  21. static void s5k4aa_dump_registers(struct sd *sd);
  22. static const struct v4l2_ctrl_ops s5k4aa_ctrl_ops = {
  23. .s_ctrl = s5k4aa_s_ctrl,
  24. };
  25. static
  26. const
  27. struct dmi_system_id s5k4aa_vflip_dmi_table[] = {
  28. {
  29. .ident = "BRUNEINIT",
  30. .matches = {
  31. DMI_MATCH(DMI_SYS_VENDOR, "BRUNENIT"),
  32. DMI_MATCH(DMI_PRODUCT_NAME, "BRUNENIT"),
  33. DMI_MATCH(DMI_BOARD_VERSION, "00030D0000000001")
  34. }
  35. }, {
  36. .ident = "Fujitsu-Siemens Amilo Xa 2528",
  37. .matches = {
  38. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  39. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xa 2528")
  40. }
  41. }, {
  42. .ident = "Fujitsu-Siemens Amilo Xi 2428",
  43. .matches = {
  44. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  45. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 2428")
  46. }
  47. }, {
  48. .ident = "Fujitsu-Siemens Amilo Xi 2528",
  49. .matches = {
  50. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  51. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 2528")
  52. }
  53. }, {
  54. .ident = "Fujitsu-Siemens Amilo Xi 2550",
  55. .matches = {
  56. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  57. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Xi 2550")
  58. }
  59. }, {
  60. .ident = "Fujitsu-Siemens Amilo Pa 2548",
  61. .matches = {
  62. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  63. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pa 2548")
  64. }
  65. }, {
  66. .ident = "Fujitsu-Siemens Amilo Pi 2530",
  67. .matches = {
  68. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  69. DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pi 2530")
  70. }
  71. }, {
  72. .ident = "MSI GX700",
  73. .matches = {
  74. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  75. DMI_MATCH(DMI_PRODUCT_NAME, "GX700"),
  76. DMI_MATCH(DMI_BIOS_DATE, "12/02/2008")
  77. }
  78. }, {
  79. .ident = "MSI GX700",
  80. .matches = {
  81. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  82. DMI_MATCH(DMI_PRODUCT_NAME, "GX700"),
  83. DMI_MATCH(DMI_BIOS_DATE, "07/26/2007")
  84. }
  85. }, {
  86. .ident = "MSI GX700",
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  89. DMI_MATCH(DMI_PRODUCT_NAME, "GX700"),
  90. DMI_MATCH(DMI_BIOS_DATE, "07/19/2007")
  91. }
  92. }, {
  93. .ident = "MSI GX700/GX705/EX700",
  94. .matches = {
  95. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  96. DMI_MATCH(DMI_PRODUCT_NAME, "GX700/GX705/EX700")
  97. }
  98. }, {
  99. .ident = "MSI L735",
  100. .matches = {
  101. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  102. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1717X")
  103. }
  104. }, {
  105. .ident = "Lenovo Y300",
  106. .matches = {
  107. DMI_MATCH(DMI_SYS_VENDOR, "L3000 Y300"),
  108. DMI_MATCH(DMI_PRODUCT_NAME, "Y300")
  109. }
  110. },
  111. { }
  112. };
  113. static struct v4l2_pix_format s5k4aa_modes[] = {
  114. {
  115. 640,
  116. 480,
  117. V4L2_PIX_FMT_SBGGR8,
  118. V4L2_FIELD_NONE,
  119. .sizeimage =
  120. 640 * 480,
  121. .bytesperline = 640,
  122. .colorspace = V4L2_COLORSPACE_SRGB,
  123. .priv = 0
  124. },
  125. {
  126. 1280,
  127. 1024,
  128. V4L2_PIX_FMT_SBGGR8,
  129. V4L2_FIELD_NONE,
  130. .sizeimage =
  131. 1280 * 1024,
  132. .bytesperline = 1280,
  133. .colorspace = V4L2_COLORSPACE_SRGB,
  134. .priv = 0
  135. }
  136. };
  137. int s5k4aa_probe(struct sd *sd)
  138. {
  139. u8 prod_id[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  140. const u8 expected_prod_id[6] = {0x00, 0x10, 0x00, 0x4b, 0x33, 0x75};
  141. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  142. int i, err = 0;
  143. if (force_sensor) {
  144. if (force_sensor == S5K4AA_SENSOR) {
  145. pr_info("Forcing a %s sensor\n", s5k4aa.name);
  146. goto sensor_found;
  147. }
  148. /* If we want to force another sensor, don't try to probe this
  149. * one */
  150. return -ENODEV;
  151. }
  152. PDEBUG(D_PROBE, "Probing for a s5k4aa sensor");
  153. /* Preinit the sensor */
  154. for (i = 0; i < ARRAY_SIZE(preinit_s5k4aa) && !err; i++) {
  155. u8 data[2] = {0x00, 0x00};
  156. switch (preinit_s5k4aa[i][0]) {
  157. case BRIDGE:
  158. err = m5602_write_bridge(sd,
  159. preinit_s5k4aa[i][1],
  160. preinit_s5k4aa[i][2]);
  161. break;
  162. case SENSOR:
  163. data[0] = preinit_s5k4aa[i][2];
  164. err = m5602_write_sensor(sd,
  165. preinit_s5k4aa[i][1],
  166. data, 1);
  167. break;
  168. case SENSOR_LONG:
  169. data[0] = preinit_s5k4aa[i][2];
  170. data[1] = preinit_s5k4aa[i][3];
  171. err = m5602_write_sensor(sd,
  172. preinit_s5k4aa[i][1],
  173. data, 2);
  174. break;
  175. default:
  176. pr_info("Invalid stream command, exiting init\n");
  177. return -EINVAL;
  178. }
  179. }
  180. /* Test some registers, but we don't know their exact meaning yet */
  181. if (m5602_read_sensor(sd, 0x00, prod_id, 2))
  182. return -ENODEV;
  183. if (m5602_read_sensor(sd, 0x02, prod_id+2, 2))
  184. return -ENODEV;
  185. if (m5602_read_sensor(sd, 0x04, prod_id+4, 2))
  186. return -ENODEV;
  187. if (memcmp(prod_id, expected_prod_id, sizeof(prod_id)))
  188. return -ENODEV;
  189. else
  190. pr_info("Detected a s5k4aa sensor\n");
  191. sensor_found:
  192. sd->gspca_dev.cam.cam_mode = s5k4aa_modes;
  193. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(s5k4aa_modes);
  194. return 0;
  195. }
  196. int s5k4aa_start(struct sd *sd)
  197. {
  198. int i, err = 0;
  199. u8 data[2];
  200. struct cam *cam = &sd->gspca_dev.cam;
  201. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  202. switch (cam->cam_mode[sd->gspca_dev.curr_mode].width) {
  203. case 1280:
  204. PDEBUG(D_CONF, "Configuring camera for SXGA mode");
  205. for (i = 0; i < ARRAY_SIZE(SXGA_s5k4aa); i++) {
  206. switch (SXGA_s5k4aa[i][0]) {
  207. case BRIDGE:
  208. err = m5602_write_bridge(sd,
  209. SXGA_s5k4aa[i][1],
  210. SXGA_s5k4aa[i][2]);
  211. break;
  212. case SENSOR:
  213. data[0] = SXGA_s5k4aa[i][2];
  214. err = m5602_write_sensor(sd,
  215. SXGA_s5k4aa[i][1],
  216. data, 1);
  217. break;
  218. case SENSOR_LONG:
  219. data[0] = SXGA_s5k4aa[i][2];
  220. data[1] = SXGA_s5k4aa[i][3];
  221. err = m5602_write_sensor(sd,
  222. SXGA_s5k4aa[i][1],
  223. data, 2);
  224. break;
  225. default:
  226. pr_err("Invalid stream command, exiting init\n");
  227. return -EINVAL;
  228. }
  229. }
  230. break;
  231. case 640:
  232. PDEBUG(D_CONF, "Configuring camera for VGA mode");
  233. for (i = 0; i < ARRAY_SIZE(VGA_s5k4aa); i++) {
  234. switch (VGA_s5k4aa[i][0]) {
  235. case BRIDGE:
  236. err = m5602_write_bridge(sd,
  237. VGA_s5k4aa[i][1],
  238. VGA_s5k4aa[i][2]);
  239. break;
  240. case SENSOR:
  241. data[0] = VGA_s5k4aa[i][2];
  242. err = m5602_write_sensor(sd,
  243. VGA_s5k4aa[i][1],
  244. data, 1);
  245. break;
  246. case SENSOR_LONG:
  247. data[0] = VGA_s5k4aa[i][2];
  248. data[1] = VGA_s5k4aa[i][3];
  249. err = m5602_write_sensor(sd,
  250. VGA_s5k4aa[i][1],
  251. data, 2);
  252. break;
  253. default:
  254. pr_err("Invalid stream command, exiting init\n");
  255. return -EINVAL;
  256. }
  257. }
  258. break;
  259. }
  260. if (err < 0)
  261. return err;
  262. return 0;
  263. }
  264. int s5k4aa_init(struct sd *sd)
  265. {
  266. int i, err = 0;
  267. for (i = 0; i < ARRAY_SIZE(init_s5k4aa) && !err; i++) {
  268. u8 data[2] = {0x00, 0x00};
  269. switch (init_s5k4aa[i][0]) {
  270. case BRIDGE:
  271. err = m5602_write_bridge(sd,
  272. init_s5k4aa[i][1],
  273. init_s5k4aa[i][2]);
  274. break;
  275. case SENSOR:
  276. data[0] = init_s5k4aa[i][2];
  277. err = m5602_write_sensor(sd,
  278. init_s5k4aa[i][1], data, 1);
  279. break;
  280. case SENSOR_LONG:
  281. data[0] = init_s5k4aa[i][2];
  282. data[1] = init_s5k4aa[i][3];
  283. err = m5602_write_sensor(sd,
  284. init_s5k4aa[i][1], data, 2);
  285. break;
  286. default:
  287. pr_info("Invalid stream command, exiting init\n");
  288. return -EINVAL;
  289. }
  290. }
  291. if (dump_sensor)
  292. s5k4aa_dump_registers(sd);
  293. return err;
  294. }
  295. int s5k4aa_init_controls(struct sd *sd)
  296. {
  297. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  298. sd->gspca_dev.vdev.ctrl_handler = hdl;
  299. v4l2_ctrl_handler_init(hdl, 6);
  300. v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_BRIGHTNESS,
  301. 0, 0x1f, 1, S5K4AA_DEFAULT_BRIGHTNESS);
  302. v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_EXPOSURE,
  303. 13, 0xfff, 1, 0x100);
  304. v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_GAIN,
  305. 0, 127, 1, S5K4AA_DEFAULT_GAIN);
  306. v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_SHARPNESS,
  307. 0, 1, 1, 1);
  308. sd->hflip = v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_HFLIP,
  309. 0, 1, 1, 0);
  310. sd->vflip = v4l2_ctrl_new_std(hdl, &s5k4aa_ctrl_ops, V4L2_CID_VFLIP,
  311. 0, 1, 1, 0);
  312. if (hdl->error) {
  313. pr_err("Could not initialize controls\n");
  314. return hdl->error;
  315. }
  316. v4l2_ctrl_cluster(2, &sd->hflip);
  317. return 0;
  318. }
  319. static int s5k4aa_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  320. {
  321. struct sd *sd = (struct sd *) gspca_dev;
  322. u8 data = S5K4AA_PAGE_MAP_2;
  323. int err;
  324. PDEBUG(D_CONF, "Set exposure to %d", val);
  325. err = m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
  326. if (err < 0)
  327. return err;
  328. data = (val >> 8) & 0xff;
  329. err = m5602_write_sensor(sd, S5K4AA_EXPOSURE_HI, &data, 1);
  330. if (err < 0)
  331. return err;
  332. data = val & 0xff;
  333. err = m5602_write_sensor(sd, S5K4AA_EXPOSURE_LO, &data, 1);
  334. return err;
  335. }
  336. static int s5k4aa_set_hvflip(struct gspca_dev *gspca_dev)
  337. {
  338. struct sd *sd = (struct sd *) gspca_dev;
  339. u8 data = S5K4AA_PAGE_MAP_2;
  340. int err;
  341. int hflip = sd->hflip->val;
  342. int vflip = sd->vflip->val;
  343. PDEBUG(D_CONF, "Set hvflip %d %d", hflip, vflip);
  344. err = m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
  345. if (err < 0)
  346. return err;
  347. err = m5602_read_sensor(sd, S5K4AA_READ_MODE, &data, 1);
  348. if (err < 0)
  349. return err;
  350. if (dmi_check_system(s5k4aa_vflip_dmi_table)) {
  351. hflip = !hflip;
  352. vflip = !vflip;
  353. }
  354. data = (data & 0x7f) | (vflip << 7) | (hflip << 6);
  355. err = m5602_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
  356. if (err < 0)
  357. return err;
  358. err = m5602_read_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
  359. if (err < 0)
  360. return err;
  361. if (hflip)
  362. data &= 0xfe;
  363. else
  364. data |= 0x01;
  365. err = m5602_write_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
  366. if (err < 0)
  367. return err;
  368. err = m5602_read_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
  369. if (err < 0)
  370. return err;
  371. if (vflip)
  372. data &= 0xfe;
  373. else
  374. data |= 0x01;
  375. err = m5602_write_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
  376. if (err < 0)
  377. return err;
  378. return 0;
  379. }
  380. static int s5k4aa_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  381. {
  382. struct sd *sd = (struct sd *) gspca_dev;
  383. u8 data = S5K4AA_PAGE_MAP_2;
  384. int err;
  385. PDEBUG(D_CONF, "Set gain to %d", val);
  386. err = m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
  387. if (err < 0)
  388. return err;
  389. data = val & 0xff;
  390. err = m5602_write_sensor(sd, S5K4AA_GAIN, &data, 1);
  391. return err;
  392. }
  393. static int s5k4aa_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  394. {
  395. struct sd *sd = (struct sd *) gspca_dev;
  396. u8 data = S5K4AA_PAGE_MAP_2;
  397. int err;
  398. PDEBUG(D_CONF, "Set brightness to %d", val);
  399. err = m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
  400. if (err < 0)
  401. return err;
  402. data = val & 0xff;
  403. return m5602_write_sensor(sd, S5K4AA_BRIGHTNESS, &data, 1);
  404. }
  405. static int s5k4aa_set_noise(struct gspca_dev *gspca_dev, __s32 val)
  406. {
  407. struct sd *sd = (struct sd *) gspca_dev;
  408. u8 data = S5K4AA_PAGE_MAP_2;
  409. int err;
  410. PDEBUG(D_CONF, "Set noise to %d", val);
  411. err = m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
  412. if (err < 0)
  413. return err;
  414. data = val & 0x01;
  415. return m5602_write_sensor(sd, S5K4AA_NOISE_SUPP, &data, 1);
  416. }
  417. static int s5k4aa_s_ctrl(struct v4l2_ctrl *ctrl)
  418. {
  419. struct gspca_dev *gspca_dev =
  420. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  421. int err;
  422. if (!gspca_dev->streaming)
  423. return 0;
  424. switch (ctrl->id) {
  425. case V4L2_CID_BRIGHTNESS:
  426. err = s5k4aa_set_brightness(gspca_dev, ctrl->val);
  427. break;
  428. case V4L2_CID_EXPOSURE:
  429. err = s5k4aa_set_exposure(gspca_dev, ctrl->val);
  430. break;
  431. case V4L2_CID_GAIN:
  432. err = s5k4aa_set_gain(gspca_dev, ctrl->val);
  433. break;
  434. case V4L2_CID_SHARPNESS:
  435. err = s5k4aa_set_noise(gspca_dev, ctrl->val);
  436. break;
  437. case V4L2_CID_HFLIP:
  438. err = s5k4aa_set_hvflip(gspca_dev);
  439. break;
  440. default:
  441. return -EINVAL;
  442. }
  443. return err;
  444. }
  445. void s5k4aa_disconnect(struct sd *sd)
  446. {
  447. sd->sensor = NULL;
  448. }
  449. static void s5k4aa_dump_registers(struct sd *sd)
  450. {
  451. int address;
  452. u8 page, old_page;
  453. m5602_read_sensor(sd, S5K4AA_PAGE_MAP, &old_page, 1);
  454. for (page = 0; page < 16; page++) {
  455. m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &page, 1);
  456. pr_info("Dumping the s5k4aa register state for page 0x%x\n",
  457. page);
  458. for (address = 0; address <= 0xff; address++) {
  459. u8 value = 0;
  460. m5602_read_sensor(sd, address, &value, 1);
  461. pr_info("register 0x%x contains 0x%x\n",
  462. address, value);
  463. }
  464. }
  465. pr_info("s5k4aa register state dump complete\n");
  466. for (page = 0; page < 16; page++) {
  467. m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &page, 1);
  468. pr_info("Probing for which registers that are read/write for page 0x%x\n",
  469. page);
  470. for (address = 0; address <= 0xff; address++) {
  471. u8 old_value, ctrl_value, test_value = 0xff;
  472. m5602_read_sensor(sd, address, &old_value, 1);
  473. m5602_write_sensor(sd, address, &test_value, 1);
  474. m5602_read_sensor(sd, address, &ctrl_value, 1);
  475. if (ctrl_value == test_value)
  476. pr_info("register 0x%x is writeable\n",
  477. address);
  478. else
  479. pr_info("register 0x%x is read only\n",
  480. address);
  481. /* Restore original value */
  482. m5602_write_sensor(sd, address, &old_value, 1);
  483. }
  484. }
  485. pr_info("Read/write register probing complete\n");
  486. m5602_write_sensor(sd, S5K4AA_PAGE_MAP, &old_page, 1);
  487. }