arv.c 22 KB

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