blob: 541099613a21d92352dc484f1a5fc460f5706143 [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 */
Laurent Pinchartf9492fd2012-12-15 23:50:45 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Paul Mundtb72421d2010-10-04 03:54:56 +090012
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
Laurent Pinchartf9165132012-12-15 23:50:44 +010024#include "core.h"
25
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010026static struct sh_pfc sh_pfc __read_mostly;
Paul Mundtb3c185a2012-06-20 17:29:04 +090027
28static void pfc_iounmap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090029{
30 int k;
31
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010032 for (k = 0; k < pfc->pdata->num_resources; k++)
Paul Mundtb3c185a2012-06-20 17:29:04 +090033 if (pfc->window[k].virt)
34 iounmap(pfc->window[k].virt);
Magnus Dammb0e10212011-12-09 12:14:27 +090035
Paul Mundtb3c185a2012-06-20 17:29:04 +090036 kfree(pfc->window);
37 pfc->window = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090038}
39
Paul Mundtb3c185a2012-06-20 17:29:04 +090040static int pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090041{
42 struct resource *res;
43 int k;
44
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010045 if (!pfc->pdata->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090046 return 0;
47
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010048 pfc->window = kzalloc(pfc->pdata->num_resources * sizeof(*pfc->window),
Magnus Dammb0e10212011-12-09 12:14:27 +090049 GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090050 if (!pfc->window)
Magnus Dammb0e10212011-12-09 12:14:27 +090051 goto err1;
52
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010053 for (k = 0; k < pfc->pdata->num_resources; k++) {
54 res = pfc->pdata->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090055 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090056 pfc->window[k].phys = res->start;
57 pfc->window[k].size = resource_size(res);
58 pfc->window[k].virt = ioremap_nocache(res->start,
Magnus Dammb0e10212011-12-09 12:14:27 +090059 resource_size(res));
Paul Mundtb3c185a2012-06-20 17:29:04 +090060 if (!pfc->window[k].virt)
Magnus Dammb0e10212011-12-09 12:14:27 +090061 goto err2;
62 }
63
64 return 0;
65
66err2:
Paul Mundtb3c185a2012-06-20 17:29:04 +090067 pfc_iounmap(pfc);
Magnus Dammb0e10212011-12-09 12:14:27 +090068err1:
69 return -1;
70}
71
Paul Mundtb3c185a2012-06-20 17:29:04 +090072static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090073 unsigned long address)
74{
75 struct pfc_window *window;
76 int k;
77
78 /* scan through physical windows and convert address */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010079 for (k = 0; k < pfc->pdata->num_resources; k++) {
Paul Mundtb3c185a2012-06-20 17:29:04 +090080 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090081
82 if (address < window->phys)
83 continue;
84
85 if (address >= (window->phys + window->size))
86 continue;
87
88 return window->virt + (address - window->phys);
89 }
90
91 /* no windows defined, register must be 1:1 mapped virt:phys */
92 return (void __iomem *)address;
93}
Magnus Damm2967dab2008-10-08 20:41:43 +090094
Magnus Damm2967dab2008-10-08 20:41:43 +090095static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
96{
97 if (enum_id < r->begin)
98 return 0;
99
100 if (enum_id > r->end)
101 return 0;
102
103 return 1;
104}
105
Magnus Dammb0e10212011-12-09 12:14:27 +0900106static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900107 unsigned long reg_width)
108{
109 switch (reg_width) {
110 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900111 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900112 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900113 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900114 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900115 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900116 }
117
118 BUG();
119 return 0;
120}
121
Magnus Dammb0e10212011-12-09 12:14:27 +0900122static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900123 unsigned long reg_width,
124 unsigned long data)
125{
126 switch (reg_width) {
127 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900128 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900129 return;
130 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900131 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900132 return;
133 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900134 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900135 return;
136 }
137
138 BUG();
139}
140
Paul Mundtb3c185a2012-06-20 17:29:04 +0900141int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900142{
143 unsigned long pos;
144
145 pos = dr->reg_width - (in_pos + 1);
146
147 pr_debug("read_bit: addr = %lx, pos = %ld, "
148 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
149
150 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
151}
152
Paul Mundtb3c185a2012-06-20 17:29:04 +0900153void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
154 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900155{
156 unsigned long pos;
157
158 pos = dr->reg_width - (in_pos + 1);
159
Paul Mundtca6f2d72009-12-09 15:51:27 +0900160 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900161 "r_width = %ld\n",
162 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900163
164 if (value)
165 set_bit(pos, &dr->reg_shadow);
166 else
167 clear_bit(pos, &dr->reg_shadow);
168
Magnus Dammb0e10212011-12-09 12:14:27 +0900169 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900170}
171
Paul Mundtb3c185a2012-06-20 17:29:04 +0900172static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900173 struct pinmux_cfg_reg *crp,
174 unsigned long in_pos,
175 void __iomem **mapped_regp,
176 unsigned long *maskp,
177 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900178{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900179 int k;
180
Paul Mundtb3c185a2012-06-20 17:29:04 +0900181 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900182
Magnus Dammf78a26f2011-12-14 01:01:05 +0900183 if (crp->field_width) {
184 *maskp = (1 << crp->field_width) - 1;
185 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
186 } else {
187 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
188 *posp = crp->reg_width;
189 for (k = 0; k <= in_pos; k++)
190 *posp -= crp->var_field_width[k];
191 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900192}
Magnus Damm2967dab2008-10-08 20:41:43 +0900193
Paul Mundtb3c185a2012-06-20 17:29:04 +0900194static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900195 struct pinmux_cfg_reg *crp,
196 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900197{
Magnus Damm18925e12011-12-14 01:00:55 +0900198 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900199 unsigned long mask, pos;
200
Paul Mundtb3c185a2012-06-20 17:29:04 +0900201 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900202
Magnus Damm18925e12011-12-14 01:00:55 +0900203 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900204 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900205 crp->reg, field, crp->reg_width, crp->field_width);
206
207 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
208}
209
Paul Mundtb3c185a2012-06-20 17:29:04 +0900210static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900211 struct pinmux_cfg_reg *crp,
212 unsigned long field, unsigned long value)
213{
214 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900215 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900216
Paul Mundtb3c185a2012-06-20 17:29:04 +0900217 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900218
219 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
220 "r_width = %ld, f_width = %ld\n",
221 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900222
223 mask = ~(mask << pos);
224 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900225
Magnus Damme499ada2011-12-14 01:01:14 +0900226 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
227 data &= mask;
228 data |= value;
229
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100230 if (pfc->pdata->unlock_reg)
231 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->pdata->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900232 32, ~data);
233
234 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900235}
236
Paul Mundtb3c185a2012-06-20 17:29:04 +0900237static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900238{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100239 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900240 struct pinmux_data_reg *data_reg;
241 int k, n;
242
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100243 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900244 return -1;
245
246 k = 0;
247 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100248 data_reg = pfc->pdata->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900249
250 if (!data_reg->reg_width)
251 break;
252
Paul Mundtb3c185a2012-06-20 17:29:04 +0900253 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900254
Magnus Damm2967dab2008-10-08 20:41:43 +0900255 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900256 if (data_reg->enum_ids[n] == gpiop->enum_id) {
257 gpiop->flags &= ~PINMUX_FLAG_DREG;
258 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
259 gpiop->flags &= ~PINMUX_FLAG_DBIT;
260 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900261 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900262 }
263 }
264 k++;
265 }
266
Magnus Damm18801be2008-12-25 18:17:09 +0900267 BUG();
268
Magnus Damm2967dab2008-10-08 20:41:43 +0900269 return -1;
270}
271
Paul Mundtb3c185a2012-06-20 17:29:04 +0900272static void setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900273{
274 struct pinmux_data_reg *drp;
275 int k;
276
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100277 for (k = pfc->pdata->first_gpio; k <= pfc->pdata->last_gpio; k++)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900278 setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900279
280 k = 0;
281 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100282 drp = pfc->pdata->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900283
284 if (!drp->reg_width)
285 break;
286
Magnus Dammb0e10212011-12-09 12:14:27 +0900287 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
288 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900289 k++;
290 }
291}
292
Paul Mundtb3c185a2012-06-20 17:29:04 +0900293int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900294 struct pinmux_data_reg **drp, int *bitp)
295{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100296 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900297 int k, n;
298
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100299 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900300 return -1;
301
302 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
303 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100304 *drp = pfc->pdata->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900305 *bitp = n;
306 return 0;
307}
308
Paul Mundtb3c185a2012-06-20 17:29:04 +0900309static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900310 struct pinmux_cfg_reg **crp,
311 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900312 unsigned long **cntp)
313{
314 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900315 unsigned long r_width, f_width, curr_width, ncomb;
316 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900317
318 k = 0;
319 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100320 config_reg = pfc->pdata->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900321
322 r_width = config_reg->reg_width;
323 f_width = config_reg->field_width;
324
325 if (!r_width)
326 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900327
328 pos = 0;
329 m = 0;
330 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
331 if (f_width)
332 curr_width = f_width;
333 else
334 curr_width = config_reg->var_field_width[m];
335
336 ncomb = 1 << curr_width;
337 for (n = 0; n < ncomb; n++) {
338 if (config_reg->enum_ids[pos + n] == enum_id) {
339 *crp = config_reg;
340 *fieldp = m;
341 *valuep = n;
342 *cntp = &config_reg->cnt[m];
343 return 0;
344 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900345 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900346 pos += ncomb;
347 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900348 }
349 k++;
350 }
351
352 return -1;
353}
354
Paul Mundtb3c185a2012-06-20 17:29:04 +0900355int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
356 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900357{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100358 pinmux_enum_t enum_id = pfc->pdata->gpios[gpio].enum_id;
359 pinmux_enum_t *data = pfc->pdata->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900360 int k;
361
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100362 if (!enum_in_range(enum_id, &pfc->pdata->data)) {
363 if (!enum_in_range(enum_id, &pfc->pdata->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900364 pr_err("non data/mark enum_id for gpio %d\n", gpio);
365 return -1;
366 }
367 }
368
369 if (pos) {
370 *enum_idp = data[pos + 1];
371 return pos + 1;
372 }
373
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100374 for (k = 0; k < pfc->pdata->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900375 if (data[k] == enum_id) {
376 *enum_idp = data[k + 1];
377 return k + 1;
378 }
379 }
380
381 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
382 return -1;
383}
384
Paul Mundtb3c185a2012-06-20 17:29:04 +0900385int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
386 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900387{
388 struct pinmux_cfg_reg *cr = NULL;
389 pinmux_enum_t enum_id;
390 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900391 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900392 unsigned long *cntp;
393
394 switch (pinmux_type) {
395
396 case PINMUX_TYPE_FUNCTION:
397 range = NULL;
398 break;
399
400 case PINMUX_TYPE_OUTPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100401 range = &pfc->pdata->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900402 break;
403
404 case PINMUX_TYPE_INPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100405 range = &pfc->pdata->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900406 break;
407
408 case PINMUX_TYPE_INPUT_PULLUP:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100409 range = &pfc->pdata->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900410 break;
411
412 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100413 range = &pfc->pdata->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900414 break;
415
416 default:
417 goto out_err;
418 }
419
420 pos = 0;
421 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900422 field = 0;
423 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900424 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900425 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900426 if (pos <= 0)
427 goto out_err;
428
429 if (!enum_id)
430 break;
431
Magnus Damm50dd3142010-01-19 13:52:28 +0000432 /* first check if this is a function enum */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100433 in_range = enum_in_range(enum_id, &pfc->pdata->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000434 if (!in_range) {
435 /* not a function enum */
436 if (range) {
437 /*
438 * other range exists, so this pin is
439 * a regular GPIO pin that now is being
440 * bound to a specific direction.
441 *
442 * for this case we only allow function enums
443 * and the enums that match the other range.
444 */
445 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900446
Magnus Damm50dd3142010-01-19 13:52:28 +0000447 /*
448 * special case pass through for fixed
449 * input-only or output-only pins without
450 * function enum register association.
451 */
452 if (in_range && enum_id == range->force)
453 continue;
454 } else {
455 /*
456 * no other range exists, so this pin
457 * must then be of the function type.
458 *
459 * allow function type pins to select
460 * any combination of function/in/out
461 * in their MARK lists.
462 */
463 in_range = 1;
464 }
Magnus Damm42eed422008-10-22 18:29:17 +0900465 }
466
Magnus Damm2967dab2008-10-08 20:41:43 +0900467 if (!in_range)
468 continue;
469
Paul Mundtb3c185a2012-06-20 17:29:04 +0900470 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900471 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900472 goto out_err;
473
474 switch (cfg_mode) {
475 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900476 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900477 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900478 continue;
479 break;
480
481 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900482 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900483 *cntp = *cntp + 1;
484 break;
485
486 case GPIO_CFG_FREE:
487 *cntp = *cntp - 1;
488 break;
489 }
490 }
491
492 return 0;
493 out_err:
494 return -1;
495}
496
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100497int register_sh_pfc(struct sh_pfc_platform_data *pdata)
Magnus Damm2967dab2008-10-08 20:41:43 +0900498{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900499 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900500
Paul Mundt06d56312012-06-21 00:03:41 +0900501 /*
502 * Ensure that the type encoding fits
503 */
504 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
505
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100506 if (sh_pfc.pdata)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900507 return -EBUSY;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900508
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100509 sh_pfc.pdata = pdata;
Magnus Dammb0e10212011-12-09 12:14:27 +0900510
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100511 ret = pfc_ioremap(&sh_pfc);
512 if (unlikely(ret < 0)) {
513 sh_pfc.pdata = NULL;
514 return ret;
515 }
516
517 spin_lock_init(&sh_pfc.lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900518
Paul Mundtca5481c62012-07-10 12:08:14 +0900519 pinctrl_provide_dummies();
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100520 setup_data_regs(&sh_pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900521
Paul Mundtca5481c62012-07-10 12:08:14 +0900522 /*
523 * Initialize pinctrl bindings first
524 */
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100525 ret = sh_pfc_register_pinctrl(&sh_pfc);
526 if (unlikely(ret != 0))
Paul Mundt159ac072012-07-17 15:18:37 +0900527 goto err;
Magnus Damm69edbba2008-12-25 18:17:34 +0900528
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100529#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900530 /*
531 * Then the GPIO chip
532 */
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100533 ret = sh_pfc_register_gpiochip(&sh_pfc);
534 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900535 /*
536 * If the GPIO chip fails to come up we still leave the
537 * PFC state as it is, given that there are already
538 * extant users of it that have succeeded by this point.
539 */
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100540 pr_notice("failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900541 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100542#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900543
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100544 pr_info("%s support registered\n", sh_pfc.pdata->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900545
Paul Mundtb3c185a2012-06-20 17:29:04 +0900546 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900547
548err:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100549 pfc_iounmap(&sh_pfc);
550 sh_pfc.pdata = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900551
552 return ret;
Paul Mundtb72421d2010-10-04 03:54:56 +0900553}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100554
555MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
556MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
557MODULE_LICENSE("GPL v2");