1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * MMIO register bitfield-controlled multiplexer driver 4 * 5 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de> 6 */ 7 8 #include <linux/bitops.h> 9 #include <linux/err.h> 10 #include <linux/mfd/syscon.h> 11 #include <linux/module.h> 12 #include <linux/mux/driver.h> 13 #include <linux/of.h> 14 #include <linux/platform_device.h> 15 #include <linux/property.h> 16 #include <linux/regmap.h> 17 18 struct mux_mmio { 19 struct regmap_field **fields; 20 unsigned int *hardware_states; 21 }; 22 23 static int mux_mmio_get(struct mux_control *mux, int *state) 24 { 25 struct mux_mmio *mux_mmio = mux_chip_priv(mux->chip); 26 unsigned int index = mux_control_get_index(mux); 27 28 return regmap_field_read(mux_mmio->fields[index], state); 29 } 30 31 static int mux_mmio_set(struct mux_control *mux, int state) 32 { 33 struct mux_mmio *mux_mmio = mux_chip_priv(mux->chip); 34 unsigned int index = mux_control_get_index(mux); 35 36 return regmap_field_write(mux_mmio->fields[index], state); 37 } 38 39 static const struct mux_control_ops mux_mmio_ops = { 40 .set = mux_mmio_set, 41 }; 42 43 static const struct of_device_id mux_mmio_dt_ids[] = { 44 { .compatible = "mmio-mux", }, 45 { .compatible = "reg-mux", }, 46 { /* sentinel */ } 47 }; 48 MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids); 49 50 static const struct regmap_config mux_mmio_regmap_cfg = { 51 .reg_bits = 32, 52 .val_bits = 32, 53 .reg_stride = 4, 54 }; 55 56 static int mux_mmio_probe(struct platform_device *pdev) 57 { 58 struct device *dev = &pdev->dev; 59 struct device_node *np = dev->of_node; 60 struct mux_chip *mux_chip; 61 struct mux_mmio *mux_mmio; 62 struct regmap *regmap; 63 void __iomem *base; 64 int num_fields; 65 int ret; 66 int i; 67 68 if (of_device_is_compatible(np, "mmio-mux")) { 69 regmap = syscon_node_to_regmap(np->parent); 70 } else { 71 base = devm_platform_ioremap_resource(pdev, 0); 72 if (IS_ERR(base)) 73 regmap = ERR_PTR(-ENODEV); 74 else 75 regmap = regmap_init_mmio(dev, base, &mux_mmio_regmap_cfg); 76 /* Fallback to checking the parent node on "real" errors. */ 77 if (IS_ERR(regmap) && regmap != ERR_PTR(-EPROBE_DEFER)) { 78 regmap = dev_get_regmap(dev->parent, NULL); 79 if (!regmap) 80 regmap = ERR_PTR(-ENODEV); 81 } 82 } 83 if (IS_ERR(regmap)) 84 return dev_err_probe(dev, PTR_ERR(regmap), 85 "failed to get regmap\n"); 86 87 ret = of_property_count_u32_elems(np, "mux-reg-masks"); 88 if (ret == 0 || ret % 2) 89 ret = -EINVAL; 90 if (ret < 0) { 91 dev_err(dev, "mux-reg-masks property missing or invalid: %d\n", 92 ret); 93 return ret; 94 } 95 num_fields = ret / 2; 96 97 mux_chip = devm_mux_chip_alloc(dev, num_fields, sizeof(struct mux_mmio)); 98 if (IS_ERR(mux_chip)) 99 return PTR_ERR(mux_chip); 100 101 mux_mmio = mux_chip_priv(mux_chip); 102 103 mux_mmio->fields = devm_kmalloc(dev, num_fields * sizeof(*mux_mmio->fields), GFP_KERNEL); 104 if (IS_ERR(mux_mmio->fields)) 105 return PTR_ERR(mux_mmio->fields); 106 107 mux_mmio->hardware_states = devm_kmalloc(dev, num_fields * 108 sizeof(*mux_mmio->hardware_states), GFP_KERNEL); 109 if (IS_ERR(mux_mmio->hardware_states)) 110 return PTR_ERR(mux_mmio->hardware_states); 111 112 for (i = 0; i < num_fields; i++) { 113 struct mux_control *mux = &mux_chip->mux[i]; 114 struct reg_field field; 115 s32 idle_state = MUX_IDLE_AS_IS; 116 u32 reg, mask; 117 int bits; 118 119 ret = of_property_read_u32_index(np, "mux-reg-masks", 120 2 * i, ®); 121 if (!ret) 122 ret = of_property_read_u32_index(np, "mux-reg-masks", 123 2 * i + 1, &mask); 124 if (ret < 0) { 125 dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n", 126 i, ret); 127 return ret; 128 } 129 130 field.reg = reg; 131 field.msb = fls(mask) - 1; 132 field.lsb = ffs(mask) - 1; 133 134 if (mask != GENMASK(field.msb, field.lsb)) { 135 dev_err(dev, "bitfield %d: invalid mask 0x%x\n", 136 i, mask); 137 return -EINVAL; 138 } 139 140 mux_mmio->fields[i] = devm_regmap_field_alloc(dev, regmap, field); 141 if (IS_ERR(mux_mmio->fields[i])) { 142 ret = PTR_ERR(mux_mmio->fields[i]); 143 dev_err(dev, "bitfield %d: failed to allocate: %d\n", 144 i, ret); 145 return ret; 146 } 147 148 bits = 1 + field.msb - field.lsb; 149 mux->states = 1 << bits; 150 151 of_property_read_u32_index(np, "idle-states", i, 152 (u32 *)&idle_state); 153 if (idle_state != MUX_IDLE_AS_IS) { 154 if (idle_state < 0 || idle_state >= mux->states) { 155 dev_err(dev, "bitfield: %d: out of range idle state %d\n", 156 i, idle_state); 157 return -EINVAL; 158 } 159 160 mux->idle_state = idle_state; 161 } 162 } 163 164 mux_chip->ops = &mux_mmio_ops; 165 166 dev_set_drvdata(dev, mux_chip); 167 168 return devm_mux_chip_register(dev, mux_chip); 169 } 170 171 static int mux_mmio_suspend_noirq(struct device *dev) 172 { 173 struct mux_chip *mux_chip = dev_get_drvdata(dev); 174 struct mux_mmio *mux_mmio = mux_chip_priv(mux_chip); 175 unsigned int state; 176 int ret, i; 177 178 for (i = 0; i < mux_chip->controllers; i++) { 179 ret = mux_mmio_get(&mux_chip->mux[i], &state); 180 if (ret) { 181 dev_err(dev, "control %u: error saving mux: %d\n", i, ret); 182 return ret; 183 } 184 185 mux_mmio->hardware_states[i] = state; 186 } 187 188 return 0; 189 } 190 191 static int mux_mmio_resume_noirq(struct device *dev) 192 { 193 struct mux_chip *mux_chip = dev_get_drvdata(dev); 194 struct mux_mmio *mux_mmio = mux_chip_priv(mux_chip); 195 int ret, i; 196 197 for (i = 0; i < mux_chip->controllers; i++) { 198 ret = mux_mmio_set(&mux_chip->mux[i], mux_mmio->hardware_states[i]); 199 if (ret) { 200 dev_err(dev, "control %u: error restoring mux: %d\n", i, ret); 201 return ret; 202 } 203 } 204 205 return 0; 206 } 207 208 static DEFINE_NOIRQ_DEV_PM_OPS(mux_mmio_pm_ops, mux_mmio_suspend_noirq, mux_mmio_resume_noirq); 209 210 static struct platform_driver mux_mmio_driver = { 211 .driver = { 212 .name = "mmio-mux", 213 .of_match_table = mux_mmio_dt_ids, 214 .pm = pm_sleep_ptr(&mux_mmio_pm_ops), 215 }, 216 .probe = mux_mmio_probe, 217 }; 218 module_platform_driver(mux_mmio_driver); 219 220 MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver"); 221 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>"); 222 MODULE_LICENSE("GPL v2"); 223