2 * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/reset-controller.h>
18 #include <linux/reboot.h>
21 struct reset_controller_dev rcdev
;
22 struct notifier_block restart_nb
;
27 #define FULL_CHIP_RESET 24
29 static int ath79_reset_update(struct reset_controller_dev
*rcdev
,
30 unsigned long id
, bool assert)
32 struct ath79_reset
*ath79_reset
=
33 container_of(rcdev
, struct ath79_reset
, rcdev
);
37 spin_lock_irqsave(&ath79_reset
->lock
, flags
);
38 val
= readl(ath79_reset
->base
);
43 writel(val
, ath79_reset
->base
);
44 spin_unlock_irqrestore(&ath79_reset
->lock
, flags
);
49 static int ath79_reset_assert(struct reset_controller_dev
*rcdev
,
52 return ath79_reset_update(rcdev
, id
, true);
55 static int ath79_reset_deassert(struct reset_controller_dev
*rcdev
,
58 return ath79_reset_update(rcdev
, id
, false);
61 static int ath79_reset_status(struct reset_controller_dev
*rcdev
,
64 struct ath79_reset
*ath79_reset
=
65 container_of(rcdev
, struct ath79_reset
, rcdev
);
68 val
= readl(ath79_reset
->base
);
70 return !!(val
& BIT(id
));
73 static const struct reset_control_ops ath79_reset_ops
= {
74 .assert = ath79_reset_assert
,
75 .deassert
= ath79_reset_deassert
,
76 .status
= ath79_reset_status
,
79 static int ath79_reset_restart_handler(struct notifier_block
*nb
,
80 unsigned long action
, void *data
)
82 struct ath79_reset
*ath79_reset
=
83 container_of(nb
, struct ath79_reset
, restart_nb
);
85 ath79_reset_assert(&ath79_reset
->rcdev
, FULL_CHIP_RESET
);
90 static int ath79_reset_probe(struct platform_device
*pdev
)
92 struct ath79_reset
*ath79_reset
;
96 ath79_reset
= devm_kzalloc(&pdev
->dev
,
97 sizeof(*ath79_reset
), GFP_KERNEL
);
101 platform_set_drvdata(pdev
, ath79_reset
);
103 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
104 ath79_reset
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
105 if (IS_ERR(ath79_reset
->base
))
106 return PTR_ERR(ath79_reset
->base
);
108 spin_lock_init(&ath79_reset
->lock
);
109 ath79_reset
->rcdev
.ops
= &ath79_reset_ops
;
110 ath79_reset
->rcdev
.owner
= THIS_MODULE
;
111 ath79_reset
->rcdev
.of_node
= pdev
->dev
.of_node
;
112 ath79_reset
->rcdev
.of_reset_n_cells
= 1;
113 ath79_reset
->rcdev
.nr_resets
= 32;
115 err
= reset_controller_register(&ath79_reset
->rcdev
);
119 ath79_reset
->restart_nb
.notifier_call
= ath79_reset_restart_handler
;
120 ath79_reset
->restart_nb
.priority
= 128;
122 err
= register_restart_handler(&ath79_reset
->restart_nb
);
124 dev_warn(&pdev
->dev
, "Failed to register restart handler\n");
129 static int ath79_reset_remove(struct platform_device
*pdev
)
131 struct ath79_reset
*ath79_reset
= platform_get_drvdata(pdev
);
133 unregister_restart_handler(&ath79_reset
->restart_nb
);
134 reset_controller_unregister(&ath79_reset
->rcdev
);
139 static const struct of_device_id ath79_reset_dt_ids
[] = {
140 { .compatible
= "qca,ar7100-reset", },
143 MODULE_DEVICE_TABLE(of
, ath79_reset_dt_ids
);
145 static struct platform_driver ath79_reset_driver
= {
146 .probe
= ath79_reset_probe
,
147 .remove
= ath79_reset_remove
,
149 .name
= "ath79-reset",
150 .of_match_table
= ath79_reset_dt_ids
,
153 module_platform_driver(ath79_reset_driver
);
155 MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
156 MODULE_DESCRIPTION("AR71xx Reset Controller Driver");
157 MODULE_LICENSE("GPL");