cx18-av-core.c 35 KB

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