Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | /* * Copyright (c) 2015 Intel Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file Driver for PCAL9535A I2C-based GPIO driver. */ #include <errno.h> #include <nanokernel.h> #include <misc/util.h> #include <gpio.h> #include <i2c.h> #include "gpio_pcal9535a.h" #define SYS_LOG_LEVEL CONFIG_SYS_LOG_GPIO_PCAL9535A_LEVEL #include <misc/sys_log.h> /* Register definitions */ #define REG_INPUT_PORT0 0x00 #define REG_INPUT_PORT1 0x01 #define REG_OUTPUT_PORT0 0x02 #define REG_OUTPUT_PORT1 0x03 #define REG_POL_INV_PORT0 0x04 #define REG_POL_INV_PORT1 0x05 #define REG_CONF_PORT0 0x06 #define REG_CONG_PORT1 0x07 #define REG_OUT_DRV_STRENGTH_PORT0_L 0x40 #define REG_OUT_DRV_STRENGTH_PORT0_H 0x41 #define REG_OUT_DRV_STRENGTH_PORT1_L 0x42 #define REG_OUT_DRV_STRENGTH_PORT1_H 0x43 #define REG_INPUT_LATCH_PORT0 0x44 #define REG_INPUT_LATCH_PORT1 0x45 #define REG_PUD_EN_PORT0 0x46 #define REG_PUD_EN_PORT1 0x47 #define REG_PUD_SEL_PORT0 0x48 #define REG_PUD_SEL_PORT1 0x49 #define REG_INT_MASK_PORT0 0x4A #define REG_INT_MASK_PORT1 0x4B #define REG_INT_STATUS_PORT0 0x4C #define REG_INT_STATUS_PORT1 0x4D #define REG_OUTPUT_PORT_CONF 0x4F /** * @brief Check to see if a I2C master is identified for communication. * * @param dev Device struct. * @return 1 if I2C master is identified, 0 if not. */ static inline int _has_i2c_master(struct device *dev) { struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; struct device * const i2c_master = drv_data->i2c_master; if (i2c_master) return 1; else return 0; } /** * @brief Read both port 0 and port 1 registers of certain register function. * * Given the register in reg, read the pair of port 0 and port 1. * * @param dev Device struct of the PCAL9535A. * @param reg Register to read (the PORT0 of the pair of registers). * @param buf Buffer to read data into. * * @return 0 if successful, failed otherwise. */ static int _read_port_regs(struct device *dev, uint8_t reg, union gpio_pcal9535a_port_data *buf) { const struct gpio_pcal9535a_config * const config = dev->config->config_info; struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; struct device * const i2c_master = drv_data->i2c_master; uint16_t i2c_addr = config->i2c_slave_addr; int ret; ret = i2c_burst_read(i2c_master, i2c_addr, reg, buf->byte, 2); if (ret) { SYS_LOG_ERR("PCAL9535A[0x%X]: error reading register 0x%X (%d)", i2c_addr, reg, ret); goto error; } SYS_LOG_DBG("PCAL9535A[0x%X]: Read: REG[0x%X] = 0x%X, REG[0x%X] = 0x%X", i2c_addr, reg, buf->byte[0], (reg + 1), buf->byte[1]); error: return ret; } /** * @brief Write both port 0 and port 1 registers of certain register function. * * Given the register in reg, write the pair of port 0 and port 1. * * @param dev Device struct of the PCAL9535A. * @param reg Register to write into (the PORT0 of the pair of registers). * @param buf Buffer to write data from. * * @return 0 if successful, failed otherwise. */ static int _write_port_regs(struct device *dev, uint8_t reg, union gpio_pcal9535a_port_data *buf) { const struct gpio_pcal9535a_config * const config = dev->config->config_info; struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; struct device * const i2c_master = drv_data->i2c_master; uint16_t i2c_addr = config->i2c_slave_addr; int ret; SYS_LOG_DBG("PCAL9535A[0x%X]: Write: REG[0x%X] = 0x%X, REG[0x%X] = " "0x%X", i2c_addr, reg, buf->byte[0], (reg + 1), buf->byte[1]); ret = i2c_burst_write(i2c_master, i2c_addr, reg, buf->byte, 2); if (ret) { SYS_LOG_ERR("PCAL9535A[0x%X]: error writing from register 0x%X " "(%d)", i2c_addr, reg, ret); } return ret; } /** * @brief Setup the pin direction (input or output) * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param flags Flags of pin or port * * @return 0 if successful, failed otherwise */ static int _setup_pin_dir(struct device *dev, int access_op, uint32_t pin, int flags) { struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; union gpio_pcal9535a_port_data *port = &drv_data->reg_cache.dir; uint16_t bit_mask; uint16_t new_value = 0; int ret; switch (access_op) { case GPIO_ACCESS_BY_PIN: bit_mask = 1 << pin; /* Config 0 == output, 1 == input */ if ((flags & GPIO_DIR_MASK) == GPIO_DIR_IN) { new_value = 1 << pin; } port->all &= ~bit_mask; port->all |= new_value; break; case GPIO_ACCESS_BY_PORT: /* Config 0 == output, 1 == input */ if ((flags & GPIO_DIR_MASK) == GPIO_DIR_IN) { port->all = 0xFFFF; } else { port->all = 0x0; } break; default: ret = -ENOTSUP; goto done; } ret = _write_port_regs(dev, REG_CONF_PORT0, port); done: return ret; } /** * @brief Setup the pin pull up/pull down status * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param flags Flags of pin or port * * @return 0 if successful, failed otherwise */ static int _setup_pin_pullupdown(struct device *dev, int access_op, uint32_t pin, int flags) { struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; union gpio_pcal9535a_port_data *port; uint16_t bit_mask; uint16_t new_value = 0; int ret; /* If disabling pull up/down, there is no need to set the selection * register. Just go straight to disabling. */ if ((flags & GPIO_PUD_MASK) == GPIO_PUD_NORMAL) { goto en_dis; } /* Setup pin pull up or pull down */ port = &drv_data->reg_cache.pud_sel; switch (access_op) { case GPIO_ACCESS_BY_PIN: bit_mask = 1 << pin; /* pull down == 0, pull up == 1*/ if ((flags & GPIO_PUD_MASK) == GPIO_PUD_PULL_UP) { new_value = 1 << pin; } port->all &= ~bit_mask; port->all |= new_value; break; case GPIO_ACCESS_BY_PORT: /* pull down == 0, pull up == 1*/ if ((flags & GPIO_PUD_MASK) == GPIO_PUD_PULL_UP) { port->all = 0xFFFF; } else { port->all = 0x0; } break; default: ret = -ENOTSUP; goto done; } ret = _write_port_regs(dev, REG_PUD_SEL_PORT0, port); if (ret) { goto done; } en_dis: /* enable/disable pull up/down */ port = &drv_data->reg_cache.pud_en; switch (access_op) { case GPIO_ACCESS_BY_PIN: bit_mask = 1 << pin; if ((flags & GPIO_PUD_MASK) != GPIO_PUD_NORMAL) { new_value = 1 << pin; } port->all &= ~bit_mask; port->all |= new_value; break; case GPIO_ACCESS_BY_PORT: if ((flags & GPIO_PUD_MASK) != GPIO_PUD_NORMAL) { port->all = 0xFFFF; } else { port->all = 0x0; } break; default: ret = -ENOTSUP; goto done; } ret = _write_port_regs(dev, REG_PUD_EN_PORT0, port); done: return ret; } /** * @brief Setup the polarity of pin or port * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param flags Flags of pin or port * * @return 0 if successful, failed otherwise */ static int _setup_pin_polarity(struct device *dev, int access_op, uint32_t pin, int flags) { struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; union gpio_pcal9535a_port_data *port = &drv_data->reg_cache.pol_inv; uint16_t bit_mask; uint16_t new_value = 0; int ret; switch (access_op) { case GPIO_ACCESS_BY_PIN: bit_mask = BIT(pin); /* normal == 0, invert == 1 */ if ((flags & GPIO_POL_MASK) == GPIO_POL_INV) { new_value = BIT(pin); } port->all &= ~bit_mask; port->all |= new_value; break; case GPIO_ACCESS_BY_PORT: /* normal == 0, invert == 1 */ if ((flags & GPIO_POL_MASK) == GPIO_POL_INV) { port->all = 0xFFFF; } else { port->all = 0x0; } break; default: ret = -ENOTSUP; goto done; } ret = _write_port_regs(dev, REG_POL_INV_PORT0, port); if (!ret) { drv_data->out_pol_inv = port->all; } done: return ret; } /** * @brief Configurate pin or port * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param flags Flags of pin or port * * @return 0 if successful, failed otherwise */ static int gpio_pcal9535a_config(struct device *dev, int access_op, uint32_t pin, int flags) { int ret; #if (CONFIG_SYS_LOG_GPIO_PCAL9535A_LEVEL >= SYS_LOG_LEVEL_DEBUG) const struct gpio_pcal9535a_config * const config = dev->config->config_info; uint16_t i2c_addr = config->i2c_slave_addr; #endif if (!_has_i2c_master(dev)) { return -EINVAL; } ret = _setup_pin_dir(dev, access_op, pin, flags); if (ret) { SYS_LOG_ERR("PCAL9535A[0x%X]: error setting pin direction (%d)", i2c_addr, ret); goto done; } ret = _setup_pin_polarity(dev, access_op, pin, flags); if (ret) { SYS_LOG_ERR("PCAL9535A[0x%X]: error setting pin polarity (%d)", i2c_addr, ret); goto done; } ret = _setup_pin_pullupdown(dev, access_op, pin, flags); if (ret) { SYS_LOG_ERR("PCAL9535A[0x%X]: error setting pin pull up/down " "(%d)", i2c_addr, ret); goto done; } done: return ret; } /** * @brief Set the pin or port output * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param value Value to set (0 or 1) * * @return 0 if successful, failed otherwise */ static int gpio_pcal9535a_write(struct device *dev, int access_op, uint32_t pin, uint32_t value) { struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; union gpio_pcal9535a_port_data *port = &drv_data->reg_cache.output; uint16_t bit_mask; uint16_t new_value; int ret; if (!_has_i2c_master(dev)) { return -EINVAL; } /* Invert input value for pins configurated as active low. */ switch (access_op) { case GPIO_ACCESS_BY_PIN: bit_mask = BIT(pin); new_value = (value << pin) & bit_mask; new_value ^= (drv_data->out_pol_inv & bit_mask); new_value &= bit_mask; port->all &= ~bit_mask; port->all |= new_value; break; case GPIO_ACCESS_BY_PORT: port->all = value; bit_mask = drv_data->out_pol_inv; new_value = value & bit_mask; new_value ^= drv_data->out_pol_inv; new_value &= bit_mask; port->all &= ~bit_mask; port->all |= new_value; break; default: ret = -ENOTSUP; goto done; } ret = _write_port_regs(dev, REG_OUTPUT_PORT0, port); done: return ret; } /** * @brief Read the pin or port status * * @param dev Device struct of the PCAL9535A * @param access_op Access operation (pin or port) * @param pin The pin number * @param value Value of input pin(s) * * @return 0 if successful, failed otherwise */ static int gpio_pcal9535a_read(struct device *dev, int access_op, uint32_t pin, uint32_t *value) { union gpio_pcal9535a_port_data buf; int ret; if (!_has_i2c_master(dev)) { return -EINVAL; } ret = _read_port_regs(dev, REG_INPUT_PORT0, &buf); if (ret != 0) { goto done; } switch (access_op) { case GPIO_ACCESS_BY_PIN: *value = (buf.all >> pin) & 0x01; break; case GPIO_ACCESS_BY_PORT: *value = buf.all; break; default: ret = -ENOTSUP; break; } done: return ret; } static int gpio_pcal9535a_manage_callback(struct device *dev, struct gpio_callback *callback, bool set) { ARG_UNUSED(dev); ARG_UNUSED(callback); ARG_UNUSED(set); return -ENOTSUP; } static int gpio_pcal9535a_enable_callback(struct device *dev, int access_op, uint32_t pin) { ARG_UNUSED(dev); ARG_UNUSED(access_op); ARG_UNUSED(pin); return -ENOTSUP; } static int gpio_pcal9535a_disable_callback(struct device *dev, int access_op, uint32_t pin) { ARG_UNUSED(dev); ARG_UNUSED(access_op); ARG_UNUSED(pin); return -ENOTSUP; } static struct gpio_driver_api gpio_pcal9535a_drv_api_funcs = { .config = gpio_pcal9535a_config, .write = gpio_pcal9535a_write, .read = gpio_pcal9535a_read, .manage_callback = gpio_pcal9535a_manage_callback, .enable_callback = gpio_pcal9535a_enable_callback, .disable_callback = gpio_pcal9535a_disable_callback, }; /** * @brief Initialization function of PCAL9535A * * @param dev Device struct * @return 0 if successful, failed otherwise. */ int gpio_pcal9535a_init(struct device *dev) { const struct gpio_pcal9535a_config * const config = dev->config->config_info; struct gpio_pcal9535a_drv_data * const drv_data = (struct gpio_pcal9535a_drv_data * const)dev->driver_data; struct device *i2c_master; /* Find out the device struct of the I2C master */ i2c_master = device_get_binding((char *)config->i2c_master_dev_name); if (!i2c_master) { return -EINVAL; } drv_data->i2c_master = i2c_master; nano_timer_init(&drv_data->timer, (void *) 0); dev->driver_api = &gpio_pcal9535a_drv_api_funcs; return 0; } /* Initialization for PCAL9535A_0 */ #ifdef CONFIG_GPIO_PCAL9535A_0 #include <device.h> #include <init.h> static struct gpio_pcal9535a_config gpio_pcal9535a_0_cfg = { .i2c_master_dev_name = CONFIG_GPIO_PCAL9535A_0_I2C_MASTER_DEV_NAME, .i2c_slave_addr = CONFIG_GPIO_PCAL9535A_0_I2C_ADDR, }; static struct gpio_pcal9535a_drv_data gpio_pcal9535a_0_drvdata = { /* Default for registers according to datasheet */ .reg_cache.output = { .all = 0xFFFF }, .reg_cache.pol_inv = { .all = 0x0 }, .reg_cache.dir = { .all = 0xFFFF }, .reg_cache.pud_en = { .all = 0x0 }, .reg_cache.pud_sel = { .all = 0xFFFF }, }; /* This has to init after I2C master */ DEVICE_INIT(gpio_pcal9535a_0, CONFIG_GPIO_PCAL9535A_0_DEV_NAME, gpio_pcal9535a_init, &gpio_pcal9535a_0_drvdata, &gpio_pcal9535a_0_cfg, SECONDARY, CONFIG_GPIO_PCAL9535A_INIT_PRIORITY); #endif /* CONFIG_GPIO_PCAL9535A_0 */ /* Initialization for PCAL9535A_1 */ #ifdef CONFIG_GPIO_PCAL9535A_1 #include <device.h> #include <init.h> static struct gpio_pcal9535a_config gpio_pcal9535a_1_cfg = { .i2c_master_dev_name = CONFIG_GPIO_PCAL9535A_1_I2C_MASTER_DEV_NAME, .i2c_slave_addr = CONFIG_GPIO_PCAL9535A_1_I2C_ADDR, }; static struct gpio_pcal9535a_drv_data gpio_pcal9535a_1_drvdata = { /* Default for registers according to datasheet */ .reg_cache.output = { .all = 0xFFFF }, .reg_cache.pol_inv = { .all = 0x0 }, .reg_cache.dir = { .all = 0xFFFF }, .reg_cache.pud_en = { .all = 0x0 }, .reg_cache.pud_sel = { .all = 0xFFFF }, }; /* This has to init after I2C master */ DEVICE_INIT(gpio_pcal9535a_1, CONFIG_GPIO_PCAL9535A_1_DEV_NAME, gpio_pcal9535a_init, &gpio_pcal9535a_1_drvdata, &gpio_pcal9535a_1_cfg, SECONDARY, CONFIG_GPIO_PCAL9535A_INIT_PRIORITY); #endif /* CONFIG_GPIO_PCAL9535A_1 */ /* Initialization for PCAL9535A_2 */ #ifdef CONFIG_GPIO_PCAL9535A_2 #include <device.h> #include <init.h> static struct gpio_pcal9535a_config gpio_pcal9535a_2_cfg = { .i2c_master_dev_name = CONFIG_GPIO_PCAL9535A_2_I2C_MASTER_DEV_NAME, .i2c_slave_addr = CONFIG_GPIO_PCAL9535A_2_I2C_ADDR, }; static struct gpio_pcal9535a_drv_data gpio_pcal9535a_2_drvdata = { /* Default for registers according to datasheet */ .reg_cache.output = { .all = 0xFFFF }, .reg_cache.pol_inv = { .all = 0x0 }, .reg_cache.dir = { .all = 0xFFFF }, .reg_cache.pud_en = { .all = 0x0 }, .reg_cache.pud_sel = { .all = 0xFFFF }, }; /* This has to init after I2C master */ DEVICE_INIT(gpio_pcal9535a_2, CONFIG_GPIO_PCAL9535A_2_DEV_NAME, gpio_pcal9535a_init, &gpio_pcal9535a_2_drvdata, &gpio_pcal9535a_2_cfg, SECONDARY, CONFIG_GPIO_PCAL9535A_INIT_PRIORITY); #endif /* CONFIG_GPIO_PCAL9535A_2 */ /* Initialization for PCAL9535A_3 */ #ifdef CONFIG_GPIO_PCAL9535A_3 #include <device.h> #include <init.h> static struct gpio_pcal9535a_config gpio_pcal9535a_3_cfg = { .i2c_master_dev_name = CONFIG_GPIO_PCAL9535A_3_I2C_MASTER_DEV_NAME, .i2c_slave_addr = CONFIG_GPIO_PCAL9535A_3_I2C_ADDR, }; static struct gpio_pcal9535a_drv_data gpio_pcal9535a_3_drvdata = { /* Default for registers according to datasheet */ .reg_cache.output = { .all = 0xFFFF }, .reg_cache.pol_inv = { .all = 0x0 }, .reg_cache.dir = { .all = 0xFFFF }, .reg_cache.pud_en = { .all = 0x0 }, .reg_cache.pud_sel = { .all = 0xFFFF }, }; /* This has to init after I2C master */ DEVICE_INIT(gpio_pcal9535a_3, CONFIG_GPIO_PCAL9535A_3_DEV_NAME, gpio_pcal9535a_init, &gpio_pcal9535a_3_drvdata, &gpio_pcal9535a_3_cfg, SECONDARY, CONFIG_GPIO_PCAL9535A_INIT_PRIORITY); #endif /* CONFIG_GPIO_PCAL9535A_3 */ |