All of lore.kernel.org
 help / color / mirror / Atom feed
From: Felipe Balbi <balbi@ti.com>
To: Tony Lindgren <tony@atomide.com>
Cc: Linux OMAP Mailing List <linux-omap@vger.kernel.org>,
	Felipe Balbi <balbi@ti.com>
Subject: [PATCH 07/27] cbus: tahvo: git it a context structure
Date: Thu, 13 Oct 2011 11:34:42 +0300	[thread overview]
Message-ID: <1318494902-13093-7-git-send-email-balbi@ti.com> (raw)
In-Reply-To: <1318494902-13093-1-git-send-email-balbi@ti.com>

just moving things around. It's a lot easier
to handle a dynamically allocated structure
as it allows for multiple instances of the
same device.

Signed-off-by: Felipe Balbi <balbi@ti.com>
---
 drivers/cbus/tahvo.c |  128 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 82 insertions(+), 46 deletions(-)

diff --git a/drivers/cbus/tahvo.c b/drivers/cbus/tahvo.c
index 4b062de..3a77d12 100644
--- a/drivers/cbus/tahvo.c
+++ b/drivers/cbus/tahvo.c
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 
+#include <linux/slab.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/device.h>
@@ -40,12 +41,16 @@
 #define TAHVO_ID		0x02
 #define PFX			"tahvo: "
 
-static int tahvo_initialized;
-static int tahvo_is_betty;
+struct tahvo {
+	/* device lock */
+	struct mutex	mutex;
+	struct device	*dev;
 
-static struct mutex tahvo_lock;
+	unsigned int	is_betty;
+	unsigned int	wide_backlight;
+};
 
-static struct device *the_dev;
+static struct tahvo *the_tahvo;
 
 struct tahvo_irq_handler_desc {
 	int (*func)(unsigned long);
@@ -57,7 +62,7 @@ static struct tahvo_irq_handler_desc tahvo_irq_handlers[MAX_TAHVO_IRQ_HANDLERS];
 
 int tahvo_get_status(void)
 {
-	return tahvo_initialized;
+	return the_tahvo != NULL;
 }
 EXPORT_SYMBOL(tahvo_get_status);
 
@@ -69,8 +74,9 @@ EXPORT_SYMBOL(tahvo_get_status);
  */
 int tahvo_read_reg(unsigned reg)
 {
-	BUG_ON(!tahvo_initialized);
-	return cbus_read_reg(the_dev, TAHVO_ID, reg);
+	struct tahvo		*tahvo = the_tahvo;
+
+	return cbus_read_reg(tahvo->dev, TAHVO_ID, reg);
 }
 EXPORT_SYMBOL(tahvo_read_reg);
 
@@ -83,8 +89,9 @@ EXPORT_SYMBOL(tahvo_read_reg);
  */
 void tahvo_write_reg(unsigned reg, u16 val)
 {
-	BUG_ON(!tahvo_initialized);
-	cbus_write_reg(the_dev, TAHVO_ID, reg, val);
+	struct tahvo		*tahvo = the_tahvo;
+
+	cbus_write_reg(tahvo->dev, TAHVO_ID, reg, val);
 }
 EXPORT_SYMBOL(tahvo_write_reg);
 
@@ -97,37 +104,40 @@ EXPORT_SYMBOL(tahvo_write_reg);
  */
 void tahvo_set_clear_reg_bits(unsigned reg, u16 set, u16 clear)
 {
-	u16 w;
+	struct tahvo		*tahvo = the_tahvo;
+	u16			w;
 
-	mutex_lock(&tahvo_lock);
+	mutex_lock(&tahvo->mutex);
 	w = tahvo_read_reg(reg);
 	w &= ~clear;
 	w |= set;
 	tahvo_write_reg(reg, w);
-	mutex_unlock(&tahvo_lock);
+	mutex_unlock(&tahvo->mutex);
 }
 
 void tahvo_disable_irq(int id)
 {
-	u16 mask;
+	struct tahvo		*tahvo = the_tahvo;
+	u16			mask;
 
-	mutex_lock(&tahvo_lock);
+	mutex_lock(&tahvo->mutex);
 	mask = tahvo_read_reg(TAHVO_REG_IMR);
 	mask |= 1 << id;
 	tahvo_write_reg(TAHVO_REG_IMR, mask);
-	mutex_unlock(&tahvo_lock);
+	mutex_unlock(&tahvo->mutex);
 }
 EXPORT_SYMBOL(tahvo_disable_irq);
 
 void tahvo_enable_irq(int id)
 {
-	u16 mask;
+	struct tahvo		*tahvo = the_tahvo;
+	u16			mask;
 
-	mutex_lock(&tahvo_lock);
+	mutex_lock(&tahvo->mutex);
 	mask = tahvo_read_reg(TAHVO_REG_IMR);
 	mask &= ~(1 << id);
 	tahvo_write_reg(TAHVO_REG_IMR, mask);
-	mutex_unlock(&tahvo_lock);
+	mutex_unlock(&tahvo->mutex);
 }
 EXPORT_SYMBOL(tahvo_enable_irq);
 
@@ -137,13 +147,12 @@ void tahvo_ack_irq(int id)
 }
 EXPORT_SYMBOL(tahvo_ack_irq);
 
-static int tahvo_7bit_backlight;
-
 int tahvo_get_backlight_level(void)
 {
-	int mask;
+	struct tahvo		*tahvo = the_tahvo;
+	int			mask;
 
-	if (tahvo_7bit_backlight)
+	if (tahvo->wide_backlight)
 		mask = 0x7f;
 	else
 		mask = 0x0f;
@@ -153,7 +162,9 @@ EXPORT_SYMBOL(tahvo_get_backlight_level);
 
 int tahvo_get_max_backlight_level(void)
 {
-	if (tahvo_7bit_backlight)
+	struct tahvo		*tahvo = the_tahvo;
+
+	if (tahvo->wide_backlight)
 		return 0x7f;
 	else
 		return 0x0f;
@@ -162,7 +173,7 @@ EXPORT_SYMBOL(tahvo_get_max_backlight_level);
 
 void tahvo_set_backlight_level(int level)
 {
-	int max_level;
+	int			max_level;
 
 	max_level = tahvo_get_max_backlight_level();
 	if (level > max_level)
@@ -174,9 +185,11 @@ EXPORT_SYMBOL(tahvo_set_backlight_level);
 static irqreturn_t tahvo_irq_handler(int irq, void *dev_id)
 {
 	struct tahvo_irq_handler_desc *hnd;
-	u16 id;
-	u16 im;
-	int i;
+
+	struct tahvo		*tahvo = the_tahvo;
+	u16			id;
+	u16			im;
+	int			i;
 
 	for (;;) {
 		id = tahvo_read_reg(TAHVO_REG_IDR);
@@ -192,7 +205,7 @@ static irqreturn_t tahvo_irq_handler(int irq, void *dev_id)
 			hnd = &tahvo_irq_handlers[i];
 			if (hnd->func == NULL) {
 				/* Spurious tahvo interrupt - just ack it */
-				printk(KERN_INFO "Spurious Tahvo interrupt "
+				dev_err(tahvo->dev, "Spurious interrupt "
 						 "(id %d)\n", i);
 				tahvo_disable_irq(i);
 				tahvo_ack_irq(i);
@@ -215,19 +228,20 @@ static irqreturn_t tahvo_irq_handler(int irq, void *dev_id)
 int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name)
 {
 	struct tahvo_irq_handler_desc *hnd;
+	struct tahvo		*tahvo = the_tahvo;
 
 	if (irq_handler == NULL || id >= MAX_TAHVO_IRQ_HANDLERS ||
 	    name == NULL) {
-		printk(KERN_ERR PFX "Invalid arguments to %s\n",
+		dev_err(tahvo->dev, "Invalid arguments to %s\n",
 		       __FUNCTION__);
 		return -EINVAL;
 	}
 	hnd = &tahvo_irq_handlers[id];
 	if (hnd->func != NULL) {
-		printk(KERN_ERR PFX "IRQ %d already reserved\n", id);
+		dev_err(tahvo->dev, "IRQ %d already reserved\n", id);
 		return -EBUSY;
 	}
-	printk(KERN_INFO PFX "Registering interrupt %d for device %s\n",
+	dev_dbg(tahvo->dev, "Registering interrupt %d for device %s\n",
 	       id, name);
 	hnd->func = irq_handler;
 	hnd->arg = arg;
@@ -246,15 +260,16 @@ EXPORT_SYMBOL(tahvo_request_irq);
 void tahvo_free_irq(int id)
 {
 	struct tahvo_irq_handler_desc *hnd;
+	struct tahvo		*tahvo = the_tahvo;
 
 	if (id >= MAX_TAHVO_IRQ_HANDLERS) {
-		printk(KERN_ERR PFX "Invalid argument to %s\n",
+		dev_err(tahvo->dev, "Invalid argument to %s\n",
 		       __FUNCTION__);
 		return;
 	}
 	hnd = &tahvo_irq_handlers[id];
 	if (hnd->func == NULL) {
-		printk(KERN_ERR PFX "IRQ %d already freed\n", id);
+		dev_err(tahvo->dev, "IRQ %d already freed\n", id);
 		return;
 	}
 
@@ -265,13 +280,24 @@ EXPORT_SYMBOL(tahvo_free_irq);
 
 static int __devinit tahvo_probe(struct platform_device *pdev)
 {
-	int rev, id, ret;
-	int irq;
+	struct tahvo		*tahvo;
+	int			rev;
+	int			ret;
+	int			irq;
+	int			id;
+
+	tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL);
+	if (!tahvo) {
+		dev_err(&pdev->dev, "not enough memory\n");
+		ret = -ENOMEM;
+		goto err0;
+	}
 
-	mutex_init(&tahvo_lock);
-	the_dev = &pdev->dev;
+	the_tahvo = tahvo;
+	platform_set_drvdata(pdev, tahvo);
 
-	tahvo_initialized = 1;
+	mutex_init(&tahvo->mutex);
+	tahvo->dev = &pdev->dev;
 
 	rev = tahvo_read_reg(TAHVO_REG_ASICR);
 
@@ -280,19 +306,20 @@ static int __devinit tahvo_probe(struct platform_device *pdev)
 	switch (id) {
 	case 0x03:
 		if ((rev & 0xff) >= 0x50)
-			tahvo_7bit_backlight = 1;
+			tahvo->wide_backlight = true;
 		break;
 	case 0x0b:
-		tahvo_is_betty = 1;
-		tahvo_7bit_backlight = 1;
+		tahvo->is_betty = true;
+		tahvo->wide_backlight = true;
 		break;
 	default:
 		dev_err(&pdev->dev, "Tahvo/Betty chip not found");
-		return -ENODEV;
+		ret = -ENODEV;
+		goto err1;
 	}
 
 	dev_err(&pdev->dev, "%s v%d.%d found\n",
-			tahvo_is_betty ? "Betty" : "Tahvo",
+			tahvo->is_betty ? "Betty" : "Tahvo",
 			(rev >> 4) & 0x0f, rev & 0x0f);
 
 	irq = platform_get_irq(pdev, 0);
@@ -305,21 +332,30 @@ static int __devinit tahvo_probe(struct platform_device *pdev)
 			"tahvo", 0);
 	if (ret < 0) {
 		dev_err(&pdev->dev, "Unable to register IRQ handler\n");
-		return ret;
+		goto err1;
 	}
+
 	return 0;
+
+err1:
+	kfree(tahvo);
+
+err0:
+	return ret;
 }
 
 static int __devexit tahvo_remove(struct platform_device *pdev)
 {
-	int irq;
+	struct tahvo		*tahvo = platform_get_drvdata(pdev);
+	int			irq;
 
 	irq = platform_get_irq(pdev, 0);
 
 	/* Mask all TAHVO interrupts */
 	tahvo_write_reg(TAHVO_REG_IMR, 0xffff);
 	free_irq(irq, 0);
-	the_dev = NULL;
+	kfree(tahvo);
+	the_tahvo = NULL;
 
 	return 0;
 }
-- 
1.7.6.396.ge0613


  parent reply	other threads:[~2011-10-13  8:35 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-13  8:34 [PATCH 01/27] cbus: tahvo: convert spinlock into mutex Felipe Balbi
2011-10-13  8:34 ` [PATCH 02/27] cbus: tahvo: move to __devinit/__devexit sections Felipe Balbi
2011-10-13  8:34 ` [PATCH 03/27] cbus: tahvo: a switch looks better Felipe Balbi
2011-10-13  8:34 ` [PATCH 04/27] cbus: tahvo: don't go over 80 columns Felipe Balbi
2011-10-13  8:34 ` [PATCH 05/27] cbus: tahvo: drop the tasklet Felipe Balbi
2011-10-13  8:34 ` [PATCH 06/27] cbus: retu: set IRQF_ONESHOT flag Felipe Balbi
2011-10-13  8:34 ` Felipe Balbi [this message]
2011-10-13  8:34 ` [PATCH 08/27] cbus: tahvo: pass tahvo to IRQ handler Felipe Balbi
2011-10-13  8:34 ` [PATCH 09/27] cbus: tahvo: introduce __tahvo_(read/write)_reg Felipe Balbi
2011-10-13  8:34 ` [PATCH 10/27] cbus: tahvo: drop some unneded defines Felipe Balbi
2011-10-13  8:34 ` [PATCH 11/27] cbus: retu: IRQ demux optimization Felipe Balbi
2011-10-13  8:34 ` [PATCH 12/27] cbus: tahvo: give it an irq_chip Felipe Balbi
2011-10-13  8:34 ` [PATCH 13/27] cbus: tahvo: start using irq_chip Felipe Balbi
2011-10-13  8:34 ` [PATCH 14/27] cbus: tahvo: usb: fix up to use threaded irqs Felipe Balbi
2011-10-13  8:34 ` [PATCH 15/27] cbus: tahvo drop the legacy interfaces Felipe Balbi
2011-10-13  8:34 ` [PATCH 16/27] cbus: tahvo: usb: drop unused variable Felipe Balbi
2011-10-13  8:34 ` [PATCH 17/27] cbus: tahvo: no need to mask interrupts on exit Felipe Balbi
2011-10-13  8:34 ` [PATCH 18/27] cbus: tahvo: drop the get_status hack Felipe Balbi
2011-10-13  8:34 ` [PATCH 19/27] cbus: tahvo: drop more unused interfaces Felipe Balbi
2011-10-13  8:34 ` [PATCH 20/27] cbus: tahvo: pass child device pointer Felipe Balbi
2011-10-13  8:34 ` [PATCH 21/27] cbus: tahvo: drop backlight interfaces Felipe Balbi
2011-10-13  8:34 ` [PATCH 22/27] cbus: tahvo: drop static global pointer Felipe Balbi
2011-10-13  8:34 ` [PATCH 23/27] cbus: tahvo: prepare for children without IRQ Felipe Balbi
2011-10-13  8:34 ` [PATCH 24/27] cbus: tahvo: allocate tahvo-pwm child Felipe Balbi
2011-10-13  8:35 ` [PATCH 25/27] cbus: fix compile issue on tahvo and retu Felipe Balbi
2011-10-13  8:35 ` [PATCH 26/27] cbus: use platform_driver_register " Felipe Balbi
2011-10-13  8:35 ` [PATCH 27/27] cbus: mark dependencies for ARCH_OMAP Felipe Balbi
2011-10-17 17:20 ` [PATCH 01/27] cbus: tahvo: convert spinlock into mutex Tony Lindgren
2011-10-17 18:04   ` Felipe Balbi
2011-10-17 18:19     ` Tony Lindgren
2011-10-17 18:26       ` Felipe Balbi

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=1318494902-13093-7-git-send-email-balbi@ti.com \
    --to=balbi@ti.com \
    --cc=linux-omap@vger.kernel.org \
    --cc=tony@atomide.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.