21 #include <linux/kernel.h>
22 #include <linux/slab.h>
24 #include <linux/i2c.h>
29 #include <linux/module.h>
95 static int i2c_write_le16(
struct i2c_client *client,
unsigned word)
97 u8 buf[2] = { word & 0xff, word >> 8, };
101 return (status < 0) ? status : 0;
104 static int i2c_read_le16(
struct i2c_client *client)
112 return (buf[1] << 8) | buf[0];
117 static int pcf857x_input(
struct gpio_chip *
chip,
unsigned offset)
130 static int pcf857x_get(
struct gpio_chip *chip,
unsigned offset)
136 return (value < 0) ? 0 : (value & (1 <<
offset));
139 static int pcf857x_output(
struct gpio_chip *chip,
unsigned offset,
int value)
156 static void pcf857x_set(
struct gpio_chip *chip,
unsigned offset,
int value)
158 pcf857x_output(chip, offset, value);
163 static int pcf857x_to_irq(
struct gpio_chip *chip,
unsigned offset)
184 gpio->status = status;
186 spin_unlock_irqrestore(&gpio->
slock, flags);
202 static int pcf857x_irq_domain_map(
struct irq_domain *domain,
unsigned int virq,
205 irq_set_chip_and_handler(virq,
212 .map = pcf857x_irq_domain_map,
215 static void pcf857x_irq_domain_cleanup(
struct pcf857x *gpio)
224 static int pcf857x_irq_domain_init(
struct pcf857x *gpio,
232 &pcf857x_irq_domain_ops,
239 dev_name(dev), gpio);
245 gpio->
chip.to_irq = pcf857x_to_irq;
251 pcf857x_irq_domain_cleanup(gpio);
257 static int pcf857x_probe(
struct i2c_client *client,
264 pdata = client->
dev.platform_data;
278 gpio->
chip.can_sleep = 1;
281 gpio->
chip.get = pcf857x_get;
282 gpio->
chip.set = pcf857x_set;
283 gpio->
chip.direction_input = pcf857x_input;
284 gpio->
chip.direction_output = pcf857x_output;
285 gpio->
chip.ngpio =
id->driver_data;
288 if (pdata && pdata->
irq) {
289 status = pcf857x_irq_domain_init(gpio, pdata, &client->
dev);
291 dev_err(&client->
dev,
"irq_domain init failed\n");
307 if (gpio->
chip.ngpio == 8) {
308 gpio->
write = i2c_write_le8;
309 gpio->
read = i2c_read_le8;
311 if (!i2c_check_functionality(client->
adapter,
325 }
else if (gpio->
chip.ngpio == 16) {
326 gpio->
write = i2c_write_le16;
327 gpio->
read = i2c_read_le16;
334 status = i2c_read_le16(client);
337 dev_dbg(&client->
dev,
"unsupported number of gpios\n");
347 i2c_set_clientdata(client, gpio);
378 client->
irq ?
" (irq ignored)" :
"");
383 if (pdata && pdata->
setup) {
384 status = pdata->
setup(client,
394 dev_dbg(&client->
dev,
"probe error %d for '%s'\n",
395 status, client->
name);
397 if (pdata && pdata->
irq)
398 pcf857x_irq_domain_cleanup(gpio);
404 static int pcf857x_remove(
struct i2c_client *client)
407 struct pcf857x *gpio = i2c_get_clientdata(client);
421 if (pdata && pdata->
irq)
422 pcf857x_irq_domain_cleanup(gpio);
428 dev_err(&client->
dev,
"%s --> %d\n",
"remove", status);
437 .probe = pcf857x_probe,
438 .remove = pcf857x_remove,
439 .id_table = pcf857x_id,
442 static int __init pcf857x_init(
void)
444 return i2c_add_driver(&pcf857x_driver);
451 static void __exit pcf857x_exit(
void)