Blame view

drivers/gpio/gpio-crystalcove.c 9.52 KB
e83624302   Andy Shevchenko   gpio: crystalcove...
1
  // SPDX-License-Identifier: GPL-2.0
104fb1d51   Zhu, Lejun   gpio: Add support...
2
  /*
e83624302   Andy Shevchenko   gpio: crystalcove...
3
   * Intel Crystal Cove GPIO Driver
104fb1d51   Zhu, Lejun   gpio: Add support...
4
5
6
   *
   * Copyright (C) 2012, 2014 Intel Corporation. All rights reserved.
   *
104fb1d51   Zhu, Lejun   gpio: Add support...
7
8
   * Author: Yang, Bin <bin.yang@intel.com>
   */
78207c5bf   Andy Shevchenko   gpio: crystalcove...
9
10
  #include <linux/bitops.h>
  #include <linux/gpio/driver.h>
104fb1d51   Zhu, Lejun   gpio: Add support...
11
  #include <linux/interrupt.h>
78207c5bf   Andy Shevchenko   gpio: crystalcove...
12
  #include <linux/mfd/intel_soc_pmic.h>
f1fb9c61b   Paul Gortmaker   drivers/gpio: inc...
13
  #include <linux/module.h>
104fb1d51   Zhu, Lejun   gpio: Add support...
14
  #include <linux/platform_device.h>
104fb1d51   Zhu, Lejun   gpio: Add support...
15
  #include <linux/regmap.h>
78207c5bf   Andy Shevchenko   gpio: crystalcove...
16
  #include <linux/seq_file.h>
104fb1d51   Zhu, Lejun   gpio: Add support...
17
18
  
  #define CRYSTALCOVE_GPIO_NUM	16
e189ca56d   Shobhit Kumar   gpio/crystalcove:...
19
  #define CRYSTALCOVE_VGPIO_NUM	95
104fb1d51   Zhu, Lejun   gpio: Add support...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
  #define UPDATE_IRQ_TYPE		BIT(0)
  #define UPDATE_IRQ_MASK		BIT(1)
  
  #define GPIO0IRQ		0x0b
  #define GPIO1IRQ		0x0c
  #define MGPIO0IRQS0		0x19
  #define MGPIO1IRQS0		0x1a
  #define MGPIO0IRQSX		0x1b
  #define MGPIO1IRQSX		0x1c
  #define GPIO0P0CTLO		0x2b
  #define GPIO0P0CTLI		0x33
  #define GPIO1P0CTLO		0x3b
  #define GPIO1P0CTLI		0x43
e189ca56d   Shobhit Kumar   gpio/crystalcove:...
34
  #define GPIOPANELCTL		0x52
104fb1d51   Zhu, Lejun   gpio: Add support...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  
  #define CTLI_INTCNT_DIS		(0)
  #define CTLI_INTCNT_NE		(1 << 1)
  #define CTLI_INTCNT_PE		(2 << 1)
  #define CTLI_INTCNT_BE		(3 << 1)
  
  #define CTLO_DIR_IN		(0)
  #define CTLO_DIR_OUT		(1 << 5)
  
  #define CTLO_DRV_CMOS		(0)
  #define CTLO_DRV_OD		(1 << 4)
  
  #define CTLO_DRV_REN		(1 << 3)
  
  #define CTLO_RVAL_2KDW		(0)
  #define CTLO_RVAL_2KUP		(1 << 1)
  #define CTLO_RVAL_50KDW		(2 << 1)
  #define CTLO_RVAL_50KUP		(3 << 1)
  
  #define CTLO_INPUT_SET	(CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
  #define CTLO_OUTPUT_SET	(CTLO_DIR_OUT | CTLO_INPUT_SET)
  
  enum ctrl_register {
  	CTRL_IN,
  	CTRL_OUT,
  };
  
  /**
   * struct crystalcove_gpio - Crystal Cove GPIO controller
   * @buslock: for bus lock/sync and unlock.
   * @chip: the abstract gpio_chip structure.
   * @regmap: the regmap from the parent device.
   * @update: pending IRQ setting update, to be written to the chip upon unlock.
   * @intcnt_value: the Interrupt Detect value to be written.
   * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
   */
  struct crystalcove_gpio {
  	struct mutex buslock; /* irq_bus_lock */
  	struct gpio_chip chip;
  	struct regmap *regmap;
  	int update;
  	int intcnt_value;
  	bool set_irq_mask;
  };
104fb1d51   Zhu, Lejun   gpio: Add support...
79
80
81
  static inline int to_reg(int gpio, enum ctrl_register reg_type)
  {
  	int reg;
9a752b4c9   Hans de Goede   gpio: crystalcove...
82
83
84
85
86
87
88
89
90
91
92
93
  	if (gpio >= CRYSTALCOVE_GPIO_NUM) {
  		/*
  		 * Virtual GPIO called from ACPI, for now we only support
  		 * the panel ctl.
  		 */
  		switch (gpio) {
  		case 0x5e:
  			return GPIOPANELCTL;
  		default:
  			return -EOPNOTSUPP;
  		}
  	}
e189ca56d   Shobhit Kumar   gpio/crystalcove:...
94

104fb1d51   Zhu, Lejun   gpio: Add support...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	if (reg_type == CTRL_IN) {
  		if (gpio < 8)
  			reg = GPIO0P0CTLI;
  		else
  			reg = GPIO1P0CTLI;
  	} else {
  		if (gpio < 8)
  			reg = GPIO0P0CTLO;
  		else
  			reg = GPIO1P0CTLO;
  	}
  
  	return reg + gpio % 8;
  }
  
  static void crystalcove_update_irq_mask(struct crystalcove_gpio *cg,
  					int gpio)
  {
  	u8 mirqs0 = gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0;
  	int mask = BIT(gpio % 8);
  
  	if (cg->set_irq_mask)
  		regmap_update_bits(cg->regmap, mirqs0, mask, mask);
  	else
  		regmap_update_bits(cg->regmap, mirqs0, mask, 0);
  }
  
  static void crystalcove_update_irq_ctrl(struct crystalcove_gpio *cg, int gpio)
  {
  	int reg = to_reg(gpio, CTRL_IN);
  
  	regmap_update_bits(cg->regmap, reg, CTLI_INTCNT_BE, cg->intcnt_value);
  }
  
  static int crystalcove_gpio_dir_in(struct gpio_chip *chip, unsigned gpio)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
131
  	struct crystalcove_gpio *cg = gpiochip_get_data(chip);
9a752b4c9   Hans de Goede   gpio: crystalcove...
132
  	int reg = to_reg(gpio, CTRL_OUT);
104fb1d51   Zhu, Lejun   gpio: Add support...
133

9a752b4c9   Hans de Goede   gpio: crystalcove...
134
  	if (reg < 0)
dcdc3018d   Aaron Lu   gpio: crystalcove...
135
  		return 0;
9a752b4c9   Hans de Goede   gpio: crystalcove...
136
  	return regmap_write(cg->regmap, reg, CTLO_INPUT_SET);
104fb1d51   Zhu, Lejun   gpio: Add support...
137
138
139
140
141
  }
  
  static int crystalcove_gpio_dir_out(struct gpio_chip *chip, unsigned gpio,
  				    int value)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
142
  	struct crystalcove_gpio *cg = gpiochip_get_data(chip);
9a752b4c9   Hans de Goede   gpio: crystalcove...
143
  	int reg = to_reg(gpio, CTRL_OUT);
104fb1d51   Zhu, Lejun   gpio: Add support...
144

9a752b4c9   Hans de Goede   gpio: crystalcove...
145
  	if (reg < 0)
dcdc3018d   Aaron Lu   gpio: crystalcove...
146
  		return 0;
9a752b4c9   Hans de Goede   gpio: crystalcove...
147
  	return regmap_write(cg->regmap, reg, CTLO_OUTPUT_SET | value);
104fb1d51   Zhu, Lejun   gpio: Add support...
148
149
150
151
  }
  
  static int crystalcove_gpio_get(struct gpio_chip *chip, unsigned gpio)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
152
  	struct crystalcove_gpio *cg = gpiochip_get_data(chip);
104fb1d51   Zhu, Lejun   gpio: Add support...
153
  	unsigned int val;
9a752b4c9   Hans de Goede   gpio: crystalcove...
154
  	int ret, reg = to_reg(gpio, CTRL_IN);
104fb1d51   Zhu, Lejun   gpio: Add support...
155

9a752b4c9   Hans de Goede   gpio: crystalcove...
156
  	if (reg < 0)
dcdc3018d   Aaron Lu   gpio: crystalcove...
157
  		return 0;
9a752b4c9   Hans de Goede   gpio: crystalcove...
158
  	ret = regmap_read(cg->regmap, reg, &val);
104fb1d51   Zhu, Lejun   gpio: Add support...
159
160
161
162
163
164
165
166
167
  	if (ret)
  		return ret;
  
  	return val & 0x1;
  }
  
  static void crystalcove_gpio_set(struct gpio_chip *chip,
  				 unsigned gpio, int value)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
168
  	struct crystalcove_gpio *cg = gpiochip_get_data(chip);
9a752b4c9   Hans de Goede   gpio: crystalcove...
169
  	int reg = to_reg(gpio, CTRL_OUT);
104fb1d51   Zhu, Lejun   gpio: Add support...
170

9a752b4c9   Hans de Goede   gpio: crystalcove...
171
  	if (reg < 0)
dcdc3018d   Aaron Lu   gpio: crystalcove...
172
  		return;
104fb1d51   Zhu, Lejun   gpio: Add support...
173
  	if (value)
9a752b4c9   Hans de Goede   gpio: crystalcove...
174
  		regmap_update_bits(cg->regmap, reg, 1, 1);
104fb1d51   Zhu, Lejun   gpio: Add support...
175
  	else
9a752b4c9   Hans de Goede   gpio: crystalcove...
176
  		regmap_update_bits(cg->regmap, reg, 1, 0);
104fb1d51   Zhu, Lejun   gpio: Add support...
177
178
179
180
  }
  
  static int crystalcove_irq_type(struct irq_data *data, unsigned type)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
181
182
  	struct crystalcove_gpio *cg =
  		gpiochip_get_data(irq_data_get_irq_chip_data(data));
104fb1d51   Zhu, Lejun   gpio: Add support...
183

9a752b4c9   Hans de Goede   gpio: crystalcove...
184
185
  	if (data->hwirq >= CRYSTALCOVE_GPIO_NUM)
  		return 0;
104fb1d51   Zhu, Lejun   gpio: Add support...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  	switch (type) {
  	case IRQ_TYPE_NONE:
  		cg->intcnt_value = CTLI_INTCNT_DIS;
  		break;
  	case IRQ_TYPE_EDGE_BOTH:
  		cg->intcnt_value = CTLI_INTCNT_BE;
  		break;
  	case IRQ_TYPE_EDGE_RISING:
  		cg->intcnt_value = CTLI_INTCNT_PE;
  		break;
  	case IRQ_TYPE_EDGE_FALLING:
  		cg->intcnt_value = CTLI_INTCNT_NE;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	cg->update |= UPDATE_IRQ_TYPE;
  
  	return 0;
  }
  
  static void crystalcove_bus_lock(struct irq_data *data)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
210
211
  	struct crystalcove_gpio *cg =
  		gpiochip_get_data(irq_data_get_irq_chip_data(data));
104fb1d51   Zhu, Lejun   gpio: Add support...
212
213
214
215
216
217
  
  	mutex_lock(&cg->buslock);
  }
  
  static void crystalcove_bus_sync_unlock(struct irq_data *data)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
218
219
  	struct crystalcove_gpio *cg =
  		gpiochip_get_data(irq_data_get_irq_chip_data(data));
104fb1d51   Zhu, Lejun   gpio: Add support...
220
221
222
223
224
225
226
227
228
229
230
231
232
  	int gpio = data->hwirq;
  
  	if (cg->update & UPDATE_IRQ_TYPE)
  		crystalcove_update_irq_ctrl(cg, gpio);
  	if (cg->update & UPDATE_IRQ_MASK)
  		crystalcove_update_irq_mask(cg, gpio);
  	cg->update = 0;
  
  	mutex_unlock(&cg->buslock);
  }
  
  static void crystalcove_irq_unmask(struct irq_data *data)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
233
234
  	struct crystalcove_gpio *cg =
  		gpiochip_get_data(irq_data_get_irq_chip_data(data));
104fb1d51   Zhu, Lejun   gpio: Add support...
235

9a752b4c9   Hans de Goede   gpio: crystalcove...
236
237
238
239
  	if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
  		cg->set_irq_mask = false;
  		cg->update |= UPDATE_IRQ_MASK;
  	}
104fb1d51   Zhu, Lejun   gpio: Add support...
240
241
242
243
  }
  
  static void crystalcove_irq_mask(struct irq_data *data)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
244
245
  	struct crystalcove_gpio *cg =
  		gpiochip_get_data(irq_data_get_irq_chip_data(data));
104fb1d51   Zhu, Lejun   gpio: Add support...
246

9a752b4c9   Hans de Goede   gpio: crystalcove...
247
248
249
250
  	if (data->hwirq < CRYSTALCOVE_GPIO_NUM) {
  		cg->set_irq_mask = true;
  		cg->update |= UPDATE_IRQ_MASK;
  	}
104fb1d51   Zhu, Lejun   gpio: Add support...
251
252
253
254
255
256
257
258
259
  }
  
  static struct irq_chip crystalcove_irqchip = {
  	.name			= "Crystal Cove",
  	.irq_mask		= crystalcove_irq_mask,
  	.irq_unmask		= crystalcove_irq_unmask,
  	.irq_set_type		= crystalcove_irq_type,
  	.irq_bus_lock		= crystalcove_bus_lock,
  	.irq_bus_sync_unlock	= crystalcove_bus_sync_unlock,
61e749d7e   Aaron Lu   gpio: crystalcove...
260
  	.flags			= IRQCHIP_SKIP_SET_WAKE,
104fb1d51   Zhu, Lejun   gpio: Add support...
261
262
263
264
265
  };
  
  static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
  {
  	struct crystalcove_gpio *cg = data;
fcce88d9c   Andy Shevchenko   gpio: crystalcove...
266
  	unsigned long pending;
104fb1d51   Zhu, Lejun   gpio: Add support...
267
  	unsigned int p0, p1;
104fb1d51   Zhu, Lejun   gpio: Add support...
268
269
270
271
272
273
274
275
276
277
278
  	int gpio;
  	unsigned int virq;
  
  	if (regmap_read(cg->regmap, GPIO0IRQ, &p0) ||
  	    regmap_read(cg->regmap, GPIO1IRQ, &p1))
  		return IRQ_NONE;
  
  	regmap_write(cg->regmap, GPIO0IRQ, p0);
  	regmap_write(cg->regmap, GPIO1IRQ, p1);
  
  	pending = p0 | p1 << 8;
fcce88d9c   Andy Shevchenko   gpio: crystalcove...
279
280
281
  	for_each_set_bit(gpio, &pending, CRYSTALCOVE_GPIO_NUM) {
  		virq = irq_find_mapping(cg->chip.irq.domain, gpio);
  		handle_nested_irq(virq);
104fb1d51   Zhu, Lejun   gpio: Add support...
282
283
284
285
286
287
288
289
  	}
  
  	return IRQ_HANDLED;
  }
  
  static void crystalcove_gpio_dbg_show(struct seq_file *s,
  				      struct gpio_chip *chip)
  {
435cc3d42   Linus Walleij   gpio: crystalcove...
290
  	struct crystalcove_gpio *cg = gpiochip_get_data(chip);
104fb1d51   Zhu, Lejun   gpio: Add support...
291
292
  	int gpio, offset;
  	unsigned int ctlo, ctli, mirqs0, mirqsx, irq;
dcdc3018d   Aaron Lu   gpio: crystalcove...
293
  	for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
104fb1d51   Zhu, Lejun   gpio: Add support...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  		regmap_read(cg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
  		regmap_read(cg->regmap, to_reg(gpio, CTRL_IN), &ctli);
  		regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQS0 : MGPIO1IRQS0,
  			    &mirqs0);
  		regmap_read(cg->regmap, gpio < 8 ? MGPIO0IRQSX : MGPIO1IRQSX,
  			    &mirqsx);
  		regmap_read(cg->regmap, gpio < 8 ? GPIO0IRQ : GPIO1IRQ,
  			    &irq);
  
  		offset = gpio % 8;
  		seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s %s
  ",
  			   gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
  			   ctli & 0x1 ? "hi" : "lo",
  			   ctli & CTLI_INTCNT_NE ? "fall" : "    ",
  			   ctli & CTLI_INTCNT_PE ? "rise" : "    ",
  			   ctlo,
  			   mirqs0 & BIT(offset) ? "s0 mask  " : "s0 unmask",
  			   mirqsx & BIT(offset) ? "sx mask  " : "sx unmask",
  			   irq & BIT(offset) ? "pending" : "       ");
  	}
  }
  
  static int crystalcove_gpio_probe(struct platform_device *pdev)
  {
  	int irq = platform_get_irq(pdev, 0);
  	struct crystalcove_gpio *cg;
  	int retval;
  	struct device *dev = pdev->dev.parent;
  	struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  
  	if (irq < 0)
  		return irq;
  
  	cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
  	if (!cg)
  		return -ENOMEM;
  
  	platform_set_drvdata(pdev, cg);
  
  	mutex_init(&cg->buslock);
  	cg->chip.label = KBUILD_MODNAME;
  	cg->chip.direction_input = crystalcove_gpio_dir_in;
  	cg->chip.direction_output = crystalcove_gpio_dir_out;
  	cg->chip.get = crystalcove_gpio_get;
  	cg->chip.set = crystalcove_gpio_set;
  	cg->chip.base = -1;
dcdc3018d   Aaron Lu   gpio: crystalcove...
341
  	cg->chip.ngpio = CRYSTALCOVE_VGPIO_NUM;
104fb1d51   Zhu, Lejun   gpio: Add support...
342
  	cg->chip.can_sleep = true;
58383c784   Linus Walleij   gpio: change memb...
343
  	cg->chip.parent = dev;
104fb1d51   Zhu, Lejun   gpio: Add support...
344
345
  	cg->chip.dbg_show = crystalcove_gpio_dbg_show;
  	cg->regmap = pmic->regmap;
828e47e45   Laxman Dewangan   gpio: crystalcove...
346
  	retval = devm_gpiochip_add_data(&pdev->dev, &cg->chip, cg);
104fb1d51   Zhu, Lejun   gpio: Add support...
347
348
349
350
351
  	if (retval) {
  		dev_warn(&pdev->dev, "add gpio chip error: %d
  ", retval);
  		return retval;
  	}
d245b3f9b   Linus Walleij   gpio: simplify ad...
352
353
  	gpiochip_irqchip_add_nested(&cg->chip, &crystalcove_irqchip, 0,
  				    handle_simple_irq, IRQ_TYPE_NONE);
104fb1d51   Zhu, Lejun   gpio: Add support...
354
355
356
357
358
359
360
  
  	retval = request_threaded_irq(irq, NULL, crystalcove_gpio_irq_handler,
  				      IRQF_ONESHOT, KBUILD_MODNAME, cg);
  
  	if (retval) {
  		dev_warn(&pdev->dev, "request irq failed: %d
  ", retval);
828e47e45   Laxman Dewangan   gpio: crystalcove...
361
  		return retval;
104fb1d51   Zhu, Lejun   gpio: Add support...
362
  	}
35ca3f616   Linus Walleij   gpio: set explici...
363
  	gpiochip_set_nested_irqchip(&cg->chip, &crystalcove_irqchip, irq);
104fb1d51   Zhu, Lejun   gpio: Add support...
364
  	return 0;
104fb1d51   Zhu, Lejun   gpio: Add support...
365
366
367
368
369
370
  }
  
  static int crystalcove_gpio_remove(struct platform_device *pdev)
  {
  	struct crystalcove_gpio *cg = platform_get_drvdata(pdev);
  	int irq = platform_get_irq(pdev, 0);
104fb1d51   Zhu, Lejun   gpio: Add support...
371
372
373
  
  	if (irq >= 0)
  		free_irq(irq, cg);
da26d5d80   Linus Walleij   gpio: remove rema...
374
  	return 0;
104fb1d51   Zhu, Lejun   gpio: Add support...
375
376
377
378
379
380
381
  }
  
  static struct platform_driver crystalcove_gpio_driver = {
  	.probe = crystalcove_gpio_probe,
  	.remove = crystalcove_gpio_remove,
  	.driver = {
  		.name = "crystal_cove_gpio",
104fb1d51   Zhu, Lejun   gpio: Add support...
382
383
384
385
386
387
388
389
  	},
  };
  
  module_platform_driver(crystalcove_gpio_driver);
  
  MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
  MODULE_DESCRIPTION("Intel Crystal Cove GPIO Driver");
  MODULE_LICENSE("GPL v2");