arv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 <media/v4l2-ioctl.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. unsigned long in_use;
  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; /* default mode(0:QVGA mode, other:VGA mode) */
  114. static int vga_interlace; /* 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 = video_get_drvdata(v);
  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 long ar_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  351. {
  352. struct video_device *dev = video_devdata(file);
  353. struct ar_device *ar = video_get_drvdata(dev);
  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 long ar_ioctl(struct file *file, unsigned int cmd,
  491. unsigned long arg)
  492. {
  493. return video_usercopy(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 = video_get_drvdata(dev);
  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 = video_get_drvdata(vfd);
  663. mutex_lock(&ar->lock);
  664. video_device_release(vfd);
  665. }
  666. /****************************************************************************
  667. *
  668. * Video4Linux Module functions
  669. *
  670. ****************************************************************************/
  671. static struct ar_device ardev;
  672. static int ar_exclusive_open(struct file *file)
  673. {
  674. return test_and_set_bit(0, &ardev.in_use) ? -EBUSY : 0;
  675. }
  676. static int ar_exclusive_release(struct file *file)
  677. {
  678. clear_bit(0, &ardev.in_use);
  679. return 0;
  680. }
  681. static const struct v4l2_file_operations ar_fops = {
  682. .owner = THIS_MODULE,
  683. .open = ar_exclusive_open,
  684. .release = ar_exclusive_release,
  685. .read = ar_read,
  686. .ioctl = ar_ioctl,
  687. };
  688. static struct video_device ar_template = {
  689. .name = "Colour AR VGA",
  690. .fops = &ar_fops,
  691. .release = ar_release,
  692. .minor = -1,
  693. };
  694. #define ALIGN4(x) ((((int)(x)) & 0x3) == 0)
  695. static int __init ar_init(void)
  696. {
  697. struct ar_device *ar;
  698. int ret;
  699. int i;
  700. DEBUG(1, "ar_init:\n");
  701. ret = -EIO;
  702. printk(KERN_INFO "arv: Colour AR VGA driver %s\n", VERSION);
  703. ar = &ardev;
  704. memset(ar, 0, sizeof(struct ar_device));
  705. #if USE_INT
  706. /* allocate a DMA buffer for 1 line. */
  707. ar->line_buff = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL | GFP_DMA);
  708. if (ar->line_buff == NULL || ! ALIGN4(ar->line_buff)) {
  709. printk("arv: buffer allocation failed for DMA.\n");
  710. ret = -ENOMEM;
  711. goto out_end;
  712. }
  713. #endif
  714. /* allocate buffers for a frame */
  715. for (i = 0; i < MAX_AR_HEIGHT; i++) {
  716. ar->frame[i] = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL);
  717. if (ar->frame[i] == NULL || ! ALIGN4(ar->frame[i])) {
  718. printk("arv: buffer allocation failed for frame.\n");
  719. ret = -ENOMEM;
  720. goto out_line_buff;
  721. }
  722. }
  723. ar->vdev = video_device_alloc();
  724. if (!ar->vdev) {
  725. printk(KERN_ERR "arv: video_device_alloc() failed\n");
  726. return -ENOMEM;
  727. }
  728. memcpy(ar->vdev, &ar_template, sizeof(ar_template));
  729. video_set_drvdata(ar->vdev, ar);
  730. if (vga) {
  731. ar->width = AR_WIDTH_VGA;
  732. ar->height = AR_HEIGHT_VGA;
  733. ar->size = AR_SIZE_VGA;
  734. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  735. ar->line_bytes = AR_LINE_BYTES_VGA;
  736. if (vga_interlace)
  737. ar->mode = AR_MODE_INTERLACE;
  738. else
  739. ar->mode = AR_MODE_NORMAL;
  740. } else {
  741. ar->width = AR_WIDTH_QVGA;
  742. ar->height = AR_HEIGHT_QVGA;
  743. ar->size = AR_SIZE_QVGA;
  744. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  745. ar->line_bytes = AR_LINE_BYTES_QVGA;
  746. ar->mode = AR_MODE_INTERLACE;
  747. }
  748. mutex_init(&ar->lock);
  749. init_waitqueue_head(&ar->wait);
  750. #if USE_INT
  751. if (request_irq(M32R_IRQ_INT3, ar_interrupt, 0, "arv", ar)) {
  752. printk("arv: request_irq(%d) failed.\n", M32R_IRQ_INT3);
  753. ret = -EIO;
  754. goto out_irq;
  755. }
  756. #endif
  757. if (ar_initialize(ar->vdev) != 0) {
  758. printk("arv: M64278 not found.\n");
  759. ret = -ENODEV;
  760. goto out_dev;
  761. }
  762. /*
  763. * ok, we can initialize h/w according to parameters,
  764. * so register video device as a frame grabber type.
  765. * device is named "video[0-64]".
  766. * video_register_device() initializes h/w using ar_initialize().
  767. */
  768. if (video_register_device(ar->vdev, VFL_TYPE_GRABBER, video_nr) != 0) {
  769. /* return -1, -ENFILE(full) or others */
  770. printk("arv: register video (Colour AR) failed.\n");
  771. ret = -ENODEV;
  772. goto out_dev;
  773. }
  774. printk("video%d: Found M64278 VGA (IRQ %d, Freq %dMHz).\n",
  775. ar->vdev->num, M32R_IRQ_INT3, freq);
  776. return 0;
  777. out_dev:
  778. #if USE_INT
  779. free_irq(M32R_IRQ_INT3, ar);
  780. out_irq:
  781. #endif
  782. for (i = 0; i < MAX_AR_HEIGHT; i++)
  783. kfree(ar->frame[i]);
  784. out_line_buff:
  785. #if USE_INT
  786. kfree(ar->line_buff);
  787. out_end:
  788. #endif
  789. return ret;
  790. }
  791. static int __init ar_init_module(void)
  792. {
  793. freq = (boot_cpu_data.bus_clock / 1000000);
  794. printk("arv: Bus clock %d\n", freq);
  795. if (freq != 50 && freq != 75)
  796. freq = DEFAULT_FREQ;
  797. return ar_init();
  798. }
  799. static void __exit ar_cleanup_module(void)
  800. {
  801. struct ar_device *ar;
  802. int i;
  803. ar = &ardev;
  804. video_unregister_device(ar->vdev);
  805. #if USE_INT
  806. free_irq(M32R_IRQ_INT3, ar);
  807. #endif
  808. for (i = 0; i < MAX_AR_HEIGHT; i++)
  809. kfree(ar->frame[i]);
  810. #if USE_INT
  811. kfree(ar->line_buff);
  812. #endif
  813. }
  814. module_init(ar_init_module);
  815. module_exit(ar_cleanup_module);
  816. MODULE_AUTHOR("Takeo Takahashi <takahashi.takeo@renesas.com>");
  817. MODULE_DESCRIPTION("Colour AR M64278(VGA) for Video4Linux");
  818. MODULE_LICENSE("GPL");