ks0127.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Video Capture Driver (Video for Linux 1/2)
  4. * for the Matrox Marvel G200,G400 and Rainbow Runner-G series
  5. *
  6. * This module is an interface to the KS0127 video decoder chip.
  7. *
  8. * Copyright (C) 1999 Ryan Drake <[email protected]>
  9. *
  10. *****************************************************************************
  11. *
  12. * Modified and extended by
  13. * Mike Bernson <[email protected]>
  14. * Gerard v.d. Horst
  15. * Leon van Stuivenberg <[email protected]>
  16. * Gernot Ziegler <[email protected]>
  17. *
  18. * Version History:
  19. * V1.0 Ryan Drake Initial version by Ryan Drake
  20. * V1.1 Gerard v.d. Horst Added some debugoutput, reset the video-standard
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/errno.h>
  26. #include <linux/kernel.h>
  27. #include <linux/i2c.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/slab.h>
  30. #include <media/v4l2-device.h>
  31. #include "ks0127.h"
  32. MODULE_DESCRIPTION("KS0127 video decoder driver");
  33. MODULE_AUTHOR("Ryan Drake");
  34. MODULE_LICENSE("GPL");
  35. /* Addresses */
  36. #define I2C_KS0127_ADDON 0xD8
  37. #define I2C_KS0127_ONBOARD 0xDA
  38. /* ks0127 control registers */
  39. #define KS_STAT 0x00
  40. #define KS_CMDA 0x01
  41. #define KS_CMDB 0x02
  42. #define KS_CMDC 0x03
  43. #define KS_CMDD 0x04
  44. #define KS_HAVB 0x05
  45. #define KS_HAVE 0x06
  46. #define KS_HS1B 0x07
  47. #define KS_HS1E 0x08
  48. #define KS_HS2B 0x09
  49. #define KS_HS2E 0x0a
  50. #define KS_AGC 0x0b
  51. #define KS_HXTRA 0x0c
  52. #define KS_CDEM 0x0d
  53. #define KS_PORTAB 0x0e
  54. #define KS_LUMA 0x0f
  55. #define KS_CON 0x10
  56. #define KS_BRT 0x11
  57. #define KS_CHROMA 0x12
  58. #define KS_CHROMB 0x13
  59. #define KS_DEMOD 0x14
  60. #define KS_SAT 0x15
  61. #define KS_HUE 0x16
  62. #define KS_VERTIA 0x17
  63. #define KS_VERTIB 0x18
  64. #define KS_VERTIC 0x19
  65. #define KS_HSCLL 0x1a
  66. #define KS_HSCLH 0x1b
  67. #define KS_VSCLL 0x1c
  68. #define KS_VSCLH 0x1d
  69. #define KS_OFMTA 0x1e
  70. #define KS_OFMTB 0x1f
  71. #define KS_VBICTL 0x20
  72. #define KS_CCDAT2 0x21
  73. #define KS_CCDAT1 0x22
  74. #define KS_VBIL30 0x23
  75. #define KS_VBIL74 0x24
  76. #define KS_VBIL118 0x25
  77. #define KS_VBIL1512 0x26
  78. #define KS_TTFRAM 0x27
  79. #define KS_TESTA 0x28
  80. #define KS_UVOFFH 0x29
  81. #define KS_UVOFFL 0x2a
  82. #define KS_UGAIN 0x2b
  83. #define KS_VGAIN 0x2c
  84. #define KS_VAVB 0x2d
  85. #define KS_VAVE 0x2e
  86. #define KS_CTRACK 0x2f
  87. #define KS_POLCTL 0x30
  88. #define KS_REFCOD 0x31
  89. #define KS_INVALY 0x32
  90. #define KS_INVALU 0x33
  91. #define KS_INVALV 0x34
  92. #define KS_UNUSEY 0x35
  93. #define KS_UNUSEU 0x36
  94. #define KS_UNUSEV 0x37
  95. #define KS_USRSAV 0x38
  96. #define KS_USREAV 0x39
  97. #define KS_SHS1A 0x3a
  98. #define KS_SHS1B 0x3b
  99. #define KS_SHS1C 0x3c
  100. #define KS_CMDE 0x3d
  101. #define KS_VSDEL 0x3e
  102. #define KS_CMDF 0x3f
  103. #define KS_GAMMA0 0x40
  104. #define KS_GAMMA1 0x41
  105. #define KS_GAMMA2 0x42
  106. #define KS_GAMMA3 0x43
  107. #define KS_GAMMA4 0x44
  108. #define KS_GAMMA5 0x45
  109. #define KS_GAMMA6 0x46
  110. #define KS_GAMMA7 0x47
  111. #define KS_GAMMA8 0x48
  112. #define KS_GAMMA9 0x49
  113. #define KS_GAMMA10 0x4a
  114. #define KS_GAMMA11 0x4b
  115. #define KS_GAMMA12 0x4c
  116. #define KS_GAMMA13 0x4d
  117. #define KS_GAMMA14 0x4e
  118. #define KS_GAMMA15 0x4f
  119. #define KS_GAMMA16 0x50
  120. #define KS_GAMMA17 0x51
  121. #define KS_GAMMA18 0x52
  122. #define KS_GAMMA19 0x53
  123. #define KS_GAMMA20 0x54
  124. #define KS_GAMMA21 0x55
  125. #define KS_GAMMA22 0x56
  126. #define KS_GAMMA23 0x57
  127. #define KS_GAMMA24 0x58
  128. #define KS_GAMMA25 0x59
  129. #define KS_GAMMA26 0x5a
  130. #define KS_GAMMA27 0x5b
  131. #define KS_GAMMA28 0x5c
  132. #define KS_GAMMA29 0x5d
  133. #define KS_GAMMA30 0x5e
  134. #define KS_GAMMA31 0x5f
  135. #define KS_GAMMAD0 0x60
  136. #define KS_GAMMAD1 0x61
  137. #define KS_GAMMAD2 0x62
  138. #define KS_GAMMAD3 0x63
  139. #define KS_GAMMAD4 0x64
  140. #define KS_GAMMAD5 0x65
  141. #define KS_GAMMAD6 0x66
  142. #define KS_GAMMAD7 0x67
  143. #define KS_GAMMAD8 0x68
  144. #define KS_GAMMAD9 0x69
  145. #define KS_GAMMAD10 0x6a
  146. #define KS_GAMMAD11 0x6b
  147. #define KS_GAMMAD12 0x6c
  148. #define KS_GAMMAD13 0x6d
  149. #define KS_GAMMAD14 0x6e
  150. #define KS_GAMMAD15 0x6f
  151. #define KS_GAMMAD16 0x70
  152. #define KS_GAMMAD17 0x71
  153. #define KS_GAMMAD18 0x72
  154. #define KS_GAMMAD19 0x73
  155. #define KS_GAMMAD20 0x74
  156. #define KS_GAMMAD21 0x75
  157. #define KS_GAMMAD22 0x76
  158. #define KS_GAMMAD23 0x77
  159. #define KS_GAMMAD24 0x78
  160. #define KS_GAMMAD25 0x79
  161. #define KS_GAMMAD26 0x7a
  162. #define KS_GAMMAD27 0x7b
  163. #define KS_GAMMAD28 0x7c
  164. #define KS_GAMMAD29 0x7d
  165. #define KS_GAMMAD30 0x7e
  166. #define KS_GAMMAD31 0x7f
  167. /****************************************************************************
  168. * mga_dev : represents one ks0127 chip.
  169. ****************************************************************************/
  170. struct adjust {
  171. int contrast;
  172. int bright;
  173. int hue;
  174. int ugain;
  175. int vgain;
  176. };
  177. struct ks0127 {
  178. struct v4l2_subdev sd;
  179. v4l2_std_id norm;
  180. u8 regs[256];
  181. };
  182. static inline struct ks0127 *to_ks0127(struct v4l2_subdev *sd)
  183. {
  184. return container_of(sd, struct ks0127, sd);
  185. }
  186. static int debug; /* insmod parameter */
  187. module_param(debug, int, 0);
  188. MODULE_PARM_DESC(debug, "Debug output");
  189. static u8 reg_defaults[64];
  190. static void init_reg_defaults(void)
  191. {
  192. static int initialized;
  193. u8 *table = reg_defaults;
  194. if (initialized)
  195. return;
  196. initialized = 1;
  197. table[KS_CMDA] = 0x2c; /* VSE=0, CCIR 601, autodetect standard */
  198. table[KS_CMDB] = 0x12; /* VALIGN=0, AGC control and input */
  199. table[KS_CMDC] = 0x00; /* Test options */
  200. /* clock & input select, write 1 to PORTA */
  201. table[KS_CMDD] = 0x01;
  202. table[KS_HAVB] = 0x00; /* HAV Start Control */
  203. table[KS_HAVE] = 0x00; /* HAV End Control */
  204. table[KS_HS1B] = 0x10; /* HS1 Start Control */
  205. table[KS_HS1E] = 0x00; /* HS1 End Control */
  206. table[KS_HS2B] = 0x00; /* HS2 Start Control */
  207. table[KS_HS2E] = 0x00; /* HS2 End Control */
  208. table[KS_AGC] = 0x53; /* Manual setting for AGC */
  209. table[KS_HXTRA] = 0x00; /* Extra Bits for HAV and HS1/2 */
  210. table[KS_CDEM] = 0x00; /* Chroma Demodulation Control */
  211. table[KS_PORTAB] = 0x0f; /* port B is input, port A output GPPORT */
  212. table[KS_LUMA] = 0x01; /* Luma control */
  213. table[KS_CON] = 0x00; /* Contrast Control */
  214. table[KS_BRT] = 0x00; /* Brightness Control */
  215. table[KS_CHROMA] = 0x2a; /* Chroma control A */
  216. table[KS_CHROMB] = 0x90; /* Chroma control B */
  217. table[KS_DEMOD] = 0x00; /* Chroma Demodulation Control & Status */
  218. table[KS_SAT] = 0x00; /* Color Saturation Control*/
  219. table[KS_HUE] = 0x00; /* Hue Control */
  220. table[KS_VERTIA] = 0x00; /* Vertical Processing Control A */
  221. /* Vertical Processing Control B, luma 1 line delayed */
  222. table[KS_VERTIB] = 0x12;
  223. table[KS_VERTIC] = 0x0b; /* Vertical Processing Control C */
  224. table[KS_HSCLL] = 0x00; /* Horizontal Scaling Ratio Low */
  225. table[KS_HSCLH] = 0x00; /* Horizontal Scaling Ratio High */
  226. table[KS_VSCLL] = 0x00; /* Vertical Scaling Ratio Low */
  227. table[KS_VSCLH] = 0x00; /* Vertical Scaling Ratio High */
  228. /* 16 bit YCbCr 4:2:2 output; I can't make the bt866 like 8 bit /Sam */
  229. table[KS_OFMTA] = 0x30;
  230. table[KS_OFMTB] = 0x00; /* Output Control B */
  231. /* VBI Decoder Control; 4bit fmt: avoid Y overflow */
  232. table[KS_VBICTL] = 0x5d;
  233. table[KS_CCDAT2] = 0x00; /* Read Only register */
  234. table[KS_CCDAT1] = 0x00; /* Read Only register */
  235. table[KS_VBIL30] = 0xa8; /* VBI data decoding options */
  236. table[KS_VBIL74] = 0xaa; /* VBI data decoding options */
  237. table[KS_VBIL118] = 0x2a; /* VBI data decoding options */
  238. table[KS_VBIL1512] = 0x00; /* VBI data decoding options */
  239. table[KS_TTFRAM] = 0x00; /* Teletext frame alignment pattern */
  240. table[KS_TESTA] = 0x00; /* test register, shouldn't be written */
  241. table[KS_UVOFFH] = 0x00; /* UV Offset Adjustment High */
  242. table[KS_UVOFFL] = 0x00; /* UV Offset Adjustment Low */
  243. table[KS_UGAIN] = 0x00; /* U Component Gain Adjustment */
  244. table[KS_VGAIN] = 0x00; /* V Component Gain Adjustment */
  245. table[KS_VAVB] = 0x07; /* VAV Begin */
  246. table[KS_VAVE] = 0x00; /* VAV End */
  247. table[KS_CTRACK] = 0x00; /* Chroma Tracking Control */
  248. table[KS_POLCTL] = 0x41; /* Timing Signal Polarity Control */
  249. table[KS_REFCOD] = 0x80; /* Reference Code Insertion Control */
  250. table[KS_INVALY] = 0x10; /* Invalid Y Code */
  251. table[KS_INVALU] = 0x80; /* Invalid U Code */
  252. table[KS_INVALV] = 0x80; /* Invalid V Code */
  253. table[KS_UNUSEY] = 0x10; /* Unused Y Code */
  254. table[KS_UNUSEU] = 0x80; /* Unused U Code */
  255. table[KS_UNUSEV] = 0x80; /* Unused V Code */
  256. table[KS_USRSAV] = 0x00; /* reserved */
  257. table[KS_USREAV] = 0x00; /* reserved */
  258. table[KS_SHS1A] = 0x00; /* User Defined SHS1 A */
  259. /* User Defined SHS1 B, ALT656=1 on 0127B */
  260. table[KS_SHS1B] = 0x80;
  261. table[KS_SHS1C] = 0x00; /* User Defined SHS1 C */
  262. table[KS_CMDE] = 0x00; /* Command Register E */
  263. table[KS_VSDEL] = 0x00; /* VS Delay Control */
  264. /* Command Register F, update -immediately- */
  265. /* (there might come no vsync)*/
  266. table[KS_CMDF] = 0x02;
  267. }
  268. /* We need to manually read because of a bug in the KS0127 chip.
  269. *
  270. * An explanation from [email protected]:
  271. *
  272. * During I2C reads, the KS0127 only samples for a stop condition
  273. * during the place where the acknowledge bit should be. Any standard
  274. * I2C implementation (correctly) throws in another clock transition
  275. * at the 9th bit, and the KS0127 will not recognize the stop condition
  276. * and will continue to clock out data.
  277. *
  278. * So we have to do the read ourself. Big deal.
  279. * workaround in i2c-algo-bit
  280. */
  281. static u8 ks0127_read(struct v4l2_subdev *sd, u8 reg)
  282. {
  283. struct i2c_client *client = v4l2_get_subdevdata(sd);
  284. char val = 0;
  285. struct i2c_msg msgs[] = {
  286. {
  287. .addr = client->addr,
  288. .len = sizeof(reg),
  289. .buf = &reg
  290. },
  291. {
  292. .addr = client->addr,
  293. .flags = I2C_M_RD | I2C_M_NO_RD_ACK,
  294. .len = sizeof(val),
  295. .buf = &val
  296. }
  297. };
  298. int ret;
  299. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  300. if (ret != ARRAY_SIZE(msgs))
  301. v4l2_dbg(1, debug, sd, "read error\n");
  302. return val;
  303. }
  304. static void ks0127_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  305. {
  306. struct i2c_client *client = v4l2_get_subdevdata(sd);
  307. struct ks0127 *ks = to_ks0127(sd);
  308. char msg[] = { reg, val };
  309. if (i2c_master_send(client, msg, sizeof(msg)) != sizeof(msg))
  310. v4l2_dbg(1, debug, sd, "write error\n");
  311. ks->regs[reg] = val;
  312. }
  313. /* generic bit-twiddling */
  314. static void ks0127_and_or(struct v4l2_subdev *sd, u8 reg, u8 and_v, u8 or_v)
  315. {
  316. struct ks0127 *ks = to_ks0127(sd);
  317. u8 val = ks->regs[reg];
  318. val = (val & and_v) | or_v;
  319. ks0127_write(sd, reg, val);
  320. }
  321. /****************************************************************************
  322. * ks0127 private api
  323. ****************************************************************************/
  324. static void ks0127_init(struct v4l2_subdev *sd)
  325. {
  326. u8 *table = reg_defaults;
  327. int i;
  328. v4l2_dbg(1, debug, sd, "reset\n");
  329. msleep(1);
  330. /* initialize all registers to known values */
  331. /* (except STAT, 0x21, 0x22, TEST and 0x38,0x39) */
  332. for (i = 1; i < 33; i++)
  333. ks0127_write(sd, i, table[i]);
  334. for (i = 35; i < 40; i++)
  335. ks0127_write(sd, i, table[i]);
  336. for (i = 41; i < 56; i++)
  337. ks0127_write(sd, i, table[i]);
  338. for (i = 58; i < 64; i++)
  339. ks0127_write(sd, i, table[i]);
  340. if ((ks0127_read(sd, KS_STAT) & 0x80) == 0) {
  341. v4l2_dbg(1, debug, sd, "ks0122s found\n");
  342. return;
  343. }
  344. switch (ks0127_read(sd, KS_CMDE) & 0x0f) {
  345. case 0:
  346. v4l2_dbg(1, debug, sd, "ks0127 found\n");
  347. break;
  348. case 9:
  349. v4l2_dbg(1, debug, sd, "ks0127B Revision A found\n");
  350. break;
  351. default:
  352. v4l2_dbg(1, debug, sd, "unknown revision\n");
  353. break;
  354. }
  355. }
  356. static int ks0127_s_routing(struct v4l2_subdev *sd,
  357. u32 input, u32 output, u32 config)
  358. {
  359. struct ks0127 *ks = to_ks0127(sd);
  360. switch (input) {
  361. case KS_INPUT_COMPOSITE_1:
  362. case KS_INPUT_COMPOSITE_2:
  363. case KS_INPUT_COMPOSITE_3:
  364. case KS_INPUT_COMPOSITE_4:
  365. case KS_INPUT_COMPOSITE_5:
  366. case KS_INPUT_COMPOSITE_6:
  367. v4l2_dbg(1, debug, sd,
  368. "s_routing %d: Composite\n", input);
  369. /* autodetect 50/60 Hz */
  370. ks0127_and_or(sd, KS_CMDA, 0xfc, 0x00);
  371. /* VSE=0 */
  372. ks0127_and_or(sd, KS_CMDA, ~0x40, 0x00);
  373. /* set input line */
  374. ks0127_and_or(sd, KS_CMDB, 0xb0, input);
  375. /* non-freerunning mode */
  376. ks0127_and_or(sd, KS_CMDC, 0x70, 0x0a);
  377. /* analog input */
  378. ks0127_and_or(sd, KS_CMDD, 0x03, 0x00);
  379. /* enable chroma demodulation */
  380. ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x00);
  381. /* chroma trap, HYBWR=1 */
  382. ks0127_and_or(sd, KS_LUMA, 0x00,
  383. (reg_defaults[KS_LUMA])|0x0c);
  384. /* scaler fullbw, luma comb off */
  385. ks0127_and_or(sd, KS_VERTIA, 0x08, 0x81);
  386. /* manual chroma comb .25 .5 .25 */
  387. ks0127_and_or(sd, KS_VERTIC, 0x0f, 0x90);
  388. /* chroma path delay */
  389. ks0127_and_or(sd, KS_CHROMB, 0x0f, 0x90);
  390. ks0127_write(sd, KS_UGAIN, reg_defaults[KS_UGAIN]);
  391. ks0127_write(sd, KS_VGAIN, reg_defaults[KS_VGAIN]);
  392. ks0127_write(sd, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
  393. ks0127_write(sd, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
  394. break;
  395. case KS_INPUT_SVIDEO_1:
  396. case KS_INPUT_SVIDEO_2:
  397. case KS_INPUT_SVIDEO_3:
  398. v4l2_dbg(1, debug, sd,
  399. "s_routing %d: S-Video\n", input);
  400. /* autodetect 50/60 Hz */
  401. ks0127_and_or(sd, KS_CMDA, 0xfc, 0x00);
  402. /* VSE=0 */
  403. ks0127_and_or(sd, KS_CMDA, ~0x40, 0x00);
  404. /* set input line */
  405. ks0127_and_or(sd, KS_CMDB, 0xb0, input);
  406. /* non-freerunning mode */
  407. ks0127_and_or(sd, KS_CMDC, 0x70, 0x0a);
  408. /* analog input */
  409. ks0127_and_or(sd, KS_CMDD, 0x03, 0x00);
  410. /* enable chroma demodulation */
  411. ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x00);
  412. ks0127_and_or(sd, KS_LUMA, 0x00,
  413. reg_defaults[KS_LUMA]);
  414. /* disable luma comb */
  415. ks0127_and_or(sd, KS_VERTIA, 0x08,
  416. (reg_defaults[KS_VERTIA]&0xf0)|0x01);
  417. ks0127_and_or(sd, KS_VERTIC, 0x0f,
  418. reg_defaults[KS_VERTIC]&0xf0);
  419. ks0127_and_or(sd, KS_CHROMB, 0x0f,
  420. reg_defaults[KS_CHROMB]&0xf0);
  421. ks0127_write(sd, KS_UGAIN, reg_defaults[KS_UGAIN]);
  422. ks0127_write(sd, KS_VGAIN, reg_defaults[KS_VGAIN]);
  423. ks0127_write(sd, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
  424. ks0127_write(sd, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
  425. break;
  426. case KS_INPUT_YUV656:
  427. v4l2_dbg(1, debug, sd, "s_routing 15: YUV656\n");
  428. if (ks->norm & V4L2_STD_525_60)
  429. /* force 60 Hz */
  430. ks0127_and_or(sd, KS_CMDA, 0xfc, 0x03);
  431. else
  432. /* force 50 Hz */
  433. ks0127_and_or(sd, KS_CMDA, 0xfc, 0x02);
  434. ks0127_and_or(sd, KS_CMDA, 0xff, 0x40); /* VSE=1 */
  435. /* set input line and VALIGN */
  436. ks0127_and_or(sd, KS_CMDB, 0xb0, (input | 0x40));
  437. /* freerunning mode, */
  438. /* TSTGEN = 1 TSTGFR=11 TSTGPH=0 TSTGPK=0 VMEM=1*/
  439. ks0127_and_or(sd, KS_CMDC, 0x70, 0x87);
  440. /* digital input, SYNDIR = 0 INPSL=01 CLKDIR=0 EAV=0 */
  441. ks0127_and_or(sd, KS_CMDD, 0x03, 0x08);
  442. /* disable chroma demodulation */
  443. ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x30);
  444. /* HYPK =01 CTRAP = 0 HYBWR=0 PED=1 RGBH=1 UNIT=1 */
  445. ks0127_and_or(sd, KS_LUMA, 0x00, 0x71);
  446. ks0127_and_or(sd, KS_VERTIC, 0x0f,
  447. reg_defaults[KS_VERTIC]&0xf0);
  448. /* scaler fullbw, luma comb off */
  449. ks0127_and_or(sd, KS_VERTIA, 0x08, 0x81);
  450. ks0127_and_or(sd, KS_CHROMB, 0x0f,
  451. reg_defaults[KS_CHROMB]&0xf0);
  452. ks0127_and_or(sd, KS_CON, 0x00, 0x00);
  453. ks0127_and_or(sd, KS_BRT, 0x00, 32); /* spec: 34 */
  454. /* spec: 229 (e5) */
  455. ks0127_and_or(sd, KS_SAT, 0x00, 0xe8);
  456. ks0127_and_or(sd, KS_HUE, 0x00, 0);
  457. ks0127_and_or(sd, KS_UGAIN, 0x00, 238);
  458. ks0127_and_or(sd, KS_VGAIN, 0x00, 0x00);
  459. /*UOFF:0x30, VOFF:0x30, TSTCGN=1 */
  460. ks0127_and_or(sd, KS_UVOFFH, 0x00, 0x4f);
  461. ks0127_and_or(sd, KS_UVOFFL, 0x00, 0x00);
  462. break;
  463. default:
  464. v4l2_dbg(1, debug, sd,
  465. "s_routing: Unknown input %d\n", input);
  466. break;
  467. }
  468. /* hack: CDMLPF sometimes spontaneously switches on; */
  469. /* force back off */
  470. ks0127_write(sd, KS_DEMOD, reg_defaults[KS_DEMOD]);
  471. return 0;
  472. }
  473. static int ks0127_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  474. {
  475. struct ks0127 *ks = to_ks0127(sd);
  476. /* Set to automatic SECAM/Fsc mode */
  477. ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x00);
  478. ks->norm = std;
  479. if (std & V4L2_STD_NTSC) {
  480. v4l2_dbg(1, debug, sd,
  481. "s_std: NTSC_M\n");
  482. ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x20);
  483. } else if (std & V4L2_STD_PAL_N) {
  484. v4l2_dbg(1, debug, sd,
  485. "s_std: NTSC_N (fixme)\n");
  486. ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x40);
  487. } else if (std & V4L2_STD_PAL) {
  488. v4l2_dbg(1, debug, sd,
  489. "s_std: PAL_N\n");
  490. ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x20);
  491. } else if (std & V4L2_STD_PAL_M) {
  492. v4l2_dbg(1, debug, sd,
  493. "s_std: PAL_M (fixme)\n");
  494. ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x40);
  495. } else if (std & V4L2_STD_SECAM) {
  496. v4l2_dbg(1, debug, sd,
  497. "s_std: SECAM\n");
  498. /* set to secam autodetection */
  499. ks0127_and_or(sd, KS_CHROMA, 0xdf, 0x20);
  500. ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x00);
  501. schedule_timeout_interruptible(HZ/10+1);
  502. /* did it autodetect? */
  503. if (!(ks0127_read(sd, KS_DEMOD) & 0x40))
  504. /* force to secam mode */
  505. ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x0f);
  506. } else {
  507. v4l2_dbg(1, debug, sd, "s_std: Unknown norm %llx\n",
  508. (unsigned long long)std);
  509. }
  510. return 0;
  511. }
  512. static int ks0127_s_stream(struct v4l2_subdev *sd, int enable)
  513. {
  514. v4l2_dbg(1, debug, sd, "s_stream(%d)\n", enable);
  515. if (enable) {
  516. /* All output pins on */
  517. ks0127_and_or(sd, KS_OFMTA, 0xcf, 0x30);
  518. /* Obey the OEN pin */
  519. ks0127_and_or(sd, KS_CDEM, 0x7f, 0x00);
  520. } else {
  521. /* Video output pins off */
  522. ks0127_and_or(sd, KS_OFMTA, 0xcf, 0x00);
  523. /* Ignore the OEN pin */
  524. ks0127_and_or(sd, KS_CDEM, 0x7f, 0x80);
  525. }
  526. return 0;
  527. }
  528. static int ks0127_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
  529. {
  530. int stat = V4L2_IN_ST_NO_SIGNAL;
  531. u8 status;
  532. v4l2_std_id std = pstd ? *pstd : V4L2_STD_ALL;
  533. status = ks0127_read(sd, KS_STAT);
  534. if (!(status & 0x20)) /* NOVID not set */
  535. stat = 0;
  536. if (!(status & 0x01)) { /* CLOCK set */
  537. stat |= V4L2_IN_ST_NO_COLOR;
  538. std = V4L2_STD_UNKNOWN;
  539. } else {
  540. if ((status & 0x08)) /* PALDET set */
  541. std &= V4L2_STD_PAL;
  542. else
  543. std &= V4L2_STD_NTSC;
  544. }
  545. if ((status & 0x10)) /* PALDET set */
  546. std &= V4L2_STD_525_60;
  547. else
  548. std &= V4L2_STD_625_50;
  549. if (pstd)
  550. *pstd = std;
  551. if (pstatus)
  552. *pstatus = stat;
  553. return 0;
  554. }
  555. static int ks0127_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  556. {
  557. v4l2_dbg(1, debug, sd, "querystd\n");
  558. return ks0127_status(sd, NULL, std);
  559. }
  560. static int ks0127_g_input_status(struct v4l2_subdev *sd, u32 *status)
  561. {
  562. v4l2_dbg(1, debug, sd, "g_input_status\n");
  563. return ks0127_status(sd, status, NULL);
  564. }
  565. /* ----------------------------------------------------------------------- */
  566. static const struct v4l2_subdev_video_ops ks0127_video_ops = {
  567. .s_std = ks0127_s_std,
  568. .s_routing = ks0127_s_routing,
  569. .s_stream = ks0127_s_stream,
  570. .querystd = ks0127_querystd,
  571. .g_input_status = ks0127_g_input_status,
  572. };
  573. static const struct v4l2_subdev_ops ks0127_ops = {
  574. .video = &ks0127_video_ops,
  575. };
  576. /* ----------------------------------------------------------------------- */
  577. static int ks0127_probe(struct i2c_client *client, const struct i2c_device_id *id)
  578. {
  579. struct ks0127 *ks;
  580. struct v4l2_subdev *sd;
  581. v4l_info(client, "%s chip found @ 0x%x (%s)\n",
  582. client->addr == (I2C_KS0127_ADDON >> 1) ? "addon" : "on-board",
  583. client->addr << 1, client->adapter->name);
  584. ks = devm_kzalloc(&client->dev, sizeof(*ks), GFP_KERNEL);
  585. if (ks == NULL)
  586. return -ENOMEM;
  587. sd = &ks->sd;
  588. v4l2_i2c_subdev_init(sd, client, &ks0127_ops);
  589. /* power up */
  590. init_reg_defaults();
  591. ks0127_write(sd, KS_CMDA, 0x2c);
  592. mdelay(10);
  593. /* reset the device */
  594. ks0127_init(sd);
  595. return 0;
  596. }
  597. static void ks0127_remove(struct i2c_client *client)
  598. {
  599. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  600. v4l2_device_unregister_subdev(sd);
  601. ks0127_write(sd, KS_OFMTA, 0x20); /* tristate */
  602. ks0127_write(sd, KS_CMDA, 0x2c | 0x80); /* power down */
  603. }
  604. static const struct i2c_device_id ks0127_id[] = {
  605. { "ks0127", 0 },
  606. { "ks0127b", 0 },
  607. { "ks0122s", 0 },
  608. { }
  609. };
  610. MODULE_DEVICE_TABLE(i2c, ks0127_id);
  611. static struct i2c_driver ks0127_driver = {
  612. .driver = {
  613. .name = "ks0127",
  614. },
  615. .probe = ks0127_probe,
  616. .remove = ks0127_remove,
  617. .id_table = ks0127_id,
  618. };
  619. module_i2c_driver(ks0127_driver);