at master 3.1 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/ 4 * Andrew Davis <afd@ti.com> 5 */ 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 28static int tpic2810_set(struct gpio_chip *chip, unsigned int offset, int value); 29 30static int tpic2810_get_direction(struct gpio_chip *chip, 31 unsigned offset) 32{ 33 /* This device always output */ 34 return GPIO_LINE_DIRECTION_OUT; 35} 36 37static int tpic2810_direction_output(struct gpio_chip *chip, 38 unsigned offset, int value) 39{ 40 /* This device always output */ 41 return tpic2810_set(chip, offset, value); 42} 43 44static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits) 45{ 46 struct tpic2810 *gpio = gpiochip_get_data(chip); 47 u8 buffer; 48 int err; 49 50 mutex_lock(&gpio->lock); 51 52 buffer = gpio->buffer & ~mask; 53 buffer |= (mask & bits); 54 55 err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND, 56 buffer); 57 if (!err) 58 gpio->buffer = buffer; 59 60 mutex_unlock(&gpio->lock); 61} 62 63static int tpic2810_set(struct gpio_chip *chip, unsigned int offset, int value) 64{ 65 tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0); 66 67 return 0; 68} 69 70static int tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask, 71 unsigned long *bits) 72{ 73 tpic2810_set_mask_bits(chip, *mask, *bits); 74 75 return 0; 76} 77 78static const struct gpio_chip template_chip = { 79 .label = "tpic2810", 80 .owner = THIS_MODULE, 81 .get_direction = tpic2810_get_direction, 82 .direction_output = tpic2810_direction_output, 83 .set = tpic2810_set, 84 .set_multiple = tpic2810_set_multiple, 85 .base = -1, 86 .ngpio = 8, 87 .can_sleep = true, 88}; 89 90static const struct of_device_id tpic2810_of_match_table[] = { 91 { .compatible = "ti,tpic2810" }, 92 { /* sentinel */ } 93}; 94MODULE_DEVICE_TABLE(of, tpic2810_of_match_table); 95 96static int tpic2810_probe(struct i2c_client *client) 97{ 98 struct tpic2810 *gpio; 99 100 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL); 101 if (!gpio) 102 return -ENOMEM; 103 104 gpio->chip = template_chip; 105 gpio->chip.parent = &client->dev; 106 107 gpio->client = client; 108 109 mutex_init(&gpio->lock); 110 111 return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio); 112} 113 114static const struct i2c_device_id tpic2810_id_table[] = { 115 { "tpic2810", }, 116 { /* sentinel */ } 117}; 118MODULE_DEVICE_TABLE(i2c, tpic2810_id_table); 119 120static struct i2c_driver tpic2810_driver = { 121 .driver = { 122 .name = "tpic2810", 123 .of_match_table = tpic2810_of_match_table, 124 }, 125 .probe = tpic2810_probe, 126 .id_table = tpic2810_id_table, 127}; 128module_i2c_driver(tpic2810_driver); 129 130MODULE_AUTHOR("Andrew Davis <afd@ti.com>"); 131MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver"); 132MODULE_LICENSE("GPL v2");