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 IT8620_ID 0x8620
38 #define IT8628_ID 0x8628
39 #define IT8728_ID 0x8728
40 #define IT8732_ID 0x8732
41 #define IT8761_ID 0x8761
47 /* Logical device Numbers LDN */
50 /* Configuration Registers and Functions */
56 * struct it87_gpio - it87-specific GPIO chip
57 * @chip the underlying gpio_chip structure
58 * @lock a lock to avoid races between operations
59 * @io_base base address for gpio ports
60 * @io_size size of the port rage starting from io_base.
61 * @output_base Super I/O register address for Output Enable register
62 * @simple_base Super I/O 'Simple I/O' Enable register
63 * @simple_size Super IO 'Simple I/O' Enable register size; this is
64 * required because IT87xx chips might only provide Simple I/O
65 * switches on a subset of lines, whereas the others keep the
66 * same status all time.
69 struct gpio_chip chip
;
78 static struct it87_gpio it87_gpio_chip
= {
79 .lock
= __SPIN_LOCK_UNLOCKED(it87_gpio_chip
.lock
),
82 /* Superio chip access functions; copied from wdt_it87 */
84 static inline int superio_enter(void)
87 * Try to reserve REG and REG + 1 for exclusive access.
89 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
99 static inline void superio_exit(void)
103 release_region(REG
, 2);
106 static inline void superio_select(int ldn
)
112 static inline int superio_inb(int reg
)
118 static inline void superio_outb(int val
, int reg
)
124 static inline int superio_inw(int reg
)
135 static inline void superio_outw(int val
, int reg
)
143 static inline void superio_set_mask(int mask
, int reg
)
145 u8 curr_val
= superio_inb(reg
);
146 u8 new_val
= curr_val
| mask
;
148 if (curr_val
!= new_val
)
149 superio_outb(new_val
, reg
);
152 static inline void superio_clear_mask(int mask
, int reg
)
154 u8 curr_val
= superio_inb(reg
);
155 u8 new_val
= curr_val
& ~mask
;
157 if (curr_val
!= new_val
)
158 superio_outb(new_val
, reg
);
161 static int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
165 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
167 mask
= 1 << (gpio_num
% 8);
168 group
= (gpio_num
/ 8);
170 spin_lock(&it87_gpio
->lock
);
172 rc
= superio_enter();
176 /* not all the IT87xx chips support Simple I/O and not all of
177 * them allow all the lines to be set/unset to Simple I/O.
179 if (group
< it87_gpio
->simple_size
)
180 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
182 /* clear output enable, setting the pin to input, as all the
183 * newly-exported GPIO interfaces are set to input.
185 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
190 spin_unlock(&it87_gpio
->lock
);
194 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
198 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
200 mask
= 1 << (gpio_num
% 8);
201 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
203 return !!(inb(reg
) & mask
);
206 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
210 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
212 mask
= 1 << (gpio_num
% 8);
213 group
= (gpio_num
/ 8);
215 spin_lock(&it87_gpio
->lock
);
217 rc
= superio_enter();
221 /* clear the output enable bit */
222 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
227 spin_unlock(&it87_gpio
->lock
);
231 static void it87_gpio_set(struct gpio_chip
*chip
,
232 unsigned gpio_num
, int val
)
236 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
238 mask
= 1 << (gpio_num
% 8);
239 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
241 curr_vals
= inb(reg
);
243 outb(curr_vals
| mask
, reg
);
245 outb(curr_vals
& ~mask
, reg
);
248 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
249 unsigned gpio_num
, int val
)
253 struct it87_gpio
*it87_gpio
= gpiochip_get_data(chip
);
255 mask
= 1 << (gpio_num
% 8);
256 group
= (gpio_num
/ 8);
258 spin_lock(&it87_gpio
->lock
);
260 rc
= superio_enter();
264 /* set the output enable bit */
265 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
267 it87_gpio_set(chip
, gpio_num
, val
);
272 spin_unlock(&it87_gpio
->lock
);
276 static const struct gpio_chip it87_template_chip
= {
277 .label
= KBUILD_MODNAME
,
278 .owner
= THIS_MODULE
,
279 .request
= it87_gpio_request
,
280 .get
= it87_gpio_get
,
281 .direction_input
= it87_gpio_direction_in
,
282 .set
= it87_gpio_set
,
283 .direction_output
= it87_gpio_direction_out
,
287 static int __init
it87_gpio_init(void)
291 u8 chip_rev
, gpio_ba_reg
;
292 char *labels
, **labels_table
;
294 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
296 rc
= superio_enter();
300 chip_type
= superio_inw(CHIPID
);
301 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
304 it87_gpio
->chip
= it87_template_chip
;
310 it87_gpio
->io_size
= 11;
311 it87_gpio
->output_base
= 0xc8;
312 it87_gpio
->simple_size
= 0;
313 it87_gpio
->chip
.ngpio
= 64;
318 it87_gpio
->io_size
= 8;
319 it87_gpio
->output_base
= 0xc8;
320 it87_gpio
->simple_base
= 0xc0;
321 it87_gpio
->simple_size
= 5;
322 it87_gpio
->chip
.ngpio
= 64;
326 it87_gpio
->io_size
= 4;
327 it87_gpio
->output_base
= 0xf0;
328 it87_gpio
->simple_size
= 0;
329 it87_gpio
->chip
.ngpio
= 16;
332 pr_err("no device\n");
335 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
336 chip_type
, chip_rev
);
340 rc
= superio_enter();
344 superio_select(GPIO
);
346 /* fetch GPIO base address */
347 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
351 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
352 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
355 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
359 /* Set up aliases for the GPIO connection.
361 * ITE documentation for recent chips such as the IT8728F
362 * refers to the GPIO lines as GPxy, with a coordinates system
363 * where x is the GPIO group (starting from 1) and y is the
364 * bit within the group.
366 * By creating these aliases, we make it easier to understand
367 * to which GPIO pin we're referring to.
369 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
371 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
374 if (!labels
|| !labels_table
) {
379 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
380 char *label
= &labels
[i
* sizeof("it87_gpXY")];
382 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
383 labels_table
[i
] = label
;
386 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
388 rc
= gpiochip_add_data(&it87_gpio
->chip
, it87_gpio
);
397 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
401 static void __exit
it87_gpio_exit(void)
403 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
405 gpiochip_remove(&it87_gpio
->chip
);
406 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
407 kfree(it87_gpio
->chip
.names
[0]);
408 kfree(it87_gpio
->chip
.names
);
411 module_init(it87_gpio_init
);
412 module_exit(it87_gpio_exit
);
414 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
415 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
416 MODULE_LICENSE("GPL");