isp.c 58 KB

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