Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Marvell Technology Group Ltd. |
| 3 | * |
| 4 | * Antoine Tenart <antoine.tenart@free-electrons.com> |
| 5 | * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> |
| 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public |
| 8 | * License version 2. This program is licensed "as is" without any |
| 9 | * warranty of any kind, whether express or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/io.h> |
Antoine Tenart | aed6f3c | 2015-05-16 00:41:25 +0200 | [diff] [blame] | 14 | #include <linux/mfd/syscon.h> |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_address.h> |
| 18 | #include <linux/platform_device.h> |
Antoine Tenart | aed6f3c | 2015-05-16 00:41:25 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 20 | #include <linux/reset-controller.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/types.h> |
| 23 | |
| 24 | #define BERLIN_MAX_RESETS 32 |
| 25 | |
| 26 | #define to_berlin_reset_priv(p) \ |
| 27 | container_of((p), struct berlin_reset_priv, rcdev) |
| 28 | |
| 29 | struct berlin_reset_priv { |
Antoine Tenart | aed6f3c | 2015-05-16 00:41:25 +0200 | [diff] [blame] | 30 | struct regmap *regmap; |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 31 | struct reset_controller_dev rcdev; |
| 32 | }; |
| 33 | |
| 34 | static int berlin_reset_reset(struct reset_controller_dev *rcdev, |
| 35 | unsigned long id) |
| 36 | { |
| 37 | struct berlin_reset_priv *priv = to_berlin_reset_priv(rcdev); |
| 38 | int offset = id >> 8; |
| 39 | int mask = BIT(id & 0x1f); |
| 40 | |
Antoine Tenart | bfff629 | 2015-05-16 00:53:44 +0200 | [diff] [blame] | 41 | regmap_write(priv->regmap, offset, mask); |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 42 | |
| 43 | /* let the reset be effective */ |
| 44 | udelay(10); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static struct reset_control_ops berlin_reset_ops = { |
| 50 | .reset = berlin_reset_reset, |
| 51 | }; |
| 52 | |
| 53 | static int berlin_reset_xlate(struct reset_controller_dev *rcdev, |
| 54 | const struct of_phandle_args *reset_spec) |
| 55 | { |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 56 | unsigned offset, bit; |
| 57 | |
| 58 | if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells)) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | offset = reset_spec->args[0]; |
| 62 | bit = reset_spec->args[1]; |
| 63 | |
Antoine Ténart | bd13251 | 2014-09-03 09:48:20 +0200 | [diff] [blame] | 64 | if (bit >= BERLIN_MAX_RESETS) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | return (offset << 8) | bit; |
| 68 | } |
| 69 | |
Antoine Tenart | aed6f3c | 2015-05-16 00:41:25 +0200 | [diff] [blame] | 70 | static int berlin2_reset_probe(struct platform_device *pdev) |
| 71 | { |
| 72 | struct device_node *parent_np = of_get_parent(pdev->dev.of_node); |
| 73 | struct berlin_reset_priv *priv; |
| 74 | |
| 75 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 76 | if (!priv) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | priv->regmap = syscon_node_to_regmap(parent_np); |
| 80 | of_node_put(parent_np); |
| 81 | if (IS_ERR(priv->regmap)) |
| 82 | return PTR_ERR(priv->regmap); |
| 83 | |
| 84 | priv->rcdev.owner = THIS_MODULE; |
| 85 | priv->rcdev.ops = &berlin_reset_ops; |
| 86 | priv->rcdev.of_node = pdev->dev.of_node; |
| 87 | priv->rcdev.of_reset_n_cells = 2; |
| 88 | priv->rcdev.of_xlate = berlin_reset_xlate; |
| 89 | |
| 90 | reset_controller_register(&priv->rcdev); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static const struct of_device_id berlin_reset_dt_match[] = { |
| 96 | { .compatible = "marvell,berlin2-reset" }, |
| 97 | { }, |
| 98 | }; |
| 99 | MODULE_DEVICE_TABLE(of, berlin_reset_dt_match); |
| 100 | |
| 101 | static struct platform_driver berlin_reset_driver = { |
| 102 | .probe = berlin2_reset_probe, |
| 103 | .driver = { |
| 104 | .name = "berlin2-reset", |
| 105 | .of_match_table = berlin_reset_dt_match, |
| 106 | }, |
| 107 | }; |
| 108 | module_platform_driver(berlin_reset_driver); |
| 109 | |
| 110 | MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>"); |
| 111 | MODULE_AUTHOR("Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>"); |
| 112 | MODULE_DESCRIPTION("Marvell Berlin reset driver"); |
| 113 | MODULE_LICENSE("GPL"); |