]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpio/pca953x.c
gpio: rename pca953x symbols
[mirror_ubuntu-zesty-kernel.git] / drivers / gpio / pca953x.c
CommitLineData
9e60fdcf 1/*
f3dc3630 2 * pca953x.c - 16-bit I/O port with interrupt and reset
9e60fdcf 3 *
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
6 *
7 * Derived from drivers/i2c/chips/pca9539.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/i2c.h>
d1c057e3 17#include <linux/i2c/pca953x.h>
9e60fdcf 18
19#include <asm/gpio.h>
20
21
f3dc3630 22#define NR_PCA953X_GPIOS 16
9e60fdcf 23
f3dc3630
GL
24#define PCA953X_INPUT 0
25#define PCA953X_OUTPUT 2
26#define PCA953X_INVERT 4
27#define PCA953X_DIRECTION 6
9e60fdcf 28
f3dc3630 29struct pca953x_chip {
9e60fdcf 30 unsigned gpio_start;
31 uint16_t reg_output;
32 uint16_t reg_direction;
33
34 struct i2c_client *client;
35 struct gpio_chip gpio_chip;
36};
37
38/* NOTE: we can't currently rely on fault codes to come from SMBus
39 * calls, so we map all errors to EIO here and return zero otherwise.
40 */
f3dc3630 41static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
9e60fdcf 42{
43 if (i2c_smbus_write_word_data(chip->client, reg, val) < 0)
44 return -EIO;
45 else
46 return 0;
47}
48
f3dc3630 49static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
9e60fdcf 50{
51 int ret;
52
53 ret = i2c_smbus_read_word_data(chip->client, reg);
54 if (ret < 0) {
55 dev_err(&chip->client->dev, "failed reading register\n");
56 return -EIO;
57 }
58
59 *val = (uint16_t)ret;
60 return 0;
61}
62
f3dc3630 63static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
9e60fdcf 64{
f3dc3630 65 struct pca953x_chip *chip;
9e60fdcf 66 uint16_t reg_val;
67 int ret;
68
f3dc3630 69 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 70
71 reg_val = chip->reg_direction | (1u << off);
f3dc3630 72 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 73 if (ret)
74 return ret;
75
76 chip->reg_direction = reg_val;
77 return 0;
78}
79
f3dc3630 80static int pca953x_gpio_direction_output(struct gpio_chip *gc,
9e60fdcf 81 unsigned off, int val)
82{
f3dc3630 83 struct pca953x_chip *chip;
9e60fdcf 84 uint16_t reg_val;
85 int ret;
86
f3dc3630 87 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 88
89 /* set output level */
90 if (val)
91 reg_val = chip->reg_output | (1u << off);
92 else
93 reg_val = chip->reg_output & ~(1u << off);
94
f3dc3630 95 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 96 if (ret)
97 return ret;
98
99 chip->reg_output = reg_val;
100
101 /* then direction */
102 reg_val = chip->reg_direction & ~(1u << off);
f3dc3630 103 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 104 if (ret)
105 return ret;
106
107 chip->reg_direction = reg_val;
108 return 0;
109}
110
f3dc3630 111static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
9e60fdcf 112{
f3dc3630 113 struct pca953x_chip *chip;
9e60fdcf 114 uint16_t reg_val;
115 int ret;
116
f3dc3630 117 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 118
f3dc3630 119 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
9e60fdcf 120 if (ret < 0) {
121 /* NOTE: diagnostic already emitted; that's all we should
122 * do unless gpio_*_value_cansleep() calls become different
123 * from their nonsleeping siblings (and report faults).
124 */
125 return 0;
126 }
127
128 return (reg_val & (1u << off)) ? 1 : 0;
129}
130
f3dc3630 131static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
9e60fdcf 132{
f3dc3630 133 struct pca953x_chip *chip;
9e60fdcf 134 uint16_t reg_val;
135 int ret;
136
f3dc3630 137 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 138
139 if (val)
140 reg_val = chip->reg_output | (1u << off);
141 else
142 reg_val = chip->reg_output & ~(1u << off);
143
f3dc3630 144 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 145 if (ret)
146 return;
147
148 chip->reg_output = reg_val;
149}
150
f3dc3630 151static int pca953x_init_gpio(struct pca953x_chip *chip)
9e60fdcf 152{
153 struct gpio_chip *gc;
154
155 gc = &chip->gpio_chip;
156
f3dc3630
GL
157 gc->direction_input = pca953x_gpio_direction_input;
158 gc->direction_output = pca953x_gpio_direction_output;
159 gc->get = pca953x_gpio_get_value;
160 gc->set = pca953x_gpio_set_value;
9e60fdcf 161
162 gc->base = chip->gpio_start;
f3dc3630
GL
163 gc->ngpio = NR_PCA953X_GPIOS;
164 gc->label = "pca953x";
9e60fdcf 165
166 return gpiochip_add(gc);
167}
168
f3dc3630 169static int __devinit pca953x_probe(struct i2c_client *client)
9e60fdcf 170{
f3dc3630
GL
171 struct pca953x_platform_data *pdata;
172 struct pca953x_chip *chip;
9e60fdcf 173 int ret;
174
175 pdata = client->dev.platform_data;
176 if (pdata == NULL)
177 return -ENODEV;
178
f3dc3630 179 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
9e60fdcf 180 if (chip == NULL)
181 return -ENOMEM;
182
183 chip->client = client;
184
185 chip->gpio_start = pdata->gpio_base;
186
187 /* initialize cached registers from their original values.
188 * we can't share this chip with another i2c master.
189 */
f3dc3630 190 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
9e60fdcf 191 if (ret)
192 goto out_failed;
193
f3dc3630 194 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
9e60fdcf 195 if (ret)
196 goto out_failed;
197
198 /* set platform specific polarity inversion */
f3dc3630 199 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
9e60fdcf 200 if (ret)
201 goto out_failed;
202
f3dc3630 203 ret = pca953x_init_gpio(chip);
9e60fdcf 204 if (ret)
205 goto out_failed;
206
207 if (pdata->setup) {
208 ret = pdata->setup(client, chip->gpio_chip.base,
209 chip->gpio_chip.ngpio, pdata->context);
210 if (ret < 0)
211 dev_warn(&client->dev, "setup failed, %d\n", ret);
212 }
213
214 i2c_set_clientdata(client, chip);
215 return 0;
216
217out_failed:
218 kfree(chip);
219 return ret;
220}
221
f3dc3630 222static int pca953x_remove(struct i2c_client *client)
9e60fdcf 223{
f3dc3630
GL
224 struct pca953x_platform_data *pdata = client->dev.platform_data;
225 struct pca953x_chip *chip = i2c_get_clientdata(client);
9e60fdcf 226 int ret = 0;
227
228 if (pdata->teardown) {
229 ret = pdata->teardown(client, chip->gpio_chip.base,
230 chip->gpio_chip.ngpio, pdata->context);
231 if (ret < 0) {
232 dev_err(&client->dev, "%s failed, %d\n",
233 "teardown", ret);
234 return ret;
235 }
236 }
237
238 ret = gpiochip_remove(&chip->gpio_chip);
239 if (ret) {
240 dev_err(&client->dev, "%s failed, %d\n",
241 "gpiochip_remove()", ret);
242 return ret;
243 }
244
245 kfree(chip);
246 return 0;
247}
248
f3dc3630 249static struct i2c_driver pca953x_driver = {
9e60fdcf 250 .driver = {
f3dc3630 251 .name = "pca953x",
9e60fdcf 252 },
f3dc3630
GL
253 .probe = pca953x_probe,
254 .remove = pca953x_remove,
9e60fdcf 255};
256
f3dc3630 257static int __init pca953x_init(void)
9e60fdcf 258{
f3dc3630 259 return i2c_add_driver(&pca953x_driver);
9e60fdcf 260}
f3dc3630 261module_init(pca953x_init);
9e60fdcf 262
f3dc3630 263static void __exit pca953x_exit(void)
9e60fdcf 264{
f3dc3630 265 i2c_del_driver(&pca953x_driver);
9e60fdcf 266}
f3dc3630 267module_exit(pca953x_exit);
9e60fdcf 268
269MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
f3dc3630 270MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
9e60fdcf 271MODULE_LICENSE("GPL");