saa7110.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/videodev2.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-chip-ident.h>
  38. MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
  39. MODULE_AUTHOR("Pauline Middelink");
  40. MODULE_LICENSE("GPL");
  41. static int debug;
  42. module_param(debug, int, 0);
  43. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  44. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  45. #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
  46. #define SAA7110_NR_REG 0x35
  47. struct saa7110 {
  48. struct v4l2_subdev sd;
  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. static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
  60. {
  61. return container_of(sd, struct saa7110, sd);
  62. }
  63. /* ----------------------------------------------------------------------- */
  64. /* I2C support functions */
  65. /* ----------------------------------------------------------------------- */
  66. static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  67. {
  68. struct i2c_client *client = v4l2_get_subdevdata(sd);
  69. struct saa7110 *decoder = to_saa7110(sd);
  70. decoder->reg[reg] = value;
  71. return i2c_smbus_write_byte_data(client, reg, value);
  72. }
  73. static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
  74. {
  75. struct i2c_client *client = v4l2_get_subdevdata(sd);
  76. struct saa7110 *decoder = to_saa7110(sd);
  77. int ret = -1;
  78. u8 reg = *data; /* first register to write to */
  79. /* Sanity check */
  80. if (reg + (len - 1) > SAA7110_NR_REG)
  81. return ret;
  82. /* the saa7110 has an autoincrement function, use it if
  83. * the adapter understands raw I2C */
  84. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  85. ret = i2c_master_send(client, data, len);
  86. /* Cache the written data */
  87. memcpy(decoder->reg + reg, data + 1, len - 1);
  88. } else {
  89. for (++data, --len; len; len--) {
  90. ret = saa7110_write(sd, reg++, *data++);
  91. if (ret < 0)
  92. break;
  93. }
  94. }
  95. return ret;
  96. }
  97. static inline int saa7110_read(struct v4l2_subdev *sd)
  98. {
  99. struct i2c_client *client = v4l2_get_subdevdata(sd);
  100. return i2c_smbus_read_byte(client);
  101. }
  102. /* ----------------------------------------------------------------------- */
  103. /* SAA7110 functions */
  104. /* ----------------------------------------------------------------------- */
  105. #define FRESP_06H_COMPST 0x03 /*0x13*/
  106. #define FRESP_06H_SVIDEO 0x83 /*0xC0*/
  107. static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
  108. {
  109. static const unsigned char modes[9][8] = {
  110. /* mode 0 */
  111. {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
  112. 0x44, 0x75, 0x16},
  113. /* mode 1 */
  114. {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
  115. 0x44, 0x75, 0x16},
  116. /* mode 2 */
  117. {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
  118. 0x60, 0xB5, 0x05},
  119. /* mode 3 */
  120. {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
  121. 0x60, 0xB5, 0x05},
  122. /* mode 4 */
  123. {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
  124. 0x60, 0xB5, 0x03},
  125. /* mode 5 */
  126. {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
  127. 0x60, 0xB5, 0x03},
  128. /* mode 6 */
  129. {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
  130. 0x44, 0x75, 0x12},
  131. /* mode 7 */
  132. {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
  133. 0x60, 0xB5, 0x14},
  134. /* mode 8 */
  135. {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
  136. 0x44, 0x75, 0x21}
  137. };
  138. struct saa7110 *decoder = to_saa7110(sd);
  139. const unsigned char *ptr = modes[chan];
  140. saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */
  141. saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */
  142. saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */
  143. saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */
  144. saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */
  145. saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */
  146. saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */
  147. saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */
  148. decoder->input = chan;
  149. return 0;
  150. }
  151. static const unsigned char initseq[1 + SAA7110_NR_REG] = {
  152. 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
  153. /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
  154. /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  155. /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  156. /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  157. /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
  158. /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
  159. };
  160. static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
  161. {
  162. DEFINE_WAIT(wait);
  163. struct saa7110 *decoder = to_saa7110(sd);
  164. int status;
  165. /* mode changed, start automatic detection */
  166. saa7110_write_block(sd, initseq, sizeof(initseq));
  167. saa7110_selmux(sd, decoder->input);
  168. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  169. schedule_timeout(msecs_to_jiffies(250));
  170. finish_wait(&decoder->wq, &wait);
  171. status = saa7110_read(sd);
  172. if (status & 0x40) {
  173. v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
  174. return decoder->norm; /* no change*/
  175. }
  176. if ((status & 3) == 0) {
  177. saa7110_write(sd, 0x06, 0x83);
  178. if (status & 0x20) {
  179. v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status);
  180. /*saa7110_write(sd,0x2E,0x81);*/
  181. return V4L2_STD_NTSC;
  182. }
  183. v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
  184. /*saa7110_write(sd,0x2E,0x9A);*/
  185. return V4L2_STD_PAL;
  186. }
  187. /*saa7110_write(sd,0x06,0x03);*/
  188. if (status & 0x20) { /* 60Hz */
  189. v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status);
  190. saa7110_write(sd, 0x0D, 0x86);
  191. saa7110_write(sd, 0x0F, 0x50);
  192. saa7110_write(sd, 0x11, 0x2C);
  193. /*saa7110_write(sd,0x2E,0x81);*/
  194. return V4L2_STD_NTSC;
  195. }
  196. /* 50Hz -> PAL/SECAM */
  197. saa7110_write(sd, 0x0D, 0x86);
  198. saa7110_write(sd, 0x0F, 0x10);
  199. saa7110_write(sd, 0x11, 0x59);
  200. /*saa7110_write(sd,0x2E,0x9A);*/
  201. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  202. schedule_timeout(msecs_to_jiffies(250));
  203. finish_wait(&decoder->wq, &wait);
  204. status = saa7110_read(sd);
  205. if ((status & 0x03) == 0x01) {
  206. v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status);
  207. saa7110_write(sd, 0x0D, 0x87);
  208. return V4L2_STD_SECAM;
  209. }
  210. v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
  211. return V4L2_STD_PAL;
  212. }
  213. static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
  214. {
  215. struct saa7110 *decoder = to_saa7110(sd);
  216. int res = V4L2_IN_ST_NO_SIGNAL;
  217. int status = saa7110_read(sd);
  218. v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
  219. status, (unsigned long long)decoder->norm);
  220. if (!(status & 0x40))
  221. res = 0;
  222. if (!(status & 0x03))
  223. res |= V4L2_IN_ST_NO_COLOR;
  224. *pstatus = res;
  225. return 0;
  226. }
  227. static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  228. {
  229. *(v4l2_std_id *)std = determine_norm(sd);
  230. return 0;
  231. }
  232. static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  233. {
  234. struct saa7110 *decoder = to_saa7110(sd);
  235. if (decoder->norm != std) {
  236. decoder->norm = std;
  237. /*saa7110_write(sd, 0x06, 0x03);*/
  238. if (std & V4L2_STD_NTSC) {
  239. saa7110_write(sd, 0x0D, 0x86);
  240. saa7110_write(sd, 0x0F, 0x50);
  241. saa7110_write(sd, 0x11, 0x2C);
  242. /*saa7110_write(sd, 0x2E, 0x81);*/
  243. v4l2_dbg(1, debug, sd, "switched to NTSC\n");
  244. } else if (std & V4L2_STD_PAL) {
  245. saa7110_write(sd, 0x0D, 0x86);
  246. saa7110_write(sd, 0x0F, 0x10);
  247. saa7110_write(sd, 0x11, 0x59);
  248. /*saa7110_write(sd, 0x2E, 0x9A);*/
  249. v4l2_dbg(1, debug, sd, "switched to PAL\n");
  250. } else if (std & V4L2_STD_SECAM) {
  251. saa7110_write(sd, 0x0D, 0x87);
  252. saa7110_write(sd, 0x0F, 0x10);
  253. saa7110_write(sd, 0x11, 0x59);
  254. /*saa7110_write(sd, 0x2E, 0x9A);*/
  255. v4l2_dbg(1, debug, sd, "switched to SECAM\n");
  256. } else {
  257. return -EINVAL;
  258. }
  259. }
  260. return 0;
  261. }
  262. static int saa7110_s_routing(struct v4l2_subdev *sd,
  263. u32 input, u32 output, u32 config)
  264. {
  265. struct saa7110 *decoder = to_saa7110(sd);
  266. if (input >= SAA7110_MAX_INPUT) {
  267. v4l2_dbg(1, debug, sd, "input=%d not available\n", input);
  268. return -EINVAL;
  269. }
  270. if (decoder->input != input) {
  271. saa7110_selmux(sd, input);
  272. v4l2_dbg(1, debug, sd, "switched to input=%d\n", input);
  273. }
  274. return 0;
  275. }
  276. static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
  277. {
  278. struct saa7110 *decoder = to_saa7110(sd);
  279. if (decoder->enable != enable) {
  280. decoder->enable = enable;
  281. saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80);
  282. v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off");
  283. }
  284. return 0;
  285. }
  286. static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  287. {
  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. return 0;
  300. }
  301. static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  302. {
  303. struct saa7110 *decoder = to_saa7110(sd);
  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. return 0;
  321. }
  322. static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  323. {
  324. struct saa7110 *decoder = to_saa7110(sd);
  325. switch (ctrl->id) {
  326. case V4L2_CID_BRIGHTNESS:
  327. if (decoder->bright != ctrl->value) {
  328. decoder->bright = ctrl->value;
  329. saa7110_write(sd, 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(sd, 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(sd, 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(sd, 0x07, decoder->hue);
  348. }
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. return 0;
  354. }
  355. static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  356. {
  357. struct i2c_client *client = v4l2_get_subdevdata(sd);
  358. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0);
  359. }
  360. /* ----------------------------------------------------------------------- */
  361. static const struct v4l2_subdev_core_ops saa7110_core_ops = {
  362. .g_chip_ident = saa7110_g_chip_ident,
  363. .g_ctrl = saa7110_g_ctrl,
  364. .s_ctrl = saa7110_s_ctrl,
  365. .queryctrl = saa7110_queryctrl,
  366. .s_std = saa7110_s_std,
  367. };
  368. static const struct v4l2_subdev_video_ops saa7110_video_ops = {
  369. .s_routing = saa7110_s_routing,
  370. .s_stream = saa7110_s_stream,
  371. .querystd = saa7110_querystd,
  372. .g_input_status = saa7110_g_input_status,
  373. };
  374. static const struct v4l2_subdev_ops saa7110_ops = {
  375. .core = &saa7110_core_ops,
  376. .video = &saa7110_video_ops,
  377. };
  378. /* ----------------------------------------------------------------------- */
  379. static int saa7110_probe(struct i2c_client *client,
  380. const struct i2c_device_id *id)
  381. {
  382. struct saa7110 *decoder;
  383. struct v4l2_subdev *sd;
  384. int rv;
  385. /* Check if the adapter supports the needed features */
  386. if (!i2c_check_functionality(client->adapter,
  387. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  388. return -ENODEV;
  389. v4l_info(client, "chip found @ 0x%x (%s)\n",
  390. client->addr << 1, client->adapter->name);
  391. decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
  392. if (!decoder)
  393. return -ENOMEM;
  394. sd = &decoder->sd;
  395. v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
  396. decoder->norm = V4L2_STD_PAL;
  397. decoder->input = 0;
  398. decoder->enable = 1;
  399. decoder->bright = 32768;
  400. decoder->contrast = 32768;
  401. decoder->hue = 32768;
  402. decoder->sat = 32768;
  403. init_waitqueue_head(&decoder->wq);
  404. rv = saa7110_write_block(sd, initseq, sizeof(initseq));
  405. if (rv < 0) {
  406. v4l2_dbg(1, debug, sd, "init status %d\n", rv);
  407. } else {
  408. int ver, status;
  409. saa7110_write(sd, 0x21, 0x10);
  410. saa7110_write(sd, 0x0e, 0x18);
  411. saa7110_write(sd, 0x0D, 0x04);
  412. ver = saa7110_read(sd);
  413. saa7110_write(sd, 0x0D, 0x06);
  414. /*mdelay(150);*/
  415. status = saa7110_read(sd);
  416. v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
  417. ver, status);
  418. saa7110_write(sd, 0x0D, 0x86);
  419. saa7110_write(sd, 0x0F, 0x10);
  420. saa7110_write(sd, 0x11, 0x59);
  421. /*saa7110_write(sd, 0x2E, 0x9A);*/
  422. }
  423. /*saa7110_selmux(sd,0);*/
  424. /*determine_norm(sd);*/
  425. /* setup and implicit mode 0 select has been performed */
  426. return 0;
  427. }
  428. static int saa7110_remove(struct i2c_client *client)
  429. {
  430. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  431. v4l2_device_unregister_subdev(sd);
  432. kfree(to_saa7110(sd));
  433. return 0;
  434. }
  435. /* ----------------------------------------------------------------------- */
  436. static const struct i2c_device_id saa7110_id[] = {
  437. { "saa7110", 0 },
  438. { }
  439. };
  440. MODULE_DEVICE_TABLE(i2c, saa7110_id);
  441. static struct i2c_driver saa7110_driver = {
  442. .driver = {
  443. .owner = THIS_MODULE,
  444. .name = "saa7110",
  445. },
  446. .probe = saa7110_probe,
  447. .remove = saa7110_remove,
  448. .id_table = saa7110_id,
  449. };
  450. static __init int init_saa7110(void)
  451. {
  452. return i2c_add_driver(&saa7110_driver);
  453. }
  454. static __exit void exit_saa7110(void)
  455. {
  456. i2c_del_driver(&saa7110_driver);
  457. }
  458. module_init(init_saa7110);
  459. module_exit(exit_saa7110);