bt856.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <linux/ioctl.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/i2c.h>
  36. #include <linux/videodev2.h>
  37. #include <media/v4l2-device.h>
  38. MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
  39. MODULE_AUTHOR("Mike Bernson & Dave Perks");
  40. MODULE_LICENSE("GPL");
  41. static int debug;
  42. module_param(debug, int, 0);
  43. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  44. /* ----------------------------------------------------------------------- */
  45. #define BT856_REG_OFFSET 0xDA
  46. #define BT856_NR_REG 6
  47. struct bt856 {
  48. struct v4l2_subdev sd;
  49. unsigned char reg[BT856_NR_REG];
  50. v4l2_std_id norm;
  51. };
  52. static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
  53. {
  54. return container_of(sd, struct bt856, sd);
  55. }
  56. /* ----------------------------------------------------------------------- */
  57. static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
  58. {
  59. struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
  60. encoder->reg[reg - BT856_REG_OFFSET] = value;
  61. return i2c_smbus_write_byte_data(client, reg, value);
  62. }
  63. static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
  64. {
  65. return bt856_write(encoder, reg,
  66. (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
  67. (value ? (1 << bit) : 0));
  68. }
  69. static void bt856_dump(struct bt856 *encoder)
  70. {
  71. int i;
  72. v4l2_info(&encoder->sd, "register dump:\n");
  73. for (i = 0; i < BT856_NR_REG; i += 2)
  74. printk(KERN_CONT " %02x", encoder->reg[i]);
  75. printk(KERN_CONT "\n");
  76. }
  77. /* ----------------------------------------------------------------------- */
  78. static int bt856_init(struct v4l2_subdev *sd, u32 arg)
  79. {
  80. struct bt856 *encoder = to_bt856(sd);
  81. /* This is just for testing!!! */
  82. v4l2_dbg(1, debug, sd, "init\n");
  83. bt856_write(encoder, 0xdc, 0x18);
  84. bt856_write(encoder, 0xda, 0);
  85. bt856_write(encoder, 0xde, 0);
  86. bt856_setbit(encoder, 0xdc, 3, 1);
  87. /*bt856_setbit(encoder, 0xdc, 6, 0);*/
  88. bt856_setbit(encoder, 0xdc, 4, 1);
  89. if (encoder->norm & V4L2_STD_NTSC)
  90. bt856_setbit(encoder, 0xdc, 2, 0);
  91. else
  92. bt856_setbit(encoder, 0xdc, 2, 1);
  93. bt856_setbit(encoder, 0xdc, 1, 1);
  94. bt856_setbit(encoder, 0xde, 4, 0);
  95. bt856_setbit(encoder, 0xde, 3, 1);
  96. if (debug != 0)
  97. bt856_dump(encoder);
  98. return 0;
  99. }
  100. static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  101. {
  102. struct bt856 *encoder = to_bt856(sd);
  103. v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
  104. if (std & V4L2_STD_NTSC) {
  105. bt856_setbit(encoder, 0xdc, 2, 0);
  106. } else if (std & V4L2_STD_PAL) {
  107. bt856_setbit(encoder, 0xdc, 2, 1);
  108. bt856_setbit(encoder, 0xda, 0, 0);
  109. /*bt856_setbit(encoder, 0xda, 0, 1);*/
  110. } else {
  111. return -EINVAL;
  112. }
  113. encoder->norm = std;
  114. if (debug != 0)
  115. bt856_dump(encoder);
  116. return 0;
  117. }
  118. static int bt856_s_routing(struct v4l2_subdev *sd,
  119. u32 input, u32 output, u32 config)
  120. {
  121. struct bt856 *encoder = to_bt856(sd);
  122. v4l2_dbg(1, debug, sd, "set input %d\n", input);
  123. /* We only have video bus.
  124. * input= 0: input is from bt819
  125. * input= 1: input is from ZR36060 */
  126. switch (input) {
  127. case 0:
  128. bt856_setbit(encoder, 0xde, 4, 0);
  129. bt856_setbit(encoder, 0xde, 3, 1);
  130. bt856_setbit(encoder, 0xdc, 3, 1);
  131. bt856_setbit(encoder, 0xdc, 6, 0);
  132. break;
  133. case 1:
  134. bt856_setbit(encoder, 0xde, 4, 0);
  135. bt856_setbit(encoder, 0xde, 3, 1);
  136. bt856_setbit(encoder, 0xdc, 3, 1);
  137. bt856_setbit(encoder, 0xdc, 6, 1);
  138. break;
  139. case 2: /* Color bar */
  140. bt856_setbit(encoder, 0xdc, 3, 0);
  141. bt856_setbit(encoder, 0xde, 4, 1);
  142. break;
  143. default:
  144. return -EINVAL;
  145. }
  146. if (debug != 0)
  147. bt856_dump(encoder);
  148. return 0;
  149. }
  150. /* ----------------------------------------------------------------------- */
  151. static const struct v4l2_subdev_core_ops bt856_core_ops = {
  152. .init = bt856_init,
  153. };
  154. static const struct v4l2_subdev_video_ops bt856_video_ops = {
  155. .s_std_output = bt856_s_std_output,
  156. .s_routing = bt856_s_routing,
  157. };
  158. static const struct v4l2_subdev_ops bt856_ops = {
  159. .core = &bt856_core_ops,
  160. .video = &bt856_video_ops,
  161. };
  162. /* ----------------------------------------------------------------------- */
  163. static int bt856_probe(struct i2c_client *client,
  164. const struct i2c_device_id *id)
  165. {
  166. struct bt856 *encoder;
  167. struct v4l2_subdev *sd;
  168. /* Check if the adapter supports the needed features */
  169. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  170. return -ENODEV;
  171. v4l_info(client, "chip found @ 0x%x (%s)\n",
  172. client->addr << 1, client->adapter->name);
  173. encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
  174. if (encoder == NULL)
  175. return -ENOMEM;
  176. sd = &encoder->sd;
  177. v4l2_i2c_subdev_init(sd, client, &bt856_ops);
  178. encoder->norm = V4L2_STD_NTSC;
  179. bt856_write(encoder, 0xdc, 0x18);
  180. bt856_write(encoder, 0xda, 0);
  181. bt856_write(encoder, 0xde, 0);
  182. bt856_setbit(encoder, 0xdc, 3, 1);
  183. /*bt856_setbit(encoder, 0xdc, 6, 0);*/
  184. bt856_setbit(encoder, 0xdc, 4, 1);
  185. if (encoder->norm & V4L2_STD_NTSC)
  186. bt856_setbit(encoder, 0xdc, 2, 0);
  187. else
  188. bt856_setbit(encoder, 0xdc, 2, 1);
  189. bt856_setbit(encoder, 0xdc, 1, 1);
  190. bt856_setbit(encoder, 0xde, 4, 0);
  191. bt856_setbit(encoder, 0xde, 3, 1);
  192. if (debug != 0)
  193. bt856_dump(encoder);
  194. return 0;
  195. }
  196. static int bt856_remove(struct i2c_client *client)
  197. {
  198. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  199. v4l2_device_unregister_subdev(sd);
  200. return 0;
  201. }
  202. static const struct i2c_device_id bt856_id[] = {
  203. { "bt856", 0 },
  204. { }
  205. };
  206. MODULE_DEVICE_TABLE(i2c, bt856_id);
  207. static struct i2c_driver bt856_driver = {
  208. .driver = {
  209. .name = "bt856",
  210. },
  211. .probe = bt856_probe,
  212. .remove = bt856_remove,
  213. .id_table = bt856_id,
  214. };
  215. module_i2c_driver(bt856_driver);