blob: 2c39811366bffc4c05e1e44721ac5efca8c6d158 [file] [log] [blame]
Zhu, Lejun104fb1d2014-06-03 13:26:04 +08001/*
2 * gpio-crystalcove.c - Intel Crystal Cove GPIO Driver
3 *
4 * Copyright (C) 2012, 2014 Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * Author: Yang, Bin <bin.yang@intel.com>
16 */
17
18#include <linux/interrupt.h>
Paul Gortmakerf1fb9c62015-04-30 21:47:39 -040019#include <linux/module.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080020#include <linux/platform_device.h>
21#include <linux/gpio.h>
Lee Jones8dbf2aa2014-06-19 15:40:41 +010022#include <linux/seq_file.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080023#include <linux/bitops.h>
24#include <linux/regmap.h>
25#include <linux/mfd/intel_soc_pmic.h>
26
27#define CRYSTALCOVE_GPIO_NUM 16
Shobhit Kumare189ca52015-03-12 22:01:26 +053028#define CRYSTALCOVE_VGPIO_NUM 95
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080029
30#define UPDATE_IRQ_TYPE BIT(0)
31#define UPDATE_IRQ_MASK BIT(1)
32
33#define GPIO0IRQ 0x0b
34#define GPIO1IRQ 0x0c
35#define MGPIO0IRQS0 0x19
36#define MGPIO1IRQS0 0x1a
37#define MGPIO0IRQSX 0x1b
38#define MGPIO1IRQSX 0x1c
39#define GPIO0P0CTLO 0x2b
40#define GPIO0P0CTLI 0x33
41#define GPIO1P0CTLO 0x3b
42#define GPIO1P0CTLI 0x43
Shobhit Kumare189ca52015-03-12 22:01:26 +053043#define GPIOPANELCTL 0x52
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080044
45#define CTLI_INTCNT_DIS (0)
46#define CTLI_INTCNT_NE (1 << 1)
47#define CTLI_INTCNT_PE (2 << 1)
48#define CTLI_INTCNT_BE (3 << 1)
49
50#define CTLO_DIR_IN (0)
51#define CTLO_DIR_OUT (1 << 5)
52
53#define CTLO_DRV_CMOS (0)
54#define CTLO_DRV_OD (1 << 4)
55
56#define CTLO_DRV_REN (1 << 3)
57
58#define CTLO_RVAL_2KDW (0)
59#define CTLO_RVAL_2KUP (1 << 1)
60#define CTLO_RVAL_50KDW (2 << 1)
61#define CTLO_RVAL_50KUP (3 << 1)
62
63#define CTLO_INPUT_SET (CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
64#define CTLO_OUTPUT_SET (CTLO_DIR_OUT | CTLO_INPUT_SET)
65
66enum ctrl_register {
67 CTRL_IN,
68 CTRL_OUT,
69};
70
71/**
72 * struct crystalcove_gpio - Crystal Cove GPIO controller
73 * @buslock: for bus lock/sync and unlock.
74 * @chip: the abstract gpio_chip structure.
75 * @regmap: the regmap from the parent device.
76 * @update: pending IRQ setting update, to be written to the chip upon unlock.
77 * @intcnt_value: the Interrupt Detect value to be written.
78 * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
79 */
80struct crystalcove_gpio {
81 struct mutex buslock; /* irq_bus_lock */
82 struct gpio_chip chip;
83 struct regmap *regmap;
84 int update;
85 int intcnt_value;
86 bool set_irq_mask;
87};
88
89static inline struct crystalcove_gpio *to_cg(struct gpio_chip *gc)
90{
91 return container_of(gc, struct crystalcove_gpio, chip);
92}
93
94static inline int to_reg(int gpio, enum ctrl_register reg_type)
95{
96 int reg;
97
Shobhit Kumare189ca52015-03-12 22:01:26 +053098 if (gpio == 94) {
99 return GPIOPANELCTL;
100 }
101
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800102 if (reg_type == CTRL_IN) {
103 if (gpio < 8)
104 reg = GPIO0P0CTLI;
105 else
106 reg = GPIO1P0CTLI;
107 } else {
108 if (gpio < 8)
109 reg = GPIO0P0CTLO;
110 else
111 reg = GPIO1P0CTLO;
112 }
113
114 return reg + gpio % 8;
115}
116
117static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
118 int gpio)
119{
120 u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
121 int mask = BIT(gpio % 8);
122
123 if (cg->set_irq_mask)
124 regmap_update_bits(cg->regmap, mirqs0, mask, mask);
125 else
126 regmap_update_bits(cg->regmap, mirqs0, mask, 0);
127}
128
129static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
130{
131 int reg = to_reg(gpio, CTRL_IN);
132
133 regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
134}
135
136static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
137{
138 struct crystalcove_gpio *cg = to_cg(chip);
139
Aaron Ludcdc3012014-09-25 10:57:26 +0800140 if (gpio > CRYSTALCOVE_VGPIO_NUM)
141 return 0;
142
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800143 return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
144 CTLO_INPUT_SET);
145}
146
147static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
148 int value)
149{
150 struct crystalcove_gpio *cg = to_cg(chip);
151
Aaron Ludcdc3012014-09-25 10:57:26 +0800152 if (gpio > CRYSTALCOVE_VGPIO_NUM)
153 return 0;
154
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800155 return regmap_write(cg->regmap, to_reg(gpio, CTRL_OUT),
156 CTLO_OUTPUT_SET | value);
157}
158
159static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
160{
161 struct crystalcove_gpio *cg = to_cg(chip);
162 int ret;
163 unsigned int val;
164
Aaron Ludcdc3012014-09-25 10:57:26 +0800165 if (gpio > CRYSTALCOVE_VGPIO_NUM)
166 return 0;
167
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800168 ret = regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &val);
169 if (ret)
170 return ret;
171
172 return val & 0x1;
173}
174
175static void crystalcove_gpio_set(struct gpio_chip *chip,
176 unsigned gpio, int value)
177{
178 struct crystalcove_gpio *cg = to_cg(chip);
179
Aaron Ludcdc3012014-09-25 10:57:26 +0800180 if (gpio > CRYSTALCOVE_VGPIO_NUM)
181 return;
182
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800183 if (value)
184 regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 1);
185 else
186 regmap_update_bits(cg->regmap, to_reg(gpio, CTRL_OUT), 1, 0);
187}
188
189static int crystalcove_irq_type(struct irq_data *data, unsigned type)
190{
191 struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
192
193 switch (type) {
194 case IRQ_TYPE_NONE:
195 cg->intcnt_value = CTLI_INTCNT_DIS;
196 break;
197 case IRQ_TYPE_EDGE_BOTH:
198 cg->intcnt_value = CTLI_INTCNT_BE;
199 break;
200 case IRQ_TYPE_EDGE_RISING:
201 cg->intcnt_value = CTLI_INTCNT_PE;
202 break;
203 case IRQ_TYPE_EDGE_FALLING:
204 cg->intcnt_value = CTLI_INTCNT_NE;
205 break;
206 default:
207 return -EINVAL;
208 }
209
210 cg->update |= UPDATE_IRQ_TYPE;
211
212 return 0;
213}
214
215static void crystalcove_bus_lock(struct irq_data *data)
216{
217 struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
218
219 mutex_lock(&cg->buslock);
220}
221
222static void crystalcove_bus_sync_unlock(struct irq_data *data)
223{
224 struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
225 int gpio = data->hwirq;
226
227 if (cg->update & UPDATE_IRQ_TYPE)
228 crystalcove_update_irq_ctrl(cg, gpio);
229 if (cg->update & UPDATE_IRQ_MASK)
230 crystalcove_update_irq_mask(cg, gpio);
231 cg->update = 0;
232
233 mutex_unlock(&cg->buslock);
234}
235
236static void crystalcove_irq_unmask(struct irq_data *data)
237{
238 struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
239
240 cg->set_irq_mask = false;
241 cg->update |= UPDATE_IRQ_MASK;
242}
243
244static void crystalcove_irq_mask(struct irq_data *data)
245{
246 struct crystalcove_gpio *cg = to_cg(irq_data_get_irq_chip_data(data));
247
248 cg->set_irq_mask = true;
249 cg->update |= UPDATE_IRQ_MASK;
250}
251
252static struct irq_chip crystalcove_irqchip = {
253 .name = "Crystal Cove",
254 .irq_mask = crystalcove_irq_mask,
255 .irq_unmask = crystalcove_irq_unmask,
256 .irq_set_type = crystalcove_irq_type,
257 .irq_bus_lock = crystalcove_bus_lock,
258 .irq_bus_sync_unlock = crystalcove_bus_sync_unlock,
Aaron Lu61e749d2015-05-28 10:58:49 +0800259 .flags = IRQCHIP_SKIP_SET_WAKE,
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800260};
261
262static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
263{
264 struct crystalcove_gpio *cg = data;
265 unsigned int p0, p1;
266 int pending;
267 int gpio;
268 unsigned int virq;
269
270 if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
271 regmap_read(cg->regmap, GPIO1IRQ, &p1))
272 return IRQ_NONE;
273
274 regmap_write(cg->regmap, GPIO0IRQ, p0);
275 regmap_write(cg->regmap, GPIO1IRQ, p1);
276
277 pending = p0 | p1 << 8;
278
Aaron Ludcdc3012014-09-25 10:57:26 +0800279 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800280 if (pending & BIT(gpio)) {
281 virq = irq_find_mapping(cg->chip.irqdomain, gpio);
Aaron Lue733a2f2015-01-12 10:09:32 +0800282 handle_nested_irq(virq);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800283 }
284 }
285
286 return IRQ_HANDLED;
287}
288
289static void crystalcove_gpio_dbg_show(struct seq_file *s,
290 struct gpio_chip *chip)
291{
292 struct crystalcove_gpio *cg = to_cg(chip);
293 int gpio, offset;
294 unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
295
Aaron Ludcdc3012014-09-25 10:57:26 +0800296 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800297 regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
298 regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
299 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
300 &mirqs0);
301 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
302 &mirqsx);
303 regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
304 &irq);
305
306 offset = gpio % 8;
307 seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s\n",
308 gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
309 ctli & 0x1 ? "hi" : "lo",
310 ctli & CTLI_INTCNT_NE ? "fall" : " ",
311 ctli & CTLI_INTCNT_PE ? "rise" : " ",
312 ctlo,
313 mirqs0 & BIT(offset) ? "s0 mask " : "s0 unmask",
314 mirqsx & BIT(offset) ? "sx mask " : "sx unmask",
315 irq & BIT(offset) ? "pending" : " ");
316 }
317}
318
319static int crystalcove_gpio_probe(struct platform_device *pdev)
320{
321 int irq = platform_get_irq(pdev, 0);
322 struct crystalcove_gpio *cg;
323 int retval;
324 struct device *dev = pdev->dev.parent;
325 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
326
327 if (irq < 0)
328 return irq;
329
330 cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
331 if (!cg)
332 return -ENOMEM;
333
334 platform_set_drvdata(pdev, cg);
335
336 mutex_init(&cg->buslock);
337 cg->chip.label = KBUILD_MODNAME;
338 cg->chip.direction_input = crystalcove_gpio_dir_in;
339 cg->chip.direction_output = crystalcove_gpio_dir_out;
340 cg->chip.get = crystalcove_gpio_get;
341 cg->chip.set = crystalcove_gpio_set;
342 cg->chip.base = -1;
Aaron Ludcdc3012014-09-25 10:57:26 +0800343 cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800344 cg->chip.can_sleep = true;
345 cg->chip.dev = dev;
346 cg->chip.dbg_show = crystalcove_gpio_dbg_show;
347 cg->regmap = pmic->regmap;
348
349 retval = gpiochip_add(&cg->chip);
350 if (retval) {
351 dev_warn(&pdev->dev, "add gpio chip error: %d\n", retval);
352 return retval;
353 }
354
355 gpiochip_irqchip_add(&cg->chip, &crystalcove_irqchip, 0,
356 handle_simple_irq, IRQ_TYPE_NONE);
357
358 retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
359 IRQF_ONESHOT, KBUILD_MODNAME, cg);
360
361 if (retval) {
362 dev_warn(&pdev->dev, "request irq failed: %d\n", retval);
363 goto out_remove_gpio;
364 }
365
366 return 0;
367
368out_remove_gpio:
Linus Walleijda26d5d2014-09-16 15:11:41 -0700369 gpiochip_remove(&cg->chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800370 return retval;
371}
372
373static int crystalcove_gpio_remove(struct platform_device *pdev)
374{
375 struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
376 int irq = platform_get_irq(pdev, 0);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800377
Linus Walleijda26d5d2014-09-16 15:11:41 -0700378 gpiochip_remove(&cg->chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800379 if (irq >= 0)
380 free_irq(irq, cg);
Linus Walleijda26d5d2014-09-16 15:11:41 -0700381 return 0;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800382}
383
384static struct platform_driver crystalcove_gpio_driver = {
385 .probe = crystalcove_gpio_probe,
386 .remove = crystalcove_gpio_remove,
387 .driver = {
388 .name = "crystal_cove_gpio",
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800389 },
390};
391
392module_platform_driver(crystalcove_gpio_driver);
393
394MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
395MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
396MODULE_LICENSE("GPL v2");