arv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 <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(KERN_INFO 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.0.5"
  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 {
  94. struct v4l2_device v4l2_dev;
  95. struct video_device vdev;
  96. unsigned int start_capture; /* duaring capture in INT. mode. */
  97. #if USE_INT
  98. unsigned char *line_buff; /* DMA line buffer */
  99. #endif
  100. unsigned char *frame[MAX_AR_HEIGHT]; /* frame data */
  101. short size; /* capture size */
  102. short mode; /* capture mode */
  103. int width, height;
  104. int frame_bytes, line_bytes;
  105. wait_queue_head_t wait;
  106. struct mutex lock;
  107. };
  108. static struct ar ardev;
  109. static int video_nr = -1; /* video device number (first free) */
  110. static unsigned char yuv[MAX_AR_FRAME_BYTES];
  111. /* module parameters */
  112. /* default frequency */
  113. #define DEFAULT_FREQ 50 /* 50 or 75 (MHz) is available as BCLK */
  114. static int freq = DEFAULT_FREQ; /* BCLK: available 50 or 70 (MHz) */
  115. static int vga; /* default mode(0:QVGA mode, other:VGA mode) */
  116. static int vga_interlace; /* 0 is normal mode for, else interlace mode */
  117. module_param(freq, int, 0);
  118. module_param(vga, int, 0);
  119. module_param(vga_interlace, int, 0);
  120. static void wait_for_vsync(void)
  121. {
  122. while (ar_inl(ARVCR0) & ARVCR0_VDS) /* wait for VSYNC */
  123. cpu_relax();
  124. while (!(ar_inl(ARVCR0) & ARVCR0_VDS)) /* wait for VSYNC */
  125. cpu_relax();
  126. }
  127. static void wait_acknowledge(void)
  128. {
  129. int i;
  130. for (i = 0; i < 1000; i++)
  131. cpu_relax();
  132. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_NOACK)
  133. cpu_relax();
  134. }
  135. /*******************************************************************
  136. * I2C functions
  137. *******************************************************************/
  138. static void iic(int n, unsigned long addr, unsigned long data1, unsigned long data2,
  139. unsigned long data3)
  140. {
  141. int i;
  142. /* Slave Address */
  143. ar_outl(addr, PLDI2CDATA);
  144. wait_for_vsync();
  145. /* Start */
  146. ar_outl(1, PLDI2CCND);
  147. wait_acknowledge();
  148. /* Transfer data 1 */
  149. ar_outl(data1, PLDI2CDATA);
  150. wait_for_vsync();
  151. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  152. wait_acknowledge();
  153. /* Transfer data 2 */
  154. ar_outl(data2, PLDI2CDATA);
  155. wait_for_vsync();
  156. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  157. wait_acknowledge();
  158. if (n == 3) {
  159. /* Transfer data 3 */
  160. ar_outl(data3, PLDI2CDATA);
  161. wait_for_vsync();
  162. ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
  163. wait_acknowledge();
  164. }
  165. /* Stop */
  166. for (i = 0; i < 100; i++)
  167. cpu_relax();
  168. ar_outl(2, PLDI2CCND);
  169. ar_outl(2, PLDI2CCND);
  170. while (ar_inl(PLDI2CSTS) & PLDI2CSTS_BB)
  171. cpu_relax();
  172. }
  173. static void init_iic(void)
  174. {
  175. DEBUG(1, "init_iic:\n");
  176. /*
  177. * ICU Setting (iic)
  178. */
  179. /* I2C Setting */
  180. ar_outl(0x0, PLDI2CCR); /* I2CCR Disable */
  181. ar_outl(0x0300, PLDI2CMOD); /* I2CMOD ACK/8b-data/7b-addr/auto */
  182. ar_outl(0x1, PLDI2CACK); /* I2CACK ACK */
  183. /* I2C CLK */
  184. /* 50MH-100k */
  185. if (freq == 75)
  186. ar_outl(369, PLDI2CFREQ); /* BCLK = 75MHz */
  187. else if (freq == 50)
  188. ar_outl(244, PLDI2CFREQ); /* BCLK = 50MHz */
  189. else
  190. ar_outl(244, PLDI2CFREQ); /* default: BCLK = 50MHz */
  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 void wait_for_vertical_sync(struct ar *ar, 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. v4l2_err(&ar->v4l2_dev, "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 ar *ar = video_drvdata(file);
  233. long ret = ar->frame_bytes; /* return read bytes */
  234. unsigned long arvcr1 = 0;
  235. unsigned long flags;
  236. unsigned char *p;
  237. int h, w;
  238. unsigned char *py, *pu, *pv;
  239. #if !USE_INT
  240. int l;
  241. #endif
  242. DEBUG(1, "ar_read()\n");
  243. if (ar->size == AR_SIZE_QVGA)
  244. arvcr1 |= ARVCR1_QVGA;
  245. if (ar->mode == AR_MODE_NORMAL)
  246. arvcr1 |= ARVCR1_NORMAL;
  247. mutex_lock(&ar->lock);
  248. #if USE_INT
  249. local_irq_save(flags);
  250. disable_dma();
  251. ar_outl(0xa1871300, M32R_DMA0CR0_PORTL);
  252. ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
  253. /* set AR FIFO address as source(BSEL5) */
  254. ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
  255. ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
  256. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* destination addr. */
  257. ar_outl(ar->line_buff, M32R_DMA0RDA_PORTL); /* reload address */
  258. ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL); /* byte count (bytes) */
  259. ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL); /* reload count (bytes) */
  260. /*
  261. * Okay, kick AR LSI to invoke an interrupt
  262. */
  263. ar->start_capture = 0;
  264. ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
  265. local_irq_restore(flags);
  266. /* .... AR interrupts .... */
  267. interruptible_sleep_on(&ar->wait);
  268. if (signal_pending(current)) {
  269. printk(KERN_ERR "arv: interrupted while get frame data.\n");
  270. ret = -EINTR;
  271. goto out_up;
  272. }
  273. #else /* ! USE_INT */
  274. /* polling */
  275. ar_outl(arvcr1, ARVCR1);
  276. disable_dma();
  277. ar_outl(0x8000, M32R_DMAEDET_PORTL);
  278. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  279. ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
  280. ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
  281. ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
  282. ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL);
  283. ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL);
  284. local_irq_save(flags);
  285. while (ar_inl(ARVHCOUNT) != 0) /* wait for 0 */
  286. cpu_relax();
  287. if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
  288. for (h = 0; h < ar->height; h++) {
  289. wait_for_vertical_sync(ar, h);
  290. if (h < (AR_HEIGHT_VGA/2))
  291. l = h << 1;
  292. else
  293. l = (((h - (AR_HEIGHT_VGA/2)) << 1) + 1);
  294. ar_outl(virt_to_phys(ar->frame[l]), M32R_DMA0CDA_PORTL);
  295. enable_dma();
  296. while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
  297. cpu_relax();
  298. disable_dma();
  299. clear_dma_status();
  300. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  301. }
  302. } else {
  303. for (h = 0; h < ar->height; h++) {
  304. wait_for_vertical_sync(ar, h);
  305. ar_outl(virt_to_phys(ar->frame[h]), M32R_DMA0CDA_PORTL);
  306. enable_dma();
  307. while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
  308. cpu_relax();
  309. disable_dma();
  310. clear_dma_status();
  311. ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
  312. }
  313. }
  314. local_irq_restore(flags);
  315. #endif /* ! USE_INT */
  316. /*
  317. * convert YUV422 to YUV422P
  318. * +--------------------+
  319. * | Y0,Y1,... |
  320. * | ..............Yn |
  321. * +--------------------+
  322. * | U0,U1,........Un |
  323. * +--------------------+
  324. * | V0,V1,........Vn |
  325. * +--------------------+
  326. */
  327. py = yuv;
  328. pu = py + (ar->frame_bytes / 2);
  329. pv = pu + (ar->frame_bytes / 4);
  330. for (h = 0; h < ar->height; h++) {
  331. p = ar->frame[h];
  332. for (w = 0; w < ar->line_bytes; w += 4) {
  333. *py++ = *p++;
  334. *pu++ = *p++;
  335. *py++ = *p++;
  336. *pv++ = *p++;
  337. }
  338. }
  339. if (copy_to_user(buf, yuv, ar->frame_bytes)) {
  340. v4l2_err(&ar->v4l2_dev, "failed while copy_to_user yuv.\n");
  341. ret = -EFAULT;
  342. goto out_up;
  343. }
  344. DEBUG(1, "ret = %d\n", ret);
  345. out_up:
  346. mutex_unlock(&ar->lock);
  347. return ret;
  348. }
  349. static int ar_querycap(struct file *file, void *priv,
  350. struct v4l2_capability *vcap)
  351. {
  352. struct ar *ar = video_drvdata(file);
  353. strlcpy(vcap->driver, ar->vdev.name, sizeof(vcap->driver));
  354. strlcpy(vcap->card, "Colour AR VGA", sizeof(vcap->card));
  355. strlcpy(vcap->bus_info, "Platform", sizeof(vcap->bus_info));
  356. vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  357. return 0;
  358. }
  359. static int ar_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  360. {
  361. if (vin->index > 0)
  362. return -EINVAL;
  363. strlcpy(vin->name, "Camera", sizeof(vin->name));
  364. vin->type = V4L2_INPUT_TYPE_CAMERA;
  365. vin->audioset = 0;
  366. vin->tuner = 0;
  367. vin->std = V4L2_STD_ALL;
  368. vin->status = 0;
  369. return 0;
  370. }
  371. static int ar_g_input(struct file *file, void *fh, unsigned int *inp)
  372. {
  373. *inp = 0;
  374. return 0;
  375. }
  376. static int ar_s_input(struct file *file, void *fh, unsigned int inp)
  377. {
  378. return inp ? -EINVAL : 0;
  379. }
  380. static int ar_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  381. {
  382. struct ar *ar = video_drvdata(file);
  383. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  384. pix->width = ar->width;
  385. pix->height = ar->height;
  386. pix->pixelformat = V4L2_PIX_FMT_YUV422P;
  387. pix->field = (ar->mode == AR_MODE_NORMAL) ? V4L2_FIELD_NONE : V4L2_FIELD_INTERLACED;
  388. pix->bytesperline = ar->width;
  389. pix->sizeimage = 2 * ar->width * ar->height;
  390. /* Just a guess */
  391. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  392. return 0;
  393. }
  394. static int ar_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  395. {
  396. struct ar *ar = video_drvdata(file);
  397. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  398. if (pix->height <= AR_HEIGHT_QVGA || pix->width <= AR_WIDTH_QVGA) {
  399. pix->height = AR_HEIGHT_QVGA;
  400. pix->width = AR_WIDTH_QVGA;
  401. pix->field = V4L2_FIELD_INTERLACED;
  402. } else {
  403. pix->height = AR_HEIGHT_VGA;
  404. pix->width = AR_WIDTH_VGA;
  405. pix->field = vga_interlace ? V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE;
  406. }
  407. pix->pixelformat = V4L2_PIX_FMT_YUV422P;
  408. pix->bytesperline = ar->width;
  409. pix->sizeimage = 2 * ar->width * ar->height;
  410. /* Just a guess */
  411. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  412. return 0;
  413. }
  414. static int ar_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  415. {
  416. struct ar *ar = video_drvdata(file);
  417. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  418. int ret = ar_try_fmt_vid_cap(file, fh, fmt);
  419. if (ret)
  420. return ret;
  421. mutex_lock(&ar->lock);
  422. ar->width = pix->width;
  423. ar->height = pix->height;
  424. if (ar->width == AR_WIDTH_VGA) {
  425. ar->size = AR_SIZE_VGA;
  426. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  427. ar->line_bytes = AR_LINE_BYTES_VGA;
  428. if (vga_interlace)
  429. ar->mode = AR_MODE_INTERLACE;
  430. else
  431. ar->mode = AR_MODE_NORMAL;
  432. } else {
  433. ar->size = AR_SIZE_QVGA;
  434. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  435. ar->line_bytes = AR_LINE_BYTES_QVGA;
  436. ar->mode = AR_MODE_INTERLACE;
  437. }
  438. /* Ok we figured out what to use from our wide choice */
  439. mutex_unlock(&ar->lock);
  440. return 0;
  441. }
  442. static int ar_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  443. {
  444. static struct v4l2_fmtdesc formats[] = {
  445. { 0, 0, 0,
  446. "YUV 4:2:2 Planar", V4L2_PIX_FMT_YUV422P,
  447. { 0, 0, 0, 0 }
  448. },
  449. };
  450. enum v4l2_buf_type type = fmt->type;
  451. if (fmt->index > 0)
  452. return -EINVAL;
  453. *fmt = formats[fmt->index];
  454. fmt->type = type;
  455. return 0;
  456. }
  457. #if USE_INT
  458. /*
  459. * Interrupt handler
  460. */
  461. static void ar_interrupt(int irq, void *dev)
  462. {
  463. struct ar *ar = dev;
  464. unsigned int line_count;
  465. unsigned int line_number;
  466. unsigned int arvcr1;
  467. line_count = ar_inl(ARVHCOUNT); /* line number */
  468. if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
  469. /* operations for interlace mode */
  470. if (line_count < (AR_HEIGHT_VGA / 2)) /* even line */
  471. line_number = (line_count << 1);
  472. else /* odd line */
  473. line_number =
  474. (((line_count - (AR_HEIGHT_VGA / 2)) << 1) + 1);
  475. } else {
  476. line_number = line_count;
  477. }
  478. if (line_number == 0) {
  479. /*
  480. * It is an interrupt for line 0.
  481. * we have to start capture.
  482. */
  483. disable_dma();
  484. #if 0
  485. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* needless? */
  486. #endif
  487. memcpy(ar->frame[0], ar->line_buff, ar->line_bytes);
  488. #if 0
  489. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  490. #endif
  491. enable_dma();
  492. ar->start_capture = 1; /* during capture */
  493. return;
  494. }
  495. if (ar->start_capture == 1 && line_number <= (ar->height - 1)) {
  496. disable_dma();
  497. memcpy(ar->frame[line_number], ar->line_buff, ar->line_bytes);
  498. /*
  499. * if captured all line of a frame, disable AR interrupt
  500. * and wake a process up.
  501. */
  502. if (line_number == (ar->height - 1)) { /* end of line */
  503. ar->start_capture = 0;
  504. /* disable AR interrupt request */
  505. arvcr1 = ar_inl(ARVCR1);
  506. arvcr1 &= ~ARVCR1_HIEN; /* clear int. flag */
  507. ar_outl(arvcr1, ARVCR1); /* disable */
  508. wake_up_interruptible(&ar->wait);
  509. } else {
  510. #if 0
  511. ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL);
  512. ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
  513. #endif
  514. enable_dma();
  515. }
  516. }
  517. }
  518. #endif
  519. /*
  520. * ar_initialize()
  521. * ar_initialize() is called by video_register_device() and
  522. * initializes AR LSI and peripherals.
  523. *
  524. * -1 is returned in all failures.
  525. * 0 is returned in success.
  526. *
  527. */
  528. static int ar_initialize(struct ar *ar)
  529. {
  530. unsigned long cr = 0;
  531. int i, found = 0;
  532. DEBUG(1, "ar_initialize:\n");
  533. /*
  534. * initialize AR LSI
  535. */
  536. ar_outl(0, ARVCR0); /* assert reset of AR LSI */
  537. for (i = 0; i < 0x18; i++) /* wait for over 10 cycles @ 27MHz */
  538. cpu_relax();
  539. ar_outl(ARVCR0_RST, ARVCR0); /* negate reset of AR LSI (enable) */
  540. for (i = 0; i < 0x40d; i++) /* wait for over 420 cycles @ 27MHz */
  541. cpu_relax();
  542. /* AR uses INT3 of CPU as interrupt pin. */
  543. ar_outl(ARINTSEL_INT3, ARINTSEL);
  544. if (ar->size == AR_SIZE_QVGA)
  545. cr |= ARVCR1_QVGA;
  546. if (ar->mode == AR_MODE_NORMAL)
  547. cr |= ARVCR1_NORMAL;
  548. ar_outl(cr, ARVCR1);
  549. /*
  550. * Initialize IIC so that CPU can communicate with AR LSI,
  551. * and send boot commands to AR LSI.
  552. */
  553. init_iic();
  554. for (i = 0; i < 0x100000; i++) { /* > 0xa1d10, 56ms */
  555. if ((ar_inl(ARVCR0) & ARVCR0_VDS)) { /* VSYNC */
  556. found = 1;
  557. break;
  558. }
  559. }
  560. if (found == 0)
  561. return -ENODEV;
  562. v4l2_info(&ar->v4l2_dev, "Initializing ");
  563. iic(2, 0x78, 0x11, 0x01, 0x00); /* start */
  564. iic(3, 0x78, 0x12, 0x00, 0x06);
  565. iic(3, 0x78, 0x12, 0x12, 0x30);
  566. iic(3, 0x78, 0x12, 0x15, 0x58);
  567. iic(3, 0x78, 0x12, 0x17, 0x30);
  568. printk(KERN_CONT ".");
  569. iic(3, 0x78, 0x12, 0x1a, 0x97);
  570. iic(3, 0x78, 0x12, 0x1b, 0xff);
  571. iic(3, 0x78, 0x12, 0x1c, 0xff);
  572. iic(3, 0x78, 0x12, 0x26, 0x10);
  573. iic(3, 0x78, 0x12, 0x27, 0x00);
  574. printk(KERN_CONT ".");
  575. iic(2, 0x78, 0x34, 0x02, 0x00);
  576. iic(2, 0x78, 0x7a, 0x10, 0x00);
  577. iic(2, 0x78, 0x80, 0x39, 0x00);
  578. iic(2, 0x78, 0x81, 0xe6, 0x00);
  579. iic(2, 0x78, 0x8d, 0x00, 0x00);
  580. printk(KERN_CONT ".");
  581. iic(2, 0x78, 0x8e, 0x0c, 0x00);
  582. iic(2, 0x78, 0x8f, 0x00, 0x00);
  583. #if 0
  584. iic(2, 0x78, 0x90, 0x00, 0x00); /* AWB on=1 off=0 */
  585. #endif
  586. iic(2, 0x78, 0x93, 0x01, 0x00);
  587. iic(2, 0x78, 0x94, 0xcd, 0x00);
  588. iic(2, 0x78, 0x95, 0x00, 0x00);
  589. printk(KERN_CONT ".");
  590. iic(2, 0x78, 0x96, 0xa0, 0x00);
  591. iic(2, 0x78, 0x97, 0x00, 0x00);
  592. iic(2, 0x78, 0x98, 0x60, 0x00);
  593. iic(2, 0x78, 0x99, 0x01, 0x00);
  594. iic(2, 0x78, 0x9a, 0x19, 0x00);
  595. printk(KERN_CONT ".");
  596. iic(2, 0x78, 0x9b, 0x02, 0x00);
  597. iic(2, 0x78, 0x9c, 0xe8, 0x00);
  598. iic(2, 0x78, 0x9d, 0x02, 0x00);
  599. iic(2, 0x78, 0x9e, 0x2e, 0x00);
  600. iic(2, 0x78, 0xb8, 0x78, 0x00);
  601. iic(2, 0x78, 0xba, 0x05, 0x00);
  602. #if 0
  603. iic(2, 0x78, 0x83, 0x8c, 0x00); /* brightness */
  604. #endif
  605. printk(KERN_CONT ".");
  606. /* color correction */
  607. iic(3, 0x78, 0x49, 0x00, 0x95); /* a */
  608. iic(3, 0x78, 0x49, 0x01, 0x96); /* b */
  609. iic(3, 0x78, 0x49, 0x03, 0x85); /* c */
  610. iic(3, 0x78, 0x49, 0x04, 0x97); /* d */
  611. iic(3, 0x78, 0x49, 0x02, 0x7e); /* e(Lo) */
  612. iic(3, 0x78, 0x49, 0x05, 0xa4); /* f(Lo) */
  613. iic(3, 0x78, 0x49, 0x06, 0x04); /* e(Hi) */
  614. iic(3, 0x78, 0x49, 0x07, 0x04); /* e(Hi) */
  615. iic(2, 0x78, 0x48, 0x01, 0x00); /* on=1 off=0 */
  616. printk(KERN_CONT ".");
  617. iic(2, 0x78, 0x11, 0x00, 0x00); /* end */
  618. printk(KERN_CONT " done\n");
  619. return 0;
  620. }
  621. /****************************************************************************
  622. *
  623. * Video4Linux Module functions
  624. *
  625. ****************************************************************************/
  626. static const struct v4l2_file_operations ar_fops = {
  627. .owner = THIS_MODULE,
  628. .read = ar_read,
  629. .unlocked_ioctl = video_ioctl2,
  630. };
  631. static const struct v4l2_ioctl_ops ar_ioctl_ops = {
  632. .vidioc_querycap = ar_querycap,
  633. .vidioc_g_input = ar_g_input,
  634. .vidioc_s_input = ar_s_input,
  635. .vidioc_enum_input = ar_enum_input,
  636. .vidioc_enum_fmt_vid_cap = ar_enum_fmt_vid_cap,
  637. .vidioc_g_fmt_vid_cap = ar_g_fmt_vid_cap,
  638. .vidioc_s_fmt_vid_cap = ar_s_fmt_vid_cap,
  639. .vidioc_try_fmt_vid_cap = ar_try_fmt_vid_cap,
  640. };
  641. #define ALIGN4(x) ((((int)(x)) & 0x3) == 0)
  642. static int __init ar_init(void)
  643. {
  644. struct ar *ar;
  645. struct v4l2_device *v4l2_dev;
  646. int ret;
  647. int i;
  648. ar = &ardev;
  649. v4l2_dev = &ar->v4l2_dev;
  650. strlcpy(v4l2_dev->name, "arv", sizeof(v4l2_dev->name));
  651. v4l2_info(v4l2_dev, "Colour AR VGA driver %s\n", VERSION);
  652. ret = v4l2_device_register(NULL, v4l2_dev);
  653. if (ret < 0) {
  654. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  655. return ret;
  656. }
  657. ret = -EIO;
  658. #if USE_INT
  659. /* allocate a DMA buffer for 1 line. */
  660. ar->line_buff = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL | GFP_DMA);
  661. if (ar->line_buff == NULL || !ALIGN4(ar->line_buff)) {
  662. v4l2_err(v4l2_dev, "buffer allocation failed for DMA.\n");
  663. ret = -ENOMEM;
  664. goto out_end;
  665. }
  666. #endif
  667. /* allocate buffers for a frame */
  668. for (i = 0; i < MAX_AR_HEIGHT; i++) {
  669. ar->frame[i] = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL);
  670. if (ar->frame[i] == NULL || !ALIGN4(ar->frame[i])) {
  671. v4l2_err(v4l2_dev, "buffer allocation failed for frame.\n");
  672. ret = -ENOMEM;
  673. goto out_line_buff;
  674. }
  675. }
  676. strlcpy(ar->vdev.name, "Colour AR VGA", sizeof(ar->vdev.name));
  677. ar->vdev.v4l2_dev = v4l2_dev;
  678. ar->vdev.fops = &ar_fops;
  679. ar->vdev.ioctl_ops = &ar_ioctl_ops;
  680. ar->vdev.release = video_device_release_empty;
  681. video_set_drvdata(&ar->vdev, ar);
  682. if (vga) {
  683. ar->width = AR_WIDTH_VGA;
  684. ar->height = AR_HEIGHT_VGA;
  685. ar->size = AR_SIZE_VGA;
  686. ar->frame_bytes = AR_FRAME_BYTES_VGA;
  687. ar->line_bytes = AR_LINE_BYTES_VGA;
  688. if (vga_interlace)
  689. ar->mode = AR_MODE_INTERLACE;
  690. else
  691. ar->mode = AR_MODE_NORMAL;
  692. } else {
  693. ar->width = AR_WIDTH_QVGA;
  694. ar->height = AR_HEIGHT_QVGA;
  695. ar->size = AR_SIZE_QVGA;
  696. ar->frame_bytes = AR_FRAME_BYTES_QVGA;
  697. ar->line_bytes = AR_LINE_BYTES_QVGA;
  698. ar->mode = AR_MODE_INTERLACE;
  699. }
  700. mutex_init(&ar->lock);
  701. init_waitqueue_head(&ar->wait);
  702. #if USE_INT
  703. if (request_irq(M32R_IRQ_INT3, ar_interrupt, 0, "arv", ar)) {
  704. v4l2_err("request_irq(%d) failed.\n", M32R_IRQ_INT3);
  705. ret = -EIO;
  706. goto out_irq;
  707. }
  708. #endif
  709. if (ar_initialize(ar) != 0) {
  710. v4l2_err(v4l2_dev, "M64278 not found.\n");
  711. ret = -ENODEV;
  712. goto out_dev;
  713. }
  714. /*
  715. * ok, we can initialize h/w according to parameters,
  716. * so register video device as a frame grabber type.
  717. * device is named "video[0-64]".
  718. * video_register_device() initializes h/w using ar_initialize().
  719. */
  720. if (video_register_device(&ar->vdev, VFL_TYPE_GRABBER, video_nr) != 0) {
  721. /* return -1, -ENFILE(full) or others */
  722. v4l2_err(v4l2_dev, "register video (Colour AR) failed.\n");
  723. ret = -ENODEV;
  724. goto out_dev;
  725. }
  726. v4l2_info(v4l2_dev, "%s: Found M64278 VGA (IRQ %d, Freq %dMHz).\n",
  727. video_device_node_name(&ar->vdev), M32R_IRQ_INT3, freq);
  728. return 0;
  729. out_dev:
  730. #if USE_INT
  731. free_irq(M32R_IRQ_INT3, ar);
  732. out_irq:
  733. #endif
  734. for (i = 0; i < MAX_AR_HEIGHT; i++)
  735. kfree(ar->frame[i]);
  736. out_line_buff:
  737. #if USE_INT
  738. kfree(ar->line_buff);
  739. out_end:
  740. #endif
  741. v4l2_device_unregister(&ar->v4l2_dev);
  742. return ret;
  743. }
  744. static int __init ar_init_module(void)
  745. {
  746. freq = (boot_cpu_data.bus_clock / 1000000);
  747. printk(KERN_INFO "arv: Bus clock %d\n", freq);
  748. if (freq != 50 && freq != 75)
  749. freq = DEFAULT_FREQ;
  750. return ar_init();
  751. }
  752. static void __exit ar_cleanup_module(void)
  753. {
  754. struct ar *ar;
  755. int i;
  756. ar = &ardev;
  757. video_unregister_device(&ar->vdev);
  758. #if USE_INT
  759. free_irq(M32R_IRQ_INT3, ar);
  760. #endif
  761. for (i = 0; i < MAX_AR_HEIGHT; i++)
  762. kfree(ar->frame[i]);
  763. #if USE_INT
  764. kfree(ar->line_buff);
  765. #endif
  766. v4l2_device_unregister(&ar->v4l2_dev);
  767. }
  768. module_init(ar_init_module);
  769. module_exit(ar_cleanup_module);
  770. MODULE_AUTHOR("Takeo Takahashi <takahashi.takeo@renesas.com>");
  771. MODULE_DESCRIPTION("Colour AR M64278(VGA) for Video4Linux");
  772. MODULE_LICENSE("GPL");
  773. MODULE_VERSION(VERSION);