cx18-av-core.c 40 KB

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