Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 1 | /* |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 2 | * Utility to set the DAVINCI MUX register from a table in mux.h |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 3 | * |
| 4 | * Author: Vladimir Barinov, MontaVista Software, Inc. <source@mvista.com> |
| 5 | * |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 6 | * Based on linux/arch/arm/plat-omap/mux.c: |
| 7 | * Copyright (C) 2003 - 2005 Nokia Corporation |
| 8 | * |
| 9 | * Written by Tony Lindgren |
| 10 | * |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 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. |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 15 | * |
| 16 | * Copyright (C) 2008 Texas Instruments. |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 17 | */ |
Joe Perches | a7ca2bc | 2014-10-31 17:51:51 -0700 | [diff] [blame] | 18 | |
| 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 22 | #include <linux/module.h> |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
| 24 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/mux.h> |
Mark A. Greer | 0e58595 | 2009-04-15 12:39:48 -0700 | [diff] [blame] | 26 | #include <mach/common.h> |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 27 | |
Cyril Chemparathy | 779b0d5 | 2010-05-07 17:06:38 -0400 | [diff] [blame] | 28 | static void __iomem *pinmux_base; |
| 29 | |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 30 | /* |
| 31 | * Sets the DAVINCI MUX register based on the table |
| 32 | */ |
Arnd Bergmann | 31612d6 | 2015-01-23 15:40:15 +0100 | [diff] [blame] | 33 | int davinci_cfg_reg(const unsigned long index) |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 34 | { |
| 35 | static DEFINE_SPINLOCK(mux_spin_lock); |
Mark A. Greer | 0e58595 | 2009-04-15 12:39:48 -0700 | [diff] [blame] | 36 | struct davinci_soc_info *soc_info = &davinci_soc_info; |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 37 | unsigned long flags; |
| 38 | const struct mux_config *cfg; |
| 39 | unsigned int reg_orig = 0, reg = 0; |
| 40 | unsigned int mask, warn = 0; |
| 41 | |
Cyril Chemparathy | 779b0d5 | 2010-05-07 17:06:38 -0400 | [diff] [blame] | 42 | if (WARN_ON(!soc_info->pinmux_pins)) |
| 43 | return -ENODEV; |
| 44 | |
| 45 | if (!pinmux_base) { |
| 46 | pinmux_base = ioremap(soc_info->pinmux_base, SZ_4K); |
| 47 | if (WARN_ON(!pinmux_base)) |
| 48 | return -ENOMEM; |
| 49 | } |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 50 | |
Mark A. Greer | 0e58595 | 2009-04-15 12:39:48 -0700 | [diff] [blame] | 51 | if (index >= soc_info->pinmux_pins_num) { |
Joe Perches | a7ca2bc | 2014-10-31 17:51:51 -0700 | [diff] [blame] | 52 | pr_err("Invalid pin mux index: %lu (%lu)\n", |
Mark A. Greer | 0e58595 | 2009-04-15 12:39:48 -0700 | [diff] [blame] | 53 | index, soc_info->pinmux_pins_num); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 54 | dump_stack(); |
| 55 | return -ENODEV; |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Mark A. Greer | 0e58595 | 2009-04-15 12:39:48 -0700 | [diff] [blame] | 58 | cfg = &soc_info->pinmux_pins[index]; |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 59 | |
| 60 | if (cfg->name == NULL) { |
Joe Perches | a7ca2bc | 2014-10-31 17:51:51 -0700 | [diff] [blame] | 61 | pr_err("No entry for the specified index\n"); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 62 | return -ENODEV; |
| 63 | } |
| 64 | |
| 65 | /* Update the mux register in question */ |
| 66 | if (cfg->mask) { |
| 67 | unsigned tmp1, tmp2; |
| 68 | |
| 69 | spin_lock_irqsave(&mux_spin_lock, flags); |
Cyril Chemparathy | 779b0d5 | 2010-05-07 17:06:38 -0400 | [diff] [blame] | 70 | reg_orig = __raw_readl(pinmux_base + cfg->mux_reg); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 71 | |
| 72 | mask = (cfg->mask << cfg->mask_offset); |
| 73 | tmp1 = reg_orig & mask; |
| 74 | reg = reg_orig & ~mask; |
| 75 | |
| 76 | tmp2 = (cfg->mode << cfg->mask_offset); |
| 77 | reg |= tmp2; |
| 78 | |
| 79 | if (tmp1 != tmp2) |
| 80 | warn = 1; |
| 81 | |
Cyril Chemparathy | 779b0d5 | 2010-05-07 17:06:38 -0400 | [diff] [blame] | 82 | __raw_writel(reg, pinmux_base + cfg->mux_reg); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 83 | spin_unlock_irqrestore(&mux_spin_lock, flags); |
| 84 | } |
| 85 | |
| 86 | if (warn) { |
| 87 | #ifdef CONFIG_DAVINCI_MUX_WARNINGS |
Joe Perches | a7ca2bc | 2014-10-31 17:51:51 -0700 | [diff] [blame] | 88 | pr_warn("initialized %s\n", cfg->name); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
| 92 | #ifdef CONFIG_DAVINCI_MUX_DEBUG |
| 93 | if (cfg->debug || warn) { |
Joe Perches | a7ca2bc | 2014-10-31 17:51:51 -0700 | [diff] [blame] | 94 | pr_warn("Setting register %s\n", cfg->name); |
| 95 | pr_warn(" %s (0x%08x) = 0x%08x -> 0x%08x\n", |
| 96 | cfg->mux_reg_name, cfg->mux_reg, reg_orig, reg); |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 97 | } |
| 98 | #endif |
| 99 | |
| 100 | return 0; |
Vladimir Barinov | 83f5322 | 2007-07-10 13:10:04 +0100 | [diff] [blame] | 101 | } |
Kevin Hilman | 5526b3f | 2009-04-14 09:50:37 -0500 | [diff] [blame] | 102 | EXPORT_SYMBOL(davinci_cfg_reg); |
Sudhakar Rajashekhara | c96b56c | 2009-07-16 05:45:32 -0400 | [diff] [blame] | 103 | |
Arnd Bergmann | 31612d6 | 2015-01-23 15:40:15 +0100 | [diff] [blame] | 104 | int davinci_cfg_reg_list(const short pins[]) |
Sudhakar Rajashekhara | c96b56c | 2009-07-16 05:45:32 -0400 | [diff] [blame] | 105 | { |
| 106 | int i, error = -EINVAL; |
| 107 | |
| 108 | if (pins) |
| 109 | for (i = 0; pins[i] >= 0; i++) { |
| 110 | error = davinci_cfg_reg(pins[i]); |
| 111 | if (error) |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return error; |
| 116 | } |