2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
4 * Copyright (c) 2009 Jonathan Cameron
6 * Copyright (c) 2007 Wouter Horre
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Currently ignoring checksum on readings.
13 * Default resolution only (14bit temp, 12bit humidity)
14 * Ignoring battery status.
16 * Timings are all conservative.
18 * Data sheet available (1/2009) at
19 * http://www.sensirion.ch/en/pdf/product_information/Datasheet-humidity-sensor-SHT1x.pdf
21 * Regulator supply name = vcc
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/mutex.h>
32 #include <linux/platform_device.h>
33 #include <linux/sched.h>
34 #include <linux/delay.h>
35 #include <linux/jiffies.h>
36 #include <linux/err.h>
37 #include <linux/sht15.h>
38 #include <linux/regulator/consumer.h>
39 #include <asm/atomic.h>
41 #define SHT15_MEASURE_TEMP 3
42 #define SHT15_MEASURE_RH 5
44 #define SHT15_READING_NOTHING 0
45 #define SHT15_READING_TEMP 1
46 #define SHT15_READING_HUMID 2
48 /* Min timings in nsecs */
49 #define SHT15_TSCKL 100 /* clock low */
50 #define SHT15_TSCKH 100 /* clock high */
51 #define SHT15_TSU 150 /* data setup time */
54 * struct sht15_temppair - elements of voltage dependant temp calc
55 * @vdd: supply voltage in microvolts
58 struct sht15_temppair
{
59 int vdd
; /* microvolts */
63 /* Table 9 from data sheet - relates temperature calculation
66 static const struct sht15_temppair temppoints
[] = {
75 * struct sht15_data - device instance specific data
76 * @pdata: platform data (gpio's etc)
77 * @read_work: bh of interrupt handler
78 * @wait_queue: wait queue for getting values from device
79 * @val_temp: last temperature value read from device
80 * @val_humid: last humidity value read from device
81 * @flag: status flag used to identify what the last request was
82 * @valid: are the current stored values valid (start condition)
83 * @last_updat: time of last update
84 * @read_lock: mutex to ensure only one read in progress
86 * @dev: associate device structure
87 * @hwmon_dev: device associated with hwmon subsystem
88 * @reg: associated regulator (if specified)
89 * @nb: notifier block to handle notifications of voltage changes
90 * @supply_uV: local copy of supply voltage used to allow
91 * use of regulator consumer if available
92 * @supply_uV_valid: indicates that an updated value has not yet
93 * been obtained from the regulator and so any calculations
94 * based upon it will be invalid.
95 * @update_supply_work: work struct that is used to update the supply_uV
96 * @interrupt_handled: flag used to indicate a hander has been scheduled
99 struct sht15_platform_data
*pdata
;
100 struct work_struct read_work
;
101 wait_queue_head_t wait_queue
;
106 unsigned long last_updat
;
107 struct mutex read_lock
;
109 struct device
*hwmon_dev
;
110 struct regulator
*reg
;
111 struct notifier_block nb
;
114 struct work_struct update_supply_work
;
115 atomic_t interrupt_handled
;
119 * sht15_connection_reset() - reset the comms interface
120 * @data: sht15 specific data
122 * This implements section 3.4 of the data sheet
124 static void sht15_connection_reset(struct sht15_data
*data
)
127 gpio_direction_output(data
->pdata
->gpio_data
, 1);
129 gpio_set_value(data
->pdata
->gpio_sck
, 0);
131 for (i
= 0; i
< 9; ++i
) {
132 gpio_set_value(data
->pdata
->gpio_sck
, 1);
134 gpio_set_value(data
->pdata
->gpio_sck
, 0);
139 * sht15_send_bit() - send an individual bit to the device
140 * @data: device state data
141 * @val: value of bit to be sent
143 static inline void sht15_send_bit(struct sht15_data
*data
, int val
)
146 gpio_set_value(data
->pdata
->gpio_data
, val
);
148 gpio_set_value(data
->pdata
->gpio_sck
, 1);
150 gpio_set_value(data
->pdata
->gpio_sck
, 0);
151 ndelay(SHT15_TSCKL
); /* clock low time */
155 * sht15_transmission_start() - specific sequence for new transmission
157 * @data: device state data
158 * Timings for this are not documented on the data sheet, so very
159 * conservative ones used in implementation. This implements
160 * figure 12 on the data sheet.
162 static void sht15_transmission_start(struct sht15_data
*data
)
164 /* ensure data is high and output */
165 gpio_direction_output(data
->pdata
->gpio_data
, 1);
167 gpio_set_value(data
->pdata
->gpio_sck
, 0);
169 gpio_set_value(data
->pdata
->gpio_sck
, 1);
171 gpio_set_value(data
->pdata
->gpio_data
, 0);
173 gpio_set_value(data
->pdata
->gpio_sck
, 0);
175 gpio_set_value(data
->pdata
->gpio_sck
, 1);
177 gpio_set_value(data
->pdata
->gpio_data
, 1);
179 gpio_set_value(data
->pdata
->gpio_sck
, 0);
183 * sht15_send_byte() - send a single byte to the device
184 * @data: device state
185 * @byte: value to be sent
187 static void sht15_send_byte(struct sht15_data
*data
, u8 byte
)
190 for (i
= 0; i
< 8; i
++) {
191 sht15_send_bit(data
, !!(byte
& 0x80));
196 * sht15_wait_for_response() - checks for ack from device
197 * @data: device state
199 static int sht15_wait_for_response(struct sht15_data
*data
)
201 gpio_direction_input(data
->pdata
->gpio_data
);
202 gpio_set_value(data
->pdata
->gpio_sck
, 1);
204 if (gpio_get_value(data
->pdata
->gpio_data
)) {
205 gpio_set_value(data
->pdata
->gpio_sck
, 0);
206 dev_err(data
->dev
, "Command not acknowledged\n");
207 sht15_connection_reset(data
);
210 gpio_set_value(data
->pdata
->gpio_sck
, 0);
216 * sht15_send_cmd() - Sends a command to the device.
217 * @data: device state
218 * @cmd: command byte to be sent
220 * On entry, sck is output low, data is output pull high
221 * and the interrupt disabled.
223 static int sht15_send_cmd(struct sht15_data
*data
, u8 cmd
)
226 sht15_transmission_start(data
);
227 sht15_send_byte(data
, cmd
);
228 ret
= sht15_wait_for_response(data
);
232 * sht15_update_single_val() - get a new value from device
233 * @data: device instance specific data
234 * @command: command sent to request value
235 * @timeout_msecs: timeout after which comms are assumed
236 * to have failed are reset.
238 static inline int sht15_update_single_val(struct sht15_data
*data
,
243 ret
= sht15_send_cmd(data
, command
);
247 gpio_direction_input(data
->pdata
->gpio_data
);
248 atomic_set(&data
->interrupt_handled
, 0);
250 enable_irq(gpio_to_irq(data
->pdata
->gpio_data
));
251 if (gpio_get_value(data
->pdata
->gpio_data
) == 0) {
252 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
253 /* Only relevant if the interrupt hasn't occured. */
254 if (!atomic_read(&data
->interrupt_handled
))
255 schedule_work(&data
->read_work
);
257 ret
= wait_event_timeout(data
->wait_queue
,
258 (data
->flag
== SHT15_READING_NOTHING
),
259 msecs_to_jiffies(timeout_msecs
));
260 if (ret
== 0) {/* timeout occurred */
261 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
262 sht15_connection_reset(data
);
269 * sht15_update_vals() - get updated readings from device if too old
270 * @data: device state
272 static int sht15_update_vals(struct sht15_data
*data
)
277 mutex_lock(&data
->read_lock
);
278 if (time_after(jiffies
, data
->last_updat
+ timeout
)
280 data
->flag
= SHT15_READING_HUMID
;
281 ret
= sht15_update_single_val(data
, SHT15_MEASURE_RH
, 160);
284 data
->flag
= SHT15_READING_TEMP
;
285 ret
= sht15_update_single_val(data
, SHT15_MEASURE_TEMP
, 400);
289 data
->last_updat
= jiffies
;
292 mutex_unlock(&data
->read_lock
);
298 * sht15_calc_temp() - convert the raw reading to a temperature
299 * @data: device state
301 * As per section 4.3 of the data sheet.
303 static inline int sht15_calc_temp(struct sht15_data
*data
)
308 for (i
= 1; i
< ARRAY_SIZE(temppoints
); i
++)
309 /* Find pointer to interpolate */
310 if (data
->supply_uV
> temppoints
[i
- 1].vdd
) {
311 d1
= (data
->supply_uV
/1000 - temppoints
[i
- 1].vdd
)
312 * (temppoints
[i
].d1
- temppoints
[i
- 1].d1
)
313 / (temppoints
[i
].vdd
- temppoints
[i
- 1].vdd
)
314 + temppoints
[i
- 1].d1
;
318 return data
->val_temp
*10 + d1
;
322 * sht15_calc_humid() - using last temperature convert raw to humid
323 * @data: device state
325 * This is the temperature compensated version as per section 4.2 of
328 static inline int sht15_calc_humid(struct sht15_data
*data
)
330 int RHlinear
; /* milli percent */
331 int temp
= sht15_calc_temp(data
);
334 const int c2
= 40500; /* x 10 ^ -6 */
335 const int c3
= -2800; /* x10 ^ -9 */
338 + c2
* data
->val_humid
/1000
339 + (data
->val_humid
* data
->val_humid
* c3
)/1000000;
340 return (temp
- 25000) * (10000 + 80 * data
->val_humid
)
341 / 1000000 + RHlinear
;
344 static ssize_t
sht15_show_temp(struct device
*dev
,
345 struct device_attribute
*attr
,
349 struct sht15_data
*data
= dev_get_drvdata(dev
);
351 /* Technically no need to read humidity as well */
352 ret
= sht15_update_vals(data
);
354 return ret
? ret
: sprintf(buf
, "%d\n",
355 sht15_calc_temp(data
));
358 static ssize_t
sht15_show_humidity(struct device
*dev
,
359 struct device_attribute
*attr
,
363 struct sht15_data
*data
= dev_get_drvdata(dev
);
365 ret
= sht15_update_vals(data
);
367 return ret
? ret
: sprintf(buf
, "%d\n", sht15_calc_humid(data
));
370 static ssize_t
show_name(struct device
*dev
,
371 struct device_attribute
*attr
,
374 struct platform_device
*pdev
= to_platform_device(dev
);
375 return sprintf(buf
, "%s\n", pdev
->name
);
378 static SENSOR_DEVICE_ATTR(temp1_input
,
379 S_IRUGO
, sht15_show_temp
,
381 static SENSOR_DEVICE_ATTR(humidity1_input
,
382 S_IRUGO
, sht15_show_humidity
,
384 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
385 static struct attribute
*sht15_attrs
[] = {
386 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
387 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
392 static const struct attribute_group sht15_attr_group
= {
393 .attrs
= sht15_attrs
,
396 static irqreturn_t
sht15_interrupt_fired(int irq
, void *d
)
398 struct sht15_data
*data
= d
;
399 /* First disable the interrupt */
400 disable_irq_nosync(irq
);
401 atomic_inc(&data
->interrupt_handled
);
402 /* Then schedule a reading work struct */
403 if (data
->flag
!= SHT15_READING_NOTHING
)
404 schedule_work(&data
->read_work
);
408 /* Each byte of data is acknowledged by pulling the data line
409 * low for one clock pulse.
411 static void sht15_ack(struct sht15_data
*data
)
413 gpio_direction_output(data
->pdata
->gpio_data
, 0);
415 gpio_set_value(data
->pdata
->gpio_sck
, 1);
417 gpio_set_value(data
->pdata
->gpio_sck
, 0);
419 gpio_set_value(data
->pdata
->gpio_data
, 1);
421 gpio_direction_input(data
->pdata
->gpio_data
);
424 * sht15_end_transmission() - notify device of end of transmission
425 * @data: device state
427 * This is basically a NAK. (single clock pulse, data high)
429 static void sht15_end_transmission(struct sht15_data
*data
)
431 gpio_direction_output(data
->pdata
->gpio_data
, 1);
433 gpio_set_value(data
->pdata
->gpio_sck
, 1);
435 gpio_set_value(data
->pdata
->gpio_sck
, 0);
439 static void sht15_bh_read_data(struct work_struct
*work_s
)
443 struct sht15_data
*data
444 = container_of(work_s
, struct sht15_data
,
446 /* Firstly, verify the line is low */
447 if (gpio_get_value(data
->pdata
->gpio_data
)) {
448 /* If not, then start the interrupt again - care
449 here as could have gone low in meantime so verify
452 atomic_set(&data
->interrupt_handled
, 0);
453 enable_irq(gpio_to_irq(data
->pdata
->gpio_data
));
454 /* If still not occured or another handler has been scheduled */
455 if (gpio_get_value(data
->pdata
->gpio_data
)
456 || atomic_read(&data
->interrupt_handled
))
459 /* Read the data back from the device */
460 for (i
= 0; i
< 16; ++i
) {
462 gpio_set_value(data
->pdata
->gpio_sck
, 1);
464 val
|= !!gpio_get_value(data
->pdata
->gpio_data
);
465 gpio_set_value(data
->pdata
->gpio_sck
, 0);
470 /* Tell the device we are done */
471 sht15_end_transmission(data
);
473 switch (data
->flag
) {
474 case SHT15_READING_TEMP
:
475 data
->val_temp
= val
;
477 case SHT15_READING_HUMID
:
478 data
->val_humid
= val
;
482 data
->flag
= SHT15_READING_NOTHING
;
483 wake_up(&data
->wait_queue
);
486 static void sht15_update_voltage(struct work_struct
*work_s
)
488 struct sht15_data
*data
489 = container_of(work_s
, struct sht15_data
,
491 data
->supply_uV
= regulator_get_voltage(data
->reg
);
495 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
496 * @nb: associated notification structure
497 * @event: voltage regulator state change event code
498 * @ignored: function parameter - ignored here
500 * Note that as the notification code holds the regulator lock, we have
501 * to schedule an update of the supply voltage rather than getting it directly.
503 static int sht15_invalidate_voltage(struct notifier_block
*nb
,
507 struct sht15_data
*data
= container_of(nb
, struct sht15_data
, nb
);
509 if (event
== REGULATOR_EVENT_VOLTAGE_CHANGE
)
510 data
->supply_uV_valid
= false;
511 schedule_work(&data
->update_supply_work
);
516 static int __devinit
sht15_probe(struct platform_device
*pdev
)
519 struct sht15_data
*data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
523 dev_err(&pdev
->dev
, "kzalloc failed");
527 INIT_WORK(&data
->read_work
, sht15_bh_read_data
);
528 INIT_WORK(&data
->update_supply_work
, sht15_update_voltage
);
529 platform_set_drvdata(pdev
, data
);
530 mutex_init(&data
->read_lock
);
531 data
->dev
= &pdev
->dev
;
532 init_waitqueue_head(&data
->wait_queue
);
534 if (pdev
->dev
.platform_data
== NULL
) {
535 dev_err(&pdev
->dev
, "no platform data supplied");
538 data
->pdata
= pdev
->dev
.platform_data
;
539 data
->supply_uV
= data
->pdata
->supply_mv
*1000;
541 /* If a regulator is available, query what the supply voltage actually is!*/
542 data
->reg
= regulator_get(data
->dev
, "vcc");
543 if (!IS_ERR(data
->reg
)) {
544 data
->supply_uV
= regulator_get_voltage(data
->reg
);
545 regulator_enable(data
->reg
);
546 /* setup a notifier block to update this if another device
547 * causes the voltage to change */
548 data
->nb
.notifier_call
= &sht15_invalidate_voltage
;
549 ret
= regulator_register_notifier(data
->reg
, &data
->nb
);
551 /* Try requesting the GPIOs */
552 ret
= gpio_request(data
->pdata
->gpio_sck
, "SHT15 sck");
554 dev_err(&pdev
->dev
, "gpio request failed");
557 gpio_direction_output(data
->pdata
->gpio_sck
, 0);
558 ret
= gpio_request(data
->pdata
->gpio_data
, "SHT15 data");
560 dev_err(&pdev
->dev
, "gpio request failed");
561 goto err_release_gpio_sck
;
563 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &sht15_attr_group
);
565 dev_err(&pdev
->dev
, "sysfs create failed");
566 goto err_release_gpio_data
;
569 ret
= request_irq(gpio_to_irq(data
->pdata
->gpio_data
),
570 sht15_interrupt_fired
,
571 IRQF_TRIGGER_FALLING
,
575 dev_err(&pdev
->dev
, "failed to get irq for data line");
576 goto err_release_gpio_data
;
578 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
579 sht15_connection_reset(data
);
580 sht15_send_cmd(data
, 0x1E);
582 data
->hwmon_dev
= hwmon_device_register(data
->dev
);
583 if (IS_ERR(data
->hwmon_dev
)) {
584 ret
= PTR_ERR(data
->hwmon_dev
);
585 goto err_release_irq
;
590 free_irq(gpio_to_irq(data
->pdata
->gpio_data
), data
);
591 err_release_gpio_data
:
592 gpio_free(data
->pdata
->gpio_data
);
593 err_release_gpio_sck
:
594 gpio_free(data
->pdata
->gpio_sck
);
602 static int __devexit
sht15_remove(struct platform_device
*pdev
)
604 struct sht15_data
*data
= platform_get_drvdata(pdev
);
606 /* Make sure any reads from the device are done and
607 * prevent new ones beginnning */
608 mutex_lock(&data
->read_lock
);
609 hwmon_device_unregister(data
->hwmon_dev
);
610 sysfs_remove_group(&pdev
->dev
.kobj
, &sht15_attr_group
);
611 if (!IS_ERR(data
->reg
)) {
612 regulator_unregister_notifier(data
->reg
, &data
->nb
);
613 regulator_disable(data
->reg
);
614 regulator_put(data
->reg
);
617 free_irq(gpio_to_irq(data
->pdata
->gpio_data
), data
);
618 gpio_free(data
->pdata
->gpio_data
);
619 gpio_free(data
->pdata
->gpio_sck
);
620 mutex_unlock(&data
->read_lock
);
627 * sht_drivers simultaneously refers to __devinit and __devexit function
628 * which causes spurious section mismatch warning. So use __refdata to
631 static struct platform_driver __refdata sht_drivers
[] = {
635 .owner
= THIS_MODULE
,
637 .probe
= sht15_probe
,
638 .remove
= __devexit_p(sht15_remove
),
642 .owner
= THIS_MODULE
,
644 .probe
= sht15_probe
,
645 .remove
= __devexit_p(sht15_remove
),
649 .owner
= THIS_MODULE
,
651 .probe
= sht15_probe
,
652 .remove
= __devexit_p(sht15_remove
),
656 .owner
= THIS_MODULE
,
658 .probe
= sht15_probe
,
659 .remove
= __devexit_p(sht15_remove
),
663 .owner
= THIS_MODULE
,
665 .probe
= sht15_probe
,
666 .remove
= __devexit_p(sht15_remove
),
671 static int __init
sht15_init(void)
676 for (i
= 0; i
< ARRAY_SIZE(sht_drivers
); i
++) {
677 ret
= platform_driver_register(&sht_drivers
[i
]);
686 platform_driver_unregister(&sht_drivers
[i
]);
690 module_init(sht15_init
);
692 static void __exit
sht15_exit(void)
695 for (i
= ARRAY_SIZE(sht_drivers
) - 1; i
>= 0; i
--)
696 platform_driver_unregister(&sht_drivers
[i
]);
698 module_exit(sht15_exit
);
700 MODULE_LICENSE("GPL");