Skip to contentSkip to conversation

add 88pm80x mfd driver

PATCH New Qiao Zhou 3 participants 8 messages Jun 13, 2012 – Jun 15, 2012

Subsystems (3)
lore
Share & tools
lore

Qiao Zhou

author
#0 lore raw

cover add 88pm80x mfd driver

add 88pm80x mfd driver, and also add sub-device: rtc and onkey. would add below sub device later: regulator, headset detect codec.

Qiao Zhou (3):
mfd: support 88pm80x in 80x driver
rtc: add rtc support to 88PM80X PMIC
input: add onkey support to 88PM80X PMIC

 drivers/input/misc/88pm80x_onkey.c |  217 ++++++++
 drivers/input/misc/Kconfig         |   10 +
 drivers/input/misc/Makefile        |    1 +
 drivers/mfd/88pm80x-core.c         | 1002 ++++++++++++++++++++++++++++++++++++
 drivers/mfd/88pm80x-i2c.c          |  370 +++++++++++++
 drivers/mfd/Kconfig                |   11 +
 drivers/mfd/Makefile               |    2 +
 drivers/rtc/Kconfig                |   10 +
 drivers/rtc/Makefile               |    1 +
 drivers/rtc/rtc-88pm80x.c          |  370 +++++++++++++
 include/linux/mfd/88pm80x.h        |  701 +++++++++++++++++++++++++
 11 files changed, 2695 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/misc/88pm80x_onkey.c
 create mode 100644 drivers/mfd/88pm80x-core.c
 create mode 100644 drivers/mfd/88pm80x-i2c.c
 create mode 100644 drivers/rtc/rtc-88pm80x.c
 create mode 100644 include/linux/mfd/88pm80x.h
Signature · 1 line
1.7.4.1
author
#1 lore raw

PATCH 1/3 mfd: support 88pm80x in 80x driver

88PM800 and 88PM805 are two discrete chips used for power management. Hardware designer can use them together or only one of them according to requirement.

There's some logic tightly linked between these two chips. For example, USB charger driver needs to access both chips by I2C interface.

Now share one driver to these two devices. Only one I2C client is identified in platform init data. If one chip is also used, user should mark it with related i2c_addr field of platform init data. Then driver could create another I2C client for the chip.

All I2C operations are accessed by 80x-i2c driver. In order to support all I2C client address, the read/write API is changed in below.

reg_read(client, offset)
reg_write(client, offset, data)

The benefit is that client drivers only need one kind of read/write API. I2C and MFD driver can be shared in both 800 and 805.

---
 drivers/mfd/88pm80x-core.c  | 1002 +++++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/88pm80x-i2c.c   |  370 ++++++++++++++++
 drivers/mfd/Kconfig         |   11 +
 drivers/mfd/Makefile        |    2 +
 include/linux/mfd/88pm80x.h |  701 ++++++++++++++++++++++++++++++
 5 files changed, 2086 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mfd/88pm80x-core.c
 create mode 100644 drivers/mfd/88pm80x-i2c.c
 create mode 100644 include/linux/mfd/88pm80x.h
drivers/mfd/88pm80x-core.c new file +1002 -0
new file mode 100644
index 0000000..dd27aad
@@ -0,0 +1,1002 @@
+/*
+ * Base driver for Marvell 88PM800
+ *
+ * Copyright (C) 2012 Marvell International Ltd.
+ * Haojian Zhuang <[email protected]>
+ * Joseph(Yossi) Hanin <[email protected]>
+ * Qiao Zhou <[email protected]>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License. See the file "COPYING" in the main directory of this
+ * archive for more details.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/88pm80x.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+
+static struct resource rtc_resources[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .start = PM800_IRQ_RTC,
+	 .end = PM800_IRQ_RTC,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell rtc_devs[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .num_resources = ARRAY_SIZE(rtc_resources),
+	 .resources = &rtc_resources[0],
+	 .id = -1,
+	 },
+};
+
+static struct resource onkey_resources[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .start = PM800_IRQ_ONKEY,
+	 .end = PM800_IRQ_ONKEY,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell onkey_devs[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .num_resources = 1,
+	 .resources = &onkey_resources[0],
+	 .id = -1,
+	 },
+};
+
+static struct resource codec_resources[] = {
+	{
+	 /* Headset microphone insertion or removal */
+	 .name = "micin",
+	 .start = PM805_IRQ_MIC_DET,
+	 .end = PM805_IRQ_MIC_DET,
+	 .flags = IORESOURCE_IRQ,
+	 },
+	{
+	 /* Audio short HP1 */
+	 .name = "audio-short1",
+	 .start = PM805_IRQ_HP1_SHRT,
+	 .end = PM805_IRQ_HP1_SHRT,
+	 .flags = IORESOURCE_IRQ,
+	 },
+	{
+	 /* Audio short HP2 */
+	 .name = "audio-short2",
+	 .start = PM805_IRQ_HP2_SHRT,
+	 .end = PM805_IRQ_HP2_SHRT,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell codec_devs[] = {
+	{
+	 .name = "88pm80x-codec",
+	 .num_resources = ARRAY_SIZE(codec_resources),
+	 .resources = &codec_resources[0],
+	 .id = -1,
+	 },
+};
+
+struct pm80x_irq_data {
+	int reg;
+	int mask_reg;
+	int enable;		/* enable or not */
+	int offs;		/* bit offset in mask register */
+};
+
+static struct pm80x_irq_data pm800_irqs[] = {
+	[PM800_IRQ_ONKEY] = {	/*0 */
+			     .reg = PM800_INT_STATUS1,
+			     .mask_reg = PM800_INT_ENA_1,
+			     .offs = 1 << 0,
+			     },
+	[PM800_IRQ_EXTON] = {
+			     .reg = PM800_INT_STATUS1,
+			     .mask_reg = PM800_INT_ENA_1,
+			     .offs = 1 << 1,
+			     },
+	[PM800_IRQ_CHG] = {
+			   .reg = PM800_INT_STATUS1,
+			   .mask_reg = PM800_INT_ENA_1,
+			   .offs = 1 << 2,
+			   },
+	[PM800_IRQ_BAT] = {
+			   .reg = PM800_INT_STATUS1,
+			   .mask_reg = PM800_INT_ENA_1,
+			   .offs = 1 << 3,
+			   },
+	[PM800_IRQ_RTC] = {
+			   .reg = PM800_INT_STATUS1,
+			   .mask_reg = PM800_INT_ENA_1,
+			   .offs = 1 << 4,
+			   },
+	[PM800_IRQ_CLASSD] = {	/*5 */
+			      .reg = PM800_INT_STATUS1,
+			      .mask_reg = PM800_INT_ENA_1,
+			      .offs = 1 << 5,
+			      },
+	[PM800_IRQ_VBAT] = {
+			    .reg = PM800_INT_STATUS2,
+			    .mask_reg = PM800_INT_ENA_2,
+			    .offs = 1 << 0,
+			    },
+	[PM800_IRQ_VSYS] = {
+			    .reg = PM800_INT_STATUS2,
+			    .mask_reg = PM800_INT_ENA_2,
+			    .offs = 1 << 1,
+			    },
+	[PM800_IRQ_VCHG] = {
+			    .reg = PM800_INT_STATUS2,
+			    .mask_reg = PM800_INT_ENA_2,
+			    .offs = 1 << 2,
+			    },
+	[PM800_IRQ_TINT] = {
+			    .reg = PM800_INT_STATUS2,
+			    .mask_reg = PM800_INT_ENA_2,
+			    .offs = 1 << 3,
+			    },
+	[PM800_IRQ_GPADC0] = {	/*10 */
+			      .reg = PM800_INT_STATUS3,
+			      .mask_reg = PM800_INT_ENA_3,
+			      .offs = 1 << 0,
+			      },
+	[PM800_IRQ_GPADC1] = {
+			      .reg = PM800_INT_STATUS3,
+			      .mask_reg = PM800_INT_ENA_3,
+			      .offs = 1 << 1,
+			      },
+	[PM800_IRQ_GPADC2] = {
+			      .reg = PM800_INT_STATUS3,
+			      .mask_reg = PM800_INT_ENA_3,
+			      .offs = 1 << 2,
+			      },
+	[PM800_IRQ_GPADC3] = {
+			      .reg = PM800_INT_STATUS3,
+			      .mask_reg = PM800_INT_ENA_3,
+			      .offs = 1 << 3,
+			      },
+	[PM800_IRQ_GPADC4] = {
+			      .reg = PM800_INT_STATUS3,
+			      .mask_reg = PM800_INT_ENA_3,
+			      .offs = 1 << 4,
+			      },
+	[PM800_IRQ_GPIO0] = {	/*15 */
+			     .reg = PM800_INT_STATUS4,
+			     .mask_reg = PM800_INT_ENA_4,
+			     .offs = 1 << 0,
+			     },
+	[PM800_IRQ_GPIO1] = {
+			     .reg = PM800_INT_STATUS4,
+			     .mask_reg = PM800_INT_ENA_4,
+			     .offs = 1 << 1,
+			     },
+	[PM800_IRQ_GPIO2] = {
+			     .reg = PM800_INT_STATUS4,
+			     .mask_reg = PM800_INT_ENA_4,
+			     .offs = 1 << 2,
+			     },
+	[PM800_IRQ_GPIO3] = {
+			     .reg = PM800_INT_STATUS4,
+			     .mask_reg = PM800_INT_ENA_4,
+			     .offs = 1 << 3,
+			     },
+	[PM800_IRQ_GPIO4] = {	/*19 */
+			     .reg = PM800_INT_STATUS4,
+			     .mask_reg = PM800_INT_ENA_4,
+			     .offs = 1 << 4,
+			     },
+};
+
+static inline struct pm80x_irq_data *irq_to_pm800(struct pm80x_chip *chip,
+						  int irq)
+{
+	int offset = irq - chip->pm800_chip->irq_base;
+	if (!chip->pm800_chip || (offset < 0)
+	    || (offset >= ARRAY_SIZE(pm800_irqs)))
+		return NULL;
+	return &pm800_irqs[offset];
+}
+
+static irqreturn_t pm800_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
+	struct pm80x_subchip *pm800_chip = chip->pm800_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c = chip->base_page;
+	int read_reg = -1, value = 0;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(pm800_irqs); i++) {
+		irq_data = &pm800_irqs[i];
+		if (read_reg != irq_data->reg) {
+			read_reg = irq_data->reg;
+			value = pm80x_reg_read(i2c, irq_data->reg);
+		}
+		if (value & irq_data->enable)
+			handle_nested_irq(pm800_chip->irq_base + i);
+	}
+	return IRQ_HANDLED;
+}
+
+static void pm800_irq_lock(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+
+	mutex_lock(&chip->pm800_irq_lock);
+}
+
+static void pm800_irq_sync_unlock(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	unsigned char mask[PM800_INT_REG_NUM] = { 0x0, 0x0, 0x0, 0x0 };
+	int i;
+
+	i2c = chip->base_page;
+
+	for (i = 0; i < ARRAY_SIZE(pm800_irqs); i++) {
+		irq_data = &pm800_irqs[i];
+		switch (irq_data->mask_reg) {
+		case PM800_INT_ENA_1:
+			mask[0] &= ~irq_data->offs;
+			mask[0] |= irq_data->enable;
+			break;
+		case PM800_INT_ENA_2:
+			mask[1] &= ~irq_data->offs;
+			mask[1] |= irq_data->enable;
+			break;
+		case PM800_INT_ENA_3:
+			mask[2] &= ~irq_data->offs;
+			mask[2] |= irq_data->enable;
+			break;
+		case PM800_INT_ENA_4:
+			mask[3] &= ~irq_data->offs;
+			mask[3] |= irq_data->enable;
+			break;
+		default:
+			dev_err(chip->dev, "wrong IRQ\n");
+			break;
+		}
+	}
+	/* update mask into registers */
+	for (i = 0; i < PM800_INT_REG_NUM; i++)
+		pm80x_reg_write(i2c, PM800_INT_ENA_1 + i, mask[i]);
+
+	mutex_unlock(&chip->pm800_irq_lock);
+}
+
+static void pm800_irq_enable(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_subchip *pm800_chip = chip->pm800_chip;
+	int offset = data->irq - pm800_chip->irq_base;
+	if ((offset >= 0) && (offset < ARRAY_SIZE(pm800_irqs)))
+		pm800_irqs[offset].enable = pm800_irqs[offset].offs;
+}
+
+static void pm800_irq_disable(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_subchip *pm800_chip = chip->pm800_chip;
+	int offset = data->irq - pm800_chip->irq_base;
+	if ((offset >= 0) && (offset < ARRAY_SIZE(pm800_irqs)))
+		pm800_irqs[data->irq - pm800_chip->irq_base].enable = 0;
+}
+
+static struct irq_chip pm800_irq_chip = {
+	.name = "88pm80x",
+	.irq_bus_lock = pm800_irq_lock,
+	.irq_bus_sync_unlock = pm800_irq_sync_unlock,
+	.irq_enable = pm800_irq_enable,
+	.irq_disable = pm800_irq_disable,
+};
+
+static struct pm80x_irq_data pm805_irqs[] = {
+	[PM805_IRQ_LDO_OFF] = {	/*0 */
+			       .reg = PM805_INT_STATUS1,
+			       .mask_reg = PM805_INT_MASK1,
+			       .offs = 1 << 5,
+			       },
+	[PM805_IRQ_SRC_DPLL_LOCK] = {
+				     .reg = PM805_INT_STATUS1,
+				     .mask_reg = PM805_INT_MASK1,
+				     .offs = 1 << 4,
+				     },
+	[PM805_IRQ_CLIP_FAULT] = {
+				  .reg = PM805_INT_STATUS1,
+				  .mask_reg = PM805_INT_MASK1,
+				  .offs = 1 << 3,
+				  },
+	[PM805_IRQ_MIC_CONFLICT] = {
+				    .reg = PM805_INT_STATUS1,
+				    .mask_reg = PM805_INT_MASK1,
+				    .offs = 1 << 2,
+				    },
+	[PM805_IRQ_HP2_SHRT] = {
+				.reg = PM805_INT_STATUS1,
+				.mask_reg = PM805_INT_MASK1,
+				.offs = 1 << 1,
+				},
+	[PM805_IRQ_HP1_SHRT] = {	/*5 */
+				.reg = PM805_INT_STATUS1,
+				.mask_reg = PM805_INT_MASK1,
+				.offs = 1 << 0,
+				},
+	[PM805_IRQ_FINE_PLL_FAULT] = {
+				      .reg = PM805_INT_STATUS2,
+				      .mask_reg = PM805_INT_MASK2,
+				      .offs = 1 << 5,
+				      },
+	[PM805_IRQ_RAW_PLL_FAULT] = {
+				     .reg = PM805_INT_STATUS2,
+				     .mask_reg = PM805_INT_MASK2,
+				     .offs = 1 << 4,
+				     },
+	[PM805_IRQ_VOLP_BTN_DET] = {
+				    .reg = PM805_INT_STATUS2,
+				    .mask_reg = PM805_INT_MASK2,
+				    .offs = 1 << 3,
+				    },
+	[PM805_IRQ_VOLM_BTN_DET] = {
+				    .reg = PM805_INT_STATUS2,
+				    .mask_reg = PM805_INT_MASK2,
+				    .offs = 1 << 2,
+				    },
+	[PM805_IRQ_SHRT_BTN_DET] = {	/*10 */
+				    .reg = PM805_INT_STATUS2,
+				    .mask_reg = PM805_INT_MASK2,
+				    .offs = 1 << 1,
+				    },
+	[PM805_IRQ_MIC_DET] = {	/*11 */
+			       .reg = PM805_INT_STATUS2,
+			       .mask_reg = PM805_INT_MASK2,
+			       .offs = 1 << 0,
+			       },
+};
+
+static inline struct pm80x_irq_data *irq_to_pm805(struct pm80x_chip *chip,
+						  int irq)
+{
+	int offset = irq - chip->pm805_chip->irq_base;
+	if (!chip->pm805_chip || (offset < 0)
+	    || (offset >= ARRAY_SIZE(pm805_irqs)))
+		return NULL;
+	return &pm805_irqs[offset];
+}
+
+static irqreturn_t pm805_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	int i, read_reg = -1, value = 0;
+
+	i2c = pm805_chip->client;
+
+	for (i = 0; i < ARRAY_SIZE(pm805_irqs); i++) {
+		irq_data = &pm805_irqs[i];
+		if (read_reg != irq_data->reg) {
+			read_reg = irq_data->reg;
+			value = pm80x_reg_read(i2c, irq_data->reg);
+		}
+		if (value & irq_data->enable)
+			handle_nested_irq(pm805_chip->irq_base + i);
+	}
+	return IRQ_HANDLED;
+}
+
+static void pm805_irq_lock(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+
+	mutex_lock(&chip->pm805_irq_lock);
+}
+
+static void pm805_irq_sync_unlock(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	unsigned char mask[PM805_INT_REG_NUM] = { 0x0 };
+	int i;
+
+	i2c = pm805_chip->client;
+
+	for (i = 0; i < ARRAY_SIZE(pm805_irqs); i++) {
+		irq_data = &pm805_irqs[i];
+		switch (irq_data->mask_reg) {
+		case PM805_INT_MASK1:
+			mask[0] &= ~irq_data->offs;
+			mask[0] |= irq_data->enable;
+			break;
+		case PM805_INT_MASK2:
+			mask[1] &= ~irq_data->offs;
+			mask[1] |= irq_data->enable;
+			break;
+		default:
+			dev_err(chip->dev, "wrong IRQ\n");
+			break;
+		}
+	}
+	/* update mask into registers */
+	for (i = 0; i < PM805_INT_REG_NUM; i++)
+		pm80x_reg_write(i2c, PM805_INT_MASK1 + i, mask[i]);
+
+	mutex_unlock(&chip->pm805_irq_lock);
+}
+
+static void pm805_irq_enable(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	int offset = data->irq - pm805_chip->irq_base;
+	if ((offset >= 0) && (offset < ARRAY_SIZE(pm805_irqs)))
+		pm805_irqs[offset].enable = pm805_irqs[offset].offs;
+}
+
+static void pm805_irq_disable(struct irq_data *data)
+{
+	struct pm80x_chip *chip = irq_data_get_irq_chip_data(data);
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	int offset = data->irq - pm805_chip->irq_base;
+	if ((offset >= 0) && (offset < ARRAY_SIZE(pm805_irqs)))
+		pm805_irqs[offset].enable = 0;
+}
+
+static struct irq_chip pm805_irq_chip = {
+	.name = "88pm805",
+	.irq_bus_lock = pm805_irq_lock,
+	.irq_bus_sync_unlock = pm805_irq_sync_unlock,
+	.irq_enable = pm805_irq_enable,
+	.irq_disable = pm805_irq_disable,
+};
+
+static int __devinit device_gpadc_init(struct pm80x_chip *chip,
+				       struct pm80x_platform_data *pdata)
+{
+	struct i2c_client *i2c_gpadc = chip->gpadc_page;
+	int data = 0, mask = 0, ret = 0;
+
+	if (!i2c_gpadc) {
+		dev_warn(chip->dev,
+			 "Warning: I2C gpdac page is not available!\n");
+		return -EINVAL;
+	}
+	/* initialize GPADC without activating it */
+	/* turn on GPADC measurments */
+	ret = pm80x_set_bits(i2c_gpadc,
+			     PM800_GPADC_MISC_CONFIG2,
+			     PM800_GPADC_MISC_GPFSM_EN,
+			     PM800_GPADC_MISC_GPFSM_EN);
+	if (ret < 0)
+		goto out;
+	/*
+	   This function configures the ADC as requires for
+	   CP implementation.CP does not "own" the ADC configuration
+	   registers and relies on AP.
+	   Reason: enable automatic ADC measurements needed
+	   for CP to get VBAT and RF temperature readings.
+	 */
+	ret = pm80x_set_bits(i2c_gpadc, PM800_GPADC_MEAS_EN1,
+			     PM800_MEAS_EN1_VBAT, PM800_MEAS_EN1_VBAT);
+	if (ret < 0)
+		goto out;
+	ret = pm80x_set_bits(i2c_gpadc, PM800_GPADC_MEAS_EN2,
+			     (PM800_MEAS_EN2_RFTMP | PM800_MEAS_GP0_EN),
+			     (PM800_MEAS_EN2_RFTMP | PM800_MEAS_GP0_EN));
+	if (ret < 0)
+		goto out;
+
+	/* the defult of PM800 is GPADC operates@100Ks/s rate */
+	/* and Number of GPADC slots with active current bias prior to
+	   GPADC sampling = 1 slot for all GPADCs */
+
+	/* set for Temprature mesurmants */
+	mask = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN1 |
+		PM800_GPADC_GP_BIAS_EN2 | PM800_GPADC_GP_BIAS_EN3);
+
+	if (pdata && (pdata->batt_det == 0)) {
+		data = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN1 |
+			PM800_GPADC_GP_BIAS_EN2 | PM800_GPADC_GP_BIAS_EN3);
+	} else {
+		data = (PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_EN2 |
+			PM800_GPADC_GP_BIAS_EN3);
+	}
+	ret = pm80x_set_bits(i2c_gpadc, PM800_GP_BIAS_ENA1, mask, data);
+	if (ret < 0)
+		goto out;
+
+	dev_info(chip->dev, "pm80x device_gpadc_init: Done\n");
+	return 0;
+
+out:
+	dev_info(chip->dev, "pm80x device_gpadc_init: Failed!\n");
+	return ret;
+}
+
+static void genirq_init_800(struct pm80x_chip *chip, int irq_base)
+{
+	int i, __irq;
+	for (i = 0; i < ARRAY_SIZE(pm800_irqs); i++) {
+		__irq = i + irq_base;
+		irq_set_chip_data(__irq, chip);
+		irq_set_chip_and_handler(__irq, &pm800_irq_chip,
+					 handle_edge_irq);
+		irq_set_nested_thread(__irq, 1);
+#ifdef CONFIG_ARM
+		set_irq_flags(__irq, IRQF_VALID);
+#else
+		irq_set_noprobe(__irq);
+#endif
+	}
+}
+
+static void genirq_exit_800(int irq_base)
+{
+	int i, __irq;
+	for (i = 0; i < ARRAY_SIZE(pm800_irqs); i++) {
+		__irq = i + irq_base;
+#ifdef CONFIG_ARM
+		set_irq_flags(__irq, 0);
+#endif
+		irq_set_chip_and_handler(__irq, NULL, NULL);
+		irq_set_chip_data(__irq, NULL);
+	}
+}
+
+static int __devinit device_irq_init_800(struct pm80x_chip *chip,
+					 struct pm80x_platform_data *pdata)
+{
+	struct pm80x_subchip *pm800_chip = chip->pm800_chip;
+	struct i2c_client *i2c_base = chip->base_page;
+	unsigned char status_buf[PM800_INT_REG_NUM];
+	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
+	struct irq_desc *desc;
+	int data, mask, ret = -EINVAL;
+	int irq, irq_base;
+
+	if (!pdata) {
+		dev_err(chip->dev, "missing platform data\n");
+		return -EINVAL;
+	}
+	if (!i2c_base) {
+		dev_err(chip->dev, "missing base_page\n");
+		return -EINVAL;
+	}
+
+	irq = pm800_chip->irq;
+	irq_base = pm800_chip->irq_base;
+
+	if (!irq) {
+		dev_err(chip->dev, "No interrupt IRQ for pm800\n");
+		return -EINVAL;
+	}
+
+	mask = PM800_WAKEUP2_INV_INT | PM800_WAKEUP2_INT_CLEAR
+	    | PM800_WAKEUP2_INT_MASK;
+	data = 0;
+	if (pdata && pdata->irq_mode) {
+		/*
+		 * irq_mode defines the way of clearing interrupt. If it's 1,
+		 * clear IRQ by write. Otherwise, clear it by read.
+		 */
+		data |= PM800_WAKEUP2_INT_CLEAR;
+		pm800_chip->irq_mode = 1;
+	}
+	ret = pm80x_set_bits(i2c_base, PM800_WAKEUP2, mask, data);
+	if (ret < 0)
+		goto out;
+
+	/* mask all IRQs */
+	memset(status_buf, 0, PM800_INT_REG_NUM);
+	ret = pm80x_bulk_write(i2c_base, PM800_INT_ENA_1,
+			       PM800_INT_REG_NUM, status_buf);
+	if (ret < 0)
+		goto out;
+
+	if (pm800_chip->irq_mode) {
+		/* clear interrupt status by write */
+		memset(status_buf, 0xFF, PM800_INT_REG_NUM);
+		ret = pm80x_bulk_write(i2c_base, PM800_INT_STATUS1,
+				       PM800_INT_REG_NUM, status_buf);
+	} else {
+		/* clear interrupt status by read */
+		ret = pm80x_bulk_read(i2c_base, PM800_INT_STATUS1,
+				      PM800_INT_REG_NUM, status_buf);
+	}
+	if (ret < 0)
+		goto out;
+
+	mutex_init(&chip->pm800_irq_lock);
+
+	desc = irq_to_desc(irq);
+	pm800_irq_chip.irq_set_wake = desc->irq_data.chip->irq_set_wake;
+
+	/* Register IRQ by genirq */
+	genirq_init_800(chip, irq_base);
+	/* Request IRQ */
+	ret = request_threaded_irq(irq, NULL, pm800_irq, flags,
+				   "88pm800", chip);
+	if (ret) {
+		pm800_chip->irq = 0;
+		dev_err(chip->dev, "Failed to request pm800 IRQ: %d\n", ret);
+		goto out_request_irq;
+	}
+	return 0;
+
+out_request_irq:
+	genirq_exit_800(irq_base);
+out:
+	return ret;
+}
+
+static void device_irq_exit_800(struct pm80x_chip *chip)
+{
+	if (chip->pm800_chip && chip->pm800_chip->irq) {
+		free_irq(chip->pm800_chip->irq, chip);
+		genirq_exit_800(chip->pm800_chip->irq_base);
+	}
+}
+
+static void genirq_init_805(struct pm80x_chip *chip, int irq_base)
+{
+	int i, __irq;
+	for (i = 0; i < ARRAY_SIZE(pm805_irqs); i++) {
+		__irq = i + irq_base;
+		irq_set_chip_data(__irq, chip);
+		irq_set_chip_and_handler(__irq, &pm805_irq_chip,
+					 handle_edge_irq);
+		irq_set_nested_thread(__irq, 1);
+#ifdef CONFIG_ARM
+		set_irq_flags(__irq, IRQF_VALID);
+#else
+		irq_set_noprobe(__irq);
+#endif
+	}
+}
+
+static void genirq_exit_805(int irq_base)
+{
+	int i, __irq;
+	for (i = 0; i < ARRAY_SIZE(pm805_irqs); i++) {
+		__irq = i + irq_base;
+#ifdef CONFIG_ARM
+		set_irq_flags(__irq, 0);
+#endif
+		irq_set_chip_and_handler(__irq, NULL, NULL);
+		irq_set_chip_data(__irq, NULL);
+	}
+}
+
+static int __devinit device_irq_init_805(struct pm80x_chip *chip,
+					 struct pm80x_platform_data *pdata)
+{
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct i2c_client *i2c = pm805_chip->client;
+	unsigned char status_buf[PM805_INT_REG_NUM];
+	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
+	struct irq_desc *desc;
+	int data, mask, ret = -EINVAL;
+	int irq, irq_base;
+
+	if (!pdata) {
+		dev_warn(chip->dev, "missing platform data\n");
+		return -EINVAL;
+	}
+
+	irq = pm805_chip->irq;
+	irq_base = pm805_chip->irq_base;
+	if (!irq) {
+		dev_warn(chip->dev, "No interrupt IRQ for pm805\n");
+		return -EINVAL;
+	}
+	mask = PM805_STATUS0_INT_CLEAR | PM805_STATUS0_INV_INT
+	    | PM800_STATUS0_INT_MASK;
+
+	data = 0;
+
+	if (pdata->irq_mode) {
+		data |= PM805_STATUS0_INT_CLEAR;
+		pm805_chip->irq_mode = 1;
+	}
+
+	ret = pm80x_set_bits(i2c, PM805_INT_STATUS0, mask, data);
+	if (ret < 0)
+		goto out;
+
+	/* Need to use delay between accesses to 32K-registers */
+	msleep(1);
+
+	/* mask all IRQs */
+	memset(status_buf, 0, PM805_INT_REG_NUM);
+	ret = pm80x_bulk_write(i2c, PM805_INT_MASK1,
+			       PM805_INT_REG_NUM, status_buf);
+	if (ret < 0)
+		goto out;
+
+	/* Need to use delay between accesses to 32K-registers */
+	msleep(1);
+
+	if (pm805_chip->irq_mode) {
+		/* clear interrupt status by write */
+		memset(status_buf, 0xFF, PM805_INT_REG_NUM);
+		ret = pm80x_bulk_write(i2c, PM805_INT_STATUS1,
+				       PM805_INT_REG_NUM, status_buf);
+	} else {
+		/* clear interrupt status by read */
+		ret = pm80x_bulk_read(i2c, PM805_INT_STATUS1,
+				      PM805_INT_REG_NUM, status_buf);
+	}
+	if (ret < 0)
+		goto out;
+
+	mutex_init(&chip->pm805_irq_lock);
+
+	if (!irq)
+		goto out;
+
+	desc = irq_to_desc(irq);
+	pm805_irq_chip.irq_set_wake = desc->irq_data.chip->irq_set_wake;
+
+	/* Register IRQ by genirq */
+	genirq_init_805(chip, irq_base);
+	/* Request IRQ */
+	ret = request_threaded_irq(irq, NULL, pm805_irq, flags,
+				   "88pm805", chip);
+	if (ret) {
+		pm805_chip->irq = 0;
+		dev_err(chip->dev, "Failed to request pm805 IRQ: %d\n", ret);
+		goto out_request_irq;
+	}
+	return 0;
+
+out_request_irq:
+	genirq_exit_805(irq_base);
+out:
+	return ret;
+}
+
+static void device_irq_exit_805(struct pm80x_chip *chip)
+{
+	if (chip->pm805_chip && chip->pm805_chip->irq) {
+		free_irq(chip->pm805_chip->irq, chip);
+		genirq_exit_805(chip->pm805_chip->irq_base);
+	}
+}
+
+static int __devinit device_805_init(struct pm80x_chip *chip,
+				     struct i2c_client *i2c,
+				     struct pm80x_platform_data *pdata)
+{
+	int ret = 0;
+	struct pm80x_subchip *pm805_chip;
+
+	dev_info(chip->dev, "pm80x:%s slave addr[0x%x]\n", __func__, i2c->addr);
+
+	/* Init PM805 subchip */
+	pm805_chip =
+	    devm_kzalloc(chip->dev, sizeof(struct pm80x_subchip), GFP_KERNEL);
+	if (!pm805_chip)
+		return -ENOMEM;
+	chip->pm805_chip = pm805_chip;
+	pm805_chip->dev = chip->dev;
+	pm805_chip->chip = chip;
+	pm805_chip->pdata = pdata;
+	pm805_chip->client = i2c;
+	pm805_chip->irq = chip->irq_pm805;
+	pm805_chip->irq_base = chip->irq_pm805_base;
+
+	ret = pm80x_reg_read(i2c, PM805_CHIP_ID);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
+		goto out_chip_id;
+	}
+	chip->chip805_version = ret;
+
+	ret = device_irq_init_805(chip, pdata);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to init pm805 irq!\n");
+		goto out_irq_init;
+	}
+
+	ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
+			      ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to add codec subdev\n");
+		goto out_codec;
+	} else
+		dev_info(chip->dev, "[%s]:Added mfd codec_devs\n", __func__);
+
+	if (pdata->pm805_plat_config)
+		pdata->pm805_plat_config(chip, pdata);
+
+	return 0;
+
+out_codec:
+	device_irq_exit_805(chip);
+out_irq_init:
+out_chip_id:
+	devm_kfree(chip->dev, chip->pm805_chip);
+	chip->pm805_chip = NULL;
+	return ret;
+}
+
+static int __devinit device_800_init(struct pm80x_chip *chip,
+				     struct i2c_client *i2c,
+				     struct pm80x_platform_data *pdata)
+{
+	struct i2c_client *i2c_base = chip->base_page;
+	struct pm80x_subchip *pm800_chip;
+	int ret, pmic_id;
+
+	if (!i2c_base) {
+		dev_err(chip->dev, "base_page is invalid\n");
+		return -EINVAL;
+	}
+
+	/* Init PM800 subchip */
+	pm800_chip =
+	    devm_kzalloc(chip->dev, sizeof(struct pm80x_subchip), GFP_KERNEL);
+	if (!pm800_chip)
+		return -ENOMEM;
+	chip->pm800_chip = pm800_chip;
+
+	pm800_chip->dev = chip->dev;
+	pm800_chip->chip = chip;
+	pm800_chip->pdata = pdata;
+	pm800_chip->client = i2c;
+	pm800_chip->irq = chip->irq_pm800;
+	pm800_chip->irq_base = chip->irq_pm800_base;
+
+	ret = pm80x_reg_read(i2c, PM800_CHIP_ID);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
+		goto out;
+	}
+
+	pmic_id = ret & PM80X_VERSION_MASK;
+
+	if ((pmic_id >= PM800_CHIP_A0) && (pmic_id <= PM800_CHIP_END)) {
+		chip->chip800_version = ret;
+		dev_info(chip->dev,
+			 "88PM80x:Marvell 88PM800 (ID:0x%x) detected\n", ret);
+	} else {
+		dev_err(chip->dev,
+			"Failed to detect Marvell 88PM800:ChipID[0x%x]\n", ret);
+		goto out;
+	}
+
+	/*
+	 * alarm wake up bit will be clear in device_irq_init(),
+	 * read before that
+	 */
+	ret = pm80x_reg_read(chip->base_page, PM800_RTC_CONTROL);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to read RTC register: %d\n", ret);
+		goto out;
+	}
+	if (ret & PM800_ALARM_WAKEUP) {
+		if (pdata && pdata->rtc)
+			pdata->rtc->rtc_wakeup = 1;
+	}
+
+	ret = device_gpadc_init(chip, pdata);
+	if (ret < 0) {
+		dev_err(chip->dev, "[%s]Failed to init gpadc\n", __func__);
+		goto out;
+	}
+
+	ret = device_irq_init_800(chip, pdata);
+	if (ret < 0) {
+		dev_err(chip->dev, "[%s]Failed to init pm800 irq\n", __func__);
+		goto out;
+	}
+
+	ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
+			      ARRAY_SIZE(onkey_devs), &onkey_resources[0], pm800_chip->irq_base);
+	if (ret < 0) {
+		dev_err(chip->dev, "Failed to add onkey subdev\n");
+		goto out_dev;
+	} else
+		dev_info(chip->dev, "[%s]:Added mfd onkey_devs\n", __func__);
+
+	if (pdata && pdata->rtc) {
+		rtc_devs[0].platform_data = pdata->rtc;
+		rtc_devs[0].pdata_size = sizeof(struct pm80x_rtc_pdata);
+		ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
+				      ARRAY_SIZE(rtc_devs), NULL,
+				      pm800_chip->irq_base);
+		if (ret < 0) {
+			dev_err(chip->dev, "Failed to add rtc subdev\n");
+			goto out_dev;
+		} else
+			dev_info(chip->dev,
+				 "[%s]:Added mfd rtc_devs\n", __func__);
+	}
+
+	if (pdata->pm800_plat_config)
+		pdata->pm800_plat_config(chip, pdata);
+
+	return 0;
+out_dev:
+	mfd_remove_devices(chip->dev);
+	device_irq_exit_800(chip);
+out:
+	devm_kfree(chip->dev, chip->pm800_chip);
+	chip->pm800_chip = NULL;
+	return ret;
+}
+
+int __devinit pm80x_device_init(struct pm80x_chip *chip,
+				struct pm80x_platform_data *pdata)
+{
+	int ret = 0;
+
+	switch (chip->id) {
+	case CHIP_PM800:
+		/* set PM800 as main chip */
+		ret = device_800_init(chip, chip->client_pm800, pdata);
+		if (ret) {
+			dev_err(chip->dev, "%s failed!\n", __func__);
+			return ret;
+		}
+
+		if (chip->client_pm805)
+			ret = device_805_init(chip, chip->client_pm805, pdata);
+
+		break;
+	case CHIP_PM805:
+		/* set PM805 as main chip */
+		ret = device_805_init(chip, chip->client_pm805, pdata);
+		break;
+	}
+	if (ret) {
+		dev_err(chip->dev, "%s failed!\n", __func__);
+		return ret;
+	}
+
+	return ret;
+}
+
+void __devexit pm80x_device_exit(struct pm80x_chip *chip)
+{
+	if (chip->pm800_chip) {
+		device_irq_exit_800(chip);
+		devm_kfree(chip->dev, chip->pm800_chip);
+	}
+	if (chip->pm805_chip) {
+		device_irq_exit_805(chip);
+		devm_kfree(chip->dev, chip->pm805_chip);
+	}
+	mfd_remove_devices(chip->dev);
+}
+
+MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM80x");
+MODULE_AUTHOR("Qiao Zhou <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/mfd/88pm80x-i2c.c new file +370 -0

370 lines not on this page · raw message

drivers/mfd/Kconfig +11 -0
index e129c82..b083111 100644
@@ -20,6 +20,17 @@ config MFD_88PM860X
 	  select individual components like voltage regulators, RTC and
 	  battery-charger under the corresponding menus.
 
+config MFD_88PM80X
+	bool "Support Marvell 88PM800/88PM805"
+	depends on I2C=y && GENERIC_HARDIRQS
+	select REGMAP_I2C
+	select MFD_CORE
+	help
+	  This supports for Marvell 88PM800/88PM805 Power Management IC.
+	  This includes the I2C driver and the core APIs _only_, you have to
+	  select individual components like voltage regulators, RTC and
+	  battery-charger under the corresponding menus.
+
 config MFD_SM501
 	tristate "Support for Silicon Motion SM501"
 	 ---help---
drivers/mfd/Makefile +2 -0
index 75f6ed6..bf5de13 100644
@@ -3,7 +3,9 @@
 #
 
 88pm860x-objs			:= 88pm860x-core.o 88pm860x-i2c.o
+88pm80x-objs			:= 88pm80x-core.o 88pm80x-i2c.o
 obj-$(CONFIG_MFD_88PM860X)	+= 88pm860x.o
+obj-$(CONFIG_MFD_88PM80X)	+= 88pm80x.o
 obj-$(CONFIG_MFD_SM501)		+= sm501.o
 obj-$(CONFIG_MFD_ASIC3)		+= asic3.o tmio_core.o
 
include/linux/mfd/88pm80x.h new file +701 -0

701 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#2 lore raw

PATCH 1/3 mfd: support 88pm80x in 80x driver

Quoted 62 lines · Qiao Zhou

88PM800 and 88PM805 are two discrete chips used for power management. Hardware designer can use them together or only one of them according to requirement.

There's some logic tightly linked between these two chips. For example, USB charger driver needs to access both chips by I2C interface.

Now share one driver to these two devices. Only one I2C client is identified in platform init data. If one chip is also used, user should mark it with related i2c_addr field of platform init data. Then driver could create another I2C client for the chip.

All I2C operations are accessed by 80x-i2c driver. In order to support all I2C client address, the read/write API is changed in below.

reg_read(client, offset)
reg_write(client, offset, data)

The benefit is that client drivers only need one kind of read/write API. I2C and MFD driver can be shared in both 800 and 805.

 drivers/mfd/88pm80x-core.c  | 1002 +++++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/88pm80x-i2c.c   |  370 ++++++++++++++++
 drivers/mfd/Kconfig         |   11 +
 drivers/mfd/Makefile        |    2 +
 include/linux/mfd/88pm80x.h |  701 ++++++++++++++++++++++++++++++
 5 files changed, 2086 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mfd/88pm80x-core.c
 create mode 100644 drivers/mfd/88pm80x-i2c.c
 create mode 100644 include/linux/mfd/88pm80x.h
drivers/mfd/88pm80x-core.c new file +26 -0
+
+static struct resource rtc_resources[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .start = PM800_IRQ_RTC,
+	 .end = PM800_IRQ_RTC,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell rtc_devs[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .num_resources = ARRAY_SIZE(rtc_resources),
+	 .resources = &rtc_resources[0],
+	 .id = -1,
+	 },
+};
+
+static struct resource onkey_resources[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .start = PM800_IRQ_ONKEY,
+	 .end = PM800_IRQ_ONKEY,
+	 .flags = IORESOURCE_IRQ,
+	 },

drivers/mfd/88pm80x-core.c line 68

Quoted 10 lines
drivers/mfd/88pm80x-core.c new file +10 -0
+};
+
+static struct mfd_cell onkey_devs[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .num_resources = 1,
+	 .resources = &onkey_resources[0],
+	 .id = -1,
+	 },
+};

I wonder if it really makes sense to use the mfd_cell abstraction here, when each array only contains a single device. Why not just use platform_device_register_simple()?

Quoted 12 lines
drivers/mfd/88pm80x-core.c new file +12 -0
+
+static struct pm80x_irq_data pm805_irqs[] = {
+	[PM805_IRQ_LDO_OFF] = {	/*0 */
+			       .reg = PM805_INT_STATUS1,
+			       .mask_reg = PM805_INT_MASK1,
+			       .offs = 1 << 5,
+			       },
+	[PM805_IRQ_SRC_DPLL_LOCK] = {
+				     .reg = PM805_INT_STATUS1,
+				     .mask_reg = PM805_INT_MASK1,
+				     .offs = 1 << 4,
+				     },

drivers/mfd/88pm80x-core.c line 338

Quoted 10 lines
drivers/mfd/88pm80x-core.c new file +10 -0
+	[PM805_IRQ_CLIP_FAULT] = {
+				  .reg = PM805_INT_STATUS1,
+				  .mask_reg = PM805_INT_MASK1,
+				  .offs = 1 << 3,
+				  },
+	[PM805_IRQ_MIC_CONFLICT] = {
+				    .reg = PM805_INT_STATUS1,
+				    .mask_reg = PM805_INT_MASK1,
+				    .offs = 1 << 2,
+				    },

[coding style] This uses a very unusual indentation. Just use a single tab to indent the fields in each of the irq data descriptions.

Quoted 7 lines
drivers/mfd/88pm80x-core.c new file +7 -0
+static inline struct pm80x_irq_data *irq_to_pm805(struct pm80x_chip *chip,
+						  int irq)
+{
+	int offset = irq - chip->pm805_chip->irq_base;
+	if (!chip->pm805_chip || (offset < 0)
+	    || (offset >= ARRAY_SIZE(pm805_irqs)))
+		return NULL;

drivers/mfd/88pm80x-core.c line 397

Quoted 10 lines
drivers/mfd/88pm80x-core.c new file +10 -0
+	return &pm805_irqs[offset];
+}
+
+static irqreturn_t pm805_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	int i, read_reg = -1, value = 0;

The functions for pm800 and pm805 look almost identical. Have you tried consolidating them so you can use the same irqchip and code with different init data structures?

Quoted 14 lines
drivers/mfd/88pm80x-i2c.c new file +14 -0
+int pm80x_reg_read(struct i2c_client *i2c, int reg)
+{
+	struct regmap *map = verify_regmap(i2c);
+	unsigned int data;
+	int ret;
+
+	ret = regmap_read(map, reg, &data);
+	if (ret < 0)
+		return ret;
+	else
+		return (int)data;
+}
+EXPORT_SYMBOL(pm80x_reg_read);
+

drivers/mfd/88pm80x-i2c.c line 72

Quoted 10 lines
drivers/mfd/88pm80x-i2c.c new file +10 -0
+int pm80x_reg_write(struct i2c_client *i2c, int reg,
+		     unsigned char data)
+{
+	struct regmap *map = verify_regmap(i2c);
+	int ret;
+
+	ret = regmap_write(map, reg, data);
+	return ret;
+}
+EXPORT_SYMBOL(pm80x_reg_write);

These functions should be EXPORT_SYMBOL_GPL if you really need them. My impression however is that you'd be better off without them and just let the client use the regmap directly after you export the verify_regmap function under a more appropriate name.

Quoted 17 lines
drivers/mfd/88pm80x-i2c.c new file +17 -0
+static const struct i2c_device_id pm80x_id_table[] = {
+	{"88PM80x", 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
+
+static int verify_addr(struct i2c_client *i2c)
+{
+	unsigned short addr_800[] = { 0x30, 0x34 };
+	unsigned short addr_805[] = { 0x38, 0x39 };
+	int size, i;
+
+	if (i2c == NULL)
+		return 0;
+	size = ARRAY_SIZE(addr_800);
+	for (i = 0; i < size; i++) {

drivers/mfd/88pm80x-i2c.c line 142

Quoted 10 lines
drivers/mfd/88pm80x-i2c.c new file +10 -0
+		if (i2c->addr == *(addr_800 + i))
+			return CHIP_PM800;
+	}
+	size = ARRAY_SIZE(addr_805);
+	for (i = 0; i < size; i++) {
+		if (i2c->addr == *(addr_805 + i))
+			return CHIP_PM805;
+	}
+	return 0;
+}

I think you should just put the devices separately into the device tree so they get probed independently. Hardcoding the address in the source code does not seem appropriate. Also, I would expect that the device name is already the specific one (88pm800 or 88pm805) in the device tree. Just put both in the pm80x_id_table and use the driver_data field to distinguish them.

Quoted 10 lines
drivers/mfd/88pm80x-i2c.c new file +10 -0
+static int pm80x_pages_init(struct pm80x_chip *chip, struct i2c_client *client,
+			    struct pm80x_platform_data *pdata)
+{
+	/*
+	 * Both pm800 and pm805 shares the same platform driver.
+	 * check whether we have have pm805 driver.
+	 */
+	if (pdata->pm805_addr) {
+		chip->pm805_addr = pdata->pm805_addr;
+		/* in case we only have pm805, it already registers

drivers/mfd/88pm80x-i2c.c line 163

Quoted 10 lines
drivers/mfd/88pm80x-i2c.c new file +10 -0
+		 * i2c handle, then no need to register again.
+		 */
+		if (chip->id != CHIP_PM805) {
+			chip->client_pm805 =
+			    i2c_new_dummy(client->adapter, chip->pm805_addr);
+			chip->regmap_pm805 = devm_regmap_init_i2c(chip->client_pm805, &pm80x_regmap_config);
+			i2c_set_clientdata(chip->client_pm805, chip);
+
+			device_init_wakeup(&chip->client_pm805->dev, 1);
+		}

No reason to put the address into platform_data, just use the regular i2c device address.

include/linux/mfd/88pm80x.h line 611

Quoted 9 lines
include/linux/mfd/88pm80x.h new file +9 -0
+struct pm80x_subchip {
+	struct device *dev;
+	struct pm80x_chip *chip;
+	struct pm80x_platform_data *pdata;
+	struct i2c_client *client;
+	int irq;
+	int irq_base;
+	int irq_mode;
+};

What is a "subchip"?

Quoted 23 lines
include/linux/mfd/88pm80x.h new file +23 -0
+struct pm80x_chip {
+	/*chip_version can only on the top of the struct*/
+	unsigned char chip800_version;
+	unsigned char chip805_version;
+	struct device *dev;
+	struct pm80x_subchip *pm800_chip;
+	struct pm80x_subchip *pm805_chip;
+	struct mutex io_lock;
+	struct mutex pm800_irq_lock;
+	struct mutex pm805_irq_lock;
+	struct i2c_client *client_pm800;
+	struct i2c_client *client_pm805;
+	struct i2c_client *base_page;	/* chip client for base page */
+	struct i2c_client *power_page;	/* chip client for power page */
+	struct i2c_client *gpadc_page;	/* chip client for gpadc page */
+	struct regmap *regmap_pm800;
+	struct regmap *regmap_pm805;
+	struct regmap *regmap_power;
+	struct regmap *regmap_gpadc;
+
+	unsigned short pm800_addr;
+	unsigned short pm805_addr;
+	unsigned short base_page_addr;	/* base page I2C address */

include/linux/mfd/88pm80x.h line 645

Quoted 10 lines
include/linux/mfd/88pm80x.h new file +10 -0
+	unsigned short power_page_addr;	/* power page I2C address */
+	unsigned short gpadc_page_addr;	/* gpadc page I2C address */
+	int id;
+	int irq_pm800;
+	int irq_pm805;
+	int irq_pm800_base;
+	int irq_pm805_base;
+	unsigned int wu_flag_pm800;
+	unsigned int wu_flag_pm805;
+};

You have duplicated almost every field in this structure, which appears to be very suboptimal. Better put all common fields into one structure and then make a derived data structure for the specific ones, like

struct pm80x_chip {
	char version;
	struct pm80x_subchip *subchip;
	struct mutex irq_lock;
	struct i2c_client i2c;
	...
	unsigned int wu_flag;
};
struct pm800_chip {
	struct pm80x_chip chip;
	struct i2c_client *base_page;
	...
};
struct pm805_chip {
	struct pm80x_chip chip;
	struct i2c_client *power_page;
	...
};
Quoted 17 lines
include/linux/mfd/88pm80x.h new file +17 -0
+struct pm80x_rtc_pdata {
+	int		(*sync)(unsigned int ticks);
+	int		vrtc;
+	int             rtc_wakeup;
+};
+
+struct pm80x_platform_data {
+	struct pm80x_rtc_pdata *rtc;
+	unsigned short pm800_addr;
+	unsigned short pm805_addr;
+	unsigned short base_page_addr;	/* base page I2C address */
+	unsigned short power_page_addr;	/* power page I2C address */
+	unsigned short gpadc_page_addr;	/* gpadc page I2C address */
+	unsigned short test_page_addr;	/* test page regs I2C address */
+	int i2c_port;		/* Controlled by GI2C or PI2C */
+	int irq_mode;		/* Clear interrupt by read/write(0/1) */
+	int irq_pm800;		/* IRQ of 88pm800 */

include/linux/mfd/88pm80x.h line 686

Quoted 10 lines
include/linux/mfd/88pm80x.h new file +10 -0
+	int irq_pm805;		/* IRQ of 88pm805 */
+	int irq_pm800_base;		/* IRQ base number of 88pm800 */
+	int irq_pm805_base;		/* IRQ base number of 88pm805 */
+	int batt_det;		/* enable/disable */
+
+	int (*pm800_plat_config)(struct pm80x_chip *chip,
+				struct pm80x_platform_data *pdata);
+	int (*pm805_plat_config)(struct pm80x_chip *chip,
+				struct pm80x_platform_data *pdata);
+};

The *_plat_config fields are completely unused here, better remove them.

The interrupt numbers should become irq resources.

Any field in the platform data that cannot be converted to a resource or completely removed should have a respective property in the device tree binding for this device.

Arnd

#3 lore raw

PATCH 1/3 mfd: support 88pm80x in 80x driver

Quoted 14 lines · Arnd Bergmann

drivers/mfd/88pm80x-core.c line 68

Quoted 8 lines · Qiao Zhou
drivers/mfd/88pm80x-core.c new file +8 -0
+static struct mfd_cell onkey_devs[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .num_resources = 1,
+	 .resources = &onkey_resources[0],
+	 .id = -1,
+	 },
+};

I wonder if it really makes sense to use the mfd_cell abstraction here, when each array only contains a single device. Why not just use platform_device_register_simple()?

mfd_cell does some other stuff like let you pass in bases for the various resource ranges when you register which looks to be being used here.

Quoted 11 lines

drivers/mfd/88pm80x-core.c line 397

Quoted 7 lines
drivers/mfd/88pm80x-core.c new file +7 -0
+static irqreturn_t pm805_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	int i, read_reg = -1, value = 0;

The functions for pm800 and pm805 look almost identical. Have you tried consolidating them so you can use the same irqchip and code with different init data structures?

Since you use regmap perhaps you can use regmap_irq and just convert them into data?

author
#4 lore raw

PATCH 1/3 mfd: support 88pm80x in 80x driver

Quoted 31 lines · Mark Brown
Quoted 14 lines · Arnd Bergmann

drivers/mfd/88pm80x-core.c line 68

Quoted 8 lines · Qiao Zhou
drivers/mfd/88pm80x-core.c new file +8 -0
+static struct mfd_cell onkey_devs[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .num_resources = 1,
+	 .resources =&onkey_resources[0],
+	 .id = -1,
+	 },
+};

I wonder if it really makes sense to use the mfd_cell abstraction here, when each array only contains a single device. Why not just use platform_device_register_simple()?

mfd_cell does some other stuff like let you pass in bases for the various resource ranges when you register which looks to be being used here.

Quoted 11 lines

drivers/mfd/88pm80x-core.c line 397

Quoted 7 lines
drivers/mfd/88pm80x-core.c new file +7 -0
+static irqreturn_t pm805_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	int i, read_reg = -1, value = 0;

The functions for pm800 and pm805 look almost identical. Have you tried consolidating them so you can use the same irqchip and code with different init data structures?

Quoted 2 lines

Since you use regmap perhaps you can use regmap_irq and just convert them into data?

Mark,

thanks for your suggestion, and would update accordingly.

Signature · 2 lines
Best Regards
Qiao
author
#5 lore raw

PATCH 1/3 mfd: support 88pm80x in 80x driver

In reply to Arnd Bergmann
Quoted 69 lines · Arnd Bergmann
Quoted 68 lines · Qiao Zhou

88PM800 and 88PM805 are two discrete chips used for power management. Hardware designer can use them together or only one of them according to requirement.

There's some logic tightly linked between these two chips. For example, USB charger driver needs to access both chips by I2C interface.

Now share one driver to these two devices. Only one I2C client is identified in platform init data. If one chip is also used, user should mark it with related i2c_addr field of platform init data. Then driver could create another I2C client for the chip.

All I2C operations are accessed by 80x-i2c driver. In order to support all I2C client address, the read/write API is changed in below.

reg_read(client, offset)
reg_write(client, offset, data)

The benefit is that client drivers only need one kind of read/write API. I2C and MFD driver can be shared in both 800 and 805.

  drivers/mfd/88pm80x-core.c  | 1002 +++++++++++++++++++++++++++++++++++++++++++
  drivers/mfd/88pm80x-i2c.c   |  370 ++++++++++++++++
  drivers/mfd/Kconfig         |   11 +
  drivers/mfd/Makefile        |    2 +
  include/linux/mfd/88pm80x.h |  701 ++++++++++++++++++++++++++++++
  5 files changed, 2086 insertions(+), 0 deletions(-)

create mode 100644 drivers/mfd/88pm80x-core.c
create mode 100644 drivers/mfd/88pm80x-i2c.c
create mode 100644 include/linux/mfd/88pm80x.h

drivers/mfd/88pm80x-core.c new file +32 -0
+
+static struct resource rtc_resources[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .start = PM800_IRQ_RTC,
+	 .end = PM800_IRQ_RTC,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell rtc_devs[] = {
+	{
+	 .name = "88pm80x-rtc",
+	 .num_resources = ARRAY_SIZE(rtc_resources),
+	 .resources =&rtc_resources[0],
+	 .id = -1,
+	 },
+};
+
+static struct resource onkey_resources[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .start = PM800_IRQ_ONKEY,
+	 .end = PM800_IRQ_ONKEY,
+	 .flags = IORESOURCE_IRQ,
+	 },
+};
+
+static struct mfd_cell onkey_devs[] = {
+	{
+	 .name = "88pm80x-onkey",
+	 .num_resources = 1,
Quoted 8 lines

drivers/mfd/88pm80x-core.c line 50

Quoted 4 lines
drivers/mfd/88pm80x-core.c new file +4 -0
+	 .resources =&onkey_resources[0],
+	 .id = -1,
+	 },
+};

I wonder if it really makes sense to use the mfd_cell abstraction here, when each array only contains a single device. Why not just use platform_device_register_simple()?

two reasons for using mfd_dev: firstly it's extensible if we have more onkey_devices in future; secondly it's for device registry consistency.

Quoted 18 lines
Quoted 18 lines
drivers/mfd/88pm80x-core.c new file +17 -0
+
+static struct pm80x_irq_data pm805_irqs[] = {
+	[PM805_IRQ_LDO_OFF] = {	/*0 */
+			       .reg = PM805_INT_STATUS1,
+			       .mask_reg = PM805_INT_MASK1,
+			       .offs = 1<<  5,
+			       },
+	[PM805_IRQ_SRC_DPLL_LOCK] = {
+				     .reg = PM805_INT_STATUS1,
+				     .mask_reg = PM805_INT_MASK1,
+				     .offs = 1<<  4,
+				     },
+	[PM805_IRQ_CLIP_FAULT] = {
+				  .reg = PM805_INT_STATUS1,
+				  .mask_reg = PM805_INT_MASK1,
+				  .offs = 1<<  3,
+				  },
Quoted 8 lines

drivers/mfd/88pm80x-core.c line 338

Quoted 5 lines
drivers/mfd/88pm80x-core.c new file +5 -0
+	[PM805_IRQ_MIC_CONFLICT] = {
+				    .reg = PM805_INT_STATUS1,
+				    .mask_reg = PM805_INT_MASK1,
+				    .offs = 1<<  2,
+				    },

[coding style] This uses a very unusual indentation. Just use a single tab to indent the fields in each of the irq data descriptions.

would update in next version.

Quoted 14 lines
Quoted 14 lines
drivers/mfd/88pm80x-core.c new file +13 -0
+static inline struct pm80x_irq_data *irq_to_pm805(struct pm80x_chip *chip,
+						  int irq)
+{
+	int offset = irq - chip->pm805_chip->irq_base;
+	if (!chip->pm805_chip || (offset<  0)
+	    || (offset>= ARRAY_SIZE(pm805_irqs)))
+		return NULL;
+	return&pm805_irqs[offset];
+}
+
+static irqreturn_t pm805_irq(int irq, void *data)
+{
+	struct pm80x_chip *chip = data;
Quoted 8 lines

drivers/mfd/88pm80x-core.c line 397

Quoted 4 lines
drivers/mfd/88pm80x-core.c new file +4 -0
+	struct pm80x_subchip *pm805_chip = chip->pm805_chip;
+	struct pm80x_irq_data *irq_data;
+	struct i2c_client *i2c;
+	int i, read_reg = -1, value = 0;

The functions for pm800 and pm805 look almost identical. Have you tried consolidating them so you can use the same irqchip and code with different init data structures?

would update in next version.

Quoted 22 lines
Quoted 22 lines
drivers/mfd/88pm80x-i2c.c new file +21 -0
+int pm80x_reg_read(struct i2c_client *i2c, int reg)
+{
+	struct regmap *map = verify_regmap(i2c);
+	unsigned int data;
+	int ret;
+
+	ret = regmap_read(map, reg,&data);
+	if (ret<  0)
+		return ret;
+	else
+		return (int)data;
+}
+EXPORT_SYMBOL(pm80x_reg_read);
+
+int pm80x_reg_write(struct i2c_client *i2c, int reg,
+		     unsigned char data)
+{
+	struct regmap *map = verify_regmap(i2c);
+	int ret;
+
+	ret = regmap_write(map, reg, data);
Quoted 9 lines

drivers/mfd/88pm80x-i2c.c line 72

Quoted 3 lines
drivers/mfd/88pm80x-i2c.c new file +3 -0
+	return ret;
+}
+EXPORT_SYMBOL(pm80x_reg_write);

These functions should be EXPORT_SYMBOL_GPL if you really need them. My impression however is that you'd be better off without them and just let the client use the regmap directly after you export the verify_regmap function under a more appropriate name.

would update EXPORT_SYMBOL_GPL and export related regmap API.

Quoted 26 lines
Quoted 26 lines
drivers/mfd/88pm80x-i2c.c new file +26 -0
+static const struct i2c_device_id pm80x_id_table[] = {
+	{"88PM80x", 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
+
+static int verify_addr(struct i2c_client *i2c)
+{
+	unsigned short addr_800[] = { 0x30, 0x34 };
+	unsigned short addr_805[] = { 0x38, 0x39 };
+	int size, i;
+
+	if (i2c == NULL)
+		return 0;
+	size = ARRAY_SIZE(addr_800);
+	for (i = 0; i<  size; i++) {
+		if (i2c->addr == *(addr_800 + i))
+			return CHIP_PM800;
+	}
+	size = ARRAY_SIZE(addr_805);
+	for (i = 0; i<  size; i++) {
+		if (i2c->addr == *(addr_805 + i))
+			return CHIP_PM805;
+	}
+	return 0;
Quoted 9 lines
Quoted 1 line

+}

I think you should just put the devices separately into the device tree so they get probed independently. Hardcoding the address in the source code does not seem appropriate. Also, I would expect that the device name is already the specific one (88pm800 or 88pm805) in the device tree. Just put both in the pm80x_id_table and use the driver_data field to distinguish them.

would update in next version.

Quoted 15 lines
Quoted 15 lines
drivers/mfd/88pm80x-i2c.c new file +15 -0
+static int pm80x_pages_init(struct pm80x_chip *chip, struct i2c_client *client,
+			    struct pm80x_platform_data *pdata)
+{
+	/*
+	 * Both pm800 and pm805 shares the same platform driver.
+	 * check whether we have have pm805 driver.
+	 */
+	if (pdata->pm805_addr) {
+		chip->pm805_addr = pdata->pm805_addr;
+		/* in case we only have pm805, it already registers
+		 * i2c handle, then no need to register again.
+		 */
+		if (chip->id != CHIP_PM805) {
+			chip->client_pm805 =
+			    i2c_new_dummy(client->adapter, chip->pm805_addr);
Quoted 9 lines

drivers/mfd/88pm80x-i2c.c line 163

Quoted 5 lines
drivers/mfd/88pm80x-i2c.c new file +5 -0
+			chip->regmap_pm805 = devm_regmap_init_i2c(chip->client_pm805,&pm80x_regmap_config);
+			i2c_set_clientdata(chip->client_pm805, chip);
+
+			device_init_wakeup(&chip->client_pm805->dev, 1);
+		}

No reason to put the address into platform_data, just use the regular i2c device address.

would update in next version.

Quoted 65 lines

include/linux/mfd/88pm80x.h line 611

Quoted 9 lines
include/linux/mfd/88pm80x.h new file +9 -0
+struct pm80x_subchip {
+	struct device *dev;
+	struct pm80x_chip *chip;
+	struct pm80x_platform_data *pdata;
+	struct i2c_client *client;
+	int irq;
+	int irq_base;
+	int irq_mode;
+};

What is a "subchip"?

Quoted 23 lines
include/linux/mfd/88pm80x.h new file +23 -0
+struct pm80x_chip {
+	/*chip_version can only on the top of the struct*/
+	unsigned char chip800_version;
+	unsigned char chip805_version;
+	struct device *dev;
+	struct pm80x_subchip *pm800_chip;
+	struct pm80x_subchip *pm805_chip;
+	struct mutex io_lock;
+	struct mutex pm800_irq_lock;
+	struct mutex pm805_irq_lock;
+	struct i2c_client *client_pm800;
+	struct i2c_client *client_pm805;
+	struct i2c_client *base_page;	/* chip client for base page */
+	struct i2c_client *power_page;	/* chip client for power page */
+	struct i2c_client *gpadc_page;	/* chip client for gpadc page */
+	struct regmap *regmap_pm800;
+	struct regmap *regmap_pm805;
+	struct regmap *regmap_power;
+	struct regmap *regmap_gpadc;
+
+	unsigned short pm800_addr;
+	unsigned short pm805_addr;
+	unsigned short base_page_addr;	/* base page I2C address */

include/linux/mfd/88pm80x.h line 645

Quoted 10 lines
include/linux/mfd/88pm80x.h new file +10 -0
+	unsigned short power_page_addr;	/* power page I2C address */
+	unsigned short gpadc_page_addr;	/* gpadc page I2C address */
+	int id;
+	int irq_pm800;
+	int irq_pm805;
+	int irq_pm800_base;
+	int irq_pm805_base;
+	unsigned int wu_flag_pm800;
+	unsigned int wu_flag_pm805;
+};

You have duplicated almost every field in this structure, which appears to be very suboptimal. Better put all common fields into one structure and then make a derived data structure for the specific ones, like

struct pm80x_chip {
	char version;
	struct pm80x_subchip *subchip;
	struct mutex irq_lock;
	struct i2c_client i2c;
	...
	unsigned int wu_flag;
};
struct pm800_chip {
	struct pm80x_chip chip;
	struct i2c_client *base_page;
	...
};
Quoted 6 lines
struct pm805_chip {
	struct pm80x_chip chip;
	struct i2c_client *power_page;
	...
};

you're right. the code is not well managed. would update in next version.

Quoted 30 lines
Quoted 17 lines
include/linux/mfd/88pm80x.h new file +17 -0
+struct pm80x_rtc_pdata {
+	int		(*sync)(unsigned int ticks);
+	int		vrtc;
+	int             rtc_wakeup;
+};
+
+struct pm80x_platform_data {
+	struct pm80x_rtc_pdata *rtc;
+	unsigned short pm800_addr;
+	unsigned short pm805_addr;
+	unsigned short base_page_addr;	/* base page I2C address */
+	unsigned short power_page_addr;	/* power page I2C address */
+	unsigned short gpadc_page_addr;	/* gpadc page I2C address */
+	unsigned short test_page_addr;	/* test page regs I2C address */
+	int i2c_port;		/* Controlled by GI2C or PI2C */
+	int irq_mode;		/* Clear interrupt by read/write(0/1) */
+	int irq_pm800;		/* IRQ of 88pm800 */

include/linux/mfd/88pm80x.h line 686

Quoted 10 lines
include/linux/mfd/88pm80x.h new file +10 -0
+	int irq_pm805;		/* IRQ of 88pm805 */
+	int irq_pm800_base;		/* IRQ base number of 88pm800 */
+	int irq_pm805_base;		/* IRQ base number of 88pm805 */
+	int batt_det;		/* enable/disable */
+
+	int (*pm800_plat_config)(struct pm80x_chip *chip,
+				struct pm80x_platform_data *pdata);
+	int (*pm805_plat_config)(struct pm80x_chip *chip,
+				struct pm80x_platform_data *pdata);
+};

The *_plat_config fields are completely unused here, better remove them.

Quoted 5 lines

The interrupt numbers should become irq resources.

Any field in the platform data that cannot be converted to a resource or completely removed should have a respective property in the device tree binding for this device.

would update in next version.

Quoted 2 lines

Arnd

Arnd, thanks for your careful review and great suggestions!

Signature · 2 lines
Best Regards
Qiao
author
#6 lore raw

PATCH 2/3 rtc: add rtc support to 88PM80X PMIC

In reply to Qiao Zhou

add rtc driver for MARVELL 88PM80X PMIC and enable rtc function.

---
 drivers/rtc/Kconfig       |   10 ++
 drivers/rtc/Makefile      |    1 +
 drivers/rtc/rtc-88pm80x.c |  370 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 381 insertions(+), 0 deletions(-)
 create mode 100644 drivers/rtc/rtc-88pm80x.c
drivers/rtc/Kconfig +10 -0
index 08cbdb9..f3b49f8 100644
@@ -135,6 +135,16 @@ config RTC_DRV_88PM860X
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-88pm860x.
 
+config RTC_DRV_88PM80X
+	tristate "Marvell 88PM80x"
+	depends on RTC_CLASS && I2C && MFD_88PM80X
+	help
+	  If you say yes here you get support for RTC function in Marvell
+	  88PM80x chips.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called rtc-88pm80x.
+
 config RTC_DRV_DS1307
 	tristate "Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025"
 	help
drivers/rtc/Makefile +1 -0
index 2973921..0d5b2b6 100644
@@ -16,6 +16,7 @@ rtc-core-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o
 # Keep the list ordered.
 
 obj-$(CONFIG_RTC_DRV_88PM860X)  += rtc-88pm860x.o
+obj-$(CONFIG_RTC_DRV_88PM80X)	+= rtc-88pm80x.o
 obj-$(CONFIG_RTC_DRV_AB3100)	+= rtc-ab3100.o
 obj-$(CONFIG_RTC_DRV_AB8500)	+= rtc-ab8500.o
 obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o
drivers/rtc/rtc-88pm80x.c new file +370 -0

370 lines not on this page · raw message

Signature · 1 line
1.7.4.1
author
#7 lore raw

PATCH 3/3 input: add onkey support to 88PM80X PMIC

In reply to Qiao Zhou

add onkey support to MARVELL 88PM80X PMIC.

---
 drivers/input/misc/88pm80x_onkey.c |  217 ++++++++++++++++++++++++++++++++++++
 drivers/input/misc/Kconfig         |   10 ++
 drivers/input/misc/Makefile        |    1 +
 3 files changed, 228 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/misc/88pm80x_onkey.c
drivers/input/misc/88pm80x_onkey.c new file +217 -0
new file mode 100644
index 0000000..a6ed3da
@@ -0,0 +1,217 @@
+/*
+ * 88pm80x_onkey.c - Marvell 88PM80x ONKEY driver
+ *
+ * Copyright (C) 2012 Marvell International Ltd.
+ * Haojian Zhuang <[email protected]>
+ * Qiao Zhou <[email protected]>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License. See the file "COPYING" in the main directory of this
+ * archive for more details.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/mfd/88pm80x.h>
+#include <linux/slab.h>
+
+#define PM800_LONG_ONKEY_EN		(1 << 0)
+#define PM800_LONG_KEY_DELAY		(8)	/* 1 .. 16 seconds */
+#define PM800_LONKEY_PRESS_TIME		((PM800_LONG_KEY_DELAY-1) << 4)
+#define PM800_LONKEY_PRESS_TIME_MASK	(0xF0)
+#define PM800_SW_PDOWN			(1 << 5)
+
+#define PM8607_ONKEY_STATUS		(1 << 0)
+#define PM8607_LONG_ONKEY_EN		(1 << 1)
+#define PM8607_WAKEUP				0x0b
+#define PM8607_SW_PDOWN			(1 << 7)
+
+struct pm80x_onkey_info {
+	struct input_dev *idev;
+	struct pm80x_chip *chip80x;
+	struct pm860x_chip *chip860x;
+	struct i2c_client *i2c;
+	int irq;
+	int core_irq;
+	int pmic_id;
+};
+
+static struct pm80x_onkey_info *pm80x_info;
+
+/* 88PM80x restart function */
+void pm80x_system_restart(void)
+{
+	struct i2c_client *i2c;
+	if (!pm80x_info || !pm80x_info->i2c)
+		return;
+	i2c = pm80x_info->i2c;
+	/* 1.Enable FAULT_WU and FAULT_WU_EN */
+	pm80x_set_bits(i2c, PM800_RTC_MISC5, (1 << 3), (1 << 3));
+	pm80x_set_bits(i2c, PM800_RTC_MISC5, (1 << 2), (1 << 2));
+	/* 2.Issue SW power down */
+	pm80x_set_bits(i2c, PM800_WAKEUP1, (1 << 5), (1 << 5));
+}
+
+/* 88PM80x poweroff function */
+void pm80x_system_poweroff(void)
+{
+	pm80x_set_bits(pm80x_info->i2c, PM800_WAKEUP1, PM800_SW_PDOWN,
+		       PM800_SW_PDOWN);
+}
+
+/* 88PM80x gives us an interrupt when ONKEY is held */
+static irqreturn_t pm80x_onkey_handler(int irq, void *data)
+{
+	struct pm80x_onkey_info *info = data;
+	int ret = 0;
+
+	ret = pm80x_reg_read(info->i2c, PM800_STATUS_1);
+	ret &= PM800_ONKEY_STS1;
+
+	input_report_key(info->idev, KEY_POWER, ret);
+	input_sync(info->idev);
+
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_PM
+static int pm80x_onkey_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
+
+	if (device_may_wakeup(dev))
+		chip->wu_flag_pm800 |= (1 << PM800_IRQ_ONKEY);
+
+	return 0;
+}
+
+static int pm80x_onkey_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
+
+	if (device_may_wakeup(dev))
+		chip->wu_flag_pm800 &= ~(1 << PM800_IRQ_ONKEY);
+
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(pm80x_onkey_pm_ops, pm80x_onkey_suspend,
+			 pm80x_onkey_resume);
+
+static int __devinit pm80x_onkey_probe(struct platform_device *pdev)
+{
+
+	void *chip = dev_get_drvdata(pdev->dev.parent);
+	struct pm80x_onkey_info *info;
+	int irq, ret;
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(&pdev->dev, "No IRQ resource!\n");
+		return -EINVAL;
+	}
+
+	info =
+	    devm_kzalloc(&pdev->dev, sizeof(struct pm80x_onkey_info),
+			 GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+	info->pmic_id = get_pmic_version(chip);
+	if (info->pmic_id <= PM800_CHIP_END) {
+		info->chip80x = (struct pm80x_chip *)chip;
+		info->i2c = info->chip80x->base_page;
+		info->irq = irq + info->chip80x->pm800_chip->irq_base;
+		info->core_irq = info->chip80x->pm800_chip->irq;
+	}
+	pm80x_info = info;
+	info->idev = input_allocate_device();
+	if (!info->idev) {
+		dev_err(&pdev->dev, "Failed to allocate input dev\n");
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	info->idev->name = "88pm80x_on";
+	info->idev->phys = "88pm80x_on/input0";
+	info->idev->id.bustype = BUS_I2C;
+	info->idev->dev.parent = &pdev->dev;
+	info->idev->evbit[0] = BIT_MASK(EV_KEY);
+	info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
+
+	ret = input_register_device(info->idev);
+	if (ret) {
+		dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
+		goto out_reg;
+	}
+
+	ret = request_threaded_irq(info->irq, NULL, pm80x_onkey_handler,
+				   IRQF_ONESHOT, "onkey", info);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to request IRQ: #%d: %d\n",
+			info->irq, ret);
+		goto out_irq;
+	}
+
+	platform_set_drvdata(pdev, info);
+
+	/* Enable long onkey detection */
+	pm80x_set_bits(info->i2c, PM800_RTC_MISC4,
+		       PM800_LONG_ONKEY_EN, PM800_LONG_ONKEY_EN);
+	/* Set 8-second interval */
+	pm80x_set_bits(info->i2c, PM800_RTC_MISC3,
+		       PM800_LONKEY_PRESS_TIME_MASK, PM800_LONKEY_PRESS_TIME);
+
+	device_init_wakeup(&pdev->dev, 1);
+	return 0;
+
+out_irq:
+	input_unregister_device(info->idev);
+out_reg:
+	input_free_device(info->idev);
+out:
+	devm_kfree(&pdev->dev, info);
+	return ret;
+}
+
+static int __devexit pm80x_onkey_remove(struct platform_device *pdev)
+{
+	struct pm80x_onkey_info *info = platform_get_drvdata(pdev);
+
+	free_irq(info->irq, info);
+	input_unregister_device(info->idev);
+	input_free_device(info->idev);
+	devm_kfree(&pdev->dev, info);
+	return 0;
+}
+
+static struct platform_driver pm80x_onkey_driver = {
+	.driver = {
+		   .name = "88pm80x-onkey",
+		   .owner = THIS_MODULE,
+		   .pm = &pm80x_onkey_pm_ops,
+		   },
+	.probe = pm80x_onkey_probe,
+	.remove = __devexit_p(pm80x_onkey_remove),
+};
+
+module_platform_driver(pm80x_onkey_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Marvell 88PM80x ONKEY driver");
+MODULE_AUTHOR("Qiao Zhou <[email protected]>");
+MODULE_ALIAS("platform:88pm80x-onkey");
drivers/input/misc/Kconfig +10 -0
index 7faf4a7..8205a66 100644
@@ -22,6 +22,16 @@ config INPUT_88PM860X_ONKEY
 	  To compile this driver as a module, choose M here: the module
 	  will be called 88pm860x_onkey.
 
+config INPUT_88PM80X_ONKEY
+	tristate "88PM80x ONKEY support"
+	depends on MFD_88PM80X
+	help
+	  Support the ONKEY of Marvell 88PM80x PMICs as an input device
+	  reporting power button status.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called 88pm80x_onkey.
+
 config INPUT_AB8500_PONKEY
 	tristate "AB8500 Pon (PowerOn) Key"
 	depends on AB8500_CORE
drivers/input/misc/Makefile +1 -0
index f55cdf4..83fe6f5 100644
@@ -5,6 +5,7 @@
 # Each configuration option enables a list of files.
 
 obj-$(CONFIG_INPUT_88PM860X_ONKEY)	+= 88pm860x_onkey.o
+obj-$(CONFIG_INPUT_88PM80X_ONKEY)	+= 88pm80x_onkey.o
 obj-$(CONFIG_INPUT_AB8500_PONKEY)	+= ab8500-ponkey.o
 obj-$(CONFIG_INPUT_AD714X)		+= ad714x.o
 obj-$(CONFIG_INPUT_AD714X_I2C)		+= ad714x-i2c.o
Signature · 1 line
1.7.4.1