cx18-av-core.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 "cx18-driver.h"
  25. #include "cx18-io.h"
  26. int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  27. {
  28. u32 reg = 0xc40000 + (addr & ~3);
  29. u32 mask = 0xff;
  30. int shift = (addr & 3) * 8;
  31. u32 x = cx18_read_reg(cx, reg);
  32. x = (x & ~(mask << shift)) | ((u32)value << shift);
  33. cx18_write_reg(cx, x, reg);
  34. return 0;
  35. }
  36. int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
  37. {
  38. u32 reg = 0xc40000 + (addr & ~3);
  39. int shift = (addr & 3) * 8;
  40. u32 x = cx18_read_reg(cx, reg);
  41. x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
  42. cx18_write_reg_expect(cx, x, reg,
  43. ((u32)eval << shift), ((u32)mask << shift));
  44. return 0;
  45. }
  46. int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  47. {
  48. cx18_write_reg(cx, value, 0xc40000 + addr);
  49. return 0;
  50. }
  51. int
  52. cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
  53. {
  54. cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
  55. return 0;
  56. }
  57. int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
  58. {
  59. cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
  60. return 0;
  61. }
  62. u8 cx18_av_read(struct cx18 *cx, u16 addr)
  63. {
  64. u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
  65. int shift = (addr & 3) * 8;
  66. return (x >> shift) & 0xff;
  67. }
  68. u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  69. {
  70. return cx18_read_reg(cx, 0xc40000 + addr);
  71. }
  72. int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  73. u8 or_value)
  74. {
  75. return cx18_av_write(cx, addr,
  76. (cx18_av_read(cx, addr) & and_mask) |
  77. or_value);
  78. }
  79. int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  80. u32 or_value)
  81. {
  82. return cx18_av_write4(cx, addr,
  83. (cx18_av_read4(cx, addr) & and_mask) |
  84. or_value);
  85. }
  86. /* ----------------------------------------------------------------------- */
  87. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  88. enum cx18_av_audio_input aud_input);
  89. static void log_audio_status(struct cx18 *cx);
  90. static void log_video_status(struct cx18 *cx);
  91. /* ----------------------------------------------------------------------- */
  92. static void cx18_av_initialize(struct cx18 *cx)
  93. {
  94. struct cx18_av_state *state = &cx->av_state;
  95. u32 v;
  96. cx18_av_loadfw(cx);
  97. /* Stop 8051 code execution */
  98. cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
  99. 0x03000000, 0x13000000);
  100. /* initallize the PLL by toggling sleep bit */
  101. v = cx18_av_read4(cx, CXADEC_HOST_REG1);
  102. /* enable sleep mode - register appears to be read only... */
  103. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
  104. /* disable sleep mode */
  105. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
  106. v & 0xfffe, 0xffff);
  107. /* initialize DLLs */
  108. v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
  109. /* disable FLD */
  110. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
  111. /* enable FLD */
  112. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
  113. v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
  114. /* disable FLD */
  115. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
  116. /* enable FLD */
  117. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
  118. /* set analog bias currents. Set Vreg to 1.20V. */
  119. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
  120. v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
  121. /* enable TUNE_FIL_RST */
  122. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
  123. /* disable TUNE_FIL_RST */
  124. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
  125. v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
  126. /* enable 656 output */
  127. cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
  128. /* video output drive strength */
  129. cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
  130. /* reset video */
  131. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
  132. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
  133. /* set video to auto-detect */
  134. /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
  135. /* set the comb notch = 1 */
  136. cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
  137. /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
  138. /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
  139. cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
  140. /* Set VGA_TRACK_RANGE to 0x20 */
  141. cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
  142. /* Enable VBI capture */
  143. cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253F);
  144. /* cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253E); */
  145. /* Set the video input.
  146. The setting in MODE_CTRL gets lost when we do the above setup */
  147. /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
  148. /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
  149. v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
  150. v &= 0xFFFBFFFF; /* turn OFF bit 18 for droop_comp_ch1 */
  151. v &= 0xFFFF7FFF; /* turn OFF bit 9 for clamp_sel_ch1 */
  152. v &= 0xFFFFFFFE; /* turn OFF bit 0 for 12db_ch1 */
  153. /* v |= 0x00000001;*/ /* turn ON bit 0 for 12db_ch1 */
  154. cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
  155. /* if(dwEnable && dw3DCombAvailable) { */
  156. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
  157. /* } else { */
  158. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
  159. /* } */
  160. cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
  161. state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
  162. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  163. }
  164. /* ----------------------------------------------------------------------- */
  165. void cx18_av_std_setup(struct cx18 *cx)
  166. {
  167. struct cx18_av_state *state = &cx->av_state;
  168. v4l2_std_id std = state->std;
  169. int hblank, hactive, burst, vblank, vactive, sc;
  170. int vblank656, src_decimation;
  171. int luma_lpf, uv_lpf, comb;
  172. u32 pll_int, pll_frac, pll_post;
  173. /* datasheet startup, step 8d */
  174. if (std & ~V4L2_STD_NTSC)
  175. cx18_av_write(cx, 0x49f, 0x11);
  176. else
  177. cx18_av_write(cx, 0x49f, 0x14);
  178. if (std & V4L2_STD_625_50) {
  179. hblank = 132;
  180. hactive = 720;
  181. burst = 93;
  182. vblank = 36;
  183. vactive = 580;
  184. vblank656 = 40;
  185. src_decimation = 0x21f;
  186. luma_lpf = 2;
  187. if (std & V4L2_STD_PAL) {
  188. uv_lpf = 1;
  189. comb = 0x20;
  190. sc = 688739;
  191. } else if (std == V4L2_STD_PAL_Nc) {
  192. uv_lpf = 1;
  193. comb = 0x20;
  194. sc = 556453;
  195. } else { /* SECAM */
  196. uv_lpf = 0;
  197. comb = 0;
  198. sc = 672351;
  199. }
  200. } else {
  201. hactive = 720;
  202. hblank = 122;
  203. vactive = 487;
  204. luma_lpf = 1;
  205. uv_lpf = 1;
  206. vblank = 26;
  207. vblank656 = 26;
  208. src_decimation = 0x21f;
  209. if (std == V4L2_STD_PAL_60) {
  210. burst = 0x5b;
  211. luma_lpf = 2;
  212. comb = 0x20;
  213. sc = 688739;
  214. } else if (std == V4L2_STD_PAL_M) {
  215. burst = 0x61;
  216. comb = 0x20;
  217. sc = 555452;
  218. } else {
  219. burst = 0x5b;
  220. comb = 0x66;
  221. sc = 556063;
  222. }
  223. }
  224. /* DEBUG: Displays configured PLL frequency */
  225. pll_int = cx18_av_read(cx, 0x108);
  226. pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
  227. pll_post = cx18_av_read(cx, 0x109);
  228. CX18_DEBUG_INFO("PLL regs = int: %u, frac: %u, post: %u\n",
  229. pll_int, pll_frac, pll_post);
  230. if (pll_post) {
  231. int fin, fsc, pll;
  232. pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
  233. pll /= pll_post;
  234. CX18_DEBUG_INFO("PLL = %d.%06d MHz\n",
  235. pll / 1000000, pll % 1000000);
  236. CX18_DEBUG_INFO("PLL/8 = %d.%06d MHz\n",
  237. pll / 8000000, (pll / 8) % 1000000);
  238. fin = ((u64)src_decimation * pll) >> 12;
  239. CX18_DEBUG_INFO("ADC Sampling freq = %d.%06d MHz\n",
  240. fin / 1000000, fin % 1000000);
  241. fsc = (((u64)sc) * pll) >> 24L;
  242. CX18_DEBUG_INFO("Chroma sub-carrier freq = %d.%06d MHz\n",
  243. fsc / 1000000, fsc % 1000000);
  244. CX18_DEBUG_INFO("hblank %i, hactive %i, "
  245. "vblank %i , vactive %i, vblank656 %i, src_dec %i,"
  246. "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x,"
  247. " sc 0x%06x\n",
  248. hblank, hactive, vblank, vactive, vblank656,
  249. src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
  250. }
  251. /* Sets horizontal blanking delay and active lines */
  252. cx18_av_write(cx, 0x470, hblank);
  253. cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
  254. (hactive << 4)));
  255. cx18_av_write(cx, 0x472, hactive >> 4);
  256. /* Sets burst gate delay */
  257. cx18_av_write(cx, 0x473, burst);
  258. /* Sets vertical blanking delay and active duration */
  259. cx18_av_write(cx, 0x474, vblank);
  260. cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
  261. (vactive << 4)));
  262. cx18_av_write(cx, 0x476, vactive >> 4);
  263. cx18_av_write(cx, 0x477, vblank656);
  264. /* Sets src decimation rate */
  265. cx18_av_write(cx, 0x478, 0xff & src_decimation);
  266. cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
  267. /* Sets Luma and UV Low pass filters */
  268. cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  269. /* Enables comb filters */
  270. cx18_av_write(cx, 0x47b, comb);
  271. /* Sets SC Step*/
  272. cx18_av_write(cx, 0x47c, sc);
  273. cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
  274. cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
  275. /* Sets VBI parameters */
  276. if (std & V4L2_STD_625_50) {
  277. cx18_av_write(cx, 0x47f, 0x01);
  278. state->vbi_line_offset = 5;
  279. } else {
  280. cx18_av_write(cx, 0x47f, 0x00);
  281. state->vbi_line_offset = 8;
  282. }
  283. }
  284. /* ----------------------------------------------------------------------- */
  285. static void input_change(struct cx18 *cx)
  286. {
  287. struct cx18_av_state *state = &cx->av_state;
  288. v4l2_std_id std = state->std;
  289. u8 v;
  290. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  291. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  292. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  293. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  294. if (std & V4L2_STD_525_60) {
  295. if (std == V4L2_STD_NTSC_M_JP) {
  296. /* Japan uses EIAJ audio standard */
  297. cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
  298. cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
  299. } else if (std == V4L2_STD_NTSC_M_KR) {
  300. /* South Korea uses A2 audio standard */
  301. cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
  302. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  303. } else {
  304. /* Others use the BTSC audio standard */
  305. cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
  306. cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
  307. }
  308. } else if (std & V4L2_STD_PAL) {
  309. /* Follow tuner change procedure for PAL */
  310. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  311. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  312. } else if (std & V4L2_STD_SECAM) {
  313. /* Select autodetect for SECAM */
  314. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  315. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  316. }
  317. v = cx18_av_read(cx, 0x803);
  318. if (v & 0x10) {
  319. /* restart audio decoder microcontroller */
  320. v &= ~0x10;
  321. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  322. v |= 0x10;
  323. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  324. }
  325. }
  326. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  327. enum cx18_av_audio_input aud_input)
  328. {
  329. struct cx18_av_state *state = &cx->av_state;
  330. u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
  331. vid_input <= CX18_AV_COMPOSITE8);
  332. u8 reg;
  333. u8 v;
  334. CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
  335. vid_input, aud_input);
  336. if (is_composite) {
  337. reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  338. } else {
  339. int luma = vid_input & 0xf0;
  340. int chroma = vid_input & 0xf00;
  341. if ((vid_input & ~0xff0) ||
  342. luma < CX18_AV_SVIDEO_LUMA1 ||
  343. luma > CX18_AV_SVIDEO_LUMA8 ||
  344. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  345. chroma > CX18_AV_SVIDEO_CHROMA8) {
  346. CX18_ERR("0x%04x is not a valid video input!\n",
  347. vid_input);
  348. return -EINVAL;
  349. }
  350. reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  351. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  352. reg &= 0x3f;
  353. reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  354. } else {
  355. reg &= 0xcf;
  356. reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  357. }
  358. }
  359. switch (aud_input) {
  360. case CX18_AV_AUDIO_SERIAL1:
  361. case CX18_AV_AUDIO_SERIAL2:
  362. /* do nothing, use serial audio input */
  363. break;
  364. case CX18_AV_AUDIO4: reg &= ~0x30; break;
  365. case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  366. case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  367. case CX18_AV_AUDIO7: reg &= ~0xc0; break;
  368. case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  369. default:
  370. CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
  371. return -EINVAL;
  372. }
  373. cx18_av_write_expect(cx, 0x103, reg, reg, 0xf7);
  374. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  375. cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
  376. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  377. v = cx18_av_read(cx, 0x102);
  378. if (reg & 0x80)
  379. v &= ~0x2;
  380. else
  381. v |= 0x2;
  382. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  383. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  384. v |= 0x4;
  385. else
  386. v &= ~0x4;
  387. cx18_av_write_expect(cx, 0x102, v, v, 0x17);
  388. /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
  389. state->vid_input = vid_input;
  390. state->aud_input = aud_input;
  391. cx18_av_audio_set_path(cx);
  392. input_change(cx);
  393. return 0;
  394. }
  395. /* ----------------------------------------------------------------------- */
  396. static int set_v4lstd(struct cx18 *cx)
  397. {
  398. struct cx18_av_state *state = &cx->av_state;
  399. u8 fmt = 0; /* zero is autodetect */
  400. u8 pal_m = 0;
  401. /* First tests should be against specific std */
  402. if (state->std == V4L2_STD_NTSC_M_JP) {
  403. fmt = 0x2;
  404. } else if (state->std == V4L2_STD_NTSC_443) {
  405. fmt = 0x3;
  406. } else if (state->std == V4L2_STD_PAL_M) {
  407. pal_m = 1;
  408. fmt = 0x5;
  409. } else if (state->std == V4L2_STD_PAL_N) {
  410. fmt = 0x6;
  411. } else if (state->std == V4L2_STD_PAL_Nc) {
  412. fmt = 0x7;
  413. } else if (state->std == V4L2_STD_PAL_60) {
  414. fmt = 0x8;
  415. } else {
  416. /* Then, test against generic ones */
  417. if (state->std & V4L2_STD_NTSC)
  418. fmt = 0x1;
  419. else if (state->std & V4L2_STD_PAL)
  420. fmt = 0x4;
  421. else if (state->std & V4L2_STD_SECAM)
  422. fmt = 0xc;
  423. }
  424. CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
  425. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  426. Without this PAL may display a vertical ghosting effect.
  427. This happens for example with the Yuan MPC622. */
  428. if (fmt >= 4 && fmt < 8) {
  429. /* Set format to NTSC-M */
  430. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  431. /* Turn off LCOMB */
  432. cx18_av_and_or(cx, 0x47b, ~6, 0);
  433. }
  434. cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
  435. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  436. cx18_av_std_setup(cx);
  437. input_change(cx);
  438. return 0;
  439. }
  440. /* ----------------------------------------------------------------------- */
  441. static int set_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
  442. {
  443. switch (ctrl->id) {
  444. case V4L2_CID_BRIGHTNESS:
  445. if (ctrl->value < 0 || ctrl->value > 255) {
  446. CX18_ERR("invalid brightness setting %d\n",
  447. ctrl->value);
  448. return -ERANGE;
  449. }
  450. cx18_av_write(cx, 0x414, ctrl->value - 128);
  451. break;
  452. case V4L2_CID_CONTRAST:
  453. if (ctrl->value < 0 || ctrl->value > 127) {
  454. CX18_ERR("invalid contrast setting %d\n",
  455. ctrl->value);
  456. return -ERANGE;
  457. }
  458. cx18_av_write(cx, 0x415, ctrl->value << 1);
  459. break;
  460. case V4L2_CID_SATURATION:
  461. if (ctrl->value < 0 || ctrl->value > 127) {
  462. CX18_ERR("invalid saturation setting %d\n",
  463. ctrl->value);
  464. return -ERANGE;
  465. }
  466. cx18_av_write(cx, 0x420, ctrl->value << 1);
  467. cx18_av_write(cx, 0x421, ctrl->value << 1);
  468. break;
  469. case V4L2_CID_HUE:
  470. if (ctrl->value < -128 || ctrl->value > 127) {
  471. CX18_ERR("invalid hue setting %d\n", ctrl->value);
  472. return -ERANGE;
  473. }
  474. cx18_av_write(cx, 0x422, ctrl->value);
  475. break;
  476. case V4L2_CID_AUDIO_VOLUME:
  477. case V4L2_CID_AUDIO_BASS:
  478. case V4L2_CID_AUDIO_TREBLE:
  479. case V4L2_CID_AUDIO_BALANCE:
  480. case V4L2_CID_AUDIO_MUTE:
  481. return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
  482. default:
  483. return -EINVAL;
  484. }
  485. return 0;
  486. }
  487. static int get_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
  488. {
  489. switch (ctrl->id) {
  490. case V4L2_CID_BRIGHTNESS:
  491. ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
  492. break;
  493. case V4L2_CID_CONTRAST:
  494. ctrl->value = cx18_av_read(cx, 0x415) >> 1;
  495. break;
  496. case V4L2_CID_SATURATION:
  497. ctrl->value = cx18_av_read(cx, 0x420) >> 1;
  498. break;
  499. case V4L2_CID_HUE:
  500. ctrl->value = (s8)cx18_av_read(cx, 0x422);
  501. break;
  502. case V4L2_CID_AUDIO_VOLUME:
  503. case V4L2_CID_AUDIO_BASS:
  504. case V4L2_CID_AUDIO_TREBLE:
  505. case V4L2_CID_AUDIO_BALANCE:
  506. case V4L2_CID_AUDIO_MUTE:
  507. return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
  508. default:
  509. return -EINVAL;
  510. }
  511. return 0;
  512. }
  513. /* ----------------------------------------------------------------------- */
  514. static int get_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
  515. {
  516. switch (fmt->type) {
  517. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  518. return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
  519. default:
  520. return -EINVAL;
  521. }
  522. return 0;
  523. }
  524. static int set_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
  525. {
  526. struct cx18_av_state *state = &cx->av_state;
  527. struct v4l2_pix_format *pix;
  528. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  529. int is_50Hz = !(state->std & V4L2_STD_525_60);
  530. switch (fmt->type) {
  531. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  532. pix = &(fmt->fmt.pix);
  533. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  534. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  535. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  536. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  537. Vlines = pix->height + (is_50Hz ? 4 : 7);
  538. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  539. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  540. CX18_ERR("%dx%d is not a valid size!\n",
  541. pix->width, pix->height);
  542. return -ERANGE;
  543. }
  544. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  545. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  546. VSC &= 0x1fff;
  547. if (pix->width >= 385)
  548. filter = 0;
  549. else if (pix->width > 192)
  550. filter = 1;
  551. else if (pix->width > 96)
  552. filter = 2;
  553. else
  554. filter = 3;
  555. CX18_DEBUG_INFO("decoder set size %dx%d -> scale %ux%u\n",
  556. pix->width, pix->height, HSC, VSC);
  557. /* HSCALE=HSC */
  558. cx18_av_write(cx, 0x418, HSC & 0xff);
  559. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  560. cx18_av_write(cx, 0x41a, HSC >> 16);
  561. /* VSCALE=VSC */
  562. cx18_av_write(cx, 0x41c, VSC & 0xff);
  563. cx18_av_write(cx, 0x41d, VSC >> 8);
  564. /* VS_INTRLACE=1 VFILT=filter */
  565. cx18_av_write(cx, 0x41e, 0x8 | filter);
  566. break;
  567. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  568. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  569. case V4L2_BUF_TYPE_VBI_CAPTURE:
  570. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  571. default:
  572. return -EINVAL;
  573. }
  574. return 0;
  575. }
  576. /* ----------------------------------------------------------------------- */
  577. int cx18_av_cmd(struct cx18 *cx, unsigned int cmd, void *arg)
  578. {
  579. struct cx18_av_state *state = &cx->av_state;
  580. struct v4l2_tuner *vt = arg;
  581. struct v4l2_routing *route = arg;
  582. /* ignore these commands */
  583. switch (cmd) {
  584. case TUNER_SET_TYPE_ADDR:
  585. return 0;
  586. }
  587. if (!state->is_initialized) {
  588. CX18_DEBUG_INFO("cmd %08x triggered fw load\n", cmd);
  589. /* initialize on first use */
  590. state->is_initialized = 1;
  591. cx18_av_initialize(cx);
  592. }
  593. switch (cmd) {
  594. case VIDIOC_INT_DECODE_VBI_LINE:
  595. return cx18_av_vbi(cx, cmd, arg);
  596. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  597. return cx18_av_audio(cx, cmd, arg);
  598. case VIDIOC_STREAMON:
  599. CX18_DEBUG_INFO("enable output\n");
  600. cx18_av_write(cx, 0x115, 0x8c);
  601. cx18_av_write(cx, 0x116, 0x07);
  602. break;
  603. case VIDIOC_STREAMOFF:
  604. CX18_DEBUG_INFO("disable output\n");
  605. cx18_av_write(cx, 0x115, 0x00);
  606. cx18_av_write(cx, 0x116, 0x00);
  607. break;
  608. case VIDIOC_LOG_STATUS:
  609. log_video_status(cx);
  610. log_audio_status(cx);
  611. break;
  612. case VIDIOC_G_CTRL:
  613. return get_v4lctrl(cx, (struct v4l2_control *)arg);
  614. case VIDIOC_S_CTRL:
  615. return set_v4lctrl(cx, (struct v4l2_control *)arg);
  616. case VIDIOC_QUERYCTRL:
  617. {
  618. struct v4l2_queryctrl *qc = arg;
  619. switch (qc->id) {
  620. case V4L2_CID_BRIGHTNESS:
  621. case V4L2_CID_CONTRAST:
  622. case V4L2_CID_SATURATION:
  623. case V4L2_CID_HUE:
  624. return v4l2_ctrl_query_fill_std(qc);
  625. default:
  626. break;
  627. }
  628. switch (qc->id) {
  629. case V4L2_CID_AUDIO_VOLUME:
  630. return v4l2_ctrl_query_fill(qc, 0, 65535,
  631. 65535 / 100, state->default_volume);
  632. case V4L2_CID_AUDIO_MUTE:
  633. case V4L2_CID_AUDIO_BALANCE:
  634. case V4L2_CID_AUDIO_BASS:
  635. case V4L2_CID_AUDIO_TREBLE:
  636. return v4l2_ctrl_query_fill_std(qc);
  637. default:
  638. return -EINVAL;
  639. }
  640. return -EINVAL;
  641. }
  642. case VIDIOC_G_STD:
  643. *(v4l2_std_id *)arg = state->std;
  644. break;
  645. case VIDIOC_S_STD:
  646. if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
  647. return 0;
  648. state->radio = 0;
  649. state->std = *(v4l2_std_id *)arg;
  650. return set_v4lstd(cx);
  651. case AUDC_SET_RADIO:
  652. state->radio = 1;
  653. break;
  654. case VIDIOC_INT_G_VIDEO_ROUTING:
  655. route->input = state->vid_input;
  656. route->output = 0;
  657. break;
  658. case VIDIOC_INT_S_VIDEO_ROUTING:
  659. return set_input(cx, route->input, state->aud_input);
  660. case VIDIOC_INT_G_AUDIO_ROUTING:
  661. route->input = state->aud_input;
  662. route->output = 0;
  663. break;
  664. case VIDIOC_INT_S_AUDIO_ROUTING:
  665. return set_input(cx, state->vid_input, route->input);
  666. case VIDIOC_S_FREQUENCY:
  667. input_change(cx);
  668. break;
  669. case VIDIOC_G_TUNER:
  670. {
  671. u8 vpres = cx18_av_read(cx, 0x40e) & 0x20;
  672. u8 mode;
  673. int val = 0;
  674. if (state->radio)
  675. break;
  676. vt->signal = vpres ? 0xffff : 0x0;
  677. vt->capability |=
  678. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  679. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  680. mode = cx18_av_read(cx, 0x804);
  681. /* get rxsubchans and audmode */
  682. if ((mode & 0xf) == 1)
  683. val |= V4L2_TUNER_SUB_STEREO;
  684. else
  685. val |= V4L2_TUNER_SUB_MONO;
  686. if (mode == 2 || mode == 4)
  687. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  688. if (mode & 0x10)
  689. val |= V4L2_TUNER_SUB_SAP;
  690. vt->rxsubchans = val;
  691. vt->audmode = state->audmode;
  692. break;
  693. }
  694. case VIDIOC_S_TUNER:
  695. {
  696. u8 v;
  697. if (state->radio)
  698. break;
  699. v = cx18_av_read(cx, 0x809);
  700. v &= ~0xf;
  701. switch (vt->audmode) {
  702. case V4L2_TUNER_MODE_MONO:
  703. /* mono -> mono
  704. stereo -> mono
  705. bilingual -> lang1 */
  706. break;
  707. case V4L2_TUNER_MODE_STEREO:
  708. case V4L2_TUNER_MODE_LANG1:
  709. /* mono -> mono
  710. stereo -> stereo
  711. bilingual -> lang1 */
  712. v |= 0x4;
  713. break;
  714. case V4L2_TUNER_MODE_LANG1_LANG2:
  715. /* mono -> mono
  716. stereo -> stereo
  717. bilingual -> lang1/lang2 */
  718. v |= 0x7;
  719. break;
  720. case V4L2_TUNER_MODE_LANG2:
  721. /* mono -> mono
  722. stereo -> stereo
  723. bilingual -> lang2 */
  724. v |= 0x1;
  725. break;
  726. default:
  727. return -EINVAL;
  728. }
  729. cx18_av_write_expect(cx, 0x809, v, v, 0xff);
  730. state->audmode = vt->audmode;
  731. break;
  732. }
  733. case VIDIOC_G_FMT:
  734. return get_v4lfmt(cx, (struct v4l2_format *)arg);
  735. case VIDIOC_S_FMT:
  736. return set_v4lfmt(cx, (struct v4l2_format *)arg);
  737. case VIDIOC_INT_RESET:
  738. cx18_av_initialize(cx);
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. return 0;
  744. }
  745. /* ----------------------------------------------------------------------- */
  746. /* ----------------------------------------------------------------------- */
  747. static void log_video_status(struct cx18 *cx)
  748. {
  749. static const char *const fmt_strs[] = {
  750. "0x0",
  751. "NTSC-M", "NTSC-J", "NTSC-4.43",
  752. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  753. "0x9", "0xA", "0xB",
  754. "SECAM",
  755. "0xD", "0xE", "0xF"
  756. };
  757. struct cx18_av_state *state = &cx->av_state;
  758. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  759. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  760. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  761. int vid_input = state->vid_input;
  762. CX18_INFO("Video signal: %spresent\n",
  763. (gen_stat2 & 0x20) ? "" : "not ");
  764. CX18_INFO("Detected format: %s\n",
  765. fmt_strs[gen_stat1 & 0xf]);
  766. CX18_INFO("Specified standard: %s\n",
  767. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  768. if (vid_input >= CX18_AV_COMPOSITE1 &&
  769. vid_input <= CX18_AV_COMPOSITE8) {
  770. CX18_INFO("Specified video input: Composite %d\n",
  771. vid_input - CX18_AV_COMPOSITE1 + 1);
  772. } else {
  773. CX18_INFO("Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  774. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  775. }
  776. CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
  777. }
  778. /* ----------------------------------------------------------------------- */
  779. static void log_audio_status(struct cx18 *cx)
  780. {
  781. struct cx18_av_state *state = &cx->av_state;
  782. u8 download_ctl = cx18_av_read(cx, 0x803);
  783. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  784. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  785. u8 audio_config = cx18_av_read(cx, 0x808);
  786. u8 pref_mode = cx18_av_read(cx, 0x809);
  787. u8 afc0 = cx18_av_read(cx, 0x80b);
  788. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  789. int aud_input = state->aud_input;
  790. char *p;
  791. switch (mod_det_stat0) {
  792. case 0x00: p = "mono"; break;
  793. case 0x01: p = "stereo"; break;
  794. case 0x02: p = "dual"; break;
  795. case 0x04: p = "tri"; break;
  796. case 0x10: p = "mono with SAP"; break;
  797. case 0x11: p = "stereo with SAP"; break;
  798. case 0x12: p = "dual with SAP"; break;
  799. case 0x14: p = "tri with SAP"; break;
  800. case 0xfe: p = "forced mode"; break;
  801. default: p = "not defined"; break;
  802. }
  803. CX18_INFO("Detected audio mode: %s\n", p);
  804. switch (mod_det_stat1) {
  805. case 0x00: p = "not defined"; break;
  806. case 0x01: p = "EIAJ"; break;
  807. case 0x02: p = "A2-M"; break;
  808. case 0x03: p = "A2-BG"; break;
  809. case 0x04: p = "A2-DK1"; break;
  810. case 0x05: p = "A2-DK2"; break;
  811. case 0x06: p = "A2-DK3"; break;
  812. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  813. case 0x08: p = "AM-L"; break;
  814. case 0x09: p = "NICAM-BG"; break;
  815. case 0x0a: p = "NICAM-DK"; break;
  816. case 0x0b: p = "NICAM-I"; break;
  817. case 0x0c: p = "NICAM-L"; break;
  818. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  819. case 0x0e: p = "IF FM Radio"; break;
  820. case 0x0f: p = "BTSC"; break;
  821. case 0x10: p = "detected chrominance"; break;
  822. case 0xfd: p = "unknown audio standard"; break;
  823. case 0xfe: p = "forced audio standard"; break;
  824. case 0xff: p = "no detected audio standard"; break;
  825. default: p = "not defined"; break;
  826. }
  827. CX18_INFO("Detected audio standard: %s\n", p);
  828. CX18_INFO("Audio muted: %s\n",
  829. (mute_ctl & 0x2) ? "yes" : "no");
  830. CX18_INFO("Audio microcontroller: %s\n",
  831. (download_ctl & 0x10) ? "running" : "stopped");
  832. switch (audio_config >> 4) {
  833. case 0x00: p = "undefined"; break;
  834. case 0x01: p = "BTSC"; break;
  835. case 0x02: p = "EIAJ"; break;
  836. case 0x03: p = "A2-M"; break;
  837. case 0x04: p = "A2-BG"; break;
  838. case 0x05: p = "A2-DK1"; break;
  839. case 0x06: p = "A2-DK2"; break;
  840. case 0x07: p = "A2-DK3"; break;
  841. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  842. case 0x09: p = "AM-L"; break;
  843. case 0x0a: p = "NICAM-BG"; break;
  844. case 0x0b: p = "NICAM-DK"; break;
  845. case 0x0c: p = "NICAM-I"; break;
  846. case 0x0d: p = "NICAM-L"; break;
  847. case 0x0e: p = "FM radio"; break;
  848. case 0x0f: p = "automatic detection"; break;
  849. default: p = "undefined"; break;
  850. }
  851. CX18_INFO("Configured audio standard: %s\n", p);
  852. if ((audio_config >> 4) < 0xF) {
  853. switch (audio_config & 0xF) {
  854. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  855. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  856. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  857. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  858. case 0x04: p = "STEREO"; break;
  859. case 0x05: p = "DUAL1 (AC)"; break;
  860. case 0x06: p = "DUAL2 (BC)"; break;
  861. case 0x07: p = "DUAL3 (AB)"; break;
  862. default: p = "undefined";
  863. }
  864. CX18_INFO("Configured audio mode: %s\n", p);
  865. } else {
  866. switch (audio_config & 0xF) {
  867. case 0x00: p = "BG"; break;
  868. case 0x01: p = "DK1"; break;
  869. case 0x02: p = "DK2"; break;
  870. case 0x03: p = "DK3"; break;
  871. case 0x04: p = "I"; break;
  872. case 0x05: p = "L"; break;
  873. case 0x06: p = "BTSC"; break;
  874. case 0x07: p = "EIAJ"; break;
  875. case 0x08: p = "A2-M"; break;
  876. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  877. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  878. case 0x0b: p = "S-Video"; break;
  879. case 0x0f: p = "automatic standard and mode detection"; break;
  880. default: p = "undefined"; break;
  881. }
  882. CX18_INFO("Configured audio system: %s\n", p);
  883. }
  884. if (aud_input)
  885. CX18_INFO("Specified audio input: Tuner (In%d)\n",
  886. aud_input);
  887. else
  888. CX18_INFO("Specified audio input: External\n");
  889. switch (pref_mode & 0xf) {
  890. case 0: p = "mono/language A"; break;
  891. case 1: p = "language B"; break;
  892. case 2: p = "language C"; break;
  893. case 3: p = "analog fallback"; break;
  894. case 4: p = "stereo"; break;
  895. case 5: p = "language AC"; break;
  896. case 6: p = "language BC"; break;
  897. case 7: p = "language AB"; break;
  898. default: p = "undefined"; break;
  899. }
  900. CX18_INFO("Preferred audio mode: %s\n", p);
  901. if ((audio_config & 0xf) == 0xf) {
  902. switch ((afc0 >> 3) & 0x1) {
  903. case 0: p = "system DK"; break;
  904. case 1: p = "system L"; break;
  905. }
  906. CX18_INFO("Selected 65 MHz format: %s\n", p);
  907. switch (afc0 & 0x7) {
  908. case 0: p = "Chroma"; break;
  909. case 1: p = "BTSC"; break;
  910. case 2: p = "EIAJ"; break;
  911. case 3: p = "A2-M"; break;
  912. case 4: p = "autodetect"; break;
  913. default: p = "undefined"; break;
  914. }
  915. CX18_INFO("Selected 45 MHz format: %s\n", p);
  916. }
  917. }