bt856.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/videodev.h>
  37. #include <linux/video_encoder.h>
  38. #include <media/v4l2-common.h>
  39. #include <media/v4l2-i2c-drv-legacy.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. unsigned char reg[BT856_NR_REG];
  51. int norm;
  52. int enable;
  53. };
  54. /* ----------------------------------------------------------------------- */
  55. static inline int bt856_write(struct i2c_client *client, u8 reg, u8 value)
  56. {
  57. struct bt856 *encoder = i2c_get_clientdata(client);
  58. encoder->reg[reg - BT856_REG_OFFSET] = value;
  59. return i2c_smbus_write_byte_data(client, reg, value);
  60. }
  61. static inline int bt856_setbit(struct i2c_client *client, u8 reg, u8 bit, u8 value)
  62. {
  63. struct bt856 *encoder = i2c_get_clientdata(client);
  64. return bt856_write(client, reg,
  65. (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
  66. (value ? (1 << bit) : 0));
  67. }
  68. static void bt856_dump(struct i2c_client *client)
  69. {
  70. int i;
  71. struct bt856 *encoder = i2c_get_clientdata(client);
  72. v4l_info(client, "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_command(struct i2c_client *client, unsigned cmd, void *arg)
  79. {
  80. struct bt856 *encoder = i2c_get_clientdata(client);
  81. switch (cmd) {
  82. case 0:
  83. /* This is just for testing!!! */
  84. v4l_dbg(1, debug, client, "init\n");
  85. bt856_write(client, 0xdc, 0x18);
  86. bt856_write(client, 0xda, 0);
  87. bt856_write(client, 0xde, 0);
  88. bt856_setbit(client, 0xdc, 3, 1);
  89. //bt856_setbit(client, 0xdc, 6, 0);
  90. bt856_setbit(client, 0xdc, 4, 1);
  91. switch (encoder->norm) {
  92. case VIDEO_MODE_NTSC:
  93. bt856_setbit(client, 0xdc, 2, 0);
  94. break;
  95. case VIDEO_MODE_PAL:
  96. bt856_setbit(client, 0xdc, 2, 1);
  97. break;
  98. }
  99. bt856_setbit(client, 0xdc, 1, 1);
  100. bt856_setbit(client, 0xde, 4, 0);
  101. bt856_setbit(client, 0xde, 3, 1);
  102. if (debug != 0)
  103. bt856_dump(client);
  104. break;
  105. case ENCODER_GET_CAPABILITIES:
  106. {
  107. struct video_encoder_capability *cap = arg;
  108. v4l_dbg(1, debug, client, "get capabilities\n");
  109. cap->flags = VIDEO_ENCODER_PAL |
  110. VIDEO_ENCODER_NTSC |
  111. VIDEO_ENCODER_CCIR;
  112. cap->inputs = 2;
  113. cap->outputs = 1;
  114. break;
  115. }
  116. case ENCODER_SET_NORM:
  117. {
  118. int *iarg = arg;
  119. v4l_dbg(1, debug, client, "set norm %d\n", *iarg);
  120. switch (*iarg) {
  121. case VIDEO_MODE_NTSC:
  122. bt856_setbit(client, 0xdc, 2, 0);
  123. break;
  124. case VIDEO_MODE_PAL:
  125. bt856_setbit(client, 0xdc, 2, 1);
  126. bt856_setbit(client, 0xda, 0, 0);
  127. //bt856_setbit(client, 0xda, 0, 1);
  128. break;
  129. default:
  130. return -EINVAL;
  131. }
  132. encoder->norm = *iarg;
  133. if (debug != 0)
  134. bt856_dump(client);
  135. break;
  136. }
  137. case ENCODER_SET_INPUT:
  138. {
  139. int *iarg = arg;
  140. v4l_dbg(1, debug, client, "set input %d\n", *iarg);
  141. /* We only have video bus.
  142. * iarg = 0: input is from bt819
  143. * iarg = 1: input is from ZR36060 */
  144. switch (*iarg) {
  145. case 0:
  146. bt856_setbit(client, 0xde, 4, 0);
  147. bt856_setbit(client, 0xde, 3, 1);
  148. bt856_setbit(client, 0xdc, 3, 1);
  149. bt856_setbit(client, 0xdc, 6, 0);
  150. break;
  151. case 1:
  152. bt856_setbit(client, 0xde, 4, 0);
  153. bt856_setbit(client, 0xde, 3, 1);
  154. bt856_setbit(client, 0xdc, 3, 1);
  155. bt856_setbit(client, 0xdc, 6, 1);
  156. break;
  157. case 2: // Color bar
  158. bt856_setbit(client, 0xdc, 3, 0);
  159. bt856_setbit(client, 0xde, 4, 1);
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. if (debug != 0)
  165. bt856_dump(client);
  166. break;
  167. }
  168. case ENCODER_SET_OUTPUT:
  169. {
  170. int *iarg = arg;
  171. v4l_dbg(1, debug, client, "set output %d\n", *iarg);
  172. /* not much choice of outputs */
  173. if (*iarg != 0)
  174. return -EINVAL;
  175. break;
  176. }
  177. case ENCODER_ENABLE_OUTPUT:
  178. {
  179. int *iarg = arg;
  180. encoder->enable = !!*iarg;
  181. v4l_dbg(1, debug, client, "enable output %d\n", encoder->enable);
  182. break;
  183. }
  184. default:
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. /* ----------------------------------------------------------------------- */
  190. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  191. I2C_CLIENT_INSMOD;
  192. static int bt856_probe(struct i2c_client *client,
  193. const struct i2c_device_id *id)
  194. {
  195. struct bt856 *encoder;
  196. /* Check if the adapter supports the needed features */
  197. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  198. return -ENODEV;
  199. v4l_info(client, "chip found @ 0x%x (%s)\n",
  200. client->addr << 1, client->adapter->name);
  201. encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
  202. if (encoder == NULL)
  203. return -ENOMEM;
  204. encoder->norm = VIDEO_MODE_NTSC;
  205. encoder->enable = 1;
  206. i2c_set_clientdata(client, encoder);
  207. bt856_write(client, 0xdc, 0x18);
  208. bt856_write(client, 0xda, 0);
  209. bt856_write(client, 0xde, 0);
  210. bt856_setbit(client, 0xdc, 3, 1);
  211. //bt856_setbit(client, 0xdc, 6, 0);
  212. bt856_setbit(client, 0xdc, 4, 1);
  213. switch (encoder->norm) {
  214. case VIDEO_MODE_NTSC:
  215. bt856_setbit(client, 0xdc, 2, 0);
  216. break;
  217. case VIDEO_MODE_PAL:
  218. bt856_setbit(client, 0xdc, 2, 1);
  219. break;
  220. }
  221. bt856_setbit(client, 0xdc, 1, 1);
  222. bt856_setbit(client, 0xde, 4, 0);
  223. bt856_setbit(client, 0xde, 3, 1);
  224. if (debug != 0)
  225. bt856_dump(client);
  226. return 0;
  227. }
  228. static int bt856_remove(struct i2c_client *client)
  229. {
  230. kfree(i2c_get_clientdata(client));
  231. return 0;
  232. }
  233. static const struct i2c_device_id bt856_id[] = {
  234. { "bt856", 0 },
  235. { }
  236. };
  237. MODULE_DEVICE_TABLE(i2c, bt856_id);
  238. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  239. .name = "bt856",
  240. .driverid = I2C_DRIVERID_BT856,
  241. .command = bt856_command,
  242. .probe = bt856_probe,
  243. .remove = bt856_remove,
  244. .id_table = bt856_id,
  245. };