m5602_po1030.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 void po1030_dump_registers(struct sd *sd);
  20. int po1030_probe(struct sd *sd)
  21. {
  22. u8 prod_id = 0, ver_id = 0, i;
  23. if (force_sensor) {
  24. if (force_sensor == PO1030_SENSOR) {
  25. info("Forcing a %s sensor", po1030.name);
  26. goto sensor_found;
  27. }
  28. /* If we want to force another sensor, don't try to probe this
  29. * one */
  30. return -ENODEV;
  31. }
  32. info("Probing for a po1030 sensor");
  33. /* Run the pre-init to actually probe the unit */
  34. for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
  35. u8 data = preinit_po1030[i][2];
  36. if (preinit_po1030[i][0] == SENSOR)
  37. po1030_write_sensor(sd,
  38. preinit_po1030[i][1], &data, 1);
  39. else
  40. m5602_write_bridge(sd, preinit_po1030[i][1], data);
  41. }
  42. if (po1030_read_sensor(sd, 0x3, &prod_id, 1))
  43. return -ENODEV;
  44. if (po1030_read_sensor(sd, 0x4, &ver_id, 1))
  45. return -ENODEV;
  46. if ((prod_id == 0x02) && (ver_id == 0xef)) {
  47. info("Detected a po1030 sensor");
  48. goto sensor_found;
  49. }
  50. return -ENODEV;
  51. sensor_found:
  52. sd->gspca_dev.cam.cam_mode = po1030.modes;
  53. sd->gspca_dev.cam.nmodes = po1030.nmodes;
  54. sd->desc->ctrls = po1030.ctrls;
  55. sd->desc->nctrls = po1030.nctrls;
  56. return 0;
  57. }
  58. int po1030_read_sensor(struct sd *sd, const u8 address,
  59. u8 *i2c_data, const u8 len)
  60. {
  61. int err, i;
  62. do {
  63. err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
  64. } while ((*i2c_data & I2C_BUSY) && !err);
  65. err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
  66. sd->sensor->i2c_slave_id);
  67. if (err < 0)
  68. goto out;
  69. err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
  70. if (err < 0)
  71. goto out;
  72. err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
  73. if (err < 0)
  74. goto out;
  75. err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
  76. if (err < 0)
  77. goto out;
  78. for (i = 0; (i < len) && !err; i++) {
  79. err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
  80. PDEBUG(D_CONF, "Reading sensor register "
  81. "0x%x containing 0x%x ", address, *i2c_data);
  82. }
  83. out:
  84. return err;
  85. }
  86. int po1030_write_sensor(struct sd *sd, const u8 address,
  87. u8 *i2c_data, const u8 len)
  88. {
  89. int err, i;
  90. u8 *p;
  91. struct usb_device *udev = sd->gspca_dev.dev;
  92. __u8 *buf = sd->gspca_dev.usb_buf;
  93. /* The po1030 only supports one byte writes */
  94. if (len > 1 || !len)
  95. return -EINVAL;
  96. memcpy(buf, sensor_urb_skeleton, sizeof(sensor_urb_skeleton));
  97. buf[11] = sd->sensor->i2c_slave_id;
  98. buf[15] = address;
  99. p = buf + 16;
  100. /* Copy a four byte write sequence for each byte to be written to */
  101. for (i = 0; i < len; i++) {
  102. memcpy(p, sensor_urb_skeleton + 16, 4);
  103. p[3] = i2c_data[i];
  104. p += 4;
  105. PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
  106. address, i2c_data[i]);
  107. }
  108. /* Copy the footer */
  109. memcpy(p, sensor_urb_skeleton + 20, 4);
  110. /* Set the total length */
  111. p[3] = 0x10 + len;
  112. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  113. 0x04, 0x40, 0x19,
  114. 0x0000, buf,
  115. 20 + len * 4, M5602_URB_MSG_TIMEOUT);
  116. return (err < 0) ? err : 0;
  117. }
  118. int po1030_init(struct sd *sd)
  119. {
  120. int i, err = 0;
  121. /* Init the sensor */
  122. for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
  123. u8 data[2] = {0x00, 0x00};
  124. switch (init_po1030[i][0]) {
  125. case BRIDGE:
  126. err = m5602_write_bridge(sd,
  127. init_po1030[i][1],
  128. init_po1030[i][2]);
  129. break;
  130. case SENSOR:
  131. data[0] = init_po1030[i][2];
  132. err = po1030_write_sensor(sd,
  133. init_po1030[i][1], data, 1);
  134. break;
  135. case SENSOR_LONG:
  136. data[0] = init_po1030[i][2];
  137. data[1] = init_po1030[i][3];
  138. err = po1030_write_sensor(sd,
  139. init_po1030[i][1], data, 2);
  140. break;
  141. default:
  142. info("Invalid stream command, exiting init");
  143. return -EINVAL;
  144. }
  145. }
  146. if (dump_sensor)
  147. po1030_dump_registers(sd);
  148. return err;
  149. }
  150. int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  151. {
  152. struct sd *sd = (struct sd *) gspca_dev;
  153. u8 i2c_data;
  154. int err;
  155. err = po1030_read_sensor(sd, PO1030_REG_INTEGLINES_H,
  156. &i2c_data, 1);
  157. if (err < 0)
  158. goto out;
  159. *val = (i2c_data << 8);
  160. err = po1030_read_sensor(sd, PO1030_REG_INTEGLINES_M,
  161. &i2c_data, 1);
  162. *val |= i2c_data;
  163. PDEBUG(D_V4L2, "Exposure read as %d", *val);
  164. out:
  165. return err;
  166. }
  167. int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  168. {
  169. struct sd *sd = (struct sd *) gspca_dev;
  170. u8 i2c_data;
  171. int err;
  172. PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
  173. i2c_data = ((val & 0xff00) >> 8);
  174. PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
  175. i2c_data);
  176. err = po1030_write_sensor(sd, PO1030_REG_INTEGLINES_H,
  177. &i2c_data, 1);
  178. if (err < 0)
  179. goto out;
  180. i2c_data = (val & 0xff);
  181. PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
  182. i2c_data);
  183. err = po1030_write_sensor(sd, PO1030_REG_INTEGLINES_M,
  184. &i2c_data, 1);
  185. out:
  186. return err;
  187. }
  188. int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  189. {
  190. struct sd *sd = (struct sd *) gspca_dev;
  191. u8 i2c_data;
  192. int err;
  193. err = po1030_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  194. &i2c_data, 1);
  195. *val = i2c_data;
  196. PDEBUG(D_V4L2, "Read global gain %d", *val);
  197. return err;
  198. }
  199. int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  200. {
  201. struct sd *sd = (struct sd *) gspca_dev;
  202. u8 i2c_data;
  203. int err;
  204. err = po1030_read_sensor(sd, PO1030_REG_CONTROL2,
  205. &i2c_data, 1);
  206. *val = (i2c_data >> 7) & 0x01 ;
  207. PDEBUG(D_V4L2, "Read hflip %d", *val);
  208. return err;
  209. }
  210. int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  211. {
  212. struct sd *sd = (struct sd *) gspca_dev;
  213. u8 i2c_data;
  214. int err;
  215. PDEBUG(D_V4L2, "Set hflip %d", val);
  216. i2c_data = (val & 0x01) << 7;
  217. err = po1030_write_sensor(sd, PO1030_REG_CONTROL2,
  218. &i2c_data, 1);
  219. return err;
  220. }
  221. int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  222. {
  223. struct sd *sd = (struct sd *) gspca_dev;
  224. u8 i2c_data;
  225. int err;
  226. err = po1030_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  227. &i2c_data, 1);
  228. *val = (i2c_data >> 6) & 0x01;
  229. PDEBUG(D_V4L2, "Read vflip %d", *val);
  230. return err;
  231. }
  232. int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  233. {
  234. struct sd *sd = (struct sd *) gspca_dev;
  235. u8 i2c_data;
  236. int err;
  237. PDEBUG(D_V4L2, "Set vflip %d", val);
  238. i2c_data = (val & 0x01) << 6;
  239. err = po1030_write_sensor(sd, PO1030_REG_CONTROL2,
  240. &i2c_data, 1);
  241. return err;
  242. }
  243. int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  244. {
  245. struct sd *sd = (struct sd *) gspca_dev;
  246. u8 i2c_data;
  247. int err;
  248. i2c_data = val & 0xff;
  249. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  250. err = po1030_write_sensor(sd, PO1030_REG_GLOBALGAIN,
  251. &i2c_data, 1);
  252. return err;
  253. }
  254. int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  255. {
  256. struct sd *sd = (struct sd *) gspca_dev;
  257. u8 i2c_data;
  258. int err;
  259. err = po1030_read_sensor(sd, PO1030_REG_RED_GAIN,
  260. &i2c_data, 1);
  261. *val = i2c_data;
  262. PDEBUG(D_V4L2, "Read red gain %d", *val);
  263. return err;
  264. }
  265. int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  266. {
  267. struct sd *sd = (struct sd *) gspca_dev;
  268. u8 i2c_data;
  269. int err;
  270. i2c_data = val & 0xff;
  271. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  272. err = po1030_write_sensor(sd, PO1030_REG_RED_GAIN,
  273. &i2c_data, 1);
  274. return err;
  275. }
  276. int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  277. {
  278. struct sd *sd = (struct sd *) gspca_dev;
  279. u8 i2c_data;
  280. int err;
  281. err = po1030_read_sensor(sd, PO1030_REG_BLUE_GAIN,
  282. &i2c_data, 1);
  283. *val = i2c_data;
  284. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  285. return err;
  286. }
  287. int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  288. {
  289. struct sd *sd = (struct sd *) gspca_dev;
  290. u8 i2c_data;
  291. int err;
  292. i2c_data = val & 0xff;
  293. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  294. err = po1030_write_sensor(sd, PO1030_REG_BLUE_GAIN,
  295. &i2c_data, 1);
  296. return err;
  297. }
  298. int po1030_power_down(struct sd *sd)
  299. {
  300. return 0;
  301. }
  302. static void po1030_dump_registers(struct sd *sd)
  303. {
  304. int address;
  305. u8 value = 0;
  306. info("Dumping the po1030 sensor core registers");
  307. for (address = 0; address < 0x7f; address++) {
  308. po1030_read_sensor(sd, address, &value, 1);
  309. info("register 0x%x contains 0x%x",
  310. address, value);
  311. }
  312. info("po1030 register state dump complete");
  313. info("Probing for which registers that are read/write");
  314. for (address = 0; address < 0xff; address++) {
  315. u8 old_value, ctrl_value;
  316. u8 test_value[2] = {0xff, 0xff};
  317. po1030_read_sensor(sd, address, &old_value, 1);
  318. po1030_write_sensor(sd, address, test_value, 1);
  319. po1030_read_sensor(sd, address, &ctrl_value, 1);
  320. if (ctrl_value == test_value[0])
  321. info("register 0x%x is writeable", address);
  322. else
  323. info("register 0x%x is read only", address);
  324. /* Restore original value */
  325. po1030_write_sensor(sd, address, &old_value, 1);
  326. }
  327. }