wis-saa7113.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software Foundation,
  15. * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/i2c.h>
  20. #include <linux/videodev2.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/slab.h>
  23. #include "wis-i2c.h"
  24. struct wis_saa7113 {
  25. int norm;
  26. int brightness;
  27. int contrast;
  28. int saturation;
  29. int hue;
  30. };
  31. static u8 initial_registers[] =
  32. {
  33. 0x01, 0x08,
  34. 0x02, 0xc0,
  35. 0x03, 0x33,
  36. 0x04, 0x00,
  37. 0x05, 0x00,
  38. 0x06, 0xe9,
  39. 0x07, 0x0d,
  40. 0x08, 0xd8,
  41. 0x09, 0x40,
  42. 0x0a, 0x80,
  43. 0x0b, 0x47,
  44. 0x0c, 0x40,
  45. 0x0d, 0x00,
  46. 0x0e, 0x01,
  47. 0x0f, 0x2a,
  48. 0x10, 0x40,
  49. 0x11, 0x0c,
  50. 0x12, 0xfe,
  51. 0x13, 0x00,
  52. 0x14, 0x00,
  53. 0x15, 0x04,
  54. 0x16, 0x00,
  55. 0x17, 0x00,
  56. 0x18, 0x00,
  57. 0x19, 0x00,
  58. 0x1a, 0x00,
  59. 0x1b, 0x00,
  60. 0x1c, 0x00,
  61. 0x1d, 0x00,
  62. 0x1e, 0x00,
  63. 0x1f, 0xc8,
  64. 0x40, 0x00,
  65. 0x41, 0xff,
  66. 0x42, 0xff,
  67. 0x43, 0xff,
  68. 0x44, 0xff,
  69. 0x45, 0xff,
  70. 0x46, 0xff,
  71. 0x47, 0xff,
  72. 0x48, 0xff,
  73. 0x49, 0xff,
  74. 0x4a, 0xff,
  75. 0x4b, 0xff,
  76. 0x4c, 0xff,
  77. 0x4d, 0xff,
  78. 0x4e, 0xff,
  79. 0x4f, 0xff,
  80. 0x50, 0xff,
  81. 0x51, 0xff,
  82. 0x52, 0xff,
  83. 0x53, 0xff,
  84. 0x54, 0xff,
  85. 0x55, 0xff,
  86. 0x56, 0xff,
  87. 0x57, 0xff,
  88. 0x58, 0x00,
  89. 0x59, 0x54,
  90. 0x5a, 0x07,
  91. 0x5b, 0x83,
  92. 0x5c, 0x00,
  93. 0x5d, 0x00,
  94. 0x5e, 0x00,
  95. 0x5f, 0x00,
  96. 0x60, 0x00,
  97. 0x61, 0x00,
  98. 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
  99. };
  100. static int write_reg(struct i2c_client *client, u8 reg, u8 value)
  101. {
  102. return i2c_smbus_write_byte_data(client, reg, value);
  103. }
  104. static int write_regs(struct i2c_client *client, u8 *regs)
  105. {
  106. int i;
  107. for (i = 0; regs[i] != 0x00; i += 2)
  108. if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
  109. return -1;
  110. return 0;
  111. }
  112. static int wis_saa7113_command(struct i2c_client *client,
  113. unsigned int cmd, void *arg)
  114. {
  115. struct wis_saa7113 *dec = i2c_get_clientdata(client);
  116. switch (cmd) {
  117. case VIDIOC_S_INPUT:
  118. {
  119. int *input = arg;
  120. i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
  121. i2c_smbus_write_byte_data(client, 0x09,
  122. *input < 6 ? 0x40 : 0x80);
  123. break;
  124. }
  125. case VIDIOC_S_STD:
  126. {
  127. v4l2_std_id *input = arg;
  128. dec->norm = *input;
  129. if (dec->norm & V4L2_STD_NTSC) {
  130. write_reg(client, 0x0e, 0x01);
  131. write_reg(client, 0x10, 0x40);
  132. } else if (dec->norm & V4L2_STD_PAL) {
  133. write_reg(client, 0x0e, 0x01);
  134. write_reg(client, 0x10, 0x48);
  135. } else if (dec->norm * V4L2_STD_SECAM) {
  136. write_reg(client, 0x0e, 0x50);
  137. write_reg(client, 0x10, 0x48);
  138. }
  139. break;
  140. }
  141. case VIDIOC_QUERYCTRL:
  142. {
  143. struct v4l2_queryctrl *ctrl = arg;
  144. switch (ctrl->id) {
  145. case V4L2_CID_BRIGHTNESS:
  146. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  147. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
  148. ctrl->minimum = 0;
  149. ctrl->maximum = 255;
  150. ctrl->step = 1;
  151. ctrl->default_value = 128;
  152. ctrl->flags = 0;
  153. break;
  154. case V4L2_CID_CONTRAST:
  155. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  156. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
  157. ctrl->minimum = 0;
  158. ctrl->maximum = 127;
  159. ctrl->step = 1;
  160. ctrl->default_value = 71;
  161. ctrl->flags = 0;
  162. break;
  163. case V4L2_CID_SATURATION:
  164. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  165. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
  166. ctrl->minimum = 0;
  167. ctrl->maximum = 127;
  168. ctrl->step = 1;
  169. ctrl->default_value = 64;
  170. ctrl->flags = 0;
  171. break;
  172. case V4L2_CID_HUE:
  173. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  174. strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
  175. ctrl->minimum = -128;
  176. ctrl->maximum = 127;
  177. ctrl->step = 1;
  178. ctrl->default_value = 0;
  179. ctrl->flags = 0;
  180. break;
  181. }
  182. break;
  183. }
  184. case VIDIOC_S_CTRL:
  185. {
  186. struct v4l2_control *ctrl = arg;
  187. switch (ctrl->id) {
  188. case V4L2_CID_BRIGHTNESS:
  189. if (ctrl->value > 255)
  190. dec->brightness = 255;
  191. else if (ctrl->value < 0)
  192. dec->brightness = 0;
  193. else
  194. dec->brightness = ctrl->value;
  195. write_reg(client, 0x0a, dec->brightness);
  196. break;
  197. case V4L2_CID_CONTRAST:
  198. if (ctrl->value > 127)
  199. dec->contrast = 127;
  200. else if (ctrl->value < 0)
  201. dec->contrast = 0;
  202. else
  203. dec->contrast = ctrl->value;
  204. write_reg(client, 0x0b, dec->contrast);
  205. break;
  206. case V4L2_CID_SATURATION:
  207. if (ctrl->value > 127)
  208. dec->saturation = 127;
  209. else if (ctrl->value < 0)
  210. dec->saturation = 0;
  211. else
  212. dec->saturation = ctrl->value;
  213. write_reg(client, 0x0c, dec->saturation);
  214. break;
  215. case V4L2_CID_HUE:
  216. if (ctrl->value > 127)
  217. dec->hue = 127;
  218. else if (ctrl->value < -128)
  219. dec->hue = -128;
  220. else
  221. dec->hue = ctrl->value;
  222. write_reg(client, 0x0d, dec->hue);
  223. break;
  224. }
  225. break;
  226. }
  227. case VIDIOC_G_CTRL:
  228. {
  229. struct v4l2_control *ctrl = arg;
  230. switch (ctrl->id) {
  231. case V4L2_CID_BRIGHTNESS:
  232. ctrl->value = dec->brightness;
  233. break;
  234. case V4L2_CID_CONTRAST:
  235. ctrl->value = dec->contrast;
  236. break;
  237. case V4L2_CID_SATURATION:
  238. ctrl->value = dec->saturation;
  239. break;
  240. case V4L2_CID_HUE:
  241. ctrl->value = dec->hue;
  242. break;
  243. }
  244. break;
  245. }
  246. default:
  247. break;
  248. }
  249. return 0;
  250. }
  251. static int wis_saa7113_probe(struct i2c_client *client,
  252. const struct i2c_device_id *id)
  253. {
  254. struct i2c_adapter *adapter = client->adapter;
  255. struct wis_saa7113 *dec;
  256. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  257. return -ENODEV;
  258. dec = kmalloc(sizeof(struct wis_saa7113), GFP_KERNEL);
  259. if (dec == NULL)
  260. return -ENOMEM;
  261. dec->norm = V4L2_STD_NTSC;
  262. dec->brightness = 128;
  263. dec->contrast = 71;
  264. dec->saturation = 64;
  265. dec->hue = 0;
  266. i2c_set_clientdata(client, dec);
  267. printk(KERN_DEBUG
  268. "wis-saa7113: initializing SAA7113 at address %d on %s\n",
  269. client->addr, adapter->name);
  270. if (write_regs(client, initial_registers) < 0) {
  271. printk(KERN_ERR
  272. "wis-saa7113: error initializing SAA7113\n");
  273. kfree(dec);
  274. return -ENODEV;
  275. }
  276. return 0;
  277. }
  278. static int wis_saa7113_remove(struct i2c_client *client)
  279. {
  280. struct wis_saa7113 *dec = i2c_get_clientdata(client);
  281. kfree(dec);
  282. return 0;
  283. }
  284. static const struct i2c_device_id wis_saa7113_id[] = {
  285. { "wis_saa7113", 0 },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(i2c, wis_saa7113_id);
  289. static struct i2c_driver wis_saa7113_driver = {
  290. .driver = {
  291. .name = "WIS SAA7113 I2C driver",
  292. },
  293. .probe = wis_saa7113_probe,
  294. .remove = wis_saa7113_remove,
  295. .command = wis_saa7113_command,
  296. .id_table = wis_saa7113_id,
  297. };
  298. static int __init wis_saa7113_init(void)
  299. {
  300. return i2c_add_driver(&wis_saa7113_driver);
  301. }
  302. static void __exit wis_saa7113_cleanup(void)
  303. {
  304. i2c_del_driver(&wis_saa7113_driver);
  305. }
  306. module_init(wis_saa7113_init);
  307. module_exit(wis_saa7113_cleanup);
  308. MODULE_LICENSE("GPL v2");