isp.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. /*
  2. * isp.c
  3. *
  4. * TI OMAP3 ISP - Core
  5. *
  6. * Copyright (C) 2006-2010 Nokia Corporation
  7. * Copyright (C) 2007-2009 Texas Instruments, Inc.
  8. *
  9. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. * Sakari Ailus <sakari.ailus@iki.fi>
  11. *
  12. * Contributors:
  13. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  14. * Sakari Ailus <sakari.ailus@iki.fi>
  15. * David Cohen <dacohen@gmail.com>
  16. * Stanimir Varbanov <svarbanov@mm-sol.com>
  17. * Vimarsh Zutshi <vimarsh.zutshi@gmail.com>
  18. * Tuukka Toivonen <tuukkat76@gmail.com>
  19. * Sergio Aguirre <saaguirre@ti.com>
  20. * Antti Koskipaa <akoskipa@gmail.com>
  21. * Ivan T. Ivanov <iivanov@mm-sol.com>
  22. * RaniSuneela <r-m@ti.com>
  23. * Atanas Filipov <afilipov@mm-sol.com>
  24. * Gjorgji Rosikopulos <grosikopulos@mm-sol.com>
  25. * Hiroshi DOYU <hiroshi.doyu@nokia.com>
  26. * Nayden Kanchev <nkanchev@mm-sol.com>
  27. * Phil Carmody <ext-phil.2.carmody@nokia.com>
  28. * Artem Bityutskiy <artem.bityutskiy@nokia.com>
  29. * Dominic Curran <dcurran@ti.com>
  30. * Ilkka Myllyperkio <ilkka.myllyperkio@sofica.fi>
  31. * Pallavi Kulkarni <p-kulkarni@ti.com>
  32. * Vaibhav Hiremath <hvaibhav@ti.com>
  33. * Mohit Jalori <mjalori@ti.com>
  34. * Sameer Venkatraman <sameerv@ti.com>
  35. * Senthilvadivu Guruswamy <svadivu@ti.com>
  36. * Thara Gopinath <thara@ti.com>
  37. * Toni Leinonen <toni.leinonen@nokia.com>
  38. * Troy Laramy <t-laramy@ti.com>
  39. *
  40. * This program is free software; you can redistribute it and/or modify
  41. * it under the terms of the GNU General Public License version 2 as
  42. * published by the Free Software Foundation.
  43. */
  44. #include <asm/cacheflush.h>
  45. #include <linux/clk.h>
  46. #include <linux/clkdev.h>
  47. #include <linux/delay.h>
  48. #include <linux/device.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/i2c.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/mfd/syscon.h>
  53. #include <linux/module.h>
  54. #include <linux/omap-iommu.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/property.h>
  57. #include <linux/regulator/consumer.h>
  58. #include <linux/slab.h>
  59. #include <linux/sched.h>
  60. #include <linux/vmalloc.h>
  61. #include <asm/dma-iommu.h>
  62. #include <media/v4l2-common.h>
  63. #include <media/v4l2-fwnode.h>
  64. #include <media/v4l2-device.h>
  65. #include <media/v4l2-mc.h>
  66. #include "isp.h"
  67. #include "ispreg.h"
  68. #include "ispccdc.h"
  69. #include "isppreview.h"
  70. #include "ispresizer.h"
  71. #include "ispcsi2.h"
  72. #include "ispccp2.h"
  73. #include "isph3a.h"
  74. #include "isphist.h"
  75. static unsigned int autoidle;
  76. module_param(autoidle, int, 0444);
  77. MODULE_PARM_DESC(autoidle, "Enable OMAP3ISP AUTOIDLE support");
  78. static void isp_save_ctx(struct isp_device *isp);
  79. static void isp_restore_ctx(struct isp_device *isp);
  80. static const struct isp_res_mapping isp_res_maps[] = {
  81. {
  82. .isp_rev = ISP_REVISION_2_0,
  83. .offset = {
  84. /* first MMIO area */
  85. 0x0000, /* base, len 0x0070 */
  86. 0x0400, /* ccp2, len 0x01f0 */
  87. 0x0600, /* ccdc, len 0x00a8 */
  88. 0x0a00, /* hist, len 0x0048 */
  89. 0x0c00, /* h3a, len 0x0060 */
  90. 0x0e00, /* preview, len 0x00a0 */
  91. 0x1000, /* resizer, len 0x00ac */
  92. 0x1200, /* sbl, len 0x00fc */
  93. /* second MMIO area */
  94. 0x0000, /* csi2a, len 0x0170 */
  95. 0x0170, /* csiphy2, len 0x000c */
  96. },
  97. .phy_type = ISP_PHY_TYPE_3430,
  98. },
  99. {
  100. .isp_rev = ISP_REVISION_15_0,
  101. .offset = {
  102. /* first MMIO area */
  103. 0x0000, /* base, len 0x0070 */
  104. 0x0400, /* ccp2, len 0x01f0 */
  105. 0x0600, /* ccdc, len 0x00a8 */
  106. 0x0a00, /* hist, len 0x0048 */
  107. 0x0c00, /* h3a, len 0x0060 */
  108. 0x0e00, /* preview, len 0x00a0 */
  109. 0x1000, /* resizer, len 0x00ac */
  110. 0x1200, /* sbl, len 0x00fc */
  111. /* second MMIO area */
  112. 0x0000, /* csi2a, len 0x0170 (1st area) */
  113. 0x0170, /* csiphy2, len 0x000c */
  114. 0x01c0, /* csi2a, len 0x0040 (2nd area) */
  115. 0x0400, /* csi2c, len 0x0170 (1st area) */
  116. 0x0570, /* csiphy1, len 0x000c */
  117. 0x05c0, /* csi2c, len 0x0040 (2nd area) */
  118. },
  119. .phy_type = ISP_PHY_TYPE_3630,
  120. },
  121. };
  122. /* Structure for saving/restoring ISP module registers */
  123. static struct isp_reg isp_reg_list[] = {
  124. {OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG, 0},
  125. {OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, 0},
  126. {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL, 0},
  127. {0, ISP_TOK_TERM, 0}
  128. };
  129. /*
  130. * omap3isp_flush - Post pending L3 bus writes by doing a register readback
  131. * @isp: OMAP3 ISP device
  132. *
  133. * In order to force posting of pending writes, we need to write and
  134. * readback the same register, in this case the revision register.
  135. *
  136. * See this link for reference:
  137. * http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
  138. */
  139. void omap3isp_flush(struct isp_device *isp)
  140. {
  141. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
  142. isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
  143. }
  144. /* -----------------------------------------------------------------------------
  145. * XCLK
  146. */
  147. #define to_isp_xclk(_hw) container_of(_hw, struct isp_xclk, hw)
  148. static void isp_xclk_update(struct isp_xclk *xclk, u32 divider)
  149. {
  150. switch (xclk->id) {
  151. case ISP_XCLK_A:
  152. isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
  153. ISPTCTRL_CTRL_DIVA_MASK,
  154. divider << ISPTCTRL_CTRL_DIVA_SHIFT);
  155. break;
  156. case ISP_XCLK_B:
  157. isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
  158. ISPTCTRL_CTRL_DIVB_MASK,
  159. divider << ISPTCTRL_CTRL_DIVB_SHIFT);
  160. break;
  161. }
  162. }
  163. static int isp_xclk_prepare(struct clk_hw *hw)
  164. {
  165. struct isp_xclk *xclk = to_isp_xclk(hw);
  166. omap3isp_get(xclk->isp);
  167. return 0;
  168. }
  169. static void isp_xclk_unprepare(struct clk_hw *hw)
  170. {
  171. struct isp_xclk *xclk = to_isp_xclk(hw);
  172. omap3isp_put(xclk->isp);
  173. }
  174. static int isp_xclk_enable(struct clk_hw *hw)
  175. {
  176. struct isp_xclk *xclk = to_isp_xclk(hw);
  177. unsigned long flags;
  178. spin_lock_irqsave(&xclk->lock, flags);
  179. isp_xclk_update(xclk, xclk->divider);
  180. xclk->enabled = true;
  181. spin_unlock_irqrestore(&xclk->lock, flags);
  182. return 0;
  183. }
  184. static void isp_xclk_disable(struct clk_hw *hw)
  185. {
  186. struct isp_xclk *xclk = to_isp_xclk(hw);
  187. unsigned long flags;
  188. spin_lock_irqsave(&xclk->lock, flags);
  189. isp_xclk_update(xclk, 0);
  190. xclk->enabled = false;
  191. spin_unlock_irqrestore(&xclk->lock, flags);
  192. }
  193. static unsigned long isp_xclk_recalc_rate(struct clk_hw *hw,
  194. unsigned long parent_rate)
  195. {
  196. struct isp_xclk *xclk = to_isp_xclk(hw);
  197. return parent_rate / xclk->divider;
  198. }
  199. static u32 isp_xclk_calc_divider(unsigned long *rate, unsigned long parent_rate)
  200. {
  201. u32 divider;
  202. if (*rate >= parent_rate) {
  203. *rate = parent_rate;
  204. return ISPTCTRL_CTRL_DIV_BYPASS;
  205. }
  206. if (*rate == 0)
  207. *rate = 1;
  208. divider = DIV_ROUND_CLOSEST(parent_rate, *rate);
  209. if (divider >= ISPTCTRL_CTRL_DIV_BYPASS)
  210. divider = ISPTCTRL_CTRL_DIV_BYPASS - 1;
  211. *rate = parent_rate / divider;
  212. return divider;
  213. }
  214. static long isp_xclk_round_rate(struct clk_hw *hw, unsigned long rate,
  215. unsigned long *parent_rate)
  216. {
  217. isp_xclk_calc_divider(&rate, *parent_rate);
  218. return rate;
  219. }
  220. static int isp_xclk_set_rate(struct clk_hw *hw, unsigned long rate,
  221. unsigned long parent_rate)
  222. {
  223. struct isp_xclk *xclk = to_isp_xclk(hw);
  224. unsigned long flags;
  225. u32 divider;
  226. divider = isp_xclk_calc_divider(&rate, parent_rate);
  227. spin_lock_irqsave(&xclk->lock, flags);
  228. xclk->divider = divider;
  229. if (xclk->enabled)
  230. isp_xclk_update(xclk, divider);
  231. spin_unlock_irqrestore(&xclk->lock, flags);
  232. dev_dbg(xclk->isp->dev, "%s: cam_xclk%c set to %lu Hz (div %u)\n",
  233. __func__, xclk->id == ISP_XCLK_A ? 'a' : 'b', rate, divider);
  234. return 0;
  235. }
  236. static const struct clk_ops isp_xclk_ops = {
  237. .prepare = isp_xclk_prepare,
  238. .unprepare = isp_xclk_unprepare,
  239. .enable = isp_xclk_enable,
  240. .disable = isp_xclk_disable,
  241. .recalc_rate = isp_xclk_recalc_rate,
  242. .round_rate = isp_xclk_round_rate,
  243. .set_rate = isp_xclk_set_rate,
  244. };
  245. static const char *isp_xclk_parent_name = "cam_mclk";
  246. static const struct clk_init_data isp_xclk_init_data = {
  247. .name = "cam_xclk",
  248. .ops = &isp_xclk_ops,
  249. .parent_names = &isp_xclk_parent_name,
  250. .num_parents = 1,
  251. };
  252. static struct clk *isp_xclk_src_get(struct of_phandle_args *clkspec, void *data)
  253. {
  254. unsigned int idx = clkspec->args[0];
  255. struct isp_device *isp = data;
  256. if (idx >= ARRAY_SIZE(isp->xclks))
  257. return ERR_PTR(-ENOENT);
  258. return isp->xclks[idx].clk;
  259. }
  260. static int isp_xclk_init(struct isp_device *isp)
  261. {
  262. struct device_node *np = isp->dev->of_node;
  263. struct clk_init_data init;
  264. unsigned int i;
  265. for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i)
  266. isp->xclks[i].clk = ERR_PTR(-EINVAL);
  267. for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
  268. struct isp_xclk *xclk = &isp->xclks[i];
  269. xclk->isp = isp;
  270. xclk->id = i == 0 ? ISP_XCLK_A : ISP_XCLK_B;
  271. xclk->divider = 1;
  272. spin_lock_init(&xclk->lock);
  273. init.name = i == 0 ? "cam_xclka" : "cam_xclkb";
  274. init.ops = &isp_xclk_ops;
  275. init.parent_names = &isp_xclk_parent_name;
  276. init.num_parents = 1;
  277. xclk->hw.init = &init;
  278. /*
  279. * The first argument is NULL in order to avoid circular
  280. * reference, as this driver takes reference on the
  281. * sensor subdevice modules and the sensors would take
  282. * reference on this module through clk_get().
  283. */
  284. xclk->clk = clk_register(NULL, &xclk->hw);
  285. if (IS_ERR(xclk->clk))
  286. return PTR_ERR(xclk->clk);
  287. }
  288. if (np)
  289. of_clk_add_provider(np, isp_xclk_src_get, isp);
  290. return 0;
  291. }
  292. static void isp_xclk_cleanup(struct isp_device *isp)
  293. {
  294. struct device_node *np = isp->dev->of_node;
  295. unsigned int i;
  296. if (np)
  297. of_clk_del_provider(np);
  298. for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
  299. struct isp_xclk *xclk = &isp->xclks[i];
  300. if (!IS_ERR(xclk->clk))
  301. clk_unregister(xclk->clk);
  302. }
  303. }
  304. /* -----------------------------------------------------------------------------
  305. * Interrupts
  306. */
  307. /*
  308. * isp_enable_interrupts - Enable ISP interrupts.
  309. * @isp: OMAP3 ISP device
  310. */
  311. static void isp_enable_interrupts(struct isp_device *isp)
  312. {
  313. static const u32 irq = IRQ0ENABLE_CSIA_IRQ
  314. | IRQ0ENABLE_CSIB_IRQ
  315. | IRQ0ENABLE_CCDC_LSC_PREF_ERR_IRQ
  316. | IRQ0ENABLE_CCDC_LSC_DONE_IRQ
  317. | IRQ0ENABLE_CCDC_VD0_IRQ
  318. | IRQ0ENABLE_CCDC_VD1_IRQ
  319. | IRQ0ENABLE_HS_VS_IRQ
  320. | IRQ0ENABLE_HIST_DONE_IRQ
  321. | IRQ0ENABLE_H3A_AWB_DONE_IRQ
  322. | IRQ0ENABLE_H3A_AF_DONE_IRQ
  323. | IRQ0ENABLE_PRV_DONE_IRQ
  324. | IRQ0ENABLE_RSZ_DONE_IRQ;
  325. isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
  326. isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
  327. }
  328. /*
  329. * isp_disable_interrupts - Disable ISP interrupts.
  330. * @isp: OMAP3 ISP device
  331. */
  332. static void isp_disable_interrupts(struct isp_device *isp)
  333. {
  334. isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
  335. }
  336. /*
  337. * isp_core_init - ISP core settings
  338. * @isp: OMAP3 ISP device
  339. * @idle: Consider idle state.
  340. *
  341. * Set the power settings for the ISP and SBL bus and configure the HS/VS
  342. * interrupt source.
  343. *
  344. * We need to configure the HS/VS interrupt source before interrupts get
  345. * enabled, as the sensor might be free-running and the ISP default setting
  346. * (HS edge) would put an unnecessary burden on the CPU.
  347. */
  348. static void isp_core_init(struct isp_device *isp, int idle)
  349. {
  350. isp_reg_writel(isp,
  351. ((idle ? ISP_SYSCONFIG_MIDLEMODE_SMARTSTANDBY :
  352. ISP_SYSCONFIG_MIDLEMODE_FORCESTANDBY) <<
  353. ISP_SYSCONFIG_MIDLEMODE_SHIFT) |
  354. ((isp->revision == ISP_REVISION_15_0) ?
  355. ISP_SYSCONFIG_AUTOIDLE : 0),
  356. OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
  357. isp_reg_writel(isp,
  358. (isp->autoidle ? ISPCTRL_SBL_AUTOIDLE : 0) |
  359. ISPCTRL_SYNC_DETECT_VSRISE,
  360. OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
  361. }
  362. /*
  363. * Configure the bridge and lane shifter. Valid inputs are
  364. *
  365. * CCDC_INPUT_PARALLEL: Parallel interface
  366. * CCDC_INPUT_CSI2A: CSI2a receiver
  367. * CCDC_INPUT_CCP2B: CCP2b receiver
  368. * CCDC_INPUT_CSI2C: CSI2c receiver
  369. *
  370. * The bridge and lane shifter are configured according to the selected input
  371. * and the ISP platform data.
  372. */
  373. void omap3isp_configure_bridge(struct isp_device *isp,
  374. enum ccdc_input_entity input,
  375. const struct isp_parallel_cfg *parcfg,
  376. unsigned int shift, unsigned int bridge)
  377. {
  378. u32 ispctrl_val;
  379. ispctrl_val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
  380. ispctrl_val &= ~ISPCTRL_SHIFT_MASK;
  381. ispctrl_val &= ~ISPCTRL_PAR_CLK_POL_INV;
  382. ispctrl_val &= ~ISPCTRL_PAR_SER_CLK_SEL_MASK;
  383. ispctrl_val &= ~ISPCTRL_PAR_BRIDGE_MASK;
  384. ispctrl_val |= bridge;
  385. switch (input) {
  386. case CCDC_INPUT_PARALLEL:
  387. ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_PARALLEL;
  388. ispctrl_val |= parcfg->clk_pol << ISPCTRL_PAR_CLK_POL_SHIFT;
  389. shift += parcfg->data_lane_shift;
  390. break;
  391. case CCDC_INPUT_CSI2A:
  392. ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIA;
  393. break;
  394. case CCDC_INPUT_CCP2B:
  395. ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIB;
  396. break;
  397. case CCDC_INPUT_CSI2C:
  398. ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIC;
  399. break;
  400. default:
  401. return;
  402. }
  403. ispctrl_val |= ((shift/2) << ISPCTRL_SHIFT_SHIFT) & ISPCTRL_SHIFT_MASK;
  404. isp_reg_writel(isp, ispctrl_val, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
  405. }
  406. void omap3isp_hist_dma_done(struct isp_device *isp)
  407. {
  408. if (omap3isp_ccdc_busy(&isp->isp_ccdc) ||
  409. omap3isp_stat_pcr_busy(&isp->isp_hist)) {
  410. /* Histogram cannot be enabled in this frame anymore */
  411. atomic_set(&isp->isp_hist.buf_err, 1);
  412. dev_dbg(isp->dev,
  413. "hist: Out of synchronization with CCDC. Ignoring next buffer.\n");
  414. }
  415. }
  416. static inline void isp_isr_dbg(struct isp_device *isp, u32 irqstatus)
  417. {
  418. static const char *name[] = {
  419. "CSIA_IRQ",
  420. "res1",
  421. "res2",
  422. "CSIB_LCM_IRQ",
  423. "CSIB_IRQ",
  424. "res5",
  425. "res6",
  426. "res7",
  427. "CCDC_VD0_IRQ",
  428. "CCDC_VD1_IRQ",
  429. "CCDC_VD2_IRQ",
  430. "CCDC_ERR_IRQ",
  431. "H3A_AF_DONE_IRQ",
  432. "H3A_AWB_DONE_IRQ",
  433. "res14",
  434. "res15",
  435. "HIST_DONE_IRQ",
  436. "CCDC_LSC_DONE",
  437. "CCDC_LSC_PREFETCH_COMPLETED",
  438. "CCDC_LSC_PREFETCH_ERROR",
  439. "PRV_DONE_IRQ",
  440. "CBUFF_IRQ",
  441. "res22",
  442. "res23",
  443. "RSZ_DONE_IRQ",
  444. "OVF_IRQ",
  445. "res26",
  446. "res27",
  447. "MMU_ERR_IRQ",
  448. "OCP_ERR_IRQ",
  449. "SEC_ERR_IRQ",
  450. "HS_VS_IRQ",
  451. };
  452. int i;
  453. dev_dbg(isp->dev, "ISP IRQ: ");
  454. for (i = 0; i < ARRAY_SIZE(name); i++) {
  455. if ((1 << i) & irqstatus)
  456. printk(KERN_CONT "%s ", name[i]);
  457. }
  458. printk(KERN_CONT "\n");
  459. }
  460. static void isp_isr_sbl(struct isp_device *isp)
  461. {
  462. struct device *dev = isp->dev;
  463. struct isp_pipeline *pipe;
  464. u32 sbl_pcr;
  465. /*
  466. * Handle shared buffer logic overflows for video buffers.
  467. * ISPSBL_PCR_CCDCPRV_2_RSZ_OVF can be safely ignored.
  468. */
  469. sbl_pcr = isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
  470. isp_reg_writel(isp, sbl_pcr, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
  471. sbl_pcr &= ~ISPSBL_PCR_CCDCPRV_2_RSZ_OVF;
  472. if (sbl_pcr)
  473. dev_dbg(dev, "SBL overflow (PCR = 0x%08x)\n", sbl_pcr);
  474. if (sbl_pcr & ISPSBL_PCR_CSIB_WBL_OVF) {
  475. pipe = to_isp_pipeline(&isp->isp_ccp2.subdev.entity);
  476. if (pipe != NULL)
  477. pipe->error = true;
  478. }
  479. if (sbl_pcr & ISPSBL_PCR_CSIA_WBL_OVF) {
  480. pipe = to_isp_pipeline(&isp->isp_csi2a.subdev.entity);
  481. if (pipe != NULL)
  482. pipe->error = true;
  483. }
  484. if (sbl_pcr & ISPSBL_PCR_CCDC_WBL_OVF) {
  485. pipe = to_isp_pipeline(&isp->isp_ccdc.subdev.entity);
  486. if (pipe != NULL)
  487. pipe->error = true;
  488. }
  489. if (sbl_pcr & ISPSBL_PCR_PRV_WBL_OVF) {
  490. pipe = to_isp_pipeline(&isp->isp_prev.subdev.entity);
  491. if (pipe != NULL)
  492. pipe->error = true;
  493. }
  494. if (sbl_pcr & (ISPSBL_PCR_RSZ1_WBL_OVF
  495. | ISPSBL_PCR_RSZ2_WBL_OVF
  496. | ISPSBL_PCR_RSZ3_WBL_OVF
  497. | ISPSBL_PCR_RSZ4_WBL_OVF)) {
  498. pipe = to_isp_pipeline(&isp->isp_res.subdev.entity);
  499. if (pipe != NULL)
  500. pipe->error = true;
  501. }
  502. if (sbl_pcr & ISPSBL_PCR_H3A_AF_WBL_OVF)
  503. omap3isp_stat_sbl_overflow(&isp->isp_af);
  504. if (sbl_pcr & ISPSBL_PCR_H3A_AEAWB_WBL_OVF)
  505. omap3isp_stat_sbl_overflow(&isp->isp_aewb);
  506. }
  507. /*
  508. * isp_isr - Interrupt Service Routine for Camera ISP module.
  509. * @irq: Not used currently.
  510. * @_isp: Pointer to the OMAP3 ISP device
  511. *
  512. * Handles the corresponding callback if plugged in.
  513. */
  514. static irqreturn_t isp_isr(int irq, void *_isp)
  515. {
  516. static const u32 ccdc_events = IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ |
  517. IRQ0STATUS_CCDC_LSC_DONE_IRQ |
  518. IRQ0STATUS_CCDC_VD0_IRQ |
  519. IRQ0STATUS_CCDC_VD1_IRQ |
  520. IRQ0STATUS_HS_VS_IRQ;
  521. struct isp_device *isp = _isp;
  522. u32 irqstatus;
  523. irqstatus = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
  524. isp_reg_writel(isp, irqstatus, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
  525. isp_isr_sbl(isp);
  526. if (irqstatus & IRQ0STATUS_CSIA_IRQ)
  527. omap3isp_csi2_isr(&isp->isp_csi2a);
  528. if (irqstatus & IRQ0STATUS_CSIB_IRQ)
  529. omap3isp_ccp2_isr(&isp->isp_ccp2);
  530. if (irqstatus & IRQ0STATUS_CCDC_VD0_IRQ) {
  531. if (isp->isp_ccdc.output & CCDC_OUTPUT_PREVIEW)
  532. omap3isp_preview_isr_frame_sync(&isp->isp_prev);
  533. if (isp->isp_ccdc.output & CCDC_OUTPUT_RESIZER)
  534. omap3isp_resizer_isr_frame_sync(&isp->isp_res);
  535. omap3isp_stat_isr_frame_sync(&isp->isp_aewb);
  536. omap3isp_stat_isr_frame_sync(&isp->isp_af);
  537. omap3isp_stat_isr_frame_sync(&isp->isp_hist);
  538. }
  539. if (irqstatus & ccdc_events)
  540. omap3isp_ccdc_isr(&isp->isp_ccdc, irqstatus & ccdc_events);
  541. if (irqstatus & IRQ0STATUS_PRV_DONE_IRQ) {
  542. if (isp->isp_prev.output & PREVIEW_OUTPUT_RESIZER)
  543. omap3isp_resizer_isr_frame_sync(&isp->isp_res);
  544. omap3isp_preview_isr(&isp->isp_prev);
  545. }
  546. if (irqstatus & IRQ0STATUS_RSZ_DONE_IRQ)
  547. omap3isp_resizer_isr(&isp->isp_res);
  548. if (irqstatus & IRQ0STATUS_H3A_AWB_DONE_IRQ)
  549. omap3isp_stat_isr(&isp->isp_aewb);
  550. if (irqstatus & IRQ0STATUS_H3A_AF_DONE_IRQ)
  551. omap3isp_stat_isr(&isp->isp_af);
  552. if (irqstatus & IRQ0STATUS_HIST_DONE_IRQ)
  553. omap3isp_stat_isr(&isp->isp_hist);
  554. omap3isp_flush(isp);
  555. #if defined(DEBUG) && defined(ISP_ISR_DEBUG)
  556. isp_isr_dbg(isp, irqstatus);
  557. #endif
  558. return IRQ_HANDLED;
  559. }
  560. static const struct media_device_ops isp_media_ops = {
  561. .link_notify = v4l2_pipeline_link_notify,
  562. };
  563. /* -----------------------------------------------------------------------------
  564. * Pipeline stream management
  565. */
  566. /*
  567. * isp_pipeline_enable - Enable streaming on a pipeline
  568. * @pipe: ISP pipeline
  569. * @mode: Stream mode (single shot or continuous)
  570. *
  571. * Walk the entities chain starting at the pipeline output video node and start
  572. * all modules in the chain in the given mode.
  573. *
  574. * Return 0 if successful, or the return value of the failed video::s_stream
  575. * operation otherwise.
  576. */
  577. static int isp_pipeline_enable(struct isp_pipeline *pipe,
  578. enum isp_pipeline_stream_state mode)
  579. {
  580. struct isp_device *isp = pipe->output->isp;
  581. struct media_entity *entity;
  582. struct media_pad *pad;
  583. struct v4l2_subdev *subdev;
  584. unsigned long flags;
  585. int ret;
  586. /* Refuse to start streaming if an entity included in the pipeline has
  587. * crashed. This check must be performed before the loop below to avoid
  588. * starting entities if the pipeline won't start anyway (those entities
  589. * would then likely fail to stop, making the problem worse).
  590. */
  591. if (media_entity_enum_intersects(&pipe->ent_enum, &isp->crashed))
  592. return -EIO;
  593. spin_lock_irqsave(&pipe->lock, flags);
  594. pipe->state &= ~(ISP_PIPELINE_IDLE_INPUT | ISP_PIPELINE_IDLE_OUTPUT);
  595. spin_unlock_irqrestore(&pipe->lock, flags);
  596. pipe->do_propagation = false;
  597. entity = &pipe->output->video.entity;
  598. while (1) {
  599. pad = &entity->pads[0];
  600. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  601. break;
  602. pad = media_entity_remote_pad(pad);
  603. if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
  604. break;
  605. entity = pad->entity;
  606. subdev = media_entity_to_v4l2_subdev(entity);
  607. ret = v4l2_subdev_call(subdev, video, s_stream, mode);
  608. if (ret < 0 && ret != -ENOIOCTLCMD)
  609. return ret;
  610. if (subdev == &isp->isp_ccdc.subdev) {
  611. v4l2_subdev_call(&isp->isp_aewb.subdev, video,
  612. s_stream, mode);
  613. v4l2_subdev_call(&isp->isp_af.subdev, video,
  614. s_stream, mode);
  615. v4l2_subdev_call(&isp->isp_hist.subdev, video,
  616. s_stream, mode);
  617. pipe->do_propagation = true;
  618. }
  619. }
  620. return 0;
  621. }
  622. static int isp_pipeline_wait_resizer(struct isp_device *isp)
  623. {
  624. return omap3isp_resizer_busy(&isp->isp_res);
  625. }
  626. static int isp_pipeline_wait_preview(struct isp_device *isp)
  627. {
  628. return omap3isp_preview_busy(&isp->isp_prev);
  629. }
  630. static int isp_pipeline_wait_ccdc(struct isp_device *isp)
  631. {
  632. return omap3isp_stat_busy(&isp->isp_af)
  633. || omap3isp_stat_busy(&isp->isp_aewb)
  634. || omap3isp_stat_busy(&isp->isp_hist)
  635. || omap3isp_ccdc_busy(&isp->isp_ccdc);
  636. }
  637. #define ISP_STOP_TIMEOUT msecs_to_jiffies(1000)
  638. static int isp_pipeline_wait(struct isp_device *isp,
  639. int(*busy)(struct isp_device *isp))
  640. {
  641. unsigned long timeout = jiffies + ISP_STOP_TIMEOUT;
  642. while (!time_after(jiffies, timeout)) {
  643. if (!busy(isp))
  644. return 0;
  645. }
  646. return 1;
  647. }
  648. /*
  649. * isp_pipeline_disable - Disable streaming on a pipeline
  650. * @pipe: ISP pipeline
  651. *
  652. * Walk the entities chain starting at the pipeline output video node and stop
  653. * all modules in the chain. Wait synchronously for the modules to be stopped if
  654. * necessary.
  655. *
  656. * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
  657. * can't be stopped (in which case a software reset of the ISP is probably
  658. * necessary).
  659. */
  660. static int isp_pipeline_disable(struct isp_pipeline *pipe)
  661. {
  662. struct isp_device *isp = pipe->output->isp;
  663. struct media_entity *entity;
  664. struct media_pad *pad;
  665. struct v4l2_subdev *subdev;
  666. int failure = 0;
  667. int ret;
  668. /*
  669. * We need to stop all the modules after CCDC first or they'll
  670. * never stop since they may not get a full frame from CCDC.
  671. */
  672. entity = &pipe->output->video.entity;
  673. while (1) {
  674. pad = &entity->pads[0];
  675. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  676. break;
  677. pad = media_entity_remote_pad(pad);
  678. if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
  679. break;
  680. entity = pad->entity;
  681. subdev = media_entity_to_v4l2_subdev(entity);
  682. if (subdev == &isp->isp_ccdc.subdev) {
  683. v4l2_subdev_call(&isp->isp_aewb.subdev,
  684. video, s_stream, 0);
  685. v4l2_subdev_call(&isp->isp_af.subdev,
  686. video, s_stream, 0);
  687. v4l2_subdev_call(&isp->isp_hist.subdev,
  688. video, s_stream, 0);
  689. }
  690. ret = v4l2_subdev_call(subdev, video, s_stream, 0);
  691. if (subdev == &isp->isp_res.subdev)
  692. ret |= isp_pipeline_wait(isp, isp_pipeline_wait_resizer);
  693. else if (subdev == &isp->isp_prev.subdev)
  694. ret |= isp_pipeline_wait(isp, isp_pipeline_wait_preview);
  695. else if (subdev == &isp->isp_ccdc.subdev)
  696. ret |= isp_pipeline_wait(isp, isp_pipeline_wait_ccdc);
  697. /* Handle stop failures. An entity that fails to stop can
  698. * usually just be restarted. Flag the stop failure nonetheless
  699. * to trigger an ISP reset the next time the device is released,
  700. * just in case.
  701. *
  702. * The preview engine is a special case. A failure to stop can
  703. * mean a hardware crash. When that happens the preview engine
  704. * won't respond to read/write operations on the L4 bus anymore,
  705. * resulting in a bus fault and a kernel oops next time it gets
  706. * accessed. Mark it as crashed to prevent pipelines including
  707. * it from being started.
  708. */
  709. if (ret) {
  710. dev_info(isp->dev, "Unable to stop %s\n", subdev->name);
  711. isp->stop_failure = true;
  712. if (subdev == &isp->isp_prev.subdev)
  713. media_entity_enum_set(&isp->crashed,
  714. &subdev->entity);
  715. failure = -ETIMEDOUT;
  716. }
  717. }
  718. return failure;
  719. }
  720. /*
  721. * omap3isp_pipeline_set_stream - Enable/disable streaming on a pipeline
  722. * @pipe: ISP pipeline
  723. * @state: Stream state (stopped, single shot or continuous)
  724. *
  725. * Set the pipeline to the given stream state. Pipelines can be started in
  726. * single-shot or continuous mode.
  727. *
  728. * Return 0 if successful, or the return value of the failed video::s_stream
  729. * operation otherwise. The pipeline state is not updated when the operation
  730. * fails, except when stopping the pipeline.
  731. */
  732. int omap3isp_pipeline_set_stream(struct isp_pipeline *pipe,
  733. enum isp_pipeline_stream_state state)
  734. {
  735. int ret;
  736. if (state == ISP_PIPELINE_STREAM_STOPPED)
  737. ret = isp_pipeline_disable(pipe);
  738. else
  739. ret = isp_pipeline_enable(pipe, state);
  740. if (ret == 0 || state == ISP_PIPELINE_STREAM_STOPPED)
  741. pipe->stream_state = state;
  742. return ret;
  743. }
  744. /*
  745. * omap3isp_pipeline_cancel_stream - Cancel stream on a pipeline
  746. * @pipe: ISP pipeline
  747. *
  748. * Cancelling a stream mark all buffers on all video nodes in the pipeline as
  749. * erroneous and makes sure no new buffer can be queued. This function is called
  750. * when a fatal error that prevents any further operation on the pipeline
  751. * occurs.
  752. */
  753. void omap3isp_pipeline_cancel_stream(struct isp_pipeline *pipe)
  754. {
  755. if (pipe->input)
  756. omap3isp_video_cancel_stream(pipe->input);
  757. if (pipe->output)
  758. omap3isp_video_cancel_stream(pipe->output);
  759. }
  760. /*
  761. * isp_pipeline_resume - Resume streaming on a pipeline
  762. * @pipe: ISP pipeline
  763. *
  764. * Resume video output and input and re-enable pipeline.
  765. */
  766. static void isp_pipeline_resume(struct isp_pipeline *pipe)
  767. {
  768. int singleshot = pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT;
  769. omap3isp_video_resume(pipe->output, !singleshot);
  770. if (singleshot)
  771. omap3isp_video_resume(pipe->input, 0);
  772. isp_pipeline_enable(pipe, pipe->stream_state);
  773. }
  774. /*
  775. * isp_pipeline_suspend - Suspend streaming on a pipeline
  776. * @pipe: ISP pipeline
  777. *
  778. * Suspend pipeline.
  779. */
  780. static void isp_pipeline_suspend(struct isp_pipeline *pipe)
  781. {
  782. isp_pipeline_disable(pipe);
  783. }
  784. /*
  785. * isp_pipeline_is_last - Verify if entity has an enabled link to the output
  786. * video node
  787. * @me: ISP module's media entity
  788. *
  789. * Returns 1 if the entity has an enabled link to the output video node or 0
  790. * otherwise. It's true only while pipeline can have no more than one output
  791. * node.
  792. */
  793. static int isp_pipeline_is_last(struct media_entity *me)
  794. {
  795. struct isp_pipeline *pipe;
  796. struct media_pad *pad;
  797. if (!me->pipe)
  798. return 0;
  799. pipe = to_isp_pipeline(me);
  800. if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED)
  801. return 0;
  802. pad = media_entity_remote_pad(&pipe->output->pad);
  803. return pad->entity == me;
  804. }
  805. /*
  806. * isp_suspend_module_pipeline - Suspend pipeline to which belongs the module
  807. * @me: ISP module's media entity
  808. *
  809. * Suspend the whole pipeline if module's entity has an enabled link to the
  810. * output video node. It works only while pipeline can have no more than one
  811. * output node.
  812. */
  813. static void isp_suspend_module_pipeline(struct media_entity *me)
  814. {
  815. if (isp_pipeline_is_last(me))
  816. isp_pipeline_suspend(to_isp_pipeline(me));
  817. }
  818. /*
  819. * isp_resume_module_pipeline - Resume pipeline to which belongs the module
  820. * @me: ISP module's media entity
  821. *
  822. * Resume the whole pipeline if module's entity has an enabled link to the
  823. * output video node. It works only while pipeline can have no more than one
  824. * output node.
  825. */
  826. static void isp_resume_module_pipeline(struct media_entity *me)
  827. {
  828. if (isp_pipeline_is_last(me))
  829. isp_pipeline_resume(to_isp_pipeline(me));
  830. }
  831. /*
  832. * isp_suspend_modules - Suspend ISP submodules.
  833. * @isp: OMAP3 ISP device
  834. *
  835. * Returns 0 if suspend left in idle state all the submodules properly,
  836. * or returns 1 if a general Reset is required to suspend the submodules.
  837. */
  838. static int isp_suspend_modules(struct isp_device *isp)
  839. {
  840. unsigned long timeout;
  841. omap3isp_stat_suspend(&isp->isp_aewb);
  842. omap3isp_stat_suspend(&isp->isp_af);
  843. omap3isp_stat_suspend(&isp->isp_hist);
  844. isp_suspend_module_pipeline(&isp->isp_res.subdev.entity);
  845. isp_suspend_module_pipeline(&isp->isp_prev.subdev.entity);
  846. isp_suspend_module_pipeline(&isp->isp_ccdc.subdev.entity);
  847. isp_suspend_module_pipeline(&isp->isp_csi2a.subdev.entity);
  848. isp_suspend_module_pipeline(&isp->isp_ccp2.subdev.entity);
  849. timeout = jiffies + ISP_STOP_TIMEOUT;
  850. while (omap3isp_stat_busy(&isp->isp_af)
  851. || omap3isp_stat_busy(&isp->isp_aewb)
  852. || omap3isp_stat_busy(&isp->isp_hist)
  853. || omap3isp_preview_busy(&isp->isp_prev)
  854. || omap3isp_resizer_busy(&isp->isp_res)
  855. || omap3isp_ccdc_busy(&isp->isp_ccdc)) {
  856. if (time_after(jiffies, timeout)) {
  857. dev_info(isp->dev, "can't stop modules.\n");
  858. return 1;
  859. }
  860. msleep(1);
  861. }
  862. return 0;
  863. }
  864. /*
  865. * isp_resume_modules - Resume ISP submodules.
  866. * @isp: OMAP3 ISP device
  867. */
  868. static void isp_resume_modules(struct isp_device *isp)
  869. {
  870. omap3isp_stat_resume(&isp->isp_aewb);
  871. omap3isp_stat_resume(&isp->isp_af);
  872. omap3isp_stat_resume(&isp->isp_hist);
  873. isp_resume_module_pipeline(&isp->isp_res.subdev.entity);
  874. isp_resume_module_pipeline(&isp->isp_prev.subdev.entity);
  875. isp_resume_module_pipeline(&isp->isp_ccdc.subdev.entity);
  876. isp_resume_module_pipeline(&isp->isp_csi2a.subdev.entity);
  877. isp_resume_module_pipeline(&isp->isp_ccp2.subdev.entity);
  878. }
  879. /*
  880. * isp_reset - Reset ISP with a timeout wait for idle.
  881. * @isp: OMAP3 ISP device
  882. */
  883. static int isp_reset(struct isp_device *isp)
  884. {
  885. unsigned long timeout = 0;
  886. isp_reg_writel(isp,
  887. isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG)
  888. | ISP_SYSCONFIG_SOFTRESET,
  889. OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
  890. while (!(isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN,
  891. ISP_SYSSTATUS) & 0x1)) {
  892. if (timeout++ > 10000) {
  893. dev_alert(isp->dev, "cannot reset ISP\n");
  894. return -ETIMEDOUT;
  895. }
  896. udelay(1);
  897. }
  898. isp->stop_failure = false;
  899. media_entity_enum_zero(&isp->crashed);
  900. return 0;
  901. }
  902. /*
  903. * isp_save_context - Saves the values of the ISP module registers.
  904. * @isp: OMAP3 ISP device
  905. * @reg_list: Structure containing pairs of register address and value to
  906. * modify on OMAP.
  907. */
  908. static void
  909. isp_save_context(struct isp_device *isp, struct isp_reg *reg_list)
  910. {
  911. struct isp_reg *next = reg_list;
  912. for (; next->reg != ISP_TOK_TERM; next++)
  913. next->val = isp_reg_readl(isp, next->mmio_range, next->reg);
  914. }
  915. /*
  916. * isp_restore_context - Restores the values of the ISP module registers.
  917. * @isp: OMAP3 ISP device
  918. * @reg_list: Structure containing pairs of register address and value to
  919. * modify on OMAP.
  920. */
  921. static void
  922. isp_restore_context(struct isp_device *isp, struct isp_reg *reg_list)
  923. {
  924. struct isp_reg *next = reg_list;
  925. for (; next->reg != ISP_TOK_TERM; next++)
  926. isp_reg_writel(isp, next->val, next->mmio_range, next->reg);
  927. }
  928. /*
  929. * isp_save_ctx - Saves ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
  930. * @isp: OMAP3 ISP device
  931. *
  932. * Routine for saving the context of each module in the ISP.
  933. * CCDC, HIST, H3A, PREV, RESZ and MMU.
  934. */
  935. static void isp_save_ctx(struct isp_device *isp)
  936. {
  937. isp_save_context(isp, isp_reg_list);
  938. omap_iommu_save_ctx(isp->dev);
  939. }
  940. /*
  941. * isp_restore_ctx - Restores ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
  942. * @isp: OMAP3 ISP device
  943. *
  944. * Routine for restoring the context of each module in the ISP.
  945. * CCDC, HIST, H3A, PREV, RESZ and MMU.
  946. */
  947. static void isp_restore_ctx(struct isp_device *isp)
  948. {
  949. isp_restore_context(isp, isp_reg_list);
  950. omap_iommu_restore_ctx(isp->dev);
  951. omap3isp_ccdc_restore_context(isp);
  952. omap3isp_preview_restore_context(isp);
  953. }
  954. /* -----------------------------------------------------------------------------
  955. * SBL resources management
  956. */
  957. #define OMAP3_ISP_SBL_READ (OMAP3_ISP_SBL_CSI1_READ | \
  958. OMAP3_ISP_SBL_CCDC_LSC_READ | \
  959. OMAP3_ISP_SBL_PREVIEW_READ | \
  960. OMAP3_ISP_SBL_RESIZER_READ)
  961. #define OMAP3_ISP_SBL_WRITE (OMAP3_ISP_SBL_CSI1_WRITE | \
  962. OMAP3_ISP_SBL_CSI2A_WRITE | \
  963. OMAP3_ISP_SBL_CSI2C_WRITE | \
  964. OMAP3_ISP_SBL_CCDC_WRITE | \
  965. OMAP3_ISP_SBL_PREVIEW_WRITE)
  966. void omap3isp_sbl_enable(struct isp_device *isp, enum isp_sbl_resource res)
  967. {
  968. u32 sbl = 0;
  969. isp->sbl_resources |= res;
  970. if (isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ)
  971. sbl |= ISPCTRL_SBL_SHARED_RPORTA;
  972. if (isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ)
  973. sbl |= ISPCTRL_SBL_SHARED_RPORTB;
  974. if (isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE)
  975. sbl |= ISPCTRL_SBL_SHARED_WPORTC;
  976. if (isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE)
  977. sbl |= ISPCTRL_SBL_WR0_RAM_EN;
  978. if (isp->sbl_resources & OMAP3_ISP_SBL_WRITE)
  979. sbl |= ISPCTRL_SBL_WR1_RAM_EN;
  980. if (isp->sbl_resources & OMAP3_ISP_SBL_READ)
  981. sbl |= ISPCTRL_SBL_RD_RAM_EN;
  982. isp_reg_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
  983. }
  984. void omap3isp_sbl_disable(struct isp_device *isp, enum isp_sbl_resource res)
  985. {
  986. u32 sbl = 0;
  987. isp->sbl_resources &= ~res;
  988. if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ))
  989. sbl |= ISPCTRL_SBL_SHARED_RPORTA;
  990. if (!(isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ))
  991. sbl |= ISPCTRL_SBL_SHARED_RPORTB;
  992. if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE))
  993. sbl |= ISPCTRL_SBL_SHARED_WPORTC;
  994. if (!(isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE))
  995. sbl |= ISPCTRL_SBL_WR0_RAM_EN;
  996. if (!(isp->sbl_resources & OMAP3_ISP_SBL_WRITE))
  997. sbl |= ISPCTRL_SBL_WR1_RAM_EN;
  998. if (!(isp->sbl_resources & OMAP3_ISP_SBL_READ))
  999. sbl |= ISPCTRL_SBL_RD_RAM_EN;
  1000. isp_reg_clr(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
  1001. }
  1002. /*
  1003. * isp_module_sync_idle - Helper to sync module with its idle state
  1004. * @me: ISP submodule's media entity
  1005. * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
  1006. * @stopping: flag which tells module wants to stop
  1007. *
  1008. * This function checks if ISP submodule needs to wait for next interrupt. If
  1009. * yes, makes the caller to sleep while waiting for such event.
  1010. */
  1011. int omap3isp_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
  1012. atomic_t *stopping)
  1013. {
  1014. struct isp_pipeline *pipe = to_isp_pipeline(me);
  1015. if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED ||
  1016. (pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT &&
  1017. !isp_pipeline_ready(pipe)))
  1018. return 0;
  1019. /*
  1020. * atomic_set() doesn't include memory barrier on ARM platform for SMP
  1021. * scenario. We'll call it here to avoid race conditions.
  1022. */
  1023. atomic_set(stopping, 1);
  1024. smp_mb();
  1025. /*
  1026. * If module is the last one, it's writing to memory. In this case,
  1027. * it's necessary to check if the module is already paused due to
  1028. * DMA queue underrun or if it has to wait for next interrupt to be
  1029. * idle.
  1030. * If it isn't the last one, the function won't sleep but *stopping
  1031. * will still be set to warn next submodule caller's interrupt the
  1032. * module wants to be idle.
  1033. */
  1034. if (isp_pipeline_is_last(me)) {
  1035. struct isp_video *video = pipe->output;
  1036. unsigned long flags;
  1037. spin_lock_irqsave(&video->irqlock, flags);
  1038. if (video->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
  1039. spin_unlock_irqrestore(&video->irqlock, flags);
  1040. atomic_set(stopping, 0);
  1041. smp_mb();
  1042. return 0;
  1043. }
  1044. spin_unlock_irqrestore(&video->irqlock, flags);
  1045. if (!wait_event_timeout(*wait, !atomic_read(stopping),
  1046. msecs_to_jiffies(1000))) {
  1047. atomic_set(stopping, 0);
  1048. smp_mb();
  1049. return -ETIMEDOUT;
  1050. }
  1051. }
  1052. return 0;
  1053. }
  1054. /*
  1055. * omap3isp_module_sync_is_stopping - Helper to verify if module was stopping
  1056. * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
  1057. * @stopping: flag which tells module wants to stop
  1058. *
  1059. * This function checks if ISP submodule was stopping. In case of yes, it
  1060. * notices the caller by setting stopping to 0 and waking up the wait queue.
  1061. * Returns 1 if it was stopping or 0 otherwise.
  1062. */
  1063. int omap3isp_module_sync_is_stopping(wait_queue_head_t *wait,
  1064. atomic_t *stopping)
  1065. {
  1066. if (atomic_cmpxchg(stopping, 1, 0)) {
  1067. wake_up(wait);
  1068. return 1;
  1069. }
  1070. return 0;
  1071. }
  1072. /* --------------------------------------------------------------------------
  1073. * Clock management
  1074. */
  1075. #define ISPCTRL_CLKS_MASK (ISPCTRL_H3A_CLK_EN | \
  1076. ISPCTRL_HIST_CLK_EN | \
  1077. ISPCTRL_RSZ_CLK_EN | \
  1078. (ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN) | \
  1079. (ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN))
  1080. static void __isp_subclk_update(struct isp_device *isp)
  1081. {
  1082. u32 clk = 0;
  1083. /* AEWB and AF share the same clock. */
  1084. if (isp->subclk_resources &
  1085. (OMAP3_ISP_SUBCLK_AEWB | OMAP3_ISP_SUBCLK_AF))
  1086. clk |= ISPCTRL_H3A_CLK_EN;
  1087. if (isp->subclk_resources & OMAP3_ISP_SUBCLK_HIST)
  1088. clk |= ISPCTRL_HIST_CLK_EN;
  1089. if (isp->subclk_resources & OMAP3_ISP_SUBCLK_RESIZER)
  1090. clk |= ISPCTRL_RSZ_CLK_EN;
  1091. /* NOTE: For CCDC & Preview submodules, we need to affect internal
  1092. * RAM as well.
  1093. */
  1094. if (isp->subclk_resources & OMAP3_ISP_SUBCLK_CCDC)
  1095. clk |= ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN;
  1096. if (isp->subclk_resources & OMAP3_ISP_SUBCLK_PREVIEW)
  1097. clk |= ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN;
  1098. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
  1099. ISPCTRL_CLKS_MASK, clk);
  1100. }
  1101. void omap3isp_subclk_enable(struct isp_device *isp,
  1102. enum isp_subclk_resource res)
  1103. {
  1104. isp->subclk_resources |= res;
  1105. __isp_subclk_update(isp);
  1106. }
  1107. void omap3isp_subclk_disable(struct isp_device *isp,
  1108. enum isp_subclk_resource res)
  1109. {
  1110. isp->subclk_resources &= ~res;
  1111. __isp_subclk_update(isp);
  1112. }
  1113. /*
  1114. * isp_enable_clocks - Enable ISP clocks
  1115. * @isp: OMAP3 ISP device
  1116. *
  1117. * Return 0 if successful, or clk_prepare_enable return value if any of them
  1118. * fails.
  1119. */
  1120. static int isp_enable_clocks(struct isp_device *isp)
  1121. {
  1122. int r;
  1123. unsigned long rate;
  1124. r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_ICK]);
  1125. if (r) {
  1126. dev_err(isp->dev, "failed to enable cam_ick clock\n");
  1127. goto out_clk_enable_ick;
  1128. }
  1129. r = clk_set_rate(isp->clock[ISP_CLK_CAM_MCLK], CM_CAM_MCLK_HZ);
  1130. if (r) {
  1131. dev_err(isp->dev, "clk_set_rate for cam_mclk failed\n");
  1132. goto out_clk_enable_mclk;
  1133. }
  1134. r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_MCLK]);
  1135. if (r) {
  1136. dev_err(isp->dev, "failed to enable cam_mclk clock\n");
  1137. goto out_clk_enable_mclk;
  1138. }
  1139. rate = clk_get_rate(isp->clock[ISP_CLK_CAM_MCLK]);
  1140. if (rate != CM_CAM_MCLK_HZ)
  1141. dev_warn(isp->dev, "unexpected cam_mclk rate:\n"
  1142. " expected : %d\n"
  1143. " actual : %ld\n", CM_CAM_MCLK_HZ, rate);
  1144. r = clk_prepare_enable(isp->clock[ISP_CLK_CSI2_FCK]);
  1145. if (r) {
  1146. dev_err(isp->dev, "failed to enable csi2_fck clock\n");
  1147. goto out_clk_enable_csi2_fclk;
  1148. }
  1149. return 0;
  1150. out_clk_enable_csi2_fclk:
  1151. clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
  1152. out_clk_enable_mclk:
  1153. clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
  1154. out_clk_enable_ick:
  1155. return r;
  1156. }
  1157. /*
  1158. * isp_disable_clocks - Disable ISP clocks
  1159. * @isp: OMAP3 ISP device
  1160. */
  1161. static void isp_disable_clocks(struct isp_device *isp)
  1162. {
  1163. clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
  1164. clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
  1165. clk_disable_unprepare(isp->clock[ISP_CLK_CSI2_FCK]);
  1166. }
  1167. static const char *isp_clocks[] = {
  1168. "cam_ick",
  1169. "cam_mclk",
  1170. "csi2_96m_fck",
  1171. "l3_ick",
  1172. };
  1173. static int isp_get_clocks(struct isp_device *isp)
  1174. {
  1175. struct clk *clk;
  1176. unsigned int i;
  1177. for (i = 0; i < ARRAY_SIZE(isp_clocks); ++i) {
  1178. clk = devm_clk_get(isp->dev, isp_clocks[i]);
  1179. if (IS_ERR(clk)) {
  1180. dev_err(isp->dev, "clk_get %s failed\n", isp_clocks[i]);
  1181. return PTR_ERR(clk);
  1182. }
  1183. isp->clock[i] = clk;
  1184. }
  1185. return 0;
  1186. }
  1187. /*
  1188. * omap3isp_get - Acquire the ISP resource.
  1189. *
  1190. * Initializes the clocks for the first acquire.
  1191. *
  1192. * Increment the reference count on the ISP. If the first reference is taken,
  1193. * enable clocks and power-up all submodules.
  1194. *
  1195. * Return a pointer to the ISP device structure, or NULL if an error occurred.
  1196. */
  1197. static struct isp_device *__omap3isp_get(struct isp_device *isp, bool irq)
  1198. {
  1199. struct isp_device *__isp = isp;
  1200. if (isp == NULL)
  1201. return NULL;
  1202. mutex_lock(&isp->isp_mutex);
  1203. if (isp->ref_count > 0)
  1204. goto out;
  1205. if (isp_enable_clocks(isp) < 0) {
  1206. __isp = NULL;
  1207. goto out;
  1208. }
  1209. /* We don't want to restore context before saving it! */
  1210. if (isp->has_context)
  1211. isp_restore_ctx(isp);
  1212. if (irq)
  1213. isp_enable_interrupts(isp);
  1214. out:
  1215. if (__isp != NULL)
  1216. isp->ref_count++;
  1217. mutex_unlock(&isp->isp_mutex);
  1218. return __isp;
  1219. }
  1220. struct isp_device *omap3isp_get(struct isp_device *isp)
  1221. {
  1222. return __omap3isp_get(isp, true);
  1223. }
  1224. /*
  1225. * omap3isp_put - Release the ISP
  1226. *
  1227. * Decrement the reference count on the ISP. If the last reference is released,
  1228. * power-down all submodules, disable clocks and free temporary buffers.
  1229. */
  1230. static void __omap3isp_put(struct isp_device *isp, bool save_ctx)
  1231. {
  1232. if (isp == NULL)
  1233. return;
  1234. mutex_lock(&isp->isp_mutex);
  1235. BUG_ON(isp->ref_count == 0);
  1236. if (--isp->ref_count == 0) {
  1237. isp_disable_interrupts(isp);
  1238. if (save_ctx) {
  1239. isp_save_ctx(isp);
  1240. isp->has_context = 1;
  1241. }
  1242. /* Reset the ISP if an entity has failed to stop. This is the
  1243. * only way to recover from such conditions.
  1244. */
  1245. if (!media_entity_enum_empty(&isp->crashed) ||
  1246. isp->stop_failure)
  1247. isp_reset(isp);
  1248. isp_disable_clocks(isp);
  1249. }
  1250. mutex_unlock(&isp->isp_mutex);
  1251. }
  1252. void omap3isp_put(struct isp_device *isp)
  1253. {
  1254. __omap3isp_put(isp, true);
  1255. }
  1256. /* --------------------------------------------------------------------------
  1257. * Platform device driver
  1258. */
  1259. /*
  1260. * omap3isp_print_status - Prints the values of the ISP Control Module registers
  1261. * @isp: OMAP3 ISP device
  1262. */
  1263. #define ISP_PRINT_REGISTER(isp, name)\
  1264. dev_dbg(isp->dev, "###ISP " #name "=0x%08x\n", \
  1265. isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_##name))
  1266. #define SBL_PRINT_REGISTER(isp, name)\
  1267. dev_dbg(isp->dev, "###SBL " #name "=0x%08x\n", \
  1268. isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_##name))
  1269. void omap3isp_print_status(struct isp_device *isp)
  1270. {
  1271. dev_dbg(isp->dev, "-------------ISP Register dump--------------\n");
  1272. ISP_PRINT_REGISTER(isp, SYSCONFIG);
  1273. ISP_PRINT_REGISTER(isp, SYSSTATUS);
  1274. ISP_PRINT_REGISTER(isp, IRQ0ENABLE);
  1275. ISP_PRINT_REGISTER(isp, IRQ0STATUS);
  1276. ISP_PRINT_REGISTER(isp, TCTRL_GRESET_LENGTH);
  1277. ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_REPLAY);
  1278. ISP_PRINT_REGISTER(isp, CTRL);
  1279. ISP_PRINT_REGISTER(isp, TCTRL_CTRL);
  1280. ISP_PRINT_REGISTER(isp, TCTRL_FRAME);
  1281. ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_DELAY);
  1282. ISP_PRINT_REGISTER(isp, TCTRL_STRB_DELAY);
  1283. ISP_PRINT_REGISTER(isp, TCTRL_SHUT_DELAY);
  1284. ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_LENGTH);
  1285. ISP_PRINT_REGISTER(isp, TCTRL_STRB_LENGTH);
  1286. ISP_PRINT_REGISTER(isp, TCTRL_SHUT_LENGTH);
  1287. SBL_PRINT_REGISTER(isp, PCR);
  1288. SBL_PRINT_REGISTER(isp, SDR_REQ_EXP);
  1289. dev_dbg(isp->dev, "--------------------------------------------\n");
  1290. }
  1291. #ifdef CONFIG_PM
  1292. /*
  1293. * Power management support.
  1294. *
  1295. * As the ISP can't properly handle an input video stream interruption on a non
  1296. * frame boundary, the ISP pipelines need to be stopped before sensors get
  1297. * suspended. However, as suspending the sensors can require a running clock,
  1298. * which can be provided by the ISP, the ISP can't be completely suspended
  1299. * before the sensor.
  1300. *
  1301. * To solve this problem power management support is split into prepare/complete
  1302. * and suspend/resume operations. The pipelines are stopped in prepare() and the
  1303. * ISP clocks get disabled in suspend(). Similarly, the clocks are reenabled in
  1304. * resume(), and the the pipelines are restarted in complete().
  1305. *
  1306. * TODO: PM dependencies between the ISP and sensors are not modelled explicitly
  1307. * yet.
  1308. */
  1309. static int isp_pm_prepare(struct device *dev)
  1310. {
  1311. struct isp_device *isp = dev_get_drvdata(dev);
  1312. int reset;
  1313. WARN_ON(mutex_is_locked(&isp->isp_mutex));
  1314. if (isp->ref_count == 0)
  1315. return 0;
  1316. reset = isp_suspend_modules(isp);
  1317. isp_disable_interrupts(isp);
  1318. isp_save_ctx(isp);
  1319. if (reset)
  1320. isp_reset(isp);
  1321. return 0;
  1322. }
  1323. static int isp_pm_suspend(struct device *dev)
  1324. {
  1325. struct isp_device *isp = dev_get_drvdata(dev);
  1326. WARN_ON(mutex_is_locked(&isp->isp_mutex));
  1327. if (isp->ref_count)
  1328. isp_disable_clocks(isp);
  1329. return 0;
  1330. }
  1331. static int isp_pm_resume(struct device *dev)
  1332. {
  1333. struct isp_device *isp = dev_get_drvdata(dev);
  1334. if (isp->ref_count == 0)
  1335. return 0;
  1336. return isp_enable_clocks(isp);
  1337. }
  1338. static void isp_pm_complete(struct device *dev)
  1339. {
  1340. struct isp_device *isp = dev_get_drvdata(dev);
  1341. if (isp->ref_count == 0)
  1342. return;
  1343. isp_restore_ctx(isp);
  1344. isp_enable_interrupts(isp);
  1345. isp_resume_modules(isp);
  1346. }
  1347. #else
  1348. #define isp_pm_prepare NULL
  1349. #define isp_pm_suspend NULL
  1350. #define isp_pm_resume NULL
  1351. #define isp_pm_complete NULL
  1352. #endif /* CONFIG_PM */
  1353. static void isp_unregister_entities(struct isp_device *isp)
  1354. {
  1355. omap3isp_csi2_unregister_entities(&isp->isp_csi2a);
  1356. omap3isp_ccp2_unregister_entities(&isp->isp_ccp2);
  1357. omap3isp_ccdc_unregister_entities(&isp->isp_ccdc);
  1358. omap3isp_preview_unregister_entities(&isp->isp_prev);
  1359. omap3isp_resizer_unregister_entities(&isp->isp_res);
  1360. omap3isp_stat_unregister_entities(&isp->isp_aewb);
  1361. omap3isp_stat_unregister_entities(&isp->isp_af);
  1362. omap3isp_stat_unregister_entities(&isp->isp_hist);
  1363. v4l2_device_unregister(&isp->v4l2_dev);
  1364. media_device_unregister(&isp->media_dev);
  1365. media_device_cleanup(&isp->media_dev);
  1366. }
  1367. static int isp_link_entity(
  1368. struct isp_device *isp, struct media_entity *entity,
  1369. enum isp_interface_type interface)
  1370. {
  1371. struct media_entity *input;
  1372. unsigned int flags;
  1373. unsigned int pad;
  1374. unsigned int i;
  1375. /* Connect the sensor to the correct interface module.
  1376. * Parallel sensors are connected directly to the CCDC, while
  1377. * serial sensors are connected to the CSI2a, CCP2b or CSI2c
  1378. * receiver through CSIPHY1 or CSIPHY2.
  1379. */
  1380. switch (interface) {
  1381. case ISP_INTERFACE_PARALLEL:
  1382. input = &isp->isp_ccdc.subdev.entity;
  1383. pad = CCDC_PAD_SINK;
  1384. flags = 0;
  1385. break;
  1386. case ISP_INTERFACE_CSI2A_PHY2:
  1387. input = &isp->isp_csi2a.subdev.entity;
  1388. pad = CSI2_PAD_SINK;
  1389. flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
  1390. break;
  1391. case ISP_INTERFACE_CCP2B_PHY1:
  1392. case ISP_INTERFACE_CCP2B_PHY2:
  1393. input = &isp->isp_ccp2.subdev.entity;
  1394. pad = CCP2_PAD_SINK;
  1395. flags = 0;
  1396. break;
  1397. case ISP_INTERFACE_CSI2C_PHY1:
  1398. input = &isp->isp_csi2c.subdev.entity;
  1399. pad = CSI2_PAD_SINK;
  1400. flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
  1401. break;
  1402. default:
  1403. dev_err(isp->dev, "%s: invalid interface type %u\n", __func__,
  1404. interface);
  1405. return -EINVAL;
  1406. }
  1407. /*
  1408. * Not all interfaces are available on all revisions of the
  1409. * ISP. The sub-devices of those interfaces aren't initialised
  1410. * in such a case. Check this by ensuring the num_pads is
  1411. * non-zero.
  1412. */
  1413. if (!input->num_pads) {
  1414. dev_err(isp->dev, "%s: invalid input %u\n", entity->name,
  1415. interface);
  1416. return -EINVAL;
  1417. }
  1418. for (i = 0; i < entity->num_pads; i++) {
  1419. if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  1420. break;
  1421. }
  1422. if (i == entity->num_pads) {
  1423. dev_err(isp->dev, "%s: no source pad in external entity %s\n",
  1424. __func__, entity->name);
  1425. return -EINVAL;
  1426. }
  1427. return media_create_pad_link(entity, i, input, pad, flags);
  1428. }
  1429. static int isp_register_entities(struct isp_device *isp)
  1430. {
  1431. int ret;
  1432. isp->media_dev.dev = isp->dev;
  1433. strlcpy(isp->media_dev.model, "TI OMAP3 ISP",
  1434. sizeof(isp->media_dev.model));
  1435. isp->media_dev.hw_revision = isp->revision;
  1436. isp->media_dev.ops = &isp_media_ops;
  1437. media_device_init(&isp->media_dev);
  1438. isp->v4l2_dev.mdev = &isp->media_dev;
  1439. ret = v4l2_device_register(isp->dev, &isp->v4l2_dev);
  1440. if (ret < 0) {
  1441. dev_err(isp->dev, "%s: V4L2 device registration failed (%d)\n",
  1442. __func__, ret);
  1443. goto done;
  1444. }
  1445. /* Register internal entities */
  1446. ret = omap3isp_ccp2_register_entities(&isp->isp_ccp2, &isp->v4l2_dev);
  1447. if (ret < 0)
  1448. goto done;
  1449. ret = omap3isp_csi2_register_entities(&isp->isp_csi2a, &isp->v4l2_dev);
  1450. if (ret < 0)
  1451. goto done;
  1452. ret = omap3isp_ccdc_register_entities(&isp->isp_ccdc, &isp->v4l2_dev);
  1453. if (ret < 0)
  1454. goto done;
  1455. ret = omap3isp_preview_register_entities(&isp->isp_prev,
  1456. &isp->v4l2_dev);
  1457. if (ret < 0)
  1458. goto done;
  1459. ret = omap3isp_resizer_register_entities(&isp->isp_res, &isp->v4l2_dev);
  1460. if (ret < 0)
  1461. goto done;
  1462. ret = omap3isp_stat_register_entities(&isp->isp_aewb, &isp->v4l2_dev);
  1463. if (ret < 0)
  1464. goto done;
  1465. ret = omap3isp_stat_register_entities(&isp->isp_af, &isp->v4l2_dev);
  1466. if (ret < 0)
  1467. goto done;
  1468. ret = omap3isp_stat_register_entities(&isp->isp_hist, &isp->v4l2_dev);
  1469. if (ret < 0)
  1470. goto done;
  1471. done:
  1472. if (ret < 0)
  1473. isp_unregister_entities(isp);
  1474. return ret;
  1475. }
  1476. /*
  1477. * isp_create_links() - Create links for internal and external ISP entities
  1478. * @isp : Pointer to ISP device
  1479. *
  1480. * This function creates all links between ISP internal and external entities.
  1481. *
  1482. * Return: A negative error code on failure or zero on success. Possible error
  1483. * codes are those returned by media_create_pad_link().
  1484. */
  1485. static int isp_create_links(struct isp_device *isp)
  1486. {
  1487. int ret;
  1488. /* Create links between entities and video nodes. */
  1489. ret = media_create_pad_link(
  1490. &isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
  1491. &isp->isp_csi2a.video_out.video.entity, 0, 0);
  1492. if (ret < 0)
  1493. return ret;
  1494. ret = media_create_pad_link(
  1495. &isp->isp_ccp2.video_in.video.entity, 0,
  1496. &isp->isp_ccp2.subdev.entity, CCP2_PAD_SINK, 0);
  1497. if (ret < 0)
  1498. return ret;
  1499. ret = media_create_pad_link(
  1500. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
  1501. &isp->isp_ccdc.video_out.video.entity, 0, 0);
  1502. if (ret < 0)
  1503. return ret;
  1504. ret = media_create_pad_link(
  1505. &isp->isp_prev.video_in.video.entity, 0,
  1506. &isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
  1507. if (ret < 0)
  1508. return ret;
  1509. ret = media_create_pad_link(
  1510. &isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
  1511. &isp->isp_prev.video_out.video.entity, 0, 0);
  1512. if (ret < 0)
  1513. return ret;
  1514. ret = media_create_pad_link(
  1515. &isp->isp_res.video_in.video.entity, 0,
  1516. &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
  1517. if (ret < 0)
  1518. return ret;
  1519. ret = media_create_pad_link(
  1520. &isp->isp_res.subdev.entity, RESZ_PAD_SOURCE,
  1521. &isp->isp_res.video_out.video.entity, 0, 0);
  1522. if (ret < 0)
  1523. return ret;
  1524. /* Create links between entities. */
  1525. ret = media_create_pad_link(
  1526. &isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
  1527. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
  1528. if (ret < 0)
  1529. return ret;
  1530. ret = media_create_pad_link(
  1531. &isp->isp_ccp2.subdev.entity, CCP2_PAD_SOURCE,
  1532. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
  1533. if (ret < 0)
  1534. return ret;
  1535. ret = media_create_pad_link(
  1536. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
  1537. &isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
  1538. if (ret < 0)
  1539. return ret;
  1540. ret = media_create_pad_link(
  1541. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
  1542. &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
  1543. if (ret < 0)
  1544. return ret;
  1545. ret = media_create_pad_link(
  1546. &isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
  1547. &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
  1548. if (ret < 0)
  1549. return ret;
  1550. ret = media_create_pad_link(
  1551. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
  1552. &isp->isp_aewb.subdev.entity, 0,
  1553. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
  1554. if (ret < 0)
  1555. return ret;
  1556. ret = media_create_pad_link(
  1557. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
  1558. &isp->isp_af.subdev.entity, 0,
  1559. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
  1560. if (ret < 0)
  1561. return ret;
  1562. ret = media_create_pad_link(
  1563. &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
  1564. &isp->isp_hist.subdev.entity, 0,
  1565. MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
  1566. if (ret < 0)
  1567. return ret;
  1568. return 0;
  1569. }
  1570. static void isp_cleanup_modules(struct isp_device *isp)
  1571. {
  1572. omap3isp_h3a_aewb_cleanup(isp);
  1573. omap3isp_h3a_af_cleanup(isp);
  1574. omap3isp_hist_cleanup(isp);
  1575. omap3isp_resizer_cleanup(isp);
  1576. omap3isp_preview_cleanup(isp);
  1577. omap3isp_ccdc_cleanup(isp);
  1578. omap3isp_ccp2_cleanup(isp);
  1579. omap3isp_csi2_cleanup(isp);
  1580. omap3isp_csiphy_cleanup(isp);
  1581. }
  1582. static int isp_initialize_modules(struct isp_device *isp)
  1583. {
  1584. int ret;
  1585. ret = omap3isp_csiphy_init(isp);
  1586. if (ret < 0) {
  1587. dev_err(isp->dev, "CSI PHY initialization failed\n");
  1588. return ret;
  1589. }
  1590. ret = omap3isp_csi2_init(isp);
  1591. if (ret < 0) {
  1592. dev_err(isp->dev, "CSI2 initialization failed\n");
  1593. goto error_csi2;
  1594. }
  1595. ret = omap3isp_ccp2_init(isp);
  1596. if (ret < 0) {
  1597. if (ret != -EPROBE_DEFER)
  1598. dev_err(isp->dev, "CCP2 initialization failed\n");
  1599. goto error_ccp2;
  1600. }
  1601. ret = omap3isp_ccdc_init(isp);
  1602. if (ret < 0) {
  1603. dev_err(isp->dev, "CCDC initialization failed\n");
  1604. goto error_ccdc;
  1605. }
  1606. ret = omap3isp_preview_init(isp);
  1607. if (ret < 0) {
  1608. dev_err(isp->dev, "Preview initialization failed\n");
  1609. goto error_preview;
  1610. }
  1611. ret = omap3isp_resizer_init(isp);
  1612. if (ret < 0) {
  1613. dev_err(isp->dev, "Resizer initialization failed\n");
  1614. goto error_resizer;
  1615. }
  1616. ret = omap3isp_hist_init(isp);
  1617. if (ret < 0) {
  1618. dev_err(isp->dev, "Histogram initialization failed\n");
  1619. goto error_hist;
  1620. }
  1621. ret = omap3isp_h3a_aewb_init(isp);
  1622. if (ret < 0) {
  1623. dev_err(isp->dev, "H3A AEWB initialization failed\n");
  1624. goto error_h3a_aewb;
  1625. }
  1626. ret = omap3isp_h3a_af_init(isp);
  1627. if (ret < 0) {
  1628. dev_err(isp->dev, "H3A AF initialization failed\n");
  1629. goto error_h3a_af;
  1630. }
  1631. return 0;
  1632. error_h3a_af:
  1633. omap3isp_h3a_aewb_cleanup(isp);
  1634. error_h3a_aewb:
  1635. omap3isp_hist_cleanup(isp);
  1636. error_hist:
  1637. omap3isp_resizer_cleanup(isp);
  1638. error_resizer:
  1639. omap3isp_preview_cleanup(isp);
  1640. error_preview:
  1641. omap3isp_ccdc_cleanup(isp);
  1642. error_ccdc:
  1643. omap3isp_ccp2_cleanup(isp);
  1644. error_ccp2:
  1645. omap3isp_csi2_cleanup(isp);
  1646. error_csi2:
  1647. omap3isp_csiphy_cleanup(isp);
  1648. return ret;
  1649. }
  1650. static void isp_detach_iommu(struct isp_device *isp)
  1651. {
  1652. arm_iommu_release_mapping(isp->mapping);
  1653. isp->mapping = NULL;
  1654. }
  1655. static int isp_attach_iommu(struct isp_device *isp)
  1656. {
  1657. struct dma_iommu_mapping *mapping;
  1658. int ret;
  1659. /*
  1660. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  1661. * VAs. This will allocate a corresponding IOMMU domain.
  1662. */
  1663. mapping = arm_iommu_create_mapping(&platform_bus_type, SZ_1G, SZ_2G);
  1664. if (IS_ERR(mapping)) {
  1665. dev_err(isp->dev, "failed to create ARM IOMMU mapping\n");
  1666. ret = PTR_ERR(mapping);
  1667. goto error;
  1668. }
  1669. isp->mapping = mapping;
  1670. /* Attach the ARM VA mapping to the device. */
  1671. ret = arm_iommu_attach_device(isp->dev, mapping);
  1672. if (ret < 0) {
  1673. dev_err(isp->dev, "failed to attach device to VA mapping\n");
  1674. goto error;
  1675. }
  1676. return 0;
  1677. error:
  1678. isp_detach_iommu(isp);
  1679. return ret;
  1680. }
  1681. /*
  1682. * isp_remove - Remove ISP platform device
  1683. * @pdev: Pointer to ISP platform device
  1684. *
  1685. * Always returns 0.
  1686. */
  1687. static int isp_remove(struct platform_device *pdev)
  1688. {
  1689. struct isp_device *isp = platform_get_drvdata(pdev);
  1690. v4l2_async_notifier_unregister(&isp->notifier);
  1691. isp_unregister_entities(isp);
  1692. isp_cleanup_modules(isp);
  1693. isp_xclk_cleanup(isp);
  1694. __omap3isp_get(isp, false);
  1695. isp_detach_iommu(isp);
  1696. __omap3isp_put(isp, false);
  1697. media_entity_enum_cleanup(&isp->crashed);
  1698. v4l2_async_notifier_cleanup(&isp->notifier);
  1699. return 0;
  1700. }
  1701. enum isp_of_phy {
  1702. ISP_OF_PHY_PARALLEL = 0,
  1703. ISP_OF_PHY_CSIPHY1,
  1704. ISP_OF_PHY_CSIPHY2,
  1705. };
  1706. static int isp_fwnode_parse(struct device *dev,
  1707. struct v4l2_fwnode_endpoint *vep,
  1708. struct v4l2_async_subdev *asd)
  1709. {
  1710. struct isp_async_subdev *isd =
  1711. container_of(asd, struct isp_async_subdev, asd);
  1712. struct isp_bus_cfg *buscfg = &isd->bus;
  1713. bool csi1 = false;
  1714. unsigned int i;
  1715. dev_dbg(dev, "parsing endpoint %pOF, interface %u\n",
  1716. to_of_node(vep->base.local_fwnode), vep->base.port);
  1717. switch (vep->base.port) {
  1718. case ISP_OF_PHY_PARALLEL:
  1719. buscfg->interface = ISP_INTERFACE_PARALLEL;
  1720. buscfg->bus.parallel.data_lane_shift =
  1721. vep->bus.parallel.data_shift;
  1722. buscfg->bus.parallel.clk_pol =
  1723. !!(vep->bus.parallel.flags
  1724. & V4L2_MBUS_PCLK_SAMPLE_FALLING);
  1725. buscfg->bus.parallel.hs_pol =
  1726. !!(vep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW);
  1727. buscfg->bus.parallel.vs_pol =
  1728. !!(vep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW);
  1729. buscfg->bus.parallel.fld_pol =
  1730. !!(vep->bus.parallel.flags & V4L2_MBUS_FIELD_EVEN_LOW);
  1731. buscfg->bus.parallel.data_pol =
  1732. !!(vep->bus.parallel.flags & V4L2_MBUS_DATA_ACTIVE_LOW);
  1733. buscfg->bus.parallel.bt656 = vep->bus_type == V4L2_MBUS_BT656;
  1734. break;
  1735. case ISP_OF_PHY_CSIPHY1:
  1736. case ISP_OF_PHY_CSIPHY2:
  1737. switch (vep->bus_type) {
  1738. case V4L2_MBUS_CCP2:
  1739. case V4L2_MBUS_CSI1:
  1740. dev_dbg(dev, "CSI-1/CCP-2 configuration\n");
  1741. csi1 = true;
  1742. break;
  1743. case V4L2_MBUS_CSI2:
  1744. dev_dbg(dev, "CSI-2 configuration\n");
  1745. csi1 = false;
  1746. break;
  1747. default:
  1748. dev_err(dev, "unsupported bus type %u\n",
  1749. vep->bus_type);
  1750. return -EINVAL;
  1751. }
  1752. switch (vep->base.port) {
  1753. case ISP_OF_PHY_CSIPHY1:
  1754. if (csi1)
  1755. buscfg->interface = ISP_INTERFACE_CCP2B_PHY1;
  1756. else
  1757. buscfg->interface = ISP_INTERFACE_CSI2C_PHY1;
  1758. break;
  1759. case ISP_OF_PHY_CSIPHY2:
  1760. if (csi1)
  1761. buscfg->interface = ISP_INTERFACE_CCP2B_PHY2;
  1762. else
  1763. buscfg->interface = ISP_INTERFACE_CSI2A_PHY2;
  1764. break;
  1765. }
  1766. if (csi1) {
  1767. buscfg->bus.ccp2.lanecfg.clk.pos =
  1768. vep->bus.mipi_csi1.clock_lane;
  1769. buscfg->bus.ccp2.lanecfg.clk.pol =
  1770. vep->bus.mipi_csi1.lane_polarity[0];
  1771. dev_dbg(dev, "clock lane polarity %u, pos %u\n",
  1772. buscfg->bus.ccp2.lanecfg.clk.pol,
  1773. buscfg->bus.ccp2.lanecfg.clk.pos);
  1774. buscfg->bus.ccp2.lanecfg.data[0].pos =
  1775. vep->bus.mipi_csi1.data_lane;
  1776. buscfg->bus.ccp2.lanecfg.data[0].pol =
  1777. vep->bus.mipi_csi1.lane_polarity[1];
  1778. dev_dbg(dev, "data lane polarity %u, pos %u\n",
  1779. buscfg->bus.ccp2.lanecfg.data[0].pol,
  1780. buscfg->bus.ccp2.lanecfg.data[0].pos);
  1781. buscfg->bus.ccp2.strobe_clk_pol =
  1782. vep->bus.mipi_csi1.clock_inv;
  1783. buscfg->bus.ccp2.phy_layer = vep->bus.mipi_csi1.strobe;
  1784. buscfg->bus.ccp2.ccp2_mode =
  1785. vep->bus_type == V4L2_MBUS_CCP2;
  1786. buscfg->bus.ccp2.vp_clk_pol = 1;
  1787. buscfg->bus.ccp2.crc = 1;
  1788. } else {
  1789. buscfg->bus.csi2.lanecfg.clk.pos =
  1790. vep->bus.mipi_csi2.clock_lane;
  1791. buscfg->bus.csi2.lanecfg.clk.pol =
  1792. vep->bus.mipi_csi2.lane_polarities[0];
  1793. dev_dbg(dev, "clock lane polarity %u, pos %u\n",
  1794. buscfg->bus.csi2.lanecfg.clk.pol,
  1795. buscfg->bus.csi2.lanecfg.clk.pos);
  1796. buscfg->bus.csi2.num_data_lanes =
  1797. vep->bus.mipi_csi2.num_data_lanes;
  1798. for (i = 0; i < buscfg->bus.csi2.num_data_lanes; i++) {
  1799. buscfg->bus.csi2.lanecfg.data[i].pos =
  1800. vep->bus.mipi_csi2.data_lanes[i];
  1801. buscfg->bus.csi2.lanecfg.data[i].pol =
  1802. vep->bus.mipi_csi2.lane_polarities[i + 1];
  1803. dev_dbg(dev,
  1804. "data lane %u polarity %u, pos %u\n", i,
  1805. buscfg->bus.csi2.lanecfg.data[i].pol,
  1806. buscfg->bus.csi2.lanecfg.data[i].pos);
  1807. }
  1808. /*
  1809. * FIXME: now we assume the CRC is always there.
  1810. * Implement a way to obtain this information from the
  1811. * sensor. Frame descriptors, perhaps?
  1812. */
  1813. buscfg->bus.csi2.crc = 1;
  1814. }
  1815. break;
  1816. default:
  1817. dev_warn(dev, "%pOF: invalid interface %u\n",
  1818. to_of_node(vep->base.local_fwnode), vep->base.port);
  1819. return -EINVAL;
  1820. }
  1821. return 0;
  1822. }
  1823. static int isp_subdev_notifier_complete(struct v4l2_async_notifier *async)
  1824. {
  1825. struct isp_device *isp = container_of(async, struct isp_device,
  1826. notifier);
  1827. struct v4l2_device *v4l2_dev = &isp->v4l2_dev;
  1828. struct v4l2_subdev *sd;
  1829. int ret;
  1830. ret = media_entity_enum_init(&isp->crashed, &isp->media_dev);
  1831. if (ret)
  1832. return ret;
  1833. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  1834. if (sd->notifier != &isp->notifier)
  1835. continue;
  1836. ret = isp_link_entity(isp, &sd->entity,
  1837. v4l2_subdev_to_bus_cfg(sd)->interface);
  1838. if (ret < 0)
  1839. return ret;
  1840. }
  1841. ret = v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
  1842. if (ret < 0)
  1843. return ret;
  1844. return media_device_register(&isp->media_dev);
  1845. }
  1846. static const struct v4l2_async_notifier_operations isp_subdev_notifier_ops = {
  1847. .complete = isp_subdev_notifier_complete,
  1848. };
  1849. /*
  1850. * isp_probe - Probe ISP platform device
  1851. * @pdev: Pointer to ISP platform device
  1852. *
  1853. * Returns 0 if successful,
  1854. * -ENOMEM if no memory available,
  1855. * -ENODEV if no platform device resources found
  1856. * or no space for remapping registers,
  1857. * -EINVAL if couldn't install ISR,
  1858. * or clk_get return error value.
  1859. */
  1860. static int isp_probe(struct platform_device *pdev)
  1861. {
  1862. struct isp_device *isp;
  1863. struct resource *mem;
  1864. int ret;
  1865. int i, m;
  1866. isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
  1867. if (!isp) {
  1868. dev_err(&pdev->dev, "could not allocate memory\n");
  1869. return -ENOMEM;
  1870. }
  1871. ret = fwnode_property_read_u32(of_fwnode_handle(pdev->dev.of_node),
  1872. "ti,phy-type", &isp->phy_type);
  1873. if (ret)
  1874. return ret;
  1875. isp->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1876. "syscon");
  1877. if (IS_ERR(isp->syscon))
  1878. return PTR_ERR(isp->syscon);
  1879. ret = of_property_read_u32_index(pdev->dev.of_node,
  1880. "syscon", 1, &isp->syscon_offset);
  1881. if (ret)
  1882. return ret;
  1883. isp->autoidle = autoidle;
  1884. mutex_init(&isp->isp_mutex);
  1885. spin_lock_init(&isp->stat_lock);
  1886. ret = v4l2_async_notifier_parse_fwnode_endpoints(
  1887. &pdev->dev, &isp->notifier, sizeof(struct isp_async_subdev),
  1888. isp_fwnode_parse);
  1889. if (ret < 0)
  1890. goto error;
  1891. isp->dev = &pdev->dev;
  1892. isp->ref_count = 0;
  1893. ret = dma_coerce_mask_and_coherent(isp->dev, DMA_BIT_MASK(32));
  1894. if (ret)
  1895. goto error;
  1896. platform_set_drvdata(pdev, isp);
  1897. /* Regulators */
  1898. isp->isp_csiphy1.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy1");
  1899. isp->isp_csiphy2.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy2");
  1900. /* Clocks
  1901. *
  1902. * The ISP clock tree is revision-dependent. We thus need to enable ICLK
  1903. * manually to read the revision before calling __omap3isp_get().
  1904. *
  1905. * Start by mapping the ISP MMIO area, which is in two pieces.
  1906. * The ISP IOMMU is in between. Map both now, and fill in the
  1907. * ISP revision specific portions a little later in the
  1908. * function.
  1909. */
  1910. for (i = 0; i < 2; i++) {
  1911. unsigned int map_idx = i ? OMAP3_ISP_IOMEM_CSI2A_REGS1 : 0;
  1912. mem = platform_get_resource(pdev, IORESOURCE_MEM, i);
  1913. isp->mmio_base[map_idx] =
  1914. devm_ioremap_resource(isp->dev, mem);
  1915. if (IS_ERR(isp->mmio_base[map_idx]))
  1916. return PTR_ERR(isp->mmio_base[map_idx]);
  1917. }
  1918. ret = isp_get_clocks(isp);
  1919. if (ret < 0)
  1920. goto error;
  1921. ret = clk_enable(isp->clock[ISP_CLK_CAM_ICK]);
  1922. if (ret < 0)
  1923. goto error;
  1924. isp->revision = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
  1925. dev_info(isp->dev, "Revision %d.%d found\n",
  1926. (isp->revision & 0xf0) >> 4, isp->revision & 0x0f);
  1927. clk_disable(isp->clock[ISP_CLK_CAM_ICK]);
  1928. if (__omap3isp_get(isp, false) == NULL) {
  1929. ret = -ENODEV;
  1930. goto error;
  1931. }
  1932. ret = isp_reset(isp);
  1933. if (ret < 0)
  1934. goto error_isp;
  1935. ret = isp_xclk_init(isp);
  1936. if (ret < 0)
  1937. goto error_isp;
  1938. /* Memory resources */
  1939. for (m = 0; m < ARRAY_SIZE(isp_res_maps); m++)
  1940. if (isp->revision == isp_res_maps[m].isp_rev)
  1941. break;
  1942. if (m == ARRAY_SIZE(isp_res_maps)) {
  1943. dev_err(isp->dev, "No resource map found for ISP rev %d.%d\n",
  1944. (isp->revision & 0xf0) >> 4, isp->revision & 0xf);
  1945. ret = -ENODEV;
  1946. goto error_isp;
  1947. }
  1948. for (i = 1; i < OMAP3_ISP_IOMEM_CSI2A_REGS1; i++)
  1949. isp->mmio_base[i] =
  1950. isp->mmio_base[0] + isp_res_maps[m].offset[i];
  1951. for (i = OMAP3_ISP_IOMEM_CSIPHY2; i < OMAP3_ISP_IOMEM_LAST; i++)
  1952. isp->mmio_base[i] =
  1953. isp->mmio_base[OMAP3_ISP_IOMEM_CSI2A_REGS1]
  1954. + isp_res_maps[m].offset[i];
  1955. isp->mmio_hist_base_phys =
  1956. mem->start + isp_res_maps[m].offset[OMAP3_ISP_IOMEM_HIST];
  1957. /* IOMMU */
  1958. ret = isp_attach_iommu(isp);
  1959. if (ret < 0) {
  1960. dev_err(&pdev->dev, "unable to attach to IOMMU\n");
  1961. goto error_isp;
  1962. }
  1963. /* Interrupt */
  1964. ret = platform_get_irq(pdev, 0);
  1965. if (ret <= 0) {
  1966. dev_err(isp->dev, "No IRQ resource\n");
  1967. ret = -ENODEV;
  1968. goto error_iommu;
  1969. }
  1970. isp->irq_num = ret;
  1971. if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED,
  1972. "OMAP3 ISP", isp)) {
  1973. dev_err(isp->dev, "Unable to request IRQ\n");
  1974. ret = -EINVAL;
  1975. goto error_iommu;
  1976. }
  1977. /* Entities */
  1978. ret = isp_initialize_modules(isp);
  1979. if (ret < 0)
  1980. goto error_iommu;
  1981. ret = isp_register_entities(isp);
  1982. if (ret < 0)
  1983. goto error_modules;
  1984. ret = isp_create_links(isp);
  1985. if (ret < 0)
  1986. goto error_register_entities;
  1987. isp->notifier.ops = &isp_subdev_notifier_ops;
  1988. ret = v4l2_async_notifier_register(&isp->v4l2_dev, &isp->notifier);
  1989. if (ret)
  1990. goto error_register_entities;
  1991. isp_core_init(isp, 1);
  1992. omap3isp_put(isp);
  1993. return 0;
  1994. error_register_entities:
  1995. isp_unregister_entities(isp);
  1996. error_modules:
  1997. isp_cleanup_modules(isp);
  1998. error_iommu:
  1999. isp_detach_iommu(isp);
  2000. error_isp:
  2001. isp_xclk_cleanup(isp);
  2002. __omap3isp_put(isp, false);
  2003. error:
  2004. v4l2_async_notifier_cleanup(&isp->notifier);
  2005. mutex_destroy(&isp->isp_mutex);
  2006. return ret;
  2007. }
  2008. static const struct dev_pm_ops omap3isp_pm_ops = {
  2009. .prepare = isp_pm_prepare,
  2010. .suspend = isp_pm_suspend,
  2011. .resume = isp_pm_resume,
  2012. .complete = isp_pm_complete,
  2013. };
  2014. static struct platform_device_id omap3isp_id_table[] = {
  2015. { "omap3isp", 0 },
  2016. { },
  2017. };
  2018. MODULE_DEVICE_TABLE(platform, omap3isp_id_table);
  2019. static const struct of_device_id omap3isp_of_table[] = {
  2020. { .compatible = "ti,omap3-isp" },
  2021. { },
  2022. };
  2023. MODULE_DEVICE_TABLE(of, omap3isp_of_table);
  2024. static struct platform_driver omap3isp_driver = {
  2025. .probe = isp_probe,
  2026. .remove = isp_remove,
  2027. .id_table = omap3isp_id_table,
  2028. .driver = {
  2029. .name = "omap3isp",
  2030. .pm = &omap3isp_pm_ops,
  2031. .of_match_table = omap3isp_of_table,
  2032. },
  2033. };
  2034. module_platform_driver(omap3isp_driver);
  2035. MODULE_AUTHOR("Nokia Corporation");
  2036. MODULE_DESCRIPTION("TI OMAP3 ISP driver");
  2037. MODULE_LICENSE("GPL");
  2038. MODULE_VERSION(ISP_VIDEO_DRIVER_VERSION);