2 * Copyright (C) 2013 STMicroelectronics Limited
3 * Author: Stephen Gallimore <stephen.gallimore@st.com>
5 * Inspired by mach-imx/src.c
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/types.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 #include <linux/mfd/syscon.h>
21 #include "reset-syscfg.h"
24 * Reset channel regmap configuration
26 * @reset: regmap field for the channel's reset bit.
27 * @ack: regmap field for the channel's ack bit (optional).
29 struct syscfg_reset_channel {
30 struct regmap_field *reset;
31 struct regmap_field *ack;
35 * A reset controller which groups together a set of related reset bits, which
36 * may be located in different system configuration registers.
38 * @rst: base reset controller structure.
39 * @active_low: are the resets in this controller active low, i.e. clearing
40 * the reset bit puts the hardware into reset.
41 * @channels: An array of reset channels for this controller.
43 struct syscfg_reset_controller {
44 struct reset_controller_dev rst;
46 struct syscfg_reset_channel *channels;
49 #define to_syscfg_reset_controller(_rst) \
50 container_of(_rst, struct syscfg_reset_controller, rst)
52 static int syscfg_reset_program_hw(struct reset_controller_dev *rcdev,
53 unsigned long idx, int assert)
55 struct syscfg_reset_controller *rst = to_syscfg_reset_controller(rcdev);
56 const struct syscfg_reset_channel *ch;
57 u32 ctrl_val = rst->active_low ? !assert : !!assert;
60 if (idx >= rcdev->nr_resets)
63 ch = &rst->channels[idx];
65 err = regmap_field_write(ch->reset, ctrl_val);
70 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
74 err = regmap_field_read(ch->ack, &ack_val);
78 if (ack_val == ctrl_val)
81 if (time_after(jiffies, timeout))
91 static int syscfg_reset_assert(struct reset_controller_dev *rcdev,
94 return syscfg_reset_program_hw(rcdev, idx, true);
97 static int syscfg_reset_deassert(struct reset_controller_dev *rcdev,
100 return syscfg_reset_program_hw(rcdev, idx, false);
103 static int syscfg_reset_dev(struct reset_controller_dev *rcdev,
106 int err = syscfg_reset_assert(rcdev, idx);
110 return syscfg_reset_deassert(rcdev, idx);
113 static struct reset_control_ops syscfg_reset_ops = {
114 .reset = syscfg_reset_dev,
115 .assert = syscfg_reset_assert,
116 .deassert = syscfg_reset_deassert,
119 static int syscfg_reset_controller_register(struct device *dev,
120 const struct syscfg_reset_controller_data *data)
122 struct syscfg_reset_controller *rc;
126 rc = devm_kzalloc(dev, sizeof(*rc), GFP_KERNEL);
130 size = sizeof(struct syscfg_reset_channel) * data->nr_channels;
132 rc->channels = devm_kzalloc(dev, size, GFP_KERNEL);
136 rc->rst.ops = &syscfg_reset_ops,
137 rc->rst.of_node = dev->of_node;
138 rc->rst.nr_resets = data->nr_channels;
139 rc->active_low = data->active_low;
141 for (i = 0; i < data->nr_channels; i++) {
143 struct regmap_field *f;
144 const char *compatible = data->channels[i].compatible;
146 map = syscon_regmap_lookup_by_compatible(compatible);
150 f = devm_regmap_field_alloc(dev, map, data->channels[i].reset);
154 rc->channels[i].reset = f;
156 if (!data->wait_for_ack)
159 f = devm_regmap_field_alloc(dev, map, data->channels[i].ack);
163 rc->channels[i].ack = f;
166 err = reset_controller_register(&rc->rst);
168 dev_info(dev, "registered\n");
173 int syscfg_reset_probe(struct platform_device *pdev)
175 struct device *dev = pdev ? &pdev->dev : NULL;
176 const struct of_device_id *match;
178 if (!dev || !dev->driver)
181 match = of_match_device(dev->driver->of_match_table, dev);
182 if (!match || !match->data)
185 return syscfg_reset_controller_register(dev, match->data);