blob: 54919026ac12a6d4dab9409373f01d6e0c0a4b76 [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}
37
Paul Mundtb3c185a2012-06-20 17:29:04 +090038static int pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090039{
40 struct resource *res;
41 int k;
42
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010043 if (!pfc->pdata->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090044 return 0;
45
Laurent Pinchart1724acf2012-12-15 23:50:48 +010046 pfc->window = devm_kzalloc(pfc->dev, pfc->pdata->num_resources *
47 sizeof(*pfc->window), GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090048 if (!pfc->window)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010049 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090050
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010051 for (k = 0; k < pfc->pdata->num_resources; k++) {
52 res = pfc->pdata->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090053 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090054 pfc->window[k].phys = res->start;
55 pfc->window[k].size = resource_size(res);
56 pfc->window[k].virt = ioremap_nocache(res->start,
Magnus Dammb0e10212011-12-09 12:14:27 +090057 resource_size(res));
Laurent Pinchart1724acf2012-12-15 23:50:48 +010058 if (!pfc->window[k].virt) {
59 pfc_iounmap(pfc);
60 return -ENOMEM;
61 }
Magnus Dammb0e10212011-12-09 12:14:27 +090062 }
63
64 return 0;
Magnus Dammb0e10212011-12-09 12:14:27 +090065}
66
Paul Mundtb3c185a2012-06-20 17:29:04 +090067static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090068 unsigned long address)
69{
70 struct pfc_window *window;
71 int k;
72
73 /* scan through physical windows and convert address */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010074 for (k = 0; k < pfc->pdata->num_resources; k++) {
Paul Mundtb3c185a2012-06-20 17:29:04 +090075 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090076
77 if (address < window->phys)
78 continue;
79
80 if (address >= (window->phys + window->size))
81 continue;
82
83 return window->virt + (address - window->phys);
84 }
85
86 /* no windows defined, register must be 1:1 mapped virt:phys */
87 return (void __iomem *)address;
88}
Magnus Damm2967dab2008-10-08 20:41:43 +090089
Magnus Damm2967dab2008-10-08 20:41:43 +090090static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
91{
92 if (enum_id < r->begin)
93 return 0;
94
95 if (enum_id > r->end)
96 return 0;
97
98 return 1;
99}
100
Magnus Dammb0e10212011-12-09 12:14:27 +0900101static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900102 unsigned long reg_width)
103{
104 switch (reg_width) {
105 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900106 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900107 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900108 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900109 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900110 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900111 }
112
113 BUG();
114 return 0;
115}
116
Magnus Dammb0e10212011-12-09 12:14:27 +0900117static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900118 unsigned long reg_width,
119 unsigned long data)
120{
121 switch (reg_width) {
122 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900123 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900124 return;
125 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900126 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900127 return;
128 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900129 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900130 return;
131 }
132
133 BUG();
134}
135
Paul Mundtb3c185a2012-06-20 17:29:04 +0900136int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900137{
138 unsigned long pos;
139
140 pos = dr->reg_width - (in_pos + 1);
141
142 pr_debug("read_bit: addr = %lx, pos = %ld, "
143 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
144
145 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
146}
147
Paul Mundtb3c185a2012-06-20 17:29:04 +0900148void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
149 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900150{
151 unsigned long pos;
152
153 pos = dr->reg_width - (in_pos + 1);
154
Paul Mundtca6f2d72009-12-09 15:51:27 +0900155 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900156 "r_width = %ld\n",
157 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900158
159 if (value)
160 set_bit(pos, &dr->reg_shadow);
161 else
162 clear_bit(pos, &dr->reg_shadow);
163
Magnus Dammb0e10212011-12-09 12:14:27 +0900164 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900165}
166
Paul Mundtb3c185a2012-06-20 17:29:04 +0900167static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900168 struct pinmux_cfg_reg *crp,
169 unsigned long in_pos,
170 void __iomem **mapped_regp,
171 unsigned long *maskp,
172 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900173{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900174 int k;
175
Paul Mundtb3c185a2012-06-20 17:29:04 +0900176 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900177
Magnus Dammf78a26f2011-12-14 01:01:05 +0900178 if (crp->field_width) {
179 *maskp = (1 << crp->field_width) - 1;
180 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
181 } else {
182 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
183 *posp = crp->reg_width;
184 for (k = 0; k <= in_pos; k++)
185 *posp -= crp->var_field_width[k];
186 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900187}
Magnus Damm2967dab2008-10-08 20:41:43 +0900188
Paul Mundtb3c185a2012-06-20 17:29:04 +0900189static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900190 struct pinmux_cfg_reg *crp,
191 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900192{
Magnus Damm18925e12011-12-14 01:00:55 +0900193 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900194 unsigned long mask, pos;
195
Paul Mundtb3c185a2012-06-20 17:29:04 +0900196 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900197
Magnus Damm18925e12011-12-14 01:00:55 +0900198 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900199 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900200 crp->reg, field, crp->reg_width, crp->field_width);
201
202 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
203}
204
Paul Mundtb3c185a2012-06-20 17:29:04 +0900205static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900206 struct pinmux_cfg_reg *crp,
207 unsigned long field, unsigned long value)
208{
209 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900210 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900211
Paul Mundtb3c185a2012-06-20 17:29:04 +0900212 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900213
214 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
215 "r_width = %ld, f_width = %ld\n",
216 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900217
218 mask = ~(mask << pos);
219 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900220
Magnus Damme499ada2011-12-14 01:01:14 +0900221 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
222 data &= mask;
223 data |= value;
224
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100225 if (pfc->pdata->unlock_reg)
226 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->pdata->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900227 32, ~data);
228
229 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900230}
231
Paul Mundtb3c185a2012-06-20 17:29:04 +0900232static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900233{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100234 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900235 struct pinmux_data_reg *data_reg;
236 int k, n;
237
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100238 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900239 return -1;
240
241 k = 0;
242 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100243 data_reg = pfc->pdata->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900244
245 if (!data_reg->reg_width)
246 break;
247
Paul Mundtb3c185a2012-06-20 17:29:04 +0900248 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900249
Magnus Damm2967dab2008-10-08 20:41:43 +0900250 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900251 if (data_reg->enum_ids[n] == gpiop->enum_id) {
252 gpiop->flags &= ~PINMUX_FLAG_DREG;
253 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
254 gpiop->flags &= ~PINMUX_FLAG_DBIT;
255 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900256 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900257 }
258 }
259 k++;
260 }
261
Magnus Damm18801be2008-12-25 18:17:09 +0900262 BUG();
263
Magnus Damm2967dab2008-10-08 20:41:43 +0900264 return -1;
265}
266
Paul Mundtb3c185a2012-06-20 17:29:04 +0900267static void setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900268{
269 struct pinmux_data_reg *drp;
270 int k;
271
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100272 for (k = pfc->pdata->first_gpio; k <= pfc->pdata->last_gpio; k++)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900273 setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900274
275 k = 0;
276 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100277 drp = pfc->pdata->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900278
279 if (!drp->reg_width)
280 break;
281
Magnus Dammb0e10212011-12-09 12:14:27 +0900282 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
283 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900284 k++;
285 }
286}
287
Paul Mundtb3c185a2012-06-20 17:29:04 +0900288int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900289 struct pinmux_data_reg **drp, int *bitp)
290{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100291 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900292 int k, n;
293
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100294 if (!enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900295 return -1;
296
297 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
298 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100299 *drp = pfc->pdata->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900300 *bitp = n;
301 return 0;
302}
303
Paul Mundtb3c185a2012-06-20 17:29:04 +0900304static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900305 struct pinmux_cfg_reg **crp,
306 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900307 unsigned long **cntp)
308{
309 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900310 unsigned long r_width, f_width, curr_width, ncomb;
311 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900312
313 k = 0;
314 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100315 config_reg = pfc->pdata->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900316
317 r_width = config_reg->reg_width;
318 f_width = config_reg->field_width;
319
320 if (!r_width)
321 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900322
323 pos = 0;
324 m = 0;
325 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
326 if (f_width)
327 curr_width = f_width;
328 else
329 curr_width = config_reg->var_field_width[m];
330
331 ncomb = 1 << curr_width;
332 for (n = 0; n < ncomb; n++) {
333 if (config_reg->enum_ids[pos + n] == enum_id) {
334 *crp = config_reg;
335 *fieldp = m;
336 *valuep = n;
337 *cntp = &config_reg->cnt[m];
338 return 0;
339 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900340 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900341 pos += ncomb;
342 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900343 }
344 k++;
345 }
346
347 return -1;
348}
349
Paul Mundtb3c185a2012-06-20 17:29:04 +0900350int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
351 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900352{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100353 pinmux_enum_t enum_id = pfc->pdata->gpios[gpio].enum_id;
354 pinmux_enum_t *data = pfc->pdata->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900355 int k;
356
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100357 if (!enum_in_range(enum_id, &pfc->pdata->data)) {
358 if (!enum_in_range(enum_id, &pfc->pdata->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900359 pr_err("non data/mark enum_id for gpio %d\n", gpio);
360 return -1;
361 }
362 }
363
364 if (pos) {
365 *enum_idp = data[pos + 1];
366 return pos + 1;
367 }
368
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100369 for (k = 0; k < pfc->pdata->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900370 if (data[k] == enum_id) {
371 *enum_idp = data[k + 1];
372 return k + 1;
373 }
374 }
375
376 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
377 return -1;
378}
379
Paul Mundtb3c185a2012-06-20 17:29:04 +0900380int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
381 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900382{
383 struct pinmux_cfg_reg *cr = NULL;
384 pinmux_enum_t enum_id;
385 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900386 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900387 unsigned long *cntp;
388
389 switch (pinmux_type) {
390
391 case PINMUX_TYPE_FUNCTION:
392 range = NULL;
393 break;
394
395 case PINMUX_TYPE_OUTPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100396 range = &pfc->pdata->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900397 break;
398
399 case PINMUX_TYPE_INPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100400 range = &pfc->pdata->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900401 break;
402
403 case PINMUX_TYPE_INPUT_PULLUP:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100404 range = &pfc->pdata->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900405 break;
406
407 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100408 range = &pfc->pdata->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900409 break;
410
411 default:
412 goto out_err;
413 }
414
415 pos = 0;
416 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900417 field = 0;
418 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900419 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900420 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900421 if (pos <= 0)
422 goto out_err;
423
424 if (!enum_id)
425 break;
426
Magnus Damm50dd3142010-01-19 13:52:28 +0000427 /* first check if this is a function enum */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100428 in_range = enum_in_range(enum_id, &pfc->pdata->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000429 if (!in_range) {
430 /* not a function enum */
431 if (range) {
432 /*
433 * other range exists, so this pin is
434 * a regular GPIO pin that now is being
435 * bound to a specific direction.
436 *
437 * for this case we only allow function enums
438 * and the enums that match the other range.
439 */
440 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900441
Magnus Damm50dd3142010-01-19 13:52:28 +0000442 /*
443 * special case pass through for fixed
444 * input-only or output-only pins without
445 * function enum register association.
446 */
447 if (in_range && enum_id == range->force)
448 continue;
449 } else {
450 /*
451 * no other range exists, so this pin
452 * must then be of the function type.
453 *
454 * allow function type pins to select
455 * any combination of function/in/out
456 * in their MARK lists.
457 */
458 in_range = 1;
459 }
Magnus Damm42eed422008-10-22 18:29:17 +0900460 }
461
Magnus Damm2967dab2008-10-08 20:41:43 +0900462 if (!in_range)
463 continue;
464
Paul Mundtb3c185a2012-06-20 17:29:04 +0900465 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900466 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900467 goto out_err;
468
469 switch (cfg_mode) {
470 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900471 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900472 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900473 continue;
474 break;
475
476 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900477 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900478 *cntp = *cntp + 1;
479 break;
480
481 case GPIO_CFG_FREE:
482 *cntp = *cntp - 1;
483 break;
484 }
485 }
486
487 return 0;
488 out_err:
489 return -1;
490}
491
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100492static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900493{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100494 struct sh_pfc_platform_data *pdata = pdev->dev.platform_data;
495 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900496 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900497
Paul Mundt06d56312012-06-21 00:03:41 +0900498 /*
499 * Ensure that the type encoding fits
500 */
501 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
502
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100503 if (pdata == NULL)
504 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900505
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100506 pfc = devm_kzalloc(&pdev->dev, sizeof(pfc), GFP_KERNEL);
507 if (pfc == NULL)
508 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900509
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100510 pfc->pdata = pdata;
511 pfc->dev = &pdev->dev;
512
513 ret = pfc_ioremap(pfc);
514 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100515 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100516
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100517 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900518
Paul Mundtca5481c62012-07-10 12:08:14 +0900519 pinctrl_provide_dummies();
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100520 setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900521
Paul Mundtca5481c62012-07-10 12:08:14 +0900522 /*
523 * Initialize pinctrl bindings first
524 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100525 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100526 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 Pinchartc6193ea2012-12-15 23:50:47 +0100533 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100534 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 Pinchartc6193ea2012-12-15 23:50:47 +0100544 platform_set_drvdata(pdev, pfc);
545
546 pr_info("%s support registered\n", pdata->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900547
Paul Mundtb3c185a2012-06-20 17:29:04 +0900548 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900549
550err:
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100551 pfc_iounmap(pfc);
Paul Mundtca5481c62012-07-10 12:08:14 +0900552 return ret;
Paul Mundtb72421d2010-10-04 03:54:56 +0900553}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100554
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100555static int sh_pfc_remove(struct platform_device *pdev)
556{
557 struct sh_pfc *pfc = platform_get_drvdata(pdev);
558
559#ifdef CONFIG_GPIO_SH_PFC
560 sh_pfc_unregister_gpiochip(pfc);
561#endif
562 sh_pfc_unregister_pinctrl(pfc);
563
564 pfc_iounmap(pfc);
565
566 platform_set_drvdata(pdev, NULL);
567
568 return 0;
569}
570
571static const struct platform_device_id sh_pfc_id_table[] = {
572 { "sh-pfc", 0 },
573 { },
574};
575MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
576
577static struct platform_driver sh_pfc_driver = {
578 .probe = sh_pfc_probe,
579 .remove = sh_pfc_remove,
580 .id_table = sh_pfc_id_table,
581 .driver = {
582 .name = DRV_NAME,
583 .owner = THIS_MODULE,
584 },
585};
586
587static struct platform_device sh_pfc_device = {
588 .name = DRV_NAME,
589 .id = -1,
590};
591
592int __init register_sh_pfc(struct sh_pfc_platform_data *pdata)
593{
594 int rc;
595
596 sh_pfc_device.dev.platform_data = pdata;
597
598 rc = platform_driver_register(&sh_pfc_driver);
599 if (likely(!rc)) {
600 rc = platform_device_register(&sh_pfc_device);
601 if (unlikely(rc))
602 platform_driver_unregister(&sh_pfc_driver);
603 }
604
605 return rc;
606}
607
608static void __exit sh_pfc_exit(void)
609{
610 platform_driver_unregister(&sh_pfc_driver);
611}
612module_exit(sh_pfc_exit);
613
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100614MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
615MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
616MODULE_LICENSE("GPL v2");