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 static int mux_mmio_set(struct mux_control *mux, int state) 19 { 20 struct regmap_field **fields = mux_chip_priv(mux->chip); 21 22 return regmap_field_write(fields[mux_control_get_index(mux)], state); 23 } 24 25 static const struct mux_control_ops mux_mmio_ops = { 26 .set = mux_mmio_set, 27 }; 28 29 static const struct of_device_id mux_mmio_dt_ids[] = { 30 { .compatible = "mmio-mux", }, 31 { .compatible = "reg-mux", }, 32 { /* sentinel */ } 33 }; 34 MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids); 35 36 static const struct regmap_config mux_mmio_regmap_cfg = { 37 .reg_bits = 32, 38 .val_bits = 32, 39 .reg_stride = 4, 40 }; 41 42 static int mux_mmio_probe(struct platform_device *pdev) 43 { 44 struct device *dev = &pdev->dev; 45 struct device_node *np = dev->of_node; 46 struct regmap_field **fields; 47 struct mux_chip *mux_chip; 48 struct regmap *regmap; 49 void __iomem *base; 50 int num_fields; 51 int ret; 52 int i; 53 54 if (of_device_is_compatible(np, "mmio-mux")) { 55 regmap = syscon_node_to_regmap(np->parent); 56 } else { 57 base = devm_platform_ioremap_resource(pdev, 0); 58 if (IS_ERR(base)) 59 regmap = ERR_PTR(-ENODEV); 60 else 61 regmap = regmap_init_mmio(dev, base, &mux_mmio_regmap_cfg); 62 /* Fallback to checking the parent node on "real" errors. */ 63 if (IS_ERR(regmap) && regmap != ERR_PTR(-EPROBE_DEFER)) { 64 regmap = dev_get_regmap(dev->parent, NULL); 65 if (!regmap) 66 regmap = ERR_PTR(-ENODEV); 67 } 68 } 69 if (IS_ERR(regmap)) 70 return dev_err_probe(dev, PTR_ERR(regmap), 71 "failed to get regmap\n"); 72 73 ret = of_property_count_u32_elems(np, "mux-reg-masks"); 74 if (ret == 0 || ret % 2) 75 ret = -EINVAL; 76 if (ret < 0) { 77 dev_err(dev, "mux-reg-masks property missing or invalid: %d\n", 78 ret); 79 return ret; 80 } 81 num_fields = ret / 2; 82 83 mux_chip = devm_mux_chip_alloc(dev, num_fields, num_fields * 84 sizeof(*fields)); 85 if (IS_ERR(mux_chip)) 86 return PTR_ERR(mux_chip); 87 88 fields = mux_chip_priv(mux_chip); 89 90 for (i = 0; i < num_fields; i++) { 91 struct mux_control *mux = &mux_chip->mux[i]; 92 struct reg_field field; 93 s32 idle_state = MUX_IDLE_AS_IS; 94 u32 reg, mask; 95 int bits; 96 97 ret = of_property_read_u32_index(np, "mux-reg-masks", 98 2 * i, ®); 99 if (!ret) 100 ret = of_property_read_u32_index(np, "mux-reg-masks", 101 2 * i + 1, &mask); 102 if (ret < 0) { 103 dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n", 104 i, ret); 105 return ret; 106 } 107 108 field.reg = reg; 109 field.msb = fls(mask) - 1; 110 field.lsb = ffs(mask) - 1; 111 112 if (mask != GENMASK(field.msb, field.lsb)) { 113 dev_err(dev, "bitfield %d: invalid mask 0x%x\n", 114 i, mask); 115 return -EINVAL; 116 } 117 118 fields[i] = devm_regmap_field_alloc(dev, regmap, field); 119 if (IS_ERR(fields[i])) { 120 ret = PTR_ERR(fields[i]); 121 dev_err(dev, "bitfield %d: failed to allocate: %d\n", 122 i, ret); 123 return ret; 124 } 125 126 bits = 1 + field.msb - field.lsb; 127 mux->states = 1 << bits; 128 129 of_property_read_u32_index(np, "idle-states", i, 130 (u32 *)&idle_state); 131 if (idle_state != MUX_IDLE_AS_IS) { 132 if (idle_state < 0 || idle_state >= mux->states) { 133 dev_err(dev, "bitfield: %d: out of range idle state %d\n", 134 i, idle_state); 135 return -EINVAL; 136 } 137 138 mux->idle_state = idle_state; 139 } 140 } 141 142 mux_chip->ops = &mux_mmio_ops; 143 144 return devm_mux_chip_register(dev, mux_chip); 145 } 146 147 static struct platform_driver mux_mmio_driver = { 148 .driver = { 149 .name = "mmio-mux", 150 .of_match_table = mux_mmio_dt_ids, 151 }, 152 .probe = mux_mmio_probe, 153 }; 154 module_platform_driver(mux_mmio_driver); 155 156 MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver"); 157 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>"); 158 MODULE_LICENSE("GPL v2"); 159