ispcsiphy.c 9.7 KB

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