saa7114.c 32 KB

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