Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 2 | /* |
| 3 | * PCF8575 I2C GPIO EXPANDER DRIVER |
| 4 | * |
Nishanth Menon | a94a407 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 5 | * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/ |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 6 | * |
| 7 | * Vignesh R <vigneshr@ti.com> |
| 8 | * |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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 | * |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 15 | * Add support for 8 bit expanders - like pca8574 |
| 16 | * Copyright (C) 2021 Lukasz Majewski - DENX Software Engineering |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 17 | * |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 18 | */ |
| 19 | |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 20 | #include <dm.h> |
| 21 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 22 | #include <log.h> |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 23 | #include <asm-generic/gpio.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 24 | #include <asm/global_data.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | struct pcf8575_chip { |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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 */ |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 43 | }; |
| 44 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 45 | /* Read/Write to I/O expander */ |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 46 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 47 | static int pcf8575_i2c_write(struct udevice *dev, unsigned int word) |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 48 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 49 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 50 | u8 buf[2] = { word & 0xff, word >> 8, }; |
| 51 | int ret; |
| 52 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 53 | ret = dm_i2c_write(dev, 0, buf, dev_get_driver_data(dev)); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 54 | if (ret) |
| 55 | printf("%s i2c write failed to addr %x\n", __func__, |
| 56 | chip->chip_addr); |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 61 | static int pcf8575_i2c_read(struct udevice *dev) |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 62 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 63 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 64 | u8 buf[2] = {0x00, 0x00}; |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 65 | int ret; |
| 66 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 67 | ret = dm_i2c_read(dev, 0, buf, dev_get_driver_data(dev)); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 79 | struct pcf8575_chip *plat = dev_get_plat(dev); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 80 | int status; |
| 81 | |
| 82 | plat->out |= BIT(offset); |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 83 | status = pcf8575_i2c_write(dev, plat->out); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 84 | |
| 85 | return status; |
| 86 | } |
| 87 | |
| 88 | static int pcf8575_direction_output(struct udevice *dev, |
| 89 | unsigned int offset, int value) |
| 90 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 91 | struct pcf8575_chip *plat = dev_get_plat(dev); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 92 | int ret; |
| 93 | |
| 94 | if (value) |
| 95 | plat->out |= BIT(offset); |
| 96 | else |
| 97 | plat->out &= ~BIT(offset); |
| 98 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 99 | ret = pcf8575_i2c_write(dev, plat->out); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int pcf8575_get_value(struct udevice *dev, unsigned int offset) |
| 105 | { |
| 106 | int value; |
| 107 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 108 | value = pcf8575_i2c_read(dev); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 119 | static int pcf8575_ofdata_plat(struct udevice *dev) |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 120 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 121 | struct pcf8575_chip *plat = dev_get_plat(dev); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 122 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 123 | |
| 124 | int n_latch; |
| 125 | |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 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; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 131 | uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 132 | "gpio-bank-name", NULL); |
| 133 | if (!uc_priv->bank_name) |
| 134 | uc_priv->bank_name = fdt_get_name(gd->fdt_blob, |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 135 | dev_of_offset(dev), NULL); |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 136 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 137 | n_latch = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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[] = { |
Lukasz Majewski | 2132fce | 2021-06-07 14:26:34 +0200 | [diff] [blame] | 162 | { .compatible = "nxp,pcf8575", .data = 2 }, |
| 163 | { .compatible = "ti,pcf8575", .data = 2 }, |
| 164 | { .compatible = "nxp,pca8574", .data = 1 }, |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 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, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 173 | .of_to_plat = pcf8575_ofdata_plat, |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 174 | .probe = pcf8575_gpio_probe, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 175 | .plat_auto = sizeof(struct pcf8575_chip), |
Vignesh R | 5746b0d | 2016-08-02 10:14:24 +0530 | [diff] [blame] | 176 | }; |