pca954x.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2015 - 2016 Xilinx, Inc.
  3. * Written by Michal Simek
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <asm/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. };
  25. struct pca954x_priv {
  26. u32 addr; /* I2C mux address */
  27. u32 width; /* I2C mux width - number of busses */
  28. };
  29. static const struct chip_desc chips[] = {
  30. [PCA9544] = {
  31. .enable = 0x4,
  32. .muxtype = pca954x_ismux,
  33. },
  34. [PCA9547] = {
  35. .enable = 0x8,
  36. .muxtype = pca954x_ismux,
  37. },
  38. [PCA9548] = {
  39. .enable = 0x8,
  40. .muxtype = pca954x_isswi,
  41. },
  42. };
  43. static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
  44. uint channel)
  45. {
  46. struct pca954x_priv *priv = dev_get_priv(mux);
  47. uchar byte = 0;
  48. return dm_i2c_write(mux, priv->addr, &byte, 1);
  49. }
  50. static int pca954x_select(struct udevice *mux, struct udevice *bus,
  51. uint channel)
  52. {
  53. struct pca954x_priv *priv = dev_get_priv(mux);
  54. const struct chip_desc *chip = &chips[dev_get_driver_data(mux)];
  55. uchar byte;
  56. if (chip->muxtype == pca954x_ismux)
  57. byte = channel | chip->enable;
  58. else
  59. byte = 1 << channel;
  60. return dm_i2c_write(mux, priv->addr, &byte, 1);
  61. }
  62. static const struct i2c_mux_ops pca954x_ops = {
  63. .select = pca954x_select,
  64. .deselect = pca954x_deselect,
  65. };
  66. static const struct udevice_id pca954x_ids[] = {
  67. { .compatible = "nxp,pca9544", .data = PCA9544 },
  68. { .compatible = "nxp,pca9547", .data = PCA9547 },
  69. { .compatible = "nxp,pca9548", .data = PCA9548 },
  70. { }
  71. };
  72. static int pca954x_ofdata_to_platdata(struct udevice *dev)
  73. {
  74. struct pca954x_priv *priv = dev_get_priv(dev);
  75. priv->addr = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", 0);
  76. if (!priv->addr) {
  77. debug("MUX not found\n");
  78. return -ENODEV;
  79. }
  80. priv->width = dev_get_driver_data(dev);
  81. if (!priv->width) {
  82. debug("No I2C MUX width specified\n");
  83. return -EINVAL;
  84. }
  85. debug("Device %s at 0x%x with width %d\n",
  86. dev->name, priv->addr, priv->width);
  87. return 0;
  88. }
  89. U_BOOT_DRIVER(pca954x) = {
  90. .name = "pca954x",
  91. .id = UCLASS_I2C_MUX,
  92. .of_match = pca954x_ids,
  93. .ops = &pca954x_ops,
  94. .ofdata_to_platdata = pca954x_ofdata_to_platdata,
  95. .priv_auto_alloc_size = sizeof(struct pca954x_priv),
  96. };