OMAP3 SRF: Voltage scaling support
[linux-ginger.git] / arch / arm / mach-omap2 / debobs.c
blobf83771f59b0058c5b41f458f7db354fa291907d9
1 /*
2 * arch/arm/mach-omap2/debobs.c
4 * Handle debobs pads
6 * Copyright (C) 2008 Nokia Corporation
8 * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License. See the file "COPYING" in the main directory of this
12 * archive for more details.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/debugfs.h>
28 #include <linux/uaccess.h>
29 #include <linux/module.h>
30 #include <linux/gpio.h>
32 #include <plat/control.h>
33 #include <plat/mux.h>
34 #include <plat/board.h>
36 #define ETK_GPIO_BEGIN 12
37 #define ETK_GPIO(i) (ETK_GPIO_BEGIN + i)
38 #define NUM_OF_DEBOBS_PADS 18
40 static int debobs_initialized;
42 enum debobs_pad_mode {
43 GPIO = 0,
44 OBS = 1,
45 ETK = 2,
46 NO_MODE = 3,
49 static char *debobs_pad_mode_names[] = {
50 [GPIO] = "GPIO",
51 [OBS] = "OBS",
52 [ETK] = "ETK",
55 struct obs {
56 u16 offset;
57 u8 value;
58 u8 mask;
61 struct debobs_pad {
62 enum debobs_pad_mode mode;
63 struct obs core_obs;
64 struct obs wakeup_obs;
67 static struct debobs_pad debobs_pads[NUM_OF_DEBOBS_PADS];
69 static int debobs_mode_open(struct inode *inode, struct file *file)
71 file->private_data = inode->i_private;
73 return 0;
76 static ssize_t debobs_mode_read(struct file *file, char __user *user_buf,
77 size_t count, loff_t *ppos)
79 char buffer[10];
80 int size;
81 int pad_number = (int)file->private_data;
82 struct debobs_pad *e = &debobs_pads[pad_number];
84 size = snprintf(buffer, sizeof(buffer), "%s\n",
85 debobs_pad_mode_names[e->mode]);
86 return simple_read_from_buffer(user_buf, count, ppos, buffer, size);
89 static ssize_t debobs_mode_write(struct file *file, const char __user *user_buf,
90 size_t count, loff_t *ppos)
92 char buffer[10];
93 int buf_size, i, pad_number;
94 u16 muxmode = OMAP34XX_MUX_MODE7;
96 memset(buffer, 0, sizeof(buffer));
97 buf_size = min(count, (sizeof(buffer)-1));
99 if (copy_from_user(buffer, user_buf, buf_size))
100 return -EFAULT;
102 pad_number = (int)file->private_data;
104 for (i = 0; i < NO_MODE; i++) {
105 if (!strnicmp(debobs_pad_mode_names[i],
106 buffer,
107 strlen(debobs_pad_mode_names[i]))) {
108 switch (i) {
109 case ETK:
110 muxmode = OMAP34XX_MUX_MODE0;
111 break;
112 case GPIO:
113 muxmode = OMAP34XX_MUX_MODE4;
114 break;
115 case OBS:
116 muxmode = OMAP34XX_MUX_MODE7;
117 break;
119 omap_ctrl_writew(muxmode,
120 OMAP343X_PADCONF_ETK(pad_number));
121 debobs_pads[pad_number].mode = i;
123 return count;
127 return -EINVAL;
130 static const struct file_operations debobs_mode_fops = {
131 .open = debobs_mode_open,
132 .read = debobs_mode_read,
133 .write = debobs_mode_write,
136 static int debobs_get(void *data, u64 *val)
138 struct obs *o = data;
140 *val = o->value;
142 return 0;
145 static int debobs_set(void *data, u64 val)
147 struct obs *o = data;
149 val &= BIT(o->mask) - 1;
151 omap_ctrl_writeb(val, o->offset);
152 o->value = val;
154 return 0;
157 DEFINE_SIMPLE_ATTRIBUTE(debobs_fops, debobs_get, debobs_set, "%llu\n");
159 static inline int __init _new_debobs_pad(struct debobs_pad *pad, char *name,
160 int number, struct dentry *root)
162 struct dentry *d;
163 struct obs *o;
165 d = debugfs_create_dir(name, root);
166 if (IS_ERR(d))
167 return PTR_ERR(d);
169 omap_ctrl_writew(OMAP34XX_MUX_MODE4, OMAP343X_PADCONF_ETK(number));
170 gpio_direction_input(ETK_GPIO(number));
171 gpio_export(ETK_GPIO(number), 1);
172 (void) debugfs_create_file("mode", S_IRUGO | S_IWUGO, d,
173 (void *)number, &debobs_mode_fops);
175 o = &pad->core_obs;
176 o->offset = OMAP343X_CONTROL_DEBOBS(number);
177 o->value = omap_ctrl_readw(o->offset);
178 o->mask = 7;
179 (void) debugfs_create_file("coreobs", S_IRUGO | S_IWUGO, d, o,
180 &debobs_fops);
182 o = &pad->wakeup_obs;
183 o->offset = OMAP343X_CONTROL_WKUP_DEBOBSMUX(number);
184 o->value = omap_ctrl_readb(o->offset);
185 o->mask = 5;
186 (void) debugfs_create_file("wakeupobs", S_IRUGO | S_IWUGO, d, o,
187 &debobs_fops);
189 return 0;
192 /* Public functions */
194 void debug_gpio_set(unsigned gpio, int value)
196 if (!debobs_initialized)
197 return ;
199 WARN_ON(gpio >= NUM_OF_DEBOBS_PADS);
200 if (gpio < NUM_OF_DEBOBS_PADS)
201 __gpio_set_value(ETK_GPIO(gpio), value);
204 int debug_gpio_get(unsigned gpio)
206 if (!debobs_initialized)
207 return -EINVAL;
209 WARN_ON(gpio >= NUM_OF_DEBOBS_PADS);
210 if (gpio < NUM_OF_DEBOBS_PADS)
211 return __gpio_get_value(ETK_GPIO(gpio));
213 return -EINVAL;
216 int __init init_debobs(void)
218 struct dentry *debobs_root;
219 int i, err;
220 char name[10];
222 debobs_root = debugfs_create_dir("debobs", NULL);
223 if (IS_ERR(debobs_root))
224 return PTR_ERR(debobs_root);
226 for (i = 0; i < NUM_OF_DEBOBS_PADS; i++) {
227 snprintf(name, sizeof(name), "hw_dbg%d", i);
228 if (!gpio_request(ETK_GPIO(i), name)) {
229 err = _new_debobs_pad(&debobs_pads[i], name, i,
230 debobs_root);
231 } else
232 gpio_free(ETK_GPIO(i));
235 debobs_initialized = 1;
237 return 0;
240 late_initcall_sync(init_debobs);