ipu_idmac.c 45 KB

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