sh_mobile_ceu_camera.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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/videodev2.h>
  32. #include <linux/pm_runtime.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. /* per video frame buffer */
  73. struct sh_mobile_ceu_buffer {
  74. struct videobuf_buffer vb; /* v4l buffer must be first */
  75. const struct soc_camera_data_format *fmt;
  76. };
  77. struct sh_mobile_ceu_dev {
  78. struct soc_camera_host ici;
  79. struct soc_camera_device *icd;
  80. unsigned int irq;
  81. void __iomem *base;
  82. unsigned long video_limit;
  83. /* lock used to protect videobuf */
  84. spinlock_t lock;
  85. struct list_head capture;
  86. struct videobuf_buffer *active;
  87. struct sh_mobile_ceu_info *pdata;
  88. unsigned int is_interlaced:1;
  89. unsigned int image_mode:1;
  90. unsigned int is_16bit:1;
  91. };
  92. struct sh_mobile_ceu_cam {
  93. struct v4l2_rect camera_rect;
  94. const struct soc_camera_data_format *extra_fmt;
  95. const struct soc_camera_data_format *camera_fmt;
  96. };
  97. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  98. {
  99. unsigned long flags;
  100. flags = SOCAM_MASTER |
  101. SOCAM_PCLK_SAMPLE_RISING |
  102. SOCAM_HSYNC_ACTIVE_HIGH |
  103. SOCAM_HSYNC_ACTIVE_LOW |
  104. SOCAM_VSYNC_ACTIVE_HIGH |
  105. SOCAM_VSYNC_ACTIVE_LOW |
  106. SOCAM_DATA_ACTIVE_HIGH;
  107. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  108. flags |= SOCAM_DATAWIDTH_8;
  109. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  110. flags |= SOCAM_DATAWIDTH_16;
  111. if (flags & SOCAM_DATAWIDTH_MASK)
  112. return flags;
  113. return 0;
  114. }
  115. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  116. unsigned long reg_offs, u32 data)
  117. {
  118. iowrite32(data, priv->base + reg_offs);
  119. }
  120. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  121. {
  122. return ioread32(priv->base + reg_offs);
  123. }
  124. /*
  125. * Videobuf operations
  126. */
  127. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  128. unsigned int *count,
  129. unsigned int *size)
  130. {
  131. struct soc_camera_device *icd = vq->priv_data;
  132. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  133. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  134. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  135. *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height *
  136. bytes_per_pixel);
  137. if (0 == *count)
  138. *count = 2;
  139. if (pcdev->video_limit) {
  140. while (*size * *count > pcdev->video_limit)
  141. (*count)--;
  142. }
  143. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  144. return 0;
  145. }
  146. static void free_buffer(struct videobuf_queue *vq,
  147. struct sh_mobile_ceu_buffer *buf)
  148. {
  149. struct soc_camera_device *icd = vq->priv_data;
  150. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  151. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  152. if (in_interrupt())
  153. BUG();
  154. videobuf_waiton(&buf->vb, 0, 0);
  155. videobuf_dma_contig_free(vq, &buf->vb);
  156. dev_dbg(&icd->dev, "%s freed\n", __func__);
  157. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  158. }
  159. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  160. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  161. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  162. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  163. static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  164. {
  165. struct soc_camera_device *icd = pcdev->icd;
  166. dma_addr_t phys_addr_top, phys_addr_bottom;
  167. /* The hardware is _very_ picky about this sequence. Especially
  168. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  169. * several not-so-well documented interrupt sources in CETCR.
  170. */
  171. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
  172. ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
  173. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
  174. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  175. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  176. if (!pcdev->active)
  177. return;
  178. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  179. ceu_write(pcdev, CDAYR, phys_addr_top);
  180. if (pcdev->is_interlaced) {
  181. phys_addr_bottom = phys_addr_top + icd->rect_current.width;
  182. ceu_write(pcdev, CDBYR, phys_addr_bottom);
  183. }
  184. switch (icd->current_fmt->fourcc) {
  185. case V4L2_PIX_FMT_NV12:
  186. case V4L2_PIX_FMT_NV21:
  187. case V4L2_PIX_FMT_NV16:
  188. case V4L2_PIX_FMT_NV61:
  189. phys_addr_top += icd->rect_current.width *
  190. icd->rect_current.height;
  191. ceu_write(pcdev, CDACR, phys_addr_top);
  192. if (pcdev->is_interlaced) {
  193. phys_addr_bottom = phys_addr_top +
  194. icd->rect_current.width;
  195. ceu_write(pcdev, CDBCR, phys_addr_bottom);
  196. }
  197. }
  198. pcdev->active->state = VIDEOBUF_ACTIVE;
  199. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  200. }
  201. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  202. struct videobuf_buffer *vb,
  203. enum v4l2_field field)
  204. {
  205. struct soc_camera_device *icd = vq->priv_data;
  206. struct sh_mobile_ceu_buffer *buf;
  207. int ret;
  208. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  209. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  210. vb, vb->baddr, vb->bsize);
  211. /* Added list head initialization on alloc */
  212. WARN_ON(!list_empty(&vb->queue));
  213. #ifdef DEBUG
  214. /* This can be useful if you want to see if we actually fill
  215. * the buffer with something */
  216. memset((void *)vb->baddr, 0xaa, vb->bsize);
  217. #endif
  218. BUG_ON(NULL == icd->current_fmt);
  219. if (buf->fmt != icd->current_fmt ||
  220. vb->width != icd->rect_current.width ||
  221. vb->height != icd->rect_current.height ||
  222. vb->field != field) {
  223. buf->fmt = icd->current_fmt;
  224. vb->width = icd->rect_current.width;
  225. vb->height = icd->rect_current.height;
  226. vb->field = field;
  227. vb->state = VIDEOBUF_NEEDS_INIT;
  228. }
  229. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  230. if (0 != vb->baddr && vb->bsize < vb->size) {
  231. ret = -EINVAL;
  232. goto out;
  233. }
  234. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  235. ret = videobuf_iolock(vq, vb, NULL);
  236. if (ret)
  237. goto fail;
  238. vb->state = VIDEOBUF_PREPARED;
  239. }
  240. return 0;
  241. fail:
  242. free_buffer(vq, buf);
  243. out:
  244. return ret;
  245. }
  246. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  247. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  248. struct videobuf_buffer *vb)
  249. {
  250. struct soc_camera_device *icd = vq->priv_data;
  251. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  252. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  253. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  254. vb, vb->baddr, vb->bsize);
  255. vb->state = VIDEOBUF_QUEUED;
  256. list_add_tail(&vb->queue, &pcdev->capture);
  257. if (!pcdev->active) {
  258. pcdev->active = vb;
  259. sh_mobile_ceu_capture(pcdev);
  260. }
  261. }
  262. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  263. struct videobuf_buffer *vb)
  264. {
  265. struct soc_camera_device *icd = vq->priv_data;
  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. spin_lock_irqsave(&pcdev->lock, flags);
  270. if (pcdev->active == vb) {
  271. /* disable capture (release DMA buffer), reset */
  272. ceu_write(pcdev, CAPSR, 1 << 16);
  273. pcdev->active = NULL;
  274. }
  275. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
  276. !list_empty(&vb->queue)) {
  277. vb->state = VIDEOBUF_ERROR;
  278. list_del_init(&vb->queue);
  279. }
  280. spin_unlock_irqrestore(&pcdev->lock, flags);
  281. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  282. }
  283. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  284. .buf_setup = sh_mobile_ceu_videobuf_setup,
  285. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  286. .buf_queue = sh_mobile_ceu_videobuf_queue,
  287. .buf_release = sh_mobile_ceu_videobuf_release,
  288. };
  289. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  290. {
  291. struct sh_mobile_ceu_dev *pcdev = data;
  292. struct videobuf_buffer *vb;
  293. unsigned long flags;
  294. spin_lock_irqsave(&pcdev->lock, flags);
  295. vb = pcdev->active;
  296. if (!vb)
  297. /* Stale interrupt from a released buffer */
  298. goto out;
  299. list_del_init(&vb->queue);
  300. if (!list_empty(&pcdev->capture))
  301. pcdev->active = list_entry(pcdev->capture.next,
  302. struct videobuf_buffer, queue);
  303. else
  304. pcdev->active = NULL;
  305. sh_mobile_ceu_capture(pcdev);
  306. vb->state = VIDEOBUF_DONE;
  307. do_gettimeofday(&vb->ts);
  308. vb->field_count++;
  309. wake_up(&vb->done);
  310. out:
  311. spin_unlock_irqrestore(&pcdev->lock, flags);
  312. return IRQ_HANDLED;
  313. }
  314. /* Called with .video_lock held */
  315. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  316. {
  317. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  318. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  319. if (pcdev->icd)
  320. return -EBUSY;
  321. dev_info(&icd->dev,
  322. "SuperH Mobile CEU driver attached to camera %d\n",
  323. icd->devnum);
  324. clk_enable(pcdev->clk);
  325. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  326. while (ceu_read(pcdev, CSTSR) & 1)
  327. msleep(1);
  328. pcdev->icd = icd;
  329. return 0;
  330. }
  331. /* Called with .video_lock held */
  332. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  333. {
  334. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  335. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  336. unsigned long flags;
  337. BUG_ON(icd != pcdev->icd);
  338. /* disable capture, disable interrupts */
  339. ceu_write(pcdev, CEIER, 0);
  340. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  341. /* make sure active buffer is canceled */
  342. spin_lock_irqsave(&pcdev->lock, flags);
  343. if (pcdev->active) {
  344. list_del(&pcdev->active->queue);
  345. pcdev->active->state = VIDEOBUF_ERROR;
  346. wake_up_all(&pcdev->active->done);
  347. pcdev->active = NULL;
  348. }
  349. spin_unlock_irqrestore(&pcdev->lock, flags);
  350. clk_disable(pcdev->clk);
  351. dev_info(&icd->dev,
  352. "SuperH Mobile CEU driver detached from camera %d\n",
  353. icd->devnum);
  354. pcdev->icd = NULL;
  355. }
  356. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
  357. struct v4l2_rect *rect)
  358. {
  359. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  360. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  361. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  362. int width, height, cfszr_width, cdwdr_width;
  363. unsigned int left_offset, top_offset;
  364. u32 camor;
  365. if (rect->left > cam->camera_rect.left) {
  366. left_offset = rect->left - cam->camera_rect.left;
  367. } else {
  368. left_offset = 0;
  369. rect->left = cam->camera_rect.left;
  370. }
  371. if (rect->top > cam->camera_rect.top) {
  372. top_offset = rect->top - cam->camera_rect.top;
  373. } else {
  374. top_offset = 0;
  375. rect->top = cam->camera_rect.top;
  376. }
  377. dev_dbg(&icd->dev, "Offsets %u:%u\n", left_offset, top_offset);
  378. if (pcdev->image_mode) {
  379. width = rect->width;
  380. if (!pcdev->is_16bit)
  381. width *= 2;
  382. cfszr_width = cdwdr_width = rect->width;
  383. } else {
  384. width = rect->width *
  385. ((icd->current_fmt->depth + 7) >> 3);
  386. width = pcdev->is_16bit ? width / 2 : width;
  387. cfszr_width = pcdev->is_16bit ? width : width / 2;
  388. cdwdr_width = pcdev->is_16bit ? width * 2 : width;
  389. }
  390. height = rect->height;
  391. if (pcdev->is_interlaced) {
  392. height /= 2;
  393. cdwdr_width *= 2;
  394. }
  395. camor = left_offset | (top_offset << 16);
  396. ceu_write(pcdev, CAMOR, camor);
  397. ceu_write(pcdev, CAPWR, (height << 16) | width);
  398. ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
  399. ceu_write(pcdev, CDWDR, cdwdr_width);
  400. }
  401. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  402. {
  403. u32 capsr = ceu_read(pcdev, CAPSR);
  404. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  405. return capsr;
  406. }
  407. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  408. {
  409. unsigned long timeout = jiffies + 10 * HZ;
  410. /*
  411. * Wait until the end of the current frame. It can take a long time,
  412. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  413. */
  414. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  415. msleep(1);
  416. if (time_after(jiffies, timeout)) {
  417. dev_err(pcdev->ici.v4l2_dev.dev,
  418. "Timeout waiting for frame end! Interface problem?\n");
  419. return;
  420. }
  421. /* Wait until reset clears, this shall not hang... */
  422. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  423. udelay(10);
  424. /* Anything to restore? */
  425. if (capsr & ~(1 << 16))
  426. ceu_write(pcdev, CAPSR, capsr);
  427. }
  428. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  429. __u32 pixfmt)
  430. {
  431. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  432. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  433. int ret;
  434. unsigned long camera_flags, common_flags, value;
  435. int yuv_lineskip;
  436. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  437. u32 capsr = capture_save_reset(pcdev);
  438. camera_flags = icd->ops->query_bus_param(icd);
  439. common_flags = soc_camera_bus_param_compatible(camera_flags,
  440. make_bus_param(pcdev));
  441. if (!common_flags)
  442. return -EINVAL;
  443. ret = icd->ops->set_bus_param(icd, common_flags);
  444. if (ret < 0)
  445. return ret;
  446. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  447. case SOCAM_DATAWIDTH_8:
  448. pcdev->is_16bit = 0;
  449. break;
  450. case SOCAM_DATAWIDTH_16:
  451. pcdev->is_16bit = 1;
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. ceu_write(pcdev, CRCNTR, 0);
  457. ceu_write(pcdev, CRCMPR, 0);
  458. value = 0x00000010; /* data fetch by default */
  459. pcdev->image_mode = yuv_lineskip = 0;
  460. switch (icd->current_fmt->fourcc) {
  461. case V4L2_PIX_FMT_NV12:
  462. case V4L2_PIX_FMT_NV21:
  463. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  464. /* fall-through */
  465. case V4L2_PIX_FMT_NV16:
  466. case V4L2_PIX_FMT_NV61:
  467. pcdev->image_mode = 1;
  468. switch (cam->camera_fmt->fourcc) {
  469. case V4L2_PIX_FMT_UYVY:
  470. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  471. break;
  472. case V4L2_PIX_FMT_VYUY:
  473. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  474. break;
  475. case V4L2_PIX_FMT_YUYV:
  476. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  477. break;
  478. case V4L2_PIX_FMT_YVYU:
  479. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  480. break;
  481. default:
  482. BUG();
  483. }
  484. }
  485. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  486. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  487. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  488. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  489. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  490. value |= pcdev->is_16bit ? 1 << 12 : 0;
  491. ceu_write(pcdev, CAMCR, value);
  492. ceu_write(pcdev, CAPCR, 0x00300000);
  493. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  494. mdelay(1);
  495. sh_mobile_ceu_set_rect(icd, &icd->rect_current);
  496. ceu_write(pcdev, CFLCR, 0); /* no scaling */
  497. /* A few words about byte order (observed in Big Endian mode)
  498. *
  499. * In data fetch mode bytes are received in chunks of 8 bytes.
  500. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  501. *
  502. * The data is however by default written to memory in reverse order:
  503. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  504. *
  505. * The lowest three bits of CDOCR allows us to do swapping,
  506. * using 7 we swap the data bytes to match the incoming order:
  507. * D0, D1, D2, D3, D4, D5, D6, D7
  508. */
  509. value = 0x00000017;
  510. if (yuv_lineskip)
  511. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  512. ceu_write(pcdev, CDOCR, value);
  513. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  514. dev_dbg(&icd->dev, "S_FMT successful for %c%c%c%c %ux%u@%u.%u\n",
  515. pixfmt & 0xff, (pixfmt >> 8) & 0xff,
  516. (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
  517. icd->rect_current.width, icd->rect_current.height,
  518. icd->rect_current.left, icd->rect_current.top);
  519. capture_restore(pcdev, capsr);
  520. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  521. return 0;
  522. }
  523. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  524. {
  525. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  526. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  527. unsigned long camera_flags, common_flags;
  528. camera_flags = icd->ops->query_bus_param(icd);
  529. common_flags = soc_camera_bus_param_compatible(camera_flags,
  530. make_bus_param(pcdev));
  531. if (!common_flags)
  532. return -EINVAL;
  533. return 0;
  534. }
  535. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  536. {
  537. .name = "NV12",
  538. .depth = 12,
  539. .fourcc = V4L2_PIX_FMT_NV12,
  540. .colorspace = V4L2_COLORSPACE_JPEG,
  541. },
  542. {
  543. .name = "NV21",
  544. .depth = 12,
  545. .fourcc = V4L2_PIX_FMT_NV21,
  546. .colorspace = V4L2_COLORSPACE_JPEG,
  547. },
  548. {
  549. .name = "NV16",
  550. .depth = 16,
  551. .fourcc = V4L2_PIX_FMT_NV16,
  552. .colorspace = V4L2_COLORSPACE_JPEG,
  553. },
  554. {
  555. .name = "NV61",
  556. .depth = 16,
  557. .fourcc = V4L2_PIX_FMT_NV61,
  558. .colorspace = V4L2_COLORSPACE_JPEG,
  559. },
  560. };
  561. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  562. struct soc_camera_format_xlate *xlate)
  563. {
  564. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  565. int ret, k, n;
  566. int formats = 0;
  567. struct sh_mobile_ceu_cam *cam;
  568. ret = sh_mobile_ceu_try_bus_param(icd);
  569. if (ret < 0)
  570. return 0;
  571. if (!icd->host_priv) {
  572. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  573. if (!cam)
  574. return -ENOMEM;
  575. icd->host_priv = cam;
  576. } else {
  577. cam = icd->host_priv;
  578. }
  579. /* Beginning of a pass */
  580. if (!idx)
  581. cam->extra_fmt = NULL;
  582. switch (icd->formats[idx].fourcc) {
  583. case V4L2_PIX_FMT_UYVY:
  584. case V4L2_PIX_FMT_VYUY:
  585. case V4L2_PIX_FMT_YUYV:
  586. case V4L2_PIX_FMT_YVYU:
  587. if (cam->extra_fmt)
  588. goto add_single_format;
  589. /*
  590. * Our case is simple so far: for any of the above four camera
  591. * formats we add all our four synthesized NV* formats, so,
  592. * just marking the device with a single flag suffices. If
  593. * the format generation rules are more complex, you would have
  594. * to actually hang your already added / counted formats onto
  595. * the host_priv pointer and check whether the format you're
  596. * going to add now is already there.
  597. */
  598. cam->extra_fmt = (void *)sh_mobile_ceu_formats;
  599. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  600. formats += n;
  601. for (k = 0; xlate && k < n; k++) {
  602. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  603. xlate->cam_fmt = icd->formats + idx;
  604. xlate->buswidth = icd->formats[idx].depth;
  605. xlate++;
  606. dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n",
  607. sh_mobile_ceu_formats[k].name,
  608. icd->formats[idx].name);
  609. }
  610. default:
  611. add_single_format:
  612. /* Generic pass-through */
  613. formats++;
  614. if (xlate) {
  615. xlate->host_fmt = icd->formats + idx;
  616. xlate->cam_fmt = icd->formats + idx;
  617. xlate->buswidth = icd->formats[idx].depth;
  618. xlate++;
  619. dev_dbg(ici->v4l2_dev.dev,
  620. "Providing format %s in pass-through mode\n",
  621. icd->formats[idx].name);
  622. }
  623. }
  624. return formats;
  625. }
  626. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  627. {
  628. kfree(icd->host_priv);
  629. icd->host_priv = NULL;
  630. }
  631. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  632. {
  633. return r1->width < r2->width || r1->height < r2->height;
  634. }
  635. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  636. struct v4l2_rect *rect)
  637. {
  638. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  639. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  640. struct v4l2_rect cam_rect = *rect;
  641. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  642. unsigned short width, height;
  643. u32 capsr;
  644. int ret;
  645. capsr = capture_save_reset(pcdev);
  646. dev_dbg(&icd->dev, "CAPSR %x\n", capsr);
  647. ret = icd->ops->set_crop(icd, &cam_rect);
  648. if (!ret && !memcmp(rect, &cam_rect, sizeof(*rect))) {
  649. dev_dbg(&icd->dev, "Camera S_CROP successful for %ux%u@%u.%u\n",
  650. cam_rect.width, cam_rect.height,
  651. cam_rect.left, cam_rect.top);
  652. goto ceu_set_rect;
  653. }
  654. /* Try to fix cropping, that camera hasn't managed to do */
  655. dev_dbg(&icd->dev, "Fix camera S_CROP %d for %ux%u@%u.%u\n",
  656. ret, cam_rect.width, cam_rect.height,
  657. cam_rect.left, cam_rect.top);
  658. /*
  659. * Popular special case - some cameras can only handle fixed sizes like
  660. * QVGA, VGA,... Take care to avoid infinite loop.
  661. */
  662. width = max(cam_rect.width, 1) * 2;
  663. height = max(cam_rect.height, 1) * 2;
  664. while (!ret && is_smaller(&cam_rect, rect) &&
  665. (icd->rect_max.width >= width &&
  666. icd->rect_max.height >= height)) {
  667. cam_rect.width = width;
  668. cam_rect.height = height;
  669. if (cam_rect.width + cam_rect.left >
  670. icd->rect_max.width + icd->rect_max.left)
  671. cam_rect.left = icd->rect_max.width +
  672. icd->rect_max.left - cam_rect.width;
  673. if (cam_rect.height + cam_rect.top >
  674. icd->rect_max.height + icd->rect_max.top)
  675. cam_rect.top = icd->rect_max.height +
  676. icd->rect_max.top - cam_rect.height;
  677. ret = icd->ops->set_crop(icd, &cam_rect);
  678. dev_dbg(&icd->dev, "Camera S_CROP %d for %ux%u@%u.%u\n",
  679. ret, cam_rect.width, cam_rect.height,
  680. cam_rect.left, cam_rect.top);
  681. width *= 2;
  682. height *= 2;
  683. }
  684. /*
  685. * If the camera failed to configure cropping, it should not modify the
  686. * rectangle
  687. */
  688. if ((ret < 0 && is_smaller(&icd->rect_current, rect)) ||
  689. is_smaller(&cam_rect, rect)) {
  690. /*
  691. * The camera failed to configure a suitable cropping,
  692. * we cannot use the current rectangle, set to max
  693. */
  694. cam_rect = icd->rect_max;
  695. ret = icd->ops->set_crop(icd, &cam_rect);
  696. dev_dbg(&icd->dev, "Camera S_CROP %d for max %ux%u@%u.%u\n",
  697. ret, cam_rect.width, cam_rect.height,
  698. cam_rect.left, cam_rect.top);
  699. if (ret < 0)
  700. /* All failed, hopefully resume current capture */
  701. goto resume_capture;
  702. }
  703. /* We now have a rectangle, larger than requested, let's crop */
  704. /*
  705. * We have to preserve camera rectangle between close() / open(),
  706. * because soc-camera core calls .set_fmt() on each first open() with
  707. * last before last close() _user_ rectangle, which can be different
  708. * from camera rectangle.
  709. */
  710. dev_dbg(&icd->dev, "SH S_CROP from %ux%u@%u.%u to %ux%u@%u.%u\n",
  711. cam_rect.width, cam_rect.height, cam_rect.left, cam_rect.top,
  712. rect->width, rect->height, rect->left, rect->top);
  713. ret = 0;
  714. ceu_set_rect:
  715. cam->camera_rect = cam_rect;
  716. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  717. if (pcdev->active)
  718. capsr |= 1;
  719. sh_mobile_ceu_set_rect(icd, rect);
  720. capture_restore(pcdev, capsr);
  721. resume_capture:
  722. /* Even if only camera cropping succeeded */
  723. return ret;
  724. }
  725. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  726. struct v4l2_format *f)
  727. {
  728. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  729. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  730. struct v4l2_pix_format *pix = &f->fmt.pix;
  731. __u32 pixfmt = pix->pixelformat;
  732. const struct soc_camera_format_xlate *xlate;
  733. int ret;
  734. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  735. if (!xlate) {
  736. dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
  737. return -EINVAL;
  738. }
  739. pix->pixelformat = xlate->cam_fmt->fourcc;
  740. ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f);
  741. pix->pixelformat = pixfmt;
  742. if (!ret) {
  743. icd->buswidth = xlate->buswidth;
  744. icd->current_fmt = xlate->host_fmt;
  745. cam->camera_fmt = xlate->cam_fmt;
  746. cam->camera_rect.width = pix->width;
  747. cam->camera_rect.height = pix->height;
  748. cam->camera_rect.left = icd->rect_current.left;
  749. cam->camera_rect.top = icd->rect_current.top;
  750. }
  751. return ret;
  752. }
  753. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  754. struct v4l2_format *f)
  755. {
  756. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  757. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  758. const struct soc_camera_format_xlate *xlate;
  759. struct v4l2_pix_format *pix = &f->fmt.pix;
  760. __u32 pixfmt = pix->pixelformat;
  761. int ret;
  762. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  763. if (!xlate) {
  764. dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
  765. return -EINVAL;
  766. }
  767. /* FIXME: calculate using depth and bus width */
  768. v4l_bound_align_image(&pix->width, 2, 2560, 1,
  769. &pix->height, 4, 1920, 2, 0);
  770. pix->bytesperline = pix->width *
  771. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  772. pix->sizeimage = pix->height * pix->bytesperline;
  773. pix->pixelformat = xlate->cam_fmt->fourcc;
  774. /* limit to sensor capabilities */
  775. ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f);
  776. pix->pixelformat = pixfmt;
  777. if (ret < 0)
  778. return ret;
  779. switch (pix->field) {
  780. case V4L2_FIELD_INTERLACED:
  781. pcdev->is_interlaced = 1;
  782. break;
  783. case V4L2_FIELD_ANY:
  784. pix->field = V4L2_FIELD_NONE;
  785. /* fall-through */
  786. case V4L2_FIELD_NONE:
  787. pcdev->is_interlaced = 0;
  788. break;
  789. default:
  790. ret = -EINVAL;
  791. break;
  792. }
  793. return ret;
  794. }
  795. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  796. struct v4l2_requestbuffers *p)
  797. {
  798. int i;
  799. /* This is for locking debugging only. I removed spinlocks and now I
  800. * check whether .prepare is ever called on a linked buffer, or whether
  801. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  802. * it hadn't triggered */
  803. for (i = 0; i < p->count; i++) {
  804. struct sh_mobile_ceu_buffer *buf;
  805. buf = container_of(icf->vb_vidq.bufs[i],
  806. struct sh_mobile_ceu_buffer, vb);
  807. INIT_LIST_HEAD(&buf->vb.queue);
  808. }
  809. return 0;
  810. }
  811. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  812. {
  813. struct soc_camera_file *icf = file->private_data;
  814. struct sh_mobile_ceu_buffer *buf;
  815. buf = list_entry(icf->vb_vidq.stream.next,
  816. struct sh_mobile_ceu_buffer, vb.stream);
  817. poll_wait(file, &buf->vb.done, pt);
  818. if (buf->vb.state == VIDEOBUF_DONE ||
  819. buf->vb.state == VIDEOBUF_ERROR)
  820. return POLLIN|POLLRDNORM;
  821. return 0;
  822. }
  823. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  824. struct v4l2_capability *cap)
  825. {
  826. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  827. cap->version = KERNEL_VERSION(0, 0, 5);
  828. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  829. return 0;
  830. }
  831. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  832. struct soc_camera_device *icd)
  833. {
  834. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  835. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  836. videobuf_queue_dma_contig_init(q,
  837. &sh_mobile_ceu_videobuf_ops,
  838. ici->v4l2_dev.dev, &pcdev->lock,
  839. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  840. pcdev->is_interlaced ?
  841. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  842. sizeof(struct sh_mobile_ceu_buffer),
  843. icd);
  844. }
  845. static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
  846. struct v4l2_control *ctrl)
  847. {
  848. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  849. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  850. u32 val;
  851. switch (ctrl->id) {
  852. case V4L2_CID_SHARPNESS:
  853. val = ceu_read(pcdev, CLFCR);
  854. ctrl->value = val ^ 1;
  855. return 0;
  856. }
  857. return -ENOIOCTLCMD;
  858. }
  859. static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
  860. struct v4l2_control *ctrl)
  861. {
  862. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  863. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  864. switch (ctrl->id) {
  865. case V4L2_CID_SHARPNESS:
  866. switch (icd->current_fmt->fourcc) {
  867. case V4L2_PIX_FMT_NV12:
  868. case V4L2_PIX_FMT_NV21:
  869. case V4L2_PIX_FMT_NV16:
  870. case V4L2_PIX_FMT_NV61:
  871. ceu_write(pcdev, CLFCR, !ctrl->value);
  872. return 0;
  873. }
  874. return -EINVAL;
  875. }
  876. return -ENOIOCTLCMD;
  877. }
  878. static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
  879. {
  880. .id = V4L2_CID_SHARPNESS,
  881. .type = V4L2_CTRL_TYPE_BOOLEAN,
  882. .name = "Low-pass filter",
  883. .minimum = 0,
  884. .maximum = 1,
  885. .step = 1,
  886. .default_value = 0,
  887. },
  888. };
  889. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  890. .owner = THIS_MODULE,
  891. .add = sh_mobile_ceu_add_device,
  892. .remove = sh_mobile_ceu_remove_device,
  893. .get_formats = sh_mobile_ceu_get_formats,
  894. .put_formats = sh_mobile_ceu_put_formats,
  895. .set_crop = sh_mobile_ceu_set_crop,
  896. .set_fmt = sh_mobile_ceu_set_fmt,
  897. .try_fmt = sh_mobile_ceu_try_fmt,
  898. .set_ctrl = sh_mobile_ceu_set_ctrl,
  899. .get_ctrl = sh_mobile_ceu_get_ctrl,
  900. .reqbufs = sh_mobile_ceu_reqbufs,
  901. .poll = sh_mobile_ceu_poll,
  902. .querycap = sh_mobile_ceu_querycap,
  903. .set_bus_param = sh_mobile_ceu_set_bus_param,
  904. .init_videobuf = sh_mobile_ceu_init_videobuf,
  905. .controls = sh_mobile_ceu_controls,
  906. .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
  907. };
  908. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  909. {
  910. struct sh_mobile_ceu_dev *pcdev;
  911. struct resource *res;
  912. void __iomem *base;
  913. unsigned int irq;
  914. int err = 0;
  915. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  916. irq = platform_get_irq(pdev, 0);
  917. if (!res || !irq) {
  918. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  919. err = -ENODEV;
  920. goto exit;
  921. }
  922. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  923. if (!pcdev) {
  924. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  925. err = -ENOMEM;
  926. goto exit;
  927. }
  928. INIT_LIST_HEAD(&pcdev->capture);
  929. spin_lock_init(&pcdev->lock);
  930. pcdev->pdata = pdev->dev.platform_data;
  931. if (!pcdev->pdata) {
  932. err = -EINVAL;
  933. dev_err(&pdev->dev, "CEU platform data not set.\n");
  934. goto exit_kfree;
  935. }
  936. base = ioremap_nocache(res->start, resource_size(res));
  937. if (!base) {
  938. err = -ENXIO;
  939. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  940. goto exit_kfree;
  941. }
  942. pcdev->irq = irq;
  943. pcdev->base = base;
  944. pcdev->video_limit = 0; /* only enabled if second resource exists */
  945. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  946. if (res) {
  947. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  948. res->start,
  949. resource_size(res),
  950. DMA_MEMORY_MAP |
  951. DMA_MEMORY_EXCLUSIVE);
  952. if (!err) {
  953. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  954. err = -ENXIO;
  955. goto exit_iounmap;
  956. }
  957. pcdev->video_limit = resource_size(res);
  958. }
  959. /* request irq */
  960. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  961. dev_name(&pdev->dev), pcdev);
  962. if (err) {
  963. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  964. goto exit_release_mem;
  965. }
  966. pm_suspend_ignore_children(&pdev->dev, true);
  967. pm_runtime_enable(&pdev->dev);
  968. pm_runtime_resume(&pdev->dev);
  969. pcdev->ici.priv = pcdev;
  970. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  971. pcdev->ici.nr = pdev->id;
  972. pcdev->ici.drv_name = dev_name(&pdev->dev);
  973. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  974. err = soc_camera_host_register(&pcdev->ici);
  975. if (err)
  976. goto exit_free_irq;
  977. return 0;
  978. exit_free_irq:
  979. free_irq(pcdev->irq, pcdev);
  980. exit_release_mem:
  981. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  982. dma_release_declared_memory(&pdev->dev);
  983. exit_iounmap:
  984. iounmap(base);
  985. exit_kfree:
  986. kfree(pcdev);
  987. exit:
  988. return err;
  989. }
  990. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  991. {
  992. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  993. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  994. struct sh_mobile_ceu_dev, ici);
  995. soc_camera_host_unregister(soc_host);
  996. free_irq(pcdev->irq, pcdev);
  997. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  998. dma_release_declared_memory(&pdev->dev);
  999. iounmap(pcdev->base);
  1000. kfree(pcdev);
  1001. return 0;
  1002. }
  1003. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1004. {
  1005. /* Runtime PM callback shared between ->runtime_suspend()
  1006. * and ->runtime_resume(). Simply returns success.
  1007. *
  1008. * This driver re-initializes all registers after
  1009. * pm_runtime_get_sync() anyway so there is no need
  1010. * to save and restore registers here.
  1011. */
  1012. return 0;
  1013. }
  1014. static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1015. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1016. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1017. };
  1018. static struct platform_driver sh_mobile_ceu_driver = {
  1019. .driver = {
  1020. .name = "sh_mobile_ceu",
  1021. .pm = &sh_mobile_ceu_dev_pm_ops,
  1022. },
  1023. .probe = sh_mobile_ceu_probe,
  1024. .remove = __exit_p(sh_mobile_ceu_remove),
  1025. };
  1026. static int __init sh_mobile_ceu_init(void)
  1027. {
  1028. return platform_driver_register(&sh_mobile_ceu_driver);
  1029. }
  1030. static void __exit sh_mobile_ceu_exit(void)
  1031. {
  1032. platform_driver_unregister(&sh_mobile_ceu_driver);
  1033. }
  1034. module_init(sh_mobile_ceu_init);
  1035. module_exit(sh_mobile_ceu_exit);
  1036. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1037. MODULE_AUTHOR("Magnus Damm");
  1038. MODULE_LICENSE("GPL");
  1039. MODULE_ALIAS("platform:sh_mobile_ceu");