arv.c 22 KB

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