indycam.c 11 KB

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