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