saa7185.c 9.0 KB

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