sta2x11_vip.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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_buffer vb;
  80. struct list_head list;
  81. dma_addr_t dma;
  82. };
  83. static inline struct vip_buffer *to_vip_buffer(struct vb2_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. * @alloc_ctx: context for videobuf2
  102. * @vb_vidq: queue maintained by videobuf2 layer
  103. * @buffer_list: list of buffer in use
  104. * @sequence: sequence number of acquired buffer
  105. * @active: current active buffer
  106. * @lock: used in videobuf2 callback
  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_alloc_ctx *alloc_ctx;
  129. struct vb2_queue vb_vidq;
  130. struct list_head buffer_list;
  131. unsigned int sequence;
  132. struct vip_buffer *active; /* current active buffer */
  133. spinlock_t lock; /* Used in videobuf2 callback */
  134. /* Interrupt counters */
  135. int tcount, bcount;
  136. int overflow;
  137. void *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, const struct v4l2_format *fmt,
  241. unsigned int *nbuffers, unsigned int *nplanes,
  242. unsigned int sizes[], void *alloc_ctxs[])
  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. alloc_ctxs[0] = vip->alloc_ctx;
  250. vip->sequence = 0;
  251. vip->active = NULL;
  252. vip->tcount = 0;
  253. vip->bcount = 0;
  254. return 0;
  255. };
  256. static int buffer_init(struct vb2_buffer *vb)
  257. {
  258. struct vip_buffer *vip_buf = to_vip_buffer(vb);
  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 sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  266. struct vip_buffer *vip_buf = to_vip_buffer(vb);
  267. unsigned long size;
  268. size = vip->format.sizeimage;
  269. if (vb2_plane_size(vb, 0) < size) {
  270. v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
  271. vb2_plane_size(vb, 0), size);
  272. return -EINVAL;
  273. }
  274. vb2_set_plane_payload(&vip_buf->vb, 0, size);
  275. return 0;
  276. }
  277. static void buffer_queue(struct vb2_buffer *vb)
  278. {
  279. struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  280. struct vip_buffer *vip_buf = to_vip_buffer(vb);
  281. spin_lock(&vip->lock);
  282. list_add_tail(&vip_buf->list, &vip->buffer_list);
  283. if (!vip->active) { /* No active buffer, active the first one */
  284. vip->active = list_first_entry(&vip->buffer_list,
  285. struct vip_buffer,
  286. list);
  287. if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
  288. start_dma(vip, vip_buf); /* start dma capture */
  289. }
  290. spin_unlock(&vip->lock);
  291. }
  292. static int buffer_finish(struct vb2_buffer *vb)
  293. {
  294. struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
  295. struct vip_buffer *vip_buf = to_vip_buffer(vb);
  296. /* Buffer handled, remove it from the list */
  297. spin_lock(&vip->lock);
  298. list_del_init(&vip_buf->list);
  299. spin_unlock(&vip->lock);
  300. vip_active_buf_next(vip);
  301. return 0;
  302. }
  303. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  304. {
  305. struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
  306. spin_lock_irq(&vip->slock);
  307. /* Enable interrupt VSYNC Top and Bottom*/
  308. reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
  309. spin_unlock_irq(&vip->slock);
  310. if (count)
  311. start_dma(vip, vip->active);
  312. return 0;
  313. }
  314. /* abort streaming and wait for last buffer */
  315. static int stop_streaming(struct vb2_queue *vq)
  316. {
  317. struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
  318. struct vip_buffer *vip_buf, *node;
  319. /* Disable acquisition */
  320. reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
  321. /* Disable all interrupts */
  322. reg_write(vip, DVP_ITM, 0);
  323. /* Release all active buffers */
  324. spin_lock(&vip->lock);
  325. list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
  326. vb2_buffer_done(&vip_buf->vb, VB2_BUF_STATE_ERROR);
  327. list_del(&vip_buf->list);
  328. }
  329. spin_unlock(&vip->lock);
  330. return 0;
  331. }
  332. static struct vb2_ops vip_video_qops = {
  333. .queue_setup = queue_setup,
  334. .buf_init = buffer_init,
  335. .buf_prepare = buffer_prepare,
  336. .buf_finish = buffer_finish,
  337. .buf_queue = buffer_queue,
  338. .start_streaming = start_streaming,
  339. .stop_streaming = stop_streaming,
  340. };
  341. /* File Operations */
  342. static const struct v4l2_file_operations vip_fops = {
  343. .owner = THIS_MODULE,
  344. .open = v4l2_fh_open,
  345. .release = vb2_fop_release,
  346. .unlocked_ioctl = video_ioctl2,
  347. .read = vb2_fop_read,
  348. .mmap = vb2_fop_mmap,
  349. .poll = vb2_fop_poll
  350. };
  351. /**
  352. * vidioc_querycap - return capabilities of device
  353. * @file: descriptor of device
  354. * @cap: contains return values
  355. *
  356. * the capabilities of the device are returned
  357. *
  358. * return value: 0, no error.
  359. */
  360. static int vidioc_querycap(struct file *file, void *priv,
  361. struct v4l2_capability *cap)
  362. {
  363. struct sta2x11_vip *vip = video_drvdata(file);
  364. strcpy(cap->driver, KBUILD_MODNAME);
  365. strcpy(cap->card, KBUILD_MODNAME);
  366. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  367. pci_name(vip->pdev));
  368. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  369. V4L2_CAP_STREAMING;
  370. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  371. return 0;
  372. }
  373. /**
  374. * vidioc_s_std - set video standard
  375. * @file: descriptor of device
  376. * @std: contains standard to be set
  377. *
  378. * the video standard is set
  379. *
  380. * return value: 0, no error.
  381. *
  382. * -EIO, no input signal detected
  383. *
  384. * other, returned from video DAC.
  385. */
  386. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
  387. {
  388. struct sta2x11_vip *vip = video_drvdata(file);
  389. v4l2_std_id oldstd = vip->std, newstd;
  390. int status;
  391. if (V4L2_STD_ALL == *std) {
  392. v4l2_subdev_call(vip->decoder, core, s_std, *std);
  393. ssleep(2);
  394. v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
  395. v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
  396. if (status & V4L2_IN_ST_NO_SIGNAL)
  397. return -EIO;
  398. *std = vip->std = newstd;
  399. if (oldstd != *std) {
  400. if (V4L2_STD_525_60 & (*std))
  401. vip->format = formats_60[0];
  402. else
  403. vip->format = formats_50[0];
  404. }
  405. return 0;
  406. }
  407. if (oldstd != *std) {
  408. if (V4L2_STD_525_60 & (*std))
  409. vip->format = formats_60[0];
  410. else
  411. vip->format = formats_50[0];
  412. }
  413. return v4l2_subdev_call(vip->decoder, core, s_std, *std);
  414. }
  415. /**
  416. * vidioc_g_std - get video standard
  417. * @file: descriptor of device
  418. * @std: contains return values
  419. *
  420. * the current video standard is returned
  421. *
  422. * return value: 0, no error.
  423. */
  424. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
  425. {
  426. struct sta2x11_vip *vip = video_drvdata(file);
  427. *std = vip->std;
  428. return 0;
  429. }
  430. /**
  431. * vidioc_querystd - get possible video standards
  432. * @file: descriptor of device
  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. * @i: new input line number
  458. *
  459. * the current active input line is set
  460. *
  461. * return value: 0, no error.
  462. *
  463. * -EINVAL, line number out of range
  464. */
  465. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  466. {
  467. struct sta2x11_vip *vip = video_drvdata(file);
  468. int ret;
  469. if (i > 1)
  470. return -EINVAL;
  471. ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
  472. if (!ret)
  473. vip->input = i;
  474. return 0;
  475. }
  476. /**
  477. * vidioc_g_input - return input line
  478. * @file: descriptor of device
  479. * @i: returned input line number
  480. *
  481. * the current active input line is returned
  482. *
  483. * return value: always 0.
  484. */
  485. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  486. {
  487. struct sta2x11_vip *vip = video_drvdata(file);
  488. *i = vip->input;
  489. return 0;
  490. }
  491. /**
  492. * vidioc_enum_fmt_vid_cap - return video capture format
  493. * @f: returned format information
  494. *
  495. * returns name and format of video capture
  496. * Only UYVY is supported by hardware.
  497. *
  498. * return value: always 0.
  499. */
  500. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  501. struct v4l2_fmtdesc *f)
  502. {
  503. if (f->index != 0)
  504. return -EINVAL;
  505. strcpy(f->description, "4:2:2, packed, UYVY");
  506. f->pixelformat = V4L2_PIX_FMT_UYVY;
  507. f->flags = 0;
  508. return 0;
  509. }
  510. /**
  511. * vidioc_try_fmt_vid_cap - set video capture format
  512. * @file: descriptor of device
  513. * @f: new format
  514. *
  515. * new video format is set which includes width and
  516. * field type. width is fixed to 720, no scaling.
  517. * Only UYVY is supported by this hardware.
  518. * the minimum height is 200, the maximum is 576 (PAL)
  519. *
  520. * return value: 0, no error
  521. *
  522. * -EINVAL, pixel or field format not supported
  523. *
  524. */
  525. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  526. struct v4l2_format *f)
  527. {
  528. struct sta2x11_vip *vip = video_drvdata(file);
  529. int interlace_lim;
  530. if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
  531. v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
  532. return -EINVAL;
  533. }
  534. if (V4L2_STD_525_60 & vip->std)
  535. interlace_lim = 240;
  536. else
  537. interlace_lim = 288;
  538. switch (f->fmt.pix.field) {
  539. default:
  540. case V4L2_FIELD_ANY:
  541. if (interlace_lim < f->fmt.pix.height)
  542. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  543. else
  544. f->fmt.pix.field = V4L2_FIELD_BOTTOM;
  545. break;
  546. case V4L2_FIELD_TOP:
  547. case V4L2_FIELD_BOTTOM:
  548. if (interlace_lim < f->fmt.pix.height)
  549. f->fmt.pix.height = interlace_lim;
  550. break;
  551. case V4L2_FIELD_INTERLACED:
  552. break;
  553. }
  554. /* It is the only supported format */
  555. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  556. f->fmt.pix.height &= ~1;
  557. if (2 * interlace_lim < f->fmt.pix.height)
  558. f->fmt.pix.height = 2 * interlace_lim;
  559. if (200 > f->fmt.pix.height)
  560. f->fmt.pix.height = 200;
  561. f->fmt.pix.width = 720;
  562. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  563. f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
  564. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  565. f->fmt.pix.priv = 0;
  566. return 0;
  567. }
  568. /**
  569. * vidioc_s_fmt_vid_cap - set current video format parameters
  570. * @file: descriptor of device
  571. * @f: returned format information
  572. *
  573. * set new capture format
  574. * return value: 0, no error
  575. *
  576. * other, delivered by video DAC routine.
  577. */
  578. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  579. struct v4l2_format *f)
  580. {
  581. struct sta2x11_vip *vip = video_drvdata(file);
  582. unsigned int t_stop, b_stop, pitch;
  583. int ret;
  584. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  585. if (ret)
  586. return ret;
  587. if (vb2_is_busy(&vip->vb_vidq)) {
  588. /* Can't change format during acquisition */
  589. v4l2_err(&vip->v4l2_dev, "device busy\n");
  590. return -EBUSY;
  591. }
  592. vip->format = f->fmt.pix;
  593. switch (vip->format.field) {
  594. case V4L2_FIELD_INTERLACED:
  595. t_stop = ((vip->format.height / 2 - 1) << 16) |
  596. (2 * vip->format.width - 1);
  597. b_stop = t_stop;
  598. pitch = 4 * vip->format.width;
  599. break;
  600. case V4L2_FIELD_TOP:
  601. t_stop = ((vip->format.height - 1) << 16) |
  602. (2 * vip->format.width - 1);
  603. b_stop = (0 << 16) | (2 * vip->format.width - 1);
  604. pitch = 2 * vip->format.width;
  605. break;
  606. case V4L2_FIELD_BOTTOM:
  607. t_stop = (0 << 16) | (2 * vip->format.width - 1);
  608. b_stop = (vip->format.height << 16) |
  609. (2 * vip->format.width - 1);
  610. pitch = 2 * vip->format.width;
  611. break;
  612. default:
  613. v4l2_err(&vip->v4l2_dev, "unknown field format\n");
  614. return -EINVAL;
  615. }
  616. spin_lock_irq(&vip->slock);
  617. /* Y-X Top Field Offset */
  618. reg_write(vip, DVP_TFO, 0);
  619. /* Y-X Bottom Field Offset */
  620. reg_write(vip, DVP_BFO, 0);
  621. /* Y-X Top Field Stop*/
  622. reg_write(vip, DVP_TFS, t_stop);
  623. /* Y-X Bottom Field Stop */
  624. reg_write(vip, DVP_BFS, b_stop);
  625. /* Video Memory Pitch */
  626. reg_write(vip, DVP_VMP, pitch);
  627. spin_unlock_irq(&vip->slock);
  628. return 0;
  629. }
  630. /**
  631. * vidioc_g_fmt_vid_cap - get current video format parameters
  632. * @file: descriptor of device
  633. * @f: contains format information
  634. *
  635. * returns current video format parameters
  636. *
  637. * return value: 0, always successful
  638. */
  639. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  640. struct v4l2_format *f)
  641. {
  642. struct sta2x11_vip *vip = video_drvdata(file);
  643. f->fmt.pix = vip->format;
  644. return 0;
  645. }
  646. static const struct v4l2_ioctl_ops vip_ioctl_ops = {
  647. .vidioc_querycap = vidioc_querycap,
  648. /* FMT handling */
  649. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  650. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  651. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  652. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  653. /* Buffer handlers */
  654. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  655. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  656. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  657. .vidioc_querybuf = vb2_ioctl_querybuf,
  658. .vidioc_qbuf = vb2_ioctl_qbuf,
  659. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  660. /* Stream on/off */
  661. .vidioc_streamon = vb2_ioctl_streamon,
  662. .vidioc_streamoff = vb2_ioctl_streamoff,
  663. /* Standard handling */
  664. .vidioc_g_std = vidioc_g_std,
  665. .vidioc_s_std = vidioc_s_std,
  666. .vidioc_querystd = vidioc_querystd,
  667. /* Input handling */
  668. .vidioc_enum_input = vidioc_enum_input,
  669. .vidioc_g_input = vidioc_g_input,
  670. .vidioc_s_input = vidioc_s_input,
  671. /* Log status ioctl */
  672. .vidioc_log_status = v4l2_ctrl_log_status,
  673. /* Event handling */
  674. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  675. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  676. };
  677. static struct video_device video_dev_template = {
  678. .name = KBUILD_MODNAME,
  679. .release = video_device_release,
  680. .fops = &vip_fops,
  681. .ioctl_ops = &vip_ioctl_ops,
  682. .tvnorms = V4L2_STD_ALL,
  683. };
  684. /**
  685. * vip_irq - interrupt routine
  686. * @irq: Number of interrupt ( not used, correct number is assumed )
  687. * @vip: local data structure containing all information
  688. *
  689. * check for both frame interrupts set ( top and bottom ).
  690. * check FIFO overflow, but limit number of log messages after open.
  691. * signal a complete buffer if done
  692. *
  693. * return value: IRQ_NONE, interrupt was not generated by VIP
  694. *
  695. * IRQ_HANDLED, interrupt done.
  696. */
  697. static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
  698. {
  699. unsigned int status;
  700. status = reg_read(vip, DVP_ITS);
  701. if (!status) /* No interrupt to handle */
  702. return IRQ_NONE;
  703. if (status & DVP_IT_FIFO)
  704. if (vip->overflow++ > 5)
  705. pr_info("VIP: fifo overflow\n");
  706. if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
  707. /* this is bad, we are too slow, hope the condition is gone
  708. * on the next frame */
  709. return IRQ_HANDLED;
  710. }
  711. if (status & DVP_IT_VST)
  712. if ((++vip->tcount) < 2)
  713. return IRQ_HANDLED;
  714. if (status & DVP_IT_VSB) {
  715. vip->bcount++;
  716. return IRQ_HANDLED;
  717. }
  718. if (vip->active) { /* Acquisition is over on this buffer */
  719. /* Disable acquisition */
  720. reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
  721. /* Remove the active buffer from the list */
  722. do_gettimeofday(&vip->active->vb.v4l2_buf.timestamp);
  723. vip->active->vb.v4l2_buf.sequence = vip->sequence++;
  724. vb2_buffer_done(&vip->active->vb, VB2_BUF_STATE_DONE);
  725. }
  726. return IRQ_HANDLED;
  727. }
  728. static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
  729. {
  730. /* Register initialization */
  731. spin_lock_irq(&vip->slock);
  732. /* Clean interrupt */
  733. reg_read(vip, DVP_ITS);
  734. /* Enable Half Line per vertical */
  735. reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
  736. /* Reset VIP control */
  737. reg_write(vip, DVP_CTL, DVP_CTL_RST);
  738. /* Clear VIP control */
  739. reg_write(vip, DVP_CTL, 0);
  740. spin_unlock_irq(&vip->slock);
  741. }
  742. static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
  743. {
  744. spin_lock_irq(&vip->slock);
  745. /* Disable interrupt */
  746. reg_write(vip, DVP_ITM, 0);
  747. /* Reset VIP Control */
  748. reg_write(vip, DVP_CTL, DVP_CTL_RST);
  749. /* Clear VIP Control */
  750. reg_write(vip, DVP_CTL, 0);
  751. /* Clean VIP Interrupt */
  752. reg_read(vip, DVP_ITS);
  753. spin_unlock_irq(&vip->slock);
  754. }
  755. static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
  756. {
  757. int err;
  758. err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
  759. if (err) {
  760. v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
  761. return err;
  762. }
  763. memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
  764. vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  765. vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
  766. vip->vb_vidq.drv_priv = vip;
  767. vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
  768. vip->vb_vidq.ops = &vip_video_qops;
  769. vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
  770. err = vb2_queue_init(&vip->vb_vidq);
  771. if (err)
  772. return err;
  773. INIT_LIST_HEAD(&vip->buffer_list);
  774. spin_lock_init(&vip->lock);
  775. vip->alloc_ctx = vb2_dma_contig_init_ctx(&vip->pdev->dev);
  776. if (IS_ERR(vip->alloc_ctx)) {
  777. v4l2_err(&vip->v4l2_dev, "Can't allocate buffer context");
  778. return PTR_ERR(vip->alloc_ctx);
  779. }
  780. return 0;
  781. }
  782. static void sta2x11_vip_release_buffer(struct sta2x11_vip *vip)
  783. {
  784. vb2_dma_contig_cleanup_ctx(vip->alloc_ctx);
  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;
  813. if (pin == -1)
  814. return 0;
  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 (pin != -1) {
  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. if (config->reset_pin >= 0) {
  897. ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
  898. config->reset_name);
  899. if (ret) {
  900. vip_gpio_release(&pdev->dev, config->pwr_pin,
  901. config->pwr_name);
  902. goto disable;
  903. }
  904. }
  905. if (config->pwr_pin != -1) {
  906. /* Datasheet says 5ms between PWR and RST */
  907. usleep_range(5000, 25000);
  908. ret = gpio_direction_output(config->pwr_pin, 1);
  909. }
  910. if (config->reset_pin != -1) {
  911. /* Datasheet says 5ms between PWR and RST */
  912. usleep_range(5000, 25000);
  913. ret = gpio_direction_output(config->reset_pin, 1);
  914. }
  915. usleep_range(5000, 25000);
  916. /* Allocate a new VIP instance */
  917. vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
  918. if (!vip) {
  919. ret = -ENOMEM;
  920. goto release_gpios;
  921. }
  922. vip->pdev = pdev;
  923. vip->std = V4L2_STD_PAL;
  924. vip->format = formats_50[0];
  925. vip->config = config;
  926. ret = sta2x11_vip_init_controls(vip);
  927. if (ret)
  928. goto free_mem;
  929. if (v4l2_device_register(&pdev->dev, &vip->v4l2_dev))
  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. /* Alloc, initialize and register video device */
  958. vip->video_dev = video_device_alloc();
  959. if (!vip->video_dev) {
  960. ret = -ENOMEM;
  961. goto release_irq;
  962. }
  963. vip->video_dev = &video_dev_template;
  964. vip->video_dev->v4l2_dev = &vip->v4l2_dev;
  965. vip->video_dev->queue = &vip->vb_vidq;
  966. set_bit(V4L2_FL_USE_FH_PRIO, &vip->video_dev->flags);
  967. video_set_drvdata(vip->video_dev, vip);
  968. ret = video_register_device(vip->video_dev, VFL_TYPE_GRABBER, -1);
  969. if (ret)
  970. goto vrelease;
  971. /* Get ADV7180 subdevice */
  972. vip->adapter = i2c_get_adapter(vip->config->i2c_id);
  973. if (!vip->adapter) {
  974. ret = -ENODEV;
  975. dev_err(&pdev->dev, "no I2C adapter found\n");
  976. goto vunreg;
  977. }
  978. vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
  979. "adv7180", vip->config->i2c_addr,
  980. NULL);
  981. if (!vip->decoder) {
  982. ret = -ENODEV;
  983. dev_err(&pdev->dev, "no decoder found\n");
  984. goto vunreg;
  985. }
  986. i2c_put_adapter(vip->adapter);
  987. v4l2_subdev_call(vip->decoder, core, init, 0);
  988. sta2x11_vip_init_register(vip);
  989. dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
  990. return 0;
  991. vunreg:
  992. video_set_drvdata(vip->video_dev, NULL);
  993. vrelease:
  994. if (video_is_registered(vip->video_dev))
  995. video_unregister_device(vip->video_dev);
  996. else
  997. video_device_release(vip->video_dev);
  998. release_irq:
  999. free_irq(pdev->irq, vip);
  1000. release_buf:
  1001. sta2x11_vip_release_buffer(vip);
  1002. pci_disable_msi(pdev);
  1003. unmap:
  1004. vb2_queue_release(&vip->vb_vidq);
  1005. pci_iounmap(pdev, vip->iomem);
  1006. release:
  1007. pci_release_regions(pdev);
  1008. unreg:
  1009. v4l2_device_unregister(&vip->v4l2_dev);
  1010. free_mem:
  1011. kfree(vip);
  1012. release_gpios:
  1013. vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
  1014. vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
  1015. disable:
  1016. /*
  1017. * do not call pci_disable_device on sta2x11 because it break all
  1018. * other Bus masters on this EP
  1019. */
  1020. return ret;
  1021. }
  1022. /**
  1023. * sta2x11_vip_remove_one - release device
  1024. * @pdev: PCI device
  1025. *
  1026. * Undo everything done in .._init_one
  1027. *
  1028. * unregister video device
  1029. * free interrupt
  1030. * unmap ioadresses
  1031. * free memory
  1032. * free GPIO pins
  1033. */
  1034. static void sta2x11_vip_remove_one(struct pci_dev *pdev)
  1035. {
  1036. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1037. struct sta2x11_vip *vip =
  1038. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1039. sta2x11_vip_clear_register(vip);
  1040. video_set_drvdata(vip->video_dev, NULL);
  1041. video_unregister_device(vip->video_dev);
  1042. /*do not call video_device_release() here, is already done */
  1043. free_irq(pdev->irq, vip);
  1044. pci_disable_msi(pdev);
  1045. vb2_queue_release(&vip->vb_vidq);
  1046. pci_iounmap(pdev, vip->iomem);
  1047. pci_release_regions(pdev);
  1048. v4l2_device_unregister(&vip->v4l2_dev);
  1049. vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
  1050. vip->config->pwr_name);
  1051. vip_gpio_release(&pdev->dev, vip->config->reset_pin,
  1052. vip->config->reset_name);
  1053. kfree(vip);
  1054. /*
  1055. * do not call pci_disable_device on sta2x11 because it break all
  1056. * other Bus masters on this EP
  1057. */
  1058. }
  1059. #ifdef CONFIG_PM
  1060. /**
  1061. * sta2x11_vip_suspend - set device into power save mode
  1062. * @pdev: PCI device
  1063. * @state: new state of device
  1064. *
  1065. * all relevant registers are saved and an attempt to set a new state is made.
  1066. *
  1067. * return value: 0 always indicate success,
  1068. * even if device could not be disabled. (workaround for hardware problem)
  1069. */
  1070. static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
  1071. {
  1072. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1073. struct sta2x11_vip *vip =
  1074. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1075. unsigned long flags;
  1076. int i;
  1077. spin_lock_irqsave(&vip->slock, flags);
  1078. vip->register_save_area[0] = reg_read(vip, DVP_CTL);
  1079. reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
  1080. vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
  1081. reg_write(vip, DVP_ITM, 0);
  1082. for (i = 1; i < SAVE_COUNT; i++)
  1083. vip->register_save_area[i] = reg_read(vip, 4 * i);
  1084. for (i = 0; i < AUX_COUNT; i++)
  1085. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
  1086. reg_read(vip, registers_to_save[i]);
  1087. spin_unlock_irqrestore(&vip->slock, flags);
  1088. /* save pci state */
  1089. pci_save_state(pdev);
  1090. if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
  1091. /*
  1092. * do not call pci_disable_device on sta2x11 because it
  1093. * break all other Bus masters on this EP
  1094. */
  1095. vip->disabled = 1;
  1096. }
  1097. pr_info("VIP: suspend\n");
  1098. return 0;
  1099. }
  1100. /**
  1101. * sta2x11_vip_resume - resume device operation
  1102. * @pdev : PCI device
  1103. *
  1104. * re-enable device, set PCI state to powered and restore registers.
  1105. * resume normal device operation afterwards.
  1106. *
  1107. * return value: 0, no error.
  1108. *
  1109. * other, could not set device to power on state.
  1110. */
  1111. static int sta2x11_vip_resume(struct pci_dev *pdev)
  1112. {
  1113. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1114. struct sta2x11_vip *vip =
  1115. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1116. unsigned long flags;
  1117. int ret, i;
  1118. pr_info("VIP: resume\n");
  1119. /* restore pci state */
  1120. if (vip->disabled) {
  1121. ret = pci_enable_device(pdev);
  1122. if (ret) {
  1123. pr_warn("VIP: Can't enable device.\n");
  1124. return ret;
  1125. }
  1126. vip->disabled = 0;
  1127. }
  1128. ret = pci_set_power_state(pdev, PCI_D0);
  1129. if (ret) {
  1130. /*
  1131. * do not call pci_disable_device on sta2x11 because it
  1132. * break all other Bus masters on this EP
  1133. */
  1134. pr_warn("VIP: Can't enable device.\n");
  1135. vip->disabled = 1;
  1136. return ret;
  1137. }
  1138. pci_restore_state(pdev);
  1139. spin_lock_irqsave(&vip->slock, flags);
  1140. for (i = 1; i < SAVE_COUNT; i++)
  1141. reg_write(vip, 4 * i, vip->register_save_area[i]);
  1142. for (i = 0; i < AUX_COUNT; i++)
  1143. reg_write(vip, registers_to_save[i],
  1144. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
  1145. reg_write(vip, DVP_CTL, vip->register_save_area[0]);
  1146. reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
  1147. spin_unlock_irqrestore(&vip->slock, flags);
  1148. return 0;
  1149. }
  1150. #endif
  1151. static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl) = {
  1152. {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
  1153. {0,}
  1154. };
  1155. static struct pci_driver sta2x11_vip_driver = {
  1156. .name = KBUILD_MODNAME,
  1157. .probe = sta2x11_vip_init_one,
  1158. .remove = sta2x11_vip_remove_one,
  1159. .id_table = sta2x11_vip_pci_tbl,
  1160. #ifdef CONFIG_PM
  1161. .suspend = sta2x11_vip_suspend,
  1162. .resume = sta2x11_vip_resume,
  1163. #endif
  1164. };
  1165. static int __init sta2x11_vip_init_module(void)
  1166. {
  1167. return pci_register_driver(&sta2x11_vip_driver);
  1168. }
  1169. static void __exit sta2x11_vip_exit_module(void)
  1170. {
  1171. pci_unregister_driver(&sta2x11_vip_driver);
  1172. }
  1173. #ifdef MODULE
  1174. module_init(sta2x11_vip_init_module);
  1175. module_exit(sta2x11_vip_exit_module);
  1176. #else
  1177. late_initcall_sync(sta2x11_vip_init_module);
  1178. #endif
  1179. MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
  1180. MODULE_AUTHOR("Wind River");
  1181. MODULE_LICENSE("GPL v2");
  1182. MODULE_SUPPORTED_DEVICE("sta2x11 video input");
  1183. MODULE_VERSION(DRV_VERSION);
  1184. MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);