blob: cace79c1b70a53fa9d339d4e7902abfe627a690b [file] [log] [blame]
Andrew F. Davisb8665262016-01-25 10:14:12 -06001/*
2 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
3 * Andrew F. Davis <afd@ti.com>
4 *
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.
8 *
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.
13 */
14
15#include <linux/gpio/driver.h>
16#include <linux/i2c.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19
20#define TPIC2810_WS_COMMAND 0x44
21
22/**
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
28 */
29struct tpic2810 {
30 struct gpio_chip chip;
31 struct i2c_client *client;
32 u8 buffer;
33 struct mutex lock;
34};
35
Axel Lince02d182016-02-15 20:09:14 +080036static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
37
Andrew F. Davisb8665262016-01-25 10:14:12 -060038static int tpic2810_get_direction(struct gpio_chip *chip,
39 unsigned offset)
40{
41 /* This device always output */
42 return 0;
43}
44
45static int tpic2810_direction_input(struct gpio_chip *chip,
46 unsigned offset)
47{
48 /* This device is output only */
49 return -EINVAL;
50}
51
52static int tpic2810_direction_output(struct gpio_chip *chip,
53 unsigned offset, int value)
54{
55 /* This device always output */
Axel Lince02d182016-02-15 20:09:14 +080056 tpic2810_set(chip, offset, value);
Andrew F. Davisb8665262016-01-25 10:14:12 -060057 return 0;
58}
59
Axel Lin6e66a652016-03-23 19:49:41 +080060static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
Andrew F. Davisb8665262016-01-25 10:14:12 -060061{
62 struct tpic2810 *gpio = gpiochip_get_data(chip);
Axel Lin6e66a652016-03-23 19:49:41 +080063 u8 buffer;
64 int err;
Andrew F. Davisb8665262016-01-25 10:14:12 -060065
66 mutex_lock(&gpio->lock);
67
Axel Lin6e66a652016-03-23 19:49:41 +080068 buffer = gpio->buffer & ~mask;
69 buffer |= (mask & bits);
Andrew F. Davisb8665262016-01-25 10:14:12 -060070
Axel Lin6e66a652016-03-23 19:49:41 +080071 err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
72 buffer);
73 if (!err)
74 gpio->buffer = buffer;
Andrew F. Davisb8665262016-01-25 10:14:12 -060075
76 mutex_unlock(&gpio->lock);
77}
78
Axel Lin6e66a652016-03-23 19:49:41 +080079static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
80{
81 tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
82}
83
Andrew F. Davisb8665262016-01-25 10:14:12 -060084static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
85 unsigned long *bits)
86{
Axel Lin6e66a652016-03-23 19:49:41 +080087 tpic2810_set_mask_bits(chip, *mask, *bits);
Andrew F. Davisb8665262016-01-25 10:14:12 -060088}
89
90static struct gpio_chip template_chip = {
91 .label = "tpic2810",
92 .owner = THIS_MODULE,
93 .get_direction = tpic2810_get_direction,
94 .direction_input = tpic2810_direction_input,
95 .direction_output = tpic2810_direction_output,
96 .set = tpic2810_set,
97 .set_multiple = tpic2810_set_multiple,
98 .base = -1,
99 .ngpio = 8,
100 .can_sleep = true,
101};
102
103static const struct of_device_id tpic2810_of_match_table[] = {
104 { .compatible = "ti,tpic2810" },
105 { /* sentinel */ }
106};
107MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
108
109static int tpic2810_probe(struct i2c_client *client,
110 const struct i2c_device_id *id)
111{
112 struct tpic2810 *gpio;
113 int ret;
114
115 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
116 if (!gpio)
117 return -ENOMEM;
118
119 i2c_set_clientdata(client, gpio);
120
121 gpio->chip = template_chip;
122 gpio->chip.parent = &client->dev;
123
124 gpio->client = client;
125
126 mutex_init(&gpio->lock);
127
128 ret = gpiochip_add_data(&gpio->chip, gpio);
129 if (ret < 0) {
130 dev_err(&client->dev, "Unable to register gpiochip\n");
131 return ret;
132 }
133
134 return 0;
135}
136
137static int tpic2810_remove(struct i2c_client *client)
138{
139 struct tpic2810 *gpio = i2c_get_clientdata(client);
140
141 gpiochip_remove(&gpio->chip);
142
143 return 0;
144}
145
146static const struct i2c_device_id tpic2810_id_table[] = {
147 { "tpic2810", },
148 { /* sentinel */ }
149};
150MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
151
152static struct i2c_driver tpic2810_driver = {
153 .driver = {
154 .name = "tpic2810",
155 .of_match_table = tpic2810_of_match_table,
156 },
157 .probe = tpic2810_probe,
158 .remove = tpic2810_remove,
159 .id_table = tpic2810_id_table,
160};
161module_i2c_driver(tpic2810_driver);
162
163MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
164MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
165MODULE_LICENSE("GPL v2");