Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. |
| 4 | * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de> |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch> |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 6 | */ |
Russell King | 2f8163b | 2011-07-26 10:53:52 +0100 | [diff] [blame] | 7 | #include <linux/gpio.h> |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/io.h> |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Shawn Guo | 267dd34 | 2012-09-13 13:26:00 +0800 | [diff] [blame] | 12 | |
Shawn Guo | 50f2de6 | 2012-09-14 14:14:45 +0800 | [diff] [blame] | 13 | #include "hardware.h" |
Shawn Guo | 267dd34 | 2012-09-13 13:26:00 +0800 | [diff] [blame] | 14 | #include "iomux-mx3.h" |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * IOMUX register (base) addresses |
| 18 | */ |
Uwe Kleine-König | 1273e76 | 2009-12-16 19:06:12 +0100 | [diff] [blame] | 19 | #define IOMUX_BASE MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR) |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 20 | #define IOMUXINT_OBS1 (IOMUX_BASE + 0x000) |
| 21 | #define IOMUXINT_OBS2 (IOMUX_BASE + 0x004) |
| 22 | #define IOMUXGPR (IOMUX_BASE + 0x008) |
| 23 | #define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C) |
| 24 | #define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154) |
| 25 | |
| 26 | static DEFINE_SPINLOCK(gpio_mux_lock); |
| 27 | |
| 28 | #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3) |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 29 | |
Joe Perches | f6d4750 | 2015-05-19 18:37:49 -0700 | [diff] [blame] | 30 | static DECLARE_BITMAP(mxc_pin_alloc_map, NB_PORTS * 32); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 31 | /* |
| 32 | * set the mode for a IOMUX pin. |
| 33 | */ |
Dmitry Voytik | c300873 | 2014-11-06 22:55:04 +0400 | [diff] [blame] | 34 | void mxc_iomux_mode(unsigned int pin_mode) |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 35 | { |
Dmitry Voytik | c300873 | 2014-11-06 22:55:04 +0400 | [diff] [blame] | 36 | u32 field; |
| 37 | u32 l; |
| 38 | u32 mode; |
Luotao Fu | defa8c3 | 2008-09-09 10:19:43 +0200 | [diff] [blame] | 39 | void __iomem *reg; |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 40 | |
| 41 | reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK); |
| 42 | field = pin_mode & 0x3; |
| 43 | mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT; |
| 44 | |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 45 | spin_lock(&gpio_mux_lock); |
| 46 | |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 47 | l = imx_readl(reg); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 48 | l &= ~(0xff << (field * 8)); |
| 49 | l |= mode << (field * 8); |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 50 | imx_writel(l, reg); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 51 | |
| 52 | spin_unlock(&gpio_mux_lock); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 53 | } |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * This function configures the pad value for a IOMUX pin. |
| 57 | */ |
| 58 | void mxc_iomux_set_pad(enum iomux_pins pin, u32 config) |
| 59 | { |
Luotao Fu | defa8c3 | 2008-09-09 10:19:43 +0200 | [diff] [blame] | 60 | u32 field, l; |
| 61 | void __iomem *reg; |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 62 | |
Guennadi Liakhovetski | 4a7b98d | 2008-11-13 12:20:49 +0100 | [diff] [blame] | 63 | pin &= IOMUX_PADNUM_MASK; |
| 64 | reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4; |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 65 | field = (pin + 2) % 3; |
| 66 | |
Guennadi Liakhovetski | 4a7b98d | 2008-11-13 12:20:49 +0100 | [diff] [blame] | 67 | pr_debug("%s: reg offset = 0x%x, field = %d\n", |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 68 | __func__, (pin + 2) / 3, field); |
| 69 | |
| 70 | spin_lock(&gpio_mux_lock); |
| 71 | |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 72 | l = imx_readl(reg); |
Guennadi Liakhovetski | 4a7b98d | 2008-11-13 12:20:49 +0100 | [diff] [blame] | 73 | l &= ~(0x1ff << (field * 10)); |
| 74 | l |= config << (field * 10); |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 75 | imx_writel(l, reg); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 76 | |
| 77 | spin_unlock(&gpio_mux_lock); |
| 78 | } |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 79 | |
| 80 | /* |
Valentin Longchamp | ef754d6 | 2009-05-06 11:44:20 +0200 | [diff] [blame] | 81 | * allocs a single pin: |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 82 | * - reserves the pin so that it is not claimed by another driver |
| 83 | * - setups the iomux according to the configuration |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 84 | */ |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 85 | int mxc_iomux_alloc_pin(unsigned int pin, const char *label) |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 86 | { |
| 87 | unsigned pad = pin & IOMUX_PADNUM_MASK; |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 88 | |
| 89 | if (pad >= (PIN_MAX + 1)) { |
Colin Ian King | aa9fff5 | 2015-11-28 16:27:34 +0000 | [diff] [blame] | 90 | printk(KERN_ERR "mxc_iomux: Attempt to request nonexistent pin %u for \"%s\"\n", |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 91 | pad, label ? label : "?"); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
| 95 | if (test_and_set_bit(pad, mxc_pin_alloc_map)) { |
| 96 | printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n", |
| 97 | pad, label ? label : "?"); |
Valentin Longchamp | ef754d6 | 2009-05-06 11:44:20 +0200 | [diff] [blame] | 98 | return -EBUSY; |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 99 | } |
| 100 | mxc_iomux_mode(pin); |
| 101 | |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | } |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 104 | |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 105 | int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count, |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 106 | const char *label) |
| 107 | { |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 108 | const unsigned int *p = pin_list; |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 109 | int i; |
| 110 | int ret = -EINVAL; |
| 111 | |
| 112 | for (i = 0; i < count; i++) { |
Valentin Longchamp | ef754d6 | 2009-05-06 11:44:20 +0200 | [diff] [blame] | 113 | ret = mxc_iomux_alloc_pin(*p, label); |
| 114 | if (ret) |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 115 | goto setup_error; |
| 116 | p++; |
| 117 | } |
| 118 | return 0; |
| 119 | |
| 120 | setup_error: |
| 121 | mxc_iomux_release_multiple_pins(pin_list, i); |
| 122 | return ret; |
| 123 | } |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 124 | |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 125 | void mxc_iomux_release_pin(unsigned int pin) |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 126 | { |
| 127 | unsigned pad = pin & IOMUX_PADNUM_MASK; |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 128 | |
| 129 | if (pad < (PIN_MAX + 1)) |
| 130 | clear_bit(pad, mxc_pin_alloc_map); |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 131 | } |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 132 | |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 133 | void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count) |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 134 | { |
Uwe Kleine-König | 10a3c45 | 2011-03-02 10:59:48 +0100 | [diff] [blame] | 135 | const unsigned int *p = pin_list; |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 136 | int i; |
| 137 | |
| 138 | for (i = 0; i < count; i++) { |
| 139 | mxc_iomux_release_pin(*p); |
| 140 | p++; |
| 141 | } |
| 142 | } |
Valentin Longchamp | b722263 | 2009-01-28 15:13:50 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 145 | * This function enables/disables the general purpose function for a particular |
| 146 | * signal. |
| 147 | */ |
| 148 | void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en) |
| 149 | { |
| 150 | u32 l; |
| 151 | |
| 152 | spin_lock(&gpio_mux_lock); |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 153 | l = imx_readl(IOMUXGPR); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 154 | if (en) |
| 155 | l |= gp; |
| 156 | else |
| 157 | l &= ~gp; |
| 158 | |
Johannes Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 159 | imx_writel(l, IOMUXGPR); |
Sascha Hauer | 90292ea | 2008-07-05 10:02:50 +0200 | [diff] [blame] | 160 | spin_unlock(&gpio_mux_lock); |
| 161 | } |