1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 - 2016 Xilinx, Inc.
4  * Copyright (C) 2017 National Instruments Corp
5  * Written by Michal Simek
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm/global_data.h>
15 
16 #include <asm-generic/gpio.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 enum pca_type {
21 	PCA9543,
22 	PCA9544,
23 	PCA9546,
24 	PCA9547,
25 	PCA9548,
26 	PCA9646
27 };
28 
29 struct chip_desc {
30 	u8 enable; /* Enable mask in ctl register (used for muxes only) */
31 	enum muxtype {
32 		pca954x_ismux = 0,
33 		pca954x_isswi,
34 	} muxtype;
35 	u32 width;
36 };
37 
38 struct pca954x_priv {
39 	u32 addr; /* I2C mux address */
40 	u32 width; /* I2C mux width - number of busses */
41 	struct gpio_desc gpio_mux_reset;
42 };
43 
44 static const struct chip_desc chips[] = {
45 	[PCA9543] = {
46 		.muxtype = pca954x_isswi,
47 		.width = 2,
48 	},
49 	[PCA9544] = {
50 		.enable = 0x4,
51 		.muxtype = pca954x_ismux,
52 		.width = 4,
53 	},
54 	[PCA9546] = {
55 		.muxtype = pca954x_isswi,
56 		.width = 4,
57 	},
58 	[PCA9547] = {
59 		.enable = 0x8,
60 		.muxtype = pca954x_ismux,
61 		.width = 8,
62 	},
63 	[PCA9548] = {
64 		.muxtype = pca954x_isswi,
65 		.width = 8,
66 	},
67 	[PCA9646] = {
68 		.muxtype = pca954x_isswi,
69 		.width = 4,
70 	},
71 };
72 
pca954x_deselect(struct udevice * mux,struct udevice * bus,uint channel)73 static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
74 			    uint channel)
75 {
76 	struct pca954x_priv *priv = dev_get_priv(mux);
77 	uchar byte = 0;
78 
79 	return dm_i2c_write(mux, priv->addr, &byte, 1);
80 }
81 
pca954x_select(struct udevice * mux,struct udevice * bus,uint channel)82 static int pca954x_select(struct udevice *mux, struct udevice *bus,
83 			  uint channel)
84 {
85 	struct pca954x_priv *priv = dev_get_priv(mux);
86 	const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
87 	uchar byte;
88 
89 	if (chip->muxtype == pca954x_ismux)
90 		byte = channel | chip->enable;
91 	else
92 		byte = 1 << channel;
93 
94 	return dm_i2c_write(mux, priv->addr, &byte, 1);
95 }
96 
97 static const struct i2c_mux_ops pca954x_ops = {
98 	.select = pca954x_select,
99 	.deselect = pca954x_deselect,
100 };
101 
102 static const struct udevice_id pca954x_ids[] = {
103 	{ .compatible = "nxp,pca9543", .data = PCA9543 },
104 	{ .compatible = "nxp,pca9544", .data = PCA9544 },
105 	{ .compatible = "nxp,pca9546", .data = PCA9546 },
106 	{ .compatible = "nxp,pca9547", .data = PCA9547 },
107 	{ .compatible = "nxp,pca9548", .data = PCA9548 },
108 	{ .compatible = "nxp,pca9646", .data = PCA9646 },
109 	{ }
110 };
111 
pca954x_of_to_plat(struct udevice * dev)112 static int pca954x_of_to_plat(struct udevice *dev)
113 {
114 	struct pca954x_priv *priv = dev_get_priv(dev);
115 	const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
116 
117 	priv->addr = dev_read_u32_default(dev, "reg", 0);
118 	if (!priv->addr) {
119 		debug("MUX not found\n");
120 		return -ENODEV;
121 	}
122 	priv->width = chip->width;
123 
124 	if (!priv->width) {
125 		debug("No I2C MUX width specified\n");
126 		return -EINVAL;
127 	}
128 
129 	debug("Device %s at 0x%x with width %d\n",
130 	      dev->name, priv->addr, priv->width);
131 
132 	return 0;
133 }
134 
pca954x_probe(struct udevice * dev)135 static int pca954x_probe(struct udevice *dev)
136 {
137 	if (CONFIG_IS_ENABLED(DM_GPIO)) {
138 		struct pca954x_priv *priv = dev_get_priv(dev);
139 		int err;
140 
141 		err = gpio_request_by_name(dev, "reset-gpios", 0,
142 				&priv->gpio_mux_reset, GPIOD_IS_OUT);
143 
144 		/* it's optional so only bail if we get a real error */
145 		if (err && (err != -ENOENT))
146 			return err;
147 
148 		/* dm will take care of polarity */
149 		if (dm_gpio_is_valid(&priv->gpio_mux_reset))
150 			dm_gpio_set_value(&priv->gpio_mux_reset, 0);
151 	}
152 
153 	return 0;
154 }
155 
pca954x_remove(struct udevice * dev)156 static int pca954x_remove(struct udevice *dev)
157 {
158 	if (CONFIG_IS_ENABLED(DM_GPIO)) {
159 		struct pca954x_priv *priv = dev_get_priv(dev);
160 
161 		if (dm_gpio_is_valid(&priv->gpio_mux_reset))
162 			dm_gpio_free(dev, &priv->gpio_mux_reset);
163 	}
164 
165 	return 0;
166 }
167 
168 U_BOOT_DRIVER(pca954x) = {
169 	.name = "pca954x",
170 	.id = UCLASS_I2C_MUX,
171 	.of_match = pca954x_ids,
172 	.probe = pca954x_probe,
173 	.remove = pca954x_remove,
174 	.ops = &pca954x_ops,
175 	.of_to_plat = pca954x_of_to_plat,
176 	.priv_auto	= sizeof(struct pca954x_priv),
177 };
178