1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BCM6345 Reset Controller Driver
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
8 #include <linux/delay.h>
9 #include <linux/init.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
;
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
);
38 spin_lock_irqsave(&bcm6345_reset
->lock
, flags
);
39 val
= __raw_readl(bcm6345_reset
->base
);
44 __raw_writel(val
, bcm6345_reset
->base
);
45 spin_unlock_irqrestore(&bcm6345_reset
->lock
, flags
);
50 static int bcm6345_reset_assert(struct reset_controller_dev
*rcdev
,
53 return bcm6345_reset_update(rcdev
, id
, true);
56 static int bcm6345_reset_deassert(struct reset_controller_dev
*rcdev
,
59 return bcm6345_reset_update(rcdev
, id
, false);
62 static int bcm6345_reset_reset(struct reset_controller_dev
*rcdev
,
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
75 usleep_range(BCM6345_RESET_SLEEP_MIN_US
,
76 BCM6345_RESET_SLEEP_MAX_US
);
81 static int bcm6345_reset_status(struct reset_controller_dev
*rcdev
,
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
);
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" },
127 static struct platform_driver bcm6345_reset_driver
= {
128 .probe
= bcm6345_reset_probe
,
130 .name
= "bcm6345-reset",
131 .of_match_table
= bcm6345_reset_of_match
,
132 .suppress_bind_attrs
= true,
135 builtin_platform_driver(bcm6345_reset_driver
);