wis-tw2804.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_tw2804 {
  25. int channel;
  26. int norm;
  27. int brightness;
  28. int contrast;
  29. int saturation;
  30. int hue;
  31. };
  32. static u8 global_registers[] =
  33. {
  34. 0x39, 0x00,
  35. 0x3a, 0xff,
  36. 0x3b, 0x84,
  37. 0x3c, 0x80,
  38. 0x3d, 0x80,
  39. 0x3e, 0x82,
  40. 0x3f, 0x82,
  41. 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
  42. };
  43. static u8 channel_registers[] =
  44. {
  45. 0x01, 0xc4,
  46. 0x02, 0xa5,
  47. 0x03, 0x20,
  48. 0x04, 0xd0,
  49. 0x05, 0x20,
  50. 0x06, 0xd0,
  51. 0x07, 0x88,
  52. 0x08, 0x20,
  53. 0x09, 0x07,
  54. 0x0a, 0xf0,
  55. 0x0b, 0x07,
  56. 0x0c, 0xf0,
  57. 0x0d, 0x40,
  58. 0x0e, 0xd2,
  59. 0x0f, 0x80,
  60. 0x10, 0x80,
  61. 0x11, 0x80,
  62. 0x12, 0x80,
  63. 0x13, 0x1f,
  64. 0x14, 0x00,
  65. 0x15, 0x00,
  66. 0x16, 0x00,
  67. 0x17, 0x00,
  68. 0x18, 0xff,
  69. 0x19, 0xff,
  70. 0x1a, 0xff,
  71. 0x1b, 0xff,
  72. 0x1c, 0xff,
  73. 0x1d, 0xff,
  74. 0x1e, 0xff,
  75. 0x1f, 0xff,
  76. 0x20, 0x07,
  77. 0x21, 0x07,
  78. 0x22, 0x00,
  79. 0x23, 0x91,
  80. 0x24, 0x51,
  81. 0x25, 0x03,
  82. 0x26, 0x00,
  83. 0x27, 0x00,
  84. 0x28, 0x00,
  85. 0x29, 0x00,
  86. 0x2a, 0x00,
  87. 0x2b, 0x00,
  88. 0x2c, 0x00,
  89. 0x2d, 0x00,
  90. 0x2e, 0x00,
  91. 0x2f, 0x00,
  92. 0x30, 0x00,
  93. 0x31, 0x00,
  94. 0x32, 0x00,
  95. 0x33, 0x00,
  96. 0x34, 0x00,
  97. 0x35, 0x00,
  98. 0x36, 0x00,
  99. 0x37, 0x00,
  100. 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
  101. };
  102. static int write_reg(struct i2c_client *client, u8 reg, u8 value, int channel)
  103. {
  104. return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
  105. }
  106. static int write_regs(struct i2c_client *client, u8 *regs, int channel)
  107. {
  108. int i;
  109. for (i = 0; regs[i] != 0xff; i += 2)
  110. if (i2c_smbus_write_byte_data(client,
  111. regs[i] | (channel << 6), regs[i + 1]) < 0)
  112. return -1;
  113. return 0;
  114. }
  115. static int wis_tw2804_command(struct i2c_client *client,
  116. unsigned int cmd, void *arg)
  117. {
  118. struct wis_tw2804 *dec = i2c_get_clientdata(client);
  119. if (cmd == DECODER_SET_CHANNEL) {
  120. int *input = arg;
  121. if (*input < 0 || *input > 3) {
  122. printk(KERN_ERR "wis-tw2804: channel %d is not "
  123. "between 0 and 3!\n", *input);
  124. return 0;
  125. }
  126. dec->channel = *input;
  127. printk(KERN_DEBUG "wis-tw2804: initializing TW2804 "
  128. "channel %d\n", dec->channel);
  129. if (dec->channel == 0 &&
  130. write_regs(client, global_registers, 0) < 0) {
  131. printk(KERN_ERR "wis-tw2804: error initializing "
  132. "TW2804 global registers\n");
  133. return 0;
  134. }
  135. if (write_regs(client, channel_registers, dec->channel) < 0) {
  136. printk(KERN_ERR "wis-tw2804: error initializing "
  137. "TW2804 channel %d\n", dec->channel);
  138. return 0;
  139. }
  140. return 0;
  141. }
  142. if (dec->channel < 0) {
  143. printk(KERN_DEBUG "wis-tw2804: ignoring command %08x until "
  144. "channel number is set\n", cmd);
  145. return 0;
  146. }
  147. switch (cmd) {
  148. case VIDIOC_S_STD:
  149. {
  150. v4l2_std_id *input = arg;
  151. u8 regs[] = {
  152. 0x01, *input & V4L2_STD_NTSC ? 0xc4 : 0x84,
  153. 0x09, *input & V4L2_STD_NTSC ? 0x07 : 0x04,
  154. 0x0a, *input & V4L2_STD_NTSC ? 0xf0 : 0x20,
  155. 0x0b, *input & V4L2_STD_NTSC ? 0x07 : 0x04,
  156. 0x0c, *input & V4L2_STD_NTSC ? 0xf0 : 0x20,
  157. 0x0d, *input & V4L2_STD_NTSC ? 0x40 : 0x4a,
  158. 0x16, *input & V4L2_STD_NTSC ? 0x00 : 0x40,
  159. 0x17, *input & V4L2_STD_NTSC ? 0x00 : 0x40,
  160. 0x20, *input & V4L2_STD_NTSC ? 0x07 : 0x0f,
  161. 0x21, *input & V4L2_STD_NTSC ? 0x07 : 0x0f,
  162. 0xff, 0xff,
  163. };
  164. write_regs(client, regs, dec->channel);
  165. dec->norm = *input;
  166. break;
  167. }
  168. case VIDIOC_QUERYCTRL:
  169. {
  170. struct v4l2_queryctrl *ctrl = arg;
  171. switch (ctrl->id) {
  172. case V4L2_CID_BRIGHTNESS:
  173. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  174. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
  175. ctrl->minimum = 0;
  176. ctrl->maximum = 255;
  177. ctrl->step = 1;
  178. ctrl->default_value = 128;
  179. ctrl->flags = 0;
  180. break;
  181. case V4L2_CID_CONTRAST:
  182. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  183. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
  184. ctrl->minimum = 0;
  185. ctrl->maximum = 255;
  186. ctrl->step = 1;
  187. ctrl->default_value = 128;
  188. ctrl->flags = 0;
  189. break;
  190. case V4L2_CID_SATURATION:
  191. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  192. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
  193. ctrl->minimum = 0;
  194. ctrl->maximum = 255;
  195. ctrl->step = 1;
  196. ctrl->default_value = 128;
  197. ctrl->flags = 0;
  198. break;
  199. case V4L2_CID_HUE:
  200. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  201. strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
  202. ctrl->minimum = 0;
  203. ctrl->maximum = 255;
  204. ctrl->step = 1;
  205. ctrl->default_value = 128;
  206. ctrl->flags = 0;
  207. break;
  208. }
  209. break;
  210. }
  211. case VIDIOC_S_CTRL:
  212. {
  213. struct v4l2_control *ctrl = arg;
  214. switch (ctrl->id) {
  215. case V4L2_CID_BRIGHTNESS:
  216. if (ctrl->value > 255)
  217. dec->brightness = 255;
  218. else if (ctrl->value < 0)
  219. dec->brightness = 0;
  220. else
  221. dec->brightness = ctrl->value;
  222. write_reg(client, 0x12, dec->brightness, dec->channel);
  223. break;
  224. case V4L2_CID_CONTRAST:
  225. if (ctrl->value > 255)
  226. dec->contrast = 255;
  227. else if (ctrl->value < 0)
  228. dec->contrast = 0;
  229. else
  230. dec->contrast = ctrl->value;
  231. write_reg(client, 0x11, dec->contrast, dec->channel);
  232. break;
  233. case V4L2_CID_SATURATION:
  234. if (ctrl->value > 255)
  235. dec->saturation = 255;
  236. else if (ctrl->value < 0)
  237. dec->saturation = 0;
  238. else
  239. dec->saturation = ctrl->value;
  240. write_reg(client, 0x10, dec->saturation, dec->channel);
  241. break;
  242. case V4L2_CID_HUE:
  243. if (ctrl->value > 255)
  244. dec->hue = 255;
  245. else if (ctrl->value < 0)
  246. dec->hue = 0;
  247. else
  248. dec->hue = ctrl->value;
  249. write_reg(client, 0x0f, dec->hue, dec->channel);
  250. break;
  251. }
  252. break;
  253. }
  254. case VIDIOC_G_CTRL:
  255. {
  256. struct v4l2_control *ctrl = arg;
  257. switch (ctrl->id) {
  258. case V4L2_CID_BRIGHTNESS:
  259. ctrl->value = dec->brightness;
  260. break;
  261. case V4L2_CID_CONTRAST:
  262. ctrl->value = dec->contrast;
  263. break;
  264. case V4L2_CID_SATURATION:
  265. ctrl->value = dec->saturation;
  266. break;
  267. case V4L2_CID_HUE:
  268. ctrl->value = dec->hue;
  269. break;
  270. }
  271. break;
  272. }
  273. default:
  274. break;
  275. }
  276. return 0;
  277. }
  278. static int wis_tw2804_probe(struct i2c_client *client,
  279. const struct i2c_device_id *id)
  280. {
  281. struct i2c_adapter *adapter = client->adapter;
  282. struct wis_tw2804 *dec;
  283. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  284. return -ENODEV;
  285. dec = kmalloc(sizeof(struct wis_tw2804), GFP_KERNEL);
  286. if (dec == NULL)
  287. return -ENOMEM;
  288. dec->channel = -1;
  289. dec->norm = V4L2_STD_NTSC;
  290. dec->brightness = 128;
  291. dec->contrast = 128;
  292. dec->saturation = 128;
  293. dec->hue = 128;
  294. i2c_set_clientdata(client, dec);
  295. printk(KERN_DEBUG "wis-tw2804: creating TW2804 at address %d on %s\n",
  296. client->addr, adapter->name);
  297. return 0;
  298. }
  299. static int wis_tw2804_remove(struct i2c_client *client)
  300. {
  301. struct wis_tw2804 *dec = i2c_get_clientdata(client);
  302. kfree(dec);
  303. return 0;
  304. }
  305. static const struct i2c_device_id wis_tw2804_id[] = {
  306. { "wis_tw2804", 0 },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(i2c, wis_tw2804_id);
  310. static struct i2c_driver wis_tw2804_driver = {
  311. .driver = {
  312. .name = "WIS TW2804 I2C driver",
  313. },
  314. .probe = wis_tw2804_probe,
  315. .remove = wis_tw2804_remove,
  316. .command = wis_tw2804_command,
  317. .id_table = wis_tw2804_id,
  318. };
  319. static int __init wis_tw2804_init(void)
  320. {
  321. return i2c_add_driver(&wis_tw2804_driver);
  322. }
  323. static void __exit wis_tw2804_cleanup(void)
  324. {
  325. i2c_del_driver(&wis_tw2804_driver);
  326. }
  327. module_init(wis_tw2804_init);
  328. module_exit(wis_tw2804_cleanup);
  329. MODULE_LICENSE("GPL v2");