sh_mobile_ceu_camera.c 18 KB

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