ipu_idmac.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008
  4. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  5. *
  6. * Copyright (C) 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/err.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/delay.h>
  14. #include <linux/list.h>
  15. #include <linux/clk.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/string.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/dma/ipu-dma.h>
  22. #include "../dmaengine.h"
  23. #include "ipu_intern.h"
  24. #define FS_VF_IN_VALID 0x00000002
  25. #define FS_ENC_IN_VALID 0x00000001
  26. static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
  27. bool wait_for_stop);
  28. /*
  29. * There can be only one, we could allocate it dynamically, but then we'd have
  30. * to add an extra parameter to some functions, and use something as ugly as
  31. * struct ipu *ipu = to_ipu(to_idmac(ichan->dma_chan.device));
  32. * in the ISR
  33. */
  34. static struct ipu ipu_data;
  35. #define to_ipu(id) container_of(id, struct ipu, idmac)
  36. static u32 __idmac_read_icreg(struct ipu *ipu, unsigned long reg)
  37. {
  38. return __raw_readl(ipu->reg_ic + reg);
  39. }
  40. #define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
  41. static void __idmac_write_icreg(struct ipu *ipu, u32 value, unsigned long reg)
  42. {
  43. __raw_writel(value, ipu->reg_ic + reg);
  44. }
  45. #define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
  46. static u32 idmac_read_ipureg(struct ipu *ipu, unsigned long reg)
  47. {
  48. return __raw_readl(ipu->reg_ipu + reg);
  49. }
  50. static void idmac_write_ipureg(struct ipu *ipu, u32 value, unsigned long reg)
  51. {
  52. __raw_writel(value, ipu->reg_ipu + reg);
  53. }
  54. /*****************************************************************************
  55. * IPU / IC common functions
  56. */
  57. static void dump_idmac_reg(struct ipu *ipu)
  58. {
  59. dev_dbg(ipu->dev, "IDMAC_CONF 0x%x, IC_CONF 0x%x, IDMAC_CHA_EN 0x%x, "
  60. "IDMAC_CHA_PRI 0x%x, IDMAC_CHA_BUSY 0x%x\n",
  61. idmac_read_icreg(ipu, IDMAC_CONF),
  62. idmac_read_icreg(ipu, IC_CONF),
  63. idmac_read_icreg(ipu, IDMAC_CHA_EN),
  64. idmac_read_icreg(ipu, IDMAC_CHA_PRI),
  65. idmac_read_icreg(ipu, IDMAC_CHA_BUSY));
  66. dev_dbg(ipu->dev, "BUF0_RDY 0x%x, BUF1_RDY 0x%x, CUR_BUF 0x%x, "
  67. "DB_MODE 0x%x, TASKS_STAT 0x%x\n",
  68. idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
  69. idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
  70. idmac_read_ipureg(ipu, IPU_CHA_CUR_BUF),
  71. idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL),
  72. idmac_read_ipureg(ipu, IPU_TASKS_STAT));
  73. }
  74. static uint32_t bytes_per_pixel(enum pixel_fmt fmt)
  75. {
  76. switch (fmt) {
  77. case IPU_PIX_FMT_GENERIC: /* generic data */
  78. case IPU_PIX_FMT_RGB332:
  79. case IPU_PIX_FMT_YUV420P:
  80. case IPU_PIX_FMT_YUV422P:
  81. default:
  82. return 1;
  83. case IPU_PIX_FMT_RGB565:
  84. case IPU_PIX_FMT_YUYV:
  85. case IPU_PIX_FMT_UYVY:
  86. return 2;
  87. case IPU_PIX_FMT_BGR24:
  88. case IPU_PIX_FMT_RGB24:
  89. return 3;
  90. case IPU_PIX_FMT_GENERIC_32: /* generic data */
  91. case IPU_PIX_FMT_BGR32:
  92. case IPU_PIX_FMT_RGB32:
  93. case IPU_PIX_FMT_ABGR32:
  94. return 4;
  95. }
  96. }
  97. /* Enable direct write to memory by the Camera Sensor Interface */
  98. static void ipu_ic_enable_task(struct ipu *ipu, enum ipu_channel channel)
  99. {
  100. uint32_t ic_conf, mask;
  101. switch (channel) {
  102. case IDMAC_IC_0:
  103. mask = IC_CONF_PRPENC_EN;
  104. break;
  105. case IDMAC_IC_7:
  106. mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
  107. break;
  108. default:
  109. return;
  110. }
  111. ic_conf = idmac_read_icreg(ipu, IC_CONF) | mask;
  112. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  113. }
  114. /* Called under spin_lock_irqsave(&ipu_data.lock) */
  115. static void ipu_ic_disable_task(struct ipu *ipu, enum ipu_channel channel)
  116. {
  117. uint32_t ic_conf, mask;
  118. switch (channel) {
  119. case IDMAC_IC_0:
  120. mask = IC_CONF_PRPENC_EN;
  121. break;
  122. case IDMAC_IC_7:
  123. mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
  124. break;
  125. default:
  126. return;
  127. }
  128. ic_conf = idmac_read_icreg(ipu, IC_CONF) & ~mask;
  129. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  130. }
  131. static uint32_t ipu_channel_status(struct ipu *ipu, enum ipu_channel channel)
  132. {
  133. uint32_t stat = TASK_STAT_IDLE;
  134. uint32_t task_stat_reg = idmac_read_ipureg(ipu, IPU_TASKS_STAT);
  135. switch (channel) {
  136. case IDMAC_IC_7:
  137. stat = (task_stat_reg & TSTAT_CSI2MEM_MASK) >>
  138. TSTAT_CSI2MEM_OFFSET;
  139. break;
  140. case IDMAC_IC_0:
  141. case IDMAC_SDC_0:
  142. case IDMAC_SDC_1:
  143. default:
  144. break;
  145. }
  146. return stat;
  147. }
  148. struct chan_param_mem_planar {
  149. /* Word 0 */
  150. u32 xv:10;
  151. u32 yv:10;
  152. u32 xb:12;
  153. u32 yb:12;
  154. u32 res1:2;
  155. u32 nsb:1;
  156. u32 lnpb:6;
  157. u32 ubo_l:11;
  158. u32 ubo_h:15;
  159. u32 vbo_l:17;
  160. u32 vbo_h:9;
  161. u32 res2:3;
  162. u32 fw:12;
  163. u32 fh_l:8;
  164. u32 fh_h:4;
  165. u32 res3:28;
  166. /* Word 1 */
  167. u32 eba0;
  168. u32 eba1;
  169. u32 bpp:3;
  170. u32 sl:14;
  171. u32 pfs:3;
  172. u32 bam:3;
  173. u32 res4:2;
  174. u32 npb:6;
  175. u32 res5:1;
  176. u32 sat:2;
  177. u32 res6:30;
  178. } __attribute__ ((packed));
  179. struct chan_param_mem_interleaved {
  180. /* Word 0 */
  181. u32 xv:10;
  182. u32 yv:10;
  183. u32 xb:12;
  184. u32 yb:12;
  185. u32 sce:1;
  186. u32 res1:1;
  187. u32 nsb:1;
  188. u32 lnpb:6;
  189. u32 sx:10;
  190. u32 sy_l:1;
  191. u32 sy_h:9;
  192. u32 ns:10;
  193. u32 sm:10;
  194. u32 sdx_l:3;
  195. u32 sdx_h:2;
  196. u32 sdy:5;
  197. u32 sdrx:1;
  198. u32 sdry:1;
  199. u32 sdr1:1;
  200. u32 res2:2;
  201. u32 fw:12;
  202. u32 fh_l:8;
  203. u32 fh_h:4;
  204. u32 res3:28;
  205. /* Word 1 */
  206. u32 eba0;
  207. u32 eba1;
  208. u32 bpp:3;
  209. u32 sl:14;
  210. u32 pfs:3;
  211. u32 bam:3;
  212. u32 res4:2;
  213. u32 npb:6;
  214. u32 res5:1;
  215. u32 sat:2;
  216. u32 scc:1;
  217. u32 ofs0:5;
  218. u32 ofs1:5;
  219. u32 ofs2:5;
  220. u32 ofs3:5;
  221. u32 wid0:3;
  222. u32 wid1:3;
  223. u32 wid2:3;
  224. u32 wid3:3;
  225. u32 dec_sel:1;
  226. u32 res6:28;
  227. } __attribute__ ((packed));
  228. union chan_param_mem {
  229. struct chan_param_mem_planar pp;
  230. struct chan_param_mem_interleaved ip;
  231. };
  232. static void ipu_ch_param_set_plane_offset(union chan_param_mem *params,
  233. u32 u_offset, u32 v_offset)
  234. {
  235. params->pp.ubo_l = u_offset & 0x7ff;
  236. params->pp.ubo_h = u_offset >> 11;
  237. params->pp.vbo_l = v_offset & 0x1ffff;
  238. params->pp.vbo_h = v_offset >> 17;
  239. }
  240. static void ipu_ch_param_set_size(union chan_param_mem *params,
  241. uint32_t pixel_fmt, uint16_t width,
  242. uint16_t height, uint16_t stride)
  243. {
  244. u32 u_offset;
  245. u32 v_offset;
  246. params->pp.fw = width - 1;
  247. params->pp.fh_l = height - 1;
  248. params->pp.fh_h = (height - 1) >> 8;
  249. params->pp.sl = stride - 1;
  250. switch (pixel_fmt) {
  251. case IPU_PIX_FMT_GENERIC:
  252. /*Represents 8-bit Generic data */
  253. params->pp.bpp = 3;
  254. params->pp.pfs = 7;
  255. params->pp.npb = 31;
  256. params->pp.sat = 2; /* SAT = use 32-bit access */
  257. break;
  258. case IPU_PIX_FMT_GENERIC_32:
  259. /*Represents 32-bit Generic data */
  260. params->pp.bpp = 0;
  261. params->pp.pfs = 7;
  262. params->pp.npb = 7;
  263. params->pp.sat = 2; /* SAT = use 32-bit access */
  264. break;
  265. case IPU_PIX_FMT_RGB565:
  266. params->ip.bpp = 2;
  267. params->ip.pfs = 4;
  268. params->ip.npb = 15;
  269. params->ip.sat = 2; /* SAT = 32-bit access */
  270. params->ip.ofs0 = 0; /* Red bit offset */
  271. params->ip.ofs1 = 5; /* Green bit offset */
  272. params->ip.ofs2 = 11; /* Blue bit offset */
  273. params->ip.ofs3 = 16; /* Alpha bit offset */
  274. params->ip.wid0 = 4; /* Red bit width - 1 */
  275. params->ip.wid1 = 5; /* Green bit width - 1 */
  276. params->ip.wid2 = 4; /* Blue bit width - 1 */
  277. break;
  278. case IPU_PIX_FMT_BGR24:
  279. params->ip.bpp = 1; /* 24 BPP & RGB PFS */
  280. params->ip.pfs = 4;
  281. params->ip.npb = 7;
  282. params->ip.sat = 2; /* SAT = 32-bit access */
  283. params->ip.ofs0 = 0; /* Red bit offset */
  284. params->ip.ofs1 = 8; /* Green bit offset */
  285. params->ip.ofs2 = 16; /* Blue bit offset */
  286. params->ip.ofs3 = 24; /* Alpha bit offset */
  287. params->ip.wid0 = 7; /* Red bit width - 1 */
  288. params->ip.wid1 = 7; /* Green bit width - 1 */
  289. params->ip.wid2 = 7; /* Blue bit width - 1 */
  290. break;
  291. case IPU_PIX_FMT_RGB24:
  292. params->ip.bpp = 1; /* 24 BPP & RGB PFS */
  293. params->ip.pfs = 4;
  294. params->ip.npb = 7;
  295. params->ip.sat = 2; /* SAT = 32-bit access */
  296. params->ip.ofs0 = 16; /* Red bit offset */
  297. params->ip.ofs1 = 8; /* Green bit offset */
  298. params->ip.ofs2 = 0; /* Blue bit offset */
  299. params->ip.ofs3 = 24; /* Alpha bit offset */
  300. params->ip.wid0 = 7; /* Red bit width - 1 */
  301. params->ip.wid1 = 7; /* Green bit width - 1 */
  302. params->ip.wid2 = 7; /* Blue bit width - 1 */
  303. break;
  304. case IPU_PIX_FMT_BGRA32:
  305. case IPU_PIX_FMT_BGR32:
  306. case IPU_PIX_FMT_ABGR32:
  307. params->ip.bpp = 0;
  308. params->ip.pfs = 4;
  309. params->ip.npb = 7;
  310. params->ip.sat = 2; /* SAT = 32-bit access */
  311. params->ip.ofs0 = 8; /* Red bit offset */
  312. params->ip.ofs1 = 16; /* Green bit offset */
  313. params->ip.ofs2 = 24; /* Blue bit offset */
  314. params->ip.ofs3 = 0; /* Alpha bit offset */
  315. params->ip.wid0 = 7; /* Red bit width - 1 */
  316. params->ip.wid1 = 7; /* Green bit width - 1 */
  317. params->ip.wid2 = 7; /* Blue bit width - 1 */
  318. params->ip.wid3 = 7; /* Alpha bit width - 1 */
  319. break;
  320. case IPU_PIX_FMT_RGBA32:
  321. case IPU_PIX_FMT_RGB32:
  322. params->ip.bpp = 0;
  323. params->ip.pfs = 4;
  324. params->ip.npb = 7;
  325. params->ip.sat = 2; /* SAT = 32-bit access */
  326. params->ip.ofs0 = 24; /* Red bit offset */
  327. params->ip.ofs1 = 16; /* Green bit offset */
  328. params->ip.ofs2 = 8; /* Blue bit offset */
  329. params->ip.ofs3 = 0; /* Alpha bit offset */
  330. params->ip.wid0 = 7; /* Red bit width - 1 */
  331. params->ip.wid1 = 7; /* Green bit width - 1 */
  332. params->ip.wid2 = 7; /* Blue bit width - 1 */
  333. params->ip.wid3 = 7; /* Alpha bit width - 1 */
  334. break;
  335. case IPU_PIX_FMT_UYVY:
  336. params->ip.bpp = 2;
  337. params->ip.pfs = 6;
  338. params->ip.npb = 7;
  339. params->ip.sat = 2; /* SAT = 32-bit access */
  340. break;
  341. case IPU_PIX_FMT_YUV420P2:
  342. case IPU_PIX_FMT_YUV420P:
  343. params->ip.bpp = 3;
  344. params->ip.pfs = 3;
  345. params->ip.npb = 7;
  346. params->ip.sat = 2; /* SAT = 32-bit access */
  347. u_offset = stride * height;
  348. v_offset = u_offset + u_offset / 4;
  349. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  350. break;
  351. case IPU_PIX_FMT_YVU422P:
  352. params->ip.bpp = 3;
  353. params->ip.pfs = 2;
  354. params->ip.npb = 7;
  355. params->ip.sat = 2; /* SAT = 32-bit access */
  356. v_offset = stride * height;
  357. u_offset = v_offset + v_offset / 2;
  358. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  359. break;
  360. case IPU_PIX_FMT_YUV422P:
  361. params->ip.bpp = 3;
  362. params->ip.pfs = 2;
  363. params->ip.npb = 7;
  364. params->ip.sat = 2; /* SAT = 32-bit access */
  365. u_offset = stride * height;
  366. v_offset = u_offset + u_offset / 2;
  367. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  368. break;
  369. default:
  370. dev_err(ipu_data.dev,
  371. "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
  372. break;
  373. }
  374. params->pp.nsb = 1;
  375. }
  376. static void ipu_ch_param_set_buffer(union chan_param_mem *params,
  377. dma_addr_t buf0, dma_addr_t buf1)
  378. {
  379. params->pp.eba0 = buf0;
  380. params->pp.eba1 = buf1;
  381. }
  382. static void ipu_ch_param_set_rotation(union chan_param_mem *params,
  383. enum ipu_rotate_mode rotate)
  384. {
  385. params->pp.bam = rotate;
  386. }
  387. static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
  388. uint32_t num_words)
  389. {
  390. for (; num_words > 0; num_words--) {
  391. dev_dbg(ipu_data.dev,
  392. "write param mem - addr = 0x%08X, data = 0x%08X\n",
  393. addr, *data);
  394. idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
  395. idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
  396. addr++;
  397. if ((addr & 0x7) == 5) {
  398. addr &= ~0x7; /* set to word 0 */
  399. addr += 8; /* increment to next row */
  400. }
  401. }
  402. }
  403. static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
  404. uint32_t *resize_coeff,
  405. uint32_t *downsize_coeff)
  406. {
  407. uint32_t temp_size;
  408. uint32_t temp_downsize;
  409. *resize_coeff = 1 << 13;
  410. *downsize_coeff = 1 << 13;
  411. /* Cannot downsize more than 8:1 */
  412. if (out_size << 3 < in_size)
  413. return -EINVAL;
  414. /* compute downsizing coefficient */
  415. temp_downsize = 0;
  416. temp_size = in_size;
  417. while (temp_size >= out_size * 2 && temp_downsize < 2) {
  418. temp_size >>= 1;
  419. temp_downsize++;
  420. }
  421. *downsize_coeff = temp_downsize;
  422. /*
  423. * compute resizing coefficient using the following formula:
  424. * resize_coeff = M*(SI -1)/(SO - 1)
  425. * where M = 2^13, SI - input size, SO - output size
  426. */
  427. *resize_coeff = (8192L * (temp_size - 1)) / (out_size - 1);
  428. if (*resize_coeff >= 16384L) {
  429. dev_err(ipu_data.dev, "Warning! Overflow on resize coeff.\n");
  430. *resize_coeff = 0x3FFF;
  431. }
  432. dev_dbg(ipu_data.dev, "resizing from %u -> %u pixels, "
  433. "downsize=%u, resize=%u.%lu (reg=%u)\n", in_size, out_size,
  434. *downsize_coeff, *resize_coeff >= 8192L ? 1 : 0,
  435. ((*resize_coeff & 0x1FFF) * 10000L) / 8192L, *resize_coeff);
  436. return 0;
  437. }
  438. static enum ipu_color_space format_to_colorspace(enum pixel_fmt fmt)
  439. {
  440. switch (fmt) {
  441. case IPU_PIX_FMT_RGB565:
  442. case IPU_PIX_FMT_BGR24:
  443. case IPU_PIX_FMT_RGB24:
  444. case IPU_PIX_FMT_BGR32:
  445. case IPU_PIX_FMT_RGB32:
  446. return IPU_COLORSPACE_RGB;
  447. default:
  448. return IPU_COLORSPACE_YCBCR;
  449. }
  450. }
  451. static int ipu_ic_init_prpenc(struct ipu *ipu,
  452. union ipu_channel_param *params, bool src_is_csi)
  453. {
  454. uint32_t reg, ic_conf;
  455. uint32_t downsize_coeff, resize_coeff;
  456. enum ipu_color_space in_fmt, out_fmt;
  457. /* Setup vertical resizing */
  458. calc_resize_coeffs(params->video.in_height,
  459. params->video.out_height,
  460. &resize_coeff, &downsize_coeff);
  461. reg = (downsize_coeff << 30) | (resize_coeff << 16);
  462. /* Setup horizontal resizing */
  463. calc_resize_coeffs(params->video.in_width,
  464. params->video.out_width,
  465. &resize_coeff, &downsize_coeff);
  466. reg |= (downsize_coeff << 14) | resize_coeff;
  467. /* Setup color space conversion */
  468. in_fmt = format_to_colorspace(params->video.in_pixel_fmt);
  469. out_fmt = format_to_colorspace(params->video.out_pixel_fmt);
  470. /*
  471. * Colourspace conversion unsupported yet - see _init_csc() in
  472. * Freescale sources
  473. */
  474. if (in_fmt != out_fmt) {
  475. dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
  476. return -EOPNOTSUPP;
  477. }
  478. idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
  479. ic_conf = idmac_read_icreg(ipu, IC_CONF);
  480. if (src_is_csi)
  481. ic_conf &= ~IC_CONF_RWS_EN;
  482. else
  483. ic_conf |= IC_CONF_RWS_EN;
  484. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  485. return 0;
  486. }
  487. static uint32_t dma_param_addr(uint32_t dma_ch)
  488. {
  489. /* Channel Parameter Memory */
  490. return 0x10000 | (dma_ch << 4);
  491. }
  492. static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
  493. bool prio)
  494. {
  495. u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
  496. if (prio)
  497. reg |= 1UL << channel;
  498. else
  499. reg &= ~(1UL << channel);
  500. idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
  501. dump_idmac_reg(ipu);
  502. }
  503. static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
  504. {
  505. uint32_t mask;
  506. switch (channel) {
  507. case IDMAC_IC_0:
  508. case IDMAC_IC_7:
  509. mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
  510. break;
  511. case IDMAC_SDC_0:
  512. case IDMAC_SDC_1:
  513. mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
  514. break;
  515. default:
  516. mask = 0;
  517. break;
  518. }
  519. return mask;
  520. }
  521. /**
  522. * ipu_enable_channel() - enable an IPU channel.
  523. * @idmac: IPU DMAC context.
  524. * @ichan: IDMAC channel.
  525. * @return: 0 on success or negative error code on failure.
  526. */
  527. static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
  528. {
  529. struct ipu *ipu = to_ipu(idmac);
  530. enum ipu_channel channel = ichan->dma_chan.chan_id;
  531. uint32_t reg;
  532. unsigned long flags;
  533. spin_lock_irqsave(&ipu->lock, flags);
  534. /* Reset to buffer 0 */
  535. idmac_write_ipureg(ipu, 1UL << channel, IPU_CHA_CUR_BUF);
  536. ichan->active_buffer = 0;
  537. ichan->status = IPU_CHANNEL_ENABLED;
  538. switch (channel) {
  539. case IDMAC_SDC_0:
  540. case IDMAC_SDC_1:
  541. case IDMAC_IC_7:
  542. ipu_channel_set_priority(ipu, channel, true);
  543. break;
  544. default:
  545. break;
  546. }
  547. reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
  548. idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
  549. ipu_ic_enable_task(ipu, channel);
  550. spin_unlock_irqrestore(&ipu->lock, flags);
  551. return 0;
  552. }
  553. /**
  554. * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
  555. * @ichan: IDMAC channel.
  556. * @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
  557. * @width: width of buffer in pixels.
  558. * @height: height of buffer in pixels.
  559. * @stride: stride length of buffer in pixels.
  560. * @rot_mode: rotation mode of buffer. A rotation setting other than
  561. * IPU_ROTATE_VERT_FLIP should only be used for input buffers of
  562. * rotation channels.
  563. * @phyaddr_0: buffer 0 physical address.
  564. * @phyaddr_1: buffer 1 physical address. Setting this to a value other than
  565. * NULL enables double buffering mode.
  566. * @return: 0 on success or negative error code on failure.
  567. */
  568. static int ipu_init_channel_buffer(struct idmac_channel *ichan,
  569. enum pixel_fmt pixel_fmt,
  570. uint16_t width, uint16_t height,
  571. uint32_t stride,
  572. enum ipu_rotate_mode rot_mode,
  573. dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
  574. {
  575. enum ipu_channel channel = ichan->dma_chan.chan_id;
  576. struct idmac *idmac = to_idmac(ichan->dma_chan.device);
  577. struct ipu *ipu = to_ipu(idmac);
  578. union chan_param_mem params = {};
  579. unsigned long flags;
  580. uint32_t reg;
  581. uint32_t stride_bytes;
  582. stride_bytes = stride * bytes_per_pixel(pixel_fmt);
  583. if (stride_bytes % 4) {
  584. dev_err(ipu->dev,
  585. "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
  586. stride, stride_bytes);
  587. return -EINVAL;
  588. }
  589. /* IC channel's stride must be a multiple of 8 pixels */
  590. if ((channel <= IDMAC_IC_13) && (stride % 8)) {
  591. dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
  592. return -EINVAL;
  593. }
  594. /* Build parameter memory data for DMA channel */
  595. ipu_ch_param_set_size(&params, pixel_fmt, width, height, stride_bytes);
  596. ipu_ch_param_set_buffer(&params, phyaddr_0, phyaddr_1);
  597. ipu_ch_param_set_rotation(&params, rot_mode);
  598. spin_lock_irqsave(&ipu->lock, flags);
  599. ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)&params, 10);
  600. reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
  601. if (phyaddr_1)
  602. reg |= 1UL << channel;
  603. else
  604. reg &= ~(1UL << channel);
  605. idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
  606. ichan->status = IPU_CHANNEL_READY;
  607. spin_unlock_irqrestore(&ipu->lock, flags);
  608. return 0;
  609. }
  610. /**
  611. * ipu_select_buffer() - mark a channel's buffer as ready.
  612. * @channel: channel ID.
  613. * @buffer_n: buffer number to mark ready.
  614. */
  615. static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
  616. {
  617. /* No locking - this is a write-one-to-set register, cleared by IPU */
  618. if (buffer_n == 0)
  619. /* Mark buffer 0 as ready. */
  620. idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
  621. else
  622. /* Mark buffer 1 as ready. */
  623. idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
  624. }
  625. /**
  626. * ipu_update_channel_buffer() - update physical address of a channel buffer.
  627. * @ichan: IDMAC channel.
  628. * @buffer_n: buffer number to update.
  629. * 0 or 1 are the only valid values.
  630. * @phyaddr: buffer physical address.
  631. */
  632. /* Called under spin_lock(_irqsave)(&ichan->lock) */
  633. static void ipu_update_channel_buffer(struct idmac_channel *ichan,
  634. int buffer_n, dma_addr_t phyaddr)
  635. {
  636. enum ipu_channel channel = ichan->dma_chan.chan_id;
  637. uint32_t reg;
  638. unsigned long flags;
  639. spin_lock_irqsave(&ipu_data.lock, flags);
  640. if (buffer_n == 0) {
  641. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  642. if (reg & (1UL << channel)) {
  643. ipu_ic_disable_task(&ipu_data, channel);
  644. ichan->status = IPU_CHANNEL_READY;
  645. }
  646. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
  647. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  648. 0x0008UL, IPU_IMA_ADDR);
  649. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  650. } else {
  651. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  652. if (reg & (1UL << channel)) {
  653. ipu_ic_disable_task(&ipu_data, channel);
  654. ichan->status = IPU_CHANNEL_READY;
  655. }
  656. /* Check if double-buffering is already enabled */
  657. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
  658. if (!(reg & (1UL << channel)))
  659. idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
  660. IPU_CHA_DB_MODE_SEL);
  661. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
  662. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  663. 0x0009UL, IPU_IMA_ADDR);
  664. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  665. }
  666. spin_unlock_irqrestore(&ipu_data.lock, flags);
  667. }
  668. /* Called under spin_lock_irqsave(&ichan->lock) */
  669. static int ipu_submit_buffer(struct idmac_channel *ichan,
  670. struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
  671. {
  672. unsigned int chan_id = ichan->dma_chan.chan_id;
  673. struct device *dev = &ichan->dma_chan.dev->device;
  674. if (async_tx_test_ack(&desc->txd))
  675. return -EINTR;
  676. /*
  677. * On first invocation this shouldn't be necessary, the call to
  678. * ipu_init_channel_buffer() above will set addresses for us, so we
  679. * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
  680. * doing it again shouldn't hurt either.
  681. */
  682. ipu_update_channel_buffer(ichan, buf_idx, sg_dma_address(sg));
  683. ipu_select_buffer(chan_id, buf_idx);
  684. dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
  685. sg, chan_id, buf_idx);
  686. return 0;
  687. }
  688. /* Called under spin_lock_irqsave(&ichan->lock) */
  689. static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
  690. struct idmac_tx_desc *desc)
  691. {
  692. struct scatterlist *sg;
  693. int i, ret = 0;
  694. for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
  695. if (!ichan->sg[i]) {
  696. ichan->sg[i] = sg;
  697. ret = ipu_submit_buffer(ichan, desc, sg, i);
  698. if (ret < 0)
  699. return ret;
  700. sg = sg_next(sg);
  701. }
  702. }
  703. return ret;
  704. }
  705. static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
  706. {
  707. struct idmac_tx_desc *desc = to_tx_desc(tx);
  708. struct idmac_channel *ichan = to_idmac_chan(tx->chan);
  709. struct idmac *idmac = to_idmac(tx->chan->device);
  710. struct ipu *ipu = to_ipu(idmac);
  711. struct device *dev = &ichan->dma_chan.dev->device;
  712. dma_cookie_t cookie;
  713. unsigned long flags;
  714. int ret;
  715. /* Sanity check */
  716. if (!list_empty(&desc->list)) {
  717. /* The descriptor doesn't belong to client */
  718. dev_err(dev, "Descriptor %p not prepared!\n", tx);
  719. return -EBUSY;
  720. }
  721. mutex_lock(&ichan->chan_mutex);
  722. async_tx_clear_ack(tx);
  723. if (ichan->status < IPU_CHANNEL_READY) {
  724. struct idmac_video_param *video = &ichan->params.video;
  725. /*
  726. * Initial buffer assignment - the first two sg-entries from
  727. * the descriptor will end up in the IDMAC buffers
  728. */
  729. dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
  730. sg_dma_address(&desc->sg[1]);
  731. WARN_ON(ichan->sg[0] || ichan->sg[1]);
  732. cookie = ipu_init_channel_buffer(ichan,
  733. video->out_pixel_fmt,
  734. video->out_width,
  735. video->out_height,
  736. video->out_stride,
  737. IPU_ROTATE_NONE,
  738. sg_dma_address(&desc->sg[0]),
  739. dma_1);
  740. if (cookie < 0)
  741. goto out;
  742. }
  743. dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
  744. cookie = dma_cookie_assign(tx);
  745. /* ipu->lock can be taken under ichan->lock, but not v.v. */
  746. spin_lock_irqsave(&ichan->lock, flags);
  747. list_add_tail(&desc->list, &ichan->queue);
  748. /* submit_buffers() atomically verifies and fills empty sg slots */
  749. ret = ipu_submit_channel_buffers(ichan, desc);
  750. spin_unlock_irqrestore(&ichan->lock, flags);
  751. if (ret < 0) {
  752. cookie = ret;
  753. goto dequeue;
  754. }
  755. if (ichan->status < IPU_CHANNEL_ENABLED) {
  756. ret = ipu_enable_channel(idmac, ichan);
  757. if (ret < 0) {
  758. cookie = ret;
  759. goto dequeue;
  760. }
  761. }
  762. dump_idmac_reg(ipu);
  763. dequeue:
  764. if (cookie < 0) {
  765. spin_lock_irqsave(&ichan->lock, flags);
  766. list_del_init(&desc->list);
  767. spin_unlock_irqrestore(&ichan->lock, flags);
  768. tx->cookie = cookie;
  769. ichan->dma_chan.cookie = cookie;
  770. }
  771. out:
  772. mutex_unlock(&ichan->chan_mutex);
  773. return cookie;
  774. }
  775. /* Called with ichan->chan_mutex held */
  776. static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
  777. {
  778. struct idmac_tx_desc *desc =
  779. vmalloc(array_size(n, sizeof(struct idmac_tx_desc)));
  780. struct idmac *idmac = to_idmac(ichan->dma_chan.device);
  781. if (!desc)
  782. return -ENOMEM;
  783. /* No interrupts, just disable the tasklet for a moment */
  784. tasklet_disable(&to_ipu(idmac)->tasklet);
  785. ichan->n_tx_desc = n;
  786. ichan->desc = desc;
  787. INIT_LIST_HEAD(&ichan->queue);
  788. INIT_LIST_HEAD(&ichan->free_list);
  789. while (n--) {
  790. struct dma_async_tx_descriptor *txd = &desc->txd;
  791. memset(txd, 0, sizeof(*txd));
  792. dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
  793. txd->tx_submit = idmac_tx_submit;
  794. list_add(&desc->list, &ichan->free_list);
  795. desc++;
  796. }
  797. tasklet_enable(&to_ipu(idmac)->tasklet);
  798. return 0;
  799. }
  800. /**
  801. * ipu_init_channel() - initialize an IPU channel.
  802. * @idmac: IPU DMAC context.
  803. * @ichan: pointer to the channel object.
  804. * @return 0 on success or negative error code on failure.
  805. */
  806. static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
  807. {
  808. union ipu_channel_param *params = &ichan->params;
  809. uint32_t ipu_conf;
  810. enum ipu_channel channel = ichan->dma_chan.chan_id;
  811. unsigned long flags;
  812. uint32_t reg;
  813. struct ipu *ipu = to_ipu(idmac);
  814. int ret = 0, n_desc = 0;
  815. dev_dbg(ipu->dev, "init channel = %d\n", channel);
  816. if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
  817. channel != IDMAC_IC_7)
  818. return -EINVAL;
  819. spin_lock_irqsave(&ipu->lock, flags);
  820. switch (channel) {
  821. case IDMAC_IC_7:
  822. n_desc = 16;
  823. reg = idmac_read_icreg(ipu, IC_CONF);
  824. idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
  825. break;
  826. case IDMAC_IC_0:
  827. n_desc = 16;
  828. reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
  829. idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
  830. ret = ipu_ic_init_prpenc(ipu, params, true);
  831. break;
  832. case IDMAC_SDC_0:
  833. case IDMAC_SDC_1:
  834. n_desc = 4;
  835. break;
  836. default:
  837. break;
  838. }
  839. ipu->channel_init_mask |= 1L << channel;
  840. /* Enable IPU sub module */
  841. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
  842. ipu_channel_conf_mask(channel);
  843. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  844. spin_unlock_irqrestore(&ipu->lock, flags);
  845. if (n_desc && !ichan->desc)
  846. ret = idmac_desc_alloc(ichan, n_desc);
  847. dump_idmac_reg(ipu);
  848. return ret;
  849. }
  850. /**
  851. * ipu_uninit_channel() - uninitialize an IPU channel.
  852. * @idmac: IPU DMAC context.
  853. * @ichan: pointer to the channel object.
  854. */
  855. static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
  856. {
  857. enum ipu_channel channel = ichan->dma_chan.chan_id;
  858. unsigned long flags;
  859. uint32_t reg;
  860. unsigned long chan_mask = 1UL << channel;
  861. uint32_t ipu_conf;
  862. struct ipu *ipu = to_ipu(idmac);
  863. spin_lock_irqsave(&ipu->lock, flags);
  864. if (!(ipu->channel_init_mask & chan_mask)) {
  865. dev_err(ipu->dev, "Channel already uninitialized %d\n",
  866. channel);
  867. spin_unlock_irqrestore(&ipu->lock, flags);
  868. return;
  869. }
  870. /* Reset the double buffer */
  871. reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
  872. idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
  873. ichan->sec_chan_en = false;
  874. switch (channel) {
  875. case IDMAC_IC_7:
  876. reg = idmac_read_icreg(ipu, IC_CONF);
  877. idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
  878. IC_CONF);
  879. break;
  880. case IDMAC_IC_0:
  881. reg = idmac_read_icreg(ipu, IC_CONF);
  882. idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
  883. IC_CONF);
  884. break;
  885. case IDMAC_SDC_0:
  886. case IDMAC_SDC_1:
  887. default:
  888. break;
  889. }
  890. ipu->channel_init_mask &= ~(1L << channel);
  891. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
  892. ~ipu_channel_conf_mask(channel);
  893. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  894. spin_unlock_irqrestore(&ipu->lock, flags);
  895. ichan->n_tx_desc = 0;
  896. vfree(ichan->desc);
  897. ichan->desc = NULL;
  898. }
  899. /**
  900. * ipu_disable_channel() - disable an IPU channel.
  901. * @idmac: IPU DMAC context.
  902. * @ichan: channel object pointer.
  903. * @wait_for_stop: flag to set whether to wait for channel end of frame or
  904. * return immediately.
  905. * @return: 0 on success or negative error code on failure.
  906. */
  907. static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
  908. bool wait_for_stop)
  909. {
  910. enum ipu_channel channel = ichan->dma_chan.chan_id;
  911. struct ipu *ipu = to_ipu(idmac);
  912. uint32_t reg;
  913. unsigned long flags;
  914. unsigned long chan_mask = 1UL << channel;
  915. unsigned int timeout;
  916. if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
  917. timeout = 40;
  918. /* This waiting always fails. Related to spurious irq problem */
  919. while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
  920. (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
  921. timeout--;
  922. msleep(10);
  923. if (!timeout) {
  924. dev_dbg(ipu->dev,
  925. "Warning: timeout waiting for channel %u to "
  926. "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
  927. "busy = 0x%08X, tstat = 0x%08X\n", channel,
  928. idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
  929. idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
  930. idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
  931. idmac_read_ipureg(ipu, IPU_TASKS_STAT));
  932. break;
  933. }
  934. }
  935. dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
  936. }
  937. /* SDC BG and FG must be disabled before DMA is disabled */
  938. if (wait_for_stop && (channel == IDMAC_SDC_0 ||
  939. channel == IDMAC_SDC_1)) {
  940. for (timeout = 5;
  941. timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
  942. msleep(5);
  943. }
  944. spin_lock_irqsave(&ipu->lock, flags);
  945. /* Disable IC task */
  946. ipu_ic_disable_task(ipu, channel);
  947. /* Disable DMA channel(s) */
  948. reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
  949. idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
  950. spin_unlock_irqrestore(&ipu->lock, flags);
  951. return 0;
  952. }
  953. static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
  954. struct idmac_tx_desc **desc, struct scatterlist *sg)
  955. {
  956. struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
  957. if (sgnew)
  958. /* next sg-element in this list */
  959. return sgnew;
  960. if ((*desc)->list.next == &ichan->queue)
  961. /* No more descriptors on the queue */
  962. return NULL;
  963. /* Fetch next descriptor */
  964. *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
  965. return (*desc)->sg;
  966. }
  967. /*
  968. * We have several possibilities here:
  969. * current BUF next BUF
  970. *
  971. * not last sg next not last sg
  972. * not last sg next last sg
  973. * last sg first sg from next descriptor
  974. * last sg NULL
  975. *
  976. * Besides, the descriptor queue might be empty or not. We process all these
  977. * cases carefully.
  978. */
  979. static irqreturn_t idmac_interrupt(int irq, void *dev_id)
  980. {
  981. struct idmac_channel *ichan = dev_id;
  982. struct device *dev = &ichan->dma_chan.dev->device;
  983. unsigned int chan_id = ichan->dma_chan.chan_id;
  984. struct scatterlist **sg, *sgnext, *sgnew = NULL;
  985. /* Next transfer descriptor */
  986. struct idmac_tx_desc *desc, *descnew;
  987. bool done = false;
  988. u32 ready0, ready1, curbuf, err;
  989. struct dmaengine_desc_callback cb;
  990. /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
  991. dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
  992. spin_lock(&ipu_data.lock);
  993. ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  994. ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  995. curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
  996. err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
  997. if (err & (1 << chan_id)) {
  998. idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
  999. spin_unlock(&ipu_data.lock);
  1000. /*
  1001. * Doing this
  1002. * ichan->sg[0] = ichan->sg[1] = NULL;
  1003. * you can force channel re-enable on the next tx_submit(), but
  1004. * this is dirty - think about descriptors with multiple
  1005. * sg elements.
  1006. */
  1007. dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
  1008. chan_id, ready0, ready1, curbuf);
  1009. return IRQ_HANDLED;
  1010. }
  1011. spin_unlock(&ipu_data.lock);
  1012. /* Other interrupts do not interfere with this channel */
  1013. spin_lock(&ichan->lock);
  1014. if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
  1015. (!ichan->active_buffer && (ready0 >> chan_id) & 1)
  1016. )) {
  1017. spin_unlock(&ichan->lock);
  1018. dev_dbg(dev,
  1019. "IRQ with active buffer still ready on channel %x, "
  1020. "active %d, ready %x, %x!\n", chan_id,
  1021. ichan->active_buffer, ready0, ready1);
  1022. return IRQ_NONE;
  1023. }
  1024. if (unlikely(list_empty(&ichan->queue))) {
  1025. ichan->sg[ichan->active_buffer] = NULL;
  1026. spin_unlock(&ichan->lock);
  1027. dev_err(dev,
  1028. "IRQ without queued buffers on channel %x, active %d, "
  1029. "ready %x, %x!\n", chan_id,
  1030. ichan->active_buffer, ready0, ready1);
  1031. return IRQ_NONE;
  1032. }
  1033. /*
  1034. * active_buffer is a software flag, it shows which buffer we are
  1035. * currently expecting back from the hardware, IDMAC should be
  1036. * processing the other buffer already
  1037. */
  1038. sg = &ichan->sg[ichan->active_buffer];
  1039. sgnext = ichan->sg[!ichan->active_buffer];
  1040. if (!*sg) {
  1041. spin_unlock(&ichan->lock);
  1042. return IRQ_HANDLED;
  1043. }
  1044. desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
  1045. descnew = desc;
  1046. dev_dbg(dev, "IDMAC irq %d, dma %#llx, next dma %#llx, current %d, curbuf %#x\n",
  1047. irq, (u64)sg_dma_address(*sg),
  1048. sgnext ? (u64)sg_dma_address(sgnext) : 0,
  1049. ichan->active_buffer, curbuf);
  1050. /* Find the descriptor of sgnext */
  1051. sgnew = idmac_sg_next(ichan, &descnew, *sg);
  1052. if (sgnext != sgnew)
  1053. dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
  1054. /*
  1055. * if sgnext == NULL sg must be the last element in a scatterlist and
  1056. * queue must be empty
  1057. */
  1058. if (unlikely(!sgnext)) {
  1059. if (!WARN_ON(sg_next(*sg)))
  1060. dev_dbg(dev, "Underrun on channel %x\n", chan_id);
  1061. ichan->sg[!ichan->active_buffer] = sgnew;
  1062. if (unlikely(sgnew)) {
  1063. ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
  1064. } else {
  1065. spin_lock(&ipu_data.lock);
  1066. ipu_ic_disable_task(&ipu_data, chan_id);
  1067. spin_unlock(&ipu_data.lock);
  1068. ichan->status = IPU_CHANNEL_READY;
  1069. /* Continue to check for complete descriptor */
  1070. }
  1071. }
  1072. /* Calculate and submit the next sg element */
  1073. sgnew = idmac_sg_next(ichan, &descnew, sgnew);
  1074. if (unlikely(!sg_next(*sg)) || !sgnext) {
  1075. /*
  1076. * Last element in scatterlist done, remove from the queue,
  1077. * _init for debugging
  1078. */
  1079. list_del_init(&desc->list);
  1080. done = true;
  1081. }
  1082. *sg = sgnew;
  1083. if (likely(sgnew) &&
  1084. ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
  1085. dmaengine_desc_get_callback(&descnew->txd, &cb);
  1086. list_del_init(&descnew->list);
  1087. spin_unlock(&ichan->lock);
  1088. dmaengine_desc_callback_invoke(&cb, NULL);
  1089. spin_lock(&ichan->lock);
  1090. }
  1091. /* Flip the active buffer - even if update above failed */
  1092. ichan->active_buffer = !ichan->active_buffer;
  1093. if (done)
  1094. dma_cookie_complete(&desc->txd);
  1095. dmaengine_desc_get_callback(&desc->txd, &cb);
  1096. spin_unlock(&ichan->lock);
  1097. if (done && (desc->txd.flags & DMA_PREP_INTERRUPT))
  1098. dmaengine_desc_callback_invoke(&cb, NULL);
  1099. return IRQ_HANDLED;
  1100. }
  1101. static void ipu_gc_tasklet(struct tasklet_struct *t)
  1102. {
  1103. struct ipu *ipu = from_tasklet(ipu, t, tasklet);
  1104. int i;
  1105. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1106. struct idmac_channel *ichan = ipu->channel + i;
  1107. struct idmac_tx_desc *desc;
  1108. unsigned long flags;
  1109. struct scatterlist *sg;
  1110. int j, k;
  1111. for (j = 0; j < ichan->n_tx_desc; j++) {
  1112. desc = ichan->desc + j;
  1113. spin_lock_irqsave(&ichan->lock, flags);
  1114. if (async_tx_test_ack(&desc->txd)) {
  1115. list_move(&desc->list, &ichan->free_list);
  1116. for_each_sg(desc->sg, sg, desc->sg_len, k) {
  1117. if (ichan->sg[0] == sg)
  1118. ichan->sg[0] = NULL;
  1119. else if (ichan->sg[1] == sg)
  1120. ichan->sg[1] = NULL;
  1121. }
  1122. async_tx_clear_ack(&desc->txd);
  1123. }
  1124. spin_unlock_irqrestore(&ichan->lock, flags);
  1125. }
  1126. }
  1127. }
  1128. /* Allocate and initialise a transfer descriptor. */
  1129. static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
  1130. struct scatterlist *sgl, unsigned int sg_len,
  1131. enum dma_transfer_direction direction, unsigned long tx_flags,
  1132. void *context)
  1133. {
  1134. struct idmac_channel *ichan = to_idmac_chan(chan);
  1135. struct idmac_tx_desc *desc = NULL;
  1136. struct dma_async_tx_descriptor *txd = NULL;
  1137. unsigned long flags;
  1138. /* We only can handle these three channels so far */
  1139. if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
  1140. chan->chan_id != IDMAC_IC_7)
  1141. return NULL;
  1142. if (!is_slave_direction(direction)) {
  1143. dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
  1144. return NULL;
  1145. }
  1146. mutex_lock(&ichan->chan_mutex);
  1147. spin_lock_irqsave(&ichan->lock, flags);
  1148. if (!list_empty(&ichan->free_list)) {
  1149. desc = list_entry(ichan->free_list.next,
  1150. struct idmac_tx_desc, list);
  1151. list_del_init(&desc->list);
  1152. desc->sg_len = sg_len;
  1153. desc->sg = sgl;
  1154. txd = &desc->txd;
  1155. txd->flags = tx_flags;
  1156. }
  1157. spin_unlock_irqrestore(&ichan->lock, flags);
  1158. mutex_unlock(&ichan->chan_mutex);
  1159. tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
  1160. return txd;
  1161. }
  1162. /* Re-select the current buffer and re-activate the channel */
  1163. static void idmac_issue_pending(struct dma_chan *chan)
  1164. {
  1165. struct idmac_channel *ichan = to_idmac_chan(chan);
  1166. struct idmac *idmac = to_idmac(chan->device);
  1167. struct ipu *ipu = to_ipu(idmac);
  1168. unsigned long flags;
  1169. /* This is not always needed, but doesn't hurt either */
  1170. spin_lock_irqsave(&ipu->lock, flags);
  1171. ipu_select_buffer(chan->chan_id, ichan->active_buffer);
  1172. spin_unlock_irqrestore(&ipu->lock, flags);
  1173. /*
  1174. * Might need to perform some parts of initialisation from
  1175. * ipu_enable_channel(), but not all, we do not want to reset to buffer
  1176. * 0, don't need to set priority again either, but re-enabling the task
  1177. * and the channel might be a good idea.
  1178. */
  1179. }
  1180. static int idmac_pause(struct dma_chan *chan)
  1181. {
  1182. struct idmac_channel *ichan = to_idmac_chan(chan);
  1183. struct idmac *idmac = to_idmac(chan->device);
  1184. struct ipu *ipu = to_ipu(idmac);
  1185. struct list_head *list, *tmp;
  1186. unsigned long flags;
  1187. mutex_lock(&ichan->chan_mutex);
  1188. spin_lock_irqsave(&ipu->lock, flags);
  1189. ipu_ic_disable_task(ipu, chan->chan_id);
  1190. /* Return all descriptors into "prepared" state */
  1191. list_for_each_safe(list, tmp, &ichan->queue)
  1192. list_del_init(list);
  1193. ichan->sg[0] = NULL;
  1194. ichan->sg[1] = NULL;
  1195. spin_unlock_irqrestore(&ipu->lock, flags);
  1196. ichan->status = IPU_CHANNEL_INITIALIZED;
  1197. mutex_unlock(&ichan->chan_mutex);
  1198. return 0;
  1199. }
  1200. static int __idmac_terminate_all(struct dma_chan *chan)
  1201. {
  1202. struct idmac_channel *ichan = to_idmac_chan(chan);
  1203. struct idmac *idmac = to_idmac(chan->device);
  1204. struct ipu *ipu = to_ipu(idmac);
  1205. unsigned long flags;
  1206. int i;
  1207. ipu_disable_channel(idmac, ichan,
  1208. ichan->status >= IPU_CHANNEL_ENABLED);
  1209. tasklet_disable(&ipu->tasklet);
  1210. /* ichan->queue is modified in ISR, have to spinlock */
  1211. spin_lock_irqsave(&ichan->lock, flags);
  1212. list_splice_init(&ichan->queue, &ichan->free_list);
  1213. if (ichan->desc)
  1214. for (i = 0; i < ichan->n_tx_desc; i++) {
  1215. struct idmac_tx_desc *desc = ichan->desc + i;
  1216. if (list_empty(&desc->list))
  1217. /* Descriptor was prepared, but not submitted */
  1218. list_add(&desc->list, &ichan->free_list);
  1219. async_tx_clear_ack(&desc->txd);
  1220. }
  1221. ichan->sg[0] = NULL;
  1222. ichan->sg[1] = NULL;
  1223. spin_unlock_irqrestore(&ichan->lock, flags);
  1224. tasklet_enable(&ipu->tasklet);
  1225. ichan->status = IPU_CHANNEL_INITIALIZED;
  1226. return 0;
  1227. }
  1228. static int idmac_terminate_all(struct dma_chan *chan)
  1229. {
  1230. struct idmac_channel *ichan = to_idmac_chan(chan);
  1231. int ret;
  1232. mutex_lock(&ichan->chan_mutex);
  1233. ret = __idmac_terminate_all(chan);
  1234. mutex_unlock(&ichan->chan_mutex);
  1235. return ret;
  1236. }
  1237. #ifdef DEBUG
  1238. static irqreturn_t ic_sof_irq(int irq, void *dev_id)
  1239. {
  1240. struct idmac_channel *ichan = dev_id;
  1241. printk(KERN_DEBUG "Got SOF IRQ %d on Channel %d\n",
  1242. irq, ichan->dma_chan.chan_id);
  1243. disable_irq_nosync(irq);
  1244. return IRQ_HANDLED;
  1245. }
  1246. static irqreturn_t ic_eof_irq(int irq, void *dev_id)
  1247. {
  1248. struct idmac_channel *ichan = dev_id;
  1249. printk(KERN_DEBUG "Got EOF IRQ %d on Channel %d\n",
  1250. irq, ichan->dma_chan.chan_id);
  1251. disable_irq_nosync(irq);
  1252. return IRQ_HANDLED;
  1253. }
  1254. static int ic_sof = -EINVAL, ic_eof = -EINVAL;
  1255. #endif
  1256. static int idmac_alloc_chan_resources(struct dma_chan *chan)
  1257. {
  1258. struct idmac_channel *ichan = to_idmac_chan(chan);
  1259. struct idmac *idmac = to_idmac(chan->device);
  1260. int ret;
  1261. /* dmaengine.c now guarantees to only offer free channels */
  1262. BUG_ON(chan->client_count > 1);
  1263. WARN_ON(ichan->status != IPU_CHANNEL_FREE);
  1264. dma_cookie_init(chan);
  1265. ret = ipu_irq_map(chan->chan_id);
  1266. if (ret < 0)
  1267. goto eimap;
  1268. ichan->eof_irq = ret;
  1269. /*
  1270. * Important to first disable the channel, because maybe someone
  1271. * used it before us, e.g., the bootloader
  1272. */
  1273. ipu_disable_channel(idmac, ichan, true);
  1274. ret = ipu_init_channel(idmac, ichan);
  1275. if (ret < 0)
  1276. goto eichan;
  1277. ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
  1278. ichan->eof_name, ichan);
  1279. if (ret < 0)
  1280. goto erirq;
  1281. #ifdef DEBUG
  1282. if (chan->chan_id == IDMAC_IC_7) {
  1283. ic_sof = ipu_irq_map(69);
  1284. if (ic_sof > 0) {
  1285. ret = request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
  1286. if (ret)
  1287. dev_err(&chan->dev->device, "request irq failed for IC SOF");
  1288. }
  1289. ic_eof = ipu_irq_map(70);
  1290. if (ic_eof > 0) {
  1291. ret = request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
  1292. if (ret)
  1293. dev_err(&chan->dev->device, "request irq failed for IC EOF");
  1294. }
  1295. }
  1296. #endif
  1297. ichan->status = IPU_CHANNEL_INITIALIZED;
  1298. dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
  1299. chan->chan_id, ichan->eof_irq);
  1300. return ret;
  1301. erirq:
  1302. ipu_uninit_channel(idmac, ichan);
  1303. eichan:
  1304. ipu_irq_unmap(chan->chan_id);
  1305. eimap:
  1306. return ret;
  1307. }
  1308. static void idmac_free_chan_resources(struct dma_chan *chan)
  1309. {
  1310. struct idmac_channel *ichan = to_idmac_chan(chan);
  1311. struct idmac *idmac = to_idmac(chan->device);
  1312. mutex_lock(&ichan->chan_mutex);
  1313. __idmac_terminate_all(chan);
  1314. if (ichan->status > IPU_CHANNEL_FREE) {
  1315. #ifdef DEBUG
  1316. if (chan->chan_id == IDMAC_IC_7) {
  1317. if (ic_sof > 0) {
  1318. free_irq(ic_sof, ichan);
  1319. ipu_irq_unmap(69);
  1320. ic_sof = -EINVAL;
  1321. }
  1322. if (ic_eof > 0) {
  1323. free_irq(ic_eof, ichan);
  1324. ipu_irq_unmap(70);
  1325. ic_eof = -EINVAL;
  1326. }
  1327. }
  1328. #endif
  1329. free_irq(ichan->eof_irq, ichan);
  1330. ipu_irq_unmap(chan->chan_id);
  1331. }
  1332. ichan->status = IPU_CHANNEL_FREE;
  1333. ipu_uninit_channel(idmac, ichan);
  1334. mutex_unlock(&ichan->chan_mutex);
  1335. tasklet_schedule(&to_ipu(idmac)->tasklet);
  1336. }
  1337. static enum dma_status idmac_tx_status(struct dma_chan *chan,
  1338. dma_cookie_t cookie, struct dma_tx_state *txstate)
  1339. {
  1340. return dma_cookie_status(chan, cookie, txstate);
  1341. }
  1342. static int __init ipu_idmac_init(struct ipu *ipu)
  1343. {
  1344. struct idmac *idmac = &ipu->idmac;
  1345. struct dma_device *dma = &idmac->dma;
  1346. int i;
  1347. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  1348. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  1349. /* Compulsory common fields */
  1350. dma->dev = ipu->dev;
  1351. dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
  1352. dma->device_free_chan_resources = idmac_free_chan_resources;
  1353. dma->device_tx_status = idmac_tx_status;
  1354. dma->device_issue_pending = idmac_issue_pending;
  1355. /* Compulsory for DMA_SLAVE fields */
  1356. dma->device_prep_slave_sg = idmac_prep_slave_sg;
  1357. dma->device_pause = idmac_pause;
  1358. dma->device_terminate_all = idmac_terminate_all;
  1359. INIT_LIST_HEAD(&dma->channels);
  1360. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1361. struct idmac_channel *ichan = ipu->channel + i;
  1362. struct dma_chan *dma_chan = &ichan->dma_chan;
  1363. spin_lock_init(&ichan->lock);
  1364. mutex_init(&ichan->chan_mutex);
  1365. ichan->status = IPU_CHANNEL_FREE;
  1366. ichan->sec_chan_en = false;
  1367. snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
  1368. dma_chan->device = &idmac->dma;
  1369. dma_cookie_init(dma_chan);
  1370. dma_chan->chan_id = i;
  1371. list_add_tail(&dma_chan->device_node, &dma->channels);
  1372. }
  1373. idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
  1374. return dma_async_device_register(&idmac->dma);
  1375. }
  1376. static void ipu_idmac_exit(struct ipu *ipu)
  1377. {
  1378. int i;
  1379. struct idmac *idmac = &ipu->idmac;
  1380. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1381. struct idmac_channel *ichan = ipu->channel + i;
  1382. idmac_terminate_all(&ichan->dma_chan);
  1383. }
  1384. dma_async_device_unregister(&idmac->dma);
  1385. }
  1386. /*****************************************************************************
  1387. * IPU common probe / remove
  1388. */
  1389. static int __init ipu_probe(struct platform_device *pdev)
  1390. {
  1391. struct resource *mem_ipu, *mem_ic;
  1392. int ret;
  1393. spin_lock_init(&ipu_data.lock);
  1394. mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1395. mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1396. if (!mem_ipu || !mem_ic)
  1397. return -EINVAL;
  1398. ipu_data.dev = &pdev->dev;
  1399. platform_set_drvdata(pdev, &ipu_data);
  1400. ret = platform_get_irq(pdev, 0);
  1401. if (ret < 0)
  1402. goto err_noirq;
  1403. ipu_data.irq_fn = ret;
  1404. ret = platform_get_irq(pdev, 1);
  1405. if (ret < 0)
  1406. goto err_noirq;
  1407. ipu_data.irq_err = ret;
  1408. dev_dbg(&pdev->dev, "fn irq %u, err irq %u\n",
  1409. ipu_data.irq_fn, ipu_data.irq_err);
  1410. /* Remap IPU common registers */
  1411. ipu_data.reg_ipu = ioremap(mem_ipu->start, resource_size(mem_ipu));
  1412. if (!ipu_data.reg_ipu) {
  1413. ret = -ENOMEM;
  1414. goto err_ioremap_ipu;
  1415. }
  1416. /* Remap Image Converter and Image DMA Controller registers */
  1417. ipu_data.reg_ic = ioremap(mem_ic->start, resource_size(mem_ic));
  1418. if (!ipu_data.reg_ic) {
  1419. ret = -ENOMEM;
  1420. goto err_ioremap_ic;
  1421. }
  1422. /* Get IPU clock */
  1423. ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
  1424. if (IS_ERR(ipu_data.ipu_clk)) {
  1425. ret = PTR_ERR(ipu_data.ipu_clk);
  1426. goto err_clk_get;
  1427. }
  1428. /* Make sure IPU HSP clock is running */
  1429. clk_prepare_enable(ipu_data.ipu_clk);
  1430. /* Disable all interrupts */
  1431. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
  1432. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
  1433. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
  1434. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
  1435. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
  1436. dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
  1437. (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
  1438. ret = ipu_irq_attach_irq(&ipu_data, pdev);
  1439. if (ret < 0)
  1440. goto err_attach_irq;
  1441. /* Initialize DMA engine */
  1442. ret = ipu_idmac_init(&ipu_data);
  1443. if (ret < 0)
  1444. goto err_idmac_init;
  1445. tasklet_setup(&ipu_data.tasklet, ipu_gc_tasklet);
  1446. ipu_data.dev = &pdev->dev;
  1447. dev_dbg(ipu_data.dev, "IPU initialized\n");
  1448. return 0;
  1449. err_idmac_init:
  1450. err_attach_irq:
  1451. ipu_irq_detach_irq(&ipu_data, pdev);
  1452. clk_disable_unprepare(ipu_data.ipu_clk);
  1453. clk_put(ipu_data.ipu_clk);
  1454. err_clk_get:
  1455. iounmap(ipu_data.reg_ic);
  1456. err_ioremap_ic:
  1457. iounmap(ipu_data.reg_ipu);
  1458. err_ioremap_ipu:
  1459. err_noirq:
  1460. dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
  1461. return ret;
  1462. }
  1463. static int ipu_remove(struct platform_device *pdev)
  1464. {
  1465. struct ipu *ipu = platform_get_drvdata(pdev);
  1466. ipu_idmac_exit(ipu);
  1467. ipu_irq_detach_irq(ipu, pdev);
  1468. clk_disable_unprepare(ipu->ipu_clk);
  1469. clk_put(ipu->ipu_clk);
  1470. iounmap(ipu->reg_ic);
  1471. iounmap(ipu->reg_ipu);
  1472. tasklet_kill(&ipu->tasklet);
  1473. return 0;
  1474. }
  1475. /*
  1476. * We need two MEM resources - with IPU-common and Image Converter registers,
  1477. * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
  1478. */
  1479. static struct platform_driver ipu_platform_driver = {
  1480. .driver = {
  1481. .name = "ipu-core",
  1482. },
  1483. .remove = ipu_remove,
  1484. };
  1485. static int __init ipu_init(void)
  1486. {
  1487. return platform_driver_probe(&ipu_platform_driver, ipu_probe);
  1488. }
  1489. subsys_initcall(ipu_init);
  1490. MODULE_DESCRIPTION("IPU core driver");
  1491. MODULE_LICENSE("GPL v2");
  1492. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1493. MODULE_ALIAS("platform:ipu-core");