ispcsiphy.c 9.4 KB

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