Skip to contentSkip to conversation

intel medfield: thermal_driver

PATCH In review Alan Cox 2 participants 3 messages Nov 15, 2010 Hardware monitoring

lore
Share & tools
lore

Alan Cox

author
#0 lore raw

PATCH 1/1 intel medfield: thermal_driver

From: Durgadoss <[email protected]>

[Merged copy for review only: combination of the two patch set]

This is the basic thermal sensor driver for Intel MID platform using the Medfield chipset. It plugs in via the thermal drivers and provides sensor readings for the device sensors.

(Open question: does it go in drivers/hwmon or drivers/thermal - Rui ?)

[Massively cleaned up]

---

 drivers/hwmon/Kconfig             |    7 
 drivers/hwmon/Makefile            |    1 
 drivers/hwmon/intel_mid_thermal.c |  574 +++++++++++++++++++++++++++++++++++++
 3 files changed, 582 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hwmon/intel_mid_thermal.c
drivers/hwmon/Kconfig +7 -0
index a56f6ad..d20aafd 100644
@@ -1171,6 +1171,13 @@ config SENSORS_MC13783_ADC
         help
           Support for the A/D converter on MC13783 PMIC.
 
+config SENSORS_THERMAL_MFLD
+        tristate "Thermal driver for Intel Medfield platform"
+        depends on INTEL_SCU_IPC && THERMAL
+        help
+          Say Y here to enable thermal driver for the  Intel Medfield
+          platform.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile +1 -0
index 2479b3d..e1f42f3 100644
@@ -109,6 +109,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_THERMAL_MFLD)   += intel_mid_thermal.o
 
 ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)
 EXTRA_CFLAGS += -DDEBUG
drivers/hwmon/intel_mid_thermal.c new file +573 -0

573 lines not on this page · raw message

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw
Quoted 10 lines · Alan Cox

From: Durgadoss <[email protected]>

[Merged copy for review only: combination of the two patch set]

This is the basic thermal sensor driver for Intel MID platform using the Medfield chipset. It plugs in via the thermal drivers and provides sensor readings for the device sensors.

(Open question: does it go in drivers/hwmon or drivers/thermal - Rui ?)

drivers/thermal seems the most natural choice.

Quoted 17 lines
[Massively cleaned up]

---

 drivers/hwmon/Kconfig             |    7
 drivers/hwmon/Makefile            |    1
 drivers/hwmon/intel_mid_thermal.c |  574 +++++++++++++++++++++++++++++++++++++
 3 files changed, 582 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hwmon/intel_mid_thermal.c
drivers/hwmon/Kconfig +0 -0
index a56f6ad..d20aafd 100644

drivers/hwmon/Kconfig config SENSORS_MC13783_ADC · line 1178

Quoted 8 lines
drivers/hwmon/Kconfig +5 -0
@@ -1171,6 +1171,13 @@ config SENSORS_MC13783_ADC
         help
           Support for the A/D converter on MC13783 PMIC.
 
+config SENSORS_THERMAL_MFLD
+        tristate "Thermal driver for Intel Medfield platform"
+        depends on INTEL_SCU_IPC && THERMAL
+        help
+          Say Y here to enable thermal driver for the  Intel Medfield
	"the  Intel" --> "the Intel"
Quoted 10 lines
drivers/hwmon/Kconfig +2 -0
+          platform.
+
 if ACPI
 
 comment "ACPI drivers"
drivers/hwmon/Makefile +0 -0
index 2479b3d..e1f42f3 100644

drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o · line 113

Quoted 5 lines
drivers/hwmon/Makefile +1 -0
@@ -109,6 +109,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_THERMAL_MFLD)   += intel_mid_thermal.o
 

This is not in alphabetical order.

Quoted 38 lines
drivers/hwmon/Makefile +0 -0
 ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)
 EXTRA_CFLAGS += -DDEBUG
drivers/hwmon/intel_mid_thermal.c new file +30 -0
new file mode 100644
index 0000000..eae58b2
@@ -0,0 +1,574 @@
+/*
+ * intel_mid_thermal.c - Intel MID platform thermal driver
+ *
+ * Copyright (C) 2010 Intel Corporation
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * 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; version 2 of the License.
+ *
+ * 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, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * Author: Ananth Krishna <[email protected]>
+ * Author: Durgadoss <[email protected]>
+ */
+
+#define pr_fmt(fmt)  "intel_mid_thermal: " fmt
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/err.h>

drivers/hwmon/intel_mid_thermal.c line 40

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+#include <linux/param.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/pm.h>
+#include <linux/thermal.h>
+
+#include <asm/intel_scu_ipc.h>
+
+

Extra blank line

drivers/hwmon/intel_mid_thermal.c line 48

Quoted 8 lines
drivers/hwmon/intel_mid_thermal.c new file +8 -0
+/* Number of thermal sensors */
+#define MSIC_THERMAL_SENSORS   4
+
+/* ADC1 - thermal registers */
+#define MSIC_THERM_ADC1CNTL1   0x1C0
+#define MSIC_ADC_ENBL          0x10
+#define MSIC_ADC_START         0x08
+

Those defines reflect masks, so please define as (1 << 4) and (1 << 3).

drivers/hwmon/intel_mid_thermal.c line 53

Quoted 5 lines
drivers/hwmon/intel_mid_thermal.c new file +5 -0
+#define MSIC_THERM_ADC1CNTL3   0x1C2
+#define MSIC_ADCTHERM_ENBL     0x04
+#define MSIC_ADCRRDATA_ENBL    0x05
+#define MSIC_CHANL_MASK_VAL    0x0F
+

This define does not seem to be used anywhere.

drivers/hwmon/intel_mid_thermal.c line 55

Quoted 2 lines
drivers/hwmon/intel_mid_thermal.c new file +2 -0
+#define MSIC_STOPBIT_MASK      16
+#define MSIC_ADCTHERM_MASK     4

Please define as masks.

drivers/hwmon/intel_mid_thermal.c line 56

Quoted 1 line
drivers/hwmon/intel_mid_thermal.c new file +1 -0
+#define ADC_CHANLS_MAX         15 /* Number of ADC channels */

"MAX" and "number of channels" are often not the same, especially if the first channel has number 0. The usage here is

    for (i = 0; i < ADC_CHANLS_MAX; i++)

indicating that it may not be the maximum, but the number of channels. If so, ADC_CHANLS_COUNT may be a better define.

drivers/hwmon/intel_mid_thermal.c line 57

Quoted 1 line
drivers/hwmon/intel_mid_thermal.c new file +1 -0
+#define ADC_LOOP_MAX           (ADC_CHANLS_MAX - MSIC_THERMAL_SENSORS)

On the other side, the usage of this define suggests that ADC_CHANLS_MAX might be the maximum channel number, indicating that there may be 16 sensors, not 15. Now what is correct ?

Quoted 17 lines
drivers/hwmon/intel_mid_thermal.c new file +17 -0
+
+#define MSIC_VAUDA             0x0DB
+#define MSIC_VAUDA_VAL         0xFF
+
+/* ADC channel code values */
+#define SKIN_SENSOR0_CODE      0x08
+#define SKIN_SENSOR1_CODE      0x09
+#define SYS_SENSOR_CODE                0x0A
+#define MSIC_DIE_SENSOR_CODE   0x03
+
+#define SKIN_THERM_SENSOR0     0
+#define SKIN_THERM_SENSOR1     1
+#define SYS_THERM_SENSOR2      2
+#define MSIC_DIE_THERM_SENSOR3 3
+
+/* ADC code range */
+#define ADC_MAX                        977

drivers/hwmon/intel_mid_thermal.c line 84

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+#define ADC_MIN                        162
+#define ADC_VAL1               887
+#define ADC_VAL2               720
+#define ADC_VAL3               508
+#define ADC_VAL4               315
+
+/* ADC base addresses */
+#define ADC_CHNL_START_ADDR    0x1C5   /* increments by 1 */
+#define ADC_DATA_START_ADDR     0x1D4   /* increments by 2 */
+

Please watch formatting.

Quoted 11 lines
drivers/hwmon/intel_mid_thermal.c new file +11 -0
+/* MSIC die attributes */
+#define MSIC_DIE_ADC_MIN       488
+#define MSIC_DIE_ADC_MAX       1004
+
+/* convert adc_val to die temperature */
+#define TO_MSIC_DIE_TEMP(adc_val)      ((368 * (adc_val) / 1000) - 220)
+
+static int channel_index;
+
+struct platform_info {
+       struct platform_device *pdev;

drivers/hwmon/intel_mid_thermal.c line 105

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+       struct thermal_zone_device *tzd[MSIC_THERMAL_SENSORS];
+};
+
+struct thermal_device_info {
+       unsigned int chnl_addr;
+       int direct;
+       long curr_temp;
+};
+
+

Extra blank line

Quoted 66 lines
drivers/hwmon/intel_mid_thermal.c new file +66 -0
+/**
+ * is_valid_adc - checks whether the adc code is within the defined range
+ * @min: minimum value for the sensor
+ * @max: maximum value for the sensor
+ *
+ * Can sleep
+ */
+static int is_valid_adc(uint16_t adc_val, uint16_t min, uint16_t max)
+{
+       return (adc_val >= min) && (adc_val <= max);
+}
+
+/**
+ * adc_to_temp - converts the ADC code to temperature in C
+ * @direct: true if ths channel is direct index
+ * @adc_val: the adc_val that needs to be converted
+ * @tp: temperature return value
+ *
+ * Linear approximation is used to covert the skin adc value into temperature.
+ * This technique is used to avoid very long look-up table to get
+ * the appropriate temp value from ADC value.
+ * The adc code vs sensor temp curve is split into five parts
+ * to achieve very close approximate temp value with less than
+ * 0.5C error
+ */
+static int adc_to_temp(int direct, uint16_t adc_val, unsigned long *tp)
+{
+       int temp;
+
+       /* Direct conversion for die temperature */
+       if (direct) {
+               if (is_valid_adc(adc_val, MSIC_DIE_ADC_MIN, MSIC_DIE_ADC_MAX)) {
+                       *tp = (int)(TO_MSIC_DIE_TEMP(adc_val) * 1000);
+                       return 0;
+               }
+               return -ERANGE;
+       }
+
+       if (!is_valid_adc(adc_val, ADC_MIN, ADC_MAX))
+               return -ERANGE;
+
+       /* Linear approximation for skin temperature */
+       if (adc_val > ADC_VAL1) /* -20 to 0C */
+               temp = 177 - (adc_val/5);
+       else if ((adc_val <= ADC_VAL1) && (adc_val > ADC_VAL2)) /* 0C to 20C */
+               temp = 111 - (adc_val/8);
+       else if ((adc_val <= ADC_VAL2) && (adc_val > ADC_VAL3)) /* 20C to 40C */
+               temp = 92 - (adc_val/10);
+       else if ((adc_val <= ADC_VAL3) && (adc_val > ADC_VAL4)) /* 40C to 60C */
+               temp = 91 - (adc_val/10);
+       else
+               temp = 112 - (adc_val/6); /* 60C to 85C */
+
+       /* Convert temperature in celsius to milli degree celsius */
+       *tp = temp * 1000;
+       return 0;
+}
+
+/**
+ * mid_read_temp - read sensors for temperature
+ * @temp: holds the current temperature for the sensor after reading
+ *
+ * reads the adc_code from the channel and converts it to real
+ * temperature. The converted value is stored in temp.
+ *
+ * Can sleep

drivers/hwmon/intel_mid_thermal.c line 181

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+ */
+static int mid_read_temp(struct thermal_zone_device *tzd, unsigned long *temp)
+{
+       struct thermal_device_info *td_info = tzd->devdata;
+       uint16_t adc_val, addr;
+       uint8_t data = 0;
+       int ret;
+       unsigned long curr_temp;
+
+

Extra blank line

Quoted 80 lines
drivers/hwmon/intel_mid_thermal.c new file +80 -0
+       addr = td_info->chnl_addr;
+
+       /* Enable the msic for conversion before reading */
+       ret = intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL3, MSIC_ADCRRDATA_ENBL);
+       if (ret)
+               return ret;
+
+       /* Re-toggle the RRDATARD bit (temporary workaround) */
+       ret = intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL3, MSIC_ADCTHERM_ENBL);
+       if (ret)
+               return ret;
+
+       /* Read the higher bits of data */
+       ret = intel_scu_ipc_ioread8(addr, &data);
+       if (ret)
+               return ret;
+
+       /* Shift bits to accomodate the lower two data bits */
+       adc_val = (data << 2);
+       addr++;
+
+       ret = intel_scu_ipc_ioread8(addr, &data);/* Read lower bits */
+       if (ret)
+               return ret;
+
+       /* Adding lower two bits to the higher bits */
+       data &= 03;
+       adc_val += data;
+
+       /* Convert ADC value to temperature */
+       ret = adc_to_temp(td_info->direct, adc_val, &curr_temp);
+       if (ret = 0)
+               *temp = td_info->curr_temp = curr_temp;
+       return ret;
+}
+
+/**
+ * configure_adc - enables/disables the ADC for conversion
+ * @val: zero: disables the adc non-zero:enables the ADC
+ *
+ * Enable/Disable the ADC depending on the argument
+ *
+ * Can sleep
+ */
+static int configure_adc(int val)
+{
+       int ret;
+       uint8_t data;
+
+       ret = intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL1, &data);
+       if (ret)
+               return ret;
+
+       if (val)
+               /* Enable and start the adc */
+               data |= (MSIC_ADC_ENBL | MSIC_ADC_START);
+       else
+               /* Just stop the adc */
+               data &= (~MSIC_ADC_START);
+
+       return intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL1, data);
+}
+
+/**
+ * set_up_therm_chnl - enable thermal channel for conversion
+ * @base_addr: index of free msic ADC channel
+ *
+ * Enable all the three channels for conversion
+ *
+ * Can sleep
+ */
+static int set_up_therm_chnl(u16 base_addr)
+{
+       int ret;
+
+       /* Enable all the sensor channels */
+       ret = intel_scu_ipc_iowrite8(base_addr, SKIN_SENSOR0_CODE);
+       if (ret)
+               return ret;
+

drivers/hwmon/intel_mid_thermal.c line 271

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+       ret = intel_scu_ipc_iowrite8(base_addr + 1, SKIN_SENSOR1_CODE);
+       if (ret)
+               return ret;
+
+       ret = intel_scu_ipc_iowrite8(base_addr + 2, SYS_SENSOR_CODE);
+       if (ret)
+               return ret;
+
+       /* Since this is the last channel, set the stop bit
+          to 1 by ORing the DIE_SENSOR_CODE with 0x10 */

Please follow guidelines for multi-line comments.

drivers/hwmon/intel_mid_thermal.c line 277

Quoted 6 lines
drivers/hwmon/intel_mid_thermal.c new file +6 -0
+       ret = intel_scu_ipc_iowrite8(base_addr + 3,
+                                       (MSIC_DIE_SENSOR_CODE | 0x10));
+       if (ret)
+               return ret;
+
+       /* Enable VAUDA line: temporary workaround for MSIC issue */

If this is a temporary workaround, what is the fix, and when will it be implemented ? And what is the issue ?

Quoted 26 lines
drivers/hwmon/intel_mid_thermal.c new file +26 -0
+       ret = intel_scu_ipc_iowrite8(MSIC_VAUDA, MSIC_VAUDA_VAL);
+       if (ret)
+               return ret;
+
+       /* Enable adc and start it */
+       ret = configure_adc(1);
+
+       return ret;
+}
+
+/**
+ * reset_stopbit - sets the stop bit to 0 on the given channel
+ * @addr: address of the channel
+ *
+ * Can sleep
+ */
+static int reset_stopbit(uint16_t addr)
+{
+       int ret;
+       uint8_t data;
+       ret = intel_scu_ipc_ioread8(addr, &data);
+       if (ret)
+               return ret;
+       /* Set the stop bit to zero */
+       return intel_scu_ipc_iowrite8(addr, (data & 0xEF));
+}

drivers/hwmon/intel_mid_thermal.c line 313

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+
+/**
+ * find_free_channel - finds an empty channel for conversion
+ *
+ * If the ADC is not enabled then start using 0th channel
+ * itself. Otherwise find an empty channel by looking for a
+ * channel in which the stopbit is set to 1. returns the index
+ * of the first free channel if succeeds or an error code.
+ *
+ * Context: can sleep

Deviating from other "can sleep" comments.

drivers/hwmon/intel_mid_thermal.c line 316

Quoted 3 lines
drivers/hwmon/intel_mid_thermal.c new file +3 -0
+ *
+ * FIXME: Ultimately the channel allocator will move into the intel_scu_ipc
+ * code.

Not sure if it is a good idea to have a FIXME in the code. Either fix it, or make it a note.

Quoted 6 lines
drivers/hwmon/intel_mid_thermal.c new file +6 -0
+ */
+static int find_free_channel(void)
+{
+       int ret;
+       int i;
+       uint8_t data;

drivers/hwmon/intel_mid_thermal.c line 332

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+
+       /* check whether ADC is enabled */
+       ret = intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL1, &data);
+       if (ret)
+               return ret;
+
+       if ((data & MSIC_ADC_ENBL) = 0)
+               return 0;
+
+       /* ADC is already enabled; Looki for an empty channel */

Looki --> Look

drivers/hwmon/intel_mid_thermal.c line 343

Quoted 11 lines
drivers/hwmon/intel_mid_thermal.c new file +11 -0
+       for (i = 0; i < ADC_CHANLS_MAX; i++) {
+               ret = intel_scu_ipc_ioread8(ADC_CHNL_START_ADDR + i, &data);
+               if (ret)
+                       return ret;
+
+               if (data & MSIC_STOPBIT_MASK) {
+                       ret = i;
+                       break;
+               }
+       }
+       return (ret > ADC_LOOP_MAX) ? (-EINVAL) : ret;

Extra () around -EINVAL

Quoted 14 lines
drivers/hwmon/intel_mid_thermal.c new file +14 -0
+}
+
+/**
+ * mid_initialize_adc - initializing the ADC
+ * @dev: our device structure
+ *
+ * Initialize the ADC for reading thermistor values. Can sleep.
+ */
+static int mid_initialize_adc(struct device *dev)
+{
+       u16 base_addr;
+       int ret;
+
+       /*

drivers/hwmon/intel_mid_thermal.c line 368

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+        * Ensure that adctherm is disabled before we
+        * initialize the ADC
+        */
+       ret = intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL3, &data);
+       if (ret)
+               return ret;
+
+       if (data & MSIC_ADCTHERM_MASK)
+               dev_warn(dev, "ADCTHERM already set");
+

Is that a problem ? Just wondering if the warning has any value.

drivers/hwmon/intel_mid_thermal.c line 370

Quoted 2 lines
drivers/hwmon/intel_mid_thermal.c new file +2 -0
+       /* Index of the first channel in which the stop bit is set */
+       channel_index = find_free_channel();

The usage and call sequence for channel_index looks odd. I don't see a well defined relationship between channel_index and the device. channel_index can change each time mid_initialize_adc() is called, ie each time resume() is called. Or if it can not change, it does not make sense to re-calculate it.

If channel_index can change, which the code implies, the initialized ADCs may be different with each resume. However, since initialize_sensor() is only called in probe, those sensors may not really be used. Maybe that just happens to work, but it does not look very clean.

It would be much cleaner to select the ADC channel during probe, and then just re-initialize (start) it in resume. If the whole point of suspend() is to stop all ADC channels, then resume() might just call configure_adc(1) and not call mid_initialize_adc() again. The global variable channel_index should not be needed at all, since it is only used to pass information from here to initialize_sensor().

Besides all this, configure_adc(0) does not disable the adc, it only stops it. So find_free_channel() will function differently after the first call.

drivers/hwmon/intel_mid_thermal.c line 378

Quoted 8 lines
drivers/hwmon/intel_mid_thermal.c new file +8 -0
+       if (channel_index < 0) {
+               dev_err(dev, "No free ADC channels");
+               return channel_index;
+       }
+
+       base_addr = ADC_CHNL_START_ADDR + channel_index;
+
+       if (!(channel_index = 0 || channel_index = ADC_LOOP_MAX)) {

This is a bit confusing. I would suggest to use

        if (channel_index != 0 && channel_index != ADC_LOOP_MAX)

instead.

drivers/hwmon/intel_mid_thermal.c line 379

Quoted 1 line
drivers/hwmon/intel_mid_thermal.c new file +1 -0
+               /* Reset stop bit for channels other than 0 and 12 */

The if statement above checks for channels 0 and 11, though.

drivers/hwmon/intel_mid_thermal.c line 386

Quoted 7 lines
drivers/hwmon/intel_mid_thermal.c new file +7 -0
+               ret = reset_stopbit(base_addr);
+               if (ret)
+                       return ret;
+
+               /* Index of the first free channel */
+               base_addr++;
+               channel_index++;

This is a bit confusing. Index values are translated as follows (with ADC_LOOP_MAX = 15-4 = 11).

                0 --> 0
                1 --> 2
                ...
                10 --> 11
                11 --> 11
                12 --> 13
                13 --> 14
                14 --> 15

So index 1 and 10 are never used, and index 11 is used twice.

Is that really what is intended, is it a bug, or do I misread the code ?

drivers/hwmon/intel_mid_thermal.c line 395

Quoted 9 lines
drivers/hwmon/intel_mid_thermal.c new file +9 -0
+       }
+
+       ret = set_up_therm_chnl(base_addr);
+       if (ret) {
+               dev_err(dev, "unable to enable ADC");
+               return ret;
+       }
+       dev_dbg(dev, "ADC initialization successful");
+       return ret;

Since ret is always 0 here, might as well return 0.

drivers/hwmon/intel_mid_thermal.c line 402

Quoted 7 lines
drivers/hwmon/intel_mid_thermal.c new file +7 -0
+}
+
+/**
+ * initialize_sensor - sets default temp and timer ranges
+ * @index: index of the sensor
+ *
+ * Context: can sleep

I would suggest to use either "can sleep" or "Context: can sleep" throughout, but not a mix.

Quoted 43 lines
drivers/hwmon/intel_mid_thermal.c new file +43 -0
+ */
+static struct thermal_device_info *initialize_sensor(int index)
+{
+       struct thermal_device_info *td_info > +               kzalloc(sizeof(struct thermal_device_info), GFP_KERNEL);
+
+       if (!td_info)
+               return NULL;
+
+       /* Set the base addr of the channel for this sensor */
+       td_info->chnl_addr = ADC_DATA_START_ADDR + 2 * (channel_index + index);
+       /* Sensor 3 is direct conversion */
+       if (index = 3)
+               td_info->direct = 1;
+       return td_info;
+}
+
+/**
+ * mid_thermal_resume - resume routine
+ * @pdev: platform device structure
+ *
+ * mid thermal resume: re-initializes the adc. Can sleep.
+ */
+static int mid_thermal_resume(struct platform_device *pdev)
+{
+       return mid_initialize_adc(&pdev->dev);
+}
+
+/**
+ * mid_thermal_suspend - suspend routine
+ * @pdev: platform device structure
+ *
+ * mid thermal suspend implements the suspend functionality
+ * by stopping the ADC. Can sleep.
+ */
+static int mid_thermal_suspend(struct platform_device *pdev, pm_message_t mesg)
+{
+       /*
+        * This just stops the ADC and does not disable it.
+        * temporary workaround until we have a generic ADC driver.
+        * If 0 is passed, it disables the ADC.
+        */
+       return configure_adc(0);
+}

drivers/hwmon/intel_mid_thermal.c line 455

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+
+/**
+ * read_curr_temp - reads the current temperature and stores in temp
+ * @temp: holds the current temperature value after reading
+ *
+ * Can sleep
+ */
+static int read_curr_temp(struct thermal_zone_device *tzd, unsigned long *temp)
+{
+       WARN_ON(tzd = NULL);

Since you access tzd in mid_read_temp(), you'll get a crash anyway, so this warning seems unnecessary. If you think checking for NULL is necessary, you should return -EFAULT if it is detected.

Quoted 24 lines
drivers/hwmon/intel_mid_thermal.c new file +24 -0
+       return mid_read_temp(tzd, temp);
+}
+
+/* Can't be const */
+static struct thermal_zone_device_ops tzd_ops = {
+       .get_temp = read_curr_temp,
+};
+
+
+/**
+ * mid_thermal_probe - mfld thermal initialize
+ * @pdev: platform device structure
+ *
+ * mid thermal probe initializes the hardware and registers
+ * all the sensors with the generic thermal framework. Can sleep.
+ */
+static int mid_thermal_probe(struct platform_device *pdev)
+{
+       static char *name[MSIC_THERMAL_SENSORS] = {
+               "skin0", "skin1", "sys", "msicdie"
+       };
+
+       int ret;
+       int i;

drivers/hwmon/intel_mid_thermal.c line 489

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+       struct platform_info *pinfo;
+
+       pinfo = kzalloc(sizeof(struct platform_info), GFP_KERNEL);
+       if (!pinfo)
+               return -ENOMEM;
+
+       /* Initializing the hardware */
+       ret = mid_initialize_adc(&pdev->dev);
+       if (ret) {
+               dev_err(&pdev->dev, "ADC init failed");

Memory leak. Please free pinfo.

drivers/hwmon/intel_mid_thermal.c line 497

Quoted 8 lines
drivers/hwmon/intel_mid_thermal.c new file +8 -0
+               return ret;
+       }
+
+       /* Register each sensor with the generic thermal framework */
+       for (i = 0; i < MSIC_THERMAL_SENSORS; i++) {
+               pinfo->tzd[i] = thermal_zone_device_register(name[i],
+                                       0, initialize_sensor(i),
+                                       &tzd_ops, 0, 0, 0, 0);

Since you don't check the result of initialize_sensor(), you will pass NULL if it fails. thermal_zone_device_register() will put NULL in tz->devdata, which you then use in mid_read_temp() without checking.

I would suggest to make this a two-step process, calling thermal_zone_device_register() only if initialize_sensor() was successful, and abort otherwise.

drivers/hwmon/intel_mid_thermal.c line 507

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+               if (IS_ERR(pinfo->tzd[i]))
+                       goto reg_fail;
+       }
+
+       pinfo->pdev = pdev;
+       platform_set_drvdata(pdev, pinfo);
+       return 0;
+
+reg_fail:
+       ret = PTR_ERR(pinfo->tzd[i]);

Assigning ret after IS_ERR above might be a bit cleaner.

drivers/hwmon/intel_mid_thermal.c line 510

Quoted 3 lines
drivers/hwmon/intel_mid_thermal.c new file +3 -0
+       while (--i >= 0)
+               thermal_zone_device_unregister(pinfo->tzd[i]);
+       configure_adc(0);

Memory leak (pinfo).

Quoted 12 lines
drivers/hwmon/intel_mid_thermal.c new file +12 -0
+       return ret;
+}
+
+/**
+ * mid_thermal_remove - mfld thermal finalize
+ * @dev: platform device structure
+ *
+ * MLFD thermal remove unregisters all the sensors from the generic
+ * thermal framework. Can sleep
+ */
+static int mid_thermal_remove(struct platform_device *pdev)
+{

drivers/hwmon/intel_mid_thermal.c line 532

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+       int i;
+       struct platform_info *pinfo = platform_get_drvdata(pdev);
+
+       for (i = 0; i < MSIC_THERMAL_SENSORS; i++)
+               thermal_zone_device_unregister(pinfo->tzd[i]);
+
+       platform_set_drvdata(pdev, NULL);
+
+       /* Stop the ADC */
+       configure_adc(0);

Memory leak (pinfo).

Quoted 29 lines
drivers/hwmon/intel_mid_thermal.c new file +29 -0
+       return 0;
+}
+
+/*********************************************************************
+ *             Driver initialisation and finalization
+ *********************************************************************/
+
+#define DRIVER_NAME "msic_sensor"
+
+static const struct platform_device_id therm_id_table[] = {
+       { DRIVER_NAME, 1 },
+};
+
+static struct platform_driver mid_thermal_driver = {
+       .driver = {
+               .name = DRIVER_NAME,
+               .owner = THIS_MODULE,
+       },
+       .probe = mid_thermal_probe,
+       .suspend = mid_thermal_suspend,
+       .resume = mid_thermal_resume,
+       .remove = __devexit_p(mid_thermal_remove),
+       .id_table = therm_id_table,
+};
+
+static int __init mid_thermal_module_init(void)
+{
+       return platform_driver_register(&mid_thermal_driver);
+}

drivers/hwmon/intel_mid_thermal.c line 571

Quoted 10 lines
drivers/hwmon/intel_mid_thermal.c new file +10 -0
+
+static void __exit mid_thermal_module_exit(void)
+{
+       platform_driver_unregister(&mid_thermal_driver);
+}
+
+module_init(mid_thermal_module_init);
+module_exit(mid_thermal_module_exit);
+
+MODULE_AUTHOR("Durgadoss <[email protected]>");

It is customary to use the full name here. No problem with me, but I have seen reviews where this was suggested.

Quoted 8 lines
drivers/hwmon/intel_mid_thermal.c new file +2 -0
+MODULE_DESCRIPTION("Intel Medfield Platform Thermal Driver");
+MODULE_LICENSE("GPL");

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

author
#2 lore raw
In reply to Alan Cox
Quoted 4 lines
Quoted 1 line

+ /* Enable VAUDA line: temporary workaround for MSIC issue */

If this is a temporary workaround, what is the fix, and when will it be implemented ? And what is the issue ?

The fix is firmware/hardware side so it'll get fixed when the hardware/firmware gets fixed. Fixed being removed in this case.

Quoted 3 lines
Quoted 1 line

+ * Context: can sleep

Deviating from other "can sleep" comments.

I tidied them up and missed that one.

Quoted 7 lines
Quoted 4 lines

+ *
+ * FIXME: Ultimately the channel allocator will move into the intel_scu_ipc
+ * code.

Not sure if it is a good idea to have a FIXME in the code. Either fix it, or make it a note.

FIXME is what is used in other bits of the kernel for this.

Quoted 4 lines

drivers/hwmon/intel_mid_thermal.c line 368

Quoted 3 lines
drivers/hwmon/intel_mid_thermal.c new file +3 -0
+       if (data & MSIC_ADCTHERM_MASK)
+               dev_warn(dev, "ADCTHERM already set");
+

Is that a problem ? Just wondering if the warning has any value.

It shouldn't happen so it is useful to know if it does.

Quoted 8 lines

drivers/hwmon/intel_mid_thermal.c line 370

Quoted 3 lines
drivers/hwmon/intel_mid_thermal.c new file +2 -0
+       /* Index of the first channel in which the stop bit is set */
+       channel_index = find_free_channel();

The usage and call sequence for channel_index looks odd. I don't see a well defined relationship between channel_index and the device. channel_index can change each time mid_initialize_adc() is called, ie each time resume() is called. Or if it can not change, it does not make sense to re-calculate it.

There isn't. It's dynamically assigned - ie ADCs and sensors are not fixed 1:1. I'm not entirely sure its right in all cases so I'll dig deeper into this.

Quoted 3 lines

Since you don't check the result of initialize_sensor(), you will pass NULL if it fails. thermal_zone_device_register() will put NULL in tz->devdata, which you then use in mid_read_temp() without checking.

Agreed

Quoted 4 lines
Quoted 1 line

+MODULE_AUTHOR("Durgadoss <[email protected]>");

It is customary to use the full name here. No problem with me, but I have seen reviews where this was suggested.

Not all parts of the world operate a naming scheme that matches Western Europe.

I'll clean up the small stuff shortly but the other bits I may need to go back round with the original author to clarify.

Thanks

Alan

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors