blob: 8a9ae09603d6978ad2c4c2f29db7d60256da4198 [file] [log] [blame]
Magnus Damm2967dab2008-10-08 20:41:43 +09001/*
Paul Mundtb3c185a2012-06-20 17:29:04 +09002 * SuperH Pin Function Controller support.
Magnus Damm2967dab2008-10-08 20:41:43 +09003 *
4 * Copyright (C) 2008 Magnus Damm
Paul Mundtb3c185a2012-06-20 17:29:04 +09005 * Copyright (C) 2009 - 2012 Paul Mundt
Magnus Damm2967dab2008-10-08 20:41:43 +09006 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
Paul Mundtb72421d2010-10-04 03:54:56 +090011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Magnus Damm2967dab2008-10-08 20:41:43 +090013#include <linux/errno.h>
14#include <linux/kernel.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/sh_pfc.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090016#include <linux/module.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090017#include <linux/err.h>
18#include <linux/io.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090019#include <linux/bitops.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090020#include <linux/slab.h>
21#include <linux/ioport.h>
22
Paul Mundtb3c185a2012-06-20 17:29:04 +090023static struct sh_pfc *sh_pfc __read_mostly;
24
25static inline bool sh_pfc_initialized(void)
26{
27 return !!sh_pfc;
28}
29
30static void pfc_iounmap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090031{
32 int k;
33
Paul Mundtb3c185a2012-06-20 17:29:04 +090034 for (k = 0; k < pfc->num_resources; k++)
35 if (pfc->window[k].virt)
36 iounmap(pfc->window[k].virt);
Magnus Dammb0e10212011-12-09 12:14:27 +090037
Paul Mundtb3c185a2012-06-20 17:29:04 +090038 kfree(pfc->window);
39 pfc->window = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090040}
41
Paul Mundtb3c185a2012-06-20 17:29:04 +090042static int pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090043{
44 struct resource *res;
45 int k;
46
Paul Mundtb3c185a2012-06-20 17:29:04 +090047 if (!pfc->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090048 return 0;
49
Paul Mundtb3c185a2012-06-20 17:29:04 +090050 pfc->window = kzalloc(pfc->num_resources * sizeof(*pfc->window),
Magnus Dammb0e10212011-12-09 12:14:27 +090051 GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090052 if (!pfc->window)
Magnus Dammb0e10212011-12-09 12:14:27 +090053 goto err1;
54
Paul Mundtb3c185a2012-06-20 17:29:04 +090055 for (k = 0; k < pfc->num_resources; k++) {
56 res = pfc->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090057 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090058 pfc->window[k].phys = res->start;
59 pfc->window[k].size = resource_size(res);
60 pfc->window[k].virt = ioremap_nocache(res->start,
Magnus Dammb0e10212011-12-09 12:14:27 +090061 resource_size(res));
Paul Mundtb3c185a2012-06-20 17:29:04 +090062 if (!pfc->window[k].virt)
Magnus Dammb0e10212011-12-09 12:14:27 +090063 goto err2;
64 }
65
66 return 0;
67
68err2:
Paul Mundtb3c185a2012-06-20 17:29:04 +090069 pfc_iounmap(pfc);
Magnus Dammb0e10212011-12-09 12:14:27 +090070err1:
71 return -1;
72}
73
Paul Mundtb3c185a2012-06-20 17:29:04 +090074static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090075 unsigned long address)
76{
77 struct pfc_window *window;
78 int k;
79
80 /* scan through physical windows and convert address */
Paul Mundtb3c185a2012-06-20 17:29:04 +090081 for (k = 0; k < pfc->num_resources; k++) {
82 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090083
84 if (address < window->phys)
85 continue;
86
87 if (address >= (window->phys + window->size))
88 continue;
89
90 return window->virt + (address - window->phys);
91 }
92
93 /* no windows defined, register must be 1:1 mapped virt:phys */
94 return (void __iomem *)address;
95}
Magnus Damm2967dab2008-10-08 20:41:43 +090096
Magnus Damm2967dab2008-10-08 20:41:43 +090097static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
98{
99 if (enum_id < r->begin)
100 return 0;
101
102 if (enum_id > r->end)
103 return 0;
104
105 return 1;
106}
107
Magnus Dammb0e10212011-12-09 12:14:27 +0900108static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900109 unsigned long reg_width)
110{
111 switch (reg_width) {
112 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900113 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900114 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900115 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900116 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900117 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900118 }
119
120 BUG();
121 return 0;
122}
123
Magnus Dammb0e10212011-12-09 12:14:27 +0900124static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900125 unsigned long reg_width,
126 unsigned long data)
127{
128 switch (reg_width) {
129 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900130 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900131 return;
132 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900133 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900134 return;
135 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900136 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900137 return;
138 }
139
140 BUG();
141}
142
Paul Mundtb3c185a2012-06-20 17:29:04 +0900143int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900144{
145 unsigned long pos;
146
147 pos = dr->reg_width - (in_pos + 1);
148
149 pr_debug("read_bit: addr = %lx, pos = %ld, "
150 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
151
152 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
153}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900154EXPORT_SYMBOL_GPL(sh_pfc_read_bit);
Magnus Damm92554d92011-12-14 01:00:37 +0900155
Paul Mundtb3c185a2012-06-20 17:29:04 +0900156void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
157 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900158{
159 unsigned long pos;
160
161 pos = dr->reg_width - (in_pos + 1);
162
Paul Mundtca6f2d72009-12-09 15:51:27 +0900163 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900164 "r_width = %ld\n",
165 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900166
167 if (value)
168 set_bit(pos, &dr->reg_shadow);
169 else
170 clear_bit(pos, &dr->reg_shadow);
171
Magnus Dammb0e10212011-12-09 12:14:27 +0900172 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900173}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900174EXPORT_SYMBOL_GPL(sh_pfc_write_bit);
Magnus Damm32920942008-12-25 18:17:26 +0900175
Paul Mundtb3c185a2012-06-20 17:29:04 +0900176static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900177 struct pinmux_cfg_reg *crp,
178 unsigned long in_pos,
179 void __iomem **mapped_regp,
180 unsigned long *maskp,
181 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900182{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900183 int k;
184
Paul Mundtb3c185a2012-06-20 17:29:04 +0900185 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900186
Magnus Dammf78a26f2011-12-14 01:01:05 +0900187 if (crp->field_width) {
188 *maskp = (1 << crp->field_width) - 1;
189 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
190 } else {
191 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
192 *posp = crp->reg_width;
193 for (k = 0; k <= in_pos; k++)
194 *posp -= crp->var_field_width[k];
195 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900196}
Magnus Damm2967dab2008-10-08 20:41:43 +0900197
Paul Mundtb3c185a2012-06-20 17:29:04 +0900198static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900199 struct pinmux_cfg_reg *crp,
200 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900201{
Magnus Damm18925e12011-12-14 01:00:55 +0900202 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900203 unsigned long mask, pos;
204
Paul Mundtb3c185a2012-06-20 17:29:04 +0900205 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900206
Magnus Damm18925e12011-12-14 01:00:55 +0900207 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900208 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900209 crp->reg, field, crp->reg_width, crp->field_width);
210
211 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
212}
213
Paul Mundtb3c185a2012-06-20 17:29:04 +0900214static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900215 struct pinmux_cfg_reg *crp,
216 unsigned long field, unsigned long value)
217{
218 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900219 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900220
Paul Mundtb3c185a2012-06-20 17:29:04 +0900221 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900222
223 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
224 "r_width = %ld, f_width = %ld\n",
225 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900226
227 mask = ~(mask << pos);
228 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900229
Magnus Damme499ada2011-12-14 01:01:14 +0900230 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
231 data &= mask;
232 data |= value;
233
Paul Mundtb3c185a2012-06-20 17:29:04 +0900234 if (pfc->unlock_reg)
235 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900236 32, ~data);
237
238 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900239}
240
Paul Mundtb3c185a2012-06-20 17:29:04 +0900241static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900242{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900243 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900244 struct pinmux_data_reg *data_reg;
245 int k, n;
246
Paul Mundtb3c185a2012-06-20 17:29:04 +0900247 if (!enum_in_range(gpiop->enum_id, &pfc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900248 return -1;
249
250 k = 0;
251 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900252 data_reg = pfc->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900253
254 if (!data_reg->reg_width)
255 break;
256
Paul Mundtb3c185a2012-06-20 17:29:04 +0900257 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900258
Magnus Damm2967dab2008-10-08 20:41:43 +0900259 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900260 if (data_reg->enum_ids[n] == gpiop->enum_id) {
261 gpiop->flags &= ~PINMUX_FLAG_DREG;
262 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
263 gpiop->flags &= ~PINMUX_FLAG_DBIT;
264 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900265 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900266 }
267 }
268 k++;
269 }
270
Magnus Damm18801be2008-12-25 18:17:09 +0900271 BUG();
272
Magnus Damm2967dab2008-10-08 20:41:43 +0900273 return -1;
274}
275
Paul Mundtb3c185a2012-06-20 17:29:04 +0900276static void setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900277{
278 struct pinmux_data_reg *drp;
279 int k;
280
Paul Mundtb3c185a2012-06-20 17:29:04 +0900281 for (k = pfc->first_gpio; k <= pfc->last_gpio; k++)
282 setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900283
284 k = 0;
285 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900286 drp = pfc->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900287
288 if (!drp->reg_width)
289 break;
290
Magnus Dammb0e10212011-12-09 12:14:27 +0900291 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
292 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900293 k++;
294 }
295}
296
Paul Mundtb3c185a2012-06-20 17:29:04 +0900297int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900298 struct pinmux_data_reg **drp, int *bitp)
299{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900300 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900301 int k, n;
302
Paul Mundtb3c185a2012-06-20 17:29:04 +0900303 if (!enum_in_range(gpiop->enum_id, &pfc->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900304 return -1;
305
306 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
307 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900308 *drp = pfc->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900309 *bitp = n;
310 return 0;
311}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900312EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg);
Magnus Damm18801be2008-12-25 18:17:09 +0900313
Paul Mundtb3c185a2012-06-20 17:29:04 +0900314static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900315 struct pinmux_cfg_reg **crp,
316 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900317 unsigned long **cntp)
318{
319 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900320 unsigned long r_width, f_width, curr_width, ncomb;
321 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900322
323 k = 0;
324 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900325 config_reg = pfc->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900326
327 r_width = config_reg->reg_width;
328 f_width = config_reg->field_width;
329
330 if (!r_width)
331 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900332
333 pos = 0;
334 m = 0;
335 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
336 if (f_width)
337 curr_width = f_width;
338 else
339 curr_width = config_reg->var_field_width[m];
340
341 ncomb = 1 << curr_width;
342 for (n = 0; n < ncomb; n++) {
343 if (config_reg->enum_ids[pos + n] == enum_id) {
344 *crp = config_reg;
345 *fieldp = m;
346 *valuep = n;
347 *cntp = &config_reg->cnt[m];
348 return 0;
349 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900350 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900351 pos += ncomb;
352 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900353 }
354 k++;
355 }
356
357 return -1;
358}
359
Paul Mundtb3c185a2012-06-20 17:29:04 +0900360int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
361 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900362{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900363 pinmux_enum_t enum_id = pfc->gpios[gpio].enum_id;
364 pinmux_enum_t *data = pfc->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900365 int k;
366
Paul Mundtb3c185a2012-06-20 17:29:04 +0900367 if (!enum_in_range(enum_id, &pfc->data)) {
368 if (!enum_in_range(enum_id, &pfc->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900369 pr_err("non data/mark enum_id for gpio %d\n", gpio);
370 return -1;
371 }
372 }
373
374 if (pos) {
375 *enum_idp = data[pos + 1];
376 return pos + 1;
377 }
378
Paul Mundtb3c185a2012-06-20 17:29:04 +0900379 for (k = 0; k < pfc->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900380 if (data[k] == enum_id) {
381 *enum_idp = data[k + 1];
382 return k + 1;
383 }
384 }
385
386 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
387 return -1;
388}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900389EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum);
Magnus Damm2967dab2008-10-08 20:41:43 +0900390
Paul Mundtb3c185a2012-06-20 17:29:04 +0900391int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
392 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900393{
394 struct pinmux_cfg_reg *cr = NULL;
395 pinmux_enum_t enum_id;
396 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900397 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900398 unsigned long *cntp;
399
400 switch (pinmux_type) {
401
402 case PINMUX_TYPE_FUNCTION:
403 range = NULL;
404 break;
405
406 case PINMUX_TYPE_OUTPUT:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900407 range = &pfc->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900408 break;
409
410 case PINMUX_TYPE_INPUT:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900411 range = &pfc->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900412 break;
413
414 case PINMUX_TYPE_INPUT_PULLUP:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900415 range = &pfc->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900416 break;
417
418 case PINMUX_TYPE_INPUT_PULLDOWN:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900419 range = &pfc->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900420 break;
421
422 default:
423 goto out_err;
424 }
425
426 pos = 0;
427 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900428 field = 0;
429 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900430 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900431 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900432 if (pos <= 0)
433 goto out_err;
434
435 if (!enum_id)
436 break;
437
Magnus Damm50dd3142010-01-19 13:52:28 +0000438 /* first check if this is a function enum */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900439 in_range = enum_in_range(enum_id, &pfc->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000440 if (!in_range) {
441 /* not a function enum */
442 if (range) {
443 /*
444 * other range exists, so this pin is
445 * a regular GPIO pin that now is being
446 * bound to a specific direction.
447 *
448 * for this case we only allow function enums
449 * and the enums that match the other range.
450 */
451 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900452
Magnus Damm50dd3142010-01-19 13:52:28 +0000453 /*
454 * special case pass through for fixed
455 * input-only or output-only pins without
456 * function enum register association.
457 */
458 if (in_range && enum_id == range->force)
459 continue;
460 } else {
461 /*
462 * no other range exists, so this pin
463 * must then be of the function type.
464 *
465 * allow function type pins to select
466 * any combination of function/in/out
467 * in their MARK lists.
468 */
469 in_range = 1;
470 }
Magnus Damm42eed422008-10-22 18:29:17 +0900471 }
472
Magnus Damm2967dab2008-10-08 20:41:43 +0900473 if (!in_range)
474 continue;
475
Paul Mundtb3c185a2012-06-20 17:29:04 +0900476 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900477 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900478 goto out_err;
479
480 switch (cfg_mode) {
481 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900482 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900483 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900484 continue;
485 break;
486
487 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900488 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900489 *cntp = *cntp + 1;
490 break;
491
492 case GPIO_CFG_FREE:
493 *cntp = *cntp - 1;
494 break;
495 }
496 }
497
498 return 0;
499 out_err:
500 return -1;
501}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900502EXPORT_SYMBOL_GPL(sh_pfc_config_gpio);
Magnus Damm2967dab2008-10-08 20:41:43 +0900503
Paul Mundtb3c185a2012-06-20 17:29:04 +0900504int sh_pfc_set_direction(struct sh_pfc *pfc, unsigned gpio,
505 int new_pinmux_type)
Magnus Damm2967dab2008-10-08 20:41:43 +0900506{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900507 int pinmux_type;
508 int ret = -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900509
Paul Mundtb3c185a2012-06-20 17:29:04 +0900510 if (!pfc)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900511 goto err_out;
512
Paul Mundtb3c185a2012-06-20 17:29:04 +0900513 pinmux_type = pfc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
Magnus Damm2967dab2008-10-08 20:41:43 +0900514
515 switch (pinmux_type) {
516 case PINMUX_TYPE_GPIO:
517 break;
518 case PINMUX_TYPE_OUTPUT:
519 case PINMUX_TYPE_INPUT:
520 case PINMUX_TYPE_INPUT_PULLUP:
521 case PINMUX_TYPE_INPUT_PULLDOWN:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900522 sh_pfc_config_gpio(pfc, gpio, pinmux_type, GPIO_CFG_FREE);
Magnus Damm2967dab2008-10-08 20:41:43 +0900523 break;
524 default:
525 goto err_out;
526 }
527
Paul Mundtb3c185a2012-06-20 17:29:04 +0900528 if (sh_pfc_config_gpio(pfc, gpio,
Magnus Damm2967dab2008-10-08 20:41:43 +0900529 new_pinmux_type,
530 GPIO_CFG_DRYRUN) != 0)
531 goto err_out;
532
Paul Mundtb3c185a2012-06-20 17:29:04 +0900533 if (sh_pfc_config_gpio(pfc, gpio,
Magnus Damm2967dab2008-10-08 20:41:43 +0900534 new_pinmux_type,
535 GPIO_CFG_REQ) != 0)
536 BUG();
537
Paul Mundtb3c185a2012-06-20 17:29:04 +0900538 pfc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
539 pfc->gpios[gpio].flags |= new_pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900540
541 ret = 0;
542 err_out:
543 return ret;
544}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900545EXPORT_SYMBOL_GPL(sh_pfc_set_direction);
Magnus Damm2967dab2008-10-08 20:41:43 +0900546
Paul Mundtb3c185a2012-06-20 17:29:04 +0900547int register_sh_pfc(struct sh_pfc *pfc)
Magnus Damm2967dab2008-10-08 20:41:43 +0900548{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900549 int (*initroutine)(struct sh_pfc *) = NULL;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900550 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900551
Paul Mundtb3c185a2012-06-20 17:29:04 +0900552 if (sh_pfc)
553 return -EBUSY;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900554
Paul Mundtb3c185a2012-06-20 17:29:04 +0900555 ret = pfc_ioremap(pfc);
556 if (unlikely(ret < 0))
Magnus Dammb0e10212011-12-09 12:14:27 +0900557 return ret;
558
Paul Mundtb3c185a2012-06-20 17:29:04 +0900559 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900560
Paul Mundtb3c185a2012-06-20 17:29:04 +0900561 setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900562
Paul Mundtb3c185a2012-06-20 17:29:04 +0900563 sh_pfc = pfc;
564 pr_info("%s support registered\n", pfc->name);
Magnus Damm69edbba2008-12-25 18:17:34 +0900565
Paul Mundtb3c185a2012-06-20 17:29:04 +0900566 initroutine = symbol_request(sh_pfc_register_gpiochip);
567 if (initroutine) {
568 (*initroutine)(pfc);
569 symbol_put_addr(initroutine);
570 }
Magnus Damm69edbba2008-12-25 18:17:34 +0900571
Paul Mundtb3c185a2012-06-20 17:29:04 +0900572 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900573}