vpif_display.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  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. int ret;
  594. vpif_dbg(2, debug, "vpif_mmap\n");
  595. if (mutex_lock_interruptible(&common->lock))
  596. return -ERESTARTSYS;
  597. ret = vb2_mmap(&common->buffer_queue, vma);
  598. mutex_unlock(&common->lock);
  599. return ret;
  600. }
  601. /*
  602. * vpif_poll: It is used for select/poll system call
  603. */
  604. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  605. {
  606. struct vpif_fh *fh = filep->private_data;
  607. struct channel_obj *ch = fh->channel;
  608. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  609. unsigned int res = 0;
  610. if (common->started) {
  611. mutex_lock(&common->lock);
  612. res = vb2_poll(&common->buffer_queue, filep, wait);
  613. mutex_unlock(&common->lock);
  614. }
  615. return res;
  616. }
  617. /*
  618. * vpif_open: It creates object of file handle structure and stores it in
  619. * private_data member of filepointer
  620. */
  621. static int vpif_open(struct file *filep)
  622. {
  623. struct video_device *vdev = video_devdata(filep);
  624. struct channel_obj *ch = video_get_drvdata(vdev);
  625. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  626. struct vpif_fh *fh;
  627. /* Allocate memory for the file handle object */
  628. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  629. if (fh == NULL) {
  630. vpif_err("unable to allocate memory for file handle object\n");
  631. return -ENOMEM;
  632. }
  633. if (mutex_lock_interruptible(&common->lock)) {
  634. kfree(fh);
  635. return -ERESTARTSYS;
  636. }
  637. /* store pointer to fh in private_data member of filep */
  638. filep->private_data = fh;
  639. fh->channel = ch;
  640. fh->initialized = 0;
  641. if (!ch->initialized) {
  642. fh->initialized = 1;
  643. ch->initialized = 1;
  644. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  645. }
  646. /* Increment channel usrs counter */
  647. atomic_inc(&ch->usrs);
  648. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  649. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  650. /* Initialize priority of this instance to default priority */
  651. fh->prio = V4L2_PRIORITY_UNSET;
  652. v4l2_prio_open(&ch->prio, &fh->prio);
  653. mutex_unlock(&common->lock);
  654. return 0;
  655. }
  656. /*
  657. * vpif_release: This function deletes buffer queue, frees the buffers and
  658. * the vpif file handle
  659. */
  660. static int vpif_release(struct file *filep)
  661. {
  662. struct vpif_fh *fh = filep->private_data;
  663. struct channel_obj *ch = fh->channel;
  664. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  665. mutex_lock(&common->lock);
  666. /* if this instance is doing IO */
  667. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  668. /* Reset io_usrs member of channel object */
  669. common->io_usrs = 0;
  670. /* Disable channel */
  671. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  672. enable_channel2(0);
  673. channel2_intr_enable(0);
  674. }
  675. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  676. (2 == common->started)) {
  677. enable_channel3(0);
  678. channel3_intr_enable(0);
  679. }
  680. common->started = 0;
  681. /* Free buffers allocated */
  682. vb2_queue_release(&common->buffer_queue);
  683. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  684. common->numbuffers =
  685. config_params.numbuffers[ch->channel_id];
  686. }
  687. /* Decrement channel usrs counter */
  688. atomic_dec(&ch->usrs);
  689. /* If this file handle has initialize encoder device, reset it */
  690. if (fh->initialized)
  691. ch->initialized = 0;
  692. /* Close the priority */
  693. v4l2_prio_close(&ch->prio, fh->prio);
  694. filep->private_data = NULL;
  695. fh->initialized = 0;
  696. mutex_unlock(&common->lock);
  697. kfree(fh);
  698. return 0;
  699. }
  700. /* functions implementing ioctls */
  701. /**
  702. * vpif_querycap() - QUERYCAP handler
  703. * @file: file ptr
  704. * @priv: file handle
  705. * @cap: ptr to v4l2_capability structure
  706. */
  707. static int vpif_querycap(struct file *file, void *priv,
  708. struct v4l2_capability *cap)
  709. {
  710. struct vpif_display_config *config = vpif_dev->platform_data;
  711. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  712. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  713. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  714. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  715. return 0;
  716. }
  717. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  718. struct v4l2_fmtdesc *fmt)
  719. {
  720. if (fmt->index != 0) {
  721. vpif_err("Invalid format index\n");
  722. return -EINVAL;
  723. }
  724. /* Fill in the information about format */
  725. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  726. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  727. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  728. return 0;
  729. }
  730. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  731. struct v4l2_format *fmt)
  732. {
  733. struct vpif_fh *fh = priv;
  734. struct channel_obj *ch = fh->channel;
  735. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  736. /* Check the validity of the buffer type */
  737. if (common->fmt.type != fmt->type)
  738. return -EINVAL;
  739. if (vpif_update_resolution(ch))
  740. return -EINVAL;
  741. *fmt = common->fmt;
  742. return 0;
  743. }
  744. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  745. struct v4l2_format *fmt)
  746. {
  747. struct vpif_fh *fh = priv;
  748. struct v4l2_pix_format *pixfmt;
  749. struct channel_obj *ch = fh->channel;
  750. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  751. int ret = 0;
  752. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  753. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  754. if (!fh->initialized) {
  755. vpif_dbg(1, debug, "Channel Busy\n");
  756. return -EBUSY;
  757. }
  758. /* Check for the priority */
  759. ret = v4l2_prio_check(&ch->prio, fh->prio);
  760. if (0 != ret)
  761. return ret;
  762. fh->initialized = 1;
  763. }
  764. if (common->started) {
  765. vpif_dbg(1, debug, "Streaming in progress\n");
  766. return -EBUSY;
  767. }
  768. pixfmt = &fmt->fmt.pix;
  769. /* Check for valid field format */
  770. ret = vpif_check_format(ch, pixfmt);
  771. if (ret)
  772. return ret;
  773. /* store the pix format in the channel object */
  774. common->fmt.fmt.pix = *pixfmt;
  775. /* store the format in the channel object */
  776. common->fmt = *fmt;
  777. return 0;
  778. }
  779. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  780. struct v4l2_format *fmt)
  781. {
  782. struct vpif_fh *fh = priv;
  783. struct channel_obj *ch = fh->channel;
  784. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  785. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  786. int ret = 0;
  787. ret = vpif_check_format(ch, pixfmt);
  788. if (ret) {
  789. *pixfmt = common->fmt.fmt.pix;
  790. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  791. }
  792. return ret;
  793. }
  794. static int vpif_reqbufs(struct file *file, void *priv,
  795. struct v4l2_requestbuffers *reqbuf)
  796. {
  797. struct vpif_fh *fh = priv;
  798. struct channel_obj *ch = fh->channel;
  799. struct common_obj *common;
  800. enum v4l2_field field;
  801. struct vb2_queue *q;
  802. u8 index = 0;
  803. /* This file handle has not initialized the channel,
  804. It is not allowed to do settings */
  805. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  806. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  807. if (!fh->initialized) {
  808. vpif_err("Channel Busy\n");
  809. return -EBUSY;
  810. }
  811. }
  812. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  813. return -EINVAL;
  814. index = VPIF_VIDEO_INDEX;
  815. common = &ch->common[index];
  816. if (common->fmt.type != reqbuf->type || !vpif_dev)
  817. return -EINVAL;
  818. if (0 != common->io_usrs)
  819. return -EBUSY;
  820. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  821. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  822. field = V4L2_FIELD_INTERLACED;
  823. else
  824. field = common->fmt.fmt.pix.field;
  825. } else {
  826. field = V4L2_VBI_INTERLACED;
  827. }
  828. /* Initialize videobuf2 queue as per the buffer type */
  829. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  830. if (!common->alloc_ctx) {
  831. vpif_err("Failed to get the context\n");
  832. return -EINVAL;
  833. }
  834. q = &common->buffer_queue;
  835. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  836. q->io_modes = VB2_MMAP | VB2_USERPTR;
  837. q->drv_priv = fh;
  838. q->ops = &video_qops;
  839. q->mem_ops = &vb2_dma_contig_memops;
  840. q->buf_struct_size = sizeof(struct vpif_disp_buffer);
  841. vb2_queue_init(q);
  842. /* Set io allowed member of file handle to TRUE */
  843. fh->io_allowed[index] = 1;
  844. /* Increment io usrs member of channel object to 1 */
  845. common->io_usrs = 1;
  846. /* Store type of memory requested in channel object */
  847. common->memory = reqbuf->memory;
  848. INIT_LIST_HEAD(&common->dma_queue);
  849. /* Allocate buffers */
  850. return vb2_reqbufs(&common->buffer_queue, reqbuf);
  851. }
  852. static int vpif_querybuf(struct file *file, void *priv,
  853. struct v4l2_buffer *tbuf)
  854. {
  855. struct vpif_fh *fh = priv;
  856. struct channel_obj *ch = fh->channel;
  857. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  858. if (common->fmt.type != tbuf->type)
  859. return -EINVAL;
  860. return vb2_querybuf(&common->buffer_queue, tbuf);
  861. }
  862. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  863. {
  864. struct vpif_fh *fh = NULL;
  865. struct channel_obj *ch = NULL;
  866. struct common_obj *common = NULL;
  867. if (!buf || !priv)
  868. return -EINVAL;
  869. fh = priv;
  870. ch = fh->channel;
  871. if (!ch)
  872. return -EINVAL;
  873. common = &(ch->common[VPIF_VIDEO_INDEX]);
  874. if (common->fmt.type != buf->type)
  875. return -EINVAL;
  876. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  877. vpif_err("fh->io_allowed\n");
  878. return -EACCES;
  879. }
  880. return vb2_qbuf(&common->buffer_queue, buf);
  881. }
  882. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  883. {
  884. struct vpif_fh *fh = priv;
  885. struct channel_obj *ch = fh->channel;
  886. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  887. int ret = 0;
  888. if (!(*std_id & VPIF_V4L2_STD))
  889. return -EINVAL;
  890. if (common->started) {
  891. vpif_err("streaming in progress\n");
  892. return -EBUSY;
  893. }
  894. /* Call encoder subdevice function to set the standard */
  895. ch->video.stdid = *std_id;
  896. ch->video.dv_preset = V4L2_DV_INVALID;
  897. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_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->ch2_clip_en)
  1001. channel2_clipping_enable(0);
  1002. enable_channel2(0);
  1003. channel2_intr_enable(0);
  1004. }
  1005. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  1006. (2 == common->started)) {
  1007. if (vpif_config_data->ch3_clip_en)
  1008. channel3_clipping_enable(0);
  1009. enable_channel3(0);
  1010. channel3_intr_enable(0);
  1011. }
  1012. }
  1013. common->started = 0;
  1014. return vb2_streamoff(&common->buffer_queue, buftype);
  1015. }
  1016. static int vpif_cropcap(struct file *file, void *priv,
  1017. struct v4l2_cropcap *crop)
  1018. {
  1019. struct vpif_fh *fh = priv;
  1020. struct channel_obj *ch = fh->channel;
  1021. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1022. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1023. return -EINVAL;
  1024. crop->bounds.left = crop->bounds.top = 0;
  1025. crop->defrect.left = crop->defrect.top = 0;
  1026. crop->defrect.height = crop->bounds.height = common->height;
  1027. crop->defrect.width = crop->bounds.width = common->width;
  1028. return 0;
  1029. }
  1030. static int vpif_enum_output(struct file *file, void *fh,
  1031. struct v4l2_output *output)
  1032. {
  1033. struct vpif_display_config *config = vpif_dev->platform_data;
  1034. if (output->index >= config->output_count) {
  1035. vpif_dbg(1, debug, "Invalid output index\n");
  1036. return -EINVAL;
  1037. }
  1038. strcpy(output->name, config->output[output->index]);
  1039. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1040. output->std = VPIF_V4L2_STD;
  1041. return 0;
  1042. }
  1043. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1044. {
  1045. struct vpif_fh *fh = priv;
  1046. struct channel_obj *ch = fh->channel;
  1047. struct video_obj *vid_ch = &ch->video;
  1048. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1049. int ret = 0;
  1050. if (common->started) {
  1051. vpif_err("Streaming in progress\n");
  1052. return -EBUSY;
  1053. }
  1054. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1055. s_routing, 0, i, 0);
  1056. if (ret < 0)
  1057. vpif_err("Failed to set output standard\n");
  1058. vid_ch->output_id = i;
  1059. return ret;
  1060. }
  1061. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1062. {
  1063. struct vpif_fh *fh = priv;
  1064. struct channel_obj *ch = fh->channel;
  1065. struct video_obj *vid_ch = &ch->video;
  1066. *i = vid_ch->output_id;
  1067. return 0;
  1068. }
  1069. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1070. {
  1071. struct vpif_fh *fh = priv;
  1072. struct channel_obj *ch = fh->channel;
  1073. *p = v4l2_prio_max(&ch->prio);
  1074. return 0;
  1075. }
  1076. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1077. {
  1078. struct vpif_fh *fh = priv;
  1079. struct channel_obj *ch = fh->channel;
  1080. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1081. }
  1082. /**
  1083. * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
  1084. * @file: file ptr
  1085. * @priv: file handle
  1086. * @preset: input preset
  1087. */
  1088. static int vpif_enum_dv_presets(struct file *file, void *priv,
  1089. struct v4l2_dv_enum_preset *preset)
  1090. {
  1091. struct vpif_fh *fh = priv;
  1092. struct channel_obj *ch = fh->channel;
  1093. struct video_obj *vid_ch = &ch->video;
  1094. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1095. video, enum_dv_presets, preset);
  1096. }
  1097. /**
  1098. * vpif_s_dv_presets() - S_DV_PRESETS handler
  1099. * @file: file ptr
  1100. * @priv: file handle
  1101. * @preset: input preset
  1102. */
  1103. static int vpif_s_dv_preset(struct file *file, void *priv,
  1104. struct v4l2_dv_preset *preset)
  1105. {
  1106. struct vpif_fh *fh = priv;
  1107. struct channel_obj *ch = fh->channel;
  1108. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1109. struct video_obj *vid_ch = &ch->video;
  1110. int ret = 0;
  1111. if (common->started) {
  1112. vpif_dbg(1, debug, "streaming in progress\n");
  1113. return -EBUSY;
  1114. }
  1115. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1116. if (ret != 0)
  1117. return ret;
  1118. fh->initialized = 1;
  1119. /* Call encoder subdevice function to set the standard */
  1120. if (mutex_lock_interruptible(&common->lock))
  1121. return -ERESTARTSYS;
  1122. ch->video.dv_preset = preset->preset;
  1123. ch->video.stdid = V4L2_STD_UNKNOWN;
  1124. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1125. /* Get the information about the standard */
  1126. if (vpif_update_resolution(ch)) {
  1127. ret = -EINVAL;
  1128. } else {
  1129. /* Configure the default format information */
  1130. vpif_config_format(ch);
  1131. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1132. video, s_dv_preset, preset);
  1133. }
  1134. mutex_unlock(&common->lock);
  1135. return ret;
  1136. }
  1137. /**
  1138. * vpif_g_dv_presets() - G_DV_PRESETS handler
  1139. * @file: file ptr
  1140. * @priv: file handle
  1141. * @preset: input preset
  1142. */
  1143. static int vpif_g_dv_preset(struct file *file, void *priv,
  1144. struct v4l2_dv_preset *preset)
  1145. {
  1146. struct vpif_fh *fh = priv;
  1147. struct channel_obj *ch = fh->channel;
  1148. preset->preset = ch->video.dv_preset;
  1149. return 0;
  1150. }
  1151. /**
  1152. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1153. * @file: file ptr
  1154. * @priv: file handle
  1155. * @timings: digital video timings
  1156. */
  1157. static int vpif_s_dv_timings(struct file *file, void *priv,
  1158. struct v4l2_dv_timings *timings)
  1159. {
  1160. struct vpif_fh *fh = priv;
  1161. struct channel_obj *ch = fh->channel;
  1162. struct vpif_params *vpifparams = &ch->vpifparams;
  1163. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1164. struct video_obj *vid_ch = &ch->video;
  1165. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1166. int ret;
  1167. if (timings->type != V4L2_DV_BT_656_1120) {
  1168. vpif_dbg(2, debug, "Timing type not defined\n");
  1169. return -EINVAL;
  1170. }
  1171. /* Configure subdevice timings, if any */
  1172. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1173. video, s_dv_timings, timings);
  1174. if (ret == -ENOIOCTLCMD) {
  1175. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1176. "subdevice\n");
  1177. return -EINVAL;
  1178. }
  1179. if (ret < 0) {
  1180. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1181. return ret;
  1182. }
  1183. if (!(timings->bt.width && timings->bt.height &&
  1184. (timings->bt.hbackporch ||
  1185. timings->bt.hfrontporch ||
  1186. timings->bt.hsync) &&
  1187. timings->bt.vfrontporch &&
  1188. (timings->bt.vbackporch ||
  1189. timings->bt.vsync))) {
  1190. vpif_dbg(2, debug, "Timings for width, height, "
  1191. "horizontal back porch, horizontal sync, "
  1192. "horizontal front porch, vertical back porch, "
  1193. "vertical sync and vertical back porch "
  1194. "must be defined\n");
  1195. return -EINVAL;
  1196. }
  1197. *bt = timings->bt;
  1198. /* Configure video port timings */
  1199. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1200. bt->hsync - 8;
  1201. std_info->sav2eav = bt->width;
  1202. std_info->l1 = 1;
  1203. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1204. if (bt->interlaced) {
  1205. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1206. std_info->vsize = bt->height * 2 +
  1207. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1208. bt->il_vfrontporch + bt->il_vsync +
  1209. bt->il_vbackporch;
  1210. std_info->l5 = std_info->vsize/2 -
  1211. (bt->vfrontporch - 1);
  1212. std_info->l7 = std_info->vsize/2 + 1;
  1213. std_info->l9 = std_info->l7 + bt->il_vsync +
  1214. bt->il_vbackporch + 1;
  1215. std_info->l11 = std_info->vsize -
  1216. (bt->il_vfrontporch - 1);
  1217. } else {
  1218. vpif_dbg(2, debug, "Required timing values for "
  1219. "interlaced BT format missing\n");
  1220. return -EINVAL;
  1221. }
  1222. } else {
  1223. std_info->vsize = bt->height + bt->vfrontporch +
  1224. bt->vsync + bt->vbackporch;
  1225. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1226. }
  1227. strncpy(std_info->name, "Custom timings BT656/1120",
  1228. VPIF_MAX_NAME);
  1229. std_info->width = bt->width;
  1230. std_info->height = bt->height;
  1231. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1232. std_info->ycmux_mode = 0;
  1233. std_info->capture_format = 0;
  1234. std_info->vbi_supported = 0;
  1235. std_info->hd_sd = 1;
  1236. std_info->stdid = 0;
  1237. std_info->dv_preset = V4L2_DV_INVALID;
  1238. vid_ch->stdid = 0;
  1239. vid_ch->dv_preset = V4L2_DV_INVALID;
  1240. return 0;
  1241. }
  1242. /**
  1243. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1244. * @file: file ptr
  1245. * @priv: file handle
  1246. * @timings: digital video timings
  1247. */
  1248. static int vpif_g_dv_timings(struct file *file, void *priv,
  1249. struct v4l2_dv_timings *timings)
  1250. {
  1251. struct vpif_fh *fh = priv;
  1252. struct channel_obj *ch = fh->channel;
  1253. struct video_obj *vid_ch = &ch->video;
  1254. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1255. timings->bt = *bt;
  1256. return 0;
  1257. }
  1258. /*
  1259. * vpif_g_chip_ident() - Identify the chip
  1260. * @file: file ptr
  1261. * @priv: file handle
  1262. * @chip: chip identity
  1263. *
  1264. * Returns zero or -EINVAL if read operations fails.
  1265. */
  1266. static int vpif_g_chip_ident(struct file *file, void *priv,
  1267. struct v4l2_dbg_chip_ident *chip)
  1268. {
  1269. chip->ident = V4L2_IDENT_NONE;
  1270. chip->revision = 0;
  1271. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1272. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1273. vpif_dbg(2, debug, "match_type is invalid.\n");
  1274. return -EINVAL;
  1275. }
  1276. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1277. g_chip_ident, chip);
  1278. }
  1279. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1280. /*
  1281. * vpif_dbg_g_register() - Read register
  1282. * @file: file ptr
  1283. * @priv: file handle
  1284. * @reg: register to be read
  1285. *
  1286. * Debugging only
  1287. * Returns zero or -EINVAL if read operations fails.
  1288. */
  1289. static int vpif_dbg_g_register(struct file *file, void *priv,
  1290. struct v4l2_dbg_register *reg){
  1291. struct vpif_fh *fh = priv;
  1292. struct channel_obj *ch = fh->channel;
  1293. struct video_obj *vid_ch = &ch->video;
  1294. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1295. g_register, reg);
  1296. }
  1297. /*
  1298. * vpif_dbg_s_register() - Write to register
  1299. * @file: file ptr
  1300. * @priv: file handle
  1301. * @reg: register to be modified
  1302. *
  1303. * Debugging only
  1304. * Returns zero or -EINVAL if write operations fails.
  1305. */
  1306. static int vpif_dbg_s_register(struct file *file, void *priv,
  1307. struct v4l2_dbg_register *reg){
  1308. struct vpif_fh *fh = priv;
  1309. struct channel_obj *ch = fh->channel;
  1310. struct video_obj *vid_ch = &ch->video;
  1311. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1312. s_register, reg);
  1313. }
  1314. #endif
  1315. /*
  1316. * vpif_log_status() - Status information
  1317. * @file: file ptr
  1318. * @priv: file handle
  1319. *
  1320. * Returns zero.
  1321. */
  1322. static int vpif_log_status(struct file *filep, void *priv)
  1323. {
  1324. /* status for sub devices */
  1325. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1326. return 0;
  1327. }
  1328. /* vpif display ioctl operations */
  1329. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1330. .vidioc_querycap = vpif_querycap,
  1331. .vidioc_g_priority = vpif_g_priority,
  1332. .vidioc_s_priority = vpif_s_priority,
  1333. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1334. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1335. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1336. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1337. .vidioc_reqbufs = vpif_reqbufs,
  1338. .vidioc_querybuf = vpif_querybuf,
  1339. .vidioc_qbuf = vpif_qbuf,
  1340. .vidioc_dqbuf = vpif_dqbuf,
  1341. .vidioc_streamon = vpif_streamon,
  1342. .vidioc_streamoff = vpif_streamoff,
  1343. .vidioc_s_std = vpif_s_std,
  1344. .vidioc_g_std = vpif_g_std,
  1345. .vidioc_enum_output = vpif_enum_output,
  1346. .vidioc_s_output = vpif_s_output,
  1347. .vidioc_g_output = vpif_g_output,
  1348. .vidioc_cropcap = vpif_cropcap,
  1349. .vidioc_enum_dv_presets = vpif_enum_dv_presets,
  1350. .vidioc_s_dv_preset = vpif_s_dv_preset,
  1351. .vidioc_g_dv_preset = vpif_g_dv_preset,
  1352. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1353. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1354. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1355. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1356. .vidioc_g_register = vpif_dbg_g_register,
  1357. .vidioc_s_register = vpif_dbg_s_register,
  1358. #endif
  1359. .vidioc_log_status = vpif_log_status,
  1360. };
  1361. static const struct v4l2_file_operations vpif_fops = {
  1362. .owner = THIS_MODULE,
  1363. .open = vpif_open,
  1364. .release = vpif_release,
  1365. .unlocked_ioctl = video_ioctl2,
  1366. .mmap = vpif_mmap,
  1367. .poll = vpif_poll
  1368. };
  1369. static struct video_device vpif_video_template = {
  1370. .name = "vpif",
  1371. .fops = &vpif_fops,
  1372. .ioctl_ops = &vpif_ioctl_ops,
  1373. .tvnorms = VPIF_V4L2_STD,
  1374. .current_norm = V4L2_STD_625_50,
  1375. };
  1376. /*Configure the channels, buffer sizei, request irq */
  1377. static int initialize_vpif(void)
  1378. {
  1379. int free_channel_objects_index;
  1380. int free_buffer_channel_index;
  1381. int free_buffer_index;
  1382. int err = 0, i, j;
  1383. /* Default number of buffers should be 3 */
  1384. if ((ch2_numbuffers > 0) &&
  1385. (ch2_numbuffers < config_params.min_numbuffers))
  1386. ch2_numbuffers = config_params.min_numbuffers;
  1387. if ((ch3_numbuffers > 0) &&
  1388. (ch3_numbuffers < config_params.min_numbuffers))
  1389. ch3_numbuffers = config_params.min_numbuffers;
  1390. /* Set buffer size to min buffers size if invalid buffer size is
  1391. * given */
  1392. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1393. ch2_bufsize =
  1394. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1395. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1396. ch3_bufsize =
  1397. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1398. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1399. if (ch2_numbuffers) {
  1400. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1401. ch2_bufsize;
  1402. }
  1403. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1404. if (ch3_numbuffers) {
  1405. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1406. ch3_bufsize;
  1407. }
  1408. /* Allocate memory for six channel objects */
  1409. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1410. vpif_obj.dev[i] =
  1411. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1412. /* If memory allocation fails, return error */
  1413. if (!vpif_obj.dev[i]) {
  1414. free_channel_objects_index = i;
  1415. err = -ENOMEM;
  1416. goto vpif_init_free_channel_objects;
  1417. }
  1418. }
  1419. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1420. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1421. free_buffer_index = config_params.numbuffers[i - 1];
  1422. return 0;
  1423. vpif_init_free_channel_objects:
  1424. for (j = 0; j < free_channel_objects_index; j++)
  1425. kfree(vpif_obj.dev[j]);
  1426. return err;
  1427. }
  1428. /*
  1429. * vpif_probe: This function creates device entries by register itself to the
  1430. * V4L2 driver and initializes fields of each channel objects
  1431. */
  1432. static __init int vpif_probe(struct platform_device *pdev)
  1433. {
  1434. struct vpif_subdev_info *subdevdata;
  1435. struct vpif_display_config *config;
  1436. int i, j = 0, k, q, m, err = 0;
  1437. struct i2c_adapter *i2c_adap;
  1438. struct common_obj *common;
  1439. struct channel_obj *ch;
  1440. struct video_device *vfd;
  1441. struct resource *res;
  1442. int subdev_count;
  1443. size_t size;
  1444. vpif_dev = &pdev->dev;
  1445. err = initialize_vpif();
  1446. if (err) {
  1447. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1448. return err;
  1449. }
  1450. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1451. if (err) {
  1452. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1453. return err;
  1454. }
  1455. k = 0;
  1456. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1457. for (i = res->start; i <= res->end; i++) {
  1458. if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
  1459. "VPIF_Display",
  1460. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1461. err = -EBUSY;
  1462. goto vpif_int_err;
  1463. }
  1464. }
  1465. k++;
  1466. }
  1467. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1468. /* Get the pointer to the channel object */
  1469. ch = vpif_obj.dev[i];
  1470. /* Allocate memory for video device */
  1471. vfd = video_device_alloc();
  1472. if (vfd == NULL) {
  1473. for (j = 0; j < i; j++) {
  1474. ch = vpif_obj.dev[j];
  1475. video_device_release(ch->video_dev);
  1476. }
  1477. err = -ENOMEM;
  1478. goto vpif_int_err;
  1479. }
  1480. /* Initialize field of video device */
  1481. *vfd = vpif_video_template;
  1482. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1483. vfd->release = video_device_release;
  1484. snprintf(vfd->name, sizeof(vfd->name),
  1485. "VPIF_Display_DRIVER_V%s",
  1486. VPIF_DISPLAY_VERSION);
  1487. /* Set video_dev to the video device */
  1488. ch->video_dev = vfd;
  1489. }
  1490. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1491. if (res) {
  1492. size = resource_size(res);
  1493. /* The resources are divided into two equal memory and when
  1494. * we have HD output we can add them together
  1495. */
  1496. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1497. ch = vpif_obj.dev[j];
  1498. ch->channel_id = j;
  1499. /* only enabled if second resource exists */
  1500. config_params.video_limit[ch->channel_id] = 0;
  1501. if (size)
  1502. config_params.video_limit[ch->channel_id] =
  1503. size/2;
  1504. }
  1505. }
  1506. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1507. ch = vpif_obj.dev[j];
  1508. /* Initialize field of the channel objects */
  1509. atomic_set(&ch->usrs, 0);
  1510. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1511. ch->common[k].numbuffers = 0;
  1512. common = &ch->common[k];
  1513. common->io_usrs = 0;
  1514. common->started = 0;
  1515. spin_lock_init(&common->irqlock);
  1516. mutex_init(&common->lock);
  1517. common->numbuffers = 0;
  1518. common->set_addr = NULL;
  1519. common->ytop_off = common->ybtm_off = 0;
  1520. common->ctop_off = common->cbtm_off = 0;
  1521. common->cur_frm = common->next_frm = NULL;
  1522. memset(&common->fmt, 0, sizeof(common->fmt));
  1523. common->numbuffers = config_params.numbuffers[k];
  1524. }
  1525. ch->initialized = 0;
  1526. ch->channel_id = j;
  1527. if (j < 2)
  1528. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1529. config_params.numbuffers[ch->channel_id];
  1530. else
  1531. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1532. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1533. /* Initialize prio member of channel object */
  1534. v4l2_prio_init(&ch->prio);
  1535. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1536. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1537. ch->video_dev->lock = &common->lock;
  1538. /* register video device */
  1539. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1540. (int)ch, (int)&ch->video_dev);
  1541. err = video_register_device(ch->video_dev,
  1542. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1543. if (err < 0)
  1544. goto probe_out;
  1545. video_set_drvdata(ch->video_dev, ch);
  1546. }
  1547. i2c_adap = i2c_get_adapter(1);
  1548. config = pdev->dev.platform_data;
  1549. subdev_count = config->subdev_count;
  1550. subdevdata = config->subdevinfo;
  1551. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1552. GFP_KERNEL);
  1553. if (vpif_obj.sd == NULL) {
  1554. vpif_err("unable to allocate memory for subdevice pointers\n");
  1555. err = -ENOMEM;
  1556. goto probe_out;
  1557. }
  1558. for (i = 0; i < subdev_count; i++) {
  1559. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1560. i2c_adap,
  1561. &subdevdata[i].board_info,
  1562. NULL);
  1563. if (!vpif_obj.sd[i]) {
  1564. vpif_err("Error registering v4l2 subdevice\n");
  1565. goto probe_subdev_out;
  1566. }
  1567. if (vpif_obj.sd[i])
  1568. vpif_obj.sd[i]->grp_id = 1 << i;
  1569. }
  1570. v4l2_info(&vpif_obj.v4l2_dev,
  1571. " VPIF display driver initialized\n");
  1572. return 0;
  1573. probe_subdev_out:
  1574. kfree(vpif_obj.sd);
  1575. probe_out:
  1576. for (k = 0; k < j; k++) {
  1577. ch = vpif_obj.dev[k];
  1578. video_unregister_device(ch->video_dev);
  1579. video_device_release(ch->video_dev);
  1580. ch->video_dev = NULL;
  1581. }
  1582. vpif_int_err:
  1583. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1584. vpif_err("VPIF IRQ request failed\n");
  1585. for (q = k; k >= 0; k--) {
  1586. for (m = i; m >= res->start; m--)
  1587. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1588. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1589. m = res->end;
  1590. }
  1591. return err;
  1592. }
  1593. /*
  1594. * vpif_remove: It un-register channels from V4L2 driver
  1595. */
  1596. static int vpif_remove(struct platform_device *device)
  1597. {
  1598. struct channel_obj *ch;
  1599. int i;
  1600. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1601. /* un-register device */
  1602. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1603. /* Get the pointer to the channel object */
  1604. ch = vpif_obj.dev[i];
  1605. /* Unregister video device */
  1606. video_unregister_device(ch->video_dev);
  1607. ch->video_dev = NULL;
  1608. }
  1609. return 0;
  1610. }
  1611. #ifdef CONFIG_PM
  1612. static int vpif_suspend(struct device *dev)
  1613. {
  1614. struct common_obj *common;
  1615. struct channel_obj *ch;
  1616. int i;
  1617. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1618. /* Get the pointer to the channel object */
  1619. ch = vpif_obj.dev[i];
  1620. common = &ch->common[VPIF_VIDEO_INDEX];
  1621. mutex_lock(&common->lock);
  1622. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1623. /* Disable channel */
  1624. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1625. enable_channel2(0);
  1626. channel2_intr_enable(0);
  1627. }
  1628. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1629. common->started == 2) {
  1630. enable_channel3(0);
  1631. channel3_intr_enable(0);
  1632. }
  1633. }
  1634. mutex_unlock(&common->lock);
  1635. }
  1636. return 0;
  1637. }
  1638. static int vpif_resume(struct device *dev)
  1639. {
  1640. struct common_obj *common;
  1641. struct channel_obj *ch;
  1642. int i;
  1643. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1644. /* Get the pointer to the channel object */
  1645. ch = vpif_obj.dev[i];
  1646. common = &ch->common[VPIF_VIDEO_INDEX];
  1647. mutex_lock(&common->lock);
  1648. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1649. /* Enable channel */
  1650. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1651. enable_channel2(1);
  1652. channel2_intr_enable(1);
  1653. }
  1654. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1655. common->started == 2) {
  1656. enable_channel3(1);
  1657. channel3_intr_enable(1);
  1658. }
  1659. }
  1660. mutex_unlock(&common->lock);
  1661. }
  1662. return 0;
  1663. }
  1664. static const struct dev_pm_ops vpif_pm = {
  1665. .suspend = vpif_suspend,
  1666. .resume = vpif_resume,
  1667. };
  1668. #define vpif_pm_ops (&vpif_pm)
  1669. #else
  1670. #define vpif_pm_ops NULL
  1671. #endif
  1672. static __refdata struct platform_driver vpif_driver = {
  1673. .driver = {
  1674. .name = "vpif_display",
  1675. .owner = THIS_MODULE,
  1676. .pm = vpif_pm_ops,
  1677. },
  1678. .probe = vpif_probe,
  1679. .remove = vpif_remove,
  1680. };
  1681. static __init int vpif_init(void)
  1682. {
  1683. return platform_driver_register(&vpif_driver);
  1684. }
  1685. /*
  1686. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1687. * frees requested irq handler and de-allocates memory allocated for channel
  1688. * objects.
  1689. */
  1690. static void vpif_cleanup(void)
  1691. {
  1692. struct platform_device *pdev;
  1693. struct resource *res;
  1694. int irq_num;
  1695. int i = 0;
  1696. pdev = container_of(vpif_dev, struct platform_device, dev);
  1697. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1698. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1699. free_irq(irq_num,
  1700. (void *)(&vpif_obj.dev[i]->channel_id));
  1701. i++;
  1702. }
  1703. platform_driver_unregister(&vpif_driver);
  1704. kfree(vpif_obj.sd);
  1705. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1706. kfree(vpif_obj.dev[i]);
  1707. }
  1708. module_init(vpif_init);
  1709. module_exit(vpif_cleanup);