From d17a7dca04b23586f6c284f98196fbe0ac7607a8 Mon Sep 17 00:00:00 2001 From: Heiko Schocher Date: Wed, 18 Jun 2014 07:37:17 +0200 Subject: hwmon: Driver for TI TMP103 temperature sensor Driver for the TI TMP103. The TI TMP103 is similar to the TMP102. It differs from the TMP102 by having only 8 bit registers. Signed-off-by: Heiko Schocher [linux@roeck-us.net: Select REGMAP_I2C in Kconfig] Signed-off-by: Guenter Roeck --- Documentation/devicetree/bindings/i2c/trivial-devices.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'Documentation/devicetree/bindings') diff --git a/Documentation/devicetree/bindings/i2c/trivial-devices.txt b/Documentation/devicetree/bindings/i2c/trivial-devices.txt index bef86e57c38..fc944e0ed03 100644 --- a/Documentation/devicetree/bindings/i2c/trivial-devices.txt +++ b/Documentation/devicetree/bindings/i2c/trivial-devices.txt @@ -83,5 +83,6 @@ stm,m41t80 M41T80 - SERIAL ACCESS RTC WITH ALARMS taos,tsl2550 Ambient Light Sensor with SMBUS/Two Wire Serial Interface ti,tsc2003 I2C Touch-Screen Controller ti,tmp102 Low Power Digital Temperature Sensor with SMBUS/Two Wire Serial Interface +ti,tmp103 Low Power Digital Temperature Sensor with SMBUS/Two Wire Serial Interface ti,tmp275 Digital Temperature Sensor winbond,wpct301 i2c trusted platform module (TPM) -- cgit v1.2.3-70-g09d2 From 3ef0bb6f769bc61af0ec073cbf49db6598bdefc0 Mon Sep 17 00:00:00 2001 From: Johannes Pointner Date: Fri, 27 Jun 2014 19:53:53 +0200 Subject: devicetree: Add vendor prefix "epcos" to vendor-prefixes.txt This patch adds the vendor prefix for "epcos" to the vendor-prefixes. Signed-off-by: Johannes Pointner Signed-off-by: Guenter Roeck --- Documentation/devicetree/bindings/vendor-prefixes.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'Documentation/devicetree/bindings') diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt index 46a311e728a..97c9c06132c 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.txt +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt @@ -42,6 +42,7 @@ dmo Data Modul AG ebv EBV Elektronik edt Emerging Display Technologies emmicro EM Microelectronic +epcos EPCOS AG epfl Ecole Polytechnique Fédérale de Lausanne epson Seiko Epson Corp. est ESTeem Wireless Modems -- cgit v1.2.3-70-g09d2 From ed67f0872be1aa516831332c732752022d4edc7c Mon Sep 17 00:00:00 2001 From: Johannes Pointner Date: Tue, 1 Jul 2014 08:05:52 +0200 Subject: hwmon: (ntc_thermistor) Support B57330V2103 from EPCOS This patch adds support for the ntc thermistor B57330V2103 from EPCOS. Signed-off-by: Johannes Pointner Signed-off-by: Guenter Roeck --- .../devicetree/bindings/hwmon/ntc_thermistor.txt | 1 + Documentation/hwmon/ntc_thermistor | 5 +++ drivers/hwmon/Kconfig | 2 +- drivers/hwmon/ntc_thermistor.c | 50 +++++++++++++++++++++- include/linux/platform_data/ntc_thermistor.h | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) (limited to 'Documentation/devicetree/bindings') diff --git a/Documentation/devicetree/bindings/hwmon/ntc_thermistor.txt b/Documentation/devicetree/bindings/hwmon/ntc_thermistor.txt index b117b2e9e1a..2391e5c4199 100644 --- a/Documentation/devicetree/bindings/hwmon/ntc_thermistor.txt +++ b/Documentation/devicetree/bindings/hwmon/ntc_thermistor.txt @@ -3,6 +3,7 @@ NTC Thermistor hwmon sensors Requires node properties: - "compatible" value : one of + "epcos,b57330v2103" "murata,ncp15wb473" "murata,ncp18wb473" "murata,ncp21wb473" diff --git a/Documentation/hwmon/ntc_thermistor b/Documentation/hwmon/ntc_thermistor index 057b77029f2..c5e05e2900a 100644 --- a/Documentation/hwmon/ntc_thermistor +++ b/Documentation/hwmon/ntc_thermistor @@ -6,6 +6,11 @@ Supported thermistors from Murata: Prefixes: 'ncp15wb473', 'ncp18wb473', 'ncp21wb473', 'ncp03wb473', 'ncp15wl333' Datasheet: Publicly available at Murata +Supported thermistors from EPCOS: +* EPCOS NTC Thermistors B57330V2103 + Prefixes: b57330v2103 + Datasheet: Publicly available at EPCOS + Other NTC thermistors can be supported simply by adding compensation tables; e.g., NCP15WL333 support is added by the table ncpXXwl333. diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 0c7359c5c31..ae1e2160c1b 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1073,7 +1073,7 @@ config SENSORS_NTC_THERMISTOR Currently, this driver supports NCP15WB473, NCP18WB473, NCP21WB473, NCP03WB473, and NCP15WL333 - from Murata. + from Murata and B57330V2103 from EPCOS. This driver can also be built as a module. If so, the module will be called ntc-thermistor. diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c index ae66f42c4d6..bd410722cd4 100644 --- a/drivers/hwmon/ntc_thermistor.c +++ b/drivers/hwmon/ntc_thermistor.c @@ -51,6 +51,7 @@ static const struct platform_device_id ntc_thermistor_id[] = { { "ncp21wb473", TYPE_NCPXXWB473 }, { "ncp03wb473", TYPE_NCPXXWB473 }, { "ncp15wl333", TYPE_NCPXXWL333 }, + { "b57330v2103", TYPE_B57330V2103}, { }, }; @@ -133,6 +134,47 @@ static const struct ntc_compensation ncpXXwl333[] = { { .temp_c = 125, .ohm = 707 }, }; +/* + * The following compensation table is from the specification of EPCOS NTC + * Thermistors Datasheet + */ +static const struct ntc_compensation b57330v2103[] = { + { .temp_c = -40, .ohm = 190030 }, + { .temp_c = -35, .ohm = 145360 }, + { .temp_c = -30, .ohm = 112060 }, + { .temp_c = -25, .ohm = 87041 }, + { .temp_c = -20, .ohm = 68104 }, + { .temp_c = -15, .ohm = 53665 }, + { .temp_c = -10, .ohm = 42576 }, + { .temp_c = -5, .ohm = 34001 }, + { .temp_c = 0, .ohm = 27326 }, + { .temp_c = 5, .ohm = 22096 }, + { .temp_c = 10, .ohm = 17973 }, + { .temp_c = 15, .ohm = 14703 }, + { .temp_c = 20, .ohm = 12090 }, + { .temp_c = 25, .ohm = 10000 }, + { .temp_c = 30, .ohm = 8311 }, + { .temp_c = 35, .ohm = 6941 }, + { .temp_c = 40, .ohm = 5825 }, + { .temp_c = 45, .ohm = 4911 }, + { .temp_c = 50, .ohm = 4158 }, + { .temp_c = 55, .ohm = 3536 }, + { .temp_c = 60, .ohm = 3019 }, + { .temp_c = 65, .ohm = 2588 }, + { .temp_c = 70, .ohm = 2227 }, + { .temp_c = 75, .ohm = 1924 }, + { .temp_c = 80, .ohm = 1668 }, + { .temp_c = 85, .ohm = 1451 }, + { .temp_c = 90, .ohm = 1266 }, + { .temp_c = 95, .ohm = 1108 }, + { .temp_c = 100, .ohm = 973 }, + { .temp_c = 105, .ohm = 857 }, + { .temp_c = 110, .ohm = 757 }, + { .temp_c = 115, .ohm = 671 }, + { .temp_c = 120, .ohm = 596 }, + { .temp_c = 125, .ohm = 531 }, +}; + struct ntc_data { struct device *hwmon_dev; struct ntc_thermistor_platform_data *pdata; @@ -173,6 +215,8 @@ static const struct of_device_id ntc_match[] = { .data = &ntc_thermistor_id[3] }, { .compatible = "murata,ncp15wl333", .data = &ntc_thermistor_id[4] }, + { .compatible = "epcos,b57330v2103", + .data = &ntc_thermistor_id[5]}, /* Usage of vendor name "ntc" is deprecated */ { .compatible = "ntc,ncp15wb473", @@ -490,6 +534,10 @@ static int ntc_thermistor_probe(struct platform_device *pdev) data->comp = ncpXXwl333; data->n_comp = ARRAY_SIZE(ncpXXwl333); break; + case TYPE_B57330V2103: + data->comp = b57330v2103; + data->n_comp = ARRAY_SIZE(b57330v2103); + break; default: dev_err(&pdev->dev, "Unknown device type: %lu(%s)\n", pdev_id->driver_data, pdev_id->name); @@ -546,7 +594,7 @@ static struct platform_driver ntc_thermistor_driver = { module_platform_driver(ntc_thermistor_driver); -MODULE_DESCRIPTION("NTC Thermistor Driver from Murata"); +MODULE_DESCRIPTION("NTC Thermistor Driver"); MODULE_AUTHOR("MyungJoo Ham "); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:ntc-thermistor"); diff --git a/include/linux/platform_data/ntc_thermistor.h b/include/linux/platform_data/ntc_thermistor.h index c7285b57546..0a6de4ca493 100644 --- a/include/linux/platform_data/ntc_thermistor.h +++ b/include/linux/platform_data/ntc_thermistor.h @@ -26,6 +26,7 @@ struct iio_channel; enum ntc_thermistor_type { TYPE_NCPXXWB473, TYPE_NCPXXWL333, + TYPE_B57330V2103, }; struct ntc_thermistor_platform_data { -- cgit v1.2.3-70-g09d2 From 24c1aa8587922ba1a3ca4fa58748e94f13b97fab Mon Sep 17 00:00:00 2001 From: Neelesh Gupta Date: Tue, 8 Jul 2014 14:38:38 +0530 Subject: hwmon: (powerpc/powernv) hwmon driver for power, fan rpm, voltage and temperature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds basic kernel support for reading power values, fan speed rpm, voltage and temperature data on powernv platforms which will be exported to user space through sysfs interface. Test results: ------------- [root@tul163p1 ~]# sensors ibmpowernv-isa-0000 Adapter: ISA adapter fan1: 5465 RPM (min = 0 RPM) fan2: 5152 RPM (min = 0 RPM) fan3: 5521 RPM (min = 0 RPM) fan4: 4891 RPM (min = 0 RPM) fan5: 0 RPM (min = 0 RPM) fan6: 0 RPM (min = 0 RPM) fan7: 7480 RPM (min = 0 RPM) fan8: 7944 RPM (min = 0 RPM) temp1: +39.0°C (high = +0.0°C) power1: 190.00 W [root@tul163p1 ~]# ls /sys/devices/platform/ alarmtimer ibmpowernv.0 power rtc-generic serial8250 uevent [root@tul163p1 ~]# ls /sys/devices/platform/ibmpowernv.0/hwmon/hwmon0/ device fan2_min fan4_min fan6_min fan8_min power fan1_fault fan3_fault fan5_fault fan7_fault in1_fault power1_input fan1_input fan3_input fan5_input fan7_input in2_fault subsystem fan1_min fan3_min fan5_min fan7_min in3_fault temp1_input fan2_fault fan4_fault fan6_fault fan8_fault in4_fault temp1_max fan2_input fan4_input fan6_input fan8_input name uevent [root@tul163p1 ~]# ls /sys/class/hwmon/hwmon0/ device fan2_min fan4_min fan6_min fan8_min power fan1_fault fan3_fault fan5_fault fan7_fault in1_fault power1_input fan1_input fan3_input fan5_input fan7_input in2_fault subsystem fan1_min fan3_min fan5_min fan7_min in3_fault temp1_input fan2_fault fan4_fault fan6_fault fan8_fault in4_fault temp1_max fan2_input fan4_input fan6_input fan8_input name uevent [root@tul163p1 ~]# Signed-off-by: Neelesh Gupta Signed-off-by: Guenter Roeck --- .../devicetree/bindings/hwmon/ibmpowernv.txt | 23 ++ Documentation/hwmon/ibmpowernv | 41 +++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/ibmpowernv.c | 364 +++++++++++++++++++++ 5 files changed, 440 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/ibmpowernv.txt create mode 100644 Documentation/hwmon/ibmpowernv create mode 100644 drivers/hwmon/ibmpowernv.c (limited to 'Documentation/devicetree/bindings') diff --git a/Documentation/devicetree/bindings/hwmon/ibmpowernv.txt b/Documentation/devicetree/bindings/hwmon/ibmpowernv.txt new file mode 100644 index 00000000000..f93242be60a --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/ibmpowernv.txt @@ -0,0 +1,23 @@ +IBM POWERNV platform sensors +---------------------------- + +Required node properties: +- compatible: must be one of + "ibm,opal-sensor-cooling-fan" + "ibm,opal-sensor-amb-temp" + "ibm,opal-sensor-power-supply" + "ibm,opal-sensor-power" +- sensor-id: an opaque id provided by the firmware to the kernel, identifies a + given sensor and its attribute data + +Example sensors node: + +cooling-fan#8-data { + sensor-id = <0x7052107>; + compatible = "ibm,opal-sensor-cooling-fan"; +}; + +amb-temp#1-thrs { + sensor-id = <0x5096000>; + compatible = "ibm,opal-sensor-amb-temp"; +}; diff --git a/Documentation/hwmon/ibmpowernv b/Documentation/hwmon/ibmpowernv new file mode 100644 index 00000000000..8826ba29db3 --- /dev/null +++ b/Documentation/hwmon/ibmpowernv @@ -0,0 +1,41 @@ +Kernel Driver IBMPOWERNV +======================== + +Supported systems: + * Any recent IBM P servers based on POWERNV platform + +Author: Neelesh Gupta + +Description +----------- + +This driver implements reading the platform sensors data like temperature/fan/ +voltage/power for 'POWERNV' platform. + +The driver uses the platform device infrastructure. It probes the device tree +for sensor devices during the __init phase and registers them with the 'hwmon'. +'hwmon' populates the 'sysfs' tree having attribute files, each for a given +sensor type and its attribute data. + +All the nodes in the DT appear under "/ibm,opal/sensors" and each valid node in +the DT maps to an attribute file in 'sysfs'. The node exports unique 'sensor-id' +which the driver uses to make an OPAL call to the firmware. + +Usage notes +----------- +The driver is built statically with the kernel by enabling the config +CONFIG_SENSORS_IBMPOWERNV. It can also be built as module 'ibmpowernv'. + +Sysfs attributes +---------------- + +fanX_input Measured RPM value. +fanX_min Threshold RPM for alert generation. +fanX_fault 0: No fail condition + 1: Failing fan +tempX_input Measured ambient temperature. +tempX_max Threshold ambient temperature for alert generation. +inX_input Measured power supply voltage +inX_fault 0: No fail condition. + 1: Failing power supply. +power1_input System power consumption (microWatt) diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 37908ff8f7f..c0aec8db462 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -554,6 +554,17 @@ config SENSORS_IBMPEX This driver can also be built as a module. If so, the module will be called ibmpex. +config SENSORS_IBMPOWERNV + tristate "IBM POWERNV platform sensors" + depends on PPC_POWERNV + default y + help + If you say yes here you get support for the temperature/fan/power + sensors on your PowerNV platform. + + This driver can also be built as a module. If so, the module + will be called ibmpowernv. + config SENSORS_IIO_HWMON tristate "Hwmon driver that uses channels specified via iio maps" depends on IIO diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index 1362382a318..f4435ea6b16 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -71,6 +71,7 @@ obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.o obj-$(CONFIG_SENSORS_IBMAEM) += ibmaem.o obj-$(CONFIG_SENSORS_IBMPEX) += ibmpex.o +obj-$(CONFIG_SENSORS_IBMPOWERNV)+= ibmpowernv.o obj-$(CONFIG_SENSORS_IIO_HWMON) += iio_hwmon.o obj-$(CONFIG_SENSORS_INA209) += ina209.o obj-$(CONFIG_SENSORS_INA2XX) += ina2xx.o diff --git a/drivers/hwmon/ibmpowernv.c b/drivers/hwmon/ibmpowernv.c new file mode 100644 index 00000000000..e6b652a3581 --- /dev/null +++ b/drivers/hwmon/ibmpowernv.c @@ -0,0 +1,364 @@ +/* + * IBM PowerNV platform sensors for temperature/fan/voltage/power + * Copyright (C) 2014 IBM + * + * 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 2 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. + */ + +#define DRVNAME "ibmpowernv" +#define pr_fmt(fmt) DRVNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define MAX_ATTR_LEN 32 + +/* Sensor suffix name from DT */ +#define DT_FAULT_ATTR_SUFFIX "faulted" +#define DT_DATA_ATTR_SUFFIX "data" +#define DT_THRESHOLD_ATTR_SUFFIX "thrs" + +/* + * Enumerates all the types of sensors in the POWERNV platform and does index + * into 'struct sensor_group' + */ +enum sensors { + FAN, + AMBIENT_TEMP, + POWER_SUPPLY, + POWER_INPUT, + MAX_SENSOR_TYPE, +}; + +static struct sensor_group { + const char *name; + const char *compatible; + struct attribute_group group; + u32 attr_count; +} sensor_groups[] = { + {"fan", "ibm,opal-sensor-cooling-fan"}, + {"temp", "ibm,opal-sensor-amb-temp"}, + {"in", "ibm,opal-sensor-power-supply"}, + {"power", "ibm,opal-sensor-power"} +}; + +struct sensor_data { + u32 id; /* An opaque id of the firmware for each sensor */ + enum sensors type; + char name[MAX_ATTR_LEN]; + struct device_attribute dev_attr; +}; + +struct platform_data { + const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1]; + u32 sensors_count; /* Total count of sensors from each group */ +}; + +/* Platform device representing all the ibmpowernv sensors */ +static struct platform_device *pdevice; + +static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr, + char *buf) +{ + struct sensor_data *sdata = container_of(devattr, struct sensor_data, + dev_attr); + ssize_t ret; + u32 x; + + ret = opal_get_sensor_data(sdata->id, &x); + if (ret) + return ret; + + /* Convert temperature to milli-degrees */ + if (sdata->type == AMBIENT_TEMP) + x *= 1000; + /* Convert power to micro-watts */ + else if (sdata->type == POWER_INPUT) + x *= 1000000; + + return sprintf(buf, "%u\n", x); +} + +static int __init get_sensor_index_attr(const char *name, u32 *index, + char *attr) +{ + char *hash_pos = strchr(name, '#'); + char buf[8] = { 0 }; + char *dash_pos; + u32 copy_len; + int err; + + if (!hash_pos) + return -EINVAL; + + dash_pos = strchr(hash_pos, '-'); + if (!dash_pos) + return -EINVAL; + + copy_len = dash_pos - hash_pos - 1; + if (copy_len >= sizeof(buf)) + return -EINVAL; + + strncpy(buf, hash_pos + 1, copy_len); + + err = kstrtou32(buf, 10, index); + if (err) + return err; + + strncpy(attr, dash_pos + 1, MAX_ATTR_LEN); + + return 0; +} + +/* + * This function translates the DT node name into the 'hwmon' attribute name. + * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc. + * which need to be mapped as fan2_input, temp1_max respectively before + * populating them inside hwmon device class. + */ +static int __init create_hwmon_attr_name(struct device *dev, enum sensors type, + const char *node_name, + char *hwmon_attr_name) +{ + char attr_suffix[MAX_ATTR_LEN]; + char *attr_name; + u32 index; + int err; + + err = get_sensor_index_attr(node_name, &index, attr_suffix); + if (err) { + dev_err(dev, "Sensor device node name '%s' is invalid\n", + node_name); + return err; + } + + if (!strcmp(attr_suffix, DT_FAULT_ATTR_SUFFIX)) { + attr_name = "fault"; + } else if (!strcmp(attr_suffix, DT_DATA_ATTR_SUFFIX)) { + attr_name = "input"; + } else if (!strcmp(attr_suffix, DT_THRESHOLD_ATTR_SUFFIX)) { + if (type == AMBIENT_TEMP) + attr_name = "max"; + else if (type == FAN) + attr_name = "min"; + else + return -ENOENT; + } else { + return -ENOENT; + } + + snprintf(hwmon_attr_name, MAX_ATTR_LEN, "%s%d_%s", + sensor_groups[type].name, index, attr_name); + return 0; +} + +static int __init populate_attr_groups(struct platform_device *pdev) +{ + struct platform_data *pdata = platform_get_drvdata(pdev); + const struct attribute_group **pgroups = pdata->attr_groups; + struct device_node *opal, *np; + enum sensors type; + + opal = of_find_node_by_path("/ibm,opal/sensors"); + if (!opal) { + dev_err(&pdev->dev, "Opal node 'sensors' not found\n"); + return -ENODEV; + } + + for_each_child_of_node(opal, np) { + if (np->name == NULL) + continue; + + for (type = 0; type < MAX_SENSOR_TYPE; type++) + if (of_device_is_compatible(np, + sensor_groups[type].compatible)) { + sensor_groups[type].attr_count++; + break; + } + } + + of_node_put(opal); + + for (type = 0; type < MAX_SENSOR_TYPE; type++) { + sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev, + sizeof(struct attribute *) * + (sensor_groups[type].attr_count + 1), + GFP_KERNEL); + if (!sensor_groups[type].group.attrs) + return -ENOMEM; + + pgroups[type] = &sensor_groups[type].group; + pdata->sensors_count += sensor_groups[type].attr_count; + sensor_groups[type].attr_count = 0; + } + + return 0; +} + +/* + * Iterate through the device tree for each child of 'sensors' node, create + * a sysfs attribute file, the file is named by translating the DT node name + * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max + * etc.. + */ +static int __init create_device_attrs(struct platform_device *pdev) +{ + struct platform_data *pdata = platform_get_drvdata(pdev); + const struct attribute_group **pgroups = pdata->attr_groups; + struct device_node *opal, *np; + struct sensor_data *sdata; + const __be32 *sensor_id; + enum sensors type; + u32 count = 0; + int err = 0; + + opal = of_find_node_by_path("/ibm,opal/sensors"); + sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata), + GFP_KERNEL); + if (!sdata) { + err = -ENOMEM; + goto exit_put_node; + } + + for_each_child_of_node(opal, np) { + if (np->name == NULL) + continue; + + for (type = 0; type < MAX_SENSOR_TYPE; type++) + if (of_device_is_compatible(np, + sensor_groups[type].compatible)) + break; + + if (type == MAX_SENSOR_TYPE) + continue; + + sensor_id = of_get_property(np, "sensor-id", NULL); + if (!sensor_id) { + dev_info(&pdev->dev, + "'sensor-id' missing in the node '%s'\n", + np->name); + continue; + } + + sdata[count].id = be32_to_cpup(sensor_id); + sdata[count].type = type; + err = create_hwmon_attr_name(&pdev->dev, type, np->name, + sdata[count].name); + if (err) + goto exit_put_node; + + sysfs_attr_init(&sdata[count].dev_attr.attr); + sdata[count].dev_attr.attr.name = sdata[count].name; + sdata[count].dev_attr.attr.mode = S_IRUGO; + sdata[count].dev_attr.show = show_sensor; + + pgroups[type]->attrs[sensor_groups[type].attr_count++] = + &sdata[count++].dev_attr.attr; + } + +exit_put_node: + of_node_put(opal); + return err; +} + +static int __init ibmpowernv_probe(struct platform_device *pdev) +{ + struct platform_data *pdata; + struct device *hwmon_dev; + int err; + + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return -ENOMEM; + + platform_set_drvdata(pdev, pdata); + pdata->sensors_count = 0; + err = populate_attr_groups(pdev); + if (err) + return err; + + /* Create sysfs attribute data for each sensor found in the DT */ + err = create_device_attrs(pdev); + if (err) + return err; + + /* Finally, register with hwmon */ + hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME, + pdata, + pdata->attr_groups); + + return PTR_ERR_OR_ZERO(hwmon_dev); +} + +static struct platform_driver ibmpowernv_driver = { + .driver = { + .owner = THIS_MODULE, + .name = DRVNAME, + }, +}; + +static int __init ibmpowernv_init(void) +{ + int err; + + pdevice = platform_device_alloc(DRVNAME, 0); + if (!pdevice) { + pr_err("Device allocation failed\n"); + err = -ENOMEM; + goto exit; + } + + err = platform_device_add(pdevice); + if (err) { + pr_err("Device addition failed (%d)\n", err); + goto exit_device_put; + } + + err = platform_driver_probe(&ibmpowernv_driver, ibmpowernv_probe); + if (err) { + pr_err("Platfrom driver probe failed\n"); + goto exit_device_del; + } + + return 0; + +exit_device_del: + platform_device_del(pdevice); +exit_device_put: + platform_device_put(pdevice); +exit: + return err; +} + +static void __exit ibmpowernv_exit(void) +{ + platform_driver_unregister(&ibmpowernv_driver); + platform_device_unregister(pdevice); +} + +MODULE_AUTHOR("Neelesh Gupta "); +MODULE_DESCRIPTION("IBM POWERNV platform sensors"); +MODULE_LICENSE("GPL"); + +module_init(ibmpowernv_init); +module_exit(ibmpowernv_exit); -- cgit v1.2.3-70-g09d2 From d82d57767c85984132cde4efaf89ad6db290cda3 Mon Sep 17 00:00:00 2001 From: Kamil Debski Date: Wed, 16 Jul 2014 17:46:42 +0200 Subject: hwmon: Add pwm-fan driver The pwm-fan driver enables control of fans connected to PWM lines. This driver uses the PWM framework, so it is compatible with all PWM devices that provide drivers through the PWM framework. Signed-off-by: Kamil Debski Reviewed-by: Varka Bhadram Reviewed-by: Tobias Klauser [Guenter Roeck: Last argument to devm_of_pwm_get is pointer, use NULL] Signed-off-by: Guenter Roeck --- .../devicetree/bindings/hwmon/pwm-fan.txt | 12 ++ Documentation/hwmon/pwm-fan | 17 ++ drivers/hwmon/Kconfig | 11 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/pwm-fan.c | 194 +++++++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/pwm-fan.txt create mode 100644 Documentation/hwmon/pwm-fan create mode 100644 drivers/hwmon/pwm-fan.c (limited to 'Documentation/devicetree/bindings') diff --git a/Documentation/devicetree/bindings/hwmon/pwm-fan.txt b/Documentation/devicetree/bindings/hwmon/pwm-fan.txt new file mode 100644 index 00000000000..610757ce449 --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/pwm-fan.txt @@ -0,0 +1,12 @@ +Bindings for a fan connected to the PWM lines + +Required properties: +- compatible : "pwm-fan" +- pwms : the PWM that is used to control the PWM fan + +Example: + pwm-fan { + compatible = "pwm-fan"; + status = "okay"; + pwms = <&pwm 0 10000 0>; + }; diff --git a/Documentation/hwmon/pwm-fan b/Documentation/hwmon/pwm-fan new file mode 100644 index 00000000000..18529d2e3bc --- /dev/null +++ b/Documentation/hwmon/pwm-fan @@ -0,0 +1,17 @@ +Kernel driver pwm-fan +===================== + +This driver enables the use of a PWM module to drive a fan. It uses the +generic PWM interface thus it is hardware independent. It can be used on +many SoCs, as long as the SoC supplies a PWM line driver that exposes +the generic PWM API. + +Author: Kamil Debski + +Description +----------- + +The driver implements a simple interface for driving a fan connected to +a PWM output. It uses the generic PWM interface, thus it can be used with +a range of SoCs. The driver exposes the fan to the user space through +the hwmon's sysfs interface. diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index c0aec8db462..b93ab97732c 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1128,6 +1128,17 @@ config SENSORS_PCF8591 source drivers/hwmon/pmbus/Kconfig +config SENSORS_PWM_FAN + tristate "PWM fan" + depends on PWM + help + If you say yes here you get support for fans connected to PWM lines. + The driver uses the generic PWM interface, thus it will work on a + variety of SoCs. + + This driver can also be built as a module. If so, the module + will be called pwm-fan. + config SENSORS_SHT15 tristate "Sensiron humidity and temperature sensors. SHT15 and compat." depends on GPIOLIB diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index f4435ea6b16..be28152c984 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -122,6 +122,7 @@ obj-$(CONFIG_SENSORS_PC87360) += pc87360.o obj-$(CONFIG_SENSORS_PC87427) += pc87427.o obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o +obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c new file mode 100644 index 00000000000..af30dd63de6 --- /dev/null +++ b/drivers/hwmon/pwm-fan.c @@ -0,0 +1,194 @@ +/* + * pwm-fan.c - Hwmon driver for fans connected to PWM lines. + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Author: Kamil Debski + * + * 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 2 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_PWM 255 + +struct pwm_fan_ctx { + struct mutex lock; + struct pwm_device *pwm; + unsigned char pwm_value; +}; + +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + unsigned long pwm, duty; + ssize_t ret; + + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM) + return -EINVAL; + + mutex_lock(&ctx->lock); + + if (ctx->pwm_value == pwm) + goto exit_set_pwm_no_change; + + if (pwm == 0) { + pwm_disable(ctx->pwm); + goto exit_set_pwm; + } + + duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM); + ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); + if (ret) + goto exit_set_pwm_err; + + if (ctx->pwm_value == 0) { + ret = pwm_enable(ctx->pwm); + if (ret) + goto exit_set_pwm_err; + } + +exit_set_pwm: + ctx->pwm_value = pwm; +exit_set_pwm_no_change: + ret = count; +exit_set_pwm_err: + mutex_unlock(&ctx->lock); + return ret; +} + +static ssize_t show_pwm(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + + return sprintf(buf, "%u\n", ctx->pwm_value); +} + + +static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); + +static struct attribute *pwm_fan_attrs[] = { + &sensor_dev_attr_pwm1.dev_attr.attr, + NULL, +}; + +ATTRIBUTE_GROUPS(pwm_fan); + +static int pwm_fan_probe(struct platform_device *pdev) +{ + struct device *hwmon; + struct pwm_fan_ctx *ctx; + int duty_cycle; + int ret; + + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + + mutex_init(&ctx->lock); + + ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); + if (IS_ERR(ctx->pwm)) { + dev_err(&pdev->dev, "Could not get PWM\n"); + return PTR_ERR(ctx->pwm); + } + + dev_set_drvdata(&pdev->dev, ctx); + platform_set_drvdata(pdev, ctx); + + /* Set duty cycle to maximum allowed */ + duty_cycle = ctx->pwm->period - 1; + ctx->pwm_value = MAX_PWM; + + ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period); + if (ret) { + dev_err(&pdev->dev, "Failed to configure PWM\n"); + return ret; + } + + /* Enbale PWM output */ + ret = pwm_enable(ctx->pwm); + if (ret) { + dev_err(&pdev->dev, "Failed to enable PWM\n"); + return ret; + } + + hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", + ctx, pwm_fan_groups); + if (IS_ERR(hwmon)) { + dev_err(&pdev->dev, "Failed to register hwmon device\n"); + pwm_disable(ctx->pwm); + return PTR_ERR(hwmon); + } + return 0; +} + +static int pwm_fan_remove(struct platform_device *pdev) +{ + struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); + + if (ctx->pwm_value) + pwm_disable(ctx->pwm); + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int pwm_fan_suspend(struct device *dev) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + + if (ctx->pwm_value) + pwm_disable(ctx->pwm); + return 0; +} + +static int pwm_fan_resume(struct device *dev) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + + if (ctx->pwm_value) + return pwm_enable(ctx->pwm); + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); + +static struct of_device_id of_pwm_fan_match[] = { + { .compatible = "pwm-fan", }, + {}, +}; + +static struct platform_driver pwm_fan_driver = { + .probe = pwm_fan_probe, + .remove = pwm_fan_remove, + .driver = { + .name = "pwm-fan", + .pm = &pwm_fan_pm, + .of_match_table = of_pwm_fan_match, + }, +}; + +module_platform_driver(pwm_fan_driver); + +MODULE_AUTHOR("Kamil Debski "); +MODULE_ALIAS("platform:pwm-fan"); +MODULE_DESCRIPTION("PWM FAN driver"); +MODULE_LICENSE("GPL"); -- cgit v1.2.3-70-g09d2