vpif_capture.c 44 KB

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