blob: 9cdbc0c9cb2da87abc65dda442fc75a1612cc51e [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
Nicolas Saenz Julienne2f97c202015-02-19 01:52:25 +000029#define to_tgd(gc) container_of(gc, struct tps65912_gpio_data, gpio_chip)
30
Margarita Olaya668a6cc2011-06-09 14:50:19 -050031static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
32{
Nicolas Saenz Julienne2f97c202015-02-19 01:52:25 +000033 struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
34 struct tps65912 *tps65912 = tps65912_gpio->tps65912;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050035 int val;
36
37 val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset);
38
39 if (val & GPIO_STS_MASK)
40 return 1;
41
42 return 0;
43}
44
45static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
46 int value)
47{
Nicolas Saenz Julienne2f97c202015-02-19 01:52:25 +000048 struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
49 struct tps65912 *tps65912 = tps65912_gpio->tps65912;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050050
51 if (value)
52 tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
53 GPIO_SET_MASK);
54 else
55 tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
56 GPIO_SET_MASK);
57}
58
59static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset,
60 int value)
61{
Nicolas Saenz Julienne2f97c202015-02-19 01:52:25 +000062 struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
63 struct tps65912 *tps65912 = tps65912_gpio->tps65912;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050064
65 /* Set the initial value */
66 tps65912_gpio_set(gc, offset, value);
67
68 return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
69 GPIO_CFG_MASK);
70}
71
72static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset)
73{
Nicolas Saenz Julienne2f97c202015-02-19 01:52:25 +000074 struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
75 struct tps65912 *tps65912 = tps65912_gpio->tps65912;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050076
77 return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
78 GPIO_CFG_MASK);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050079}
80
81static struct gpio_chip template_chip = {
82 .label = "tps65912",
83 .owner = THIS_MODULE,
84 .direction_input = tps65912_gpio_input,
85 .direction_output = tps65912_gpio_output,
86 .get = tps65912_gpio_get,
87 .set = tps65912_gpio_set,
Linus Walleij9fb1f392013-12-04 14:42:46 +010088 .can_sleep = true,
Margarita Olaya668a6cc2011-06-09 14:50:19 -050089 .ngpio = 5,
90 .base = -1,
91};
92
Bill Pemberton38363092012-11-19 13:22:34 -050093static int tps65912_gpio_probe(struct platform_device *pdev)
Margarita Olaya668a6cc2011-06-09 14:50:19 -050094{
95 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
Jingoo Hane56aee12013-07-30 17:08:05 +090096 struct tps65912_board *pdata = dev_get_platdata(tps65912->dev);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050097 struct tps65912_gpio_data *tps65912_gpio;
98 int ret;
99
Axel Lin45e253a2012-09-01 17:44:27 +0800100 tps65912_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65912_gpio),
101 GFP_KERNEL);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500102 if (tps65912_gpio == NULL)
103 return -ENOMEM;
104
105 tps65912_gpio->tps65912 = tps65912;
106 tps65912_gpio->gpio_chip = template_chip;
107 tps65912_gpio->gpio_chip.dev = &pdev->dev;
108 if (pdata && pdata->gpio_base)
109 tps65912_gpio->gpio_chip.base = pdata->gpio_base;
110
111 ret = gpiochip_add(&tps65912_gpio->gpio_chip);
112 if (ret < 0) {
113 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
Axel Lin45e253a2012-09-01 17:44:27 +0800114 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500115 }
116
117 platform_set_drvdata(pdev, tps65912_gpio);
118
119 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500120}
121
Bill Pemberton206210c2012-11-19 13:25:50 -0500122static int tps65912_gpio_remove(struct platform_device *pdev)
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500123{
124 struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500125
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200126 gpiochip_remove(&tps65912_gpio->gpio_chip);
127 return 0;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500128}
129
130static struct platform_driver tps65912_gpio_driver = {
131 .driver = {
132 .name = "tps65912-gpio",
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500133 },
134 .probe = tps65912_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500135 .remove = tps65912_gpio_remove,
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500136};
137
138static int __init tps65912_gpio_init(void)
139{
140 return platform_driver_register(&tps65912_gpio_driver);
141}
142subsys_initcall(tps65912_gpio_init);
143
144static void __exit tps65912_gpio_exit(void)
145{
146 platform_driver_unregister(&tps65912_gpio_driver);
147}
148module_exit(tps65912_gpio_exit);
149
150MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
151MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
152MODULE_LICENSE("GPL v2");
153MODULE_ALIAS("platform:tps65912-gpio");