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/driver.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 int lp3943_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
50 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
51 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
53 /* Return an error if the pin is already assigned */
54 if (test_and_set_bit(offset
, &lp3943
->pin_used
))
60 static void lp3943_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
62 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
63 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
65 clear_bit(offset
, &lp3943
->pin_used
);
68 static int lp3943_gpio_set_mode(struct lp3943_gpio
*lp3943_gpio
, u8 offset
,
71 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
72 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
74 return lp3943_update_bits(lp3943
, mux
[offset
].reg
, mux
[offset
].mask
,
75 val
<< mux
[offset
].shift
);
78 static int lp3943_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
80 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
82 lp3943_gpio
->input_mask
|= BIT(offset
);
84 return lp3943_gpio_set_mode(lp3943_gpio
, offset
, LP3943_GPIO_IN
);
87 static int lp3943_get_gpio_in_status(struct lp3943_gpio
*lp3943_gpio
,
88 struct gpio_chip
*chip
, unsigned offset
)
94 case LP3943_GPIO1
... LP3943_GPIO8
:
95 addr
= LP3943_REG_GPIO_A
;
97 case LP3943_GPIO9
... LP3943_GPIO16
:
98 addr
= LP3943_REG_GPIO_B
;
105 err
= lp3943_read_byte(lp3943_gpio
->lp3943
, addr
, &read
);
109 return !!(read
& BIT(offset
));
112 static int lp3943_get_gpio_out_status(struct lp3943_gpio
*lp3943_gpio
,
113 struct gpio_chip
*chip
, unsigned offset
)
115 struct lp3943
*lp3943
= lp3943_gpio
->lp3943
;
116 const struct lp3943_reg_cfg
*mux
= lp3943
->mux_cfg
;
120 err
= lp3943_read_byte(lp3943
, mux
[offset
].reg
, &read
);
124 read
= (read
& mux
[offset
].mask
) >> mux
[offset
].shift
;
126 if (read
== LP3943_GPIO_OUT_HIGH
)
128 else if (read
== LP3943_GPIO_OUT_LOW
)
134 static int lp3943_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
136 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
140 * LP3943 doesn't have the GPIO direction register. It provides
141 * only input and output status registers.
142 * So, direction info is required to handle the 'get' operation.
143 * This variable is updated whenever the direction is changed and
147 if (lp3943_gpio
->input_mask
& BIT(offset
))
148 return lp3943_get_gpio_in_status(lp3943_gpio
, chip
, offset
);
150 return lp3943_get_gpio_out_status(lp3943_gpio
, chip
, offset
);
153 static void lp3943_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
155 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
159 data
= LP3943_GPIO_OUT_HIGH
;
161 data
= LP3943_GPIO_OUT_LOW
;
163 lp3943_gpio_set_mode(lp3943_gpio
, offset
, data
);
166 static int lp3943_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
169 struct lp3943_gpio
*lp3943_gpio
= gpiochip_get_data(chip
);
171 lp3943_gpio_set(chip
, offset
, value
);
172 lp3943_gpio
->input_mask
&= ~BIT(offset
);
177 static const struct gpio_chip lp3943_gpio_chip
= {
179 .owner
= THIS_MODULE
,
180 .request
= lp3943_gpio_request
,
181 .free
= lp3943_gpio_free
,
182 .direction_input
= lp3943_gpio_direction_input
,
183 .get
= lp3943_gpio_get
,
184 .direction_output
= lp3943_gpio_direction_output
,
185 .set
= lp3943_gpio_set
,
187 .ngpio
= LP3943_MAX_GPIO
,
191 static int lp3943_gpio_probe(struct platform_device
*pdev
)
193 struct lp3943
*lp3943
= dev_get_drvdata(pdev
->dev
.parent
);
194 struct lp3943_gpio
*lp3943_gpio
;
196 lp3943_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*lp3943_gpio
),
201 lp3943_gpio
->lp3943
= lp3943
;
202 lp3943_gpio
->chip
= lp3943_gpio_chip
;
203 lp3943_gpio
->chip
.parent
= &pdev
->dev
;
205 platform_set_drvdata(pdev
, lp3943_gpio
);
207 return devm_gpiochip_add_data(&pdev
->dev
, &lp3943_gpio
->chip
,
211 static const struct of_device_id lp3943_gpio_of_match
[] = {
212 { .compatible
= "ti,lp3943-gpio", },
215 MODULE_DEVICE_TABLE(of
, lp3943_gpio_of_match
);
217 static struct platform_driver lp3943_gpio_driver
= {
218 .probe
= lp3943_gpio_probe
,
220 .name
= "lp3943-gpio",
221 .of_match_table
= lp3943_gpio_of_match
,
224 module_platform_driver(lp3943_gpio_driver
);
226 MODULE_DESCRIPTION("LP3943 GPIO driver");
227 MODULE_ALIAS("platform:lp3943-gpio");
228 MODULE_AUTHOR("Milo Kim");
229 MODULE_LICENSE("GPL");