hwmon: remove use of __devexit_p
[linux-2.6-block.git] / drivers / hwmon / sht15.c
CommitLineData
251eb40f
JC
1/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
edec5af7 4 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
82c7465b 5 * Jerome Oufella <jerome.oufella@savoirfairelinux.com>
cc15c7eb
VD
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 *
251eb40f
JC
8 * 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 *
99a0378d 16 * For further information, see the Documentation/hwmon/sht15 file.
251eb40f
JC
17 */
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>
f9b693eb 27#include <linux/platform_data/sht15.h>
251eb40f 28#include <linux/platform_device.h>
d43c36dc 29#include <linux/sched.h>
251eb40f
JC
30#include <linux/delay.h>
31#include <linux/jiffies.h>
32#include <linux/err.h>
251eb40f 33#include <linux/regulator/consumer.h>
5a0e3ad6 34#include <linux/slab.h>
60063497 35#include <linux/atomic.h>
251eb40f 36
99a0378d
VD
37/* Commands */
38#define SHT15_MEASURE_TEMP 0x03
39#define SHT15_MEASURE_RH 0x05
cc15c7eb
VD
40#define SHT15_WRITE_STATUS 0x06
41#define SHT15_READ_STATUS 0x07
181148ae 42#define SHT15_SOFT_RESET 0x1E
251eb40f 43
99a0378d
VD
44/* 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 */
181148ae 48#define SHT15_TSRST 11 /* (msecs) soft reset time */
251eb40f 49
cc15c7eb
VD
50/* 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
edec5af7
VD
56/* List of supported chips */
57enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
58
99a0378d
VD
59/* Actions the driver may be doing */
60enum sht15_state {
61 SHT15_READING_NOTHING,
62 SHT15_READING_TEMP,
63 SHT15_READING_HUMID
64};
251eb40f
JC
65
66/**
25985edc 67 * struct sht15_temppair - elements of voltage dependent temp calc
251eb40f
JC
68 * @vdd: supply voltage in microvolts
69 * @d1: see data sheet
70 */
71struct sht15_temppair {
72 int vdd; /* microvolts */
73 int d1;
74};
75
99a0378d 76/* Table 9 from datasheet - relates temperature calculation to supply voltage */
251eb40f
JC
77static const struct sht15_temppair temppoints[] = {
78 { 2500000, -39400 },
79 { 3000000, -39600 },
80 { 3500000, -39700 },
81 { 4000000, -39800 },
82 { 5000000, -40100 },
83};
84
82c7465b
JO
85/* 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
251eb40f
JC
121/**
122 * struct sht15_data - device instance specific data
99a0378d
VD
123 * @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.
cc15c7eb 128 * @val_status: last status register value read from device.
82c7465b
JO
129 * @checksum_ok: last value read from the device passed CRC validation.
130 * @checksumming: flag used to enable the data validation with CRC.
99a0378d
VD
131 * @state: state identifying the action the driver is doing.
132 * @measurements_valid: are the current stored measures valid (start condition).
cc15c7eb 133 * @status_valid: is the current stored status valid (start condition).
99a0378d 134 * @last_measurement: time of last measure.
cc15c7eb 135 * @last_status: time of last status reading.
99a0378d
VD
136 * @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.
251eb40f
JC
149 */
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;
cc15c7eb 156 u8 val_status;
82c7465b
JO
157 bool checksum_ok;
158 bool checksumming;
99a0378d
VD
159 enum sht15_state state;
160 bool measurements_valid;
cc15c7eb 161 bool status_valid;
99a0378d 162 unsigned long last_measurement;
cc15c7eb 163 unsigned long last_status;
251eb40f
JC
164 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;
99a0378d 170 bool supply_uV_valid;
251eb40f
JC
171 struct work_struct update_supply_work;
172 atomic_t interrupt_handled;
173};
174
82c7465b
JO
175/**
176 * 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
251eb40f
JC
209/**
210 * 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;
99a0378d 218
251eb40f
JC
219 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}
99a0378d 230
251eb40f
JC
231/**
232 * sht15_send_bit() - send an individual bit to the device
233 * @data: device state data
234 * @val: value of bit to be sent
99a0378d 235 */
251eb40f
JC
236static inline void sht15_send_bit(struct sht15_data *data, int val)
237{
251eb40f
JC
238 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
251eb40f 248 * @data: device state data
99a0378d 249 *
251eb40f
JC
250 * 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.
99a0378d 253 */
251eb40f
JC
254static 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}
99a0378d 274
251eb40f
JC
275/**
276 * sht15_send_byte() - send a single byte to the device
277 * @data: device state
278 * @byte: value to be sent
99a0378d 279 */
251eb40f
JC
280static void sht15_send_byte(struct sht15_data *data, u8 byte)
281{
282 int i;
99a0378d 283
251eb40f
JC
284 for (i = 0; i < 8; i++) {
285 sht15_send_bit(data, !!(byte & 0x80));
286 byte <<= 1;
287 }
288}
99a0378d 289
251eb40f
JC
290/**
291 * sht15_wait_for_response() - checks for ack from device
292 * @data: device state
99a0378d 293 */
251eb40f
JC
294static 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.
99a0378d 317 */
251eb40f
JC
318static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
319{
320 int ret = 0;
99a0378d 321
251eb40f
JC
322 sht15_transmission_start(data);
323 sht15_send_byte(data, cmd);
324 ret = sht15_wait_for_response(data);
325 return ret;
326}
99a0378d 327
181148ae
VD
328/**
329 * 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);
cc15c7eb
VD
342 /* device resets default hardware status register value */
343 data->val_status = 0;
344
345 return ret;
346}
347
82c7465b
JO
348/**
349 * 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
cc15c7eb
VD
368/**
369 * 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;
181148ae 424
cc15c7eb 425 data->val_status = status;
181148ae
VD
426 return 0;
427}
428
cc15c7eb
VD
429/**
430 * 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;
82c7465b
JO
439 u8 previous_config;
440 u8 dev_checksum = 0;
441 u8 checksum_vals[2];
cc15c7eb
VD
442 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
82c7465b
JO
452 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
cc15c7eb
VD
461 sht15_end_transmission(data);
462
82c7465b
JO
463 /*
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
cc15c7eb
VD
486 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
251eb40f 496/**
99a0378d 497 * sht15_measurement() - get a new value from device
251eb40f
JC
498 * @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.
99a0378d
VD
502 */
503static int sht15_measurement(struct sht15_data *data,
504 int command,
505 int timeout_msecs)
251eb40f
JC
506{
507 int ret;
82c7465b 508 u8 previous_config;
99a0378d 509
251eb40f
JC
510 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));
25985edc 520 /* Only relevant if the interrupt hasn't occurred. */
251eb40f
JC
521 if (!atomic_read(&data->interrupt_handled))
522 schedule_work(&data->read_work);
523 }
524 ret = wait_event_timeout(data->wait_queue,
99a0378d 525 (data->state == SHT15_READING_NOTHING),
251eb40f
JC
526 msecs_to_jiffies(timeout_msecs));
527 if (ret == 0) {/* timeout occurred */
24205e08 528 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
251eb40f
JC
529 sht15_connection_reset(data);
530 return -ETIME;
531 }
82c7465b
JO
532
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
251eb40f
JC
555 return 0;
556}
557
558/**
99a0378d 559 * sht15_update_measurements() - get updated measures from device if too old
251eb40f 560 * @data: device state
99a0378d
VD
561 */
562static int sht15_update_measurements(struct sht15_data *data)
251eb40f
JC
563{
564 int ret = 0;
565 int timeout = HZ;
566
567 mutex_lock(&data->read_lock);
99a0378d
VD
568 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);
251eb40f
JC
572 if (ret)
573 goto error_ret;
99a0378d
VD
574 data->state = SHT15_READING_TEMP;
575 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
251eb40f
JC
576 if (ret)
577 goto error_ret;
99a0378d
VD
578 data->measurements_valid = true;
579 data->last_measurement = jiffies;
251eb40f
JC
580 }
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.
99a0378d 592 */
251eb40f
JC
593static inline int sht15_calc_temp(struct sht15_data *data)
594{
328a2c22 595 int d1 = temppoints[0].d1;
cc15c7eb 596 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
251eb40f
JC
597 int i;
598
328a2c22 599 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
251eb40f
JC
600 /* Find pointer to interpolate */
601 if (data->supply_uV > temppoints[i - 1].vdd) {
328a2c22 602 d1 = (data->supply_uV - temppoints[i - 1].vdd)
251eb40f
JC
603 * (temppoints[i].d1 - temppoints[i - 1].d1)
604 / (temppoints[i].vdd - temppoints[i - 1].vdd)
605 + temppoints[i - 1].d1;
606 break;
607 }
608
cc15c7eb 609 return data->val_temp * d2 + d1;
251eb40f
JC
610}
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.
99a0378d
VD
618 *
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 */
251eb40f
JC
622static inline int sht15_calc_humid(struct sht15_data *data)
623{
99a0378d 624 int rh_linear; /* milli percent */
251eb40f 625 int temp = sht15_calc_temp(data);
cc15c7eb
VD
626 int c2, c3;
627 int t2;
251eb40f 628 const int c1 = -4;
cc15c7eb
VD
629
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 }
251eb40f 639
99a0378d
VD
640 rh_linear = c1 * 1000
641 + c2 * data->val_humid / 1000
ccd32e73 642 + (data->val_humid * data->val_humid * c3) / 10000;
cc15c7eb 643 return (temp - 25000) * (10000 + t2 * data->val_humid)
99a0378d 644 / 1000000 + rh_linear;
251eb40f
JC
645}
646
cc15c7eb
VD
647/**
648 * 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 *
e9b6e9f3 677 * Will be called on write access to heater_enable sysfs attribute.
cc15c7eb
VD
678 * 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
179c4fdb 689 if (kstrtol(buf, 10, &value))
cc15c7eb
VD
690 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
99a0378d
VD
705/**
706 * 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 */
251eb40f
JC
714static 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 */
99a0378d 722 ret = sht15_update_measurements(data);
251eb40f
JC
723
724 return ret ? ret : sprintf(buf, "%d\n",
725 sht15_calc_temp(data));
726}
727
99a0378d
VD
728/**
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 */
251eb40f
JC
737static 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
99a0378d 744 ret = sht15_update_measurements(data);
251eb40f
JC
745
746 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
99a0378d
VD
747}
748
251eb40f
JC
749static 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
99a0378d
VD
757static 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);
cc15c7eb
VD
761static 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);
251eb40f
JC
767static 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,
cc15c7eb
VD
771 &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,
251eb40f
JC
774 &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;
99a0378d 785
251eb40f
JC
786 /* First disable the interrupt */
787 disable_irq_nosync(irq);
788 atomic_inc(&data->interrupt_handled);
789 /* Then schedule a reading work struct */
99a0378d 790 if (data->state != SHT15_READING_NOTHING)
251eb40f
JC
791 schedule_work(&data->read_work);
792 return IRQ_HANDLED;
793}
794
251eb40f
JC
795static void sht15_bh_read_data(struct work_struct *work_s)
796{
251eb40f 797 uint16_t val = 0;
82c7465b
JO
798 u8 dev_checksum = 0;
799 u8 checksum_vals[3];
251eb40f
JC
800 struct sht15_data *data
801 = container_of(work_s, struct sht15_data,
802 read_work);
99a0378d 803
251eb40f
JC
804 /* Firstly, verify the line is low */
805 if (gpio_get_value(data->pdata->gpio_data)) {
99a0378d
VD
806 /*
807 * If not, then start the interrupt again - care here as could
808 * have gone low in meantime so verify it hasn't!
809 */
251eb40f
JC
810 atomic_set(&data->interrupt_handled, 0);
811 enable_irq(gpio_to_irq(data->pdata->gpio_data));
c9e1498a 812 /* If still not occurred or another handler was scheduled */
251eb40f
JC
813 if (gpio_get_value(data->pdata->gpio_data)
814 || atomic_read(&data->interrupt_handled))
815 return;
816 }
99a0378d 817
251eb40f 818 /* Read the data back from the device */
cc15c7eb
VD
819 val = sht15_read_byte(data);
820 val <<= 8;
821 sht15_ack(data);
822 val |= sht15_read_byte(data);
99a0378d 823
82c7465b
JO
824 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
251eb40f
JC
839 /* Tell the device we are done */
840 sht15_end_transmission(data);
841
99a0378d 842 switch (data->state) {
251eb40f
JC
843 case SHT15_READING_TEMP:
844 data->val_temp = val;
845 break;
846 case SHT15_READING_HUMID:
847 data->val_humid = val;
848 break;
99a0378d
VD
849 default:
850 break;
251eb40f
JC
851 }
852
99a0378d 853 data->state = SHT15_READING_NOTHING;
251eb40f
JC
854 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.
99a0378d 873 */
251eb40f 874static int sht15_invalidate_voltage(struct notifier_block *nb,
99a0378d
VD
875 unsigned long event,
876 void *ignored)
251eb40f
JC
877{
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
887static int __devinit sht15_probe(struct platform_device *pdev)
888{
6edf3c30 889 int ret;
38fe7560 890 struct sht15_data *data;
cc15c7eb 891 u8 status = 0;
251eb40f 892
38fe7560
GR
893 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
894 if (!data)
895 return -ENOMEM;
251eb40f
JC
896
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) {
99a0378d 905 dev_err(&pdev->dev, "no platform data supplied\n");
38fe7560 906 return -EINVAL;
251eb40f
JC
907 }
908 data->pdata = pdev->dev.platform_data;
99a0378d 909 data->supply_uV = data->pdata->supply_mv * 1000;
82c7465b
JO
910 if (data->pdata->checksum)
911 data->checksumming = true;
cc15c7eb
VD
912 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;
251eb40f 916
99a0378d
VD
917 /*
918 * If a regulator is available,
919 * query what the supply voltage actually is!
920 */
38fe7560 921 data->reg = devm_regulator_get(data->dev, "vcc");
251eb40f 922 if (!IS_ERR(data->reg)) {
c7a78d2c
JD
923 int voltage;
924
925 voltage = regulator_get_voltage(data->reg);
926 if (voltage)
927 data->supply_uV = voltage;
928
251eb40f 929 regulator_enable(data->reg);
99a0378d
VD
930 /*
931 * Setup a notifier block to update this if another device
932 * causes the voltage to change
933 */
251eb40f
JC
934 data->nb.notifier_call = &sht15_invalidate_voltage;
935 ret = regulator_register_notifier(data->reg, &data->nb);
181148ae
VD
936 if (ret) {
937 dev_err(&pdev->dev,
938 "regulator notifier request failed\n");
939 regulator_disable(data->reg);
38fe7560 940 return ret;
181148ae 941 }
251eb40f 942 }
99a0378d
VD
943
944 /* Try requesting the GPIOs */
38fe7560 945 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_sck, "SHT15 sck");
251eb40f 946 if (ret) {
99a0378d 947 dev_err(&pdev->dev, "gpio request failed\n");
181148ae 948 goto err_release_reg;
251eb40f
JC
949 }
950 gpio_direction_output(data->pdata->gpio_sck, 0);
99a0378d 951
38fe7560
GR
952 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
953 "SHT15 data");
251eb40f 954 if (ret) {
99a0378d 955 dev_err(&pdev->dev, "gpio request failed\n");
38fe7560 956 goto err_release_reg;
251eb40f 957 }
251eb40f 958
38fe7560
GR
959 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);
251eb40f 964 if (ret) {
99a0378d 965 dev_err(&pdev->dev, "failed to get irq for data line\n");
38fe7560 966 goto err_release_reg;
251eb40f
JC
967 }
968 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
969 sht15_connection_reset(data);
181148ae
VD
970 ret = sht15_soft_reset(data);
971 if (ret)
38fe7560 972 goto err_release_reg;
181148ae 973
cc15c7eb
VD
974 /* write status with platform data options */
975 if (status) {
976 ret = sht15_send_status(data, status);
977 if (ret)
38fe7560 978 goto err_release_reg;
cc15c7eb
VD
979 }
980
181148ae
VD
981 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
982 if (ret) {
983 dev_err(&pdev->dev, "sysfs create failed\n");
38fe7560 984 goto err_release_reg;
181148ae 985 }
251eb40f
JC
986
987 data->hwmon_dev = hwmon_device_register(data->dev);
988 if (IS_ERR(data->hwmon_dev)) {
989 ret = PTR_ERR(data->hwmon_dev);
181148ae 990 goto err_release_sysfs_group;
251eb40f 991 }
99a0378d 992
251eb40f
JC
993 return 0;
994
181148ae
VD
995err_release_sysfs_group:
996 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
181148ae
VD
997err_release_reg:
998 if (!IS_ERR(data->reg)) {
999 regulator_unregister_notifier(data->reg, &data->nb);
1000 regulator_disable(data->reg);
181148ae 1001 }
251eb40f
JC
1002 return ret;
1003}
1004
1005static int __devexit sht15_remove(struct platform_device *pdev)
1006{
1007 struct sht15_data *data = platform_get_drvdata(pdev);
1008
99a0378d
VD
1009 /*
1010 * Make sure any reads from the device are done and
1011 * prevent new ones beginning
1012 */
251eb40f 1013 mutex_lock(&data->read_lock);
cc15c7eb
VD
1014 if (sht15_soft_reset(data)) {
1015 mutex_unlock(&data->read_lock);
1016 return -EFAULT;
1017 }
251eb40f
JC
1018 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);
251eb40f
JC
1023 }
1024
251eb40f 1025 mutex_unlock(&data->read_lock);
99a0378d 1026
251eb40f
JC
1027 return 0;
1028}
1029
edec5af7
VD
1030static struct platform_device_id sht15_device_ids[] = {
1031 { "sht10", sht10 },
1032 { "sht11", sht11 },
1033 { "sht15", sht15 },
1034 { "sht71", sht71 },
1035 { "sht75", sht75 },
1036 { }
251eb40f 1037};
edec5af7 1038MODULE_DEVICE_TABLE(platform, sht15_device_ids);
251eb40f 1039
edec5af7
VD
1040static struct platform_driver sht15_driver = {
1041 .driver = {
1042 .name = "sht15",
1043 .owner = THIS_MODULE,
1044 },
1045 .probe = sht15_probe,
9e5e9b7a 1046 .remove = sht15_remove,
edec5af7
VD
1047 .id_table = sht15_device_ids,
1048};
1049module_platform_driver(sht15_driver);
251eb40f
JC
1050
1051MODULE_LICENSE("GPL");
edec5af7 1052MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");