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