2 * TI/National Semiconductor LP3943 GPIO driver
4 * Copyright 2013 Texas Instruments
6 * Author: Milo Kim <milo.kim@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2.
13 #include <linux/bitops.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/mfd/lp3943.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
43 struct gpio_chip chip
;
44 struct lp3943
*lp3943
;
45 u16 input_mask
; /* 1 = GPIO is input direction, 0 = output */
48 static inline struct lp3943_gpio
*to_lp3943_gpio(struct gpio_chip
*_chip
)
50 return container_of(_chip
, struct lp3943_gpio
, chip
);
53 static int lp3943_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
55 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
56 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
58 /* Return an error if the pin is already assigned */
59 if (test_and_set_bit(offset
, &lp3943
->pin_used
))
65 static void lp3943_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
67 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
68 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
70 clear_bit(offset
, &lp3943
->pin_used
);
73 static int lp3943_gpio_set_mode(struct lp3943_gpio
*lp3943_gpio
, u8 offset
,
76 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
77 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
79 return lp3943_update_bits(lp3943
, mux
[offset
].reg
, mux
[offset
].mask
,
80 val
<< mux
[offset
].shift
);
83 static int lp3943_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
85 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
87 lp3943_gpio
->input_mask
|= BIT(offset
);
89 return lp3943_gpio_set_mode(lp3943_gpio
, offset
, LP3943_GPIO_IN
);
92 static int lp3943_get_gpio_in_status(struct lp3943_gpio
*lp3943_gpio
,
93 struct gpio_chip
*chip
, unsigned offset
)
99 case LP3943_GPIO1
... LP3943_GPIO8
:
100 addr
= LP3943_REG_GPIO_A
;
102 case LP3943_GPIO9
... LP3943_GPIO16
:
103 addr
= LP3943_REG_GPIO_B
;
110 err
= lp3943_read_byte(lp3943_gpio
->lp3943
, addr
, &read
);
114 return !!(read
& BIT(offset
));
117 static int lp3943_get_gpio_out_status(struct lp3943_gpio
*lp3943_gpio
,
118 struct gpio_chip
*chip
, unsigned offset
)
120 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
121 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
125 err
= lp3943_read_byte(lp3943
, mux
[offset
].reg
, &read
);
129 read
= (read
& mux
[offset
].mask
) >> mux
[offset
].shift
;
131 if (read
== LP3943_GPIO_OUT_HIGH
)
133 else if (read
== LP3943_GPIO_OUT_LOW
)
139 static int lp3943_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
141 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
145 * LP3943 doesn't have the GPIO direction register. It provides
146 * only input and output status registers.
147 * So, direction info is required to handle the 'get' operation.
148 * This variable is updated whenever the direction is changed and
152 if (lp3943_gpio
->input_mask
& BIT(offset
))
153 return lp3943_get_gpio_in_status(lp3943_gpio
, chip
, offset
);
155 return lp3943_get_gpio_out_status(lp3943_gpio
, chip
, offset
);
158 static void lp3943_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
160 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
164 data
= LP3943_GPIO_OUT_HIGH
;
166 data
= LP3943_GPIO_OUT_LOW
;
168 lp3943_gpio_set_mode(lp3943_gpio
, offset
, data
);
171 static int lp3943_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
174 struct lp3943_gpio
*lp3943_gpio
= to_lp3943_gpio(chip
);
176 lp3943_gpio_set(chip
, offset
, value
);
177 lp3943_gpio
->input_mask
&= ~BIT(offset
);
182 static const struct gpio_chip lp3943_gpio_chip
= {
184 .owner
= THIS_MODULE
,
185 .request
= lp3943_gpio_request
,
186 .free
= lp3943_gpio_free
,
187 .direction_input
= lp3943_gpio_direction_input
,
188 .get
= lp3943_gpio_get
,
189 .direction_output
= lp3943_gpio_direction_output
,
190 .set
= lp3943_gpio_set
,
192 .ngpio
= LP3943_MAX_GPIO
,
196 static int lp3943_gpio_probe(struct platform_device
*pdev
)
198 struct lp3943
*lp3943
= dev_get_drvdata(pdev
->dev
.parent
);
199 struct lp3943_gpio
*lp3943_gpio
;
201 lp3943_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*lp3943_gpio
),
206 lp3943_gpio
->lp3943
= lp3943
;
207 lp3943_gpio
->chip
= lp3943_gpio_chip
;
208 lp3943_gpio
->chip
.dev
= &pdev
->dev
;
210 platform_set_drvdata(pdev
, lp3943_gpio
);
212 return gpiochip_add(&lp3943_gpio
->chip
);
215 static int lp3943_gpio_remove(struct platform_device
*pdev
)
217 struct lp3943_gpio
*lp3943_gpio
= platform_get_drvdata(pdev
);
219 return gpiochip_remove(&lp3943_gpio
->chip
);
222 static const struct of_device_id lp3943_gpio_of_match
[] = {
223 { .compatible
= "ti,lp3943-gpio", },
226 MODULE_DEVICE_TABLE(of
, lp3943_gpio_of_match
);
228 static struct platform_driver lp3943_gpio_driver
= {
229 .probe
= lp3943_gpio_probe
,
230 .remove
= lp3943_gpio_remove
,
232 .name
= "lp3943-gpio",
233 .owner
= THIS_MODULE
,
234 .of_match_table
= lp3943_gpio_of_match
,
237 module_platform_driver(lp3943_gpio_driver
);
239 MODULE_DESCRIPTION("LP3943 GPIO driver");
240 MODULE_ALIAS("platform:lp3943-gpio");
241 MODULE_AUTHOR("Milo Kim");
242 MODULE_LICENSE("GPL");