ispcsiphy.c 9.9 KB

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