cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

gpio-pca9570.c (3202B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * Driver for PCA9570 I2C GPO expander
      4 *
      5 * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
      6 *
      7 * Based on gpio-tpic2810.c
      8 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
      9 *	Andrew F. Davis <afd@ti.com>
     10 */
     11
     12#include <linux/gpio/driver.h>
     13#include <linux/i2c.h>
     14#include <linux/module.h>
     15#include <linux/mutex.h>
     16#include <linux/property.h>
     17
     18/**
     19 * struct pca9570 - GPIO driver data
     20 * @chip: GPIO controller chip
     21 * @lock: Protects write sequences
     22 * @out: Buffer for device register
     23 */
     24struct pca9570 {
     25	struct gpio_chip chip;
     26	struct mutex lock;
     27	u8 out;
     28};
     29
     30static int pca9570_read(struct pca9570 *gpio, u8 *value)
     31{
     32	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
     33	int ret;
     34
     35	ret = i2c_smbus_read_byte(client);
     36	if (ret < 0)
     37		return ret;
     38
     39	*value = ret;
     40	return 0;
     41}
     42
     43static int pca9570_write(struct pca9570 *gpio, u8 value)
     44{
     45	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
     46
     47	return i2c_smbus_write_byte(client, value);
     48}
     49
     50static int pca9570_get_direction(struct gpio_chip *chip,
     51				 unsigned offset)
     52{
     53	/* This device always output */
     54	return GPIO_LINE_DIRECTION_OUT;
     55}
     56
     57static int pca9570_get(struct gpio_chip *chip, unsigned offset)
     58{
     59	struct pca9570 *gpio = gpiochip_get_data(chip);
     60	u8 buffer;
     61	int ret;
     62
     63	ret = pca9570_read(gpio, &buffer);
     64	if (ret)
     65		return ret;
     66
     67	return !!(buffer & BIT(offset));
     68}
     69
     70static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
     71{
     72	struct pca9570 *gpio = gpiochip_get_data(chip);
     73	u8 buffer;
     74	int ret;
     75
     76	mutex_lock(&gpio->lock);
     77
     78	buffer = gpio->out;
     79	if (value)
     80		buffer |= BIT(offset);
     81	else
     82		buffer &= ~BIT(offset);
     83
     84	ret = pca9570_write(gpio, buffer);
     85	if (ret)
     86		goto out;
     87
     88	gpio->out = buffer;
     89
     90out:
     91	mutex_unlock(&gpio->lock);
     92}
     93
     94static int pca9570_probe(struct i2c_client *client)
     95{
     96	struct pca9570 *gpio;
     97
     98	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
     99	if (!gpio)
    100		return -ENOMEM;
    101
    102	gpio->chip.label = client->name;
    103	gpio->chip.parent = &client->dev;
    104	gpio->chip.owner = THIS_MODULE;
    105	gpio->chip.get_direction = pca9570_get_direction;
    106	gpio->chip.get = pca9570_get;
    107	gpio->chip.set = pca9570_set;
    108	gpio->chip.base = -1;
    109	gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
    110	gpio->chip.can_sleep = true;
    111
    112	mutex_init(&gpio->lock);
    113
    114	/* Read the current output level */
    115	pca9570_read(gpio, &gpio->out);
    116
    117	i2c_set_clientdata(client, gpio);
    118
    119	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
    120}
    121
    122static const struct i2c_device_id pca9570_id_table[] = {
    123	{ "pca9570", 4 },
    124	{ /* sentinel */ }
    125};
    126MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
    127
    128static const struct of_device_id pca9570_of_match_table[] = {
    129	{ .compatible = "nxp,pca9570", .data = (void *)4 },
    130	{ /* sentinel */ }
    131};
    132MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
    133
    134static struct i2c_driver pca9570_driver = {
    135	.driver = {
    136		.name = "pca9570",
    137		.of_match_table = pca9570_of_match_table,
    138	},
    139	.probe_new = pca9570_probe,
    140	.id_table = pca9570_id_table,
    141};
    142module_i2c_driver(pca9570_driver);
    143
    144MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
    145MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
    146MODULE_LICENSE("GPL v2");