mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] at24: support write-protect GPIO
@ 2015-07-30 15:38 Jan Luebbe
  2015-07-31  8:20 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Luebbe @ 2015-07-30 15:38 UTC (permalink / raw)
  To: barebox

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] at24: support write-protect GPIO
  2015-07-30 15:38 [PATCH] at24: support write-protect GPIO Jan Luebbe
@ 2015-07-31  8:20 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2015-07-31  8:20 UTC (permalink / raw)
  To: Jan Luebbe; +Cc: barebox

On Thu, Jul 30, 2015 at 05:38:29PM +0200, Jan Luebbe wrote:
> Signed-off-by: Jan Luebbe <jluebbe@debian.org>
> ---
>  drivers/eeprom/at24.c | 38 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 38 insertions(+)

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-31  8:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-30 15:38 [PATCH] at24: support write-protect GPIO Jan Luebbe
2015-07-31  8:20 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox