cx18-av-core.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-core.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@radix.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., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301, USA.
  23. */
  24. #include <media/v4l2-chip-ident.h>
  25. #include "cx18-driver.h"
  26. #include "cx18-io.h"
  27. #include "cx18-cards.h"
  28. int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  29. {
  30. u32 reg = 0xc40000 + (addr & ~3);
  31. u32 mask = 0xff;
  32. int shift = (addr & 3) * 8;
  33. u32 x = cx18_read_reg(cx, reg);
  34. x = (x & ~(mask << shift)) | ((u32)value << shift);
  35. cx18_write_reg(cx, x, reg);
  36. return 0;
  37. }
  38. int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
  39. {
  40. u32 reg = 0xc40000 + (addr & ~3);
  41. int shift = (addr & 3) * 8;
  42. u32 x = cx18_read_reg(cx, reg);
  43. x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
  44. cx18_write_reg_expect(cx, x, reg,
  45. ((u32)eval << shift), ((u32)mask << shift));
  46. return 0;
  47. }
  48. int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  49. {
  50. cx18_write_reg(cx, value, 0xc40000 + addr);
  51. return 0;
  52. }
  53. int
  54. cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
  55. {
  56. cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
  57. return 0;
  58. }
  59. int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
  60. {
  61. cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
  62. return 0;
  63. }
  64. u8 cx18_av_read(struct cx18 *cx, u16 addr)
  65. {
  66. u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
  67. int shift = (addr & 3) * 8;
  68. return (x >> shift) & 0xff;
  69. }
  70. u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  71. {
  72. return cx18_read_reg(cx, 0xc40000 + addr);
  73. }
  74. int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  75. u8 or_value)
  76. {
  77. return cx18_av_write(cx, addr,
  78. (cx18_av_read(cx, addr) & and_mask) |
  79. or_value);
  80. }
  81. int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  82. u32 or_value)
  83. {
  84. return cx18_av_write4(cx, addr,
  85. (cx18_av_read4(cx, addr) & and_mask) |
  86. or_value);
  87. }
  88. static void cx18_av_init(struct cx18 *cx)
  89. {
  90. /*
  91. * The crystal freq used in calculations in this driver will be
  92. * 28.636360 MHz.
  93. * Aim to run the PLLs' VCOs near 400 MHz to minimze errors.
  94. */
  95. /*
  96. * VDCLK Integer = 0x0f, Post Divider = 0x04
  97. * AIMCLK Integer = 0x0e, Post Divider = 0x16
  98. */
  99. cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);
  100. /* VDCLK Fraction = 0x2be2fe */
  101. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */
  102. cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);
  103. /* AIMCLK Fraction = 0x05227ad */
  104. /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/
  105. cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);
  106. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
  107. cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);
  108. }
  109. static void cx18_av_initialize(struct v4l2_subdev *sd)
  110. {
  111. struct cx18_av_state *state = to_cx18_av_state(sd);
  112. struct cx18 *cx = v4l2_get_subdevdata(sd);
  113. u32 v;
  114. cx18_av_loadfw(cx);
  115. /* Stop 8051 code execution */
  116. cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
  117. 0x03000000, 0x13000000);
  118. /* initallize the PLL by toggling sleep bit */
  119. v = cx18_av_read4(cx, CXADEC_HOST_REG1);
  120. /* enable sleep mode - register appears to be read only... */
  121. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
  122. /* disable sleep mode */
  123. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
  124. v & 0xfffe, 0xffff);
  125. /* initialize DLLs */
  126. v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
  127. /* disable FLD */
  128. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
  129. /* enable FLD */
  130. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
  131. v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
  132. /* disable FLD */
  133. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
  134. /* enable FLD */
  135. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
  136. /* set analog bias currents. Set Vreg to 1.20V. */
  137. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
  138. v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
  139. /* enable TUNE_FIL_RST */
  140. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
  141. /* disable TUNE_FIL_RST */
  142. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
  143. v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
  144. /* enable 656 output */
  145. cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
  146. /* video output drive strength */
  147. cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
  148. /* reset video */
  149. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
  150. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
  151. /*
  152. * Disable Video Auto-config of the Analog Front End and Video PLL.
  153. *
  154. * Since we only use BT.656 pixel mode, which works for both 525 and 625
  155. * line systems, it's just easier for us to set registers
  156. * 0x102 (CXADEC_CHIP_CTRL), 0x104-0x106 (CXADEC_AFE_CTRL),
  157. * 0x108-0x109 (CXADEC_PLL_CTRL1), and 0x10c-0x10f (CXADEC_VID_PLL_FRAC)
  158. * ourselves, than to run around cleaning up after the auto-config.
  159. *
  160. * (Note: my CX23418 chip doesn't seem to let the ACFG_DIS bit
  161. * get set to 1, but OTOH, it doesn't seem to do AFE and VID PLL
  162. * autoconfig either.)
  163. *
  164. * As a default, also turn off Dual mode for ADC2 and set ADC2 to CH3.
  165. */
  166. cx18_av_and_or4(cx, CXADEC_CHIP_CTRL, 0xFFFBFFFF, 0x00120000);
  167. /* Setup the Video and and Aux/Audio PLLs */
  168. cx18_av_init(cx);
  169. /* set video to auto-detect */
  170. /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
  171. /* set the comb notch = 1 */
  172. cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
  173. /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
  174. /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
  175. cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
  176. /* Set VGA_TRACK_RANGE to 0x20 */
  177. cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
  178. /*
  179. * Initial VBI setup
  180. * VIP-1.1, 10 bit mode, enable Raw, disable sliced,
  181. * don't clamp raw samples when codes are in use, 1 byte user D-words,
  182. * IDID0 has line #, RP code V bit transition on VBLANK, data during
  183. * blanking intervals
  184. */
  185. cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);
  186. /* Set the video input.
  187. The setting in MODE_CTRL gets lost when we do the above setup */
  188. /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
  189. /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
  190. /*
  191. * Analog Front End (AFE)
  192. * Default to luma on ch1/ADC1, chroma on ch2/ADC2, SIF on ch3/ADC2
  193. * bypass_ch[1-3] use filter
  194. * droop_comp_ch[1-3] disable
  195. * clamp_en_ch[1-3] disable
  196. * aud_in_sel ADC2
  197. * luma_in_sel ADC1
  198. * chroma_in_sel ADC2
  199. * clamp_sel_ch[2-3] midcode
  200. * clamp_sel_ch1 video decoder
  201. * vga_sel_ch3 audio decoder
  202. * vga_sel_ch[1-2] video decoder
  203. * half_bw_ch[1-3] disable
  204. * +12db_ch[1-3] disable
  205. */
  206. cx18_av_and_or4(cx, CXADEC_AFE_CTRL, 0xFF000000, 0x00005D00);
  207. /* if(dwEnable && dw3DCombAvailable) { */
  208. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
  209. /* } else { */
  210. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
  211. /* } */
  212. cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
  213. state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
  214. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  215. }
  216. static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)
  217. {
  218. cx18_av_initialize(sd);
  219. return 0;
  220. }
  221. static int cx18_av_load_fw(struct v4l2_subdev *sd)
  222. {
  223. struct cx18_av_state *state = to_cx18_av_state(sd);
  224. if (!state->is_initialized) {
  225. /* initialize on first use */
  226. state->is_initialized = 1;
  227. cx18_av_initialize(sd);
  228. }
  229. return 0;
  230. }
  231. void cx18_av_std_setup(struct cx18 *cx)
  232. {
  233. struct cx18_av_state *state = &cx->av_state;
  234. struct v4l2_subdev *sd = &state->sd;
  235. v4l2_std_id std = state->std;
  236. /*
  237. * Video ADC crystal clock to pixel clock SRC decimation ratio
  238. * 28.636360 MHz/13.5 Mpps * 256 = 0x21f.07b
  239. */
  240. const int src_decimation = 0x21f;
  241. int hblank, hactive, burst, vblank, vactive, sc;
  242. int vblank656;
  243. int luma_lpf, uv_lpf, comb;
  244. u32 pll_int, pll_frac, pll_post;
  245. /* datasheet startup, step 8d */
  246. if (std & ~V4L2_STD_NTSC)
  247. cx18_av_write(cx, 0x49f, 0x11);
  248. else
  249. cx18_av_write(cx, 0x49f, 0x14);
  250. /*
  251. * Note: At the end of a field, there are 3 sets of half line duration
  252. * (double horizontal rate) pulses:
  253. *
  254. * 5 (625) or 6 (525) half-lines to blank for the vertical retrace
  255. * 5 (625) or 6 (525) vertical sync pulses of half line duration
  256. * 5 (625) or 6 (525) half-lines of equalization pulses
  257. */
  258. if (std & V4L2_STD_625_50) {
  259. /*
  260. * The following relationships of half line counts should hold:
  261. * 625 = vblank656 + vactive
  262. * 10 = vblank656 - vblank = vsync pulses + equalization pulses
  263. *
  264. * vblank656: half lines after line 625/mid-313 of blanked video
  265. * vblank: half lines, after line 5/317, of blanked video
  266. * vactive: half lines of active video +
  267. * 5 half lines after the end of active video
  268. *
  269. * As far as I can tell:
  270. * vblank656 starts counting from the falling edge of the first
  271. * vsync pulse (start of line 1 or mid-313)
  272. * vblank starts counting from the after the 5 vsync pulses and
  273. * 5 or 4 equalization pulses (start of line 6 or 318)
  274. *
  275. * For 625 line systems the driver will extract VBI information
  276. * from lines 6-23 and lines 318-335 (but the slicer can only
  277. * handle 17 lines, not the 18 in the vblank region).
  278. * In addition, we need vblank656 and vblank to be one whole
  279. * line longer, to cover line 24 and 336, so the SAV/EAV RP
  280. * codes get generated such that the encoder can actually
  281. * extract line 23 & 335 (WSS). We'll lose 1 line in each field
  282. * at the top of the screen.
  283. *
  284. * It appears the 5 half lines that happen after active
  285. * video must be included in vactive (579 instead of 574),
  286. * otherwise the colors get badly displayed in various regions
  287. * of the screen. I guess the chroma comb filter gets confused
  288. * without them (at least when a PVR-350 is the PAL source).
  289. */
  290. vblank656 = 48; /* lines 1 - 24 & 313 - 336 */
  291. vblank = 38; /* lines 6 - 24 & 318 - 336 */
  292. vactive = 579; /* lines 24 - 313 & 337 - 626 */
  293. /*
  294. * For a 13.5 Mpps clock and 15,625 Hz line rate, a line is
  295. * is 864 pixels = 720 active + 144 blanking. ITU-R BT.601
  296. * specifies 12 luma clock periods or ~ 0.9 * 13.5 Mpps after
  297. * the end of active video to start a horizontal line, so that
  298. * leaves 132 pixels of hblank to ignore.
  299. */
  300. hblank = 132;
  301. hactive = 720;
  302. /*
  303. * Burst gate delay (for 625 line systems)
  304. * Hsync leading edge to color burst rise = 5.6 us
  305. * Color burst width = 2.25 us
  306. * Gate width = 4 pixel clocks
  307. * (5.6 us + 2.25/2 us) * 13.5 Mpps + 4/2 clocks = 92.79 clocks
  308. */
  309. burst = 93;
  310. luma_lpf = 2;
  311. if (std & V4L2_STD_PAL) {
  312. uv_lpf = 1;
  313. comb = 0x20;
  314. /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
  315. sc = 688700;
  316. } else if (std == V4L2_STD_PAL_Nc) {
  317. uv_lpf = 1;
  318. comb = 0x20;
  319. /* sc = 3582056.25 * src_decimation/28636360 * 2^13 */
  320. sc = 556422;
  321. } else { /* SECAM */
  322. uv_lpf = 0;
  323. comb = 0;
  324. /* (fr + fb)/2 = (4406260 + 4250000)/2 = 4328130 */
  325. /* sc = 4328130 * src_decimation/28636360 * 2^13 */
  326. sc = 672314;
  327. }
  328. } else {
  329. /*
  330. * The following relationships of half line counts should hold:
  331. * 525 = prevsync + vblank656 + vactive
  332. * 12 = vblank656 - vblank = vsync pulses + equalization pulses
  333. *
  334. * prevsync: 6 half-lines before the vsync pulses
  335. * vblank656: half lines, after line 3/mid-266, of blanked video
  336. * vblank: half lines, after line 9/272, of blanked video
  337. * vactive: half lines of active video
  338. *
  339. * As far as I can tell:
  340. * vblank656 starts counting from the falling edge of the first
  341. * vsync pulse (start of line 4 or mid-266)
  342. * vblank starts counting from the after the 6 vsync pulses and
  343. * 6 or 5 equalization pulses (start of line 10 or 272)
  344. *
  345. * For 525 line systems the driver will extract VBI information
  346. * from lines 10-21 and lines 273-284.
  347. */
  348. vblank656 = 38; /* lines 4 - 22 & 266 - 284 */
  349. vblank = 26; /* lines 10 - 22 & 272 - 284 */
  350. vactive = 481; /* lines 23 - 263 & 285 - 525 */
  351. /*
  352. * For a 13.5 Mpps clock and 15,734.26 Hz line rate, a line is
  353. * is 858 pixels = 720 active + 138 blanking. The Hsync leading
  354. * edge should happen 1.2 us * 13.5 Mpps ~= 16 pixels after the
  355. * end of active video, leaving 122 pixels of hblank to ignore
  356. * before active video starts.
  357. */
  358. hactive = 720;
  359. hblank = 122;
  360. luma_lpf = 1;
  361. uv_lpf = 1;
  362. /*
  363. * Burst gate delay (for 525 line systems)
  364. * Hsync leading edge to color burst rise = 5.3 us
  365. * Color burst width = 2.5 us
  366. * Gate width = 4 pixel clocks
  367. * (5.3 us + 2.5/2 us) * 13.5 Mpps + 4/2 clocks = 90.425 clocks
  368. */
  369. if (std == V4L2_STD_PAL_60) {
  370. burst = 90;
  371. luma_lpf = 2;
  372. comb = 0x20;
  373. /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
  374. sc = 688700;
  375. } else if (std == V4L2_STD_PAL_M) {
  376. /* The 97 needs to be verified against PAL-M timings */
  377. burst = 97;
  378. comb = 0x20;
  379. /* sc = 3575611.49 * src_decimation/28636360 * 2^13 */
  380. sc = 555421;
  381. } else {
  382. burst = 90;
  383. comb = 0x66;
  384. /* sc = 3579545.45.. * src_decimation/28636360 * 2^13 */
  385. sc = 556032;
  386. }
  387. }
  388. /* DEBUG: Displays configured PLL frequency */
  389. pll_int = cx18_av_read(cx, 0x108);
  390. pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
  391. pll_post = cx18_av_read(cx, 0x109);
  392. CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",
  393. pll_int, pll_frac, pll_post);
  394. if (pll_post) {
  395. int fsc, pll;
  396. u64 tmp;
  397. pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
  398. pll /= pll_post;
  399. CX18_DEBUG_INFO_DEV(sd, "Video PLL = %d.%06d MHz\n",
  400. pll / 1000000, pll % 1000000);
  401. CX18_DEBUG_INFO_DEV(sd, "Pixel rate = %d.%06d Mpixel/sec\n",
  402. pll / 8000000, (pll / 8) % 1000000);
  403. CX18_DEBUG_INFO_DEV(sd, "ADC XTAL/pixel clock decimation ratio "
  404. "= %d.%03d\n", src_decimation / 256,
  405. ((src_decimation % 256) * 1000) / 256);
  406. tmp = 28636360 * (u64) sc;
  407. do_div(tmp, src_decimation);
  408. fsc = tmp >> 13;
  409. CX18_DEBUG_INFO_DEV(sd,
  410. "Chroma sub-carrier initial freq = %d.%06d "
  411. "MHz\n", fsc / 1000000, fsc % 1000000);
  412. CX18_DEBUG_INFO_DEV(sd, "hblank %i, hactive %i, vblank %i, "
  413. "vactive %i, vblank656 %i, src_dec %i, "
  414. "burst 0x%02x, luma_lpf %i, uv_lpf %i, "
  415. "comb 0x%02x, sc 0x%06x\n",
  416. hblank, hactive, vblank, vactive, vblank656,
  417. src_decimation, burst, luma_lpf, uv_lpf,
  418. comb, sc);
  419. }
  420. /* Sets horizontal blanking delay and active lines */
  421. cx18_av_write(cx, 0x470, hblank);
  422. cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
  423. (hactive << 4)));
  424. cx18_av_write(cx, 0x472, hactive >> 4);
  425. /* Sets burst gate delay */
  426. cx18_av_write(cx, 0x473, burst);
  427. /* Sets vertical blanking delay and active duration */
  428. cx18_av_write(cx, 0x474, vblank);
  429. cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
  430. (vactive << 4)));
  431. cx18_av_write(cx, 0x476, vactive >> 4);
  432. cx18_av_write(cx, 0x477, vblank656);
  433. /* Sets src decimation rate */
  434. cx18_av_write(cx, 0x478, 0xff & src_decimation);
  435. cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
  436. /* Sets Luma and UV Low pass filters */
  437. cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  438. /* Enables comb filters */
  439. cx18_av_write(cx, 0x47b, comb);
  440. /* Sets SC Step*/
  441. cx18_av_write(cx, 0x47c, sc);
  442. cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
  443. cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
  444. if (std & V4L2_STD_625_50) {
  445. state->slicer_line_delay = 1;
  446. state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
  447. } else {
  448. state->slicer_line_delay = 0;
  449. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  450. }
  451. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  452. }
  453. static void input_change(struct cx18 *cx)
  454. {
  455. struct cx18_av_state *state = &cx->av_state;
  456. v4l2_std_id std = state->std;
  457. u8 v;
  458. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  459. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  460. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  461. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  462. if (std & V4L2_STD_525_60) {
  463. if (std == V4L2_STD_NTSC_M_JP) {
  464. /* Japan uses EIAJ audio standard */
  465. cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
  466. cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
  467. } else if (std == V4L2_STD_NTSC_M_KR) {
  468. /* South Korea uses A2 audio standard */
  469. cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
  470. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  471. } else {
  472. /* Others use the BTSC audio standard */
  473. cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
  474. cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
  475. }
  476. } else if (std & V4L2_STD_PAL) {
  477. /* Follow tuner change procedure for PAL */
  478. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  479. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  480. } else if (std & V4L2_STD_SECAM) {
  481. /* Select autodetect for SECAM */
  482. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  483. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  484. }
  485. v = cx18_av_read(cx, 0x803);
  486. if (v & 0x10) {
  487. /* restart audio decoder microcontroller */
  488. v &= ~0x10;
  489. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  490. v |= 0x10;
  491. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  492. }
  493. }
  494. static int cx18_av_s_frequency(struct v4l2_subdev *sd,
  495. struct v4l2_frequency *freq)
  496. {
  497. struct cx18 *cx = v4l2_get_subdevdata(sd);
  498. input_change(cx);
  499. return 0;
  500. }
  501. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  502. enum cx18_av_audio_input aud_input)
  503. {
  504. struct cx18_av_state *state = &cx->av_state;
  505. struct v4l2_subdev *sd = &state->sd;
  506. enum analog_signal_type {
  507. NONE, CVBS, Y, C, SIF, Pb, Pr
  508. } ch[3] = {NONE, NONE, NONE};
  509. u8 afe_mux_cfg;
  510. u8 adc2_cfg;
  511. u8 input_mode;
  512. u32 afe_cfg;
  513. int i;
  514. CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",
  515. vid_input, aud_input);
  516. if (vid_input >= CX18_AV_COMPOSITE1 &&
  517. vid_input <= CX18_AV_COMPOSITE8) {
  518. afe_mux_cfg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  519. ch[0] = CVBS;
  520. input_mode = 0x0;
  521. } else if (vid_input >= CX18_AV_COMPONENT_LUMA1) {
  522. int luma = vid_input & 0xf000;
  523. int r_chroma = vid_input & 0xf0000;
  524. int b_chroma = vid_input & 0xf00000;
  525. if ((vid_input & ~0xfff000) ||
  526. luma < CX18_AV_COMPONENT_LUMA1 ||
  527. luma > CX18_AV_COMPONENT_LUMA8 ||
  528. r_chroma < CX18_AV_COMPONENT_R_CHROMA4 ||
  529. r_chroma > CX18_AV_COMPONENT_R_CHROMA6 ||
  530. b_chroma < CX18_AV_COMPONENT_B_CHROMA7 ||
  531. b_chroma > CX18_AV_COMPONENT_B_CHROMA8) {
  532. CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
  533. vid_input);
  534. return -EINVAL;
  535. }
  536. afe_mux_cfg = (luma - CX18_AV_COMPONENT_LUMA1) >> 12;
  537. ch[0] = Y;
  538. afe_mux_cfg |= (r_chroma - CX18_AV_COMPONENT_R_CHROMA4) >> 12;
  539. ch[1] = Pr;
  540. afe_mux_cfg |= (b_chroma - CX18_AV_COMPONENT_B_CHROMA7) >> 14;
  541. ch[2] = Pb;
  542. input_mode = 0x6;
  543. } else {
  544. int luma = vid_input & 0xf0;
  545. int chroma = vid_input & 0xf00;
  546. if ((vid_input & ~0xff0) ||
  547. luma < CX18_AV_SVIDEO_LUMA1 ||
  548. luma > CX18_AV_SVIDEO_LUMA8 ||
  549. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  550. chroma > CX18_AV_SVIDEO_CHROMA8) {
  551. CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
  552. vid_input);
  553. return -EINVAL;
  554. }
  555. afe_mux_cfg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  556. ch[0] = Y;
  557. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  558. afe_mux_cfg &= 0x3f;
  559. afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  560. ch[2] = C;
  561. } else {
  562. afe_mux_cfg &= 0xcf;
  563. afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  564. ch[1] = C;
  565. }
  566. input_mode = 0x2;
  567. }
  568. switch (aud_input) {
  569. case CX18_AV_AUDIO_SERIAL1:
  570. case CX18_AV_AUDIO_SERIAL2:
  571. /* do nothing, use serial audio input */
  572. break;
  573. case CX18_AV_AUDIO4:
  574. afe_mux_cfg &= ~0x30;
  575. ch[1] = SIF;
  576. break;
  577. case CX18_AV_AUDIO5:
  578. afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x10;
  579. ch[1] = SIF;
  580. break;
  581. case CX18_AV_AUDIO6:
  582. afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x20;
  583. ch[1] = SIF;
  584. break;
  585. case CX18_AV_AUDIO7:
  586. afe_mux_cfg &= ~0xc0;
  587. ch[2] = SIF;
  588. break;
  589. case CX18_AV_AUDIO8:
  590. afe_mux_cfg = (afe_mux_cfg & ~0xc0) | 0x40;
  591. ch[2] = SIF;
  592. break;
  593. default:
  594. CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",
  595. aud_input);
  596. return -EINVAL;
  597. }
  598. /* Set up analog front end multiplexers */
  599. cx18_av_write_expect(cx, 0x103, afe_mux_cfg, afe_mux_cfg, 0xf7);
  600. /* Set INPUT_MODE to Composite, S-Video, or Component */
  601. cx18_av_and_or(cx, 0x401, ~0x6, input_mode);
  602. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  603. adc2_cfg = cx18_av_read(cx, 0x102);
  604. if (ch[2] == NONE)
  605. adc2_cfg &= ~0x2; /* No sig on CH3, set ADC2 to CH2 for input */
  606. else
  607. adc2_cfg |= 0x2; /* Signal on CH3, set ADC2 to CH3 for input */
  608. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  609. if (ch[1] != NONE && ch[2] != NONE)
  610. adc2_cfg |= 0x4; /* Set dual mode */
  611. else
  612. adc2_cfg &= ~0x4; /* Clear dual mode */
  613. cx18_av_write_expect(cx, 0x102, adc2_cfg, adc2_cfg, 0x17);
  614. /* Configure the analog front end */
  615. afe_cfg = cx18_av_read4(cx, CXADEC_AFE_CTRL);
  616. afe_cfg &= 0xff000000;
  617. afe_cfg |= 0x00005000; /* CHROMA_IN, AUD_IN: ADC2; LUMA_IN: ADC1 */
  618. if (ch[1] != NONE && ch[2] != NONE)
  619. afe_cfg |= 0x00000030; /* half_bw_ch[2-3] since in dual mode */
  620. for (i = 0; i < 3; i++) {
  621. switch (ch[i]) {
  622. default:
  623. case NONE:
  624. /* CLAMP_SEL = Fixed to midcode clamp level */
  625. afe_cfg |= (0x00000200 << i);
  626. break;
  627. case CVBS:
  628. case Y:
  629. if (i > 0)
  630. afe_cfg |= 0x00002000; /* LUMA_IN_SEL: ADC2 */
  631. break;
  632. case C:
  633. case Pb:
  634. case Pr:
  635. /* CLAMP_SEL = Fixed to midcode clamp level */
  636. afe_cfg |= (0x00000200 << i);
  637. if (i == 0 && ch[i] == C)
  638. afe_cfg &= ~0x00001000; /* CHROMA_IN_SEL ADC1 */
  639. break;
  640. case SIF:
  641. /*
  642. * VGA_GAIN_SEL = Audio Decoder
  643. * CLAMP_SEL = Fixed to midcode clamp level
  644. */
  645. afe_cfg |= (0x00000240 << i);
  646. if (i == 0)
  647. afe_cfg &= ~0x00004000; /* AUD_IN_SEL ADC1 */
  648. break;
  649. }
  650. }
  651. cx18_av_write4(cx, CXADEC_AFE_CTRL, afe_cfg);
  652. state->vid_input = vid_input;
  653. state->aud_input = aud_input;
  654. cx18_av_audio_set_path(cx);
  655. input_change(cx);
  656. return 0;
  657. }
  658. static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
  659. u32 input, u32 output, u32 config)
  660. {
  661. struct cx18_av_state *state = to_cx18_av_state(sd);
  662. struct cx18 *cx = v4l2_get_subdevdata(sd);
  663. return set_input(cx, input, state->aud_input);
  664. }
  665. static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
  666. u32 input, u32 output, u32 config)
  667. {
  668. struct cx18_av_state *state = to_cx18_av_state(sd);
  669. struct cx18 *cx = v4l2_get_subdevdata(sd);
  670. return set_input(cx, state->vid_input, input);
  671. }
  672. static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  673. {
  674. struct cx18_av_state *state = to_cx18_av_state(sd);
  675. struct cx18 *cx = v4l2_get_subdevdata(sd);
  676. u8 vpres;
  677. u8 mode;
  678. int val = 0;
  679. if (state->radio)
  680. return 0;
  681. vpres = cx18_av_read(cx, 0x40e) & 0x20;
  682. vt->signal = vpres ? 0xffff : 0x0;
  683. vt->capability |=
  684. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  685. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  686. mode = cx18_av_read(cx, 0x804);
  687. /* get rxsubchans and audmode */
  688. if ((mode & 0xf) == 1)
  689. val |= V4L2_TUNER_SUB_STEREO;
  690. else
  691. val |= V4L2_TUNER_SUB_MONO;
  692. if (mode == 2 || mode == 4)
  693. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  694. if (mode & 0x10)
  695. val |= V4L2_TUNER_SUB_SAP;
  696. vt->rxsubchans = val;
  697. vt->audmode = state->audmode;
  698. return 0;
  699. }
  700. static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  701. {
  702. struct cx18_av_state *state = to_cx18_av_state(sd);
  703. struct cx18 *cx = v4l2_get_subdevdata(sd);
  704. u8 v;
  705. if (state->radio)
  706. return 0;
  707. v = cx18_av_read(cx, 0x809);
  708. v &= ~0xf;
  709. switch (vt->audmode) {
  710. case V4L2_TUNER_MODE_MONO:
  711. /* mono -> mono
  712. stereo -> mono
  713. bilingual -> lang1 */
  714. break;
  715. case V4L2_TUNER_MODE_STEREO:
  716. case V4L2_TUNER_MODE_LANG1:
  717. /* mono -> mono
  718. stereo -> stereo
  719. bilingual -> lang1 */
  720. v |= 0x4;
  721. break;
  722. case V4L2_TUNER_MODE_LANG1_LANG2:
  723. /* mono -> mono
  724. stereo -> stereo
  725. bilingual -> lang1/lang2 */
  726. v |= 0x7;
  727. break;
  728. case V4L2_TUNER_MODE_LANG2:
  729. /* mono -> mono
  730. stereo -> stereo
  731. bilingual -> lang2 */
  732. v |= 0x1;
  733. break;
  734. default:
  735. return -EINVAL;
  736. }
  737. cx18_av_write_expect(cx, 0x809, v, v, 0xff);
  738. state->audmode = vt->audmode;
  739. return 0;
  740. }
  741. static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  742. {
  743. struct cx18_av_state *state = to_cx18_av_state(sd);
  744. struct cx18 *cx = v4l2_get_subdevdata(sd);
  745. u8 fmt = 0; /* zero is autodetect */
  746. u8 pal_m = 0;
  747. if (state->radio == 0 && state->std == norm)
  748. return 0;
  749. state->radio = 0;
  750. state->std = norm;
  751. /* First tests should be against specific std */
  752. if (state->std == V4L2_STD_NTSC_M_JP) {
  753. fmt = 0x2;
  754. } else if (state->std == V4L2_STD_NTSC_443) {
  755. fmt = 0x3;
  756. } else if (state->std == V4L2_STD_PAL_M) {
  757. pal_m = 1;
  758. fmt = 0x5;
  759. } else if (state->std == V4L2_STD_PAL_N) {
  760. fmt = 0x6;
  761. } else if (state->std == V4L2_STD_PAL_Nc) {
  762. fmt = 0x7;
  763. } else if (state->std == V4L2_STD_PAL_60) {
  764. fmt = 0x8;
  765. } else {
  766. /* Then, test against generic ones */
  767. if (state->std & V4L2_STD_NTSC)
  768. fmt = 0x1;
  769. else if (state->std & V4L2_STD_PAL)
  770. fmt = 0x4;
  771. else if (state->std & V4L2_STD_SECAM)
  772. fmt = 0xc;
  773. }
  774. CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);
  775. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  776. Without this PAL may display a vertical ghosting effect.
  777. This happens for example with the Yuan MPC622. */
  778. if (fmt >= 4 && fmt < 8) {
  779. /* Set format to NTSC-M */
  780. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  781. /* Turn off LCOMB */
  782. cx18_av_and_or(cx, 0x47b, ~6, 0);
  783. }
  784. cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
  785. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  786. cx18_av_std_setup(cx);
  787. input_change(cx);
  788. return 0;
  789. }
  790. static int cx18_av_s_radio(struct v4l2_subdev *sd)
  791. {
  792. struct cx18_av_state *state = to_cx18_av_state(sd);
  793. state->radio = 1;
  794. return 0;
  795. }
  796. static int cx18_av_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  797. {
  798. struct cx18 *cx = v4l2_get_subdevdata(sd);
  799. switch (ctrl->id) {
  800. case V4L2_CID_BRIGHTNESS:
  801. if (ctrl->value < 0 || ctrl->value > 255) {
  802. CX18_ERR_DEV(sd, "invalid brightness setting %d\n",
  803. ctrl->value);
  804. return -ERANGE;
  805. }
  806. cx18_av_write(cx, 0x414, ctrl->value - 128);
  807. break;
  808. case V4L2_CID_CONTRAST:
  809. if (ctrl->value < 0 || ctrl->value > 127) {
  810. CX18_ERR_DEV(sd, "invalid contrast setting %d\n",
  811. ctrl->value);
  812. return -ERANGE;
  813. }
  814. cx18_av_write(cx, 0x415, ctrl->value << 1);
  815. break;
  816. case V4L2_CID_SATURATION:
  817. if (ctrl->value < 0 || ctrl->value > 127) {
  818. CX18_ERR_DEV(sd, "invalid saturation setting %d\n",
  819. ctrl->value);
  820. return -ERANGE;
  821. }
  822. cx18_av_write(cx, 0x420, ctrl->value << 1);
  823. cx18_av_write(cx, 0x421, ctrl->value << 1);
  824. break;
  825. case V4L2_CID_HUE:
  826. if (ctrl->value < -128 || ctrl->value > 127) {
  827. CX18_ERR_DEV(sd, "invalid hue setting %d\n",
  828. ctrl->value);
  829. return -ERANGE;
  830. }
  831. cx18_av_write(cx, 0x422, ctrl->value);
  832. break;
  833. case V4L2_CID_AUDIO_VOLUME:
  834. case V4L2_CID_AUDIO_BASS:
  835. case V4L2_CID_AUDIO_TREBLE:
  836. case V4L2_CID_AUDIO_BALANCE:
  837. case V4L2_CID_AUDIO_MUTE:
  838. return cx18_av_audio_s_ctrl(cx, ctrl);
  839. default:
  840. return -EINVAL;
  841. }
  842. return 0;
  843. }
  844. static int cx18_av_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  845. {
  846. struct cx18 *cx = v4l2_get_subdevdata(sd);
  847. switch (ctrl->id) {
  848. case V4L2_CID_BRIGHTNESS:
  849. ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
  850. break;
  851. case V4L2_CID_CONTRAST:
  852. ctrl->value = cx18_av_read(cx, 0x415) >> 1;
  853. break;
  854. case V4L2_CID_SATURATION:
  855. ctrl->value = cx18_av_read(cx, 0x420) >> 1;
  856. break;
  857. case V4L2_CID_HUE:
  858. ctrl->value = (s8)cx18_av_read(cx, 0x422);
  859. break;
  860. case V4L2_CID_AUDIO_VOLUME:
  861. case V4L2_CID_AUDIO_BASS:
  862. case V4L2_CID_AUDIO_TREBLE:
  863. case V4L2_CID_AUDIO_BALANCE:
  864. case V4L2_CID_AUDIO_MUTE:
  865. return cx18_av_audio_g_ctrl(cx, ctrl);
  866. default:
  867. return -EINVAL;
  868. }
  869. return 0;
  870. }
  871. static int cx18_av_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  872. {
  873. struct cx18_av_state *state = to_cx18_av_state(sd);
  874. switch (qc->id) {
  875. case V4L2_CID_BRIGHTNESS:
  876. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  877. case V4L2_CID_CONTRAST:
  878. case V4L2_CID_SATURATION:
  879. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  880. case V4L2_CID_HUE:
  881. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  882. default:
  883. break;
  884. }
  885. switch (qc->id) {
  886. case V4L2_CID_AUDIO_VOLUME:
  887. return v4l2_ctrl_query_fill(qc, 0, 65535,
  888. 65535 / 100, state->default_volume);
  889. case V4L2_CID_AUDIO_MUTE:
  890. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  891. case V4L2_CID_AUDIO_BALANCE:
  892. case V4L2_CID_AUDIO_BASS:
  893. case V4L2_CID_AUDIO_TREBLE:
  894. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
  895. default:
  896. return -EINVAL;
  897. }
  898. return -EINVAL;
  899. }
  900. static int cx18_av_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  901. {
  902. struct cx18 *cx = v4l2_get_subdevdata(sd);
  903. return cx18_av_vbi_g_fmt(cx, fmt);
  904. }
  905. static int cx18_av_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  906. {
  907. struct cx18_av_state *state = to_cx18_av_state(sd);
  908. struct cx18 *cx = v4l2_get_subdevdata(sd);
  909. struct v4l2_pix_format *pix;
  910. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  911. int is_50Hz = !(state->std & V4L2_STD_525_60);
  912. switch (fmt->type) {
  913. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  914. pix = &(fmt->fmt.pix);
  915. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  916. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  917. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  918. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  919. /*
  920. * This adjustment reflects the excess of vactive, set in
  921. * cx18_av_std_setup(), above standard values:
  922. *
  923. * 480 + 1 for 60 Hz systems
  924. * 576 + 3 for 50 Hz systems
  925. */
  926. Vlines = pix->height + (is_50Hz ? 3 : 1);
  927. /*
  928. * Invalid height and width scaling requests are:
  929. * 1. width less than 1/16 of the source width
  930. * 2. width greater than the source width
  931. * 3. height less than 1/8 of the source height
  932. * 4. height greater than the source height
  933. */
  934. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  935. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  936. CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",
  937. pix->width, pix->height);
  938. return -ERANGE;
  939. }
  940. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  941. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  942. VSC &= 0x1fff;
  943. if (pix->width >= 385)
  944. filter = 0;
  945. else if (pix->width > 192)
  946. filter = 1;
  947. else if (pix->width > 96)
  948. filter = 2;
  949. else
  950. filter = 3;
  951. CX18_DEBUG_INFO_DEV(sd,
  952. "decoder set size %dx%d -> scale %ux%u\n",
  953. pix->width, pix->height, HSC, VSC);
  954. /* HSCALE=HSC */
  955. cx18_av_write(cx, 0x418, HSC & 0xff);
  956. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  957. cx18_av_write(cx, 0x41a, HSC >> 16);
  958. /* VSCALE=VSC */
  959. cx18_av_write(cx, 0x41c, VSC & 0xff);
  960. cx18_av_write(cx, 0x41d, VSC >> 8);
  961. /* VS_INTRLACE=1 VFILT=filter */
  962. cx18_av_write(cx, 0x41e, 0x8 | filter);
  963. break;
  964. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  965. return cx18_av_vbi_s_fmt(cx, fmt);
  966. case V4L2_BUF_TYPE_VBI_CAPTURE:
  967. return cx18_av_vbi_s_fmt(cx, fmt);
  968. default:
  969. return -EINVAL;
  970. }
  971. return 0;
  972. }
  973. static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
  974. {
  975. struct cx18 *cx = v4l2_get_subdevdata(sd);
  976. CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");
  977. if (enable) {
  978. cx18_av_write(cx, 0x115, 0x8c);
  979. cx18_av_write(cx, 0x116, 0x07);
  980. } else {
  981. cx18_av_write(cx, 0x115, 0x00);
  982. cx18_av_write(cx, 0x116, 0x00);
  983. }
  984. return 0;
  985. }
  986. static void log_video_status(struct cx18 *cx)
  987. {
  988. static const char *const fmt_strs[] = {
  989. "0x0",
  990. "NTSC-M", "NTSC-J", "NTSC-4.43",
  991. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  992. "0x9", "0xA", "0xB",
  993. "SECAM",
  994. "0xD", "0xE", "0xF"
  995. };
  996. struct cx18_av_state *state = &cx->av_state;
  997. struct v4l2_subdev *sd = &state->sd;
  998. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  999. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  1000. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  1001. int vid_input = state->vid_input;
  1002. CX18_INFO_DEV(sd, "Video signal: %spresent\n",
  1003. (gen_stat2 & 0x20) ? "" : "not ");
  1004. CX18_INFO_DEV(sd, "Detected format: %s\n",
  1005. fmt_strs[gen_stat1 & 0xf]);
  1006. CX18_INFO_DEV(sd, "Specified standard: %s\n",
  1007. vidfmt_sel ? fmt_strs[vidfmt_sel]
  1008. : "automatic detection");
  1009. if (vid_input >= CX18_AV_COMPOSITE1 &&
  1010. vid_input <= CX18_AV_COMPOSITE8) {
  1011. CX18_INFO_DEV(sd, "Specified video input: Composite %d\n",
  1012. vid_input - CX18_AV_COMPOSITE1 + 1);
  1013. } else {
  1014. CX18_INFO_DEV(sd, "Specified video input: "
  1015. "S-Video (Luma In%d, Chroma In%d)\n",
  1016. (vid_input & 0xf0) >> 4,
  1017. (vid_input & 0xf00) >> 8);
  1018. }
  1019. CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",
  1020. state->audclk_freq);
  1021. }
  1022. static void log_audio_status(struct cx18 *cx)
  1023. {
  1024. struct cx18_av_state *state = &cx->av_state;
  1025. struct v4l2_subdev *sd = &state->sd;
  1026. u8 download_ctl = cx18_av_read(cx, 0x803);
  1027. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  1028. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  1029. u8 audio_config = cx18_av_read(cx, 0x808);
  1030. u8 pref_mode = cx18_av_read(cx, 0x809);
  1031. u8 afc0 = cx18_av_read(cx, 0x80b);
  1032. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  1033. int aud_input = state->aud_input;
  1034. char *p;
  1035. switch (mod_det_stat0) {
  1036. case 0x00: p = "mono"; break;
  1037. case 0x01: p = "stereo"; break;
  1038. case 0x02: p = "dual"; break;
  1039. case 0x04: p = "tri"; break;
  1040. case 0x10: p = "mono with SAP"; break;
  1041. case 0x11: p = "stereo with SAP"; break;
  1042. case 0x12: p = "dual with SAP"; break;
  1043. case 0x14: p = "tri with SAP"; break;
  1044. case 0xfe: p = "forced mode"; break;
  1045. default: p = "not defined"; break;
  1046. }
  1047. CX18_INFO_DEV(sd, "Detected audio mode: %s\n", p);
  1048. switch (mod_det_stat1) {
  1049. case 0x00: p = "not defined"; break;
  1050. case 0x01: p = "EIAJ"; break;
  1051. case 0x02: p = "A2-M"; break;
  1052. case 0x03: p = "A2-BG"; break;
  1053. case 0x04: p = "A2-DK1"; break;
  1054. case 0x05: p = "A2-DK2"; break;
  1055. case 0x06: p = "A2-DK3"; break;
  1056. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  1057. case 0x08: p = "AM-L"; break;
  1058. case 0x09: p = "NICAM-BG"; break;
  1059. case 0x0a: p = "NICAM-DK"; break;
  1060. case 0x0b: p = "NICAM-I"; break;
  1061. case 0x0c: p = "NICAM-L"; break;
  1062. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  1063. case 0x0e: p = "IF FM Radio"; break;
  1064. case 0x0f: p = "BTSC"; break;
  1065. case 0x10: p = "detected chrominance"; break;
  1066. case 0xfd: p = "unknown audio standard"; break;
  1067. case 0xfe: p = "forced audio standard"; break;
  1068. case 0xff: p = "no detected audio standard"; break;
  1069. default: p = "not defined"; break;
  1070. }
  1071. CX18_INFO_DEV(sd, "Detected audio standard: %s\n", p);
  1072. CX18_INFO_DEV(sd, "Audio muted: %s\n",
  1073. (mute_ctl & 0x2) ? "yes" : "no");
  1074. CX18_INFO_DEV(sd, "Audio microcontroller: %s\n",
  1075. (download_ctl & 0x10) ? "running" : "stopped");
  1076. switch (audio_config >> 4) {
  1077. case 0x00: p = "undefined"; break;
  1078. case 0x01: p = "BTSC"; break;
  1079. case 0x02: p = "EIAJ"; break;
  1080. case 0x03: p = "A2-M"; break;
  1081. case 0x04: p = "A2-BG"; break;
  1082. case 0x05: p = "A2-DK1"; break;
  1083. case 0x06: p = "A2-DK2"; break;
  1084. case 0x07: p = "A2-DK3"; break;
  1085. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  1086. case 0x09: p = "AM-L"; break;
  1087. case 0x0a: p = "NICAM-BG"; break;
  1088. case 0x0b: p = "NICAM-DK"; break;
  1089. case 0x0c: p = "NICAM-I"; break;
  1090. case 0x0d: p = "NICAM-L"; break;
  1091. case 0x0e: p = "FM radio"; break;
  1092. case 0x0f: p = "automatic detection"; break;
  1093. default: p = "undefined"; break;
  1094. }
  1095. CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);
  1096. if ((audio_config >> 4) < 0xF) {
  1097. switch (audio_config & 0xF) {
  1098. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  1099. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  1100. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  1101. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  1102. case 0x04: p = "STEREO"; break;
  1103. case 0x05: p = "DUAL1 (AC)"; break;
  1104. case 0x06: p = "DUAL2 (BC)"; break;
  1105. case 0x07: p = "DUAL3 (AB)"; break;
  1106. default: p = "undefined";
  1107. }
  1108. CX18_INFO_DEV(sd, "Configured audio mode: %s\n", p);
  1109. } else {
  1110. switch (audio_config & 0xF) {
  1111. case 0x00: p = "BG"; break;
  1112. case 0x01: p = "DK1"; break;
  1113. case 0x02: p = "DK2"; break;
  1114. case 0x03: p = "DK3"; break;
  1115. case 0x04: p = "I"; break;
  1116. case 0x05: p = "L"; break;
  1117. case 0x06: p = "BTSC"; break;
  1118. case 0x07: p = "EIAJ"; break;
  1119. case 0x08: p = "A2-M"; break;
  1120. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  1121. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  1122. case 0x0b: p = "S-Video"; break;
  1123. case 0x0f: p = "automatic standard and mode detection"; break;
  1124. default: p = "undefined"; break;
  1125. }
  1126. CX18_INFO_DEV(sd, "Configured audio system: %s\n", p);
  1127. }
  1128. if (aud_input)
  1129. CX18_INFO_DEV(sd, "Specified audio input: Tuner (In%d)\n",
  1130. aud_input);
  1131. else
  1132. CX18_INFO_DEV(sd, "Specified audio input: External\n");
  1133. switch (pref_mode & 0xf) {
  1134. case 0: p = "mono/language A"; break;
  1135. case 1: p = "language B"; break;
  1136. case 2: p = "language C"; break;
  1137. case 3: p = "analog fallback"; break;
  1138. case 4: p = "stereo"; break;
  1139. case 5: p = "language AC"; break;
  1140. case 6: p = "language BC"; break;
  1141. case 7: p = "language AB"; break;
  1142. default: p = "undefined"; break;
  1143. }
  1144. CX18_INFO_DEV(sd, "Preferred audio mode: %s\n", p);
  1145. if ((audio_config & 0xf) == 0xf) {
  1146. switch ((afc0 >> 3) & 0x1) {
  1147. case 0: p = "system DK"; break;
  1148. case 1: p = "system L"; break;
  1149. }
  1150. CX18_INFO_DEV(sd, "Selected 65 MHz format: %s\n", p);
  1151. switch (afc0 & 0x7) {
  1152. case 0: p = "Chroma"; break;
  1153. case 1: p = "BTSC"; break;
  1154. case 2: p = "EIAJ"; break;
  1155. case 3: p = "A2-M"; break;
  1156. case 4: p = "autodetect"; break;
  1157. default: p = "undefined"; break;
  1158. }
  1159. CX18_INFO_DEV(sd, "Selected 45 MHz format: %s\n", p);
  1160. }
  1161. }
  1162. static int cx18_av_log_status(struct v4l2_subdev *sd)
  1163. {
  1164. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1165. log_video_status(cx);
  1166. log_audio_status(cx);
  1167. return 0;
  1168. }
  1169. static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)
  1170. {
  1171. return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;
  1172. }
  1173. static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,
  1174. struct v4l2_dbg_chip_ident *chip)
  1175. {
  1176. struct cx18_av_state *state = to_cx18_av_state(sd);
  1177. if (cx18_av_dbg_match(&chip->match)) {
  1178. chip->ident = state->id;
  1179. chip->revision = state->rev;
  1180. }
  1181. return 0;
  1182. }
  1183. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1184. static int cx18_av_g_register(struct v4l2_subdev *sd,
  1185. struct v4l2_dbg_register *reg)
  1186. {
  1187. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1188. if (!cx18_av_dbg_match(&reg->match))
  1189. return -EINVAL;
  1190. if ((reg->reg & 0x3) != 0)
  1191. return -EINVAL;
  1192. if (!capable(CAP_SYS_ADMIN))
  1193. return -EPERM;
  1194. reg->size = 4;
  1195. reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
  1196. return 0;
  1197. }
  1198. static int cx18_av_s_register(struct v4l2_subdev *sd,
  1199. struct v4l2_dbg_register *reg)
  1200. {
  1201. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1202. if (!cx18_av_dbg_match(&reg->match))
  1203. return -EINVAL;
  1204. if ((reg->reg & 0x3) != 0)
  1205. return -EINVAL;
  1206. if (!capable(CAP_SYS_ADMIN))
  1207. return -EPERM;
  1208. cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
  1209. return 0;
  1210. }
  1211. #endif
  1212. static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
  1213. .g_chip_ident = cx18_av_g_chip_ident,
  1214. .log_status = cx18_av_log_status,
  1215. .load_fw = cx18_av_load_fw,
  1216. .reset = cx18_av_reset,
  1217. .queryctrl = cx18_av_queryctrl,
  1218. .g_ctrl = cx18_av_g_ctrl,
  1219. .s_ctrl = cx18_av_s_ctrl,
  1220. .s_std = cx18_av_s_std,
  1221. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1222. .g_register = cx18_av_g_register,
  1223. .s_register = cx18_av_s_register,
  1224. #endif
  1225. };
  1226. static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
  1227. .s_radio = cx18_av_s_radio,
  1228. .s_frequency = cx18_av_s_frequency,
  1229. .g_tuner = cx18_av_g_tuner,
  1230. .s_tuner = cx18_av_s_tuner,
  1231. };
  1232. static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
  1233. .s_clock_freq = cx18_av_s_clock_freq,
  1234. .s_routing = cx18_av_s_audio_routing,
  1235. };
  1236. static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
  1237. .s_routing = cx18_av_s_video_routing,
  1238. .s_stream = cx18_av_s_stream,
  1239. .g_fmt = cx18_av_g_fmt,
  1240. .s_fmt = cx18_av_s_fmt,
  1241. };
  1242. static const struct v4l2_subdev_vbi_ops cx18_av_vbi_ops = {
  1243. .decode_vbi_line = cx18_av_decode_vbi_line,
  1244. };
  1245. static const struct v4l2_subdev_ops cx18_av_ops = {
  1246. .core = &cx18_av_general_ops,
  1247. .tuner = &cx18_av_tuner_ops,
  1248. .audio = &cx18_av_audio_ops,
  1249. .video = &cx18_av_video_ops,
  1250. .vbi = &cx18_av_vbi_ops,
  1251. };
  1252. int cx18_av_probe(struct cx18 *cx)
  1253. {
  1254. struct cx18_av_state *state = &cx->av_state;
  1255. struct v4l2_subdev *sd;
  1256. int err;
  1257. state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
  1258. state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)
  1259. ? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;
  1260. state->vid_input = CX18_AV_COMPOSITE7;
  1261. state->aud_input = CX18_AV_AUDIO8;
  1262. state->audclk_freq = 48000;
  1263. state->audmode = V4L2_TUNER_MODE_LANG1;
  1264. state->slicer_line_delay = 0;
  1265. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  1266. sd = &state->sd;
  1267. v4l2_subdev_init(sd, &cx18_av_ops);
  1268. v4l2_set_subdevdata(sd, cx);
  1269. snprintf(sd->name, sizeof(sd->name),
  1270. "%s %03x", cx->v4l2_dev.name, (state->rev >> 4));
  1271. sd->grp_id = CX18_HW_418_AV;
  1272. err = v4l2_device_register_subdev(&cx->v4l2_dev, sd);
  1273. if (!err)
  1274. cx18_av_init(cx);
  1275. return err;
  1276. }