blob: 085e60106ec2c2f94dd83bfbc2f21fbd8ba17da6 [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
Patrice Chotard0493e642013-01-08 10:41:02 +0100112static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000113 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100114{
Linus Walleij2b016d22015-12-08 08:29:43 +0100115 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100116 u8 pos = offset % 8;
117 u8 val;
118 int ret;
119
120 reg += offset / 8;
121 ret = abx500_get_register_interruptible(pct->dev,
122 AB8500_MISC, reg, &val);
123
124 *bit = !!(val & BIT(pos));
125
126 if (ret < 0)
127 dev_err(pct->dev,
Patrice Chotard9be580a2013-06-24 14:41:46 +0200128 "%s read reg =%x, offset=%x failed (%d)\n",
129 __func__, reg, offset, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100130
131 return ret;
132}
133
134static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000135 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100136{
Linus Walleij2b016d22015-12-08 08:29:43 +0100137 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100138 u8 pos = offset % 8;
139 int ret;
140
141 reg += offset / 8;
142 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000143 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100144 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200145 dev_err(pct->dev, "%s write reg, %x offset %x failed (%d)\n",
146 __func__, reg, offset, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000147
Patrice Chotard0493e642013-01-08 10:41:02 +0100148 return ret;
149}
Lee Jones83b423c2013-01-23 13:24:08 +0000150
Patrice Chotard0493e642013-01-08 10:41:02 +0100151/**
152 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000153 * @chip: Gpio device
154 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100155 */
156static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
157{
Linus Walleij2b016d22015-12-08 08:29:43 +0100158 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100159 bool bit;
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200160 bool is_out;
161 u8 gpio_offset = offset - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100162 int ret;
163
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200164 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
165 gpio_offset, &is_out);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200166 if (ret < 0)
167 goto out;
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200168
169 if (is_out)
170 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
171 gpio_offset, &bit);
172 else
173 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
174 gpio_offset, &bit);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200175out:
Patrice Chotard0493e642013-01-08 10:41:02 +0100176 if (ret < 0) {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200177 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100178 return ret;
179 }
Lee Jones83b423c2013-01-23 13:24:08 +0000180
Patrice Chotard0493e642013-01-08 10:41:02 +0100181 return bit;
182}
183
184static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
185{
Linus Walleij2b016d22015-12-08 08:29:43 +0100186 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100187 int ret;
188
189 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
190 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200191 dev_err(pct->dev, "%s write failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100192}
193
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200194static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
195 enum abx500_gpio_pull_updown *pull_updown)
196{
197 u8 pos;
198 u8 val;
199 int ret;
200 struct pullud *pullud;
201
202 if (!pct->soc->pullud) {
203 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
204 __func__);
205 ret = -EPERM;
206 goto out;
207 }
208
209 pullud = pct->soc->pullud;
210
211 if ((offset < pullud->first_pin)
212 || (offset > pullud->last_pin)) {
213 ret = -EINVAL;
214 goto out;
215 }
216
217 ret = abx500_get_register_interruptible(pct->dev,
218 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
219
220 pos = (offset - pullud->first_pin) << 1;
221 *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
222
223out:
224 if (ret < 0)
225 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
226
227 return ret;
228}
229
230static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
231 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100232{
233 u8 pos;
234 int ret;
235 struct pullud *pullud;
236
237 if (!pct->soc->pullud) {
238 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
239 __func__);
240 ret = -EPERM;
241 goto out;
242 }
243
244 pullud = pct->soc->pullud;
245
246 if ((offset < pullud->first_pin)
247 || (offset > pullud->last_pin)) {
248 ret = -EINVAL;
249 goto out;
250 }
Patrice Chotard10a8be52013-05-24 14:06:29 +0200251 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100252
253 ret = abx500_mask_and_set_register_interruptible(pct->dev,
254 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
255 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
256
257out:
258 if (ret < 0)
259 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000260
Patrice Chotard0493e642013-01-08 10:41:02 +0100261 return ret;
262}
263
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200264static bool abx500_pullud_supported(struct gpio_chip *chip, unsigned gpio)
265{
Linus Walleij2b016d22015-12-08 08:29:43 +0100266 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200267 struct pullud *pullud = pct->soc->pullud;
268
269 return (pullud &&
270 gpio >= pullud->first_pin &&
271 gpio <= pullud->last_pin);
272}
273
Patrice Chotard0493e642013-01-08 10:41:02 +0100274static int abx500_gpio_direction_output(struct gpio_chip *chip,
275 unsigned offset,
276 int val)
277{
Linus Walleij2b016d22015-12-08 08:29:43 +0100278 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100279 unsigned gpio;
280 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000281
Patrice Chotard0493e642013-01-08 10:41:02 +0100282 /* set direction as output */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200283 ret = abx500_gpio_set_bits(chip,
284 AB8500_GPIO_DIR1_REG,
285 offset,
286 ABX500_GPIO_OUTPUT);
Patrice Chotard0493e642013-01-08 10:41:02 +0100287 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200288 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100289
290 /* disable pull down */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200291 ret = abx500_gpio_set_bits(chip,
292 AB8500_GPIO_PUD1_REG,
293 offset,
294 ABX500_GPIO_PULL_NONE);
Patrice Chotard0493e642013-01-08 10:41:02 +0100295 if (ret < 0)
Patrice Chotard9be580a2013-06-24 14:41:46 +0200296 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100297
298 /* if supported, disable both pull down and pull up */
299 gpio = offset + 1;
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200300 if (abx500_pullud_supported(chip, gpio)) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200301 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100302 gpio,
303 ABX500_GPIO_PULL_NONE);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200304 }
305out:
306 if (ret < 0) {
307 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
308 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100309 }
Lee Jones83b423c2013-01-23 13:24:08 +0000310
Patrice Chotard0493e642013-01-08 10:41:02 +0100311 /* set the output as 1 or 0 */
312 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
313}
314
315static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
316{
317 /* set the register as input */
Patrice Chotardacd260b2013-06-24 14:41:45 +0200318 return abx500_gpio_set_bits(chip,
319 AB8500_GPIO_DIR1_REG,
320 offset,
321 ABX500_GPIO_INPUT);
Patrice Chotard0493e642013-01-08 10:41:02 +0100322}
323
324static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
325{
Linus Walleij2b016d22015-12-08 08:29:43 +0100326 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000327 /* The AB8500 GPIO numbers are off by one */
328 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000329 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100330 int i;
331
332 for (i = 0; i < pct->irq_cluster_size; i++) {
333 struct abx500_gpio_irq_cluster *cluster =
334 &pct->irq_cluster[i];
335
Lee Jonesa6a16d22013-01-31 09:57:52 +0000336 if (gpio >= cluster->start && gpio <= cluster->end) {
337 /*
338 * The ABx500 GPIO's associated IRQs are clustered together
339 * throughout the interrupt numbers at irregular intervals.
340 * To solve this quandry, we have placed the read-in values
341 * into the cluster information table.
342 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100343 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000344 return irq_create_mapping(pct->parent->domain, hwirq);
345 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100346 }
347
348 return -EINVAL;
349}
350
351static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000352 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100353{
354 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
355 struct alternate_functions af = pct->soc->alternate_functions[gpio];
356 int ret;
357 int val;
358 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000359
Patrice Chotard0493e642013-01-08 10:41:02 +0100360 const char *modes[] = {
361 [ABX500_DEFAULT] = "default",
362 [ABX500_ALT_A] = "altA",
363 [ABX500_ALT_B] = "altB",
364 [ABX500_ALT_C] = "altC",
365 };
366
367 /* sanity check */
368 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
369 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
370 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
371 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
372 modes[alt_setting]);
373 return -EINVAL;
374 }
375
376 /* on ABx5xx, there is no GPIO0, so adjust the offset */
377 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000378
Patrice Chotard0493e642013-01-08 10:41:02 +0100379 switch (alt_setting) {
380 case ABX500_DEFAULT:
381 /*
382 * for ABx5xx family, default mode is always selected by
383 * writing 0 to GPIOSELx register, except for pins which
384 * support at least ALT_B mode, default mode is selected
385 * by writing 1 to GPIOSELx register
386 */
387 val = 0;
388 if (af.alt_bit1 != UNUSED)
389 val++;
390
391 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
392 offset, val);
393 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000394
Patrice Chotard0493e642013-01-08 10:41:02 +0100395 case ABX500_ALT_A:
396 /*
397 * for ABx5xx family, alt_a mode is always selected by
398 * writing 1 to GPIOSELx register, except for pins which
399 * support at least ALT_B mode, alt_a mode is selected
400 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
401 * register
402 */
403 if (af.alt_bit1 != UNUSED) {
404 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
405 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200406 if (ret < 0)
407 goto out;
408
Patrice Chotard0493e642013-01-08 10:41:02 +0100409 ret = abx500_gpio_set_bits(chip,
410 AB8500_GPIO_ALTFUN_REG,
411 af.alt_bit1,
Dan Carpenterc5908542013-11-10 02:35:56 +0300412 !!(af.alta_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200413 if (ret < 0)
414 goto out;
415
Patrice Chotard0493e642013-01-08 10:41:02 +0100416 if (af.alt_bit2 != UNUSED)
417 ret = abx500_gpio_set_bits(chip,
418 AB8500_GPIO_ALTFUN_REG,
419 af.alt_bit2,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300420 !!(af.alta_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100421 } else
422 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
423 offset, 1);
424 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000425
Patrice Chotard0493e642013-01-08 10:41:02 +0100426 case ABX500_ALT_B:
427 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
428 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200429 if (ret < 0)
430 goto out;
431
Patrice Chotard0493e642013-01-08 10:41:02 +0100432 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenterc5908542013-11-10 02:35:56 +0300433 af.alt_bit1, !!(af.altb_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200434 if (ret < 0)
435 goto out;
436
Patrice Chotard0493e642013-01-08 10:41:02 +0100437 if (af.alt_bit2 != UNUSED)
438 ret = abx500_gpio_set_bits(chip,
439 AB8500_GPIO_ALTFUN_REG,
440 af.alt_bit2,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300441 !!(af.altb_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100442 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000443
Patrice Chotard0493e642013-01-08 10:41:02 +0100444 case ABX500_ALT_C:
445 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
446 offset, 0);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200447 if (ret < 0)
448 goto out;
449
Patrice Chotard0493e642013-01-08 10:41:02 +0100450 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenter6da33db2013-08-26 19:03:50 +0300451 af.alt_bit2, !!(af.altc_val & BIT(0)));
Patrice Chotard9be580a2013-06-24 14:41:46 +0200452 if (ret < 0)
453 goto out;
454
Patrice Chotard0493e642013-01-08 10:41:02 +0100455 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
Dan Carpenterc5908542013-11-10 02:35:56 +0300456 af.alt_bit2, !!(af.altc_val & BIT(1)));
Patrice Chotard0493e642013-01-08 10:41:02 +0100457 break;
458
459 default:
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900460 dev_dbg(pct->dev, "unknown alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000461
Patrice Chotard0493e642013-01-08 10:41:02 +0100462 return -EINVAL;
463 }
Patrice Chotard9be580a2013-06-24 14:41:46 +0200464out:
465 if (ret < 0)
466 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000467
Patrice Chotard0493e642013-01-08 10:41:02 +0100468 return ret;
469}
470
Patrice Chotard9be580a2013-06-24 14:41:46 +0200471static int abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000472 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100473{
474 u8 mode;
475 bool bit_mode;
476 bool alt_bit1;
477 bool alt_bit2;
478 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
479 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100480 /* on ABx5xx, there is no GPIO0, so adjust the offset */
481 unsigned offset = gpio - 1;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200482 int ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100483
484 /*
485 * if gpiosel_bit is set to unused,
486 * it means no GPIO or special case
487 */
488 if (af.gpiosel_bit == UNUSED)
489 return ABX500_DEFAULT;
490
491 /* read GpioSelx register */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200492 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100493 af.gpiosel_bit, &bit_mode);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200494 if (ret < 0)
495 goto out;
496
Patrice Chotard0493e642013-01-08 10:41:02 +0100497 mode = bit_mode;
498
499 /* sanity check */
500 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
501 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
502 dev_err(pct->dev,
503 "alt_bitX value not in correct range (-1 to 7)\n");
504 return -EINVAL;
505 }
Lee Jones83b423c2013-01-23 13:24:08 +0000506
Patrice Chotard0493e642013-01-08 10:41:02 +0100507 /* if alt_bit2 is used, alt_bit1 must be used too */
508 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
509 dev_err(pct->dev,
510 "if alt_bit2 is used, alt_bit1 can't be unused\n");
511 return -EINVAL;
512 }
513
514 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800515 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100516 return mode;
517 /*
518 * if pin GPIOSEL bit is set and pin supports alternate function,
519 * it means DEFAULT mode
520 */
521 if (mode)
522 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000523
Patrice Chotard0493e642013-01-08 10:41:02 +0100524 /*
525 * pin use the AlternatFunction register
526 * read alt_bit1 value
527 */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200528 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
Patrice Chotard0493e642013-01-08 10:41:02 +0100529 af.alt_bit1, &alt_bit1);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200530 if (ret < 0)
531 goto out;
Patrice Chotard0493e642013-01-08 10:41:02 +0100532
Patrice Chotard9be580a2013-06-24 14:41:46 +0200533 if (af.alt_bit2 != UNUSED) {
Patrice Chotard0493e642013-01-08 10:41:02 +0100534 /* read alt_bit2 value */
Patrice Chotard9be580a2013-06-24 14:41:46 +0200535 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
536 af.alt_bit2,
Patrice Chotard0493e642013-01-08 10:41:02 +0100537 &alt_bit2);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200538 if (ret < 0)
539 goto out;
540 } else
Patrice Chotard0493e642013-01-08 10:41:02 +0100541 alt_bit2 = 0;
542
543 mode = (alt_bit2 << 1) + alt_bit1;
544 if (mode == af.alta_val)
545 return ABX500_ALT_A;
546 else if (mode == af.altb_val)
547 return ABX500_ALT_B;
548 else
549 return ABX500_ALT_C;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200550
551out:
552 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
553 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100554}
555
556#ifdef CONFIG_DEBUG_FS
557
558#include <linux/seq_file.h>
559
560static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000561 struct pinctrl_dev *pctldev,
562 struct gpio_chip *chip,
563 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100564{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200565 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100566 const char *label = gpiochip_is_requested(chip, offset - 1);
567 u8 gpio_offset = offset - 1;
568 int mode = -1;
569 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200570 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200571 enum abx500_gpio_pull_updown pud = 0;
Patrice Chotard9be580a2013-06-24 14:41:46 +0200572 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000573
Patrice Chotard0493e642013-01-08 10:41:02 +0100574 const char *modes[] = {
575 [ABX500_DEFAULT] = "default",
576 [ABX500_ALT_A] = "altA",
577 [ABX500_ALT_B] = "altB",
578 [ABX500_ALT_C] = "altC",
579 };
580
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200581 const char *pull_up_down[] = {
582 [ABX500_GPIO_PULL_DOWN] = "pull down",
583 [ABX500_GPIO_PULL_NONE] = "pull none",
584 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
585 [ABX500_GPIO_PULL_UP] = "pull up",
586 };
587
Patrice Chotard9be580a2013-06-24 14:41:46 +0200588 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
589 gpio_offset, &is_out);
590 if (ret < 0)
591 goto out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200592
593 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
594 gpio, label ?: "(none)",
595 is_out ? "out" : "in ");
596
597 if (!is_out) {
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200598 if (abx500_pullud_supported(chip, offset)) {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200599 ret = abx500_get_pull_updown(pct, offset, &pud);
600 if (ret < 0)
601 goto out;
602
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200603 seq_printf(s, " %-9s", pull_up_down[pud]);
604 } else {
Patrice Chotard9be580a2013-06-24 14:41:46 +0200605 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
606 gpio_offset, &pd);
607 if (ret < 0)
608 goto out;
609
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200610 seq_printf(s, " %-9s", pull_up_down[pd]);
611 }
612 } else
613 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100614
Patrice CHOTARD1d54f0f2014-08-01 09:38:43 +0200615 mode = abx500_get_mode(pctldev, chip, offset);
Patrice Chotard0493e642013-01-08 10:41:02 +0100616
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200617 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200618
619out:
620 if (ret < 0)
621 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Patrice Chotard0493e642013-01-08 10:41:02 +0100622}
623
624static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
625{
626 unsigned i;
627 unsigned gpio = chip->base;
Linus Walleij2b016d22015-12-08 08:29:43 +0100628 struct abx500_pinctrl *pct = gpiochip_get_data(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100629 struct pinctrl_dev *pctldev = pct->pctldev;
630
631 for (i = 0; i < chip->ngpio; i++, gpio++) {
632 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
633 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
634 seq_printf(s, "\n");
635 }
636}
637
638#else
639static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000640 struct pinctrl_dev *pctldev,
641 struct gpio_chip *chip,
642 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100643{
644}
645#define abx500_gpio_dbg_show NULL
646#endif
647
Patrice Chotard0493e642013-01-08 10:41:02 +0100648static struct gpio_chip abx500gpio_chip = {
649 .label = "abx500-gpio",
650 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200651 .request = gpiochip_generic_request,
652 .free = gpiochip_generic_free,
Patrice Chotard0493e642013-01-08 10:41:02 +0100653 .direction_input = abx500_gpio_direction_input,
654 .get = abx500_gpio_get,
655 .direction_output = abx500_gpio_direction_output,
656 .set = abx500_gpio_set,
657 .to_irq = abx500_gpio_to_irq,
658 .dbg_show = abx500_gpio_dbg_show,
659};
660
Patrice Chotard0493e642013-01-08 10:41:02 +0100661static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
662{
663 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
664
665 return pct->soc->nfunctions;
666}
667
668static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
669 unsigned function)
670{
671 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
672
673 return pct->soc->functions[function].name;
674}
675
676static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000677 unsigned function,
678 const char * const **groups,
679 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100680{
681 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
682
683 *groups = pct->soc->functions[function].groups;
684 *num_groups = pct->soc->functions[function].ngroups;
685
686 return 0;
687}
688
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200689static int abx500_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
690 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100691{
692 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
693 struct gpio_chip *chip = &pct->chip;
694 const struct abx500_pingroup *g;
695 int i;
696 int ret = 0;
697
698 g = &pct->soc->groups[group];
699 if (g->altsetting < 0)
700 return -EINVAL;
701
702 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
703
704 for (i = 0; i < g->npins; i++) {
705 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
706 g->pins[i], g->altsetting);
707
Patrice Chotard0493e642013-01-08 10:41:02 +0100708 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
709 }
Lee Jones83b423c2013-01-23 13:24:08 +0000710
Patrice Chotard9be580a2013-06-24 14:41:46 +0200711 if (ret < 0)
712 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
713
Patrice Chotard0493e642013-01-08 10:41:02 +0100714 return ret;
715}
716
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530717static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000718 struct pinctrl_gpio_range *range,
719 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100720{
721 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
722 const struct abx500_pinrange *p;
723 int ret;
724 int i;
725
726 /*
727 * Different ranges have different ways to enable GPIO function on a
728 * pin, so refer back to our local range type, where we handily define
729 * what altfunc enables GPIO for a certain pin.
730 */
731 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
732 p = &pct->soc->gpio_ranges[i];
733 if ((offset >= p->offset) &&
734 (offset < (p->offset + p->npins)))
735 break;
736 }
737
738 if (i == pct->soc->gpio_num_ranges) {
739 dev_err(pct->dev, "%s failed to locate range\n", __func__);
740 return -ENODEV;
741 }
742
743 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
744 p->altfunc, offset);
745
746 ret = abx500_set_mode(pct->pctldev, &pct->chip,
747 offset, p->altfunc);
Patrice Chotard9be580a2013-06-24 14:41:46 +0200748 if (ret < 0)
Patrice Chotard0493e642013-01-08 10:41:02 +0100749 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
Patrice Chotard0493e642013-01-08 10:41:02 +0100750
751 return ret;
752}
753
754static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000755 struct pinctrl_gpio_range *range,
756 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100757{
758}
759
Laurent Pinchart022ab142013-02-16 10:25:07 +0100760static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100761 .get_functions_count = abx500_pmx_get_funcs_cnt,
762 .get_function_name = abx500_pmx_get_func_name,
763 .get_function_groups = abx500_pmx_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200764 .set_mux = abx500_pmx_set,
Patrice Chotard0493e642013-01-08 10:41:02 +0100765 .gpio_request_enable = abx500_gpio_request_enable,
766 .gpio_disable_free = abx500_gpio_disable_free,
767};
768
769static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
770{
771 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
772
773 return pct->soc->ngroups;
774}
775
776static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000777 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100778{
779 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
780
781 return pct->soc->groups[selector].name;
782}
783
784static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000785 unsigned selector,
786 const unsigned **pins,
787 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100788{
789 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
790
791 *pins = pct->soc->groups[selector].pins;
792 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000793
Patrice Chotard0493e642013-01-08 10:41:02 +0100794 return 0;
795}
796
797static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000798 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100799{
800 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
801 struct gpio_chip *chip = &pct->chip;
802
803 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
804 chip->base + offset - 1);
805}
806
Patrice Chotard64a45c92013-06-20 16:04:59 +0200807static int abx500_dt_add_map_mux(struct pinctrl_map **map,
808 unsigned *reserved_maps,
809 unsigned *num_maps, const char *group,
810 const char *function)
811{
812 if (*num_maps == *reserved_maps)
813 return -ENOSPC;
814
815 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
816 (*map)[*num_maps].data.mux.group = group;
817 (*map)[*num_maps].data.mux.function = function;
818 (*num_maps)++;
819
820 return 0;
821}
822
823static int abx500_dt_add_map_configs(struct pinctrl_map **map,
824 unsigned *reserved_maps,
825 unsigned *num_maps, const char *group,
826 unsigned long *configs, unsigned num_configs)
827{
828 unsigned long *dup_configs;
829
830 if (*num_maps == *reserved_maps)
831 return -ENOSPC;
832
833 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
834 GFP_KERNEL);
835 if (!dup_configs)
836 return -ENOMEM;
837
838 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
839
840 (*map)[*num_maps].data.configs.group_or_pin = group;
841 (*map)[*num_maps].data.configs.configs = dup_configs;
842 (*map)[*num_maps].data.configs.num_configs = num_configs;
843 (*num_maps)++;
844
845 return 0;
846}
847
848static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
849 const char *pin_name)
850{
851 int i, pin_number;
852 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
853
854 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
855 for (i = 0; i < npct->soc->npins; i++)
856 if (npct->soc->pins[i].number == pin_number)
857 return npct->soc->pins[i].name;
858 return NULL;
859}
860
861static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
862 struct device_node *np,
863 struct pinctrl_map **map,
864 unsigned *reserved_maps,
865 unsigned *num_maps)
866{
867 int ret;
868 const char *function = NULL;
869 unsigned long *configs;
870 unsigned int nconfigs = 0;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200871 struct property *prop;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200872
Linus Walleij51d39932014-09-30 12:10:11 +0200873 ret = of_property_read_string(np, "function", &function);
Linus Walleij259145f2014-09-30 11:22:07 +0200874 if (ret >= 0) {
Linus Walleij51d39932014-09-30 12:10:11 +0200875 const char *group;
876
877 ret = of_property_count_strings(np, "groups");
Linus Walleij259145f2014-09-30 11:22:07 +0200878 if (ret < 0)
879 goto exit;
880
881 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
882 num_maps, ret);
883 if (ret < 0)
884 goto exit;
885
Linus Walleij51d39932014-09-30 12:10:11 +0200886 of_property_for_each_string(np, "groups", prop, group) {
Linus Walleij259145f2014-09-30 11:22:07 +0200887 ret = abx500_dt_add_map_mux(map, reserved_maps,
888 num_maps, group, function);
889 if (ret < 0)
890 goto exit;
891 }
892 }
Patrice Chotard64a45c92013-06-20 16:04:59 +0200893
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800894 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &nconfigs);
Linus Walleijeea11b02014-09-30 12:23:15 +0200895 if (nconfigs) {
Linus Walleij51d39932014-09-30 12:10:11 +0200896 const char *gpio_name;
897 const char *pin;
898
Linus Walleij0564f7d2014-09-30 12:19:40 +0200899 ret = of_property_count_strings(np, "pins");
Linus Walleij259145f2014-09-30 11:22:07 +0200900 if (ret < 0)
901 goto exit;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200902
Linus Walleij259145f2014-09-30 11:22:07 +0200903 ret = pinctrl_utils_reserve_map(pctldev, map,
904 reserved_maps,
905 num_maps, ret);
906 if (ret < 0)
907 goto exit;
Patrice Chotard64a45c92013-06-20 16:04:59 +0200908
Linus Walleij0564f7d2014-09-30 12:19:40 +0200909 of_property_for_each_string(np, "pins", prop, pin) {
Linus Walleij51d39932014-09-30 12:10:11 +0200910 gpio_name = abx500_find_pin_name(pctldev, pin);
Patrice Chotard64a45c92013-06-20 16:04:59 +0200911
912 ret = abx500_dt_add_map_configs(map, reserved_maps,
913 num_maps, gpio_name, configs, 1);
914 if (ret < 0)
915 goto exit;
916 }
Patrice Chotard64a45c92013-06-20 16:04:59 +0200917 }
Linus Walleij259145f2014-09-30 11:22:07 +0200918
Patrice Chotard64a45c92013-06-20 16:04:59 +0200919exit:
920 return ret;
921}
922
923static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
924 struct device_node *np_config,
925 struct pinctrl_map **map, unsigned *num_maps)
926{
927 unsigned reserved_maps;
928 struct device_node *np;
929 int ret;
930
931 reserved_maps = 0;
932 *map = NULL;
933 *num_maps = 0;
934
935 for_each_child_of_node(np_config, np) {
936 ret = abx500_dt_subnode_to_map(pctldev, np, map,
937 &reserved_maps, num_maps);
938 if (ret < 0) {
Linus Walleijb07f92a2014-09-30 11:11:50 +0200939 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
Patrice Chotard64a45c92013-06-20 16:04:59 +0200940 return ret;
941 }
942 }
943
944 return 0;
945}
946
Laurent Pinchart022ab142013-02-16 10:25:07 +0100947static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100948 .get_groups_count = abx500_get_groups_cnt,
949 .get_group_name = abx500_get_group_name,
950 .get_group_pins = abx500_get_group_pins,
951 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200952 .dt_node_to_map = abx500_dt_node_to_map,
Linus Walleijb07f92a2014-09-30 11:11:50 +0200953 .dt_free_map = pinctrl_utils_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100954};
955
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530956static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000957 unsigned pin,
958 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100959{
Lee Jones1abeebe2012-12-20 11:11:19 +0000960 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100961}
962
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530963static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000964 unsigned pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700965 unsigned long *configs,
966 unsigned num_configs)
Patrice Chotard0493e642013-01-08 10:41:02 +0100967{
968 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100969 struct gpio_chip *chip = &pct->chip;
970 unsigned offset;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200971 int ret = -EINVAL;
Sherman Yin03b054e2013-08-27 11:32:12 -0700972 int i;
973 enum pin_config_param param;
974 enum pin_config_param argument;
Patrice Chotard0493e642013-01-08 10:41:02 +0100975
Sherman Yin03b054e2013-08-27 11:32:12 -0700976 for (i = 0; i < num_configs; i++) {
977 param = pinconf_to_config_param(configs[i]);
978 argument = pinconf_to_config_argument(configs[i]);
Lee Jones83b423c2013-01-23 13:24:08 +0000979
Linus Walleij58383c72015-11-04 09:56:26 +0100980 dev_dbg(chip->parent, "pin %d [%#lx]: %s %s\n",
Sherman Yin03b054e2013-08-27 11:32:12 -0700981 pin, configs[i],
982 (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
983 (param == PIN_CONFIG_OUTPUT) ?
984 (argument ? "high" : "low") :
985 (argument ? "pull up" : "pull down"));
Patrice Chotard0493e642013-01-08 10:41:02 +0100986
Sherman Yin03b054e2013-08-27 11:32:12 -0700987 /* on ABx500, there is no GPIO0, so adjust the offset */
988 offset = pin - 1;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200989
Sherman Yin03b054e2013-08-27 11:32:12 -0700990 switch (param) {
991 case PIN_CONFIG_BIAS_DISABLE:
992 ret = abx500_gpio_direction_input(chip, offset);
993 if (ret < 0)
994 goto out;
995 /*
996 * Some chips only support pull down, while some
997 * actually support both pull up and pull down. Such
998 * chips have a "pullud" range specified for the pins
999 * that support both features. If the pin is not
1000 * within that range, we fall back to the old bit set
1001 * that only support pull down.
1002 */
1003 if (abx500_pullud_supported(chip, pin))
1004 ret = abx500_set_pull_updown(pct,
1005 pin,
1006 ABX500_GPIO_PULL_NONE);
1007 else
1008 /* Chip only supports pull down */
1009 ret = abx500_gpio_set_bits(chip,
1010 AB8500_GPIO_PUD1_REG, offset,
1011 ABX500_GPIO_PULL_NONE);
1012 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001013
Sherman Yin03b054e2013-08-27 11:32:12 -07001014 case PIN_CONFIG_BIAS_PULL_DOWN:
1015 ret = abx500_gpio_direction_input(chip, offset);
1016 if (ret < 0)
1017 goto out;
1018 /*
1019 * if argument = 1 set the pull down
1020 * else clear the pull down
1021 * Some chips only support pull down, while some
1022 * actually support both pull up and pull down. Such
1023 * chips have a "pullud" range specified for the pins
1024 * that support both features. If the pin is not
1025 * within that range, we fall back to the old bit set
1026 * that only support pull down.
1027 */
1028 if (abx500_pullud_supported(chip, pin))
1029 ret = abx500_set_pull_updown(pct,
1030 pin,
1031 argument ? ABX500_GPIO_PULL_DOWN :
1032 ABX500_GPIO_PULL_NONE);
1033 else
1034 /* Chip only supports pull down */
1035 ret = abx500_gpio_set_bits(chip,
1036 AB8500_GPIO_PUD1_REG,
1037 offset,
1038 argument ? ABX500_GPIO_PULL_DOWN :
1039 ABX500_GPIO_PULL_NONE);
1040 break;
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001041
Sherman Yin03b054e2013-08-27 11:32:12 -07001042 case PIN_CONFIG_BIAS_PULL_UP:
1043 ret = abx500_gpio_direction_input(chip, offset);
1044 if (ret < 0)
1045 goto out;
1046 /*
1047 * if argument = 1 set the pull up
1048 * else clear the pull up
1049 */
1050 ret = abx500_gpio_direction_input(chip, offset);
1051 /*
1052 * Some chips only support pull down, while some
1053 * actually support both pull up and pull down. Such
1054 * chips have a "pullud" range specified for the pins
1055 * that support both features. If the pin is not
1056 * within that range, do nothing
1057 */
1058 if (abx500_pullud_supported(chip, pin))
1059 ret = abx500_set_pull_updown(pct,
1060 pin,
1061 argument ? ABX500_GPIO_PULL_UP :
1062 ABX500_GPIO_PULL_NONE);
1063 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001064
Sherman Yin03b054e2013-08-27 11:32:12 -07001065 case PIN_CONFIG_OUTPUT:
1066 ret = abx500_gpio_direction_output(chip, offset,
1067 argument);
1068 break;
1069
1070 default:
Linus Walleij58383c72015-11-04 09:56:26 +01001071 dev_err(chip->parent,
1072 "illegal configuration requested\n");
Sherman Yin03b054e2013-08-27 11:32:12 -07001073 }
1074 } /* for each config */
Patrice Chotard9be580a2013-06-24 14:41:46 +02001075out:
1076 if (ret < 0)
1077 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +00001078
Patrice Chotard0493e642013-01-08 10:41:02 +01001079 return ret;
1080}
1081
Laurent Pinchart022ab142013-02-16 10:25:07 +01001082static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001083 .pin_config_get = abx500_pin_config_get,
1084 .pin_config_set = abx500_pin_config_set,
Linus Walleij71ca9172014-09-30 13:12:28 +02001085 .is_generic = true,
Patrice Chotard0493e642013-01-08 10:41:02 +01001086};
1087
1088static struct pinctrl_desc abx500_pinctrl_desc = {
1089 .name = "pinctrl-abx500",
1090 .pctlops = &abx500_pinctrl_ops,
1091 .pmxops = &abx500_pinmux_ops,
1092 .confops = &abx500_pinconf_ops,
1093 .owner = THIS_MODULE,
1094};
1095
1096static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1097{
1098 unsigned int lowest = 0;
1099 unsigned int highest = 0;
1100 unsigned int npins = 0;
1101 int i;
1102
1103 /*
1104 * Compute number of GPIOs from the last SoC gpio range descriptors
1105 * These ranges may include "holes" but the GPIO number space shall
1106 * still be homogeneous, so we need to detect and account for any
1107 * such holes so that these are included in the number of GPIO pins.
1108 */
1109 for (i = 0; i < soc->gpio_num_ranges; i++) {
1110 unsigned gstart;
1111 unsigned gend;
1112 const struct abx500_pinrange *p;
1113
1114 p = &soc->gpio_ranges[i];
1115 gstart = p->offset;
1116 gend = p->offset + p->npins - 1;
1117
1118 if (i == 0) {
1119 /* First iteration, set start values */
1120 lowest = gstart;
1121 highest = gend;
1122 } else {
1123 if (gstart < lowest)
1124 lowest = gstart;
1125 if (gend > highest)
1126 highest = gend;
1127 }
1128 }
1129 /* this gives the absolute number of pins */
1130 npins = highest - lowest + 1;
1131 return npins;
1132}
1133
Lee Jonesf30a3832013-01-31 11:07:40 +00001134static const struct of_device_id abx500_gpio_match[] = {
1135 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1136 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1137 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1138 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001139 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001140};
1141
Patrice Chotard0493e642013-01-08 10:41:02 +01001142static int abx500_gpio_probe(struct platform_device *pdev)
1143{
Lee Jonesf30a3832013-01-31 11:07:40 +00001144 struct device_node *np = pdev->dev.of_node;
Linus Walleijac99a032013-12-03 15:50:17 +01001145 const struct of_device_id *match;
Patrice Chotard0493e642013-01-08 10:41:02 +01001146 struct abx500_pinctrl *pct;
Lee Jonesf30a3832013-01-31 11:07:40 +00001147 unsigned int id = -1;
Linus Walleij3a4b0942014-10-02 09:30:43 +02001148 int ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001149 int i;
1150
Linus Walleijac99a032013-12-03 15:50:17 +01001151 if (!np) {
1152 dev_err(&pdev->dev, "gpio dt node missing\n");
Lee Jones86c976e2013-05-08 14:29:08 +01001153 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001154 }
1155
1156 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1157 GFP_KERNEL);
1158 if (pct == NULL) {
1159 dev_err(&pdev->dev,
1160 "failed to allocate memory for pct\n");
1161 return -ENOMEM;
1162 }
1163
1164 pct->dev = &pdev->dev;
1165 pct->parent = dev_get_drvdata(pdev->dev.parent);
1166 pct->chip = abx500gpio_chip;
Linus Walleij58383c72015-11-04 09:56:26 +01001167 pct->chip.parent = &pdev->dev;
Linus Walleijac99a032013-12-03 15:50:17 +01001168 pct->chip.base = -1; /* Dynamic allocation */
Patrice Chotard0493e642013-01-08 10:41:02 +01001169
Linus Walleijac99a032013-12-03 15:50:17 +01001170 match = of_match_device(abx500_gpio_match, &pdev->dev);
1171 if (!match) {
1172 dev_err(&pdev->dev, "gpio dt not matching\n");
1173 return -ENODEV;
Lee Jones86c976e2013-05-08 14:29:08 +01001174 }
Linus Walleijac99a032013-12-03 15:50:17 +01001175 id = (unsigned long)match->data;
Lee Jones86c976e2013-05-08 14:29:08 +01001176
Patrice Chotard0493e642013-01-08 10:41:02 +01001177 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001178 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001179 case PINCTRL_AB8500:
1180 abx500_pinctrl_ab8500_init(&pct->soc);
1181 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001182 case PINCTRL_AB8540:
1183 abx500_pinctrl_ab8540_init(&pct->soc);
1184 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001185 case PINCTRL_AB9540:
1186 abx500_pinctrl_ab9540_init(&pct->soc);
1187 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001188 case PINCTRL_AB8505:
1189 abx500_pinctrl_ab8505_init(&pct->soc);
1190 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001191 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001192 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001193 return -EINVAL;
1194 }
1195
1196 if (!pct->soc) {
1197 dev_err(&pdev->dev, "Invalid SOC data\n");
1198 return -EINVAL;
1199 }
1200
1201 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1202 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1203 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001204
Linus Walleij2b016d22015-12-08 08:29:43 +01001205 ret = gpiochip_add_data(&pct->chip, pct);
Patrice Chotard0493e642013-01-08 10:41:02 +01001206 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001207 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001208 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001209 }
1210 dev_info(&pdev->dev, "added gpiochip\n");
1211
1212 abx500_pinctrl_desc.pins = pct->soc->pins;
1213 abx500_pinctrl_desc.npins = pct->soc->npins;
1214 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001215 if (IS_ERR(pct->pctldev)) {
Patrice Chotard0493e642013-01-08 10:41:02 +01001216 dev_err(&pdev->dev,
1217 "could not register abx500 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001218 ret = PTR_ERR(pct->pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +01001219 goto out_rem_chip;
1220 }
1221 dev_info(&pdev->dev, "registered pin controller\n");
1222
1223 /* We will handle a range of GPIO pins */
1224 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1225 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1226
1227 ret = gpiochip_add_pin_range(&pct->chip,
1228 dev_name(&pdev->dev),
1229 p->offset - 1, p->offset, p->npins);
1230 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001231 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001232 }
1233
1234 platform_set_drvdata(pdev, pct);
1235 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1236
1237 return 0;
1238
1239out_rem_chip:
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001240 gpiochip_remove(&pct->chip);
Patrice Chotard0493e642013-01-08 10:41:02 +01001241 return ret;
1242}
1243
Lee Jones83b423c2013-01-23 13:24:08 +00001244/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001245 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001246 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001247 */
1248static int abx500_gpio_remove(struct platform_device *pdev)
1249{
1250 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
Patrice Chotard0493e642013-01-08 10:41:02 +01001251
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001252 gpiochip_remove(&pct->chip);
Patrice Chotard0493e642013-01-08 10:41:02 +01001253 return 0;
1254}
1255
Patrice Chotard0493e642013-01-08 10:41:02 +01001256static struct platform_driver abx500_gpio_driver = {
1257 .driver = {
1258 .name = "abx500-gpio",
Lee Jonesf30a3832013-01-31 11:07:40 +00001259 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001260 },
1261 .probe = abx500_gpio_probe,
1262 .remove = abx500_gpio_remove,
Patrice Chotard0493e642013-01-08 10:41:02 +01001263};
1264
1265static int __init abx500_gpio_init(void)
1266{
1267 return platform_driver_register(&abx500_gpio_driver);
1268}
1269core_initcall(abx500_gpio_init);
1270
1271MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1272MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1273MODULE_ALIAS("platform:abx500-gpio");
1274MODULE_LICENSE("GPL v2");