bt856.c 7.0 KB

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