saa7110.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/wait.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/i2c.h>
  31. #include <linux/videodev2.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ctrls.h>
  34. MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
  35. MODULE_AUTHOR("Pauline Middelink");
  36. MODULE_LICENSE("GPL");
  37. static int debug;
  38. module_param(debug, int, 0);
  39. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  40. #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
  41. #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
  42. #define SAA7110_NR_REG 0x35
  43. struct saa7110 {
  44. struct v4l2_subdev sd;
  45. struct v4l2_ctrl_handler hdl;
  46. u8 reg[SAA7110_NR_REG];
  47. v4l2_std_id norm;
  48. int input;
  49. int enable;
  50. wait_queue_head_t wq;
  51. };
  52. static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
  53. {
  54. return container_of(sd, struct saa7110, sd);
  55. }
  56. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  57. {
  58. return &container_of(ctrl->handler, struct saa7110, hdl)->sd;
  59. }
  60. /* ----------------------------------------------------------------------- */
  61. /* I2C support functions */
  62. /* ----------------------------------------------------------------------- */
  63. static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  64. {
  65. struct i2c_client *client = v4l2_get_subdevdata(sd);
  66. struct saa7110 *decoder = to_saa7110(sd);
  67. decoder->reg[reg] = value;
  68. return i2c_smbus_write_byte_data(client, reg, value);
  69. }
  70. static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
  71. {
  72. struct i2c_client *client = v4l2_get_subdevdata(sd);
  73. struct saa7110 *decoder = to_saa7110(sd);
  74. int ret = -1;
  75. u8 reg = *data; /* first register to write to */
  76. /* Sanity check */
  77. if (reg + (len - 1) > SAA7110_NR_REG)
  78. return ret;
  79. /* the saa7110 has an autoincrement function, use it if
  80. * the adapter understands raw I2C */
  81. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  82. ret = i2c_master_send(client, data, len);
  83. /* Cache the written data */
  84. memcpy(decoder->reg + reg, data + 1, len - 1);
  85. } else {
  86. for (++data, --len; len; len--) {
  87. ret = saa7110_write(sd, reg++, *data++);
  88. if (ret < 0)
  89. break;
  90. }
  91. }
  92. return ret;
  93. }
  94. static inline int saa7110_read(struct v4l2_subdev *sd)
  95. {
  96. struct i2c_client *client = v4l2_get_subdevdata(sd);
  97. return i2c_smbus_read_byte(client);
  98. }
  99. /* ----------------------------------------------------------------------- */
  100. /* SAA7110 functions */
  101. /* ----------------------------------------------------------------------- */
  102. #define FRESP_06H_COMPST 0x03 /*0x13*/
  103. #define FRESP_06H_SVIDEO 0x83 /*0xC0*/
  104. static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
  105. {
  106. static const unsigned char modes[9][8] = {
  107. /* mode 0 */
  108. {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
  109. 0x44, 0x75, 0x16},
  110. /* mode 1 */
  111. {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
  112. 0x44, 0x75, 0x16},
  113. /* mode 2 */
  114. {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
  115. 0x60, 0xB5, 0x05},
  116. /* mode 3 */
  117. {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
  118. 0x60, 0xB5, 0x05},
  119. /* mode 4 */
  120. {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
  121. 0x60, 0xB5, 0x03},
  122. /* mode 5 */
  123. {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
  124. 0x60, 0xB5, 0x03},
  125. /* mode 6 */
  126. {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
  127. 0x44, 0x75, 0x12},
  128. /* mode 7 */
  129. {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
  130. 0x60, 0xB5, 0x14},
  131. /* mode 8 */
  132. {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
  133. 0x44, 0x75, 0x21}
  134. };
  135. struct saa7110 *decoder = to_saa7110(sd);
  136. const unsigned char *ptr = modes[chan];
  137. saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */
  138. saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */
  139. saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */
  140. saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */
  141. saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */
  142. saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */
  143. saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */
  144. saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */
  145. decoder->input = chan;
  146. return 0;
  147. }
  148. static const unsigned char initseq[1 + SAA7110_NR_REG] = {
  149. 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
  150. /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
  151. /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
  152. /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
  154. /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
  155. /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
  156. };
  157. static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
  158. {
  159. DEFINE_WAIT(wait);
  160. struct saa7110 *decoder = to_saa7110(sd);
  161. int status;
  162. /* mode changed, start automatic detection */
  163. saa7110_write_block(sd, initseq, sizeof(initseq));
  164. saa7110_selmux(sd, decoder->input);
  165. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  166. schedule_timeout(msecs_to_jiffies(250));
  167. finish_wait(&decoder->wq, &wait);
  168. status = saa7110_read(sd);
  169. if (status & 0x40) {
  170. v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
  171. return V4L2_STD_UNKNOWN;
  172. }
  173. if ((status & 3) == 0) {
  174. saa7110_write(sd, 0x06, 0x83);
  175. if (status & 0x20) {
  176. v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status);
  177. /*saa7110_write(sd,0x2E,0x81);*/
  178. return V4L2_STD_NTSC;
  179. }
  180. v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
  181. /*saa7110_write(sd,0x2E,0x9A);*/
  182. return V4L2_STD_PAL;
  183. }
  184. /*saa7110_write(sd,0x06,0x03);*/
  185. if (status & 0x20) { /* 60Hz */
  186. v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status);
  187. saa7110_write(sd, 0x0D, 0x86);
  188. saa7110_write(sd, 0x0F, 0x50);
  189. saa7110_write(sd, 0x11, 0x2C);
  190. /*saa7110_write(sd,0x2E,0x81);*/
  191. return V4L2_STD_NTSC;
  192. }
  193. /* 50Hz -> PAL/SECAM */
  194. saa7110_write(sd, 0x0D, 0x86);
  195. saa7110_write(sd, 0x0F, 0x10);
  196. saa7110_write(sd, 0x11, 0x59);
  197. /*saa7110_write(sd,0x2E,0x9A);*/
  198. prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
  199. schedule_timeout(msecs_to_jiffies(250));
  200. finish_wait(&decoder->wq, &wait);
  201. status = saa7110_read(sd);
  202. if ((status & 0x03) == 0x01) {
  203. v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status);
  204. saa7110_write(sd, 0x0D, 0x87);
  205. return V4L2_STD_SECAM;
  206. }
  207. v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
  208. return V4L2_STD_PAL;
  209. }
  210. static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
  211. {
  212. struct saa7110 *decoder = to_saa7110(sd);
  213. int res = V4L2_IN_ST_NO_SIGNAL;
  214. int status = saa7110_read(sd);
  215. v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
  216. status, (unsigned long long)decoder->norm);
  217. if (!(status & 0x40))
  218. res = 0;
  219. if (!(status & 0x03))
  220. res |= V4L2_IN_ST_NO_COLOR;
  221. *pstatus = res;
  222. return 0;
  223. }
  224. static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  225. {
  226. *std &= determine_norm(sd);
  227. return 0;
  228. }
  229. static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  230. {
  231. struct saa7110 *decoder = to_saa7110(sd);
  232. if (decoder->norm != std) {
  233. decoder->norm = std;
  234. /*saa7110_write(sd, 0x06, 0x03);*/
  235. if (std & V4L2_STD_NTSC) {
  236. saa7110_write(sd, 0x0D, 0x86);
  237. saa7110_write(sd, 0x0F, 0x50);
  238. saa7110_write(sd, 0x11, 0x2C);
  239. /*saa7110_write(sd, 0x2E, 0x81);*/
  240. v4l2_dbg(1, debug, sd, "switched to NTSC\n");
  241. } else if (std & V4L2_STD_PAL) {
  242. saa7110_write(sd, 0x0D, 0x86);
  243. saa7110_write(sd, 0x0F, 0x10);
  244. saa7110_write(sd, 0x11, 0x59);
  245. /*saa7110_write(sd, 0x2E, 0x9A);*/
  246. v4l2_dbg(1, debug, sd, "switched to PAL\n");
  247. } else if (std & V4L2_STD_SECAM) {
  248. saa7110_write(sd, 0x0D, 0x87);
  249. saa7110_write(sd, 0x0F, 0x10);
  250. saa7110_write(sd, 0x11, 0x59);
  251. /*saa7110_write(sd, 0x2E, 0x9A);*/
  252. v4l2_dbg(1, debug, sd, "switched to SECAM\n");
  253. } else {
  254. return -EINVAL;
  255. }
  256. }
  257. return 0;
  258. }
  259. static int saa7110_s_routing(struct v4l2_subdev *sd,
  260. u32 input, u32 output, u32 config)
  261. {
  262. struct saa7110 *decoder = to_saa7110(sd);
  263. if (input >= SAA7110_MAX_INPUT) {
  264. v4l2_dbg(1, debug, sd, "input=%d not available\n", input);
  265. return -EINVAL;
  266. }
  267. if (decoder->input != input) {
  268. saa7110_selmux(sd, input);
  269. v4l2_dbg(1, debug, sd, "switched to input=%d\n", input);
  270. }
  271. return 0;
  272. }
  273. static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
  274. {
  275. struct saa7110 *decoder = to_saa7110(sd);
  276. if (decoder->enable != enable) {
  277. decoder->enable = enable;
  278. saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80);
  279. v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off");
  280. }
  281. return 0;
  282. }
  283. static int saa7110_s_ctrl(struct v4l2_ctrl *ctrl)
  284. {
  285. struct v4l2_subdev *sd = to_sd(ctrl);
  286. switch (ctrl->id) {
  287. case V4L2_CID_BRIGHTNESS:
  288. saa7110_write(sd, 0x19, ctrl->val);
  289. break;
  290. case V4L2_CID_CONTRAST:
  291. saa7110_write(sd, 0x13, ctrl->val);
  292. break;
  293. case V4L2_CID_SATURATION:
  294. saa7110_write(sd, 0x12, ctrl->val);
  295. break;
  296. case V4L2_CID_HUE:
  297. saa7110_write(sd, 0x07, ctrl->val);
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. return 0;
  303. }
  304. /* ----------------------------------------------------------------------- */
  305. static const struct v4l2_ctrl_ops saa7110_ctrl_ops = {
  306. .s_ctrl = saa7110_s_ctrl,
  307. };
  308. static const struct v4l2_subdev_video_ops saa7110_video_ops = {
  309. .s_std = saa7110_s_std,
  310. .s_routing = saa7110_s_routing,
  311. .s_stream = saa7110_s_stream,
  312. .querystd = saa7110_querystd,
  313. .g_input_status = saa7110_g_input_status,
  314. };
  315. static const struct v4l2_subdev_ops saa7110_ops = {
  316. .video = &saa7110_video_ops,
  317. };
  318. /* ----------------------------------------------------------------------- */
  319. static int saa7110_probe(struct i2c_client *client,
  320. const struct i2c_device_id *id)
  321. {
  322. struct saa7110 *decoder;
  323. struct v4l2_subdev *sd;
  324. int rv;
  325. /* Check if the adapter supports the needed features */
  326. if (!i2c_check_functionality(client->adapter,
  327. I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  328. return -ENODEV;
  329. v4l_info(client, "chip found @ 0x%x (%s)\n",
  330. client->addr << 1, client->adapter->name);
  331. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  332. if (!decoder)
  333. return -ENOMEM;
  334. sd = &decoder->sd;
  335. v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
  336. decoder->norm = V4L2_STD_PAL;
  337. decoder->input = 0;
  338. decoder->enable = 1;
  339. v4l2_ctrl_handler_init(&decoder->hdl, 2);
  340. v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops,
  341. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  342. v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops,
  343. V4L2_CID_CONTRAST, 0, 127, 1, 64);
  344. v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops,
  345. V4L2_CID_SATURATION, 0, 127, 1, 64);
  346. v4l2_ctrl_new_std(&decoder->hdl, &saa7110_ctrl_ops,
  347. V4L2_CID_HUE, -128, 127, 1, 0);
  348. sd->ctrl_handler = &decoder->hdl;
  349. if (decoder->hdl.error) {
  350. int err = decoder->hdl.error;
  351. v4l2_ctrl_handler_free(&decoder->hdl);
  352. return err;
  353. }
  354. v4l2_ctrl_handler_setup(&decoder->hdl);
  355. init_waitqueue_head(&decoder->wq);
  356. rv = saa7110_write_block(sd, initseq, sizeof(initseq));
  357. if (rv < 0) {
  358. v4l2_dbg(1, debug, sd, "init status %d\n", rv);
  359. } else {
  360. int ver, status;
  361. saa7110_write(sd, 0x21, 0x10);
  362. saa7110_write(sd, 0x0e, 0x18);
  363. saa7110_write(sd, 0x0D, 0x04);
  364. ver = saa7110_read(sd);
  365. saa7110_write(sd, 0x0D, 0x06);
  366. /*mdelay(150);*/
  367. status = saa7110_read(sd);
  368. v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
  369. ver, status);
  370. saa7110_write(sd, 0x0D, 0x86);
  371. saa7110_write(sd, 0x0F, 0x10);
  372. saa7110_write(sd, 0x11, 0x59);
  373. /*saa7110_write(sd, 0x2E, 0x9A);*/
  374. }
  375. /*saa7110_selmux(sd,0);*/
  376. /*determine_norm(sd);*/
  377. /* setup and implicit mode 0 select has been performed */
  378. return 0;
  379. }
  380. static int saa7110_remove(struct i2c_client *client)
  381. {
  382. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  383. struct saa7110 *decoder = to_saa7110(sd);
  384. v4l2_device_unregister_subdev(sd);
  385. v4l2_ctrl_handler_free(&decoder->hdl);
  386. return 0;
  387. }
  388. /* ----------------------------------------------------------------------- */
  389. static const struct i2c_device_id saa7110_id[] = {
  390. { "saa7110", 0 },
  391. { }
  392. };
  393. MODULE_DEVICE_TABLE(i2c, saa7110_id);
  394. static struct i2c_driver saa7110_driver = {
  395. .driver = {
  396. .name = "saa7110",
  397. },
  398. .probe = saa7110_probe,
  399. .remove = saa7110_remove,
  400. .id_table = saa7110_id,
  401. };
  402. module_i2c_driver(saa7110_driver);