vpif_display.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. /*
  2. * vpif-display - VPIF display driver
  3. * Display driver for TI DaVinci VPIF
  4. *
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/string.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <asm/irq.h>
  33. #include <asm/page.h>
  34. #include <media/adv7343.h>
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/v4l2-chip-ident.h>
  38. #include "vpif_display.h"
  39. #include "vpif.h"
  40. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  43. #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  44. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  45. #define vpif_dbg(level, debug, fmt, arg...) \
  46. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  47. static int debug = 1;
  48. static u32 ch2_numbuffers = 3;
  49. static u32 ch3_numbuffers = 3;
  50. static u32 ch2_bufsize = 1920 * 1080 * 2;
  51. static u32 ch3_bufsize = 720 * 576 * 2;
  52. module_param(debug, int, 0644);
  53. module_param(ch2_numbuffers, uint, S_IRUGO);
  54. module_param(ch3_numbuffers, uint, S_IRUGO);
  55. module_param(ch2_bufsize, uint, S_IRUGO);
  56. module_param(ch3_bufsize, uint, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level 0-1");
  58. MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  59. MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  61. MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  62. static struct vpif_config_params config_params = {
  63. .min_numbuffers = 3,
  64. .numbuffers[0] = 3,
  65. .numbuffers[1] = 3,
  66. .min_bufsize[0] = 720 * 480 * 2,
  67. .min_bufsize[1] = 720 * 480 * 2,
  68. .channel_bufsize[0] = 1920 * 1080 * 2,
  69. .channel_bufsize[1] = 720 * 576 * 2,
  70. };
  71. static struct vpif_device vpif_obj = { {NULL} };
  72. static struct device *vpif_dev;
  73. static void vpif_calculate_offsets(struct channel_obj *ch);
  74. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  75. /*
  76. * buffer_prepare: This is the callback function called from vb2_qbuf()
  77. * function the buffer is prepared and user space virtual address is converted
  78. * into physical address
  79. */
  80. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  81. {
  82. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  83. struct vb2_queue *q = vb->vb2_queue;
  84. struct common_obj *common;
  85. unsigned long addr;
  86. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  87. if (vb->state != VB2_BUF_STATE_ACTIVE &&
  88. vb->state != VB2_BUF_STATE_PREPARED) {
  89. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  90. if (vb2_plane_vaddr(vb, 0) &&
  91. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  92. goto buf_align_exit;
  93. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  94. if (q->streaming &&
  95. (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  96. if (!ISALIGNED(addr + common->ytop_off) ||
  97. !ISALIGNED(addr + common->ybtm_off) ||
  98. !ISALIGNED(addr + common->ctop_off) ||
  99. !ISALIGNED(addr + common->cbtm_off))
  100. goto buf_align_exit;
  101. }
  102. }
  103. return 0;
  104. buf_align_exit:
  105. vpif_err("buffer offset not aligned to 8 bytes\n");
  106. return -EINVAL;
  107. }
  108. /*
  109. * vpif_buffer_queue_setup: This function allocates memory for the buffers
  110. */
  111. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  112. const struct v4l2_format *fmt,
  113. unsigned int *nbuffers, unsigned int *nplanes,
  114. unsigned int sizes[], void *alloc_ctxs[])
  115. {
  116. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  117. struct channel_obj *ch = fh->channel;
  118. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  119. unsigned long size;
  120. if (V4L2_MEMORY_MMAP == common->memory) {
  121. size = config_params.channel_bufsize[ch->channel_id];
  122. /*
  123. * Checking if the buffer size exceeds the available buffer
  124. * ycmux_mode = 0 means 1 channel mode HD and
  125. * ycmux_mode = 1 means 2 channels mode SD
  126. */
  127. if (ch->vpifparams.std_info.ycmux_mode == 0) {
  128. if (config_params.video_limit[ch->channel_id])
  129. while (size * *nbuffers >
  130. (config_params.video_limit[0]
  131. + config_params.video_limit[1]))
  132. (*nbuffers)--;
  133. } else {
  134. if (config_params.video_limit[ch->channel_id])
  135. while (size * *nbuffers >
  136. config_params.video_limit[ch->channel_id])
  137. (*nbuffers)--;
  138. }
  139. } else {
  140. size = common->fmt.fmt.pix.sizeimage;
  141. }
  142. if (*nbuffers < config_params.min_numbuffers)
  143. *nbuffers = config_params.min_numbuffers;
  144. *nplanes = 1;
  145. sizes[0] = size;
  146. alloc_ctxs[0] = common->alloc_ctx;
  147. return 0;
  148. }
  149. /*
  150. * vpif_buffer_queue: This function adds the buffer to DMA queue
  151. */
  152. static void vpif_buffer_queue(struct vb2_buffer *vb)
  153. {
  154. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  155. struct vpif_disp_buffer *buf = container_of(vb,
  156. struct vpif_disp_buffer, vb);
  157. struct channel_obj *ch = fh->channel;
  158. struct common_obj *common;
  159. common = &ch->common[VPIF_VIDEO_INDEX];
  160. /* add the buffer to the DMA queue */
  161. list_add_tail(&buf->list, &common->dma_queue);
  162. }
  163. /*
  164. * vpif_buf_cleanup: This function is called from the videobuf2 layer to
  165. * free memory allocated to the buffers
  166. */
  167. static void vpif_buf_cleanup(struct vb2_buffer *vb)
  168. {
  169. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  170. struct vpif_disp_buffer *buf = container_of(vb,
  171. struct vpif_disp_buffer, vb);
  172. struct channel_obj *ch = fh->channel;
  173. struct common_obj *common;
  174. unsigned long flags;
  175. common = &ch->common[VPIF_VIDEO_INDEX];
  176. spin_lock_irqsave(&common->irqlock, flags);
  177. if (vb->state == VB2_BUF_STATE_ACTIVE)
  178. list_del_init(&buf->list);
  179. spin_unlock_irqrestore(&common->irqlock, flags);
  180. }
  181. static void vpif_wait_prepare(struct vb2_queue *vq)
  182. {
  183. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  184. struct channel_obj *ch = fh->channel;
  185. struct common_obj *common;
  186. common = &ch->common[VPIF_VIDEO_INDEX];
  187. mutex_unlock(&common->lock);
  188. }
  189. static void vpif_wait_finish(struct vb2_queue *vq)
  190. {
  191. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  192. struct channel_obj *ch = fh->channel;
  193. struct common_obj *common;
  194. common = &ch->common[VPIF_VIDEO_INDEX];
  195. mutex_lock(&common->lock);
  196. }
  197. static int vpif_buffer_init(struct vb2_buffer *vb)
  198. {
  199. struct vpif_disp_buffer *buf = container_of(vb,
  200. struct vpif_disp_buffer, vb);
  201. INIT_LIST_HEAD(&buf->list);
  202. return 0;
  203. }
  204. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  205. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  206. {
  207. struct vpif_display_config *vpif_config_data =
  208. vpif_dev->platform_data;
  209. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  210. struct channel_obj *ch = fh->channel;
  211. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  212. struct vpif_params *vpif = &ch->vpifparams;
  213. unsigned long addr = 0;
  214. int ret;
  215. /* If buffer queue is empty, return error */
  216. if (list_empty(&common->dma_queue)) {
  217. vpif_err("buffer queue is empty\n");
  218. return -EIO;
  219. }
  220. /* Get the next frame from the buffer queue */
  221. common->next_frm = common->cur_frm =
  222. list_entry(common->dma_queue.next,
  223. struct vpif_disp_buffer, list);
  224. list_del(&common->cur_frm->list);
  225. /* Mark state of the current frame to active */
  226. common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  227. /* Initialize field_id and started member */
  228. ch->field_id = 0;
  229. common->started = 1;
  230. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
  231. /* Calculate the offset for Y and C data in the buffer */
  232. vpif_calculate_offsets(ch);
  233. if ((ch->vpifparams.std_info.frm_fmt &&
  234. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  235. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  236. || (!ch->vpifparams.std_info.frm_fmt
  237. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  238. vpif_err("conflict in field format and std format\n");
  239. return -EINVAL;
  240. }
  241. /* clock settings */
  242. ret =
  243. vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
  244. ch->vpifparams.std_info.hd_sd);
  245. if (ret < 0) {
  246. vpif_err("can't set clock\n");
  247. return ret;
  248. }
  249. /* set the parameters and addresses */
  250. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  251. if (ret < 0)
  252. return ret;
  253. common->started = ret;
  254. vpif_config_addr(ch, ret);
  255. common->set_addr((addr + common->ytop_off),
  256. (addr + common->ybtm_off),
  257. (addr + common->ctop_off),
  258. (addr + common->cbtm_off));
  259. /* Set interrupt for both the fields in VPIF
  260. Register enable channel in VPIF register */
  261. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  262. channel2_intr_assert();
  263. channel2_intr_enable(1);
  264. enable_channel2(1);
  265. if (vpif_config_data->ch2_clip_en)
  266. channel2_clipping_enable(1);
  267. }
  268. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  269. || (common->started == 2)) {
  270. channel3_intr_assert();
  271. channel3_intr_enable(1);
  272. enable_channel3(1);
  273. if (vpif_config_data->ch3_clip_en)
  274. channel3_clipping_enable(1);
  275. }
  276. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  277. return 0;
  278. }
  279. /* abort streaming and wait for last buffer */
  280. static int vpif_stop_streaming(struct vb2_queue *vq)
  281. {
  282. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  283. struct channel_obj *ch = fh->channel;
  284. struct common_obj *common;
  285. if (!vb2_is_streaming(vq))
  286. return 0;
  287. common = &ch->common[VPIF_VIDEO_INDEX];
  288. /* release all active buffers */
  289. while (!list_empty(&common->dma_queue)) {
  290. common->next_frm = list_entry(common->dma_queue.next,
  291. struct vpif_disp_buffer, list);
  292. list_del(&common->next_frm->list);
  293. vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
  294. }
  295. return 0;
  296. }
  297. static struct vb2_ops video_qops = {
  298. .queue_setup = vpif_buffer_queue_setup,
  299. .wait_prepare = vpif_wait_prepare,
  300. .wait_finish = vpif_wait_finish,
  301. .buf_init = vpif_buffer_init,
  302. .buf_prepare = vpif_buffer_prepare,
  303. .start_streaming = vpif_start_streaming,
  304. .stop_streaming = vpif_stop_streaming,
  305. .buf_cleanup = vpif_buf_cleanup,
  306. .buf_queue = vpif_buffer_queue,
  307. };
  308. static void process_progressive_mode(struct common_obj *common)
  309. {
  310. unsigned long addr = 0;
  311. /* Get the next buffer from buffer queue */
  312. common->next_frm = list_entry(common->dma_queue.next,
  313. struct vpif_disp_buffer, list);
  314. /* Remove that buffer from the buffer queue */
  315. list_del(&common->next_frm->list);
  316. /* Mark status of the buffer as active */
  317. common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  318. /* Set top and bottom field addrs in VPIF registers */
  319. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
  320. common->set_addr(addr + common->ytop_off,
  321. addr + common->ybtm_off,
  322. addr + common->ctop_off,
  323. addr + common->cbtm_off);
  324. }
  325. static void process_interlaced_mode(int fid, struct common_obj *common)
  326. {
  327. /* device field id and local field id are in sync */
  328. /* If this is even field */
  329. if (0 == fid) {
  330. if (common->cur_frm == common->next_frm)
  331. return;
  332. /* one frame is displayed If next frame is
  333. * available, release cur_frm and move on */
  334. /* Copy frame display time */
  335. do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
  336. /* Change status of the cur_frm */
  337. vb2_buffer_done(&common->cur_frm->vb,
  338. VB2_BUF_STATE_DONE);
  339. /* Make cur_frm pointing to next_frm */
  340. common->cur_frm = common->next_frm;
  341. } else if (1 == fid) { /* odd field */
  342. if (list_empty(&common->dma_queue)
  343. || (common->cur_frm != common->next_frm)) {
  344. return;
  345. }
  346. /* one field is displayed configure the next
  347. * frame if it is available else hold on current
  348. * frame */
  349. /* Get next from the buffer queue */
  350. process_progressive_mode(common);
  351. }
  352. }
  353. /*
  354. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  355. * buffer from the queue and sets its address in VPIF registers
  356. */
  357. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  358. {
  359. struct vpif_device *dev = &vpif_obj;
  360. struct channel_obj *ch;
  361. struct common_obj *common;
  362. enum v4l2_field field;
  363. int fid = -1, i;
  364. int channel_id = 0;
  365. channel_id = *(int *)(dev_id);
  366. if (!vpif_intr_status(channel_id + 2))
  367. return IRQ_NONE;
  368. ch = dev->dev[channel_id];
  369. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  370. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  371. common = &ch->common[i];
  372. /* If streaming is started in this channel */
  373. if (0 == common->started)
  374. continue;
  375. if (1 == ch->vpifparams.std_info.frm_fmt) {
  376. if (list_empty(&common->dma_queue))
  377. continue;
  378. /* Progressive mode */
  379. if (!channel_first_int[i][channel_id]) {
  380. /* Mark status of the cur_frm to
  381. * done and unlock semaphore on it */
  382. do_gettimeofday(&common->cur_frm->vb.
  383. v4l2_buf.timestamp);
  384. vb2_buffer_done(&common->cur_frm->vb,
  385. VB2_BUF_STATE_DONE);
  386. /* Make cur_frm pointing to next_frm */
  387. common->cur_frm = common->next_frm;
  388. }
  389. channel_first_int[i][channel_id] = 0;
  390. process_progressive_mode(common);
  391. } else {
  392. /* Interlaced mode */
  393. /* If it is first interrupt, ignore it */
  394. if (channel_first_int[i][channel_id]) {
  395. channel_first_int[i][channel_id] = 0;
  396. continue;
  397. }
  398. if (0 == i) {
  399. ch->field_id ^= 1;
  400. /* Get field id from VPIF registers */
  401. fid = vpif_channel_getfid(ch->channel_id + 2);
  402. /* If fid does not match with stored field id */
  403. if (fid != ch->field_id) {
  404. /* Make them in sync */
  405. if (0 == fid)
  406. ch->field_id = fid;
  407. return IRQ_HANDLED;
  408. }
  409. }
  410. process_interlaced_mode(fid, common);
  411. }
  412. }
  413. return IRQ_HANDLED;
  414. }
  415. static int vpif_update_std_info(struct channel_obj *ch)
  416. {
  417. struct video_obj *vid_ch = &ch->video;
  418. struct vpif_params *vpifparams = &ch->vpifparams;
  419. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  420. const struct vpif_channel_config_params *config;
  421. int i;
  422. for (i = 0; i < vpif_ch_params_count; i++) {
  423. config = &ch_params[i];
  424. if (config->hd_sd == 0) {
  425. vpif_dbg(2, debug, "SD format\n");
  426. if (config->stdid & vid_ch->stdid) {
  427. memcpy(std_info, config, sizeof(*config));
  428. break;
  429. }
  430. } else {
  431. vpif_dbg(2, debug, "HD format\n");
  432. if (config->dv_preset == vid_ch->dv_preset) {
  433. memcpy(std_info, config, sizeof(*config));
  434. break;
  435. }
  436. }
  437. }
  438. if (i == vpif_ch_params_count) {
  439. vpif_dbg(1, debug, "Format not found\n");
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. static int vpif_update_resolution(struct channel_obj *ch)
  445. {
  446. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  447. struct video_obj *vid_ch = &ch->video;
  448. struct vpif_params *vpifparams = &ch->vpifparams;
  449. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  450. if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
  451. return -EINVAL;
  452. if (vid_ch->stdid || vid_ch->dv_preset) {
  453. if (vpif_update_std_info(ch))
  454. return -EINVAL;
  455. }
  456. common->fmt.fmt.pix.width = std_info->width;
  457. common->fmt.fmt.pix.height = std_info->height;
  458. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  459. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  460. /* Set height and width paramateres */
  461. common->height = std_info->height;
  462. common->width = std_info->width;
  463. return 0;
  464. }
  465. /*
  466. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  467. * in the top and bottom field
  468. */
  469. static void vpif_calculate_offsets(struct channel_obj *ch)
  470. {
  471. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  472. struct vpif_params *vpifparams = &ch->vpifparams;
  473. enum v4l2_field field = common->fmt.fmt.pix.field;
  474. struct video_obj *vid_ch = &ch->video;
  475. unsigned int hpitch, vpitch, sizeimage;
  476. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  477. if (ch->vpifparams.std_info.frm_fmt)
  478. vid_ch->buf_field = V4L2_FIELD_NONE;
  479. else
  480. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  481. } else {
  482. vid_ch->buf_field = common->fmt.fmt.pix.field;
  483. }
  484. sizeimage = common->fmt.fmt.pix.sizeimage;
  485. hpitch = common->fmt.fmt.pix.bytesperline;
  486. vpitch = sizeimage / (hpitch * 2);
  487. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  488. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  489. common->ytop_off = 0;
  490. common->ybtm_off = hpitch;
  491. common->ctop_off = sizeimage / 2;
  492. common->cbtm_off = sizeimage / 2 + hpitch;
  493. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  494. common->ytop_off = 0;
  495. common->ybtm_off = sizeimage / 4;
  496. common->ctop_off = sizeimage / 2;
  497. common->cbtm_off = common->ctop_off + sizeimage / 4;
  498. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  499. common->ybtm_off = 0;
  500. common->ytop_off = sizeimage / 4;
  501. common->cbtm_off = sizeimage / 2;
  502. common->ctop_off = common->cbtm_off + sizeimage / 4;
  503. }
  504. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  505. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  506. vpifparams->video_params.storage_mode = 1;
  507. } else {
  508. vpifparams->video_params.storage_mode = 0;
  509. }
  510. if (ch->vpifparams.std_info.frm_fmt == 1) {
  511. vpifparams->video_params.hpitch =
  512. common->fmt.fmt.pix.bytesperline;
  513. } else {
  514. if ((field == V4L2_FIELD_ANY) ||
  515. (field == V4L2_FIELD_INTERLACED))
  516. vpifparams->video_params.hpitch =
  517. common->fmt.fmt.pix.bytesperline * 2;
  518. else
  519. vpifparams->video_params.hpitch =
  520. common->fmt.fmt.pix.bytesperline;
  521. }
  522. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  523. }
  524. static void vpif_config_format(struct channel_obj *ch)
  525. {
  526. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  527. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  528. if (config_params.numbuffers[ch->channel_id] == 0)
  529. common->memory = V4L2_MEMORY_USERPTR;
  530. else
  531. common->memory = V4L2_MEMORY_MMAP;
  532. common->fmt.fmt.pix.sizeimage =
  533. config_params.channel_bufsize[ch->channel_id];
  534. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  535. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  536. }
  537. static int vpif_check_format(struct channel_obj *ch,
  538. struct v4l2_pix_format *pixfmt)
  539. {
  540. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  541. enum v4l2_field field = pixfmt->field;
  542. u32 sizeimage, hpitch, vpitch;
  543. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  544. goto invalid_fmt_exit;
  545. if (!(VPIF_VALID_FIELD(field)))
  546. goto invalid_fmt_exit;
  547. if (pixfmt->bytesperline <= 0)
  548. goto invalid_pitch_exit;
  549. sizeimage = pixfmt->sizeimage;
  550. if (vpif_update_resolution(ch))
  551. return -EINVAL;
  552. hpitch = pixfmt->bytesperline;
  553. vpitch = sizeimage / (hpitch * 2);
  554. /* Check for valid value of pitch */
  555. if ((hpitch < ch->vpifparams.std_info.width) ||
  556. (vpitch < ch->vpifparams.std_info.height))
  557. goto invalid_pitch_exit;
  558. /* Check for 8 byte alignment */
  559. if (!ISALIGNED(hpitch)) {
  560. vpif_err("invalid pitch alignment\n");
  561. return -EINVAL;
  562. }
  563. pixfmt->width = common->fmt.fmt.pix.width;
  564. pixfmt->height = common->fmt.fmt.pix.height;
  565. return 0;
  566. invalid_fmt_exit:
  567. vpif_err("invalid field format\n");
  568. return -EINVAL;
  569. invalid_pitch_exit:
  570. vpif_err("invalid pitch\n");
  571. return -EINVAL;
  572. }
  573. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  574. {
  575. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  576. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  577. common->set_addr = ch3_set_videobuf_addr;
  578. } else {
  579. if (2 == muxmode)
  580. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  581. else
  582. common->set_addr = ch2_set_videobuf_addr;
  583. }
  584. }
  585. /*
  586. * vpif_mmap: It is used to map kernel space buffers into user spaces
  587. */
  588. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  589. {
  590. struct vpif_fh *fh = filep->private_data;
  591. struct channel_obj *ch = fh->channel;
  592. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  593. vpif_dbg(2, debug, "vpif_mmap\n");
  594. return vb2_mmap(&common->buffer_queue, vma);
  595. }
  596. /*
  597. * vpif_poll: It is used for select/poll system call
  598. */
  599. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  600. {
  601. struct vpif_fh *fh = filep->private_data;
  602. struct channel_obj *ch = fh->channel;
  603. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  604. if (common->started)
  605. return vb2_poll(&common->buffer_queue, filep, wait);
  606. return 0;
  607. }
  608. /*
  609. * vpif_open: It creates object of file handle structure and stores it in
  610. * private_data member of filepointer
  611. */
  612. static int vpif_open(struct file *filep)
  613. {
  614. struct video_device *vdev = video_devdata(filep);
  615. struct channel_obj *ch = NULL;
  616. struct vpif_fh *fh = NULL;
  617. ch = video_get_drvdata(vdev);
  618. /* Allocate memory for the file handle object */
  619. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  620. if (fh == NULL) {
  621. vpif_err("unable to allocate memory for file handle object\n");
  622. return -ENOMEM;
  623. }
  624. /* store pointer to fh in private_data member of filep */
  625. filep->private_data = fh;
  626. fh->channel = ch;
  627. fh->initialized = 0;
  628. if (!ch->initialized) {
  629. fh->initialized = 1;
  630. ch->initialized = 1;
  631. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  632. }
  633. /* Increment channel usrs counter */
  634. atomic_inc(&ch->usrs);
  635. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  636. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  637. /* Initialize priority of this instance to default priority */
  638. fh->prio = V4L2_PRIORITY_UNSET;
  639. v4l2_prio_open(&ch->prio, &fh->prio);
  640. return 0;
  641. }
  642. /*
  643. * vpif_release: This function deletes buffer queue, frees the buffers and
  644. * the vpif file handle
  645. */
  646. static int vpif_release(struct file *filep)
  647. {
  648. struct vpif_fh *fh = filep->private_data;
  649. struct channel_obj *ch = fh->channel;
  650. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  651. /* if this instance is doing IO */
  652. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  653. /* Reset io_usrs member of channel object */
  654. common->io_usrs = 0;
  655. /* Disable channel */
  656. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  657. enable_channel2(0);
  658. channel2_intr_enable(0);
  659. }
  660. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  661. (2 == common->started)) {
  662. enable_channel3(0);
  663. channel3_intr_enable(0);
  664. }
  665. common->started = 0;
  666. /* Free buffers allocated */
  667. vb2_queue_release(&common->buffer_queue);
  668. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  669. common->numbuffers =
  670. config_params.numbuffers[ch->channel_id];
  671. }
  672. /* Decrement channel usrs counter */
  673. atomic_dec(&ch->usrs);
  674. /* If this file handle has initialize encoder device, reset it */
  675. if (fh->initialized)
  676. ch->initialized = 0;
  677. /* Close the priority */
  678. v4l2_prio_close(&ch->prio, fh->prio);
  679. filep->private_data = NULL;
  680. fh->initialized = 0;
  681. kfree(fh);
  682. return 0;
  683. }
  684. /* functions implementing ioctls */
  685. /**
  686. * vpif_querycap() - QUERYCAP handler
  687. * @file: file ptr
  688. * @priv: file handle
  689. * @cap: ptr to v4l2_capability structure
  690. */
  691. static int vpif_querycap(struct file *file, void *priv,
  692. struct v4l2_capability *cap)
  693. {
  694. struct vpif_display_config *config = vpif_dev->platform_data;
  695. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  696. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  697. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  698. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  699. return 0;
  700. }
  701. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  702. struct v4l2_fmtdesc *fmt)
  703. {
  704. if (fmt->index != 0) {
  705. vpif_err("Invalid format index\n");
  706. return -EINVAL;
  707. }
  708. /* Fill in the information about format */
  709. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  710. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  711. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  712. return 0;
  713. }
  714. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  715. struct v4l2_format *fmt)
  716. {
  717. struct vpif_fh *fh = priv;
  718. struct channel_obj *ch = fh->channel;
  719. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  720. /* Check the validity of the buffer type */
  721. if (common->fmt.type != fmt->type)
  722. return -EINVAL;
  723. if (vpif_update_resolution(ch))
  724. return -EINVAL;
  725. *fmt = common->fmt;
  726. return 0;
  727. }
  728. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  729. struct v4l2_format *fmt)
  730. {
  731. struct vpif_fh *fh = priv;
  732. struct v4l2_pix_format *pixfmt;
  733. struct channel_obj *ch = fh->channel;
  734. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  735. int ret = 0;
  736. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  737. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  738. if (!fh->initialized) {
  739. vpif_dbg(1, debug, "Channel Busy\n");
  740. return -EBUSY;
  741. }
  742. /* Check for the priority */
  743. ret = v4l2_prio_check(&ch->prio, fh->prio);
  744. if (0 != ret)
  745. return ret;
  746. fh->initialized = 1;
  747. }
  748. if (common->started) {
  749. vpif_dbg(1, debug, "Streaming in progress\n");
  750. return -EBUSY;
  751. }
  752. pixfmt = &fmt->fmt.pix;
  753. /* Check for valid field format */
  754. ret = vpif_check_format(ch, pixfmt);
  755. if (ret)
  756. return ret;
  757. /* store the pix format in the channel object */
  758. common->fmt.fmt.pix = *pixfmt;
  759. /* store the format in the channel object */
  760. common->fmt = *fmt;
  761. return 0;
  762. }
  763. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  764. struct v4l2_format *fmt)
  765. {
  766. struct vpif_fh *fh = priv;
  767. struct channel_obj *ch = fh->channel;
  768. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  769. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  770. int ret = 0;
  771. ret = vpif_check_format(ch, pixfmt);
  772. if (ret) {
  773. *pixfmt = common->fmt.fmt.pix;
  774. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  775. }
  776. return ret;
  777. }
  778. static int vpif_reqbufs(struct file *file, void *priv,
  779. struct v4l2_requestbuffers *reqbuf)
  780. {
  781. struct vpif_fh *fh = priv;
  782. struct channel_obj *ch = fh->channel;
  783. struct common_obj *common;
  784. enum v4l2_field field;
  785. struct vb2_queue *q;
  786. u8 index = 0;
  787. /* This file handle has not initialized the channel,
  788. It is not allowed to do settings */
  789. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  790. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  791. if (!fh->initialized) {
  792. vpif_err("Channel Busy\n");
  793. return -EBUSY;
  794. }
  795. }
  796. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  797. return -EINVAL;
  798. index = VPIF_VIDEO_INDEX;
  799. common = &ch->common[index];
  800. if (common->fmt.type != reqbuf->type || !vpif_dev)
  801. return -EINVAL;
  802. if (0 != common->io_usrs)
  803. return -EBUSY;
  804. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  805. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  806. field = V4L2_FIELD_INTERLACED;
  807. else
  808. field = common->fmt.fmt.pix.field;
  809. } else {
  810. field = V4L2_VBI_INTERLACED;
  811. }
  812. /* Initialize videobuf2 queue as per the buffer type */
  813. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  814. if (!common->alloc_ctx) {
  815. vpif_err("Failed to get the context\n");
  816. return -EINVAL;
  817. }
  818. q = &common->buffer_queue;
  819. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  820. q->io_modes = VB2_MMAP | VB2_USERPTR;
  821. q->drv_priv = fh;
  822. q->ops = &video_qops;
  823. q->mem_ops = &vb2_dma_contig_memops;
  824. q->buf_struct_size = sizeof(struct vpif_disp_buffer);
  825. vb2_queue_init(q);
  826. /* Set io allowed member of file handle to TRUE */
  827. fh->io_allowed[index] = 1;
  828. /* Increment io usrs member of channel object to 1 */
  829. common->io_usrs = 1;
  830. /* Store type of memory requested in channel object */
  831. common->memory = reqbuf->memory;
  832. INIT_LIST_HEAD(&common->dma_queue);
  833. /* Allocate buffers */
  834. return vb2_reqbufs(&common->buffer_queue, reqbuf);
  835. }
  836. static int vpif_querybuf(struct file *file, void *priv,
  837. struct v4l2_buffer *tbuf)
  838. {
  839. struct vpif_fh *fh = priv;
  840. struct channel_obj *ch = fh->channel;
  841. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  842. if (common->fmt.type != tbuf->type)
  843. return -EINVAL;
  844. return vb2_querybuf(&common->buffer_queue, tbuf);
  845. }
  846. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  847. {
  848. struct vpif_fh *fh = NULL;
  849. struct channel_obj *ch = NULL;
  850. struct common_obj *common = NULL;
  851. if (!buf || !priv)
  852. return -EINVAL;
  853. fh = priv;
  854. ch = fh->channel;
  855. if (!ch)
  856. return -EINVAL;
  857. common = &(ch->common[VPIF_VIDEO_INDEX]);
  858. if (common->fmt.type != buf->type)
  859. return -EINVAL;
  860. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  861. vpif_err("fh->io_allowed\n");
  862. return -EACCES;
  863. }
  864. return vb2_qbuf(&common->buffer_queue, buf);
  865. }
  866. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  867. {
  868. struct vpif_fh *fh = priv;
  869. struct channel_obj *ch = fh->channel;
  870. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  871. int ret = 0;
  872. if (!(*std_id & VPIF_V4L2_STD))
  873. return -EINVAL;
  874. if (common->started) {
  875. vpif_err("streaming in progress\n");
  876. return -EBUSY;
  877. }
  878. /* Call encoder subdevice function to set the standard */
  879. ch->video.stdid = *std_id;
  880. ch->video.dv_preset = V4L2_DV_INVALID;
  881. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  882. /* Get the information about the standard */
  883. if (vpif_update_resolution(ch))
  884. return -EINVAL;
  885. if ((ch->vpifparams.std_info.width *
  886. ch->vpifparams.std_info.height * 2) >
  887. config_params.channel_bufsize[ch->channel_id]) {
  888. vpif_err("invalid std for this size\n");
  889. return -EINVAL;
  890. }
  891. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  892. /* Configure the default format information */
  893. vpif_config_format(ch);
  894. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  895. s_std_output, *std_id);
  896. if (ret < 0) {
  897. vpif_err("Failed to set output standard\n");
  898. return ret;
  899. }
  900. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  901. s_std, *std_id);
  902. if (ret < 0)
  903. vpif_err("Failed to set standard for sub devices\n");
  904. return ret;
  905. }
  906. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  907. {
  908. struct vpif_fh *fh = priv;
  909. struct channel_obj *ch = fh->channel;
  910. *std = ch->video.stdid;
  911. return 0;
  912. }
  913. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  914. {
  915. struct vpif_fh *fh = priv;
  916. struct channel_obj *ch = fh->channel;
  917. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  918. return vb2_dqbuf(&common->buffer_queue, p,
  919. (file->f_flags & O_NONBLOCK));
  920. }
  921. static int vpif_streamon(struct file *file, void *priv,
  922. enum v4l2_buf_type buftype)
  923. {
  924. struct vpif_fh *fh = priv;
  925. struct channel_obj *ch = fh->channel;
  926. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  927. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  928. int ret = 0;
  929. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  930. vpif_err("buffer type not supported\n");
  931. return -EINVAL;
  932. }
  933. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  934. vpif_err("fh->io_allowed\n");
  935. return -EACCES;
  936. }
  937. /* If Streaming is already started, return error */
  938. if (common->started) {
  939. vpif_err("channel->started\n");
  940. return -EBUSY;
  941. }
  942. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  943. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  944. ch->vpifparams.std_info.ycmux_mode == 0)
  945. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  946. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  947. vpif_err("other channel is using\n");
  948. return -EBUSY;
  949. }
  950. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  951. if (ret < 0)
  952. return ret;
  953. /* Call vb2_streamon to start streaming in videobuf2 */
  954. ret = vb2_streamon(&common->buffer_queue, buftype);
  955. if (ret < 0) {
  956. vpif_err("vb2_streamon\n");
  957. return ret;
  958. }
  959. return ret;
  960. }
  961. static int vpif_streamoff(struct file *file, void *priv,
  962. enum v4l2_buf_type buftype)
  963. {
  964. struct vpif_fh *fh = priv;
  965. struct channel_obj *ch = fh->channel;
  966. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  967. struct vpif_display_config *vpif_config_data =
  968. vpif_dev->platform_data;
  969. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  970. vpif_err("buffer type not supported\n");
  971. return -EINVAL;
  972. }
  973. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  974. vpif_err("fh->io_allowed\n");
  975. return -EACCES;
  976. }
  977. if (!common->started) {
  978. vpif_err("channel->started\n");
  979. return -EINVAL;
  980. }
  981. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  982. /* disable channel */
  983. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  984. if (vpif_config_data->ch2_clip_en)
  985. channel2_clipping_enable(0);
  986. enable_channel2(0);
  987. channel2_intr_enable(0);
  988. }
  989. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  990. (2 == common->started)) {
  991. if (vpif_config_data->ch3_clip_en)
  992. channel3_clipping_enable(0);
  993. enable_channel3(0);
  994. channel3_intr_enable(0);
  995. }
  996. }
  997. common->started = 0;
  998. return vb2_streamoff(&common->buffer_queue, buftype);
  999. }
  1000. static int vpif_cropcap(struct file *file, void *priv,
  1001. struct v4l2_cropcap *crop)
  1002. {
  1003. struct vpif_fh *fh = priv;
  1004. struct channel_obj *ch = fh->channel;
  1005. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1006. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1007. return -EINVAL;
  1008. crop->bounds.left = crop->bounds.top = 0;
  1009. crop->defrect.left = crop->defrect.top = 0;
  1010. crop->defrect.height = crop->bounds.height = common->height;
  1011. crop->defrect.width = crop->bounds.width = common->width;
  1012. return 0;
  1013. }
  1014. static int vpif_enum_output(struct file *file, void *fh,
  1015. struct v4l2_output *output)
  1016. {
  1017. struct vpif_display_config *config = vpif_dev->platform_data;
  1018. if (output->index >= config->output_count) {
  1019. vpif_dbg(1, debug, "Invalid output index\n");
  1020. return -EINVAL;
  1021. }
  1022. strcpy(output->name, config->output[output->index]);
  1023. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1024. output->std = VPIF_V4L2_STD;
  1025. return 0;
  1026. }
  1027. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1028. {
  1029. struct vpif_fh *fh = priv;
  1030. struct channel_obj *ch = fh->channel;
  1031. struct video_obj *vid_ch = &ch->video;
  1032. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1033. int ret = 0;
  1034. if (common->started) {
  1035. vpif_err("Streaming in progress\n");
  1036. return -EBUSY;
  1037. }
  1038. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1039. s_routing, 0, i, 0);
  1040. if (ret < 0)
  1041. vpif_err("Failed to set output standard\n");
  1042. vid_ch->output_id = i;
  1043. return ret;
  1044. }
  1045. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1046. {
  1047. struct vpif_fh *fh = priv;
  1048. struct channel_obj *ch = fh->channel;
  1049. struct video_obj *vid_ch = &ch->video;
  1050. *i = vid_ch->output_id;
  1051. return 0;
  1052. }
  1053. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1054. {
  1055. struct vpif_fh *fh = priv;
  1056. struct channel_obj *ch = fh->channel;
  1057. *p = v4l2_prio_max(&ch->prio);
  1058. return 0;
  1059. }
  1060. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1061. {
  1062. struct vpif_fh *fh = priv;
  1063. struct channel_obj *ch = fh->channel;
  1064. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1065. }
  1066. /**
  1067. * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
  1068. * @file: file ptr
  1069. * @priv: file handle
  1070. * @preset: input preset
  1071. */
  1072. static int vpif_enum_dv_presets(struct file *file, void *priv,
  1073. struct v4l2_dv_enum_preset *preset)
  1074. {
  1075. struct vpif_fh *fh = priv;
  1076. struct channel_obj *ch = fh->channel;
  1077. struct video_obj *vid_ch = &ch->video;
  1078. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1079. video, enum_dv_presets, preset);
  1080. }
  1081. /**
  1082. * vpif_s_dv_presets() - S_DV_PRESETS handler
  1083. * @file: file ptr
  1084. * @priv: file handle
  1085. * @preset: input preset
  1086. */
  1087. static int vpif_s_dv_preset(struct file *file, void *priv,
  1088. struct v4l2_dv_preset *preset)
  1089. {
  1090. struct vpif_fh *fh = priv;
  1091. struct channel_obj *ch = fh->channel;
  1092. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1093. struct video_obj *vid_ch = &ch->video;
  1094. int ret = 0;
  1095. if (common->started) {
  1096. vpif_dbg(1, debug, "streaming in progress\n");
  1097. return -EBUSY;
  1098. }
  1099. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1100. if (ret != 0)
  1101. return ret;
  1102. fh->initialized = 1;
  1103. /* Call encoder subdevice function to set the standard */
  1104. if (mutex_lock_interruptible(&common->lock))
  1105. return -ERESTARTSYS;
  1106. ch->video.dv_preset = preset->preset;
  1107. ch->video.stdid = V4L2_STD_UNKNOWN;
  1108. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1109. /* Get the information about the standard */
  1110. if (vpif_update_resolution(ch)) {
  1111. ret = -EINVAL;
  1112. } else {
  1113. /* Configure the default format information */
  1114. vpif_config_format(ch);
  1115. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1116. video, s_dv_preset, preset);
  1117. }
  1118. mutex_unlock(&common->lock);
  1119. return ret;
  1120. }
  1121. /**
  1122. * vpif_g_dv_presets() - G_DV_PRESETS handler
  1123. * @file: file ptr
  1124. * @priv: file handle
  1125. * @preset: input preset
  1126. */
  1127. static int vpif_g_dv_preset(struct file *file, void *priv,
  1128. struct v4l2_dv_preset *preset)
  1129. {
  1130. struct vpif_fh *fh = priv;
  1131. struct channel_obj *ch = fh->channel;
  1132. preset->preset = ch->video.dv_preset;
  1133. return 0;
  1134. }
  1135. /**
  1136. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1137. * @file: file ptr
  1138. * @priv: file handle
  1139. * @timings: digital video timings
  1140. */
  1141. static int vpif_s_dv_timings(struct file *file, void *priv,
  1142. struct v4l2_dv_timings *timings)
  1143. {
  1144. struct vpif_fh *fh = priv;
  1145. struct channel_obj *ch = fh->channel;
  1146. struct vpif_params *vpifparams = &ch->vpifparams;
  1147. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1148. struct video_obj *vid_ch = &ch->video;
  1149. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1150. int ret;
  1151. if (timings->type != V4L2_DV_BT_656_1120) {
  1152. vpif_dbg(2, debug, "Timing type not defined\n");
  1153. return -EINVAL;
  1154. }
  1155. /* Configure subdevice timings, if any */
  1156. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1157. video, s_dv_timings, timings);
  1158. if (ret == -ENOIOCTLCMD) {
  1159. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1160. "subdevice\n");
  1161. return -EINVAL;
  1162. }
  1163. if (ret < 0) {
  1164. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1165. return ret;
  1166. }
  1167. if (!(timings->bt.width && timings->bt.height &&
  1168. (timings->bt.hbackporch ||
  1169. timings->bt.hfrontporch ||
  1170. timings->bt.hsync) &&
  1171. timings->bt.vfrontporch &&
  1172. (timings->bt.vbackporch ||
  1173. timings->bt.vsync))) {
  1174. vpif_dbg(2, debug, "Timings for width, height, "
  1175. "horizontal back porch, horizontal sync, "
  1176. "horizontal front porch, vertical back porch, "
  1177. "vertical sync and vertical back porch "
  1178. "must be defined\n");
  1179. return -EINVAL;
  1180. }
  1181. *bt = timings->bt;
  1182. /* Configure video port timings */
  1183. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1184. bt->hsync - 8;
  1185. std_info->sav2eav = bt->width;
  1186. std_info->l1 = 1;
  1187. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1188. if (bt->interlaced) {
  1189. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1190. std_info->vsize = bt->height * 2 +
  1191. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1192. bt->il_vfrontporch + bt->il_vsync +
  1193. bt->il_vbackporch;
  1194. std_info->l5 = std_info->vsize/2 -
  1195. (bt->vfrontporch - 1);
  1196. std_info->l7 = std_info->vsize/2 + 1;
  1197. std_info->l9 = std_info->l7 + bt->il_vsync +
  1198. bt->il_vbackporch + 1;
  1199. std_info->l11 = std_info->vsize -
  1200. (bt->il_vfrontporch - 1);
  1201. } else {
  1202. vpif_dbg(2, debug, "Required timing values for "
  1203. "interlaced BT format missing\n");
  1204. return -EINVAL;
  1205. }
  1206. } else {
  1207. std_info->vsize = bt->height + bt->vfrontporch +
  1208. bt->vsync + bt->vbackporch;
  1209. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1210. }
  1211. strncpy(std_info->name, "Custom timings BT656/1120",
  1212. VPIF_MAX_NAME);
  1213. std_info->width = bt->width;
  1214. std_info->height = bt->height;
  1215. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1216. std_info->ycmux_mode = 0;
  1217. std_info->capture_format = 0;
  1218. std_info->vbi_supported = 0;
  1219. std_info->hd_sd = 1;
  1220. std_info->stdid = 0;
  1221. std_info->dv_preset = V4L2_DV_INVALID;
  1222. vid_ch->stdid = 0;
  1223. vid_ch->dv_preset = V4L2_DV_INVALID;
  1224. return 0;
  1225. }
  1226. /**
  1227. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1228. * @file: file ptr
  1229. * @priv: file handle
  1230. * @timings: digital video timings
  1231. */
  1232. static int vpif_g_dv_timings(struct file *file, void *priv,
  1233. struct v4l2_dv_timings *timings)
  1234. {
  1235. struct vpif_fh *fh = priv;
  1236. struct channel_obj *ch = fh->channel;
  1237. struct video_obj *vid_ch = &ch->video;
  1238. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1239. timings->bt = *bt;
  1240. return 0;
  1241. }
  1242. /*
  1243. * vpif_g_chip_ident() - Identify the chip
  1244. * @file: file ptr
  1245. * @priv: file handle
  1246. * @chip: chip identity
  1247. *
  1248. * Returns zero or -EINVAL if read operations fails.
  1249. */
  1250. static int vpif_g_chip_ident(struct file *file, void *priv,
  1251. struct v4l2_dbg_chip_ident *chip)
  1252. {
  1253. chip->ident = V4L2_IDENT_NONE;
  1254. chip->revision = 0;
  1255. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1256. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1257. vpif_dbg(2, debug, "match_type is invalid.\n");
  1258. return -EINVAL;
  1259. }
  1260. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1261. g_chip_ident, chip);
  1262. }
  1263. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1264. /*
  1265. * vpif_dbg_g_register() - Read register
  1266. * @file: file ptr
  1267. * @priv: file handle
  1268. * @reg: register to be read
  1269. *
  1270. * Debugging only
  1271. * Returns zero or -EINVAL if read operations fails.
  1272. */
  1273. static int vpif_dbg_g_register(struct file *file, void *priv,
  1274. struct v4l2_dbg_register *reg){
  1275. struct vpif_fh *fh = priv;
  1276. struct channel_obj *ch = fh->channel;
  1277. struct video_obj *vid_ch = &ch->video;
  1278. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1279. g_register, reg);
  1280. }
  1281. /*
  1282. * vpif_dbg_s_register() - Write to register
  1283. * @file: file ptr
  1284. * @priv: file handle
  1285. * @reg: register to be modified
  1286. *
  1287. * Debugging only
  1288. * Returns zero or -EINVAL if write operations fails.
  1289. */
  1290. static int vpif_dbg_s_register(struct file *file, void *priv,
  1291. struct v4l2_dbg_register *reg){
  1292. struct vpif_fh *fh = priv;
  1293. struct channel_obj *ch = fh->channel;
  1294. struct video_obj *vid_ch = &ch->video;
  1295. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1296. s_register, reg);
  1297. }
  1298. #endif
  1299. /*
  1300. * vpif_log_status() - Status information
  1301. * @file: file ptr
  1302. * @priv: file handle
  1303. *
  1304. * Returns zero.
  1305. */
  1306. static int vpif_log_status(struct file *filep, void *priv)
  1307. {
  1308. /* status for sub devices */
  1309. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1310. return 0;
  1311. }
  1312. /* vpif display ioctl operations */
  1313. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1314. .vidioc_querycap = vpif_querycap,
  1315. .vidioc_g_priority = vpif_g_priority,
  1316. .vidioc_s_priority = vpif_s_priority,
  1317. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1318. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1319. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1320. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1321. .vidioc_reqbufs = vpif_reqbufs,
  1322. .vidioc_querybuf = vpif_querybuf,
  1323. .vidioc_qbuf = vpif_qbuf,
  1324. .vidioc_dqbuf = vpif_dqbuf,
  1325. .vidioc_streamon = vpif_streamon,
  1326. .vidioc_streamoff = vpif_streamoff,
  1327. .vidioc_s_std = vpif_s_std,
  1328. .vidioc_g_std = vpif_g_std,
  1329. .vidioc_enum_output = vpif_enum_output,
  1330. .vidioc_s_output = vpif_s_output,
  1331. .vidioc_g_output = vpif_g_output,
  1332. .vidioc_cropcap = vpif_cropcap,
  1333. .vidioc_enum_dv_presets = vpif_enum_dv_presets,
  1334. .vidioc_s_dv_preset = vpif_s_dv_preset,
  1335. .vidioc_g_dv_preset = vpif_g_dv_preset,
  1336. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1337. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1338. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1339. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1340. .vidioc_g_register = vpif_dbg_g_register,
  1341. .vidioc_s_register = vpif_dbg_s_register,
  1342. #endif
  1343. .vidioc_log_status = vpif_log_status,
  1344. };
  1345. static const struct v4l2_file_operations vpif_fops = {
  1346. .owner = THIS_MODULE,
  1347. .open = vpif_open,
  1348. .release = vpif_release,
  1349. .unlocked_ioctl = video_ioctl2,
  1350. .mmap = vpif_mmap,
  1351. .poll = vpif_poll
  1352. };
  1353. static struct video_device vpif_video_template = {
  1354. .name = "vpif",
  1355. .fops = &vpif_fops,
  1356. .ioctl_ops = &vpif_ioctl_ops,
  1357. .tvnorms = VPIF_V4L2_STD,
  1358. .current_norm = V4L2_STD_625_50,
  1359. };
  1360. /*Configure the channels, buffer sizei, request irq */
  1361. static int initialize_vpif(void)
  1362. {
  1363. int free_channel_objects_index;
  1364. int free_buffer_channel_index;
  1365. int free_buffer_index;
  1366. int err = 0, i, j;
  1367. /* Default number of buffers should be 3 */
  1368. if ((ch2_numbuffers > 0) &&
  1369. (ch2_numbuffers < config_params.min_numbuffers))
  1370. ch2_numbuffers = config_params.min_numbuffers;
  1371. if ((ch3_numbuffers > 0) &&
  1372. (ch3_numbuffers < config_params.min_numbuffers))
  1373. ch3_numbuffers = config_params.min_numbuffers;
  1374. /* Set buffer size to min buffers size if invalid buffer size is
  1375. * given */
  1376. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1377. ch2_bufsize =
  1378. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1379. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1380. ch3_bufsize =
  1381. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1382. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1383. if (ch2_numbuffers) {
  1384. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1385. ch2_bufsize;
  1386. }
  1387. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1388. if (ch3_numbuffers) {
  1389. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1390. ch3_bufsize;
  1391. }
  1392. /* Allocate memory for six channel objects */
  1393. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1394. vpif_obj.dev[i] =
  1395. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1396. /* If memory allocation fails, return error */
  1397. if (!vpif_obj.dev[i]) {
  1398. free_channel_objects_index = i;
  1399. err = -ENOMEM;
  1400. goto vpif_init_free_channel_objects;
  1401. }
  1402. }
  1403. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1404. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1405. free_buffer_index = config_params.numbuffers[i - 1];
  1406. return 0;
  1407. vpif_init_free_channel_objects:
  1408. for (j = 0; j < free_channel_objects_index; j++)
  1409. kfree(vpif_obj.dev[j]);
  1410. return err;
  1411. }
  1412. /*
  1413. * vpif_probe: This function creates device entries by register itself to the
  1414. * V4L2 driver and initializes fields of each channel objects
  1415. */
  1416. static __init int vpif_probe(struct platform_device *pdev)
  1417. {
  1418. struct vpif_subdev_info *subdevdata;
  1419. struct vpif_display_config *config;
  1420. int i, j = 0, k, q, m, err = 0;
  1421. struct i2c_adapter *i2c_adap;
  1422. struct common_obj *common;
  1423. struct channel_obj *ch;
  1424. struct video_device *vfd;
  1425. struct resource *res;
  1426. int subdev_count;
  1427. size_t size;
  1428. vpif_dev = &pdev->dev;
  1429. err = initialize_vpif();
  1430. if (err) {
  1431. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1432. return err;
  1433. }
  1434. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1435. if (err) {
  1436. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1437. return err;
  1438. }
  1439. k = 0;
  1440. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1441. for (i = res->start; i <= res->end; i++) {
  1442. if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
  1443. "VPIF_Display",
  1444. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1445. err = -EBUSY;
  1446. goto vpif_int_err;
  1447. }
  1448. }
  1449. k++;
  1450. }
  1451. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1452. /* Get the pointer to the channel object */
  1453. ch = vpif_obj.dev[i];
  1454. /* Allocate memory for video device */
  1455. vfd = video_device_alloc();
  1456. if (vfd == NULL) {
  1457. for (j = 0; j < i; j++) {
  1458. ch = vpif_obj.dev[j];
  1459. video_device_release(ch->video_dev);
  1460. }
  1461. err = -ENOMEM;
  1462. goto vpif_int_err;
  1463. }
  1464. /* Initialize field of video device */
  1465. *vfd = vpif_video_template;
  1466. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1467. vfd->release = video_device_release;
  1468. snprintf(vfd->name, sizeof(vfd->name),
  1469. "VPIF_Display_DRIVER_V%s",
  1470. VPIF_DISPLAY_VERSION);
  1471. /* Set video_dev to the video device */
  1472. ch->video_dev = vfd;
  1473. }
  1474. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1475. if (res) {
  1476. size = resource_size(res);
  1477. /* The resources are divided into two equal memory and when
  1478. * we have HD output we can add them together
  1479. */
  1480. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1481. ch = vpif_obj.dev[j];
  1482. ch->channel_id = j;
  1483. /* only enabled if second resource exists */
  1484. config_params.video_limit[ch->channel_id] = 0;
  1485. if (size)
  1486. config_params.video_limit[ch->channel_id] =
  1487. size/2;
  1488. }
  1489. }
  1490. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1491. ch = vpif_obj.dev[j];
  1492. /* Initialize field of the channel objects */
  1493. atomic_set(&ch->usrs, 0);
  1494. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1495. ch->common[k].numbuffers = 0;
  1496. common = &ch->common[k];
  1497. common->io_usrs = 0;
  1498. common->started = 0;
  1499. spin_lock_init(&common->irqlock);
  1500. mutex_init(&common->lock);
  1501. common->numbuffers = 0;
  1502. common->set_addr = NULL;
  1503. common->ytop_off = common->ybtm_off = 0;
  1504. common->ctop_off = common->cbtm_off = 0;
  1505. common->cur_frm = common->next_frm = NULL;
  1506. memset(&common->fmt, 0, sizeof(common->fmt));
  1507. common->numbuffers = config_params.numbuffers[k];
  1508. }
  1509. ch->initialized = 0;
  1510. ch->channel_id = j;
  1511. if (j < 2)
  1512. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1513. config_params.numbuffers[ch->channel_id];
  1514. else
  1515. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1516. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1517. /* Initialize prio member of channel object */
  1518. v4l2_prio_init(&ch->prio);
  1519. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1520. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1521. /* Locking in file operations other than ioctl should be done
  1522. by the driver, not the V4L2 core.
  1523. This driver needs auditing so that this flag can be removed. */
  1524. set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
  1525. ch->video_dev->lock = &common->lock;
  1526. /* register video device */
  1527. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1528. (int)ch, (int)&ch->video_dev);
  1529. err = video_register_device(ch->video_dev,
  1530. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1531. if (err < 0)
  1532. goto probe_out;
  1533. video_set_drvdata(ch->video_dev, ch);
  1534. }
  1535. i2c_adap = i2c_get_adapter(1);
  1536. config = pdev->dev.platform_data;
  1537. subdev_count = config->subdev_count;
  1538. subdevdata = config->subdevinfo;
  1539. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1540. GFP_KERNEL);
  1541. if (vpif_obj.sd == NULL) {
  1542. vpif_err("unable to allocate memory for subdevice pointers\n");
  1543. err = -ENOMEM;
  1544. goto probe_out;
  1545. }
  1546. for (i = 0; i < subdev_count; i++) {
  1547. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1548. i2c_adap,
  1549. &subdevdata[i].board_info,
  1550. NULL);
  1551. if (!vpif_obj.sd[i]) {
  1552. vpif_err("Error registering v4l2 subdevice\n");
  1553. goto probe_subdev_out;
  1554. }
  1555. if (vpif_obj.sd[i])
  1556. vpif_obj.sd[i]->grp_id = 1 << i;
  1557. }
  1558. v4l2_info(&vpif_obj.v4l2_dev,
  1559. " VPIF display driver initialized\n");
  1560. return 0;
  1561. probe_subdev_out:
  1562. kfree(vpif_obj.sd);
  1563. probe_out:
  1564. for (k = 0; k < j; k++) {
  1565. ch = vpif_obj.dev[k];
  1566. video_unregister_device(ch->video_dev);
  1567. video_device_release(ch->video_dev);
  1568. ch->video_dev = NULL;
  1569. }
  1570. vpif_int_err:
  1571. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1572. vpif_err("VPIF IRQ request failed\n");
  1573. for (q = k; k >= 0; k--) {
  1574. for (m = i; m >= res->start; m--)
  1575. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1576. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1577. m = res->end;
  1578. }
  1579. return err;
  1580. }
  1581. /*
  1582. * vpif_remove: It un-register channels from V4L2 driver
  1583. */
  1584. static int vpif_remove(struct platform_device *device)
  1585. {
  1586. struct channel_obj *ch;
  1587. int i;
  1588. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1589. /* un-register device */
  1590. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1591. /* Get the pointer to the channel object */
  1592. ch = vpif_obj.dev[i];
  1593. /* Unregister video device */
  1594. video_unregister_device(ch->video_dev);
  1595. ch->video_dev = NULL;
  1596. }
  1597. return 0;
  1598. }
  1599. #ifdef CONFIG_PM
  1600. static int vpif_suspend(struct device *dev)
  1601. {
  1602. struct common_obj *common;
  1603. struct channel_obj *ch;
  1604. int i;
  1605. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1606. /* Get the pointer to the channel object */
  1607. ch = vpif_obj.dev[i];
  1608. common = &ch->common[VPIF_VIDEO_INDEX];
  1609. mutex_lock(&common->lock);
  1610. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1611. /* Disable channel */
  1612. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1613. enable_channel2(0);
  1614. channel2_intr_enable(0);
  1615. }
  1616. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1617. common->started == 2) {
  1618. enable_channel3(0);
  1619. channel3_intr_enable(0);
  1620. }
  1621. }
  1622. mutex_unlock(&common->lock);
  1623. }
  1624. return 0;
  1625. }
  1626. static int vpif_resume(struct device *dev)
  1627. {
  1628. struct common_obj *common;
  1629. struct channel_obj *ch;
  1630. int i;
  1631. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1632. /* Get the pointer to the channel object */
  1633. ch = vpif_obj.dev[i];
  1634. common = &ch->common[VPIF_VIDEO_INDEX];
  1635. mutex_lock(&common->lock);
  1636. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1637. /* Enable channel */
  1638. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1639. enable_channel2(1);
  1640. channel2_intr_enable(1);
  1641. }
  1642. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1643. common->started == 2) {
  1644. enable_channel3(1);
  1645. channel3_intr_enable(1);
  1646. }
  1647. }
  1648. mutex_unlock(&common->lock);
  1649. }
  1650. return 0;
  1651. }
  1652. static const struct dev_pm_ops vpif_pm = {
  1653. .suspend = vpif_suspend,
  1654. .resume = vpif_resume,
  1655. };
  1656. #define vpif_pm_ops (&vpif_pm)
  1657. #else
  1658. #define vpif_pm_ops NULL
  1659. #endif
  1660. static __refdata struct platform_driver vpif_driver = {
  1661. .driver = {
  1662. .name = "vpif_display",
  1663. .owner = THIS_MODULE,
  1664. .pm = vpif_pm_ops,
  1665. },
  1666. .probe = vpif_probe,
  1667. .remove = vpif_remove,
  1668. };
  1669. static __init int vpif_init(void)
  1670. {
  1671. return platform_driver_register(&vpif_driver);
  1672. }
  1673. /*
  1674. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1675. * frees requested irq handler and de-allocates memory allocated for channel
  1676. * objects.
  1677. */
  1678. static void vpif_cleanup(void)
  1679. {
  1680. struct platform_device *pdev;
  1681. struct resource *res;
  1682. int irq_num;
  1683. int i = 0;
  1684. pdev = container_of(vpif_dev, struct platform_device, dev);
  1685. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1686. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1687. free_irq(irq_num,
  1688. (void *)(&vpif_obj.dev[i]->channel_id));
  1689. i++;
  1690. }
  1691. platform_driver_unregister(&vpif_driver);
  1692. kfree(vpif_obj.sd);
  1693. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1694. kfree(vpif_obj.dev[i]);
  1695. }
  1696. module_init(vpif_init);
  1697. module_exit(vpif_cleanup);