mux.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Utility to set the DAVINCI MUX register from a table in mux.h
  4. *
  5. * Author: Vladimir Barinov, MontaVista Software, Inc. <[email protected]>
  6. *
  7. * Based on linux/arch/arm/plat-omap/mux.c:
  8. * Copyright (C) 2003 - 2005 Nokia Corporation
  9. *
  10. * Written by Tony Lindgren
  11. *
  12. * 2007 (c) MontaVista Software, Inc.
  13. *
  14. * Copyright (C) 2008 Texas Instruments.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/spinlock.h>
  20. #include "mux.h"
  21. #include "common.h"
  22. static void __iomem *pinmux_base;
  23. /*
  24. * Sets the DAVINCI MUX register based on the table
  25. */
  26. int davinci_cfg_reg(const unsigned long index)
  27. {
  28. static DEFINE_SPINLOCK(mux_spin_lock);
  29. struct davinci_soc_info *soc_info = &davinci_soc_info;
  30. unsigned long flags;
  31. const struct mux_config *cfg;
  32. unsigned int reg_orig = 0, reg = 0;
  33. unsigned int mask, warn = 0;
  34. if (WARN_ON(!soc_info->pinmux_pins))
  35. return -ENODEV;
  36. if (!pinmux_base) {
  37. pinmux_base = ioremap(soc_info->pinmux_base, SZ_4K);
  38. if (WARN_ON(!pinmux_base))
  39. return -ENOMEM;
  40. }
  41. if (index >= soc_info->pinmux_pins_num) {
  42. pr_err("Invalid pin mux index: %lu (%lu)\n",
  43. index, soc_info->pinmux_pins_num);
  44. dump_stack();
  45. return -ENODEV;
  46. }
  47. cfg = &soc_info->pinmux_pins[index];
  48. if (cfg->name == NULL) {
  49. pr_err("No entry for the specified index\n");
  50. return -ENODEV;
  51. }
  52. /* Update the mux register in question */
  53. if (cfg->mask) {
  54. unsigned tmp1, tmp2;
  55. spin_lock_irqsave(&mux_spin_lock, flags);
  56. reg_orig = __raw_readl(pinmux_base + cfg->mux_reg);
  57. mask = (cfg->mask << cfg->mask_offset);
  58. tmp1 = reg_orig & mask;
  59. reg = reg_orig & ~mask;
  60. tmp2 = (cfg->mode << cfg->mask_offset);
  61. reg |= tmp2;
  62. if (tmp1 != tmp2)
  63. warn = 1;
  64. __raw_writel(reg, pinmux_base + cfg->mux_reg);
  65. spin_unlock_irqrestore(&mux_spin_lock, flags);
  66. }
  67. if (warn) {
  68. #ifdef CONFIG_DAVINCI_MUX_WARNINGS
  69. pr_warn("initialized %s\n", cfg->name);
  70. #endif
  71. }
  72. #ifdef CONFIG_DAVINCI_MUX_DEBUG
  73. if (cfg->debug || warn) {
  74. pr_warn("Setting register %s\n", cfg->name);
  75. pr_warn(" %s (0x%08x) = 0x%08x -> 0x%08x\n",
  76. cfg->mux_reg_name, cfg->mux_reg, reg_orig, reg);
  77. }
  78. #endif
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(davinci_cfg_reg);
  82. int davinci_cfg_reg_list(const short pins[])
  83. {
  84. int i, error = -EINVAL;
  85. if (pins)
  86. for (i = 0; pins[i] >= 0; i++) {
  87. error = davinci_cfg_reg(pins[i]);
  88. if (error)
  89. break;
  90. }
  91. return error;
  92. }