2 * GPIO interface for IT87xx Super I/O chips
4 * Author: Diego Elio Pettenò <flameeyes@flameeyes.eu>
5 * Copyright (c) 2017 Google, Inc.
7 * Based on it87_wdt.c by Oliver Schuster
8 * gpio-it8761e.c by Denis Turischev
9 * gpio-stmpe.c by Rabin Vincent
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License 2 as published
13 * by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/ioport.h>
33 #include <linux/slab.h>
34 #include <linux/gpio/driver.h>
37 #define NO_DEV_ID 0xffff
38 #define IT8613_ID 0x8613
39 #define IT8620_ID 0x8620
40 #define IT8628_ID 0x8628
41 #define IT8718_ID 0x8718
42 #define IT8728_ID 0x8728
43 #define IT8732_ID 0x8732
44 #define IT8761_ID 0x8761
45 #define IT8772_ID 0x8772
46 #define IT8786_ID 0x8786
52 /* Logical device Numbers LDN */
55 /* Configuration Registers and Functions */
61 * struct it87_gpio - it87-specific GPIO chip
62 * @chip the underlying gpio_chip structure
63 * @lock a lock to avoid races between operations
64 * @io_base base address for gpio ports
65 * @io_size size of the port rage starting from io_base.
66 * @output_base Super I/O register address for Output Enable register
67 * @simple_base Super I/O 'Simple I/O' Enable register
68 * @simple_size Super IO 'Simple I/O' Enable register size; this is
69 * required because IT87xx chips might only provide Simple I/O
70 * switches on a subset of lines, whereas the others keep the
71 * same status all time.
74 struct gpio_chip chip
;
83 static struct it87_gpio it87_gpio_chip
= {
84 .lock
= __SPIN_LOCK_UNLOCKED(it87_gpio_chip
.lock
),
87 /* Superio chip access functions; copied from wdt_it87 */
89 static inline int superio_enter(void)
92 * Try to reserve REG and REG + 1 for exclusive access.
94 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
104 static inline void superio_exit(void)
108 release_region(REG
, 2);
111 static inline void superio_select(int ldn
)
117 static inline int superio_inb(int reg
)
123 static inline void superio_outb(int val
, int reg
)
129 static inline int superio_inw(int reg
)
140 static inline void superio_outw(int val
, int reg
)
148 static inline void superio_set_mask(int mask
, int reg
)
150 u8 curr_val
= superio_inb(reg
);
151 u8 new_val
= curr_val
| mask
;
153 if (curr_val
!= new_val
)
154 superio_outb(new_val
, reg
);
157 static inline void superio_clear_mask(int mask
, int reg
)
159 u8 curr_val
= superio_inb(reg
);
160 u8 new_val
= curr_val
& ~mask
;
162 if (curr_val
!= new_val
)
163 superio_outb(new_val
, reg
);
166 static int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
170 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
172 mask
= 1 << (gpio_num
% 8);
173 group
= (gpio_num
/ 8);
175 spin_lock(&it87_gpio
->lock
);
177 rc
= superio_enter();
181 /* not all the IT87xx chips support Simple I/O and not all of
182 * them allow all the lines to be set/unset to Simple I/O.
184 if (group
< it87_gpio
->simple_size
)
185 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
187 /* clear output enable, setting the pin to input, as all the
188 * newly-exported GPIO interfaces are set to input.
190 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
195 spin_unlock(&it87_gpio
->lock
);
199 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
203 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
205 mask
= 1 << (gpio_num
% 8);
206 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
208 return !!(inb(reg
) & mask
);
211 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
215 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
217 mask
= 1 << (gpio_num
% 8);
218 group
= (gpio_num
/ 8);
220 spin_lock(&it87_gpio
->lock
);
222 rc
= superio_enter();
226 /* clear the output enable bit */
227 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
232 spin_unlock(&it87_gpio
->lock
);
236 static void it87_gpio_set(struct gpio_chip
*chip
,
237 unsigned gpio_num
, int val
)
241 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
243 mask
= 1 << (gpio_num
% 8);
244 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
246 curr_vals
= inb(reg
);
248 outb(curr_vals
| mask
, reg
);
250 outb(curr_vals
& ~mask
, reg
);
253 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
254 unsigned gpio_num
, int val
)
258 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
260 mask
= 1 << (gpio_num
% 8);
261 group
= (gpio_num
/ 8);
263 spin_lock(&it87_gpio
->lock
);
265 rc
= superio_enter();
269 /* set the output enable bit */
270 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
272 it87_gpio_set(chip
, gpio_num
, val
);
277 spin_unlock(&it87_gpio
->lock
);
281 static const struct gpio_chip it87_template_chip
= {
282 .label
= KBUILD_MODNAME
,
283 .owner
= THIS_MODULE
,
284 .request
= it87_gpio_request
,
285 .get
= it87_gpio_get
,
286 .direction_input
= it87_gpio_direction_in
,
287 .set
= it87_gpio_set
,
288 .direction_output
= it87_gpio_direction_out
,
292 static int __init
it87_gpio_init(void)
296 u8 chip_rev
, gpio_ba_reg
;
297 char *labels
, **labels_table
;
299 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
301 rc
= superio_enter();
305 chip_type
= superio_inw(CHIPID
);
306 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
309 it87_gpio
->chip
= it87_template_chip
;
314 it87_gpio
->io_size
= 8; /* it8613 only needs 6, use 8 for alignment */
315 it87_gpio
->output_base
= 0xc8;
316 it87_gpio
->simple_base
= 0xc0;
317 it87_gpio
->simple_size
= 6;
318 it87_gpio
->chip
.ngpio
= 64; /* has 48, use 64 for convenient calc */
323 it87_gpio
->io_size
= 11;
324 it87_gpio
->output_base
= 0xc8;
325 it87_gpio
->simple_size
= 0;
326 it87_gpio
->chip
.ngpio
= 64;
334 it87_gpio
->io_size
= 8;
335 it87_gpio
->output_base
= 0xc8;
336 it87_gpio
->simple_base
= 0xc0;
337 it87_gpio
->simple_size
= 5;
338 it87_gpio
->chip
.ngpio
= 64;
342 it87_gpio
->io_size
= 4;
343 it87_gpio
->output_base
= 0xf0;
344 it87_gpio
->simple_size
= 0;
345 it87_gpio
->chip
.ngpio
= 16;
348 pr_err("no device\n");
351 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
352 chip_type
, chip_rev
);
356 rc
= superio_enter();
360 superio_select(GPIO
);
362 /* fetch GPIO base address */
363 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
367 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
368 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
371 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
375 /* Set up aliases for the GPIO connection.
377 * ITE documentation for recent chips such as the IT8728F
378 * refers to the GPIO lines as GPxy, with a coordinates system
379 * where x is the GPIO group (starting from 1) and y is the
380 * bit within the group.
382 * By creating these aliases, we make it easier to understand
383 * to which GPIO pin we're referring to.
385 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
387 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
390 if (!labels
|| !labels_table
) {
395 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
396 char *label
= &labels
[i
* sizeof("it87_gpXY")];
398 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
399 labels_table
[i
] = label
;
402 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
404 rc
= gpiochip_add_data(&it87_gpio
->chip
, it87_gpio
);
413 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
417 static void __exit
it87_gpio_exit(void)
419 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
421 gpiochip_remove(&it87_gpio
->chip
);
422 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
423 kfree(it87_gpio
->chip
.names
[0]);
424 kfree(it87_gpio
->chip
.names
);
427 module_init(it87_gpio_init
);
428 module_exit(it87_gpio_exit
);
430 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
431 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
432 MODULE_LICENSE("GPL");