saa7114.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * saa7114 - Philips SAA7114H video decoder driver version 0.0.1
  3. *
  4. * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
  5. *
  6. * Based on saa7111 driver by Dave Perks
  7. *
  8. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  9. *
  10. * Slight changes for video timing and attachment output by
  11. * Wolfgang Scherr <scherr@net4you.net>
  12. *
  13. * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
  14. * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
  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/types.h>
  32. #include <linux/ioctl.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/i2c.h>
  35. #include <linux/i2c-id.h>
  36. #include <linux/videodev.h>
  37. #include <linux/video_decoder.h>
  38. #include <media/v4l2-common.h>
  39. #include <media/v4l2-i2c-drv-legacy.h>
  40. MODULE_DESCRIPTION("Philips SAA7114H video decoder driver");
  41. MODULE_AUTHOR("Maxim Yevtyushkin");
  42. MODULE_LICENSE("GPL");
  43. static int debug;
  44. module_param(debug, int, 0);
  45. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  46. /* ----------------------------------------------------------------------- */
  47. struct saa7114 {
  48. unsigned char reg[0xf0 * 2];
  49. int norm;
  50. int input;
  51. int enable;
  52. int bright;
  53. int contrast;
  54. int hue;
  55. int sat;
  56. int playback;
  57. };
  58. #define I2C_DELAY 10
  59. //#define SAA_7114_NTSC_HSYNC_START (-3)
  60. //#define SAA_7114_NTSC_HSYNC_STOP (-18)
  61. #define SAA_7114_NTSC_HSYNC_START (-17)
  62. #define SAA_7114_NTSC_HSYNC_STOP (-32)
  63. //#define SAA_7114_NTSC_HOFFSET (5)
  64. #define SAA_7114_NTSC_HOFFSET (6)
  65. #define SAA_7114_NTSC_VOFFSET (10)
  66. #define SAA_7114_NTSC_WIDTH (720)
  67. #define SAA_7114_NTSC_HEIGHT (250)
  68. #define SAA_7114_SECAM_HSYNC_START (-17)
  69. #define SAA_7114_SECAM_HSYNC_STOP (-32)
  70. #define SAA_7114_SECAM_HOFFSET (2)
  71. #define SAA_7114_SECAM_VOFFSET (10)
  72. #define SAA_7114_SECAM_WIDTH (720)
  73. #define SAA_7114_SECAM_HEIGHT (300)
  74. #define SAA_7114_PAL_HSYNC_START (-17)
  75. #define SAA_7114_PAL_HSYNC_STOP (-32)
  76. #define SAA_7114_PAL_HOFFSET (2)
  77. #define SAA_7114_PAL_VOFFSET (10)
  78. #define SAA_7114_PAL_WIDTH (720)
  79. #define SAA_7114_PAL_HEIGHT (300)
  80. #define SAA_7114_VERTICAL_CHROMA_OFFSET 0 //0x50504040
  81. #define SAA_7114_VERTICAL_LUMA_OFFSET 0
  82. #define REG_ADDR(x) (((x) << 1) + 1)
  83. #define LOBYTE(x) ((unsigned char)((x) & 0xff))
  84. #define HIBYTE(x) ((unsigned char)(((x) >> 8) & 0xff))
  85. #define LOWORD(x) ((unsigned short int)((x) & 0xffff))
  86. #define HIWORD(x) ((unsigned short int)(((x) >> 16) & 0xffff))
  87. /* ----------------------------------------------------------------------- */
  88. static inline int saa7114_write(struct i2c_client *client, u8 reg, u8 value)
  89. {
  90. return i2c_smbus_write_byte_data(client, reg, value);
  91. }
  92. static int saa7114_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
  93. {
  94. int ret = -1;
  95. u8 reg;
  96. /* the saa7114 has an autoincrement function, use it if
  97. * the adapter understands raw I2C */
  98. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  99. /* do raw I2C, not smbus compatible */
  100. u8 block_data[32];
  101. int block_len;
  102. while (len >= 2) {
  103. block_len = 0;
  104. block_data[block_len++] = reg = data[0];
  105. do {
  106. block_data[block_len++] = data[1];
  107. reg++;
  108. len -= 2;
  109. data += 2;
  110. } while (len >= 2 && data[0] == reg && block_len < 32);
  111. ret = i2c_master_send(client, block_data, block_len);
  112. if (ret < 0)
  113. break;
  114. }
  115. } else {
  116. /* do some slow I2C emulation kind of thing */
  117. while (len >= 2) {
  118. reg = *data++;
  119. ret = saa7114_write(client, reg, *data++);
  120. if (ret < 0)
  121. break;
  122. len -= 2;
  123. }
  124. }
  125. return ret;
  126. }
  127. static inline int saa7114_read(struct i2c_client *client, u8 reg)
  128. {
  129. return i2c_smbus_read_byte_data(client, reg);
  130. }
  131. /* ----------------------------------------------------------------------- */
  132. // initially set NTSC, composite
  133. static const unsigned char init[] = {
  134. 0x00, 0x00, /* 00 - ID byte , chip version,
  135. * read only */
  136. 0x01, 0x08, /* 01 - X,X,X,X, IDEL3 to IDEL0 -
  137. * horizontal increment delay,
  138. * recommended position */
  139. 0x02, 0x00, /* 02 - FUSE=3, GUDL=2, MODE=0 ;
  140. * input control */
  141. 0x03, 0x10, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
  142. * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
  143. 0x04, 0x90, /* 04 - GAI1=256 */
  144. 0x05, 0x90, /* 05 - GAI2=256 */
  145. 0x06, SAA_7114_NTSC_HSYNC_START, /* 06 - HSB: hsync start,
  146. * depends on the video standard */
  147. 0x07, SAA_7114_NTSC_HSYNC_STOP, /* 07 - HSS: hsync stop, depends
  148. *on the video standard */
  149. 0x08, 0xb8, /* 08 - AUFD=1, FSEL=1, EXFIL=0, VTRC=1,
  150. * HPLL: free running in playback, locked
  151. * in capture, VNOI=0 */
  152. 0x09, 0x80, /* 09 - BYPS=0, PREF=0, BPSS=0, VBLB=0,
  153. * UPTCV=0, APER=1; depends from input */
  154. 0x0a, 0x80, /* 0a - BRIG=128 */
  155. 0x0b, 0x44, /* 0b - CONT=1.109 */
  156. 0x0c, 0x40, /* 0c - SATN=1.0 */
  157. 0x0d, 0x00, /* 0d - HUE=0 */
  158. 0x0e, 0x84, /* 0e - CDTO, CSTD2 to 0, DCVF, FCTC,
  159. * CCOMB; depends from video standard */
  160. 0x0f, 0x24, /* 0f - ACGC,CGAIN6 to CGAIN0; depends
  161. * from video standard */
  162. 0x10, 0x03, /* 10 - OFFU1 to 0, OFFV1 to 0, CHBW,
  163. * LCBW2 to 0 */
  164. 0x11, 0x59, /* 11 - COLO, RTP1, HEDL1 to 0, RTP0,
  165. * YDEL2 to 0 */
  166. 0x12, 0xc9, /* 12 - RT signal control RTSE13 to 10
  167. * and 03 to 00 */
  168. 0x13, 0x80, /* 13 - RT/X port output control */
  169. 0x14, 0x00, /* 14 - analog, ADC, compatibility control */
  170. 0x15, 0x00, /* 15 - VGATE start FID change */
  171. 0x16, 0xfe, /* 16 - VGATE stop */
  172. 0x17, 0x00, /* 17 - Misc., VGATE MSBs */
  173. 0x18, 0x40, /* RAWG */
  174. 0x19, 0x80, /* RAWO */
  175. 0x1a, 0x00,
  176. 0x1b, 0x00,
  177. 0x1c, 0x00,
  178. 0x1d, 0x00,
  179. 0x1e, 0x00,
  180. 0x1f, 0x00, /* status byte, read only */
  181. 0x20, 0x00, /* video decoder reserved part */
  182. 0x21, 0x00,
  183. 0x22, 0x00,
  184. 0x23, 0x00,
  185. 0x24, 0x00,
  186. 0x25, 0x00,
  187. 0x26, 0x00,
  188. 0x27, 0x00,
  189. 0x28, 0x00,
  190. 0x29, 0x00,
  191. 0x2a, 0x00,
  192. 0x2b, 0x00,
  193. 0x2c, 0x00,
  194. 0x2d, 0x00,
  195. 0x2e, 0x00,
  196. 0x2f, 0x00,
  197. 0x30, 0xbc, /* audio clock generator */
  198. 0x31, 0xdf,
  199. 0x32, 0x02,
  200. 0x33, 0x00,
  201. 0x34, 0xcd,
  202. 0x35, 0xcc,
  203. 0x36, 0x3a,
  204. 0x37, 0x00,
  205. 0x38, 0x03,
  206. 0x39, 0x10,
  207. 0x3a, 0x00,
  208. 0x3b, 0x00,
  209. 0x3c, 0x00,
  210. 0x3d, 0x00,
  211. 0x3e, 0x00,
  212. 0x3f, 0x00,
  213. 0x40, 0x00, /* VBI data slicer */
  214. 0x41, 0xff,
  215. 0x42, 0xff,
  216. 0x43, 0xff,
  217. 0x44, 0xff,
  218. 0x45, 0xff,
  219. 0x46, 0xff,
  220. 0x47, 0xff,
  221. 0x48, 0xff,
  222. 0x49, 0xff,
  223. 0x4a, 0xff,
  224. 0x4b, 0xff,
  225. 0x4c, 0xff,
  226. 0x4d, 0xff,
  227. 0x4e, 0xff,
  228. 0x4f, 0xff,
  229. 0x50, 0xff,
  230. 0x51, 0xff,
  231. 0x52, 0xff,
  232. 0x53, 0xff,
  233. 0x54, 0xff,
  234. 0x55, 0xff,
  235. 0x56, 0xff,
  236. 0x57, 0xff,
  237. 0x58, 0x40, // framing code
  238. 0x59, 0x47, // horizontal offset
  239. 0x5a, 0x06, // vertical offset
  240. 0x5b, 0x83, // field offset
  241. 0x5c, 0x00, // reserved
  242. 0x5d, 0x3e, // header and data
  243. 0x5e, 0x00, // sliced data
  244. 0x5f, 0x00, // reserved
  245. 0x60, 0x00, /* video decoder reserved part */
  246. 0x61, 0x00,
  247. 0x62, 0x00,
  248. 0x63, 0x00,
  249. 0x64, 0x00,
  250. 0x65, 0x00,
  251. 0x66, 0x00,
  252. 0x67, 0x00,
  253. 0x68, 0x00,
  254. 0x69, 0x00,
  255. 0x6a, 0x00,
  256. 0x6b, 0x00,
  257. 0x6c, 0x00,
  258. 0x6d, 0x00,
  259. 0x6e, 0x00,
  260. 0x6f, 0x00,
  261. 0x70, 0x00, /* video decoder reserved part */
  262. 0x71, 0x00,
  263. 0x72, 0x00,
  264. 0x73, 0x00,
  265. 0x74, 0x00,
  266. 0x75, 0x00,
  267. 0x76, 0x00,
  268. 0x77, 0x00,
  269. 0x78, 0x00,
  270. 0x79, 0x00,
  271. 0x7a, 0x00,
  272. 0x7b, 0x00,
  273. 0x7c, 0x00,
  274. 0x7d, 0x00,
  275. 0x7e, 0x00,
  276. 0x7f, 0x00,
  277. 0x80, 0x00, /* X-port, I-port and scaler */
  278. 0x81, 0x00,
  279. 0x82, 0x00,
  280. 0x83, 0x00,
  281. 0x84, 0xc5,
  282. 0x85, 0x0d, // hsync and vsync ?
  283. 0x86, 0x40,
  284. 0x87, 0x01,
  285. 0x88, 0x00,
  286. 0x89, 0x00,
  287. 0x8a, 0x00,
  288. 0x8b, 0x00,
  289. 0x8c, 0x00,
  290. 0x8d, 0x00,
  291. 0x8e, 0x00,
  292. 0x8f, 0x00,
  293. 0x90, 0x03, /* Task A definition */
  294. 0x91, 0x08,
  295. 0x92, 0x00,
  296. 0x93, 0x40,
  297. 0x94, 0x00, // window settings
  298. 0x95, 0x00,
  299. 0x96, 0x00,
  300. 0x97, 0x00,
  301. 0x98, 0x00,
  302. 0x99, 0x00,
  303. 0x9a, 0x00,
  304. 0x9b, 0x00,
  305. 0x9c, 0x00,
  306. 0x9d, 0x00,
  307. 0x9e, 0x00,
  308. 0x9f, 0x00,
  309. 0xa0, 0x01, /* horizontal integer prescaling ratio */
  310. 0xa1, 0x00, /* horizontal prescaler accumulation
  311. * sequence length */
  312. 0xa2, 0x00, /* UV FIR filter, Y FIR filter, prescaler
  313. * DC gain */
  314. 0xa3, 0x00,
  315. 0xa4, 0x80, // luminance brightness
  316. 0xa5, 0x40, // luminance gain
  317. 0xa6, 0x40, // chrominance saturation
  318. 0xa7, 0x00,
  319. 0xa8, 0x00, // horizontal luminance scaling increment
  320. 0xa9, 0x04,
  321. 0xaa, 0x00, // horizontal luminance phase offset
  322. 0xab, 0x00,
  323. 0xac, 0x00, // horizontal chrominance scaling increment
  324. 0xad, 0x02,
  325. 0xae, 0x00, // horizontal chrominance phase offset
  326. 0xaf, 0x00,
  327. 0xb0, 0x00, // vertical luminance scaling increment
  328. 0xb1, 0x04,
  329. 0xb2, 0x00, // vertical chrominance scaling increment
  330. 0xb3, 0x04,
  331. 0xb4, 0x00,
  332. 0xb5, 0x00,
  333. 0xb6, 0x00,
  334. 0xb7, 0x00,
  335. 0xb8, 0x00,
  336. 0xb9, 0x00,
  337. 0xba, 0x00,
  338. 0xbb, 0x00,
  339. 0xbc, 0x00,
  340. 0xbd, 0x00,
  341. 0xbe, 0x00,
  342. 0xbf, 0x00,
  343. 0xc0, 0x02, // Task B definition
  344. 0xc1, 0x08,
  345. 0xc2, 0x00,
  346. 0xc3, 0x40,
  347. 0xc4, 0x00, // window settings
  348. 0xc5, 0x00,
  349. 0xc6, 0x00,
  350. 0xc7, 0x00,
  351. 0xc8, 0x00,
  352. 0xc9, 0x00,
  353. 0xca, 0x00,
  354. 0xcb, 0x00,
  355. 0xcc, 0x00,
  356. 0xcd, 0x00,
  357. 0xce, 0x00,
  358. 0xcf, 0x00,
  359. 0xd0, 0x01, // horizontal integer prescaling ratio
  360. 0xd1, 0x00, // horizontal prescaler accumulation sequence length
  361. 0xd2, 0x00, // UV FIR filter, Y FIR filter, prescaler DC gain
  362. 0xd3, 0x00,
  363. 0xd4, 0x80, // luminance brightness
  364. 0xd5, 0x40, // luminance gain
  365. 0xd6, 0x40, // chrominance saturation
  366. 0xd7, 0x00,
  367. 0xd8, 0x00, // horizontal luminance scaling increment
  368. 0xd9, 0x04,
  369. 0xda, 0x00, // horizontal luminance phase offset
  370. 0xdb, 0x00,
  371. 0xdc, 0x00, // horizontal chrominance scaling increment
  372. 0xdd, 0x02,
  373. 0xde, 0x00, // horizontal chrominance phase offset
  374. 0xdf, 0x00,
  375. 0xe0, 0x00, // vertical luminance scaling increment
  376. 0xe1, 0x04,
  377. 0xe2, 0x00, // vertical chrominance scaling increment
  378. 0xe3, 0x04,
  379. 0xe4, 0x00,
  380. 0xe5, 0x00,
  381. 0xe6, 0x00,
  382. 0xe7, 0x00,
  383. 0xe8, 0x00,
  384. 0xe9, 0x00,
  385. 0xea, 0x00,
  386. 0xeb, 0x00,
  387. 0xec, 0x00,
  388. 0xed, 0x00,
  389. 0xee, 0x00,
  390. 0xef, 0x00
  391. };
  392. static int saa7114_command(struct i2c_client *client, unsigned cmd, void *arg)
  393. {
  394. struct saa7114 *decoder = i2c_get_clientdata(client);
  395. switch (cmd) {
  396. case 0:
  397. //dprintk(1, KERN_INFO "%s: writing init\n", I2C_NAME(client));
  398. //saa7114_write_block(client, init, sizeof(init));
  399. break;
  400. case DECODER_DUMP:
  401. {
  402. int i;
  403. if (!debug)
  404. break;
  405. v4l_info(client, "decoder dump\n");
  406. for (i = 0; i < 32; i += 16) {
  407. int j;
  408. v4l_info(client, "%03x", i);
  409. for (j = 0; j < 16; ++j) {
  410. printk(KERN_CONT " %02x",
  411. saa7114_read(client, i + j));
  412. }
  413. printk(KERN_CONT "\n");
  414. }
  415. break;
  416. }
  417. case DECODER_GET_CAPABILITIES:
  418. {
  419. struct video_decoder_capability *cap = arg;
  420. v4l_dbg(1, debug, client, "get capabilities\n");
  421. cap->flags = VIDEO_DECODER_PAL |
  422. VIDEO_DECODER_NTSC |
  423. VIDEO_DECODER_AUTO |
  424. VIDEO_DECODER_CCIR;
  425. cap->inputs = 8;
  426. cap->outputs = 1;
  427. break;
  428. }
  429. case DECODER_GET_STATUS:
  430. {
  431. int *iarg = arg;
  432. int status;
  433. int res;
  434. status = saa7114_read(client, 0x1f);
  435. v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
  436. res = 0;
  437. if ((status & (1 << 6)) == 0) {
  438. res |= DECODER_STATUS_GOOD;
  439. }
  440. switch (decoder->norm) {
  441. case VIDEO_MODE_NTSC:
  442. res |= DECODER_STATUS_NTSC;
  443. break;
  444. case VIDEO_MODE_PAL:
  445. res |= DECODER_STATUS_PAL;
  446. break;
  447. case VIDEO_MODE_SECAM:
  448. res |= DECODER_STATUS_SECAM;
  449. break;
  450. default:
  451. case VIDEO_MODE_AUTO:
  452. if ((status & (1 << 5)) != 0) {
  453. res |= DECODER_STATUS_NTSC;
  454. } else {
  455. res |= DECODER_STATUS_PAL;
  456. }
  457. break;
  458. }
  459. if ((status & (1 << 0)) != 0) {
  460. res |= DECODER_STATUS_COLOR;
  461. }
  462. *iarg = res;
  463. break;
  464. }
  465. case DECODER_SET_NORM:
  466. {
  467. int *iarg = arg;
  468. short int hoff = 0, voff = 0, w = 0, h = 0;
  469. v4l_dbg(1, debug, client, "set norm\n");
  470. switch (*iarg) {
  471. case VIDEO_MODE_NTSC:
  472. v4l_dbg(1, debug, client, "NTSC\n");
  473. decoder->reg[REG_ADDR(0x06)] =
  474. SAA_7114_NTSC_HSYNC_START;
  475. decoder->reg[REG_ADDR(0x07)] =
  476. SAA_7114_NTSC_HSYNC_STOP;
  477. decoder->reg[REG_ADDR(0x08)] = decoder->playback ? 0x7c : 0xb8; // PLL free when playback, PLL close when capture
  478. decoder->reg[REG_ADDR(0x0e)] = 0x85;
  479. decoder->reg[REG_ADDR(0x0f)] = 0x24;
  480. hoff = SAA_7114_NTSC_HOFFSET;
  481. voff = SAA_7114_NTSC_VOFFSET;
  482. w = SAA_7114_NTSC_WIDTH;
  483. h = SAA_7114_NTSC_HEIGHT;
  484. break;
  485. case VIDEO_MODE_PAL:
  486. v4l_dbg(1, debug, client, "PAL\n");
  487. decoder->reg[REG_ADDR(0x06)] =
  488. SAA_7114_PAL_HSYNC_START;
  489. decoder->reg[REG_ADDR(0x07)] =
  490. SAA_7114_PAL_HSYNC_STOP;
  491. decoder->reg[REG_ADDR(0x08)] = decoder->playback ? 0x7c : 0xb8; // PLL free when playback, PLL close when capture
  492. decoder->reg[REG_ADDR(0x0e)] = 0x81;
  493. decoder->reg[REG_ADDR(0x0f)] = 0x24;
  494. hoff = SAA_7114_PAL_HOFFSET;
  495. voff = SAA_7114_PAL_VOFFSET;
  496. w = SAA_7114_PAL_WIDTH;
  497. h = SAA_7114_PAL_HEIGHT;
  498. break;
  499. default:
  500. v4l_dbg(1, debug, client, "Unknown video mode\n");
  501. return -EINVAL;
  502. }
  503. decoder->reg[REG_ADDR(0x94)] = LOBYTE(hoff); // hoffset low
  504. decoder->reg[REG_ADDR(0x95)] = HIBYTE(hoff) & 0x0f; // hoffset high
  505. decoder->reg[REG_ADDR(0x96)] = LOBYTE(w); // width low
  506. decoder->reg[REG_ADDR(0x97)] = HIBYTE(w) & 0x0f; // width high
  507. decoder->reg[REG_ADDR(0x98)] = LOBYTE(voff); // voffset low
  508. decoder->reg[REG_ADDR(0x99)] = HIBYTE(voff) & 0x0f; // voffset high
  509. decoder->reg[REG_ADDR(0x9a)] = LOBYTE(h + 2); // height low
  510. decoder->reg[REG_ADDR(0x9b)] = HIBYTE(h + 2) & 0x0f; // height high
  511. decoder->reg[REG_ADDR(0x9c)] = LOBYTE(w); // out width low
  512. decoder->reg[REG_ADDR(0x9d)] = HIBYTE(w) & 0x0f; // out width high
  513. decoder->reg[REG_ADDR(0x9e)] = LOBYTE(h); // out height low
  514. decoder->reg[REG_ADDR(0x9f)] = HIBYTE(h) & 0x0f; // out height high
  515. decoder->reg[REG_ADDR(0xc4)] = LOBYTE(hoff); // hoffset low
  516. decoder->reg[REG_ADDR(0xc5)] = HIBYTE(hoff) & 0x0f; // hoffset high
  517. decoder->reg[REG_ADDR(0xc6)] = LOBYTE(w); // width low
  518. decoder->reg[REG_ADDR(0xc7)] = HIBYTE(w) & 0x0f; // width high
  519. decoder->reg[REG_ADDR(0xc8)] = LOBYTE(voff); // voffset low
  520. decoder->reg[REG_ADDR(0xc9)] = HIBYTE(voff) & 0x0f; // voffset high
  521. decoder->reg[REG_ADDR(0xca)] = LOBYTE(h + 2); // height low
  522. decoder->reg[REG_ADDR(0xcb)] = HIBYTE(h + 2) & 0x0f; // height high
  523. decoder->reg[REG_ADDR(0xcc)] = LOBYTE(w); // out width low
  524. decoder->reg[REG_ADDR(0xcd)] = HIBYTE(w) & 0x0f; // out width high
  525. decoder->reg[REG_ADDR(0xce)] = LOBYTE(h); // out height low
  526. decoder->reg[REG_ADDR(0xcf)] = HIBYTE(h) & 0x0f; // out height high
  527. saa7114_write(client, 0x80, 0x06); // i-port and scaler back end clock selection, task A&B off
  528. saa7114_write(client, 0x88, 0xd8); // sw reset scaler
  529. saa7114_write(client, 0x88, 0xf8); // sw reset scaler release
  530. saa7114_write_block(client, decoder->reg + (0x06 << 1),
  531. 3 << 1);
  532. saa7114_write_block(client, decoder->reg + (0x0e << 1),
  533. 2 << 1);
  534. saa7114_write_block(client, decoder->reg + (0x5a << 1),
  535. 2 << 1);
  536. saa7114_write_block(client, decoder->reg + (0x94 << 1),
  537. (0x9f + 1 - 0x94) << 1);
  538. saa7114_write_block(client, decoder->reg + (0xc4 << 1),
  539. (0xcf + 1 - 0xc4) << 1);
  540. saa7114_write(client, 0x88, 0xd8); // sw reset scaler
  541. saa7114_write(client, 0x88, 0xf8); // sw reset scaler release
  542. saa7114_write(client, 0x80, 0x36); // i-port and scaler back end clock selection
  543. decoder->norm = *iarg;
  544. break;
  545. }
  546. case DECODER_SET_INPUT:
  547. {
  548. int *iarg = arg;
  549. v4l_dbg(1, debug, client, "set input (%d)\n", *iarg);
  550. if (*iarg < 0 || *iarg > 7) {
  551. return -EINVAL;
  552. }
  553. if (decoder->input != *iarg) {
  554. v4l_dbg(1, debug, client, "now setting %s input\n",
  555. *iarg >= 6 ? "S-Video" : "Composite");
  556. decoder->input = *iarg;
  557. /* select mode */
  558. decoder->reg[REG_ADDR(0x02)] =
  559. (decoder->
  560. reg[REG_ADDR(0x02)] & 0xf0) | (decoder->
  561. input <
  562. 6 ? 0x0 : 0x9);
  563. saa7114_write(client, 0x02,
  564. decoder->reg[REG_ADDR(0x02)]);
  565. /* bypass chrominance trap for modes 6..9 */
  566. decoder->reg[REG_ADDR(0x09)] =
  567. (decoder->
  568. reg[REG_ADDR(0x09)] & 0x7f) | (decoder->
  569. input <
  570. 6 ? 0x0 :
  571. 0x80);
  572. saa7114_write(client, 0x09,
  573. decoder->reg[REG_ADDR(0x09)]);
  574. decoder->reg[REG_ADDR(0x0e)] =
  575. decoder->input <
  576. 6 ? decoder->
  577. reg[REG_ADDR(0x0e)] | 1 : decoder->
  578. reg[REG_ADDR(0x0e)] & ~1;
  579. saa7114_write(client, 0x0e,
  580. decoder->reg[REG_ADDR(0x0e)]);
  581. }
  582. break;
  583. }
  584. case DECODER_SET_OUTPUT:
  585. {
  586. int *iarg = arg;
  587. v4l_dbg(1, debug, client, "set output\n");
  588. /* not much choice of outputs */
  589. if (*iarg != 0) {
  590. return -EINVAL;
  591. }
  592. break;
  593. }
  594. case DECODER_ENABLE_OUTPUT:
  595. {
  596. int *iarg = arg;
  597. int enable = (*iarg != 0);
  598. v4l_dbg(1, debug, client, "%s output\n",
  599. enable ? "enable" : "disable");
  600. decoder->playback = !enable;
  601. if (decoder->enable != enable) {
  602. decoder->enable = enable;
  603. /* RJ: If output should be disabled (for
  604. * playing videos), we also need a open PLL.
  605. * The input is set to 0 (where no input
  606. * source is connected), although this
  607. * is not necessary.
  608. *
  609. * If output should be enabled, we have to
  610. * reverse the above.
  611. */
  612. if (decoder->enable) {
  613. decoder->reg[REG_ADDR(0x08)] = 0xb8;
  614. decoder->reg[REG_ADDR(0x12)] = 0xc9;
  615. decoder->reg[REG_ADDR(0x13)] = 0x80;
  616. decoder->reg[REG_ADDR(0x87)] = 0x01;
  617. } else {
  618. decoder->reg[REG_ADDR(0x08)] = 0x7c;
  619. decoder->reg[REG_ADDR(0x12)] = 0x00;
  620. decoder->reg[REG_ADDR(0x13)] = 0x00;
  621. decoder->reg[REG_ADDR(0x87)] = 0x00;
  622. }
  623. saa7114_write_block(client,
  624. decoder->reg + (0x12 << 1),
  625. 2 << 1);
  626. saa7114_write(client, 0x08,
  627. decoder->reg[REG_ADDR(0x08)]);
  628. saa7114_write(client, 0x87,
  629. decoder->reg[REG_ADDR(0x87)]);
  630. saa7114_write(client, 0x88, 0xd8); // sw reset scaler
  631. saa7114_write(client, 0x88, 0xf8); // sw reset scaler release
  632. saa7114_write(client, 0x80, 0x36);
  633. }
  634. break;
  635. }
  636. case DECODER_SET_PICTURE:
  637. {
  638. struct video_picture *pic = arg;
  639. v4l_dbg(1, debug, client,
  640. "decoder set picture bright=%d contrast=%d saturation=%d hue=%d\n",
  641. pic->brightness, pic->contrast, pic->colour, pic->hue);
  642. if (decoder->bright != pic->brightness) {
  643. /* We want 0 to 255 we get 0-65535 */
  644. decoder->bright = pic->brightness;
  645. saa7114_write(client, 0x0a, decoder->bright >> 8);
  646. }
  647. if (decoder->contrast != pic->contrast) {
  648. /* We want 0 to 127 we get 0-65535 */
  649. decoder->contrast = pic->contrast;
  650. saa7114_write(client, 0x0b,
  651. decoder->contrast >> 9);
  652. }
  653. if (decoder->sat != pic->colour) {
  654. /* We want 0 to 127 we get 0-65535 */
  655. decoder->sat = pic->colour;
  656. saa7114_write(client, 0x0c, decoder->sat >> 9);
  657. }
  658. if (decoder->hue != pic->hue) {
  659. /* We want -128 to 127 we get 0-65535 */
  660. decoder->hue = pic->hue;
  661. saa7114_write(client, 0x0d,
  662. (decoder->hue - 32768) >> 8);
  663. }
  664. break;
  665. }
  666. default:
  667. return -EINVAL;
  668. }
  669. return 0;
  670. }
  671. /* ----------------------------------------------------------------------- */
  672. static unsigned short normal_i2c[] = { 0x42 >> 1, 0x40 >> 1, I2C_CLIENT_END };
  673. I2C_CLIENT_INSMOD;
  674. static int saa7114_probe(struct i2c_client *client,
  675. const struct i2c_device_id *id)
  676. {
  677. int i, err[30];
  678. short int hoff = SAA_7114_NTSC_HOFFSET;
  679. short int voff = SAA_7114_NTSC_VOFFSET;
  680. short int w = SAA_7114_NTSC_WIDTH;
  681. short int h = SAA_7114_NTSC_HEIGHT;
  682. struct saa7114 *decoder;
  683. /* Check if the adapter supports the needed features */
  684. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  685. return -ENODEV;
  686. v4l_info(client, "chip found @ 0x%x (%s)\n",
  687. client->addr << 1, client->adapter->name);
  688. decoder = kzalloc(sizeof(struct saa7114), GFP_KERNEL);
  689. if (decoder == NULL)
  690. return -ENOMEM;
  691. decoder->norm = VIDEO_MODE_NTSC;
  692. decoder->input = -1;
  693. decoder->enable = 1;
  694. decoder->bright = 32768;
  695. decoder->contrast = 32768;
  696. decoder->hue = 32768;
  697. decoder->sat = 32768;
  698. decoder->playback = 0; // initially capture mode useda
  699. i2c_set_clientdata(client, decoder);
  700. memcpy(decoder->reg, init, sizeof(init));
  701. decoder->reg[REG_ADDR(0x94)] = LOBYTE(hoff); // hoffset low
  702. decoder->reg[REG_ADDR(0x95)] = HIBYTE(hoff) & 0x0f; // hoffset high
  703. decoder->reg[REG_ADDR(0x96)] = LOBYTE(w); // width low
  704. decoder->reg[REG_ADDR(0x97)] = HIBYTE(w) & 0x0f; // width high
  705. decoder->reg[REG_ADDR(0x98)] = LOBYTE(voff); // voffset low
  706. decoder->reg[REG_ADDR(0x99)] = HIBYTE(voff) & 0x0f; // voffset high
  707. decoder->reg[REG_ADDR(0x9a)] = LOBYTE(h + 2); // height low
  708. decoder->reg[REG_ADDR(0x9b)] = HIBYTE(h + 2) & 0x0f; // height high
  709. decoder->reg[REG_ADDR(0x9c)] = LOBYTE(w); // out width low
  710. decoder->reg[REG_ADDR(0x9d)] = HIBYTE(w) & 0x0f; // out width high
  711. decoder->reg[REG_ADDR(0x9e)] = LOBYTE(h); // out height low
  712. decoder->reg[REG_ADDR(0x9f)] = HIBYTE(h) & 0x0f; // out height high
  713. decoder->reg[REG_ADDR(0xc4)] = LOBYTE(hoff); // hoffset low
  714. decoder->reg[REG_ADDR(0xc5)] = HIBYTE(hoff) & 0x0f; // hoffset high
  715. decoder->reg[REG_ADDR(0xc6)] = LOBYTE(w); // width low
  716. decoder->reg[REG_ADDR(0xc7)] = HIBYTE(w) & 0x0f; // width high
  717. decoder->reg[REG_ADDR(0xc8)] = LOBYTE(voff); // voffset low
  718. decoder->reg[REG_ADDR(0xc9)] = HIBYTE(voff) & 0x0f; // voffset high
  719. decoder->reg[REG_ADDR(0xca)] = LOBYTE(h + 2); // height low
  720. decoder->reg[REG_ADDR(0xcb)] = HIBYTE(h + 2) & 0x0f; // height high
  721. decoder->reg[REG_ADDR(0xcc)] = LOBYTE(w); // out width low
  722. decoder->reg[REG_ADDR(0xcd)] = HIBYTE(w) & 0x0f; // out width high
  723. decoder->reg[REG_ADDR(0xce)] = LOBYTE(h); // out height low
  724. decoder->reg[REG_ADDR(0xcf)] = HIBYTE(h) & 0x0f; // out height high
  725. decoder->reg[REG_ADDR(0xb8)] =
  726. LOBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  727. decoder->reg[REG_ADDR(0xb9)] =
  728. HIBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  729. decoder->reg[REG_ADDR(0xba)] =
  730. LOBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  731. decoder->reg[REG_ADDR(0xbb)] =
  732. HIBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  733. decoder->reg[REG_ADDR(0xbc)] =
  734. LOBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  735. decoder->reg[REG_ADDR(0xbd)] =
  736. HIBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  737. decoder->reg[REG_ADDR(0xbe)] =
  738. LOBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  739. decoder->reg[REG_ADDR(0xbf)] =
  740. HIBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  741. decoder->reg[REG_ADDR(0xe8)] =
  742. LOBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  743. decoder->reg[REG_ADDR(0xe9)] =
  744. HIBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  745. decoder->reg[REG_ADDR(0xea)] =
  746. LOBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  747. decoder->reg[REG_ADDR(0xeb)] =
  748. HIBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
  749. decoder->reg[REG_ADDR(0xec)] =
  750. LOBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  751. decoder->reg[REG_ADDR(0xed)] =
  752. HIBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  753. decoder->reg[REG_ADDR(0xee)] =
  754. LOBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  755. decoder->reg[REG_ADDR(0xef)] =
  756. HIBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
  757. decoder->reg[REG_ADDR(0x13)] = 0x80; // RTC0 on
  758. decoder->reg[REG_ADDR(0x87)] = 0x01; // I-Port
  759. decoder->reg[REG_ADDR(0x12)] = 0xc9; // RTS0
  760. decoder->reg[REG_ADDR(0x02)] = 0xc0; // set composite1 input, aveasy
  761. decoder->reg[REG_ADDR(0x09)] = 0x00; // chrominance trap
  762. decoder->reg[REG_ADDR(0x0e)] |= 1; // combfilter on
  763. v4l_dbg(1, debug, client, "starting init\n");
  764. err[0] =
  765. saa7114_write_block(client, decoder->reg + (0x20 << 1),
  766. 0x10 << 1);
  767. err[1] =
  768. saa7114_write_block(client, decoder->reg + (0x30 << 1),
  769. 0x10 << 1);
  770. err[2] =
  771. saa7114_write_block(client, decoder->reg + (0x63 << 1),
  772. (0x7f + 1 - 0x63) << 1);
  773. err[3] =
  774. saa7114_write_block(client, decoder->reg + (0x89 << 1),
  775. 6 << 1);
  776. err[4] =
  777. saa7114_write_block(client, decoder->reg + (0xb8 << 1),
  778. 8 << 1);
  779. err[5] =
  780. saa7114_write_block(client, decoder->reg + (0xe8 << 1),
  781. 8 << 1);
  782. for (i = 0; i <= 5; i++) {
  783. if (err[i] < 0) {
  784. v4l_dbg(1, debug, client,
  785. "init error %d at stage %d, leaving attach.\n",
  786. i, err[i]);
  787. kfree(decoder);
  788. return -EIO;
  789. }
  790. }
  791. for (i = 6; i < 8; i++) {
  792. v4l_dbg(1, debug, client,
  793. "reg[0x%02x] = 0x%02x (0x%02x)\n",
  794. i, saa7114_read(client, i),
  795. decoder->reg[REG_ADDR(i)]);
  796. }
  797. v4l_dbg(1, debug, client,
  798. "performing decoder reset sequence\n");
  799. err[6] = saa7114_write(client, 0x80, 0x06); // i-port and scaler backend clock selection, task A&B off
  800. err[7] = saa7114_write(client, 0x88, 0xd8); // sw reset scaler
  801. err[8] = saa7114_write(client, 0x88, 0xf8); // sw reset scaler release
  802. for (i = 6; i <= 8; i++) {
  803. if (err[i] < 0) {
  804. v4l_dbg(1, debug, client,
  805. "init error %d at stage %d, leaving attach.\n",
  806. i, err[i]);
  807. kfree(decoder);
  808. return -EIO;
  809. }
  810. }
  811. v4l_dbg(1, debug, client, "performing the rest of init\n");
  812. err[9] = saa7114_write(client, 0x01, decoder->reg[REG_ADDR(0x01)]);
  813. err[10] = saa7114_write_block(client, decoder->reg + (0x03 << 1), (0x1e + 1 - 0x03) << 1); // big seq
  814. err[11] = saa7114_write_block(client, decoder->reg + (0x40 << 1), (0x5f + 1 - 0x40) << 1); // slicer
  815. err[12] = saa7114_write_block(client, decoder->reg + (0x81 << 1), 2 << 1); // ?
  816. err[13] = saa7114_write_block(client, decoder->reg + (0x83 << 1), 5 << 1); // ?
  817. err[14] = saa7114_write_block(client, decoder->reg + (0x90 << 1), 4 << 1); // Task A
  818. err[15] =
  819. saa7114_write_block(client, decoder->reg + (0x94 << 1),
  820. 12 << 1);
  821. err[16] =
  822. saa7114_write_block(client, decoder->reg + (0xa0 << 1),
  823. 8 << 1);
  824. err[17] =
  825. saa7114_write_block(client, decoder->reg + (0xa8 << 1),
  826. 8 << 1);
  827. err[18] =
  828. saa7114_write_block(client, decoder->reg + (0xb0 << 1),
  829. 8 << 1);
  830. err[19] = saa7114_write_block(client, decoder->reg + (0xc0 << 1), 4 << 1); // Task B
  831. err[15] =
  832. saa7114_write_block(client, decoder->reg + (0xc4 << 1),
  833. 12 << 1);
  834. err[16] =
  835. saa7114_write_block(client, decoder->reg + (0xd0 << 1),
  836. 8 << 1);
  837. err[17] =
  838. saa7114_write_block(client, decoder->reg + (0xd8 << 1),
  839. 8 << 1);
  840. err[18] =
  841. saa7114_write_block(client, decoder->reg + (0xe0 << 1),
  842. 8 << 1);
  843. for (i = 9; i <= 18; i++) {
  844. if (err[i] < 0) {
  845. v4l_dbg(1, debug, client,
  846. "init error %d at stage %d, leaving attach.\n",
  847. i, err[i]);
  848. kfree(decoder);
  849. return -EIO;
  850. }
  851. }
  852. for (i = 6; i < 8; i++) {
  853. v4l_dbg(1, debug, client,
  854. "reg[0x%02x] = 0x%02x (0x%02x)\n",
  855. i, saa7114_read(client, i),
  856. decoder->reg[REG_ADDR(i)]);
  857. }
  858. for (i = 0x11; i <= 0x13; i++) {
  859. v4l_dbg(1, debug, client,
  860. "reg[0x%02x] = 0x%02x (0x%02x)\n",
  861. i, saa7114_read(client, i),
  862. decoder->reg[REG_ADDR(i)]);
  863. }
  864. v4l_dbg(1, debug, client, "setting video input\n");
  865. err[19] =
  866. saa7114_write(client, 0x02, decoder->reg[REG_ADDR(0x02)]);
  867. err[20] =
  868. saa7114_write(client, 0x09, decoder->reg[REG_ADDR(0x09)]);
  869. err[21] =
  870. saa7114_write(client, 0x0e, decoder->reg[REG_ADDR(0x0e)]);
  871. for (i = 19; i <= 21; i++) {
  872. if (err[i] < 0) {
  873. v4l_dbg(1, debug, client,
  874. "init error %d at stage %d, leaving attach.\n",
  875. i, err[i]);
  876. kfree(decoder);
  877. return -EIO;
  878. }
  879. }
  880. v4l_dbg(1, debug, client, "performing decoder reset sequence\n");
  881. err[22] = saa7114_write(client, 0x88, 0xd8); // sw reset scaler
  882. err[23] = saa7114_write(client, 0x88, 0xf8); // sw reset scaler release
  883. err[24] = saa7114_write(client, 0x80, 0x36); // i-port and scaler backend clock selection, task A&B off
  884. for (i = 22; i <= 24; i++) {
  885. if (err[i] < 0) {
  886. v4l_dbg(1, debug, client,
  887. "init error %d at stage %d, leaving attach.\n",
  888. i, err[i]);
  889. kfree(decoder);
  890. return -EIO;
  891. }
  892. }
  893. err[25] = saa7114_write(client, 0x06, init[REG_ADDR(0x06)]);
  894. err[26] = saa7114_write(client, 0x07, init[REG_ADDR(0x07)]);
  895. err[27] = saa7114_write(client, 0x10, init[REG_ADDR(0x10)]);
  896. v4l_dbg(1, debug, client, "chip version %x, decoder status 0x%02x\n",
  897. saa7114_read(client, 0x00) >> 4,
  898. saa7114_read(client, 0x1f));
  899. v4l_dbg(1, debug, client,
  900. "power save control: 0x%02x, scaler status: 0x%02x\n",
  901. saa7114_read(client, 0x88),
  902. saa7114_read(client, 0x8f));
  903. for (i = 0x94; i < 0x96; i++) {
  904. v4l_dbg(1, debug, client,
  905. "reg[0x%02x] = 0x%02x (0x%02x)\n",
  906. i, saa7114_read(client, i),
  907. decoder->reg[REG_ADDR(i)]);
  908. }
  909. //i = saa7114_write_block(client, init, sizeof(init));
  910. return 0;
  911. }
  912. static int saa7114_remove(struct i2c_client *client)
  913. {
  914. kfree(i2c_get_clientdata(client));
  915. return 0;
  916. }
  917. /* ----------------------------------------------------------------------- */
  918. static const struct i2c_device_id saa7114_id[] = {
  919. { "saa7114_old", 0 }, /* "saa7114" maps to the saa7115 driver */
  920. { }
  921. };
  922. MODULE_DEVICE_TABLE(i2c, saa7114_id);
  923. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  924. .name = "saa7114",
  925. .driverid = I2C_DRIVERID_SAA7114,
  926. .command = saa7114_command,
  927. .probe = saa7114_probe,
  928. .remove = saa7114_remove,
  929. .id_table = saa7114_id,
  930. };