saa7134-video.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * device driver for philips saa7134 based TV cards
  5. * video4linux video interface
  6. *
  7. * (c) 2001-03 Gerd Knorr <[email protected]> [SuSE Labs]
  8. */
  9. #include "saa7134.h"
  10. #include "saa7134-reg.h"
  11. #include <linux/init.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/sort.h>
  17. #include <media/v4l2-common.h>
  18. #include <media/v4l2-event.h>
  19. #include <media/i2c/saa6588.h>
  20. /* ------------------------------------------------------------------ */
  21. unsigned int video_debug;
  22. static unsigned int gbuffers = 8;
  23. static unsigned int noninterlaced; /* 0 */
  24. static unsigned int gbufsize = 720*576*4;
  25. static unsigned int gbufsize_max = 720*576*4;
  26. static char secam[] = "--";
  27. module_param(video_debug, int, 0644);
  28. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  29. module_param(gbuffers, int, 0444);
  30. MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
  31. module_param(noninterlaced, int, 0644);
  32. MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
  33. module_param_string(secam, secam, sizeof(secam), 0644);
  34. MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
  35. #define video_dbg(fmt, arg...) do { \
  36. if (video_debug & 0x04) \
  37. printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
  38. } while (0)
  39. /* ------------------------------------------------------------------ */
  40. /* Defines for Video Output Port Register at address 0x191 */
  41. /* Bit 0: VIP code T bit polarity */
  42. #define VP_T_CODE_P_NON_INVERTED 0x00
  43. #define VP_T_CODE_P_INVERTED 0x01
  44. /* ------------------------------------------------------------------ */
  45. /* Defines for Video Output Port Register at address 0x195 */
  46. /* Bit 2: Video output clock delay control */
  47. #define VP_CLK_CTRL2_NOT_DELAYED 0x00
  48. #define VP_CLK_CTRL2_DELAYED 0x04
  49. /* Bit 1: Video output clock invert control */
  50. #define VP_CLK_CTRL1_NON_INVERTED 0x00
  51. #define VP_CLK_CTRL1_INVERTED 0x02
  52. /* ------------------------------------------------------------------ */
  53. /* Defines for Video Output Port Register at address 0x196 */
  54. /* Bits 2 to 0: VSYNC pin video vertical sync type */
  55. #define VP_VS_TYPE_MASK 0x07
  56. #define VP_VS_TYPE_OFF 0x00
  57. #define VP_VS_TYPE_V123 0x01
  58. #define VP_VS_TYPE_V_ITU 0x02
  59. #define VP_VS_TYPE_VGATE_L 0x03
  60. #define VP_VS_TYPE_RESERVED1 0x04
  61. #define VP_VS_TYPE_RESERVED2 0x05
  62. #define VP_VS_TYPE_F_ITU 0x06
  63. #define VP_VS_TYPE_SC_FID 0x07
  64. /* ------------------------------------------------------------------ */
  65. /* data structs for video */
  66. static int video_out[][9] = {
  67. [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
  68. };
  69. static struct saa7134_format formats[] = {
  70. {
  71. .fourcc = V4L2_PIX_FMT_GREY,
  72. .depth = 8,
  73. .pm = 0x06,
  74. },{
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .depth = 16,
  77. .pm = 0x13 | 0x80,
  78. },{
  79. .fourcc = V4L2_PIX_FMT_RGB555X,
  80. .depth = 16,
  81. .pm = 0x13 | 0x80,
  82. .bswap = 1,
  83. },{
  84. .fourcc = V4L2_PIX_FMT_RGB565,
  85. .depth = 16,
  86. .pm = 0x10 | 0x80,
  87. },{
  88. .fourcc = V4L2_PIX_FMT_RGB565X,
  89. .depth = 16,
  90. .pm = 0x10 | 0x80,
  91. .bswap = 1,
  92. },{
  93. .fourcc = V4L2_PIX_FMT_BGR24,
  94. .depth = 24,
  95. .pm = 0x11,
  96. },{
  97. .fourcc = V4L2_PIX_FMT_RGB24,
  98. .depth = 24,
  99. .pm = 0x11,
  100. .bswap = 1,
  101. },{
  102. .fourcc = V4L2_PIX_FMT_BGR32,
  103. .depth = 32,
  104. .pm = 0x12,
  105. },{
  106. .fourcc = V4L2_PIX_FMT_RGB32,
  107. .depth = 32,
  108. .pm = 0x12,
  109. .bswap = 1,
  110. .wswap = 1,
  111. },{
  112. .fourcc = V4L2_PIX_FMT_YUYV,
  113. .depth = 16,
  114. .pm = 0x00,
  115. .bswap = 1,
  116. .yuv = 1,
  117. },{
  118. .fourcc = V4L2_PIX_FMT_UYVY,
  119. .depth = 16,
  120. .pm = 0x00,
  121. .yuv = 1,
  122. },{
  123. .fourcc = V4L2_PIX_FMT_YUV422P,
  124. .depth = 16,
  125. .pm = 0x09,
  126. .yuv = 1,
  127. .planar = 1,
  128. .hshift = 1,
  129. .vshift = 0,
  130. },{
  131. .fourcc = V4L2_PIX_FMT_YUV420,
  132. .depth = 12,
  133. .pm = 0x0a,
  134. .yuv = 1,
  135. .planar = 1,
  136. .hshift = 1,
  137. .vshift = 1,
  138. },{
  139. .fourcc = V4L2_PIX_FMT_YVU420,
  140. .depth = 12,
  141. .pm = 0x0a,
  142. .yuv = 1,
  143. .planar = 1,
  144. .uvswap = 1,
  145. .hshift = 1,
  146. .vshift = 1,
  147. }
  148. };
  149. #define FORMATS ARRAY_SIZE(formats)
  150. #define NORM_625_50 \
  151. .h_start = 0, \
  152. .h_stop = 719, \
  153. .video_v_start = 24, \
  154. .video_v_stop = 311, \
  155. .vbi_v_start_0 = 7, \
  156. .vbi_v_stop_0 = 23, \
  157. .vbi_v_start_1 = 319, \
  158. .src_timing = 4
  159. #define NORM_525_60 \
  160. .h_start = 0, \
  161. .h_stop = 719, \
  162. .video_v_start = 23, \
  163. .video_v_stop = 262, \
  164. .vbi_v_start_0 = 10, \
  165. .vbi_v_stop_0 = 21, \
  166. .vbi_v_start_1 = 273, \
  167. .src_timing = 7
  168. static struct saa7134_tvnorm tvnorms[] = {
  169. {
  170. .name = "PAL", /* autodetect */
  171. .id = V4L2_STD_PAL,
  172. NORM_625_50,
  173. .sync_control = 0x18,
  174. .luma_control = 0x40,
  175. .chroma_ctrl1 = 0x81,
  176. .chroma_gain = 0x2a,
  177. .chroma_ctrl2 = 0x06,
  178. .vgate_misc = 0x1c,
  179. },{
  180. .name = "PAL-BG",
  181. .id = V4L2_STD_PAL_BG,
  182. NORM_625_50,
  183. .sync_control = 0x18,
  184. .luma_control = 0x40,
  185. .chroma_ctrl1 = 0x81,
  186. .chroma_gain = 0x2a,
  187. .chroma_ctrl2 = 0x06,
  188. .vgate_misc = 0x1c,
  189. },{
  190. .name = "PAL-I",
  191. .id = V4L2_STD_PAL_I,
  192. NORM_625_50,
  193. .sync_control = 0x18,
  194. .luma_control = 0x40,
  195. .chroma_ctrl1 = 0x81,
  196. .chroma_gain = 0x2a,
  197. .chroma_ctrl2 = 0x06,
  198. .vgate_misc = 0x1c,
  199. },{
  200. .name = "PAL-DK",
  201. .id = V4L2_STD_PAL_DK,
  202. NORM_625_50,
  203. .sync_control = 0x18,
  204. .luma_control = 0x40,
  205. .chroma_ctrl1 = 0x81,
  206. .chroma_gain = 0x2a,
  207. .chroma_ctrl2 = 0x06,
  208. .vgate_misc = 0x1c,
  209. },{
  210. .name = "NTSC",
  211. .id = V4L2_STD_NTSC,
  212. NORM_525_60,
  213. .sync_control = 0x59,
  214. .luma_control = 0x40,
  215. .chroma_ctrl1 = 0x89,
  216. .chroma_gain = 0x2a,
  217. .chroma_ctrl2 = 0x0e,
  218. .vgate_misc = 0x18,
  219. },{
  220. .name = "SECAM",
  221. .id = V4L2_STD_SECAM,
  222. NORM_625_50,
  223. .sync_control = 0x18,
  224. .luma_control = 0x1b,
  225. .chroma_ctrl1 = 0xd1,
  226. .chroma_gain = 0x80,
  227. .chroma_ctrl2 = 0x00,
  228. .vgate_misc = 0x1c,
  229. },{
  230. .name = "SECAM-DK",
  231. .id = V4L2_STD_SECAM_DK,
  232. NORM_625_50,
  233. .sync_control = 0x18,
  234. .luma_control = 0x1b,
  235. .chroma_ctrl1 = 0xd1,
  236. .chroma_gain = 0x80,
  237. .chroma_ctrl2 = 0x00,
  238. .vgate_misc = 0x1c,
  239. },{
  240. .name = "SECAM-L",
  241. .id = V4L2_STD_SECAM_L,
  242. NORM_625_50,
  243. .sync_control = 0x18,
  244. .luma_control = 0x1b,
  245. .chroma_ctrl1 = 0xd1,
  246. .chroma_gain = 0x80,
  247. .chroma_ctrl2 = 0x00,
  248. .vgate_misc = 0x1c,
  249. },{
  250. .name = "SECAM-Lc",
  251. .id = V4L2_STD_SECAM_LC,
  252. NORM_625_50,
  253. .sync_control = 0x18,
  254. .luma_control = 0x1b,
  255. .chroma_ctrl1 = 0xd1,
  256. .chroma_gain = 0x80,
  257. .chroma_ctrl2 = 0x00,
  258. .vgate_misc = 0x1c,
  259. },{
  260. .name = "PAL-M",
  261. .id = V4L2_STD_PAL_M,
  262. NORM_525_60,
  263. .sync_control = 0x59,
  264. .luma_control = 0x40,
  265. .chroma_ctrl1 = 0xb9,
  266. .chroma_gain = 0x2a,
  267. .chroma_ctrl2 = 0x0e,
  268. .vgate_misc = 0x18,
  269. },{
  270. .name = "PAL-Nc",
  271. .id = V4L2_STD_PAL_Nc,
  272. NORM_625_50,
  273. .sync_control = 0x18,
  274. .luma_control = 0x40,
  275. .chroma_ctrl1 = 0xa1,
  276. .chroma_gain = 0x2a,
  277. .chroma_ctrl2 = 0x06,
  278. .vgate_misc = 0x1c,
  279. },{
  280. .name = "PAL-60",
  281. .id = V4L2_STD_PAL_60,
  282. .h_start = 0,
  283. .h_stop = 719,
  284. .video_v_start = 23,
  285. .video_v_stop = 262,
  286. .vbi_v_start_0 = 10,
  287. .vbi_v_stop_0 = 21,
  288. .vbi_v_start_1 = 273,
  289. .src_timing = 7,
  290. .sync_control = 0x18,
  291. .luma_control = 0x40,
  292. .chroma_ctrl1 = 0x81,
  293. .chroma_gain = 0x2a,
  294. .chroma_ctrl2 = 0x06,
  295. .vgate_misc = 0x1c,
  296. }
  297. };
  298. #define TVNORMS ARRAY_SIZE(tvnorms)
  299. static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
  300. {
  301. unsigned int i;
  302. for (i = 0; i < FORMATS; i++)
  303. if (formats[i].fourcc == fourcc)
  304. return formats+i;
  305. return NULL;
  306. }
  307. /* ------------------------------------------------------------------ */
  308. static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
  309. {
  310. video_dbg("set tv norm = %s\n", norm->name);
  311. dev->tvnorm = norm;
  312. /* setup cropping */
  313. dev->crop_bounds.left = norm->h_start;
  314. dev->crop_defrect.left = norm->h_start;
  315. dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
  316. dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
  317. dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
  318. dev->crop_defrect.top = norm->video_v_start*2;
  319. dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
  320. - dev->crop_bounds.top;
  321. dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
  322. dev->crop_current = dev->crop_defrect;
  323. saa7134_set_tvnorm_hw(dev);
  324. }
  325. static void video_mux(struct saa7134_dev *dev, int input)
  326. {
  327. video_dbg("video input = %d [%s]\n",
  328. input, saa7134_input_name[card_in(dev, input).type]);
  329. dev->ctl_input = input;
  330. set_tvnorm(dev, dev->tvnorm);
  331. saa7134_tvaudio_setinput(dev, &card_in(dev, input));
  332. }
  333. static void saa7134_set_decoder(struct saa7134_dev *dev)
  334. {
  335. int luma_control, sync_control, chroma_ctrl1, mux;
  336. struct saa7134_tvnorm *norm = dev->tvnorm;
  337. mux = card_in(dev, dev->ctl_input).vmux;
  338. luma_control = norm->luma_control;
  339. sync_control = norm->sync_control;
  340. chroma_ctrl1 = norm->chroma_ctrl1;
  341. if (mux > 5)
  342. luma_control |= 0x80; /* svideo */
  343. if (noninterlaced || dev->nosignal)
  344. sync_control |= 0x20;
  345. /* switch on auto standard detection */
  346. sync_control |= SAA7134_SYNC_CTRL_AUFD;
  347. chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
  348. chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
  349. luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
  350. /* setup video decoder */
  351. saa_writeb(SAA7134_INCR_DELAY, 0x08);
  352. saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
  353. saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
  354. saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
  355. saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
  356. saa_writeb(SAA7134_HSYNC_START, 0xeb);
  357. saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
  358. saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
  359. saa_writeb(SAA7134_SYNC_CTRL, sync_control);
  360. saa_writeb(SAA7134_LUMA_CTRL, luma_control);
  361. saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
  362. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  363. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  364. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  365. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  366. saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
  367. saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
  368. saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
  369. saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
  370. saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
  371. saa_writeb(SAA7134_ANALOG_ADC, 0x01);
  372. saa_writeb(SAA7134_VGATE_START, 0x11);
  373. saa_writeb(SAA7134_VGATE_STOP, 0xfe);
  374. saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
  375. saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
  376. saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
  377. }
  378. void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
  379. {
  380. saa7134_set_decoder(dev);
  381. saa_call_all(dev, video, s_std, dev->tvnorm->id);
  382. /* Set the correct norm for the saa6752hs. This function
  383. does nothing if there is no saa6752hs. */
  384. saa_call_empress(dev, video, s_std, dev->tvnorm->id);
  385. }
  386. static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
  387. {
  388. static const struct {
  389. int xpsc;
  390. int xacl;
  391. int xc2_1;
  392. int xdcg;
  393. int vpfy;
  394. } vals[] = {
  395. /* XPSC XACL XC2_1 XDCG VPFY */
  396. { 1, 0, 0, 0, 0 },
  397. { 2, 2, 1, 2, 2 },
  398. { 3, 4, 1, 3, 2 },
  399. { 4, 8, 1, 4, 2 },
  400. { 5, 8, 1, 4, 2 },
  401. { 6, 8, 1, 4, 3 },
  402. { 7, 8, 1, 4, 3 },
  403. { 8, 15, 0, 4, 3 },
  404. { 9, 15, 0, 4, 3 },
  405. { 10, 16, 1, 5, 3 },
  406. };
  407. static const int count = ARRAY_SIZE(vals);
  408. int i;
  409. for (i = 0; i < count; i++)
  410. if (vals[i].xpsc == prescale)
  411. break;
  412. if (i == count)
  413. return;
  414. saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
  415. saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
  416. saa_writeb(SAA7134_LEVEL_CTRL(task),
  417. (vals[i].xc2_1 << 3) | (vals[i].xdcg));
  418. saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
  419. (vals[i].vpfy << 2) | vals[i].vpfy);
  420. }
  421. static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
  422. {
  423. int val,mirror;
  424. saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
  425. saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
  426. mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
  427. if (yscale < 2048) {
  428. /* LPI */
  429. video_dbg("yscale LPI yscale=%d\n", yscale);
  430. saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
  431. saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
  432. saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
  433. } else {
  434. /* ACM */
  435. val = 0x40 * 1024 / yscale;
  436. video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
  437. saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
  438. saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
  439. saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
  440. }
  441. saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
  442. }
  443. static void set_size(struct saa7134_dev *dev, int task,
  444. int width, int height, int interlace)
  445. {
  446. int prescale,xscale,yscale,y_even,y_odd;
  447. int h_start, h_stop, v_start, v_stop;
  448. int div = interlace ? 2 : 1;
  449. /* setup video scaler */
  450. h_start = dev->crop_current.left;
  451. v_start = dev->crop_current.top/2;
  452. h_stop = (dev->crop_current.left + dev->crop_current.width -1);
  453. v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
  454. saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
  455. saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
  456. saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
  457. saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
  458. saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
  459. saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
  460. saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
  461. saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
  462. prescale = dev->crop_current.width / width;
  463. if (0 == prescale)
  464. prescale = 1;
  465. xscale = 1024 * dev->crop_current.width / prescale / width;
  466. yscale = 512 * div * dev->crop_current.height / height;
  467. video_dbg("prescale=%d xscale=%d yscale=%d\n",
  468. prescale, xscale, yscale);
  469. set_h_prescale(dev,task,prescale);
  470. saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
  471. saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
  472. set_v_scale(dev,task,yscale);
  473. saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
  474. saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
  475. saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
  476. saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
  477. /* deinterlace y offsets */
  478. y_odd = dev->ctl_y_odd;
  479. y_even = dev->ctl_y_even;
  480. saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
  481. saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
  482. saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
  483. saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
  484. }
  485. /* ------------------------------------------------------------------ */
  486. struct cliplist {
  487. __u16 position;
  488. __u8 enable;
  489. __u8 disable;
  490. };
  491. static void set_cliplist(struct saa7134_dev *dev, int reg,
  492. struct cliplist *cl, int entries, char *name)
  493. {
  494. __u8 winbits = 0;
  495. int i;
  496. for (i = 0; i < entries; i++) {
  497. winbits |= cl[i].enable;
  498. winbits &= ~cl[i].disable;
  499. if (i < 15 && cl[i].position == cl[i+1].position)
  500. continue;
  501. saa_writeb(reg + 0, winbits);
  502. saa_writeb(reg + 2, cl[i].position & 0xff);
  503. saa_writeb(reg + 3, cl[i].position >> 8);
  504. video_dbg("clip: %s winbits=%02x pos=%d\n",
  505. name,winbits,cl[i].position);
  506. reg += 8;
  507. }
  508. for (; reg < 0x400; reg += 8) {
  509. saa_writeb(reg+ 0, 0);
  510. saa_writeb(reg + 1, 0);
  511. saa_writeb(reg + 2, 0);
  512. saa_writeb(reg + 3, 0);
  513. }
  514. }
  515. static int clip_range(int val)
  516. {
  517. if (val < 0)
  518. val = 0;
  519. return val;
  520. }
  521. /* Sort into smallest position first order */
  522. static int cliplist_cmp(const void *a, const void *b)
  523. {
  524. const struct cliplist *cla = a;
  525. const struct cliplist *clb = b;
  526. if (cla->position < clb->position)
  527. return -1;
  528. if (cla->position > clb->position)
  529. return 1;
  530. return 0;
  531. }
  532. static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
  533. int nclips, int interlace)
  534. {
  535. struct cliplist col[16], row[16];
  536. int cols = 0, rows = 0, i;
  537. int div = interlace ? 2 : 1;
  538. memset(col, 0, sizeof(col));
  539. memset(row, 0, sizeof(row));
  540. for (i = 0; i < nclips && i < 8; i++) {
  541. col[cols].position = clip_range(clips[i].c.left);
  542. col[cols].enable = (1 << i);
  543. cols++;
  544. col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
  545. col[cols].disable = (1 << i);
  546. cols++;
  547. row[rows].position = clip_range(clips[i].c.top / div);
  548. row[rows].enable = (1 << i);
  549. rows++;
  550. row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
  551. / div);
  552. row[rows].disable = (1 << i);
  553. rows++;
  554. }
  555. sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
  556. sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
  557. set_cliplist(dev,0x380,col,cols,"cols");
  558. set_cliplist(dev,0x384,row,rows,"rows");
  559. return 0;
  560. }
  561. static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
  562. {
  563. enum v4l2_field field;
  564. int maxw, maxh;
  565. if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
  566. return -EINVAL;
  567. if (win->w.width < 48)
  568. win->w.width = 48;
  569. if (win->w.height < 32)
  570. win->w.height = 32;
  571. if (win->clipcount > 8)
  572. win->clipcount = 8;
  573. win->chromakey = 0;
  574. win->global_alpha = 0;
  575. field = win->field;
  576. maxw = dev->crop_current.width;
  577. maxh = dev->crop_current.height;
  578. if (V4L2_FIELD_ANY == field) {
  579. field = (win->w.height > maxh/2)
  580. ? V4L2_FIELD_INTERLACED
  581. : V4L2_FIELD_TOP;
  582. }
  583. switch (field) {
  584. case V4L2_FIELD_TOP:
  585. case V4L2_FIELD_BOTTOM:
  586. maxh = maxh / 2;
  587. break;
  588. default:
  589. field = V4L2_FIELD_INTERLACED;
  590. break;
  591. }
  592. win->field = field;
  593. if (win->w.width > maxw)
  594. win->w.width = maxw;
  595. if (win->w.height > maxh)
  596. win->w.height = maxh;
  597. return 0;
  598. }
  599. static int start_preview(struct saa7134_dev *dev)
  600. {
  601. unsigned long base,control,bpl;
  602. int err;
  603. err = verify_preview(dev, &dev->win, false);
  604. if (0 != err)
  605. return err;
  606. dev->ovfield = dev->win.field;
  607. video_dbg("%s %dx%d+%d+%d 0x%08x field=%s\n", __func__,
  608. dev->win.w.width, dev->win.w.height,
  609. dev->win.w.left, dev->win.w.top,
  610. dev->ovfmt->fourcc, v4l2_field_names[dev->ovfield]);
  611. /* setup window + clipping */
  612. set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
  613. V4L2_FIELD_HAS_BOTH(dev->ovfield));
  614. setup_clipping(dev, dev->clips, dev->nclips,
  615. V4L2_FIELD_HAS_BOTH(dev->ovfield));
  616. if (dev->ovfmt->yuv)
  617. saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
  618. else
  619. saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
  620. saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
  621. /* dma: setup channel 1 (= Video Task B) */
  622. base = (unsigned long)dev->ovbuf.base;
  623. base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
  624. base += dev->ovfmt->depth/8 * dev->win.w.left;
  625. bpl = dev->ovbuf.fmt.bytesperline;
  626. control = SAA7134_RS_CONTROL_BURST_16;
  627. if (dev->ovfmt->bswap)
  628. control |= SAA7134_RS_CONTROL_BSWAP;
  629. if (dev->ovfmt->wswap)
  630. control |= SAA7134_RS_CONTROL_WSWAP;
  631. if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
  632. saa_writel(SAA7134_RS_BA1(1),base);
  633. saa_writel(SAA7134_RS_BA2(1),base+bpl);
  634. saa_writel(SAA7134_RS_PITCH(1),bpl*2);
  635. saa_writel(SAA7134_RS_CONTROL(1),control);
  636. } else {
  637. saa_writel(SAA7134_RS_BA1(1),base);
  638. saa_writel(SAA7134_RS_BA2(1),base);
  639. saa_writel(SAA7134_RS_PITCH(1),bpl);
  640. saa_writel(SAA7134_RS_CONTROL(1),control);
  641. }
  642. /* start dma */
  643. dev->ovenable = 1;
  644. saa7134_set_dmabits(dev);
  645. return 0;
  646. }
  647. static int stop_preview(struct saa7134_dev *dev)
  648. {
  649. dev->ovenable = 0;
  650. saa7134_set_dmabits(dev);
  651. return 0;
  652. }
  653. /*
  654. * Media Controller helper functions
  655. */
  656. static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
  657. {
  658. #ifdef CONFIG_MEDIA_CONTROLLER
  659. struct media_device *mdev = dev->media_dev;
  660. struct media_entity *source;
  661. struct media_link *link, *found_link = NULL;
  662. int ret, active_links = 0;
  663. if (!mdev || !dev->decoder)
  664. return 0;
  665. /*
  666. * This will find the tuner that is connected into the decoder.
  667. * Technically, this is not 100% correct, as the device may be
  668. * using an analog input instead of the tuner. However, as we can't
  669. * do DVB streaming while the DMA engine is being used for V4L2,
  670. * this should be enough for the actual needs.
  671. */
  672. list_for_each_entry(link, &dev->decoder->links, list) {
  673. if (link->sink->entity == dev->decoder) {
  674. found_link = link;
  675. if (link->flags & MEDIA_LNK_FL_ENABLED)
  676. active_links++;
  677. break;
  678. }
  679. }
  680. if (active_links == 1 || !found_link)
  681. return 0;
  682. source = found_link->source->entity;
  683. list_for_each_entry(link, &source->links, list) {
  684. struct media_entity *sink;
  685. int flags = 0;
  686. sink = link->sink->entity;
  687. if (sink == dev->decoder)
  688. flags = MEDIA_LNK_FL_ENABLED;
  689. ret = media_entity_setup_link(link, flags);
  690. if (ret) {
  691. pr_err("Couldn't change link %s->%s to %s. Error %d\n",
  692. source->name, sink->name,
  693. flags ? "enabled" : "disabled",
  694. ret);
  695. return ret;
  696. }
  697. }
  698. #endif
  699. return 0;
  700. }
  701. /* ------------------------------------------------------------------ */
  702. static int buffer_activate(struct saa7134_dev *dev,
  703. struct saa7134_buf *buf,
  704. struct saa7134_buf *next)
  705. {
  706. struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
  707. unsigned long base,control,bpl;
  708. unsigned long bpl_uv, lines_uv, base2, base3; /* planar */
  709. video_dbg("buffer_activate buf=%p\n", buf);
  710. buf->top_seen = 0;
  711. set_size(dev, TASK_A, dev->width, dev->height,
  712. V4L2_FIELD_HAS_BOTH(dev->field));
  713. if (dev->fmt->yuv)
  714. saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
  715. else
  716. saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
  717. saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
  718. /* DMA: setup channel 0 (= Video Task A0) */
  719. base = saa7134_buffer_base(buf);
  720. if (dev->fmt->planar)
  721. bpl = dev->width;
  722. else
  723. bpl = (dev->width * dev->fmt->depth) / 8;
  724. control = SAA7134_RS_CONTROL_BURST_16 |
  725. SAA7134_RS_CONTROL_ME |
  726. (dmaq->pt.dma >> 12);
  727. if (dev->fmt->bswap)
  728. control |= SAA7134_RS_CONTROL_BSWAP;
  729. if (dev->fmt->wswap)
  730. control |= SAA7134_RS_CONTROL_WSWAP;
  731. if (V4L2_FIELD_HAS_BOTH(dev->field)) {
  732. /* interlaced */
  733. saa_writel(SAA7134_RS_BA1(0),base);
  734. saa_writel(SAA7134_RS_BA2(0),base+bpl);
  735. saa_writel(SAA7134_RS_PITCH(0),bpl*2);
  736. } else {
  737. /* non-interlaced */
  738. saa_writel(SAA7134_RS_BA1(0),base);
  739. saa_writel(SAA7134_RS_BA2(0),base);
  740. saa_writel(SAA7134_RS_PITCH(0),bpl);
  741. }
  742. saa_writel(SAA7134_RS_CONTROL(0),control);
  743. if (dev->fmt->planar) {
  744. /* DMA: setup channel 4+5 (= planar task A) */
  745. bpl_uv = bpl >> dev->fmt->hshift;
  746. lines_uv = dev->height >> dev->fmt->vshift;
  747. base2 = base + bpl * dev->height;
  748. base3 = base2 + bpl_uv * lines_uv;
  749. if (dev->fmt->uvswap)
  750. swap(base2, base3);
  751. video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
  752. bpl_uv,lines_uv,base2,base3);
  753. if (V4L2_FIELD_HAS_BOTH(dev->field)) {
  754. /* interlaced */
  755. saa_writel(SAA7134_RS_BA1(4),base2);
  756. saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
  757. saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
  758. saa_writel(SAA7134_RS_BA1(5),base3);
  759. saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
  760. saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
  761. } else {
  762. /* non-interlaced */
  763. saa_writel(SAA7134_RS_BA1(4),base2);
  764. saa_writel(SAA7134_RS_BA2(4),base2);
  765. saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
  766. saa_writel(SAA7134_RS_BA1(5),base3);
  767. saa_writel(SAA7134_RS_BA2(5),base3);
  768. saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
  769. }
  770. saa_writel(SAA7134_RS_CONTROL(4),control);
  771. saa_writel(SAA7134_RS_CONTROL(5),control);
  772. }
  773. /* start DMA */
  774. saa7134_set_dmabits(dev);
  775. mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
  776. return 0;
  777. }
  778. static int buffer_init(struct vb2_buffer *vb2)
  779. {
  780. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  781. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  782. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  783. dmaq->curr = NULL;
  784. buf->activate = buffer_activate;
  785. return 0;
  786. }
  787. static int buffer_prepare(struct vb2_buffer *vb2)
  788. {
  789. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  790. struct saa7134_dev *dev = dmaq->dev;
  791. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  792. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  793. struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
  794. unsigned int size;
  795. if (dma->sgl->offset) {
  796. pr_err("The buffer is not page-aligned\n");
  797. return -EINVAL;
  798. }
  799. size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  800. if (vb2_plane_size(vb2, 0) < size)
  801. return -EINVAL;
  802. vb2_set_plane_payload(vb2, 0, size);
  803. vbuf->field = dev->field;
  804. return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
  805. saa7134_buffer_startpage(buf));
  806. }
  807. static int queue_setup(struct vb2_queue *q,
  808. unsigned int *nbuffers, unsigned int *nplanes,
  809. unsigned int sizes[], struct device *alloc_devs[])
  810. {
  811. struct saa7134_dmaqueue *dmaq = q->drv_priv;
  812. struct saa7134_dev *dev = dmaq->dev;
  813. int size = dev->fmt->depth * dev->width * dev->height >> 3;
  814. if (dev->width < 48 ||
  815. dev->height < 32 ||
  816. dev->width/4 > dev->crop_current.width ||
  817. dev->height/4 > dev->crop_current.height ||
  818. dev->width > dev->crop_bounds.width ||
  819. dev->height > dev->crop_bounds.height)
  820. return -EINVAL;
  821. *nbuffers = saa7134_buffer_count(size, *nbuffers);
  822. *nplanes = 1;
  823. sizes[0] = size;
  824. saa7134_enable_analog_tuner(dev);
  825. return 0;
  826. }
  827. /*
  828. * move buffer to hardware queue
  829. */
  830. void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
  831. {
  832. struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
  833. struct saa7134_dev *dev = dmaq->dev;
  834. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  835. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  836. saa7134_buffer_queue(dev, dmaq, buf);
  837. }
  838. EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
  839. int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  840. {
  841. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  842. struct saa7134_dev *dev = dmaq->dev;
  843. /*
  844. * Planar video capture and TS share the same DMA channel,
  845. * so only one can be active at a time.
  846. */
  847. if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
  848. dmaq == &dev->video_q && dev->fmt->planar) {
  849. struct saa7134_buf *buf, *tmp;
  850. list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
  851. list_del(&buf->entry);
  852. vb2_buffer_done(&buf->vb2.vb2_buf,
  853. VB2_BUF_STATE_QUEUED);
  854. }
  855. if (dmaq->curr) {
  856. vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
  857. VB2_BUF_STATE_QUEUED);
  858. dmaq->curr = NULL;
  859. }
  860. return -EBUSY;
  861. }
  862. /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
  863. * configured conservatively, there's 22 usec of buffering for video.
  864. * We therefore request a DMA latency of 20 usec, giving us 2 usec of
  865. * margin in case the FIFO is configured differently to the datasheet.
  866. * Unfortunately, I lack register-level documentation to check the
  867. * Linux FIFO setup and confirm the perfect value.
  868. */
  869. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  870. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  871. cpu_latency_qos_add_request(&dev->qos_request, 20);
  872. dmaq->seq_nr = 0;
  873. return 0;
  874. }
  875. void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
  876. {
  877. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  878. struct saa7134_dev *dev = dmaq->dev;
  879. saa7134_stop_streaming(dev, dmaq);
  880. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  881. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  882. cpu_latency_qos_remove_request(&dev->qos_request);
  883. }
  884. static const struct vb2_ops vb2_qops = {
  885. .queue_setup = queue_setup,
  886. .buf_init = buffer_init,
  887. .buf_prepare = buffer_prepare,
  888. .buf_queue = saa7134_vb2_buffer_queue,
  889. .wait_prepare = vb2_ops_wait_prepare,
  890. .wait_finish = vb2_ops_wait_finish,
  891. .start_streaming = saa7134_vb2_start_streaming,
  892. .stop_streaming = saa7134_vb2_stop_streaming,
  893. };
  894. /* ------------------------------------------------------------------ */
  895. static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
  896. {
  897. struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
  898. unsigned long flags;
  899. int restart_overlay = 0;
  900. switch (ctrl->id) {
  901. case V4L2_CID_BRIGHTNESS:
  902. dev->ctl_bright = ctrl->val;
  903. saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
  904. break;
  905. case V4L2_CID_HUE:
  906. dev->ctl_hue = ctrl->val;
  907. saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
  908. break;
  909. case V4L2_CID_CONTRAST:
  910. dev->ctl_contrast = ctrl->val;
  911. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  912. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  913. break;
  914. case V4L2_CID_SATURATION:
  915. dev->ctl_saturation = ctrl->val;
  916. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  917. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  918. break;
  919. case V4L2_CID_AUDIO_MUTE:
  920. dev->ctl_mute = ctrl->val;
  921. saa7134_tvaudio_setmute(dev);
  922. break;
  923. case V4L2_CID_AUDIO_VOLUME:
  924. dev->ctl_volume = ctrl->val;
  925. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  926. break;
  927. case V4L2_CID_PRIVATE_INVERT:
  928. dev->ctl_invert = ctrl->val;
  929. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  930. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  931. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  932. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  933. break;
  934. case V4L2_CID_HFLIP:
  935. dev->ctl_mirror = ctrl->val;
  936. restart_overlay = 1;
  937. break;
  938. case V4L2_CID_PRIVATE_Y_EVEN:
  939. dev->ctl_y_even = ctrl->val;
  940. restart_overlay = 1;
  941. break;
  942. case V4L2_CID_PRIVATE_Y_ODD:
  943. dev->ctl_y_odd = ctrl->val;
  944. restart_overlay = 1;
  945. break;
  946. case V4L2_CID_PRIVATE_AUTOMUTE:
  947. {
  948. struct v4l2_priv_tun_config tda9887_cfg;
  949. tda9887_cfg.tuner = TUNER_TDA9887;
  950. tda9887_cfg.priv = &dev->tda9887_conf;
  951. dev->ctl_automute = ctrl->val;
  952. if (dev->tda9887_conf) {
  953. if (dev->ctl_automute)
  954. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  955. else
  956. dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
  957. saa_call_all(dev, tuner, s_config, &tda9887_cfg);
  958. }
  959. break;
  960. }
  961. default:
  962. return -EINVAL;
  963. }
  964. if (restart_overlay && dev->overlay_owner) {
  965. spin_lock_irqsave(&dev->slock, flags);
  966. stop_preview(dev);
  967. start_preview(dev);
  968. spin_unlock_irqrestore(&dev->slock, flags);
  969. }
  970. return 0;
  971. }
  972. /* ------------------------------------------------------------------ */
  973. static int video_open(struct file *file)
  974. {
  975. struct video_device *vdev = video_devdata(file);
  976. struct saa7134_dev *dev = video_drvdata(file);
  977. int ret = v4l2_fh_open(file);
  978. if (ret < 0)
  979. return ret;
  980. mutex_lock(&dev->lock);
  981. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  982. /* switch to radio mode */
  983. saa7134_tvaudio_setinput(dev, &card(dev).radio);
  984. saa_call_all(dev, tuner, s_radio);
  985. } else {
  986. /* switch to video/vbi mode */
  987. video_mux(dev, dev->ctl_input);
  988. }
  989. mutex_unlock(&dev->lock);
  990. return 0;
  991. }
  992. static int video_release(struct file *file)
  993. {
  994. struct video_device *vdev = video_devdata(file);
  995. struct saa7134_dev *dev = video_drvdata(file);
  996. struct v4l2_fh *fh = file->private_data;
  997. struct saa6588_command cmd;
  998. unsigned long flags;
  999. mutex_lock(&dev->lock);
  1000. saa7134_tvaudio_close(dev);
  1001. /* turn off overlay */
  1002. if (fh == dev->overlay_owner) {
  1003. spin_lock_irqsave(&dev->slock,flags);
  1004. stop_preview(dev);
  1005. spin_unlock_irqrestore(&dev->slock,flags);
  1006. dev->overlay_owner = NULL;
  1007. }
  1008. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1009. v4l2_fh_release(file);
  1010. else
  1011. _vb2_fop_release(file, NULL);
  1012. /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
  1013. saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
  1014. saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
  1015. saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
  1016. saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
  1017. saa_call_all(dev, tuner, standby);
  1018. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1019. saa_call_all(dev, core, command, SAA6588_CMD_CLOSE, &cmd);
  1020. mutex_unlock(&dev->lock);
  1021. return 0;
  1022. }
  1023. static ssize_t radio_read(struct file *file, char __user *data,
  1024. size_t count, loff_t *ppos)
  1025. {
  1026. struct saa7134_dev *dev = video_drvdata(file);
  1027. struct saa6588_command cmd;
  1028. cmd.block_count = count/3;
  1029. cmd.nonblocking = file->f_flags & O_NONBLOCK;
  1030. cmd.buffer = data;
  1031. cmd.instance = file;
  1032. cmd.result = -ENODEV;
  1033. mutex_lock(&dev->lock);
  1034. saa_call_all(dev, core, command, SAA6588_CMD_READ, &cmd);
  1035. mutex_unlock(&dev->lock);
  1036. return cmd.result;
  1037. }
  1038. static __poll_t radio_poll(struct file *file, poll_table *wait)
  1039. {
  1040. struct saa7134_dev *dev = video_drvdata(file);
  1041. struct saa6588_command cmd;
  1042. __poll_t rc = v4l2_ctrl_poll(file, wait);
  1043. cmd.instance = file;
  1044. cmd.event_list = wait;
  1045. cmd.poll_mask = 0;
  1046. mutex_lock(&dev->lock);
  1047. saa_call_all(dev, core, command, SAA6588_CMD_POLL, &cmd);
  1048. mutex_unlock(&dev->lock);
  1049. return rc | cmd.poll_mask;
  1050. }
  1051. /* ------------------------------------------------------------------ */
  1052. static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
  1053. struct v4l2_format *f)
  1054. {
  1055. struct saa7134_dev *dev = video_drvdata(file);
  1056. struct saa7134_tvnorm *norm = dev->tvnorm;
  1057. memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1058. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1059. f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
  1060. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1061. f->fmt.vbi.offset = 64 * 4;
  1062. f->fmt.vbi.start[0] = norm->vbi_v_start_0;
  1063. f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
  1064. f->fmt.vbi.start[1] = norm->vbi_v_start_1;
  1065. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1066. f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
  1067. return 0;
  1068. }
  1069. static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
  1070. struct v4l2_format *f)
  1071. {
  1072. struct saa7134_dev *dev = video_drvdata(file);
  1073. f->fmt.pix.width = dev->width;
  1074. f->fmt.pix.height = dev->height;
  1075. f->fmt.pix.field = dev->field;
  1076. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  1077. if (dev->fmt->planar)
  1078. f->fmt.pix.bytesperline = f->fmt.pix.width;
  1079. else
  1080. f->fmt.pix.bytesperline =
  1081. (f->fmt.pix.width * dev->fmt->depth) / 8;
  1082. f->fmt.pix.sizeimage =
  1083. (f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
  1084. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1085. return 0;
  1086. }
  1087. static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
  1088. struct v4l2_format *f)
  1089. {
  1090. struct saa7134_dev *dev = video_drvdata(file);
  1091. u32 clipcount = f->fmt.win.clipcount;
  1092. int i;
  1093. if (saa7134_no_overlay > 0) {
  1094. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1095. return -EINVAL;
  1096. }
  1097. f->fmt.win = dev->win;
  1098. if (!f->fmt.win.clips) {
  1099. f->fmt.win.clipcount = 0;
  1100. return 0;
  1101. }
  1102. if (dev->nclips < clipcount)
  1103. clipcount = dev->nclips;
  1104. f->fmt.win.clipcount = clipcount;
  1105. for (i = 0; i < clipcount; i++) {
  1106. memcpy(&f->fmt.win.clips[i].c, &dev->clips[i].c,
  1107. sizeof(struct v4l2_rect));
  1108. }
  1109. return 0;
  1110. }
  1111. static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
  1112. struct v4l2_format *f)
  1113. {
  1114. struct saa7134_dev *dev = video_drvdata(file);
  1115. struct saa7134_format *fmt;
  1116. enum v4l2_field field;
  1117. unsigned int maxw, maxh;
  1118. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1119. if (NULL == fmt)
  1120. return -EINVAL;
  1121. field = f->fmt.pix.field;
  1122. maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
  1123. maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
  1124. if (V4L2_FIELD_ANY == field) {
  1125. field = (f->fmt.pix.height > maxh/2)
  1126. ? V4L2_FIELD_INTERLACED
  1127. : V4L2_FIELD_BOTTOM;
  1128. }
  1129. switch (field) {
  1130. case V4L2_FIELD_TOP:
  1131. case V4L2_FIELD_BOTTOM:
  1132. maxh = maxh / 2;
  1133. break;
  1134. default:
  1135. field = V4L2_FIELD_INTERLACED;
  1136. break;
  1137. }
  1138. f->fmt.pix.field = field;
  1139. if (f->fmt.pix.width < 48)
  1140. f->fmt.pix.width = 48;
  1141. if (f->fmt.pix.height < 32)
  1142. f->fmt.pix.height = 32;
  1143. if (f->fmt.pix.width > maxw)
  1144. f->fmt.pix.width = maxw;
  1145. if (f->fmt.pix.height > maxh)
  1146. f->fmt.pix.height = maxh;
  1147. f->fmt.pix.width &= ~0x03;
  1148. if (fmt->planar)
  1149. f->fmt.pix.bytesperline = f->fmt.pix.width;
  1150. else
  1151. f->fmt.pix.bytesperline =
  1152. (f->fmt.pix.width * fmt->depth) / 8;
  1153. f->fmt.pix.sizeimage =
  1154. (f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
  1155. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1156. return 0;
  1157. }
  1158. static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
  1159. struct v4l2_format *f)
  1160. {
  1161. struct saa7134_dev *dev = video_drvdata(file);
  1162. if (saa7134_no_overlay > 0) {
  1163. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1164. return -EINVAL;
  1165. }
  1166. if (f->fmt.win.clips == NULL)
  1167. f->fmt.win.clipcount = 0;
  1168. return verify_preview(dev, &f->fmt.win, true);
  1169. }
  1170. static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
  1171. struct v4l2_format *f)
  1172. {
  1173. struct saa7134_dev *dev = video_drvdata(file);
  1174. int err;
  1175. err = saa7134_try_fmt_vid_cap(file, priv, f);
  1176. if (0 != err)
  1177. return err;
  1178. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1179. dev->width = f->fmt.pix.width;
  1180. dev->height = f->fmt.pix.height;
  1181. dev->field = f->fmt.pix.field;
  1182. return 0;
  1183. }
  1184. static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
  1185. struct v4l2_format *f)
  1186. {
  1187. struct saa7134_dev *dev = video_drvdata(file);
  1188. int err;
  1189. unsigned long flags;
  1190. if (saa7134_no_overlay > 0) {
  1191. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1192. return -EINVAL;
  1193. }
  1194. if (f->fmt.win.clips == NULL)
  1195. f->fmt.win.clipcount = 0;
  1196. err = verify_preview(dev, &f->fmt.win, true);
  1197. if (0 != err)
  1198. return err;
  1199. dev->win = f->fmt.win;
  1200. dev->nclips = f->fmt.win.clipcount;
  1201. memcpy(dev->clips, f->fmt.win.clips,
  1202. sizeof(struct v4l2_clip) * dev->nclips);
  1203. if (priv == dev->overlay_owner) {
  1204. spin_lock_irqsave(&dev->slock, flags);
  1205. stop_preview(dev);
  1206. start_preview(dev);
  1207. spin_unlock_irqrestore(&dev->slock, flags);
  1208. }
  1209. return 0;
  1210. }
  1211. int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1212. {
  1213. struct saa7134_dev *dev = video_drvdata(file);
  1214. unsigned int n;
  1215. n = i->index;
  1216. if (n >= SAA7134_INPUT_MAX)
  1217. return -EINVAL;
  1218. if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
  1219. return -EINVAL;
  1220. i->index = n;
  1221. strscpy(i->name, saa7134_input_name[card_in(dev, n).type],
  1222. sizeof(i->name));
  1223. switch (card_in(dev, n).type) {
  1224. case SAA7134_INPUT_TV:
  1225. case SAA7134_INPUT_TV_MONO:
  1226. i->type = V4L2_INPUT_TYPE_TUNER;
  1227. break;
  1228. default:
  1229. i->type = V4L2_INPUT_TYPE_CAMERA;
  1230. break;
  1231. }
  1232. if (n == dev->ctl_input) {
  1233. int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1234. int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1235. if (0 != (v1 & 0x40))
  1236. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1237. if (0 != (v2 & 0x40))
  1238. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1239. if (0 != (v2 & 0x0e))
  1240. i->status |= V4L2_IN_ST_MACROVISION;
  1241. }
  1242. i->std = SAA7134_NORMS;
  1243. return 0;
  1244. }
  1245. EXPORT_SYMBOL_GPL(saa7134_enum_input);
  1246. int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
  1247. {
  1248. struct saa7134_dev *dev = video_drvdata(file);
  1249. *i = dev->ctl_input;
  1250. return 0;
  1251. }
  1252. EXPORT_SYMBOL_GPL(saa7134_g_input);
  1253. int saa7134_s_input(struct file *file, void *priv, unsigned int i)
  1254. {
  1255. struct saa7134_dev *dev = video_drvdata(file);
  1256. if (i >= SAA7134_INPUT_MAX)
  1257. return -EINVAL;
  1258. if (card_in(dev, i).type == SAA7134_NO_INPUT)
  1259. return -EINVAL;
  1260. video_mux(dev, i);
  1261. return 0;
  1262. }
  1263. EXPORT_SYMBOL_GPL(saa7134_s_input);
  1264. int saa7134_querycap(struct file *file, void *priv,
  1265. struct v4l2_capability *cap)
  1266. {
  1267. struct saa7134_dev *dev = video_drvdata(file);
  1268. strscpy(cap->driver, "saa7134", sizeof(cap->driver));
  1269. strscpy(cap->card, saa7134_boards[dev->board].name,
  1270. sizeof(cap->card));
  1271. cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
  1272. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE |
  1273. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  1274. if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
  1275. cap->capabilities |= V4L2_CAP_TUNER;
  1276. if (dev->has_rds)
  1277. cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
  1278. if (saa7134_no_overlay <= 0)
  1279. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  1280. return 0;
  1281. }
  1282. EXPORT_SYMBOL_GPL(saa7134_querycap);
  1283. int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
  1284. {
  1285. struct saa7134_dev *dev = video_drvdata(file);
  1286. struct v4l2_fh *fh = priv;
  1287. unsigned long flags;
  1288. unsigned int i;
  1289. v4l2_std_id fixup;
  1290. if (is_empress(file) && dev->overlay_owner) {
  1291. /* Don't change the std from the mpeg device
  1292. if overlay is active. */
  1293. return -EBUSY;
  1294. }
  1295. for (i = 0; i < TVNORMS; i++)
  1296. if (id == tvnorms[i].id)
  1297. break;
  1298. if (i == TVNORMS)
  1299. for (i = 0; i < TVNORMS; i++)
  1300. if (id & tvnorms[i].id)
  1301. break;
  1302. if (i == TVNORMS)
  1303. return -EINVAL;
  1304. if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
  1305. if (secam[0] == 'L' || secam[0] == 'l') {
  1306. if (secam[1] == 'C' || secam[1] == 'c')
  1307. fixup = V4L2_STD_SECAM_LC;
  1308. else
  1309. fixup = V4L2_STD_SECAM_L;
  1310. } else {
  1311. if (secam[0] == 'D' || secam[0] == 'd')
  1312. fixup = V4L2_STD_SECAM_DK;
  1313. else
  1314. fixup = V4L2_STD_SECAM;
  1315. }
  1316. for (i = 0; i < TVNORMS; i++) {
  1317. if (fixup == tvnorms[i].id)
  1318. break;
  1319. }
  1320. if (i == TVNORMS)
  1321. return -EINVAL;
  1322. }
  1323. if (!is_empress(file) && fh == dev->overlay_owner) {
  1324. spin_lock_irqsave(&dev->slock, flags);
  1325. stop_preview(dev);
  1326. spin_unlock_irqrestore(&dev->slock, flags);
  1327. set_tvnorm(dev, &tvnorms[i]);
  1328. spin_lock_irqsave(&dev->slock, flags);
  1329. start_preview(dev);
  1330. spin_unlock_irqrestore(&dev->slock, flags);
  1331. } else
  1332. set_tvnorm(dev, &tvnorms[i]);
  1333. saa7134_tvaudio_do_scan(dev);
  1334. return 0;
  1335. }
  1336. EXPORT_SYMBOL_GPL(saa7134_s_std);
  1337. int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1338. {
  1339. struct saa7134_dev *dev = video_drvdata(file);
  1340. *id = dev->tvnorm->id;
  1341. return 0;
  1342. }
  1343. EXPORT_SYMBOL_GPL(saa7134_g_std);
  1344. static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
  1345. {
  1346. static v4l2_std_id stds[] = {
  1347. V4L2_STD_UNKNOWN,
  1348. V4L2_STD_NTSC,
  1349. V4L2_STD_PAL,
  1350. V4L2_STD_SECAM };
  1351. v4l2_std_id result = 0;
  1352. u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1353. u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1354. if (!(st2 & 0x1)) /* RDCAP == 0 */
  1355. result = V4L2_STD_UNKNOWN;
  1356. else
  1357. result = stds[st1 & 0x03];
  1358. return result;
  1359. }
  1360. int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
  1361. {
  1362. struct saa7134_dev *dev = video_drvdata(file);
  1363. *std &= saa7134_read_std(dev);
  1364. return 0;
  1365. }
  1366. EXPORT_SYMBOL_GPL(saa7134_querystd);
  1367. static int saa7134_g_pixelaspect(struct file *file, void *priv,
  1368. int type, struct v4l2_fract *f)
  1369. {
  1370. struct saa7134_dev *dev = video_drvdata(file);
  1371. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1372. type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1373. return -EINVAL;
  1374. if (dev->tvnorm->id & V4L2_STD_525_60) {
  1375. f->numerator = 11;
  1376. f->denominator = 10;
  1377. }
  1378. if (dev->tvnorm->id & V4L2_STD_625_50) {
  1379. f->numerator = 54;
  1380. f->denominator = 59;
  1381. }
  1382. return 0;
  1383. }
  1384. static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1385. {
  1386. struct saa7134_dev *dev = video_drvdata(file);
  1387. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1388. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1389. return -EINVAL;
  1390. switch (sel->target) {
  1391. case V4L2_SEL_TGT_CROP:
  1392. sel->r = dev->crop_current;
  1393. break;
  1394. case V4L2_SEL_TGT_CROP_DEFAULT:
  1395. sel->r = dev->crop_defrect;
  1396. break;
  1397. case V4L2_SEL_TGT_CROP_BOUNDS:
  1398. sel->r = dev->crop_bounds;
  1399. break;
  1400. default:
  1401. return -EINVAL;
  1402. }
  1403. return 0;
  1404. }
  1405. static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1406. {
  1407. struct saa7134_dev *dev = video_drvdata(file);
  1408. struct v4l2_rect *b = &dev->crop_bounds;
  1409. struct v4l2_rect *c = &dev->crop_current;
  1410. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1411. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1412. return -EINVAL;
  1413. if (sel->target != V4L2_SEL_TGT_CROP)
  1414. return -EINVAL;
  1415. if (dev->overlay_owner)
  1416. return -EBUSY;
  1417. if (vb2_is_streaming(&dev->video_vbq))
  1418. return -EBUSY;
  1419. *c = sel->r;
  1420. if (c->top < b->top)
  1421. c->top = b->top;
  1422. if (c->top > b->top + b->height)
  1423. c->top = b->top + b->height;
  1424. if (c->height > b->top - c->top + b->height)
  1425. c->height = b->top - c->top + b->height;
  1426. if (c->left < b->left)
  1427. c->left = b->left;
  1428. if (c->left > b->left + b->width)
  1429. c->left = b->left + b->width;
  1430. if (c->width > b->left - c->left + b->width)
  1431. c->width = b->left - c->left + b->width;
  1432. sel->r = *c;
  1433. return 0;
  1434. }
  1435. int saa7134_g_tuner(struct file *file, void *priv,
  1436. struct v4l2_tuner *t)
  1437. {
  1438. struct saa7134_dev *dev = video_drvdata(file);
  1439. int n;
  1440. if (0 != t->index)
  1441. return -EINVAL;
  1442. memset(t, 0, sizeof(*t));
  1443. for (n = 0; n < SAA7134_INPUT_MAX; n++) {
  1444. if (card_in(dev, n).type == SAA7134_INPUT_TV ||
  1445. card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
  1446. break;
  1447. }
  1448. if (n == SAA7134_INPUT_MAX)
  1449. return -EINVAL;
  1450. if (card_in(dev, n).type != SAA7134_NO_INPUT) {
  1451. strscpy(t->name, "Television", sizeof(t->name));
  1452. t->type = V4L2_TUNER_ANALOG_TV;
  1453. saa_call_all(dev, tuner, g_tuner, t);
  1454. t->capability = V4L2_TUNER_CAP_NORM |
  1455. V4L2_TUNER_CAP_STEREO |
  1456. V4L2_TUNER_CAP_LANG1 |
  1457. V4L2_TUNER_CAP_LANG2;
  1458. t->rxsubchans = saa7134_tvaudio_getstereo(dev);
  1459. t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
  1460. }
  1461. if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
  1462. t->signal = 0xffff;
  1463. return 0;
  1464. }
  1465. EXPORT_SYMBOL_GPL(saa7134_g_tuner);
  1466. int saa7134_s_tuner(struct file *file, void *priv,
  1467. const struct v4l2_tuner *t)
  1468. {
  1469. struct saa7134_dev *dev = video_drvdata(file);
  1470. int rx, mode;
  1471. if (0 != t->index)
  1472. return -EINVAL;
  1473. mode = dev->thread.mode;
  1474. if (UNSET == mode) {
  1475. rx = saa7134_tvaudio_getstereo(dev);
  1476. mode = saa7134_tvaudio_rx2mode(rx);
  1477. }
  1478. if (mode != t->audmode)
  1479. dev->thread.mode = t->audmode;
  1480. return 0;
  1481. }
  1482. EXPORT_SYMBOL_GPL(saa7134_s_tuner);
  1483. int saa7134_g_frequency(struct file *file, void *priv,
  1484. struct v4l2_frequency *f)
  1485. {
  1486. struct saa7134_dev *dev = video_drvdata(file);
  1487. if (0 != f->tuner)
  1488. return -EINVAL;
  1489. saa_call_all(dev, tuner, g_frequency, f);
  1490. return 0;
  1491. }
  1492. EXPORT_SYMBOL_GPL(saa7134_g_frequency);
  1493. int saa7134_s_frequency(struct file *file, void *priv,
  1494. const struct v4l2_frequency *f)
  1495. {
  1496. struct saa7134_dev *dev = video_drvdata(file);
  1497. if (0 != f->tuner)
  1498. return -EINVAL;
  1499. saa_call_all(dev, tuner, s_frequency, f);
  1500. saa7134_tvaudio_do_scan(dev);
  1501. return 0;
  1502. }
  1503. EXPORT_SYMBOL_GPL(saa7134_s_frequency);
  1504. static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
  1505. struct v4l2_fmtdesc *f)
  1506. {
  1507. if (f->index >= FORMATS)
  1508. return -EINVAL;
  1509. f->pixelformat = formats[f->index].fourcc;
  1510. return 0;
  1511. }
  1512. static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
  1513. struct v4l2_fmtdesc *f)
  1514. {
  1515. if (saa7134_no_overlay > 0) {
  1516. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1517. return -EINVAL;
  1518. }
  1519. if ((f->index >= FORMATS) || formats[f->index].planar)
  1520. return -EINVAL;
  1521. f->pixelformat = formats[f->index].fourcc;
  1522. return 0;
  1523. }
  1524. static int saa7134_g_fbuf(struct file *file, void *f,
  1525. struct v4l2_framebuffer *fb)
  1526. {
  1527. struct saa7134_dev *dev = video_drvdata(file);
  1528. *fb = dev->ovbuf;
  1529. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1530. return 0;
  1531. }
  1532. static int saa7134_s_fbuf(struct file *file, void *f,
  1533. const struct v4l2_framebuffer *fb)
  1534. {
  1535. struct saa7134_dev *dev = video_drvdata(file);
  1536. struct saa7134_format *fmt;
  1537. if (!capable(CAP_SYS_ADMIN) &&
  1538. !capable(CAP_SYS_RAWIO))
  1539. return -EPERM;
  1540. /* check args */
  1541. fmt = format_by_fourcc(fb->fmt.pixelformat);
  1542. if (NULL == fmt)
  1543. return -EINVAL;
  1544. /* ok, accept it */
  1545. dev->ovbuf = *fb;
  1546. dev->ovfmt = fmt;
  1547. if (0 == dev->ovbuf.fmt.bytesperline)
  1548. dev->ovbuf.fmt.bytesperline =
  1549. dev->ovbuf.fmt.width*fmt->depth/8;
  1550. return 0;
  1551. }
  1552. static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
  1553. {
  1554. struct saa7134_dev *dev = video_drvdata(file);
  1555. unsigned long flags;
  1556. if (on) {
  1557. if (saa7134_no_overlay > 0) {
  1558. video_dbg("no_overlay\n");
  1559. return -EINVAL;
  1560. }
  1561. if (dev->overlay_owner && priv != dev->overlay_owner)
  1562. return -EBUSY;
  1563. dev->overlay_owner = priv;
  1564. spin_lock_irqsave(&dev->slock, flags);
  1565. start_preview(dev);
  1566. spin_unlock_irqrestore(&dev->slock, flags);
  1567. } else {
  1568. if (priv != dev->overlay_owner)
  1569. return -EINVAL;
  1570. spin_lock_irqsave(&dev->slock, flags);
  1571. stop_preview(dev);
  1572. spin_unlock_irqrestore(&dev->slock, flags);
  1573. dev->overlay_owner = NULL;
  1574. }
  1575. return 0;
  1576. }
  1577. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1578. static int vidioc_g_register (struct file *file, void *priv,
  1579. struct v4l2_dbg_register *reg)
  1580. {
  1581. struct saa7134_dev *dev = video_drvdata(file);
  1582. reg->val = saa_readb(reg->reg & 0xffffff);
  1583. reg->size = 1;
  1584. return 0;
  1585. }
  1586. static int vidioc_s_register (struct file *file, void *priv,
  1587. const struct v4l2_dbg_register *reg)
  1588. {
  1589. struct saa7134_dev *dev = video_drvdata(file);
  1590. saa_writeb(reg->reg & 0xffffff, reg->val);
  1591. return 0;
  1592. }
  1593. #endif
  1594. static int radio_g_tuner(struct file *file, void *priv,
  1595. struct v4l2_tuner *t)
  1596. {
  1597. struct saa7134_dev *dev = video_drvdata(file);
  1598. if (0 != t->index)
  1599. return -EINVAL;
  1600. strscpy(t->name, "Radio", sizeof(t->name));
  1601. saa_call_all(dev, tuner, g_tuner, t);
  1602. t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
  1603. if (dev->input->amux == TV) {
  1604. t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
  1605. t->rxsubchans = (saa_readb(0x529) & 0x08) ?
  1606. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  1607. }
  1608. return 0;
  1609. }
  1610. static int radio_s_tuner(struct file *file, void *priv,
  1611. const struct v4l2_tuner *t)
  1612. {
  1613. struct saa7134_dev *dev = video_drvdata(file);
  1614. if (0 != t->index)
  1615. return -EINVAL;
  1616. saa_call_all(dev, tuner, s_tuner, t);
  1617. return 0;
  1618. }
  1619. static const struct v4l2_file_operations video_fops =
  1620. {
  1621. .owner = THIS_MODULE,
  1622. .open = video_open,
  1623. .release = video_release,
  1624. .read = vb2_fop_read,
  1625. .poll = vb2_fop_poll,
  1626. .mmap = vb2_fop_mmap,
  1627. .unlocked_ioctl = video_ioctl2,
  1628. };
  1629. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1630. .vidioc_querycap = saa7134_querycap,
  1631. .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
  1632. .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
  1633. .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
  1634. .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
  1635. .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
  1636. .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
  1637. .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
  1638. .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
  1639. .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1640. .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1641. .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1642. .vidioc_g_pixelaspect = saa7134_g_pixelaspect,
  1643. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1644. .vidioc_querybuf = vb2_ioctl_querybuf,
  1645. .vidioc_qbuf = vb2_ioctl_qbuf,
  1646. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1647. .vidioc_expbuf = vb2_ioctl_expbuf,
  1648. .vidioc_s_std = saa7134_s_std,
  1649. .vidioc_g_std = saa7134_g_std,
  1650. .vidioc_querystd = saa7134_querystd,
  1651. .vidioc_enum_input = saa7134_enum_input,
  1652. .vidioc_g_input = saa7134_g_input,
  1653. .vidioc_s_input = saa7134_s_input,
  1654. .vidioc_streamon = vb2_ioctl_streamon,
  1655. .vidioc_streamoff = vb2_ioctl_streamoff,
  1656. .vidioc_g_tuner = saa7134_g_tuner,
  1657. .vidioc_s_tuner = saa7134_s_tuner,
  1658. .vidioc_g_selection = saa7134_g_selection,
  1659. .vidioc_s_selection = saa7134_s_selection,
  1660. .vidioc_g_fbuf = saa7134_g_fbuf,
  1661. .vidioc_s_fbuf = saa7134_s_fbuf,
  1662. .vidioc_overlay = saa7134_overlay,
  1663. .vidioc_g_frequency = saa7134_g_frequency,
  1664. .vidioc_s_frequency = saa7134_s_frequency,
  1665. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1666. .vidioc_g_register = vidioc_g_register,
  1667. .vidioc_s_register = vidioc_s_register,
  1668. #endif
  1669. .vidioc_log_status = v4l2_ctrl_log_status,
  1670. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1671. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1672. };
  1673. static const struct v4l2_file_operations radio_fops = {
  1674. .owner = THIS_MODULE,
  1675. .open = video_open,
  1676. .read = radio_read,
  1677. .release = video_release,
  1678. .unlocked_ioctl = video_ioctl2,
  1679. .poll = radio_poll,
  1680. };
  1681. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1682. .vidioc_querycap = saa7134_querycap,
  1683. .vidioc_g_tuner = radio_g_tuner,
  1684. .vidioc_s_tuner = radio_s_tuner,
  1685. .vidioc_g_frequency = saa7134_g_frequency,
  1686. .vidioc_s_frequency = saa7134_s_frequency,
  1687. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1688. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1689. };
  1690. /* ----------------------------------------------------------- */
  1691. /* exported stuff */
  1692. struct video_device saa7134_video_template = {
  1693. .name = "saa7134-video",
  1694. .fops = &video_fops,
  1695. .ioctl_ops = &video_ioctl_ops,
  1696. .tvnorms = SAA7134_NORMS,
  1697. };
  1698. struct video_device saa7134_radio_template = {
  1699. .name = "saa7134-radio",
  1700. .fops = &radio_fops,
  1701. .ioctl_ops = &radio_ioctl_ops,
  1702. };
  1703. static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
  1704. .s_ctrl = saa7134_s_ctrl,
  1705. };
  1706. static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
  1707. .ops = &saa7134_ctrl_ops,
  1708. .id = V4L2_CID_PRIVATE_INVERT,
  1709. .name = "Invert",
  1710. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1711. .min = 0,
  1712. .max = 1,
  1713. .step = 1,
  1714. };
  1715. static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
  1716. .ops = &saa7134_ctrl_ops,
  1717. .id = V4L2_CID_PRIVATE_Y_ODD,
  1718. .name = "Y Offset Odd Field",
  1719. .type = V4L2_CTRL_TYPE_INTEGER,
  1720. .min = 0,
  1721. .max = 128,
  1722. .step = 1,
  1723. };
  1724. static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
  1725. .ops = &saa7134_ctrl_ops,
  1726. .id = V4L2_CID_PRIVATE_Y_EVEN,
  1727. .name = "Y Offset Even Field",
  1728. .type = V4L2_CTRL_TYPE_INTEGER,
  1729. .min = 0,
  1730. .max = 128,
  1731. .step = 1,
  1732. };
  1733. static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
  1734. .ops = &saa7134_ctrl_ops,
  1735. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1736. .name = "Automute",
  1737. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1738. .min = 0,
  1739. .max = 1,
  1740. .step = 1,
  1741. .def = 1,
  1742. };
  1743. int saa7134_video_init1(struct saa7134_dev *dev)
  1744. {
  1745. struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
  1746. struct vb2_queue *q;
  1747. int ret;
  1748. /* sanitycheck insmod options */
  1749. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  1750. gbuffers = 2;
  1751. if (gbufsize > gbufsize_max)
  1752. gbufsize = gbufsize_max;
  1753. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  1754. v4l2_ctrl_handler_init(hdl, 11);
  1755. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1756. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1757. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1758. V4L2_CID_CONTRAST, 0, 127, 1, 68);
  1759. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1760. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1761. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1762. V4L2_CID_HUE, -128, 127, 1, 0);
  1763. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1764. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1765. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1766. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1767. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1768. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1769. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
  1770. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
  1771. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
  1772. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
  1773. if (hdl->error)
  1774. return hdl->error;
  1775. if (card_has_radio(dev)) {
  1776. hdl = &dev->radio_ctrl_handler;
  1777. v4l2_ctrl_handler_init(hdl, 2);
  1778. v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
  1779. v4l2_ctrl_radio_filter, false);
  1780. if (hdl->error)
  1781. return hdl->error;
  1782. }
  1783. dev->ctl_mute = 1;
  1784. if (dev->tda9887_conf && saa7134_ctrl_automute.def)
  1785. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  1786. dev->automute = 0;
  1787. INIT_LIST_HEAD(&dev->video_q.queue);
  1788. timer_setup(&dev->video_q.timeout, saa7134_buffer_timeout, 0);
  1789. dev->video_q.dev = dev;
  1790. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1791. dev->width = 720;
  1792. dev->height = 576;
  1793. dev->field = V4L2_FIELD_INTERLACED;
  1794. dev->win.w.width = dev->width;
  1795. dev->win.w.height = dev->height;
  1796. dev->win.field = V4L2_FIELD_INTERLACED;
  1797. dev->ovbuf.fmt.width = dev->width;
  1798. dev->ovbuf.fmt.height = dev->height;
  1799. dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
  1800. dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1801. if (saa7134_boards[dev->board].video_out)
  1802. saa7134_videoport_init(dev);
  1803. q = &dev->video_vbq;
  1804. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1805. /*
  1806. * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
  1807. * engine cannot handle transfers that do not start at the beginning
  1808. * of a page. A user-provided pointer can start anywhere in a page, so
  1809. * USERPTR support is a no-go unless the application knows about these
  1810. * limitations and has special support for this.
  1811. */
  1812. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1813. if (saa7134_userptr)
  1814. q->io_modes |= VB2_USERPTR;
  1815. q->drv_priv = &dev->video_q;
  1816. q->ops = &vb2_qops;
  1817. q->gfp_flags = GFP_DMA32;
  1818. q->mem_ops = &vb2_dma_sg_memops;
  1819. q->buf_struct_size = sizeof(struct saa7134_buf);
  1820. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1821. q->lock = &dev->lock;
  1822. q->dev = &dev->pci->dev;
  1823. ret = vb2_queue_init(q);
  1824. if (ret)
  1825. return ret;
  1826. saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
  1827. q = &dev->vbi_vbq;
  1828. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1829. /* Don't add VB2_USERPTR, see comment above */
  1830. q->io_modes = VB2_MMAP | VB2_READ;
  1831. if (saa7134_userptr)
  1832. q->io_modes |= VB2_USERPTR;
  1833. q->drv_priv = &dev->vbi_q;
  1834. q->ops = &saa7134_vbi_qops;
  1835. q->gfp_flags = GFP_DMA32;
  1836. q->mem_ops = &vb2_dma_sg_memops;
  1837. q->buf_struct_size = sizeof(struct saa7134_buf);
  1838. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1839. q->lock = &dev->lock;
  1840. q->dev = &dev->pci->dev;
  1841. ret = vb2_queue_init(q);
  1842. if (ret)
  1843. return ret;
  1844. saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
  1845. return 0;
  1846. }
  1847. void saa7134_video_fini(struct saa7134_dev *dev)
  1848. {
  1849. del_timer_sync(&dev->video_q.timeout);
  1850. /* free stuff */
  1851. saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
  1852. saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
  1853. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1854. if (card_has_radio(dev))
  1855. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1856. }
  1857. int saa7134_videoport_init(struct saa7134_dev *dev)
  1858. {
  1859. /* enable video output */
  1860. int vo = saa7134_boards[dev->board].video_out;
  1861. int video_reg;
  1862. unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
  1863. /* Configure videoport */
  1864. saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
  1865. video_reg = video_out[vo][1];
  1866. if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
  1867. video_reg &= ~VP_T_CODE_P_INVERTED;
  1868. saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
  1869. saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
  1870. saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
  1871. video_reg = video_out[vo][5];
  1872. if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
  1873. video_reg &= ~VP_CLK_CTRL2_DELAYED;
  1874. if (vid_port_opts & SET_CLOCK_INVERTED)
  1875. video_reg |= VP_CLK_CTRL1_INVERTED;
  1876. saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
  1877. video_reg = video_out[vo][6];
  1878. if (vid_port_opts & SET_VSYNC_OFF) {
  1879. video_reg &= ~VP_VS_TYPE_MASK;
  1880. video_reg |= VP_VS_TYPE_OFF;
  1881. }
  1882. saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
  1883. saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
  1884. saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
  1885. /* Start videoport */
  1886. saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
  1887. return 0;
  1888. }
  1889. int saa7134_video_init2(struct saa7134_dev *dev)
  1890. {
  1891. /* init video hw */
  1892. set_tvnorm(dev,&tvnorms[0]);
  1893. video_mux(dev,0);
  1894. v4l2_ctrl_handler_setup(&dev->ctrl_handler);
  1895. saa7134_tvaudio_setmute(dev);
  1896. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  1897. return 0;
  1898. }
  1899. void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
  1900. {
  1901. static const char *st[] = {
  1902. "(no signal)", "NTSC", "PAL", "SECAM" };
  1903. u32 st1,st2;
  1904. st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1905. st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1906. video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
  1907. (st1 & 0x40) ? "not locked" : "locked",
  1908. (st2 & 0x40) ? "no" : "yes",
  1909. st[st1 & 0x03]);
  1910. dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
  1911. if (dev->nosignal) {
  1912. /* no video signal -> mute audio */
  1913. if (dev->ctl_automute)
  1914. dev->automute = 1;
  1915. saa7134_tvaudio_setmute(dev);
  1916. } else {
  1917. /* wake up tvaudio audio carrier scan thread */
  1918. saa7134_tvaudio_do_scan(dev);
  1919. }
  1920. if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
  1921. saa_clearb(SAA7134_SYNC_CTRL, 0x20);
  1922. else
  1923. saa_setb(SAA7134_SYNC_CTRL, 0x20);
  1924. if (dev->mops && dev->mops->signal_change)
  1925. dev->mops->signal_change(dev);
  1926. }
  1927. void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
  1928. {
  1929. enum v4l2_field field;
  1930. spin_lock(&dev->slock);
  1931. if (dev->video_q.curr) {
  1932. field = dev->field;
  1933. if (V4L2_FIELD_HAS_BOTH(field)) {
  1934. /* make sure we have seen both fields */
  1935. if ((status & 0x10) == 0x00) {
  1936. dev->video_q.curr->top_seen = 1;
  1937. goto done;
  1938. }
  1939. if (!dev->video_q.curr->top_seen)
  1940. goto done;
  1941. } else if (field == V4L2_FIELD_TOP) {
  1942. if ((status & 0x10) != 0x10)
  1943. goto done;
  1944. } else if (field == V4L2_FIELD_BOTTOM) {
  1945. if ((status & 0x10) != 0x00)
  1946. goto done;
  1947. }
  1948. saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
  1949. }
  1950. saa7134_buffer_next(dev, &dev->video_q);
  1951. done:
  1952. spin_unlock(&dev->slock);
  1953. }