sh_mobile_ceu_camera.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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_cap(struct soc_camera_device *icd,
  369. __u32 pixfmt, struct v4l2_rect *rect)
  370. {
  371. return icd->ops->set_fmt_cap(icd, pixfmt, rect);
  372. }
  373. static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device *icd,
  374. struct v4l2_format *f)
  375. {
  376. int ret = sh_mobile_ceu_try_bus_param(icd, f->fmt.pix.pixelformat);
  377. if (ret < 0)
  378. return ret;
  379. /* FIXME: calculate using depth and bus width */
  380. if (f->fmt.pix.height < 4)
  381. f->fmt.pix.height = 4;
  382. if (f->fmt.pix.height > 1920)
  383. f->fmt.pix.height = 1920;
  384. if (f->fmt.pix.width < 2)
  385. f->fmt.pix.width = 2;
  386. if (f->fmt.pix.width > 2560)
  387. f->fmt.pix.width = 2560;
  388. f->fmt.pix.width &= ~0x01;
  389. f->fmt.pix.height &= ~0x03;
  390. /* limit to sensor capabilities */
  391. return icd->ops->try_fmt_cap(icd, f);
  392. }
  393. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  394. struct v4l2_requestbuffers *p)
  395. {
  396. int i;
  397. /* This is for locking debugging only. I removed spinlocks and now I
  398. * check whether .prepare is ever called on a linked buffer, or whether
  399. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  400. * it hadn't triggered */
  401. for (i = 0; i < p->count; i++) {
  402. struct sh_mobile_ceu_buffer *buf;
  403. buf = container_of(icf->vb_vidq.bufs[i],
  404. struct sh_mobile_ceu_buffer, vb);
  405. INIT_LIST_HEAD(&buf->vb.queue);
  406. }
  407. return 0;
  408. }
  409. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  410. {
  411. struct soc_camera_file *icf = file->private_data;
  412. struct sh_mobile_ceu_buffer *buf;
  413. buf = list_entry(icf->vb_vidq.stream.next,
  414. struct sh_mobile_ceu_buffer, vb.stream);
  415. poll_wait(file, &buf->vb.done, pt);
  416. if (buf->vb.state == VIDEOBUF_DONE ||
  417. buf->vb.state == VIDEOBUF_ERROR)
  418. return POLLIN|POLLRDNORM;
  419. return 0;
  420. }
  421. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  422. struct v4l2_capability *cap)
  423. {
  424. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  425. cap->version = KERNEL_VERSION(0, 0, 5);
  426. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  427. return 0;
  428. }
  429. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  430. struct soc_camera_device *icd)
  431. {
  432. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  433. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  434. videobuf_queue_dma_contig_init(q,
  435. &sh_mobile_ceu_videobuf_ops,
  436. &ici->dev, &pcdev->lock,
  437. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  438. V4L2_FIELD_NONE,
  439. sizeof(struct sh_mobile_ceu_buffer),
  440. icd);
  441. }
  442. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  443. .owner = THIS_MODULE,
  444. .add = sh_mobile_ceu_add_device,
  445. .remove = sh_mobile_ceu_remove_device,
  446. .set_fmt_cap = sh_mobile_ceu_set_fmt_cap,
  447. .try_fmt_cap = sh_mobile_ceu_try_fmt_cap,
  448. .reqbufs = sh_mobile_ceu_reqbufs,
  449. .poll = sh_mobile_ceu_poll,
  450. .querycap = sh_mobile_ceu_querycap,
  451. .set_bus_param = sh_mobile_ceu_set_bus_param,
  452. .init_videobuf = sh_mobile_ceu_init_videobuf,
  453. };
  454. static int sh_mobile_ceu_probe(struct platform_device *pdev)
  455. {
  456. struct sh_mobile_ceu_dev *pcdev;
  457. struct resource *res;
  458. void __iomem *base;
  459. char clk_name[8];
  460. unsigned int irq;
  461. int err = 0;
  462. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  463. irq = platform_get_irq(pdev, 0);
  464. if (!res || !irq) {
  465. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  466. err = -ENODEV;
  467. goto exit;
  468. }
  469. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  470. if (!pcdev) {
  471. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  472. err = -ENOMEM;
  473. goto exit;
  474. }
  475. platform_set_drvdata(pdev, pcdev);
  476. INIT_LIST_HEAD(&pcdev->capture);
  477. spin_lock_init(&pcdev->lock);
  478. pcdev->pdata = pdev->dev.platform_data;
  479. if (!pcdev->pdata) {
  480. err = -EINVAL;
  481. dev_err(&pdev->dev, "CEU platform data not set.\n");
  482. goto exit_kfree;
  483. }
  484. base = ioremap_nocache(res->start, res->end - res->start + 1);
  485. if (!base) {
  486. err = -ENXIO;
  487. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  488. goto exit_kfree;
  489. }
  490. pcdev->irq = irq;
  491. pcdev->base = base;
  492. pcdev->video_limit = 0; /* only enabled if second resource exists */
  493. pcdev->dev = &pdev->dev;
  494. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  495. if (res) {
  496. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  497. res->start,
  498. (res->end - res->start) + 1,
  499. DMA_MEMORY_MAP |
  500. DMA_MEMORY_EXCLUSIVE);
  501. if (!err) {
  502. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  503. err = -ENXIO;
  504. goto exit_iounmap;
  505. }
  506. pcdev->video_limit = (res->end - res->start) + 1;
  507. }
  508. /* request irq */
  509. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  510. dev_name(&pdev->dev), pcdev);
  511. if (err) {
  512. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  513. goto exit_release_mem;
  514. }
  515. snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
  516. pcdev->clk = clk_get(&pdev->dev, clk_name);
  517. if (IS_ERR(pcdev->clk)) {
  518. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  519. err = PTR_ERR(pcdev->clk);
  520. goto exit_free_irq;
  521. }
  522. pcdev->ici.priv = pcdev;
  523. pcdev->ici.dev.parent = &pdev->dev;
  524. pcdev->ici.nr = pdev->id;
  525. pcdev->ici.drv_name = dev_name(&pdev->dev);
  526. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  527. err = soc_camera_host_register(&pcdev->ici);
  528. if (err)
  529. goto exit_free_clk;
  530. return 0;
  531. exit_free_clk:
  532. clk_put(pcdev->clk);
  533. exit_free_irq:
  534. free_irq(pcdev->irq, pcdev);
  535. exit_release_mem:
  536. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  537. dma_release_declared_memory(&pdev->dev);
  538. exit_iounmap:
  539. iounmap(base);
  540. exit_kfree:
  541. kfree(pcdev);
  542. exit:
  543. return err;
  544. }
  545. static int sh_mobile_ceu_remove(struct platform_device *pdev)
  546. {
  547. struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
  548. soc_camera_host_unregister(&pcdev->ici);
  549. clk_put(pcdev->clk);
  550. free_irq(pcdev->irq, pcdev);
  551. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  552. dma_release_declared_memory(&pdev->dev);
  553. iounmap(pcdev->base);
  554. kfree(pcdev);
  555. return 0;
  556. }
  557. static struct platform_driver sh_mobile_ceu_driver = {
  558. .driver = {
  559. .name = "sh_mobile_ceu",
  560. },
  561. .probe = sh_mobile_ceu_probe,
  562. .remove = sh_mobile_ceu_remove,
  563. };
  564. static int __init sh_mobile_ceu_init(void)
  565. {
  566. return platform_driver_register(&sh_mobile_ceu_driver);
  567. }
  568. static void __exit sh_mobile_ceu_exit(void)
  569. {
  570. platform_driver_unregister(&sh_mobile_ceu_driver);
  571. }
  572. module_init(sh_mobile_ceu_init);
  573. module_exit(sh_mobile_ceu_exit);
  574. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  575. MODULE_AUTHOR("Magnus Damm");
  576. MODULE_LICENSE("GPL");