bt856.c 6.9 KB

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