hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// SPDX-License-Identifier: GPL-2.0-only
/*
 *  NCA9539 I2C Port Expander I/O
 *
 *  Copyright (C) 2023 Cody Xie <cody.xie@rock-chips.com>
 *
 */
#include <common.h>
#include <errno.h>
#include <dm.h>
#include <fdtdec.h>
#include <i2c.h>
#include <malloc.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <dt-bindings/gpio/gpio.h>
#include <linux/bitops.h>
 
#define NCA9539_REG_INPUT_PORT_BASE 0x00
#define NCA9539_REG_INPUT_PORT0 (NCA9539_REG_INPUT_PORT_BASE + 0x0)
#define NCA9539_REG_INPUT_PORT1 (NCA9539_REG_INPUT_PORT_BASE + 0x1)
#define NCA9539_REG_OUTPUT_PORT_BASE 0x02
#define NCA9539_REG_OUTPUT_PORT0 (NCA9539_REG_OUTPUT_PORT_BASE + 0x0)
#define NCA9539_REG_OUTPUT_PORT1 (NCA9539_REG_OUTPUT_PORT_BASE + 0x1)
#define NCA9539_REG_POLARITY_BASE 0x04
#define NCA9539_REG_POLARITY_PORT0 (NCA9539_REG_POLARITY_BASE + 0x0)
#define NCA9539_REG_POLARITY_PORT1 (NCA9539_REG_POLARITY_BASE + 0x1)
#define NCA9539_REG_CONFIG_BASE 0x06
#define NCA9539_REG_CONFIG_PORT0 (NCA9539_REG_CONFIG_BASE + 0x0)
#define NCA9539_REG_CONFIG_PORT1 (NCA9539_REG_CONFIG_BASE + 0x1)
 
#define NCA9539_BANK_SZ 8
#define NCA9539_MAX_BANK 2
 
#define NCA9539_CHIP_ADDR 0x74
 
#ifndef BIT
#define BIT(nr) (1UL << (nr))
#endif
 
struct nca9539_info {
   struct udevice *dev;
   int addr;
   unsigned int ngpio;
};
 
static int nca9539_write_reg(struct udevice *dev, int reg, u8 val)
{
   int ret = 0;
 
   ret = dm_i2c_write(dev, reg, &val, 1);
   if (ret) {
       dev_err(dev, "%s error\n", __func__);
       return ret;
   }
 
   return 0;
}
 
static int nca9539_read_reg(struct udevice *dev, int reg, u8 *val)
{
   int ret;
   u8 byte;
 
   ret = dm_i2c_read(dev, reg, &byte, 1);
   if (ret) {
       dev_err(dev, "%s error\n", __func__);
       return ret;
   }
 
   *val = byte;
 
   return 0;
}
 
static int nca9539_gpio_get_direction(struct udevice *dev, unsigned int offset)
{
   unsigned int port = offset / NCA9539_BANK_SZ;
   unsigned int pin = offset % NCA9539_BANK_SZ;
   u8 value;
   int ret;
 
   dev_dbg(dev, "%s offset(%d)\n", __func__, offset);
   ret = nca9539_read_reg(dev, NCA9539_REG_CONFIG_BASE + port, &value);
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) read config failed\n", __func__,
           offset);
       return ret;
   }
 
   if (value & BIT(pin))
       return GPIOF_INPUT;
 
   return GPIOF_OUTPUT;
}
 
static int nca9539_gpio_direction_input(struct udevice *dev,
                   unsigned int offset)
{
   unsigned int port = offset / NCA9539_BANK_SZ;
   unsigned int pin = offset % NCA9539_BANK_SZ;
   u8 val;
   int ret = 0;
 
   dev_dbg(dev, "%s offset(%d)\n", __func__, offset);
 
   ret = nca9539_read_reg(dev, NCA9539_REG_CONFIG_BASE + port, &val);
   if (!ret) {
       val &= ~BIT(pin);
       val |= BIT(pin);
       ret = nca9539_write_reg(dev, NCA9539_REG_CONFIG_BASE + port,
                   val);
   }
 
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) read config failed\n", __func__,
           offset);
   }
 
   return ret;
}
 
static int nca9539_gpio_direction_output(struct udevice *dev,
                    unsigned int offset, int val)
{
   unsigned int port = offset / NCA9539_BANK_SZ;
   unsigned int pin = offset % NCA9539_BANK_SZ;
   u8 value;
   int ret;
 
   dev_dbg(dev, "%s offset(%d) val(%d)\n", __func__, offset, val);
 
   ret = nca9539_read_reg(dev, NCA9539_REG_CONFIG_BASE + port, &value);
   if (!ret) {
       value &= ~BIT(pin);
       ret = nca9539_write_reg(dev, NCA9539_REG_CONFIG_BASE + port,
                   value);
   }
   if (ret < 0) {
       dev_warn(dev, "%s offset(%d) read config failed\n", __func__,
           offset);
   }
 
   ret = nca9539_read_reg(dev, NCA9539_REG_OUTPUT_PORT_BASE + port,
                  &value);
   if (!ret) {
       value &= ~BIT(pin);
       value |= val ? BIT(pin) : 0;
       ret = nca9539_write_reg(
           dev, NCA9539_REG_OUTPUT_PORT_BASE + port, value);
   }
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) val(%d) update output failed\n",
           __func__, offset, val);
   }
 
   return ret;
}
 
static int nca9539_gpio_get(struct udevice *dev, unsigned int offset)
{
   unsigned int port = offset / NCA9539_BANK_SZ;
   unsigned int pin = offset % NCA9539_BANK_SZ;
   int reg;
   u8 value;
   int ret;
 
   dev_dbg(dev, "%s offset(%d)\n", __func__, offset);
   ret = nca9539_read_reg(dev, NCA9539_REG_CONFIG_BASE + port, &value);
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) check config failed\n", __func__,
           offset);
       return ret;
   }
   if (!(BIT(pin) & value))
       reg = NCA9539_REG_OUTPUT_PORT_BASE + port;
   else
       reg = NCA9539_REG_INPUT_PORT_BASE + port;
   ret = nca9539_read_reg(dev, reg, &value);
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) read value failed\n", __func__,
           offset);
       return -EIO;
   }
 
   return !!(BIT(pin) & value);
}
 
static int nca9539_gpio_set(struct udevice *dev, unsigned int offset, int val)
{
   unsigned int port = offset / NCA9539_BANK_SZ;
   unsigned int pin = offset % NCA9539_BANK_SZ;
   u8 value;
   int ret;
 
   dev_dbg(dev, "%s offset(%d) val(%d)\n", __func__, offset, val);
   ret = nca9539_read_reg(dev, NCA9539_REG_CONFIG_BASE + port, &value);
   if (ret < 0 || !!(BIT(pin) & value)) {
       dev_warn(dev, "%s offset(%d) val(%d) check config failed\n",
           __func__, offset, val);
   }
 
   ret = nca9539_read_reg(dev, NCA9539_REG_OUTPUT_PORT_BASE + port,
                  &value);
   if (!ret) {
       value &= ~BIT(pin);
       value |= val ? BIT(pin) : 0;
       ret = nca9539_write_reg(
           dev, NCA9539_REG_OUTPUT_PORT_BASE + port, value);
   }
   if (ret < 0) {
       dev_err(dev, "%s offset(%d) val(%d) read input failed\n",
           __func__, offset, val);
   }
 
   return ret;
}
 
static int nca9539_get_function(struct udevice *dev, unsigned offset)
{
   return nca9539_gpio_get_direction(dev, offset);
}
 
static int nca9539_xlate(struct udevice *dev, struct gpio_desc *desc,
            struct ofnode_phandle_args *args)
{
   desc->offset = args->args[0];
   desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
 
   return 0;
}
 
static const struct dm_gpio_ops nca9539_ops = {
   .direction_input = nca9539_gpio_direction_input,
   .direction_output = nca9539_gpio_direction_output,
   .get_value = nca9539_gpio_get,
   .set_value = nca9539_gpio_set,
   .get_function = nca9539_get_function,
   .xlate = nca9539_xlate,
};
 
static int nca9539_probe(struct udevice *dev)
{
   struct nca9539_info *info = dev_get_platdata(dev);
   struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
   struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
   char name[32], *str;
   ulong driver_data;
 
   if (!info) {
       dev_err(dev, "platdata not ready\n");
       return -ENOMEM;
   }
 
   if (!chip) {
       dev_err(dev, "i2c not ready\n");
       return -ENODEV;
   }
 
#if CONFIG_IS_ENABLED(OF_CONTROL)
   info->addr = chip->chip_addr;
#else
   info->addr = NCA9539_CHIP_ADDR;
#endif
 
   driver_data = dev_get_driver_data(dev);
   info->ngpio = driver_data;
   if (info->ngpio > NCA9539_MAX_BANK * NCA9539_BANK_SZ) {
       dev_err(dev, "Max support %d pins now\n",
           NCA9539_MAX_BANK * NCA9539_BANK_SZ);
       return -EINVAL;
   }
 
   snprintf(name, sizeof(name), "gpio@%x_", info->addr);
   str = strdup(name);
   if (!str)
       return -ENOMEM;
   uc_priv->bank_name = str;
   uc_priv->gpio_count = info->ngpio;
 
   dev_dbg(dev, "%s is ready\n", str);
 
   return 0;
}
 
static const struct udevice_id nca9539_ids[] = {
   {
       .compatible = "novo,nca9539-gpio",
       .data = (ulong)16,
   },
   { /* sentinel */ },
};
 
U_BOOT_DRIVER(nca9539) = {
   .name = "nca9539",
   .id = UCLASS_GPIO,
   .ops = &nca9539_ops,
   .probe = nca9539_probe,
   .platdata_auto_alloc_size = sizeof(struct nca9539_info),
   .of_match = nca9539_ids,
};