cx18-av-core.c 40 KB

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