diff options
author | Jan Luebbe <jluebbe@debian.org> | 2015-07-30 17:38:29 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2015-08-20 07:47:14 +0200 |
commit | e87360ad056bce90318aaa2da1627969922d9053 (patch) | |
tree | 501400ed318f724e2f425ad106cf1aa2b3663ab9 /drivers/eeprom | |
parent | 0d14bc0ec4f8017e49ab5d2d91c45f5957afc31d (diff) | |
download | barebox-e87360ad056bce90318aaa2da1627969922d9053.tar.gz barebox-e87360ad056bce90318aaa2da1627969922d9053.tar.xz |
at24: support write-protect GPIO
Signed-off-by: Jan Luebbe <jluebbe@debian.org>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/eeprom')
-rw-r--r-- | drivers/eeprom/at24.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/drivers/eeprom/at24.c b/drivers/eeprom/at24.c index bb2dd533b6..76f30e7e22 100644 --- a/drivers/eeprom/at24.c +++ b/drivers/eeprom/at24.c @@ -21,6 +21,8 @@ #include <linux/log2.h> #include <i2c/i2c.h> #include <i2c/at24.h> +#include <gpio.h> +#include <of_gpio.h> /* * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable. @@ -55,6 +57,8 @@ struct at24_data { u8 *writebuf; unsigned write_max; unsigned num_addresses; + int wp_gpio; + int wp_active_low; /* * Some chips tie up multiple I2C addresses; dummy devices reserve @@ -345,6 +349,25 @@ static ssize_t at24_cdev_write(struct cdev *cdev, const void *buf, size_t count, return at24_write(at24, buf, off, count); } +static ssize_t at24_cdev_protect(struct cdev *cdev, size_t count, loff_t offset, + int prot) +{ + struct at24_data *at24 = cdev->priv; + + if (!gpio_is_valid(at24->wp_gpio)) + return -EOPNOTSUPP; + + prot = !!prot; + if (at24->wp_active_low) + prot = !prot; + + gpio_set_value(at24->wp_gpio, prot); + + udelay(50); + + return 0; +} + static int at24_probe(struct device_d *dev) { struct i2c_client *client = to_i2c_client(dev); @@ -403,6 +426,7 @@ static int at24_probe(struct device_d *dev) at24->cdev.ops = &at24->fops; at24->fops.lseek = dev_lseek_default; at24->fops.read = at24_cdev_read, + at24->fops.protect = at24_cdev_protect, at24->cdev.size = chip.byte_len; writable = !(chip.flags & AT24_FLAG_READONLY); @@ -419,6 +443,19 @@ static int at24_probe(struct device_d *dev) at24->writebuf = xmalloc(write_max + 2); } + at24->wp_gpio = -1; + if (dev->device_node) { + enum of_gpio_flags flags; + at24->wp_gpio = of_get_named_gpio_flags(dev->device_node, + "wp-gpios", 0, &flags); + if (gpio_is_valid(at24->wp_gpio)) { + at24->wp_active_low = flags & OF_GPIO_ACTIVE_LOW; + gpio_request(at24->wp_gpio, "eeprom-wp"); + gpio_direction_output(at24->wp_gpio, + !at24->wp_active_low); + } + } + at24->client[0] = client; /* use dummy devices for multiple-address chips */ @@ -440,6 +477,7 @@ static int at24_probe(struct device_d *dev) return 0; err_clients: + gpio_free(at24->wp_gpio); kfree(at24->writebuf); kfree(at24); err_out: |