arv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Colour AR M64278(VGA) driver for Video4Linux
  3. *
  4. * Copyright (C) 2003 Takeo Takahashi <takahashi.takeo@renesas.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Some code is taken from AR driver sample program for M3T-M32700UT.
  12. *
  13. * AR driver sample (M32R SDK):
  14. * Copyright (c) 2003 RENESAS TECHNOROGY CORPORATION
  15. * AND RENESAS SOLUTIONS CORPORATION
  16. * All Rights Reserved.
  17. *
  18. * 2003-09-01: Support w3cam by Takeo Takahashi
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/sched.h>
  30. #include <linux/videodev.h>
  31. #include <media/v4l2-common.h>
  32. #include <linux/mutex.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/m32r.h>
  35. #include <asm/io.h>
  36. #include <asm/dma.h>
  37. #include <asm/byteorder.h>
  38. #if 0
  39. #define DEBUG(n, args...) printk(args)
  40. #define CHECK_LOST 1
  41. #else
  42. #define DEBUG(n, args...)
  43. #define CHECK_LOST 0
  44. #endif
  45. /*
  46. * USE_INT is always 0, interrupt mode is not available
  47. * on linux due to lack of speed
  48. */
  49. #define USE_INT 0 /* Don't modify */
  50. #define VERSION "0.03"
  51. #define ar_inl(addr) inl((unsigned long)(addr))
  52. #define ar_outl(val, addr) outl((unsigned long)(val),(unsigned long)(addr))
  53. extern struct cpuinfo_m32r boot_cpu_data;
  54. /*
  55. * CCD pixel size
  56. * Note that M32700UT does not support CIF mode, but QVGA is
  57. * supported by M32700UT hardware using VGA mode of AR LSI.
  58. *
  59. * Supported: VGA (Normal mode, Interlace mode)
  60. * QVGA (Always Interlace mode of VGA)
  61. *
  62. */
  63. #define AR_WIDTH_VGA 640
  64. #define AR_HEIGHT_VGA 480
  65. #define AR_WIDTH_QVGA 320
  66. #define AR_HEIGHT_QVGA 240
  67. #define MIN_AR_WIDTH AR_WIDTH_QVGA
  68. #define MIN_AR_HEIGHT AR_HEIGHT_QVGA
  69. #define MAX_AR_WIDTH AR_WIDTH_VGA
  70. #define MAX_AR_HEIGHT AR_HEIGHT_VGA
  71. /* bits & bytes per pixel */
  72. #define AR_BITS_PER_PIXEL 16
  73. #define AR_BYTES_PER_PIXEL (AR_BITS_PER_PIXEL/8)
  74. /* line buffer size */
  75. #define AR_LINE_BYTES_VGA (AR_WIDTH_VGA * AR_BYTES_PER_PIXEL)
  76. #define AR_LINE_BYTES_QVGA (AR_WIDTH_QVGA * AR_BYTES_PER_PIXEL)
  77. #define MAX_AR_LINE_BYTES AR_LINE_BYTES_VGA
  78. /* frame size & type */
  79. #define AR_FRAME_BYTES_VGA \
  80. (AR_WIDTH_VGA * AR_HEIGHT_VGA * AR_BYTES_PER_PIXEL)
  81. #define AR_FRAME_BYTES_QVGA \
  82. (AR_WIDTH_QVGA * AR_HEIGHT_QVGA * AR_BYTES_PER_PIXEL)
  83. #define MAX_AR_FRAME_BYTES \
  84. (MAX_AR_WIDTH * MAX_AR_HEIGHT * AR_BYTES_PER_PIXEL)
  85. #define AR_MAX_FRAME 15
  86. /* capture size */
  87. #define AR_SIZE_VGA 0
  88. #define AR_SIZE_QVGA 1
  89. /* capture mode */
  90. #define AR_MODE_INTERLACE 0
  91. #define AR_MODE_NORMAL 1
  92. struct ar_device {
  93. struct video_device *vdev;
  94. unsigned int start_capture; /* duaring capture in INT. mode. */
  95. #if USE_INT
  96. unsigned char *line_buff; /* DMA line buffer */
  97. #endif
  98. unsigned char *frame[MAX_AR_HEIGHT]; /* frame data */
  99. short size; /* capture size */
  100. short mode; /* capture mode */
  101. int width, height;
  102. int frame_bytes, line_bytes;
  103. wait_queue_head_t wait;
  104. struct mutex lock;
  105. };
  106. static int video_nr = -1; /* video device number (first free) */
  107. static unsigned char yuv[MAX_AR_FRAME_BYTES];
  108. /* module parameters */
  109. /* default frequency */
  110. #define DEFAULT_FREQ 50 /* 50 or 75 (MHz) is available as BCLK */
  111. static int freq = DEFAULT_FREQ; /* BCLK: available 50 or 70 (MHz) */
  112. static int vga = 0; /* default mode(0:QVGA mode, other:VGA mode) */
  113. static int vga_interlace = 0; /* 0 is normal mode for, else interlace mode */
  114. module_param(freq, int, 0);
  115. module_param(vga, int, 0);
  116. module_param(vga_interlace, int, 0);
  117. static int ar_initialize(struct video_device *dev);
  118. static inline void wait_for_vsync(void)
  119. {
  120. while (ar_inl(ARVCR0) & ARVCR0_VDS) /* wait for VSYNC */
  121. cpu_relax();
  122. while (!(ar_inl(ARVCR0) & ARVCR0_VDS)) /* wait for VSYNC */
  123. cpu_relax();
  124. }
  125. static inline void wait_acknowledge(void)
  126. {
  127. int i;
  128. for (i = 0; i < 1000; i++)
  129. cpu_relax();
  130. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_NOACK)
  131. cpu_relax();
  132. }
  133. /*******************************************************************
  134. * I2C functions
  135. *******************************************************************/
  136. void iic(int n, unsigned long addr, unsigned long data1, unsigned long data2,
  137. unsigned long data3)
  138. {
  139. int i;
  140. /* Slave Address */
  141. ar_outl(addr, PLDI2CDATA);
  142. wait_for_vsync();
  143. /* Start */
  144. ar_outl(1, PLDI2CCND);
  145. wait_acknowledge();
  146. /* Transfer data 1 */
  147. ar_outl(data1, PLDI2CDATA);
  148. wait_for_vsync();
  149. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  150. wait_acknowledge();
  151. /* Transfer data 2 */
  152. ar_outl(data2, PLDI2CDATA);
  153. wait_for_vsync();
  154. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  155. wait_acknowledge();
  156. if (n == 3) {
  157. /* Transfer data 3 */
  158. ar_outl(data3, PLDI2CDATA);
  159. wait_for_vsync();
  160. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  161. wait_acknowledge();
  162. }
  163. /* Stop */
  164. for (i = 0; i < 100; i++)
  165. cpu_relax();
  166. ar_outl(2, PLDI2CCND);
  167. ar_outl(2, PLDI2CCND);
  168. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_BB)
  169. cpu_relax();
  170. }
  171. void init_iic(void)
  172. {
  173. DEBUG(1, "init_iic:\n");
  174. /*
  175. * ICU Setting (iic)
  176. */
  177. /* I2C Setting */
  178. ar_outl(0x0, PLDI2CCR); /* I2CCR Disable */
  179. ar_outl(0x0300, PLDI2CMOD); /* I2CMOD ACK/8b-data/7b-addr/auto */
  180. ar_outl(0x1, PLDI2CACK); /* I2CACK ACK */
  181. /* I2C CLK */
  182. /* 50MH-100k */
  183. if (freq == 75) {
  184. ar_outl(369, PLDI2CFREQ); /* BCLK = 75MHz */
  185. } else if (freq == 50) {
  186. ar_outl(244, PLDI2CFREQ); /* BCLK = 50MHz */
  187. } else {
  188. ar_outl(244, PLDI2CFREQ); /* default: BCLK = 50MHz */
  189. }
  190. ar_outl(0x1, PLDI2CCR); /* I2CCR Enable */
  191. }
  192. /**************************************************************************
  193. *
  194. * Video4Linux Interface functions
  195. *
  196. **************************************************************************/
  197. static inline void disable_dma(void)
  198. {
  199. ar_outl(0x8000, M32R_DMAEN_PORTL); /* disable DMA0 */
  200. }
  201. static inline void enable_dma(void)
  202. {
  203. ar_outl(0x8080, M32R_DMAEN_PORTL); /* enable DMA0 */
  204. }
  205. static inline void clear_dma_status(void)
  206. {
  207. ar_outl(0x8000, M32R_DMAEDET_PORTL); /* clear status */
  208. }
  209. static inline void wait_for_vertical_sync(int exp_line)
  210. {
  211. #if CHECK_LOST
  212. int tmout = 10000; /* FIXME */
  213. int l;
  214. /*
  215. * check HCOUNT because we cannot check vertical sync.
  216. */
  217. for (; tmout >= 0; tmout--) {
  218. l = ar_inl(ARVHCOUNT);
  219. if (l == exp_line)
  220. break;
  221. }
  222. if (tmout < 0)
  223. printk("arv: lost %d -> %d\n", exp_line, l);
  224. #else
  225. while (ar_inl(ARVHCOUNT) != exp_line)
  226. cpu_relax();
  227. #endif
  228. }
  229. static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
  230. {
  231. struct video_device *v = video_devdata(file);
  232. struct ar_device *ar = v->priv;
  233. long ret = ar->frame_bytes; /* return read bytes */
  234. unsigned long arvcr1 = 0;
  235. unsigned long flags;
  236. unsigned char *p;
  237. int h, w;
  238. unsigned char *py, *pu, *pv;
  239. #if ! USE_INT
  240. int l;
  241. #endif
  242. DEBUG(1, "ar_read()\n");
  243. if (ar->size == AR_SIZE_QVGA)
  244. arvcr1 |= ARVCR1_QVGA;
  245. if (ar->mode == AR_MODE_NORMAL)
  246. arvcr1 |= ARVCR1_NORMAL;
  247. mutex_lock(&ar->lock);
  248. #if USE_INT
  249. local_irq_save(flags);
  250. disable_dma();
  251. ar_outl(0xa1871300, M32R_DMA0CR0_PORTL);
  252. ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
  253. /* set AR FIFO address as source(BSEL5) */
  254. ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
  255. ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
  256. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* destination addr. */
  257. ar_outl(ar->line_buff, M32R_DMA0RDA_PORTL); /* reload address */
  258. ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL); /* byte count (bytes) */
  259. ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL); /* reload count (bytes) */
  260. /*
  261. * Okey , kicks AR LSI to invoke an interrupt
  262. */
  263. ar->start_capture = 0;
  264. ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
  265. local_irq_restore(flags);
  266. /* .... AR interrupts .... */
  267. interruptible_sleep_on(&ar->wait);
  268. if (signal_pending(current)) {
  269. printk("arv: interrupted while get frame data.\n");
  270. ret = -EINTR;
  271. goto out_up;
  272. }
  273. #else /* ! USE_INT */
  274. /* polling */
  275. ar_outl(arvcr1, ARVCR1);
  276. disable_dma();
  277. ar_outl(0x8000, M32R_DMAEDET_PORTL);
  278. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  279. ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
  280. ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
  281. ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
  282. ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL);
  283. ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL);
  284. local_irq_save(flags);
  285. while (ar_inl(ARVHCOUNT) != 0) /* wait for 0 */
  286. cpu_relax();
  287. if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
  288. for (h = 0; h < ar->height; h++) {
  289. wait_for_vertical_sync(h);
  290. if (h < (AR_HEIGHT_VGA/2))
  291. l = h << 1;
  292. else
  293. l = (((h - (AR_HEIGHT_VGA/2)) << 1) + 1);
  294. ar_outl(virt_to_phys(ar->frame[l]), M32R_DMA0CDA_PORTL);
  295. enable_dma();
  296. while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
  297. cpu_relax();
  298. disable_dma();
  299. clear_dma_status();
  300. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  301. }
  302. } else {
  303. for (h = 0; h < ar->height; h++) {
  304. wait_for_vertical_sync(h);
  305. ar_outl(virt_to_phys(ar->frame[h]), M32R_DMA0CDA_PORTL);
  306. enable_dma();
  307. while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
  308. cpu_relax();
  309. disable_dma();
  310. clear_dma_status();
  311. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  312. }
  313. }
  314. local_irq_restore(flags);
  315. #endif /* ! USE_INT */
  316. /*
  317. * convert YUV422 to YUV422P
  318. * +--------------------+
  319. * | Y0,Y1,... |
  320. * | ..............Yn |
  321. * +--------------------+
  322. * | U0,U1,........Un |
  323. * +--------------------+
  324. * | V0,V1,........Vn |
  325. * +--------------------+
  326. */
  327. py = yuv;
  328. pu = py + (ar->frame_bytes / 2);
  329. pv = pu + (ar->frame_bytes / 4);
  330. for (h = 0; h < ar->height; h++) {
  331. p = ar->frame[h];
  332. for (w = 0; w < ar->line_bytes; w += 4) {
  333. *py++ = *p++;
  334. *pu++ = *p++;
  335. *py++ = *p++;
  336. *pv++ = *p++;
  337. }
  338. }
  339. if (copy_to_user(buf, yuv, ar->frame_bytes)) {
  340. printk("arv: failed while copy_to_user yuv.\n");
  341. ret = -EFAULT;
  342. goto out_up;
  343. }
  344. DEBUG(1, "ret = %d\n", ret);
  345. out_up:
  346. mutex_unlock(&ar->lock);
  347. return ret;
  348. }
  349. static int ar_do_ioctl(struct inode *inode, struct file *file,
  350. unsigned int cmd, void *arg)
  351. {
  352. struct video_device *dev = video_devdata(file);
  353. struct ar_device *ar = dev->priv;
  354. DEBUG(1, "ar_ioctl()\n");
  355. switch(cmd) {
  356. case VIDIOCGCAP:
  357. {
  358. struct video_capability *b = arg;
  359. DEBUG(1, "VIDIOCGCAP:\n");
  360. strcpy(b->name, ar->vdev->name);
  361. b->type = VID_TYPE_CAPTURE;
  362. b->channels = 0;
  363. b->audios = 0;
  364. b->maxwidth = MAX_AR_WIDTH;
  365. b->maxheight = MAX_AR_HEIGHT;
  366. b->minwidth = MIN_AR_WIDTH;
  367. b->minheight = MIN_AR_HEIGHT;
  368. return 0;
  369. }
  370. case VIDIOCGCHAN:
  371. DEBUG(1, "VIDIOCGCHAN:\n");
  372. return 0;
  373. case VIDIOCSCHAN:
  374. DEBUG(1, "VIDIOCSCHAN:\n");
  375. return 0;
  376. case VIDIOCGTUNER:
  377. DEBUG(1, "VIDIOCGTUNER:\n");
  378. return 0;
  379. case VIDIOCSTUNER:
  380. DEBUG(1, "VIDIOCSTUNER:\n");
  381. return 0;
  382. case VIDIOCGPICT:
  383. DEBUG(1, "VIDIOCGPICT:\n");
  384. return 0;
  385. case VIDIOCSPICT:
  386. DEBUG(1, "VIDIOCSPICT:\n");
  387. return 0;
  388. case VIDIOCCAPTURE:
  389. DEBUG(1, "VIDIOCCAPTURE:\n");
  390. return -EINVAL;
  391. case VIDIOCGWIN:
  392. {
  393. struct video_window *w = arg;
  394. DEBUG(1, "VIDIOCGWIN:\n");
  395. memset(w, 0, sizeof(w));
  396. w->width = ar->width;
  397. w->height = ar->height;
  398. return 0;
  399. }
  400. case VIDIOCSWIN:
  401. {
  402. struct video_window *w = arg;
  403. DEBUG(1, "VIDIOCSWIN:\n");
  404. if ((w->width != AR_WIDTH_VGA || w->height != AR_HEIGHT_VGA) &&
  405. (w->width != AR_WIDTH_QVGA || w->height != AR_HEIGHT_QVGA))
  406. return -EINVAL;
  407. mutex_lock(&ar->lock);
  408. ar->width = w->width;
  409. ar->height = w->height;
  410. if (ar->width == AR_WIDTH_VGA) {
  411. ar->size = AR_SIZE_VGA;
  412. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  413. ar->line_bytes = AR_LINE_BYTES_VGA;
  414. if (vga_interlace)
  415. ar->mode = AR_MODE_INTERLACE;
  416. else
  417. ar->mode = AR_MODE_NORMAL;
  418. } else {
  419. ar->size = AR_SIZE_QVGA;
  420. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  421. ar->line_bytes = AR_LINE_BYTES_QVGA;
  422. ar->mode = AR_MODE_INTERLACE;
  423. }
  424. mutex_unlock(&ar->lock);
  425. return 0;
  426. }
  427. case VIDIOCGFBUF:
  428. DEBUG(1, "VIDIOCGFBUF:\n");
  429. return -EINVAL;
  430. case VIDIOCSFBUF:
  431. DEBUG(1, "VIDIOCSFBUF:\n");
  432. return -EINVAL;
  433. case VIDIOCKEY:
  434. DEBUG(1, "VIDIOCKEY:\n");
  435. return 0;
  436. case VIDIOCGFREQ:
  437. DEBUG(1, "VIDIOCGFREQ:\n");
  438. return -EINVAL;
  439. case VIDIOCSFREQ:
  440. DEBUG(1, "VIDIOCSFREQ:\n");
  441. return -EINVAL;
  442. case VIDIOCGAUDIO:
  443. DEBUG(1, "VIDIOCGAUDIO:\n");
  444. return -EINVAL;
  445. case VIDIOCSAUDIO:
  446. DEBUG(1, "VIDIOCSAUDIO:\n");
  447. return -EINVAL;
  448. case VIDIOCSYNC:
  449. DEBUG(1, "VIDIOCSYNC:\n");
  450. return -EINVAL;
  451. case VIDIOCMCAPTURE:
  452. DEBUG(1, "VIDIOCMCAPTURE:\n");
  453. return -EINVAL;
  454. case VIDIOCGMBUF:
  455. DEBUG(1, "VIDIOCGMBUF:\n");
  456. return -EINVAL;
  457. case VIDIOCGUNIT:
  458. DEBUG(1, "VIDIOCGUNIT:\n");
  459. return -EINVAL;
  460. case VIDIOCGCAPTURE:
  461. DEBUG(1, "VIDIOCGCAPTURE:\n");
  462. return -EINVAL;
  463. case VIDIOCSCAPTURE:
  464. DEBUG(1, "VIDIOCSCAPTURE:\n");
  465. return -EINVAL;
  466. case VIDIOCSPLAYMODE:
  467. DEBUG(1, "VIDIOCSPLAYMODE:\n");
  468. return -EINVAL;
  469. case VIDIOCSWRITEMODE:
  470. DEBUG(1, "VIDIOCSWRITEMODE:\n");
  471. return -EINVAL;
  472. case VIDIOCGPLAYINFO:
  473. DEBUG(1, "VIDIOCGPLAYINFO:\n");
  474. return -EINVAL;
  475. case VIDIOCSMICROCODE:
  476. DEBUG(1, "VIDIOCSMICROCODE:\n");
  477. return -EINVAL;
  478. case VIDIOCGVBIFMT:
  479. DEBUG(1, "VIDIOCGVBIFMT:\n");
  480. return -EINVAL;
  481. case VIDIOCSVBIFMT:
  482. DEBUG(1, "VIDIOCSVBIFMT:\n");
  483. return -EINVAL;
  484. default:
  485. DEBUG(1, "Unknown ioctl(0x%08x)\n", cmd);
  486. return -ENOIOCTLCMD;
  487. }
  488. return 0;
  489. }
  490. static int ar_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  491. unsigned long arg)
  492. {
  493. return video_usercopy(inode, file, cmd, arg, ar_do_ioctl);
  494. }
  495. #if USE_INT
  496. /*
  497. * Interrupt handler
  498. */
  499. static void ar_interrupt(int irq, void *dev)
  500. {
  501. struct ar_device *ar = dev;
  502. unsigned int line_count;
  503. unsigned int line_number;
  504. unsigned int arvcr1;
  505. line_count = ar_inl(ARVHCOUNT); /* line number */
  506. if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
  507. /* operations for interlace mode */
  508. if ( line_count < (AR_HEIGHT_VGA/2) ) /* even line */
  509. line_number = (line_count << 1);
  510. else /* odd line */
  511. line_number =
  512. (((line_count - (AR_HEIGHT_VGA/2)) << 1) + 1);
  513. } else {
  514. line_number = line_count;
  515. }
  516. if (line_number == 0) {
  517. /*
  518. * It is an interrupt for line 0.
  519. * we have to start capture.
  520. */
  521. disable_dma();
  522. #if 0
  523. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* needless? */
  524. #endif
  525. memcpy(ar->frame[0], ar->line_buff, ar->line_bytes);
  526. #if 0
  527. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  528. #endif
  529. enable_dma();
  530. ar->start_capture = 1; /* during capture */
  531. return;
  532. }
  533. if (ar->start_capture == 1 && line_number <= (ar->height - 1)) {
  534. disable_dma();
  535. memcpy(ar->frame[line_number], ar->line_buff, ar->line_bytes);
  536. /*
  537. * if captured all line of a frame, disable AR interrupt
  538. * and wake a process up.
  539. */
  540. if (line_number == (ar->height - 1)) { /* end of line */
  541. ar->start_capture = 0;
  542. /* disable AR interrupt request */
  543. arvcr1 = ar_inl(ARVCR1);
  544. arvcr1 &= ~ARVCR1_HIEN; /* clear int. flag */
  545. ar_outl(arvcr1, ARVCR1); /* disable */
  546. wake_up_interruptible(&ar->wait);
  547. } else {
  548. #if 0
  549. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL);
  550. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  551. #endif
  552. enable_dma();
  553. }
  554. }
  555. }
  556. #endif
  557. /*
  558. * ar_initialize()
  559. * ar_initialize() is called by video_register_device() and
  560. * initializes AR LSI and peripherals.
  561. *
  562. * -1 is returned in all failures.
  563. * 0 is returned in success.
  564. *
  565. */
  566. static int ar_initialize(struct video_device *dev)
  567. {
  568. struct ar_device *ar = dev->priv;
  569. unsigned long cr = 0;
  570. int i,found=0;
  571. DEBUG(1, "ar_initialize:\n");
  572. /*
  573. * initialize AR LSI
  574. */
  575. ar_outl(0, ARVCR0); /* assert reset of AR LSI */
  576. for (i = 0; i < 0x18; i++) /* wait for over 10 cycles @ 27MHz */
  577. cpu_relax();
  578. ar_outl(ARVCR0_RST, ARVCR0); /* negate reset of AR LSI (enable) */
  579. for (i = 0; i < 0x40d; i++) /* wait for over 420 cycles @ 27MHz */
  580. cpu_relax();
  581. /* AR uses INT3 of CPU as interrupt pin. */
  582. ar_outl(ARINTSEL_INT3, ARINTSEL);
  583. if (ar->size == AR_SIZE_QVGA)
  584. cr |= ARVCR1_QVGA;
  585. if (ar->mode == AR_MODE_NORMAL)
  586. cr |= ARVCR1_NORMAL;
  587. ar_outl(cr, ARVCR1);
  588. /*
  589. * Initialize IIC so that CPU can communicate with AR LSI,
  590. * and send boot commands to AR LSI.
  591. */
  592. init_iic();
  593. for (i = 0; i < 0x100000; i++) { /* > 0xa1d10, 56ms */
  594. if ((ar_inl(ARVCR0) & ARVCR0_VDS)) { /* VSYNC */
  595. found = 1;
  596. break;
  597. }
  598. }
  599. if (found == 0)
  600. return -ENODEV;
  601. printk("arv: Initializing ");
  602. iic(2,0x78,0x11,0x01,0x00); /* start */
  603. iic(3,0x78,0x12,0x00,0x06);
  604. iic(3,0x78,0x12,0x12,0x30);
  605. iic(3,0x78,0x12,0x15,0x58);
  606. iic(3,0x78,0x12,0x17,0x30);
  607. printk(".");
  608. iic(3,0x78,0x12,0x1a,0x97);
  609. iic(3,0x78,0x12,0x1b,0xff);
  610. iic(3,0x78,0x12,0x1c,0xff);
  611. iic(3,0x78,0x12,0x26,0x10);
  612. iic(3,0x78,0x12,0x27,0x00);
  613. printk(".");
  614. iic(2,0x78,0x34,0x02,0x00);
  615. iic(2,0x78,0x7a,0x10,0x00);
  616. iic(2,0x78,0x80,0x39,0x00);
  617. iic(2,0x78,0x81,0xe6,0x00);
  618. iic(2,0x78,0x8d,0x00,0x00);
  619. printk(".");
  620. iic(2,0x78,0x8e,0x0c,0x00);
  621. iic(2,0x78,0x8f,0x00,0x00);
  622. #if 0
  623. iic(2,0x78,0x90,0x00,0x00); /* AWB on=1 off=0 */
  624. #endif
  625. iic(2,0x78,0x93,0x01,0x00);
  626. iic(2,0x78,0x94,0xcd,0x00);
  627. iic(2,0x78,0x95,0x00,0x00);
  628. printk(".");
  629. iic(2,0x78,0x96,0xa0,0x00);
  630. iic(2,0x78,0x97,0x00,0x00);
  631. iic(2,0x78,0x98,0x60,0x00);
  632. iic(2,0x78,0x99,0x01,0x00);
  633. iic(2,0x78,0x9a,0x19,0x00);
  634. printk(".");
  635. iic(2,0x78,0x9b,0x02,0x00);
  636. iic(2,0x78,0x9c,0xe8,0x00);
  637. iic(2,0x78,0x9d,0x02,0x00);
  638. iic(2,0x78,0x9e,0x2e,0x00);
  639. iic(2,0x78,0xb8,0x78,0x00);
  640. iic(2,0x78,0xba,0x05,0x00);
  641. #if 0
  642. iic(2,0x78,0x83,0x8c,0x00); /* brightness */
  643. #endif
  644. printk(".");
  645. /* color correction */
  646. iic(3,0x78,0x49,0x00,0x95); /* a */
  647. iic(3,0x78,0x49,0x01,0x96); /* b */
  648. iic(3,0x78,0x49,0x03,0x85); /* c */
  649. iic(3,0x78,0x49,0x04,0x97); /* d */
  650. iic(3,0x78,0x49,0x02,0x7e); /* e(Lo) */
  651. iic(3,0x78,0x49,0x05,0xa4); /* f(Lo) */
  652. iic(3,0x78,0x49,0x06,0x04); /* e(Hi) */
  653. iic(3,0x78,0x49,0x07,0x04); /* e(Hi) */
  654. iic(2,0x78,0x48,0x01,0x00); /* on=1 off=0 */
  655. printk(".");
  656. iic(2,0x78,0x11,0x00,0x00); /* end */
  657. printk(" done\n");
  658. return 0;
  659. }
  660. void ar_release(struct video_device *vfd)
  661. {
  662. struct ar_device *ar = vfd->priv;
  663. mutex_lock(&ar->lock);
  664. video_device_release(vfd);
  665. }
  666. /****************************************************************************
  667. *
  668. * Video4Linux Module functions
  669. *
  670. ****************************************************************************/
  671. static const struct file_operations ar_fops = {
  672. .owner = THIS_MODULE,
  673. .open = video_exclusive_open,
  674. .release = video_exclusive_release,
  675. .read = ar_read,
  676. .ioctl = ar_ioctl,
  677. .compat_ioctl = v4l_compat_ioctl32,
  678. .llseek = no_llseek,
  679. };
  680. static struct video_device ar_template = {
  681. .owner = THIS_MODULE,
  682. .name = "Colour AR VGA",
  683. .type = VID_TYPE_CAPTURE,
  684. .hardware = VID_HARDWARE_ARV,
  685. .fops = &ar_fops,
  686. .release = ar_release,
  687. .minor = -1,
  688. };
  689. #define ALIGN4(x) ((((int)(x)) & 0x3) == 0)
  690. static struct ar_device ardev;
  691. static int __init ar_init(void)
  692. {
  693. struct ar_device *ar;
  694. int ret;
  695. int i;
  696. DEBUG(1, "ar_init:\n");
  697. ret = -EIO;
  698. printk(KERN_INFO "arv: Colour AR VGA driver %s\n", VERSION);
  699. ar = &ardev;
  700. memset(ar, 0, sizeof(struct ar_device));
  701. #if USE_INT
  702. /* allocate a DMA buffer for 1 line. */
  703. ar->line_buff = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL | GFP_DMA);
  704. if (ar->line_buff == NULL || ! ALIGN4(ar->line_buff)) {
  705. printk("arv: buffer allocation failed for DMA.\n");
  706. ret = -ENOMEM;
  707. goto out_end;
  708. }
  709. #endif
  710. /* allocate buffers for a frame */
  711. for (i = 0; i < MAX_AR_HEIGHT; i++) {
  712. ar->frame[i] = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL);
  713. if (ar->frame[i] == NULL || ! ALIGN4(ar->frame[i])) {
  714. printk("arv: buffer allocation failed for frame.\n");
  715. ret = -ENOMEM;
  716. goto out_line_buff;
  717. }
  718. }
  719. ar->vdev = video_device_alloc();
  720. if (!ar->vdev) {
  721. printk(KERN_ERR "arv: video_device_alloc() failed\n");
  722. return -ENOMEM;
  723. }
  724. memcpy(ar->vdev, &ar_template, sizeof(ar_template));
  725. ar->vdev->priv = ar;
  726. if (vga) {
  727. ar->width = AR_WIDTH_VGA;
  728. ar->height = AR_HEIGHT_VGA;
  729. ar->size = AR_SIZE_VGA;
  730. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  731. ar->line_bytes = AR_LINE_BYTES_VGA;
  732. if (vga_interlace)
  733. ar->mode = AR_MODE_INTERLACE;
  734. else
  735. ar->mode = AR_MODE_NORMAL;
  736. } else {
  737. ar->width = AR_WIDTH_QVGA;
  738. ar->height = AR_HEIGHT_QVGA;
  739. ar->size = AR_SIZE_QVGA;
  740. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  741. ar->line_bytes = AR_LINE_BYTES_QVGA;
  742. ar->mode = AR_MODE_INTERLACE;
  743. }
  744. mutex_init(&ar->lock);
  745. init_waitqueue_head(&ar->wait);
  746. #if USE_INT
  747. if (request_irq(M32R_IRQ_INT3, ar_interrupt, 0, "arv", ar)) {
  748. printk("arv: request_irq(%d) failed.\n", M32R_IRQ_INT3);
  749. ret = -EIO;
  750. goto out_irq;
  751. }
  752. #endif
  753. if (ar_initialize(ar->vdev) != 0) {
  754. printk("arv: M64278 not found.\n");
  755. ret = -ENODEV;
  756. goto out_dev;
  757. }
  758. /*
  759. * ok, we can initialize h/w according to parameters,
  760. * so register video device as a frame grabber type.
  761. * device is named "video[0-64]".
  762. * video_register_device() initializes h/w using ar_initialize().
  763. */
  764. if (video_register_device(ar->vdev, VFL_TYPE_GRABBER, video_nr) != 0) {
  765. /* return -1, -ENFILE(full) or others */
  766. printk("arv: register video (Colour AR) failed.\n");
  767. ret = -ENODEV;
  768. goto out_dev;
  769. }
  770. printk("video%d: Found M64278 VGA (IRQ %d, Freq %dMHz).\n",
  771. ar->vdev->minor, M32R_IRQ_INT3, freq);
  772. return 0;
  773. out_dev:
  774. #if USE_INT
  775. free_irq(M32R_IRQ_INT3, ar);
  776. out_irq:
  777. #endif
  778. for (i = 0; i < MAX_AR_HEIGHT; i++)
  779. kfree(ar->frame[i]);
  780. out_line_buff:
  781. #if USE_INT
  782. kfree(ar->line_buff);
  783. out_end:
  784. #endif
  785. return ret;
  786. }
  787. static int __init ar_init_module(void)
  788. {
  789. freq = (boot_cpu_data.bus_clock / 1000000);
  790. printk("arv: Bus clock %d\n", freq);
  791. if (freq != 50 && freq != 75)
  792. freq = DEFAULT_FREQ;
  793. return ar_init();
  794. }
  795. static void __exit ar_cleanup_module(void)
  796. {
  797. struct ar_device *ar;
  798. int i;
  799. ar = &ardev;
  800. video_unregister_device(ar->vdev);
  801. #if USE_INT
  802. free_irq(M32R_IRQ_INT3, ar);
  803. #endif
  804. for (i = 0; i < MAX_AR_HEIGHT; i++)
  805. kfree(ar->frame[i]);
  806. #if USE_INT
  807. kfree(ar->line_buff);
  808. #endif
  809. }
  810. module_init(ar_init_module);
  811. module_exit(ar_cleanup_module);
  812. MODULE_AUTHOR("Takeo Takahashi <takahashi.takeo@renesas.com>");
  813. MODULE_DESCRIPTION("Colour AR M64278(VGA) for Video4Linux");
  814. MODULE_LICENSE("GPL");