linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] cbus/retu drivers to mainline
@ 2012-09-03 20:23 Aaro Koskinen
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
                   ` (4 more replies)
  0 siblings, 5 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-03 20:23 UTC (permalink / raw)
  To: linux-omap, linux-kernel

This patch set introduces drivers for CBUS access and Retu multifunction
chip found on Nokia Internet Tablets (770, N800, N810). It would be
nice get these patches applied as the functionality of these devices is
severely lacking without Retu. E.g. watchdog support is mandatory at
least on Nokia N800, you cannot currently run the mainline kernel for
longer than ~60 seconds (there is no way to disable the watchdog).

Drivers originate from linux-omap cbus branch and have been cleaned
up/rewritten around i2c and MFD core.

Patches have been tested on top of 3.6-rc4 with Nokia N800 (watchdog
feeding works, power off shuts down the device, power button triggers
IRQs and input events).

Changes since the RFC version
(http://marc.info/?l=linux-omap&m=134618967116737&w=2):
	- added DT support for getting i2c-cbus GPIO pins
	- merged n8x0 board file changes into i2c-cbus patch
	- corrected typo in Kconfig for MFD_RETU
	- added power off functionality to retu-mfd
	- added IRQ functionality to retu-mfd
	- added power button key driver
	- some cleanups

Aaro Koskinen (4):
  i2c: introduce i2c-cbus driver
  mfd: introduce retu-mfd driver
  watchdog: introduce retu_wdt driver
  input: misc: introduce retu-pwrbutton

 arch/arm/mach-omap2/board-n8x0.c    |   42 ++++
 drivers/i2c/busses/Kconfig          |   10 +
 drivers/i2c/busses/Makefile         |    1 +
 drivers/i2c/busses/i2c-cbus.c       |  369 +++++++++++++++++++++++++++++++++++
 drivers/input/misc/Kconfig          |   10 +
 drivers/input/misc/Makefile         |    1 +
 drivers/input/misc/retu-pwrbutton.c |  127 ++++++++++++
 drivers/mfd/Kconfig                 |    8 +
 drivers/mfd/Makefile                |    1 +
 drivers/mfd/retu-mfd.c              |  347 ++++++++++++++++++++++++++++++++
 drivers/watchdog/Kconfig            |   12 ++
 drivers/watchdog/Makefile           |    1 +
 drivers/watchdog/retu_wdt.c         |  188 ++++++++++++++++++
 include/linux/i2c-cbus.h            |   27 +++
 include/linux/mfd/retu.h            |   22 ++
 15 files changed, 1166 insertions(+), 0 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-cbus.c
 create mode 100644 drivers/input/misc/retu-pwrbutton.c
 create mode 100644 drivers/mfd/retu-mfd.c
 create mode 100644 drivers/watchdog/retu_wdt.c
 create mode 100644 include/linux/i2c-cbus.h
 create mode 100644 include/linux/mfd/retu.h

-- 
1.7.2.5


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
@ 2012-09-03 20:23 ` Aaro Koskinen
  2012-09-04  9:05   ` Felipe Balbi
                     ` (2 more replies)
  2012-09-03 20:23 ` [PATCH 2/4] mfd: introduce retu-mfd driver Aaro Koskinen
                   ` (3 subsequent siblings)
  4 siblings, 3 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-03 20:23 UTC (permalink / raw)
  To: linux-omap, linux-kernel; +Cc: linux-i2c

Add i2c driver to enable access to devices behind CBUS on Nokia Internet
Tablets.

The patch also adds CBUS I2C configuration for N8x0 which is one of the
users of this driver.

Cc: linux-i2c@vger.kernel.org
Acked-by: Felipe Balbi <balbi@ti.com>
Acked-by: Tony Lindgren <tony@atomide.com>
Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
---
 arch/arm/mach-omap2/board-n8x0.c |   42 +++++
 drivers/i2c/busses/Kconfig       |   10 +
 drivers/i2c/busses/Makefile      |    1 +
 drivers/i2c/busses/i2c-cbus.c    |  369 ++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-cbus.h         |   27 +++
 5 files changed, 449 insertions(+), 0 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-cbus.c
 create mode 100644 include/linux/i2c-cbus.h

diff --git a/arch/arm/mach-omap2/board-n8x0.c b/arch/arm/mach-omap2/board-n8x0.c
index 677357f..2495f2d 100644
--- a/arch/arm/mach-omap2/board-n8x0.c
+++ b/arch/arm/mach-omap2/board-n8x0.c
@@ -16,8 +16,10 @@
 #include <linux/gpio.h>
 #include <linux/init.h>
 #include <linux/io.h>
+#include <linux/irq.h>
 #include <linux/stddef.h>
 #include <linux/i2c.h>
+#include <linux/i2c-cbus.h>
 #include <linux/spi/spi.h>
 #include <linux/usb/musb.h>
 #include <sound/tlv320aic3x.h>
@@ -42,6 +44,45 @@
 #define TUSB6010_GPIO_ENABLE	0
 #define TUSB6010_DMACHAN	0x3f
 
+#if defined(CONFIG_I2C_CBUS) || defined(CONFIG_I2C_CBUS_MODULE)
+static struct i2c_cbus_platform_data n8x0_cbus_data = {
+	.clk_gpio = 66,
+	.dat_gpio = 65,
+	.sel_gpio = 64,
+};
+
+static struct platform_device n8x0_cbus_device = {
+	.name	= "i2c-cbus",
+	.id	= 3,
+	.dev	= {
+		.platform_data = &n8x0_cbus_data,
+	},
+};
+
+static struct i2c_board_info n8x0_i2c_board_info_3[] __initdata = {
+	{
+		I2C_BOARD_INFO("retu-mfd", 0x01),
+	},
+};
+
+static void __init n8x0_cbus_init(void)
+{
+	const int retu_irq_gpio = 108;
+
+	if (gpio_request_one(retu_irq_gpio, GPIOF_IN, "Retu IRQ"))
+		return;
+	irq_set_irq_type(gpio_to_irq(retu_irq_gpio), IRQ_TYPE_EDGE_RISING);
+	n8x0_i2c_board_info_3[0].irq = gpio_to_irq(retu_irq_gpio);
+	i2c_register_board_info(3, n8x0_i2c_board_info_3,
+				ARRAY_SIZE(n8x0_i2c_board_info_3));
+	platform_device_register(&n8x0_cbus_device);
+}
+#else /* CONFIG_I2C_CBUS */
+static void __init n8x0_cbus_init(void)
+{
+}
+#endif /* CONFIG_I2C_CBUS */
+
 #if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_TUSB6010_MODULE)
 /*
  * Enable or disable power to TUSB6010. When enabling, turn on 3.3 V and
@@ -681,6 +722,7 @@ static void __init n8x0_init_machine(void)
 	gpmc_onenand_init(board_onenand_data);
 	n8x0_mmc_init();
 	n8x0_usb_init();
+	n8x0_cbus_init();
 }
 
 MACHINE_START(NOKIA_N800, "Nokia N800")
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index b4aaa1b..184ef43 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -331,6 +331,16 @@ config I2C_BLACKFIN_TWI_CLK_KHZ
 	help
 	  The unit of the TWI clock is kHz.
 
+config I2C_CBUS
+	tristate "CBUS I2C driver"
+	depends on GENERIC_GPIO
+	help
+	  Support for CBUS access using I2C API. Mostly relevant for Nokia
+	  Internet Tablets (770, N800 and N810).
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-cbus.
+
 config I2C_CPM
 	tristate "Freescale CPM1 or CPM2 (MPC8xx/826x)"
 	depends on (CPM1 || CPM2) && OF_I2C
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index ce3c2be..44dbfd1 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_I2C_POWERMAC)	+= i2c-powermac.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_BLACKFIN_TWI)	+= i2c-bfin-twi.o
+obj-$(CONFIG_I2C_CBUS)		+= i2c-cbus.o
 obj-$(CONFIG_I2C_CPM)		+= i2c-cpm.o
 obj-$(CONFIG_I2C_DAVINCI)	+= i2c-davinci.o
 obj-$(CONFIG_I2C_DESIGNWARE_PLATFORM)	+= i2c-designware-platform.o
diff --git a/drivers/i2c/busses/i2c-cbus.c b/drivers/i2c/busses/i2c-cbus.c
new file mode 100644
index 0000000..bacf2a9
--- /dev/null
+++ b/drivers/i2c/busses/i2c-cbus.c
@@ -0,0 +1,369 @@
+/*
+ * CBUS I2C driver for Nokia Internet Tablets.
+ *
+ * Copyright (C) 2004-2010 Nokia Corporation
+ *
+ * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
+ * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
+ *
+ * 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.
+ */
+
+#include <linux/io.h>
+#include <linux/i2c.h>
+#include <linux/gpio.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_gpio.h>
+#include <linux/i2c-cbus.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+struct cbus_host {
+	/* host lock */
+	spinlock_t	lock;
+
+	struct device	*dev;
+
+	int		clk_gpio;
+	int		dat_gpio;
+	int		sel_gpio;
+};
+
+/**
+ * cbus_send_bit - sends one bit over the bus
+ * @host: the host we're using
+ * @bit: one bit of information to send
+ * @input: whether to set data pin as input after sending
+ */
+static int cbus_send_bit(struct cbus_host *host, unsigned bit,
+		unsigned input)
+{
+	int ret = 0;
+
+	gpio_set_value(host->dat_gpio, bit ? 1 : 0);
+	gpio_set_value(host->clk_gpio, 1);
+
+	/* The data bit is read on the rising edge of CLK */
+	if (input)
+		ret = gpio_direction_input(host->dat_gpio);
+
+	gpio_set_value(host->clk_gpio, 0);
+
+	return ret;
+}
+
+/**
+ * cbus_send_data - sends @len amount of data over the bus
+ * @host: the host we're using
+ * @data: the data to send
+ * @len: size of the transfer
+ * @input: whether to set data pin as input after sending
+ */
+static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
+		unsigned input)
+{
+	int ret = 0;
+	int i;
+
+	for (i = len; i > 0; i--) {
+		ret = cbus_send_bit(host, data & (1 << (i - 1)),
+				input && (i == 1));
+		if (ret < 0)
+			goto out;
+	}
+
+out:
+	return ret;
+}
+
+/**
+ * cbus_receive_bit - receives one bit from the bus
+ * @host: the host we're using
+ */
+static int cbus_receive_bit(struct cbus_host *host)
+{
+	int ret;
+
+	gpio_set_value(host->clk_gpio, 1);
+	ret = gpio_get_value(host->dat_gpio);
+	if (ret < 0)
+		goto out;
+	gpio_set_value(host->clk_gpio, 0);
+
+out:
+	return ret;
+}
+
+/**
+ * cbus_receive_word - receives 16-bit word from the bus
+ * @host: the host we're using
+ */
+static int cbus_receive_word(struct cbus_host *host)
+{
+	int ret = 0;
+	int i;
+
+	for (i = 16; i > 0; i--) {
+		int bit = cbus_receive_bit(host);
+
+		if (bit < 0)
+			goto out;
+
+		if (bit)
+			ret |= 1 << (i - 1);
+	}
+
+out:
+	return ret;
+}
+
+/**
+ * cbus_transfer - transfers data over the bus
+ * @host: the host we're using
+ * @rw: read/write flag
+ * @dev: device address
+ * @reg: register address
+ * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
+ */
+static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
+			 unsigned reg, unsigned data)
+{
+	unsigned long flags;
+	int ret;
+
+	/* We don't want interrupts disturbing our transfer */
+	spin_lock_irqsave(&host->lock, flags);
+
+	/* Reset state and start of transfer, SEL stays down during transfer */
+	gpio_set_value(host->sel_gpio, 0);
+
+	/* Set the DAT pin to output */
+	gpio_direction_output(host->dat_gpio, 1);
+
+	/* Send the device address */
+	ret = cbus_send_data(host, dev, 3, 0);
+	if (ret < 0) {
+		dev_dbg(host->dev, "failed sending device addr\n");
+		goto out;
+	}
+
+	/* Send the rw flag */
+	ret = cbus_send_bit(host, rw == I2C_SMBUS_READ, 0);
+	if (ret < 0) {
+		dev_dbg(host->dev, "failed sending read/write flag\n");
+		goto out;
+	}
+
+	/* Send the device address */
+	ret = cbus_send_data(host, reg, 5, rw == I2C_SMBUS_READ);
+	if (ret < 0) {
+		dev_dbg(host->dev, "failed sending register addr\n");
+		goto out;
+	}
+
+	if (rw == I2C_SMBUS_WRITE) {
+		ret = cbus_send_data(host, data, 16, 0);
+		if (ret < 0) {
+			dev_dbg(host->dev, "failed sending data\n");
+			goto out;
+		}
+	} else {
+		gpio_set_value(host->clk_gpio, 1);
+
+		ret = cbus_receive_word(host);
+		if (ret < 0) {
+			dev_dbg(host->dev, "failed receiving data\n");
+			goto out;
+		}
+	}
+
+	/* Indicate end of transfer, SEL goes up until next transfer */
+	gpio_set_value(host->sel_gpio, 1);
+	gpio_set_value(host->clk_gpio, 1);
+	gpio_set_value(host->clk_gpio, 0);
+
+out:
+	spin_unlock_irqrestore(&host->lock, flags);
+
+	return ret;
+}
+
+static int cbus_i2c_smbus_xfer(struct i2c_adapter	*adapter,
+			       u16			addr,
+			       unsigned short		flags,
+			       char			read_write,
+			       u8			command,
+			       int			size,
+			       union i2c_smbus_data	*data)
+{
+	struct cbus_host *chost = i2c_get_adapdata(adapter);
+	int ret;
+
+	if (size != I2C_SMBUS_WORD_DATA)
+		return -EINVAL;
+
+	ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
+			    command, data->word);
+	if (ret < 0)
+		return ret;
+
+	if (read_write == I2C_SMBUS_READ)
+		data->word = ret;
+
+	return 0;
+}
+
+static u32 cbus_i2c_func(struct i2c_adapter *adapter)
+{
+	return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
+}
+
+static const struct i2c_algorithm cbus_i2c_algo = {
+	.smbus_xfer	= cbus_i2c_smbus_xfer,
+	.functionality	= cbus_i2c_func,
+};
+
+static int cbus_i2c_remove(struct platform_device *pdev)
+{
+	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
+	struct cbus_host *chost = i2c_get_adapdata(adapter);
+	int ret;
+
+	ret = i2c_del_adapter(adapter);
+	if (ret)
+		return ret;
+	gpio_free(chost->clk_gpio);
+	gpio_free(chost->dat_gpio);
+	gpio_free(chost->sel_gpio);
+	kfree(chost);
+	kfree(adapter);
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+static int cbus_i2c_probe(struct platform_device *pdev)
+{
+	struct i2c_adapter *adapter;
+	struct cbus_host *chost;
+	struct gpio gpios[3];
+	int ret;
+
+	adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
+	if (!adapter) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	chost = kzalloc(sizeof(*chost), GFP_KERNEL);
+	if (!chost) {
+		ret = -ENOMEM;
+		goto error_mem;
+	}
+
+	if (pdev->dev.of_node) {
+		struct device_node *dnode = pdev->dev.of_node;
+		if (of_gpio_count(dnode) != 3) {
+			ret = -ENODEV;
+			goto error_mem;
+		}
+		chost->clk_gpio = of_get_gpio(dnode, 0);
+		chost->dat_gpio = of_get_gpio(dnode, 1);
+		chost->sel_gpio = of_get_gpio(dnode, 2);
+	} else if (pdev->dev.platform_data) {
+		struct i2c_cbus_platform_data *pdata = pdev->dev.platform_data;
+		chost->clk_gpio = pdata->clk_gpio;
+		chost->dat_gpio = pdata->dat_gpio;
+		chost->sel_gpio = pdata->sel_gpio;
+	} else {
+		ret = -ENODEV;
+		goto error_mem;
+	}
+
+	adapter->owner		= THIS_MODULE;
+	adapter->class		= I2C_CLASS_HWMON;
+	adapter->dev.parent	= &pdev->dev;
+	adapter->nr		= pdev->id;
+	adapter->timeout	= HZ;
+	adapter->algo		= &cbus_i2c_algo;
+	strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
+
+	spin_lock_init(&chost->lock);
+	chost->dev = &pdev->dev;
+
+	gpios[0].gpio  = chost->clk_gpio;
+	gpios[0].flags = GPIOF_OUT_INIT_LOW;
+	gpios[0].label = "CBUS clk";
+
+	gpios[1].gpio  = chost->dat_gpio;
+	gpios[1].flags = GPIOF_IN;
+	gpios[1].label = "CBUS data";
+
+	gpios[2].gpio  = chost->sel_gpio;
+	gpios[2].flags = GPIOF_OUT_INIT_HIGH;
+	gpios[2].label = "CBUS sel";
+
+	ret = gpio_request_array(gpios, ARRAY_SIZE(gpios));
+	if (ret)
+		goto error_gpio;
+
+	gpio_set_value(chost->clk_gpio, 1);
+	gpio_set_value(chost->clk_gpio, 0);
+
+	i2c_set_adapdata(adapter, chost);
+	platform_set_drvdata(pdev, adapter);
+
+	ret = i2c_add_numbered_adapter(adapter);
+	if (ret)
+		goto error_i2c;
+
+	return 0;
+
+error_i2c:
+	gpio_free_array(gpios, ARRAY_SIZE(gpios));
+error_gpio:
+	kfree(chost);
+error_mem:
+	kfree(adapter);
+error:
+	return ret;
+}
+
+#if defined(CONFIG_OF)
+static const struct of_device_id i2c_cbus_dt_ids[] = {
+	{ .compatible = "i2c-cbus", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
+#endif
+
+static struct platform_driver cbus_i2c_driver = {
+	.probe	= cbus_i2c_probe,
+	.remove	= cbus_i2c_remove,
+	.driver	= {
+		.owner	= THIS_MODULE,
+		.name	= "i2c-cbus",
+	},
+};
+module_platform_driver(cbus_i2c_driver);
+
+MODULE_ALIAS("platform:i2c-cbus");
+MODULE_DESCRIPTION("CBUS I2C driver");
+MODULE_AUTHOR("Juha Yrjölä");
+MODULE_AUTHOR("David Weinehall");
+MODULE_AUTHOR("Mikko Ylinen");
+MODULE_AUTHOR("Felipe Balbi");
+MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/i2c-cbus.h b/include/linux/i2c-cbus.h
new file mode 100644
index 0000000..636d726
--- /dev/null
+++ b/include/linux/i2c-cbus.h
@@ -0,0 +1,27 @@
+/*
+ * i2c-cbus.h - CBUS I2C platform_data definition
+ *
+ * Copyright (C) 2004-2009 Nokia Corporation
+ *
+ * Written by Felipe Balbi and Aaro Koskinen.
+ *
+ * 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.
+ */
+
+#ifndef __INCLUDE_LINUX_I2C_CBUS_H
+#define __INCLUDE_LINUX_I2C_CBUS_H
+
+struct i2c_cbus_platform_data {
+	int dat_gpio;
+	int clk_gpio;
+	int sel_gpio;
+};
+
+#endif /* __INCLUDE_LINUX_I2C_CBUS_H */
-- 
1.7.2.5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/4] mfd: introduce retu-mfd driver
  2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
@ 2012-09-03 20:23 ` Aaro Koskinen
  2012-09-19 16:02   ` Samuel Ortiz
  2012-09-03 20:23 ` [PATCH 3/4] watchdog: introduce retu_wdt driver Aaro Koskinen
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-03 20:23 UTC (permalink / raw)
  To: linux-omap, linux-kernel; +Cc: sameo

Retu is a multi-function device found on Nokia Internet Tablets
implementing at least watchdog, RTC, headset detection and power button
functionality.

This patch implements minimum functionality providing register access,
IRQ handling and power off functions.

Cc: sameo@linux.intel.com
Acked-by: Tony Lindgren <tony@atomide.com>
Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
---
 drivers/mfd/Kconfig      |    8 +
 drivers/mfd/Makefile     |    1 +
 drivers/mfd/retu-mfd.c   |  347 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/retu.h |   22 +++
 4 files changed, 378 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mfd/retu-mfd.c
 create mode 100644 include/linux/mfd/retu.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index b1a1462..7d4ce92 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -1003,6 +1003,14 @@ config MFD_PALMAS
 	  If you say yes here you get support for the Palmas
 	  series of PMIC chips from Texas Instruments.
 
+config MFD_RETU
+	tristate "Support for Retu multi-function device"
+	select MFD_CORE
+	depends on I2C
+	help
+	  Retu is a multi-function device found on Nokia Internet Tablets
+	  (770, N800 and N810).
+
 endmenu
 endif
 
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 79dd22d..962ec9d 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -132,3 +132,4 @@ obj-$(CONFIG_MFD_RC5T583)	+= rc5t583.o rc5t583-irq.o
 obj-$(CONFIG_MFD_SEC_CORE)	+= sec-core.o sec-irq.o
 obj-$(CONFIG_MFD_ANATOP)	+= anatop-mfd.o
 obj-$(CONFIG_MFD_LM3533)	+= lm3533-core.o lm3533-ctrlbank.o
+obj-$(CONFIG_MFD_RETU)		+= retu-mfd.o
diff --git a/drivers/mfd/retu-mfd.c b/drivers/mfd/retu-mfd.c
new file mode 100644
index 0000000..99059d3
--- /dev/null
+++ b/drivers/mfd/retu-mfd.c
@@ -0,0 +1,347 @@
+/*
+ * Retu MFD driver
+ *
+ * Copyright (C) 2004, 2005 Nokia Corporation
+ *
+ * Based on code written by Juha Yrjölä, David Weinehall and Mikko Ylinen.
+ * Rewritten to MFD/I2C driver by Aaro Koskinen.
+ *
+ * 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.
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/irq.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/mutex.h>
+#include <linux/module.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/retu.h>
+#include <linux/interrupt.h>
+#include <linux/moduleparam.h>
+
+/* Registers */
+#define RETU_REG_ASICR		0x00		/* ASIC ID and revision */
+#define RETU_REG_ASICR_VILMA	(1 << 7)	/* Bit indicating Vilma */
+#define RETU_REG_IDR		0x01		/* Interrupt ID */
+#define RETU_REG_IMR		0x02		/* Interrupt mask */
+
+/* Interrupt sources */
+#define RETU_INT_PWR		0		/* Power button */
+
+#define RETU_MAX_IRQ_HANDLERS	16
+
+struct retu_dev {
+	struct device		*dev;
+	struct i2c_client	*i2c;
+	struct mutex		mutex;
+
+	struct irq_chip		irq_chip;
+	int			irq_base;
+	int			irq_end;
+	int			irq_mask;
+	bool			irq_mask_pending;
+};
+
+static struct resource retu_pwrbutton_res[] = {
+	{
+		.name	= "retu-pwrbutton",
+		.start	= RETU_INT_PWR,
+		.end	= RETU_INT_PWR,
+		.flags = IORESOURCE_IRQ,
+	},
+};
+
+static struct mfd_cell retu_devs[] = {
+	{
+		.name		= "retu-wdt"
+	},
+	{
+		.name		= "retu-pwrbutton",
+		.resources	= &retu_pwrbutton_res[0],
+		.num_resources	= ARRAY_SIZE(retu_pwrbutton_res),
+	}
+};
+
+/* Retu device registered for the power off. */
+static struct retu_dev *retu_pm_power_off;
+
+int retu_read(struct retu_dev *rdev, u8 reg)
+{
+	int ret;
+
+	mutex_lock(&rdev->mutex);
+	ret = i2c_smbus_read_word_data(rdev->i2c, reg);
+	mutex_unlock(&rdev->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(retu_read);
+
+int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
+{
+	int ret;
+
+	mutex_lock(&rdev->mutex);
+	ret = i2c_smbus_write_word_data(rdev->i2c, reg, data);
+	mutex_unlock(&rdev->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(retu_write);
+
+static void retu_power_off(void)
+{
+	struct retu_dev *rdev = retu_pm_power_off;
+	int reg;
+
+	mutex_lock(&retu_pm_power_off->mutex);
+
+	/* Ignore power button state */
+	reg = i2c_smbus_read_word_data(rdev->i2c, RETU_REG_CC1);
+	i2c_smbus_write_word_data(rdev->i2c, RETU_REG_CC1, reg | 2);
+
+	/* Expire watchdog immediately */
+	i2c_smbus_write_word_data(rdev->i2c, RETU_REG_WATCHDOG, 0);
+
+	/* Wait for poweroff */
+	for (;;)
+		cpu_relax();
+
+	mutex_unlock(&retu_pm_power_off->mutex);
+}
+
+static irqreturn_t retu_irq_handler(int irq, void *retu)
+{
+	struct retu_dev *rdev = retu;
+	int idr;
+	int imr;
+
+	mutex_lock(&rdev->mutex);
+
+	idr = i2c_smbus_read_word_data(rdev->i2c, RETU_REG_IDR);
+	if (idr < 0)
+		goto i2c_error;
+
+	imr = i2c_smbus_read_word_data(rdev->i2c, RETU_REG_IMR);
+	if (imr < 0)
+		goto i2c_error;
+
+	idr &= ~imr;
+	i2c_smbus_write_word_data(rdev->i2c, RETU_REG_IDR, idr);
+
+	mutex_unlock(&rdev->mutex);
+
+	if (!idr) {
+		dev_vdbg(rdev->dev, "No IRQ, spurious?\n");
+		return IRQ_NONE;
+	}
+
+	while (idr) {
+		unsigned long pending = __ffs(idr);
+		unsigned int irq;
+
+		idr &= ~BIT(pending);
+		irq = pending + rdev->irq_base;
+		handle_nested_irq(irq);
+	}
+
+	return IRQ_HANDLED;
+
+i2c_error:
+	mutex_unlock(&rdev->mutex);
+	return IRQ_NONE;
+}
+
+static void retu_irq_mask(struct irq_data *data)
+{
+	struct retu_dev *rdev = irq_data_get_irq_chip_data(data);
+	int irq = data->irq;
+
+	rdev->irq_mask |= (1 << (irq - rdev->irq_base));
+	rdev->irq_mask_pending = true;
+}
+
+static void retu_irq_unmask(struct irq_data *data)
+{
+	struct retu_dev *rdev = irq_data_get_irq_chip_data(data);
+	int irq = data->irq;
+
+	rdev->irq_mask &= ~(1 << (irq - rdev->irq_base));
+	rdev->irq_mask_pending = true;
+}
+
+static void retu_bus_lock(struct irq_data *data)
+{
+	struct retu_dev *rdev = irq_data_get_irq_chip_data(data);
+
+	mutex_lock(&rdev->mutex);
+}
+
+static void retu_bus_sync_unlock(struct irq_data *data)
+{
+	struct retu_dev *rdev = irq_data_get_irq_chip_data(data);
+
+	if (rdev->irq_mask_pending) {
+		i2c_smbus_write_word_data(rdev->i2c, RETU_REG_IMR,
+					  rdev->irq_mask);
+		rdev->irq_mask_pending = false;
+	}
+	mutex_unlock(&rdev->mutex);
+}
+
+static void retu_irq_init(struct retu_dev *rdev)
+{
+	int base = rdev->irq_base;
+	int end = rdev->irq_end;
+	int irq;
+
+	rdev->irq_chip.name		   = "RETU";
+	rdev->irq_chip.irq_bus_lock	   = retu_bus_lock;
+	rdev->irq_chip.irq_bus_sync_unlock = retu_bus_sync_unlock;
+	rdev->irq_chip.irq_mask		   = retu_irq_mask;
+	rdev->irq_chip.irq_unmask	   = retu_irq_unmask;
+
+	for (irq = base; irq < end; irq++) {
+		irq_set_chip_data(irq, rdev);
+		irq_set_chip(irq, &rdev->irq_chip);
+		irq_set_nested_thread(irq, 1);
+#ifdef CONFIG_ARM
+		set_irq_flags(irq, IRQF_VALID);
+#else
+		irq_set_noprobe(irq);
+#endif
+	}
+
+	/* Mask all RETU interrupts */
+	rdev->irq_mask = 0xffff;
+	i2c_smbus_write_word_data(rdev->i2c, RETU_REG_IMR, rdev->irq_mask);
+}
+
+static void retu_irq_exit(struct retu_dev *rdev)
+{
+	int base = rdev->irq_base;
+	int end = rdev->irq_end;
+	int irq;
+
+	for (irq = base; irq < end; irq++) {
+#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 retu_probe(struct i2c_client *i2c,
+				const struct i2c_device_id *id)
+{
+	struct retu_dev *rdev;
+	int ret;
+
+	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
+	if (rdev == NULL)
+		return -ENOMEM;
+
+	i2c_set_clientdata(i2c, rdev);
+	rdev->dev = &i2c->dev;
+	rdev->i2c = i2c;
+	mutex_init(&rdev->mutex);
+
+	ret = retu_read(rdev, RETU_REG_ASICR);
+	if (ret < 0) {
+		dev_err(rdev->dev, "could not read Retu revision: %d\n", ret);
+		goto error;
+	}
+
+	dev_info(rdev->dev, "Retu%s v%d.%d found\n",
+		 (ret & RETU_REG_ASICR_VILMA) ? " & Vilma" : "",
+		 (ret >> 4) & 0x7, ret & 0xf);
+
+	ret = irq_alloc_descs(-1, 0, RETU_MAX_IRQ_HANDLERS, 0);
+	if (ret < 0) {
+		dev_err(rdev->dev, "failed to allocate IRQ descs: %d\n", ret);
+		goto error;
+	}
+	rdev->irq_base = ret;
+	rdev->irq_end  = ret + RETU_MAX_IRQ_HANDLERS;
+
+	retu_irq_init(rdev);
+
+	ret = request_threaded_irq(rdev->i2c->irq, NULL, retu_irq_handler,
+				   IRQF_ONESHOT, "retu-mfd", rdev);
+	if (ret < 0) {
+		dev_err(rdev->dev, "unable to register IRQ handler: %d\n", ret);
+		goto error_irq;
+	}
+	irq_set_irq_wake(rdev->i2c->irq, 1);
+
+	ret = mfd_add_devices(rdev->dev, -1, retu_devs, ARRAY_SIZE(retu_devs),
+			      NULL, rdev->irq_base);
+	if (ret < 0)
+		goto error_add;
+
+	if (!pm_power_off) {
+		pm_power_off	  = retu_power_off;
+		retu_pm_power_off = rdev;
+	}
+
+	return 0;
+
+error_add:
+	free_irq(rdev->i2c->irq, rdev);
+error_irq:
+	irq_free_descs(rdev->irq_base, RETU_MAX_IRQ_HANDLERS);
+error:
+	kfree(rdev);
+	return ret;
+}
+
+static int __devexit retu_remove(struct i2c_client *i2c)
+{
+	struct retu_dev *rdev = i2c_get_clientdata(i2c);
+
+	if (retu_pm_power_off == rdev) {
+		pm_power_off	  = NULL;
+		retu_pm_power_off = NULL;
+	}
+	free_irq(rdev->i2c->irq, rdev);
+	retu_irq_exit(rdev);
+	irq_free_descs(rdev->irq_base, RETU_MAX_IRQ_HANDLERS);
+	mfd_remove_devices(rdev->dev);
+	kfree(rdev);
+
+	return 0;
+}
+
+static const struct i2c_device_id retu_id[] = {
+	{ "retu-mfd", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, retu_id);
+
+static struct i2c_driver retu_driver = {
+	.driver		= {
+		.name = "retu-mfd",
+		.owner = THIS_MODULE,
+	},
+	.probe		= retu_probe,
+	.remove		= retu_remove,
+	.id_table	= retu_id,
+};
+module_i2c_driver(retu_driver);
+
+MODULE_DESCRIPTION("Retu MFD driver");
+MODULE_AUTHOR("Juha Yrjölä");
+MODULE_AUTHOR("David Weinehall");
+MODULE_AUTHOR("Mikko Ylinen");
+MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/mfd/retu.h b/include/linux/mfd/retu.h
new file mode 100644
index 0000000..1e2715d
--- /dev/null
+++ b/include/linux/mfd/retu.h
@@ -0,0 +1,22 @@
+/*
+ * Retu MFD driver interface
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_MFD_RETU_H
+#define __LINUX_MFD_RETU_H
+
+struct retu_dev;
+
+int retu_read(struct retu_dev *, u8);
+int retu_write(struct retu_dev *, u8, u16);
+
+/* Registers */
+#define RETU_REG_WATCHDOG	0x17		/* Watchdog */
+#define RETU_REG_CC1		0x0d		/* Common control register 1 */
+#define RETU_REG_STATUS		0x16		/* Status register */
+
+#endif /* __LINUX_MFD_RETU_H */
-- 
1.7.2.5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 3/4] watchdog: introduce retu_wdt driver
  2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
  2012-09-03 20:23 ` [PATCH 2/4] mfd: introduce retu-mfd driver Aaro Koskinen
@ 2012-09-03 20:23 ` Aaro Koskinen
  2012-09-03 20:23 ` [PATCH 4/4] input: misc: introduce retu-pwrbutton Aaro Koskinen
  2012-09-04 16:09 ` [PATCH 0/4] cbus/retu drivers to mainline Andi Shyti
  4 siblings, 0 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-03 20:23 UTC (permalink / raw)
  To: linux-omap, linux-kernel; +Cc: linux-watchdog

Introduce Retu watchdog driver.

Cc: linux-watchdog@vger.kernel.org
Acked-by: Tony Lindgren <tony@atomide.com>
Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
---
 drivers/watchdog/Kconfig    |   12 +++
 drivers/watchdog/Makefile   |    1 +
 drivers/watchdog/retu_wdt.c |  188 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 201 insertions(+), 0 deletions(-)
 create mode 100644 drivers/watchdog/retu_wdt.c

diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 53d7571..0526c7a 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -352,6 +352,18 @@ config IMX2_WDT
 	  To compile this driver as a module, choose M here: the
 	  module will be called imx2_wdt.
 
+config RETU_WATCHDOG
+	tristate "Retu watchdog"
+	depends on MFD_RETU
+	select WATCHDOG_CORE
+	help
+	  Retu watchdog driver for Nokia Internet Tablets (700, N800,
+	  N810). At least on N800 the watchdog cannot be disabled, so
+	  this driver is essential and you should enable it.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called retu_wdt.
+
 # AVR32 Architecture
 
 config AT32AP700X_WDT
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 572b39b..d2f1c0c 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -52,6 +52,7 @@ obj-$(CONFIG_STMP3XXX_WATCHDOG) += stmp3xxx_wdt.o
 obj-$(CONFIG_NUC900_WATCHDOG) += nuc900_wdt.o
 obj-$(CONFIG_TS72XX_WATCHDOG) += ts72xx_wdt.o
 obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
+obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
 
 # AVR32 Architecture
 obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o
diff --git a/drivers/watchdog/retu_wdt.c b/drivers/watchdog/retu_wdt.c
new file mode 100644
index 0000000..da9750a
--- /dev/null
+++ b/drivers/watchdog/retu_wdt.c
@@ -0,0 +1,188 @@
+/*
+ * Retu watchdog driver
+ *
+ * Copyright (C) 2004, 2005 Nokia Corporation
+ *
+ * Based on code written by Amit Kucheria and Michael Buesch. Rewritten
+ * to use Retu MFD driver and new watchdog core by Aaro Koskinen.
+ *
+ * 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.
+ */
+
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mfd/retu.h>
+#include <linux/watchdog.h>
+#include <linux/platform_device.h>
+
+/* Watchdog timer values in seconds */
+#define RETU_WDT_MAX_TIMER	63
+
+struct retu_wdt_dev {
+	struct retu_dev		*rdev;
+	struct device		*dev;
+	struct delayed_work	ping_work;
+};
+
+/*
+ * Since Retu watchdog cannot be disabled in hardware, we must kick it
+ * with a timer until userspace watchdog software takes over. If
+ * CONFIG_WATCHDOG_NOWAYOUT is set, we never start the feeding.
+ */
+static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
+{
+	retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
+	schedule_delayed_work(&wdev->ping_work,
+			round_jiffies_relative(RETU_WDT_MAX_TIMER * HZ / 2));
+}
+
+static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
+{
+	retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
+	cancel_delayed_work_sync(&wdev->ping_work);
+}
+
+static void retu_wdt_ping_work(struct work_struct *work)
+{
+	struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
+						struct retu_wdt_dev, ping_work);
+	retu_wdt_ping_enable(wdev);
+}
+
+static int retu_wdt_start(struct watchdog_device *wdog)
+{
+	struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
+
+	retu_wdt_ping_disable(wdev);
+
+	return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
+}
+
+static int retu_wdt_stop(struct watchdog_device *wdog)
+{
+	struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
+
+	retu_wdt_ping_enable(wdev);
+
+	return 0;
+}
+
+static int retu_wdt_ping(struct watchdog_device *wdog)
+{
+	struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
+
+	return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
+}
+
+static int retu_wdt_set_timeout(struct watchdog_device *wdog,
+				unsigned int timeout)
+{
+	struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
+
+	wdog->timeout = timeout;
+	return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
+}
+
+static const struct watchdog_info retu_wdt_info = {
+	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
+	.identity = "Retu watchdog",
+};
+
+static const struct watchdog_ops retu_wdt_ops = {
+	.owner		= THIS_MODULE,
+	.start		= retu_wdt_start,
+	.stop		= retu_wdt_stop,
+	.ping		= retu_wdt_ping,
+	.set_timeout	= retu_wdt_set_timeout,
+};
+
+static int __devinit retu_wdt_probe(struct platform_device *pdev)
+{
+	struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
+	bool nowayout = WATCHDOG_NOWAYOUT;
+	struct watchdog_device *retu_wdt;
+	struct retu_wdt_dev *wdev;
+	int ret;
+
+	retu_wdt = kzalloc(sizeof(*retu_wdt), GFP_KERNEL);
+	if (!retu_wdt)
+		return -ENOMEM;
+
+	wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
+	if (!wdev) {
+		ret = -ENOMEM;
+		goto error_mem;
+	}
+
+	retu_wdt->info		= &retu_wdt_info;
+	retu_wdt->ops		= &retu_wdt_ops;
+	retu_wdt->timeout	= RETU_WDT_MAX_TIMER;
+	retu_wdt->min_timeout	= 0;
+	retu_wdt->max_timeout	= RETU_WDT_MAX_TIMER;
+
+	watchdog_set_drvdata(retu_wdt, wdev);
+	watchdog_set_nowayout(retu_wdt, nowayout);
+
+	wdev->rdev		= rdev;
+	wdev->dev		= &pdev->dev;
+
+	INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
+
+	ret = watchdog_register_device(retu_wdt);
+	if (ret < 0)
+		goto error_reg;
+
+	if (nowayout)
+		retu_wdt_ping(retu_wdt);
+	else
+		retu_wdt_ping_enable(wdev);
+
+	platform_set_drvdata(pdev, retu_wdt);
+
+	return 0;
+
+error_reg:
+	kfree(wdev);
+error_mem:
+	kfree(retu_wdt);
+	return ret;
+}
+
+static int __devexit retu_wdt_remove(struct platform_device *pdev)
+{
+	struct watchdog_device *wdog = platform_get_drvdata(pdev);
+	struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
+
+	watchdog_unregister_device(wdog);
+	cancel_delayed_work_sync(&wdev->ping_work);
+	kfree(wdev);
+	kfree(wdog);
+
+	return 0;
+}
+
+static struct platform_driver retu_wdt_driver = {
+	.probe		= retu_wdt_probe,
+	.remove		= __devexit_p(retu_wdt_remove),
+	.driver		= {
+		.name	= "retu-wdt",
+	},
+};
+module_platform_driver(retu_wdt_driver);
+
+MODULE_ALIAS("platform:retu-wdt");
+MODULE_DESCRIPTION("Retu watchdog");
+MODULE_AUTHOR("Amit Kucheria");
+MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
+MODULE_LICENSE("GPL");
-- 
1.7.2.5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 4/4] input: misc: introduce retu-pwrbutton
  2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
                   ` (2 preceding siblings ...)
  2012-09-03 20:23 ` [PATCH 3/4] watchdog: introduce retu_wdt driver Aaro Koskinen
@ 2012-09-03 20:23 ` Aaro Koskinen
  2012-09-04 16:09 ` [PATCH 0/4] cbus/retu drivers to mainline Andi Shyti
  4 siblings, 0 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-03 20:23 UTC (permalink / raw)
  To: linux-omap, linux-kernel; +Cc: linux-input

Add Retu power button driver.

Cc: linux-input@vger.kernel.org
Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
---
 drivers/input/misc/Kconfig          |   10 +++
 drivers/input/misc/Makefile         |    1 +
 drivers/input/misc/retu-pwrbutton.c |  127 +++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 0 deletions(-)
 create mode 100644 drivers/input/misc/retu-pwrbutton.c

diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index 7c0f1ec..e5be189 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -367,6 +367,16 @@ config INPUT_CM109
 	  To compile this driver as a module, choose M here: the module will be
 	  called cm109.
 
+config INPUT_RETU_PWRBUTTON
+	tristate "Retu Power button Driver"
+	depends on MFD_RETU
+	help
+	  Say Y here if you want to enable power key reporting via the
+	  Retu chips found in Nokia Internet Tablets (770, N800, N810).
+
+	  To compile this driver as a module, choose M here. The module will
+	  be called retu-pwrbutton.
+
 config INPUT_TWL4030_PWRBUTTON
 	tristate "TWL4030 Power button Driver"
 	depends on TWL4030_CORE
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 83fe6f5..4fbee0d 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -45,6 +45,7 @@ obj-$(CONFIG_INPUT_PMIC8XXX_PWRKEY)	+= pmic8xxx-pwrkey.o
 obj-$(CONFIG_INPUT_POWERMATE)		+= powermate.o
 obj-$(CONFIG_INPUT_PWM_BEEPER)		+= pwm-beeper.o
 obj-$(CONFIG_INPUT_RB532_BUTTON)	+= rb532_button.o
+obj-$(CONFIG_INPUT_RETU_PWRBUTTON)	+= retu-pwrbutton.o
 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER)	+= rotary_encoder.o
 obj-$(CONFIG_INPUT_SGI_BTNS)		+= sgi_btns.o
 obj-$(CONFIG_INPUT_SPARCSPKR)		+= sparcspkr.o
diff --git a/drivers/input/misc/retu-pwrbutton.c b/drivers/input/misc/retu-pwrbutton.c
new file mode 100644
index 0000000..f802d4d
--- /dev/null
+++ b/drivers/input/misc/retu-pwrbutton.c
@@ -0,0 +1,127 @@
+/*
+ * Retu power button driver.
+ *
+ * Copyright (C) 2004-2010 Nokia Corporation
+ *
+ * Original code written by Ari Saastamoinen, Juha Yrjölä and Felipe Balbi.
+ * Converted to use to use Retu MFD driver by Aaro Koskinen.
+ *
+ * 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.
+ */
+
+#include <linux/irq.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/input.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mfd/retu.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define RETU_STATUS_PWRONX (1 << 5)
+
+struct retu_pwrbutton {
+	struct input_dev	*idev;
+	struct retu_dev		*rdev;
+	struct device		*dev;
+	bool			pressed;
+	int			irq;
+};
+
+static irqreturn_t retu_pwrbutton_irq(int irq, void *_pwr)
+{
+	struct retu_pwrbutton *pwr = _pwr;
+	bool state;
+
+	state = !(retu_read(pwr->rdev, RETU_REG_STATUS) & RETU_STATUS_PWRONX);
+
+	if (pwr->pressed != state) {
+		input_report_key(pwr->idev, KEY_POWER, state);
+		input_sync(pwr->idev);
+		pwr->pressed = state;
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int __devinit retu_pwrbutton_probe(struct platform_device *pdev)
+{
+	struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
+	struct retu_pwrbutton *pwr;
+	int ret;
+
+	pwr = kzalloc(sizeof(*pwr), GFP_KERNEL);
+	if (!pwr)
+		return -ENOMEM;
+
+	pwr->rdev = rdev;
+	pwr->dev  = &pdev->dev;
+	pwr->irq  = platform_get_irq(pdev, 0);
+	platform_set_drvdata(pdev, pwr);
+
+	ret = request_threaded_irq(pwr->irq, NULL, retu_pwrbutton_irq, 0,
+				   "retu-pwrbutton", pwr);
+	if (ret < 0)
+		goto error_irq;
+
+	pwr->idev = input_allocate_device();
+	if (!pwr->idev) {
+		ret = -ENOMEM;
+		goto error_input;
+	}
+
+	pwr->idev->evbit[0]			= BIT_MASK(EV_KEY);
+	pwr->idev->keybit[BIT_WORD(KEY_POWER)]	= BIT_MASK(KEY_POWER);
+	pwr->idev->name				= "retu-pwrbutton";
+
+	ret = input_register_device(pwr->idev);
+	if (ret < 0)
+		goto error_reg;
+
+	return 0;
+
+error_reg:
+	input_free_device(pwr->idev);
+error_input:
+	free_irq(pwr->irq, pwr);
+error_irq:
+	kfree(pwr);
+	return ret;
+}
+
+static int __devexit retu_pwrbutton_remove(struct platform_device *pdev)
+{
+	struct retu_pwrbutton *pwr = platform_get_drvdata(pdev);
+
+	free_irq(pwr->irq, pwr);
+	input_unregister_device(pwr->idev);
+	input_free_device(pwr->idev);
+	kfree(pwr);
+
+	return 0;
+}
+
+static struct platform_driver retu_pwrbutton_driver = {
+	.probe		= retu_pwrbutton_probe,
+	.remove		= __devexit_p(retu_pwrbutton_remove),
+	.driver		= {
+		.name	= "retu-pwrbutton",
+	},
+};
+module_platform_driver(retu_pwrbutton_driver);
+
+MODULE_ALIAS("platform:retu-pwrbutton");
+MODULE_DESCRIPTION("Retu Power Button");
+MODULE_AUTHOR("Ari Saastamoinen");
+MODULE_AUTHOR("Felipe Balbi");
+MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
+MODULE_LICENSE("GPL");
-- 
1.7.2.5


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
@ 2012-09-04  9:05   ` Felipe Balbi
  2012-09-04  9:31     ` Aaro Koskinen
  2012-09-13 10:53   ` Wolfram Sang
  2012-09-14 10:08   ` Wolfram Sang
  2 siblings, 1 reply; 18+ messages in thread
From: Felipe Balbi @ 2012-09-04  9:05 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: linux-omap, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 10218 bytes --]

Hi,

On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> diff --git a/drivers/i2c/busses/i2c-cbus.c b/drivers/i2c/busses/i2c-cbus.c
> new file mode 100644
> index 0000000..bacf2a9
> --- /dev/null
> +++ b/drivers/i2c/busses/i2c-cbus.c
> @@ -0,0 +1,369 @@
> +/*
> + * CBUS I2C driver for Nokia Internet Tablets.
> + *
> + * Copyright (C) 2004-2010 Nokia Corporation
> + *
> + * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
> + * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
> + *
> + * 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.
> + */
> +
> +#include <linux/io.h>
> +#include <linux/i2c.h>
> +#include <linux/gpio.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/delay.h>
> +#include <linux/errno.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/of_gpio.h>
> +#include <linux/i2c-cbus.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +
> +struct cbus_host {
> +	/* host lock */
> +	spinlock_t	lock;
> +
> +	struct device	*dev;
> +
> +	int		clk_gpio;
> +	int		dat_gpio;
> +	int		sel_gpio;
> +};
> +
> +/**
> + * cbus_send_bit - sends one bit over the bus
> + * @host: the host we're using
> + * @bit: one bit of information to send
> + * @input: whether to set data pin as input after sending
> + */
> +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
> +		unsigned input)
> +{
> +	int ret = 0;
> +
> +	gpio_set_value(host->dat_gpio, bit ? 1 : 0);
> +	gpio_set_value(host->clk_gpio, 1);
> +
> +	/* The data bit is read on the rising edge of CLK */
> +	if (input)
> +		ret = gpio_direction_input(host->dat_gpio);
> +
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +	return ret;
> +}
> +
> +/**
> + * cbus_send_data - sends @len amount of data over the bus
> + * @host: the host we're using
> + * @data: the data to send
> + * @len: size of the transfer
> + * @input: whether to set data pin as input after sending
> + */
> +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
> +		unsigned input)
> +{
> +	int ret = 0;
> +	int i;
> +
> +	for (i = len; i > 0; i--) {
> +		ret = cbus_send_bit(host, data & (1 << (i - 1)),
> +				input && (i == 1));
> +		if (ret < 0)
> +			goto out;
> +	}
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_receive_bit - receives one bit from the bus
> + * @host: the host we're using
> + */
> +static int cbus_receive_bit(struct cbus_host *host)
> +{
> +	int ret;
> +
> +	gpio_set_value(host->clk_gpio, 1);
> +	ret = gpio_get_value(host->dat_gpio);
> +	if (ret < 0)
> +		goto out;
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_receive_word - receives 16-bit word from the bus
> + * @host: the host we're using
> + */
> +static int cbus_receive_word(struct cbus_host *host)
> +{
> +	int ret = 0;
> +	int i;
> +
> +	for (i = 16; i > 0; i--) {
> +		int bit = cbus_receive_bit(host);
> +
> +		if (bit < 0)
> +			goto out;
> +
> +		if (bit)
> +			ret |= 1 << (i - 1);
> +	}
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_transfer - transfers data over the bus
> + * @host: the host we're using
> + * @rw: read/write flag
> + * @dev: device address
> + * @reg: register address
> + * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
> + */
> +static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
> +			 unsigned reg, unsigned data)
> +{
> +	unsigned long flags;
> +	int ret;
> +
> +	/* We don't want interrupts disturbing our transfer */
> +	spin_lock_irqsave(&host->lock, flags);
> +
> +	/* Reset state and start of transfer, SEL stays down during transfer */
> +	gpio_set_value(host->sel_gpio, 0);
> +
> +	/* Set the DAT pin to output */
> +	gpio_direction_output(host->dat_gpio, 1);
> +
> +	/* Send the device address */
> +	ret = cbus_send_data(host, dev, 3, 0);
> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending device addr\n");
> +		goto out;
> +	}
> +
> +	/* Send the rw flag */
> +	ret = cbus_send_bit(host, rw == I2C_SMBUS_READ, 0);
> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending read/write flag\n");
> +		goto out;
> +	}
> +
> +	/* Send the device address */
> +	ret = cbus_send_data(host, reg, 5, rw == I2C_SMBUS_READ);
> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending register addr\n");
> +		goto out;
> +	}
> +
> +	if (rw == I2C_SMBUS_WRITE) {
> +		ret = cbus_send_data(host, data, 16, 0);
> +		if (ret < 0) {
> +			dev_dbg(host->dev, "failed sending data\n");
> +			goto out;
> +		}
> +	} else {
> +		gpio_set_value(host->clk_gpio, 1);
> +
> +		ret = cbus_receive_word(host);
> +		if (ret < 0) {
> +			dev_dbg(host->dev, "failed receiving data\n");
> +			goto out;
> +		}
> +	}
> +
> +	/* Indicate end of transfer, SEL goes up until next transfer */
> +	gpio_set_value(host->sel_gpio, 1);
> +	gpio_set_value(host->clk_gpio, 1);
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +out:
> +	spin_unlock_irqrestore(&host->lock, flags);
> +
> +	return ret;
> +}
> +
> +static int cbus_i2c_smbus_xfer(struct i2c_adapter	*adapter,
> +			       u16			addr,
> +			       unsigned short		flags,
> +			       char			read_write,
> +			       u8			command,
> +			       int			size,
> +			       union i2c_smbus_data	*data)
> +{
> +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> +	int ret;
> +
> +	if (size != I2C_SMBUS_WORD_DATA)
> +		return -EINVAL;
> +
> +	ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
> +			    command, data->word);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (read_write == I2C_SMBUS_READ)
> +		data->word = ret;
> +
> +	return 0;
> +}
> +
> +static u32 cbus_i2c_func(struct i2c_adapter *adapter)
> +{
> +	return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
> +}
> +
> +static const struct i2c_algorithm cbus_i2c_algo = {
> +	.smbus_xfer	= cbus_i2c_smbus_xfer,
> +	.functionality	= cbus_i2c_func,
> +};
> +
> +static int cbus_i2c_remove(struct platform_device *pdev)
> +{
> +	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
> +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> +	int ret;
> +
> +	ret = i2c_del_adapter(adapter);
> +	if (ret)
> +		return ret;
> +	gpio_free(chost->clk_gpio);
> +	gpio_free(chost->dat_gpio);
> +	gpio_free(chost->sel_gpio);
> +	kfree(chost);
> +	kfree(adapter);
> +	platform_set_drvdata(pdev, NULL);
> +
> +	return 0;
> +}
> +
> +static int cbus_i2c_probe(struct platform_device *pdev)
> +{
> +	struct i2c_adapter *adapter;
> +	struct cbus_host *chost;
> +	struct gpio gpios[3];
> +	int ret;
> +
> +	adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
> +	if (!adapter) {
> +		ret = -ENOMEM;
> +		goto error;
> +	}
> +
> +	chost = kzalloc(sizeof(*chost), GFP_KERNEL);
> +	if (!chost) {
> +		ret = -ENOMEM;
> +		goto error_mem;
> +	}
> +
> +	if (pdev->dev.of_node) {
> +		struct device_node *dnode = pdev->dev.of_node;
> +		if (of_gpio_count(dnode) != 3) {
> +			ret = -ENODEV;
> +			goto error_mem;
> +		}
> +		chost->clk_gpio = of_get_gpio(dnode, 0);
> +		chost->dat_gpio = of_get_gpio(dnode, 1);
> +		chost->sel_gpio = of_get_gpio(dnode, 2);
> +	} else if (pdev->dev.platform_data) {
> +		struct i2c_cbus_platform_data *pdata = pdev->dev.platform_data;
> +		chost->clk_gpio = pdata->clk_gpio;
> +		chost->dat_gpio = pdata->dat_gpio;
> +		chost->sel_gpio = pdata->sel_gpio;
> +	} else {
> +		ret = -ENODEV;
> +		goto error_mem;
> +	}
> +
> +	adapter->owner		= THIS_MODULE;
> +	adapter->class		= I2C_CLASS_HWMON;
> +	adapter->dev.parent	= &pdev->dev;
> +	adapter->nr		= pdev->id;
> +	adapter->timeout	= HZ;
> +	adapter->algo		= &cbus_i2c_algo;
> +	strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
> +
> +	spin_lock_init(&chost->lock);
> +	chost->dev = &pdev->dev;
> +
> +	gpios[0].gpio  = chost->clk_gpio;
> +	gpios[0].flags = GPIOF_OUT_INIT_LOW;
> +	gpios[0].label = "CBUS clk";
> +
> +	gpios[1].gpio  = chost->dat_gpio;
> +	gpios[1].flags = GPIOF_IN;
> +	gpios[1].label = "CBUS data";
> +
> +	gpios[2].gpio  = chost->sel_gpio;
> +	gpios[2].flags = GPIOF_OUT_INIT_HIGH;
> +	gpios[2].label = "CBUS sel";
> +
> +	ret = gpio_request_array(gpios, ARRAY_SIZE(gpios));
> +	if (ret)
> +		goto error_gpio;
> +
> +	gpio_set_value(chost->clk_gpio, 1);
> +	gpio_set_value(chost->clk_gpio, 0);
> +
> +	i2c_set_adapdata(adapter, chost);
> +	platform_set_drvdata(pdev, adapter);
> +
> +	ret = i2c_add_numbered_adapter(adapter);
> +	if (ret)
> +		goto error_i2c;
> +
> +	return 0;
> +
> +error_i2c:
> +	gpio_free_array(gpios, ARRAY_SIZE(gpios));
> +error_gpio:
> +	kfree(chost);
> +error_mem:
> +	kfree(adapter);
> +error:
> +	return ret;
> +}
> +
> +#if defined(CONFIG_OF)
> +static const struct of_device_id i2c_cbus_dt_ids[] = {
> +	{ .compatible = "i2c-cbus", },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
> +#endif
> +
> +static struct platform_driver cbus_i2c_driver = {
> +	.probe	= cbus_i2c_probe,
> +	.remove	= cbus_i2c_remove,
> +	.driver	= {
> +		.owner	= THIS_MODULE,
> +		.name	= "i2c-cbus",
> +	},
> +};
> +module_platform_driver(cbus_i2c_driver);
> +
> +MODULE_ALIAS("platform:i2c-cbus");
> +MODULE_DESCRIPTION("CBUS I2C driver");
> +MODULE_AUTHOR("Juha Yrjölä");
> +MODULE_AUTHOR("David Weinehall");
> +MODULE_AUTHOR("Mikko Ylinen");
> +MODULE_AUTHOR("Felipe Balbi");
> +MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
> +MODULE_LICENSE("GPL");

this version misses the entire IRQ handling we already had on linux-omap
tree, so it's quite a regression.

-- 
balbi

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-04  9:05   ` Felipe Balbi
@ 2012-09-04  9:31     ` Aaro Koskinen
  2012-09-04 11:41       ` Felipe Balbi
  0 siblings, 1 reply; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-04  9:31 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: linux-omap, linux-kernel, linux-i2c

On Tue, Sep 04, 2012 at 12:05:07PM +0300, Felipe Balbi wrote:
> > + * CBUS I2C driver for Nokia Internet Tablets.

[...]

> this version misses the entire IRQ handling we already had on linux-omap
> tree, so it's quite a regression.

There's no interrupts used in plain CBUS protocol/communication I think?

But Retu MFD supports the GPIO IRQ. I added the code for this set. And
the power button driver (patch 4/4) is using this functionality:

# cat /proc/interrupts
[...]
204:         29      GPIO  retu-mfd
224:         29      RETU  retu-pwrbutton

A.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-04  9:31     ` Aaro Koskinen
@ 2012-09-04 11:41       ` Felipe Balbi
  0 siblings, 0 replies; 18+ messages in thread
From: Felipe Balbi @ 2012-09-04 11:41 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: Felipe Balbi, linux-omap, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 857 bytes --]

Hi,

On Tue, Sep 04, 2012 at 12:31:42PM +0300, Aaro Koskinen wrote:
> On Tue, Sep 04, 2012 at 12:05:07PM +0300, Felipe Balbi wrote:
> > > + * CBUS I2C driver for Nokia Internet Tablets.
> 
> [...]
> 
> > this version misses the entire IRQ handling we already had on linux-omap
> > tree, so it's quite a regression.
> 
> There's no interrupts used in plain CBUS protocol/communication I think?
> 
> But Retu MFD supports the GPIO IRQ. I added the code for this set. And
> the power button driver (patch 4/4) is using this functionality:
> 
> # cat /proc/interrupts
> [...]
> 204:         29      GPIO  retu-mfd
> 224:         29      RETU  retu-pwrbutton

oops, indeed. My bad. I looked at the wrong patch. Nevermind, this
series looks good to me.

FWIW:

Acked-by: Felipe Balbi <balbi@ti.com>

(for entire series)

-- 
balbi

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/4] cbus/retu drivers to mainline
  2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
                   ` (3 preceding siblings ...)
  2012-09-03 20:23 ` [PATCH 4/4] input: misc: introduce retu-pwrbutton Aaro Koskinen
@ 2012-09-04 16:09 ` Andi Shyti
  4 siblings, 0 replies; 18+ messages in thread
From: Andi Shyti @ 2012-09-04 16:09 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: linux-omap, linux-kernel

Hi Aaro,

On Mon, Sep 03, 2012 at 11:23:21PM +0300, Aaro Koskinen wrote:
> This patch set introduces drivers for CBUS access and Retu multifunction
> chip found on Nokia Internet Tablets (770, N800, N810). It would be
> nice get these patches applied as the functionality of these devices is
> severely lacking without Retu. E.g. watchdog support is mandatory at
> least on Nokia N800, you cannot currently run the mainline kernel for
> longer than ~60 seconds (there is no way to disable the watchdog).
> 
> Drivers originate from linux-omap cbus branch and have been cleaned
> up/rewritten around i2c and MFD core.
> 
> Patches have been tested on top of 3.6-rc4 with Nokia N800 (watchdog
> feeding works, power off shuts down the device, power button triggers
> IRQs and input events).
> 
> Changes since the RFC version
> (http://marc.info/?l=linux-omap&m=134618967116737&w=2):
> 	- added DT support for getting i2c-cbus GPIO pins
> 	- merged n8x0 board file changes into i2c-cbus patch
> 	- corrected typo in Kconfig for MFD_RETU
> 	- added power off functionality to retu-mfd
> 	- added IRQ functionality to retu-mfd
> 	- added power button key driver
> 	- some cleanups
> 
> Aaro Koskinen (4):
>   i2c: introduce i2c-cbus driver
>   mfd: introduce retu-mfd driver
>   watchdog: introduce retu_wdt driver
>   input: misc: introduce retu-pwrbutton

I had a look to the patches 1, 2 and 4, so for them

Reviewed-by: Andi Shyti <andi@etezian.org>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
  2012-09-04  9:05   ` Felipe Balbi
@ 2012-09-13 10:53   ` Wolfram Sang
  2012-09-13 14:51     ` Aaro Koskinen
  2012-09-14 10:08   ` Wolfram Sang
  2 siblings, 1 reply; 18+ messages in thread
From: Wolfram Sang @ 2012-09-13 10:53 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: linux-omap, linux-kernel, linux-i2c, Jean Delvare

[-- Attachment #1: Type: text/plain, Size: 979 bytes --]

On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> Tablets.
> 
> The patch also adds CBUS I2C configuration for N8x0 which is one of the
> users of this driver.
> 
> Cc: linux-i2c@vger.kernel.org
> Acked-by: Felipe Balbi <balbi@ti.com>
> Acked-by: Tony Lindgren <tony@atomide.com>
> Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>

My main question is: what is CBUS? It doesn't look like an I2C/SMBUS
host controller, but some bit-banging protocol? As such, it shouldn't go
to i2c/busses/. And the protocol doesn't look much like I2C, neither.
There is no ACK/NACK/START/STOP, so I wonder if it should be in i2c
after all...

Jean, maybe you want to have a glimpse on this as well?

Regards,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-13 10:53   ` Wolfram Sang
@ 2012-09-13 14:51     ` Aaro Koskinen
  0 siblings, 0 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-13 14:51 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-omap, linux-kernel, linux-i2c, Jean Delvare

On Thu, Sep 13, 2012 at 12:53:09PM +0200, Wolfram Sang wrote:
> On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> > Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> > Tablets.
> > 
> > The patch also adds CBUS I2C configuration for N8x0 which is one of the
> > users of this driver.
> > 
> > Cc: linux-i2c@vger.kernel.org
> > Acked-by: Felipe Balbi <balbi@ti.com>
> > Acked-by: Tony Lindgren <tony@atomide.com>
> > Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> 
> My main question is: what is CBUS? It doesn't look like an I2C/SMBUS
> host controller, but some bit-banging protocol? As such, it shouldn't go
> to i2c/busses/. And the protocol doesn't look much like I2C, neither.
> There is no ACK/NACK/START/STOP, so I wonder if it should be in i2c
> after all...

It's some legacy 3-wire bus protocol. Not much info is available,
there's some references to CBUS in i2c spec, so I thought it wouldn't
be a completly wrong place for it...

A.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
  2012-09-04  9:05   ` Felipe Balbi
  2012-09-13 10:53   ` Wolfram Sang
@ 2012-09-14 10:08   ` Wolfram Sang
  2012-09-14 10:21     ` Jean Delvare
                       ` (2 more replies)
  2 siblings, 3 replies; 18+ messages in thread
From: Wolfram Sang @ 2012-09-14 10:08 UTC (permalink / raw)
  To: Aaro Koskinen, Jean Delvare; +Cc: linux-omap, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 11513 bytes --]

On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> Tablets.
> 
> The patch also adds CBUS I2C configuration for N8x0 which is one of the
> users of this driver.
> 
> Cc: linux-i2c@vger.kernel.org
> Acked-by: Felipe Balbi <balbi@ti.com>
> Acked-by: Tony Lindgren <tony@atomide.com>
> Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>

OK, I found the short paragrahp about CBUS in the I2C spec, so I2C might
be an appropriate place. Still, before deciding if it should rather be
in the core directory, I still have a few questions.

Also, does anybody know of a generic bit-banging implementation in the
kernel which could be used here?

Jean: I'd appreciate your general opinion here, too.

> diff --git a/drivers/i2c/busses/i2c-cbus.c b/drivers/i2c/busses/i2c-cbus.c
> new file mode 100644
> index 0000000..bacf2a9
> --- /dev/null
> +++ b/drivers/i2c/busses/i2c-cbus.c
> @@ -0,0 +1,369 @@
> +/*
> + * CBUS I2C driver for Nokia Internet Tablets.
> + *
> + * Copyright (C) 2004-2010 Nokia Corporation
> + *
> + * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
> + * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
> + *
> + * 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.
> + */
> +
> +#include <linux/io.h>
> +#include <linux/i2c.h>
> +#include <linux/gpio.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/delay.h>
> +#include <linux/errno.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/of_gpio.h>
> +#include <linux/i2c-cbus.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +
> +struct cbus_host {
> +	/* host lock */
> +	spinlock_t	lock;
> +
> +	struct device	*dev;
> +
> +	int		clk_gpio;
> +	int		dat_gpio;
> +	int		sel_gpio;
> +};
> +
> +/**
> + * cbus_send_bit - sends one bit over the bus
> + * @host: the host we're using
> + * @bit: one bit of information to send
> + * @input: whether to set data pin as input after sending
> + */
> +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
> +		unsigned input)
> +{
> +	int ret = 0;
> +
> +	gpio_set_value(host->dat_gpio, bit ? 1 : 0);
> +	gpio_set_value(host->clk_gpio, 1);
> +
> +	/* The data bit is read on the rising edge of CLK */

This comment doesn't belong to the if-block, or?

> +	if (input)
> +		ret = gpio_direction_input(host->dat_gpio);

No minimum delay for the signal to be on the bus?

> +
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +	return ret;
> +}
> +
> +/**
> + * cbus_send_data - sends @len amount of data over the bus
> + * @host: the host we're using
> + * @data: the data to send
> + * @len: size of the transfer
> + * @input: whether to set data pin as input after sending
> + */
> +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
> +		unsigned input)
> +{
> +	int ret = 0;
> +	int i;
> +
> +	for (i = len; i > 0; i--) {
> +		ret = cbus_send_bit(host, data & (1 << (i - 1)),
> +				input && (i == 1));
> +		if (ret < 0)
> +			goto out;
> +	}
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_receive_bit - receives one bit from the bus
> + * @host: the host we're using
> + */
> +static int cbus_receive_bit(struct cbus_host *host)
> +{
> +	int ret;
> +
> +	gpio_set_value(host->clk_gpio, 1);

No delays to ensure the signal has stabilized?

> +	ret = gpio_get_value(host->dat_gpio);
> +	if (ret < 0)
> +		goto out;
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_receive_word - receives 16-bit word from the bus
> + * @host: the host we're using
> + */
> +static int cbus_receive_word(struct cbus_host *host)
> +{
> +	int ret = 0;
> +	int i;
> +
> +	for (i = 16; i > 0; i--) {
> +		int bit = cbus_receive_bit(host);
> +
> +		if (bit < 0)
> +			goto out;
> +
> +		if (bit)
> +			ret |= 1 << (i - 1);
> +	}
> +
> +out:
> +	return ret;
> +}
> +
> +/**
> + * cbus_transfer - transfers data over the bus
> + * @host: the host we're using
> + * @rw: read/write flag
> + * @dev: device address
> + * @reg: register address
> + * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
> + */
> +static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
> +			 unsigned reg, unsigned data)
> +{
> +	unsigned long flags;
> +	int ret;
> +
> +	/* We don't want interrupts disturbing our transfer */
> +	spin_lock_irqsave(&host->lock, flags);
> +
> +	/* Reset state and start of transfer, SEL stays down during transfer */
> +	gpio_set_value(host->sel_gpio, 0);
> +
> +	/* Set the DAT pin to output */
> +	gpio_direction_output(host->dat_gpio, 1);
> +
> +	/* Send the device address */
> +	ret = cbus_send_data(host, dev, 3, 0);

Is the device address always 3 bit? From the (admittetly sparse) I2C
spec, I'd assume to be 7?

> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending device addr\n");
> +		goto out;
> +	}
> +
> +	/* Send the rw flag */
> +	ret = cbus_send_bit(host, rw == I2C_SMBUS_READ, 0);
> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending read/write flag\n");
> +		goto out;
> +	}
> +
> +	/* Send the device address */

Comment is wrong.

> +	ret = cbus_send_data(host, reg, 5, rw == I2C_SMBUS_READ);

Is reg always 5 bit?

> +	if (ret < 0) {
> +		dev_dbg(host->dev, "failed sending register addr\n");
> +		goto out;
> +	}
> +
> +	if (rw == I2C_SMBUS_WRITE) {
> +		ret = cbus_send_data(host, data, 16, 0);
> +		if (ret < 0) {
> +			dev_dbg(host->dev, "failed sending data\n");
> +			goto out;
> +		}
> +	} else {

What about setting dat to input here and drop the parameter from
cbus_data_send()?

> +		gpio_set_value(host->clk_gpio, 1);
> +
> +		ret = cbus_receive_word(host);
> +		if (ret < 0) {
> +			dev_dbg(host->dev, "failed receiving data\n");
> +			goto out;
> +		}
> +	}
> +
> +	/* Indicate end of transfer, SEL goes up until next transfer */
> +	gpio_set_value(host->sel_gpio, 1);
> +	gpio_set_value(host->clk_gpio, 1);
> +	gpio_set_value(host->clk_gpio, 0);
> +
> +out:
> +	spin_unlock_irqrestore(&host->lock, flags);
> +
> +	return ret;
> +}
> +
> +static int cbus_i2c_smbus_xfer(struct i2c_adapter	*adapter,
> +			       u16			addr,
> +			       unsigned short		flags,
> +			       char			read_write,
> +			       u8			command,
> +			       int			size,
> +			       union i2c_smbus_data	*data)
> +{
> +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> +	int ret;
> +
> +	if (size != I2C_SMBUS_WORD_DATA)
> +		return -EINVAL;
> +
> +	ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
> +			    command, data->word);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (read_write == I2C_SMBUS_READ)
> +		data->word = ret;
> +
> +	return 0;
> +}
> +
> +static u32 cbus_i2c_func(struct i2c_adapter *adapter)
> +{
> +	return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
> +}
> +
> +static const struct i2c_algorithm cbus_i2c_algo = {
> +	.smbus_xfer	= cbus_i2c_smbus_xfer,
> +	.functionality	= cbus_i2c_func,
> +};
> +
> +static int cbus_i2c_remove(struct platform_device *pdev)
> +{
> +	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
> +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> +	int ret;
> +
> +	ret = i2c_del_adapter(adapter);
> +	if (ret)
> +		return ret;
> +	gpio_free(chost->clk_gpio);
> +	gpio_free(chost->dat_gpio);
> +	gpio_free(chost->sel_gpio);
> +	kfree(chost);
> +	kfree(adapter);

Please use devm_gpio_* and devm_kzalloc_*

> +	platform_set_drvdata(pdev, NULL);
> +
> +	return 0;
> +}
> +
> +static int cbus_i2c_probe(struct platform_device *pdev)
> +{
> +	struct i2c_adapter *adapter;
> +	struct cbus_host *chost;
> +	struct gpio gpios[3];
> +	int ret;
> +
> +	adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
> +	if (!adapter) {
> +		ret = -ENOMEM;
> +		goto error;
> +	}
> +
> +	chost = kzalloc(sizeof(*chost), GFP_KERNEL);
> +	if (!chost) {
> +		ret = -ENOMEM;
> +		goto error_mem;
> +	}
> +
> +	if (pdev->dev.of_node) {
> +		struct device_node *dnode = pdev->dev.of_node;
> +		if (of_gpio_count(dnode) != 3) {
> +			ret = -ENODEV;
> +			goto error_mem;
> +		}
> +		chost->clk_gpio = of_get_gpio(dnode, 0);
> +		chost->dat_gpio = of_get_gpio(dnode, 1);
> +		chost->sel_gpio = of_get_gpio(dnode, 2);
> +	} else if (pdev->dev.platform_data) {
> +		struct i2c_cbus_platform_data *pdata = pdev->dev.platform_data;
> +		chost->clk_gpio = pdata->clk_gpio;
> +		chost->dat_gpio = pdata->dat_gpio;
> +		chost->sel_gpio = pdata->sel_gpio;
> +	} else {
> +		ret = -ENODEV;
> +		goto error_mem;
> +	}
> +
> +	adapter->owner		= THIS_MODULE;
> +	adapter->class		= I2C_CLASS_HWMON;
> +	adapter->dev.parent	= &pdev->dev;
> +	adapter->nr		= pdev->id;
> +	adapter->timeout	= HZ;
> +	adapter->algo		= &cbus_i2c_algo;
> +	strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
> +
> +	spin_lock_init(&chost->lock);
> +	chost->dev = &pdev->dev;
> +
> +	gpios[0].gpio  = chost->clk_gpio;
> +	gpios[0].flags = GPIOF_OUT_INIT_LOW;
> +	gpios[0].label = "CBUS clk";
> +
> +	gpios[1].gpio  = chost->dat_gpio;
> +	gpios[1].flags = GPIOF_IN;
> +	gpios[1].label = "CBUS data";
> +
> +	gpios[2].gpio  = chost->sel_gpio;
> +	gpios[2].flags = GPIOF_OUT_INIT_HIGH;
> +	gpios[2].label = "CBUS sel";
> +
> +	ret = gpio_request_array(gpios, ARRAY_SIZE(gpios));
> +	if (ret)
> +		goto error_gpio;
> +
> +	gpio_set_value(chost->clk_gpio, 1);
> +	gpio_set_value(chost->clk_gpio, 0);

What is that pulse needed for?

> +
> +	i2c_set_adapdata(adapter, chost);
> +	platform_set_drvdata(pdev, adapter);
> +
> +	ret = i2c_add_numbered_adapter(adapter);
> +	if (ret)
> +		goto error_i2c;
> +
> +	return 0;
> +
> +error_i2c:
> +	gpio_free_array(gpios, ARRAY_SIZE(gpios));
> +error_gpio:
> +	kfree(chost);
> +error_mem:
> +	kfree(adapter);
> +error:
> +	return ret;
> +}
> +
> +#if defined(CONFIG_OF)
> +static const struct of_device_id i2c_cbus_dt_ids[] = {
> +	{ .compatible = "i2c-cbus", },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
> +#endif
> +
> +static struct platform_driver cbus_i2c_driver = {
> +	.probe	= cbus_i2c_probe,
> +	.remove	= cbus_i2c_remove,
> +	.driver	= {
> +		.owner	= THIS_MODULE,
> +		.name	= "i2c-cbus",
> +	},
> +};
> +module_platform_driver(cbus_i2c_driver);
> +
> +MODULE_ALIAS("platform:i2c-cbus");
> +MODULE_DESCRIPTION("CBUS I2C driver");
> +MODULE_AUTHOR("Juha Yrjölä");
> +MODULE_AUTHOR("David Weinehall");
> +MODULE_AUTHOR("Mikko Ylinen");
> +MODULE_AUTHOR("Felipe Balbi");
> +MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
> +MODULE_LICENSE("GPL");

So much for now.

Regards,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-14 10:08   ` Wolfram Sang
@ 2012-09-14 10:21     ` Jean Delvare
  2012-09-14 12:16     ` Felipe Balbi
  2012-09-19 20:08     ` Aaro Koskinen
  2 siblings, 0 replies; 18+ messages in thread
From: Jean Delvare @ 2012-09-14 10:21 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: Aaro Koskinen, linux-omap, linux-kernel, linux-i2c

On Fri, 14 Sep 2012 12:08:06 +0200, Wolfram Sang wrote:
> OK, I found the short paragrahp about CBUS in the I2C spec, so I2C might
> be an appropriate place. Still, before deciding if it should rather be
> in the core directory, I still have a few questions.
> 
> Also, does anybody know of a generic bit-banging implementation in the
> kernel which could be used here?
> 
> Jean: I'd appreciate your general opinion here, too.

Out of time at the moment :(

-- 
Jean Delvare

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-14 10:08   ` Wolfram Sang
  2012-09-14 10:21     ` Jean Delvare
@ 2012-09-14 12:16     ` Felipe Balbi
  2012-09-19 20:08     ` Aaro Koskinen
  2 siblings, 0 replies; 18+ messages in thread
From: Felipe Balbi @ 2012-09-14 12:16 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Aaro Koskinen, Jean Delvare, linux-omap, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 1125 bytes --]

On Fri, Sep 14, 2012 at 12:08:06PM +0200, Wolfram Sang wrote:
> On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> > Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> > Tablets.
> > 
> > The patch also adds CBUS I2C configuration for N8x0 which is one of the
> > users of this driver.
> > 
> > Cc: linux-i2c@vger.kernel.org
> > Acked-by: Felipe Balbi <balbi@ti.com>
> > Acked-by: Tony Lindgren <tony@atomide.com>
> > Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> 
> OK, I found the short paragrahp about CBUS in the I2C spec, so I2C might
> be an appropriate place. Still, before deciding if it should rather be
> in the core directory, I still have a few questions.
> 
> Also, does anybody know of a generic bit-banging implementation in the
> kernel which could be used here?

there is i2c-gpio, but it wouldn't help much for cbus, unless we change
it quite a lot. The generic i2c_algo_bit_data would have to be changed
to learn about a 3 wire bus. I guess the impact is much bigger than just
accepting this small, self-contained driver.

-- 
balbi

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 2/4] mfd: introduce retu-mfd driver
  2012-09-03 20:23 ` [PATCH 2/4] mfd: introduce retu-mfd driver Aaro Koskinen
@ 2012-09-19 16:02   ` Samuel Ortiz
  2012-09-19 20:09     ` Aaro Koskinen
  0 siblings, 1 reply; 18+ messages in thread
From: Samuel Ortiz @ 2012-09-19 16:02 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: linux-omap, linux-kernel

Hi Aaro,

On Mon, Sep 03, 2012 at 11:23:23PM +0300, Aaro Koskinen wrote:
> Retu is a multi-function device found on Nokia Internet Tablets
> implementing at least watchdog, RTC, headset detection and power button
> functionality.
> 
> This patch implements minimum functionality providing register access,
> IRQ handling and power off functions.
> 
> Cc: sameo@linux.intel.com
> Acked-by: Tony Lindgren <tony@atomide.com>
> Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> ---
>  drivers/mfd/Kconfig      |    8 +
>  drivers/mfd/Makefile     |    1 +
>  drivers/mfd/retu-mfd.c   |  347 ++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/mfd/retu.h |   22 +++
>  4 files changed, 378 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/mfd/retu-mfd.c
>  create mode 100644 include/linux/mfd/retu.h
Now with the IRQ chip code, using the regmap APIs would be even more
benefitial.

Cheers,
Samuel.

-- 
Intel Open Source Technology Centre
http://oss.intel.com/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-14 10:08   ` Wolfram Sang
  2012-09-14 10:21     ` Jean Delvare
  2012-09-14 12:16     ` Felipe Balbi
@ 2012-09-19 20:08     ` Aaro Koskinen
  2012-09-20 10:43       ` Wolfram Sang
  2 siblings, 1 reply; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-19 20:08 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: Jean Delvare, linux-omap, linux-kernel, linux-i2c

On Fri, Sep 14, 2012 at 12:08:06PM +0200, Wolfram Sang wrote:
> On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> > Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> > Tablets.
> > 
> > The patch also adds CBUS I2C configuration for N8x0 which is one of the
> > users of this driver.
> > 
> > Cc: linux-i2c@vger.kernel.org
> > Acked-by: Felipe Balbi <balbi@ti.com>
> > Acked-by: Tony Lindgren <tony@atomide.com>
> > Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> 
> OK, I found the short paragrahp about CBUS in the I2C spec, so I2C might
> be an appropriate place. Still, before deciding if it should rather be
> in the core directory, I still have a few questions.

Thanks for your feedback. I will fix up the incorrect comments, and do
other improvements (e.g. use devm_* stuff) in the next version.

The questions about delays and bit counts are very valid, but it's
difficult to do anything with them due to lack of documentation and HW on
which to test - I have Nokia tablets to test the driver, and the driver
is very reliable, but I do not know of any other HW with CBUS. :-/

A.

> Also, does anybody know of a generic bit-banging implementation in the
> kernel which could be used here?
> 
> Jean: I'd appreciate your general opinion here, too.
> 
> > diff --git a/drivers/i2c/busses/i2c-cbus.c b/drivers/i2c/busses/i2c-cbus.c
> > new file mode 100644
> > index 0000000..bacf2a9
> > --- /dev/null
> > +++ b/drivers/i2c/busses/i2c-cbus.c
> > @@ -0,0 +1,369 @@
> > +/*
> > + * CBUS I2C driver for Nokia Internet Tablets.
> > + *
> > + * Copyright (C) 2004-2010 Nokia Corporation
> > + *
> > + * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
> > + * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
> > + *
> > + * 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.
> > + */
> > +
> > +#include <linux/io.h>
> > +#include <linux/i2c.h>
> > +#include <linux/gpio.h>
> > +#include <linux/init.h>
> > +#include <linux/slab.h>
> > +#include <linux/delay.h>
> > +#include <linux/errno.h>
> > +#include <linux/kernel.h>
> > +#include <linux/module.h>
> > +#include <linux/of_gpio.h>
> > +#include <linux/i2c-cbus.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/platform_device.h>
> > +
> > +struct cbus_host {
> > +	/* host lock */
> > +	spinlock_t	lock;
> > +
> > +	struct device	*dev;
> > +
> > +	int		clk_gpio;
> > +	int		dat_gpio;
> > +	int		sel_gpio;
> > +};
> > +
> > +/**
> > + * cbus_send_bit - sends one bit over the bus
> > + * @host: the host we're using
> > + * @bit: one bit of information to send
> > + * @input: whether to set data pin as input after sending
> > + */
> > +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
> > +		unsigned input)
> > +{
> > +	int ret = 0;
> > +
> > +	gpio_set_value(host->dat_gpio, bit ? 1 : 0);
> > +	gpio_set_value(host->clk_gpio, 1);
> > +
> > +	/* The data bit is read on the rising edge of CLK */
> 
> This comment doesn't belong to the if-block, or?
> 
> > +	if (input)
> > +		ret = gpio_direction_input(host->dat_gpio);
> 
> No minimum delay for the signal to be on the bus?
> 
> > +
> > +	gpio_set_value(host->clk_gpio, 0);
> > +
> > +	return ret;
> > +}
> > +
> > +/**
> > + * cbus_send_data - sends @len amount of data over the bus
> > + * @host: the host we're using
> > + * @data: the data to send
> > + * @len: size of the transfer
> > + * @input: whether to set data pin as input after sending
> > + */
> > +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
> > +		unsigned input)
> > +{
> > +	int ret = 0;
> > +	int i;
> > +
> > +	for (i = len; i > 0; i--) {
> > +		ret = cbus_send_bit(host, data & (1 << (i - 1)),
> > +				input && (i == 1));
> > +		if (ret < 0)
> > +			goto out;
> > +	}
> > +
> > +out:
> > +	return ret;
> > +}
> > +
> > +/**
> > + * cbus_receive_bit - receives one bit from the bus
> > + * @host: the host we're using
> > + */
> > +static int cbus_receive_bit(struct cbus_host *host)
> > +{
> > +	int ret;
> > +
> > +	gpio_set_value(host->clk_gpio, 1);
> 
> No delays to ensure the signal has stabilized?
> 
> > +	ret = gpio_get_value(host->dat_gpio);
> > +	if (ret < 0)
> > +		goto out;
> > +	gpio_set_value(host->clk_gpio, 0);
> > +
> > +out:
> > +	return ret;
> > +}
> > +
> > +/**
> > + * cbus_receive_word - receives 16-bit word from the bus
> > + * @host: the host we're using
> > + */
> > +static int cbus_receive_word(struct cbus_host *host)
> > +{
> > +	int ret = 0;
> > +	int i;
> > +
> > +	for (i = 16; i > 0; i--) {
> > +		int bit = cbus_receive_bit(host);
> > +
> > +		if (bit < 0)
> > +			goto out;
> > +
> > +		if (bit)
> > +			ret |= 1 << (i - 1);
> > +	}
> > +
> > +out:
> > +	return ret;
> > +}
> > +
> > +/**
> > + * cbus_transfer - transfers data over the bus
> > + * @host: the host we're using
> > + * @rw: read/write flag
> > + * @dev: device address
> > + * @reg: register address
> > + * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
> > + */
> > +static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
> > +			 unsigned reg, unsigned data)
> > +{
> > +	unsigned long flags;
> > +	int ret;
> > +
> > +	/* We don't want interrupts disturbing our transfer */
> > +	spin_lock_irqsave(&host->lock, flags);
> > +
> > +	/* Reset state and start of transfer, SEL stays down during transfer */
> > +	gpio_set_value(host->sel_gpio, 0);
> > +
> > +	/* Set the DAT pin to output */
> > +	gpio_direction_output(host->dat_gpio, 1);
> > +
> > +	/* Send the device address */
> > +	ret = cbus_send_data(host, dev, 3, 0);
> 
> Is the device address always 3 bit? From the (admittetly sparse) I2C
> spec, I'd assume to be 7?
> 
> > +	if (ret < 0) {
> > +		dev_dbg(host->dev, "failed sending device addr\n");
> > +		goto out;
> > +	}
> > +
> > +	/* Send the rw flag */
> > +	ret = cbus_send_bit(host, rw == I2C_SMBUS_READ, 0);
> > +	if (ret < 0) {
> > +		dev_dbg(host->dev, "failed sending read/write flag\n");
> > +		goto out;
> > +	}
> > +
> > +	/* Send the device address */
> 
> Comment is wrong.
> 
> > +	ret = cbus_send_data(host, reg, 5, rw == I2C_SMBUS_READ);
> 
> Is reg always 5 bit?
> 
> > +	if (ret < 0) {
> > +		dev_dbg(host->dev, "failed sending register addr\n");
> > +		goto out;
> > +	}
> > +
> > +	if (rw == I2C_SMBUS_WRITE) {
> > +		ret = cbus_send_data(host, data, 16, 0);
> > +		if (ret < 0) {
> > +			dev_dbg(host->dev, "failed sending data\n");
> > +			goto out;
> > +		}
> > +	} else {
> 
> What about setting dat to input here and drop the parameter from
> cbus_data_send()?
> 
> > +		gpio_set_value(host->clk_gpio, 1);
> > +
> > +		ret = cbus_receive_word(host);
> > +		if (ret < 0) {
> > +			dev_dbg(host->dev, "failed receiving data\n");
> > +			goto out;
> > +		}
> > +	}
> > +
> > +	/* Indicate end of transfer, SEL goes up until next transfer */
> > +	gpio_set_value(host->sel_gpio, 1);
> > +	gpio_set_value(host->clk_gpio, 1);
> > +	gpio_set_value(host->clk_gpio, 0);
> > +
> > +out:
> > +	spin_unlock_irqrestore(&host->lock, flags);
> > +
> > +	return ret;
> > +}
> > +
> > +static int cbus_i2c_smbus_xfer(struct i2c_adapter	*adapter,
> > +			       u16			addr,
> > +			       unsigned short		flags,
> > +			       char			read_write,
> > +			       u8			command,
> > +			       int			size,
> > +			       union i2c_smbus_data	*data)
> > +{
> > +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> > +	int ret;
> > +
> > +	if (size != I2C_SMBUS_WORD_DATA)
> > +		return -EINVAL;
> > +
> > +	ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
> > +			    command, data->word);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	if (read_write == I2C_SMBUS_READ)
> > +		data->word = ret;
> > +
> > +	return 0;
> > +}
> > +
> > +static u32 cbus_i2c_func(struct i2c_adapter *adapter)
> > +{
> > +	return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
> > +}
> > +
> > +static const struct i2c_algorithm cbus_i2c_algo = {
> > +	.smbus_xfer	= cbus_i2c_smbus_xfer,
> > +	.functionality	= cbus_i2c_func,
> > +};
> > +
> > +static int cbus_i2c_remove(struct platform_device *pdev)
> > +{
> > +	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
> > +	struct cbus_host *chost = i2c_get_adapdata(adapter);
> > +	int ret;
> > +
> > +	ret = i2c_del_adapter(adapter);
> > +	if (ret)
> > +		return ret;
> > +	gpio_free(chost->clk_gpio);
> > +	gpio_free(chost->dat_gpio);
> > +	gpio_free(chost->sel_gpio);
> > +	kfree(chost);
> > +	kfree(adapter);
> 
> Please use devm_gpio_* and devm_kzalloc_*
> 
> > +	platform_set_drvdata(pdev, NULL);
> > +
> > +	return 0;
> > +}
> > +
> > +static int cbus_i2c_probe(struct platform_device *pdev)
> > +{
> > +	struct i2c_adapter *adapter;
> > +	struct cbus_host *chost;
> > +	struct gpio gpios[3];
> > +	int ret;
> > +
> > +	adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
> > +	if (!adapter) {
> > +		ret = -ENOMEM;
> > +		goto error;
> > +	}
> > +
> > +	chost = kzalloc(sizeof(*chost), GFP_KERNEL);
> > +	if (!chost) {
> > +		ret = -ENOMEM;
> > +		goto error_mem;
> > +	}
> > +
> > +	if (pdev->dev.of_node) {
> > +		struct device_node *dnode = pdev->dev.of_node;
> > +		if (of_gpio_count(dnode) != 3) {
> > +			ret = -ENODEV;
> > +			goto error_mem;
> > +		}
> > +		chost->clk_gpio = of_get_gpio(dnode, 0);
> > +		chost->dat_gpio = of_get_gpio(dnode, 1);
> > +		chost->sel_gpio = of_get_gpio(dnode, 2);
> > +	} else if (pdev->dev.platform_data) {
> > +		struct i2c_cbus_platform_data *pdata = pdev->dev.platform_data;
> > +		chost->clk_gpio = pdata->clk_gpio;
> > +		chost->dat_gpio = pdata->dat_gpio;
> > +		chost->sel_gpio = pdata->sel_gpio;
> > +	} else {
> > +		ret = -ENODEV;
> > +		goto error_mem;
> > +	}
> > +
> > +	adapter->owner		= THIS_MODULE;
> > +	adapter->class		= I2C_CLASS_HWMON;
> > +	adapter->dev.parent	= &pdev->dev;
> > +	adapter->nr		= pdev->id;
> > +	adapter->timeout	= HZ;
> > +	adapter->algo		= &cbus_i2c_algo;
> > +	strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
> > +
> > +	spin_lock_init(&chost->lock);
> > +	chost->dev = &pdev->dev;
> > +
> > +	gpios[0].gpio  = chost->clk_gpio;
> > +	gpios[0].flags = GPIOF_OUT_INIT_LOW;
> > +	gpios[0].label = "CBUS clk";
> > +
> > +	gpios[1].gpio  = chost->dat_gpio;
> > +	gpios[1].flags = GPIOF_IN;
> > +	gpios[1].label = "CBUS data";
> > +
> > +	gpios[2].gpio  = chost->sel_gpio;
> > +	gpios[2].flags = GPIOF_OUT_INIT_HIGH;
> > +	gpios[2].label = "CBUS sel";
> > +
> > +	ret = gpio_request_array(gpios, ARRAY_SIZE(gpios));
> > +	if (ret)
> > +		goto error_gpio;
> > +
> > +	gpio_set_value(chost->clk_gpio, 1);
> > +	gpio_set_value(chost->clk_gpio, 0);
> 
> What is that pulse needed for?
> 
> > +
> > +	i2c_set_adapdata(adapter, chost);
> > +	platform_set_drvdata(pdev, adapter);
> > +
> > +	ret = i2c_add_numbered_adapter(adapter);
> > +	if (ret)
> > +		goto error_i2c;
> > +
> > +	return 0;
> > +
> > +error_i2c:
> > +	gpio_free_array(gpios, ARRAY_SIZE(gpios));
> > +error_gpio:
> > +	kfree(chost);
> > +error_mem:
> > +	kfree(adapter);
> > +error:
> > +	return ret;
> > +}
> > +
> > +#if defined(CONFIG_OF)
> > +static const struct of_device_id i2c_cbus_dt_ids[] = {
> > +	{ .compatible = "i2c-cbus", },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
> > +#endif
> > +
> > +static struct platform_driver cbus_i2c_driver = {
> > +	.probe	= cbus_i2c_probe,
> > +	.remove	= cbus_i2c_remove,
> > +	.driver	= {
> > +		.owner	= THIS_MODULE,
> > +		.name	= "i2c-cbus",
> > +	},
> > +};
> > +module_platform_driver(cbus_i2c_driver);
> > +
> > +MODULE_ALIAS("platform:i2c-cbus");
> > +MODULE_DESCRIPTION("CBUS I2C driver");
> > +MODULE_AUTHOR("Juha Yrjölä");
> > +MODULE_AUTHOR("David Weinehall");
> > +MODULE_AUTHOR("Mikko Ylinen");
> > +MODULE_AUTHOR("Felipe Balbi");
> > +MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
> > +MODULE_LICENSE("GPL");
> 
> So much for now.
> 
> Regards,
> 
>    Wolfram
> 
> -- 
> Pengutronix e.K.                           | Wolfram Sang                |
> Industrial Linux Solutions                 | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 2/4] mfd: introduce retu-mfd driver
  2012-09-19 16:02   ` Samuel Ortiz
@ 2012-09-19 20:09     ` Aaro Koskinen
  0 siblings, 0 replies; 18+ messages in thread
From: Aaro Koskinen @ 2012-09-19 20:09 UTC (permalink / raw)
  To: Samuel Ortiz; +Cc: linux-omap, linux-kernel

Hi,

On Wed, Sep 19, 2012 at 06:02:30PM +0200, Samuel Ortiz wrote:
> On Mon, Sep 03, 2012 at 11:23:23PM +0300, Aaro Koskinen wrote:
> > Retu is a multi-function device found on Nokia Internet Tablets
> > implementing at least watchdog, RTC, headset detection and power button
> > functionality.
> > 
> > This patch implements minimum functionality providing register access,
> > IRQ handling and power off functions.
> > 
> > Cc: sameo@linux.intel.com
> > Acked-by: Tony Lindgren <tony@atomide.com>
> > Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> > ---
> >  drivers/mfd/Kconfig      |    8 +
> >  drivers/mfd/Makefile     |    1 +
> >  drivers/mfd/retu-mfd.c   |  347 ++++++++++++++++++++++++++++++++++++++++++++++
> >  include/linux/mfd/retu.h |   22 +++
> >  4 files changed, 378 insertions(+), 0 deletions(-)
> >  create mode 100644 drivers/mfd/retu-mfd.c
> >  create mode 100644 include/linux/mfd/retu.h
> Now with the IRQ chip code, using the regmap APIs would be even more
> benefitial.

Thanks for feedback, I'll look into this.

A.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/4] i2c: introduce i2c-cbus driver
  2012-09-19 20:08     ` Aaro Koskinen
@ 2012-09-20 10:43       ` Wolfram Sang
  0 siblings, 0 replies; 18+ messages in thread
From: Wolfram Sang @ 2012-09-20 10:43 UTC (permalink / raw)
  To: Aaro Koskinen; +Cc: Jean Delvare, linux-omap, linux-kernel, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 1480 bytes --]

On Wed, Sep 19, 2012 at 11:08:13PM +0300, Aaro Koskinen wrote:
> On Fri, Sep 14, 2012 at 12:08:06PM +0200, Wolfram Sang wrote:
> > On Mon, Sep 03, 2012 at 11:23:22PM +0300, Aaro Koskinen wrote:
> > > Add i2c driver to enable access to devices behind CBUS on Nokia Internet
> > > Tablets.
> > > 
> > > The patch also adds CBUS I2C configuration for N8x0 which is one of the
> > > users of this driver.
> > > 
> > > Cc: linux-i2c@vger.kernel.org
> > > Acked-by: Felipe Balbi <balbi@ti.com>
> > > Acked-by: Tony Lindgren <tony@atomide.com>
> > > Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
> > 
> > OK, I found the short paragrahp about CBUS in the I2C spec, so I2C might
> > be an appropriate place. Still, before deciding if it should rather be
> > in the core directory, I still have a few questions.
> 
> Thanks for your feedback. I will fix up the incorrect comments, and do
> other improvements (e.g. use devm_* stuff) in the next version.
> 
> The questions about delays and bit counts are very valid, but it's
> difficult to do anything with them due to lack of documentation and HW on
> which to test - I have Nokia tablets to test the driver, and the driver
> is very reliable, but I do not know of any other HW with CBUS. :-/

Fine enough, then please add comments saying that.

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2012-09-20 10:43 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-03 20:23 [PATCH 0/4] cbus/retu drivers to mainline Aaro Koskinen
2012-09-03 20:23 ` [PATCH 1/4] i2c: introduce i2c-cbus driver Aaro Koskinen
2012-09-04  9:05   ` Felipe Balbi
2012-09-04  9:31     ` Aaro Koskinen
2012-09-04 11:41       ` Felipe Balbi
2012-09-13 10:53   ` Wolfram Sang
2012-09-13 14:51     ` Aaro Koskinen
2012-09-14 10:08   ` Wolfram Sang
2012-09-14 10:21     ` Jean Delvare
2012-09-14 12:16     ` Felipe Balbi
2012-09-19 20:08     ` Aaro Koskinen
2012-09-20 10:43       ` Wolfram Sang
2012-09-03 20:23 ` [PATCH 2/4] mfd: introduce retu-mfd driver Aaro Koskinen
2012-09-19 16:02   ` Samuel Ortiz
2012-09-19 20:09     ` Aaro Koskinen
2012-09-03 20:23 ` [PATCH 3/4] watchdog: introduce retu_wdt driver Aaro Koskinen
2012-09-03 20:23 ` [PATCH 4/4] input: misc: introduce retu-pwrbutton Aaro Koskinen
2012-09-04 16:09 ` [PATCH 0/4] cbus/retu drivers to mainline Andi Shyti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).