blob: 865d235612c5200a381805ff1b9763091f626e14 [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{
Laurent Pinchart70c8f012013-12-11 04:26:26 +010032 unsigned int num_windows = 0;
33 unsigned int num_irqs = 0;
34 struct sh_pfc_window *windows;
35 unsigned int *irqs = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090036 struct resource *res;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010037 unsigned int i;
Magnus Dammb0e10212011-12-09 12:14:27 +090038
Laurent Pinchart70c8f012013-12-11 04:26:26 +010039 /* Count the MEM and IRQ resources. */
40 for (i = 0; i < pdev->num_resources; ++i) {
41 switch (resource_type(&pdev->resource[i])) {
42 case IORESOURCE_MEM:
43 num_windows++;
44 break;
45
46 case IORESOURCE_IRQ:
47 num_irqs++;
48 break;
49 }
50 }
51
52 if (num_windows == 0)
Laurent Pinchartbee9f222013-02-16 23:39:07 +010053 return -EINVAL;
Laurent Pinchart973931a2012-12-15 23:50:55 +010054
Laurent Pinchart70c8f012013-12-11 04:26:26 +010055 /* Allocate memory windows and IRQs arrays. */
56 windows = devm_kzalloc(pfc->dev, num_windows * sizeof(*windows),
57 GFP_KERNEL);
58 if (windows == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010059 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090060
Laurent Pinchart70c8f012013-12-11 04:26:26 +010061 pfc->num_windows = num_windows;
62 pfc->windows = windows;
Laurent Pinchart973931a2012-12-15 23:50:55 +010063
Laurent Pinchart70c8f012013-12-11 04:26:26 +010064 if (num_irqs) {
65 irqs = devm_kzalloc(pfc->dev, num_irqs * sizeof(*irqs),
66 GFP_KERNEL);
67 if (irqs == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010068 return -ENOMEM;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010069
70 pfc->num_irqs = num_irqs;
71 pfc->irqs = irqs;
72 }
73
74 /* Fill them. */
75 for (i = 0, res = pdev->resource; i < pdev->num_resources; i++, res++) {
76 switch (resource_type(res)) {
77 case IORESOURCE_MEM:
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++;
84 break;
85
86 case IORESOURCE_IRQ:
87 *irqs++ = res->start;
88 break;
89 }
Magnus Dammb0e10212011-12-09 12:14:27 +090090 }
91
92 return 0;
Magnus Dammb0e10212011-12-09 12:14:27 +090093}
94
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010095static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
Magnus Dammb0e10212011-12-09 12:14:27 +090096{
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010097 struct sh_pfc_window *window;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010098 phys_addr_t address = reg;
Laurent Pinchartbee9f222013-02-16 23:39:07 +010099 unsigned int i;
Magnus Dammb0e10212011-12-09 12:14:27 +0900100
101 /* scan through physical windows and convert address */
Laurent Pinchartbee9f222013-02-16 23:39:07 +0100102 for (i = 0; i < pfc->num_windows; i++) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100103 window = pfc->windows + i;
Magnus Dammb0e10212011-12-09 12:14:27 +0900104
105 if (address < window->phys)
106 continue;
107
108 if (address >= (window->phys + window->size))
109 continue;
110
111 return window->virt + (address - window->phys);
112 }
113
Laurent Pinchartbee9f222013-02-16 23:39:07 +0100114 BUG();
Laurent Pinchart1960d582013-03-26 01:44:52 +0100115 return NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +0900116}
Magnus Damm2967dab2008-10-08 20:41:43 +0900117
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100118int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
Laurent Pinchart934cb022013-02-14 22:35:09 +0100119{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100120 unsigned int offset;
121 unsigned int i;
122
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200123 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
124 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100125
126 if (pin <= range->end)
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200127 return pin >= range->start
128 ? offset + pin - range->start : -1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100129
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200130 offset += range->end - range->start + 1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100131 }
132
Laurent Pinchartb705c052013-03-10 16:38:23 +0100133 return -EINVAL;
Laurent Pinchart934cb022013-02-14 22:35:09 +0100134}
135
Laurent Pinchart533743d2013-07-15 13:03:20 +0200136static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
Magnus Damm2967dab2008-10-08 20:41:43 +0900137{
138 if (enum_id < r->begin)
139 return 0;
140
141 if (enum_id > r->end)
142 return 0;
143
144 return 1;
145}
146
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100147u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
Magnus Damm32920942008-12-25 18:17:26 +0900148{
149 switch (reg_width) {
150 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900151 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900152 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900153 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900154 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900155 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900156 }
157
158 BUG();
159 return 0;
160}
161
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100162void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100163 u32 data)
Magnus Damm32920942008-12-25 18:17:26 +0900164{
165 switch (reg_width) {
166 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900167 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900168 return;
169 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900170 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900171 return;
172 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900173 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900174 return;
175 }
176
177 BUG();
178}
179
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100180static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100181 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100182 unsigned int in_pos,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100183 void __iomem **mapped_regp, u32 *maskp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100184 unsigned int *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900185{
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100186 unsigned int k;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900187
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100188 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900189
Magnus Dammf78a26f2011-12-14 01:01:05 +0900190 if (crp->field_width) {
191 *maskp = (1 << crp->field_width) - 1;
192 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
193 } else {
194 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
195 *posp = crp->reg_width;
196 for (k = 0; k <= in_pos; k++)
197 *posp -= crp->var_field_width[k];
198 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900199}
Magnus Damm2967dab2008-10-08 20:41:43 +0900200
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100201static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100202 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100203 unsigned int field, u32 value)
Magnus Damm18925e12011-12-14 01:00:55 +0900204{
205 void __iomem *mapped_reg;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100206 unsigned int pos;
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100207 u32 mask, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900208
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100209 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900210
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100211 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
Geert Uytterhoevendc700712015-03-12 11:09:13 +0100212 "r_width = %u, f_width = %u\n",
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100213 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900214
215 mask = ~(mask << pos);
216 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900217
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100218 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
Magnus Damme499ada2011-12-14 01:01:14 +0900219 data &= mask;
220 data |= value;
221
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100222 if (pfc->info->unlock_reg)
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100223 sh_pfc_write_raw_reg(
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100224 sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100225 ~data);
Magnus Damme499ada2011-12-14 01:01:14 +0900226
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100227 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900228}
229
Laurent Pinchart533743d2013-07-15 13:03:20 +0200230static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100231 const struct pinmux_cfg_reg **crp,
232 unsigned int *fieldp, u32 *valuep)
Magnus Damm2967dab2008-10-08 20:41:43 +0900233{
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100234 unsigned int k = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900235
Magnus Damm2967dab2008-10-08 20:41:43 +0900236 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100237 const struct pinmux_cfg_reg *config_reg =
238 pfc->info->cfg_regs + k;
239 unsigned int r_width = config_reg->reg_width;
240 unsigned int f_width = config_reg->field_width;
241 unsigned int curr_width;
242 unsigned int bit_pos;
243 unsigned int pos = 0;
244 unsigned int m = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900245
246 if (!r_width)
247 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900248
Magnus Dammf78a26f2011-12-14 01:01:05 +0900249 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100250 u32 ncomb;
251 u32 n;
252
Magnus Dammf78a26f2011-12-14 01:01:05 +0900253 if (f_width)
254 curr_width = f_width;
255 else
256 curr_width = config_reg->var_field_width[m];
257
258 ncomb = 1 << curr_width;
259 for (n = 0; n < ncomb; n++) {
260 if (config_reg->enum_ids[pos + n] == enum_id) {
261 *crp = config_reg;
262 *fieldp = m;
263 *valuep = n;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900264 return 0;
265 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900266 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900267 pos += ncomb;
268 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900269 }
270 k++;
271 }
272
Laurent Pinchartb705c052013-03-10 16:38:23 +0100273 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900274}
275
Laurent Pinchart533743d2013-07-15 13:03:20 +0200276static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
277 u16 *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900278{
Laurent Pinchart533743d2013-07-15 13:03:20 +0200279 const u16 *data = pfc->info->gpio_data;
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100280 unsigned int k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900281
Magnus Damm2967dab2008-10-08 20:41:43 +0900282 if (pos) {
283 *enum_idp = data[pos + 1];
284 return pos + 1;
285 }
286
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100287 for (k = 0; k < pfc->info->gpio_data_size; k++) {
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100288 if (data[k] == mark) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900289 *enum_idp = data[k + 1];
290 return k + 1;
291 }
292 }
293
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100294 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
295 mark);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100296 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900297}
298
Laurent Pinchart861601d2013-03-10 15:29:14 +0100299int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
Magnus Damm2967dab2008-10-08 20:41:43 +0900300{
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100301 const struct pinmux_range *range;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100302 int pos = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900303
304 switch (pinmux_type) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100305 case PINMUX_TYPE_GPIO:
Magnus Damm2967dab2008-10-08 20:41:43 +0900306 case PINMUX_TYPE_FUNCTION:
307 range = NULL;
308 break;
309
310 case PINMUX_TYPE_OUTPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100311 range = &pfc->info->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900312 break;
313
314 case PINMUX_TYPE_INPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100315 range = &pfc->info->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900316 break;
317
Magnus Damm2967dab2008-10-08 20:41:43 +0900318 default:
Laurent Pinchartb705c052013-03-10 16:38:23 +0100319 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900320 }
321
Laurent Pincharte3c470512013-03-10 17:30:25 +0100322 /* Iterate over all the configuration fields we need to update. */
Magnus Damm2967dab2008-10-08 20:41:43 +0900323 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100324 const struct pinmux_cfg_reg *cr;
325 unsigned int field;
326 u16 enum_id;
327 u32 value;
328 int in_range;
329 int ret;
330
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100331 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100332 if (pos < 0)
333 return pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900334
335 if (!enum_id)
336 break;
337
Laurent Pincharte3c470512013-03-10 17:30:25 +0100338 /* Check if the configuration field selects a function. If it
339 * doesn't, skip the field if it's not applicable to the
340 * requested pinmux type.
341 */
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100342 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000343 if (!in_range) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100344 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
345 /* Functions are allowed to modify all
346 * fields.
347 */
348 in_range = 1;
349 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
350 /* Input/output types can only modify fields
351 * that correspond to their respective ranges.
Magnus Damm50dd3142010-01-19 13:52:28 +0000352 */
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100353 in_range = sh_pfc_enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900354
Magnus Damm50dd3142010-01-19 13:52:28 +0000355 /*
356 * special case pass through for fixed
357 * input-only or output-only pins without
358 * function enum register association.
359 */
360 if (in_range && enum_id == range->force)
361 continue;
Magnus Damm50dd3142010-01-19 13:52:28 +0000362 }
Laurent Pincharte3c470512013-03-10 17:30:25 +0100363 /* GPIOs are only allowed to modify function fields. */
Magnus Damm42eed422008-10-22 18:29:17 +0900364 }
365
Magnus Damm2967dab2008-10-08 20:41:43 +0900366 if (!in_range)
367 continue;
368
Laurent Pinchartb705c052013-03-10 16:38:23 +0100369 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
370 if (ret < 0)
371 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900372
Laurent Pinchart861601d2013-03-10 15:29:14 +0100373 sh_pfc_write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900374 }
375
376 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900377}
378
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200379static int sh_pfc_init_ranges(struct sh_pfc *pfc)
380{
381 struct sh_pfc_pin_range *range;
382 unsigned int nr_ranges;
383 unsigned int i;
384
385 if (pfc->info->pins[0].pin == (u16)-1) {
386 /* Pin number -1 denotes that the SoC doesn't report pin numbers
387 * in its pin arrays yet. Consider the pin numbers range as
388 * continuous and allocate a single range.
389 */
390 pfc->nr_ranges = 1;
391 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
392 GFP_KERNEL);
393 if (pfc->ranges == NULL)
394 return -ENOMEM;
395
396 pfc->ranges->start = 0;
397 pfc->ranges->end = pfc->info->nr_pins - 1;
398 pfc->nr_gpio_pins = pfc->info->nr_pins;
399
400 return 0;
401 }
402
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200403 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
404 * be sorted by pin numbers, and pins without a GPIO port must come
405 * last.
406 */
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200407 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
408 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
409 nr_ranges++;
410 }
411
412 pfc->nr_ranges = nr_ranges;
413 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges) * nr_ranges,
414 GFP_KERNEL);
415 if (pfc->ranges == NULL)
416 return -ENOMEM;
417
418 range = pfc->ranges;
419 range->start = pfc->info->pins[0].pin;
420
421 for (i = 1; i < pfc->info->nr_pins; ++i) {
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200422 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
423 continue;
424
425 range->end = pfc->info->pins[i-1].pin;
426 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
427 pfc->nr_gpio_pins = range->end + 1;
428
429 range++;
430 range->start = pfc->info->pins[i].pin;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200431 }
432
433 range->end = pfc->info->pins[i-1].pin;
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200434 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
435 pfc->nr_gpio_pins = range->end + 1;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200436
437 return 0;
438}
439
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200440#ifdef CONFIG_OF
441static const struct of_device_id sh_pfc_of_table[] = {
Niklas Söderlund1e7d5d82015-01-25 14:49:52 +0100442#ifdef CONFIG_PINCTRL_PFC_EMEV2
443 {
444 .compatible = "renesas,pfc-emev2",
445 .data = &emev2_pinmux_info,
446 },
447#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200448#ifdef CONFIG_PINCTRL_PFC_R8A73A4
449 {
450 .compatible = "renesas,pfc-r8a73a4",
451 .data = &r8a73a4_pinmux_info,
452 },
453#endif
454#ifdef CONFIG_PINCTRL_PFC_R8A7740
455 {
456 .compatible = "renesas,pfc-r8a7740",
457 .data = &r8a7740_pinmux_info,
458 },
459#endif
460#ifdef CONFIG_PINCTRL_PFC_R8A7778
461 {
462 .compatible = "renesas,pfc-r8a7778",
463 .data = &r8a7778_pinmux_info,
464 },
465#endif
466#ifdef CONFIG_PINCTRL_PFC_R8A7779
467 {
468 .compatible = "renesas,pfc-r8a7779",
469 .data = &r8a7779_pinmux_info,
470 },
471#endif
472#ifdef CONFIG_PINCTRL_PFC_R8A7790
473 {
474 .compatible = "renesas,pfc-r8a7790",
475 .data = &r8a7790_pinmux_info,
476 },
477#endif
Hisashi Nakamura50884512013-10-17 06:46:05 +0900478#ifdef CONFIG_PINCTRL_PFC_R8A7791
479 {
480 .compatible = "renesas,pfc-r8a7791",
481 .data = &r8a7791_pinmux_info,
482 },
483#endif
Ulrich Hecht19e1e982015-05-12 11:13:19 +0200484#ifdef CONFIG_PINCTRL_PFC_R8A7793
485 {
486 .compatible = "renesas,pfc-r8a7793",
487 .data = &r8a7793_pinmux_info,
488 },
489#endif
Hisashi Nakamura43c44362015-06-06 01:34:48 +0300490#ifdef CONFIG_PINCTRL_PFC_R8A7794
491 {
492 .compatible = "renesas,pfc-r8a7794",
493 .data = &r8a7794_pinmux_info,
494 },
495#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200496#ifdef CONFIG_PINCTRL_PFC_SH73A0
497 {
498 .compatible = "renesas,pfc-sh73a0",
499 .data = &sh73a0_pinmux_info,
500 },
501#endif
502 { },
503};
504MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
505#endif
506
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100507static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900508{
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200509 const struct platform_device_id *platid = platform_get_device_id(pdev);
510#ifdef CONFIG_OF
511 struct device_node *np = pdev->dev.of_node;
512#endif
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100513 const struct sh_pfc_soc_info *info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100514 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900515 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900516
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200517#ifdef CONFIG_OF
518 if (np)
519 info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
520 else
521#endif
522 info = platid ? (const void *)platid->driver_data : NULL;
523
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100524 if (info == NULL)
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100525 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900526
Magnus Damm8c43fcc2013-02-14 21:23:47 +0900527 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100528 if (pfc == NULL)
529 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900530
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100531 pfc->info = info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100532 pfc->dev = &pdev->dev;
533
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100534 ret = sh_pfc_map_resources(pfc, pdev);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100535 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100536 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100537
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100538 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900539
Laurent Pinchart0c151062013-04-21 20:21:57 +0200540 if (info->ops && info->ops->init) {
541 ret = info->ops->init(pfc);
542 if (ret < 0)
543 return ret;
544 }
545
Paul Mundtca5481c62012-07-10 12:08:14 +0900546 pinctrl_provide_dummies();
Magnus Damm69edbba2008-12-25 18:17:34 +0900547
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200548 ret = sh_pfc_init_ranges(pfc);
549 if (ret < 0)
550 return ret;
551
Paul Mundtca5481c62012-07-10 12:08:14 +0900552 /*
553 * Initialize pinctrl bindings first
554 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100555 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100556 if (unlikely(ret != 0))
Laurent Pinchart0a332c92014-09-11 00:55:55 +0300557 return ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900558
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100559#ifdef CONFIG_GPIO_SH_PFC
Paul Mundtca5481c62012-07-10 12:08:14 +0900560 /*
561 * Then the GPIO chip
562 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100563 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100564 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900565 /*
566 * If the GPIO chip fails to come up we still leave the
567 * PFC state as it is, given that there are already
568 * extant users of it that have succeeded by this point.
569 */
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100570 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900571 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100572#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900573
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100574 platform_set_drvdata(pdev, pfc);
575
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100576 dev_info(pfc->dev, "%s support registered\n", info->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900577
Paul Mundtb3c185a2012-06-20 17:29:04 +0900578 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900579}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100580
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100581static int sh_pfc_remove(struct platform_device *pdev)
582{
583 struct sh_pfc *pfc = platform_get_drvdata(pdev);
584
585#ifdef CONFIG_GPIO_SH_PFC
586 sh_pfc_unregister_gpiochip(pfc);
587#endif
588 sh_pfc_unregister_pinctrl(pfc);
589
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100590 return 0;
591}
592
593static const struct platform_device_id sh_pfc_id_table[] = {
Laurent Pinchartd5b15212012-12-15 23:51:21 +0100594#ifdef CONFIG_PINCTRL_PFC_R8A7740
595 { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
596#endif
Kuninori Morimoto87f8c982013-04-12 05:37:20 +0000597#ifdef CONFIG_PINCTRL_PFC_R8A7778
598 { "pfc-r8a7778", (kernel_ulong_t)&r8a7778_pinmux_info },
599#endif
Laurent Pinchart881023d2012-12-15 23:51:22 +0100600#ifdef CONFIG_PINCTRL_PFC_R8A7779
601 { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
602#endif
Laurent Pinchartccda5522012-12-15 23:51:29 +0100603#ifdef CONFIG_PINCTRL_PFC_SH7203
604 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
605#endif
Laurent Pincharta8d42fc2012-12-15 23:51:30 +0100606#ifdef CONFIG_PINCTRL_PFC_SH7264
607 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
608#endif
Laurent Pinchartf5e811f2012-12-15 23:51:31 +0100609#ifdef CONFIG_PINCTRL_PFC_SH7269
610 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
611#endif
Laurent Pinchart5d5166d2012-12-15 23:51:24 +0100612#ifdef CONFIG_PINCTRL_PFC_SH73A0
613 { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
614#endif
Laurent Pinchart74cad602012-12-15 23:51:32 +0100615#ifdef CONFIG_PINCTRL_PFC_SH7720
616 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
617#endif
Laurent Pinchartf5e25ae2012-12-15 23:51:33 +0100618#ifdef CONFIG_PINCTRL_PFC_SH7722
619 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
620#endif
Laurent Pinchartd05afa02012-12-15 23:51:34 +0100621#ifdef CONFIG_PINCTRL_PFC_SH7723
622 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
623#endif
Laurent Pinchart0ff25ba2012-12-15 23:51:35 +0100624#ifdef CONFIG_PINCTRL_PFC_SH7724
625 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
626#endif
Laurent Pinchartac1ebc22012-12-15 23:51:36 +0100627#ifdef CONFIG_PINCTRL_PFC_SH7734
628 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
629#endif
Laurent Pinchart0bb92672012-12-15 23:51:37 +0100630#ifdef CONFIG_PINCTRL_PFC_SH7757
631 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
632#endif
Laurent Pincharta56398e2012-12-15 23:51:38 +0100633#ifdef CONFIG_PINCTRL_PFC_SH7785
634 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
635#endif
Laurent Pinchartd2a31bd2012-12-15 23:51:39 +0100636#ifdef CONFIG_PINCTRL_PFC_SH7786
637 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
638#endif
Laurent Pinchartd5d9a812012-12-15 23:51:40 +0100639#ifdef CONFIG_PINCTRL_PFC_SHX3
640 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
641#endif
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100642 { "sh-pfc", 0 },
643 { },
644};
645MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
646
647static struct platform_driver sh_pfc_driver = {
648 .probe = sh_pfc_probe,
649 .remove = sh_pfc_remove,
650 .id_table = sh_pfc_id_table,
651 .driver = {
652 .name = DRV_NAME,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200653 .of_match_table = of_match_ptr(sh_pfc_of_table),
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100654 },
655};
656
Laurent Pinchart40ee6fc2012-12-15 23:50:54 +0100657static int __init sh_pfc_init(void)
658{
659 return platform_driver_register(&sh_pfc_driver);
660}
661postcore_initcall(sh_pfc_init);
662
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100663static void __exit sh_pfc_exit(void)
664{
665 platform_driver_unregister(&sh_pfc_driver);
666}
667module_exit(sh_pfc_exit);
668
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100669MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
670MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
671MODULE_LICENSE("GPL v2");