1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI/National Semiconductor LP3943 GPIO driver
5 * Copyright 2013 Texas Instruments
7 * Author: Milo Kim <milo.kim@ti.com>
10 #include <linux/bitops.h>
11 #include <linux/err.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/mfd/lp3943.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
40 struct gpio_chip chip
;
41 struct lp3943
*lp3943
;
42 u16 input_mask
; /* 1 = GPIO is input direction, 0 = output */
45 static int lp3943_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
47 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
48 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
50 /* Return an error if the pin is already assigned */
51 if (test_and_set_bit(offset
, &lp3943
->pin_used
))
57 static void lp3943_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
59 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
60 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
62 clear_bit(offset
, &lp3943
->pin_used
);
65 static int lp3943_gpio_set_mode(struct lp3943_gpio
*lp3943_gpio
, u8 offset
,
68 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
69 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
71 return lp3943_update_bits(lp3943
, mux
[offset
].reg
, mux
[offset
].mask
,
72 val
<< mux
[offset
].shift
);
75 static int lp3943_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
77 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
79 lp3943_gpio
->input_mask
|= BIT(offset
);
81 return lp3943_gpio_set_mode(lp3943_gpio
, offset
, LP3943_GPIO_IN
);
84 static int lp3943_get_gpio_in_status(struct lp3943_gpio
*lp3943_gpio
,
85 struct gpio_chip
*chip
, unsigned offset
)
91 case LP3943_GPIO1
... LP3943_GPIO8
:
92 addr
= LP3943_REG_GPIO_A
;
94 case LP3943_GPIO9
... LP3943_GPIO16
:
95 addr
= LP3943_REG_GPIO_B
;
102 err
= lp3943_read_byte(lp3943_gpio
->lp3943
, addr
, &read
);
106 return !!(read
& BIT(offset
));
109 static int lp3943_get_gpio_out_status(struct lp3943_gpio
*lp3943_gpio
,
110 struct gpio_chip
*chip
, unsigned offset
)
112 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
113 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
117 err
= lp3943_read_byte(lp3943
, mux
[offset
].reg
, &read
);
121 read
= (read
& mux
[offset
].mask
) >> mux
[offset
].shift
;
123 if (read
== LP3943_GPIO_OUT_HIGH
)
125 else if (read
== LP3943_GPIO_OUT_LOW
)
131 static int lp3943_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
133 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
137 * LP3943 doesn't have the GPIO direction register. It provides
138 * only input and output status registers.
139 * So, direction info is required to handle the 'get' operation.
140 * This variable is updated whenever the direction is changed and
144 if (lp3943_gpio
->input_mask
& BIT(offset
))
145 return lp3943_get_gpio_in_status(lp3943_gpio
, chip
, offset
);
147 return lp3943_get_gpio_out_status(lp3943_gpio
, chip
, offset
);
150 static void lp3943_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
152 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
156 data
= LP3943_GPIO_OUT_HIGH
;
158 data
= LP3943_GPIO_OUT_LOW
;
160 lp3943_gpio_set_mode(lp3943_gpio
, offset
, data
);
163 static int lp3943_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
166 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
168 lp3943_gpio_set(chip
, offset
, value
);
169 lp3943_gpio
->input_mask
&= ~BIT(offset
);
174 static const struct gpio_chip lp3943_gpio_chip
= {
176 .owner
= THIS_MODULE
,
177 .request
= lp3943_gpio_request
,
178 .free
= lp3943_gpio_free
,
179 .direction_input
= lp3943_gpio_direction_input
,
180 .get
= lp3943_gpio_get
,
181 .direction_output
= lp3943_gpio_direction_output
,
182 .set
= lp3943_gpio_set
,
184 .ngpio
= LP3943_MAX_GPIO
,
188 static int lp3943_gpio_probe(struct platform_device
*pdev
)
190 struct lp3943
*lp3943
= dev_get_drvdata(pdev
->dev
.parent
);
191 struct lp3943_gpio
*lp3943_gpio
;
193 lp3943_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*lp3943_gpio
),
198 lp3943_gpio
->lp3943
= lp3943
;
199 lp3943_gpio
->chip
= lp3943_gpio_chip
;
200 lp3943_gpio
->chip
.parent
= &pdev
->dev
;
202 platform_set_drvdata(pdev
, lp3943_gpio
);
204 return devm_gpiochip_add_data(&pdev
->dev
, &lp3943_gpio
->chip
,
208 static const struct of_device_id lp3943_gpio_of_match
[] = {
209 { .compatible
= "ti,lp3943-gpio", },
212 MODULE_DEVICE_TABLE(of
, lp3943_gpio_of_match
);
214 static struct platform_driver lp3943_gpio_driver
= {
215 .probe
= lp3943_gpio_probe
,
217 .name
= "lp3943-gpio",
218 .of_match_table
= lp3943_gpio_of_match
,
221 module_platform_driver(lp3943_gpio_driver
);
223 MODULE_DESCRIPTION("LP3943 GPIO driver");
224 MODULE_ALIAS("platform:lp3943-gpio");
225 MODULE_AUTHOR("Milo Kim");
226 MODULE_LICENSE("GPL");