blob: 97cd45a8432c03f7a87bceaac5bbe5c8e038683e [file] [log] [blame]
Jonathan Cameron251eb402009-04-13 14:39:45 -07001/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
Vivien Didelotedec5af2012-08-30 21:46:19 -04004 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
Jerome Oufella82c74652011-04-12 15:34:39 -04005 * Jerome Oufella <jerome.oufella@savoirfairelinux.com>
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04006 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 *
Jonathan Cameron251eb402009-04-13 14:39:45 -07008 * Copyright (c) 2009 Jonathan Cameron
9 *
10 * Copyright (c) 2007 Wouter Horre
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
Vivien Didelot99a03782011-04-12 15:34:36 -040016 * For further information, see the Documentation/hwmon/sht15 file.
Jonathan Cameron251eb402009-04-13 14:39:45 -070017 */
18
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/gpio.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
26#include <linux/mutex.h>
Vivien Didelotf9b693e2012-09-12 12:23:44 -040027#include <linux/platform_data/sht15.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070028#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040029#include <linux/sched.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070030#include <linux/delay.h>
31#include <linux/jiffies.h>
32#include <linux/err.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070033#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070036
Vivien Didelot99a03782011-04-12 15:34:36 -040037/* Commands */
38#define SHT15_MEASURE_TEMP 0x03
39#define SHT15_MEASURE_RH 0x05
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040040#define SHT15_WRITE_STATUS 0x06
41#define SHT15_READ_STATUS 0x07
Vivien Didelot181148a2011-04-12 15:34:37 -040042#define SHT15_SOFT_RESET 0x1E
Jonathan Cameron251eb402009-04-13 14:39:45 -070043
Vivien Didelot99a03782011-04-12 15:34:36 -040044/* Min timings */
45#define SHT15_TSCKL 100 /* (nsecs) clock low */
46#define SHT15_TSCKH 100 /* (nsecs) clock high */
47#define SHT15_TSU 150 /* (nsecs) data setup time */
Vivien Didelot181148a2011-04-12 15:34:37 -040048#define SHT15_TSRST 11 /* (msecs) soft reset time */
Jonathan Cameron251eb402009-04-13 14:39:45 -070049
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040050/* Status Register Bits */
51#define SHT15_STATUS_LOW_RESOLUTION 0x01
52#define SHT15_STATUS_NO_OTP_RELOAD 0x02
53#define SHT15_STATUS_HEATER 0x04
54#define SHT15_STATUS_LOW_BATTERY 0x40
55
Vivien Didelotedec5af2012-08-30 21:46:19 -040056/* List of supported chips */
57enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
58
Vivien Didelot99a03782011-04-12 15:34:36 -040059/* Actions the driver may be doing */
60enum sht15_state {
61 SHT15_READING_NOTHING,
62 SHT15_READING_TEMP,
63 SHT15_READING_HUMID
64};
Jonathan Cameron251eb402009-04-13 14:39:45 -070065
66/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030067 * struct sht15_temppair - elements of voltage dependent temp calc
Jonathan Cameron251eb402009-04-13 14:39:45 -070068 * @vdd: supply voltage in microvolts
69 * @d1: see data sheet
70 */
71struct sht15_temppair {
72 int vdd; /* microvolts */
73 int d1;
74};
75
Vivien Didelot99a03782011-04-12 15:34:36 -040076/* Table 9 from datasheet - relates temperature calculation to supply voltage */
Jonathan Cameron251eb402009-04-13 14:39:45 -070077static const struct sht15_temppair temppoints[] = {
78 { 2500000, -39400 },
79 { 3000000, -39600 },
80 { 3500000, -39700 },
81 { 4000000, -39800 },
82 { 5000000, -40100 },
83};
84
Jerome Oufella82c74652011-04-12 15:34:39 -040085/* Table from CRC datasheet, section 2.4 */
86static const u8 sht15_crc8_table[] = {
87 0, 49, 98, 83, 196, 245, 166, 151,
88 185, 136, 219, 234, 125, 76, 31, 46,
89 67, 114, 33, 16, 135, 182, 229, 212,
90 250, 203, 152, 169, 62, 15, 92, 109,
91 134, 183, 228, 213, 66, 115, 32, 17,
92 63, 14, 93, 108, 251, 202, 153, 168,
93 197, 244, 167, 150, 1, 48, 99, 82,
94 124, 77, 30, 47, 184, 137, 218, 235,
95 61, 12, 95, 110, 249, 200, 155, 170,
96 132, 181, 230, 215, 64, 113, 34, 19,
97 126, 79, 28, 45, 186, 139, 216, 233,
98 199, 246, 165, 148, 3, 50, 97, 80,
99 187, 138, 217, 232, 127, 78, 29, 44,
100 2, 51, 96, 81, 198, 247, 164, 149,
101 248, 201, 154, 171, 60, 13, 94, 111,
102 65, 112, 35, 18, 133, 180, 231, 214,
103 122, 75, 24, 41, 190, 143, 220, 237,
104 195, 242, 161, 144, 7, 54, 101, 84,
105 57, 8, 91, 106, 253, 204, 159, 174,
106 128, 177, 226, 211, 68, 117, 38, 23,
107 252, 205, 158, 175, 56, 9, 90, 107,
108 69, 116, 39, 22, 129, 176, 227, 210,
109 191, 142, 221, 236, 123, 74, 25, 40,
110 6, 55, 100, 85, 194, 243, 160, 145,
111 71, 118, 37, 20, 131, 178, 225, 208,
112 254, 207, 156, 173, 58, 11, 88, 105,
113 4, 53, 102, 87, 192, 241, 162, 147,
114 189, 140, 223, 238, 121, 72, 27, 42,
115 193, 240, 163, 146, 5, 52, 103, 86,
116 120, 73, 26, 43, 188, 141, 222, 239,
117 130, 179, 224, 209, 70, 119, 36, 21,
118 59, 10, 89, 104, 255, 206, 157, 172
119};
120
Jonathan Cameron251eb402009-04-13 14:39:45 -0700121/**
122 * struct sht15_data - device instance specific data
Vivien Didelot99a03782011-04-12 15:34:36 -0400123 * @pdata: platform data (gpio's etc).
124 * @read_work: bh of interrupt handler.
125 * @wait_queue: wait queue for getting values from device.
126 * @val_temp: last temperature value read from device.
127 * @val_humid: last humidity value read from device.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400128 * @val_status: last status register value read from device.
Jerome Oufella82c74652011-04-12 15:34:39 -0400129 * @checksum_ok: last value read from the device passed CRC validation.
130 * @checksumming: flag used to enable the data validation with CRC.
Vivien Didelot99a03782011-04-12 15:34:36 -0400131 * @state: state identifying the action the driver is doing.
132 * @measurements_valid: are the current stored measures valid (start condition).
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400133 * @status_valid: is the current stored status valid (start condition).
Vivien Didelot99a03782011-04-12 15:34:36 -0400134 * @last_measurement: time of last measure.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400135 * @last_status: time of last status reading.
Vivien Didelot99a03782011-04-12 15:34:36 -0400136 * @read_lock: mutex to ensure only one read in progress at a time.
137 * @dev: associate device structure.
138 * @hwmon_dev: device associated with hwmon subsystem.
139 * @reg: associated regulator (if specified).
140 * @nb: notifier block to handle notifications of voltage
141 * changes.
Vivien Didelot142c0902013-01-07 14:18:38 -0500142 * @supply_uv: local copy of supply voltage used to allow use of
Vivien Didelot99a03782011-04-12 15:34:36 -0400143 * regulator consumer if available.
Vivien Didelot142c0902013-01-07 14:18:38 -0500144 * @supply_uv_valid: indicates that an updated value has not yet been
Vivien Didelot99a03782011-04-12 15:34:36 -0400145 * obtained from the regulator and so any calculations
146 * based upon it will be invalid.
Vivien Didelot142c0902013-01-07 14:18:38 -0500147 * @update_supply_work: work struct that is used to update the supply_uv.
Vivien Didelot99a03782011-04-12 15:34:36 -0400148 * @interrupt_handled: flag used to indicate a handler has been scheduled.
Jonathan Cameron251eb402009-04-13 14:39:45 -0700149 */
150struct sht15_data {
151 struct sht15_platform_data *pdata;
152 struct work_struct read_work;
153 wait_queue_head_t wait_queue;
154 uint16_t val_temp;
155 uint16_t val_humid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400156 u8 val_status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400157 bool checksum_ok;
158 bool checksumming;
Vivien Didelot99a03782011-04-12 15:34:36 -0400159 enum sht15_state state;
160 bool measurements_valid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400161 bool status_valid;
Vivien Didelot99a03782011-04-12 15:34:36 -0400162 unsigned long last_measurement;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400163 unsigned long last_status;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700164 struct mutex read_lock;
165 struct device *dev;
166 struct device *hwmon_dev;
167 struct regulator *reg;
168 struct notifier_block nb;
Vivien Didelot142c0902013-01-07 14:18:38 -0500169 int supply_uv;
170 bool supply_uv_valid;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700171 struct work_struct update_supply_work;
172 atomic_t interrupt_handled;
173};
174
175/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400176 * sht15_reverse() - reverse a byte
177 * @byte: byte to reverse.
178 */
179static u8 sht15_reverse(u8 byte)
180{
181 u8 i, c;
182
183 for (c = 0, i = 0; i < 8; i++)
184 c |= (!!(byte & (1 << i))) << (7 - i);
185 return c;
186}
187
188/**
189 * sht15_crc8() - compute crc8
190 * @data: sht15 specific data.
191 * @value: sht15 retrieved data.
192 *
193 * This implements section 2 of the CRC datasheet.
194 */
195static u8 sht15_crc8(struct sht15_data *data,
196 const u8 *value,
197 int len)
198{
199 u8 crc = sht15_reverse(data->val_status & 0x0F);
200
201 while (len--) {
202 crc = sht15_crc8_table[*value ^ crc];
203 value++;
204 }
205
206 return crc;
207}
208
209/**
Jonathan Cameron251eb402009-04-13 14:39:45 -0700210 * sht15_connection_reset() - reset the comms interface
211 * @data: sht15 specific data
212 *
213 * This implements section 3.4 of the data sheet
214 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500215static int sht15_connection_reset(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700216{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500217 int i, err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400218
Vivien Didelot412e29c2013-01-15 13:33:06 -0500219 err = gpio_direction_output(data->pdata->gpio_data, 1);
220 if (err)
221 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700222 ndelay(SHT15_TSCKL);
223 gpio_set_value(data->pdata->gpio_sck, 0);
224 ndelay(SHT15_TSCKL);
225 for (i = 0; i < 9; ++i) {
226 gpio_set_value(data->pdata->gpio_sck, 1);
227 ndelay(SHT15_TSCKH);
228 gpio_set_value(data->pdata->gpio_sck, 0);
229 ndelay(SHT15_TSCKL);
230 }
Vivien Didelot412e29c2013-01-15 13:33:06 -0500231 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700232}
Vivien Didelot99a03782011-04-12 15:34:36 -0400233
Jonathan Cameron251eb402009-04-13 14:39:45 -0700234/**
235 * sht15_send_bit() - send an individual bit to the device
236 * @data: device state data
237 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400238 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700239static inline void sht15_send_bit(struct sht15_data *data, int val)
240{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700241 gpio_set_value(data->pdata->gpio_data, val);
242 ndelay(SHT15_TSU);
243 gpio_set_value(data->pdata->gpio_sck, 1);
244 ndelay(SHT15_TSCKH);
245 gpio_set_value(data->pdata->gpio_sck, 0);
246 ndelay(SHT15_TSCKL); /* clock low time */
247}
248
249/**
250 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700251 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400252 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700253 * Timings for this are not documented on the data sheet, so very
254 * conservative ones used in implementation. This implements
255 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400256 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500257static int sht15_transmission_start(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700258{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500259 int err;
260
Jonathan Cameron251eb402009-04-13 14:39:45 -0700261 /* ensure data is high and output */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500262 err = gpio_direction_output(data->pdata->gpio_data, 1);
263 if (err)
264 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700265 ndelay(SHT15_TSU);
266 gpio_set_value(data->pdata->gpio_sck, 0);
267 ndelay(SHT15_TSCKL);
268 gpio_set_value(data->pdata->gpio_sck, 1);
269 ndelay(SHT15_TSCKH);
270 gpio_set_value(data->pdata->gpio_data, 0);
271 ndelay(SHT15_TSU);
272 gpio_set_value(data->pdata->gpio_sck, 0);
273 ndelay(SHT15_TSCKL);
274 gpio_set_value(data->pdata->gpio_sck, 1);
275 ndelay(SHT15_TSCKH);
276 gpio_set_value(data->pdata->gpio_data, 1);
277 ndelay(SHT15_TSU);
278 gpio_set_value(data->pdata->gpio_sck, 0);
279 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500280 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700281}
Vivien Didelot99a03782011-04-12 15:34:36 -0400282
Jonathan Cameron251eb402009-04-13 14:39:45 -0700283/**
284 * sht15_send_byte() - send a single byte to the device
285 * @data: device state
286 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400287 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700288static void sht15_send_byte(struct sht15_data *data, u8 byte)
289{
290 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400291
Jonathan Cameron251eb402009-04-13 14:39:45 -0700292 for (i = 0; i < 8; i++) {
293 sht15_send_bit(data, !!(byte & 0x80));
294 byte <<= 1;
295 }
296}
Vivien Didelot99a03782011-04-12 15:34:36 -0400297
Jonathan Cameron251eb402009-04-13 14:39:45 -0700298/**
299 * sht15_wait_for_response() - checks for ack from device
300 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400301 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700302static int sht15_wait_for_response(struct sht15_data *data)
303{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500304 int err;
305
306 err = gpio_direction_input(data->pdata->gpio_data);
307 if (err)
308 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700309 gpio_set_value(data->pdata->gpio_sck, 1);
310 ndelay(SHT15_TSCKH);
311 if (gpio_get_value(data->pdata->gpio_data)) {
312 gpio_set_value(data->pdata->gpio_sck, 0);
313 dev_err(data->dev, "Command not acknowledged\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500314 err = sht15_connection_reset(data);
315 if (err)
316 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700317 return -EIO;
318 }
319 gpio_set_value(data->pdata->gpio_sck, 0);
320 ndelay(SHT15_TSCKL);
321 return 0;
322}
323
324/**
325 * sht15_send_cmd() - Sends a command to the device.
326 * @data: device state
327 * @cmd: command byte to be sent
328 *
329 * On entry, sck is output low, data is output pull high
330 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400331 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700332static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
333{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500334 int err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400335
Vivien Didelot412e29c2013-01-15 13:33:06 -0500336 err = sht15_transmission_start(data);
337 if (err)
338 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700339 sht15_send_byte(data, cmd);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500340 return sht15_wait_for_response(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700341}
Vivien Didelot99a03782011-04-12 15:34:36 -0400342
Jonathan Cameron251eb402009-04-13 14:39:45 -0700343/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400344 * sht15_soft_reset() - send a soft reset command
345 * @data: sht15 specific data.
346 *
347 * As described in section 3.2 of the datasheet.
348 */
349static int sht15_soft_reset(struct sht15_data *data)
350{
351 int ret;
352
353 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
354 if (ret)
355 return ret;
356 msleep(SHT15_TSRST);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400357 /* device resets default hardware status register value */
358 data->val_status = 0;
Vivien Didelot181148a2011-04-12 15:34:37 -0400359
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400360 return ret;
361}
362
363/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400364 * sht15_ack() - send a ack
365 * @data: sht15 specific data.
366 *
367 * Each byte of data is acknowledged by pulling the data line
368 * low for one clock pulse.
369 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500370static int sht15_ack(struct sht15_data *data)
Jerome Oufella82c74652011-04-12 15:34:39 -0400371{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500372 int err;
373
374 err = gpio_direction_output(data->pdata->gpio_data, 0);
375 if (err)
376 return err;
Jerome Oufella82c74652011-04-12 15:34:39 -0400377 ndelay(SHT15_TSU);
378 gpio_set_value(data->pdata->gpio_sck, 1);
379 ndelay(SHT15_TSU);
380 gpio_set_value(data->pdata->gpio_sck, 0);
381 ndelay(SHT15_TSU);
382 gpio_set_value(data->pdata->gpio_data, 1);
383
Vivien Didelot412e29c2013-01-15 13:33:06 -0500384 return gpio_direction_input(data->pdata->gpio_data);
Jerome Oufella82c74652011-04-12 15:34:39 -0400385}
386
387/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400388 * sht15_end_transmission() - notify device of end of transmission
389 * @data: device state.
390 *
391 * This is basically a NAK (single clock pulse, data high).
392 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500393static int sht15_end_transmission(struct sht15_data *data)
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400394{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500395 int err;
396
397 err = gpio_direction_output(data->pdata->gpio_data, 1);
398 if (err)
399 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400400 ndelay(SHT15_TSU);
401 gpio_set_value(data->pdata->gpio_sck, 1);
402 ndelay(SHT15_TSCKH);
403 gpio_set_value(data->pdata->gpio_sck, 0);
404 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500405 return 0;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400406}
407
408/**
409 * sht15_read_byte() - Read a byte back from the device
410 * @data: device state.
411 */
412static u8 sht15_read_byte(struct sht15_data *data)
413{
414 int i;
415 u8 byte = 0;
416
417 for (i = 0; i < 8; ++i) {
418 byte <<= 1;
419 gpio_set_value(data->pdata->gpio_sck, 1);
420 ndelay(SHT15_TSCKH);
421 byte |= !!gpio_get_value(data->pdata->gpio_data);
422 gpio_set_value(data->pdata->gpio_sck, 0);
423 ndelay(SHT15_TSCKL);
424 }
425 return byte;
426}
427
428/**
429 * sht15_send_status() - write the status register byte
430 * @data: sht15 specific data.
431 * @status: the byte to set the status register with.
432 *
433 * As described in figure 14 and table 5 of the datasheet.
434 */
435static int sht15_send_status(struct sht15_data *data, u8 status)
436{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500437 int err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400438
Vivien Didelot412e29c2013-01-15 13:33:06 -0500439 err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
440 if (err)
441 return err;
442 err = gpio_direction_output(data->pdata->gpio_data, 1);
443 if (err)
444 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400445 ndelay(SHT15_TSU);
446 sht15_send_byte(data, status);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500447 err = sht15_wait_for_response(data);
448 if (err)
449 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400450
451 data->val_status = status;
Vivien Didelot181148a2011-04-12 15:34:37 -0400452 return 0;
453}
454
455/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400456 * sht15_update_status() - get updated status register from device if too old
457 * @data: device instance specific data.
458 *
459 * As described in figure 15 and table 5 of the datasheet.
460 */
461static int sht15_update_status(struct sht15_data *data)
462{
463 int ret = 0;
464 u8 status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400465 u8 previous_config;
466 u8 dev_checksum = 0;
467 u8 checksum_vals[2];
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400468 int timeout = HZ;
469
470 mutex_lock(&data->read_lock);
471 if (time_after(jiffies, data->last_status + timeout)
472 || !data->status_valid) {
473 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
474 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500475 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400476 status = sht15_read_byte(data);
477
Jerome Oufella82c74652011-04-12 15:34:39 -0400478 if (data->checksumming) {
479 sht15_ack(data);
480 dev_checksum = sht15_reverse(sht15_read_byte(data));
481 checksum_vals[0] = SHT15_READ_STATUS;
482 checksum_vals[1] = status;
483 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
484 == dev_checksum);
485 }
486
Vivien Didelot412e29c2013-01-15 13:33:06 -0500487 ret = sht15_end_transmission(data);
488 if (ret)
489 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400490
Jerome Oufella82c74652011-04-12 15:34:39 -0400491 /*
492 * Perform checksum validation on the received data.
493 * Specification mentions that in case a checksum verification
494 * fails, a soft reset command must be sent to the device.
495 */
496 if (data->checksumming && !data->checksum_ok) {
497 previous_config = data->val_status & 0x07;
498 ret = sht15_soft_reset(data);
499 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500500 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400501 if (previous_config) {
502 ret = sht15_send_status(data, previous_config);
503 if (ret) {
504 dev_err(data->dev,
505 "CRC validation failed, unable "
506 "to restore device settings\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500507 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400508 }
509 }
510 ret = -EAGAIN;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500511 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400512 }
513
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400514 data->val_status = status;
515 data->status_valid = true;
516 data->last_status = jiffies;
517 }
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400518
Vivien Didelot412e29c2013-01-15 13:33:06 -0500519unlock:
520 mutex_unlock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400521 return ret;
522}
523
524/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400525 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700526 * @data: device instance specific data
527 * @command: command sent to request value
528 * @timeout_msecs: timeout after which comms are assumed
529 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400530 */
531static int sht15_measurement(struct sht15_data *data,
532 int command,
533 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700534{
535 int ret;
Jerome Oufella82c74652011-04-12 15:34:39 -0400536 u8 previous_config;
Vivien Didelot99a03782011-04-12 15:34:36 -0400537
Jonathan Cameron251eb402009-04-13 14:39:45 -0700538 ret = sht15_send_cmd(data, command);
539 if (ret)
540 return ret;
541
Vivien Didelot412e29c2013-01-15 13:33:06 -0500542 ret = gpio_direction_input(data->pdata->gpio_data);
543 if (ret)
544 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700545 atomic_set(&data->interrupt_handled, 0);
546
547 enable_irq(gpio_to_irq(data->pdata->gpio_data));
548 if (gpio_get_value(data->pdata->gpio_data) == 0) {
549 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300550 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700551 if (!atomic_read(&data->interrupt_handled))
552 schedule_work(&data->read_work);
553 }
554 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400555 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700556 msecs_to_jiffies(timeout_msecs));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500557 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
558 data->state = SHT15_READING_NOTHING;
559 return -EIO;
560 } else if (ret == 0) { /* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200561 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500562 ret = sht15_connection_reset(data);
563 if (ret)
564 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700565 return -ETIME;
566 }
Jerome Oufella82c74652011-04-12 15:34:39 -0400567
568 /*
569 * Perform checksum validation on the received data.
570 * Specification mentions that in case a checksum verification fails,
571 * a soft reset command must be sent to the device.
572 */
573 if (data->checksumming && !data->checksum_ok) {
574 previous_config = data->val_status & 0x07;
575 ret = sht15_soft_reset(data);
576 if (ret)
577 return ret;
578 if (previous_config) {
579 ret = sht15_send_status(data, previous_config);
580 if (ret) {
581 dev_err(data->dev,
582 "CRC validation failed, unable "
583 "to restore device settings\n");
584 return ret;
585 }
586 }
587 return -EAGAIN;
588 }
589
Jonathan Cameron251eb402009-04-13 14:39:45 -0700590 return 0;
591}
592
593/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400594 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700595 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400596 */
597static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700598{
599 int ret = 0;
600 int timeout = HZ;
601
602 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400603 if (time_after(jiffies, data->last_measurement + timeout)
604 || !data->measurements_valid) {
605 data->state = SHT15_READING_HUMID;
606 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700607 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500608 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400609 data->state = SHT15_READING_TEMP;
610 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700611 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500612 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400613 data->measurements_valid = true;
614 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700615 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700616
Vivien Didelot412e29c2013-01-15 13:33:06 -0500617unlock:
618 mutex_unlock(&data->read_lock);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700619 return ret;
620}
621
622/**
623 * sht15_calc_temp() - convert the raw reading to a temperature
624 * @data: device state
625 *
626 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400627 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700628static inline int sht15_calc_temp(struct sht15_data *data)
629{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200630 int d1 = temppoints[0].d1;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400631 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700632 int i;
633
Jerome Oufella328a2c22010-04-14 16:14:07 +0200634 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700635 /* Find pointer to interpolate */
Vivien Didelot142c0902013-01-07 14:18:38 -0500636 if (data->supply_uv > temppoints[i - 1].vdd) {
637 d1 = (data->supply_uv - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700638 * (temppoints[i].d1 - temppoints[i - 1].d1)
639 / (temppoints[i].vdd - temppoints[i - 1].vdd)
640 + temppoints[i - 1].d1;
641 break;
642 }
643
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400644 return data->val_temp * d2 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700645}
646
647/**
648 * sht15_calc_humid() - using last temperature convert raw to humid
649 * @data: device state
650 *
651 * This is the temperature compensated version as per section 4.2 of
652 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400653 *
654 * The sensor is assumed to be V3, which is compatible with V4.
655 * Humidity conversion coefficients are shown in table 7 of the datasheet.
656 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700657static inline int sht15_calc_humid(struct sht15_data *data)
658{
Vivien Didelot99a03782011-04-12 15:34:36 -0400659 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700660 int temp = sht15_calc_temp(data);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400661 int c2, c3;
662 int t2;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700663 const int c1 = -4;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400664
665 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
666 c2 = 648000; /* x 10 ^ -6 */
667 c3 = -7200; /* x 10 ^ -7 */
668 t2 = 1280;
669 } else {
670 c2 = 40500; /* x 10 ^ -6 */
671 c3 = -28; /* x 10 ^ -7 */
672 t2 = 80;
673 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700674
Vivien Didelot99a03782011-04-12 15:34:36 -0400675 rh_linear = c1 * 1000
676 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100677 + (data->val_humid * data->val_humid * c3) / 10000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400678 return (temp - 25000) * (10000 + t2 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400679 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700680}
681
Vivien Didelot99a03782011-04-12 15:34:36 -0400682/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400683 * sht15_show_status() - show status information in sysfs
684 * @dev: device.
685 * @attr: device attribute.
686 * @buf: sysfs buffer where information is written to.
687 *
688 * Will be called on read access to temp1_fault, humidity1_fault
689 * and heater_enable sysfs attributes.
690 * Returns number of bytes written into buffer, negative errno on error.
691 */
692static ssize_t sht15_show_status(struct device *dev,
693 struct device_attribute *attr,
694 char *buf)
695{
696 int ret;
697 struct sht15_data *data = dev_get_drvdata(dev);
698 u8 bit = to_sensor_dev_attr(attr)->index;
699
700 ret = sht15_update_status(data);
701
702 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
703}
704
705/**
706 * sht15_store_heater() - change heater state via sysfs
707 * @dev: device.
708 * @attr: device attribute.
709 * @buf: sysfs buffer to read the new heater state from.
710 * @count: length of the data.
711 *
Vivien Didelote9b6e9f2011-07-25 21:46:10 +0200712 * Will be called on write access to heater_enable sysfs attribute.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400713 * Returns number of bytes actually decoded, negative errno on error.
714 */
715static ssize_t sht15_store_heater(struct device *dev,
716 struct device_attribute *attr,
717 const char *buf, size_t count)
718{
719 int ret;
720 struct sht15_data *data = dev_get_drvdata(dev);
721 long value;
722 u8 status;
723
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100724 if (kstrtol(buf, 10, &value))
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400725 return -EINVAL;
726
727 mutex_lock(&data->read_lock);
728 status = data->val_status & 0x07;
729 if (!!value)
730 status |= SHT15_STATUS_HEATER;
731 else
732 status &= ~SHT15_STATUS_HEATER;
733
734 ret = sht15_send_status(data, status);
735 mutex_unlock(&data->read_lock);
736
737 return ret ? ret : count;
738}
739
740/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400741 * sht15_show_temp() - show temperature measurement value in sysfs
742 * @dev: device.
743 * @attr: device attribute.
744 * @buf: sysfs buffer where measurement values are written to.
745 *
746 * Will be called on read access to temp1_input sysfs attribute.
747 * Returns number of bytes written into buffer, negative errno on error.
748 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700749static ssize_t sht15_show_temp(struct device *dev,
750 struct device_attribute *attr,
751 char *buf)
752{
753 int ret;
754 struct sht15_data *data = dev_get_drvdata(dev);
755
756 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400757 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700758
759 return ret ? ret : sprintf(buf, "%d\n",
760 sht15_calc_temp(data));
761}
762
Vivien Didelot99a03782011-04-12 15:34:36 -0400763/**
764 * sht15_show_humidity() - show humidity measurement value in sysfs
765 * @dev: device.
766 * @attr: device attribute.
767 * @buf: sysfs buffer where measurement values are written to.
768 *
769 * Will be called on read access to humidity1_input sysfs attribute.
770 * Returns number of bytes written into buffer, negative errno on error.
771 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700772static ssize_t sht15_show_humidity(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
775{
776 int ret;
777 struct sht15_data *data = dev_get_drvdata(dev);
778
Vivien Didelot99a03782011-04-12 15:34:36 -0400779 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700780
781 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
Vivien Didelot99a03782011-04-12 15:34:36 -0400782}
783
Jonathan Cameron251eb402009-04-13 14:39:45 -0700784static ssize_t show_name(struct device *dev,
785 struct device_attribute *attr,
786 char *buf)
787{
788 struct platform_device *pdev = to_platform_device(dev);
789 return sprintf(buf, "%s\n", pdev->name);
790}
791
Vivien Didelot99a03782011-04-12 15:34:36 -0400792static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
793 sht15_show_temp, NULL, 0);
794static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
795 sht15_show_humidity, NULL, 0);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400796static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
797 SHT15_STATUS_LOW_BATTERY);
798static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
799 SHT15_STATUS_LOW_BATTERY);
800static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
801 sht15_store_heater, SHT15_STATUS_HEATER);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700802static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
803static struct attribute *sht15_attrs[] = {
804 &sensor_dev_attr_temp1_input.dev_attr.attr,
805 &sensor_dev_attr_humidity1_input.dev_attr.attr,
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400806 &sensor_dev_attr_temp1_fault.dev_attr.attr,
807 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
808 &sensor_dev_attr_heater_enable.dev_attr.attr,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700809 &dev_attr_name.attr,
810 NULL,
811};
812
813static const struct attribute_group sht15_attr_group = {
814 .attrs = sht15_attrs,
815};
816
817static irqreturn_t sht15_interrupt_fired(int irq, void *d)
818{
819 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400820
Jonathan Cameron251eb402009-04-13 14:39:45 -0700821 /* First disable the interrupt */
822 disable_irq_nosync(irq);
823 atomic_inc(&data->interrupt_handled);
824 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400825 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700826 schedule_work(&data->read_work);
827 return IRQ_HANDLED;
828}
829
Jonathan Cameron251eb402009-04-13 14:39:45 -0700830static void sht15_bh_read_data(struct work_struct *work_s)
831{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700832 uint16_t val = 0;
Jerome Oufella82c74652011-04-12 15:34:39 -0400833 u8 dev_checksum = 0;
834 u8 checksum_vals[3];
Jonathan Cameron251eb402009-04-13 14:39:45 -0700835 struct sht15_data *data
836 = container_of(work_s, struct sht15_data,
837 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400838
Jonathan Cameron251eb402009-04-13 14:39:45 -0700839 /* Firstly, verify the line is low */
840 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400841 /*
842 * If not, then start the interrupt again - care here as could
843 * have gone low in meantime so verify it hasn't!
844 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700845 atomic_set(&data->interrupt_handled, 0);
846 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Frans Meulenbroeksc9e14982012-01-08 19:34:06 +0100847 /* If still not occurred or another handler was scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700848 if (gpio_get_value(data->pdata->gpio_data)
849 || atomic_read(&data->interrupt_handled))
850 return;
851 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400852
Jonathan Cameron251eb402009-04-13 14:39:45 -0700853 /* Read the data back from the device */
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400854 val = sht15_read_byte(data);
855 val <<= 8;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500856 if (sht15_ack(data))
857 goto wakeup;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400858 val |= sht15_read_byte(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400859
Jerome Oufella82c74652011-04-12 15:34:39 -0400860 if (data->checksumming) {
861 /*
862 * Ask the device for a checksum and read it back.
863 * Note: the device sends the checksum byte reversed.
864 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500865 if (sht15_ack(data))
866 goto wakeup;
Jerome Oufella82c74652011-04-12 15:34:39 -0400867 dev_checksum = sht15_reverse(sht15_read_byte(data));
868 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
869 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
870 checksum_vals[1] = (u8) (val >> 8);
871 checksum_vals[2] = (u8) val;
872 data->checksum_ok
873 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
874 }
875
Jonathan Cameron251eb402009-04-13 14:39:45 -0700876 /* Tell the device we are done */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500877 if (sht15_end_transmission(data))
878 goto wakeup;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700879
Vivien Didelot99a03782011-04-12 15:34:36 -0400880 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700881 case SHT15_READING_TEMP:
882 data->val_temp = val;
883 break;
884 case SHT15_READING_HUMID:
885 data->val_humid = val;
886 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400887 default:
888 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700889 }
890
Vivien Didelot99a03782011-04-12 15:34:36 -0400891 data->state = SHT15_READING_NOTHING;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500892wakeup:
Jonathan Cameron251eb402009-04-13 14:39:45 -0700893 wake_up(&data->wait_queue);
894}
895
896static void sht15_update_voltage(struct work_struct *work_s)
897{
898 struct sht15_data *data
899 = container_of(work_s, struct sht15_data,
900 update_supply_work);
Vivien Didelot142c0902013-01-07 14:18:38 -0500901 data->supply_uv = regulator_get_voltage(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700902}
903
904/**
905 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
906 * @nb: associated notification structure
907 * @event: voltage regulator state change event code
908 * @ignored: function parameter - ignored here
909 *
910 * Note that as the notification code holds the regulator lock, we have
911 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400912 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700913static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400914 unsigned long event,
915 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700916{
917 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
918
919 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
Vivien Didelot142c0902013-01-07 14:18:38 -0500920 data->supply_uv_valid = false;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700921 schedule_work(&data->update_supply_work);
922
923 return NOTIFY_OK;
924}
925
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500926static int sht15_probe(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700927{
Vivien Didelot6edf3c32012-01-26 15:59:00 -0500928 int ret;
Guenter Roeck38fe7562012-06-02 11:20:22 -0700929 struct sht15_data *data;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400930 u8 status = 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700931
Guenter Roeck38fe7562012-06-02 11:20:22 -0700932 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
933 if (!data)
934 return -ENOMEM;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700935
936 INIT_WORK(&data->read_work, sht15_bh_read_data);
937 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
938 platform_set_drvdata(pdev, data);
939 mutex_init(&data->read_lock);
940 data->dev = &pdev->dev;
941 init_waitqueue_head(&data->wait_queue);
942
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900943 if (dev_get_platdata(&pdev->dev) == NULL) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400944 dev_err(&pdev->dev, "no platform data supplied\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700945 return -EINVAL;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700946 }
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900947 data->pdata = dev_get_platdata(&pdev->dev);
Vivien Didelot142c0902013-01-07 14:18:38 -0500948 data->supply_uv = data->pdata->supply_mv * 1000;
Jerome Oufella82c74652011-04-12 15:34:39 -0400949 if (data->pdata->checksum)
950 data->checksumming = true;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400951 if (data->pdata->no_otp_reload)
952 status |= SHT15_STATUS_NO_OTP_RELOAD;
953 if (data->pdata->low_resolution)
954 status |= SHT15_STATUS_LOW_RESOLUTION;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700955
Vivien Didelot99a03782011-04-12 15:34:36 -0400956 /*
957 * If a regulator is available,
958 * query what the supply voltage actually is!
959 */
Mark Brown9e059ba2013-08-18 17:35:52 +0100960 data->reg = devm_regulator_get_optional(data->dev, "vcc");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700961 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +0200962 int voltage;
963
964 voltage = regulator_get_voltage(data->reg);
965 if (voltage)
Vivien Didelot142c0902013-01-07 14:18:38 -0500966 data->supply_uv = voltage;
Jean Delvarec7a78d22010-04-14 16:14:08 +0200967
Mark Brown3e780802013-03-02 15:33:30 +0800968 ret = regulator_enable(data->reg);
969 if (ret != 0) {
970 dev_err(&pdev->dev,
971 "failed to enable regulator: %d\n", ret);
972 return ret;
973 }
974
Vivien Didelot99a03782011-04-12 15:34:36 -0400975 /*
976 * Setup a notifier block to update this if another device
977 * causes the voltage to change
978 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700979 data->nb.notifier_call = &sht15_invalidate_voltage;
980 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -0400981 if (ret) {
982 dev_err(&pdev->dev,
983 "regulator notifier request failed\n");
984 regulator_disable(data->reg);
Guenter Roeck38fe7562012-06-02 11:20:22 -0700985 return ret;
Vivien Didelot181148a2011-04-12 15:34:37 -0400986 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700987 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400988
989 /* Try requesting the GPIOs */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500990 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
991 GPIOF_OUT_INIT_LOW, "SHT15 sck");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700992 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -0500993 dev_err(&pdev->dev, "clock line GPIO request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -0400994 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700995 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400996
Guenter Roeck38fe7562012-06-02 11:20:22 -0700997 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
998 "SHT15 data");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700999 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -05001000 dev_err(&pdev->dev, "data line GPIO request failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001001 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001002 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001003
Guenter Roeck38fe7562012-06-02 11:20:22 -07001004 ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
1005 sht15_interrupt_fired,
1006 IRQF_TRIGGER_FALLING,
1007 "sht15 data",
1008 data);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001009 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -04001010 dev_err(&pdev->dev, "failed to get irq for data line\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001011 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001012 }
1013 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -05001014 ret = sht15_connection_reset(data);
1015 if (ret)
1016 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001017 ret = sht15_soft_reset(data);
1018 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001019 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001020
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001021 /* write status with platform data options */
1022 if (status) {
1023 ret = sht15_send_status(data, status);
1024 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001025 goto err_release_reg;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001026 }
1027
Vivien Didelot181148a2011-04-12 15:34:37 -04001028 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1029 if (ret) {
1030 dev_err(&pdev->dev, "sysfs create failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001031 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001032 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001033
1034 data->hwmon_dev = hwmon_device_register(data->dev);
1035 if (IS_ERR(data->hwmon_dev)) {
1036 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -04001037 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001038 }
Vivien Didelot99a03782011-04-12 15:34:36 -04001039
Jonathan Cameron251eb402009-04-13 14:39:45 -07001040 return 0;
1041
Vivien Didelot181148a2011-04-12 15:34:37 -04001042err_release_sysfs_group:
1043 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Vivien Didelot181148a2011-04-12 15:34:37 -04001044err_release_reg:
1045 if (!IS_ERR(data->reg)) {
1046 regulator_unregister_notifier(data->reg, &data->nb);
1047 regulator_disable(data->reg);
Vivien Didelot181148a2011-04-12 15:34:37 -04001048 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001049 return ret;
1050}
1051
Bill Pemberton281dfd02012-11-19 13:25:51 -05001052static int sht15_remove(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -07001053{
1054 struct sht15_data *data = platform_get_drvdata(pdev);
1055
Vivien Didelot99a03782011-04-12 15:34:36 -04001056 /*
1057 * Make sure any reads from the device are done and
1058 * prevent new ones beginning
1059 */
Jonathan Cameron251eb402009-04-13 14:39:45 -07001060 mutex_lock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001061 if (sht15_soft_reset(data)) {
1062 mutex_unlock(&data->read_lock);
1063 return -EFAULT;
1064 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001065 hwmon_device_unregister(data->hwmon_dev);
1066 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1067 if (!IS_ERR(data->reg)) {
1068 regulator_unregister_notifier(data->reg, &data->nb);
1069 regulator_disable(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001070 }
1071
Jonathan Cameron251eb402009-04-13 14:39:45 -07001072 mutex_unlock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -04001073
Jonathan Cameron251eb402009-04-13 14:39:45 -07001074 return 0;
1075}
1076
Vivien Didelotedec5af2012-08-30 21:46:19 -04001077static struct platform_device_id sht15_device_ids[] = {
1078 { "sht10", sht10 },
1079 { "sht11", sht11 },
1080 { "sht15", sht15 },
1081 { "sht71", sht71 },
1082 { "sht75", sht75 },
1083 { }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001084};
Vivien Didelotedec5af2012-08-30 21:46:19 -04001085MODULE_DEVICE_TABLE(platform, sht15_device_ids);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001086
Vivien Didelotedec5af2012-08-30 21:46:19 -04001087static struct platform_driver sht15_driver = {
1088 .driver = {
1089 .name = "sht15",
1090 .owner = THIS_MODULE,
1091 },
1092 .probe = sht15_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001093 .remove = sht15_remove,
Vivien Didelotedec5af2012-08-30 21:46:19 -04001094 .id_table = sht15_device_ids,
1095};
1096module_platform_driver(sht15_driver);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001097
1098MODULE_LICENSE("GPL");
Vivien Didelotedec5af2012-08-30 21:46:19 -04001099MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");