bt856.c 9.3 KB

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