sh_mobile_ceu_camera.c 49 KB

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