blob: 276a4229b032583379dd1490716dd98565c6bca3 [file] [log] [blame]
Margarita Olaya668a6cc2011-06-09 14:50:19 -05001/*
2 * Copyright 2011 Texas Instruments Inc.
3 *
4 * Author: Margarita Olaya <magi@slimlogic.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This driver is based on wm8350 implementation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/gpio.h>
18#include <linux/mfd/core.h>
19#include <linux/platform_device.h>
20#include <linux/seq_file.h>
21#include <linux/slab.h>
22#include <linux/mfd/tps65912.h>
23
24struct tps65912_gpio_data {
25 struct tps65912 *tps65912;
26 struct gpio_chip gpio_chip;
27};
28
29static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
30{
31 struct tps65912 *tps65912 = container_of(gc, struct tps65912, gpio);
32 int val;
33
34 val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset);
35
36 if (val & GPIO_STS_MASK)
37 return 1;
38
39 return 0;
40}
41
42static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
43 int value)
44{
45 struct tps65912 *tps65912 = container_of(gc, struct tps65912, gpio);
46
47 if (value)
48 tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
49 GPIO_SET_MASK);
50 else
51 tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
52 GPIO_SET_MASK);
53}
54
55static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset,
56 int value)
57{
58 struct tps65912 *tps65912 = container_of(gc, struct tps65912, gpio);
59
60 /* Set the initial value */
61 tps65912_gpio_set(gc, offset, value);
62
63 return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
64 GPIO_CFG_MASK);
65}
66
67static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset)
68{
69 struct tps65912 *tps65912 = container_of(gc, struct tps65912, gpio);
70
71 return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
72 GPIO_CFG_MASK);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050073}
74
75static struct gpio_chip template_chip = {
76 .label = "tps65912",
77 .owner = THIS_MODULE,
78 .direction_input = tps65912_gpio_input,
79 .direction_output = tps65912_gpio_output,
80 .get = tps65912_gpio_get,
81 .set = tps65912_gpio_set,
82 .can_sleep = 1,
83 .ngpio = 5,
84 .base = -1,
85};
86
Bill Pemberton38363092012-11-19 13:22:34 -050087static int tps65912_gpio_probe(struct platform_device *pdev)
Margarita Olaya668a6cc2011-06-09 14:50:19 -050088{
89 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
Jingoo Hane56aee12013-07-30 17:08:05 +090090 struct tps65912_board *pdata = dev_get_platdata(tps65912->dev);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050091 struct tps65912_gpio_data *tps65912_gpio;
92 int ret;
93
Axel Lin45e253a2012-09-01 17:44:27 +080094 tps65912_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65912_gpio),
95 GFP_KERNEL);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050096 if (tps65912_gpio == NULL)
97 return -ENOMEM;
98
99 tps65912_gpio->tps65912 = tps65912;
100 tps65912_gpio->gpio_chip = template_chip;
101 tps65912_gpio->gpio_chip.dev = &pdev->dev;
102 if (pdata && pdata->gpio_base)
103 tps65912_gpio->gpio_chip.base = pdata->gpio_base;
104
105 ret = gpiochip_add(&tps65912_gpio->gpio_chip);
106 if (ret < 0) {
107 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
Axel Lin45e253a2012-09-01 17:44:27 +0800108 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500109 }
110
111 platform_set_drvdata(pdev, tps65912_gpio);
112
113 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500114}
115
Bill Pemberton206210c2012-11-19 13:25:50 -0500116static int tps65912_gpio_remove(struct platform_device *pdev)
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500117{
118 struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500119
Axel Lin45e253a2012-09-01 17:44:27 +0800120 return gpiochip_remove(&tps65912_gpio->gpio_chip);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500121}
122
123static struct platform_driver tps65912_gpio_driver = {
124 .driver = {
125 .name = "tps65912-gpio",
126 .owner = THIS_MODULE,
127 },
128 .probe = tps65912_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500129 .remove = tps65912_gpio_remove,
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500130};
131
132static int __init tps65912_gpio_init(void)
133{
134 return platform_driver_register(&tps65912_gpio_driver);
135}
136subsys_initcall(tps65912_gpio_init);
137
138static void __exit tps65912_gpio_exit(void)
139{
140 platform_driver_unregister(&tps65912_gpio_driver);
141}
142module_exit(tps65912_gpio_exit);
143
144MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
145MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
146MODULE_LICENSE("GPL v2");
147MODULE_ALIAS("platform:tps65912-gpio");