sht15.c 27.5 KB
Newer Older
1 2 3
/*
 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
 *
4
 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
5
 *          Jerome Oufella <jerome.oufella@savoirfairelinux.com>
6 7
 *          Vivien Didelot <vivien.didelot@savoirfairelinux.com>
 *
8 9 10 11 12 13 14 15
 * Copyright (c) 2009 Jonathan Cameron
 *
 * Copyright (c) 2007 Wouter Horre
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
16
 * For further information, see the Documentation/hwmon/sht15 file.
17 18 19 20 21 22 23 24 25 26
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
27
#include <linux/sched.h>
28 29 30 31
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>
32
#include <linux/slab.h>
A
Arun Sharma 已提交
33
#include <linux/atomic.h>
Y
yalin wang 已提交
34
#include <linux/bitrev.h>
35 36
#include <linux/gpio/consumer.h>
#include <linux/of.h>
37

38 39 40
/* Commands */
#define SHT15_MEASURE_TEMP		0x03
#define SHT15_MEASURE_RH		0x05
41 42
#define SHT15_WRITE_STATUS		0x06
#define SHT15_READ_STATUS		0x07
43
#define SHT15_SOFT_RESET		0x1E
44

45 46 47 48
/* Min timings */
#define SHT15_TSCKL			100	/* (nsecs) clock low */
#define SHT15_TSCKH			100	/* (nsecs) clock high */
#define SHT15_TSU			150	/* (nsecs) data setup time */
49
#define SHT15_TSRST			11	/* (msecs) soft reset time */
50

51 52 53 54 55 56
/* Status Register Bits */
#define SHT15_STATUS_LOW_RESOLUTION	0x01
#define SHT15_STATUS_NO_OTP_RELOAD	0x02
#define SHT15_STATUS_HEATER		0x04
#define SHT15_STATUS_LOW_BATTERY	0x40

57 58 59
/* List of supported chips */
enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };

60 61 62 63 64 65
/* Actions the driver may be doing */
enum sht15_state {
	SHT15_READING_NOTHING,
	SHT15_READING_TEMP,
	SHT15_READING_HUMID
};
66 67

/**
L
Lucas De Marchi 已提交
68
 * struct sht15_temppair - elements of voltage dependent temp calc
69 70 71 72 73 74 75 76
 * @vdd:	supply voltage in microvolts
 * @d1:		see data sheet
 */
struct sht15_temppair {
	int vdd; /* microvolts */
	int d1;
};

77
/* Table 9 from datasheet - relates temperature calculation to supply voltage */
78 79 80 81 82 83 84 85
static const struct sht15_temppair temppoints[] = {
	{ 2500000, -39400 },
	{ 3000000, -39600 },
	{ 3500000, -39700 },
	{ 4000000, -39800 },
	{ 5000000, -40100 },
};

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* Table from CRC datasheet, section 2.4 */
static const u8 sht15_crc8_table[] = {
	0,	49,	98,	83,	196,	245,	166,	151,
	185,	136,	219,	234,	125,	76,	31,	46,
	67,	114,	33,	16,	135,	182,	229,	212,
	250,	203,	152,	169,	62,	15,	92,	109,
	134,	183,	228,	213,	66,	115,	32,	17,
	63,	14,	93,	108,	251,	202,	153,	168,
	197,	244,	167,	150,	1,	48,	99,	82,
	124,	77,	30,	47,	184,	137,	218,	235,
	61,	12,	95,	110,	249,	200,	155,	170,
	132,	181,	230,	215,	64,	113,	34,	19,
	126,	79,	28,	45,	186,	139,	216,	233,
	199,	246,	165,	148,	3,	50,	97,	80,
	187,	138,	217,	232,	127,	78,	29,	44,
	2,	51,	96,	81,	198,	247,	164,	149,
	248,	201,	154,	171,	60,	13,	94,	111,
	65,	112,	35,	18,	133,	180,	231,	214,
	122,	75,	24,	41,	190,	143,	220,	237,
	195,	242,	161,	144,	7,	54,	101,	84,
	57,	8,	91,	106,	253,	204,	159,	174,
	128,	177,	226,	211,	68,	117,	38,	23,
	252,	205,	158,	175,	56,	9,	90,	107,
	69,	116,	39,	22,	129,	176,	227,	210,
	191,	142,	221,	236,	123,	74,	25,	40,
	6,	55,	100,	85,	194,	243,	160,	145,
	71,	118,	37,	20,	131,	178,	225,	208,
	254,	207,	156,	173,	58,	11,	88,	105,
	4,	53,	102,	87,	192,	241,	162,	147,
	189,	140,	223,	238,	121,	72,	27,	42,
	193,	240,	163,	146,	5,	52,	103,	86,
	120,	73,	26,	43,	188,	141,	222,	239,
	130,	179,	224,	209,	70,	119,	36,	21,
	59,	10,	89,	104,	255,	206,	157,	172
};

122 123
/**
 * struct sht15_data - device instance specific data
124 125
 * @sck:		clock GPIO line
 * @data:		data GPIO line
126 127 128 129
 * @read_work:		bh of interrupt handler.
 * @wait_queue:		wait queue for getting values from device.
 * @val_temp:		last temperature value read from device.
 * @val_humid:		last humidity value read from device.
130
 * @val_status:		last status register value read from device.
131 132
 * @checksum_ok:	last value read from the device passed CRC validation.
 * @checksumming:	flag used to enable the data validation with CRC.
133 134
 * @state:		state identifying the action the driver is doing.
 * @measurements_valid:	are the current stored measures valid (start condition).
135
 * @status_valid:	is the current stored status valid (start condition).
136
 * @last_measurement:	time of last measure.
137
 * @last_status:	time of last status reading.
138 139 140 141 142 143
 * @read_lock:		mutex to ensure only one read in progress at a time.
 * @dev:		associate device structure.
 * @hwmon_dev:		device associated with hwmon subsystem.
 * @reg:		associated regulator (if specified).
 * @nb:			notifier block to handle notifications of voltage
 *                      changes.
V
Vivien Didelot 已提交
144
 * @supply_uv:		local copy of supply voltage used to allow use of
145
 *                      regulator consumer if available.
V
Vivien Didelot 已提交
146
 * @supply_uv_valid:	indicates that an updated value has not yet been
147 148
 *			obtained from the regulator and so any calculations
 *			based upon it will be invalid.
V
Vivien Didelot 已提交
149
 * @update_supply_work:	work struct that is used to update the supply_uv.
150
 * @interrupt_handled:	flag used to indicate a handler has been scheduled.
151 152
 */
struct sht15_data {
153 154
	struct gpio_desc		*sck;
	struct gpio_desc		*data;
155 156 157 158
	struct work_struct		read_work;
	wait_queue_head_t		wait_queue;
	uint16_t			val_temp;
	uint16_t			val_humid;
159
	u8				val_status;
160 161
	bool				checksum_ok;
	bool				checksumming;
162 163
	enum sht15_state		state;
	bool				measurements_valid;
164
	bool				status_valid;
165
	unsigned long			last_measurement;
166
	unsigned long			last_status;
167 168 169 170 171
	struct mutex			read_lock;
	struct device			*dev;
	struct device			*hwmon_dev;
	struct regulator		*reg;
	struct notifier_block		nb;
V
Vivien Didelot 已提交
172 173
	int				supply_uv;
	bool				supply_uv_valid;
174 175 176 177
	struct work_struct		update_supply_work;
	atomic_t			interrupt_handled;
};

178 179 180 181
/**
 * sht15_crc8() - compute crc8
 * @data:	sht15 specific data.
 * @value:	sht15 retrieved data.
182
 * @len:	Length of retrieved data
183 184 185 186 187 188 189
 *
 * This implements section 2 of the CRC datasheet.
 */
static u8 sht15_crc8(struct sht15_data *data,
		const u8 *value,
		int len)
{
Y
yalin wang 已提交
190
	u8 crc = bitrev8(data->val_status & 0x0F);
191 192 193 194 195 196 197 198 199

	while (len--) {
		crc = sht15_crc8_table[*value ^ crc];
		value++;
	}

	return crc;
}

200 201 202 203 204 205
/**
 * sht15_connection_reset() - reset the comms interface
 * @data:	sht15 specific data
 *
 * This implements section 3.4 of the data sheet
 */
206
static int sht15_connection_reset(struct sht15_data *data)
207
{
208
	int i, err;
209

210
	err = gpiod_direction_output(data->data, 1);
211 212
	if (err)
		return err;
213
	ndelay(SHT15_TSCKL);
214
	gpiod_set_value(data->sck, 0);
215 216
	ndelay(SHT15_TSCKL);
	for (i = 0; i < 9; ++i) {
217
		gpiod_set_value(data->sck, 1);
218
		ndelay(SHT15_TSCKH);
219
		gpiod_set_value(data->sck, 0);
220 221
		ndelay(SHT15_TSCKL);
	}
222
	return 0;
223
}
224

225 226 227 228
/**
 * sht15_send_bit() - send an individual bit to the device
 * @data:	device state data
 * @val:	value of bit to be sent
229
 */
230 231
static inline void sht15_send_bit(struct sht15_data *data, int val)
{
232
	gpiod_set_value(data->data, val);
233
	ndelay(SHT15_TSU);
234
	gpiod_set_value(data->sck, 1);
235
	ndelay(SHT15_TSCKH);
236
	gpiod_set_value(data->sck, 0);
237 238 239 240 241 242
	ndelay(SHT15_TSCKL); /* clock low time */
}

/**
 * sht15_transmission_start() - specific sequence for new transmission
 * @data:	device state data
243
 *
244 245 246
 * Timings for this are not documented on the data sheet, so very
 * conservative ones used in implementation. This implements
 * figure 12 on the data sheet.
247
 */
248
static int sht15_transmission_start(struct sht15_data *data)
249
{
250 251
	int err;

252
	/* ensure data is high and output */
253
	err = gpiod_direction_output(data->data, 1);
254 255
	if (err)
		return err;
256
	ndelay(SHT15_TSU);
257
	gpiod_set_value(data->sck, 0);
258
	ndelay(SHT15_TSCKL);
259
	gpiod_set_value(data->sck, 1);
260
	ndelay(SHT15_TSCKH);
261
	gpiod_set_value(data->data, 0);
262
	ndelay(SHT15_TSU);
263
	gpiod_set_value(data->sck, 0);
264
	ndelay(SHT15_TSCKL);
265
	gpiod_set_value(data->sck, 1);
266
	ndelay(SHT15_TSCKH);
267
	gpiod_set_value(data->data, 1);
268
	ndelay(SHT15_TSU);
269
	gpiod_set_value(data->sck, 0);
270
	ndelay(SHT15_TSCKL);
271
	return 0;
272
}
273

274 275 276 277
/**
 * sht15_send_byte() - send a single byte to the device
 * @data:	device state
 * @byte:	value to be sent
278
 */
279 280 281
static void sht15_send_byte(struct sht15_data *data, u8 byte)
{
	int i;
282

283 284 285 286 287
	for (i = 0; i < 8; i++) {
		sht15_send_bit(data, !!(byte & 0x80));
		byte <<= 1;
	}
}
288

289 290 291
/**
 * sht15_wait_for_response() - checks for ack from device
 * @data:	device state
292
 */
293 294
static int sht15_wait_for_response(struct sht15_data *data)
{
295 296
	int err;

297
	err = gpiod_direction_input(data->data);
298 299
	if (err)
		return err;
300
	gpiod_set_value(data->sck, 1);
301
	ndelay(SHT15_TSCKH);
302 303
	if (gpiod_get_value(data->data)) {
		gpiod_set_value(data->sck, 0);
304
		dev_err(data->dev, "Command not acknowledged\n");
305 306 307
		err = sht15_connection_reset(data);
		if (err)
			return err;
308 309
		return -EIO;
	}
310
	gpiod_set_value(data->sck, 0);
311 312 313 314 315 316 317 318 319 320 321
	ndelay(SHT15_TSCKL);
	return 0;
}

/**
 * sht15_send_cmd() - Sends a command to the device.
 * @data:	device state
 * @cmd:	command byte to be sent
 *
 * On entry, sck is output low, data is output pull high
 * and the interrupt disabled.
322
 */
323 324
static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
{
325
	int err;
326

327 328 329
	err = sht15_transmission_start(data);
	if (err)
		return err;
330
	sht15_send_byte(data, cmd);
331
	return sht15_wait_for_response(data);
332
}
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347
/**
 * sht15_soft_reset() - send a soft reset command
 * @data:	sht15 specific data.
 *
 * As described in section 3.2 of the datasheet.
 */
static int sht15_soft_reset(struct sht15_data *data)
{
	int ret;

	ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
	if (ret)
		return ret;
	msleep(SHT15_TSRST);
348 349 350 351 352 353
	/* device resets default hardware status register value */
	data->val_status = 0;

	return ret;
}

354 355 356 357 358 359 360
/**
 * sht15_ack() - send a ack
 * @data:	sht15 specific data.
 *
 * Each byte of data is acknowledged by pulling the data line
 * low for one clock pulse.
 */
361
static int sht15_ack(struct sht15_data *data)
362
{
363 364
	int err;

365
	err = gpiod_direction_output(data->data, 0);
366 367
	if (err)
		return err;
368
	ndelay(SHT15_TSU);
369
	gpiod_set_value(data->sck, 1);
370
	ndelay(SHT15_TSU);
371
	gpiod_set_value(data->sck, 0);
372
	ndelay(SHT15_TSU);
373
	gpiod_set_value(data->data, 1);
374

375
	return gpiod_direction_input(data->data);
376 377
}

378 379 380 381 382 383
/**
 * sht15_end_transmission() - notify device of end of transmission
 * @data:	device state.
 *
 * This is basically a NAK (single clock pulse, data high).
 */
384
static int sht15_end_transmission(struct sht15_data *data)
385
{
386 387
	int err;

388
	err = gpiod_direction_output(data->data, 1);
389 390
	if (err)
		return err;
391
	ndelay(SHT15_TSU);
392
	gpiod_set_value(data->sck, 1);
393
	ndelay(SHT15_TSCKH);
394
	gpiod_set_value(data->sck, 0);
395
	ndelay(SHT15_TSCKL);
396
	return 0;
397 398 399 400 401 402 403 404 405 406 407 408 409
}

/**
 * sht15_read_byte() - Read a byte back from the device
 * @data:	device state.
 */
static u8 sht15_read_byte(struct sht15_data *data)
{
	int i;
	u8 byte = 0;

	for (i = 0; i < 8; ++i) {
		byte <<= 1;
410
		gpiod_set_value(data->sck, 1);
411
		ndelay(SHT15_TSCKH);
412 413
		byte |= !!gpiod_get_value(data->data);
		gpiod_set_value(data->sck, 0);
414 415 416 417 418 419 420 421 422 423 424 425 426 427
		ndelay(SHT15_TSCKL);
	}
	return byte;
}

/**
 * sht15_send_status() - write the status register byte
 * @data:	sht15 specific data.
 * @status:	the byte to set the status register with.
 *
 * As described in figure 14 and table 5 of the datasheet.
 */
static int sht15_send_status(struct sht15_data *data, u8 status)
{
428 429 430 431 432
	int err;

	err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
	if (err)
		return err;
433
	err = gpiod_direction_output(data->data, 1);
434 435
	if (err)
		return err;
436 437
	ndelay(SHT15_TSU);
	sht15_send_byte(data, status);
438 439 440
	err = sht15_wait_for_response(data);
	if (err)
		return err;
441

442
	data->val_status = status;
443 444 445
	return 0;
}

446 447 448 449 450 451 452 453 454 455
/**
 * sht15_update_status() - get updated status register from device if too old
 * @data:	device instance specific data.
 *
 * As described in figure 15 and table 5 of the datasheet.
 */
static int sht15_update_status(struct sht15_data *data)
{
	int ret = 0;
	u8 status;
456 457 458
	u8 previous_config;
	u8 dev_checksum = 0;
	u8 checksum_vals[2];
459 460 461 462 463 464 465
	int timeout = HZ;

	mutex_lock(&data->read_lock);
	if (time_after(jiffies, data->last_status + timeout)
			|| !data->status_valid) {
		ret = sht15_send_cmd(data, SHT15_READ_STATUS);
		if (ret)
466
			goto unlock;
467 468
		status = sht15_read_byte(data);

469 470
		if (data->checksumming) {
			sht15_ack(data);
Y
yalin wang 已提交
471
			dev_checksum = bitrev8(sht15_read_byte(data));
472 473 474 475 476 477
			checksum_vals[0] = SHT15_READ_STATUS;
			checksum_vals[1] = status;
			data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
					== dev_checksum);
		}

478 479 480
		ret = sht15_end_transmission(data);
		if (ret)
			goto unlock;
481

482 483 484 485 486 487 488 489 490
		/*
		 * Perform checksum validation on the received data.
		 * Specification mentions that in case a checksum verification
		 * fails, a soft reset command must be sent to the device.
		 */
		if (data->checksumming && !data->checksum_ok) {
			previous_config = data->val_status & 0x07;
			ret = sht15_soft_reset(data);
			if (ret)
491
				goto unlock;
492 493 494 495 496 497
			if (previous_config) {
				ret = sht15_send_status(data, previous_config);
				if (ret) {
					dev_err(data->dev,
						"CRC validation failed, unable "
						"to restore device settings\n");
498
					goto unlock;
499 500 501
				}
			}
			ret = -EAGAIN;
502
			goto unlock;
503 504
		}

505 506 507 508 509
		data->val_status = status;
		data->status_valid = true;
		data->last_status = jiffies;
	}

510 511
unlock:
	mutex_unlock(&data->read_lock);
512 513 514
	return ret;
}

515
/**
516
 * sht15_measurement() - get a new value from device
517 518 519 520
 * @data:		device instance specific data
 * @command:		command sent to request value
 * @timeout_msecs:	timeout after which comms are assumed
 *			to have failed are reset.
521 522 523 524
 */
static int sht15_measurement(struct sht15_data *data,
			     int command,
			     int timeout_msecs)
525 526
{
	int ret;
527
	u8 previous_config;
528

529 530 531 532
	ret = sht15_send_cmd(data, command);
	if (ret)
		return ret;

533
	ret = gpiod_direction_input(data->data);
534 535
	if (ret)
		return ret;
536 537
	atomic_set(&data->interrupt_handled, 0);

538 539 540
	enable_irq(gpiod_to_irq(data->data));
	if (gpiod_get_value(data->data) == 0) {
		disable_irq_nosync(gpiod_to_irq(data->data));
L
Lucas De Marchi 已提交
541
		/* Only relevant if the interrupt hasn't occurred. */
542 543 544 545
		if (!atomic_read(&data->interrupt_handled))
			schedule_work(&data->read_work);
	}
	ret = wait_event_timeout(data->wait_queue,
546
				 (data->state == SHT15_READING_NOTHING),
547
				 msecs_to_jiffies(timeout_msecs));
548 549 550 551
	if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
		data->state = SHT15_READING_NOTHING;
		return -EIO;
	} else if (ret == 0) { /* timeout occurred */
552
		disable_irq_nosync(gpiod_to_irq(data->data));
553 554 555
		ret = sht15_connection_reset(data);
		if (ret)
			return ret;
556 557
		return -ETIME;
	}
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

	/*
	 *  Perform checksum validation on the received data.
	 *  Specification mentions that in case a checksum verification fails,
	 *  a soft reset command must be sent to the device.
	 */
	if (data->checksumming && !data->checksum_ok) {
		previous_config = data->val_status & 0x07;
		ret = sht15_soft_reset(data);
		if (ret)
			return ret;
		if (previous_config) {
			ret = sht15_send_status(data, previous_config);
			if (ret) {
				dev_err(data->dev,
					"CRC validation failed, unable "
					"to restore device settings\n");
				return ret;
			}
		}
		return -EAGAIN;
	}

581 582 583 584
	return 0;
}

/**
585
 * sht15_update_measurements() - get updated measures from device if too old
586
 * @data:	device state
587 588
 */
static int sht15_update_measurements(struct sht15_data *data)
589 590 591 592 593
{
	int ret = 0;
	int timeout = HZ;

	mutex_lock(&data->read_lock);
594 595 596 597
	if (time_after(jiffies, data->last_measurement + timeout)
	    || !data->measurements_valid) {
		data->state = SHT15_READING_HUMID;
		ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
598
		if (ret)
599
			goto unlock;
600 601
		data->state = SHT15_READING_TEMP;
		ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
602
		if (ret)
603
			goto unlock;
604 605
		data->measurements_valid = true;
		data->last_measurement = jiffies;
606 607
	}

608 609
unlock:
	mutex_unlock(&data->read_lock);
610 611 612 613 614 615 616 617
	return ret;
}

/**
 * sht15_calc_temp() - convert the raw reading to a temperature
 * @data:	device state
 *
 * As per section 4.3 of the data sheet.
618
 */
619 620
static inline int sht15_calc_temp(struct sht15_data *data)
{
621
	int d1 = temppoints[0].d1;
622
	int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
623 624
	int i;

625
	for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
626
		/* Find pointer to interpolate */
V
Vivien Didelot 已提交
627 628
		if (data->supply_uv > temppoints[i - 1].vdd) {
			d1 = (data->supply_uv - temppoints[i - 1].vdd)
629 630 631 632 633 634
				* (temppoints[i].d1 - temppoints[i - 1].d1)
				/ (temppoints[i].vdd - temppoints[i - 1].vdd)
				+ temppoints[i - 1].d1;
			break;
		}

635
	return data->val_temp * d2 + d1;
636 637 638 639 640 641 642 643
}

/**
 * sht15_calc_humid() - using last temperature convert raw to humid
 * @data:	device state
 *
 * This is the temperature compensated version as per section 4.2 of
 * the data sheet.
644 645 646 647
 *
 * The sensor is assumed to be V3, which is compatible with V4.
 * Humidity conversion coefficients are shown in table 7 of the datasheet.
 */
648 649
static inline int sht15_calc_humid(struct sht15_data *data)
{
650
	int rh_linear; /* milli percent */
651
	int temp = sht15_calc_temp(data);
652 653
	int c2, c3;
	int t2;
654
	const int c1 = -4;
655 656 657 658 659 660 661 662 663 664

	if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
		c2 = 648000; /* x 10 ^ -6 */
		c3 = -7200;  /* x 10 ^ -7 */
		t2 = 1280;
	} else {
		c2 = 40500;  /* x 10 ^ -6 */
		c3 = -28;    /* x 10 ^ -7 */
		t2 = 80;
	}
665

666 667
	rh_linear = c1 * 1000
		+ c2 * data->val_humid / 1000
668
		+ (data->val_humid * data->val_humid * c3) / 10000;
669
	return (temp - 25000) * (10000 + t2 * data->val_humid)
670
		/ 1000000 + rh_linear;
671 672
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
/**
 * sht15_show_status() - show status information in sysfs
 * @dev:	device.
 * @attr:	device attribute.
 * @buf:	sysfs buffer where information is written to.
 *
 * Will be called on read access to temp1_fault, humidity1_fault
 * and heater_enable sysfs attributes.
 * Returns number of bytes written into buffer, negative errno on error.
 */
static ssize_t sht15_show_status(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
	int ret;
	struct sht15_data *data = dev_get_drvdata(dev);
	u8 bit = to_sensor_dev_attr(attr)->index;

	ret = sht15_update_status(data);

	return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
}

/**
 * sht15_store_heater() - change heater state via sysfs
 * @dev:	device.
 * @attr:	device attribute.
 * @buf:	sysfs buffer to read the new heater state from.
 * @count:	length of the data.
 *
703
 * Will be called on write access to heater_enable sysfs attribute.
704 705 706 707 708 709 710 711 712 713 714
 * Returns number of bytes actually decoded, negative errno on error.
 */
static ssize_t sht15_store_heater(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	int ret;
	struct sht15_data *data = dev_get_drvdata(dev);
	long value;
	u8 status;

715
	if (kstrtol(buf, 10, &value))
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		return -EINVAL;

	mutex_lock(&data->read_lock);
	status = data->val_status & 0x07;
	if (!!value)
		status |= SHT15_STATUS_HEATER;
	else
		status &= ~SHT15_STATUS_HEATER;

	ret = sht15_send_status(data, status);
	mutex_unlock(&data->read_lock);

	return ret ? ret : count;
}

731 732 733 734 735 736 737 738 739
/**
 * sht15_show_temp() - show temperature measurement value in sysfs
 * @dev:	device.
 * @attr:	device attribute.
 * @buf:	sysfs buffer where measurement values are written to.
 *
 * Will be called on read access to temp1_input sysfs attribute.
 * Returns number of bytes written into buffer, negative errno on error.
 */
740 741 742 743 744 745 746 747
static ssize_t sht15_show_temp(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
	int ret;
	struct sht15_data *data = dev_get_drvdata(dev);

	/* Technically no need to read humidity as well */
748
	ret = sht15_update_measurements(data);
749 750 751 752 753

	return ret ? ret : sprintf(buf, "%d\n",
				   sht15_calc_temp(data));
}

754 755 756 757 758 759 760 761 762
/**
 * sht15_show_humidity() - show humidity measurement value in sysfs
 * @dev:	device.
 * @attr:	device attribute.
 * @buf:	sysfs buffer where measurement values are written to.
 *
 * Will be called on read access to humidity1_input sysfs attribute.
 * Returns number of bytes written into buffer, negative errno on error.
 */
763 764 765 766 767 768 769
static ssize_t sht15_show_humidity(struct device *dev,
				   struct device_attribute *attr,
				   char *buf)
{
	int ret;
	struct sht15_data *data = dev_get_drvdata(dev);

770
	ret = sht15_update_measurements(data);
771 772

	return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
773 774
}

775
static ssize_t name_show(struct device *dev,
776 777 778 779 780 781 782
			 struct device_attribute *attr,
			 char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	return sprintf(buf, "%s\n", pdev->name);
}

783 784 785 786
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
			  sht15_show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
			  sht15_show_humidity, NULL, 0);
787 788 789 790 791 792
static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
			  SHT15_STATUS_LOW_BATTERY);
static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
			  SHT15_STATUS_LOW_BATTERY);
static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
			  sht15_store_heater, SHT15_STATUS_HEATER);
793
static DEVICE_ATTR_RO(name);
794 795 796
static struct attribute *sht15_attrs[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_humidity1_input.dev_attr.attr,
797 798 799
	&sensor_dev_attr_temp1_fault.dev_attr.attr,
	&sensor_dev_attr_humidity1_fault.dev_attr.attr,
	&sensor_dev_attr_heater_enable.dev_attr.attr,
800 801 802 803 804 805 806 807 808 809 810
	&dev_attr_name.attr,
	NULL,
};

static const struct attribute_group sht15_attr_group = {
	.attrs = sht15_attrs,
};

static irqreturn_t sht15_interrupt_fired(int irq, void *d)
{
	struct sht15_data *data = d;
811

812 813 814 815
	/* First disable the interrupt */
	disable_irq_nosync(irq);
	atomic_inc(&data->interrupt_handled);
	/* Then schedule a reading work struct */
816
	if (data->state != SHT15_READING_NOTHING)
817 818 819 820 821 822 823
		schedule_work(&data->read_work);
	return IRQ_HANDLED;
}

static void sht15_bh_read_data(struct work_struct *work_s)
{
	uint16_t val = 0;
824 825
	u8 dev_checksum = 0;
	u8 checksum_vals[3];
826 827 828
	struct sht15_data *data
		= container_of(work_s, struct sht15_data,
			       read_work);
829

830
	/* Firstly, verify the line is low */
831
	if (gpiod_get_value(data->data)) {
832 833 834 835
		/*
		 * If not, then start the interrupt again - care here as could
		 * have gone low in meantime so verify it hasn't!
		 */
836
		atomic_set(&data->interrupt_handled, 0);
837
		enable_irq(gpiod_to_irq(data->data));
838
		/* If still not occurred or another handler was scheduled */
839
		if (gpiod_get_value(data->data)
840 841 842
		    || atomic_read(&data->interrupt_handled))
			return;
	}
843

844
	/* Read the data back from the device */
845 846
	val = sht15_read_byte(data);
	val <<= 8;
847 848
	if (sht15_ack(data))
		goto wakeup;
849
	val |= sht15_read_byte(data);
850

851 852 853 854 855
	if (data->checksumming) {
		/*
		 * Ask the device for a checksum and read it back.
		 * Note: the device sends the checksum byte reversed.
		 */
856 857
		if (sht15_ack(data))
			goto wakeup;
Y
yalin wang 已提交
858
		dev_checksum = bitrev8(sht15_read_byte(data));
859 860 861 862 863 864 865 866
		checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
			SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
		checksum_vals[1] = (u8) (val >> 8);
		checksum_vals[2] = (u8) val;
		data->checksum_ok
			= (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
	}

867
	/* Tell the device we are done */
868 869
	if (sht15_end_transmission(data))
		goto wakeup;
870

871
	switch (data->state) {
872 873 874 875 876 877
	case SHT15_READING_TEMP:
		data->val_temp = val;
		break;
	case SHT15_READING_HUMID:
		data->val_humid = val;
		break;
878 879
	default:
		break;
880 881
	}

882
	data->state = SHT15_READING_NOTHING;
883
wakeup:
884 885 886 887 888 889 890 891
	wake_up(&data->wait_queue);
}

static void sht15_update_voltage(struct work_struct *work_s)
{
	struct sht15_data *data
		= container_of(work_s, struct sht15_data,
			       update_supply_work);
V
Vivien Didelot 已提交
892
	data->supply_uv = regulator_get_voltage(data->reg);
893 894 895 896 897 898 899 900 901 902
}

/**
 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
 * @nb:		associated notification structure
 * @event:	voltage regulator state change event code
 * @ignored:	function parameter - ignored here
 *
 * Note that as the notification code holds the regulator lock, we have
 * to schedule an update of the supply voltage rather than getting it directly.
903
 */
904
static int sht15_invalidate_voltage(struct notifier_block *nb,
905 906
				    unsigned long event,
				    void *ignored)
907 908 909 910
{
	struct sht15_data *data = container_of(nb, struct sht15_data, nb);

	if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
V
Vivien Didelot 已提交
911
		data->supply_uv_valid = false;
912 913 914 915 916
	schedule_work(&data->update_supply_work);

	return NOTIFY_OK;
}

917 918 919 920 921 922 923 924
#ifdef CONFIG_OF
static const struct of_device_id sht15_dt_match[] = {
	{ .compatible = "sensirion,sht15" },
	{ },
};
MODULE_DEVICE_TABLE(of, sht15_dt_match);
#endif

B
Bill Pemberton 已提交
925
static int sht15_probe(struct platform_device *pdev)
926
{
927
	int ret;
928
	struct sht15_data *data;
929

930 931 932
	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;
933 934 935 936 937 938 939 940

	INIT_WORK(&data->read_work, sht15_bh_read_data);
	INIT_WORK(&data->update_supply_work, sht15_update_voltage);
	platform_set_drvdata(pdev, data);
	mutex_init(&data->read_lock);
	data->dev = &pdev->dev;
	init_waitqueue_head(&data->wait_queue);

941 942 943 944
	/*
	 * If a regulator is available,
	 * query what the supply voltage actually is!
	 */
945
	data->reg = devm_regulator_get_optional(data->dev, "vcc");
946
	if (!IS_ERR(data->reg)) {
947 948 949 950
		int voltage;

		voltage = regulator_get_voltage(data->reg);
		if (voltage)
V
Vivien Didelot 已提交
951
			data->supply_uv = voltage;
952

953 954 955 956 957 958 959
		ret = regulator_enable(data->reg);
		if (ret != 0) {
			dev_err(&pdev->dev,
				"failed to enable regulator: %d\n", ret);
			return ret;
		}

960 961 962 963
		/*
		 * Setup a notifier block to update this if another device
		 * causes the voltage to change
		 */
964 965
		data->nb.notifier_call = &sht15_invalidate_voltage;
		ret = regulator_register_notifier(data->reg, &data->nb);
966 967 968 969
		if (ret) {
			dev_err(&pdev->dev,
				"regulator notifier request failed\n");
			regulator_disable(data->reg);
970
			return ret;
971
		}
972
	}
973 974

	/* Try requesting the GPIOs */
975 976 977
	data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW);
	if (IS_ERR(data->sck)) {
		ret = PTR_ERR(data->sck);
978
		dev_err(&pdev->dev, "clock line GPIO request failed\n");
979
		goto err_release_reg;
980
	}
981 982 983
	data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
	if (IS_ERR(data->data)) {
		ret = PTR_ERR(data->data);
984
		dev_err(&pdev->dev, "data line GPIO request failed\n");
985
		goto err_release_reg;
986 987
	}

988
	ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data),
989 990 991 992
			       sht15_interrupt_fired,
			       IRQF_TRIGGER_FALLING,
			       "sht15 data",
			       data);
993
	if (ret) {
994
		dev_err(&pdev->dev, "failed to get irq for data line\n");
995
		goto err_release_reg;
996
	}
997
	disable_irq_nosync(gpiod_to_irq(data->data));
998 999 1000
	ret = sht15_connection_reset(data);
	if (ret)
		goto err_release_reg;
1001 1002
	ret = sht15_soft_reset(data);
	if (ret)
1003
		goto err_release_reg;
1004 1005 1006 1007

	ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
	if (ret) {
		dev_err(&pdev->dev, "sysfs create failed\n");
1008
		goto err_release_reg;
1009
	}
1010 1011 1012 1013

	data->hwmon_dev = hwmon_device_register(data->dev);
	if (IS_ERR(data->hwmon_dev)) {
		ret = PTR_ERR(data->hwmon_dev);
1014
		goto err_release_sysfs_group;
1015
	}
1016

1017 1018
	return 0;

1019 1020 1021 1022 1023 1024 1025
err_release_sysfs_group:
	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
err_release_reg:
	if (!IS_ERR(data->reg)) {
		regulator_unregister_notifier(data->reg, &data->nb);
		regulator_disable(data->reg);
	}
1026 1027 1028
	return ret;
}

B
Bill Pemberton 已提交
1029
static int sht15_remove(struct platform_device *pdev)
1030 1031 1032
{
	struct sht15_data *data = platform_get_drvdata(pdev);

1033 1034 1035 1036
	/*
	 * Make sure any reads from the device are done and
	 * prevent new ones beginning
	 */
1037
	mutex_lock(&data->read_lock);
1038 1039 1040 1041
	if (sht15_soft_reset(data)) {
		mutex_unlock(&data->read_lock);
		return -EFAULT;
	}
1042 1043 1044 1045 1046 1047 1048 1049
	hwmon_device_unregister(data->hwmon_dev);
	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
	if (!IS_ERR(data->reg)) {
		regulator_unregister_notifier(data->reg, &data->nb);
		regulator_disable(data->reg);
	}

	mutex_unlock(&data->read_lock);
1050

1051 1052 1053
	return 0;
}

1054
static const struct platform_device_id sht15_device_ids[] = {
1055 1056 1057 1058 1059 1060
	{ "sht10", sht10 },
	{ "sht11", sht11 },
	{ "sht15", sht15 },
	{ "sht71", sht71 },
	{ "sht75", sht75 },
	{ }
1061
};
1062
MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1063

1064 1065 1066
static struct platform_driver sht15_driver = {
	.driver = {
		.name = "sht15",
1067
		.of_match_table = of_match_ptr(sht15_dt_match),
1068 1069
	},
	.probe = sht15_probe,
B
Bill Pemberton 已提交
1070
	.remove = sht15_remove,
1071 1072 1073
	.id_table = sht15_device_ids,
};
module_platform_driver(sht15_driver);
1074 1075

MODULE_LICENSE("GPL");
1076
MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");