isp.c 59 KB

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