mux.c 2.5 KB

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