saa7110.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * saa7110 - Philips SAA7110(A) video decoder driver
  3. *
  4. * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
  5. *
  6. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  7. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  8. * - some corrections for Pinnacle Systems Inc. DC10plus card.
  9. *
  10. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  11. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/wait.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/i2c.h>
  35. #include <linux/videodev.h>
  36. #include <linux/video_decoder.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-i2c-drv-legacy.h>
  39. MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
  40. MODULE_AUTHOR("Pauline Middelink");
  41. MODULE_LICENSE("GPL");
  42. static int debug;
  43. module_param(debug, int, 0);
  44. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  45. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  46. #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
  47. #define SAA7110_NR_REG 0x35
  48. struct saa7110 {
  49. u8 reg[SAA7110_NR_REG];
  50. v4l2_std_id norm;
  51. int input;
  52. int enable;
  53. int bright;
  54. int contrast;
  55. int hue;
  56. int sat;
  57. wait_queue_head_t wq;
  58. };
  59. /* ----------------------------------------------------------------------- */
  60. /* I2C support functions */
  61. /* ----------------------------------------------------------------------- */
  62. static int saa7110_write(struct i2c_client *client, u8 reg, u8 value)
  63. {
  64. struct saa7110 *decoder = i2c_get_clientdata(client);
  65. decoder->reg[reg] = value;
  66. return i2c_smbus_write_byte_data(client, reg, value);
  67. }
  68. static int saa7110_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
  69. {
  70. int ret = -1;
  71. u8 reg = *data; /* first register to write to */
  72. /* Sanity check */
  73. if (reg + (len - 1) > SAA7110_NR_REG)
  74. return ret;
  75. /* the saa7110 has an autoincrement function, use it if
  76. * the adapter understands raw I2C */
  77. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  78. struct saa7110 *decoder = i2c_get_clientdata(client);
  79. ret = i2c_master_send(client, data, len);
  80. /* Cache the written data */
  81. memcpy(decoder->reg + reg, data + 1, len - 1);
  82. } else {
  83. for (++data, --len; len; len--) {
  84. ret = saa7110_write(client, reg++, *data++);
  85. if (ret < 0)
  86. break;
  87. }
  88. }
  89. return ret;
  90. }
  91. static inline int saa7110_read(struct i2c_client *client)
  92. {
  93. return i2c_smbus_read_byte(client);
  94. }
  95. /* ----------------------------------------------------------------------- */
  96. /* SAA7110 functions */
  97. /* ----------------------------------------------------------------------- */
  98. #define FRESP_06H_COMPST 0x03 //0x13
  99. #define FRESP_06H_SVIDEO 0x83 //0xC0
  100. static int saa7110_selmux(struct i2c_client *client, int chan)
  101. {
  102. static const unsigned char modes[9][8] = {
  103. /* mode 0 */
  104. {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
  105. 0x44, 0x75, 0x16},
  106. /* mode 1 */
  107. {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
  108. 0x44, 0x75, 0x16},
  109. /* mode 2 */
  110. {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
  111. 0x60, 0xB5, 0x05},
  112. /* mode 3 */
  113. {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
  114. 0x60, 0xB5, 0x05},
  115. /* mode 4 */
  116. {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
  117. 0x60, 0xB5, 0x03},
  118. /* mode 5 */
  119. {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
  120. 0x60, 0xB5, 0x03},
  121. /* mode 6 */
  122. {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
  123. 0x44, 0x75, 0x12},
  124. /* mode 7 */
  125. {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
  126. 0x60, 0xB5, 0x14},
  127. /* mode 8 */
  128. {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
  129. 0x44, 0x75, 0x21}
  130. };
  131. struct saa7110 *decoder = i2c_get_clientdata(client);
  132. const unsigned char *ptr = modes[chan];
  133. saa7110_write(client, 0x06, ptr[0]); /* Luminance control */
  134. saa7110_write(client, 0x20, ptr[1]); /* Analog Control #1 */
  135. saa7110_write(client, 0x21, ptr[2]); /* Analog Control #2 */
  136. saa7110_write(client, 0x22, ptr[3]); /* Mixer Control #1 */
  137. saa7110_write(client, 0x2C, ptr[4]); /* Mixer Control #2 */
  138. saa7110_write(client, 0x30, ptr[5]); /* ADCs gain control */
  139. saa7110_write(client, 0x31, ptr[6]); /* Mixer Control #3 */
  140. saa7110_write(client, 0x21, ptr[7]); /* Analog Control #2 */
  141. decoder->input = chan;
  142. return 0;
  143. }
  144. static const unsigned char initseq[1 + SAA7110_NR_REG] = {
  145. 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
  146. /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
  147. /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  148. /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  150. /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
  151. /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
  152. };
  153. static v4l2_std_id determine_norm(struct i2c_client *client)
  154. {
  155. DEFINE_WAIT(wait);
  156. struct saa7110 *decoder = i2c_get_clientdata(client);
  157. int status;
  158. /* mode changed, start automatic detection */
  159. saa7110_write_block(client, initseq, sizeof(initseq));
  160. saa7110_selmux(client, decoder->input);
  161. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  162. schedule_timeout(msecs_to_jiffies(250));
  163. finish_wait(&decoder->wq, &wait);
  164. status = saa7110_read(client);
  165. if (status & 0x40) {
  166. v4l_dbg(1, debug, client, "status=0x%02x (no signal)\n", status);
  167. return decoder->norm; // no change
  168. }
  169. if ((status & 3) == 0) {
  170. saa7110_write(client, 0x06, 0x83);
  171. if (status & 0x20) {
  172. v4l_dbg(1, debug, client, "status=0x%02x (NTSC/no color)\n", status);
  173. //saa7110_write(client,0x2E,0x81);
  174. return V4L2_STD_NTSC;
  175. }
  176. v4l_dbg(1, debug, client, "status=0x%02x (PAL/no color)\n", status);
  177. //saa7110_write(client,0x2E,0x9A);
  178. return V4L2_STD_PAL;
  179. }
  180. //saa7110_write(client,0x06,0x03);
  181. if (status & 0x20) { /* 60Hz */
  182. v4l_dbg(1, debug, client, "status=0x%02x (NTSC)\n", status);
  183. saa7110_write(client, 0x0D, 0x86);
  184. saa7110_write(client, 0x0F, 0x50);
  185. saa7110_write(client, 0x11, 0x2C);
  186. //saa7110_write(client,0x2E,0x81);
  187. return V4L2_STD_NTSC;
  188. }
  189. /* 50Hz -> PAL/SECAM */
  190. saa7110_write(client, 0x0D, 0x86);
  191. saa7110_write(client, 0x0F, 0x10);
  192. saa7110_write(client, 0x11, 0x59);
  193. //saa7110_write(client,0x2E,0x9A);
  194. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  195. schedule_timeout(msecs_to_jiffies(250));
  196. finish_wait(&decoder->wq, &wait);
  197. status = saa7110_read(client);
  198. if ((status & 0x03) == 0x01) {
  199. v4l_dbg(1, debug, client, "status=0x%02x (SECAM)\n", status);
  200. saa7110_write(client, 0x0D, 0x87);
  201. return V4L2_STD_SECAM;
  202. }
  203. v4l_dbg(1, debug, client, "status=0x%02x (PAL)\n", status);
  204. return V4L2_STD_PAL;
  205. }
  206. static int
  207. saa7110_command (struct i2c_client *client,
  208. unsigned int cmd,
  209. void *arg)
  210. {
  211. struct saa7110 *decoder = i2c_get_clientdata(client);
  212. struct v4l2_routing *route = arg;
  213. v4l2_std_id std;
  214. int v;
  215. switch (cmd) {
  216. case VIDIOC_INT_INIT:
  217. //saa7110_write_block(client, initseq, sizeof(initseq));
  218. break;
  219. case VIDIOC_INT_G_INPUT_STATUS:
  220. {
  221. int res = V4L2_IN_ST_NO_SIGNAL;
  222. int status;
  223. status = saa7110_read(client);
  224. v4l_dbg(1, debug, client, "status=0x%02x norm=%llx\n",
  225. status, decoder->norm);
  226. if (!(status & 0x40))
  227. res = 0;
  228. if (!(status & 0x03))
  229. res |= V4L2_IN_ST_NO_COLOR;
  230. *(int *) arg = res;
  231. break;
  232. }
  233. case VIDIOC_QUERYSTD:
  234. {
  235. *(v4l2_std_id *)arg = determine_norm(client);
  236. break;
  237. }
  238. case VIDIOC_S_STD:
  239. std = *(v4l2_std_id *) arg;
  240. if (decoder->norm != std) {
  241. decoder->norm = std;
  242. //saa7110_write(client, 0x06, 0x03);
  243. if (std & V4L2_STD_NTSC) {
  244. saa7110_write(client, 0x0D, 0x86);
  245. saa7110_write(client, 0x0F, 0x50);
  246. saa7110_write(client, 0x11, 0x2C);
  247. //saa7110_write(client, 0x2E, 0x81);
  248. v4l_dbg(1, debug, client, "switched to NTSC\n");
  249. } else if (std & V4L2_STD_PAL) {
  250. saa7110_write(client, 0x0D, 0x86);
  251. saa7110_write(client, 0x0F, 0x10);
  252. saa7110_write(client, 0x11, 0x59);
  253. //saa7110_write(client, 0x2E, 0x9A);
  254. v4l_dbg(1, debug, client, "switched to PAL\n");
  255. } else if (std & V4L2_STD_SECAM) {
  256. saa7110_write(client, 0x0D, 0x87);
  257. saa7110_write(client, 0x0F, 0x10);
  258. saa7110_write(client, 0x11, 0x59);
  259. //saa7110_write(client, 0x2E, 0x9A);
  260. v4l_dbg(1, debug, client, "switched to SECAM\n");
  261. } else {
  262. return -EINVAL;
  263. }
  264. }
  265. break;
  266. case VIDIOC_INT_S_VIDEO_ROUTING:
  267. if (route->input < 0 || route->input >= SAA7110_MAX_INPUT) {
  268. v4l_dbg(1, debug, client, "input=%d not available\n", route->input);
  269. return -EINVAL;
  270. }
  271. if (decoder->input != route->input) {
  272. saa7110_selmux(client, route->input);
  273. v4l_dbg(1, debug, client, "switched to input=%d\n", route->input);
  274. }
  275. break;
  276. case VIDIOC_STREAMON:
  277. case VIDIOC_STREAMOFF:
  278. v = cmd == VIDIOC_STREAMON;
  279. if (decoder->enable != v) {
  280. decoder->enable = v;
  281. saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
  282. v4l_dbg(1, debug, client, "YUV %s\n", v ? "on" : "off");
  283. }
  284. break;
  285. case VIDIOC_QUERYCTRL:
  286. {
  287. struct v4l2_queryctrl *qc = arg;
  288. switch (qc->id) {
  289. case V4L2_CID_BRIGHTNESS:
  290. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  291. case V4L2_CID_CONTRAST:
  292. case V4L2_CID_SATURATION:
  293. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  294. case V4L2_CID_HUE:
  295. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  296. default:
  297. return -EINVAL;
  298. }
  299. break;
  300. }
  301. case VIDIOC_G_CTRL:
  302. {
  303. struct v4l2_control *ctrl = arg;
  304. switch (ctrl->id) {
  305. case V4L2_CID_BRIGHTNESS:
  306. ctrl->value = decoder->bright;
  307. break;
  308. case V4L2_CID_CONTRAST:
  309. ctrl->value = decoder->contrast;
  310. break;
  311. case V4L2_CID_SATURATION:
  312. ctrl->value = decoder->sat;
  313. break;
  314. case V4L2_CID_HUE:
  315. ctrl->value = decoder->hue;
  316. break;
  317. default:
  318. return -EINVAL;
  319. }
  320. break;
  321. }
  322. case VIDIOC_S_CTRL:
  323. {
  324. struct v4l2_control *ctrl = arg;
  325. switch (ctrl->id) {
  326. case V4L2_CID_BRIGHTNESS:
  327. if (decoder->bright != ctrl->value) {
  328. decoder->bright = ctrl->value;
  329. saa7110_write(client, 0x19, decoder->bright);
  330. }
  331. break;
  332. case V4L2_CID_CONTRAST:
  333. if (decoder->contrast != ctrl->value) {
  334. decoder->contrast = ctrl->value;
  335. saa7110_write(client, 0x13, decoder->contrast);
  336. }
  337. break;
  338. case V4L2_CID_SATURATION:
  339. if (decoder->sat != ctrl->value) {
  340. decoder->sat = ctrl->value;
  341. saa7110_write(client, 0x12, decoder->sat);
  342. }
  343. break;
  344. case V4L2_CID_HUE:
  345. if (decoder->hue != ctrl->value) {
  346. decoder->hue = ctrl->value;
  347. saa7110_write(client, 0x07, decoder->hue);
  348. }
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. break;
  354. }
  355. default:
  356. v4l_dbg(1, debug, client, "unknown command %08x\n", cmd);
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. /* ----------------------------------------------------------------------- */
  362. /*
  363. * Generic i2c probe
  364. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  365. */
  366. static unsigned short normal_i2c[] = { 0x9c >> 1, 0x9e >> 1, I2C_CLIENT_END };
  367. I2C_CLIENT_INSMOD;
  368. static int saa7110_probe(struct i2c_client *client,
  369. const struct i2c_device_id *id)
  370. {
  371. struct saa7110 *decoder;
  372. int rv;
  373. /* Check if the adapter supports the needed features */
  374. if (!i2c_check_functionality(client->adapter,
  375. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  376. return -ENODEV;
  377. v4l_info(client, "chip found @ 0x%x (%s)\n",
  378. client->addr << 1, client->adapter->name);
  379. decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
  380. if (!decoder)
  381. return -ENOMEM;
  382. decoder->norm = V4L2_STD_PAL;
  383. decoder->input = 0;
  384. decoder->enable = 1;
  385. decoder->bright = 32768;
  386. decoder->contrast = 32768;
  387. decoder->hue = 32768;
  388. decoder->sat = 32768;
  389. init_waitqueue_head(&decoder->wq);
  390. i2c_set_clientdata(client, decoder);
  391. rv = saa7110_write_block(client, initseq, sizeof(initseq));
  392. if (rv < 0) {
  393. v4l_dbg(1, debug, client, "init status %d\n", rv);
  394. } else {
  395. int ver, status;
  396. saa7110_write(client, 0x21, 0x10);
  397. saa7110_write(client, 0x0e, 0x18);
  398. saa7110_write(client, 0x0D, 0x04);
  399. ver = saa7110_read(client);
  400. saa7110_write(client, 0x0D, 0x06);
  401. //mdelay(150);
  402. status = saa7110_read(client);
  403. v4l_dbg(1, debug, client, "version %x, status=0x%02x\n",
  404. ver, status);
  405. saa7110_write(client, 0x0D, 0x86);
  406. saa7110_write(client, 0x0F, 0x10);
  407. saa7110_write(client, 0x11, 0x59);
  408. //saa7110_write(client, 0x2E, 0x9A);
  409. }
  410. //saa7110_selmux(client,0);
  411. //determine_norm(client);
  412. /* setup and implicit mode 0 select has been performed */
  413. return 0;
  414. }
  415. static int saa7110_remove(struct i2c_client *client)
  416. {
  417. kfree(i2c_get_clientdata(client));
  418. return 0;
  419. }
  420. /* ----------------------------------------------------------------------- */
  421. static const struct i2c_device_id saa7110_id[] = {
  422. { "saa7110", 0 },
  423. { }
  424. };
  425. MODULE_DEVICE_TABLE(i2c, saa7110_id);
  426. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  427. .name = "saa7110",
  428. .driverid = I2C_DRIVERID_SAA7110,
  429. .command = saa7110_command,
  430. .probe = saa7110_probe,
  431. .remove = saa7110_remove,
  432. .id_table = saa7110_id,
  433. };