m5602_po1030.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Driver for the po1030 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. #include "m5602_po1030.h"
  19. static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val);
  20. static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val);
  21. static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val);
  22. static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val);
  23. static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val);
  24. static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val);
  25. static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val);
  26. static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val);
  27. static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val);
  28. static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val);
  29. static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val);
  30. static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val);
  31. static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
  32. __s32 val);
  33. static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
  34. __s32 *val);
  35. static struct v4l2_pix_format po1030_modes[] = {
  36. {
  37. 640,
  38. 480,
  39. V4L2_PIX_FMT_SBGGR8,
  40. V4L2_FIELD_NONE,
  41. .sizeimage = 640 * 480,
  42. .bytesperline = 640,
  43. .colorspace = V4L2_COLORSPACE_SRGB,
  44. .priv = 0
  45. }
  46. };
  47. const static struct ctrl po1030_ctrls[] = {
  48. #define GAIN_IDX 0
  49. {
  50. {
  51. .id = V4L2_CID_GAIN,
  52. .type = V4L2_CTRL_TYPE_INTEGER,
  53. .name = "gain",
  54. .minimum = 0x00,
  55. .maximum = 0x4f,
  56. .step = 0x1,
  57. .default_value = PO1030_GLOBAL_GAIN_DEFAULT,
  58. .flags = V4L2_CTRL_FLAG_SLIDER
  59. },
  60. .set = po1030_set_gain,
  61. .get = po1030_get_gain
  62. },
  63. #define EXPOSURE_IDX 1
  64. {
  65. {
  66. .id = V4L2_CID_EXPOSURE,
  67. .type = V4L2_CTRL_TYPE_INTEGER,
  68. .name = "exposure",
  69. .minimum = 0x00,
  70. .maximum = 0x02ff,
  71. .step = 0x1,
  72. .default_value = PO1030_EXPOSURE_DEFAULT,
  73. .flags = V4L2_CTRL_FLAG_SLIDER
  74. },
  75. .set = po1030_set_exposure,
  76. .get = po1030_get_exposure
  77. },
  78. #define RED_BALANCE_IDX 2
  79. {
  80. {
  81. .id = V4L2_CID_RED_BALANCE,
  82. .type = V4L2_CTRL_TYPE_INTEGER,
  83. .name = "red balance",
  84. .minimum = 0x00,
  85. .maximum = 0xff,
  86. .step = 0x1,
  87. .default_value = PO1030_RED_GAIN_DEFAULT,
  88. .flags = V4L2_CTRL_FLAG_SLIDER
  89. },
  90. .set = po1030_set_red_balance,
  91. .get = po1030_get_red_balance
  92. },
  93. #define BLUE_BALANCE_IDX 3
  94. {
  95. {
  96. .id = V4L2_CID_BLUE_BALANCE,
  97. .type = V4L2_CTRL_TYPE_INTEGER,
  98. .name = "blue balance",
  99. .minimum = 0x00,
  100. .maximum = 0xff,
  101. .step = 0x1,
  102. .default_value = PO1030_BLUE_GAIN_DEFAULT,
  103. .flags = V4L2_CTRL_FLAG_SLIDER
  104. },
  105. .set = po1030_set_blue_balance,
  106. .get = po1030_get_blue_balance
  107. },
  108. #define HFLIP_IDX 4
  109. {
  110. {
  111. .id = V4L2_CID_HFLIP,
  112. .type = V4L2_CTRL_TYPE_BOOLEAN,
  113. .name = "horizontal flip",
  114. .minimum = 0,
  115. .maximum = 1,
  116. .step = 1,
  117. .default_value = 0,
  118. },
  119. .set = po1030_set_hflip,
  120. .get = po1030_get_hflip
  121. },
  122. #define VFLIP_IDX 5
  123. {
  124. {
  125. .id = V4L2_CID_VFLIP,
  126. .type = V4L2_CTRL_TYPE_BOOLEAN,
  127. .name = "vertical flip",
  128. .minimum = 0,
  129. .maximum = 1,
  130. .step = 1,
  131. .default_value = 0,
  132. },
  133. .set = po1030_set_vflip,
  134. .get = po1030_get_vflip
  135. },
  136. #define AUTO_WHITE_BALANCE_IDX 6
  137. {
  138. {
  139. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  140. .type = V4L2_CTRL_TYPE_BOOLEAN,
  141. .name = "auto white balance",
  142. .minimum = 0,
  143. .maximum = 1,
  144. .step = 1,
  145. .default_value = 0,
  146. },
  147. .set = po1030_set_auto_white_balance,
  148. .get = po1030_get_auto_white_balance
  149. }
  150. };
  151. static void po1030_dump_registers(struct sd *sd);
  152. int po1030_probe(struct sd *sd)
  153. {
  154. u8 dev_id_h = 0, i;
  155. s32 *sensor_settings;
  156. if (force_sensor) {
  157. if (force_sensor == PO1030_SENSOR) {
  158. info("Forcing a %s sensor", po1030.name);
  159. goto sensor_found;
  160. }
  161. /* If we want to force another sensor, don't try to probe this
  162. * one */
  163. return -ENODEV;
  164. }
  165. info("Probing for a po1030 sensor");
  166. /* Run the pre-init to actually probe the unit */
  167. for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
  168. u8 data = preinit_po1030[i][2];
  169. if (preinit_po1030[i][0] == SENSOR)
  170. m5602_write_sensor(sd,
  171. preinit_po1030[i][1], &data, 1);
  172. else
  173. m5602_write_bridge(sd, preinit_po1030[i][1], data);
  174. }
  175. if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
  176. return -ENODEV;
  177. if (dev_id_h == 0x30) {
  178. info("Detected a po1030 sensor");
  179. goto sensor_found;
  180. }
  181. return -ENODEV;
  182. sensor_found:
  183. sensor_settings = kmalloc(
  184. ARRAY_SIZE(po1030_ctrls) * sizeof(s32), GFP_KERNEL);
  185. if (!sensor_settings)
  186. return -ENOMEM;
  187. sd->gspca_dev.cam.cam_mode = po1030_modes;
  188. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
  189. sd->desc->ctrls = po1030_ctrls;
  190. sd->desc->nctrls = ARRAY_SIZE(po1030_ctrls);
  191. for (i = 0; i < ARRAY_SIZE(po1030_ctrls); i++)
  192. sensor_settings[i] = po1030_ctrls[i].qctrl.default_value;
  193. sd->sensor_priv = sensor_settings;
  194. return 0;
  195. }
  196. int po1030_init(struct sd *sd)
  197. {
  198. s32 *sensor_settings = sd->sensor_priv;
  199. int i, err = 0;
  200. /* Init the sensor */
  201. for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
  202. u8 data[2] = {0x00, 0x00};
  203. switch (init_po1030[i][0]) {
  204. case BRIDGE:
  205. err = m5602_write_bridge(sd,
  206. init_po1030[i][1],
  207. init_po1030[i][2]);
  208. break;
  209. case SENSOR:
  210. data[0] = init_po1030[i][2];
  211. err = m5602_write_sensor(sd,
  212. init_po1030[i][1], data, 1);
  213. break;
  214. default:
  215. info("Invalid stream command, exiting init");
  216. return -EINVAL;
  217. }
  218. }
  219. if (err < 0)
  220. return err;
  221. if (dump_sensor)
  222. po1030_dump_registers(sd);
  223. err = po1030_set_exposure(&sd->gspca_dev,
  224. sensor_settings[EXPOSURE_IDX]);
  225. if (err < 0)
  226. return err;
  227. err = po1030_set_gain(&sd->gspca_dev, sensor_settings[GAIN_IDX]);
  228. if (err < 0)
  229. return err;
  230. err = po1030_set_hflip(&sd->gspca_dev, sensor_settings[HFLIP_IDX]);
  231. if (err < 0)
  232. return err;
  233. err = po1030_set_vflip(&sd->gspca_dev, sensor_settings[VFLIP_IDX]);
  234. if (err < 0)
  235. return err;
  236. err = po1030_set_red_balance(&sd->gspca_dev,
  237. sensor_settings[RED_BALANCE_IDX]);
  238. if (err < 0)
  239. return err;
  240. err = po1030_set_blue_balance(&sd->gspca_dev,
  241. sensor_settings[BLUE_BALANCE_IDX]);
  242. if (err < 0)
  243. return err;
  244. err = po1030_set_auto_white_balance(&sd->gspca_dev,
  245. sensor_settings[AUTO_WHITE_BALANCE_IDX]);
  246. return err;
  247. }
  248. int po1030_start(struct sd *sd)
  249. {
  250. int i, err = 0;
  251. /* Synthesize the vsync/hsync setup */
  252. for (i = 0; i < ARRAY_SIZE(start_po1030) && !err; i++) {
  253. if (start_po1030[i][0] == BRIDGE)
  254. err = m5602_write_bridge(sd, start_po1030[i][1],
  255. start_po1030[i][2]);
  256. else if (start_po1030[i][0] == SENSOR) {
  257. u8 data = start_po1030[i][2];
  258. err = m5602_write_sensor(sd,
  259. start_po1030[i][1], &data, 1);
  260. }
  261. }
  262. return err;
  263. }
  264. static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  265. {
  266. struct sd *sd = (struct sd *) gspca_dev;
  267. s32 *sensor_settings = sd->sensor_priv;
  268. *val = sensor_settings[EXPOSURE_IDX];
  269. PDEBUG(D_V4L2, "Exposure read as %d", *val);
  270. return 0;
  271. }
  272. static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  273. {
  274. struct sd *sd = (struct sd *) gspca_dev;
  275. s32 *sensor_settings = sd->sensor_priv;
  276. u8 i2c_data;
  277. int err;
  278. sensor_settings[EXPOSURE_IDX] = val;
  279. PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
  280. i2c_data = ((val & 0xff00) >> 8);
  281. PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
  282. i2c_data);
  283. err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
  284. &i2c_data, 1);
  285. if (err < 0)
  286. return err;
  287. i2c_data = (val & 0xff);
  288. PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
  289. i2c_data);
  290. err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
  291. &i2c_data, 1);
  292. return err;
  293. }
  294. static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  295. {
  296. struct sd *sd = (struct sd *) gspca_dev;
  297. s32 *sensor_settings = sd->sensor_priv;
  298. *val = sensor_settings[GAIN_IDX];
  299. PDEBUG(D_V4L2, "Read global gain %d", *val);
  300. return 0;
  301. }
  302. static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  303. {
  304. struct sd *sd = (struct sd *) gspca_dev;
  305. s32 *sensor_settings = sd->sensor_priv;
  306. u8 i2c_data;
  307. int err;
  308. sensor_settings[GAIN_IDX] = val;
  309. i2c_data = val & 0xff;
  310. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  311. err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
  312. &i2c_data, 1);
  313. return err;
  314. }
  315. static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  316. {
  317. struct sd *sd = (struct sd *) gspca_dev;
  318. s32 *sensor_settings = sd->sensor_priv;
  319. *val = sensor_settings[HFLIP_IDX];
  320. PDEBUG(D_V4L2, "Read hflip %d", *val);
  321. return 0;
  322. }
  323. static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  324. {
  325. struct sd *sd = (struct sd *) gspca_dev;
  326. s32 *sensor_settings = sd->sensor_priv;
  327. u8 i2c_data;
  328. int err;
  329. sensor_settings[HFLIP_IDX] = val;
  330. PDEBUG(D_V4L2, "Set hflip %d", val);
  331. err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
  332. if (err < 0)
  333. return err;
  334. i2c_data = (0x7f & i2c_data) | ((val & 0x01) << 7);
  335. err = m5602_write_sensor(sd, PO1030_CONTROL2,
  336. &i2c_data, 1);
  337. return err;
  338. }
  339. static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  340. {
  341. struct sd *sd = (struct sd *) gspca_dev;
  342. s32 *sensor_settings = sd->sensor_priv;
  343. *val = sensor_settings[VFLIP_IDX];
  344. PDEBUG(D_V4L2, "Read vflip %d", *val);
  345. return 0;
  346. }
  347. static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  348. {
  349. struct sd *sd = (struct sd *) gspca_dev;
  350. s32 *sensor_settings = sd->sensor_priv;
  351. u8 i2c_data;
  352. int err;
  353. sensor_settings[VFLIP_IDX] = val;
  354. PDEBUG(D_V4L2, "Set vflip %d", val);
  355. err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
  356. if (err < 0)
  357. return err;
  358. i2c_data = (i2c_data & 0xbf) | ((val & 0x01) << 6);
  359. err = m5602_write_sensor(sd, PO1030_CONTROL2,
  360. &i2c_data, 1);
  361. return err;
  362. }
  363. static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  364. {
  365. struct sd *sd = (struct sd *) gspca_dev;
  366. s32 *sensor_settings = sd->sensor_priv;
  367. *val = sensor_settings[RED_BALANCE_IDX];
  368. PDEBUG(D_V4L2, "Read red gain %d", *val);
  369. return 0;
  370. }
  371. static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  372. {
  373. struct sd *sd = (struct sd *) gspca_dev;
  374. s32 *sensor_settings = sd->sensor_priv;
  375. u8 i2c_data;
  376. int err;
  377. sensor_settings[RED_BALANCE_IDX] = val;
  378. i2c_data = val & 0xff;
  379. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  380. err = m5602_write_sensor(sd, PO1030_RED_GAIN,
  381. &i2c_data, 1);
  382. return err;
  383. }
  384. static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  385. {
  386. struct sd *sd = (struct sd *) gspca_dev;
  387. s32 *sensor_settings = sd->sensor_priv;
  388. *val = sensor_settings[BLUE_BALANCE_IDX];
  389. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  390. return 0;
  391. }
  392. static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  393. {
  394. struct sd *sd = (struct sd *) gspca_dev;
  395. s32 *sensor_settings = sd->sensor_priv;
  396. u8 i2c_data;
  397. int err;
  398. sensor_settings[BLUE_BALANCE_IDX] = val;
  399. i2c_data = val & 0xff;
  400. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  401. err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
  402. &i2c_data, 1);
  403. return err;
  404. }
  405. static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
  406. __s32 *val)
  407. {
  408. struct sd *sd = (struct sd *) gspca_dev;
  409. s32 *sensor_settings = sd->sensor_priv;
  410. *val = sensor_settings[AUTO_WHITE_BALANCE_IDX];
  411. PDEBUG(D_V4L2, "Auto white balancing is %d", *val);
  412. return 0;
  413. }
  414. static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
  415. __s32 val)
  416. {
  417. struct sd *sd = (struct sd *) gspca_dev;
  418. s32 *sensor_settings = sd->sensor_priv;
  419. u8 i2c_data;
  420. int err;
  421. sensor_settings[AUTO_WHITE_BALANCE_IDX] = val;
  422. err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
  423. if (err < 0)
  424. return err;
  425. i2c_data = (i2c_data & 0xfe) | (val & 0x01);
  426. err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
  427. return err;
  428. }
  429. void po1030_disconnect(struct sd *sd)
  430. {
  431. sd->sensor = NULL;
  432. kfree(sd->sensor_priv);
  433. }
  434. static void po1030_dump_registers(struct sd *sd)
  435. {
  436. int address;
  437. u8 value = 0;
  438. info("Dumping the po1030 sensor core registers");
  439. for (address = 0; address < 0x7f; address++) {
  440. m5602_read_sensor(sd, address, &value, 1);
  441. info("register 0x%x contains 0x%x",
  442. address, value);
  443. }
  444. info("po1030 register state dump complete");
  445. info("Probing for which registers that are read/write");
  446. for (address = 0; address < 0xff; address++) {
  447. u8 old_value, ctrl_value;
  448. u8 test_value[2] = {0xff, 0xff};
  449. m5602_read_sensor(sd, address, &old_value, 1);
  450. m5602_write_sensor(sd, address, test_value, 1);
  451. m5602_read_sensor(sd, address, &ctrl_value, 1);
  452. if (ctrl_value == test_value[0])
  453. info("register 0x%x is writeable", address);
  454. else
  455. info("register 0x%x is read only", address);
  456. /* Restore original value */
  457. m5602_write_sensor(sd, address, &old_value, 1);
  458. }
  459. }