Merge tag 'pm+acpi-4.6-rc1-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / drivers / gpio / gpio-tps65912.c
CommitLineData
668a6cc7 1/*
ca801a22 2 * GPIO driver for TI TPS65912x PMICs
668a6cc7 3 *
ca801a22
AD
4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
5 * Andrew F. Davis <afd@ti.com>
668a6cc7 6 *
ca801a22
AD
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
668a6cc7 10 *
ca801a22
AD
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether expressed or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License version 2 for more details.
15 *
16 * Based on the Arizona GPIO driver and the previous TPS65912 driver by
17 * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
668a6cc7
MO
18 */
19
668a6cc7 20#include <linux/gpio.h>
ca801a22 21#include <linux/module.h>
668a6cc7 22#include <linux/platform_device.h>
ca801a22 23
668a6cc7
MO
24#include <linux/mfd/tps65912.h>
25
ca801a22 26struct tps65912_gpio {
668a6cc7 27 struct gpio_chip gpio_chip;
ca801a22 28 struct tps65912 *tps;
668a6cc7
MO
29};
30
ca801a22
AD
31static int tps65912_gpio_get_direction(struct gpio_chip *gc,
32 unsigned offset)
668a6cc7 33{
ca801a22 34 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
668a6cc7 35
ca801a22 36 int ret, val;
668a6cc7 37
ca801a22
AD
38 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
39 if (ret)
40 return ret;
668a6cc7 41
ca801a22
AD
42 if (val & GPIO_CFG_MASK)
43 return GPIOF_DIR_OUT;
44 else
45 return GPIOF_DIR_IN;
668a6cc7
MO
46}
47
ca801a22 48static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
668a6cc7 49{
ca801a22 50 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
668a6cc7 51
ca801a22
AD
52 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
53 GPIO_CFG_MASK, 0);
668a6cc7
MO
54}
55
ca801a22
AD
56static int tps65912_gpio_direction_output(struct gpio_chip *gc,
57 unsigned offset, int value)
668a6cc7 58{
ca801a22 59 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
668a6cc7
MO
60
61 /* Set the initial value */
ca801a22
AD
62 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
63 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
668a6cc7 64
ca801a22
AD
65 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
66 GPIO_CFG_MASK, GPIO_CFG_MASK);
668a6cc7
MO
67}
68
ca801a22 69static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
668a6cc7 70{
ca801a22
AD
71 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
72 int ret, val;
73
74 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
75 if (ret)
76 return ret;
668a6cc7 77
ca801a22
AD
78 if (val & GPIO_STS_MASK)
79 return 1;
80
81 return 0;
82}
83
84static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
85 int value)
86{
87 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
88
89 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
90 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
668a6cc7
MO
91}
92
93static struct gpio_chip template_chip = {
ca801a22 94 .label = "tps65912-gpio",
668a6cc7 95 .owner = THIS_MODULE,
ca801a22
AD
96 .get_direction = tps65912_gpio_get_direction,
97 .direction_input = tps65912_gpio_direction_input,
98 .direction_output = tps65912_gpio_direction_output,
668a6cc7
MO
99 .get = tps65912_gpio_get,
100 .set = tps65912_gpio_set,
668a6cc7 101 .base = -1,
ca801a22
AD
102 .ngpio = 5,
103 .can_sleep = true,
668a6cc7
MO
104};
105
3836309d 106static int tps65912_gpio_probe(struct platform_device *pdev)
668a6cc7 107{
ca801a22
AD
108 struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
109 struct tps65912_gpio *gpio;
668a6cc7
MO
110 int ret;
111
ca801a22
AD
112 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
113 if (!gpio)
668a6cc7
MO
114 return -ENOMEM;
115
ca801a22
AD
116 gpio->tps = dev_get_drvdata(pdev->dev.parent);
117 gpio->gpio_chip = template_chip;
118 gpio->gpio_chip.parent = tps->dev;
668a6cc7 119
9d93efe3
LW
120 ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip,
121 gpio);
668a6cc7 122 if (ret < 0) {
ca801a22 123 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
45e253a7 124 return ret;
668a6cc7
MO
125 }
126
ca801a22 127 platform_set_drvdata(pdev, gpio);
668a6cc7 128
ca801a22 129 return 0;
668a6cc7
MO
130}
131
ca801a22
AD
132static const struct platform_device_id tps65912_gpio_id_table[] = {
133 { "tps65912-gpio", },
134 { /* sentinel */ }
135};
136MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
137
668a6cc7
MO
138static struct platform_driver tps65912_gpio_driver = {
139 .driver = {
140 .name = "tps65912-gpio",
668a6cc7
MO
141 },
142 .probe = tps65912_gpio_probe,
ca801a22 143 .id_table = tps65912_gpio_id_table,
668a6cc7 144};
ca801a22 145module_platform_driver(tps65912_gpio_driver);
668a6cc7 146
ca801a22
AD
147MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
148MODULE_DESCRIPTION("TPS65912 GPIO driver");
668a6cc7 149MODULE_LICENSE("GPL v2");