2 * GPIO interface for IT87xx Super I/O chips
4 * Author: Diego Elio Pettenò <flameeyes@flameeyes.eu>
6 * Based on it87_wdt.c by Oliver Schuster
7 * gpio-it8761e.c by Denis Turischev
8 * gpio-stmpe.c by Rabin Vincent
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License 2 as published
12 * by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/gpio.h>
36 #define NO_DEV_ID 0xffff
37 #define IT8728_ID 0x8728
38 #define IT8732_ID 0x8732
39 #define IT8761_ID 0x8761
45 /* Logical device Numbers LDN */
48 /* Configuration Registers and Functions */
54 * struct it87_gpio - it87-specific GPIO chip
55 * @chip the underlying gpio_chip structure
56 * @lock a lock to avoid races between operations
57 * @io_base base address for gpio ports
58 * @io_size size of the port rage starting from io_base.
59 * @output_base Super I/O register address for Output Enable register
60 * @simple_base Super I/O 'Simple I/O' Enable register
61 * @simple_size Super IO 'Simple I/O' Enable register size; this is
62 * required because IT87xx chips might only provide Simple I/O
63 * switches on a subset of lines, whereas the others keep the
64 * same status all time.
67 struct gpio_chip chip
;
76 static struct it87_gpio it87_gpio_chip
= {
77 .lock
= __SPIN_LOCK_UNLOCKED(it87_gpio_chip
.lock
),
80 /* Superio chip access functions; copied from wdt_it87 */
82 static inline int superio_enter(void)
85 * Try to reserve REG and REG + 1 for exclusive access.
87 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
97 static inline void superio_exit(void)
101 release_region(REG
, 2);
104 static inline void superio_select(int ldn
)
110 static inline int superio_inb(int reg
)
116 static inline void superio_outb(int val
, int reg
)
122 static inline int superio_inw(int reg
)
133 static inline void superio_outw(int val
, int reg
)
141 static inline void superio_set_mask(int mask
, int reg
)
143 u8 curr_val
= superio_inb(reg
);
144 u8 new_val
= curr_val
| mask
;
146 if (curr_val
!= new_val
)
147 superio_outb(new_val
, reg
);
150 static inline void superio_clear_mask(int mask
, int reg
)
152 u8 curr_val
= superio_inb(reg
);
153 u8 new_val
= curr_val
& ~mask
;
155 if (curr_val
!= new_val
)
156 superio_outb(new_val
, reg
);
159 static int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
163 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
165 mask
= 1 << (gpio_num
% 8);
166 group
= (gpio_num
/ 8);
168 spin_lock(&it87_gpio
->lock
);
170 rc
= superio_enter();
174 /* not all the IT87xx chips support Simple I/O and not all of
175 * them allow all the lines to be set/unset to Simple I/O.
177 if (group
< it87_gpio
->simple_size
)
178 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
180 /* clear output enable, setting the pin to input, as all the
181 * newly-exported GPIO interfaces are set to input.
183 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
188 spin_unlock(&it87_gpio
->lock
);
192 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
196 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
198 mask
= 1 << (gpio_num
% 8);
199 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
201 return !!(inb(reg
) & mask
);
204 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
208 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
210 mask
= 1 << (gpio_num
% 8);
211 group
= (gpio_num
/ 8);
213 spin_lock(&it87_gpio
->lock
);
215 rc
= superio_enter();
219 /* clear the output enable bit */
220 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
225 spin_unlock(&it87_gpio
->lock
);
229 static void it87_gpio_set(struct gpio_chip
*chip
,
230 unsigned gpio_num
, int val
)
234 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
236 mask
= 1 << (gpio_num
% 8);
237 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
239 curr_vals
= inb(reg
);
241 outb(curr_vals
| mask
, reg
);
243 outb(curr_vals
& ~mask
, reg
);
246 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
247 unsigned gpio_num
, int val
)
251 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
253 mask
= 1 << (gpio_num
% 8);
254 group
= (gpio_num
/ 8);
256 spin_lock(&it87_gpio
->lock
);
258 rc
= superio_enter();
262 /* set the output enable bit */
263 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
265 it87_gpio_set(chip
, gpio_num
, val
);
270 spin_unlock(&it87_gpio
->lock
);
274 static struct gpio_chip it87_template_chip
= {
275 .label
= KBUILD_MODNAME
,
276 .owner
= THIS_MODULE
,
277 .request
= it87_gpio_request
,
278 .get
= it87_gpio_get
,
279 .direction_input
= it87_gpio_direction_in
,
280 .set
= it87_gpio_set
,
281 .direction_output
= it87_gpio_direction_out
,
285 static int __init
it87_gpio_init(void)
289 u8 chip_rev
, gpio_ba_reg
;
290 char *labels
, **labels_table
;
292 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
294 rc
= superio_enter();
298 chip_type
= superio_inw(CHIPID
);
299 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
302 it87_gpio
->chip
= it87_template_chip
;
308 it87_gpio
->io_size
= 8;
309 it87_gpio
->output_base
= 0xc8;
310 it87_gpio
->simple_base
= 0xc0;
311 it87_gpio
->simple_size
= 5;
312 it87_gpio
->chip
.ngpio
= 64;
316 it87_gpio
->io_size
= 4;
317 it87_gpio
->output_base
= 0xf0;
318 it87_gpio
->simple_size
= 0;
319 it87_gpio
->chip
.ngpio
= 16;
322 pr_err("no device\n");
325 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
326 chip_type
, chip_rev
);
330 rc
= superio_enter();
334 superio_select(GPIO
);
336 /* fetch GPIO base address */
337 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
341 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
342 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
345 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
349 /* Set up aliases for the GPIO connection.
351 * ITE documentation for recent chips such as the IT8728F
352 * refers to the GPIO lines as GPxy, with a coordinates system
353 * where x is the GPIO group (starting from 1) and y is the
354 * bit within the group.
356 * By creating these aliases, we make it easier to understand
357 * to which GPIO pin we're referring to.
359 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
361 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
364 if (!labels
|| !labels_table
) {
369 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
370 char *label
= &labels
[i
* sizeof("it87_gpXY")];
372 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
373 labels_table
[i
] = label
;
376 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
378 rc
= gpiochip_add_data(&it87_gpio
->chip
, it87_gpio
);
387 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
391 static void __exit
it87_gpio_exit(void)
393 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
395 gpiochip_remove(&it87_gpio
->chip
);
396 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
397 kfree(it87_gpio
->chip
.names
[0]);
398 kfree(it87_gpio
->chip
.names
);
401 module_init(it87_gpio_init
);
402 module_exit(it87_gpio_exit
);
404 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
405 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
406 MODULE_LICENSE("GPL");