xref: /openbmc/linux/drivers/gpio/gpio-wcd934x.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019, Linaro Limited
3 
4 #include <linux/module.h>
5 #include <linux/gpio/driver.h>
6 #include <linux/regmap.h>
7 #include <linux/slab.h>
8 #include <linux/of_device.h>
9 
10 #define WCD_PIN_MASK(p) BIT(p - 1)
11 #define WCD_REG_DIR_CTL_OFFSET 0x42
12 #define WCD_REG_VAL_CTL_OFFSET 0x43
13 #define WCD934X_NPINS		5
14 
15 struct wcd_gpio_data {
16 	struct regmap *map;
17 	struct gpio_chip chip;
18 };
19 
20 static int wcd_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
21 {
22 	struct wcd_gpio_data *data = gpiochip_get_data(chip);
23 	unsigned int value;
24 	int ret;
25 
26 	ret = regmap_read(data->map, WCD_REG_DIR_CTL_OFFSET, &value);
27 	if (ret < 0)
28 		return ret;
29 
30 	if (value & WCD_PIN_MASK(pin))
31 		return GPIO_LINE_DIRECTION_OUT;
32 
33 	return GPIO_LINE_DIRECTION_IN;
34 }
35 
36 static int wcd_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
37 {
38 	struct wcd_gpio_data *data = gpiochip_get_data(chip);
39 
40 	return regmap_update_bits(data->map, WCD_REG_DIR_CTL_OFFSET,
41 				  WCD_PIN_MASK(pin), 0);
42 }
43 
44 static int wcd_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
45 				     int val)
46 {
47 	struct wcd_gpio_data *data = gpiochip_get_data(chip);
48 
49 	regmap_update_bits(data->map, WCD_REG_DIR_CTL_OFFSET,
50 			   WCD_PIN_MASK(pin), WCD_PIN_MASK(pin));
51 
52 	return regmap_update_bits(data->map, WCD_REG_VAL_CTL_OFFSET,
53 				  WCD_PIN_MASK(pin),
54 				  val ? WCD_PIN_MASK(pin) : 0);
55 }
56 
57 static int wcd_gpio_get(struct gpio_chip *chip, unsigned int pin)
58 {
59 	struct wcd_gpio_data *data = gpiochip_get_data(chip);
60 	unsigned int value;
61 
62 	regmap_read(data->map, WCD_REG_VAL_CTL_OFFSET, &value);
63 
64 	return !!(value & WCD_PIN_MASK(pin));
65 }
66 
67 static void wcd_gpio_set(struct gpio_chip *chip, unsigned int pin, int val)
68 {
69 	struct wcd_gpio_data *data = gpiochip_get_data(chip);
70 
71 	regmap_update_bits(data->map, WCD_REG_VAL_CTL_OFFSET,
72 			   WCD_PIN_MASK(pin), val ? WCD_PIN_MASK(pin) : 0);
73 }
74 
75 static int wcd_gpio_probe(struct platform_device *pdev)
76 {
77 	struct device *dev = &pdev->dev;
78 	struct wcd_gpio_data *data;
79 	struct gpio_chip *chip;
80 
81 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
82 	if (!data)
83 		return -ENOMEM;
84 
85 	data->map = dev_get_regmap(dev->parent, NULL);
86 	if (!data->map) {
87 		dev_err(dev, "%s: failed to get regmap\n", __func__);
88 		return  -EINVAL;
89 	}
90 
91 	chip = &data->chip;
92 	chip->direction_input  = wcd_gpio_direction_input;
93 	chip->direction_output = wcd_gpio_direction_output;
94 	chip->get_direction = wcd_gpio_get_direction;
95 	chip->get = wcd_gpio_get;
96 	chip->set = wcd_gpio_set;
97 	chip->parent = dev;
98 	chip->base = -1;
99 	chip->ngpio = WCD934X_NPINS;
100 	chip->label = dev_name(dev);
101 	chip->of_gpio_n_cells = 2;
102 	chip->can_sleep = false;
103 
104 	return devm_gpiochip_add_data(dev, chip, data);
105 }
106 
107 static const struct of_device_id wcd_gpio_of_match[] = {
108 	{ .compatible = "qcom,wcd9340-gpio" },
109 	{ .compatible = "qcom,wcd9341-gpio" },
110 	{ }
111 };
112 MODULE_DEVICE_TABLE(of, wcd_gpio_of_match);
113 
114 static struct platform_driver wcd_gpio_driver = {
115 	.driver = {
116 		   .name = "wcd934x-gpio",
117 		   .of_match_table = wcd_gpio_of_match,
118 	},
119 	.probe = wcd_gpio_probe,
120 };
121 
122 module_platform_driver(wcd_gpio_driver);
123 MODULE_DESCRIPTION("Qualcomm Technologies, Inc WCD GPIO control driver");
124 MODULE_LICENSE("GPL v2");
125