vpif_capture.c 43 KB

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