Merge remote-tracking branch 'asoc/topic/davinci' into asoc-next
[deliverable/linux.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.
142c0901 142 * @supply_uv: local copy of supply voltage used to allow use of
99a0378d 143 * regulator consumer if available.
142c0901 144 * @supply_uv_valid: indicates that an updated value has not yet been
99a0378d
VD
145 * obtained from the regulator and so any calculations
146 * based upon it will be invalid.
142c0901 147 * @update_supply_work: work struct that is used to update the supply_uv.
99a0378d 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;
142c0901
VD
169 int supply_uv;
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 */
412e29c1 215static int sht15_connection_reset(struct sht15_data *data)
251eb40f 216{
412e29c1 217 int i, err;
99a0378d 218
412e29c1
VD
219 err = gpio_direction_output(data->pdata->gpio_data, 1);
220 if (err)
221 return err;
251eb40f
JC
222 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 }
412e29c1 231 return 0;
251eb40f 232}
99a0378d 233
251eb40f
JC
234/**
235 * sht15_send_bit() - send an individual bit to the device
236 * @data: device state data
237 * @val: value of bit to be sent
99a0378d 238 */
251eb40f
JC
239static inline void sht15_send_bit(struct sht15_data *data, int val)
240{
251eb40f
JC
241 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
251eb40f 251 * @data: device state data
99a0378d 252 *
251eb40f
JC
253 * 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.
99a0378d 256 */
412e29c1 257static int sht15_transmission_start(struct sht15_data *data)
251eb40f 258{
412e29c1
VD
259 int err;
260
251eb40f 261 /* ensure data is high and output */
412e29c1
VD
262 err = gpio_direction_output(data->pdata->gpio_data, 1);
263 if (err)
264 return err;
251eb40f
JC
265 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);
412e29c1 280 return 0;
251eb40f 281}
99a0378d 282
251eb40f
JC
283/**
284 * sht15_send_byte() - send a single byte to the device
285 * @data: device state
286 * @byte: value to be sent
99a0378d 287 */
251eb40f
JC
288static void sht15_send_byte(struct sht15_data *data, u8 byte)
289{
290 int i;
99a0378d 291
251eb40f
JC
292 for (i = 0; i < 8; i++) {
293 sht15_send_bit(data, !!(byte & 0x80));
294 byte <<= 1;
295 }
296}
99a0378d 297
251eb40f
JC
298/**
299 * sht15_wait_for_response() - checks for ack from device
300 * @data: device state
99a0378d 301 */
251eb40f
JC
302static int sht15_wait_for_response(struct sht15_data *data)
303{
412e29c1
VD
304 int err;
305
306 err = gpio_direction_input(data->pdata->gpio_data);
307 if (err)
308 return err;
251eb40f
JC
309 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");
412e29c1
VD
314 err = sht15_connection_reset(data);
315 if (err)
316 return err;
251eb40f
JC
317 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.
99a0378d 331 */
251eb40f
JC
332static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
333{
412e29c1 334 int err;
99a0378d 335
412e29c1
VD
336 err = sht15_transmission_start(data);
337 if (err)
338 return err;
251eb40f 339 sht15_send_byte(data, cmd);
412e29c1 340 return sht15_wait_for_response(data);
251eb40f 341}
99a0378d 342
181148ae
VD
343/**
344 * 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);
cc15c7eb
VD
357 /* device resets default hardware status register value */
358 data->val_status = 0;
359
360 return ret;
361}
362
82c7465b
JO
363/**
364 * 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 */
412e29c1 370static int sht15_ack(struct sht15_data *data)
82c7465b 371{
412e29c1
VD
372 int err;
373
374 err = gpio_direction_output(data->pdata->gpio_data, 0);
375 if (err)
376 return err;
82c7465b
JO
377 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
412e29c1 384 return gpio_direction_input(data->pdata->gpio_data);
82c7465b
JO
385}
386
cc15c7eb
VD
387/**
388 * 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 */
412e29c1 393static int sht15_end_transmission(struct sht15_data *data)
cc15c7eb 394{
412e29c1
VD
395 int err;
396
397 err = gpio_direction_output(data->pdata->gpio_data, 1);
398 if (err)
399 return err;
cc15c7eb
VD
400 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);
412e29c1 405 return 0;
cc15c7eb
VD
406}
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{
412e29c1
VD
437 int err;
438
439 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;
cc15c7eb
VD
445 ndelay(SHT15_TSU);
446 sht15_send_byte(data, status);
412e29c1
VD
447 err = sht15_wait_for_response(data);
448 if (err)
449 return err;
181148ae 450
cc15c7eb 451 data->val_status = status;
181148ae
VD
452 return 0;
453}
454
cc15c7eb
VD
455/**
456 * 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;
82c7465b
JO
465 u8 previous_config;
466 u8 dev_checksum = 0;
467 u8 checksum_vals[2];
cc15c7eb
VD
468 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)
412e29c1 475 goto unlock;
cc15c7eb
VD
476 status = sht15_read_byte(data);
477
82c7465b
JO
478 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
412e29c1
VD
487 ret = sht15_end_transmission(data);
488 if (ret)
489 goto unlock;
cc15c7eb 490
82c7465b
JO
491 /*
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)
412e29c1 500 goto unlock;
82c7465b
JO
501 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");
412e29c1 507 goto unlock;
82c7465b
JO
508 }
509 }
510 ret = -EAGAIN;
412e29c1 511 goto unlock;
82c7465b
JO
512 }
513
cc15c7eb
VD
514 data->val_status = status;
515 data->status_valid = true;
516 data->last_status = jiffies;
517 }
cc15c7eb 518
412e29c1
VD
519unlock:
520 mutex_unlock(&data->read_lock);
cc15c7eb
VD
521 return ret;
522}
523
251eb40f 524/**
99a0378d 525 * sht15_measurement() - get a new value from device
251eb40f
JC
526 * @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.
99a0378d
VD
530 */
531static int sht15_measurement(struct sht15_data *data,
532 int command,
533 int timeout_msecs)
251eb40f
JC
534{
535 int ret;
82c7465b 536 u8 previous_config;
99a0378d 537
251eb40f
JC
538 ret = sht15_send_cmd(data, command);
539 if (ret)
540 return ret;
541
412e29c1
VD
542 ret = gpio_direction_input(data->pdata->gpio_data);
543 if (ret)
544 return ret;
251eb40f
JC
545 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));
25985edc 550 /* Only relevant if the interrupt hasn't occurred. */
251eb40f
JC
551 if (!atomic_read(&data->interrupt_handled))
552 schedule_work(&data->read_work);
553 }
554 ret = wait_event_timeout(data->wait_queue,
99a0378d 555 (data->state == SHT15_READING_NOTHING),
251eb40f 556 msecs_to_jiffies(timeout_msecs));
412e29c1
VD
557 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 */
24205e08 561 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
412e29c1
VD
562 ret = sht15_connection_reset(data);
563 if (ret)
564 return ret;
251eb40f
JC
565 return -ETIME;
566 }
82c7465b
JO
567
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
251eb40f
JC
590 return 0;
591}
592
593/**
99a0378d 594 * sht15_update_measurements() - get updated measures from device if too old
251eb40f 595 * @data: device state
99a0378d
VD
596 */
597static int sht15_update_measurements(struct sht15_data *data)
251eb40f
JC
598{
599 int ret = 0;
600 int timeout = HZ;
601
602 mutex_lock(&data->read_lock);
99a0378d
VD
603 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);
251eb40f 607 if (ret)
412e29c1 608 goto unlock;
99a0378d
VD
609 data->state = SHT15_READING_TEMP;
610 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
251eb40f 611 if (ret)
412e29c1 612 goto unlock;
99a0378d
VD
613 data->measurements_valid = true;
614 data->last_measurement = jiffies;
251eb40f 615 }
251eb40f 616
412e29c1
VD
617unlock:
618 mutex_unlock(&data->read_lock);
251eb40f
JC
619 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.
99a0378d 627 */
251eb40f
JC
628static inline int sht15_calc_temp(struct sht15_data *data)
629{
328a2c22 630 int d1 = temppoints[0].d1;
cc15c7eb 631 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
251eb40f
JC
632 int i;
633
328a2c22 634 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
251eb40f 635 /* Find pointer to interpolate */
142c0901
VD
636 if (data->supply_uv > temppoints[i - 1].vdd) {
637 d1 = (data->supply_uv - temppoints[i - 1].vdd)
251eb40f
JC
638 * (temppoints[i].d1 - temppoints[i - 1].d1)
639 / (temppoints[i].vdd - temppoints[i - 1].vdd)
640 + temppoints[i - 1].d1;
641 break;
642 }
643
cc15c7eb 644 return data->val_temp * d2 + d1;
251eb40f
JC
645}
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.
99a0378d
VD
653 *
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 */
251eb40f
JC
657static inline int sht15_calc_humid(struct sht15_data *data)
658{
99a0378d 659 int rh_linear; /* milli percent */
251eb40f 660 int temp = sht15_calc_temp(data);
cc15c7eb
VD
661 int c2, c3;
662 int t2;
251eb40f 663 const int c1 = -4;
cc15c7eb
VD
664
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 }
251eb40f 674
99a0378d
VD
675 rh_linear = c1 * 1000
676 + c2 * data->val_humid / 1000
ccd32e73 677 + (data->val_humid * data->val_humid * c3) / 10000;
cc15c7eb 678 return (temp - 25000) * (10000 + t2 * data->val_humid)
99a0378d 679 / 1000000 + rh_linear;
251eb40f
JC
680}
681
cc15c7eb
VD
682/**
683 * 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 *
e9b6e9f3 712 * Will be called on write access to heater_enable sysfs attribute.
cc15c7eb
VD
713 * 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
179c4fdb 724 if (kstrtol(buf, 10, &value))
cc15c7eb
VD
725 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
99a0378d
VD
740/**
741 * 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 */
251eb40f
JC
749static 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 */
99a0378d 757 ret = sht15_update_measurements(data);
251eb40f
JC
758
759 return ret ? ret : sprintf(buf, "%d\n",
760 sht15_calc_temp(data));
761}
762
99a0378d
VD
763/**
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 */
251eb40f
JC
772static 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
99a0378d 779 ret = sht15_update_measurements(data);
251eb40f
JC
780
781 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
99a0378d
VD
782}
783
251eb40f
JC
784static 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
99a0378d
VD
792static 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);
cc15c7eb
VD
796static 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);
251eb40f
JC
802static 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,
cc15c7eb
VD
806 &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,
251eb40f
JC
809 &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;
99a0378d 820
251eb40f
JC
821 /* First disable the interrupt */
822 disable_irq_nosync(irq);
823 atomic_inc(&data->interrupt_handled);
824 /* Then schedule a reading work struct */
99a0378d 825 if (data->state != SHT15_READING_NOTHING)
251eb40f
JC
826 schedule_work(&data->read_work);
827 return IRQ_HANDLED;
828}
829
251eb40f
JC
830static void sht15_bh_read_data(struct work_struct *work_s)
831{
251eb40f 832 uint16_t val = 0;
82c7465b
JO
833 u8 dev_checksum = 0;
834 u8 checksum_vals[3];
251eb40f
JC
835 struct sht15_data *data
836 = container_of(work_s, struct sht15_data,
837 read_work);
99a0378d 838
251eb40f
JC
839 /* Firstly, verify the line is low */
840 if (gpio_get_value(data->pdata->gpio_data)) {
99a0378d
VD
841 /*
842 * If not, then start the interrupt again - care here as could
843 * have gone low in meantime so verify it hasn't!
844 */
251eb40f
JC
845 atomic_set(&data->interrupt_handled, 0);
846 enable_irq(gpio_to_irq(data->pdata->gpio_data));
c9e1498a 847 /* If still not occurred or another handler was scheduled */
251eb40f
JC
848 if (gpio_get_value(data->pdata->gpio_data)
849 || atomic_read(&data->interrupt_handled))
850 return;
851 }
99a0378d 852
251eb40f 853 /* Read the data back from the device */
cc15c7eb
VD
854 val = sht15_read_byte(data);
855 val <<= 8;
412e29c1
VD
856 if (sht15_ack(data))
857 goto wakeup;
cc15c7eb 858 val |= sht15_read_byte(data);
99a0378d 859
82c7465b
JO
860 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 */
412e29c1
VD
865 if (sht15_ack(data))
866 goto wakeup;
82c7465b
JO
867 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
251eb40f 876 /* Tell the device we are done */
412e29c1
VD
877 if (sht15_end_transmission(data))
878 goto wakeup;
251eb40f 879
99a0378d 880 switch (data->state) {
251eb40f
JC
881 case SHT15_READING_TEMP:
882 data->val_temp = val;
883 break;
884 case SHT15_READING_HUMID:
885 data->val_humid = val;
886 break;
99a0378d
VD
887 default:
888 break;
251eb40f
JC
889 }
890
99a0378d 891 data->state = SHT15_READING_NOTHING;
412e29c1 892wakeup:
251eb40f
JC
893 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);
142c0901 901 data->supply_uv = regulator_get_voltage(data->reg);
251eb40f
JC
902}
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.
99a0378d 912 */
251eb40f 913static int sht15_invalidate_voltage(struct notifier_block *nb,
99a0378d
VD
914 unsigned long event,
915 void *ignored)
251eb40f
JC
916{
917 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
918
919 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
142c0901 920 data->supply_uv_valid = false;
251eb40f
JC
921 schedule_work(&data->update_supply_work);
922
923 return NOTIFY_OK;
924}
925
6c931ae1 926static int sht15_probe(struct platform_device *pdev)
251eb40f 927{
6edf3c30 928 int ret;
38fe7560 929 struct sht15_data *data;
cc15c7eb 930 u8 status = 0;
251eb40f 931
38fe7560
GR
932 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
933 if (!data)
934 return -ENOMEM;
251eb40f
JC
935
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
a8b3a3a5 943 if (dev_get_platdata(&pdev->dev) == NULL) {
99a0378d 944 dev_err(&pdev->dev, "no platform data supplied\n");
38fe7560 945 return -EINVAL;
251eb40f 946 }
a8b3a3a5 947 data->pdata = dev_get_platdata(&pdev->dev);
142c0901 948 data->supply_uv = data->pdata->supply_mv * 1000;
82c7465b
JO
949 if (data->pdata->checksum)
950 data->checksumming = true;
cc15c7eb
VD
951 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;
251eb40f 955
99a0378d
VD
956 /*
957 * If a regulator is available,
958 * query what the supply voltage actually is!
959 */
9e059bac 960 data->reg = devm_regulator_get_optional(data->dev, "vcc");
251eb40f 961 if (!IS_ERR(data->reg)) {
c7a78d2c
JD
962 int voltage;
963
964 voltage = regulator_get_voltage(data->reg);
965 if (voltage)
142c0901 966 data->supply_uv = voltage;
c7a78d2c 967
3e78080f
MB
968 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
99a0378d
VD
975 /*
976 * Setup a notifier block to update this if another device
977 * causes the voltage to change
978 */
251eb40f
JC
979 data->nb.notifier_call = &sht15_invalidate_voltage;
980 ret = regulator_register_notifier(data->reg, &data->nb);
181148ae
VD
981 if (ret) {
982 dev_err(&pdev->dev,
983 "regulator notifier request failed\n");
984 regulator_disable(data->reg);
38fe7560 985 return ret;
181148ae 986 }
251eb40f 987 }
99a0378d
VD
988
989 /* Try requesting the GPIOs */
412e29c1
VD
990 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
991 GPIOF_OUT_INIT_LOW, "SHT15 sck");
251eb40f 992 if (ret) {
412e29c1 993 dev_err(&pdev->dev, "clock line GPIO request failed\n");
181148ae 994 goto err_release_reg;
251eb40f 995 }
99a0378d 996
38fe7560
GR
997 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
998 "SHT15 data");
251eb40f 999 if (ret) {
412e29c1 1000 dev_err(&pdev->dev, "data line GPIO request failed\n");
38fe7560 1001 goto err_release_reg;
251eb40f 1002 }
251eb40f 1003
38fe7560
GR
1004 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);
251eb40f 1009 if (ret) {
99a0378d 1010 dev_err(&pdev->dev, "failed to get irq for data line\n");
38fe7560 1011 goto err_release_reg;
251eb40f
JC
1012 }
1013 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
412e29c1
VD
1014 ret = sht15_connection_reset(data);
1015 if (ret)
1016 goto err_release_reg;
181148ae
VD
1017 ret = sht15_soft_reset(data);
1018 if (ret)
38fe7560 1019 goto err_release_reg;
181148ae 1020
cc15c7eb
VD
1021 /* write status with platform data options */
1022 if (status) {
1023 ret = sht15_send_status(data, status);
1024 if (ret)
38fe7560 1025 goto err_release_reg;
cc15c7eb
VD
1026 }
1027
181148ae
VD
1028 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1029 if (ret) {
1030 dev_err(&pdev->dev, "sysfs create failed\n");
38fe7560 1031 goto err_release_reg;
181148ae 1032 }
251eb40f
JC
1033
1034 data->hwmon_dev = hwmon_device_register(data->dev);
1035 if (IS_ERR(data->hwmon_dev)) {
1036 ret = PTR_ERR(data->hwmon_dev);
181148ae 1037 goto err_release_sysfs_group;
251eb40f 1038 }
99a0378d 1039
251eb40f
JC
1040 return 0;
1041
181148ae
VD
1042err_release_sysfs_group:
1043 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
181148ae
VD
1044err_release_reg:
1045 if (!IS_ERR(data->reg)) {
1046 regulator_unregister_notifier(data->reg, &data->nb);
1047 regulator_disable(data->reg);
181148ae 1048 }
251eb40f
JC
1049 return ret;
1050}
1051
281dfd0b 1052static int sht15_remove(struct platform_device *pdev)
251eb40f
JC
1053{
1054 struct sht15_data *data = platform_get_drvdata(pdev);
1055
99a0378d
VD
1056 /*
1057 * Make sure any reads from the device are done and
1058 * prevent new ones beginning
1059 */
251eb40f 1060 mutex_lock(&data->read_lock);
cc15c7eb
VD
1061 if (sht15_soft_reset(data)) {
1062 mutex_unlock(&data->read_lock);
1063 return -EFAULT;
1064 }
251eb40f
JC
1065 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);
251eb40f
JC
1070 }
1071
251eb40f 1072 mutex_unlock(&data->read_lock);
99a0378d 1073
251eb40f
JC
1074 return 0;
1075}
1076
edec5af7
VD
1077static struct platform_device_id sht15_device_ids[] = {
1078 { "sht10", sht10 },
1079 { "sht11", sht11 },
1080 { "sht15", sht15 },
1081 { "sht71", sht71 },
1082 { "sht75", sht75 },
1083 { }
251eb40f 1084};
edec5af7 1085MODULE_DEVICE_TABLE(platform, sht15_device_ids);
251eb40f 1086
edec5af7
VD
1087static struct platform_driver sht15_driver = {
1088 .driver = {
1089 .name = "sht15",
1090 .owner = THIS_MODULE,
1091 },
1092 .probe = sht15_probe,
9e5e9b7a 1093 .remove = sht15_remove,
edec5af7
VD
1094 .id_table = sht15_device_ids,
1095};
1096module_platform_driver(sht15_driver);
251eb40f
JC
1097
1098MODULE_LICENSE("GPL");
edec5af7 1099MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");
This page took 0.395928 seconds and 5 git commands to generate.