Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 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> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
| 15 | #include "gpiomux.h" |
| 16 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 17 | struct msm_gpiomux_rec { |
| 18 | struct gpiomux_setting *sets[GPIOMUX_NSETTINGS]; |
| 19 | int ref; |
| 20 | }; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 21 | static DEFINE_SPINLOCK(gpiomux_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | static struct msm_gpiomux_rec *msm_gpiomux_recs; |
| 23 | static struct gpiomux_setting *msm_gpiomux_sets; |
| 24 | static unsigned msm_gpiomux_ngpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 25 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | int msm_gpiomux_write(unsigned gpio, enum msm_gpiomux_setting which, |
| 27 | struct gpiomux_setting *setting, struct gpiomux_setting *old_setting) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 28 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
| 30 | unsigned set_slot = gpio * GPIOMUX_NSETTINGS + which; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 31 | unsigned long irq_flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | struct gpiomux_setting *new_set; |
| 33 | int status = 0; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 34 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | if (!msm_gpiomux_recs) |
| 36 | return -EFAULT; |
| 37 | |
| 38 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 39 | return -EINVAL; |
| 40 | |
| 41 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
| 42 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | if (old_setting) { |
| 44 | if (rec->sets[which] == NULL) |
| 45 | status = 1; |
| 46 | else |
| 47 | *old_setting = *(rec->sets[which]); |
| 48 | } |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 49 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | if (setting) { |
| 51 | msm_gpiomux_sets[set_slot] = *setting; |
| 52 | rec->sets[which] = &msm_gpiomux_sets[set_slot]; |
| 53 | } else { |
| 54 | rec->sets[which] = NULL; |
| 55 | } |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 56 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | new_set = rec->ref ? rec->sets[GPIOMUX_ACTIVE] : |
| 58 | rec->sets[GPIOMUX_SUSPENDED]; |
| 59 | if (new_set) |
| 60 | __msm_gpiomux_write(gpio, *new_set); |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 61 | |
| 62 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 63 | return status; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL(msm_gpiomux_write); |
| 66 | |
| 67 | int msm_gpiomux_get(unsigned gpio) |
| 68 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 70 | unsigned long irq_flags; |
| 71 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 72 | if (!msm_gpiomux_recs) |
| 73 | return -EFAULT; |
| 74 | |
| 75 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | |
| 78 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | if (rec->ref++ == 0 && rec->sets[GPIOMUX_ACTIVE]) |
| 80 | __msm_gpiomux_write(gpio, *rec->sets[GPIOMUX_ACTIVE]); |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 81 | spin_unlock_irqrestore(&gpiomux_lock, irq_flags); |
| 82 | return 0; |
| 83 | } |
| 84 | EXPORT_SYMBOL(msm_gpiomux_get); |
| 85 | |
| 86 | int msm_gpiomux_put(unsigned gpio) |
| 87 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | struct msm_gpiomux_rec *rec = msm_gpiomux_recs + gpio; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 89 | unsigned long irq_flags; |
| 90 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | if (!msm_gpiomux_recs) |
| 92 | return -EFAULT; |
| 93 | |
| 94 | if (gpio >= msm_gpiomux_ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 95 | return -EINVAL; |
| 96 | |
| 97 | spin_lock_irqsave(&gpiomux_lock, irq_flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | BUG_ON(rec->ref == 0); |
| 99 | if (--rec->ref == 0 && rec->sets[GPIOMUX_SUSPENDED]) |
| 100 | __msm_gpiomux_write(gpio, *rec->sets[GPIOMUX_SUSPENDED]); |
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_put); |
| 105 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | int msm_gpiomux_init(size_t ngpio) |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 107 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 108 | if (!ngpio) |
| 109 | return -EINVAL; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 110 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | if (msm_gpiomux_recs) |
| 112 | return -EPERM; |
| 113 | |
| 114 | msm_gpiomux_recs = kzalloc(sizeof(struct msm_gpiomux_rec) * ngpio, |
| 115 | GFP_KERNEL); |
| 116 | if (!msm_gpiomux_recs) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | /* There is no need to zero this memory, as clients will be blindly |
| 120 | * installing settings on top of it. |
| 121 | */ |
| 122 | msm_gpiomux_sets = kmalloc(sizeof(struct gpiomux_setting) * ngpio * |
| 123 | GPIOMUX_NSETTINGS, GFP_KERNEL); |
| 124 | if (!msm_gpiomux_sets) { |
| 125 | kfree(msm_gpiomux_recs); |
| 126 | msm_gpiomux_recs = NULL; |
| 127 | return -ENOMEM; |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 128 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | |
| 130 | msm_gpiomux_ngpio = ngpio; |
| 131 | |
Gregory Bean | 1963a2a | 2010-08-28 10:05:44 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 134 | EXPORT_SYMBOL(msm_gpiomux_init); |
| 135 | |
| 136 | void msm_gpiomux_install(struct msm_gpiomux_config *configs, unsigned nconfigs) |
| 137 | { |
| 138 | unsigned c, s; |
| 139 | int rc; |
| 140 | |
| 141 | for (c = 0; c < nconfigs; ++c) { |
| 142 | for (s = 0; s < GPIOMUX_NSETTINGS; ++s) { |
| 143 | rc = msm_gpiomux_write(configs[c].gpio, s, |
| 144 | configs[c].settings[s], NULL); |
| 145 | if (rc) |
| 146 | pr_err("%s: write failure: %d\n", __func__, rc); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | EXPORT_SYMBOL(msm_gpiomux_install); |