ipu3-cio2.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 Intel Corporation
  4. *
  5. * Based partially on Intel IPU4 driver written by
  6. * Sakari Ailus <sakari.ailus@linux.intel.com>
  7. * Samu Onkalo <samu.onkalo@intel.com>
  8. * Jouni Högander <jouni.hogander@intel.com>
  9. * Jouni Ukkonen <jouni.ukkonen@intel.com>
  10. * Antti Laakso <antti.laakso@intel.com>
  11. * et al.
  12. *
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/property.h>
  20. #include <linux/vmalloc.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-event.h>
  24. #include <media/v4l2-fwnode.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/videobuf2-dma-sg.h>
  27. #include "ipu3-cio2.h"
  28. struct ipu3_cio2_fmt {
  29. u32 mbus_code;
  30. u32 fourcc;
  31. u8 mipicode;
  32. };
  33. /*
  34. * These are raw formats used in Intel's third generation of
  35. * Image Processing Unit known as IPU3.
  36. * 10bit raw bayer packed, 32 bytes for every 25 pixels,
  37. * last LSB 6 bits unused.
  38. */
  39. static const struct ipu3_cio2_fmt formats[] = {
  40. { /* put default entry at beginning */
  41. .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
  42. .fourcc = V4L2_PIX_FMT_IPU3_SGRBG10,
  43. .mipicode = 0x2b,
  44. }, {
  45. .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
  46. .fourcc = V4L2_PIX_FMT_IPU3_SGBRG10,
  47. .mipicode = 0x2b,
  48. }, {
  49. .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
  50. .fourcc = V4L2_PIX_FMT_IPU3_SBGGR10,
  51. .mipicode = 0x2b,
  52. }, {
  53. .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
  54. .fourcc = V4L2_PIX_FMT_IPU3_SRGGB10,
  55. .mipicode = 0x2b,
  56. },
  57. };
  58. /*
  59. * cio2_find_format - lookup color format by fourcc or/and media bus code
  60. * @pixelformat: fourcc to match, ignored if null
  61. * @mbus_code: media bus code to match, ignored if null
  62. */
  63. static const struct ipu3_cio2_fmt *cio2_find_format(const u32 *pixelformat,
  64. const u32 *mbus_code)
  65. {
  66. unsigned int i;
  67. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  68. if (pixelformat && *pixelformat != formats[i].fourcc)
  69. continue;
  70. if (mbus_code && *mbus_code != formats[i].mbus_code)
  71. continue;
  72. return &formats[i];
  73. }
  74. return NULL;
  75. }
  76. static inline u32 cio2_bytesperline(const unsigned int width)
  77. {
  78. /*
  79. * 64 bytes for every 50 pixels, the line length
  80. * in bytes is multiple of 64 (line end alignment).
  81. */
  82. return DIV_ROUND_UP(width, 50) * 64;
  83. }
  84. /**************** FBPT operations ****************/
  85. static void cio2_fbpt_exit_dummy(struct cio2_device *cio2)
  86. {
  87. if (cio2->dummy_lop) {
  88. dma_free_coherent(&cio2->pci_dev->dev, CIO2_PAGE_SIZE,
  89. cio2->dummy_lop, cio2->dummy_lop_bus_addr);
  90. cio2->dummy_lop = NULL;
  91. }
  92. if (cio2->dummy_page) {
  93. dma_free_coherent(&cio2->pci_dev->dev, CIO2_PAGE_SIZE,
  94. cio2->dummy_page, cio2->dummy_page_bus_addr);
  95. cio2->dummy_page = NULL;
  96. }
  97. }
  98. static int cio2_fbpt_init_dummy(struct cio2_device *cio2)
  99. {
  100. unsigned int i;
  101. cio2->dummy_page = dma_alloc_coherent(&cio2->pci_dev->dev,
  102. CIO2_PAGE_SIZE,
  103. &cio2->dummy_page_bus_addr,
  104. GFP_KERNEL);
  105. cio2->dummy_lop = dma_alloc_coherent(&cio2->pci_dev->dev,
  106. CIO2_PAGE_SIZE,
  107. &cio2->dummy_lop_bus_addr,
  108. GFP_KERNEL);
  109. if (!cio2->dummy_page || !cio2->dummy_lop) {
  110. cio2_fbpt_exit_dummy(cio2);
  111. return -ENOMEM;
  112. }
  113. /*
  114. * List of Pointers(LOP) contains 1024x32b pointers to 4KB page each
  115. * Initialize each entry to dummy_page bus base address.
  116. */
  117. for (i = 0; i < CIO2_PAGE_SIZE / sizeof(*cio2->dummy_lop); i++)
  118. cio2->dummy_lop[i] = cio2->dummy_page_bus_addr >> PAGE_SHIFT;
  119. return 0;
  120. }
  121. static void cio2_fbpt_entry_enable(struct cio2_device *cio2,
  122. struct cio2_fbpt_entry entry[CIO2_MAX_LOPS])
  123. {
  124. /*
  125. * The CPU first initializes some fields in fbpt, then sets
  126. * the VALID bit, this barrier is to ensure that the DMA(device)
  127. * does not see the VALID bit enabled before other fields are
  128. * initialized; otherwise it could lead to havoc.
  129. */
  130. dma_wmb();
  131. /*
  132. * Request interrupts for start and completion
  133. * Valid bit is applicable only to 1st entry
  134. */
  135. entry[0].first_entry.ctrl = CIO2_FBPT_CTRL_VALID |
  136. CIO2_FBPT_CTRL_IOC | CIO2_FBPT_CTRL_IOS;
  137. }
  138. /* Initialize fpbt entries to point to dummy frame */
  139. static void cio2_fbpt_entry_init_dummy(struct cio2_device *cio2,
  140. struct cio2_fbpt_entry
  141. entry[CIO2_MAX_LOPS])
  142. {
  143. unsigned int i;
  144. entry[0].first_entry.first_page_offset = 0;
  145. entry[1].second_entry.num_of_pages =
  146. CIO2_PAGE_SIZE / sizeof(u32) * CIO2_MAX_LOPS;
  147. entry[1].second_entry.last_page_available_bytes = CIO2_PAGE_SIZE - 1;
  148. for (i = 0; i < CIO2_MAX_LOPS; i++)
  149. entry[i].lop_page_addr = cio2->dummy_lop_bus_addr >> PAGE_SHIFT;
  150. cio2_fbpt_entry_enable(cio2, entry);
  151. }
  152. /* Initialize fpbt entries to point to a given buffer */
  153. static void cio2_fbpt_entry_init_buf(struct cio2_device *cio2,
  154. struct cio2_buffer *b,
  155. struct cio2_fbpt_entry
  156. entry[CIO2_MAX_LOPS])
  157. {
  158. struct vb2_buffer *vb = &b->vbb.vb2_buf;
  159. unsigned int length = vb->planes[0].length;
  160. int remaining, i;
  161. entry[0].first_entry.first_page_offset = b->offset;
  162. remaining = length + entry[0].first_entry.first_page_offset;
  163. entry[1].second_entry.num_of_pages =
  164. DIV_ROUND_UP(remaining, CIO2_PAGE_SIZE);
  165. /*
  166. * last_page_available_bytes has the offset of the last byte in the
  167. * last page which is still accessible by DMA. DMA cannot access
  168. * beyond this point. Valid range for this is from 0 to 4095.
  169. * 0 indicates 1st byte in the page is DMA accessible.
  170. * 4095 (CIO2_PAGE_SIZE - 1) means every single byte in the last page
  171. * is available for DMA transfer.
  172. */
  173. entry[1].second_entry.last_page_available_bytes =
  174. (remaining & ~PAGE_MASK) ?
  175. (remaining & ~PAGE_MASK) - 1 :
  176. CIO2_PAGE_SIZE - 1;
  177. /* Fill FBPT */
  178. remaining = length;
  179. i = 0;
  180. while (remaining > 0) {
  181. entry->lop_page_addr = b->lop_bus_addr[i] >> PAGE_SHIFT;
  182. remaining -= CIO2_PAGE_SIZE / sizeof(u32) * CIO2_PAGE_SIZE;
  183. entry++;
  184. i++;
  185. }
  186. /*
  187. * The first not meaningful FBPT entry should point to a valid LOP
  188. */
  189. entry->lop_page_addr = cio2->dummy_lop_bus_addr >> PAGE_SHIFT;
  190. cio2_fbpt_entry_enable(cio2, entry);
  191. }
  192. static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q)
  193. {
  194. struct device *dev = &cio2->pci_dev->dev;
  195. q->fbpt = dma_alloc_coherent(dev, CIO2_FBPT_SIZE, &q->fbpt_bus_addr,
  196. GFP_KERNEL);
  197. if (!q->fbpt)
  198. return -ENOMEM;
  199. memset(q->fbpt, 0, CIO2_FBPT_SIZE);
  200. return 0;
  201. }
  202. static void cio2_fbpt_exit(struct cio2_queue *q, struct device *dev)
  203. {
  204. dma_free_coherent(dev, CIO2_FBPT_SIZE, q->fbpt, q->fbpt_bus_addr);
  205. }
  206. /**************** CSI2 hardware setup ****************/
  207. /*
  208. * The CSI2 receiver has several parameters affecting
  209. * the receiver timings. These depend on the MIPI bus frequency
  210. * F in Hz (sensor transmitter rate) as follows:
  211. * register value = (A/1e9 + B * UI) / COUNT_ACC
  212. * where
  213. * UI = 1 / (2 * F) in seconds
  214. * COUNT_ACC = counter accuracy in seconds
  215. * For IPU3 COUNT_ACC = 0.0625
  216. *
  217. * A and B are coefficients from the table below,
  218. * depending whether the register minimum or maximum value is
  219. * calculated.
  220. * Minimum Maximum
  221. * Clock lane A B A B
  222. * reg_rx_csi_dly_cnt_termen_clane 0 0 38 0
  223. * reg_rx_csi_dly_cnt_settle_clane 95 -8 300 -16
  224. * Data lanes
  225. * reg_rx_csi_dly_cnt_termen_dlane0 0 0 35 4
  226. * reg_rx_csi_dly_cnt_settle_dlane0 85 -2 145 -6
  227. * reg_rx_csi_dly_cnt_termen_dlane1 0 0 35 4
  228. * reg_rx_csi_dly_cnt_settle_dlane1 85 -2 145 -6
  229. * reg_rx_csi_dly_cnt_termen_dlane2 0 0 35 4
  230. * reg_rx_csi_dly_cnt_settle_dlane2 85 -2 145 -6
  231. * reg_rx_csi_dly_cnt_termen_dlane3 0 0 35 4
  232. * reg_rx_csi_dly_cnt_settle_dlane3 85 -2 145 -6
  233. *
  234. * We use the minimum values of both A and B.
  235. */
  236. /*
  237. * shift for keeping value range suitable for 32-bit integer arithmetics
  238. */
  239. #define LIMIT_SHIFT 8
  240. static s32 cio2_rx_timing(s32 a, s32 b, s64 freq, int def)
  241. {
  242. const u32 accinv = 16; /* invert of counter resolution */
  243. const u32 uiinv = 500000000; /* 1e9 / 2 */
  244. s32 r;
  245. freq >>= LIMIT_SHIFT;
  246. if (WARN_ON(freq <= 0 || freq > S32_MAX))
  247. return def;
  248. /*
  249. * b could be 0, -2 or -8, so |accinv * b| is always
  250. * less than (1 << ds) and thus |r| < 500000000.
  251. */
  252. r = accinv * b * (uiinv >> LIMIT_SHIFT);
  253. r = r / (s32)freq;
  254. /* max value of a is 95 */
  255. r += accinv * a;
  256. return r;
  257. };
  258. /* Calculate the the delay value for termination enable of clock lane HS Rx */
  259. static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
  260. struct cio2_csi2_timing *timing)
  261. {
  262. struct device *dev = &cio2->pci_dev->dev;
  263. struct v4l2_querymenu qm = {.id = V4L2_CID_LINK_FREQ, };
  264. struct v4l2_ctrl *link_freq;
  265. s64 freq;
  266. int r;
  267. if (!q->sensor)
  268. return -ENODEV;
  269. link_freq = v4l2_ctrl_find(q->sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
  270. if (!link_freq) {
  271. dev_err(dev, "failed to find LINK_FREQ\n");
  272. return -EPIPE;
  273. }
  274. qm.index = v4l2_ctrl_g_ctrl(link_freq);
  275. r = v4l2_querymenu(q->sensor->ctrl_handler, &qm);
  276. if (r) {
  277. dev_err(dev, "failed to get menu item\n");
  278. return r;
  279. }
  280. if (!qm.value) {
  281. dev_err(dev, "error invalid link_freq\n");
  282. return -EINVAL;
  283. }
  284. freq = qm.value;
  285. timing->clk_termen = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_A,
  286. CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_B,
  287. freq,
  288. CIO2_CSIRX_DLY_CNT_TERMEN_DEFAULT);
  289. timing->clk_settle = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_A,
  290. CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_B,
  291. freq,
  292. CIO2_CSIRX_DLY_CNT_SETTLE_DEFAULT);
  293. timing->dat_termen = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_A,
  294. CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_B,
  295. freq,
  296. CIO2_CSIRX_DLY_CNT_TERMEN_DEFAULT);
  297. timing->dat_settle = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_A,
  298. CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_B,
  299. freq,
  300. CIO2_CSIRX_DLY_CNT_SETTLE_DEFAULT);
  301. dev_dbg(dev, "freq ct value is %d\n", timing->clk_termen);
  302. dev_dbg(dev, "freq cs value is %d\n", timing->clk_settle);
  303. dev_dbg(dev, "freq dt value is %d\n", timing->dat_termen);
  304. dev_dbg(dev, "freq ds value is %d\n", timing->dat_settle);
  305. return 0;
  306. };
  307. static int cio2_hw_init(struct cio2_device *cio2, struct cio2_queue *q)
  308. {
  309. static const int NUM_VCS = 4;
  310. static const int SID; /* Stream id */
  311. static const int ENTRY;
  312. static const int FBPT_WIDTH = DIV_ROUND_UP(CIO2_MAX_LOPS,
  313. CIO2_FBPT_SUBENTRY_UNIT);
  314. const u32 num_buffers1 = CIO2_MAX_BUFFERS - 1;
  315. const struct ipu3_cio2_fmt *fmt;
  316. void __iomem *const base = cio2->base;
  317. u8 lanes, csi2bus = q->csi2.port;
  318. u8 sensor_vc = SENSOR_VIR_CH_DFLT;
  319. struct cio2_csi2_timing timing;
  320. int i, r;
  321. fmt = cio2_find_format(NULL, &q->subdev_fmt.code);
  322. if (!fmt)
  323. return -EINVAL;
  324. lanes = q->csi2.lanes;
  325. r = cio2_csi2_calc_timing(cio2, q, &timing);
  326. if (r)
  327. return r;
  328. writel(timing.clk_termen, q->csi_rx_base +
  329. CIO2_REG_CSIRX_DLY_CNT_TERMEN(CIO2_CSIRX_DLY_CNT_CLANE_IDX));
  330. writel(timing.clk_settle, q->csi_rx_base +
  331. CIO2_REG_CSIRX_DLY_CNT_SETTLE(CIO2_CSIRX_DLY_CNT_CLANE_IDX));
  332. for (i = 0; i < lanes; i++) {
  333. writel(timing.dat_termen, q->csi_rx_base +
  334. CIO2_REG_CSIRX_DLY_CNT_TERMEN(i));
  335. writel(timing.dat_settle, q->csi_rx_base +
  336. CIO2_REG_CSIRX_DLY_CNT_SETTLE(i));
  337. }
  338. writel(CIO2_PBM_WMCTRL1_MIN_2CK |
  339. CIO2_PBM_WMCTRL1_MID1_2CK |
  340. CIO2_PBM_WMCTRL1_MID2_2CK, base + CIO2_REG_PBM_WMCTRL1);
  341. writel(CIO2_PBM_WMCTRL2_HWM_2CK << CIO2_PBM_WMCTRL2_HWM_2CK_SHIFT |
  342. CIO2_PBM_WMCTRL2_LWM_2CK << CIO2_PBM_WMCTRL2_LWM_2CK_SHIFT |
  343. CIO2_PBM_WMCTRL2_OBFFWM_2CK <<
  344. CIO2_PBM_WMCTRL2_OBFFWM_2CK_SHIFT |
  345. CIO2_PBM_WMCTRL2_TRANSDYN << CIO2_PBM_WMCTRL2_TRANSDYN_SHIFT |
  346. CIO2_PBM_WMCTRL2_OBFF_MEM_EN, base + CIO2_REG_PBM_WMCTRL2);
  347. writel(CIO2_PBM_ARB_CTRL_LANES_DIV <<
  348. CIO2_PBM_ARB_CTRL_LANES_DIV_SHIFT |
  349. CIO2_PBM_ARB_CTRL_LE_EN |
  350. CIO2_PBM_ARB_CTRL_PLL_POST_SHTDN <<
  351. CIO2_PBM_ARB_CTRL_PLL_POST_SHTDN_SHIFT |
  352. CIO2_PBM_ARB_CTRL_PLL_AHD_WK_UP <<
  353. CIO2_PBM_ARB_CTRL_PLL_AHD_WK_UP_SHIFT,
  354. base + CIO2_REG_PBM_ARB_CTRL);
  355. writel(CIO2_CSIRX_STATUS_DLANE_HS_MASK,
  356. q->csi_rx_base + CIO2_REG_CSIRX_STATUS_DLANE_HS);
  357. writel(CIO2_CSIRX_STATUS_DLANE_LP_MASK,
  358. q->csi_rx_base + CIO2_REG_CSIRX_STATUS_DLANE_LP);
  359. writel(CIO2_FB_HPLL_FREQ, base + CIO2_REG_FB_HPLL_FREQ);
  360. writel(CIO2_ISCLK_RATIO, base + CIO2_REG_ISCLK_RATIO);
  361. /* Configure MIPI backend */
  362. for (i = 0; i < NUM_VCS; i++)
  363. writel(1, q->csi_rx_base + CIO2_REG_MIPIBE_SP_LUT_ENTRY(i));
  364. /* There are 16 short packet LUT entry */
  365. for (i = 0; i < 16; i++)
  366. writel(CIO2_MIPIBE_LP_LUT_ENTRY_DISREGARD,
  367. q->csi_rx_base + CIO2_REG_MIPIBE_LP_LUT_ENTRY(i));
  368. writel(CIO2_MIPIBE_GLOBAL_LUT_DISREGARD,
  369. q->csi_rx_base + CIO2_REG_MIPIBE_GLOBAL_LUT_DISREGARD);
  370. writel(CIO2_INT_EN_EXT_IE_MASK, base + CIO2_REG_INT_EN_EXT_IE);
  371. writel(CIO2_IRQCTRL_MASK, q->csi_rx_base + CIO2_REG_IRQCTRL_MASK);
  372. writel(CIO2_IRQCTRL_MASK, q->csi_rx_base + CIO2_REG_IRQCTRL_ENABLE);
  373. writel(0, q->csi_rx_base + CIO2_REG_IRQCTRL_EDGE);
  374. writel(0, q->csi_rx_base + CIO2_REG_IRQCTRL_LEVEL_NOT_PULSE);
  375. writel(CIO2_INT_EN_EXT_OE_MASK, base + CIO2_REG_INT_EN_EXT_OE);
  376. writel(CIO2_REG_INT_EN_IRQ | CIO2_INT_IOC(CIO2_DMA_CHAN) |
  377. CIO2_REG_INT_EN_IOS(CIO2_DMA_CHAN),
  378. base + CIO2_REG_INT_EN);
  379. writel((CIO2_PXM_PXF_FMT_CFG_BPP_10 | CIO2_PXM_PXF_FMT_CFG_PCK_64B)
  380. << CIO2_PXM_PXF_FMT_CFG_SID0_SHIFT,
  381. base + CIO2_REG_PXM_PXF_FMT_CFG0(csi2bus));
  382. writel(SID << CIO2_MIPIBE_LP_LUT_ENTRY_SID_SHIFT |
  383. sensor_vc << CIO2_MIPIBE_LP_LUT_ENTRY_VC_SHIFT |
  384. fmt->mipicode << CIO2_MIPIBE_LP_LUT_ENTRY_FORMAT_TYPE_SHIFT,
  385. q->csi_rx_base + CIO2_REG_MIPIBE_LP_LUT_ENTRY(ENTRY));
  386. writel(0, q->csi_rx_base + CIO2_REG_MIPIBE_COMP_FORMAT(sensor_vc));
  387. writel(0, q->csi_rx_base + CIO2_REG_MIPIBE_FORCE_RAW8);
  388. writel(0, base + CIO2_REG_PXM_SID2BID0(csi2bus));
  389. writel(lanes, q->csi_rx_base + CIO2_REG_CSIRX_NOF_ENABLED_LANES);
  390. writel(CIO2_CGC_PRIM_TGE |
  391. CIO2_CGC_SIDE_TGE |
  392. CIO2_CGC_XOSC_TGE |
  393. CIO2_CGC_D3I3_TGE |
  394. CIO2_CGC_CSI2_INTERFRAME_TGE |
  395. CIO2_CGC_CSI2_PORT_DCGE |
  396. CIO2_CGC_SIDE_DCGE |
  397. CIO2_CGC_PRIM_DCGE |
  398. CIO2_CGC_ROSC_DCGE |
  399. CIO2_CGC_XOSC_DCGE |
  400. CIO2_CGC_CLKGATE_HOLDOFF << CIO2_CGC_CLKGATE_HOLDOFF_SHIFT |
  401. CIO2_CGC_CSI_CLKGATE_HOLDOFF
  402. << CIO2_CGC_CSI_CLKGATE_HOLDOFF_SHIFT, base + CIO2_REG_CGC);
  403. writel(CIO2_LTRCTRL_LTRDYNEN, base + CIO2_REG_LTRCTRL);
  404. writel(CIO2_LTRVAL0_VAL << CIO2_LTRVAL02_VAL_SHIFT |
  405. CIO2_LTRVAL0_SCALE << CIO2_LTRVAL02_SCALE_SHIFT |
  406. CIO2_LTRVAL1_VAL << CIO2_LTRVAL13_VAL_SHIFT |
  407. CIO2_LTRVAL1_SCALE << CIO2_LTRVAL13_SCALE_SHIFT,
  408. base + CIO2_REG_LTRVAL01);
  409. writel(CIO2_LTRVAL2_VAL << CIO2_LTRVAL02_VAL_SHIFT |
  410. CIO2_LTRVAL2_SCALE << CIO2_LTRVAL02_SCALE_SHIFT |
  411. CIO2_LTRVAL3_VAL << CIO2_LTRVAL13_VAL_SHIFT |
  412. CIO2_LTRVAL3_SCALE << CIO2_LTRVAL13_SCALE_SHIFT,
  413. base + CIO2_REG_LTRVAL23);
  414. for (i = 0; i < CIO2_NUM_DMA_CHAN; i++) {
  415. writel(0, base + CIO2_REG_CDMABA(i));
  416. writel(0, base + CIO2_REG_CDMAC0(i));
  417. writel(0, base + CIO2_REG_CDMAC1(i));
  418. }
  419. /* Enable DMA */
  420. writel(q->fbpt_bus_addr >> PAGE_SHIFT,
  421. base + CIO2_REG_CDMABA(CIO2_DMA_CHAN));
  422. writel(num_buffers1 << CIO2_CDMAC0_FBPT_LEN_SHIFT |
  423. FBPT_WIDTH << CIO2_CDMAC0_FBPT_WIDTH_SHIFT |
  424. CIO2_CDMAC0_DMA_INTR_ON_FE |
  425. CIO2_CDMAC0_FBPT_UPDATE_FIFO_FULL |
  426. CIO2_CDMAC0_DMA_EN |
  427. CIO2_CDMAC0_DMA_INTR_ON_FS |
  428. CIO2_CDMAC0_DMA_HALTED, base + CIO2_REG_CDMAC0(CIO2_DMA_CHAN));
  429. writel(1 << CIO2_CDMAC1_LINENUMUPDATE_SHIFT,
  430. base + CIO2_REG_CDMAC1(CIO2_DMA_CHAN));
  431. writel(0, base + CIO2_REG_PBM_FOPN_ABORT);
  432. writel(CIO2_PXM_FRF_CFG_CRC_TH << CIO2_PXM_FRF_CFG_CRC_TH_SHIFT |
  433. CIO2_PXM_FRF_CFG_MSK_ECC_DPHY_NR |
  434. CIO2_PXM_FRF_CFG_MSK_ECC_RE |
  435. CIO2_PXM_FRF_CFG_MSK_ECC_DPHY_NE,
  436. base + CIO2_REG_PXM_FRF_CFG(q->csi2.port));
  437. /* Clear interrupts */
  438. writel(CIO2_IRQCTRL_MASK, q->csi_rx_base + CIO2_REG_IRQCTRL_CLEAR);
  439. writel(~0, base + CIO2_REG_INT_STS_EXT_OE);
  440. writel(~0, base + CIO2_REG_INT_STS_EXT_IE);
  441. writel(~0, base + CIO2_REG_INT_STS);
  442. /* Enable devices, starting from the last device in the pipe */
  443. writel(1, q->csi_rx_base + CIO2_REG_MIPIBE_ENABLE);
  444. writel(1, q->csi_rx_base + CIO2_REG_CSIRX_ENABLE);
  445. return 0;
  446. }
  447. static void cio2_hw_exit(struct cio2_device *cio2, struct cio2_queue *q)
  448. {
  449. void __iomem *base = cio2->base;
  450. unsigned int i, maxloops = 1000;
  451. /* Disable CSI receiver and MIPI backend devices */
  452. writel(0, q->csi_rx_base + CIO2_REG_IRQCTRL_MASK);
  453. writel(0, q->csi_rx_base + CIO2_REG_IRQCTRL_ENABLE);
  454. writel(0, q->csi_rx_base + CIO2_REG_CSIRX_ENABLE);
  455. writel(0, q->csi_rx_base + CIO2_REG_MIPIBE_ENABLE);
  456. /* Halt DMA */
  457. writel(0, base + CIO2_REG_CDMAC0(CIO2_DMA_CHAN));
  458. do {
  459. if (readl(base + CIO2_REG_CDMAC0(CIO2_DMA_CHAN)) &
  460. CIO2_CDMAC0_DMA_HALTED)
  461. break;
  462. usleep_range(1000, 2000);
  463. } while (--maxloops);
  464. if (!maxloops)
  465. dev_err(&cio2->pci_dev->dev,
  466. "DMA %i can not be halted\n", CIO2_DMA_CHAN);
  467. for (i = 0; i < CIO2_NUM_PORTS; i++) {
  468. writel(readl(base + CIO2_REG_PXM_FRF_CFG(i)) |
  469. CIO2_PXM_FRF_CFG_ABORT, base + CIO2_REG_PXM_FRF_CFG(i));
  470. writel(readl(base + CIO2_REG_PBM_FOPN_ABORT) |
  471. CIO2_PBM_FOPN_ABORT(i), base + CIO2_REG_PBM_FOPN_ABORT);
  472. }
  473. }
  474. static void cio2_buffer_done(struct cio2_device *cio2, unsigned int dma_chan)
  475. {
  476. struct device *dev = &cio2->pci_dev->dev;
  477. struct cio2_queue *q = cio2->cur_queue;
  478. int buffers_found = 0;
  479. u64 ns = ktime_get_ns();
  480. if (dma_chan >= CIO2_QUEUES) {
  481. dev_err(dev, "bad DMA channel %i\n", dma_chan);
  482. return;
  483. }
  484. /* Find out which buffer(s) are ready */
  485. do {
  486. struct cio2_fbpt_entry *const entry =
  487. &q->fbpt[q->bufs_first * CIO2_MAX_LOPS];
  488. struct cio2_buffer *b;
  489. if (entry->first_entry.ctrl & CIO2_FBPT_CTRL_VALID)
  490. break;
  491. b = q->bufs[q->bufs_first];
  492. if (b) {
  493. unsigned int bytes = entry[1].second_entry.num_of_bytes;
  494. q->bufs[q->bufs_first] = NULL;
  495. atomic_dec(&q->bufs_queued);
  496. dev_dbg(&cio2->pci_dev->dev,
  497. "buffer %i done\n", b->vbb.vb2_buf.index);
  498. b->vbb.vb2_buf.timestamp = ns;
  499. b->vbb.field = V4L2_FIELD_NONE;
  500. b->vbb.sequence = atomic_read(&q->frame_sequence);
  501. if (b->vbb.vb2_buf.planes[0].length != bytes)
  502. dev_warn(dev, "buffer length is %d received %d\n",
  503. b->vbb.vb2_buf.planes[0].length,
  504. bytes);
  505. vb2_buffer_done(&b->vbb.vb2_buf, VB2_BUF_STATE_DONE);
  506. }
  507. atomic_inc(&q->frame_sequence);
  508. cio2_fbpt_entry_init_dummy(cio2, entry);
  509. q->bufs_first = (q->bufs_first + 1) % CIO2_MAX_BUFFERS;
  510. buffers_found++;
  511. } while (1);
  512. if (buffers_found == 0)
  513. dev_warn(&cio2->pci_dev->dev,
  514. "no ready buffers found on DMA channel %u\n",
  515. dma_chan);
  516. }
  517. static void cio2_queue_event_sof(struct cio2_device *cio2, struct cio2_queue *q)
  518. {
  519. /*
  520. * For the user space camera control algorithms it is essential
  521. * to know when the reception of a frame has begun. That's often
  522. * the best timing information to get from the hardware.
  523. */
  524. struct v4l2_event event = {
  525. .type = V4L2_EVENT_FRAME_SYNC,
  526. .u.frame_sync.frame_sequence = atomic_read(&q->frame_sequence),
  527. };
  528. v4l2_event_queue(q->subdev.devnode, &event);
  529. }
  530. static const char *const cio2_irq_errs[] = {
  531. "single packet header error corrected",
  532. "multiple packet header errors detected",
  533. "payload checksum (CRC) error",
  534. "fifo overflow",
  535. "reserved short packet data type detected",
  536. "reserved long packet data type detected",
  537. "incomplete long packet detected",
  538. "frame sync error",
  539. "line sync error",
  540. "DPHY start of transmission error",
  541. "DPHY synchronization error",
  542. "escape mode error",
  543. "escape mode trigger event",
  544. "escape mode ultra-low power state for data lane(s)",
  545. "escape mode ultra-low power state exit for clock lane",
  546. "inter-frame short packet discarded",
  547. "inter-frame long packet discarded",
  548. "non-matching Long Packet stalled",
  549. };
  550. static const char *const cio2_port_errs[] = {
  551. "ECC recoverable",
  552. "DPHY not recoverable",
  553. "ECC not recoverable",
  554. "CRC error",
  555. "INTERFRAMEDATA",
  556. "PKT2SHORT",
  557. "PKT2LONG",
  558. };
  559. static void cio2_irq_handle_once(struct cio2_device *cio2, u32 int_status)
  560. {
  561. void __iomem *const base = cio2->base;
  562. struct device *dev = &cio2->pci_dev->dev;
  563. if (int_status & CIO2_INT_IOOE) {
  564. /*
  565. * Interrupt on Output Error:
  566. * 1) SRAM is full and FS received, or
  567. * 2) An invalid bit detected by DMA.
  568. */
  569. u32 oe_status, oe_clear;
  570. oe_clear = readl(base + CIO2_REG_INT_STS_EXT_OE);
  571. oe_status = oe_clear;
  572. if (oe_status & CIO2_INT_EXT_OE_DMAOE_MASK) {
  573. dev_err(dev, "DMA output error: 0x%x\n",
  574. (oe_status & CIO2_INT_EXT_OE_DMAOE_MASK)
  575. >> CIO2_INT_EXT_OE_DMAOE_SHIFT);
  576. oe_status &= ~CIO2_INT_EXT_OE_DMAOE_MASK;
  577. }
  578. if (oe_status & CIO2_INT_EXT_OE_OES_MASK) {
  579. dev_err(dev, "DMA output error on CSI2 buses: 0x%x\n",
  580. (oe_status & CIO2_INT_EXT_OE_OES_MASK)
  581. >> CIO2_INT_EXT_OE_OES_SHIFT);
  582. oe_status &= ~CIO2_INT_EXT_OE_OES_MASK;
  583. }
  584. writel(oe_clear, base + CIO2_REG_INT_STS_EXT_OE);
  585. if (oe_status)
  586. dev_warn(dev, "unknown interrupt 0x%x on OE\n",
  587. oe_status);
  588. int_status &= ~CIO2_INT_IOOE;
  589. }
  590. if (int_status & CIO2_INT_IOC_MASK) {
  591. /* DMA IO done -- frame ready */
  592. u32 clr = 0;
  593. unsigned int d;
  594. for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
  595. if (int_status & CIO2_INT_IOC(d)) {
  596. clr |= CIO2_INT_IOC(d);
  597. cio2_buffer_done(cio2, d);
  598. }
  599. int_status &= ~clr;
  600. }
  601. if (int_status & CIO2_INT_IOS_IOLN_MASK) {
  602. /* DMA IO starts or reached specified line */
  603. u32 clr = 0;
  604. unsigned int d;
  605. for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
  606. if (int_status & CIO2_INT_IOS_IOLN(d)) {
  607. clr |= CIO2_INT_IOS_IOLN(d);
  608. if (d == CIO2_DMA_CHAN)
  609. cio2_queue_event_sof(cio2,
  610. cio2->cur_queue);
  611. }
  612. int_status &= ~clr;
  613. }
  614. if (int_status & (CIO2_INT_IOIE | CIO2_INT_IOIRQ)) {
  615. /* CSI2 receiver (error) interrupt */
  616. u32 ie_status, ie_clear;
  617. unsigned int port;
  618. ie_clear = readl(base + CIO2_REG_INT_STS_EXT_IE);
  619. ie_status = ie_clear;
  620. for (port = 0; port < CIO2_NUM_PORTS; port++) {
  621. u32 port_status = (ie_status >> (port * 8)) & 0xff;
  622. u32 err_mask = BIT_MASK(ARRAY_SIZE(cio2_port_errs)) - 1;
  623. void __iomem *const csi_rx_base =
  624. base + CIO2_REG_PIPE_BASE(port);
  625. unsigned int i;
  626. while (port_status & err_mask) {
  627. i = ffs(port_status) - 1;
  628. dev_err(dev, "port %i error %s\n",
  629. port, cio2_port_errs[i]);
  630. ie_status &= ~BIT(port * 8 + i);
  631. port_status &= ~BIT(i);
  632. }
  633. if (ie_status & CIO2_INT_EXT_IE_IRQ(port)) {
  634. u32 csi2_status, csi2_clear;
  635. csi2_status = readl(csi_rx_base +
  636. CIO2_REG_IRQCTRL_STATUS);
  637. csi2_clear = csi2_status;
  638. err_mask =
  639. BIT_MASK(ARRAY_SIZE(cio2_irq_errs)) - 1;
  640. while (csi2_status & err_mask) {
  641. i = ffs(csi2_status) - 1;
  642. dev_err(dev,
  643. "CSI-2 receiver port %i: %s\n",
  644. port, cio2_irq_errs[i]);
  645. csi2_status &= ~BIT(i);
  646. }
  647. writel(csi2_clear,
  648. csi_rx_base + CIO2_REG_IRQCTRL_CLEAR);
  649. if (csi2_status)
  650. dev_warn(dev,
  651. "unknown CSI2 error 0x%x on port %i\n",
  652. csi2_status, port);
  653. ie_status &= ~CIO2_INT_EXT_IE_IRQ(port);
  654. }
  655. }
  656. writel(ie_clear, base + CIO2_REG_INT_STS_EXT_IE);
  657. if (ie_status)
  658. dev_warn(dev, "unknown interrupt 0x%x on IE\n",
  659. ie_status);
  660. int_status &= ~(CIO2_INT_IOIE | CIO2_INT_IOIRQ);
  661. }
  662. if (int_status)
  663. dev_warn(dev, "unknown interrupt 0x%x on INT\n", int_status);
  664. }
  665. static irqreturn_t cio2_irq(int irq, void *cio2_ptr)
  666. {
  667. struct cio2_device *cio2 = cio2_ptr;
  668. void __iomem *const base = cio2->base;
  669. struct device *dev = &cio2->pci_dev->dev;
  670. u32 int_status;
  671. int_status = readl(base + CIO2_REG_INT_STS);
  672. dev_dbg(dev, "isr enter - interrupt status 0x%x\n", int_status);
  673. if (!int_status)
  674. return IRQ_NONE;
  675. do {
  676. writel(int_status, base + CIO2_REG_INT_STS);
  677. cio2_irq_handle_once(cio2, int_status);
  678. int_status = readl(base + CIO2_REG_INT_STS);
  679. if (int_status)
  680. dev_dbg(dev, "pending status 0x%x\n", int_status);
  681. } while (int_status);
  682. return IRQ_HANDLED;
  683. }
  684. /**************** Videobuf2 interface ****************/
  685. static void cio2_vb2_return_all_buffers(struct cio2_queue *q,
  686. enum vb2_buffer_state state)
  687. {
  688. unsigned int i;
  689. for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
  690. if (q->bufs[i]) {
  691. atomic_dec(&q->bufs_queued);
  692. vb2_buffer_done(&q->bufs[i]->vbb.vb2_buf,
  693. state);
  694. }
  695. }
  696. }
  697. static int cio2_vb2_queue_setup(struct vb2_queue *vq,
  698. unsigned int *num_buffers,
  699. unsigned int *num_planes,
  700. unsigned int sizes[],
  701. struct device *alloc_devs[])
  702. {
  703. struct cio2_device *cio2 = vb2_get_drv_priv(vq);
  704. struct cio2_queue *q = vb2q_to_cio2_queue(vq);
  705. unsigned int i;
  706. *num_planes = q->format.num_planes;
  707. for (i = 0; i < *num_planes; ++i) {
  708. sizes[i] = q->format.plane_fmt[i].sizeimage;
  709. alloc_devs[i] = &cio2->pci_dev->dev;
  710. }
  711. *num_buffers = clamp_val(*num_buffers, 1, CIO2_MAX_BUFFERS);
  712. /* Initialize buffer queue */
  713. for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
  714. q->bufs[i] = NULL;
  715. cio2_fbpt_entry_init_dummy(cio2, &q->fbpt[i * CIO2_MAX_LOPS]);
  716. }
  717. atomic_set(&q->bufs_queued, 0);
  718. q->bufs_first = 0;
  719. q->bufs_next = 0;
  720. return 0;
  721. }
  722. /* Called after each buffer is allocated */
  723. static int cio2_vb2_buf_init(struct vb2_buffer *vb)
  724. {
  725. struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
  726. struct device *dev = &cio2->pci_dev->dev;
  727. struct cio2_buffer *b =
  728. container_of(vb, struct cio2_buffer, vbb.vb2_buf);
  729. static const unsigned int entries_per_page =
  730. CIO2_PAGE_SIZE / sizeof(u32);
  731. unsigned int pages = DIV_ROUND_UP(vb->planes[0].length, CIO2_PAGE_SIZE);
  732. unsigned int lops = DIV_ROUND_UP(pages + 1, entries_per_page);
  733. struct sg_table *sg;
  734. struct sg_page_iter sg_iter;
  735. int i, j;
  736. if (lops <= 0 || lops > CIO2_MAX_LOPS) {
  737. dev_err(dev, "%s: bad buffer size (%i)\n", __func__,
  738. vb->planes[0].length);
  739. return -ENOSPC; /* Should never happen */
  740. }
  741. memset(b->lop, 0, sizeof(b->lop));
  742. /* Allocate LOP table */
  743. for (i = 0; i < lops; i++) {
  744. b->lop[i] = dma_alloc_coherent(dev, CIO2_PAGE_SIZE,
  745. &b->lop_bus_addr[i], GFP_KERNEL);
  746. if (!b->lop[i])
  747. goto fail;
  748. }
  749. /* Fill LOP */
  750. sg = vb2_dma_sg_plane_desc(vb, 0);
  751. if (!sg)
  752. return -ENOMEM;
  753. if (sg->nents && sg->sgl)
  754. b->offset = sg->sgl->offset;
  755. i = j = 0;
  756. for_each_sg_page(sg->sgl, &sg_iter, sg->nents, 0) {
  757. if (!pages--)
  758. break;
  759. b->lop[i][j] = sg_page_iter_dma_address(&sg_iter) >> PAGE_SHIFT;
  760. j++;
  761. if (j == entries_per_page) {
  762. i++;
  763. j = 0;
  764. }
  765. }
  766. b->lop[i][j] = cio2->dummy_page_bus_addr >> PAGE_SHIFT;
  767. return 0;
  768. fail:
  769. for (i--; i >= 0; i--)
  770. dma_free_coherent(dev, CIO2_PAGE_SIZE,
  771. b->lop[i], b->lop_bus_addr[i]);
  772. return -ENOMEM;
  773. }
  774. /* Transfer buffer ownership to cio2 */
  775. static void cio2_vb2_buf_queue(struct vb2_buffer *vb)
  776. {
  777. struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
  778. struct cio2_queue *q =
  779. container_of(vb->vb2_queue, struct cio2_queue, vbq);
  780. struct cio2_buffer *b =
  781. container_of(vb, struct cio2_buffer, vbb.vb2_buf);
  782. struct cio2_fbpt_entry *entry;
  783. unsigned long flags;
  784. unsigned int i, j, next = q->bufs_next;
  785. int bufs_queued = atomic_inc_return(&q->bufs_queued);
  786. u32 fbpt_rp;
  787. dev_dbg(&cio2->pci_dev->dev, "queue buffer %d\n", vb->index);
  788. /*
  789. * This code queues the buffer to the CIO2 DMA engine, which starts
  790. * running once streaming has started. It is possible that this code
  791. * gets pre-empted due to increased CPU load. Upon this, the driver
  792. * does not get an opportunity to queue new buffers to the CIO2 DMA
  793. * engine. When the DMA engine encounters an FBPT entry without the
  794. * VALID bit set, the DMA engine halts, which requires a restart of
  795. * the DMA engine and sensor, to continue streaming.
  796. * This is not desired and is highly unlikely given that there are
  797. * 32 FBPT entries that the DMA engine needs to process, to run into
  798. * an FBPT entry, without the VALID bit set. We try to mitigate this
  799. * by disabling interrupts for the duration of this queueing.
  800. */
  801. local_irq_save(flags);
  802. fbpt_rp = (readl(cio2->base + CIO2_REG_CDMARI(CIO2_DMA_CHAN))
  803. >> CIO2_CDMARI_FBPT_RP_SHIFT)
  804. & CIO2_CDMARI_FBPT_RP_MASK;
  805. /*
  806. * fbpt_rp is the fbpt entry that the dma is currently working
  807. * on, but since it could jump to next entry at any time,
  808. * assume that we might already be there.
  809. */
  810. fbpt_rp = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;
  811. if (bufs_queued <= 1 || fbpt_rp == next)
  812. /* Buffers were drained */
  813. next = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;
  814. for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
  815. /*
  816. * We have allocated CIO2_MAX_BUFFERS circularly for the
  817. * hw, the user has requested N buffer queue. The driver
  818. * ensures N <= CIO2_MAX_BUFFERS and guarantees that whenever
  819. * user queues a buffer, there necessarily is a free buffer.
  820. */
  821. if (!q->bufs[next]) {
  822. q->bufs[next] = b;
  823. entry = &q->fbpt[next * CIO2_MAX_LOPS];
  824. cio2_fbpt_entry_init_buf(cio2, b, entry);
  825. local_irq_restore(flags);
  826. q->bufs_next = (next + 1) % CIO2_MAX_BUFFERS;
  827. for (j = 0; j < vb->num_planes; j++)
  828. vb2_set_plane_payload(vb, j,
  829. q->format.plane_fmt[j].sizeimage);
  830. return;
  831. }
  832. dev_dbg(&cio2->pci_dev->dev, "entry %i was full!\n", next);
  833. next = (next + 1) % CIO2_MAX_BUFFERS;
  834. }
  835. local_irq_restore(flags);
  836. dev_err(&cio2->pci_dev->dev, "error: all cio2 entries were full!\n");
  837. atomic_dec(&q->bufs_queued);
  838. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  839. }
  840. /* Called when each buffer is freed */
  841. static void cio2_vb2_buf_cleanup(struct vb2_buffer *vb)
  842. {
  843. struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
  844. struct cio2_buffer *b =
  845. container_of(vb, struct cio2_buffer, vbb.vb2_buf);
  846. unsigned int i;
  847. /* Free LOP table */
  848. for (i = 0; i < CIO2_MAX_LOPS; i++) {
  849. if (b->lop[i])
  850. dma_free_coherent(&cio2->pci_dev->dev, CIO2_PAGE_SIZE,
  851. b->lop[i], b->lop_bus_addr[i]);
  852. }
  853. }
  854. static int cio2_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  855. {
  856. struct cio2_queue *q = vb2q_to_cio2_queue(vq);
  857. struct cio2_device *cio2 = vb2_get_drv_priv(vq);
  858. int r;
  859. cio2->cur_queue = q;
  860. atomic_set(&q->frame_sequence, 0);
  861. r = pm_runtime_get_sync(&cio2->pci_dev->dev);
  862. if (r < 0) {
  863. dev_info(&cio2->pci_dev->dev, "failed to set power %d\n", r);
  864. pm_runtime_put_noidle(&cio2->pci_dev->dev);
  865. return r;
  866. }
  867. r = media_pipeline_start(&q->vdev.entity, &q->pipe);
  868. if (r)
  869. goto fail_pipeline;
  870. r = cio2_hw_init(cio2, q);
  871. if (r)
  872. goto fail_hw;
  873. /* Start streaming on sensor */
  874. r = v4l2_subdev_call(q->sensor, video, s_stream, 1);
  875. if (r)
  876. goto fail_csi2_subdev;
  877. cio2->streaming = true;
  878. return 0;
  879. fail_csi2_subdev:
  880. cio2_hw_exit(cio2, q);
  881. fail_hw:
  882. media_pipeline_stop(&q->vdev.entity);
  883. fail_pipeline:
  884. dev_dbg(&cio2->pci_dev->dev, "failed to start streaming (%d)\n", r);
  885. cio2_vb2_return_all_buffers(q, VB2_BUF_STATE_QUEUED);
  886. pm_runtime_put(&cio2->pci_dev->dev);
  887. return r;
  888. }
  889. static void cio2_vb2_stop_streaming(struct vb2_queue *vq)
  890. {
  891. struct cio2_queue *q = vb2q_to_cio2_queue(vq);
  892. struct cio2_device *cio2 = vb2_get_drv_priv(vq);
  893. if (v4l2_subdev_call(q->sensor, video, s_stream, 0))
  894. dev_err(&cio2->pci_dev->dev,
  895. "failed to stop sensor streaming\n");
  896. cio2_hw_exit(cio2, q);
  897. synchronize_irq(cio2->pci_dev->irq);
  898. cio2_vb2_return_all_buffers(q, VB2_BUF_STATE_ERROR);
  899. media_pipeline_stop(&q->vdev.entity);
  900. pm_runtime_put(&cio2->pci_dev->dev);
  901. cio2->streaming = false;
  902. }
  903. static const struct vb2_ops cio2_vb2_ops = {
  904. .buf_init = cio2_vb2_buf_init,
  905. .buf_queue = cio2_vb2_buf_queue,
  906. .buf_cleanup = cio2_vb2_buf_cleanup,
  907. .queue_setup = cio2_vb2_queue_setup,
  908. .start_streaming = cio2_vb2_start_streaming,
  909. .stop_streaming = cio2_vb2_stop_streaming,
  910. .wait_prepare = vb2_ops_wait_prepare,
  911. .wait_finish = vb2_ops_wait_finish,
  912. };
  913. /**************** V4L2 interface ****************/
  914. static int cio2_v4l2_querycap(struct file *file, void *fh,
  915. struct v4l2_capability *cap)
  916. {
  917. struct cio2_device *cio2 = video_drvdata(file);
  918. strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
  919. strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
  920. snprintf(cap->bus_info, sizeof(cap->bus_info),
  921. "PCI:%s", pci_name(cio2->pci_dev));
  922. return 0;
  923. }
  924. static int cio2_v4l2_enum_fmt(struct file *file, void *fh,
  925. struct v4l2_fmtdesc *f)
  926. {
  927. if (f->index >= ARRAY_SIZE(formats))
  928. return -EINVAL;
  929. f->pixelformat = formats[f->index].fourcc;
  930. return 0;
  931. }
  932. /* The format is validated in cio2_video_link_validate() */
  933. static int cio2_v4l2_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  934. {
  935. struct cio2_queue *q = file_to_cio2_queue(file);
  936. f->fmt.pix_mp = q->format;
  937. return 0;
  938. }
  939. static int cio2_v4l2_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
  940. {
  941. const struct ipu3_cio2_fmt *fmt;
  942. struct v4l2_pix_format_mplane *mpix = &f->fmt.pix_mp;
  943. fmt = cio2_find_format(&mpix->pixelformat, NULL);
  944. if (!fmt)
  945. fmt = &formats[0];
  946. /* Only supports up to 4224x3136 */
  947. if (mpix->width > CIO2_IMAGE_MAX_WIDTH)
  948. mpix->width = CIO2_IMAGE_MAX_WIDTH;
  949. if (mpix->height > CIO2_IMAGE_MAX_LENGTH)
  950. mpix->height = CIO2_IMAGE_MAX_LENGTH;
  951. mpix->num_planes = 1;
  952. mpix->pixelformat = fmt->fourcc;
  953. mpix->colorspace = V4L2_COLORSPACE_RAW;
  954. mpix->field = V4L2_FIELD_NONE;
  955. memset(mpix->reserved, 0, sizeof(mpix->reserved));
  956. mpix->plane_fmt[0].bytesperline = cio2_bytesperline(mpix->width);
  957. mpix->plane_fmt[0].sizeimage = mpix->plane_fmt[0].bytesperline *
  958. mpix->height;
  959. memset(mpix->plane_fmt[0].reserved, 0,
  960. sizeof(mpix->plane_fmt[0].reserved));
  961. /* use default */
  962. mpix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  963. mpix->quantization = V4L2_QUANTIZATION_DEFAULT;
  964. mpix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  965. return 0;
  966. }
  967. static int cio2_v4l2_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  968. {
  969. struct cio2_queue *q = file_to_cio2_queue(file);
  970. cio2_v4l2_try_fmt(file, fh, f);
  971. q->format = f->fmt.pix_mp;
  972. return 0;
  973. }
  974. static int
  975. cio2_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
  976. {
  977. if (input->index > 0)
  978. return -EINVAL;
  979. strlcpy(input->name, "camera", sizeof(input->name));
  980. input->type = V4L2_INPUT_TYPE_CAMERA;
  981. return 0;
  982. }
  983. static int
  984. cio2_video_g_input(struct file *file, void *fh, unsigned int *input)
  985. {
  986. *input = 0;
  987. return 0;
  988. }
  989. static int
  990. cio2_video_s_input(struct file *file, void *fh, unsigned int input)
  991. {
  992. return input == 0 ? 0 : -EINVAL;
  993. }
  994. static const struct v4l2_file_operations cio2_v4l2_fops = {
  995. .owner = THIS_MODULE,
  996. .unlocked_ioctl = video_ioctl2,
  997. .open = v4l2_fh_open,
  998. .release = vb2_fop_release,
  999. .poll = vb2_fop_poll,
  1000. .mmap = vb2_fop_mmap,
  1001. };
  1002. static const struct v4l2_ioctl_ops cio2_v4l2_ioctl_ops = {
  1003. .vidioc_querycap = cio2_v4l2_querycap,
  1004. .vidioc_enum_fmt_vid_cap_mplane = cio2_v4l2_enum_fmt,
  1005. .vidioc_g_fmt_vid_cap_mplane = cio2_v4l2_g_fmt,
  1006. .vidioc_s_fmt_vid_cap_mplane = cio2_v4l2_s_fmt,
  1007. .vidioc_try_fmt_vid_cap_mplane = cio2_v4l2_try_fmt,
  1008. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1009. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1010. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1011. .vidioc_querybuf = vb2_ioctl_querybuf,
  1012. .vidioc_qbuf = vb2_ioctl_qbuf,
  1013. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1014. .vidioc_streamon = vb2_ioctl_streamon,
  1015. .vidioc_streamoff = vb2_ioctl_streamoff,
  1016. .vidioc_expbuf = vb2_ioctl_expbuf,
  1017. .vidioc_enum_input = cio2_video_enum_input,
  1018. .vidioc_g_input = cio2_video_g_input,
  1019. .vidioc_s_input = cio2_video_s_input,
  1020. };
  1021. static int cio2_subdev_subscribe_event(struct v4l2_subdev *sd,
  1022. struct v4l2_fh *fh,
  1023. struct v4l2_event_subscription *sub)
  1024. {
  1025. if (sub->type != V4L2_EVENT_FRAME_SYNC)
  1026. return -EINVAL;
  1027. /* Line number. For now only zero accepted. */
  1028. if (sub->id != 0)
  1029. return -EINVAL;
  1030. return v4l2_event_subscribe(fh, sub, 0, NULL);
  1031. }
  1032. static int cio2_subdev_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1033. {
  1034. struct v4l2_mbus_framefmt *format;
  1035. const struct v4l2_mbus_framefmt fmt_default = {
  1036. .width = 1936,
  1037. .height = 1096,
  1038. .code = formats[0].mbus_code,
  1039. .field = V4L2_FIELD_NONE,
  1040. .colorspace = V4L2_COLORSPACE_RAW,
  1041. .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
  1042. .quantization = V4L2_QUANTIZATION_DEFAULT,
  1043. .xfer_func = V4L2_XFER_FUNC_DEFAULT,
  1044. };
  1045. /* Initialize try_fmt */
  1046. format = v4l2_subdev_get_try_format(sd, fh->pad, CIO2_PAD_SINK);
  1047. *format = fmt_default;
  1048. /* same as sink */
  1049. format = v4l2_subdev_get_try_format(sd, fh->pad, CIO2_PAD_SOURCE);
  1050. *format = fmt_default;
  1051. return 0;
  1052. }
  1053. /*
  1054. * cio2_subdev_get_fmt - Handle get format by pads subdev method
  1055. * @sd : pointer to v4l2 subdev structure
  1056. * @cfg: V4L2 subdev pad config
  1057. * @fmt: pointer to v4l2 subdev format structure
  1058. * return -EINVAL or zero on success
  1059. */
  1060. static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
  1061. struct v4l2_subdev_pad_config *cfg,
  1062. struct v4l2_subdev_format *fmt)
  1063. {
  1064. struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
  1065. struct v4l2_subdev_format format;
  1066. int ret;
  1067. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1068. fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1069. return 0;
  1070. }
  1071. if (fmt->pad == CIO2_PAD_SINK) {
  1072. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  1073. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
  1074. &format);
  1075. if (ret)
  1076. return ret;
  1077. /* update colorspace etc */
  1078. q->subdev_fmt.colorspace = format.format.colorspace;
  1079. q->subdev_fmt.ycbcr_enc = format.format.ycbcr_enc;
  1080. q->subdev_fmt.quantization = format.format.quantization;
  1081. q->subdev_fmt.xfer_func = format.format.xfer_func;
  1082. }
  1083. fmt->format = q->subdev_fmt;
  1084. return 0;
  1085. }
  1086. /*
  1087. * cio2_subdev_set_fmt - Handle set format by pads subdev method
  1088. * @sd : pointer to v4l2 subdev structure
  1089. * @cfg: V4L2 subdev pad config
  1090. * @fmt: pointer to v4l2 subdev format structure
  1091. * return -EINVAL or zero on success
  1092. */
  1093. static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
  1094. struct v4l2_subdev_pad_config *cfg,
  1095. struct v4l2_subdev_format *fmt)
  1096. {
  1097. struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
  1098. /*
  1099. * Only allow setting sink pad format;
  1100. * source always propagates from sink
  1101. */
  1102. if (fmt->pad == CIO2_PAD_SOURCE)
  1103. return cio2_subdev_get_fmt(sd, cfg, fmt);
  1104. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1105. *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
  1106. } else {
  1107. /* It's the sink, allow changing frame size */
  1108. q->subdev_fmt.width = fmt->format.width;
  1109. q->subdev_fmt.height = fmt->format.height;
  1110. q->subdev_fmt.code = fmt->format.code;
  1111. fmt->format = q->subdev_fmt;
  1112. }
  1113. return 0;
  1114. }
  1115. static int cio2_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  1116. struct v4l2_subdev_pad_config *cfg,
  1117. struct v4l2_subdev_mbus_code_enum *code)
  1118. {
  1119. if (code->index >= ARRAY_SIZE(formats))
  1120. return -EINVAL;
  1121. code->code = formats[code->index].mbus_code;
  1122. return 0;
  1123. }
  1124. static int cio2_subdev_link_validate_get_format(struct media_pad *pad,
  1125. struct v4l2_subdev_format *fmt)
  1126. {
  1127. if (is_media_entity_v4l2_subdev(pad->entity)) {
  1128. struct v4l2_subdev *sd =
  1129. media_entity_to_v4l2_subdev(pad->entity);
  1130. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  1131. fmt->pad = pad->index;
  1132. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  1133. }
  1134. return -EINVAL;
  1135. }
  1136. static int cio2_video_link_validate(struct media_link *link)
  1137. {
  1138. struct video_device *vd = container_of(link->sink->entity,
  1139. struct video_device, entity);
  1140. struct cio2_queue *q = container_of(vd, struct cio2_queue, vdev);
  1141. struct cio2_device *cio2 = video_get_drvdata(vd);
  1142. struct v4l2_subdev_format source_fmt;
  1143. int ret;
  1144. if (!media_entity_remote_pad(link->sink->entity->pads)) {
  1145. dev_info(&cio2->pci_dev->dev,
  1146. "video node %s pad not connected\n", vd->name);
  1147. return -ENOTCONN;
  1148. }
  1149. ret = cio2_subdev_link_validate_get_format(link->source, &source_fmt);
  1150. if (ret < 0)
  1151. return 0;
  1152. if (source_fmt.format.width != q->format.width ||
  1153. source_fmt.format.height != q->format.height) {
  1154. dev_err(&cio2->pci_dev->dev,
  1155. "Wrong width or height %ux%u (%ux%u expected)\n",
  1156. q->format.width, q->format.height,
  1157. source_fmt.format.width, source_fmt.format.height);
  1158. return -EINVAL;
  1159. }
  1160. if (!cio2_find_format(&q->format.pixelformat, &source_fmt.format.code))
  1161. return -EINVAL;
  1162. return 0;
  1163. }
  1164. static const struct v4l2_subdev_core_ops cio2_subdev_core_ops = {
  1165. .subscribe_event = cio2_subdev_subscribe_event,
  1166. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1167. };
  1168. static const struct v4l2_subdev_internal_ops cio2_subdev_internal_ops = {
  1169. .open = cio2_subdev_open,
  1170. };
  1171. static const struct v4l2_subdev_pad_ops cio2_subdev_pad_ops = {
  1172. .link_validate = v4l2_subdev_link_validate_default,
  1173. .get_fmt = cio2_subdev_get_fmt,
  1174. .set_fmt = cio2_subdev_set_fmt,
  1175. .enum_mbus_code = cio2_subdev_enum_mbus_code,
  1176. };
  1177. static const struct v4l2_subdev_ops cio2_subdev_ops = {
  1178. .core = &cio2_subdev_core_ops,
  1179. .pad = &cio2_subdev_pad_ops,
  1180. };
  1181. /******* V4L2 sub-device asynchronous registration callbacks***********/
  1182. struct sensor_async_subdev {
  1183. struct v4l2_async_subdev asd;
  1184. struct csi2_bus_info csi2;
  1185. };
  1186. /* The .bound() notifier callback when a match is found */
  1187. static int cio2_notifier_bound(struct v4l2_async_notifier *notifier,
  1188. struct v4l2_subdev *sd,
  1189. struct v4l2_async_subdev *asd)
  1190. {
  1191. struct cio2_device *cio2 = container_of(notifier,
  1192. struct cio2_device, notifier);
  1193. struct sensor_async_subdev *s_asd = container_of(asd,
  1194. struct sensor_async_subdev, asd);
  1195. struct cio2_queue *q;
  1196. if (cio2->queue[s_asd->csi2.port].sensor)
  1197. return -EBUSY;
  1198. q = &cio2->queue[s_asd->csi2.port];
  1199. q->csi2 = s_asd->csi2;
  1200. q->sensor = sd;
  1201. q->csi_rx_base = cio2->base + CIO2_REG_PIPE_BASE(q->csi2.port);
  1202. return 0;
  1203. }
  1204. /* The .unbind callback */
  1205. static void cio2_notifier_unbind(struct v4l2_async_notifier *notifier,
  1206. struct v4l2_subdev *sd,
  1207. struct v4l2_async_subdev *asd)
  1208. {
  1209. struct cio2_device *cio2 = container_of(notifier,
  1210. struct cio2_device, notifier);
  1211. struct sensor_async_subdev *s_asd = container_of(asd,
  1212. struct sensor_async_subdev, asd);
  1213. cio2->queue[s_asd->csi2.port].sensor = NULL;
  1214. }
  1215. /* .complete() is called after all subdevices have been located */
  1216. static int cio2_notifier_complete(struct v4l2_async_notifier *notifier)
  1217. {
  1218. struct cio2_device *cio2 = container_of(notifier, struct cio2_device,
  1219. notifier);
  1220. struct sensor_async_subdev *s_asd;
  1221. struct cio2_queue *q;
  1222. unsigned int i, pad;
  1223. int ret;
  1224. for (i = 0; i < notifier->num_subdevs; i++) {
  1225. s_asd = container_of(cio2->notifier.subdevs[i],
  1226. struct sensor_async_subdev, asd);
  1227. q = &cio2->queue[s_asd->csi2.port];
  1228. for (pad = 0; pad < q->sensor->entity.num_pads; pad++)
  1229. if (q->sensor->entity.pads[pad].flags &
  1230. MEDIA_PAD_FL_SOURCE)
  1231. break;
  1232. if (pad == q->sensor->entity.num_pads) {
  1233. dev_err(&cio2->pci_dev->dev,
  1234. "failed to find src pad for %s\n",
  1235. q->sensor->name);
  1236. return -ENXIO;
  1237. }
  1238. ret = media_create_pad_link(
  1239. &q->sensor->entity, pad,
  1240. &q->subdev.entity, CIO2_PAD_SINK,
  1241. 0);
  1242. if (ret) {
  1243. dev_err(&cio2->pci_dev->dev,
  1244. "failed to create link for %s\n",
  1245. cio2->queue[i].sensor->name);
  1246. return ret;
  1247. }
  1248. }
  1249. return v4l2_device_register_subdev_nodes(&cio2->v4l2_dev);
  1250. }
  1251. static const struct v4l2_async_notifier_operations cio2_async_ops = {
  1252. .bound = cio2_notifier_bound,
  1253. .unbind = cio2_notifier_unbind,
  1254. .complete = cio2_notifier_complete,
  1255. };
  1256. static int cio2_fwnode_parse(struct device *dev,
  1257. struct v4l2_fwnode_endpoint *vep,
  1258. struct v4l2_async_subdev *asd)
  1259. {
  1260. struct sensor_async_subdev *s_asd =
  1261. container_of(asd, struct sensor_async_subdev, asd);
  1262. if (vep->bus_type != V4L2_MBUS_CSI2) {
  1263. dev_err(dev, "Only CSI2 bus type is currently supported\n");
  1264. return -EINVAL;
  1265. }
  1266. s_asd->csi2.port = vep->base.port;
  1267. s_asd->csi2.lanes = vep->bus.mipi_csi2.num_data_lanes;
  1268. return 0;
  1269. }
  1270. static int cio2_notifier_init(struct cio2_device *cio2)
  1271. {
  1272. int ret;
  1273. ret = v4l2_async_notifier_parse_fwnode_endpoints(
  1274. &cio2->pci_dev->dev, &cio2->notifier,
  1275. sizeof(struct sensor_async_subdev),
  1276. cio2_fwnode_parse);
  1277. if (ret < 0)
  1278. return ret;
  1279. if (!cio2->notifier.num_subdevs)
  1280. return -ENODEV; /* no endpoint */
  1281. cio2->notifier.ops = &cio2_async_ops;
  1282. ret = v4l2_async_notifier_register(&cio2->v4l2_dev, &cio2->notifier);
  1283. if (ret) {
  1284. dev_err(&cio2->pci_dev->dev,
  1285. "failed to register async notifier : %d\n", ret);
  1286. v4l2_async_notifier_cleanup(&cio2->notifier);
  1287. }
  1288. return ret;
  1289. }
  1290. static void cio2_notifier_exit(struct cio2_device *cio2)
  1291. {
  1292. v4l2_async_notifier_unregister(&cio2->notifier);
  1293. v4l2_async_notifier_cleanup(&cio2->notifier);
  1294. }
  1295. /**************** Queue initialization ****************/
  1296. static const struct media_entity_operations cio2_media_ops = {
  1297. .link_validate = v4l2_subdev_link_validate,
  1298. };
  1299. static const struct media_entity_operations cio2_video_entity_ops = {
  1300. .link_validate = cio2_video_link_validate,
  1301. };
  1302. static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
  1303. {
  1304. static const u32 default_width = 1936;
  1305. static const u32 default_height = 1096;
  1306. const struct ipu3_cio2_fmt dflt_fmt = formats[0];
  1307. struct video_device *vdev = &q->vdev;
  1308. struct vb2_queue *vbq = &q->vbq;
  1309. struct v4l2_subdev *subdev = &q->subdev;
  1310. struct v4l2_mbus_framefmt *fmt;
  1311. int r;
  1312. /* Initialize miscellaneous variables */
  1313. mutex_init(&q->lock);
  1314. /* Initialize formats to default values */
  1315. fmt = &q->subdev_fmt;
  1316. fmt->width = default_width;
  1317. fmt->height = default_height;
  1318. fmt->code = dflt_fmt.mbus_code;
  1319. fmt->field = V4L2_FIELD_NONE;
  1320. q->format.width = default_width;
  1321. q->format.height = default_height;
  1322. q->format.pixelformat = dflt_fmt.fourcc;
  1323. q->format.colorspace = V4L2_COLORSPACE_RAW;
  1324. q->format.field = V4L2_FIELD_NONE;
  1325. q->format.num_planes = 1;
  1326. q->format.plane_fmt[0].bytesperline =
  1327. cio2_bytesperline(q->format.width);
  1328. q->format.plane_fmt[0].sizeimage = q->format.plane_fmt[0].bytesperline *
  1329. q->format.height;
  1330. /* Initialize fbpt */
  1331. r = cio2_fbpt_init(cio2, q);
  1332. if (r)
  1333. goto fail_fbpt;
  1334. /* Initialize media entities */
  1335. q->subdev_pads[CIO2_PAD_SINK].flags = MEDIA_PAD_FL_SINK |
  1336. MEDIA_PAD_FL_MUST_CONNECT;
  1337. q->subdev_pads[CIO2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1338. subdev->entity.ops = &cio2_media_ops;
  1339. subdev->internal_ops = &cio2_subdev_internal_ops;
  1340. r = media_entity_pads_init(&subdev->entity, CIO2_PADS, q->subdev_pads);
  1341. if (r) {
  1342. dev_err(&cio2->pci_dev->dev,
  1343. "failed initialize subdev media entity (%d)\n", r);
  1344. goto fail_subdev_media_entity;
  1345. }
  1346. q->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
  1347. vdev->entity.ops = &cio2_video_entity_ops;
  1348. r = media_entity_pads_init(&vdev->entity, 1, &q->vdev_pad);
  1349. if (r) {
  1350. dev_err(&cio2->pci_dev->dev,
  1351. "failed initialize videodev media entity (%d)\n", r);
  1352. goto fail_vdev_media_entity;
  1353. }
  1354. /* Initialize subdev */
  1355. v4l2_subdev_init(subdev, &cio2_subdev_ops);
  1356. subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1357. subdev->owner = THIS_MODULE;
  1358. snprintf(subdev->name, sizeof(subdev->name),
  1359. CIO2_ENTITY_NAME " %td", q - cio2->queue);
  1360. v4l2_set_subdevdata(subdev, cio2);
  1361. r = v4l2_device_register_subdev(&cio2->v4l2_dev, subdev);
  1362. if (r) {
  1363. dev_err(&cio2->pci_dev->dev,
  1364. "failed initialize subdev (%d)\n", r);
  1365. goto fail_subdev;
  1366. }
  1367. /* Initialize vbq */
  1368. vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1369. vbq->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF;
  1370. vbq->ops = &cio2_vb2_ops;
  1371. vbq->mem_ops = &vb2_dma_sg_memops;
  1372. vbq->buf_struct_size = sizeof(struct cio2_buffer);
  1373. vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1374. vbq->min_buffers_needed = 1;
  1375. vbq->drv_priv = cio2;
  1376. vbq->lock = &q->lock;
  1377. r = vb2_queue_init(vbq);
  1378. if (r) {
  1379. dev_err(&cio2->pci_dev->dev,
  1380. "failed to initialize videobuf2 queue (%d)\n", r);
  1381. goto fail_vbq;
  1382. }
  1383. /* Initialize vdev */
  1384. snprintf(vdev->name, sizeof(vdev->name),
  1385. "%s %td", CIO2_NAME, q - cio2->queue);
  1386. vdev->release = video_device_release_empty;
  1387. vdev->fops = &cio2_v4l2_fops;
  1388. vdev->ioctl_ops = &cio2_v4l2_ioctl_ops;
  1389. vdev->lock = &cio2->lock;
  1390. vdev->v4l2_dev = &cio2->v4l2_dev;
  1391. vdev->queue = &q->vbq;
  1392. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING;
  1393. video_set_drvdata(vdev, cio2);
  1394. r = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1395. if (r) {
  1396. dev_err(&cio2->pci_dev->dev,
  1397. "failed to register video device (%d)\n", r);
  1398. goto fail_vdev;
  1399. }
  1400. /* Create link from CIO2 subdev to output node */
  1401. r = media_create_pad_link(
  1402. &subdev->entity, CIO2_PAD_SOURCE, &vdev->entity, 0,
  1403. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
  1404. if (r)
  1405. goto fail_link;
  1406. return 0;
  1407. fail_link:
  1408. video_unregister_device(&q->vdev);
  1409. fail_vdev:
  1410. vb2_queue_release(vbq);
  1411. fail_vbq:
  1412. v4l2_device_unregister_subdev(subdev);
  1413. fail_subdev:
  1414. media_entity_cleanup(&vdev->entity);
  1415. fail_vdev_media_entity:
  1416. media_entity_cleanup(&subdev->entity);
  1417. fail_subdev_media_entity:
  1418. cio2_fbpt_exit(q, &cio2->pci_dev->dev);
  1419. fail_fbpt:
  1420. mutex_destroy(&q->lock);
  1421. return r;
  1422. }
  1423. static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q)
  1424. {
  1425. video_unregister_device(&q->vdev);
  1426. media_entity_cleanup(&q->vdev.entity);
  1427. vb2_queue_release(&q->vbq);
  1428. v4l2_device_unregister_subdev(&q->subdev);
  1429. media_entity_cleanup(&q->subdev.entity);
  1430. cio2_fbpt_exit(q, &cio2->pci_dev->dev);
  1431. mutex_destroy(&q->lock);
  1432. }
  1433. static int cio2_queues_init(struct cio2_device *cio2)
  1434. {
  1435. int i, r;
  1436. for (i = 0; i < CIO2_QUEUES; i++) {
  1437. r = cio2_queue_init(cio2, &cio2->queue[i]);
  1438. if (r)
  1439. break;
  1440. }
  1441. if (i == CIO2_QUEUES)
  1442. return 0;
  1443. for (i--; i >= 0; i--)
  1444. cio2_queue_exit(cio2, &cio2->queue[i]);
  1445. return r;
  1446. }
  1447. static void cio2_queues_exit(struct cio2_device *cio2)
  1448. {
  1449. unsigned int i;
  1450. for (i = 0; i < CIO2_QUEUES; i++)
  1451. cio2_queue_exit(cio2, &cio2->queue[i]);
  1452. }
  1453. /**************** PCI interface ****************/
  1454. static int cio2_pci_config_setup(struct pci_dev *dev)
  1455. {
  1456. u16 pci_command;
  1457. int r = pci_enable_msi(dev);
  1458. if (r) {
  1459. dev_err(&dev->dev, "failed to enable MSI (%d)\n", r);
  1460. return r;
  1461. }
  1462. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1463. pci_command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  1464. PCI_COMMAND_INTX_DISABLE;
  1465. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1466. return 0;
  1467. }
  1468. static int cio2_pci_probe(struct pci_dev *pci_dev,
  1469. const struct pci_device_id *id)
  1470. {
  1471. struct cio2_device *cio2;
  1472. void __iomem *const *iomap;
  1473. int r;
  1474. cio2 = devm_kzalloc(&pci_dev->dev, sizeof(*cio2), GFP_KERNEL);
  1475. if (!cio2)
  1476. return -ENOMEM;
  1477. cio2->pci_dev = pci_dev;
  1478. r = pcim_enable_device(pci_dev);
  1479. if (r) {
  1480. dev_err(&pci_dev->dev, "failed to enable device (%d)\n", r);
  1481. return r;
  1482. }
  1483. dev_info(&pci_dev->dev, "device 0x%x (rev: 0x%x)\n",
  1484. pci_dev->device, pci_dev->revision);
  1485. r = pcim_iomap_regions(pci_dev, 1 << CIO2_PCI_BAR, pci_name(pci_dev));
  1486. if (r) {
  1487. dev_err(&pci_dev->dev, "failed to remap I/O memory (%d)\n", r);
  1488. return -ENODEV;
  1489. }
  1490. iomap = pcim_iomap_table(pci_dev);
  1491. if (!iomap) {
  1492. dev_err(&pci_dev->dev, "failed to iomap table\n");
  1493. return -ENODEV;
  1494. }
  1495. cio2->base = iomap[CIO2_PCI_BAR];
  1496. pci_set_drvdata(pci_dev, cio2);
  1497. pci_set_master(pci_dev);
  1498. r = pci_set_dma_mask(pci_dev, CIO2_DMA_MASK);
  1499. if (r) {
  1500. dev_err(&pci_dev->dev, "failed to set DMA mask (%d)\n", r);
  1501. return -ENODEV;
  1502. }
  1503. r = cio2_pci_config_setup(pci_dev);
  1504. if (r)
  1505. return -ENODEV;
  1506. r = cio2_fbpt_init_dummy(cio2);
  1507. if (r)
  1508. return r;
  1509. mutex_init(&cio2->lock);
  1510. cio2->media_dev.dev = &cio2->pci_dev->dev;
  1511. strlcpy(cio2->media_dev.model, CIO2_DEVICE_NAME,
  1512. sizeof(cio2->media_dev.model));
  1513. snprintf(cio2->media_dev.bus_info, sizeof(cio2->media_dev.bus_info),
  1514. "PCI:%s", pci_name(cio2->pci_dev));
  1515. cio2->media_dev.hw_revision = 0;
  1516. media_device_init(&cio2->media_dev);
  1517. r = media_device_register(&cio2->media_dev);
  1518. if (r < 0)
  1519. goto fail_mutex_destroy;
  1520. cio2->v4l2_dev.mdev = &cio2->media_dev;
  1521. r = v4l2_device_register(&pci_dev->dev, &cio2->v4l2_dev);
  1522. if (r) {
  1523. dev_err(&pci_dev->dev,
  1524. "failed to register V4L2 device (%d)\n", r);
  1525. goto fail_media_device_unregister;
  1526. }
  1527. r = cio2_queues_init(cio2);
  1528. if (r)
  1529. goto fail_v4l2_device_unregister;
  1530. /* Register notifier for subdevices we care */
  1531. r = cio2_notifier_init(cio2);
  1532. if (r)
  1533. goto fail_cio2_queue_exit;
  1534. r = devm_request_irq(&pci_dev->dev, pci_dev->irq, cio2_irq,
  1535. IRQF_SHARED, CIO2_NAME, cio2);
  1536. if (r) {
  1537. dev_err(&pci_dev->dev, "failed to request IRQ (%d)\n", r);
  1538. goto fail;
  1539. }
  1540. pm_runtime_put_noidle(&pci_dev->dev);
  1541. pm_runtime_allow(&pci_dev->dev);
  1542. return 0;
  1543. fail:
  1544. cio2_notifier_exit(cio2);
  1545. fail_cio2_queue_exit:
  1546. cio2_queues_exit(cio2);
  1547. fail_v4l2_device_unregister:
  1548. v4l2_device_unregister(&cio2->v4l2_dev);
  1549. fail_media_device_unregister:
  1550. media_device_unregister(&cio2->media_dev);
  1551. media_device_cleanup(&cio2->media_dev);
  1552. fail_mutex_destroy:
  1553. mutex_destroy(&cio2->lock);
  1554. cio2_fbpt_exit_dummy(cio2);
  1555. return r;
  1556. }
  1557. static void cio2_pci_remove(struct pci_dev *pci_dev)
  1558. {
  1559. struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
  1560. unsigned int i;
  1561. media_device_unregister(&cio2->media_dev);
  1562. cio2_notifier_exit(cio2);
  1563. for (i = 0; i < CIO2_QUEUES; i++)
  1564. cio2_queue_exit(cio2, &cio2->queue[i]);
  1565. cio2_fbpt_exit_dummy(cio2);
  1566. v4l2_device_unregister(&cio2->v4l2_dev);
  1567. media_device_cleanup(&cio2->media_dev);
  1568. mutex_destroy(&cio2->lock);
  1569. }
  1570. static int __maybe_unused cio2_runtime_suspend(struct device *dev)
  1571. {
  1572. struct pci_dev *pci_dev = to_pci_dev(dev);
  1573. struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
  1574. void __iomem *const base = cio2->base;
  1575. u16 pm;
  1576. writel(CIO2_D0I3C_I3, base + CIO2_REG_D0I3C);
  1577. dev_dbg(dev, "cio2 runtime suspend.\n");
  1578. pci_read_config_word(pci_dev, pci_dev->pm_cap + CIO2_PMCSR_OFFSET, &pm);
  1579. pm = (pm >> CIO2_PMCSR_D0D3_SHIFT) << CIO2_PMCSR_D0D3_SHIFT;
  1580. pm |= CIO2_PMCSR_D3;
  1581. pci_write_config_word(pci_dev, pci_dev->pm_cap + CIO2_PMCSR_OFFSET, pm);
  1582. return 0;
  1583. }
  1584. static int __maybe_unused cio2_runtime_resume(struct device *dev)
  1585. {
  1586. struct pci_dev *pci_dev = to_pci_dev(dev);
  1587. struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
  1588. void __iomem *const base = cio2->base;
  1589. u16 pm;
  1590. writel(CIO2_D0I3C_RR, base + CIO2_REG_D0I3C);
  1591. dev_dbg(dev, "cio2 runtime resume.\n");
  1592. pci_read_config_word(pci_dev, pci_dev->pm_cap + CIO2_PMCSR_OFFSET, &pm);
  1593. pm = (pm >> CIO2_PMCSR_D0D3_SHIFT) << CIO2_PMCSR_D0D3_SHIFT;
  1594. pci_write_config_word(pci_dev, pci_dev->pm_cap + CIO2_PMCSR_OFFSET, pm);
  1595. return 0;
  1596. }
  1597. /*
  1598. * Helper function to advance all the elements of a circular buffer by "start"
  1599. * positions
  1600. */
  1601. static void arrange(void *ptr, size_t elem_size, size_t elems, size_t start)
  1602. {
  1603. struct {
  1604. size_t begin, end;
  1605. } arr[2] = {
  1606. { 0, start - 1 },
  1607. { start, elems - 1 },
  1608. };
  1609. #define CHUNK_SIZE(a) ((a)->end - (a)->begin + 1)
  1610. /* Loop as long as we have out-of-place entries */
  1611. while (CHUNK_SIZE(&arr[0]) && CHUNK_SIZE(&arr[1])) {
  1612. size_t size0, i;
  1613. /*
  1614. * Find the number of entries that can be arranged on this
  1615. * iteration.
  1616. */
  1617. size0 = min(CHUNK_SIZE(&arr[0]), CHUNK_SIZE(&arr[1]));
  1618. /* Swap the entries in two parts of the array. */
  1619. for (i = 0; i < size0; i++) {
  1620. u8 *d = ptr + elem_size * (arr[1].begin + i);
  1621. u8 *s = ptr + elem_size * (arr[0].begin + i);
  1622. size_t j;
  1623. for (j = 0; j < elem_size; j++)
  1624. swap(d[j], s[j]);
  1625. }
  1626. if (CHUNK_SIZE(&arr[0]) > CHUNK_SIZE(&arr[1])) {
  1627. /* The end of the first array remains unarranged. */
  1628. arr[0].begin += size0;
  1629. } else {
  1630. /*
  1631. * The first array is fully arranged so we proceed
  1632. * handling the next one.
  1633. */
  1634. arr[0].begin = arr[1].begin;
  1635. arr[0].end = arr[1].begin + size0 - 1;
  1636. arr[1].begin += size0;
  1637. }
  1638. }
  1639. }
  1640. static void cio2_fbpt_rearrange(struct cio2_device *cio2, struct cio2_queue *q)
  1641. {
  1642. unsigned int i, j;
  1643. for (i = 0, j = q->bufs_first; i < CIO2_MAX_BUFFERS;
  1644. i++, j = (j + 1) % CIO2_MAX_BUFFERS)
  1645. if (q->bufs[j])
  1646. break;
  1647. if (i == CIO2_MAX_BUFFERS)
  1648. return;
  1649. if (j) {
  1650. arrange(q->fbpt, sizeof(struct cio2_fbpt_entry) * CIO2_MAX_LOPS,
  1651. CIO2_MAX_BUFFERS, j);
  1652. arrange(q->bufs, sizeof(struct cio2_buffer *),
  1653. CIO2_MAX_BUFFERS, j);
  1654. }
  1655. /*
  1656. * DMA clears the valid bit when accessing the buffer.
  1657. * When stopping stream in suspend callback, some of the buffers
  1658. * may be in invalid state. After resume, when DMA meets the invalid
  1659. * buffer, it will halt and stop receiving new data.
  1660. * To avoid DMA halting, set the valid bit for all buffers in FBPT.
  1661. */
  1662. for (i = 0; i < CIO2_MAX_BUFFERS; i++)
  1663. cio2_fbpt_entry_enable(cio2, q->fbpt + i * CIO2_MAX_LOPS);
  1664. }
  1665. static int __maybe_unused cio2_suspend(struct device *dev)
  1666. {
  1667. struct pci_dev *pci_dev = to_pci_dev(dev);
  1668. struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
  1669. struct cio2_queue *q = cio2->cur_queue;
  1670. dev_dbg(dev, "cio2 suspend\n");
  1671. if (!cio2->streaming)
  1672. return 0;
  1673. /* Stop stream */
  1674. cio2_hw_exit(cio2, q);
  1675. synchronize_irq(pci_dev->irq);
  1676. pm_runtime_force_suspend(dev);
  1677. /*
  1678. * Upon resume, hw starts to process the fbpt entries from beginning,
  1679. * so relocate the queued buffs to the fbpt head before suspend.
  1680. */
  1681. cio2_fbpt_rearrange(cio2, q);
  1682. q->bufs_first = 0;
  1683. q->bufs_next = 0;
  1684. return 0;
  1685. }
  1686. static int __maybe_unused cio2_resume(struct device *dev)
  1687. {
  1688. struct pci_dev *pci_dev = to_pci_dev(dev);
  1689. struct cio2_device *cio2 = pci_get_drvdata(pci_dev);
  1690. int r = 0;
  1691. struct cio2_queue *q = cio2->cur_queue;
  1692. dev_dbg(dev, "cio2 resume\n");
  1693. if (!cio2->streaming)
  1694. return 0;
  1695. /* Start stream */
  1696. r = pm_runtime_force_resume(&cio2->pci_dev->dev);
  1697. if (r < 0) {
  1698. dev_err(&cio2->pci_dev->dev,
  1699. "failed to set power %d\n", r);
  1700. return r;
  1701. }
  1702. r = cio2_hw_init(cio2, q);
  1703. if (r)
  1704. dev_err(dev, "fail to init cio2 hw\n");
  1705. return r;
  1706. }
  1707. static const struct dev_pm_ops cio2_pm_ops = {
  1708. SET_RUNTIME_PM_OPS(&cio2_runtime_suspend, &cio2_runtime_resume, NULL)
  1709. SET_SYSTEM_SLEEP_PM_OPS(&cio2_suspend, &cio2_resume)
  1710. };
  1711. static const struct pci_device_id cio2_pci_id_table[] = {
  1712. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, CIO2_PCI_ID) },
  1713. { 0 }
  1714. };
  1715. MODULE_DEVICE_TABLE(pci, cio2_pci_id_table);
  1716. static struct pci_driver cio2_pci_driver = {
  1717. .name = CIO2_NAME,
  1718. .id_table = cio2_pci_id_table,
  1719. .probe = cio2_pci_probe,
  1720. .remove = cio2_pci_remove,
  1721. .driver = {
  1722. .pm = &cio2_pm_ops,
  1723. },
  1724. };
  1725. module_pci_driver(cio2_pci_driver);
  1726. MODULE_AUTHOR("Tuukka Toivonen <tuukka.toivonen@intel.com>");
  1727. MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
  1728. MODULE_AUTHOR("Jian Xu Zheng <jian.xu.zheng@intel.com>");
  1729. MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
  1730. MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
  1731. MODULE_LICENSE("GPL v2");
  1732. MODULE_DESCRIPTION("IPU3 CIO2 driver");