bt856.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * bt856 - BT856A Digital Video Encoder (Rockwell Part)
  3. *
  4. * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
  5. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  6. *
  7. * Modifications for LML33/DC10plus unified driver
  8. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  9. *
  10. * This code was modify/ported from the saa7111 driver written
  11. * by Dave Perks.
  12. *
  13. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  14. * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/i2c.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-device.h>
  34. MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
  35. MODULE_AUTHOR("Mike Bernson & Dave Perks");
  36. MODULE_LICENSE("GPL");
  37. static int debug;
  38. module_param(debug, int, 0);
  39. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  40. /* ----------------------------------------------------------------------- */
  41. #define BT856_REG_OFFSET 0xDA
  42. #define BT856_NR_REG 6
  43. struct bt856 {
  44. struct v4l2_subdev sd;
  45. unsigned char reg[BT856_NR_REG];
  46. v4l2_std_id norm;
  47. };
  48. static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
  49. {
  50. return container_of(sd, struct bt856, sd);
  51. }
  52. /* ----------------------------------------------------------------------- */
  53. static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
  54. {
  55. struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
  56. encoder->reg[reg - BT856_REG_OFFSET] = value;
  57. return i2c_smbus_write_byte_data(client, reg, value);
  58. }
  59. static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
  60. {
  61. return bt856_write(encoder, reg,
  62. (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
  63. (value ? (1 << bit) : 0));
  64. }
  65. static void bt856_dump(struct bt856 *encoder)
  66. {
  67. int i;
  68. v4l2_info(&encoder->sd, "register dump:\n");
  69. for (i = 0; i < BT856_NR_REG; i += 2)
  70. printk(KERN_CONT " %02x", encoder->reg[i]);
  71. printk(KERN_CONT "\n");
  72. }
  73. /* ----------------------------------------------------------------------- */
  74. static int bt856_init(struct v4l2_subdev *sd, u32 arg)
  75. {
  76. struct bt856 *encoder = to_bt856(sd);
  77. /* This is just for testing!!! */
  78. v4l2_dbg(1, debug, sd, "init\n");
  79. bt856_write(encoder, 0xdc, 0x18);
  80. bt856_write(encoder, 0xda, 0);
  81. bt856_write(encoder, 0xde, 0);
  82. bt856_setbit(encoder, 0xdc, 3, 1);
  83. /*bt856_setbit(encoder, 0xdc, 6, 0);*/
  84. bt856_setbit(encoder, 0xdc, 4, 1);
  85. if (encoder->norm & V4L2_STD_NTSC)
  86. bt856_setbit(encoder, 0xdc, 2, 0);
  87. else
  88. bt856_setbit(encoder, 0xdc, 2, 1);
  89. bt856_setbit(encoder, 0xdc, 1, 1);
  90. bt856_setbit(encoder, 0xde, 4, 0);
  91. bt856_setbit(encoder, 0xde, 3, 1);
  92. if (debug != 0)
  93. bt856_dump(encoder);
  94. return 0;
  95. }
  96. static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  97. {
  98. struct bt856 *encoder = to_bt856(sd);
  99. v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
  100. if (std & V4L2_STD_NTSC) {
  101. bt856_setbit(encoder, 0xdc, 2, 0);
  102. } else if (std & V4L2_STD_PAL) {
  103. bt856_setbit(encoder, 0xdc, 2, 1);
  104. bt856_setbit(encoder, 0xda, 0, 0);
  105. /*bt856_setbit(encoder, 0xda, 0, 1);*/
  106. } else {
  107. return -EINVAL;
  108. }
  109. encoder->norm = std;
  110. if (debug != 0)
  111. bt856_dump(encoder);
  112. return 0;
  113. }
  114. static int bt856_s_routing(struct v4l2_subdev *sd,
  115. u32 input, u32 output, u32 config)
  116. {
  117. struct bt856 *encoder = to_bt856(sd);
  118. v4l2_dbg(1, debug, sd, "set input %d\n", input);
  119. /* We only have video bus.
  120. * input= 0: input is from bt819
  121. * input= 1: input is from ZR36060 */
  122. switch (input) {
  123. case 0:
  124. bt856_setbit(encoder, 0xde, 4, 0);
  125. bt856_setbit(encoder, 0xde, 3, 1);
  126. bt856_setbit(encoder, 0xdc, 3, 1);
  127. bt856_setbit(encoder, 0xdc, 6, 0);
  128. break;
  129. case 1:
  130. bt856_setbit(encoder, 0xde, 4, 0);
  131. bt856_setbit(encoder, 0xde, 3, 1);
  132. bt856_setbit(encoder, 0xdc, 3, 1);
  133. bt856_setbit(encoder, 0xdc, 6, 1);
  134. break;
  135. case 2: /* Color bar */
  136. bt856_setbit(encoder, 0xdc, 3, 0);
  137. bt856_setbit(encoder, 0xde, 4, 1);
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. if (debug != 0)
  143. bt856_dump(encoder);
  144. return 0;
  145. }
  146. /* ----------------------------------------------------------------------- */
  147. static const struct v4l2_subdev_core_ops bt856_core_ops = {
  148. .init = bt856_init,
  149. };
  150. static const struct v4l2_subdev_video_ops bt856_video_ops = {
  151. .s_std_output = bt856_s_std_output,
  152. .s_routing = bt856_s_routing,
  153. };
  154. static const struct v4l2_subdev_ops bt856_ops = {
  155. .core = &bt856_core_ops,
  156. .video = &bt856_video_ops,
  157. };
  158. /* ----------------------------------------------------------------------- */
  159. static int bt856_probe(struct i2c_client *client,
  160. const struct i2c_device_id *id)
  161. {
  162. struct bt856 *encoder;
  163. struct v4l2_subdev *sd;
  164. /* Check if the adapter supports the needed features */
  165. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  166. return -ENODEV;
  167. v4l_info(client, "chip found @ 0x%x (%s)\n",
  168. client->addr << 1, client->adapter->name);
  169. encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
  170. if (encoder == NULL)
  171. return -ENOMEM;
  172. sd = &encoder->sd;
  173. v4l2_i2c_subdev_init(sd, client, &bt856_ops);
  174. encoder->norm = V4L2_STD_NTSC;
  175. bt856_write(encoder, 0xdc, 0x18);
  176. bt856_write(encoder, 0xda, 0);
  177. bt856_write(encoder, 0xde, 0);
  178. bt856_setbit(encoder, 0xdc, 3, 1);
  179. /*bt856_setbit(encoder, 0xdc, 6, 0);*/
  180. bt856_setbit(encoder, 0xdc, 4, 1);
  181. if (encoder->norm & V4L2_STD_NTSC)
  182. bt856_setbit(encoder, 0xdc, 2, 0);
  183. else
  184. bt856_setbit(encoder, 0xdc, 2, 1);
  185. bt856_setbit(encoder, 0xdc, 1, 1);
  186. bt856_setbit(encoder, 0xde, 4, 0);
  187. bt856_setbit(encoder, 0xde, 3, 1);
  188. if (debug != 0)
  189. bt856_dump(encoder);
  190. return 0;
  191. }
  192. static int bt856_remove(struct i2c_client *client)
  193. {
  194. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  195. v4l2_device_unregister_subdev(sd);
  196. return 0;
  197. }
  198. static const struct i2c_device_id bt856_id[] = {
  199. { "bt856", 0 },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(i2c, bt856_id);
  203. static struct i2c_driver bt856_driver = {
  204. .driver = {
  205. .name = "bt856",
  206. },
  207. .probe = bt856_probe,
  208. .remove = bt856_remove,
  209. .id_table = bt856_id,
  210. };
  211. module_i2c_driver(bt856_driver);