pca954x.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. PCA9543,
  15. PCA9544,
  16. PCA9547,
  17. PCA9548,
  18. PCA9646
  19. };
  20. struct chip_desc {
  21. u8 enable; /* Enable mask in ctl register (used for muxes only) */
  22. enum muxtype {
  23. pca954x_ismux = 0,
  24. pca954x_isswi,
  25. } muxtype;
  26. u32 width;
  27. };
  28. struct pca954x_priv {
  29. u32 addr; /* I2C mux address */
  30. u32 width; /* I2C mux width - number of busses */
  31. struct gpio_desc gpio_mux_reset;
  32. };
  33. static const struct chip_desc chips[] = {
  34. [PCA9543] = {
  35. .muxtype = pca954x_isswi,
  36. .width = 2,
  37. },
  38. [PCA9544] = {
  39. .enable = 0x4,
  40. .muxtype = pca954x_ismux,
  41. .width = 4,
  42. },
  43. [PCA9547] = {
  44. .enable = 0x8,
  45. .muxtype = pca954x_ismux,
  46. .width = 8,
  47. },
  48. [PCA9548] = {
  49. .muxtype = pca954x_isswi,
  50. .width = 8,
  51. },
  52. [PCA9646] = {
  53. .muxtype = pca954x_isswi,
  54. .width = 4,
  55. },
  56. };
  57. static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
  58. uint channel)
  59. {
  60. struct pca954x_priv *priv = dev_get_priv(mux);
  61. uchar byte = 0;
  62. return dm_i2c_write(mux, priv->addr, &byte, 1);
  63. }
  64. static int pca954x_select(struct udevice *mux, struct udevice *bus,
  65. uint channel)
  66. {
  67. struct pca954x_priv *priv = dev_get_priv(mux);
  68. const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
  69. uchar byte;
  70. if (chip->muxtype == pca954x_ismux)
  71. byte = channel | chip->enable;
  72. else
  73. byte = 1 << channel;
  74. return dm_i2c_write(mux, priv->addr, &byte, 1);
  75. }
  76. static const struct i2c_mux_ops pca954x_ops = {
  77. .select = pca954x_select,
  78. .deselect = pca954x_deselect,
  79. };
  80. static const struct udevice_id pca954x_ids[] = {
  81. { .compatible = "nxp,pca9543", .data = PCA9543 },
  82. { .compatible = "nxp,pca9544", .data = PCA9544 },
  83. { .compatible = "nxp,pca9547", .data = PCA9547 },
  84. { .compatible = "nxp,pca9548", .data = PCA9548 },
  85. { .compatible = "nxp,pca9646", .data = PCA9646 },
  86. { }
  87. };
  88. static int pca954x_ofdata_to_platdata(struct udevice *dev)
  89. {
  90. struct pca954x_priv *priv = dev_get_priv(dev);
  91. const struct chip_desc *chip = &chips[dev_get_driver_data(dev)];
  92. priv->addr = dev_read_u32_default(dev, "reg", 0);
  93. if (!priv->addr) {
  94. debug("MUX not found\n");
  95. return -ENODEV;
  96. }
  97. priv->width = chip->width;
  98. if (!priv->width) {
  99. debug("No I2C MUX width specified\n");
  100. return -EINVAL;
  101. }
  102. debug("Device %s at 0x%x with width %d\n",
  103. dev->name, priv->addr, priv->width);
  104. return 0;
  105. }
  106. static int pca954x_probe(struct udevice *dev)
  107. {
  108. if (CONFIG_IS_ENABLED(DM_GPIO)) {
  109. struct pca954x_priv *priv = dev_get_priv(dev);
  110. int err;
  111. err = gpio_request_by_name(dev, "reset-gpios", 0,
  112. &priv->gpio_mux_reset, GPIOD_IS_OUT);
  113. /* it's optional so only bail if we get a real error */
  114. if (err && (err != -ENOENT))
  115. return err;
  116. /* dm will take care of polarity */
  117. if (dm_gpio_is_valid(&priv->gpio_mux_reset))
  118. dm_gpio_set_value(&priv->gpio_mux_reset, 0);
  119. }
  120. return 0;
  121. }
  122. static int pca954x_remove(struct udevice *dev)
  123. {
  124. if (CONFIG_IS_ENABLED(DM_GPIO)) {
  125. struct pca954x_priv *priv = dev_get_priv(dev);
  126. if (dm_gpio_is_valid(&priv->gpio_mux_reset))
  127. dm_gpio_free(dev, &priv->gpio_mux_reset);
  128. }
  129. return 0;
  130. }
  131. U_BOOT_DRIVER(pca954x) = {
  132. .name = "pca954x",
  133. .id = UCLASS_I2C_MUX,
  134. .of_match = pca954x_ids,
  135. .probe = pca954x_probe,
  136. .remove = pca954x_remove,
  137. .ops = &pca954x_ops,
  138. .ofdata_to_platdata = pca954x_ofdata_to_platdata,
  139. .priv_auto_alloc_size = sizeof(struct pca954x_priv),
  140. };