temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / w1 / masters / tegra_w1.c
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
108 struct 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
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");
133
134 /* Reads the OWR register specified by base offset in 'reg'. */
135 static 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'. */
142 static 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. */
149 static 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. */
156 static 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. */
184 static 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. */
215 static 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. */
244 static 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_unlock;
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 %d\n", rc);
286                         goto done;
287                 }
288         }
289
290 done:
291
292         w1_imask(dev, 0);
293         dev->transfer_completion = NULL;
294         clk_disable(dev->clk);
295 done_unlock:
296         mutex_unlock(&dev->mutex);
297         return return_bit;
298 }
299
300 /* Performs a bus reset cycle, and returns 0 if slaves present. */
301 static u8 tegra_w1_reset_bus(void *data)
302 {
303         int rc;
304         int presence;
305         unsigned long value;
306         DECLARE_COMPLETION_ONSTACK(reset_done);
307         struct tegra_device *dev = (struct tegra_device *) data;
308
309         presence = 1;
310         mutex_lock(&dev->mutex);
311         if (!dev->ready)
312                 goto not_ready;
313
314         clk_enable(dev->clk);
315         w1_imask(dev, OI_PRESENCE_DONE);
316         dev->transfer_completion = &reset_done;
317         value =
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);
322
323         rc = w1_wait(dev, OI_PRESENCE_DONE);
324         if (rc)
325                 goto done;
326
327         presence = 0;
328 done:
329
330         if (debug_print & DEBUG_PRESENCE) {
331                 if (presence)
332                         W1_ERR("no slaves present\n");
333                 else
334                         W1_ERR("slaves present\n");
335         }
336
337         w1_imask(dev, 0);
338         dev->transfer_completion = NULL;
339         clk_disable(dev->clk);
340 not_ready:
341         mutex_unlock(&dev->mutex);
342         return presence;
343 }
344
345 static int tegra_w1_probe(struct platform_device *pdev)
346 {
347         int rc;
348         int irq;
349         struct resource *res;
350         struct tegra_device *dev;
351         struct tegra_w1_platform_data *plat = pdev->dev.platform_data;
352
353         printk(KERN_INFO "Driver for Tegra SoC 1-wire controller\n");
354
355         if (plat == NULL || plat->timings == NULL)
356                 return -ENXIO;
357
358         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
359         if (res == NULL)
360                 return -ENODEV;
361
362         irq = res->start;
363
364         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365         if (res == NULL)
366                 return -ENODEV;
367
368         dev = kzalloc(sizeof(struct tegra_device), GFP_KERNEL);
369         if (!dev)
370                 return -ENOMEM;
371
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);
376                 goto cleanup_alloc;
377         }
378
379         /* OWR requires 1MHz clock. */
380         rc = clk_set_rate(dev->clk, OWR_CLOCK);
381         if (rc)
382                 goto cleanup_clock;
383
384         if (!request_mem_region
385             (res->start, res->end - res->start + 1, dev_name(&pdev->dev))) {
386                 rc = -EBUSY;
387                 goto cleanup_clock;
388         }
389
390         dev->ioaddr = ioremap(res->start, res->end - res->start + 1);
391         if (!dev->ioaddr) {
392                 rc = -ENOMEM;
393                 goto cleanup_reqmem;
394         }
395
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;
400
401         spin_lock_init(&dev->spinlock);
402         mutex_init(&dev->mutex);
403
404         /* Program device into known state. */
405         w1_setup(dev);
406
407         rc = request_irq(irq, tegra_w1_irq, IRQF_SHARED, DRIVER_NAME, dev);
408         if (rc)
409                 goto cleanup_ioremap;
410
411         rc = w1_add_master_device(&dev->bus_master);
412         if (rc)
413                 goto cleanup_irq;
414
415         dev->ready = true;
416         return 0;
417
418 cleanup_irq:
419         free_irq(irq, dev);
420 cleanup_ioremap:
421         iounmap(dev->ioaddr);
422 cleanup_reqmem:
423         release_mem_region(res->start,
424                            res->end - res->start + 1);
425 cleanup_clock:
426         clk_put(dev->clk);
427 cleanup_alloc:
428         platform_set_drvdata(pdev, NULL);
429         kfree(dev);
430         return rc;
431 }
432
433 static int tegra_w1_remove(struct platform_device *pdev)
434 {
435         struct resource *res;
436         struct tegra_device *dev = platform_get_drvdata(pdev);
437
438         mutex_lock(&dev->mutex);
439         dev->ready = false;
440         mutex_unlock(&dev->mutex);
441
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);
447         clk_put(dev->clk);
448         platform_set_drvdata(pdev, NULL);
449         kfree(dev);
450         return 0;
451 }
452
453 static int tegra_w1_suspend(struct platform_device *pdev, pm_message_t state)
454 {
455         struct tegra_device *dev = platform_get_drvdata(pdev);
456         mutex_lock(&dev->mutex);
457         dev->ready = false;
458         mutex_unlock(&dev->mutex);
459         return 0;
460 }
461
462 static int tegra_w1_resume(struct platform_device *pdev)
463 {
464         struct tegra_device *dev = platform_get_drvdata(pdev);
465
466         /* TODO: Is this necessary? I would assume yes. */
467         mutex_lock(&dev->mutex);
468         w1_setup(dev);
469         dev->ready = true;
470         mutex_unlock(&dev->mutex);
471         return 0;
472 }
473
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,
479         .driver = {
480                    .name = DRIVER_NAME,
481                    .owner = THIS_MODULE,
482         },
483 };
484
485 static int __init tegra_w1_init(void)
486 {
487         return platform_driver_register(&tegra_w1_driver);
488 }
489
490 static void __exit tegra_w1_exit(void)
491 {
492         platform_driver_unregister(&tegra_w1_driver);
493 }
494
495 module_init(tegra_w1_init);
496 module_exit(tegra_w1_exit);
497
498 MODULE_DESCRIPTION("Tegra W1 master driver");
499 MODULE_AUTHOR("Andrei Warkentin <andreiw@motorola.com>");
500 MODULE_LICENSE("GPL");