ipu-common.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
  3. * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/export.h>
  17. #include <linux/types.h>
  18. #include <linux/reset.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/err.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/list.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqchip/chained_irq.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_graph.h>
  32. #include <drm/drm_fourcc.h>
  33. #include <video/imx-ipu-v3.h>
  34. #include "ipu-prv.h"
  35. static inline u32 ipu_cm_read(struct ipu_soc *ipu, unsigned offset)
  36. {
  37. return readl(ipu->cm_reg + offset);
  38. }
  39. static inline void ipu_cm_write(struct ipu_soc *ipu, u32 value, unsigned offset)
  40. {
  41. writel(value, ipu->cm_reg + offset);
  42. }
  43. void ipu_srm_dp_sync_update(struct ipu_soc *ipu)
  44. {
  45. u32 val;
  46. val = ipu_cm_read(ipu, IPU_SRM_PRI2);
  47. val |= 0x8;
  48. ipu_cm_write(ipu, val, IPU_SRM_PRI2);
  49. }
  50. EXPORT_SYMBOL_GPL(ipu_srm_dp_sync_update);
  51. enum ipu_color_space ipu_drm_fourcc_to_colorspace(u32 drm_fourcc)
  52. {
  53. switch (drm_fourcc) {
  54. case DRM_FORMAT_ARGB1555:
  55. case DRM_FORMAT_ABGR1555:
  56. case DRM_FORMAT_RGBA5551:
  57. case DRM_FORMAT_BGRA5551:
  58. case DRM_FORMAT_RGB565:
  59. case DRM_FORMAT_BGR565:
  60. case DRM_FORMAT_RGB888:
  61. case DRM_FORMAT_BGR888:
  62. case DRM_FORMAT_ARGB4444:
  63. case DRM_FORMAT_XRGB8888:
  64. case DRM_FORMAT_XBGR8888:
  65. case DRM_FORMAT_RGBX8888:
  66. case DRM_FORMAT_BGRX8888:
  67. case DRM_FORMAT_ARGB8888:
  68. case DRM_FORMAT_ABGR8888:
  69. case DRM_FORMAT_RGBA8888:
  70. case DRM_FORMAT_BGRA8888:
  71. return IPUV3_COLORSPACE_RGB;
  72. case DRM_FORMAT_YUYV:
  73. case DRM_FORMAT_UYVY:
  74. case DRM_FORMAT_YUV420:
  75. case DRM_FORMAT_YVU420:
  76. case DRM_FORMAT_YUV422:
  77. case DRM_FORMAT_YVU422:
  78. case DRM_FORMAT_NV12:
  79. case DRM_FORMAT_NV21:
  80. case DRM_FORMAT_NV16:
  81. case DRM_FORMAT_NV61:
  82. return IPUV3_COLORSPACE_YUV;
  83. default:
  84. return IPUV3_COLORSPACE_UNKNOWN;
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace);
  88. enum ipu_color_space ipu_pixelformat_to_colorspace(u32 pixelformat)
  89. {
  90. switch (pixelformat) {
  91. case V4L2_PIX_FMT_YUV420:
  92. case V4L2_PIX_FMT_YVU420:
  93. case V4L2_PIX_FMT_YUV422P:
  94. case V4L2_PIX_FMT_UYVY:
  95. case V4L2_PIX_FMT_YUYV:
  96. case V4L2_PIX_FMT_NV12:
  97. case V4L2_PIX_FMT_NV21:
  98. case V4L2_PIX_FMT_NV16:
  99. case V4L2_PIX_FMT_NV61:
  100. return IPUV3_COLORSPACE_YUV;
  101. case V4L2_PIX_FMT_RGB32:
  102. case V4L2_PIX_FMT_BGR32:
  103. case V4L2_PIX_FMT_RGB24:
  104. case V4L2_PIX_FMT_BGR24:
  105. case V4L2_PIX_FMT_RGB565:
  106. return IPUV3_COLORSPACE_RGB;
  107. default:
  108. return IPUV3_COLORSPACE_UNKNOWN;
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace);
  112. bool ipu_pixelformat_is_planar(u32 pixelformat)
  113. {
  114. switch (pixelformat) {
  115. case V4L2_PIX_FMT_YUV420:
  116. case V4L2_PIX_FMT_YVU420:
  117. case V4L2_PIX_FMT_YUV422P:
  118. case V4L2_PIX_FMT_NV12:
  119. case V4L2_PIX_FMT_NV21:
  120. case V4L2_PIX_FMT_NV16:
  121. case V4L2_PIX_FMT_NV61:
  122. return true;
  123. }
  124. return false;
  125. }
  126. EXPORT_SYMBOL_GPL(ipu_pixelformat_is_planar);
  127. enum ipu_color_space ipu_mbus_code_to_colorspace(u32 mbus_code)
  128. {
  129. switch (mbus_code & 0xf000) {
  130. case 0x1000:
  131. return IPUV3_COLORSPACE_RGB;
  132. case 0x2000:
  133. return IPUV3_COLORSPACE_YUV;
  134. default:
  135. return IPUV3_COLORSPACE_UNKNOWN;
  136. }
  137. }
  138. EXPORT_SYMBOL_GPL(ipu_mbus_code_to_colorspace);
  139. int ipu_stride_to_bytes(u32 pixel_stride, u32 pixelformat)
  140. {
  141. switch (pixelformat) {
  142. case V4L2_PIX_FMT_YUV420:
  143. case V4L2_PIX_FMT_YVU420:
  144. case V4L2_PIX_FMT_YUV422P:
  145. case V4L2_PIX_FMT_NV12:
  146. case V4L2_PIX_FMT_NV21:
  147. case V4L2_PIX_FMT_NV16:
  148. case V4L2_PIX_FMT_NV61:
  149. /*
  150. * for the planar YUV formats, the stride passed to
  151. * cpmem must be the stride in bytes of the Y plane.
  152. * And all the planar YUV formats have an 8-bit
  153. * Y component.
  154. */
  155. return (8 * pixel_stride) >> 3;
  156. case V4L2_PIX_FMT_RGB565:
  157. case V4L2_PIX_FMT_YUYV:
  158. case V4L2_PIX_FMT_UYVY:
  159. return (16 * pixel_stride) >> 3;
  160. case V4L2_PIX_FMT_BGR24:
  161. case V4L2_PIX_FMT_RGB24:
  162. return (24 * pixel_stride) >> 3;
  163. case V4L2_PIX_FMT_BGR32:
  164. case V4L2_PIX_FMT_RGB32:
  165. return (32 * pixel_stride) >> 3;
  166. default:
  167. break;
  168. }
  169. return -EINVAL;
  170. }
  171. EXPORT_SYMBOL_GPL(ipu_stride_to_bytes);
  172. int ipu_degrees_to_rot_mode(enum ipu_rotate_mode *mode, int degrees,
  173. bool hflip, bool vflip)
  174. {
  175. u32 r90, vf, hf;
  176. switch (degrees) {
  177. case 0:
  178. vf = hf = r90 = 0;
  179. break;
  180. case 90:
  181. vf = hf = 0;
  182. r90 = 1;
  183. break;
  184. case 180:
  185. vf = hf = 1;
  186. r90 = 0;
  187. break;
  188. case 270:
  189. vf = hf = r90 = 1;
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. hf ^= (u32)hflip;
  195. vf ^= (u32)vflip;
  196. *mode = (enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(ipu_degrees_to_rot_mode);
  200. int ipu_rot_mode_to_degrees(int *degrees, enum ipu_rotate_mode mode,
  201. bool hflip, bool vflip)
  202. {
  203. u32 r90, vf, hf;
  204. r90 = ((u32)mode >> 2) & 0x1;
  205. hf = ((u32)mode >> 1) & 0x1;
  206. vf = ((u32)mode >> 0) & 0x1;
  207. hf ^= (u32)hflip;
  208. vf ^= (u32)vflip;
  209. switch ((enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf)) {
  210. case IPU_ROTATE_NONE:
  211. *degrees = 0;
  212. break;
  213. case IPU_ROTATE_90_RIGHT:
  214. *degrees = 90;
  215. break;
  216. case IPU_ROTATE_180:
  217. *degrees = 180;
  218. break;
  219. case IPU_ROTATE_90_LEFT:
  220. *degrees = 270;
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(ipu_rot_mode_to_degrees);
  228. struct ipuv3_channel *ipu_idmac_get(struct ipu_soc *ipu, unsigned num)
  229. {
  230. struct ipuv3_channel *channel;
  231. dev_dbg(ipu->dev, "%s %d\n", __func__, num);
  232. if (num > 63)
  233. return ERR_PTR(-ENODEV);
  234. mutex_lock(&ipu->channel_lock);
  235. channel = &ipu->channel[num];
  236. if (channel->busy) {
  237. channel = ERR_PTR(-EBUSY);
  238. goto out;
  239. }
  240. channel->busy = true;
  241. channel->num = num;
  242. out:
  243. mutex_unlock(&ipu->channel_lock);
  244. return channel;
  245. }
  246. EXPORT_SYMBOL_GPL(ipu_idmac_get);
  247. void ipu_idmac_put(struct ipuv3_channel *channel)
  248. {
  249. struct ipu_soc *ipu = channel->ipu;
  250. dev_dbg(ipu->dev, "%s %d\n", __func__, channel->num);
  251. mutex_lock(&ipu->channel_lock);
  252. channel->busy = false;
  253. mutex_unlock(&ipu->channel_lock);
  254. }
  255. EXPORT_SYMBOL_GPL(ipu_idmac_put);
  256. #define idma_mask(ch) (1 << ((ch) & 0x1f))
  257. /*
  258. * This is an undocumented feature, a write one to a channel bit in
  259. * IPU_CHA_CUR_BUF and IPU_CHA_TRIPLE_CUR_BUF will reset the channel's
  260. * internal current buffer pointer so that transfers start from buffer
  261. * 0 on the next channel enable (that's the theory anyway, the imx6 TRM
  262. * only says these are read-only registers). This operation is required
  263. * for channel linking to work correctly, for instance video capture
  264. * pipelines that carry out image rotations will fail after the first
  265. * streaming unless this function is called for each channel before
  266. * re-enabling the channels.
  267. */
  268. static void __ipu_idmac_reset_current_buffer(struct ipuv3_channel *channel)
  269. {
  270. struct ipu_soc *ipu = channel->ipu;
  271. unsigned int chno = channel->num;
  272. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_CUR_BUF(chno));
  273. }
  274. void ipu_idmac_set_double_buffer(struct ipuv3_channel *channel,
  275. bool doublebuffer)
  276. {
  277. struct ipu_soc *ipu = channel->ipu;
  278. unsigned long flags;
  279. u32 reg;
  280. spin_lock_irqsave(&ipu->lock, flags);
  281. reg = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
  282. if (doublebuffer)
  283. reg |= idma_mask(channel->num);
  284. else
  285. reg &= ~idma_mask(channel->num);
  286. ipu_cm_write(ipu, reg, IPU_CHA_DB_MODE_SEL(channel->num));
  287. __ipu_idmac_reset_current_buffer(channel);
  288. spin_unlock_irqrestore(&ipu->lock, flags);
  289. }
  290. EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer);
  291. static const struct {
  292. int chnum;
  293. u32 reg;
  294. int shift;
  295. } idmac_lock_en_info[] = {
  296. { .chnum = 5, .reg = IDMAC_CH_LOCK_EN_1, .shift = 0, },
  297. { .chnum = 11, .reg = IDMAC_CH_LOCK_EN_1, .shift = 2, },
  298. { .chnum = 12, .reg = IDMAC_CH_LOCK_EN_1, .shift = 4, },
  299. { .chnum = 14, .reg = IDMAC_CH_LOCK_EN_1, .shift = 6, },
  300. { .chnum = 15, .reg = IDMAC_CH_LOCK_EN_1, .shift = 8, },
  301. { .chnum = 20, .reg = IDMAC_CH_LOCK_EN_1, .shift = 10, },
  302. { .chnum = 21, .reg = IDMAC_CH_LOCK_EN_1, .shift = 12, },
  303. { .chnum = 22, .reg = IDMAC_CH_LOCK_EN_1, .shift = 14, },
  304. { .chnum = 23, .reg = IDMAC_CH_LOCK_EN_1, .shift = 16, },
  305. { .chnum = 27, .reg = IDMAC_CH_LOCK_EN_1, .shift = 18, },
  306. { .chnum = 28, .reg = IDMAC_CH_LOCK_EN_1, .shift = 20, },
  307. { .chnum = 45, .reg = IDMAC_CH_LOCK_EN_2, .shift = 0, },
  308. { .chnum = 46, .reg = IDMAC_CH_LOCK_EN_2, .shift = 2, },
  309. { .chnum = 47, .reg = IDMAC_CH_LOCK_EN_2, .shift = 4, },
  310. { .chnum = 48, .reg = IDMAC_CH_LOCK_EN_2, .shift = 6, },
  311. { .chnum = 49, .reg = IDMAC_CH_LOCK_EN_2, .shift = 8, },
  312. { .chnum = 50, .reg = IDMAC_CH_LOCK_EN_2, .shift = 10, },
  313. };
  314. int ipu_idmac_lock_enable(struct ipuv3_channel *channel, int num_bursts)
  315. {
  316. struct ipu_soc *ipu = channel->ipu;
  317. unsigned long flags;
  318. u32 bursts, regval;
  319. int i;
  320. switch (num_bursts) {
  321. case 0:
  322. case 1:
  323. bursts = 0x00; /* locking disabled */
  324. break;
  325. case 2:
  326. bursts = 0x01;
  327. break;
  328. case 4:
  329. bursts = 0x02;
  330. break;
  331. case 8:
  332. bursts = 0x03;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. for (i = 0; i < ARRAY_SIZE(idmac_lock_en_info); i++) {
  338. if (channel->num == idmac_lock_en_info[i].chnum)
  339. break;
  340. }
  341. if (i >= ARRAY_SIZE(idmac_lock_en_info))
  342. return -EINVAL;
  343. spin_lock_irqsave(&ipu->lock, flags);
  344. regval = ipu_idmac_read(ipu, idmac_lock_en_info[i].reg);
  345. regval &= ~(0x03 << idmac_lock_en_info[i].shift);
  346. regval |= (bursts << idmac_lock_en_info[i].shift);
  347. ipu_idmac_write(ipu, regval, idmac_lock_en_info[i].reg);
  348. spin_unlock_irqrestore(&ipu->lock, flags);
  349. return 0;
  350. }
  351. EXPORT_SYMBOL_GPL(ipu_idmac_lock_enable);
  352. int ipu_module_enable(struct ipu_soc *ipu, u32 mask)
  353. {
  354. unsigned long lock_flags;
  355. u32 val;
  356. spin_lock_irqsave(&ipu->lock, lock_flags);
  357. val = ipu_cm_read(ipu, IPU_DISP_GEN);
  358. if (mask & IPU_CONF_DI0_EN)
  359. val |= IPU_DI0_COUNTER_RELEASE;
  360. if (mask & IPU_CONF_DI1_EN)
  361. val |= IPU_DI1_COUNTER_RELEASE;
  362. ipu_cm_write(ipu, val, IPU_DISP_GEN);
  363. val = ipu_cm_read(ipu, IPU_CONF);
  364. val |= mask;
  365. ipu_cm_write(ipu, val, IPU_CONF);
  366. spin_unlock_irqrestore(&ipu->lock, lock_flags);
  367. return 0;
  368. }
  369. EXPORT_SYMBOL_GPL(ipu_module_enable);
  370. int ipu_module_disable(struct ipu_soc *ipu, u32 mask)
  371. {
  372. unsigned long lock_flags;
  373. u32 val;
  374. spin_lock_irqsave(&ipu->lock, lock_flags);
  375. val = ipu_cm_read(ipu, IPU_CONF);
  376. val &= ~mask;
  377. ipu_cm_write(ipu, val, IPU_CONF);
  378. val = ipu_cm_read(ipu, IPU_DISP_GEN);
  379. if (mask & IPU_CONF_DI0_EN)
  380. val &= ~IPU_DI0_COUNTER_RELEASE;
  381. if (mask & IPU_CONF_DI1_EN)
  382. val &= ~IPU_DI1_COUNTER_RELEASE;
  383. ipu_cm_write(ipu, val, IPU_DISP_GEN);
  384. spin_unlock_irqrestore(&ipu->lock, lock_flags);
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(ipu_module_disable);
  388. int ipu_idmac_get_current_buffer(struct ipuv3_channel *channel)
  389. {
  390. struct ipu_soc *ipu = channel->ipu;
  391. unsigned int chno = channel->num;
  392. return (ipu_cm_read(ipu, IPU_CHA_CUR_BUF(chno)) & idma_mask(chno)) ? 1 : 0;
  393. }
  394. EXPORT_SYMBOL_GPL(ipu_idmac_get_current_buffer);
  395. bool ipu_idmac_buffer_is_ready(struct ipuv3_channel *channel, u32 buf_num)
  396. {
  397. struct ipu_soc *ipu = channel->ipu;
  398. unsigned long flags;
  399. u32 reg = 0;
  400. spin_lock_irqsave(&ipu->lock, flags);
  401. switch (buf_num) {
  402. case 0:
  403. reg = ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num));
  404. break;
  405. case 1:
  406. reg = ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num));
  407. break;
  408. case 2:
  409. reg = ipu_cm_read(ipu, IPU_CHA_BUF2_RDY(channel->num));
  410. break;
  411. }
  412. spin_unlock_irqrestore(&ipu->lock, flags);
  413. return ((reg & idma_mask(channel->num)) != 0);
  414. }
  415. EXPORT_SYMBOL_GPL(ipu_idmac_buffer_is_ready);
  416. void ipu_idmac_select_buffer(struct ipuv3_channel *channel, u32 buf_num)
  417. {
  418. struct ipu_soc *ipu = channel->ipu;
  419. unsigned int chno = channel->num;
  420. unsigned long flags;
  421. spin_lock_irqsave(&ipu->lock, flags);
  422. /* Mark buffer as ready. */
  423. if (buf_num == 0)
  424. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
  425. else
  426. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
  427. spin_unlock_irqrestore(&ipu->lock, flags);
  428. }
  429. EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer);
  430. void ipu_idmac_clear_buffer(struct ipuv3_channel *channel, u32 buf_num)
  431. {
  432. struct ipu_soc *ipu = channel->ipu;
  433. unsigned int chno = channel->num;
  434. unsigned long flags;
  435. spin_lock_irqsave(&ipu->lock, flags);
  436. ipu_cm_write(ipu, 0xF0300000, IPU_GPR); /* write one to clear */
  437. switch (buf_num) {
  438. case 0:
  439. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
  440. break;
  441. case 1:
  442. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
  443. break;
  444. case 2:
  445. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF2_RDY(chno));
  446. break;
  447. default:
  448. break;
  449. }
  450. ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
  451. spin_unlock_irqrestore(&ipu->lock, flags);
  452. }
  453. EXPORT_SYMBOL_GPL(ipu_idmac_clear_buffer);
  454. int ipu_idmac_enable_channel(struct ipuv3_channel *channel)
  455. {
  456. struct ipu_soc *ipu = channel->ipu;
  457. u32 val;
  458. unsigned long flags;
  459. spin_lock_irqsave(&ipu->lock, flags);
  460. val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
  461. val |= idma_mask(channel->num);
  462. ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
  463. spin_unlock_irqrestore(&ipu->lock, flags);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel);
  467. bool ipu_idmac_channel_busy(struct ipu_soc *ipu, unsigned int chno)
  468. {
  469. return (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(chno)) & idma_mask(chno));
  470. }
  471. EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy);
  472. int ipu_idmac_wait_busy(struct ipuv3_channel *channel, int ms)
  473. {
  474. struct ipu_soc *ipu = channel->ipu;
  475. unsigned long timeout;
  476. timeout = jiffies + msecs_to_jiffies(ms);
  477. while (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(channel->num)) &
  478. idma_mask(channel->num)) {
  479. if (time_after(jiffies, timeout))
  480. return -ETIMEDOUT;
  481. cpu_relax();
  482. }
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy);
  486. int ipu_wait_interrupt(struct ipu_soc *ipu, int irq, int ms)
  487. {
  488. unsigned long timeout;
  489. timeout = jiffies + msecs_to_jiffies(ms);
  490. ipu_cm_write(ipu, BIT(irq % 32), IPU_INT_STAT(irq / 32));
  491. while (!(ipu_cm_read(ipu, IPU_INT_STAT(irq / 32) & BIT(irq % 32)))) {
  492. if (time_after(jiffies, timeout))
  493. return -ETIMEDOUT;
  494. cpu_relax();
  495. }
  496. return 0;
  497. }
  498. EXPORT_SYMBOL_GPL(ipu_wait_interrupt);
  499. int ipu_idmac_disable_channel(struct ipuv3_channel *channel)
  500. {
  501. struct ipu_soc *ipu = channel->ipu;
  502. u32 val;
  503. unsigned long flags;
  504. spin_lock_irqsave(&ipu->lock, flags);
  505. /* Disable DMA channel(s) */
  506. val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
  507. val &= ~idma_mask(channel->num);
  508. ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
  509. __ipu_idmac_reset_current_buffer(channel);
  510. /* Set channel buffers NOT to be ready */
  511. ipu_cm_write(ipu, 0xf0000000, IPU_GPR); /* write one to clear */
  512. if (ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num)) &
  513. idma_mask(channel->num)) {
  514. ipu_cm_write(ipu, idma_mask(channel->num),
  515. IPU_CHA_BUF0_RDY(channel->num));
  516. }
  517. if (ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num)) &
  518. idma_mask(channel->num)) {
  519. ipu_cm_write(ipu, idma_mask(channel->num),
  520. IPU_CHA_BUF1_RDY(channel->num));
  521. }
  522. ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
  523. /* Reset the double buffer */
  524. val = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
  525. val &= ~idma_mask(channel->num);
  526. ipu_cm_write(ipu, val, IPU_CHA_DB_MODE_SEL(channel->num));
  527. spin_unlock_irqrestore(&ipu->lock, flags);
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel);
  531. /*
  532. * The imx6 rev. D TRM says that enabling the WM feature will increase
  533. * a channel's priority. Refer to Table 36-8 Calculated priority value.
  534. * The sub-module that is the sink or source for the channel must enable
  535. * watermark signal for this to take effect (SMFC_WM for instance).
  536. */
  537. void ipu_idmac_enable_watermark(struct ipuv3_channel *channel, bool enable)
  538. {
  539. struct ipu_soc *ipu = channel->ipu;
  540. unsigned long flags;
  541. u32 val;
  542. spin_lock_irqsave(&ipu->lock, flags);
  543. val = ipu_idmac_read(ipu, IDMAC_WM_EN(channel->num));
  544. if (enable)
  545. val |= 1 << (channel->num % 32);
  546. else
  547. val &= ~(1 << (channel->num % 32));
  548. ipu_idmac_write(ipu, val, IDMAC_WM_EN(channel->num));
  549. spin_unlock_irqrestore(&ipu->lock, flags);
  550. }
  551. EXPORT_SYMBOL_GPL(ipu_idmac_enable_watermark);
  552. static int ipu_memory_reset(struct ipu_soc *ipu)
  553. {
  554. unsigned long timeout;
  555. ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
  556. timeout = jiffies + msecs_to_jiffies(1000);
  557. while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
  558. if (time_after(jiffies, timeout))
  559. return -ETIME;
  560. cpu_relax();
  561. }
  562. return 0;
  563. }
  564. /*
  565. * Set the source mux for the given CSI. Selects either parallel or
  566. * MIPI CSI2 sources.
  567. */
  568. void ipu_set_csi_src_mux(struct ipu_soc *ipu, int csi_id, bool mipi_csi2)
  569. {
  570. unsigned long flags;
  571. u32 val, mask;
  572. mask = (csi_id == 1) ? IPU_CONF_CSI1_DATA_SOURCE :
  573. IPU_CONF_CSI0_DATA_SOURCE;
  574. spin_lock_irqsave(&ipu->lock, flags);
  575. val = ipu_cm_read(ipu, IPU_CONF);
  576. if (mipi_csi2)
  577. val |= mask;
  578. else
  579. val &= ~mask;
  580. ipu_cm_write(ipu, val, IPU_CONF);
  581. spin_unlock_irqrestore(&ipu->lock, flags);
  582. }
  583. EXPORT_SYMBOL_GPL(ipu_set_csi_src_mux);
  584. /*
  585. * Set the source mux for the IC. Selects either CSI[01] or the VDI.
  586. */
  587. void ipu_set_ic_src_mux(struct ipu_soc *ipu, int csi_id, bool vdi)
  588. {
  589. unsigned long flags;
  590. u32 val;
  591. spin_lock_irqsave(&ipu->lock, flags);
  592. val = ipu_cm_read(ipu, IPU_CONF);
  593. if (vdi) {
  594. val |= IPU_CONF_IC_INPUT;
  595. } else {
  596. val &= ~IPU_CONF_IC_INPUT;
  597. if (csi_id == 1)
  598. val |= IPU_CONF_CSI_SEL;
  599. else
  600. val &= ~IPU_CONF_CSI_SEL;
  601. }
  602. ipu_cm_write(ipu, val, IPU_CONF);
  603. spin_unlock_irqrestore(&ipu->lock, flags);
  604. }
  605. EXPORT_SYMBOL_GPL(ipu_set_ic_src_mux);
  606. struct ipu_devtype {
  607. const char *name;
  608. unsigned long cm_ofs;
  609. unsigned long cpmem_ofs;
  610. unsigned long srm_ofs;
  611. unsigned long tpm_ofs;
  612. unsigned long csi0_ofs;
  613. unsigned long csi1_ofs;
  614. unsigned long ic_ofs;
  615. unsigned long disp0_ofs;
  616. unsigned long disp1_ofs;
  617. unsigned long dc_tmpl_ofs;
  618. unsigned long vdi_ofs;
  619. enum ipuv3_type type;
  620. };
  621. static struct ipu_devtype ipu_type_imx51 = {
  622. .name = "IPUv3EX",
  623. .cm_ofs = 0x1e000000,
  624. .cpmem_ofs = 0x1f000000,
  625. .srm_ofs = 0x1f040000,
  626. .tpm_ofs = 0x1f060000,
  627. .csi0_ofs = 0x1e030000,
  628. .csi1_ofs = 0x1e038000,
  629. .ic_ofs = 0x1e020000,
  630. .disp0_ofs = 0x1e040000,
  631. .disp1_ofs = 0x1e048000,
  632. .dc_tmpl_ofs = 0x1f080000,
  633. .vdi_ofs = 0x1e068000,
  634. .type = IPUV3EX,
  635. };
  636. static struct ipu_devtype ipu_type_imx53 = {
  637. .name = "IPUv3M",
  638. .cm_ofs = 0x06000000,
  639. .cpmem_ofs = 0x07000000,
  640. .srm_ofs = 0x07040000,
  641. .tpm_ofs = 0x07060000,
  642. .csi0_ofs = 0x06030000,
  643. .csi1_ofs = 0x06038000,
  644. .ic_ofs = 0x06020000,
  645. .disp0_ofs = 0x06040000,
  646. .disp1_ofs = 0x06048000,
  647. .dc_tmpl_ofs = 0x07080000,
  648. .vdi_ofs = 0x06068000,
  649. .type = IPUV3M,
  650. };
  651. static struct ipu_devtype ipu_type_imx6q = {
  652. .name = "IPUv3H",
  653. .cm_ofs = 0x00200000,
  654. .cpmem_ofs = 0x00300000,
  655. .srm_ofs = 0x00340000,
  656. .tpm_ofs = 0x00360000,
  657. .csi0_ofs = 0x00230000,
  658. .csi1_ofs = 0x00238000,
  659. .ic_ofs = 0x00220000,
  660. .disp0_ofs = 0x00240000,
  661. .disp1_ofs = 0x00248000,
  662. .dc_tmpl_ofs = 0x00380000,
  663. .vdi_ofs = 0x00268000,
  664. .type = IPUV3H,
  665. };
  666. static const struct of_device_id imx_ipu_dt_ids[] = {
  667. { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
  668. { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
  669. { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
  670. { /* sentinel */ }
  671. };
  672. MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
  673. static int ipu_submodules_init(struct ipu_soc *ipu,
  674. struct platform_device *pdev, unsigned long ipu_base,
  675. struct clk *ipu_clk)
  676. {
  677. char *unit;
  678. int ret;
  679. struct device *dev = &pdev->dev;
  680. const struct ipu_devtype *devtype = ipu->devtype;
  681. ret = ipu_cpmem_init(ipu, dev, ipu_base + devtype->cpmem_ofs);
  682. if (ret) {
  683. unit = "cpmem";
  684. goto err_cpmem;
  685. }
  686. ret = ipu_csi_init(ipu, dev, 0, ipu_base + devtype->csi0_ofs,
  687. IPU_CONF_CSI0_EN, ipu_clk);
  688. if (ret) {
  689. unit = "csi0";
  690. goto err_csi_0;
  691. }
  692. ret = ipu_csi_init(ipu, dev, 1, ipu_base + devtype->csi1_ofs,
  693. IPU_CONF_CSI1_EN, ipu_clk);
  694. if (ret) {
  695. unit = "csi1";
  696. goto err_csi_1;
  697. }
  698. ret = ipu_ic_init(ipu, dev,
  699. ipu_base + devtype->ic_ofs,
  700. ipu_base + devtype->tpm_ofs);
  701. if (ret) {
  702. unit = "ic";
  703. goto err_ic;
  704. }
  705. ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
  706. IPU_CONF_DI0_EN, ipu_clk);
  707. if (ret) {
  708. unit = "di0";
  709. goto err_di_0;
  710. }
  711. ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
  712. IPU_CONF_DI1_EN, ipu_clk);
  713. if (ret) {
  714. unit = "di1";
  715. goto err_di_1;
  716. }
  717. ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
  718. IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
  719. if (ret) {
  720. unit = "dc_template";
  721. goto err_dc;
  722. }
  723. ret = ipu_dmfc_init(ipu, dev, ipu_base +
  724. devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
  725. if (ret) {
  726. unit = "dmfc";
  727. goto err_dmfc;
  728. }
  729. ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
  730. if (ret) {
  731. unit = "dp";
  732. goto err_dp;
  733. }
  734. ret = ipu_smfc_init(ipu, dev, ipu_base +
  735. devtype->cm_ofs + IPU_CM_SMFC_REG_OFS);
  736. if (ret) {
  737. unit = "smfc";
  738. goto err_smfc;
  739. }
  740. return 0;
  741. err_smfc:
  742. ipu_dp_exit(ipu);
  743. err_dp:
  744. ipu_dmfc_exit(ipu);
  745. err_dmfc:
  746. ipu_dc_exit(ipu);
  747. err_dc:
  748. ipu_di_exit(ipu, 1);
  749. err_di_1:
  750. ipu_di_exit(ipu, 0);
  751. err_di_0:
  752. ipu_ic_exit(ipu);
  753. err_ic:
  754. ipu_csi_exit(ipu, 1);
  755. err_csi_1:
  756. ipu_csi_exit(ipu, 0);
  757. err_csi_0:
  758. ipu_cpmem_exit(ipu);
  759. err_cpmem:
  760. dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
  761. return ret;
  762. }
  763. static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
  764. {
  765. unsigned long status;
  766. int i, bit, irq;
  767. for (i = 0; i < num_regs; i++) {
  768. status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
  769. status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
  770. for_each_set_bit(bit, &status, 32) {
  771. irq = irq_linear_revmap(ipu->domain,
  772. regs[i] * 32 + bit);
  773. if (irq)
  774. generic_handle_irq(irq);
  775. }
  776. }
  777. }
  778. static void ipu_irq_handler(struct irq_desc *desc)
  779. {
  780. struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
  781. struct irq_chip *chip = irq_desc_get_chip(desc);
  782. const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
  783. chained_irq_enter(chip, desc);
  784. ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
  785. chained_irq_exit(chip, desc);
  786. }
  787. static void ipu_err_irq_handler(struct irq_desc *desc)
  788. {
  789. struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
  790. struct irq_chip *chip = irq_desc_get_chip(desc);
  791. const int int_reg[] = { 4, 5, 8, 9};
  792. chained_irq_enter(chip, desc);
  793. ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
  794. chained_irq_exit(chip, desc);
  795. }
  796. int ipu_map_irq(struct ipu_soc *ipu, int irq)
  797. {
  798. int virq;
  799. virq = irq_linear_revmap(ipu->domain, irq);
  800. if (!virq)
  801. virq = irq_create_mapping(ipu->domain, irq);
  802. return virq;
  803. }
  804. EXPORT_SYMBOL_GPL(ipu_map_irq);
  805. int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
  806. enum ipu_channel_irq irq_type)
  807. {
  808. return ipu_map_irq(ipu, irq_type + channel->num);
  809. }
  810. EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
  811. static void ipu_submodules_exit(struct ipu_soc *ipu)
  812. {
  813. ipu_smfc_exit(ipu);
  814. ipu_dp_exit(ipu);
  815. ipu_dmfc_exit(ipu);
  816. ipu_dc_exit(ipu);
  817. ipu_di_exit(ipu, 1);
  818. ipu_di_exit(ipu, 0);
  819. ipu_ic_exit(ipu);
  820. ipu_csi_exit(ipu, 1);
  821. ipu_csi_exit(ipu, 0);
  822. ipu_cpmem_exit(ipu);
  823. }
  824. static int platform_remove_devices_fn(struct device *dev, void *unused)
  825. {
  826. struct platform_device *pdev = to_platform_device(dev);
  827. platform_device_unregister(pdev);
  828. return 0;
  829. }
  830. static void platform_device_unregister_children(struct platform_device *pdev)
  831. {
  832. device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
  833. }
  834. struct ipu_platform_reg {
  835. struct ipu_client_platformdata pdata;
  836. const char *name;
  837. };
  838. /* These must be in the order of the corresponding device tree port nodes */
  839. static struct ipu_platform_reg client_reg[] = {
  840. {
  841. .pdata = {
  842. .csi = 0,
  843. .dma[0] = IPUV3_CHANNEL_CSI0,
  844. .dma[1] = -EINVAL,
  845. },
  846. .name = "imx-ipuv3-camera",
  847. }, {
  848. .pdata = {
  849. .csi = 1,
  850. .dma[0] = IPUV3_CHANNEL_CSI1,
  851. .dma[1] = -EINVAL,
  852. },
  853. .name = "imx-ipuv3-camera",
  854. }, {
  855. .pdata = {
  856. .di = 0,
  857. .dc = 5,
  858. .dp = IPU_DP_FLOW_SYNC_BG,
  859. .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
  860. .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
  861. },
  862. .name = "imx-ipuv3-crtc",
  863. }, {
  864. .pdata = {
  865. .di = 1,
  866. .dc = 1,
  867. .dp = -EINVAL,
  868. .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
  869. .dma[1] = -EINVAL,
  870. },
  871. .name = "imx-ipuv3-crtc",
  872. },
  873. };
  874. static DEFINE_MUTEX(ipu_client_id_mutex);
  875. static int ipu_client_id;
  876. static int ipu_add_client_devices(struct ipu_soc *ipu, unsigned long ipu_base)
  877. {
  878. struct device *dev = ipu->dev;
  879. unsigned i;
  880. int id, ret;
  881. mutex_lock(&ipu_client_id_mutex);
  882. id = ipu_client_id;
  883. ipu_client_id += ARRAY_SIZE(client_reg);
  884. mutex_unlock(&ipu_client_id_mutex);
  885. for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
  886. struct ipu_platform_reg *reg = &client_reg[i];
  887. struct platform_device *pdev;
  888. struct device_node *of_node;
  889. /* Associate subdevice with the corresponding port node */
  890. of_node = of_graph_get_port_by_id(dev->of_node, i);
  891. if (!of_node) {
  892. dev_info(dev,
  893. "no port@%d node in %s, not using %s%d\n",
  894. i, dev->of_node->full_name,
  895. (i / 2) ? "DI" : "CSI", i % 2);
  896. continue;
  897. }
  898. pdev = platform_device_alloc(reg->name, id++);
  899. if (!pdev) {
  900. ret = -ENOMEM;
  901. goto err_register;
  902. }
  903. pdev->dev.parent = dev;
  904. reg->pdata.of_node = of_node;
  905. ret = platform_device_add_data(pdev, &reg->pdata,
  906. sizeof(reg->pdata));
  907. if (!ret)
  908. ret = platform_device_add(pdev);
  909. if (ret) {
  910. platform_device_put(pdev);
  911. goto err_register;
  912. }
  913. /*
  914. * Set of_node only after calling platform_device_add. Otherwise
  915. * the platform:imx-ipuv3-crtc modalias won't be used.
  916. */
  917. pdev->dev.of_node = of_node;
  918. }
  919. return 0;
  920. err_register:
  921. platform_device_unregister_children(to_platform_device(dev));
  922. return ret;
  923. }
  924. static int ipu_irq_init(struct ipu_soc *ipu)
  925. {
  926. struct irq_chip_generic *gc;
  927. struct irq_chip_type *ct;
  928. unsigned long unused[IPU_NUM_IRQS / 32] = {
  929. 0x400100d0, 0xffe000fd,
  930. 0x400100d0, 0xffe000fd,
  931. 0x400100d0, 0xffe000fd,
  932. 0x4077ffff, 0xffe7e1fd,
  933. 0x23fffffe, 0x8880fff0,
  934. 0xf98fe7d0, 0xfff81fff,
  935. 0x400100d0, 0xffe000fd,
  936. 0x00000000,
  937. };
  938. int ret, i;
  939. ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
  940. &irq_generic_chip_ops, ipu);
  941. if (!ipu->domain) {
  942. dev_err(ipu->dev, "failed to add irq domain\n");
  943. return -ENODEV;
  944. }
  945. ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
  946. handle_level_irq, 0, 0, 0);
  947. if (ret < 0) {
  948. dev_err(ipu->dev, "failed to alloc generic irq chips\n");
  949. irq_domain_remove(ipu->domain);
  950. return ret;
  951. }
  952. for (i = 0; i < IPU_NUM_IRQS; i += 32)
  953. ipu_cm_write(ipu, 0, IPU_INT_CTRL(i / 32));
  954. for (i = 0; i < IPU_NUM_IRQS; i += 32) {
  955. gc = irq_get_domain_generic_chip(ipu->domain, i);
  956. gc->reg_base = ipu->cm_reg;
  957. gc->unused = unused[i / 32];
  958. ct = gc->chip_types;
  959. ct->chip.irq_ack = irq_gc_ack_set_bit;
  960. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  961. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  962. ct->regs.ack = IPU_INT_STAT(i / 32);
  963. ct->regs.mask = IPU_INT_CTRL(i / 32);
  964. }
  965. irq_set_chained_handler_and_data(ipu->irq_sync, ipu_irq_handler, ipu);
  966. irq_set_chained_handler_and_data(ipu->irq_err, ipu_err_irq_handler,
  967. ipu);
  968. return 0;
  969. }
  970. static void ipu_irq_exit(struct ipu_soc *ipu)
  971. {
  972. int i, irq;
  973. irq_set_chained_handler_and_data(ipu->irq_err, NULL, NULL);
  974. irq_set_chained_handler_and_data(ipu->irq_sync, NULL, NULL);
  975. /* TODO: remove irq_domain_generic_chips */
  976. for (i = 0; i < IPU_NUM_IRQS; i++) {
  977. irq = irq_linear_revmap(ipu->domain, i);
  978. if (irq)
  979. irq_dispose_mapping(irq);
  980. }
  981. irq_domain_remove(ipu->domain);
  982. }
  983. void ipu_dump(struct ipu_soc *ipu)
  984. {
  985. int i;
  986. dev_dbg(ipu->dev, "IPU_CONF = \t0x%08X\n",
  987. ipu_cm_read(ipu, IPU_CONF));
  988. dev_dbg(ipu->dev, "IDMAC_CONF = \t0x%08X\n",
  989. ipu_idmac_read(ipu, IDMAC_CONF));
  990. dev_dbg(ipu->dev, "IDMAC_CHA_EN1 = \t0x%08X\n",
  991. ipu_idmac_read(ipu, IDMAC_CHA_EN(0)));
  992. dev_dbg(ipu->dev, "IDMAC_CHA_EN2 = \t0x%08X\n",
  993. ipu_idmac_read(ipu, IDMAC_CHA_EN(32)));
  994. dev_dbg(ipu->dev, "IDMAC_CHA_PRI1 = \t0x%08X\n",
  995. ipu_idmac_read(ipu, IDMAC_CHA_PRI(0)));
  996. dev_dbg(ipu->dev, "IDMAC_CHA_PRI2 = \t0x%08X\n",
  997. ipu_idmac_read(ipu, IDMAC_CHA_PRI(32)));
  998. dev_dbg(ipu->dev, "IDMAC_BAND_EN1 = \t0x%08X\n",
  999. ipu_idmac_read(ipu, IDMAC_BAND_EN(0)));
  1000. dev_dbg(ipu->dev, "IDMAC_BAND_EN2 = \t0x%08X\n",
  1001. ipu_idmac_read(ipu, IDMAC_BAND_EN(32)));
  1002. dev_dbg(ipu->dev, "IPU_CHA_DB_MODE_SEL0 = \t0x%08X\n",
  1003. ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(0)));
  1004. dev_dbg(ipu->dev, "IPU_CHA_DB_MODE_SEL1 = \t0x%08X\n",
  1005. ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(32)));
  1006. dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW1 = \t0x%08X\n",
  1007. ipu_cm_read(ipu, IPU_FS_PROC_FLOW1));
  1008. dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW2 = \t0x%08X\n",
  1009. ipu_cm_read(ipu, IPU_FS_PROC_FLOW2));
  1010. dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW3 = \t0x%08X\n",
  1011. ipu_cm_read(ipu, IPU_FS_PROC_FLOW3));
  1012. dev_dbg(ipu->dev, "IPU_FS_DISP_FLOW1 = \t0x%08X\n",
  1013. ipu_cm_read(ipu, IPU_FS_DISP_FLOW1));
  1014. for (i = 0; i < 15; i++)
  1015. dev_dbg(ipu->dev, "IPU_INT_CTRL(%d) = \t%08X\n", i,
  1016. ipu_cm_read(ipu, IPU_INT_CTRL(i)));
  1017. }
  1018. EXPORT_SYMBOL_GPL(ipu_dump);
  1019. static int ipu_probe(struct platform_device *pdev)
  1020. {
  1021. const struct of_device_id *of_id =
  1022. of_match_device(imx_ipu_dt_ids, &pdev->dev);
  1023. struct ipu_soc *ipu;
  1024. struct resource *res;
  1025. unsigned long ipu_base;
  1026. int i, ret, irq_sync, irq_err;
  1027. const struct ipu_devtype *devtype;
  1028. devtype = of_id->data;
  1029. irq_sync = platform_get_irq(pdev, 0);
  1030. irq_err = platform_get_irq(pdev, 1);
  1031. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1032. dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
  1033. irq_sync, irq_err);
  1034. if (!res || irq_sync < 0 || irq_err < 0)
  1035. return -ENODEV;
  1036. ipu_base = res->start;
  1037. ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
  1038. if (!ipu)
  1039. return -ENODEV;
  1040. for (i = 0; i < 64; i++)
  1041. ipu->channel[i].ipu = ipu;
  1042. ipu->devtype = devtype;
  1043. ipu->ipu_type = devtype->type;
  1044. spin_lock_init(&ipu->lock);
  1045. mutex_init(&ipu->channel_lock);
  1046. dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
  1047. ipu_base + devtype->cm_ofs);
  1048. dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
  1049. ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
  1050. dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
  1051. ipu_base + devtype->cpmem_ofs);
  1052. dev_dbg(&pdev->dev, "csi0: 0x%08lx\n",
  1053. ipu_base + devtype->csi0_ofs);
  1054. dev_dbg(&pdev->dev, "csi1: 0x%08lx\n",
  1055. ipu_base + devtype->csi1_ofs);
  1056. dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
  1057. ipu_base + devtype->ic_ofs);
  1058. dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
  1059. ipu_base + devtype->disp0_ofs);
  1060. dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
  1061. ipu_base + devtype->disp1_ofs);
  1062. dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
  1063. ipu_base + devtype->srm_ofs);
  1064. dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
  1065. ipu_base + devtype->tpm_ofs);
  1066. dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
  1067. ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
  1068. dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
  1069. ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
  1070. dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
  1071. ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
  1072. dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
  1073. ipu_base + devtype->vdi_ofs);
  1074. ipu->cm_reg = devm_ioremap(&pdev->dev,
  1075. ipu_base + devtype->cm_ofs, PAGE_SIZE);
  1076. ipu->idmac_reg = devm_ioremap(&pdev->dev,
  1077. ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
  1078. PAGE_SIZE);
  1079. if (!ipu->cm_reg || !ipu->idmac_reg)
  1080. return -ENOMEM;
  1081. ipu->clk = devm_clk_get(&pdev->dev, "bus");
  1082. if (IS_ERR(ipu->clk)) {
  1083. ret = PTR_ERR(ipu->clk);
  1084. dev_err(&pdev->dev, "clk_get failed with %d", ret);
  1085. return ret;
  1086. }
  1087. platform_set_drvdata(pdev, ipu);
  1088. ret = clk_prepare_enable(ipu->clk);
  1089. if (ret) {
  1090. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  1091. return ret;
  1092. }
  1093. ipu->dev = &pdev->dev;
  1094. ipu->irq_sync = irq_sync;
  1095. ipu->irq_err = irq_err;
  1096. ret = ipu_irq_init(ipu);
  1097. if (ret)
  1098. goto out_failed_irq;
  1099. ret = device_reset(&pdev->dev);
  1100. if (ret) {
  1101. dev_err(&pdev->dev, "failed to reset: %d\n", ret);
  1102. goto out_failed_reset;
  1103. }
  1104. ret = ipu_memory_reset(ipu);
  1105. if (ret)
  1106. goto out_failed_reset;
  1107. /* Set MCU_T to divide MCU access window into 2 */
  1108. ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
  1109. IPU_DISP_GEN);
  1110. ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
  1111. if (ret)
  1112. goto failed_submodules_init;
  1113. ret = ipu_add_client_devices(ipu, ipu_base);
  1114. if (ret) {
  1115. dev_err(&pdev->dev, "adding client devices failed with %d\n",
  1116. ret);
  1117. goto failed_add_clients;
  1118. }
  1119. dev_info(&pdev->dev, "%s probed\n", devtype->name);
  1120. return 0;
  1121. failed_add_clients:
  1122. ipu_submodules_exit(ipu);
  1123. failed_submodules_init:
  1124. out_failed_reset:
  1125. ipu_irq_exit(ipu);
  1126. out_failed_irq:
  1127. clk_disable_unprepare(ipu->clk);
  1128. return ret;
  1129. }
  1130. static int ipu_remove(struct platform_device *pdev)
  1131. {
  1132. struct ipu_soc *ipu = platform_get_drvdata(pdev);
  1133. platform_device_unregister_children(pdev);
  1134. ipu_submodules_exit(ipu);
  1135. ipu_irq_exit(ipu);
  1136. clk_disable_unprepare(ipu->clk);
  1137. return 0;
  1138. }
  1139. static struct platform_driver imx_ipu_driver = {
  1140. .driver = {
  1141. .name = "imx-ipuv3",
  1142. .of_match_table = imx_ipu_dt_ids,
  1143. },
  1144. .probe = ipu_probe,
  1145. .remove = ipu_remove,
  1146. };
  1147. module_platform_driver(imx_ipu_driver);
  1148. MODULE_ALIAS("platform:imx-ipuv3");
  1149. MODULE_DESCRIPTION("i.MX IPU v3 driver");
  1150. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  1151. MODULE_LICENSE("GPL");