2 * GPIO driver for Exar XR17V35X chip
4 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/bitops.h>
11 #include <linux/device.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
19 #define EXAR_OFFSET_MPIOLVL_LO 0x90
20 #define EXAR_OFFSET_MPIOSEL_LO 0x93
21 #define EXAR_OFFSET_MPIOLVL_HI 0x96
22 #define EXAR_OFFSET_MPIOSEL_HI 0x99
24 #define DRIVER_NAME "gpio_exar"
26 static DEFINE_IDA(ida_index
);
28 struct exar_gpio_chip
{
29 struct gpio_chip gpio_chip
;
34 unsigned int first_pin
;
37 static void exar_update(struct gpio_chip
*chip
, unsigned int reg
, int val
,
40 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
43 mutex_lock(&exar_gpio
->lock
);
44 temp
= readb(exar_gpio
->regs
+ reg
);
48 writeb(temp
, exar_gpio
->regs
+ reg
);
49 mutex_unlock(&exar_gpio
->lock
);
52 static int exar_set_direction(struct gpio_chip
*chip
, int direction
,
55 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
56 unsigned int addr
= (offset
+ exar_gpio
->first_pin
) / 8 ?
57 EXAR_OFFSET_MPIOSEL_HI
: EXAR_OFFSET_MPIOSEL_LO
;
58 unsigned int bit
= (offset
+ exar_gpio
->first_pin
) % 8;
60 exar_update(chip
, addr
, direction
, bit
);
64 static int exar_get(struct gpio_chip
*chip
, unsigned int reg
)
66 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
69 mutex_lock(&exar_gpio
->lock
);
70 value
= readb(exar_gpio
->regs
+ reg
);
71 mutex_unlock(&exar_gpio
->lock
);
76 static int exar_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
78 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
79 unsigned int addr
= (offset
+ exar_gpio
->first_pin
) / 8 ?
80 EXAR_OFFSET_MPIOSEL_HI
: EXAR_OFFSET_MPIOSEL_LO
;
81 unsigned int bit
= (offset
+ exar_gpio
->first_pin
) % 8;
83 return !!(exar_get(chip
, addr
) & BIT(bit
));
86 static int exar_get_value(struct gpio_chip
*chip
, unsigned int offset
)
88 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
89 unsigned int addr
= (offset
+ exar_gpio
->first_pin
) / 8 ?
90 EXAR_OFFSET_MPIOLVL_HI
: EXAR_OFFSET_MPIOLVL_LO
;
91 unsigned int bit
= (offset
+ exar_gpio
->first_pin
) % 8;
93 return !!(exar_get(chip
, addr
) & BIT(bit
));
96 static void exar_set_value(struct gpio_chip
*chip
, unsigned int offset
,
99 struct exar_gpio_chip
*exar_gpio
= gpiochip_get_data(chip
);
100 unsigned int addr
= (offset
+ exar_gpio
->first_pin
) / 8 ?
101 EXAR_OFFSET_MPIOLVL_HI
: EXAR_OFFSET_MPIOLVL_LO
;
102 unsigned int bit
= (offset
+ exar_gpio
->first_pin
) % 8;
104 exar_update(chip
, addr
, value
, bit
);
107 static int exar_direction_output(struct gpio_chip
*chip
, unsigned int offset
,
110 exar_set_value(chip
, offset
, value
);
111 return exar_set_direction(chip
, 0, offset
);
114 static int exar_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
116 return exar_set_direction(chip
, 1, offset
);
119 static int gpio_exar_probe(struct platform_device
*pdev
)
121 struct pci_dev
*pcidev
= to_pci_dev(pdev
->dev
.parent
);
122 struct exar_gpio_chip
*exar_gpio
;
123 u32 first_pin
, ngpios
;
128 * The UART driver must have mapped region 0 prior to registering this
131 p
= pcim_iomap_table(pcidev
)[0];
135 ret
= device_property_read_u32(&pdev
->dev
, "exar,first-pin",
140 ret
= device_property_read_u32(&pdev
->dev
, "ngpios", &ngpios
);
144 exar_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*exar_gpio
), GFP_KERNEL
);
148 mutex_init(&exar_gpio
->lock
);
150 index
= ida_simple_get(&ida_index
, 0, 0, GFP_KERNEL
);
152 sprintf(exar_gpio
->name
, "exar_gpio%d", index
);
153 exar_gpio
->gpio_chip
.label
= exar_gpio
->name
;
154 exar_gpio
->gpio_chip
.parent
= &pdev
->dev
;
155 exar_gpio
->gpio_chip
.direction_output
= exar_direction_output
;
156 exar_gpio
->gpio_chip
.direction_input
= exar_direction_input
;
157 exar_gpio
->gpio_chip
.get_direction
= exar_get_direction
;
158 exar_gpio
->gpio_chip
.get
= exar_get_value
;
159 exar_gpio
->gpio_chip
.set
= exar_set_value
;
160 exar_gpio
->gpio_chip
.base
= -1;
161 exar_gpio
->gpio_chip
.ngpio
= ngpios
;
163 exar_gpio
->index
= index
;
164 exar_gpio
->first_pin
= first_pin
;
166 ret
= devm_gpiochip_add_data(&pdev
->dev
,
167 &exar_gpio
->gpio_chip
, exar_gpio
);
171 platform_set_drvdata(pdev
, exar_gpio
);
176 ida_simple_remove(&ida_index
, index
);
177 mutex_destroy(&exar_gpio
->lock
);
181 static int gpio_exar_remove(struct platform_device
*pdev
)
183 struct exar_gpio_chip
*exar_gpio
= platform_get_drvdata(pdev
);
185 ida_simple_remove(&ida_index
, exar_gpio
->index
);
186 mutex_destroy(&exar_gpio
->lock
);
191 static struct platform_driver gpio_exar_driver
= {
192 .probe
= gpio_exar_probe
,
193 .remove
= gpio_exar_remove
,
199 module_platform_driver(gpio_exar_driver
);
201 MODULE_ALIAS("platform:" DRIVER_NAME
);
202 MODULE_DESCRIPTION("Exar GPIO driver");
203 MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
204 MODULE_LICENSE("GPL");