3ff7c26
From bd0d7169342e47919f68e75d659968f02b62f84b Mon Sep 17 00:00:00 2001
3ff7c26
From: Hans de Goede <hdegoede@redhat.com>
3ff7c26
Date: Fri, 3 Mar 2017 23:48:50 +0100
3ff7c26
Subject: [PATCH 15/16] i2c-cht-wc: Add Intel Cherry Trail Whiskey Cove SMBUS
3ff7c26
 controller driver
3ff7c26
3ff7c26
The Intel Cherry Trail Whiskey Cove PMIC does not contain a builtin
3ff7c26
battery charger, instead boards with this PMIC use an external TI
3ff7c26
bq24292i charger IC, which is connected to a SMBUS controller built into
3ff7c26
the PMIC.
3ff7c26
3ff7c26
This commit adds an i2c-bus driver for the PMIC's builtin SMBUS
3ff7c26
controller. The probe function for this i2c-bus will also register an
3ff7c26
i2c-client for the TI bq24292i charger after the i2c-bus has been
3ff7c26
registered.
3ff7c26
3ff7c26
Note that several device-properties are set on the client-device to
3ff7c26
tell the bq24190 power-supply driver to integrate the Whiskey Cove PMIC
3ff7c26
and e.g. use the PMIC's BC1.2 detection (through extcon) to determine
3ff7c26
the maximum input current.
3ff7c26
3ff7c26
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
3ff7c26
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
3ff7c26
---
3ff7c26
Changes in v2:
3ff7c26
-Various style (mostly captialization and variable name) fixes
3ff7c26
-Use device-properties instead of platform_data for the i2c_board_info
3ff7c26
---
3ff7c26
 drivers/i2c/busses/Kconfig      |   8 +
3ff7c26
 drivers/i2c/busses/Makefile     |   1 +
3ff7c26
 drivers/i2c/busses/i2c-cht-wc.c | 336 ++++++++++++++++++++++++++++++++++++++++
3ff7c26
 3 files changed, 345 insertions(+)
3ff7c26
 create mode 100644 drivers/i2c/busses/i2c-cht-wc.c
3ff7c26
3ff7c26
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
3ff7c26
index 144cbadc7c72..18c96178b177 100644
3ff7c26
--- a/drivers/i2c/busses/Kconfig
3ff7c26
+++ b/drivers/i2c/busses/Kconfig
3ff7c26
@@ -187,6 +187,14 @@ config I2C_PIIX4
3ff7c26
 	  This driver can also be built as a module.  If so, the module
3ff7c26
 	  will be called i2c-piix4.
3ff7c26
 
3ff7c26
+config I2C_CHT_WC
3ff7c26
+	tristate "Intel Cherry Trail Whiskey Cove PMIC smbus controller"
3ff7c26
+	depends on INTEL_SOC_PMIC_CHTWC
3ff7c26
+	help
3ff7c26
+	  If you say yes to this option, support will be included for the
3ff7c26
+	  SMBus controller found in the Intel Cherry Trail Whiskey Cove PMIC
3ff7c26
+	  found on some Intel Cherry Trail systems.
3ff7c26
+
3ff7c26
 config I2C_NFORCE2
3ff7c26
 	tristate "Nvidia nForce2, nForce3 and nForce4"
3ff7c26
 	depends on PCI
3ff7c26
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
3ff7c26
index 30b60855fbcd..f6443fa44f61 100644
3ff7c26
--- a/drivers/i2c/busses/Makefile
3ff7c26
+++ b/drivers/i2c/busses/Makefile
3ff7c26
@@ -12,6 +12,7 @@ obj-$(CONFIG_I2C_ALI15X3)	+= i2c-ali15x3.o
3ff7c26
 obj-$(CONFIG_I2C_AMD756)	+= i2c-amd756.o
3ff7c26
 obj-$(CONFIG_I2C_AMD756_S4882)	+= i2c-amd756-s4882.o
3ff7c26
 obj-$(CONFIG_I2C_AMD8111)	+= i2c-amd8111.o
3ff7c26
+obj-$(CONFIG_I2C_CHT_WC)	+= i2c-cht-wc.o
3ff7c26
 obj-$(CONFIG_I2C_I801)		+= i2c-i801.o
3ff7c26
 obj-$(CONFIG_I2C_ISCH)		+= i2c-isch.o
3ff7c26
 obj-$(CONFIG_I2C_ISMT)		+= i2c-ismt.o
3ff7c26
diff --git a/drivers/i2c/busses/i2c-cht-wc.c b/drivers/i2c/busses/i2c-cht-wc.c
3ff7c26
new file mode 100644
3ff7c26
index 000000000000..ccf0785bcb75
3ff7c26
--- /dev/null
3ff7c26
+++ b/drivers/i2c/busses/i2c-cht-wc.c
3ff7c26
@@ -0,0 +1,336 @@
3ff7c26
+/*
3ff7c26
+ * Intel CHT Whiskey Cove PMIC I2C Master driver
3ff7c26
+ * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
3ff7c26
+ *
3ff7c26
+ * Based on various non upstream patches to support the CHT Whiskey Cove PMIC:
3ff7c26
+ * Copyright (C) 2011 - 2014 Intel Corporation. All rights reserved.
3ff7c26
+ *
3ff7c26
+ * This program is free software; you can redistribute it and/or
3ff7c26
+ * modify it under the terms of the GNU General Public License version
3ff7c26
+ * 2 as published by the Free Software Foundation.
3ff7c26
+ *
3ff7c26
+ * This program is distributed in the hope that it will be useful,
3ff7c26
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ff7c26
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3ff7c26
+ * GNU General Public License for more details.
3ff7c26
+ */
3ff7c26
+
3ff7c26
+#include <linux/completion.h>
3ff7c26
+#include <linux/delay.h>
3ff7c26
+#include <linux/i2c.h>
3ff7c26
+#include <linux/interrupt.h>
3ff7c26
+#include <linux/irq.h>
3ff7c26
+#include <linux/irqdomain.h>
3ff7c26
+#include <linux/mfd/intel_soc_pmic.h>
3ff7c26
+#include <linux/module.h>
3ff7c26
+#include <linux/platform_device.h>
3ff7c26
+#include <linux/slab.h>
3ff7c26
+
3ff7c26
+#define CHT_WC_I2C_CTRL			0x5e24
3ff7c26
+#define CHT_WC_I2C_CTRL_WR		BIT(0)
3ff7c26
+#define CHT_WC_I2C_CTRL_RD		BIT(1)
3ff7c26
+#define CHT_WC_I2C_CLIENT_ADDR		0x5e25
3ff7c26
+#define CHT_WC_I2C_REG_OFFSET		0x5e26
3ff7c26
+#define CHT_WC_I2C_WRDATA		0x5e27
3ff7c26
+#define CHT_WC_I2C_RDDATA		0x5e28
3ff7c26
+
3ff7c26
+#define CHT_WC_EXTCHGRIRQ		0x6e0a
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_CLIENT_IRQ	BIT(0)
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_WRITE_IRQ	BIT(1)
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_READ_IRQ	BIT(2)
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_NACK_IRQ	BIT(3)
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK	((u8)GENMASK(3, 1))
3ff7c26
+#define CHT_WC_EXTCHGRIRQ_MSK		0x6e17
3ff7c26
+
3ff7c26
+struct cht_wc_i2c_adap {
3ff7c26
+	struct i2c_adapter adapter;
3ff7c26
+	wait_queue_head_t wait;
3ff7c26
+	struct irq_chip irqchip;
3ff7c26
+	struct mutex irqchip_lock;
3ff7c26
+	struct regmap *regmap;
3ff7c26
+	struct irq_domain *irq_domain;
3ff7c26
+	struct i2c_client *client;
3ff7c26
+	int client_irq;
3ff7c26
+	u8 irq_mask;
3ff7c26
+	u8 old_irq_mask;
3ff7c26
+	bool nack;
3ff7c26
+	bool done;
3ff7c26
+};
3ff7c26
+
3ff7c26
+static irqreturn_t cht_wc_i2c_adap_thread_handler(int id, void *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = data;
3ff7c26
+	int ret, reg;
3ff7c26
+
3ff7c26
+	/* Read IRQs */
3ff7c26
+	ret = regmap_read(adap->regmap, CHT_WC_EXTCHGRIRQ, ®);
3ff7c26
+	if (ret) {
3ff7c26
+		dev_err(&adap->adapter.dev, "Error reading extchgrirq reg\n");
3ff7c26
+		return IRQ_NONE;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	reg &= ~adap->irq_mask;
3ff7c26
+
3ff7c26
+	/*
3ff7c26
+	 * Immediately ack IRQs, so that if new IRQs arrives while we're
3ff7c26
+	 * handling the previous ones our irq will re-trigger when we're done.
3ff7c26
+	 */
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ, reg);
3ff7c26
+	if (ret)
3ff7c26
+		dev_err(&adap->adapter.dev, "Error writing extchgrirq reg\n");
3ff7c26
+
3ff7c26
+	/*
3ff7c26
+	 * Do NOT use handle_nested_irq here, the client irq handler will
3ff7c26
+	 * likely want to do i2c transfers and the i2c controller uses this
3ff7c26
+	 * interrupt handler as well, so running the client irq handler from
3ff7c26
+	 * this thread will cause things to lock up.
3ff7c26
+	 */
3ff7c26
+	if (reg & CHT_WC_EXTCHGRIRQ_CLIENT_IRQ) {
3ff7c26
+		/*
3ff7c26
+		 * generic_handle_irq expects local IRQs to be disabled
3ff7c26
+		 * as normally it is called from interrupt context.
3ff7c26
+		 */
3ff7c26
+		local_irq_disable();
3ff7c26
+		generic_handle_irq(adap->client_irq);
3ff7c26
+		local_irq_enable();
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	if (reg & CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK) {
3ff7c26
+		adap->nack = !!(reg & CHT_WC_EXTCHGRIRQ_NACK_IRQ);
3ff7c26
+		adap->done = true;
3ff7c26
+		wake_up(&adap->wait);
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	return IRQ_HANDLED;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static u32 cht_wc_i2c_adap_master_func(struct i2c_adapter *adap)
3ff7c26
+{
3ff7c26
+	/* This i2c adapter only supports SMBUS byte transfers */
3ff7c26
+	return I2C_FUNC_SMBUS_BYTE_DATA;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static int cht_wc_i2c_adap_smbus_xfer(struct i2c_adapter *_adap, u16 addr,
3ff7c26
+				      unsigned short flags, char read_write,
3ff7c26
+				      u8 command, int size,
3ff7c26
+				      union i2c_smbus_data *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = i2c_get_adapdata(_adap);
3ff7c26
+	int ret, reg;
3ff7c26
+
3ff7c26
+	adap->nack = false;
3ff7c26
+	adap->done = false;
3ff7c26
+
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_I2C_CLIENT_ADDR, addr);
3ff7c26
+	if (ret)
3ff7c26
+		return ret;
3ff7c26
+
3ff7c26
+	if (read_write == I2C_SMBUS_WRITE) {
3ff7c26
+		ret = regmap_write(adap->regmap, CHT_WC_I2C_WRDATA, data->byte);
3ff7c26
+		if (ret)
3ff7c26
+			return ret;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_I2C_REG_OFFSET, command);
3ff7c26
+	if (ret)
3ff7c26
+		return ret;
3ff7c26
+
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_I2C_CTRL,
3ff7c26
+			   (read_write == I2C_SMBUS_WRITE) ?
3ff7c26
+			   CHT_WC_I2C_CTRL_WR : CHT_WC_I2C_CTRL_RD);
3ff7c26
+	if (ret)
3ff7c26
+		return ret;
3ff7c26
+
3ff7c26
+	/* 3 second timeout, during cable plug the PMIC responds quite slow */
3ff7c26
+	ret = wait_event_timeout(adap->wait, adap->done, 3 * HZ);
3ff7c26
+	if (ret == 0)
3ff7c26
+		return -ETIMEDOUT;
3ff7c26
+	if (adap->nack)
3ff7c26
+		return -EIO;
3ff7c26
+
3ff7c26
+	if (read_write == I2C_SMBUS_READ) {
3ff7c26
+		ret = regmap_read(adap->regmap, CHT_WC_I2C_RDDATA, ®);
3ff7c26
+		if (ret)
3ff7c26
+			return ret;
3ff7c26
+
3ff7c26
+		data->byte = reg;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	return 0;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static const struct i2c_algorithm cht_wc_i2c_adap_algo = {
3ff7c26
+	.functionality = cht_wc_i2c_adap_master_func,
3ff7c26
+	.smbus_xfer = cht_wc_i2c_adap_smbus_xfer,
3ff7c26
+};
3ff7c26
+
3ff7c26
+/**** irqchip for the client connected to the extchgr i2c adapter ****/
3ff7c26
+static void cht_wc_i2c_irq_lock(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
3ff7c26
+
3ff7c26
+	mutex_lock(&adap->irqchip_lock);
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void cht_wc_i2c_irq_sync_unlock(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
3ff7c26
+	int ret;
3ff7c26
+
3ff7c26
+	if (adap->irq_mask != adap->old_irq_mask) {
3ff7c26
+		ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK,
3ff7c26
+				   adap->irq_mask);
3ff7c26
+		if (ret == 0)
3ff7c26
+			adap->old_irq_mask = adap->irq_mask;
3ff7c26
+		else
3ff7c26
+			dev_err(&adap->adapter.dev, "Error writing EXTCHGRIRQ_MSK\n");
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	mutex_unlock(&adap->irqchip_lock);
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void cht_wc_i2c_irq_enable(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
3ff7c26
+
3ff7c26
+	adap->irq_mask &= ~CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void cht_wc_i2c_irq_disable(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = irq_data_get_irq_chip_data(data);
3ff7c26
+
3ff7c26
+	adap->irq_mask |= CHT_WC_EXTCHGRIRQ_CLIENT_IRQ;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static const struct irq_chip cht_wc_i2c_irq_chip = {
3ff7c26
+	.irq_bus_lock		= cht_wc_i2c_irq_lock,
3ff7c26
+	.irq_bus_sync_unlock	= cht_wc_i2c_irq_sync_unlock,
3ff7c26
+	.irq_disable		= cht_wc_i2c_irq_disable,
3ff7c26
+	.irq_enable		= cht_wc_i2c_irq_enable,
3ff7c26
+	.name			= "cht_wc_ext_chrg_irq_chip",
3ff7c26
+};
3ff7c26
+
3ff7c26
+static const struct property_entry bq24190_props[] = {
3ff7c26
+	PROPERTY_ENTRY_STRING("extcon-name", "cht_wcove_pwrsrc"),
3ff7c26
+	PROPERTY_ENTRY_BOOL("omit-battery-class"),
3ff7c26
+	PROPERTY_ENTRY_BOOL("disable-reset"),
3ff7c26
+	{ }
3ff7c26
+};
3ff7c26
+
3ff7c26
+static int cht_wc_i2c_adap_i2c_probe(struct platform_device *pdev)
3ff7c26
+{
3ff7c26
+	struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
3ff7c26
+	struct cht_wc_i2c_adap *adap;
3ff7c26
+	struct i2c_board_info board_info = {
3ff7c26
+		.type = "bq24190",
3ff7c26
+		.addr = 0x6b,
3ff7c26
+		.properties = bq24190_props,
3ff7c26
+	};
3ff7c26
+	int ret, irq;
3ff7c26
+
3ff7c26
+	irq = platform_get_irq(pdev, 0);
3ff7c26
+	if (irq < 0) {
3ff7c26
+		dev_err(&pdev->dev, "Error missing irq resource\n");
3ff7c26
+		return -EINVAL;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	adap = devm_kzalloc(&pdev->dev, sizeof(*adap), GFP_KERNEL);
3ff7c26
+	if (!adap)
3ff7c26
+		return -ENOMEM;
3ff7c26
+
3ff7c26
+	init_waitqueue_head(&adap->wait);
3ff7c26
+	mutex_init(&adap->irqchip_lock);
3ff7c26
+	adap->irqchip = cht_wc_i2c_irq_chip;
3ff7c26
+	adap->regmap = pmic->regmap;
3ff7c26
+	adap->adapter.owner = THIS_MODULE;
3ff7c26
+	adap->adapter.class = I2C_CLASS_HWMON;
3ff7c26
+	adap->adapter.algo = &cht_wc_i2c_adap_algo;
3ff7c26
+	strlcpy(adap->adapter.name, "PMIC I2C Adapter",
3ff7c26
+		sizeof(adap->adapter.name));
3ff7c26
+	adap->adapter.dev.parent = &pdev->dev;
3ff7c26
+
3ff7c26
+	/* Clear and activate i2c-adapter interrupts, disable client IRQ */
3ff7c26
+	adap->old_irq_mask = adap->irq_mask = ~CHT_WC_EXTCHGRIRQ_ADAP_IRQMASK;
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ, ~adap->irq_mask);
3ff7c26
+	if (ret)
3ff7c26
+		return ret;
3ff7c26
+
3ff7c26
+	ret = regmap_write(adap->regmap, CHT_WC_EXTCHGRIRQ_MSK, adap->irq_mask);
3ff7c26
+	if (ret)
3ff7c26
+		return ret;
3ff7c26
+
3ff7c26
+	/* Alloc and register client IRQ */
3ff7c26
+	adap->irq_domain = irq_domain_add_linear(pdev->dev.of_node, 1,
3ff7c26
+						 &irq_domain_simple_ops, NULL);
3ff7c26
+	if (!adap->irq_domain)
3ff7c26
+		return -ENOMEM;
3ff7c26
+
3ff7c26
+	adap->client_irq = irq_create_mapping(adap->irq_domain, 0);
3ff7c26
+	if (!adap->client_irq) {
3ff7c26
+		ret = -ENOMEM;
3ff7c26
+		goto remove_irq_domain;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	irq_set_chip_data(adap->client_irq, adap);
3ff7c26
+	irq_set_chip_and_handler(adap->client_irq, &adap->irqchip,
3ff7c26
+				 handle_simple_irq);
3ff7c26
+
3ff7c26
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3ff7c26
+					cht_wc_i2c_adap_thread_handler,
3ff7c26
+					IRQF_ONESHOT, "PMIC I2C Adapter", adap);
3ff7c26
+	if (ret)
3ff7c26
+		goto remove_irq_domain;
3ff7c26
+
3ff7c26
+	i2c_set_adapdata(&adap->adapter, adap);
3ff7c26
+	ret = i2c_add_adapter(&adap->adapter);
3ff7c26
+	if (ret)
3ff7c26
+		goto remove_irq_domain;
3ff7c26
+
3ff7c26
+	board_info.irq = adap->client_irq;
3ff7c26
+	adap->client = i2c_new_device(&adap->adapter, &board_info);
3ff7c26
+	if (!adap->client) {
3ff7c26
+		ret = -ENOMEM;
3ff7c26
+		goto del_adapter;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	platform_set_drvdata(pdev, adap);
3ff7c26
+	return 0;
3ff7c26
+
3ff7c26
+del_adapter:
3ff7c26
+	i2c_del_adapter(&adap->adapter);
3ff7c26
+remove_irq_domain:
3ff7c26
+	irq_domain_remove(adap->irq_domain);
3ff7c26
+	return ret;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static int cht_wc_i2c_adap_i2c_remove(struct platform_device *pdev)
3ff7c26
+{
3ff7c26
+	struct cht_wc_i2c_adap *adap = platform_get_drvdata(pdev);
3ff7c26
+
3ff7c26
+	i2c_unregister_device(adap->client);
3ff7c26
+	i2c_del_adapter(&adap->adapter);
3ff7c26
+	irq_domain_remove(adap->irq_domain);
3ff7c26
+
3ff7c26
+	return 0;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static struct platform_device_id cht_wc_i2c_adap_id_table[] = {
3ff7c26
+	{ .name = "cht_wcove_ext_chgr" },
3ff7c26
+	{},
3ff7c26
+};
3ff7c26
+MODULE_DEVICE_TABLE(platform, cht_wc_i2c_adap_id_table);
3ff7c26
+
3ff7c26
+struct platform_driver cht_wc_i2c_adap_driver = {
3ff7c26
+	.probe = cht_wc_i2c_adap_i2c_probe,
3ff7c26
+	.remove = cht_wc_i2c_adap_i2c_remove,
3ff7c26
+	.driver = {
3ff7c26
+		.name = "cht_wcove_ext_chgr",
3ff7c26
+	},
3ff7c26
+	.id_table = cht_wc_i2c_adap_id_table,
3ff7c26
+};
3ff7c26
+module_platform_driver(cht_wc_i2c_adap_driver);
3ff7c26
+
3ff7c26
+MODULE_DESCRIPTION("Intel CHT Whiskey Cove PMIC I2C Master driver");
3ff7c26
+MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
3ff7c26
+MODULE_LICENSE("GPL");
3ff7c26
-- 
3ff7c26
2.13.0
3ff7c26