Rohit Vaswani | af4a6d6 | 2013-02-22 12:19:58 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010,2013, The Linux Foundation. All rights reserved. |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/module.h> |
Rohit Vaswani | 341c203 | 2012-11-08 18:49:29 -0800 | [diff] [blame] | 13 | #include <linux/of.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
Rohit Vaswani | bb62f3e | 2013-03-01 14:14:48 -0800 | [diff] [blame] | 16 | #include <linux/io.h> |
Rohit Vaswani | a513aa8d | 2011-07-18 15:14:28 -0700 | [diff] [blame] | 17 | #include <mach/gpiomux.h> |
Rohit Vaswani | bb62f3e | 2013-03-01 14:14:48 -0800 | [diff] [blame] | 18 | #include <mach/msm_iomap.h> |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 19 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | struct msm_gpiomux_rec { |
| 21 | struct gpiomux_setting *sets[GPIOMUX_NSETTINGS]; |
| 22 | int ref; |
| 23 | }; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 24 | static DEFINE_SPINLOCK(gpiomux_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | static struct msm_gpiomux_rec *msm_gpiomux_recs; |
| 26 | static struct gpiomux_setting *msm_gpiomux_sets; |
| 27 | static unsigned msm_gpiomux_ngpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 28 | |
Rohit Vaswani | af4a6d6 | 2013-02-22 12:19:58 -0800 | [diff] [blame] | 29 | static int msm_gpiomux_store(unsigned gpio, enum msm_gpiomux_setting which, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 30 | struct gpiomux_setting *setting, struct gpiomux_setting *old_setting) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 31 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
| 33 | unsigned set_slot = gpio * GPIOMUX_NSETTINGS + which; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 34 | unsigned long irq_flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | int status = 0; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 36 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | if (!msm_gpiomux_recs) |
| 38 | return -EFAULT; |
| 39 | |
| 40 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | |
| 43 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
| 44 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | if (old_setting) { |
| 46 | if (rec->sets[which] == NULL) |
| 47 | status = 1; |
| 48 | else |
| 49 | *old_setting = *(rec->sets[which]); |
| 50 | } |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 51 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 52 | if (setting) { |
| 53 | msm_gpiomux_sets[set_slot] = *setting; |
| 54 | rec->sets[which] = &msm_gpiomux_sets[set_slot]; |
| 55 | } else { |
| 56 | rec->sets[which] = NULL; |
| 57 | } |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 58 | |
Rohit Vaswani | af4a6d6 | 2013-02-22 12:19:58 -0800 | [diff] [blame] | 59 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
| 60 | return status; |
| 61 | } |
| 62 | |
| 63 | int msm_gpiomux_write(unsigned gpio, enum msm_gpiomux_setting which, |
| 64 | struct gpiomux_setting *setting, struct gpiomux_setting *old_setting) |
| 65 | { |
| 66 | int ret; |
| 67 | unsigned long irq_flags; |
| 68 | struct gpiomux_setting *new_set; |
| 69 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
| 70 | |
| 71 | ret = msm_gpiomux_store(gpio, which, setting, old_setting); |
| 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | |
| 75 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
| 76 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | new_set = rec->ref ? rec->sets[GPIOMUX_ACTIVE] : |
| 78 | rec->sets[GPIOMUX_SUSPENDED]; |
| 79 | if (new_set) |
| 80 | __msm_gpiomux_write(gpio, *new_set); |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 81 | |
| 82 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
Rohit Vaswani | af4a6d6 | 2013-02-22 12:19:58 -0800 | [diff] [blame] | 83 | return ret; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 84 | } |
| 85 | EXPORT_SYMBOL(msm_gpiomux_write); |
| 86 | |
| 87 | int msm_gpiomux_get(unsigned gpio) |
| 88 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 89 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 90 | unsigned long irq_flags; |
| 91 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | if (!msm_gpiomux_recs) |
| 93 | return -EFAULT; |
| 94 | |
| 95 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 96 | return -EINVAL; |
| 97 | |
| 98 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | if (rec->ref++ == 0 && rec->sets[GPIOMUX_ACTIVE]) |
| 100 | __msm_gpiomux_write(gpio, *rec->sets[GPIOMUX_ACTIVE]); |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 101 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
| 102 | return 0; |
| 103 | } |
| 104 | EXPORT_SYMBOL(msm_gpiomux_get); |
| 105 | |
| 106 | int msm_gpiomux_put(unsigned gpio) |
| 107 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 108 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 109 | unsigned long irq_flags; |
| 110 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | if (!msm_gpiomux_recs) |
| 112 | return -EFAULT; |
| 113 | |
| 114 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 115 | return -EINVAL; |
| 116 | |
| 117 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | BUG_ON(rec->ref == 0); |
| 119 | if (--rec->ref == 0 && rec->sets[GPIOMUX_SUSPENDED]) |
| 120 | __msm_gpiomux_write(gpio, *rec->sets[GPIOMUX_SUSPENDED]); |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 121 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
| 122 | return 0; |
| 123 | } |
| 124 | EXPORT_SYMBOL(msm_gpiomux_put); |
| 125 | |
Rohit Vaswani | bb62f3e | 2013-03-01 14:14:48 -0800 | [diff] [blame] | 126 | void msm_tlmm_misc_reg_write(enum msm_tlmm_misc_reg misc_reg, int val) |
| 127 | { |
| 128 | writel_relaxed(val, MSM_TLMM_BASE + misc_reg); |
| 129 | /* ensure the write completes before returning */ |
| 130 | mb(); |
| 131 | } |
| 132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | int msm_gpiomux_init(size_t ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 134 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 135 | if (!ngpio) |
| 136 | return -EINVAL; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 137 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | if (msm_gpiomux_recs) |
| 139 | return -EPERM; |
| 140 | |
| 141 | msm_gpiomux_recs = kzalloc(sizeof(struct msm_gpiomux_rec) * ngpio, |
| 142 | GFP_KERNEL); |
| 143 | if (!msm_gpiomux_recs) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | /* There is no need to zero this memory, as clients will be blindly |
| 147 | * installing settings on top of it. |
| 148 | */ |
| 149 | msm_gpiomux_sets = kmalloc(sizeof(struct gpiomux_setting) * ngpio * |
| 150 | GPIOMUX_NSETTINGS, GFP_KERNEL); |
| 151 | if (!msm_gpiomux_sets) { |
| 152 | kfree(msm_gpiomux_recs); |
| 153 | msm_gpiomux_recs = NULL; |
| 154 | return -ENOMEM; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 155 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | |
| 157 | msm_gpiomux_ngpio = ngpio; |
| 158 | |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | EXPORT_SYMBOL(msm_gpiomux_init); |
| 162 | |
Rohit Vaswani | af4a6d6 | 2013-02-22 12:19:58 -0800 | [diff] [blame] | 163 | void msm_gpiomux_install_nowrite(struct msm_gpiomux_config *configs, |
| 164 | unsigned nconfigs) |
| 165 | { |
| 166 | unsigned c, s; |
| 167 | int rc; |
| 168 | |
| 169 | for (c = 0; c < nconfigs; ++c) { |
| 170 | for (s = 0; s < GPIOMUX_NSETTINGS; ++s) { |
| 171 | rc = msm_gpiomux_store(configs[c].gpio, s, |
| 172 | configs[c].settings[s], NULL); |
| 173 | if (rc) |
| 174 | pr_err("%s: write failure: %d\n", __func__, rc); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 179 | void msm_gpiomux_install(struct msm_gpiomux_config *configs, unsigned nconfigs) |
| 180 | { |
| 181 | unsigned c, s; |
| 182 | int rc; |
| 183 | |
| 184 | for (c = 0; c < nconfigs; ++c) { |
| 185 | for (s = 0; s < GPIOMUX_NSETTINGS; ++s) { |
| 186 | rc = msm_gpiomux_write(configs[c].gpio, s, |
| 187 | configs[c].settings[s], NULL); |
| 188 | if (rc) |
| 189 | pr_err("%s: write failure: %d\n", __func__, rc); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | EXPORT_SYMBOL(msm_gpiomux_install); |
Rohit Vaswani | 341c203 | 2012-11-08 18:49:29 -0800 | [diff] [blame] | 194 | |
| 195 | int msm_gpiomux_init_dt(void) |
| 196 | { |
| 197 | int rc; |
| 198 | unsigned int ngpio; |
| 199 | struct device_node *of_gpio_node; |
| 200 | |
| 201 | of_gpio_node = of_find_compatible_node(NULL, NULL, "qcom,msm-gpio"); |
| 202 | if (!of_gpio_node) { |
| 203 | pr_err("%s: Failed to find qcom,msm-gpio node\n", __func__); |
| 204 | return -ENODEV; |
| 205 | } |
| 206 | |
| 207 | rc = of_property_read_u32(of_gpio_node, "ngpio", &ngpio); |
| 208 | if (rc) { |
| 209 | pr_err("%s: Failed to find ngpio property in msm-gpio device node %d\n" |
| 210 | , __func__, rc); |
| 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | return msm_gpiomux_init(ngpio); |
| 215 | } |
| 216 | EXPORT_SYMBOL(msm_gpiomux_init_dt); |