blob: 6d162e694e68b570c1758ce63ec999053b5c399d [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 Pinchartc6193ea2012-12-15 23:50:47 +010011
12#define DRV_NAME "sh-pfc"
Laurent Pinchartf9492fd2012-12-15 23:50:45 +010013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Paul Mundtb72421d2010-10-04 03:54:56 +090014
Magnus Damm2967dab2008-10-08 20:41:43 +090015#include <linux/errno.h>
16#include <linux/kernel.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090017#include <linux/sh_pfc.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090018#include <linux/module.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090019#include <linux/err.h>
20#include <linux/io.h>
Magnus Damm2967dab2008-10-08 20:41:43 +090021#include <linux/bitops.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090022#include <linux/slab.h>
23#include <linux/ioport.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090024#include <linux/pinctrl/machine.h>
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010025#include <linux/platform_device.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090026
Laurent Pinchartf9165132012-12-15 23:50:44 +010027#include "core.h"
28
Paul Mundtb3c185a2012-06-20 17:29:04 +090029static void pfc_iounmap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090030{
31 int k;
32
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010033 for (k = 0; k < pfc->pdata->num_resources; k++)
Paul Mundtb3c185a2012-06-20 17:29:04 +090034 if (pfc->window[k].virt)
35 iounmap(pfc->window[k].virt);
Magnus Dammb0e10212011-12-09 12:14:27 +090036
Paul Mundtb3c185a2012-06-20 17:29:04 +090037 kfree(pfc->window);
38 pfc->window = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090039}
40
Paul Mundtb3c185a2012-06-20 17:29:04 +090041static int pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090042{
43 struct resource *res;
44 int k;
45
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010046 if (!pfc->pdata->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090047 return 0;
48
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010049 pfc->window = kzalloc(pfc->pdata->num_resources * sizeof(*pfc->window),
Magnus Dammb0e10212011-12-09 12:14:27 +090050 GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090051 if (!pfc->window)
Magnus Dammb0e10212011-12-09 12:14:27 +090052 goto err1;
53
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010054 for (k = 0; k < pfc->pdata->num_resources; k++) {
55 res = pfc->pdata->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090056 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090057 pfc->window[k].phys = res->start;
58 pfc->window[k].size = resource_size(res);
59 pfc->window[k].virt = ioremap_nocache(res->start,
Magnus Dammb0e10212011-12-09 12:14:27 +090060 resource_size(res));
Paul Mundtb3c185a2012-06-20 17:29:04 +090061 if (!pfc->window[k].virt)
Magnus Dammb0e10212011-12-09 12:14:27 +090062 goto err2;
63 }
64
65 return 0;
66
67err2:
Paul Mundtb3c185a2012-06-20 17:29:04 +090068 pfc_iounmap(pfc);
Magnus Dammb0e10212011-12-09 12:14:27 +090069err1:
70 return -1;
71}
72
Paul Mundtb3c185a2012-06-20 17:29:04 +090073static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090074 unsigned long address)
75{
76 struct pfc_window *window;
77 int k;
78
79 /* scan through physical windows and convert address */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010080 for (k = 0; k < pfc->pdata->num_resources; k++) {
Paul Mundtb3c185a2012-06-20 17:29:04 +090081 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090082
83 if (address < window->phys)
84 continue;
85
86 if (address >= (window->phys + window->size))
87 continue;
88
89 return window->virt + (address - window->phys);
90 }
91
92 /* no windows defined, register must be 1:1 mapped virt:phys */
93 return (void __iomem *)address;
94}
Magnus Damm2967dab2008-10-08 20:41:43 +090095
Magnus Damm2967dab2008-10-08 20:41:43 +090096static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
97{
98 if (enum_id < r->begin)
99 return 0;
100
101 if (enum_id > r->end)
102 return 0;
103
104 return 1;
105}
106
Magnus Dammb0e10212011-12-09 12:14:27 +0900107static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900108 unsigned long reg_width)
109{
110 switch (reg_width) {
111 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900112 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900113 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900114 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900115 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900116 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900117 }
118
119 BUG();
120 return 0;
121}
122
Magnus Dammb0e10212011-12-09 12:14:27 +0900123static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900124 unsigned long reg_width,
125 unsigned long data)
126{
127 switch (reg_width) {
128 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900129 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900130 return;
131 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900132 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900133 return;
134 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900135 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900136 return;
137 }
138
139 BUG();
140}
141
Paul Mundtb3c185a2012-06-20 17:29:04 +0900142int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900143{
144 unsigned long pos;
145
146 pos = dr->reg_width - (in_pos + 1);
147
148 pr_debug("read_bit: addr = %lx, pos = %ld, "
149 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
150
151 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
152}
153
Paul Mundtb3c185a2012-06-20 17:29:04 +0900154void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
155 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900156{
157 unsigned long pos;
158
159 pos = dr->reg_width - (in_pos + 1);
160
Paul Mundtca6f2d72009-12-09 15:51:27 +0900161 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900162 "r_width = %ld\n",
163 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900164
165 if (value)
166 set_bit(pos, &dr->reg_shadow);
167 else
168 clear_bit(pos, &dr->reg_shadow);
169
Magnus Dammb0e10212011-12-09 12:14:27 +0900170 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900171}
172
Paul Mundtb3c185a2012-06-20 17:29:04 +0900173static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900174 struct pinmux_cfg_reg *crp,
175 unsigned long in_pos,
176 void __iomem **mapped_regp,
177 unsigned long *maskp,
178 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900179{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900180 int k;
181
Paul Mundtb3c185a2012-06-20 17:29:04 +0900182 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900183
Magnus Dammf78a26f2011-12-14 01:01:05 +0900184 if (crp->field_width) {
185 *maskp = (1 << crp->field_width) - 1;
186 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
187 } else {
188 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
189 *posp = crp->reg_width;
190 for (k = 0; k <= in_pos; k++)
191 *posp -= crp->var_field_width[k];
192 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900193}
Magnus Damm2967dab2008-10-08 20:41:43 +0900194
Paul Mundtb3c185a2012-06-20 17:29:04 +0900195static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900196 struct pinmux_cfg_reg *crp,
197 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900198{
Magnus Damm18925e12011-12-14 01:00:55 +0900199 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900200 unsigned long mask, pos;
201
Paul Mundtb3c185a2012-06-20 17:29:04 +0900202 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900203
Magnus Damm18925e12011-12-14 01:00:55 +0900204 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900205 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900206 crp->reg, field, crp->reg_width, crp->field_width);
207
208 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
209}
210
Paul Mundtb3c185a2012-06-20 17:29:04 +0900211static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900212 struct pinmux_cfg_reg *crp,
213 unsigned long field, unsigned long value)
214{
215 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900216 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900217
Paul Mundtb3c185a2012-06-20 17:29:04 +0900218 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900219
220 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
221 "r_width = %ld, f_width = %ld\n",
222 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900223
224 mask = ~(mask << pos);
225 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900226
Magnus Damme499ada2011-12-14 01:01:14 +0900227 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
228 data &= mask;
229 data |= value;
230
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100231 if (pfc->pdata->unlock_reg)
232 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->pdata->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900233 32, ~data);
234
235 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900236}
237
Paul Mundtb3c185a2012-06-20 17:29:04 +0900238static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900239{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100240 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900241 struct pinmux_data_reg *data_reg;
242 int k, n;
243
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100244 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900245 return -1;
246
247 k = 0;
248 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100249 data_reg = pfc->pdata->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900250
251 if (!data_reg->reg_width)
252 break;
253
Paul Mundtb3c185a2012-06-20 17:29:04 +0900254 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900255
Magnus Damm2967dab2008-10-08 20:41:43 +0900256 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900257 if (data_reg->enum_ids[n] == gpiop->enum_id) {
258 gpiop->flags &= ~PINMUX_FLAG_DREG;
259 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
260 gpiop->flags &= ~PINMUX_FLAG_DBIT;
261 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900262 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900263 }
264 }
265 k++;
266 }
267
Magnus Damm18801be2008-12-25 18:17:09 +0900268 BUG();
269
Magnus Damm2967dab2008-10-08 20:41:43 +0900270 return -1;
271}
272
Paul Mundtb3c185a2012-06-20 17:29:04 +0900273static void setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900274{
275 struct pinmux_data_reg *drp;
276 int k;
277
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100278 for (k = pfc->pdata->first_gpio; k <= pfc->pdata->last_gpio; k++)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900279 setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900280
281 k = 0;
282 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100283 drp = pfc->pdata->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900284
285 if (!drp->reg_width)
286 break;
287
Magnus Dammb0e10212011-12-09 12:14:27 +0900288 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
289 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900290 k++;
291 }
292}
293
Paul Mundtb3c185a2012-06-20 17:29:04 +0900294int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900295 struct pinmux_data_reg **drp, int *bitp)
296{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100297 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900298 int k, n;
299
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100300 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900301 return -1;
302
303 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
304 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100305 *drp = pfc->pdata->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900306 *bitp = n;
307 return 0;
308}
309
Paul Mundtb3c185a2012-06-20 17:29:04 +0900310static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900311 struct pinmux_cfg_reg **crp,
312 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900313 unsigned long **cntp)
314{
315 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900316 unsigned long r_width, f_width, curr_width, ncomb;
317 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900318
319 k = 0;
320 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100321 config_reg = pfc->pdata->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900322
323 r_width = config_reg->reg_width;
324 f_width = config_reg->field_width;
325
326 if (!r_width)
327 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900328
329 pos = 0;
330 m = 0;
331 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
332 if (f_width)
333 curr_width = f_width;
334 else
335 curr_width = config_reg->var_field_width[m];
336
337 ncomb = 1 << curr_width;
338 for (n = 0; n < ncomb; n++) {
339 if (config_reg->enum_ids[pos + n] == enum_id) {
340 *crp = config_reg;
341 *fieldp = m;
342 *valuep = n;
343 *cntp = &config_reg->cnt[m];
344 return 0;
345 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900346 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900347 pos += ncomb;
348 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900349 }
350 k++;
351 }
352
353 return -1;
354}
355
Paul Mundtb3c185a2012-06-20 17:29:04 +0900356int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
357 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900358{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100359 pinmux_enum_t enum_id = pfc->pdata->gpios[gpio].enum_id;
360 pinmux_enum_t *data = pfc->pdata->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900361 int k;
362
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100363 if (!enum_in_range(enum_id, &pfc->pdata->data)) {
364 if (!enum_in_range(enum_id, &pfc->pdata->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900365 pr_err("non data/mark enum_id for gpio %d\n", gpio);
366 return -1;
367 }
368 }
369
370 if (pos) {
371 *enum_idp = data[pos + 1];
372 return pos + 1;
373 }
374
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100375 for (k = 0; k < pfc->pdata->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900376 if (data[k] == enum_id) {
377 *enum_idp = data[k + 1];
378 return k + 1;
379 }
380 }
381
382 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
383 return -1;
384}
385
Paul Mundtb3c185a2012-06-20 17:29:04 +0900386int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
387 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900388{
389 struct pinmux_cfg_reg *cr = NULL;
390 pinmux_enum_t enum_id;
391 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900392 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900393 unsigned long *cntp;
394
395 switch (pinmux_type) {
396
397 case PINMUX_TYPE_FUNCTION:
398 range = NULL;
399 break;
400
401 case PINMUX_TYPE_OUTPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100402 range = &pfc->pdata->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900403 break;
404
405 case PINMUX_TYPE_INPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100406 range = &pfc->pdata->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900407 break;
408
409 case PINMUX_TYPE_INPUT_PULLUP:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100410 range = &pfc->pdata->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900411 break;
412
413 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100414 range = &pfc->pdata->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900415 break;
416
417 default:
418 goto out_err;
419 }
420
421 pos = 0;
422 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900423 field = 0;
424 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900425 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900426 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900427 if (pos <= 0)
428 goto out_err;
429
430 if (!enum_id)
431 break;
432
Magnus Damm50dd3142010-01-19 13:52:28 +0000433 /* first check if this is a function enum */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100434 in_range = enum_in_range(enum_id, &pfc->pdata->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000435 if (!in_range) {
436 /* not a function enum */
437 if (range) {
438 /*
439 * other range exists, so this pin is
440 * a regular GPIO pin that now is being
441 * bound to a specific direction.
442 *
443 * for this case we only allow function enums
444 * and the enums that match the other range.
445 */
446 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900447
Magnus Damm50dd3142010-01-19 13:52:28 +0000448 /*
449 * special case pass through for fixed
450 * input-only or output-only pins without
451 * function enum register association.
452 */
453 if (in_range && enum_id == range->force)
454 continue;
455 } else {
456 /*
457 * no other range exists, so this pin
458 * must then be of the function type.
459 *
460 * allow function type pins to select
461 * any combination of function/in/out
462 * in their MARK lists.
463 */
464 in_range = 1;
465 }
Magnus Damm42eed422008-10-22 18:29:17 +0900466 }
467
Magnus Damm2967dab2008-10-08 20:41:43 +0900468 if (!in_range)
469 continue;
470
Paul Mundtb3c185a2012-06-20 17:29:04 +0900471 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900472 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900473 goto out_err;
474
475 switch (cfg_mode) {
476 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900477 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900478 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900479 continue;
480 break;
481
482 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900483 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900484 *cntp = *cntp + 1;
485 break;
486
487 case GPIO_CFG_FREE:
488 *cntp = *cntp - 1;
489 break;
490 }
491 }
492
493 return 0;
494 out_err:
495 return -1;
496}
497
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100498static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900499{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100500 struct sh_pfc_platform_data *pdata = pdev->dev.platform_data;
501 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900502 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900503
Paul Mundt06d56312012-06-21 00:03:41 +0900504 /*
505 * Ensure that the type encoding fits
506 */
507 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
508
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100509 if (pdata == NULL)
510 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900511
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100512 pfc = devm_kzalloc(&pdev->dev, sizeof(pfc), GFP_KERNEL);
513 if (pfc == NULL)
514 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900515
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100516 pfc->pdata = pdata;
517 pfc->dev = &pdev->dev;
518
519 ret = pfc_ioremap(pfc);
520 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100521 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100522
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100523 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900524
Paul Mundtca5481c62012-07-10 12:08:14 +0900525 pinctrl_provide_dummies();
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100526 setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900527
Paul Mundtca5481c62012-07-10 12:08:14 +0900528 /*
529 * Initialize pinctrl bindings first
530 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100531 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100532 if (unlikely(ret != 0))
Paul Mundt159ac072012-07-17 15:18:37 +0900533 goto err;
Magnus Damm69edbba2008-12-25 18:17:34 +0900534
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100535#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900536 /*
537 * Then the GPIO chip
538 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100539 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100540 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900541 /*
542 * If the GPIO chip fails to come up we still leave the
543 * PFC state as it is, given that there are already
544 * extant users of it that have succeeded by this point.
545 */
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100546 pr_notice("failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900547 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100548#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900549
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100550 platform_set_drvdata(pdev, pfc);
551
552 pr_info("%s support registered\n", pdata->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900553
Paul Mundtb3c185a2012-06-20 17:29:04 +0900554 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900555
556err:
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100557 pfc_iounmap(pfc);
Paul Mundtca5481c62012-07-10 12:08:14 +0900558 return ret;
Paul Mundtb72421d2010-10-04 03:54:56 +0900559}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100560
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100561static int sh_pfc_remove(struct platform_device *pdev)
562{
563 struct sh_pfc *pfc = platform_get_drvdata(pdev);
564
565#ifdef CONFIG_GPIO_SH_PFC
566 sh_pfc_unregister_gpiochip(pfc);
567#endif
568 sh_pfc_unregister_pinctrl(pfc);
569
570 pfc_iounmap(pfc);
571
572 platform_set_drvdata(pdev, NULL);
573
574 return 0;
575}
576
577static const struct platform_device_id sh_pfc_id_table[] = {
578 { "sh-pfc", 0 },
579 { },
580};
581MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
582
583static struct platform_driver sh_pfc_driver = {
584 .probe = sh_pfc_probe,
585 .remove = sh_pfc_remove,
586 .id_table = sh_pfc_id_table,
587 .driver = {
588 .name = DRV_NAME,
589 .owner = THIS_MODULE,
590 },
591};
592
593static struct platform_device sh_pfc_device = {
594 .name = DRV_NAME,
595 .id = -1,
596};
597
598int __init register_sh_pfc(struct sh_pfc_platform_data *pdata)
599{
600 int rc;
601
602 sh_pfc_device.dev.platform_data = pdata;
603
604 rc = platform_driver_register(&sh_pfc_driver);
605 if (likely(!rc)) {
606 rc = platform_device_register(&sh_pfc_device);
607 if (unlikely(rc))
608 platform_driver_unregister(&sh_pfc_driver);
609 }
610
611 return rc;
612}
613
614static void __exit sh_pfc_exit(void)
615{
616 platform_driver_unregister(&sh_pfc_driver);
617}
618module_exit(sh_pfc_exit);
619
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100620MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
621MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
622MODULE_LICENSE("GPL v2");