blob: ccf4b7a7cefaabd6267429a941ac5d93f064542b [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
Paul Mundtb3c185a2012-06-20 17:29:04 +090029static int 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
Paul Mundtb3c185a2012-06-20 17:29:04 +090056static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
Magnus Dammb0e10212011-12-09 12:14:27 +090057 unsigned long address)
58{
59 struct pfc_window *window;
60 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
Magnus Damm2967dab2008-10-08 20:41:43 +090079static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
80{
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
Magnus Dammb0e10212011-12-09 12:14:27 +090090static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +090091 unsigned long reg_width)
92{
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
Magnus Dammb0e10212011-12-09 12:14:27 +0900106static void gpio_write_raw_reg(void __iomem *mapped_reg,
Magnus Damm32920942008-12-25 18:17:26 +0900107 unsigned long reg_width,
108 unsigned long data)
109{
110 switch (reg_width) {
111 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900112 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900113 return;
114 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900115 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900116 return;
117 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900118 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900119 return;
120 }
121
122 BUG();
123}
124
Paul Mundtb3c185a2012-06-20 17:29:04 +0900125int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
Magnus Damm92554d92011-12-14 01:00:37 +0900126{
127 unsigned long pos;
128
129 pos = dr->reg_width - (in_pos + 1);
130
131 pr_debug("read_bit: addr = %lx, pos = %ld, "
132 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
133
134 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
135}
136
Paul Mundtb3c185a2012-06-20 17:29:04 +0900137void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
138 unsigned long value)
Magnus Damm32920942008-12-25 18:17:26 +0900139{
140 unsigned long pos;
141
142 pos = dr->reg_width - (in_pos + 1);
143
Paul Mundtca6f2d72009-12-09 15:51:27 +0900144 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900145 "r_width = %ld\n",
146 dr->reg, !!value, pos, dr->reg_width);
Magnus Damm32920942008-12-25 18:17:26 +0900147
148 if (value)
149 set_bit(pos, &dr->reg_shadow);
150 else
151 clear_bit(pos, &dr->reg_shadow);
152
Magnus Dammb0e10212011-12-09 12:14:27 +0900153 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Magnus Damm32920942008-12-25 18:17:26 +0900154}
155
Paul Mundtb3c185a2012-06-20 17:29:04 +0900156static void config_reg_helper(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900157 struct pinmux_cfg_reg *crp,
158 unsigned long in_pos,
159 void __iomem **mapped_regp,
160 unsigned long *maskp,
161 unsigned long *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900162{
Magnus Dammf78a26f2011-12-14 01:01:05 +0900163 int k;
164
Paul Mundtb3c185a2012-06-20 17:29:04 +0900165 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900166
Magnus Dammf78a26f2011-12-14 01:01:05 +0900167 if (crp->field_width) {
168 *maskp = (1 << crp->field_width) - 1;
169 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
170 } else {
171 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
172 *posp = crp->reg_width;
173 for (k = 0; k <= in_pos; k++)
174 *posp -= crp->var_field_width[k];
175 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900176}
Magnus Damm2967dab2008-10-08 20:41:43 +0900177
Paul Mundtb3c185a2012-06-20 17:29:04 +0900178static int read_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900179 struct pinmux_cfg_reg *crp,
180 unsigned long field)
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900181{
Magnus Damm18925e12011-12-14 01:00:55 +0900182 void __iomem *mapped_reg;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900183 unsigned long mask, pos;
184
Paul Mundtb3c185a2012-06-20 17:29:04 +0900185 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900186
Magnus Damm18925e12011-12-14 01:00:55 +0900187 pr_debug("read_reg: addr = %lx, field = %ld, "
Paul Mundtfd2cb0c2009-11-30 12:15:04 +0900188 "r_width = %ld, f_width = %ld\n",
Magnus Damm18925e12011-12-14 01:00:55 +0900189 crp->reg, field, crp->reg_width, crp->field_width);
190
191 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
192}
193
Paul Mundtb3c185a2012-06-20 17:29:04 +0900194static void write_config_reg(struct sh_pfc *pfc,
Magnus Damm18925e12011-12-14 01:00:55 +0900195 struct pinmux_cfg_reg *crp,
196 unsigned long field, unsigned long value)
197{
198 void __iomem *mapped_reg;
Magnus Damme499ada2011-12-14 01:01:14 +0900199 unsigned long mask, pos, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900200
Paul Mundtb3c185a2012-06-20 17:29:04 +0900201 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900202
203 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
204 "r_width = %ld, f_width = %ld\n",
205 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900206
207 mask = ~(mask << pos);
208 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900209
Magnus Damme499ada2011-12-14 01:01:14 +0900210 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
211 data &= mask;
212 data |= value;
213
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100214 if (pfc->pdata->unlock_reg)
215 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->pdata->unlock_reg),
Magnus Damme499ada2011-12-14 01:01:14 +0900216 32, ~data);
217
218 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900219}
220
Paul Mundtb3c185a2012-06-20 17:29:04 +0900221static int 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 Pinchartd4e62d02012-12-15 23:50:43 +0100227 if (!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
Paul Mundtb3c185a2012-06-20 17:29:04 +0900237 data_reg->mapped_reg = 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
Paul Mundtb3c185a2012-06-20 17:29:04 +0900256static void 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++)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900262 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
Magnus Dammb0e10212011-12-09 12:14:27 +0900271 drp->reg_shadow = gpio_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 Pinchartd4e62d02012-12-15 23:50:43 +0100283 if (!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
Paul Mundtb3c185a2012-06-20 17:29:04 +0900293static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900294 struct pinmux_cfg_reg **crp,
295 int *fieldp, int *valuep,
Magnus Damm2967dab2008-10-08 20:41:43 +0900296 unsigned long **cntp)
297{
298 struct pinmux_cfg_reg *config_reg;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900299 unsigned long r_width, f_width, curr_width, ncomb;
300 int k, m, n, pos, bit_pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900301
302 k = 0;
303 while (1) {
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100304 config_reg = pfc->pdata->cfg_regs + k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900305
306 r_width = config_reg->reg_width;
307 f_width = config_reg->field_width;
308
309 if (!r_width)
310 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900311
312 pos = 0;
313 m = 0;
314 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
315 if (f_width)
316 curr_width = f_width;
317 else
318 curr_width = config_reg->var_field_width[m];
319
320 ncomb = 1 << curr_width;
321 for (n = 0; n < ncomb; n++) {
322 if (config_reg->enum_ids[pos + n] == enum_id) {
323 *crp = config_reg;
324 *fieldp = m;
325 *valuep = n;
326 *cntp = &config_reg->cnt[m];
327 return 0;
328 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900329 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900330 pos += ncomb;
331 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900332 }
333 k++;
334 }
335
336 return -1;
337}
338
Paul Mundtb3c185a2012-06-20 17:29:04 +0900339int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
340 pinmux_enum_t *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900341{
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100342 pinmux_enum_t enum_id = pfc->pdata->gpios[gpio].enum_id;
343 pinmux_enum_t *data = pfc->pdata->gpio_data;
Magnus Damm2967dab2008-10-08 20:41:43 +0900344 int k;
345
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100346 if (!enum_in_range(enum_id, &pfc->pdata->data)) {
347 if (!enum_in_range(enum_id, &pfc->pdata->mark)) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900348 pr_err("non data/mark enum_id for gpio %d\n", gpio);
349 return -1;
350 }
351 }
352
353 if (pos) {
354 *enum_idp = data[pos + 1];
355 return pos + 1;
356 }
357
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100358 for (k = 0; k < pfc->pdata->gpio_data_size; k++) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900359 if (data[k] == enum_id) {
360 *enum_idp = data[k + 1];
361 return k + 1;
362 }
363 }
364
365 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
366 return -1;
367}
368
Paul Mundtb3c185a2012-06-20 17:29:04 +0900369int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
370 int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900371{
372 struct pinmux_cfg_reg *cr = NULL;
373 pinmux_enum_t enum_id;
374 struct pinmux_range *range;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900375 int in_range, pos, field, value;
Magnus Damm2967dab2008-10-08 20:41:43 +0900376 unsigned long *cntp;
377
378 switch (pinmux_type) {
379
380 case PINMUX_TYPE_FUNCTION:
381 range = NULL;
382 break;
383
384 case PINMUX_TYPE_OUTPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100385 range = &pfc->pdata->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900386 break;
387
388 case PINMUX_TYPE_INPUT:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100389 range = &pfc->pdata->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900390 break;
391
392 case PINMUX_TYPE_INPUT_PULLUP:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100393 range = &pfc->pdata->input_pu;
Magnus Damm2967dab2008-10-08 20:41:43 +0900394 break;
395
396 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100397 range = &pfc->pdata->input_pd;
Magnus Damm2967dab2008-10-08 20:41:43 +0900398 break;
399
400 default:
401 goto out_err;
402 }
403
404 pos = 0;
405 enum_id = 0;
Magnus Dammad4a07f2011-12-14 01:00:46 +0900406 field = 0;
407 value = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900408 while (1) {
Paul Mundtb3c185a2012-06-20 17:29:04 +0900409 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
Magnus Damm2967dab2008-10-08 20:41:43 +0900410 if (pos <= 0)
411 goto out_err;
412
413 if (!enum_id)
414 break;
415
Magnus Damm50dd3142010-01-19 13:52:28 +0000416 /* first check if this is a function enum */
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100417 in_range = enum_in_range(enum_id, &pfc->pdata->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000418 if (!in_range) {
419 /* not a function enum */
420 if (range) {
421 /*
422 * other range exists, so this pin is
423 * a regular GPIO pin that now is being
424 * bound to a specific direction.
425 *
426 * for this case we only allow function enums
427 * and the enums that match the other range.
428 */
429 in_range = enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900430
Magnus Damm50dd3142010-01-19 13:52:28 +0000431 /*
432 * special case pass through for fixed
433 * input-only or output-only pins without
434 * function enum register association.
435 */
436 if (in_range && enum_id == range->force)
437 continue;
438 } else {
439 /*
440 * no other range exists, so this pin
441 * must then be of the function type.
442 *
443 * allow function type pins to select
444 * any combination of function/in/out
445 * in their MARK lists.
446 */
447 in_range = 1;
448 }
Magnus Damm42eed422008-10-22 18:29:17 +0900449 }
450
Magnus Damm2967dab2008-10-08 20:41:43 +0900451 if (!in_range)
452 continue;
453
Paul Mundtb3c185a2012-06-20 17:29:04 +0900454 if (get_config_reg(pfc, enum_id, &cr,
Magnus Dammad4a07f2011-12-14 01:00:46 +0900455 &field, &value, &cntp) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900456 goto out_err;
457
458 switch (cfg_mode) {
459 case GPIO_CFG_DRYRUN:
Magnus Damm18925e12011-12-14 01:00:55 +0900460 if (!*cntp ||
Paul Mundtb3c185a2012-06-20 17:29:04 +0900461 (read_config_reg(pfc, cr, field) != value))
Magnus Damm2967dab2008-10-08 20:41:43 +0900462 continue;
463 break;
464
465 case GPIO_CFG_REQ:
Paul Mundtb3c185a2012-06-20 17:29:04 +0900466 write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900467 *cntp = *cntp + 1;
468 break;
469
470 case GPIO_CFG_FREE:
471 *cntp = *cntp - 1;
472 break;
473 }
474 }
475
476 return 0;
477 out_err:
478 return -1;
479}
480
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100481static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900482{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100483 struct sh_pfc_platform_data *pdata = pdev->dev.platform_data;
484 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900485 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900486
Paul Mundt06d56312012-06-21 00:03:41 +0900487 /*
488 * Ensure that the type encoding fits
489 */
490 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
491
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100492 if (pdata == NULL)
493 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900494
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100495 pfc = devm_kzalloc(&pdev->dev, sizeof(pfc), GFP_KERNEL);
496 if (pfc == NULL)
497 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900498
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100499 pfc->pdata = pdata;
500 pfc->dev = &pdev->dev;
501
502 ret = pfc_ioremap(pfc);
503 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100504 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100505
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100506 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900507
Paul Mundtca5481c62012-07-10 12:08:14 +0900508 pinctrl_provide_dummies();
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100509 setup_data_regs(pfc);
Magnus Damm69edbba2008-12-25 18:17:34 +0900510
Paul Mundtca5481c62012-07-10 12:08:14 +0900511 /*
512 * Initialize pinctrl bindings first
513 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100514 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100515 if (unlikely(ret != 0))
Laurent Pinchartc9fa88e2012-12-15 23:50:49 +0100516 return ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900517
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100518#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900519 /*
520 * Then the GPIO chip
521 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100522 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100523 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900524 /*
525 * If the GPIO chip fails to come up we still leave the
526 * PFC state as it is, given that there are already
527 * extant users of it that have succeeded by this point.
528 */
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100529 pr_notice("failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900530 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100531#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900532
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100533 platform_set_drvdata(pdev, pfc);
534
535 pr_info("%s support registered\n", pdata->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900536
Paul Mundtb3c185a2012-06-20 17:29:04 +0900537 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900538}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100539
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100540static int sh_pfc_remove(struct platform_device *pdev)
541{
542 struct sh_pfc *pfc = platform_get_drvdata(pdev);
543
544#ifdef CONFIG_GPIO_SH_PFC
545 sh_pfc_unregister_gpiochip(pfc);
546#endif
547 sh_pfc_unregister_pinctrl(pfc);
548
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100549 platform_set_drvdata(pdev, NULL);
550
551 return 0;
552}
553
554static const struct platform_device_id sh_pfc_id_table[] = {
555 { "sh-pfc", 0 },
556 { },
557};
558MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
559
560static struct platform_driver sh_pfc_driver = {
561 .probe = sh_pfc_probe,
562 .remove = sh_pfc_remove,
563 .id_table = sh_pfc_id_table,
564 .driver = {
565 .name = DRV_NAME,
566 .owner = THIS_MODULE,
567 },
568};
569
570static struct platform_device sh_pfc_device = {
571 .name = DRV_NAME,
572 .id = -1,
573};
574
575int __init register_sh_pfc(struct sh_pfc_platform_data *pdata)
576{
577 int rc;
578
579 sh_pfc_device.dev.platform_data = pdata;
580
581 rc = platform_driver_register(&sh_pfc_driver);
582 if (likely(!rc)) {
583 rc = platform_device_register(&sh_pfc_device);
584 if (unlikely(rc))
585 platform_driver_unregister(&sh_pfc_driver);
586 }
587
588 return rc;
589}
590
591static void __exit sh_pfc_exit(void)
592{
593 platform_driver_unregister(&sh_pfc_driver);
594}
595module_exit(sh_pfc_exit);
596
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100597MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
598MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
599MODULE_LICENSE("GPL v2");