2 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
3 * Andrew F. Davis <afd@ti.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether expressed or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License version 2 for more details.
15 #include <linux/gpio/driver.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
20 #define TPIC2810_WS_COMMAND 0x44
23 * struct tpic2810 - GPIO driver data
24 * @chip: GPIO controller chip
25 * @client: I2C device pointer
26 * @buffer: Buffer for device register
27 * @lock: Protects write sequences
30 struct gpio_chip chip
;
31 struct i2c_client
*client
;
36 static void tpic2810_set(struct gpio_chip
*chip
, unsigned offset
, int value
);
38 static int tpic2810_get_direction(struct gpio_chip
*chip
,
41 /* This device always output */
45 static int tpic2810_direction_input(struct gpio_chip
*chip
,
48 /* This device is output only */
52 static int tpic2810_direction_output(struct gpio_chip
*chip
,
53 unsigned offset
, int value
)
55 /* This device always output */
56 tpic2810_set(chip
, offset
, value
);
60 static void tpic2810_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
62 struct tpic2810
*gpio
= gpiochip_get_data(chip
);
64 mutex_lock(&gpio
->lock
);
67 gpio
->buffer
|= BIT(offset
);
69 gpio
->buffer
&= ~BIT(offset
);
71 i2c_smbus_write_byte_data(gpio
->client
, TPIC2810_WS_COMMAND
,
74 mutex_unlock(&gpio
->lock
);
77 static void tpic2810_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
80 struct tpic2810
*gpio
= gpiochip_get_data(chip
);
82 mutex_lock(&gpio
->lock
);
84 /* clear bits under mask */
85 gpio
->buffer
&= ~(*mask
);
86 /* set bits under mask */
87 gpio
->buffer
|= ((*mask
) & (*bits
));
89 i2c_smbus_write_byte_data(gpio
->client
, TPIC2810_WS_COMMAND
,
92 mutex_unlock(&gpio
->lock
);
95 static struct gpio_chip template_chip
= {
98 .get_direction
= tpic2810_get_direction
,
99 .direction_input
= tpic2810_direction_input
,
100 .direction_output
= tpic2810_direction_output
,
102 .set_multiple
= tpic2810_set_multiple
,
108 static const struct of_device_id tpic2810_of_match_table
[] = {
109 { .compatible
= "ti,tpic2810" },
112 MODULE_DEVICE_TABLE(of
, tpic2810_of_match_table
);
114 static int tpic2810_probe(struct i2c_client
*client
,
115 const struct i2c_device_id
*id
)
117 struct tpic2810
*gpio
;
120 gpio
= devm_kzalloc(&client
->dev
, sizeof(*gpio
), GFP_KERNEL
);
124 i2c_set_clientdata(client
, gpio
);
126 gpio
->chip
= template_chip
;
127 gpio
->chip
.parent
= &client
->dev
;
129 gpio
->client
= client
;
131 mutex_init(&gpio
->lock
);
133 ret
= gpiochip_add_data(&gpio
->chip
, gpio
);
135 dev_err(&client
->dev
, "Unable to register gpiochip\n");
142 static int tpic2810_remove(struct i2c_client
*client
)
144 struct tpic2810
*gpio
= i2c_get_clientdata(client
);
146 gpiochip_remove(&gpio
->chip
);
151 static const struct i2c_device_id tpic2810_id_table
[] = {
155 MODULE_DEVICE_TABLE(i2c
, tpic2810_id_table
);
157 static struct i2c_driver tpic2810_driver
= {
160 .of_match_table
= tpic2810_of_match_table
,
162 .probe
= tpic2810_probe
,
163 .remove
= tpic2810_remove
,
164 .id_table
= tpic2810_id_table
,
166 module_i2c_driver(tpic2810_driver
);
168 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
169 MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
170 MODULE_LICENSE("GPL v2");