blob: 181ea98a63b7ab02d367581b4027997ea0f6a574 [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"
Paul Mundtb72421d2010-10-04 03:54:56 +090013
Magnus Damm2967dab2008-10-08 20:41:43 +090014#include <linux/bitops.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010015#include <linux/err.h>
16#include <linux/errno.h>
17#include <linux/io.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090018#include <linux/ioport.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010019#include <linux/kernel.h>
20#include <linux/module.h>
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020021#include <linux/of.h>
22#include <linux/of_device.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090023#include <linux/pinctrl/machine.h>
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010024#include <linux/platform_device.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010025#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 Pinchart70c8f012013-12-11 04:26:26 +010029static int sh_pfc_map_resources(struct sh_pfc *pfc,
30 struct platform_device *pdev)
Magnus Dammb0e10212011-12-09 12:14:27 +090031{
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020032 unsigned int num_windows, num_irqs;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010033 struct sh_pfc_window *windows;
34 unsigned int *irqs = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090035 struct resource *res;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010036 unsigned int i;
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020037 int irq;
Magnus Dammb0e10212011-12-09 12:14:27 +090038
Laurent Pinchart70c8f012013-12-11 04:26:26 +010039 /* Count the MEM and IRQ resources. */
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020040 for (num_windows = 0;; num_windows++) {
41 res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
42 if (!res)
Laurent Pinchart70c8f012013-12-11 04:26:26 +010043 break;
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020044 }
45 for (num_irqs = 0;; num_irqs++) {
46 irq = platform_get_irq(pdev, num_irqs);
47 if (irq == -EPROBE_DEFER)
48 return irq;
49 if (irq < 0)
Laurent Pinchart70c8f012013-12-11 04:26:26 +010050 break;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010051 }
52
53 if (num_windows == 0)
Laurent Pinchartbee9f222013-02-16 23:39:07 +010054 return -EINVAL;
Laurent Pinchart973931a2012-12-15 23:50:55 +010055
Laurent Pinchart70c8f012013-12-11 04:26:26 +010056 /* Allocate memory windows and IRQs arrays. */
57 windows = devm_kzalloc(pfc->dev, num_windows * sizeof(*windows),
58 GFP_KERNEL);
59 if (windows == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010060 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090061
Laurent Pinchart70c8f012013-12-11 04:26:26 +010062 pfc->num_windows = num_windows;
63 pfc->windows = windows;
Laurent Pinchart973931a2012-12-15 23:50:55 +010064
Laurent Pinchart70c8f012013-12-11 04:26:26 +010065 if (num_irqs) {
66 irqs = devm_kzalloc(pfc->dev, num_irqs * sizeof(*irqs),
67 GFP_KERNEL);
68 if (irqs == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010069 return -ENOMEM;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010070
71 pfc->num_irqs = num_irqs;
72 pfc->irqs = irqs;
73 }
74
75 /* Fill them. */
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020076 for (i = 0; i < num_windows; i++) {
77 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
78 windows->phys = res->start;
79 windows->size = resource_size(res);
80 windows->virt = devm_ioremap_resource(pfc->dev, res);
81 if (IS_ERR(windows->virt))
82 return -ENOMEM;
83 windows++;
Magnus Dammb0e10212011-12-09 12:14:27 +090084 }
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020085 for (i = 0; i < num_irqs; i++)
86 *irqs++ = platform_get_irq(pdev, i);
Magnus Dammb0e10212011-12-09 12:14:27 +090087
88 return 0;
Magnus Dammb0e10212011-12-09 12:14:27 +090089}
90
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010091static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
Magnus Dammb0e10212011-12-09 12:14:27 +090092{
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010093 struct sh_pfc_window *window;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010094 phys_addr_t address = reg;
Laurent Pinchartbee9f222013-02-16 23:39:07 +010095 unsigned int i;
Magnus Dammb0e10212011-12-09 12:14:27 +090096
97 /* scan through physical windows and convert address */
Laurent Pinchartbee9f222013-02-16 23:39:07 +010098 for (i = 0; i < pfc->num_windows; i++) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +010099 window = pfc->windows + i;
Magnus Dammb0e10212011-12-09 12:14:27 +0900100
101 if (address < window->phys)
102 continue;
103
104 if (address >= (window->phys + window->size))
105 continue;
106
107 return window->virt + (address - window->phys);
108 }
109
Laurent Pinchartbee9f222013-02-16 23:39:07 +0100110 BUG();
Laurent Pinchart1960d582013-03-26 01:44:52 +0100111 return NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +0900112}
Magnus Damm2967dab2008-10-08 20:41:43 +0900113
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100114int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
Laurent Pinchart934cb022013-02-14 22:35:09 +0100115{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100116 unsigned int offset;
117 unsigned int i;
118
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200119 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
120 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100121
122 if (pin <= range->end)
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200123 return pin >= range->start
124 ? offset + pin - range->start : -1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100125
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200126 offset += range->end - range->start + 1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100127 }
128
Laurent Pinchartb705c052013-03-10 16:38:23 +0100129 return -EINVAL;
Laurent Pinchart934cb022013-02-14 22:35:09 +0100130}
131
Laurent Pinchart533743d2013-07-15 13:03:20 +0200132static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
Magnus Damm2967dab2008-10-08 20:41:43 +0900133{
134 if (enum_id < r->begin)
135 return 0;
136
137 if (enum_id > r->end)
138 return 0;
139
140 return 1;
141}
142
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100143u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
Magnus Damm32920942008-12-25 18:17:26 +0900144{
145 switch (reg_width) {
146 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900147 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900148 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900149 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900150 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900151 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900152 }
153
154 BUG();
155 return 0;
156}
157
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100158void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100159 u32 data)
Magnus Damm32920942008-12-25 18:17:26 +0900160{
161 switch (reg_width) {
162 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900163 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900164 return;
165 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900166 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900167 return;
168 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900169 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900170 return;
171 }
172
173 BUG();
174}
175
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100176static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100177 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100178 unsigned int in_pos,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100179 void __iomem **mapped_regp, u32 *maskp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100180 unsigned int *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900181{
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100182 unsigned int k;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900183
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100184 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900185
Magnus Dammf78a26f2011-12-14 01:01:05 +0900186 if (crp->field_width) {
187 *maskp = (1 << crp->field_width) - 1;
188 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
189 } else {
190 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
191 *posp = crp->reg_width;
192 for (k = 0; k <= in_pos; k++)
193 *posp -= crp->var_field_width[k];
194 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900195}
Magnus Damm2967dab2008-10-08 20:41:43 +0900196
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100197static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100198 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100199 unsigned int field, u32 value)
Magnus Damm18925e12011-12-14 01:00:55 +0900200{
201 void __iomem *mapped_reg;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100202 unsigned int pos;
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100203 u32 mask, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900204
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100205 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900206
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100207 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
Geert Uytterhoevendc700712015-03-12 11:09:13 +0100208 "r_width = %u, f_width = %u\n",
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100209 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900210
211 mask = ~(mask << pos);
212 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900213
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100214 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
Magnus Damme499ada2011-12-14 01:01:14 +0900215 data &= mask;
216 data |= value;
217
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100218 if (pfc->info->unlock_reg)
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100219 sh_pfc_write_raw_reg(
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100220 sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100221 ~data);
Magnus Damme499ada2011-12-14 01:01:14 +0900222
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100223 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900224}
225
Laurent Pinchart533743d2013-07-15 13:03:20 +0200226static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100227 const struct pinmux_cfg_reg **crp,
228 unsigned int *fieldp, u32 *valuep)
Magnus Damm2967dab2008-10-08 20:41:43 +0900229{
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100230 unsigned int k = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900231
Magnus Damm2967dab2008-10-08 20:41:43 +0900232 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100233 const struct pinmux_cfg_reg *config_reg =
234 pfc->info->cfg_regs + k;
235 unsigned int r_width = config_reg->reg_width;
236 unsigned int f_width = config_reg->field_width;
237 unsigned int curr_width;
238 unsigned int bit_pos;
239 unsigned int pos = 0;
240 unsigned int m = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900241
242 if (!r_width)
243 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900244
Magnus Dammf78a26f2011-12-14 01:01:05 +0900245 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100246 u32 ncomb;
247 u32 n;
248
Magnus Dammf78a26f2011-12-14 01:01:05 +0900249 if (f_width)
250 curr_width = f_width;
251 else
252 curr_width = config_reg->var_field_width[m];
253
254 ncomb = 1 << curr_width;
255 for (n = 0; n < ncomb; n++) {
256 if (config_reg->enum_ids[pos + n] == enum_id) {
257 *crp = config_reg;
258 *fieldp = m;
259 *valuep = n;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900260 return 0;
261 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900262 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900263 pos += ncomb;
264 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900265 }
266 k++;
267 }
268
Laurent Pinchartb705c052013-03-10 16:38:23 +0100269 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900270}
271
Laurent Pinchart533743d2013-07-15 13:03:20 +0200272static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
273 u16 *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900274{
Geert Uytterhoevenb8b47d62015-09-21 16:27:23 +0200275 const u16 *data = pfc->info->pinmux_data;
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100276 unsigned int k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900277
Magnus Damm2967dab2008-10-08 20:41:43 +0900278 if (pos) {
279 *enum_idp = data[pos + 1];
280 return pos + 1;
281 }
282
Geert Uytterhoevenb8b47d62015-09-21 16:27:23 +0200283 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100284 if (data[k] == mark) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900285 *enum_idp = data[k + 1];
286 return k + 1;
287 }
288 }
289
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100290 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
291 mark);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100292 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900293}
294
Laurent Pinchart861601d2013-03-10 15:29:14 +0100295int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
Magnus Damm2967dab2008-10-08 20:41:43 +0900296{
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100297 const struct pinmux_range *range;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100298 int pos = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900299
300 switch (pinmux_type) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100301 case PINMUX_TYPE_GPIO:
Magnus Damm2967dab2008-10-08 20:41:43 +0900302 case PINMUX_TYPE_FUNCTION:
303 range = NULL;
304 break;
305
306 case PINMUX_TYPE_OUTPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100307 range = &pfc->info->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900308 break;
309
310 case PINMUX_TYPE_INPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100311 range = &pfc->info->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900312 break;
313
Magnus Damm2967dab2008-10-08 20:41:43 +0900314 default:
Laurent Pinchartb705c052013-03-10 16:38:23 +0100315 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900316 }
317
Laurent Pincharte3c470512013-03-10 17:30:25 +0100318 /* Iterate over all the configuration fields we need to update. */
Magnus Damm2967dab2008-10-08 20:41:43 +0900319 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100320 const struct pinmux_cfg_reg *cr;
321 unsigned int field;
322 u16 enum_id;
323 u32 value;
324 int in_range;
325 int ret;
326
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100327 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100328 if (pos < 0)
329 return pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900330
331 if (!enum_id)
332 break;
333
Laurent Pincharte3c470512013-03-10 17:30:25 +0100334 /* Check if the configuration field selects a function. If it
335 * doesn't, skip the field if it's not applicable to the
336 * requested pinmux type.
337 */
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100338 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000339 if (!in_range) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100340 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
341 /* Functions are allowed to modify all
342 * fields.
343 */
344 in_range = 1;
345 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
346 /* Input/output types can only modify fields
347 * that correspond to their respective ranges.
Magnus Damm50dd3142010-01-19 13:52:28 +0000348 */
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100349 in_range = sh_pfc_enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900350
Magnus Damm50dd3142010-01-19 13:52:28 +0000351 /*
352 * special case pass through for fixed
353 * input-only or output-only pins without
354 * function enum register association.
355 */
356 if (in_range && enum_id == range->force)
357 continue;
Magnus Damm50dd3142010-01-19 13:52:28 +0000358 }
Laurent Pincharte3c470512013-03-10 17:30:25 +0100359 /* GPIOs are only allowed to modify function fields. */
Magnus Damm42eed422008-10-22 18:29:17 +0900360 }
361
Magnus Damm2967dab2008-10-08 20:41:43 +0900362 if (!in_range)
363 continue;
364
Laurent Pinchartb705c052013-03-10 16:38:23 +0100365 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
366 if (ret < 0)
367 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900368
Laurent Pinchart861601d2013-03-10 15:29:14 +0100369 sh_pfc_write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900370 }
371
372 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900373}
374
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200375static int sh_pfc_init_ranges(struct sh_pfc *pfc)
376{
377 struct sh_pfc_pin_range *range;
378 unsigned int nr_ranges;
379 unsigned int i;
380
381 if (pfc->info->pins[0].pin == (u16)-1) {
382 /* Pin number -1 denotes that the SoC doesn't report pin numbers
383 * in its pin arrays yet. Consider the pin numbers range as
384 * continuous and allocate a single range.
385 */
386 pfc->nr_ranges = 1;
387 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
388 GFP_KERNEL);
389 if (pfc->ranges == NULL)
390 return -ENOMEM;
391
392 pfc->ranges->start = 0;
393 pfc->ranges->end = pfc->info->nr_pins - 1;
394 pfc->nr_gpio_pins = pfc->info->nr_pins;
395
396 return 0;
397 }
398
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200399 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
400 * be sorted by pin numbers, and pins without a GPIO port must come
401 * last.
402 */
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200403 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
404 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
405 nr_ranges++;
406 }
407
408 pfc->nr_ranges = nr_ranges;
409 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges) * nr_ranges,
410 GFP_KERNEL);
411 if (pfc->ranges == NULL)
412 return -ENOMEM;
413
414 range = pfc->ranges;
415 range->start = pfc->info->pins[0].pin;
416
417 for (i = 1; i < pfc->info->nr_pins; ++i) {
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200418 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
419 continue;
420
421 range->end = pfc->info->pins[i-1].pin;
422 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
423 pfc->nr_gpio_pins = range->end + 1;
424
425 range++;
426 range->start = pfc->info->pins[i].pin;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200427 }
428
429 range->end = pfc->info->pins[i-1].pin;
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200430 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
431 pfc->nr_gpio_pins = range->end + 1;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200432
433 return 0;
434}
435
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200436#ifdef CONFIG_OF
437static const struct of_device_id sh_pfc_of_table[] = {
Niklas Söderlund1e7d5d82015-01-25 14:49:52 +0100438#ifdef CONFIG_PINCTRL_PFC_EMEV2
439 {
440 .compatible = "renesas,pfc-emev2",
441 .data = &emev2_pinmux_info,
442 },
443#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200444#ifdef CONFIG_PINCTRL_PFC_R8A73A4
445 {
446 .compatible = "renesas,pfc-r8a73a4",
447 .data = &r8a73a4_pinmux_info,
448 },
449#endif
450#ifdef CONFIG_PINCTRL_PFC_R8A7740
451 {
452 .compatible = "renesas,pfc-r8a7740",
453 .data = &r8a7740_pinmux_info,
454 },
455#endif
456#ifdef CONFIG_PINCTRL_PFC_R8A7778
457 {
458 .compatible = "renesas,pfc-r8a7778",
459 .data = &r8a7778_pinmux_info,
460 },
461#endif
462#ifdef CONFIG_PINCTRL_PFC_R8A7779
463 {
464 .compatible = "renesas,pfc-r8a7779",
465 .data = &r8a7779_pinmux_info,
466 },
467#endif
468#ifdef CONFIG_PINCTRL_PFC_R8A7790
469 {
470 .compatible = "renesas,pfc-r8a7790",
471 .data = &r8a7790_pinmux_info,
472 },
473#endif
Hisashi Nakamura50884512013-10-17 06:46:05 +0900474#ifdef CONFIG_PINCTRL_PFC_R8A7791
475 {
476 .compatible = "renesas,pfc-r8a7791",
477 .data = &r8a7791_pinmux_info,
478 },
479#endif
Ulrich Hecht19e1e982015-05-12 11:13:19 +0200480#ifdef CONFIG_PINCTRL_PFC_R8A7793
481 {
482 .compatible = "renesas,pfc-r8a7793",
483 .data = &r8a7793_pinmux_info,
484 },
485#endif
Hisashi Nakamura43c44362015-06-06 01:34:48 +0300486#ifdef CONFIG_PINCTRL_PFC_R8A7794
487 {
488 .compatible = "renesas,pfc-r8a7794",
489 .data = &r8a7794_pinmux_info,
490 },
491#endif
Takeshi Kihara0b0ffc92015-09-03 02:51:49 +0000492#ifdef CONFIG_PINCTRL_PFC_R8A7795
493 {
494 .compatible = "renesas,pfc-r8a7795",
495 .data = &r8a7795_pinmux_info,
496 },
497#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200498#ifdef CONFIG_PINCTRL_PFC_SH73A0
499 {
500 .compatible = "renesas,pfc-sh73a0",
501 .data = &sh73a0_pinmux_info,
502 },
503#endif
504 { },
505};
506MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
507#endif
508
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100509static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900510{
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200511 const struct platform_device_id *platid = platform_get_device_id(pdev);
512#ifdef CONFIG_OF
513 struct device_node *np = pdev->dev.of_node;
514#endif
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100515 const struct sh_pfc_soc_info *info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100516 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900517 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900518
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200519#ifdef CONFIG_OF
520 if (np)
521 info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
522 else
523#endif
524 info = platid ? (const void *)platid->driver_data : NULL;
525
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100526 if (info == NULL)
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100527 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900528
Magnus Damm8c43fcc2013-02-14 21:23:47 +0900529 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100530 if (pfc == NULL)
531 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900532
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100533 pfc->info = info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100534 pfc->dev = &pdev->dev;
535
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100536 ret = sh_pfc_map_resources(pfc, pdev);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100537 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100538 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100539
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100540 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900541
Laurent Pinchart0c151062013-04-21 20:21:57 +0200542 if (info->ops && info->ops->init) {
543 ret = info->ops->init(pfc);
544 if (ret < 0)
545 return ret;
546 }
547
Paul Mundtca5481c62012-07-10 12:08:14 +0900548 pinctrl_provide_dummies();
Magnus Damm69edbba2008-12-25 18:17:34 +0900549
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200550 ret = sh_pfc_init_ranges(pfc);
551 if (ret < 0)
552 return ret;
553
Paul Mundtca5481c62012-07-10 12:08:14 +0900554 /*
555 * Initialize pinctrl bindings first
556 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100557 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100558 if (unlikely(ret != 0))
Laurent Pinchart0a332c92014-09-11 00:55:55 +0300559 return ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900560
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100561#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900562 /*
563 * Then the GPIO chip
564 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100565 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100566 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900567 /*
568 * If the GPIO chip fails to come up we still leave the
569 * PFC state as it is, given that there are already
570 * extant users of it that have succeeded by this point.
571 */
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100572 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900573 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100574#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900575
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100576 platform_set_drvdata(pdev, pfc);
577
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100578 dev_info(pfc->dev, "%s support registered\n", info->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900579
Paul Mundtb3c185a2012-06-20 17:29:04 +0900580 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900581}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100582
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100583static int sh_pfc_remove(struct platform_device *pdev)
584{
585 struct sh_pfc *pfc = platform_get_drvdata(pdev);
586
587#ifdef CONFIG_GPIO_SH_PFC
588 sh_pfc_unregister_gpiochip(pfc);
589#endif
590 sh_pfc_unregister_pinctrl(pfc);
591
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100592 return 0;
593}
594
595static const struct platform_device_id sh_pfc_id_table[] = {
Laurent Pinchartccda5522012-12-15 23:51:29 +0100596#ifdef CONFIG_PINCTRL_PFC_SH7203
597 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
598#endif
Laurent Pincharta8d42fc2012-12-15 23:51:30 +0100599#ifdef CONFIG_PINCTRL_PFC_SH7264
600 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
601#endif
Laurent Pinchartf5e811f2012-12-15 23:51:31 +0100602#ifdef CONFIG_PINCTRL_PFC_SH7269
603 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
604#endif
Laurent Pinchart74cad602012-12-15 23:51:32 +0100605#ifdef CONFIG_PINCTRL_PFC_SH7720
606 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
607#endif
Laurent Pinchartf5e25ae2012-12-15 23:51:33 +0100608#ifdef CONFIG_PINCTRL_PFC_SH7722
609 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
610#endif
Laurent Pinchartd05afa02012-12-15 23:51:34 +0100611#ifdef CONFIG_PINCTRL_PFC_SH7723
612 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
613#endif
Laurent Pinchart0ff25ba2012-12-15 23:51:35 +0100614#ifdef CONFIG_PINCTRL_PFC_SH7724
615 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
616#endif
Laurent Pinchartac1ebc22012-12-15 23:51:36 +0100617#ifdef CONFIG_PINCTRL_PFC_SH7734
618 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
619#endif
Laurent Pinchart0bb92672012-12-15 23:51:37 +0100620#ifdef CONFIG_PINCTRL_PFC_SH7757
621 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
622#endif
Laurent Pincharta56398e2012-12-15 23:51:38 +0100623#ifdef CONFIG_PINCTRL_PFC_SH7785
624 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
625#endif
Laurent Pinchartd2a31bd2012-12-15 23:51:39 +0100626#ifdef CONFIG_PINCTRL_PFC_SH7786
627 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
628#endif
Laurent Pinchartd5d9a812012-12-15 23:51:40 +0100629#ifdef CONFIG_PINCTRL_PFC_SHX3
630 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
631#endif
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100632 { "sh-pfc", 0 },
633 { },
634};
635MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
636
637static struct platform_driver sh_pfc_driver = {
638 .probe = sh_pfc_probe,
639 .remove = sh_pfc_remove,
640 .id_table = sh_pfc_id_table,
641 .driver = {
642 .name = DRV_NAME,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200643 .of_match_table = of_match_ptr(sh_pfc_of_table),
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100644 },
645};
646
Laurent Pinchart40ee6fc2012-12-15 23:50:54 +0100647static int __init sh_pfc_init(void)
648{
649 return platform_driver_register(&sh_pfc_driver);
650}
651postcore_initcall(sh_pfc_init);
652
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100653static void __exit sh_pfc_exit(void)
654{
655 platform_driver_unregister(&sh_pfc_driver);
656}
657module_exit(sh_pfc_exit);
658
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100659MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
660MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
661MODULE_LICENSE("GPL v2");