ks0127.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Video Capture Driver (Video for Linux 1/2)
  3. * for the Matrox Marvel G200,G400 and Rainbow Runner-G series
  4. *
  5. * This module is an interface to the KS0127 video decoder chip.
  6. *
  7. * Copyright (C) 1999 Ryan Drake <stiletto@mediaone.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. *****************************************************************************
  24. *
  25. * Modified and extended by
  26. * Mike Bernson <mike@mlb.org>
  27. * Gerard v.d. Horst
  28. * Leon van Stuivenberg <l.vanstuivenberg@chello.nl>
  29. * Gernot Ziegler <gz@lysator.liu.se>
  30. *
  31. * Version History:
  32. * V1.0 Ryan Drake Initial version by Ryan Drake
  33. * V1.1 Gerard v.d. Horst Added some debugoutput, reset the video-standard
  34. */
  35. #ifndef __KERNEL__
  36. #define __KERNEL__
  37. #endif
  38. #include <linux/init.h>
  39. #include <linux/module.h>
  40. #include <linux/delay.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/proc_fs.h>
  45. #include "ks0127.h"
  46. #include <linux/i2c.h>
  47. #include <linux/video_decoder.h>
  48. #define dprintk if (debug) printk
  49. /* i2c identification */
  50. #define I2C_KS0127_ADDON 0xD8
  51. #define I2C_KS0127_ONBOARD 0xDA
  52. #define KS_TYPE_UNKNOWN 0
  53. #define KS_TYPE_0122S 1
  54. #define KS_TYPE_0127 2
  55. #define KS_TYPE_0127B 3
  56. /* ks0127 control registers */
  57. #define KS_STAT 0x00
  58. #define KS_CMDA 0x01
  59. #define KS_CMDB 0x02
  60. #define KS_CMDC 0x03
  61. #define KS_CMDD 0x04
  62. #define KS_HAVB 0x05
  63. #define KS_HAVE 0x06
  64. #define KS_HS1B 0x07
  65. #define KS_HS1E 0x08
  66. #define KS_HS2B 0x09
  67. #define KS_HS2E 0x0a
  68. #define KS_AGC 0x0b
  69. #define KS_HXTRA 0x0c
  70. #define KS_CDEM 0x0d
  71. #define KS_PORTAB 0x0e
  72. #define KS_LUMA 0x0f
  73. #define KS_CON 0x10
  74. #define KS_BRT 0x11
  75. #define KS_CHROMA 0x12
  76. #define KS_CHROMB 0x13
  77. #define KS_DEMOD 0x14
  78. #define KS_SAT 0x15
  79. #define KS_HUE 0x16
  80. #define KS_VERTIA 0x17
  81. #define KS_VERTIB 0x18
  82. #define KS_VERTIC 0x19
  83. #define KS_HSCLL 0x1a
  84. #define KS_HSCLH 0x1b
  85. #define KS_VSCLL 0x1c
  86. #define KS_VSCLH 0x1d
  87. #define KS_OFMTA 0x1e
  88. #define KS_OFMTB 0x1f
  89. #define KS_VBICTL 0x20
  90. #define KS_CCDAT2 0x21
  91. #define KS_CCDAT1 0x22
  92. #define KS_VBIL30 0x23
  93. #define KS_VBIL74 0x24
  94. #define KS_VBIL118 0x25
  95. #define KS_VBIL1512 0x26
  96. #define KS_TTFRAM 0x27
  97. #define KS_TESTA 0x28
  98. #define KS_UVOFFH 0x29
  99. #define KS_UVOFFL 0x2a
  100. #define KS_UGAIN 0x2b
  101. #define KS_VGAIN 0x2c
  102. #define KS_VAVB 0x2d
  103. #define KS_VAVE 0x2e
  104. #define KS_CTRACK 0x2f
  105. #define KS_POLCTL 0x30
  106. #define KS_REFCOD 0x31
  107. #define KS_INVALY 0x32
  108. #define KS_INVALU 0x33
  109. #define KS_INVALV 0x34
  110. #define KS_UNUSEY 0x35
  111. #define KS_UNUSEU 0x36
  112. #define KS_UNUSEV 0x37
  113. #define KS_USRSAV 0x38
  114. #define KS_USREAV 0x39
  115. #define KS_SHS1A 0x3a
  116. #define KS_SHS1B 0x3b
  117. #define KS_SHS1C 0x3c
  118. #define KS_CMDE 0x3d
  119. #define KS_VSDEL 0x3e
  120. #define KS_CMDF 0x3f
  121. #define KS_GAMMA0 0x40
  122. #define KS_GAMMA1 0x41
  123. #define KS_GAMMA2 0x42
  124. #define KS_GAMMA3 0x43
  125. #define KS_GAMMA4 0x44
  126. #define KS_GAMMA5 0x45
  127. #define KS_GAMMA6 0x46
  128. #define KS_GAMMA7 0x47
  129. #define KS_GAMMA8 0x48
  130. #define KS_GAMMA9 0x49
  131. #define KS_GAMMA10 0x4a
  132. #define KS_GAMMA11 0x4b
  133. #define KS_GAMMA12 0x4c
  134. #define KS_GAMMA13 0x4d
  135. #define KS_GAMMA14 0x4e
  136. #define KS_GAMMA15 0x4f
  137. #define KS_GAMMA16 0x50
  138. #define KS_GAMMA17 0x51
  139. #define KS_GAMMA18 0x52
  140. #define KS_GAMMA19 0x53
  141. #define KS_GAMMA20 0x54
  142. #define KS_GAMMA21 0x55
  143. #define KS_GAMMA22 0x56
  144. #define KS_GAMMA23 0x57
  145. #define KS_GAMMA24 0x58
  146. #define KS_GAMMA25 0x59
  147. #define KS_GAMMA26 0x5a
  148. #define KS_GAMMA27 0x5b
  149. #define KS_GAMMA28 0x5c
  150. #define KS_GAMMA29 0x5d
  151. #define KS_GAMMA30 0x5e
  152. #define KS_GAMMA31 0x5f
  153. #define KS_GAMMAD0 0x60
  154. #define KS_GAMMAD1 0x61
  155. #define KS_GAMMAD2 0x62
  156. #define KS_GAMMAD3 0x63
  157. #define KS_GAMMAD4 0x64
  158. #define KS_GAMMAD5 0x65
  159. #define KS_GAMMAD6 0x66
  160. #define KS_GAMMAD7 0x67
  161. #define KS_GAMMAD8 0x68
  162. #define KS_GAMMAD9 0x69
  163. #define KS_GAMMAD10 0x6a
  164. #define KS_GAMMAD11 0x6b
  165. #define KS_GAMMAD12 0x6c
  166. #define KS_GAMMAD13 0x6d
  167. #define KS_GAMMAD14 0x6e
  168. #define KS_GAMMAD15 0x6f
  169. #define KS_GAMMAD16 0x70
  170. #define KS_GAMMAD17 0x71
  171. #define KS_GAMMAD18 0x72
  172. #define KS_GAMMAD19 0x73
  173. #define KS_GAMMAD20 0x74
  174. #define KS_GAMMAD21 0x75
  175. #define KS_GAMMAD22 0x76
  176. #define KS_GAMMAD23 0x77
  177. #define KS_GAMMAD24 0x78
  178. #define KS_GAMMAD25 0x79
  179. #define KS_GAMMAD26 0x7a
  180. #define KS_GAMMAD27 0x7b
  181. #define KS_GAMMAD28 0x7c
  182. #define KS_GAMMAD29 0x7d
  183. #define KS_GAMMAD30 0x7e
  184. #define KS_GAMMAD31 0x7f
  185. /****************************************************************************
  186. * mga_dev : represents one ks0127 chip.
  187. ****************************************************************************/
  188. struct adjust {
  189. int contrast;
  190. int bright;
  191. int hue;
  192. int ugain;
  193. int vgain;
  194. };
  195. struct ks0127 {
  196. struct i2c_client *client;
  197. unsigned char addr;
  198. int format_width;
  199. int format_height;
  200. int cap_width;
  201. int cap_height;
  202. int norm;
  203. int ks_type;
  204. u8 regs[256];
  205. };
  206. static int debug; /* insmod parameter */
  207. module_param(debug, int, 0);
  208. MODULE_PARM_DESC(debug, "Debug output");
  209. MODULE_LICENSE("GPL");
  210. static u8 reg_defaults[64];
  211. static void init_reg_defaults(void)
  212. {
  213. u8 *table = reg_defaults;
  214. table[KS_CMDA] = 0x2c; /* VSE=0, CCIR 601, autodetect standard */
  215. table[KS_CMDB] = 0x12; /* VALIGN=0, AGC control and input */
  216. table[KS_CMDC] = 0x00; /* Test options */
  217. /* clock & input select, write 1 to PORTA */
  218. table[KS_CMDD] = 0x01;
  219. table[KS_HAVB] = 0x00; /* HAV Start Control */
  220. table[KS_HAVE] = 0x00; /* HAV End Control */
  221. table[KS_HS1B] = 0x10; /* HS1 Start Control */
  222. table[KS_HS1E] = 0x00; /* HS1 End Control */
  223. table[KS_HS2B] = 0x00; /* HS2 Start Control */
  224. table[KS_HS2E] = 0x00; /* HS2 End Control */
  225. table[KS_AGC] = 0x53; /* Manual setting for AGC */
  226. table[KS_HXTRA] = 0x00; /* Extra Bits for HAV and HS1/2 */
  227. table[KS_CDEM] = 0x00; /* Chroma Demodulation Control */
  228. table[KS_PORTAB] = 0x0f; /* port B is input, port A output GPPORT */
  229. table[KS_LUMA] = 0x01; /* Luma control */
  230. table[KS_CON] = 0x00; /* Contrast Control */
  231. table[KS_BRT] = 0x00; /* Brightness Control */
  232. table[KS_CHROMA] = 0x2a; /* Chroma control A */
  233. table[KS_CHROMB] = 0x90; /* Chroma control B */
  234. table[KS_DEMOD] = 0x00; /* Chroma Demodulation Control & Status */
  235. table[KS_SAT] = 0x00; /* Color Saturation Control*/
  236. table[KS_HUE] = 0x00; /* Hue Control */
  237. table[KS_VERTIA] = 0x00; /* Vertical Processing Control A */
  238. /* Vertical Processing Control B, luma 1 line delayed */
  239. table[KS_VERTIB] = 0x12;
  240. table[KS_VERTIC] = 0x0b; /* Vertical Processing Control C */
  241. table[KS_HSCLL] = 0x00; /* Horizontal Scaling Ratio Low */
  242. table[KS_HSCLH] = 0x00; /* Horizontal Scaling Ratio High */
  243. table[KS_VSCLL] = 0x00; /* Vertical Scaling Ratio Low */
  244. table[KS_VSCLH] = 0x00; /* Vertical Scaling Ratio High */
  245. /* 16 bit YCbCr 4:2:2 output; I can't make the bt866 like 8 bit /Sam */
  246. table[KS_OFMTA] = 0x30;
  247. table[KS_OFMTB] = 0x00; /* Output Control B */
  248. /* VBI Decoder Control; 4bit fmt: avoid Y overflow */
  249. table[KS_VBICTL] = 0x5d;
  250. table[KS_CCDAT2] = 0x00; /* Read Only register */
  251. table[KS_CCDAT1] = 0x00; /* Read Only register */
  252. table[KS_VBIL30] = 0xa8; /* VBI data decoding options */
  253. table[KS_VBIL74] = 0xaa; /* VBI data decoding options */
  254. table[KS_VBIL118] = 0x2a; /* VBI data decoding options */
  255. table[KS_VBIL1512] = 0x00; /* VBI data decoding options */
  256. table[KS_TTFRAM] = 0x00; /* Teletext frame alignment pattern */
  257. table[KS_TESTA] = 0x00; /* test register, shouldn't be written */
  258. table[KS_UVOFFH] = 0x00; /* UV Offset Adjustment High */
  259. table[KS_UVOFFL] = 0x00; /* UV Offset Adjustment Low */
  260. table[KS_UGAIN] = 0x00; /* U Component Gain Adjustment */
  261. table[KS_VGAIN] = 0x00; /* V Component Gain Adjustment */
  262. table[KS_VAVB] = 0x07; /* VAV Begin */
  263. table[KS_VAVE] = 0x00; /* VAV End */
  264. table[KS_CTRACK] = 0x00; /* Chroma Tracking Control */
  265. table[KS_POLCTL] = 0x41; /* Timing Signal Polarity Control */
  266. table[KS_REFCOD] = 0x80; /* Reference Code Insertion Control */
  267. table[KS_INVALY] = 0x10; /* Invalid Y Code */
  268. table[KS_INVALU] = 0x80; /* Invalid U Code */
  269. table[KS_INVALV] = 0x80; /* Invalid V Code */
  270. table[KS_UNUSEY] = 0x10; /* Unused Y Code */
  271. table[KS_UNUSEU] = 0x80; /* Unused U Code */
  272. table[KS_UNUSEV] = 0x80; /* Unused V Code */
  273. table[KS_USRSAV] = 0x00; /* reserved */
  274. table[KS_USREAV] = 0x00; /* reserved */
  275. table[KS_SHS1A] = 0x00; /* User Defined SHS1 A */
  276. /* User Defined SHS1 B, ALT656=1 on 0127B */
  277. table[KS_SHS1B] = 0x80;
  278. table[KS_SHS1C] = 0x00; /* User Defined SHS1 C */
  279. table[KS_CMDE] = 0x00; /* Command Register E */
  280. table[KS_VSDEL] = 0x00; /* VS Delay Control */
  281. /* Command Register F, update -immediately- */
  282. /* (there might come no vsync)*/
  283. table[KS_CMDF] = 0x02;
  284. }
  285. /* We need to manually read because of a bug in the KS0127 chip.
  286. *
  287. * An explanation from kayork@mail.utexas.edu:
  288. *
  289. * During I2C reads, the KS0127 only samples for a stop condition
  290. * during the place where the acknoledge bit should be. Any standard
  291. * I2C implementation (correctly) throws in another clock transition
  292. * at the 9th bit, and the KS0127 will not recognize the stop condition
  293. * and will continue to clock out data.
  294. *
  295. * So we have to do the read ourself. Big deal.
  296. workaround in i2c-algo-bit
  297. */
  298. static u8 ks0127_read(struct ks0127 *ks, u8 reg)
  299. {
  300. struct i2c_client *c = ks->client;
  301. char val = 0;
  302. struct i2c_msg msgs[] = {
  303. {c->addr, 0, sizeof(reg), &reg},
  304. {c->addr, I2C_M_RD | I2C_M_NO_RD_ACK, sizeof(val), &val}};
  305. int ret;
  306. ret = i2c_transfer(c->adapter, msgs, ARRAY_SIZE(msgs));
  307. if (ret != ARRAY_SIZE(msgs))
  308. dprintk("ks0127_write error\n");
  309. return val;
  310. }
  311. static void ks0127_write(struct ks0127 *ks, u8 reg, u8 val)
  312. {
  313. char msg[] = {reg, val};
  314. if (i2c_master_send(ks->client, msg, sizeof(msg)) != sizeof(msg))
  315. dprintk("ks0127_write error\n");
  316. ks->regs[reg] = val;
  317. }
  318. /* generic bit-twiddling */
  319. static void ks0127_and_or(struct ks0127 *ks, u8 reg, u8 and_v, u8 or_v)
  320. {
  321. u8 val = ks->regs[reg];
  322. val = (val & and_v) | or_v;
  323. ks0127_write(ks, reg, val);
  324. }
  325. /****************************************************************************
  326. * ks0127 private api
  327. ****************************************************************************/
  328. static void ks0127_reset(struct ks0127* ks)
  329. {
  330. int i;
  331. u8 *table = reg_defaults;
  332. ks->ks_type = KS_TYPE_UNKNOWN;
  333. dprintk("ks0127: reset\n");
  334. msleep(1);
  335. /* initialize all registers to known values */
  336. /* (except STAT, 0x21, 0x22, TEST and 0x38,0x39) */
  337. for(i = 1; i < 33; i++)
  338. ks0127_write(ks, i, table[i]);
  339. for(i = 35; i < 40; i++)
  340. ks0127_write(ks, i, table[i]);
  341. for(i = 41; i < 56; i++)
  342. ks0127_write(ks, i, table[i]);
  343. for(i = 58; i < 64; i++)
  344. ks0127_write(ks, i, table[i]);
  345. if ((ks0127_read(ks, KS_STAT) & 0x80) == 0) {
  346. ks->ks_type = KS_TYPE_0122S;
  347. dprintk("ks0127: ks0122s Found\n");
  348. return;
  349. }
  350. switch(ks0127_read(ks, KS_CMDE) & 0x0f) {
  351. case 0:
  352. ks->ks_type = KS_TYPE_0127;
  353. dprintk("ks0127: ks0127 found\n");
  354. break;
  355. case 9:
  356. ks->ks_type = KS_TYPE_0127B;
  357. dprintk("ks0127: ks0127B Revision A found\n");
  358. break;
  359. default:
  360. dprintk("ks0127: unknown revision\n");
  361. break;
  362. }
  363. }
  364. static int ks0127_command(struct i2c_client *client,
  365. unsigned int cmd, void *arg)
  366. {
  367. struct ks0127 *ks = i2c_get_clientdata(client);
  368. int *iarg = (int*)arg;
  369. int status;
  370. if (!ks)
  371. return -ENODEV;
  372. switch (cmd) {
  373. case DECODER_INIT:
  374. dprintk("ks0127: command DECODER_INIT\n");
  375. ks0127_reset(ks);
  376. break;
  377. case DECODER_SET_INPUT:
  378. switch(*iarg) {
  379. case KS_INPUT_COMPOSITE_1:
  380. case KS_INPUT_COMPOSITE_2:
  381. case KS_INPUT_COMPOSITE_3:
  382. case KS_INPUT_COMPOSITE_4:
  383. case KS_INPUT_COMPOSITE_5:
  384. case KS_INPUT_COMPOSITE_6:
  385. dprintk("ks0127: command DECODER_SET_INPUT %d: "
  386. "Composite\n", *iarg);
  387. /* autodetect 50/60 Hz */
  388. ks0127_and_or(ks, KS_CMDA, 0xfc, 0x00);
  389. /* VSE=0 */
  390. ks0127_and_or(ks, KS_CMDA, ~0x40, 0x00);
  391. /* set input line */
  392. ks0127_and_or(ks, KS_CMDB, 0xb0, *iarg);
  393. /* non-freerunning mode */
  394. ks0127_and_or(ks, KS_CMDC, 0x70, 0x0a);
  395. /* analog input */
  396. ks0127_and_or(ks, KS_CMDD, 0x03, 0x00);
  397. /* enable chroma demodulation */
  398. ks0127_and_or(ks, KS_CTRACK, 0xcf, 0x00);
  399. /* chroma trap, HYBWR=1 */
  400. ks0127_and_or(ks, KS_LUMA, 0x00,
  401. (reg_defaults[KS_LUMA])|0x0c);
  402. /* scaler fullbw, luma comb off */
  403. ks0127_and_or(ks, KS_VERTIA, 0x08, 0x81);
  404. /* manual chroma comb .25 .5 .25 */
  405. ks0127_and_or(ks, KS_VERTIC, 0x0f, 0x90);
  406. /* chroma path delay */
  407. ks0127_and_or(ks, KS_CHROMB, 0x0f, 0x90);
  408. ks0127_write(ks, KS_UGAIN, reg_defaults[KS_UGAIN]);
  409. ks0127_write(ks, KS_VGAIN, reg_defaults[KS_VGAIN]);
  410. ks0127_write(ks, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
  411. ks0127_write(ks, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
  412. break;
  413. case KS_INPUT_SVIDEO_1:
  414. case KS_INPUT_SVIDEO_2:
  415. case KS_INPUT_SVIDEO_3:
  416. dprintk("ks0127: command DECODER_SET_INPUT %d: "
  417. "S-Video\n", *iarg);
  418. /* autodetect 50/60 Hz */
  419. ks0127_and_or(ks, KS_CMDA, 0xfc, 0x00);
  420. /* VSE=0 */
  421. ks0127_and_or(ks, KS_CMDA, ~0x40, 0x00);
  422. /* set input line */
  423. ks0127_and_or(ks, KS_CMDB, 0xb0, *iarg);
  424. /* non-freerunning mode */
  425. ks0127_and_or(ks, KS_CMDC, 0x70, 0x0a);
  426. /* analog input */
  427. ks0127_and_or(ks, KS_CMDD, 0x03, 0x00);
  428. /* enable chroma demodulation */
  429. ks0127_and_or(ks, KS_CTRACK, 0xcf, 0x00);
  430. ks0127_and_or(ks, KS_LUMA, 0x00,
  431. reg_defaults[KS_LUMA]);
  432. /* disable luma comb */
  433. ks0127_and_or(ks, KS_VERTIA, 0x08,
  434. (reg_defaults[KS_VERTIA]&0xf0)|0x01);
  435. ks0127_and_or(ks, KS_VERTIC, 0x0f,
  436. reg_defaults[KS_VERTIC]&0xf0);
  437. ks0127_and_or(ks, KS_CHROMB, 0x0f,
  438. reg_defaults[KS_CHROMB]&0xf0);
  439. ks0127_write(ks, KS_UGAIN, reg_defaults[KS_UGAIN]);
  440. ks0127_write(ks, KS_VGAIN, reg_defaults[KS_VGAIN]);
  441. ks0127_write(ks, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
  442. ks0127_write(ks, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
  443. break;
  444. case KS_INPUT_YUV656:
  445. dprintk("ks0127: command DECODER_SET_INPUT 15: "
  446. "YUV656\n");
  447. if (ks->norm == VIDEO_MODE_NTSC ||
  448. ks->norm == KS_STD_PAL_M)
  449. /* force 60 Hz */
  450. ks0127_and_or(ks, KS_CMDA, 0xfc, 0x03);
  451. else
  452. /* force 50 Hz */
  453. ks0127_and_or(ks, KS_CMDA, 0xfc, 0x02);
  454. ks0127_and_or(ks, KS_CMDA, 0xff, 0x40); /* VSE=1 */
  455. /* set input line and VALIGN */
  456. ks0127_and_or(ks, KS_CMDB, 0xb0, (*iarg | 0x40));
  457. /* freerunning mode, */
  458. /* TSTGEN = 1 TSTGFR=11 TSTGPH=0 TSTGPK=0 VMEM=1*/
  459. ks0127_and_or(ks, KS_CMDC, 0x70, 0x87);
  460. /* digital input, SYNDIR = 0 INPSL=01 CLKDIR=0 EAV=0 */
  461. ks0127_and_or(ks, KS_CMDD, 0x03, 0x08);
  462. /* disable chroma demodulation */
  463. ks0127_and_or(ks, KS_CTRACK, 0xcf, 0x30);
  464. /* HYPK =01 CTRAP = 0 HYBWR=0 PED=1 RGBH=1 UNIT=1 */
  465. ks0127_and_or(ks, KS_LUMA, 0x00, 0x71);
  466. ks0127_and_or(ks, KS_VERTIC, 0x0f,
  467. reg_defaults[KS_VERTIC]&0xf0);
  468. /* scaler fullbw, luma comb off */
  469. ks0127_and_or(ks, KS_VERTIA, 0x08, 0x81);
  470. ks0127_and_or(ks, KS_CHROMB, 0x0f,
  471. reg_defaults[KS_CHROMB]&0xf0);
  472. ks0127_and_or(ks, KS_CON, 0x00, 0x00);
  473. ks0127_and_or(ks, KS_BRT, 0x00, 32); /* spec: 34 */
  474. /* spec: 229 (e5) */
  475. ks0127_and_or(ks, KS_SAT, 0x00, 0xe8);
  476. ks0127_and_or(ks, KS_HUE, 0x00, 0);
  477. ks0127_and_or(ks, KS_UGAIN, 0x00, 238);
  478. ks0127_and_or(ks, KS_VGAIN, 0x00, 0x00);
  479. /*UOFF:0x30, VOFF:0x30, TSTCGN=1 */
  480. ks0127_and_or(ks, KS_UVOFFH, 0x00, 0x4f);
  481. ks0127_and_or(ks, KS_UVOFFL, 0x00, 0x00);
  482. break;
  483. default:
  484. dprintk("ks0127: command DECODER_SET_INPUT: "
  485. "Unknown input %d\n", *iarg);
  486. break;
  487. }
  488. /* hack: CDMLPF sometimes spontaneously switches on; */
  489. /* force back off */
  490. ks0127_write(ks, KS_DEMOD, reg_defaults[KS_DEMOD]);
  491. break;
  492. case DECODER_SET_OUTPUT:
  493. switch(*iarg) {
  494. case KS_OUTPUT_YUV656E:
  495. dprintk("ks0127: command DECODER_SET_OUTPUT: "
  496. "OUTPUT_YUV656E (Missing)\n");
  497. return -EINVAL;
  498. break;
  499. case KS_OUTPUT_EXV:
  500. dprintk("ks0127: command DECODER_SET_OUTPUT: "
  501. "OUTPUT_EXV\n");
  502. ks0127_and_or(ks, KS_OFMTA, 0xf0, 0x09);
  503. break;
  504. }
  505. break;
  506. case DECODER_SET_NORM: //sam This block mixes old and new norm names...
  507. /* Set to automatic SECAM/Fsc mode */
  508. ks0127_and_or(ks, KS_DEMOD, 0xf0, 0x00);
  509. ks->norm = *iarg;
  510. switch(*iarg)
  511. {
  512. /* this is untested !! */
  513. /* It just detects PAL_N/NTSC_M (no special frequencies) */
  514. /* And you have to set the standard a second time afterwards */
  515. case VIDEO_MODE_AUTO:
  516. dprintk("ks0127: command DECODER_SET_NORM: AUTO\n");
  517. /* The chip determines the format */
  518. /* based on the current field rate */
  519. ks0127_and_or(ks, KS_CMDA, 0xfc, 0x00);
  520. ks0127_and_or(ks, KS_CHROMA, 0x9f, 0x20);
  521. /* This is wrong for PAL ! As I said, */
  522. /* you need to set the standard once again !! */
  523. ks->format_height = 240;
  524. ks->format_width = 704;
  525. break;
  526. case VIDEO_MODE_NTSC:
  527. dprintk("ks0127: command DECODER_SET_NORM: NTSC_M\n");
  528. ks0127_and_or(ks, KS_CHROMA, 0x9f, 0x20);
  529. ks->format_height = 240;
  530. ks->format_width = 704;
  531. break;
  532. case KS_STD_NTSC_N:
  533. dprintk("ks0127: command KS0127_SET_STANDARD: "
  534. "NTSC_N (fixme)\n");
  535. ks0127_and_or(ks, KS_CHROMA, 0x9f, 0x40);
  536. ks->format_height = 240;
  537. ks->format_width = 704;
  538. break;
  539. case VIDEO_MODE_PAL:
  540. dprintk("ks0127: command DECODER_SET_NORM: PAL_N\n");
  541. ks0127_and_or(ks, KS_CHROMA, 0x9f, 0x20);
  542. ks->format_height = 290;
  543. ks->format_width = 704;
  544. break;
  545. case KS_STD_PAL_M:
  546. dprintk("ks0127: command KS0127_SET_STANDARD: "
  547. "PAL_M (fixme)\n");
  548. ks0127_and_or(ks, KS_CHROMA, 0x9f, 0x40);
  549. ks->format_height = 290;
  550. ks->format_width = 704;
  551. break;
  552. case VIDEO_MODE_SECAM:
  553. dprintk("ks0127: command KS0127_SET_STANDARD: "
  554. "SECAM\n");
  555. ks->format_height = 290;
  556. ks->format_width = 704;
  557. /* set to secam autodetection */
  558. ks0127_and_or(ks, KS_CHROMA, 0xdf, 0x20);
  559. ks0127_and_or(ks, KS_DEMOD, 0xf0, 0x00);
  560. schedule_timeout_interruptible(HZ/10+1);
  561. /* did it autodetect? */
  562. if (ks0127_read(ks, KS_DEMOD) & 0x40)
  563. break;
  564. /* force to secam mode */
  565. ks0127_and_or(ks, KS_DEMOD, 0xf0, 0x0f);
  566. break;
  567. default:
  568. dprintk("ks0127: command DECODER_SET_NORM: "
  569. "Unknown norm %d\n", *iarg);
  570. break;
  571. }
  572. break;
  573. case DECODER_SET_PICTURE:
  574. dprintk("ks0127: command DECODER_SET_PICTURE "
  575. "not yet supported (fixme)\n");
  576. return -EINVAL;
  577. //sam todo: KS0127_SET_BRIGHTNESS: Merge into DECODER_SET_PICTURE
  578. //sam todo: KS0127_SET_CONTRAST: Merge into DECODER_SET_PICTURE
  579. //sam todo: KS0127_SET_HUE: Merge into DECODER_SET_PICTURE?
  580. //sam todo: KS0127_SET_SATURATION: Merge into DECODER_SET_PICTURE
  581. //sam todo: KS0127_SET_AGC_MODE:
  582. //sam todo: KS0127_SET_AGC:
  583. //sam todo: KS0127_SET_CHROMA_MODE:
  584. //sam todo: KS0127_SET_PIXCLK_MODE:
  585. //sam todo: KS0127_SET_GAMMA_MODE:
  586. //sam todo: KS0127_SET_UGAIN:
  587. //sam todo: KS0127_SET_VGAIN:
  588. //sam todo: KS0127_SET_INVALY:
  589. //sam todo: KS0127_SET_INVALU:
  590. //sam todo: KS0127_SET_INVALV:
  591. //sam todo: KS0127_SET_UNUSEY:
  592. //sam todo: KS0127_SET_UNUSEU:
  593. //sam todo: KS0127_SET_UNUSEV:
  594. //sam todo: KS0127_SET_VSALIGN_MODE:
  595. case DECODER_ENABLE_OUTPUT:
  596. {
  597. int *iarg = arg;
  598. int enable = (*iarg != 0);
  599. if (enable) {
  600. dprintk("ks0127: command "
  601. "DECODER_ENABLE_OUTPUT on "
  602. "(%d)\n", enable);
  603. /* All output pins on */
  604. ks0127_and_or(ks, KS_OFMTA, 0xcf, 0x30);
  605. /* Obey the OEN pin */
  606. ks0127_and_or(ks, KS_CDEM, 0x7f, 0x00);
  607. } else {
  608. dprintk("ks0127: command "
  609. "DECODER_ENABLE_OUTPUT off "
  610. "(%d)\n", enable);
  611. /* Video output pins off */
  612. ks0127_and_or(ks, KS_OFMTA, 0xcf, 0x00);
  613. /* Ignore the OEN pin */
  614. ks0127_and_or(ks, KS_CDEM, 0x7f, 0x80);
  615. }
  616. }
  617. break;
  618. //sam todo: KS0127_SET_OUTPUT_MODE:
  619. //sam todo: KS0127_SET_WIDTH:
  620. //sam todo: KS0127_SET_HEIGHT:
  621. //sam todo: KS0127_SET_HSCALE:
  622. case DECODER_GET_STATUS:
  623. dprintk("ks0127: command DECODER_GET_STATUS\n");
  624. *iarg = 0;
  625. status = ks0127_read(ks, KS_STAT);
  626. if (!(status & 0x20)) /* NOVID not set */
  627. *iarg = (*iarg | DECODER_STATUS_GOOD);
  628. if ((status & 0x01)) /* CLOCK set */
  629. *iarg = (*iarg | DECODER_STATUS_COLOR);
  630. if ((status & 0x08)) /* PALDET set */
  631. *iarg = (*iarg | DECODER_STATUS_PAL);
  632. else
  633. *iarg = (*iarg | DECODER_STATUS_NTSC);
  634. break;
  635. //Catch any unknown command
  636. default:
  637. dprintk("ks0127: command unknown: %04X\n", cmd);
  638. return -EINVAL;
  639. }
  640. return 0;
  641. }
  642. static int ks0127_probe(struct i2c_adapter *adapter);
  643. static int ks0127_detach(struct i2c_client *client);
  644. static int ks0127_command(struct i2c_client *client,
  645. unsigned int cmd, void *arg);
  646. /* Addresses to scan */
  647. static unsigned short normal_i2c[] = {I2C_KS0127_ADDON>>1,
  648. I2C_KS0127_ONBOARD>>1, I2C_CLIENT_END};
  649. static unsigned short probe[2] = {I2C_CLIENT_END, I2C_CLIENT_END};
  650. static unsigned short ignore[2] = {I2C_CLIENT_END, I2C_CLIENT_END};
  651. static struct i2c_client_address_data addr_data = {
  652. normal_i2c,
  653. probe,
  654. ignore,
  655. };
  656. static struct i2c_driver i2c_driver_ks0127 = {
  657. .driver.name = "ks0127",
  658. .id = I2C_DRIVERID_KS0127,
  659. .attach_adapter = ks0127_probe,
  660. .detach_client = ks0127_detach,
  661. .command = ks0127_command
  662. };
  663. static struct i2c_client ks0127_client_tmpl =
  664. {
  665. .name = "(ks0127 unset)",
  666. .addr = 0,
  667. .adapter = NULL,
  668. .driver = &i2c_driver_ks0127,
  669. .usage_count = 0
  670. };
  671. static int ks0127_found_proc(struct i2c_adapter *adapter, int addr, int kind)
  672. {
  673. struct ks0127 *ks;
  674. struct i2c_client *client;
  675. client = kzalloc(sizeof(*client), GFP_KERNEL);
  676. if (client == NULL)
  677. return -ENOMEM;
  678. memcpy(client, &ks0127_client_tmpl, sizeof(*client));
  679. ks = kzalloc(sizeof(*ks), GFP_KERNEL);
  680. if (ks == NULL) {
  681. kfree(client);
  682. return -ENOMEM;
  683. }
  684. i2c_set_clientdata(client, ks);
  685. client->adapter = adapter;
  686. client->addr = addr;
  687. sprintf(client->name, "ks0127-%02x", adapter->id);
  688. ks->client = client;
  689. ks->addr = addr;
  690. ks->ks_type = KS_TYPE_UNKNOWN;
  691. /* power up */
  692. ks0127_write(ks, KS_CMDA, 0x2c);
  693. mdelay(10);
  694. /* reset the device */
  695. ks0127_reset(ks);
  696. printk(KERN_INFO "ks0127: attach: %s video decoder\n",
  697. ks->addr==(I2C_KS0127_ADDON>>1) ? "addon" : "on-board");
  698. i2c_attach_client(client);
  699. return 0;
  700. }
  701. static int ks0127_probe(struct i2c_adapter *adapter)
  702. {
  703. if (adapter->id == I2C_HW_B_ZR36067)
  704. return i2c_probe(adapter, &addr_data, ks0127_found_proc);
  705. return 0;
  706. }
  707. static int ks0127_detach(struct i2c_client *client)
  708. {
  709. struct ks0127 *ks = i2c_get_clientdata(client);
  710. ks0127_write(ks, KS_OFMTA, 0x20); /*tristate*/
  711. ks0127_write(ks, KS_CMDA, 0x2c | 0x80); /* power down */
  712. i2c_detach_client(client);
  713. kfree(ks);
  714. kfree(client);
  715. dprintk("ks0127: detach\n");
  716. return 0;
  717. }
  718. static int __devinit ks0127_init_module(void)
  719. {
  720. init_reg_defaults();
  721. return i2c_add_driver(&i2c_driver_ks0127);
  722. }
  723. static void __devexit ks0127_cleanup_module(void)
  724. {
  725. i2c_del_driver(&i2c_driver_ks0127);
  726. }
  727. module_init(ks0127_init_module);
  728. module_exit(ks0127_cleanup_module);