wis-saa7115.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_saa7115 {
  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, 0x20,
  36. 0x04, 0x80,
  37. 0x05, 0x80,
  38. 0x06, 0xeb,
  39. 0x07, 0xe0,
  40. 0x08, 0xf0, /* always toggle FID */
  41. 0x09, 0x40,
  42. 0x0a, 0x80,
  43. 0x0b, 0x40,
  44. 0x0c, 0x40,
  45. 0x0d, 0x00,
  46. 0x0e, 0x03,
  47. 0x0f, 0x2a,
  48. 0x10, 0x0e,
  49. 0x11, 0x00,
  50. 0x12, 0x8d,
  51. 0x13, 0x00,
  52. 0x14, 0x00,
  53. 0x15, 0x11,
  54. 0x16, 0x01,
  55. 0x17, 0xda,
  56. 0x18, 0x40,
  57. 0x19, 0x80,
  58. 0x1a, 0x00,
  59. 0x1b, 0x42,
  60. 0x1c, 0xa9,
  61. 0x30, 0x66,
  62. 0x31, 0x90,
  63. 0x32, 0x01,
  64. 0x34, 0x00,
  65. 0x35, 0x00,
  66. 0x36, 0x20,
  67. 0x38, 0x03,
  68. 0x39, 0x20,
  69. 0x3a, 0x88,
  70. 0x40, 0x00,
  71. 0x41, 0xff,
  72. 0x42, 0xff,
  73. 0x43, 0xff,
  74. 0x44, 0xff,
  75. 0x45, 0xff,
  76. 0x46, 0xff,
  77. 0x47, 0xff,
  78. 0x48, 0xff,
  79. 0x49, 0xff,
  80. 0x4a, 0xff,
  81. 0x4b, 0xff,
  82. 0x4c, 0xff,
  83. 0x4d, 0xff,
  84. 0x4e, 0xff,
  85. 0x4f, 0xff,
  86. 0x50, 0xff,
  87. 0x51, 0xff,
  88. 0x52, 0xff,
  89. 0x53, 0xff,
  90. 0x54, 0xf4 /*0xff*/,
  91. 0x55, 0xff,
  92. 0x56, 0xff,
  93. 0x57, 0xff,
  94. 0x58, 0x40,
  95. 0x59, 0x47,
  96. 0x5a, 0x06 /*0x03*/,
  97. 0x5b, 0x83,
  98. 0x5d, 0x06,
  99. 0x5e, 0x00,
  100. 0x80, 0x30, /* window defined scaler operation, task A and B enabled */
  101. 0x81, 0x03, /* use scaler datapath generated V */
  102. 0x83, 0x00,
  103. 0x84, 0x00,
  104. 0x85, 0x00,
  105. 0x86, 0x45,
  106. 0x87, 0x31,
  107. 0x88, 0xc0,
  108. 0x90, 0x02, /* task A process top field */
  109. 0x91, 0x08,
  110. 0x92, 0x09,
  111. 0x93, 0x80,
  112. 0x94, 0x06,
  113. 0x95, 0x00,
  114. 0x96, 0xc0,
  115. 0x97, 0x02,
  116. 0x98, 0x12,
  117. 0x99, 0x00,
  118. 0x9a, 0xf2,
  119. 0x9b, 0x00,
  120. 0x9c, 0xd0,
  121. 0x9d, 0x02,
  122. 0x9e, 0xf2,
  123. 0x9f, 0x00,
  124. 0xa0, 0x01,
  125. 0xa1, 0x01,
  126. 0xa2, 0x01,
  127. 0xa4, 0x80,
  128. 0xa5, 0x40,
  129. 0xa6, 0x40,
  130. 0xa8, 0x00,
  131. 0xa9, 0x04,
  132. 0xaa, 0x00,
  133. 0xac, 0x00,
  134. 0xad, 0x02,
  135. 0xae, 0x00,
  136. 0xb0, 0x00,
  137. 0xb1, 0x04,
  138. 0xb2, 0x00,
  139. 0xb3, 0x04,
  140. 0xb4, 0x00,
  141. 0xb8, 0x00,
  142. 0xbc, 0x00,
  143. 0xc0, 0x03, /* task B process bottom field */
  144. 0xc1, 0x08,
  145. 0xc2, 0x09,
  146. 0xc3, 0x80,
  147. 0xc4, 0x06,
  148. 0xc5, 0x00,
  149. 0xc6, 0xc0,
  150. 0xc7, 0x02,
  151. 0xc8, 0x12,
  152. 0xc9, 0x00,
  153. 0xca, 0xf2,
  154. 0xcb, 0x00,
  155. 0xcc, 0xd0,
  156. 0xcd, 0x02,
  157. 0xce, 0xf2,
  158. 0xcf, 0x00,
  159. 0xd0, 0x01,
  160. 0xd1, 0x01,
  161. 0xd2, 0x01,
  162. 0xd4, 0x80,
  163. 0xd5, 0x40,
  164. 0xd6, 0x40,
  165. 0xd8, 0x00,
  166. 0xd9, 0x04,
  167. 0xda, 0x00,
  168. 0xdc, 0x00,
  169. 0xdd, 0x02,
  170. 0xde, 0x00,
  171. 0xe0, 0x00,
  172. 0xe1, 0x04,
  173. 0xe2, 0x00,
  174. 0xe3, 0x04,
  175. 0xe4, 0x00,
  176. 0xe8, 0x00,
  177. 0x88, 0xf0, /* End of original static list */
  178. 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
  179. };
  180. static int write_reg(struct i2c_client *client, u8 reg, u8 value)
  181. {
  182. return i2c_smbus_write_byte_data(client, reg, value);
  183. }
  184. static int write_regs(struct i2c_client *client, u8 *regs)
  185. {
  186. int i;
  187. for (i = 0; regs[i] != 0x00; i += 2)
  188. if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
  189. return -1;
  190. return 0;
  191. }
  192. static int wis_saa7115_command(struct i2c_client *client,
  193. unsigned int cmd, void *arg)
  194. {
  195. struct wis_saa7115 *dec = i2c_get_clientdata(client);
  196. switch (cmd) {
  197. case VIDIOC_S_INPUT:
  198. {
  199. int *input = arg;
  200. i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
  201. i2c_smbus_write_byte_data(client, 0x09,
  202. *input < 6 ? 0x40 : 0xC0);
  203. break;
  204. }
  205. case DECODER_SET_RESOLUTION:
  206. {
  207. struct video_decoder_resolution *res = arg;
  208. /* Course-grained scaler */
  209. int h_integer_scaler = res->width < 704 ? 704 / res->width : 1;
  210. /* Fine-grained scaler to take care of remainder */
  211. int h_scaling_increment = (704 / h_integer_scaler) *
  212. 1024 / res->width;
  213. /* Fine-grained scaler only */
  214. int v_scaling_increment = (dec->norm & V4L2_STD_NTSC ?
  215. 240 : 288) * 1024 / res->height;
  216. u8 regs[] = {
  217. 0x88, 0xc0,
  218. 0x9c, res->width & 0xff,
  219. 0x9d, res->width >> 8,
  220. 0x9e, res->height & 0xff,
  221. 0x9f, res->height >> 8,
  222. 0xa0, h_integer_scaler,
  223. 0xa1, 1,
  224. 0xa2, 1,
  225. 0xa8, h_scaling_increment & 0xff,
  226. 0xa9, h_scaling_increment >> 8,
  227. 0xac, (h_scaling_increment / 2) & 0xff,
  228. 0xad, (h_scaling_increment / 2) >> 8,
  229. 0xb0, v_scaling_increment & 0xff,
  230. 0xb1, v_scaling_increment >> 8,
  231. 0xb2, v_scaling_increment & 0xff,
  232. 0xb3, v_scaling_increment >> 8,
  233. 0xcc, res->width & 0xff,
  234. 0xcd, res->width >> 8,
  235. 0xce, res->height & 0xff,
  236. 0xcf, res->height >> 8,
  237. 0xd0, h_integer_scaler,
  238. 0xd1, 1,
  239. 0xd2, 1,
  240. 0xd8, h_scaling_increment & 0xff,
  241. 0xd9, h_scaling_increment >> 8,
  242. 0xdc, (h_scaling_increment / 2) & 0xff,
  243. 0xdd, (h_scaling_increment / 2) >> 8,
  244. 0xe0, v_scaling_increment & 0xff,
  245. 0xe1, v_scaling_increment >> 8,
  246. 0xe2, v_scaling_increment & 0xff,
  247. 0xe3, v_scaling_increment >> 8,
  248. 0x88, 0xf0,
  249. 0, 0,
  250. };
  251. write_regs(client, regs);
  252. break;
  253. }
  254. case VIDIOC_S_STD:
  255. {
  256. v4l2_std_id *input = arg;
  257. u8 regs[] = {
  258. 0x88, 0xc0,
  259. 0x98, *input & V4L2_STD_NTSC ? 0x12 : 0x16,
  260. 0x9a, *input & V4L2_STD_NTSC ? 0xf2 : 0x20,
  261. 0x9b, *input & V4L2_STD_NTSC ? 0x00 : 0x01,
  262. 0xc8, *input & V4L2_STD_NTSC ? 0x12 : 0x16,
  263. 0xca, *input & V4L2_STD_NTSC ? 0xf2 : 0x20,
  264. 0xcb, *input & V4L2_STD_NTSC ? 0x00 : 0x01,
  265. 0x88, 0xf0,
  266. 0x30, *input & V4L2_STD_NTSC ? 0x66 : 0x00,
  267. 0x31, *input & V4L2_STD_NTSC ? 0x90 : 0xe0,
  268. 0, 0,
  269. };
  270. write_regs(client, regs);
  271. dec->norm = *input;
  272. break;
  273. }
  274. case VIDIOC_QUERYCTRL:
  275. {
  276. struct v4l2_queryctrl *ctrl = arg;
  277. switch (ctrl->id) {
  278. case V4L2_CID_BRIGHTNESS:
  279. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  280. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
  281. ctrl->minimum = 0;
  282. ctrl->maximum = 255;
  283. ctrl->step = 1;
  284. ctrl->default_value = 128;
  285. ctrl->flags = 0;
  286. break;
  287. case V4L2_CID_CONTRAST:
  288. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  289. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
  290. ctrl->minimum = 0;
  291. ctrl->maximum = 127;
  292. ctrl->step = 1;
  293. ctrl->default_value = 64;
  294. ctrl->flags = 0;
  295. break;
  296. case V4L2_CID_SATURATION:
  297. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  298. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
  299. ctrl->minimum = 0;
  300. ctrl->maximum = 127;
  301. ctrl->step = 1;
  302. ctrl->default_value = 64;
  303. ctrl->flags = 0;
  304. break;
  305. case V4L2_CID_HUE:
  306. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  307. strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
  308. ctrl->minimum = -128;
  309. ctrl->maximum = 127;
  310. ctrl->step = 1;
  311. ctrl->default_value = 0;
  312. ctrl->flags = 0;
  313. break;
  314. }
  315. break;
  316. }
  317. case VIDIOC_S_CTRL:
  318. {
  319. struct v4l2_control *ctrl = arg;
  320. switch (ctrl->id) {
  321. case V4L2_CID_BRIGHTNESS:
  322. if (ctrl->value > 255)
  323. dec->brightness = 255;
  324. else if (ctrl->value < 0)
  325. dec->brightness = 0;
  326. else
  327. dec->brightness = ctrl->value;
  328. write_reg(client, 0x0a, dec->brightness);
  329. break;
  330. case V4L2_CID_CONTRAST:
  331. if (ctrl->value > 127)
  332. dec->contrast = 127;
  333. else if (ctrl->value < 0)
  334. dec->contrast = 0;
  335. else
  336. dec->contrast = ctrl->value;
  337. write_reg(client, 0x0b, dec->contrast);
  338. break;
  339. case V4L2_CID_SATURATION:
  340. if (ctrl->value > 127)
  341. dec->saturation = 127;
  342. else if (ctrl->value < 0)
  343. dec->saturation = 0;
  344. else
  345. dec->saturation = ctrl->value;
  346. write_reg(client, 0x0c, dec->saturation);
  347. break;
  348. case V4L2_CID_HUE:
  349. if (ctrl->value > 127)
  350. dec->hue = 127;
  351. else if (ctrl->value < -128)
  352. dec->hue = -128;
  353. else
  354. dec->hue = ctrl->value;
  355. write_reg(client, 0x0d, dec->hue);
  356. break;
  357. }
  358. break;
  359. }
  360. case VIDIOC_G_CTRL:
  361. {
  362. struct v4l2_control *ctrl = arg;
  363. switch (ctrl->id) {
  364. case V4L2_CID_BRIGHTNESS:
  365. ctrl->value = dec->brightness;
  366. break;
  367. case V4L2_CID_CONTRAST:
  368. ctrl->value = dec->contrast;
  369. break;
  370. case V4L2_CID_SATURATION:
  371. ctrl->value = dec->saturation;
  372. break;
  373. case V4L2_CID_HUE:
  374. ctrl->value = dec->hue;
  375. break;
  376. }
  377. break;
  378. }
  379. default:
  380. break;
  381. }
  382. return 0;
  383. }
  384. static int wis_saa7115_probe(struct i2c_client *client,
  385. const struct i2c_device_id *id)
  386. {
  387. struct i2c_adapter *adapter = client->adapter;
  388. struct wis_saa7115 *dec;
  389. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  390. return -ENODEV;
  391. dec = kmalloc(sizeof(struct wis_saa7115), GFP_KERNEL);
  392. if (dec == NULL)
  393. return -ENOMEM;
  394. dec->norm = V4L2_STD_NTSC;
  395. dec->brightness = 128;
  396. dec->contrast = 64;
  397. dec->saturation = 64;
  398. dec->hue = 0;
  399. i2c_set_clientdata(client, dec);
  400. printk(KERN_DEBUG
  401. "wis-saa7115: initializing SAA7115 at address %d on %s\n",
  402. client->addr, adapter->name);
  403. if (write_regs(client, initial_registers) < 0) {
  404. printk(KERN_ERR
  405. "wis-saa7115: error initializing SAA7115\n");
  406. kfree(dec);
  407. return -ENODEV;
  408. }
  409. return 0;
  410. }
  411. static int wis_saa7115_remove(struct i2c_client *client)
  412. {
  413. struct wis_saa7115 *dec = i2c_get_clientdata(client);
  414. kfree(dec);
  415. return 0;
  416. }
  417. static const struct i2c_device_id wis_saa7115_id[] = {
  418. { "wis_saa7115", 0 },
  419. { }
  420. };
  421. MODULE_DEVICE_TABLE(i2c, wis_saa7115_id);
  422. static struct i2c_driver wis_saa7115_driver = {
  423. .driver = {
  424. .name = "WIS SAA7115 I2C driver",
  425. },
  426. .probe = wis_saa7115_probe,
  427. .remove = wis_saa7115_remove,
  428. .command = wis_saa7115_command,
  429. .id_table = wis_saa7115_id,
  430. };
  431. static int __init wis_saa7115_init(void)
  432. {
  433. return i2c_add_driver(&wis_saa7115_driver);
  434. }
  435. static void __exit wis_saa7115_cleanup(void)
  436. {
  437. i2c_del_driver(&wis_saa7115_driver);
  438. }
  439. module_init(wis_saa7115_init);
  440. module_exit(wis_saa7115_cleanup);
  441. MODULE_LICENSE("GPL v2");