vpif_display.c 53 KB

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