arv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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/videodev2.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-fh.h>
  34. #include <linux/mutex.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/m32r.h>
  37. #include <asm/io.h>
  38. #include <asm/dma.h>
  39. #include <asm/byteorder.h>
  40. #if 0
  41. #define DEBUG(n, args...) printk(KERN_INFO args)
  42. #define CHECK_LOST 1
  43. #else
  44. #define DEBUG(n, args...)
  45. #define CHECK_LOST 0
  46. #endif
  47. /*
  48. * USE_INT is always 0, interrupt mode is not available
  49. * on linux due to lack of speed
  50. */
  51. #define USE_INT 0 /* Don't modify */
  52. #define VERSION "0.0.5"
  53. #define ar_inl(addr) inl((unsigned long)(addr))
  54. #define ar_outl(val, addr) outl((unsigned long)(val), (unsigned long)(addr))
  55. extern struct cpuinfo_m32r boot_cpu_data;
  56. /*
  57. * CCD pixel size
  58. * Note that M32700UT does not support CIF mode, but QVGA is
  59. * supported by M32700UT hardware using VGA mode of AR LSI.
  60. *
  61. * Supported: VGA (Normal mode, Interlace mode)
  62. * QVGA (Always Interlace mode of VGA)
  63. *
  64. */
  65. #define AR_WIDTH_VGA 640
  66. #define AR_HEIGHT_VGA 480
  67. #define AR_WIDTH_QVGA 320
  68. #define AR_HEIGHT_QVGA 240
  69. #define MIN_AR_WIDTH AR_WIDTH_QVGA
  70. #define MIN_AR_HEIGHT AR_HEIGHT_QVGA
  71. #define MAX_AR_WIDTH AR_WIDTH_VGA
  72. #define MAX_AR_HEIGHT AR_HEIGHT_VGA
  73. /* bits & bytes per pixel */
  74. #define AR_BITS_PER_PIXEL 16
  75. #define AR_BYTES_PER_PIXEL (AR_BITS_PER_PIXEL / 8)
  76. /* line buffer size */
  77. #define AR_LINE_BYTES_VGA (AR_WIDTH_VGA * AR_BYTES_PER_PIXEL)
  78. #define AR_LINE_BYTES_QVGA (AR_WIDTH_QVGA * AR_BYTES_PER_PIXEL)
  79. #define MAX_AR_LINE_BYTES AR_LINE_BYTES_VGA
  80. /* frame size & type */
  81. #define AR_FRAME_BYTES_VGA \
  82. (AR_WIDTH_VGA * AR_HEIGHT_VGA * AR_BYTES_PER_PIXEL)
  83. #define AR_FRAME_BYTES_QVGA \
  84. (AR_WIDTH_QVGA * AR_HEIGHT_QVGA * AR_BYTES_PER_PIXEL)
  85. #define MAX_AR_FRAME_BYTES \
  86. (MAX_AR_WIDTH * MAX_AR_HEIGHT * AR_BYTES_PER_PIXEL)
  87. #define AR_MAX_FRAME 15
  88. /* capture size */
  89. #define AR_SIZE_VGA 0
  90. #define AR_SIZE_QVGA 1
  91. /* capture mode */
  92. #define AR_MODE_INTERLACE 0
  93. #define AR_MODE_NORMAL 1
  94. struct ar {
  95. struct v4l2_device v4l2_dev;
  96. struct video_device vdev;
  97. int start_capture; /* duaring capture in INT. mode. */
  98. #if USE_INT
  99. unsigned char *line_buff; /* DMA line buffer */
  100. #endif
  101. unsigned char *frame[MAX_AR_HEIGHT]; /* frame data */
  102. short size; /* capture size */
  103. short mode; /* capture mode */
  104. int width, height;
  105. int frame_bytes, line_bytes;
  106. wait_queue_head_t wait;
  107. struct mutex lock;
  108. };
  109. static struct ar ardev;
  110. static int video_nr = -1; /* video device number (first free) */
  111. static unsigned char yuv[MAX_AR_FRAME_BYTES];
  112. /* module parameters */
  113. /* default frequency */
  114. #define DEFAULT_FREQ 50 /* 50 or 75 (MHz) is available as BCLK */
  115. static int freq = DEFAULT_FREQ; /* BCLK: available 50 or 70 (MHz) */
  116. static int vga; /* default mode(0:QVGA mode, other:VGA mode) */
  117. static int vga_interlace; /* 0 is normal mode for, else interlace mode */
  118. module_param(freq, int, 0);
  119. module_param(vga, int, 0);
  120. module_param(vga_interlace, int, 0);
  121. static void wait_for_vsync(void)
  122. {
  123. while (ar_inl(ARVCR0) & ARVCR0_VDS) /* wait for VSYNC */
  124. cpu_relax();
  125. while (!(ar_inl(ARVCR0) & ARVCR0_VDS)) /* wait for VSYNC */
  126. cpu_relax();
  127. }
  128. static void wait_acknowledge(void)
  129. {
  130. int i;
  131. for (i = 0; i < 1000; i++)
  132. cpu_relax();
  133. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_NOACK)
  134. cpu_relax();
  135. }
  136. /*******************************************************************
  137. * I2C functions
  138. *******************************************************************/
  139. static void iic(int n, unsigned long addr, unsigned long data1, unsigned long data2,
  140. unsigned long data3)
  141. {
  142. int i;
  143. /* Slave Address */
  144. ar_outl(addr, PLDI2CDATA);
  145. wait_for_vsync();
  146. /* Start */
  147. ar_outl(1, PLDI2CCND);
  148. wait_acknowledge();
  149. /* Transfer data 1 */
  150. ar_outl(data1, PLDI2CDATA);
  151. wait_for_vsync();
  152. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  153. wait_acknowledge();
  154. /* Transfer data 2 */
  155. ar_outl(data2, PLDI2CDATA);
  156. wait_for_vsync();
  157. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  158. wait_acknowledge();
  159. if (n == 3) {
  160. /* Transfer data 3 */
  161. ar_outl(data3, PLDI2CDATA);
  162. wait_for_vsync();
  163. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  164. wait_acknowledge();
  165. }
  166. /* Stop */
  167. for (i = 0; i < 100; i++)
  168. cpu_relax();
  169. ar_outl(2, PLDI2CCND);
  170. ar_outl(2, PLDI2CCND);
  171. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_BB)
  172. cpu_relax();
  173. }
  174. static void init_iic(void)
  175. {
  176. DEBUG(1, "init_iic:\n");
  177. /*
  178. * ICU Setting (iic)
  179. */
  180. /* I2C Setting */
  181. ar_outl(0x0, PLDI2CCR); /* I2CCR Disable */
  182. ar_outl(0x0300, PLDI2CMOD); /* I2CMOD ACK/8b-data/7b-addr/auto */
  183. ar_outl(0x1, PLDI2CACK); /* I2CACK ACK */
  184. /* I2C CLK */
  185. /* 50MH-100k */
  186. if (freq == 75)
  187. ar_outl(369, PLDI2CFREQ); /* BCLK = 75MHz */
  188. else if (freq == 50)
  189. ar_outl(244, PLDI2CFREQ); /* BCLK = 50MHz */
  190. else
  191. ar_outl(244, PLDI2CFREQ); /* default: BCLK = 50MHz */
  192. ar_outl(0x1, PLDI2CCR); /* I2CCR Enable */
  193. }
  194. /**************************************************************************
  195. *
  196. * Video4Linux Interface functions
  197. *
  198. **************************************************************************/
  199. static inline void disable_dma(void)
  200. {
  201. ar_outl(0x8000, M32R_DMAEN_PORTL); /* disable DMA0 */
  202. }
  203. static inline void enable_dma(void)
  204. {
  205. ar_outl(0x8080, M32R_DMAEN_PORTL); /* enable DMA0 */
  206. }
  207. static inline void clear_dma_status(void)
  208. {
  209. ar_outl(0x8000, M32R_DMAEDET_PORTL); /* clear status */
  210. }
  211. static void wait_for_vertical_sync(struct ar *ar, int exp_line)
  212. {
  213. #if CHECK_LOST
  214. int tmout = 10000; /* FIXME */
  215. int l;
  216. /*
  217. * check HCOUNT because we cannot check vertical sync.
  218. */
  219. for (; tmout >= 0; tmout--) {
  220. l = ar_inl(ARVHCOUNT);
  221. if (l == exp_line)
  222. break;
  223. }
  224. if (tmout < 0)
  225. v4l2_err(&ar->v4l2_dev, "lost %d -> %d\n", exp_line, l);
  226. #else
  227. while (ar_inl(ARVHCOUNT) != exp_line)
  228. cpu_relax();
  229. #endif
  230. }
  231. static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
  232. {
  233. struct ar *ar = video_drvdata(file);
  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. * Okay, kick AR LSI to invoke an interrupt
  263. */
  264. ar->start_capture = -1;
  265. ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
  266. local_irq_restore(flags);
  267. /* .... AR interrupts .... */
  268. wait_event_interruptible(ar->wait, ar->start_capture == 0);
  269. if (signal_pending(current)) {
  270. printk(KERN_ERR "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(ar, 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(ar, 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. v4l2_err(&ar->v4l2_dev, "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_querycap(struct file *file, void *priv,
  351. struct v4l2_capability *vcap)
  352. {
  353. struct ar *ar = video_drvdata(file);
  354. strlcpy(vcap->driver, ar->vdev.name, sizeof(vcap->driver));
  355. strlcpy(vcap->card, "Colour AR VGA", sizeof(vcap->card));
  356. strlcpy(vcap->bus_info, "Platform", sizeof(vcap->bus_info));
  357. vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  358. vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
  359. return 0;
  360. }
  361. static int ar_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  362. {
  363. if (vin->index > 0)
  364. return -EINVAL;
  365. strlcpy(vin->name, "Camera", sizeof(vin->name));
  366. vin->type = V4L2_INPUT_TYPE_CAMERA;
  367. vin->audioset = 0;
  368. vin->tuner = 0;
  369. vin->std = V4L2_STD_ALL;
  370. vin->status = 0;
  371. return 0;
  372. }
  373. static int ar_g_input(struct file *file, void *fh, unsigned int *inp)
  374. {
  375. *inp = 0;
  376. return 0;
  377. }
  378. static int ar_s_input(struct file *file, void *fh, unsigned int inp)
  379. {
  380. return inp ? -EINVAL : 0;
  381. }
  382. static int ar_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  383. {
  384. struct ar *ar = video_drvdata(file);
  385. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  386. pix->width = ar->width;
  387. pix->height = ar->height;
  388. pix->pixelformat = V4L2_PIX_FMT_YUV422P;
  389. pix->field = (ar->mode == AR_MODE_NORMAL) ? V4L2_FIELD_NONE : V4L2_FIELD_INTERLACED;
  390. pix->bytesperline = ar->width;
  391. pix->sizeimage = 2 * ar->width * ar->height;
  392. /* Just a guess */
  393. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  394. return 0;
  395. }
  396. static int ar_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  397. {
  398. struct ar *ar = video_drvdata(file);
  399. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  400. if (pix->height <= AR_HEIGHT_QVGA || pix->width <= AR_WIDTH_QVGA) {
  401. pix->height = AR_HEIGHT_QVGA;
  402. pix->width = AR_WIDTH_QVGA;
  403. pix->field = V4L2_FIELD_INTERLACED;
  404. } else {
  405. pix->height = AR_HEIGHT_VGA;
  406. pix->width = AR_WIDTH_VGA;
  407. pix->field = vga_interlace ? V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE;
  408. }
  409. pix->pixelformat = V4L2_PIX_FMT_YUV422P;
  410. pix->bytesperline = ar->width;
  411. pix->sizeimage = 2 * ar->width * ar->height;
  412. /* Just a guess */
  413. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  414. return 0;
  415. }
  416. static int ar_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  417. {
  418. struct ar *ar = video_drvdata(file);
  419. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  420. int ret = ar_try_fmt_vid_cap(file, fh, fmt);
  421. if (ret)
  422. return ret;
  423. mutex_lock(&ar->lock);
  424. ar->width = pix->width;
  425. ar->height = pix->height;
  426. if (ar->width == AR_WIDTH_VGA) {
  427. ar->size = AR_SIZE_VGA;
  428. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  429. ar->line_bytes = AR_LINE_BYTES_VGA;
  430. if (vga_interlace)
  431. ar->mode = AR_MODE_INTERLACE;
  432. else
  433. ar->mode = AR_MODE_NORMAL;
  434. } else {
  435. ar->size = AR_SIZE_QVGA;
  436. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  437. ar->line_bytes = AR_LINE_BYTES_QVGA;
  438. ar->mode = AR_MODE_INTERLACE;
  439. }
  440. /* Ok we figured out what to use from our wide choice */
  441. mutex_unlock(&ar->lock);
  442. return 0;
  443. }
  444. static int ar_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  445. {
  446. static struct v4l2_fmtdesc formats[] = {
  447. { 0, 0, 0,
  448. "YUV 4:2:2 Planar", V4L2_PIX_FMT_YUV422P,
  449. { 0, 0, 0, 0 }
  450. },
  451. };
  452. enum v4l2_buf_type type = fmt->type;
  453. if (fmt->index > 0)
  454. return -EINVAL;
  455. *fmt = formats[fmt->index];
  456. fmt->type = type;
  457. return 0;
  458. }
  459. #if USE_INT
  460. /*
  461. * Interrupt handler
  462. */
  463. static void ar_interrupt(int irq, void *dev)
  464. {
  465. struct ar *ar = dev;
  466. unsigned int line_count;
  467. unsigned int line_number;
  468. unsigned int arvcr1;
  469. line_count = ar_inl(ARVHCOUNT); /* line number */
  470. if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
  471. /* operations for interlace mode */
  472. if (line_count < (AR_HEIGHT_VGA / 2)) /* even line */
  473. line_number = (line_count << 1);
  474. else /* odd line */
  475. line_number =
  476. (((line_count - (AR_HEIGHT_VGA / 2)) << 1) + 1);
  477. } else {
  478. line_number = line_count;
  479. }
  480. if (line_number == 0) {
  481. /*
  482. * It is an interrupt for line 0.
  483. * we have to start capture.
  484. */
  485. disable_dma();
  486. #if 0
  487. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* needless? */
  488. #endif
  489. memcpy(ar->frame[0], ar->line_buff, ar->line_bytes);
  490. #if 0
  491. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  492. #endif
  493. enable_dma();
  494. ar->start_capture = 1; /* during capture */
  495. return;
  496. }
  497. if (ar->start_capture == 1 && line_number <= (ar->height - 1)) {
  498. disable_dma();
  499. memcpy(ar->frame[line_number], ar->line_buff, ar->line_bytes);
  500. /*
  501. * if captured all line of a frame, disable AR interrupt
  502. * and wake a process up.
  503. */
  504. if (line_number == (ar->height - 1)) { /* end of line */
  505. ar->start_capture = 0;
  506. /* disable AR interrupt request */
  507. arvcr1 = ar_inl(ARVCR1);
  508. arvcr1 &= ~ARVCR1_HIEN; /* clear int. flag */
  509. ar_outl(arvcr1, ARVCR1); /* disable */
  510. wake_up_interruptible(&ar->wait);
  511. } else {
  512. #if 0
  513. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL);
  514. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  515. #endif
  516. enable_dma();
  517. }
  518. }
  519. }
  520. #endif
  521. /*
  522. * ar_initialize()
  523. * ar_initialize() is called by video_register_device() and
  524. * initializes AR LSI and peripherals.
  525. *
  526. * -1 is returned in all failures.
  527. * 0 is returned in success.
  528. *
  529. */
  530. static int ar_initialize(struct ar *ar)
  531. {
  532. unsigned long cr = 0;
  533. int i, found = 0;
  534. DEBUG(1, "ar_initialize:\n");
  535. /*
  536. * initialize AR LSI
  537. */
  538. ar_outl(0, ARVCR0); /* assert reset of AR LSI */
  539. for (i = 0; i < 0x18; i++) /* wait for over 10 cycles @ 27MHz */
  540. cpu_relax();
  541. ar_outl(ARVCR0_RST, ARVCR0); /* negate reset of AR LSI (enable) */
  542. for (i = 0; i < 0x40d; i++) /* wait for over 420 cycles @ 27MHz */
  543. cpu_relax();
  544. /* AR uses INT3 of CPU as interrupt pin. */
  545. ar_outl(ARINTSEL_INT3, ARINTSEL);
  546. if (ar->size == AR_SIZE_QVGA)
  547. cr |= ARVCR1_QVGA;
  548. if (ar->mode == AR_MODE_NORMAL)
  549. cr |= ARVCR1_NORMAL;
  550. ar_outl(cr, ARVCR1);
  551. /*
  552. * Initialize IIC so that CPU can communicate with AR LSI,
  553. * and send boot commands to AR LSI.
  554. */
  555. init_iic();
  556. for (i = 0; i < 0x100000; i++) { /* > 0xa1d10, 56ms */
  557. if ((ar_inl(ARVCR0) & ARVCR0_VDS)) { /* VSYNC */
  558. found = 1;
  559. break;
  560. }
  561. }
  562. if (found == 0)
  563. return -ENODEV;
  564. v4l2_info(&ar->v4l2_dev, "Initializing ");
  565. iic(2, 0x78, 0x11, 0x01, 0x00); /* start */
  566. iic(3, 0x78, 0x12, 0x00, 0x06);
  567. iic(3, 0x78, 0x12, 0x12, 0x30);
  568. iic(3, 0x78, 0x12, 0x15, 0x58);
  569. iic(3, 0x78, 0x12, 0x17, 0x30);
  570. printk(KERN_CONT ".");
  571. iic(3, 0x78, 0x12, 0x1a, 0x97);
  572. iic(3, 0x78, 0x12, 0x1b, 0xff);
  573. iic(3, 0x78, 0x12, 0x1c, 0xff);
  574. iic(3, 0x78, 0x12, 0x26, 0x10);
  575. iic(3, 0x78, 0x12, 0x27, 0x00);
  576. printk(KERN_CONT ".");
  577. iic(2, 0x78, 0x34, 0x02, 0x00);
  578. iic(2, 0x78, 0x7a, 0x10, 0x00);
  579. iic(2, 0x78, 0x80, 0x39, 0x00);
  580. iic(2, 0x78, 0x81, 0xe6, 0x00);
  581. iic(2, 0x78, 0x8d, 0x00, 0x00);
  582. printk(KERN_CONT ".");
  583. iic(2, 0x78, 0x8e, 0x0c, 0x00);
  584. iic(2, 0x78, 0x8f, 0x00, 0x00);
  585. #if 0
  586. iic(2, 0x78, 0x90, 0x00, 0x00); /* AWB on=1 off=0 */
  587. #endif
  588. iic(2, 0x78, 0x93, 0x01, 0x00);
  589. iic(2, 0x78, 0x94, 0xcd, 0x00);
  590. iic(2, 0x78, 0x95, 0x00, 0x00);
  591. printk(KERN_CONT ".");
  592. iic(2, 0x78, 0x96, 0xa0, 0x00);
  593. iic(2, 0x78, 0x97, 0x00, 0x00);
  594. iic(2, 0x78, 0x98, 0x60, 0x00);
  595. iic(2, 0x78, 0x99, 0x01, 0x00);
  596. iic(2, 0x78, 0x9a, 0x19, 0x00);
  597. printk(KERN_CONT ".");
  598. iic(2, 0x78, 0x9b, 0x02, 0x00);
  599. iic(2, 0x78, 0x9c, 0xe8, 0x00);
  600. iic(2, 0x78, 0x9d, 0x02, 0x00);
  601. iic(2, 0x78, 0x9e, 0x2e, 0x00);
  602. iic(2, 0x78, 0xb8, 0x78, 0x00);
  603. iic(2, 0x78, 0xba, 0x05, 0x00);
  604. #if 0
  605. iic(2, 0x78, 0x83, 0x8c, 0x00); /* brightness */
  606. #endif
  607. printk(KERN_CONT ".");
  608. /* color correction */
  609. iic(3, 0x78, 0x49, 0x00, 0x95); /* a */
  610. iic(3, 0x78, 0x49, 0x01, 0x96); /* b */
  611. iic(3, 0x78, 0x49, 0x03, 0x85); /* c */
  612. iic(3, 0x78, 0x49, 0x04, 0x97); /* d */
  613. iic(3, 0x78, 0x49, 0x02, 0x7e); /* e(Lo) */
  614. iic(3, 0x78, 0x49, 0x05, 0xa4); /* f(Lo) */
  615. iic(3, 0x78, 0x49, 0x06, 0x04); /* e(Hi) */
  616. iic(3, 0x78, 0x49, 0x07, 0x04); /* e(Hi) */
  617. iic(2, 0x78, 0x48, 0x01, 0x00); /* on=1 off=0 */
  618. printk(KERN_CONT ".");
  619. iic(2, 0x78, 0x11, 0x00, 0x00); /* end */
  620. printk(KERN_CONT " done\n");
  621. return 0;
  622. }
  623. /****************************************************************************
  624. *
  625. * Video4Linux Module functions
  626. *
  627. ****************************************************************************/
  628. static const struct v4l2_file_operations ar_fops = {
  629. .owner = THIS_MODULE,
  630. .open = v4l2_fh_open,
  631. .release = v4l2_fh_release,
  632. .read = ar_read,
  633. .unlocked_ioctl = video_ioctl2,
  634. };
  635. static const struct v4l2_ioctl_ops ar_ioctl_ops = {
  636. .vidioc_querycap = ar_querycap,
  637. .vidioc_g_input = ar_g_input,
  638. .vidioc_s_input = ar_s_input,
  639. .vidioc_enum_input = ar_enum_input,
  640. .vidioc_enum_fmt_vid_cap = ar_enum_fmt_vid_cap,
  641. .vidioc_g_fmt_vid_cap = ar_g_fmt_vid_cap,
  642. .vidioc_s_fmt_vid_cap = ar_s_fmt_vid_cap,
  643. .vidioc_try_fmt_vid_cap = ar_try_fmt_vid_cap,
  644. };
  645. #define ALIGN4(x) ((((int)(x)) & 0x3) == 0)
  646. static int __init ar_init(void)
  647. {
  648. struct ar *ar;
  649. struct v4l2_device *v4l2_dev;
  650. int ret;
  651. int i;
  652. ar = &ardev;
  653. v4l2_dev = &ar->v4l2_dev;
  654. strlcpy(v4l2_dev->name, "arv", sizeof(v4l2_dev->name));
  655. v4l2_info(v4l2_dev, "Colour AR VGA driver %s\n", VERSION);
  656. ret = v4l2_device_register(NULL, v4l2_dev);
  657. if (ret < 0) {
  658. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  659. return ret;
  660. }
  661. ret = -EIO;
  662. #if USE_INT
  663. /* allocate a DMA buffer for 1 line. */
  664. ar->line_buff = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL | GFP_DMA);
  665. if (ar->line_buff == NULL || !ALIGN4(ar->line_buff)) {
  666. v4l2_err(v4l2_dev, "buffer allocation failed for DMA.\n");
  667. ret = -ENOMEM;
  668. goto out_end;
  669. }
  670. #endif
  671. /* allocate buffers for a frame */
  672. for (i = 0; i < MAX_AR_HEIGHT; i++) {
  673. ar->frame[i] = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL);
  674. if (ar->frame[i] == NULL || !ALIGN4(ar->frame[i])) {
  675. v4l2_err(v4l2_dev, "buffer allocation failed for frame.\n");
  676. ret = -ENOMEM;
  677. goto out_line_buff;
  678. }
  679. }
  680. strlcpy(ar->vdev.name, "Colour AR VGA", sizeof(ar->vdev.name));
  681. ar->vdev.v4l2_dev = v4l2_dev;
  682. ar->vdev.fops = &ar_fops;
  683. ar->vdev.ioctl_ops = &ar_ioctl_ops;
  684. ar->vdev.release = video_device_release_empty;
  685. video_set_drvdata(&ar->vdev, ar);
  686. if (vga) {
  687. ar->width = AR_WIDTH_VGA;
  688. ar->height = AR_HEIGHT_VGA;
  689. ar->size = AR_SIZE_VGA;
  690. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  691. ar->line_bytes = AR_LINE_BYTES_VGA;
  692. if (vga_interlace)
  693. ar->mode = AR_MODE_INTERLACE;
  694. else
  695. ar->mode = AR_MODE_NORMAL;
  696. } else {
  697. ar->width = AR_WIDTH_QVGA;
  698. ar->height = AR_HEIGHT_QVGA;
  699. ar->size = AR_SIZE_QVGA;
  700. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  701. ar->line_bytes = AR_LINE_BYTES_QVGA;
  702. ar->mode = AR_MODE_INTERLACE;
  703. }
  704. mutex_init(&ar->lock);
  705. init_waitqueue_head(&ar->wait);
  706. #if USE_INT
  707. if (request_irq(M32R_IRQ_INT3, ar_interrupt, 0, "arv", ar)) {
  708. v4l2_err("request_irq(%d) failed.\n", M32R_IRQ_INT3);
  709. ret = -EIO;
  710. goto out_irq;
  711. }
  712. #endif
  713. if (ar_initialize(ar) != 0) {
  714. v4l2_err(v4l2_dev, "M64278 not found.\n");
  715. ret = -ENODEV;
  716. goto out_dev;
  717. }
  718. /*
  719. * ok, we can initialize h/w according to parameters,
  720. * so register video device as a frame grabber type.
  721. * device is named "video[0-64]".
  722. * video_register_device() initializes h/w using ar_initialize().
  723. */
  724. if (video_register_device(&ar->vdev, VFL_TYPE_GRABBER, video_nr) != 0) {
  725. /* return -1, -ENFILE(full) or others */
  726. v4l2_err(v4l2_dev, "register video (Colour AR) failed.\n");
  727. ret = -ENODEV;
  728. goto out_dev;
  729. }
  730. v4l2_info(v4l2_dev, "%s: Found M64278 VGA (IRQ %d, Freq %dMHz).\n",
  731. video_device_node_name(&ar->vdev), M32R_IRQ_INT3, freq);
  732. return 0;
  733. out_dev:
  734. #if USE_INT
  735. free_irq(M32R_IRQ_INT3, ar);
  736. out_irq:
  737. #endif
  738. for (i = 0; i < MAX_AR_HEIGHT; i++)
  739. kfree(ar->frame[i]);
  740. out_line_buff:
  741. #if USE_INT
  742. kfree(ar->line_buff);
  743. out_end:
  744. #endif
  745. v4l2_device_unregister(&ar->v4l2_dev);
  746. return ret;
  747. }
  748. static int __init ar_init_module(void)
  749. {
  750. freq = (boot_cpu_data.bus_clock / 1000000);
  751. printk(KERN_INFO "arv: Bus clock %d\n", freq);
  752. if (freq != 50 && freq != 75)
  753. freq = DEFAULT_FREQ;
  754. return ar_init();
  755. }
  756. static void __exit ar_cleanup_module(void)
  757. {
  758. struct ar *ar;
  759. int i;
  760. ar = &ardev;
  761. video_unregister_device(&ar->vdev);
  762. #if USE_INT
  763. free_irq(M32R_IRQ_INT3, ar);
  764. #endif
  765. for (i = 0; i < MAX_AR_HEIGHT; i++)
  766. kfree(ar->frame[i]);
  767. #if USE_INT
  768. kfree(ar->line_buff);
  769. #endif
  770. v4l2_device_unregister(&ar->v4l2_dev);
  771. }
  772. module_init(ar_init_module);
  773. module_exit(ar_cleanup_module);
  774. MODULE_AUTHOR("Takeo Takahashi <takahashi.takeo@renesas.com>");
  775. MODULE_DESCRIPTION("Colour AR M64278(VGA) for Video4Linux");
  776. MODULE_LICENSE("GPL");
  777. MODULE_VERSION(VERSION);