sh_mobile_ceu_camera.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * V4L2 Driver for SuperH Mobile CEU interface
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
  7. *
  8. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/io.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/time.h>
  28. #include <linux/version.h>
  29. #include <linux/device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/mutex.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/clk.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-dev.h>
  36. #include <media/soc_camera.h>
  37. #include <media/sh_mobile_ceu.h>
  38. #include <media/videobuf-dma-contig.h>
  39. /* register offsets for sh7722 / sh7723 */
  40. #define CAPSR 0x00 /* Capture start register */
  41. #define CAPCR 0x04 /* Capture control register */
  42. #define CAMCR 0x08 /* Capture interface control register */
  43. #define CMCYR 0x0c /* Capture interface cycle register */
  44. #define CAMOR 0x10 /* Capture interface offset register */
  45. #define CAPWR 0x14 /* Capture interface width register */
  46. #define CAIFR 0x18 /* Capture interface input format register */
  47. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  48. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  49. #define CRCNTR 0x28 /* CEU register control register */
  50. #define CRCMPR 0x2c /* CEU register forcible control register */
  51. #define CFLCR 0x30 /* Capture filter control register */
  52. #define CFSZR 0x34 /* Capture filter size clip register */
  53. #define CDWDR 0x38 /* Capture destination width register */
  54. #define CDAYR 0x3c /* Capture data address Y register */
  55. #define CDACR 0x40 /* Capture data address C register */
  56. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  57. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  58. #define CBDSR 0x4c /* Capture bundle destination size register */
  59. #define CFWCR 0x5c /* Firewall operation control register */
  60. #define CLFCR 0x60 /* Capture low-pass filter control register */
  61. #define CDOCR 0x64 /* Capture data output control register */
  62. #define CDDCR 0x68 /* Capture data complexity level register */
  63. #define CDDAR 0x6c /* Capture data complexity level address register */
  64. #define CEIER 0x70 /* Capture event interrupt enable register */
  65. #define CETCR 0x74 /* Capture event flag clear register */
  66. #define CSTSR 0x7c /* Capture status register */
  67. #define CSRTR 0x80 /* Capture software reset register */
  68. #define CDSSR 0x84 /* Capture data size register */
  69. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  70. #define CDACR2 0x94 /* Capture data address C register 2 */
  71. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  72. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  73. static DEFINE_MUTEX(camera_lock);
  74. /* per video frame buffer */
  75. struct sh_mobile_ceu_buffer {
  76. struct videobuf_buffer vb; /* v4l buffer must be first */
  77. const struct soc_camera_data_format *fmt;
  78. };
  79. struct sh_mobile_ceu_dev {
  80. struct device *dev;
  81. struct soc_camera_host ici;
  82. struct soc_camera_device *icd;
  83. unsigned int irq;
  84. void __iomem *base;
  85. struct clk *clk;
  86. unsigned long video_limit;
  87. /* lock used to protect videobuf */
  88. spinlock_t lock;
  89. struct list_head capture;
  90. struct videobuf_buffer *active;
  91. struct sh_mobile_ceu_info *pdata;
  92. };
  93. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  94. unsigned long reg_offs, unsigned long data)
  95. {
  96. iowrite32(data, priv->base + reg_offs);
  97. }
  98. static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
  99. unsigned long reg_offs)
  100. {
  101. return ioread32(priv->base + reg_offs);
  102. }
  103. /*
  104. * Videobuf operations
  105. */
  106. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  107. unsigned int *count,
  108. unsigned int *size)
  109. {
  110. struct soc_camera_device *icd = vq->priv_data;
  111. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  112. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  113. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  114. *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
  115. if (0 == *count)
  116. *count = 2;
  117. if (pcdev->video_limit) {
  118. while (*size * *count > pcdev->video_limit)
  119. (*count)--;
  120. }
  121. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  122. return 0;
  123. }
  124. static void free_buffer(struct videobuf_queue *vq,
  125. struct sh_mobile_ceu_buffer *buf)
  126. {
  127. struct soc_camera_device *icd = vq->priv_data;
  128. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  129. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  130. if (in_interrupt())
  131. BUG();
  132. videobuf_dma_contig_free(vq, &buf->vb);
  133. dev_dbg(&icd->dev, "%s freed\n", __func__);
  134. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  135. }
  136. static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  137. {
  138. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
  139. ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
  140. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
  141. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
  142. ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
  143. if (pcdev->active) {
  144. pcdev->active->state = VIDEOBUF_ACTIVE;
  145. ceu_write(pcdev, CDAYR, videobuf_to_dma_contig(pcdev->active));
  146. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  147. }
  148. }
  149. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  150. struct videobuf_buffer *vb,
  151. enum v4l2_field field)
  152. {
  153. struct soc_camera_device *icd = vq->priv_data;
  154. struct sh_mobile_ceu_buffer *buf;
  155. int ret;
  156. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  157. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  158. vb, vb->baddr, vb->bsize);
  159. /* Added list head initialization on alloc */
  160. WARN_ON(!list_empty(&vb->queue));
  161. #ifdef DEBUG
  162. /* This can be useful if you want to see if we actually fill
  163. * the buffer with something */
  164. memset((void *)vb->baddr, 0xaa, vb->bsize);
  165. #endif
  166. BUG_ON(NULL == icd->current_fmt);
  167. if (buf->fmt != icd->current_fmt ||
  168. vb->width != icd->width ||
  169. vb->height != icd->height ||
  170. vb->field != field) {
  171. buf->fmt = icd->current_fmt;
  172. vb->width = icd->width;
  173. vb->height = icd->height;
  174. vb->field = field;
  175. vb->state = VIDEOBUF_NEEDS_INIT;
  176. }
  177. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  178. if (0 != vb->baddr && vb->bsize < vb->size) {
  179. ret = -EINVAL;
  180. goto out;
  181. }
  182. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  183. ret = videobuf_iolock(vq, vb, NULL);
  184. if (ret)
  185. goto fail;
  186. vb->state = VIDEOBUF_PREPARED;
  187. }
  188. return 0;
  189. fail:
  190. free_buffer(vq, buf);
  191. out:
  192. return ret;
  193. }
  194. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  195. struct videobuf_buffer *vb)
  196. {
  197. struct soc_camera_device *icd = vq->priv_data;
  198. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  199. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  200. unsigned long flags;
  201. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  202. vb, vb->baddr, vb->bsize);
  203. vb->state = VIDEOBUF_QUEUED;
  204. spin_lock_irqsave(&pcdev->lock, flags);
  205. list_add_tail(&vb->queue, &pcdev->capture);
  206. if (!pcdev->active) {
  207. pcdev->active = vb;
  208. sh_mobile_ceu_capture(pcdev);
  209. }
  210. spin_unlock_irqrestore(&pcdev->lock, flags);
  211. }
  212. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  213. struct videobuf_buffer *vb)
  214. {
  215. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  216. }
  217. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  218. .buf_setup = sh_mobile_ceu_videobuf_setup,
  219. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  220. .buf_queue = sh_mobile_ceu_videobuf_queue,
  221. .buf_release = sh_mobile_ceu_videobuf_release,
  222. };
  223. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  224. {
  225. struct sh_mobile_ceu_dev *pcdev = data;
  226. struct videobuf_buffer *vb;
  227. unsigned long flags;
  228. spin_lock_irqsave(&pcdev->lock, flags);
  229. vb = pcdev->active;
  230. list_del_init(&vb->queue);
  231. if (!list_empty(&pcdev->capture))
  232. pcdev->active = list_entry(pcdev->capture.next,
  233. struct videobuf_buffer, queue);
  234. else
  235. pcdev->active = NULL;
  236. sh_mobile_ceu_capture(pcdev);
  237. vb->state = VIDEOBUF_DONE;
  238. do_gettimeofday(&vb->ts);
  239. vb->field_count++;
  240. wake_up(&vb->done);
  241. spin_unlock_irqrestore(&pcdev->lock, flags);
  242. return IRQ_HANDLED;
  243. }
  244. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  245. {
  246. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  247. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  248. int ret = -EBUSY;
  249. mutex_lock(&camera_lock);
  250. if (pcdev->icd)
  251. goto err;
  252. dev_info(&icd->dev,
  253. "SuperH Mobile CEU driver attached to camera %d\n",
  254. icd->devnum);
  255. ret = icd->ops->init(icd);
  256. if (ret)
  257. goto err;
  258. clk_enable(pcdev->clk);
  259. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  260. while (ceu_read(pcdev, CSTSR) & 1)
  261. msleep(1);
  262. pcdev->icd = icd;
  263. err:
  264. mutex_unlock(&camera_lock);
  265. return ret;
  266. }
  267. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  268. {
  269. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  270. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  271. unsigned long flags;
  272. BUG_ON(icd != pcdev->icd);
  273. /* disable capture, disable interrupts */
  274. ceu_write(pcdev, CEIER, 0);
  275. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  276. /* make sure active buffer is canceled */
  277. spin_lock_irqsave(&pcdev->lock, flags);
  278. if (pcdev->active) {
  279. list_del(&pcdev->active->queue);
  280. pcdev->active->state = VIDEOBUF_ERROR;
  281. wake_up_all(&pcdev->active->done);
  282. pcdev->active = NULL;
  283. }
  284. spin_unlock_irqrestore(&pcdev->lock, flags);
  285. clk_disable(pcdev->clk);
  286. icd->ops->release(icd);
  287. dev_info(&icd->dev,
  288. "SuperH Mobile CEU driver detached from camera %d\n",
  289. icd->devnum);
  290. pcdev->icd = NULL;
  291. }
  292. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  293. __u32 pixfmt)
  294. {
  295. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  296. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  297. int ret, buswidth, width, cfszr_width, cdwdr_width;
  298. unsigned long camera_flags, common_flags, value;
  299. camera_flags = icd->ops->query_bus_param(icd);
  300. common_flags = soc_camera_bus_param_compatible(camera_flags,
  301. pcdev->pdata->flags);
  302. if (!common_flags)
  303. return -EINVAL;
  304. ret = icd->ops->set_bus_param(icd, common_flags);
  305. if (ret < 0)
  306. return ret;
  307. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  308. case SOCAM_DATAWIDTH_8:
  309. buswidth = 8;
  310. break;
  311. case SOCAM_DATAWIDTH_16:
  312. buswidth = 16;
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. ceu_write(pcdev, CRCNTR, 0);
  318. ceu_write(pcdev, CRCMPR, 0);
  319. value = 0x00000010;
  320. value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
  321. value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
  322. value |= (buswidth == 16) ? (1 << 12) : 0;
  323. ceu_write(pcdev, CAMCR, value);
  324. ceu_write(pcdev, CAPCR, 0x00300000);
  325. ceu_write(pcdev, CAIFR, 0);
  326. mdelay(1);
  327. width = icd->width * (icd->current_fmt->depth / 8);
  328. width = (buswidth == 16) ? width / 2 : width;
  329. cfszr_width = (buswidth == 8) ? width / 2 : width;
  330. cdwdr_width = (buswidth == 16) ? width * 2 : width;
  331. ceu_write(pcdev, CAMOR, 0);
  332. ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
  333. ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */
  334. ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
  335. ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */
  336. /* A few words about byte order (observed in Big Endian mode)
  337. *
  338. * In data fetch mode bytes are received in chunks of 8 bytes.
  339. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  340. *
  341. * The data is however by default written to memory in reverse order:
  342. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  343. *
  344. * The lowest three bits of CDOCR allows us to do swapping,
  345. * using 7 we swap the data bytes to match the incoming order:
  346. * D0, D1, D2, D3, D4, D5, D6, D7
  347. */
  348. ceu_write(pcdev, CDOCR, 0x00000017);
  349. ceu_write(pcdev, CDWDR, cdwdr_width);
  350. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  351. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  352. /* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
  353. return 0;
  354. }
  355. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
  356. __u32 pixfmt)
  357. {
  358. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  359. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  360. unsigned long camera_flags, common_flags;
  361. camera_flags = icd->ops->query_bus_param(icd);
  362. common_flags = soc_camera_bus_param_compatible(camera_flags,
  363. pcdev->pdata->flags);
  364. if (!common_flags)
  365. return -EINVAL;
  366. return 0;
  367. }
  368. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  369. __u32 pixfmt, struct v4l2_rect *rect)
  370. {
  371. const struct soc_camera_data_format *cam_fmt = NULL;
  372. int ret;
  373. /*
  374. * TODO: find a suitable supported by the SoC output format, check
  375. * whether the sensor supports one of acceptable input formats.
  376. */
  377. if (pixfmt) {
  378. cam_fmt = soc_camera_format_by_fourcc(icd, pixfmt);
  379. if (!cam_fmt)
  380. return -EINVAL;
  381. }
  382. ret = icd->ops->set_fmt(icd, pixfmt, rect);
  383. if (pixfmt && !ret)
  384. icd->current_fmt = cam_fmt;
  385. return ret;
  386. }
  387. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  388. struct v4l2_format *f)
  389. {
  390. const struct soc_camera_data_format *cam_fmt;
  391. int ret = sh_mobile_ceu_try_bus_param(icd, f->fmt.pix.pixelformat);
  392. if (ret < 0)
  393. return ret;
  394. /*
  395. * TODO: find a suitable supported by the SoC output format, check
  396. * whether the sensor supports one of acceptable input formats.
  397. */
  398. cam_fmt = soc_camera_format_by_fourcc(icd, f->fmt.pix.pixelformat);
  399. if (!cam_fmt)
  400. return -EINVAL;
  401. /* FIXME: calculate using depth and bus width */
  402. if (f->fmt.pix.height < 4)
  403. f->fmt.pix.height = 4;
  404. if (f->fmt.pix.height > 1920)
  405. f->fmt.pix.height = 1920;
  406. if (f->fmt.pix.width < 2)
  407. f->fmt.pix.width = 2;
  408. if (f->fmt.pix.width > 2560)
  409. f->fmt.pix.width = 2560;
  410. f->fmt.pix.width &= ~0x01;
  411. f->fmt.pix.height &= ~0x03;
  412. f->fmt.pix.bytesperline = f->fmt.pix.width *
  413. DIV_ROUND_UP(cam_fmt->depth, 8);
  414. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  415. /* limit to sensor capabilities */
  416. return icd->ops->try_fmt(icd, f);
  417. }
  418. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  419. struct v4l2_requestbuffers *p)
  420. {
  421. int i;
  422. /* This is for locking debugging only. I removed spinlocks and now I
  423. * check whether .prepare is ever called on a linked buffer, or whether
  424. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  425. * it hadn't triggered */
  426. for (i = 0; i < p->count; i++) {
  427. struct sh_mobile_ceu_buffer *buf;
  428. buf = container_of(icf->vb_vidq.bufs[i],
  429. struct sh_mobile_ceu_buffer, vb);
  430. INIT_LIST_HEAD(&buf->vb.queue);
  431. }
  432. return 0;
  433. }
  434. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  435. {
  436. struct soc_camera_file *icf = file->private_data;
  437. struct sh_mobile_ceu_buffer *buf;
  438. buf = list_entry(icf->vb_vidq.stream.next,
  439. struct sh_mobile_ceu_buffer, vb.stream);
  440. poll_wait(file, &buf->vb.done, pt);
  441. if (buf->vb.state == VIDEOBUF_DONE ||
  442. buf->vb.state == VIDEOBUF_ERROR)
  443. return POLLIN|POLLRDNORM;
  444. return 0;
  445. }
  446. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  447. struct v4l2_capability *cap)
  448. {
  449. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  450. cap->version = KERNEL_VERSION(0, 0, 5);
  451. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  452. return 0;
  453. }
  454. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  455. struct soc_camera_device *icd)
  456. {
  457. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  458. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  459. videobuf_queue_dma_contig_init(q,
  460. &sh_mobile_ceu_videobuf_ops,
  461. &ici->dev, &pcdev->lock,
  462. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  463. V4L2_FIELD_NONE,
  464. sizeof(struct sh_mobile_ceu_buffer),
  465. icd);
  466. }
  467. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  468. .owner = THIS_MODULE,
  469. .add = sh_mobile_ceu_add_device,
  470. .remove = sh_mobile_ceu_remove_device,
  471. .set_fmt = sh_mobile_ceu_set_fmt,
  472. .try_fmt = sh_mobile_ceu_try_fmt,
  473. .reqbufs = sh_mobile_ceu_reqbufs,
  474. .poll = sh_mobile_ceu_poll,
  475. .querycap = sh_mobile_ceu_querycap,
  476. .set_bus_param = sh_mobile_ceu_set_bus_param,
  477. .init_videobuf = sh_mobile_ceu_init_videobuf,
  478. };
  479. static int sh_mobile_ceu_probe(struct platform_device *pdev)
  480. {
  481. struct sh_mobile_ceu_dev *pcdev;
  482. struct resource *res;
  483. void __iomem *base;
  484. char clk_name[8];
  485. unsigned int irq;
  486. int err = 0;
  487. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  488. irq = platform_get_irq(pdev, 0);
  489. if (!res || !irq) {
  490. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  491. err = -ENODEV;
  492. goto exit;
  493. }
  494. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  495. if (!pcdev) {
  496. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  497. err = -ENOMEM;
  498. goto exit;
  499. }
  500. platform_set_drvdata(pdev, pcdev);
  501. INIT_LIST_HEAD(&pcdev->capture);
  502. spin_lock_init(&pcdev->lock);
  503. pcdev->pdata = pdev->dev.platform_data;
  504. if (!pcdev->pdata) {
  505. err = -EINVAL;
  506. dev_err(&pdev->dev, "CEU platform data not set.\n");
  507. goto exit_kfree;
  508. }
  509. base = ioremap_nocache(res->start, res->end - res->start + 1);
  510. if (!base) {
  511. err = -ENXIO;
  512. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  513. goto exit_kfree;
  514. }
  515. pcdev->irq = irq;
  516. pcdev->base = base;
  517. pcdev->video_limit = 0; /* only enabled if second resource exists */
  518. pcdev->dev = &pdev->dev;
  519. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  520. if (res) {
  521. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  522. res->start,
  523. (res->end - res->start) + 1,
  524. DMA_MEMORY_MAP |
  525. DMA_MEMORY_EXCLUSIVE);
  526. if (!err) {
  527. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  528. err = -ENXIO;
  529. goto exit_iounmap;
  530. }
  531. pcdev->video_limit = (res->end - res->start) + 1;
  532. }
  533. /* request irq */
  534. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  535. dev_name(&pdev->dev), pcdev);
  536. if (err) {
  537. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  538. goto exit_release_mem;
  539. }
  540. snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
  541. pcdev->clk = clk_get(&pdev->dev, clk_name);
  542. if (IS_ERR(pcdev->clk)) {
  543. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  544. err = PTR_ERR(pcdev->clk);
  545. goto exit_free_irq;
  546. }
  547. pcdev->ici.priv = pcdev;
  548. pcdev->ici.dev.parent = &pdev->dev;
  549. pcdev->ici.nr = pdev->id;
  550. pcdev->ici.drv_name = dev_name(&pdev->dev);
  551. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  552. err = soc_camera_host_register(&pcdev->ici);
  553. if (err)
  554. goto exit_free_clk;
  555. return 0;
  556. exit_free_clk:
  557. clk_put(pcdev->clk);
  558. exit_free_irq:
  559. free_irq(pcdev->irq, pcdev);
  560. exit_release_mem:
  561. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  562. dma_release_declared_memory(&pdev->dev);
  563. exit_iounmap:
  564. iounmap(base);
  565. exit_kfree:
  566. kfree(pcdev);
  567. exit:
  568. return err;
  569. }
  570. static int sh_mobile_ceu_remove(struct platform_device *pdev)
  571. {
  572. struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
  573. soc_camera_host_unregister(&pcdev->ici);
  574. clk_put(pcdev->clk);
  575. free_irq(pcdev->irq, pcdev);
  576. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  577. dma_release_declared_memory(&pdev->dev);
  578. iounmap(pcdev->base);
  579. kfree(pcdev);
  580. return 0;
  581. }
  582. static struct platform_driver sh_mobile_ceu_driver = {
  583. .driver = {
  584. .name = "sh_mobile_ceu",
  585. },
  586. .probe = sh_mobile_ceu_probe,
  587. .remove = sh_mobile_ceu_remove,
  588. };
  589. static int __init sh_mobile_ceu_init(void)
  590. {
  591. return platform_driver_register(&sh_mobile_ceu_driver);
  592. }
  593. static void __exit sh_mobile_ceu_exit(void)
  594. {
  595. platform_driver_unregister(&sh_mobile_ceu_driver);
  596. }
  597. module_init(sh_mobile_ceu_init);
  598. module_exit(sh_mobile_ceu_exit);
  599. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  600. MODULE_AUTHOR("Magnus Damm");
  601. MODULE_LICENSE("GPL");