blob: 02e9f62e2b28028cc2c46a07d0d23536c4cf8e85 [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>
Paul Mundtca5481c62012-07-10 12:08:14 +090022#include <linux/pinctrl/machine.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090023
Paul Mundtb3c185a2012-06-20 17:29:04 +090024static struct sh_pfc *sh_pfc __read_mostly;
25
26static inline bool sh_pfc_initialized(void)
27{
28 return !!sh_pfc;
29}
30
31static void pfc_iounmap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090032{
33 int k;
34
Paul Mundtb3c185a2012-06-20 17:29:04 +090035 for (k = 0; k < pfc->num_resources; k++)
36 if (pfc->window[k].virt)
37 iounmap(pfc->window[k].virt);
Magnus Dammb0e10212011-12-09 12:14:27 +090038
Paul Mundtb3c185a2012-06-20 17:29:04 +090039 kfree(pfc->window);
40 pfc->window = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090041}
42
Paul Mundtb3c185a2012-06-20 17:29:04 +090043static int pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090044{
45 struct resource *res;
46 int k;
47
Paul Mundtb3c185a2012-06-20 17:29:04 +090048 if (!pfc->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090049 return 0;
50
Paul Mundtb3c185a2012-06-20 17:29:04 +090051 pfc->window = kzalloc(pfc->num_resources * sizeof(*pfc->window),
Magnus Dammb0e10212011-12-09 12:14:27 +090052 GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090053 if (!pfc->window)
Magnus Dammb0e10212011-12-09 12:14:27 +090054 goto err1;
55
Paul Mundtb3c185a2012-06-20 17:29:04 +090056 for (k = 0; k < pfc->num_resources; k++) {
57 res = pfc->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090058 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090059 pfc->window[k].phys = res->start;
60 pfc->window[k].size = resource_size(res);
61 pfc->window[k].virt = ioremap_nocache(res->start,
Magnus Dammb0e10212011-12-09 12:14:27 +090062 resource_size(res));
Paul Mundtb3c185a2012-06-20 17:29:04 +090063 if (!pfc->window[k].virt)
Magnus Dammb0e10212011-12-09 12:14:27 +090064 goto err2;
65 }
66
67 return 0;
68
69err2:
Paul Mundtb3c185a2012-06-20 17:29:04 +090070 pfc_iounmap(pfc);
Magnus Dammb0e10212011-12-09 12:14:27 +090071err1:
72 return -1;
73}
74
Paul Mundtb3c185a2012-06-20 17:29:04 +090075static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090076 unsigned long address)
77{
78 struct pfc_window *window;
79 int k;
80
81 /* scan through physical windows and convert address */
Paul Mundtb3c185a2012-06-20 17:29:04 +090082 for (k = 0; k < pfc->num_resources; k++) {
83 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090084
85 if (address < window->phys)
86 continue;
87
88 if (address >= (window->phys + window->size))
89 continue;
90
91 return window->virt + (address - window->phys);
92 }
93
94 /* no windows defined, register must be 1:1 mapped virt:phys */
95 return (void __iomem *)address;
96}
Magnus Damm2967dab2008-10-08 20:41:43 +090097
Magnus Damm2967dab2008-10-08 20:41:43 +090098static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
99{
100 if (enum_id < r->begin)
101 return 0;
102
103 if (enum_id > r->end)
104 return 0;
105
106 return 1;
107}
108
Magnus Dammb0e10212011-12-09 12:14:27 +0900109static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900110 unsigned long reg_width)
111{
112 switch (reg_width) {
113 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900114 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900115 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900116 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900117 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900118 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900119 }
120
121 BUG();
122 return 0;
123}
124
Magnus Dammb0e10212011-12-09 12:14:27 +0900125static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900126 unsigned long reg_width,
127 unsigned long data)
128{
129 switch (reg_width) {
130 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900131 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900132 return;
133 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900134 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900135 return;
136 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900137 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900138 return;
139 }
140
141 BUG();
142}
143
Paul Mundtb3c185a2012-06-20 17:29:04 +0900144int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900145{
146 unsigned long pos;
147
148 pos = dr->reg_width - (in_pos + 1);
149
150 pr_debug("read_bit: addr = %lx, pos = %ld, "
151 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
152
153 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
154}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900155EXPORT_SYMBOL_GPL(sh_pfc_read_bit);
Magnus Damm92554d92011-12-14 01:00:37 +0900156
Paul Mundtb3c185a2012-06-20 17:29:04 +0900157void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
158 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900159{
160 unsigned long pos;
161
162 pos = dr->reg_width - (in_pos + 1);
163
Paul Mundtca6f2d72009-12-09 15:51:27 +0900164 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900165 "r_width = %ld\n",
166 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900167
168 if (value)
169 set_bit(pos, &dr->reg_shadow);
170 else
171 clear_bit(pos, &dr->reg_shadow);
172
Magnus Dammb0e10212011-12-09 12:14:27 +0900173 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900174}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900175EXPORT_SYMBOL_GPL(sh_pfc_write_bit);
Magnus Damm32920942008-12-25 18:17:26 +0900176
Paul Mundtb3c185a2012-06-20 17:29:04 +0900177static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900178 struct pinmux_cfg_reg *crp,
179 unsigned long in_pos,
180 void __iomem **mapped_regp,
181 unsigned long *maskp,
182 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900183{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900184 int k;
185
Paul Mundtb3c185a2012-06-20 17:29:04 +0900186 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900187
Magnus Dammf78a26f2011-12-14 01:01:05 +0900188 if (crp->field_width) {
189 *maskp = (1 << crp->field_width) - 1;
190 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
191 } else {
192 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
193 *posp = crp->reg_width;
194 for (k = 0; k <= in_pos; k++)
195 *posp -= crp->var_field_width[k];
196 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900197}
Magnus Damm2967dab2008-10-08 20:41:43 +0900198
Paul Mundtb3c185a2012-06-20 17:29:04 +0900199static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900200 struct pinmux_cfg_reg *crp,
201 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900202{
Magnus Damm18925e12011-12-14 01:00:55 +0900203 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900204 unsigned long mask, pos;
205
Paul Mundtb3c185a2012-06-20 17:29:04 +0900206 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900207
Magnus Damm18925e12011-12-14 01:00:55 +0900208 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900209 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900210 crp->reg, field, crp->reg_width, crp->field_width);
211
212 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
213}
214
Paul Mundtb3c185a2012-06-20 17:29:04 +0900215static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900216 struct pinmux_cfg_reg *crp,
217 unsigned long field, unsigned long value)
218{
219 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900220 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900221
Paul Mundtb3c185a2012-06-20 17:29:04 +0900222 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900223
224 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
225 "r_width = %ld, f_width = %ld\n",
226 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900227
228 mask = ~(mask << pos);
229 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900230
Magnus Damme499ada2011-12-14 01:01:14 +0900231 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
232 data &= mask;
233 data |= value;
234
Paul Mundtb3c185a2012-06-20 17:29:04 +0900235 if (pfc->unlock_reg)
236 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900237 32, ~data);
238
239 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900240}
241
Paul Mundtb3c185a2012-06-20 17:29:04 +0900242static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900243{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900244 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900245 struct pinmux_data_reg *data_reg;
246 int k, n;
247
Paul Mundtb3c185a2012-06-20 17:29:04 +0900248 if (!enum_in_range(gpiop->enum_id, &pfc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900249 return -1;
250
251 k = 0;
252 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900253 data_reg = pfc->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900254
255 if (!data_reg->reg_width)
256 break;
257
Paul Mundtb3c185a2012-06-20 17:29:04 +0900258 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900259
Magnus Damm2967dab2008-10-08 20:41:43 +0900260 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900261 if (data_reg->enum_ids[n] == gpiop->enum_id) {
262 gpiop->flags &= ~PINMUX_FLAG_DREG;
263 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
264 gpiop->flags &= ~PINMUX_FLAG_DBIT;
265 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900266 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900267 }
268 }
269 k++;
270 }
271
Magnus Damm18801be2008-12-25 18:17:09 +0900272 BUG();
273
Magnus Damm2967dab2008-10-08 20:41:43 +0900274 return -1;
275}
276
Paul Mundtb3c185a2012-06-20 17:29:04 +0900277static void setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900278{
279 struct pinmux_data_reg *drp;
280 int k;
281
Paul Mundtb3c185a2012-06-20 17:29:04 +0900282 for (k = pfc->first_gpio; k <= pfc->last_gpio; k++)
283 setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900284
285 k = 0;
286 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900287 drp = pfc->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900288
289 if (!drp->reg_width)
290 break;
291
Magnus Dammb0e10212011-12-09 12:14:27 +0900292 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
293 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900294 k++;
295 }
296}
297
Paul Mundtb3c185a2012-06-20 17:29:04 +0900298int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900299 struct pinmux_data_reg **drp, int *bitp)
300{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900301 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900302 int k, n;
303
Paul Mundtb3c185a2012-06-20 17:29:04 +0900304 if (!enum_in_range(gpiop->enum_id, &pfc->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900305 return -1;
306
307 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
308 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900309 *drp = pfc->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900310 *bitp = n;
311 return 0;
312}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900313EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg);
Magnus Damm18801be2008-12-25 18:17:09 +0900314
Paul Mundtb3c185a2012-06-20 17:29:04 +0900315static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900316 struct pinmux_cfg_reg **crp,
317 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900318 unsigned long **cntp)
319{
320 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900321 unsigned long r_width, f_width, curr_width, ncomb;
322 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900323
324 k = 0;
325 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900326 config_reg = pfc->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900327
328 r_width = config_reg->reg_width;
329 f_width = config_reg->field_width;
330
331 if (!r_width)
332 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900333
334 pos = 0;
335 m = 0;
336 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
337 if (f_width)
338 curr_width = f_width;
339 else
340 curr_width = config_reg->var_field_width[m];
341
342 ncomb = 1 << curr_width;
343 for (n = 0; n < ncomb; n++) {
344 if (config_reg->enum_ids[pos + n] == enum_id) {
345 *crp = config_reg;
346 *fieldp = m;
347 *valuep = n;
348 *cntp = &config_reg->cnt[m];
349 return 0;
350 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900351 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900352 pos += ncomb;
353 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900354 }
355 k++;
356 }
357
358 return -1;
359}
360
Paul Mundtb3c185a2012-06-20 17:29:04 +0900361int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
362 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900363{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900364 pinmux_enum_t enum_id = pfc->gpios[gpio].enum_id;
365 pinmux_enum_t *data = pfc->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900366 int k;
367
Paul Mundtb3c185a2012-06-20 17:29:04 +0900368 if (!enum_in_range(enum_id, &pfc->data)) {
369 if (!enum_in_range(enum_id, &pfc->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900370 pr_err("non data/mark enum_id for gpio %d\n", gpio);
371 return -1;
372 }
373 }
374
375 if (pos) {
376 *enum_idp = data[pos + 1];
377 return pos + 1;
378 }
379
Paul Mundtb3c185a2012-06-20 17:29:04 +0900380 for (k = 0; k < pfc->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900381 if (data[k] == enum_id) {
382 *enum_idp = data[k + 1];
383 return k + 1;
384 }
385 }
386
387 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
388 return -1;
389}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900390EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum);
Magnus Damm2967dab2008-10-08 20:41:43 +0900391
Paul Mundtb3c185a2012-06-20 17:29:04 +0900392int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
393 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900394{
395 struct pinmux_cfg_reg *cr = NULL;
396 pinmux_enum_t enum_id;
397 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900398 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900399 unsigned long *cntp;
400
401 switch (pinmux_type) {
402
403 case PINMUX_TYPE_FUNCTION:
404 range = NULL;
405 break;
406
407 case PINMUX_TYPE_OUTPUT:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900408 range = &pfc->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900409 break;
410
411 case PINMUX_TYPE_INPUT:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900412 range = &pfc->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900413 break;
414
415 case PINMUX_TYPE_INPUT_PULLUP:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900416 range = &pfc->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900417 break;
418
419 case PINMUX_TYPE_INPUT_PULLDOWN:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900420 range = &pfc->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900421 break;
422
423 default:
424 goto out_err;
425 }
426
427 pos = 0;
428 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900429 field = 0;
430 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900431 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900432 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900433 if (pos <= 0)
434 goto out_err;
435
436 if (!enum_id)
437 break;
438
Magnus Damm50dd3142010-01-19 13:52:28 +0000439 /* first check if this is a function enum */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900440 in_range = enum_in_range(enum_id, &pfc->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000441 if (!in_range) {
442 /* not a function enum */
443 if (range) {
444 /*
445 * other range exists, so this pin is
446 * a regular GPIO pin that now is being
447 * bound to a specific direction.
448 *
449 * for this case we only allow function enums
450 * and the enums that match the other range.
451 */
452 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900453
Magnus Damm50dd3142010-01-19 13:52:28 +0000454 /*
455 * special case pass through for fixed
456 * input-only or output-only pins without
457 * function enum register association.
458 */
459 if (in_range && enum_id == range->force)
460 continue;
461 } else {
462 /*
463 * no other range exists, so this pin
464 * must then be of the function type.
465 *
466 * allow function type pins to select
467 * any combination of function/in/out
468 * in their MARK lists.
469 */
470 in_range = 1;
471 }
Magnus Damm42eed422008-10-22 18:29:17 +0900472 }
473
Magnus Damm2967dab2008-10-08 20:41:43 +0900474 if (!in_range)
475 continue;
476
Paul Mundtb3c185a2012-06-20 17:29:04 +0900477 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900478 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900479 goto out_err;
480
481 switch (cfg_mode) {
482 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900483 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900484 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900485 continue;
486 break;
487
488 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900489 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900490 *cntp = *cntp + 1;
491 break;
492
493 case GPIO_CFG_FREE:
494 *cntp = *cntp - 1;
495 break;
496 }
497 }
498
499 return 0;
500 out_err:
501 return -1;
502}
Paul Mundtb3c185a2012-06-20 17:29:04 +0900503EXPORT_SYMBOL_GPL(sh_pfc_config_gpio);
Magnus Damm2967dab2008-10-08 20:41:43 +0900504
Paul Mundtb3c185a2012-06-20 17:29:04 +0900505int register_sh_pfc(struct sh_pfc *pfc)
Magnus Damm2967dab2008-10-08 20:41:43 +0900506{
Paul Mundtb3c185a2012-06-20 17:29:04 +0900507 int (*initroutine)(struct sh_pfc *) = NULL;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900508 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900509
Paul Mundt06d56312012-06-21 00:03:41 +0900510 /*
511 * Ensure that the type encoding fits
512 */
513 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
514
Paul Mundtb3c185a2012-06-20 17:29:04 +0900515 if (sh_pfc)
516 return -EBUSY;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900517
Paul Mundtb3c185a2012-06-20 17:29:04 +0900518 ret = pfc_ioremap(pfc);
519 if (unlikely(ret < 0))
Magnus Dammb0e10212011-12-09 12:14:27 +0900520 return ret;
521
Paul Mundtb3c185a2012-06-20 17:29:04 +0900522 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900523
Paul Mundtca5481c62012-07-10 12:08:14 +0900524 pinctrl_provide_dummies();
Paul Mundtb3c185a2012-06-20 17:29:04 +0900525 setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900526
Paul Mundtb3c185a2012-06-20 17:29:04 +0900527 sh_pfc = pfc;
Magnus Damm69edbba2008-12-25 18:17:34 +0900528
Paul Mundtca5481c62012-07-10 12:08:14 +0900529 /*
530 * Initialize pinctrl bindings first
531 */
532 initroutine = symbol_request(sh_pfc_register_pinctrl);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900533 if (initroutine) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900534 ret = (*initroutine)(pfc);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900535 symbol_put_addr(initroutine);
Paul Mundtca5481c62012-07-10 12:08:14 +0900536
537 if (unlikely(ret != 0))
538 goto err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900539 }
Magnus Damm69edbba2008-12-25 18:17:34 +0900540
Paul Mundtca5481c62012-07-10 12:08:14 +0900541 /*
542 * Then the GPIO chip
543 */
544 initroutine = symbol_request(sh_pfc_register_gpiochip);
545 if (initroutine) {
546 ret = (*initroutine)(pfc);
547 symbol_put_addr(initroutine);
548
549 /*
550 * If the GPIO chip fails to come up we still leave the
551 * PFC state as it is, given that there are already
552 * extant users of it that have succeeded by this point.
553 */
554 if (unlikely(ret != 0)) {
555 pr_notice("failed to init GPIO chip, ignoring...\n");
556 ret = 0;
557 }
558 }
559
560 pr_info("%s support registered\n", pfc->name);
561
Paul Mundtb3c185a2012-06-20 17:29:04 +0900562 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900563
564err:
565 pfc_iounmap(pfc);
566 sh_pfc = NULL;
567
568 return ret;
Paul Mundtb72421d2010-10-04 03:54:56 +0900569}