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.h>
37 #define NO_DEV_ID 0xffff
38 #define IT8620_ID 0x8620
39 #define IT8628_ID 0x8628
40 #define IT8728_ID 0x8728
41 #define IT8732_ID 0x8732
42 #define IT8761_ID 0x8761
43 #define IT8772_ID 0x8772
49 /* Logical device Numbers LDN */
52 /* Configuration Registers and Functions */
58 * struct it87_gpio - it87-specific GPIO chip
59 * @chip the underlying gpio_chip structure
60 * @lock a lock to avoid races between operations
61 * @io_base base address for gpio ports
62 * @io_size size of the port rage starting from io_base.
63 * @output_base Super I/O register address for Output Enable register
64 * @simple_base Super I/O 'Simple I/O' Enable register
65 * @simple_size Super IO 'Simple I/O' Enable register size; this is
66 * required because IT87xx chips might only provide Simple I/O
67 * switches on a subset of lines, whereas the others keep the
68 * same status all time.
71 struct gpio_chip chip
;
80 static struct it87_gpio it87_gpio_chip
= {
81 .lock
= __SPIN_LOCK_UNLOCKED(it87_gpio_chip
.lock
),
84 /* Superio chip access functions; copied from wdt_it87 */
86 static inline int superio_enter(void)
89 * Try to reserve REG and REG + 1 for exclusive access.
91 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
101 static inline void superio_exit(void)
105 release_region(REG
, 2);
108 static inline void superio_select(int ldn
)
114 static inline int superio_inb(int reg
)
120 static inline void superio_outb(int val
, int reg
)
126 static inline int superio_inw(int reg
)
137 static inline void superio_outw(int val
, int reg
)
145 static inline void superio_set_mask(int mask
, int reg
)
147 u8 curr_val
= superio_inb(reg
);
148 u8 new_val
= curr_val
| mask
;
150 if (curr_val
!= new_val
)
151 superio_outb(new_val
, reg
);
154 static inline void superio_clear_mask(int mask
, int reg
)
156 u8 curr_val
= superio_inb(reg
);
157 u8 new_val
= curr_val
& ~mask
;
159 if (curr_val
!= new_val
)
160 superio_outb(new_val
, reg
);
163 static int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
167 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
169 mask
= 1 << (gpio_num
% 8);
170 group
= (gpio_num
/ 8);
172 spin_lock(&it87_gpio
->lock
);
174 rc
= superio_enter();
178 /* not all the IT87xx chips support Simple I/O and not all of
179 * them allow all the lines to be set/unset to Simple I/O.
181 if (group
< it87_gpio
->simple_size
)
182 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
184 /* clear output enable, setting the pin to input, as all the
185 * newly-exported GPIO interfaces are set to input.
187 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
192 spin_unlock(&it87_gpio
->lock
);
196 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
200 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
202 mask
= 1 << (gpio_num
% 8);
203 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
205 return !!(inb(reg
) & mask
);
208 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
212 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
214 mask
= 1 << (gpio_num
% 8);
215 group
= (gpio_num
/ 8);
217 spin_lock(&it87_gpio
->lock
);
219 rc
= superio_enter();
223 /* clear the output enable bit */
224 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
229 spin_unlock(&it87_gpio
->lock
);
233 static void it87_gpio_set(struct gpio_chip
*chip
,
234 unsigned gpio_num
, int val
)
238 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
240 mask
= 1 << (gpio_num
% 8);
241 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
243 curr_vals
= inb(reg
);
245 outb(curr_vals
| mask
, reg
);
247 outb(curr_vals
& ~mask
, reg
);
250 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
251 unsigned gpio_num
, int val
)
255 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
257 mask
= 1 << (gpio_num
% 8);
258 group
= (gpio_num
/ 8);
260 spin_lock(&it87_gpio
->lock
);
262 rc
= superio_enter();
266 /* set the output enable bit */
267 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
269 it87_gpio_set(chip
, gpio_num
, val
);
274 spin_unlock(&it87_gpio
->lock
);
278 static const struct gpio_chip it87_template_chip
= {
279 .label
= KBUILD_MODNAME
,
280 .owner
= THIS_MODULE
,
281 .request
= it87_gpio_request
,
282 .get
= it87_gpio_get
,
283 .direction_input
= it87_gpio_direction_in
,
284 .set
= it87_gpio_set
,
285 .direction_output
= it87_gpio_direction_out
,
289 static int __init
it87_gpio_init(void)
293 u8 chip_rev
, gpio_ba_reg
;
294 char *labels
, **labels_table
;
296 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
298 rc
= superio_enter();
302 chip_type
= superio_inw(CHIPID
);
303 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
306 it87_gpio
->chip
= it87_template_chip
;
312 it87_gpio
->io_size
= 11;
313 it87_gpio
->output_base
= 0xc8;
314 it87_gpio
->simple_size
= 0;
315 it87_gpio
->chip
.ngpio
= 64;
321 it87_gpio
->io_size
= 8;
322 it87_gpio
->output_base
= 0xc8;
323 it87_gpio
->simple_base
= 0xc0;
324 it87_gpio
->simple_size
= 5;
325 it87_gpio
->chip
.ngpio
= 64;
329 it87_gpio
->io_size
= 4;
330 it87_gpio
->output_base
= 0xf0;
331 it87_gpio
->simple_size
= 0;
332 it87_gpio
->chip
.ngpio
= 16;
335 pr_err("no device\n");
338 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
339 chip_type
, chip_rev
);
343 rc
= superio_enter();
347 superio_select(GPIO
);
349 /* fetch GPIO base address */
350 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
354 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
355 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
358 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
362 /* Set up aliases for the GPIO connection.
364 * ITE documentation for recent chips such as the IT8728F
365 * refers to the GPIO lines as GPxy, with a coordinates system
366 * where x is the GPIO group (starting from 1) and y is the
367 * bit within the group.
369 * By creating these aliases, we make it easier to understand
370 * to which GPIO pin we're referring to.
372 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
374 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
377 if (!labels
|| !labels_table
) {
382 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
383 char *label
= &labels
[i
* sizeof("it87_gpXY")];
385 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
386 labels_table
[i
] = label
;
389 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
391 rc
= gpiochip_add_data(&it87_gpio
->chip
, it87_gpio
);
400 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
404 static void __exit
it87_gpio_exit(void)
406 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
408 gpiochip_remove(&it87_gpio
->chip
);
409 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
410 kfree(it87_gpio
->chip
.names
[0]);
411 kfree(it87_gpio
->chip
.names
);
414 module_init(it87_gpio_init
);
415 module_exit(it87_gpio_exit
);
417 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
418 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
419 MODULE_LICENSE("GPL");