sh_mobile_ceu_camera.c 55 KB

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