Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 2 | /* |
| 3 | * I2C multiplexer using GPIO API |
| 4 | * |
| 5 | * Copyright 2017 NXP |
| 6 | * |
| 7 | * Peng Fan <peng.fan@nxp.com> |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm-generic/gpio.h> |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <dm/devres.h> |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 16 | #include <dm/pinctrl.h> |
| 17 | #include <fdtdec.h> |
| 18 | #include <i2c.h> |
| 19 | #include <linux/errno.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 20 | #include <linux/libfdt.h> |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | /** |
| 25 | * struct i2c_mux_gpio_priv - private data for i2c mux gpio |
| 26 | * |
| 27 | * @values: the reg value of each child node |
| 28 | * @n_values: num of regs |
| 29 | * @gpios: the mux-gpios array |
| 30 | * @n_gpios: num of gpios in mux-gpios |
| 31 | * @idle: the value of idle-state |
| 32 | */ |
| 33 | struct i2c_mux_gpio_priv { |
| 34 | u32 *values; |
| 35 | int n_values; |
| 36 | struct gpio_desc *gpios; |
| 37 | int n_gpios; |
| 38 | u32 idle; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | static int i2c_mux_gpio_select(struct udevice *dev, struct udevice *bus, |
| 43 | uint channel) |
| 44 | { |
| 45 | struct i2c_mux_gpio_priv *priv = dev_get_priv(dev); |
| 46 | int i, ret; |
| 47 | |
| 48 | for (i = 0; i < priv->n_gpios; i++) { |
| 49 | ret = dm_gpio_set_value(&priv->gpios[i], (channel >> i) & 1); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int i2c_mux_gpio_deselect(struct udevice *dev, struct udevice *bus, |
| 58 | uint channel) |
| 59 | { |
| 60 | struct i2c_mux_gpio_priv *priv = dev_get_priv(dev); |
| 61 | int i, ret; |
| 62 | |
| 63 | for (i = 0; i < priv->n_gpios; i++) { |
| 64 | ret = dm_gpio_set_value(&priv->gpios[i], (priv->idle >> i) & 1); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int i2c_mux_gpio_probe(struct udevice *dev) |
| 73 | { |
| 74 | const void *fdt = gd->fdt_blob; |
| 75 | int node = dev_of_offset(dev); |
| 76 | struct i2c_mux_gpio_priv *mux = dev_get_priv(dev); |
| 77 | struct gpio_desc *gpios; |
| 78 | u32 *values; |
| 79 | int i = 0, subnode, ret; |
| 80 | |
| 81 | mux->n_values = fdtdec_get_child_count(fdt, node); |
| 82 | values = devm_kzalloc(dev, sizeof(*mux->values) * mux->n_values, |
| 83 | GFP_KERNEL); |
| 84 | if (!values) { |
| 85 | dev_err(dev, "Cannot alloc values array"); |
| 86 | return -ENOMEM; |
| 87 | } |
| 88 | |
| 89 | fdt_for_each_subnode(subnode, fdt, node) { |
| 90 | *(values + i) = fdtdec_get_uint(fdt, subnode, "reg", -1); |
| 91 | i++; |
| 92 | } |
| 93 | |
| 94 | mux->values = values; |
| 95 | |
| 96 | mux->idle = fdtdec_get_uint(fdt, node, "idle-state", -1); |
| 97 | |
| 98 | mux->n_gpios = gpio_get_list_count(dev, "mux-gpios"); |
| 99 | if (mux->n_gpios < 0) { |
| 100 | dev_err(dev, "Missing mux-gpios property\n"); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | gpios = devm_kzalloc(dev, sizeof(struct gpio_desc) * mux->n_gpios, |
| 105 | GFP_KERNEL); |
| 106 | if (!gpios) { |
| 107 | dev_err(dev, "Cannot allocate gpios array\n"); |
| 108 | return -ENOMEM; |
| 109 | } |
| 110 | |
| 111 | ret = gpio_request_list_by_name(dev, "mux-gpios", gpios, mux->n_gpios, |
Ye Li | 1908e0c | 2019-07-10 10:23:09 +0000 | [diff] [blame] | 112 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 113 | if (ret <= 0) { |
| 114 | dev_err(dev, "Failed to request mux-gpios\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | mux->gpios = gpios; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static const struct i2c_mux_ops i2c_mux_gpio_ops = { |
| 124 | .select = i2c_mux_gpio_select, |
| 125 | .deselect = i2c_mux_gpio_deselect, |
| 126 | }; |
| 127 | |
| 128 | static const struct udevice_id i2c_mux_gpio_ids[] = { |
| 129 | { .compatible = "i2c-mux-gpio", }, |
| 130 | {} |
| 131 | }; |
| 132 | |
| 133 | U_BOOT_DRIVER(i2c_mux_gpio) = { |
| 134 | .name = "i2c_mux_gpio", |
| 135 | .id = UCLASS_I2C_MUX, |
| 136 | .of_match = i2c_mux_gpio_ids, |
| 137 | .ops = &i2c_mux_gpio_ops, |
| 138 | .probe = i2c_mux_gpio_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 139 | .priv_auto = sizeof(struct i2c_mux_gpio_priv), |
Peng Fan | 27ab770 | 2017-08-14 18:00:05 +0800 | [diff] [blame] | 140 | }; |