saa7110.c 13 KB

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