162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Digital I/O driver for Technologic Systems I2C FPGA Core 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2015, 2018 Technologic Systems 662306a36Sopenharmony_ci * Copyright (C) 2016 Savoir-Faire Linux 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/gpio/driver.h> 1062306a36Sopenharmony_ci#include <linux/i2c.h> 1162306a36Sopenharmony_ci#include <linux/of.h> 1262306a36Sopenharmony_ci#include <linux/module.h> 1362306a36Sopenharmony_ci#include <linux/regmap.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#define DEFAULT_PIN_NUMBER 32 1662306a36Sopenharmony_ci/* 1762306a36Sopenharmony_ci * Register bits used by the GPIO device 1862306a36Sopenharmony_ci * Some boards, such as TS-7970 do not have a separate input bit 1962306a36Sopenharmony_ci */ 2062306a36Sopenharmony_ci#define TS4900_GPIO_OE 0x01 2162306a36Sopenharmony_ci#define TS4900_GPIO_OUT 0x02 2262306a36Sopenharmony_ci#define TS4900_GPIO_IN 0x04 2362306a36Sopenharmony_ci#define TS7970_GPIO_IN 0x02 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_cistruct ts4900_gpio_priv { 2662306a36Sopenharmony_ci struct regmap *regmap; 2762306a36Sopenharmony_ci struct gpio_chip gpio_chip; 2862306a36Sopenharmony_ci unsigned int input_bit; 2962306a36Sopenharmony_ci}; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cistatic int ts4900_gpio_get_direction(struct gpio_chip *chip, 3262306a36Sopenharmony_ci unsigned int offset) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci struct ts4900_gpio_priv *priv = gpiochip_get_data(chip); 3562306a36Sopenharmony_ci unsigned int reg; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci regmap_read(priv->regmap, offset, ®); 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci if (reg & TS4900_GPIO_OE) 4062306a36Sopenharmony_ci return GPIO_LINE_DIRECTION_OUT; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci return GPIO_LINE_DIRECTION_IN; 4362306a36Sopenharmony_ci} 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_cistatic int ts4900_gpio_direction_input(struct gpio_chip *chip, 4662306a36Sopenharmony_ci unsigned int offset) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci struct ts4900_gpio_priv *priv = gpiochip_get_data(chip); 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci /* 5162306a36Sopenharmony_ci * Only clear the OE bit here, requires a RMW. Prevents a potential issue 5262306a36Sopenharmony_ci * with OE and DAT getting to the physical pin at different times. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci return regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OE, 0); 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic int ts4900_gpio_direction_output(struct gpio_chip *chip, 5862306a36Sopenharmony_ci unsigned int offset, int value) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci struct ts4900_gpio_priv *priv = gpiochip_get_data(chip); 6162306a36Sopenharmony_ci unsigned int reg; 6262306a36Sopenharmony_ci int ret; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci /* 6562306a36Sopenharmony_ci * If changing from an input to an output, we need to first set the 6662306a36Sopenharmony_ci * GPIO's DAT bit to what is requested and then set the OE bit. This 6762306a36Sopenharmony_ci * prevents a glitch that can occur on the IO line. 6862306a36Sopenharmony_ci */ 6962306a36Sopenharmony_ci regmap_read(priv->regmap, offset, ®); 7062306a36Sopenharmony_ci if (!(reg & TS4900_GPIO_OE)) { 7162306a36Sopenharmony_ci if (value) 7262306a36Sopenharmony_ci reg = TS4900_GPIO_OUT; 7362306a36Sopenharmony_ci else 7462306a36Sopenharmony_ci reg &= ~TS4900_GPIO_OUT; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci regmap_write(priv->regmap, offset, reg); 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci if (value) 8062306a36Sopenharmony_ci ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE | 8162306a36Sopenharmony_ci TS4900_GPIO_OUT); 8262306a36Sopenharmony_ci else 8362306a36Sopenharmony_ci ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci return ret; 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic int ts4900_gpio_get(struct gpio_chip *chip, unsigned int offset) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci struct ts4900_gpio_priv *priv = gpiochip_get_data(chip); 9162306a36Sopenharmony_ci unsigned int reg; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci regmap_read(priv->regmap, offset, ®); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci return !!(reg & priv->input_bit); 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_cistatic void ts4900_gpio_set(struct gpio_chip *chip, unsigned int offset, 9962306a36Sopenharmony_ci int value) 10062306a36Sopenharmony_ci{ 10162306a36Sopenharmony_ci struct ts4900_gpio_priv *priv = gpiochip_get_data(chip); 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci if (value) 10462306a36Sopenharmony_ci regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 10562306a36Sopenharmony_ci TS4900_GPIO_OUT); 10662306a36Sopenharmony_ci else 10762306a36Sopenharmony_ci regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 0); 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic const struct regmap_config ts4900_regmap_config = { 11162306a36Sopenharmony_ci .reg_bits = 16, 11262306a36Sopenharmony_ci .val_bits = 8, 11362306a36Sopenharmony_ci}; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_cistatic const struct gpio_chip template_chip = { 11662306a36Sopenharmony_ci .label = "ts4900-gpio", 11762306a36Sopenharmony_ci .owner = THIS_MODULE, 11862306a36Sopenharmony_ci .get_direction = ts4900_gpio_get_direction, 11962306a36Sopenharmony_ci .direction_input = ts4900_gpio_direction_input, 12062306a36Sopenharmony_ci .direction_output = ts4900_gpio_direction_output, 12162306a36Sopenharmony_ci .get = ts4900_gpio_get, 12262306a36Sopenharmony_ci .set = ts4900_gpio_set, 12362306a36Sopenharmony_ci .base = -1, 12462306a36Sopenharmony_ci .can_sleep = true, 12562306a36Sopenharmony_ci}; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_cistatic const struct of_device_id ts4900_gpio_of_match_table[] = { 12862306a36Sopenharmony_ci { 12962306a36Sopenharmony_ci .compatible = "technologic,ts4900-gpio", 13062306a36Sopenharmony_ci .data = (void *)TS4900_GPIO_IN, 13162306a36Sopenharmony_ci }, { 13262306a36Sopenharmony_ci .compatible = "technologic,ts7970-gpio", 13362306a36Sopenharmony_ci .data = (void *)TS7970_GPIO_IN, 13462306a36Sopenharmony_ci }, 13562306a36Sopenharmony_ci { /* sentinel */ }, 13662306a36Sopenharmony_ci}; 13762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, ts4900_gpio_of_match_table); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cistatic int ts4900_gpio_probe(struct i2c_client *client) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci struct ts4900_gpio_priv *priv; 14262306a36Sopenharmony_ci u32 ngpio; 14362306a36Sopenharmony_ci int ret; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci if (of_property_read_u32(client->dev.of_node, "ngpios", &ngpio)) 14662306a36Sopenharmony_ci ngpio = DEFAULT_PIN_NUMBER; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); 14962306a36Sopenharmony_ci if (!priv) 15062306a36Sopenharmony_ci return -ENOMEM; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci priv->gpio_chip = template_chip; 15362306a36Sopenharmony_ci priv->gpio_chip.label = "ts4900-gpio"; 15462306a36Sopenharmony_ci priv->gpio_chip.ngpio = ngpio; 15562306a36Sopenharmony_ci priv->gpio_chip.parent = &client->dev; 15662306a36Sopenharmony_ci priv->input_bit = (uintptr_t)of_device_get_match_data(&client->dev); 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci priv->regmap = devm_regmap_init_i2c(client, &ts4900_regmap_config); 15962306a36Sopenharmony_ci if (IS_ERR(priv->regmap)) { 16062306a36Sopenharmony_ci ret = PTR_ERR(priv->regmap); 16162306a36Sopenharmony_ci dev_err(&client->dev, "Failed to allocate register map: %d\n", 16262306a36Sopenharmony_ci ret); 16362306a36Sopenharmony_ci return ret; 16462306a36Sopenharmony_ci } 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci ret = devm_gpiochip_add_data(&client->dev, &priv->gpio_chip, priv); 16762306a36Sopenharmony_ci if (ret < 0) { 16862306a36Sopenharmony_ci dev_err(&client->dev, "Unable to register gpiochip\n"); 16962306a36Sopenharmony_ci return ret; 17062306a36Sopenharmony_ci } 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci i2c_set_clientdata(client, priv); 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci return 0; 17562306a36Sopenharmony_ci} 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_cistatic const struct i2c_device_id ts4900_gpio_id_table[] = { 17862306a36Sopenharmony_ci { "ts4900-gpio", }, 17962306a36Sopenharmony_ci { /* sentinel */ } 18062306a36Sopenharmony_ci}; 18162306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ts4900_gpio_id_table); 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_cistatic struct i2c_driver ts4900_gpio_driver = { 18462306a36Sopenharmony_ci .driver = { 18562306a36Sopenharmony_ci .name = "ts4900-gpio", 18662306a36Sopenharmony_ci .of_match_table = ts4900_gpio_of_match_table, 18762306a36Sopenharmony_ci }, 18862306a36Sopenharmony_ci .probe = ts4900_gpio_probe, 18962306a36Sopenharmony_ci .id_table = ts4900_gpio_id_table, 19062306a36Sopenharmony_ci}; 19162306a36Sopenharmony_cimodule_i2c_driver(ts4900_gpio_driver); 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ciMODULE_AUTHOR("Technologic Systems"); 19462306a36Sopenharmony_ciMODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core"); 19562306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 196