pca954x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 - 2016 Xilinx, Inc.
  4. * Copyright (C) 2017 National Instruments Corp
  5. * Written by Michal Simek
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <asm-generic/gpio.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. enum pca_type {
  14. PCA9544,
  15. PCA9547,
  16. PCA9548
  17. };
  18. struct chip_desc {
  19. u8 enable;
  20. enum muxtype {
  21. pca954x_ismux = 0,
  22. pca954x_isswi,
  23. } muxtype;
  24. u32 width;
  25. };
  26. struct pca954x_priv {
  27. u32 addr; /* I2C mux address */
  28. u32 width; /* I2C mux width - number of busses */
  29. struct gpio_desc gpio_mux_reset;
  30. };
  31. static const struct chip_desc chips[] = {
  32. [PCA9544] = {
  33. .enable = 0x4,
  34. .muxtype = pca954x_ismux,
  35. .width = 4,
  36. },
  37. [PCA9547] = {
  38. .enable = 0x8,
  39. .muxtype = pca954x_ismux,
  40. .width = 8,
  41. },
  42. [PCA9548] = {
  43. .enable = 0x8,
  44. .muxtype = pca954x_isswi,
  45. .width = 8,
  46. },
  47. };
  48. static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
  49. uint channel)
  50. {
  51. struct pca954x_priv *priv = dev_get_priv(mux);
  52. uchar byte = 0;
  53. return dm_i2c_write(mux, priv->addr, &byte, 1);
  54. }
  55. static int pca954x_select(struct udevice *mux, struct udevice *bus,
  56. uint channel)
  57. {
  58. struct pca954x_priv *priv = dev_get_priv(mux);
  59. const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
  60. uchar byte;
  61. if (chip->muxtype == pca954x_ismux)
  62. byte = channel | chip->enable;
  63. else
  64. byte = 1 << channel;
  65. return dm_i2c_write(mux, priv->addr, &byte, 1);
  66. }
  67. static const struct i2c_mux_ops pca954x_ops = {
  68. .select = pca954x_select,
  69. .deselect = pca954x_deselect,
  70. };
  71. static const struct udevice_id pca954x_ids[] = {
  72. { .compatible = "nxp,pca9544", .data = PCA9544 },
  73. { .compatible = "nxp,pca9547", .data = PCA9547 },
  74. { .compatible = "nxp,pca9548", .data = PCA9548 },
  75. { }
  76. };
  77. static int pca954x_ofdata_to_platdata(struct udevice *dev)
  78. {
  79. struct pca954x_priv *priv = dev_get_priv(dev);
  80. const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
  81. priv->addr = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", 0);
  82. if (!priv->addr) {
  83. debug("MUX not found\n");
  84. return -ENODEV;
  85. }
  86. priv->width = chip->width;
  87. if (!priv->width) {
  88. debug("No I2C MUX width specified\n");
  89. return -EINVAL;
  90. }
  91. debug("Device %s at 0x%x with width %d\n",
  92. dev->name, priv->addr, priv->width);
  93. return 0;
  94. }
  95. static int pca954x_probe(struct udevice *dev)
  96. {
  97. if (IS_ENABLED(CONFIG_DM_GPIO)) {
  98. struct pca954x_priv *priv = dev_get_priv(dev);
  99. int err;
  100. err = gpio_request_by_name(dev, "reset-gpios", 0,
  101. &priv->gpio_mux_reset, GPIOD_IS_OUT);
  102. /* it's optional so only bail if we get a real error */
  103. if (err && (err != -ENOENT))
  104. return err;
  105. /* dm will take care of polarity */
  106. if (dm_gpio_is_valid(&priv->gpio_mux_reset))
  107. dm_gpio_set_value(&priv->gpio_mux_reset, 0);
  108. }
  109. return 0;
  110. }
  111. static int pca954x_remove(struct udevice *dev)
  112. {
  113. if (IS_ENABLED(CONFIG_DM_GPIO)) {
  114. struct pca954x_priv *priv = dev_get_priv(dev);
  115. if (dm_gpio_is_valid(&priv->gpio_mux_reset))
  116. dm_gpio_free(dev, &priv->gpio_mux_reset);
  117. }
  118. return 0;
  119. }
  120. U_BOOT_DRIVER(pca954x) = {
  121. .name = "pca954x",
  122. .id = UCLASS_I2C_MUX,
  123. .of_match = pca954x_ids,
  124. .probe = pca954x_probe,
  125. .remove = pca954x_remove,
  126. .ops = &pca954x_ops,
  127. .ofdata_to_platdata = pca954x_ofdata_to_platdata,
  128. .priv_auto_alloc_size = sizeof(struct pca954x_priv),
  129. };