blob: effb7b8ff81fd51664b7f51a124aaafbf3504feb [file] [log] [blame]
Thomas Gleixnerabd46272022-06-07 16:11:31 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrew F. Davisb8665262016-01-25 10:14:12 -06002/*
Andrew Davis2148a7a2023-05-15 12:44:08 -05003 * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
4 * Andrew Davis <afd@ti.com>
Andrew F. Davisb8665262016-01-25 10:14:12 -06005 */
6
7#include <linux/gpio/driver.h>
8#include <linux/i2c.h>
9#include <linux/module.h>
10#include <linux/mutex.h>
11
12#define TPIC2810_WS_COMMAND 0x44
13
14/**
15 * struct tpic2810 - GPIO driver data
16 * @chip: GPIO controller chip
17 * @client: I2C device pointer
18 * @buffer: Buffer for device register
19 * @lock: Protects write sequences
20 */
21struct tpic2810 {
22 struct gpio_chip chip;
23 struct i2c_client *client;
24 u8 buffer;
25 struct mutex lock;
26};
27
Axel Lince02d182016-02-15 20:09:14 +080028static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
29
Andrew F. Davisb8665262016-01-25 10:14:12 -060030static int tpic2810_get_direction(struct gpio_chip *chip,
31 unsigned offset)
32{
33 /* This device always output */
Matti Vaittinene42615e2019-11-06 10:54:12 +020034 return GPIO_LINE_DIRECTION_OUT;
Andrew F. Davisb8665262016-01-25 10:14:12 -060035}
36
37static int tpic2810_direction_input(struct gpio_chip *chip,
38 unsigned offset)
39{
40 /* This device is output only */
41 return -EINVAL;
42}
43
44static int tpic2810_direction_output(struct gpio_chip *chip,
45 unsigned offset, int value)
46{
47 /* This device always output */
Axel Lince02d182016-02-15 20:09:14 +080048 tpic2810_set(chip, offset, value);
Andrew F. Davisb8665262016-01-25 10:14:12 -060049 return 0;
50}
51
Axel Lin6e66a652016-03-23 19:49:41 +080052static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
Andrew F. Davisb8665262016-01-25 10:14:12 -060053{
54 struct tpic2810 *gpio = gpiochip_get_data(chip);
Axel Lin6e66a652016-03-23 19:49:41 +080055 u8 buffer;
56 int err;
Andrew F. Davisb8665262016-01-25 10:14:12 -060057
58 mutex_lock(&gpio->lock);
59
Axel Lin6e66a652016-03-23 19:49:41 +080060 buffer = gpio->buffer & ~mask;
61 buffer |= (mask & bits);
Andrew F. Davisb8665262016-01-25 10:14:12 -060062
Axel Lin6e66a652016-03-23 19:49:41 +080063 err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
64 buffer);
65 if (!err)
66 gpio->buffer = buffer;
Andrew F. Davisb8665262016-01-25 10:14:12 -060067
68 mutex_unlock(&gpio->lock);
69}
70
Axel Lin6e66a652016-03-23 19:49:41 +080071static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
72{
73 tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
74}
75
Andrew F. Davisb8665262016-01-25 10:14:12 -060076static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
77 unsigned long *bits)
78{
Axel Lin6e66a652016-03-23 19:49:41 +080079 tpic2810_set_mask_bits(chip, *mask, *bits);
Andrew F. Davisb8665262016-01-25 10:14:12 -060080}
81
Julia Lawalle35b5ab2016-09-11 14:14:37 +020082static const struct gpio_chip template_chip = {
Andrew F. Davisb8665262016-01-25 10:14:12 -060083 .label = "tpic2810",
84 .owner = THIS_MODULE,
85 .get_direction = tpic2810_get_direction,
86 .direction_input = tpic2810_direction_input,
87 .direction_output = tpic2810_direction_output,
88 .set = tpic2810_set,
89 .set_multiple = tpic2810_set_multiple,
90 .base = -1,
91 .ngpio = 8,
92 .can_sleep = true,
93};
94
95static const struct of_device_id tpic2810_of_match_table[] = {
96 { .compatible = "ti,tpic2810" },
97 { /* sentinel */ }
98};
99MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
100
Stephen Kittbf08ce12022-10-12 16:25:23 +0200101static int tpic2810_probe(struct i2c_client *client)
Andrew F. Davisb8665262016-01-25 10:14:12 -0600102{
103 struct tpic2810 *gpio;
Andrew F. Davisb8665262016-01-25 10:14:12 -0600104
105 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
106 if (!gpio)
107 return -ENOMEM;
108
Andrew F. Davisb8665262016-01-25 10:14:12 -0600109 gpio->chip = template_chip;
110 gpio->chip.parent = &client->dev;
111
112 gpio->client = client;
113
114 mutex_init(&gpio->lock);
115
Andrew Davis2148a7a2023-05-15 12:44:08 -0500116 return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
Andrew F. Davisb8665262016-01-25 10:14:12 -0600117}
118
119static const struct i2c_device_id tpic2810_id_table[] = {
120 { "tpic2810", },
121 { /* sentinel */ }
122};
123MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
124
125static struct i2c_driver tpic2810_driver = {
126 .driver = {
127 .name = "tpic2810",
128 .of_match_table = tpic2810_of_match_table,
129 },
Uwe Kleine-Königb41cabb2023-05-20 19:47:35 +0200130 .probe = tpic2810_probe,
Andrew F. Davisb8665262016-01-25 10:14:12 -0600131 .id_table = tpic2810_id_table,
132};
133module_i2c_driver(tpic2810_driver);
134
Andrew Davis2148a7a2023-05-15 12:44:08 -0500135MODULE_AUTHOR("Andrew Davis <afd@ti.com>");
Andrew F. Davisb8665262016-01-25 10:14:12 -0600136MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
137MODULE_LICENSE("GPL v2");