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_platform.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 { /* sentinel */ } 32 }; 33 MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids); 34 35 static int mux_mmio_probe(struct platform_device *pdev) 36 { 37 struct device *dev = &pdev->dev; 38 struct device_node *np = dev->of_node; 39 struct regmap_field **fields; 40 struct mux_chip *mux_chip; 41 struct regmap *regmap; 42 int num_fields; 43 int ret; 44 int i; 45 46 regmap = syscon_node_to_regmap(np->parent); 47 if (IS_ERR(regmap)) { 48 ret = PTR_ERR(regmap); 49 dev_err(dev, "failed to get regmap: %d\n", ret); 50 return ret; 51 } 52 53 ret = of_property_count_u32_elems(np, "mux-reg-masks"); 54 if (ret == 0 || ret % 2) 55 ret = -EINVAL; 56 if (ret < 0) { 57 dev_err(dev, "mux-reg-masks property missing or invalid: %d\n", 58 ret); 59 return ret; 60 } 61 num_fields = ret / 2; 62 63 mux_chip = devm_mux_chip_alloc(dev, num_fields, num_fields * 64 sizeof(*fields)); 65 if (IS_ERR(mux_chip)) 66 return PTR_ERR(mux_chip); 67 68 fields = mux_chip_priv(mux_chip); 69 70 for (i = 0; i < num_fields; i++) { 71 struct mux_control *mux = &mux_chip->mux[i]; 72 struct reg_field field; 73 s32 idle_state = MUX_IDLE_AS_IS; 74 u32 reg, mask; 75 int bits; 76 77 ret = of_property_read_u32_index(np, "mux-reg-masks", 78 2 * i, ®); 79 if (!ret) 80 ret = of_property_read_u32_index(np, "mux-reg-masks", 81 2 * i + 1, &mask); 82 if (ret < 0) { 83 dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n", 84 i, ret); 85 return ret; 86 } 87 88 field.reg = reg; 89 field.msb = fls(mask) - 1; 90 field.lsb = ffs(mask) - 1; 91 92 if (mask != GENMASK(field.msb, field.lsb)) { 93 dev_err(dev, "bitfield %d: invalid mask 0x%x\n", 94 i, mask); 95 return -EINVAL; 96 } 97 98 fields[i] = devm_regmap_field_alloc(dev, regmap, field); 99 if (IS_ERR(fields[i])) { 100 ret = PTR_ERR(fields[i]); 101 dev_err(dev, "bitfield %d: failed allocate: %d\n", 102 i, ret); 103 return ret; 104 } 105 106 bits = 1 + field.msb - field.lsb; 107 mux->states = 1 << bits; 108 109 of_property_read_u32_index(np, "idle-states", i, 110 (u32 *)&idle_state); 111 if (idle_state != MUX_IDLE_AS_IS) { 112 if (idle_state < 0 || idle_state >= mux->states) { 113 dev_err(dev, "bitfield: %d: out of range idle state %d\n", 114 i, idle_state); 115 return -EINVAL; 116 } 117 118 mux->idle_state = idle_state; 119 } 120 } 121 122 mux_chip->ops = &mux_mmio_ops; 123 124 return devm_mux_chip_register(dev, mux_chip); 125 } 126 127 static struct platform_driver mux_mmio_driver = { 128 .driver = { 129 .name = "mmio-mux", 130 .of_match_table = of_match_ptr(mux_mmio_dt_ids), 131 }, 132 .probe = mux_mmio_probe, 133 }; 134 module_platform_driver(mux_mmio_driver); 135 136 MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver"); 137 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>"); 138 MODULE_LICENSE("GPL v2"); 139