Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference. |
| 3 | * |
| 4 | * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Note: |
| 12 | * The driver's compatible table is borrowed from Linux Kernel, |
| 13 | * but now max supported gpio pins is 24 and only PCA953X_TYPE |
| 14 | * is supported. PCA957X_TYPE is not supported now. |
| 15 | * Also the Polarity Inversion feature is not supported now. |
| 16 | * |
| 17 | * TODO: |
| 18 | * 1. Support PCA957X_TYPE |
mario.six@gdsys.cc | 71db327 | 2016-04-25 15:25:37 +0200 | [diff] [blame] | 19 | * 2. Support 24 gpio pins |
| 20 | * 3. Support Polarity Inversion |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <errno.h> |
| 25 | #include <dm.h> |
| 26 | #include <fdtdec.h> |
| 27 | #include <i2c.h> |
| 28 | #include <malloc.h> |
| 29 | #include <asm/gpio.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <dt-bindings/gpio/gpio.h> |
| 32 | |
| 33 | #define PCA953X_INPUT 0 |
| 34 | #define PCA953X_OUTPUT 1 |
| 35 | #define PCA953X_INVERT 2 |
| 36 | #define PCA953X_DIRECTION 3 |
| 37 | |
| 38 | #define PCA_GPIO_MASK 0x00FF |
| 39 | #define PCA_INT 0x0100 |
| 40 | #define PCA953X_TYPE 0x1000 |
| 41 | #define PCA957X_TYPE 0x2000 |
| 42 | #define PCA_TYPE_MASK 0xF000 |
| 43 | #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK) |
| 44 | |
| 45 | enum { |
| 46 | PCA953X_DIRECTION_IN, |
| 47 | PCA953X_DIRECTION_OUT, |
| 48 | }; |
| 49 | |
mario.six@gdsys.cc | 71db327 | 2016-04-25 15:25:37 +0200 | [diff] [blame] | 50 | #define MAX_BANK 5 |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 51 | #define BANK_SZ 8 |
| 52 | |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 53 | /* |
| 54 | * struct pca953x_info - Data for pca953x |
| 55 | * |
| 56 | * @dev: udevice structure for the device |
| 57 | * @addr: i2c slave address |
| 58 | * @invert: Polarity inversion or not |
| 59 | * @gpio_count: the number of gpio pins that the device supports |
| 60 | * @chip_type: indicate the chip type,PCA953X or PCA957X |
| 61 | * @bank_count: the number of banks that the device supports |
| 62 | * @reg_output: array to hold the value of output registers |
| 63 | * @reg_direction: array to hold the value of direction registers |
| 64 | */ |
| 65 | struct pca953x_info { |
| 66 | struct udevice *dev; |
| 67 | int addr; |
| 68 | int invert; |
| 69 | int gpio_count; |
| 70 | int chip_type; |
| 71 | int bank_count; |
| 72 | u8 reg_output[MAX_BANK]; |
| 73 | u8 reg_direction[MAX_BANK]; |
| 74 | }; |
| 75 | |
| 76 | static int pca953x_write_single(struct udevice *dev, int reg, u8 val, |
| 77 | int offset) |
| 78 | { |
| 79 | struct pca953x_info *info = dev_get_platdata(dev); |
| 80 | int bank_shift = fls((info->gpio_count - 1) / BANK_SZ); |
| 81 | int off = offset / BANK_SZ; |
| 82 | int ret = 0; |
| 83 | |
| 84 | ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1); |
| 85 | if (ret) { |
| 86 | dev_err(dev, "%s error\n", __func__); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int pca953x_read_single(struct udevice *dev, int reg, u8 *val, |
| 94 | int offset) |
| 95 | { |
| 96 | struct pca953x_info *info = dev_get_platdata(dev); |
| 97 | int bank_shift = fls((info->gpio_count - 1) / BANK_SZ); |
| 98 | int off = offset / BANK_SZ; |
| 99 | int ret; |
| 100 | u8 byte; |
| 101 | |
| 102 | ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1); |
| 103 | if (ret) { |
| 104 | dev_err(dev, "%s error\n", __func__); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | *val = byte; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val) |
| 114 | { |
| 115 | struct pca953x_info *info = dev_get_platdata(dev); |
| 116 | int ret = 0; |
| 117 | |
| 118 | if (info->gpio_count <= 8) { |
| 119 | ret = dm_i2c_read(dev, reg, val, 1); |
| 120 | } else if (info->gpio_count <= 16) { |
| 121 | ret = dm_i2c_read(dev, reg << 1, val, info->bank_count); |
mario.six@gdsys.cc | 71db327 | 2016-04-25 15:25:37 +0200 | [diff] [blame] | 122 | } else if (info->gpio_count == 40) { |
| 123 | /* Auto increment */ |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 124 | ret = dm_i2c_read(dev, (reg << 3) | 0x80, val, |
| 125 | info->bank_count); |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 126 | } else { |
| 127 | dev_err(dev, "Unsupported now\n"); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static int pca953x_is_output(struct udevice *dev, int offset) |
| 135 | { |
| 136 | struct pca953x_info *info = dev_get_platdata(dev); |
| 137 | |
| 138 | int bank = offset / BANK_SZ; |
| 139 | int off = offset % BANK_SZ; |
| 140 | |
| 141 | /*0: output; 1: input */ |
| 142 | return !(info->reg_direction[bank] & (1 << off)); |
| 143 | } |
| 144 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 145 | static int pca953x_get_value(struct udevice *dev, uint offset) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 146 | { |
| 147 | int ret; |
| 148 | u8 val = 0; |
| 149 | |
mario.six@gdsys.cc | fc76b69 | 2016-05-23 09:54:56 +0200 | [diff] [blame] | 150 | int off = offset % BANK_SZ; |
| 151 | |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 152 | ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
mario.six@gdsys.cc | fc76b69 | 2016-05-23 09:54:56 +0200 | [diff] [blame] | 156 | return (val >> off) & 0x1; |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 159 | static int pca953x_set_value(struct udevice *dev, uint offset, int value) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 160 | { |
| 161 | struct pca953x_info *info = dev_get_platdata(dev); |
| 162 | int bank = offset / BANK_SZ; |
| 163 | int off = offset % BANK_SZ; |
| 164 | u8 val; |
| 165 | int ret; |
| 166 | |
| 167 | if (value) |
| 168 | val = info->reg_output[bank] | (1 << off); |
| 169 | else |
| 170 | val = info->reg_output[bank] & ~(1 << off); |
| 171 | |
| 172 | ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | info->reg_output[bank] = val; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 181 | static int pca953x_set_direction(struct udevice *dev, uint offset, int dir) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 182 | { |
| 183 | struct pca953x_info *info = dev_get_platdata(dev); |
| 184 | int bank = offset / BANK_SZ; |
| 185 | int off = offset % BANK_SZ; |
| 186 | u8 val; |
| 187 | int ret; |
| 188 | |
| 189 | if (dir == PCA953X_DIRECTION_IN) |
| 190 | val = info->reg_direction[bank] | (1 << off); |
| 191 | else |
| 192 | val = info->reg_direction[bank] & ~(1 << off); |
| 193 | |
| 194 | ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
| 198 | info->reg_direction[bank] = val; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 203 | static int pca953x_direction_input(struct udevice *dev, uint offset) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 204 | { |
| 205 | return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN); |
| 206 | } |
| 207 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 208 | static int pca953x_direction_output(struct udevice *dev, uint offset, int value) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 209 | { |
| 210 | /* Configure output value. */ |
| 211 | pca953x_set_value(dev, offset, value); |
| 212 | |
| 213 | /* Configure direction as output. */ |
| 214 | pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 219 | static int pca953x_get_function(struct udevice *dev, uint offset) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 220 | { |
| 221 | if (pca953x_is_output(dev, offset)) |
| 222 | return GPIOF_OUTPUT; |
| 223 | else |
| 224 | return GPIOF_INPUT; |
| 225 | } |
| 226 | |
| 227 | static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 228 | struct ofnode_phandle_args *args) |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 229 | { |
| 230 | desc->offset = args->args[0]; |
Mario Six | fb01e07 | 2018-01-15 11:07:44 +0100 | [diff] [blame] | 231 | desc->flags = args->args[1] & (GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0); |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static const struct dm_gpio_ops pca953x_ops = { |
| 237 | .direction_input = pca953x_direction_input, |
| 238 | .direction_output = pca953x_direction_output, |
| 239 | .get_value = pca953x_get_value, |
| 240 | .set_value = pca953x_set_value, |
| 241 | .get_function = pca953x_get_function, |
| 242 | .xlate = pca953x_xlate, |
| 243 | }; |
| 244 | |
| 245 | static int pca953x_probe(struct udevice *dev) |
| 246 | { |
| 247 | struct pca953x_info *info = dev_get_platdata(dev); |
| 248 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Mario Six | 1e5f898 | 2018-03-01 14:45:04 +0100 | [diff] [blame^] | 249 | char name[32], label[8], *str; |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 250 | int addr; |
| 251 | ulong driver_data; |
| 252 | int ret; |
Mario Six | 1e5f898 | 2018-03-01 14:45:04 +0100 | [diff] [blame^] | 253 | int size; |
| 254 | const u8 *tmp; |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 255 | |
Mario Six | f62ca2cd | 2018-01-15 11:07:45 +0100 | [diff] [blame] | 256 | addr = dev_read_addr(dev); |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 257 | if (addr == 0) |
| 258 | return -ENODEV; |
| 259 | |
| 260 | info->addr = addr; |
| 261 | |
| 262 | driver_data = dev_get_driver_data(dev); |
| 263 | |
| 264 | info->gpio_count = driver_data & PCA_GPIO_MASK; |
| 265 | if (info->gpio_count > MAX_BANK * BANK_SZ) { |
| 266 | dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | info->chip_type = PCA_CHIP_TYPE(driver_data); |
| 271 | if (info->chip_type != PCA953X_TYPE) { |
| 272 | dev_err(dev, "Only support PCA953X chip type now.\n"); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ); |
| 277 | |
| 278 | ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output); |
| 279 | if (ret) { |
| 280 | dev_err(dev, "Error reading output register\n"); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction); |
| 285 | if (ret) { |
| 286 | dev_err(dev, "Error reading direction register\n"); |
| 287 | return ret; |
| 288 | } |
| 289 | |
Mario Six | 1e5f898 | 2018-03-01 14:45:04 +0100 | [diff] [blame^] | 290 | tmp = dev_read_prop(dev, "label", &size); |
| 291 | |
| 292 | if (tmp) { |
| 293 | memcpy(label, tmp, sizeof(label) - 1); |
| 294 | label[sizeof(label) - 1] = '\0'; |
| 295 | snprintf(name, sizeof(name), "%s@%x_", label, info->addr); |
| 296 | } else { |
| 297 | snprintf(name, sizeof(name), "gpio@%x_", info->addr); |
| 298 | } |
| 299 | |
Peng Fan | 0377343 | 2016-04-14 21:45:06 +0800 | [diff] [blame] | 300 | str = strdup(name); |
| 301 | if (!str) |
| 302 | return -ENOMEM; |
| 303 | uc_priv->bank_name = str; |
| 304 | uc_priv->gpio_count = info->gpio_count; |
| 305 | |
| 306 | dev_dbg(dev, "%s is ready\n", str); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int) |
| 312 | #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int) |
| 313 | |
| 314 | static const struct udevice_id pca953x_ids[] = { |
| 315 | { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), }, |
| 316 | { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), }, |
| 317 | { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), }, |
| 318 | { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), }, |
| 319 | { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), }, |
| 320 | { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), }, |
| 321 | { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), }, |
| 322 | { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), }, |
| 323 | { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), }, |
| 324 | { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), }, |
| 325 | { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), }, |
| 326 | { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), }, |
| 327 | { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), }, |
| 328 | { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), }, |
| 329 | |
| 330 | { .compatible = "maxim,max7310", .data = OF_953X(8, 0), }, |
| 331 | { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), }, |
| 332 | { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), }, |
| 333 | { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), }, |
| 334 | |
| 335 | { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), }, |
| 336 | { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), }, |
| 337 | { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), }, |
| 338 | { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), }, |
| 339 | |
| 340 | { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), }, |
| 341 | |
| 342 | { .compatible = "exar,xra1202", .data = OF_953X(8, 0), }, |
| 343 | { } |
| 344 | }; |
| 345 | |
| 346 | U_BOOT_DRIVER(pca953x) = { |
| 347 | .name = "pca953x", |
| 348 | .id = UCLASS_GPIO, |
| 349 | .ops = &pca953x_ops, |
| 350 | .probe = pca953x_probe, |
| 351 | .platdata_auto_alloc_size = sizeof(struct pca953x_info), |
| 352 | .of_match = pca953x_ids, |
| 353 | }; |