mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: barebox@lists.infradead.org
Cc: sha@pengutronix.de
Subject: [PATCH 1/8] mc9sdz60: clean up driver interface
Date: Mon, 18 Jan 2010 12:56:24 +0100	[thread overview]
Message-ID: <1263815791-10839-2-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1263815791-10839-1-git-send-email-mkl@pengutronix.de>

Export mc9sdz60_reg_read, mc9sdz60_reg_write and mc9sdz60_set_bits
function instead of exposing the i2c interface.

Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/i2c/mc9sdz60.c |   84 ++++++++++++++++++++++++++++++++++++-----------
 include/i2c/mc9sdz60.h |   61 +++++++++++++++++++++++++++++++++-
 2 files changed, 123 insertions(+), 22 deletions(-)

diff --git a/drivers/i2c/mc9sdz60.c b/drivers/i2c/mc9sdz60.c
index 4b1068d..3580af8 100644
--- a/drivers/i2c/mc9sdz60.c
+++ b/drivers/i2c/mc9sdz60.c
@@ -26,45 +26,88 @@
 #include <errno.h>
 
 #include <i2c/i2c.h>
-
-#include <asm/byteorder.h>
+#include <i2c/mc9sdz60.h>
 
 #define DRIVERNAME		"mc9sdz60"
 
-struct mc_priv {
-	struct cdev		cdev;
-	struct i2c_client	*client;
-};
-
-#define to_mc_priv(a)		container_of(a, struct mc_priv, cdev)
+#define to_mc9sdz60(a)		container_of(a, struct mc9sdz60, cdev)
 
-static struct mc_priv *mc_dev;
+static struct mc9sdz60 *mc_dev;
 
-struct i2c_client *mc9sdz60_get_client(void)
+struct mc9sdz60 *mc9sdz60_get(void)
 {
 	if (!mc_dev)
 		return NULL;
 
-	return mc_dev->client;
+	return mc_dev;
 }
+EXPORT_SYMBOL(mc9sdz60_get);
 
-static u32 mc_read_reg(struct mc_priv *mc, int reg)
+int mc9sdz60_reg_read(struct mc9sdz60 *mc9sdz60, enum mc9sdz60_reg reg, u8 *val)
 {
-	u8 buf;
+	int ret;
 
-	i2c_read_reg(mc->client, reg, &buf, sizeof(buf));
+	ret = i2c_read_reg(mc9sdz60->client, reg, val, 1);
 
-	return buf;
+	return ret == 1 ? 0 : ret;
 }
+EXPORT_SYMBOL(mc9sdz60_reg_read)
+
+int mc9sdz60_reg_write(struct mc9sdz60 *mc9sdz60, enum mc9sdz60_reg reg, u8 val)
+{
+	int ret;
+
+	ret = i2c_write_reg(mc9sdz60->client, reg, &val, 1);
+
+	return ret == 1 ? 0 : ret;
+}
+EXPORT_SYMBOL(mc9sdz60_reg_write)
+
+int mc9sdz60_set_bits(struct mc9sdz60 *mc9sdz60, enum mc9sdz60_reg reg, u8 mask, u8 val)
+{
+	u8 tmp;
+	int err;
+
+	err = mc9sdz60_reg_read(mc9sdz60, reg, &tmp);
+	tmp = (tmp & ~mask) | val;
+
+	if (!err)
+		err = mc9sdz60_reg_write(mc9sdz60, reg, tmp);
+
+	return err;
+}
+EXPORT_SYMBOL(mc9sdz60_set_bits);
 
 static ssize_t mc_read(struct cdev *cdev, void *_buf, size_t count, ulong offset, ulong flags)
 {
-	struct mc_priv *priv = to_mc_priv(cdev);
-	int i = count;
+	struct mc9sdz60 *mc9sdz60 = to_mc9sdz60(cdev);
 	u8 *buf = _buf;
+	size_t i = count;
+	int err;
+
+	while (i) {
+		err = mc9sdz60_reg_read(mc9sdz60, offset, buf);
+		if (err)
+			return (ssize_t)err;
+		buf++;
+		i--;
+		offset++;
+	}
+
+	return count;
+}
+
+static ssize_t mc_write(struct cdev *cdev, const void *_buf, size_t count, ulong offset, ulong flags)
+{
+	struct mc9sdz60 *mc9sdz60 = to_mc9sdz60(cdev);
+	const u8 *buf = _buf;
+	size_t i = count;
+	int err;
 
 	while (i) {
-		*buf = mc_read_reg(priv, offset);
+		err = mc9sdz60_reg_write(mc9sdz60, offset, *buf);
+		if (err)
+			return (ssize_t)err;
 		buf++;
 		i--;
 		offset++;
@@ -76,6 +119,7 @@ static ssize_t mc_read(struct cdev *cdev, void *_buf, size_t count, ulong offset
 static struct file_operations mc_fops = {
 	.lseek	= dev_lseek_default,
 	.read	= mc_read,
+	.write	= mc_write,
 };
 
 static int mc_probe(struct device_d *dev)
@@ -83,10 +127,10 @@ static int mc_probe(struct device_d *dev)
 	if (mc_dev)
 		return -EBUSY;
 
-	mc_dev = xzalloc(sizeof(struct mc_priv));
+	mc_dev = xzalloc(sizeof(struct mc9sdz60));
 	mc_dev->cdev.name = DRIVERNAME;
 	mc_dev->client = to_i2c_client(dev);
-	mc_dev->cdev.size = 256;
+	mc_dev->cdev.size = 64;		/* 35 known registers */
 	mc_dev->cdev.dev = dev;
 	mc_dev->cdev.ops = &mc_fops;
 
diff --git a/include/i2c/mc9sdz60.h b/include/i2c/mc9sdz60.h
index 04cfca0..4cc233e 100644
--- a/include/i2c/mc9sdz60.h
+++ b/include/i2c/mc9sdz60.h
@@ -1,7 +1,64 @@
+/*
+ * Copyright (C) 2009 Marc Kleine-Budde <mkl@pengutronix.de>
+ *
+ * This file is released under the GPLv2
+ *
+ * Derived from:
+ * - mcu_max8660-bus.h --  contains interface of the mc9sdz60 and max8660
+ *   Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ *
+ */
+
 #ifndef __ASM_ARCH_MC9SDZ60_H
 #define __ASM_ARCH_MC9SDZ60_H
 
-extern struct i2c_client *mc9sdz60_get_client(void);
+enum mc9sdz60_reg {
+	MC9SDZ60_REG_VERSION		= 0x00,
+	MC9SDZ60_REG_SECS		= 0x01,
+	MC9SDZ60_REG_MINS		= 0x02,
+	MC9SDZ60_REG_HRS		= 0x03,
+	MC9SDZ60_REG_DAY		= 0x04,
+	MC9SDZ60_REG_DATE		= 0x05,
+	MC9SDZ60_REG_MONTH		= 0x06,
+	MC9SDZ60_REG_YEAR		= 0x07,
+	MC9SDZ60_REG_ALARM_SECS		= 0x08,
+	MC9SDZ60_REG_ALARM_MINS		= 0x09,
+	MC9SDZ60_REG_ALARM_HRS		= 0x0a,
+	MC9SDZ60_REG_TS_CONTROL		= 0x0b,
+	MC9SDZ60_REG_X_LOW		= 0x0c,
+	MC9SDZ60_REG_Y_LOW		= 0x0d,
+	MC9SDZ60_REG_XY_HIGH		= 0x0e,
+	MC9SDZ60_REG_X_LEFT_LOW		= 0x0f,
+	MC9SDZ60_REG_X_LEFT_HIGH	= 0x10,
+	MC9SDZ60_REG_X_RIGHT		= 0x11,
+	MC9SDZ60_REG_Y_TOP_LOW		= 0x12,
+	MC9SDZ60_REG_Y_TOP_HIGH		= 0x13,
+	MC9SDZ60_REG_Y_BOTTOM		= 0x14,
+	MC9SDZ60_REG_RESET_1		= 0x15,
+	MC9SDZ60_REG_RESET_2		= 0x16,
+	MC9SDZ60_REG_POWER_CTL		= 0x17,
+	MC9SDZ60_REG_DELAY_CONFIG	= 0x18,
+	MC9SDZ60_REG_GPIO_1		= 0x19,
+	MC9SDZ60_REG_GPIO_2		= 0x1a,
+	MC9SDZ60_REG_KPD_1		= 0x1b,
+	MC9SDZ60_REG_KPD_2		= 0x1c,
+	MC9SDZ60_REG_KPD_CONTROL	= 0x1d,
+	MC9SDZ60_REG_INT_ENABLE_1	= 0x1e,
+	MC9SDZ60_REG_INT_ENABLE_2	= 0x1f,
+	MC9SDZ60_REG_INT_FLAG_1		= 0x20,
+	MC9SDZ60_REG_INT_FLAG_2		= 0x21,
+	MC9SDZ60_REG_DES_FLAG		= 0x22,
+};
 
-#endif /* __ASM_ARCH_MC9SDZ60_H */
+struct mc9sdz60 {
+	struct cdev		cdev;
+	struct i2c_client	*client;
+};
+
+extern struct mc9sdz60 *mc9sdz60_get(void);
 
+extern int mc9sdz60_reg_read(struct mc9sdz60 *priv, enum mc9sdz60_reg reg, u8 *val);
+extern int mc9sdz60_reg_write(struct mc9sdz60 *priv, enum mc9sdz60_reg reg, u8 val);
+extern int mc9sdz60_set_bits(struct mc9sdz60 *priv, enum mc9sdz60_reg reg, u8 mask, u8 val);
+
+#endif /* __ASM_ARCH_MC9SDZ60_H */
-- 
1.6.6


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

  reply	other threads:[~2010-01-18 11:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-18 11:56 [PATCH 0/8] update i.MX35 3stack and i2c drivers Marc Kleine-Budde
2010-01-18 11:56 ` Marc Kleine-Budde [this message]
2010-01-18 11:56   ` [PATCH 2/8] mc13892: clean up driver interface Marc Kleine-Budde
2010-01-18 11:56     ` [PATCH 3/8] i.MX35 3stack: adopt board to new mc13892 and mc9sdz60 " Marc Kleine-Budde
2010-01-18 11:56       ` [PATCH 4/8] i.MX35 3stack: clean up indention Marc Kleine-Budde
2010-01-18 11:56         ` [PATCH 5/8] i.MX35 3stack: clean up indention of lowlevel_init Marc Kleine-Budde
2010-01-18 11:56           ` [PATCH 6/8] i.MX35 3stack: increase env partition to hold splash image Marc Kleine-Budde
2010-01-18 11:56             ` [PATCH 7/8] i.MX35 3stack: update environemnt to support jffs and ubi/ubifs Marc Kleine-Budde
2010-01-18 11:56               ` [PATCH 8/8] i.MX35 3stack: update of defconfig Marc Kleine-Budde
2010-01-19  8:26 ` [PATCH 0/8] update i.MX35 3stack and i2c drivers 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=1263815791-10839-2-git-send-email-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=sha@pengutronix.de \
    /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