wis-tw9903.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_tw9903 {
  25. int norm;
  26. int brightness;
  27. int contrast;
  28. int hue;
  29. };
  30. static u8 initial_registers[] =
  31. {
  32. 0x02, 0x44, /* input 1, composite */
  33. 0x03, 0x92, /* correct digital format */
  34. 0x04, 0x00,
  35. 0x05, 0x80, /* or 0x00 for PAL */
  36. 0x06, 0x40, /* second internal current reference */
  37. 0x07, 0x02, /* window */
  38. 0x08, 0x14, /* window */
  39. 0x09, 0xf0, /* window */
  40. 0x0a, 0x81, /* window */
  41. 0x0b, 0xd0, /* window */
  42. 0x0c, 0x8c,
  43. 0x0d, 0x00, /* scaling */
  44. 0x0e, 0x11, /* scaling */
  45. 0x0f, 0x00, /* scaling */
  46. 0x10, 0x00, /* brightness */
  47. 0x11, 0x60, /* contrast */
  48. 0x12, 0x01, /* sharpness */
  49. 0x13, 0x7f, /* U gain */
  50. 0x14, 0x5a, /* V gain */
  51. 0x15, 0x00, /* hue */
  52. 0x16, 0xc3, /* sharpness */
  53. 0x18, 0x00,
  54. 0x19, 0x58, /* vbi */
  55. 0x1a, 0x80,
  56. 0x1c, 0x0f, /* video norm */
  57. 0x1d, 0x7f, /* video norm */
  58. 0x20, 0xa0, /* clamping gain (working 0x50) */
  59. 0x21, 0x22,
  60. 0x22, 0xf0,
  61. 0x23, 0xfe,
  62. 0x24, 0x3c,
  63. 0x25, 0x38,
  64. 0x26, 0x44,
  65. 0x27, 0x20,
  66. 0x28, 0x00,
  67. 0x29, 0x15,
  68. 0x2a, 0xa0,
  69. 0x2b, 0x44,
  70. 0x2c, 0x37,
  71. 0x2d, 0x00,
  72. 0x2e, 0xa5, /* burst PLL control (working: a9) */
  73. 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
  74. 0x31, 0x00,
  75. 0x33, 0x22,
  76. 0x34, 0x11,
  77. 0x35, 0x35,
  78. 0x3b, 0x05,
  79. 0x06, 0xc0, /* reset device */
  80. 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
  81. };
  82. static int write_reg(struct i2c_client *client, u8 reg, u8 value)
  83. {
  84. return i2c_smbus_write_byte_data(client, reg, value);
  85. }
  86. static int write_regs(struct i2c_client *client, u8 *regs)
  87. {
  88. int i;
  89. for (i = 0; regs[i] != 0x00; i += 2)
  90. if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
  91. return -1;
  92. return 0;
  93. }
  94. static int wis_tw9903_command(struct i2c_client *client,
  95. unsigned int cmd, void *arg)
  96. {
  97. struct wis_tw9903 *dec = i2c_get_clientdata(client);
  98. switch (cmd) {
  99. case VIDIOC_S_INPUT:
  100. {
  101. int *input = arg;
  102. i2c_smbus_write_byte_data(client, 0x02, 0x40 | (*input << 1));
  103. break;
  104. }
  105. #if 0
  106. /* The scaler on this thing seems to be horribly broken */
  107. case DECODER_SET_RESOLUTION:
  108. {
  109. struct video_decoder_resolution *res = arg;
  110. /*int hscale = 256 * 720 / res->width;*/
  111. int hscale = 256 * 720 / (res->width - (res->width > 704 ? 0 : 8));
  112. int vscale = 256 * (dec->norm & V4L2_STD_NTSC ? 240 : 288)
  113. / res->height;
  114. u8 regs[] = {
  115. 0x0d, vscale & 0xff,
  116. 0x0f, hscale & 0xff,
  117. 0x0e, ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8),
  118. 0x06, 0xc0, /* reset device */
  119. 0, 0,
  120. };
  121. printk(KERN_DEBUG "vscale is %04x, hscale is %04x\n",
  122. vscale, hscale);
  123. /*write_regs(client, regs);*/
  124. break;
  125. }
  126. #endif
  127. case VIDIOC_S_STD:
  128. {
  129. v4l2_std_id *input = arg;
  130. u8 regs[] = {
  131. 0x05, *input & V4L2_STD_NTSC ? 0x80 : 0x00,
  132. 0x07, *input & V4L2_STD_NTSC ? 0x02 : 0x12,
  133. 0x08, *input & V4L2_STD_NTSC ? 0x14 : 0x18,
  134. 0x09, *input & V4L2_STD_NTSC ? 0xf0 : 0x20,
  135. 0, 0,
  136. };
  137. write_regs(client, regs);
  138. dec->norm = *input;
  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 = -128;
  149. ctrl->maximum = 127;
  150. ctrl->step = 1;
  151. ctrl->default_value = 0x00;
  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 = 255;
  159. ctrl->step = 1;
  160. ctrl->default_value = 0x60;
  161. ctrl->flags = 0;
  162. break;
  163. #if 0
  164. /* I don't understand how the Chroma Gain registers work... */
  165. case V4L2_CID_SATURATION:
  166. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  167. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
  168. ctrl->minimum = 0;
  169. ctrl->maximum = 127;
  170. ctrl->step = 1;
  171. ctrl->default_value = 64;
  172. ctrl->flags = 0;
  173. break;
  174. #endif
  175. case V4L2_CID_HUE:
  176. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  177. strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
  178. ctrl->minimum = -128;
  179. ctrl->maximum = 127;
  180. ctrl->step = 1;
  181. ctrl->default_value = 0;
  182. ctrl->flags = 0;
  183. break;
  184. }
  185. break;
  186. }
  187. case VIDIOC_S_CTRL:
  188. {
  189. struct v4l2_control *ctrl = arg;
  190. switch (ctrl->id) {
  191. case V4L2_CID_BRIGHTNESS:
  192. if (ctrl->value > 127)
  193. dec->brightness = 127;
  194. else if (ctrl->value < -128)
  195. dec->brightness = -128;
  196. else
  197. dec->brightness = ctrl->value;
  198. write_reg(client, 0x10, dec->brightness);
  199. break;
  200. case V4L2_CID_CONTRAST:
  201. if (ctrl->value > 255)
  202. dec->contrast = 255;
  203. else if (ctrl->value < 0)
  204. dec->contrast = 0;
  205. else
  206. dec->contrast = ctrl->value;
  207. write_reg(client, 0x11, dec->contrast);
  208. break;
  209. #if 0
  210. case V4L2_CID_SATURATION:
  211. if (ctrl->value > 127)
  212. dec->saturation = 127;
  213. else if (ctrl->value < 0)
  214. dec->saturation = 0;
  215. else
  216. dec->saturation = ctrl->value;
  217. /*write_reg(client, 0x0c, dec->saturation);*/
  218. break;
  219. #endif
  220. case V4L2_CID_HUE:
  221. if (ctrl->value > 127)
  222. dec->hue = 127;
  223. else if (ctrl->value < -128)
  224. dec->hue = -128;
  225. else
  226. dec->hue = ctrl->value;
  227. write_reg(client, 0x15, dec->hue);
  228. break;
  229. }
  230. break;
  231. }
  232. case VIDIOC_G_CTRL:
  233. {
  234. struct v4l2_control *ctrl = arg;
  235. switch (ctrl->id) {
  236. case V4L2_CID_BRIGHTNESS:
  237. ctrl->value = dec->brightness;
  238. break;
  239. case V4L2_CID_CONTRAST:
  240. ctrl->value = dec->contrast;
  241. break;
  242. #if 0
  243. case V4L2_CID_SATURATION:
  244. ctrl->value = dec->saturation;
  245. break;
  246. #endif
  247. case V4L2_CID_HUE:
  248. ctrl->value = dec->hue;
  249. break;
  250. }
  251. break;
  252. }
  253. default:
  254. break;
  255. }
  256. return 0;
  257. }
  258. static int wis_tw9903_probe(struct i2c_client *client,
  259. const struct i2c_device_id *id)
  260. {
  261. struct i2c_adapter *adapter = client->adapter;
  262. struct wis_tw9903 *dec;
  263. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  264. return -ENODEV;
  265. dec = kmalloc(sizeof(struct wis_tw9903), GFP_KERNEL);
  266. if (dec == NULL)
  267. return -ENOMEM;
  268. dec->norm = V4L2_STD_NTSC;
  269. dec->brightness = 0;
  270. dec->contrast = 0x60;
  271. dec->hue = 0;
  272. i2c_set_clientdata(client, dec);
  273. printk(KERN_DEBUG
  274. "wis-tw9903: initializing TW9903 at address %d on %s\n",
  275. client->addr, adapter->name);
  276. if (write_regs(client, initial_registers) < 0) {
  277. printk(KERN_ERR "wis-tw9903: error initializing TW9903\n");
  278. kfree(dec);
  279. return -ENODEV;
  280. }
  281. return 0;
  282. }
  283. static int wis_tw9903_remove(struct i2c_client *client)
  284. {
  285. struct wis_tw9903 *dec = i2c_get_clientdata(client);
  286. kfree(dec);
  287. return 0;
  288. }
  289. static const struct i2c_device_id wis_tw9903_id[] = {
  290. { "wis_tw9903", 0 },
  291. { }
  292. };
  293. MODULE_DEVICE_TABLE(i2c, wis_tw9903_id);
  294. static struct i2c_driver wis_tw9903_driver = {
  295. .driver = {
  296. .name = "WIS TW9903 I2C driver",
  297. },
  298. .probe = wis_tw9903_probe,
  299. .remove = wis_tw9903_remove,
  300. .command = wis_tw9903_command,
  301. .id_table = wis_tw9903_id,
  302. };
  303. static int __init wis_tw9903_init(void)
  304. {
  305. return i2c_add_driver(&wis_tw9903_driver);
  306. }
  307. static void __exit wis_tw9903_cleanup(void)
  308. {
  309. i2c_del_driver(&wis_tw9903_driver);
  310. }
  311. module_init(wis_tw9903_init);
  312. module_exit(wis_tw9903_cleanup);
  313. MODULE_LICENSE("GPL v2");