bt866.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. bt866 - BT866 Digital Video Encoder (Rockwell Part)
  4. Copyright (C) 1999 Mike Bernson <[email protected]>
  5. Copyright (C) 1998 Dave Perks <[email protected]>
  6. Modifications for LML33/DC10plus unified driver
  7. Copyright (C) 2000 Serguei Miridonov <[email protected]>
  8. This code was modify/ported from the saa7111 driver written
  9. by Dave Perks.
  10. This code was adapted for the bt866 by Christer Weinigel and ported
  11. to 2.6 by Martin Samuelsson.
  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("Brooktree-866 video encoder driver");
  22. MODULE_AUTHOR("Mike Bernson & 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 bt866 {
  29. struct v4l2_subdev sd;
  30. u8 reg[256];
  31. };
  32. static inline struct bt866 *to_bt866(struct v4l2_subdev *sd)
  33. {
  34. return container_of(sd, struct bt866, sd);
  35. }
  36. static int bt866_write(struct bt866 *encoder, u8 subaddr, u8 data)
  37. {
  38. struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
  39. u8 buffer[2];
  40. int err;
  41. buffer[0] = subaddr;
  42. buffer[1] = data;
  43. encoder->reg[subaddr] = data;
  44. v4l_dbg(1, debug, client, "write 0x%02x = 0x%02x\n", subaddr, data);
  45. for (err = 0; err < 3;) {
  46. if (i2c_master_send(client, buffer, 2) == 2)
  47. break;
  48. err++;
  49. v4l_warn(client, "error #%d writing to 0x%02x\n",
  50. err, subaddr);
  51. schedule_timeout_interruptible(msecs_to_jiffies(100));
  52. }
  53. if (err == 3) {
  54. v4l_warn(client, "giving up\n");
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. static int bt866_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  60. {
  61. v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
  62. /* Only PAL supported by this driver at the moment! */
  63. if (!(std & V4L2_STD_NTSC))
  64. return -EINVAL;
  65. return 0;
  66. }
  67. static int bt866_s_routing(struct v4l2_subdev *sd,
  68. u32 input, u32 output, u32 config)
  69. {
  70. static const __u8 init[] = {
  71. 0xc8, 0xcc, /* CRSCALE */
  72. 0xca, 0x91, /* CBSCALE */
  73. 0xcc, 0x24, /* YC16 | OSDNUM */
  74. 0xda, 0x00, /* */
  75. 0xdc, 0x24, /* SETMODE | PAL */
  76. 0xde, 0x02, /* EACTIVE */
  77. /* overlay colors */
  78. 0x70, 0xEB, 0x90, 0x80, 0xB0, 0x80, /* white */
  79. 0x72, 0xA2, 0x92, 0x8E, 0xB2, 0x2C, /* yellow */
  80. 0x74, 0x83, 0x94, 0x2C, 0xB4, 0x9C, /* cyan */
  81. 0x76, 0x70, 0x96, 0x3A, 0xB6, 0x48, /* green */
  82. 0x78, 0x54, 0x98, 0xC6, 0xB8, 0xB8, /* magenta */
  83. 0x7A, 0x41, 0x9A, 0xD4, 0xBA, 0x64, /* red */
  84. 0x7C, 0x23, 0x9C, 0x72, 0xBC, 0xD4, /* blue */
  85. 0x7E, 0x10, 0x9E, 0x80, 0xBE, 0x80, /* black */
  86. 0x60, 0xEB, 0x80, 0x80, 0xc0, 0x80, /* white */
  87. 0x62, 0xA2, 0x82, 0x8E, 0xc2, 0x2C, /* yellow */
  88. 0x64, 0x83, 0x84, 0x2C, 0xc4, 0x9C, /* cyan */
  89. 0x66, 0x70, 0x86, 0x3A, 0xc6, 0x48, /* green */
  90. 0x68, 0x54, 0x88, 0xC6, 0xc8, 0xB8, /* magenta */
  91. 0x6A, 0x41, 0x8A, 0xD4, 0xcA, 0x64, /* red */
  92. 0x6C, 0x23, 0x8C, 0x72, 0xcC, 0xD4, /* blue */
  93. 0x6E, 0x10, 0x8E, 0x80, 0xcE, 0x80, /* black */
  94. };
  95. struct bt866 *encoder = to_bt866(sd);
  96. u8 val;
  97. int i;
  98. for (i = 0; i < ARRAY_SIZE(init) / 2; i += 2)
  99. bt866_write(encoder, init[i], init[i+1]);
  100. val = encoder->reg[0xdc];
  101. if (input == 0)
  102. val |= 0x40; /* CBSWAP */
  103. else
  104. val &= ~0x40; /* !CBSWAP */
  105. bt866_write(encoder, 0xdc, val);
  106. val = encoder->reg[0xcc];
  107. if (input == 2)
  108. val |= 0x01; /* OSDBAR */
  109. else
  110. val &= ~0x01; /* !OSDBAR */
  111. bt866_write(encoder, 0xcc, val);
  112. v4l2_dbg(1, debug, sd, "set input %d\n", input);
  113. switch (input) {
  114. case 0:
  115. case 1:
  116. case 2:
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. #if 0
  124. /* Code to setup square pixels, might be of some use in the future,
  125. but is currently unused. */
  126. val = encoder->reg[0xdc];
  127. if (*iarg)
  128. val |= 1; /* SQUARE */
  129. else
  130. val &= ~1; /* !SQUARE */
  131. bt866_write(client, 0xdc, val);
  132. #endif
  133. /* ----------------------------------------------------------------------- */
  134. static const struct v4l2_subdev_video_ops bt866_video_ops = {
  135. .s_std_output = bt866_s_std_output,
  136. .s_routing = bt866_s_routing,
  137. };
  138. static const struct v4l2_subdev_ops bt866_ops = {
  139. .video = &bt866_video_ops,
  140. };
  141. static int bt866_probe(struct i2c_client *client,
  142. const struct i2c_device_id *id)
  143. {
  144. struct bt866 *encoder;
  145. struct v4l2_subdev *sd;
  146. v4l_info(client, "chip found @ 0x%x (%s)\n",
  147. client->addr << 1, client->adapter->name);
  148. encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
  149. if (encoder == NULL)
  150. return -ENOMEM;
  151. sd = &encoder->sd;
  152. v4l2_i2c_subdev_init(sd, client, &bt866_ops);
  153. return 0;
  154. }
  155. static void bt866_remove(struct i2c_client *client)
  156. {
  157. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  158. v4l2_device_unregister_subdev(sd);
  159. }
  160. static const struct i2c_device_id bt866_id[] = {
  161. { "bt866", 0 },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, bt866_id);
  165. static struct i2c_driver bt866_driver = {
  166. .driver = {
  167. .name = "bt866",
  168. },
  169. .probe = bt866_probe,
  170. .remove = bt866_remove,
  171. .id_table = bt866_id,
  172. };
  173. module_i2c_driver(bt866_driver);