isp.c 58 KB

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