Merge tag 'pm-5.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / drivers / gpio / gpio-bd9571mwv.c
CommitLineData
b9f71d14 1// SPDX-License-Identifier: GPL-2.0-only
93847930 2/*
2e35627e 3 * ROHM BD9571MWV-M and BD9574MWF-M GPIO driver
93847930
MV
4 *
5 * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
6 *
93847930
MV
7 * Based on the TPS65086 driver
8 *
9 * NOTE: Interrupts are not supported yet.
10 */
11
12#include <linux/gpio/driver.h>
2e35627e 13#include <linux/mfd/rohm-generic.h>
93847930
MV
14#include <linux/module.h>
15#include <linux/platform_device.h>
16
17#include <linux/mfd/bd9571mwv.h>
18
19struct bd9571mwv_gpio {
2d7af444 20 struct regmap *regmap;
93847930 21 struct gpio_chip chip;
93847930
MV
22};
23
24static int bd9571mwv_gpio_get_direction(struct gpio_chip *chip,
25 unsigned int offset)
26{
27 struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
28 int ret, val;
29
2d7af444 30 ret = regmap_read(gpio->regmap, BD9571MWV_GPIO_DIR, &val);
93847930
MV
31 if (ret < 0)
32 return ret;
e42615ec
MV
33 if (val & BIT(offset))
34 return GPIO_LINE_DIRECTION_IN;
93847930 35
e42615ec 36 return GPIO_LINE_DIRECTION_OUT;
93847930
MV
37}
38
39static int bd9571mwv_gpio_direction_input(struct gpio_chip *chip,
40 unsigned int offset)
41{
42 struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
43
2d7af444 44 regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_DIR, BIT(offset), 0);
93847930
MV
45
46 return 0;
47}
48
49static int bd9571mwv_gpio_direction_output(struct gpio_chip *chip,
50 unsigned int offset, int value)
51{
52 struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
53
54 /* Set the initial value */
2d7af444 55 regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_OUT,
93847930 56 BIT(offset), value ? BIT(offset) : 0);
2d7af444 57 regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_DIR,
93847930
MV
58 BIT(offset), BIT(offset));
59
60 return 0;
61}
62
63static int bd9571mwv_gpio_get(struct gpio_chip *chip, unsigned int offset)
64{
65 struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
66 int ret, val;
67
2d7af444 68 ret = regmap_read(gpio->regmap, BD9571MWV_GPIO_IN, &val);
93847930
MV
69 if (ret < 0)
70 return ret;
71
72 return val & BIT(offset);
73}
74
75static void bd9571mwv_gpio_set(struct gpio_chip *chip, unsigned int offset,
76 int value)
77{
78 struct bd9571mwv_gpio *gpio = gpiochip_get_data(chip);
79
2d7af444 80 regmap_update_bits(gpio->regmap, BD9571MWV_GPIO_OUT,
93847930
MV
81 BIT(offset), value ? BIT(offset) : 0);
82}
83
84static const struct gpio_chip template_chip = {
85 .label = "bd9571mwv-gpio",
86 .owner = THIS_MODULE,
87 .get_direction = bd9571mwv_gpio_get_direction,
88 .direction_input = bd9571mwv_gpio_direction_input,
89 .direction_output = bd9571mwv_gpio_direction_output,
90 .get = bd9571mwv_gpio_get,
91 .set = bd9571mwv_gpio_set,
92 .base = -1,
93 .ngpio = 2,
94 .can_sleep = true,
95};
96
97static int bd9571mwv_gpio_probe(struct platform_device *pdev)
98{
99 struct bd9571mwv_gpio *gpio;
100 int ret;
101
102 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
103 if (!gpio)
104 return -ENOMEM;
105
106 platform_set_drvdata(pdev, gpio);
107
2d7af444 108 gpio->regmap = dev_get_regmap(pdev->dev.parent, NULL);
93847930 109 gpio->chip = template_chip;
2d7af444 110 gpio->chip.parent = pdev->dev.parent;
93847930
MV
111
112 ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
113 if (ret < 0) {
114 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
115 return ret;
116 }
117
118 return 0;
119}
120
121static const struct platform_device_id bd9571mwv_gpio_id_table[] = {
2e35627e
YS
122 { "bd9571mwv-gpio", ROHM_CHIP_TYPE_BD9571 },
123 { "bd9574mwf-gpio", ROHM_CHIP_TYPE_BD9574 },
93847930
MV
124 { /* sentinel */ }
125};
126MODULE_DEVICE_TABLE(platform, bd9571mwv_gpio_id_table);
127
128static struct platform_driver bd9571mwv_gpio_driver = {
129 .driver = {
130 .name = "bd9571mwv-gpio",
131 },
132 .probe = bd9571mwv_gpio_probe,
133 .id_table = bd9571mwv_gpio_id_table,
134};
135module_platform_driver(bd9571mwv_gpio_driver);
136
137MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
138MODULE_DESCRIPTION("BD9571MWV GPIO driver");
139MODULE_LICENSE("GPL v2");