1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO interface for IT87xx Super I/O chips
5 * Author: Diego Elio Pettenò <flameeyes@flameeyes.eu>
6 * Copyright (c) 2017 Google, Inc.
8 * Based on it87_wdt.c by Oliver Schuster
9 * gpio-it8761e.c by Denis Turischev
10 * gpio-stmpe.c by Rabin Vincent
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/ioport.h>
21 #include <linux/slab.h>
22 #include <linux/gpio/driver.h>
25 #define NO_DEV_ID 0xffff
26 #define IT8613_ID 0x8613
27 #define IT8620_ID 0x8620
28 #define IT8628_ID 0x8628
29 #define IT8718_ID 0x8718
30 #define IT8728_ID 0x8728
31 #define IT8732_ID 0x8732
32 #define IT8761_ID 0x8761
33 #define IT8772_ID 0x8772
34 #define IT8786_ID 0x8786
40 /* Logical device Numbers LDN */
43 /* Configuration Registers and Functions */
49 * struct it87_gpio - it87-specific GPIO chip
50 * @chip: the underlying gpio_chip structure
51 * @lock: a lock to avoid races between operations
52 * @io_base: base address for gpio ports
53 * @io_size: size of the port rage starting from io_base.
54 * @output_base: Super I/O register address for Output Enable register
55 * @simple_base: Super I/O 'Simple I/O' Enable register
56 * @simple_size: Super IO 'Simple I/O' Enable register size; this is
57 * required because IT87xx chips might only provide Simple I/O
58 * switches on a subset of lines, whereas the others keep the
59 * same status all time.
62 struct gpio_chip chip
;
71 static struct it87_gpio it87_gpio_chip
= {
72 .lock
= __SPIN_LOCK_UNLOCKED(it87_gpio_chip
.lock
),
75 /* Superio chip access functions; copied from wdt_it87 */
77 static inline int superio_enter(void)
80 * Try to reserve REG and REG + 1 for exclusive access.
82 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
92 static inline void superio_exit(void)
96 release_region(REG
, 2);
99 static inline void superio_select(int ldn
)
105 static inline int superio_inb(int reg
)
111 static inline void superio_outb(int val
, int reg
)
117 static inline int superio_inw(int reg
)
128 static inline void superio_outw(int val
, int reg
)
136 static inline void superio_set_mask(int mask
, int reg
)
138 u8 curr_val
= superio_inb(reg
);
139 u8 new_val
= curr_val
| mask
;
141 if (curr_val
!= new_val
)
142 superio_outb(new_val
, reg
);
145 static inline void superio_clear_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 int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
158 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
160 mask
= 1 << (gpio_num
% 8);
161 group
= (gpio_num
/ 8);
163 spin_lock(&it87_gpio
->lock
);
165 rc
= superio_enter();
169 /* not all the IT87xx chips support Simple I/O and not all of
170 * them allow all the lines to be set/unset to Simple I/O.
172 if (group
< it87_gpio
->simple_size
)
173 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
175 /* clear output enable, setting the pin to input, as all the
176 * newly-exported GPIO interfaces are set to input.
178 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
183 spin_unlock(&it87_gpio
->lock
);
187 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
191 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
193 mask
= 1 << (gpio_num
% 8);
194 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
196 return !!(inb(reg
) & mask
);
199 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
203 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
205 mask
= 1 << (gpio_num
% 8);
206 group
= (gpio_num
/ 8);
208 spin_lock(&it87_gpio
->lock
);
210 rc
= superio_enter();
214 /* clear the output enable bit */
215 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
220 spin_unlock(&it87_gpio
->lock
);
224 static void it87_gpio_set(struct gpio_chip
*chip
,
225 unsigned gpio_num
, int val
)
229 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
231 mask
= 1 << (gpio_num
% 8);
232 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
234 curr_vals
= inb(reg
);
236 outb(curr_vals
| mask
, reg
);
238 outb(curr_vals
& ~mask
, reg
);
241 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
242 unsigned gpio_num
, int val
)
246 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
248 mask
= 1 << (gpio_num
% 8);
249 group
= (gpio_num
/ 8);
251 spin_lock(&it87_gpio
->lock
);
253 rc
= superio_enter();
257 /* set the output enable bit */
258 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
260 it87_gpio_set(chip
, gpio_num
, val
);
265 spin_unlock(&it87_gpio
->lock
);
269 static const struct gpio_chip it87_template_chip
= {
270 .label
= KBUILD_MODNAME
,
271 .owner
= THIS_MODULE
,
272 .request
= it87_gpio_request
,
273 .get
= it87_gpio_get
,
274 .direction_input
= it87_gpio_direction_in
,
275 .set
= it87_gpio_set
,
276 .direction_output
= it87_gpio_direction_out
,
280 static int __init
it87_gpio_init(void)
284 u8 chip_rev
, gpio_ba_reg
;
285 char *labels
, **labels_table
;
287 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
289 rc
= superio_enter();
293 chip_type
= superio_inw(CHIPID
);
294 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
297 it87_gpio
->chip
= it87_template_chip
;
302 it87_gpio
->io_size
= 8; /* it8613 only needs 6, use 8 for alignment */
303 it87_gpio
->output_base
= 0xc8;
304 it87_gpio
->simple_base
= 0xc0;
305 it87_gpio
->simple_size
= 6;
306 it87_gpio
->chip
.ngpio
= 64; /* has 48, use 64 for convenient calc */
311 it87_gpio
->io_size
= 11;
312 it87_gpio
->output_base
= 0xc8;
313 it87_gpio
->simple_size
= 0;
314 it87_gpio
->chip
.ngpio
= 64;
322 it87_gpio
->io_size
= 8;
323 it87_gpio
->output_base
= 0xc8;
324 it87_gpio
->simple_base
= 0xc0;
325 it87_gpio
->simple_size
= 5;
326 it87_gpio
->chip
.ngpio
= 64;
330 it87_gpio
->io_size
= 4;
331 it87_gpio
->output_base
= 0xf0;
332 it87_gpio
->simple_size
= 0;
333 it87_gpio
->chip
.ngpio
= 16;
336 pr_err("no device\n");
339 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
340 chip_type
, chip_rev
);
344 rc
= superio_enter();
348 superio_select(GPIO
);
350 /* fetch GPIO base address */
351 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
355 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
356 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
359 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
363 /* Set up aliases for the GPIO connection.
365 * ITE documentation for recent chips such as the IT8728F
366 * refers to the GPIO lines as GPxy, with a coordinates system
367 * where x is the GPIO group (starting from 1) and y is the
368 * bit within the group.
370 * By creating these aliases, we make it easier to understand
371 * to which GPIO pin we're referring to.
373 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
375 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
378 if (!labels
|| !labels_table
) {
383 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
384 char *label
= &labels
[i
* sizeof("it87_gpXY")];
386 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
387 labels_table
[i
] = label
;
390 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
392 rc
= gpiochip_add_data(&it87_gpio
->chip
, it87_gpio
);
401 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
405 static void __exit
it87_gpio_exit(void)
407 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
409 gpiochip_remove(&it87_gpio
->chip
);
410 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
411 kfree(it87_gpio
->chip
.names
[0]);
412 kfree(it87_gpio
->chip
.names
);
415 module_init(it87_gpio_init
);
416 module_exit(it87_gpio_exit
);
418 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
419 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
420 MODULE_LICENSE("GPL");