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 static inline struct it87_gpio
*to_it87_gpio(struct gpio_chip
*chip
)
82 return container_of(chip
, struct it87_gpio
, chip
);
85 /* Superio chip access functions; copied from wdt_it87 */
87 static inline int superio_enter(void)
90 * Try to reserve REG and REG + 1 for exclusive access.
92 if (!request_muxed_region(REG
, 2, KBUILD_MODNAME
))
102 static inline void superio_exit(void)
106 release_region(REG
, 2);
109 static inline void superio_select(int ldn
)
115 static inline int superio_inb(int reg
)
121 static inline void superio_outb(int val
, int reg
)
127 static inline int superio_inw(int reg
)
138 static inline void superio_outw(int val
, int reg
)
146 static inline void superio_set_mask(int mask
, int reg
)
148 u8 curr_val
= superio_inb(reg
);
149 u8 new_val
= curr_val
| mask
;
151 if (curr_val
!= new_val
)
152 superio_outb(new_val
, reg
);
155 static inline void superio_clear_mask(int mask
, int reg
)
157 u8 curr_val
= superio_inb(reg
);
158 u8 new_val
= curr_val
& ~mask
;
160 if (curr_val
!= new_val
)
161 superio_outb(new_val
, reg
);
164 static int it87_gpio_request(struct gpio_chip
*chip
, unsigned gpio_num
)
168 struct it87_gpio
*it87_gpio
= to_it87_gpio(chip
);
170 mask
= 1 << (gpio_num
% 8);
171 group
= (gpio_num
/ 8);
173 spin_lock(&it87_gpio
->lock
);
175 rc
= superio_enter();
179 /* not all the IT87xx chips support Simple I/O and not all of
180 * them allow all the lines to be set/unset to Simple I/O.
182 if (group
< it87_gpio
->simple_size
)
183 superio_set_mask(mask
, group
+ it87_gpio
->simple_base
);
185 /* clear output enable, setting the pin to input, as all the
186 * newly-exported GPIO interfaces are set to input.
188 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
193 spin_unlock(&it87_gpio
->lock
);
197 static int it87_gpio_get(struct gpio_chip
*chip
, unsigned gpio_num
)
201 struct it87_gpio
*it87_gpio
= to_it87_gpio(chip
);
203 mask
= 1 << (gpio_num
% 8);
204 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
206 return !!(inb(reg
) & mask
);
209 static int it87_gpio_direction_in(struct gpio_chip
*chip
, unsigned gpio_num
)
213 struct it87_gpio
*it87_gpio
= to_it87_gpio(chip
);
215 mask
= 1 << (gpio_num
% 8);
216 group
= (gpio_num
/ 8);
218 spin_lock(&it87_gpio
->lock
);
220 rc
= superio_enter();
224 /* clear the output enable bit */
225 superio_clear_mask(mask
, group
+ it87_gpio
->output_base
);
230 spin_unlock(&it87_gpio
->lock
);
234 static void it87_gpio_set(struct gpio_chip
*chip
,
235 unsigned gpio_num
, int val
)
239 struct it87_gpio
*it87_gpio
= to_it87_gpio(chip
);
241 mask
= 1 << (gpio_num
% 8);
242 reg
= (gpio_num
/ 8) + it87_gpio
->io_base
;
244 curr_vals
= inb(reg
);
246 outb(curr_vals
| mask
, reg
);
248 outb(curr_vals
& ~mask
, reg
);
251 static int it87_gpio_direction_out(struct gpio_chip
*chip
,
252 unsigned gpio_num
, int val
)
256 struct it87_gpio
*it87_gpio
= to_it87_gpio(chip
);
258 mask
= 1 << (gpio_num
% 8);
259 group
= (gpio_num
/ 8);
261 spin_lock(&it87_gpio
->lock
);
263 rc
= superio_enter();
267 /* set the output enable bit */
268 superio_set_mask(mask
, group
+ it87_gpio
->output_base
);
270 it87_gpio_set(chip
, gpio_num
, val
);
275 spin_unlock(&it87_gpio
->lock
);
279 static struct gpio_chip it87_template_chip
= {
280 .label
= KBUILD_MODNAME
,
281 .owner
= THIS_MODULE
,
282 .request
= it87_gpio_request
,
283 .get
= it87_gpio_get
,
284 .direction_input
= it87_gpio_direction_in
,
285 .set
= it87_gpio_set
,
286 .direction_output
= it87_gpio_direction_out
,
290 static int __init
it87_gpio_init(void)
294 u8 chip_rev
, gpio_ba_reg
;
295 char *labels
, **labels_table
;
297 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
299 rc
= superio_enter();
303 chip_type
= superio_inw(CHIPID
);
304 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
307 it87_gpio
->chip
= it87_template_chip
;
313 it87_gpio
->io_size
= 8;
314 it87_gpio
->output_base
= 0xc8;
315 it87_gpio
->simple_base
= 0xc0;
316 it87_gpio
->simple_size
= 5;
317 it87_gpio
->chip
.ngpio
= 64;
321 it87_gpio
->io_size
= 4;
322 it87_gpio
->output_base
= 0xf0;
323 it87_gpio
->simple_size
= 0;
324 it87_gpio
->chip
.ngpio
= 16;
327 pr_err("no device\n");
330 pr_err("Unknown Chip found, Chip %04x Revision %x\n",
331 chip_type
, chip_rev
);
335 rc
= superio_enter();
339 superio_select(GPIO
);
341 /* fetch GPIO base address */
342 it87_gpio
->io_base
= superio_inw(gpio_ba_reg
);
346 pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
347 chip_type
, chip_rev
, it87_gpio
->chip
.ngpio
,
350 if (!request_region(it87_gpio
->io_base
, it87_gpio
->io_size
,
354 /* Set up aliases for the GPIO connection.
356 * ITE documentation for recent chips such as the IT8728F
357 * refers to the GPIO lines as GPxy, with a coordinates system
358 * where x is the GPIO group (starting from 1) and y is the
359 * bit within the group.
361 * By creating these aliases, we make it easier to understand
362 * to which GPIO pin we're referring to.
364 labels
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof("it87_gpXY"),
366 labels_table
= kcalloc(it87_gpio
->chip
.ngpio
, sizeof(const char *),
369 if (!labels
|| !labels_table
) {
374 for (i
= 0; i
< it87_gpio
->chip
.ngpio
; i
++) {
375 char *label
= &labels
[i
* sizeof("it87_gpXY")];
377 sprintf(label
, "it87_gp%u%u", 1+(i
/8), i
%8);
378 labels_table
[i
] = label
;
381 it87_gpio
->chip
.names
= (const char *const*)labels_table
;
383 rc
= gpiochip_add(&it87_gpio
->chip
);
392 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
396 static void __exit
it87_gpio_exit(void)
398 struct it87_gpio
*it87_gpio
= &it87_gpio_chip
;
400 gpiochip_remove(&it87_gpio
->chip
);
401 release_region(it87_gpio
->io_base
, it87_gpio
->io_size
);
402 kfree(it87_gpio
->chip
.names
[0]);
403 kfree(it87_gpio
->chip
.names
);
406 module_init(it87_gpio_init
);
407 module_exit(it87_gpio_exit
);
409 MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
410 MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
411 MODULE_LICENSE("GPL");