sta2x11_vip.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /*
  2. * This is the driver for the STA2x11 Video Input Port.
  3. *
  4. * Copyright (C) 2010 WindRiver Systems, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Author: Andreas Kies <andreas.kies@windriver.com>
  23. * Vlad Lungu <vlad.lungu@windriver.com>
  24. *
  25. */
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/kmod.h>
  33. #include <linux/pci.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/mutex.h>
  36. #include <linux/io.h>
  37. #include <linux/gpio.h>
  38. #include <linux/i2c.h>
  39. #include <linux/delay.h>
  40. #include <media/v4l2-common.h>
  41. #include <media/v4l2-device.h>
  42. #include <media/v4l2-ioctl.h>
  43. #include <media/videobuf-dma-contig.h>
  44. #include "sta2x11_vip.h"
  45. #define DRV_NAME "sta2x11_vip"
  46. #define DRV_VERSION "1.3"
  47. #ifndef PCI_DEVICE_ID_STMICRO_VIP
  48. #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
  49. #endif
  50. #define MAX_FRAMES 4
  51. /*Register offsets*/
  52. #define DVP_CTL 0x00
  53. #define DVP_TFO 0x04
  54. #define DVP_TFS 0x08
  55. #define DVP_BFO 0x0C
  56. #define DVP_BFS 0x10
  57. #define DVP_VTP 0x14
  58. #define DVP_VBP 0x18
  59. #define DVP_VMP 0x1C
  60. #define DVP_ITM 0x98
  61. #define DVP_ITS 0x9C
  62. #define DVP_STA 0xA0
  63. #define DVP_HLFLN 0xA8
  64. #define DVP_RGB 0xC0
  65. #define DVP_PKZ 0xF0
  66. /*Register fields*/
  67. #define DVP_CTL_ENA 0x00000001
  68. #define DVP_CTL_RST 0x80000000
  69. #define DVP_CTL_DIS (~0x00040001)
  70. #define DVP_IT_VSB 0x00000008
  71. #define DVP_IT_VST 0x00000010
  72. #define DVP_IT_FIFO 0x00000020
  73. #define DVP_HLFLN_SD 0x00000001
  74. #define REG_WRITE(vip, reg, value) iowrite32((value), (vip->iomem)+(reg))
  75. #define REG_READ(vip, reg) ioread32((vip->iomem)+(reg))
  76. #define SAVE_COUNT 8
  77. #define AUX_COUNT 3
  78. #define IRQ_COUNT 1
  79. /**
  80. * struct sta2x11_vip - All internal data for one instance of device
  81. * @v4l2_dev: device registered in v4l layer
  82. * @video_dev: properties of our device
  83. * @pdev: PCI device
  84. * @adapter: contains I2C adapter information
  85. * @register_save_area: All relevant register are saved here during suspend
  86. * @decoder: contains information about video DAC
  87. * @format: pixel format, fixed UYVY
  88. * @std: video standard (e.g. PAL/NTSC)
  89. * @input: input line for video signal ( 0 or 1 )
  90. * @users: Number of open of device ( max. 1 )
  91. * @disabled: Device is in power down state
  92. * @mutex: ensures exclusive opening of device
  93. * @slock: for excluse acces of registers
  94. * @vb_vidq: queue maintained by videobuf layer
  95. * @capture: linked list of capture buffer
  96. * @active: struct videobuf_buffer currently beingg filled
  97. * @started: device is ready to capture frame
  98. * @closing: device will be shut down
  99. * @tcount: Number of top frames
  100. * @bcount: Number of bottom frames
  101. * @overflow: Number of FIFO overflows
  102. * @mem_spare: small buffer of unused frame
  103. * @dma_spare: dma addres of mem_spare
  104. * @iomem: hardware base address
  105. * @config: I2C and gpio config from platform
  106. *
  107. * All non-local data is accessed via this structure.
  108. */
  109. struct sta2x11_vip {
  110. struct v4l2_device v4l2_dev;
  111. struct video_device *video_dev;
  112. struct pci_dev *pdev;
  113. struct i2c_adapter *adapter;
  114. unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
  115. struct v4l2_subdev *decoder;
  116. struct v4l2_pix_format format;
  117. v4l2_std_id std;
  118. unsigned int input;
  119. int users;
  120. int disabled;
  121. struct mutex mutex; /* exclusive access during open */
  122. spinlock_t slock; /* spin lock for hardware and queue access */
  123. struct videobuf_queue vb_vidq;
  124. struct list_head capture;
  125. struct videobuf_buffer *active;
  126. int started, closing, tcount, bcount;
  127. int overflow;
  128. void *mem_spare;
  129. dma_addr_t dma_spare;
  130. void *iomem;
  131. struct vip_config *config;
  132. };
  133. static const unsigned int registers_to_save[AUX_COUNT] = {
  134. DVP_HLFLN, DVP_RGB, DVP_PKZ
  135. };
  136. static struct v4l2_pix_format formats_50[] = {
  137. { /*PAL interlaced */
  138. .width = 720,
  139. .height = 576,
  140. .pixelformat = V4L2_PIX_FMT_UYVY,
  141. .field = V4L2_FIELD_INTERLACED,
  142. .bytesperline = 720 * 2,
  143. .sizeimage = 720 * 2 * 576,
  144. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  145. { /*PAL top */
  146. .width = 720,
  147. .height = 288,
  148. .pixelformat = V4L2_PIX_FMT_UYVY,
  149. .field = V4L2_FIELD_TOP,
  150. .bytesperline = 720 * 2,
  151. .sizeimage = 720 * 2 * 288,
  152. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  153. { /*PAL bottom */
  154. .width = 720,
  155. .height = 288,
  156. .pixelformat = V4L2_PIX_FMT_UYVY,
  157. .field = V4L2_FIELD_BOTTOM,
  158. .bytesperline = 720 * 2,
  159. .sizeimage = 720 * 2 * 288,
  160. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  161. };
  162. static struct v4l2_pix_format formats_60[] = {
  163. { /*NTSC interlaced */
  164. .width = 720,
  165. .height = 480,
  166. .pixelformat = V4L2_PIX_FMT_UYVY,
  167. .field = V4L2_FIELD_INTERLACED,
  168. .bytesperline = 720 * 2,
  169. .sizeimage = 720 * 2 * 480,
  170. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  171. { /*NTSC top */
  172. .width = 720,
  173. .height = 240,
  174. .pixelformat = V4L2_PIX_FMT_UYVY,
  175. .field = V4L2_FIELD_TOP,
  176. .bytesperline = 720 * 2,
  177. .sizeimage = 720 * 2 * 240,
  178. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  179. { /*NTSC bottom */
  180. .width = 720,
  181. .height = 240,
  182. .pixelformat = V4L2_PIX_FMT_UYVY,
  183. .field = V4L2_FIELD_BOTTOM,
  184. .bytesperline = 720 * 2,
  185. .sizeimage = 720 * 2 * 240,
  186. .colorspace = V4L2_COLORSPACE_SMPTE170M},
  187. };
  188. /**
  189. * buf_setup - Get size and number of video buffer
  190. * @vq: queue in videobuf
  191. * @count: Number of buffers (1..MAX_FRAMES).
  192. * 0 use default value.
  193. * @size: size of buffer in bytes
  194. *
  195. * returns size and number of buffers
  196. * a preset value of 0 returns the default number.
  197. * return value: 0, always succesfull.
  198. */
  199. static int buf_setup(struct videobuf_queue *vq, unsigned int *count,
  200. unsigned int *size)
  201. {
  202. struct sta2x11_vip *vip = vq->priv_data;
  203. *size = vip->format.width * vip->format.height * 2;
  204. if (0 == *count || MAX_FRAMES < *count)
  205. *count = MAX_FRAMES;
  206. return 0;
  207. };
  208. /**
  209. * buf_prepare - prepare buffer for usage
  210. * @vq: queue in videobuf layer
  211. * @vb: buffer to be prepared
  212. * @field: type of video data (interlaced/non-interlaced)
  213. *
  214. * Allocate or realloc buffer
  215. * return value: 0, successful.
  216. *
  217. * -EINVAL, supplied buffer is too small.
  218. *
  219. * other, buffer could not be locked.
  220. */
  221. static int buf_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  222. enum v4l2_field field)
  223. {
  224. struct sta2x11_vip *vip = vq->priv_data;
  225. int ret;
  226. vb->size = vip->format.width * vip->format.height * 2;
  227. if ((0 != vb->baddr) && (vb->bsize < vb->size))
  228. return -EINVAL;
  229. vb->width = vip->format.width;
  230. vb->height = vip->format.height;
  231. vb->field = field;
  232. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  233. ret = videobuf_iolock(vq, vb, NULL);
  234. if (ret)
  235. goto fail;
  236. }
  237. vb->state = VIDEOBUF_PREPARED;
  238. return 0;
  239. fail:
  240. videobuf_dma_contig_free(vq, vb);
  241. vb->state = VIDEOBUF_NEEDS_INIT;
  242. return ret;
  243. }
  244. /**
  245. * buf_queu - queue buffer for filling
  246. * @vq: queue in videobuf layer
  247. * @vb: buffer to be queued
  248. *
  249. * if capturing is already running, the buffer will be queued. Otherwise
  250. * capture is started and the buffer is used directly.
  251. */
  252. static void buf_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  253. {
  254. struct sta2x11_vip *vip = vq->priv_data;
  255. u32 dma;
  256. vb->state = VIDEOBUF_QUEUED;
  257. if (vip->active) {
  258. list_add_tail(&vb->queue, &vip->capture);
  259. return;
  260. }
  261. vip->started = 1;
  262. vip->tcount = 0;
  263. vip->bcount = 0;
  264. vip->active = vb;
  265. vb->state = VIDEOBUF_ACTIVE;
  266. dma = videobuf_to_dma_contig(vb);
  267. REG_WRITE(vip, DVP_TFO, (0 << 16) | (0));
  268. /* despite of interlace mode, upper and lower frames start at zero */
  269. REG_WRITE(vip, DVP_BFO, (0 << 16) | (0));
  270. switch (vip->format.field) {
  271. case V4L2_FIELD_INTERLACED:
  272. REG_WRITE(vip, DVP_TFS,
  273. ((vip->format.height / 2 - 1) << 16) |
  274. (2 * vip->format.width - 1));
  275. REG_WRITE(vip, DVP_BFS, ((vip->format.height / 2 - 1) << 16) |
  276. (2 * vip->format.width - 1));
  277. REG_WRITE(vip, DVP_VTP, dma);
  278. REG_WRITE(vip, DVP_VBP, dma + vip->format.width * 2);
  279. REG_WRITE(vip, DVP_VMP, 4 * vip->format.width);
  280. break;
  281. case V4L2_FIELD_TOP:
  282. REG_WRITE(vip, DVP_TFS,
  283. ((vip->format.height - 1) << 16) |
  284. (2 * vip->format.width - 1));
  285. REG_WRITE(vip, DVP_BFS, ((0) << 16) |
  286. (2 * vip->format.width - 1));
  287. REG_WRITE(vip, DVP_VTP, dma);
  288. REG_WRITE(vip, DVP_VBP, dma);
  289. REG_WRITE(vip, DVP_VMP, 2 * vip->format.width);
  290. break;
  291. case V4L2_FIELD_BOTTOM:
  292. REG_WRITE(vip, DVP_TFS, ((0) << 16) |
  293. (2 * vip->format.width - 1));
  294. REG_WRITE(vip, DVP_BFS,
  295. ((vip->format.height) << 16) |
  296. (2 * vip->format.width - 1));
  297. REG_WRITE(vip, DVP_VTP, dma);
  298. REG_WRITE(vip, DVP_VBP, dma);
  299. REG_WRITE(vip, DVP_VMP, 2 * vip->format.width);
  300. break;
  301. default:
  302. pr_warning("VIP: unknown field format\n");
  303. return;
  304. }
  305. REG_WRITE(vip, DVP_CTL, DVP_CTL_ENA);
  306. }
  307. /**
  308. * buff_release - release buffer
  309. * @vq: queue in videobuf layer
  310. * @vb: buffer to be released
  311. *
  312. * release buffer in videobuf layer
  313. */
  314. static void buf_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  315. {
  316. videobuf_dma_contig_free(vq, vb);
  317. vb->state = VIDEOBUF_NEEDS_INIT;
  318. }
  319. static struct videobuf_queue_ops vip_qops = {
  320. .buf_setup = buf_setup,
  321. .buf_prepare = buf_prepare,
  322. .buf_queue = buf_queue,
  323. .buf_release = buf_release,
  324. };
  325. /**
  326. * vip_open - open video device
  327. * @file: descriptor of device
  328. *
  329. * open device, make sure it is only opened once.
  330. * return value: 0, no error.
  331. *
  332. * -EBUSY, device is already opened
  333. *
  334. * -ENOMEM, no memory for auxiliary DMA buffer
  335. */
  336. static int vip_open(struct file *file)
  337. {
  338. struct video_device *dev = video_devdata(file);
  339. struct sta2x11_vip *vip = video_get_drvdata(dev);
  340. mutex_lock(&vip->mutex);
  341. vip->users++;
  342. if (vip->users > 1) {
  343. vip->users--;
  344. mutex_unlock(&vip->mutex);
  345. return -EBUSY;
  346. }
  347. file->private_data = dev;
  348. vip->overflow = 0;
  349. vip->started = 0;
  350. vip->closing = 0;
  351. vip->active = NULL;
  352. INIT_LIST_HEAD(&vip->capture);
  353. vip->mem_spare = dma_alloc_coherent(&vip->pdev->dev, 64,
  354. &vip->dma_spare, GFP_KERNEL);
  355. if (!vip->mem_spare) {
  356. vip->users--;
  357. mutex_unlock(&vip->mutex);
  358. return -ENOMEM;
  359. }
  360. mutex_unlock(&vip->mutex);
  361. videobuf_queue_dma_contig_init_cached(&vip->vb_vidq,
  362. &vip_qops,
  363. &vip->pdev->dev,
  364. &vip->slock,
  365. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  366. V4L2_FIELD_INTERLACED,
  367. sizeof(struct videobuf_buffer),
  368. vip, NULL);
  369. REG_READ(vip, DVP_ITS);
  370. REG_WRITE(vip, DVP_HLFLN, DVP_HLFLN_SD);
  371. REG_WRITE(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
  372. REG_WRITE(vip, DVP_CTL, DVP_CTL_RST);
  373. REG_WRITE(vip, DVP_CTL, 0);
  374. REG_READ(vip, DVP_ITS);
  375. return 0;
  376. }
  377. /**
  378. * vip_close - close video device
  379. * @file: descriptor of device
  380. *
  381. * close video device, wait until all pending operations are finished
  382. * ( maximum FRAME_MAX buffers pending )
  383. * Turn off interrupts.
  384. *
  385. * return value: 0, always succesful.
  386. */
  387. static int vip_close(struct file *file)
  388. {
  389. struct video_device *dev = video_devdata(file);
  390. struct sta2x11_vip *vip = video_get_drvdata(dev);
  391. vip->closing = 1;
  392. if (vip->active)
  393. videobuf_waiton(&vip->vb_vidq, vip->active, 0, 0);
  394. spin_lock_irq(&vip->slock);
  395. REG_WRITE(vip, DVP_ITM, 0);
  396. REG_WRITE(vip, DVP_CTL, DVP_CTL_RST);
  397. REG_WRITE(vip, DVP_CTL, 0);
  398. REG_READ(vip, DVP_ITS);
  399. vip->started = 0;
  400. vip->active = NULL;
  401. spin_unlock_irq(&vip->slock);
  402. videobuf_stop(&vip->vb_vidq);
  403. videobuf_mmap_free(&vip->vb_vidq);
  404. dma_free_coherent(&vip->pdev->dev, 64, vip->mem_spare, vip->dma_spare);
  405. file->private_data = NULL;
  406. mutex_lock(&vip->mutex);
  407. vip->users--;
  408. mutex_unlock(&vip->mutex);
  409. return 0;
  410. }
  411. /**
  412. * vip_read - read from video input
  413. * @file: descriptor of device
  414. * @data: user buffer
  415. * @count: number of bytes to be read
  416. * @ppos: position within stream
  417. *
  418. * read video data from video device.
  419. * handling is done in generic videobuf layer
  420. * return value: provided by videobuf layer
  421. */
  422. static ssize_t vip_read(struct file *file, char __user *data,
  423. size_t count, loff_t *ppos)
  424. {
  425. struct video_device *dev = file->private_data;
  426. struct sta2x11_vip *vip = video_get_drvdata(dev);
  427. return videobuf_read_stream(&vip->vb_vidq, data, count, ppos, 0,
  428. file->f_flags & O_NONBLOCK);
  429. }
  430. /**
  431. * vip_mmap - map user buffer
  432. * @file: descriptor of device
  433. * @vma: user buffer
  434. *
  435. * map user space buffer into kernel mode, including DMA address.
  436. * handling is done in generic videobuf layer.
  437. * return value: provided by videobuf layer
  438. */
  439. static int vip_mmap(struct file *file, struct vm_area_struct *vma)
  440. {
  441. struct video_device *dev = file->private_data;
  442. struct sta2x11_vip *vip = video_get_drvdata(dev);
  443. return videobuf_mmap_mapper(&vip->vb_vidq, vma);
  444. }
  445. /**
  446. * vip_poll - poll for event
  447. * @file: descriptor of device
  448. * @wait: contains events to be waited for
  449. *
  450. * wait for event related to video device.
  451. * handling is done in generic videobuf layer.
  452. * return value: provided by videobuf layer
  453. */
  454. static unsigned int vip_poll(struct file *file, struct poll_table_struct *wait)
  455. {
  456. struct video_device *dev = file->private_data;
  457. struct sta2x11_vip *vip = video_get_drvdata(dev);
  458. return videobuf_poll_stream(file, &vip->vb_vidq, wait);
  459. }
  460. /**
  461. * vidioc_querycap - return capabilities of device
  462. * @file: descriptor of device (not used)
  463. * @priv: points to current videodevice
  464. * @cap: contains return values
  465. *
  466. * the capabilities of the device are returned
  467. *
  468. * return value: 0, no error.
  469. */
  470. static int vidioc_querycap(struct file *file, void *priv,
  471. struct v4l2_capability *cap)
  472. {
  473. struct video_device *dev = priv;
  474. struct sta2x11_vip *vip = video_get_drvdata(dev);
  475. memset(cap, 0, sizeof(struct v4l2_capability));
  476. strcpy(cap->driver, DRV_NAME);
  477. strcpy(cap->card, DRV_NAME);
  478. cap->version = 0;
  479. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  480. pci_name(vip->pdev));
  481. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  482. V4L2_CAP_STREAMING;
  483. return 0;
  484. }
  485. /**
  486. * vidioc_s_std - set video standard
  487. * @file: descriptor of device (not used)
  488. * @priv: points to current videodevice
  489. * @std: contains standard to be set
  490. *
  491. * the video standard is set
  492. *
  493. * return value: 0, no error.
  494. *
  495. * -EIO, no input signal detected
  496. *
  497. * other, returned from video DAC.
  498. */
  499. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
  500. {
  501. struct video_device *dev = priv;
  502. struct sta2x11_vip *vip = video_get_drvdata(dev);
  503. v4l2_std_id oldstd = vip->std, newstd;
  504. int status;
  505. if (V4L2_STD_ALL == *std) {
  506. v4l2_subdev_call(vip->decoder, core, s_std, *std);
  507. ssleep(2);
  508. v4l2_subdev_call(vip->decoder, video, querystd, &newstd);
  509. v4l2_subdev_call(vip->decoder, video, g_input_status, &status);
  510. if (status & V4L2_IN_ST_NO_SIGNAL)
  511. return -EIO;
  512. *std = vip->std = newstd;
  513. if (oldstd != *std) {
  514. if (V4L2_STD_525_60 & (*std))
  515. vip->format = formats_60[0];
  516. else
  517. vip->format = formats_50[0];
  518. }
  519. return 0;
  520. }
  521. if (oldstd != *std) {
  522. if (V4L2_STD_525_60 & (*std))
  523. vip->format = formats_60[0];
  524. else
  525. vip->format = formats_50[0];
  526. }
  527. return v4l2_subdev_call(vip->decoder, core, s_std, *std);
  528. }
  529. /**
  530. * vidioc_g_std - get video standard
  531. * @file: descriptor of device (not used)
  532. * @priv: points to current videodevice
  533. * @std: contains return values
  534. *
  535. * the current video standard is returned
  536. *
  537. * return value: 0, no error.
  538. */
  539. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
  540. {
  541. struct video_device *dev = priv;
  542. struct sta2x11_vip *vip = video_get_drvdata(dev);
  543. *std = vip->std;
  544. return 0;
  545. }
  546. /**
  547. * vidioc_querystd - get possible video standards
  548. * @file: descriptor of device (not used)
  549. * @priv: points to current videodevice
  550. * @std: contains return values
  551. *
  552. * all possible video standards are returned
  553. *
  554. * return value: delivered by video DAC routine.
  555. */
  556. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
  557. {
  558. struct video_device *dev = priv;
  559. struct sta2x11_vip *vip = video_get_drvdata(dev);
  560. return v4l2_subdev_call(vip->decoder, video, querystd, std);
  561. }
  562. /**
  563. * vidioc_queryctl - get possible control settings
  564. * @file: descriptor of device (not used)
  565. * @priv: points to current videodevice
  566. * @ctrl: contains return values
  567. *
  568. * return possible values for a control
  569. * return value: delivered by video DAC routine.
  570. */
  571. static int vidioc_queryctrl(struct file *file, void *priv,
  572. struct v4l2_queryctrl *ctrl)
  573. {
  574. struct video_device *dev = priv;
  575. struct sta2x11_vip *vip = video_get_drvdata(dev);
  576. return v4l2_subdev_call(vip->decoder, core, queryctrl, ctrl);
  577. }
  578. /**
  579. * vidioc_g_ctl - get control value
  580. * @file: descriptor of device (not used)
  581. * @priv: points to current videodevice
  582. * @ctrl: contains return values
  583. *
  584. * return setting for a control value
  585. * return value: delivered by video DAC routine.
  586. */
  587. static int vidioc_g_ctrl(struct file *file, void *priv,
  588. struct v4l2_control *ctrl)
  589. {
  590. struct video_device *dev = priv;
  591. struct sta2x11_vip *vip = video_get_drvdata(dev);
  592. return v4l2_subdev_call(vip->decoder, core, g_ctrl, ctrl);
  593. }
  594. /**
  595. * vidioc_s_ctl - set control value
  596. * @file: descriptor of device (not used)
  597. * @priv: points to current videodevice
  598. * @ctrl: contains value to be set
  599. *
  600. * set value for a specific control
  601. * return value: delivered by video DAC routine.
  602. */
  603. static int vidioc_s_ctrl(struct file *file, void *priv,
  604. struct v4l2_control *ctrl)
  605. {
  606. struct video_device *dev = priv;
  607. struct sta2x11_vip *vip = video_get_drvdata(dev);
  608. return v4l2_subdev_call(vip->decoder, core, s_ctrl, ctrl);
  609. }
  610. /**
  611. * vidioc_enum_input - return name of input line
  612. * @file: descriptor of device (not used)
  613. * @priv: points to current videodevice
  614. * @inp: contains return values
  615. *
  616. * the user friendly name of the input line is returned
  617. *
  618. * return value: 0, no error.
  619. *
  620. * -EINVAL, input line number out of range
  621. */
  622. static int vidioc_enum_input(struct file *file, void *priv,
  623. struct v4l2_input *inp)
  624. {
  625. if (inp->index > 1)
  626. return -EINVAL;
  627. inp->type = V4L2_INPUT_TYPE_CAMERA;
  628. inp->std = V4L2_STD_ALL;
  629. sprintf(inp->name, "Camera %u", inp->index);
  630. return 0;
  631. }
  632. /**
  633. * vidioc_s_input - set input line
  634. * @file: descriptor of device ( not used)
  635. * @priv: points to current videodevice
  636. * @i: new input line number
  637. *
  638. * the current active input line is set
  639. *
  640. * return value: 0, no error.
  641. *
  642. * -EINVAL, line number out of range
  643. */
  644. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  645. {
  646. struct video_device *dev = priv;
  647. struct sta2x11_vip *vip = video_get_drvdata(dev);
  648. int ret;
  649. if (i > 1)
  650. return -EINVAL;
  651. ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
  652. if (!ret)
  653. vip->input = i;
  654. return 0;
  655. }
  656. /**
  657. * vidioc_g_input - return input line
  658. * @file: descriptor of device ( not used)
  659. * @priv: points to current videodevice
  660. * @i: returned input line number
  661. *
  662. * the current active input line is returned
  663. *
  664. * return value: always 0.
  665. */
  666. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  667. {
  668. struct video_device *dev = priv;
  669. struct sta2x11_vip *vip = video_get_drvdata(dev);
  670. *i = vip->input;
  671. return 0;
  672. }
  673. /**
  674. * vidioc_enum_fmt_vid_cap - return video capture format
  675. * @file: descriptor of device ( not used)
  676. * @priv: points to current videodevice
  677. * @f: returned format information
  678. *
  679. * returns name and format of video capture
  680. * Only UYVY is supported by hardware.
  681. *
  682. * return value: always 0.
  683. */
  684. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  685. struct v4l2_fmtdesc *f)
  686. {
  687. if (f->index != 0)
  688. return -EINVAL;
  689. strcpy(f->description, "4:2:2, packed, UYVY");
  690. f->pixelformat = V4L2_PIX_FMT_UYVY;
  691. f->flags = 0;
  692. return 0;
  693. }
  694. /**
  695. * vidioc_try_fmt_vid_cap - set video capture format
  696. * @file: descriptor of device ( not used)
  697. * @priv: points to current videodevice
  698. * @f: new format
  699. *
  700. * new video format is set which includes width and
  701. * field type. width is fixed to 720, no scaling.
  702. * Only UYVY is supported by this hardware.
  703. * the minimum height is 200, the maximum is 576 (PAL)
  704. *
  705. * return value: 0, no error
  706. *
  707. * -EINVAL, pixel or field format not supported
  708. *
  709. */
  710. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  711. struct v4l2_format *f)
  712. {
  713. struct video_device *dev = priv;
  714. struct sta2x11_vip *vip = video_get_drvdata(dev);
  715. int interlace_lim;
  716. if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat)
  717. return -EINVAL;
  718. if (V4L2_STD_525_60 & vip->std)
  719. interlace_lim = 240;
  720. else
  721. interlace_lim = 288;
  722. switch (f->fmt.pix.field) {
  723. case V4L2_FIELD_ANY:
  724. if (interlace_lim < f->fmt.pix.height)
  725. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  726. else
  727. f->fmt.pix.field = V4L2_FIELD_BOTTOM;
  728. break;
  729. case V4L2_FIELD_TOP:
  730. case V4L2_FIELD_BOTTOM:
  731. if (interlace_lim < f->fmt.pix.height)
  732. f->fmt.pix.height = interlace_lim;
  733. break;
  734. case V4L2_FIELD_INTERLACED:
  735. break;
  736. default:
  737. return -EINVAL;
  738. }
  739. f->fmt.pix.height &= ~1;
  740. if (2 * interlace_lim < f->fmt.pix.height)
  741. f->fmt.pix.height = 2 * interlace_lim;
  742. if (200 > f->fmt.pix.height)
  743. f->fmt.pix.height = 200;
  744. f->fmt.pix.width = 720;
  745. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  746. f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
  747. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  748. f->fmt.pix.priv = 0;
  749. return 0;
  750. }
  751. /**
  752. * vidioc_s_fmt_vid_cap - set current video format parameters
  753. * @file: descriptor of device ( not used)
  754. * @priv: points to current videodevice
  755. * @f: returned format information
  756. *
  757. * set new capture format
  758. * return value: 0, no error
  759. *
  760. * other, delivered by video DAC routine.
  761. */
  762. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  763. struct v4l2_format *f)
  764. {
  765. struct video_device *dev = priv;
  766. struct sta2x11_vip *vip = video_get_drvdata(dev);
  767. int ret;
  768. ret = vidioc_try_fmt_vid_cap(file, priv, f);
  769. if (ret)
  770. return ret;
  771. memcpy(&vip->format, &f->fmt.pix, sizeof(struct v4l2_pix_format));
  772. return 0;
  773. }
  774. /**
  775. * vidioc_g_fmt_vid_cap - get current video format parameters
  776. * @file: descriptor of device ( not used)
  777. * @priv: points to current videodevice
  778. * @f: contains format information
  779. *
  780. * returns current video format parameters
  781. *
  782. * return value: 0, always successful
  783. */
  784. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  785. struct v4l2_format *f)
  786. {
  787. struct video_device *dev = priv;
  788. struct sta2x11_vip *vip = video_get_drvdata(dev);
  789. memcpy(&f->fmt.pix, &vip->format, sizeof(struct v4l2_pix_format));
  790. return 0;
  791. }
  792. /**
  793. * vidioc_reqfs - request buffer
  794. * @file: descriptor of device ( not used)
  795. * @priv: points to current videodevice
  796. * @p: video buffer
  797. *
  798. * Handling is done in generic videobuf layer.
  799. */
  800. static int vidioc_reqbufs(struct file *file, void *priv,
  801. struct v4l2_requestbuffers *p)
  802. {
  803. struct video_device *dev = priv;
  804. struct sta2x11_vip *vip = video_get_drvdata(dev);
  805. return videobuf_reqbufs(&vip->vb_vidq, p);
  806. }
  807. /**
  808. * vidioc_querybuf - query buffer
  809. * @file: descriptor of device ( not used)
  810. * @priv: points to current videodevice
  811. * @p: video buffer
  812. *
  813. * query buffer state.
  814. * Handling is done in generic videobuf layer.
  815. */
  816. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  817. {
  818. struct video_device *dev = priv;
  819. struct sta2x11_vip *vip = video_get_drvdata(dev);
  820. return videobuf_querybuf(&vip->vb_vidq, p);
  821. }
  822. /**
  823. * vidioc_qbuf - queue a buffer
  824. * @file: descriptor of device ( not used)
  825. * @priv: points to current videodevice
  826. * @p: video buffer
  827. *
  828. * Handling is done in generic videobuf layer.
  829. */
  830. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  831. {
  832. struct video_device *dev = priv;
  833. struct sta2x11_vip *vip = video_get_drvdata(dev);
  834. return videobuf_qbuf(&vip->vb_vidq, p);
  835. }
  836. /**
  837. * vidioc_dqbuf - dequeue a buffer
  838. * @file: descriptor of device ( not used)
  839. * @priv: points to current videodevice
  840. * @p: video buffer
  841. *
  842. * Handling is done in generic videobuf layer.
  843. */
  844. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  845. {
  846. struct video_device *dev = priv;
  847. struct sta2x11_vip *vip = video_get_drvdata(dev);
  848. return videobuf_dqbuf(&vip->vb_vidq, p, file->f_flags & O_NONBLOCK);
  849. }
  850. /**
  851. * vidioc_streamon - turn on streaming
  852. * @file: descriptor of device ( not used)
  853. * @priv: points to current videodevice
  854. * @type: type of capture
  855. *
  856. * turn on streaming.
  857. * Handling is done in generic videobuf layer.
  858. */
  859. static int vidioc_streamon(struct file *file, void *priv,
  860. enum v4l2_buf_type type)
  861. {
  862. struct video_device *dev = priv;
  863. struct sta2x11_vip *vip = video_get_drvdata(dev);
  864. return videobuf_streamon(&vip->vb_vidq);
  865. }
  866. /**
  867. * vidioc_streamoff - turn off streaming
  868. * @file: descriptor of device ( not used)
  869. * @priv: points to current videodevice
  870. * @type: type of capture
  871. *
  872. * turn off streaming.
  873. * Handling is done in generic videobuf layer.
  874. */
  875. static int vidioc_streamoff(struct file *file, void *priv,
  876. enum v4l2_buf_type type)
  877. {
  878. struct video_device *dev = priv;
  879. struct sta2x11_vip *vip = video_get_drvdata(dev);
  880. return videobuf_streamoff(&vip->vb_vidq);
  881. }
  882. static const struct v4l2_file_operations vip_fops = {
  883. .owner = THIS_MODULE,
  884. .open = vip_open,
  885. .release = vip_close,
  886. .ioctl = video_ioctl2,
  887. .read = vip_read,
  888. .mmap = vip_mmap,
  889. .poll = vip_poll
  890. };
  891. static const struct v4l2_ioctl_ops vip_ioctl_ops = {
  892. .vidioc_querycap = vidioc_querycap,
  893. .vidioc_s_std = vidioc_s_std,
  894. .vidioc_g_std = vidioc_g_std,
  895. .vidioc_querystd = vidioc_querystd,
  896. .vidioc_queryctrl = vidioc_queryctrl,
  897. .vidioc_g_ctrl = vidioc_g_ctrl,
  898. .vidioc_s_ctrl = vidioc_s_ctrl,
  899. .vidioc_enum_input = vidioc_enum_input,
  900. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  901. .vidioc_s_input = vidioc_s_input,
  902. .vidioc_g_input = vidioc_g_input,
  903. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  904. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  905. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  906. .vidioc_reqbufs = vidioc_reqbufs,
  907. .vidioc_querybuf = vidioc_querybuf,
  908. .vidioc_qbuf = vidioc_qbuf,
  909. .vidioc_dqbuf = vidioc_dqbuf,
  910. .vidioc_streamon = vidioc_streamon,
  911. .vidioc_streamoff = vidioc_streamoff,
  912. };
  913. static struct video_device video_dev_template = {
  914. .name = DRV_NAME,
  915. .release = video_device_release,
  916. .fops = &vip_fops,
  917. .ioctl_ops = &vip_ioctl_ops,
  918. .tvnorms = V4L2_STD_ALL,
  919. };
  920. /**
  921. * vip_irq - interrupt routine
  922. * @irq: Number of interrupt ( not used, correct number is assumed )
  923. * @vip: local data structure containing all information
  924. *
  925. * check for both frame interrupts set ( top and bottom ).
  926. * check FIFO overflow, but limit number of log messages after open.
  927. * signal a complete buffer if done.
  928. * dequeue a new buffer if available.
  929. * disable VIP if no buffer available.
  930. *
  931. * return value: IRQ_NONE, interrupt was not generated by VIP
  932. *
  933. * IRQ_HANDLED, interrupt done.
  934. */
  935. static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
  936. {
  937. u32 status, dma;
  938. unsigned long flags;
  939. struct videobuf_buffer *vb;
  940. status = REG_READ(vip, DVP_ITS);
  941. if (!status) {
  942. pr_debug("VIP: irq ignored\n");
  943. return IRQ_NONE;
  944. }
  945. if (!vip->started)
  946. return IRQ_HANDLED;
  947. if (status & DVP_IT_VSB)
  948. vip->bcount++;
  949. if (status & DVP_IT_VST)
  950. vip->tcount++;
  951. if ((DVP_IT_VSB | DVP_IT_VST) == (status & (DVP_IT_VST | DVP_IT_VSB))) {
  952. /* this is bad, we are too slow, hope the condition is gone
  953. * on the next frame */
  954. pr_info("VIP: both irqs\n");
  955. return IRQ_HANDLED;
  956. }
  957. if (status & DVP_IT_FIFO) {
  958. if (5 > vip->overflow++)
  959. pr_info("VIP: fifo overflow\n");
  960. }
  961. if (2 > vip->tcount)
  962. return IRQ_HANDLED;
  963. if (status & DVP_IT_VSB)
  964. return IRQ_HANDLED;
  965. spin_lock_irqsave(&vip->slock, flags);
  966. REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) & ~DVP_CTL_ENA);
  967. if (vip->active) {
  968. do_gettimeofday(&vip->active->ts);
  969. vip->active->field_count++;
  970. vip->active->state = VIDEOBUF_DONE;
  971. wake_up(&vip->active->done);
  972. vip->active = NULL;
  973. }
  974. if (!vip->closing) {
  975. if (list_empty(&vip->capture))
  976. goto done;
  977. vb = list_first_entry(&vip->capture, struct videobuf_buffer,
  978. queue);
  979. if (NULL == vb) {
  980. pr_info("VIP: no buffer\n");
  981. goto done;
  982. }
  983. vb->state = VIDEOBUF_ACTIVE;
  984. list_del(&vb->queue);
  985. vip->active = vb;
  986. dma = videobuf_to_dma_contig(vb);
  987. switch (vip->format.field) {
  988. case V4L2_FIELD_INTERLACED:
  989. REG_WRITE(vip, DVP_VTP, dma);
  990. REG_WRITE(vip, DVP_VBP, dma + vip->format.width * 2);
  991. break;
  992. case V4L2_FIELD_TOP:
  993. case V4L2_FIELD_BOTTOM:
  994. REG_WRITE(vip, DVP_VTP, dma);
  995. REG_WRITE(vip, DVP_VBP, dma);
  996. break;
  997. default:
  998. pr_warning("VIP: unknown field format\n");
  999. goto done;
  1000. break;
  1001. }
  1002. REG_WRITE(vip, DVP_CTL, REG_READ(vip, DVP_CTL) | DVP_CTL_ENA);
  1003. }
  1004. done:
  1005. spin_unlock_irqrestore(&vip->slock, flags);
  1006. return IRQ_HANDLED;
  1007. }
  1008. /**
  1009. * vip_gpio_reserve - reserve gpio pin
  1010. * @dev: device
  1011. * @pin: GPIO pin number
  1012. * @dir: direction, input or output
  1013. * @name: GPIO pin name
  1014. *
  1015. */
  1016. static int vip_gpio_reserve(struct device *dev, int pin, int dir,
  1017. const char *name)
  1018. {
  1019. int ret;
  1020. if (pin == -1)
  1021. return 0;
  1022. ret = gpio_request(pin, name);
  1023. if (ret) {
  1024. dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
  1025. return ret;
  1026. }
  1027. ret = gpio_direction_output(pin, dir);
  1028. if (ret) {
  1029. dev_err(dev, "Failed to set direction for pin %d (%s)\n",
  1030. pin, name);
  1031. gpio_free(pin);
  1032. return ret;
  1033. }
  1034. ret = gpio_export(pin, false);
  1035. if (ret) {
  1036. dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
  1037. gpio_free(pin);
  1038. return ret;
  1039. }
  1040. return 0;
  1041. }
  1042. /**
  1043. * vip_gpio_release - release gpio pin
  1044. * @dev: device
  1045. * @pin: GPIO pin number
  1046. * @name: GPIO pin name
  1047. *
  1048. */
  1049. static void vip_gpio_release(struct device *dev, int pin, const char *name)
  1050. {
  1051. if (pin != -1) {
  1052. dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
  1053. gpio_unexport(pin);
  1054. gpio_free(pin);
  1055. }
  1056. }
  1057. /**
  1058. * sta2x11_vip_init_one - init one instance of video device
  1059. * @pdev: PCI device
  1060. * @ent: (not used)
  1061. *
  1062. * allocate reset pins for DAC.
  1063. * Reset video DAC, this is done via reset line.
  1064. * allocate memory for managing device
  1065. * request interrupt
  1066. * map IO region
  1067. * register device
  1068. * find and initialize video DAC
  1069. *
  1070. * return value: 0, no error
  1071. *
  1072. * -ENOMEM, no memory
  1073. *
  1074. * -ENODEV, device could not be detected or registered
  1075. */
  1076. static int sta2x11_vip_init_one(struct pci_dev *pdev,
  1077. const struct pci_device_id *ent)
  1078. {
  1079. int ret;
  1080. struct sta2x11_vip *vip;
  1081. struct vip_config *config;
  1082. ret = pci_enable_device(pdev);
  1083. if (ret)
  1084. return ret;
  1085. config = dev_get_platdata(&pdev->dev);
  1086. if (!config) {
  1087. dev_info(&pdev->dev, "VIP slot disabled\n");
  1088. ret = -EINVAL;
  1089. goto disable;
  1090. }
  1091. ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
  1092. config->pwr_name);
  1093. if (ret)
  1094. goto disable;
  1095. if (config->reset_pin >= 0) {
  1096. ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
  1097. config->reset_name);
  1098. if (ret) {
  1099. vip_gpio_release(&pdev->dev, config->pwr_pin,
  1100. config->pwr_name);
  1101. goto disable;
  1102. }
  1103. }
  1104. if (config->pwr_pin != -1) {
  1105. /* Datasheet says 5ms between PWR and RST */
  1106. usleep_range(5000, 25000);
  1107. ret = gpio_direction_output(config->pwr_pin, 1);
  1108. }
  1109. if (config->reset_pin != -1) {
  1110. /* Datasheet says 5ms between PWR and RST */
  1111. usleep_range(5000, 25000);
  1112. ret = gpio_direction_output(config->reset_pin, 1);
  1113. }
  1114. usleep_range(5000, 25000);
  1115. vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
  1116. if (!vip) {
  1117. ret = -ENOMEM;
  1118. goto release_gpios;
  1119. }
  1120. vip->pdev = pdev;
  1121. vip->std = V4L2_STD_PAL;
  1122. vip->format = formats_50[0];
  1123. vip->config = config;
  1124. if (v4l2_device_register(&pdev->dev, &vip->v4l2_dev))
  1125. goto free_mem;
  1126. dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
  1127. (unsigned long)pci_resource_start(pdev, 0),
  1128. (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
  1129. pci_set_master(pdev);
  1130. ret = pci_request_regions(pdev, DRV_NAME);
  1131. if (ret)
  1132. goto unreg;
  1133. vip->iomem = pci_iomap(pdev, 0, 0x100);
  1134. if (!vip->iomem) {
  1135. ret = -ENOMEM; /* FIXME */
  1136. goto release;
  1137. }
  1138. pci_enable_msi(pdev);
  1139. INIT_LIST_HEAD(&vip->capture);
  1140. spin_lock_init(&vip->slock);
  1141. mutex_init(&vip->mutex);
  1142. vip->started = 0;
  1143. vip->disabled = 0;
  1144. ret = request_irq(pdev->irq,
  1145. (irq_handler_t) vip_irq,
  1146. IRQF_SHARED, DRV_NAME, vip);
  1147. if (ret) {
  1148. dev_err(&pdev->dev, "request_irq failed\n");
  1149. ret = -ENODEV;
  1150. goto unmap;
  1151. }
  1152. vip->video_dev = video_device_alloc();
  1153. if (!vip->video_dev) {
  1154. ret = -ENOMEM;
  1155. goto release_irq;
  1156. }
  1157. *(vip->video_dev) = video_dev_template;
  1158. video_set_drvdata(vip->video_dev, vip);
  1159. ret = video_register_device(vip->video_dev, VFL_TYPE_GRABBER, -1);
  1160. if (ret)
  1161. goto vrelease;
  1162. vip->adapter = i2c_get_adapter(vip->config->i2c_id);
  1163. if (!vip->adapter) {
  1164. ret = -ENODEV;
  1165. dev_err(&pdev->dev, "no I2C adapter found\n");
  1166. goto vunreg;
  1167. }
  1168. vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
  1169. "adv7180", vip->config->i2c_addr,
  1170. NULL);
  1171. if (!vip->decoder) {
  1172. ret = -ENODEV;
  1173. dev_err(&pdev->dev, "no decoder found\n");
  1174. goto vunreg;
  1175. }
  1176. i2c_put_adapter(vip->adapter);
  1177. v4l2_subdev_call(vip->decoder, core, init, 0);
  1178. pr_info("STA2X11 Video Input Port (VIP) loaded\n");
  1179. return 0;
  1180. vunreg:
  1181. video_set_drvdata(vip->video_dev, NULL);
  1182. vrelease:
  1183. if (video_is_registered(vip->video_dev))
  1184. video_unregister_device(vip->video_dev);
  1185. else
  1186. video_device_release(vip->video_dev);
  1187. release_irq:
  1188. free_irq(pdev->irq, vip);
  1189. pci_disable_msi(pdev);
  1190. unmap:
  1191. pci_iounmap(pdev, vip->iomem);
  1192. mutex_destroy(&vip->mutex);
  1193. release:
  1194. pci_release_regions(pdev);
  1195. unreg:
  1196. v4l2_device_unregister(&vip->v4l2_dev);
  1197. free_mem:
  1198. kfree(vip);
  1199. release_gpios:
  1200. vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
  1201. vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
  1202. disable:
  1203. /*
  1204. * do not call pci_disable_device on sta2x11 because it break all
  1205. * other Bus masters on this EP
  1206. */
  1207. return ret;
  1208. }
  1209. /**
  1210. * sta2x11_vip_remove_one - release device
  1211. * @pdev: PCI device
  1212. *
  1213. * Undo everything done in .._init_one
  1214. *
  1215. * unregister video device
  1216. * free interrupt
  1217. * unmap ioadresses
  1218. * free memory
  1219. * free GPIO pins
  1220. */
  1221. static void sta2x11_vip_remove_one(struct pci_dev *pdev)
  1222. {
  1223. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1224. struct sta2x11_vip *vip =
  1225. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1226. video_set_drvdata(vip->video_dev, NULL);
  1227. video_unregister_device(vip->video_dev);
  1228. /*do not call video_device_release() here, is already done */
  1229. free_irq(pdev->irq, vip);
  1230. pci_disable_msi(pdev);
  1231. pci_iounmap(pdev, vip->iomem);
  1232. pci_release_regions(pdev);
  1233. v4l2_device_unregister(&vip->v4l2_dev);
  1234. mutex_destroy(&vip->mutex);
  1235. vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
  1236. vip->config->pwr_name);
  1237. vip_gpio_release(&pdev->dev, vip->config->reset_pin,
  1238. vip->config->reset_name);
  1239. kfree(vip);
  1240. /*
  1241. * do not call pci_disable_device on sta2x11 because it break all
  1242. * other Bus masters on this EP
  1243. */
  1244. }
  1245. #ifdef CONFIG_PM
  1246. /**
  1247. * sta2x11_vip_suspend - set device into power save mode
  1248. * @pdev: PCI device
  1249. * @state: new state of device
  1250. *
  1251. * all relevant registers are saved and an attempt to set a new state is made.
  1252. *
  1253. * return value: 0 always indicate success,
  1254. * even if device could not be disabled. (workaround for hardware problem)
  1255. *
  1256. * reurn value : 0, always succesful, even if hardware does not not support
  1257. * power down mode.
  1258. */
  1259. static int sta2x11_vip_suspend(struct pci_dev *pdev, pm_message_t state)
  1260. {
  1261. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1262. struct sta2x11_vip *vip =
  1263. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1264. unsigned long flags;
  1265. int i;
  1266. spin_lock_irqsave(&vip->slock, flags);
  1267. vip->register_save_area[0] = REG_READ(vip, DVP_CTL);
  1268. REG_WRITE(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
  1269. vip->register_save_area[SAVE_COUNT] = REG_READ(vip, DVP_ITM);
  1270. REG_WRITE(vip, DVP_ITM, 0);
  1271. for (i = 1; i < SAVE_COUNT; i++)
  1272. vip->register_save_area[i] = REG_READ(vip, 4 * i);
  1273. for (i = 0; i < AUX_COUNT; i++)
  1274. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
  1275. REG_READ(vip, registers_to_save[i]);
  1276. spin_unlock_irqrestore(&vip->slock, flags);
  1277. /* save pci state */
  1278. pci_save_state(pdev);
  1279. if (pci_set_power_state(pdev, pci_choose_state(pdev, state))) {
  1280. /*
  1281. * do not call pci_disable_device on sta2x11 because it
  1282. * break all other Bus masters on this EP
  1283. */
  1284. vip->disabled = 1;
  1285. }
  1286. pr_info("VIP: suspend\n");
  1287. return 0;
  1288. }
  1289. /**
  1290. * sta2x11_vip_resume - resume device operation
  1291. * @pdev : PCI device
  1292. *
  1293. * re-enable device, set PCI state to powered and restore registers.
  1294. * resume normal device operation afterwards.
  1295. *
  1296. * return value: 0, no error.
  1297. *
  1298. * other, could not set device to power on state.
  1299. */
  1300. static int sta2x11_vip_resume(struct pci_dev *pdev)
  1301. {
  1302. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  1303. struct sta2x11_vip *vip =
  1304. container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
  1305. unsigned long flags;
  1306. int ret, i;
  1307. pr_info("VIP: resume\n");
  1308. /* restore pci state */
  1309. if (vip->disabled) {
  1310. ret = pci_enable_device(pdev);
  1311. if (ret) {
  1312. pr_warning("VIP: Can't enable device.\n");
  1313. return ret;
  1314. }
  1315. vip->disabled = 0;
  1316. }
  1317. ret = pci_set_power_state(pdev, PCI_D0);
  1318. if (ret) {
  1319. /*
  1320. * do not call pci_disable_device on sta2x11 because it
  1321. * break all other Bus masters on this EP
  1322. */
  1323. pr_warning("VIP: Can't enable device.\n");
  1324. vip->disabled = 1;
  1325. return ret;
  1326. }
  1327. pci_restore_state(pdev);
  1328. spin_lock_irqsave(&vip->slock, flags);
  1329. for (i = 1; i < SAVE_COUNT; i++)
  1330. REG_WRITE(vip, 4 * i, vip->register_save_area[i]);
  1331. for (i = 0; i < AUX_COUNT; i++)
  1332. REG_WRITE(vip, registers_to_save[i],
  1333. vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
  1334. REG_WRITE(vip, DVP_CTL, vip->register_save_area[0]);
  1335. REG_WRITE(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
  1336. spin_unlock_irqrestore(&vip->slock, flags);
  1337. return 0;
  1338. }
  1339. #endif
  1340. static DEFINE_PCI_DEVICE_TABLE(sta2x11_vip_pci_tbl) = {
  1341. {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
  1342. {0,}
  1343. };
  1344. static struct pci_driver sta2x11_vip_driver = {
  1345. .name = DRV_NAME,
  1346. .probe = sta2x11_vip_init_one,
  1347. .remove = sta2x11_vip_remove_one,
  1348. .id_table = sta2x11_vip_pci_tbl,
  1349. #ifdef CONFIG_PM
  1350. .suspend = sta2x11_vip_suspend,
  1351. .resume = sta2x11_vip_resume,
  1352. #endif
  1353. };
  1354. static int __init sta2x11_vip_init_module(void)
  1355. {
  1356. return pci_register_driver(&sta2x11_vip_driver);
  1357. }
  1358. static void __exit sta2x11_vip_exit_module(void)
  1359. {
  1360. pci_unregister_driver(&sta2x11_vip_driver);
  1361. }
  1362. #ifdef MODULE
  1363. module_init(sta2x11_vip_init_module);
  1364. module_exit(sta2x11_vip_exit_module);
  1365. #else
  1366. late_initcall_sync(sta2x11_vip_init_module);
  1367. #endif
  1368. MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
  1369. MODULE_AUTHOR("Wind River");
  1370. MODULE_LICENSE("GPL v2");
  1371. MODULE_SUPPORTED_DEVICE("sta2x11 video input");
  1372. MODULE_VERSION(DRV_VERSION);
  1373. MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);