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