mipi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/host1x.h>
  25. #include <linux/io.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include "dev.h"
  30. #define MIPI_CAL_CTRL 0x00
  31. #define MIPI_CAL_CTRL_START (1 << 0)
  32. #define MIPI_CAL_AUTOCAL_CTRL 0x01
  33. #define MIPI_CAL_STATUS 0x02
  34. #define MIPI_CAL_STATUS_DONE (1 << 16)
  35. #define MIPI_CAL_STATUS_ACTIVE (1 << 0)
  36. #define MIPI_CAL_CONFIG_CSIA 0x05
  37. #define MIPI_CAL_CONFIG_CSIB 0x06
  38. #define MIPI_CAL_CONFIG_CSIC 0x07
  39. #define MIPI_CAL_CONFIG_CSID 0x08
  40. #define MIPI_CAL_CONFIG_CSIE 0x09
  41. #define MIPI_CAL_CONFIG_DSIA 0x0e
  42. #define MIPI_CAL_CONFIG_DSIB 0x0f
  43. #define MIPI_CAL_CONFIG_DSIC 0x10
  44. #define MIPI_CAL_CONFIG_DSID 0x11
  45. #define MIPI_CAL_CONFIG_DSIAB_CLK 0x19
  46. #define MIPI_CAL_CONFIG_DSICD_CLK 0x1a
  47. #define MIPI_CAL_CONFIG_CSIAB_CLK 0x1b
  48. #define MIPI_CAL_CONFIG_CSICD_CLK 0x1c
  49. #define MIPI_CAL_CONFIG_CSIE_CLK 0x1d
  50. /* for data and clock lanes */
  51. #define MIPI_CAL_CONFIG_SELECT (1 << 21)
  52. /* for data lanes */
  53. #define MIPI_CAL_CONFIG_HSPDOS(x) (((x) & 0x1f) << 16)
  54. #define MIPI_CAL_CONFIG_HSPUOS(x) (((x) & 0x1f) << 8)
  55. #define MIPI_CAL_CONFIG_TERMOS(x) (((x) & 0x1f) << 0)
  56. /* for clock lanes */
  57. #define MIPI_CAL_CONFIG_HSCLKPDOSD(x) (((x) & 0x1f) << 8)
  58. #define MIPI_CAL_CONFIG_HSCLKPUOSD(x) (((x) & 0x1f) << 0)
  59. #define MIPI_CAL_BIAS_PAD_CFG0 0x16
  60. #define MIPI_CAL_BIAS_PAD_PDVCLAMP (1 << 1)
  61. #define MIPI_CAL_BIAS_PAD_E_VCLAMP_REF (1 << 0)
  62. #define MIPI_CAL_BIAS_PAD_CFG1 0x17
  63. #define MIPI_CAL_BIAS_PAD_DRV_DN_REF(x) (((x) & 0x7) << 16)
  64. #define MIPI_CAL_BIAS_PAD_CFG2 0x18
  65. #define MIPI_CAL_BIAS_PAD_PDVREG (1 << 1)
  66. struct tegra_mipi_pad {
  67. unsigned long data;
  68. unsigned long clk;
  69. };
  70. struct tegra_mipi_soc {
  71. bool has_clk_lane;
  72. const struct tegra_mipi_pad *pads;
  73. unsigned int num_pads;
  74. };
  75. struct tegra_mipi {
  76. const struct tegra_mipi_soc *soc;
  77. void __iomem *regs;
  78. struct mutex lock;
  79. struct clk *clk;
  80. };
  81. struct tegra_mipi_device {
  82. struct platform_device *pdev;
  83. struct tegra_mipi *mipi;
  84. struct device *device;
  85. unsigned long pads;
  86. };
  87. static inline u32 tegra_mipi_readl(struct tegra_mipi *mipi,
  88. unsigned long offset)
  89. {
  90. return readl(mipi->regs + (offset << 2));
  91. }
  92. static inline void tegra_mipi_writel(struct tegra_mipi *mipi, u32 value,
  93. unsigned long offset)
  94. {
  95. writel(value, mipi->regs + (offset << 2));
  96. }
  97. struct tegra_mipi_device *tegra_mipi_request(struct device *device)
  98. {
  99. struct device_node *np = device->of_node;
  100. struct tegra_mipi_device *dev;
  101. struct of_phandle_args args;
  102. int err;
  103. err = of_parse_phandle_with_args(np, "nvidia,mipi-calibrate",
  104. "#nvidia,mipi-calibrate-cells", 0,
  105. &args);
  106. if (err < 0)
  107. return ERR_PTR(err);
  108. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  109. if (!dev) {
  110. err = -ENOMEM;
  111. goto out;
  112. }
  113. dev->pdev = of_find_device_by_node(args.np);
  114. if (!dev->pdev) {
  115. err = -ENODEV;
  116. goto free;
  117. }
  118. dev->mipi = platform_get_drvdata(dev->pdev);
  119. if (!dev->mipi) {
  120. err = -EPROBE_DEFER;
  121. goto put;
  122. }
  123. of_node_put(args.np);
  124. dev->pads = args.args[0];
  125. dev->device = device;
  126. return dev;
  127. put:
  128. platform_device_put(dev->pdev);
  129. free:
  130. kfree(dev);
  131. out:
  132. of_node_put(args.np);
  133. return ERR_PTR(err);
  134. }
  135. EXPORT_SYMBOL(tegra_mipi_request);
  136. void tegra_mipi_free(struct tegra_mipi_device *device)
  137. {
  138. platform_device_put(device->pdev);
  139. kfree(device);
  140. }
  141. EXPORT_SYMBOL(tegra_mipi_free);
  142. static int tegra_mipi_wait(struct tegra_mipi *mipi)
  143. {
  144. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  145. u32 value;
  146. while (time_before(jiffies, timeout)) {
  147. value = tegra_mipi_readl(mipi, MIPI_CAL_STATUS);
  148. if ((value & MIPI_CAL_STATUS_ACTIVE) == 0 &&
  149. (value & MIPI_CAL_STATUS_DONE) != 0)
  150. return 0;
  151. usleep_range(10, 50);
  152. }
  153. return -ETIMEDOUT;
  154. }
  155. int tegra_mipi_calibrate(struct tegra_mipi_device *device)
  156. {
  157. const struct tegra_mipi_soc *soc = device->mipi->soc;
  158. unsigned int i;
  159. u32 value;
  160. int err;
  161. err = clk_enable(device->mipi->clk);
  162. if (err < 0)
  163. return err;
  164. mutex_lock(&device->mipi->lock);
  165. value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG0);
  166. value &= ~MIPI_CAL_BIAS_PAD_PDVCLAMP;
  167. value |= MIPI_CAL_BIAS_PAD_E_VCLAMP_REF;
  168. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG0);
  169. tegra_mipi_writel(device->mipi, MIPI_CAL_BIAS_PAD_DRV_DN_REF(2),
  170. MIPI_CAL_BIAS_PAD_CFG1);
  171. value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG2);
  172. value &= ~MIPI_CAL_BIAS_PAD_PDVREG;
  173. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG2);
  174. for (i = 0; i < soc->num_pads; i++) {
  175. u32 clk = 0, data = 0;
  176. if (device->pads & BIT(i)) {
  177. data = MIPI_CAL_CONFIG_SELECT |
  178. MIPI_CAL_CONFIG_HSPDOS(0) |
  179. MIPI_CAL_CONFIG_HSPUOS(4) |
  180. MIPI_CAL_CONFIG_TERMOS(5);
  181. clk = MIPI_CAL_CONFIG_SELECT |
  182. MIPI_CAL_CONFIG_HSCLKPDOSD(0) |
  183. MIPI_CAL_CONFIG_HSCLKPUOSD(4);
  184. }
  185. tegra_mipi_writel(device->mipi, data, soc->pads[i].data);
  186. if (soc->has_clk_lane)
  187. tegra_mipi_writel(device->mipi, clk, soc->pads[i].clk);
  188. }
  189. value = tegra_mipi_readl(device->mipi, MIPI_CAL_CTRL);
  190. value |= MIPI_CAL_CTRL_START;
  191. tegra_mipi_writel(device->mipi, value, MIPI_CAL_CTRL);
  192. err = tegra_mipi_wait(device->mipi);
  193. mutex_unlock(&device->mipi->lock);
  194. clk_disable(device->mipi->clk);
  195. return err;
  196. }
  197. EXPORT_SYMBOL(tegra_mipi_calibrate);
  198. static const struct tegra_mipi_pad tegra114_mipi_pads[] = {
  199. { .data = MIPI_CAL_CONFIG_CSIA },
  200. { .data = MIPI_CAL_CONFIG_CSIB },
  201. { .data = MIPI_CAL_CONFIG_CSIC },
  202. { .data = MIPI_CAL_CONFIG_CSID },
  203. { .data = MIPI_CAL_CONFIG_CSIE },
  204. { .data = MIPI_CAL_CONFIG_DSIA },
  205. { .data = MIPI_CAL_CONFIG_DSIB },
  206. { .data = MIPI_CAL_CONFIG_DSIC },
  207. { .data = MIPI_CAL_CONFIG_DSID },
  208. };
  209. static const struct tegra_mipi_soc tegra114_mipi_soc = {
  210. .has_clk_lane = false,
  211. .pads = tegra114_mipi_pads,
  212. .num_pads = ARRAY_SIZE(tegra114_mipi_pads),
  213. };
  214. static const struct tegra_mipi_pad tegra124_mipi_pads[] = {
  215. { .data = MIPI_CAL_CONFIG_CSIA, .clk = MIPI_CAL_CONFIG_CSIAB_CLK },
  216. { .data = MIPI_CAL_CONFIG_CSIB, .clk = MIPI_CAL_CONFIG_CSIAB_CLK },
  217. { .data = MIPI_CAL_CONFIG_CSIC, .clk = MIPI_CAL_CONFIG_CSICD_CLK },
  218. { .data = MIPI_CAL_CONFIG_CSID, .clk = MIPI_CAL_CONFIG_CSICD_CLK },
  219. { .data = MIPI_CAL_CONFIG_CSIE, .clk = MIPI_CAL_CONFIG_CSIE_CLK },
  220. { .data = MIPI_CAL_CONFIG_DSIA, .clk = MIPI_CAL_CONFIG_DSIAB_CLK },
  221. { .data = MIPI_CAL_CONFIG_DSIB, .clk = MIPI_CAL_CONFIG_DSIAB_CLK },
  222. };
  223. static const struct tegra_mipi_soc tegra124_mipi_soc = {
  224. .has_clk_lane = true,
  225. .pads = tegra124_mipi_pads,
  226. .num_pads = ARRAY_SIZE(tegra124_mipi_pads),
  227. };
  228. static struct of_device_id tegra_mipi_of_match[] = {
  229. { .compatible = "nvidia,tegra114-mipi", .data = &tegra114_mipi_soc },
  230. { .compatible = "nvidia,tegra124-mipi", .data = &tegra124_mipi_soc },
  231. { },
  232. };
  233. static int tegra_mipi_probe(struct platform_device *pdev)
  234. {
  235. const struct of_device_id *match;
  236. struct tegra_mipi *mipi;
  237. struct resource *res;
  238. int err;
  239. match = of_match_node(tegra_mipi_of_match, pdev->dev.of_node);
  240. if (!match)
  241. return -ENODEV;
  242. mipi = devm_kzalloc(&pdev->dev, sizeof(*mipi), GFP_KERNEL);
  243. if (!mipi)
  244. return -ENOMEM;
  245. mipi->soc = match->data;
  246. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. mipi->regs = devm_ioremap_resource(&pdev->dev, res);
  248. if (IS_ERR(mipi->regs))
  249. return PTR_ERR(mipi->regs);
  250. mutex_init(&mipi->lock);
  251. mipi->clk = devm_clk_get(&pdev->dev, NULL);
  252. if (IS_ERR(mipi->clk)) {
  253. dev_err(&pdev->dev, "failed to get clock\n");
  254. return PTR_ERR(mipi->clk);
  255. }
  256. err = clk_prepare(mipi->clk);
  257. if (err < 0)
  258. return err;
  259. platform_set_drvdata(pdev, mipi);
  260. return 0;
  261. }
  262. static int tegra_mipi_remove(struct platform_device *pdev)
  263. {
  264. struct tegra_mipi *mipi = platform_get_drvdata(pdev);
  265. clk_unprepare(mipi->clk);
  266. return 0;
  267. }
  268. struct platform_driver tegra_mipi_driver = {
  269. .driver = {
  270. .name = "tegra-mipi",
  271. .of_match_table = tegra_mipi_of_match,
  272. },
  273. .probe = tegra_mipi_probe,
  274. .remove = tegra_mipi_remove,
  275. };