ocelot.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /* Copyright 2022 Innovative Advantage Inc. */
  3. #ifndef _LINUX_MFD_OCELOT_H
  4. #define _LINUX_MFD_OCELOT_H
  5. #include <linux/err.h>
  6. #include <linux/errno.h>
  7. #include <linux/ioport.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/types.h>
  11. struct resource;
  12. static inline struct regmap *
  13. ocelot_regmap_from_resource_optional(struct platform_device *pdev,
  14. unsigned int index,
  15. const struct regmap_config *config)
  16. {
  17. struct device *dev = &pdev->dev;
  18. struct resource *res;
  19. void __iomem *regs;
  20. /*
  21. * Don't use _get_and_ioremap_resource() here, since that will invoke
  22. * prints of "invalid resource" which will simply add confusion.
  23. */
  24. res = platform_get_resource(pdev, IORESOURCE_MEM, index);
  25. if (res) {
  26. regs = devm_ioremap_resource(dev, res);
  27. if (IS_ERR(regs))
  28. return ERR_CAST(regs);
  29. return devm_regmap_init_mmio(dev, regs, config);
  30. }
  31. /*
  32. * Fall back to using REG and getting the resource from the parent
  33. * device, which is possible in an MFD configuration
  34. */
  35. if (dev->parent) {
  36. res = platform_get_resource(pdev, IORESOURCE_REG, index);
  37. if (!res)
  38. return NULL;
  39. return dev_get_regmap(dev->parent, res->name);
  40. }
  41. return NULL;
  42. }
  43. static inline struct regmap *
  44. ocelot_regmap_from_resource(struct platform_device *pdev, unsigned int index,
  45. const struct regmap_config *config)
  46. {
  47. struct regmap *map;
  48. map = ocelot_regmap_from_resource_optional(pdev, index, config);
  49. return map ?: ERR_PTR(-ENOENT);
  50. }
  51. #endif