sh_mobile_ceu_camera.c 51 KB

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