Skip to contentSkip to conversation

mfd: iManager2: Add support for IT8516/18/28

PATCH In review Wei-Chun Pan 3 participants 5 messages May 29, 2014 – May 30, 2014

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

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

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.

---
 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
drivers/mfd/Kconfig +6 -0
old mode 100644
new mode 100755
index 3383412..48b063f
@@ -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
drivers/mfd/Makefile +2 -0
old mode 100644
new mode 100755
index 2851275..10c64ae
@@ -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
drivers/mfd/imanager2_core.c new file +316 -0

316 lines not on this page · raw message

drivers/mfd/imanager2_ec.c new file +1093 -0

1,093 lines not on this page · raw message

include/linux/mfd/advantech/imanager2.h new file +61 -0

61 lines not on this page · raw message

include/linux/mfd/advantech/imanager2_ec.h new file +389 -0

389 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw

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

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.

---
 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
drivers/hwmon/Kconfig +7 -0
old mode 100644
new mode 100755
index bc196f4..d4aeab6
@@ -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
drivers/hwmon/Makefile +1 -0
old mode 100644
new mode 100755
index c48f987..a2c8f07
@@ -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/
 
drivers/hwmon/imanager2_hwm.c new file +635 -0

635 lines not on this page · raw message

drivers/hwmon/imanager2_hwm.h new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#2 lore raw

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

Quoted 4 lines · 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.

Doesn't really follow SubmittingPatches guidelines; lines should be shorter.

Quoted 1 line

I assume 'Developer' is not part of your name ?

Quoted 10 lines
---
  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

Executable source files ? Note that checkpatch complains about this.

Quoted 8 lines
drivers/hwmon/Kconfig +0 -0
old mode 100644
new mode 100755
index bc196f4..d4aeab6

drivers/hwmon/Kconfig config HWMON_DEBUG_CHIP · line 45

Quoted 7 lines
drivers/hwmon/Kconfig +4 -0
@@ -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

It is customary to express this as dependency. Should be
depends on MFD_IMANAGER2

Quoted 35 lines
drivers/hwmon/Kconfig +3 -0
+	help
+	  Support for the Advantech iManager2 EC H.W. Monitor
+
  config SENSORS_AB8500
  	tristate "AB8500 thermal monitoring"
  	depends on AB8500_GPADC && AB8500_BM
drivers/hwmon/Makefile +1 -0
old mode 100644
new mode 100755
index c48f987..a2c8f07
@@ -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/
 
drivers/hwmon/imanager2_hwm.c new file +9 -0
new file mode 100755
index 0000000..48fe3dd
@@ -0,0 +1,635 @@
+/* imanager2_hwm.c - HW Monitoring interface for Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch <[email protected]>
+ *
+ * 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,

drivers/hwmon/imanager2_hwm.c line 19

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+ * 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
+

Not needed; see below.

drivers/hwmon/imanager2_hwm.c line 27

Quoted 8 lines
drivers/hwmon/imanager2_hwm.c new file +8 -0
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/mfd/advantech/imanager2.h>
+#include "imanager2_hwm.h"
+
+#define DRV_NAME	CHIP_NAME "_hwm"

Note that "it85xx" as chip name is a bit too generic. You don't support all chips starting with IT85, do you ? I would suggest to pick one of the supported chips as driver name.

drivers/hwmon/imanager2_hwm.c line 31

Quoted 4 lines
drivers/hwmon/imanager2_hwm.c new file +4 -0
+#define DRV_VERSION	"0.4.6"
+
+/* Voltage */
+static int ec_get_voltage_adc(struct it85xx *ec, int index,

Same applies to this structure, really.

drivers/hwmon/imanager2_hwm.c line 41

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+			      u32 *volt_millivolt)
+{
+	int ret;
+	u8 portnum, tmp[2];
+
+	*volt_millivolt = 0;
+
+	spin_lock(&ec->lock);
+
+	if ((ec->flag & EC_F_MAILBOX) != 0) {

The "!= 0" in those comparisons is really unnecessary. This is implied.

Quoted 61 lines
drivers/hwmon/imanager2_hwm.c new file +61 -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_unlock(&ec->lock);
+
+	if (ret != 0)
+		return ret;
+
+	*volt_millivolt = ((tmp[0] << 8 | tmp[1]) & EC_ADC_RESOLUTION_MAX) *
+			  ec_volt_table[index].factor *
+			  EC_ADC_VOLTAGE_VALUE_MAX / EC_ADC_RESOLUTION_MAX;
+
+	return 0;
+}
+
+static void ec_volt_init_once(struct it85xx *ec, int index)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ec_volt_table); i++) {
+		if (ec->table.devid2itemnum[ec_volt_table[i].did] !=
+							EC_TABLE_ITEM_UNUSED) {
+			ec_volt_table[i].factor = 1;
+			ec_volt_table[i].visible = 1;
+		} else if (ec->table.devid2itemnum[ec_volt_table[i].did + 1] !=
+							EC_TABLE_ITEM_UNUSED) {
+			ec_volt_table[i].did += 1;
+			ec_volt_table[i].factor = 2;
+			ec_volt_table[i].visible = 1;
+		} else if (ec->table.devid2itemnum[ec_volt_table[i].did + 2] !=
+							EC_TABLE_ITEM_UNUSED) {
+			ec_volt_table[i].did += 2;
+			ec_volt_table[i].factor = 10;
+			ec_volt_table[i].visible = 1;
+		} else {
+			ec_volt_table[i].visible = 0;
+		}
+	}
+}
+

drivers/hwmon/imanager2_hwm.c line 112

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+static ssize_t show_in(struct device *dev, struct device_attribute *dev_attr,
+		       char *buf)
+{
+	struct it85xx *ec = dev_get_drvdata(dev);
+	u32 val;
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	int ret = ec_get_voltage_adc(ec, i, &val);
+
+	if (ret != 0)
+		return (ssize_t)ret;

Unnecessary typecast.

Quoted 104 lines
drivers/hwmon/imanager2_hwm.c new file +104 -0
+
+	return sprintf(buf, "%u\n", val);
+}
+
+static ssize_t show_in_label(struct device *dev,
+			     struct device_attribute *dev_attr, char *buf)
+{
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	return sprintf(buf, "%s\n", ec_volt_table[i].name);
+}
+
+static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
+static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
+static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
+static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
+static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
+static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
+static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
+static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
+static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
+static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 9);
+static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 10);
+static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 11);
+
+static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_in_label, NULL, 0);
+static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_in_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_in_label, NULL, 2);
+static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_in_label, NULL, 3);
+static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_in_label, NULL, 4);
+static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_in_label, NULL, 5);
+static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_in_label, NULL, 6);
+static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_in_label, NULL, 7);
+static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_in_label, NULL, 8);
+static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_in_label, NULL, 9);
+static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, show_in_label, NULL, 10);
+static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, show_in_label, NULL, 11);
+
+static struct attribute *it85xx_volt_attrs[] = {
+	&sensor_dev_attr_in0_label.dev_attr.attr,
+	&sensor_dev_attr_in0_input.dev_attr.attr,
+
+	&sensor_dev_attr_in1_label.dev_attr.attr,
+	&sensor_dev_attr_in1_input.dev_attr.attr,
+
+	&sensor_dev_attr_in2_label.dev_attr.attr,
+	&sensor_dev_attr_in2_input.dev_attr.attr,
+
+	&sensor_dev_attr_in3_label.dev_attr.attr,
+	&sensor_dev_attr_in3_input.dev_attr.attr,
+
+	&sensor_dev_attr_in4_label.dev_attr.attr,
+	&sensor_dev_attr_in4_input.dev_attr.attr,
+
+	&sensor_dev_attr_in5_label.dev_attr.attr,
+	&sensor_dev_attr_in5_input.dev_attr.attr,
+
+	&sensor_dev_attr_in6_label.dev_attr.attr,
+	&sensor_dev_attr_in6_input.dev_attr.attr,
+
+	&sensor_dev_attr_in7_label.dev_attr.attr,
+	&sensor_dev_attr_in7_input.dev_attr.attr,
+
+	&sensor_dev_attr_in8_label.dev_attr.attr,
+	&sensor_dev_attr_in8_input.dev_attr.attr,
+
+	&sensor_dev_attr_in9_label.dev_attr.attr,
+	&sensor_dev_attr_in9_input.dev_attr.attr,
+
+	&sensor_dev_attr_in10_label.dev_attr.attr,
+	&sensor_dev_attr_in10_input.dev_attr.attr,
+
+	&sensor_dev_attr_in11_label.dev_attr.attr,
+	&sensor_dev_attr_in11_input.dev_attr.attr,
+
+	NULL
+};
+
+static umode_t it85xx_volt_mode(struct kobject *kobj, struct attribute *attr,
+				int index)
+{
+	struct sensor_device_attribute *sensor = container_of(
+			attr, struct sensor_device_attribute, dev_attr.attr);
+
+	if (ec_volt_table[sensor->index].visible == 0)
+		return 0;
+
+	return attr->mode;
+}
+
+static const struct attribute_group it85xx_volt_group = {
+	.attrs = it85xx_volt_attrs,
+	.is_visible = it85xx_volt_mode,
+};
+
+/* Current */
+static int ec_get_current_adc(struct it85xx *ec, int index,
+			      u32 *curr_milliampere)
+{
+	int ret;
+	u8 i, tmp[5];
+	u16 value, factor;
+	u32 baseunit;
+
+	*curr_milliampere = 0;

drivers/hwmon/imanager2_hwm.c line 226

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -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_curr_table[index].did,
+					     tmp, ARRAY_SIZE(tmp));
+	else
+		ret = -EOPNOTSUPP;
+

If the operation is not supported in this case, why is the attribute visible in the first place ?

drivers/hwmon/imanager2_hwm.c line 233

Quoted 7 lines
drivers/hwmon/imanager2_hwm.c new file +7 -0
+	spin_unlock(&ec->lock);
+
+	if (ret != 0)
+		return ret;
+
+	value = (tmp[0] << 8 | tmp[1]) & EC_ADC_RESOLUTION_MAX;
+	factor = tmp[2] << 8 | tmp[3];

I would suggest to use ( ) around the shift operations. Technically not necessary but makes it more obvious what is intended, and you do it elsewhere.

drivers/hwmon/imanager2_hwm.c line 240

Quoted 7 lines
drivers/hwmon/imanager2_hwm.c new file +7 -0
+	baseunit = 1;
+	for (i = 1; i < tmp[4]; i++)
+		baseunit *= 10;
+
+	*curr_milliampere = value * factor * baseunit *
+			    EC_ADC_CURRENT_VALUE_MAX / EC_ADC_RESOLUTION_MAX;
+

Would it make sense to use DIV_ROUND_CLOSEST() for those operations to improve accuracy ?

drivers/hwmon/imanager2_hwm.c line 253

Quoted 13 lines
drivers/hwmon/imanager2_hwm.c new file +13 -0
+	return 0;
+}
+
+static void ec_current_init_once(struct it85xx *ec)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ec_curr_table); i++)
+		if (ec->table.devid2itemnum[ec_curr_table[i].did] !=
+							EC_TABLE_ITEM_UNUSED)
+			ec_curr_table[i].visible = 1;
+		else
+			ec_curr_table[i].visible = 0;
		ec_curr_table[i].visible = ec->table.devid2itemnum[ec_curr_table[i].did] !=
							EC_TABLE_ITEM_UNUSED;

would be a bit simpler. Either case, there should be no need to ever set visible to 0.

Quoted 43 lines
drivers/hwmon/imanager2_hwm.c new file +43 -0
+}
+
+static ssize_t show_curr(struct device *dev, struct device_attribute *dev_attr,
+			 char *buf)
+{
+	struct it85xx *ec = dev_get_drvdata(dev);
+	u32 val;
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	int ret = ec_get_current_adc(ec, i, &val);
+
+	if (ret != 0)
+		return (ssize_t)ret;
+
+	return sprintf(buf, "%u\n", val);
+}
+
+static ssize_t show_curr_label(struct device *dev,
+			       struct device_attribute *dev_attr, char *buf)
+{
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	return sprintf(buf, "%s\n", ec_curr_table[i].name);
+}
+
+static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, show_curr, NULL, 0);
+static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_curr_label, NULL, 0);
+
+static struct attribute *it85xx_curr_attrs[] = {
+	&sensor_dev_attr_curr1_label.dev_attr.attr,
+	&sensor_dev_attr_curr1_input.dev_attr.attr,
+
+	NULL
+};
+
+static umode_t it85xx_curr_mode(struct kobject *kobj, struct attribute *attr,
+				int index)
+{
+	struct sensor_device_attribute *sensor = container_of(
+			attr, struct sensor_device_attribute, dev_attr.attr);
+
+	if (ec_curr_table[sensor->index].visible == 0)
+		return 0;
+
+	return attr->mode;

drivers/hwmon/imanager2_hwm.c line 306

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+}
+
+static const struct attribute_group it85xx_curr_group = {
+	.attrs = it85xx_curr_attrs,
+	.is_visible = it85xx_curr_mode,
+};
+
+/* Temperature */
+static int ec_get_thermal_temperature(struct it85xx *ec, int index,
+				      int *temp_millicelsius)

thermal_temperature seems to be a bit redundant. Is there another non-thermal temperature ? Just use 'temperature'.

Quoted 5 lines
drivers/hwmon/imanager2_hwm.c new file +5 -0
+{
+	int ret;
+	u8 tmp;
+
+	*temp_millicelsius = 0;

drivers/hwmon/imanager2_hwm.c line 321

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+
+	spin_lock(&ec->lock);
+	ret = ec_acpiram_read_byte(ec, ec_temp_table[index].zonetype_acpireg,
+				   &tmp);
+	spin_unlock(&ec->lock);
+
+	if (ret != 0)
+		return ret;
+
+	*temp_millicelsius = ((signed)tmp) * 1000;

Does this mean that tmp is really s8 and can be negative. If so, I don't think the 'signed' typecase will help; you might need (s8) instead. Please test.

Quoted 116 lines
drivers/hwmon/imanager2_hwm.c new file +116 -0
+
+	return 0;
+}
+
+static void ec_temp_init_once(struct it85xx *ec)
+{
+	int i, j, ret;
+	u8 tmltype;
+	struct ec_thermalzone zone;
+
+	for (i = 0; i < ARRAY_SIZE(ec_temp_table); i++)
+		ec_temp_table[i].visible = 0;
+
+	for (i = 0; i < EC_MAX_THERMAL_ZONE; i++) {
+		spin_lock(&ec->lock);
+
+		if ((ec->flag & EC_F_MAILBOX) != 0) {
+			int len = sizeof(struct ec_thermalzone);
+			ret = ec_read_thermalzone(ec, i, NULL, NULL,
+						  (u8 *)&zone, &len);
+		} else {
+			ret = ec_io_read(
+				EC_CMD_HWRAM_READ,
+				EC_HWRAM_ADDR_THERMAL_SOURCE_SMB_STATUS(i),
+				&zone.status, 1);
+		}
+
+		spin_unlock(&ec->lock);
+
+		if (ret != 0)
+			continue;
+
+		tmltype = (zone.status >> 5);
+
+		for (j = 0; j < ARRAY_SIZE(ec_temp_table); j++) {
+			if (tmltype == ec_temp_table[j].zonetype_value) {
+				ec_temp_table[j].visible = 1;
+				break;
+			}
+		}
+	}
+}
+
+static ssize_t show_temp(struct device *dev, struct device_attribute *dev_attr,
+			 char *buf)
+{
+	struct it85xx *ec = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	int val, ret;
+	ret = ec_get_thermal_temperature(ec, i, &val);
+
+	if (ret != 0)
+		return (ssize_t)ret;
+
+	return sprintf(buf, "%d\n", val);
+}
+
+static ssize_t show_temp_label(struct device *dev,
+			       struct device_attribute *dev_attr, char *buf)
+{
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	return sprintf(buf, "%s\n", ec_temp_table[i].name);
+}
+
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
+static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
+static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
+
+static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2);
+static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3);
+
+static struct attribute *it85xx_temp_attrs[] = {
+	&sensor_dev_attr_temp1_label.dev_attr.attr,
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+
+	&sensor_dev_attr_temp2_label.dev_attr.attr,
+	&sensor_dev_attr_temp2_input.dev_attr.attr,
+
+	&sensor_dev_attr_temp3_label.dev_attr.attr,
+	&sensor_dev_attr_temp3_input.dev_attr.attr,
+
+	&sensor_dev_attr_temp4_label.dev_attr.attr,
+	&sensor_dev_attr_temp4_input.dev_attr.attr,
+
+	NULL
+};
+
+static umode_t it85xx_temp_mode(struct kobject *kobj, struct attribute *attr,
+				int index)
+{
+	struct sensor_device_attribute *sensor = container_of(
+			attr, struct sensor_device_attribute, dev_attr.attr);
+
+	if (ec_temp_table[sensor->index].visible == 0)
+		return 0;
+
+	return attr->mode;
+}
+
+static const struct attribute_group it85xx_temp_group = {
+	.attrs = it85xx_temp_attrs,
+	.is_visible = it85xx_temp_mode,
+};
+
+/* Fan Speed */
+static int ec_get_fan_speed(struct it85xx *ec, int index, u32 *speed_rpm)
+{
+	int ret;
+	u8 tmp[2];
+
+	*speed_rpm = 0;
+
+	spin_lock(&ec->lock);

drivers/hwmon/imanager2_hwm.c line 447

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+
+	if ((ec->flag & EC_F_MAILBOX) != 0)
+		ret = ec_mailbox_read_buffer(ec, EC_CMD_MAILBOX_READ_HW_PIN,
+					     ec_fan_table[index].did,
+					     &tmp[0], 2);
+	else
+		ret = ec_io_read(EC_CMD_ACPIRAM_READ,
+				 ec_fan_table[index].fspeed_acpireg,
+				 &tmp[0], 2);
+

For exported functions those function names are quite generic. Might make sense to pick something a bit less generic.

Quoted 41 lines
drivers/hwmon/imanager2_hwm.c new file +41 -0
+	spin_unlock(&ec->lock);
+
+	if (ret != 0)
+		return ret;
+
+	if (tmp[0] == 0xFF && tmp[1] == 0xFF)
+		return -ENODEV;
+
+	*speed_rpm = (tmp[0] << 8) | tmp[1];
+
+	return 0;
+}
+
+static void ec_fan_init_once(struct it85xx *ec)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ec_fan_table); i++)
+		ec_fan_table[i].visible = 0;
+
+	if ((ec->flag & EC_F_MAILBOX) != 0) {
+		for (i = 0; i < ARRAY_SIZE(ec_fan_table); i++)
+			if (ec->table.devid2itemnum[ec_fan_table[i].did] !=
+							EC_TABLE_ITEM_UNUSED)
+				ec_fan_table[i].visible = 1;
+	} else {
+		int fnum, ret;
+		u8 tmp, fscr;
+
+		for (fnum = 0; fnum < EC_MAX_IO_FAN; fnum++) {
+			spin_lock(&ec->lock);
+			ret = ec_io_read(EC_CMD_HWRAM_READ,
+					 EC_HWRAM_ADDR_FAN_CONTROL(fnum),
+					 &tmp, 1);
+			spin_unlock(&ec->lock);
+
+			if (ret != 0)
+				continue;
+
+			fscr = (tmp >> 4) & 0x03;
+

drivers/hwmon/imanager2_hwm.c line 498

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+			switch (fscr) {
+			case 1:	/* tacho0 */
+			case 2:	/* tacho1 */
+			case 3:	/* tacho2 */
+				i = fscr - 1;
+				break;
+			default:
+				continue;
+			}
+
			if (!fscr)
				continue;
			i = fscr - 1;

would simpler and do the same. Actually you don't even need 'i'; you could just use fscr as index, after

			fscr--;

drivers/hwmon/imanager2_hwm.c line 501

Quoted 3 lines
drivers/hwmon/imanager2_hwm.c new file +3 -0
+			if (ec_fan_table[i].visible == 1)
+				continue;
+

Can you explain this logic a bit ?

Quoted 14 lines
drivers/hwmon/imanager2_hwm.c new file +14 -0
+			ec_fan_table[i].fspeed_acpireg =
+					EC_ACPIRAM_ADDR_FAN_SPEED_BASE(i);
+
+			ec_fan_table[i].visible = 1;
+		}
+	}
+}
+
+static ssize_t show_fan(struct device *dev, struct device_attribute *dev_attr,
+			char *buf)
+{
+	struct it85xx *ec = dev_get_drvdata(dev);
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	u32 val;

drivers/hwmon/imanager2_hwm.c line 525

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+	int ret = ec_get_fan_speed(ec, i, &val);
+
+	if (ret != 0)
+		return (ssize_t)ret;
+
+	return sprintf(buf, "%u\n", val);
+}
+
+static ssize_t show_fan_label(struct device *dev,
+			      struct device_attribute *dev_attr, char *buf)

The second-line alignment differs from function to function. Please make it consistent, preferably aligned with ( as suggested in coding style.

Quoted 23 lines
drivers/hwmon/imanager2_hwm.c new file +23 -0
+{
+	int i = to_sensor_dev_attr(dev_attr)->index;
+	return sprintf(buf, "%s\n", ec_fan_table[i].name);
+}
+
+static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
+
+static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, show_fan_label, NULL, 0);
+static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, show_fan_label, NULL, 1);
+static SENSOR_DEVICE_ATTR(fan3_label, S_IRUGO, show_fan_label, NULL, 2);
+
+static struct attribute *it85xx_fan_attrs[] = {
+	&sensor_dev_attr_fan1_label.dev_attr.attr,
+	&sensor_dev_attr_fan1_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan2_label.dev_attr.attr,
+	&sensor_dev_attr_fan2_input.dev_attr.attr,
+
+	&sensor_dev_attr_fan3_label.dev_attr.attr,
+	&sensor_dev_attr_fan3_input.dev_attr.attr,
+

drivers/hwmon/imanager2_hwm.c line 558

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+	NULL
+};
+
+static umode_t it85xx_fan_mode(struct kobject *kobj, struct attribute *attr,
+			       int index)
+{
+	struct sensor_device_attribute *sensor = container_of(
+			attr, struct sensor_device_attribute, dev_attr.attr);
+
+	if (ec_fan_table[sensor->index].visible == 0)

the '== 0' is really unnecessary here.

	if (!ec_fan_table[sensor->index].visible)

would be a better fit (and visible could be a boolean).

Quoted 26 lines
drivers/hwmon/imanager2_hwm.c new file +26 -0
+		return 0;
+
+	return attr->mode;
+}
+
+static const struct attribute_group it85xx_fan_group = {
+	.attrs = it85xx_fan_attrs,
+	.is_visible = it85xx_fan_mode,
+};
+
+/* HWM groups */
+static const struct attribute_group *it85xx_hwmon_groups[] = {
+	&it85xx_volt_group,
+	&it85xx_curr_group,
+	&it85xx_temp_group,
+	&it85xx_fan_group,
+
+	NULL
+};
+
+/* Module */
+static int it85xx_hwmon_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct it85xx *hwmon_ec;
+	struct device *hwmon_dev;

drivers/hwmon/imanager2_hwm.c line 594

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+
+	hwmon_ec = devm_kzalloc(dev, sizeof(struct it85xx), GFP_KERNEL);
+
+	if (hwmon_ec == NULL)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, hwmon_ec);
+
+	hwmon_ec = dev->parent->platform_data;
+

You lost me here. What is this for ? Why do you allocate this structure locally just to drop it and replace it with the one you got from the parent platform data ?

Quoted 6 lines
drivers/hwmon/imanager2_hwm.c new file +6 -0
+	ec_volt_init_once(hwmon_ec, 0);
+	ec_temp_init_once(hwmon_ec);
+	ec_current_init_once(hwmon_ec);
+	ec_fan_init_once(hwmon_ec);
+
+	hwmon_dev = devm_hwmon_device_register_with_groups(dev, CHIP_NAME,

drivers/hwmon/imanager2_hwm.c line 610

Quoted 10 lines
drivers/hwmon/imanager2_hwm.c new file +10 -0
+							   hwmon_ec,
+							   it85xx_hwmon_groups);
+
+	if (IS_ERR(hwmon_dev) != 0) {
+		pr_err("Could not register it85xx hwmon device\n");
+		return PTR_ERR(hwmon_dev);
+	}
+
+	pr_info("HWM driver v%s loaded\n", DRV_VERSION);
+

Can you drop all this noise ? Besides, you can use dev_info and dev_err in all cases and don't need to use pr_ functions.

drivers/hwmon/imanager2_hwm.c line 619

Quoted 9 lines
drivers/hwmon/imanager2_hwm.c new file +9 -0
+	return 0;
+}
+
+static int it85xx_hwmon_remove(struct platform_device *pdev)
+{
+	pr_info("HWM driver removed\n");
+
+	return 0;
+}

Please drop the remove function.

Quoted 42 lines
drivers/hwmon/imanager2_hwm.c new file +16 -0
+
+static struct platform_driver it85xx_hwmon_driver = {
+	.probe = it85xx_hwmon_probe,
+	.remove = it85xx_hwmon_remove,
+	.driver = {
+		.owner = THIS_MODULE,
+		.name  = DRV_NAME,
+	},
+};
+
+module_platform_driver(it85xx_hwmon_driver);
+
+MODULE_AUTHOR("Richard Vidal-Dorsch <richard.dorsch at advantech.com>");
+MODULE_DESCRIPTION("HW Monitoring interface for Advantech EC IT8516/18/28");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(DRV_VERSION);
drivers/hwmon/imanager2_hwm.h new file +20 -0
new file mode 100755
index 0000000..ccf4a4c
@@ -0,0 +1,118 @@
+/* imanager2_hwm.h - HW Monitoring interface for Advantech EC IT8516/18/28
+ * Copyright (C) 2014  Richard Vidal-Dorsch <[email protected]>
+ *
+ * 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_HWM_H__
+#define __IMANAGER2_HWM_H__
+

drivers/hwmon/imanager2_hwm.h line 30

Quoted 10 lines
drivers/hwmon/imanager2_hwm.h new file +10 -0
+/* ADC */
+#define EC_ADC_RESOLUTION_MAX		0x03FF	/* 10-bit */
+#define EC_ADC_VOLTAGE_VALUE_MAX	3000	/* max: 3.0 V */
+#define EC_ADC_CURRENT_VALUE_MAX	3000	/* max: 3.0 A */
+
+struct volt_item {
+	u8 did;
+	const char *name;
+	int factor;
+	int visible;

Any reason for not using bool for the 'visible' variables ?

Quoted 75 lines
drivers/hwmon/imanager2_hwm.h new file +75 -0
+};
+
+struct curr_item {
+	const u8 did;
+	const char *name;
+	int visible;
+};
+
+static struct volt_item ec_volt_table[] = {
+	{.did = adcmosbat, .name = "BAT CMOS", .factor = 0, .visible = 0},
+	{.did = adcbat, .name = "BAT", .factor = 0, .visible = 0},
+	{.did = adc5vs0, .name = "5V S0", .factor = 0, .visible = 0},
+	{.did = adv5vs5, .name = "5V S5", .factor = 0, .visible = 0},
+	{.did = adc33vs0, .name = "3V3 S0", .factor = 0, .visible = 0},
+	{.did = adc33vs5, .name = "3V3 S5", .factor = 0, .visible = 0},
+	{.did = adv12vs0, .name = "12V S0", .factor = 0, .visible = 0},
+	{.did = adcvcorea, .name = "Vcore A", .factor = 0, .visible = 0},
+	{.did = adcvcoreb, .name = "Vcore B", .factor = 0, .visible = 0},
+	{.did = adcdc, .name = "DC", .factor = 0, .visible = 0},
+	{.did = adcdcstby, .name = "DC Standby", .factor = 0, .visible = 0},
+	{.did = adcdcother, .name = "DC Other", .factor = 0, .visible = 0}
+};
+
+static struct curr_item ec_curr_table[] = {
+	{.did = adccurrent, .name = "IMON", .visible = 0}
+};
+
+/* Thermal */
+#define EC_MAX_THERMAL_ZONE	4
+
+#define EC_THERMAL_TYPE_NONE	0
+#define EC_THERMAL_TYPE_SYS1	1
+#define EC_THERMAL_TYPE_CPU	2
+#define EC_THERMAL_TYPE_SYS3	3
+#define EC_THERMAL_TYPE_SYS2	4
+
+struct temp_item {
+	const char *name;
+	const u8 zonetype_value;
+	u8 zonetype_acpireg;
+	int visible;
+};
+
+static struct temp_item ec_temp_table[] = {
+	{.name = "Temp SYS",
+	 .zonetype_value = EC_THERMAL_TYPE_SYS1,
+	 .zonetype_acpireg = EC_ACPIRAM_ADDR_LOCAL_TEMPERATURE(1),
+	 .visible = 0},
+	{.name = "Temp CPU",
+	 .zonetype_value = EC_THERMAL_TYPE_CPU,
+	 .zonetype_acpireg = EC_ACPIRAM_ADDR_REMOTE_TEMPERATURE(1),
+	 .visible = 0},
+	{.name = "Temp SYS3",
+	 .zonetype_value = EC_THERMAL_TYPE_SYS3,
+	 .zonetype_acpireg = EC_ACPIRAM_ADDR_LOCAL_TEMPERATURE(2),
+	 .visible = 0},
+	{.name = "Temp SYS2",
+	 .zonetype_value = EC_THERMAL_TYPE_SYS2,
+	 .zonetype_acpireg = EC_ACPIRAM_ADDR_REMOTE_TEMPERATURE(2),
+	 .visible = 0},
+};
+
+struct ec_thermalzone {
+	u8 channel,
+	   addr,
+	   cmd,
+	   status,
+	   fancode,
+	   temp;
+};
+
+/* Tacho */
+#define EC_MAX_IO_FAN	3
+
+struct fan_item {

drivers/hwmon/imanager2_hwm.h line 115

Quoted 10 lines
drivers/hwmon/imanager2_hwm.h new file +10 -0
+	const u8 did;
+	const char *name;
+	u8 fspeed_acpireg;
+	int visible;
+};
+
+static struct fan_item ec_fan_table[] = {
+	{.did = tacho0, .name = "Fan CPU", .fspeed_acpireg = 0, .visible = 0},
+	{.did = tacho1, .name = "Fan SYS", .fspeed_acpireg = 0, .visible = 0},
+	{.did = tacho2, .name = "Fan SYS2", .fspeed_acpireg = 0, .visible = 0},

No need to initialize static variables with 0.

drivers/hwmon/imanager2_hwm.h line 118

Quoted 4 lines
drivers/hwmon/imanager2_hwm.h new file +3 -0
+};
+
+#endif /* __IMANAGER2_HWM_H__ */

Please merge the include file into the source. There is no benefit of having it extra, and variable declarations in include files is a no-go.

Thanks,
Guenter

#3 lore raw

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

In reply to 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.

---
 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
drivers/i2c/busses/Kconfig +8 -0
old mode 100644
new mode 100755
index c94db1c..8aad058
@@ -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
drivers/i2c/busses/Makefile +1 -0
old mode 100644
new mode 100755
index 18d18ff..8a2a26b
@@ -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
drivers/i2c/busses/imanager2_i2c.c new file +257 -0

257 lines not on this page · raw message

drivers/i2c/busses/imanager2_i2c.h new file +38 -0
new file mode 100755
index 0000000..b9b836b
@@ -0,0 +1,38 @@
+/* imanager2_i2c.h - I2C interface for Advantech EC IT8516/18/28 driver
+ * Copyright (C) 2014  Richard Vidal-Dorsch <[email protected]>
+ *
+ * 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_I2C_H__
+#define __IMANAGER2_I2C_H__
+
+#define EC_I2C_SMB_DEV_MAX	8
+#define EC_I2C_BLOCK_MAX	32
+
+struct i2c_item {
+	const u8 did;
+	const char *name;
+};
+
+static struct i2c_item ec_i2c_table[] = {
+	{smboem0, "IT85xx SMBus0"},
+	{smboem1, "IT85xx SMBus1"},
+	{smboem2, "IT85xx SMBus2"},
+	{smbthermal0, "IT85xx Thermal0"},
+	{smbthermal1, "IT85xx Thermal1"},
+	{i2coem, "IT85xx I2COEM"}
+};
+
+#endif /* __IMANAGER2_I2C_H__ */
Signature · 1 line
1.9.1
#4 lore raw
In reply to Wei-Chun Pan

Couple of things on top of Guenter's comments.

drivers/mfd/Kconfig config MFD_CORE · line 15

Quoted 3 lines
drivers/mfd/Kconfig +3 -0
+config MFD_IMANAGER2
+	tristate "Support for Advantech iManager2 EC ICs"
+	select MFD_CORE

So you're not using any other frameworks? I see quite a lot of Mailbox stuff. Why aren't you using the Mailbox API, drivers/mailbox? I'm gussing you'll need I2C somewhere down the line too. And Regmap?

Quoted 1 line

+/* imanager2_core.c - MFD core driver of Advantech EC IT8516/18/28

I can't find any documentation on this device. Is there a datasheet or similar?

Quoted 7 lines

+ * Copyright (C) 2014 Richard Vidal-Dorsch <[email protected]>
+ *
+ * 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.
+ *

Quoted 8 lines

+ * 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/>.
+ */

Documentation/CodingStyle - Comments.

Run scripts/checkpatch.pl on your patches _before_ resending.

Quoted 1 line

+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Don't like this.

drivers/mfd/imanager2_core.c line 24

Quoted 5 lines
drivers/mfd/imanager2_core.c new file +5 -0
+#include <linux/module.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/advantech/imanager2.h>
+
+#define DRV_NAME	CHIP_NAME

Or this.

drivers/mfd/imanager2_core.c line 27

Quoted 3 lines
drivers/mfd/imanager2_core.c new file +3 -0
+#define DRV_VERSION	"0.2.2"
+
+static struct platform_device *pdev;

Why don't you register from Device Tree or platform data? I.e. from arch/<arch>/. While we're on the subject, which architecture(s) is this likely to run on, or is this device completely agnostic?

drivers/mfd/imanager2_core.c line 32

Quoted 4 lines
drivers/mfd/imanager2_core.c new file +4 -0
+static struct mfd_cell it85xx_devs[] = {
+	{ .name = DRV_NAME "_hwm", },
+	{ .name = DRV_NAME "_i2c", },
+};

Don't like this, just use "it85xx" instead of DRV_NAME.

drivers/mfd/imanager2_core.c line 41

Quoted 8 lines
drivers/mfd/imanager2_core.c new file +8 -0
+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) {

One would guess that the Mailbox driver should be taking care of all these.

[...]

drivers/mfd/imanager2_core.c line 71

Quoted 3 lines
drivers/mfd/imanager2_core.c new file +3 -0
+static int ec_get_chip_type(struct it85xx *ec)
+{
+	spin_lock(&ec->lock);

Can you explain to me why you've chosen spin_lock over, say a mutex?

drivers/mfd/imanager2_core.c line 73

Quoted 1 line
drivers/mfd/imanager2_core.c new file +1 -0
+	outb(0x20, EC_SIO_CMD);

Don't use magic numbers - all addresses/masks should be #defined

[...]

drivers/mfd/imanager2_core.c line 101

Quoted 4 lines
drivers/mfd/imanager2_core.c new file +4 -0
+static int ec_get_info(struct it85xx *ec)
+{
+	int ret;
+	/* first kernel version that supports ITE mailbox */

What?

drivers/mfd/imanager2_core.c line 102

Quoted 1 line
drivers/mfd/imanager2_core.c new file +1 -0
+	const u16 supmbox_1st_kver = 0x1105;

No way, please remove.

drivers/mfd/imanager2_core.c line 110

Quoted 8 lines
drivers/mfd/imanager2_core.c new file +8 -0
+	u8 *tmp = (u8 *)&ec->info.version.kernel_ver;
+
+	spin_lock(&ec->lock);
+
+	ret = ec_io_read(EC_CMD_ACPIRAM_READ,
+			 EC_ACPIRAM_ADDR_KERNEL_MAJOR_VERSION, &tmp[0], 2);
+
+	if (ec->info.version.kernel_ver >= supmbox_1st_kver) {

Yuk!

If you explain why you're doing this, maybe we can suggest another way of achieving the same aim.

Quoted 11 lines
drivers/mfd/imanager2_core.c new file +11 -0
+static int __init it85xx_probe(struct it85xx *ec)
+{
+	int ret;
+
+	ret = ec_authentication(ec);
+	if (ret != 0)
+		return ret;
+
+	ret = ec_get_chip_type(ec);
+	if (ret != 0)
+		return ret;

drivers/mfd/imanager2_core.c line 231

Quoted 10 lines
drivers/mfd/imanager2_core.c new file +10 -0
+
+	ret = ec_get_info(ec);
+	if (ret != 0)
+		return ret;
+
+	ret = ec_build_device_table(ec);
+	if (ret != 0)
+		return ret;
+
+	if (request_region(EC_IO_PORT_DATA, 2, DRV_NAME) == NULL) {

devm_*

Review

Quoted 1 line
(quoted diff) +1 -0
+		release_region(EC_IO_PORT_DATA, 2);

Delete

drivers/mfd/imanager2_core.c line 236

Quoted 4 lines
drivers/mfd/imanager2_core.c new file +4 -0
+		return -EIO;
+	}
+
+	if (request_region(EC_ITE_PORT_OFS, 2, DRV_NAME) == NULL) {

Etc ...

Quoted 6 lines
drivers/mfd/imanager2_core.c new file +6 -0
+		release_region(EC_ITE_PORT_OFS, 2);
+		return -EIO;
+	}
+
+	return 0;
+}

drivers/mfd/imanager2_core.c line 252

Quoted 10 lines
drivers/mfd/imanager2_core.c new file +10 -0
+
+static int __init it85xx_device_add(const struct it85xx *ec)
+{
+	int ret;
+
+	pdev = platform_device_alloc(DRV_NAME, 0);
+	if (pdev == NULL) {
+		ret = -ENOMEM;
+		pr_err("Device allocation failed\n");
+		goto exit;

Remove the 'exit' lable and just 'return ret' here.

drivers/mfd/imanager2_core.c line 256

Quoted 4 lines
drivers/mfd/imanager2_core.c new file +4 -0
+	}
+
+	ret = platform_device_add_data(pdev, ec,
+				       sizeof(struct it85xx));

ec is not platform data, it's device data. Platform data should be passed in via the platform_data structure or Device Tree.

drivers/mfd/imanager2_core.c line 269

Quoted 13 lines
drivers/mfd/imanager2_core.c new file +13 -0
+	if (ret != 0) {
+		pr_err("Platform data allocation failed\n");
+		goto exit_device_put;
+	}
+
+	ret = platform_device_add(pdev);
+	if (ret != 0) {
+		pr_err("Device addition failed (%d)\n", ret);
+		goto exit_device_put;
+	}
+
+	ret = mfd_add_devices(&pdev->dev, pdev->id, it85xx_devs,
+			      ARRAY_SIZE(it85xx_devs), NULL, -1, NULL);

Second argument here should probably be -1 for 'auto'.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret != 0) {
+		pr_err("Cannot add sub device (error=%d)\n", ret);

Once you've converted this whole driver to a platform device, all the pr_*'s need moving over to dev_*'s.

drivers/mfd/imanager2_core.c line 274

Quoted 2 lines
drivers/mfd/imanager2_core.c new file +2 -0
+		goto exit_device_unregister;
+	} else {

No need for the else.

drivers/mfd/imanager2_core.c line 275

Quoted 1 line
drivers/mfd/imanager2_core.c new file +1 -0
+		pr_info("MFD core driver v%s loaded\n", DRV_VERSION);

Remove this.

drivers/mfd/imanager2_core.c line 288

Quoted 13 lines
drivers/mfd/imanager2_core.c new file +13 -0
+	}
+
+	return 0;
+
+exit_device_unregister:
+	platform_device_unregister(pdev);
+exit_device_put:
+	platform_device_put(pdev);
+exit:
+	return ret;
+}
+
+

Too many '\n'.

drivers/mfd/imanager2_core.c line 295

Quoted 7 lines
drivers/mfd/imanager2_core.c new file +7 -0
+static int __init it85xx_init(void)
+{
+	struct it85xx ec;
+
+	memset(&ec, 0, sizeof(struct it85xx));
+	spin_lock_init(&ec.lock);
+	if (it85xx_probe(&ec) != 0)

Don't role your own Device Driver framework, instead [un]register with platform_driver_[un]register().

drivers/mfd/imanager2_core.c line 305

Quoted 10 lines
drivers/mfd/imanager2_core.c new file +10 -0
+		return -ENODEV;
+
+	return it85xx_device_add(&ec);
+}
+
+static void __exit it85xx_exit(void)
+{
+	release_region(EC_ITE_PORT_OFS, 2);
+	release_region(EC_IO_PORT_DATA, 2);
+	mfd_remove_devices(&pdev->dev);

This should all be done in .remove, not exit.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	platform_device_unregister(pdev);
+	pr_info("MFD core driver removed\n");

Useless print, please remove.

Quoted 2 lines

+++ b/drivers/mfd/imanager2_ec.c

(quoted diff) +0 -0
@@ -0,0 +1,1093 @@

[...]

drivers/mfd/imanager2_ec.c line 23

Quoted 6 lines
drivers/mfd/imanager2_ec.c new file +6 -0
+#include <linux/io.h>
+#include <linux/delay.h>
+#include <linux/mfd/advantech/imanager2.h>
+
+#define EC_UDELAY_TIME		100
+#define EC_MAX_TIMEOUT_COUNT	10000

Are these arbitrary?

Quoted 6 lines

+/*===========================================================
+ * Name : wait_obf
+ * Purpose: wait output buffer full flag set
+ * Input : none
+ * Output : 0: success; else: fail
+ *===========================================================*/

These headers are ugly, please use kerneldoc format.

Quoted 12 lines
drivers/mfd/imanager2_ec.c new file +12 -0
+static int wait_obf(void)
+{
+	int i;
+	for (i = 0; i < EC_MAX_TIMEOUT_COUNT; i++) {
+		if ((inb(EC_IO_PORT_CMD) & OBF_MASK) != 0)
+			return 0;
+
+		udelay(EC_UDELAY_TIME);
+	}
+
+	return -ETIMEDOUT;
+}

[...]

Quoted 19 lines
drivers/mfd/imanager2_ec.c new file +19 -0
+int ec_mailbox_read_buffer(struct it85xx *ec, u8 cmd, u8 para,
+			   u8 *data, int len)
+{
+	int ret, i;
+	u8 status;
+
+	ret = ec_wait_cmd_clear(ec);
+	if (ret != 0)
+		return ret;
+
+	ec_write_mailbox(ec, EC_MAILBOX_OFFSET_PARA, para);
+	ec_write_mailbox(ec, EC_MAILBOX_OFFSET_CMD, cmd);
+
+	ret = ec_wait_cmd_clear(ec);
+	if (ret != 0)
+		return ret;
+
+	ret = ec_read_mailbox(ec, EC_MAILBOX_OFFSET_STATUS, &status);
+	if (ret != 0)

drivers/mfd/imanager2_ec.c line 212

Quoted 10 lines
drivers/mfd/imanager2_ec.c new file +10 -0
+		return ret;
+	if (status != EC_MAILBOX_STATUS_SUCCESS)
+		return -EFAULT;
+
+	for (i = 0; i < len; i++)
+		ec_read_mailbox(ec, EC_MAILBOX_OFFSET_DAT(i), &data[i]);
+
+	return 0;
+}
+EXPORT_SYMBOL(ec_mailbox_read_buffer);

I suggest that you'll need to move a lot of this code to drivers/mailbox.

[...]

I need to stop here - this patch is massive.

Please break it up into more manageable chunks for your next submission.

A 1500 line patch, is pretty unacceptable.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog