Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 1 | /* Moorestown PMIC GPIO (access through IPC) driver |
| 2 | * Copyright (c) 2008 - 2009, Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 16 | */ |
| 17 | |
| 18 | /* Supports: |
| 19 | * Moorestown platform PMIC chip |
| 20 | */ |
| 21 | |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 22 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 23 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/stddef.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/ioport.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/io.h> |
Linus Walleij | 3769a89 | 2015-12-08 22:54:20 +0100 | [diff] [blame] | 33 | #include <linux/gpio/driver.h> |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 34 | #include <asm/intel_scu_ipc.h> |
| 35 | #include <linux/device.h> |
| 36 | #include <linux/intel_pmic_gpio.h> |
| 37 | #include <linux/platform_device.h> |
| 38 | |
| 39 | #define DRIVER_NAME "pmic_gpio" |
| 40 | |
| 41 | /* register offset that IPC driver should use |
| 42 | * 8 GPIO + 8 GPOSW (6 controllable) + 8GPO |
| 43 | */ |
| 44 | enum pmic_gpio_register { |
| 45 | GPIO0 = 0xE0, |
| 46 | GPIO7 = 0xE7, |
| 47 | GPIOINT = 0xE8, |
| 48 | GPOSWCTL0 = 0xEC, |
| 49 | GPOSWCTL5 = 0xF1, |
| 50 | GPO = 0xF4, |
| 51 | }; |
| 52 | |
| 53 | /* bits definition for GPIO & GPOSW */ |
| 54 | #define GPIO_DRV 0x01 |
| 55 | #define GPIO_DIR 0x02 |
| 56 | #define GPIO_DIN 0x04 |
| 57 | #define GPIO_DOU 0x08 |
| 58 | #define GPIO_INTCTL 0x30 |
| 59 | #define GPIO_DBC 0xc0 |
| 60 | |
| 61 | #define GPOSW_DRV 0x01 |
| 62 | #define GPOSW_DOU 0x08 |
| 63 | #define GPOSW_RDRV 0x30 |
| 64 | |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 65 | #define GPIO_UPDATE_TYPE 0x80000000 |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 66 | |
| 67 | #define NUM_GPIO 24 |
| 68 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 69 | struct pmic_gpio { |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 70 | struct mutex buslock; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 71 | struct gpio_chip chip; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 72 | void *gpiointr; |
| 73 | int irq; |
| 74 | unsigned irq_base; |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 75 | unsigned int update_type; |
| 76 | u32 trigger_type; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Thomas Gleixner | 65d7ac03 | 2011-04-02 21:23:36 +0200 | [diff] [blame] | 79 | static void pmic_program_irqtype(int gpio, int type) |
| 80 | { |
| 81 | if (type & IRQ_TYPE_EDGE_RISING) |
| 82 | intel_scu_ipc_update_register(GPIO0 + gpio, 0x20, 0x20); |
| 83 | else |
| 84 | intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x20); |
| 85 | |
| 86 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 87 | intel_scu_ipc_update_register(GPIO0 + gpio, 0x10, 0x10); |
| 88 | else |
| 89 | intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10); |
| 90 | }; |
| 91 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 92 | static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 93 | { |
Axel Lin | 21a35427 | 2014-04-15 10:54:11 +0800 | [diff] [blame] | 94 | if (offset >= 8) { |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 95 | pr_err("only pin 0-7 support input\n"); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 96 | return -1;/* we only have 8 GPIO can use as input */ |
| 97 | } |
| 98 | return intel_scu_ipc_update_register(GPIO0 + offset, |
| 99 | GPIO_DIR, GPIO_DIR); |
| 100 | } |
| 101 | |
| 102 | static int pmic_gpio_direction_output(struct gpio_chip *chip, |
| 103 | unsigned offset, int value) |
| 104 | { |
| 105 | int rc = 0; |
| 106 | |
| 107 | if (offset < 8)/* it is GPIO */ |
| 108 | rc = intel_scu_ipc_update_register(GPIO0 + offset, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 109 | GPIO_DRV | (value ? GPIO_DOU : 0), |
| 110 | GPIO_DRV | GPIO_DOU | GPIO_DIR); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 111 | else if (offset < 16)/* it is GPOSW */ |
| 112 | rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 113 | GPOSW_DRV | (value ? GPOSW_DOU : 0), |
| 114 | GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 115 | else if (offset > 15 && offset < 24)/* it is GPO */ |
| 116 | rc = intel_scu_ipc_update_register(GPO, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 117 | value ? 1 << (offset - 16) : 0, |
| 118 | 1 << (offset - 16)); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 119 | else { |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 120 | pr_err("invalid PMIC GPIO pin %d!\n", offset); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 121 | WARN_ON(1); |
| 122 | } |
| 123 | |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 128 | { |
| 129 | u8 r; |
| 130 | int ret; |
| 131 | |
| 132 | /* we only have 8 GPIO pins we can use as input */ |
Axel Lin | 21a35427 | 2014-04-15 10:54:11 +0800 | [diff] [blame] | 133 | if (offset >= 8) |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 134 | return -EOPNOTSUPP; |
| 135 | ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | return r & GPIO_DIN; |
| 139 | } |
| 140 | |
| 141 | static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 142 | { |
| 143 | if (offset < 8)/* it is GPIO */ |
| 144 | intel_scu_ipc_update_register(GPIO0 + offset, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 145 | GPIO_DRV | (value ? GPIO_DOU : 0), |
| 146 | GPIO_DRV | GPIO_DOU); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 147 | else if (offset < 16)/* it is GPOSW */ |
| 148 | intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 149 | GPOSW_DRV | (value ? GPOSW_DOU : 0), |
| 150 | GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 151 | else if (offset > 15 && offset < 24) /* it is GPO */ |
| 152 | intel_scu_ipc_update_register(GPO, |
Alek Du | ffcfff3 | 2010-10-04 16:40:35 +0100 | [diff] [blame] | 153 | value ? 1 << (offset - 16) : 0, |
| 154 | 1 << (offset - 16)); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 157 | /* |
| 158 | * This is called from genirq with pg->buslock locked and |
| 159 | * irq_desc->lock held. We can not access the scu bus here, so we |
| 160 | * store the change and update in the bus_sync_unlock() function below |
| 161 | */ |
Thomas Gleixner | cb8e5e6 | 2011-02-05 10:46:30 +0000 | [diff] [blame] | 162 | static int pmic_irq_type(struct irq_data *data, unsigned type) |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 163 | { |
Thomas Gleixner | cb8e5e6 | 2011-02-05 10:46:30 +0000 | [diff] [blame] | 164 | struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); |
| 165 | u32 gpio = data->irq - pg->irq_base; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 166 | |
Axel Lin | 4119617 | 2010-10-08 17:54:31 +0800 | [diff] [blame] | 167 | if (gpio >= pg->chip.ngpio) |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 168 | return -EINVAL; |
| 169 | |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 170 | pg->trigger_type = type; |
| 171 | pg->update_type = gpio | GPIO_UPDATE_TYPE; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 175 | static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 176 | { |
Linus Walleij | 3769a89 | 2015-12-08 22:54:20 +0100 | [diff] [blame] | 177 | struct pmic_gpio *pg = gpiochip_get_data(chip); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 178 | |
| 179 | return pg->irq_base + offset; |
| 180 | } |
| 181 | |
Thomas Gleixner | 65d7ac03 | 2011-04-02 21:23:36 +0200 | [diff] [blame] | 182 | static void pmic_bus_lock(struct irq_data *data) |
| 183 | { |
| 184 | struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); |
| 185 | |
| 186 | mutex_lock(&pg->buslock); |
| 187 | } |
| 188 | |
| 189 | static void pmic_bus_sync_unlock(struct irq_data *data) |
| 190 | { |
| 191 | struct pmic_gpio *pg = irq_data_get_irq_chip_data(data); |
| 192 | |
| 193 | if (pg->update_type) { |
| 194 | unsigned int gpio = pg->update_type & ~GPIO_UPDATE_TYPE; |
| 195 | |
| 196 | pmic_program_irqtype(gpio, pg->trigger_type); |
| 197 | pg->update_type = 0; |
| 198 | } |
| 199 | mutex_unlock(&pg->buslock); |
| 200 | } |
| 201 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 202 | /* the gpiointr register is read-clear, so just do nothing. */ |
Thomas Gleixner | cb8e5e6 | 2011-02-05 10:46:30 +0000 | [diff] [blame] | 203 | static void pmic_irq_unmask(struct irq_data *data) { } |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 204 | |
Thomas Gleixner | cb8e5e6 | 2011-02-05 10:46:30 +0000 | [diff] [blame] | 205 | static void pmic_irq_mask(struct irq_data *data) { } |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 206 | |
| 207 | static struct irq_chip pmic_irqchip = { |
Thomas Gleixner | 65d7ac03 | 2011-04-02 21:23:36 +0200 | [diff] [blame] | 208 | .name = "PMIC-GPIO", |
| 209 | .irq_mask = pmic_irq_mask, |
| 210 | .irq_unmask = pmic_irq_unmask, |
| 211 | .irq_set_type = pmic_irq_type, |
Matthew Garrett | 21a8d02 | 2011-04-13 11:52:16 -0400 | [diff] [blame] | 212 | .irq_bus_lock = pmic_bus_lock, |
Thomas Gleixner | 65d7ac03 | 2011-04-02 21:23:36 +0200 | [diff] [blame] | 213 | .irq_bus_sync_unlock = pmic_bus_sync_unlock, |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 214 | }; |
| 215 | |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 216 | static irqreturn_t pmic_irq_handler(int irq, void *data) |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 217 | { |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 218 | struct pmic_gpio *pg = data; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 219 | u8 intsts = *((u8 *)pg->gpiointr + 4); |
| 220 | int gpio; |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 221 | irqreturn_t ret = IRQ_NONE; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 222 | |
| 223 | for (gpio = 0; gpio < 8; gpio++) { |
| 224 | if (intsts & (1 << gpio)) { |
| 225 | pr_debug("pmic pin %d triggered\n", gpio); |
| 226 | generic_handle_irq(pg->irq_base + gpio); |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 227 | ret = IRQ_HANDLED; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 228 | } |
| 229 | } |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 230 | return ret; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Greg Kroah-Hartman | b859f15 | 2012-12-21 13:18:33 -0800 | [diff] [blame] | 233 | static int platform_pmic_gpio_probe(struct platform_device *pdev) |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 234 | { |
| 235 | struct device *dev = &pdev->dev; |
| 236 | int irq = platform_get_irq(pdev, 0); |
| 237 | struct intel_pmic_gpio_platform_data *pdata = dev->platform_data; |
| 238 | |
| 239 | struct pmic_gpio *pg; |
| 240 | int retval; |
| 241 | int i; |
| 242 | |
| 243 | if (irq < 0) { |
| 244 | dev_dbg(dev, "no IRQ line\n"); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | if (!pdata || !pdata->gpio_base || !pdata->irq_base) { |
| 249 | dev_dbg(dev, "incorrect or missing platform data\n"); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 254 | if (!pg) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | dev_set_drvdata(dev, pg); |
| 258 | |
| 259 | pg->irq = irq; |
| 260 | /* setting up SRAM mapping for GPIOINT register */ |
| 261 | pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8); |
| 262 | if (!pg->gpiointr) { |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 263 | pr_err("Can not map GPIOINT\n"); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 264 | retval = -EINVAL; |
| 265 | goto err2; |
| 266 | } |
| 267 | pg->irq_base = pdata->irq_base; |
| 268 | pg->chip.label = "intel_pmic"; |
| 269 | pg->chip.direction_input = pmic_gpio_direction_input; |
| 270 | pg->chip.direction_output = pmic_gpio_direction_output; |
| 271 | pg->chip.get = pmic_gpio_get; |
| 272 | pg->chip.set = pmic_gpio_set; |
| 273 | pg->chip.to_irq = pmic_gpio_to_irq; |
| 274 | pg->chip.base = pdata->gpio_base; |
| 275 | pg->chip.ngpio = NUM_GPIO; |
| 276 | pg->chip.can_sleep = 1; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 277 | pg->chip.parent = dev; |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 278 | |
Thomas Gleixner | d4b7de6 | 2011-02-05 10:46:32 +0000 | [diff] [blame] | 279 | mutex_init(&pg->buslock); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 280 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 281 | pg->chip.parent = dev; |
Linus Walleij | 3769a89 | 2015-12-08 22:54:20 +0100 | [diff] [blame] | 282 | retval = gpiochip_add_data(&pg->chip, pg); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 283 | if (retval) { |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 284 | pr_err("Can not add pmic gpio chip\n"); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 285 | goto err; |
| 286 | } |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 287 | |
| 288 | retval = request_irq(pg->irq, pmic_irq_handler, 0, "pmic", pg); |
| 289 | if (retval) { |
Joe Perches | 9a2ffd1 | 2011-03-29 15:21:45 -0700 | [diff] [blame] | 290 | pr_warn("Interrupt request failed\n"); |
Libo Chen | fef8ce16 | 2013-07-09 20:34:28 +0800 | [diff] [blame] | 291 | goto fail_request_irq; |
Thomas Gleixner | 98401ae | 2011-02-07 21:41:30 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 294 | for (i = 0; i < 8; i++) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 295 | irq_set_chip_and_handler_name(i + pg->irq_base, |
| 296 | &pmic_irqchip, |
| 297 | handle_simple_irq, |
| 298 | "demux"); |
| 299 | irq_set_chip_data(i + pg->irq_base, pg); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 300 | } |
| 301 | return 0; |
Libo Chen | fef8ce16 | 2013-07-09 20:34:28 +0800 | [diff] [blame] | 302 | |
| 303 | fail_request_irq: |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 304 | gpiochip_remove(&pg->chip); |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 305 | err: |
| 306 | iounmap(pg->gpiointr); |
| 307 | err2: |
| 308 | kfree(pg); |
| 309 | return retval; |
| 310 | } |
| 311 | |
| 312 | /* at the same time, register a platform driver |
| 313 | * this supports the sfi 0.81 fw */ |
| 314 | static struct platform_driver platform_pmic_gpio_driver = { |
| 315 | .driver = { |
| 316 | .name = DRIVER_NAME, |
Alek Du | 8950778 | 2010-07-13 10:56:25 +0100 | [diff] [blame] | 317 | }, |
| 318 | .probe = platform_pmic_gpio_probe, |
| 319 | }; |
| 320 | |
| 321 | static int __init platform_pmic_gpio_init(void) |
| 322 | { |
| 323 | return platform_driver_register(&platform_pmic_gpio_driver); |
| 324 | } |
| 325 | |
| 326 | subsys_initcall(platform_pmic_gpio_init); |
| 327 | |
| 328 | MODULE_AUTHOR("Alek Du <alek.du@intel.com>"); |
| 329 | MODULE_DESCRIPTION("Intel Moorestown PMIC GPIO driver"); |
| 330 | MODULE_LICENSE("GPL v2"); |