isp.c 58 KB

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