aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/pca953x.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/pca953x.c')
-rw-r--r--drivers/gpio/pca953x.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/drivers/gpio/pca953x.c b/drivers/gpio/pca953x.c
index a261972f603d..b473429eee75 100644
--- a/drivers/gpio/pca953x.c
+++ b/drivers/gpio/pca953x.c
@@ -60,6 +60,7 @@ struct pca953x_chip {
60 unsigned gpio_start; 60 unsigned gpio_start;
61 uint16_t reg_output; 61 uint16_t reg_output;
62 uint16_t reg_direction; 62 uint16_t reg_direction;
63 struct mutex i2c_lock;
63 64
64#ifdef CONFIG_GPIO_PCA953X_IRQ 65#ifdef CONFIG_GPIO_PCA953X_IRQ
65 struct mutex irq_lock; 66 struct mutex irq_lock;
@@ -119,13 +120,17 @@ static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
119 120
120 chip = container_of(gc, struct pca953x_chip, gpio_chip); 121 chip = container_of(gc, struct pca953x_chip, gpio_chip);
121 122
123 mutex_lock(&chip->i2c_lock);
122 reg_val = chip->reg_direction | (1u << off); 124 reg_val = chip->reg_direction | (1u << off);
123 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); 125 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
124 if (ret) 126 if (ret)
125 return ret; 127 goto exit;
126 128
127 chip->reg_direction = reg_val; 129 chip->reg_direction = reg_val;
128 return 0; 130 ret = 0;
131exit:
132 mutex_unlock(&chip->i2c_lock);
133 return ret;
129} 134}
130 135
131static int pca953x_gpio_direction_output(struct gpio_chip *gc, 136static int pca953x_gpio_direction_output(struct gpio_chip *gc,
@@ -137,6 +142,7 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
137 142
138 chip = container_of(gc, struct pca953x_chip, gpio_chip); 143 chip = container_of(gc, struct pca953x_chip, gpio_chip);
139 144
145 mutex_lock(&chip->i2c_lock);
140 /* set output level */ 146 /* set output level */
141 if (val) 147 if (val)
142 reg_val = chip->reg_output | (1u << off); 148 reg_val = chip->reg_output | (1u << off);
@@ -145,7 +151,7 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
145 151
146 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); 152 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
147 if (ret) 153 if (ret)
148 return ret; 154 goto exit;
149 155
150 chip->reg_output = reg_val; 156 chip->reg_output = reg_val;
151 157
@@ -153,10 +159,13 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
153 reg_val = chip->reg_direction & ~(1u << off); 159 reg_val = chip->reg_direction & ~(1u << off);
154 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); 160 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
155 if (ret) 161 if (ret)
156 return ret; 162 goto exit;
157 163
158 chip->reg_direction = reg_val; 164 chip->reg_direction = reg_val;
159 return 0; 165 ret = 0;
166exit:
167 mutex_unlock(&chip->i2c_lock);
168 return ret;
160} 169}
161 170
162static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) 171static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
@@ -167,7 +176,9 @@ static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
167 176
168 chip = container_of(gc, struct pca953x_chip, gpio_chip); 177 chip = container_of(gc, struct pca953x_chip, gpio_chip);
169 178
179 mutex_lock(&chip->i2c_lock);
170 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val); 180 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
181 mutex_unlock(&chip->i2c_lock);
171 if (ret < 0) { 182 if (ret < 0) {
172 /* NOTE: diagnostic already emitted; that's all we should 183 /* NOTE: diagnostic already emitted; that's all we should
173 * do unless gpio_*_value_cansleep() calls become different 184 * do unless gpio_*_value_cansleep() calls become different
@@ -187,6 +198,7 @@ static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
187 198
188 chip = container_of(gc, struct pca953x_chip, gpio_chip); 199 chip = container_of(gc, struct pca953x_chip, gpio_chip);
189 200
201 mutex_lock(&chip->i2c_lock);
190 if (val) 202 if (val)
191 reg_val = chip->reg_output | (1u << off); 203 reg_val = chip->reg_output | (1u << off);
192 else 204 else
@@ -194,9 +206,11 @@ static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
194 206
195 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); 207 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
196 if (ret) 208 if (ret)
197 return; 209 goto exit;
198 210
199 chip->reg_output = reg_val; 211 chip->reg_output = reg_val;
212exit:
213 mutex_unlock(&chip->i2c_lock);
200} 214}
201 215
202static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) 216static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
@@ -517,6 +531,8 @@ static int __devinit pca953x_probe(struct i2c_client *client,
517 531
518 chip->names = pdata->names; 532 chip->names = pdata->names;
519 533
534 mutex_init(&chip->i2c_lock);
535
520 /* initialize cached registers from their original values. 536 /* initialize cached registers from their original values.
521 * we can't share this chip with another i2c master. 537 * we can't share this chip with another i2c master.
522 */ 538 */