123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/ioport.h>
- #include <linux/module.h>
- #include <linux/pci.h>
- #include <linux/gpio.h>
- #include <linux/platform_device.h>
- #include <linux/mfd/lpc_ich.h>
- #define DRV_NAME "gpio_ich"
- enum GPIO_REG {
- GPIO_USE_SEL = 0,
- GPIO_IO_SEL,
- GPIO_LVL,
- GPO_BLINK
- };
- static const u8 ichx_regs[4][3] = {
- {0x00, 0x30, 0x40},
- {0x04, 0x34, 0x44},
- {0x0c, 0x38, 0x48},
- {0x18, 0x18, 0x18},
- };
- static const u8 ichx_reglen[3] = {
- 0x30, 0x10, 0x10,
- };
- static const u8 avoton_regs[4][3] = {
- {0x00, 0x80, 0x00},
- {0x04, 0x84, 0x00},
- {0x08, 0x88, 0x00},
- };
- static const u8 avoton_reglen[3] = {
- 0x10, 0x10, 0x00,
- };
- #define ICHX_WRITE(val, reg, base_res) outl(val, (reg) + (base_res)->start)
- #define ICHX_READ(reg, base_res) inl((reg) + (base_res)->start)
- struct ichx_desc {
-
- uint ngpio;
-
- const u8 (*regs)[3];
- const u8 *reglen;
-
- bool have_blink;
-
- bool uses_gpe0;
-
- u32 use_sel_ignore[3];
-
- int (*request)(struct gpio_chip *chip, unsigned offset);
- int (*get)(struct gpio_chip *chip, unsigned offset);
-
- bool use_outlvl_cache;
- };
- static struct {
- spinlock_t lock;
- struct platform_device *dev;
- struct gpio_chip chip;
- struct resource *gpio_base;
- struct resource *pm_base;
- struct ichx_desc *desc;
- u32 orig_gpio_ctrl;
- u8 use_gpio;
- int outlvl_cache[3];
- } ichx_priv;
- static int modparam_gpiobase = -1;
- module_param_named(gpiobase, modparam_gpiobase, int, 0444);
- MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, "
- "which is the default.");
- static int ichx_write_bit(int reg, unsigned nr, int val, int verify)
- {
- unsigned long flags;
- u32 data, tmp;
- int reg_nr = nr / 32;
- int bit = nr & 0x1f;
- int ret = 0;
- spin_lock_irqsave(&ichx_priv.lock, flags);
- if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
- data = ichx_priv.outlvl_cache[reg_nr];
- else
- data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
- ichx_priv.gpio_base);
- if (val)
- data |= 1 << bit;
- else
- data &= ~(1 << bit);
- ICHX_WRITE(data, ichx_priv.desc->regs[reg][reg_nr],
- ichx_priv.gpio_base);
- if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
- ichx_priv.outlvl_cache[reg_nr] = data;
- tmp = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
- ichx_priv.gpio_base);
- if (verify && data != tmp)
- ret = -EPERM;
- spin_unlock_irqrestore(&ichx_priv.lock, flags);
- return ret;
- }
- static int ichx_read_bit(int reg, unsigned nr)
- {
- unsigned long flags;
- u32 data;
- int reg_nr = nr / 32;
- int bit = nr & 0x1f;
- spin_lock_irqsave(&ichx_priv.lock, flags);
- data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
- ichx_priv.gpio_base);
- if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
- data = ichx_priv.outlvl_cache[reg_nr] | data;
- spin_unlock_irqrestore(&ichx_priv.lock, flags);
- return data & (1 << bit) ? 1 : 0;
- }
- static bool ichx_gpio_check_available(struct gpio_chip *gpio, unsigned nr)
- {
- return !!(ichx_priv.use_gpio & (1 << (nr / 32)));
- }
- static int ichx_gpio_get_direction(struct gpio_chip *gpio, unsigned nr)
- {
- return ichx_read_bit(GPIO_IO_SEL, nr) ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
- }
- static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
- {
-
- if (ichx_write_bit(GPIO_IO_SEL, nr, 1, 1))
- return -EINVAL;
- return 0;
- }
- static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
- int val)
- {
-
- if (nr < 32 && ichx_priv.desc->have_blink)
- ichx_write_bit(GPO_BLINK, nr, 0, 0);
-
- ichx_write_bit(GPIO_LVL, nr, val, 0);
-
- if (ichx_write_bit(GPIO_IO_SEL, nr, 0, 1))
- return -EINVAL;
- return 0;
- }
- static int ichx_gpio_get(struct gpio_chip *chip, unsigned nr)
- {
- return ichx_read_bit(GPIO_LVL, nr);
- }
- static int ich6_gpio_get(struct gpio_chip *chip, unsigned nr)
- {
- unsigned long flags;
- u32 data;
-
- if (nr < 16) {
- if (!ichx_priv.pm_base)
- return -ENXIO;
- spin_lock_irqsave(&ichx_priv.lock, flags);
-
- ICHX_WRITE(1 << (16 + nr), 0, ichx_priv.pm_base);
- data = ICHX_READ(0, ichx_priv.pm_base);
- spin_unlock_irqrestore(&ichx_priv.lock, flags);
- return (data >> 16) & (1 << nr) ? 1 : 0;
- } else {
- return ichx_gpio_get(chip, nr);
- }
- }
- static int ichx_gpio_request(struct gpio_chip *chip, unsigned nr)
- {
- if (!ichx_gpio_check_available(chip, nr))
- return -ENXIO;
-
- if (ichx_priv.desc->use_sel_ignore[nr / 32] & (1 << (nr & 0x1f)))
- return 0;
- return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
- }
- static int ich6_gpio_request(struct gpio_chip *chip, unsigned nr)
- {
-
- if (nr == 16 || nr == 17)
- nr -= 16;
- return ichx_gpio_request(chip, nr);
- }
- static void ichx_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
- {
- ichx_write_bit(GPIO_LVL, nr, val, 0);
- }
- static void ichx_gpiolib_setup(struct gpio_chip *chip)
- {
- chip->owner = THIS_MODULE;
- chip->label = DRV_NAME;
- chip->parent = &ichx_priv.dev->dev;
-
- chip->request = ichx_priv.desc->request ?
- ichx_priv.desc->request : ichx_gpio_request;
- chip->get = ichx_priv.desc->get ?
- ichx_priv.desc->get : ichx_gpio_get;
- chip->set = ichx_gpio_set;
- chip->get_direction = ichx_gpio_get_direction;
- chip->direction_input = ichx_gpio_direction_input;
- chip->direction_output = ichx_gpio_direction_output;
- chip->base = modparam_gpiobase;
- chip->ngpio = ichx_priv.desc->ngpio;
- chip->can_sleep = false;
- chip->dbg_show = NULL;
- }
- static struct ichx_desc ich6_desc = {
-
- .request = ich6_gpio_request,
- .get = ich6_gpio_get,
-
- .uses_gpe0 = true,
- .ngpio = 50,
- .have_blink = true,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc i3100_desc = {
-
- .use_sel_ignore = {0x00130000, 0x00010000, 0x0},
-
- .request = ich6_gpio_request,
- .get = ich6_gpio_get,
-
- .uses_gpe0 = true,
- .ngpio = 50,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc ich7_desc = {
- .ngpio = 50,
- .have_blink = true,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc ich9_desc = {
- .ngpio = 61,
- .have_blink = true,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc ich10_cons_desc = {
- .ngpio = 61,
- .have_blink = true,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc ich10_corp_desc = {
- .ngpio = 72,
- .have_blink = true,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc intel5_desc = {
- .ngpio = 76,
- .regs = ichx_regs,
- .reglen = ichx_reglen,
- };
- static struct ichx_desc avoton_desc = {
-
- .ngpio = 60,
- .regs = avoton_regs,
- .reglen = avoton_reglen,
- .use_outlvl_cache = true,
- };
- static int ichx_gpio_request_regions(struct device *dev,
- struct resource *res_base, const char *name, u8 use_gpio)
- {
- int i;
- if (!res_base || !res_base->start || !res_base->end)
- return -ENODEV;
- for (i = 0; i < ARRAY_SIZE(ichx_priv.desc->regs[0]); i++) {
- if (!(use_gpio & (1 << i)))
- continue;
- if (!devm_request_region(dev,
- res_base->start + ichx_priv.desc->regs[0][i],
- ichx_priv.desc->reglen[i], name))
- return -EBUSY;
- }
- return 0;
- }
- static int ichx_gpio_probe(struct platform_device *pdev)
- {
- struct resource *res_base, *res_pm;
- int err;
- struct lpc_ich_info *ich_info = dev_get_platdata(&pdev->dev);
- if (!ich_info)
- return -ENODEV;
- ichx_priv.dev = pdev;
- switch (ich_info->gpio_version) {
- case ICH_I3100_GPIO:
- ichx_priv.desc = &i3100_desc;
- break;
- case ICH_V5_GPIO:
- ichx_priv.desc = &intel5_desc;
- break;
- case ICH_V6_GPIO:
- ichx_priv.desc = &ich6_desc;
- break;
- case ICH_V7_GPIO:
- ichx_priv.desc = &ich7_desc;
- break;
- case ICH_V9_GPIO:
- ichx_priv.desc = &ich9_desc;
- break;
- case ICH_V10CORP_GPIO:
- ichx_priv.desc = &ich10_corp_desc;
- break;
- case ICH_V10CONS_GPIO:
- ichx_priv.desc = &ich10_cons_desc;
- break;
- case AVOTON_GPIO:
- ichx_priv.desc = &avoton_desc;
- break;
- default:
- return -ENODEV;
- }
- spin_lock_init(&ichx_priv.lock);
- res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
- ichx_priv.use_gpio = ich_info->use_gpio;
- err = ichx_gpio_request_regions(&pdev->dev, res_base, pdev->name,
- ichx_priv.use_gpio);
- if (err)
- return err;
- ichx_priv.gpio_base = res_base;
-
- if (!ichx_priv.desc->uses_gpe0)
- goto init;
- res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
- if (!res_pm) {
- pr_warn("ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
- goto init;
- }
- if (!devm_request_region(&pdev->dev, res_pm->start,
- resource_size(res_pm), pdev->name)) {
- pr_warn("ACPI BAR is busy, GPI 0 - 15 unavailable\n");
- goto init;
- }
- ichx_priv.pm_base = res_pm;
- init:
- ichx_gpiolib_setup(&ichx_priv.chip);
- err = gpiochip_add_data(&ichx_priv.chip, NULL);
- if (err) {
- pr_err("Failed to register GPIOs\n");
- return err;
- }
- pr_info("GPIO from %d to %d on %s\n", ichx_priv.chip.base,
- ichx_priv.chip.base + ichx_priv.chip.ngpio - 1, DRV_NAME);
- return 0;
- }
- static int ichx_gpio_remove(struct platform_device *pdev)
- {
- gpiochip_remove(&ichx_priv.chip);
- return 0;
- }
- static struct platform_driver ichx_gpio_driver = {
- .driver = {
- .name = DRV_NAME,
- },
- .probe = ichx_gpio_probe,
- .remove = ichx_gpio_remove,
- };
- module_platform_driver(ichx_gpio_driver);
- MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
- MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("platform:"DRV_NAME);
|