0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/init.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regmap.h>
0014 #include <linux/reset-controller.h>
0015 #include <linux/slab.h>
0016 #include <linux/mfd/syscon.h>
0017
0018 #include <dt-bindings/reset/pistachio-resets.h>
0019
0020 #define PISTACHIO_SOFT_RESET 0
0021
0022 struct pistachio_reset_data {
0023 struct reset_controller_dev rcdev;
0024 struct regmap *periph_regs;
0025 };
0026
0027 static inline int pistachio_reset_shift(unsigned long id)
0028 {
0029 switch (id) {
0030 case PISTACHIO_RESET_I2C0:
0031 case PISTACHIO_RESET_I2C1:
0032 case PISTACHIO_RESET_I2C2:
0033 case PISTACHIO_RESET_I2C3:
0034 case PISTACHIO_RESET_I2S_IN:
0035 case PISTACHIO_RESET_PRL_OUT:
0036 case PISTACHIO_RESET_SPDIF_OUT:
0037 case PISTACHIO_RESET_SPI:
0038 case PISTACHIO_RESET_PWM_PDM:
0039 case PISTACHIO_RESET_UART0:
0040 case PISTACHIO_RESET_UART1:
0041 case PISTACHIO_RESET_QSPI:
0042 case PISTACHIO_RESET_MDC:
0043 case PISTACHIO_RESET_SDHOST:
0044 case PISTACHIO_RESET_ETHERNET:
0045 case PISTACHIO_RESET_IR:
0046 case PISTACHIO_RESET_HASH:
0047 case PISTACHIO_RESET_TIMER:
0048 return id;
0049 case PISTACHIO_RESET_I2S_OUT:
0050 case PISTACHIO_RESET_SPDIF_IN:
0051 case PISTACHIO_RESET_EVT:
0052 return id + 6;
0053 case PISTACHIO_RESET_USB_H:
0054 case PISTACHIO_RESET_USB_PR:
0055 case PISTACHIO_RESET_USB_PHY_PR:
0056 case PISTACHIO_RESET_USB_PHY_PON:
0057 return id + 7;
0058 default:
0059 return -EINVAL;
0060 }
0061 }
0062
0063 static int pistachio_reset_assert(struct reset_controller_dev *rcdev,
0064 unsigned long id)
0065 {
0066 struct pistachio_reset_data *rd;
0067 u32 mask;
0068 int shift;
0069
0070 rd = container_of(rcdev, struct pistachio_reset_data, rcdev);
0071 shift = pistachio_reset_shift(id);
0072 if (shift < 0)
0073 return shift;
0074 mask = BIT(shift);
0075
0076 return regmap_update_bits(rd->periph_regs, PISTACHIO_SOFT_RESET,
0077 mask, mask);
0078 }
0079
0080 static int pistachio_reset_deassert(struct reset_controller_dev *rcdev,
0081 unsigned long id)
0082 {
0083 struct pistachio_reset_data *rd;
0084 u32 mask;
0085 int shift;
0086
0087 rd = container_of(rcdev, struct pistachio_reset_data, rcdev);
0088 shift = pistachio_reset_shift(id);
0089 if (shift < 0)
0090 return shift;
0091 mask = BIT(shift);
0092
0093 return regmap_update_bits(rd->periph_regs, PISTACHIO_SOFT_RESET,
0094 mask, 0);
0095 }
0096
0097 static const struct reset_control_ops pistachio_reset_ops = {
0098 .assert = pistachio_reset_assert,
0099 .deassert = pistachio_reset_deassert,
0100 };
0101
0102 static int pistachio_reset_probe(struct platform_device *pdev)
0103 {
0104 struct pistachio_reset_data *rd;
0105 struct device *dev = &pdev->dev;
0106 struct device_node *np = pdev->dev.of_node;
0107
0108 rd = devm_kzalloc(dev, sizeof(*rd), GFP_KERNEL);
0109 if (!rd)
0110 return -ENOMEM;
0111
0112 rd->periph_regs = syscon_node_to_regmap(np->parent);
0113 if (IS_ERR(rd->periph_regs))
0114 return PTR_ERR(rd->periph_regs);
0115
0116 rd->rcdev.owner = THIS_MODULE;
0117 rd->rcdev.nr_resets = PISTACHIO_RESET_MAX + 1;
0118 rd->rcdev.ops = &pistachio_reset_ops;
0119 rd->rcdev.of_node = np;
0120
0121 return devm_reset_controller_register(dev, &rd->rcdev);
0122 }
0123
0124 static const struct of_device_id pistachio_reset_dt_ids[] = {
0125 { .compatible = "img,pistachio-reset", },
0126 { },
0127 };
0128
0129 static struct platform_driver pistachio_reset_driver = {
0130 .probe = pistachio_reset_probe,
0131 .driver = {
0132 .name = "pistachio-reset",
0133 .of_match_table = pistachio_reset_dt_ids,
0134 },
0135 };
0136 builtin_platform_driver(pistachio_reset_driver);