vpif_capture.c 49 KB

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