saa7134-video.c 60 KB

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