blob: 6a2ff0a654a5b53efce08a40c60a1d25f603b166 [file] [log] [blame]
Vladimir Barinov83f53222007-07-10 13:10:04 +01001/*
Kevin Hilman5526b3f2009-04-14 09:50:37 -05002 * Utility to set the DAVINCI MUX register from a table in mux.h
Vladimir Barinov83f53222007-07-10 13:10:04 +01003 *
4 * Author: Vladimir Barinov, MontaVista Software, Inc. <source@mvista.com>
5 *
Kevin Hilman5526b3f2009-04-14 09:50:37 -05006 * Based on linux/arch/arm/plat-omap/mux.c:
7 * Copyright (C) 2003 - 2005 Nokia Corporation
8 *
9 * Written by Tony Lindgren
10 *
Vladimir Barinov83f53222007-07-10 13:10:04 +010011 * 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 Hilman5526b3f2009-04-14 09:50:37 -050015 *
16 * Copyright (C) 2008 Texas Instruments.
Vladimir Barinov83f53222007-07-10 13:10:04 +010017 */
Joe Perchesa7ca2bc2014-10-31 17:51:51 -070018
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Vladimir Barinov83f53222007-07-10 13:10:04 +010021#include <linux/io.h>
Kevin Hilman5526b3f2009-04-14 09:50:37 -050022#include <linux/module.h>
Vladimir Barinov83f53222007-07-10 13:10:04 +010023#include <linux/spinlock.h>
24
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/mux.h>
Mark A. Greer0e585952009-04-15 12:39:48 -070026#include <mach/common.h>
Kevin Hilman5526b3f2009-04-14 09:50:37 -050027
Cyril Chemparathy779b0d52010-05-07 17:06:38 -040028static void __iomem *pinmux_base;
29
Kevin Hilman5526b3f2009-04-14 09:50:37 -050030/*
31 * Sets the DAVINCI MUX register based on the table
32 */
Arnd Bergmann31612d62015-01-23 15:40:15 +010033int davinci_cfg_reg(const unsigned long index)
Kevin Hilman5526b3f2009-04-14 09:50:37 -050034{
35 static DEFINE_SPINLOCK(mux_spin_lock);
Mark A. Greer0e585952009-04-15 12:39:48 -070036 struct davinci_soc_info *soc_info = &davinci_soc_info;
Kevin Hilman5526b3f2009-04-14 09:50:37 -050037 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 Chemparathy779b0d52010-05-07 17:06:38 -040042 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 Hilman5526b3f2009-04-14 09:50:37 -050050
Mark A. Greer0e585952009-04-15 12:39:48 -070051 if (index >= soc_info->pinmux_pins_num) {
Joe Perchesa7ca2bc2014-10-31 17:51:51 -070052 pr_err("Invalid pin mux index: %lu (%lu)\n",
Mark A. Greer0e585952009-04-15 12:39:48 -070053 index, soc_info->pinmux_pins_num);
Kevin Hilman5526b3f2009-04-14 09:50:37 -050054 dump_stack();
55 return -ENODEV;
Vladimir Barinov83f53222007-07-10 13:10:04 +010056 }
57
Mark A. Greer0e585952009-04-15 12:39:48 -070058 cfg = &soc_info->pinmux_pins[index];
Kevin Hilman5526b3f2009-04-14 09:50:37 -050059
60 if (cfg->name == NULL) {
Joe Perchesa7ca2bc2014-10-31 17:51:51 -070061 pr_err("No entry for the specified index\n");
Kevin Hilman5526b3f2009-04-14 09:50:37 -050062 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 Chemparathy779b0d52010-05-07 17:06:38 -040070 reg_orig = __raw_readl(pinmux_base + cfg->mux_reg);
Kevin Hilman5526b3f2009-04-14 09:50:37 -050071
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 Chemparathy779b0d52010-05-07 17:06:38 -040082 __raw_writel(reg, pinmux_base + cfg->mux_reg);
Kevin Hilman5526b3f2009-04-14 09:50:37 -050083 spin_unlock_irqrestore(&mux_spin_lock, flags);
84 }
85
86 if (warn) {
87#ifdef CONFIG_DAVINCI_MUX_WARNINGS
Joe Perchesa7ca2bc2014-10-31 17:51:51 -070088 pr_warn("initialized %s\n", cfg->name);
Kevin Hilman5526b3f2009-04-14 09:50:37 -050089#endif
90 }
91
92#ifdef CONFIG_DAVINCI_MUX_DEBUG
93 if (cfg->debug || warn) {
Joe Perchesa7ca2bc2014-10-31 17:51:51 -070094 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 Hilman5526b3f2009-04-14 09:50:37 -050097 }
98#endif
99
100 return 0;
Vladimir Barinov83f53222007-07-10 13:10:04 +0100101}
Kevin Hilman5526b3f2009-04-14 09:50:37 -0500102EXPORT_SYMBOL(davinci_cfg_reg);
Sudhakar Rajashekharac96b56c2009-07-16 05:45:32 -0400103
Arnd Bergmann31612d62015-01-23 15:40:15 +0100104int davinci_cfg_reg_list(const short pins[])
Sudhakar Rajashekharac96b56c2009-07-16 05:45:32 -0400105{
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}