bt856.c 9.1 KB

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