isp.c 62 KB

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