ipu_idmac.c 46 KB

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