vpif_capture.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Inc
  3. * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * TODO : add support for VBI & HBI data service
  16. * add static buffer allocation
  17. */
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include "vpif.h"
  24. #include "vpif_capture.h"
  25. MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
  26. MODULE_LICENSE("GPL");
  27. MODULE_VERSION(VPIF_CAPTURE_VERSION);
  28. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  29. #define vpif_dbg(level, debug, fmt, arg...) \
  30. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  31. static int debug = 1;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Debug level 0-1");
  34. #define VPIF_DRIVER_NAME "vpif_capture"
  35. MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
  36. /* global variables */
  37. static struct vpif_device vpif_obj = { {NULL} };
  38. static struct device *vpif_dev;
  39. static void vpif_calculate_offsets(struct channel_obj *ch);
  40. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  41. static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
  42. /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
  43. static int ycmux_mode;
  44. static inline
  45. struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
  46. {
  47. return container_of(vb, struct vpif_cap_buffer, vb);
  48. }
  49. /**
  50. * vpif_buffer_prepare : callback function for buffer prepare
  51. * @vb: ptr to vb2_buffer
  52. *
  53. * This is the callback function for buffer prepare when vb2_qbuf()
  54. * function is called. The buffer is prepared and user space virtual address
  55. * or user address is converted into physical address
  56. */
  57. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  58. {
  59. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  60. struct vb2_queue *q = vb->vb2_queue;
  61. struct channel_obj *ch = vb2_get_drv_priv(q);
  62. struct common_obj *common;
  63. unsigned long addr;
  64. vpif_dbg(2, debug, "vpif_buffer_prepare\n");
  65. common = &ch->common[VPIF_VIDEO_INDEX];
  66. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  67. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  68. return -EINVAL;
  69. vbuf->field = common->fmt.fmt.pix.field;
  70. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  71. if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
  72. !IS_ALIGNED((addr + common->ybtm_off), 8) ||
  73. !IS_ALIGNED((addr + common->ctop_off), 8) ||
  74. !IS_ALIGNED((addr + common->cbtm_off), 8)) {
  75. vpif_dbg(1, debug, "offset is not aligned\n");
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. /**
  81. * vpif_buffer_queue_setup : Callback function for buffer setup.
  82. * @vq: vb2_queue ptr
  83. * @nbuffers: ptr to number of buffers requested by application
  84. * @nplanes:: contains number of distinct video planes needed to hold a frame
  85. * @sizes[]: contains the size (in bytes) of each plane.
  86. * @alloc_devs: ptr to allocation context
  87. *
  88. * This callback function is called when reqbuf() is called to adjust
  89. * the buffer count and buffer size
  90. */
  91. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  92. unsigned int *nbuffers, unsigned int *nplanes,
  93. unsigned int sizes[], struct device *alloc_devs[])
  94. {
  95. struct channel_obj *ch = vb2_get_drv_priv(vq);
  96. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  97. unsigned size = common->fmt.fmt.pix.sizeimage;
  98. vpif_dbg(2, debug, "vpif_buffer_setup\n");
  99. if (*nplanes) {
  100. if (sizes[0] < size)
  101. return -EINVAL;
  102. size = sizes[0];
  103. }
  104. if (vq->num_buffers + *nbuffers < 3)
  105. *nbuffers = 3 - vq->num_buffers;
  106. *nplanes = 1;
  107. sizes[0] = size;
  108. /* Calculate the offset for Y and C data in the buffer */
  109. vpif_calculate_offsets(ch);
  110. return 0;
  111. }
  112. /**
  113. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  114. * @vb: ptr to vb2_buffer
  115. */
  116. static void vpif_buffer_queue(struct vb2_buffer *vb)
  117. {
  118. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  119. struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
  120. struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
  121. struct common_obj *common;
  122. unsigned long flags;
  123. common = &ch->common[VPIF_VIDEO_INDEX];
  124. vpif_dbg(2, debug, "vpif_buffer_queue\n");
  125. spin_lock_irqsave(&common->irqlock, flags);
  126. /* add the buffer to the DMA queue */
  127. list_add_tail(&buf->list, &common->dma_queue);
  128. spin_unlock_irqrestore(&common->irqlock, flags);
  129. }
  130. /**
  131. * vpif_start_streaming : Starts the DMA engine for streaming
  132. * @vb: ptr to vb2_buffer
  133. * @count: number of buffers
  134. */
  135. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  136. {
  137. struct vpif_capture_config *vpif_config_data =
  138. vpif_dev->platform_data;
  139. struct channel_obj *ch = vb2_get_drv_priv(vq);
  140. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  141. struct vpif_params *vpif = &ch->vpifparams;
  142. struct vpif_cap_buffer *buf, *tmp;
  143. unsigned long addr, flags;
  144. int ret;
  145. /* Initialize field_id */
  146. ch->field_id = 0;
  147. /* configure 1 or 2 channel mode */
  148. if (vpif_config_data->setup_input_channel_mode) {
  149. ret = vpif_config_data->
  150. setup_input_channel_mode(vpif->std_info.ycmux_mode);
  151. if (ret < 0) {
  152. vpif_dbg(1, debug, "can't set vpif channel mode\n");
  153. goto err;
  154. }
  155. }
  156. ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
  157. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
  158. vpif_dbg(1, debug, "stream on failed in subdev\n");
  159. goto err;
  160. }
  161. /* Call vpif_set_params function to set the parameters and addresses */
  162. ret = vpif_set_video_params(vpif, ch->channel_id);
  163. if (ret < 0) {
  164. vpif_dbg(1, debug, "can't set video params\n");
  165. goto err;
  166. }
  167. ycmux_mode = ret;
  168. vpif_config_addr(ch, ret);
  169. /* Get the next frame from the buffer queue */
  170. spin_lock_irqsave(&common->irqlock, flags);
  171. common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
  172. struct vpif_cap_buffer, list);
  173. /* Remove buffer from the buffer queue */
  174. list_del(&common->cur_frm->list);
  175. spin_unlock_irqrestore(&common->irqlock, flags);
  176. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
  177. common->set_addr(addr + common->ytop_off,
  178. addr + common->ybtm_off,
  179. addr + common->ctop_off,
  180. addr + common->cbtm_off);
  181. /**
  182. * Set interrupt for both the fields in VPIF Register enable channel in
  183. * VPIF register
  184. */
  185. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  186. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  187. channel0_intr_assert();
  188. channel0_intr_enable(1);
  189. enable_channel0(1);
  190. }
  191. if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
  192. ycmux_mode == 2) {
  193. channel1_intr_assert();
  194. channel1_intr_enable(1);
  195. enable_channel1(1);
  196. }
  197. return 0;
  198. err:
  199. spin_lock_irqsave(&common->irqlock, flags);
  200. list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
  201. list_del(&buf->list);
  202. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  203. }
  204. spin_unlock_irqrestore(&common->irqlock, flags);
  205. return ret;
  206. }
  207. /**
  208. * vpif_stop_streaming : Stop the DMA engine
  209. * @vq: ptr to vb2_queue
  210. *
  211. * This callback stops the DMA engine and any remaining buffers
  212. * in the DMA queue are released.
  213. */
  214. static void vpif_stop_streaming(struct vb2_queue *vq)
  215. {
  216. struct channel_obj *ch = vb2_get_drv_priv(vq);
  217. struct common_obj *common;
  218. unsigned long flags;
  219. int ret;
  220. common = &ch->common[VPIF_VIDEO_INDEX];
  221. /* Disable channel as per its device type and channel id */
  222. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  223. enable_channel0(0);
  224. channel0_intr_enable(0);
  225. }
  226. if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
  227. ycmux_mode == 2) {
  228. enable_channel1(0);
  229. channel1_intr_enable(0);
  230. }
  231. ycmux_mode = 0;
  232. ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
  233. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
  234. vpif_dbg(1, debug, "stream off failed in subdev\n");
  235. /* release all active buffers */
  236. if (common->cur_frm == common->next_frm) {
  237. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  238. VB2_BUF_STATE_ERROR);
  239. } else {
  240. if (common->cur_frm)
  241. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  242. VB2_BUF_STATE_ERROR);
  243. if (common->next_frm)
  244. vb2_buffer_done(&common->next_frm->vb.vb2_buf,
  245. VB2_BUF_STATE_ERROR);
  246. }
  247. spin_lock_irqsave(&common->irqlock, flags);
  248. while (!list_empty(&common->dma_queue)) {
  249. common->next_frm = list_entry(common->dma_queue.next,
  250. struct vpif_cap_buffer, list);
  251. list_del(&common->next_frm->list);
  252. vb2_buffer_done(&common->next_frm->vb.vb2_buf,
  253. VB2_BUF_STATE_ERROR);
  254. }
  255. spin_unlock_irqrestore(&common->irqlock, flags);
  256. }
  257. static struct vb2_ops video_qops = {
  258. .queue_setup = vpif_buffer_queue_setup,
  259. .buf_prepare = vpif_buffer_prepare,
  260. .start_streaming = vpif_start_streaming,
  261. .stop_streaming = vpif_stop_streaming,
  262. .buf_queue = vpif_buffer_queue,
  263. .wait_prepare = vb2_ops_wait_prepare,
  264. .wait_finish = vb2_ops_wait_finish,
  265. };
  266. /**
  267. * vpif_process_buffer_complete: process a completed buffer
  268. * @common: ptr to common channel object
  269. *
  270. * This function time stamp the buffer and mark it as DONE. It also
  271. * wake up any process waiting on the QUEUE and set the next buffer
  272. * as current
  273. */
  274. static void vpif_process_buffer_complete(struct common_obj *common)
  275. {
  276. common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  277. vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  278. /* Make curFrm pointing to nextFrm */
  279. common->cur_frm = common->next_frm;
  280. }
  281. /**
  282. * vpif_schedule_next_buffer: set next buffer address for capture
  283. * @common : ptr to common channel object
  284. *
  285. * This function will get next buffer from the dma queue and
  286. * set the buffer address in the vpif register for capture.
  287. * the buffer is marked active
  288. */
  289. static void vpif_schedule_next_buffer(struct common_obj *common)
  290. {
  291. unsigned long addr = 0;
  292. spin_lock(&common->irqlock);
  293. common->next_frm = list_entry(common->dma_queue.next,
  294. struct vpif_cap_buffer, list);
  295. /* Remove that buffer from the buffer queue */
  296. list_del(&common->next_frm->list);
  297. spin_unlock(&common->irqlock);
  298. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
  299. /* Set top and bottom field addresses in VPIF registers */
  300. common->set_addr(addr + common->ytop_off,
  301. addr + common->ybtm_off,
  302. addr + common->ctop_off,
  303. addr + common->cbtm_off);
  304. }
  305. /**
  306. * vpif_channel_isr : ISR handler for vpif capture
  307. * @irq: irq number
  308. * @dev_id: dev_id ptr
  309. *
  310. * It changes status of the captured buffer, takes next buffer from the queue
  311. * and sets its address in VPIF registers
  312. */
  313. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  314. {
  315. struct vpif_device *dev = &vpif_obj;
  316. struct common_obj *common;
  317. struct channel_obj *ch;
  318. int channel_id;
  319. int fid = -1, i;
  320. channel_id = *(int *)(dev_id);
  321. if (!vpif_intr_status(channel_id))
  322. return IRQ_NONE;
  323. ch = dev->dev[channel_id];
  324. for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
  325. common = &ch->common[i];
  326. /* skip If streaming is not started in this channel */
  327. /* Check the field format */
  328. if (1 == ch->vpifparams.std_info.frm_fmt) {
  329. /* Progressive mode */
  330. spin_lock(&common->irqlock);
  331. if (list_empty(&common->dma_queue)) {
  332. spin_unlock(&common->irqlock);
  333. continue;
  334. }
  335. spin_unlock(&common->irqlock);
  336. if (!channel_first_int[i][channel_id])
  337. vpif_process_buffer_complete(common);
  338. channel_first_int[i][channel_id] = 0;
  339. vpif_schedule_next_buffer(common);
  340. channel_first_int[i][channel_id] = 0;
  341. } else {
  342. /**
  343. * Interlaced mode. If it is first interrupt, ignore
  344. * it
  345. */
  346. if (channel_first_int[i][channel_id]) {
  347. channel_first_int[i][channel_id] = 0;
  348. continue;
  349. }
  350. if (0 == i) {
  351. ch->field_id ^= 1;
  352. /* Get field id from VPIF registers */
  353. fid = vpif_channel_getfid(ch->channel_id);
  354. if (fid != ch->field_id) {
  355. /**
  356. * If field id does not match stored
  357. * field id, make them in sync
  358. */
  359. if (0 == fid)
  360. ch->field_id = fid;
  361. return IRQ_HANDLED;
  362. }
  363. }
  364. /* device field id and local field id are in sync */
  365. if (0 == fid) {
  366. /* this is even field */
  367. if (common->cur_frm == common->next_frm)
  368. continue;
  369. /* mark the current buffer as done */
  370. vpif_process_buffer_complete(common);
  371. } else if (1 == fid) {
  372. /* odd field */
  373. spin_lock(&common->irqlock);
  374. if (list_empty(&common->dma_queue) ||
  375. (common->cur_frm != common->next_frm)) {
  376. spin_unlock(&common->irqlock);
  377. continue;
  378. }
  379. spin_unlock(&common->irqlock);
  380. vpif_schedule_next_buffer(common);
  381. }
  382. }
  383. }
  384. return IRQ_HANDLED;
  385. }
  386. /**
  387. * vpif_update_std_info() - update standard related info
  388. * @ch: ptr to channel object
  389. *
  390. * For a given standard selected by application, update values
  391. * in the device data structures
  392. */
  393. static int vpif_update_std_info(struct channel_obj *ch)
  394. {
  395. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  396. struct vpif_params *vpifparams = &ch->vpifparams;
  397. const struct vpif_channel_config_params *config;
  398. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  399. struct video_obj *vid_ch = &ch->video;
  400. int index;
  401. vpif_dbg(2, debug, "vpif_update_std_info\n");
  402. for (index = 0; index < vpif_ch_params_count; index++) {
  403. config = &vpif_ch_params[index];
  404. if (config->hd_sd == 0) {
  405. vpif_dbg(2, debug, "SD format\n");
  406. if (config->stdid & vid_ch->stdid) {
  407. memcpy(std_info, config, sizeof(*config));
  408. break;
  409. }
  410. } else {
  411. vpif_dbg(2, debug, "HD format\n");
  412. if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
  413. sizeof(vid_ch->dv_timings))) {
  414. memcpy(std_info, config, sizeof(*config));
  415. break;
  416. }
  417. }
  418. }
  419. /* standard not found */
  420. if (index == vpif_ch_params_count)
  421. return -EINVAL;
  422. common->fmt.fmt.pix.width = std_info->width;
  423. common->width = std_info->width;
  424. common->fmt.fmt.pix.height = std_info->height;
  425. common->height = std_info->height;
  426. common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
  427. common->fmt.fmt.pix.bytesperline = std_info->width;
  428. vpifparams->video_params.hpitch = std_info->width;
  429. vpifparams->video_params.storage_mode = std_info->frm_fmt;
  430. if (vid_ch->stdid)
  431. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  432. else
  433. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  434. if (ch->vpifparams.std_info.frm_fmt)
  435. common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
  436. else
  437. common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  438. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
  439. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
  440. else
  441. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  442. common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  443. return 0;
  444. }
  445. /**
  446. * vpif_calculate_offsets : This function calculates buffers offsets
  447. * @ch : ptr to channel object
  448. *
  449. * This function calculates buffer offsets for Y and C in the top and
  450. * bottom field
  451. */
  452. static void vpif_calculate_offsets(struct channel_obj *ch)
  453. {
  454. unsigned int hpitch, sizeimage;
  455. struct video_obj *vid_ch = &(ch->video);
  456. struct vpif_params *vpifparams = &ch->vpifparams;
  457. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  458. enum v4l2_field field = common->fmt.fmt.pix.field;
  459. vpif_dbg(2, debug, "vpif_calculate_offsets\n");
  460. if (V4L2_FIELD_ANY == field) {
  461. if (vpifparams->std_info.frm_fmt)
  462. vid_ch->buf_field = V4L2_FIELD_NONE;
  463. else
  464. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  465. } else
  466. vid_ch->buf_field = common->fmt.fmt.pix.field;
  467. sizeimage = common->fmt.fmt.pix.sizeimage;
  468. hpitch = common->fmt.fmt.pix.bytesperline;
  469. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  470. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  471. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  472. common->ytop_off = 0;
  473. common->ybtm_off = hpitch;
  474. common->ctop_off = sizeimage / 2;
  475. common->cbtm_off = sizeimage / 2 + hpitch;
  476. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  477. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  478. common->ytop_off = 0;
  479. common->ybtm_off = sizeimage / 4;
  480. common->ctop_off = sizeimage / 2;
  481. common->cbtm_off = common->ctop_off + sizeimage / 4;
  482. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  483. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  484. common->ybtm_off = 0;
  485. common->ytop_off = sizeimage / 4;
  486. common->cbtm_off = sizeimage / 2;
  487. common->ctop_off = common->cbtm_off + sizeimage / 4;
  488. }
  489. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  490. (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
  491. vpifparams->video_params.storage_mode = 1;
  492. else
  493. vpifparams->video_params.storage_mode = 0;
  494. if (1 == vpifparams->std_info.frm_fmt)
  495. vpifparams->video_params.hpitch =
  496. common->fmt.fmt.pix.bytesperline;
  497. else {
  498. if ((field == V4L2_FIELD_ANY)
  499. || (field == V4L2_FIELD_INTERLACED))
  500. vpifparams->video_params.hpitch =
  501. common->fmt.fmt.pix.bytesperline * 2;
  502. else
  503. vpifparams->video_params.hpitch =
  504. common->fmt.fmt.pix.bytesperline;
  505. }
  506. ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
  507. }
  508. /**
  509. * vpif_get_default_field() - Get default field type based on interface
  510. * @vpif_params - ptr to vpif params
  511. */
  512. static inline enum v4l2_field vpif_get_default_field(
  513. struct vpif_interface *iface)
  514. {
  515. return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
  516. V4L2_FIELD_INTERLACED;
  517. }
  518. /**
  519. * vpif_config_addr() - function to configure buffer address in vpif
  520. * @ch - channel ptr
  521. * @muxmode - channel mux mode
  522. */
  523. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  524. {
  525. struct common_obj *common;
  526. vpif_dbg(2, debug, "vpif_config_addr\n");
  527. common = &(ch->common[VPIF_VIDEO_INDEX]);
  528. if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
  529. common->set_addr = ch1_set_videobuf_addr;
  530. else if (2 == muxmode)
  531. common->set_addr = ch0_set_videobuf_addr_yc_nmux;
  532. else
  533. common->set_addr = ch0_set_videobuf_addr;
  534. }
  535. /**
  536. * vpif_input_to_subdev() - Maps input to sub device
  537. * @vpif_cfg - global config ptr
  538. * @chan_cfg - channel config ptr
  539. * @input_index - Given input index from application
  540. *
  541. * lookup the sub device information for a given input index.
  542. * we report all the inputs to application. inputs table also
  543. * has sub device name for the each input
  544. */
  545. static int vpif_input_to_subdev(
  546. struct vpif_capture_config *vpif_cfg,
  547. struct vpif_capture_chan_config *chan_cfg,
  548. int input_index)
  549. {
  550. struct vpif_subdev_info *subdev_info;
  551. const char *subdev_name;
  552. int i;
  553. vpif_dbg(2, debug, "vpif_input_to_subdev\n");
  554. if (!chan_cfg)
  555. return -1;
  556. if (input_index >= chan_cfg->input_count)
  557. return -1;
  558. subdev_name = chan_cfg->inputs[input_index].subdev_name;
  559. if (!subdev_name)
  560. return -1;
  561. /* loop through the sub device list to get the sub device info */
  562. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  563. subdev_info = &vpif_cfg->subdev_info[i];
  564. if (!strcmp(subdev_info->name, subdev_name))
  565. return i;
  566. }
  567. return -1;
  568. }
  569. /**
  570. * vpif_set_input() - Select an input
  571. * @vpif_cfg - global config ptr
  572. * @ch - channel
  573. * @_index - Given input index from application
  574. *
  575. * Select the given input.
  576. */
  577. static int vpif_set_input(
  578. struct vpif_capture_config *vpif_cfg,
  579. struct channel_obj *ch,
  580. int index)
  581. {
  582. struct vpif_capture_chan_config *chan_cfg =
  583. &vpif_cfg->chan_config[ch->channel_id];
  584. struct vpif_subdev_info *subdev_info = NULL;
  585. struct v4l2_subdev *sd = NULL;
  586. u32 input = 0, output = 0;
  587. int sd_index;
  588. int ret;
  589. sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
  590. if (sd_index >= 0) {
  591. sd = vpif_obj.sd[sd_index];
  592. subdev_info = &vpif_cfg->subdev_info[sd_index];
  593. } else {
  594. /* no subdevice, no input to setup */
  595. return 0;
  596. }
  597. /* first setup input path from sub device to vpif */
  598. if (sd && vpif_cfg->setup_input_path) {
  599. ret = vpif_cfg->setup_input_path(ch->channel_id,
  600. subdev_info->name);
  601. if (ret < 0) {
  602. vpif_dbg(1, debug, "couldn't setup input path for the" \
  603. " sub device %s, for input index %d\n",
  604. subdev_info->name, index);
  605. return ret;
  606. }
  607. }
  608. if (sd) {
  609. input = chan_cfg->inputs[index].input_route;
  610. output = chan_cfg->inputs[index].output_route;
  611. ret = v4l2_subdev_call(sd, video, s_routing,
  612. input, output, 0);
  613. if (ret < 0 && ret != -ENOIOCTLCMD) {
  614. vpif_dbg(1, debug, "Failed to set input\n");
  615. return ret;
  616. }
  617. }
  618. ch->input_idx = index;
  619. ch->sd = sd;
  620. /* copy interface parameters to vpif */
  621. ch->vpifparams.iface = chan_cfg->vpif_if;
  622. /* update tvnorms from the sub device input info */
  623. ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
  624. return 0;
  625. }
  626. /**
  627. * vpif_querystd() - querystd handler
  628. * @file: file ptr
  629. * @priv: file handle
  630. * @std_id: ptr to std id
  631. *
  632. * This function is called to detect standard at the selected input
  633. */
  634. static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  635. {
  636. struct video_device *vdev = video_devdata(file);
  637. struct channel_obj *ch = video_get_drvdata(vdev);
  638. int ret;
  639. vpif_dbg(2, debug, "vpif_querystd\n");
  640. /* Call querystd function of decoder device */
  641. ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
  642. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  643. return -ENODATA;
  644. if (ret) {
  645. vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
  646. return ret;
  647. }
  648. return 0;
  649. }
  650. /**
  651. * vpif_g_std() - get STD handler
  652. * @file: file ptr
  653. * @priv: file handle
  654. * @std_id: ptr to std id
  655. */
  656. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  657. {
  658. struct vpif_capture_config *config = vpif_dev->platform_data;
  659. struct video_device *vdev = video_devdata(file);
  660. struct channel_obj *ch = video_get_drvdata(vdev);
  661. struct vpif_capture_chan_config *chan_cfg;
  662. struct v4l2_input input;
  663. vpif_dbg(2, debug, "vpif_g_std\n");
  664. if (!config->chan_config[ch->channel_id].inputs)
  665. return -ENODATA;
  666. chan_cfg = &config->chan_config[ch->channel_id];
  667. input = chan_cfg->inputs[ch->input_idx].input;
  668. if (input.capabilities != V4L2_IN_CAP_STD)
  669. return -ENODATA;
  670. *std = ch->video.stdid;
  671. return 0;
  672. }
  673. /**
  674. * vpif_s_std() - set STD handler
  675. * @file: file ptr
  676. * @priv: file handle
  677. * @std_id: ptr to std id
  678. */
  679. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
  680. {
  681. struct vpif_capture_config *config = vpif_dev->platform_data;
  682. struct video_device *vdev = video_devdata(file);
  683. struct channel_obj *ch = video_get_drvdata(vdev);
  684. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  685. struct vpif_capture_chan_config *chan_cfg;
  686. struct v4l2_input input;
  687. int ret;
  688. vpif_dbg(2, debug, "vpif_s_std\n");
  689. if (!config->chan_config[ch->channel_id].inputs)
  690. return -ENODATA;
  691. chan_cfg = &config->chan_config[ch->channel_id];
  692. input = chan_cfg->inputs[ch->input_idx].input;
  693. if (input.capabilities != V4L2_IN_CAP_STD)
  694. return -ENODATA;
  695. if (vb2_is_busy(&common->buffer_queue))
  696. return -EBUSY;
  697. /* Call encoder subdevice function to set the standard */
  698. ch->video.stdid = std_id;
  699. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  700. /* Get the information about the standard */
  701. if (vpif_update_std_info(ch)) {
  702. vpif_err("Error getting the standard info\n");
  703. return -EINVAL;
  704. }
  705. /* set standard in the sub device */
  706. ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
  707. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
  708. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  709. return ret;
  710. }
  711. return 0;
  712. }
  713. /**
  714. * vpif_enum_input() - ENUMINPUT handler
  715. * @file: file ptr
  716. * @priv: file handle
  717. * @input: ptr to input structure
  718. */
  719. static int vpif_enum_input(struct file *file, void *priv,
  720. struct v4l2_input *input)
  721. {
  722. struct vpif_capture_config *config = vpif_dev->platform_data;
  723. struct video_device *vdev = video_devdata(file);
  724. struct channel_obj *ch = video_get_drvdata(vdev);
  725. struct vpif_capture_chan_config *chan_cfg;
  726. chan_cfg = &config->chan_config[ch->channel_id];
  727. if (input->index >= chan_cfg->input_count)
  728. return -EINVAL;
  729. memcpy(input, &chan_cfg->inputs[input->index].input,
  730. sizeof(*input));
  731. return 0;
  732. }
  733. /**
  734. * vpif_g_input() - Get INPUT handler
  735. * @file: file ptr
  736. * @priv: file handle
  737. * @index: ptr to input index
  738. */
  739. static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
  740. {
  741. struct video_device *vdev = video_devdata(file);
  742. struct channel_obj *ch = video_get_drvdata(vdev);
  743. *index = ch->input_idx;
  744. return 0;
  745. }
  746. /**
  747. * vpif_s_input() - Set INPUT handler
  748. * @file: file ptr
  749. * @priv: file handle
  750. * @index: input index
  751. */
  752. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  753. {
  754. struct vpif_capture_config *config = vpif_dev->platform_data;
  755. struct video_device *vdev = video_devdata(file);
  756. struct channel_obj *ch = video_get_drvdata(vdev);
  757. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  758. struct vpif_capture_chan_config *chan_cfg;
  759. chan_cfg = &config->chan_config[ch->channel_id];
  760. if (index >= chan_cfg->input_count)
  761. return -EINVAL;
  762. if (vb2_is_busy(&common->buffer_queue))
  763. return -EBUSY;
  764. return vpif_set_input(config, ch, index);
  765. }
  766. /**
  767. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  768. * @file: file ptr
  769. * @priv: file handle
  770. * @index: input index
  771. */
  772. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  773. struct v4l2_fmtdesc *fmt)
  774. {
  775. struct video_device *vdev = video_devdata(file);
  776. struct channel_obj *ch = video_get_drvdata(vdev);
  777. if (fmt->index != 0) {
  778. vpif_dbg(1, debug, "Invalid format index\n");
  779. return -EINVAL;
  780. }
  781. /* Fill in the information about format */
  782. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  783. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  784. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  785. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  786. } else {
  787. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  788. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  789. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  790. }
  791. return 0;
  792. }
  793. /**
  794. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  795. * @file: file ptr
  796. * @priv: file handle
  797. * @fmt: ptr to v4l2 format structure
  798. */
  799. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  800. struct v4l2_format *fmt)
  801. {
  802. struct video_device *vdev = video_devdata(file);
  803. struct channel_obj *ch = video_get_drvdata(vdev);
  804. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  805. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  806. struct vpif_params *vpif_params = &ch->vpifparams;
  807. /*
  808. * to supress v4l-compliance warnings silently correct
  809. * the pixelformat
  810. */
  811. if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
  812. if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
  813. pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  814. } else {
  815. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  816. pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  817. }
  818. common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
  819. vpif_update_std_info(ch);
  820. pixfmt->field = common->fmt.fmt.pix.field;
  821. pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
  822. pixfmt->bytesperline = common->fmt.fmt.pix.width;
  823. pixfmt->width = common->fmt.fmt.pix.width;
  824. pixfmt->height = common->fmt.fmt.pix.height;
  825. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
  826. pixfmt->priv = 0;
  827. return 0;
  828. }
  829. /**
  830. * vpif_g_fmt_vid_cap() - Set INPUT handler
  831. * @file: file ptr
  832. * @priv: file handle
  833. * @fmt: ptr to v4l2 format structure
  834. */
  835. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  836. struct v4l2_format *fmt)
  837. {
  838. struct video_device *vdev = video_devdata(file);
  839. struct channel_obj *ch = video_get_drvdata(vdev);
  840. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  841. /* Check the validity of the buffer type */
  842. if (common->fmt.type != fmt->type)
  843. return -EINVAL;
  844. /* Fill in the information about format */
  845. *fmt = common->fmt;
  846. return 0;
  847. }
  848. /**
  849. * vpif_s_fmt_vid_cap() - Set FMT handler
  850. * @file: file ptr
  851. * @priv: file handle
  852. * @fmt: ptr to v4l2 format structure
  853. */
  854. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  855. struct v4l2_format *fmt)
  856. {
  857. struct video_device *vdev = video_devdata(file);
  858. struct channel_obj *ch = video_get_drvdata(vdev);
  859. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  860. int ret;
  861. vpif_dbg(2, debug, "%s\n", __func__);
  862. if (vb2_is_busy(&common->buffer_queue))
  863. return -EBUSY;
  864. ret = vpif_try_fmt_vid_cap(file, priv, fmt);
  865. if (ret)
  866. return ret;
  867. /* store the format in the channel object */
  868. common->fmt = *fmt;
  869. return 0;
  870. }
  871. /**
  872. * vpif_querycap() - QUERYCAP handler
  873. * @file: file ptr
  874. * @priv: file handle
  875. * @cap: ptr to v4l2_capability structure
  876. */
  877. static int vpif_querycap(struct file *file, void *priv,
  878. struct v4l2_capability *cap)
  879. {
  880. struct vpif_capture_config *config = vpif_dev->platform_data;
  881. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  882. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  883. strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
  884. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  885. dev_name(vpif_dev));
  886. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  887. return 0;
  888. }
  889. /**
  890. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  891. * @file: file ptr
  892. * @priv: file handle
  893. * @timings: input timings
  894. */
  895. static int
  896. vpif_enum_dv_timings(struct file *file, void *priv,
  897. struct v4l2_enum_dv_timings *timings)
  898. {
  899. struct vpif_capture_config *config = vpif_dev->platform_data;
  900. struct video_device *vdev = video_devdata(file);
  901. struct channel_obj *ch = video_get_drvdata(vdev);
  902. struct vpif_capture_chan_config *chan_cfg;
  903. struct v4l2_input input;
  904. int ret;
  905. if (!config->chan_config[ch->channel_id].inputs)
  906. return -ENODATA;
  907. chan_cfg = &config->chan_config[ch->channel_id];
  908. input = chan_cfg->inputs[ch->input_idx].input;
  909. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  910. return -ENODATA;
  911. timings->pad = 0;
  912. ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
  913. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  914. return -EINVAL;
  915. return ret;
  916. }
  917. /**
  918. * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
  919. * @file: file ptr
  920. * @priv: file handle
  921. * @timings: input timings
  922. */
  923. static int
  924. vpif_query_dv_timings(struct file *file, void *priv,
  925. struct v4l2_dv_timings *timings)
  926. {
  927. struct vpif_capture_config *config = vpif_dev->platform_data;
  928. struct video_device *vdev = video_devdata(file);
  929. struct channel_obj *ch = video_get_drvdata(vdev);
  930. struct vpif_capture_chan_config *chan_cfg;
  931. struct v4l2_input input;
  932. int ret;
  933. if (!config->chan_config[ch->channel_id].inputs)
  934. return -ENODATA;
  935. chan_cfg = &config->chan_config[ch->channel_id];
  936. input = chan_cfg->inputs[ch->input_idx].input;
  937. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  938. return -ENODATA;
  939. ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
  940. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  941. return -ENODATA;
  942. return ret;
  943. }
  944. /**
  945. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  946. * @file: file ptr
  947. * @priv: file handle
  948. * @timings: digital video timings
  949. */
  950. static int vpif_s_dv_timings(struct file *file, void *priv,
  951. struct v4l2_dv_timings *timings)
  952. {
  953. struct vpif_capture_config *config = vpif_dev->platform_data;
  954. struct video_device *vdev = video_devdata(file);
  955. struct channel_obj *ch = video_get_drvdata(vdev);
  956. struct vpif_params *vpifparams = &ch->vpifparams;
  957. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  958. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  959. struct video_obj *vid_ch = &ch->video;
  960. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  961. struct vpif_capture_chan_config *chan_cfg;
  962. struct v4l2_input input;
  963. int ret;
  964. if (!config->chan_config[ch->channel_id].inputs)
  965. return -ENODATA;
  966. chan_cfg = &config->chan_config[ch->channel_id];
  967. input = chan_cfg->inputs[ch->input_idx].input;
  968. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  969. return -ENODATA;
  970. if (timings->type != V4L2_DV_BT_656_1120) {
  971. vpif_dbg(2, debug, "Timing type not defined\n");
  972. return -EINVAL;
  973. }
  974. if (vb2_is_busy(&common->buffer_queue))
  975. return -EBUSY;
  976. /* Configure subdevice timings, if any */
  977. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  978. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  979. ret = 0;
  980. if (ret < 0) {
  981. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  982. return ret;
  983. }
  984. if (!(timings->bt.width && timings->bt.height &&
  985. (timings->bt.hbackporch ||
  986. timings->bt.hfrontporch ||
  987. timings->bt.hsync) &&
  988. timings->bt.vfrontporch &&
  989. (timings->bt.vbackporch ||
  990. timings->bt.vsync))) {
  991. vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
  992. return -EINVAL;
  993. }
  994. vid_ch->dv_timings = *timings;
  995. /* Configure video port timings */
  996. std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
  997. std_info->sav2eav = bt->width;
  998. std_info->l1 = 1;
  999. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1000. std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  1001. if (bt->interlaced) {
  1002. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1003. std_info->l5 = std_info->vsize/2 -
  1004. (bt->vfrontporch - 1);
  1005. std_info->l7 = std_info->vsize/2 + 1;
  1006. std_info->l9 = std_info->l7 + bt->il_vsync +
  1007. bt->il_vbackporch + 1;
  1008. std_info->l11 = std_info->vsize -
  1009. (bt->il_vfrontporch - 1);
  1010. } else {
  1011. vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
  1012. return -EINVAL;
  1013. }
  1014. } else {
  1015. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1016. }
  1017. strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
  1018. std_info->width = bt->width;
  1019. std_info->height = bt->height;
  1020. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1021. std_info->ycmux_mode = 0;
  1022. std_info->capture_format = 0;
  1023. std_info->vbi_supported = 0;
  1024. std_info->hd_sd = 1;
  1025. std_info->stdid = 0;
  1026. vid_ch->stdid = 0;
  1027. return 0;
  1028. }
  1029. /**
  1030. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1031. * @file: file ptr
  1032. * @priv: file handle
  1033. * @timings: digital video timings
  1034. */
  1035. static int vpif_g_dv_timings(struct file *file, void *priv,
  1036. struct v4l2_dv_timings *timings)
  1037. {
  1038. struct vpif_capture_config *config = vpif_dev->platform_data;
  1039. struct video_device *vdev = video_devdata(file);
  1040. struct channel_obj *ch = video_get_drvdata(vdev);
  1041. struct video_obj *vid_ch = &ch->video;
  1042. struct vpif_capture_chan_config *chan_cfg;
  1043. struct v4l2_input input;
  1044. if (!config->chan_config[ch->channel_id].inputs)
  1045. return -ENODATA;
  1046. chan_cfg = &config->chan_config[ch->channel_id];
  1047. input = chan_cfg->inputs[ch->input_idx].input;
  1048. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  1049. return -ENODATA;
  1050. *timings = vid_ch->dv_timings;
  1051. return 0;
  1052. }
  1053. /*
  1054. * vpif_log_status() - Status information
  1055. * @file: file ptr
  1056. * @priv: file handle
  1057. *
  1058. * Returns zero.
  1059. */
  1060. static int vpif_log_status(struct file *filep, void *priv)
  1061. {
  1062. /* status for sub devices */
  1063. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1064. return 0;
  1065. }
  1066. /* vpif capture ioctl operations */
  1067. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1068. .vidioc_querycap = vpif_querycap,
  1069. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1070. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1071. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1072. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1073. .vidioc_enum_input = vpif_enum_input,
  1074. .vidioc_s_input = vpif_s_input,
  1075. .vidioc_g_input = vpif_g_input,
  1076. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1077. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1078. .vidioc_querybuf = vb2_ioctl_querybuf,
  1079. .vidioc_qbuf = vb2_ioctl_qbuf,
  1080. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1081. .vidioc_expbuf = vb2_ioctl_expbuf,
  1082. .vidioc_streamon = vb2_ioctl_streamon,
  1083. .vidioc_streamoff = vb2_ioctl_streamoff,
  1084. .vidioc_querystd = vpif_querystd,
  1085. .vidioc_s_std = vpif_s_std,
  1086. .vidioc_g_std = vpif_g_std,
  1087. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1088. .vidioc_query_dv_timings = vpif_query_dv_timings,
  1089. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1090. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1091. .vidioc_log_status = vpif_log_status,
  1092. };
  1093. /* vpif file operations */
  1094. static struct v4l2_file_operations vpif_fops = {
  1095. .owner = THIS_MODULE,
  1096. .open = v4l2_fh_open,
  1097. .release = vb2_fop_release,
  1098. .unlocked_ioctl = video_ioctl2,
  1099. .mmap = vb2_fop_mmap,
  1100. .poll = vb2_fop_poll
  1101. };
  1102. /**
  1103. * initialize_vpif() - Initialize vpif data structures
  1104. *
  1105. * Allocate memory for data structures and initialize them
  1106. */
  1107. static int initialize_vpif(void)
  1108. {
  1109. int err, i, j;
  1110. int free_channel_objects_index;
  1111. /* Allocate memory for six channel objects */
  1112. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1113. vpif_obj.dev[i] =
  1114. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1115. /* If memory allocation fails, return error */
  1116. if (!vpif_obj.dev[i]) {
  1117. free_channel_objects_index = i;
  1118. err = -ENOMEM;
  1119. goto vpif_init_free_channel_objects;
  1120. }
  1121. }
  1122. return 0;
  1123. vpif_init_free_channel_objects:
  1124. for (j = 0; j < free_channel_objects_index; j++)
  1125. kfree(vpif_obj.dev[j]);
  1126. return err;
  1127. }
  1128. static int vpif_async_bound(struct v4l2_async_notifier *notifier,
  1129. struct v4l2_subdev *subdev,
  1130. struct v4l2_async_subdev *asd)
  1131. {
  1132. int i;
  1133. for (i = 0; i < vpif_obj.config->subdev_count; i++)
  1134. if (!strcmp(vpif_obj.config->subdev_info[i].name,
  1135. subdev->name)) {
  1136. vpif_obj.sd[i] = subdev;
  1137. return 0;
  1138. }
  1139. return -EINVAL;
  1140. }
  1141. static int vpif_probe_complete(void)
  1142. {
  1143. struct common_obj *common;
  1144. struct video_device *vdev;
  1145. struct channel_obj *ch;
  1146. struct vb2_queue *q;
  1147. int j, err, k;
  1148. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1149. ch = vpif_obj.dev[j];
  1150. ch->channel_id = j;
  1151. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1152. spin_lock_init(&common->irqlock);
  1153. mutex_init(&common->lock);
  1154. /* select input 0 */
  1155. err = vpif_set_input(vpif_obj.config, ch, 0);
  1156. if (err)
  1157. goto probe_out;
  1158. /* set initial format */
  1159. ch->video.stdid = V4L2_STD_525_60;
  1160. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  1161. vpif_update_std_info(ch);
  1162. /* Initialize vb2 queue */
  1163. q = &common->buffer_queue;
  1164. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1165. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1166. q->drv_priv = ch;
  1167. q->ops = &video_qops;
  1168. q->mem_ops = &vb2_dma_contig_memops;
  1169. q->buf_struct_size = sizeof(struct vpif_cap_buffer);
  1170. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1171. q->min_buffers_needed = 1;
  1172. q->lock = &common->lock;
  1173. q->dev = vpif_dev;
  1174. err = vb2_queue_init(q);
  1175. if (err) {
  1176. vpif_err("vpif_capture: vb2_queue_init() failed\n");
  1177. goto probe_out;
  1178. }
  1179. INIT_LIST_HEAD(&common->dma_queue);
  1180. /* Initialize the video_device structure */
  1181. vdev = &ch->video_dev;
  1182. strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
  1183. vdev->release = video_device_release_empty;
  1184. vdev->fops = &vpif_fops;
  1185. vdev->ioctl_ops = &vpif_ioctl_ops;
  1186. vdev->v4l2_dev = &vpif_obj.v4l2_dev;
  1187. vdev->vfl_dir = VFL_DIR_RX;
  1188. vdev->queue = q;
  1189. vdev->lock = &common->lock;
  1190. video_set_drvdata(&ch->video_dev, ch);
  1191. err = video_register_device(vdev,
  1192. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1193. if (err)
  1194. goto probe_out;
  1195. }
  1196. v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
  1197. return 0;
  1198. probe_out:
  1199. for (k = 0; k < j; k++) {
  1200. /* Get the pointer to the channel object */
  1201. ch = vpif_obj.dev[k];
  1202. common = &ch->common[k];
  1203. /* Unregister video device */
  1204. video_unregister_device(&ch->video_dev);
  1205. }
  1206. kfree(vpif_obj.sd);
  1207. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1208. return err;
  1209. }
  1210. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1211. {
  1212. return vpif_probe_complete();
  1213. }
  1214. /**
  1215. * vpif_probe : This function probes the vpif capture driver
  1216. * @pdev: platform device pointer
  1217. *
  1218. * This creates device entries by register itself to the V4L2 driver and
  1219. * initializes fields of each channel objects
  1220. */
  1221. static __init int vpif_probe(struct platform_device *pdev)
  1222. {
  1223. struct vpif_subdev_info *subdevdata;
  1224. struct i2c_adapter *i2c_adap;
  1225. struct resource *res;
  1226. int subdev_count;
  1227. int res_idx = 0;
  1228. int i, err;
  1229. if (!pdev->dev.platform_data) {
  1230. dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
  1231. return -EINVAL;
  1232. }
  1233. vpif_dev = &pdev->dev;
  1234. err = initialize_vpif();
  1235. if (err) {
  1236. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1237. return err;
  1238. }
  1239. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1240. if (err) {
  1241. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1242. return err;
  1243. }
  1244. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1245. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1246. IRQF_SHARED, VPIF_DRIVER_NAME,
  1247. (void *)(&vpif_obj.dev[res_idx]->
  1248. channel_id));
  1249. if (err) {
  1250. err = -EINVAL;
  1251. goto vpif_unregister;
  1252. }
  1253. res_idx++;
  1254. }
  1255. vpif_obj.config = pdev->dev.platform_data;
  1256. subdev_count = vpif_obj.config->subdev_count;
  1257. vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
  1258. if (!vpif_obj.sd) {
  1259. err = -ENOMEM;
  1260. goto vpif_unregister;
  1261. }
  1262. if (!vpif_obj.config->asd_sizes) {
  1263. int i2c_id = vpif_obj.config->i2c_adapter_id;
  1264. i2c_adap = i2c_get_adapter(i2c_id);
  1265. WARN_ON(!i2c_adap);
  1266. for (i = 0; i < subdev_count; i++) {
  1267. subdevdata = &vpif_obj.config->subdev_info[i];
  1268. vpif_obj.sd[i] =
  1269. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1270. i2c_adap,
  1271. &subdevdata->
  1272. board_info,
  1273. NULL);
  1274. if (!vpif_obj.sd[i]) {
  1275. vpif_err("Error registering v4l2 subdevice\n");
  1276. err = -ENODEV;
  1277. goto probe_subdev_out;
  1278. }
  1279. v4l2_info(&vpif_obj.v4l2_dev,
  1280. "registered sub device %s\n",
  1281. subdevdata->name);
  1282. }
  1283. vpif_probe_complete();
  1284. } else {
  1285. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1286. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1287. vpif_obj.notifier.bound = vpif_async_bound;
  1288. vpif_obj.notifier.complete = vpif_async_complete;
  1289. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1290. &vpif_obj.notifier);
  1291. if (err) {
  1292. vpif_err("Error registering async notifier\n");
  1293. err = -EINVAL;
  1294. goto probe_subdev_out;
  1295. }
  1296. }
  1297. return 0;
  1298. probe_subdev_out:
  1299. /* free sub devices memory */
  1300. kfree(vpif_obj.sd);
  1301. vpif_unregister:
  1302. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1303. return err;
  1304. }
  1305. /**
  1306. * vpif_remove() - driver remove handler
  1307. * @device: ptr to platform device structure
  1308. *
  1309. * The vidoe device is unregistered
  1310. */
  1311. static int vpif_remove(struct platform_device *device)
  1312. {
  1313. struct common_obj *common;
  1314. struct channel_obj *ch;
  1315. int i;
  1316. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1317. kfree(vpif_obj.sd);
  1318. /* un-register device */
  1319. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1320. /* Get the pointer to the channel object */
  1321. ch = vpif_obj.dev[i];
  1322. common = &ch->common[VPIF_VIDEO_INDEX];
  1323. /* Unregister video device */
  1324. video_unregister_device(&ch->video_dev);
  1325. kfree(vpif_obj.dev[i]);
  1326. }
  1327. return 0;
  1328. }
  1329. #ifdef CONFIG_PM_SLEEP
  1330. /**
  1331. * vpif_suspend: vpif device suspend
  1332. */
  1333. static int vpif_suspend(struct device *dev)
  1334. {
  1335. struct common_obj *common;
  1336. struct channel_obj *ch;
  1337. int i;
  1338. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1339. /* Get the pointer to the channel object */
  1340. ch = vpif_obj.dev[i];
  1341. common = &ch->common[VPIF_VIDEO_INDEX];
  1342. if (!vb2_start_streaming_called(&common->buffer_queue))
  1343. continue;
  1344. mutex_lock(&common->lock);
  1345. /* Disable channel */
  1346. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1347. enable_channel0(0);
  1348. channel0_intr_enable(0);
  1349. }
  1350. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1351. ycmux_mode == 2) {
  1352. enable_channel1(0);
  1353. channel1_intr_enable(0);
  1354. }
  1355. mutex_unlock(&common->lock);
  1356. }
  1357. return 0;
  1358. }
  1359. /*
  1360. * vpif_resume: vpif device suspend
  1361. */
  1362. static int vpif_resume(struct device *dev)
  1363. {
  1364. struct common_obj *common;
  1365. struct channel_obj *ch;
  1366. int i;
  1367. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1368. /* Get the pointer to the channel object */
  1369. ch = vpif_obj.dev[i];
  1370. common = &ch->common[VPIF_VIDEO_INDEX];
  1371. if (!vb2_start_streaming_called(&common->buffer_queue))
  1372. continue;
  1373. mutex_lock(&common->lock);
  1374. /* Enable channel */
  1375. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1376. enable_channel0(1);
  1377. channel0_intr_enable(1);
  1378. }
  1379. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1380. ycmux_mode == 2) {
  1381. enable_channel1(1);
  1382. channel1_intr_enable(1);
  1383. }
  1384. mutex_unlock(&common->lock);
  1385. }
  1386. return 0;
  1387. }
  1388. #endif
  1389. static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
  1390. static __refdata struct platform_driver vpif_driver = {
  1391. .driver = {
  1392. .name = VPIF_DRIVER_NAME,
  1393. .pm = &vpif_pm_ops,
  1394. },
  1395. .probe = vpif_probe,
  1396. .remove = vpif_remove,
  1397. };
  1398. module_platform_driver(vpif_driver);