bt856.c 9.1 KB

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