1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI SYSCON regmap reset driver
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
7 * Suman Anna <afd@ti.com>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/reset-controller.h>
17 #include <dt-bindings/reset/ti-syscon.h>
20 * struct ti_syscon_reset_control - reset control structure
21 * @assert_offset: reset assert control register offset from syscon base
22 * @assert_bit: reset assert bit in the reset assert control register
23 * @deassert_offset: reset deassert control register offset from syscon base
24 * @deassert_bit: reset deassert bit in the reset deassert control register
25 * @status_offset: reset status register offset from syscon base
26 * @status_bit: reset status bit in the reset status register
27 * @flags: reset flag indicating how the (de)assert and status are handled
29 struct ti_syscon_reset_control
{
30 unsigned int assert_offset
;
31 unsigned int assert_bit
;
32 unsigned int deassert_offset
;
33 unsigned int deassert_bit
;
34 unsigned int status_offset
;
35 unsigned int status_bit
;
40 * struct ti_syscon_reset_data - reset controller information structure
41 * @rcdev: reset controller entity
42 * @regmap: regmap handle containing the memory-mapped reset registers
43 * @controls: array of reset controls
44 * @nr_controls: number of controls in control array
46 struct ti_syscon_reset_data
{
47 struct reset_controller_dev rcdev
;
48 struct regmap
*regmap
;
49 struct ti_syscon_reset_control
*controls
;
50 unsigned int nr_controls
;
53 #define to_ti_syscon_reset_data(_rcdev) \
54 container_of(_rcdev, struct ti_syscon_reset_data, rcdev)
57 * ti_syscon_reset_assert() - assert device reset
58 * @rcdev: reset controller entity
59 * @id: ID of the reset to be asserted
61 * This function implements the reset driver op to assert a device's reset.
62 * This asserts the reset in a manner prescribed by the reset flags.
64 * Return: 0 for successful request, else a corresponding error value
66 static int ti_syscon_reset_assert(struct reset_controller_dev
*rcdev
,
69 struct ti_syscon_reset_data
*data
= to_ti_syscon_reset_data(rcdev
);
70 struct ti_syscon_reset_control
*control
;
71 unsigned int mask
, value
;
73 if (id
>= data
->nr_controls
)
76 control
= &data
->controls
[id
];
78 if (control
->flags
& ASSERT_NONE
)
79 return -ENOTSUPP
; /* assert not supported for this reset */
81 mask
= BIT(control
->assert_bit
);
82 value
= (control
->flags
& ASSERT_SET
) ? mask
: 0x0;
84 return regmap_write_bits(data
->regmap
, control
->assert_offset
, mask
, value
);
88 * ti_syscon_reset_deassert() - deassert device reset
89 * @rcdev: reset controller entity
90 * @id: ID of reset to be deasserted
92 * This function implements the reset driver op to deassert a device's reset.
93 * This deasserts the reset in a manner prescribed by the reset flags.
95 * Return: 0 for successful request, else a corresponding error value
97 static int ti_syscon_reset_deassert(struct reset_controller_dev
*rcdev
,
100 struct ti_syscon_reset_data
*data
= to_ti_syscon_reset_data(rcdev
);
101 struct ti_syscon_reset_control
*control
;
102 unsigned int mask
, value
;
104 if (id
>= data
->nr_controls
)
107 control
= &data
->controls
[id
];
109 if (control
->flags
& DEASSERT_NONE
)
110 return -ENOTSUPP
; /* deassert not supported for this reset */
112 mask
= BIT(control
->deassert_bit
);
113 value
= (control
->flags
& DEASSERT_SET
) ? mask
: 0x0;
115 return regmap_write_bits(data
->regmap
, control
->deassert_offset
, mask
, value
);
119 * ti_syscon_reset_status() - check device reset status
120 * @rcdev: reset controller entity
121 * @id: ID of the reset for which the status is being requested
123 * This function implements the reset driver op to return the status of a
126 * Return: 0 if reset is deasserted, true if reset is asserted, else a
127 * corresponding error value
129 static int ti_syscon_reset_status(struct reset_controller_dev
*rcdev
,
132 struct ti_syscon_reset_data
*data
= to_ti_syscon_reset_data(rcdev
);
133 struct ti_syscon_reset_control
*control
;
134 unsigned int reset_state
;
137 if (id
>= data
->nr_controls
)
140 control
= &data
->controls
[id
];
142 if (control
->flags
& STATUS_NONE
)
143 return -ENOTSUPP
; /* status not supported for this reset */
145 ret
= regmap_read(data
->regmap
, control
->status_offset
, &reset_state
);
149 return !(reset_state
& BIT(control
->status_bit
)) ==
150 !(control
->flags
& STATUS_SET
);
153 static const struct reset_control_ops ti_syscon_reset_ops
= {
154 .assert = ti_syscon_reset_assert
,
155 .deassert
= ti_syscon_reset_deassert
,
156 .status
= ti_syscon_reset_status
,
159 static int ti_syscon_reset_probe(struct platform_device
*pdev
)
161 struct device
*dev
= &pdev
->dev
;
162 struct device_node
*np
= dev
->of_node
;
163 struct ti_syscon_reset_data
*data
;
164 struct regmap
*regmap
;
166 struct ti_syscon_reset_control
*controls
;
167 int size
, nr_controls
, i
;
169 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
173 regmap
= syscon_node_to_regmap(np
->parent
);
175 return PTR_ERR(regmap
);
177 list
= of_get_property(np
, "ti,reset-bits", &size
);
178 if (!list
|| (size
/ sizeof(*list
)) % 7 != 0) {
179 dev_err(dev
, "invalid DT reset description\n");
183 nr_controls
= (size
/ sizeof(*list
)) / 7;
184 controls
= devm_kcalloc(dev
, nr_controls
, sizeof(*controls
),
189 for (i
= 0; i
< nr_controls
; i
++) {
190 controls
[i
].assert_offset
= be32_to_cpup(list
++);
191 controls
[i
].assert_bit
= be32_to_cpup(list
++);
192 controls
[i
].deassert_offset
= be32_to_cpup(list
++);
193 controls
[i
].deassert_bit
= be32_to_cpup(list
++);
194 controls
[i
].status_offset
= be32_to_cpup(list
++);
195 controls
[i
].status_bit
= be32_to_cpup(list
++);
196 controls
[i
].flags
= be32_to_cpup(list
++);
199 data
->rcdev
.ops
= &ti_syscon_reset_ops
;
200 data
->rcdev
.owner
= THIS_MODULE
;
201 data
->rcdev
.of_node
= np
;
202 data
->rcdev
.nr_resets
= nr_controls
;
203 data
->regmap
= regmap
;
204 data
->controls
= controls
;
205 data
->nr_controls
= nr_controls
;
207 return devm_reset_controller_register(dev
, &data
->rcdev
);
210 static const struct of_device_id ti_syscon_reset_of_match
[] = {
211 { .compatible
= "ti,syscon-reset", },
214 MODULE_DEVICE_TABLE(of
, ti_syscon_reset_of_match
);
216 static struct platform_driver ti_syscon_reset_driver
= {
217 .probe
= ti_syscon_reset_probe
,
219 .name
= "ti-syscon-reset",
220 .of_match_table
= ti_syscon_reset_of_match
,
223 module_platform_driver(ti_syscon_reset_driver
);
225 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
226 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
227 MODULE_DESCRIPTION("TI SYSCON Regmap Reset Driver");
228 MODULE_LICENSE("GPL v2");