xref: /openbmc/linux/drivers/gpio/gpio-exar.c (revision a39f2fe7)
1 /*
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 
26 static DEFINE_IDA(ida_index);
27 
28 struct 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 
36 static 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 
51 static 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 
62 static 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 
71 	return value;
72 }
73 
74 static 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;
81 	val = exar_get(chip, addr) & BIT(offset % 8);
82 
83 	return !!val;
84 }
85 
86 static 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 
92 	addr = bank ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
93 	val = exar_get(chip, addr) & BIT(offset % 8);
94 
95 	return !!val;
96 }
97 
98 static 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 
108 static 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 
115 static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
116 {
117 	return exar_set_direction(chip, 1, offset);
118 }
119 
120 static int gpio_exar_probe(struct platform_device *pdev)
121 {
122 	struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
123 	struct exar_gpio_chip *exar_gpio;
124 	void __iomem *p;
125 	int index, ret;
126 
127 	/*
128 	 * Map the pci device to get the register addresses.
129 	 * We will need to read and write those registers to control
130 	 * the GPIO pins.
131 	 * Using managed functions will save us from unmaping on exit.
132 	 * As the device is enabled using managed functions by the
133 	 * UART driver we can also use managed functions here.
134 	 */
135 	p = pcim_iomap(pcidev, 0, 0);
136 	if (!p)
137 		return -ENOMEM;
138 
139 	exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
140 	if (!exar_gpio)
141 		return -ENOMEM;
142 
143 	mutex_init(&exar_gpio->lock);
144 
145 	index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
146 
147 	sprintf(exar_gpio->name, "exar_gpio%d", index);
148 	exar_gpio->gpio_chip.label = exar_gpio->name;
149 	exar_gpio->gpio_chip.parent = &pcidev->dev;
150 	exar_gpio->gpio_chip.direction_output = exar_direction_output;
151 	exar_gpio->gpio_chip.direction_input = exar_direction_input;
152 	exar_gpio->gpio_chip.get_direction = exar_get_direction;
153 	exar_gpio->gpio_chip.get = exar_get_value;
154 	exar_gpio->gpio_chip.set = exar_set_value;
155 	exar_gpio->gpio_chip.base = -1;
156 	exar_gpio->gpio_chip.ngpio = 16;
157 	exar_gpio->regs = p;
158 	exar_gpio->index = index;
159 
160 	ret = devm_gpiochip_add_data(&pdev->dev,
161 				     &exar_gpio->gpio_chip, exar_gpio);
162 	if (ret)
163 		goto err_destroy;
164 
165 	platform_set_drvdata(pdev, exar_gpio);
166 
167 	return 0;
168 
169 err_destroy:
170 	ida_simple_remove(&ida_index, index);
171 	mutex_destroy(&exar_gpio->lock);
172 	return ret;
173 }
174 
175 static int gpio_exar_remove(struct platform_device *pdev)
176 {
177 	struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
178 
179 	ida_simple_remove(&ida_index, exar_gpio->index);
180 	mutex_destroy(&exar_gpio->lock);
181 
182 	return 0;
183 }
184 
185 static struct platform_driver gpio_exar_driver = {
186 	.probe	= gpio_exar_probe,
187 	.remove	= gpio_exar_remove,
188 	.driver	= {
189 		.name = DRIVER_NAME,
190 	},
191 };
192 
193 module_platform_driver(gpio_exar_driver);
194 
195 MODULE_ALIAS("platform:" DRIVER_NAME);
196 MODULE_DESCRIPTION("Exar GPIO driver");
197 MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
198 MODULE_LICENSE("GPL");
199