blob: fae327d5b06e80a7728aef8d9dfea885b7f645cf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Sudip Mukherjee6596e592017-01-19 22:23:20 +00002/*
3 * GPIO driver for Exar XR17V35X chip
4 *
5 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
Sudip Mukherjee6596e592017-01-19 22:23:20 +00006 */
7#include <linux/bitops.h>
8#include <linux/device.h>
9#include <linux/gpio/driver.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/pci.h>
14#include <linux/platform_device.h>
15
16#define EXAR_OFFSET_MPIOLVL_LO 0x90
17#define EXAR_OFFSET_MPIOSEL_LO 0x93
18#define EXAR_OFFSET_MPIOLVL_HI 0x96
19#define EXAR_OFFSET_MPIOSEL_HI 0x99
20
21#define DRIVER_NAME "gpio_exar"
22
23static DEFINE_IDA(ida_index);
24
25struct exar_gpio_chip {
26 struct gpio_chip gpio_chip;
27 struct mutex lock;
28 int index;
29 void __iomem *regs;
30 char name[20];
Jan Kiszka380b1e22017-05-22 12:43:18 +020031 unsigned int first_pin;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000032};
33
34static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
35 unsigned int offset)
36{
37 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
38 int temp;
39
40 mutex_lock(&exar_gpio->lock);
41 temp = readb(exar_gpio->regs + reg);
42 temp &= ~BIT(offset);
43 if (val)
44 temp |= BIT(offset);
45 writeb(temp, exar_gpio->regs + reg);
46 mutex_unlock(&exar_gpio->lock);
47}
48
49static int exar_set_direction(struct gpio_chip *chip, int direction,
50 unsigned int offset)
51{
Jan Kiszka380b1e22017-05-22 12:43:18 +020052 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
53 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
54 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
55 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000056
Jan Kiszka380b1e22017-05-22 12:43:18 +020057 exar_update(chip, addr, direction, bit);
Sudip Mukherjee6596e592017-01-19 22:23:20 +000058 return 0;
59}
60
Sudip Mukherjee6596e592017-01-19 22:23:20 +000061static int exar_get(struct gpio_chip *chip, unsigned int reg)
62{
63 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
64 int value;
65
66 mutex_lock(&exar_gpio->lock);
67 value = readb(exar_gpio->regs + reg);
68 mutex_unlock(&exar_gpio->lock);
69
Jan Kiszka7f45a872017-06-09 20:33:13 +020070 return value;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000071}
72
73static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
74{
Jan Kiszka380b1e22017-05-22 12:43:18 +020075 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
76 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
77 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
78 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000079
Jan Kiszka380b1e22017-05-22 12:43:18 +020080 return !!(exar_get(chip, addr) & BIT(bit));
Sudip Mukherjee6596e592017-01-19 22:23:20 +000081}
82
83static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
84{
Jan Kiszka380b1e22017-05-22 12:43:18 +020085 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
86 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
87 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
88 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000089
Jan Kiszka380b1e22017-05-22 12:43:18 +020090 return !!(exar_get(chip, addr) & BIT(bit));
Sudip Mukherjee6596e592017-01-19 22:23:20 +000091}
92
93static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
94 int value)
95{
Jan Kiszka380b1e22017-05-22 12:43:18 +020096 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
97 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
98 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
99 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000100
Jan Kiszka380b1e22017-05-22 12:43:18 +0200101 exar_update(chip, addr, value, bit);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000102}
103
Axel Lineddeae02017-02-26 22:36:58 +0800104static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
105 int value)
106{
107 exar_set_value(chip, offset, value);
108 return exar_set_direction(chip, 0, offset);
109}
110
111static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
112{
113 return exar_set_direction(chip, 1, offset);
114}
115
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000116static int gpio_exar_probe(struct platform_device *pdev)
117{
Jan Kiszkad3936d72017-06-09 20:33:10 +0200118 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000119 struct exar_gpio_chip *exar_gpio;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200120 u32 first_pin, ngpios;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000121 void __iomem *p;
122 int index, ret;
123
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000124 /*
Jan Kiszka8847f5f2017-05-02 08:42:40 +0200125 * The UART driver must have mapped region 0 prior to registering this
126 * device - use it.
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000127 */
Jan Kiszka8847f5f2017-05-02 08:42:40 +0200128 p = pcim_iomap_table(pcidev)[0];
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000129 if (!p)
130 return -ENOMEM;
131
Jan Kiszkaa589e212017-07-19 07:31:14 +0200132 ret = device_property_read_u32(&pdev->dev, "exar,first-pin",
Jan Kiszka380b1e22017-05-22 12:43:18 +0200133 &first_pin);
134 if (ret)
135 return ret;
136
137 ret = device_property_read_u32(&pdev->dev, "ngpios", &ngpios);
138 if (ret)
139 return ret;
140
Jan Kiszka5dab5872017-06-09 20:33:11 +0200141 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000142 if (!exar_gpio)
143 return -ENOMEM;
144
145 mutex_init(&exar_gpio->lock);
146
147 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
Kangjie Lu7ecced02019-03-08 22:07:57 -0600148 if (index < 0)
149 goto err_destroy;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000150
151 sprintf(exar_gpio->name, "exar_gpio%d", index);
152 exar_gpio->gpio_chip.label = exar_gpio->name;
Jan Kiszka4076cf02017-05-21 11:49:24 +0200153 exar_gpio->gpio_chip.parent = &pdev->dev;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000154 exar_gpio->gpio_chip.direction_output = exar_direction_output;
155 exar_gpio->gpio_chip.direction_input = exar_direction_input;
156 exar_gpio->gpio_chip.get_direction = exar_get_direction;
157 exar_gpio->gpio_chip.get = exar_get_value;
158 exar_gpio->gpio_chip.set = exar_set_value;
159 exar_gpio->gpio_chip.base = -1;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200160 exar_gpio->gpio_chip.ngpio = ngpios;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000161 exar_gpio->regs = p;
162 exar_gpio->index = index;
Jan Kiszka380b1e22017-05-22 12:43:18 +0200163 exar_gpio->first_pin = first_pin;
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000164
Jan Kiszka5dab5872017-06-09 20:33:11 +0200165 ret = devm_gpiochip_add_data(&pdev->dev,
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000166 &exar_gpio->gpio_chip, exar_gpio);
167 if (ret)
168 goto err_destroy;
169
170 platform_set_drvdata(pdev, exar_gpio);
171
172 return 0;
173
174err_destroy:
175 ida_simple_remove(&ida_index, index);
176 mutex_destroy(&exar_gpio->lock);
177 return ret;
178}
179
180static int gpio_exar_remove(struct platform_device *pdev)
181{
182 struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
183
184 ida_simple_remove(&ida_index, exar_gpio->index);
185 mutex_destroy(&exar_gpio->lock);
186
187 return 0;
188}
189
190static struct platform_driver gpio_exar_driver = {
191 .probe = gpio_exar_probe,
192 .remove = gpio_exar_remove,
193 .driver = {
194 .name = DRIVER_NAME,
195 },
196};
197
198module_platform_driver(gpio_exar_driver);
199
200MODULE_ALIAS("platform:" DRIVER_NAME);
201MODULE_DESCRIPTION("Exar GPIO driver");
202MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
203MODULE_LICENSE("GPL");