2 * drivers/w1/masters/tegra-w1.c
4 * W1 master driver for internal OWR controllers in NVIDIA Tegra SoCs.
6 * Copyright (C) 2010 Motorola, Inc
7 * Author: Andrei Warkentin <andreiw@motorola.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <linux/module.h>
25 #include <linux/clk.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/err.h>
32 #include <linux/uaccess.h>
37 #include "../w1_int.h"
38 #include "../w1_log.h"
40 #define DRIVER_NAME "tegra_w1"
42 /* OWR_CONTROL_0 is the main control register, and should be configured
43 last after configuring all other settings. */
44 #define OWR_CONTROL (0x0)
45 #define OC_RD_BIT (1 << 31)
46 #define OC_WR0_BIT (1 << 30)
47 #define OC_RD_SCLK_SHIFT (23)
48 #define OC_RD_SCLK_MASK (0xF)
49 #define OC_P_SCLK_SHIFT (15)
50 #define OC_P_SCLK_MASK (0xFF)
51 #define OC_BIT_XMODE (1 << 2)
52 #define OC_GO (1 << 0)
54 /* OWR_WR_RD_TCTL_0 controls read/write timings. */
55 #define OWR_WR_RD_TCTL (0xc)
56 #define ORWT_TSU_SHIFT (28)
57 #define ORWT_TSU_MASK (0x3)
58 #define ORWT_TRELEASE_SHIFT (22)
59 #define ORWT_TRELEASE_MASK (0x3F)
60 #define ORWT_TRDV_SHIFT (18)
61 #define ORWT_TRDV_MASK (0xF)
62 #define ORWT_TLOW0_SHIFT (11)
63 #define ORWT_TLOW0_MASK (0x7F)
64 #define ORWT_TLOW1_SHIFT (7)
65 #define ORWT_TLOW1_MASK (0xF)
66 #define ORWT_TSLOT_SHIFT (0)
67 #define ORWT_TSLOT_MASK (0x7F)
69 /* OWR_RST_PRES_TCTL_0 controls reset presence timings. */
70 #define OWR_RST_PRES_TCTL (0x10)
71 #define ORPT_TPDL_SHIFT (24)
72 #define ORPT_TPDL_MASK (0xFF)
73 #define ORPT_TPDH_SHIFT (18)
74 #define ORPT_TPDH_MASK (0x3F)
75 #define ORPT_TRSTL_SHIFT (9)
76 #define ORPT_TRSTL_MASK (0x1FF)
77 #define ORPT_TRSTH_SHIFT (0)
78 #define ORPT_TRSTH_MASK (0x1FF)
80 /* OWR_INTR_MASK_0 stores the masks for the interrupts. */
81 #define OWR_INTR_MASK (0x24)
82 #define OI_BIT_XFER_DONE (1 << 13)
83 #define OI_PRESENCE_DONE (1 << 5)
84 #define OI_PRESENCE_ERR (1 << 0)
86 /* OWR_INTR_STATUS_0 is the interrupt status register. */
87 #define OWR_INTR_STATUS (0x28)
89 /* OWR_STATUS_0 is the status register. */
90 #define OWR_STATUS (0x34)
91 #define OS_READ_BIT_SHIFT (23)
92 #define OS_RDY (1 << 0)
94 /* Transfer_completion wait time. */
95 #define BIT_XFER_COMPLETION_TIMEOUT_MSEC (5000)
97 /* Errors in the interrupt status register for bit
99 #define BIT_XFER_ERRORS (OI_PRESENCE_ERR)
101 /* OWR requires 1MHz clock. This value is in Herz. */
102 #define OWR_CLOCK (1000000)
104 #define W1_ERR(format, ...) \
105 printk(KERN_ERR "(%s: line %d) " format, \
106 __func__, __LINE__, ## __VA_ARGS__)
108 struct tegra_device {
110 struct w1_bus_master bus_master;
112 void __iomem *ioaddr;
115 struct completion *transfer_completion;
116 unsigned long intr_status;
117 struct tegra_w1_timings *timings;
120 /* If debug_print & DEBUG_PRESENCE, print whether slaves detected
121 or not in reset_bus. */
122 #define DEBUG_PRESENCE (0x1)
124 /* If debug_print & DEBUG_TIMEOUT, print whether timeouts on waiting
125 for device interrupts occurs. */
126 #define DEBUG_TIMEOUT (0x2)
128 static uint32_t debug_print;
129 module_param_named(debug, debug_print, uint, S_IRUGO | S_IWUSR);
130 MODULE_PARM_DESC(debug, "Debugging output commands:\n"
131 "\tbit 0 - log reset_bus presence detects\n"
132 "\tbit 1 - log interrupt timeouts\n");
134 /* Reads the OWR register specified by base offset in 'reg'. */
135 static inline unsigned long w1_readl(struct tegra_device *dev,
138 return readl(dev->ioaddr + reg);
141 /* Writes 'val' into the OWR registers specified by base offset in 'reg'. */
142 static inline void w1_writel(struct tegra_device *dev, unsigned long val,
145 writel(val, dev->ioaddr + reg);
148 /* Sets interrupt mask the device. */
149 static inline void w1_imask(struct tegra_device *dev, unsigned long mask)
151 w1_writel(dev, mask, OWR_INTR_MASK);
154 /* Waits for completion of a bit transfer, checks intr_status against
155 BIT_XFER_ERRORS and an additional provided bit mask. */
156 static inline int w1_wait(struct tegra_device *dev, unsigned long mask)
159 unsigned long irq_flags;
160 unsigned long intr_status;
162 ret = wait_for_completion_timeout(dev->transfer_completion,
163 msecs_to_jiffies(BIT_XFER_COMPLETION_TIMEOUT_MSEC));
165 if (unlikely(!ret)) {
166 if (debug_print & DEBUG_TIMEOUT)
171 spin_lock_irqsave(&dev->spinlock, irq_flags);
172 intr_status = dev->intr_status;
173 dev->intr_status = 0;
174 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
176 if (unlikely(intr_status & BIT_XFER_ERRORS ||
177 !(intr_status & mask)))
182 /* Programs timing registers, and puts the device into a known state.
183 Interrupts are safe to enable past this point. */
184 static int w1_setup(struct tegra_device *dev)
187 clk_enable(dev->clk);
190 ((dev->timings->tslot & ORWT_TSLOT_MASK) << ORWT_TSLOT_SHIFT) |
191 ((dev->timings->tlow1 & ORWT_TLOW1_MASK) << ORWT_TLOW1_SHIFT) |
192 ((dev->timings->tlow0 & ORWT_TLOW0_MASK) << ORWT_TLOW0_SHIFT) |
193 ((dev->timings->trdv & ORWT_TRDV_MASK) << ORWT_TRDV_SHIFT) |
194 ((dev->timings->trelease & ORWT_TRELEASE_MASK) <<
195 ORWT_TRELEASE_SHIFT) |
196 ((dev->timings->tsu & ORWT_TSU_MASK) << ORWT_TSU_SHIFT);
197 w1_writel(dev, value, OWR_WR_RD_TCTL);
200 ((dev->timings->trsth & ORPT_TRSTH_MASK) << ORPT_TRSTH_SHIFT) |
201 ((dev->timings->trstl & ORPT_TRSTL_MASK) << ORPT_TRSTL_SHIFT) |
202 ((dev->timings->tpdh & ORPT_TPDH_MASK) << ORPT_TPDH_SHIFT) |
203 ((dev->timings->tpdl & ORPT_TPDL_MASK) << ORPT_TPDL_SHIFT);
204 w1_writel(dev, value, OWR_RST_PRES_TCTL);
206 /* Clear interrupt status/mask registers in case
207 anything was set in it. */
209 w1_writel(dev, 0xFFFFFFFF, OWR_INTR_STATUS);
210 clk_disable(dev->clk);
214 /* Interrupt handler for OWR communication. */
215 static irqreturn_t tegra_w1_irq(int irq, void *cookie)
217 unsigned long irq_flags;
218 unsigned long status;
219 struct tegra_device *dev = cookie;
221 status = w1_readl(dev, OWR_INTR_STATUS);
222 if (unlikely(!status)) {
224 /* Not for me if no status bits are set. */
228 spin_lock_irqsave(&dev->spinlock, irq_flags);
230 if (likely(dev->transfer_completion)) {
231 dev->intr_status = status;
232 w1_writel(dev, status, OWR_INTR_STATUS);
233 complete(dev->transfer_completion);
235 W1_ERR("spurious interrupt, status = 0x%lx\n", status);
238 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
242 /* Perform a write-0 cycle if bit == 0, otherwise
243 perform a read cycle. */
244 static u8 tegra_w1_touch_bit(void *data, u8 bit)
248 unsigned long control;
249 DECLARE_COMPLETION_ONSTACK(touch_done);
250 struct tegra_device *dev = (struct tegra_device *) data;
253 mutex_lock(&dev->mutex);
257 clk_enable(dev->clk);
258 w1_imask(dev, OI_BIT_XFER_DONE);
259 dev->transfer_completion = &touch_done;
261 ((dev->timings->rdsclk & OC_RD_SCLK_MASK) << OC_RD_SCLK_SHIFT) |
262 ((dev->timings->psclk & OC_P_SCLK_MASK) << OC_P_SCLK_SHIFT) |
265 /* Read bit (well, writes a 1 to the bus as well). */
267 w1_writel(dev, control | OC_RD_BIT, OWR_CONTROL);
268 rc = w1_wait(dev, OI_BIT_XFER_DONE);
271 W1_ERR("write-1/read failed\n");
276 (w1_readl(dev, OWR_STATUS) >> OS_READ_BIT_SHIFT) & 1;
282 w1_writel(dev, control | OC_WR0_BIT, OWR_CONTROL);
283 rc = w1_wait(dev, OI_BIT_XFER_DONE);
285 W1_ERR("write-0 failed %d\n", rc);
293 dev->transfer_completion = NULL;
294 clk_disable(dev->clk);
296 mutex_unlock(&dev->mutex);
300 /* Performs a bus reset cycle, and returns 0 if slaves present. */
301 static u8 tegra_w1_reset_bus(void *data)
306 DECLARE_COMPLETION_ONSTACK(reset_done);
307 struct tegra_device *dev = (struct tegra_device *) data;
310 mutex_lock(&dev->mutex);
314 clk_enable(dev->clk);
315 w1_imask(dev, OI_PRESENCE_DONE);
316 dev->transfer_completion = &reset_done;
318 ((dev->timings->rdsclk & OC_RD_SCLK_MASK) << OC_RD_SCLK_SHIFT) |
319 ((dev->timings->psclk & OC_P_SCLK_MASK) << OC_P_SCLK_SHIFT) |
320 OC_BIT_XMODE | OC_GO;
321 w1_writel(dev, value, OWR_CONTROL);
323 rc = w1_wait(dev, OI_PRESENCE_DONE);
330 if (debug_print & DEBUG_PRESENCE) {
332 W1_ERR("no slaves present\n");
334 W1_ERR("slaves present\n");
338 dev->transfer_completion = NULL;
339 clk_disable(dev->clk);
341 mutex_unlock(&dev->mutex);
345 static int tegra_w1_probe(struct platform_device *pdev)
349 struct resource *res;
350 struct tegra_device *dev;
351 struct tegra_w1_platform_data *plat = pdev->dev.platform_data;
353 printk(KERN_INFO "Driver for Tegra SoC 1-wire controller\n");
355 if (plat == NULL || plat->timings == NULL)
358 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
368 dev = kzalloc(sizeof(struct tegra_device), GFP_KERNEL);
372 platform_set_drvdata(pdev, dev);
373 dev->clk = clk_get(&pdev->dev, plat->clk_id);
374 if (IS_ERR(dev->clk)) {
375 rc = PTR_ERR(dev->clk);
379 /* OWR requires 1MHz clock. */
380 rc = clk_set_rate(dev->clk, OWR_CLOCK);
384 if (!request_mem_region
385 (res->start, res->end - res->start + 1, dev_name(&pdev->dev))) {
390 dev->ioaddr = ioremap(res->start, res->end - res->start + 1);
396 dev->timings = plat->timings;
397 dev->bus_master.data = dev;
398 dev->bus_master.touch_bit = tegra_w1_touch_bit;
399 dev->bus_master.reset_bus = tegra_w1_reset_bus;
401 spin_lock_init(&dev->spinlock);
402 mutex_init(&dev->mutex);
404 /* Program device into known state. */
407 rc = request_irq(irq, tegra_w1_irq, IRQF_SHARED, DRIVER_NAME, dev);
409 goto cleanup_ioremap;
411 rc = w1_add_master_device(&dev->bus_master);
421 iounmap(dev->ioaddr);
423 release_mem_region(res->start,
424 res->end - res->start + 1);
428 platform_set_drvdata(pdev, NULL);
433 static int tegra_w1_remove(struct platform_device *pdev)
435 struct resource *res;
436 struct tegra_device *dev = platform_get_drvdata(pdev);
438 mutex_lock(&dev->mutex);
440 mutex_unlock(&dev->mutex);
442 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
443 free_irq(res->start, dev);
444 iounmap(dev->ioaddr);
445 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 release_mem_region(res->start, res->end - res->start + 1);
448 platform_set_drvdata(pdev, NULL);
453 static int tegra_w1_suspend(struct platform_device *pdev, pm_message_t state)
455 struct tegra_device *dev = platform_get_drvdata(pdev);
456 mutex_lock(&dev->mutex);
458 mutex_unlock(&dev->mutex);
462 static int tegra_w1_resume(struct platform_device *pdev)
464 struct tegra_device *dev = platform_get_drvdata(pdev);
466 /* TODO: Is this necessary? I would assume yes. */
467 mutex_lock(&dev->mutex);
470 mutex_unlock(&dev->mutex);
474 static struct platform_driver tegra_w1_driver = {
475 .probe = tegra_w1_probe,
476 .remove = tegra_w1_remove,
477 .suspend = tegra_w1_suspend,
478 .resume = tegra_w1_resume,
481 .owner = THIS_MODULE,
485 static int __init tegra_w1_init(void)
487 return platform_driver_register(&tegra_w1_driver);
490 static void __exit tegra_w1_exit(void)
492 platform_driver_unregister(&tegra_w1_driver);
495 module_init(tegra_w1_init);
496 module_exit(tegra_w1_exit);
498 MODULE_DESCRIPTION("Tegra W1 master driver");
499 MODULE_AUTHOR("Andrei Warkentin <andreiw@motorola.com>");
500 MODULE_LICENSE("GPL");