saa7185.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * saa7185 - Philips SAA7185B video encoder driver version 0.0.3
  3. *
  4. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  5. *
  6. * Slight changes for video timing and attachment output by
  7. * Wolfgang Scherr <scherr@net4you.net>
  8. *
  9. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  10. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/errno.h>
  30. #include <linux/fs.h>
  31. #include <linux/kernel.h>
  32. #include <linux/major.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/pci.h>
  36. #include <linux/signal.h>
  37. #include <asm/io.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/page.h>
  40. #include <linux/sched.h>
  41. #include <asm/segment.h>
  42. #include <linux/types.h>
  43. #include <linux/videodev.h>
  44. #include <asm/uaccess.h>
  45. MODULE_DESCRIPTION("Philips SAA7185 video encoder driver");
  46. MODULE_AUTHOR("Dave Perks");
  47. MODULE_LICENSE("GPL");
  48. #include <linux/i2c.h>
  49. #include <linux/i2c-dev.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. struct saa7185 {
  62. unsigned char reg[128];
  63. int norm;
  64. int enable;
  65. int bright;
  66. int contrast;
  67. int hue;
  68. int sat;
  69. };
  70. #define I2C_SAA7185 0x88
  71. /* ----------------------------------------------------------------------- */
  72. static inline int
  73. saa7185_read (struct i2c_client *client)
  74. {
  75. return i2c_smbus_read_byte(client);
  76. }
  77. static int
  78. saa7185_write (struct i2c_client *client,
  79. u8 reg,
  80. u8 value)
  81. {
  82. struct saa7185 *encoder = i2c_get_clientdata(client);
  83. dprintk(1, KERN_DEBUG "SAA7185: %02x set to %02x\n", reg, value);
  84. encoder->reg[reg] = value;
  85. return i2c_smbus_write_byte_data(client, reg, value);
  86. }
  87. static int
  88. saa7185_write_block (struct i2c_client *client,
  89. const u8 *data,
  90. unsigned int len)
  91. {
  92. int ret = -1;
  93. u8 reg;
  94. /* the adv7175 has an autoincrement function, use it if
  95. * the adapter understands raw I2C */
  96. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  97. /* do raw I2C, not smbus compatible */
  98. struct saa7185 *encoder = i2c_get_clientdata(client);
  99. struct i2c_msg msg;
  100. u8 block_data[32];
  101. msg.addr = client->addr;
  102. msg.flags = 0;
  103. while (len >= 2) {
  104. msg.buf = (char *) block_data;
  105. msg.len = 0;
  106. block_data[msg.len++] = reg = data[0];
  107. do {
  108. block_data[msg.len++] =
  109. encoder->reg[reg++] = data[1];
  110. len -= 2;
  111. data += 2;
  112. } while (len >= 2 && data[0] == reg &&
  113. msg.len < 32);
  114. if ((ret = i2c_transfer(client->adapter,
  115. &msg, 1)) < 0)
  116. break;
  117. }
  118. } else {
  119. /* do some slow I2C emulation kind of thing */
  120. while (len >= 2) {
  121. reg = *data++;
  122. if ((ret = saa7185_write(client, reg,
  123. *data++)) < 0)
  124. break;
  125. len -= 2;
  126. }
  127. }
  128. return ret;
  129. }
  130. /* ----------------------------------------------------------------------- */
  131. static const unsigned char init_common[] = {
  132. 0x3a, 0x0f, /* CBENB=0, V656=0, VY2C=1,
  133. * YUV2C=1, MY2C=1, MUV2C=1 */
  134. 0x42, 0x6b, /* OVLY0=107 */
  135. 0x43, 0x00, /* OVLU0=0 white */
  136. 0x44, 0x00, /* OVLV0=0 */
  137. 0x45, 0x22, /* OVLY1=34 */
  138. 0x46, 0xac, /* OVLU1=172 yellow */
  139. 0x47, 0x0e, /* OVLV1=14 */
  140. 0x48, 0x03, /* OVLY2=3 */
  141. 0x49, 0x1d, /* OVLU2=29 cyan */
  142. 0x4a, 0xac, /* OVLV2=172 */
  143. 0x4b, 0xf0, /* OVLY3=240 */
  144. 0x4c, 0xc8, /* OVLU3=200 green */
  145. 0x4d, 0xb9, /* OVLV3=185 */
  146. 0x4e, 0xd4, /* OVLY4=212 */
  147. 0x4f, 0x38, /* OVLU4=56 magenta */
  148. 0x50, 0x47, /* OVLV4=71 */
  149. 0x51, 0xc1, /* OVLY5=193 */
  150. 0x52, 0xe3, /* OVLU5=227 red */
  151. 0x53, 0x54, /* OVLV5=84 */
  152. 0x54, 0xa3, /* OVLY6=163 */
  153. 0x55, 0x54, /* OVLU6=84 blue */
  154. 0x56, 0xf2, /* OVLV6=242 */
  155. 0x57, 0x90, /* OVLY7=144 */
  156. 0x58, 0x00, /* OVLU7=0 black */
  157. 0x59, 0x00, /* OVLV7=0 */
  158. 0x5a, 0x00, /* CHPS=0 */
  159. 0x5b, 0x76, /* GAINU=118 */
  160. 0x5c, 0xa5, /* GAINV=165 */
  161. 0x5d, 0x3c, /* BLCKL=60 */
  162. 0x5e, 0x3a, /* BLNNL=58 */
  163. 0x5f, 0x3a, /* CCRS=0, BLNVB=58 */
  164. 0x60, 0x00, /* NULL */
  165. /* 0x61 - 0x66 set according to norm */
  166. 0x67, 0x00, /* 0 : caption 1st byte odd field */
  167. 0x68, 0x00, /* 0 : caption 2nd byte odd field */
  168. 0x69, 0x00, /* 0 : caption 1st byte even field */
  169. 0x6a, 0x00, /* 0 : caption 2nd byte even field */
  170. 0x6b, 0x91, /* MODIN=2, PCREF=0, SCCLN=17 */
  171. 0x6c, 0x20, /* SRCV1=0, TRCV2=1, ORCV1=0, PRCV1=0,
  172. * CBLF=0, ORCV2=0, PRCV2=0 */
  173. 0x6d, 0x00, /* SRCM1=0, CCEN=0 */
  174. 0x6e, 0x0e, /* HTRIG=0x005, approx. centered, at
  175. * least for PAL */
  176. 0x6f, 0x00, /* HTRIG upper bits */
  177. 0x70, 0x20, /* PHRES=0, SBLN=1, VTRIG=0 */
  178. /* The following should not be needed */
  179. 0x71, 0x15, /* BMRQ=0x115 */
  180. 0x72, 0x90, /* EMRQ=0x690 */
  181. 0x73, 0x61, /* EMRQ=0x690, BMRQ=0x115 */
  182. 0x74, 0x00, /* NULL */
  183. 0x75, 0x00, /* NULL */
  184. 0x76, 0x00, /* NULL */
  185. 0x77, 0x15, /* BRCV=0x115 */
  186. 0x78, 0x90, /* ERCV=0x690 */
  187. 0x79, 0x61, /* ERCV=0x690, BRCV=0x115 */
  188. /* Field length controls */
  189. 0x7a, 0x70, /* FLC=0 */
  190. /* The following should not be needed if SBLN = 1 */
  191. 0x7b, 0x16, /* FAL=22 */
  192. 0x7c, 0x35, /* LAL=244 */
  193. 0x7d, 0x20, /* LAL=244, FAL=22 */
  194. };
  195. static const unsigned char init_pal[] = {
  196. 0x61, 0x1e, /* FISE=0, PAL=1, SCBW=1, RTCE=1,
  197. * YGS=1, INPI=0, DOWN=0 */
  198. 0x62, 0xc8, /* DECTYP=1, BSTA=72 */
  199. 0x63, 0xcb, /* FSC0 */
  200. 0x64, 0x8a, /* FSC1 */
  201. 0x65, 0x09, /* FSC2 */
  202. 0x66, 0x2a, /* FSC3 */
  203. };
  204. static const unsigned char init_ntsc[] = {
  205. 0x61, 0x1d, /* FISE=1, PAL=0, SCBW=1, RTCE=1,
  206. * YGS=1, INPI=0, DOWN=0 */
  207. 0x62, 0xe6, /* DECTYP=1, BSTA=102 */
  208. 0x63, 0x1f, /* FSC0 */
  209. 0x64, 0x7c, /* FSC1 */
  210. 0x65, 0xf0, /* FSC2 */
  211. 0x66, 0x21, /* FSC3 */
  212. };
  213. static int
  214. saa7185_command (struct i2c_client *client,
  215. unsigned int cmd,
  216. void *arg)
  217. {
  218. struct saa7185 *encoder = i2c_get_clientdata(client);
  219. switch (cmd) {
  220. case 0:
  221. saa7185_write_block(client, init_common,
  222. sizeof(init_common));
  223. switch (encoder->norm) {
  224. case VIDEO_MODE_NTSC:
  225. saa7185_write_block(client, init_ntsc,
  226. sizeof(init_ntsc));
  227. break;
  228. case VIDEO_MODE_PAL:
  229. saa7185_write_block(client, init_pal,
  230. sizeof(init_pal));
  231. break;
  232. }
  233. break;
  234. case ENCODER_GET_CAPABILITIES:
  235. {
  236. struct video_encoder_capability *cap = arg;
  237. cap->flags =
  238. VIDEO_ENCODER_PAL | VIDEO_ENCODER_NTSC |
  239. VIDEO_ENCODER_SECAM | VIDEO_ENCODER_CCIR;
  240. cap->inputs = 1;
  241. cap->outputs = 1;
  242. }
  243. break;
  244. case ENCODER_SET_NORM:
  245. {
  246. int *iarg = arg;
  247. //saa7185_write_block(client, init_common, sizeof(init_common));
  248. switch (*iarg) {
  249. case VIDEO_MODE_NTSC:
  250. saa7185_write_block(client, init_ntsc,
  251. sizeof(init_ntsc));
  252. break;
  253. case VIDEO_MODE_PAL:
  254. saa7185_write_block(client, init_pal,
  255. sizeof(init_pal));
  256. break;
  257. case VIDEO_MODE_SECAM:
  258. default:
  259. return -EINVAL;
  260. }
  261. encoder->norm = *iarg;
  262. }
  263. break;
  264. case ENCODER_SET_INPUT:
  265. {
  266. int *iarg = arg;
  267. /* RJ: *iarg = 0: input is from SA7111
  268. *iarg = 1: input is from ZR36060 */
  269. switch (*iarg) {
  270. case 0:
  271. /* Switch RTCE to 1 */
  272. saa7185_write(client, 0x61,
  273. (encoder->reg[0x61] & 0xf7) | 0x08);
  274. saa7185_write(client, 0x6e, 0x01);
  275. break;
  276. case 1:
  277. /* Switch RTCE to 0 */
  278. saa7185_write(client, 0x61,
  279. (encoder->reg[0x61] & 0xf7) | 0x00);
  280. /* SW: a slight sync problem... */
  281. saa7185_write(client, 0x6e, 0x00);
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. }
  287. break;
  288. case ENCODER_SET_OUTPUT:
  289. {
  290. int *iarg = arg;
  291. /* not much choice of outputs */
  292. if (*iarg != 0) {
  293. return -EINVAL;
  294. }
  295. }
  296. break;
  297. case ENCODER_ENABLE_OUTPUT:
  298. {
  299. int *iarg = arg;
  300. encoder->enable = !!*iarg;
  301. saa7185_write(client, 0x61,
  302. (encoder->reg[0x61] & 0xbf) |
  303. (encoder->enable ? 0x00 : 0x40));
  304. }
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. /* ----------------------------------------------------------------------- */
  312. /*
  313. * Generic i2c probe
  314. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  315. */
  316. static unsigned short normal_i2c[] = { I2C_SAA7185 >> 1, I2C_CLIENT_END };
  317. static unsigned short ignore = I2C_CLIENT_END;
  318. static struct i2c_client_address_data addr_data = {
  319. .normal_i2c = normal_i2c,
  320. .probe = &ignore,
  321. .ignore = &ignore,
  322. .force = &ignore,
  323. };
  324. static struct i2c_driver i2c_driver_saa7185;
  325. static int
  326. saa7185_detect_client (struct i2c_adapter *adapter,
  327. int address,
  328. int kind)
  329. {
  330. int i;
  331. struct i2c_client *client;
  332. struct saa7185 *encoder;
  333. dprintk(1,
  334. KERN_INFO
  335. "saa7185.c: detecting saa7185 client on address 0x%x\n",
  336. address << 1);
  337. /* Check if the adapter supports the needed features */
  338. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  339. return 0;
  340. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  341. if (client == 0)
  342. return -ENOMEM;
  343. memset(client, 0, sizeof(struct i2c_client));
  344. client->addr = address;
  345. client->adapter = adapter;
  346. client->driver = &i2c_driver_saa7185;
  347. client->flags = I2C_CLIENT_ALLOW_USE;
  348. strlcpy(I2C_NAME(client), "saa7185", sizeof(I2C_NAME(client)));
  349. encoder = kmalloc(sizeof(struct saa7185), GFP_KERNEL);
  350. if (encoder == NULL) {
  351. kfree(client);
  352. return -ENOMEM;
  353. }
  354. memset(encoder, 0, sizeof(struct saa7185));
  355. encoder->norm = VIDEO_MODE_NTSC;
  356. encoder->enable = 1;
  357. i2c_set_clientdata(client, encoder);
  358. i = i2c_attach_client(client);
  359. if (i) {
  360. kfree(client);
  361. kfree(encoder);
  362. return i;
  363. }
  364. i = saa7185_write_block(client, init_common, sizeof(init_common));
  365. if (i >= 0) {
  366. i = saa7185_write_block(client, init_ntsc,
  367. sizeof(init_ntsc));
  368. }
  369. if (i < 0) {
  370. dprintk(1, KERN_ERR "%s_attach: init error %d\n",
  371. I2C_NAME(client), i);
  372. } else {
  373. dprintk(1,
  374. KERN_INFO
  375. "%s_attach: chip version %d at address 0x%x\n",
  376. I2C_NAME(client), saa7185_read(client) >> 5,
  377. client->addr << 1);
  378. }
  379. return 0;
  380. }
  381. static int
  382. saa7185_attach_adapter (struct i2c_adapter *adapter)
  383. {
  384. dprintk(1,
  385. KERN_INFO
  386. "saa7185.c: starting probe for adapter %s (0x%x)\n",
  387. I2C_NAME(adapter), adapter->id);
  388. return i2c_probe(adapter, &addr_data, &saa7185_detect_client);
  389. }
  390. static int
  391. saa7185_detach_client (struct i2c_client *client)
  392. {
  393. struct saa7185 *encoder = i2c_get_clientdata(client);
  394. int err;
  395. err = i2c_detach_client(client);
  396. if (err) {
  397. return err;
  398. }
  399. saa7185_write(client, 0x61, (encoder->reg[0x61]) | 0x40); /* SW: output off is active */
  400. //saa7185_write(client, 0x3a, (encoder->reg[0x3a]) | 0x80); /* SW: color bar */
  401. kfree(encoder);
  402. kfree(client);
  403. return 0;
  404. }
  405. /* ----------------------------------------------------------------------- */
  406. static struct i2c_driver i2c_driver_saa7185 = {
  407. .owner = THIS_MODULE,
  408. .name = "saa7185", /* name */
  409. .id = I2C_DRIVERID_SAA7185B,
  410. .flags = I2C_DF_NOTIFY,
  411. .attach_adapter = saa7185_attach_adapter,
  412. .detach_client = saa7185_detach_client,
  413. .command = saa7185_command,
  414. };
  415. static int __init
  416. saa7185_init (void)
  417. {
  418. return i2c_add_driver(&i2c_driver_saa7185);
  419. }
  420. static void __exit
  421. saa7185_exit (void)
  422. {
  423. i2c_del_driver(&i2c_driver_saa7185);
  424. }
  425. module_init(saa7185_init);
  426. module_exit(saa7185_exit);