vpif_capture.c 49 KB

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