blob: cd8f09dcea95feea0d2122f599748ff2aeacd5b9 [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/bitops.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010016#include <linux/err.h>
17#include <linux/errno.h>
18#include <linux/io.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090019#include <linux/ioport.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010020#include <linux/kernel.h>
21#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090022#include <linux/pinctrl/machine.h>
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010023#include <linux/platform_device.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010024#include <linux/sh_pfc.h>
25#include <linux/slab.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090026
Laurent Pinchartf9165132012-12-15 23:50:44 +010027#include "core.h"
28
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010029static int sh_pfc_ioremap(struct sh_pfc *pfc)
Magnus Dammb0e10212011-12-09 12:14:27 +090030{
31 struct resource *res;
32 int k;
33
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010034 if (!pfc->pdata->num_resources)
Magnus Dammb0e10212011-12-09 12:14:27 +090035 return 0;
36
Laurent Pinchart1724acf2012-12-15 23:50:48 +010037 pfc->window = devm_kzalloc(pfc->dev, pfc->pdata->num_resources *
38 sizeof(*pfc->window), GFP_NOWAIT);
Paul Mundtb3c185a2012-06-20 17:29:04 +090039 if (!pfc->window)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010040 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090041
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010042 for (k = 0; k < pfc->pdata->num_resources; k++) {
43 res = pfc->pdata->resource + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090044 WARN_ON(resource_type(res) != IORESOURCE_MEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +090045 pfc->window[k].phys = res->start;
46 pfc->window[k].size = resource_size(res);
Laurent Pinchartc9fa88e2012-12-15 23:50:49 +010047 pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
48 resource_size(res));
49 if (!pfc->window[k].virt)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010050 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090051 }
52
53 return 0;
Magnus Dammb0e10212011-12-09 12:14:27 +090054}
55
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010056static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
57 unsigned long address)
Magnus Dammb0e10212011-12-09 12:14:27 +090058{
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010059 struct sh_pfc_window *window;
Magnus Dammb0e10212011-12-09 12:14:27 +090060 int k;
61
62 /* scan through physical windows and convert address */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +010063 for (k = 0; k < pfc->pdata->num_resources; k++) {
Paul Mundtb3c185a2012-06-20 17:29:04 +090064 window = pfc->window + k;
Magnus Dammb0e10212011-12-09 12:14:27 +090065
66 if (address < window->phys)
67 continue;
68
69 if (address >= (window->phys + window->size))
70 continue;
71
72 return window->virt + (address - window->phys);
73 }
74
75 /* no windows defined, register must be 1:1 mapped virt:phys */
76 return (void __iomem *)address;
77}
Magnus Damm2967dab2008-10-08 20:41:43 +090078
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010079static int sh_pfc_enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
Magnus Damm2967dab2008-10-08 20:41:43 +090080{
81 if (enum_id < r->begin)
82 return 0;
83
84 if (enum_id > r->end)
85 return 0;
86
87 return 1;
88}
89
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010090static unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
91 unsigned long reg_width)
Magnus Damm32920942008-12-25 18:17:26 +090092{
93 switch (reg_width) {
94 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +090095 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +090096 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +090097 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +090098 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +090099 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900100 }
101
102 BUG();
103 return 0;
104}
105
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100106static void sh_pfc_write_raw_reg(void __iomem *mapped_reg,
107 unsigned long reg_width, unsigned long data)
Magnus Damm32920942008-12-25 18:17:26 +0900108{
109 switch (reg_width) {
110 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900111 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900112 return;
113 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900114 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900115 return;
116 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900117 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900118 return;
119 }
120
121 BUG();
122}
123
Paul Mundtb3c185a2012-06-20 17:29:04 +0900124int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900125{
126 unsigned long pos;
127
128 pos = dr->reg_width - (in_pos + 1);
129
130 pr_debug("read_bit: addr = %lx, pos = %ld, "
131 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
132
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100133 return (sh_pfc_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
Magnus Damm92554d92011-12-14 01:00:37 +0900134}
135
Paul Mundtb3c185a2012-06-20 17:29:04 +0900136void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
137 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900138{
139 unsigned long pos;
140
141 pos = dr->reg_width - (in_pos + 1);
142
Paul Mundtca6f2d72009-12-09 15:51:27 +0900143 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900144 "r_width = %ld\n",
145 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900146
147 if (value)
148 set_bit(pos, &dr->reg_shadow);
149 else
150 clear_bit(pos, &dr->reg_shadow);
151
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100152 sh_pfc_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900153}
154
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100155static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
156 struct pinmux_cfg_reg *crp,
157 unsigned long in_pos,
158 void __iomem **mapped_regp,
159 unsigned long *maskp,
160 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900161{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900162 int k;
163
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100164 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900165
Magnus Dammf78a26f2011-12-14 01:01:05 +0900166 if (crp->field_width) {
167 *maskp = (1 << crp->field_width) - 1;
168 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
169 } else {
170 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
171 *posp = crp->reg_width;
172 for (k = 0; k <= in_pos; k++)
173 *posp -= crp->var_field_width[k];
174 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900175}
Magnus Damm2967dab2008-10-08 20:41:43 +0900176
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100177static int sh_pfc_read_config_reg(struct sh_pfc *pfc,
178 struct pinmux_cfg_reg *crp,
179 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900180{
Magnus Damm18925e12011-12-14 01:00:55 +0900181 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900182 unsigned long mask, pos;
183
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100184 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900185
Magnus Damm18925e12011-12-14 01:00:55 +0900186 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900187 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900188 crp->reg, field, crp->reg_width, crp->field_width);
189
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100190 return (sh_pfc_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
Magnus Damm18925e12011-12-14 01:00:55 +0900191}
192
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100193static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
194 struct pinmux_cfg_reg *crp,
195 unsigned long field, unsigned long value)
Magnus Damm18925e12011-12-14 01:00:55 +0900196{
197 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900198 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900199
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100200 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900201
202 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
203 "r_width = %ld, f_width = %ld\n",
204 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900205
206 mask = ~(mask << pos);
207 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900208
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100209 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
Magnus Damme499ada2011-12-14 01:01:14 +0900210 data &= mask;
211 data |= value;
212
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100213 if (pfc->pdata->unlock_reg)
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100214 sh_pfc_write_raw_reg(
215 sh_pfc_phys_to_virt(pfc, pfc->pdata->unlock_reg), 32,
216 ~data);
Magnus Damme499ada2011-12-14 01:01:14 +0900217
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100218 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900219}
220
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100221static int sh_pfc_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900222{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100223 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900224 struct pinmux_data_reg *data_reg;
225 int k, n;
226
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100227 if (!sh_pfc_enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900228 return -1;
229
230 k = 0;
231 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100232 data_reg = pfc->pdata->data_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900233
234 if (!data_reg->reg_width)
235 break;
236
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100237 data_reg->mapped_reg = sh_pfc_phys_to_virt(pfc, data_reg->reg);
Magnus Dammb0e10212011-12-09 12:14:27 +0900238
Magnus Damm2967dab2008-10-08 20:41:43 +0900239 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900240 if (data_reg->enum_ids[n] == gpiop->enum_id) {
241 gpiop->flags &= ~PINMUX_FLAG_DREG;
242 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
243 gpiop->flags &= ~PINMUX_FLAG_DBIT;
244 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900245 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900246 }
247 }
248 k++;
249 }
250
Magnus Damm18801be2008-12-25 18:17:09 +0900251 BUG();
252
Magnus Damm2967dab2008-10-08 20:41:43 +0900253 return -1;
254}
255
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100256static void sh_pfc_setup_data_regs(struct sh_pfc *pfc)
Magnus Damm32920942008-12-25 18:17:26 +0900257{
258 struct pinmux_data_reg *drp;
259 int k;
260
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100261 for (k = pfc->pdata->first_gpio; k <= pfc->pdata->last_gpio; k++)
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100262 sh_pfc_setup_data_reg(pfc, k);
Magnus Damm32920942008-12-25 18:17:26 +0900263
264 k = 0;
265 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100266 drp = pfc->pdata->data_regs + k;
Magnus Damm32920942008-12-25 18:17:26 +0900267
268 if (!drp->reg_width)
269 break;
270
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100271 drp->reg_shadow = sh_pfc_read_raw_reg(drp->mapped_reg,
272 drp->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900273 k++;
274 }
275}
276
Paul Mundtb3c185a2012-06-20 17:29:04 +0900277int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
Magnus Damm18801be2008-12-25 18:17:09 +0900278 struct pinmux_data_reg **drp, int *bitp)
279{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100280 struct pinmux_gpio *gpiop = &pfc->pdata->gpios[gpio];
Magnus Damm18801be2008-12-25 18:17:09 +0900281 int k, n;
282
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100283 if (!sh_pfc_enum_in_range(gpiop->enum_id, &pfc->pdata->data))
Magnus Damm18801be2008-12-25 18:17:09 +0900284 return -1;
285
286 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
287 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100288 *drp = pfc->pdata->data_regs + k;
Magnus Damm18801be2008-12-25 18:17:09 +0900289 *bitp = n;
290 return 0;
291}
292
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100293static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
294 struct pinmux_cfg_reg **crp, int *fieldp,
295 int *valuep, unsigned long **cntp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900296{
297 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900298 unsigned long r_width, f_width, curr_width, ncomb;
299 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900300
301 k = 0;
302 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100303 config_reg = pfc->pdata->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900304
305 r_width = config_reg->reg_width;
306 f_width = config_reg->field_width;
307
308 if (!r_width)
309 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900310
311 pos = 0;
312 m = 0;
313 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
314 if (f_width)
315 curr_width = f_width;
316 else
317 curr_width = config_reg->var_field_width[m];
318
319 ncomb = 1 << curr_width;
320 for (n = 0; n < ncomb; n++) {
321 if (config_reg->enum_ids[pos + n] == enum_id) {
322 *crp = config_reg;
323 *fieldp = m;
324 *valuep = n;
325 *cntp = &config_reg->cnt[m];
326 return 0;
327 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900328 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900329 pos += ncomb;
330 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900331 }
332 k++;
333 }
334
335 return -1;
336}
337
Paul Mundtb3c185a2012-06-20 17:29:04 +0900338int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
339 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900340{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100341 pinmux_enum_t enum_id = pfc->pdata->gpios[gpio].enum_id;
342 pinmux_enum_t *data = pfc->pdata->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900343 int k;
344
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100345 if (!sh_pfc_enum_in_range(enum_id, &pfc->pdata->data)) {
346 if (!sh_pfc_enum_in_range(enum_id, &pfc->pdata->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900347 pr_err("non data/mark enum_id for gpio %d\n", gpio);
348 return -1;
349 }
350 }
351
352 if (pos) {
353 *enum_idp = data[pos + 1];
354 return pos + 1;
355 }
356
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100357 for (k = 0; k < pfc->pdata->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900358 if (data[k] == enum_id) {
359 *enum_idp = data[k + 1];
360 return k + 1;
361 }
362 }
363
364 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
365 return -1;
366}
367
Paul Mundtb3c185a2012-06-20 17:29:04 +0900368int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
369 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900370{
371 struct pinmux_cfg_reg *cr = NULL;
372 pinmux_enum_t enum_id;
373 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900374 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900375 unsigned long *cntp;
376
377 switch (pinmux_type) {
378
379 case PINMUX_TYPE_FUNCTION:
380 range = NULL;
381 break;
382
383 case PINMUX_TYPE_OUTPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100384 range = &pfc->pdata->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900385 break;
386
387 case PINMUX_TYPE_INPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100388 range = &pfc->pdata->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900389 break;
390
391 case PINMUX_TYPE_INPUT_PULLUP:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100392 range = &pfc->pdata->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900393 break;
394
395 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100396 range = &pfc->pdata->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900397 break;
398
399 default:
400 goto out_err;
401 }
402
403 pos = 0;
404 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900405 field = 0;
406 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900407 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900408 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900409 if (pos <= 0)
410 goto out_err;
411
412 if (!enum_id)
413 break;
414
Magnus Damm50dd3142010-01-19 13:52:28 +0000415 /* first check if this is a function enum */
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100416 in_range = sh_pfc_enum_in_range(enum_id, &pfc->pdata->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000417 if (!in_range) {
418 /* not a function enum */
419 if (range) {
420 /*
421 * other range exists, so this pin is
422 * a regular GPIO pin that now is being
423 * bound to a specific direction.
424 *
425 * for this case we only allow function enums
426 * and the enums that match the other range.
427 */
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100428 in_range = sh_pfc_enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900429
Magnus Damm50dd3142010-01-19 13:52:28 +0000430 /*
431 * special case pass through for fixed
432 * input-only or output-only pins without
433 * function enum register association.
434 */
435 if (in_range && enum_id == range->force)
436 continue;
437 } else {
438 /*
439 * no other range exists, so this pin
440 * must then be of the function type.
441 *
442 * allow function type pins to select
443 * any combination of function/in/out
444 * in their MARK lists.
445 */
446 in_range = 1;
447 }
Magnus Damm42eed422008-10-22 18:29:17 +0900448 }
449
Magnus Damm2967dab2008-10-08 20:41:43 +0900450 if (!in_range)
451 continue;
452
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100453 if (sh_pfc_get_config_reg(pfc, enum_id, &cr,
454 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900455 goto out_err;
456
457 switch (cfg_mode) {
458 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900459 if (!*cntp ||
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100460 (sh_pfc_read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900461 continue;
462 break;
463
464 case GPIO_CFG_REQ:
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100465 sh_pfc_write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900466 *cntp = *cntp + 1;
467 break;
468
469 case GPIO_CFG_FREE:
470 *cntp = *cntp - 1;
471 break;
472 }
473 }
474
475 return 0;
476 out_err:
477 return -1;
478}
479
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100480static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900481{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100482 struct sh_pfc_platform_data *pdata = pdev->dev.platform_data;
483 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900484 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900485
Paul Mundt06d56312012-06-21 00:03:41 +0900486 /*
487 * Ensure that the type encoding fits
488 */
489 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
490
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100491 if (pdata == NULL)
492 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900493
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100494 pfc = devm_kzalloc(&pdev->dev, sizeof(pfc), GFP_KERNEL);
495 if (pfc == NULL)
496 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900497
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100498 pfc->pdata = pdata;
499 pfc->dev = &pdev->dev;
500
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100501 ret = sh_pfc_ioremap(pfc);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100502 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100503 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100504
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100505 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900506
Paul Mundtca5481c62012-07-10 12:08:14 +0900507 pinctrl_provide_dummies();
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100508 sh_pfc_setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900509
Paul Mundtca5481c62012-07-10 12:08:14 +0900510 /*
511 * Initialize pinctrl bindings first
512 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100513 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100514 if (unlikely(ret != 0))
Laurent Pinchartc9fa88e2012-12-15 23:50:49 +0100515 return ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900516
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100517#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900518 /*
519 * Then the GPIO chip
520 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100521 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100522 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900523 /*
524 * If the GPIO chip fails to come up we still leave the
525 * PFC state as it is, given that there are already
526 * extant users of it that have succeeded by this point.
527 */
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100528 pr_notice("failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900529 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100530#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900531
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100532 platform_set_drvdata(pdev, pfc);
533
534 pr_info("%s support registered\n", pdata->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900535
Paul Mundtb3c185a2012-06-20 17:29:04 +0900536 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900537}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100538
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100539static int sh_pfc_remove(struct platform_device *pdev)
540{
541 struct sh_pfc *pfc = platform_get_drvdata(pdev);
542
543#ifdef CONFIG_GPIO_SH_PFC
544 sh_pfc_unregister_gpiochip(pfc);
545#endif
546 sh_pfc_unregister_pinctrl(pfc);
547
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100548 platform_set_drvdata(pdev, NULL);
549
550 return 0;
551}
552
553static const struct platform_device_id sh_pfc_id_table[] = {
554 { "sh-pfc", 0 },
555 { },
556};
557MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
558
559static struct platform_driver sh_pfc_driver = {
560 .probe = sh_pfc_probe,
561 .remove = sh_pfc_remove,
562 .id_table = sh_pfc_id_table,
563 .driver = {
564 .name = DRV_NAME,
565 .owner = THIS_MODULE,
566 },
567};
568
569static struct platform_device sh_pfc_device = {
570 .name = DRV_NAME,
571 .id = -1,
572};
573
574int __init register_sh_pfc(struct sh_pfc_platform_data *pdata)
575{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100576 sh_pfc_device.dev.platform_data = pdata;
577
Laurent Pinchart40ee6fc2012-12-15 23:50:54 +0100578 return platform_device_register(&sh_pfc_device);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100579}
580
Laurent Pinchart40ee6fc2012-12-15 23:50:54 +0100581static int __init sh_pfc_init(void)
582{
583 return platform_driver_register(&sh_pfc_driver);
584}
585postcore_initcall(sh_pfc_init);
586
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100587static void __exit sh_pfc_exit(void)
588{
589 platform_driver_unregister(&sh_pfc_driver);
590}
591module_exit(sh_pfc_exit);
592
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100593MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
594MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
595MODULE_LICENSE("GPL v2");