sh_mobile_ceu_camera.c 49 KB

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