indycam.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * indycam.c - Silicon Graphics IndyCam digital camera driver
  3. *
  4. * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
  5. * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/major.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev.h>
  22. /* IndyCam decodes stream of photons into digital image representation ;-) */
  23. #include <linux/video_decoder.h>
  24. #include <linux/i2c.h>
  25. #include "indycam.h"
  26. #define INDYCAM_MODULE_VERSION "0.0.5"
  27. MODULE_DESCRIPTION("SGI IndyCam driver");
  28. MODULE_VERSION(INDYCAM_MODULE_VERSION);
  29. MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
  30. MODULE_LICENSE("GPL");
  31. // #define INDYCAM_DEBUG
  32. #ifdef INDYCAM_DEBUG
  33. #define dprintk(x...) printk("IndyCam: " x);
  34. #define indycam_regdump(client) indycam_regdump_debug(client)
  35. #else
  36. #define dprintk(x...)
  37. #define indycam_regdump(client)
  38. #endif
  39. struct indycam {
  40. struct i2c_client *client;
  41. u8 version;
  42. };
  43. static struct i2c_driver i2c_driver_indycam;
  44. static const u8 initseq[] = {
  45. INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
  46. INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
  47. INDYCAM_GAIN_DEFAULT, /* INDYCAM_GAIN */
  48. 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
  49. INDYCAM_RED_BALANCE_DEFAULT, /* INDYCAM_RED_BALANCE */
  50. INDYCAM_BLUE_BALANCE_DEFAULT, /* INDYCAM_BLUE_BALANCE */
  51. INDYCAM_RED_SATURATION_DEFAULT, /* INDYCAM_RED_SATURATION */
  52. INDYCAM_BLUE_SATURATION_DEFAULT,/* INDYCAM_BLUE_SATURATION */
  53. };
  54. /* IndyCam register handling */
  55. static int indycam_read_reg(struct i2c_client *client, u8 reg, u8 *value)
  56. {
  57. int ret;
  58. if (reg == INDYCAM_REG_RESET) {
  59. dprintk("indycam_read_reg(): "
  60. "skipping write-only register %d\n", reg);
  61. *value = 0;
  62. return 0;
  63. }
  64. ret = i2c_smbus_read_byte_data(client, reg);
  65. if (ret < 0) {
  66. printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
  67. "register = 0x%02x\n", reg);
  68. return ret;
  69. }
  70. *value = (u8)ret;
  71. return 0;
  72. }
  73. static int indycam_write_reg(struct i2c_client *client, u8 reg, u8 value)
  74. {
  75. int err;
  76. if ((reg == INDYCAM_REG_BRIGHTNESS)
  77. || (reg == INDYCAM_REG_VERSION)) {
  78. dprintk("indycam_write_reg(): "
  79. "skipping read-only register %d\n", reg);
  80. return 0;
  81. }
  82. dprintk("Writing Reg %d = 0x%02x\n", reg, value);
  83. err = i2c_smbus_write_byte_data(client, reg, value);
  84. if (err) {
  85. printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
  86. "register = 0x%02x, value = 0x%02x\n", reg, value);
  87. }
  88. return err;
  89. }
  90. static int indycam_write_block(struct i2c_client *client, u8 reg,
  91. u8 length, u8 *data)
  92. {
  93. int i, err;
  94. for (i = 0; i < length; i++) {
  95. err = indycam_write_reg(client, reg + i, data[i]);
  96. if (err)
  97. return err;
  98. }
  99. return 0;
  100. }
  101. /* Helper functions */
  102. #ifdef INDYCAM_DEBUG
  103. static void indycam_regdump_debug(struct i2c_client *client)
  104. {
  105. int i;
  106. u8 val;
  107. for (i = 0; i < 9; i++) {
  108. indycam_read_reg(client, i, &val);
  109. dprintk("Reg %d = 0x%02x\n", i, val);
  110. }
  111. }
  112. #endif
  113. static int indycam_get_control(struct i2c_client *client,
  114. struct indycam_control *ctrl)
  115. {
  116. struct indycam *camera = i2c_get_clientdata(client);
  117. u8 reg;
  118. int ret = 0;
  119. switch (ctrl->type) {
  120. case INDYCAM_CONTROL_AGC:
  121. case INDYCAM_CONTROL_AWB:
  122. ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
  123. if (ret)
  124. return -EIO;
  125. if (ctrl->type == INDYCAM_CONTROL_AGC)
  126. ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
  127. ? 1 : 0;
  128. else
  129. ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
  130. ? 1 : 0;
  131. break;
  132. case INDYCAM_CONTROL_SHUTTER:
  133. ret = indycam_read_reg(client, INDYCAM_REG_SHUTTER, &reg);
  134. if (ret)
  135. return -EIO;
  136. ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
  137. break;
  138. case INDYCAM_CONTROL_GAIN:
  139. ret = indycam_read_reg(client, INDYCAM_REG_GAIN, &reg);
  140. if (ret)
  141. return -EIO;
  142. ctrl->value = (s32)reg;
  143. break;
  144. case INDYCAM_CONTROL_RED_BALANCE:
  145. ret = indycam_read_reg(client, INDYCAM_REG_RED_BALANCE, &reg);
  146. if (ret)
  147. return -EIO;
  148. ctrl->value = (s32)reg;
  149. break;
  150. case INDYCAM_CONTROL_BLUE_BALANCE:
  151. ret = indycam_read_reg(client, INDYCAM_REG_BLUE_BALANCE, &reg);
  152. if (ret)
  153. return -EIO;
  154. ctrl->value = (s32)reg;
  155. break;
  156. case INDYCAM_CONTROL_RED_SATURATION:
  157. ret = indycam_read_reg(client,
  158. INDYCAM_REG_RED_SATURATION, &reg);
  159. if (ret)
  160. return -EIO;
  161. ctrl->value = (s32)reg;
  162. break;
  163. case INDYCAM_CONTROL_BLUE_SATURATION:
  164. ret = indycam_read_reg(client,
  165. INDYCAM_REG_BLUE_SATURATION, &reg);
  166. if (ret)
  167. return -EIO;
  168. ctrl->value = (s32)reg;
  169. break;
  170. case INDYCAM_CONTROL_GAMMA:
  171. if (camera->version == CAMERA_VERSION_MOOSE) {
  172. ret = indycam_read_reg(client,
  173. INDYCAM_REG_GAMMA, &reg);
  174. if (ret)
  175. return -EIO;
  176. ctrl->value = (s32)reg;
  177. } else {
  178. ctrl->value = INDYCAM_GAMMA_DEFAULT;
  179. }
  180. break;
  181. default:
  182. ret = -EINVAL;
  183. }
  184. return ret;
  185. }
  186. static int indycam_set_control(struct i2c_client *client,
  187. struct indycam_control *ctrl)
  188. {
  189. struct indycam *camera = i2c_get_clientdata(client);
  190. u8 reg;
  191. int ret = 0;
  192. switch (ctrl->type) {
  193. case INDYCAM_CONTROL_AGC:
  194. case INDYCAM_CONTROL_AWB:
  195. ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
  196. if (ret)
  197. break;
  198. if (ctrl->type == INDYCAM_CONTROL_AGC) {
  199. if (ctrl->value)
  200. reg |= INDYCAM_CONTROL_AGCENA;
  201. else
  202. reg &= ~INDYCAM_CONTROL_AGCENA;
  203. } else {
  204. if (ctrl->value)
  205. reg |= INDYCAM_CONTROL_AWBCTL;
  206. else
  207. reg &= ~INDYCAM_CONTROL_AWBCTL;
  208. }
  209. ret = indycam_write_reg(client, INDYCAM_REG_CONTROL, reg);
  210. break;
  211. case INDYCAM_CONTROL_SHUTTER:
  212. reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
  213. ret = indycam_write_reg(client, INDYCAM_REG_SHUTTER, reg);
  214. break;
  215. case INDYCAM_CONTROL_GAIN:
  216. ret = indycam_write_reg(client, INDYCAM_REG_GAIN, ctrl->value);
  217. break;
  218. case INDYCAM_CONTROL_RED_BALANCE:
  219. ret = indycam_write_reg(client, INDYCAM_REG_RED_BALANCE,
  220. ctrl->value);
  221. break;
  222. case INDYCAM_CONTROL_BLUE_BALANCE:
  223. ret = indycam_write_reg(client, INDYCAM_REG_BLUE_BALANCE,
  224. ctrl->value);
  225. break;
  226. case INDYCAM_CONTROL_RED_SATURATION:
  227. ret = indycam_write_reg(client, INDYCAM_REG_RED_SATURATION,
  228. ctrl->value);
  229. break;
  230. case INDYCAM_CONTROL_BLUE_SATURATION:
  231. ret = indycam_write_reg(client, INDYCAM_REG_BLUE_SATURATION,
  232. ctrl->value);
  233. break;
  234. case INDYCAM_CONTROL_GAMMA:
  235. if (camera->version == CAMERA_VERSION_MOOSE) {
  236. ret = indycam_write_reg(client, INDYCAM_REG_GAMMA,
  237. ctrl->value);
  238. }
  239. break;
  240. default:
  241. ret = -EINVAL;
  242. }
  243. return ret;
  244. }
  245. /* I2C-interface */
  246. static int indycam_attach(struct i2c_adapter *adap, int addr, int kind)
  247. {
  248. int err = 0;
  249. struct indycam *camera;
  250. struct i2c_client *client;
  251. printk(KERN_INFO "SGI IndyCam driver version %s\n",
  252. INDYCAM_MODULE_VERSION);
  253. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  254. if (!client)
  255. return -ENOMEM;
  256. camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
  257. if (!camera) {
  258. err = -ENOMEM;
  259. goto out_free_client;
  260. }
  261. client->addr = addr;
  262. client->adapter = adap;
  263. client->driver = &i2c_driver_indycam;
  264. client->flags = 0;
  265. strcpy(client->name, "IndyCam client");
  266. i2c_set_clientdata(client, camera);
  267. camera->client = client;
  268. err = i2c_attach_client(client);
  269. if (err)
  270. goto out_free_camera;
  271. camera->version = i2c_smbus_read_byte_data(client,
  272. INDYCAM_REG_VERSION);
  273. if (camera->version != CAMERA_VERSION_INDY &&
  274. camera->version != CAMERA_VERSION_MOOSE) {
  275. err = -ENODEV;
  276. goto out_detach_client;
  277. }
  278. printk(KERN_INFO "IndyCam v%d.%d detected\n",
  279. INDYCAM_VERSION_MAJOR(camera->version),
  280. INDYCAM_VERSION_MINOR(camera->version));
  281. indycam_regdump(client);
  282. // initialize
  283. err = indycam_write_block(client, 0, sizeof(initseq), (u8 *)&initseq);
  284. if (err) {
  285. printk(KERN_ERR "IndyCam initalization failed\n");
  286. err = -EIO;
  287. goto out_detach_client;
  288. }
  289. indycam_regdump(client);
  290. // white balance
  291. err = indycam_write_reg(client, INDYCAM_REG_CONTROL,
  292. INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
  293. if (err) {
  294. printk(KERN_ERR "IndyCam: White balancing camera failed\n");
  295. err = -EIO;
  296. goto out_detach_client;
  297. }
  298. indycam_regdump(client);
  299. printk(KERN_INFO "IndyCam initialized\n");
  300. return 0;
  301. out_detach_client:
  302. i2c_detach_client(client);
  303. out_free_camera:
  304. kfree(camera);
  305. out_free_client:
  306. kfree(client);
  307. return err;
  308. }
  309. static int indycam_probe(struct i2c_adapter *adap)
  310. {
  311. /* Indy specific crap */
  312. if (adap->id == I2C_HW_SGI_VINO)
  313. return indycam_attach(adap, INDYCAM_ADDR, 0);
  314. /* Feel free to add probe here :-) */
  315. return -ENODEV;
  316. }
  317. static int indycam_detach(struct i2c_client *client)
  318. {
  319. struct indycam *camera = i2c_get_clientdata(client);
  320. i2c_detach_client(client);
  321. kfree(camera);
  322. kfree(client);
  323. return 0;
  324. }
  325. static int indycam_command(struct i2c_client *client, unsigned int cmd,
  326. void *arg)
  327. {
  328. // struct indycam *camera = i2c_get_clientdata(client);
  329. /* The old video_decoder interface just isn't enough,
  330. * so we'll use some custom commands. */
  331. switch (cmd) {
  332. case DECODER_GET_CAPABILITIES: {
  333. struct video_decoder_capability *cap = arg;
  334. cap->flags = VIDEO_DECODER_NTSC;
  335. cap->inputs = 1;
  336. cap->outputs = 1;
  337. break;
  338. }
  339. case DECODER_GET_STATUS: {
  340. int *iarg = arg;
  341. *iarg = DECODER_STATUS_GOOD | DECODER_STATUS_NTSC |
  342. DECODER_STATUS_COLOR;
  343. break;
  344. }
  345. case DECODER_SET_NORM: {
  346. int *iarg = arg;
  347. switch (*iarg) {
  348. case VIDEO_MODE_NTSC:
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. break;
  354. }
  355. case DECODER_SET_INPUT: {
  356. int *iarg = arg;
  357. if (*iarg != 0)
  358. return -EINVAL;
  359. break;
  360. }
  361. case DECODER_SET_OUTPUT: {
  362. int *iarg = arg;
  363. if (*iarg != 0)
  364. return -EINVAL;
  365. break;
  366. }
  367. case DECODER_ENABLE_OUTPUT: {
  368. /* Always enabled */
  369. break;
  370. }
  371. case DECODER_SET_PICTURE: {
  372. // struct video_picture *pic = arg;
  373. /* TODO: convert values for indycam_set_controls() */
  374. break;
  375. }
  376. case DECODER_INDYCAM_GET_CONTROL: {
  377. return indycam_get_control(client, arg);
  378. }
  379. case DECODER_INDYCAM_SET_CONTROL: {
  380. return indycam_set_control(client, arg);
  381. }
  382. default:
  383. return -EINVAL;
  384. }
  385. return 0;
  386. }
  387. static struct i2c_driver i2c_driver_indycam = {
  388. .driver = {
  389. .name = "indycam",
  390. },
  391. .id = I2C_DRIVERID_INDYCAM,
  392. .attach_adapter = indycam_probe,
  393. .detach_client = indycam_detach,
  394. .command = indycam_command,
  395. };
  396. static int __init indycam_init(void)
  397. {
  398. return i2c_add_driver(&i2c_driver_indycam);
  399. }
  400. static void __exit indycam_exit(void)
  401. {
  402. i2c_del_driver(&i2c_driver_indycam);
  403. }
  404. module_init(indycam_init);
  405. module_exit(indycam_exit);