sh_mobile_ceu_camera.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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 <linux/sched.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. #undef DEBUG_GEOMETRY
  74. #ifdef DEBUG_GEOMETRY
  75. #define dev_geo dev_info
  76. #else
  77. #define dev_geo dev_dbg
  78. #endif
  79. /* per video frame buffer */
  80. struct sh_mobile_ceu_buffer {
  81. struct videobuf_buffer vb; /* v4l buffer must be first */
  82. const struct soc_camera_data_format *fmt;
  83. };
  84. struct sh_mobile_ceu_dev {
  85. struct soc_camera_host ici;
  86. struct soc_camera_device *icd;
  87. unsigned int irq;
  88. void __iomem *base;
  89. unsigned long video_limit;
  90. /* lock used to protect videobuf */
  91. spinlock_t lock;
  92. struct list_head capture;
  93. struct videobuf_buffer *active;
  94. struct sh_mobile_ceu_info *pdata;
  95. u32 cflcr;
  96. unsigned int is_interlaced:1;
  97. unsigned int image_mode:1;
  98. unsigned int is_16bit:1;
  99. };
  100. struct sh_mobile_ceu_cam {
  101. struct v4l2_rect ceu_rect;
  102. unsigned int cam_width;
  103. unsigned int cam_height;
  104. const struct soc_camera_data_format *extra_fmt;
  105. const struct soc_camera_data_format *camera_fmt;
  106. };
  107. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  108. {
  109. unsigned long flags;
  110. flags = SOCAM_MASTER |
  111. SOCAM_PCLK_SAMPLE_RISING |
  112. SOCAM_HSYNC_ACTIVE_HIGH |
  113. SOCAM_HSYNC_ACTIVE_LOW |
  114. SOCAM_VSYNC_ACTIVE_HIGH |
  115. SOCAM_VSYNC_ACTIVE_LOW |
  116. SOCAM_DATA_ACTIVE_HIGH;
  117. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  118. flags |= SOCAM_DATAWIDTH_8;
  119. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  120. flags |= SOCAM_DATAWIDTH_16;
  121. if (flags & SOCAM_DATAWIDTH_MASK)
  122. return flags;
  123. return 0;
  124. }
  125. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  126. unsigned long reg_offs, u32 data)
  127. {
  128. iowrite32(data, priv->base + reg_offs);
  129. }
  130. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  131. {
  132. return ioread32(priv->base + reg_offs);
  133. }
  134. /*
  135. * Videobuf operations
  136. */
  137. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  138. unsigned int *count,
  139. unsigned int *size)
  140. {
  141. struct soc_camera_device *icd = vq->priv_data;
  142. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  143. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  144. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  145. *size = PAGE_ALIGN(icd->user_width * icd->user_height *
  146. bytes_per_pixel);
  147. if (0 == *count)
  148. *count = 2;
  149. if (pcdev->video_limit) {
  150. while (*size * *count > pcdev->video_limit)
  151. (*count)--;
  152. }
  153. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  154. return 0;
  155. }
  156. static void free_buffer(struct videobuf_queue *vq,
  157. struct sh_mobile_ceu_buffer *buf)
  158. {
  159. struct soc_camera_device *icd = vq->priv_data;
  160. struct device *dev = icd->dev.parent;
  161. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  162. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  163. if (in_interrupt())
  164. BUG();
  165. videobuf_waiton(&buf->vb, 0, 0);
  166. videobuf_dma_contig_free(vq, &buf->vb);
  167. dev_dbg(dev, "%s freed\n", __func__);
  168. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  169. }
  170. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  171. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  172. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  173. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  174. static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  175. {
  176. struct soc_camera_device *icd = pcdev->icd;
  177. dma_addr_t phys_addr_top, phys_addr_bottom;
  178. /* The hardware is _very_ picky about this sequence. Especially
  179. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  180. * several not-so-well documented interrupt sources in CETCR.
  181. */
  182. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
  183. ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
  184. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
  185. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  186. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  187. if (!pcdev->active)
  188. return;
  189. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  190. ceu_write(pcdev, CDAYR, phys_addr_top);
  191. if (pcdev->is_interlaced) {
  192. phys_addr_bottom = phys_addr_top + icd->user_width;
  193. ceu_write(pcdev, CDBYR, phys_addr_bottom);
  194. }
  195. switch (icd->current_fmt->fourcc) {
  196. case V4L2_PIX_FMT_NV12:
  197. case V4L2_PIX_FMT_NV21:
  198. case V4L2_PIX_FMT_NV16:
  199. case V4L2_PIX_FMT_NV61:
  200. phys_addr_top += icd->user_width *
  201. icd->user_height;
  202. ceu_write(pcdev, CDACR, phys_addr_top);
  203. if (pcdev->is_interlaced) {
  204. phys_addr_bottom = phys_addr_top +
  205. icd->user_width;
  206. ceu_write(pcdev, CDBCR, phys_addr_bottom);
  207. }
  208. }
  209. pcdev->active->state = VIDEOBUF_ACTIVE;
  210. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  211. }
  212. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  213. struct videobuf_buffer *vb,
  214. enum v4l2_field field)
  215. {
  216. struct soc_camera_device *icd = vq->priv_data;
  217. struct sh_mobile_ceu_buffer *buf;
  218. int ret;
  219. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  220. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  221. vb, vb->baddr, vb->bsize);
  222. /* Added list head initialization on alloc */
  223. WARN_ON(!list_empty(&vb->queue));
  224. #ifdef DEBUG
  225. /* This can be useful if you want to see if we actually fill
  226. * the buffer with something */
  227. memset((void *)vb->baddr, 0xaa, vb->bsize);
  228. #endif
  229. BUG_ON(NULL == icd->current_fmt);
  230. if (buf->fmt != icd->current_fmt ||
  231. vb->width != icd->user_width ||
  232. vb->height != icd->user_height ||
  233. vb->field != field) {
  234. buf->fmt = icd->current_fmt;
  235. vb->width = icd->user_width;
  236. vb->height = icd->user_height;
  237. vb->field = field;
  238. vb->state = VIDEOBUF_NEEDS_INIT;
  239. }
  240. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  241. if (0 != vb->baddr && vb->bsize < vb->size) {
  242. ret = -EINVAL;
  243. goto out;
  244. }
  245. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  246. ret = videobuf_iolock(vq, vb, NULL);
  247. if (ret)
  248. goto fail;
  249. vb->state = VIDEOBUF_PREPARED;
  250. }
  251. return 0;
  252. fail:
  253. free_buffer(vq, buf);
  254. out:
  255. return ret;
  256. }
  257. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  258. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  259. struct videobuf_buffer *vb)
  260. {
  261. struct soc_camera_device *icd = vq->priv_data;
  262. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  263. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  264. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  265. vb, vb->baddr, vb->bsize);
  266. vb->state = VIDEOBUF_QUEUED;
  267. list_add_tail(&vb->queue, &pcdev->capture);
  268. if (!pcdev->active) {
  269. pcdev->active = vb;
  270. sh_mobile_ceu_capture(pcdev);
  271. }
  272. }
  273. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  274. struct videobuf_buffer *vb)
  275. {
  276. struct soc_camera_device *icd = vq->priv_data;
  277. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  278. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  279. unsigned long flags;
  280. spin_lock_irqsave(&pcdev->lock, flags);
  281. if (pcdev->active == vb) {
  282. /* disable capture (release DMA buffer), reset */
  283. ceu_write(pcdev, CAPSR, 1 << 16);
  284. pcdev->active = NULL;
  285. }
  286. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
  287. !list_empty(&vb->queue)) {
  288. vb->state = VIDEOBUF_ERROR;
  289. list_del_init(&vb->queue);
  290. }
  291. spin_unlock_irqrestore(&pcdev->lock, flags);
  292. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  293. }
  294. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  295. .buf_setup = sh_mobile_ceu_videobuf_setup,
  296. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  297. .buf_queue = sh_mobile_ceu_videobuf_queue,
  298. .buf_release = sh_mobile_ceu_videobuf_release,
  299. };
  300. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  301. {
  302. struct sh_mobile_ceu_dev *pcdev = data;
  303. struct videobuf_buffer *vb;
  304. unsigned long flags;
  305. spin_lock_irqsave(&pcdev->lock, flags);
  306. vb = pcdev->active;
  307. if (!vb)
  308. /* Stale interrupt from a released buffer */
  309. goto out;
  310. list_del_init(&vb->queue);
  311. if (!list_empty(&pcdev->capture))
  312. pcdev->active = list_entry(pcdev->capture.next,
  313. struct videobuf_buffer, queue);
  314. else
  315. pcdev->active = NULL;
  316. sh_mobile_ceu_capture(pcdev);
  317. vb->state = VIDEOBUF_DONE;
  318. do_gettimeofday(&vb->ts);
  319. vb->field_count++;
  320. wake_up(&vb->done);
  321. out:
  322. spin_unlock_irqrestore(&pcdev->lock, flags);
  323. return IRQ_HANDLED;
  324. }
  325. /* Called with .video_lock held */
  326. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  327. {
  328. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  329. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  330. if (pcdev->icd)
  331. return -EBUSY;
  332. dev_info(icd->dev.parent,
  333. "SuperH Mobile CEU driver attached to camera %d\n",
  334. icd->devnum);
  335. pm_runtime_get_sync(ici->v4l2_dev.dev);
  336. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  337. while (ceu_read(pcdev, CSTSR) & 1)
  338. msleep(1);
  339. pcdev->icd = icd;
  340. return 0;
  341. }
  342. /* Called with .video_lock held */
  343. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  344. {
  345. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  346. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  347. unsigned long flags;
  348. BUG_ON(icd != pcdev->icd);
  349. /* disable capture, disable interrupts */
  350. ceu_write(pcdev, CEIER, 0);
  351. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  352. /* make sure active buffer is canceled */
  353. spin_lock_irqsave(&pcdev->lock, flags);
  354. if (pcdev->active) {
  355. list_del(&pcdev->active->queue);
  356. pcdev->active->state = VIDEOBUF_ERROR;
  357. wake_up_all(&pcdev->active->done);
  358. pcdev->active = NULL;
  359. }
  360. spin_unlock_irqrestore(&pcdev->lock, flags);
  361. pm_runtime_put_sync(ici->v4l2_dev.dev);
  362. dev_info(icd->dev.parent,
  363. "SuperH Mobile CEU driver detached from camera %d\n",
  364. icd->devnum);
  365. pcdev->icd = NULL;
  366. }
  367. /*
  368. * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
  369. * in SH7722 Hardware Manual
  370. */
  371. static unsigned int size_dst(unsigned int src, unsigned int scale)
  372. {
  373. unsigned int mant_pre = scale >> 12;
  374. if (!src || !scale)
  375. return src;
  376. return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
  377. mant_pre * 4096 / scale + 1;
  378. }
  379. static u16 calc_scale(unsigned int src, unsigned int *dst)
  380. {
  381. u16 scale;
  382. if (src == *dst)
  383. return 0;
  384. scale = (src * 4096 / *dst) & ~7;
  385. while (scale > 4096 && size_dst(src, scale) < *dst)
  386. scale -= 8;
  387. *dst = size_dst(src, scale);
  388. return scale;
  389. }
  390. /* rect is guaranteed to not exceed the scaled camera rectangle */
  391. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
  392. unsigned int out_width,
  393. unsigned int out_height)
  394. {
  395. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  396. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  397. struct v4l2_rect *rect = &cam->ceu_rect;
  398. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  399. unsigned int height, width, cdwdr_width, in_width, in_height;
  400. unsigned int left_offset, top_offset;
  401. u32 camor;
  402. dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
  403. rect->width, rect->height, rect->left, rect->top);
  404. left_offset = rect->left;
  405. top_offset = rect->top;
  406. if (pcdev->image_mode) {
  407. in_width = rect->width;
  408. if (!pcdev->is_16bit) {
  409. in_width *= 2;
  410. left_offset *= 2;
  411. }
  412. width = cdwdr_width = out_width;
  413. } else {
  414. unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
  415. width = out_width * w_factor / 2;
  416. if (!pcdev->is_16bit)
  417. w_factor *= 2;
  418. in_width = rect->width * w_factor / 2;
  419. left_offset = left_offset * w_factor / 2;
  420. cdwdr_width = width * 2;
  421. }
  422. height = out_height;
  423. in_height = rect->height;
  424. if (pcdev->is_interlaced) {
  425. height /= 2;
  426. in_height /= 2;
  427. top_offset /= 2;
  428. cdwdr_width *= 2;
  429. }
  430. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  431. camor = left_offset | (top_offset << 16);
  432. dev_geo(icd->dev.parent,
  433. "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
  434. (in_height << 16) | in_width, (height << 16) | width,
  435. cdwdr_width);
  436. ceu_write(pcdev, CAMOR, camor);
  437. ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
  438. ceu_write(pcdev, CFSZR, (height << 16) | width);
  439. ceu_write(pcdev, CDWDR, cdwdr_width);
  440. }
  441. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  442. {
  443. u32 capsr = ceu_read(pcdev, CAPSR);
  444. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  445. return capsr;
  446. }
  447. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  448. {
  449. unsigned long timeout = jiffies + 10 * HZ;
  450. /*
  451. * Wait until the end of the current frame. It can take a long time,
  452. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  453. */
  454. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  455. msleep(1);
  456. if (time_after(jiffies, timeout)) {
  457. dev_err(pcdev->ici.v4l2_dev.dev,
  458. "Timeout waiting for frame end! Interface problem?\n");
  459. return;
  460. }
  461. /* Wait until reset clears, this shall not hang... */
  462. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  463. udelay(10);
  464. /* Anything to restore? */
  465. if (capsr & ~(1 << 16))
  466. ceu_write(pcdev, CAPSR, capsr);
  467. }
  468. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  469. __u32 pixfmt)
  470. {
  471. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  472. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  473. int ret;
  474. unsigned long camera_flags, common_flags, value;
  475. int yuv_lineskip;
  476. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  477. u32 capsr = capture_save_reset(pcdev);
  478. camera_flags = icd->ops->query_bus_param(icd);
  479. common_flags = soc_camera_bus_param_compatible(camera_flags,
  480. make_bus_param(pcdev));
  481. if (!common_flags)
  482. return -EINVAL;
  483. ret = icd->ops->set_bus_param(icd, common_flags);
  484. if (ret < 0)
  485. return ret;
  486. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  487. case SOCAM_DATAWIDTH_8:
  488. pcdev->is_16bit = 0;
  489. break;
  490. case SOCAM_DATAWIDTH_16:
  491. pcdev->is_16bit = 1;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. ceu_write(pcdev, CRCNTR, 0);
  497. ceu_write(pcdev, CRCMPR, 0);
  498. value = 0x00000010; /* data fetch by default */
  499. yuv_lineskip = 0;
  500. switch (icd->current_fmt->fourcc) {
  501. case V4L2_PIX_FMT_NV12:
  502. case V4L2_PIX_FMT_NV21:
  503. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  504. /* fall-through */
  505. case V4L2_PIX_FMT_NV16:
  506. case V4L2_PIX_FMT_NV61:
  507. switch (cam->camera_fmt->fourcc) {
  508. case V4L2_PIX_FMT_UYVY:
  509. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  510. break;
  511. case V4L2_PIX_FMT_VYUY:
  512. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  513. break;
  514. case V4L2_PIX_FMT_YUYV:
  515. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  516. break;
  517. case V4L2_PIX_FMT_YVYU:
  518. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  519. break;
  520. default:
  521. BUG();
  522. }
  523. }
  524. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  525. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  526. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  527. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  528. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  529. value |= pcdev->is_16bit ? 1 << 12 : 0;
  530. ceu_write(pcdev, CAMCR, value);
  531. ceu_write(pcdev, CAPCR, 0x00300000);
  532. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  533. sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
  534. mdelay(1);
  535. ceu_write(pcdev, CFLCR, pcdev->cflcr);
  536. /* A few words about byte order (observed in Big Endian mode)
  537. *
  538. * In data fetch mode bytes are received in chunks of 8 bytes.
  539. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  540. *
  541. * The data is however by default written to memory in reverse order:
  542. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  543. *
  544. * The lowest three bits of CDOCR allows us to do swapping,
  545. * using 7 we swap the data bytes to match the incoming order:
  546. * D0, D1, D2, D3, D4, D5, D6, D7
  547. */
  548. value = 0x00000017;
  549. if (yuv_lineskip)
  550. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  551. ceu_write(pcdev, CDOCR, value);
  552. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  553. dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
  554. pixfmt & 0xff, (pixfmt >> 8) & 0xff,
  555. (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
  556. icd->user_width, icd->user_height);
  557. capture_restore(pcdev, capsr);
  558. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  559. return 0;
  560. }
  561. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  562. {
  563. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  564. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  565. unsigned long camera_flags, common_flags;
  566. camera_flags = icd->ops->query_bus_param(icd);
  567. common_flags = soc_camera_bus_param_compatible(camera_flags,
  568. make_bus_param(pcdev));
  569. if (!common_flags)
  570. return -EINVAL;
  571. return 0;
  572. }
  573. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  574. {
  575. .name = "NV12",
  576. .depth = 12,
  577. .fourcc = V4L2_PIX_FMT_NV12,
  578. .colorspace = V4L2_COLORSPACE_JPEG,
  579. },
  580. {
  581. .name = "NV21",
  582. .depth = 12,
  583. .fourcc = V4L2_PIX_FMT_NV21,
  584. .colorspace = V4L2_COLORSPACE_JPEG,
  585. },
  586. {
  587. .name = "NV16",
  588. .depth = 16,
  589. .fourcc = V4L2_PIX_FMT_NV16,
  590. .colorspace = V4L2_COLORSPACE_JPEG,
  591. },
  592. {
  593. .name = "NV61",
  594. .depth = 16,
  595. .fourcc = V4L2_PIX_FMT_NV61,
  596. .colorspace = V4L2_COLORSPACE_JPEG,
  597. },
  598. };
  599. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  600. struct soc_camera_format_xlate *xlate)
  601. {
  602. struct device *dev = icd->dev.parent;
  603. int ret, k, n;
  604. int formats = 0;
  605. struct sh_mobile_ceu_cam *cam;
  606. ret = sh_mobile_ceu_try_bus_param(icd);
  607. if (ret < 0)
  608. return 0;
  609. if (!icd->host_priv) {
  610. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  611. if (!cam)
  612. return -ENOMEM;
  613. icd->host_priv = cam;
  614. } else {
  615. cam = icd->host_priv;
  616. }
  617. /* Beginning of a pass */
  618. if (!idx)
  619. cam->extra_fmt = NULL;
  620. switch (icd->formats[idx].fourcc) {
  621. case V4L2_PIX_FMT_UYVY:
  622. case V4L2_PIX_FMT_VYUY:
  623. case V4L2_PIX_FMT_YUYV:
  624. case V4L2_PIX_FMT_YVYU:
  625. if (cam->extra_fmt)
  626. goto add_single_format;
  627. /*
  628. * Our case is simple so far: for any of the above four camera
  629. * formats we add all our four synthesized NV* formats, so,
  630. * just marking the device with a single flag suffices. If
  631. * the format generation rules are more complex, you would have
  632. * to actually hang your already added / counted formats onto
  633. * the host_priv pointer and check whether the format you're
  634. * going to add now is already there.
  635. */
  636. cam->extra_fmt = (void *)sh_mobile_ceu_formats;
  637. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  638. formats += n;
  639. for (k = 0; xlate && k < n; k++) {
  640. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  641. xlate->cam_fmt = icd->formats + idx;
  642. xlate->buswidth = icd->formats[idx].depth;
  643. xlate++;
  644. dev_dbg(dev, "Providing format %s using %s\n",
  645. sh_mobile_ceu_formats[k].name,
  646. icd->formats[idx].name);
  647. }
  648. default:
  649. add_single_format:
  650. /* Generic pass-through */
  651. formats++;
  652. if (xlate) {
  653. xlate->host_fmt = icd->formats + idx;
  654. xlate->cam_fmt = icd->formats + idx;
  655. xlate->buswidth = icd->formats[idx].depth;
  656. xlate++;
  657. dev_dbg(dev,
  658. "Providing format %s in pass-through mode\n",
  659. icd->formats[idx].name);
  660. }
  661. }
  662. return formats;
  663. }
  664. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  665. {
  666. kfree(icd->host_priv);
  667. icd->host_priv = NULL;
  668. }
  669. /* Check if any dimension of r1 is smaller than respective one of r2 */
  670. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  671. {
  672. return r1->width < r2->width || r1->height < r2->height;
  673. }
  674. /* Check if r1 fails to cover r2 */
  675. static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
  676. {
  677. return r1->left > r2->left || r1->top > r2->top ||
  678. r1->left + r1->width < r2->left + r2->width ||
  679. r1->top + r1->height < r2->top + r2->height;
  680. }
  681. static unsigned int scale_down(unsigned int size, unsigned int scale)
  682. {
  683. return (size * 4096 + scale / 2) / scale;
  684. }
  685. static unsigned int scale_up(unsigned int size, unsigned int scale)
  686. {
  687. return (size * scale + 2048) / 4096;
  688. }
  689. static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
  690. {
  691. return (input * 4096 + output / 2) / output;
  692. }
  693. static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
  694. {
  695. struct v4l2_crop crop;
  696. struct v4l2_cropcap cap;
  697. int ret;
  698. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  699. ret = v4l2_subdev_call(sd, video, g_crop, &crop);
  700. if (!ret) {
  701. *rect = crop.c;
  702. return ret;
  703. }
  704. /* Camera driver doesn't support .g_crop(), assume default rectangle */
  705. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  706. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  707. if (ret < 0)
  708. return ret;
  709. *rect = cap.defrect;
  710. return ret;
  711. }
  712. /*
  713. * The common for both scaling and cropping iterative approach is:
  714. * 1. try if the client can produce exactly what requested by the user
  715. * 2. if (1) failed, try to double the client image until we get one big enough
  716. * 3. if (2) failed, try to request the maximum image
  717. */
  718. static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
  719. struct v4l2_crop *cam_crop)
  720. {
  721. struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
  722. struct device *dev = sd->v4l2_dev->dev;
  723. struct v4l2_cropcap cap;
  724. int ret;
  725. unsigned int width, height;
  726. v4l2_subdev_call(sd, video, s_crop, crop);
  727. ret = client_g_rect(sd, cam_rect);
  728. if (ret < 0)
  729. return ret;
  730. /*
  731. * Now cam_crop contains the current camera input rectangle, and it must
  732. * be within camera cropcap bounds
  733. */
  734. if (!memcmp(rect, cam_rect, sizeof(*rect))) {
  735. /* Even if camera S_CROP failed, but camera rectangle matches */
  736. dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
  737. rect->width, rect->height, rect->left, rect->top);
  738. return 0;
  739. }
  740. /* Try to fix cropping, that camera hasn't managed to set */
  741. dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
  742. cam_rect->width, cam_rect->height,
  743. cam_rect->left, cam_rect->top,
  744. rect->width, rect->height, rect->left, rect->top);
  745. /* We need sensor maximum rectangle */
  746. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  747. if (ret < 0)
  748. return ret;
  749. soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
  750. cap.bounds.width);
  751. soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
  752. cap.bounds.height);
  753. /*
  754. * Popular special case - some cameras can only handle fixed sizes like
  755. * QVGA, VGA,... Take care to avoid infinite loop.
  756. */
  757. width = max(cam_rect->width, 2);
  758. height = max(cam_rect->height, 2);
  759. while (!ret && (is_smaller(cam_rect, rect) ||
  760. is_inside(cam_rect, rect)) &&
  761. (cap.bounds.width > width || cap.bounds.height > height)) {
  762. width *= 2;
  763. height *= 2;
  764. cam_rect->width = width;
  765. cam_rect->height = height;
  766. /*
  767. * We do not know what capabilities the camera has to set up
  768. * left and top borders. We could try to be smarter in iterating
  769. * them, e.g., if camera current left is to the right of the
  770. * target left, set it to the middle point between the current
  771. * left and minimum left. But that would add too much
  772. * complexity: we would have to iterate each border separately.
  773. */
  774. if (cam_rect->left > rect->left)
  775. cam_rect->left = cap.bounds.left;
  776. if (cam_rect->left + cam_rect->width < rect->left + rect->width)
  777. cam_rect->width = rect->left + rect->width -
  778. cam_rect->left;
  779. if (cam_rect->top > rect->top)
  780. cam_rect->top = cap.bounds.top;
  781. if (cam_rect->top + cam_rect->height < rect->top + rect->height)
  782. cam_rect->height = rect->top + rect->height -
  783. cam_rect->top;
  784. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  785. ret = client_g_rect(sd, cam_rect);
  786. dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
  787. cam_rect->width, cam_rect->height,
  788. cam_rect->left, cam_rect->top);
  789. }
  790. /* S_CROP must not modify the rectangle */
  791. if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
  792. /*
  793. * The camera failed to configure a suitable cropping,
  794. * we cannot use the current rectangle, set to max
  795. */
  796. *cam_rect = cap.bounds;
  797. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  798. ret = client_g_rect(sd, cam_rect);
  799. dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
  800. cam_rect->width, cam_rect->height,
  801. cam_rect->left, cam_rect->top);
  802. }
  803. return ret;
  804. }
  805. static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
  806. unsigned int *scale_h, unsigned int *scale_v)
  807. {
  808. struct v4l2_format f;
  809. int ret;
  810. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  811. ret = v4l2_subdev_call(sd, video, g_fmt, &f);
  812. if (ret < 0)
  813. return ret;
  814. *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
  815. *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
  816. return 0;
  817. }
  818. static int get_camera_subwin(struct soc_camera_device *icd,
  819. struct v4l2_rect *cam_subrect,
  820. unsigned int cam_hscale, unsigned int cam_vscale)
  821. {
  822. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  823. struct v4l2_rect *ceu_rect = &cam->ceu_rect;
  824. if (!ceu_rect->width) {
  825. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  826. struct device *dev = icd->dev.parent;
  827. struct v4l2_format f;
  828. struct v4l2_pix_format *pix = &f.fmt.pix;
  829. int ret;
  830. /* First time */
  831. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  832. ret = v4l2_subdev_call(sd, video, g_fmt, &f);
  833. if (ret < 0)
  834. return ret;
  835. dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
  836. if (pix->width > 2560) {
  837. ceu_rect->width = 2560;
  838. ceu_rect->left = (pix->width - 2560) / 2;
  839. } else {
  840. ceu_rect->width = pix->width;
  841. ceu_rect->left = 0;
  842. }
  843. if (pix->height > 1920) {
  844. ceu_rect->height = 1920;
  845. ceu_rect->top = (pix->height - 1920) / 2;
  846. } else {
  847. ceu_rect->height = pix->height;
  848. ceu_rect->top = 0;
  849. }
  850. dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
  851. ceu_rect->width, ceu_rect->height,
  852. ceu_rect->left, ceu_rect->top);
  853. }
  854. cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
  855. cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
  856. cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
  857. cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
  858. return 0;
  859. }
  860. static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
  861. bool ceu_can_scale)
  862. {
  863. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  864. struct device *dev = icd->dev.parent;
  865. struct v4l2_pix_format *pix = &f->fmt.pix;
  866. unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
  867. unsigned int max_width, max_height;
  868. struct v4l2_cropcap cap;
  869. int ret;
  870. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  871. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  872. if (ret < 0)
  873. return ret;
  874. max_width = min(cap.bounds.width, 2560);
  875. max_height = min(cap.bounds.height, 1920);
  876. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  877. if (ret < 0)
  878. return ret;
  879. dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
  880. if ((width == pix->width && height == pix->height) || !ceu_can_scale)
  881. return 0;
  882. /* Camera set a format, but geometry is not precise, try to improve */
  883. tmp_w = pix->width;
  884. tmp_h = pix->height;
  885. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  886. while ((width > tmp_w || height > tmp_h) &&
  887. tmp_w < max_width && tmp_h < max_height) {
  888. tmp_w = min(2 * tmp_w, max_width);
  889. tmp_h = min(2 * tmp_h, max_height);
  890. pix->width = tmp_w;
  891. pix->height = tmp_h;
  892. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  893. dev_geo(dev, "Camera scaled to %ux%u\n",
  894. pix->width, pix->height);
  895. if (ret < 0) {
  896. /* This shouldn't happen */
  897. dev_err(dev, "Client failed to set format: %d\n", ret);
  898. return ret;
  899. }
  900. }
  901. return 0;
  902. }
  903. /**
  904. * @rect - camera cropped rectangle
  905. * @sub_rect - CEU cropped rectangle, mapped back to camera input area
  906. * @ceu_rect - on output calculated CEU crop rectangle
  907. */
  908. static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
  909. struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
  910. struct v4l2_format *f, bool ceu_can_scale)
  911. {
  912. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  913. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  914. struct device *dev = icd->dev.parent;
  915. struct v4l2_format f_tmp = *f;
  916. struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
  917. unsigned int scale_h, scale_v;
  918. int ret;
  919. /* 5. Apply iterative camera S_FMT for camera user window. */
  920. ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
  921. if (ret < 0)
  922. return ret;
  923. dev_geo(dev, "5: camera scaled to %ux%u\n",
  924. pix_tmp->width, pix_tmp->height);
  925. /* 6. Retrieve camera output window (g_fmt) */
  926. /* unneeded - it is already in "f_tmp" */
  927. /* 7. Calculate new camera scales. */
  928. ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
  929. if (ret < 0)
  930. return ret;
  931. dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
  932. cam->cam_width = pix_tmp->width;
  933. cam->cam_height = pix_tmp->height;
  934. f->fmt.pix.width = pix_tmp->width;
  935. f->fmt.pix.height = pix_tmp->height;
  936. /*
  937. * 8. Calculate new CEU crop - apply camera scales to previously
  938. * calculated "effective" crop.
  939. */
  940. ceu_rect->left = scale_down(sub_rect->left, scale_h);
  941. ceu_rect->width = scale_down(sub_rect->width, scale_h);
  942. ceu_rect->top = scale_down(sub_rect->top, scale_v);
  943. ceu_rect->height = scale_down(sub_rect->height, scale_v);
  944. dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
  945. ceu_rect->width, ceu_rect->height,
  946. ceu_rect->left, ceu_rect->top);
  947. return 0;
  948. }
  949. /* Get combined scales */
  950. static int get_scales(struct soc_camera_device *icd,
  951. unsigned int *scale_h, unsigned int *scale_v)
  952. {
  953. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  954. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  955. struct v4l2_crop cam_crop;
  956. unsigned int width_in, height_in;
  957. int ret;
  958. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  959. ret = client_g_rect(sd, &cam_crop.c);
  960. if (ret < 0)
  961. return ret;
  962. ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
  963. if (ret < 0)
  964. return ret;
  965. width_in = scale_up(cam->ceu_rect.width, *scale_h);
  966. height_in = scale_up(cam->ceu_rect.height, *scale_v);
  967. *scale_h = calc_generic_scale(width_in, icd->user_width);
  968. *scale_v = calc_generic_scale(height_in, icd->user_height);
  969. return 0;
  970. }
  971. /*
  972. * CEU can scale and crop, but we don't want to waste bandwidth and kill the
  973. * framerate by always requesting the maximum image from the client. See
  974. * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
  975. * scaling and cropping algorithms and for the meaning of referenced here steps.
  976. */
  977. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  978. struct v4l2_crop *a)
  979. {
  980. struct v4l2_rect *rect = &a->c;
  981. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  982. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  983. struct v4l2_crop cam_crop;
  984. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  985. struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
  986. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  987. struct device *dev = icd->dev.parent;
  988. struct v4l2_format f;
  989. struct v4l2_pix_format *pix = &f.fmt.pix;
  990. unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
  991. out_width, out_height;
  992. u32 capsr, cflcr;
  993. int ret;
  994. /* 1. Calculate current combined scales. */
  995. ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
  996. if (ret < 0)
  997. return ret;
  998. dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
  999. /* 2. Apply iterative camera S_CROP for new input window. */
  1000. ret = client_s_crop(sd, a, &cam_crop);
  1001. if (ret < 0)
  1002. return ret;
  1003. dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
  1004. cam_rect->width, cam_rect->height,
  1005. cam_rect->left, cam_rect->top);
  1006. /* On success cam_crop contains current camera crop */
  1007. /*
  1008. * 3. If old combined scales applied to new crop produce an impossible
  1009. * user window, adjust scales to produce nearest possible window.
  1010. */
  1011. out_width = scale_down(rect->width, scale_comb_h);
  1012. out_height = scale_down(rect->height, scale_comb_v);
  1013. if (out_width > 2560)
  1014. out_width = 2560;
  1015. else if (out_width < 2)
  1016. out_width = 2;
  1017. if (out_height > 1920)
  1018. out_height = 1920;
  1019. else if (out_height < 4)
  1020. out_height = 4;
  1021. dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
  1022. /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
  1023. /*
  1024. * 5. Using actual input window and calculated combined scales calculate
  1025. * camera target output window.
  1026. */
  1027. pix->width = scale_down(cam_rect->width, scale_comb_h);
  1028. pix->height = scale_down(cam_rect->height, scale_comb_v);
  1029. dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
  1030. /* 6. - 9. */
  1031. pix->pixelformat = cam->camera_fmt->fourcc;
  1032. pix->colorspace = cam->camera_fmt->colorspace;
  1033. capsr = capture_save_reset(pcdev);
  1034. dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
  1035. /* Make relative to camera rectangle */
  1036. rect->left -= cam_rect->left;
  1037. rect->top -= cam_rect->top;
  1038. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1039. ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
  1040. pcdev->image_mode && !pcdev->is_interlaced);
  1041. dev_geo(dev, "6-9: %d\n", ret);
  1042. /* 10. Use CEU cropping to crop to the new window. */
  1043. sh_mobile_ceu_set_rect(icd, out_width, out_height);
  1044. dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
  1045. ceu_rect->width, ceu_rect->height,
  1046. ceu_rect->left, ceu_rect->top);
  1047. /*
  1048. * 11. Calculate CEU scales from camera scales from results of (10) and
  1049. * user window from (3)
  1050. */
  1051. scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
  1052. scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
  1053. dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
  1054. /* 12. Apply CEU scales. */
  1055. cflcr = scale_ceu_h | (scale_ceu_v << 16);
  1056. if (cflcr != pcdev->cflcr) {
  1057. pcdev->cflcr = cflcr;
  1058. ceu_write(pcdev, CFLCR, cflcr);
  1059. }
  1060. /* Restore capture */
  1061. if (pcdev->active)
  1062. capsr |= 1;
  1063. capture_restore(pcdev, capsr);
  1064. icd->user_width = out_width;
  1065. icd->user_height = out_height;
  1066. /* Even if only camera cropping succeeded */
  1067. return ret;
  1068. }
  1069. /* Similar to set_crop multistage iterative algorithm */
  1070. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  1071. struct v4l2_format *f)
  1072. {
  1073. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1074. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1075. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1076. struct v4l2_pix_format *pix = &f->fmt.pix;
  1077. struct v4l2_format cam_f = *f;
  1078. struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
  1079. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1080. struct device *dev = icd->dev.parent;
  1081. __u32 pixfmt = pix->pixelformat;
  1082. const struct soc_camera_format_xlate *xlate;
  1083. struct v4l2_crop cam_crop;
  1084. struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
  1085. unsigned int scale_cam_h, scale_cam_v;
  1086. u16 scale_v, scale_h;
  1087. int ret;
  1088. bool is_interlaced, image_mode;
  1089. switch (pix->field) {
  1090. case V4L2_FIELD_INTERLACED:
  1091. is_interlaced = true;
  1092. break;
  1093. case V4L2_FIELD_ANY:
  1094. default:
  1095. pix->field = V4L2_FIELD_NONE;
  1096. /* fall-through */
  1097. case V4L2_FIELD_NONE:
  1098. is_interlaced = false;
  1099. break;
  1100. }
  1101. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1102. if (!xlate) {
  1103. dev_warn(dev, "Format %x not found\n", pixfmt);
  1104. return -EINVAL;
  1105. }
  1106. /* 1. Calculate current camera scales. */
  1107. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1108. ret = client_g_rect(sd, cam_rect);
  1109. if (ret < 0)
  1110. return ret;
  1111. ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
  1112. if (ret < 0)
  1113. return ret;
  1114. dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
  1115. /*
  1116. * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
  1117. * scaled back at current camera scales onto input window.
  1118. */
  1119. ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
  1120. if (ret < 0)
  1121. return ret;
  1122. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  1123. cam_subrect.width, cam_subrect.height,
  1124. cam_subrect.left, cam_subrect.top);
  1125. /*
  1126. * 3. Calculate new combined scales from "effective" input window to
  1127. * requested user window.
  1128. */
  1129. scale_h = calc_generic_scale(cam_subrect.width, pix->width);
  1130. scale_v = calc_generic_scale(cam_subrect.height, pix->height);
  1131. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  1132. /*
  1133. * 4. Calculate camera output window by applying combined scales to real
  1134. * input window.
  1135. */
  1136. cam_pix->width = scale_down(cam_rect->width, scale_h);
  1137. cam_pix->height = scale_down(cam_rect->height, scale_v);
  1138. cam_pix->pixelformat = xlate->cam_fmt->fourcc;
  1139. switch (pixfmt) {
  1140. case V4L2_PIX_FMT_NV12:
  1141. case V4L2_PIX_FMT_NV21:
  1142. case V4L2_PIX_FMT_NV16:
  1143. case V4L2_PIX_FMT_NV61:
  1144. image_mode = true;
  1145. break;
  1146. default:
  1147. image_mode = false;
  1148. }
  1149. dev_geo(dev, "4: camera output %ux%u\n",
  1150. cam_pix->width, cam_pix->height);
  1151. /* 5. - 9. */
  1152. ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
  1153. image_mode && !is_interlaced);
  1154. dev_geo(dev, "5-9: client scale %d\n", ret);
  1155. /* Done with the camera. Now see if we can improve the result */
  1156. dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
  1157. ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
  1158. if (ret < 0)
  1159. return ret;
  1160. /* 10. Use CEU scaling to scale to the requested user window. */
  1161. /* We cannot scale up */
  1162. if (pix->width > cam_pix->width)
  1163. pix->width = cam_pix->width;
  1164. if (pix->width > ceu_rect.width)
  1165. pix->width = ceu_rect.width;
  1166. if (pix->height > cam_pix->height)
  1167. pix->height = cam_pix->height;
  1168. if (pix->height > ceu_rect.height)
  1169. pix->height = ceu_rect.height;
  1170. /* Let's rock: scale pix->{width x height} down to width x height */
  1171. scale_h = calc_scale(ceu_rect.width, &pix->width);
  1172. scale_v = calc_scale(ceu_rect.height, &pix->height);
  1173. dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
  1174. ceu_rect.width, scale_h, pix->width,
  1175. ceu_rect.height, scale_v, pix->height);
  1176. pcdev->cflcr = scale_h | (scale_v << 16);
  1177. icd->buswidth = xlate->buswidth;
  1178. icd->current_fmt = xlate->host_fmt;
  1179. cam->camera_fmt = xlate->cam_fmt;
  1180. cam->ceu_rect = ceu_rect;
  1181. pcdev->is_interlaced = is_interlaced;
  1182. pcdev->image_mode = image_mode;
  1183. return 0;
  1184. }
  1185. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  1186. struct v4l2_format *f)
  1187. {
  1188. const struct soc_camera_format_xlate *xlate;
  1189. struct v4l2_pix_format *pix = &f->fmt.pix;
  1190. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1191. __u32 pixfmt = pix->pixelformat;
  1192. int width, height;
  1193. int ret;
  1194. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1195. if (!xlate) {
  1196. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  1197. return -EINVAL;
  1198. }
  1199. /* FIXME: calculate using depth and bus width */
  1200. v4l_bound_align_image(&pix->width, 2, 2560, 1,
  1201. &pix->height, 4, 1920, 2, 0);
  1202. width = pix->width;
  1203. height = pix->height;
  1204. pix->bytesperline = pix->width *
  1205. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  1206. pix->sizeimage = pix->height * pix->bytesperline;
  1207. pix->pixelformat = xlate->cam_fmt->fourcc;
  1208. /* limit to sensor capabilities */
  1209. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  1210. pix->pixelformat = pixfmt;
  1211. if (ret < 0)
  1212. return ret;
  1213. switch (pixfmt) {
  1214. case V4L2_PIX_FMT_NV12:
  1215. case V4L2_PIX_FMT_NV21:
  1216. case V4L2_PIX_FMT_NV16:
  1217. case V4L2_PIX_FMT_NV61:
  1218. /* FIXME: check against rect_max after converting soc-camera */
  1219. /* We can scale precisely, need a bigger image from camera */
  1220. if (pix->width < width || pix->height < height) {
  1221. int tmp_w = pix->width, tmp_h = pix->height;
  1222. pix->width = 2560;
  1223. pix->height = 1920;
  1224. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  1225. if (ret < 0) {
  1226. /* Shouldn't actually happen... */
  1227. dev_err(icd->dev.parent,
  1228. "FIXME: try_fmt() returned %d\n", ret);
  1229. pix->width = tmp_w;
  1230. pix->height = tmp_h;
  1231. }
  1232. }
  1233. if (pix->width > width)
  1234. pix->width = width;
  1235. if (pix->height > height)
  1236. pix->height = height;
  1237. }
  1238. return ret;
  1239. }
  1240. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  1241. struct v4l2_requestbuffers *p)
  1242. {
  1243. int i;
  1244. /* This is for locking debugging only. I removed spinlocks and now I
  1245. * check whether .prepare is ever called on a linked buffer, or whether
  1246. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1247. * it hadn't triggered */
  1248. for (i = 0; i < p->count; i++) {
  1249. struct sh_mobile_ceu_buffer *buf;
  1250. buf = container_of(icf->vb_vidq.bufs[i],
  1251. struct sh_mobile_ceu_buffer, vb);
  1252. INIT_LIST_HEAD(&buf->vb.queue);
  1253. }
  1254. return 0;
  1255. }
  1256. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  1257. {
  1258. struct soc_camera_file *icf = file->private_data;
  1259. struct sh_mobile_ceu_buffer *buf;
  1260. buf = list_entry(icf->vb_vidq.stream.next,
  1261. struct sh_mobile_ceu_buffer, vb.stream);
  1262. poll_wait(file, &buf->vb.done, pt);
  1263. if (buf->vb.state == VIDEOBUF_DONE ||
  1264. buf->vb.state == VIDEOBUF_ERROR)
  1265. return POLLIN|POLLRDNORM;
  1266. return 0;
  1267. }
  1268. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  1269. struct v4l2_capability *cap)
  1270. {
  1271. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  1272. cap->version = KERNEL_VERSION(0, 0, 5);
  1273. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1274. return 0;
  1275. }
  1276. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  1277. struct soc_camera_device *icd)
  1278. {
  1279. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1280. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1281. videobuf_queue_dma_contig_init(q,
  1282. &sh_mobile_ceu_videobuf_ops,
  1283. icd->dev.parent, &pcdev->lock,
  1284. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1285. pcdev->is_interlaced ?
  1286. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  1287. sizeof(struct sh_mobile_ceu_buffer),
  1288. icd);
  1289. }
  1290. static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
  1291. struct v4l2_control *ctrl)
  1292. {
  1293. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1294. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1295. u32 val;
  1296. switch (ctrl->id) {
  1297. case V4L2_CID_SHARPNESS:
  1298. val = ceu_read(pcdev, CLFCR);
  1299. ctrl->value = val ^ 1;
  1300. return 0;
  1301. }
  1302. return -ENOIOCTLCMD;
  1303. }
  1304. static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
  1305. struct v4l2_control *ctrl)
  1306. {
  1307. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1308. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1309. switch (ctrl->id) {
  1310. case V4L2_CID_SHARPNESS:
  1311. switch (icd->current_fmt->fourcc) {
  1312. case V4L2_PIX_FMT_NV12:
  1313. case V4L2_PIX_FMT_NV21:
  1314. case V4L2_PIX_FMT_NV16:
  1315. case V4L2_PIX_FMT_NV61:
  1316. ceu_write(pcdev, CLFCR, !ctrl->value);
  1317. return 0;
  1318. }
  1319. return -EINVAL;
  1320. }
  1321. return -ENOIOCTLCMD;
  1322. }
  1323. static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
  1324. {
  1325. .id = V4L2_CID_SHARPNESS,
  1326. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1327. .name = "Low-pass filter",
  1328. .minimum = 0,
  1329. .maximum = 1,
  1330. .step = 1,
  1331. .default_value = 0,
  1332. },
  1333. };
  1334. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  1335. .owner = THIS_MODULE,
  1336. .add = sh_mobile_ceu_add_device,
  1337. .remove = sh_mobile_ceu_remove_device,
  1338. .get_formats = sh_mobile_ceu_get_formats,
  1339. .put_formats = sh_mobile_ceu_put_formats,
  1340. .set_crop = sh_mobile_ceu_set_crop,
  1341. .set_fmt = sh_mobile_ceu_set_fmt,
  1342. .try_fmt = sh_mobile_ceu_try_fmt,
  1343. .set_ctrl = sh_mobile_ceu_set_ctrl,
  1344. .get_ctrl = sh_mobile_ceu_get_ctrl,
  1345. .reqbufs = sh_mobile_ceu_reqbufs,
  1346. .poll = sh_mobile_ceu_poll,
  1347. .querycap = sh_mobile_ceu_querycap,
  1348. .set_bus_param = sh_mobile_ceu_set_bus_param,
  1349. .init_videobuf = sh_mobile_ceu_init_videobuf,
  1350. .controls = sh_mobile_ceu_controls,
  1351. .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
  1352. };
  1353. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  1354. {
  1355. struct sh_mobile_ceu_dev *pcdev;
  1356. struct resource *res;
  1357. void __iomem *base;
  1358. unsigned int irq;
  1359. int err = 0;
  1360. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1361. irq = platform_get_irq(pdev, 0);
  1362. if (!res || !irq) {
  1363. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  1364. err = -ENODEV;
  1365. goto exit;
  1366. }
  1367. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1368. if (!pcdev) {
  1369. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1370. err = -ENOMEM;
  1371. goto exit;
  1372. }
  1373. INIT_LIST_HEAD(&pcdev->capture);
  1374. spin_lock_init(&pcdev->lock);
  1375. pcdev->pdata = pdev->dev.platform_data;
  1376. if (!pcdev->pdata) {
  1377. err = -EINVAL;
  1378. dev_err(&pdev->dev, "CEU platform data not set.\n");
  1379. goto exit_kfree;
  1380. }
  1381. base = ioremap_nocache(res->start, resource_size(res));
  1382. if (!base) {
  1383. err = -ENXIO;
  1384. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  1385. goto exit_kfree;
  1386. }
  1387. pcdev->irq = irq;
  1388. pcdev->base = base;
  1389. pcdev->video_limit = 0; /* only enabled if second resource exists */
  1390. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1391. if (res) {
  1392. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  1393. res->start,
  1394. resource_size(res),
  1395. DMA_MEMORY_MAP |
  1396. DMA_MEMORY_EXCLUSIVE);
  1397. if (!err) {
  1398. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  1399. err = -ENXIO;
  1400. goto exit_iounmap;
  1401. }
  1402. pcdev->video_limit = resource_size(res);
  1403. }
  1404. /* request irq */
  1405. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  1406. dev_name(&pdev->dev), pcdev);
  1407. if (err) {
  1408. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  1409. goto exit_release_mem;
  1410. }
  1411. pm_suspend_ignore_children(&pdev->dev, true);
  1412. pm_runtime_enable(&pdev->dev);
  1413. pm_runtime_resume(&pdev->dev);
  1414. pcdev->ici.priv = pcdev;
  1415. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  1416. pcdev->ici.nr = pdev->id;
  1417. pcdev->ici.drv_name = dev_name(&pdev->dev);
  1418. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  1419. err = soc_camera_host_register(&pcdev->ici);
  1420. if (err)
  1421. goto exit_free_clk;
  1422. return 0;
  1423. exit_free_clk:
  1424. pm_runtime_disable(&pdev->dev);
  1425. free_irq(pcdev->irq, pcdev);
  1426. exit_release_mem:
  1427. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1428. dma_release_declared_memory(&pdev->dev);
  1429. exit_iounmap:
  1430. iounmap(base);
  1431. exit_kfree:
  1432. kfree(pcdev);
  1433. exit:
  1434. return err;
  1435. }
  1436. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  1437. {
  1438. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1439. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  1440. struct sh_mobile_ceu_dev, ici);
  1441. soc_camera_host_unregister(soc_host);
  1442. pm_runtime_disable(&pdev->dev);
  1443. free_irq(pcdev->irq, pcdev);
  1444. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1445. dma_release_declared_memory(&pdev->dev);
  1446. iounmap(pcdev->base);
  1447. kfree(pcdev);
  1448. return 0;
  1449. }
  1450. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1451. {
  1452. /* Runtime PM callback shared between ->runtime_suspend()
  1453. * and ->runtime_resume(). Simply returns success.
  1454. *
  1455. * This driver re-initializes all registers after
  1456. * pm_runtime_get_sync() anyway so there is no need
  1457. * to save and restore registers here.
  1458. */
  1459. return 0;
  1460. }
  1461. static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1462. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1463. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1464. };
  1465. static struct platform_driver sh_mobile_ceu_driver = {
  1466. .driver = {
  1467. .name = "sh_mobile_ceu",
  1468. .pm = &sh_mobile_ceu_dev_pm_ops,
  1469. },
  1470. .probe = sh_mobile_ceu_probe,
  1471. .remove = __exit_p(sh_mobile_ceu_remove),
  1472. };
  1473. static int __init sh_mobile_ceu_init(void)
  1474. {
  1475. return platform_driver_register(&sh_mobile_ceu_driver);
  1476. }
  1477. static void __exit sh_mobile_ceu_exit(void)
  1478. {
  1479. platform_driver_unregister(&sh_mobile_ceu_driver);
  1480. }
  1481. module_init(sh_mobile_ceu_init);
  1482. module_exit(sh_mobile_ceu_exit);
  1483. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1484. MODULE_AUTHOR("Magnus Damm");
  1485. MODULE_LICENSE("GPL");
  1486. MODULE_ALIAS("platform:sh_mobile_ceu");