bt856.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/errno.h>
  34. #include <linux/fs.h>
  35. #include <linux/kernel.h>
  36. #include <linux/major.h>
  37. #include <linux/slab.h>
  38. #include <linux/mm.h>
  39. #include <linux/pci.h>
  40. #include <linux/signal.h>
  41. #include <asm/io.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/page.h>
  44. #include <linux/sched.h>
  45. #include <linux/types.h>
  46. #include <linux/videodev.h>
  47. #include <asm/uaccess.h>
  48. MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
  49. MODULE_AUTHOR("Mike Bernson & Dave Perks");
  50. MODULE_LICENSE("GPL");
  51. #include <linux/i2c.h>
  52. #define I2C_NAME(s) (s)->name
  53. #include <linux/video_encoder.h>
  54. static int debug = 0;
  55. module_param(debug, int, 0);
  56. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  57. #define dprintk(num, format, args...) \
  58. do { \
  59. if (debug >= num) \
  60. printk(format, ##args); \
  61. } while (0)
  62. /* ----------------------------------------------------------------------- */
  63. #define REG_OFFSET 0xDA
  64. #define BT856_NR_REG 6
  65. struct bt856 {
  66. unsigned char reg[BT856_NR_REG];
  67. int norm;
  68. int enable;
  69. };
  70. #define I2C_BT856 0x88
  71. /* ----------------------------------------------------------------------- */
  72. static inline int
  73. bt856_write (struct i2c_client *client,
  74. u8 reg,
  75. u8 value)
  76. {
  77. struct bt856 *encoder = i2c_get_clientdata(client);
  78. encoder->reg[reg - REG_OFFSET] = value;
  79. return i2c_smbus_write_byte_data(client, reg, value);
  80. }
  81. static inline int
  82. bt856_setbit (struct i2c_client *client,
  83. u8 reg,
  84. u8 bit,
  85. u8 value)
  86. {
  87. struct bt856 *encoder = i2c_get_clientdata(client);
  88. return bt856_write(client, reg,
  89. (encoder->
  90. reg[reg - REG_OFFSET] & ~(1 << bit)) |
  91. (value ? (1 << bit) : 0));
  92. }
  93. static void
  94. bt856_dump (struct i2c_client *client)
  95. {
  96. int i;
  97. struct bt856 *encoder = i2c_get_clientdata(client);
  98. printk(KERN_INFO "%s: register dump:", I2C_NAME(client));
  99. for (i = 0; i < BT856_NR_REG; i += 2)
  100. printk(" %02x", encoder->reg[i]);
  101. printk("\n");
  102. }
  103. /* ----------------------------------------------------------------------- */
  104. static int
  105. bt856_command (struct i2c_client *client,
  106. unsigned int cmd,
  107. void *arg)
  108. {
  109. struct bt856 *encoder = i2c_get_clientdata(client);
  110. switch (cmd) {
  111. case 0:
  112. /* This is just for testing!!! */
  113. dprintk(1, KERN_INFO "bt856: init\n");
  114. bt856_write(client, 0xdc, 0x18);
  115. bt856_write(client, 0xda, 0);
  116. bt856_write(client, 0xde, 0);
  117. bt856_setbit(client, 0xdc, 3, 1);
  118. //bt856_setbit(client, 0xdc, 6, 0);
  119. bt856_setbit(client, 0xdc, 4, 1);
  120. switch (encoder->norm) {
  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. break;
  127. }
  128. bt856_setbit(client, 0xdc, 1, 1);
  129. bt856_setbit(client, 0xde, 4, 0);
  130. bt856_setbit(client, 0xde, 3, 1);
  131. if (debug != 0)
  132. bt856_dump(client);
  133. break;
  134. case ENCODER_GET_CAPABILITIES:
  135. {
  136. struct video_encoder_capability *cap = arg;
  137. dprintk(1, KERN_INFO "%s: get capabilities\n",
  138. I2C_NAME(client));
  139. cap->flags = VIDEO_ENCODER_PAL |
  140. VIDEO_ENCODER_NTSC |
  141. VIDEO_ENCODER_CCIR;
  142. cap->inputs = 2;
  143. cap->outputs = 1;
  144. }
  145. break;
  146. case ENCODER_SET_NORM:
  147. {
  148. int *iarg = arg;
  149. dprintk(1, KERN_INFO "%s: set norm %d\n", I2C_NAME(client),
  150. *iarg);
  151. switch (*iarg) {
  152. case VIDEO_MODE_NTSC:
  153. bt856_setbit(client, 0xdc, 2, 0);
  154. break;
  155. case VIDEO_MODE_PAL:
  156. bt856_setbit(client, 0xdc, 2, 1);
  157. bt856_setbit(client, 0xda, 0, 0);
  158. //bt856_setbit(client, 0xda, 0, 1);
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. encoder->norm = *iarg;
  164. if (debug != 0)
  165. bt856_dump(client);
  166. }
  167. break;
  168. case ENCODER_SET_INPUT:
  169. {
  170. int *iarg = arg;
  171. dprintk(1, KERN_INFO "%s: set input %d\n", I2C_NAME(client),
  172. *iarg);
  173. /* We only have video bus.
  174. * iarg = 0: input is from bt819
  175. * iarg = 1: input is from ZR36060 */
  176. switch (*iarg) {
  177. case 0:
  178. bt856_setbit(client, 0xde, 4, 0);
  179. bt856_setbit(client, 0xde, 3, 1);
  180. bt856_setbit(client, 0xdc, 3, 1);
  181. bt856_setbit(client, 0xdc, 6, 0);
  182. break;
  183. case 1:
  184. bt856_setbit(client, 0xde, 4, 0);
  185. bt856_setbit(client, 0xde, 3, 1);
  186. bt856_setbit(client, 0xdc, 3, 1);
  187. bt856_setbit(client, 0xdc, 6, 1);
  188. break;
  189. case 2: // Color bar
  190. bt856_setbit(client, 0xdc, 3, 0);
  191. bt856_setbit(client, 0xde, 4, 1);
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. if (debug != 0)
  197. bt856_dump(client);
  198. }
  199. break;
  200. case ENCODER_SET_OUTPUT:
  201. {
  202. int *iarg = arg;
  203. dprintk(1, KERN_INFO "%s: set output %d\n", I2C_NAME(client),
  204. *iarg);
  205. /* not much choice of outputs */
  206. if (*iarg != 0) {
  207. return -EINVAL;
  208. }
  209. }
  210. break;
  211. case ENCODER_ENABLE_OUTPUT:
  212. {
  213. int *iarg = arg;
  214. encoder->enable = !!*iarg;
  215. dprintk(1, KERN_INFO "%s: enable output %d\n",
  216. I2C_NAME(client), encoder->enable);
  217. }
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. return 0;
  223. }
  224. /* ----------------------------------------------------------------------- */
  225. /*
  226. * Generic i2c probe
  227. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  228. */
  229. static unsigned short normal_i2c[] = { I2C_BT856 >> 1, I2C_CLIENT_END };
  230. static unsigned short ignore = I2C_CLIENT_END;
  231. static struct i2c_client_address_data addr_data = {
  232. .normal_i2c = normal_i2c,
  233. .probe = &ignore,
  234. .ignore = &ignore,
  235. };
  236. static struct i2c_driver i2c_driver_bt856;
  237. static int
  238. bt856_detect_client (struct i2c_adapter *adapter,
  239. int address,
  240. int kind)
  241. {
  242. int i;
  243. struct i2c_client *client;
  244. struct bt856 *encoder;
  245. dprintk(1,
  246. KERN_INFO
  247. "bt856.c: detecting bt856 client on address 0x%x\n",
  248. address << 1);
  249. /* Check if the adapter supports the needed features */
  250. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  251. return 0;
  252. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  253. if (client == 0)
  254. return -ENOMEM;
  255. client->addr = address;
  256. client->adapter = adapter;
  257. client->driver = &i2c_driver_bt856;
  258. strlcpy(I2C_NAME(client), "bt856", sizeof(I2C_NAME(client)));
  259. encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
  260. if (encoder == NULL) {
  261. kfree(client);
  262. return -ENOMEM;
  263. }
  264. encoder->norm = VIDEO_MODE_NTSC;
  265. encoder->enable = 1;
  266. i2c_set_clientdata(client, encoder);
  267. i = i2c_attach_client(client);
  268. if (i) {
  269. kfree(client);
  270. kfree(encoder);
  271. return i;
  272. }
  273. bt856_write(client, 0xdc, 0x18);
  274. bt856_write(client, 0xda, 0);
  275. bt856_write(client, 0xde, 0);
  276. bt856_setbit(client, 0xdc, 3, 1);
  277. //bt856_setbit(client, 0xdc, 6, 0);
  278. bt856_setbit(client, 0xdc, 4, 1);
  279. switch (encoder->norm) {
  280. case VIDEO_MODE_NTSC:
  281. bt856_setbit(client, 0xdc, 2, 0);
  282. break;
  283. case VIDEO_MODE_PAL:
  284. bt856_setbit(client, 0xdc, 2, 1);
  285. break;
  286. }
  287. bt856_setbit(client, 0xdc, 1, 1);
  288. bt856_setbit(client, 0xde, 4, 0);
  289. bt856_setbit(client, 0xde, 3, 1);
  290. if (debug != 0)
  291. bt856_dump(client);
  292. dprintk(1, KERN_INFO "%s_attach: at address 0x%x\n", I2C_NAME(client),
  293. client->addr << 1);
  294. return 0;
  295. }
  296. static int
  297. bt856_attach_adapter (struct i2c_adapter *adapter)
  298. {
  299. dprintk(1,
  300. KERN_INFO
  301. "bt856.c: starting probe for adapter %s (0x%x)\n",
  302. I2C_NAME(adapter), adapter->id);
  303. return i2c_probe(adapter, &addr_data, &bt856_detect_client);
  304. }
  305. static int
  306. bt856_detach_client (struct i2c_client *client)
  307. {
  308. struct bt856 *encoder = i2c_get_clientdata(client);
  309. int err;
  310. err = i2c_detach_client(client);
  311. if (err) {
  312. return err;
  313. }
  314. kfree(encoder);
  315. kfree(client);
  316. return 0;
  317. }
  318. /* ----------------------------------------------------------------------- */
  319. static struct i2c_driver i2c_driver_bt856 = {
  320. .driver = {
  321. .name = "bt856",
  322. },
  323. .id = I2C_DRIVERID_BT856,
  324. .attach_adapter = bt856_attach_adapter,
  325. .detach_client = bt856_detach_client,
  326. .command = bt856_command,
  327. };
  328. static int __init
  329. bt856_init (void)
  330. {
  331. return i2c_add_driver(&i2c_driver_bt856);
  332. }
  333. static void __exit
  334. bt856_exit (void)
  335. {
  336. i2c_del_driver(&i2c_driver_bt856);
  337. }
  338. module_init(bt856_init);
  339. module_exit(bt856_exit);