blob: 2b70ff82bdd09af59abbe2e894b38328dac55687 [file] [log] [blame]
Michal Simekd79ac322016-04-25 10:50:42 +02001/*
2 * Copyright (C) 2015 - 2016 Xilinx, Inc.
Moritz Fischer36bdeb72017-09-12 06:46:59 -07003 * Copyright (C) 2017 National Instruments Corp
Michal Simekd79ac322016-04-25 10:50:42 +02004 * 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>
Moritz Fischer36bdeb72017-09-12 06:46:59 -070013
14#include <asm-generic/gpio.h>
Michal Simekd79ac322016-04-25 10:50:42 +020015
16DECLARE_GLOBAL_DATA_PTR;
17
Marek Behún8e6eda72017-06-09 19:28:43 +020018enum pca_type {
19 PCA9544,
20 PCA9547,
21 PCA9548
22};
23
24struct chip_desc {
25 u8 enable;
26 enum muxtype {
27 pca954x_ismux = 0,
28 pca954x_isswi,
29 } muxtype;
Chris Packham5bc90a82017-09-29 10:53:36 +130030 u32 width;
Marek Behún8e6eda72017-06-09 19:28:43 +020031};
32
Michal Simekd79ac322016-04-25 10:50:42 +020033struct pca954x_priv {
34 u32 addr; /* I2C mux address */
35 u32 width; /* I2C mux width - number of busses */
Moritz Fischer36bdeb72017-09-12 06:46:59 -070036 struct gpio_desc gpio_mux_reset;
Michal Simekd79ac322016-04-25 10:50:42 +020037};
38
Marek Behún8e6eda72017-06-09 19:28:43 +020039static const struct chip_desc chips[] = {
40 [PCA9544] = {
41 .enable = 0x4,
42 .muxtype = pca954x_ismux,
Chris Packham5bc90a82017-09-29 10:53:36 +130043 .width = 4,
Marek Behún8e6eda72017-06-09 19:28:43 +020044 },
45 [PCA9547] = {
46 .enable = 0x8,
47 .muxtype = pca954x_ismux,
Chris Packham5bc90a82017-09-29 10:53:36 +130048 .width = 8,
Marek Behún8e6eda72017-06-09 19:28:43 +020049 },
50 [PCA9548] = {
51 .enable = 0x8,
52 .muxtype = pca954x_isswi,
Chris Packham5bc90a82017-09-29 10:53:36 +130053 .width = 8,
Marek Behún8e6eda72017-06-09 19:28:43 +020054 },
55};
56
Michal Simekd79ac322016-04-25 10:50:42 +020057static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
58 uint channel)
59{
60 struct pca954x_priv *priv = dev_get_priv(mux);
61 uchar byte = 0;
62
63 return dm_i2c_write(mux, priv->addr, &byte, 1);
64}
65
66static int pca954x_select(struct udevice *mux, struct udevice *bus,
67 uint channel)
68{
69 struct pca954x_priv *priv = dev_get_priv(mux);
Marek Behún8e6eda72017-06-09 19:28:43 +020070 const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
71 uchar byte;
72
73 if (chip->muxtype == pca954x_ismux)
74 byte = channel | chip->enable;
75 else
76 byte = 1 << channel;
Michal Simekd79ac322016-04-25 10:50:42 +020077
78 return dm_i2c_write(mux, priv->addr, &byte, 1);
79}
80
81static const struct i2c_mux_ops pca954x_ops = {
82 .select = pca954x_select,
83 .deselect = pca954x_deselect,
84};
85
86static const struct udevice_id pca954x_ids[] = {
Marek Behún8e6eda72017-06-09 19:28:43 +020087 { .compatible = "nxp,pca9544", .data = PCA9544 },
88 { .compatible = "nxp,pca9547", .data = PCA9547 },
89 { .compatible = "nxp,pca9548", .data = PCA9548 },
Michal Simekd79ac322016-04-25 10:50:42 +020090 { }
91};
92
93static int pca954x_ofdata_to_platdata(struct udevice *dev)
94{
95 struct pca954x_priv *priv = dev_get_priv(dev);
Chris Packham5bc90a82017-09-29 10:53:36 +130096 const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
Michal Simekd79ac322016-04-25 10:50:42 +020097
Simon Glasse160f7d2017-01-17 16:52:55 -070098 priv->addr = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", 0);
Michal Simekd79ac322016-04-25 10:50:42 +020099 if (!priv->addr) {
100 debug("MUX not found\n");
101 return -ENODEV;
102 }
Chris Packham5bc90a82017-09-29 10:53:36 +1300103 priv->width = chip->width;
Michal Simekd79ac322016-04-25 10:50:42 +0200104
105 if (!priv->width) {
106 debug("No I2C MUX width specified\n");
107 return -EINVAL;
108 }
109
110 debug("Device %s at 0x%x with width %d\n",
111 dev->name, priv->addr, priv->width);
112
113 return 0;
114}
115
Moritz Fischer36bdeb72017-09-12 06:46:59 -0700116static int pca954x_probe(struct udevice *dev)
117{
118 if (IS_ENABLED(CONFIG_DM_GPIO)) {
119 struct pca954x_priv *priv = dev_get_priv(dev);
120 int err;
121
122 err = gpio_request_by_name(dev, "reset-gpios", 0,
123 &priv->gpio_mux_reset, GPIOD_IS_OUT);
124
125 /* it's optional so only bail if we get a real error */
126 if (err && (err != -ENOENT))
127 return err;
128
129 /* dm will take care of polarity */
130 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
131 dm_gpio_set_value(&priv->gpio_mux_reset, 0);
132 }
133
134 return 0;
135}
136
137static int pca954x_remove(struct udevice *dev)
138{
139 if (IS_ENABLED(CONFIG_DM_GPIO)) {
140 struct pca954x_priv *priv = dev_get_priv(dev);
141
142 if (dm_gpio_is_valid(&priv->gpio_mux_reset))
143 dm_gpio_free(dev, &priv->gpio_mux_reset);
144 }
145
146 return 0;
147}
148
Michal Simekd79ac322016-04-25 10:50:42 +0200149U_BOOT_DRIVER(pca954x) = {
150 .name = "pca954x",
151 .id = UCLASS_I2C_MUX,
152 .of_match = pca954x_ids,
Moritz Fischer36bdeb72017-09-12 06:46:59 -0700153 .probe = pca954x_probe,
154 .remove = pca954x_remove,
Michal Simekd79ac322016-04-25 10:50:42 +0200155 .ops = &pca954x_ops,
156 .ofdata_to_platdata = pca954x_ofdata_to_platdata,
157 .priv_auto_alloc_size = sizeof(struct pca954x_priv),
158};