blob: 14d1f4c933b69c951e23c22b5ce4da660493942d [file] [log] [blame]
Andy Shevchenkoe8362432018-11-06 14:11:42 +02001// SPDX-License-Identifier: GPL-2.0
Zhu, Lejun104fb1d2014-06-03 13:26:04 +08002/*
Andy Shevchenkoe8362432018-11-06 14:11:42 +02003 * Intel Crystal Cove GPIO Driver
Zhu, Lejun104fb1d2014-06-03 13:26:04 +08004 *
5 * Copyright (C) 2012, 2014 Intel Corporation. All rights reserved.
6 *
Zhu, Lejun104fb1d2014-06-03 13:26:04 +08007 * Author: Yang, Bin <bin.yang@intel.com>
8 */
9
Andy Shevchenko78207c52018-09-04 14:26:25 +030010#include <linux/bitops.h>
11#include <linux/gpio/driver.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080012#include <linux/interrupt.h>
Andy Shevchenko78207c52018-09-04 14:26:25 +030013#include <linux/mfd/intel_soc_pmic.h>
Paul Gortmakerf1fb9c62015-04-30 21:47:39 -040014#include <linux/module.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080015#include <linux/platform_device.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080016#include <linux/regmap.h>
Andy Shevchenko78207c52018-09-04 14:26:25 +030017#include <linux/seq_file.h>
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080018
19#define CRYSTALCOVE_GPIO_NUM 16
Shobhit Kumare189ca52015-03-12 22:01:26 +053020#define CRYSTALCOVE_VGPIO_NUM 95
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080021
22#define UPDATE_IRQ_TYPE BIT(0)
23#define UPDATE_IRQ_MASK BIT(1)
24
25#define GPIO0IRQ 0x0b
26#define GPIO1IRQ 0x0c
27#define MGPIO0IRQS0 0x19
28#define MGPIO1IRQS0 0x1a
29#define MGPIO0IRQSX 0x1b
30#define MGPIO1IRQSX 0x1c
31#define GPIO0P0CTLO 0x2b
32#define GPIO0P0CTLI 0x33
33#define GPIO1P0CTLO 0x3b
34#define GPIO1P0CTLI 0x43
Shobhit Kumare189ca52015-03-12 22:01:26 +053035#define GPIOPANELCTL 0x52
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080036
37#define CTLI_INTCNT_DIS (0)
38#define CTLI_INTCNT_NE (1 << 1)
39#define CTLI_INTCNT_PE (2 << 1)
40#define CTLI_INTCNT_BE (3 << 1)
41
42#define CTLO_DIR_IN (0)
43#define CTLO_DIR_OUT (1 << 5)
44
45#define CTLO_DRV_CMOS (0)
46#define CTLO_DRV_OD (1 << 4)
47
48#define CTLO_DRV_REN (1 << 3)
49
50#define CTLO_RVAL_2KDW (0)
51#define CTLO_RVAL_2KUP (1 << 1)
52#define CTLO_RVAL_50KDW (2 << 1)
53#define CTLO_RVAL_50KUP (3 << 1)
54
55#define CTLO_INPUT_SET (CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
56#define CTLO_OUTPUT_SET (CTLO_DIR_OUT | CTLO_INPUT_SET)
57
58enum ctrl_register {
59 CTRL_IN,
60 CTRL_OUT,
61};
62
63/**
64 * struct crystalcove_gpio - Crystal Cove GPIO controller
65 * @buslock: for bus lock/sync and unlock.
66 * @chip: the abstract gpio_chip structure.
67 * @regmap: the regmap from the parent device.
68 * @update: pending IRQ setting update, to be written to the chip upon unlock.
69 * @intcnt_value: the Interrupt Detect value to be written.
70 * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
71 */
72struct crystalcove_gpio {
73 struct mutex buslock; /* irq_bus_lock */
74 struct gpio_chip chip;
75 struct regmap *regmap;
76 int update;
77 int intcnt_value;
78 bool set_irq_mask;
79};
80
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080081static inline int to_reg(int gpio, enum ctrl_register reg_type)
82{
83 int reg;
84
Hans de Goede9a752b42017-05-13 14:39:53 +020085 if (gpio >= CRYSTALCOVE_GPIO_NUM) {
86 /*
87 * Virtual GPIO called from ACPI, for now we only support
88 * the panel ctl.
89 */
90 switch (gpio) {
91 case 0x5e:
92 return GPIOPANELCTL;
93 default:
94 return -EOPNOTSUPP;
95 }
96 }
Shobhit Kumare189ca52015-03-12 22:01:26 +053097
Zhu, Lejun104fb1d2014-06-03 13:26:04 +080098 if (reg_type == CTRL_IN) {
99 if (gpio < 8)
100 reg = GPIO0P0CTLI;
101 else
102 reg = GPIO1P0CTLI;
103 } else {
104 if (gpio < 8)
105 reg = GPIO0P0CTLO;
106 else
107 reg = GPIO1P0CTLO;
108 }
109
110 return reg + gpio % 8;
111}
112
113static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
114 int gpio)
115{
116 u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
117 int mask = BIT(gpio % 8);
118
119 if (cg->set_irq_mask)
120 regmap_update_bits(cg->regmap, mirqs0, mask, mask);
121 else
122 regmap_update_bits(cg->regmap, mirqs0, mask, 0);
123}
124
125static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
126{
127 int reg = to_reg(gpio, CTRL_IN);
128
129 regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
130}
131
132static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
133{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100134 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200135 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800136
Hans de Goede9a752b42017-05-13 14:39:53 +0200137 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800138 return 0;
139
Hans de Goede9a752b42017-05-13 14:39:53 +0200140 return regmap_write(cg->regmap, reg, CTLO_INPUT_SET);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800141}
142
143static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
144 int value)
145{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100146 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200147 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800148
Hans de Goede9a752b42017-05-13 14:39:53 +0200149 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800150 return 0;
151
Hans de Goede9a752b42017-05-13 14:39:53 +0200152 return regmap_write(cg->regmap, reg, CTLO_OUTPUT_SET | value);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800153}
154
155static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
156{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100157 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800158 unsigned int val;
Hans de Goede9a752b42017-05-13 14:39:53 +0200159 int ret, reg = to_reg(gpio, CTRL_IN);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800160
Hans de Goede9a752b42017-05-13 14:39:53 +0200161 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800162 return 0;
163
Hans de Goede9a752b42017-05-13 14:39:53 +0200164 ret = regmap_read(cg->regmap, reg, &val);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800165 if (ret)
166 return ret;
167
168 return val & 0x1;
169}
170
171static void crystalcove_gpio_set(struct gpio_chip *chip,
172 unsigned gpio, int value)
173{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100174 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Hans de Goede9a752b42017-05-13 14:39:53 +0200175 int reg = to_reg(gpio, CTRL_OUT);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800176
Hans de Goede9a752b42017-05-13 14:39:53 +0200177 if (reg < 0)
Aaron Ludcdc3012014-09-25 10:57:26 +0800178 return;
179
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800180 if (value)
Hans de Goede9a752b42017-05-13 14:39:53 +0200181 regmap_update_bits(cg->regmap, reg, 1, 1);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800182 else
Hans de Goede9a752b42017-05-13 14:39:53 +0200183 regmap_update_bits(cg->regmap, reg, 1, 0);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800184}
185
186static int crystalcove_irq_type(struct irq_data *data, unsigned type)
187{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100188 struct crystalcove_gpio *cg =
189 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800190
Hans de Goede9a752b42017-05-13 14:39:53 +0200191 if (data->hwirq >= CRYSTALCOVE_GPIO_NUM)
192 return 0;
193
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800194 switch (type) {
195 case IRQ_TYPE_NONE:
196 cg->intcnt_value = CTLI_INTCNT_DIS;
197 break;
198 case IRQ_TYPE_EDGE_BOTH:
199 cg->intcnt_value = CTLI_INTCNT_BE;
200 break;
201 case IRQ_TYPE_EDGE_RISING:
202 cg->intcnt_value = CTLI_INTCNT_PE;
203 break;
204 case IRQ_TYPE_EDGE_FALLING:
205 cg->intcnt_value = CTLI_INTCNT_NE;
206 break;
207 default:
208 return -EINVAL;
209 }
210
211 cg->update |= UPDATE_IRQ_TYPE;
212
213 return 0;
214}
215
216static void crystalcove_bus_lock(struct irq_data *data)
217{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100218 struct crystalcove_gpio *cg =
219 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800220
221 mutex_lock(&cg->buslock);
222}
223
224static void crystalcove_bus_sync_unlock(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 int gpio = data->hwirq;
229
230 if (cg->update & UPDATE_IRQ_TYPE)
231 crystalcove_update_irq_ctrl(cg, gpio);
232 if (cg->update & UPDATE_IRQ_MASK)
233 crystalcove_update_irq_mask(cg, gpio);
234 cg->update = 0;
235
236 mutex_unlock(&cg->buslock);
237}
238
239static void crystalcove_irq_unmask(struct irq_data *data)
240{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100241 struct crystalcove_gpio *cg =
242 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800243
Hans de Goede9a752b42017-05-13 14:39:53 +0200244 if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
245 cg->set_irq_mask = false;
246 cg->update |= UPDATE_IRQ_MASK;
247 }
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800248}
249
250static void crystalcove_irq_mask(struct irq_data *data)
251{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100252 struct crystalcove_gpio *cg =
253 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800254
Hans de Goede9a752b42017-05-13 14:39:53 +0200255 if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
256 cg->set_irq_mask = true;
257 cg->update |= UPDATE_IRQ_MASK;
258 }
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800259}
260
261static struct irq_chip crystalcove_irqchip = {
262 .name = "Crystal Cove",
263 .irq_mask = crystalcove_irq_mask,
264 .irq_unmask = crystalcove_irq_unmask,
265 .irq_set_type = crystalcove_irq_type,
266 .irq_bus_lock = crystalcove_bus_lock,
267 .irq_bus_sync_unlock = crystalcove_bus_sync_unlock,
Aaron Lu61e749d2015-05-28 10:58:49 +0800268 .flags = IRQCHIP_SKIP_SET_WAKE,
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800269};
270
271static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
272{
273 struct crystalcove_gpio *cg = data;
Andy Shevchenkofcce88d2018-11-06 14:38:55 +0200274 unsigned long pending;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800275 unsigned int p0, p1;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800276 int gpio;
277 unsigned int virq;
278
279 if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
280 regmap_read(cg->regmap, GPIO1IRQ, &p1))
281 return IRQ_NONE;
282
283 regmap_write(cg->regmap, GPIO0IRQ, p0);
284 regmap_write(cg->regmap, GPIO1IRQ, p1);
285
286 pending = p0 | p1 << 8;
287
Andy Shevchenkofcce88d2018-11-06 14:38:55 +0200288 for_each_set_bit(gpio, &pending, CRYSTALCOVE_GPIO_NUM) {
289 virq = irq_find_mapping(cg->chip.irq.domain, gpio);
290 handle_nested_irq(virq);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800291 }
292
293 return IRQ_HANDLED;
294}
295
296static void crystalcove_gpio_dbg_show(struct seq_file *s,
297 struct gpio_chip *chip)
298{
Linus Walleij435cc3d2015-12-04 15:47:54 +0100299 struct crystalcove_gpio *cg = gpiochip_get_data(chip);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800300 int gpio, offset;
301 unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
302
Aaron Ludcdc3012014-09-25 10:57:26 +0800303 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800304 regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
305 regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
306 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
307 &mirqs0);
308 regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
309 &mirqsx);
310 regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
311 &irq);
312
313 offset = gpio % 8;
314 seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s\n",
315 gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
316 ctli & 0x1 ? "hi" : "lo",
317 ctli & CTLI_INTCNT_NE ? "fall" : " ",
318 ctli & CTLI_INTCNT_PE ? "rise" : " ",
319 ctlo,
320 mirqs0 & BIT(offset) ? "s0 mask " : "s0 unmask",
321 mirqsx & BIT(offset) ? "sx mask " : "sx unmask",
322 irq & BIT(offset) ? "pending" : " ");
323 }
324}
325
326static int crystalcove_gpio_probe(struct platform_device *pdev)
327{
328 int irq = platform_get_irq(pdev, 0);
329 struct crystalcove_gpio *cg;
330 int retval;
331 struct device *dev = pdev->dev.parent;
332 struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
333
334 if (irq < 0)
335 return irq;
336
337 cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
338 if (!cg)
339 return -ENOMEM;
340
341 platform_set_drvdata(pdev, cg);
342
343 mutex_init(&cg->buslock);
344 cg->chip.label = KBUILD_MODNAME;
345 cg->chip.direction_input = crystalcove_gpio_dir_in;
346 cg->chip.direction_output = crystalcove_gpio_dir_out;
347 cg->chip.get = crystalcove_gpio_get;
348 cg->chip.set = crystalcove_gpio_set;
349 cg->chip.base = -1;
Aaron Ludcdc3012014-09-25 10:57:26 +0800350 cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800351 cg->chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100352 cg->chip.parent = dev;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800353 cg->chip.dbg_show = crystalcove_gpio_dbg_show;
354 cg->regmap = pmic->regmap;
355
Laxman Dewangan828e47e2016-02-22 17:43:28 +0530356 retval = devm_gpiochip_add_data(&pdev->dev, &cg->chip, cg);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800357 if (retval) {
358 dev_warn(&pdev->dev, "add gpio chip error: %d\n", retval);
359 return retval;
360 }
361
Linus Walleijd245b3f2016-11-24 10:57:25 +0100362 gpiochip_irqchip_add_nested(&cg->chip, &crystalcove_irqchip, 0,
363 handle_simple_irq, IRQ_TYPE_NONE);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800364
365 retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
366 IRQF_ONESHOT, KBUILD_MODNAME, cg);
367
368 if (retval) {
369 dev_warn(&pdev->dev, "request irq failed: %d\n", retval);
Laxman Dewangan828e47e2016-02-22 17:43:28 +0530370 return retval;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800371 }
372
Linus Walleij35ca3f62016-11-24 13:27:54 +0100373 gpiochip_set_nested_irqchip(&cg->chip, &crystalcove_irqchip, irq);
374
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800375 return 0;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800376}
377
378static int crystalcove_gpio_remove(struct platform_device *pdev)
379{
380 struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
381 int irq = platform_get_irq(pdev, 0);
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800382
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800383 if (irq >= 0)
384 free_irq(irq, cg);
Linus Walleijda26d5d2014-09-16 15:11:41 -0700385 return 0;
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800386}
387
388static struct platform_driver crystalcove_gpio_driver = {
389 .probe = crystalcove_gpio_probe,
390 .remove = crystalcove_gpio_remove,
391 .driver = {
392 .name = "crystal_cove_gpio",
Zhu, Lejun104fb1d2014-06-03 13:26:04 +0800393 },
394};
395
396module_platform_driver(crystalcove_gpio_driver);
397
398MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
399MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
400MODULE_LICENSE("GPL v2");