WIP FPC-III support
[linux/fpc-iii.git] / drivers / reset / reset-bcm6345.c
blob737e4e81f6b70c2d0520182dbba78b704410818c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * BCM6345 Reset Controller Driver
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
6 */
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/io.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/platform_device.h>
13 #include <linux/reset-controller.h>
15 #define BCM6345_RESET_NUM 32
16 #define BCM6345_RESET_SLEEP_MIN_US 10000
17 #define BCM6345_RESET_SLEEP_MAX_US 20000
19 struct bcm6345_reset {
20 struct reset_controller_dev rcdev;
21 void __iomem *base;
22 spinlock_t lock;
25 static inline struct bcm6345_reset *
26 to_bcm6345_reset(struct reset_controller_dev *rcdev)
28 return container_of(rcdev, struct bcm6345_reset, rcdev);
31 static int bcm6345_reset_update(struct reset_controller_dev *rcdev,
32 unsigned long id, bool assert)
34 struct bcm6345_reset *bcm6345_reset = to_bcm6345_reset(rcdev);
35 unsigned long flags;
36 uint32_t val;
38 spin_lock_irqsave(&bcm6345_reset->lock, flags);
39 val = __raw_readl(bcm6345_reset->base);
40 if (assert)
41 val &= ~BIT(id);
42 else
43 val |= BIT(id);
44 __raw_writel(val, bcm6345_reset->base);
45 spin_unlock_irqrestore(&bcm6345_reset->lock, flags);
47 return 0;
50 static int bcm6345_reset_assert(struct reset_controller_dev *rcdev,
51 unsigned long id)
53 return bcm6345_reset_update(rcdev, id, true);
56 static int bcm6345_reset_deassert(struct reset_controller_dev *rcdev,
57 unsigned long id)
59 return bcm6345_reset_update(rcdev, id, false);
62 static int bcm6345_reset_reset(struct reset_controller_dev *rcdev,
63 unsigned long id)
65 bcm6345_reset_update(rcdev, id, true);
66 usleep_range(BCM6345_RESET_SLEEP_MIN_US,
67 BCM6345_RESET_SLEEP_MAX_US);
69 bcm6345_reset_update(rcdev, id, false);
71 * Ensure component is taken out reset state by sleeping also after
72 * deasserting the reset. Otherwise, the component may not be ready
73 * for operation.
75 usleep_range(BCM6345_RESET_SLEEP_MIN_US,
76 BCM6345_RESET_SLEEP_MAX_US);
78 return 0;
81 static int bcm6345_reset_status(struct reset_controller_dev *rcdev,
82 unsigned long id)
84 struct bcm6345_reset *bcm6345_reset = to_bcm6345_reset(rcdev);
86 return !(__raw_readl(bcm6345_reset->base) & BIT(id));
89 static struct reset_control_ops bcm6345_reset_ops = {
90 .assert = bcm6345_reset_assert,
91 .deassert = bcm6345_reset_deassert,
92 .reset = bcm6345_reset_reset,
93 .status = bcm6345_reset_status,
96 static int bcm6345_reset_probe(struct platform_device *pdev)
98 struct bcm6345_reset *bcm6345_reset;
100 bcm6345_reset = devm_kzalloc(&pdev->dev,
101 sizeof(*bcm6345_reset), GFP_KERNEL);
102 if (!bcm6345_reset)
103 return -ENOMEM;
105 platform_set_drvdata(pdev, bcm6345_reset);
107 bcm6345_reset->base = devm_platform_ioremap_resource(pdev, 0);
108 if (IS_ERR(bcm6345_reset->base))
109 return PTR_ERR(bcm6345_reset->base);
111 spin_lock_init(&bcm6345_reset->lock);
112 bcm6345_reset->rcdev.ops = &bcm6345_reset_ops;
113 bcm6345_reset->rcdev.owner = THIS_MODULE;
114 bcm6345_reset->rcdev.of_node = pdev->dev.of_node;
115 bcm6345_reset->rcdev.of_reset_n_cells = 1;
116 bcm6345_reset->rcdev.nr_resets = BCM6345_RESET_NUM;
118 return devm_reset_controller_register(&pdev->dev,
119 &bcm6345_reset->rcdev);
122 static const struct of_device_id bcm6345_reset_of_match[] = {
123 { .compatible = "brcm,bcm6345-reset" },
124 { /* sentinel */ },
127 static struct platform_driver bcm6345_reset_driver = {
128 .probe = bcm6345_reset_probe,
129 .driver = {
130 .name = "bcm6345-reset",
131 .of_match_table = bcm6345_reset_of_match,
132 .suppress_bind_attrs = true,
135 builtin_platform_driver(bcm6345_reset_driver);