mipi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_NOISE_FILTER(x) (((x) & 0xf) << 26)
  32. #define MIPI_CAL_CTRL_PRESCALE(x) (((x) & 0x3) << 24)
  33. #define MIPI_CAL_CTRL_CLKEN_OVR (1 << 4)
  34. #define MIPI_CAL_CTRL_START (1 << 0)
  35. #define MIPI_CAL_AUTOCAL_CTRL 0x01
  36. #define MIPI_CAL_STATUS 0x02
  37. #define MIPI_CAL_STATUS_DONE (1 << 16)
  38. #define MIPI_CAL_STATUS_ACTIVE (1 << 0)
  39. #define MIPI_CAL_CONFIG_CSIA 0x05
  40. #define MIPI_CAL_CONFIG_CSIB 0x06
  41. #define MIPI_CAL_CONFIG_CSIC 0x07
  42. #define MIPI_CAL_CONFIG_CSID 0x08
  43. #define MIPI_CAL_CONFIG_CSIE 0x09
  44. #define MIPI_CAL_CONFIG_DSIA 0x0e
  45. #define MIPI_CAL_CONFIG_DSIB 0x0f
  46. #define MIPI_CAL_CONFIG_DSIC 0x10
  47. #define MIPI_CAL_CONFIG_DSID 0x11
  48. #define MIPI_CAL_CONFIG_DSIA_CLK 0x19
  49. #define MIPI_CAL_CONFIG_DSIB_CLK 0x1a
  50. #define MIPI_CAL_CONFIG_CSIAB_CLK 0x1b
  51. #define MIPI_CAL_CONFIG_CSICD_CLK 0x1c
  52. #define MIPI_CAL_CONFIG_CSIE_CLK 0x1d
  53. /* for data and clock lanes */
  54. #define MIPI_CAL_CONFIG_SELECT (1 << 21)
  55. /* for data lanes */
  56. #define MIPI_CAL_CONFIG_HSPDOS(x) (((x) & 0x1f) << 16)
  57. #define MIPI_CAL_CONFIG_HSPUOS(x) (((x) & 0x1f) << 8)
  58. #define MIPI_CAL_CONFIG_TERMOS(x) (((x) & 0x1f) << 0)
  59. /* for clock lanes */
  60. #define MIPI_CAL_CONFIG_HSCLKPDOSD(x) (((x) & 0x1f) << 8)
  61. #define MIPI_CAL_CONFIG_HSCLKPUOSD(x) (((x) & 0x1f) << 0)
  62. #define MIPI_CAL_BIAS_PAD_CFG0 0x16
  63. #define MIPI_CAL_BIAS_PAD_PDVCLAMP (1 << 1)
  64. #define MIPI_CAL_BIAS_PAD_E_VCLAMP_REF (1 << 0)
  65. #define MIPI_CAL_BIAS_PAD_CFG1 0x17
  66. #define MIPI_CAL_BIAS_PAD_DRV_DN_REF(x) (((x) & 0x7) << 16)
  67. #define MIPI_CAL_BIAS_PAD_DRV_UP_REF(x) (((x) & 0x7) << 8)
  68. #define MIPI_CAL_BIAS_PAD_CFG2 0x18
  69. #define MIPI_CAL_BIAS_PAD_VCLAMP(x) (((x) & 0x7) << 16)
  70. #define MIPI_CAL_BIAS_PAD_VAUXP(x) (((x) & 0x7) << 4)
  71. #define MIPI_CAL_BIAS_PAD_PDVREG (1 << 1)
  72. struct tegra_mipi_pad {
  73. unsigned long data;
  74. unsigned long clk;
  75. };
  76. struct tegra_mipi_soc {
  77. bool has_clk_lane;
  78. const struct tegra_mipi_pad *pads;
  79. unsigned int num_pads;
  80. bool clock_enable_override;
  81. bool needs_vclamp_ref;
  82. /* bias pad configuration settings */
  83. u8 pad_drive_down_ref;
  84. u8 pad_drive_up_ref;
  85. u8 pad_vclamp_level;
  86. u8 pad_vauxp_level;
  87. /* calibration settings for data lanes */
  88. u8 hspdos;
  89. u8 hspuos;
  90. u8 termos;
  91. /* calibration settings for clock lanes */
  92. u8 hsclkpdos;
  93. u8 hsclkpuos;
  94. };
  95. struct tegra_mipi {
  96. const struct tegra_mipi_soc *soc;
  97. void __iomem *regs;
  98. struct mutex lock;
  99. struct clk *clk;
  100. };
  101. struct tegra_mipi_device {
  102. struct platform_device *pdev;
  103. struct tegra_mipi *mipi;
  104. struct device *device;
  105. unsigned long pads;
  106. };
  107. static inline u32 tegra_mipi_readl(struct tegra_mipi *mipi,
  108. unsigned long offset)
  109. {
  110. return readl(mipi->regs + (offset << 2));
  111. }
  112. static inline void tegra_mipi_writel(struct tegra_mipi *mipi, u32 value,
  113. unsigned long offset)
  114. {
  115. writel(value, mipi->regs + (offset << 2));
  116. }
  117. struct tegra_mipi_device *tegra_mipi_request(struct device *device)
  118. {
  119. struct device_node *np = device->of_node;
  120. struct tegra_mipi_device *dev;
  121. struct of_phandle_args args;
  122. int err;
  123. err = of_parse_phandle_with_args(np, "nvidia,mipi-calibrate",
  124. "#nvidia,mipi-calibrate-cells", 0,
  125. &args);
  126. if (err < 0)
  127. return ERR_PTR(err);
  128. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  129. if (!dev) {
  130. err = -ENOMEM;
  131. goto out;
  132. }
  133. dev->pdev = of_find_device_by_node(args.np);
  134. if (!dev->pdev) {
  135. err = -ENODEV;
  136. goto free;
  137. }
  138. dev->mipi = platform_get_drvdata(dev->pdev);
  139. if (!dev->mipi) {
  140. err = -EPROBE_DEFER;
  141. goto put;
  142. }
  143. of_node_put(args.np);
  144. dev->pads = args.args[0];
  145. dev->device = device;
  146. return dev;
  147. put:
  148. platform_device_put(dev->pdev);
  149. free:
  150. kfree(dev);
  151. out:
  152. of_node_put(args.np);
  153. return ERR_PTR(err);
  154. }
  155. EXPORT_SYMBOL(tegra_mipi_request);
  156. void tegra_mipi_free(struct tegra_mipi_device *device)
  157. {
  158. platform_device_put(device->pdev);
  159. kfree(device);
  160. }
  161. EXPORT_SYMBOL(tegra_mipi_free);
  162. static int tegra_mipi_wait(struct tegra_mipi *mipi)
  163. {
  164. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  165. u32 value;
  166. while (time_before(jiffies, timeout)) {
  167. value = tegra_mipi_readl(mipi, MIPI_CAL_STATUS);
  168. if ((value & MIPI_CAL_STATUS_ACTIVE) == 0 &&
  169. (value & MIPI_CAL_STATUS_DONE) != 0)
  170. return 0;
  171. usleep_range(10, 50);
  172. }
  173. return -ETIMEDOUT;
  174. }
  175. int tegra_mipi_calibrate(struct tegra_mipi_device *device)
  176. {
  177. const struct tegra_mipi_soc *soc = device->mipi->soc;
  178. unsigned int i;
  179. u32 value;
  180. int err;
  181. err = clk_enable(device->mipi->clk);
  182. if (err < 0)
  183. return err;
  184. mutex_lock(&device->mipi->lock);
  185. value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG0);
  186. value &= ~MIPI_CAL_BIAS_PAD_PDVCLAMP;
  187. if (soc->needs_vclamp_ref)
  188. value |= MIPI_CAL_BIAS_PAD_E_VCLAMP_REF;
  189. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG0);
  190. value = MIPI_CAL_BIAS_PAD_DRV_DN_REF(soc->pad_drive_down_ref) |
  191. MIPI_CAL_BIAS_PAD_DRV_UP_REF(soc->pad_drive_up_ref);
  192. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG1);
  193. value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG2);
  194. value &= ~MIPI_CAL_BIAS_PAD_PDVREG;
  195. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG2);
  196. value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG2);
  197. value &= ~MIPI_CAL_BIAS_PAD_VCLAMP(0x7);
  198. value &= ~MIPI_CAL_BIAS_PAD_VAUXP(0x7);
  199. value |= MIPI_CAL_BIAS_PAD_VCLAMP(soc->pad_vclamp_level);
  200. value |= MIPI_CAL_BIAS_PAD_VAUXP(soc->pad_vauxp_level);
  201. tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG2);
  202. for (i = 0; i < soc->num_pads; i++) {
  203. u32 clk = 0, data = 0;
  204. if (device->pads & BIT(i)) {
  205. data = MIPI_CAL_CONFIG_SELECT |
  206. MIPI_CAL_CONFIG_HSPDOS(soc->hspdos) |
  207. MIPI_CAL_CONFIG_HSPUOS(soc->hspuos) |
  208. MIPI_CAL_CONFIG_TERMOS(soc->termos);
  209. clk = MIPI_CAL_CONFIG_SELECT |
  210. MIPI_CAL_CONFIG_HSCLKPDOSD(soc->hsclkpdos) |
  211. MIPI_CAL_CONFIG_HSCLKPUOSD(soc->hsclkpuos);
  212. }
  213. tegra_mipi_writel(device->mipi, data, soc->pads[i].data);
  214. if (soc->has_clk_lane)
  215. tegra_mipi_writel(device->mipi, clk, soc->pads[i].clk);
  216. }
  217. value = tegra_mipi_readl(device->mipi, MIPI_CAL_CTRL);
  218. value &= ~MIPI_CAL_CTRL_NOISE_FILTER(0xf);
  219. value &= ~MIPI_CAL_CTRL_PRESCALE(0x3);
  220. value |= MIPI_CAL_CTRL_NOISE_FILTER(0xa);
  221. value |= MIPI_CAL_CTRL_PRESCALE(0x2);
  222. if (!soc->clock_enable_override)
  223. value &= ~MIPI_CAL_CTRL_CLKEN_OVR;
  224. else
  225. value |= MIPI_CAL_CTRL_CLKEN_OVR;
  226. tegra_mipi_writel(device->mipi, value, MIPI_CAL_CTRL);
  227. /* clear any pending status bits */
  228. value = tegra_mipi_readl(device->mipi, MIPI_CAL_STATUS);
  229. tegra_mipi_writel(device->mipi, value, MIPI_CAL_STATUS);
  230. value = tegra_mipi_readl(device->mipi, MIPI_CAL_CTRL);
  231. value |= MIPI_CAL_CTRL_START;
  232. tegra_mipi_writel(device->mipi, value, MIPI_CAL_CTRL);
  233. err = tegra_mipi_wait(device->mipi);
  234. mutex_unlock(&device->mipi->lock);
  235. clk_disable(device->mipi->clk);
  236. return err;
  237. }
  238. EXPORT_SYMBOL(tegra_mipi_calibrate);
  239. static const struct tegra_mipi_pad tegra114_mipi_pads[] = {
  240. { .data = MIPI_CAL_CONFIG_CSIA },
  241. { .data = MIPI_CAL_CONFIG_CSIB },
  242. { .data = MIPI_CAL_CONFIG_CSIC },
  243. { .data = MIPI_CAL_CONFIG_CSID },
  244. { .data = MIPI_CAL_CONFIG_CSIE },
  245. { .data = MIPI_CAL_CONFIG_DSIA },
  246. { .data = MIPI_CAL_CONFIG_DSIB },
  247. { .data = MIPI_CAL_CONFIG_DSIC },
  248. { .data = MIPI_CAL_CONFIG_DSID },
  249. };
  250. static const struct tegra_mipi_soc tegra114_mipi_soc = {
  251. .has_clk_lane = false,
  252. .pads = tegra114_mipi_pads,
  253. .num_pads = ARRAY_SIZE(tegra114_mipi_pads),
  254. .clock_enable_override = true,
  255. .needs_vclamp_ref = true,
  256. .pad_drive_down_ref = 0x2,
  257. .pad_drive_up_ref = 0x0,
  258. .pad_vclamp_level = 0x0,
  259. .pad_vauxp_level = 0x0,
  260. .hspdos = 0x0,
  261. .hspuos = 0x4,
  262. .termos = 0x5,
  263. .hsclkpdos = 0x0,
  264. .hsclkpuos = 0x4,
  265. };
  266. static const struct tegra_mipi_pad tegra124_mipi_pads[] = {
  267. { .data = MIPI_CAL_CONFIG_CSIA, .clk = MIPI_CAL_CONFIG_CSIAB_CLK },
  268. { .data = MIPI_CAL_CONFIG_CSIB, .clk = MIPI_CAL_CONFIG_CSIAB_CLK },
  269. { .data = MIPI_CAL_CONFIG_CSIC, .clk = MIPI_CAL_CONFIG_CSICD_CLK },
  270. { .data = MIPI_CAL_CONFIG_CSID, .clk = MIPI_CAL_CONFIG_CSICD_CLK },
  271. { .data = MIPI_CAL_CONFIG_CSIE, .clk = MIPI_CAL_CONFIG_CSIE_CLK },
  272. { .data = MIPI_CAL_CONFIG_DSIA, .clk = MIPI_CAL_CONFIG_DSIA_CLK },
  273. { .data = MIPI_CAL_CONFIG_DSIB, .clk = MIPI_CAL_CONFIG_DSIB_CLK },
  274. };
  275. static const struct tegra_mipi_soc tegra124_mipi_soc = {
  276. .has_clk_lane = true,
  277. .pads = tegra124_mipi_pads,
  278. .num_pads = ARRAY_SIZE(tegra124_mipi_pads),
  279. .clock_enable_override = true,
  280. .needs_vclamp_ref = true,
  281. .pad_drive_down_ref = 0x2,
  282. .pad_drive_up_ref = 0x0,
  283. .pad_vclamp_level = 0x0,
  284. .pad_vauxp_level = 0x0,
  285. .hspdos = 0x0,
  286. .hspuos = 0x0,
  287. .termos = 0x0,
  288. .hsclkpdos = 0x1,
  289. .hsclkpuos = 0x2,
  290. };
  291. static const struct of_device_id tegra_mipi_of_match[] = {
  292. { .compatible = "nvidia,tegra114-mipi", .data = &tegra114_mipi_soc },
  293. { .compatible = "nvidia,tegra124-mipi", .data = &tegra124_mipi_soc },
  294. { },
  295. };
  296. static int tegra_mipi_probe(struct platform_device *pdev)
  297. {
  298. const struct of_device_id *match;
  299. struct tegra_mipi *mipi;
  300. struct resource *res;
  301. int err;
  302. match = of_match_node(tegra_mipi_of_match, pdev->dev.of_node);
  303. if (!match)
  304. return -ENODEV;
  305. mipi = devm_kzalloc(&pdev->dev, sizeof(*mipi), GFP_KERNEL);
  306. if (!mipi)
  307. return -ENOMEM;
  308. mipi->soc = match->data;
  309. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  310. mipi->regs = devm_ioremap_resource(&pdev->dev, res);
  311. if (IS_ERR(mipi->regs))
  312. return PTR_ERR(mipi->regs);
  313. mutex_init(&mipi->lock);
  314. mipi->clk = devm_clk_get(&pdev->dev, NULL);
  315. if (IS_ERR(mipi->clk)) {
  316. dev_err(&pdev->dev, "failed to get clock\n");
  317. return PTR_ERR(mipi->clk);
  318. }
  319. err = clk_prepare(mipi->clk);
  320. if (err < 0)
  321. return err;
  322. platform_set_drvdata(pdev, mipi);
  323. return 0;
  324. }
  325. static int tegra_mipi_remove(struct platform_device *pdev)
  326. {
  327. struct tegra_mipi *mipi = platform_get_drvdata(pdev);
  328. clk_unprepare(mipi->clk);
  329. return 0;
  330. }
  331. struct platform_driver tegra_mipi_driver = {
  332. .driver = {
  333. .name = "tegra-mipi",
  334. .of_match_table = tegra_mipi_of_match,
  335. },
  336. .probe = tegra_mipi_probe,
  337. .remove = tegra_mipi_remove,
  338. };