bt856.c 6.9 KB

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