isp.c 58 KB

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