vpif_capture.c 48 KB

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