]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/sht15.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.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>
251eb40f
JC
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/mutex.h>
26#include <linux/platform_device.h>
d43c36dc 27#include <linux/sched.h>
251eb40f
JC
28#include <linux/delay.h>
29#include <linux/jiffies.h>
30#include <linux/err.h>
251eb40f 31#include <linux/regulator/consumer.h>
5a0e3ad6 32#include <linux/slab.h>
60063497 33#include <linux/atomic.h>
33836ee9 34#include <linux/bitrev.h>
18673114
LW
35#include <linux/gpio/consumer.h>
36#include <linux/of.h>
251eb40f 37
99a0378d
VD
38/* Commands */
39#define SHT15_MEASURE_TEMP 0x03
40#define SHT15_MEASURE_RH 0x05
cc15c7eb
VD
41#define SHT15_WRITE_STATUS 0x06
42#define SHT15_READ_STATUS 0x07
181148ae 43#define SHT15_SOFT_RESET 0x1E
251eb40f 44
99a0378d
VD
45/* Min timings */
46#define SHT15_TSCKL 100 /* (nsecs) clock low */
47#define SHT15_TSCKH 100 /* (nsecs) clock high */
48#define SHT15_TSU 150 /* (nsecs) data setup time */
181148ae 49#define SHT15_TSRST 11 /* (msecs) soft reset time */
251eb40f 50
cc15c7eb
VD
51/* Status Register Bits */
52#define SHT15_STATUS_LOW_RESOLUTION 0x01
53#define SHT15_STATUS_NO_OTP_RELOAD 0x02
54#define SHT15_STATUS_HEATER 0x04
55#define SHT15_STATUS_LOW_BATTERY 0x40
56
edec5af7
VD
57/* List of supported chips */
58enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
59
99a0378d
VD
60/* Actions the driver may be doing */
61enum sht15_state {
62 SHT15_READING_NOTHING,
63 SHT15_READING_TEMP,
64 SHT15_READING_HUMID
65};
251eb40f
JC
66
67/**
25985edc 68 * struct sht15_temppair - elements of voltage dependent temp calc
251eb40f
JC
69 * @vdd: supply voltage in microvolts
70 * @d1: see data sheet
71 */
72struct sht15_temppair {
73 int vdd; /* microvolts */
74 int d1;
75};
76
99a0378d 77/* Table 9 from datasheet - relates temperature calculation to supply voltage */
251eb40f
JC
78static const struct sht15_temppair temppoints[] = {
79 { 2500000, -39400 },
80 { 3000000, -39600 },
81 { 3500000, -39700 },
82 { 4000000, -39800 },
83 { 5000000, -40100 },
84};
85
82c7465b
JO
86/* Table from CRC datasheet, section 2.4 */
87static const u8 sht15_crc8_table[] = {
88 0, 49, 98, 83, 196, 245, 166, 151,
89 185, 136, 219, 234, 125, 76, 31, 46,
90 67, 114, 33, 16, 135, 182, 229, 212,
91 250, 203, 152, 169, 62, 15, 92, 109,
92 134, 183, 228, 213, 66, 115, 32, 17,
93 63, 14, 93, 108, 251, 202, 153, 168,
94 197, 244, 167, 150, 1, 48, 99, 82,
95 124, 77, 30, 47, 184, 137, 218, 235,
96 61, 12, 95, 110, 249, 200, 155, 170,
97 132, 181, 230, 215, 64, 113, 34, 19,
98 126, 79, 28, 45, 186, 139, 216, 233,
99 199, 246, 165, 148, 3, 50, 97, 80,
100 187, 138, 217, 232, 127, 78, 29, 44,
101 2, 51, 96, 81, 198, 247, 164, 149,
102 248, 201, 154, 171, 60, 13, 94, 111,
103 65, 112, 35, 18, 133, 180, 231, 214,
104 122, 75, 24, 41, 190, 143, 220, 237,
105 195, 242, 161, 144, 7, 54, 101, 84,
106 57, 8, 91, 106, 253, 204, 159, 174,
107 128, 177, 226, 211, 68, 117, 38, 23,
108 252, 205, 158, 175, 56, 9, 90, 107,
109 69, 116, 39, 22, 129, 176, 227, 210,
110 191, 142, 221, 236, 123, 74, 25, 40,
111 6, 55, 100, 85, 194, 243, 160, 145,
112 71, 118, 37, 20, 131, 178, 225, 208,
113 254, 207, 156, 173, 58, 11, 88, 105,
114 4, 53, 102, 87, 192, 241, 162, 147,
115 189, 140, 223, 238, 121, 72, 27, 42,
116 193, 240, 163, 146, 5, 52, 103, 86,
117 120, 73, 26, 43, 188, 141, 222, 239,
118 130, 179, 224, 209, 70, 119, 36, 21,
119 59, 10, 89, 104, 255, 206, 157, 172
120};
121
251eb40f
JC
122/**
123 * struct sht15_data - device instance specific data
18673114
LW
124 * @sck: clock GPIO line
125 * @data: data GPIO line
99a0378d
VD
126 * @read_work: bh of interrupt handler.
127 * @wait_queue: wait queue for getting values from device.
128 * @val_temp: last temperature value read from device.
129 * @val_humid: last humidity value read from device.
cc15c7eb 130 * @val_status: last status register value read from device.
82c7465b
JO
131 * @checksum_ok: last value read from the device passed CRC validation.
132 * @checksumming: flag used to enable the data validation with CRC.
99a0378d
VD
133 * @state: state identifying the action the driver is doing.
134 * @measurements_valid: are the current stored measures valid (start condition).
cc15c7eb 135 * @status_valid: is the current stored status valid (start condition).
99a0378d 136 * @last_measurement: time of last measure.
cc15c7eb 137 * @last_status: time of last status reading.
99a0378d
VD
138 * @read_lock: mutex to ensure only one read in progress at a time.
139 * @dev: associate device structure.
140 * @hwmon_dev: device associated with hwmon subsystem.
141 * @reg: associated regulator (if specified).
142 * @nb: notifier block to handle notifications of voltage
143 * changes.
142c0901 144 * @supply_uv: local copy of supply voltage used to allow use of
99a0378d 145 * regulator consumer if available.
142c0901 146 * @supply_uv_valid: indicates that an updated value has not yet been
99a0378d
VD
147 * obtained from the regulator and so any calculations
148 * based upon it will be invalid.
142c0901 149 * @update_supply_work: work struct that is used to update the supply_uv.
99a0378d 150 * @interrupt_handled: flag used to indicate a handler has been scheduled.
251eb40f
JC
151 */
152struct sht15_data {
18673114
LW
153 struct gpio_desc *sck;
154 struct gpio_desc *data;
251eb40f
JC
155 struct work_struct read_work;
156 wait_queue_head_t wait_queue;
157 uint16_t val_temp;
158 uint16_t val_humid;
cc15c7eb 159 u8 val_status;
82c7465b
JO
160 bool checksum_ok;
161 bool checksumming;
99a0378d
VD
162 enum sht15_state state;
163 bool measurements_valid;
cc15c7eb 164 bool status_valid;
99a0378d 165 unsigned long last_measurement;
cc15c7eb 166 unsigned long last_status;
251eb40f
JC
167 struct mutex read_lock;
168 struct device *dev;
169 struct device *hwmon_dev;
170 struct regulator *reg;
171 struct notifier_block nb;
142c0901
VD
172 int supply_uv;
173 bool supply_uv_valid;
251eb40f
JC
174 struct work_struct update_supply_work;
175 atomic_t interrupt_handled;
176};
177
82c7465b
JO
178/**
179 * sht15_crc8() - compute crc8
180 * @data: sht15 specific data.
181 * @value: sht15 retrieved data.
182 *
183 * This implements section 2 of the CRC datasheet.
184 */
185static u8 sht15_crc8(struct sht15_data *data,
186 const u8 *value,
187 int len)
188{
33836ee9 189 u8 crc = bitrev8(data->val_status & 0x0F);
82c7465b
JO
190
191 while (len--) {
192 crc = sht15_crc8_table[*value ^ crc];
193 value++;
194 }
195
196 return crc;
197}
198
251eb40f
JC
199/**
200 * sht15_connection_reset() - reset the comms interface
201 * @data: sht15 specific data
202 *
203 * This implements section 3.4 of the data sheet
204 */
412e29c1 205static int sht15_connection_reset(struct sht15_data *data)
251eb40f 206{
412e29c1 207 int i, err;
99a0378d 208
18673114 209 err = gpiod_direction_output(data->data, 1);
412e29c1
VD
210 if (err)
211 return err;
251eb40f 212 ndelay(SHT15_TSCKL);
18673114 213 gpiod_set_value(data->sck, 0);
251eb40f
JC
214 ndelay(SHT15_TSCKL);
215 for (i = 0; i < 9; ++i) {
18673114 216 gpiod_set_value(data->sck, 1);
251eb40f 217 ndelay(SHT15_TSCKH);
18673114 218 gpiod_set_value(data->sck, 0);
251eb40f
JC
219 ndelay(SHT15_TSCKL);
220 }
412e29c1 221 return 0;
251eb40f 222}
99a0378d 223
251eb40f
JC
224/**
225 * sht15_send_bit() - send an individual bit to the device
226 * @data: device state data
227 * @val: value of bit to be sent
99a0378d 228 */
251eb40f
JC
229static inline void sht15_send_bit(struct sht15_data *data, int val)
230{
18673114 231 gpiod_set_value(data->data, val);
251eb40f 232 ndelay(SHT15_TSU);
18673114 233 gpiod_set_value(data->sck, 1);
251eb40f 234 ndelay(SHT15_TSCKH);
18673114 235 gpiod_set_value(data->sck, 0);
251eb40f
JC
236 ndelay(SHT15_TSCKL); /* clock low time */
237}
238
239/**
240 * sht15_transmission_start() - specific sequence for new transmission
251eb40f 241 * @data: device state data
99a0378d 242 *
251eb40f
JC
243 * Timings for this are not documented on the data sheet, so very
244 * conservative ones used in implementation. This implements
245 * figure 12 on the data sheet.
99a0378d 246 */
412e29c1 247static int sht15_transmission_start(struct sht15_data *data)
251eb40f 248{
412e29c1
VD
249 int err;
250
251eb40f 251 /* ensure data is high and output */
18673114 252 err = gpiod_direction_output(data->data, 1);
412e29c1
VD
253 if (err)
254 return err;
251eb40f 255 ndelay(SHT15_TSU);
18673114 256 gpiod_set_value(data->sck, 0);
251eb40f 257 ndelay(SHT15_TSCKL);
18673114 258 gpiod_set_value(data->sck, 1);
251eb40f 259 ndelay(SHT15_TSCKH);
18673114 260 gpiod_set_value(data->data, 0);
251eb40f 261 ndelay(SHT15_TSU);
18673114 262 gpiod_set_value(data->sck, 0);
251eb40f 263 ndelay(SHT15_TSCKL);
18673114 264 gpiod_set_value(data->sck, 1);
251eb40f 265 ndelay(SHT15_TSCKH);
18673114 266 gpiod_set_value(data->data, 1);
251eb40f 267 ndelay(SHT15_TSU);
18673114 268 gpiod_set_value(data->sck, 0);
251eb40f 269 ndelay(SHT15_TSCKL);
412e29c1 270 return 0;
251eb40f 271}
99a0378d 272
251eb40f
JC
273/**
274 * sht15_send_byte() - send a single byte to the device
275 * @data: device state
276 * @byte: value to be sent
99a0378d 277 */
251eb40f
JC
278static void sht15_send_byte(struct sht15_data *data, u8 byte)
279{
280 int i;
99a0378d 281
251eb40f
JC
282 for (i = 0; i < 8; i++) {
283 sht15_send_bit(data, !!(byte & 0x80));
284 byte <<= 1;
285 }
286}
99a0378d 287
251eb40f
JC
288/**
289 * sht15_wait_for_response() - checks for ack from device
290 * @data: device state
99a0378d 291 */
251eb40f
JC
292static int sht15_wait_for_response(struct sht15_data *data)
293{
412e29c1
VD
294 int err;
295
18673114 296 err = gpiod_direction_input(data->data);
412e29c1
VD
297 if (err)
298 return err;
18673114 299 gpiod_set_value(data->sck, 1);
251eb40f 300 ndelay(SHT15_TSCKH);
18673114
LW
301 if (gpiod_get_value(data->data)) {
302 gpiod_set_value(data->sck, 0);
251eb40f 303 dev_err(data->dev, "Command not acknowledged\n");
412e29c1
VD
304 err = sht15_connection_reset(data);
305 if (err)
306 return err;
251eb40f
JC
307 return -EIO;
308 }
18673114 309 gpiod_set_value(data->sck, 0);
251eb40f
JC
310 ndelay(SHT15_TSCKL);
311 return 0;
312}
313
314/**
315 * sht15_send_cmd() - Sends a command to the device.
316 * @data: device state
317 * @cmd: command byte to be sent
318 *
319 * On entry, sck is output low, data is output pull high
320 * and the interrupt disabled.
99a0378d 321 */
251eb40f
JC
322static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
323{
412e29c1 324 int err;
99a0378d 325
412e29c1
VD
326 err = sht15_transmission_start(data);
327 if (err)
328 return err;
251eb40f 329 sht15_send_byte(data, cmd);
412e29c1 330 return sht15_wait_for_response(data);
251eb40f 331}
99a0378d 332
181148ae
VD
333/**
334 * sht15_soft_reset() - send a soft reset command
335 * @data: sht15 specific data.
336 *
337 * As described in section 3.2 of the datasheet.
338 */
339static int sht15_soft_reset(struct sht15_data *data)
340{
341 int ret;
342
343 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
344 if (ret)
345 return ret;
346 msleep(SHT15_TSRST);
cc15c7eb
VD
347 /* device resets default hardware status register value */
348 data->val_status = 0;
349
350 return ret;
351}
352
82c7465b
JO
353/**
354 * sht15_ack() - send a ack
355 * @data: sht15 specific data.
356 *
357 * Each byte of data is acknowledged by pulling the data line
358 * low for one clock pulse.
359 */
412e29c1 360static int sht15_ack(struct sht15_data *data)
82c7465b 361{
412e29c1
VD
362 int err;
363
18673114 364 err = gpiod_direction_output(data->data, 0);
412e29c1
VD
365 if (err)
366 return err;
82c7465b 367 ndelay(SHT15_TSU);
18673114 368 gpiod_set_value(data->sck, 1);
82c7465b 369 ndelay(SHT15_TSU);
18673114 370 gpiod_set_value(data->sck, 0);
82c7465b 371 ndelay(SHT15_TSU);
18673114 372 gpiod_set_value(data->data, 1);
82c7465b 373
18673114 374 return gpiod_direction_input(data->data);
82c7465b
JO
375}
376
cc15c7eb
VD
377/**
378 * sht15_end_transmission() - notify device of end of transmission
379 * @data: device state.
380 *
381 * This is basically a NAK (single clock pulse, data high).
382 */
412e29c1 383static int sht15_end_transmission(struct sht15_data *data)
cc15c7eb 384{
412e29c1
VD
385 int err;
386
18673114 387 err = gpiod_direction_output(data->data, 1);
412e29c1
VD
388 if (err)
389 return err;
cc15c7eb 390 ndelay(SHT15_TSU);
18673114 391 gpiod_set_value(data->sck, 1);
cc15c7eb 392 ndelay(SHT15_TSCKH);
18673114 393 gpiod_set_value(data->sck, 0);
cc15c7eb 394 ndelay(SHT15_TSCKL);
412e29c1 395 return 0;
cc15c7eb
VD
396}
397
398/**
399 * sht15_read_byte() - Read a byte back from the device
400 * @data: device state.
401 */
402static u8 sht15_read_byte(struct sht15_data *data)
403{
404 int i;
405 u8 byte = 0;
406
407 for (i = 0; i < 8; ++i) {
408 byte <<= 1;
18673114 409 gpiod_set_value(data->sck, 1);
cc15c7eb 410 ndelay(SHT15_TSCKH);
18673114
LW
411 byte |= !!gpiod_get_value(data->data);
412 gpiod_set_value(data->sck, 0);
cc15c7eb
VD
413 ndelay(SHT15_TSCKL);
414 }
415 return byte;
416}
417
418/**
419 * sht15_send_status() - write the status register byte
420 * @data: sht15 specific data.
421 * @status: the byte to set the status register with.
422 *
423 * As described in figure 14 and table 5 of the datasheet.
424 */
425static int sht15_send_status(struct sht15_data *data, u8 status)
426{
412e29c1
VD
427 int err;
428
429 err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
430 if (err)
431 return err;
18673114 432 err = gpiod_direction_output(data->data, 1);
412e29c1
VD
433 if (err)
434 return err;
cc15c7eb
VD
435 ndelay(SHT15_TSU);
436 sht15_send_byte(data, status);
412e29c1
VD
437 err = sht15_wait_for_response(data);
438 if (err)
439 return err;
181148ae 440
cc15c7eb 441 data->val_status = status;
181148ae
VD
442 return 0;
443}
444
cc15c7eb
VD
445/**
446 * sht15_update_status() - get updated status register from device if too old
447 * @data: device instance specific data.
448 *
449 * As described in figure 15 and table 5 of the datasheet.
450 */
451static int sht15_update_status(struct sht15_data *data)
452{
453 int ret = 0;
454 u8 status;
82c7465b
JO
455 u8 previous_config;
456 u8 dev_checksum = 0;
457 u8 checksum_vals[2];
cc15c7eb
VD
458 int timeout = HZ;
459
460 mutex_lock(&data->read_lock);
461 if (time_after(jiffies, data->last_status + timeout)
462 || !data->status_valid) {
463 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
464 if (ret)
412e29c1 465 goto unlock;
cc15c7eb
VD
466 status = sht15_read_byte(data);
467
82c7465b
JO
468 if (data->checksumming) {
469 sht15_ack(data);
33836ee9 470 dev_checksum = bitrev8(sht15_read_byte(data));
82c7465b
JO
471 checksum_vals[0] = SHT15_READ_STATUS;
472 checksum_vals[1] = status;
473 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
474 == dev_checksum);
475 }
476
412e29c1
VD
477 ret = sht15_end_transmission(data);
478 if (ret)
479 goto unlock;
cc15c7eb 480
82c7465b
JO
481 /*
482 * Perform checksum validation on the received data.
483 * Specification mentions that in case a checksum verification
484 * fails, a soft reset command must be sent to the device.
485 */
486 if (data->checksumming && !data->checksum_ok) {
487 previous_config = data->val_status & 0x07;
488 ret = sht15_soft_reset(data);
489 if (ret)
412e29c1 490 goto unlock;
82c7465b
JO
491 if (previous_config) {
492 ret = sht15_send_status(data, previous_config);
493 if (ret) {
494 dev_err(data->dev,
495 "CRC validation failed, unable "
496 "to restore device settings\n");
412e29c1 497 goto unlock;
82c7465b
JO
498 }
499 }
500 ret = -EAGAIN;
412e29c1 501 goto unlock;
82c7465b
JO
502 }
503
cc15c7eb
VD
504 data->val_status = status;
505 data->status_valid = true;
506 data->last_status = jiffies;
507 }
cc15c7eb 508
412e29c1
VD
509unlock:
510 mutex_unlock(&data->read_lock);
cc15c7eb
VD
511 return ret;
512}
513
251eb40f 514/**
99a0378d 515 * sht15_measurement() - get a new value from device
251eb40f
JC
516 * @data: device instance specific data
517 * @command: command sent to request value
518 * @timeout_msecs: timeout after which comms are assumed
519 * to have failed are reset.
99a0378d
VD
520 */
521static int sht15_measurement(struct sht15_data *data,
522 int command,
523 int timeout_msecs)
251eb40f
JC
524{
525 int ret;
82c7465b 526 u8 previous_config;
99a0378d 527
251eb40f
JC
528 ret = sht15_send_cmd(data, command);
529 if (ret)
530 return ret;
531
18673114 532 ret = gpiod_direction_input(data->data);
412e29c1
VD
533 if (ret)
534 return ret;
251eb40f
JC
535 atomic_set(&data->interrupt_handled, 0);
536
18673114
LW
537 enable_irq(gpiod_to_irq(data->data));
538 if (gpiod_get_value(data->data) == 0) {
539 disable_irq_nosync(gpiod_to_irq(data->data));
25985edc 540 /* Only relevant if the interrupt hasn't occurred. */
251eb40f
JC
541 if (!atomic_read(&data->interrupt_handled))
542 schedule_work(&data->read_work);
543 }
544 ret = wait_event_timeout(data->wait_queue,
99a0378d 545 (data->state == SHT15_READING_NOTHING),
251eb40f 546 msecs_to_jiffies(timeout_msecs));
412e29c1
VD
547 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
548 data->state = SHT15_READING_NOTHING;
549 return -EIO;
550 } else if (ret == 0) { /* timeout occurred */
18673114 551 disable_irq_nosync(gpiod_to_irq(data->data));
412e29c1
VD
552 ret = sht15_connection_reset(data);
553 if (ret)
554 return ret;
251eb40f
JC
555 return -ETIME;
556 }
82c7465b
JO
557
558 /*
559 * Perform checksum validation on the received data.
560 * Specification mentions that in case a checksum verification fails,
561 * a soft reset command must be sent to the device.
562 */
563 if (data->checksumming && !data->checksum_ok) {
564 previous_config = data->val_status & 0x07;
565 ret = sht15_soft_reset(data);
566 if (ret)
567 return ret;
568 if (previous_config) {
569 ret = sht15_send_status(data, previous_config);
570 if (ret) {
571 dev_err(data->dev,
572 "CRC validation failed, unable "
573 "to restore device settings\n");
574 return ret;
575 }
576 }
577 return -EAGAIN;
578 }
579
251eb40f
JC
580 return 0;
581}
582
583/**
99a0378d 584 * sht15_update_measurements() - get updated measures from device if too old
251eb40f 585 * @data: device state
99a0378d
VD
586 */
587static int sht15_update_measurements(struct sht15_data *data)
251eb40f
JC
588{
589 int ret = 0;
590 int timeout = HZ;
591
592 mutex_lock(&data->read_lock);
99a0378d
VD
593 if (time_after(jiffies, data->last_measurement + timeout)
594 || !data->measurements_valid) {
595 data->state = SHT15_READING_HUMID;
596 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
251eb40f 597 if (ret)
412e29c1 598 goto unlock;
99a0378d
VD
599 data->state = SHT15_READING_TEMP;
600 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
251eb40f 601 if (ret)
412e29c1 602 goto unlock;
99a0378d
VD
603 data->measurements_valid = true;
604 data->last_measurement = jiffies;
251eb40f 605 }
251eb40f 606
412e29c1
VD
607unlock:
608 mutex_unlock(&data->read_lock);
251eb40f
JC
609 return ret;
610}
611
612/**
613 * sht15_calc_temp() - convert the raw reading to a temperature
614 * @data: device state
615 *
616 * As per section 4.3 of the data sheet.
99a0378d 617 */
251eb40f
JC
618static inline int sht15_calc_temp(struct sht15_data *data)
619{
328a2c22 620 int d1 = temppoints[0].d1;
cc15c7eb 621 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
251eb40f
JC
622 int i;
623
328a2c22 624 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
251eb40f 625 /* Find pointer to interpolate */
142c0901
VD
626 if (data->supply_uv > temppoints[i - 1].vdd) {
627 d1 = (data->supply_uv - temppoints[i - 1].vdd)
251eb40f
JC
628 * (temppoints[i].d1 - temppoints[i - 1].d1)
629 / (temppoints[i].vdd - temppoints[i - 1].vdd)
630 + temppoints[i - 1].d1;
631 break;
632 }
633
cc15c7eb 634 return data->val_temp * d2 + d1;
251eb40f
JC
635}
636
637/**
638 * sht15_calc_humid() - using last temperature convert raw to humid
639 * @data: device state
640 *
641 * This is the temperature compensated version as per section 4.2 of
642 * the data sheet.
99a0378d
VD
643 *
644 * The sensor is assumed to be V3, which is compatible with V4.
645 * Humidity conversion coefficients are shown in table 7 of the datasheet.
646 */
251eb40f
JC
647static inline int sht15_calc_humid(struct sht15_data *data)
648{
99a0378d 649 int rh_linear; /* milli percent */
251eb40f 650 int temp = sht15_calc_temp(data);
cc15c7eb
VD
651 int c2, c3;
652 int t2;
251eb40f 653 const int c1 = -4;
cc15c7eb
VD
654
655 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
656 c2 = 648000; /* x 10 ^ -6 */
657 c3 = -7200; /* x 10 ^ -7 */
658 t2 = 1280;
659 } else {
660 c2 = 40500; /* x 10 ^ -6 */
661 c3 = -28; /* x 10 ^ -7 */
662 t2 = 80;
663 }
251eb40f 664
99a0378d
VD
665 rh_linear = c1 * 1000
666 + c2 * data->val_humid / 1000
ccd32e73 667 + (data->val_humid * data->val_humid * c3) / 10000;
cc15c7eb 668 return (temp - 25000) * (10000 + t2 * data->val_humid)
99a0378d 669 / 1000000 + rh_linear;
251eb40f
JC
670}
671
cc15c7eb
VD
672/**
673 * sht15_show_status() - show status information in sysfs
674 * @dev: device.
675 * @attr: device attribute.
676 * @buf: sysfs buffer where information is written to.
677 *
678 * Will be called on read access to temp1_fault, humidity1_fault
679 * and heater_enable sysfs attributes.
680 * Returns number of bytes written into buffer, negative errno on error.
681 */
682static ssize_t sht15_show_status(struct device *dev,
683 struct device_attribute *attr,
684 char *buf)
685{
686 int ret;
687 struct sht15_data *data = dev_get_drvdata(dev);
688 u8 bit = to_sensor_dev_attr(attr)->index;
689
690 ret = sht15_update_status(data);
691
692 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
693}
694
695/**
696 * sht15_store_heater() - change heater state via sysfs
697 * @dev: device.
698 * @attr: device attribute.
699 * @buf: sysfs buffer to read the new heater state from.
700 * @count: length of the data.
701 *
e9b6e9f3 702 * Will be called on write access to heater_enable sysfs attribute.
cc15c7eb
VD
703 * Returns number of bytes actually decoded, negative errno on error.
704 */
705static ssize_t sht15_store_heater(struct device *dev,
706 struct device_attribute *attr,
707 const char *buf, size_t count)
708{
709 int ret;
710 struct sht15_data *data = dev_get_drvdata(dev);
711 long value;
712 u8 status;
713
179c4fdb 714 if (kstrtol(buf, 10, &value))
cc15c7eb
VD
715 return -EINVAL;
716
717 mutex_lock(&data->read_lock);
718 status = data->val_status & 0x07;
719 if (!!value)
720 status |= SHT15_STATUS_HEATER;
721 else
722 status &= ~SHT15_STATUS_HEATER;
723
724 ret = sht15_send_status(data, status);
725 mutex_unlock(&data->read_lock);
726
727 return ret ? ret : count;
728}
729
99a0378d
VD
730/**
731 * sht15_show_temp() - show temperature measurement value in sysfs
732 * @dev: device.
733 * @attr: device attribute.
734 * @buf: sysfs buffer where measurement values are written to.
735 *
736 * Will be called on read access to temp1_input sysfs attribute.
737 * Returns number of bytes written into buffer, negative errno on error.
738 */
251eb40f
JC
739static ssize_t sht15_show_temp(struct device *dev,
740 struct device_attribute *attr,
741 char *buf)
742{
743 int ret;
744 struct sht15_data *data = dev_get_drvdata(dev);
745
746 /* Technically no need to read humidity as well */
99a0378d 747 ret = sht15_update_measurements(data);
251eb40f
JC
748
749 return ret ? ret : sprintf(buf, "%d\n",
750 sht15_calc_temp(data));
751}
752
99a0378d
VD
753/**
754 * sht15_show_humidity() - show humidity measurement value in sysfs
755 * @dev: device.
756 * @attr: device attribute.
757 * @buf: sysfs buffer where measurement values are written to.
758 *
759 * Will be called on read access to humidity1_input sysfs attribute.
760 * Returns number of bytes written into buffer, negative errno on error.
761 */
251eb40f
JC
762static ssize_t sht15_show_humidity(struct device *dev,
763 struct device_attribute *attr,
764 char *buf)
765{
766 int ret;
767 struct sht15_data *data = dev_get_drvdata(dev);
768
99a0378d 769 ret = sht15_update_measurements(data);
251eb40f
JC
770
771 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
99a0378d
VD
772}
773
af3d387f 774static ssize_t name_show(struct device *dev,
251eb40f
JC
775 struct device_attribute *attr,
776 char *buf)
777{
778 struct platform_device *pdev = to_platform_device(dev);
779 return sprintf(buf, "%s\n", pdev->name);
780}
781
99a0378d
VD
782static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
783 sht15_show_temp, NULL, 0);
784static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
785 sht15_show_humidity, NULL, 0);
cc15c7eb
VD
786static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
787 SHT15_STATUS_LOW_BATTERY);
788static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
789 SHT15_STATUS_LOW_BATTERY);
790static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
791 sht15_store_heater, SHT15_STATUS_HEATER);
af3d387f 792static DEVICE_ATTR_RO(name);
251eb40f
JC
793static struct attribute *sht15_attrs[] = {
794 &sensor_dev_attr_temp1_input.dev_attr.attr,
795 &sensor_dev_attr_humidity1_input.dev_attr.attr,
cc15c7eb
VD
796 &sensor_dev_attr_temp1_fault.dev_attr.attr,
797 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
798 &sensor_dev_attr_heater_enable.dev_attr.attr,
251eb40f
JC
799 &dev_attr_name.attr,
800 NULL,
801};
802
803static const struct attribute_group sht15_attr_group = {
804 .attrs = sht15_attrs,
805};
806
807static irqreturn_t sht15_interrupt_fired(int irq, void *d)
808{
809 struct sht15_data *data = d;
99a0378d 810
251eb40f
JC
811 /* First disable the interrupt */
812 disable_irq_nosync(irq);
813 atomic_inc(&data->interrupt_handled);
814 /* Then schedule a reading work struct */
99a0378d 815 if (data->state != SHT15_READING_NOTHING)
251eb40f
JC
816 schedule_work(&data->read_work);
817 return IRQ_HANDLED;
818}
819
251eb40f
JC
820static void sht15_bh_read_data(struct work_struct *work_s)
821{
251eb40f 822 uint16_t val = 0;
82c7465b
JO
823 u8 dev_checksum = 0;
824 u8 checksum_vals[3];
251eb40f
JC
825 struct sht15_data *data
826 = container_of(work_s, struct sht15_data,
827 read_work);
99a0378d 828
251eb40f 829 /* Firstly, verify the line is low */
18673114 830 if (gpiod_get_value(data->data)) {
99a0378d
VD
831 /*
832 * If not, then start the interrupt again - care here as could
833 * have gone low in meantime so verify it hasn't!
834 */
251eb40f 835 atomic_set(&data->interrupt_handled, 0);
18673114 836 enable_irq(gpiod_to_irq(data->data));
c9e1498a 837 /* If still not occurred or another handler was scheduled */
18673114 838 if (gpiod_get_value(data->data)
251eb40f
JC
839 || atomic_read(&data->interrupt_handled))
840 return;
841 }
99a0378d 842
251eb40f 843 /* Read the data back from the device */
cc15c7eb
VD
844 val = sht15_read_byte(data);
845 val <<= 8;
412e29c1
VD
846 if (sht15_ack(data))
847 goto wakeup;
cc15c7eb 848 val |= sht15_read_byte(data);
99a0378d 849
82c7465b
JO
850 if (data->checksumming) {
851 /*
852 * Ask the device for a checksum and read it back.
853 * Note: the device sends the checksum byte reversed.
854 */
412e29c1
VD
855 if (sht15_ack(data))
856 goto wakeup;
33836ee9 857 dev_checksum = bitrev8(sht15_read_byte(data));
82c7465b
JO
858 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
859 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
860 checksum_vals[1] = (u8) (val >> 8);
861 checksum_vals[2] = (u8) val;
862 data->checksum_ok
863 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
864 }
865
251eb40f 866 /* Tell the device we are done */
412e29c1
VD
867 if (sht15_end_transmission(data))
868 goto wakeup;
251eb40f 869
99a0378d 870 switch (data->state) {
251eb40f
JC
871 case SHT15_READING_TEMP:
872 data->val_temp = val;
873 break;
874 case SHT15_READING_HUMID:
875 data->val_humid = val;
876 break;
99a0378d
VD
877 default:
878 break;
251eb40f
JC
879 }
880
99a0378d 881 data->state = SHT15_READING_NOTHING;
412e29c1 882wakeup:
251eb40f
JC
883 wake_up(&data->wait_queue);
884}
885
886static void sht15_update_voltage(struct work_struct *work_s)
887{
888 struct sht15_data *data
889 = container_of(work_s, struct sht15_data,
890 update_supply_work);
142c0901 891 data->supply_uv = regulator_get_voltage(data->reg);
251eb40f
JC
892}
893
894/**
895 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
896 * @nb: associated notification structure
897 * @event: voltage regulator state change event code
898 * @ignored: function parameter - ignored here
899 *
900 * Note that as the notification code holds the regulator lock, we have
901 * to schedule an update of the supply voltage rather than getting it directly.
99a0378d 902 */
251eb40f 903static int sht15_invalidate_voltage(struct notifier_block *nb,
99a0378d
VD
904 unsigned long event,
905 void *ignored)
251eb40f
JC
906{
907 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
908
909 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
142c0901 910 data->supply_uv_valid = false;
251eb40f
JC
911 schedule_work(&data->update_supply_work);
912
913 return NOTIFY_OK;
914}
915
2f1736ff
MF
916#ifdef CONFIG_OF
917static const struct of_device_id sht15_dt_match[] = {
918 { .compatible = "sensirion,sht15" },
919 { },
920};
921MODULE_DEVICE_TABLE(of, sht15_dt_match);
2f1736ff
MF
922#endif
923
6c931ae1 924static int sht15_probe(struct platform_device *pdev)
251eb40f 925{
6edf3c30 926 int ret;
38fe7560 927 struct sht15_data *data;
251eb40f 928
38fe7560
GR
929 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
930 if (!data)
931 return -ENOMEM;
251eb40f
JC
932
933 INIT_WORK(&data->read_work, sht15_bh_read_data);
934 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
935 platform_set_drvdata(pdev, data);
936 mutex_init(&data->read_lock);
937 data->dev = &pdev->dev;
938 init_waitqueue_head(&data->wait_queue);
939
99a0378d
VD
940 /*
941 * If a regulator is available,
942 * query what the supply voltage actually is!
943 */
9e059bac 944 data->reg = devm_regulator_get_optional(data->dev, "vcc");
251eb40f 945 if (!IS_ERR(data->reg)) {
c7a78d2c
JD
946 int voltage;
947
948 voltage = regulator_get_voltage(data->reg);
949 if (voltage)
142c0901 950 data->supply_uv = voltage;
c7a78d2c 951
3e78080f
MB
952 ret = regulator_enable(data->reg);
953 if (ret != 0) {
954 dev_err(&pdev->dev,
955 "failed to enable regulator: %d\n", ret);
956 return ret;
957 }
958
99a0378d
VD
959 /*
960 * Setup a notifier block to update this if another device
961 * causes the voltage to change
962 */
251eb40f
JC
963 data->nb.notifier_call = &sht15_invalidate_voltage;
964 ret = regulator_register_notifier(data->reg, &data->nb);
181148ae
VD
965 if (ret) {
966 dev_err(&pdev->dev,
967 "regulator notifier request failed\n");
968 regulator_disable(data->reg);
38fe7560 969 return ret;
181148ae 970 }
251eb40f 971 }
99a0378d
VD
972
973 /* Try requesting the GPIOs */
18673114
LW
974 data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW);
975 if (IS_ERR(data->sck)) {
976 ret = PTR_ERR(data->sck);
412e29c1 977 dev_err(&pdev->dev, "clock line GPIO request failed\n");
181148ae 978 goto err_release_reg;
251eb40f 979 }
18673114
LW
980 data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
981 if (IS_ERR(data->data)) {
982 ret = PTR_ERR(data->data);
412e29c1 983 dev_err(&pdev->dev, "data line GPIO request failed\n");
38fe7560 984 goto err_release_reg;
251eb40f 985 }
251eb40f 986
18673114 987 ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data),
38fe7560
GR
988 sht15_interrupt_fired,
989 IRQF_TRIGGER_FALLING,
990 "sht15 data",
991 data);
251eb40f 992 if (ret) {
99a0378d 993 dev_err(&pdev->dev, "failed to get irq for data line\n");
38fe7560 994 goto err_release_reg;
251eb40f 995 }
18673114 996 disable_irq_nosync(gpiod_to_irq(data->data));
412e29c1
VD
997 ret = sht15_connection_reset(data);
998 if (ret)
999 goto err_release_reg;
181148ae
VD
1000 ret = sht15_soft_reset(data);
1001 if (ret)
38fe7560 1002 goto err_release_reg;
181148ae
VD
1003
1004 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1005 if (ret) {
1006 dev_err(&pdev->dev, "sysfs create failed\n");
38fe7560 1007 goto err_release_reg;
181148ae 1008 }
251eb40f
JC
1009
1010 data->hwmon_dev = hwmon_device_register(data->dev);
1011 if (IS_ERR(data->hwmon_dev)) {
1012 ret = PTR_ERR(data->hwmon_dev);
181148ae 1013 goto err_release_sysfs_group;
251eb40f 1014 }
99a0378d 1015
251eb40f
JC
1016 return 0;
1017
181148ae
VD
1018err_release_sysfs_group:
1019 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
181148ae
VD
1020err_release_reg:
1021 if (!IS_ERR(data->reg)) {
1022 regulator_unregister_notifier(data->reg, &data->nb);
1023 regulator_disable(data->reg);
181148ae 1024 }
251eb40f
JC
1025 return ret;
1026}
1027
281dfd0b 1028static int sht15_remove(struct platform_device *pdev)
251eb40f
JC
1029{
1030 struct sht15_data *data = platform_get_drvdata(pdev);
1031
99a0378d
VD
1032 /*
1033 * Make sure any reads from the device are done and
1034 * prevent new ones beginning
1035 */
251eb40f 1036 mutex_lock(&data->read_lock);
cc15c7eb
VD
1037 if (sht15_soft_reset(data)) {
1038 mutex_unlock(&data->read_lock);
1039 return -EFAULT;
1040 }
251eb40f
JC
1041 hwmon_device_unregister(data->hwmon_dev);
1042 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1043 if (!IS_ERR(data->reg)) {
1044 regulator_unregister_notifier(data->reg, &data->nb);
1045 regulator_disable(data->reg);
251eb40f
JC
1046 }
1047
251eb40f 1048 mutex_unlock(&data->read_lock);
99a0378d 1049
251eb40f
JC
1050 return 0;
1051}
1052
9c40723e 1053static const struct platform_device_id sht15_device_ids[] = {
edec5af7
VD
1054 { "sht10", sht10 },
1055 { "sht11", sht11 },
1056 { "sht15", sht15 },
1057 { "sht71", sht71 },
1058 { "sht75", sht75 },
1059 { }
251eb40f 1060};
edec5af7 1061MODULE_DEVICE_TABLE(platform, sht15_device_ids);
251eb40f 1062
edec5af7
VD
1063static struct platform_driver sht15_driver = {
1064 .driver = {
1065 .name = "sht15",
2f1736ff 1066 .of_match_table = of_match_ptr(sht15_dt_match),
edec5af7
VD
1067 },
1068 .probe = sht15_probe,
9e5e9b7a 1069 .remove = sht15_remove,
edec5af7
VD
1070 .id_table = sht15_device_ids,
1071};
1072module_platform_driver(sht15_driver);
251eb40f
JC
1073
1074MODULE_LICENSE("GPL");
edec5af7 1075MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");