sh_mobile_ceu_camera.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  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/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/time.h>
  29. #include <linux/slab.h>
  30. #include <linux/device.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/sched.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-dev.h>
  37. #include <media/soc_camera.h>
  38. #include <media/sh_mobile_ceu.h>
  39. #include <media/sh_mobile_csi2.h>
  40. #include <media/videobuf2-dma-contig.h>
  41. #include <media/v4l2-mediabus.h>
  42. #include <media/soc_mediabus.h>
  43. /* register offsets for sh7722 / sh7723 */
  44. #define CAPSR 0x00 /* Capture start register */
  45. #define CAPCR 0x04 /* Capture control register */
  46. #define CAMCR 0x08 /* Capture interface control register */
  47. #define CMCYR 0x0c /* Capture interface cycle register */
  48. #define CAMOR 0x10 /* Capture interface offset register */
  49. #define CAPWR 0x14 /* Capture interface width register */
  50. #define CAIFR 0x18 /* Capture interface input format register */
  51. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  52. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  53. #define CRCNTR 0x28 /* CEU register control register */
  54. #define CRCMPR 0x2c /* CEU register forcible control register */
  55. #define CFLCR 0x30 /* Capture filter control register */
  56. #define CFSZR 0x34 /* Capture filter size clip register */
  57. #define CDWDR 0x38 /* Capture destination width register */
  58. #define CDAYR 0x3c /* Capture data address Y register */
  59. #define CDACR 0x40 /* Capture data address C register */
  60. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  61. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  62. #define CBDSR 0x4c /* Capture bundle destination size register */
  63. #define CFWCR 0x5c /* Firewall operation control register */
  64. #define CLFCR 0x60 /* Capture low-pass filter control register */
  65. #define CDOCR 0x64 /* Capture data output control register */
  66. #define CDDCR 0x68 /* Capture data complexity level register */
  67. #define CDDAR 0x6c /* Capture data complexity level address register */
  68. #define CEIER 0x70 /* Capture event interrupt enable register */
  69. #define CETCR 0x74 /* Capture event flag clear register */
  70. #define CSTSR 0x7c /* Capture status register */
  71. #define CSRTR 0x80 /* Capture software reset register */
  72. #define CDSSR 0x84 /* Capture data size register */
  73. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  74. #define CDACR2 0x94 /* Capture data address C register 2 */
  75. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  76. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  77. #undef DEBUG_GEOMETRY
  78. #ifdef DEBUG_GEOMETRY
  79. #define dev_geo dev_info
  80. #else
  81. #define dev_geo dev_dbg
  82. #endif
  83. /* per video frame buffer */
  84. struct sh_mobile_ceu_buffer {
  85. struct vb2_buffer vb; /* v4l buffer must be first */
  86. struct list_head queue;
  87. };
  88. struct sh_mobile_ceu_dev {
  89. struct soc_camera_host ici;
  90. struct soc_camera_device *icd;
  91. struct platform_device *csi2_pdev;
  92. unsigned int irq;
  93. void __iomem *base;
  94. size_t video_limit;
  95. size_t buf_total;
  96. spinlock_t lock; /* Protects video buffer lists */
  97. struct list_head capture;
  98. struct vb2_buffer *active;
  99. struct vb2_alloc_ctx *alloc_ctx;
  100. struct sh_mobile_ceu_info *pdata;
  101. struct completion complete;
  102. u32 cflcr;
  103. /* static max sizes either from platform data or default */
  104. int max_width;
  105. int max_height;
  106. enum v4l2_field field;
  107. int sequence;
  108. unsigned int image_mode:1;
  109. unsigned int is_16bit:1;
  110. unsigned int frozen:1;
  111. };
  112. struct sh_mobile_ceu_cam {
  113. /* CEU offsets within the camera output, before the CEU scaler */
  114. unsigned int ceu_left;
  115. unsigned int ceu_top;
  116. /* Client output, as seen by the CEU */
  117. unsigned int width;
  118. unsigned int height;
  119. /*
  120. * User window from S_CROP / G_CROP, produced by client cropping and
  121. * scaling, CEU scaling and CEU cropping, mapped back onto the client
  122. * input window
  123. */
  124. struct v4l2_rect subrect;
  125. /* Camera cropping rectangle */
  126. struct v4l2_rect rect;
  127. const struct soc_mbus_pixelfmt *extra_fmt;
  128. enum v4l2_mbus_pixelcode code;
  129. };
  130. static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
  131. {
  132. return container_of(vb, struct sh_mobile_ceu_buffer, vb);
  133. }
  134. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  135. unsigned long reg_offs, u32 data)
  136. {
  137. iowrite32(data, priv->base + reg_offs);
  138. }
  139. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  140. {
  141. return ioread32(priv->base + reg_offs);
  142. }
  143. static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
  144. {
  145. int i, success = 0;
  146. struct soc_camera_device *icd = pcdev->icd;
  147. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  148. /* wait CSTSR.CPTON bit */
  149. for (i = 0; i < 1000; i++) {
  150. if (!(ceu_read(pcdev, CSTSR) & 1)) {
  151. success++;
  152. break;
  153. }
  154. udelay(1);
  155. }
  156. /* wait CAPSR.CPKIL bit */
  157. for (i = 0; i < 1000; i++) {
  158. if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
  159. success++;
  160. break;
  161. }
  162. udelay(1);
  163. }
  164. if (2 != success) {
  165. dev_warn(icd->pdev, "soft reset time out\n");
  166. return -EIO;
  167. }
  168. return 0;
  169. }
  170. /*
  171. * Videobuf operations
  172. */
  173. /*
  174. * .queue_setup() is called to check, whether the driver can accept the
  175. * requested number of buffers and to fill in plane sizes
  176. * for the current frame format if required
  177. */
  178. static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
  179. const struct v4l2_format *fmt,
  180. unsigned int *count, unsigned int *num_planes,
  181. unsigned int sizes[], void *alloc_ctxs[])
  182. {
  183. struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
  184. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  185. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  186. if (fmt) {
  187. const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
  188. fmt->fmt.pix.pixelformat);
  189. int bytes_per_line;
  190. if (!xlate)
  191. return -EINVAL;
  192. bytes_per_line = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
  193. xlate->host_fmt);
  194. if (bytes_per_line < 0)
  195. return bytes_per_line;
  196. sizes[0] = bytes_per_line * fmt->fmt.pix.height;
  197. } else {
  198. /* Called from VIDIOC_REQBUFS or in compatibility mode */
  199. sizes[0] = icd->sizeimage;
  200. }
  201. alloc_ctxs[0] = pcdev->alloc_ctx;
  202. if (!vq->num_buffers)
  203. pcdev->sequence = 0;
  204. if (!*count)
  205. *count = 2;
  206. /* If *num_planes != 0, we have already verified *count. */
  207. if (pcdev->video_limit && !*num_planes) {
  208. size_t size = PAGE_ALIGN(sizes[0]) * *count;
  209. if (size + pcdev->buf_total > pcdev->video_limit)
  210. *count = (pcdev->video_limit - pcdev->buf_total) /
  211. PAGE_ALIGN(sizes[0]);
  212. }
  213. *num_planes = 1;
  214. dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
  215. return 0;
  216. }
  217. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  218. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  219. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  220. #define CEU_CEIER_VBP (1 << 20) /* vbp error */
  221. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  222. #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
  223. /*
  224. * return value doesn't reflex the success/failure to queue the new buffer,
  225. * but rather the status of the previous buffer.
  226. */
  227. static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  228. {
  229. struct soc_camera_device *icd = pcdev->icd;
  230. dma_addr_t phys_addr_top, phys_addr_bottom;
  231. unsigned long top1, top2;
  232. unsigned long bottom1, bottom2;
  233. u32 status;
  234. bool planar;
  235. int ret = 0;
  236. /*
  237. * The hardware is _very_ picky about this sequence. Especially
  238. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  239. * several not-so-well documented interrupt sources in CETCR.
  240. */
  241. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
  242. status = ceu_read(pcdev, CETCR);
  243. ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
  244. if (!pcdev->frozen)
  245. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
  246. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  247. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  248. /*
  249. * When a VBP interrupt occurs, a capture end interrupt does not occur
  250. * and the image of that frame is not captured correctly. So, soft reset
  251. * is needed here.
  252. */
  253. if (status & CEU_CEIER_VBP) {
  254. sh_mobile_ceu_soft_reset(pcdev);
  255. ret = -EIO;
  256. }
  257. if (pcdev->frozen) {
  258. complete(&pcdev->complete);
  259. return ret;
  260. }
  261. if (!pcdev->active)
  262. return ret;
  263. if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
  264. top1 = CDBYR;
  265. top2 = CDBCR;
  266. bottom1 = CDAYR;
  267. bottom2 = CDACR;
  268. } else {
  269. top1 = CDAYR;
  270. top2 = CDACR;
  271. bottom1 = CDBYR;
  272. bottom2 = CDBCR;
  273. }
  274. phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0);
  275. switch (icd->current_fmt->host_fmt->fourcc) {
  276. case V4L2_PIX_FMT_NV12:
  277. case V4L2_PIX_FMT_NV21:
  278. case V4L2_PIX_FMT_NV16:
  279. case V4L2_PIX_FMT_NV61:
  280. planar = true;
  281. break;
  282. default:
  283. planar = false;
  284. }
  285. ceu_write(pcdev, top1, phys_addr_top);
  286. if (V4L2_FIELD_NONE != pcdev->field) {
  287. if (planar)
  288. phys_addr_bottom = phys_addr_top + icd->user_width;
  289. else
  290. phys_addr_bottom = phys_addr_top +
  291. soc_mbus_bytes_per_line(icd->user_width,
  292. icd->current_fmt->host_fmt);
  293. ceu_write(pcdev, bottom1, phys_addr_bottom);
  294. }
  295. if (planar) {
  296. phys_addr_top += icd->user_width *
  297. icd->user_height;
  298. ceu_write(pcdev, top2, phys_addr_top);
  299. if (V4L2_FIELD_NONE != pcdev->field) {
  300. phys_addr_bottom = phys_addr_top + icd->user_width;
  301. ceu_write(pcdev, bottom2, phys_addr_bottom);
  302. }
  303. }
  304. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  305. return ret;
  306. }
  307. static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
  308. {
  309. struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
  310. /* Added list head initialization on alloc */
  311. WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
  312. return 0;
  313. }
  314. static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
  315. {
  316. struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
  317. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  318. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  319. struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
  320. unsigned long size;
  321. size = icd->sizeimage;
  322. if (vb2_plane_size(vb, 0) < size) {
  323. dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
  324. vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
  325. goto error;
  326. }
  327. vb2_set_plane_payload(vb, 0, size);
  328. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  329. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  330. #ifdef DEBUG
  331. /*
  332. * This can be useful if you want to see if we actually fill
  333. * the buffer with something
  334. */
  335. if (vb2_plane_vaddr(vb, 0))
  336. memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
  337. #endif
  338. spin_lock_irq(&pcdev->lock);
  339. list_add_tail(&buf->queue, &pcdev->capture);
  340. if (!pcdev->active) {
  341. /*
  342. * Because there were no active buffer at this moment,
  343. * we are not interested in the return value of
  344. * sh_mobile_ceu_capture here.
  345. */
  346. pcdev->active = vb;
  347. sh_mobile_ceu_capture(pcdev);
  348. }
  349. spin_unlock_irq(&pcdev->lock);
  350. return;
  351. error:
  352. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  353. }
  354. static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
  355. {
  356. struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
  357. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  358. struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
  359. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  360. spin_lock_irq(&pcdev->lock);
  361. if (pcdev->active == vb) {
  362. /* disable capture (release DMA buffer), reset */
  363. ceu_write(pcdev, CAPSR, 1 << 16);
  364. pcdev->active = NULL;
  365. }
  366. /*
  367. * Doesn't hurt also if the list is empty, but it hurts, if queuing the
  368. * buffer failed, and .buf_init() hasn't been called
  369. */
  370. if (buf->queue.next)
  371. list_del_init(&buf->queue);
  372. pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
  373. dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
  374. pcdev->buf_total);
  375. spin_unlock_irq(&pcdev->lock);
  376. }
  377. static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
  378. {
  379. struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
  380. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  381. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  382. pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
  383. dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
  384. pcdev->buf_total);
  385. /* This is for locking debugging only */
  386. INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
  387. return 0;
  388. }
  389. static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
  390. {
  391. struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
  392. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  393. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  394. struct list_head *buf_head, *tmp;
  395. spin_lock_irq(&pcdev->lock);
  396. pcdev->active = NULL;
  397. list_for_each_safe(buf_head, tmp, &pcdev->capture)
  398. list_del_init(buf_head);
  399. spin_unlock_irq(&pcdev->lock);
  400. return sh_mobile_ceu_soft_reset(pcdev);
  401. }
  402. static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
  403. .queue_setup = sh_mobile_ceu_videobuf_setup,
  404. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  405. .buf_queue = sh_mobile_ceu_videobuf_queue,
  406. .buf_cleanup = sh_mobile_ceu_videobuf_release,
  407. .buf_init = sh_mobile_ceu_videobuf_init,
  408. .wait_prepare = soc_camera_unlock,
  409. .wait_finish = soc_camera_lock,
  410. .stop_streaming = sh_mobile_ceu_stop_streaming,
  411. };
  412. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  413. {
  414. struct sh_mobile_ceu_dev *pcdev = data;
  415. struct vb2_buffer *vb;
  416. int ret;
  417. spin_lock(&pcdev->lock);
  418. vb = pcdev->active;
  419. if (!vb)
  420. /* Stale interrupt from a released buffer */
  421. goto out;
  422. list_del_init(&to_ceu_vb(vb)->queue);
  423. if (!list_empty(&pcdev->capture))
  424. pcdev->active = &list_entry(pcdev->capture.next,
  425. struct sh_mobile_ceu_buffer, queue)->vb;
  426. else
  427. pcdev->active = NULL;
  428. ret = sh_mobile_ceu_capture(pcdev);
  429. do_gettimeofday(&vb->v4l2_buf.timestamp);
  430. if (!ret) {
  431. vb->v4l2_buf.field = pcdev->field;
  432. vb->v4l2_buf.sequence = pcdev->sequence++;
  433. }
  434. vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  435. out:
  436. spin_unlock(&pcdev->lock);
  437. return IRQ_HANDLED;
  438. }
  439. static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev)
  440. {
  441. struct v4l2_subdev *sd;
  442. if (!pcdev->csi2_pdev)
  443. return NULL;
  444. v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev)
  445. if (&pcdev->csi2_pdev->dev == v4l2_get_subdevdata(sd))
  446. return sd;
  447. return NULL;
  448. }
  449. /* Called with .video_lock held */
  450. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  451. {
  452. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  453. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  454. struct v4l2_subdev *csi2_sd;
  455. int ret;
  456. if (pcdev->icd)
  457. return -EBUSY;
  458. dev_info(icd->parent,
  459. "SuperH Mobile CEU driver attached to camera %d\n",
  460. icd->devnum);
  461. pm_runtime_get_sync(ici->v4l2_dev.dev);
  462. pcdev->buf_total = 0;
  463. ret = sh_mobile_ceu_soft_reset(pcdev);
  464. csi2_sd = find_csi2(pcdev);
  465. if (csi2_sd) {
  466. csi2_sd->grp_id = soc_camera_grp_id(icd);
  467. v4l2_set_subdev_hostdata(csi2_sd, icd);
  468. }
  469. ret = v4l2_subdev_call(csi2_sd, core, s_power, 1);
  470. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) {
  471. pm_runtime_put_sync(ici->v4l2_dev.dev);
  472. return ret;
  473. }
  474. /*
  475. * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver
  476. * has not found this soc-camera device among its clients
  477. */
  478. if (ret == -ENODEV && csi2_sd)
  479. csi2_sd->grp_id = 0;
  480. pcdev->icd = icd;
  481. return 0;
  482. }
  483. /* Called with .video_lock held */
  484. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  485. {
  486. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  487. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  488. struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
  489. BUG_ON(icd != pcdev->icd);
  490. v4l2_subdev_call(csi2_sd, core, s_power, 0);
  491. if (csi2_sd)
  492. csi2_sd->grp_id = 0;
  493. /* disable capture, disable interrupts */
  494. ceu_write(pcdev, CEIER, 0);
  495. sh_mobile_ceu_soft_reset(pcdev);
  496. /* make sure active buffer is canceled */
  497. spin_lock_irq(&pcdev->lock);
  498. if (pcdev->active) {
  499. list_del_init(&to_ceu_vb(pcdev->active)->queue);
  500. vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
  501. pcdev->active = NULL;
  502. }
  503. spin_unlock_irq(&pcdev->lock);
  504. pm_runtime_put_sync(ici->v4l2_dev.dev);
  505. dev_info(icd->parent,
  506. "SuperH Mobile CEU driver detached from camera %d\n",
  507. icd->devnum);
  508. pcdev->icd = NULL;
  509. }
  510. /*
  511. * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
  512. * in SH7722 Hardware Manual
  513. */
  514. static unsigned int size_dst(unsigned int src, unsigned int scale)
  515. {
  516. unsigned int mant_pre = scale >> 12;
  517. if (!src || !scale)
  518. return src;
  519. return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
  520. mant_pre * 4096 / scale + 1;
  521. }
  522. static u16 calc_scale(unsigned int src, unsigned int *dst)
  523. {
  524. u16 scale;
  525. if (src == *dst)
  526. return 0;
  527. scale = (src * 4096 / *dst) & ~7;
  528. while (scale > 4096 && size_dst(src, scale) < *dst)
  529. scale -= 8;
  530. *dst = size_dst(src, scale);
  531. return scale;
  532. }
  533. /* rect is guaranteed to not exceed the scaled camera rectangle */
  534. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
  535. {
  536. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  537. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  538. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  539. unsigned int height, width, cdwdr_width, in_width, in_height;
  540. unsigned int left_offset, top_offset;
  541. u32 camor;
  542. dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
  543. icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
  544. left_offset = cam->ceu_left;
  545. top_offset = cam->ceu_top;
  546. WARN_ON(icd->user_width & 3 || icd->user_height & 3);
  547. width = icd->user_width;
  548. if (pcdev->image_mode) {
  549. in_width = cam->width;
  550. if (!pcdev->is_16bit) {
  551. in_width *= 2;
  552. left_offset *= 2;
  553. }
  554. cdwdr_width = width;
  555. } else {
  556. int bytes_per_line = soc_mbus_bytes_per_line(width,
  557. icd->current_fmt->host_fmt);
  558. unsigned int w_factor;
  559. switch (icd->current_fmt->host_fmt->packing) {
  560. case SOC_MBUS_PACKING_2X8_PADHI:
  561. w_factor = 2;
  562. break;
  563. default:
  564. w_factor = 1;
  565. }
  566. in_width = cam->width * w_factor;
  567. left_offset *= w_factor;
  568. if (bytes_per_line < 0)
  569. cdwdr_width = width;
  570. else
  571. cdwdr_width = bytes_per_line;
  572. }
  573. height = icd->user_height;
  574. in_height = cam->height;
  575. if (V4L2_FIELD_NONE != pcdev->field) {
  576. height = (height / 2) & ~3;
  577. in_height /= 2;
  578. top_offset /= 2;
  579. cdwdr_width *= 2;
  580. }
  581. /* CSI2 special configuration */
  582. if (pcdev->pdata->csi2) {
  583. in_width = ((in_width - 2) * 2);
  584. left_offset *= 2;
  585. }
  586. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  587. camor = left_offset | (top_offset << 16);
  588. dev_geo(icd->parent,
  589. "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
  590. (in_height << 16) | in_width, (height << 16) | width,
  591. cdwdr_width);
  592. ceu_write(pcdev, CAMOR, camor);
  593. ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
  594. /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */
  595. ceu_write(pcdev, CFSZR, (height << 16) | width);
  596. ceu_write(pcdev, CDWDR, cdwdr_width);
  597. }
  598. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  599. {
  600. u32 capsr = ceu_read(pcdev, CAPSR);
  601. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  602. return capsr;
  603. }
  604. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  605. {
  606. unsigned long timeout = jiffies + 10 * HZ;
  607. /*
  608. * Wait until the end of the current frame. It can take a long time,
  609. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  610. */
  611. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  612. msleep(1);
  613. if (time_after(jiffies, timeout)) {
  614. dev_err(pcdev->ici.v4l2_dev.dev,
  615. "Timeout waiting for frame end! Interface problem?\n");
  616. return;
  617. }
  618. /* Wait until reset clears, this shall not hang... */
  619. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  620. udelay(10);
  621. /* Anything to restore? */
  622. if (capsr & ~(1 << 16))
  623. ceu_write(pcdev, CAPSR, capsr);
  624. }
  625. /* Find the bus subdevice driver, e.g., CSI2 */
  626. static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev,
  627. struct soc_camera_device *icd)
  628. {
  629. if (pcdev->csi2_pdev) {
  630. struct v4l2_subdev *csi2_sd = find_csi2(pcdev);
  631. if (csi2_sd && csi2_sd->grp_id == soc_camera_grp_id(icd))
  632. return csi2_sd;
  633. }
  634. return soc_camera_to_subdev(icd);
  635. }
  636. #define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
  637. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  638. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  639. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  640. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  641. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  642. V4L2_MBUS_DATA_ACTIVE_HIGH)
  643. /* Capture is not running, no interrupts, no locking needed */
  644. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
  645. {
  646. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  647. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  648. struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
  649. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  650. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  651. unsigned long value, common_flags = CEU_BUS_FLAGS;
  652. u32 capsr = capture_save_reset(pcdev);
  653. unsigned int yuv_lineskip;
  654. int ret;
  655. /*
  656. * If the client doesn't implement g_mbus_config, we just use our
  657. * platform data
  658. */
  659. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  660. if (!ret) {
  661. common_flags = soc_mbus_config_compatible(&cfg,
  662. common_flags);
  663. if (!common_flags)
  664. return -EINVAL;
  665. } else if (ret != -ENOIOCTLCMD) {
  666. return ret;
  667. }
  668. /* Make choises, based on platform preferences */
  669. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  670. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  671. if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
  672. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  673. else
  674. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  675. }
  676. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  677. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  678. if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
  679. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  680. else
  681. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  682. }
  683. cfg.flags = common_flags;
  684. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  685. if (ret < 0 && ret != -ENOIOCTLCMD)
  686. return ret;
  687. if (icd->current_fmt->host_fmt->bits_per_sample > 8)
  688. pcdev->is_16bit = 1;
  689. else
  690. pcdev->is_16bit = 0;
  691. ceu_write(pcdev, CRCNTR, 0);
  692. ceu_write(pcdev, CRCMPR, 0);
  693. value = 0x00000010; /* data fetch by default */
  694. yuv_lineskip = 0x10;
  695. switch (icd->current_fmt->host_fmt->fourcc) {
  696. case V4L2_PIX_FMT_NV12:
  697. case V4L2_PIX_FMT_NV21:
  698. /* convert 4:2:2 -> 4:2:0 */
  699. yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */
  700. /* fall-through */
  701. case V4L2_PIX_FMT_NV16:
  702. case V4L2_PIX_FMT_NV61:
  703. switch (cam->code) {
  704. case V4L2_MBUS_FMT_UYVY8_2X8:
  705. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  706. break;
  707. case V4L2_MBUS_FMT_VYUY8_2X8:
  708. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  709. break;
  710. case V4L2_MBUS_FMT_YUYV8_2X8:
  711. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  712. break;
  713. case V4L2_MBUS_FMT_YVYU8_2X8:
  714. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  715. break;
  716. default:
  717. BUG();
  718. }
  719. }
  720. if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  721. icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
  722. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  723. value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  724. value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  725. value |= pcdev->is_16bit ? 1 << 12 : 0;
  726. /* CSI2 mode */
  727. if (pcdev->pdata->csi2)
  728. value |= 3 << 12;
  729. ceu_write(pcdev, CAMCR, value);
  730. ceu_write(pcdev, CAPCR, 0x00300000);
  731. switch (pcdev->field) {
  732. case V4L2_FIELD_INTERLACED_TB:
  733. value = 0x101;
  734. break;
  735. case V4L2_FIELD_INTERLACED_BT:
  736. value = 0x102;
  737. break;
  738. default:
  739. value = 0;
  740. break;
  741. }
  742. ceu_write(pcdev, CAIFR, value);
  743. sh_mobile_ceu_set_rect(icd);
  744. mdelay(1);
  745. dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
  746. ceu_write(pcdev, CFLCR, pcdev->cflcr);
  747. /*
  748. * A few words about byte order (observed in Big Endian mode)
  749. *
  750. * In data fetch mode bytes are received in chunks of 8 bytes.
  751. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  752. *
  753. * The data is however by default written to memory in reverse order:
  754. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  755. *
  756. * The lowest three bits of CDOCR allows us to do swapping,
  757. * using 7 we swap the data bytes to match the incoming order:
  758. * D0, D1, D2, D3, D4, D5, D6, D7
  759. */
  760. value = 0x00000007 | yuv_lineskip;
  761. ceu_write(pcdev, CDOCR, value);
  762. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  763. capture_restore(pcdev, capsr);
  764. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  765. return 0;
  766. }
  767. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
  768. unsigned char buswidth)
  769. {
  770. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  771. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  772. struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd);
  773. unsigned long common_flags = CEU_BUS_FLAGS;
  774. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  775. int ret;
  776. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  777. if (!ret)
  778. common_flags = soc_mbus_config_compatible(&cfg,
  779. common_flags);
  780. else if (ret != -ENOIOCTLCMD)
  781. return ret;
  782. if (!common_flags || buswidth > 16)
  783. return -EINVAL;
  784. return 0;
  785. }
  786. static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
  787. {
  788. .fourcc = V4L2_PIX_FMT_NV12,
  789. .name = "NV12",
  790. .bits_per_sample = 8,
  791. .packing = SOC_MBUS_PACKING_1_5X8,
  792. .order = SOC_MBUS_ORDER_LE,
  793. }, {
  794. .fourcc = V4L2_PIX_FMT_NV21,
  795. .name = "NV21",
  796. .bits_per_sample = 8,
  797. .packing = SOC_MBUS_PACKING_1_5X8,
  798. .order = SOC_MBUS_ORDER_LE,
  799. }, {
  800. .fourcc = V4L2_PIX_FMT_NV16,
  801. .name = "NV16",
  802. .bits_per_sample = 8,
  803. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  804. .order = SOC_MBUS_ORDER_LE,
  805. }, {
  806. .fourcc = V4L2_PIX_FMT_NV61,
  807. .name = "NV61",
  808. .bits_per_sample = 8,
  809. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  810. .order = SOC_MBUS_ORDER_LE,
  811. },
  812. };
  813. /* This will be corrected as we get more formats */
  814. static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  815. {
  816. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  817. (fmt->bits_per_sample == 8 &&
  818. fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
  819. (fmt->bits_per_sample == 8 &&
  820. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  821. (fmt->bits_per_sample > 8 &&
  822. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  823. }
  824. static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
  825. static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
  826. {
  827. return container_of(ctrl->handler, struct soc_camera_device,
  828. ctrl_handler);
  829. }
  830. static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
  831. {
  832. struct soc_camera_device *icd = ctrl_to_icd(ctrl);
  833. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  834. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  835. switch (ctrl->id) {
  836. case V4L2_CID_SHARPNESS:
  837. switch (icd->current_fmt->host_fmt->fourcc) {
  838. case V4L2_PIX_FMT_NV12:
  839. case V4L2_PIX_FMT_NV21:
  840. case V4L2_PIX_FMT_NV16:
  841. case V4L2_PIX_FMT_NV61:
  842. ceu_write(pcdev, CLFCR, !ctrl->val);
  843. return 0;
  844. }
  845. break;
  846. }
  847. return -EINVAL;
  848. }
  849. static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
  850. .s_ctrl = sh_mobile_ceu_s_ctrl,
  851. };
  852. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
  853. struct soc_camera_format_xlate *xlate)
  854. {
  855. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  856. struct device *dev = icd->parent;
  857. struct soc_camera_host *ici = to_soc_camera_host(dev);
  858. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  859. int ret, k, n;
  860. int formats = 0;
  861. struct sh_mobile_ceu_cam *cam;
  862. enum v4l2_mbus_pixelcode code;
  863. const struct soc_mbus_pixelfmt *fmt;
  864. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  865. if (ret < 0)
  866. /* No more formats */
  867. return 0;
  868. fmt = soc_mbus_get_fmtdesc(code);
  869. if (!fmt) {
  870. dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
  871. return 0;
  872. }
  873. if (!pcdev->pdata->csi2) {
  874. /* Are there any restrictions in the CSI-2 case? */
  875. ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
  876. if (ret < 0)
  877. return 0;
  878. }
  879. if (!icd->host_priv) {
  880. struct v4l2_mbus_framefmt mf;
  881. struct v4l2_rect rect;
  882. int shift = 0;
  883. /* Add our control */
  884. v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
  885. V4L2_CID_SHARPNESS, 0, 1, 1, 0);
  886. if (icd->ctrl_handler.error)
  887. return icd->ctrl_handler.error;
  888. /* FIXME: subwindow is lost between close / open */
  889. /* Cache current client geometry */
  890. ret = client_g_rect(sd, &rect);
  891. if (ret < 0)
  892. return ret;
  893. /* First time */
  894. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  895. if (ret < 0)
  896. return ret;
  897. /*
  898. * All currently existing CEU implementations support 2560x1920
  899. * or larger frames. If the sensor is proposing too big a frame,
  900. * don't bother with possibly supportred by the CEU larger
  901. * sizes, just try VGA multiples. If needed, this can be
  902. * adjusted in the future.
  903. */
  904. while ((mf.width > pcdev->max_width ||
  905. mf.height > pcdev->max_height) && shift < 4) {
  906. /* Try 2560x1920, 1280x960, 640x480, 320x240 */
  907. mf.width = 2560 >> shift;
  908. mf.height = 1920 >> shift;
  909. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  910. soc_camera_grp_id(icd), video,
  911. s_mbus_fmt, &mf);
  912. if (ret < 0)
  913. return ret;
  914. shift++;
  915. }
  916. if (shift == 4) {
  917. dev_err(dev, "Failed to configure the client below %ux%x\n",
  918. mf.width, mf.height);
  919. return -EIO;
  920. }
  921. dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
  922. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  923. if (!cam)
  924. return -ENOMEM;
  925. /* We are called with current camera crop, initialise subrect with it */
  926. cam->rect = rect;
  927. cam->subrect = rect;
  928. cam->width = mf.width;
  929. cam->height = mf.height;
  930. icd->host_priv = cam;
  931. } else {
  932. cam = icd->host_priv;
  933. }
  934. /* Beginning of a pass */
  935. if (!idx)
  936. cam->extra_fmt = NULL;
  937. switch (code) {
  938. case V4L2_MBUS_FMT_UYVY8_2X8:
  939. case V4L2_MBUS_FMT_VYUY8_2X8:
  940. case V4L2_MBUS_FMT_YUYV8_2X8:
  941. case V4L2_MBUS_FMT_YVYU8_2X8:
  942. if (cam->extra_fmt)
  943. break;
  944. /*
  945. * Our case is simple so far: for any of the above four camera
  946. * formats we add all our four synthesized NV* formats, so,
  947. * just marking the device with a single flag suffices. If
  948. * the format generation rules are more complex, you would have
  949. * to actually hang your already added / counted formats onto
  950. * the host_priv pointer and check whether the format you're
  951. * going to add now is already there.
  952. */
  953. cam->extra_fmt = sh_mobile_ceu_formats;
  954. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  955. formats += n;
  956. for (k = 0; xlate && k < n; k++) {
  957. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  958. xlate->code = code;
  959. xlate++;
  960. dev_dbg(dev, "Providing format %s using code %d\n",
  961. sh_mobile_ceu_formats[k].name, code);
  962. }
  963. break;
  964. default:
  965. if (!sh_mobile_ceu_packing_supported(fmt))
  966. return 0;
  967. }
  968. /* Generic pass-through */
  969. formats++;
  970. if (xlate) {
  971. xlate->host_fmt = fmt;
  972. xlate->code = code;
  973. xlate++;
  974. dev_dbg(dev, "Providing format %s in pass-through mode\n",
  975. fmt->name);
  976. }
  977. return formats;
  978. }
  979. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  980. {
  981. kfree(icd->host_priv);
  982. icd->host_priv = NULL;
  983. }
  984. /* Check if any dimension of r1 is smaller than respective one of r2 */
  985. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  986. {
  987. return r1->width < r2->width || r1->height < r2->height;
  988. }
  989. /* Check if r1 fails to cover r2 */
  990. static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
  991. {
  992. return r1->left > r2->left || r1->top > r2->top ||
  993. r1->left + r1->width < r2->left + r2->width ||
  994. r1->top + r1->height < r2->top + r2->height;
  995. }
  996. static unsigned int scale_down(unsigned int size, unsigned int scale)
  997. {
  998. return (size * 4096 + scale / 2) / scale;
  999. }
  1000. static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
  1001. {
  1002. return (input * 4096 + output / 2) / output;
  1003. }
  1004. /* Get and store current client crop */
  1005. static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
  1006. {
  1007. struct v4l2_crop crop;
  1008. struct v4l2_cropcap cap;
  1009. int ret;
  1010. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1011. ret = v4l2_subdev_call(sd, video, g_crop, &crop);
  1012. if (!ret) {
  1013. *rect = crop.c;
  1014. return ret;
  1015. }
  1016. /* Camera driver doesn't support .g_crop(), assume default rectangle */
  1017. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1018. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  1019. if (!ret)
  1020. *rect = cap.defrect;
  1021. return ret;
  1022. }
  1023. /* Client crop has changed, update our sub-rectangle to remain within the area */
  1024. static void update_subrect(struct sh_mobile_ceu_cam *cam)
  1025. {
  1026. struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
  1027. if (rect->width < subrect->width)
  1028. subrect->width = rect->width;
  1029. if (rect->height < subrect->height)
  1030. subrect->height = rect->height;
  1031. if (rect->left > subrect->left)
  1032. subrect->left = rect->left;
  1033. else if (rect->left + rect->width >
  1034. subrect->left + subrect->width)
  1035. subrect->left = rect->left + rect->width -
  1036. subrect->width;
  1037. if (rect->top > subrect->top)
  1038. subrect->top = rect->top;
  1039. else if (rect->top + rect->height >
  1040. subrect->top + subrect->height)
  1041. subrect->top = rect->top + rect->height -
  1042. subrect->height;
  1043. }
  1044. /*
  1045. * The common for both scaling and cropping iterative approach is:
  1046. * 1. try if the client can produce exactly what requested by the user
  1047. * 2. if (1) failed, try to double the client image until we get one big enough
  1048. * 3. if (2) failed, try to request the maximum image
  1049. */
  1050. static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
  1051. struct v4l2_crop *cam_crop)
  1052. {
  1053. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1054. struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
  1055. struct device *dev = sd->v4l2_dev->dev;
  1056. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1057. struct v4l2_cropcap cap;
  1058. int ret;
  1059. unsigned int width, height;
  1060. v4l2_subdev_call(sd, video, s_crop, crop);
  1061. ret = client_g_rect(sd, cam_rect);
  1062. if (ret < 0)
  1063. return ret;
  1064. /*
  1065. * Now cam_crop contains the current camera input rectangle, and it must
  1066. * be within camera cropcap bounds
  1067. */
  1068. if (!memcmp(rect, cam_rect, sizeof(*rect))) {
  1069. /* Even if camera S_CROP failed, but camera rectangle matches */
  1070. dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
  1071. rect->width, rect->height, rect->left, rect->top);
  1072. cam->rect = *cam_rect;
  1073. return 0;
  1074. }
  1075. /* Try to fix cropping, that camera hasn't managed to set */
  1076. dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
  1077. cam_rect->width, cam_rect->height,
  1078. cam_rect->left, cam_rect->top,
  1079. rect->width, rect->height, rect->left, rect->top);
  1080. /* We need sensor maximum rectangle */
  1081. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  1082. if (ret < 0)
  1083. return ret;
  1084. /* Put user requested rectangle within sensor bounds */
  1085. soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
  1086. cap.bounds.width);
  1087. soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
  1088. cap.bounds.height);
  1089. /*
  1090. * Popular special case - some cameras can only handle fixed sizes like
  1091. * QVGA, VGA,... Take care to avoid infinite loop.
  1092. */
  1093. width = max(cam_rect->width, 2);
  1094. height = max(cam_rect->height, 2);
  1095. /*
  1096. * Loop as long as sensor is not covering the requested rectangle and
  1097. * is still within its bounds
  1098. */
  1099. while (!ret && (is_smaller(cam_rect, rect) ||
  1100. is_inside(cam_rect, rect)) &&
  1101. (cap.bounds.width > width || cap.bounds.height > height)) {
  1102. width *= 2;
  1103. height *= 2;
  1104. cam_rect->width = width;
  1105. cam_rect->height = height;
  1106. /*
  1107. * We do not know what capabilities the camera has to set up
  1108. * left and top borders. We could try to be smarter in iterating
  1109. * them, e.g., if camera current left is to the right of the
  1110. * target left, set it to the middle point between the current
  1111. * left and minimum left. But that would add too much
  1112. * complexity: we would have to iterate each border separately.
  1113. * Instead we just drop to the left and top bounds.
  1114. */
  1115. if (cam_rect->left > rect->left)
  1116. cam_rect->left = cap.bounds.left;
  1117. if (cam_rect->left + cam_rect->width < rect->left + rect->width)
  1118. cam_rect->width = rect->left + rect->width -
  1119. cam_rect->left;
  1120. if (cam_rect->top > rect->top)
  1121. cam_rect->top = cap.bounds.top;
  1122. if (cam_rect->top + cam_rect->height < rect->top + rect->height)
  1123. cam_rect->height = rect->top + rect->height -
  1124. cam_rect->top;
  1125. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  1126. ret = client_g_rect(sd, cam_rect);
  1127. dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
  1128. cam_rect->width, cam_rect->height,
  1129. cam_rect->left, cam_rect->top);
  1130. }
  1131. /* S_CROP must not modify the rectangle */
  1132. if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
  1133. /*
  1134. * The camera failed to configure a suitable cropping,
  1135. * we cannot use the current rectangle, set to max
  1136. */
  1137. *cam_rect = cap.bounds;
  1138. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  1139. ret = client_g_rect(sd, cam_rect);
  1140. dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
  1141. cam_rect->width, cam_rect->height,
  1142. cam_rect->left, cam_rect->top);
  1143. }
  1144. if (!ret) {
  1145. cam->rect = *cam_rect;
  1146. update_subrect(cam);
  1147. }
  1148. return ret;
  1149. }
  1150. /* Iterative s_mbus_fmt, also updates cached client crop on success */
  1151. static int client_s_fmt(struct soc_camera_device *icd,
  1152. struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
  1153. {
  1154. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1155. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1156. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1157. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1158. struct device *dev = icd->parent;
  1159. unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
  1160. unsigned int max_width, max_height;
  1161. struct v4l2_cropcap cap;
  1162. bool ceu_1to1;
  1163. int ret;
  1164. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  1165. soc_camera_grp_id(icd), video,
  1166. s_mbus_fmt, mf);
  1167. if (ret < 0)
  1168. return ret;
  1169. dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
  1170. if (width == mf->width && height == mf->height) {
  1171. /* Perfect! The client has done it all. */
  1172. ceu_1to1 = true;
  1173. goto update_cache;
  1174. }
  1175. ceu_1to1 = false;
  1176. if (!ceu_can_scale)
  1177. goto update_cache;
  1178. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1179. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  1180. if (ret < 0)
  1181. return ret;
  1182. max_width = min(cap.bounds.width, pcdev->max_width);
  1183. max_height = min(cap.bounds.height, pcdev->max_height);
  1184. /* Camera set a format, but geometry is not precise, try to improve */
  1185. tmp_w = mf->width;
  1186. tmp_h = mf->height;
  1187. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  1188. while ((width > tmp_w || height > tmp_h) &&
  1189. tmp_w < max_width && tmp_h < max_height) {
  1190. tmp_w = min(2 * tmp_w, max_width);
  1191. tmp_h = min(2 * tmp_h, max_height);
  1192. mf->width = tmp_w;
  1193. mf->height = tmp_h;
  1194. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  1195. soc_camera_grp_id(icd), video,
  1196. s_mbus_fmt, mf);
  1197. dev_geo(dev, "Camera scaled to %ux%u\n",
  1198. mf->width, mf->height);
  1199. if (ret < 0) {
  1200. /* This shouldn't happen */
  1201. dev_err(dev, "Client failed to set format: %d\n", ret);
  1202. return ret;
  1203. }
  1204. }
  1205. update_cache:
  1206. /* Update cache */
  1207. ret = client_g_rect(sd, &cam->rect);
  1208. if (ret < 0)
  1209. return ret;
  1210. if (ceu_1to1)
  1211. cam->subrect = cam->rect;
  1212. else
  1213. update_subrect(cam);
  1214. return 0;
  1215. }
  1216. /**
  1217. * @width - on output: user width, mapped back to input
  1218. * @height - on output: user height, mapped back to input
  1219. * @mf - in- / output camera output window
  1220. */
  1221. static int client_scale(struct soc_camera_device *icd,
  1222. struct v4l2_mbus_framefmt *mf,
  1223. unsigned int *width, unsigned int *height,
  1224. bool ceu_can_scale)
  1225. {
  1226. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1227. struct device *dev = icd->parent;
  1228. struct v4l2_mbus_framefmt mf_tmp = *mf;
  1229. unsigned int scale_h, scale_v;
  1230. int ret;
  1231. /*
  1232. * 5. Apply iterative camera S_FMT for camera user window (also updates
  1233. * client crop cache and the imaginary sub-rectangle).
  1234. */
  1235. ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
  1236. if (ret < 0)
  1237. return ret;
  1238. dev_geo(dev, "5: camera scaled to %ux%u\n",
  1239. mf_tmp.width, mf_tmp.height);
  1240. /* 6. Retrieve camera output window (g_fmt) */
  1241. /* unneeded - it is already in "mf_tmp" */
  1242. /* 7. Calculate new client scales. */
  1243. scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
  1244. scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
  1245. mf->width = mf_tmp.width;
  1246. mf->height = mf_tmp.height;
  1247. mf->colorspace = mf_tmp.colorspace;
  1248. /*
  1249. * 8. Calculate new CEU crop - apply camera scales to previously
  1250. * updated "effective" crop.
  1251. */
  1252. *width = scale_down(cam->subrect.width, scale_h);
  1253. *height = scale_down(cam->subrect.height, scale_v);
  1254. dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
  1255. return 0;
  1256. }
  1257. /*
  1258. * CEU can scale and crop, but we don't want to waste bandwidth and kill the
  1259. * framerate by always requesting the maximum image from the client. See
  1260. * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
  1261. * scaling and cropping algorithms and for the meaning of referenced here steps.
  1262. */
  1263. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  1264. struct v4l2_crop *a)
  1265. {
  1266. struct v4l2_rect *rect = &a->c;
  1267. struct device *dev = icd->parent;
  1268. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1269. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1270. struct v4l2_crop cam_crop;
  1271. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1272. struct v4l2_rect *cam_rect = &cam_crop.c;
  1273. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1274. struct v4l2_mbus_framefmt mf;
  1275. unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
  1276. out_width, out_height;
  1277. int interm_width, interm_height;
  1278. u32 capsr, cflcr;
  1279. int ret;
  1280. dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
  1281. rect->left, rect->top);
  1282. /* During camera cropping its output window can change too, stop CEU */
  1283. capsr = capture_save_reset(pcdev);
  1284. dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
  1285. /*
  1286. * 1. - 2. Apply iterative camera S_CROP for new input window, read back
  1287. * actual camera rectangle.
  1288. */
  1289. ret = client_s_crop(icd, a, &cam_crop);
  1290. if (ret < 0)
  1291. return ret;
  1292. dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
  1293. cam_rect->width, cam_rect->height,
  1294. cam_rect->left, cam_rect->top);
  1295. /* On success cam_crop contains current camera crop */
  1296. /* 3. Retrieve camera output window */
  1297. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  1298. if (ret < 0)
  1299. return ret;
  1300. if (mf.width > pcdev->max_width || mf.height > pcdev->max_height)
  1301. return -EINVAL;
  1302. /* 4. Calculate camera scales */
  1303. scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
  1304. scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
  1305. /* Calculate intermediate window */
  1306. interm_width = scale_down(rect->width, scale_cam_h);
  1307. interm_height = scale_down(rect->height, scale_cam_v);
  1308. if (interm_width < icd->user_width) {
  1309. u32 new_scale_h;
  1310. new_scale_h = calc_generic_scale(rect->width, icd->user_width);
  1311. mf.width = scale_down(cam_rect->width, new_scale_h);
  1312. }
  1313. if (interm_height < icd->user_height) {
  1314. u32 new_scale_v;
  1315. new_scale_v = calc_generic_scale(rect->height, icd->user_height);
  1316. mf.height = scale_down(cam_rect->height, new_scale_v);
  1317. }
  1318. if (interm_width < icd->user_width || interm_height < icd->user_height) {
  1319. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  1320. soc_camera_grp_id(icd), video,
  1321. s_mbus_fmt, &mf);
  1322. if (ret < 0)
  1323. return ret;
  1324. dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
  1325. scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
  1326. scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
  1327. interm_width = scale_down(rect->width, scale_cam_h);
  1328. interm_height = scale_down(rect->height, scale_cam_v);
  1329. }
  1330. /* Cache camera output window */
  1331. cam->width = mf.width;
  1332. cam->height = mf.height;
  1333. if (pcdev->image_mode) {
  1334. out_width = min(interm_width, icd->user_width);
  1335. out_height = min(interm_height, icd->user_height);
  1336. } else {
  1337. out_width = interm_width;
  1338. out_height = interm_height;
  1339. }
  1340. /*
  1341. * 5. Calculate CEU scales from camera scales from results of (5) and
  1342. * the user window
  1343. */
  1344. scale_ceu_h = calc_scale(interm_width, &out_width);
  1345. scale_ceu_v = calc_scale(interm_height, &out_height);
  1346. dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
  1347. /* Apply CEU scales. */
  1348. cflcr = scale_ceu_h | (scale_ceu_v << 16);
  1349. if (cflcr != pcdev->cflcr) {
  1350. pcdev->cflcr = cflcr;
  1351. ceu_write(pcdev, CFLCR, cflcr);
  1352. }
  1353. icd->user_width = out_width & ~3;
  1354. icd->user_height = out_height & ~3;
  1355. /* Offsets are applied at the CEU scaling filter input */
  1356. cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
  1357. cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
  1358. /* 6. Use CEU cropping to crop to the new window. */
  1359. sh_mobile_ceu_set_rect(icd);
  1360. cam->subrect = *rect;
  1361. dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
  1362. icd->user_width, icd->user_height,
  1363. cam->ceu_left, cam->ceu_top);
  1364. /* Restore capture. The CE bit can be cleared by the hardware */
  1365. if (pcdev->active)
  1366. capsr |= 1;
  1367. capture_restore(pcdev, capsr);
  1368. /* Even if only camera cropping succeeded */
  1369. return ret;
  1370. }
  1371. static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
  1372. struct v4l2_crop *a)
  1373. {
  1374. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1375. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1376. a->c = cam->subrect;
  1377. return 0;
  1378. }
  1379. /*
  1380. * Calculate real client output window by applying new scales to the current
  1381. * client crop. New scales are calculated from the requested output format and
  1382. * CEU crop, mapped backed onto the client input (subrect).
  1383. */
  1384. static void calculate_client_output(struct soc_camera_device *icd,
  1385. const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
  1386. {
  1387. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1388. struct device *dev = icd->parent;
  1389. struct v4l2_rect *cam_subrect = &cam->subrect;
  1390. unsigned int scale_v, scale_h;
  1391. if (cam_subrect->width == cam->rect.width &&
  1392. cam_subrect->height == cam->rect.height) {
  1393. /* No sub-cropping */
  1394. mf->width = pix->width;
  1395. mf->height = pix->height;
  1396. return;
  1397. }
  1398. /* 1.-2. Current camera scales and subwin - cached. */
  1399. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  1400. cam_subrect->width, cam_subrect->height,
  1401. cam_subrect->left, cam_subrect->top);
  1402. /*
  1403. * 3. Calculate new combined scales from input sub-window to requested
  1404. * user window.
  1405. */
  1406. /*
  1407. * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
  1408. * (128x96) or larger than VGA
  1409. */
  1410. scale_h = calc_generic_scale(cam_subrect->width, pix->width);
  1411. scale_v = calc_generic_scale(cam_subrect->height, pix->height);
  1412. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  1413. /*
  1414. * 4. Calculate desired client output window by applying combined scales
  1415. * to client (real) input window.
  1416. */
  1417. mf->width = scale_down(cam->rect.width, scale_h);
  1418. mf->height = scale_down(cam->rect.height, scale_v);
  1419. }
  1420. /* Similar to set_crop multistage iterative algorithm */
  1421. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  1422. struct v4l2_format *f)
  1423. {
  1424. struct device *dev = icd->parent;
  1425. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1426. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1427. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1428. struct v4l2_pix_format *pix = &f->fmt.pix;
  1429. struct v4l2_mbus_framefmt mf;
  1430. __u32 pixfmt = pix->pixelformat;
  1431. const struct soc_camera_format_xlate *xlate;
  1432. /* Keep Compiler Happy */
  1433. unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
  1434. u16 scale_v, scale_h;
  1435. int ret;
  1436. bool image_mode;
  1437. enum v4l2_field field;
  1438. switch (pix->field) {
  1439. default:
  1440. pix->field = V4L2_FIELD_NONE;
  1441. /* fall-through */
  1442. case V4L2_FIELD_INTERLACED_TB:
  1443. case V4L2_FIELD_INTERLACED_BT:
  1444. case V4L2_FIELD_NONE:
  1445. field = pix->field;
  1446. break;
  1447. case V4L2_FIELD_INTERLACED:
  1448. field = V4L2_FIELD_INTERLACED_TB;
  1449. break;
  1450. }
  1451. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1452. if (!xlate) {
  1453. dev_warn(dev, "Format %x not found\n", pixfmt);
  1454. return -EINVAL;
  1455. }
  1456. /* 1.-4. Calculate desired client output geometry */
  1457. calculate_client_output(icd, pix, &mf);
  1458. mf.field = pix->field;
  1459. mf.colorspace = pix->colorspace;
  1460. mf.code = xlate->code;
  1461. switch (pixfmt) {
  1462. case V4L2_PIX_FMT_NV12:
  1463. case V4L2_PIX_FMT_NV21:
  1464. case V4L2_PIX_FMT_NV16:
  1465. case V4L2_PIX_FMT_NV61:
  1466. image_mode = true;
  1467. break;
  1468. default:
  1469. image_mode = false;
  1470. }
  1471. dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
  1472. pix->width, pix->height);
  1473. dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
  1474. /* 5. - 9. */
  1475. ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
  1476. image_mode && V4L2_FIELD_NONE == field);
  1477. dev_geo(dev, "5-9: client scale return %d\n", ret);
  1478. /* Done with the camera. Now see if we can improve the result */
  1479. dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
  1480. mf.width, mf.height, pix->width, pix->height);
  1481. if (ret < 0)
  1482. return ret;
  1483. if (mf.code != xlate->code)
  1484. return -EINVAL;
  1485. /* 9. Prepare CEU crop */
  1486. cam->width = mf.width;
  1487. cam->height = mf.height;
  1488. /* 10. Use CEU scaling to scale to the requested user window. */
  1489. /* We cannot scale up */
  1490. if (pix->width > ceu_sub_width)
  1491. ceu_sub_width = pix->width;
  1492. if (pix->height > ceu_sub_height)
  1493. ceu_sub_height = pix->height;
  1494. pix->colorspace = mf.colorspace;
  1495. if (image_mode) {
  1496. /* Scale pix->{width x height} down to width x height */
  1497. scale_h = calc_scale(ceu_sub_width, &pix->width);
  1498. scale_v = calc_scale(ceu_sub_height, &pix->height);
  1499. } else {
  1500. pix->width = ceu_sub_width;
  1501. pix->height = ceu_sub_height;
  1502. scale_h = 0;
  1503. scale_v = 0;
  1504. }
  1505. pcdev->cflcr = scale_h | (scale_v << 16);
  1506. /*
  1507. * We have calculated CFLCR, the actual configuration will be performed
  1508. * in sh_mobile_ceu_set_bus_param()
  1509. */
  1510. dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
  1511. ceu_sub_width, scale_h, pix->width,
  1512. ceu_sub_height, scale_v, pix->height);
  1513. cam->code = xlate->code;
  1514. icd->current_fmt = xlate;
  1515. pcdev->field = field;
  1516. pcdev->image_mode = image_mode;
  1517. /* CFSZR requirement */
  1518. pix->width &= ~3;
  1519. pix->height &= ~3;
  1520. return 0;
  1521. }
  1522. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  1523. struct v4l2_format *f)
  1524. {
  1525. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1526. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1527. const struct soc_camera_format_xlate *xlate;
  1528. struct v4l2_pix_format *pix = &f->fmt.pix;
  1529. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1530. struct v4l2_mbus_framefmt mf;
  1531. __u32 pixfmt = pix->pixelformat;
  1532. int width, height;
  1533. int ret;
  1534. dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
  1535. pixfmt, pix->width, pix->height);
  1536. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1537. if (!xlate) {
  1538. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  1539. return -EINVAL;
  1540. }
  1541. /* FIXME: calculate using depth and bus width */
  1542. /* CFSZR requires height and width to be 4-pixel aligned */
  1543. v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
  1544. &pix->height, 4, pcdev->max_height, 2, 0);
  1545. width = pix->width;
  1546. height = pix->height;
  1547. /* limit to sensor capabilities */
  1548. mf.width = pix->width;
  1549. mf.height = pix->height;
  1550. mf.field = pix->field;
  1551. mf.code = xlate->code;
  1552. mf.colorspace = pix->colorspace;
  1553. ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
  1554. video, try_mbus_fmt, &mf);
  1555. if (ret < 0)
  1556. return ret;
  1557. pix->width = mf.width;
  1558. pix->height = mf.height;
  1559. pix->field = mf.field;
  1560. pix->colorspace = mf.colorspace;
  1561. switch (pixfmt) {
  1562. case V4L2_PIX_FMT_NV12:
  1563. case V4L2_PIX_FMT_NV21:
  1564. case V4L2_PIX_FMT_NV16:
  1565. case V4L2_PIX_FMT_NV61:
  1566. /* FIXME: check against rect_max after converting soc-camera */
  1567. /* We can scale precisely, need a bigger image from camera */
  1568. if (pix->width < width || pix->height < height) {
  1569. /*
  1570. * We presume, the sensor behaves sanely, i.e., if
  1571. * requested a bigger rectangle, it will not return a
  1572. * smaller one.
  1573. */
  1574. mf.width = pcdev->max_width;
  1575. mf.height = pcdev->max_height;
  1576. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  1577. soc_camera_grp_id(icd), video,
  1578. try_mbus_fmt, &mf);
  1579. if (ret < 0) {
  1580. /* Shouldn't actually happen... */
  1581. dev_err(icd->parent,
  1582. "FIXME: client try_fmt() = %d\n", ret);
  1583. return ret;
  1584. }
  1585. }
  1586. /* We will scale exactly */
  1587. if (mf.width > width)
  1588. pix->width = width;
  1589. if (mf.height > height)
  1590. pix->height = height;
  1591. }
  1592. pix->width &= ~3;
  1593. pix->height &= ~3;
  1594. dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
  1595. __func__, ret, pix->pixelformat, pix->width, pix->height);
  1596. return ret;
  1597. }
  1598. static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
  1599. struct v4l2_crop *a)
  1600. {
  1601. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1602. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1603. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1604. u32 out_width = icd->user_width, out_height = icd->user_height;
  1605. int ret;
  1606. /* Freeze queue */
  1607. pcdev->frozen = 1;
  1608. /* Wait for frame */
  1609. ret = wait_for_completion_interruptible(&pcdev->complete);
  1610. /* Stop the client */
  1611. ret = v4l2_subdev_call(sd, video, s_stream, 0);
  1612. if (ret < 0)
  1613. dev_warn(icd->parent,
  1614. "Client failed to stop the stream: %d\n", ret);
  1615. else
  1616. /* Do the crop, if it fails, there's nothing more we can do */
  1617. sh_mobile_ceu_set_crop(icd, a);
  1618. dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
  1619. if (icd->user_width != out_width || icd->user_height != out_height) {
  1620. struct v4l2_format f = {
  1621. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1622. .fmt.pix = {
  1623. .width = out_width,
  1624. .height = out_height,
  1625. .pixelformat = icd->current_fmt->host_fmt->fourcc,
  1626. .field = pcdev->field,
  1627. .colorspace = icd->colorspace,
  1628. },
  1629. };
  1630. ret = sh_mobile_ceu_set_fmt(icd, &f);
  1631. if (!ret && (out_width != f.fmt.pix.width ||
  1632. out_height != f.fmt.pix.height))
  1633. ret = -EINVAL;
  1634. if (!ret) {
  1635. icd->user_width = out_width & ~3;
  1636. icd->user_height = out_height & ~3;
  1637. ret = sh_mobile_ceu_set_bus_param(icd);
  1638. }
  1639. }
  1640. /* Thaw the queue */
  1641. pcdev->frozen = 0;
  1642. spin_lock_irq(&pcdev->lock);
  1643. sh_mobile_ceu_capture(pcdev);
  1644. spin_unlock_irq(&pcdev->lock);
  1645. /* Start the client */
  1646. ret = v4l2_subdev_call(sd, video, s_stream, 1);
  1647. return ret;
  1648. }
  1649. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  1650. {
  1651. struct soc_camera_device *icd = file->private_data;
  1652. return vb2_poll(&icd->vb2_vidq, file, pt);
  1653. }
  1654. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  1655. struct v4l2_capability *cap)
  1656. {
  1657. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  1658. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1659. return 0;
  1660. }
  1661. static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
  1662. struct soc_camera_device *icd)
  1663. {
  1664. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1665. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1666. q->drv_priv = icd;
  1667. q->ops = &sh_mobile_ceu_videobuf_ops;
  1668. q->mem_ops = &vb2_dma_contig_memops;
  1669. q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
  1670. return vb2_queue_init(q);
  1671. }
  1672. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  1673. .owner = THIS_MODULE,
  1674. .add = sh_mobile_ceu_add_device,
  1675. .remove = sh_mobile_ceu_remove_device,
  1676. .get_formats = sh_mobile_ceu_get_formats,
  1677. .put_formats = sh_mobile_ceu_put_formats,
  1678. .get_crop = sh_mobile_ceu_get_crop,
  1679. .set_crop = sh_mobile_ceu_set_crop,
  1680. .set_livecrop = sh_mobile_ceu_set_livecrop,
  1681. .set_fmt = sh_mobile_ceu_set_fmt,
  1682. .try_fmt = sh_mobile_ceu_try_fmt,
  1683. .poll = sh_mobile_ceu_poll,
  1684. .querycap = sh_mobile_ceu_querycap,
  1685. .set_bus_param = sh_mobile_ceu_set_bus_param,
  1686. .init_videobuf2 = sh_mobile_ceu_init_videobuf,
  1687. };
  1688. struct bus_wait {
  1689. struct notifier_block notifier;
  1690. struct completion completion;
  1691. struct device *dev;
  1692. };
  1693. static int bus_notify(struct notifier_block *nb,
  1694. unsigned long action, void *data)
  1695. {
  1696. struct device *dev = data;
  1697. struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
  1698. if (wait->dev != dev)
  1699. return NOTIFY_DONE;
  1700. switch (action) {
  1701. case BUS_NOTIFY_UNBOUND_DRIVER:
  1702. /* Protect from module unloading */
  1703. wait_for_completion(&wait->completion);
  1704. return NOTIFY_OK;
  1705. }
  1706. return NOTIFY_DONE;
  1707. }
  1708. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  1709. {
  1710. struct sh_mobile_ceu_dev *pcdev;
  1711. struct resource *res;
  1712. void __iomem *base;
  1713. unsigned int irq;
  1714. int err = 0;
  1715. struct bus_wait wait = {
  1716. .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
  1717. .notifier.notifier_call = bus_notify,
  1718. };
  1719. struct sh_mobile_ceu_companion *csi2;
  1720. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1721. irq = platform_get_irq(pdev, 0);
  1722. if (!res || (int)irq <= 0) {
  1723. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  1724. err = -ENODEV;
  1725. goto exit;
  1726. }
  1727. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1728. if (!pcdev) {
  1729. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1730. err = -ENOMEM;
  1731. goto exit;
  1732. }
  1733. INIT_LIST_HEAD(&pcdev->capture);
  1734. spin_lock_init(&pcdev->lock);
  1735. init_completion(&pcdev->complete);
  1736. pcdev->pdata = pdev->dev.platform_data;
  1737. if (!pcdev->pdata) {
  1738. err = -EINVAL;
  1739. dev_err(&pdev->dev, "CEU platform data not set.\n");
  1740. goto exit_kfree;
  1741. }
  1742. pcdev->max_width = pcdev->pdata->max_width ? : 2560;
  1743. pcdev->max_height = pcdev->pdata->max_height ? : 1920;
  1744. base = ioremap_nocache(res->start, resource_size(res));
  1745. if (!base) {
  1746. err = -ENXIO;
  1747. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  1748. goto exit_kfree;
  1749. }
  1750. pcdev->irq = irq;
  1751. pcdev->base = base;
  1752. pcdev->video_limit = 0; /* only enabled if second resource exists */
  1753. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1754. if (res) {
  1755. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  1756. res->start,
  1757. resource_size(res),
  1758. DMA_MEMORY_MAP |
  1759. DMA_MEMORY_EXCLUSIVE);
  1760. if (!err) {
  1761. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  1762. err = -ENXIO;
  1763. goto exit_iounmap;
  1764. }
  1765. pcdev->video_limit = resource_size(res);
  1766. }
  1767. /* request irq */
  1768. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  1769. dev_name(&pdev->dev), pcdev);
  1770. if (err) {
  1771. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  1772. goto exit_release_mem;
  1773. }
  1774. pm_suspend_ignore_children(&pdev->dev, true);
  1775. pm_runtime_enable(&pdev->dev);
  1776. pm_runtime_resume(&pdev->dev);
  1777. pcdev->ici.priv = pcdev;
  1778. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  1779. pcdev->ici.nr = pdev->id;
  1780. pcdev->ici.drv_name = dev_name(&pdev->dev);
  1781. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  1782. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1783. if (IS_ERR(pcdev->alloc_ctx)) {
  1784. err = PTR_ERR(pcdev->alloc_ctx);
  1785. goto exit_free_clk;
  1786. }
  1787. err = soc_camera_host_register(&pcdev->ici);
  1788. if (err)
  1789. goto exit_free_ctx;
  1790. /* CSI2 interfacing */
  1791. csi2 = pcdev->pdata->csi2;
  1792. if (csi2) {
  1793. struct platform_device *csi2_pdev =
  1794. platform_device_alloc("sh-mobile-csi2", csi2->id);
  1795. struct sh_csi2_pdata *csi2_pdata = csi2->platform_data;
  1796. if (!csi2_pdev) {
  1797. err = -ENOMEM;
  1798. goto exit_host_unregister;
  1799. }
  1800. pcdev->csi2_pdev = csi2_pdev;
  1801. err = platform_device_add_data(csi2_pdev, csi2_pdata, sizeof(*csi2_pdata));
  1802. if (err < 0)
  1803. goto exit_pdev_put;
  1804. csi2_pdata = csi2_pdev->dev.platform_data;
  1805. csi2_pdata->v4l2_dev = &pcdev->ici.v4l2_dev;
  1806. csi2_pdev->resource = csi2->resource;
  1807. csi2_pdev->num_resources = csi2->num_resources;
  1808. err = platform_device_add(csi2_pdev);
  1809. if (err < 0)
  1810. goto exit_pdev_put;
  1811. wait.dev = &csi2_pdev->dev;
  1812. err = bus_register_notifier(&platform_bus_type, &wait.notifier);
  1813. if (err < 0)
  1814. goto exit_pdev_unregister;
  1815. /*
  1816. * From this point the driver module will not unload, until
  1817. * we complete the completion.
  1818. */
  1819. if (!csi2_pdev->dev.driver) {
  1820. complete(&wait.completion);
  1821. /* Either too late, or probing failed */
  1822. bus_unregister_notifier(&platform_bus_type, &wait.notifier);
  1823. err = -ENXIO;
  1824. goto exit_pdev_unregister;
  1825. }
  1826. /*
  1827. * The module is still loaded, in the worst case it is hanging
  1828. * in device release on our completion. So, _now_ dereferencing
  1829. * the "owner" is safe!
  1830. */
  1831. err = try_module_get(csi2_pdev->dev.driver->owner);
  1832. /* Let notifier complete, if it has been locked */
  1833. complete(&wait.completion);
  1834. bus_unregister_notifier(&platform_bus_type, &wait.notifier);
  1835. if (!err) {
  1836. err = -ENODEV;
  1837. goto exit_pdev_unregister;
  1838. }
  1839. }
  1840. return 0;
  1841. exit_pdev_unregister:
  1842. platform_device_del(pcdev->csi2_pdev);
  1843. exit_pdev_put:
  1844. pcdev->csi2_pdev->resource = NULL;
  1845. platform_device_put(pcdev->csi2_pdev);
  1846. exit_host_unregister:
  1847. soc_camera_host_unregister(&pcdev->ici);
  1848. exit_free_ctx:
  1849. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1850. exit_free_clk:
  1851. pm_runtime_disable(&pdev->dev);
  1852. free_irq(pcdev->irq, pcdev);
  1853. exit_release_mem:
  1854. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1855. dma_release_declared_memory(&pdev->dev);
  1856. exit_iounmap:
  1857. iounmap(base);
  1858. exit_kfree:
  1859. kfree(pcdev);
  1860. exit:
  1861. return err;
  1862. }
  1863. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  1864. {
  1865. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1866. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  1867. struct sh_mobile_ceu_dev, ici);
  1868. struct platform_device *csi2_pdev = pcdev->csi2_pdev;
  1869. soc_camera_host_unregister(soc_host);
  1870. pm_runtime_disable(&pdev->dev);
  1871. free_irq(pcdev->irq, pcdev);
  1872. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1873. dma_release_declared_memory(&pdev->dev);
  1874. iounmap(pcdev->base);
  1875. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1876. if (csi2_pdev && csi2_pdev->dev.driver) {
  1877. struct module *csi2_drv = csi2_pdev->dev.driver->owner;
  1878. platform_device_del(csi2_pdev);
  1879. csi2_pdev->resource = NULL;
  1880. platform_device_put(csi2_pdev);
  1881. module_put(csi2_drv);
  1882. }
  1883. kfree(pcdev);
  1884. return 0;
  1885. }
  1886. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1887. {
  1888. /* Runtime PM callback shared between ->runtime_suspend()
  1889. * and ->runtime_resume(). Simply returns success.
  1890. *
  1891. * This driver re-initializes all registers after
  1892. * pm_runtime_get_sync() anyway so there is no need
  1893. * to save and restore registers here.
  1894. */
  1895. return 0;
  1896. }
  1897. static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1898. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1899. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1900. };
  1901. static struct platform_driver sh_mobile_ceu_driver = {
  1902. .driver = {
  1903. .name = "sh_mobile_ceu",
  1904. .pm = &sh_mobile_ceu_dev_pm_ops,
  1905. },
  1906. .probe = sh_mobile_ceu_probe,
  1907. .remove = __devexit_p(sh_mobile_ceu_remove),
  1908. };
  1909. static int __init sh_mobile_ceu_init(void)
  1910. {
  1911. /* Whatever return code */
  1912. request_module("sh_mobile_csi2");
  1913. return platform_driver_register(&sh_mobile_ceu_driver);
  1914. }
  1915. static void __exit sh_mobile_ceu_exit(void)
  1916. {
  1917. platform_driver_unregister(&sh_mobile_ceu_driver);
  1918. }
  1919. module_init(sh_mobile_ceu_init);
  1920. module_exit(sh_mobile_ceu_exit);
  1921. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1922. MODULE_AUTHOR("Magnus Damm");
  1923. MODULE_LICENSE("GPL");
  1924. MODULE_VERSION("0.0.6");
  1925. MODULE_ALIAS("platform:sh_mobile_ceu");