blob: e98e1e56dbcd6f3bd33bf794006355da4c101a62 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fan03773432016-04-14 21:45:06 +08002/*
3 * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
4 *
5 * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
6 *
Peng Fan03773432016-04-14 21:45:06 +08007 */
8
9/*
10 * Note:
11 * The driver's compatible table is borrowed from Linux Kernel,
12 * but now max supported gpio pins is 24 and only PCA953X_TYPE
13 * is supported. PCA957X_TYPE is not supported now.
14 * Also the Polarity Inversion feature is not supported now.
15 *
16 * TODO:
17 * 1. Support PCA957X_TYPE
Vignesh Raghavendradd6638a2020-01-27 23:19:00 +053018 * 2. Support Polarity Inversion
Peng Fan03773432016-04-14 21:45:06 +080019 */
20
21#include <common.h>
22#include <errno.h>
23#include <dm.h>
24#include <fdtdec.h>
25#include <i2c.h>
26#include <malloc.h>
27#include <asm/gpio.h>
28#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070029#include <dm/device_compat.h>
Peng Fan03773432016-04-14 21:45:06 +080030#include <dt-bindings/gpio/gpio.h>
Simon Glasscd93d622020-05-10 11:40:13 -060031#include <linux/bitops.h>
Peng Fan03773432016-04-14 21:45:06 +080032
33#define PCA953X_INPUT 0
34#define PCA953X_OUTPUT 1
35#define PCA953X_INVERT 2
36#define PCA953X_DIRECTION 3
37
Luca Elleroc170fe02022-03-22 15:38:36 +010038#define PCA957X_INPUT 0
39#define PCA957X_OUTPUT 5
40#define PCA957X_INVERT 1
41#define PCA957X_DIRECTION 4
42
43
Peng Fan03773432016-04-14 21:45:06 +080044#define PCA_GPIO_MASK 0x00FF
45#define PCA_INT 0x0100
46#define PCA953X_TYPE 0x1000
47#define PCA957X_TYPE 0x2000
48#define PCA_TYPE_MASK 0xF000
49#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
50
51enum {
52 PCA953X_DIRECTION_IN,
53 PCA953X_DIRECTION_OUT,
54};
55
mario.six@gdsys.cc71db3272016-04-25 15:25:37 +020056#define MAX_BANK 5
Peng Fan03773432016-04-14 21:45:06 +080057#define BANK_SZ 8
58
Luca Elleroc170fe02022-03-22 15:38:36 +010059struct pca95xx_reg {
60 int input;
61 int output;
62 int invert;
63 int direction;
64};
65
66static const struct pca95xx_reg pca953x_regs = {
67 .direction = PCA953X_DIRECTION,
68 .output = PCA953X_OUTPUT,
69 .input = PCA953X_INPUT,
70 .invert = PCA953X_INVERT,
71};
72
73static const struct pca95xx_reg pca957x_regs = {
74 .direction = PCA957X_DIRECTION,
75 .output = PCA957X_OUTPUT,
76 .input = PCA957X_INPUT,
77 .invert = PCA957X_INVERT,
78};
79
Peng Fan03773432016-04-14 21:45:06 +080080/*
Luca Elleroc170fe02022-03-22 15:38:36 +010081 * struct pca953x_info - Data for pca953x/pca957x
Peng Fan03773432016-04-14 21:45:06 +080082 *
83 * @dev: udevice structure for the device
84 * @addr: i2c slave address
85 * @invert: Polarity inversion or not
86 * @gpio_count: the number of gpio pins that the device supports
87 * @chip_type: indicate the chip type,PCA953X or PCA957X
88 * @bank_count: the number of banks that the device supports
89 * @reg_output: array to hold the value of output registers
90 * @reg_direction: array to hold the value of direction registers
Luca Elleroc170fe02022-03-22 15:38:36 +010091 * @regs: struct to hold the registers addresses
Peng Fan03773432016-04-14 21:45:06 +080092 */
93struct pca953x_info {
94 struct udevice *dev;
95 int addr;
96 int invert;
97 int gpio_count;
98 int chip_type;
99 int bank_count;
100 u8 reg_output[MAX_BANK];
101 u8 reg_direction[MAX_BANK];
Luca Elleroc170fe02022-03-22 15:38:36 +0100102 const struct pca95xx_reg *regs;
Peng Fan03773432016-04-14 21:45:06 +0800103};
104
105static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
106 int offset)
107{
Simon Glassc69cda22020-12-03 16:55:20 -0700108 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800109 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
110 int off = offset / BANK_SZ;
111 int ret = 0;
112
113 ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
114 if (ret) {
115 dev_err(dev, "%s error\n", __func__);
116 return ret;
117 }
118
119 return 0;
120}
121
122static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
123 int offset)
124{
Simon Glassc69cda22020-12-03 16:55:20 -0700125 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800126 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
127 int off = offset / BANK_SZ;
128 int ret;
129 u8 byte;
130
131 ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
132 if (ret) {
133 dev_err(dev, "%s error\n", __func__);
134 return ret;
135 }
136
137 *val = byte;
138
139 return 0;
140}
141
142static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
143{
Simon Glassc69cda22020-12-03 16:55:20 -0700144 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800145 int ret = 0;
146
147 if (info->gpio_count <= 8) {
148 ret = dm_i2c_read(dev, reg, val, 1);
149 } else if (info->gpio_count <= 16) {
150 ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
Vignesh Raghavendradd6638a2020-01-27 23:19:00 +0530151 } else if (info->gpio_count <= 24) {
152 /* Auto increment */
153 ret = dm_i2c_read(dev, (reg << 2) | 0x80, val,
154 info->bank_count);
mario.six@gdsys.cc71db3272016-04-25 15:25:37 +0200155 } else if (info->gpio_count == 40) {
156 /* Auto increment */
Mario Sixfb01e072018-01-15 11:07:44 +0100157 ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
158 info->bank_count);
Peng Fan03773432016-04-14 21:45:06 +0800159 } else {
160 dev_err(dev, "Unsupported now\n");
161 return -EINVAL;
162 }
163
164 return ret;
165}
166
Ye Li3764b2b2018-10-18 16:16:46 +0200167static int pca953x_write_regs(struct udevice *dev, int reg, u8 *val)
168{
Simon Glassc69cda22020-12-03 16:55:20 -0700169 struct pca953x_info *info = dev_get_plat(dev);
Ye Li3764b2b2018-10-18 16:16:46 +0200170 int ret = 0;
171
172 if (info->gpio_count <= 8) {
173 ret = dm_i2c_write(dev, reg, val, 1);
174 } else if (info->gpio_count <= 16) {
175 ret = dm_i2c_write(dev, reg << 1, val, info->bank_count);
Vignesh Raghavendradd6638a2020-01-27 23:19:00 +0530176 } else if (info->gpio_count <= 24) {
177 /* Auto increment */
178 ret = dm_i2c_write(dev, (reg << 2) | 0x80, val,
179 info->bank_count);
Ye Li3764b2b2018-10-18 16:16:46 +0200180 } else if (info->gpio_count == 40) {
181 /* Auto increment */
182 ret = dm_i2c_write(dev, (reg << 3) | 0x80, val, info->bank_count);
183 } else {
184 return -EINVAL;
185 }
186
187 return ret;
188}
189
Peng Fan03773432016-04-14 21:45:06 +0800190static int pca953x_is_output(struct udevice *dev, int offset)
191{
Simon Glassc69cda22020-12-03 16:55:20 -0700192 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800193
194 int bank = offset / BANK_SZ;
195 int off = offset % BANK_SZ;
196
197 /*0: output; 1: input */
198 return !(info->reg_direction[bank] & (1 << off));
199}
200
Mario Sixfb01e072018-01-15 11:07:44 +0100201static int pca953x_get_value(struct udevice *dev, uint offset)
Peng Fan03773432016-04-14 21:45:06 +0800202{
Luca Elleroc170fe02022-03-22 15:38:36 +0100203 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800204 int ret;
205 u8 val = 0;
206
mario.six@gdsys.ccfc76b692016-05-23 09:54:56 +0200207 int off = offset % BANK_SZ;
208
Luca Elleroc170fe02022-03-22 15:38:36 +0100209 ret = pca953x_read_single(dev, info->regs->input, &val, offset);
Peng Fan03773432016-04-14 21:45:06 +0800210 if (ret)
211 return ret;
212
mario.six@gdsys.ccfc76b692016-05-23 09:54:56 +0200213 return (val >> off) & 0x1;
Peng Fan03773432016-04-14 21:45:06 +0800214}
215
Mario Sixfb01e072018-01-15 11:07:44 +0100216static int pca953x_set_value(struct udevice *dev, uint offset, int value)
Peng Fan03773432016-04-14 21:45:06 +0800217{
Simon Glassc69cda22020-12-03 16:55:20 -0700218 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800219 int bank = offset / BANK_SZ;
220 int off = offset % BANK_SZ;
221 u8 val;
222 int ret;
223
224 if (value)
225 val = info->reg_output[bank] | (1 << off);
226 else
227 val = info->reg_output[bank] & ~(1 << off);
228
Luca Elleroc170fe02022-03-22 15:38:36 +0100229 ret = pca953x_write_single(dev, info->regs->output, val, offset);
Peng Fan03773432016-04-14 21:45:06 +0800230 if (ret)
231 return ret;
232
233 info->reg_output[bank] = val;
234
235 return 0;
236}
237
Mario Sixfb01e072018-01-15 11:07:44 +0100238static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
Peng Fan03773432016-04-14 21:45:06 +0800239{
Simon Glassc69cda22020-12-03 16:55:20 -0700240 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800241 int bank = offset / BANK_SZ;
242 int off = offset % BANK_SZ;
243 u8 val;
244 int ret;
245
246 if (dir == PCA953X_DIRECTION_IN)
247 val = info->reg_direction[bank] | (1 << off);
248 else
249 val = info->reg_direction[bank] & ~(1 << off);
250
Luca Elleroc170fe02022-03-22 15:38:36 +0100251 ret = pca953x_write_single(dev, info->regs->direction, val, offset);
Peng Fan03773432016-04-14 21:45:06 +0800252 if (ret)
253 return ret;
254
255 info->reg_direction[bank] = val;
256
257 return 0;
258}
259
Mario Sixfb01e072018-01-15 11:07:44 +0100260static int pca953x_direction_input(struct udevice *dev, uint offset)
Peng Fan03773432016-04-14 21:45:06 +0800261{
262 return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
263}
264
Mario Sixfb01e072018-01-15 11:07:44 +0100265static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
Peng Fan03773432016-04-14 21:45:06 +0800266{
267 /* Configure output value. */
268 pca953x_set_value(dev, offset, value);
269
270 /* Configure direction as output. */
271 pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
272
273 return 0;
274}
275
Mario Sixfb01e072018-01-15 11:07:44 +0100276static int pca953x_get_function(struct udevice *dev, uint offset)
Peng Fan03773432016-04-14 21:45:06 +0800277{
278 if (pca953x_is_output(dev, offset))
279 return GPIOF_OUTPUT;
280 else
281 return GPIOF_INPUT;
282}
283
284static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600285 struct ofnode_phandle_args *args)
Peng Fan03773432016-04-14 21:45:06 +0800286{
287 desc->offset = args->args[0];
Anatolij Gustschin745915a2018-10-18 16:15:39 +0200288 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
Peng Fan03773432016-04-14 21:45:06 +0800289
290 return 0;
291}
292
293static const struct dm_gpio_ops pca953x_ops = {
294 .direction_input = pca953x_direction_input,
295 .direction_output = pca953x_direction_output,
296 .get_value = pca953x_get_value,
297 .set_value = pca953x_set_value,
298 .get_function = pca953x_get_function,
299 .xlate = pca953x_xlate,
300};
301
302static int pca953x_probe(struct udevice *dev)
303{
Simon Glassc69cda22020-12-03 16:55:20 -0700304 struct pca953x_info *info = dev_get_plat(dev);
Peng Fan03773432016-04-14 21:45:06 +0800305 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Mario Six1e5f8982018-03-01 14:45:04 +0100306 char name[32], label[8], *str;
Peng Fan03773432016-04-14 21:45:06 +0800307 int addr;
308 ulong driver_data;
309 int ret;
Mario Six1e5f8982018-03-01 14:45:04 +0100310 int size;
311 const u8 *tmp;
Ye Li3764b2b2018-10-18 16:16:46 +0200312 u8 val[MAX_BANK];
Peng Fan03773432016-04-14 21:45:06 +0800313
Mario Sixf62ca2cd2018-01-15 11:07:45 +0100314 addr = dev_read_addr(dev);
Peng Fan03773432016-04-14 21:45:06 +0800315 if (addr == 0)
316 return -ENODEV;
317
318 info->addr = addr;
319
320 driver_data = dev_get_driver_data(dev);
321
322 info->gpio_count = driver_data & PCA_GPIO_MASK;
323 if (info->gpio_count > MAX_BANK * BANK_SZ) {
324 dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
325 return -EINVAL;
326 }
327
328 info->chip_type = PCA_CHIP_TYPE(driver_data);
Luca Elleroc170fe02022-03-22 15:38:36 +0100329 if (info->chip_type == PCA953X_TYPE)
330 info->regs = &pca953x_regs;
331 else
332 info->regs = &pca957x_regs;
Peng Fan03773432016-04-14 21:45:06 +0800333
334 info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
335
Luca Elleroc170fe02022-03-22 15:38:36 +0100336 ret = pca953x_read_regs(dev, info->regs->output, info->reg_output);
Peng Fan03773432016-04-14 21:45:06 +0800337 if (ret) {
338 dev_err(dev, "Error reading output register\n");
339 return ret;
340 }
341
342 ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
343 if (ret) {
344 dev_err(dev, "Error reading direction register\n");
345 return ret;
346 }
347
Mario Six1e5f8982018-03-01 14:45:04 +0100348 tmp = dev_read_prop(dev, "label", &size);
349
350 if (tmp) {
351 memcpy(label, tmp, sizeof(label) - 1);
352 label[sizeof(label) - 1] = '\0';
353 snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
354 } else {
355 snprintf(name, sizeof(name), "gpio@%x_", info->addr);
356 }
357
Ye Li3764b2b2018-10-18 16:16:46 +0200358 /* Clear the polarity registers to no invert */
359 memset(val, 0, MAX_BANK);
Luca Elleroc170fe02022-03-22 15:38:36 +0100360 ret = pca953x_write_regs(dev, info->regs->invert, val);
Ye Li3764b2b2018-10-18 16:16:46 +0200361 if (ret < 0) {
362 dev_err(dev, "Error writing invert register\n");
363 return ret;
364 }
365
Peng Fan03773432016-04-14 21:45:06 +0800366 str = strdup(name);
367 if (!str)
368 return -ENOMEM;
369 uc_priv->bank_name = str;
370 uc_priv->gpio_count = info->gpio_count;
371
372 dev_dbg(dev, "%s is ready\n", str);
373
374 return 0;
375}
376
377#define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
378#define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
379
380static const struct udevice_id pca953x_ids[] = {
381 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
382 { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
383 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
384 { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
385 { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
386 { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
387 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
388 { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
389 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
390 { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
391 { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
392 { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
393 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
394 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
395
396 { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
397 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
398 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
399 { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
400
401 { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
402 { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
403 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
404 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Marek Vasut4a098312019-05-25 22:52:20 +0200405 { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
Peng Fan03773432016-04-14 21:45:06 +0800406
407 { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
408
409 { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
410 { }
411};
412
413U_BOOT_DRIVER(pca953x) = {
414 .name = "pca953x",
415 .id = UCLASS_GPIO,
416 .ops = &pca953x_ops,
417 .probe = pca953x_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700418 .plat_auto = sizeof(struct pca953x_info),
Peng Fan03773432016-04-14 21:45:06 +0800419 .of_match = pca953x_ids,
420};