From e583591037796f962d07e935b2a1bdd0f89a61fb Mon Sep 17 00:00:00 2001
From: Lars-Peter Clausen <lars@metafoo.de>
Date: Mon, 24 May 2010 23:37:19 +0200
Subject: [PATCH 38/69] MFD: pcf50633: Use mfd cells to register child devices

This patch changes the pcf50633 core code to use mfd cells to register child
devices instead of calling platform_device_{alloc,add} for each child.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/mfd/Kconfig                          |    1 +
 drivers/mfd/pcf50633-adc.c                   |   16 ++--
 drivers/mfd/pcf50633-core.c                  |  104 ++++++++++---------------
 drivers/power/pcf50633-charger.c             |   13 ++-
 drivers/regulator/pcf50633-regulator.c       |    3 +-
 drivers/video/backlight/pcf50633-backlight.c |    8 ++-
 include/linux/mfd/pcf50633/core.h            |   10 +--
 7 files changed, 70 insertions(+), 85 deletions(-)

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 9393e5b..9b2b1bb 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -448,6 +448,7 @@ config MFD_WM8994
 
 config MFD_PCF50633
 	tristate "Support for NXP PCF50633"
+	select MFD_CORE
 	depends on I2C
 	help
 	  Say yes here if you have NXP PCF50633 chip on your board.
diff --git a/drivers/mfd/pcf50633-adc.c b/drivers/mfd/pcf50633-adc.c
index aed0d2a..bf77c8f 100644
--- a/drivers/mfd/pcf50633-adc.c
+++ b/drivers/mfd/pcf50633-adc.c
@@ -51,11 +51,6 @@ struct pcf50633_adc {
 	struct mutex queue_mutex;
 };
 
-static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
-{
-	return platform_get_drvdata(pcf->adc_pdev);
-}
-
 static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
 {
 	channel &= PCF50633_ADCC1_ADCMUX_MASK;
@@ -71,7 +66,7 @@ static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
 
 static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
 {
-	struct pcf50633_adc *adc = __to_adc(pcf);
+	struct pcf50633_adc *adc = pcf->adc;
 	int head;
 
 	head = adc->queue_head;
@@ -85,7 +80,7 @@ static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
 static int
 adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
 {
-	struct pcf50633_adc *adc = __to_adc(pcf);
+	struct pcf50633_adc *adc = pcf->adc;
 	int head, tail;
 
 	mutex_lock(&adc->queue_mutex);
@@ -201,13 +196,14 @@ static void pcf50633_adc_irq(int irq, void *data)
 
 static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
 {
+	struct pcf50633 *pcf = dev_to_pcf50633(pdev->dev.parent);
 	struct pcf50633_adc *adc;
 
 	adc = kzalloc(sizeof(*adc), GFP_KERNEL);
 	if (!adc)
 		return -ENOMEM;
 
-	adc->pcf = dev_to_pcf50633(pdev->dev.parent);
+	adc->pcf = pcf;
 	platform_set_drvdata(pdev, adc);
 
 	pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
@@ -215,6 +211,8 @@ static int __devinit pcf50633_adc_probe(struct platform_device *pdev)
 
 	mutex_init(&adc->queue_mutex);
 
+	pcf->adc = adc;
+
 	return 0;
 }
 
@@ -223,6 +221,8 @@ static int __devexit pcf50633_adc_remove(struct platform_device *pdev)
 	struct pcf50633_adc *adc = platform_get_drvdata(pdev);
 	int i, head;
 
+	adc->pcf->adc = NULL;
+
 	pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
 
 	mutex_lock(&adc->queue_mutex);
diff --git a/drivers/mfd/pcf50633-core.c b/drivers/mfd/pcf50633-core.c
index 3d2eccd..07a018d 100644
--- a/drivers/mfd/pcf50633-core.c
+++ b/drivers/mfd/pcf50633-core.c
@@ -24,6 +24,8 @@
 #include <linux/pm.h>
 #include <linux/slab.h>
 
+#include <linux/mfd/core.h>
+
 #include <linux/mfd/pcf50633/core.h>
 
 static int __pcf50633_read(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
@@ -209,29 +211,8 @@ static struct attribute_group pcf_attr_group = {
 	.attrs	= pcf_sysfs_entries,
 };
 
-static void
-pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
-							struct platform_device **pdev)
-{
-	int ret;
-
-	*pdev = platform_device_alloc(name, -1);
-	if (!*pdev) {
-		dev_err(pcf->dev, "Falied to allocate %s\n", name);
-		return;
-	}
-
-	(*pdev)->dev.parent = pcf->dev;
-
-	ret = platform_device_add(*pdev);
-	if (ret) {
-		dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
-		platform_device_put(*pdev);
-		*pdev = NULL;
-	}
-}
-
 #ifdef CONFIG_PM_SLEEP
+
 static int pcf50633_suspend(struct device *dev)
 {
 	struct i2c_client *client = to_i2c_client(dev);
@@ -251,12 +232,44 @@ static int pcf50633_resume(struct device *dev)
 
 static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
 
+#define PCF50633_CELL(_name) \
+	{ \
+		.name = _name, \
+		.id = -1, \
+	} \
+
+#define PCF50633_CELL_ID(_name, _id) \
+	{ \
+		.name = _name, \
+		.id = _id, \
+	} \
+
+static struct mfd_cell pcf50633_cells[] = {
+	PCF50633_CELL("pcf50633-input"),
+	PCF50633_CELL("pcf50633-rtc"),
+	PCF50633_CELL("pcf50633-mbc"),
+	PCF50633_CELL("pcf50633-adc"),
+	PCF50633_CELL("pcf50633-backlight"),
+	PCF50633_CELL("pcf50633-gpio"),
+	PCF50633_CELL_ID("pcf50633-regltr", 0),
+	PCF50633_CELL_ID("pcf50633-regltr", 1),
+	PCF50633_CELL_ID("pcf50633-regltr", 2),
+	PCF50633_CELL_ID("pcf50633-regltr", 3),
+	PCF50633_CELL_ID("pcf50633-regltr", 4),
+	PCF50633_CELL_ID("pcf50633-regltr", 5),
+	PCF50633_CELL_ID("pcf50633-regltr", 6),
+	PCF50633_CELL_ID("pcf50633-regltr", 7),
+	PCF50633_CELL_ID("pcf50633-regltr", 8),
+	PCF50633_CELL_ID("pcf50633-regltr", 9),
+	PCF50633_CELL_ID("pcf50633-regltr", 10),
+};
+
 static int __devinit pcf50633_probe(struct i2c_client *client,
 				const struct i2c_device_id *ids)
 {
 	struct pcf50633 *pcf;
 	struct pcf50633_platform_data *pdata = client->dev.platform_data;
-	int i, ret;
+	int ret;
 	int version, variant;
 
 	if (!client->irq) {
@@ -289,35 +302,11 @@ static int __devinit pcf50633_probe(struct i2c_client *client,
 
 	pcf50633_irq_init(pcf, client->irq);
 
-	/* Create sub devices */
-	pcf50633_client_dev_register(pcf, "pcf50633-input",
-						&pcf->input_pdev);
-	pcf50633_client_dev_register(pcf, "pcf50633-rtc",
-						&pcf->rtc_pdev);
-	pcf50633_client_dev_register(pcf, "pcf50633-mbc",
-						&pcf->mbc_pdev);
-	pcf50633_client_dev_register(pcf, "pcf50633-adc",
-						&pcf->adc_pdev);
-	pcf50633_client_dev_register(pcf, "pcf50633-backlight",
-						&pcf->bl_pdev);
-	pcf50633_client_dev_register(pcf, "pcf50633-gpio",
-						&pcf->gpio_pdev);
-
-	for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
-		struct platform_device *pdev;
-
-		pdev = platform_device_alloc("pcf50633-regltr", i);
-		if (!pdev) {
-			dev_err(pcf->dev, "Cannot create regulator %d\n", i);
-			continue;
-		}
-
-		pdev->dev.parent = pcf->dev;
-		platform_device_add_data(pdev, &pdata->reg_init_data[i],
-					sizeof(pdata->reg_init_data[i]));
-		pcf->regulator_pdev[i] = pdev;
-
-		platform_device_add(pdev);
+	ret = mfd_add_devices(pcf->dev, 0, pcf50633_cells,
+			ARRAY_SIZE(pcf50633_cells), NULL, 0);
+	if (ret) {
+		dev_err(pcf->dev, "Failed to add mfd cells.\n");
+		goto err_free;
 	}
 
 	ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
@@ -338,20 +327,11 @@ err_free:
 static int __devexit pcf50633_remove(struct i2c_client *client)
 {
 	struct pcf50633 *pcf = i2c_get_clientdata(client);
-	int i;
 
 	sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
 	pcf50633_irq_free(pcf);
 
-	platform_device_unregister(pcf->gpio_pdev);
-	platform_device_unregister(pcf->input_pdev);
-	platform_device_unregister(pcf->rtc_pdev);
-	platform_device_unregister(pcf->mbc_pdev);
-	platform_device_unregister(pcf->adc_pdev);
-	platform_device_unregister(pcf->bl_pdev);
-
-	for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
-		platform_device_unregister(pcf->regulator_pdev[i]);
+	mfd_remove_devices(pcf->dev);
 
 	kfree(pcf);
 
diff --git a/drivers/power/pcf50633-charger.c b/drivers/power/pcf50633-charger.c
index 4fa52e1..3699611 100644
--- a/drivers/power/pcf50633-charger.c
+++ b/drivers/power/pcf50633-charger.c
@@ -40,7 +40,7 @@ struct pcf50633_mbc {
 
 int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
 {
-	struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
+	struct pcf50633_mbc *mbc = pcf->mbc;
 	int ret = 0;
 	u8 bits;
 	int charging_start = 1;
@@ -112,7 +112,7 @@ EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
 
 int pcf50633_mbc_get_status(struct pcf50633 *pcf)
 {
-	struct pcf50633_mbc *mbc  = platform_get_drvdata(pcf->mbc_pdev);
+	struct pcf50633_mbc *mbc = pcf->mbc;
 	int status = 0;
 	u8 chgmod;
 
@@ -143,7 +143,7 @@ EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
 
 int pcf50633_mbc_get_usb_online_status(struct pcf50633 *pcf)
 {
-	struct pcf50633_mbc *mbc  = platform_get_drvdata(pcf->mbc_pdev);
+	struct pcf50633_mbc *mbc = pcf->mbc;
 
 	if (!mbc)
 		return 0;
@@ -368,6 +368,7 @@ static const u8 mbc_irq_handlers[] = {
 
 static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
 {
+	struct pcf50633 *pcf = dev_to_pcf50633(pdev->dev.parent);
 	struct pcf50633_mbc *mbc;
 	int ret;
 	int i;
@@ -378,7 +379,7 @@ static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	platform_set_drvdata(pdev, mbc);
-	mbc->pcf = dev_to_pcf50633(pdev->dev.parent);
+	mbc->pcf = pcf;
 
 	/* Set up IRQ handlers */
 	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
@@ -444,6 +445,8 @@ static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
 	if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
 		pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
 
+	pcf->mbc = mbc;
+
 	return 0;
 }
 
@@ -452,6 +455,8 @@ static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
 	struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
 	int i;
 
+	mbc->pcf->mbc = NULL;
+
 	/* Remove IRQ handlers */
 	for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
 		pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
diff --git a/drivers/regulator/pcf50633-regulator.c b/drivers/regulator/pcf50633-regulator.c
index 69a11d9..5e7deb5 100644
--- a/drivers/regulator/pcf50633-regulator.c
+++ b/drivers/regulator/pcf50633-regulator.c
@@ -316,11 +316,10 @@ static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
 	struct regulator_dev *rdev;
 	struct pcf50633 *pcf;
 
-	/* Already set by core driver */
 	pcf = dev_to_pcf50633(pdev->dev.parent);
 
 	rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
-				  pdev->dev.platform_data, pcf);
+				  &pcf->pdata->reg_init_data[pdev->id], pcf);
 	if (IS_ERR(rdev))
 		return PTR_ERR(rdev);
 
diff --git a/drivers/video/backlight/pcf50633-backlight.c b/drivers/video/backlight/pcf50633-backlight.c
index ef5628d..7d80574 100644
--- a/drivers/video/backlight/pcf50633-backlight.c
+++ b/drivers/video/backlight/pcf50633-backlight.c
@@ -44,7 +44,7 @@ struct pcf50633_bl {
  */
 int pcf50633_bl_set_brightness_limit(struct pcf50633 *pcf, unsigned int limit)
 {
-	struct pcf50633_bl *pcf_bl = platform_get_drvdata(pcf->bl_pdev);
+	struct pcf50633_bl *pcf_bl = pcf->bl;
 
 	if (!pcf_bl)
 		return -ENODEV;
@@ -102,6 +102,7 @@ static const struct backlight_ops pcf50633_bl_ops = {
 static int __devinit pcf50633_bl_probe(struct platform_device *pdev)
 {
 	int ret;
+	struct pcf50633 *pcf = dev_to_pcf50633(pdev->dev.parent);
 	struct pcf50633_bl *pcf_bl;
 	struct device *parent = pdev->dev.parent;
 	struct pcf50633_platform_data *pcf50633_data = parent->platform_data;
@@ -124,7 +125,7 @@ static int __devinit pcf50633_bl_probe(struct platform_device *pdev)
 		pcf_bl->brightness_limit = 0x3f;
 	}
 
-	pcf_bl->pcf = dev_to_pcf50633(pdev->dev.parent);
+	pcf_bl->pcf = pcf;
 
 	pcf_bl->bl = backlight_device_register(pdev->name, &pdev->dev, pcf_bl,
 						&pcf50633_bl_ops, &bl_props);
@@ -134,6 +135,7 @@ static int __devinit pcf50633_bl_probe(struct platform_device *pdev)
 		goto err_free;
 	}
 
+	pcf->bl = pcf_bl;
 	platform_set_drvdata(pdev, pcf_bl);
 
 	pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDDIM, pdata->ramp_time);
@@ -156,6 +158,8 @@ static int __devexit pcf50633_bl_remove(struct platform_device *pdev)
 {
 	struct pcf50633_bl *pcf_bl = platform_get_drvdata(pdev);
 
+	pcf_bl->pcf->bl = NULL;
+
 	backlight_device_unregister(pcf_bl->bl);
 
 	platform_set_drvdata(pdev, NULL);
diff --git a/include/linux/mfd/pcf50633/core.h b/include/linux/mfd/pcf50633/core.h
index e0bd9a2..5f486f9 100644
--- a/include/linux/mfd/pcf50633/core.h
+++ b/include/linux/mfd/pcf50633/core.h
@@ -154,13 +154,9 @@ struct pcf50633 {
 
 	int onkey1s_held;
 
-	struct platform_device *gpio_pdev;
-	struct platform_device *rtc_pdev;
-	struct platform_device *mbc_pdev;
-	struct platform_device *adc_pdev;
-	struct platform_device *input_pdev;
-	struct platform_device *bl_pdev;
-	struct platform_device *regulator_pdev[PCF50633_NUM_REGULATORS];
+	struct pcf50633_mbc *mbc;
+	struct pcf50633_adc *adc;
+	struct pcf50633_bl *bl;
 };
 
 enum pcf50633_reg_int1 {
-- 
1.7.2.5

