vpif_capture.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  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. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  816. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  817. } else {
  818. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  819. strcpy(fmt->description, "YCbCr4:2:2 Semi-Planar");
  820. fmt->pixelformat = V4L2_PIX_FMT_NV16;
  821. }
  822. return 0;
  823. }
  824. /**
  825. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  826. * @file: file ptr
  827. * @priv: file handle
  828. * @fmt: ptr to v4l2 format structure
  829. */
  830. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  831. struct v4l2_format *fmt)
  832. {
  833. struct video_device *vdev = video_devdata(file);
  834. struct channel_obj *ch = video_get_drvdata(vdev);
  835. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  836. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  837. common->fmt = *fmt;
  838. vpif_update_std_info(ch);
  839. pixfmt->field = common->fmt.fmt.pix.field;
  840. pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
  841. pixfmt->bytesperline = common->fmt.fmt.pix.width;
  842. pixfmt->width = common->fmt.fmt.pix.width;
  843. pixfmt->height = common->fmt.fmt.pix.height;
  844. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
  845. if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) {
  846. pixfmt->bytesperline = common->fmt.fmt.pix.width * 2;
  847. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  848. }
  849. pixfmt->priv = 0;
  850. dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d pixelformat=0x%08x, field=%d, size=%d\n", __func__,
  851. pixfmt->width, pixfmt->height,
  852. pixfmt->bytesperline, pixfmt->pixelformat,
  853. pixfmt->field, pixfmt->sizeimage);
  854. return 0;
  855. }
  856. /**
  857. * vpif_g_fmt_vid_cap() - Set INPUT handler
  858. * @file: file ptr
  859. * @priv: file handle
  860. * @fmt: ptr to v4l2 format structure
  861. */
  862. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  863. struct v4l2_format *fmt)
  864. {
  865. struct video_device *vdev = video_devdata(file);
  866. struct channel_obj *ch = video_get_drvdata(vdev);
  867. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  868. struct v4l2_pix_format *pix_fmt = &fmt->fmt.pix;
  869. struct v4l2_subdev_format format = {
  870. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  871. };
  872. struct v4l2_mbus_framefmt *mbus_fmt = &format.format;
  873. int ret;
  874. /* Check the validity of the buffer type */
  875. if (common->fmt.type != fmt->type)
  876. return -EINVAL;
  877. /* By default, use currently set fmt */
  878. *fmt = common->fmt;
  879. /* If subdev has get_fmt, use that to override */
  880. ret = v4l2_subdev_call(ch->sd, pad, get_fmt, NULL, &format);
  881. if (!ret && mbus_fmt->code) {
  882. v4l2_fill_pix_format(pix_fmt, mbus_fmt);
  883. pix_fmt->bytesperline = pix_fmt->width;
  884. if (mbus_fmt->code == MEDIA_BUS_FMT_SGRBG10_1X10) {
  885. /* e.g. mt9v032 */
  886. pix_fmt->pixelformat = V4L2_PIX_FMT_SGRBG10;
  887. pix_fmt->bytesperline = pix_fmt->width * 2;
  888. } else if (mbus_fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
  889. /* e.g. tvp514x */
  890. pix_fmt->pixelformat = V4L2_PIX_FMT_NV16;
  891. pix_fmt->bytesperline = pix_fmt->width * 2;
  892. } else {
  893. dev_warn(vpif_dev, "%s: Unhandled media-bus format 0x%x\n",
  894. __func__, mbus_fmt->code);
  895. }
  896. pix_fmt->sizeimage = pix_fmt->bytesperline * pix_fmt->height;
  897. dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d, pixelformat=0x%08x, code=0x%x, field=%d, size=%d\n", __func__,
  898. pix_fmt->width, pix_fmt->height,
  899. pix_fmt->bytesperline, pix_fmt->pixelformat,
  900. mbus_fmt->code, pix_fmt->field, pix_fmt->sizeimage);
  901. common->fmt = *fmt;
  902. vpif_update_std_info(ch);
  903. }
  904. return 0;
  905. }
  906. /**
  907. * vpif_s_fmt_vid_cap() - Set FMT handler
  908. * @file: file ptr
  909. * @priv: file handle
  910. * @fmt: ptr to v4l2 format structure
  911. */
  912. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  913. struct v4l2_format *fmt)
  914. {
  915. struct video_device *vdev = video_devdata(file);
  916. struct channel_obj *ch = video_get_drvdata(vdev);
  917. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  918. int ret;
  919. vpif_dbg(2, debug, "%s\n", __func__);
  920. if (vb2_is_busy(&common->buffer_queue))
  921. return -EBUSY;
  922. ret = vpif_try_fmt_vid_cap(file, priv, fmt);
  923. if (ret)
  924. return ret;
  925. /* store the format in the channel object */
  926. common->fmt = *fmt;
  927. return 0;
  928. }
  929. /**
  930. * vpif_querycap() - QUERYCAP handler
  931. * @file: file ptr
  932. * @priv: file handle
  933. * @cap: ptr to v4l2_capability structure
  934. */
  935. static int vpif_querycap(struct file *file, void *priv,
  936. struct v4l2_capability *cap)
  937. {
  938. struct vpif_capture_config *config = vpif_dev->platform_data;
  939. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  940. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  941. strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
  942. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  943. dev_name(vpif_dev));
  944. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  945. return 0;
  946. }
  947. /**
  948. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  949. * @file: file ptr
  950. * @priv: file handle
  951. * @timings: input timings
  952. */
  953. static int
  954. vpif_enum_dv_timings(struct file *file, void *priv,
  955. struct v4l2_enum_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. timings->pad = 0;
  970. ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
  971. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  972. return -EINVAL;
  973. return ret;
  974. }
  975. /**
  976. * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
  977. * @file: file ptr
  978. * @priv: file handle
  979. * @timings: input timings
  980. */
  981. static int
  982. vpif_query_dv_timings(struct file *file, void *priv,
  983. struct v4l2_dv_timings *timings)
  984. {
  985. struct vpif_capture_config *config = vpif_dev->platform_data;
  986. struct video_device *vdev = video_devdata(file);
  987. struct channel_obj *ch = video_get_drvdata(vdev);
  988. struct vpif_capture_chan_config *chan_cfg;
  989. struct v4l2_input input;
  990. int ret;
  991. if (!config->chan_config[ch->channel_id].inputs)
  992. return -ENODATA;
  993. chan_cfg = &config->chan_config[ch->channel_id];
  994. input = chan_cfg->inputs[ch->input_idx].input;
  995. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  996. return -ENODATA;
  997. ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
  998. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  999. return -ENODATA;
  1000. return ret;
  1001. }
  1002. /**
  1003. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1004. * @file: file ptr
  1005. * @priv: file handle
  1006. * @timings: digital video timings
  1007. */
  1008. static int vpif_s_dv_timings(struct file *file, void *priv,
  1009. struct v4l2_dv_timings *timings)
  1010. {
  1011. struct vpif_capture_config *config = vpif_dev->platform_data;
  1012. struct video_device *vdev = video_devdata(file);
  1013. struct channel_obj *ch = video_get_drvdata(vdev);
  1014. struct vpif_params *vpifparams = &ch->vpifparams;
  1015. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1016. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1017. struct video_obj *vid_ch = &ch->video;
  1018. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  1019. struct vpif_capture_chan_config *chan_cfg;
  1020. struct v4l2_input input;
  1021. int ret;
  1022. if (!config->chan_config[ch->channel_id].inputs)
  1023. return -ENODATA;
  1024. chan_cfg = &config->chan_config[ch->channel_id];
  1025. input = chan_cfg->inputs[ch->input_idx].input;
  1026. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  1027. return -ENODATA;
  1028. if (timings->type != V4L2_DV_BT_656_1120) {
  1029. vpif_dbg(2, debug, "Timing type not defined\n");
  1030. return -EINVAL;
  1031. }
  1032. if (vb2_is_busy(&common->buffer_queue))
  1033. return -EBUSY;
  1034. /* Configure subdevice timings, if any */
  1035. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  1036. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1037. ret = 0;
  1038. if (ret < 0) {
  1039. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1040. return ret;
  1041. }
  1042. if (!(timings->bt.width && timings->bt.height &&
  1043. (timings->bt.hbackporch ||
  1044. timings->bt.hfrontporch ||
  1045. timings->bt.hsync) &&
  1046. timings->bt.vfrontporch &&
  1047. (timings->bt.vbackporch ||
  1048. timings->bt.vsync))) {
  1049. 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");
  1050. return -EINVAL;
  1051. }
  1052. vid_ch->dv_timings = *timings;
  1053. /* Configure video port timings */
  1054. std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
  1055. std_info->sav2eav = bt->width;
  1056. std_info->l1 = 1;
  1057. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1058. std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  1059. if (bt->interlaced) {
  1060. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1061. std_info->l5 = std_info->vsize/2 -
  1062. (bt->vfrontporch - 1);
  1063. std_info->l7 = std_info->vsize/2 + 1;
  1064. std_info->l9 = std_info->l7 + bt->il_vsync +
  1065. bt->il_vbackporch + 1;
  1066. std_info->l11 = std_info->vsize -
  1067. (bt->il_vfrontporch - 1);
  1068. } else {
  1069. vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
  1070. return -EINVAL;
  1071. }
  1072. } else {
  1073. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1074. }
  1075. strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
  1076. std_info->width = bt->width;
  1077. std_info->height = bt->height;
  1078. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1079. std_info->ycmux_mode = 0;
  1080. std_info->capture_format = 0;
  1081. std_info->vbi_supported = 0;
  1082. std_info->hd_sd = 1;
  1083. std_info->stdid = 0;
  1084. vid_ch->stdid = 0;
  1085. return 0;
  1086. }
  1087. /**
  1088. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1089. * @file: file ptr
  1090. * @priv: file handle
  1091. * @timings: digital video timings
  1092. */
  1093. static int vpif_g_dv_timings(struct file *file, void *priv,
  1094. struct v4l2_dv_timings *timings)
  1095. {
  1096. struct vpif_capture_config *config = vpif_dev->platform_data;
  1097. struct video_device *vdev = video_devdata(file);
  1098. struct channel_obj *ch = video_get_drvdata(vdev);
  1099. struct video_obj *vid_ch = &ch->video;
  1100. struct vpif_capture_chan_config *chan_cfg;
  1101. struct v4l2_input input;
  1102. if (!config->chan_config[ch->channel_id].inputs)
  1103. return -ENODATA;
  1104. chan_cfg = &config->chan_config[ch->channel_id];
  1105. input = chan_cfg->inputs[ch->input_idx].input;
  1106. if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
  1107. return -ENODATA;
  1108. *timings = vid_ch->dv_timings;
  1109. return 0;
  1110. }
  1111. /*
  1112. * vpif_log_status() - Status information
  1113. * @file: file ptr
  1114. * @priv: file handle
  1115. *
  1116. * Returns zero.
  1117. */
  1118. static int vpif_log_status(struct file *filep, void *priv)
  1119. {
  1120. /* status for sub devices */
  1121. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1122. return 0;
  1123. }
  1124. /* vpif capture ioctl operations */
  1125. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1126. .vidioc_querycap = vpif_querycap,
  1127. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1128. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1129. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1130. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1131. .vidioc_enum_input = vpif_enum_input,
  1132. .vidioc_s_input = vpif_s_input,
  1133. .vidioc_g_input = vpif_g_input,
  1134. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1135. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1136. .vidioc_querybuf = vb2_ioctl_querybuf,
  1137. .vidioc_qbuf = vb2_ioctl_qbuf,
  1138. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1139. .vidioc_expbuf = vb2_ioctl_expbuf,
  1140. .vidioc_streamon = vb2_ioctl_streamon,
  1141. .vidioc_streamoff = vb2_ioctl_streamoff,
  1142. .vidioc_querystd = vpif_querystd,
  1143. .vidioc_s_std = vpif_s_std,
  1144. .vidioc_g_std = vpif_g_std,
  1145. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1146. .vidioc_query_dv_timings = vpif_query_dv_timings,
  1147. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1148. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1149. .vidioc_log_status = vpif_log_status,
  1150. };
  1151. /* vpif file operations */
  1152. static const struct v4l2_file_operations vpif_fops = {
  1153. .owner = THIS_MODULE,
  1154. .open = v4l2_fh_open,
  1155. .release = vb2_fop_release,
  1156. .unlocked_ioctl = video_ioctl2,
  1157. .mmap = vb2_fop_mmap,
  1158. .poll = vb2_fop_poll
  1159. };
  1160. /**
  1161. * initialize_vpif() - Initialize vpif data structures
  1162. *
  1163. * Allocate memory for data structures and initialize them
  1164. */
  1165. static int initialize_vpif(void)
  1166. {
  1167. int err, i, j;
  1168. int free_channel_objects_index;
  1169. /* Allocate memory for six channel objects */
  1170. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1171. vpif_obj.dev[i] =
  1172. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1173. /* If memory allocation fails, return error */
  1174. if (!vpif_obj.dev[i]) {
  1175. free_channel_objects_index = i;
  1176. err = -ENOMEM;
  1177. goto vpif_init_free_channel_objects;
  1178. }
  1179. }
  1180. return 0;
  1181. vpif_init_free_channel_objects:
  1182. for (j = 0; j < free_channel_objects_index; j++)
  1183. kfree(vpif_obj.dev[j]);
  1184. return err;
  1185. }
  1186. static int vpif_async_bound(struct v4l2_async_notifier *notifier,
  1187. struct v4l2_subdev *subdev,
  1188. struct v4l2_async_subdev *asd)
  1189. {
  1190. int i;
  1191. for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
  1192. struct v4l2_async_subdev *_asd = vpif_obj.config->asd[i];
  1193. const struct fwnode_handle *fwnode = _asd->match.fwnode;
  1194. if (fwnode == subdev->fwnode) {
  1195. vpif_obj.sd[i] = subdev;
  1196. vpif_obj.config->chan_config->inputs[i].subdev_name =
  1197. (char *)to_of_node(subdev->fwnode)->full_name;
  1198. vpif_dbg(2, debug,
  1199. "%s: setting input %d subdev_name = %s\n",
  1200. __func__, i,
  1201. vpif_obj.config->chan_config->inputs[i].subdev_name);
  1202. return 0;
  1203. }
  1204. }
  1205. for (i = 0; i < vpif_obj.config->subdev_count; i++)
  1206. if (!strcmp(vpif_obj.config->subdev_info[i].name,
  1207. subdev->name)) {
  1208. vpif_obj.sd[i] = subdev;
  1209. return 0;
  1210. }
  1211. return -EINVAL;
  1212. }
  1213. static int vpif_probe_complete(void)
  1214. {
  1215. struct common_obj *common;
  1216. struct video_device *vdev;
  1217. struct channel_obj *ch;
  1218. struct vb2_queue *q;
  1219. int j, err, k;
  1220. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1221. ch = vpif_obj.dev[j];
  1222. ch->channel_id = j;
  1223. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1224. spin_lock_init(&common->irqlock);
  1225. mutex_init(&common->lock);
  1226. /* select input 0 */
  1227. err = vpif_set_input(vpif_obj.config, ch, 0);
  1228. if (err)
  1229. goto probe_out;
  1230. /* set initial format */
  1231. ch->video.stdid = V4L2_STD_525_60;
  1232. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  1233. common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1234. vpif_update_std_info(ch);
  1235. /* Initialize vb2 queue */
  1236. q = &common->buffer_queue;
  1237. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1238. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1239. q->drv_priv = ch;
  1240. q->ops = &video_qops;
  1241. q->mem_ops = &vb2_dma_contig_memops;
  1242. q->buf_struct_size = sizeof(struct vpif_cap_buffer);
  1243. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1244. q->min_buffers_needed = 1;
  1245. q->lock = &common->lock;
  1246. q->dev = vpif_dev;
  1247. err = vb2_queue_init(q);
  1248. if (err) {
  1249. vpif_err("vpif_capture: vb2_queue_init() failed\n");
  1250. goto probe_out;
  1251. }
  1252. INIT_LIST_HEAD(&common->dma_queue);
  1253. /* Initialize the video_device structure */
  1254. vdev = &ch->video_dev;
  1255. strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
  1256. vdev->release = video_device_release_empty;
  1257. vdev->fops = &vpif_fops;
  1258. vdev->ioctl_ops = &vpif_ioctl_ops;
  1259. vdev->v4l2_dev = &vpif_obj.v4l2_dev;
  1260. vdev->vfl_dir = VFL_DIR_RX;
  1261. vdev->queue = q;
  1262. vdev->lock = &common->lock;
  1263. video_set_drvdata(&ch->video_dev, ch);
  1264. err = video_register_device(vdev,
  1265. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1266. if (err)
  1267. goto probe_out;
  1268. }
  1269. v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
  1270. return 0;
  1271. probe_out:
  1272. for (k = 0; k < j; k++) {
  1273. /* Get the pointer to the channel object */
  1274. ch = vpif_obj.dev[k];
  1275. common = &ch->common[k];
  1276. /* Unregister video device */
  1277. video_unregister_device(&ch->video_dev);
  1278. }
  1279. kfree(vpif_obj.sd);
  1280. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1281. return err;
  1282. }
  1283. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1284. {
  1285. return vpif_probe_complete();
  1286. }
  1287. static const struct v4l2_async_notifier_operations vpif_async_ops = {
  1288. .bound = vpif_async_bound,
  1289. .complete = vpif_async_complete,
  1290. };
  1291. static struct vpif_capture_config *
  1292. vpif_capture_get_pdata(struct platform_device *pdev)
  1293. {
  1294. struct device_node *endpoint = NULL;
  1295. struct v4l2_fwnode_endpoint bus_cfg;
  1296. struct vpif_capture_config *pdata;
  1297. struct vpif_subdev_info *sdinfo;
  1298. struct vpif_capture_chan_config *chan;
  1299. unsigned int i;
  1300. /*
  1301. * DT boot: OF node from parent device contains
  1302. * video ports & endpoints data.
  1303. */
  1304. if (pdev->dev.parent && pdev->dev.parent->of_node)
  1305. pdev->dev.of_node = pdev->dev.parent->of_node;
  1306. if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
  1307. return pdev->dev.platform_data;
  1308. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1309. if (!pdata)
  1310. return NULL;
  1311. pdata->subdev_info =
  1312. devm_kzalloc(&pdev->dev, sizeof(*pdata->subdev_info) *
  1313. VPIF_CAPTURE_NUM_CHANNELS, GFP_KERNEL);
  1314. if (!pdata->subdev_info)
  1315. return NULL;
  1316. for (i = 0; i < VPIF_CAPTURE_NUM_CHANNELS; i++) {
  1317. struct device_node *rem;
  1318. unsigned int flags;
  1319. int err;
  1320. endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
  1321. endpoint);
  1322. if (!endpoint)
  1323. break;
  1324. sdinfo = &pdata->subdev_info[i];
  1325. chan = &pdata->chan_config[i];
  1326. chan->inputs = devm_kzalloc(&pdev->dev,
  1327. sizeof(*chan->inputs) *
  1328. VPIF_CAPTURE_NUM_CHANNELS,
  1329. GFP_KERNEL);
  1330. if (!chan->inputs)
  1331. return NULL;
  1332. chan->input_count++;
  1333. chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA;
  1334. chan->inputs[i].input.std = V4L2_STD_ALL;
  1335. chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD;
  1336. err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
  1337. &bus_cfg);
  1338. if (err) {
  1339. dev_err(&pdev->dev, "Could not parse the endpoint\n");
  1340. goto done;
  1341. }
  1342. dev_dbg(&pdev->dev, "Endpoint %pOF, bus_width = %d\n",
  1343. endpoint, bus_cfg.bus.parallel.bus_width);
  1344. flags = bus_cfg.bus.parallel.flags;
  1345. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  1346. chan->vpif_if.hd_pol = 1;
  1347. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  1348. chan->vpif_if.vd_pol = 1;
  1349. rem = of_graph_get_remote_port_parent(endpoint);
  1350. if (!rem) {
  1351. dev_dbg(&pdev->dev, "Remote device at %pOF not found\n",
  1352. endpoint);
  1353. goto done;
  1354. }
  1355. dev_dbg(&pdev->dev, "Remote device %s, %pOF found\n",
  1356. rem->name, rem);
  1357. sdinfo->name = rem->full_name;
  1358. pdata->asd[i] = devm_kzalloc(&pdev->dev,
  1359. sizeof(struct v4l2_async_subdev),
  1360. GFP_KERNEL);
  1361. if (!pdata->asd[i]) {
  1362. of_node_put(rem);
  1363. pdata = NULL;
  1364. goto done;
  1365. }
  1366. pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_FWNODE;
  1367. pdata->asd[i]->match.fwnode = of_fwnode_handle(rem);
  1368. of_node_put(rem);
  1369. }
  1370. done:
  1371. if (pdata) {
  1372. pdata->asd_sizes[0] = i;
  1373. pdata->subdev_count = i;
  1374. pdata->card_name = "DA850/OMAP-L138 Video Capture";
  1375. }
  1376. return pdata;
  1377. }
  1378. /**
  1379. * vpif_probe : This function probes the vpif capture driver
  1380. * @pdev: platform device pointer
  1381. *
  1382. * This creates device entries by register itself to the V4L2 driver and
  1383. * initializes fields of each channel objects
  1384. */
  1385. static __init int vpif_probe(struct platform_device *pdev)
  1386. {
  1387. struct vpif_subdev_info *subdevdata;
  1388. struct i2c_adapter *i2c_adap;
  1389. struct resource *res;
  1390. int subdev_count;
  1391. int res_idx = 0;
  1392. int i, err;
  1393. pdev->dev.platform_data = vpif_capture_get_pdata(pdev);
  1394. if (!pdev->dev.platform_data) {
  1395. dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
  1396. return -EINVAL;
  1397. }
  1398. if (!pdev->dev.platform_data) {
  1399. dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
  1400. return -EINVAL;
  1401. }
  1402. vpif_dev = &pdev->dev;
  1403. err = initialize_vpif();
  1404. if (err) {
  1405. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1406. return err;
  1407. }
  1408. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1409. if (err) {
  1410. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1411. return err;
  1412. }
  1413. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1414. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1415. IRQF_SHARED, VPIF_DRIVER_NAME,
  1416. (void *)(&vpif_obj.dev[res_idx]->
  1417. channel_id));
  1418. if (err) {
  1419. err = -EINVAL;
  1420. goto vpif_unregister;
  1421. }
  1422. res_idx++;
  1423. }
  1424. vpif_obj.config = pdev->dev.platform_data;
  1425. subdev_count = vpif_obj.config->subdev_count;
  1426. vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
  1427. if (!vpif_obj.sd) {
  1428. err = -ENOMEM;
  1429. goto vpif_unregister;
  1430. }
  1431. if (!vpif_obj.config->asd_sizes[0]) {
  1432. int i2c_id = vpif_obj.config->i2c_adapter_id;
  1433. i2c_adap = i2c_get_adapter(i2c_id);
  1434. WARN_ON(!i2c_adap);
  1435. for (i = 0; i < subdev_count; i++) {
  1436. subdevdata = &vpif_obj.config->subdev_info[i];
  1437. vpif_obj.sd[i] =
  1438. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1439. i2c_adap,
  1440. &subdevdata->
  1441. board_info,
  1442. NULL);
  1443. if (!vpif_obj.sd[i]) {
  1444. vpif_err("Error registering v4l2 subdevice\n");
  1445. err = -ENODEV;
  1446. goto probe_subdev_out;
  1447. }
  1448. v4l2_info(&vpif_obj.v4l2_dev,
  1449. "registered sub device %s\n",
  1450. subdevdata->name);
  1451. }
  1452. vpif_probe_complete();
  1453. } else {
  1454. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1455. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1456. vpif_obj.notifier.ops = &vpif_async_ops;
  1457. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1458. &vpif_obj.notifier);
  1459. if (err) {
  1460. vpif_err("Error registering async notifier\n");
  1461. err = -EINVAL;
  1462. goto probe_subdev_out;
  1463. }
  1464. }
  1465. return 0;
  1466. probe_subdev_out:
  1467. /* free sub devices memory */
  1468. kfree(vpif_obj.sd);
  1469. vpif_unregister:
  1470. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1471. return err;
  1472. }
  1473. /**
  1474. * vpif_remove() - driver remove handler
  1475. * @device: ptr to platform device structure
  1476. *
  1477. * The vidoe device is unregistered
  1478. */
  1479. static int vpif_remove(struct platform_device *device)
  1480. {
  1481. struct channel_obj *ch;
  1482. int i;
  1483. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1484. kfree(vpif_obj.sd);
  1485. /* un-register device */
  1486. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1487. /* Get the pointer to the channel object */
  1488. ch = vpif_obj.dev[i];
  1489. /* Unregister video device */
  1490. video_unregister_device(&ch->video_dev);
  1491. kfree(vpif_obj.dev[i]);
  1492. }
  1493. return 0;
  1494. }
  1495. #ifdef CONFIG_PM_SLEEP
  1496. /**
  1497. * vpif_suspend: vpif device suspend
  1498. * @dev: pointer to &struct device
  1499. */
  1500. static int vpif_suspend(struct device *dev)
  1501. {
  1502. struct common_obj *common;
  1503. struct channel_obj *ch;
  1504. int i;
  1505. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1506. /* Get the pointer to the channel object */
  1507. ch = vpif_obj.dev[i];
  1508. common = &ch->common[VPIF_VIDEO_INDEX];
  1509. if (!vb2_start_streaming_called(&common->buffer_queue))
  1510. continue;
  1511. mutex_lock(&common->lock);
  1512. /* Disable channel */
  1513. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1514. enable_channel0(0);
  1515. channel0_intr_enable(0);
  1516. }
  1517. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1518. ycmux_mode == 2) {
  1519. enable_channel1(0);
  1520. channel1_intr_enable(0);
  1521. }
  1522. mutex_unlock(&common->lock);
  1523. }
  1524. return 0;
  1525. }
  1526. /*
  1527. * vpif_resume: vpif device suspend
  1528. */
  1529. static int vpif_resume(struct device *dev)
  1530. {
  1531. struct common_obj *common;
  1532. struct channel_obj *ch;
  1533. int i;
  1534. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1535. /* Get the pointer to the channel object */
  1536. ch = vpif_obj.dev[i];
  1537. common = &ch->common[VPIF_VIDEO_INDEX];
  1538. if (!vb2_start_streaming_called(&common->buffer_queue))
  1539. continue;
  1540. mutex_lock(&common->lock);
  1541. /* Enable channel */
  1542. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1543. enable_channel0(1);
  1544. channel0_intr_enable(1);
  1545. }
  1546. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1547. ycmux_mode == 2) {
  1548. enable_channel1(1);
  1549. channel1_intr_enable(1);
  1550. }
  1551. mutex_unlock(&common->lock);
  1552. }
  1553. return 0;
  1554. }
  1555. #endif
  1556. static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
  1557. static __refdata struct platform_driver vpif_driver = {
  1558. .driver = {
  1559. .name = VPIF_DRIVER_NAME,
  1560. .pm = &vpif_pm_ops,
  1561. },
  1562. .probe = vpif_probe,
  1563. .remove = vpif_remove,
  1564. };
  1565. module_platform_driver(vpif_driver);