bt856.c 9.0 KB

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