saa7110.c 12 KB

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