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