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> 14 #include <linux/mfd/syscon.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_address.h> 18 #include <linux/platform_device.h> 19 #include <linux/regmap.h> 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 { 30 struct regmap *regmap; 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 41 regmap_write(priv->regmap, offset, mask); 42 43 /* let the reset be effective */ 44 udelay(10); 45 46 return 0; 47 } 48 49 static const 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 { 56 unsigned offset, bit; 57 58 offset = reset_spec->args[0]; 59 bit = reset_spec->args[1]; 60 61 if (bit >= BERLIN_MAX_RESETS) 62 return -EINVAL; 63 64 return (offset << 8) | bit; 65 } 66 67 static int berlin2_reset_probe(struct platform_device *pdev) 68 { 69 struct device_node *parent_np = of_get_parent(pdev->dev.of_node); 70 struct berlin_reset_priv *priv; 71 72 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 73 if (!priv) 74 return -ENOMEM; 75 76 priv->regmap = syscon_node_to_regmap(parent_np); 77 of_node_put(parent_np); 78 if (IS_ERR(priv->regmap)) 79 return PTR_ERR(priv->regmap); 80 81 priv->rcdev.owner = THIS_MODULE; 82 priv->rcdev.ops = &berlin_reset_ops; 83 priv->rcdev.of_node = pdev->dev.of_node; 84 priv->rcdev.of_reset_n_cells = 2; 85 priv->rcdev.of_xlate = berlin_reset_xlate; 86 87 return reset_controller_register(&priv->rcdev); 88 } 89 90 static const struct of_device_id berlin_reset_dt_match[] = { 91 { .compatible = "marvell,berlin2-reset" }, 92 { }, 93 }; 94 MODULE_DEVICE_TABLE(of, berlin_reset_dt_match); 95 96 static struct platform_driver berlin_reset_driver = { 97 .probe = berlin2_reset_probe, 98 .driver = { 99 .name = "berlin2-reset", 100 .of_match_table = berlin_reset_dt_match, 101 }, 102 }; 103 module_platform_driver(berlin_reset_driver); 104 105 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>"); 106 MODULE_AUTHOR("Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>"); 107 MODULE_DESCRIPTION("Marvell Berlin reset driver"); 108 MODULE_LICENSE("GPL"); 109