ispcsiphy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /*
  33. * csiphy_lanes_config - Configuration of CSIPHY lanes.
  34. *
  35. * Updates HW configuration.
  36. * Called with phy->mutex taken.
  37. */
  38. static void csiphy_lanes_config(struct isp_csiphy *phy)
  39. {
  40. unsigned int i;
  41. u32 reg;
  42. reg = isp_reg_readl(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG);
  43. for (i = 0; i < phy->num_data_lanes; i++) {
  44. reg &= ~(ISPCSI2_PHY_CFG_DATA_POL_MASK(i + 1) |
  45. ISPCSI2_PHY_CFG_DATA_POSITION_MASK(i + 1));
  46. reg |= (phy->lanes.data[i].pol <<
  47. ISPCSI2_PHY_CFG_DATA_POL_SHIFT(i + 1));
  48. reg |= (phy->lanes.data[i].pos <<
  49. ISPCSI2_PHY_CFG_DATA_POSITION_SHIFT(i + 1));
  50. }
  51. reg &= ~(ISPCSI2_PHY_CFG_CLOCK_POL_MASK |
  52. ISPCSI2_PHY_CFG_CLOCK_POSITION_MASK);
  53. reg |= phy->lanes.clk.pol << ISPCSI2_PHY_CFG_CLOCK_POL_SHIFT;
  54. reg |= phy->lanes.clk.pos << ISPCSI2_PHY_CFG_CLOCK_POSITION_SHIFT;
  55. isp_reg_writel(phy->isp, reg, phy->cfg_regs, ISPCSI2_PHY_CFG);
  56. }
  57. /*
  58. * csiphy_power_autoswitch_enable
  59. * @enable: Sets or clears the autoswitch function enable flag.
  60. */
  61. static void csiphy_power_autoswitch_enable(struct isp_csiphy *phy, bool enable)
  62. {
  63. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  64. ISPCSI2_PHY_CFG_PWR_AUTO,
  65. enable ? ISPCSI2_PHY_CFG_PWR_AUTO : 0);
  66. }
  67. /*
  68. * csiphy_set_power
  69. * @power: Power state to be set.
  70. *
  71. * Returns 0 if successful, or -EBUSY if the retry count is exceeded.
  72. */
  73. static int csiphy_set_power(struct isp_csiphy *phy, u32 power)
  74. {
  75. u32 reg;
  76. u8 retry_count;
  77. isp_reg_clr_set(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG,
  78. ISPCSI2_PHY_CFG_PWR_CMD_MASK, power);
  79. retry_count = 0;
  80. do {
  81. udelay(50);
  82. reg = isp_reg_readl(phy->isp, phy->cfg_regs, ISPCSI2_PHY_CFG) &
  83. ISPCSI2_PHY_CFG_PWR_STATUS_MASK;
  84. if (reg != power >> 2)
  85. retry_count++;
  86. } while ((reg != power >> 2) && (retry_count < 100));
  87. if (retry_count == 100) {
  88. printk(KERN_ERR "CSI2 CIO set power failed!\n");
  89. return -EBUSY;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * csiphy_dphy_config - Configure CSI2 D-PHY parameters.
  95. *
  96. * Called with phy->mutex taken.
  97. */
  98. static void csiphy_dphy_config(struct isp_csiphy *phy)
  99. {
  100. u32 reg;
  101. /* Set up ISPCSIPHY_REG0 */
  102. reg = isp_reg_readl(phy->isp, phy->phy_regs, ISPCSIPHY_REG0);
  103. reg &= ~(ISPCSIPHY_REG0_THS_TERM_MASK |
  104. ISPCSIPHY_REG0_THS_SETTLE_MASK);
  105. reg |= phy->dphy.ths_term << ISPCSIPHY_REG0_THS_TERM_SHIFT;
  106. reg |= phy->dphy.ths_settle << ISPCSIPHY_REG0_THS_SETTLE_SHIFT;
  107. isp_reg_writel(phy->isp, reg, phy->phy_regs, ISPCSIPHY_REG0);
  108. /* Set up ISPCSIPHY_REG1 */
  109. reg = isp_reg_readl(phy->isp, phy->phy_regs, ISPCSIPHY_REG1);
  110. reg &= ~(ISPCSIPHY_REG1_TCLK_TERM_MASK |
  111. ISPCSIPHY_REG1_TCLK_MISS_MASK |
  112. ISPCSIPHY_REG1_TCLK_SETTLE_MASK);
  113. reg |= phy->dphy.tclk_term << ISPCSIPHY_REG1_TCLK_TERM_SHIFT;
  114. reg |= phy->dphy.tclk_miss << ISPCSIPHY_REG1_TCLK_MISS_SHIFT;
  115. reg |= phy->dphy.tclk_settle << ISPCSIPHY_REG1_TCLK_SETTLE_SHIFT;
  116. isp_reg_writel(phy->isp, reg, phy->phy_regs, ISPCSIPHY_REG1);
  117. }
  118. static int csiphy_config(struct isp_csiphy *phy,
  119. struct isp_csiphy_dphy_cfg *dphy,
  120. struct isp_csiphy_lanes_cfg *lanes)
  121. {
  122. unsigned int used_lanes = 0;
  123. unsigned int i;
  124. /* Clock and data lanes verification */
  125. for (i = 0; i < phy->num_data_lanes; i++) {
  126. if (lanes->data[i].pol > 1 || lanes->data[i].pos > 3)
  127. return -EINVAL;
  128. if (used_lanes & (1 << lanes->data[i].pos))
  129. return -EINVAL;
  130. used_lanes |= 1 << lanes->data[i].pos;
  131. }
  132. if (lanes->clk.pol > 1 || lanes->clk.pos > 3)
  133. return -EINVAL;
  134. if (lanes->clk.pos == 0 || used_lanes & (1 << lanes->clk.pos))
  135. return -EINVAL;
  136. mutex_lock(&phy->mutex);
  137. phy->dphy = *dphy;
  138. phy->lanes = *lanes;
  139. mutex_unlock(&phy->mutex);
  140. return 0;
  141. }
  142. int omap3isp_csiphy_acquire(struct isp_csiphy *phy)
  143. {
  144. int rval;
  145. if (phy->vdd == NULL) {
  146. dev_err(phy->isp->dev, "Power regulator for CSI PHY not "
  147. "available\n");
  148. return -ENODEV;
  149. }
  150. mutex_lock(&phy->mutex);
  151. rval = regulator_enable(phy->vdd);
  152. if (rval < 0)
  153. goto done;
  154. omap3isp_csi2_reset(phy->csi2);
  155. csiphy_dphy_config(phy);
  156. csiphy_lanes_config(phy);
  157. rval = csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_ON);
  158. if (rval) {
  159. regulator_disable(phy->vdd);
  160. goto done;
  161. }
  162. csiphy_power_autoswitch_enable(phy, true);
  163. phy->phy_in_use = 1;
  164. done:
  165. mutex_unlock(&phy->mutex);
  166. return rval;
  167. }
  168. void omap3isp_csiphy_release(struct isp_csiphy *phy)
  169. {
  170. mutex_lock(&phy->mutex);
  171. if (phy->phy_in_use) {
  172. csiphy_power_autoswitch_enable(phy, false);
  173. csiphy_set_power(phy, ISPCSI2_PHY_CFG_PWR_CMD_OFF);
  174. regulator_disable(phy->vdd);
  175. phy->phy_in_use = 0;
  176. }
  177. mutex_unlock(&phy->mutex);
  178. }
  179. /*
  180. * omap3isp_csiphy_init - Initialize the CSI PHY frontends
  181. */
  182. int omap3isp_csiphy_init(struct isp_device *isp)
  183. {
  184. struct isp_csiphy *phy1 = &isp->isp_csiphy1;
  185. struct isp_csiphy *phy2 = &isp->isp_csiphy2;
  186. isp->platform_cb.csiphy_config = csiphy_config;
  187. phy2->isp = isp;
  188. phy2->csi2 = &isp->isp_csi2a;
  189. phy2->num_data_lanes = ISP_CSIPHY2_NUM_DATA_LANES;
  190. phy2->cfg_regs = OMAP3_ISP_IOMEM_CSI2A_REGS1;
  191. phy2->phy_regs = OMAP3_ISP_IOMEM_CSIPHY2;
  192. mutex_init(&phy2->mutex);
  193. if (isp->revision == ISP_REVISION_15_0) {
  194. phy1->isp = isp;
  195. phy1->csi2 = &isp->isp_csi2c;
  196. phy1->num_data_lanes = ISP_CSIPHY1_NUM_DATA_LANES;
  197. phy1->cfg_regs = OMAP3_ISP_IOMEM_CSI2C_REGS1;
  198. phy1->phy_regs = OMAP3_ISP_IOMEM_CSIPHY1;
  199. mutex_init(&phy1->mutex);
  200. }
  201. return 0;
  202. }