bt856.c 9.3 KB

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