saa7185.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * saa7185 - Philips SAA7185B video encoder driver version 0.0.3
  3. *
  4. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  5. *
  6. * Slight changes for video timing and attachment output by
  7. * Wolfgang Scherr <scherr@net4you.net>
  8. *
  9. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  10. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/i2c.h>
  28. #include <linux/videodev2.h>
  29. #include <media/v4l2-device.h>
  30. MODULE_DESCRIPTION("Philips SAA7185 video encoder driver");
  31. MODULE_AUTHOR("Dave Perks");
  32. MODULE_LICENSE("GPL");
  33. static int debug;
  34. module_param(debug, int, 0);
  35. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  36. /* ----------------------------------------------------------------------- */
  37. struct saa7185 {
  38. struct v4l2_subdev sd;
  39. unsigned char reg[128];
  40. v4l2_std_id norm;
  41. };
  42. static inline struct saa7185 *to_saa7185(struct v4l2_subdev *sd)
  43. {
  44. return container_of(sd, struct saa7185, sd);
  45. }
  46. /* ----------------------------------------------------------------------- */
  47. static inline int saa7185_read(struct v4l2_subdev *sd)
  48. {
  49. struct i2c_client *client = v4l2_get_subdevdata(sd);
  50. return i2c_smbus_read_byte(client);
  51. }
  52. static int saa7185_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  53. {
  54. struct i2c_client *client = v4l2_get_subdevdata(sd);
  55. struct saa7185 *encoder = to_saa7185(sd);
  56. v4l2_dbg(1, debug, sd, "%02x set to %02x\n", reg, value);
  57. encoder->reg[reg] = value;
  58. return i2c_smbus_write_byte_data(client, reg, value);
  59. }
  60. static int saa7185_write_block(struct v4l2_subdev *sd,
  61. const u8 *data, unsigned int len)
  62. {
  63. struct i2c_client *client = v4l2_get_subdevdata(sd);
  64. struct saa7185 *encoder = to_saa7185(sd);
  65. int ret = -1;
  66. u8 reg;
  67. /* the adv7175 has an autoincrement function, use it if
  68. * the adapter understands raw I2C */
  69. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  70. /* do raw I2C, not smbus compatible */
  71. u8 block_data[32];
  72. int block_len;
  73. while (len >= 2) {
  74. block_len = 0;
  75. block_data[block_len++] = reg = data[0];
  76. do {
  77. block_data[block_len++] =
  78. encoder->reg[reg++] = data[1];
  79. len -= 2;
  80. data += 2;
  81. } while (len >= 2 && data[0] == reg && block_len < 32);
  82. ret = i2c_master_send(client, block_data, block_len);
  83. if (ret < 0)
  84. break;
  85. }
  86. } else {
  87. /* do some slow I2C emulation kind of thing */
  88. while (len >= 2) {
  89. reg = *data++;
  90. ret = saa7185_write(sd, reg, *data++);
  91. if (ret < 0)
  92. break;
  93. len -= 2;
  94. }
  95. }
  96. return ret;
  97. }
  98. /* ----------------------------------------------------------------------- */
  99. static const unsigned char init_common[] = {
  100. 0x3a, 0x0f, /* CBENB=0, V656=0, VY2C=1,
  101. * YUV2C=1, MY2C=1, MUV2C=1 */
  102. 0x42, 0x6b, /* OVLY0=107 */
  103. 0x43, 0x00, /* OVLU0=0 white */
  104. 0x44, 0x00, /* OVLV0=0 */
  105. 0x45, 0x22, /* OVLY1=34 */
  106. 0x46, 0xac, /* OVLU1=172 yellow */
  107. 0x47, 0x0e, /* OVLV1=14 */
  108. 0x48, 0x03, /* OVLY2=3 */
  109. 0x49, 0x1d, /* OVLU2=29 cyan */
  110. 0x4a, 0xac, /* OVLV2=172 */
  111. 0x4b, 0xf0, /* OVLY3=240 */
  112. 0x4c, 0xc8, /* OVLU3=200 green */
  113. 0x4d, 0xb9, /* OVLV3=185 */
  114. 0x4e, 0xd4, /* OVLY4=212 */
  115. 0x4f, 0x38, /* OVLU4=56 magenta */
  116. 0x50, 0x47, /* OVLV4=71 */
  117. 0x51, 0xc1, /* OVLY5=193 */
  118. 0x52, 0xe3, /* OVLU5=227 red */
  119. 0x53, 0x54, /* OVLV5=84 */
  120. 0x54, 0xa3, /* OVLY6=163 */
  121. 0x55, 0x54, /* OVLU6=84 blue */
  122. 0x56, 0xf2, /* OVLV6=242 */
  123. 0x57, 0x90, /* OVLY7=144 */
  124. 0x58, 0x00, /* OVLU7=0 black */
  125. 0x59, 0x00, /* OVLV7=0 */
  126. 0x5a, 0x00, /* CHPS=0 */
  127. 0x5b, 0x76, /* GAINU=118 */
  128. 0x5c, 0xa5, /* GAINV=165 */
  129. 0x5d, 0x3c, /* BLCKL=60 */
  130. 0x5e, 0x3a, /* BLNNL=58 */
  131. 0x5f, 0x3a, /* CCRS=0, BLNVB=58 */
  132. 0x60, 0x00, /* NULL */
  133. /* 0x61 - 0x66 set according to norm */
  134. 0x67, 0x00, /* 0 : caption 1st byte odd field */
  135. 0x68, 0x00, /* 0 : caption 2nd byte odd field */
  136. 0x69, 0x00, /* 0 : caption 1st byte even field */
  137. 0x6a, 0x00, /* 0 : caption 2nd byte even field */
  138. 0x6b, 0x91, /* MODIN=2, PCREF=0, SCCLN=17 */
  139. 0x6c, 0x20, /* SRCV1=0, TRCV2=1, ORCV1=0, PRCV1=0,
  140. * CBLF=0, ORCV2=0, PRCV2=0 */
  141. 0x6d, 0x00, /* SRCM1=0, CCEN=0 */
  142. 0x6e, 0x0e, /* HTRIG=0x005, approx. centered, at
  143. * least for PAL */
  144. 0x6f, 0x00, /* HTRIG upper bits */
  145. 0x70, 0x20, /* PHRES=0, SBLN=1, VTRIG=0 */
  146. /* The following should not be needed */
  147. 0x71, 0x15, /* BMRQ=0x115 */
  148. 0x72, 0x90, /* EMRQ=0x690 */
  149. 0x73, 0x61, /* EMRQ=0x690, BMRQ=0x115 */
  150. 0x74, 0x00, /* NULL */
  151. 0x75, 0x00, /* NULL */
  152. 0x76, 0x00, /* NULL */
  153. 0x77, 0x15, /* BRCV=0x115 */
  154. 0x78, 0x90, /* ERCV=0x690 */
  155. 0x79, 0x61, /* ERCV=0x690, BRCV=0x115 */
  156. /* Field length controls */
  157. 0x7a, 0x70, /* FLC=0 */
  158. /* The following should not be needed if SBLN = 1 */
  159. 0x7b, 0x16, /* FAL=22 */
  160. 0x7c, 0x35, /* LAL=244 */
  161. 0x7d, 0x20, /* LAL=244, FAL=22 */
  162. };
  163. static const unsigned char init_pal[] = {
  164. 0x61, 0x1e, /* FISE=0, PAL=1, SCBW=1, RTCE=1,
  165. * YGS=1, INPI=0, DOWN=0 */
  166. 0x62, 0xc8, /* DECTYP=1, BSTA=72 */
  167. 0x63, 0xcb, /* FSC0 */
  168. 0x64, 0x8a, /* FSC1 */
  169. 0x65, 0x09, /* FSC2 */
  170. 0x66, 0x2a, /* FSC3 */
  171. };
  172. static const unsigned char init_ntsc[] = {
  173. 0x61, 0x1d, /* FISE=1, PAL=0, SCBW=1, RTCE=1,
  174. * YGS=1, INPI=0, DOWN=0 */
  175. 0x62, 0xe6, /* DECTYP=1, BSTA=102 */
  176. 0x63, 0x1f, /* FSC0 */
  177. 0x64, 0x7c, /* FSC1 */
  178. 0x65, 0xf0, /* FSC2 */
  179. 0x66, 0x21, /* FSC3 */
  180. };
  181. static int saa7185_init(struct v4l2_subdev *sd, u32 val)
  182. {
  183. struct saa7185 *encoder = to_saa7185(sd);
  184. saa7185_write_block(sd, init_common, sizeof(init_common));
  185. if (encoder->norm & V4L2_STD_NTSC)
  186. saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
  187. else
  188. saa7185_write_block(sd, init_pal, sizeof(init_pal));
  189. return 0;
  190. }
  191. static int saa7185_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  192. {
  193. struct saa7185 *encoder = to_saa7185(sd);
  194. if (std & V4L2_STD_NTSC)
  195. saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
  196. else if (std & V4L2_STD_PAL)
  197. saa7185_write_block(sd, init_pal, sizeof(init_pal));
  198. else
  199. return -EINVAL;
  200. encoder->norm = std;
  201. return 0;
  202. }
  203. static int saa7185_s_routing(struct v4l2_subdev *sd,
  204. u32 input, u32 output, u32 config)
  205. {
  206. struct saa7185 *encoder = to_saa7185(sd);
  207. /* RJ: input = 0: input is from SA7111
  208. input = 1: input is from ZR36060 */
  209. switch (input) {
  210. case 0:
  211. /* turn off colorbar */
  212. saa7185_write(sd, 0x3a, 0x0f);
  213. /* Switch RTCE to 1 */
  214. saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x08);
  215. saa7185_write(sd, 0x6e, 0x01);
  216. break;
  217. case 1:
  218. /* turn off colorbar */
  219. saa7185_write(sd, 0x3a, 0x0f);
  220. /* Switch RTCE to 0 */
  221. saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x00);
  222. /* SW: a slight sync problem... */
  223. saa7185_write(sd, 0x6e, 0x00);
  224. break;
  225. case 2:
  226. /* turn on colorbar */
  227. saa7185_write(sd, 0x3a, 0x8f);
  228. /* Switch RTCE to 0 */
  229. saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x08);
  230. /* SW: a slight sync problem... */
  231. saa7185_write(sd, 0x6e, 0x01);
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. return 0;
  237. }
  238. /* ----------------------------------------------------------------------- */
  239. static const struct v4l2_subdev_core_ops saa7185_core_ops = {
  240. .init = saa7185_init,
  241. };
  242. static const struct v4l2_subdev_video_ops saa7185_video_ops = {
  243. .s_std_output = saa7185_s_std_output,
  244. .s_routing = saa7185_s_routing,
  245. };
  246. static const struct v4l2_subdev_ops saa7185_ops = {
  247. .core = &saa7185_core_ops,
  248. .video = &saa7185_video_ops,
  249. };
  250. /* ----------------------------------------------------------------------- */
  251. static int saa7185_probe(struct i2c_client *client,
  252. const struct i2c_device_id *id)
  253. {
  254. int i;
  255. struct saa7185 *encoder;
  256. struct v4l2_subdev *sd;
  257. /* Check if the adapter supports the needed features */
  258. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  259. return -ENODEV;
  260. v4l_info(client, "chip found @ 0x%x (%s)\n",
  261. client->addr << 1, client->adapter->name);
  262. encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
  263. if (encoder == NULL)
  264. return -ENOMEM;
  265. encoder->norm = V4L2_STD_NTSC;
  266. sd = &encoder->sd;
  267. v4l2_i2c_subdev_init(sd, client, &saa7185_ops);
  268. i = saa7185_write_block(sd, init_common, sizeof(init_common));
  269. if (i >= 0)
  270. i = saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
  271. if (i < 0)
  272. v4l2_dbg(1, debug, sd, "init error %d\n", i);
  273. else
  274. v4l2_dbg(1, debug, sd, "revision 0x%x\n",
  275. saa7185_read(sd) >> 5);
  276. return 0;
  277. }
  278. static int saa7185_remove(struct i2c_client *client)
  279. {
  280. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  281. struct saa7185 *encoder = to_saa7185(sd);
  282. v4l2_device_unregister_subdev(sd);
  283. /* SW: output off is active */
  284. saa7185_write(sd, 0x61, (encoder->reg[0x61]) | 0x40);
  285. return 0;
  286. }
  287. /* ----------------------------------------------------------------------- */
  288. static const struct i2c_device_id saa7185_id[] = {
  289. { "saa7185", 0 },
  290. { }
  291. };
  292. MODULE_DEVICE_TABLE(i2c, saa7185_id);
  293. static struct i2c_driver saa7185_driver = {
  294. .driver = {
  295. .name = "saa7185",
  296. },
  297. .probe = saa7185_probe,
  298. .remove = saa7185_remove,
  299. .id_table = saa7185_id,
  300. };
  301. module_i2c_driver(saa7185_driver);