blob: 92f697f2a453fbbc8595393d55c8bb19cac30eb3 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Qualcomm PMIC8XXX GPIO driver
3 *
Willie Ruan0fe879c2013-01-14 15:16:48 -08004 * Copyright (c) 2011-2013, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 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
16#define pr_fmt(fmt) "%s: " fmt, __func__
17
Steve Mucklef132c6c2012-06-06 18:30:57 -070018#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/mfd/pm8xxx/core.h>
22#include <linux/mfd/pm8xxx/gpio.h>
23#include <linux/debugfs.h>
24#include <linux/uaccess.h>
25#include <linux/fs.h>
26#include <linux/seq_file.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29
30/* GPIO registers */
31#define SSBI_REG_ADDR_GPIO_BASE 0x150
32#define SSBI_REG_ADDR_GPIO(n) (SSBI_REG_ADDR_GPIO_BASE + n)
33
34/* GPIO */
35#define PM_GPIO_BANK_MASK 0x70
36#define PM_GPIO_BANK_SHIFT 4
37#define PM_GPIO_WRITE 0x80
38
39/* Bank 0 */
40#define PM_GPIO_VIN_MASK 0x0E
41#define PM_GPIO_VIN_SHIFT 1
42#define PM_GPIO_MODE_ENABLE 0x01
43
44/* Bank 1 */
45#define PM_GPIO_MODE_MASK 0x0C
46#define PM_GPIO_MODE_SHIFT 2
47#define PM_GPIO_OUT_BUFFER 0x02
48#define PM_GPIO_OUT_INVERT 0x01
49
50#define PM_GPIO_MODE_OFF 3
51#define PM_GPIO_MODE_OUTPUT 2
52#define PM_GPIO_MODE_INPUT 0
53#define PM_GPIO_MODE_BOTH 1
54
55/* Bank 2 */
56#define PM_GPIO_PULL_MASK 0x0E
57#define PM_GPIO_PULL_SHIFT 1
58
59/* Bank 3 */
60#define PM_GPIO_OUT_STRENGTH_MASK 0x0C
61#define PM_GPIO_OUT_STRENGTH_SHIFT 2
62#define PM_GPIO_PIN_ENABLE 0x00
63#define PM_GPIO_PIN_DISABLE 0x01
64
65/* Bank 4 */
66#define PM_GPIO_FUNC_MASK 0x0E
67#define PM_GPIO_FUNC_SHIFT 1
68
69/* Bank 5 */
70#define PM_GPIO_NON_INT_POL_INV 0x08
71#define PM_GPIO_BANKS 6
72
73struct pm_gpio_chip {
74 struct list_head link;
75 struct gpio_chip gpio_chip;
76 spinlock_t pm_lock;
77 u8 *bank1;
78 int irq_base;
79};
80
81static LIST_HEAD(pm_gpio_chips);
82static DEFINE_MUTEX(pm_gpio_chips_lock);
83
84static int pm_gpio_get(struct pm_gpio_chip *pm_gpio_chip, unsigned gpio)
85{
86 int mode;
87
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 /* Get gpio value from config bank 1 if output gpio.
89 Get gpio value from IRQ RT status register for all other gpio modes.
90 */
91 mode = (pm_gpio_chip->bank1[gpio] & PM_GPIO_MODE_MASK) >>
92 PM_GPIO_MODE_SHIFT;
93 if (mode == PM_GPIO_MODE_OUTPUT)
94 return pm_gpio_chip->bank1[gpio] & PM_GPIO_OUT_INVERT;
95 else
96 return pm8xxx_read_irq_stat(pm_gpio_chip->gpio_chip.dev->parent,
97 pm_gpio_chip->irq_base + gpio);
98}
99
100static int pm_gpio_set(struct pm_gpio_chip *pm_gpio_chip,
101 unsigned gpio, int value)
102{
103 int rc;
104 u8 bank1;
105 unsigned long flags;
106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
108 bank1 = PM_GPIO_WRITE
109 | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_OUT_INVERT);
110
111 if (value)
112 bank1 |= PM_GPIO_OUT_INVERT;
113
114 pm_gpio_chip->bank1[gpio] = bank1;
115 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
116 SSBI_REG_ADDR_GPIO(gpio), bank1);
117 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
118
119 if (rc)
120 pr_err("FAIL pm8xxx_writeb(): rc=%d. "
121 "(gpio=%d, value=%d)\n",
122 rc, gpio, value);
123
124 return rc;
125}
126
127static int dir_map[] = {
128 PM_GPIO_MODE_OFF,
129 PM_GPIO_MODE_OUTPUT,
130 PM_GPIO_MODE_INPUT,
131 PM_GPIO_MODE_BOTH,
132};
133
134static int pm_gpio_set_direction(struct pm_gpio_chip *pm_gpio_chip,
135 unsigned gpio, int direction)
136{
137 int rc;
138 u8 bank1;
139 unsigned long flags;
140
141 if (!direction || pm_gpio_chip == NULL)
142 return -EINVAL;
143
144 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
145 bank1 = PM_GPIO_WRITE
146 | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_MODE_MASK);
147
148 bank1 |= ((dir_map[direction] << PM_GPIO_MODE_SHIFT)
149 & PM_GPIO_MODE_MASK);
150
151 pm_gpio_chip->bank1[gpio] = bank1;
152 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
153 SSBI_REG_ADDR_GPIO(gpio), bank1);
154 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
155
156 if (rc)
157 pr_err("Failed on pm8xxx_writeb(): rc=%d (GPIO config)\n",
158 rc);
159
160 return rc;
161}
162
163static int pm_gpio_init_bank1(struct pm_gpio_chip *pm_gpio_chip)
164{
165 int i, rc;
166 u8 bank;
167
168 for (i = 0; i < pm_gpio_chip->gpio_chip.ngpio; i++) {
169 bank = 1 << PM_GPIO_BANK_SHIFT;
170 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
171 SSBI_REG_ADDR_GPIO(i),
172 bank);
173 if (rc) {
174 pr_err("error setting bank rc=%d\n", rc);
175 return rc;
176 }
177
178 rc = pm8xxx_readb(pm_gpio_chip->gpio_chip.dev->parent,
179 SSBI_REG_ADDR_GPIO(i),
180 &pm_gpio_chip->bank1[i]);
181 if (rc) {
182 pr_err("error reading bank 1 rc=%d\n", rc);
183 return rc;
184 }
185 }
186 return 0;
187}
188
189static int pm_gpio_to_irq(struct gpio_chip *gpio_chip, unsigned offset)
190{
191 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
192
193 return pm_gpio_chip->irq_base + offset;
194}
195
196static int pm_gpio_read(struct gpio_chip *gpio_chip, unsigned offset)
197{
198 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
199
200 return pm_gpio_get(pm_gpio_chip, offset);
201}
202
203static void pm_gpio_write(struct gpio_chip *gpio_chip,
204 unsigned offset, int val)
205{
206 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
207
208 pm_gpio_set(pm_gpio_chip, offset, val);
209}
210
211static int pm_gpio_direction_input(struct gpio_chip *gpio_chip,
212 unsigned offset)
213{
214 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
215
216 return pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_IN);
217}
218
219static int pm_gpio_direction_output(struct gpio_chip *gpio_chip,
220 unsigned offset,
221 int val)
222{
223 int ret;
224 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
225
226 ret = pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_OUT);
227 if (!ret)
228 ret = pm_gpio_set(pm_gpio_chip, offset, val);
229
230 return ret;
231}
232
233static void pm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gpio_chip)
234{
235 static const char * const cmode[] = { "in", "in/out", "out", "off" };
236 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
237 u8 mode, state, bank;
238 const char *label;
239 int i, j;
240
241 for (i = 0; i < gpio_chip->ngpio; i++) {
242 label = gpiochip_is_requested(gpio_chip, i);
243 mode = (pm_gpio_chip->bank1[i] & PM_GPIO_MODE_MASK) >>
244 PM_GPIO_MODE_SHIFT;
245 state = pm_gpio_get(pm_gpio_chip, i);
246 seq_printf(s, "gpio-%-3d (%-12.12s) %-10.10s"
247 " %s",
248 gpio_chip->base + i,
249 label ? label : "--",
250 cmode[mode],
251 state ? "hi" : "lo");
252 for (j = 0; j < PM_GPIO_BANKS; j++) {
253 bank = j << PM_GPIO_BANK_SHIFT;
254 pm8xxx_writeb(gpio_chip->dev->parent,
255 SSBI_REG_ADDR_GPIO(i),
256 bank);
257 pm8xxx_readb(gpio_chip->dev->parent,
258 SSBI_REG_ADDR_GPIO(i),
259 &bank);
260 seq_printf(s, " 0x%02x", bank);
261 }
262 seq_printf(s, "\n");
263 }
264}
265
266static int __devinit pm_gpio_probe(struct platform_device *pdev)
267{
268 int ret;
269 const struct pm8xxx_gpio_platform_data *pdata = pdev->dev.platform_data;
270 struct pm_gpio_chip *pm_gpio_chip;
271
272 if (!pdata) {
273 pr_err("missing platform data\n");
274 return -EINVAL;
275 }
276
277 pm_gpio_chip = kzalloc(sizeof(struct pm_gpio_chip), GFP_KERNEL);
278 if (!pm_gpio_chip) {
279 pr_err("Cannot allocate pm_gpio_chip\n");
280 return -ENOMEM;
281 }
282
283 pm_gpio_chip->bank1 = kzalloc(sizeof(u8) * pdata->gpio_cdata.ngpios,
284 GFP_KERNEL);
285 if (!pm_gpio_chip->bank1) {
286 pr_err("Cannot allocate pm_gpio_chip->bank1\n");
Jay Chokshifc9f9332011-10-27 15:45:10 -0700287 ret = -ENOMEM;
288 goto free_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 }
290
291 spin_lock_init(&pm_gpio_chip->pm_lock);
292 pm_gpio_chip->gpio_chip.label = "pm-gpio";
293 pm_gpio_chip->gpio_chip.direction_input = pm_gpio_direction_input;
294 pm_gpio_chip->gpio_chip.direction_output = pm_gpio_direction_output;
295 pm_gpio_chip->gpio_chip.to_irq = pm_gpio_to_irq;
296 pm_gpio_chip->gpio_chip.get = pm_gpio_read;
297 pm_gpio_chip->gpio_chip.set = pm_gpio_write;
298 pm_gpio_chip->gpio_chip.dbg_show = pm_gpio_dbg_show;
299 pm_gpio_chip->gpio_chip.ngpio = pdata->gpio_cdata.ngpios;
David Collinsc010fa52011-12-14 14:10:49 -0800300 pm_gpio_chip->gpio_chip.can_sleep = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 pm_gpio_chip->gpio_chip.dev = &pdev->dev;
302 pm_gpio_chip->gpio_chip.base = pdata->gpio_base;
303 pm_gpio_chip->irq_base = platform_get_irq(pdev, 0);
304 mutex_lock(&pm_gpio_chips_lock);
305 list_add(&pm_gpio_chip->link, &pm_gpio_chips);
306 mutex_unlock(&pm_gpio_chips_lock);
307 platform_set_drvdata(pdev, pm_gpio_chip);
308
309 ret = gpiochip_add(&pm_gpio_chip->gpio_chip);
310 if (ret) {
311 pr_err("gpiochip_add failed ret = %d\n", ret);
312 goto reset_drvdata;
313 }
314
315 ret = pm_gpio_init_bank1(pm_gpio_chip);
316 if (ret) {
317 pr_err("gpio init bank failed ret = %d\n", ret);
318 goto remove_chip;
319 }
320
321 pr_info("OK: base=%d, ngpio=%d\n", pm_gpio_chip->gpio_chip.base,
322 pm_gpio_chip->gpio_chip.ngpio);
323
324 return 0;
325
326remove_chip:
327 if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
328 pr_err("failed to remove gpio chip\n");
329reset_drvdata:
330 platform_set_drvdata(pdev, NULL);
Jay Chokshifc9f9332011-10-27 15:45:10 -0700331 kfree(pm_gpio_chip->bank1);
332free_chip:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 kfree(pm_gpio_chip);
334 return ret;
335}
336
337static int __devexit pm_gpio_remove(struct platform_device *pdev)
338{
339 struct pm_gpio_chip *pm_gpio_chip
340 = platform_get_drvdata(pdev);
341
342 mutex_lock(&pm_gpio_chips_lock);
343 list_del(&pm_gpio_chip->link);
344 mutex_unlock(&pm_gpio_chips_lock);
345 platform_set_drvdata(pdev, NULL);
346 if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
347 pr_err("failed to remove gpio chip\n");
348 kfree(pm_gpio_chip->bank1);
349 kfree(pm_gpio_chip);
350 return 0;
351}
352
353int pm8xxx_gpio_config(int gpio, struct pm_gpio *param)
354{
355 int rc, pm_gpio = -EINVAL;
356 u8 bank[8];
357 unsigned long flags;
358 struct pm_gpio_chip *pm_gpio_chip;
359 struct gpio_chip *gpio_chip;
360
361 if (param == NULL)
362 return -EINVAL;
363
364 mutex_lock(&pm_gpio_chips_lock);
365 list_for_each_entry(pm_gpio_chip, &pm_gpio_chips, link) {
366 gpio_chip = &pm_gpio_chip->gpio_chip;
367 if (gpio >= gpio_chip->base
368 && gpio < gpio_chip->base + gpio_chip->ngpio) {
369 pm_gpio = gpio - gpio_chip->base;
370 break;
371 }
372 }
373 mutex_unlock(&pm_gpio_chips_lock);
374 if (pm_gpio < 0) {
375 pr_err("called on gpio %d not handled by any pmic\n", gpio);
376 return -EINVAL;
377 }
378
379 /* Select banks and configure the gpio */
380 bank[0] = PM_GPIO_WRITE |
381 ((param->vin_sel << PM_GPIO_VIN_SHIFT) &
382 PM_GPIO_VIN_MASK) |
383 PM_GPIO_MODE_ENABLE;
384 bank[1] = PM_GPIO_WRITE |
385 ((1 << PM_GPIO_BANK_SHIFT) &
386 PM_GPIO_BANK_MASK) |
387 ((dir_map[param->direction] <<
388 PM_GPIO_MODE_SHIFT) &
389 PM_GPIO_MODE_MASK) |
390 ((param->direction & PM_GPIO_DIR_OUT) ?
391 ((param->output_buffer & 1) ?
392 PM_GPIO_OUT_BUFFER : 0) : 0) |
393 ((param->direction & PM_GPIO_DIR_OUT) ?
394 param->output_value & 0x01 : 0);
395 bank[2] = PM_GPIO_WRITE |
396 ((2 << PM_GPIO_BANK_SHIFT) &
397 PM_GPIO_BANK_MASK) |
398 ((param->pull << PM_GPIO_PULL_SHIFT) &
399 PM_GPIO_PULL_MASK);
400 bank[3] = PM_GPIO_WRITE |
401 ((3 << PM_GPIO_BANK_SHIFT) &
402 PM_GPIO_BANK_MASK) |
403 ((param->out_strength <<
404 PM_GPIO_OUT_STRENGTH_SHIFT) &
405 PM_GPIO_OUT_STRENGTH_MASK) |
406 (param->disable_pin ?
407 PM_GPIO_PIN_DISABLE : PM_GPIO_PIN_ENABLE);
408 bank[4] = PM_GPIO_WRITE |
409 ((4 << PM_GPIO_BANK_SHIFT) &
410 PM_GPIO_BANK_MASK) |
411 ((param->function << PM_GPIO_FUNC_SHIFT) &
412 PM_GPIO_FUNC_MASK);
413 bank[5] = PM_GPIO_WRITE |
414 ((5 << PM_GPIO_BANK_SHIFT) & PM_GPIO_BANK_MASK) |
415 (param->inv_int_pol ? 0 : PM_GPIO_NON_INT_POL_INV);
416
417 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
418 /* Remember bank1 for later use */
419 pm_gpio_chip->bank1[pm_gpio] = bank[1];
420 rc = pm8xxx_write_buf(pm_gpio_chip->gpio_chip.dev->parent,
421 SSBI_REG_ADDR_GPIO(pm_gpio), bank, 6);
422 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
423
424 if (rc)
425 pr_err("Failed on pm8xxx_write_buf() rc=%d (GPIO config)\n",
426 rc);
427
428 return rc;
429}
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530430EXPORT_SYMBOL(pm8xxx_gpio_config);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431
432static struct platform_driver pm_gpio_driver = {
433 .probe = pm_gpio_probe,
434 .remove = __devexit_p(pm_gpio_remove),
435 .driver = {
436 .name = PM8XXX_GPIO_DEV_NAME,
437 .owner = THIS_MODULE,
438 },
439};
440
441static int __init pm_gpio_init(void)
442{
443 return platform_driver_register(&pm_gpio_driver);
444}
445postcore_initcall(pm_gpio_init);
446
447static void __exit pm_gpio_exit(void)
448{
449 platform_driver_unregister(&pm_gpio_driver);
450}
451module_exit(pm_gpio_exit);
452
453MODULE_LICENSE("GPL v2");
454MODULE_DESCRIPTION("PMIC GPIO driver");
455MODULE_VERSION("1.0");
456MODULE_ALIAS("platform:" PM8XXX_GPIO_DEV_NAME);