isp.c 59 KB

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