blob: 0ecd2369c2cad0daa5e08696ab85b91af5235a26 [file] [log] [blame]
Sudip Mukherjee6596e592017-01-19 22:23:20 +00001/*
2 * GPIO driver for Exar XR17V35X chip
3 *
4 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
5 *
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.
9 */
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>
18
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
23
24#define DRIVER_NAME "gpio_exar"
25
26static DEFINE_IDA(ida_index);
27
28struct exar_gpio_chip {
29 struct gpio_chip gpio_chip;
30 struct mutex lock;
31 int index;
32 void __iomem *regs;
33 char name[20];
Jan Kiszka380b1e22017-05-22 12:43:18 +020034 unsigned int first_pin;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000035};
36
37static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
38 unsigned int offset)
39{
40 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
41 int temp;
42
43 mutex_lock(&exar_gpio->lock);
44 temp = readb(exar_gpio->regs + reg);
45 temp &= ~BIT(offset);
46 if (val)
47 temp |= BIT(offset);
48 writeb(temp, exar_gpio->regs + reg);
49 mutex_unlock(&exar_gpio->lock);
50}
51
52static int exar_set_direction(struct gpio_chip *chip, int direction,
53 unsigned int offset)
54{
Jan Kiszka380b1e22017-05-22 12:43:18 +020055 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;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000059
Jan Kiszka380b1e22017-05-22 12:43:18 +020060 exar_update(chip, addr, direction, bit);
Sudip Mukherjee6596e592017-01-19 22:23:20 +000061 return 0;
62}
63
Sudip Mukherjee6596e592017-01-19 22:23:20 +000064static int exar_get(struct gpio_chip *chip, unsigned int reg)
65{
66 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
67 int value;
68
69 mutex_lock(&exar_gpio->lock);
70 value = readb(exar_gpio->regs + reg);
71 mutex_unlock(&exar_gpio->lock);
72
Jan Kiszka7f45a872017-06-09 20:33:13 +020073 return value;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000074}
75
76static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
77{
Jan Kiszka380b1e22017-05-22 12:43:18 +020078 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;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000082
Jan Kiszka380b1e22017-05-22 12:43:18 +020083 return !!(exar_get(chip, addr) & BIT(bit));
Sudip Mukherjee6596e592017-01-19 22:23:20 +000084}
85
86static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
87{
Jan Kiszka380b1e22017-05-22 12:43:18 +020088 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;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000092
Jan Kiszka380b1e22017-05-22 12:43:18 +020093 return !!(exar_get(chip, addr) & BIT(bit));
Sudip Mukherjee6596e592017-01-19 22:23:20 +000094}
95
96static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
97 int value)
98{
Jan Kiszka380b1e22017-05-22 12:43:18 +020099 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;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000103
Jan Kiszka380b1e22017-05-22 12:43:18 +0200104 exar_update(chip, addr, value, bit);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000105}
106
Axel Lineddeae02017-02-26 22:36:58 +0800107static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
108 int value)
109{
110 exar_set_value(chip, offset, value);
111 return exar_set_direction(chip, 0, offset);
112}
113
114static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
115{
116 return exar_set_direction(chip, 1, offset);
117}
118
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000119static int gpio_exar_probe(struct platform_device *pdev)
120{
Jan Kiszkad3936d72017-06-09 20:33:10 +0200121 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000122 struct exar_gpio_chip *exar_gpio;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200123 u32 first_pin, ngpios;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000124 void __iomem *p;
125 int index, ret;
126
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000127 /*
Jan Kiszka8847f5f2017-05-02 08:42:40 +0200128 * The UART driver must have mapped region 0 prior to registering this
129 * device - use it.
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000130 */
Jan Kiszka8847f5f2017-05-02 08:42:40 +0200131 p = pcim_iomap_table(pcidev)[0];
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000132 if (!p)
133 return -ENOMEM;
134
Jan Kiszkaa589e212017-07-19 07:31:14 +0200135 ret = device_property_read_u32(&pdev->dev, "exar,first-pin",
Jan Kiszka380b1e22017-05-22 12:43:18 +0200136 &first_pin);
137 if (ret)
138 return ret;
139
140 ret = device_property_read_u32(&pdev->dev, "ngpios", &ngpios);
141 if (ret)
142 return ret;
143
Jan Kiszka5dab5872017-06-09 20:33:11 +0200144 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000145 if (!exar_gpio)
146 return -ENOMEM;
147
148 mutex_init(&exar_gpio->lock);
149
150 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
151
152 sprintf(exar_gpio->name, "exar_gpio%d", index);
153 exar_gpio->gpio_chip.label = exar_gpio->name;
Jan Kiszka4076cf02017-05-21 11:49:24 +0200154 exar_gpio->gpio_chip.parent = &pdev->dev;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000155 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;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200161 exar_gpio->gpio_chip.ngpio = ngpios;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000162 exar_gpio->regs = p;
163 exar_gpio->index = index;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200164 exar_gpio->first_pin = first_pin;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000165
Jan Kiszka5dab5872017-06-09 20:33:11 +0200166 ret = devm_gpiochip_add_data(&pdev->dev,
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000167 &exar_gpio->gpio_chip, exar_gpio);
168 if (ret)
169 goto err_destroy;
170
171 platform_set_drvdata(pdev, exar_gpio);
172
173 return 0;
174
175err_destroy:
176 ida_simple_remove(&ida_index, index);
177 mutex_destroy(&exar_gpio->lock);
178 return ret;
179}
180
181static int gpio_exar_remove(struct platform_device *pdev)
182{
183 struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
184
185 ida_simple_remove(&ida_index, exar_gpio->index);
186 mutex_destroy(&exar_gpio->lock);
187
188 return 0;
189}
190
191static struct platform_driver gpio_exar_driver = {
192 .probe = gpio_exar_probe,
193 .remove = gpio_exar_remove,
194 .driver = {
195 .name = DRIVER_NAME,
196 },
197};
198
199module_platform_driver(gpio_exar_driver);
200
201MODULE_ALIAS("platform:" DRIVER_NAME);
202MODULE_DESCRIPTION("Exar GPIO driver");
203MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
204MODULE_LICENSE("GPL");