1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
6 #include <linux/mfd/syscon.h>
7 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/reset-controller.h>
12 #include <linux/slab.h>
17 struct regmap
*regmap
;
19 struct reset_controller_dev rcdev
;
22 static int mtk_reset_assert_set_clr(struct reset_controller_dev
*rcdev
,
25 struct mtk_reset
*data
= container_of(rcdev
, struct mtk_reset
, rcdev
);
26 unsigned int reg
= data
->regofs
+ ((id
/ 32) << 4);
28 return regmap_write(data
->regmap
, reg
, 1);
31 static int mtk_reset_deassert_set_clr(struct reset_controller_dev
*rcdev
,
34 struct mtk_reset
*data
= container_of(rcdev
, struct mtk_reset
, rcdev
);
35 unsigned int reg
= data
->regofs
+ ((id
/ 32) << 4) + 0x4;
37 return regmap_write(data
->regmap
, reg
, 1);
40 static int mtk_reset_assert(struct reset_controller_dev
*rcdev
,
43 struct mtk_reset
*data
= container_of(rcdev
, struct mtk_reset
, rcdev
);
45 return regmap_update_bits(data
->regmap
, data
->regofs
+ ((id
/ 32) << 2),
49 static int mtk_reset_deassert(struct reset_controller_dev
*rcdev
,
52 struct mtk_reset
*data
= container_of(rcdev
, struct mtk_reset
, rcdev
);
54 return regmap_update_bits(data
->regmap
, data
->regofs
+ ((id
/ 32) << 2),
58 static int mtk_reset(struct reset_controller_dev
*rcdev
,
63 ret
= mtk_reset_assert(rcdev
, id
);
67 return mtk_reset_deassert(rcdev
, id
);
70 static int mtk_reset_set_clr(struct reset_controller_dev
*rcdev
,
75 ret
= mtk_reset_assert_set_clr(rcdev
, id
);
78 return mtk_reset_deassert_set_clr(rcdev
, id
);
81 static const struct reset_control_ops mtk_reset_ops
= {
82 .assert = mtk_reset_assert
,
83 .deassert
= mtk_reset_deassert
,
87 static const struct reset_control_ops mtk_reset_ops_set_clr
= {
88 .assert = mtk_reset_assert_set_clr
,
89 .deassert
= mtk_reset_deassert_set_clr
,
90 .reset
= mtk_reset_set_clr
,
93 static void mtk_register_reset_controller_common(struct device_node
*np
,
94 unsigned int num_regs
, int regofs
,
95 const struct reset_control_ops
*reset_ops
)
97 struct mtk_reset
*data
;
99 struct regmap
*regmap
;
101 regmap
= syscon_node_to_regmap(np
);
102 if (IS_ERR(regmap
)) {
103 pr_err("Cannot find regmap for %pOF: %ld\n", np
,
108 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
112 data
->regmap
= regmap
;
113 data
->regofs
= regofs
;
114 data
->rcdev
.owner
= THIS_MODULE
;
115 data
->rcdev
.nr_resets
= num_regs
* 32;
116 data
->rcdev
.ops
= reset_ops
;
117 data
->rcdev
.of_node
= np
;
119 ret
= reset_controller_register(&data
->rcdev
);
121 pr_err("could not register reset controller: %d\n", ret
);
127 void mtk_register_reset_controller(struct device_node
*np
,
128 unsigned int num_regs
, int regofs
)
130 mtk_register_reset_controller_common(np
, num_regs
, regofs
,
134 void mtk_register_reset_controller_set_clr(struct device_node
*np
,
135 unsigned int num_regs
, int regofs
)
137 mtk_register_reset_controller_common(np
, num_regs
, regofs
,
138 &mtk_reset_ops_set_clr
);