Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * GPIO driver for TI TPS65912x PMICs
4 *
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
7 *
8 * Based on the Arizona GPIO driver and the previous TPS65912 driver by
9 * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
10 */
11
12#include <linux/gpio/driver.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15
16#include <linux/mfd/tps65912.h>
17
18struct tps65912_gpio {
19 struct gpio_chip gpio_chip;
20 struct tps65912 *tps;
21};
22
23static int tps65912_gpio_get_direction(struct gpio_chip *gc,
24 unsigned offset)
25{
26 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
27
28 int ret, val;
29
30 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
31 if (ret)
32 return ret;
33
34 if (val & GPIO_CFG_MASK)
35 return GPIO_LINE_DIRECTION_OUT;
36 else
37 return GPIO_LINE_DIRECTION_IN;
38}
39
40static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
41{
42 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
43
44 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
45 GPIO_CFG_MASK, 0);
46}
47
48static int tps65912_gpio_direction_output(struct gpio_chip *gc,
49 unsigned offset, int value)
50{
51 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
52
53 /* Set the initial value */
54 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
55 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
56
57 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
58 GPIO_CFG_MASK, GPIO_CFG_MASK);
59}
60
61static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
62{
63 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
64 int ret, val;
65
66 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
67 if (ret)
68 return ret;
69
70 if (val & GPIO_STS_MASK)
71 return 1;
72
73 return 0;
74}
75
76static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
77 int value)
78{
79 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
80
81 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
82 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
83}
84
85static const struct gpio_chip template_chip = {
86 .label = "tps65912-gpio",
87 .owner = THIS_MODULE,
88 .get_direction = tps65912_gpio_get_direction,
89 .direction_input = tps65912_gpio_direction_input,
90 .direction_output = tps65912_gpio_direction_output,
91 .get = tps65912_gpio_get,
92 .set = tps65912_gpio_set,
93 .base = -1,
94 .ngpio = 5,
95 .can_sleep = true,
96};
97
98static int tps65912_gpio_probe(struct platform_device *pdev)
99{
100 struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
101 struct tps65912_gpio *gpio;
102
103 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
104 if (!gpio)
105 return -ENOMEM;
106
107 gpio->tps = dev_get_drvdata(pdev->dev.parent);
108 gpio->gpio_chip = template_chip;
109 gpio->gpio_chip.parent = tps->dev;
110
111 return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip, gpio);
112}
113
114static const struct platform_device_id tps65912_gpio_id_table[] = {
115 { "tps65912-gpio", },
116 { /* sentinel */ }
117};
118MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
119
120static struct platform_driver tps65912_gpio_driver = {
121 .driver = {
122 .name = "tps65912-gpio",
123 },
124 .probe = tps65912_gpio_probe,
125 .id_table = tps65912_gpio_id_table,
126};
127module_platform_driver(tps65912_gpio_driver);
128
129MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
130MODULE_DESCRIPTION("TPS65912 GPIO driver");
131MODULE_LICENSE("GPL v2");
1/*
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);
73
74}
75
76static struct gpio_chip template_chip = {
77 .label = "tps65912",
78 .owner = THIS_MODULE,
79 .direction_input = tps65912_gpio_input,
80 .direction_output = tps65912_gpio_output,
81 .get = tps65912_gpio_get,
82 .set = tps65912_gpio_set,
83 .can_sleep = 1,
84 .ngpio = 5,
85 .base = -1,
86};
87
88static int __devinit tps65912_gpio_probe(struct platform_device *pdev)
89{
90 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
91 struct tps65912_board *pdata = tps65912->dev->platform_data;
92 struct tps65912_gpio_data *tps65912_gpio;
93 int ret;
94
95 tps65912_gpio = kzalloc(sizeof(*tps65912_gpio), GFP_KERNEL);
96 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);
108 goto err;
109 }
110
111 platform_set_drvdata(pdev, tps65912_gpio);
112
113 return ret;
114
115err:
116 kfree(tps65912_gpio);
117 return ret;
118}
119
120static int __devexit tps65912_gpio_remove(struct platform_device *pdev)
121{
122 struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
123 int ret;
124
125 ret = gpiochip_remove(&tps65912_gpio->gpio_chip);
126 if (ret == 0)
127 kfree(tps65912_gpio);
128
129 return ret;
130}
131
132static struct platform_driver tps65912_gpio_driver = {
133 .driver = {
134 .name = "tps65912-gpio",
135 .owner = THIS_MODULE,
136 },
137 .probe = tps65912_gpio_probe,
138 .remove = __devexit_p(tps65912_gpio_remove),
139};
140
141static int __init tps65912_gpio_init(void)
142{
143 return platform_driver_register(&tps65912_gpio_driver);
144}
145subsys_initcall(tps65912_gpio_init);
146
147static void __exit tps65912_gpio_exit(void)
148{
149 platform_driver_unregister(&tps65912_gpio_driver);
150}
151module_exit(tps65912_gpio_exit);
152
153MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
154MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
155MODULE_LICENSE("GPL v2");
156MODULE_ALIAS("platform:tps65912-gpio");