blob: 3c22dbebc80f202087c9940b05e3887a4c002252 [file] [log] [blame]
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001/*
2 * Pinctrl driver for Rockchip SoCs
3 *
4 * Copyright (c) 2013 MundoReader S.L.
5 * Author: Heiko Stuebner <heiko@sntech.de>
6 *
7 * With some ideas taken from pinctrl-samsung:
8 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
9 * http://www.samsung.com
10 * Copyright (c) 2012 Linaro Ltd
11 * http://www.linaro.org
12 *
13 * and pinctrl-at91:
14 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as published
18 * by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/io.h>
29#include <linux/bitops.h>
30#include <linux/gpio.h>
31#include <linux/of_address.h>
32#include <linux/of_irq.h>
33#include <linux/pinctrl/machine.h>
34#include <linux/pinctrl/pinconf.h>
35#include <linux/pinctrl/pinctrl.h>
36#include <linux/pinctrl/pinmux.h>
37#include <linux/pinctrl/pinconf-generic.h>
38#include <linux/irqchip/chained_irq.h>
Heiko Stübner7e865ab2013-07-23 13:34:20 +020039#include <linux/clk.h>
Heiko Stübner751a99a2014-05-05 13:58:20 +020040#include <linux/regmap.h>
Heiko Stübner14dee862014-05-05 13:59:09 +020041#include <linux/mfd/syscon.h>
Heiko Stübnerd3e51162013-06-10 22:16:22 +020042#include <dt-bindings/pinctrl/rockchip.h>
43
44#include "core.h"
45#include "pinconf.h"
46
47/* GPIO control registers */
48#define GPIO_SWPORT_DR 0x00
49#define GPIO_SWPORT_DDR 0x04
50#define GPIO_INTEN 0x30
51#define GPIO_INTMASK 0x34
52#define GPIO_INTTYPE_LEVEL 0x38
53#define GPIO_INT_POLARITY 0x3c
54#define GPIO_INT_STATUS 0x40
55#define GPIO_INT_RAWSTATUS 0x44
56#define GPIO_DEBOUNCE 0x48
57#define GPIO_PORTS_EOI 0x4c
58#define GPIO_EXT_PORT 0x50
59#define GPIO_LS_SYNC 0x60
60
Heiko Stübnera2829262013-10-16 01:07:20 +020061enum rockchip_pinctrl_type {
62 RK2928,
63 RK3066B,
64 RK3188,
Heiko Stübner66d750e2014-07-20 01:49:17 +020065 RK3288,
Heiko Stübnera2829262013-10-16 01:07:20 +020066};
67
Heiko Stübnerfc72c922014-06-16 01:36:05 +020068/**
69 * Encode variants of iomux registers into a type variable
70 */
71#define IOMUX_GPIO_ONLY BIT(0)
Heiko Stübner03716e12014-06-16 01:36:57 +020072#define IOMUX_WIDTH_4BIT BIT(1)
Heiko Stübner95ec8ae2014-06-16 01:37:23 +020073#define IOMUX_SOURCE_PMU BIT(2)
Heiko Stübner62f49222014-06-16 01:37:49 +020074#define IOMUX_UNROUTED BIT(3)
Heiko Stübnerfc72c922014-06-16 01:36:05 +020075
76/**
77 * @type: iomux variant using IOMUX_* constants
Heiko Stübner6bc0d122014-06-16 01:36:33 +020078 * @offset: if initialized to -1 it will be autocalculated, by specifying
79 * an initial offset value the relevant source offset can be reset
80 * to a new value for autocalculating the following iomux registers.
Heiko Stübnerfc72c922014-06-16 01:36:05 +020081 */
82struct rockchip_iomux {
83 int type;
Heiko Stübner6bc0d122014-06-16 01:36:33 +020084 int offset;
Heiko Stübner65fca612013-10-16 01:07:49 +020085};
86
Heiko Stübnerd3e51162013-06-10 22:16:22 +020087/**
88 * @reg_base: register base of the gpio bank
Heiko Stübner6ca52742013-10-16 01:08:42 +020089 * @reg_pull: optional separate register for additional pull settings
Heiko Stübnerd3e51162013-06-10 22:16:22 +020090 * @clk: clock of the gpio bank
91 * @irq: interrupt of the gpio bank
Doug Anderson68bda472014-11-19 14:51:32 -080092 * @saved_enables: Saved content of GPIO_INTEN at suspend time.
Heiko Stübnerd3e51162013-06-10 22:16:22 +020093 * @pin_base: first pin number
94 * @nr_pins: number of pins in this bank
95 * @name: name of the bank
96 * @bank_num: number of the bank, to account for holes
Heiko Stübnerfc72c922014-06-16 01:36:05 +020097 * @iomux: array describing the 4 iomux sources of the bank
Heiko Stübnerd3e51162013-06-10 22:16:22 +020098 * @valid: are all necessary informations present
99 * @of_node: dt node of this bank
100 * @drvdata: common pinctrl basedata
101 * @domain: irqdomain of the gpio bank
102 * @gpio_chip: gpiolib chip
103 * @grange: gpio range
104 * @slock: spinlock for the gpio bank
105 */
106struct rockchip_pin_bank {
107 void __iomem *reg_base;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200108 struct regmap *regmap_pull;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200109 struct clk *clk;
110 int irq;
Doug Anderson68bda472014-11-19 14:51:32 -0800111 u32 saved_enables;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200112 u32 pin_base;
113 u8 nr_pins;
114 char *name;
115 u8 bank_num;
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200116 struct rockchip_iomux iomux[4];
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200117 bool valid;
118 struct device_node *of_node;
119 struct rockchip_pinctrl *drvdata;
120 struct irq_domain *domain;
121 struct gpio_chip gpio_chip;
122 struct pinctrl_gpio_range grange;
123 spinlock_t slock;
Heiko Stübner5a927502013-10-16 01:09:08 +0200124 u32 toggle_edge_mode;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200125};
126
127#define PIN_BANK(id, pins, label) \
128 { \
129 .bank_num = id, \
130 .nr_pins = pins, \
131 .name = label, \
Heiko Stübner6bc0d122014-06-16 01:36:33 +0200132 .iomux = { \
133 { .offset = -1 }, \
134 { .offset = -1 }, \
135 { .offset = -1 }, \
136 { .offset = -1 }, \
137 }, \
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200138 }
139
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200140#define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
141 { \
142 .bank_num = id, \
143 .nr_pins = pins, \
144 .name = label, \
145 .iomux = { \
Heiko Stübner6bc0d122014-06-16 01:36:33 +0200146 { .type = iom0, .offset = -1 }, \
147 { .type = iom1, .offset = -1 }, \
148 { .type = iom2, .offset = -1 }, \
149 { .type = iom3, .offset = -1 }, \
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200150 }, \
151 }
152
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200153/**
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200154 */
155struct rockchip_pin_ctrl {
156 struct rockchip_pin_bank *pin_banks;
157 u32 nr_banks;
158 u32 nr_pins;
159 char *label;
Heiko Stübnera2829262013-10-16 01:07:20 +0200160 enum rockchip_pinctrl_type type;
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200161 int grf_mux_offset;
162 int pmu_mux_offset;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200163 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
164 int pin_num, struct regmap **regmap,
165 int *reg, u8 *bit);
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200166};
167
168struct rockchip_pin_config {
169 unsigned int func;
170 unsigned long *configs;
171 unsigned int nconfigs;
172};
173
174/**
175 * struct rockchip_pin_group: represent group of pins of a pinmux function.
176 * @name: name of the pin group, used to lookup the group.
177 * @pins: the pins included in this group.
178 * @npins: number of pins included in this group.
179 * @func: the mux function number to be programmed when selected.
180 * @configs: the config values to be set for each pin
181 * @nconfigs: number of configs for each pin
182 */
183struct rockchip_pin_group {
184 const char *name;
185 unsigned int npins;
186 unsigned int *pins;
187 struct rockchip_pin_config *data;
188};
189
190/**
191 * struct rockchip_pmx_func: represent a pin function.
192 * @name: name of the pin function, used to lookup the function.
193 * @groups: one or more names of pin groups that provide this function.
194 * @num_groups: number of groups included in @groups.
195 */
196struct rockchip_pmx_func {
197 const char *name;
198 const char **groups;
199 u8 ngroups;
200};
201
202struct rockchip_pinctrl {
Heiko Stübner751a99a2014-05-05 13:58:20 +0200203 struct regmap *regmap_base;
Heiko Stübnerbfc7a422014-05-05 13:58:00 +0200204 int reg_size;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200205 struct regmap *regmap_pull;
Heiko Stübner14dee862014-05-05 13:59:09 +0200206 struct regmap *regmap_pmu;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200207 struct device *dev;
208 struct rockchip_pin_ctrl *ctrl;
209 struct pinctrl_desc pctl;
210 struct pinctrl_dev *pctl_dev;
211 struct rockchip_pin_group *groups;
212 unsigned int ngroups;
213 struct rockchip_pmx_func *functions;
214 unsigned int nfunctions;
215};
216
Heiko Stübner751a99a2014-05-05 13:58:20 +0200217static struct regmap_config rockchip_regmap_config = {
218 .reg_bits = 32,
219 .val_bits = 32,
220 .reg_stride = 4,
221};
222
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200223static inline struct rockchip_pin_bank *gc_to_pin_bank(struct gpio_chip *gc)
224{
225 return container_of(gc, struct rockchip_pin_bank, gpio_chip);
226}
227
228static const inline struct rockchip_pin_group *pinctrl_name_to_group(
229 const struct rockchip_pinctrl *info,
230 const char *name)
231{
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200232 int i;
233
234 for (i = 0; i < info->ngroups; i++) {
Axel Lin1cb95392013-08-21 10:28:50 +0800235 if (!strcmp(info->groups[i].name, name))
236 return &info->groups[i];
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200237 }
238
Axel Lin1cb95392013-08-21 10:28:50 +0800239 return NULL;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200240}
241
242/*
243 * given a pin number that is local to a pin controller, find out the pin bank
244 * and the register base of the pin bank.
245 */
246static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
247 unsigned pin)
248{
249 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
250
Axel Lin51578b92013-08-23 15:49:00 +0800251 while (pin >= (b->pin_base + b->nr_pins))
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200252 b++;
253
254 return b;
255}
256
257static struct rockchip_pin_bank *bank_num_to_bank(
258 struct rockchip_pinctrl *info,
259 unsigned num)
260{
261 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
262 int i;
263
Axel Lin1cb95392013-08-21 10:28:50 +0800264 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200265 if (b->bank_num == num)
Axel Lin1cb95392013-08-21 10:28:50 +0800266 return b;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200267 }
268
Axel Lin1cb95392013-08-21 10:28:50 +0800269 return ERR_PTR(-EINVAL);
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200270}
271
272/*
273 * Pinctrl_ops handling
274 */
275
276static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
277{
278 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
279
280 return info->ngroups;
281}
282
283static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
284 unsigned selector)
285{
286 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
287
288 return info->groups[selector].name;
289}
290
291static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
292 unsigned selector, const unsigned **pins,
293 unsigned *npins)
294{
295 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
296
297 if (selector >= info->ngroups)
298 return -EINVAL;
299
300 *pins = info->groups[selector].pins;
301 *npins = info->groups[selector].npins;
302
303 return 0;
304}
305
306static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
307 struct device_node *np,
308 struct pinctrl_map **map, unsigned *num_maps)
309{
310 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
311 const struct rockchip_pin_group *grp;
312 struct pinctrl_map *new_map;
313 struct device_node *parent;
314 int map_num = 1;
315 int i;
316
317 /*
318 * first find the group of this node and check if we need to create
319 * config maps for pins
320 */
321 grp = pinctrl_name_to_group(info, np->name);
322 if (!grp) {
323 dev_err(info->dev, "unable to find group for node %s\n",
324 np->name);
325 return -EINVAL;
326 }
327
328 map_num += grp->npins;
329 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
330 GFP_KERNEL);
331 if (!new_map)
332 return -ENOMEM;
333
334 *map = new_map;
335 *num_maps = map_num;
336
337 /* create mux map */
338 parent = of_get_parent(np);
339 if (!parent) {
340 devm_kfree(pctldev->dev, new_map);
341 return -EINVAL;
342 }
343 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
344 new_map[0].data.mux.function = parent->name;
345 new_map[0].data.mux.group = np->name;
346 of_node_put(parent);
347
348 /* create config map */
349 new_map++;
350 for (i = 0; i < grp->npins; i++) {
351 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
352 new_map[i].data.configs.group_or_pin =
353 pin_get_name(pctldev, grp->pins[i]);
354 new_map[i].data.configs.configs = grp->data[i].configs;
355 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
356 }
357
358 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
359 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
360
361 return 0;
362}
363
364static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
365 struct pinctrl_map *map, unsigned num_maps)
366{
367}
368
369static const struct pinctrl_ops rockchip_pctrl_ops = {
370 .get_groups_count = rockchip_get_groups_count,
371 .get_group_name = rockchip_get_group_name,
372 .get_group_pins = rockchip_get_group_pins,
373 .dt_node_to_map = rockchip_dt_node_to_map,
374 .dt_free_map = rockchip_dt_free_map,
375};
376
377/*
378 * Hardware access
379 */
380
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200381static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
382{
383 struct rockchip_pinctrl *info = bank->drvdata;
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200384 int iomux_num = (pin / 8);
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200385 struct regmap *regmap;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200386 unsigned int val;
Heiko Stübner03716e12014-06-16 01:36:57 +0200387 int reg, ret, mask;
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200388 u8 bit;
389
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200390 if (iomux_num > 3)
391 return -EINVAL;
392
Heiko Stübner62f49222014-06-16 01:37:49 +0200393 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
394 dev_err(info->dev, "pin %d is unrouted\n", pin);
395 return -EINVAL;
396 }
397
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200398 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200399 return RK_FUNC_GPIO;
400
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200401 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
402 ? info->regmap_pmu : info->regmap_base;
403
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200404 /* get basic quadrupel of mux registers and the correct reg inside */
Heiko Stübner03716e12014-06-16 01:36:57 +0200405 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
Heiko Stübner6bc0d122014-06-16 01:36:33 +0200406 reg = bank->iomux[iomux_num].offset;
Heiko Stübner03716e12014-06-16 01:36:57 +0200407 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
408 if ((pin % 8) >= 4)
409 reg += 0x4;
410 bit = (pin % 4) * 4;
411 } else {
412 bit = (pin % 8) * 2;
413 }
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200414
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200415 ret = regmap_read(regmap, reg, &val);
Heiko Stübner751a99a2014-05-05 13:58:20 +0200416 if (ret)
417 return ret;
418
Heiko Stübner03716e12014-06-16 01:36:57 +0200419 return ((val >> bit) & mask);
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200420}
421
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200422/*
423 * Set a new mux function for a pin.
424 *
425 * The register is divided into the upper and lower 16 bit. When changing
426 * a value, the previous register value is not read and changed. Instead
427 * it seems the changed bits are marked in the upper 16 bit, while the
428 * changed value gets set in the same offset in the lower 16 bit.
429 * All pin settings seem to be 2 bit wide in both the upper and lower
430 * parts.
431 * @bank: pin bank to change
432 * @pin: pin to change
433 * @mux: new mux function to set
434 */
Heiko Stübner14797182014-03-26 00:57:00 +0100435static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200436{
437 struct rockchip_pinctrl *info = bank->drvdata;
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200438 int iomux_num = (pin / 8);
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200439 struct regmap *regmap;
Heiko Stübner03716e12014-06-16 01:36:57 +0200440 int reg, ret, mask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200441 unsigned long flags;
442 u8 bit;
Sonny Rao99e872d2014-07-31 22:58:00 -0700443 u32 data, rmask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200444
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200445 if (iomux_num > 3)
446 return -EINVAL;
447
Heiko Stübner62f49222014-06-16 01:37:49 +0200448 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
449 dev_err(info->dev, "pin %d is unrouted\n", pin);
450 return -EINVAL;
451 }
452
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200453 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
Heiko Stübnerc4a532de2014-03-26 00:57:52 +0100454 if (mux != RK_FUNC_GPIO) {
455 dev_err(info->dev,
456 "pin %d only supports a gpio mux\n", pin);
457 return -ENOTSUPP;
458 } else {
459 return 0;
460 }
461 }
462
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200463 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
464 bank->bank_num, pin, mux);
465
Heiko Stübner95ec8ae2014-06-16 01:37:23 +0200466 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
467 ? info->regmap_pmu : info->regmap_base;
468
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200469 /* get basic quadrupel of mux registers and the correct reg inside */
Heiko Stübner03716e12014-06-16 01:36:57 +0200470 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
Heiko Stübner6bc0d122014-06-16 01:36:33 +0200471 reg = bank->iomux[iomux_num].offset;
Heiko Stübner03716e12014-06-16 01:36:57 +0200472 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
473 if ((pin % 8) >= 4)
474 reg += 0x4;
475 bit = (pin % 4) * 4;
476 } else {
477 bit = (pin % 8) * 2;
478 }
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200479
480 spin_lock_irqsave(&bank->slock, flags);
481
Heiko Stübner03716e12014-06-16 01:36:57 +0200482 data = (mask << (bit + 16));
Sonny Rao99e872d2014-07-31 22:58:00 -0700483 rmask = data | (data >> 16);
Heiko Stübner03716e12014-06-16 01:36:57 +0200484 data |= (mux & mask) << bit;
Sonny Rao99e872d2014-07-31 22:58:00 -0700485 ret = regmap_update_bits(regmap, reg, rmask, data);
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200486
487 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübner14797182014-03-26 00:57:00 +0100488
Heiko Stübner751a99a2014-05-05 13:58:20 +0200489 return ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200490}
491
Heiko Stübnera2829262013-10-16 01:07:20 +0200492#define RK2928_PULL_OFFSET 0x118
493#define RK2928_PULL_PINS_PER_REG 16
494#define RK2928_PULL_BANK_STRIDE 8
495
496static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
Heiko Stübner751a99a2014-05-05 13:58:20 +0200497 int pin_num, struct regmap **regmap,
498 int *reg, u8 *bit)
Heiko Stübnera2829262013-10-16 01:07:20 +0200499{
500 struct rockchip_pinctrl *info = bank->drvdata;
501
Heiko Stübner751a99a2014-05-05 13:58:20 +0200502 *regmap = info->regmap_base;
503 *reg = RK2928_PULL_OFFSET;
Heiko Stübnera2829262013-10-16 01:07:20 +0200504 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
505 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
506
507 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
508};
509
Heiko Stübnerbfc7a422014-05-05 13:58:00 +0200510#define RK3188_PULL_OFFSET 0x164
Heiko Stübner6ca52742013-10-16 01:08:42 +0200511#define RK3188_PULL_BITS_PER_PIN 2
512#define RK3188_PULL_PINS_PER_REG 8
513#define RK3188_PULL_BANK_STRIDE 16
Heiko Stübner14dee862014-05-05 13:59:09 +0200514#define RK3188_PULL_PMU_OFFSET 0x64
Heiko Stübner6ca52742013-10-16 01:08:42 +0200515
516static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
Heiko Stübner751a99a2014-05-05 13:58:20 +0200517 int pin_num, struct regmap **regmap,
518 int *reg, u8 *bit)
Heiko Stübner6ca52742013-10-16 01:08:42 +0200519{
520 struct rockchip_pinctrl *info = bank->drvdata;
521
522 /* The first 12 pins of the first bank are located elsewhere */
Heiko Stübnerfc72c922014-06-16 01:36:05 +0200523 if (bank->bank_num == 0 && pin_num < 12) {
Heiko Stübner14dee862014-05-05 13:59:09 +0200524 *regmap = info->regmap_pmu ? info->regmap_pmu
525 : bank->regmap_pull;
526 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200527 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
Heiko Stübner6ca52742013-10-16 01:08:42 +0200528 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
529 *bit *= RK3188_PULL_BITS_PER_PIN;
530 } else {
Heiko Stübner751a99a2014-05-05 13:58:20 +0200531 *regmap = info->regmap_pull ? info->regmap_pull
532 : info->regmap_base;
533 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
534
Heiko Stübnerbfc7a422014-05-05 13:58:00 +0200535 /* correct the offset, as it is the 2nd pull register */
536 *reg -= 4;
Heiko Stübner6ca52742013-10-16 01:08:42 +0200537 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
538 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
539
540 /*
541 * The bits in these registers have an inverse ordering
542 * with the lowest pin being in bits 15:14 and the highest
543 * pin in bits 1:0
544 */
545 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
546 *bit *= RK3188_PULL_BITS_PER_PIN;
547 }
548}
549
Heiko Stübner304f0772014-06-16 01:38:14 +0200550#define RK3288_PULL_OFFSET 0x140
551static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
552 int pin_num, struct regmap **regmap,
553 int *reg, u8 *bit)
554{
555 struct rockchip_pinctrl *info = bank->drvdata;
556
557 /* The first 24 pins of the first bank are located in PMU */
558 if (bank->bank_num == 0) {
559 *regmap = info->regmap_pmu;
560 *reg = RK3188_PULL_PMU_OFFSET;
561
562 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
563 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
564 *bit *= RK3188_PULL_BITS_PER_PIN;
565 } else {
566 *regmap = info->regmap_base;
567 *reg = RK3288_PULL_OFFSET;
568
569 /* correct the offset, as we're starting with the 2nd bank */
570 *reg -= 0x10;
571 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
572 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
573
574 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
575 *bit *= RK3188_PULL_BITS_PER_PIN;
576 }
577}
578
Heiko Stübnerb547c802014-07-20 01:50:11 +0200579#define RK3288_DRV_PMU_OFFSET 0x70
580#define RK3288_DRV_GRF_OFFSET 0x1c0
581#define RK3288_DRV_BITS_PER_PIN 2
582#define RK3288_DRV_PINS_PER_REG 8
583#define RK3288_DRV_BANK_STRIDE 16
584static int rk3288_drv_list[] = { 2, 4, 8, 12 };
585
586static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
587 int pin_num, struct regmap **regmap,
588 int *reg, u8 *bit)
589{
590 struct rockchip_pinctrl *info = bank->drvdata;
591
592 /* The first 24 pins of the first bank are located in PMU */
593 if (bank->bank_num == 0) {
594 *regmap = info->regmap_pmu;
595 *reg = RK3288_DRV_PMU_OFFSET;
596
597 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
598 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
599 *bit *= RK3288_DRV_BITS_PER_PIN;
600 } else {
601 *regmap = info->regmap_base;
602 *reg = RK3288_DRV_GRF_OFFSET;
603
604 /* correct the offset, as we're starting with the 2nd bank */
605 *reg -= 0x10;
606 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
607 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
608
609 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
610 *bit *= RK3288_DRV_BITS_PER_PIN;
611 }
612}
613
614static int rk3288_get_drive(struct rockchip_pin_bank *bank, int pin_num)
615{
616 struct regmap *regmap;
617 int reg, ret;
618 u32 data;
619 u8 bit;
620
621 rk3288_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
622
623 ret = regmap_read(regmap, reg, &data);
624 if (ret)
625 return ret;
626
627 data >>= bit;
628 data &= (1 << RK3288_DRV_BITS_PER_PIN) - 1;
629
630 return rk3288_drv_list[data];
631}
632
633static int rk3288_set_drive(struct rockchip_pin_bank *bank, int pin_num,
634 int strength)
635{
636 struct rockchip_pinctrl *info = bank->drvdata;
637 struct regmap *regmap;
638 unsigned long flags;
639 int reg, ret, i;
Sonny Rao99e872d2014-07-31 22:58:00 -0700640 u32 data, rmask;
Heiko Stübnerb547c802014-07-20 01:50:11 +0200641 u8 bit;
642
643 rk3288_calc_drv_reg_and_bit(bank, pin_num, &regmap, &reg, &bit);
644
645 ret = -EINVAL;
646 for (i = 0; i < ARRAY_SIZE(rk3288_drv_list); i++) {
647 if (rk3288_drv_list[i] == strength) {
648 ret = i;
649 break;
650 }
651 }
652
653 if (ret < 0) {
654 dev_err(info->dev, "unsupported driver strength %d\n",
655 strength);
656 return ret;
657 }
658
659 spin_lock_irqsave(&bank->slock, flags);
660
661 /* enable the write to the equivalent lower bits */
662 data = ((1 << RK3288_DRV_BITS_PER_PIN) - 1) << (bit + 16);
Sonny Rao99e872d2014-07-31 22:58:00 -0700663 rmask = data | (data >> 16);
Heiko Stübnerb547c802014-07-20 01:50:11 +0200664 data |= (ret << bit);
665
Sonny Rao99e872d2014-07-31 22:58:00 -0700666 ret = regmap_update_bits(regmap, reg, rmask, data);
Heiko Stübnerb547c802014-07-20 01:50:11 +0200667 spin_unlock_irqrestore(&bank->slock, flags);
668
669 return ret;
670}
671
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200672static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
673{
674 struct rockchip_pinctrl *info = bank->drvdata;
675 struct rockchip_pin_ctrl *ctrl = info->ctrl;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200676 struct regmap *regmap;
677 int reg, ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200678 u8 bit;
Heiko Stübner6ca52742013-10-16 01:08:42 +0200679 u32 data;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200680
681 /* rk3066b does support any pulls */
Heiko Stübnera2829262013-10-16 01:07:20 +0200682 if (ctrl->type == RK3066B)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200683 return PIN_CONFIG_BIAS_DISABLE;
684
Heiko Stübner751a99a2014-05-05 13:58:20 +0200685 ctrl->pull_calc_reg(bank, pin_num, &regmap, &reg, &bit);
686
687 ret = regmap_read(regmap, reg, &data);
688 if (ret)
689 return ret;
Heiko Stübner6ca52742013-10-16 01:08:42 +0200690
Heiko Stübnera2829262013-10-16 01:07:20 +0200691 switch (ctrl->type) {
692 case RK2928:
Heiko Stübner751a99a2014-05-05 13:58:20 +0200693 return !(data & BIT(bit))
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200694 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
695 : PIN_CONFIG_BIAS_DISABLE;
Heiko Stübnera2829262013-10-16 01:07:20 +0200696 case RK3188:
Heiko Stübner66d750e2014-07-20 01:49:17 +0200697 case RK3288:
Heiko Stübner751a99a2014-05-05 13:58:20 +0200698 data >>= bit;
Heiko Stübner6ca52742013-10-16 01:08:42 +0200699 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
700
701 switch (data) {
702 case 0:
703 return PIN_CONFIG_BIAS_DISABLE;
704 case 1:
705 return PIN_CONFIG_BIAS_PULL_UP;
706 case 2:
707 return PIN_CONFIG_BIAS_PULL_DOWN;
708 case 3:
709 return PIN_CONFIG_BIAS_BUS_HOLD;
710 }
711
712 dev_err(info->dev, "unknown pull setting\n");
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200713 return -EIO;
Heiko Stübnera2829262013-10-16 01:07:20 +0200714 default:
715 dev_err(info->dev, "unsupported pinctrl type\n");
716 return -EINVAL;
717 };
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200718}
719
720static int rockchip_set_pull(struct rockchip_pin_bank *bank,
721 int pin_num, int pull)
722{
723 struct rockchip_pinctrl *info = bank->drvdata;
724 struct rockchip_pin_ctrl *ctrl = info->ctrl;
Heiko Stübner751a99a2014-05-05 13:58:20 +0200725 struct regmap *regmap;
726 int reg, ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200727 unsigned long flags;
728 u8 bit;
Sonny Rao99e872d2014-07-31 22:58:00 -0700729 u32 data, rmask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200730
731 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
732 bank->bank_num, pin_num, pull);
733
734 /* rk3066b does support any pulls */
Heiko Stübnera2829262013-10-16 01:07:20 +0200735 if (ctrl->type == RK3066B)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200736 return pull ? -EINVAL : 0;
737
Heiko Stübner751a99a2014-05-05 13:58:20 +0200738 ctrl->pull_calc_reg(bank, pin_num, &regmap, &reg, &bit);
Heiko Stübner6ca52742013-10-16 01:08:42 +0200739
Heiko Stübnera2829262013-10-16 01:07:20 +0200740 switch (ctrl->type) {
741 case RK2928:
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200742 spin_lock_irqsave(&bank->slock, flags);
743
744 data = BIT(bit + 16);
745 if (pull == PIN_CONFIG_BIAS_DISABLE)
746 data |= BIT(bit);
Heiko Stübner751a99a2014-05-05 13:58:20 +0200747 ret = regmap_write(regmap, reg, data);
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200748
749 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübnera2829262013-10-16 01:07:20 +0200750 break;
751 case RK3188:
Heiko Stübner66d750e2014-07-20 01:49:17 +0200752 case RK3288:
Heiko Stübner6ca52742013-10-16 01:08:42 +0200753 spin_lock_irqsave(&bank->slock, flags);
754
755 /* enable the write to the equivalent lower bits */
756 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
Sonny Rao99e872d2014-07-31 22:58:00 -0700757 rmask = data | (data >> 16);
Heiko Stübner6ca52742013-10-16 01:08:42 +0200758
759 switch (pull) {
760 case PIN_CONFIG_BIAS_DISABLE:
761 break;
762 case PIN_CONFIG_BIAS_PULL_UP:
763 data |= (1 << bit);
764 break;
765 case PIN_CONFIG_BIAS_PULL_DOWN:
766 data |= (2 << bit);
767 break;
768 case PIN_CONFIG_BIAS_BUS_HOLD:
769 data |= (3 << bit);
770 break;
771 default:
Dan Carpenterd32c3e22013-11-14 11:22:54 +0300772 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübner6ca52742013-10-16 01:08:42 +0200773 dev_err(info->dev, "unsupported pull setting %d\n",
774 pull);
775 return -EINVAL;
776 }
777
Sonny Rao99e872d2014-07-31 22:58:00 -0700778 ret = regmap_update_bits(regmap, reg, rmask, data);
Heiko Stübner6ca52742013-10-16 01:08:42 +0200779
780 spin_unlock_irqrestore(&bank->slock, flags);
781 break;
Heiko Stübnera2829262013-10-16 01:07:20 +0200782 default:
783 dev_err(info->dev, "unsupported pinctrl type\n");
784 return -EINVAL;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200785 }
786
Heiko Stübner751a99a2014-05-05 13:58:20 +0200787 return ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200788}
789
790/*
791 * Pinmux_ops handling
792 */
793
794static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
795{
796 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
797
798 return info->nfunctions;
799}
800
801static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
802 unsigned selector)
803{
804 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
805
806 return info->functions[selector].name;
807}
808
809static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
810 unsigned selector, const char * const **groups,
811 unsigned * const num_groups)
812{
813 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
814
815 *groups = info->functions[selector].groups;
816 *num_groups = info->functions[selector].ngroups;
817
818 return 0;
819}
820
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200821static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
822 unsigned group)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200823{
824 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
825 const unsigned int *pins = info->groups[group].pins;
826 const struct rockchip_pin_config *data = info->groups[group].data;
827 struct rockchip_pin_bank *bank;
Heiko Stübner14797182014-03-26 00:57:00 +0100828 int cnt, ret = 0;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200829
830 dev_dbg(info->dev, "enable function %s group %s\n",
831 info->functions[selector].name, info->groups[group].name);
832
833 /*
834 * for each pin in the pin group selected, program the correspoding pin
835 * pin function number in the config register.
836 */
837 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
838 bank = pin_to_bank(info, pins[cnt]);
Heiko Stübner14797182014-03-26 00:57:00 +0100839 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
840 data[cnt].func);
841 if (ret)
842 break;
843 }
844
845 if (ret) {
846 /* revert the already done pin settings */
847 for (cnt--; cnt >= 0; cnt--)
848 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
849
850 return ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200851 }
852
853 return 0;
854}
855
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200856/*
857 * The calls to gpio_direction_output() and gpio_direction_input()
858 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
859 * function called from the gpiolib interface).
860 */
Doug Andersone5c2c9d2014-10-21 10:47:33 -0700861static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
862 int pin, bool input)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200863{
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200864 struct rockchip_pin_bank *bank;
Doug Andersone5c2c9d2014-10-21 10:47:33 -0700865 int ret;
Doug Andersonfab262f2014-10-21 10:47:35 -0700866 unsigned long flags;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200867 u32 data;
868
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200869 bank = gc_to_pin_bank(chip);
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200870
Heiko Stübner14797182014-03-26 00:57:00 +0100871 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
872 if (ret < 0)
873 return ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200874
Doug Andersonfab262f2014-10-21 10:47:35 -0700875 spin_lock_irqsave(&bank->slock, flags);
876
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200877 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
878 /* set bit to 1 for output, 0 for input */
879 if (!input)
880 data |= BIT(pin);
881 else
882 data &= ~BIT(pin);
883 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
884
Doug Andersonfab262f2014-10-21 10:47:35 -0700885 spin_unlock_irqrestore(&bank->slock, flags);
886
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200887 return 0;
888}
889
Doug Andersone5c2c9d2014-10-21 10:47:33 -0700890static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
891 struct pinctrl_gpio_range *range,
892 unsigned offset, bool input)
893{
894 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
895 struct gpio_chip *chip;
896 int pin;
897
898 chip = range->gc;
899 pin = offset - chip->base;
900 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
901 offset, range->name, pin, input ? "input" : "output");
902
903 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
904 input);
905}
906
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200907static const struct pinmux_ops rockchip_pmx_ops = {
908 .get_functions_count = rockchip_pmx_get_funcs_count,
909 .get_function_name = rockchip_pmx_get_func_name,
910 .get_function_groups = rockchip_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200911 .set_mux = rockchip_pmx_set,
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200912 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
913};
914
915/*
916 * Pinconf_ops handling
917 */
918
Heiko Stübner44b6d932013-06-16 17:41:16 +0200919static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
920 enum pin_config_param pull)
921{
Heiko Stübnera2829262013-10-16 01:07:20 +0200922 switch (ctrl->type) {
923 case RK2928:
924 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
925 pull == PIN_CONFIG_BIAS_DISABLE);
926 case RK3066B:
Heiko Stübner44b6d932013-06-16 17:41:16 +0200927 return pull ? false : true;
Heiko Stübnera2829262013-10-16 01:07:20 +0200928 case RK3188:
Heiko Stübner66d750e2014-07-20 01:49:17 +0200929 case RK3288:
Heiko Stübnera2829262013-10-16 01:07:20 +0200930 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
Heiko Stübner44b6d932013-06-16 17:41:16 +0200931 }
932
Heiko Stübnera2829262013-10-16 01:07:20 +0200933 return false;
Heiko Stübner44b6d932013-06-16 17:41:16 +0200934}
935
Doug Andersone5c2c9d2014-10-21 10:47:33 -0700936static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200937static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
938
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200939/* set the pin config settings for a specified pin */
940static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700941 unsigned long *configs, unsigned num_configs)
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200942{
943 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
944 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
Sherman Yin03b054e2013-08-27 11:32:12 -0700945 enum pin_config_param param;
946 u16 arg;
947 int i;
948 int rc;
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200949
Sherman Yin03b054e2013-08-27 11:32:12 -0700950 for (i = 0; i < num_configs; i++) {
951 param = pinconf_to_config_param(configs[i]);
952 arg = pinconf_to_config_argument(configs[i]);
953
954 switch (param) {
955 case PIN_CONFIG_BIAS_DISABLE:
956 rc = rockchip_set_pull(bank, pin - bank->pin_base,
957 param);
958 if (rc)
959 return rc;
960 break;
961 case PIN_CONFIG_BIAS_PULL_UP:
962 case PIN_CONFIG_BIAS_PULL_DOWN:
963 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
Heiko Stübner6ca52742013-10-16 01:08:42 +0200964 case PIN_CONFIG_BIAS_BUS_HOLD:
Sherman Yin03b054e2013-08-27 11:32:12 -0700965 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
966 return -ENOTSUPP;
967
968 if (!arg)
969 return -EINVAL;
970
971 rc = rockchip_set_pull(bank, pin - bank->pin_base,
972 param);
973 if (rc)
974 return rc;
975 break;
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200976 case PIN_CONFIG_OUTPUT:
Doug Andersone5c2c9d2014-10-21 10:47:33 -0700977 rockchip_gpio_set(&bank->gpio_chip,
978 pin - bank->pin_base, arg);
979 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
980 pin - bank->pin_base, false);
Heiko Stübnera076e2e2014-04-23 14:28:59 +0200981 if (rc)
982 return rc;
983 break;
Heiko Stübnerb547c802014-07-20 01:50:11 +0200984 case PIN_CONFIG_DRIVE_STRENGTH:
985 /* rk3288 is the first with per-pin drive-strength */
986 if (info->ctrl->type != RK3288)
987 return -ENOTSUPP;
988
989 rc = rk3288_set_drive(bank, pin - bank->pin_base, arg);
990 if (rc < 0)
991 return rc;
992 break;
Sherman Yin03b054e2013-08-27 11:32:12 -0700993 default:
Heiko Stübner44b6d932013-06-16 17:41:16 +0200994 return -ENOTSUPP;
Sherman Yin03b054e2013-08-27 11:32:12 -0700995 break;
996 }
997 } /* for each config */
Heiko Stübnerd3e51162013-06-10 22:16:22 +0200998
999 return 0;
1000}
1001
1002/* get the pin config settings for a specified pin */
1003static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1004 unsigned long *config)
1005{
1006 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1007 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1008 enum pin_config_param param = pinconf_to_config_param(*config);
Heiko Stübnerdab3eba2014-04-23 14:27:51 +02001009 u16 arg;
Heiko Stübnera076e2e2014-04-23 14:28:59 +02001010 int rc;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001011
1012 switch (param) {
1013 case PIN_CONFIG_BIAS_DISABLE:
Heiko Stübner44b6d932013-06-16 17:41:16 +02001014 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001015 return -EINVAL;
1016
Heiko Stübnerdab3eba2014-04-23 14:27:51 +02001017 arg = 0;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001018 break;
Heiko Stübner44b6d932013-06-16 17:41:16 +02001019 case PIN_CONFIG_BIAS_PULL_UP:
1020 case PIN_CONFIG_BIAS_PULL_DOWN:
1021 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
Heiko Stübner6ca52742013-10-16 01:08:42 +02001022 case PIN_CONFIG_BIAS_BUS_HOLD:
Heiko Stübner44b6d932013-06-16 17:41:16 +02001023 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1024 return -ENOTSUPP;
1025
1026 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1027 return -EINVAL;
1028
Heiko Stübnerdab3eba2014-04-23 14:27:51 +02001029 arg = 1;
Heiko Stübner44b6d932013-06-16 17:41:16 +02001030 break;
Heiko Stübnera076e2e2014-04-23 14:28:59 +02001031 case PIN_CONFIG_OUTPUT:
1032 rc = rockchip_get_mux(bank, pin - bank->pin_base);
1033 if (rc != RK_FUNC_GPIO)
1034 return -EINVAL;
1035
1036 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
1037 if (rc < 0)
1038 return rc;
1039
1040 arg = rc ? 1 : 0;
1041 break;
Heiko Stübnerb547c802014-07-20 01:50:11 +02001042 case PIN_CONFIG_DRIVE_STRENGTH:
1043 /* rk3288 is the first with per-pin drive-strength */
1044 if (info->ctrl->type != RK3288)
1045 return -ENOTSUPP;
1046
1047 rc = rk3288_get_drive(bank, pin - bank->pin_base);
1048 if (rc < 0)
1049 return rc;
1050
1051 arg = rc;
1052 break;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001053 default:
1054 return -ENOTSUPP;
1055 break;
1056 }
1057
Heiko Stübnerdab3eba2014-04-23 14:27:51 +02001058 *config = pinconf_to_config_packed(param, arg);
1059
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001060 return 0;
1061}
1062
1063static const struct pinconf_ops rockchip_pinconf_ops = {
1064 .pin_config_get = rockchip_pinconf_get,
1065 .pin_config_set = rockchip_pinconf_set,
Heiko Stübnered62f2f2014-07-20 01:48:45 +02001066 .is_generic = true,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001067};
1068
Heiko Stübner65fca612013-10-16 01:07:49 +02001069static const struct of_device_id rockchip_bank_match[] = {
1070 { .compatible = "rockchip,gpio-bank" },
Heiko Stübner6ca52742013-10-16 01:08:42 +02001071 { .compatible = "rockchip,rk3188-gpio-bank0" },
Heiko Stübner65fca612013-10-16 01:07:49 +02001072 {},
1073};
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001074
1075static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
1076 struct device_node *np)
1077{
1078 struct device_node *child;
1079
1080 for_each_child_of_node(np, child) {
Heiko Stübner65fca612013-10-16 01:07:49 +02001081 if (of_match_node(rockchip_bank_match, child))
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001082 continue;
1083
1084 info->nfunctions++;
1085 info->ngroups += of_get_child_count(child);
1086 }
1087}
1088
1089static int rockchip_pinctrl_parse_groups(struct device_node *np,
1090 struct rockchip_pin_group *grp,
1091 struct rockchip_pinctrl *info,
1092 u32 index)
1093{
1094 struct rockchip_pin_bank *bank;
1095 int size;
1096 const __be32 *list;
1097 int num;
1098 int i, j;
1099 int ret;
1100
1101 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1102
1103 /* Initialise group */
1104 grp->name = np->name;
1105
1106 /*
1107 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1108 * do sanity check and calculate pins number
1109 */
1110 list = of_get_property(np, "rockchip,pins", &size);
1111 /* we do not check return since it's safe node passed down */
1112 size /= sizeof(*list);
1113 if (!size || size % 4) {
1114 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1115 return -EINVAL;
1116 }
1117
1118 grp->npins = size / 4;
1119
1120 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1121 GFP_KERNEL);
1122 grp->data = devm_kzalloc(info->dev, grp->npins *
1123 sizeof(struct rockchip_pin_config),
1124 GFP_KERNEL);
1125 if (!grp->pins || !grp->data)
1126 return -ENOMEM;
1127
1128 for (i = 0, j = 0; i < size; i += 4, j++) {
1129 const __be32 *phandle;
1130 struct device_node *np_config;
1131
1132 num = be32_to_cpu(*list++);
1133 bank = bank_num_to_bank(info, num);
1134 if (IS_ERR(bank))
1135 return PTR_ERR(bank);
1136
1137 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
1138 grp->data[j].func = be32_to_cpu(*list++);
1139
1140 phandle = list++;
1141 if (!phandle)
1142 return -EINVAL;
1143
1144 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
1145 ret = pinconf_generic_parse_dt_config(np_config,
1146 &grp->data[j].configs, &grp->data[j].nconfigs);
1147 if (ret)
1148 return ret;
1149 }
1150
1151 return 0;
1152}
1153
1154static int rockchip_pinctrl_parse_functions(struct device_node *np,
1155 struct rockchip_pinctrl *info,
1156 u32 index)
1157{
1158 struct device_node *child;
1159 struct rockchip_pmx_func *func;
1160 struct rockchip_pin_group *grp;
1161 int ret;
1162 static u32 grp_index;
1163 u32 i = 0;
1164
1165 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1166
1167 func = &info->functions[index];
1168
1169 /* Initialise function */
1170 func->name = np->name;
1171 func->ngroups = of_get_child_count(np);
1172 if (func->ngroups <= 0)
1173 return 0;
1174
1175 func->groups = devm_kzalloc(info->dev,
1176 func->ngroups * sizeof(char *), GFP_KERNEL);
1177 if (!func->groups)
1178 return -ENOMEM;
1179
1180 for_each_child_of_node(np, child) {
1181 func->groups[i] = child->name;
1182 grp = &info->groups[grp_index++];
1183 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
1184 if (ret)
1185 return ret;
1186 }
1187
1188 return 0;
1189}
1190
1191static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
1192 struct rockchip_pinctrl *info)
1193{
1194 struct device *dev = &pdev->dev;
1195 struct device_node *np = dev->of_node;
1196 struct device_node *child;
1197 int ret;
1198 int i;
1199
1200 rockchip_pinctrl_child_count(info, np);
1201
1202 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1203 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1204
1205 info->functions = devm_kzalloc(dev, info->nfunctions *
1206 sizeof(struct rockchip_pmx_func),
1207 GFP_KERNEL);
1208 if (!info->functions) {
1209 dev_err(dev, "failed to allocate memory for function list\n");
1210 return -EINVAL;
1211 }
1212
1213 info->groups = devm_kzalloc(dev, info->ngroups *
1214 sizeof(struct rockchip_pin_group),
1215 GFP_KERNEL);
1216 if (!info->groups) {
1217 dev_err(dev, "failed allocate memory for ping group list\n");
1218 return -EINVAL;
1219 }
1220
1221 i = 0;
1222
1223 for_each_child_of_node(np, child) {
Heiko Stübner65fca612013-10-16 01:07:49 +02001224 if (of_match_node(rockchip_bank_match, child))
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001225 continue;
Heiko Stübner65fca612013-10-16 01:07:49 +02001226
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001227 ret = rockchip_pinctrl_parse_functions(child, info, i++);
1228 if (ret) {
1229 dev_err(&pdev->dev, "failed to parse function\n");
1230 return ret;
1231 }
1232 }
1233
1234 return 0;
1235}
1236
1237static int rockchip_pinctrl_register(struct platform_device *pdev,
1238 struct rockchip_pinctrl *info)
1239{
1240 struct pinctrl_desc *ctrldesc = &info->pctl;
1241 struct pinctrl_pin_desc *pindesc, *pdesc;
1242 struct rockchip_pin_bank *pin_bank;
1243 int pin, bank, ret;
1244 int k;
1245
1246 ctrldesc->name = "rockchip-pinctrl";
1247 ctrldesc->owner = THIS_MODULE;
1248 ctrldesc->pctlops = &rockchip_pctrl_ops;
1249 ctrldesc->pmxops = &rockchip_pmx_ops;
1250 ctrldesc->confops = &rockchip_pinconf_ops;
1251
1252 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
1253 info->ctrl->nr_pins, GFP_KERNEL);
1254 if (!pindesc) {
1255 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
1256 return -ENOMEM;
1257 }
1258 ctrldesc->pins = pindesc;
1259 ctrldesc->npins = info->ctrl->nr_pins;
1260
1261 pdesc = pindesc;
1262 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
1263 pin_bank = &info->ctrl->pin_banks[bank];
1264 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
1265 pdesc->number = k;
1266 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
1267 pin_bank->name, pin);
1268 pdesc++;
1269 }
1270 }
1271
Doug Anderson0fb7dcb2014-10-21 10:47:34 -07001272 ret = rockchip_pinctrl_parse_dt(pdev, info);
1273 if (ret)
1274 return ret;
1275
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001276 info->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, info);
1277 if (!info->pctl_dev) {
1278 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1279 return -EINVAL;
1280 }
1281
1282 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
1283 pin_bank = &info->ctrl->pin_banks[bank];
1284 pin_bank->grange.name = pin_bank->name;
1285 pin_bank->grange.id = bank;
1286 pin_bank->grange.pin_base = pin_bank->pin_base;
1287 pin_bank->grange.base = pin_bank->gpio_chip.base;
1288 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
1289 pin_bank->grange.gc = &pin_bank->gpio_chip;
1290 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
1291 }
1292
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001293 return 0;
1294}
1295
1296/*
1297 * GPIO handling
1298 */
1299
Axel Lin0351c282013-08-27 22:30:17 +08001300static int rockchip_gpio_request(struct gpio_chip *chip, unsigned offset)
1301{
1302 return pinctrl_request_gpio(chip->base + offset);
1303}
1304
1305static void rockchip_gpio_free(struct gpio_chip *chip, unsigned offset)
1306{
1307 pinctrl_free_gpio(chip->base + offset);
1308}
1309
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001310static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
1311{
1312 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1313 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
1314 unsigned long flags;
1315 u32 data;
1316
1317 spin_lock_irqsave(&bank->slock, flags);
1318
1319 data = readl(reg);
1320 data &= ~BIT(offset);
1321 if (value)
1322 data |= BIT(offset);
1323 writel(data, reg);
1324
1325 spin_unlock_irqrestore(&bank->slock, flags);
1326}
1327
1328/*
1329 * Returns the level of the pin for input direction and setting of the DR
1330 * register for output gpios.
1331 */
1332static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
1333{
1334 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1335 u32 data;
1336
1337 data = readl(bank->reg_base + GPIO_EXT_PORT);
1338 data >>= offset;
1339 data &= 1;
1340 return data;
1341}
1342
1343/*
1344 * gpiolib gpio_direction_input callback function. The setting of the pin
1345 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
1346 * interface.
1347 */
1348static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
1349{
1350 return pinctrl_gpio_direction_input(gc->base + offset);
1351}
1352
1353/*
1354 * gpiolib gpio_direction_output callback function. The setting of the pin
1355 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
1356 * interface.
1357 */
1358static int rockchip_gpio_direction_output(struct gpio_chip *gc,
1359 unsigned offset, int value)
1360{
1361 rockchip_gpio_set(gc, offset, value);
1362 return pinctrl_gpio_direction_output(gc->base + offset);
1363}
1364
1365/*
1366 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
1367 * and a virtual IRQ, if not already present.
1368 */
1369static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
1370{
1371 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1372 unsigned int virq;
1373
1374 if (!bank->domain)
1375 return -ENXIO;
1376
1377 virq = irq_create_mapping(bank->domain, offset);
1378
1379 return (virq) ? : -ENXIO;
1380}
1381
1382static const struct gpio_chip rockchip_gpiolib_chip = {
Axel Lin0351c282013-08-27 22:30:17 +08001383 .request = rockchip_gpio_request,
1384 .free = rockchip_gpio_free,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001385 .set = rockchip_gpio_set,
1386 .get = rockchip_gpio_get,
1387 .direction_input = rockchip_gpio_direction_input,
1388 .direction_output = rockchip_gpio_direction_output,
1389 .to_irq = rockchip_gpio_to_irq,
1390 .owner = THIS_MODULE,
1391};
1392
1393/*
1394 * Interrupt handling
1395 */
1396
1397static void rockchip_irq_demux(unsigned int irq, struct irq_desc *desc)
1398{
1399 struct irq_chip *chip = irq_get_chip(irq);
1400 struct rockchip_pin_bank *bank = irq_get_handler_data(irq);
Heiko Stübner5a927502013-10-16 01:09:08 +02001401 u32 polarity = 0, data = 0;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001402 u32 pend;
Heiko Stübner5a927502013-10-16 01:09:08 +02001403 bool edge_changed = false;
Doug Andersonfab262f2014-10-21 10:47:35 -07001404 unsigned long flags;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001405
1406 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
1407
1408 chained_irq_enter(chip, desc);
1409
1410 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
1411
Heiko Stübner5a927502013-10-16 01:09:08 +02001412 if (bank->toggle_edge_mode) {
1413 polarity = readl_relaxed(bank->reg_base +
1414 GPIO_INT_POLARITY);
1415 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
1416 }
1417
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001418 while (pend) {
1419 unsigned int virq;
1420
1421 irq = __ffs(pend);
1422 pend &= ~BIT(irq);
1423 virq = irq_linear_revmap(bank->domain, irq);
1424
1425 if (!virq) {
1426 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
1427 continue;
1428 }
1429
1430 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
1431
Heiko Stübner5a927502013-10-16 01:09:08 +02001432 /*
1433 * Triggering IRQ on both rising and falling edge
1434 * needs manual intervention.
1435 */
1436 if (bank->toggle_edge_mode & BIT(irq)) {
1437 if (data & BIT(irq))
1438 polarity &= ~BIT(irq);
1439 else
1440 polarity |= BIT(irq);
1441
1442 edge_changed = true;
1443 }
1444
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001445 generic_handle_irq(virq);
1446 }
1447
Heiko Stübner5a927502013-10-16 01:09:08 +02001448 if (bank->toggle_edge_mode && edge_changed) {
1449 /* Interrupt params should only be set with ints disabled */
Doug Andersonfab262f2014-10-21 10:47:35 -07001450 spin_lock_irqsave(&bank->slock, flags);
1451
Heiko Stübner5a927502013-10-16 01:09:08 +02001452 data = readl_relaxed(bank->reg_base + GPIO_INTEN);
1453 writel_relaxed(0, bank->reg_base + GPIO_INTEN);
1454 writel(polarity, bank->reg_base + GPIO_INT_POLARITY);
1455 writel(data, bank->reg_base + GPIO_INTEN);
Doug Andersonfab262f2014-10-21 10:47:35 -07001456
1457 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübner5a927502013-10-16 01:09:08 +02001458 }
1459
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001460 chained_irq_exit(chip, desc);
1461}
1462
1463static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
1464{
1465 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1466 struct rockchip_pin_bank *bank = gc->private;
1467 u32 mask = BIT(d->hwirq);
1468 u32 polarity;
1469 u32 level;
1470 u32 data;
Doug Andersonfab262f2014-10-21 10:47:35 -07001471 unsigned long flags;
Heiko Stübner14797182014-03-26 00:57:00 +01001472 int ret;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001473
Heiko Stübner5a927502013-10-16 01:09:08 +02001474 /* make sure the pin is configured as gpio input */
Heiko Stübner14797182014-03-26 00:57:00 +01001475 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
1476 if (ret < 0)
1477 return ret;
1478
Doug Andersonfab262f2014-10-21 10:47:35 -07001479 spin_lock_irqsave(&bank->slock, flags);
1480
Heiko Stübner5a927502013-10-16 01:09:08 +02001481 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1482 data &= ~mask;
1483 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
1484
Doug Andersonfab262f2014-10-21 10:47:35 -07001485 spin_unlock_irqrestore(&bank->slock, flags);
1486
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001487 if (type & IRQ_TYPE_EDGE_BOTH)
1488 __irq_set_handler_locked(d->irq, handle_edge_irq);
1489 else
1490 __irq_set_handler_locked(d->irq, handle_level_irq);
1491
Doug Andersonfab262f2014-10-21 10:47:35 -07001492 spin_lock_irqsave(&bank->slock, flags);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001493 irq_gc_lock(gc);
1494
1495 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
1496 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
1497
1498 switch (type) {
Heiko Stübner5a927502013-10-16 01:09:08 +02001499 case IRQ_TYPE_EDGE_BOTH:
1500 bank->toggle_edge_mode |= mask;
1501 level |= mask;
1502
1503 /*
1504 * Determine gpio state. If 1 next interrupt should be falling
1505 * otherwise rising.
1506 */
1507 data = readl(bank->reg_base + GPIO_EXT_PORT);
1508 if (data & mask)
1509 polarity &= ~mask;
1510 else
1511 polarity |= mask;
1512 break;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001513 case IRQ_TYPE_EDGE_RISING:
Heiko Stübner5a927502013-10-16 01:09:08 +02001514 bank->toggle_edge_mode &= ~mask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001515 level |= mask;
1516 polarity |= mask;
1517 break;
1518 case IRQ_TYPE_EDGE_FALLING:
Heiko Stübner5a927502013-10-16 01:09:08 +02001519 bank->toggle_edge_mode &= ~mask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001520 level |= mask;
1521 polarity &= ~mask;
1522 break;
1523 case IRQ_TYPE_LEVEL_HIGH:
Heiko Stübner5a927502013-10-16 01:09:08 +02001524 bank->toggle_edge_mode &= ~mask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001525 level &= ~mask;
1526 polarity |= mask;
1527 break;
1528 case IRQ_TYPE_LEVEL_LOW:
Heiko Stübner5a927502013-10-16 01:09:08 +02001529 bank->toggle_edge_mode &= ~mask;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001530 level &= ~mask;
1531 polarity &= ~mask;
1532 break;
1533 default:
Axel Lin7cc5f972013-06-23 08:48:34 +08001534 irq_gc_unlock(gc);
Doug Andersonfab262f2014-10-21 10:47:35 -07001535 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001536 return -EINVAL;
1537 }
1538
1539 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
1540 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
1541
1542 irq_gc_unlock(gc);
Doug Andersonfab262f2014-10-21 10:47:35 -07001543 spin_unlock_irqrestore(&bank->slock, flags);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001544
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001545 return 0;
1546}
1547
Doug Anderson68bda472014-11-19 14:51:32 -08001548static void rockchip_irq_suspend(struct irq_data *d)
1549{
1550 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1551 struct rockchip_pin_bank *bank = gc->private;
1552
1553 bank->saved_enables = irq_reg_readl(gc, GPIO_INTEN);
1554 irq_reg_writel(gc, gc->wake_active, GPIO_INTEN);
1555}
1556
1557static void rockchip_irq_resume(struct irq_data *d)
1558{
1559 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1560 struct rockchip_pin_bank *bank = gc->private;
1561
1562 irq_reg_writel(gc, bank->saved_enables, GPIO_INTEN);
1563}
1564
Doug Andersonf2dd0282014-11-19 14:51:33 -08001565static void rockchip_irq_disable(struct irq_data *d)
1566{
1567 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1568 u32 val;
1569
1570 irq_gc_lock(gc);
1571
1572 val = irq_reg_readl(gc, GPIO_INTEN);
1573 val &= ~d->mask;
1574 irq_reg_writel(gc, val, GPIO_INTEN);
1575
1576 irq_gc_unlock(gc);
1577}
1578
1579static void rockchip_irq_enable(struct irq_data *d)
1580{
1581 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1582 u32 val;
1583
1584 irq_gc_lock(gc);
1585
1586 val = irq_reg_readl(gc, GPIO_INTEN);
1587 val |= d->mask;
1588 irq_reg_writel(gc, val, GPIO_INTEN);
1589
1590 irq_gc_unlock(gc);
1591}
1592
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001593static int rockchip_interrupts_register(struct platform_device *pdev,
1594 struct rockchip_pinctrl *info)
1595{
1596 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1597 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1598 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
1599 struct irq_chip_generic *gc;
1600 int ret;
1601 int i;
1602
1603 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1604 if (!bank->valid) {
1605 dev_warn(&pdev->dev, "bank %s is not valid\n",
1606 bank->name);
1607 continue;
1608 }
1609
1610 bank->domain = irq_domain_add_linear(bank->of_node, 32,
1611 &irq_generic_chip_ops, NULL);
1612 if (!bank->domain) {
1613 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
1614 bank->name);
1615 continue;
1616 }
1617
1618 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
1619 "rockchip_gpio_irq", handle_level_irq,
1620 clr, 0, IRQ_GC_INIT_MASK_CACHE);
1621 if (ret) {
1622 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
1623 bank->name);
1624 irq_domain_remove(bank->domain);
1625 continue;
1626 }
1627
1628 gc = irq_get_domain_generic_chip(bank->domain, 0);
1629 gc->reg_base = bank->reg_base;
1630 gc->private = bank;
Doug Andersonf2dd0282014-11-19 14:51:33 -08001631 gc->chip_types[0].regs.mask = GPIO_INTMASK;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001632 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
1633 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
Doug Andersonf2dd0282014-11-19 14:51:33 -08001634 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
1635 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
1636 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
1637 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001638 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
Doug Anderson68bda472014-11-19 14:51:32 -08001639 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
1640 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001641 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
Doug Anderson876d7162014-10-21 10:47:32 -07001642 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001643
1644 irq_set_handler_data(bank->irq, bank);
1645 irq_set_chained_handler(bank->irq, rockchip_irq_demux);
1646 }
1647
1648 return 0;
1649}
1650
1651static int rockchip_gpiolib_register(struct platform_device *pdev,
1652 struct rockchip_pinctrl *info)
1653{
1654 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1655 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1656 struct gpio_chip *gc;
1657 int ret;
1658 int i;
1659
1660 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1661 if (!bank->valid) {
1662 dev_warn(&pdev->dev, "bank %s is not valid\n",
1663 bank->name);
1664 continue;
1665 }
1666
1667 bank->gpio_chip = rockchip_gpiolib_chip;
1668
1669 gc = &bank->gpio_chip;
1670 gc->base = bank->pin_base;
1671 gc->ngpio = bank->nr_pins;
1672 gc->dev = &pdev->dev;
1673 gc->of_node = bank->of_node;
1674 gc->label = bank->name;
1675
1676 ret = gpiochip_add(gc);
1677 if (ret) {
1678 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
1679 gc->label, ret);
1680 goto fail;
1681 }
1682 }
1683
1684 rockchip_interrupts_register(pdev, info);
1685
1686 return 0;
1687
1688fail:
1689 for (--i, --bank; i >= 0; --i, --bank) {
1690 if (!bank->valid)
1691 continue;
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001692 gpiochip_remove(&bank->gpio_chip);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001693 }
1694 return ret;
1695}
1696
1697static int rockchip_gpiolib_unregister(struct platform_device *pdev,
1698 struct rockchip_pinctrl *info)
1699{
1700 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1701 struct rockchip_pin_bank *bank = ctrl->pin_banks;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001702 int i;
1703
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001704 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001705 if (!bank->valid)
1706 continue;
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001707 gpiochip_remove(&bank->gpio_chip);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001708 }
1709
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001710 return 0;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001711}
1712
1713static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
Heiko Stübner622f3232014-05-05 13:58:46 +02001714 struct rockchip_pinctrl *info)
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001715{
1716 struct resource res;
Heiko Stübner751a99a2014-05-05 13:58:20 +02001717 void __iomem *base;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001718
1719 if (of_address_to_resource(bank->of_node, 0, &res)) {
Heiko Stübner622f3232014-05-05 13:58:46 +02001720 dev_err(info->dev, "cannot find IO resource for bank\n");
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001721 return -ENOENT;
1722 }
1723
Heiko Stübner622f3232014-05-05 13:58:46 +02001724 bank->reg_base = devm_ioremap_resource(info->dev, &res);
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001725 if (IS_ERR(bank->reg_base))
1726 return PTR_ERR(bank->reg_base);
1727
Heiko Stübner6ca52742013-10-16 01:08:42 +02001728 /*
1729 * special case, where parts of the pull setting-registers are
1730 * part of the PMU register space
1731 */
1732 if (of_device_is_compatible(bank->of_node,
1733 "rockchip,rk3188-gpio-bank0")) {
Heiko Stübnera658efa2014-05-05 13:59:30 +02001734 struct device_node *node;
Heiko Stübnerbfc7a422014-05-05 13:58:00 +02001735
Heiko Stübnera658efa2014-05-05 13:59:30 +02001736 node = of_parse_phandle(bank->of_node->parent,
1737 "rockchip,pmu", 0);
1738 if (!node) {
1739 if (of_address_to_resource(bank->of_node, 1, &res)) {
1740 dev_err(info->dev, "cannot find IO resource for bank\n");
1741 return -ENOENT;
1742 }
Heiko Stübner6ca52742013-10-16 01:08:42 +02001743
Heiko Stübnera658efa2014-05-05 13:59:30 +02001744 base = devm_ioremap_resource(info->dev, &res);
1745 if (IS_ERR(base))
1746 return PTR_ERR(base);
1747 rockchip_regmap_config.max_register =
1748 resource_size(&res) - 4;
1749 rockchip_regmap_config.name =
1750 "rockchip,rk3188-gpio-bank0-pull";
1751 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
1752 base,
1753 &rockchip_regmap_config);
1754 }
Heiko Stübner6ca52742013-10-16 01:08:42 +02001755 }
Heiko Stübner65fca612013-10-16 01:07:49 +02001756
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001757 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
1758
1759 bank->clk = of_clk_get(bank->of_node, 0);
1760 if (IS_ERR(bank->clk))
1761 return PTR_ERR(bank->clk);
1762
1763 return clk_prepare_enable(bank->clk);
1764}
1765
1766static const struct of_device_id rockchip_pinctrl_dt_match[];
1767
1768/* retrieve the soc specific data */
1769static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
1770 struct rockchip_pinctrl *d,
1771 struct platform_device *pdev)
1772{
1773 const struct of_device_id *match;
1774 struct device_node *node = pdev->dev.of_node;
1775 struct device_node *np;
1776 struct rockchip_pin_ctrl *ctrl;
1777 struct rockchip_pin_bank *bank;
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001778 int grf_offs, pmu_offs, i, j;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001779
1780 match = of_match_node(rockchip_pinctrl_dt_match, node);
1781 ctrl = (struct rockchip_pin_ctrl *)match->data;
1782
1783 for_each_child_of_node(node, np) {
1784 if (!of_find_property(np, "gpio-controller", NULL))
1785 continue;
1786
1787 bank = ctrl->pin_banks;
1788 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1789 if (!strcmp(bank->name, np->name)) {
1790 bank->of_node = np;
1791
Heiko Stübner622f3232014-05-05 13:58:46 +02001792 if (!rockchip_get_bank_data(bank, d))
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001793 bank->valid = true;
1794
1795 break;
1796 }
1797 }
1798 }
1799
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001800 grf_offs = ctrl->grf_mux_offset;
1801 pmu_offs = ctrl->pmu_mux_offset;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001802 bank = ctrl->pin_banks;
1803 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001804 int bank_pins = 0;
1805
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001806 spin_lock_init(&bank->slock);
1807 bank->drvdata = d;
1808 bank->pin_base = ctrl->nr_pins;
1809 ctrl->nr_pins += bank->nr_pins;
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001810
1811 /* calculate iomux offsets */
1812 for (j = 0; j < 4; j++) {
1813 struct rockchip_iomux *iom = &bank->iomux[j];
Heiko Stübner03716e12014-06-16 01:36:57 +02001814 int inc;
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001815
1816 if (bank_pins >= bank->nr_pins)
1817 break;
1818
1819 /* preset offset value, set new start value */
1820 if (iom->offset >= 0) {
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001821 if (iom->type & IOMUX_SOURCE_PMU)
1822 pmu_offs = iom->offset;
1823 else
1824 grf_offs = iom->offset;
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001825 } else { /* set current offset */
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001826 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
1827 pmu_offs : grf_offs;
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001828 }
1829
1830 dev_dbg(d->dev, "bank %d, iomux %d has offset 0x%x\n",
1831 i, j, iom->offset);
1832
1833 /*
1834 * Increase offset according to iomux width.
Heiko Stübner03716e12014-06-16 01:36:57 +02001835 * 4bit iomux'es are spread over two registers.
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001836 */
Heiko Stübner03716e12014-06-16 01:36:57 +02001837 inc = (iom->type & IOMUX_WIDTH_4BIT) ? 8 : 4;
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001838 if (iom->type & IOMUX_SOURCE_PMU)
1839 pmu_offs += inc;
1840 else
1841 grf_offs += inc;
Heiko Stübner6bc0d122014-06-16 01:36:33 +02001842
1843 bank_pins += 8;
1844 }
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001845 }
1846
1847 return ctrl;
1848}
1849
Chris Zhong8dca9332014-10-29 19:52:00 +08001850#define RK3288_GRF_GPIO6C_IOMUX 0x64
1851#define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
1852
1853static u32 rk3288_grf_gpio6c_iomux;
1854
Chris Zhong9198f502014-10-29 19:51:59 +08001855static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
1856{
1857 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
Chris Zhong8dca9332014-10-29 19:52:00 +08001858 int ret = pinctrl_force_sleep(info->pctl_dev);
Chris Zhong9198f502014-10-29 19:51:59 +08001859
Chris Zhong8dca9332014-10-29 19:52:00 +08001860 if (ret)
1861 return ret;
1862
1863 /*
1864 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
1865 * the setting here, and restore it at resume.
1866 */
1867 if (info->ctrl->type == RK3288) {
1868 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
1869 &rk3288_grf_gpio6c_iomux);
1870 if (ret) {
1871 pinctrl_force_default(info->pctl_dev);
1872 return ret;
1873 }
1874 }
1875
1876 return 0;
Chris Zhong9198f502014-10-29 19:51:59 +08001877}
1878
1879static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
1880{
1881 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
Chris Zhong8dca9332014-10-29 19:52:00 +08001882 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
1883 rk3288_grf_gpio6c_iomux |
1884 GPIO6C6_SEL_WRITE_ENABLE);
1885
1886 if (ret)
1887 return ret;
Chris Zhong9198f502014-10-29 19:51:59 +08001888
1889 return pinctrl_force_default(info->pctl_dev);
1890}
1891
1892static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
1893 rockchip_pinctrl_resume);
1894
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001895static int rockchip_pinctrl_probe(struct platform_device *pdev)
1896{
1897 struct rockchip_pinctrl *info;
1898 struct device *dev = &pdev->dev;
1899 struct rockchip_pin_ctrl *ctrl;
Heiko Stübner14dee862014-05-05 13:59:09 +02001900 struct device_node *np = pdev->dev.of_node, *node;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001901 struct resource *res;
Heiko Stübner751a99a2014-05-05 13:58:20 +02001902 void __iomem *base;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001903 int ret;
1904
1905 if (!dev->of_node) {
1906 dev_err(dev, "device tree node not found\n");
1907 return -ENODEV;
1908 }
1909
1910 info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL);
1911 if (!info)
1912 return -ENOMEM;
1913
Heiko Stübner622f3232014-05-05 13:58:46 +02001914 info->dev = dev;
1915
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001916 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
1917 if (!ctrl) {
1918 dev_err(dev, "driver data not available\n");
1919 return -EINVAL;
1920 }
1921 info->ctrl = ctrl;
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001922
Heiko Stübner1e747e52014-05-05 13:59:51 +02001923 node = of_parse_phandle(np, "rockchip,grf", 0);
1924 if (node) {
1925 info->regmap_base = syscon_node_to_regmap(node);
1926 if (IS_ERR(info->regmap_base))
1927 return PTR_ERR(info->regmap_base);
1928 } else {
1929 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Heiko Stübner751a99a2014-05-05 13:58:20 +02001930 base = devm_ioremap_resource(&pdev->dev, res);
1931 if (IS_ERR(base))
1932 return PTR_ERR(base);
1933
1934 rockchip_regmap_config.max_register = resource_size(res) - 4;
Heiko Stübner1e747e52014-05-05 13:59:51 +02001935 rockchip_regmap_config.name = "rockchip,pinctrl";
1936 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
1937 &rockchip_regmap_config);
1938
1939 /* to check for the old dt-bindings */
1940 info->reg_size = resource_size(res);
1941
1942 /* Honor the old binding, with pull registers as 2nd resource */
1943 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
1944 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1945 base = devm_ioremap_resource(&pdev->dev, res);
1946 if (IS_ERR(base))
1947 return PTR_ERR(base);
1948
1949 rockchip_regmap_config.max_register =
1950 resource_size(res) - 4;
1951 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
1952 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
1953 base,
1954 &rockchip_regmap_config);
1955 }
Heiko Stübner6ca52742013-10-16 01:08:42 +02001956 }
1957
Heiko Stübner14dee862014-05-05 13:59:09 +02001958 /* try to find the optional reference to the pmu syscon */
1959 node = of_parse_phandle(np, "rockchip,pmu", 0);
1960 if (node) {
1961 info->regmap_pmu = syscon_node_to_regmap(node);
1962 if (IS_ERR(info->regmap_pmu))
1963 return PTR_ERR(info->regmap_pmu);
1964 }
1965
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001966 ret = rockchip_gpiolib_register(pdev, info);
1967 if (ret)
1968 return ret;
1969
1970 ret = rockchip_pinctrl_register(pdev, info);
1971 if (ret) {
1972 rockchip_gpiolib_unregister(pdev, info);
1973 return ret;
1974 }
1975
1976 platform_set_drvdata(pdev, info);
1977
1978 return 0;
1979}
1980
1981static struct rockchip_pin_bank rk2928_pin_banks[] = {
1982 PIN_BANK(0, 32, "gpio0"),
1983 PIN_BANK(1, 32, "gpio1"),
1984 PIN_BANK(2, 32, "gpio2"),
1985 PIN_BANK(3, 32, "gpio3"),
1986};
1987
1988static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
1989 .pin_banks = rk2928_pin_banks,
1990 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
1991 .label = "RK2928-GPIO",
Heiko Stübnera2829262013-10-16 01:07:20 +02001992 .type = RK2928,
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02001993 .grf_mux_offset = 0xa8,
Heiko Stübnera2829262013-10-16 01:07:20 +02001994 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02001995};
1996
1997static struct rockchip_pin_bank rk3066a_pin_banks[] = {
1998 PIN_BANK(0, 32, "gpio0"),
1999 PIN_BANK(1, 32, "gpio1"),
2000 PIN_BANK(2, 32, "gpio2"),
2001 PIN_BANK(3, 32, "gpio3"),
2002 PIN_BANK(4, 32, "gpio4"),
2003 PIN_BANK(6, 16, "gpio6"),
2004};
2005
2006static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
2007 .pin_banks = rk3066a_pin_banks,
2008 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
2009 .label = "RK3066a-GPIO",
Heiko Stübnera2829262013-10-16 01:07:20 +02002010 .type = RK2928,
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02002011 .grf_mux_offset = 0xa8,
Heiko Stübnera2829262013-10-16 01:07:20 +02002012 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002013};
2014
2015static struct rockchip_pin_bank rk3066b_pin_banks[] = {
2016 PIN_BANK(0, 32, "gpio0"),
2017 PIN_BANK(1, 32, "gpio1"),
2018 PIN_BANK(2, 32, "gpio2"),
2019 PIN_BANK(3, 32, "gpio3"),
2020};
2021
2022static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
2023 .pin_banks = rk3066b_pin_banks,
2024 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
2025 .label = "RK3066b-GPIO",
Heiko Stübnera2829262013-10-16 01:07:20 +02002026 .type = RK3066B,
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02002027 .grf_mux_offset = 0x60,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002028};
2029
2030static struct rockchip_pin_bank rk3188_pin_banks[] = {
Heiko Stübnerfc72c922014-06-16 01:36:05 +02002031 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002032 PIN_BANK(1, 32, "gpio1"),
2033 PIN_BANK(2, 32, "gpio2"),
2034 PIN_BANK(3, 32, "gpio3"),
2035};
2036
2037static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
2038 .pin_banks = rk3188_pin_banks,
2039 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
2040 .label = "RK3188-GPIO",
Heiko Stübnera2829262013-10-16 01:07:20 +02002041 .type = RK3188,
Heiko Stübner95ec8ae2014-06-16 01:37:23 +02002042 .grf_mux_offset = 0x60,
Heiko Stübner6ca52742013-10-16 01:08:42 +02002043 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002044};
2045
Heiko Stübner304f0772014-06-16 01:38:14 +02002046static struct rockchip_pin_bank rk3288_pin_banks[] = {
2047 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
2048 IOMUX_SOURCE_PMU,
2049 IOMUX_SOURCE_PMU,
2050 IOMUX_UNROUTED
2051 ),
2052 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
2053 IOMUX_UNROUTED,
2054 IOMUX_UNROUTED,
2055 0
2056 ),
2057 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
2058 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
2059 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
2060 IOMUX_WIDTH_4BIT,
2061 0,
2062 0
2063 ),
2064 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
2065 0,
2066 0,
2067 IOMUX_UNROUTED
2068 ),
2069 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
2070 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2071 0,
2072 IOMUX_WIDTH_4BIT,
2073 IOMUX_UNROUTED
2074 ),
2075 PIN_BANK(8, 16, "gpio8"),
2076};
2077
2078static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
2079 .pin_banks = rk3288_pin_banks,
2080 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
2081 .label = "RK3288-GPIO",
Heiko Stübner66d750e2014-07-20 01:49:17 +02002082 .type = RK3288,
Heiko Stübner304f0772014-06-16 01:38:14 +02002083 .grf_mux_offset = 0x0,
2084 .pmu_mux_offset = 0x84,
2085 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
2086};
2087
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002088static const struct of_device_id rockchip_pinctrl_dt_match[] = {
2089 { .compatible = "rockchip,rk2928-pinctrl",
2090 .data = (void *)&rk2928_pin_ctrl },
2091 { .compatible = "rockchip,rk3066a-pinctrl",
2092 .data = (void *)&rk3066a_pin_ctrl },
2093 { .compatible = "rockchip,rk3066b-pinctrl",
2094 .data = (void *)&rk3066b_pin_ctrl },
2095 { .compatible = "rockchip,rk3188-pinctrl",
2096 .data = (void *)&rk3188_pin_ctrl },
Heiko Stübner304f0772014-06-16 01:38:14 +02002097 { .compatible = "rockchip,rk3288-pinctrl",
2098 .data = (void *)&rk3288_pin_ctrl },
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002099 {},
2100};
2101MODULE_DEVICE_TABLE(of, rockchip_pinctrl_dt_match);
2102
2103static struct platform_driver rockchip_pinctrl_driver = {
2104 .probe = rockchip_pinctrl_probe,
2105 .driver = {
2106 .name = "rockchip-pinctrl",
Chris Zhong9198f502014-10-29 19:51:59 +08002107 .pm = &rockchip_pinctrl_dev_pm_ops,
Axel Lin0be9e702013-08-23 14:27:53 +08002108 .of_match_table = rockchip_pinctrl_dt_match,
Heiko Stübnerd3e51162013-06-10 22:16:22 +02002109 },
2110};
2111
2112static int __init rockchip_pinctrl_drv_register(void)
2113{
2114 return platform_driver_register(&rockchip_pinctrl_driver);
2115}
2116postcore_initcall(rockchip_pinctrl_drv_register);
2117
2118MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
2119MODULE_DESCRIPTION("Rockchip pinctrl driver");
2120MODULE_LICENSE("GPL v2");