pca954x.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. struct pca954x_priv {
  14. u32 addr; /* I2C mux address */
  15. u32 width; /* I2C mux width - number of busses */
  16. };
  17. static int pca954x_deselect(struct udevice *mux, struct udevice *bus,
  18. uint channel)
  19. {
  20. struct pca954x_priv *priv = dev_get_priv(mux);
  21. uchar byte = 0;
  22. return dm_i2c_write(mux, priv->addr, &byte, 1);
  23. }
  24. static int pca954x_select(struct udevice *mux, struct udevice *bus,
  25. uint channel)
  26. {
  27. struct pca954x_priv *priv = dev_get_priv(mux);
  28. uchar byte = 1 << channel;
  29. return dm_i2c_write(mux, priv->addr, &byte, 1);
  30. }
  31. static const struct i2c_mux_ops pca954x_ops = {
  32. .select = pca954x_select,
  33. .deselect = pca954x_deselect,
  34. };
  35. static const struct udevice_id pca954x_ids[] = {
  36. { .compatible = "nxp,pca9548", .data = (ulong)8 },
  37. { .compatible = "nxp,pca9544", .data = (ulong)4 },
  38. { }
  39. };
  40. static int pca954x_ofdata_to_platdata(struct udevice *dev)
  41. {
  42. struct pca954x_priv *priv = dev_get_priv(dev);
  43. priv->addr = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", 0);
  44. if (!priv->addr) {
  45. debug("MUX not found\n");
  46. return -ENODEV;
  47. }
  48. priv->width = dev_get_driver_data(dev);
  49. if (!priv->width) {
  50. debug("No I2C MUX width specified\n");
  51. return -EINVAL;
  52. }
  53. debug("Device %s at 0x%x with width %d\n",
  54. dev->name, priv->addr, priv->width);
  55. return 0;
  56. }
  57. U_BOOT_DRIVER(pca954x) = {
  58. .name = "pca954x",
  59. .id = UCLASS_I2C_MUX,
  60. .of_match = pca954x_ids,
  61. .ops = &pca954x_ops,
  62. .ofdata_to_platdata = pca954x_ofdata_to_platdata,
  63. .priv_auto_alloc_size = sizeof(struct pca954x_priv),
  64. };