blob: 10ae86ec5d457431770ae78442119cc4ef5ea539 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Vignesh R0caf07c2016-08-02 10:14:24 +05302/*
3 * PCF8575 I2C GPIO EXPANDER DRIVER
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/
Vignesh R0caf07c2016-08-02 10:14:24 +05306 *
7 * Vignesh R <vigneshr@ti.com>
8 *
Vignesh R0caf07c2016-08-02 10:14:24 +05309 *
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 *
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020015 * Add support for 8 bit expanders - like pca8574
16 * Copyright (C) 2021 Lukasz Majewski - DENX Software Engineering
Vignesh R0caf07c2016-08-02 10:14:24 +053017 *
Vignesh R0caf07c2016-08-02 10:14:24 +053018 */
19
Vignesh R0caf07c2016-08-02 10:14:24 +053020#include <dm.h>
21#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060022#include <log.h>
Vignesh R0caf07c2016-08-02 10:14:24 +053023#include <asm-generic/gpio.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Vignesh R0caf07c2016-08-02 10:14:24 +053026
27DECLARE_GLOBAL_DATA_PTR;
28
29struct pcf8575_chip {
Vignesh R0caf07c2016-08-02 10:14:24 +053030 /* 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 */
Vignesh R0caf07c2016-08-02 10:14:24 +053043};
44
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020045/* Read/Write to I/O expander */
Vignesh R0caf07c2016-08-02 10:14:24 +053046
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020047static int pcf8575_i2c_write(struct udevice *dev, unsigned int word)
Vignesh R0caf07c2016-08-02 10:14:24 +053048{
Simon Glass71fa5b42020-12-03 16:55:18 -070049 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Vignesh R0caf07c2016-08-02 10:14:24 +053050 u8 buf[2] = { word & 0xff, word >> 8, };
51 int ret;
52
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020053 ret = dm_i2c_write(dev, 0, buf, dev_get_driver_data(dev));
Vignesh R0caf07c2016-08-02 10:14:24 +053054 if (ret)
55 printf("%s i2c write failed to addr %x\n", __func__,
56 chip->chip_addr);
57
58 return ret;
59}
60
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020061static int pcf8575_i2c_read(struct udevice *dev)
Vignesh R0caf07c2016-08-02 10:14:24 +053062{
Simon Glass71fa5b42020-12-03 16:55:18 -070063 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020064 u8 buf[2] = {0x00, 0x00};
Vignesh R0caf07c2016-08-02 10:14:24 +053065 int ret;
66
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020067 ret = dm_i2c_read(dev, 0, buf, dev_get_driver_data(dev));
Vignesh R0caf07c2016-08-02 10:14:24 +053068 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
77static int pcf8575_direction_input(struct udevice *dev, unsigned offset)
78{
Simon Glassfa20e932020-12-03 16:55:20 -070079 struct pcf8575_chip *plat = dev_get_plat(dev);
Vignesh R0caf07c2016-08-02 10:14:24 +053080 int status;
81
82 plat->out |= BIT(offset);
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020083 status = pcf8575_i2c_write(dev, plat->out);
Vignesh R0caf07c2016-08-02 10:14:24 +053084
85 return status;
86}
87
88static int pcf8575_direction_output(struct udevice *dev,
89 unsigned int offset, int value)
90{
Simon Glassfa20e932020-12-03 16:55:20 -070091 struct pcf8575_chip *plat = dev_get_plat(dev);
Vignesh R0caf07c2016-08-02 10:14:24 +053092 int ret;
93
94 if (value)
95 plat->out |= BIT(offset);
96 else
97 plat->out &= ~BIT(offset);
98
Lukasz Majewski18fcbb62021-06-07 14:26:34 +020099 ret = pcf8575_i2c_write(dev, plat->out);
Vignesh R0caf07c2016-08-02 10:14:24 +0530100
101 return ret;
102}
103
104static int pcf8575_get_value(struct udevice *dev, unsigned int offset)
105{
106 int value;
107
Lukasz Majewski18fcbb62021-06-07 14:26:34 +0200108 value = pcf8575_i2c_read(dev);
Vignesh R0caf07c2016-08-02 10:14:24 +0530109
110 return (value < 0) ? value : ((value & BIT(offset)) >> offset);
111}
112
113static int pcf8575_set_value(struct udevice *dev, unsigned int offset,
114 int value)
115{
116 return pcf8575_direction_output(dev, offset, value);
117}
118
Simon Glassb75b15b2020-12-03 16:55:23 -0700119static int pcf8575_ofdata_plat(struct udevice *dev)
Vignesh R0caf07c2016-08-02 10:14:24 +0530120{
Simon Glassfa20e932020-12-03 16:55:20 -0700121 struct pcf8575_chip *plat = dev_get_plat(dev);
Vignesh R0caf07c2016-08-02 10:14:24 +0530122 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
123
124 int n_latch;
125
Lukasz Majewski18fcbb62021-06-07 14:26:34 +0200126 /*
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;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700131 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Vignesh R0caf07c2016-08-02 10:14:24 +0530132 "gpio-bank-name", NULL);
133 if (!uc_priv->bank_name)
134 uc_priv->bank_name = fdt_get_name(gd->fdt_blob,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700135 dev_of_offset(dev), NULL);
Vignesh R0caf07c2016-08-02 10:14:24 +0530136
Simon Glassdd79d6e2017-01-17 16:52:55 -0700137 n_latch = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
Vignesh R0caf07c2016-08-02 10:14:24 +0530138 "lines-initial-states", 0);
139 plat->out = ~n_latch;
140
141 return 0;
142}
143
144static 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
154static 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
161static const struct udevice_id pcf8575_gpio_ids[] = {
Lukasz Majewski18fcbb62021-06-07 14:26:34 +0200162 { .compatible = "nxp,pcf8575", .data = 2 },
163 { .compatible = "ti,pcf8575", .data = 2 },
164 { .compatible = "nxp,pca8574", .data = 1 },
Vignesh R0caf07c2016-08-02 10:14:24 +0530165 { }
166};
167
168U_BOOT_DRIVER(gpio_pcf8575) = {
169 .name = "gpio_pcf8575",
170 .id = UCLASS_GPIO,
171 .ops = &pcf8575_gpio_ops,
172 .of_match = pcf8575_gpio_ids,
Simon Glassb75b15b2020-12-03 16:55:23 -0700173 .of_to_plat = pcf8575_ofdata_plat,
Vignesh R0caf07c2016-08-02 10:14:24 +0530174 .probe = pcf8575_gpio_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700175 .plat_auto = sizeof(struct pcf8575_chip),
Vignesh R0caf07c2016-08-02 10:14:24 +0530176};