isp.c 63 KB

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