Re: [PATCH 3/4] mfd: imanager2: Add Core supports for IT8516/18/28

2014-08-19 Thread Wei-Chun Pan
> You've replied to this email as an independent message, so now there is little
> context.  When you reply, please ensure your mailer replies to the thread.
> 

Could you tell me more detail about this?
I use Git to reply mails but don't know how to set them to be dependent on.

> On Thu, 07 Aug 2014, Wei-Chun Pan wrote:
> > I have a question for your comment. As your mail,
> >
> > > > +
> > > > +static void __exit imanager2_mfd_device_exit(void) {
> > > > +   mfd_remove_devices(&ec_pdev->dev);
> > > > +   platform_device_unregister(ec_pdev);
> > > > +}
> > > > +
> > > > +module_init(imanager2_mfd_device_init);
> > > > +module_exit(imanager2_mfd_device_exit);
> > >
> > > This is a mess.  Use module_platform_driver() instead.
> > >
> > > > +MODULE_AUTHOR("Richard Vidal-Dorsch  > > > +advantech.com>");
> > > > +MODULE_DESCRIPTION("iManager2 platform device definitions v"
> > > > +DRV_VERSION); MODULE_LICENSE("GPL");
> MODULE_VERSION(DRV_VERSION);
> > >
> >
> > How can I use module_platform_driver() and my driver exit function
> > also can do mfd_remove_devices().
> > Or any other better function install of mfd_remove_devices()?
> 
> That's the point - you don't do mfd_remove_devices() in exit(), you do
> mfd_add_devices() in .probe() and mfd_remove_devices() in .remove().
> 
> The only thing you want to do in init() is platform_driver_register(), or 
> even better
> - don't have init() and exit() calls and use use
> module_platform_driver_probe() instead.
> 

It seems that module_platform_driver() only registers/unregisters driver but 
not device.
Is there a method to register/unregister my device?

--
Wei-Chun Pan
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


RE: [PATCH 3/4] mfd: imanager2: Add Core supports for IT8516/18/28

2014-08-07 Thread Wei-Chun Pan
I have a question for your comment. As your mail,

> > +
> > +static void __exit imanager2_mfd_device_exit(void) {
> > +   mfd_remove_devices(&ec_pdev->dev);
> > +   platform_device_unregister(ec_pdev);
> > +}
> > +
> > +module_init(imanager2_mfd_device_init);
> > +module_exit(imanager2_mfd_device_exit);
> 
> This is a mess.  Use module_platform_driver() instead.
> 
> > +MODULE_AUTHOR("Richard Vidal-Dorsch  > +advantech.com>");
> > +MODULE_DESCRIPTION("iManager2 platform device definitions v"
> > +DRV_VERSION); MODULE_LICENSE("GPL"); MODULE_VERSION(DRV_VERSION);
> 

How can I use module_platform_driver() and my driver exit function also can do
mfd_remove_devices().
Or any other better function install of mfd_remove_devices()?

Signed-off-by: Wei-Chun Pan 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/3] imanager2: rename io functions and remove no used functions

2014-08-07 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/hwmon/imanager2_hwm.c| 27 +--
 drivers/mfd/imanager2_core.c | 64 +++---
 drivers/mfd/imanager2_ec.c   | 99 
 include/linux/mfd/imanager2_ec.h | 24 --
 4 files changed, 67 insertions(+), 147 deletions(-)

diff --git a/drivers/hwmon/imanager2_hwm.c b/drivers/hwmon/imanager2_hwm.c
index 335bffb..ab63296 100644
--- a/drivers/hwmon/imanager2_hwm.c
+++ b/drivers/hwmon/imanager2_hwm.c
@@ -119,17 +119,17 @@ static int imanager2_volt_get_value_by_io(struct 
imanager2 *ec, int index,
u8 portnum;
int ret;
 
-   ret = imanager2_mbox_io_read(EC_CMD_ADC_INDEX, pin, &portnum, 1);
+   ret = imanager2_io_read_data(EC_CMD_ADC_INDEX, pin, &portnum, 1);
if (ret)
return ret;
if (portnum == EC_ERROR)
return -ENXIO;
 
-   ret = imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_LSB, &buf[1]);
+   ret = imanager2_io_nooffset_readbyte(EC_CMD_ADC_READ_LSB, &buf[1]);
if (ret)
return ret;
 
-   return imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_MSB, &buf[0]);
+   return imanager2_io_nooffset_readbyte(EC_CMD_ADC_READ_MSB, &buf[0]);
 }
 
 static int imanager2_volt_get_value(struct imanager2 *ec, int index,
@@ -468,7 +468,7 @@ static void imanager2_temp_init(struct imanager2 *ec)
  (u8 *)&zone,
  &len);
} else {
-   ret = imanager2_mbox_io_read(
+   ret = imanager2_io_read_data(
EC_CMD_HWRAM_READ,
EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_STATUS(thm),
&zone.status, 1);
@@ -566,20 +566,17 @@ static struct fan_item ec_fan_table[] = {
{
.did = tacho0,
.name = "Fan CPU",
-   .fspeed_acpireg = 0,
-   .visible = false
+   .visible = false,
},
{
.did = tacho1,
.name = "Fan SYS",
-   .fspeed_acpireg = 0,
-   .visible = false
+   .visible = false,
},
{
.did = tacho2,
.name = "Fan SYS2",
-   .fspeed_acpireg = 0,
-   .visible = false
+   .visible = false,
},
 };
 
@@ -596,7 +593,7 @@ static int imanager2_fan_get_value(struct imanager2 *ec, 
int index,
   EC_CMD_MAILBOX_READ_HW_PIN,
   ec_fan_table[index].did, tmp, 2);
else
-   ret = imanager2_mbox_io_read(EC_CMD_ACPIRAM_READ,
+   ret = imanager2_io_read_data(EC_CMD_ACPIRAM_READ,
 ec_fan_table[index].fspeed_acpireg,
 tmp, 2);
 
@@ -605,11 +602,11 @@ static int imanager2_fan_get_value(struct imanager2 *ec, 
int index,
if (ret)
return ret;
 
-   if (tmp[0] == 0xFF && tmp[1] == 0xFF)
-   return -ENODEV;
-
*speed_rpm = (tmp[0] << 8) | tmp[1];
 
+   if (*speed_rpm == 0x)
+   *speed_rpm = 0;
+
return 0;
 }
 
@@ -621,7 +618,7 @@ static int imanager2_fan_item_init_by_io(struct imanager2 
*ec, int fnum)
u8 tmp;
 
mutex_lock(&ec->lock);
-   ret = imanager2_mbox_io_read(EC_CMD_HWRAM_READ,
+   ret = imanager2_io_read_data(EC_CMD_HWRAM_READ,
 EC_HWRAM_ADDR_FAN_CONTROL(fnum), &tmp, 1);
mutex_unlock(&ec->lock);
 
diff --git a/drivers/mfd/imanager2_core.c b/drivers/mfd/imanager2_core.c
index 2264d29..a978d96 100644
--- a/drivers/mfd/imanager2_core.c
+++ b/drivers/mfd/imanager2_core.c
@@ -38,7 +38,7 @@ enum chips {
it8528 = 0x8528,
 };
 
-#define EC_CMD_AUTHENTICATION  0x30
+#define EC_EBRAIN_CODE 0x95
 
 static int imanager2_authentication(struct imanager2 *ec)
 {
@@ -52,28 +52,24 @@ static int imanager2_authentication(struct imanager2 *ec)
goto unlock;
}
 
-   if (inb(EC_IO_PORT_CMD) & IO_FLAG_OBF)
-   inb(EC_IO_PORT_DATA);   /* initial OBF */
-
-   if (ec_outb_after_ibc0(EC_IO_PORT_CMD, EC_CMD_AUTHENTICATION)) {
-   ret = -ENODEV;
-   goto unlock;
-   }
-
-   ret = ec_inb_after_obf1(&tmp);
+   ec_clear_obf();
 
+   ret = imanager2_io_nooffset_readbyte(EC_CMD_AUTHENTICATION, &tmp);
 unlock:
mutex_unlock(&ec->lock);
 
if (ret)
return ret;
 
-   if (tmp != 0x95)
+   if (tmp != EC_EBRAIN_CODE)
return -ENODEV;
 
return 0;
 }
 
+#define EC_SIO_CMD

RE: [PATCH 4/4] hwmon: (imanager2) Add support for IT8516/18/28

2014-08-07 Thread Wei-Chun Pan
Sorry, I sent wrong patch just now. Please ignore the mail "[PATCH 1/3]
 imanager2: rename io functions and remove no used functions".

This mail include 2 patches:
The first patch is shown I rename some functions.
The second patch is shown I moditfy code according to your comment.

Signed-off-by: Wei-Chun Pan 

---
diff --git a/drivers/hwmon/imanager2_hwm.c b/drivers/hwmon/imanager2_hwm.c
index 335bffb..ab63296 100644
--- a/drivers/hwmon/imanager2_hwm.c
+++ b/drivers/hwmon/imanager2_hwm.c
@@ -119,17 +119,17 @@ static int imanager2_volt_get_value_by_io(struct 
imanager2 *ec, int index,
u8 portnum;
int ret;
 
-   ret = imanager2_mbox_io_read(EC_CMD_ADC_INDEX, pin, &portnum, 1);
+   ret = imanager2_io_read_data(EC_CMD_ADC_INDEX, pin, &portnum, 1);
if (ret)
return ret;
if (portnum == EC_ERROR)
return -ENXIO;
 
-   ret = imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_LSB, &buf[1]);
+   ret = imanager2_io_nooffset_readbyte(EC_CMD_ADC_READ_LSB, &buf[1]);
if (ret)
return ret;
 
-   return imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_MSB, &buf[0]);
+   return imanager2_io_nooffset_readbyte(EC_CMD_ADC_READ_MSB, &buf[0]);
 }
 
 static int imanager2_volt_get_value(struct imanager2 *ec, int index,
@@ -468,7 +468,7 @@ static void imanager2_temp_init(struct imanager2 *ec)
  (u8 *)&zone,
  &len);
} else {
-   ret = imanager2_mbox_io_read(
+   ret = imanager2_io_read_data(
EC_CMD_HWRAM_READ,
EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_STATUS(thm),
&zone.status, 1);
 
@@ -596,7 +593,7 @@ static int imanager2_fan_get_value(struct imanager2 *ec, 
int index,
   EC_CMD_MAILBOX_READ_HW_PIN,
   ec_fan_table[index].did, tmp, 2);
else
-   ret = imanager2_mbox_io_read(EC_CMD_ACPIRAM_READ,
+   ret = imanager2_io_read_data(EC_CMD_ACPIRAM_READ,
 ec_fan_table[index].fspeed_acpireg,
 tmp, 2);
 
@@ -621,7 +618,7 @@ static int imanager2_fan_item_init_by_io(struct imanager2 
*ec, int fnum)
u8 tmp;
 
mutex_lock(&ec->lock);
-   ret = imanager2_mbox_io_read(EC_CMD_HWRAM_READ,
+   ret = imanager2_io_read_data(EC_CMD_HWRAM_READ,
 EC_HWRAM_ADDR_FAN_CONTROL(fnum), &tmp, 1);
mutex_unlock(&ec->lock);

---
> > ---
> >  drivers/hwmon/Kconfig |   5 +
> >  drivers/hwmon/Makefile|   1 +
> >  drivers/hwmon/imanager2_hwm.c | 768
> ++
> 
> Documentation/hwmon/imanager2_hwm missing.

diff --git a/Documentation/hwmon/imanager2_hwm 
b/Documentation/hwmon/imanager2_hwm
new file mode 100644
index 000..bf3d0b5
--- /dev/null
+++ b/Documentation/hwmon/imanager2_hwm
@@ -0,0 +1,42 @@
+Kernel driver imanager2_hwm
+===
+
+Supported chips:
+  * ITE IT8516
+Prefix: 'it8516'
+Addresses scanned: I/O chennel 0x029A/0x0299,
+   IET mailbox chennel 0x029E/0x029F
+Datasheet: Not publicly available
+  * ITE IT8518
+Prefix: 'it8518'
+Addresses scanned: I/O chennel 0x029A/0x0299,
+   IET mailbox chennel 0x029E/0x029F
+Datasheet: Not publicly available
+  * ITE IT8528
+Prefix: 'it8528'
+Addresses scanned: I/O chennel 0x029A/0x0299
+Datasheet: Not publicly available
+
+Authors:
+Richard Vidal-Dorsch 
+
+
+Description
+---
+
+This driver supports the hardware monitoring features of the IT8516, IT8518, 
and
+IT8528 chips. These features include 11 voltage sensors, 1 current sensor, 4
+temperature sensors, and 3 fan rotation speed sensors.
+
+ITE IT8516, IT8518, and IT8528 are are 'EC chips'. These chips are like Super 
I/O control boards. For IT8516 and IT 8518 The control chennel can be I/O or 
ITE mailbox chennel. I/O chennel is a common way but ITE mailbox chennel 
performance faster since it does not need to wait IBF (input buffer full) and 
OBF (output buffer full) to before send or get data.
+
+ITE IT8528 use an I/O chennel way to access mailbox, called I/O mailbox for 
cost down. Its performance the between pure I/O controller and ITE mailbox 
controller.
+
+
+sysfs-Interface
+---
+
+in[0-11]_input - adc voltage input
+curr1_input- adc current input
+temp[1-4]_input- temperature input
+fan[1-3]_input - fan speed input

> 
> >  3 files changed, 774 insertions(+)
> >  create mode 100644 dr

[PATCH 4/4] hwmon: (imanager2) Add support for IT8516/18/28

2014-07-14 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/hwmon/Kconfig |   5 +
 drivers/hwmon/Makefile|   1 +
 drivers/hwmon/imanager2_hwm.c | 768 ++
 3 files changed, 774 insertions(+)
 create mode 100644 drivers/hwmon/imanager2_hwm.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index bc196f4..7524fc3 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -39,6 +39,11 @@ config HWMON_DEBUG_CHIP
 
 comment "Native drivers"
 
+config SENSORS_IMANAGER2
+   tristate "Support for Advantech iManager2 EC H.W. Monitor"
+   select MFD_CORE
+   depends on MFD_IMANAGER2
+
 config SENSORS_AB8500
tristate "AB8500 thermal monitoring"
depends on AB8500_GPADC && AB8500_BM
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index c48f987..a2c8f07 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -146,6 +146,7 @@ obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)+= w83l786ng.o
 obj-$(CONFIG_SENSORS_WM831X)   += wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
+obj-$(CONFIG_SENSORS_IMANAGER2)+= imanager2_hwm.o
 
 obj-$(CONFIG_PMBUS)+= pmbus/
 
diff --git a/drivers/hwmon/imanager2_hwm.c b/drivers/hwmon/imanager2_hwm.c
new file mode 100644
index 000..335bffb
--- /dev/null
+++ b/drivers/hwmon/imanager2_hwm.c
@@ -0,0 +1,768 @@
+/*
+ * imanager2_hwm.c - HW Monitoring interface for Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define DRV_NAME   "imanager2_hwm"
+#define DRV_VERSION"4.0.1"
+
+/* ADC */
+#define EC_ADC_RESOLUTION_MAX  0x03FF  /* 10-bit */
+#define EC_ADC_VALUE_MAX   3000/* max: 3000 mV or mA */
+
+/* Thermal */
+#define EC_THERMAL_ZONE_MAX4
+
+enum thermaltype {
+   none,
+   sys1,
+   cpu,
+   sys3,
+   sys2,
+};
+
+struct ec_thermalzone {
+   u8 channel,
+  addr,
+  cmd,
+  status,
+  fancode,
+  temp;
+};
+
+/* Tacho */
+#define EC_MAX_IO_FAN  3
+
+/* Voltage */
+struct volt_item {
+   u8 did;
+   const char *name;
+   int factor;
+   bool visible;
+};
+
+static struct volt_item ec_volt_table[] = {
+   {
+   .did = adcmosbat,
+   .name = "BAT CMOS",
+   },
+   {
+   .did = adcbat,
+   .name = "BAT",
+   },
+   {
+   .did = adc5vs0,
+   .name = "5V S0",
+   },
+   {
+   .did = adv5vs5,
+   .name = "5V S5",
+   },
+   {
+   .did = adc33vs0,
+   .name = "3V3 S0",
+   },
+   {
+   .did = adc33vs5,
+   .name = "3V3 S5",
+   },
+   {
+   .did = adv12vs0,
+   .name = "12V S0",
+   },
+   {
+   .did = adcvcorea,
+   .name = "Vcore A",
+   },
+   {
+   .did = adcvcoreb,
+   .name = "Vcore B",
+   },
+   {
+   .did = adcdc,
+   .name = "DC",
+   },
+   {
+   .did = adcdcstby,
+   .name = "DC Standby",
+   },
+   {
+   .did = adcdcother,
+   .name = "DC Other",
+   },
+};
+
+static int imanager2_volt_get_value_by_io(struct imanager2 *ec, int index,
+ u8 *buf)
+{
+   u8 item = ec->table.devid2itemnum[ec_volt_table[index].did];
+   u8 pin = ec->table.pinnum[item];
+   u8 portnum;
+   int ret;
+
+   ret = imanager2_mbox_io_read(EC_CMD_ADC_INDEX, pin, &portnum, 1);
+   if (ret)
+   return ret;
+   if (portnum == EC_ERROR)
+   return -ENXIO;
+
+   ret = imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_LSB, &buf[1]);
+   if (ret)
+   return ret;
+
+   return imanager2_mbox_io_simple_read(EC_CMD_ADC_READ_MSB, &buf[0]);
+}
+
+static int imanager2_volt_get_value(struct imanager2 *ec, int index,
+ 

[PATCH 1/4] mfd: imanager2: Add defines support for IT8516/18/28

2014-07-14 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 include/linux/mfd/imanager2_ec.h | 358 +++
 1 file changed, 358 insertions(+)
 create mode 100644 include/linux/mfd/imanager2_ec.h

diff --git a/include/linux/mfd/imanager2_ec.h b/include/linux/mfd/imanager2_ec.h
new file mode 100644
index 000..bf7d70e
--- /dev/null
+++ b/include/linux/mfd/imanager2_ec.h
@@ -0,0 +1,358 @@
+/*
+ * imanager2_ec.h - MFD driver defines of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __IMANAGER2_EC_H__
+#define __IMANAGER2_EC_H__
+
+#include 
+
+#define EC_FLAG_IO 0
+#define EC_FLAG_IO_MAILBOX (1 << 0)
+#define EC_FLAG_MAILBOX(1 << 1)
+
+#define EC_MAX_DEVICE_ID_NUM   0xFF
+#define EC_MAX_ITEM_NUM32
+
+struct ec_table {
+   u8 devid2itemnum[EC_MAX_DEVICE_ID_NUM];
+   u8 pinnum[EC_MAX_ITEM_NUM];
+   u8 devid[EC_MAX_ITEM_NUM];
+};
+
+struct ec_version {
+   u16 kernel_ver,
+   chip_code,
+   prj_id,
+   prj_ver;
+};
+
+#define EC_MAX_LEN_PROJECT_NAME8
+
+struct imanager2 {
+   u16 id;
+   u32 flag;
+   struct mutex lock;  /* protects io */
+   char prj_name[EC_MAX_LEN_PROJECT_NAME + 1]; /* strlen + '\0' */
+   struct ec_version version;
+   struct ec_table table;
+};
+
+/*
+ * Definition
+ */
+#define EC_TABLE_ITEM_UNUSED   0xFF
+#define EC_TABLE_DID_NODEV 0x00
+#define EC_TABLE_HWP_NODEV 0xFF
+#define EC_TABLE_NOITEM0xFF
+
+#define EC_ERROR   0xFF
+
+#define EC_RAM_BANK_SIZE   32  /* 32 bytes size for each bank. */
+#define EC_RAM_BUFFER_SIZE 256 /* 32 bytes * 8 banks = 256 bytes */
+
+#define EC_SIO_CMD 0x29C
+#define EC_SIO_DATA0x29D
+
+/* Access Mailbox */
+#define EC_IO_PORT_CMD 0x29A
+#define EC_IO_PORT_DATA0x299
+
+#define EC_IO_CMD_READ_OFFSET  0xA0
+#define EC_IO_CMD_WRITE_OFFSET 0x50
+
+#define EC_ITE_PORT_OFS0x29E
+#define EC_ITE_PORT_DATA   0x29F
+
+/*
+ * CMD - IO
+ */
+/* ADC */
+#define EC_CMD_ADC_INDEX   0x15
+#define EC_CMD_ADC_READ_LSB0x16
+#define EC_CMD_ADC_READ_MSB0x1F
+/* HW Control Table */
+#define EC_CMD_HWCTRLTABLE_INDEX   0x20
+#define EC_CMD_HWCTRLTABLE_GET_PIN_NUM 0x21
+#define EC_CMD_HWCTRLTABLE_GET_DEVICE_ID   0x22
+#define EC_CMD_HWCTRLTABLE_GET_PIN_ACTIVE_POLARITY 0x23
+/* ACPI RAM */
+#define EC_CMD_ACPIRAM_READ0x80
+#define EC_CMD_ACPIRAM_WRITE   0x81
+/* Extend RAM */
+#define EC_CMD_EXTRAM_READ 0x86
+#define EC_CMD_EXTRAM_WRITE0x87
+/* HW RAM */
+#define EC_CMD_HWRAM_READ  0x88
+#define EC_CMD_HWRAM_WRITE 0x89
+
+/*
+ * ACPI RAM Address Table
+ */
+/* n = 1 ~ 2 */
+#define EC_ACPIRAM_ADDR_TEMPERATURE_BASE(n)(0x60 + 3 * ((n) - 1))
+#defineEC_ACPIRAM_ADDR_LOCAL_TEMPERATURE(n) \
+   EC_ACPIRAM_ADDR_TEMPERATURE_BASE(n)
+#defineEC_ACPIRAM_ADDR_REMOTE_TEMPERATURE(n) \
+   (EC_ACPIRAM_ADDR_TEMPERATURE_BASE(n) + 1)
+#defineEC_ACPIRAM_ADDR_WARNING_TEMPERATURE(n)\
+   (EC_ACPIRAM_ADDR_TEMPERATURE_BASE(n) + 2)
+
+/* N = 0 ~ 2 */
+#define EC_ACPIRAM_ADDR_FAN_SPEED_BASE(N)  (0x70 + 2 * (N))
+
+#define EC_ACPIRAM_ADDR_KERNEL_MAJOR_VERSION   0xF8
+#define EC_ACPIRAM_ADDR_CHIP_VENDOR_CODE   0xFA
+#define EC_ACPIRAM_ADDR_PROJECT_NAME_CODE  0xFC
+#define EC_ACPIRAM_ADDR_FIRMWARE_MAJOR_VERSION 0xFE
+
+/*
+ * HW RAM Address Table
+ */
+/* Thermal Source Control RAM 0xB0-0xC7 (N: 0 ~ 3) */
+#define EC_HWRAM_ADDR_THERMAL_SOURCE_BASE_ADDR(N)  (0xB0 + 6 * (N))
+#define EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_CHANNEL(N) \
+   EC_HWRAM_ADDR_THERMAL_SOURCE_BASE_ADDR(N)
+#define EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_ADDR(N) \
+   (EC_HWRAM_ADDR_THERMAL_SOURCE_BASE_ADDR(N) + 1)
+#define EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_CMD(N) \
+   (EC_HWRAM_ADDR_THERMAL_SOURCE_BASE_ADDR(N) + 2)
+#define EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_STATUS(N) \
+   (EC_HWRAM_ADD

[PATCH 3/4] mfd: imanager2: Add Core supports for IT8516/18/28

2014-07-14 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/mfd/Kconfig  |   6 +
 drivers/mfd/Makefile |   2 +
 drivers/mfd/imanager2_core.c | 303 +++
 3 files changed, 311 insertions(+)
 create mode 100644 drivers/mfd/imanager2_core.c

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3383412..48b063f 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -10,6 +10,12 @@ config MFD_CORE
select IRQ_DOMAIN
default n
 
+config MFD_IMANAGER2
+   tristate "Support for Advantech iManager2 EC ICs"
+   select MFD_CORE
+   help
+ Support for Advantech iManager2 EC ICs
+
 config MFD_CS5535
tristate "AMD CS5535 and CS5536 southbridge core functions"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 2851275..10c64ae 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -166,3 +166,5 @@ obj-$(CONFIG_MFD_RETU)  += retu-mfd.o
 obj-$(CONFIG_MFD_AS3711)   += as3711.o
 obj-$(CONFIG_MFD_AS3722)   += as3722.o
 obj-$(CONFIG_MFD_STW481X)  += stw481x.o
+imanager2-objs := imanager2_core.o imanager2_ec.o
+obj-$(CONFIG_MFD_IMANAGER2)+= imanager2.o
diff --git a/drivers/mfd/imanager2_core.c b/drivers/mfd/imanager2_core.c
new file mode 100644
index 000..2264d29
--- /dev/null
+++ b/drivers/mfd/imanager2_core.c
@@ -0,0 +1,303 @@
+/*
+ * imanager2_core.c - MFD core driver of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+
+#define DRV_NAME   "imanager2"
+#define DRV_VERSION"4.0.1"
+
+static struct mfd_cell imanager2_cells[] = {
+   {
+   .name = "imanager2_hwm",
+   },
+   {
+   .name = "imanager2_i2c",
+   },
+};
+
+enum chips {
+   it8516 = 0x8516,
+   it8518 = 0x8518,
+   it8528 = 0x8528,
+};
+
+#define EC_CMD_AUTHENTICATION  0x30
+
+static int imanager2_authentication(struct imanager2 *ec)
+{
+   u8 tmp;
+   int ret;
+
+   mutex_lock(&ec->lock);
+
+   if (inb(EC_IO_PORT_CMD) == 0xFF && inb(EC_IO_PORT_DATA) == 0xFF) {
+   ret = -ENODEV;
+   goto unlock;
+   }
+
+   if (inb(EC_IO_PORT_CMD) & IO_FLAG_OBF)
+   inb(EC_IO_PORT_DATA);   /* initial OBF */
+
+   if (ec_outb_after_ibc0(EC_IO_PORT_CMD, EC_CMD_AUTHENTICATION)) {
+   ret = -ENODEV;
+   goto unlock;
+   }
+
+   ret = ec_inb_after_obf1(&tmp);
+
+unlock:
+   mutex_unlock(&ec->lock);
+
+   if (ret)
+   return ret;
+
+   if (tmp != 0x95)
+   return -ENODEV;
+
+   return 0;
+}
+
+#define EC_ITE_CHIPID_H8   0x20
+#define EC_ITE_CHIPID_L8   0x21
+
+static int imanager2_get_chip_type(struct imanager2 *ec)
+{
+   mutex_lock(&ec->lock);
+
+   outb(EC_ITE_CHIPID_H8, EC_SIO_CMD);
+   ec->id = inb(EC_SIO_DATA) << 8;
+   outb(EC_ITE_CHIPID_L8, EC_SIO_CMD);
+   ec->id |= inb(EC_SIO_DATA);
+
+   mutex_unlock(&ec->lock);
+
+   switch (ec->id) {
+   case it8516:
+   case it8518:
+   ec->flag = EC_FLAG_IO;
+   break;
+   case it8528:
+   ec->flag |= EC_FLAG_IO_MAILBOX;
+   break;
+   default:
+   return -ENODEV;
+   }
+
+   return 0;
+}
+
+/*
+ * EC provides IO channel and ITE mailbox ways to access mailbox. IO channel is
+ * a common way to access mailbox, but IET mailbox way is much faster than IO
+ * channel. We prefer ITE mailbox if firmware supports. Source kernel code
+ * X11_05 the first firmware version that supports ITE mailbox.
+ */
+#define EC_CHIPFW_SUPP_ITEMAILBOX  0x1105
+
+static int imanager2_get_info(struct imanager2 *ec)
+{
+   int ret;
+   u8 *tmp = (u8 *)&ec->version.kernel_ver;
+
+   mutex_lock(&ec->lock);
+
+   ret = imanager2_mbox_io_read(EC_CMD_ACPIRAM_READ,
+EC_ACPIRAM_ADDR_KERNEL_MAJOR_VERSION,
+&tmp[0], 2);
+
+   if (ret)
+   goto unlock;
+
+   if (ec->version.kernel_ver >= EC_CHIPFW_SUPP_IT

[PATCH 2/4] mfd: imanager2: Add Advantech EC APIs support for IT8516/18/28

2014-07-14 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/mfd/imanager2_ec.c | 615 +
 1 file changed, 615 insertions(+)
 create mode 100644 drivers/mfd/imanager2_ec.c

diff --git a/drivers/mfd/imanager2_ec.c b/drivers/mfd/imanager2_ec.c
new file mode 100644
index 000..f7a0003
--- /dev/null
+++ b/drivers/mfd/imanager2_ec.c
@@ -0,0 +1,615 @@
+/*
+ * imanager2_ec.c - MFD accessing driver of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+
+#define EC_UDELAY_TIME 50
+#define EC_MAX_TIMEOUT_COUNT   1000
+
+static int ec_wait_obf1(void)
+{
+   int i = EC_MAX_TIMEOUT_COUNT;
+   while (i--) {
+   if (inb(EC_IO_PORT_CMD) & IO_FLAG_OBF)
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   };
+
+   return -EBUSY;
+}
+
+int ec_inb_after_obf1(u8 *data)
+{
+   int ret = ec_wait_obf1();
+   if (ret)
+   return ret;
+   *data = inb(EC_IO_PORT_DATA);
+   return 0;
+}
+EXPORT_SYMBOL(ec_inb_after_obf1);
+
+static int ec_wait_ibc0(void)
+{
+   int i = EC_MAX_TIMEOUT_COUNT;
+   while (i--) {
+   if (!(inb(EC_IO_PORT_CMD) & IO_FLAG_IBF))
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   };
+
+   return -EBUSY;
+}
+
+int ec_outb_after_ibc0(u16 port, u8 data)
+{
+   int ret = ec_wait_ibc0();
+   if (ret)
+   return ret;
+   outb(data, port);
+   return 0;
+}
+EXPORT_SYMBOL(ec_outb_after_ibc0);
+
+static int imanager2_read_mailbox(u32 ecflag, u8 offset, u8 *data)
+{
+   if (ecflag & EC_FLAG_IO_MAILBOX) {
+   int ret = ec_wait_ibc0();
+   if (ret)
+   return ret;
+   inb(EC_IO_PORT_DATA);
+   outb(offset + EC_IO_CMD_READ_OFFSET, EC_IO_PORT_CMD);
+
+   return ec_inb_after_obf1(data);
+   } else {
+   outb(offset, EC_ITE_PORT_OFS);
+   *data = inb(EC_ITE_PORT_DATA);
+   }
+
+   return 0;
+}
+
+static int imanager2_write_mailbox(u32 ecflag, u8 offset, u8 data)
+{
+   if (ecflag & EC_FLAG_IO_MAILBOX) {
+   int ret = ec_outb_after_ibc0(EC_IO_PORT_CMD,
+offset + EC_IO_CMD_WRITE_OFFSET);
+   if (ret)
+   return ret;
+
+   return ec_outb_after_ibc0(EC_IO_PORT_DATA, data);
+   } else {
+   outb(offset, EC_ITE_PORT_OFS);
+   outb(data, EC_ITE_PORT_DATA);
+   }
+
+   return 0;
+}
+
+static int imanager2_wait_mailbox_command0(u32 ecflag)
+{
+   u8 cmd;
+   int i, ret;
+
+   for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+   ret = imanager2_read_mailbox(ecflag, EC_MAILBOX_OFFSET_CMD,
+&cmd);
+   if (ret)
+   return ret;
+   if (!cmd)
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   }
+
+   return -EBUSY;
+}
+
+int imanager2_mbox_read_data(u32 ecflag, u8 cmd, u8 para, u8 *data, int len)
+{
+   int ret, i;
+   u8 status;
+
+   ret = imanager2_wait_mailbox_command0(ecflag);
+   if (ret)
+   return ret;
+
+   ret = imanager2_write_mailbox(ecflag, EC_MAILBOX_OFFSET_PARA, para);
+   if (ret)
+   return ret;
+
+   ret = imanager2_write_mailbox(ecflag, EC_MAILBOX_OFFSET_CMD, cmd);
+   if (ret)
+   return ret;
+
+   ret = imanager2_wait_mailbox_command0(ecflag);
+   if (ret)
+   return ret;
+
+   ret = imanager2_read_mailbox(ecflag, EC_MAILBOX_OFFSET_STATUS, &status);
+   if (ret)
+   return ret;
+   if (status != EC_MAILBOX_STATUS_SUCCESS)
+   return -ENXIO;
+
+   for (i = 0; i < len; i++) {
+   ret = imanager2_read_mailbox(ecflag, EC_MAILBOX_OFFSET_DAT(i),
+&data[i]);
+   if (ret)
+   return ret;
+   }
+
+   return 0;
+}
+EXPORT_SYMBOL(imanager2_mbox_read_data);
+
+int imanager2_mbox_write_data(u32 ecflag, u8 cmd, u8 para, u8 *data, int len)
+{
+   int 

[PATCH 4/5] hwmon: (imanager2) Add support for IT8516/18/28

2014-06-23 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/hwmon/Kconfig |   5 +
 drivers/hwmon/Makefile|   1 +
 drivers/hwmon/imanager2_hwm.c | 773 ++
 3 files changed, 779 insertions(+)
 create mode 100644 drivers/hwmon/imanager2_hwm.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index bc196f4..7524fc3 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -39,6 +39,11 @@ config HWMON_DEBUG_CHIP
 
 comment "Native drivers"
 
+config SENSORS_IMANAGER2
+   tristate "Support for Advantech iManager2 EC H.W. Monitor"
+   select MFD_CORE
+   depends on MFD_IMANAGER2
+
 config SENSORS_AB8500
tristate "AB8500 thermal monitoring"
depends on AB8500_GPADC && AB8500_BM
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index c48f987..a2c8f07 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -146,6 +146,7 @@ obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)+= w83l786ng.o
 obj-$(CONFIG_SENSORS_WM831X)   += wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
+obj-$(CONFIG_SENSORS_IMANAGER2)+= imanager2_hwm.o
 
 obj-$(CONFIG_PMBUS)+= pmbus/
 
diff --git a/drivers/hwmon/imanager2_hwm.c b/drivers/hwmon/imanager2_hwm.c
new file mode 100644
index 000..ae7508c
--- /dev/null
+++ b/drivers/hwmon/imanager2_hwm.c
@@ -0,0 +1,773 @@
+/*
+ * imanager2_hwm.c - HW Monitoring interface for Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define DRV_NAME   "imanager2_hwm"
+#define DRV_VERSION"4.0.1"
+
+/* ADC */
+#define EC_ADC_RESOLUTION_MAX  0x03FF  /* 10-bit */
+#define EC_ADC_VALUE_MAX   3000/* max: 3000 mV or mA */
+
+/* Thermal */
+#define EC_THERMAL_ZONE_MAX4
+
+enum thermaltype {
+   none,
+   sys1,
+   cpu,
+   sys3,
+   sys2,
+};
+
+struct ec_thermalzone {
+   u8 channel,
+  addr,
+  cmd,
+  status,
+  fancode,
+  temp;
+};
+
+/* Tacho */
+#define EC_MAX_IO_FAN  3
+
+/* Voltage */
+struct volt_item {
+   u8 did;
+   const char *name;
+   int factor;
+   bool visible;
+};
+
+static struct volt_item ec_volt_table[] = {
+   {
+   .did = adcmosbat,
+   .name = "BAT CMOS",
+   },
+   {
+   .did = adcbat,
+   .name = "BAT",
+   },
+   {
+   .did = adc5vs0,
+   .name = "5V S0",
+   },
+   {
+   .did = adv5vs5,
+   .name = "5V S5",
+   },
+   {
+   .did = adc33vs0,
+   .name = "3V3 S0",
+   },
+   {
+   .did = adc33vs5,
+   .name = "3V3 S5",
+   },
+   {
+   .did = adv12vs0,
+   .name = "12V S0",
+   },
+   {
+   .did = adcvcorea,
+   .name = "Vcore A",
+   },
+   {
+   .did = adcvcoreb,
+   .name = "Vcore B",
+   },
+   {
+   .did = adcdc,
+   .name = "DC",
+   },
+   {
+   .did = adcdcstby,
+   .name = "DC Standby",
+   },
+   {
+   .did = adcdcother,
+   .name = "DC Other",
+   },
+};
+
+static int imanager2_volt_get_value_by_io(struct imanager2 *ec, int index,
+ u8 *buf)
+{
+   u8 item = ec->table.devid2itemnum[ec_volt_table[index].did];
+   u8 pin = ec->table.pinnum[item];
+   u8 portnum;
+   int ret;
+
+   ret = imanager2_io_read(EC_CMD_ADC_INDEX, pin, &portnum, 1);
+   if (ret)
+   return ret;
+   if (portnum == EC_ERROR)
+   return -ENXIO;
+
+   ret = imanager2_io_read_byte_without_offset(EC_CMD_ADC_READ_LSB,
+   &buf[1]);
+   if (ret)
+   return ret;
+
+   ret = imanager2_io_read_byte_without_offset(EC_CMD_ADC_READ_MSB,
+   

[PATCH 2/5] mfd: imanager2: Add Advantech EC APIs support for IT8516/18/28

2014-06-23 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/mfd/imanager2_ec.c | 1219 
 1 file changed, 1219 insertions(+)
 create mode 100644 drivers/mfd/imanager2_ec.c

diff --git a/drivers/mfd/imanager2_ec.c b/drivers/mfd/imanager2_ec.c
new file mode 100644
index 000..ec0bcbf
--- /dev/null
+++ b/drivers/mfd/imanager2_ec.c
@@ -0,0 +1,1219 @@
+/*
+ * imanager2_ec.c - MFD accessing driver of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+
+#define EC_UDELAY_TIME 50
+#define EC_MAX_TIMEOUT_COUNT   1000
+
+static int wait_obf(void)
+{
+   int i;
+   for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+   /* wait output buffer full flag set */
+   if (inb(EC_IO_PORT_CMD) & OBF_MASK)
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   }
+
+   return -EBUSY;
+}
+
+int inb_after_obf(u8 *data)
+{
+   int ret = wait_obf();
+   if (ret)
+   return ret;
+   *data = inb(EC_IO_PORT_DATA);
+   return 0;
+}
+EXPORT_SYMBOL(inb_after_obf);
+
+static int wait_ibc(void)
+{
+   int i;
+   for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+   /* wait input buffer full flag clear */
+   if (!(inb(EC_IO_PORT_CMD) & IBF_MASK))
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   }
+
+   return -EBUSY;
+}
+
+int outb_after_ibc(u16 port, u8 data)
+{
+   int ret = wait_ibc();
+   if (ret)
+   return ret;
+   outb(data, port);
+   return 0;
+}
+EXPORT_SYMBOL(outb_after_ibc);
+
+static int imanager2_read_mailbox(struct imanager2 *ec, u8 offset, u8 *data)
+{
+   if (ec->flag & EC_FLAG_IO_MAILBOX) {
+   int ret = wait_ibc();
+   if (ret)
+   return ret;
+   inb(EC_IO_PORT_DATA);
+   outb(offset + EC_IO_CMD_READ_OFFSET, EC_IO_PORT_CMD);
+
+   return inb_after_obf(data);
+   } else {
+   outb(offset, EC_ITE_PORT_OFS);
+   *data = inb(EC_ITE_PORT_DATA);
+   }
+
+   return 0;
+}
+
+static int imanager2_write_mailbox(struct imanager2 *ec, u8 offset, u8 data)
+{
+   if (ec->flag & EC_FLAG_IO_MAILBOX) {
+   int ret = outb_after_ibc(EC_IO_PORT_CMD,
+offset + EC_IO_CMD_WRITE_OFFSET);
+   if (ret)
+   return ret;
+
+   return outb_after_ibc(EC_IO_PORT_DATA, data);
+   } else {
+   outb(offset, EC_ITE_PORT_OFS);
+   outb(data, EC_ITE_PORT_DATA);
+   }
+
+   return 0;
+}
+
+static int imanager2_wait_cmd_clear(struct imanager2 *ec)
+{
+   u8 cmd;
+   int i, ret;
+
+   for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+   ret = imanager2_read_mailbox(ec, EC_MAILBOX_OFFSET_CMD, &cmd);
+   if (ret)
+   return ret;
+   if (cmd == 0x00)
+   return 0;
+
+   udelay(EC_UDELAY_TIME);
+   }
+
+   return -EBUSY;
+}
+
+int imanager2_clear_mailbox(struct imanager2 *ec)
+{
+   int ret = imanager2_wait_cmd_clear(ec);
+   if (ret)
+   return ret;
+
+   return imanager2_write_mailbox(ec, EC_MAILBOX_OFFSET_CMD,
+  EC_CMD_MAILBOX_CLEAR_ALL);
+}
+EXPORT_SYMBOL(imanager2_clear_mailbox);
+
+int imanager2_read_mailbox_buffer(struct imanager2 *ec, u8 cmd, u8 para,
+ u8 *data, int len)
+{
+   int ret, i;
+   u8 status;
+
+   ret = imanager2_wait_cmd_clear(ec);
+   if (ret)
+   return ret;
+
+   ret = imanager2_write_mailbox(ec, EC_MAILBOX_OFFSET_PARA, para);
+   if (ret)
+   return ret;
+
+   ret = imanager2_write_mailbox(ec, EC_MAILBOX_OFFSET_CMD, cmd);
+   if (ret)
+   return ret;
+
+   ret = imanager2_wait_cmd_clear(ec);
+   if (ret)
+   return ret;
+
+   ret = imanager2_read_mailbox(ec, EC_MAILBOX_OFFSET_STATUS, &status);
+   if (ret)
+   return ret;
+   if (status != EC_MAILBOX_STATUS_SUCCESS)
+   return -ENXIO;
+
+   fo

[PATCH 5/5] i2c: imanager2: add support for IT8516/18/28

2014-06-23 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/i2c/busses/Kconfig |   8 ++
 drivers/i2c/busses/Makefile|   1 +
 drivers/i2c/busses/imanager2_i2c.c | 261 +
 3 files changed, 270 insertions(+)
 create mode 100644 drivers/i2c/busses/imanager2_i2c.c

diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index c94db1c..8aad058 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -8,6 +8,14 @@ menu "I2C Hardware Bus support"
 comment "PC SMBus host controller drivers"
depends on PCI
 
+config I2C_IMANAGER2
+   tristate "Support for Advantech iManager2 EC I2C"
+   select MFD_CORE
+   select MFD_IMANAGER2
+   depends on I2C=y
+   help
+ Support for the Advantech iManager2 EC I2C.
+
 config I2C_ALI1535
tristate "ALI 1535"
depends on PCI
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 18d18ff..8a2a26b 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_I2C_OCTEON)  += i2c-octeon.o
 obj-$(CONFIG_I2C_XILINX)   += i2c-xiic.o
 obj-$(CONFIG_I2C_XLR)  += i2c-xlr.o
 obj-$(CONFIG_I2C_RCAR) += i2c-rcar.o
+obj-$(CONFIG_I2C_IMANAGER2)+= imanager2_i2c.o
 
 # External I2C/SMBus adapter drivers
 obj-$(CONFIG_I2C_DIOLAN_U2C)   += i2c-diolan-u2c.o
diff --git a/drivers/i2c/busses/imanager2_i2c.c 
b/drivers/i2c/busses/imanager2_i2c.c
new file mode 100644
index 000..bb83155
--- /dev/null
+++ b/drivers/i2c/busses/imanager2_i2c.c
@@ -0,0 +1,261 @@
+/*
+ * imanager2_i2c.c - I2C interface for Advantech EC IT8516/18/28 driver
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#define DRV_NAME   "imanager2_i2c"
+#define DRV_VERSION"4.0.1"
+
+#define EC_I2C_SMB_DEV_MAX 8
+
+struct imanager2_i2c {
+   struct i2c_adapter adapter;
+   struct imanager2 *ec;
+   enum ec_device_id did;
+};
+
+struct imanager2_i2c_drv {
+   struct imanager2_i2c *devs[EC_I2C_SMB_DEV_MAX];
+   int devcount;
+};
+
+static int imanager2_smb_access(struct i2c_adapter *adap, u16 addr,
+   unsigned short flags, char read_write,
+   u8 command, int size,
+   union i2c_smbus_data *data)
+{
+   struct imanager2_i2c *i2cdev = i2c_get_adapdata(adap);
+   int ret;
+   u8 protocol = 0, *wdata = NULL, wlen = 0, *rdata = NULL, *rlen = NULL;
+   u8 buf[EC_MAILBOX_SMBI2C_DATA_LENGTH];
+
+   addr <<= 1;
+
+   switch (size) {
+   case I2C_SMBUS_QUICK:
+   if (read_write == I2C_SMBUS_WRITE)
+   protocol = EC_CMD_MALLBOX_SMBUS_WRITE_QUICK;
+   else
+   protocol = EC_CMD_MALLBOX_SMBUS_READ_QUICK;
+   break;
+   case I2C_SMBUS_BYTE:
+   if (read_write == I2C_SMBUS_WRITE) {
+   protocol = EC_CMD_MALLBOX_SMBUS_SEND_BYTE;
+   wdata = &data->byte;
+   wlen = 1;
+   } else {
+   protocol = EC_CMD_MALLBOX_SMBUS_RECEIVE_BYTE;
+   rdata = &data->byte;
+   *rlen = 1;
+   }
+   break;
+   case I2C_SMBUS_BYTE_DATA:
+   if (read_write == I2C_SMBUS_WRITE) {
+   protocol = EC_CMD_MALLBOX_SMBUS_WRITE_BYTE;
+   wdata = &data->byte;
+   wlen = 1;
+   } else {
+   protocol = EC_CMD_MALLBOX_SMBUS_READ_BYTE;
+   rdata = &data->byte;
+   *rlen = 1;
+   }
+   break;
+   case I2C_SMBUS_WORD_DATA:
+   if (read_write == I2C_SMBUS_WRITE) {
+   protocol = EC_CMD_MALLBOX_SMBUS_WRITE_WORD;
+   wdata = (u8 *)&data->word;
+   wlen = 2;
+   } else {
+   protocol = EC_CMD_MALLBOX_SMBUS_READ_WORD;
+   rdata = (u8 *)&data->word;
+   *rlen = 2;
+  

[PATCH 1/5] mfd: imanager2: Add defines support for IT8516/18/28

2014-06-23 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 include/linux/mfd/advantech/imanager2.h|  59 +
 include/linux/mfd/advantech/imanager2_ec.h | 391 +
 2 files changed, 450 insertions(+)
 create mode 100644 include/linux/mfd/advantech/imanager2.h
 create mode 100644 include/linux/mfd/advantech/imanager2_ec.h

diff --git a/include/linux/mfd/advantech/imanager2.h 
b/include/linux/mfd/advantech/imanager2.h
new file mode 100644
index 000..80808d1
--- /dev/null
+++ b/include/linux/mfd/advantech/imanager2.h
@@ -0,0 +1,59 @@
+/*
+ * imanager2.h - MFD chip information defines of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __IMANAGER2_H__
+#define __IMANAGER2_H__
+
+#include 
+#include "imanager2_ec.h"
+
+#define EC_FLAG_IO 0
+#define EC_FLAG_IO_MAILBOX (1 << 0)
+#define EC_FLAG_MAILBOX(1 << 1)
+
+enum chips {
+   it8516 = 0x8516,
+   it8518 = 0x8518,
+   it8528 = 0x8528,
+};
+
+struct ec_table {
+   u8 devid2itemnum[EC_MAX_DEVICE_ID_NUM];
+   u8 pinnum[EC_MAX_ITEM_NUM];
+   u8 devid[EC_MAX_ITEM_NUM];
+   u8 active_polarity[EC_MAX_ITEM_NUM];
+};
+
+struct ec_version {
+   u16 kernel_ver, chip_code, proj_id, proj_ver;
+};
+
+struct ec_version_info {
+   char prj_name[EC_MAX_LEN_PROJECT_NAME + 1]; /* strlen + '\0' */
+   struct ec_version version;
+};
+
+struct imanager2 {
+   u16 id;
+   u32 flag;
+   struct mutex lock;  /* protects io */
+   struct ec_version_info info;
+   struct ec_table table;
+};
+
+#endif /* __IMANAGER2_H__ */
diff --git a/include/linux/mfd/advantech/imanager2_ec.h 
b/include/linux/mfd/advantech/imanager2_ec.h
new file mode 100644
index 000..fd03de5
--- /dev/null
+++ b/include/linux/mfd/advantech/imanager2_ec.h
@@ -0,0 +1,391 @@
+/*
+ * imanager2_ec.h - MFD driver defines of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __IMANAGER2_EC_H__
+#define __IMANAGER2_EC_H__
+
+/*
+ * Definition
+ */
+#define EC_SIO_CMD 0x29C
+#define EC_SIO_DATA0x29D
+
+/* Access Mailbox */
+#define EC_IO_PORT_CMD 0x29A
+#define EC_IO_PORT_DATA0x299
+
+#define EC_IO_CMD_READ_OFFSET  0xA0
+#define EC_IO_CMD_WRITE_OFFSET 0x50
+
+#define EC_ITE_PORT_OFS0x29E
+#define EC_ITE_PORT_DATA   0x29F
+
+#define EC_TABLE_ITEM_UNUSED   0xFF
+#define EC_TABLE_DID_NODEV 0x00
+#define EC_TABLE_HWP_NODEV 0xFF
+#define EC_TABLE_NOITEM0xFF
+
+#define EC_ERROR   0xFF
+
+#define EC_RAM_BANK_SIZE   32  /* 32 bytes size for each bank. */
+#define EC_RAM_BUFFER_SIZE 256 /* 32 bytes * 8 banks = 256 bytes */
+
+#define EC_CMD_AUTHENTICATION  0x30
+#define EC_MAX_ITEM_NUM32
+#define EC_MAX_DEVICE_ID_NUM   0xFF
+#define EC_MAX_LEN_PROJECT_NAME8
+#define EC_MAX_LEN_FW_VERSION  9
+
+/*
+ * CMD - IO
+ */
+/* ADC */
+#define EC_CMD_ADC_INDEX   0x15
+#define EC_CMD_ADC_READ_LSB0x16
+#define EC_CMD_ADC_READ_MSB0x1F
+/* HW Control Table */
+#define EC_CMD_HWCTRLTABLE_INDEX   0x20
+#define EC_CMD_HWCTRLTABLE_GET_PIN_NUM 0x21
+#define EC_CMD_HWCTRLTABLE_GET_DEVICE_ID   0x22
+#define EC_CMD_HWCTRLTABLE_GET_PIN_ACTIVE_POLARITY 0x23
+/* ACPI RAM */
+#define EC_CMD_ACPIRAM_READ0x80
+#define EC_CMD_ACPIRAM_WRITE   0x81
+/* Extend RAM */
+#define EC_CMD_EXTRAM_READ  

[PATCH 3/5] mfd: imanager2: Add Core supports for IT8516/18/28

2014-06-23 Thread Wei-Chun Pan
Signed-off-by: Wei-Chun Pan 
---
 drivers/mfd/Kconfig  |   6 +
 drivers/mfd/Makefile |   2 +
 drivers/mfd/imanager2_core.c | 276 +++
 3 files changed, 284 insertions(+)
 create mode 100644 drivers/mfd/imanager2_core.c

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3383412..48b063f 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -10,6 +10,12 @@ config MFD_CORE
select IRQ_DOMAIN
default n
 
+config MFD_IMANAGER2
+   tristate "Support for Advantech iManager2 EC ICs"
+   select MFD_CORE
+   help
+ Support for Advantech iManager2 EC ICs
+
 config MFD_CS5535
tristate "AMD CS5535 and CS5536 southbridge core functions"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 2851275..10c64ae 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -166,3 +166,5 @@ obj-$(CONFIG_MFD_RETU)  += retu-mfd.o
 obj-$(CONFIG_MFD_AS3711)   += as3711.o
 obj-$(CONFIG_MFD_AS3722)   += as3722.o
 obj-$(CONFIG_MFD_STW481X)  += stw481x.o
+imanager2-objs := imanager2_core.o imanager2_ec.o
+obj-$(CONFIG_MFD_IMANAGER2)+= imanager2.o
diff --git a/drivers/mfd/imanager2_core.c b/drivers/mfd/imanager2_core.c
new file mode 100644
index 000..1a45f09
--- /dev/null
+++ b/drivers/mfd/imanager2_core.c
@@ -0,0 +1,276 @@
+/*
+ * imanager2_core.c - MFD core driver of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+
+#define DRV_NAME   "imanager2"
+#define DRV_VERSION"4.0.1"
+
+static struct mfd_cell imanager2_cells[] = {
+   {.name = "imanager2_hwm",},
+   {.name = "imanager2_i2c",},
+};
+
+static int imanager2_authentication(struct imanager2 *ec)
+{
+   u8 tmp;
+   int ret;
+
+   mutex_lock(&ec->lock);
+
+   if (inb(EC_IO_PORT_CMD) == 0xFF && inb(EC_IO_PORT_DATA) == 0xFF) {
+   ret = -ENODEV;
+   goto unlock;
+   }
+
+   if (inb(EC_IO_PORT_CMD) & OBF_MASK)
+   inb(EC_IO_PORT_DATA);   /* initial OBF */
+
+   if (outb_after_ibc(EC_IO_PORT_CMD, EC_CMD_AUTHENTICATION)) {
+   ret = -ENODEV;
+   goto unlock;
+   }
+
+   ret = inb_after_obf(&tmp);
+
+unlock:
+   mutex_unlock(&ec->lock);
+
+   if (ret)
+   return ret;
+
+   if (tmp != 0x95)
+   return -ENODEV;
+
+   return 0;
+}
+
+#define EC_ITE_CHIPID_H8   0x20
+#define EC_ITE_CHIPID_L8   0x21
+
+static int imanager2_get_chip_type(struct imanager2 *ec)
+{
+   mutex_lock(&ec->lock);
+
+   outb(EC_ITE_CHIPID_H8, EC_SIO_CMD);
+   ec->id = inb(EC_SIO_DATA) << 8;
+   outb(EC_ITE_CHIPID_L8, EC_SIO_CMD);
+   ec->id |= inb(EC_SIO_DATA);
+
+   mutex_unlock(&ec->lock);
+
+   switch (ec->id) {
+   case it8516:
+   case it8518:
+   ec->flag = EC_FLAG_IO;
+   break;
+   case it8528:
+   ec->flag |= EC_FLAG_IO_MAILBOX;
+   break;
+   default:
+   return -ENODEV;
+   }
+
+   return 0;
+}
+
+/*
+ * EC provides IO channel and ITE mailbox ways to access mailbox. IO channel is
+ * a common way to access mailbox, but IET mailbox way is much faster than IO
+ * channel. We prefer ITE mailbox if firmware supports. Source kernel code
+ * X11_05 the first firmware version that supports ITE mailbox.
+ */
+#define EC_CHIPFW_SUPP_ITEMAILBOX  0x1105
+
+static int imanager2_get_info(struct imanager2 *ec)
+{
+   int ret;
+   u8 *tmp = (u8 *)&ec->info.version.kernel_ver;
+
+   mutex_lock(&ec->lock);
+
+   ret = imanager2_io_read(EC_CMD_ACPIRAM_READ,
+   EC_ACPIRAM_ADDR_KERNEL_MAJOR_VERSION, &tmp[0],
+   2);
+
+   if (ret)
+   goto unlock;
+
+   if (ec->info.version.kernel_ver >= EC_CHIPFW_SUPP_ITEMAILBOX) {
+   ec->flag |= EC_FLAG_MAILBOX;
+   ret = imanager2_get_firmware_version_and_project_name(
+   ec, ec->info.pr

[PATCH 3/3] i2c: iManager2: add support for IT8516/18/28

2014-05-28 Thread Wei-Chun Pan
Advantech's new module comes equipped with "iManager" - an embedded 
controller (EC), providing embedded features for system integrators to increase 
reliability and simplify integration.
This patch add the MFD driver for enabling Advantech iManager V2.0 chipset. 
Available functions support I2C base on ITE-IT85XX chip. These functions are 
tested on Advantech SOM-5892 board. All the embedded functions are configured 
by a utility. Advantech has done all the hard work for user with the release of 
a suite of Software APIs.
These provide not only the underlying drivers required but also a rich set 
of user-friendly, intelligent and integrated interfaces, which speeds 
development, enhances security and offers add-on value for Advantech platforms.

Signed-off-by: Wei-Chun Pan Developer 
---
 drivers/i2c/busses/Kconfig |   8 ++
 drivers/i2c/busses/Makefile|   1 +
 drivers/i2c/busses/imanager2_i2c.c | 257 +
 drivers/i2c/busses/imanager2_i2c.h |  38 ++
 4 files changed, 304 insertions(+)
 mode change 100644 => 100755 drivers/i2c/busses/Kconfig
 mode change 100644 => 100755 drivers/i2c/busses/Makefile
 create mode 100755 drivers/i2c/busses/imanager2_i2c.c
 create mode 100755 drivers/i2c/busses/imanager2_i2c.h

diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
old mode 100644
new mode 100755
index c94db1c..8aad058
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -8,6 +8,14 @@ menu "I2C Hardware Bus support"
 comment "PC SMBus host controller drivers"
depends on PCI
 
+config I2C_IMANAGER2
+   tristate "Support for Advantech iManager2 EC I2C"
+   select MFD_CORE
+   select MFD_IMANAGER2
+   depends on I2C=y
+   help
+ Support for the Advantech iManager2 EC I2C.
+
 config I2C_ALI1535
tristate "ALI 1535"
depends on PCI
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
old mode 100644
new mode 100755
index 18d18ff..8a2a26b
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_I2C_OCTEON)  += i2c-octeon.o
 obj-$(CONFIG_I2C_XILINX)   += i2c-xiic.o
 obj-$(CONFIG_I2C_XLR)  += i2c-xlr.o
 obj-$(CONFIG_I2C_RCAR) += i2c-rcar.o
+obj-$(CONFIG_I2C_IMANAGER2)+= imanager2_i2c.o
 
 # External I2C/SMBus adapter drivers
 obj-$(CONFIG_I2C_DIOLAN_U2C)   += i2c-diolan-u2c.o
diff --git a/drivers/i2c/busses/imanager2_i2c.c 
b/drivers/i2c/busses/imanager2_i2c.c
new file mode 100755
index 000..6510dca
--- /dev/null
+++ b/drivers/i2c/busses/imanager2_i2c.c
@@ -0,0 +1,257 @@
+/* imanager2_i2c.c - I2C interface for Advantech EC IT8516/18/28 driver
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include "imanager2_i2c.h"
+
+#define DRV_NAME   CHIP_NAME "_i2c"
+#define DRV_VERSION"0.2.4"
+
+struct it85xx_i2c {
+   struct i2c_adapter adapter;
+   struct it85xx *ec;
+   enum ec_device_id did;
+};
+
+struct it85xx_i2c_drv {
+   struct it85xx_i2c *devs[EC_I2C_SMB_DEV_MAX];
+   int devcount;
+};
+
+static int it85xx_smb_access(struct i2c_adapter *adap, u16 addr,
+unsigned short flags, char read_write, u8 command,
+int size, union i2c_smbus_data *data)
+{
+   struct it85xx_i2c *i2cdev = i2c_get_adapdata(adap);
+   int ret = 0;
+   u8 rlen = 0;
+
+   addr <<= 1;
+
+   spin_lock(&i2cdev->ec->lock);
+
+   switch (size) {
+   case I2C_SMBUS_QUICK:
+   pr_info("I2C_SMBUS_QUICK\n");
+   if (read_write == I2C_SMBUS_WRITE)
+   ret = ec_smbus_transmit_routine(
+   i2cdev->ec, i2cdev->did,
+   EC_CMD_MALLBOX_SMBUS_WRITE_QUICK,
+   (u8)addr, 0, NULL, 0, NULL, NULL, 0);
+   else
+   ret = ec_smbus_transmit_routine(
+   i2cdev->ec, i2cdev->did,
+   EC_CMD_MALLB

[PATCH 1/3] mfd: iManager2: Add support for IT8516/18/28

2014-05-28 Thread Wei-Chun Pan
Advantech's new module comes equipped with "iManager" - an embedded 
controller (EC), providing embedded features for system integrators to increase 
reliability and simplify integration.
This patch add the MFD driver for enabling Advantech iManager V2.0 chipset. 
Available functions support to recognize ITE-IT85XX and basic control APIs. 
These functions are tested on Advantech SOM-5892 board. All the embedded 
functions are configured by a utility. Advantech has done all the hard work for 
user with the release of a suite of Software APIs.
These provide not only the underlying drivers required but also a rich set 
of user-friendly, intelligent and integrated interfaces, which speeds 
development, enhances security and offers add-on value for Advantech platforms.

Signed-off-by: Wei-Chun Pan Developer 
---
 drivers/mfd/Kconfig|6 +
 drivers/mfd/Makefile   |2 +
 drivers/mfd/imanager2_core.c   |  316 
 drivers/mfd/imanager2_ec.c | 1093 
 include/linux/mfd/advantech/imanager2.h|   61 ++
 include/linux/mfd/advantech/imanager2_ec.h |  389 ++
 6 files changed, 1867 insertions(+)
 mode change 100644 => 100755 drivers/mfd/Kconfig
 mode change 100644 => 100755 drivers/mfd/Makefile
 create mode 100755 drivers/mfd/imanager2_core.c
 create mode 100755 drivers/mfd/imanager2_ec.c
 create mode 100755 include/linux/mfd/advantech/imanager2.h
 create mode 100755 include/linux/mfd/advantech/imanager2_ec.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
old mode 100644
new mode 100755
index 3383412..48b063f
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -10,6 +10,12 @@ config MFD_CORE
select IRQ_DOMAIN
default n
 
+config MFD_IMANAGER2
+   tristate "Support for Advantech iManager2 EC ICs"
+   select MFD_CORE
+   help
+ Support for Advantech iManager2 EC ICs
+
 config MFD_CS5535
tristate "AMD CS5535 and CS5536 southbridge core functions"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
old mode 100644
new mode 100755
index 2851275..10c64ae
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -166,3 +166,5 @@ obj-$(CONFIG_MFD_RETU)  += retu-mfd.o
 obj-$(CONFIG_MFD_AS3711)   += as3711.o
 obj-$(CONFIG_MFD_AS3722)   += as3722.o
 obj-$(CONFIG_MFD_STW481X)  += stw481x.o
+imanager2-objs := imanager2_core.o imanager2_ec.o
+obj-$(CONFIG_MFD_IMANAGER2)+= imanager2.o
diff --git a/drivers/mfd/imanager2_core.c b/drivers/mfd/imanager2_core.c
new file mode 100755
index 000..786853d
--- /dev/null
+++ b/drivers/mfd/imanager2_core.c
@@ -0,0 +1,316 @@
+/* imanager2_core.c - MFD core driver of Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+
+#define DRV_NAME   CHIP_NAME
+#define DRV_VERSION"0.2.2"
+
+static struct platform_device *pdev;
+
+static struct mfd_cell it85xx_devs[] = {
+   { .name = DRV_NAME "_hwm", },
+   { .name = DRV_NAME "_i2c", },
+};
+
+static int ec_authentication(struct it85xx *ec)
+{
+   u8 tmp;
+   int ret = 0;
+
+   spin_lock(&ec->lock);
+
+   if (inb(EC_IO_PORT_CMD) == 0xFF && inb(EC_IO_PORT_DATA) == 0xFF) {
+   ret = -ENODEV;
+   goto out;
+   }
+
+   if ((inb(EC_IO_PORT_CMD) & OBF_MASK) != 0)
+   inb(EC_IO_PORT_DATA);   /* initial OBF */
+
+   if (outb_after_ibc(EC_IO_PORT_CMD, EC_CMD_AUTHENTICATION) != 0) {
+   ret = -ENODEV;
+   goto out;
+   }
+
+   if (inb_after_obf(&tmp) != 0)
+   ret = -EFAULT;
+
+out:
+   spin_unlock(&ec->lock);
+
+   if (ret != 0)
+   return ret;
+
+   if (tmp != 0x95)
+   return -EOPNOTSUPP;
+
+   return 0;
+}
+
+static int ec_get_chip_type(struct it85xx *ec)
+{
+   spin_lock(&ec->lock);
+
+   outb(0x20, EC_SIO_CMD);
+   ec->type = (inb(EC_SIO_DATA) << 8);
+   outb(0x21, EC_SIO_CMD);
+   ec->type |= inb(EC_SIO_DATA);
+
+   pr_info(&

[PATCH 2/3] hwmon: (iManager2) Add support for IT8516/18/28

2014-05-28 Thread Wei-Chun Pan
Advantech's new module comes equipped with "iManager" - an embedded 
controller (EC), providing embedded features for system integrators to increase 
reliability and simplify integration.
This patch add the MFD driver for enabling Advantech iManager V2.0 chipset. 
Available functions support HW Monitor base on ITE-IT85XX chip. These functions 
are tested on Advantech SOM-5892 board. All the embedded functions are 
configured by a utility. Advantech has done all the hard work for user with the 
release of a suite of Software APIs.
These provide not only the underlying drivers required but also a rich set 
of user-friendly, intelligent and integrated interfaces, which speeds 
development, enhances security and offers add-on value for Advantech platforms.

Signed-off-by: Wei-Chun Pan Developer 
---
 drivers/hwmon/Kconfig |   7 +
 drivers/hwmon/Makefile|   1 +
 drivers/hwmon/imanager2_hwm.c | 635 ++
 drivers/hwmon/imanager2_hwm.h | 118 
 4 files changed, 761 insertions(+)
 mode change 100644 => 100755 drivers/hwmon/Kconfig
 mode change 100644 => 100755 drivers/hwmon/Makefile
 create mode 100755 drivers/hwmon/imanager2_hwm.c
 create mode 100755 drivers/hwmon/imanager2_hwm.h

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
old mode 100644
new mode 100755
index bc196f4..d4aeab6
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -39,6 +39,13 @@ config HWMON_DEBUG_CHIP
 
 comment "Native drivers"
 
+config SENSORS_IMANAGER2
+   tristate "Support for Advantech iManager2 EC H.W. Monitor"
+   select MFD_CORE
+   select MFD_IMANAGER2
+   help
+ Support for the Advantech iManager2 EC H.W. Monitor
+
 config SENSORS_AB8500
tristate "AB8500 thermal monitoring"
depends on AB8500_GPADC && AB8500_BM
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
old mode 100644
new mode 100755
index c48f987..a2c8f07
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -146,6 +146,7 @@ obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)+= w83l786ng.o
 obj-$(CONFIG_SENSORS_WM831X)   += wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
+obj-$(CONFIG_SENSORS_IMANAGER2)+= imanager2_hwm.o
 
 obj-$(CONFIG_PMBUS)+= pmbus/
 
diff --git a/drivers/hwmon/imanager2_hwm.c b/drivers/hwmon/imanager2_hwm.c
new file mode 100755
index 000..48fe3dd
--- /dev/null
+++ b/drivers/hwmon/imanager2_hwm.c
@@ -0,0 +1,635 @@
+/* imanager2_hwm.c - HW Monitoring interface for Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "imanager2_hwm.h"
+
+#define DRV_NAME   CHIP_NAME "_hwm"
+#define DRV_VERSION"0.4.6"
+
+/* Voltage */
+static int ec_get_voltage_adc(struct it85xx *ec, int index,
+ u32 *volt_millivolt)
+{
+   int ret;
+   u8 portnum, tmp[2];
+
+   *volt_millivolt = 0;
+
+   spin_lock(&ec->lock);
+
+   if ((ec->flag & EC_F_MAILBOX) != 0) {
+   ret = ec_mailbox_read_buffer(ec, EC_CMD_MAILBOX_READ_HW_PIN,
+ec_volt_table[index].did, &tmp[0],
+2);
+   } else {
+   u8 pin = ec->table.pinnum[ec->table.devid2itemnum[
+   ec_volt_table[index].did]];
+
+   ret = ec_io_read(EC_CMD_ADC_INDEX, pin, &portnum, 1);
+   if (ret != 0)
+   goto unlock;
+   if (portnum == 0xFF) {
+   ret = -EFAULT;
+   goto unlock;
+   }
+
+   ret = ec_io_read_byte_without_offset(EC_CMD_ADC_READ_LSB,
+&tmp[1]);
+   if (ret != 0)
+   goto unlock;
+
+   ret = ec_io_read_byte_without_offset(EC_CMD_ADC_READ_MSB,
+&tmp[0]);
+   }
+unlock:
+   spin_unloc