2 * Copyright (C) 2013 NVIDIA Corporation
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.
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
23 #include <linux/clk.h>
24 #include <linux/delay.h>
26 #include <linux/of_platform.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
30 #define MIPI_CAL_CTRL 0x00
31 #define MIPI_CAL_CTRL_START (1 << 0)
33 #define MIPI_CAL_AUTOCAL_CTRL 0x01
35 #define MIPI_CAL_STATUS 0x02
36 #define MIPI_CAL_STATUS_DONE (1 << 16)
37 #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
49 #define MIPI_CAL_CONFIG_SELECT (1 << 21)
50 #define MIPI_CAL_CONFIG_HSPDOS(x) (((x) & 0x1f) << 16)
51 #define MIPI_CAL_CONFIG_HSPUOS(x) (((x) & 0x1f) << 8)
52 #define MIPI_CAL_CONFIG_TERMOS(x) (((x) & 0x1f) << 0)
54 #define MIPI_CAL_BIAS_PAD_CFG0 0x16
55 #define MIPI_CAL_BIAS_PAD_PDVCLAMP (1 << 1)
56 #define MIPI_CAL_BIAS_PAD_E_VCLAMP_REF (1 << 0)
58 #define MIPI_CAL_BIAS_PAD_CFG1 0x17
60 #define MIPI_CAL_BIAS_PAD_CFG2 0x18
61 #define MIPI_CAL_BIAS_PAD_PDVREG (1 << 1)
63 static const struct module {
66 { .reg = MIPI_CAL_CONFIG_CSIA },
67 { .reg = MIPI_CAL_CONFIG_CSIB },
68 { .reg = MIPI_CAL_CONFIG_CSIC },
69 { .reg = MIPI_CAL_CONFIG_CSID },
70 { .reg = MIPI_CAL_CONFIG_CSIE },
71 { .reg = MIPI_CAL_CONFIG_DSIA },
72 { .reg = MIPI_CAL_CONFIG_DSIB },
73 { .reg = MIPI_CAL_CONFIG_DSIC },
74 { .reg = MIPI_CAL_CONFIG_DSID },
83 struct tegra_mipi_device {
84 struct platform_device *pdev;
85 struct tegra_mipi *mipi;
86 struct device *device;
90 static inline unsigned long tegra_mipi_readl(struct tegra_mipi *mipi,
93 return readl(mipi->regs + (reg << 2));
96 static inline void tegra_mipi_writel(struct tegra_mipi *mipi,
97 unsigned long value, unsigned long reg)
99 writel(value, mipi->regs + (reg << 2));
102 struct tegra_mipi_device *tegra_mipi_request(struct device *device)
104 struct device_node *np = device->of_node;
105 struct tegra_mipi_device *dev;
106 struct of_phandle_args args;
109 err = of_parse_phandle_with_args(np, "nvidia,mipi-calibrate",
110 "#nvidia,mipi-calibrate-cells", 0,
115 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
117 of_node_put(args.np);
122 dev->pdev = of_find_device_by_node(args.np);
124 of_node_put(args.np);
129 of_node_put(args.np);
131 dev->mipi = platform_get_drvdata(dev->pdev);
137 dev->pads = args.args[0];
138 dev->device = device;
143 platform_device_put(dev->pdev);
149 EXPORT_SYMBOL(tegra_mipi_request);
151 void tegra_mipi_free(struct tegra_mipi_device *device)
153 platform_device_put(device->pdev);
156 EXPORT_SYMBOL(tegra_mipi_free);
158 static int tegra_mipi_wait(struct tegra_mipi *mipi)
160 unsigned long timeout = jiffies + msecs_to_jiffies(250);
163 while (time_before(jiffies, timeout)) {
164 value = tegra_mipi_readl(mipi, MIPI_CAL_STATUS);
165 if ((value & MIPI_CAL_STATUS_ACTIVE) == 0 &&
166 (value & MIPI_CAL_STATUS_DONE) != 0)
169 usleep_range(10, 50);
175 int tegra_mipi_calibrate(struct tegra_mipi_device *device)
181 err = clk_enable(device->mipi->clk);
185 mutex_lock(&device->mipi->lock);
187 value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG0);
188 value &= ~MIPI_CAL_BIAS_PAD_PDVCLAMP;
189 value |= MIPI_CAL_BIAS_PAD_E_VCLAMP_REF;
190 tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG0);
192 value = tegra_mipi_readl(device->mipi, MIPI_CAL_BIAS_PAD_CFG2);
193 value &= ~MIPI_CAL_BIAS_PAD_PDVREG;
194 tegra_mipi_writel(device->mipi, value, MIPI_CAL_BIAS_PAD_CFG2);
196 for (i = 0; i < ARRAY_SIZE(modules); i++) {
197 if (device->pads & BIT(i))
198 value = MIPI_CAL_CONFIG_SELECT |
199 MIPI_CAL_CONFIG_HSPDOS(0) |
200 MIPI_CAL_CONFIG_HSPUOS(4) |
201 MIPI_CAL_CONFIG_TERMOS(5);
205 tegra_mipi_writel(device->mipi, value, modules[i].reg);
208 tegra_mipi_writel(device->mipi, MIPI_CAL_CTRL_START, MIPI_CAL_CTRL);
210 err = tegra_mipi_wait(device->mipi);
212 mutex_unlock(&device->mipi->lock);
213 clk_disable(device->mipi->clk);
217 EXPORT_SYMBOL(tegra_mipi_calibrate);
219 static int tegra_mipi_probe(struct platform_device *pdev)
221 struct tegra_mipi *mipi;
222 struct resource *res;
225 mipi = devm_kzalloc(&pdev->dev, sizeof(*mipi), GFP_KERNEL);
229 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
230 mipi->regs = devm_ioremap_resource(&pdev->dev, res);
231 if (IS_ERR(mipi->regs))
232 return PTR_ERR(mipi->regs);
234 mutex_init(&mipi->lock);
236 mipi->clk = devm_clk_get(&pdev->dev, NULL);
237 if (IS_ERR(mipi->clk)) {
238 dev_err(&pdev->dev, "failed to get clock\n");
239 return PTR_ERR(mipi->clk);
242 err = clk_prepare(mipi->clk);
246 platform_set_drvdata(pdev, mipi);
251 static int tegra_mipi_remove(struct platform_device *pdev)
253 struct tegra_mipi *mipi = platform_get_drvdata(pdev);
255 clk_unprepare(mipi->clk);
260 static struct of_device_id tegra_mipi_of_match[] = {
261 { .compatible = "nvidia,tegra114-mipi", },
265 struct platform_driver tegra_mipi_driver = {
267 .name = "tegra-mipi",
268 .of_match_table = tegra_mipi_of_match,
270 .probe = tegra_mipi_probe,
271 .remove = tegra_mipi_remove,