vpif_capture.c 44 KB

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