1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <common.h>
28#include <dm.h>
29#include <i2c.h>
30#include <asm-generic/gpio.h>
31
32DECLARE_GLOBAL_DATA_PTR;
33
34struct pcf8575_chip {
35 int gpio_count;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 unsigned int out;
50 const char *bank_name;
51};
52
53
54
55static int pcf8575_i2c_write_le16(struct udevice *dev, unsigned int word)
56{
57 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
58 u8 buf[2] = { word & 0xff, word >> 8, };
59 int ret;
60
61 ret = dm_i2c_write(dev, 0, buf, 2);
62 if (ret)
63 printf("%s i2c write failed to addr %x\n", __func__,
64 chip->chip_addr);
65
66 return ret;
67}
68
69static int pcf8575_i2c_read_le16(struct udevice *dev)
70{
71 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
72 u8 buf[2];
73 int ret;
74
75 ret = dm_i2c_read(dev, 0, buf, 2);
76 if (ret) {
77 printf("%s i2c read failed from addr %x\n", __func__,
78 chip->chip_addr);
79 return ret;
80 }
81
82 return (buf[1] << 8) | buf[0];
83}
84
85static int pcf8575_direction_input(struct udevice *dev, unsigned offset)
86{
87 struct pcf8575_chip *plat = dev_get_platdata(dev);
88 int status;
89
90 plat->out |= BIT(offset);
91 status = pcf8575_i2c_write_le16(dev, plat->out);
92
93 return status;
94}
95
96static int pcf8575_direction_output(struct udevice *dev,
97 unsigned int offset, int value)
98{
99 struct pcf8575_chip *plat = dev_get_platdata(dev);
100 int ret;
101
102 if (value)
103 plat->out |= BIT(offset);
104 else
105 plat->out &= ~BIT(offset);
106
107 ret = pcf8575_i2c_write_le16(dev, plat->out);
108
109 return ret;
110}
111
112static int pcf8575_get_value(struct udevice *dev, unsigned int offset)
113{
114 int value;
115
116 value = pcf8575_i2c_read_le16(dev);
117
118 return (value < 0) ? value : ((value & BIT(offset)) >> offset);
119}
120
121static int pcf8575_set_value(struct udevice *dev, unsigned int offset,
122 int value)
123{
124 return pcf8575_direction_output(dev, offset, value);
125}
126
127static int pcf8575_ofdata_platdata(struct udevice *dev)
128{
129 struct pcf8575_chip *plat = dev_get_platdata(dev);
130 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
131
132 int n_latch;
133
134 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
135 "gpio-count", 16);
136 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
137 "gpio-bank-name", NULL);
138 if (!uc_priv->bank_name)
139 uc_priv->bank_name = fdt_get_name(gd->fdt_blob,
140 dev_of_offset(dev), NULL);
141
142 n_latch = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
143 "lines-initial-states", 0);
144 plat->out = ~n_latch;
145
146 return 0;
147}
148
149static int pcf8575_gpio_probe(struct udevice *dev)
150{
151 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
152
153 debug("%s GPIO controller with %d gpios probed\n",
154 uc_priv->bank_name, uc_priv->gpio_count);
155
156 return 0;
157}
158
159static const struct dm_gpio_ops pcf8575_gpio_ops = {
160 .direction_input = pcf8575_direction_input,
161 .direction_output = pcf8575_direction_output,
162 .get_value = pcf8575_get_value,
163 .set_value = pcf8575_set_value,
164};
165
166static const struct udevice_id pcf8575_gpio_ids[] = {
167 { .compatible = "nxp,pcf8575" },
168 { .compatible = "ti,pcf8575" },
169 { }
170};
171
172U_BOOT_DRIVER(gpio_pcf8575) = {
173 .name = "gpio_pcf8575",
174 .id = UCLASS_GPIO,
175 .ops = &pcf8575_gpio_ops,
176 .of_match = pcf8575_gpio_ids,
177 .ofdata_to_platdata = pcf8575_ofdata_platdata,
178 .probe = pcf8575_gpio_probe,
179 .platdata_auto_alloc_size = sizeof(struct pcf8575_chip),
180};
181