]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5746b0df V |
2 | /* |
3 | * PCF8575 I2C GPIO EXPANDER DRIVER | |
4 | * | |
a94a4071 | 5 | * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/ |
5746b0df V |
6 | * |
7 | * Vignesh R <[email protected]> | |
8 | * | |
5746b0df V |
9 | * |
10 | * Driver for TI PCF-8575 16-bit I2C gpio expander. Based on | |
11 | * gpio-pcf857x Linux Kernel(v4.7) driver. | |
12 | * | |
13 | * Copyright (C) 2007 David Brownell | |
14 | * | |
2132fce7 LM |
15 | * Add support for 8 bit expanders - like pca8574 |
16 | * Copyright (C) 2021 Lukasz Majewski - DENX Software Engineering | |
5746b0df | 17 | * |
5746b0df V |
18 | */ |
19 | ||
5746b0df V |
20 | #include <dm.h> |
21 | #include <i2c.h> | |
f7ae49fc | 22 | #include <log.h> |
5746b0df | 23 | #include <asm-generic/gpio.h> |
401d1c4f | 24 | #include <asm/global_data.h> |
cd93d625 | 25 | #include <linux/bitops.h> |
5746b0df V |
26 | |
27 | DECLARE_GLOBAL_DATA_PTR; | |
28 | ||
29 | struct pcf8575_chip { | |
5746b0df V |
30 | /* NOTE: these chips have strange "quasi-bidirectional" I/O pins. |
31 | * We can't actually know whether a pin is configured (a) as output | |
32 | * and driving the signal low, or (b) as input and reporting a low | |
33 | * value ... without knowing the last value written since the chip | |
34 | * came out of reset (if any). We can't read the latched output. | |
35 | * In short, the only reliable solution for setting up pin direction | |
36 | * is to do it explicitly. | |
37 | * | |
38 | * Using "out" avoids that trouble. When left initialized to zero, | |
39 | * our software copy of the "latch" then matches the chip's all-ones | |
40 | * reset state. Otherwise it flags pins to be driven low. | |
41 | */ | |
42 | unsigned int out; /* software latch */ | |
5746b0df V |
43 | }; |
44 | ||
2132fce7 | 45 | /* Read/Write to I/O expander */ |
5746b0df | 46 | |
2132fce7 | 47 | static int pcf8575_i2c_write(struct udevice *dev, unsigned int word) |
5746b0df | 48 | { |
caa4daa2 | 49 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
5746b0df V |
50 | u8 buf[2] = { word & 0xff, word >> 8, }; |
51 | int ret; | |
52 | ||
2132fce7 | 53 | ret = dm_i2c_write(dev, 0, buf, dev_get_driver_data(dev)); |
5746b0df V |
54 | if (ret) |
55 | printf("%s i2c write failed to addr %x\n", __func__, | |
56 | chip->chip_addr); | |
57 | ||
58 | return ret; | |
59 | } | |
60 | ||
2132fce7 | 61 | static int pcf8575_i2c_read(struct udevice *dev) |
5746b0df | 62 | { |
caa4daa2 | 63 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
2132fce7 | 64 | u8 buf[2] = {0x00, 0x00}; |
5746b0df V |
65 | int ret; |
66 | ||
2132fce7 | 67 | ret = dm_i2c_read(dev, 0, buf, dev_get_driver_data(dev)); |
5746b0df V |
68 | if (ret) { |
69 | printf("%s i2c read failed from addr %x\n", __func__, | |
70 | chip->chip_addr); | |
71 | return ret; | |
72 | } | |
73 | ||
74 | return (buf[1] << 8) | buf[0]; | |
75 | } | |
76 | ||
77 | static int pcf8575_direction_input(struct udevice *dev, unsigned offset) | |
78 | { | |
c69cda25 | 79 | struct pcf8575_chip *plat = dev_get_plat(dev); |
5746b0df V |
80 | int status; |
81 | ||
82 | plat->out |= BIT(offset); | |
2132fce7 | 83 | status = pcf8575_i2c_write(dev, plat->out); |
5746b0df V |
84 | |
85 | return status; | |
86 | } | |
87 | ||
88 | static int pcf8575_direction_output(struct udevice *dev, | |
89 | unsigned int offset, int value) | |
90 | { | |
c69cda25 | 91 | struct pcf8575_chip *plat = dev_get_plat(dev); |
5746b0df V |
92 | int ret; |
93 | ||
94 | if (value) | |
95 | plat->out |= BIT(offset); | |
96 | else | |
97 | plat->out &= ~BIT(offset); | |
98 | ||
2132fce7 | 99 | ret = pcf8575_i2c_write(dev, plat->out); |
5746b0df V |
100 | |
101 | return ret; | |
102 | } | |
103 | ||
104 | static int pcf8575_get_value(struct udevice *dev, unsigned int offset) | |
105 | { | |
106 | int value; | |
107 | ||
2132fce7 | 108 | value = pcf8575_i2c_read(dev); |
5746b0df V |
109 | |
110 | return (value < 0) ? value : ((value & BIT(offset)) >> offset); | |
111 | } | |
112 | ||
113 | static int pcf8575_set_value(struct udevice *dev, unsigned int offset, | |
114 | int value) | |
115 | { | |
116 | return pcf8575_direction_output(dev, offset, value); | |
117 | } | |
118 | ||
8a8d24bd | 119 | static int pcf8575_ofdata_plat(struct udevice *dev) |
5746b0df | 120 | { |
c69cda25 | 121 | struct pcf8575_chip *plat = dev_get_plat(dev); |
5746b0df V |
122 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
123 | ||
124 | int n_latch; | |
125 | ||
2132fce7 LM |
126 | /* |
127 | * Number of pins depends on the expander device and is specified | |
128 | * in the struct udevice_id (as in the Linue kernel). | |
129 | */ | |
130 | uc_priv->gpio_count = dev_get_driver_data(dev) * 8; | |
e160f7d4 | 131 | uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
5746b0df V |
132 | "gpio-bank-name", NULL); |
133 | if (!uc_priv->bank_name) | |
134 | uc_priv->bank_name = fdt_get_name(gd->fdt_blob, | |
e160f7d4 | 135 | dev_of_offset(dev), NULL); |
5746b0df | 136 | |
e160f7d4 | 137 | n_latch = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
5746b0df V |
138 | "lines-initial-states", 0); |
139 | plat->out = ~n_latch; | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | static int pcf8575_gpio_probe(struct udevice *dev) | |
145 | { | |
146 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); | |
147 | ||
148 | debug("%s GPIO controller with %d gpios probed\n", | |
149 | uc_priv->bank_name, uc_priv->gpio_count); | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | static const struct dm_gpio_ops pcf8575_gpio_ops = { | |
155 | .direction_input = pcf8575_direction_input, | |
156 | .direction_output = pcf8575_direction_output, | |
157 | .get_value = pcf8575_get_value, | |
158 | .set_value = pcf8575_set_value, | |
159 | }; | |
160 | ||
161 | static const struct udevice_id pcf8575_gpio_ids[] = { | |
2132fce7 LM |
162 | { .compatible = "nxp,pcf8575", .data = 2 }, |
163 | { .compatible = "ti,pcf8575", .data = 2 }, | |
164 | { .compatible = "nxp,pca8574", .data = 1 }, | |
5746b0df V |
165 | { } |
166 | }; | |
167 | ||
168 | U_BOOT_DRIVER(gpio_pcf8575) = { | |
169 | .name = "gpio_pcf8575", | |
170 | .id = UCLASS_GPIO, | |
171 | .ops = &pcf8575_gpio_ops, | |
172 | .of_match = pcf8575_gpio_ids, | |
8a8d24bd | 173 | .of_to_plat = pcf8575_ofdata_plat, |
5746b0df | 174 | .probe = pcf8575_gpio_probe, |
caa4daa2 | 175 | .plat_auto = sizeof(struct pcf8575_chip), |
5746b0df | 176 | }; |