Skip to contentSkip to conversation

mfd: Kontron PLD mfd driver

PATCH Superseded Kevin Strasser 10 participants 48 messages Apr 8, 2013 – Jul 21, 2013

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 mfd: Kontron PLD mfd driver

From: Michael Brunner <[email protected]>

Add core MFD driver for the on-board PLD found on some Kontron embedded modules. The PLD device may provide functions like watchdog, GPIO, UART and I2C bus.

The following modules are supported:
* COMe-bIP#
* COMe-bPC2 (ETXexpress-PC)
* COMe-bSC# (ETXexpress-SC T#)
* COMe-cCT6
* COMe-cDC2 (microETXexpress-DC)
* COMe-cPC2 (microETXexpress-PC)
* COMe-mCT10
* COMe-mSP1 (nanoETXexpress-SP)
* COMe-mTT10 (nanoETXexpress-TT)
* ETX-OH

---
 drivers/mfd/Kconfig        |   23 +
 drivers/mfd/Makefile       |    1 +
 drivers/mfd/kempld-core.c  | 1088 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/kempld.h |  152 +++++++
 4 files changed, 1264 insertions(+)
 create mode 100644 drivers/mfd/kempld-core.c
 create mode 100644 include/linux/mfd/kempld.h
drivers/mfd/Kconfig +23 -0
index c346941..2491843 100644
@@ -912,6 +912,29 @@ config MFD_TIMBERDALE
 	The timberdale FPGA can be found on the Intel Atom development board
 	for in-vehicle infontainment, called Russellville.
 
+config MFD_KEMPLD
+	tristate "Support for Kontron module PLD device"
+	select MFD_CORE
+	help
+	  This is the core driver for the PLD device found on some Kontron
+	  ETX and COMexpress (ETXexpress) modules. The PLD device may provide
+	  functions like watchdog, GPIO, UART and I2C bus.
+
+	  The following modules are supported:
+		* COMe-bIP#
+		* COMe-bPC2 (ETXexpress-PC)
+		* COMe-bSC# (ETXexpress-SC T#)
+		* COMe-cCT6
+		* COMe-cDC2 (microETXexpress-DC)
+		* COMe-cPC2 (microETXexpress-PC)
+		* COMe-mCT10
+		* COMe-mSP1 (nanoETXexpress-SP)
+		* COMe-mTT10 (nanoETXexpress-TT)
+		* ETX-OH
+
+	  This driver can also be built as a module. If so, the module
+	  will be called kempld-core.
+
 config LPC_SCH
 	tristate "Intel SCH LPC"
 	depends on PCI && GENERIC_HARDIRQS
drivers/mfd/Makefile +1 -0
index b90409c..cb1cb16 100644
@@ -123,6 +123,7 @@ obj-$(CONFIG_MFD_DB8500_PRCMU)	+= db8500-prcmu.o
 obj-$(CONFIG_AB8500_CORE)	+= ab8500-core.o ab8500-sysctrl.o
 obj-$(CONFIG_MFD_TIMBERDALE)    += timberdale.o
 obj-$(CONFIG_PMIC_ADP5520)	+= adp5520.o
+obj-$(CONFIG_MFD_KEMPLD)    += kempld-core.o
 obj-$(CONFIG_LPC_SCH)		+= lpc_sch.o
 obj-$(CONFIG_LPC_ICH)		+= lpc_ich.o
 obj-$(CONFIG_MFD_RDC321X)	+= rdc321x-southbridge.o
drivers/mfd/kempld-core.c new file +1088 -0

1,088 lines not on this page · raw message

include/linux/mfd/kempld.h new file +152 -0

152 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 2/4 i2c: Kontron PLD i2c bus driver

From: Michael Brunner <[email protected]>

Add i2c support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/i2c/busses/Kconfig      |   20 ++
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  679 +++++++++++++++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-kempld.h |   86 +++++
 4 files changed, 786 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
 create mode 100644 drivers/i2c/busses/i2c-kempld.h
drivers/i2c/busses/Kconfig +20 -0
index adfee98..7aecd61 100644
@@ -494,6 +494,26 @@ config I2C_IOP3XX
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-iop3xx.
 
+config I2C_KEMPLD
+	tristate "Kontron COM I2C"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the I2C bus interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-kempld.
+
+config I2C_KEMPLD_MUX
+	bool "Enable MUXed I2C ports (EXPERIMENTAL)"
+	depends on I2C_KEMPLD && I2C_MUX
+	default n
+	help
+	  This enables support for additional I2C ports available on some
+	  modules. Usually those ports are for board internal usage and
+	  not routed outside the module.
+	  Do not use this option unless you know what you are doing!
+
 config I2C_MPC
 	tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
 	depends on PPC
drivers/i2c/busses/Makefile +1 -0
index 8f4fc23..411b8ce 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_I2C_IBM_IIC)	+= i2c-ibm_iic.o
 obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
 obj-$(CONFIG_I2C_INTEL_MID)	+= i2c-intel-mid.o
 obj-$(CONFIG_I2C_IOP3XX)	+= i2c-iop3xx.o
+obj-$(CONFIG_I2C_KEMPLD)    += i2c-kempld.o
 obj-$(CONFIG_I2C_MPC)		+= i2c-mpc.o
 obj-$(CONFIG_I2C_MV64XXX)	+= i2c-mv64xxx.o
 obj-$(CONFIG_I2C_MXS)		+= i2c-mxs.o
drivers/i2c/busses/i2c-kempld.c new file +679 -0

679 lines not on this page · raw message

drivers/i2c/busses/i2c-kempld.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#2 lore raw

Re: [PATCH 2/4] i2c: Kontron PLD i2c bus driver

Quoted 7 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add i2c support for the on-board PLD found on some Kontron embedded modules.

Overall well written, though I have a couple of nitpicks.

I would prefer two separate drivers, one for the mux and one for the i2c bus. If that is possible, it would help getting rid of the #ifdef in the code, which is frowned upon in the kernel.

I dislike unnecessary ( ). Maintainer's call, though.

Couple of places have missing spaces around operators (checkpatch doesn't catch all those).

As far as I know, devm_ functions are supposed to print an error message on failure, so it should be unnecessary to print another one if that happens (this might need some confirmation).

Thanks,
Guenter

Quoted 837 lines
---
 drivers/i2c/busses/Kconfig      |   20 ++
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  679 +++++++++++++++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-kempld.h |   86 +++++
 4 files changed, 786 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
 create mode 100644 drivers/i2c/busses/i2c-kempld.h
drivers/i2c/busses/Kconfig +20 -0
index adfee98..7aecd61 100644
@@ -494,6 +494,26 @@ config I2C_IOP3XX
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-iop3xx.
 
+config I2C_KEMPLD
+	tristate "Kontron COM I2C"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the I2C bus interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-kempld.
+
+config I2C_KEMPLD_MUX
+	bool "Enable MUXed I2C ports (EXPERIMENTAL)"
+	depends on I2C_KEMPLD && I2C_MUX
+	default n
+	help
+	  This enables support for additional I2C ports available on some
+	  modules. Usually those ports are for board internal usage and
+	  not routed outside the module.
+	  Do not use this option unless you know what you are doing!
+
 config I2C_MPC
 	tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
 	depends on PPC
drivers/i2c/busses/Makefile +1 -0
index 8f4fc23..411b8ce 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_I2C_IBM_IIC)	+= i2c-ibm_iic.o
 obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
 obj-$(CONFIG_I2C_INTEL_MID)	+= i2c-intel-mid.o
 obj-$(CONFIG_I2C_IOP3XX)	+= i2c-iop3xx.o
+obj-$(CONFIG_I2C_KEMPLD)    += i2c-kempld.o
 obj-$(CONFIG_I2C_MPC)		+= i2c-mpc.o
 obj-$(CONFIG_I2C_MV64XXX)	+= i2c-mv64xxx.o
 obj-$(CONFIG_I2C_MXS)		+= i2c-mxs.o
drivers/i2c/busses/i2c-kempld.c new file +679 -0
new file mode 100644
index 0000000..c6b44e7
@@ -0,0 +1,679 @@
+/*
+ *  i2c-kempld.c: I2C bus driver for Kontron COM modules
+ *
+ *  Copyright (c) 2010-2013 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  The driver is based on the i2c-ocores driver by Peter Korsgaard.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/errno.h>
+#include <linux/platform_device.h>
+#include <linux/i2c.h>
+#ifdef CONFIG_I2C_KEMPLD_MUX
+#include <linux/i2c-mux.h>
+#endif
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/mfd/kempld.h>
+#include <linux/interrupt.h>
+#include <linux/time.h>
+
+#include "i2c-kempld.h"
+
+static int scl_frequency;
+static int i2c_bus = -1;
+static int i2c_mx_bus = -1;
+static bool force_polling;
+static int i2c_gpio_mux = -1;
+
+#ifdef CONFIG_I2C_KEMPLD_MUX
+static int kempld_i2cmux_select(struct i2c_adapter *adap, void *data, u32 chan)
+{
+	struct kempld_i2c_data *i2c = data;
+	struct kempld_device_data *pld = i2c->pld;
+	int ret = 0;
+
+	if ((i2c->state == STATE_DONE)
+		|| (i2c->state == STATE_ERROR)) {
+		if (i2c->mx != chan) {
+			kempld_get_mutex_set_index(pld, KEMPLD_I2C_MX);
+			i2c->mx = chan & 0x0f;
+			kempld_write8(pld, KEMPLD_I2C_MX, i2c->mx);
+			kempld_release_mutex(pld);
+		}
+
+		/* Reset controller if the last transfer ended with an error */
+		if (i2c->state == STATE_ERROR) {
+			u8 ctrl;
+
+			kempld_get_mutex_set_index(pld, KEMPLD_I2C_CMD);
+			ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+			ctrl &= ~OCI2C_CTRL_EN;
+			kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+			kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_IACK);
+			ctrl |= OCI2C_CTRL_EN;
+			kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+			kempld_release_mutex(pld);
+		}
+
+	} else
+		ret = -EBUSY;
+
+	return ret;
+}
+
+static void kempld_i2cmux_del(struct kempld_i2c_data *i2c)
+{
+	int i;
+
+	for (i = 0; i <= i2c->mx_max; i++) {
+		if (i2c->mxadap[i]) {
+			i2c_del_mux_adapter(i2c->mxadap[i]);
+			i2c->mxadap[i] = NULL;
+		}
+	}
+}
+
+static int kempld_i2cmux_add(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	int i;
+	int ret = -ENODEV;
+
+	for (i = 0; (i <= (i2c->mx_max)); i++) {
+		i2c->mxadap[i] = i2c_add_mux_adapter(&i2c->adap,
+							NULL, i2c, 0, i, 0,
+							kempld_i2cmux_select,
+							NULL);
+		if (!i2c->mxadap[i]) {
+			ret = -ENODEV;
+			dev_err(pld->dev,
+				"Failed to register MUX adapter %d\n", i);
+			goto add_mux_adapter_failed;
+		}
+	}
+
+	return 0;
+
+add_mux_adapter_failed:
+	kempld_i2cmux_del(i2c);
+
+	return ret;
+}
+
+#else
+#define kempld_i2cmux_add(x) (0)
+#define kempld_i2cmux_del(x) do {} while (0)
+#endif
+
+static int kempld_i2c_process(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	struct i2c_msg *msg = i2c->msg;
+	u8 stat = kempld_read8(pld, KEMPLD_I2C_STATUS);
+
+	/* ready? */
+	if (stat & OCI2C_STAT_TIP)
+		return -EBUSY;
+
+	if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
+		/* stop has been sent */
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_IACK);
+		if (i2c->irq)
+			wake_up(&i2c->wait);
+		if (i2c->state == STATE_ERROR)
+			return -EIO;
+		else
+			return 0;
+	}
+
+	/* error? */
+	if (stat & OCI2C_STAT_ARBLOST) {
+		i2c->state = STATE_ERROR;
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_STOP);
+		return -EAGAIN;
+	}
+
+	if (i2c->state == STATE_INIT) {
+		/* check if bus is free */
+		if (stat & OCI2C_STAT_BUSY)
+			return -EBUSY;
+
+		i2c->state = STATE_ADDR;
+	}
+
+	if (i2c->state == STATE_ADDR) {
+		u8 addr;
+		/* 10 bit address? */
+		if (i2c->msg->flags & I2C_M_TEN) {
+			addr = 0xf0 | ((i2c->msg->addr >> 7) & 0x6);
+			i2c->state = STATE_ADDR10;
+		} else {
+			addr = (i2c->msg->addr << 1);
+			i2c->state = STATE_START;
+		}
+
+		/* set read bit if necessary */
+		addr |= (i2c->msg->flags & I2C_M_RD) ? 1 : 0;
+
+		kempld_write8(pld, KEMPLD_I2C_DATA, addr);
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_START);
+
+		return 0;
+	}
+
+	/* second part of 10 bit addressing */
+	if (i2c->state == STATE_ADDR10) {
+		kempld_write8(pld, KEMPLD_I2C_DATA, i2c->msg->addr & 0xff);
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_WRITE);
+
+		i2c->state = STATE_START;
+		return 0;
+	}
+
+	if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
+		i2c->state =
+			(msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
+
+		if (stat & OCI2C_STAT_NACK) {
+			i2c->state = STATE_ERROR;
+			kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_STOP);
+			return -ENXIO;
+		}
+	} else
+		msg->buf[i2c->pos++] = kempld_read8(pld, KEMPLD_I2C_DATA);
+
+	/* end of msg? */
+	if (i2c->pos >= msg->len) {
+		i2c->nmsgs--;
+		i2c->msg++;
+		i2c->pos = 0;
+		msg = i2c->msg;
+
+		if (i2c->nmsgs) {	/* end? */
+			/* send start? */
+			if (!(msg->flags & I2C_M_NOSTART)) {
+				i2c->state = STATE_ADDR;
+				if (i2c->irq)
+					wake_up(&i2c->wait);
+				return 0;
+			} else
+				i2c->state = (msg->flags & I2C_M_RD)
+					? STATE_READ : STATE_WRITE;
+		} else {
+			i2c->state = STATE_DONE;
+			kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_STOP);
+			return 0;
+		}
+	}
+
+	if (i2c->state == STATE_READ) {
+		kempld_write8(pld, KEMPLD_I2C_CMD, i2c->pos == (msg->len-1) ?
+			OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
+	} else {
+		kempld_write8(pld, KEMPLD_I2C_DATA, msg->buf[i2c->pos++]);
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_WRITE);
+	}
+
+	return 0;
+}
+
+static irqreturn_t kempld_i2c_isr(int irq, void *dev_id)
+{
+	struct kempld_i2c_data *i2c = dev_id;
+
+	/* The actual ISR handler is put into a tasklet as it may block
+	 * and therefore rescheduling must be possible */
+	tasklet_schedule(&i2c->tasklet);
+
+	return IRQ_HANDLED;
+}
+
+void kempld_i2c_tasklet(unsigned long data)
+{
+	struct kempld_i2c_data *i2c = (struct kempld_i2c_data *)data;
+	struct kempld_device_data *pld = i2c->pld;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_I2C_STATUS);
+	kempld_i2c_process(i2c);
+	kempld_release_mutex(pld);
+}
+
+static int kempld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+			int num)
+{
+	struct kempld_i2c_data *i2c = i2c_get_adapdata(adap);
+	struct kempld_device_data *pld = i2c->pld;
+	unsigned long timeout = jiffies + HZ;
+	int ret;
+
+	i2c->msg = msgs;
+	i2c->pos = 0;
+	i2c->nmsgs = num;
+	i2c->state = STATE_INIT;
+
+	/* handle the transfer */
+	while (time_before(jiffies, timeout)) {
+		kempld_get_mutex_set_index(pld, KEMPLD_I2C_STATUS);
+		ret = kempld_i2c_process(i2c);
+		kempld_release_mutex(pld);
+
+		if (i2c->irq && ((i2c->state >= STATE_START)
+				 || (i2c->state == STATE_ERROR))) {
+			wait_event_timeout(i2c->wait,
+					(i2c->state == STATE_ERROR) ||
+					(i2c->state == STATE_DONE) ||
+					(i2c->state == STATE_ADDR), HZ);
+			if (i2c->state == STATE_ERROR)
+				ret = -EIO;
+		}
+
+		if ((i2c->state == STATE_DONE)
+			|| (i2c->state == STATE_ERROR))
+			return (i2c->state == STATE_DONE) ? num : ret;
+
+		if (ret == 0)
+			timeout = jiffies + HZ;
+
+		usleep_range(5, 15);
+	}
+
+	i2c->state = STATE_ERROR;
+
+	return -ETIMEDOUT;
+}
+
+static void kempld_i2c_device_init(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	long prescale;
+	u16 prescale_corr;
+	u8 cfg;
+	u8 ctrl;
+	u8 stat;
+	u8 mx;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_I2C_CONTROL);
+
+	ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+	if (ctrl & OCI2C_CTRL_EN)
+		i2c->was_active = 1;
+
+	/* set bus frequency */
+	if (scl_frequency > 0) {
+		/* make sure the device is disabled */
+		ctrl &= ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN);
+		kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+
+		/* The clock frequency calculation has been changed a bit
+		 * between the spec. revisions */
+		if (pld->info.spec_major == 1)
+			prescale = (pld->pld_clock / (scl_frequency*5)) - 1000;
+		else
+			prescale = (pld->pld_clock / (scl_frequency*4)) - 3000;
+
+		/* Prevent negative prescaler values */
+		if (prescale < 0)
+			prescale = 0;
+
+		/* Round to the best matching value */
+		prescale_corr = prescale / 1000;
+		if ((prescale % 1000) >= 500)
+			prescale_corr++;
+
+		kempld_write8(pld, KEMPLD_I2C_PRELOW, prescale_corr & 0xff);
+		kempld_write8(pld, KEMPLD_I2C_PREHIGH, prescale_corr >> 8);
+	}
+
+	/* Activate I2C bus output on GPIO pins */
+	if (i2c_gpio_mux > -1) {
+		cfg = kempld_read8(pld, KEMPLD_CFG);
+		if (i2c_gpio_mux)
+			cfg |= KEMPLD_CFG_GPIO_I2C_MUX;
+		else
+			cfg &= ~KEMPLD_CFG_GPIO_I2C_MUX;
+		kempld_write8(pld, KEMPLD_CFG, cfg);
+	}
+	cfg = kempld_read8(pld, KEMPLD_CFG);
+	if (cfg & KEMPLD_CFG_GPIO_I2C_MUX)
+		i2c->gpio_mux = 1;
+	else
+		i2c->gpio_mux = 0;
+	if (((i2c_gpio_mux > 0) && (!i2c->gpio_mux))
+		|| ((i2c_gpio_mux == 0) && (i2c->gpio_mux)))
+		dev_warn(pld->dev, "Unable to change GPIO I2C MUX setting\n");
+
+	/* Check how much multiplexed I2C busses we have */
+	mx = kempld_read8(pld, KEMPLD_I2C_MX);
+	if (pld->info.spec_major > 1) {
+		i2c->mx_max = KEMPLD_I2C_MX_GET_MAX(mx);
+		if (i2c->mx_max == 0xf) /* No multiplexer available */
+			i2c->mx_max = 0;
+	} else
+		i2c->mx_max = 1;
+	/* 2 busses should be enough for all
+	 * boards using specification revision 1 */
+
+	/* Check which MX setting should be set */
+	if ((i2c_mx_bus == -1) || (i2c_mx_bus > i2c->mx_max)) {
+		if (i2c_mx_bus > i2c->mx_max) {
+			dev_err(pld->dev,
+				"bus selected with i2c_mx_bus not available "
+				"- leaving MX setting unchanged\n");
+		}
+		i2c->mx = mx & KEMPLD_I2C_MX_MASK;
+	} else
+		i2c->mx = i2c_mx_bus;
+
+	/* Connect the controller to the chosen bus output */
+	kempld_write8(pld, KEMPLD_I2C_MX, i2c->mx);
+
+	/* enable the device */
+	kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_IACK);
+	ctrl |= OCI2C_CTRL_EN;
+	kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+
+	/* If bus is busy send a STOP signal to be sure the controller is
+	 * not hanging... */
+	stat = kempld_read8(pld, KEMPLD_I2C_STATUS);
+	if (stat & OCI2C_STAT_BUSY) {
+		dev_warn(pld->dev,
+			 "I2C bus is busy - generating stop signal\n");
+		kempld_write8(pld, KEMPLD_I2C_CMD, OCI2C_CMD_STOP);
+	}
+
+	kempld_release_mutex(pld);
+
+	if ((pld->info.spec_major == 1) && (i2c->mx == 0xf))
+		i2c->mx = 0;
+}
+
+static void kempld_i2c_irq_enable(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	u8 irq, ctrl;
+	int ret;
+
+	irq = i2c->irq;
+
+	/* This only has to be done once */
+	if (i2c->irq == 0) {
+		kempld_get_mutex_set_index(pld, KEMPLD_IRQ_I2C);
+		irq = kempld_read8(pld, KEMPLD_IRQ_I2C);
+		kempld_release_mutex(pld);
+
+		/* Leave if interrupts are not supported by the I2C core */
+		if ((irq & 0xf0) == 0xf0)
+			return;
+		irq &= 0x0f;
+		if (irq == 0)
+			return;
+
+		/* Initialize interrupt handlers if not already done */
+		init_waitqueue_head(&i2c->wait);
+		tasklet_init(&i2c->tasklet, kempld_i2c_tasklet,
+				(unsigned long)i2c);
+
+		ret = devm_request_irq(pld->dev, irq, kempld_i2c_isr,
+					IRQF_SHARED, i2c->adap.name, i2c);
+		if (ret) {
+			dev_err(pld->dev,
+				"Unable to claim IRQ - using polling mode\n");
+			return;
+		}
+	}
+
+	/* Now enable interrupts in the controller */
+	kempld_get_mutex_set_index(pld, KEMPLD_I2C_CONTROL);
+	ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+	ctrl |= OCI2C_CTRL_IEN;
+	kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+	kempld_release_mutex(pld);
+
+	i2c->irq = irq & 0x0f;
+}
+
+static void kempld_i2c_irq_disable(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	u8 ctrl;
+	int irq;
+
+	if (i2c->irq == 0)
+		return;
+
+	irq = i2c->irq;
+	i2c->irq = 0;
+
+	tasklet_kill(&i2c->tasklet);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_I2C_CONTROL);
+	ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+	ctrl &= ~OCI2C_CTRL_IEN;
+	kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+	kempld_release_mutex(pld);
+
+	devm_free_irq(pld->dev, irq, i2c);
+}
+
+static u32 kempld_i2c_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR
+		| I2C_FUNC_SMBUS_EMUL;
+}
+
+static const struct i2c_algorithm kempld_i2c_algorithm = {
+	.master_xfer	= kempld_i2c_xfer,
+	.functionality	= kempld_i2c_func,
+};
+
+static struct i2c_adapter kempld_i2c_adapter = {
+	.owner		= THIS_MODULE,
+	.name		= "i2c-kempld",
+	.class		= I2C_CLASS_HWMON | I2C_CLASS_SPD,
+	.algo		= &kempld_i2c_algorithm,
+};
+
+static int kempld_i2c_get_scl_frequency(struct kempld_i2c_data *i2c)
+{
+	struct kempld_device_data *pld = i2c->pld;
+	int frequency;
+	u16 prescale;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_I2C_PRELOW);
+
+	prescale = kempld_read8(pld, KEMPLD_I2C_PRELOW)
+		| kempld_read8(pld, KEMPLD_I2C_PREHIGH)<<8;
+
+	kempld_release_mutex(pld);
+
+	/* The clock frequency calculation has been changed a bit
+	 * between the spec. revisions */
+	if (pld->info.spec_major == 1)
+		frequency = (pld->pld_clock / (prescale + 1)) / 5000;
+	else
+		frequency = (pld->pld_clock / (prescale + 3)) / 4000;
+
+	return frequency;
+}
+
+static int kempld_i2c_probe(struct platform_device *pdev)
+{
+	struct kempld_i2c_data *i2c;
+	struct kempld_device_data *pld;
+	int ret;
+
+	pld = dev_get_drvdata(pdev->dev.parent);
+
+	i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
+	if (!i2c)
+		return -ENOMEM;
+
+	i2c->pld = pld;
+
+	kempld_i2c_device_init(i2c);
+
+	/* hook up driver to tree */
+	platform_set_drvdata(pdev, i2c);
+	i2c->adap = kempld_i2c_adapter;
+	i2c_set_adapdata(&i2c->adap, i2c);
+	i2c->adap.dev.parent = &pdev->dev;
+
+	i2c->irq = 0;
+	if (!force_polling)
+		kempld_i2c_irq_enable(i2c);
+
+	/* add I2C adapter to I2C tree */
+	i2c->adap.nr = i2c_bus;
+	ret = i2c_add_numbered_adapter(&i2c->adap);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to add adapter\n");
+		goto add_adapter_failed;
+	}
+
+	ret = kempld_i2cmux_add(i2c);
+	if (ret)
+		goto add_mux_adapters_failed;
+
+	dev_info(pld->dev, "I2C bus initialized with %d kHz SCL frequency\n",
+		 kempld_i2c_get_scl_frequency(i2c));
+	dev_info(pld->dev, "I2C MUX connected to bus %d (available: %s%d)\n",
+		 i2c->mx, i2c->mx_max ? "0-" : "", i2c->mx_max);
+	dev_info(pld->dev, "I2C IRQs %s\n", i2c->irq ? "enabled" : "disabled");
+	if (i2c->gpio_mux)
+		dev_info(pld->dev, "GPIO I2C MUX pins enabled\n");
+
+	return 0;
+
+add_mux_adapters_failed:
+	i2c_del_adapter(&i2c->adap);
+add_adapter_failed:
+	kfree(i2c);
+
+	return ret;
+}
+
+static int kempld_i2c_remove(struct platform_device *pdev)
+{
+	struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
+	struct kempld_device_data *pld = i2c->pld;
+	u8 ctrl;
+
+	kempld_i2c_irq_disable(i2c);
+
+	if (!i2c->was_active) {
+		/* disable I2C logic if it was not activated before the
+		 * driver loaded */
+		kempld_get_mutex_set_index(pld, KEMPLD_I2C_CONTROL);
+		ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+		ctrl &= ~OCI2C_CTRL_EN;
+		kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+		kempld_release_mutex(pld);
+	}
+
+	/* remove adapter & data */
+	kempld_i2cmux_del(i2c);
+	i2c_del_adapter(&i2c->adap);
+
+	platform_set_drvdata(pdev, NULL);
+
+	kfree(i2c);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int kempld_i2c_suspend(struct platform_device *pdev, pm_message_t state)
+{
+	struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
+	struct kempld_device_data *pld = i2c->pld;
+	u8 ctrl;
+
+	kempld_i2c_irq_disable(i2c);
+
+	if (!i2c->was_active) {
+		/* make sure the device is disabled */
+		kempld_get_mutex_set_index(pld, KEMPLD_I2C_CONTROL);
+		ctrl = kempld_read8(pld, KEMPLD_I2C_CONTROL);
+		ctrl &= ~OCI2C_CTRL_EN;
+		kempld_write8(pld, KEMPLD_I2C_CONTROL, ctrl);
+		kempld_release_mutex(pld);
+	}
+
+	return 0;
+}
+
+static int kempld_i2c_resume(struct platform_device *pdev)
+{
+	struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
+
+	kempld_i2c_device_init(i2c);
+	kempld_i2c_irq_enable(i2c);
+
+	return 0;
+}
+#else
+#define kempld_i2c_suspend	NULL
+#define kempld_i2c_resume	NULL
+#endif
+
+static struct platform_driver kempld_i2c_driver = {
+	.driver = {
+		.name = "kempld-i2c",
+		.owner = THIS_MODULE,
+	},
+	.probe		= kempld_i2c_probe,
+	.remove		= kempld_i2c_remove,
+	.suspend	= kempld_i2c_suspend,
+	.resume		= kempld_i2c_resume,
+};
+
+static int __init kempld_i2c_init(void)
+{
+	/* Check if a valid value for the i2c_mx_bus parameter is provided */
+	if ((i2c_mx_bus != -1) && (i2c_mx_bus & ~KEMPLD_I2C_MX_MASK))
+		return -EINVAL;
+
+	return platform_driver_register(&kempld_i2c_driver);
+}
+
+static void __exit kempld_i2c_exit(void)
+{
+	platform_driver_unregister(&kempld_i2c_driver);
+}
+
+module_init(kempld_i2c_init);
+module_exit(kempld_i2c_exit);
+
+module_param(scl_frequency, int, 0);
+module_param(i2c_bus, int, 0);
+module_param(i2c_mx_bus, int, 0);
+module_param(force_polling, bool, 0);
+module_param(i2c_gpio_mux, int, 0);
+
+MODULE_DESCRIPTION("KEM PLD I2C Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:kempld_i2c");
+MODULE_PARM_DESC(scl_frequency, "Set I2C SCL frequency (in kHz) default=0");
+MODULE_PARM_DESC(i2c_bus, "Set I2C bus (-1 for dynamic assignment");
+MODULE_PARM_DESC(i2c_mx_bus, "Set I2C MX bus (0-15, default=-1 (FW default))");
+MODULE_PARM_DESC(force_polling, "Force polling mode");
+MODULE_PARM_DESC(i2c_gpio_mux, "Enable I2C port on GPIO out");
drivers/i2c/busses/i2c-kempld.h new file +86 -0
new file mode 100644
index 0000000..2229662
@@ -0,0 +1,86 @@
+/*
+ *  i2c-kempld.h - Kontron PLD I2C driver definitions
+ *
+ *  Copyright (c) 2010-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _KEMPLD_I2C_H_
+#define _KEMPLD_I2C_H_
+
+struct kempld_i2c_data {
+	struct i2c_adapter		adap;
+	struct i2c_adapter		*mxadap[15];
+	struct i2c_msg			*msg;
+	int				pos;
+	int				nmsgs;
+	int				state; /* see STATE_ */
+	int				was_active;
+	int				mx;
+	int				mx_max;
+	int				gpio_mux;
+	wait_queue_head_t		wait;
+	struct tasklet_struct		tasklet;
+	int				irq;
+	struct kempld_device_data	*pld;
+};
+
+/* I2C register definitions */
+#define KEMPLD_I2C_PRELOW	0x0b
+#define KEMPLD_I2C_PREHIGH	0x0c
+#define KEMPLD_I2C_CONTROL	0x0d
+#define KEMPLD_I2C_DATA		0x0e
+#define KEMPLD_I2C_CMD		0x0f /* write only */
+#define		KEMPLD_I2C_CMD_STA	0x80
+#define		KEMPLD_I2C_CMD_STO	0x40
+#define		KEMPLD_I2C_CMD_RD	0x20
+#define		KEMPLD_I2C_CMD_WR	0x10
+#define		KEMPLD_I2C_CMD_NACK	0x08
+#define		KEMPLD_I2C_CMD_IACK	0x01
+#define KEMPLD_I2C_STATUS	0x0f /* read only, same address as
+					KEMPLD_I2C_CMD */
+#define KEMPLD_I2C_MX		0x15
+#define		KEMPLD_I2C_MX_GET_MAX(x)	((x & 0xf0)>>4)
+#define		KEMPLD_I2C_MX_MASK		0x0f
+
+#define STATE_DONE		0
+#define STATE_INIT		1
+#define STATE_ADDR		2
+#define STATE_ADDR10		3
+#define STATE_START		4
+#define STATE_WRITE		5
+#define STATE_READ		6
+#define STATE_ERROR		7
+
+/* defines taken from i2c-ocores */
+#define OCI2C_CTRL_IEN		0x40
+#define OCI2C_CTRL_EN		0x80
+
+#define OCI2C_CMD_START		0x91
+#define OCI2C_CMD_STOP		0x41
+#define OCI2C_CMD_READ		0x21
+#define OCI2C_CMD_WRITE		0x11
+#define OCI2C_CMD_READ_ACK	0x21
+#define OCI2C_CMD_READ_NACK	0x29
+#define OCI2C_CMD_IACK		0x01
+
+#define OCI2C_STAT_IF		0x01
+#define OCI2C_STAT_TIP		0x02
+#define OCI2C_STAT_ARBLOST	0x20
+#define OCI2C_STAT_BUSY		0x40
+#define OCI2C_STAT_NACK		0x80
+
+#endif /* _KEMPLD_I2C_H_ */
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Re: [PATCH 2/4] i2c: Kontron PLD i2c bus driver

Quoted 15 lines · Guenter Roeck
Quoted 7 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add i2c support for the on-board PLD found on some Kontron embedded modules.

Overall well written, though I have a couple of nitpicks.

I would prefer two separate drivers, one for the mux and one for the i2c bus. If that is possible, it would help getting rid of the #ifdef in the code, which is frowned upon in the kernel.

Quoted 8 lines

I dislike unnecessary ( ). Maintainer's call, though.

Couple of places have missing spaces around operators (checkpatch doesn't catch all those).

As far as I know, devm_ functions are supposed to print an error message on failure, so it should be unnecessary to print another one if that happens (this might need some confirmation).

Haven't done a full review due to tglx NACK. I agree to the points mentioned here by Guenter, though.

#4 lore raw

PATCH v2 3/4 gpio: Kontron PLD gpio driver

In reply to Kevin Strasser

From: Michael Brunner <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/gpio/Kconfig            |   22 ++
 drivers/gpio/Makefile           |    2 +
 drivers/gpio/gpio-kempld.c      |  476 +++++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpio-kempld.h      |   50 ++++
 drivers/gpio/gpio-kempld_now1.c |  280 +++++++++++++++++++++++
 5 files changed, 830 insertions(+)
 create mode 100644 drivers/gpio/gpio-kempld.c
 create mode 100644 drivers/gpio/gpio-kempld.h
 create mode 100644 drivers/gpio/gpio-kempld_now1.c
drivers/gpio/Kconfig +22 -0
index 93aaadf..88e1bc9 100644
@@ -658,6 +658,28 @@ config GPIO_UCB1400
 	  This enables support for the Philips UCB1400 GPIO pins.
 	  The UCB1400 is an AC97 audio codec.
 
+comment "LPC GPIO expanders:"
+
+config GPIO_KEMPLD
+	tristate "Kontron COM GPIO"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the PLD GPIO interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called gpio-kempld.
+
+config GPIO_NOW1_KEMPLD
+	tristate "Kontron COMe-mSP1 (nanoETXexpress-SP) GPIO"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the PLD GPIO interface on the Kontron
+	  COMe-mSP1 (nanoETXexpress-SP) module.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called gpio-kempld_now1.
+
 comment "MODULbus GPIO expanders:"
 
 config GPIO_JANZ_TTL
drivers/gpio/Makefile +2 -0
index 22e07bc..59919db 100644
@@ -28,6 +28,8 @@ obj-$(CONFIG_GPIO_GE_FPGA)	+= gpio-ge.o
 obj-$(CONFIG_GPIO_ICH)		+= gpio-ich.o
 obj-$(CONFIG_GPIO_IT8761E)	+= gpio-it8761e.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= gpio-janz-ttl.o
+obj-$(CONFIG_GPIO_KEMPLD)   += gpio-kempld.o
+obj-$(CONFIG_GPIO_NOW1_KEMPLD) += gpio-kempld_now1.o
 obj-$(CONFIG_ARCH_KS8695)	+= gpio-ks8695.o
 obj-$(CONFIG_GPIO_LANGWELL)	+= gpio-langwell.o
 obj-$(CONFIG_ARCH_LPC32XX)	+= gpio-lpc32xx.o
drivers/gpio/gpio-kempld.c new file +476 -0

476 lines not on this page · raw message

drivers/gpio/gpio-kempld.h new file +50 -0

50 lines not on this page · raw message

drivers/gpio/gpio-kempld_now1.c new file +280 -0

280 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#5 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

Quoted 7 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

This looks very generic, setting and clearing bits in bytesized registers.

Can you please attempt to use generic GPIO for this?

drivers/gpio/gpio-generic.c
<linux/basic_mmio_gpio.h>

See for example:
gpio-ep93xx.c, gpio-sodaville.c ...

Since you don't even have IRQ support in this it will be even simpler.

Yours,
Linus Walleij

maintainer
#6 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

Quoted 8 lines · Linus Walleij
Quoted 5 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Quoted 8 lines
Quoted 2 lines

This looks very generic, setting and clearing bits in bytesized registers.

Can you please attempt to use generic GPIO for this?

Linus,

I looked into it, but for my part I seem to be missing how the generic GPIO code permits locking access to the hardware (PLD) and setting the PLD's page register. In other words, I don't immediately see how to call kempld_get_mutex_set_index() from the generic GPIO code. The other drivers using generic GPIO code don't seem to have that requirement.

Thanks,
Guenter

Quoted 15 lines

drivers/gpio/gpio-generic.c
<linux/basic_mmio_gpio.h>

See for example:
gpio-ep93xx.c, gpio-sodaville.c ...

Since you don't even have IRQ support in this it will be even simpler.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

Quoted 17 lines · Guenter Roeck
Quoted 8 lines · Linus Walleij
Quoted 5 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Quoted 8 lines
Quoted 2 lines

This looks very generic, setting and clearing bits in bytesized registers.

Can you please attempt to use generic GPIO for this?

Quoted 7 lines

Linus,

I looked into it, but for my part I seem to be missing how the generic GPIO code permits locking access to the hardware (PLD) and setting the PLD's page register. In other words, I don't immediately see how to call kempld_get_mutex_set_index() from the generic GPIO code. The other drivers using generic GPIO code don't seem to have that requirement.

Ah yes, I was totally wrong here.

I thought it was MMIO while it is indeed through an MFD proxy.

I'll have a second look then...

Yours,
Linus Walleij

#8 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

In reply to Kevin Strasser
Quoted 7 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Trying to do some real review...

(...)

Quoted 2 lines

+++ b/drivers/gpio/gpio-kempld.c
+#include <linux/acpi.h>

Is this used?

drivers/gpio/gpio-kempld.c line 39

Quoted 12 lines
drivers/gpio/gpio-kempld.c new file +12 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/mfd/kempld.h>
+#include <linux/seq_file.h>
+
+#include "gpio-kempld.h"
+
+static int gpiobase = -1;
+static int gpioien = 0x00;
+static int gpioevt_lvl_edge = -1;
+static int gpioevt_low_high = -1;
+static int gpionmien = 0x00;

(...)

+static int kempld_gpio_to_irq(struct gpio_chip *chip, unsigned offset) +{
+ struct kempld_gpio_data *gpio
+ = container_of(chip, struct kempld_gpio_data, chip);
+ return gpio->irq;
+}

I don't understand this *at all* so help me out here.

.gpio_to_irq() should return a *Linux* IRQ number, usually we take the event offset (in this case) and map to a Linux IRQ using the irqdomain helper library. Can you explain how we can be sure that this number (apparently just a read from a register on the device) can be made to correspond to a Linux IRQ?

Also if this thing can generate IRQs, are these one line to the CPU per IRQ really? Don't you need to demux the status register and create a cascades irqchip?

Maybe it's just me not understanding x86 & ACPI so bear with me...

Quoted 6 lines
drivers/gpio/gpio-kempld.c new file +6 -0
+static int kempld_gpio_setup_event(struct kempld_gpio_data *gpio)
+{
+       struct kempld_device_data *pld = gpio->pld;
+       struct gpio_chip *chip = &gpio->chip;
+       int irq;
+

drivers/gpio/gpio-kempld.c line 275

Quoted 10 lines
drivers/gpio/gpio-kempld.c new file +10 -0
+       irq = gpio->irq;
+
+       kempld_get_mutex_set_index(pld, KEMPLD_IRQ_GPIO);
+       irq = kempld_read8(pld, KEMPLD_IRQ_GPIO);
+
+       /* Leave if interrupts are not supported by the GPIO core */
+       if ((irq & 0xf0) == 0xf0)
+               return 0;
+
+       gpio->irq = irq & 0x0f;

So you read the IRQ from some plug-n-play here, and it's some system-wide IRQ number?

(...)

drivers/gpio/gpio-kempld.c line 401

Quoted 2 lines
drivers/gpio/gpio-kempld.c new file +2 -0
+       if (gpio->irq)
+               chip->to_irq =          kempld_gpio_to_irq;

So that is this mystery with the IRQs and how they turn into Linux IRQs.

Quoted 1 line

+module_param(gpiobase, int, 0444);

Why do you need to be able to configure this?
It must be a real usecase, debugging can be done by patching the code.

drivers/gpio/gpio-kempld.c line 463

Quoted 4 lines
drivers/gpio/gpio-kempld.c new file +4 -0
+module_param(gpioien, int, 0444);
+module_param(gpioevt_lvl_edge, int, 0444);
+module_param(gpioevt_low_high, int, 0444);
+module_param(gpionmien, int, 0444);

Argh how can anyone possibly make this out ... do you really need them or can we get rid of some and rely on autodetect?

drivers/gpio/gpio-kempld.c line 475

Quoted 11 lines
drivers/gpio/gpio-kempld.c new file +11 -0
+MODULE_DESCRIPTION("KEM PLD GPIO Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:kempld_gpio");
+MODULE_PARM_DESC(gpiobase, "Set GPIO base (default -1=dynamic)");
+MODULE_PARM_DESC(gpioien, "Set GPIO IEN register (default 0x00)");
+MODULE_PARM_DESC(gpioevt_lvl_edge,
+                       "Set GPIO EVT_LVL_EDGE register (default -1=no change)");
+MODULE_PARM_DESC(gpioevt_low_high,
+                       "Set GPIO EVT_LOW_HIGH register (default -1=no change)");
+MODULE_PARM_DESC(gpionmien, "Set GPIO NMIEN register (default 0x00)");

So I don't really like that interrupt enablement and edge and low/high is done with module parameters instead of just creating an irqchip and have it implement the operations to do exactly these things at runtime instead.

Again maybe some x86 thing I don't get...

Quoted 1 line
drivers/gpio/gpio-kempld.h +0 -0

(...)

drivers/gpio/gpio-kempld.h line 47

Quoted 5 lines
drivers/gpio/gpio-kempld.h +5 -0
+struct kempld_gpio_data {
+       struct gpio_chip                chip;
+       int                             irq;
+       struct kempld_device_data       *pld;
+       uint16_t                        mask;

Just u16?

Quoted 1 line

+};

(...)

drivers/gpio/gpio-kempld_now1.c

Quoted 2 lines
drivers/gpio/gpio-kempld_now1.c +1 -0
+#include <linux/io.h>

Do you use this?

drivers/gpio/gpio-kempld_now1.c

Quoted 3 lines
drivers/gpio/gpio-kempld_now1.c +3 -0
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>

And this?

Quoted 6 lines
drivers/gpio/gpio-kempld_now1.c +6 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/mfd/kempld.h>
+#include <linux/seq_file.h>
+
+#include "gpio-kempld.h"

(...)

Quoted 1 line

+

Most comments concern the other driver too.

Yours,
Linus Walleij

#9 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

Hi Linus,

As this code is from me I will comment on your review.

On Wed, 10 Apr 2013 22:45:51 +0200
Linus Walleij <[email protected]> wrote:
(...)

Quoted 7 lines

Trying to do some real review...

(...)

Quoted 2 lines

+++ b/drivers/gpio/gpio-kempld.c
+#include <linux/acpi.h>

Is this used?

Actually not, this can be removed.

Quoted 30 lines

drivers/gpio/gpio-kempld.c line 39

Quoted 12 lines
drivers/gpio/gpio-kempld.c new file +12 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/mfd/kempld.h>
+#include <linux/seq_file.h>
+
+#include "gpio-kempld.h"
+
+static int gpiobase = -1;
+static int gpioien = 0x00;
+static int gpioevt_lvl_edge = -1;
+static int gpioevt_low_high = -1;
+static int gpionmien = 0x00;  

(...)

+static int kempld_gpio_to_irq(struct gpio_chip *chip, unsigned offset) +{

drivers/gpio/gpio-kempld.c new file +4 -0
+       struct kempld_gpio_data *gpio
+               = container_of(chip, struct kempld_gpio_data, chip);
+       return gpio->irq;
+}

I don't understand this *at all* so help me out here.

.gpio_to_irq() should return a *Linux* IRQ number, usually we take the event offset (in this case) and map to a Linux IRQ using the irqdomain helper library. Can you explain how we can be sure that this number (apparently just a read from a register on the device) can be made to correspond to a Linux IRQ?

Quoted 5 lines

Also if this thing can generate IRQs, are these one line to the CPU per IRQ really? Don't you need to demux the status register and create a cascades irqchip?

Maybe it's just me not understanding x86 & ACPI so bear with me...

The chip is connected to the CPU through a serial IRQ line and IRQs are managed through the (A)PIC which is configured by the firmware. I never saw a difference between Linux and HW IRQ numbers for the legacy IRQs (0-15) this chip generates. But I will take another look at the IRQ handling of this driver.

Quoted 11 lines
Quoted 11 lines
drivers/gpio/gpio-kempld.c new file +11 -0
+static int kempld_gpio_setup_event(struct kempld_gpio_data *gpio)
+{
+       struct kempld_device_data *pld = gpio->pld;
+       struct gpio_chip *chip = &gpio->chip;
+       int irq;
+
+       irq = gpio->irq;
+
+       kempld_get_mutex_set_index(pld, KEMPLD_IRQ_GPIO);
+       irq = kempld_read8(pld, KEMPLD_IRQ_GPIO);
+
Quoted 8 lines

drivers/gpio/gpio-kempld.c line 275

Quoted 5 lines
drivers/gpio/gpio-kempld.c new file +5 -0
+       /* Leave if interrupts are not supported by the GPIO core */
+       if ((irq & 0xf0) == 0xf0)
+               return 0;
+
+       gpio->irq = irq & 0x0f;  

So you read the IRQ from some plug-n-play here, and it's some system-wide IRQ number?

Correct.

Quoted 4 lines

(...)

drivers/gpio/gpio-kempld.c line 401

Quoted 2 lines
drivers/gpio/gpio-kempld.c new file +2 -0
+       if (gpio->irq)
+               chip->to_irq =          kempld_gpio_to_irq;  
Quoted 8 lines

So that is this mystery with the IRQs and how they turn into Linux IRQs.

Quoted 1 line

+module_param(gpiobase, int, 0444);

Why do you need to be able to configure this?
It must be a real usecase, debugging can be done by patching the code.

This was intended to help developing userspace applications or scripts.
For this parameter I had in mind that one configures a static
GPIO base and then maps the GPIOs with the help of the sysfs interface
without the need to first find out which is the actual GPIO base. If you think this shouldn't be done this way I won't insist to keep this parameter.

Quoted 7 lines

drivers/gpio/gpio-kempld.c line 463

Quoted 4 lines
drivers/gpio/gpio-kempld.c new file +4 -0
+module_param(gpioien, int, 0444);
+module_param(gpioevt_lvl_edge, int, 0444);
+module_param(gpioevt_low_high, int, 0444);
+module_param(gpionmien, int, 0444);  

Argh how can anyone possibly make this out ... do you really need them or can we get rid of some and rely on autodetect?

As the chip sits on a computer module that is usually only configured generically, it is not possible to auto detect the needed configuration. Those parameters are intended to let the developer configure the chip without having to touch the driver code.
You are right anyway, doing it this way might not be the best way. So if there is a good way to configure this stuff at runtime by using a generic interface I would also prefer this.

Quoted 19 lines
Quoted 11 lines
drivers/gpio/gpio-kempld.c new file +9 -2
+MODULE_DESCRIPTION("KEM PLD GPIO Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:kempld_gpio");
+MODULE_PARM_DESC(gpiobase, "Set GPIO base (default -1=dynamic)");
+MODULE_PARM_DESC(gpioien, "Set GPIO IEN register (default 0x00)");
+MODULE_PARM_DESC(gpioevt_lvl_edge,
+                       "Set GPIO EVT_LVL_EDGE register (default
-1=no change)"); +MODULE_PARM_DESC(gpioevt_low_high,
+                       "Set GPIO EVT_LOW_HIGH register (default
-1=no change)"); +MODULE_PARM_DESC(gpionmien, "Set GPIO NMIEN
Quoted 1 line

register (default 0x00)");

So I don't really like that interrupt enablement and edge and low/high is done with module parameters instead of just creating an irqchip and have it implement the operations to do exactly these things at runtime instead.

Quoted 1 line

Again maybe some x86 thing I don't get...

Possibly not. I am not very familiar with irqchip so far, therefore I will have a look at it and check if the whole IRQ handling can be ported to this framework.

Quoted 10 lines
Quoted 2 lines
drivers/gpio/gpio-kempld.h +0 -0

(...)

drivers/gpio/gpio-kempld.h line 47

Quoted 5 lines
drivers/gpio/gpio-kempld.h +5 -0
+struct kempld_gpio_data {
+       struct gpio_chip                chip;
+       int                             irq;
+       struct kempld_device_data       *pld;
+       uint16_t                        mask;  

Just u16?

The specification allows 16 GPIOs for this device, therefore this seems to be the right size. Would it be better to use another type instead?

Quoted 7 lines
Quoted 1 line

+};

(...)

Quoted 2 lines
(quoted diff) +0 -0

b/drivers/gpio/gpio-kempld_now1.c +#include <linux/io.h>

Do you use this?

This can be removed.

Quoted 5 lines

drivers/gpio/gpio-kempld.h

Quoted 3 lines
drivers/gpio/gpio-kempld.h +3 -0
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/acpi.h>  

And this?

linux/slab.h is necessary for kzalloc, but the rest can be removed.

Quoted 7 lines
Quoted 6 lines
drivers/gpio/gpio-kempld.h +6 -0
+#include <linux/platform_device.h>
+#include <linux/gpio.h>
+#include <linux/mfd/kempld.h>
+#include <linux/seq_file.h>
+
+#include "gpio-kempld.h"  

(...)

Quoted 6 lines
Quoted 1 line

+

Most comments concern the other driver too.

Yours,
Linus Walleij

Thank you for the review!

Best regards,
Michael

#10 lore raw

Re: [PATCH 3/4] gpio: Kontron PLD gpio driver

Quoted 11 lines · Michael Brunner
Quoted 8 lines

(...)

drivers/gpio/gpio-kempld.h line 47

Quoted 5 lines
drivers/gpio/gpio-kempld.h new file +5 -0
+struct kempld_gpio_data {
+       struct gpio_chip                chip;
+       int                             irq;
+       struct kempld_device_data       *pld;
+       uint16_t                        mask;

Just u16?

The specification allows 16 GPIOs for this device, therefore this seems to be the right size. Would it be better to use another type instead?

Ah, I was just asking you to use "u16" instead of "uint16_t".

Yours,
Linus Walleij

#11 lore raw

PATCH v2 4/4 watchdog: Kontron PLD watchdog timer

In reply to Kevin Strasser

From: Michael Brunner <[email protected]>

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/watchdog/Kconfig           |   20 +
 drivers/watchdog/Makefile          |    2 +
 drivers/watchdog/kempld_now1_wdt.c |  602 +++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.c      |  796 ++++++++++++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.h      |   75 ++++
 5 files changed, 1495 insertions(+)
 create mode 100644 drivers/watchdog/kempld_now1_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.h
drivers/watchdog/Kconfig +20 -0
index 9fcc70c..9ac71ca 100644
@@ -687,6 +687,26 @@ config HP_WATCHDOG
 	  To compile this driver as a module, choose M here: the module will be
 	  called hpwdt.
 
+config KEMPLD_WDT
+	tristate "Kontron COM watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on some Kontron ETX and COMexpress
+	  (ETXexpress) modules
+
+	  This driver can also be built as a module. If so, the module will be
+	  called kempld_wdt.
+
+config KEMPLD_NOW1_WDT
+	tristate "Kontron COMe-mSP1 (nanoETXexpress-SP) watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on the Kontron COMe-mSP1
+	  (nanoETXexpress-SP) module.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called kempld_now1_wdt.
+
 config HPWDT_NMI_DECODING
 	bool "NMI decoding support for the HP ProLiant iLO2+ Hardware Watchdog Timer"
 	depends on HP_WATCHDOG
drivers/watchdog/Makefile +2 -0
index a300b94..a029930 100644
@@ -90,6 +90,8 @@ endif
 obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
+obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
+obj-$(COnFIG_KEMPLD_NOW1_WDT) += kempld_now1_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/kempld_now1_wdt.c new file +602 -0

602 lines not on this page · raw message

drivers/watchdog/kempld_wdt.c new file +796 -0

796 lines not on this page · raw message

drivers/watchdog/kempld_wdt.h new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#12 lore raw

Re: [PATCH 4/4] watchdog: Kontron PLD watchdog timer

Quoted 7 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

Personally I would prefer two separate patches for the two drivers, and to have the drivers converted to the watchdog infrastructure. Wim's call, of course.

Thanks,
Guenter

Quoted 1,554 lines
---
 drivers/watchdog/Kconfig           |   20 +
 drivers/watchdog/Makefile          |    2 +
 drivers/watchdog/kempld_now1_wdt.c |  602 +++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.c      |  796 ++++++++++++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.h      |   75 ++++
 5 files changed, 1495 insertions(+)
 create mode 100644 drivers/watchdog/kempld_now1_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.h
drivers/watchdog/Kconfig +20 -0
index 9fcc70c..9ac71ca 100644
@@ -687,6 +687,26 @@ config HP_WATCHDOG
 	  To compile this driver as a module, choose M here: the module will be
 	  called hpwdt.
 
+config KEMPLD_WDT
+	tristate "Kontron COM watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on some Kontron ETX and COMexpress
+	  (ETXexpress) modules
+
+	  This driver can also be built as a module. If so, the module will be
+	  called kempld_wdt.
+
+config KEMPLD_NOW1_WDT
+	tristate "Kontron COMe-mSP1 (nanoETXexpress-SP) watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on the Kontron COMe-mSP1
+	  (nanoETXexpress-SP) module.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called kempld_now1_wdt.
+
 config HPWDT_NMI_DECODING
 	bool "NMI decoding support for the HP ProLiant iLO2+ Hardware Watchdog Timer"
 	depends on HP_WATCHDOG
drivers/watchdog/Makefile +2 -0
index a300b94..a029930 100644
@@ -90,6 +90,8 @@ endif
 obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
+obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
+obj-$(COnFIG_KEMPLD_NOW1_WDT) += kempld_now1_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/kempld_now1_wdt.c new file +602 -0
new file mode 100644
index 0000000..19b7272
@@ -0,0 +1,602 @@
+/*
+ *  kempld_now1_wdt.c - Kontron PLD watchdog driver for COMe-mSP1
+ *
+ *  Copyright (c) 2011-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  Note: The watchdog of the COMe-mSP1 (nanoETXexpress-SP) has one stage and
+ *        only supports predefined watchdog timeout values.
+ *
+ *        The supported timeouts are:
+ *              1 s, 5 s, 10 s, 30 s, 1 min, 5 min, 10 min, 15 min
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/miscdevice.h>
+#include <linux/watchdog.h>
+#include <linux/fs.h>
+#include <linux/ioport.h>
+#include <linux/init.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/kempld.h>
+
+#include "kempld_wdt.h"
+
+#define WATCHDOG_TIMEOUT 30
+static int timeout = WATCHDOG_TIMEOUT;
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. (1, 5, 10, 30, 60, 300, 600, "
+		 "900, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout,
+		 "Watchdog cannot be stopped once started (default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+/* nanoETXexpress-SP watchdog register definitions */
+#define KEMPLD_WDT_NOW1					0xA2
+#define		KEMPLD_WDT_NOW1_KICK_MASK		0x80
+#define		KEMPLD_WDT_NOW1_ENABLE_MASK		0x40
+#define		KEMPLD_WDT_NOW1_TIMEOUT_MASK		0x1f
+#define			KEMPLD_WDT_NOW1_TIMEOUT_1SEC	0x00
+#define			KEMPLD_WDT_NOW1_TIMEOUT_5SEC	0x01
+#define			KEMPLD_WDT_NOW1_TIMEOUT_10SEC	0x02
+#define			KEMPLD_WDT_NOW1_TIMEOUT_30SEC	0x03
+#define			KEMPLD_WDT_NOW1_TIMEOUT_1MIN	0x10
+#define			KEMPLD_WDT_NOW1_TIMEOUT_5MIN	0x11
+#define			KEMPLD_WDT_NOW1_TIMEOUT_10MIN	0x12
+#define			KEMPLD_WDT_NOW1_TIMEOUT_15MIN	0x13
+
+/* delay in us necessary due to clock domain sync */
+#define		KEMPLD_WDT_NOW1_SYNC_DELAY		31
+
+static struct kempld_watchdog_data *kempld_now1_wdt;
+
+static int kempld_now1_wdt_read_supported;
+static int kempld_now1_wdt_reg_cache;
+
+static int kempld_now1_wdt_start(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	if (kempld_now1_wdt_read_supported)
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	wdt_reg |= KEMPLD_WDT_NOW1_ENABLE_MASK;
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	if (kempld_now1_wdt_read_supported) {
+		/* read out the register again to check if everything worked */
+		kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_release_mutex(pld);
+
+		/* check if the watchdog was disabled */
+		if (!(wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK))
+			return -EACCES;
+	}
+
+	return 0;
+}
+
+static int kempld_now1_wdt_stop(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	wdt_reg &= ~KEMPLD_WDT_NOW1_ENABLE_MASK;
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	if (kempld_now1_wdt_read_supported) {
+		/* read out the register again to check if everything worked */
+		kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_release_mutex(pld);
+
+		/* check if the watchdog was disabled */
+		if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK)
+			return -EACCES;
+	}
+
+	return 0;
+}
+
+static int kempld_now1_wdt_keepalive(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+	if (kempld_now1_wdt_read_supported) {
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = kempld_now1_wdt_reg_cache;
+		/* write the state again to be sure the trigger register has
+		 * the right level */
+		kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	}
+
+	if (wdt_reg & KEMPLD_WDT_NOW1_KICK_MASK)
+		wdt_reg &= ~KEMPLD_WDT_NOW1_KICK_MASK;
+	else
+		wdt_reg |= KEMPLD_WDT_NOW1_KICK_MASK;
+
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+
+	kempld_now1_wdt_reg_cache = wdt_reg;
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+
+static int kempld_now1_wdt_settimeout(struct kempld_watchdog_data *wdt,
+					int check_only)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+	int ret = 0;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+
+	wdt_reg &= ~KEMPLD_WDT_NOW1_TIMEOUT_MASK;
+
+	switch (wdt->timeout) {
+	case 1:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_1SEC;
+		break;
+	case 5:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_5SEC;
+		break;
+	case 10:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_10SEC;
+		break;
+	case 30:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_30SEC;
+		break;
+	case 60:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_1MIN;
+		break;
+	case 300:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_5MIN;
+		break;
+	case 600:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_10MIN;
+		break;
+	case 900:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_15MIN;
+		break;
+	default:
+		ret = -EINVAL;
+		dev_err(wdt->pld->dev,
+			"Invalid timeout value given!\n");
+	}
+
+	if (!check_only) {
+		if (ret == 0) {
+			udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+			kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+		}
+	}
+
+	kempld_now1_wdt_reg_cache = wdt_reg;
+
+	kempld_release_mutex(pld);
+
+	return ret;
+}
+
+static int kempld_now1_wdt_gettimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+	int timeout;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_now1_wdt_reg_cache = wdt_reg;
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+
+	kempld_release_mutex(pld);
+
+	switch (wdt_reg&KEMPLD_WDT_NOW1_TIMEOUT_MASK) {
+	case KEMPLD_WDT_NOW1_TIMEOUT_1SEC:
+		timeout = 1;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_5SEC:
+		timeout = 5;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_10SEC:
+		timeout = 10;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_30SEC:
+		timeout = 30;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_1MIN:
+		timeout = 60;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_5MIN:
+		timeout = 300;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_10MIN:
+		timeout = 600;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_15MIN:
+		timeout = 900;
+		break;
+	default:
+		timeout = -ERANGE;
+	}
+
+	return timeout;
+}
+
+static ssize_t kempld_now1_wdt_write(struct file *file, const char __user
+					*data, size_t count, loff_t *ppos)
+{
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (count) {
+		kempld_now1_wdt_keepalive(wdt);
+
+		if (!nowayout) {
+			size_t i;
+
+			wdt->expect_close = 0;
+
+			for (i = 0; i < count; i++) {
+				char c;
+				if (get_user(c, data+i))
+					return -EFAULT;
+				if (c == 'V')
+					wdt->expect_close = 42;
+			}
+		}
+	}
+
+	return count;
+}
+
+static long kempld_now1_wdt_ioctl(struct file *file, unsigned int cmd,
+				unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	int __user *p = argp;
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+	int options;
+	int value;
+	int ret = 0;
+
+	BUG_ON(wdt == NULL);
+
+	switch (cmd) {
+	case WDIOC_GETSUPPORT:
+		if (copy_to_user(argp, &wdt->ident, sizeof(wdt->ident)))
+			ret = -EFAULT;
+		break;
+	case WDIOC_GETSTATUS:
+	case WDIOC_GETBOOTSTATUS:
+		ret = put_user(0, p);
+		break;
+	case WDIOC_SETOPTIONS:
+		if (get_user(options, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		if (options & WDIOS_DISABLECARD)
+			ret = kempld_now1_wdt_stop(wdt);
+		if (options & WDIOS_ENABLECARD) {
+			ret = kempld_now1_wdt_start(wdt);
+			kempld_now1_wdt_keepalive(wdt);
+		}
+		break;
+	case WDIOC_KEEPALIVE:
+		kempld_now1_wdt_keepalive(wdt);
+		break;
+	case WDIOC_SETTIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		wdt->timeout = value;
+		ret = kempld_now1_wdt_settimeout(wdt, 0);
+		kempld_now1_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETTIMEOUT:
+		value = kempld_now1_wdt_gettimeout(wdt, wdt->timeout_stage);
+		if (timeout < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(timeout, p);
+		break;
+	default:
+		ret = -ENOTTY;
+	}
+
+	return ret;
+}
+
+static int kempld_now1_wdt_release(struct inode *inode, struct file *file)
+{
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (wdt->expect_close)
+		kempld_now1_wdt_stop(wdt);
+	else {
+		dev_warn(wdt->pld->dev,
+			 "Unexpected close, not stopping watchdog!\n");
+		kempld_now1_wdt_keepalive(wdt);
+	}
+
+	kempld_now1_wdt->expect_close = 0;
+
+	clear_bit(0, &wdt->is_open);
+
+	return 0;
+}
+
+static int kempld_now1_wdt_open(struct inode *inode, struct file *file)
+{
+	int ret;
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+	struct kempld_device_data *pld = wdt->pld;
+	u8 wdt_reg;
+
+	BUG_ON(wdt == NULL);
+
+	if (test_and_set_bit(0, &wdt->is_open))
+		return -EBUSY;
+
+	if (nowayout)
+		__module_get(THIS_MODULE);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	}
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	/* kick the watchdog if it is already enabled, otherwise start it */
+	if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK) {
+		kempld_now1_wdt_keepalive(wdt);
+	} else {
+		ret = kempld_now1_wdt_settimeout(wdt, 0);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_now1_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+	}
+
+	return nonseekable_open(inode, file);
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev, "Failed to enable the watchdog timer!\n");
+	wdt->expect_close = 1;
+	kempld_now1_wdt_release(inode, file);
+
+	return ret;
+}
+
+static const struct file_operations kempld_now1_wdt_fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.write		= kempld_now1_wdt_write,
+	.unlocked_ioctl	= kempld_now1_wdt_ioctl,
+	.open		= kempld_now1_wdt_open,
+	.release	= kempld_now1_wdt_release,
+};
+
+static struct miscdevice kempld_now1_wdt_miscdev = {
+	.minor	= WATCHDOG_MINOR,
+	.name	= "watchdog",
+	.fops	= &kempld_now1_wdt_fops,
+};
+
+static int kempld_now1_wdt_probe(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt;
+	struct kempld_device_data *pld;
+	u8 wdt_reg;
+	int ret;
+
+	if (kempld_now1_wdt != NULL) {
+		dev_err(&pdev->dev,
+			"unable to support more than one watchdog devices\n");
+		return -EMFILE;
+	}
+
+	wdt = kzalloc(sizeof(struct kempld_watchdog_data), GFP_KERNEL);
+	if (wdt == NULL) {
+		dev_err(&pdev->dev, "unable to get memory for device data\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	wdt->timeout_stage = kzalloc(sizeof(struct kempld_watchdog_stage),
+					GFP_KERNEL);
+	if (wdt->timeout_stage == NULL) {
+		dev_err(&pdev->dev,
+			"unable to get memory for watchdog stage\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	wdt->stages = 1;
+	wdt->stage[0] = wdt->timeout_stage;
+
+	pld = dev_get_drvdata(pdev->dev.parent);
+	wdt->pld = pld;
+
+	platform_set_drvdata(pdev, wdt);
+
+	strncpy(wdt->ident.identity, "nanoETXexpress-SP Watchdog",
+		sizeof(wdt->ident.identity));
+
+	/* set default values for the case we start the watchdog or change
+	 * the configuration */
+	wdt->timeout = timeout;
+
+	/* use settimeout to check if the timeout parameter is valid */
+	ret = kempld_now1_wdt_settimeout(wdt, 1);
+	if (ret)
+		goto err_check_timeout;
+	wdt->ident.firmware_version = (pld->info.major<<8) + pld->info.minor;
+	if (pld->info.major > 67)
+		kempld_now1_wdt_read_supported = 1;
+	else
+		dev_info(wdt->pld->dev,
+			 "Watchdog revision does not support read - "
+			 "unable to get watchdog state!\n");
+
+	/* get initial watchdog status */
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = 0x0;
+	}
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(wdt->pld);
+
+	/* check if watchdog is enabled */
+	if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK)
+		dev_info(wdt->pld->dev, "Watchdog is already enabled!\n");
+
+	wdt->ident.options = WDIOF_KEEPALIVEPING;
+	wdt->ident.options |= WDIOF_SETTIMEOUT;
+	if (!nowayout)
+		wdt->ident.options |= WDIOF_MAGICCLOSE;
+
+	kempld_now1_wdt = wdt;
+
+	ret = misc_register(&kempld_now1_wdt_miscdev);
+	if (ret)
+		goto err_misc_register;
+
+	dev_info(wdt->pld->dev, "watchdog initialized\n");
+
+	return 0;
+
+err_misc_register:
+	kfree(kempld_now1_wdt);
+	kempld_now1_wdt = NULL;
+err_check_timeout:
+err_alloc_dev_data:
+	return ret;
+}
+
+static int kempld_now1_wdt_remove(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_now1_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	if (wdt != NULL) {
+		if (!nowayout)
+			kempld_now1_wdt_stop(wdt);
+		else
+			kempld_now1_wdt_keepalive(wdt);
+	}
+
+	misc_deregister(&kempld_now1_wdt_miscdev);
+
+	kfree(wdt);
+	kempld_now1_wdt = NULL;
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+static struct platform_driver kempld_now1_wdt_driver = {
+	.driver = {
+		.name = "kempld_now1-wdt",
+		.owner = THIS_MODULE,
+	},
+	.probe = kempld_now1_wdt_probe,
+	.remove = kempld_now1_wdt_remove,
+};
+
+static int __init kempld_now1_wdt_init(void)
+{
+	return platform_driver_register(&kempld_now1_wdt_driver);
+}
+
+static void __exit kempld_now1_wdt_exit(void)
+{
+	platform_driver_unregister(&kempld_now1_wdt_driver);
+}
+
+module_init(kempld_now1_wdt_init);
+module_exit(kempld_now1_wdt_exit);
+
+MODULE_DESCRIPTION("KEM PLD nanoETXexpress-SP Watchdog Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
drivers/watchdog/kempld_wdt.c new file +796 -0
new file mode 100644
index 0000000..bc150e5
@@ -0,0 +1,796 @@
+/*
+ *  kempld_wdt.c - Kontron PLD watchdog driver
+ *
+ *  Copyright (c) 2010-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  Note: From the PLD watchdog point of view timeout and pretimeout are
+ *        defined differently than in the kernel.
+ *        First the pretimeout stage runs out before the timeout stage gets
+ *        active. This has to be kept in mind.
+ *
+ *  Kernel/API:                     P-----| pretimeout
+ *                |-----------------------T timeout
+ *  Watchdog:     |-----------------P       pretimeout_stage
+ *                                  |-----T timeout_stage
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/miscdevice.h>
+#include <linux/watchdog.h>
+#include <linux/fs.h>
+#include <linux/ioport.h>
+#include <linux/init.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/kempld.h>
+
+#include "kempld_wdt.h"
+
+#define WATCHDOG_DEFAULT_TIMEOUT 20
+#define WATCHDOG_DEFAULT_PRETIMEOUT 0
+static int timeout = -1;
+static int pretimeout = -1;
+/* The maximum timeout values have to be probed */
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. (>0, default="
+		__MODULE_STRING(WATCHDOG_DEFAULT_TIMEOUT) ")");
+module_param(pretimeout, int, 0);
+MODULE_PARM_DESC(pretimeout,
+		 "Watchdog pretimeout in seconds. (>=0, default="
+		__MODULE_STRING(WATCHDOG_DEFAULT_PRETIMEOUT) ")");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout,
+		 "Watchdog cannot be stopped once started (default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static struct kempld_watchdog_data *kempld_wdt;
+
+static int kempld_wdt_start(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	status |= KEMPLD_WDT_CFG_ENABLE;
+	kempld_write8(pld, KEMPLD_WDT_CFG, status);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+
+	kempld_release_mutex(pld);
+
+	/* check if the watchdog was enabled */
+	if (!(status & KEMPLD_WDT_CFG_ENABLE))
+		return -EACCES;
+
+	return 0;
+}
+
+static int kempld_wdt_stop(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	status &= ~KEMPLD_WDT_CFG_ENABLE;
+	kempld_write8(pld, KEMPLD_WDT_CFG, status);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+
+	kempld_release_mutex(pld);
+
+	/* check if the watchdog was disabled */
+	if (status & KEMPLD_WDT_CFG_ENABLE)
+		return -EACCES;
+
+	return 0;
+}
+
+static int kempld_wdt_keepalive(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	kempld_write8(pld, KEMPLD_WDT_KICK, 'K');
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static int kempld_wdt_gettimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+	int bits;
+	u64 timeout;
+	u32 remainder;
+
+	if (stage == NULL)
+		return 0;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	timeout = kempld_read32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->num));
+
+	kempld_release_mutex(pld);
+
+	bits = KEMPLD_WDT_STAGE_CFG_GET_PRESCALER(stage_cfg);
+	timeout = (timeout & stage->timeout_mask) * KEMPLD_PRESCALER(bits);
+	remainder = do_div(timeout, pld->pld_clock);
+
+	/* Round up the return value if necessary */
+	if ((timeout > 0) && (remainder >= (pld->pld_clock/2)))
+		timeout++;
+
+	return timeout;
+}
+
+static int kempld_wdt_setstageaction(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage,
+				 int action)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+
+	if (stage == NULL)
+		return -EINVAL;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_ACTION_MASK;
+	stage_cfg |= (action & KEMPLD_WDT_STAGE_CFG_ACTION_MASK);
+	if (action == KEMPLD_WDT_ACTION_RESET)
+		stage_cfg |= KEMPLD_WDT_STAGE_CFG_ASSERT;
+	else
+		stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_ASSERT;
+
+	kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->num), stage_cfg);
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static int kempld_wdt_setstagetimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage,
+				 int timeout)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+	u8 prescaler;
+	u64 stage_timeout64;
+	u32 stage_timeout;
+	u32 remainder;
+
+	if (stage == NULL)
+		return -EINVAL;
+
+	prescaler = KEMPLD_WDT_PRESCALER_21BIT;
+
+	stage_timeout64 = ((u64)timeout*pld->pld_clock);
+	remainder = do_div(stage_timeout64, KEMPLD_PRESCALER(prescaler));
+	if (remainder)
+		stage_timeout64++;
+	stage_timeout = stage_timeout64 & stage->timeout_mask;
+
+	if (stage_timeout64 != (u64)stage_timeout)
+		return -EINVAL;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_PRESCALER_MASK;
+	stage_cfg |= KEMPLD_WDT_STAGE_CFG_SET_PRESCALER(prescaler);
+	kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->num), stage_cfg);
+	kempld_write32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->num),
+			stage_timeout);
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static void kempld_wdt_update_timeouts(struct kempld_watchdog_data *wdt)
+{
+	int pretimeout_stage;
+	int timeout_stage;
+
+	pretimeout_stage = kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+	timeout_stage = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+
+	if (pretimeout_stage)
+		wdt->pretimeout = timeout_stage;
+	else
+		wdt->pretimeout = 0;
+
+	wdt->timeout = pretimeout_stage + timeout_stage;
+
+	if (wdt->pretimeout < 0) {
+		wdt->pretimeout = WATCHDOG_DEFAULT_PRETIMEOUT;
+		dev_err(wdt->pld->dev, "failed to get valid pretimeout value\n"
+			" -> using driver default\n");
+	}
+	if (wdt->timeout < 0) {
+		wdt->timeout = WATCHDOG_DEFAULT_TIMEOUT;
+		dev_err(wdt->pld->dev, "failed to get valid timeout value\n"
+			" -> using driver default\n");
+	}
+}
+
+static int kempld_wdt_settimeout(struct kempld_watchdog_data *wdt)
+{
+	int stage_timeout;
+	int stage_pretimeout;
+	int ret;
+
+	if ((wdt->timeout <= 0) ||
+		(wdt->pretimeout < 0) ||
+		(wdt->pretimeout > wdt->timeout)) {
+		ret = -EINVAL;
+		goto err_check_values;
+	}
+
+	if ((wdt->pretimeout == 0) || (wdt->pretimeout_stage == NULL)) {
+		if (wdt->pretimeout != 0)
+			dev_warn(wdt->pld->dev,
+				 "no pretimeout stage available\n"
+				 " -> only enabling reset\n");
+		stage_pretimeout = 0;
+		stage_timeout = wdt->timeout;
+	} else {
+		stage_pretimeout = wdt->timeout - wdt->pretimeout;
+		stage_timeout = wdt->pretimeout;
+	}
+
+	if (stage_pretimeout != 0) {
+		ret = kempld_wdt_setstageaction(wdt, wdt->pretimeout_stage,
+						KEMPLD_WDT_ACTION_NMI);
+	} else if ((stage_pretimeout == 0)
+			&& (wdt->pretimeout_stage != NULL)) {
+		ret = kempld_wdt_setstageaction(wdt, wdt->pretimeout_stage,
+						KEMPLD_WDT_ACTION_NONE);
+	} else
+		ret = 0;
+	if (ret)
+		goto err_setstage;
+
+	if (stage_pretimeout != 0) {
+		ret = kempld_wdt_setstagetimeout(wdt, wdt->pretimeout_stage,
+						 stage_pretimeout);
+		if (ret)
+			goto err_setstage;
+	}
+
+	ret = kempld_wdt_setstageaction(wdt, wdt->timeout_stage,
+					KEMPLD_WDT_ACTION_RESET);
+	if (ret)
+		goto err_setstage;
+
+	ret = kempld_wdt_setstagetimeout(wdt, wdt->timeout_stage,
+					 stage_timeout);
+	if (ret)
+		goto err_setstage;
+
+	return 0;
+err_setstage:
+err_check_values:
+	return ret;
+}
+
+static ssize_t kempld_wdt_write(struct file *file, const char __user *data,
+				size_t count, loff_t *ppos)
+{
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (count) {
+		kempld_wdt_keepalive(wdt);
+
+		if (!nowayout) {
+			size_t i;
+
+			wdt->expect_close = 0;
+
+			for (i = 0; i < count; i++) {
+				char c;
+				if (get_user(c, data+i))
+					return -EFAULT;
+				if (c == 'V')
+					wdt->expect_close = 42;
+			}
+		}
+	}
+
+	return count;
+}
+
+static long kempld_wdt_ioctl(struct file *file, unsigned int cmd,
+				unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	int __user *p = argp;
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+	int options;
+	int value;
+	int ret = 0;
+
+	BUG_ON(wdt == NULL);
+
+	switch (cmd) {
+	case WDIOC_GETSUPPORT:
+		if (copy_to_user(argp, &wdt->ident, sizeof(wdt->ident)))
+			ret = -EFAULT;
+		break;
+	case WDIOC_GETSTATUS:
+	case WDIOC_GETBOOTSTATUS:
+		ret = put_user(0, p);
+		break;
+	case WDIOC_SETOPTIONS:
+		if (get_user(options, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		if (options & WDIOS_DISABLECARD)
+			ret = kempld_wdt_stop(wdt);
+		if (options & WDIOS_ENABLECARD) {
+			ret = kempld_wdt_start(wdt);
+			kempld_wdt_keepalive(wdt);
+		}
+		break;
+	case WDIOC_KEEPALIVE:
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_SETTIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		kempld_wdt_update_timeouts(wdt);
+		wdt->timeout = value;
+		ret = kempld_wdt_settimeout(wdt);
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETTIMEOUT:
+		value = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+		value += kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+		if (value < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(value, p);
+		break;
+	case WDIOC_SETPRETIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		kempld_wdt_update_timeouts(wdt);
+		wdt->pretimeout = value;
+		ret = kempld_wdt_settimeout(wdt);
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETPRETIMEOUT:
+		value = kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+		if (value)
+			value = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+		if (value < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(value, p);
+		break;
+	default:
+		ret = -ENOTTY;
+	}
+
+	return ret;
+}
+
+static int kempld_wdt_release(struct inode *inode, struct file *file)
+{
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (wdt->expect_close)
+		kempld_wdt_stop(wdt);
+	else {
+		dev_warn(wdt->pld->dev,
+			 "Unexpected close, not stopping watchdog!\n");
+		kempld_wdt_keepalive(wdt);
+	}
+
+	kempld_wdt->expect_close = 0;
+
+	clear_bit(0, &wdt->is_open);
+
+	return 0;
+}
+
+static int kempld_wdt_open(struct inode *inode, struct file *file)
+{
+	int ret;
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	BUG_ON(wdt == NULL);
+
+	if (test_and_set_bit(0, &wdt->is_open))
+		return -EBUSY;
+
+	if (nowayout)
+		__module_get(THIS_MODULE);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(pld);
+
+	/* kick the watchdog if it is already enabled, otherwise start it */
+	if (status & KEMPLD_WDT_CFG_ENABLE) {
+		kempld_wdt_keepalive(wdt);
+	} else {
+		ret = kempld_wdt_settimeout(wdt);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+	}
+
+	return nonseekable_open(inode, file);
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev, "Failed to enable the watchdog timer!\n");
+	wdt->expect_close = 1;
+	kempld_wdt_release(inode, file);
+
+	return ret;
+}
+
+static void kempld_wdt_release_stages(struct kempld_watchdog_data *wdt)
+{
+	int stage;
+
+	wdt->timeout_stage = NULL;
+	wdt->pretimeout_stage = NULL;
+
+	for (stage = 0; stage < KEMPLD_WDT_MAX_STAGES; stage++) {
+		kfree(wdt->stage[stage]);
+		wdt->stage[stage] = NULL;
+	}
+}
+
+static int kempld_wdt_probe_stages(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int i, ret;
+	u32 timeout_mask;
+	struct kempld_watchdog_stage *stage;
+
+	wdt->stages = 0;
+	wdt->timeout_stage = NULL;
+	wdt->pretimeout_stage = NULL;
+
+	for (i = 0; i < KEMPLD_WDT_MAX_STAGES; i++) {
+		int j;
+		u8 index, data, data_orig;
+
+		index = KEMPLD_WDT_STAGE_TIMEOUT(i);
+		timeout_mask = ~0;
+
+		kempld_get_mutex_set_index(pld, index);
+
+		/* Probe each byte individually according to new spec revision.
+		 * Register content is restored afterwards. */
+		for (j = 0; j < 4; j++) {
+			data_orig = kempld_read8(pld, index);
+			kempld_write8(pld, index, 0x00);
+			data = kempld_read8(pld, index);
+			kempld_write8(pld, index, data_orig);
+			*(((u8 *)&timeout_mask)+j) &= data;
+			if (data != 0x0)
+				break;
+			index++;
+		}
+
+		kempld_release_mutex(pld);
+
+		if ((timeout_mask & 0xff) != 0xff) {
+			stage = kzalloc(sizeof(struct kempld_watchdog_stage),
+					GFP_KERNEL);
+			if (stage == NULL) {
+				ret = -ENOMEM;
+				goto err_alloc_stages;
+			}
+			stage->num = i;
+			stage->timeout_mask = ~timeout_mask;
+			wdt->stage[i] = stage;
+			wdt->stages++;
+
+			/* assign available stages to timeout and pretimeout */
+			if (wdt->timeout_stage == NULL) {
+				wdt->timeout_stage = stage;
+			} else if ((wdt->pretimeout_stage == NULL) &&
+				(pld->feature_mask & KEMPLD_FEATURE_BIT_NMI)) {
+				wdt->pretimeout_stage = wdt->timeout_stage;
+				wdt->timeout_stage = stage;
+			}
+		} else
+			wdt->stage[i] = NULL;
+	}
+
+	return 0;
+
+err_alloc_stages:
+	kempld_wdt_release_stages(wdt);
+
+	return ret;
+}
+
+static const struct file_operations kempld_wdt_fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.write		= kempld_wdt_write,
+	.unlocked_ioctl	= kempld_wdt_ioctl,
+	.open		= kempld_wdt_open,
+	.release	= kempld_wdt_release,
+};
+
+static struct miscdevice kempld_wdt_miscdev = {
+	.minor	= WATCHDOG_MINOR,
+	.name	= "watchdog",
+	.fops	= &kempld_wdt_fops,
+};
+
+static int kempld_wdt_probe(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt;
+	struct kempld_device_data *pld;
+	u8 status;
+	int ret;
+
+	if (kempld_wdt != NULL) {
+		dev_err(&pdev->dev,
+			"unable to support more than one watchdog devices\n");
+		return -EMFILE;
+	}
+
+	wdt = kzalloc(sizeof(struct kempld_watchdog_data), GFP_KERNEL);
+	if (wdt == NULL) {
+		dev_err(&pdev->dev, "unable to get memory for device data\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	pld = dev_get_drvdata(pdev->dev.parent);
+	wdt->pld = pld;
+
+	platform_set_drvdata(pdev, wdt);
+
+	strncpy(wdt->ident.identity, "KEMPLD Watchdog",
+		sizeof(wdt->ident.identity));
+
+	/* watchdog firmware version is identical to the CPLD version */
+	wdt->ident.firmware_version = (pld->info.major<<24)
+		| (pld->info.minor<<16) | pld->info.buildnr;
+
+	/* probe how many usable stages we have */
+	ret = kempld_wdt_probe_stages(wdt);
+	if (ret)
+		goto err_probe_stages;
+
+	/* get initial watchdog status */
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(wdt->pld);
+
+	/* check if the watchdog is already locked and enable the nowayout
+	 * option in that case */
+	if (status & (KEMPLD_WDT_CFG_ENABLE_LOCK |
+			KEMPLD_WDT_CFG_GLOBAL_LOCK)) {
+		if (!nowayout)
+			dev_warn(wdt->pld->dev,
+				 "Forcing nowayout - watchdog lock enabled!\n");
+		nowayout = 1;
+	}
+
+	/* set default values for the case we start the watchdog or change
+	 * the configuration */
+	wdt->timeout = WATCHDOG_DEFAULT_TIMEOUT;
+	wdt->pretimeout = WATCHDOG_DEFAULT_PRETIMEOUT;
+
+	/* check if watchdog is enabled */
+	if (status & KEMPLD_WDT_CFG_ENABLE) {
+		/* Get current watchdog settings */
+		kempld_wdt_update_timeouts(wdt);
+
+		dev_info(wdt->pld->dev, "Watchdog is already enabled:\n"
+			 "%d s timeout and %d s pretimeout!\n",
+			 wdt->timeout, wdt->pretimeout);
+	}
+
+	/* update the timeout settings if requested by module parameters */
+	if (timeout > 0)
+		wdt->timeout = timeout;
+	if (pretimeout >= 0)
+		wdt->pretimeout = pretimeout;
+
+	dev_info(wdt->pld->dev, "watchdog will be set on (re)start\n"
+		 "new settings: %d s timeout and %d s pretimeout\n",
+		 wdt->timeout, wdt->pretimeout);
+
+	wdt->ident.options = WDIOF_KEEPALIVEPING;
+	if ((wdt->timeout_stage) && !(status & KEMPLD_WDT_CFG_GLOBAL_LOCK))
+		wdt->ident.options |= WDIOF_SETTIMEOUT;
+	if (wdt->pretimeout_stage)
+		wdt->ident.options |= WDIOF_PRETIMEOUT;
+	if (!nowayout)
+		wdt->ident.options |= WDIOF_MAGICCLOSE;
+
+	kempld_wdt = wdt;
+
+	ret = misc_register(&kempld_wdt_miscdev);
+	if (ret)
+		goto err_misc_register;
+
+	dev_info(wdt->pld->dev,
+		 "%d stage watchdog initialized, pretimeout %ssupported\n",
+		 wdt->stages, wdt->pretimeout_stage ? "" : "not ");
+
+	return 0;
+
+err_probe_stages:
+err_misc_register:
+	kfree(kempld_wdt);
+	kempld_wdt = NULL;
+err_alloc_dev_data:
+	return ret;
+}
+
+static void kempld_wdt_shutdown(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	if (wdt != NULL) {
+		if (!nowayout)
+			kempld_wdt_stop(wdt);
+		else
+			kempld_wdt_keepalive(wdt);
+	}
+}
+
+static int kempld_wdt_remove(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	kempld_wdt_shutdown(pdev);
+
+	misc_deregister(&kempld_wdt_miscdev);
+
+	kempld_wdt_release_stages(wdt);
+
+	kfree(wdt);
+	kempld_wdt = NULL;
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int wdt_pm_status_store;
+
+/* Disable watchdog if it is active during suspend */
+static int kempld_wdt_suspend(struct platform_device *pdev,
+				pm_message_t message)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+	struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	wdt_pm_status_store = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(pld);
+
+	kempld_wdt_update_timeouts(wdt);
+
+	if (wdt_pm_status_store & KEMPLD_WDT_CFG_ENABLE)
+		kempld_wdt_shutdown(pdev);
+
+	return 0;
+}
+
+/* Enable watchdog and configure it if necessary */
+static int kempld_wdt_resume(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+	int ret;
+
+	/* if watchdog was stopped before suspend be sure it gets disabled
+	 * again, for the case BIOS has enabled it during resume */
+	if (wdt_pm_status_store & KEMPLD_WDT_CFG_ENABLE) {
+		ret = kempld_wdt_settimeout(wdt);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+
+		dev_info(wdt->pld->dev, "Resuming watchdog operation:\n"
+			 "%d s timeout and %d s pretimeout\n", wdt->timeout,
+			 wdt->pretimeout);
+	} else
+		kempld_wdt_shutdown(pdev);
+
+	return 0;
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev,
+		"Failed to reenable the watchdog timer after resume!\n");
+
+	return ret;
+}
+#else
+#define kempld_wdt_suspend	NULL
+#define kempld_wdt_resume	NULL
+#endif
+
+static struct platform_driver kempld_wdt_driver = {
+	.driver = {
+		.name = "kempld-wdt",
+		.owner = THIS_MODULE,
+	},
+	.probe = kempld_wdt_probe,
+	.remove = kempld_wdt_remove,
+	.shutdown = kempld_wdt_shutdown,
+	.suspend = kempld_wdt_suspend,
+	.resume = kempld_wdt_resume,
+};
+
+static int __init kempld_wdt_init(void)
+{
+	return platform_driver_register(&kempld_wdt_driver);
+}
+
+static void __exit kempld_wdt_exit(void)
+{
+	platform_driver_unregister(&kempld_wdt_driver);
+}
+
+module_init(kempld_wdt_init);
+module_exit(kempld_wdt_exit);
+
+MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
drivers/watchdog/kempld_wdt.h new file +75 -0
new file mode 100644
index 0000000..80f68f6
@@ -0,0 +1,75 @@
+/*
+ *  kempld_wdt.h - Kontron PLD watchdog driver definitions
+ *
+ *  Copyright (c) 2010-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _KEMPLD_WDT_H_
+#define _KEMPLD_WDT_H_
+
+/* watchdog register definitions */
+#define KEMPLD_WDT_KICK			0x16
+#define KEMPLD_WDT_CFG			0x17
+#define		KEMPLD_WDT_CFG_STAGE_TIMEOUT_OCCURED(x)	(1<<x)
+#define		KEMPLD_WDT_CFG_ENABLE_LOCK		0x8
+#define		KEMPLD_WDT_CFG_ENABLE			0x10
+#define		KEMPLD_WDT_CFG_AUTO_RELOAD		0x40
+#define		KEMPLD_WDT_CFG_GLOBAL_LOCK		0x80
+#define KEMPLD_WDT_STAGE_CFG(x)		(0x18+x)
+#define		KEMPLD_WDT_STAGE_CFG_ACTION_MASK	0x7
+#define		KEMPLD_WDT_STAGE_CFG_GET_ACTION(x)	(x & 0x7)
+#define		KEMPLD_WDT_STAGE_CFG_ASSERT		(1<<3)
+#define		KEMPLD_WDT_STAGE_CFG_PRESCALER_MASK	0x30
+#define		KEMPLD_WDT_STAGE_CFG_GET_PRESCALER(x)	((x & 0x30)>>4)
+#define		KEMPLD_WDT_STAGE_CFG_SET_PRESCALER(x)	((x & 0x30)<<4)
+#define KEMPLD_WDT_STAGE_TIMEOUT(x)	(0x1b+x*4)
+#define KEMPLD_WDT_MAX_STAGES		3
+
+#define	KEMPLD_WDT_ACTION_NONE		0x0
+#define	KEMPLD_WDT_ACTION_RESET		0x1
+#define	KEMPLD_WDT_ACTION_NMI		0x2
+#define	KEMPLD_WDT_ACTION_SMI		0x3
+#define	KEMPLD_WDT_ACTION_SCI		0x4
+#define	KEMPLD_WDT_ACTION_DELAY		0x5
+
+#define	KEMPLD_WDT_PRESCALER_21BIT	0x0
+#define	KEMPLD_WDT_PRESCALER_17BIT	0x1
+#define	KEMPLD_WDT_PRESCALER_12BIT	0x2
+
+const int kempld_prescaler_bits[] = { 21, 17, 12 };
+#define KEMPLD_PRESCALER(x)	(0xffffffff>>(32-kempld_prescaler_bits[x]))
+
+
+struct kempld_watchdog_stage {
+	int	num;
+	u32	timeout_mask;
+};
+
+struct kempld_watchdog_data {
+	int				timeout;
+	int				pretimeout;
+	unsigned long			is_open;
+	unsigned long			expect_close;
+	int				stages;
+	struct kempld_watchdog_stage	*timeout_stage;
+	struct kempld_watchdog_stage	*pretimeout_stage;
+	struct kempld_device_data	*pld;
+	struct watchdog_info		ident;
+	struct kempld_watchdog_stage	*stage[KEMPLD_WDT_MAX_STAGES];
+};
+
+#endif /* _KEMPLD_WDT_H_ */
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#13 lore raw

Re: [PATCH 4/4] watchdog: Kontron PLD watchdog timer

Quoted 6 lines · Guenter Roeck
Quoted 5 lines · Kevin Strasser

From: Michael Brunner <[email protected]>

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

Quoted 7 lines
Quoted 2 lines

Personally I would prefer two separate patches for the two drivers, and to have the drivers converted to the watchdog infrastructure. Wim's call, of course.

Thanks for the feedback. I'm happy to do both if Wim thinks it's necessary.

-Kevin

Quoted 1,557 lines

Thanks,
Guenter

Quoted 1,554 lines
---
 drivers/watchdog/Kconfig           |   20 +
 drivers/watchdog/Makefile          |    2 +
 drivers/watchdog/kempld_now1_wdt.c |  602 +++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.c      |  796 ++++++++++++++++++++++++++++++++++++
 drivers/watchdog/kempld_wdt.h      |   75 ++++
 5 files changed, 1495 insertions(+)
 create mode 100644 drivers/watchdog/kempld_now1_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.c
 create mode 100644 drivers/watchdog/kempld_wdt.h
drivers/watchdog/Kconfig +20 -0
index 9fcc70c..9ac71ca 100644
@@ -687,6 +687,26 @@ config HP_WATCHDOG
 	  To compile this driver as a module, choose M here: the module will be
 	  called hpwdt.
 
+config KEMPLD_WDT
+	tristate "Kontron COM watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on some Kontron ETX and COMexpress
+	  (ETXexpress) modules
+
+	  This driver can also be built as a module. If so, the module will be
+	  called kempld_wdt.
+
+config KEMPLD_NOW1_WDT
+	tristate "Kontron COMe-mSP1 (nanoETXexpress-SP) watchdog"
+	depends on MFD_KEMPLD
+	help
+	  Support for the PLD watchdog on the Kontron COMe-mSP1
+	  (nanoETXexpress-SP) module.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called kempld_now1_wdt.
+
 config HPWDT_NMI_DECODING
 	bool "NMI decoding support for the HP ProLiant iLO2+ Hardware Watchdog Timer"
 	depends on HP_WATCHDOG
drivers/watchdog/Makefile +2 -0
index a300b94..a029930 100644
@@ -90,6 +90,8 @@ endif
 obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
+obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
+obj-$(COnFIG_KEMPLD_NOW1_WDT) += kempld_now1_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/kempld_now1_wdt.c new file +602 -0
new file mode 100644
index 0000000..19b7272
@@ -0,0 +1,602 @@
+/*
+ *  kempld_now1_wdt.c - Kontron PLD watchdog driver for COMe-mSP1
+ *
+ *  Copyright (c) 2011-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  Note: The watchdog of the COMe-mSP1 (nanoETXexpress-SP) has one stage and
+ *        only supports predefined watchdog timeout values.
+ *
+ *        The supported timeouts are:
+ *              1 s, 5 s, 10 s, 30 s, 1 min, 5 min, 10 min, 15 min
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/miscdevice.h>
+#include <linux/watchdog.h>
+#include <linux/fs.h>
+#include <linux/ioport.h>
+#include <linux/init.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/kempld.h>
+
+#include "kempld_wdt.h"
+
+#define WATCHDOG_TIMEOUT 30
+static int timeout = WATCHDOG_TIMEOUT;
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. (1, 5, 10, 30, 60, 300, 600, "
+		 "900, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout,
+		 "Watchdog cannot be stopped once started (default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+/* nanoETXexpress-SP watchdog register definitions */
+#define KEMPLD_WDT_NOW1					0xA2
+#define		KEMPLD_WDT_NOW1_KICK_MASK		0x80
+#define		KEMPLD_WDT_NOW1_ENABLE_MASK		0x40
+#define		KEMPLD_WDT_NOW1_TIMEOUT_MASK		0x1f
+#define			KEMPLD_WDT_NOW1_TIMEOUT_1SEC	0x00
+#define			KEMPLD_WDT_NOW1_TIMEOUT_5SEC	0x01
+#define			KEMPLD_WDT_NOW1_TIMEOUT_10SEC	0x02
+#define			KEMPLD_WDT_NOW1_TIMEOUT_30SEC	0x03
+#define			KEMPLD_WDT_NOW1_TIMEOUT_1MIN	0x10
+#define			KEMPLD_WDT_NOW1_TIMEOUT_5MIN	0x11
+#define			KEMPLD_WDT_NOW1_TIMEOUT_10MIN	0x12
+#define			KEMPLD_WDT_NOW1_TIMEOUT_15MIN	0x13
+
+/* delay in us necessary due to clock domain sync */
+#define		KEMPLD_WDT_NOW1_SYNC_DELAY		31
+
+static struct kempld_watchdog_data *kempld_now1_wdt;
+
+static int kempld_now1_wdt_read_supported;
+static int kempld_now1_wdt_reg_cache;
+
+static int kempld_now1_wdt_start(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	if (kempld_now1_wdt_read_supported)
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	wdt_reg |= KEMPLD_WDT_NOW1_ENABLE_MASK;
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	if (kempld_now1_wdt_read_supported) {
+		/* read out the register again to check if everything worked */
+		kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_release_mutex(pld);
+
+		/* check if the watchdog was disabled */
+		if (!(wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK))
+			return -EACCES;
+	}
+
+	return 0;
+}
+
+static int kempld_now1_wdt_stop(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	wdt_reg &= ~KEMPLD_WDT_NOW1_ENABLE_MASK;
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	if (kempld_now1_wdt_read_supported) {
+		/* read out the register again to check if everything worked */
+		kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_release_mutex(pld);
+
+		/* check if the watchdog was disabled */
+		if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK)
+			return -EACCES;
+	}
+
+	return 0;
+}
+
+static int kempld_now1_wdt_keepalive(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+	if (kempld_now1_wdt_read_supported) {
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = kempld_now1_wdt_reg_cache;
+		/* write the state again to be sure the trigger register has
+		 * the right level */
+		kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+	}
+
+	if (wdt_reg & KEMPLD_WDT_NOW1_KICK_MASK)
+		wdt_reg &= ~KEMPLD_WDT_NOW1_KICK_MASK;
+	else
+		wdt_reg |= KEMPLD_WDT_NOW1_KICK_MASK;
+
+	udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+	kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+
+	kempld_now1_wdt_reg_cache = wdt_reg;
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+
+static int kempld_now1_wdt_settimeout(struct kempld_watchdog_data *wdt,
+					int check_only)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+	int ret = 0;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+
+	wdt_reg &= ~KEMPLD_WDT_NOW1_TIMEOUT_MASK;
+
+	switch (wdt->timeout) {
+	case 1:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_1SEC;
+		break;
+	case 5:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_5SEC;
+		break;
+	case 10:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_10SEC;
+		break;
+	case 30:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_30SEC;
+		break;
+	case 60:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_1MIN;
+		break;
+	case 300:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_5MIN;
+		break;
+	case 600:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_10MIN;
+		break;
+	case 900:
+		wdt_reg |= KEMPLD_WDT_NOW1_TIMEOUT_15MIN;
+		break;
+	default:
+		ret = -EINVAL;
+		dev_err(wdt->pld->dev,
+			"Invalid timeout value given!\n");
+	}
+
+	if (!check_only) {
+		if (ret == 0) {
+			udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+
+			kempld_write8(pld, KEMPLD_WDT_NOW1, wdt_reg);
+		}
+	}
+
+	kempld_now1_wdt_reg_cache = wdt_reg;
+
+	kempld_release_mutex(pld);
+
+	return ret;
+}
+
+static int kempld_now1_wdt_gettimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int wdt_reg;
+	int timeout;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+		kempld_now1_wdt_reg_cache = wdt_reg;
+	} else
+		wdt_reg = kempld_now1_wdt_reg_cache;
+
+	kempld_release_mutex(pld);
+
+	switch (wdt_reg&KEMPLD_WDT_NOW1_TIMEOUT_MASK) {
+	case KEMPLD_WDT_NOW1_TIMEOUT_1SEC:
+		timeout = 1;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_5SEC:
+		timeout = 5;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_10SEC:
+		timeout = 10;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_30SEC:
+		timeout = 30;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_1MIN:
+		timeout = 60;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_5MIN:
+		timeout = 300;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_10MIN:
+		timeout = 600;
+		break;
+	case KEMPLD_WDT_NOW1_TIMEOUT_15MIN:
+		timeout = 900;
+		break;
+	default:
+		timeout = -ERANGE;
+	}
+
+	return timeout;
+}
+
+static ssize_t kempld_now1_wdt_write(struct file *file, const char __user
+					*data, size_t count, loff_t *ppos)
+{
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (count) {
+		kempld_now1_wdt_keepalive(wdt);
+
+		if (!nowayout) {
+			size_t i;
+
+			wdt->expect_close = 0;
+
+			for (i = 0; i < count; i++) {
+				char c;
+				if (get_user(c, data+i))
+					return -EFAULT;
+				if (c == 'V')
+					wdt->expect_close = 42;
+			}
+		}
+	}
+
+	return count;
+}
+
+static long kempld_now1_wdt_ioctl(struct file *file, unsigned int cmd,
+				unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	int __user *p = argp;
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+	int options;
+	int value;
+	int ret = 0;
+
+	BUG_ON(wdt == NULL);
+
+	switch (cmd) {
+	case WDIOC_GETSUPPORT:
+		if (copy_to_user(argp, &wdt->ident, sizeof(wdt->ident)))
+			ret = -EFAULT;
+		break;
+	case WDIOC_GETSTATUS:
+	case WDIOC_GETBOOTSTATUS:
+		ret = put_user(0, p);
+		break;
+	case WDIOC_SETOPTIONS:
+		if (get_user(options, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		if (options & WDIOS_DISABLECARD)
+			ret = kempld_now1_wdt_stop(wdt);
+		if (options & WDIOS_ENABLECARD) {
+			ret = kempld_now1_wdt_start(wdt);
+			kempld_now1_wdt_keepalive(wdt);
+		}
+		break;
+	case WDIOC_KEEPALIVE:
+		kempld_now1_wdt_keepalive(wdt);
+		break;
+	case WDIOC_SETTIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		wdt->timeout = value;
+		ret = kempld_now1_wdt_settimeout(wdt, 0);
+		kempld_now1_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETTIMEOUT:
+		value = kempld_now1_wdt_gettimeout(wdt, wdt->timeout_stage);
+		if (timeout < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(timeout, p);
+		break;
+	default:
+		ret = -ENOTTY;
+	}
+
+	return ret;
+}
+
+static int kempld_now1_wdt_release(struct inode *inode, struct file *file)
+{
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (wdt->expect_close)
+		kempld_now1_wdt_stop(wdt);
+	else {
+		dev_warn(wdt->pld->dev,
+			 "Unexpected close, not stopping watchdog!\n");
+		kempld_now1_wdt_keepalive(wdt);
+	}
+
+	kempld_now1_wdt->expect_close = 0;
+
+	clear_bit(0, &wdt->is_open);
+
+	return 0;
+}
+
+static int kempld_now1_wdt_open(struct inode *inode, struct file *file)
+{
+	int ret;
+	struct kempld_watchdog_data *wdt = kempld_now1_wdt;
+	struct kempld_device_data *pld = wdt->pld;
+	u8 wdt_reg;
+
+	BUG_ON(wdt == NULL);
+
+	if (test_and_set_bit(0, &wdt->is_open))
+		return -EBUSY;
+
+	if (nowayout)
+		__module_get(THIS_MODULE);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = kempld_now1_wdt_reg_cache;
+	}
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(pld);
+
+	/* kick the watchdog if it is already enabled, otherwise start it */
+	if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK) {
+		kempld_now1_wdt_keepalive(wdt);
+	} else {
+		ret = kempld_now1_wdt_settimeout(wdt, 0);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_now1_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+	}
+
+	return nonseekable_open(inode, file);
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev, "Failed to enable the watchdog timer!\n");
+	wdt->expect_close = 1;
+	kempld_now1_wdt_release(inode, file);
+
+	return ret;
+}
+
+static const struct file_operations kempld_now1_wdt_fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.write		= kempld_now1_wdt_write,
+	.unlocked_ioctl	= kempld_now1_wdt_ioctl,
+	.open		= kempld_now1_wdt_open,
+	.release	= kempld_now1_wdt_release,
+};
+
+static struct miscdevice kempld_now1_wdt_miscdev = {
+	.minor	= WATCHDOG_MINOR,
+	.name	= "watchdog",
+	.fops	= &kempld_now1_wdt_fops,
+};
+
+static int kempld_now1_wdt_probe(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt;
+	struct kempld_device_data *pld;
+	u8 wdt_reg;
+	int ret;
+
+	if (kempld_now1_wdt != NULL) {
+		dev_err(&pdev->dev,
+			"unable to support more than one watchdog devices\n");
+		return -EMFILE;
+	}
+
+	wdt = kzalloc(sizeof(struct kempld_watchdog_data), GFP_KERNEL);
+	if (wdt == NULL) {
+		dev_err(&pdev->dev, "unable to get memory for device data\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	wdt->timeout_stage = kzalloc(sizeof(struct kempld_watchdog_stage),
+					GFP_KERNEL);
+	if (wdt->timeout_stage == NULL) {
+		dev_err(&pdev->dev,
+			"unable to get memory for watchdog stage\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	wdt->stages = 1;
+	wdt->stage[0] = wdt->timeout_stage;
+
+	pld = dev_get_drvdata(pdev->dev.parent);
+	wdt->pld = pld;
+
+	platform_set_drvdata(pdev, wdt);
+
+	strncpy(wdt->ident.identity, "nanoETXexpress-SP Watchdog",
+		sizeof(wdt->ident.identity));
+
+	/* set default values for the case we start the watchdog or change
+	 * the configuration */
+	wdt->timeout = timeout;
+
+	/* use settimeout to check if the timeout parameter is valid */
+	ret = kempld_now1_wdt_settimeout(wdt, 1);
+	if (ret)
+		goto err_check_timeout;
+	wdt->ident.firmware_version = (pld->info.major<<8) + pld->info.minor;
+	if (pld->info.major > 67)
+		kempld_now1_wdt_read_supported = 1;
+	else
+		dev_info(wdt->pld->dev,
+			 "Watchdog revision does not support read - "
+			 "unable to get watchdog state!\n");
+
+	/* get initial watchdog status */
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_NOW1);
+	if (kempld_now1_wdt_read_supported) {
+		udelay(KEMPLD_WDT_NOW1_SYNC_DELAY);
+		wdt_reg = kempld_read8(pld, KEMPLD_WDT_NOW1);
+	} else {
+		wdt_reg = 0x0;
+	}
+	kempld_now1_wdt_reg_cache = wdt_reg;
+	kempld_release_mutex(wdt->pld);
+
+	/* check if watchdog is enabled */
+	if (wdt_reg & KEMPLD_WDT_NOW1_ENABLE_MASK)
+		dev_info(wdt->pld->dev, "Watchdog is already enabled!\n");
+
+	wdt->ident.options = WDIOF_KEEPALIVEPING;
+	wdt->ident.options |= WDIOF_SETTIMEOUT;
+	if (!nowayout)
+		wdt->ident.options |= WDIOF_MAGICCLOSE;
+
+	kempld_now1_wdt = wdt;
+
+	ret = misc_register(&kempld_now1_wdt_miscdev);
+	if (ret)
+		goto err_misc_register;
+
+	dev_info(wdt->pld->dev, "watchdog initialized\n");
+
+	return 0;
+
+err_misc_register:
+	kfree(kempld_now1_wdt);
+	kempld_now1_wdt = NULL;
+err_check_timeout:
+err_alloc_dev_data:
+	return ret;
+}
+
+static int kempld_now1_wdt_remove(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_now1_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	if (wdt != NULL) {
+		if (!nowayout)
+			kempld_now1_wdt_stop(wdt);
+		else
+			kempld_now1_wdt_keepalive(wdt);
+	}
+
+	misc_deregister(&kempld_now1_wdt_miscdev);
+
+	kfree(wdt);
+	kempld_now1_wdt = NULL;
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+static struct platform_driver kempld_now1_wdt_driver = {
+	.driver = {
+		.name = "kempld_now1-wdt",
+		.owner = THIS_MODULE,
+	},
+	.probe = kempld_now1_wdt_probe,
+	.remove = kempld_now1_wdt_remove,
+};
+
+static int __init kempld_now1_wdt_init(void)
+{
+	return platform_driver_register(&kempld_now1_wdt_driver);
+}
+
+static void __exit kempld_now1_wdt_exit(void)
+{
+	platform_driver_unregister(&kempld_now1_wdt_driver);
+}
+
+module_init(kempld_now1_wdt_init);
+module_exit(kempld_now1_wdt_exit);
+
+MODULE_DESCRIPTION("KEM PLD nanoETXexpress-SP Watchdog Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
drivers/watchdog/kempld_wdt.c new file +796 -0
new file mode 100644
index 0000000..bc150e5
@@ -0,0 +1,796 @@
+/*
+ *  kempld_wdt.c - Kontron PLD watchdog driver
+ *
+ *  Copyright (c) 2010-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  Note: From the PLD watchdog point of view timeout and pretimeout are
+ *        defined differently than in the kernel.
+ *        First the pretimeout stage runs out before the timeout stage gets
+ *        active. This has to be kept in mind.
+ *
+ *  Kernel/API:                     P-----| pretimeout
+ *                |-----------------------T timeout
+ *  Watchdog:     |-----------------P       pretimeout_stage
+ *                                  |-----T timeout_stage
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/miscdevice.h>
+#include <linux/watchdog.h>
+#include <linux/fs.h>
+#include <linux/ioport.h>
+#include <linux/init.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/kempld.h>
+
+#include "kempld_wdt.h"
+
+#define WATCHDOG_DEFAULT_TIMEOUT 20
+#define WATCHDOG_DEFAULT_PRETIMEOUT 0
+static int timeout = -1;
+static int pretimeout = -1;
+/* The maximum timeout values have to be probed */
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. (>0, default="
+		__MODULE_STRING(WATCHDOG_DEFAULT_TIMEOUT) ")");
+module_param(pretimeout, int, 0);
+MODULE_PARM_DESC(pretimeout,
+		 "Watchdog pretimeout in seconds. (>=0, default="
+		__MODULE_STRING(WATCHDOG_DEFAULT_PRETIMEOUT) ")");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout,
+		 "Watchdog cannot be stopped once started (default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static struct kempld_watchdog_data *kempld_wdt;
+
+static int kempld_wdt_start(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	status |= KEMPLD_WDT_CFG_ENABLE;
+	kempld_write8(pld, KEMPLD_WDT_CFG, status);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+
+	kempld_release_mutex(pld);
+
+	/* check if the watchdog was enabled */
+	if (!(status & KEMPLD_WDT_CFG_ENABLE))
+		return -EACCES;
+
+	return 0;
+}
+
+static int kempld_wdt_stop(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	status &= ~KEMPLD_WDT_CFG_ENABLE;
+	kempld_write8(pld, KEMPLD_WDT_CFG, status);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+
+	kempld_release_mutex(pld);
+
+	/* check if the watchdog was disabled */
+	if (status & KEMPLD_WDT_CFG_ENABLE)
+		return -EACCES;
+
+	return 0;
+}
+
+static int kempld_wdt_keepalive(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+
+	kempld_write8(pld, KEMPLD_WDT_KICK, 'K');
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static int kempld_wdt_gettimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+	int bits;
+	u64 timeout;
+	u32 remainder;
+
+	if (stage == NULL)
+		return 0;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	timeout = kempld_read32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->num));
+
+	kempld_release_mutex(pld);
+
+	bits = KEMPLD_WDT_STAGE_CFG_GET_PRESCALER(stage_cfg);
+	timeout = (timeout & stage->timeout_mask) * KEMPLD_PRESCALER(bits);
+	remainder = do_div(timeout, pld->pld_clock);
+
+	/* Round up the return value if necessary */
+	if ((timeout > 0) && (remainder >= (pld->pld_clock/2)))
+		timeout++;
+
+	return timeout;
+}
+
+static int kempld_wdt_setstageaction(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage,
+				 int action)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+
+	if (stage == NULL)
+		return -EINVAL;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_ACTION_MASK;
+	stage_cfg |= (action & KEMPLD_WDT_STAGE_CFG_ACTION_MASK);
+	if (action == KEMPLD_WDT_ACTION_RESET)
+		stage_cfg |= KEMPLD_WDT_STAGE_CFG_ASSERT;
+	else
+		stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_ASSERT;
+
+	kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->num), stage_cfg);
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static int kempld_wdt_setstagetimeout(struct kempld_watchdog_data *wdt,
+				 struct kempld_watchdog_stage *stage,
+				 int timeout)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	u8 stage_cfg;
+	u8 prescaler;
+	u64 stage_timeout64;
+	u32 stage_timeout;
+	u32 remainder;
+
+	if (stage == NULL)
+		return -EINVAL;
+
+	prescaler = KEMPLD_WDT_PRESCALER_21BIT;
+
+	stage_timeout64 = ((u64)timeout*pld->pld_clock);
+	remainder = do_div(stage_timeout64, KEMPLD_PRESCALER(prescaler));
+	if (remainder)
+		stage_timeout64++;
+	stage_timeout = stage_timeout64 & stage->timeout_mask;
+
+	if (stage_timeout64 != (u64)stage_timeout)
+		return -EINVAL;
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+
+	stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->num));
+	stage_cfg &= ~KEMPLD_WDT_STAGE_CFG_PRESCALER_MASK;
+	stage_cfg |= KEMPLD_WDT_STAGE_CFG_SET_PRESCALER(prescaler);
+	kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->num), stage_cfg);
+	kempld_write32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->num),
+			stage_timeout);
+
+	kempld_release_mutex(pld);
+
+	return 0;
+}
+
+static void kempld_wdt_update_timeouts(struct kempld_watchdog_data *wdt)
+{
+	int pretimeout_stage;
+	int timeout_stage;
+
+	pretimeout_stage = kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+	timeout_stage = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+
+	if (pretimeout_stage)
+		wdt->pretimeout = timeout_stage;
+	else
+		wdt->pretimeout = 0;
+
+	wdt->timeout = pretimeout_stage + timeout_stage;
+
+	if (wdt->pretimeout < 0) {
+		wdt->pretimeout = WATCHDOG_DEFAULT_PRETIMEOUT;
+		dev_err(wdt->pld->dev, "failed to get valid pretimeout value\n"
+			" -> using driver default\n");
+	}
+	if (wdt->timeout < 0) {
+		wdt->timeout = WATCHDOG_DEFAULT_TIMEOUT;
+		dev_err(wdt->pld->dev, "failed to get valid timeout value\n"
+			" -> using driver default\n");
+	}
+}
+
+static int kempld_wdt_settimeout(struct kempld_watchdog_data *wdt)
+{
+	int stage_timeout;
+	int stage_pretimeout;
+	int ret;
+
+	if ((wdt->timeout <= 0) ||
+		(wdt->pretimeout < 0) ||
+		(wdt->pretimeout > wdt->timeout)) {
+		ret = -EINVAL;
+		goto err_check_values;
+	}
+
+	if ((wdt->pretimeout == 0) || (wdt->pretimeout_stage == NULL)) {
+		if (wdt->pretimeout != 0)
+			dev_warn(wdt->pld->dev,
+				 "no pretimeout stage available\n"
+				 " -> only enabling reset\n");
+		stage_pretimeout = 0;
+		stage_timeout = wdt->timeout;
+	} else {
+		stage_pretimeout = wdt->timeout - wdt->pretimeout;
+		stage_timeout = wdt->pretimeout;
+	}
+
+	if (stage_pretimeout != 0) {
+		ret = kempld_wdt_setstageaction(wdt, wdt->pretimeout_stage,
+						KEMPLD_WDT_ACTION_NMI);
+	} else if ((stage_pretimeout == 0)
+			&& (wdt->pretimeout_stage != NULL)) {
+		ret = kempld_wdt_setstageaction(wdt, wdt->pretimeout_stage,
+						KEMPLD_WDT_ACTION_NONE);
+	} else
+		ret = 0;
+	if (ret)
+		goto err_setstage;
+
+	if (stage_pretimeout != 0) {
+		ret = kempld_wdt_setstagetimeout(wdt, wdt->pretimeout_stage,
+						 stage_pretimeout);
+		if (ret)
+			goto err_setstage;
+	}
+
+	ret = kempld_wdt_setstageaction(wdt, wdt->timeout_stage,
+					KEMPLD_WDT_ACTION_RESET);
+	if (ret)
+		goto err_setstage;
+
+	ret = kempld_wdt_setstagetimeout(wdt, wdt->timeout_stage,
+					 stage_timeout);
+	if (ret)
+		goto err_setstage;
+
+	return 0;
+err_setstage:
+err_check_values:
+	return ret;
+}
+
+static ssize_t kempld_wdt_write(struct file *file, const char __user *data,
+				size_t count, loff_t *ppos)
+{
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (count) {
+		kempld_wdt_keepalive(wdt);
+
+		if (!nowayout) {
+			size_t i;
+
+			wdt->expect_close = 0;
+
+			for (i = 0; i < count; i++) {
+				char c;
+				if (get_user(c, data+i))
+					return -EFAULT;
+				if (c == 'V')
+					wdt->expect_close = 42;
+			}
+		}
+	}
+
+	return count;
+}
+
+static long kempld_wdt_ioctl(struct file *file, unsigned int cmd,
+				unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	int __user *p = argp;
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+	int options;
+	int value;
+	int ret = 0;
+
+	BUG_ON(wdt == NULL);
+
+	switch (cmd) {
+	case WDIOC_GETSUPPORT:
+		if (copy_to_user(argp, &wdt->ident, sizeof(wdt->ident)))
+			ret = -EFAULT;
+		break;
+	case WDIOC_GETSTATUS:
+	case WDIOC_GETBOOTSTATUS:
+		ret = put_user(0, p);
+		break;
+	case WDIOC_SETOPTIONS:
+		if (get_user(options, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		if (options & WDIOS_DISABLECARD)
+			ret = kempld_wdt_stop(wdt);
+		if (options & WDIOS_ENABLECARD) {
+			ret = kempld_wdt_start(wdt);
+			kempld_wdt_keepalive(wdt);
+		}
+		break;
+	case WDIOC_KEEPALIVE:
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_SETTIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		kempld_wdt_update_timeouts(wdt);
+		wdt->timeout = value;
+		ret = kempld_wdt_settimeout(wdt);
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETTIMEOUT:
+		value = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+		value += kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+		if (value < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(value, p);
+		break;
+	case WDIOC_SETPRETIMEOUT:
+		if (get_user(value, p)) {
+			ret = -EFAULT;
+			break;
+		}
+		kempld_wdt_update_timeouts(wdt);
+		wdt->pretimeout = value;
+		ret = kempld_wdt_settimeout(wdt);
+		kempld_wdt_keepalive(wdt);
+		break;
+	case WDIOC_GETPRETIMEOUT:
+		value = kempld_wdt_gettimeout(wdt, wdt->pretimeout_stage);
+		if (value)
+			value = kempld_wdt_gettimeout(wdt, wdt->timeout_stage);
+		if (value < 0)
+			ret = ERANGE;
+		else
+			ret = put_user(value, p);
+		break;
+	default:
+		ret = -ENOTTY;
+	}
+
+	return ret;
+}
+
+static int kempld_wdt_release(struct inode *inode, struct file *file)
+{
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+
+	BUG_ON(wdt == NULL);
+
+	if (wdt->expect_close)
+		kempld_wdt_stop(wdt);
+	else {
+		dev_warn(wdt->pld->dev,
+			 "Unexpected close, not stopping watchdog!\n");
+		kempld_wdt_keepalive(wdt);
+	}
+
+	kempld_wdt->expect_close = 0;
+
+	clear_bit(0, &wdt->is_open);
+
+	return 0;
+}
+
+static int kempld_wdt_open(struct inode *inode, struct file *file)
+{
+	int ret;
+	struct kempld_watchdog_data *wdt = kempld_wdt;
+	struct kempld_device_data *pld = wdt->pld;
+	u8 status;
+
+	BUG_ON(wdt == NULL);
+
+	if (test_and_set_bit(0, &wdt->is_open))
+		return -EBUSY;
+
+	if (nowayout)
+		__module_get(THIS_MODULE);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(pld);
+
+	/* kick the watchdog if it is already enabled, otherwise start it */
+	if (status & KEMPLD_WDT_CFG_ENABLE) {
+		kempld_wdt_keepalive(wdt);
+	} else {
+		ret = kempld_wdt_settimeout(wdt);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+	}
+
+	return nonseekable_open(inode, file);
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev, "Failed to enable the watchdog timer!\n");
+	wdt->expect_close = 1;
+	kempld_wdt_release(inode, file);
+
+	return ret;
+}
+
+static void kempld_wdt_release_stages(struct kempld_watchdog_data *wdt)
+{
+	int stage;
+
+	wdt->timeout_stage = NULL;
+	wdt->pretimeout_stage = NULL;
+
+	for (stage = 0; stage < KEMPLD_WDT_MAX_STAGES; stage++) {
+		kfree(wdt->stage[stage]);
+		wdt->stage[stage] = NULL;
+	}
+}
+
+static int kempld_wdt_probe_stages(struct kempld_watchdog_data *wdt)
+{
+	struct kempld_device_data *pld = wdt->pld;
+	int i, ret;
+	u32 timeout_mask;
+	struct kempld_watchdog_stage *stage;
+
+	wdt->stages = 0;
+	wdt->timeout_stage = NULL;
+	wdt->pretimeout_stage = NULL;
+
+	for (i = 0; i < KEMPLD_WDT_MAX_STAGES; i++) {
+		int j;
+		u8 index, data, data_orig;
+
+		index = KEMPLD_WDT_STAGE_TIMEOUT(i);
+		timeout_mask = ~0;
+
+		kempld_get_mutex_set_index(pld, index);
+
+		/* Probe each byte individually according to new spec revision.
+		 * Register content is restored afterwards. */
+		for (j = 0; j < 4; j++) {
+			data_orig = kempld_read8(pld, index);
+			kempld_write8(pld, index, 0x00);
+			data = kempld_read8(pld, index);
+			kempld_write8(pld, index, data_orig);
+			*(((u8 *)&timeout_mask)+j) &= data;
+			if (data != 0x0)
+				break;
+			index++;
+		}
+
+		kempld_release_mutex(pld);
+
+		if ((timeout_mask & 0xff) != 0xff) {
+			stage = kzalloc(sizeof(struct kempld_watchdog_stage),
+					GFP_KERNEL);
+			if (stage == NULL) {
+				ret = -ENOMEM;
+				goto err_alloc_stages;
+			}
+			stage->num = i;
+			stage->timeout_mask = ~timeout_mask;
+			wdt->stage[i] = stage;
+			wdt->stages++;
+
+			/* assign available stages to timeout and pretimeout */
+			if (wdt->timeout_stage == NULL) {
+				wdt->timeout_stage = stage;
+			} else if ((wdt->pretimeout_stage == NULL) &&
+				(pld->feature_mask & KEMPLD_FEATURE_BIT_NMI)) {
+				wdt->pretimeout_stage = wdt->timeout_stage;
+				wdt->timeout_stage = stage;
+			}
+		} else
+			wdt->stage[i] = NULL;
+	}
+
+	return 0;
+
+err_alloc_stages:
+	kempld_wdt_release_stages(wdt);
+
+	return ret;
+}
+
+static const struct file_operations kempld_wdt_fops = {
+	.owner		= THIS_MODULE,
+	.llseek		= no_llseek,
+	.write		= kempld_wdt_write,
+	.unlocked_ioctl	= kempld_wdt_ioctl,
+	.open		= kempld_wdt_open,
+	.release	= kempld_wdt_release,
+};
+
+static struct miscdevice kempld_wdt_miscdev = {
+	.minor	= WATCHDOG_MINOR,
+	.name	= "watchdog",
+	.fops	= &kempld_wdt_fops,
+};
+
+static int kempld_wdt_probe(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt;
+	struct kempld_device_data *pld;
+	u8 status;
+	int ret;
+
+	if (kempld_wdt != NULL) {
+		dev_err(&pdev->dev,
+			"unable to support more than one watchdog devices\n");
+		return -EMFILE;
+	}
+
+	wdt = kzalloc(sizeof(struct kempld_watchdog_data), GFP_KERNEL);
+	if (wdt == NULL) {
+		dev_err(&pdev->dev, "unable to get memory for device data\n");
+		ret = -ENOMEM;
+		goto err_alloc_dev_data;
+	}
+
+	pld = dev_get_drvdata(pdev->dev.parent);
+	wdt->pld = pld;
+
+	platform_set_drvdata(pdev, wdt);
+
+	strncpy(wdt->ident.identity, "KEMPLD Watchdog",
+		sizeof(wdt->ident.identity));
+
+	/* watchdog firmware version is identical to the CPLD version */
+	wdt->ident.firmware_version = (pld->info.major<<24)
+		| (pld->info.minor<<16) | pld->info.buildnr;
+
+	/* probe how many usable stages we have */
+	ret = kempld_wdt_probe_stages(wdt);
+	if (ret)
+		goto err_probe_stages;
+
+	/* get initial watchdog status */
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	status = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(wdt->pld);
+
+	/* check if the watchdog is already locked and enable the nowayout
+	 * option in that case */
+	if (status & (KEMPLD_WDT_CFG_ENABLE_LOCK |
+			KEMPLD_WDT_CFG_GLOBAL_LOCK)) {
+		if (!nowayout)
+			dev_warn(wdt->pld->dev,
+				 "Forcing nowayout - watchdog lock enabled!\n");
+		nowayout = 1;
+	}
+
+	/* set default values for the case we start the watchdog or change
+	 * the configuration */
+	wdt->timeout = WATCHDOG_DEFAULT_TIMEOUT;
+	wdt->pretimeout = WATCHDOG_DEFAULT_PRETIMEOUT;
+
+	/* check if watchdog is enabled */
+	if (status & KEMPLD_WDT_CFG_ENABLE) {
+		/* Get current watchdog settings */
+		kempld_wdt_update_timeouts(wdt);
+
+		dev_info(wdt->pld->dev, "Watchdog is already enabled:\n"
+			 "%d s timeout and %d s pretimeout!\n",
+			 wdt->timeout, wdt->pretimeout);
+	}
+
+	/* update the timeout settings if requested by module parameters */
+	if (timeout > 0)
+		wdt->timeout = timeout;
+	if (pretimeout >= 0)
+		wdt->pretimeout = pretimeout;
+
+	dev_info(wdt->pld->dev, "watchdog will be set on (re)start\n"
+		 "new settings: %d s timeout and %d s pretimeout\n",
+		 wdt->timeout, wdt->pretimeout);
+
+	wdt->ident.options = WDIOF_KEEPALIVEPING;
+	if ((wdt->timeout_stage) && !(status & KEMPLD_WDT_CFG_GLOBAL_LOCK))
+		wdt->ident.options |= WDIOF_SETTIMEOUT;
+	if (wdt->pretimeout_stage)
+		wdt->ident.options |= WDIOF_PRETIMEOUT;
+	if (!nowayout)
+		wdt->ident.options |= WDIOF_MAGICCLOSE;
+
+	kempld_wdt = wdt;
+
+	ret = misc_register(&kempld_wdt_miscdev);
+	if (ret)
+		goto err_misc_register;
+
+	dev_info(wdt->pld->dev,
+		 "%d stage watchdog initialized, pretimeout %ssupported\n",
+		 wdt->stages, wdt->pretimeout_stage ? "" : "not ");
+
+	return 0;
+
+err_probe_stages:
+err_misc_register:
+	kfree(kempld_wdt);
+	kempld_wdt = NULL;
+err_alloc_dev_data:
+	return ret;
+}
+
+static void kempld_wdt_shutdown(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	if (wdt != NULL) {
+		if (!nowayout)
+			kempld_wdt_stop(wdt);
+		else
+			kempld_wdt_keepalive(wdt);
+	}
+}
+
+static int kempld_wdt_remove(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+
+	BUG_ON(wdt != kempld_wdt);
+
+	/* stop or at least keepalive the watchdog before we leave */
+	kempld_wdt_shutdown(pdev);
+
+	misc_deregister(&kempld_wdt_miscdev);
+
+	kempld_wdt_release_stages(wdt);
+
+	kfree(wdt);
+	kempld_wdt = NULL;
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int wdt_pm_status_store;
+
+/* Disable watchdog if it is active during suspend */
+static int kempld_wdt_suspend(struct platform_device *pdev,
+				pm_message_t message)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+	struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
+
+	kempld_get_mutex_set_index(pld, KEMPLD_WDT_CFG);
+	wdt_pm_status_store = kempld_read8(pld, KEMPLD_WDT_CFG);
+	kempld_release_mutex(pld);
+
+	kempld_wdt_update_timeouts(wdt);
+
+	if (wdt_pm_status_store & KEMPLD_WDT_CFG_ENABLE)
+		kempld_wdt_shutdown(pdev);
+
+	return 0;
+}
+
+/* Enable watchdog and configure it if necessary */
+static int kempld_wdt_resume(struct platform_device *pdev)
+{
+	struct kempld_watchdog_data *wdt = platform_get_drvdata(pdev);
+	int ret;
+
+	/* if watchdog was stopped before suspend be sure it gets disabled
+	 * again, for the case BIOS has enabled it during resume */
+	if (wdt_pm_status_store & KEMPLD_WDT_CFG_ENABLE) {
+		ret = kempld_wdt_settimeout(wdt);
+		if (ret)
+			goto err_enable_wdt;
+		ret = kempld_wdt_start(wdt);
+		if (ret)
+			goto err_enable_wdt;
+
+		dev_info(wdt->pld->dev, "Resuming watchdog operation:\n"
+			 "%d s timeout and %d s pretimeout\n", wdt->timeout,
+			 wdt->pretimeout);
+	} else
+		kempld_wdt_shutdown(pdev);
+
+	return 0;
+
+err_enable_wdt:
+	dev_err(wdt->pld->dev,
+		"Failed to reenable the watchdog timer after resume!\n");
+
+	return ret;
+}
+#else
+#define kempld_wdt_suspend	NULL
+#define kempld_wdt_resume	NULL
+#endif
+
+static struct platform_driver kempld_wdt_driver = {
+	.driver = {
+		.name = "kempld-wdt",
+		.owner = THIS_MODULE,
+	},
+	.probe = kempld_wdt_probe,
+	.remove = kempld_wdt_remove,
+	.shutdown = kempld_wdt_shutdown,
+	.suspend = kempld_wdt_suspend,
+	.resume = kempld_wdt_resume,
+};
+
+static int __init kempld_wdt_init(void)
+{
+	return platform_driver_register(&kempld_wdt_driver);
+}
+
+static void __exit kempld_wdt_exit(void)
+{
+	platform_driver_unregister(&kempld_wdt_driver);
+}
+
+module_init(kempld_wdt_init);
+module_exit(kempld_wdt_exit);
+
+MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
+MODULE_AUTHOR("Michael Brunner <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
drivers/watchdog/kempld_wdt.h new file +75 -0
new file mode 100644
index 0000000..80f68f6
@@ -0,0 +1,75 @@
+/*
+ *  kempld_wdt.h - Kontron PLD watchdog driver definitions
+ *
+ *  Copyright (c) 2010-2012 Kontron Europe GmbH
+ *  Author: Michael Brunner <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License 2 as published
+ *  by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _KEMPLD_WDT_H_
+#define _KEMPLD_WDT_H_
+
+/* watchdog register definitions */
+#define KEMPLD_WDT_KICK			0x16
+#define KEMPLD_WDT_CFG			0x17
+#define		KEMPLD_WDT_CFG_STAGE_TIMEOUT_OCCURED(x)	(1<<x)
+#define		KEMPLD_WDT_CFG_ENABLE_LOCK		0x8
+#define		KEMPLD_WDT_CFG_ENABLE			0x10
+#define		KEMPLD_WDT_CFG_AUTO_RELOAD		0x40
+#define		KEMPLD_WDT_CFG_GLOBAL_LOCK		0x80
+#define KEMPLD_WDT_STAGE_CFG(x)		(0x18+x)
+#define		KEMPLD_WDT_STAGE_CFG_ACTION_MASK	0x7
+#define		KEMPLD_WDT_STAGE_CFG_GET_ACTION(x)	(x & 0x7)
+#define		KEMPLD_WDT_STAGE_CFG_ASSERT		(1<<3)
+#define		KEMPLD_WDT_STAGE_CFG_PRESCALER_MASK	0x30
+#define		KEMPLD_WDT_STAGE_CFG_GET_PRESCALER(x)	((x & 0x30)>>4)
+#define		KEMPLD_WDT_STAGE_CFG_SET_PRESCALER(x)	((x & 0x30)<<4)
+#define KEMPLD_WDT_STAGE_TIMEOUT(x)	(0x1b+x*4)
+#define KEMPLD_WDT_MAX_STAGES		3
+
+#define	KEMPLD_WDT_ACTION_NONE		0x0
+#define	KEMPLD_WDT_ACTION_RESET		0x1
+#define	KEMPLD_WDT_ACTION_NMI		0x2
+#define	KEMPLD_WDT_ACTION_SMI		0x3
+#define	KEMPLD_WDT_ACTION_SCI		0x4
+#define	KEMPLD_WDT_ACTION_DELAY		0x5
+
+#define	KEMPLD_WDT_PRESCALER_21BIT	0x0
+#define	KEMPLD_WDT_PRESCALER_17BIT	0x1
+#define	KEMPLD_WDT_PRESCALER_12BIT	0x2
+
+const int kempld_prescaler_bits[] = { 21, 17, 12 };
+#define KEMPLD_PRESCALER(x)	(0xffffffff>>(32-kempld_prescaler_bits[x]))
+
+
+struct kempld_watchdog_stage {
+	int	num;
+	u32	timeout_mask;
+};
+
+struct kempld_watchdog_data {
+	int				timeout;
+	int				pretimeout;
+	unsigned long			is_open;
+	unsigned long			expect_close;
+	int				stages;
+	struct kempld_watchdog_stage	*timeout_stage;
+	struct kempld_watchdog_stage	*pretimeout_stage;
+	struct kempld_device_data	*pld;
+	struct watchdog_info		ident;
+	struct kempld_watchdog_stage	*stage[KEMPLD_WDT_MAX_STAGES];
+};
+
+#endif /* _KEMPLD_WDT_H_ */
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#14 lore raw

Re: [PATCH 4/4] watchdog: Kontron PLD watchdog timer

Hi Kevin,

Quoted 10 lines · Guenter Roeck
Quoted 9 lines · Kevin Strasser
Quoted 7 lines

From: Michael Brunner <[email protected]>

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

Quoted 6 lines
Quoted 4 lines

Personally I would prefer two separate patches for the two drivers, and to have the drivers converted to the watchdog infrastructure. Wim's call, of course.

Thanks for the feedback. I'm happy to do both if Wim thinks it's necessary.

Yes, 2 patches with conversion to the new watchdog infrastructure please.

Kind regards,
Wim.

#15 lore raw
In reply to Kevin Strasser
Quoted 4 lines · Kevin Strasser
drivers/mfd/kempld-core.c new file +2 -0
+#include <linux/kernel.h>
+#include <linux/module.h>

drivers/mfd/kempld-core.c line 32

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/dmi.h>
+#include <linux/slab.h>
+#include <linux/io.h>
+#include <linux/sched.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/kempld.h>

I seriously doubt, that all these includes are required.

Quoted 1 line

+#define KEMPLD_MAINTAIN_EFT_COMPATIBILITY 1

What's the point of this define ?

drivers/mfd/kempld-core.c line 42

Quoted 7 lines
drivers/mfd/kempld-core.c new file +7 -0
+static int kempld_platform_device_register(const struct dmi_system_id *id);
+static int kempld_get_mutex_set_index_generic(struct kempld_device_data *pld,
+					u8 index, unsigned int timeout);
+static void kempld_release_mutex_generic(struct kempld_device_data *pld);
+
+static int kempld_get_info(struct kempld_device_data *pld);
+static int kempld_get_info_NOW1(struct kempld_device_data *pld);

Can you please get rid of the CamelCase ?

drivers/mfd/kempld-core.c line 46

Quoted 4 lines
drivers/mfd/kempld-core.c new file +4 -0
+static int kempld_get_info_generic(struct kempld_device_data *pld);
+static int kempld_get_features(struct kempld_device_data *pld);
+static int kempld_register_cells_generic(struct kempld_device_data *pld);
+static int kempld_register_cells_NOW1(struct kempld_device_data *pld);

Can you please reshuffle the code, so that we can get rid of all these forward declarations ?

drivers/mfd/kempld-core.c line 51

Quoted 4 lines
drivers/mfd/kempld-core.c new file +4 -0
+#define MAX_IDENT_LEN 4
+static char force_ident[MAX_IDENT_LEN + 1] = "";
+module_param_string(force_ident, force_ident, sizeof(force_ident), 0);
+MODULE_PARM_DESC(force_ident, "Force detection of specific product");

Please change this to something which is ad hoc understandable w/o reading the code. e.g. "kempld_device_id".

Quoted 2 lines

+/* this option is only here for debugging and should never be needed in
+ * production environments */

/*
* Please use standard multiline comment style and proper
* sentences starting with a capital letter
*/

drivers/mfd/kempld-core.c line 57

Quoted 3 lines
drivers/mfd/kempld-core.c new file +3 -0
+static bool force_unlock;
+module_param(force_unlock, bool, 0);
+MODULE_PARM_DESC(force_unlock, "Force breaking the semaphore on driver load");

Is it really necessary to carry this in the kernel? If yes, then please put it under

#ifdef DEBUG

We really can do without random debug code. And the comment should be a little more elaborate about what the heck this is doing. "Force breaking the semaphore ..." makes me shudder, w/o reading the code which uses this.

Quoted 9 lines
drivers/mfd/kempld-core.c new file +9 -0
+/**
+ * kempld_read8 - read 8 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * This function reads an 8 bit register of the PLD and returns its value.
+ *
+ * In order for this function to work correctly, kempld_try_get_mutex_set_index
+ * or kempld_get_mutex_set_index has to be called before calling the function

drivers/mfd/kempld-core.c line 333

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+ * to acquire the mutex. Afterwards the mutex has to be released with
+ * kempld_release_mutex.
+ */
+u8 kempld_read8(struct kempld_device_data *pld, u8 index)
+{
+	kempld_set_index(pld, index);
+
+	return ioread8(pld->io_data);
+}
+EXPORT_SYMBOL(kempld_read8);

EXPORT_SYMBOL_GPL please. All over the place.

Quoted 5 lines
drivers/mfd/kempld-core.c new file +5 -0
+/**
+ * kempld_read16 - read 16 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *

drivers/mfd/kempld-core.c line 370

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+ * This function reads a 16 bit register of the PLD and returns its value.
+ *
+ * In order for this function to work correctly, kempld_try_get_mutex_set_index
+ * or kempld_get_mutex_set_index has to be called before calling the function
+ * to acquire the mutex. Afterwards the mutex has to be released with
+ * kempld_release_mutex.
+ */
+u16 kempld_read16(struct kempld_device_data *pld, u8 index)
+{
+	BUG_ON(index+1 < index);

Yuck. What kind of problem are you catching here? Just the corner case that someone hands in 0xff as index?

I'd rather assume that you tried to catch the case where someone hand in an index with BIT0 set. So that would be:

	BUG_ON(index & 0x01);

Aside of that, do you really want to kill the machine here? A WARN_ON[_ONCE] would be more appropriate.

	WARN_ON_ONCE(index & 0x01);

drivers/mfd/kempld-core.c line 372

Quoted 1 line
drivers/mfd/kempld-core.c new file +1 -0
+	return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

index + 1)
Please

drivers/mfd/kempld-core.c line 391

Quoted 3 lines
drivers/mfd/kempld-core.c new file +3 -0
+void kempld_write16(struct kempld_device_data *pld, u8 index, u16 data)
+{
+	BUG_ON(index+1 < index);

See above. And all other functions which use that silly BUG_ON as well.

Quoted 6 lines

+/**
+ * kempld_set_index - change the current register index of the PLD
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * This function changes the register index of the PLD.

That's really important information after reading the above function descriptor...

Quoted 2 lines

+ *
+ * If the PLD mutex has been acquired the whole time and the desired index is

-ENOPARSE

drivers/mfd/kempld-core.c line 459

Quoted 12 lines

+ * already set there might be no actual hardware access done in this function.
+ *
+ * In order for this function to work correctly, kempld_try_get_mutex_set_index
+ * or kempld_get_mutex_set_index has to be called before calling the function
+ * to acquire the mutex. Afterwards the mutex has to be released with
+ * kempld_release_mutex.
+ */

drivers/mfd/kempld-core.c new file +5 -0
+void kempld_set_index(struct kempld_device_data *pld, u8 index)
+{
+	struct kempld_platform_data *pdata = pld->dev->platform_data;
+
+	BUG_ON(pld->have_mutex == 0);

What the heck is this? Does pld->have_mutex indicate that there is a mutex associated with that PLD or what?

If you want to check whether the caller has acquired the mutex which is always associated to that PLD then you should perhaps read the mutex documentation^Wcode and figure out how to do that correctly.

Quoted 4 lines
drivers/mfd/kempld-core.c new file +4 -0
+	if (pld->last_index != index || pdata->force_index_write) {
+		iowrite8(index, pld->io_index);
+		pld->last_index = index;
+	}

drivers/mfd/kempld-core.c line 474

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+}
+EXPORT_SYMBOL(kempld_set_index);
+
+static int kempld_get_mutex_set_index_generic(struct kempld_device_data *pld,
+					u8 index, unsigned int timeout)
+{
+	struct kempld_platform_data *pdata = pld->dev->platform_data;
+	int data;
+
+	if (!pld->have_mutex) {

So you use a boolean value to maintain concurrency? OMG. So any task which calls this code and observes pld->have_mutex != 0 can proceed. Brilliant design.

drivers/mfd/kempld-core.c line 478

Quoted 4 lines
drivers/mfd/kempld-core.c new file +4 -0
+		unsigned long loop_timeout = jiffies + (HZ*timeout)/1000;
+
+		while ((((data = ioread8(pld->io_index)) & KEMPLD_MUTEX_KEY)
+				== KEMPLD_MUTEX_KEY)) {

So there is a hardware concurrency control, which has a single KEY for everyone? At least that's what I read from that code.

drivers/mfd/kempld-core.c line 479

Quoted 1 line
drivers/mfd/kempld-core.c new file +1 -0
+			if (timeout != KEMPLD_MUTEX_NOTIMEOUT)

WTF are you introducing another constant fpor INFINITE timeout?

drivers/mfd/kempld-core.c line 484

Quoted 5 lines
drivers/mfd/kempld-core.c new file +5 -0
+				if (!time_before(jiffies, loop_timeout))
+					return -ETIMEDOUT;
+
+			/* we will have to wait until mutex is free */
+			spin_unlock_irqrestore(&pld->lock, pld->lock_flags);

Where the heck is documented that this function needs to be called with pld->lock held and interrupts disabled?

drivers/mfd/kempld-core.c line 487

Quoted 2 lines
drivers/mfd/kempld-core.c new file +2 -0
+			/* give other tasks a chance to release the mutex */
+			schedule_timeout_interruptible(0);

Creative avoidance of yield? Not that yield is a good idea, but this is f*cking absurd.

drivers/mfd/kempld-core.c

Quoted 1 line
drivers/mfd/kempld-core.c new file +1 -0
+			spin_lock_irqsave(&pld->lock, pld->lock_flags);

What's the point of this exercise?

drivers/mfd/kempld-core.c line 494

Quoted 5 lines
drivers/mfd/kempld-core.c new file +5 -0
+		}
+	} else
+		data = ioread8(pld->io_index);
+
+	if (KEMPLD_MAINTAIN_EFT_COMPATIBILITY

Evaluates to TRUE unconditionally. What's the point ?

Quoted 12 lines
drivers/mfd/kempld-core.c new file +12 -0
+		 || ((pld->last_index != (data & ~KEMPLD_MUTEX_KEY))
+		 || pdata->force_index_write)) {
+		iowrite8(index, pld->io_index);
+		pld->last_index = index;
+	}
+
+	return 0;
+}
+
+/**
+ * kempld_get_mutex_set_index - acquire the PLD mutex and set register index
+ * @pld: kempld_device_data structure describing the PLD

drivers/mfd/kempld-core.c line 516

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+ * @index: register index on the chip
+ *
+ * This function acquires a PLD spinlock and the PLD mutex, additionally it
+ * also changes the register index. In order to do no unnecessary write cycles
+ * the index provided to this function should be the same that will be used
+ * with the first PLD access that is done afterwards.
+ *
+ * The function will block for at least 10 seconds if the mutex can't be
+ * acquired and issue a warning in that case. In order to not lock the device,
+ * the function assumes that the mutex has been acquired in that case.

What the heck? We do not do that in the kernel. Either you get your locking correct, or you don't. There is no point of 10 seconds timeout to get a "mutex" which is actually not a mutex. You call that code with the spinlock held and irqs disabled. Pretty much not mutex semantics.

Quoted 7 lines

+ * To release the spinlock and mutex kempld_release_mutex can be called.
+ * The spinlock and mutex should only be kept for a few milliseconds, in order
+ * to give other drivers a chance to work with the PLD.
+ */

drivers/mfd/kempld-core.c new file +3 -0
+inline void kempld_get_mutex_set_index(struct kempld_device_data *pld,
+					u8 index)
+{

drivers/mfd/kempld-core.c line 534

Quoted 10 lines
drivers/mfd/kempld-core.c new file +10 -0
+	struct kempld_platform_data *pdata = pld->dev->platform_data;
+
+	spin_lock_irqsave(&pld->lock, pld->lock_flags);
+
+	if (pdata->get_mutex_set_index) {
+		/* use a long timeout here as this shouldn't fail */
+		if (pdata->get_mutex_set_index(pld, index, 10000))
+			dev_warn(pld->dev, "semaphore broken!\n");
+
+		pld->have_mutex = 1;

Now I really start to go berserk. What's the point of this timeout thing and what is the point of pdata->get_mutex_set_index ?

Either you have the need for a hardware controlled serialization or you do not. I have the feeling that your understanding of concurrency control is close to ZERO.

I'm stopping the review here as this is just a f*cking nightmare and going further down the patch is just a pointless exercise.

NAK to the whole patch series.

Thanks,

tglx

maintainer
#16 lore raw

On Sat, Apr 13, 2013 at 10:38:07PM +0200, Thomas Gleixner wrote:

[ ... ]

Quoted 6 lines

drivers/mfd/kempld-core.c line 372

Quoted 2 lines
drivers/mfd/kempld-core.c new file +1 -0
+	return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

index + 1)
Please

Wondering .... why does checkpatch not report those ? I just reviewed another driver with the same kind of problem, and checkpatch is just as silent. There seems to be a whole class of expressions where it does not complain about missing spaces.

Joe, any idea ?

Thanks,
Guenter

#17 lore raw
Quoted 5 lines · Guenter Roeck
Quoted 3 lines · Thomas Gleixner
Quoted 1 line

+ return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

                                                        index + 1)

Please

Wondering .... why does checkpatch not report those ?

because checkpatch doesn't care about spacing around arithmetic as long as it's consistent.

Documentation/CodingStyle doesn't say anything about it either.

Look around checkpatch line 2654

                                } elsif ($op eq '<<' or $op eq '>>' or
                                         $op eq '&' or $op eq '^' or $op eq '|' or
                                         $op eq '+' or $op eq '-' or
                                         $op eq '*' or $op eq '/' or
                                         $op eq '%')
                                {
                                        if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
                                                ERROR("SPACING",
                                                      "need consistent spacing around '$op' $at\n" .
                                                        $hereptr);
                                        }
maintainer
#18 lore raw
Quoted 5 lines · Joe Perches
Quoted 4 lines · Guenter Roeck
Quoted 3 lines · Thomas Gleixner
Quoted 1 line

+ return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

                                                        index + 1)

Please

Quoted 8 lines
Quoted 1 line

Wondering .... why does checkpatch not report those ?

because checkpatch doesn't care about spacing around arithmetic as long as it's consistent.

Documentation/CodingStyle doesn't say anything about it either.

Hi Joe,

"Use one space around (on each side of) most binary and ternary operators"

doesn't apply, then ? When does it apply ? I always thought it would apply to cases such as the above.

Thanks,
Guenter

#19 lore raw
Quoted 14 lines · Guenter Roeck
Quoted 5 lines · Joe Perches
Quoted 4 lines · Guenter Roeck
Quoted 3 lines · Thomas Gleixner
Quoted 1 line

+ return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

                                                        index + 1)

Please

Quoted 8 lines
Quoted 1 line

Wondering .... why does checkpatch not report those ?

because checkpatch doesn't care about spacing around arithmetic as long as it's consistent.

Documentation/CodingStyle doesn't say anything about it either.

Quoted 6 lines

Hi Joe,

"Use one space around (on each side of) most binary and ternary operators"

doesn't apply, then ? When does it apply ? I always thought it would apply to cases such as the above.

There's a _lot_ of code that doesn't follow the "single space around binary operators" style, it's contentious, and was determined when Andy did the original checkpatch implementation to not be a valuable addition or worth the complaint pain.

maintainer
#20 lore raw
Quoted 19 lines · Joe Perches
Quoted 18 lines · Guenter Roeck
Quoted 5 lines · Joe Perches
Quoted 4 lines · Guenter Roeck
Quoted 3 lines · Thomas Gleixner
Quoted 1 line

+ return kempld_read8(pld, index) | kempld_read8(pld, index+1) << 8;

                                                        index + 1)

Please

Quoted 8 lines
Quoted 1 line

Wondering .... why does checkpatch not report those ?

because checkpatch doesn't care about spacing around arithmetic as long as it's consistent.

Documentation/CodingStyle doesn't say anything about it either.

Hi Joe,

"Use one space around (on each side of) most binary and ternary operators"

Quoted 9 lines
Quoted 2 lines

doesn't apply, then ? When does it apply ? I always thought it would apply to cases such as the above.

There's a _lot_ of code that doesn't follow the "single space around binary operators" style, it's contentious, and was determined when Andy did the original checkpatch implementation to not be a valuable addition or worth the complaint pain.

Looks like it is contentious either way.

Thanks a lot for the clarification.

Guenter

#21 lore raw

v2 cover Kontron PLD drivers

In reply to Kevin Strasser

mfd changes since v1:
- Use a mutex instead of spinlock
- Poll for hardware mutex without timeout
- Restructure mfd cell structs, only call mfd_add_devices once
- Drop pointless BUG_ONs
- EXPORT_SYMBOL -> EXPORT_SYMBOL_GPL
- kempld_*_mutex_set_index -> kempld_*_mutex
- Removed kempld_try_get_mutex
- Drop last_index
- Remove EFT code
- Drop unused includes
- Use devm_ioport_map
- Restructure to get rid of forward function prototypes
- Renamed module parameter force_ident -> force_device_id
- Dropped force_unlock module parameter
- Formatting fixes

i2c changes since v1:
- Always disable bus during suspend,
- Detect was_active in probe
- Remove i2c-kempld.h
- Clean up register definitions
- Use the correct device for printing
- Set default bus frequency to 100kHz
- Drop irq support
- Drop now1 driver
- Remove i2c-mux code
- Clean up includes
- Use devm_kzalloc
- Formatting fixes

gpio changes since v1:
- Change label from kempld-gpio -> gpio-kempld
- Drop unnecessary include seq_file.h
- Register and value parameters to kempld_get_bit and kempld_bitop are now u8
- Status variable in kempld_bitop is now u8
- Fix kempld_gpio_pincount(). hweight16 doesn't work, but __ffs does
- Drop interrupt support
- Drop gpio-kempld.h
- Use helper functions for bit and read operations
- Use generic DEBUG_FS code
- Use devm_kzalloc
- Get gpio_base from platform data
- Drop all module parameters
- Use module_platform_driver
- Drop unused includes
- Include device.h instead of slab.h for devm_kzalloc
- Some cleanup here and there

watchdog changes since v1:
- Use watchdog framework
- Allocate stages statically
- Drop now1 driver
- Use devm_kzalloc
- Change default timeout to 30 seconds
- Drop unused includes
- General formatting cleanup

Kevin Strasser (4):
mfd: Kontron PLD mfd driver
i2c: Kontron PLD i2c bus driver
gpio: Kontron PLD gpio driver
watchdog: Kontron PLD watchdog timer driver

 drivers/gpio/Kconfig            |   12 +
 drivers/gpio/Makefile           |    1 +
 drivers/gpio/gpio-kempld.c      |  225 ++++++++++++++
 drivers/i2c/busses/Kconfig      |   10 +
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  410 +++++++++++++++++++++++++
 drivers/mfd/Kconfig             |   21 ++
 drivers/mfd/Makefile            |    1 +
 drivers/mfd/kempld-core.c       |  642 +++++++++++++++++++++++++++++++++++++++
 drivers/watchdog/Kconfig        |   11 +
 drivers/watchdog/Makefile       |    1 +
 drivers/watchdog/kempld_wdt.c   |  580 +++++++++++++++++++++++++++++++++++
 include/linux/mfd/kempld.h      |  125 ++++++++
 13 files changed, 2040 insertions(+)
 create mode 100644 drivers/gpio/gpio-kempld.c
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
 create mode 100644 drivers/mfd/kempld-core.c
 create mode 100644 drivers/watchdog/kempld_wdt.c
 create mode 100644 include/linux/mfd/kempld.h
Signature · 1 line
1.7.9.5
#22 lore raw

PATCH v2 1/4 mfd: Kontron PLD mfd driver

Add core MFD driver for the on-board PLD found on some Kontron embedded modules. The PLD device may provide functions like watchdog, GPIO, UART and I2C bus.

The following modules are supported:
* COMe-bIP#
* COMe-bPC2 (ETXexpress-PC)
* COMe-bSC# (ETXexpress-SC T#)
* COMe-cCT6
* COMe-cDC2 (microETXexpress-DC)
* COMe-cPC2 (microETXexpress-PC)
* COMe-mCT10
* ETX-OH

---
 drivers/mfd/Kconfig        |   21 ++
 drivers/mfd/Makefile       |    1 +
 drivers/mfd/kempld-core.c  |  642 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/kempld.h |  125 +++++++++
 4 files changed, 789 insertions(+)
 create mode 100644 drivers/mfd/kempld-core.c
 create mode 100644 include/linux/mfd/kempld.h
drivers/mfd/Kconfig +21 -0
index c346941..eea3737 100644
@@ -912,6 +912,27 @@ config MFD_TIMBERDALE
 	The timberdale FPGA can be found on the Intel Atom development board
 	for in-vehicle infontainment, called Russellville.
 
+config MFD_KEMPLD
+	tristate "Support for Kontron module PLD device"
+	select MFD_CORE
+	help
+	  This is the core driver for the PLD (Programmable Logic Device) found
+	  on some Kontron ETX and COMexpress (ETXexpress) modules. The PLD
+	  device may provide functions like watchdog, GPIO, UART and I2C bus.
+
+	  The following modules are supported:
+		* COMe-bIP#
+		* COMe-bPC2 (ETXexpress-PC)
+		* COMe-bSC# (ETXexpress-SC T#)
+		* COMe-cCT6
+		* COMe-cDC2 (microETXexpress-DC)
+		* COMe-cPC2 (microETXexpress-PC)
+		* COMe-mCT10
+		* ETX-OH
+
+	  This driver can also be built as a module. If so, the module
+	  will be called kempld-core.
+
 config LPC_SCH
 	tristate "Intel SCH LPC"
 	depends on PCI && GENERIC_HARDIRQS
drivers/mfd/Makefile +1 -0
index b90409c..a2f6a9f 100644
@@ -123,6 +123,7 @@ obj-$(CONFIG_MFD_DB8500_PRCMU)	+= db8500-prcmu.o
 obj-$(CONFIG_AB8500_CORE)	+= ab8500-core.o ab8500-sysctrl.o
 obj-$(CONFIG_MFD_TIMBERDALE)    += timberdale.o
 obj-$(CONFIG_PMIC_ADP5520)	+= adp5520.o
+obj-$(CONFIG_MFD_KEMPLD)	+= kempld-core.o
 obj-$(CONFIG_LPC_SCH)		+= lpc_sch.o
 obj-$(CONFIG_LPC_ICH)		+= lpc_ich.o
 obj-$(CONFIG_MFD_RDC321X)	+= rdc321x-southbridge.o
drivers/mfd/kempld-core.c new file +642 -0

642 lines not on this page · raw message

include/linux/mfd/kempld.h new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#23 lore raw
Quoted 18 lines · Kevin Strasser

Add core MFD driver for the on-board PLD found on some Kontron embedded modules. The PLD device may provide functions like watchdog, GPIO, UART and I2C bus.

The following modules are supported:
* COMe-bIP#
* COMe-bPC2 (ETXexpress-PC)
* COMe-bSC# (ETXexpress-SC T#)
* COMe-cCT6
* COMe-cDC2 (microETXexpress-DC)
* COMe-cPC2 (microETXexpress-PC)
* COMe-mCT10
* ETX-OH

(...)

Quoted 84 lines
drivers/mfd/kempld-core.c new file +84 -0
+/**
+ * kempld_read8 - read 8 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u8 kempld_read8(struct kempld_device_data *pld, u8 index)
+{
+       iowrite8(index, pld->io_index);
+       return ioread8(pld->io_data);
+}
+EXPORT_SYMBOL_GPL(kempld_read8);
+
+/**
+ * kempld_write8 - write 8 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write8(struct kempld_device_data *pld, u8 index, u8 data)
+{
+       iowrite8(index, pld->io_index);
+       iowrite8(data, pld->io_data);
+}
+EXPORT_SYMBOL_GPL(kempld_write8);
+
+/**
+ * kempld_read16 - read 16 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u16 kempld_read16(struct kempld_device_data *pld, u8 index)
+{
+       return kempld_read8(pld, index) | kempld_read8(pld, index + 1) << 8;
+}
+EXPORT_SYMBOL_GPL(kempld_read16);
+
+/**
+ * kempld_write16 - write 16 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write16(struct kempld_device_data *pld, u8 index, u16 data)
+{
+       kempld_write8(pld, index, (u8)data);
+       kempld_write8(pld, index + 1, (u8)(data >> 8));
+}
+EXPORT_SYMBOL_GPL(kempld_write16);
+
+/**
+ * kempld_read32 - read 32 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u32 kempld_read32(struct kempld_device_data *pld, u8 index)
+{
+       return kempld_read16(pld, index) | kempld_read16(pld, index + 2) << 16;
+}
+EXPORT_SYMBOL_GPL(kempld_read32);
+
+/**
+ * kempld_write32 - write 32 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write32(struct kempld_device_data *pld, u8 index, u32 data)
+{
+       kempld_write16(pld, index, (u16)data);
+       kempld_write16(pld, index + 2, (u16)(data >> 16));
+}
+EXPORT_SYMBOL_GPL(kempld_write32);

(...)

include/linux/mfd/kempld.h line 123

Quoted 6 lines
include/linux/mfd/kempld.h new file +6 -0
+extern u8 kempld_read8(struct kempld_device_data *pld, u8 index);
+extern void kempld_write8(struct kempld_device_data *pld, u8 index, u8 data);
+extern u16 kempld_read16(struct kempld_device_data *pld, u8 index);
+extern void kempld_write16(struct kempld_device_data *pld, u8 index, u16 data);
+extern u32 kempld_read32(struct kempld_device_data *pld, u8 index);
+extern void kempld_write32(struct kempld_device_data *pld, u8 index, u32 data);

Not really my business, but I think if I was to implement this inter-module API I would use regmap for this read/write marshalling right here.

Yours,
Linus Walleij

#24 lore raw

Hi Linus,

Quoted 107 lines · Linus Walleij
Quoted 18 lines · Kevin Strasser

Add core MFD driver for the on-board PLD found on some Kontron embedded modules. The PLD device may provide functions like watchdog, GPIO, UART and I2C bus.

The following modules are supported:
* COMe-bIP#
* COMe-bPC2 (ETXexpress-PC)
* COMe-bSC# (ETXexpress-SC T#)
* COMe-cCT6
* COMe-cDC2 (microETXexpress-DC)
* COMe-cPC2 (microETXexpress-PC)
* COMe-mCT10
* ETX-OH

(...)

Quoted 84 lines
drivers/mfd/kempld-core.c new file +84 -0
+/**
+ * kempld_read8 - read 8 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u8 kempld_read8(struct kempld_device_data *pld, u8 index)
+{
+       iowrite8(index, pld->io_index);
+       return ioread8(pld->io_data);
+}
+EXPORT_SYMBOL_GPL(kempld_read8);
+
+/**
+ * kempld_write8 - write 8 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write8(struct kempld_device_data *pld, u8 index, u8 data)
+{
+       iowrite8(index, pld->io_index);
+       iowrite8(data, pld->io_data);
+}
+EXPORT_SYMBOL_GPL(kempld_write8);
+
+/**
+ * kempld_read16 - read 16 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u16 kempld_read16(struct kempld_device_data *pld, u8 index)
+{
+       return kempld_read8(pld, index) | kempld_read8(pld, index + 1) << 8;
+}
+EXPORT_SYMBOL_GPL(kempld_read16);
+
+/**
+ * kempld_write16 - write 16 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write16(struct kempld_device_data *pld, u8 index, u16 data)
+{
+       kempld_write8(pld, index, (u8)data);
+       kempld_write8(pld, index + 1, (u8)(data >> 8));
+}
+EXPORT_SYMBOL_GPL(kempld_write16);
+
+/**
+ * kempld_read32 - read 32 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+u32 kempld_read32(struct kempld_device_data *pld, u8 index)
+{
+       return kempld_read16(pld, index) | kempld_read16(pld, index + 2) << 16;
+}
+EXPORT_SYMBOL_GPL(kempld_read32);
+
+/**
+ * kempld_write32 - write 32 bit register
+ * @pld: kempld_device_data structure describing the PLD
+ * @index: register index on the chip
+ * @data: new register value
+ *
+ * kempld_get_mutex must be called prior to calling this function.
+ */
+void kempld_write32(struct kempld_device_data *pld, u8 index, u32 data)
+{
+       kempld_write16(pld, index, (u16)data);
+       kempld_write16(pld, index + 2, (u16)(data >> 16));
+}
+EXPORT_SYMBOL_GPL(kempld_write32);

(...)

Quoted 10 lines

include/linux/mfd/kempld.h line 123

Quoted 6 lines
include/linux/mfd/kempld.h new file +6 -0
+extern u8 kempld_read8(struct kempld_device_data *pld, u8 index);
+extern void kempld_write8(struct kempld_device_data *pld, u8 index, u8 data);
+extern u16 kempld_read16(struct kempld_device_data *pld, u8 index);
+extern void kempld_write16(struct kempld_device_data *pld, u8 index, u16 data);
+extern u32 kempld_read32(struct kempld_device_data *pld, u8 index);
+extern void kempld_write32(struct kempld_device_data *pld, u8 index, u32 data);

Not really my business, but I think if I was to implement this inter-module API I would use regmap for this read/write marshalling right here.

I thought about this one and was under the impression that the regmap API would not let us implement this kind of stuff. I don't see how the reg_read and reg_write hooks would allow us to implement that, but I may be missing something here. Maybe Mark can provide some more input.

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/
#25 lore raw
Quoted 10 lines · Samuel Ortiz
Quoted 3 lines · Linus Walleij

Not really my business, but I think if I was to implement this inter-module API I would use regmap for this read/write marshalling right here.

I thought about this one and was under the impression that the regmap API would not let us implement this kind of stuff. I don't see how the reg_read and reg_write hooks would allow us to implement that, but I may be missing something here. Maybe Mark can provide some more input.

You should be able to use reg_read() and reg_write() but you're right it would be a bit hoop jumping. Really the code looks like a new generic bus type for ioread()/iowrite() users though - I guess there will be other similar devices connected in the same way and they probably don't have radically different ideas about registers.

Acked-by on v2 1/4
#26 lore raw
In reply to Kevin Strasser

On Tue, 18 Jun 2013, Kevin Strasser wrote:

This patch set looks reasonable now, except a formal issue.

Quoted 2 lines

This is wrong, as it says:

Kevin authored the code, sent it to Michael and Michael sent it to LKML.

The original code was authored by Michael. This version was authored by Kevin based on Michaels code.

So either you keep Michael as the author, then you want

From: Michael Brunner <[email protected]>

<patch description/>

or you decide that it's a major rewrite by you, then you want:

This patch is based on the original version authored by Michael Brunner <[email protected]>.

Or

Originally-From: Michael Brunner <[email protected]>.

Other than that.

#27 lore raw
Quoted 30 lines · Thomas Gleixner

On Tue, 18 Jun 2013, Kevin Strasser wrote:

This patch set looks reasonable now, except a formal issue.

Quoted 2 lines

This is wrong, as it says:

Kevin authored the code, sent it to Michael and Michael sent it to LKML.

The original code was authored by Michael. This version was authored by Kevin based on Michaels code.

So either you keep Michael as the author, then you want

From: Michael Brunner <[email protected]>

<patch description/>

or you decide that it's a major rewrite by you, then you want:

This patch is based on the original version authored by Michael Brunner <[email protected]>.

Quoted 6 lines

Or

Originally-From: Michael Brunner <[email protected]>.

Ok, I think this will make sense for all the patches except gpio, which is a major rewrite by Guenter.

So maybe this would be appropriate:

From: Guenter Roeck <[email protected]>

<patch description>

Originally-From: Michael Brunner <[email protected]>

...

Thanks,
Kevin

Quoted 4 lines

Other than that.

maintainer
#28 lore raw
Quoted 43 lines · Kevin Strasser
Quoted 30 lines · Thomas Gleixner

On Tue, 18 Jun 2013, Kevin Strasser wrote:

This patch set looks reasonable now, except a formal issue.

Quoted 2 lines

This is wrong, as it says:

Kevin authored the code, sent it to Michael and Michael sent it to LKML.

The original code was authored by Michael. This version was authored by Kevin based on Michaels code.

So either you keep Michael as the author, then you want

From: Michael Brunner <[email protected]>

<patch description/>

or you decide that it's a major rewrite by you, then you want:

This patch is based on the original version authored by Michael Brunner <[email protected]>.

Quoted 6 lines

Or

Originally-From: Michael Brunner <[email protected]>.

Ok, I think this will make sense for all the patches except gpio, which is a major rewrite by Guenter.

So maybe this would be appropriate:

Quoted 8 lines

From: Guenter Roeck <[email protected]>

<patch description>

Originally-From: Michael Brunner <[email protected]>

...

Ok with me. Always happy to take the blame :)

Thanks,
Guenter

Quoted 9 lines

Thanks,
Kevin

Quoted 4 lines

Other than that.

#29 lore raw

PATCH v2 2/4 i2c: Kontron PLD i2c bus driver

In reply to Kevin Strasser

Add i2c support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/i2c/busses/Kconfig      |   10 +
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  410 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 421 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
drivers/i2c/busses/Kconfig +10 -0
index adfee98..3e1457d 100644
@@ -494,6 +494,16 @@ config I2C_IOP3XX
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-iop3xx.
 
+config I2C_KEMPLD
+	tristate "Kontron COM I2C Controller"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the I2C bus interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-kempld.
+
 config I2C_MPC
 	tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
 	depends on PPC
drivers/i2c/busses/Makefile +1 -0
index 8f4fc23..150fa15 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_I2C_IBM_IIC)	+= i2c-ibm_iic.o
 obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
 obj-$(CONFIG_I2C_INTEL_MID)	+= i2c-intel-mid.o
 obj-$(CONFIG_I2C_IOP3XX)	+= i2c-iop3xx.o
+obj-$(CONFIG_I2C_KEMPLD)	+= i2c-kempld.o
 obj-$(CONFIG_I2C_MPC)		+= i2c-mpc.o
 obj-$(CONFIG_I2C_MV64XXX)	+= i2c-mv64xxx.o
 obj-$(CONFIG_I2C_MXS)		+= i2c-mxs.o
drivers/i2c/busses/i2c-kempld.c new file +410 -0

410 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#30 lore raw

PATCH v2 3/4 gpio: Kontron PLD gpio driver

In reply to Kevin Strasser

Add gpio support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/gpio/Kconfig       |   12 +++
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/gpio-kempld.c |  225 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 238 insertions(+)
 create mode 100644 drivers/gpio/gpio-kempld.c
drivers/gpio/Kconfig +12 -0
index 93aaadf..e94b266 100644
@@ -658,6 +658,18 @@ config GPIO_UCB1400
 	  This enables support for the Philips UCB1400 GPIO pins.
 	  The UCB1400 is an AC97 audio codec.
 
+comment "LPC GPIO expanders:"
+
+config GPIO_KEMPLD
+	tristate "Kontron ETX / COMexpress GPIO"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the PLD GPIO interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called gpio-kempld.
+
 comment "MODULbus GPIO expanders:"
 
 config GPIO_JANZ_TTL
drivers/gpio/Makefile +1 -0
index 22e07bc..758c348 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_GPIO_GE_FPGA)	+= gpio-ge.o
 obj-$(CONFIG_GPIO_ICH)		+= gpio-ich.o
 obj-$(CONFIG_GPIO_IT8761E)	+= gpio-it8761e.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= gpio-janz-ttl.o
+obj-$(CONFIG_GPIO_KEMPLD)	+= gpio-kempld.o
 obj-$(CONFIG_ARCH_KS8695)	+= gpio-ks8695.o
 obj-$(CONFIG_GPIO_LANGWELL)	+= gpio-langwell.o
 obj-$(CONFIG_ARCH_LPC32XX)	+= gpio-lpc32xx.o
drivers/gpio/gpio-kempld.c new file +225 -0

225 lines not on this page · raw message

Signature · 1 line
1.7.9.5
Reviewed-by on v2 3/4
#31 lore raw

Re: [PATCH v2 3/4] gpio: Kontron PLD gpio driver

Quoted 6 lines · Kevin Strasser

Add gpio support for the on-board PLD found on some Kontron embedded modules.

This is looking good.

Yours,
Linus Walleij

#32 lore raw

Re: [PATCH v2 3/4] gpio: Kontron PLD gpio driver

Hi Linus,

Quoted 5 lines · Linus Walleij
Quoted 2 lines · Kevin Strasser

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Quoted 7 lines
Quoted 4 lines

This is looking good.

Thanks for the review. Are you planning on taking this in gpio-next, or would you prefer that it goes through mfd?

Thanks,
Kevin

Quoted 3 lines

Yours,
Linus Walleij

#33 lore raw

PATCH v2 4/4 watchdog: Kontron PLD watchdog timer driver

In reply to Kevin Strasser

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

---
 drivers/watchdog/Kconfig      |   11 +
 drivers/watchdog/Makefile     |    1 +
 drivers/watchdog/kempld_wdt.c |  580 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 592 insertions(+)
 create mode 100644 drivers/watchdog/kempld_wdt.c
drivers/watchdog/Kconfig +11 -0
index e89fc31..7460d34 100644
@@ -687,6 +687,17 @@ config HP_WATCHDOG
 	  To compile this driver as a module, choose M here: the module will be
 	  called hpwdt.
 
+config KEMPLD_WDT
+	tristate "Kontron COM Watchdog Timer"
+	depends on MFD_KEMPLD
+	select WATCHDOG_CORE
+	help
+	  Support for the PLD watchdog on some Kontron ETX and COMexpress
+	  (ETXexpress) modules
+
+	  This driver can also be built as a module. If so, the module will be
+	  called kempld_wdt.
+
 config HPWDT_NMI_DECODING
 	bool "NMI decoding support for the HP ProLiant iLO2+ Hardware Watchdog Timer"
 	depends on HP_WATCHDOG
drivers/watchdog/Makefile +1 -0
index a300b94..ec26899 100644
@@ -90,6 +90,7 @@ endif
 obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
+obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/kempld_wdt.c new file +580 -0

580 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#34 lore raw

v3 cover Kontron PLD drivers

In reply to Kevin Strasser

Changes since v2:
-Change Michael's "Signed-off-by" to "Originally-From" in all patches
-Add "From: Guenter Roeck <[email protected]>" to gpio patch

Guenter Roeck (1):
gpio: Kontron PLD gpio driver

Kevin Strasser (3):
mfd: Kontron PLD mfd driver
i2c: Kontron PLD i2c bus driver
watchdog: Kontron PLD watchdog timer driver

 drivers/gpio/Kconfig            |   12 +
 drivers/gpio/Makefile           |    1 +
 drivers/gpio/gpio-kempld.c      |  225 ++++++++++++++
 drivers/i2c/busses/Kconfig      |   10 +
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  410 +++++++++++++++++++++++++
 drivers/mfd/Kconfig             |   21 ++
 drivers/mfd/Makefile            |    1 +
 drivers/mfd/kempld-core.c       |  642 +++++++++++++++++++++++++++++++++++++++
 drivers/watchdog/Kconfig        |   11 +
 drivers/watchdog/Makefile       |    1 +
 drivers/watchdog/kempld_wdt.c   |  580 +++++++++++++++++++++++++++++++++++
 include/linux/mfd/kempld.h      |  125 ++++++++
 13 files changed, 2040 insertions(+)
 create mode 100644 drivers/gpio/gpio-kempld.c
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
 create mode 100644 drivers/mfd/kempld-core.c
 create mode 100644 drivers/watchdog/kempld_wdt.c
 create mode 100644 include/linux/mfd/kempld.h
Signature · 1 line
1.7.9.5
#35 lore raw

PATCH v3 1/4 mfd: Kontron PLD mfd driver

Add core MFD driver for the on-board PLD found on some Kontron embedded modules. The PLD device may provide functions like watchdog, GPIO, UART and I2C bus.

The following modules are supported:
* COMe-bIP#
* COMe-bPC2 (ETXexpress-PC)
* COMe-bSC# (ETXexpress-SC T#)
* COMe-cCT6
* COMe-cDC2 (microETXexpress-DC)
* COMe-cPC2 (microETXexpress-PC)
* COMe-mCT10
* ETX-OH

Originally-From: Michael Brunner <[email protected]>

---
 drivers/mfd/Kconfig        |   21 ++
 drivers/mfd/Makefile       |    1 +
 drivers/mfd/kempld-core.c  |  642 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/kempld.h |  125 +++++++++
 4 files changed, 789 insertions(+)
 create mode 100644 drivers/mfd/kempld-core.c
 create mode 100644 include/linux/mfd/kempld.h
drivers/mfd/Kconfig +21 -0
index c346941..eea3737 100644
@@ -912,6 +912,27 @@ config MFD_TIMBERDALE
 	The timberdale FPGA can be found on the Intel Atom development board
 	for in-vehicle infontainment, called Russellville.
 
+config MFD_KEMPLD
+	tristate "Support for Kontron module PLD device"
+	select MFD_CORE
+	help
+	  This is the core driver for the PLD (Programmable Logic Device) found
+	  on some Kontron ETX and COMexpress (ETXexpress) modules. The PLD
+	  device may provide functions like watchdog, GPIO, UART and I2C bus.
+
+	  The following modules are supported:
+		* COMe-bIP#
+		* COMe-bPC2 (ETXexpress-PC)
+		* COMe-bSC# (ETXexpress-SC T#)
+		* COMe-cCT6
+		* COMe-cDC2 (microETXexpress-DC)
+		* COMe-cPC2 (microETXexpress-PC)
+		* COMe-mCT10
+		* ETX-OH
+
+	  This driver can also be built as a module. If so, the module
+	  will be called kempld-core.
+
 config LPC_SCH
 	tristate "Intel SCH LPC"
 	depends on PCI && GENERIC_HARDIRQS
drivers/mfd/Makefile +1 -0
index b90409c..a2f6a9f 100644
@@ -123,6 +123,7 @@ obj-$(CONFIG_MFD_DB8500_PRCMU)	+= db8500-prcmu.o
 obj-$(CONFIG_AB8500_CORE)	+= ab8500-core.o ab8500-sysctrl.o
 obj-$(CONFIG_MFD_TIMBERDALE)    += timberdale.o
 obj-$(CONFIG_PMIC_ADP5520)	+= adp5520.o
+obj-$(CONFIG_MFD_KEMPLD)	+= kempld-core.o
 obj-$(CONFIG_LPC_SCH)		+= lpc_sch.o
 obj-$(CONFIG_LPC_ICH)		+= lpc_ich.o
 obj-$(CONFIG_MFD_RDC321X)	+= rdc321x-southbridge.o
drivers/mfd/kempld-core.c new file +642 -0

642 lines not on this page · raw message

include/linux/mfd/kempld.h new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#36 lore raw

PATCH v3 2/4 i2c: Kontron PLD i2c bus driver

In reply to Kevin Strasser

Add i2c support for the on-board PLD found on some Kontron embedded modules.

Originally-From: Michael Brunner <[email protected]>

---
 drivers/i2c/busses/Kconfig      |   10 +
 drivers/i2c/busses/Makefile     |    1 +
 drivers/i2c/busses/i2c-kempld.c |  410 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 421 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-kempld.c
drivers/i2c/busses/Kconfig +10 -0
index adfee98..3e1457d 100644
@@ -494,6 +494,16 @@ config I2C_IOP3XX
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-iop3xx.
 
+config I2C_KEMPLD
+	tristate "Kontron COM I2C Controller"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the I2C bus interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-kempld.
+
 config I2C_MPC
 	tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
 	depends on PPC
drivers/i2c/busses/Makefile +1 -0
index 8f4fc23..150fa15 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_I2C_IBM_IIC)	+= i2c-ibm_iic.o
 obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
 obj-$(CONFIG_I2C_INTEL_MID)	+= i2c-intel-mid.o
 obj-$(CONFIG_I2C_IOP3XX)	+= i2c-iop3xx.o
+obj-$(CONFIG_I2C_KEMPLD)	+= i2c-kempld.o
 obj-$(CONFIG_I2C_MPC)		+= i2c-mpc.o
 obj-$(CONFIG_I2C_MV64XXX)	+= i2c-mv64xxx.o
 obj-$(CONFIG_I2C_MXS)		+= i2c-mxs.o
drivers/i2c/busses/i2c-kempld.c new file +410 -0

410 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#38 lore raw

PATCH v3 3/4 gpio: Kontron PLD gpio driver

In reply to Kevin Strasser

From: Guenter Roeck <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Originally-From: Michael Brunner <[email protected]>

---
 drivers/gpio/Kconfig       |   12 +++
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/gpio-kempld.c |  225 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 238 insertions(+)
 create mode 100644 drivers/gpio/gpio-kempld.c
drivers/gpio/Kconfig +12 -0
index 93aaadf..e94b266 100644
@@ -658,6 +658,18 @@ config GPIO_UCB1400
 	  This enables support for the Philips UCB1400 GPIO pins.
 	  The UCB1400 is an AC97 audio codec.
 
+comment "LPC GPIO expanders:"
+
+config GPIO_KEMPLD
+	tristate "Kontron ETX / COMexpress GPIO"
+	depends on MFD_KEMPLD
+	help
+	  This enables support for the PLD GPIO interface on some Kontron ETX
+	  and COMexpress (ETXexpress) modules.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called gpio-kempld.
+
 comment "MODULbus GPIO expanders:"
 
 config GPIO_JANZ_TTL
drivers/gpio/Makefile +1 -0
index 22e07bc..758c348 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_GPIO_GE_FPGA)	+= gpio-ge.o
 obj-$(CONFIG_GPIO_ICH)		+= gpio-ich.o
 obj-$(CONFIG_GPIO_IT8761E)	+= gpio-it8761e.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= gpio-janz-ttl.o
+obj-$(CONFIG_GPIO_KEMPLD)	+= gpio-kempld.o
 obj-$(CONFIG_ARCH_KS8695)	+= gpio-ks8695.o
 obj-$(CONFIG_GPIO_LANGWELL)	+= gpio-langwell.o
 obj-$(CONFIG_ARCH_LPC32XX)	+= gpio-lpc32xx.o
drivers/gpio/gpio-kempld.c new file +225 -0

225 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#39 lore raw

Re: [PATCH v3 3/4] gpio: Kontron PLD gpio driver

Quoted 9 lines · Kevin Strasser

From: Guenter Roeck <[email protected]>

Add gpio support for the on-board PLD found on some Kontron embedded modules.

Originally-From: Michael Brunner <[email protected]>

Applied this to the GPIO tree now that the deps are upstream.

I did actually think that Sam would take this through MFD but maybe I was not clear enough :-(

Yours,
Linus Walleij

#40 lore raw

PATCH v3 4/4 watchdog: Kontron PLD watchdog timer driver

In reply to Kevin Strasser

Add watchdog timer support for the on-board PLD found on some Kontron embedded modules.

Originally-From: Michael Brunner <[email protected]>

---
 drivers/watchdog/Kconfig      |   11 +
 drivers/watchdog/Makefile     |    1 +
 drivers/watchdog/kempld_wdt.c |  580 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 592 insertions(+)
 create mode 100644 drivers/watchdog/kempld_wdt.c
drivers/watchdog/Kconfig +11 -0
index e89fc31..7460d34 100644
@@ -687,6 +687,17 @@ config HP_WATCHDOG
 	  To compile this driver as a module, choose M here: the module will be
 	  called hpwdt.
 
+config KEMPLD_WDT
+	tristate "Kontron COM Watchdog Timer"
+	depends on MFD_KEMPLD
+	select WATCHDOG_CORE
+	help
+	  Support for the PLD watchdog on some Kontron ETX and COMexpress
+	  (ETXexpress) modules
+
+	  This driver can also be built as a module. If so, the module will be
+	  called kempld_wdt.
+
 config HPWDT_NMI_DECODING
 	bool "NMI decoding support for the HP ProLiant iLO2+ Hardware Watchdog Timer"
 	depends on HP_WATCHDOG
drivers/watchdog/Makefile +1 -0
index a300b94..ec26899 100644
@@ -90,6 +90,7 @@ endif
 obj-$(CONFIG_IT8712F_WDT) += it8712f_wdt.o
 obj-$(CONFIG_IT87_WDT) += it87_wdt.o
 obj-$(CONFIG_HP_WATCHDOG) += hpwdt.o
+obj-$(CONFIG_KEMPLD_WDT) += kempld_wdt.o
 obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o
 obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o
 obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o
drivers/watchdog/kempld_wdt.c new file +580 -0

580 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#41 lore raw

Re: [PATCH v3 4/4] watchdog: Kontron PLD watchdog timer driver

Hi Wim,

This driver hasn't received any further feedback. Are you ready to take it into watchdog-next, or do you still need to review it?

Thanks,
Kevin

#42 lore raw

Re: [PATCH v3 4/4] watchdog: Kontron PLD watchdog timer driver

Hi Kevin,

Quoted 4 lines · Kevin Strasser

Hi Wim,

This driver hasn't received any further feedback. Are you ready to take it into watchdog-next, or do you still need to review it?

I applied the watchdog patch to mfd-next and got the following warning:

CC [M] drivers/watchdog/kempld_wdt.o
drivers/watchdog/kempld_wdt.c: In function ‘kempld_wdt_suspend’:
drivers/watchdog/kempld_wdt.c:538:3: warning: passing argument 1 of ‘kempld_wdt_stop’ from incompatible pointer type [enabled by default] drivers/watchdog/kempld_wdt.c:313:12: note: expected ‘struct watchdog_device *’ but argument is of type ‘struct platform_device *’

I fixed it up, but I'd appreciate if you could double check that kempld_wdt_suspend() is ok.

Cheers,
Samuel.

Signature · 6 lines
Intel Open Source Technology Centre
http://oss.intel.com/
--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#43 lore raw

Re: [PATCH v3 4/4] watchdog: Kontron PLD watchdog timer driver

Hi Samuel,

Quoted 16 lines · Samuel Ortiz

Hi Kevin,

Quoted 4 lines · Kevin Strasser

Hi Wim,

This driver hasn't received any further feedback. Are you ready to take it into watchdog-next, or do you still need to review it?

I applied the watchdog patch to mfd-next and got the following warning:

CC [M] drivers/watchdog/kempld_wdt.o
drivers/watchdog/kempld_wdt.c: In function ‘kempld_wdt_suspend’:
drivers/watchdog/kempld_wdt.c:538:3: warning: passing argument 1 of ‘kempld_wdt_stop’ from incompatible pointer type [enabled by default] drivers/watchdog/kempld_wdt.c:313:12: note: expected ‘struct watchdog_device *’ but argument is of type ‘struct platform_device *’

Quoted 2 lines

I fixed it up, but I'd appreciate if you could double check that kempld_wdt_suspend() is ok.

Ah, yes you made the appropriate change. This used to be a call to kempld_wdt_shutdown, which I changed to kempld_wdt_stop to avoid the pointless redirection. Somehow I forgot to convert the argument.

Thanks,
Kevin

Quoted 7 lines

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/

--
To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#44 lore raw

Re: [PATCH v3 0/4] Kontron PLD drivers

In reply to Kevin Strasser

Hi Kevin,

Quoted 11 lines · Kevin Strasser

Changes since v2:

(quoted diff) +0 -2
-Change Michael's "Signed-off-by" to "Originally-From" in all patches
-Add "From: Guenter Roeck <[email protected]>" to gpio patch

Guenter Roeck (1):
gpio: Kontron PLD gpio driver

Kevin Strasser (3):
mfd: Kontron PLD mfd driver
i2c: Kontron PLD i2c bus driver
watchdog: Kontron PLD watchdog timer driver

I just applied (after a warning fix) and pushed the MFD driver, thanks.

I am happy to take the sub devices driver as well, although the respective maintainers can safely carry them as they all have a Kconfig dependency on MFD_KEMPLD. So it's up to them.
If I have to take them, I'd need Wolfram's ACK for the i2c one and Wim's for the watchdog one. Linus already ACKed the GPIO one.

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/
#45 lore raw

Re: [PATCH v3 0/4] Kontron PLD drivers

Quoted 4 lines

I am happy to take the sub devices driver as well, although the respective maintainers can safely carry them as they all have a Kconfig dependency on MFD_KEMPLD. So it's up to them.

I'd like to push it via the i2c tree. Thanks!

#46 lore raw

Re: [PATCH v3 0/4] Kontron PLD drivers

In reply to Samuel Ortiz

Hi All,

Quoted 13 lines
Quoted 11 lines

Changes since v2:

(quoted diff) +0 -2
-Change Michael's "Signed-off-by" to "Originally-From" in all patches
-Add "From: Guenter Roeck <[email protected]>" to gpio patch

Guenter Roeck (1):
gpio: Kontron PLD gpio driver

Kevin Strasser (3):
mfd: Kontron PLD mfd driver
i2c: Kontron PLD i2c bus driver
watchdog: Kontron PLD watchdog timer driver

I just applied (after a warning fix) and pushed the MFD driver, thanks.

Quoted 5 lines

I am happy to take the sub devices driver as well, although the respective maintainers can safely carry them as they all have a Kconfig dependency on MFD_KEMPLD. So it's up to them.
If I have to take them, I'd need Wolfram's ACK for the i2c one and Wim's for the watchdog one. Linus already ACKed the GPIO one.

I'm OK that itgoes via the MFD tree.

Kind regards,
Wim.

#47 lore raw

Re: [PATCH v3 0/4] Kontron PLD drivers

Hi Wim,

Quoted 21 lines · Wim Van Sebroeck

Hi All,

Quoted 13 lines
Quoted 11 lines

Changes since v2:

(quoted diff) +0 -2
-Change Michael's "Signed-off-by" to "Originally-From" in all patches
-Add "From: Guenter Roeck <[email protected]>" to gpio patch

Guenter Roeck (1):
gpio: Kontron PLD gpio driver

Kevin Strasser (3):
mfd: Kontron PLD mfd driver
i2c: Kontron PLD i2c bus driver
watchdog: Kontron PLD watchdog timer driver

I just applied (after a warning fix) and pushed the MFD driver, thanks.

Quoted 5 lines

I am happy to take the sub devices driver as well, although the respective maintainers can safely carry them as they all have a Kconfig dependency on MFD_KEMPLD. So it's up to them.
If I have to take them, I'd need Wolfram's ACK for the i2c one and Wim's for the watchdog one. Linus already ACKed the GPIO one.

Quoted 3 lines

I'm OK that itgoes via the MFD tree.

I took it, thanks for your ACK.

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/