ispcsiphy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_lanes_config - Configuration of CSIPHY lanes.
  111. *
  112. * Updates HW configuration.
  113. * Called with phy->mutex taken.
  114. */
  115. static void csiphy_lanes_config(struct isp_csiphy *phy)
  116. {
  117. unsigned int i;
  118. u32 reg;
  119. reg = isp_reg_readl(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG);
  120. for (i = 0; i < phy->num_data_lanes; i++) {
  121. reg &= ~(ISPCSI2_PHY_CFG_DATA_POL_MASK(i + 1) |
  122. ISPCSI2_PHY_CFG_DATA_POSITION_MASK(i + 1));
  123. reg |= (phy->lanes.data[i].pol <<
  124. ISPCSI2_PHY_CFG_DATA_POL_SHIFT(i + 1));
  125. reg |= (phy->lanes.data[i].pos <<
  126. ISPCSI2_PHY_CFG_DATA_POSITION_SHIFT(i + 1));
  127. }
  128. reg &= ~(ISPCSI2_PHY_CFG_CLOCK_POL_MASK |
  129. ISPCSI2_PHY_CFG_CLOCK_POSITION_MASK);
  130. reg |= phy->lanes.clk.pol << ISPCSI2_PHY_CFG_CLOCK_POL_SHIFT;
  131. reg |= phy->lanes.clk.pos << ISPCSI2_PHY_CFG_CLOCK_POSITION_SHIFT;
  132. isp_reg_writel(phy->isp, reg, phy->cfg_regs, ISPCSI2_PHY_CFG);
  133. }
  134. /*
  135. * csiphy_power_autoswitch_enable
  136. * @enable: Sets or clears the autoswitch function enable flag.
  137. */
  138. static void csiphy_power_autoswitch_enable(struct isp_csiphy *phy, bool enable)
  139. {
  140. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  141. ISPCSI2_PHY_CFG_PWR_AUTO,
  142. enable ? ISPCSI2_PHY_CFG_PWR_AUTO : 0);
  143. }
  144. /*
  145. * csiphy_set_power
  146. * @power: Power state to be set.
  147. *
  148. * Returns 0 if successful, or -EBUSY if the retry count is exceeded.
  149. */
  150. static int csiphy_set_power(struct isp_csiphy *phy, u32 power)
  151. {
  152. u32 reg;
  153. u8 retry_count;
  154. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  155. ISPCSI2_PHY_CFG_PWR_CMD_MASK, power);
  156. retry_count = 0;
  157. do {
  158. udelay(50);
  159. reg = isp_reg_readl(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG) &
  160. ISPCSI2_PHY_CFG_PWR_STATUS_MASK;
  161. if (reg != power >> 2)
  162. retry_count++;
  163. } while ((reg != power >> 2) && (retry_count < 100));
  164. if (retry_count == 100) {
  165. printk(KERN_ERR "CSI2 CIO set power failed!\n");
  166. return -EBUSY;
  167. }
  168. return 0;
  169. }
  170. /*
  171. * csiphy_dphy_config - Configure CSI2 D-PHY parameters.
  172. *
  173. * Called with phy->mutex taken.
  174. */
  175. static void csiphy_dphy_config(struct isp_csiphy *phy)
  176. {
  177. u32 reg;
  178. /* Set up ISPCSIPHY_REG0 */
  179. reg = isp_reg_readl(phy->isp, phy->phy_regs, ISPCSIPHY_REG0);
  180. reg &= ~(ISPCSIPHY_REG0_THS_TERM_MASK |
  181. ISPCSIPHY_REG0_THS_SETTLE_MASK);
  182. reg |= phy->dphy.ths_term << ISPCSIPHY_REG0_THS_TERM_SHIFT;
  183. reg |= phy->dphy.ths_settle << ISPCSIPHY_REG0_THS_SETTLE_SHIFT;
  184. isp_reg_writel(phy->isp, reg, phy->phy_regs, ISPCSIPHY_REG0);
  185. /* Set up ISPCSIPHY_REG1 */
  186. reg = isp_reg_readl(phy->isp, phy->phy_regs, ISPCSIPHY_REG1);
  187. reg &= ~(ISPCSIPHY_REG1_TCLK_TERM_MASK |
  188. ISPCSIPHY_REG1_TCLK_MISS_MASK |
  189. ISPCSIPHY_REG1_TCLK_SETTLE_MASK);
  190. reg |= phy->dphy.tclk_term << ISPCSIPHY_REG1_TCLK_TERM_SHIFT;
  191. reg |= phy->dphy.tclk_miss << ISPCSIPHY_REG1_TCLK_MISS_SHIFT;
  192. reg |= phy->dphy.tclk_settle << ISPCSIPHY_REG1_TCLK_SETTLE_SHIFT;
  193. isp_reg_writel(phy->isp, reg, phy->phy_regs, ISPCSIPHY_REG1);
  194. }
  195. static int csiphy_config(struct isp_csiphy *phy,
  196. struct isp_csiphy_dphy_cfg *dphy,
  197. struct isp_csiphy_lanes_cfg *lanes)
  198. {
  199. unsigned int used_lanes = 0;
  200. unsigned int i;
  201. /* Clock and data lanes verification */
  202. for (i = 0; i < phy->num_data_lanes; i++) {
  203. if (lanes->data[i].pol > 1 || lanes->data[i].pos > 3)
  204. return -EINVAL;
  205. if (used_lanes & (1 << lanes->data[i].pos))
  206. return -EINVAL;
  207. used_lanes |= 1 << lanes->data[i].pos;
  208. }
  209. if (lanes->clk.pol > 1 || lanes->clk.pos > 3)
  210. return -EINVAL;
  211. if (lanes->clk.pos == 0 || used_lanes & (1 << lanes->clk.pos))
  212. return -EINVAL;
  213. mutex_lock(&phy->mutex);
  214. phy->dphy = *dphy;
  215. phy->lanes = *lanes;
  216. mutex_unlock(&phy->mutex);
  217. return 0;
  218. }
  219. int omap3isp_csiphy_acquire(struct isp_csiphy *phy)
  220. {
  221. int rval;
  222. if (phy->vdd == NULL) {
  223. dev_err(phy->isp->dev, "Power regulator for CSI PHY not "
  224. "available\n");
  225. return -ENODEV;
  226. }
  227. mutex_lock(&phy->mutex);
  228. rval = regulator_enable(phy->vdd);
  229. if (rval < 0)
  230. goto done;
  231. rval = omap3isp_csi2_reset(phy->csi2);
  232. if (rval < 0)
  233. goto done;
  234. csiphy_dphy_config(phy);
  235. csiphy_lanes_config(phy);
  236. rval = csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_ON);
  237. if (rval) {
  238. regulator_disable(phy->vdd);
  239. goto done;
  240. }
  241. csiphy_power_autoswitch_enable(phy, true);
  242. phy->phy_in_use = 1;
  243. done:
  244. mutex_unlock(&phy->mutex);
  245. return rval;
  246. }
  247. void omap3isp_csiphy_release(struct isp_csiphy *phy)
  248. {
  249. mutex_lock(&phy->mutex);
  250. if (phy->phy_in_use) {
  251. csiphy_power_autoswitch_enable(phy, false);
  252. csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_OFF);
  253. regulator_disable(phy->vdd);
  254. phy->phy_in_use = 0;
  255. }
  256. mutex_unlock(&phy->mutex);
  257. }
  258. /*
  259. * omap3isp_csiphy_init - Initialize the CSI PHY frontends
  260. */
  261. int omap3isp_csiphy_init(struct isp_device *isp)
  262. {
  263. struct isp_csiphy *phy1 = &isp->isp_csiphy1;
  264. struct isp_csiphy *phy2 = &isp->isp_csiphy2;
  265. isp->platform_cb.csiphy_config = csiphy_config;
  266. phy2->isp = isp;
  267. phy2->csi2 = &isp->isp_csi2a;
  268. phy2->num_data_lanes = ISP_CSIPHY2_NUM_DATA_LANES;
  269. phy2->cfg_regs = OMAP3_ISP_IOMEM_CSI2A_REGS1;
  270. phy2->phy_regs = OMAP3_ISP_IOMEM_CSIPHY2;
  271. mutex_init(&phy2->mutex);
  272. if (isp->revision == ISP_REVISION_15_0) {
  273. phy1->isp = isp;
  274. phy1->csi2 = &isp->isp_csi2c;
  275. phy1->num_data_lanes = ISP_CSIPHY1_NUM_DATA_LANES;
  276. phy1->cfg_regs = OMAP3_ISP_IOMEM_CSI2C_REGS1;
  277. phy1->phy_regs = OMAP3_ISP_IOMEM_CSIPHY1;
  278. mutex_init(&phy1->mutex);
  279. }
  280. return 0;
  281. }