cx18-av-core.c 40 KB

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