blob: 557d0f2a3031b1a0295fe60796ec17ea7817fc52 [file] [log] [blame]
Patrice Chotard0493e642013-01-08 10:41:02 +01001/*
2 * Copyright (C) ST-Ericsson SA 2013
3 *
4 * Author: Patrice Chotard <patrice.chotard@st.com>
5 * License terms: GNU General Public License (GPL) version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/err.h>
Lee Jonesf30a3832013-01-31 11:07:40 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010019#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/irq.h>
Lee Jonesac652d72013-01-31 10:43:00 +000022#include <linux/irqdomain.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010023#include <linux/interrupt.h>
24#include <linux/bitops.h>
25#include <linux/mfd/abx500.h>
26#include <linux/mfd/abx500/ab8500.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/consumer.h>
29#include <linux/pinctrl/pinmux.h>
30#include <linux/pinctrl/pinconf.h>
31#include <linux/pinctrl/pinconf-generic.h>
Patrice Chotard64a45c92013-06-20 16:04:59 +020032#include <linux/pinctrl/machine.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010033
34#include "pinctrl-abx500.h"
Linus Walleij3a198052014-07-11 14:57:06 +020035#include "../core.h"
36#include "../pinconf.h"
Linus Walleijb07f92a2014-09-30 11:11:50 +020037#include "../pinctrl-utils.h"
Patrice Chotard0493e642013-01-08 10:41:02 +010038
39/*
40 * The AB9540 and AB8540 GPIO support are extended versions
41 * of the AB8500 GPIO support.
42 * The AB9540 supports an additional (7th) register so that
43 * more GPIO may be configured and used.
44 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
45 * internal pull-up and pull-down capabilities.
46 */
47
48/*
49 * GPIO registers offset
50 * Bank: 0x10
51 */
52#define AB8500_GPIO_SEL1_REG 0x00
53#define AB8500_GPIO_SEL2_REG 0x01
54#define AB8500_GPIO_SEL3_REG 0x02
55#define AB8500_GPIO_SEL4_REG 0x03
56#define AB8500_GPIO_SEL5_REG 0x04
57#define AB8500_GPIO_SEL6_REG 0x05
58#define AB9540_GPIO_SEL7_REG 0x06
59
60#define AB8500_GPIO_DIR1_REG 0x10
61#define AB8500_GPIO_DIR2_REG 0x11
62#define AB8500_GPIO_DIR3_REG 0x12
63#define AB8500_GPIO_DIR4_REG 0x13
64#define AB8500_GPIO_DIR5_REG 0x14
65#define AB8500_GPIO_DIR6_REG 0x15
66#define AB9540_GPIO_DIR7_REG 0x16
67
68#define AB8500_GPIO_OUT1_REG 0x20
69#define AB8500_GPIO_OUT2_REG 0x21
70#define AB8500_GPIO_OUT3_REG 0x22
71#define AB8500_GPIO_OUT4_REG 0x23
72#define AB8500_GPIO_OUT5_REG 0x24
73#define AB8500_GPIO_OUT6_REG 0x25
74#define AB9540_GPIO_OUT7_REG 0x26
75
76#define AB8500_GPIO_PUD1_REG 0x30
77#define AB8500_GPIO_PUD2_REG 0x31
78#define AB8500_GPIO_PUD3_REG 0x32
79#define AB8500_GPIO_PUD4_REG 0x33
80#define AB8500_GPIO_PUD5_REG 0x34
81#define AB8500_GPIO_PUD6_REG 0x35
82#define AB9540_GPIO_PUD7_REG 0x36
83
84#define AB8500_GPIO_IN1_REG 0x40
85#define AB8500_GPIO_IN2_REG 0x41
86#define AB8500_GPIO_IN3_REG 0x42
87#define AB8500_GPIO_IN4_REG 0x43
88#define AB8500_GPIO_IN5_REG 0x44
89#define AB8500_GPIO_IN6_REG 0x45
90#define AB9540_GPIO_IN7_REG 0x46
91#define AB8540_GPIO_VINSEL_REG 0x47
92#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
93#define AB8500_GPIO_ALTFUN_REG 0x50
Patrice Chotard0493e642013-01-08 10:41:02 +010094#define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
95#define AB8540_GPIO_VINSEL_MASK 0x03
96#define AB8540_GPIOX_VBAT_START 51
97#define AB8540_GPIOX_VBAT_END 54
98
Patrice Chotardacd260b2013-06-24 14:41:45 +020099#define ABX500_GPIO_INPUT 0
100#define ABX500_GPIO_OUTPUT 1
101
Patrice Chotard0493e642013-01-08 10:41:02 +0100102struct abx500_pinctrl {
103 struct device *dev;
104 struct pinctrl_dev *pctldev;
105 struct abx500_pinctrl_soc_data *soc;
106 struct gpio_chip chip;
107 struct ab8500 *parent;
Patrice Chotard0493e642013-01-08 10:41:02 +0100108 struct abx500_gpio_irq_cluster *irq_cluster;
109 int irq_cluster_size;
Patrice Chotard0493e642013-01-08 10:41:02 +0100110};
111
112/**
113 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
114 * @chip: Member of the structure abx500_pinctrl
115 */
116static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
117{
118 return container_of(chip, struct abx500_pinctrl, chip);
119}
120
121static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000122 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100123{
124 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
125 u8 pos = offset % 8;
126 u8 val;
127 int ret;
128
129 reg += offset / 8;
130 ret = abx500_get_register_interruptible(pct->dev,
131 AB8500_MISC, reg, &val);
132
133 *bit = !!(val & BIT(pos));
134
135 if (ret < 0)
136 dev_err(pct->dev,
Patrice Chotard9be580a2013-06-24 14:41:46 +0200137 "%s read reg =%x, offset=%x failed (%d)\n",
138 __func__, reg, offset, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100139
140 return ret;
141}
142
143static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000144 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100145{
146 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
147 u8 pos = offset % 8;
148 int ret;
149
150 reg += offset / 8;
151 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000152 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100153 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200154 dev_err(pct->dev, "%s write reg, %x offset %x failed (%d)\n",
155 __func__, reg, offset, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000156
Patrice Chotard0493e642013-01-08 10:41:02 +0100157 return ret;
158}
Lee Jones83b423c2013-01-23 13:24:08 +0000159
Patrice Chotard0493e642013-01-08 10:41:02 +0100160/**
161 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000162 * @chip: Gpio device
163 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100164 */
165static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
166{
167 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
168 bool bit;
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200169 bool is_out;
170 u8 gpio_offset = offset - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100171 int ret;
172
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200173 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
174 gpio_offset, &is_out);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200175 if (ret < 0)
176 goto out;
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200177
178 if (is_out)
179 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
180 gpio_offset, &bit);
181 else
182 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
183 gpio_offset, &bit);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200184out:
Patrice Chotard0493e642013-01-08 10:41:02 +0100185 if (ret < 0) {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200186 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100187 return ret;
188 }
Lee Jones83b423c2013-01-23 13:24:08 +0000189
Patrice Chotard0493e642013-01-08 10:41:02 +0100190 return bit;
191}
192
193static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
194{
195 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
196 int ret;
197
198 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
199 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200200 dev_err(pct->dev, "%s write failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100201}
202
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200203static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
204 enum abx500_gpio_pull_updown *pull_updown)
205{
206 u8 pos;
207 u8 val;
208 int ret;
209 struct pullud *pullud;
210
211 if (!pct->soc->pullud) {
212 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
213 __func__);
214 ret = -EPERM;
215 goto out;
216 }
217
218 pullud = pct->soc->pullud;
219
220 if ((offset < pullud->first_pin)
221 || (offset > pullud->last_pin)) {
222 ret = -EINVAL;
223 goto out;
224 }
225
226 ret = abx500_get_register_interruptible(pct->dev,
227 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
228
229 pos = (offset - pullud->first_pin) << 1;
230 *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
231
232out:
233 if (ret < 0)
234 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
235
236 return ret;
237}
238
239static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
240 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100241{
242 u8 pos;
243 int ret;
244 struct pullud *pullud;
245
246 if (!pct->soc->pullud) {
247 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
248 __func__);
249 ret = -EPERM;
250 goto out;
251 }
252
253 pullud = pct->soc->pullud;
254
255 if ((offset < pullud->first_pin)
256 || (offset > pullud->last_pin)) {
257 ret = -EINVAL;
258 goto out;
259 }
Patrice Chotard10a8be52013-05-24 14:06:29 +0200260 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100261
262 ret = abx500_mask_and_set_register_interruptible(pct->dev,
263 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
264 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
265
266out:
267 if (ret < 0)
268 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000269
Patrice Chotard0493e642013-01-08 10:41:02 +0100270 return ret;
271}
272
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200273static bool abx500_pullud_supported(struct gpio_chip *chip, unsigned gpio)
274{
275 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
276 struct pullud *pullud = pct->soc->pullud;
277
278 return (pullud &&
279 gpio >= pullud->first_pin &&
280 gpio <= pullud->last_pin);
281}
282
Patrice Chotard0493e642013-01-08 10:41:02 +0100283static int abx500_gpio_direction_output(struct gpio_chip *chip,
284 unsigned offset,
285 int val)
286{
287 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100288 unsigned gpio;
289 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000290
Patrice Chotard0493e642013-01-08 10:41:02 +0100291 /* set direction as output */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200292 ret = abx500_gpio_set_bits(chip,
293 AB8500_GPIO_DIR1_REG,
294 offset,
295 ABX500_GPIO_OUTPUT);
Patrice Chotard0493e642013-01-08 10:41:02 +0100296 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200297 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100298
299 /* disable pull down */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200300 ret = abx500_gpio_set_bits(chip,
301 AB8500_GPIO_PUD1_REG,
302 offset,
303 ABX500_GPIO_PULL_NONE);
Patrice Chotard0493e642013-01-08 10:41:02 +0100304 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200305 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100306
307 /* if supported, disable both pull down and pull up */
308 gpio = offset + 1;
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200309 if (abx500_pullud_supported(chip, gpio)) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200310 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100311 gpio,
312 ABX500_GPIO_PULL_NONE);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200313 }
314out:
315 if (ret < 0) {
316 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
317 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100318 }
Lee Jones83b423c2013-01-23 13:24:08 +0000319
Patrice Chotard0493e642013-01-08 10:41:02 +0100320 /* set the output as 1 or 0 */
321 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
322}
323
324static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
325{
326 /* set the register as input */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200327 return abx500_gpio_set_bits(chip,
328 AB8500_GPIO_DIR1_REG,
329 offset,
330 ABX500_GPIO_INPUT);
Patrice Chotard0493e642013-01-08 10:41:02 +0100331}
332
333static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
334{
335 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000336 /* The AB8500 GPIO numbers are off by one */
337 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000338 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100339 int i;
340
341 for (i = 0; i < pct->irq_cluster_size; i++) {
342 struct abx500_gpio_irq_cluster *cluster =
343 &pct->irq_cluster[i];
344
Lee Jonesa6a16d22013-01-31 09:57:52 +0000345 if (gpio >= cluster->start && gpio <= cluster->end) {
346 /*
347 * The ABx500 GPIO's associated IRQs are clustered together
348 * throughout the interrupt numbers at irregular intervals.
349 * To solve this quandry, we have placed the read-in values
350 * into the cluster information table.
351 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100352 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000353 return irq_create_mapping(pct->parent->domain, hwirq);
354 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100355 }
356
357 return -EINVAL;
358}
359
360static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000361 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100362{
363 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
364 struct alternate_functions af = pct->soc->alternate_functions[gpio];
365 int ret;
366 int val;
367 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000368
Patrice Chotard0493e642013-01-08 10:41:02 +0100369 const char *modes[] = {
370 [ABX500_DEFAULT] = "default",
371 [ABX500_ALT_A] = "altA",
372 [ABX500_ALT_B] = "altB",
373 [ABX500_ALT_C] = "altC",
374 };
375
376 /* sanity check */
377 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
378 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
379 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
380 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
381 modes[alt_setting]);
382 return -EINVAL;
383 }
384
385 /* on ABx5xx, there is no GPIO0, so adjust the offset */
386 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000387
Patrice Chotard0493e642013-01-08 10:41:02 +0100388 switch (alt_setting) {
389 case ABX500_DEFAULT:
390 /*
391 * for ABx5xx family, default mode is always selected by
392 * writing 0 to GPIOSELx register, except for pins which
393 * support at least ALT_B mode, default mode is selected
394 * by writing 1 to GPIOSELx register
395 */
396 val = 0;
397 if (af.alt_bit1 != UNUSED)
398 val++;
399
400 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
401 offset, val);
402 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000403
Patrice Chotard0493e642013-01-08 10:41:02 +0100404 case ABX500_ALT_A:
405 /*
406 * for ABx5xx family, alt_a mode is always selected by
407 * writing 1 to GPIOSELx register, except for pins which
408 * support at least ALT_B mode, alt_a mode is selected
409 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
410 * register
411 */
412 if (af.alt_bit1 != UNUSED) {
413 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
414 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200415 if (ret < 0)
416 goto out;
417
Patrice Chotard0493e642013-01-08 10:41:02 +0100418 ret = abx500_gpio_set_bits(chip,
419 AB8500_GPIO_ALTFUN_REG,
420 af.alt_bit1,
Dan Carpenterc5908542013-11-10 02:35:56 +0300421 !!(af.alta_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200422 if (ret < 0)
423 goto out;
424
Patrice Chotard0493e642013-01-08 10:41:02 +0100425 if (af.alt_bit2 != UNUSED)
426 ret = abx500_gpio_set_bits(chip,
427 AB8500_GPIO_ALTFUN_REG,
428 af.alt_bit2,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300429 !!(af.alta_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100430 } else
431 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
432 offset, 1);
433 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000434
Patrice Chotard0493e642013-01-08 10:41:02 +0100435 case ABX500_ALT_B:
436 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
437 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200438 if (ret < 0)
439 goto out;
440
Patrice Chotard0493e642013-01-08 10:41:02 +0100441 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenterc5908542013-11-10 02:35:56 +0300442 af.alt_bit1, !!(af.altb_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200443 if (ret < 0)
444 goto out;
445
Patrice Chotard0493e642013-01-08 10:41:02 +0100446 if (af.alt_bit2 != UNUSED)
447 ret = abx500_gpio_set_bits(chip,
448 AB8500_GPIO_ALTFUN_REG,
449 af.alt_bit2,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300450 !!(af.altb_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100451 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000452
Patrice Chotard0493e642013-01-08 10:41:02 +0100453 case ABX500_ALT_C:
454 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
455 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200456 if (ret < 0)
457 goto out;
458
Patrice Chotard0493e642013-01-08 10:41:02 +0100459 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300460 af.alt_bit2, !!(af.altc_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200461 if (ret < 0)
462 goto out;
463
Patrice Chotard0493e642013-01-08 10:41:02 +0100464 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenterc5908542013-11-10 02:35:56 +0300465 af.alt_bit2, !!(af.altc_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100466 break;
467
468 default:
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900469 dev_dbg(pct->dev, "unknown alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000470
Patrice Chotard0493e642013-01-08 10:41:02 +0100471 return -EINVAL;
472 }
Patrice Chotard9be580a2013-06-24 14:41:46 +0200473out:
474 if (ret < 0)
475 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000476
Patrice Chotard0493e642013-01-08 10:41:02 +0100477 return ret;
478}
479
Patrice Chotard9be580a2013-06-24 14:41:46 +0200480static int abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000481 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100482{
483 u8 mode;
484 bool bit_mode;
485 bool alt_bit1;
486 bool alt_bit2;
487 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
488 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100489 /* on ABx5xx, there is no GPIO0, so adjust the offset */
490 unsigned offset = gpio - 1;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200491 int ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100492
493 /*
494 * if gpiosel_bit is set to unused,
495 * it means no GPIO or special case
496 */
497 if (af.gpiosel_bit == UNUSED)
498 return ABX500_DEFAULT;
499
500 /* read GpioSelx register */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200501 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100502 af.gpiosel_bit, &bit_mode);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200503 if (ret < 0)
504 goto out;
505
Patrice Chotard0493e642013-01-08 10:41:02 +0100506 mode = bit_mode;
507
508 /* sanity check */
509 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
510 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
511 dev_err(pct->dev,
512 "alt_bitX value not in correct range (-1 to 7)\n");
513 return -EINVAL;
514 }
Lee Jones83b423c2013-01-23 13:24:08 +0000515
Patrice Chotard0493e642013-01-08 10:41:02 +0100516 /* if alt_bit2 is used, alt_bit1 must be used too */
517 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
518 dev_err(pct->dev,
519 "if alt_bit2 is used, alt_bit1 can't be unused\n");
520 return -EINVAL;
521 }
522
523 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800524 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100525 return mode;
526 /*
527 * if pin GPIOSEL bit is set and pin supports alternate function,
528 * it means DEFAULT mode
529 */
530 if (mode)
531 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000532
Patrice Chotard0493e642013-01-08 10:41:02 +0100533 /*
534 * pin use the AlternatFunction register
535 * read alt_bit1 value
536 */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200537 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
Patrice Chotard0493e642013-01-08 10:41:02 +0100538 af.alt_bit1, &alt_bit1);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200539 if (ret < 0)
540 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100541
Patrice Chotard9be580a2013-06-24 14:41:46 +0200542 if (af.alt_bit2 != UNUSED) {
Patrice Chotard0493e642013-01-08 10:41:02 +0100543 /* read alt_bit2 value */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200544 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
545 af.alt_bit2,
Patrice Chotard0493e642013-01-08 10:41:02 +0100546 &alt_bit2);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200547 if (ret < 0)
548 goto out;
549 } else
Patrice Chotard0493e642013-01-08 10:41:02 +0100550 alt_bit2 = 0;
551
552 mode = (alt_bit2 << 1) + alt_bit1;
553 if (mode == af.alta_val)
554 return ABX500_ALT_A;
555 else if (mode == af.altb_val)
556 return ABX500_ALT_B;
557 else
558 return ABX500_ALT_C;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200559
560out:
561 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
562 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100563}
564
565#ifdef CONFIG_DEBUG_FS
566
567#include <linux/seq_file.h>
568
569static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000570 struct pinctrl_dev *pctldev,
571 struct gpio_chip *chip,
572 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100573{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200574 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100575 const char *label = gpiochip_is_requested(chip, offset - 1);
576 u8 gpio_offset = offset - 1;
577 int mode = -1;
578 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200579 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200580 enum abx500_gpio_pull_updown pud = 0;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200581 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000582
Patrice Chotard0493e642013-01-08 10:41:02 +0100583 const char *modes[] = {
584 [ABX500_DEFAULT] = "default",
585 [ABX500_ALT_A] = "altA",
586 [ABX500_ALT_B] = "altB",
587 [ABX500_ALT_C] = "altC",
588 };
589
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200590 const char *pull_up_down[] = {
591 [ABX500_GPIO_PULL_DOWN] = "pull down",
592 [ABX500_GPIO_PULL_NONE] = "pull none",
593 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
594 [ABX500_GPIO_PULL_UP] = "pull up",
595 };
596
Patrice Chotard9be580a2013-06-24 14:41:46 +0200597 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
598 gpio_offset, &is_out);
599 if (ret < 0)
600 goto out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200601
602 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
603 gpio, label ?: "(none)",
604 is_out ? "out" : "in ");
605
606 if (!is_out) {
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200607 if (abx500_pullud_supported(chip, offset)) {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200608 ret = abx500_get_pull_updown(pct, offset, &pud);
609 if (ret < 0)
610 goto out;
611
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200612 seq_printf(s, " %-9s", pull_up_down[pud]);
613 } else {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200614 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
615 gpio_offset, &pd);
616 if (ret < 0)
617 goto out;
618
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200619 seq_printf(s, " %-9s", pull_up_down[pd]);
620 }
621 } else
622 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100623
Patrice CHOTARD1d54f0f2014-08-01 09:38:43 +0200624 mode = abx500_get_mode(pctldev, chip, offset);
Patrice Chotard0493e642013-01-08 10:41:02 +0100625
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200626 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200627
628out:
629 if (ret < 0)
630 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100631}
632
633static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
634{
635 unsigned i;
636 unsigned gpio = chip->base;
637 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
638 struct pinctrl_dev *pctldev = pct->pctldev;
639
640 for (i = 0; i < chip->ngpio; i++, gpio++) {
641 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
642 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
643 seq_printf(s, "\n");
644 }
645}
646
647#else
648static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000649 struct pinctrl_dev *pctldev,
650 struct gpio_chip *chip,
651 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100652{
653}
654#define abx500_gpio_dbg_show NULL
655#endif
656
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530657static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100658{
659 int gpio = chip->base + offset;
660
661 return pinctrl_request_gpio(gpio);
662}
663
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530664static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100665{
666 int gpio = chip->base + offset;
667
668 pinctrl_free_gpio(gpio);
669}
670
671static struct gpio_chip abx500gpio_chip = {
672 .label = "abx500-gpio",
673 .owner = THIS_MODULE,
674 .request = abx500_gpio_request,
675 .free = abx500_gpio_free,
676 .direction_input = abx500_gpio_direction_input,
677 .get = abx500_gpio_get,
678 .direction_output = abx500_gpio_direction_output,
679 .set = abx500_gpio_set,
680 .to_irq = abx500_gpio_to_irq,
681 .dbg_show = abx500_gpio_dbg_show,
682};
683
Patrice Chotard0493e642013-01-08 10:41:02 +0100684static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
685{
686 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
687
688 return pct->soc->nfunctions;
689}
690
691static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
692 unsigned function)
693{
694 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
695
696 return pct->soc->functions[function].name;
697}
698
699static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000700 unsigned function,
701 const char * const **groups,
702 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100703{
704 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
705
706 *groups = pct->soc->functions[function].groups;
707 *num_groups = pct->soc->functions[function].ngroups;
708
709 return 0;
710}
711
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200712static int abx500_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
713 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100714{
715 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
716 struct gpio_chip *chip = &pct->chip;
717 const struct abx500_pingroup *g;
718 int i;
719 int ret = 0;
720
721 g = &pct->soc->groups[group];
722 if (g->altsetting < 0)
723 return -EINVAL;
724
725 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
726
727 for (i = 0; i < g->npins; i++) {
728 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
729 g->pins[i], g->altsetting);
730
Patrice Chotard0493e642013-01-08 10:41:02 +0100731 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
732 }
Lee Jones83b423c2013-01-23 13:24:08 +0000733
Patrice Chotard9be580a2013-06-24 14:41:46 +0200734 if (ret < 0)
735 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
736
Patrice Chotard0493e642013-01-08 10:41:02 +0100737 return ret;
738}
739
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530740static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000741 struct pinctrl_gpio_range *range,
742 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100743{
744 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
745 const struct abx500_pinrange *p;
746 int ret;
747 int i;
748
749 /*
750 * Different ranges have different ways to enable GPIO function on a
751 * pin, so refer back to our local range type, where we handily define
752 * what altfunc enables GPIO for a certain pin.
753 */
754 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
755 p = &pct->soc->gpio_ranges[i];
756 if ((offset >= p->offset) &&
757 (offset < (p->offset + p->npins)))
758 break;
759 }
760
761 if (i == pct->soc->gpio_num_ranges) {
762 dev_err(pct->dev, "%s failed to locate range\n", __func__);
763 return -ENODEV;
764 }
765
766 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
767 p->altfunc, offset);
768
769 ret = abx500_set_mode(pct->pctldev, &pct->chip,
770 offset, p->altfunc);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200771 if (ret < 0)
Patrice Chotard0493e642013-01-08 10:41:02 +0100772 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
Patrice Chotard0493e642013-01-08 10:41:02 +0100773
774 return ret;
775}
776
777static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000778 struct pinctrl_gpio_range *range,
779 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100780{
781}
782
Laurent Pinchart022ab142013-02-16 10:25:07 +0100783static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100784 .get_functions_count = abx500_pmx_get_funcs_cnt,
785 .get_function_name = abx500_pmx_get_func_name,
786 .get_function_groups = abx500_pmx_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200787 .set_mux = abx500_pmx_set,
Patrice Chotard0493e642013-01-08 10:41:02 +0100788 .gpio_request_enable = abx500_gpio_request_enable,
789 .gpio_disable_free = abx500_gpio_disable_free,
Linus Walleija21763a2015-05-06 14:43:45 +0200790 .strict = true,
Patrice Chotard0493e642013-01-08 10:41:02 +0100791};
792
793static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
794{
795 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
796
797 return pct->soc->ngroups;
798}
799
800static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000801 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100802{
803 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
804
805 return pct->soc->groups[selector].name;
806}
807
808static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000809 unsigned selector,
810 const unsigned **pins,
811 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100812{
813 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
814
815 *pins = pct->soc->groups[selector].pins;
816 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000817
Patrice Chotard0493e642013-01-08 10:41:02 +0100818 return 0;
819}
820
821static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000822 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100823{
824 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
825 struct gpio_chip *chip = &pct->chip;
826
827 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
828 chip->base + offset - 1);
829}
830
Patrice Chotard64a45c92013-06-20 16:04:59 +0200831static int abx500_dt_add_map_mux(struct pinctrl_map **map,
832 unsigned *reserved_maps,
833 unsigned *num_maps, const char *group,
834 const char *function)
835{
836 if (*num_maps == *reserved_maps)
837 return -ENOSPC;
838
839 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
840 (*map)[*num_maps].data.mux.group = group;
841 (*map)[*num_maps].data.mux.function = function;
842 (*num_maps)++;
843
844 return 0;
845}
846
847static int abx500_dt_add_map_configs(struct pinctrl_map **map,
848 unsigned *reserved_maps,
849 unsigned *num_maps, const char *group,
850 unsigned long *configs, unsigned num_configs)
851{
852 unsigned long *dup_configs;
853
854 if (*num_maps == *reserved_maps)
855 return -ENOSPC;
856
857 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
858 GFP_KERNEL);
859 if (!dup_configs)
860 return -ENOMEM;
861
862 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
863
864 (*map)[*num_maps].data.configs.group_or_pin = group;
865 (*map)[*num_maps].data.configs.configs = dup_configs;
866 (*map)[*num_maps].data.configs.num_configs = num_configs;
867 (*num_maps)++;
868
869 return 0;
870}
871
872static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
873 const char *pin_name)
874{
875 int i, pin_number;
876 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
877
878 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
879 for (i = 0; i < npct->soc->npins; i++)
880 if (npct->soc->pins[i].number == pin_number)
881 return npct->soc->pins[i].name;
882 return NULL;
883}
884
885static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
886 struct device_node *np,
887 struct pinctrl_map **map,
888 unsigned *reserved_maps,
889 unsigned *num_maps)
890{
891 int ret;
892 const char *function = NULL;
893 unsigned long *configs;
894 unsigned int nconfigs = 0;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200895 struct property *prop;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200896
Linus Walleij51d39932014-09-30 12:10:11 +0200897 ret = of_property_read_string(np, "function", &function);
Linus Walleij259145f2014-09-30 11:22:07 +0200898 if (ret >= 0) {
Linus Walleij51d39932014-09-30 12:10:11 +0200899 const char *group;
900
901 ret = of_property_count_strings(np, "groups");
Linus Walleij259145f2014-09-30 11:22:07 +0200902 if (ret < 0)
903 goto exit;
904
905 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
906 num_maps, ret);
907 if (ret < 0)
908 goto exit;
909
Linus Walleij51d39932014-09-30 12:10:11 +0200910 of_property_for_each_string(np, "groups", prop, group) {
Linus Walleij259145f2014-09-30 11:22:07 +0200911 ret = abx500_dt_add_map_mux(map, reserved_maps,
912 num_maps, group, function);
913 if (ret < 0)
914 goto exit;
915 }
916 }
Patrice Chotard64a45c92013-06-20 16:04:59 +0200917
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800918 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &nconfigs);
Linus Walleijeea11b02014-09-30 12:23:15 +0200919 if (nconfigs) {
Linus Walleij51d39932014-09-30 12:10:11 +0200920 const char *gpio_name;
921 const char *pin;
922
Linus Walleij0564f7d2014-09-30 12:19:40 +0200923 ret = of_property_count_strings(np, "pins");
Linus Walleij259145f2014-09-30 11:22:07 +0200924 if (ret < 0)
925 goto exit;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200926
Linus Walleij259145f2014-09-30 11:22:07 +0200927 ret = pinctrl_utils_reserve_map(pctldev, map,
928 reserved_maps,
929 num_maps, ret);
930 if (ret < 0)
931 goto exit;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200932
Linus Walleij0564f7d2014-09-30 12:19:40 +0200933 of_property_for_each_string(np, "pins", prop, pin) {
Linus Walleij51d39932014-09-30 12:10:11 +0200934 gpio_name = abx500_find_pin_name(pctldev, pin);
Patrice Chotard64a45c92013-06-20 16:04:59 +0200935
936 ret = abx500_dt_add_map_configs(map, reserved_maps,
937 num_maps, gpio_name, configs, 1);
938 if (ret < 0)
939 goto exit;
940 }
Patrice Chotard64a45c92013-06-20 16:04:59 +0200941 }
Linus Walleij259145f2014-09-30 11:22:07 +0200942
Patrice Chotard64a45c92013-06-20 16:04:59 +0200943exit:
944 return ret;
945}
946
947static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
948 struct device_node *np_config,
949 struct pinctrl_map **map, unsigned *num_maps)
950{
951 unsigned reserved_maps;
952 struct device_node *np;
953 int ret;
954
955 reserved_maps = 0;
956 *map = NULL;
957 *num_maps = 0;
958
959 for_each_child_of_node(np_config, np) {
960 ret = abx500_dt_subnode_to_map(pctldev, np, map,
961 &reserved_maps, num_maps);
962 if (ret < 0) {
Linus Walleijb07f92a2014-09-30 11:11:50 +0200963 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
Patrice Chotard64a45c92013-06-20 16:04:59 +0200964 return ret;
965 }
966 }
967
968 return 0;
969}
970
Laurent Pinchart022ab142013-02-16 10:25:07 +0100971static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100972 .get_groups_count = abx500_get_groups_cnt,
973 .get_group_name = abx500_get_group_name,
974 .get_group_pins = abx500_get_group_pins,
975 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200976 .dt_node_to_map = abx500_dt_node_to_map,
Linus Walleijb07f92a2014-09-30 11:11:50 +0200977 .dt_free_map = pinctrl_utils_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100978};
979
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530980static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000981 unsigned pin,
982 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100983{
Lee Jones1abeebe2012-12-20 11:11:19 +0000984 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100985}
986
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530987static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000988 unsigned pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700989 unsigned long *configs,
990 unsigned num_configs)
Patrice Chotard0493e642013-01-08 10:41:02 +0100991{
992 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100993 struct gpio_chip *chip = &pct->chip;
994 unsigned offset;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200995 int ret = -EINVAL;
Sherman Yin03b054e2013-08-27 11:32:12 -0700996 int i;
997 enum pin_config_param param;
998 enum pin_config_param argument;
Patrice Chotard0493e642013-01-08 10:41:02 +0100999
Sherman Yin03b054e2013-08-27 11:32:12 -07001000 for (i = 0; i < num_configs; i++) {
1001 param = pinconf_to_config_param(configs[i]);
1002 argument = pinconf_to_config_argument(configs[i]);
Lee Jones83b423c2013-01-23 13:24:08 +00001003
Sherman Yin03b054e2013-08-27 11:32:12 -07001004 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
1005 pin, configs[i],
1006 (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
1007 (param == PIN_CONFIG_OUTPUT) ?
1008 (argument ? "high" : "low") :
1009 (argument ? "pull up" : "pull down"));
Patrice Chotard0493e642013-01-08 10:41:02 +01001010
Sherman Yin03b054e2013-08-27 11:32:12 -07001011 /* on ABx500, there is no GPIO0, so adjust the offset */
1012 offset = pin - 1;
Patrice Chotard61ce1352013-06-20 16:05:00 +02001013
Sherman Yin03b054e2013-08-27 11:32:12 -07001014 switch (param) {
1015 case PIN_CONFIG_BIAS_DISABLE:
1016 ret = abx500_gpio_direction_input(chip, offset);
1017 if (ret < 0)
1018 goto out;
1019 /*
1020 * Some chips only support pull down, while some
1021 * actually support both pull up and pull down. Such
1022 * chips have a "pullud" range specified for the pins
1023 * that support both features. If the pin is not
1024 * within that range, we fall back to the old bit set
1025 * that only support pull down.
1026 */
1027 if (abx500_pullud_supported(chip, pin))
1028 ret = abx500_set_pull_updown(pct,
1029 pin,
1030 ABX500_GPIO_PULL_NONE);
1031 else
1032 /* Chip only supports pull down */
1033 ret = abx500_gpio_set_bits(chip,
1034 AB8500_GPIO_PUD1_REG, offset,
1035 ABX500_GPIO_PULL_NONE);
1036 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001037
Sherman Yin03b054e2013-08-27 11:32:12 -07001038 case PIN_CONFIG_BIAS_PULL_DOWN:
1039 ret = abx500_gpio_direction_input(chip, offset);
1040 if (ret < 0)
1041 goto out;
1042 /*
1043 * if argument = 1 set the pull down
1044 * else clear the pull down
1045 * Some chips only support pull down, while some
1046 * actually support both pull up and pull down. Such
1047 * chips have a "pullud" range specified for the pins
1048 * that support both features. If the pin is not
1049 * within that range, we fall back to the old bit set
1050 * that only support pull down.
1051 */
1052 if (abx500_pullud_supported(chip, pin))
1053 ret = abx500_set_pull_updown(pct,
1054 pin,
1055 argument ? ABX500_GPIO_PULL_DOWN :
1056 ABX500_GPIO_PULL_NONE);
1057 else
1058 /* Chip only supports pull down */
1059 ret = abx500_gpio_set_bits(chip,
1060 AB8500_GPIO_PUD1_REG,
1061 offset,
1062 argument ? ABX500_GPIO_PULL_DOWN :
1063 ABX500_GPIO_PULL_NONE);
1064 break;
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001065
Sherman Yin03b054e2013-08-27 11:32:12 -07001066 case PIN_CONFIG_BIAS_PULL_UP:
1067 ret = abx500_gpio_direction_input(chip, offset);
1068 if (ret < 0)
1069 goto out;
1070 /*
1071 * if argument = 1 set the pull up
1072 * else clear the pull up
1073 */
1074 ret = abx500_gpio_direction_input(chip, offset);
1075 /*
1076 * Some chips only support pull down, while some
1077 * actually support both pull up and pull down. Such
1078 * chips have a "pullud" range specified for the pins
1079 * that support both features. If the pin is not
1080 * within that range, do nothing
1081 */
1082 if (abx500_pullud_supported(chip, pin))
1083 ret = abx500_set_pull_updown(pct,
1084 pin,
1085 argument ? ABX500_GPIO_PULL_UP :
1086 ABX500_GPIO_PULL_NONE);
1087 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001088
Sherman Yin03b054e2013-08-27 11:32:12 -07001089 case PIN_CONFIG_OUTPUT:
1090 ret = abx500_gpio_direction_output(chip, offset,
1091 argument);
1092 break;
1093
1094 default:
1095 dev_err(chip->dev, "illegal configuration requested\n");
1096 }
1097 } /* for each config */
Patrice Chotard9be580a2013-06-24 14:41:46 +02001098out:
1099 if (ret < 0)
1100 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +00001101
Patrice Chotard0493e642013-01-08 10:41:02 +01001102 return ret;
1103}
1104
Laurent Pinchart022ab142013-02-16 10:25:07 +01001105static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001106 .pin_config_get = abx500_pin_config_get,
1107 .pin_config_set = abx500_pin_config_set,
Linus Walleij71ca9172014-09-30 13:12:28 +02001108 .is_generic = true,
Patrice Chotard0493e642013-01-08 10:41:02 +01001109};
1110
1111static struct pinctrl_desc abx500_pinctrl_desc = {
1112 .name = "pinctrl-abx500",
1113 .pctlops = &abx500_pinctrl_ops,
1114 .pmxops = &abx500_pinmux_ops,
1115 .confops = &abx500_pinconf_ops,
1116 .owner = THIS_MODULE,
1117};
1118
1119static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1120{
1121 unsigned int lowest = 0;
1122 unsigned int highest = 0;
1123 unsigned int npins = 0;
1124 int i;
1125
1126 /*
1127 * Compute number of GPIOs from the last SoC gpio range descriptors
1128 * These ranges may include "holes" but the GPIO number space shall
1129 * still be homogeneous, so we need to detect and account for any
1130 * such holes so that these are included in the number of GPIO pins.
1131 */
1132 for (i = 0; i < soc->gpio_num_ranges; i++) {
1133 unsigned gstart;
1134 unsigned gend;
1135 const struct abx500_pinrange *p;
1136
1137 p = &soc->gpio_ranges[i];
1138 gstart = p->offset;
1139 gend = p->offset + p->npins - 1;
1140
1141 if (i == 0) {
1142 /* First iteration, set start values */
1143 lowest = gstart;
1144 highest = gend;
1145 } else {
1146 if (gstart < lowest)
1147 lowest = gstart;
1148 if (gend > highest)
1149 highest = gend;
1150 }
1151 }
1152 /* this gives the absolute number of pins */
1153 npins = highest - lowest + 1;
1154 return npins;
1155}
1156
Lee Jonesf30a3832013-01-31 11:07:40 +00001157static const struct of_device_id abx500_gpio_match[] = {
1158 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1159 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1160 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1161 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001162 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001163};
1164
Patrice Chotard0493e642013-01-08 10:41:02 +01001165static int abx500_gpio_probe(struct platform_device *pdev)
1166{
Lee Jonesf30a3832013-01-31 11:07:40 +00001167 struct device_node *np = pdev->dev.of_node;
Linus Walleijac99a032013-12-03 15:50:17 +01001168 const struct of_device_id *match;
Patrice Chotard0493e642013-01-08 10:41:02 +01001169 struct abx500_pinctrl *pct;
Lee Jonesf30a3832013-01-31 11:07:40 +00001170 unsigned int id = -1;
Linus Walleij3a4b0942014-10-02 09:30:43 +02001171 int ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001172 int i;
1173
Linus Walleijac99a032013-12-03 15:50:17 +01001174 if (!np) {
1175 dev_err(&pdev->dev, "gpio dt node missing\n");
Lee Jones86c976e2013-05-08 14:29:08 +01001176 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001177 }
1178
1179 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1180 GFP_KERNEL);
1181 if (pct == NULL) {
1182 dev_err(&pdev->dev,
1183 "failed to allocate memory for pct\n");
1184 return -ENOMEM;
1185 }
1186
1187 pct->dev = &pdev->dev;
1188 pct->parent = dev_get_drvdata(pdev->dev.parent);
1189 pct->chip = abx500gpio_chip;
1190 pct->chip.dev = &pdev->dev;
Linus Walleijac99a032013-12-03 15:50:17 +01001191 pct->chip.base = -1; /* Dynamic allocation */
Patrice Chotard0493e642013-01-08 10:41:02 +01001192
Linus Walleijac99a032013-12-03 15:50:17 +01001193 match = of_match_device(abx500_gpio_match, &pdev->dev);
1194 if (!match) {
1195 dev_err(&pdev->dev, "gpio dt not matching\n");
1196 return -ENODEV;
Lee Jones86c976e2013-05-08 14:29:08 +01001197 }
Linus Walleijac99a032013-12-03 15:50:17 +01001198 id = (unsigned long)match->data;
Lee Jones86c976e2013-05-08 14:29:08 +01001199
Patrice Chotard0493e642013-01-08 10:41:02 +01001200 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001201 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001202 case PINCTRL_AB8500:
1203 abx500_pinctrl_ab8500_init(&pct->soc);
1204 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001205 case PINCTRL_AB8540:
1206 abx500_pinctrl_ab8540_init(&pct->soc);
1207 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001208 case PINCTRL_AB9540:
1209 abx500_pinctrl_ab9540_init(&pct->soc);
1210 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001211 case PINCTRL_AB8505:
1212 abx500_pinctrl_ab8505_init(&pct->soc);
1213 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001214 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001215 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001216 return -EINVAL;
1217 }
1218
1219 if (!pct->soc) {
1220 dev_err(&pdev->dev, "Invalid SOC data\n");
1221 return -EINVAL;
1222 }
1223
1224 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1225 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1226 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001227
Patrice Chotard0493e642013-01-08 10:41:02 +01001228 ret = gpiochip_add(&pct->chip);
1229 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001230 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001231 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001232 }
1233 dev_info(&pdev->dev, "added gpiochip\n");
1234
1235 abx500_pinctrl_desc.pins = pct->soc->pins;
1236 abx500_pinctrl_desc.npins = pct->soc->npins;
1237 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001238 if (IS_ERR(pct->pctldev)) {
Patrice Chotard0493e642013-01-08 10:41:02 +01001239 dev_err(&pdev->dev,
1240 "could not register abx500 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001241 ret = PTR_ERR(pct->pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +01001242 goto out_rem_chip;
1243 }
1244 dev_info(&pdev->dev, "registered pin controller\n");
1245
1246 /* We will handle a range of GPIO pins */
1247 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1248 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1249
1250 ret = gpiochip_add_pin_range(&pct->chip,
1251 dev_name(&pdev->dev),
1252 p->offset - 1, p->offset, p->npins);
1253 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001254 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001255 }
1256
1257 platform_set_drvdata(pdev, pct);
1258 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1259
1260 return 0;
1261
1262out_rem_chip:
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001263 gpiochip_remove(&pct->chip);
Patrice Chotard0493e642013-01-08 10:41:02 +01001264 return ret;
1265}
1266
Lee Jones83b423c2013-01-23 13:24:08 +00001267/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001268 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001269 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001270 */
1271static int abx500_gpio_remove(struct platform_device *pdev)
1272{
1273 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
Patrice Chotard0493e642013-01-08 10:41:02 +01001274
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001275 gpiochip_remove(&pct->chip);
Patrice Chotard0493e642013-01-08 10:41:02 +01001276 return 0;
1277}
1278
Patrice Chotard0493e642013-01-08 10:41:02 +01001279static struct platform_driver abx500_gpio_driver = {
1280 .driver = {
1281 .name = "abx500-gpio",
Lee Jonesf30a3832013-01-31 11:07:40 +00001282 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001283 },
1284 .probe = abx500_gpio_probe,
1285 .remove = abx500_gpio_remove,
Patrice Chotard0493e642013-01-08 10:41:02 +01001286};
1287
1288static int __init abx500_gpio_init(void)
1289{
1290 return platform_driver_register(&abx500_gpio_driver);
1291}
1292core_initcall(abx500_gpio_init);
1293
1294MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1295MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1296MODULE_ALIAS("platform:abx500-gpio");
1297MODULE_LICENSE("GPL v2");