sta2x11_vip.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * This is the driver for the STA2x11 Video Input Port.
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * author: Federico Vaga <federico.vaga@gmail.com>
  6. * Copyright (C) 2010 WindRiver Systems, Inc.
  7. * authors: Andreas Kies <andreas.kies@windriver.com>
  8. * Vlad Lungu <vlad.lungu@windriver.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * The full GNU General Public License is included in this distribution in
  24. * the file called "COPYING".
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/kmod.h>
  33. #include <linux/pci.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/gpio.h>
  37. #include <linux/i2c.h>
  38. #include <linux/delay.h>
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-device.h>
  41. #include <media/v4l2-ctrls.h>
  42. #include <media/v4l2-ioctl.h>
  43. #include <media/v4l2-fh.h>
  44. #include <media/v4l2-event.h>
  45. #include <media/videobuf2-dma-contig.h>
  46. #include "sta2x11_vip.h"
  47. #define DRV_VERSION "1.3"
  48. #ifndef PCI_DEVICE_ID_STMICRO_VIP
  49. #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
  50. #endif
  51. #define MAX_FRAMES 4
  52. /*Register offsets*/
  53. #define DVP_CTL 0x00
  54. #define DVP_TFO 0x04
  55. #define DVP_TFS 0x08
  56. #define DVP_BFO 0x0C
  57. #define DVP_BFS 0x10
  58. #define DVP_VTP 0x14
  59. #define DVP_VBP 0x18
  60. #define DVP_VMP 0x1C
  61. #define DVP_ITM 0x98
  62. #define DVP_ITS 0x9C
  63. #define DVP_STA 0xA0
  64. #define DVP_HLFLN 0xA8
  65. #define DVP_RGB 0xC0
  66. #define DVP_PKZ 0xF0
  67. /*Register fields*/
  68. #define DVP_CTL_ENA 0x00000001
  69. #define DVP_CTL_RST 0x80000000
  70. #define DVP_CTL_DIS (~0x00040001)
  71. #define DVP_IT_VSB 0x00000008
  72. #define DVP_IT_VST 0x00000010
  73. #define DVP_IT_FIFO 0x00000020
  74. #define DVP_HLFLN_SD 0x00000001
  75. #define SAVE_COUNT 8
  76. #define AUX_COUNT 3
  77. #define IRQ_COUNT 1
  78. struct vip_buffer {
  79. struct vb2_v4l2_buffer vb;
  80. struct list_head list;
  81. dma_addr_t dma;
  82. };
  83. static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
  84. {
  85. return container_of(vb2, struct vip_buffer, vb);
  86. }
  87. /**
  88. * struct sta2x11_vip - All internal data for one instance of device
  89. * @v4l2_dev: device registered in v4l layer
  90. * @video_dev: properties of our device
  91. * @pdev: PCI device
  92. * @adapter: contains I2C adapter information
  93. * @register_save_area: All relevant register are saved here during suspend
  94. * @decoder: contains information about video DAC
  95. * @ctrl_hdl: handler for control framework
  96. * @format: pixel format, fixed UYVY
  97. * @std: video standard (e.g. PAL/NTSC)
  98. * @input: input line for video signal ( 0 or 1 )
  99. * @disabled: Device is in power down state
  100. * @slock: for excluse acces of registers
  101. * @vb_vidq: queue maintained by videobuf2 layer
  102. * @buffer_list: list of buffer in use
  103. * @sequence: sequence number of acquired buffer
  104. * @active: current active buffer
  105. * @lock: used in videobuf2 callback
  106. * @v4l_lock: serialize its video4linux ioctls
  107. * @tcount: Number of top frames
  108. * @bcount: Number of bottom frames
  109. * @overflow: Number of FIFO overflows
  110. * @iomem: hardware base address
  111. * @config: I2C and gpio config from platform
  112. *
  113. * All non-local data is accessed via this structure.
  114. */
  115. struct sta2x11_vip {
  116. struct v4l2_device v4l2_dev;
  117. struct video_device video_dev;
  118. struct pci_dev *pdev;
  119. struct i2c_adapter *adapter;
  120. unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
  121. struct v4l2_subdev *decoder;
  122. struct v4l2_ctrl_handler ctrl_hdl;
  123. struct v4l2_pix_format format;
  124. v4l2_std_id std;
  125. unsigned int input;
  126. int disabled;
  127. spinlock_t slock;
  128. struct vb2_queue vb_vidq;
  129. struct list_head buffer_list;
  130. unsigned int sequence;
  131. struct vip_buffer *active; /* current active buffer */
  132. spinlock_t lock; /* Used in videobuf2 callback */
  133. struct mutex v4l_lock;
  134. /* Interrupt counters */
  135. int tcount, bcount;
  136. int overflow;
  137. void __iomem *iomem; /* I/O Memory */
  138. struct vip_config *config;
  139. };
  140. static const unsigned int registers_to_save[AUX_COUNT] = {
  141. DVP_HLFLN, DVP_RGB, DVP_PKZ
  142. };
  143. static struct v4l2_pix_format formats_50[] = {
  144. { /*PAL interlaced */
  145. .width = 720,
  146. .height = 576,
  147. .pixelformat = V4L2_PIX_FMT_UYVY,
  148. .field = V4L2_FIELD_INTERLACED,
  149. .bytesperline = 720 * 2,
  150. .sizeimage = 720 * 2 * 576,
  151. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  152. { /*PAL top */
  153. .width = 720,
  154. .height = 288,
  155. .pixelformat = V4L2_PIX_FMT_UYVY,
  156. .field = V4L2_FIELD_TOP,
  157. .bytesperline = 720 * 2,
  158. .sizeimage = 720 * 2 * 288,
  159. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  160. { /*PAL bottom */
  161. .width = 720,
  162. .height = 288,
  163. .pixelformat = V4L2_PIX_FMT_UYVY,
  164. .field = V4L2_FIELD_BOTTOM,
  165. .bytesperline = 720 * 2,
  166. .sizeimage = 720 * 2 * 288,
  167. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  168. };
  169. static struct v4l2_pix_format formats_60[] = {
  170. { /*NTSC interlaced */
  171. .width = 720,
  172. .height = 480,
  173. .pixelformat = V4L2_PIX_FMT_UYVY,
  174. .field = V4L2_FIELD_INTERLACED,
  175. .bytesperline = 720 * 2,
  176. .sizeimage = 720 * 2 * 480,
  177. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  178. { /*NTSC top */
  179. .width = 720,
  180. .height = 240,
  181. .pixelformat = V4L2_PIX_FMT_UYVY,
  182. .field = V4L2_FIELD_TOP,
  183. .bytesperline = 720 * 2,
  184. .sizeimage = 720 * 2 * 240,
  185. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  186. { /*NTSC bottom */
  187. .width = 720,
  188. .height = 240,
  189. .pixelformat = V4L2_PIX_FMT_UYVY,
  190. .field = V4L2_FIELD_BOTTOM,
  191. .bytesperline = 720 * 2,
  192. .sizeimage = 720 * 2 * 240,
  193. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  194. };
  195. /* Write VIP register */
  196. static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
  197. {
  198. iowrite32((val), (vip->iomem)+(reg));
  199. }
  200. /* Read VIP register */
  201. static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
  202. {
  203. return ioread32((vip->iomem)+(reg));
  204. }
  205. /* Start DMA acquisition */
  206. static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
  207. {
  208. unsigned long offset = 0;
  209. if (vip->format.field == V4L2_FIELD_INTERLACED)
  210. offset = vip->format.width * 2;
  211. spin_lock_irq(&vip->slock);
  212. /* Enable acquisition */
  213. reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
  214. /* Set Top and Bottom Field memory address */
  215. reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
  216. reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
  217. spin_unlock_irq(&vip->slock);
  218. }
  219. /* Fetch the next buffer to activate */
  220. static void vip_active_buf_next(struct sta2x11_vip *vip)
  221. {
  222. /* Get the next buffer */
  223. spin_lock(&vip->lock);
  224. if (list_empty(&vip->buffer_list)) {/* No available buffer */
  225. spin_unlock(&vip->lock);
  226. return;
  227. }
  228. vip->active = list_first_entry(&vip->buffer_list,
  229. struct vip_buffer,
  230. list);
  231. /* Reset Top and Bottom counter */
  232. vip->tcount = 0;
  233. vip->bcount = 0;
  234. spin_unlock(&vip->lock);
  235. if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */
  236. start_dma(vip, vip->active); /* start dma capture */
  237. }
  238. }
  239. /* Videobuf2 Operations */
  240. static int queue_setup(struct vb2_queue *vq,
  241. unsigned int *nbuffers, unsigned int *nplanes,
  242. unsigned int sizes[], struct device *alloc_devs[])
  243. {
  244. struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
  245. if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
  246. *nbuffers = MAX_FRAMES;
  247. *nplanes = 1;
  248. sizes[0] = vip->format.sizeimage;
  249. vip->sequence = 0;
  250. vip->active = NULL;
  251. vip->tcount = 0;
  252. vip->bcount = 0;
  253. return 0;
  254. };
  255. static int buffer_init(struct vb2_buffer *vb)
  256. {
  257. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  258. struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
  259. vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
  260. INIT_LIST_HEAD(&vip_buf->list);
  261. return 0;
  262. }
  263. static int buffer_prepare(struct vb2_buffer *vb)
  264. {
  265. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  266. struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  267. struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
  268. unsigned long size;
  269. size = vip->format.sizeimage;
  270. if (vb2_plane_size(vb, 0) < size) {
  271. v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
  272. vb2_plane_size(vb, 0), size);
  273. return -EINVAL;
  274. }
  275. vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
  276. return 0;
  277. }
  278. static void buffer_queue(struct vb2_buffer *vb)
  279. {
  280. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  281. struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  282. struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
  283. spin_lock(&vip->lock);
  284. list_add_tail(&vip_buf->list, &vip->buffer_list);
  285. if (!vip->active) { /* No active buffer, active the first one */
  286. vip->active = list_first_entry(&vip->buffer_list,
  287. struct vip_buffer,
  288. list);
  289. if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
  290. start_dma(vip, vip_buf); /* start dma capture */
  291. }
  292. spin_unlock(&vip->lock);
  293. }
  294. static void buffer_finish(struct vb2_buffer *vb)
  295. {
  296. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  297. struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  298. struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
  299. /* Buffer handled, remove it from the list */
  300. spin_lock(&vip->lock);
  301. list_del_init(&vip_buf->list);
  302. spin_unlock(&vip->lock);
  303. if (vb2_is_streaming(vb->vb2_queue))
  304. vip_active_buf_next(vip);
  305. }
  306. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  307. {
  308. struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
  309. spin_lock_irq(&vip->slock);
  310. /* Enable interrupt VSYNC Top and Bottom*/
  311. reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
  312. spin_unlock_irq(&vip->slock);
  313. if (count)
  314. start_dma(vip, vip->active);
  315. return 0;
  316. }
  317. /* abort streaming and wait for last buffer */
  318. static void stop_streaming(struct vb2_queue *vq)
  319. {
  320. struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
  321. struct vip_buffer *vip_buf, *node;
  322. /* Disable acquisition */
  323. reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
  324. /* Disable all interrupts */
  325. reg_write(vip, DVP_ITM, 0);
  326. /* Release all active buffers */
  327. spin_lock(&vip->lock);
  328. list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
  329. vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  330. list_del(&vip_buf->list);
  331. }
  332. spin_unlock(&vip->lock);
  333. }
  334. static const struct vb2_ops vip_video_qops = {
  335. .queue_setup = queue_setup,
  336. .buf_init = buffer_init,
  337. .buf_prepare = buffer_prepare,
  338. .buf_finish = buffer_finish,
  339. .buf_queue = buffer_queue,
  340. .start_streaming = start_streaming,
  341. .stop_streaming = stop_streaming,
  342. .wait_prepare = vb2_ops_wait_prepare,
  343. .wait_finish = vb2_ops_wait_finish,
  344. };
  345. /* File Operations */
  346. static const struct v4l2_file_operations vip_fops = {
  347. .owner = THIS_MODULE,
  348. .open = v4l2_fh_open,
  349. .release = vb2_fop_release,
  350. .unlocked_ioctl = video_ioctl2,
  351. .read = vb2_fop_read,
  352. .mmap = vb2_fop_mmap,
  353. .poll = vb2_fop_poll
  354. };
  355. /**
  356. * vidioc_querycap - return capabilities of device
  357. * @file: descriptor of device
  358. * @cap: contains return values
  359. * @priv: unused
  360. *
  361. * the capabilities of the device are returned
  362. *
  363. * return value: 0, no error.
  364. */
  365. static int vidioc_querycap(struct file *file, void *priv,
  366. struct v4l2_capability *cap)
  367. {
  368. struct sta2x11_vip *vip = video_drvdata(file);
  369. strcpy(cap->driver, KBUILD_MODNAME);
  370. strcpy(cap->card, KBUILD_MODNAME);
  371. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  372. pci_name(vip->pdev));
  373. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  374. V4L2_CAP_STREAMING;
  375. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  376. return 0;
  377. }
  378. /**
  379. * vidioc_s_std - set video standard
  380. * @file: descriptor of device
  381. * @std: contains standard to be set
  382. * @priv: unused
  383. *
  384. * the video standard is set
  385. *
  386. * return value: 0, no error.
  387. *
  388. * -EIO, no input signal detected
  389. *
  390. * other, returned from video DAC.
  391. */
  392. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
  393. {
  394. struct sta2x11_vip *vip = video_drvdata(file);
  395. /*
  396. * This is here for backwards compatibility only.
  397. * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
  398. */
  399. if (std == V4L2_STD_ALL) {
  400. v4l2_subdev_call(vip->decoder, video, querystd, &std);
  401. if (std == V4L2_STD_UNKNOWN)
  402. return -EIO;
  403. }
  404. if (vip->std != std) {
  405. vip->std = std;
  406. if (V4L2_STD_525_60 & std)
  407. vip->format = formats_60[0];
  408. else
  409. vip->format = formats_50[0];
  410. }
  411. return v4l2_subdev_call(vip->decoder, video, s_std, std);
  412. }
  413. /**
  414. * vidioc_g_std - get video standard
  415. * @file: descriptor of device
  416. * @priv: unused
  417. * @std: contains return values
  418. *
  419. * the current video standard is returned
  420. *
  421. * return value: 0, no error.
  422. */
  423. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
  424. {
  425. struct sta2x11_vip *vip = video_drvdata(file);
  426. *std = vip->std;
  427. return 0;
  428. }
  429. /**
  430. * vidioc_querystd - get possible video standards
  431. * @file: descriptor of device
  432. * @priv: unused
  433. * @std: contains return values
  434. *
  435. * all possible video standards are returned
  436. *
  437. * return value: delivered by video DAC routine.
  438. */
  439. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
  440. {
  441. struct sta2x11_vip *vip = video_drvdata(file);
  442. return v4l2_subdev_call(vip->decoder, video, querystd, std);
  443. }
  444. static int vidioc_enum_input(struct file *file, void *priv,
  445. struct v4l2_input *inp)
  446. {
  447. if (inp->index > 1)
  448. return -EINVAL;
  449. inp->type = V4L2_INPUT_TYPE_CAMERA;
  450. inp->std = V4L2_STD_ALL;
  451. sprintf(inp->name, "Camera %u", inp->index);
  452. return 0;
  453. }
  454. /**
  455. * vidioc_s_input - set input line
  456. * @file: descriptor of device
  457. * @priv: unused
  458. * @i: new input line number
  459. *
  460. * the current active input line is set
  461. *
  462. * return value: 0, no error.
  463. *
  464. * -EINVAL, line number out of range
  465. */
  466. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  467. {
  468. struct sta2x11_vip *vip = video_drvdata(file);
  469. int ret;
  470. if (i > 1)
  471. return -EINVAL;
  472. ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
  473. if (!ret)
  474. vip->input = i;
  475. return 0;
  476. }
  477. /**
  478. * vidioc_g_input - return input line
  479. * @file: descriptor of device
  480. * @priv: unused
  481. * @i: returned input line number
  482. *
  483. * the current active input line is returned
  484. *
  485. * return value: always 0.
  486. */
  487. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  488. {
  489. struct sta2x11_vip *vip = video_drvdata(file);
  490. *i = vip->input;
  491. return 0;
  492. }
  493. /**
  494. * vidioc_enum_fmt_vid_cap - return video capture format
  495. * @file: descriptor of device
  496. * @priv: unused
  497. * @f: returned format information
  498. *
  499. * returns name and format of video capture
  500. * Only UYVY is supported by hardware.
  501. *
  502. * return value: always 0.
  503. */
  504. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  505. struct v4l2_fmtdesc *f)
  506. {
  507. if (f->index != 0)
  508. return -EINVAL;
  509. strcpy(f->description, "4:2:2, packed, UYVY");
  510. f->pixelformat = V4L2_PIX_FMT_UYVY;
  511. f->flags = 0;
  512. return 0;
  513. }
  514. /**
  515. * vidioc_try_fmt_vid_cap - set video capture format
  516. * @file: descriptor of device
  517. * @priv: unused
  518. * @f: new format
  519. *
  520. * new video format is set which includes width and
  521. * field type. width is fixed to 720, no scaling.
  522. * Only UYVY is supported by this hardware.
  523. * the minimum height is 200, the maximum is 576 (PAL)
  524. *
  525. * return value: 0, no error
  526. *
  527. * -EINVAL, pixel or field format not supported
  528. *
  529. */
  530. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  531. struct v4l2_format *f)
  532. {
  533. struct sta2x11_vip *vip = video_drvdata(file);
  534. int interlace_lim;
  535. if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
  536. v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
  537. return -EINVAL;
  538. }
  539. if (V4L2_STD_525_60 & vip->std)
  540. interlace_lim = 240;
  541. else
  542. interlace_lim = 288;
  543. switch (f->fmt.pix.field) {
  544. default:
  545. case V4L2_FIELD_ANY:
  546. if (interlace_lim < f->fmt.pix.height)
  547. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  548. else
  549. f->fmt.pix.field = V4L2_FIELD_BOTTOM;
  550. break;
  551. case V4L2_FIELD_TOP:
  552. case V4L2_FIELD_BOTTOM:
  553. if (interlace_lim < f->fmt.pix.height)
  554. f->fmt.pix.height = interlace_lim;
  555. break;
  556. case V4L2_FIELD_INTERLACED:
  557. break;
  558. }
  559. /* It is the only supported format */
  560. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  561. f->fmt.pix.height &= ~1;
  562. if (2 * interlace_lim < f->fmt.pix.height)
  563. f->fmt.pix.height = 2 * interlace_lim;
  564. if (200 > f->fmt.pix.height)
  565. f->fmt.pix.height = 200;
  566. f->fmt.pix.width = 720;
  567. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  568. f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
  569. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  570. return 0;
  571. }
  572. /**
  573. * vidioc_s_fmt_vid_cap - set current video format parameters
  574. * @file: descriptor of device
  575. * @priv: unused
  576. * @f: returned format information
  577. *
  578. * set new capture format
  579. * return value: 0, no error
  580. *
  581. * other, delivered by video DAC routine.
  582. */
  583. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  584. struct v4l2_format *f)
  585. {
  586. struct sta2x11_vip *vip = video_drvdata(file);
  587. unsigned int t_stop, b_stop, pitch;
  588. int ret;
  589. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  590. if (ret)
  591. return ret;
  592. if (vb2_is_busy(&vip->vb_vidq)) {
  593. /* Can't change format during acquisition */
  594. v4l2_err(&vip->v4l2_dev, "device busy\n");
  595. return -EBUSY;
  596. }
  597. vip->format = f->fmt.pix;
  598. switch (vip->format.field) {
  599. case V4L2_FIELD_INTERLACED:
  600. t_stop = ((vip->format.height / 2 - 1) << 16) |
  601. (2 * vip->format.width - 1);
  602. b_stop = t_stop;
  603. pitch = 4 * vip->format.width;
  604. break;
  605. case V4L2_FIELD_TOP:
  606. t_stop = ((vip->format.height - 1) << 16) |
  607. (2 * vip->format.width - 1);
  608. b_stop = (0 << 16) | (2 * vip->format.width - 1);
  609. pitch = 2 * vip->format.width;
  610. break;
  611. case V4L2_FIELD_BOTTOM:
  612. t_stop = (0 << 16) | (2 * vip->format.width - 1);
  613. b_stop = (vip->format.height << 16) |
  614. (2 * vip->format.width - 1);
  615. pitch = 2 * vip->format.width;
  616. break;
  617. default:
  618. v4l2_err(&vip->v4l2_dev, "unknown field format\n");
  619. return -EINVAL;
  620. }
  621. spin_lock_irq(&vip->slock);
  622. /* Y-X Top Field Offset */
  623. reg_write(vip, DVP_TFO, 0);
  624. /* Y-X Bottom Field Offset */
  625. reg_write(vip, DVP_BFO, 0);
  626. /* Y-X Top Field Stop*/
  627. reg_write(vip, DVP_TFS, t_stop);
  628. /* Y-X Bottom Field Stop */
  629. reg_write(vip, DVP_BFS, b_stop);
  630. /* Video Memory Pitch */
  631. reg_write(vip, DVP_VMP, pitch);
  632. spin_unlock_irq(&vip->slock);
  633. return 0;
  634. }
  635. /**
  636. * vidioc_g_fmt_vid_cap - get current video format parameters
  637. * @file: descriptor of device
  638. * @priv: unused
  639. * @f: contains format information
  640. *
  641. * returns current video format parameters
  642. *
  643. * return value: 0, always successful
  644. */
  645. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  646. struct v4l2_format *f)
  647. {
  648. struct sta2x11_vip *vip = video_drvdata(file);
  649. f->fmt.pix = vip->format;
  650. return 0;
  651. }
  652. static const struct v4l2_ioctl_ops vip_ioctl_ops = {
  653. .vidioc_querycap = vidioc_querycap,
  654. /* FMT handling */
  655. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  656. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  657. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  658. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  659. /* Buffer handlers */
  660. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  661. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  662. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  663. .vidioc_querybuf = vb2_ioctl_querybuf,
  664. .vidioc_qbuf = vb2_ioctl_qbuf,
  665. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  666. /* Stream on/off */
  667. .vidioc_streamon = vb2_ioctl_streamon,
  668. .vidioc_streamoff = vb2_ioctl_streamoff,
  669. /* Standard handling */
  670. .vidioc_g_std = vidioc_g_std,
  671. .vidioc_s_std = vidioc_s_std,
  672. .vidioc_querystd = vidioc_querystd,
  673. /* Input handling */
  674. .vidioc_enum_input = vidioc_enum_input,
  675. .vidioc_g_input = vidioc_g_input,
  676. .vidioc_s_input = vidioc_s_input,
  677. /* Log status ioctl */
  678. .vidioc_log_status = v4l2_ctrl_log_status,
  679. /* Event handling */
  680. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  681. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  682. };
  683. static const struct video_device video_dev_template = {
  684. .name = KBUILD_MODNAME,
  685. .release = video_device_release_empty,
  686. .fops = &vip_fops,
  687. .ioctl_ops = &vip_ioctl_ops,
  688. .tvnorms = V4L2_STD_ALL,
  689. };
  690. /**
  691. * vip_irq - interrupt routine
  692. * @irq: Number of interrupt ( not used, correct number is assumed )
  693. * @vip: local data structure containing all information
  694. *
  695. * check for both frame interrupts set ( top and bottom ).
  696. * check FIFO overflow, but limit number of log messages after open.
  697. * signal a complete buffer if done
  698. *
  699. * return value: IRQ_NONE, interrupt was not generated by VIP
  700. *
  701. * IRQ_HANDLED, interrupt done.
  702. */
  703. static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
  704. {
  705. unsigned int status;
  706. status = reg_read(vip, DVP_ITS);
  707. if (!status) /* No interrupt to handle */
  708. return IRQ_NONE;
  709. if (status & DVP_IT_FIFO)
  710. if (vip->overflow++ > 5)
  711. pr_info("VIP: fifo overflow\n");
  712. if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
  713. /* this is bad, we are too slow, hope the condition is gone
  714. * on the next frame */
  715. return IRQ_HANDLED;
  716. }
  717. if (status & DVP_IT_VST)
  718. if ((++vip->tcount) < 2)
  719. return IRQ_HANDLED;
  720. if (status & DVP_IT_VSB) {
  721. vip->bcount++;
  722. return IRQ_HANDLED;
  723. }
  724. if (vip->active) { /* Acquisition is over on this buffer */
  725. /* Disable acquisition */
  726. reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
  727. /* Remove the active buffer from the list */
  728. vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
  729. vip->active->vb.sequence = vip->sequence++;
  730. vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
  731. }
  732. return IRQ_HANDLED;
  733. }
  734. static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
  735. {
  736. /* Register initialization */
  737. spin_lock_irq(&vip->slock);
  738. /* Clean interrupt */
  739. reg_read(vip, DVP_ITS);
  740. /* Enable Half Line per vertical */
  741. reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
  742. /* Reset VIP control */
  743. reg_write(vip, DVP_CTL, DVP_CTL_RST);
  744. /* Clear VIP control */
  745. reg_write(vip, DVP_CTL, 0);
  746. spin_unlock_irq(&vip->slock);
  747. }
  748. static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
  749. {
  750. spin_lock_irq(&vip->slock);
  751. /* Disable interrupt */
  752. reg_write(vip, DVP_ITM, 0);
  753. /* Reset VIP Control */
  754. reg_write(vip, DVP_CTL, DVP_CTL_RST);
  755. /* Clear VIP Control */
  756. reg_write(vip, DVP_CTL, 0);
  757. /* Clean VIP Interrupt */
  758. reg_read(vip, DVP_ITS);
  759. spin_unlock_irq(&vip->slock);
  760. }
  761. static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
  762. {
  763. int err;
  764. err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
  765. if (err) {
  766. v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
  767. return err;
  768. }
  769. memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
  770. vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  771. vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
  772. vip->vb_vidq.drv_priv = vip;
  773. vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
  774. vip->vb_vidq.ops = &vip_video_qops;
  775. vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
  776. vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  777. vip->vb_vidq.dev = &vip->pdev->dev;
  778. vip->vb_vidq.lock = &vip->v4l_lock;
  779. err = vb2_queue_init(&vip->vb_vidq);
  780. if (err)
  781. return err;
  782. INIT_LIST_HEAD(&vip->buffer_list);
  783. spin_lock_init(&vip->lock);
  784. return 0;
  785. }
  786. static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
  787. {
  788. /*
  789. * Inititialize an empty control so VIP can inerithing controls
  790. * from ADV7180
  791. */
  792. v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
  793. vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
  794. if (vip->ctrl_hdl.error) {
  795. int err = vip->ctrl_hdl.error;
  796. v4l2_ctrl_handler_free(&vip->ctrl_hdl);
  797. return err;
  798. }
  799. return 0;
  800. }
  801. /**
  802. * vip_gpio_reserve - reserve gpio pin
  803. * @dev: device
  804. * @pin: GPIO pin number
  805. * @dir: direction, input or output
  806. * @name: GPIO pin name
  807. *
  808. */
  809. static int vip_gpio_reserve(struct device *dev, int pin, int dir,
  810. const char *name)
  811. {
  812. int ret = -ENODEV;
  813. if (!gpio_is_valid(pin))
  814. return ret;
  815. ret = gpio_request(pin, name);
  816. if (ret) {
  817. dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
  818. return ret;
  819. }
  820. ret = gpio_direction_output(pin, dir);
  821. if (ret) {
  822. dev_err(dev, "Failed to set direction for pin %d (%s)\n",
  823. pin, name);
  824. gpio_free(pin);
  825. return ret;
  826. }
  827. ret = gpio_export(pin, false);
  828. if (ret) {
  829. dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
  830. gpio_free(pin);
  831. return ret;
  832. }
  833. return 0;
  834. }
  835. /**
  836. * vip_gpio_release - release gpio pin
  837. * @dev: device
  838. * @pin: GPIO pin number
  839. * @name: GPIO pin name
  840. *
  841. */
  842. static void vip_gpio_release(struct device *dev, int pin, const char *name)
  843. {
  844. if (gpio_is_valid(pin)) {
  845. dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
  846. gpio_unexport(pin);
  847. gpio_free(pin);
  848. }
  849. }
  850. /**
  851. * sta2x11_vip_init_one - init one instance of video device
  852. * @pdev: PCI device
  853. * @ent: (not used)
  854. *
  855. * allocate reset pins for DAC.
  856. * Reset video DAC, this is done via reset line.
  857. * allocate memory for managing device
  858. * request interrupt
  859. * map IO region
  860. * register device
  861. * find and initialize video DAC
  862. *
  863. * return value: 0, no error
  864. *
  865. * -ENOMEM, no memory
  866. *
  867. * -ENODEV, device could not be detected or registered
  868. */
  869. static int sta2x11_vip_init_one(struct pci_dev *pdev,
  870. const struct pci_device_id *ent)
  871. {
  872. int ret;
  873. struct sta2x11_vip *vip;
  874. struct vip_config *config;
  875. /* Check if hardware support 26-bit DMA */
  876. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
  877. dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
  878. return -EINVAL;
  879. }
  880. /* Enable PCI */
  881. ret = pci_enable_device(pdev);
  882. if (ret)
  883. return ret;
  884. /* Get VIP platform data */
  885. config = dev_get_platdata(&pdev->dev);
  886. if (!config) {
  887. dev_info(&pdev->dev, "VIP slot disabled\n");
  888. ret = -EINVAL;
  889. goto disable;
  890. }
  891. /* Power configuration */
  892. ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
  893. config->pwr_name);
  894. if (ret)
  895. goto disable;
  896. ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
  897. config->reset_name);
  898. if (ret) {
  899. vip_gpio_release(&pdev->dev, config->pwr_pin,
  900. config->pwr_name);
  901. goto disable;
  902. }
  903. if (gpio_is_valid(config->pwr_pin)) {
  904. /* Datasheet says 5ms between PWR and RST */
  905. usleep_range(5000, 25000);
  906. gpio_direction_output(config->pwr_pin, 1);
  907. }
  908. if (gpio_is_valid(config->reset_pin)) {
  909. /* Datasheet says 5ms between PWR and RST */
  910. usleep_range(5000, 25000);
  911. gpio_direction_output(config->reset_pin, 1);
  912. }
  913. usleep_range(5000, 25000);
  914. /* Allocate a new VIP instance */
  915. vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
  916. if (!vip) {
  917. ret = -ENOMEM;
  918. goto release_gpios;
  919. }
  920. vip->pdev = pdev;
  921. vip->std = V4L2_STD_PAL;
  922. vip->format = formats_50[0];
  923. vip->config = config;
  924. mutex_init(&vip->v4l_lock);
  925. ret = sta2x11_vip_init_controls(vip);
  926. if (ret)
  927. goto free_mem;
  928. ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
  929. if (ret)
  930. goto free_mem;
  931. dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
  932. (unsigned long)pci_resource_start(pdev, 0),
  933. (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
  934. pci_set_master(pdev);
  935. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  936. if (ret)
  937. goto unreg;
  938. vip->iomem = pci_iomap(pdev, 0, 0x100);
  939. if (!vip->iomem) {
  940. ret = -ENOMEM;
  941. goto release;
  942. }
  943. pci_enable_msi(pdev);
  944. /* Initialize buffer */
  945. ret = sta2x11_vip_init_buffer(vip);
  946. if (ret)
  947. goto unmap;
  948. spin_lock_init(&vip->slock);
  949. ret = request_irq(pdev->irq,
  950. (irq_handler_t) vip_irq,
  951. IRQF_SHARED, KBUILD_MODNAME, vip);
  952. if (ret) {
  953. dev_err(&pdev->dev, "request_irq failed\n");
  954. ret = -ENODEV;
  955. goto release_buf;
  956. }
  957. /* Initialize and register video device */
  958. vip->video_dev = video_dev_template;
  959. vip->video_dev.v4l2_dev = &vip->v4l2_dev;
  960. vip->video_dev.queue = &vip->vb_vidq;
  961. vip->video_dev.lock = &vip->v4l_lock;
  962. video_set_drvdata(&vip->video_dev, vip);
  963. ret = video_register_device(&vip->video_dev, VFL_TYPE_GRABBER, -1);
  964. if (ret)
  965. goto vrelease;
  966. /* Get ADV7180 subdevice */
  967. vip->adapter = i2c_get_adapter(vip->config->i2c_id);
  968. if (!vip->adapter) {
  969. ret = -ENODEV;
  970. dev_err(&pdev->dev, "no I2C adapter found\n");
  971. goto vunreg;
  972. }
  973. vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
  974. "adv7180", vip->config->i2c_addr,
  975. NULL);
  976. if (!vip->decoder) {
  977. ret = -ENODEV;
  978. dev_err(&pdev->dev, "no decoder found\n");
  979. goto vunreg;
  980. }
  981. i2c_put_adapter(vip->adapter);
  982. v4l2_subdev_call(vip->decoder, core, init, 0);
  983. sta2x11_vip_init_register(vip);
  984. dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
  985. return 0;
  986. vunreg:
  987. video_set_drvdata(&vip->video_dev, NULL);
  988. vrelease:
  989. video_unregister_device(&vip->video_dev);
  990. free_irq(pdev->irq, vip);
  991. release_buf:
  992. pci_disable_msi(pdev);
  993. unmap:
  994. vb2_queue_release(&vip->vb_vidq);
  995. pci_iounmap(pdev, vip->iomem);
  996. release:
  997. pci_release_regions(pdev);
  998. unreg:
  999. v4l2_device_unregister(&vip->v4l2_dev);
  1000. free_mem:
  1001. kfree(vip);
  1002. release_gpios:
  1003. vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
  1004. vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
  1005. disable:
  1006. /*
  1007. * do not call pci_disable_device on sta2x11 because it break all
  1008. * other Bus masters on this EP
  1009. */
  1010. return ret;
  1011. }
  1012. /**
  1013. * sta2x11_vip_remove_one - release device
  1014. * @pdev: PCI device
  1015. *
  1016. * Undo everything done in .._init_one
  1017. *
  1018. * unregister video device
  1019. * free interrupt
  1020. * unmap ioadresses
  1021. * free memory
  1022. * free GPIO pins
  1023. */
  1024. static void sta2x11_vip_remove_one(struct pci_dev *pdev)
  1025. {
  1026. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1027. struct sta2x11_vip *vip =
  1028. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1029. sta2x11_vip_clear_register(vip);
  1030. video_set_drvdata(&vip->video_dev, NULL);
  1031. video_unregister_device(&vip->video_dev);
  1032. free_irq(pdev->irq, vip);
  1033. pci_disable_msi(pdev);
  1034. vb2_queue_release(&vip->vb_vidq);
  1035. pci_iounmap(pdev, vip->iomem);
  1036. pci_release_regions(pdev);
  1037. v4l2_device_unregister(&vip->v4l2_dev);
  1038. vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
  1039. vip->config->pwr_name);
  1040. vip_gpio_release(&pdev->dev, vip->config->reset_pin,
  1041. vip->config->reset_name);
  1042. kfree(vip);
  1043. /*
  1044. * do not call pci_disable_device on sta2x11 because it break all
  1045. * other Bus masters on this EP
  1046. */
  1047. }
  1048. #ifdef CONFIG_PM
  1049. /**
  1050. * sta2x11_vip_suspend - set device into power save mode
  1051. * @pdev: PCI device
  1052. * @state: new state of device
  1053. *
  1054. * all relevant registers are saved and an attempt to set a new state is made.
  1055. *
  1056. * return value: 0 always indicate success,
  1057. * even if device could not be disabled. (workaround for hardware problem)
  1058. */
  1059. static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
  1060. {
  1061. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1062. struct sta2x11_vip *vip =
  1063. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1064. unsigned long flags;
  1065. int i;
  1066. spin_lock_irqsave(&vip->slock, flags);
  1067. vip->register_save_area[0] = reg_read(vip, DVP_CTL);
  1068. reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
  1069. vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
  1070. reg_write(vip, DVP_ITM, 0);
  1071. for (i = 1; i < SAVE_COUNT; i++)
  1072. vip->register_save_area[i] = reg_read(vip, 4 * i);
  1073. for (i = 0; i < AUX_COUNT; i++)
  1074. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
  1075. reg_read(vip, registers_to_save[i]);
  1076. spin_unlock_irqrestore(&vip->slock, flags);
  1077. /* save pci state */
  1078. pci_save_state(pdev);
  1079. if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
  1080. /*
  1081. * do not call pci_disable_device on sta2x11 because it
  1082. * break all other Bus masters on this EP
  1083. */
  1084. vip->disabled = 1;
  1085. }
  1086. pr_info("VIP: suspend\n");
  1087. return 0;
  1088. }
  1089. /**
  1090. * sta2x11_vip_resume - resume device operation
  1091. * @pdev : PCI device
  1092. *
  1093. * re-enable device, set PCI state to powered and restore registers.
  1094. * resume normal device operation afterwards.
  1095. *
  1096. * return value: 0, no error.
  1097. *
  1098. * other, could not set device to power on state.
  1099. */
  1100. static int sta2x11_vip_resume(struct pci_dev *pdev)
  1101. {
  1102. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1103. struct sta2x11_vip *vip =
  1104. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1105. unsigned long flags;
  1106. int ret, i;
  1107. pr_info("VIP: resume\n");
  1108. /* restore pci state */
  1109. if (vip->disabled) {
  1110. ret = pci_enable_device(pdev);
  1111. if (ret) {
  1112. pr_warn("VIP: Can't enable device.\n");
  1113. return ret;
  1114. }
  1115. vip->disabled = 0;
  1116. }
  1117. ret = pci_set_power_state(pdev, PCI_D0);
  1118. if (ret) {
  1119. /*
  1120. * do not call pci_disable_device on sta2x11 because it
  1121. * break all other Bus masters on this EP
  1122. */
  1123. pr_warn("VIP: Can't enable device.\n");
  1124. vip->disabled = 1;
  1125. return ret;
  1126. }
  1127. pci_restore_state(pdev);
  1128. spin_lock_irqsave(&vip->slock, flags);
  1129. for (i = 1; i < SAVE_COUNT; i++)
  1130. reg_write(vip, 4 * i, vip->register_save_area[i]);
  1131. for (i = 0; i < AUX_COUNT; i++)
  1132. reg_write(vip, registers_to_save[i],
  1133. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
  1134. reg_write(vip, DVP_CTL, vip->register_save_area[0]);
  1135. reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
  1136. spin_unlock_irqrestore(&vip->slock, flags);
  1137. return 0;
  1138. }
  1139. #endif
  1140. static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
  1141. {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
  1142. {0,}
  1143. };
  1144. static struct pci_driver sta2x11_vip_driver = {
  1145. .name = KBUILD_MODNAME,
  1146. .probe = sta2x11_vip_init_one,
  1147. .remove = sta2x11_vip_remove_one,
  1148. .id_table = sta2x11_vip_pci_tbl,
  1149. #ifdef CONFIG_PM
  1150. .suspend = sta2x11_vip_suspend,
  1151. .resume = sta2x11_vip_resume,
  1152. #endif
  1153. };
  1154. static int __init sta2x11_vip_init_module(void)
  1155. {
  1156. return pci_register_driver(&sta2x11_vip_driver);
  1157. }
  1158. static void __exit sta2x11_vip_exit_module(void)
  1159. {
  1160. pci_unregister_driver(&sta2x11_vip_driver);
  1161. }
  1162. #ifdef MODULE
  1163. module_init(sta2x11_vip_init_module);
  1164. module_exit(sta2x11_vip_exit_module);
  1165. #else
  1166. late_initcall_sync(sta2x11_vip_init_module);
  1167. #endif
  1168. MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
  1169. MODULE_AUTHOR("Wind River");
  1170. MODULE_LICENSE("GPL v2");
  1171. MODULE_SUPPORTED_DEVICE("sta2x11 video input");
  1172. MODULE_VERSION(DRV_VERSION);
  1173. MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);