1 // SPDX-License-Identifier: GPL-2.0+
13 * This driver is used for MDIO muxes driven by writing to a register of an I2C
14 * chip. The board it was developed for uses a mux controlled by on-board FPGA
15 * which in turn is accessed as a chip over I2C.
18 struct mdio_mux_i2creg_priv {
24 static int mdio_mux_i2creg_select(struct udevice *mux, int cur, int sel)
26 struct mdio_mux_i2creg_priv *priv = dev_get_priv(mux);
29 /* if last selection didn't change we're good to go */
33 val_old = dm_i2c_reg_read(priv->chip, priv->reg);
34 val = (val_old & ~priv->mask) | (sel & priv->mask);
35 debug("%s: chip %s, reg %x, val %x => %x\n", __func__, priv->chip->name,
36 priv->reg, val_old, val);
37 dm_i2c_reg_write(priv->chip, priv->reg, val);
42 static const struct mdio_mux_ops mdio_mux_i2creg_ops = {
43 .select = mdio_mux_i2creg_select,
46 static int mdio_mux_i2creg_probe(struct udevice *dev)
48 struct mdio_mux_i2creg_priv *priv = dev_get_priv(dev);
49 ofnode chip_node, bus_node;
50 struct udevice *i2c_bus;
55 /* read the register addr/mask pair */
56 err = dev_read_u32_array(dev, "mux-reg-masks", reg_mask, 2);
58 debug("%s: error reading mux-reg-masks property\n", __func__);
62 /* parent should be an I2C chip, grandparent should be an I2C bus */
63 chip_node = ofnode_get_parent(dev->node);
64 bus_node = ofnode_get_parent(chip_node);
66 err = uclass_get_device_by_ofnode(UCLASS_I2C, bus_node, &i2c_bus);
68 debug("%s: can't find I2C bus for node %s\n", __func__,
69 ofnode_get_name(bus_node));
73 err = ofnode_read_u32(chip_node, "reg", &chip_addr);
75 debug("%s: can't read chip address in %s\n", __func__,
76 ofnode_get_name(chip_node));
80 err = i2c_get_chip(i2c_bus, (uint)chip_addr, 1, &priv->chip);
82 debug("%s: can't find i2c chip device for addr %x\n", __func__,
87 priv->reg = (int)reg_mask[0];
88 priv->mask = (int)reg_mask[1];
90 debug("%s: chip %s, reg %x, mask %x\n", __func__, priv->chip->name,
91 priv->reg, priv->mask);
96 static const struct udevice_id mdio_mux_i2creg_ids[] = {
97 { .compatible = "mdio-mux-i2creg" },
101 U_BOOT_DRIVER(mdio_mux_i2creg) = {
102 .name = "mdio_mux_i2creg",
103 .id = UCLASS_MDIO_MUX,
104 .of_match = mdio_mux_i2creg_ids,
105 .probe = mdio_mux_i2creg_probe,
106 .ops = &mdio_mux_i2creg_ops,
107 .priv_auto_alloc_size = sizeof(struct mdio_mux_i2creg_priv),