blob: cb874e859bde448f455c570eb213d0f92715f7ea [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Qualcomm PMIC8XXX GPIO driver
3 *
David Collinsc010fa52011-12-14 14:10:49 -08004 * Copyright (c) 2011-2012, 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
88 if (gpio >= pm_gpio_chip->gpio_chip.ngpio || pm_gpio_chip == NULL)
89 return -EINVAL;
90
91 /* Get gpio value from config bank 1 if output gpio.
92 Get gpio value from IRQ RT status register for all other gpio modes.
93 */
94 mode = (pm_gpio_chip->bank1[gpio] & PM_GPIO_MODE_MASK) >>
95 PM_GPIO_MODE_SHIFT;
96 if (mode == PM_GPIO_MODE_OUTPUT)
97 return pm_gpio_chip->bank1[gpio] & PM_GPIO_OUT_INVERT;
98 else
99 return pm8xxx_read_irq_stat(pm_gpio_chip->gpio_chip.dev->parent,
100 pm_gpio_chip->irq_base + gpio);
101}
102
103static int pm_gpio_set(struct pm_gpio_chip *pm_gpio_chip,
104 unsigned gpio, int value)
105{
106 int rc;
107 u8 bank1;
108 unsigned long flags;
109
110 if (gpio >= pm_gpio_chip->gpio_chip.ngpio || pm_gpio_chip == NULL)
111 return -EINVAL;
112
113 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
114 bank1 = PM_GPIO_WRITE
115 | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_OUT_INVERT);
116
117 if (value)
118 bank1 |= PM_GPIO_OUT_INVERT;
119
120 pm_gpio_chip->bank1[gpio] = bank1;
121 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
122 SSBI_REG_ADDR_GPIO(gpio), bank1);
123 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
124
125 if (rc)
126 pr_err("FAIL pm8xxx_writeb(): rc=%d. "
127 "(gpio=%d, value=%d)\n",
128 rc, gpio, value);
129
130 return rc;
131}
132
133static int dir_map[] = {
134 PM_GPIO_MODE_OFF,
135 PM_GPIO_MODE_OUTPUT,
136 PM_GPIO_MODE_INPUT,
137 PM_GPIO_MODE_BOTH,
138};
139
140static int pm_gpio_set_direction(struct pm_gpio_chip *pm_gpio_chip,
141 unsigned gpio, int direction)
142{
143 int rc;
144 u8 bank1;
145 unsigned long flags;
146
147 if (!direction || pm_gpio_chip == NULL)
148 return -EINVAL;
149
150 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
151 bank1 = PM_GPIO_WRITE
152 | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_MODE_MASK);
153
154 bank1 |= ((dir_map[direction] << PM_GPIO_MODE_SHIFT)
155 & PM_GPIO_MODE_MASK);
156
157 pm_gpio_chip->bank1[gpio] = bank1;
158 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
159 SSBI_REG_ADDR_GPIO(gpio), bank1);
160 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
161
162 if (rc)
163 pr_err("Failed on pm8xxx_writeb(): rc=%d (GPIO config)\n",
164 rc);
165
166 return rc;
167}
168
169static int pm_gpio_init_bank1(struct pm_gpio_chip *pm_gpio_chip)
170{
171 int i, rc;
172 u8 bank;
173
174 for (i = 0; i < pm_gpio_chip->gpio_chip.ngpio; i++) {
175 bank = 1 << PM_GPIO_BANK_SHIFT;
176 rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
177 SSBI_REG_ADDR_GPIO(i),
178 bank);
179 if (rc) {
180 pr_err("error setting bank rc=%d\n", rc);
181 return rc;
182 }
183
184 rc = pm8xxx_readb(pm_gpio_chip->gpio_chip.dev->parent,
185 SSBI_REG_ADDR_GPIO(i),
186 &pm_gpio_chip->bank1[i]);
187 if (rc) {
188 pr_err("error reading bank 1 rc=%d\n", rc);
189 return rc;
190 }
191 }
192 return 0;
193}
194
195static int pm_gpio_to_irq(struct gpio_chip *gpio_chip, unsigned offset)
196{
197 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
198
199 return pm_gpio_chip->irq_base + offset;
200}
201
202static int pm_gpio_read(struct gpio_chip *gpio_chip, unsigned offset)
203{
204 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
205
206 return pm_gpio_get(pm_gpio_chip, offset);
207}
208
209static void pm_gpio_write(struct gpio_chip *gpio_chip,
210 unsigned offset, int val)
211{
212 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
213
214 pm_gpio_set(pm_gpio_chip, offset, val);
215}
216
217static int pm_gpio_direction_input(struct gpio_chip *gpio_chip,
218 unsigned offset)
219{
220 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
221
222 return pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_IN);
223}
224
225static int pm_gpio_direction_output(struct gpio_chip *gpio_chip,
226 unsigned offset,
227 int val)
228{
229 int ret;
230 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
231
232 ret = pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_OUT);
233 if (!ret)
234 ret = pm_gpio_set(pm_gpio_chip, offset, val);
235
236 return ret;
237}
238
239static void pm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gpio_chip)
240{
241 static const char * const cmode[] = { "in", "in/out", "out", "off" };
242 struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
243 u8 mode, state, bank;
244 const char *label;
245 int i, j;
246
247 for (i = 0; i < gpio_chip->ngpio; i++) {
248 label = gpiochip_is_requested(gpio_chip, i);
249 mode = (pm_gpio_chip->bank1[i] & PM_GPIO_MODE_MASK) >>
250 PM_GPIO_MODE_SHIFT;
251 state = pm_gpio_get(pm_gpio_chip, i);
252 seq_printf(s, "gpio-%-3d (%-12.12s) %-10.10s"
253 " %s",
254 gpio_chip->base + i,
255 label ? label : "--",
256 cmode[mode],
257 state ? "hi" : "lo");
258 for (j = 0; j < PM_GPIO_BANKS; j++) {
259 bank = j << PM_GPIO_BANK_SHIFT;
260 pm8xxx_writeb(gpio_chip->dev->parent,
261 SSBI_REG_ADDR_GPIO(i),
262 bank);
263 pm8xxx_readb(gpio_chip->dev->parent,
264 SSBI_REG_ADDR_GPIO(i),
265 &bank);
266 seq_printf(s, " 0x%02x", bank);
267 }
268 seq_printf(s, "\n");
269 }
270}
271
272static int __devinit pm_gpio_probe(struct platform_device *pdev)
273{
274 int ret;
275 const struct pm8xxx_gpio_platform_data *pdata = pdev->dev.platform_data;
276 struct pm_gpio_chip *pm_gpio_chip;
277
278 if (!pdata) {
279 pr_err("missing platform data\n");
280 return -EINVAL;
281 }
282
283 pm_gpio_chip = kzalloc(sizeof(struct pm_gpio_chip), GFP_KERNEL);
284 if (!pm_gpio_chip) {
285 pr_err("Cannot allocate pm_gpio_chip\n");
286 return -ENOMEM;
287 }
288
289 pm_gpio_chip->bank1 = kzalloc(sizeof(u8) * pdata->gpio_cdata.ngpios,
290 GFP_KERNEL);
291 if (!pm_gpio_chip->bank1) {
292 pr_err("Cannot allocate pm_gpio_chip->bank1\n");
Jay Chokshifc9f9332011-10-27 15:45:10 -0700293 ret = -ENOMEM;
294 goto free_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 }
296
297 spin_lock_init(&pm_gpio_chip->pm_lock);
298 pm_gpio_chip->gpio_chip.label = "pm-gpio";
299 pm_gpio_chip->gpio_chip.direction_input = pm_gpio_direction_input;
300 pm_gpio_chip->gpio_chip.direction_output = pm_gpio_direction_output;
301 pm_gpio_chip->gpio_chip.to_irq = pm_gpio_to_irq;
302 pm_gpio_chip->gpio_chip.get = pm_gpio_read;
303 pm_gpio_chip->gpio_chip.set = pm_gpio_write;
304 pm_gpio_chip->gpio_chip.dbg_show = pm_gpio_dbg_show;
305 pm_gpio_chip->gpio_chip.ngpio = pdata->gpio_cdata.ngpios;
David Collinsc010fa52011-12-14 14:10:49 -0800306 pm_gpio_chip->gpio_chip.can_sleep = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 pm_gpio_chip->gpio_chip.dev = &pdev->dev;
308 pm_gpio_chip->gpio_chip.base = pdata->gpio_base;
309 pm_gpio_chip->irq_base = platform_get_irq(pdev, 0);
310 mutex_lock(&pm_gpio_chips_lock);
311 list_add(&pm_gpio_chip->link, &pm_gpio_chips);
312 mutex_unlock(&pm_gpio_chips_lock);
313 platform_set_drvdata(pdev, pm_gpio_chip);
314
315 ret = gpiochip_add(&pm_gpio_chip->gpio_chip);
316 if (ret) {
317 pr_err("gpiochip_add failed ret = %d\n", ret);
318 goto reset_drvdata;
319 }
320
321 ret = pm_gpio_init_bank1(pm_gpio_chip);
322 if (ret) {
323 pr_err("gpio init bank failed ret = %d\n", ret);
324 goto remove_chip;
325 }
326
327 pr_info("OK: base=%d, ngpio=%d\n", pm_gpio_chip->gpio_chip.base,
328 pm_gpio_chip->gpio_chip.ngpio);
329
330 return 0;
331
332remove_chip:
333 if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
334 pr_err("failed to remove gpio chip\n");
335reset_drvdata:
336 platform_set_drvdata(pdev, NULL);
Jay Chokshifc9f9332011-10-27 15:45:10 -0700337 kfree(pm_gpio_chip->bank1);
338free_chip:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 kfree(pm_gpio_chip);
340 return ret;
341}
342
343static int __devexit pm_gpio_remove(struct platform_device *pdev)
344{
345 struct pm_gpio_chip *pm_gpio_chip
346 = platform_get_drvdata(pdev);
347
348 mutex_lock(&pm_gpio_chips_lock);
349 list_del(&pm_gpio_chip->link);
350 mutex_unlock(&pm_gpio_chips_lock);
351 platform_set_drvdata(pdev, NULL);
352 if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
353 pr_err("failed to remove gpio chip\n");
354 kfree(pm_gpio_chip->bank1);
355 kfree(pm_gpio_chip);
356 return 0;
357}
358
359int pm8xxx_gpio_config(int gpio, struct pm_gpio *param)
360{
361 int rc, pm_gpio = -EINVAL;
362 u8 bank[8];
363 unsigned long flags;
364 struct pm_gpio_chip *pm_gpio_chip;
365 struct gpio_chip *gpio_chip;
366
367 if (param == NULL)
368 return -EINVAL;
369
370 mutex_lock(&pm_gpio_chips_lock);
371 list_for_each_entry(pm_gpio_chip, &pm_gpio_chips, link) {
372 gpio_chip = &pm_gpio_chip->gpio_chip;
373 if (gpio >= gpio_chip->base
374 && gpio < gpio_chip->base + gpio_chip->ngpio) {
375 pm_gpio = gpio - gpio_chip->base;
376 break;
377 }
378 }
379 mutex_unlock(&pm_gpio_chips_lock);
380 if (pm_gpio < 0) {
381 pr_err("called on gpio %d not handled by any pmic\n", gpio);
382 return -EINVAL;
383 }
384
385 /* Select banks and configure the gpio */
386 bank[0] = PM_GPIO_WRITE |
387 ((param->vin_sel << PM_GPIO_VIN_SHIFT) &
388 PM_GPIO_VIN_MASK) |
389 PM_GPIO_MODE_ENABLE;
390 bank[1] = PM_GPIO_WRITE |
391 ((1 << PM_GPIO_BANK_SHIFT) &
392 PM_GPIO_BANK_MASK) |
393 ((dir_map[param->direction] <<
394 PM_GPIO_MODE_SHIFT) &
395 PM_GPIO_MODE_MASK) |
396 ((param->direction & PM_GPIO_DIR_OUT) ?
397 ((param->output_buffer & 1) ?
398 PM_GPIO_OUT_BUFFER : 0) : 0) |
399 ((param->direction & PM_GPIO_DIR_OUT) ?
400 param->output_value & 0x01 : 0);
401 bank[2] = PM_GPIO_WRITE |
402 ((2 << PM_GPIO_BANK_SHIFT) &
403 PM_GPIO_BANK_MASK) |
404 ((param->pull << PM_GPIO_PULL_SHIFT) &
405 PM_GPIO_PULL_MASK);
406 bank[3] = PM_GPIO_WRITE |
407 ((3 << PM_GPIO_BANK_SHIFT) &
408 PM_GPIO_BANK_MASK) |
409 ((param->out_strength <<
410 PM_GPIO_OUT_STRENGTH_SHIFT) &
411 PM_GPIO_OUT_STRENGTH_MASK) |
412 (param->disable_pin ?
413 PM_GPIO_PIN_DISABLE : PM_GPIO_PIN_ENABLE);
414 bank[4] = PM_GPIO_WRITE |
415 ((4 << PM_GPIO_BANK_SHIFT) &
416 PM_GPIO_BANK_MASK) |
417 ((param->function << PM_GPIO_FUNC_SHIFT) &
418 PM_GPIO_FUNC_MASK);
419 bank[5] = PM_GPIO_WRITE |
420 ((5 << PM_GPIO_BANK_SHIFT) & PM_GPIO_BANK_MASK) |
421 (param->inv_int_pol ? 0 : PM_GPIO_NON_INT_POL_INV);
422
423 spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
424 /* Remember bank1 for later use */
425 pm_gpio_chip->bank1[pm_gpio] = bank[1];
426 rc = pm8xxx_write_buf(pm_gpio_chip->gpio_chip.dev->parent,
427 SSBI_REG_ADDR_GPIO(pm_gpio), bank, 6);
428 spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
429
430 if (rc)
431 pr_err("Failed on pm8xxx_write_buf() rc=%d (GPIO config)\n",
432 rc);
433
434 return rc;
435}
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530436EXPORT_SYMBOL(pm8xxx_gpio_config);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437
438static struct platform_driver pm_gpio_driver = {
439 .probe = pm_gpio_probe,
440 .remove = __devexit_p(pm_gpio_remove),
441 .driver = {
442 .name = PM8XXX_GPIO_DEV_NAME,
443 .owner = THIS_MODULE,
444 },
445};
446
447static int __init pm_gpio_init(void)
448{
449 return platform_driver_register(&pm_gpio_driver);
450}
451postcore_initcall(pm_gpio_init);
452
453static void __exit pm_gpio_exit(void)
454{
455 platform_driver_unregister(&pm_gpio_driver);
456}
457module_exit(pm_gpio_exit);
458
459MODULE_LICENSE("GPL v2");
460MODULE_DESCRIPTION("PMIC GPIO driver");
461MODULE_VERSION("1.0");
462MODULE_ALIAS("platform:" PM8XXX_GPIO_DEV_NAME);