isp.c 62 KB

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