aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/w1/masters/tegra_w1.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/w1/masters/tegra_w1.c')
-rw-r--r--drivers/w1/masters/tegra_w1.c491
1 files changed, 491 insertions, 0 deletions
diff --git a/drivers/w1/masters/tegra_w1.c b/drivers/w1/masters/tegra_w1.c
new file mode 100644
index 00000000000..9443c4b1dbc
--- /dev/null
+++ b/drivers/w1/masters/tegra_w1.c
@@ -0,0 +1,491 @@
1/*
2 * drivers/w1/masters/tegra-w1.c
3 *
4 * W1 master driver for internal OWR controllers in NVIDIA Tegra SoCs.
5 *
6 * Copyright (C) 2010 Motorola, Inc
7 * Author: Andrei Warkentin <andreiw@motorola.com>
8 *
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.
13 *
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
17 * more details.
18 *
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.
22 */
23
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>
31#include <linux/io.h>
32#include <linux/uaccess.h>
33
34#include <mach/w1.h>
35
36#include "../w1.h"
37#include "../w1_int.h"
38#include "../w1_log.h"
39
40#define DRIVER_NAME "tegra_w1"
41
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)
53
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)
68
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)
79
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)
85
86/* OWR_INTR_STATUS_0 is the interrupt status register. */
87#define OWR_INTR_STATUS (0x28)
88
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)
93
94/* Transfer_completion wait time. */
95#define BIT_XFER_COMPLETION_TIMEOUT_MSEC (5000)
96
97/* Errors in the interrupt status register for bit
98 transfers. */
99#define BIT_XFER_ERRORS (OI_PRESENCE_ERR)
100
101/* OWR requires 1MHz clock. This value is in Herz. */
102#define OWR_CLOCK (1000000)
103
104#define W1_ERR(format, ...) \
105 printk(KERN_ERR "(%s: line %d) " format, \
106 __func__, __LINE__, ## __VA_ARGS__)
107
108struct tegra_device {
109 bool ready;
110 struct w1_bus_master bus_master;
111 struct clk *clk;
112 void __iomem *ioaddr;
113 struct mutex mutex;
114 spinlock_t spinlock;
115 struct completion *transfer_completion;
116 unsigned long intr_status;
117 struct tegra_w1_timings *timings;
118};
119
120/* If debug_print & DEBUG_PRESENCE, print whether slaves detected
121 or not in reset_bus. */
122#define DEBUG_PRESENCE (0x1)
123
124/* If debug_print & DEBUG_TIMEOUT, print whether timeouts on waiting
125 for device interrupts occurs. */
126#define DEBUG_TIMEOUT (0x2)
127
128static uint32_t debug_print;
129module_param_named(debug, debug_print, uint, S_IRUGO | S_IWUSR);
130MODULE_PARM_DESC(debug, "Debugging output commands:\n"
131 "\tbit 0 - log reset_bus presence detects\n"
132 "\tbit 1 - log interrupt timeouts\n");
133
134/* Reads the OWR register specified by base offset in 'reg'. */
135static inline unsigned long w1_readl(struct tegra_device *dev,
136 unsigned long reg)
137{
138 return readl(dev->ioaddr + reg);
139}
140
141/* Writes 'val' into the OWR registers specified by base offset in 'reg'. */
142static inline void w1_writel(struct tegra_device *dev, unsigned long val,
143 unsigned long reg)
144{
145 writel(val, dev->ioaddr + reg);
146}
147
148/* Sets interrupt mask the device. */
149static inline void w1_imask(struct tegra_device *dev, unsigned long mask)
150{
151 w1_writel(dev, mask, OWR_INTR_MASK);
152}
153
154/* Waits for completion of a bit transfer, checks intr_status against
155 BIT_XFER_ERRORS and an additional provided bit mask. */
156static inline int w1_wait(struct tegra_device *dev, unsigned long mask)
157{
158 int ret;
159 unsigned long irq_flags;
160 unsigned long intr_status;
161
162 ret = wait_for_completion_timeout(dev->transfer_completion,
163 msecs_to_jiffies(BIT_XFER_COMPLETION_TIMEOUT_MSEC));
164
165 if (unlikely(!ret)) {
166 if (debug_print & DEBUG_TIMEOUT)
167 W1_ERR("timeout\n");
168 return -ETIME;
169 }
170
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);
175
176 if (unlikely(intr_status & BIT_XFER_ERRORS ||
177 !(intr_status & mask)))
178 return -EIO;
179 return 0;
180}
181
182/* Programs timing registers, and puts the device into a known state.
183 Interrupts are safe to enable past this point. */
184static int w1_setup(struct tegra_device *dev)
185{
186 unsigned long value;
187 clk_enable(dev->clk);
188
189 value =
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);
198
199 value =
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);
205
206 /* Clear interrupt status/mask registers in case
207 anything was set in it. */
208 w1_imask(dev, 0);
209 w1_writel(dev, 0xFFFFFFFF, OWR_INTR_STATUS);
210 clk_disable(dev->clk);
211 return 0;
212}
213
214/* Interrupt handler for OWR communication. */
215static irqreturn_t tegra_w1_irq(int irq, void *cookie)
216{
217 unsigned long irq_flags;
218 unsigned long status;
219 struct tegra_device *dev = cookie;
220
221 status = w1_readl(dev, OWR_INTR_STATUS);
222 if (unlikely(!status)) {
223
224 /* Not for me if no status bits are set. */
225 return IRQ_NONE;
226 }
227
228 spin_lock_irqsave(&dev->spinlock, irq_flags);
229
230 if (likely(dev->transfer_completion)) {
231 dev->intr_status = status;
232 w1_writel(dev, status, OWR_INTR_STATUS);
233 complete(dev->transfer_completion);
234 } else {
235 W1_ERR("spurious interrupt, status = 0x%lx\n", status);
236 }
237
238 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
239 return IRQ_HANDLED;
240}
241
242/* Perform a write-0 cycle if bit == 0, otherwise
243 perform a read cycle. */
244static u8 tegra_w1_touch_bit(void *data, u8 bit)
245{
246 int rc;
247 u8 return_bit;
248 unsigned long control;
249 DECLARE_COMPLETION_ONSTACK(touch_done);
250 struct tegra_device *dev = (struct tegra_device *) data;
251
252 return_bit = 0;
253 mutex_lock(&dev->mutex);
254 if (!dev->ready)
255 goto done;
256
257 clk_enable(dev->clk);
258 w1_imask(dev, OI_BIT_XFER_DONE);
259 dev->transfer_completion = &touch_done;
260 control =
261 ((dev->timings->rdsclk & OC_RD_SCLK_MASK) << OC_RD_SCLK_SHIFT) |
262 ((dev->timings->psclk & OC_P_SCLK_MASK) << OC_P_SCLK_SHIFT) |
263 OC_BIT_XMODE;
264
265 /* Read bit (well, writes a 1 to the bus as well). */
266 if (bit) {
267 w1_writel(dev, control | OC_RD_BIT, OWR_CONTROL);
268 rc = w1_wait(dev, OI_BIT_XFER_DONE);
269
270 if (rc) {
271 W1_ERR("write-1/read failed\n");
272 goto done;
273 }
274
275 return_bit =
276 (w1_readl(dev, OWR_STATUS) >> OS_READ_BIT_SHIFT) & 1;
277
278 }
279
280 /* Write 0. */
281 else {
282 w1_writel(dev, control | OC_WR0_BIT, OWR_CONTROL);
283 rc = w1_wait(dev, OI_BIT_XFER_DONE);
284 if (rc) {
285 W1_ERR("write-0 failed\n");
286 goto done;
287 }
288 }
289
290done:
291
292 w1_imask(dev, 0);
293 dev->transfer_completion = NULL;
294 clk_disable(dev->clk);
295 mutex_unlock(&dev->mutex);
296 return return_bit;
297}
298
299/* Performs a bus reset cycle, and returns 0 if slaves present. */
300static u8 tegra_w1_reset_bus(void *data)
301{
302 int rc;
303 int presence;
304 unsigned long value;
305 DECLARE_COMPLETION_ONSTACK(reset_done);
306 struct tegra_device *dev = (struct tegra_device *) data;
307
308 presence = 1;
309 mutex_lock(&dev->mutex);
310 if (!dev->ready)
311 goto done;
312
313 clk_enable(dev->clk);
314 w1_imask(dev, OI_PRESENCE_DONE);
315 dev->transfer_completion = &reset_done;
316 value =
317 ((dev->timings->rdsclk & OC_RD_SCLK_MASK) << OC_RD_SCLK_SHIFT) |
318 ((dev->timings->psclk & OC_P_SCLK_MASK) << OC_P_SCLK_SHIFT) |
319 OC_BIT_XMODE | OC_GO;
320 w1_writel(dev, value, OWR_CONTROL);
321
322 rc = w1_wait(dev, OI_PRESENCE_DONE);
323 if (rc)
324 goto done;
325
326 presence = 0;
327done:
328
329 if (debug_print & DEBUG_PRESENCE) {
330 if (presence)
331 W1_ERR("no slaves present\n");
332 else
333 W1_ERR("slaves present\n");
334 }
335
336 w1_imask(dev, 0);
337 dev->transfer_completion = NULL;
338 clk_disable(dev->clk);
339 mutex_unlock(&dev->mutex);
340 return presence;
341}
342
343static int tegra_w1_probe(struct platform_device *pdev)
344{
345 int rc;
346 int irq;
347 struct resource *res;
348 struct tegra_device *dev;
349 struct tegra_w1_platform_data *plat = pdev->dev.platform_data;
350
351 printk(KERN_INFO "Driver for Tegra SoC 1-wire controller\n");
352
353 if (plat == NULL || plat->timings == NULL)
354 return -ENXIO;
355
356 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
357 if (res == NULL)
358 return -ENODEV;
359
360 irq = res->start;
361
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 if (res == NULL)
364 return -ENODEV;
365
366 dev = kzalloc(sizeof(struct tegra_device), GFP_KERNEL);
367 if (!dev)
368 return -ENOMEM;
369
370 platform_set_drvdata(pdev, dev);
371 dev->clk = clk_get(&pdev->dev, plat->clk_id);
372 if (IS_ERR(dev->clk)) {
373 rc = PTR_ERR(dev->clk);
374 goto cleanup_alloc;
375 }
376
377 /* OWR requires 1MHz clock. */
378 rc = clk_set_rate(dev->clk, OWR_CLOCK);
379 if (rc)
380 goto cleanup_clock;
381
382 if (!request_mem_region
383 (res->start, res->end - res->start + 1, dev_name(&pdev->dev))) {
384 rc = -EBUSY;
385 goto cleanup_clock;
386 }
387
388 dev->ioaddr = ioremap(res->start, res->end - res->start + 1);
389 if (!dev->ioaddr) {
390 rc = -ENOMEM;
391 goto cleanup_reqmem;
392 }
393
394 dev->timings = plat->timings;
395 dev->bus_master.data = dev;
396 dev->bus_master.touch_bit = tegra_w1_touch_bit;
397 dev->bus_master.reset_bus = tegra_w1_reset_bus;
398
399 spin_lock_init(&dev->spinlock);
400 mutex_init(&dev->mutex);
401
402 /* Program device into known state. */
403 w1_setup(dev);
404
405 rc = request_irq(irq, tegra_w1_irq, IRQF_SHARED, DRIVER_NAME, dev);
406 if (rc)
407 goto cleanup_ioremap;
408
409 rc = w1_add_master_device(&dev->bus_master);
410 if (rc)
411 goto cleanup_irq;
412
413 dev->ready = true;
414 return 0;
415
416cleanup_irq:
417 free_irq(irq, dev);
418cleanup_ioremap:
419 iounmap(dev->ioaddr);
420cleanup_reqmem:
421 release_mem_region(res->start,
422 res->end - res->start + 1);
423cleanup_clock:
424 clk_put(dev->clk);
425cleanup_alloc:
426 platform_set_drvdata(pdev, NULL);
427 kfree(dev);
428 return rc;
429}
430
431static int tegra_w1_remove(struct platform_device *pdev)
432{
433 struct resource *res;
434 struct tegra_device *dev = platform_get_drvdata(pdev);
435
436 mutex_lock(&dev->mutex);
437 dev->ready = false;
438 mutex_unlock(&dev->mutex);
439
440 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
441 free_irq(res->start, dev);
442 iounmap(dev->ioaddr);
443 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
444 release_mem_region(res->start, res->end - res->start + 1);
445 clk_put(dev->clk);
446 platform_set_drvdata(pdev, NULL);
447 kfree(dev);
448 return 0;
449}
450
451static int tegra_w1_suspend(struct platform_device *pdev, pm_message_t state)
452{
453 return 0;
454}
455
456static int tegra_w1_resume(struct platform_device *pdev)
457{
458 struct tegra_device *dev = platform_get_drvdata(pdev);
459
460 /* TODO: Is this necessary? I would assume yes. */
461 w1_setup(dev);
462 return 0;
463}
464
465static struct platform_driver tegra_w1_driver = {
466 .probe = tegra_w1_probe,
467 .remove = tegra_w1_remove,
468 .suspend = tegra_w1_suspend,
469 .resume = tegra_w1_resume,
470 .driver = {
471 .name = DRIVER_NAME,
472 .owner = THIS_MODULE,
473 },
474};
475
476static int __init tegra_w1_init(void)
477{
478 return platform_driver_register(&tegra_w1_driver);
479}
480
481static void __exit tegra_w1_exit(void)
482{
483 platform_driver_unregister(&tegra_w1_driver);
484}
485
486module_init(tegra_w1_init);
487module_exit(tegra_w1_exit);
488
489MODULE_DESCRIPTION("Tegra W1 master driver");
490MODULE_AUTHOR("Andrei Warkentin <andreiw@motorola.com>");
491MODULE_LICENSE("GPL");