vpif_capture.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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 i, 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;
  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. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1210. ch = vpif_obj.dev[i];
  1211. /* Note: does nothing if ch->video_dev == NULL */
  1212. video_device_release(ch->video_dev);
  1213. }
  1214. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1215. return err;
  1216. }
  1217. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1218. {
  1219. return vpif_probe_complete();
  1220. }
  1221. /**
  1222. * vpif_probe : This function probes the vpif capture driver
  1223. * @pdev: platform device pointer
  1224. *
  1225. * This creates device entries by register itself to the V4L2 driver and
  1226. * initializes fields of each channel objects
  1227. */
  1228. static __init int vpif_probe(struct platform_device *pdev)
  1229. {
  1230. struct vpif_subdev_info *subdevdata;
  1231. int i, j, err;
  1232. int res_idx = 0;
  1233. struct i2c_adapter *i2c_adap;
  1234. struct channel_obj *ch;
  1235. struct video_device *vfd;
  1236. struct resource *res;
  1237. int subdev_count;
  1238. vpif_dev = &pdev->dev;
  1239. err = initialize_vpif();
  1240. if (err) {
  1241. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1242. return err;
  1243. }
  1244. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1245. if (err) {
  1246. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1247. return err;
  1248. }
  1249. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1250. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1251. IRQF_SHARED, VPIF_DRIVER_NAME,
  1252. (void *)(&vpif_obj.dev[res_idx]->
  1253. channel_id));
  1254. if (err) {
  1255. err = -EINVAL;
  1256. goto vpif_unregister;
  1257. }
  1258. res_idx++;
  1259. }
  1260. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1261. /* Get the pointer to the channel object */
  1262. ch = vpif_obj.dev[i];
  1263. /* Allocate memory for video device */
  1264. vfd = video_device_alloc();
  1265. if (NULL == vfd) {
  1266. for (j = 0; j < i; j++) {
  1267. ch = vpif_obj.dev[j];
  1268. video_device_release(ch->video_dev);
  1269. }
  1270. err = -ENOMEM;
  1271. goto vpif_unregister;
  1272. }
  1273. /* Set video_dev to the video device */
  1274. ch->video_dev = vfd;
  1275. }
  1276. vpif_obj.config = pdev->dev.platform_data;
  1277. subdev_count = vpif_obj.config->subdev_count;
  1278. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1279. GFP_KERNEL);
  1280. if (vpif_obj.sd == NULL) {
  1281. vpif_err("unable to allocate memory for subdevice pointers\n");
  1282. err = -ENOMEM;
  1283. goto vpif_sd_error;
  1284. }
  1285. if (!vpif_obj.config->asd_sizes) {
  1286. i2c_adap = i2c_get_adapter(1);
  1287. for (i = 0; i < subdev_count; i++) {
  1288. subdevdata = &vpif_obj.config->subdev_info[i];
  1289. vpif_obj.sd[i] =
  1290. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1291. i2c_adap,
  1292. &subdevdata->
  1293. board_info,
  1294. NULL);
  1295. if (!vpif_obj.sd[i]) {
  1296. vpif_err("Error registering v4l2 subdevice\n");
  1297. err = -ENODEV;
  1298. goto probe_subdev_out;
  1299. }
  1300. v4l2_info(&vpif_obj.v4l2_dev,
  1301. "registered sub device %s\n",
  1302. subdevdata->name);
  1303. }
  1304. vpif_probe_complete();
  1305. } else {
  1306. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1307. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1308. vpif_obj.notifier.bound = vpif_async_bound;
  1309. vpif_obj.notifier.complete = vpif_async_complete;
  1310. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1311. &vpif_obj.notifier);
  1312. if (err) {
  1313. vpif_err("Error registering async notifier\n");
  1314. err = -EINVAL;
  1315. goto probe_subdev_out;
  1316. }
  1317. }
  1318. return 0;
  1319. probe_subdev_out:
  1320. /* free sub devices memory */
  1321. kfree(vpif_obj.sd);
  1322. vpif_sd_error:
  1323. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1324. ch = vpif_obj.dev[i];
  1325. /* Note: does nothing if ch->video_dev == NULL */
  1326. video_device_release(ch->video_dev);
  1327. }
  1328. vpif_unregister:
  1329. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1330. return err;
  1331. }
  1332. /**
  1333. * vpif_remove() - driver remove handler
  1334. * @device: ptr to platform device structure
  1335. *
  1336. * The vidoe device is unregistered
  1337. */
  1338. static int vpif_remove(struct platform_device *device)
  1339. {
  1340. struct common_obj *common;
  1341. struct channel_obj *ch;
  1342. int i;
  1343. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1344. kfree(vpif_obj.sd);
  1345. /* un-register device */
  1346. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1347. /* Get the pointer to the channel object */
  1348. ch = vpif_obj.dev[i];
  1349. common = &ch->common[VPIF_VIDEO_INDEX];
  1350. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  1351. /* Unregister video device */
  1352. video_unregister_device(ch->video_dev);
  1353. kfree(vpif_obj.dev[i]);
  1354. }
  1355. return 0;
  1356. }
  1357. #ifdef CONFIG_PM_SLEEP
  1358. /**
  1359. * vpif_suspend: vpif device suspend
  1360. */
  1361. static int vpif_suspend(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. /* Disable channel */
  1374. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1375. enable_channel0(0);
  1376. channel0_intr_enable(0);
  1377. }
  1378. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1379. ycmux_mode == 2) {
  1380. enable_channel1(0);
  1381. channel1_intr_enable(0);
  1382. }
  1383. mutex_unlock(&common->lock);
  1384. }
  1385. return 0;
  1386. }
  1387. /*
  1388. * vpif_resume: vpif device suspend
  1389. */
  1390. static int vpif_resume(struct device *dev)
  1391. {
  1392. struct common_obj *common;
  1393. struct channel_obj *ch;
  1394. int i;
  1395. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1396. /* Get the pointer to the channel object */
  1397. ch = vpif_obj.dev[i];
  1398. common = &ch->common[VPIF_VIDEO_INDEX];
  1399. if (!vb2_start_streaming_called(&common->buffer_queue))
  1400. continue;
  1401. mutex_lock(&common->lock);
  1402. /* Enable channel */
  1403. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1404. enable_channel0(1);
  1405. channel0_intr_enable(1);
  1406. }
  1407. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1408. ycmux_mode == 2) {
  1409. enable_channel1(1);
  1410. channel1_intr_enable(1);
  1411. }
  1412. mutex_unlock(&common->lock);
  1413. }
  1414. return 0;
  1415. }
  1416. #endif
  1417. static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
  1418. static __refdata struct platform_driver vpif_driver = {
  1419. .driver = {
  1420. .name = VPIF_DRIVER_NAME,
  1421. .pm = &vpif_pm_ops,
  1422. },
  1423. .probe = vpif_probe,
  1424. .remove = vpif_remove,
  1425. };
  1426. module_platform_driver(vpif_driver);