blob: 4a5a0feb931bf43ccdb768f142a45e34ace30823 [file] [log] [blame]
Magnus Damm2967dab2008-10-08 20:41:43 +09001/*
Paul Gortmakera43647b2016-02-29 15:48:40 -05002 * Pin Control and GPIO driver for SuperH Pin Function Controller.
3 *
4 * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
Magnus Damm2967dab2008-10-08 20:41:43 +09005 *
6 * Copyright (C) 2008 Magnus Damm
Paul Mundtb3c185a2012-06-20 17:29:04 +09007 * Copyright (C) 2009 - 2012 Paul Mundt
Magnus Damm2967dab2008-10-08 20:41:43 +09008 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
11 * for more details.
12 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010013
14#define DRV_NAME "sh-pfc"
Paul Mundtb72421d2010-10-04 03:54:56 +090015
Magnus Damm2967dab2008-10-08 20:41:43 +090016#include <linux/bitops.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010017#include <linux/err.h>
18#include <linux/errno.h>
19#include <linux/io.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090020#include <linux/ioport.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010021#include <linux/kernel.h>
Paul Gortmakera43647b2016-02-29 15:48:40 -050022#include <linux/init.h>
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020023#include <linux/of.h>
24#include <linux/of_device.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090025#include <linux/pinctrl/machine.h>
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010026#include <linux/platform_device.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010027#include <linux/slab.h>
Magnus Dammb0e10212011-12-09 12:14:27 +090028
Laurent Pinchartf9165132012-12-15 23:50:44 +010029#include "core.h"
30
Laurent Pinchart70c8f012013-12-11 04:26:26 +010031static int sh_pfc_map_resources(struct sh_pfc *pfc,
32 struct platform_device *pdev)
Magnus Dammb0e10212011-12-09 12:14:27 +090033{
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020034 unsigned int num_windows, num_irqs;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010035 struct sh_pfc_window *windows;
36 unsigned int *irqs = NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +090037 struct resource *res;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010038 unsigned int i;
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020039 int irq;
Magnus Dammb0e10212011-12-09 12:14:27 +090040
Laurent Pinchart70c8f012013-12-11 04:26:26 +010041 /* Count the MEM and IRQ resources. */
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020042 for (num_windows = 0;; num_windows++) {
43 res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
44 if (!res)
Laurent Pinchart70c8f012013-12-11 04:26:26 +010045 break;
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020046 }
47 for (num_irqs = 0;; num_irqs++) {
48 irq = platform_get_irq(pdev, num_irqs);
49 if (irq == -EPROBE_DEFER)
50 return irq;
51 if (irq < 0)
Laurent Pinchart70c8f012013-12-11 04:26:26 +010052 break;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010053 }
54
55 if (num_windows == 0)
Laurent Pinchartbee9f222013-02-16 23:39:07 +010056 return -EINVAL;
Laurent Pinchart973931a2012-12-15 23:50:55 +010057
Laurent Pinchart70c8f012013-12-11 04:26:26 +010058 /* Allocate memory windows and IRQs arrays. */
59 windows = devm_kzalloc(pfc->dev, num_windows * sizeof(*windows),
60 GFP_KERNEL);
61 if (windows == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010062 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +090063
Laurent Pinchart70c8f012013-12-11 04:26:26 +010064 pfc->num_windows = num_windows;
65 pfc->windows = windows;
Laurent Pinchart973931a2012-12-15 23:50:55 +010066
Laurent Pinchart70c8f012013-12-11 04:26:26 +010067 if (num_irqs) {
68 irqs = devm_kzalloc(pfc->dev, num_irqs * sizeof(*irqs),
69 GFP_KERNEL);
70 if (irqs == NULL)
Laurent Pinchart1724acf2012-12-15 23:50:48 +010071 return -ENOMEM;
Laurent Pinchart70c8f012013-12-11 04:26:26 +010072
73 pfc->num_irqs = num_irqs;
74 pfc->irqs = irqs;
75 }
76
77 /* Fill them. */
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020078 for (i = 0; i < num_windows; i++) {
79 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
80 windows->phys = res->start;
81 windows->size = resource_size(res);
82 windows->virt = devm_ioremap_resource(pfc->dev, res);
83 if (IS_ERR(windows->virt))
84 return -ENOMEM;
85 windows++;
Magnus Dammb0e10212011-12-09 12:14:27 +090086 }
Geert Uytterhoevenc7977ec2015-06-25 11:39:53 +020087 for (i = 0; i < num_irqs; i++)
88 *irqs++ = platform_get_irq(pdev, i);
Magnus Dammb0e10212011-12-09 12:14:27 +090089
90 return 0;
Magnus Dammb0e10212011-12-09 12:14:27 +090091}
92
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010093static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
Magnus Dammb0e10212011-12-09 12:14:27 +090094{
Laurent Pinchart4aeacd52012-12-15 23:50:53 +010095 struct sh_pfc_window *window;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010096 phys_addr_t address = reg;
Laurent Pinchartbee9f222013-02-16 23:39:07 +010097 unsigned int i;
Magnus Dammb0e10212011-12-09 12:14:27 +090098
99 /* scan through physical windows and convert address */
Laurent Pinchartbee9f222013-02-16 23:39:07 +0100100 for (i = 0; i < pfc->num_windows; i++) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100101 window = pfc->windows + i;
Magnus Dammb0e10212011-12-09 12:14:27 +0900102
103 if (address < window->phys)
104 continue;
105
106 if (address >= (window->phys + window->size))
107 continue;
108
109 return window->virt + (address - window->phys);
110 }
111
Laurent Pinchartbee9f222013-02-16 23:39:07 +0100112 BUG();
Laurent Pinchart1960d582013-03-26 01:44:52 +0100113 return NULL;
Magnus Dammb0e10212011-12-09 12:14:27 +0900114}
Magnus Damm2967dab2008-10-08 20:41:43 +0900115
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100116int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
Laurent Pinchart934cb022013-02-14 22:35:09 +0100117{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100118 unsigned int offset;
119 unsigned int i;
120
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200121 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
122 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100123
124 if (pin <= range->end)
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200125 return pin >= range->start
126 ? offset + pin - range->start : -1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100127
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200128 offset += range->end - range->start + 1;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100129 }
130
Laurent Pinchartb705c052013-03-10 16:38:23 +0100131 return -EINVAL;
Laurent Pinchart934cb022013-02-14 22:35:09 +0100132}
133
Laurent Pinchart533743d2013-07-15 13:03:20 +0200134static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
Magnus Damm2967dab2008-10-08 20:41:43 +0900135{
136 if (enum_id < r->begin)
137 return 0;
138
139 if (enum_id > r->end)
140 return 0;
141
142 return 1;
143}
144
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100145u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
Magnus Damm32920942008-12-25 18:17:26 +0900146{
147 switch (reg_width) {
148 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900149 return ioread8(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900150 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900151 return ioread16(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900152 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900153 return ioread32(mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900154 }
155
156 BUG();
157 return 0;
158}
159
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100160void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100161 u32 data)
Magnus Damm32920942008-12-25 18:17:26 +0900162{
163 switch (reg_width) {
164 case 8:
Magnus Dammb0e10212011-12-09 12:14:27 +0900165 iowrite8(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900166 return;
167 case 16:
Magnus Dammb0e10212011-12-09 12:14:27 +0900168 iowrite16(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900169 return;
170 case 32:
Magnus Dammb0e10212011-12-09 12:14:27 +0900171 iowrite32(data, mapped_reg);
Magnus Damm32920942008-12-25 18:17:26 +0900172 return;
173 }
174
175 BUG();
176}
177
Laurent Pinchart3caa7d82016-03-23 16:06:00 +0200178u32 sh_pfc_read_reg(struct sh_pfc *pfc, u32 reg, unsigned int width)
179{
180 return sh_pfc_read_raw_reg(sh_pfc_phys_to_virt(pfc, reg), width);
181}
182
183void sh_pfc_write_reg(struct sh_pfc *pfc, u32 reg, unsigned int width, u32 data)
184{
185 if (pfc->info->unlock_reg)
186 sh_pfc_write_raw_reg(
187 sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
188 ~data);
189
190 sh_pfc_write_raw_reg(sh_pfc_phys_to_virt(pfc, reg), width, data);
191}
192
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100193static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100194 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100195 unsigned int in_pos,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100196 void __iomem **mapped_regp, u32 *maskp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100197 unsigned int *posp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900198{
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100199 unsigned int k;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900200
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100201 *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900202
Magnus Dammf78a26f2011-12-14 01:01:05 +0900203 if (crp->field_width) {
204 *maskp = (1 << crp->field_width) - 1;
205 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
206 } else {
207 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
208 *posp = crp->reg_width;
209 for (k = 0; k <= in_pos; k++)
210 *posp -= crp->var_field_width[k];
211 }
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900212}
Magnus Damm2967dab2008-10-08 20:41:43 +0900213
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100214static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100215 const struct pinmux_cfg_reg *crp,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100216 unsigned int field, u32 value)
Magnus Damm18925e12011-12-14 01:00:55 +0900217{
218 void __iomem *mapped_reg;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100219 unsigned int pos;
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100220 u32 mask, data;
Magnus Damm18925e12011-12-14 01:00:55 +0900221
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100222 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
Magnus Damm18925e12011-12-14 01:00:55 +0900223
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100224 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
Geert Uytterhoevendc700712015-03-12 11:09:13 +0100225 "r_width = %u, f_width = %u\n",
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100226 crp->reg, value, field, crp->reg_width, crp->field_width);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900227
228 mask = ~(mask << pos);
229 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900230
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100231 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
Magnus Damme499ada2011-12-14 01:01:14 +0900232 data &= mask;
233 data |= value;
234
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100235 if (pfc->info->unlock_reg)
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100236 sh_pfc_write_raw_reg(
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100237 sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100238 ~data);
Magnus Damme499ada2011-12-14 01:01:14 +0900239
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100240 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
Magnus Damm2967dab2008-10-08 20:41:43 +0900241}
242
Laurent Pinchart533743d2013-07-15 13:03:20 +0200243static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100244 const struct pinmux_cfg_reg **crp,
245 unsigned int *fieldp, u32 *valuep)
Magnus Damm2967dab2008-10-08 20:41:43 +0900246{
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100247 unsigned int k = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900248
Magnus Damm2967dab2008-10-08 20:41:43 +0900249 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100250 const struct pinmux_cfg_reg *config_reg =
251 pfc->info->cfg_regs + k;
252 unsigned int r_width = config_reg->reg_width;
253 unsigned int f_width = config_reg->field_width;
254 unsigned int curr_width;
255 unsigned int bit_pos;
256 unsigned int pos = 0;
257 unsigned int m = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900258
259 if (!r_width)
260 break;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900261
Magnus Dammf78a26f2011-12-14 01:01:05 +0900262 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100263 u32 ncomb;
264 u32 n;
265
Magnus Dammf78a26f2011-12-14 01:01:05 +0900266 if (f_width)
267 curr_width = f_width;
268 else
269 curr_width = config_reg->var_field_width[m];
270
271 ncomb = 1 << curr_width;
272 for (n = 0; n < ncomb; n++) {
273 if (config_reg->enum_ids[pos + n] == enum_id) {
274 *crp = config_reg;
275 *fieldp = m;
276 *valuep = n;
Magnus Dammf78a26f2011-12-14 01:01:05 +0900277 return 0;
278 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900279 }
Magnus Dammf78a26f2011-12-14 01:01:05 +0900280 pos += ncomb;
281 m++;
Magnus Damm2967dab2008-10-08 20:41:43 +0900282 }
283 k++;
284 }
285
Laurent Pinchartb705c052013-03-10 16:38:23 +0100286 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900287}
288
Laurent Pinchart533743d2013-07-15 13:03:20 +0200289static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
290 u16 *enum_idp)
Magnus Damm2967dab2008-10-08 20:41:43 +0900291{
Geert Uytterhoevenb8b47d62015-09-21 16:27:23 +0200292 const u16 *data = pfc->info->pinmux_data;
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100293 unsigned int k;
Magnus Damm2967dab2008-10-08 20:41:43 +0900294
Magnus Damm2967dab2008-10-08 20:41:43 +0900295 if (pos) {
296 *enum_idp = data[pos + 1];
297 return pos + 1;
298 }
299
Geert Uytterhoevenb8b47d62015-09-21 16:27:23 +0200300 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100301 if (data[k] == mark) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900302 *enum_idp = data[k + 1];
303 return k + 1;
304 }
305 }
306
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100307 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
308 mark);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100309 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900310}
311
Laurent Pinchart861601d2013-03-10 15:29:14 +0100312int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
Magnus Damm2967dab2008-10-08 20:41:43 +0900313{
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100314 const struct pinmux_range *range;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100315 int pos = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900316
317 switch (pinmux_type) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100318 case PINMUX_TYPE_GPIO:
Magnus Damm2967dab2008-10-08 20:41:43 +0900319 case PINMUX_TYPE_FUNCTION:
320 range = NULL;
321 break;
322
323 case PINMUX_TYPE_OUTPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100324 range = &pfc->info->output;
Magnus Damm2967dab2008-10-08 20:41:43 +0900325 break;
326
327 case PINMUX_TYPE_INPUT:
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100328 range = &pfc->info->input;
Magnus Damm2967dab2008-10-08 20:41:43 +0900329 break;
330
Magnus Damm2967dab2008-10-08 20:41:43 +0900331 default:
Laurent Pinchartb705c052013-03-10 16:38:23 +0100332 return -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900333 }
334
Laurent Pincharte3c470512013-03-10 17:30:25 +0100335 /* Iterate over all the configuration fields we need to update. */
Magnus Damm2967dab2008-10-08 20:41:43 +0900336 while (1) {
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100337 const struct pinmux_cfg_reg *cr;
338 unsigned int field;
339 u16 enum_id;
340 u32 value;
341 int in_range;
342 int ret;
343
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100344 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100345 if (pos < 0)
346 return pos;
Magnus Damm2967dab2008-10-08 20:41:43 +0900347
348 if (!enum_id)
349 break;
350
Laurent Pincharte3c470512013-03-10 17:30:25 +0100351 /* Check if the configuration field selects a function. If it
352 * doesn't, skip the field if it's not applicable to the
353 * requested pinmux type.
354 */
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100355 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
Magnus Damm50dd3142010-01-19 13:52:28 +0000356 if (!in_range) {
Laurent Pincharte3c470512013-03-10 17:30:25 +0100357 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
358 /* Functions are allowed to modify all
359 * fields.
360 */
361 in_range = 1;
362 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
363 /* Input/output types can only modify fields
364 * that correspond to their respective ranges.
Magnus Damm50dd3142010-01-19 13:52:28 +0000365 */
Laurent Pinchart4aeacd52012-12-15 23:50:53 +0100366 in_range = sh_pfc_enum_in_range(enum_id, range);
Magnus Damm2967dab2008-10-08 20:41:43 +0900367
Magnus Damm50dd3142010-01-19 13:52:28 +0000368 /*
369 * special case pass through for fixed
370 * input-only or output-only pins without
371 * function enum register association.
372 */
373 if (in_range && enum_id == range->force)
374 continue;
Magnus Damm50dd3142010-01-19 13:52:28 +0000375 }
Laurent Pincharte3c470512013-03-10 17:30:25 +0100376 /* GPIOs are only allowed to modify function fields. */
Magnus Damm42eed422008-10-22 18:29:17 +0900377 }
378
Magnus Damm2967dab2008-10-08 20:41:43 +0900379 if (!in_range)
380 continue;
381
Laurent Pinchartb705c052013-03-10 16:38:23 +0100382 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
383 if (ret < 0)
384 return ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900385
Laurent Pinchart861601d2013-03-10 15:29:14 +0100386 sh_pfc_write_config_reg(pfc, cr, field, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900387 }
388
389 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900390}
391
Niklas Söderlund92293362016-11-12 17:04:25 +0100392const struct sh_pfc_bias_info *
393sh_pfc_pin_to_bias_info(const struct sh_pfc_bias_info *info,
394 unsigned int num, unsigned int pin)
395{
396 unsigned int i;
397
398 for (i = 0; i < num; i++)
399 if (info[i].pin == pin)
400 return &info[i];
401
402 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
403
404 return NULL;
405}
406
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200407static int sh_pfc_init_ranges(struct sh_pfc *pfc)
408{
409 struct sh_pfc_pin_range *range;
410 unsigned int nr_ranges;
411 unsigned int i;
412
413 if (pfc->info->pins[0].pin == (u16)-1) {
414 /* Pin number -1 denotes that the SoC doesn't report pin numbers
415 * in its pin arrays yet. Consider the pin numbers range as
416 * continuous and allocate a single range.
417 */
418 pfc->nr_ranges = 1;
419 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
420 GFP_KERNEL);
421 if (pfc->ranges == NULL)
422 return -ENOMEM;
423
424 pfc->ranges->start = 0;
425 pfc->ranges->end = pfc->info->nr_pins - 1;
426 pfc->nr_gpio_pins = pfc->info->nr_pins;
427
428 return 0;
429 }
430
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200431 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
432 * be sorted by pin numbers, and pins without a GPIO port must come
433 * last.
434 */
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200435 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
436 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
437 nr_ranges++;
438 }
439
440 pfc->nr_ranges = nr_ranges;
441 pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges) * nr_ranges,
442 GFP_KERNEL);
443 if (pfc->ranges == NULL)
444 return -ENOMEM;
445
446 range = pfc->ranges;
447 range->start = pfc->info->pins[0].pin;
448
449 for (i = 1; i < pfc->info->nr_pins; ++i) {
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200450 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
451 continue;
452
453 range->end = pfc->info->pins[i-1].pin;
454 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
455 pfc->nr_gpio_pins = range->end + 1;
456
457 range++;
458 range->start = pfc->info->pins[i].pin;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200459 }
460
461 range->end = pfc->info->pins[i-1].pin;
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200462 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
463 pfc->nr_gpio_pins = range->end + 1;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200464
465 return 0;
466}
467
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200468#ifdef CONFIG_OF
469static const struct of_device_id sh_pfc_of_table[] = {
Niklas Söderlund1e7d5d82015-01-25 14:49:52 +0100470#ifdef CONFIG_PINCTRL_PFC_EMEV2
471 {
472 .compatible = "renesas,pfc-emev2",
473 .data = &emev2_pinmux_info,
474 },
475#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200476#ifdef CONFIG_PINCTRL_PFC_R8A73A4
477 {
478 .compatible = "renesas,pfc-r8a73a4",
479 .data = &r8a73a4_pinmux_info,
480 },
481#endif
482#ifdef CONFIG_PINCTRL_PFC_R8A7740
483 {
484 .compatible = "renesas,pfc-r8a7740",
485 .data = &r8a7740_pinmux_info,
486 },
487#endif
488#ifdef CONFIG_PINCTRL_PFC_R8A7778
489 {
490 .compatible = "renesas,pfc-r8a7778",
491 .data = &r8a7778_pinmux_info,
492 },
493#endif
494#ifdef CONFIG_PINCTRL_PFC_R8A7779
495 {
496 .compatible = "renesas,pfc-r8a7779",
497 .data = &r8a7779_pinmux_info,
498 },
499#endif
500#ifdef CONFIG_PINCTRL_PFC_R8A7790
501 {
502 .compatible = "renesas,pfc-r8a7790",
503 .data = &r8a7790_pinmux_info,
504 },
505#endif
Hisashi Nakamura50884512013-10-17 06:46:05 +0900506#ifdef CONFIG_PINCTRL_PFC_R8A7791
507 {
508 .compatible = "renesas,pfc-r8a7791",
509 .data = &r8a7791_pinmux_info,
510 },
511#endif
Sergei Shtylyov2cf59e02016-06-30 00:21:08 +0300512#ifdef CONFIG_PINCTRL_PFC_R8A7792
513 {
514 .compatible = "renesas,pfc-r8a7792",
515 .data = &r8a7792_pinmux_info,
516 },
517#endif
Ulrich Hecht19e1e982015-05-12 11:13:19 +0200518#ifdef CONFIG_PINCTRL_PFC_R8A7793
519 {
520 .compatible = "renesas,pfc-r8a7793",
521 .data = &r8a7793_pinmux_info,
522 },
523#endif
Hisashi Nakamura43c44362015-06-06 01:34:48 +0300524#ifdef CONFIG_PINCTRL_PFC_R8A7794
525 {
526 .compatible = "renesas,pfc-r8a7794",
527 .data = &r8a7794_pinmux_info,
528 },
529#endif
Takeshi Kihara0b0ffc92015-09-03 02:51:49 +0000530#ifdef CONFIG_PINCTRL_PFC_R8A7795
531 {
532 .compatible = "renesas,pfc-r8a7795",
533 .data = &r8a7795_pinmux_info,
534 },
535#endif
Takeshi Kiharaf9aece72016-08-18 15:12:32 +0200536#ifdef CONFIG_PINCTRL_PFC_R8A7796
537 {
538 .compatible = "renesas,pfc-r8a7796",
539 .data = &r8a7796_pinmux_info,
540 },
541#endif
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200542#ifdef CONFIG_PINCTRL_PFC_SH73A0
543 {
544 .compatible = "renesas,pfc-sh73a0",
545 .data = &sh73a0_pinmux_info,
546 },
547#endif
548 { },
549};
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200550#endif
551
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100552static int sh_pfc_probe(struct platform_device *pdev)
Magnus Damm2967dab2008-10-08 20:41:43 +0900553{
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200554 const struct platform_device_id *platid = platform_get_device_id(pdev);
555#ifdef CONFIG_OF
556 struct device_node *np = pdev->dev.of_node;
557#endif
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100558 const struct sh_pfc_soc_info *info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100559 struct sh_pfc *pfc;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900560 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900561
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200562#ifdef CONFIG_OF
563 if (np)
Wolfram Sang331207a2016-03-01 17:38:46 +0100564 info = of_device_get_match_data(&pdev->dev);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200565 else
566#endif
567 info = platid ? (const void *)platid->driver_data : NULL;
568
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100569 if (info == NULL)
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100570 return -ENODEV;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900571
Magnus Damm8c43fcc2013-02-14 21:23:47 +0900572 pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100573 if (pfc == NULL)
574 return -ENOMEM;
Magnus Dammb0e10212011-12-09 12:14:27 +0900575
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100576 pfc->info = info;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100577 pfc->dev = &pdev->dev;
578
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100579 ret = sh_pfc_map_resources(pfc, pdev);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100580 if (unlikely(ret < 0))
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100581 return ret;
Laurent Pinchartd4e62d02012-12-15 23:50:43 +0100582
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100583 spin_lock_init(&pfc->lock);
Magnus Damm69edbba2008-12-25 18:17:34 +0900584
Laurent Pinchart0c151062013-04-21 20:21:57 +0200585 if (info->ops && info->ops->init) {
586 ret = info->ops->init(pfc);
587 if (ret < 0)
588 return ret;
Geert Uytterhoeven369bbf42017-03-09 19:20:48 +0100589
590 /* .init() may have overridden pfc->info */
591 info = pfc->info;
Laurent Pinchart0c151062013-04-21 20:21:57 +0200592 }
593
Wolfram Sang01298012016-03-07 19:40:57 +0100594 /* Enable dummy states for those platforms without pinctrl support */
595 if (!of_have_populated_dt())
596 pinctrl_provide_dummies();
Magnus Damm69edbba2008-12-25 18:17:34 +0900597
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200598 ret = sh_pfc_init_ranges(pfc);
599 if (ret < 0)
600 return ret;
601
Paul Mundtca5481c62012-07-10 12:08:14 +0900602 /*
603 * Initialize pinctrl bindings first
604 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100605 ret = sh_pfc_register_pinctrl(pfc);
Laurent Pinchartf9492fd2012-12-15 23:50:45 +0100606 if (unlikely(ret != 0))
Laurent Pinchart0a332c92014-09-11 00:55:55 +0300607 return ret;
Magnus Damm69edbba2008-12-25 18:17:34 +0900608
Magnus Dammabc60d42016-02-17 17:15:49 +0900609#ifdef CONFIG_PINCTRL_SH_PFC_GPIO
Paul Mundtca5481c62012-07-10 12:08:14 +0900610 /*
611 * Then the GPIO chip
612 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100613 ret = sh_pfc_register_gpiochip(pfc);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100614 if (unlikely(ret != 0)) {
Paul Mundtca5481c62012-07-10 12:08:14 +0900615 /*
616 * If the GPIO chip fails to come up we still leave the
617 * PFC state as it is, given that there are already
618 * extant users of it that have succeeded by this point.
619 */
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100620 dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
Paul Mundtca5481c62012-07-10 12:08:14 +0900621 }
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100622#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900623
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100624 platform_set_drvdata(pdev, pfc);
625
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100626 dev_info(pfc->dev, "%s support registered\n", info->name);
Paul Mundtca5481c62012-07-10 12:08:14 +0900627
Paul Mundtb3c185a2012-06-20 17:29:04 +0900628 return 0;
Paul Mundtb72421d2010-10-04 03:54:56 +0900629}
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100630
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100631static const struct platform_device_id sh_pfc_id_table[] = {
Laurent Pinchartccda5522012-12-15 23:51:29 +0100632#ifdef CONFIG_PINCTRL_PFC_SH7203
633 { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
634#endif
Laurent Pincharta8d42fc2012-12-15 23:51:30 +0100635#ifdef CONFIG_PINCTRL_PFC_SH7264
636 { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
637#endif
Laurent Pinchartf5e811f2012-12-15 23:51:31 +0100638#ifdef CONFIG_PINCTRL_PFC_SH7269
639 { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
640#endif
Laurent Pinchart74cad602012-12-15 23:51:32 +0100641#ifdef CONFIG_PINCTRL_PFC_SH7720
642 { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
643#endif
Laurent Pinchartf5e25ae2012-12-15 23:51:33 +0100644#ifdef CONFIG_PINCTRL_PFC_SH7722
645 { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
646#endif
Laurent Pinchartd05afa02012-12-15 23:51:34 +0100647#ifdef CONFIG_PINCTRL_PFC_SH7723
648 { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
649#endif
Laurent Pinchart0ff25ba2012-12-15 23:51:35 +0100650#ifdef CONFIG_PINCTRL_PFC_SH7724
651 { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
652#endif
Laurent Pinchartac1ebc22012-12-15 23:51:36 +0100653#ifdef CONFIG_PINCTRL_PFC_SH7734
654 { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
655#endif
Laurent Pinchart0bb92672012-12-15 23:51:37 +0100656#ifdef CONFIG_PINCTRL_PFC_SH7757
657 { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
658#endif
Laurent Pincharta56398e2012-12-15 23:51:38 +0100659#ifdef CONFIG_PINCTRL_PFC_SH7785
660 { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
661#endif
Laurent Pinchartd2a31bd2012-12-15 23:51:39 +0100662#ifdef CONFIG_PINCTRL_PFC_SH7786
663 { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
664#endif
Laurent Pinchartd5d9a812012-12-15 23:51:40 +0100665#ifdef CONFIG_PINCTRL_PFC_SHX3
666 { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
667#endif
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100668 { "sh-pfc", 0 },
669 { },
670};
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100671
672static struct platform_driver sh_pfc_driver = {
673 .probe = sh_pfc_probe,
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100674 .id_table = sh_pfc_id_table,
675 .driver = {
676 .name = DRV_NAME,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200677 .of_match_table = of_match_ptr(sh_pfc_of_table),
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100678 },
679};
680
Laurent Pinchart40ee6fc2012-12-15 23:50:54 +0100681static int __init sh_pfc_init(void)
682{
683 return platform_driver_register(&sh_pfc_driver);
684}
685postcore_initcall(sh_pfc_init);