blob: f3585a184f392d1a25f193ee43e81dec748ee7b6 [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];
34};
35
36static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
37 unsigned int offset)
38{
39 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
40 int temp;
41
42 mutex_lock(&exar_gpio->lock);
43 temp = readb(exar_gpio->regs + reg);
44 temp &= ~BIT(offset);
45 if (val)
46 temp |= BIT(offset);
47 writeb(temp, exar_gpio->regs + reg);
48 mutex_unlock(&exar_gpio->lock);
49}
50
51static int exar_set_direction(struct gpio_chip *chip, int direction,
52 unsigned int offset)
53{
54 unsigned int bank = offset / 8;
55 unsigned int addr;
56
57 addr = bank ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
58 exar_update(chip, addr, direction, offset % 8);
59 return 0;
60}
61
Sudip Mukherjee6596e592017-01-19 22:23:20 +000062static int exar_get(struct gpio_chip *chip, unsigned int reg)
63{
64 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
65 int value;
66
67 mutex_lock(&exar_gpio->lock);
68 value = readb(exar_gpio->regs + reg);
69 mutex_unlock(&exar_gpio->lock);
70
Jan Kiszka7f45a872017-06-09 20:33:13 +020071 return value;
Sudip Mukherjee6596e592017-01-19 22:23:20 +000072}
73
74static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
75{
76 unsigned int bank = offset / 8;
77 unsigned int addr;
78 int val;
79
80 addr = bank ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
Jan Kiszka7f45a872017-06-09 20:33:13 +020081 val = exar_get(chip, addr) & BIT(offset % 8);
Sudip Mukherjee6596e592017-01-19 22:23:20 +000082
83 return !!val;
84}
85
86static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
87{
88 unsigned int bank = offset / 8;
89 unsigned int addr;
90 int val;
91
Jan Kiszka7f45a872017-06-09 20:33:13 +020092 addr = bank ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
93 val = exar_get(chip, addr) & BIT(offset % 8);
Sudip Mukherjee6596e592017-01-19 22:23:20 +000094
95 return !!val;
96}
97
98static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
99 int value)
100{
101 unsigned int bank = offset / 8;
102 unsigned int addr;
103
104 addr = bank ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
105 exar_update(chip, addr, value, offset % 8);
106}
107
Axel Lineddeae02017-02-26 22:36:58 +0800108static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
109 int value)
110{
111 exar_set_value(chip, offset, value);
112 return exar_set_direction(chip, 0, offset);
113}
114
115static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
116{
117 return exar_set_direction(chip, 1, offset);
118}
119
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000120static int gpio_exar_probe(struct platform_device *pdev)
121{
Jan Kiszkad3936d72017-06-09 20:33:10 +0200122 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000123 struct exar_gpio_chip *exar_gpio;
124 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 Kiszka5dab5872017-06-09 20:33:11 +0200135 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000136 if (!exar_gpio)
137 return -ENOMEM;
138
139 mutex_init(&exar_gpio->lock);
140
141 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
142
143 sprintf(exar_gpio->name, "exar_gpio%d", index);
144 exar_gpio->gpio_chip.label = exar_gpio->name;
145 exar_gpio->gpio_chip.parent = &pcidev->dev;
146 exar_gpio->gpio_chip.direction_output = exar_direction_output;
147 exar_gpio->gpio_chip.direction_input = exar_direction_input;
148 exar_gpio->gpio_chip.get_direction = exar_get_direction;
149 exar_gpio->gpio_chip.get = exar_get_value;
150 exar_gpio->gpio_chip.set = exar_set_value;
151 exar_gpio->gpio_chip.base = -1;
152 exar_gpio->gpio_chip.ngpio = 16;
153 exar_gpio->regs = p;
154 exar_gpio->index = index;
155
Jan Kiszka5dab5872017-06-09 20:33:11 +0200156 ret = devm_gpiochip_add_data(&pdev->dev,
Sudip Mukherjee6596e592017-01-19 22:23:20 +0000157 &exar_gpio->gpio_chip, exar_gpio);
158 if (ret)
159 goto err_destroy;
160
161 platform_set_drvdata(pdev, exar_gpio);
162
163 return 0;
164
165err_destroy:
166 ida_simple_remove(&ida_index, index);
167 mutex_destroy(&exar_gpio->lock);
168 return ret;
169}
170
171static int gpio_exar_remove(struct platform_device *pdev)
172{
173 struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
174
175 ida_simple_remove(&ida_index, exar_gpio->index);
176 mutex_destroy(&exar_gpio->lock);
177
178 return 0;
179}
180
181static struct platform_driver gpio_exar_driver = {
182 .probe = gpio_exar_probe,
183 .remove = gpio_exar_remove,
184 .driver = {
185 .name = DRIVER_NAME,
186 },
187};
188
189module_platform_driver(gpio_exar_driver);
190
191MODULE_ALIAS("platform:" DRIVER_NAME);
192MODULE_DESCRIPTION("Exar GPIO driver");
193MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
194MODULE_LICENSE("GPL");