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