2009-04-14 01:39:45 +04:00
|
|
|
/*
|
|
|
|
* sht15.c - support for the SHT15 Temperature and Humidity Sensor
|
|
|
|
*
|
2012-08-31 05:46:19 +04:00
|
|
|
* Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
|
2011-04-12 23:34:39 +04:00
|
|
|
* Jerome Oufella <jerome.oufella@savoirfairelinux.com>
|
2011-04-12 23:34:38 +04:00
|
|
|
* Vivien Didelot <vivien.didelot@savoirfairelinux.com>
|
|
|
|
*
|
2009-04-14 01:39:45 +04:00
|
|
|
* Copyright (c) 2009 Jonathan Cameron
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Wouter Horre
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
2011-04-12 23:34:36 +04:00
|
|
|
* For further information, see the Documentation/hwmon/sht15 file.
|
2009-04-14 01:39:45 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/hwmon-sysfs.h>
|
|
|
|
#include <linux/mutex.h>
|
2012-09-12 20:23:44 +04:00
|
|
|
#include <linux/platform_data/sht15.h>
|
2009-04-14 01:39:45 +04:00
|
|
|
#include <linux/platform_device.h>
|
2009-10-07 17:09:06 +04:00
|
|
|
#include <linux/sched.h>
|
2009-04-14 01:39:45 +04:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/regulator/consumer.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
|
|
|
#include <linux/slab.h>
|
2011-07-27 03:09:06 +04:00
|
|
|
#include <linux/atomic.h>
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/* Commands */
|
|
|
|
#define SHT15_MEASURE_TEMP 0x03
|
|
|
|
#define SHT15_MEASURE_RH 0x05
|
2011-04-12 23:34:38 +04:00
|
|
|
#define SHT15_WRITE_STATUS 0x06
|
|
|
|
#define SHT15_READ_STATUS 0x07
|
2011-04-12 23:34:37 +04:00
|
|
|
#define SHT15_SOFT_RESET 0x1E
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/* Min timings */
|
|
|
|
#define SHT15_TSCKL 100 /* (nsecs) clock low */
|
|
|
|
#define SHT15_TSCKH 100 /* (nsecs) clock high */
|
|
|
|
#define SHT15_TSU 150 /* (nsecs) data setup time */
|
2011-04-12 23:34:37 +04:00
|
|
|
#define SHT15_TSRST 11 /* (msecs) soft reset time */
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
/* Status Register Bits */
|
|
|
|
#define SHT15_STATUS_LOW_RESOLUTION 0x01
|
|
|
|
#define SHT15_STATUS_NO_OTP_RELOAD 0x02
|
|
|
|
#define SHT15_STATUS_HEATER 0x04
|
|
|
|
#define SHT15_STATUS_LOW_BATTERY 0x40
|
|
|
|
|
2012-08-31 05:46:19 +04:00
|
|
|
/* List of supported chips */
|
|
|
|
enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/* Actions the driver may be doing */
|
|
|
|
enum sht15_state {
|
|
|
|
SHT15_READING_NOTHING,
|
|
|
|
SHT15_READING_TEMP,
|
|
|
|
SHT15_READING_HUMID
|
|
|
|
};
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
/**
|
2011-03-31 05:57:33 +04:00
|
|
|
* struct sht15_temppair - elements of voltage dependent temp calc
|
2009-04-14 01:39:45 +04:00
|
|
|
* @vdd: supply voltage in microvolts
|
|
|
|
* @d1: see data sheet
|
|
|
|
*/
|
|
|
|
struct sht15_temppair {
|
|
|
|
int vdd; /* microvolts */
|
|
|
|
int d1;
|
|
|
|
};
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/* Table 9 from datasheet - relates temperature calculation to supply voltage */
|
2009-04-14 01:39:45 +04:00
|
|
|
static const struct sht15_temppair temppoints[] = {
|
|
|
|
{ 2500000, -39400 },
|
|
|
|
{ 3000000, -39600 },
|
|
|
|
{ 3500000, -39700 },
|
|
|
|
{ 4000000, -39800 },
|
|
|
|
{ 5000000, -40100 },
|
|
|
|
};
|
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
/* Table from CRC datasheet, section 2.4 */
|
|
|
|
static const u8 sht15_crc8_table[] = {
|
|
|
|
0, 49, 98, 83, 196, 245, 166, 151,
|
|
|
|
185, 136, 219, 234, 125, 76, 31, 46,
|
|
|
|
67, 114, 33, 16, 135, 182, 229, 212,
|
|
|
|
250, 203, 152, 169, 62, 15, 92, 109,
|
|
|
|
134, 183, 228, 213, 66, 115, 32, 17,
|
|
|
|
63, 14, 93, 108, 251, 202, 153, 168,
|
|
|
|
197, 244, 167, 150, 1, 48, 99, 82,
|
|
|
|
124, 77, 30, 47, 184, 137, 218, 235,
|
|
|
|
61, 12, 95, 110, 249, 200, 155, 170,
|
|
|
|
132, 181, 230, 215, 64, 113, 34, 19,
|
|
|
|
126, 79, 28, 45, 186, 139, 216, 233,
|
|
|
|
199, 246, 165, 148, 3, 50, 97, 80,
|
|
|
|
187, 138, 217, 232, 127, 78, 29, 44,
|
|
|
|
2, 51, 96, 81, 198, 247, 164, 149,
|
|
|
|
248, 201, 154, 171, 60, 13, 94, 111,
|
|
|
|
65, 112, 35, 18, 133, 180, 231, 214,
|
|
|
|
122, 75, 24, 41, 190, 143, 220, 237,
|
|
|
|
195, 242, 161, 144, 7, 54, 101, 84,
|
|
|
|
57, 8, 91, 106, 253, 204, 159, 174,
|
|
|
|
128, 177, 226, 211, 68, 117, 38, 23,
|
|
|
|
252, 205, 158, 175, 56, 9, 90, 107,
|
|
|
|
69, 116, 39, 22, 129, 176, 227, 210,
|
|
|
|
191, 142, 221, 236, 123, 74, 25, 40,
|
|
|
|
6, 55, 100, 85, 194, 243, 160, 145,
|
|
|
|
71, 118, 37, 20, 131, 178, 225, 208,
|
|
|
|
254, 207, 156, 173, 58, 11, 88, 105,
|
|
|
|
4, 53, 102, 87, 192, 241, 162, 147,
|
|
|
|
189, 140, 223, 238, 121, 72, 27, 42,
|
|
|
|
193, 240, 163, 146, 5, 52, 103, 86,
|
|
|
|
120, 73, 26, 43, 188, 141, 222, 239,
|
|
|
|
130, 179, 224, 209, 70, 119, 36, 21,
|
|
|
|
59, 10, 89, 104, 255, 206, 157, 172
|
|
|
|
};
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
|
|
|
* struct sht15_data - device instance specific data
|
2011-04-12 23:34:36 +04:00
|
|
|
* @pdata: platform data (gpio's etc).
|
|
|
|
* @read_work: bh of interrupt handler.
|
|
|
|
* @wait_queue: wait queue for getting values from device.
|
|
|
|
* @val_temp: last temperature value read from device.
|
|
|
|
* @val_humid: last humidity value read from device.
|
2011-04-12 23:34:38 +04:00
|
|
|
* @val_status: last status register value read from device.
|
2011-04-12 23:34:39 +04:00
|
|
|
* @checksum_ok: last value read from the device passed CRC validation.
|
|
|
|
* @checksumming: flag used to enable the data validation with CRC.
|
2011-04-12 23:34:36 +04:00
|
|
|
* @state: state identifying the action the driver is doing.
|
|
|
|
* @measurements_valid: are the current stored measures valid (start condition).
|
2011-04-12 23:34:38 +04:00
|
|
|
* @status_valid: is the current stored status valid (start condition).
|
2011-04-12 23:34:36 +04:00
|
|
|
* @last_measurement: time of last measure.
|
2011-04-12 23:34:38 +04:00
|
|
|
* @last_status: time of last status reading.
|
2011-04-12 23:34:36 +04:00
|
|
|
* @read_lock: mutex to ensure only one read in progress at a time.
|
|
|
|
* @dev: associate device structure.
|
|
|
|
* @hwmon_dev: device associated with hwmon subsystem.
|
|
|
|
* @reg: associated regulator (if specified).
|
|
|
|
* @nb: notifier block to handle notifications of voltage
|
|
|
|
* changes.
|
2013-01-07 23:18:38 +04:00
|
|
|
* @supply_uv: local copy of supply voltage used to allow use of
|
2011-04-12 23:34:36 +04:00
|
|
|
* regulator consumer if available.
|
2013-01-07 23:18:38 +04:00
|
|
|
* @supply_uv_valid: indicates that an updated value has not yet been
|
2011-04-12 23:34:36 +04:00
|
|
|
* obtained from the regulator and so any calculations
|
|
|
|
* based upon it will be invalid.
|
2013-01-07 23:18:38 +04:00
|
|
|
* @update_supply_work: work struct that is used to update the supply_uv.
|
2011-04-12 23:34:36 +04:00
|
|
|
* @interrupt_handled: flag used to indicate a handler has been scheduled.
|
2009-04-14 01:39:45 +04:00
|
|
|
*/
|
|
|
|
struct sht15_data {
|
|
|
|
struct sht15_platform_data *pdata;
|
|
|
|
struct work_struct read_work;
|
|
|
|
wait_queue_head_t wait_queue;
|
|
|
|
uint16_t val_temp;
|
|
|
|
uint16_t val_humid;
|
2011-04-12 23:34:38 +04:00
|
|
|
u8 val_status;
|
2011-04-12 23:34:39 +04:00
|
|
|
bool checksum_ok;
|
|
|
|
bool checksumming;
|
2011-04-12 23:34:36 +04:00
|
|
|
enum sht15_state state;
|
|
|
|
bool measurements_valid;
|
2011-04-12 23:34:38 +04:00
|
|
|
bool status_valid;
|
2011-04-12 23:34:36 +04:00
|
|
|
unsigned long last_measurement;
|
2011-04-12 23:34:38 +04:00
|
|
|
unsigned long last_status;
|
2009-04-14 01:39:45 +04:00
|
|
|
struct mutex read_lock;
|
|
|
|
struct device *dev;
|
|
|
|
struct device *hwmon_dev;
|
|
|
|
struct regulator *reg;
|
|
|
|
struct notifier_block nb;
|
2013-01-07 23:18:38 +04:00
|
|
|
int supply_uv;
|
|
|
|
bool supply_uv_valid;
|
2009-04-14 01:39:45 +04:00
|
|
|
struct work_struct update_supply_work;
|
|
|
|
atomic_t interrupt_handled;
|
|
|
|
};
|
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
/**
|
|
|
|
* sht15_reverse() - reverse a byte
|
|
|
|
* @byte: byte to reverse.
|
|
|
|
*/
|
|
|
|
static u8 sht15_reverse(u8 byte)
|
|
|
|
{
|
|
|
|
u8 i, c;
|
|
|
|
|
|
|
|
for (c = 0, i = 0; i < 8; i++)
|
|
|
|
c |= (!!(byte & (1 << i))) << (7 - i);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_crc8() - compute crc8
|
|
|
|
* @data: sht15 specific data.
|
|
|
|
* @value: sht15 retrieved data.
|
|
|
|
*
|
|
|
|
* This implements section 2 of the CRC datasheet.
|
|
|
|
*/
|
|
|
|
static u8 sht15_crc8(struct sht15_data *data,
|
|
|
|
const u8 *value,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
u8 crc = sht15_reverse(data->val_status & 0x0F);
|
|
|
|
|
|
|
|
while (len--) {
|
|
|
|
crc = sht15_crc8_table[*value ^ crc];
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
|
|
|
* sht15_connection_reset() - reset the comms interface
|
|
|
|
* @data: sht15 specific data
|
|
|
|
*
|
|
|
|
* This implements section 3.4 of the data sheet
|
|
|
|
*/
|
2013-01-15 22:33:06 +04:00
|
|
|
static int sht15_connection_reset(struct sht15_data *data)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int i, err;
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
err = gpio_direction_output(data->pdata->gpio_data, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-04-14 01:39:45 +04:00
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
for (i = 0; i < 9; ++i) {
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
}
|
2013-01-15 22:33:06 +04:00
|
|
|
return 0;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
|
|
|
* sht15_send_bit() - send an individual bit to the device
|
|
|
|
* @data: device state data
|
|
|
|
* @val: value of bit to be sent
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static inline void sht15_send_bit(struct sht15_data *data, int val)
|
|
|
|
{
|
|
|
|
gpio_set_value(data->pdata->gpio_data, val);
|
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL); /* clock low time */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_transmission_start() - specific sequence for new transmission
|
|
|
|
* @data: device state data
|
2011-04-12 23:34:36 +04:00
|
|
|
*
|
2009-04-14 01:39:45 +04:00
|
|
|
* Timings for this are not documented on the data sheet, so very
|
|
|
|
* conservative ones used in implementation. This implements
|
|
|
|
* figure 12 on the data sheet.
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2013-01-15 22:33:06 +04:00
|
|
|
static int sht15_transmission_start(struct sht15_data *data)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/* ensure data is high and output */
|
2013-01-15 22:33:06 +04:00
|
|
|
err = gpio_direction_output(data->pdata->gpio_data, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-04-14 01:39:45 +04:00
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
gpio_set_value(data->pdata->gpio_data, 0);
|
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
gpio_set_value(data->pdata->gpio_data, 1);
|
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
2013-01-15 22:33:06 +04:00
|
|
|
return 0;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
|
|
|
* sht15_send_byte() - send a single byte to the device
|
|
|
|
* @data: device state
|
|
|
|
* @byte: value to be sent
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static void sht15_send_byte(struct sht15_data *data, u8 byte)
|
|
|
|
{
|
|
|
|
int i;
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
sht15_send_bit(data, !!(byte & 0x80));
|
|
|
|
byte <<= 1;
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
|
|
|
* sht15_wait_for_response() - checks for ack from device
|
|
|
|
* @data: device state
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static int sht15_wait_for_response(struct sht15_data *data)
|
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gpio_direction_input(data->pdata->gpio_data);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-04-14 01:39:45 +04:00
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
if (gpio_get_value(data->pdata->gpio_data)) {
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
dev_err(data->dev, "Command not acknowledged\n");
|
2013-01-15 22:33:06 +04:00
|
|
|
err = sht15_connection_reset(data);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-04-14 01:39:45 +04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_send_cmd() - Sends a command to the device.
|
|
|
|
* @data: device state
|
|
|
|
* @cmd: command byte to be sent
|
|
|
|
*
|
|
|
|
* On entry, sck is output low, data is output pull high
|
|
|
|
* and the interrupt disabled.
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
|
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
err = sht15_transmission_start(data);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-04-14 01:39:45 +04:00
|
|
|
sht15_send_byte(data, cmd);
|
2013-01-15 22:33:06 +04:00
|
|
|
return sht15_wait_for_response(data);
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2011-04-12 23:34:37 +04:00
|
|
|
/**
|
|
|
|
* sht15_soft_reset() - send a soft reset command
|
|
|
|
* @data: sht15 specific data.
|
|
|
|
*
|
|
|
|
* As described in section 3.2 of the datasheet.
|
|
|
|
*/
|
|
|
|
static int sht15_soft_reset(struct sht15_data *data)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
msleep(SHT15_TSRST);
|
2011-04-12 23:34:38 +04:00
|
|
|
/* device resets default hardware status register value */
|
|
|
|
data->val_status = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
/**
|
|
|
|
* sht15_ack() - send a ack
|
|
|
|
* @data: sht15 specific data.
|
|
|
|
*
|
|
|
|
* Each byte of data is acknowledged by pulling the data line
|
|
|
|
* low for one clock pulse.
|
|
|
|
*/
|
2013-01-15 22:33:06 +04:00
|
|
|
static int sht15_ack(struct sht15_data *data)
|
2011-04-12 23:34:39 +04:00
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gpio_direction_output(data->pdata->gpio_data, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-04-12 23:34:39 +04:00
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_data, 1);
|
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
return gpio_direction_input(data->pdata->gpio_data);
|
2011-04-12 23:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
/**
|
|
|
|
* sht15_end_transmission() - notify device of end of transmission
|
|
|
|
* @data: device state.
|
|
|
|
*
|
|
|
|
* This is basically a NAK (single clock pulse, data high).
|
|
|
|
*/
|
2013-01-15 22:33:06 +04:00
|
|
|
static int sht15_end_transmission(struct sht15_data *data)
|
2011-04-12 23:34:38 +04:00
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gpio_direction_output(data->pdata->gpio_data, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-04-12 23:34:38 +04:00
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
2013-01-15 22:33:06 +04:00
|
|
|
return 0;
|
2011-04-12 23:34:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_read_byte() - Read a byte back from the device
|
|
|
|
* @data: device state.
|
|
|
|
*/
|
|
|
|
static u8 sht15_read_byte(struct sht15_data *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u8 byte = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
byte <<= 1;
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 1);
|
|
|
|
ndelay(SHT15_TSCKH);
|
|
|
|
byte |= !!gpio_get_value(data->pdata->gpio_data);
|
|
|
|
gpio_set_value(data->pdata->gpio_sck, 0);
|
|
|
|
ndelay(SHT15_TSCKL);
|
|
|
|
}
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_send_status() - write the status register byte
|
|
|
|
* @data: sht15 specific data.
|
|
|
|
* @status: the byte to set the status register with.
|
|
|
|
*
|
|
|
|
* As described in figure 14 and table 5 of the datasheet.
|
|
|
|
*/
|
|
|
|
static int sht15_send_status(struct sht15_data *data, u8 status)
|
|
|
|
{
|
2013-01-15 22:33:06 +04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
err = gpio_direction_output(data->pdata->gpio_data, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-04-12 23:34:38 +04:00
|
|
|
ndelay(SHT15_TSU);
|
|
|
|
sht15_send_byte(data, status);
|
2013-01-15 22:33:06 +04:00
|
|
|
err = sht15_wait_for_response(data);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-04-12 23:34:37 +04:00
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
data->val_status = status;
|
2011-04-12 23:34:37 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
/**
|
|
|
|
* sht15_update_status() - get updated status register from device if too old
|
|
|
|
* @data: device instance specific data.
|
|
|
|
*
|
|
|
|
* As described in figure 15 and table 5 of the datasheet.
|
|
|
|
*/
|
|
|
|
static int sht15_update_status(struct sht15_data *data)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
u8 status;
|
2011-04-12 23:34:39 +04:00
|
|
|
u8 previous_config;
|
|
|
|
u8 dev_checksum = 0;
|
|
|
|
u8 checksum_vals[2];
|
2011-04-12 23:34:38 +04:00
|
|
|
int timeout = HZ;
|
|
|
|
|
|
|
|
mutex_lock(&data->read_lock);
|
|
|
|
if (time_after(jiffies, data->last_status + timeout)
|
|
|
|
|| !data->status_valid) {
|
|
|
|
ret = sht15_send_cmd(data, SHT15_READ_STATUS);
|
|
|
|
if (ret)
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:38 +04:00
|
|
|
status = sht15_read_byte(data);
|
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
if (data->checksumming) {
|
|
|
|
sht15_ack(data);
|
|
|
|
dev_checksum = sht15_reverse(sht15_read_byte(data));
|
|
|
|
checksum_vals[0] = SHT15_READ_STATUS;
|
|
|
|
checksum_vals[1] = status;
|
|
|
|
data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
|
|
|
|
== dev_checksum);
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
ret = sht15_end_transmission(data);
|
|
|
|
if (ret)
|
|
|
|
goto unlock;
|
2011-04-12 23:34:38 +04:00
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
/*
|
|
|
|
* Perform checksum validation on the received data.
|
|
|
|
* Specification mentions that in case a checksum verification
|
|
|
|
* fails, a soft reset command must be sent to the device.
|
|
|
|
*/
|
|
|
|
if (data->checksumming && !data->checksum_ok) {
|
|
|
|
previous_config = data->val_status & 0x07;
|
|
|
|
ret = sht15_soft_reset(data);
|
|
|
|
if (ret)
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:39 +04:00
|
|
|
if (previous_config) {
|
|
|
|
ret = sht15_send_status(data, previous_config);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(data->dev,
|
|
|
|
"CRC validation failed, unable "
|
|
|
|
"to restore device settings\n");
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = -EAGAIN;
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:39 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
data->val_status = status;
|
|
|
|
data->status_valid = true;
|
|
|
|
data->last_status = jiffies;
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
unlock:
|
|
|
|
mutex_unlock(&data->read_lock);
|
2011-04-12 23:34:38 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/**
|
2011-04-12 23:34:36 +04:00
|
|
|
* sht15_measurement() - get a new value from device
|
2009-04-14 01:39:45 +04:00
|
|
|
* @data: device instance specific data
|
|
|
|
* @command: command sent to request value
|
|
|
|
* @timeout_msecs: timeout after which comms are assumed
|
|
|
|
* to have failed are reset.
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
|
|
|
static int sht15_measurement(struct sht15_data *data,
|
|
|
|
int command,
|
|
|
|
int timeout_msecs)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
|
|
|
int ret;
|
2011-04-12 23:34:39 +04:00
|
|
|
u8 previous_config;
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
ret = sht15_send_cmd(data, command);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
ret = gpio_direction_input(data->pdata->gpio_data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-04-14 01:39:45 +04:00
|
|
|
atomic_set(&data->interrupt_handled, 0);
|
|
|
|
|
|
|
|
enable_irq(gpio_to_irq(data->pdata->gpio_data));
|
|
|
|
if (gpio_get_value(data->pdata->gpio_data) == 0) {
|
|
|
|
disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
|
2011-03-31 05:57:33 +04:00
|
|
|
/* Only relevant if the interrupt hasn't occurred. */
|
2009-04-14 01:39:45 +04:00
|
|
|
if (!atomic_read(&data->interrupt_handled))
|
|
|
|
schedule_work(&data->read_work);
|
|
|
|
}
|
|
|
|
ret = wait_event_timeout(data->wait_queue,
|
2011-04-12 23:34:36 +04:00
|
|
|
(data->state == SHT15_READING_NOTHING),
|
2009-04-14 01:39:45 +04:00
|
|
|
msecs_to_jiffies(timeout_msecs));
|
2013-01-15 22:33:06 +04:00
|
|
|
if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
|
|
|
|
data->state = SHT15_READING_NOTHING;
|
|
|
|
return -EIO;
|
|
|
|
} else if (ret == 0) { /* timeout occurred */
|
2009-07-11 15:42:37 +04:00
|
|
|
disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
|
2013-01-15 22:33:06 +04:00
|
|
|
ret = sht15_connection_reset(data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-04-14 01:39:45 +04:00
|
|
|
return -ETIME;
|
|
|
|
}
|
2011-04-12 23:34:39 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform checksum validation on the received data.
|
|
|
|
* Specification mentions that in case a checksum verification fails,
|
|
|
|
* a soft reset command must be sent to the device.
|
|
|
|
*/
|
|
|
|
if (data->checksumming && !data->checksum_ok) {
|
|
|
|
previous_config = data->val_status & 0x07;
|
|
|
|
ret = sht15_soft_reset(data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (previous_config) {
|
|
|
|
ret = sht15_send_status(data, previous_config);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(data->dev,
|
|
|
|
"CRC validation failed, unable "
|
|
|
|
"to restore device settings\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-12 23:34:36 +04:00
|
|
|
* sht15_update_measurements() - get updated measures from device if too old
|
2009-04-14 01:39:45 +04:00
|
|
|
* @data: device state
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
|
|
|
static int sht15_update_measurements(struct sht15_data *data)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int timeout = HZ;
|
|
|
|
|
|
|
|
mutex_lock(&data->read_lock);
|
2011-04-12 23:34:36 +04:00
|
|
|
if (time_after(jiffies, data->last_measurement + timeout)
|
|
|
|
|| !data->measurements_valid) {
|
|
|
|
data->state = SHT15_READING_HUMID;
|
|
|
|
ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
|
2009-04-14 01:39:45 +04:00
|
|
|
if (ret)
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:36 +04:00
|
|
|
data->state = SHT15_READING_TEMP;
|
|
|
|
ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
|
2009-04-14 01:39:45 +04:00
|
|
|
if (ret)
|
2013-01-15 22:33:06 +04:00
|
|
|
goto unlock;
|
2011-04-12 23:34:36 +04:00
|
|
|
data->measurements_valid = true;
|
|
|
|
data->last_measurement = jiffies;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
2013-01-15 22:33:06 +04:00
|
|
|
unlock:
|
|
|
|
mutex_unlock(&data->read_lock);
|
2009-04-14 01:39:45 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_calc_temp() - convert the raw reading to a temperature
|
|
|
|
* @data: device state
|
|
|
|
*
|
|
|
|
* As per section 4.3 of the data sheet.
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static inline int sht15_calc_temp(struct sht15_data *data)
|
|
|
|
{
|
2010-04-14 18:14:07 +04:00
|
|
|
int d1 = temppoints[0].d1;
|
2011-04-12 23:34:38 +04:00
|
|
|
int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
|
2009-04-14 01:39:45 +04:00
|
|
|
int i;
|
|
|
|
|
2010-04-14 18:14:07 +04:00
|
|
|
for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
|
2009-04-14 01:39:45 +04:00
|
|
|
/* Find pointer to interpolate */
|
2013-01-07 23:18:38 +04:00
|
|
|
if (data->supply_uv > temppoints[i - 1].vdd) {
|
|
|
|
d1 = (data->supply_uv - temppoints[i - 1].vdd)
|
2009-04-14 01:39:45 +04:00
|
|
|
* (temppoints[i].d1 - temppoints[i - 1].d1)
|
|
|
|
/ (temppoints[i].vdd - temppoints[i - 1].vdd)
|
|
|
|
+ temppoints[i - 1].d1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
return data->val_temp * d2 + d1;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_calc_humid() - using last temperature convert raw to humid
|
|
|
|
* @data: device state
|
|
|
|
*
|
|
|
|
* This is the temperature compensated version as per section 4.2 of
|
|
|
|
* the data sheet.
|
2011-04-12 23:34:36 +04:00
|
|
|
*
|
|
|
|
* The sensor is assumed to be V3, which is compatible with V4.
|
|
|
|
* Humidity conversion coefficients are shown in table 7 of the datasheet.
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static inline int sht15_calc_humid(struct sht15_data *data)
|
|
|
|
{
|
2011-04-12 23:34:36 +04:00
|
|
|
int rh_linear; /* milli percent */
|
2009-04-14 01:39:45 +04:00
|
|
|
int temp = sht15_calc_temp(data);
|
2011-04-12 23:34:38 +04:00
|
|
|
int c2, c3;
|
|
|
|
int t2;
|
2009-04-14 01:39:45 +04:00
|
|
|
const int c1 = -4;
|
2011-04-12 23:34:38 +04:00
|
|
|
|
|
|
|
if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
|
|
|
|
c2 = 648000; /* x 10 ^ -6 */
|
|
|
|
c3 = -7200; /* x 10 ^ -7 */
|
|
|
|
t2 = 1280;
|
|
|
|
} else {
|
|
|
|
c2 = 40500; /* x 10 ^ -6 */
|
|
|
|
c3 = -28; /* x 10 ^ -7 */
|
|
|
|
t2 = 80;
|
|
|
|
}
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
rh_linear = c1 * 1000
|
|
|
|
+ c2 * data->val_humid / 1000
|
2011-03-21 19:59:35 +03:00
|
|
|
+ (data->val_humid * data->val_humid * c3) / 10000;
|
2011-04-12 23:34:38 +04:00
|
|
|
return (temp - 25000) * (10000 + t2 * data->val_humid)
|
2011-04-12 23:34:36 +04:00
|
|
|
/ 1000000 + rh_linear;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
/**
|
|
|
|
* sht15_show_status() - show status information in sysfs
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute.
|
|
|
|
* @buf: sysfs buffer where information is written to.
|
|
|
|
*
|
|
|
|
* Will be called on read access to temp1_fault, humidity1_fault
|
|
|
|
* and heater_enable sysfs attributes.
|
|
|
|
* Returns number of bytes written into buffer, negative errno on error.
|
|
|
|
*/
|
|
|
|
static ssize_t sht15_show_status(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sht15_data *data = dev_get_drvdata(dev);
|
|
|
|
u8 bit = to_sensor_dev_attr(attr)->index;
|
|
|
|
|
|
|
|
ret = sht15_update_status(data);
|
|
|
|
|
|
|
|
return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_store_heater() - change heater state via sysfs
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute.
|
|
|
|
* @buf: sysfs buffer to read the new heater state from.
|
|
|
|
* @count: length of the data.
|
|
|
|
*
|
2011-07-25 23:46:10 +04:00
|
|
|
* Will be called on write access to heater_enable sysfs attribute.
|
2011-04-12 23:34:38 +04:00
|
|
|
* Returns number of bytes actually decoded, negative errno on error.
|
|
|
|
*/
|
|
|
|
static ssize_t sht15_store_heater(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sht15_data *data = dev_get_drvdata(dev);
|
|
|
|
long value;
|
|
|
|
u8 status;
|
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &value))
|
2011-04-12 23:34:38 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&data->read_lock);
|
|
|
|
status = data->val_status & 0x07;
|
|
|
|
if (!!value)
|
|
|
|
status |= SHT15_STATUS_HEATER;
|
|
|
|
else
|
|
|
|
status &= ~SHT15_STATUS_HEATER;
|
|
|
|
|
|
|
|
ret = sht15_send_status(data, status);
|
|
|
|
mutex_unlock(&data->read_lock);
|
|
|
|
|
|
|
|
return ret ? ret : count;
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/**
|
|
|
|
* sht15_show_temp() - show temperature measurement value in sysfs
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute.
|
|
|
|
* @buf: sysfs buffer where measurement values are written to.
|
|
|
|
*
|
|
|
|
* Will be called on read access to temp1_input sysfs attribute.
|
|
|
|
* Returns number of bytes written into buffer, negative errno on error.
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static ssize_t sht15_show_temp(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sht15_data *data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
/* Technically no need to read humidity as well */
|
2011-04-12 23:34:36 +04:00
|
|
|
ret = sht15_update_measurements(data);
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
return ret ? ret : sprintf(buf, "%d\n",
|
|
|
|
sht15_calc_temp(data));
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/**
|
|
|
|
* sht15_show_humidity() - show humidity measurement value in sysfs
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute.
|
|
|
|
* @buf: sysfs buffer where measurement values are written to.
|
|
|
|
*
|
|
|
|
* Will be called on read access to humidity1_input sysfs attribute.
|
|
|
|
* Returns number of bytes written into buffer, negative errno on error.
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static ssize_t sht15_show_humidity(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sht15_data *data = dev_get_drvdata(dev);
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
ret = sht15_update_measurements(data);
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
|
2011-04-12 23:34:36 +04:00
|
|
|
}
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
static ssize_t show_name(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
return sprintf(buf, "%s\n", pdev->name);
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
|
|
|
|
sht15_show_temp, NULL, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
|
|
|
|
sht15_show_humidity, NULL, 0);
|
2011-04-12 23:34:38 +04:00
|
|
|
static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
|
|
|
|
SHT15_STATUS_LOW_BATTERY);
|
|
|
|
static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
|
|
|
|
SHT15_STATUS_LOW_BATTERY);
|
|
|
|
static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
|
|
|
|
sht15_store_heater, SHT15_STATUS_HEATER);
|
2009-04-14 01:39:45 +04:00
|
|
|
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
|
|
|
|
static struct attribute *sht15_attrs[] = {
|
|
|
|
&sensor_dev_attr_temp1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_humidity1_input.dev_attr.attr,
|
2011-04-12 23:34:38 +04:00
|
|
|
&sensor_dev_attr_temp1_fault.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_humidity1_fault.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_heater_enable.dev_attr.attr,
|
2009-04-14 01:39:45 +04:00
|
|
|
&dev_attr_name.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group sht15_attr_group = {
|
|
|
|
.attrs = sht15_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t sht15_interrupt_fired(int irq, void *d)
|
|
|
|
{
|
|
|
|
struct sht15_data *data = d;
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/* First disable the interrupt */
|
|
|
|
disable_irq_nosync(irq);
|
|
|
|
atomic_inc(&data->interrupt_handled);
|
|
|
|
/* Then schedule a reading work struct */
|
2011-04-12 23:34:36 +04:00
|
|
|
if (data->state != SHT15_READING_NOTHING)
|
2009-04-14 01:39:45 +04:00
|
|
|
schedule_work(&data->read_work);
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sht15_bh_read_data(struct work_struct *work_s)
|
|
|
|
{
|
|
|
|
uint16_t val = 0;
|
2011-04-12 23:34:39 +04:00
|
|
|
u8 dev_checksum = 0;
|
|
|
|
u8 checksum_vals[3];
|
2009-04-14 01:39:45 +04:00
|
|
|
struct sht15_data *data
|
|
|
|
= container_of(work_s, struct sht15_data,
|
|
|
|
read_work);
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/* Firstly, verify the line is low */
|
|
|
|
if (gpio_get_value(data->pdata->gpio_data)) {
|
2011-04-12 23:34:36 +04:00
|
|
|
/*
|
|
|
|
* If not, then start the interrupt again - care here as could
|
|
|
|
* have gone low in meantime so verify it hasn't!
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
atomic_set(&data->interrupt_handled, 0);
|
|
|
|
enable_irq(gpio_to_irq(data->pdata->gpio_data));
|
2012-01-08 22:34:06 +04:00
|
|
|
/* If still not occurred or another handler was scheduled */
|
2009-04-14 01:39:45 +04:00
|
|
|
if (gpio_get_value(data->pdata->gpio_data)
|
|
|
|
|| atomic_read(&data->interrupt_handled))
|
|
|
|
return;
|
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/* Read the data back from the device */
|
2011-04-12 23:34:38 +04:00
|
|
|
val = sht15_read_byte(data);
|
|
|
|
val <<= 8;
|
2013-01-15 22:33:06 +04:00
|
|
|
if (sht15_ack(data))
|
|
|
|
goto wakeup;
|
2011-04-12 23:34:38 +04:00
|
|
|
val |= sht15_read_byte(data);
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2011-04-12 23:34:39 +04:00
|
|
|
if (data->checksumming) {
|
|
|
|
/*
|
|
|
|
* Ask the device for a checksum and read it back.
|
|
|
|
* Note: the device sends the checksum byte reversed.
|
|
|
|
*/
|
2013-01-15 22:33:06 +04:00
|
|
|
if (sht15_ack(data))
|
|
|
|
goto wakeup;
|
2011-04-12 23:34:39 +04:00
|
|
|
dev_checksum = sht15_reverse(sht15_read_byte(data));
|
|
|
|
checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
|
|
|
|
SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
|
|
|
|
checksum_vals[1] = (u8) (val >> 8);
|
|
|
|
checksum_vals[2] = (u8) val;
|
|
|
|
data->checksum_ok
|
|
|
|
= (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
|
|
|
|
}
|
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
/* Tell the device we are done */
|
2013-01-15 22:33:06 +04:00
|
|
|
if (sht15_end_transmission(data))
|
|
|
|
goto wakeup;
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
switch (data->state) {
|
2009-04-14 01:39:45 +04:00
|
|
|
case SHT15_READING_TEMP:
|
|
|
|
data->val_temp = val;
|
|
|
|
break;
|
|
|
|
case SHT15_READING_HUMID:
|
|
|
|
data->val_humid = val;
|
|
|
|
break;
|
2011-04-12 23:34:36 +04:00
|
|
|
default:
|
|
|
|
break;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
data->state = SHT15_READING_NOTHING;
|
2013-01-15 22:33:06 +04:00
|
|
|
wakeup:
|
2009-04-14 01:39:45 +04:00
|
|
|
wake_up(&data->wait_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sht15_update_voltage(struct work_struct *work_s)
|
|
|
|
{
|
|
|
|
struct sht15_data *data
|
|
|
|
= container_of(work_s, struct sht15_data,
|
|
|
|
update_supply_work);
|
2013-01-07 23:18:38 +04:00
|
|
|
data->supply_uv = regulator_get_voltage(data->reg);
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
|
|
|
|
* @nb: associated notification structure
|
|
|
|
* @event: voltage regulator state change event code
|
|
|
|
* @ignored: function parameter - ignored here
|
|
|
|
*
|
|
|
|
* Note that as the notification code holds the regulator lock, we have
|
|
|
|
* to schedule an update of the supply voltage rather than getting it directly.
|
2011-04-12 23:34:36 +04:00
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
static int sht15_invalidate_voltage(struct notifier_block *nb,
|
2011-04-12 23:34:36 +04:00
|
|
|
unsigned long event,
|
|
|
|
void *ignored)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
|
|
|
struct sht15_data *data = container_of(nb, struct sht15_data, nb);
|
|
|
|
|
|
|
|
if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
|
2013-01-07 23:18:38 +04:00
|
|
|
data->supply_uv_valid = false;
|
2009-04-14 01:39:45 +04:00
|
|
|
schedule_work(&data->update_supply_work);
|
|
|
|
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-19 22:22:35 +04:00
|
|
|
static int sht15_probe(struct platform_device *pdev)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
2012-01-27 00:59:00 +04:00
|
|
|
int ret;
|
2012-06-02 22:20:22 +04:00
|
|
|
struct sht15_data *data;
|
2011-04-12 23:34:38 +04:00
|
|
|
u8 status = 0;
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2012-06-02 22:20:22 +04:00
|
|
|
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
INIT_WORK(&data->read_work, sht15_bh_read_data);
|
|
|
|
INIT_WORK(&data->update_supply_work, sht15_update_voltage);
|
|
|
|
platform_set_drvdata(pdev, data);
|
|
|
|
mutex_init(&data->read_lock);
|
|
|
|
data->dev = &pdev->dev;
|
|
|
|
init_waitqueue_head(&data->wait_queue);
|
|
|
|
|
2013-07-30 12:13:06 +04:00
|
|
|
if (dev_get_platdata(&pdev->dev) == NULL) {
|
2011-04-12 23:34:36 +04:00
|
|
|
dev_err(&pdev->dev, "no platform data supplied\n");
|
2012-06-02 22:20:22 +04:00
|
|
|
return -EINVAL;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2013-07-30 12:13:06 +04:00
|
|
|
data->pdata = dev_get_platdata(&pdev->dev);
|
2013-01-07 23:18:38 +04:00
|
|
|
data->supply_uv = data->pdata->supply_mv * 1000;
|
2011-04-12 23:34:39 +04:00
|
|
|
if (data->pdata->checksum)
|
|
|
|
data->checksumming = true;
|
2011-04-12 23:34:38 +04:00
|
|
|
if (data->pdata->no_otp_reload)
|
|
|
|
status |= SHT15_STATUS_NO_OTP_RELOAD;
|
|
|
|
if (data->pdata->low_resolution)
|
|
|
|
status |= SHT15_STATUS_LOW_RESOLUTION;
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/*
|
|
|
|
* If a regulator is available,
|
|
|
|
* query what the supply voltage actually is!
|
|
|
|
*/
|
2013-08-18 20:35:52 +04:00
|
|
|
data->reg = devm_regulator_get_optional(data->dev, "vcc");
|
2009-04-14 01:39:45 +04:00
|
|
|
if (!IS_ERR(data->reg)) {
|
2010-04-14 18:14:08 +04:00
|
|
|
int voltage;
|
|
|
|
|
|
|
|
voltage = regulator_get_voltage(data->reg);
|
|
|
|
if (voltage)
|
2013-01-07 23:18:38 +04:00
|
|
|
data->supply_uv = voltage;
|
2010-04-14 18:14:08 +04:00
|
|
|
|
2013-03-02 11:33:30 +04:00
|
|
|
ret = regulator_enable(data->reg);
|
|
|
|
if (ret != 0) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"failed to enable regulator: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/*
|
|
|
|
* Setup a notifier block to update this if another device
|
|
|
|
* causes the voltage to change
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
data->nb.notifier_call = &sht15_invalidate_voltage;
|
|
|
|
ret = regulator_register_notifier(data->reg, &data->nb);
|
2011-04-12 23:34:37 +04:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"regulator notifier request failed\n");
|
|
|
|
regulator_disable(data->reg);
|
2012-06-02 22:20:22 +04:00
|
|
|
return ret;
|
2011-04-12 23:34:37 +04:00
|
|
|
}
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
|
|
|
/* Try requesting the GPIOs */
|
2013-01-15 22:33:06 +04:00
|
|
|
ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
|
|
|
|
GPIOF_OUT_INIT_LOW, "SHT15 sck");
|
2009-04-14 01:39:45 +04:00
|
|
|
if (ret) {
|
2013-01-15 22:33:06 +04:00
|
|
|
dev_err(&pdev->dev, "clock line GPIO request failed\n");
|
2011-04-12 23:34:37 +04:00
|
|
|
goto err_release_reg;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2012-06-02 22:20:22 +04:00
|
|
|
ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
|
|
|
|
"SHT15 data");
|
2009-04-14 01:39:45 +04:00
|
|
|
if (ret) {
|
2013-01-15 22:33:06 +04:00
|
|
|
dev_err(&pdev->dev, "data line GPIO request failed\n");
|
2012-06-02 22:20:22 +04:00
|
|
|
goto err_release_reg;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
|
2012-06-02 22:20:22 +04:00
|
|
|
ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
|
|
|
|
sht15_interrupt_fired,
|
|
|
|
IRQF_TRIGGER_FALLING,
|
|
|
|
"sht15 data",
|
|
|
|
data);
|
2009-04-14 01:39:45 +04:00
|
|
|
if (ret) {
|
2011-04-12 23:34:36 +04:00
|
|
|
dev_err(&pdev->dev, "failed to get irq for data line\n");
|
2012-06-02 22:20:22 +04:00
|
|
|
goto err_release_reg;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
|
|
|
disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
|
2013-01-15 22:33:06 +04:00
|
|
|
ret = sht15_connection_reset(data);
|
|
|
|
if (ret)
|
|
|
|
goto err_release_reg;
|
2011-04-12 23:34:37 +04:00
|
|
|
ret = sht15_soft_reset(data);
|
|
|
|
if (ret)
|
2012-06-02 22:20:22 +04:00
|
|
|
goto err_release_reg;
|
2011-04-12 23:34:37 +04:00
|
|
|
|
2011-04-12 23:34:38 +04:00
|
|
|
/* write status with platform data options */
|
|
|
|
if (status) {
|
|
|
|
ret = sht15_send_status(data, status);
|
|
|
|
if (ret)
|
2012-06-02 22:20:22 +04:00
|
|
|
goto err_release_reg;
|
2011-04-12 23:34:38 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 23:34:37 +04:00
|
|
|
ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "sysfs create failed\n");
|
2012-06-02 22:20:22 +04:00
|
|
|
goto err_release_reg;
|
2011-04-12 23:34:37 +04:00
|
|
|
}
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
data->hwmon_dev = hwmon_device_register(data->dev);
|
|
|
|
if (IS_ERR(data->hwmon_dev)) {
|
|
|
|
ret = PTR_ERR(data->hwmon_dev);
|
2011-04-12 23:34:37 +04:00
|
|
|
goto err_release_sysfs_group;
|
2009-04-14 01:39:45 +04:00
|
|
|
}
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
return 0;
|
|
|
|
|
2011-04-12 23:34:37 +04:00
|
|
|
err_release_sysfs_group:
|
|
|
|
sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
|
|
|
|
err_release_reg:
|
|
|
|
if (!IS_ERR(data->reg)) {
|
|
|
|
regulator_unregister_notifier(data->reg, &data->nb);
|
|
|
|
regulator_disable(data->reg);
|
|
|
|
}
|
2009-04-14 01:39:45 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-19 22:25:51 +04:00
|
|
|
static int sht15_remove(struct platform_device *pdev)
|
2009-04-14 01:39:45 +04:00
|
|
|
{
|
|
|
|
struct sht15_data *data = platform_get_drvdata(pdev);
|
|
|
|
|
2011-04-12 23:34:36 +04:00
|
|
|
/*
|
|
|
|
* Make sure any reads from the device are done and
|
|
|
|
* prevent new ones beginning
|
|
|
|
*/
|
2009-04-14 01:39:45 +04:00
|
|
|
mutex_lock(&data->read_lock);
|
2011-04-12 23:34:38 +04:00
|
|
|
if (sht15_soft_reset(data)) {
|
|
|
|
mutex_unlock(&data->read_lock);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2009-04-14 01:39:45 +04:00
|
|
|
hwmon_device_unregister(data->hwmon_dev);
|
|
|
|
sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
|
|
|
|
if (!IS_ERR(data->reg)) {
|
|
|
|
regulator_unregister_notifier(data->reg, &data->nb);
|
|
|
|
regulator_disable(data->reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&data->read_lock);
|
2011-04-12 23:34:36 +04:00
|
|
|
|
2009-04-14 01:39:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 05:46:19 +04:00
|
|
|
static struct platform_device_id sht15_device_ids[] = {
|
|
|
|
{ "sht10", sht10 },
|
|
|
|
{ "sht11", sht11 },
|
|
|
|
{ "sht15", sht15 },
|
|
|
|
{ "sht71", sht71 },
|
|
|
|
{ "sht75", sht75 },
|
|
|
|
{ }
|
2009-04-14 01:39:45 +04:00
|
|
|
};
|
2012-08-31 05:46:19 +04:00
|
|
|
MODULE_DEVICE_TABLE(platform, sht15_device_ids);
|
2009-04-14 01:39:45 +04:00
|
|
|
|
2012-08-31 05:46:19 +04:00
|
|
|
static struct platform_driver sht15_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "sht15",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
.probe = sht15_probe,
|
2012-11-19 22:21:20 +04:00
|
|
|
.remove = sht15_remove,
|
2012-08-31 05:46:19 +04:00
|
|
|
.id_table = sht15_device_ids,
|
|
|
|
};
|
|
|
|
module_platform_driver(sht15_driver);
|
2009-04-14 01:39:45 +04:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2012-08-31 05:46:19 +04:00
|
|
|
MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");
|