From: Jan Luebbe <jluebbe@debian.org>
To: barebox@lists.infradead.org
Subject: [PATCH] at24: support write-protect GPIO
Date: Thu, 30 Jul 2015 17:38:29 +0200 [thread overview]
Message-ID: <1438270709-2066-1-git-send-email-jluebbe@debian.org> (raw)
Signed-off-by: Jan Luebbe <jluebbe@debian.org>
---
drivers/eeprom/at24.c | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/drivers/eeprom/at24.c b/drivers/eeprom/at24.c
index bb2dd53..76f30e7 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:
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next reply other threads:[~2015-07-30 15:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-30 15:38 Jan Luebbe [this message]
2015-07-31 8:20 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1438270709-2066-1-git-send-email-jluebbe@debian.org \
--to=jluebbe@debian.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox