blob: 1c85d39df171670a4727cb95484d3cd5acbf5721 [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.
142 * @supply_uV: local copy of supply voltage used to allow use of
143 * regulator consumer if available.
144 * @supply_uV_valid: indicates that an updated value has not yet been
145 * obtained from the regulator and so any calculations
146 * based upon it will be invalid.
147 * @update_supply_work: work struct that is used to update the supply_uV.
148 * @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;
169 int supply_uV;
Vivien Didelot99a03782011-04-12 15:34:36 -0400170 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 */
215static void sht15_connection_reset(struct sht15_data *data)
216{
217 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400218
Jonathan Cameron251eb402009-04-13 14:39:45 -0700219 gpio_direction_output(data->pdata->gpio_data, 1);
220 ndelay(SHT15_TSCKL);
221 gpio_set_value(data->pdata->gpio_sck, 0);
222 ndelay(SHT15_TSCKL);
223 for (i = 0; i < 9; ++i) {
224 gpio_set_value(data->pdata->gpio_sck, 1);
225 ndelay(SHT15_TSCKH);
226 gpio_set_value(data->pdata->gpio_sck, 0);
227 ndelay(SHT15_TSCKL);
228 }
229}
Vivien Didelot99a03782011-04-12 15:34:36 -0400230
Jonathan Cameron251eb402009-04-13 14:39:45 -0700231/**
232 * sht15_send_bit() - send an individual bit to the device
233 * @data: device state data
234 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400235 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700236static inline void sht15_send_bit(struct sht15_data *data, int val)
237{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700238 gpio_set_value(data->pdata->gpio_data, val);
239 ndelay(SHT15_TSU);
240 gpio_set_value(data->pdata->gpio_sck, 1);
241 ndelay(SHT15_TSCKH);
242 gpio_set_value(data->pdata->gpio_sck, 0);
243 ndelay(SHT15_TSCKL); /* clock low time */
244}
245
246/**
247 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700248 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400249 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700250 * Timings for this are not documented on the data sheet, so very
251 * conservative ones used in implementation. This implements
252 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400253 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700254static void sht15_transmission_start(struct sht15_data *data)
255{
256 /* ensure data is high and output */
257 gpio_direction_output(data->pdata->gpio_data, 1);
258 ndelay(SHT15_TSU);
259 gpio_set_value(data->pdata->gpio_sck, 0);
260 ndelay(SHT15_TSCKL);
261 gpio_set_value(data->pdata->gpio_sck, 1);
262 ndelay(SHT15_TSCKH);
263 gpio_set_value(data->pdata->gpio_data, 0);
264 ndelay(SHT15_TSU);
265 gpio_set_value(data->pdata->gpio_sck, 0);
266 ndelay(SHT15_TSCKL);
267 gpio_set_value(data->pdata->gpio_sck, 1);
268 ndelay(SHT15_TSCKH);
269 gpio_set_value(data->pdata->gpio_data, 1);
270 ndelay(SHT15_TSU);
271 gpio_set_value(data->pdata->gpio_sck, 0);
272 ndelay(SHT15_TSCKL);
273}
Vivien Didelot99a03782011-04-12 15:34:36 -0400274
Jonathan Cameron251eb402009-04-13 14:39:45 -0700275/**
276 * sht15_send_byte() - send a single byte to the device
277 * @data: device state
278 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400279 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700280static void sht15_send_byte(struct sht15_data *data, u8 byte)
281{
282 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400283
Jonathan Cameron251eb402009-04-13 14:39:45 -0700284 for (i = 0; i < 8; i++) {
285 sht15_send_bit(data, !!(byte & 0x80));
286 byte <<= 1;
287 }
288}
Vivien Didelot99a03782011-04-12 15:34:36 -0400289
Jonathan Cameron251eb402009-04-13 14:39:45 -0700290/**
291 * sht15_wait_for_response() - checks for ack from device
292 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400293 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700294static int sht15_wait_for_response(struct sht15_data *data)
295{
296 gpio_direction_input(data->pdata->gpio_data);
297 gpio_set_value(data->pdata->gpio_sck, 1);
298 ndelay(SHT15_TSCKH);
299 if (gpio_get_value(data->pdata->gpio_data)) {
300 gpio_set_value(data->pdata->gpio_sck, 0);
301 dev_err(data->dev, "Command not acknowledged\n");
302 sht15_connection_reset(data);
303 return -EIO;
304 }
305 gpio_set_value(data->pdata->gpio_sck, 0);
306 ndelay(SHT15_TSCKL);
307 return 0;
308}
309
310/**
311 * sht15_send_cmd() - Sends a command to the device.
312 * @data: device state
313 * @cmd: command byte to be sent
314 *
315 * On entry, sck is output low, data is output pull high
316 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400317 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700318static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
319{
320 int ret = 0;
Vivien Didelot99a03782011-04-12 15:34:36 -0400321
Jonathan Cameron251eb402009-04-13 14:39:45 -0700322 sht15_transmission_start(data);
323 sht15_send_byte(data, cmd);
324 ret = sht15_wait_for_response(data);
325 return ret;
326}
Vivien Didelot99a03782011-04-12 15:34:36 -0400327
Jonathan Cameron251eb402009-04-13 14:39:45 -0700328/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400329 * sht15_soft_reset() - send a soft reset command
330 * @data: sht15 specific data.
331 *
332 * As described in section 3.2 of the datasheet.
333 */
334static int sht15_soft_reset(struct sht15_data *data)
335{
336 int ret;
337
338 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
339 if (ret)
340 return ret;
341 msleep(SHT15_TSRST);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400342 /* device resets default hardware status register value */
343 data->val_status = 0;
Vivien Didelot181148a2011-04-12 15:34:37 -0400344
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400345 return ret;
346}
347
348/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400349 * sht15_ack() - send a ack
350 * @data: sht15 specific data.
351 *
352 * Each byte of data is acknowledged by pulling the data line
353 * low for one clock pulse.
354 */
355static void sht15_ack(struct sht15_data *data)
356{
357 gpio_direction_output(data->pdata->gpio_data, 0);
358 ndelay(SHT15_TSU);
359 gpio_set_value(data->pdata->gpio_sck, 1);
360 ndelay(SHT15_TSU);
361 gpio_set_value(data->pdata->gpio_sck, 0);
362 ndelay(SHT15_TSU);
363 gpio_set_value(data->pdata->gpio_data, 1);
364
365 gpio_direction_input(data->pdata->gpio_data);
366}
367
368/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400369 * sht15_end_transmission() - notify device of end of transmission
370 * @data: device state.
371 *
372 * This is basically a NAK (single clock pulse, data high).
373 */
374static void sht15_end_transmission(struct sht15_data *data)
375{
376 gpio_direction_output(data->pdata->gpio_data, 1);
377 ndelay(SHT15_TSU);
378 gpio_set_value(data->pdata->gpio_sck, 1);
379 ndelay(SHT15_TSCKH);
380 gpio_set_value(data->pdata->gpio_sck, 0);
381 ndelay(SHT15_TSCKL);
382}
383
384/**
385 * sht15_read_byte() - Read a byte back from the device
386 * @data: device state.
387 */
388static u8 sht15_read_byte(struct sht15_data *data)
389{
390 int i;
391 u8 byte = 0;
392
393 for (i = 0; i < 8; ++i) {
394 byte <<= 1;
395 gpio_set_value(data->pdata->gpio_sck, 1);
396 ndelay(SHT15_TSCKH);
397 byte |= !!gpio_get_value(data->pdata->gpio_data);
398 gpio_set_value(data->pdata->gpio_sck, 0);
399 ndelay(SHT15_TSCKL);
400 }
401 return byte;
402}
403
404/**
405 * sht15_send_status() - write the status register byte
406 * @data: sht15 specific data.
407 * @status: the byte to set the status register with.
408 *
409 * As described in figure 14 and table 5 of the datasheet.
410 */
411static int sht15_send_status(struct sht15_data *data, u8 status)
412{
413 int ret;
414
415 ret = sht15_send_cmd(data, SHT15_WRITE_STATUS);
416 if (ret)
417 return ret;
418 gpio_direction_output(data->pdata->gpio_data, 1);
419 ndelay(SHT15_TSU);
420 sht15_send_byte(data, status);
421 ret = sht15_wait_for_response(data);
422 if (ret)
423 return ret;
424
425 data->val_status = status;
Vivien Didelot181148a2011-04-12 15:34:37 -0400426 return 0;
427}
428
429/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400430 * sht15_update_status() - get updated status register from device if too old
431 * @data: device instance specific data.
432 *
433 * As described in figure 15 and table 5 of the datasheet.
434 */
435static int sht15_update_status(struct sht15_data *data)
436{
437 int ret = 0;
438 u8 status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400439 u8 previous_config;
440 u8 dev_checksum = 0;
441 u8 checksum_vals[2];
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400442 int timeout = HZ;
443
444 mutex_lock(&data->read_lock);
445 if (time_after(jiffies, data->last_status + timeout)
446 || !data->status_valid) {
447 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
448 if (ret)
449 goto error_ret;
450 status = sht15_read_byte(data);
451
Jerome Oufella82c74652011-04-12 15:34:39 -0400452 if (data->checksumming) {
453 sht15_ack(data);
454 dev_checksum = sht15_reverse(sht15_read_byte(data));
455 checksum_vals[0] = SHT15_READ_STATUS;
456 checksum_vals[1] = status;
457 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
458 == dev_checksum);
459 }
460
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400461 sht15_end_transmission(data);
462
Jerome Oufella82c74652011-04-12 15:34:39 -0400463 /*
464 * Perform checksum validation on the received data.
465 * Specification mentions that in case a checksum verification
466 * fails, a soft reset command must be sent to the device.
467 */
468 if (data->checksumming && !data->checksum_ok) {
469 previous_config = data->val_status & 0x07;
470 ret = sht15_soft_reset(data);
471 if (ret)
472 goto error_ret;
473 if (previous_config) {
474 ret = sht15_send_status(data, previous_config);
475 if (ret) {
476 dev_err(data->dev,
477 "CRC validation failed, unable "
478 "to restore device settings\n");
479 goto error_ret;
480 }
481 }
482 ret = -EAGAIN;
483 goto error_ret;
484 }
485
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400486 data->val_status = status;
487 data->status_valid = true;
488 data->last_status = jiffies;
489 }
490error_ret:
491 mutex_unlock(&data->read_lock);
492
493 return ret;
494}
495
496/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400497 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700498 * @data: device instance specific data
499 * @command: command sent to request value
500 * @timeout_msecs: timeout after which comms are assumed
501 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400502 */
503static int sht15_measurement(struct sht15_data *data,
504 int command,
505 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700506{
507 int ret;
Jerome Oufella82c74652011-04-12 15:34:39 -0400508 u8 previous_config;
Vivien Didelot99a03782011-04-12 15:34:36 -0400509
Jonathan Cameron251eb402009-04-13 14:39:45 -0700510 ret = sht15_send_cmd(data, command);
511 if (ret)
512 return ret;
513
514 gpio_direction_input(data->pdata->gpio_data);
515 atomic_set(&data->interrupt_handled, 0);
516
517 enable_irq(gpio_to_irq(data->pdata->gpio_data));
518 if (gpio_get_value(data->pdata->gpio_data) == 0) {
519 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300520 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700521 if (!atomic_read(&data->interrupt_handled))
522 schedule_work(&data->read_work);
523 }
524 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400525 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700526 msecs_to_jiffies(timeout_msecs));
527 if (ret == 0) {/* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200528 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Jonathan Cameron251eb402009-04-13 14:39:45 -0700529 sht15_connection_reset(data);
530 return -ETIME;
531 }
Jerome Oufella82c74652011-04-12 15:34:39 -0400532
533 /*
534 * Perform checksum validation on the received data.
535 * Specification mentions that in case a checksum verification fails,
536 * a soft reset command must be sent to the device.
537 */
538 if (data->checksumming && !data->checksum_ok) {
539 previous_config = data->val_status & 0x07;
540 ret = sht15_soft_reset(data);
541 if (ret)
542 return ret;
543 if (previous_config) {
544 ret = sht15_send_status(data, previous_config);
545 if (ret) {
546 dev_err(data->dev,
547 "CRC validation failed, unable "
548 "to restore device settings\n");
549 return ret;
550 }
551 }
552 return -EAGAIN;
553 }
554
Jonathan Cameron251eb402009-04-13 14:39:45 -0700555 return 0;
556}
557
558/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400559 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700560 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400561 */
562static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700563{
564 int ret = 0;
565 int timeout = HZ;
566
567 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400568 if (time_after(jiffies, data->last_measurement + timeout)
569 || !data->measurements_valid) {
570 data->state = SHT15_READING_HUMID;
571 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700572 if (ret)
573 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400574 data->state = SHT15_READING_TEMP;
575 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700576 if (ret)
577 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400578 data->measurements_valid = true;
579 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700580 }
581error_ret:
582 mutex_unlock(&data->read_lock);
583
584 return ret;
585}
586
587/**
588 * sht15_calc_temp() - convert the raw reading to a temperature
589 * @data: device state
590 *
591 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400592 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700593static inline int sht15_calc_temp(struct sht15_data *data)
594{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200595 int d1 = temppoints[0].d1;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400596 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700597 int i;
598
Jerome Oufella328a2c22010-04-14 16:14:07 +0200599 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700600 /* Find pointer to interpolate */
601 if (data->supply_uV > temppoints[i - 1].vdd) {
Jerome Oufella328a2c22010-04-14 16:14:07 +0200602 d1 = (data->supply_uV - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700603 * (temppoints[i].d1 - temppoints[i - 1].d1)
604 / (temppoints[i].vdd - temppoints[i - 1].vdd)
605 + temppoints[i - 1].d1;
606 break;
607 }
608
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400609 return data->val_temp * d2 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700610}
611
612/**
613 * sht15_calc_humid() - using last temperature convert raw to humid
614 * @data: device state
615 *
616 * This is the temperature compensated version as per section 4.2 of
617 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400618 *
619 * The sensor is assumed to be V3, which is compatible with V4.
620 * Humidity conversion coefficients are shown in table 7 of the datasheet.
621 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700622static inline int sht15_calc_humid(struct sht15_data *data)
623{
Vivien Didelot99a03782011-04-12 15:34:36 -0400624 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700625 int temp = sht15_calc_temp(data);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400626 int c2, c3;
627 int t2;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700628 const int c1 = -4;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400629
630 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
631 c2 = 648000; /* x 10 ^ -6 */
632 c3 = -7200; /* x 10 ^ -7 */
633 t2 = 1280;
634 } else {
635 c2 = 40500; /* x 10 ^ -6 */
636 c3 = -28; /* x 10 ^ -7 */
637 t2 = 80;
638 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700639
Vivien Didelot99a03782011-04-12 15:34:36 -0400640 rh_linear = c1 * 1000
641 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100642 + (data->val_humid * data->val_humid * c3) / 10000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400643 return (temp - 25000) * (10000 + t2 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400644 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700645}
646
Vivien Didelot99a03782011-04-12 15:34:36 -0400647/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400648 * sht15_show_status() - show status information in sysfs
649 * @dev: device.
650 * @attr: device attribute.
651 * @buf: sysfs buffer where information is written to.
652 *
653 * Will be called on read access to temp1_fault, humidity1_fault
654 * and heater_enable sysfs attributes.
655 * Returns number of bytes written into buffer, negative errno on error.
656 */
657static ssize_t sht15_show_status(struct device *dev,
658 struct device_attribute *attr,
659 char *buf)
660{
661 int ret;
662 struct sht15_data *data = dev_get_drvdata(dev);
663 u8 bit = to_sensor_dev_attr(attr)->index;
664
665 ret = sht15_update_status(data);
666
667 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
668}
669
670/**
671 * sht15_store_heater() - change heater state via sysfs
672 * @dev: device.
673 * @attr: device attribute.
674 * @buf: sysfs buffer to read the new heater state from.
675 * @count: length of the data.
676 *
Vivien Didelote9b6e9f2011-07-25 21:46:10 +0200677 * Will be called on write access to heater_enable sysfs attribute.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400678 * Returns number of bytes actually decoded, negative errno on error.
679 */
680static ssize_t sht15_store_heater(struct device *dev,
681 struct device_attribute *attr,
682 const char *buf, size_t count)
683{
684 int ret;
685 struct sht15_data *data = dev_get_drvdata(dev);
686 long value;
687 u8 status;
688
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100689 if (kstrtol(buf, 10, &value))
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400690 return -EINVAL;
691
692 mutex_lock(&data->read_lock);
693 status = data->val_status & 0x07;
694 if (!!value)
695 status |= SHT15_STATUS_HEATER;
696 else
697 status &= ~SHT15_STATUS_HEATER;
698
699 ret = sht15_send_status(data, status);
700 mutex_unlock(&data->read_lock);
701
702 return ret ? ret : count;
703}
704
705/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400706 * sht15_show_temp() - show temperature measurement value in sysfs
707 * @dev: device.
708 * @attr: device attribute.
709 * @buf: sysfs buffer where measurement values are written to.
710 *
711 * Will be called on read access to temp1_input sysfs attribute.
712 * Returns number of bytes written into buffer, negative errno on error.
713 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700714static ssize_t sht15_show_temp(struct device *dev,
715 struct device_attribute *attr,
716 char *buf)
717{
718 int ret;
719 struct sht15_data *data = dev_get_drvdata(dev);
720
721 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400722 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700723
724 return ret ? ret : sprintf(buf, "%d\n",
725 sht15_calc_temp(data));
726}
727
Vivien Didelot99a03782011-04-12 15:34:36 -0400728/**
729 * sht15_show_humidity() - show humidity measurement value in sysfs
730 * @dev: device.
731 * @attr: device attribute.
732 * @buf: sysfs buffer where measurement values are written to.
733 *
734 * Will be called on read access to humidity1_input sysfs attribute.
735 * Returns number of bytes written into buffer, negative errno on error.
736 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700737static ssize_t sht15_show_humidity(struct device *dev,
738 struct device_attribute *attr,
739 char *buf)
740{
741 int ret;
742 struct sht15_data *data = dev_get_drvdata(dev);
743
Vivien Didelot99a03782011-04-12 15:34:36 -0400744 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700745
746 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
Vivien Didelot99a03782011-04-12 15:34:36 -0400747}
748
Jonathan Cameron251eb402009-04-13 14:39:45 -0700749static ssize_t show_name(struct device *dev,
750 struct device_attribute *attr,
751 char *buf)
752{
753 struct platform_device *pdev = to_platform_device(dev);
754 return sprintf(buf, "%s\n", pdev->name);
755}
756
Vivien Didelot99a03782011-04-12 15:34:36 -0400757static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
758 sht15_show_temp, NULL, 0);
759static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
760 sht15_show_humidity, NULL, 0);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400761static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
762 SHT15_STATUS_LOW_BATTERY);
763static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
764 SHT15_STATUS_LOW_BATTERY);
765static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
766 sht15_store_heater, SHT15_STATUS_HEATER);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700767static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
768static struct attribute *sht15_attrs[] = {
769 &sensor_dev_attr_temp1_input.dev_attr.attr,
770 &sensor_dev_attr_humidity1_input.dev_attr.attr,
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400771 &sensor_dev_attr_temp1_fault.dev_attr.attr,
772 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
773 &sensor_dev_attr_heater_enable.dev_attr.attr,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700774 &dev_attr_name.attr,
775 NULL,
776};
777
778static const struct attribute_group sht15_attr_group = {
779 .attrs = sht15_attrs,
780};
781
782static irqreturn_t sht15_interrupt_fired(int irq, void *d)
783{
784 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400785
Jonathan Cameron251eb402009-04-13 14:39:45 -0700786 /* First disable the interrupt */
787 disable_irq_nosync(irq);
788 atomic_inc(&data->interrupt_handled);
789 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400790 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700791 schedule_work(&data->read_work);
792 return IRQ_HANDLED;
793}
794
Jonathan Cameron251eb402009-04-13 14:39:45 -0700795static void sht15_bh_read_data(struct work_struct *work_s)
796{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700797 uint16_t val = 0;
Jerome Oufella82c74652011-04-12 15:34:39 -0400798 u8 dev_checksum = 0;
799 u8 checksum_vals[3];
Jonathan Cameron251eb402009-04-13 14:39:45 -0700800 struct sht15_data *data
801 = container_of(work_s, struct sht15_data,
802 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400803
Jonathan Cameron251eb402009-04-13 14:39:45 -0700804 /* Firstly, verify the line is low */
805 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400806 /*
807 * If not, then start the interrupt again - care here as could
808 * have gone low in meantime so verify it hasn't!
809 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700810 atomic_set(&data->interrupt_handled, 0);
811 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Frans Meulenbroeksc9e14982012-01-08 19:34:06 +0100812 /* If still not occurred or another handler was scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700813 if (gpio_get_value(data->pdata->gpio_data)
814 || atomic_read(&data->interrupt_handled))
815 return;
816 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400817
Jonathan Cameron251eb402009-04-13 14:39:45 -0700818 /* Read the data back from the device */
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400819 val = sht15_read_byte(data);
820 val <<= 8;
821 sht15_ack(data);
822 val |= sht15_read_byte(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400823
Jerome Oufella82c74652011-04-12 15:34:39 -0400824 if (data->checksumming) {
825 /*
826 * Ask the device for a checksum and read it back.
827 * Note: the device sends the checksum byte reversed.
828 */
829 sht15_ack(data);
830 dev_checksum = sht15_reverse(sht15_read_byte(data));
831 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
832 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
833 checksum_vals[1] = (u8) (val >> 8);
834 checksum_vals[2] = (u8) val;
835 data->checksum_ok
836 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
837 }
838
Jonathan Cameron251eb402009-04-13 14:39:45 -0700839 /* Tell the device we are done */
840 sht15_end_transmission(data);
841
Vivien Didelot99a03782011-04-12 15:34:36 -0400842 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700843 case SHT15_READING_TEMP:
844 data->val_temp = val;
845 break;
846 case SHT15_READING_HUMID:
847 data->val_humid = val;
848 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400849 default:
850 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700851 }
852
Vivien Didelot99a03782011-04-12 15:34:36 -0400853 data->state = SHT15_READING_NOTHING;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700854 wake_up(&data->wait_queue);
855}
856
857static void sht15_update_voltage(struct work_struct *work_s)
858{
859 struct sht15_data *data
860 = container_of(work_s, struct sht15_data,
861 update_supply_work);
862 data->supply_uV = regulator_get_voltage(data->reg);
863}
864
865/**
866 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
867 * @nb: associated notification structure
868 * @event: voltage regulator state change event code
869 * @ignored: function parameter - ignored here
870 *
871 * Note that as the notification code holds the regulator lock, we have
872 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400873 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700874static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400875 unsigned long event,
876 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700877{
878 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
879
880 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
881 data->supply_uV_valid = false;
882 schedule_work(&data->update_supply_work);
883
884 return NOTIFY_OK;
885}
886
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500887static int sht15_probe(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700888{
Vivien Didelot6edf3c32012-01-26 15:59:00 -0500889 int ret;
Guenter Roeck38fe7562012-06-02 11:20:22 -0700890 struct sht15_data *data;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400891 u8 status = 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700892
Guenter Roeck38fe7562012-06-02 11:20:22 -0700893 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
894 if (!data)
895 return -ENOMEM;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700896
897 INIT_WORK(&data->read_work, sht15_bh_read_data);
898 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
899 platform_set_drvdata(pdev, data);
900 mutex_init(&data->read_lock);
901 data->dev = &pdev->dev;
902 init_waitqueue_head(&data->wait_queue);
903
904 if (pdev->dev.platform_data == NULL) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400905 dev_err(&pdev->dev, "no platform data supplied\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700906 return -EINVAL;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700907 }
908 data->pdata = pdev->dev.platform_data;
Vivien Didelot99a03782011-04-12 15:34:36 -0400909 data->supply_uV = data->pdata->supply_mv * 1000;
Jerome Oufella82c74652011-04-12 15:34:39 -0400910 if (data->pdata->checksum)
911 data->checksumming = true;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400912 if (data->pdata->no_otp_reload)
913 status |= SHT15_STATUS_NO_OTP_RELOAD;
914 if (data->pdata->low_resolution)
915 status |= SHT15_STATUS_LOW_RESOLUTION;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700916
Vivien Didelot99a03782011-04-12 15:34:36 -0400917 /*
918 * If a regulator is available,
919 * query what the supply voltage actually is!
920 */
Guenter Roeck38fe7562012-06-02 11:20:22 -0700921 data->reg = devm_regulator_get(data->dev, "vcc");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700922 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +0200923 int voltage;
924
925 voltage = regulator_get_voltage(data->reg);
926 if (voltage)
927 data->supply_uV = voltage;
928
Jonathan Cameron251eb402009-04-13 14:39:45 -0700929 regulator_enable(data->reg);
Vivien Didelot99a03782011-04-12 15:34:36 -0400930 /*
931 * Setup a notifier block to update this if another device
932 * causes the voltage to change
933 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700934 data->nb.notifier_call = &sht15_invalidate_voltage;
935 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -0400936 if (ret) {
937 dev_err(&pdev->dev,
938 "regulator notifier request failed\n");
939 regulator_disable(data->reg);
Guenter Roeck38fe7562012-06-02 11:20:22 -0700940 return ret;
Vivien Didelot181148a2011-04-12 15:34:37 -0400941 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700942 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400943
944 /* Try requesting the GPIOs */
Guenter Roeck38fe7562012-06-02 11:20:22 -0700945 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_sck, "SHT15 sck");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700946 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400947 dev_err(&pdev->dev, "gpio request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -0400948 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700949 }
950 gpio_direction_output(data->pdata->gpio_sck, 0);
Vivien Didelot99a03782011-04-12 15:34:36 -0400951
Guenter Roeck38fe7562012-06-02 11:20:22 -0700952 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
953 "SHT15 data");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700954 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400955 dev_err(&pdev->dev, "gpio request failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700956 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700957 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700958
Guenter Roeck38fe7562012-06-02 11:20:22 -0700959 ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
960 sht15_interrupt_fired,
961 IRQF_TRIGGER_FALLING,
962 "sht15 data",
963 data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700964 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400965 dev_err(&pdev->dev, "failed to get irq for data line\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700966 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700967 }
968 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
969 sht15_connection_reset(data);
Vivien Didelot181148a2011-04-12 15:34:37 -0400970 ret = sht15_soft_reset(data);
971 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -0700972 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -0400973
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400974 /* write status with platform data options */
975 if (status) {
976 ret = sht15_send_status(data, status);
977 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -0700978 goto err_release_reg;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400979 }
980
Vivien Didelot181148a2011-04-12 15:34:37 -0400981 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
982 if (ret) {
983 dev_err(&pdev->dev, "sysfs create failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700984 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -0400985 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700986
987 data->hwmon_dev = hwmon_device_register(data->dev);
988 if (IS_ERR(data->hwmon_dev)) {
989 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -0400990 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700991 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400992
Jonathan Cameron251eb402009-04-13 14:39:45 -0700993 return 0;
994
Vivien Didelot181148a2011-04-12 15:34:37 -0400995err_release_sysfs_group:
996 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Vivien Didelot181148a2011-04-12 15:34:37 -0400997err_release_reg:
998 if (!IS_ERR(data->reg)) {
999 regulator_unregister_notifier(data->reg, &data->nb);
1000 regulator_disable(data->reg);
Vivien Didelot181148a2011-04-12 15:34:37 -04001001 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001002 return ret;
1003}
1004
Bill Pemberton281dfd02012-11-19 13:25:51 -05001005static int sht15_remove(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -07001006{
1007 struct sht15_data *data = platform_get_drvdata(pdev);
1008
Vivien Didelot99a03782011-04-12 15:34:36 -04001009 /*
1010 * Make sure any reads from the device are done and
1011 * prevent new ones beginning
1012 */
Jonathan Cameron251eb402009-04-13 14:39:45 -07001013 mutex_lock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001014 if (sht15_soft_reset(data)) {
1015 mutex_unlock(&data->read_lock);
1016 return -EFAULT;
1017 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001018 hwmon_device_unregister(data->hwmon_dev);
1019 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1020 if (!IS_ERR(data->reg)) {
1021 regulator_unregister_notifier(data->reg, &data->nb);
1022 regulator_disable(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001023 }
1024
Jonathan Cameron251eb402009-04-13 14:39:45 -07001025 mutex_unlock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -04001026
Jonathan Cameron251eb402009-04-13 14:39:45 -07001027 return 0;
1028}
1029
Vivien Didelotedec5af2012-08-30 21:46:19 -04001030static struct platform_device_id sht15_device_ids[] = {
1031 { "sht10", sht10 },
1032 { "sht11", sht11 },
1033 { "sht15", sht15 },
1034 { "sht71", sht71 },
1035 { "sht75", sht75 },
1036 { }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001037};
Vivien Didelotedec5af2012-08-30 21:46:19 -04001038MODULE_DEVICE_TABLE(platform, sht15_device_ids);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001039
Vivien Didelotedec5af2012-08-30 21:46:19 -04001040static struct platform_driver sht15_driver = {
1041 .driver = {
1042 .name = "sht15",
1043 .owner = THIS_MODULE,
1044 },
1045 .probe = sht15_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001046 .remove = sht15_remove,
Vivien Didelotedec5af2012-08-30 21:46:19 -04001047 .id_table = sht15_device_ids,
1048};
1049module_platform_driver(sht15_driver);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001050
1051MODULE_LICENSE("GPL");
Vivien Didelotedec5af2012-08-30 21:46:19 -04001052MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");