cx18-av-core.c 34 KB

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