ispcsiphy.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * ispcsiphy.c
  3. *
  4. * TI OMAP3 ISP - CSI PHY module
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2009 Texas Instruments, Inc.
  8. *
  9. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. * Sakari Ailus <sakari.ailus@iki.fi>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/regulator/consumer.h>
  19. #include "isp.h"
  20. #include "ispreg.h"
  21. #include "ispcsiphy.h"
  22. static void csiphy_routing_cfg_3630(struct isp_csiphy *phy,
  23. enum isp_interface_type iface,
  24. bool ccp2_strobe)
  25. {
  26. u32 reg = isp_reg_readl(
  27. phy->isp, OMAP3_ISP_IOMEM_3630_CONTROL_CAMERA_PHY_CTRL, 0);
  28. u32 shift, mode;
  29. switch (iface) {
  30. default:
  31. /* Should not happen in practice, but let's keep the compiler happy. */
  32. case ISP_INTERFACE_CCP2B_PHY1:
  33. reg &= ~OMAP3630_CONTROL_CAMERA_PHY_CTRL_CSI1_RX_SEL_PHY2;
  34. shift = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_PHY1_SHIFT;
  35. break;
  36. case ISP_INTERFACE_CSI2C_PHY1:
  37. shift = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_PHY1_SHIFT;
  38. mode = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_DPHY;
  39. break;
  40. case ISP_INTERFACE_CCP2B_PHY2:
  41. reg |= OMAP3630_CONTROL_CAMERA_PHY_CTRL_CSI1_RX_SEL_PHY2;
  42. shift = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_PHY2_SHIFT;
  43. break;
  44. case ISP_INTERFACE_CSI2A_PHY2:
  45. shift = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_PHY2_SHIFT;
  46. mode = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_DPHY;
  47. break;
  48. }
  49. /* Select data/clock or data/strobe mode for CCP2 */
  50. if (iface == ISP_INTERFACE_CCP2B_PHY1 ||
  51. iface == ISP_INTERFACE_CCP2B_PHY2) {
  52. if (ccp2_strobe)
  53. mode = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_CCP2_DATA_STROBE;
  54. else
  55. mode = OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_CCP2_DATA_CLOCK;
  56. }
  57. reg &= ~(OMAP3630_CONTROL_CAMERA_PHY_CTRL_CAMMODE_MASK << shift);
  58. reg |= mode << shift;
  59. isp_reg_writel(phy->isp, reg,
  60. OMAP3_ISP_IOMEM_3630_CONTROL_CAMERA_PHY_CTRL, 0);
  61. }
  62. static void csiphy_routing_cfg_3430(struct isp_csiphy *phy, u32 iface, bool on,
  63. bool ccp2_strobe)
  64. {
  65. u32 csirxfe = OMAP343X_CONTROL_CSIRXFE_PWRDNZ
  66. | OMAP343X_CONTROL_CSIRXFE_RESET;
  67. /* Only the CCP2B on PHY1 is configurable. */
  68. if (iface != ISP_INTERFACE_CCP2B_PHY1)
  69. return;
  70. if (!on) {
  71. isp_reg_writel(phy->isp, 0,
  72. OMAP3_ISP_IOMEM_343X_CONTROL_CSIRXFE, 0);
  73. return;
  74. }
  75. if (ccp2_strobe)
  76. csirxfe |= OMAP343X_CONTROL_CSIRXFE_SELFORM;
  77. isp_reg_writel(phy->isp, csirxfe,
  78. OMAP3_ISP_IOMEM_343X_CONTROL_CSIRXFE, 0);
  79. }
  80. /*
  81. * Configure OMAP 3 CSI PHY routing.
  82. * @phy: relevant phy device
  83. * @iface: ISP_INTERFACE_*
  84. * @on: power on or off
  85. * @ccp2_strobe: false: data/clock, true: data/strobe
  86. *
  87. * Note that the underlying routing configuration registers are part of the
  88. * control (SCM) register space and part of the CORE power domain on both 3430
  89. * and 3630, so they will not hold their contents in off-mode. This isn't an
  90. * issue since the MPU power domain is forced on whilst the ISP is in use.
  91. */
  92. static void csiphy_routing_cfg(struct isp_csiphy *phy,
  93. enum isp_interface_type iface, bool on,
  94. bool ccp2_strobe)
  95. {
  96. if (phy->isp->mmio_base[OMAP3_ISP_IOMEM_3630_CONTROL_CAMERA_PHY_CTRL]
  97. && on)
  98. return csiphy_routing_cfg_3630(phy, iface, ccp2_strobe);
  99. if (phy->isp->mmio_base[OMAP3_ISP_IOMEM_343X_CONTROL_CSIRXFE])
  100. return csiphy_routing_cfg_3430(phy, iface, on, ccp2_strobe);
  101. }
  102. /*
  103. * csiphy_power_autoswitch_enable
  104. * @enable: Sets or clears the autoswitch function enable flag.
  105. */
  106. static void csiphy_power_autoswitch_enable(struct isp_csiphy *phy, bool enable)
  107. {
  108. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  109. ISPCSI2_PHY_CFG_PWR_AUTO,
  110. enable ? ISPCSI2_PHY_CFG_PWR_AUTO : 0);
  111. }
  112. /*
  113. * csiphy_set_power
  114. * @power: Power state to be set.
  115. *
  116. * Returns 0 if successful, or -EBUSY if the retry count is exceeded.
  117. */
  118. static int csiphy_set_power(struct isp_csiphy *phy, u32 power)
  119. {
  120. u32 reg;
  121. u8 retry_count;
  122. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  123. ISPCSI2_PHY_CFG_PWR_CMD_MASK, power);
  124. retry_count = 0;
  125. do {
  126. udelay(50);
  127. reg = isp_reg_readl(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG) &
  128. ISPCSI2_PHY_CFG_PWR_STATUS_MASK;
  129. if (reg != power >> 2)
  130. retry_count++;
  131. } while ((reg != power >> 2) && (retry_count < 100));
  132. if (retry_count == 100) {
  133. dev_err(phy->isp->dev, "CSI2 CIO set power failed!\n");
  134. return -EBUSY;
  135. }
  136. return 0;
  137. }
  138. /*
  139. * TCLK values are OK at their reset values
  140. */
  141. #define TCLK_TERM 0
  142. #define TCLK_MISS 1
  143. #define TCLK_SETTLE 14
  144. static int omap3isp_csiphy_config(struct isp_csiphy *phy)
  145. {
  146. struct isp_csi2_device *csi2 = phy->csi2;
  147. struct isp_pipeline *pipe = to_isp_pipeline(&csi2->subdev.entity);
  148. struct isp_v4l2_subdevs_group *subdevs = pipe->external->host_priv;
  149. struct isp_csiphy_lanes_cfg *lanes;
  150. int csi2_ddrclk_khz;
  151. unsigned int used_lanes = 0;
  152. unsigned int i;
  153. u32 reg;
  154. if (subdevs->interface == ISP_INTERFACE_CCP2B_PHY1
  155. || subdevs->interface == ISP_INTERFACE_CCP2B_PHY2)
  156. lanes = &subdevs->bus.ccp2.lanecfg;
  157. else
  158. lanes = &subdevs->bus.csi2.lanecfg;
  159. /* Clock and data lanes verification */
  160. for (i = 0; i < phy->num_data_lanes; i++) {
  161. if (lanes->data[i].pol > 1 || lanes->data[i].pos > 3)
  162. return -EINVAL;
  163. if (used_lanes & (1 << lanes->data[i].pos))
  164. return -EINVAL;
  165. used_lanes |= 1 << lanes->data[i].pos;
  166. }
  167. if (lanes->clk.pol > 1 || lanes->clk.pos > 3)
  168. return -EINVAL;
  169. if (lanes->clk.pos == 0 || used_lanes & (1 << lanes->clk.pos))
  170. return -EINVAL;
  171. /*
  172. * The PHY configuration is lost in off mode, that's not an
  173. * issue since the MPU power domain is forced on whilst the
  174. * ISP is in use.
  175. */
  176. csiphy_routing_cfg(phy, subdevs->interface, true,
  177. subdevs->bus.ccp2.phy_layer);
  178. /* DPHY timing configuration */
  179. /* CSI-2 is DDR and we only count used lanes. */
  180. csi2_ddrclk_khz = pipe->external_rate / 1000
  181. / (2 * hweight32(used_lanes)) * pipe->external_width;
  182. reg = isp_reg_readl(csi2->isp, phy->phy_regs, ISPCSIPHY_REG0);
  183. reg &= ~(ISPCSIPHY_REG0_THS_TERM_MASK |
  184. ISPCSIPHY_REG0_THS_SETTLE_MASK);
  185. /* THS_TERM: Programmed value = ceil(12.5 ns/DDRClk period) - 1. */
  186. reg |= (DIV_ROUND_UP(25 * csi2_ddrclk_khz, 2000000) - 1)
  187. << ISPCSIPHY_REG0_THS_TERM_SHIFT;
  188. /* THS_SETTLE: Programmed value = ceil(90 ns/DDRClk period) + 3. */
  189. reg |= (DIV_ROUND_UP(90 * csi2_ddrclk_khz, 1000000) + 3)
  190. << ISPCSIPHY_REG0_THS_SETTLE_SHIFT;
  191. isp_reg_writel(csi2->isp, reg, phy->phy_regs, ISPCSIPHY_REG0);
  192. reg = isp_reg_readl(csi2->isp, phy->phy_regs, ISPCSIPHY_REG1);
  193. reg &= ~(ISPCSIPHY_REG1_TCLK_TERM_MASK |
  194. ISPCSIPHY_REG1_TCLK_MISS_MASK |
  195. ISPCSIPHY_REG1_TCLK_SETTLE_MASK);
  196. reg |= TCLK_TERM << ISPCSIPHY_REG1_TCLK_TERM_SHIFT;
  197. reg |= TCLK_MISS << ISPCSIPHY_REG1_TCLK_MISS_SHIFT;
  198. reg |= TCLK_SETTLE << ISPCSIPHY_REG1_TCLK_SETTLE_SHIFT;
  199. isp_reg_writel(csi2->isp, reg, phy->phy_regs, ISPCSIPHY_REG1);
  200. /* DPHY lane configuration */
  201. reg = isp_reg_readl(csi2->isp, phy->cfg_regs, ISPCSI2_PHY_CFG);
  202. for (i = 0; i < phy->num_data_lanes; i++) {
  203. reg &= ~(ISPCSI2_PHY_CFG_DATA_POL_MASK(i + 1) |
  204. ISPCSI2_PHY_CFG_DATA_POSITION_MASK(i + 1));
  205. reg |= (lanes->data[i].pol <<
  206. ISPCSI2_PHY_CFG_DATA_POL_SHIFT(i + 1));
  207. reg |= (lanes->data[i].pos <<
  208. ISPCSI2_PHY_CFG_DATA_POSITION_SHIFT(i + 1));
  209. }
  210. reg &= ~(ISPCSI2_PHY_CFG_CLOCK_POL_MASK |
  211. ISPCSI2_PHY_CFG_CLOCK_POSITION_MASK);
  212. reg |= lanes->clk.pol << ISPCSI2_PHY_CFG_CLOCK_POL_SHIFT;
  213. reg |= lanes->clk.pos << ISPCSI2_PHY_CFG_CLOCK_POSITION_SHIFT;
  214. isp_reg_writel(csi2->isp, reg, phy->cfg_regs, ISPCSI2_PHY_CFG);
  215. return 0;
  216. }
  217. int omap3isp_csiphy_acquire(struct isp_csiphy *phy)
  218. {
  219. int rval;
  220. if (phy->vdd == NULL) {
  221. dev_err(phy->isp->dev, "Power regulator for CSI PHY not "
  222. "available\n");
  223. return -ENODEV;
  224. }
  225. mutex_lock(&phy->mutex);
  226. rval = regulator_enable(phy->vdd);
  227. if (rval < 0)
  228. goto done;
  229. rval = omap3isp_csi2_reset(phy->csi2);
  230. if (rval < 0)
  231. goto done;
  232. rval = omap3isp_csiphy_config(phy);
  233. if (rval < 0)
  234. goto done;
  235. rval = csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_ON);
  236. if (rval) {
  237. regulator_disable(phy->vdd);
  238. goto done;
  239. }
  240. csiphy_power_autoswitch_enable(phy, true);
  241. phy->phy_in_use = 1;
  242. done:
  243. mutex_unlock(&phy->mutex);
  244. return rval;
  245. }
  246. void omap3isp_csiphy_release(struct isp_csiphy *phy)
  247. {
  248. mutex_lock(&phy->mutex);
  249. if (phy->phy_in_use) {
  250. struct isp_csi2_device *csi2 = phy->csi2;
  251. struct isp_pipeline *pipe =
  252. to_isp_pipeline(&csi2->subdev.entity);
  253. struct isp_v4l2_subdevs_group *subdevs =
  254. pipe->external->host_priv;
  255. csiphy_routing_cfg(phy, subdevs->interface, false,
  256. subdevs->bus.ccp2.phy_layer);
  257. csiphy_power_autoswitch_enable(phy, false);
  258. csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_OFF);
  259. regulator_disable(phy->vdd);
  260. phy->phy_in_use = 0;
  261. }
  262. mutex_unlock(&phy->mutex);
  263. }
  264. /*
  265. * omap3isp_csiphy_init - Initialize the CSI PHY frontends
  266. */
  267. int omap3isp_csiphy_init(struct isp_device *isp)
  268. {
  269. struct isp_csiphy *phy1 = &isp->isp_csiphy1;
  270. struct isp_csiphy *phy2 = &isp->isp_csiphy2;
  271. phy2->isp = isp;
  272. phy2->csi2 = &isp->isp_csi2a;
  273. phy2->num_data_lanes = ISP_CSIPHY2_NUM_DATA_LANES;
  274. phy2->cfg_regs = OMAP3_ISP_IOMEM_CSI2A_REGS1;
  275. phy2->phy_regs = OMAP3_ISP_IOMEM_CSIPHY2;
  276. mutex_init(&phy2->mutex);
  277. if (isp->revision == ISP_REVISION_15_0) {
  278. phy1->isp = isp;
  279. phy1->csi2 = &isp->isp_csi2c;
  280. phy1->num_data_lanes = ISP_CSIPHY1_NUM_DATA_LANES;
  281. phy1->cfg_regs = OMAP3_ISP_IOMEM_CSI2C_REGS1;
  282. phy1->phy_regs = OMAP3_ISP_IOMEM_CSIPHY1;
  283. mutex_init(&phy1->mutex);
  284. }
  285. return 0;
  286. }