blob: 3c39d1b6c3ada4a6860d0c5001763b63f8fedc6e [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>
Linus Walleij6ba40f82018-01-14 01:48:48 +010021#include <linux/gpio/driver.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
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080089static inline int to_reg(int gpio, enum ctrl_register reg_type)
90{
91 int reg;
92
Hans de Goede9a752b42017-05-13 14:39:53 +020093 if (gpio >= CRYSTALCOVE_GPIO_NUM) {
94 /*
95 * Virtual GPIO called from ACPI, for now we only support
96 * the panel ctl.
97 */
98 switch (gpio) {
99 case 0x5e:
100 return GPIOPANELCTL;
101 default:
102 return -EOPNOTSUPP;
103 }
104 }
Shobhit Kumare189ca52015-03-12 22:01:26 +0530105
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800106 if (reg_type == CTRL_IN) {
107 if (gpio < 8)
108 reg = GPIO0P0CTLI;
109 else
110 reg = GPIO1P0CTLI;
111 } else {
112 if (gpio < 8)
113 reg = GPIO0P0CTLO;
114 else
115 reg = GPIO1P0CTLO;
116 }
117
118 return reg + gpio % 8;
119}
120
121static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
122 int gpio)
123{
124 u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
125 int mask = BIT(gpio % 8);
126
127 if (cg->set_irq_mask)
128 regmap_update_bits(cg->regmap, mirqs0, mask, mask);
129 else
130 regmap_update_bits(cg->regmap, mirqs0, mask, 0);
131}
132
133static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
134{
135 int reg = to_reg(gpio, CTRL_IN);
136
137 regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
138}
139
140static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
141{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100142 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200143 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800144
Hans de Goede9a752b42017-05-13 14:39:53 +0200145 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800146 return 0;
147
Hans de Goede9a752b42017-05-13 14:39:53 +0200148 return regmap_write(cg->regmap, reg, CTLO_INPUT_SET);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800149}
150
151static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
152 int value)
153{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100154 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200155 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800156
Hans de Goede9a752b42017-05-13 14:39:53 +0200157 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800158 return 0;
159
Hans de Goede9a752b42017-05-13 14:39:53 +0200160 return regmap_write(cg->regmap, reg, CTLO_OUTPUT_SET | value);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800161}
162
163static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
164{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100165 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800166 unsigned int val;
Hans de Goede9a752b42017-05-13 14:39:53 +0200167 int ret, reg = to_reg(gpio, CTRL_IN);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800168
Hans de Goede9a752b42017-05-13 14:39:53 +0200169 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800170 return 0;
171
Hans de Goede9a752b42017-05-13 14:39:53 +0200172 ret = regmap_read(cg->regmap, reg, &val);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800173 if (ret)
174 return ret;
175
176 return val & 0x1;
177}
178
179static void crystalcove_gpio_set(struct gpio_chip *chip,
180 unsigned gpio, int value)
181{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100182 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200183 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800184
Hans de Goede9a752b42017-05-13 14:39:53 +0200185 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800186 return;
187
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800188 if (value)
Hans de Goede9a752b42017-05-13 14:39:53 +0200189 regmap_update_bits(cg->regmap, reg, 1, 1);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800190 else
Hans de Goede9a752b42017-05-13 14:39:53 +0200191 regmap_update_bits(cg->regmap, reg, 1, 0);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800192}
193
194static int crystalcove_irq_type(struct irq_data *data, unsigned type)
195{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100196 struct crystalcove_gpio *cg =
197 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800198
Hans de Goede9a752b42017-05-13 14:39:53 +0200199 if (data->hwirq >= CRYSTALCOVE_GPIO_NUM)
200 return 0;
201
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800202 switch (type) {
203 case IRQ_TYPE_NONE:
204 cg->intcnt_value = CTLI_INTCNT_DIS;
205 break;
206 case IRQ_TYPE_EDGE_BOTH:
207 cg->intcnt_value = CTLI_INTCNT_BE;
208 break;
209 case IRQ_TYPE_EDGE_RISING:
210 cg->intcnt_value = CTLI_INTCNT_PE;
211 break;
212 case IRQ_TYPE_EDGE_FALLING:
213 cg->intcnt_value = CTLI_INTCNT_NE;
214 break;
215 default:
216 return -EINVAL;
217 }
218
219 cg->update |= UPDATE_IRQ_TYPE;
220
221 return 0;
222}
223
224static void crystalcove_bus_lock(struct irq_data *data)
225{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100226 struct crystalcove_gpio *cg =
227 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800228
229 mutex_lock(&cg->buslock);
230}
231
232static void crystalcove_bus_sync_unlock(struct irq_data *data)
233{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100234 struct crystalcove_gpio *cg =
235 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800236 int gpio = data->hwirq;
237
238 if (cg->update & UPDATE_IRQ_TYPE)
239 crystalcove_update_irq_ctrl(cg, gpio);
240 if (cg->update & UPDATE_IRQ_MASK)
241 crystalcove_update_irq_mask(cg, gpio);
242 cg->update = 0;
243
244 mutex_unlock(&cg->buslock);
245}
246
247static void crystalcove_irq_unmask(struct irq_data *data)
248{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100249 struct crystalcove_gpio *cg =
250 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800251
Hans de Goede9a752b42017-05-13 14:39:53 +0200252 if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
253 cg->set_irq_mask = false;
254 cg->update |= UPDATE_IRQ_MASK;
255 }
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800256}
257
258static void crystalcove_irq_mask(struct irq_data *data)
259{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100260 struct crystalcove_gpio *cg =
261 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800262
Hans de Goede9a752b42017-05-13 14:39:53 +0200263 if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
264 cg->set_irq_mask = true;
265 cg->update |= UPDATE_IRQ_MASK;
266 }
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800267}
268
269static struct irq_chip crystalcove_irqchip = {
270 .name = "Crystal Cove",
271 .irq_mask = crystalcove_irq_mask,
272 .irq_unmask = crystalcove_irq_unmask,
273 .irq_set_type = crystalcove_irq_type,
274 .irq_bus_lock = crystalcove_bus_lock,
275 .irq_bus_sync_unlock = crystalcove_bus_sync_unlock,
Aaron Lu61e749d2015-05-28 10:58:49 +0800276 .flags = IRQCHIP_SKIP_SET_WAKE,
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800277};
278
279static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
280{
281 struct crystalcove_gpio *cg = data;
Andy Shevchenkofcce88d2018-11-06 14:38:55 +0200282 unsigned long pending;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800283 unsigned int p0, p1;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800284 int gpio;
285 unsigned int virq;
286
287 if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
288 regmap_read(cg->regmap, GPIO1IRQ, &p1))
289 return IRQ_NONE;
290
291 regmap_write(cg->regmap, GPIO0IRQ, p0);
292 regmap_write(cg->regmap, GPIO1IRQ, p1);
293
294 pending = p0 | p1 << 8;
295
Andy Shevchenkofcce88d2018-11-06 14:38:55 +0200296 for_each_set_bit(gpio, &pending, CRYSTALCOVE_GPIO_NUM) {
297 virq = irq_find_mapping(cg->chip.irq.domain, gpio);
298 handle_nested_irq(virq);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800299 }
300
301 return IRQ_HANDLED;
302}
303
304static void crystalcove_gpio_dbg_show(struct seq_file *s,
305 struct gpio_chip *chip)
306{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100307 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800308 int gpio, offset;
309 unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
310
Aaron Ludcdc3012014-09-25 10:57:26 +0800311 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800312 regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
313 regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
314 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
315 &mirqs0);
316 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
317 &mirqsx);
318 regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
319 &irq);
320
321 offset = gpio % 8;
322 seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s\n",
323 gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
324 ctli & 0x1 ? "hi" : "lo",
325 ctli & CTLI_INTCNT_NE ? "fall" : " ",
326 ctli & CTLI_INTCNT_PE ? "rise" : " ",
327 ctlo,
328 mirqs0 & BIT(offset) ? "s0 mask " : "s0 unmask",
329 mirqsx & BIT(offset) ? "sx mask " : "sx unmask",
330 irq & BIT(offset) ? "pending" : " ");
331 }
332}
333
334static int crystalcove_gpio_probe(struct platform_device *pdev)
335{
336 int irq = platform_get_irq(pdev, 0);
337 struct crystalcove_gpio *cg;
338 int retval;
339 struct device *dev = pdev->dev.parent;
340 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
341
342 if (irq < 0)
343 return irq;
344
345 cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
346 if (!cg)
347 return -ENOMEM;
348
349 platform_set_drvdata(pdev, cg);
350
351 mutex_init(&cg->buslock);
352 cg->chip.label = KBUILD_MODNAME;
353 cg->chip.direction_input = crystalcove_gpio_dir_in;
354 cg->chip.direction_output = crystalcove_gpio_dir_out;
355 cg->chip.get = crystalcove_gpio_get;
356 cg->chip.set = crystalcove_gpio_set;
357 cg->chip.base = -1;
Aaron Ludcdc3012014-09-25 10:57:26 +0800358 cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800359 cg->chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100360 cg->chip.parent = dev;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800361 cg->chip.dbg_show = crystalcove_gpio_dbg_show;
362 cg->regmap = pmic->regmap;
363
Laxman Dewangan828e47e2016-02-22 17:43:28 +0530364 retval = devm_gpiochip_add_data(&pdev->dev, &cg->chip, cg);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800365 if (retval) {
366 dev_warn(&pdev->dev, "add gpio chip error: %d\n", retval);
367 return retval;
368 }
369
Linus Walleijd245b3f2016-11-24 10:57:25 +0100370 gpiochip_irqchip_add_nested(&cg->chip, &crystalcove_irqchip, 0,
371 handle_simple_irq, IRQ_TYPE_NONE);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800372
373 retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
374 IRQF_ONESHOT, KBUILD_MODNAME, cg);
375
376 if (retval) {
377 dev_warn(&pdev->dev, "request irq failed: %d\n", retval);
Laxman Dewangan828e47e2016-02-22 17:43:28 +0530378 return retval;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800379 }
380
Linus Walleij35ca3f62016-11-24 13:27:54 +0100381 gpiochip_set_nested_irqchip(&cg->chip, &crystalcove_irqchip, irq);
382
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800383 return 0;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800384}
385
386static int crystalcove_gpio_remove(struct platform_device *pdev)
387{
388 struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
389 int irq = platform_get_irq(pdev, 0);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800390
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800391 if (irq >= 0)
392 free_irq(irq, cg);
Linus Walleijda26d5d2014-09-16 15:11:41 -0700393 return 0;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800394}
395
396static struct platform_driver crystalcove_gpio_driver = {
397 .probe = crystalcove_gpio_probe,
398 .remove = crystalcove_gpio_remove,
399 .driver = {
400 .name = "crystal_cove_gpio",
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800401 },
402};
403
404module_platform_driver(crystalcove_gpio_driver);
405
406MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
407MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
408MODULE_LICENSE("GPL v2");