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 <linux/slab.h>
40 #include <asm/atomic.h>
42 #define SHT15_MEASURE_TEMP 3
43 #define SHT15_MEASURE_RH 5
45 #define SHT15_READING_NOTHING 0
46 #define SHT15_READING_TEMP 1
47 #define SHT15_READING_HUMID 2
49 /* Min timings in nsecs */
50 #define SHT15_TSCKL 100 /* clock low */
51 #define SHT15_TSCKH 100 /* clock high */
52 #define SHT15_TSU 150 /* data setup time */
55 * struct sht15_temppair - elements of voltage dependant temp calc
56 * @vdd: supply voltage in microvolts
59 struct sht15_temppair
{
60 int vdd
; /* microvolts */
64 /* Table 9 from data sheet - relates temperature calculation
67 static const struct sht15_temppair temppoints
[] = {
76 * struct sht15_data - device instance specific data
77 * @pdata: platform data (gpio's etc)
78 * @read_work: bh of interrupt handler
79 * @wait_queue: wait queue for getting values from device
80 * @val_temp: last temperature value read from device
81 * @val_humid: last humidity value read from device
82 * @flag: status flag used to identify what the last request was
83 * @valid: are the current stored values valid (start condition)
84 * @last_updat: time of last update
85 * @read_lock: mutex to ensure only one read in progress
87 * @dev: associate device structure
88 * @hwmon_dev: device associated with hwmon subsystem
89 * @reg: associated regulator (if specified)
90 * @nb: notifier block to handle notifications of voltage changes
91 * @supply_uV: local copy of supply voltage used to allow
92 * use of regulator consumer if available
93 * @supply_uV_valid: indicates that an updated value has not yet
94 * been obtained from the regulator and so any calculations
95 * based upon it will be invalid.
96 * @update_supply_work: work struct that is used to update the supply_uV
97 * @interrupt_handled: flag used to indicate a hander has been scheduled
100 struct sht15_platform_data
*pdata
;
101 struct work_struct read_work
;
102 wait_queue_head_t wait_queue
;
107 unsigned long last_updat
;
108 struct mutex read_lock
;
110 struct device
*hwmon_dev
;
111 struct regulator
*reg
;
112 struct notifier_block nb
;
115 struct work_struct update_supply_work
;
116 atomic_t interrupt_handled
;
120 * sht15_connection_reset() - reset the comms interface
121 * @data: sht15 specific data
123 * This implements section 3.4 of the data sheet
125 static void sht15_connection_reset(struct sht15_data
*data
)
128 gpio_direction_output(data
->pdata
->gpio_data
, 1);
130 gpio_set_value(data
->pdata
->gpio_sck
, 0);
132 for (i
= 0; i
< 9; ++i
) {
133 gpio_set_value(data
->pdata
->gpio_sck
, 1);
135 gpio_set_value(data
->pdata
->gpio_sck
, 0);
140 * sht15_send_bit() - send an individual bit to the device
141 * @data: device state data
142 * @val: value of bit to be sent
144 static inline void sht15_send_bit(struct sht15_data
*data
, int val
)
147 gpio_set_value(data
->pdata
->gpio_data
, val
);
149 gpio_set_value(data
->pdata
->gpio_sck
, 1);
151 gpio_set_value(data
->pdata
->gpio_sck
, 0);
152 ndelay(SHT15_TSCKL
); /* clock low time */
156 * sht15_transmission_start() - specific sequence for new transmission
158 * @data: device state data
159 * Timings for this are not documented on the data sheet, so very
160 * conservative ones used in implementation. This implements
161 * figure 12 on the data sheet.
163 static void sht15_transmission_start(struct sht15_data
*data
)
165 /* ensure data is high and output */
166 gpio_direction_output(data
->pdata
->gpio_data
, 1);
168 gpio_set_value(data
->pdata
->gpio_sck
, 0);
170 gpio_set_value(data
->pdata
->gpio_sck
, 1);
172 gpio_set_value(data
->pdata
->gpio_data
, 0);
174 gpio_set_value(data
->pdata
->gpio_sck
, 0);
176 gpio_set_value(data
->pdata
->gpio_sck
, 1);
178 gpio_set_value(data
->pdata
->gpio_data
, 1);
180 gpio_set_value(data
->pdata
->gpio_sck
, 0);
184 * sht15_send_byte() - send a single byte to the device
185 * @data: device state
186 * @byte: value to be sent
188 static void sht15_send_byte(struct sht15_data
*data
, u8 byte
)
191 for (i
= 0; i
< 8; i
++) {
192 sht15_send_bit(data
, !!(byte
& 0x80));
197 * sht15_wait_for_response() - checks for ack from device
198 * @data: device state
200 static int sht15_wait_for_response(struct sht15_data
*data
)
202 gpio_direction_input(data
->pdata
->gpio_data
);
203 gpio_set_value(data
->pdata
->gpio_sck
, 1);
205 if (gpio_get_value(data
->pdata
->gpio_data
)) {
206 gpio_set_value(data
->pdata
->gpio_sck
, 0);
207 dev_err(data
->dev
, "Command not acknowledged\n");
208 sht15_connection_reset(data
);
211 gpio_set_value(data
->pdata
->gpio_sck
, 0);
217 * sht15_send_cmd() - Sends a command to the device.
218 * @data: device state
219 * @cmd: command byte to be sent
221 * On entry, sck is output low, data is output pull high
222 * and the interrupt disabled.
224 static int sht15_send_cmd(struct sht15_data
*data
, u8 cmd
)
227 sht15_transmission_start(data
);
228 sht15_send_byte(data
, cmd
);
229 ret
= sht15_wait_for_response(data
);
233 * sht15_update_single_val() - get a new value from device
234 * @data: device instance specific data
235 * @command: command sent to request value
236 * @timeout_msecs: timeout after which comms are assumed
237 * to have failed are reset.
239 static inline int sht15_update_single_val(struct sht15_data
*data
,
244 ret
= sht15_send_cmd(data
, command
);
248 gpio_direction_input(data
->pdata
->gpio_data
);
249 atomic_set(&data
->interrupt_handled
, 0);
251 enable_irq(gpio_to_irq(data
->pdata
->gpio_data
));
252 if (gpio_get_value(data
->pdata
->gpio_data
) == 0) {
253 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
254 /* Only relevant if the interrupt hasn't occured. */
255 if (!atomic_read(&data
->interrupt_handled
))
256 schedule_work(&data
->read_work
);
258 ret
= wait_event_timeout(data
->wait_queue
,
259 (data
->flag
== SHT15_READING_NOTHING
),
260 msecs_to_jiffies(timeout_msecs
));
261 if (ret
== 0) {/* timeout occurred */
262 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
263 sht15_connection_reset(data
);
270 * sht15_update_vals() - get updated readings from device if too old
271 * @data: device state
273 static int sht15_update_vals(struct sht15_data
*data
)
278 mutex_lock(&data
->read_lock
);
279 if (time_after(jiffies
, data
->last_updat
+ timeout
)
281 data
->flag
= SHT15_READING_HUMID
;
282 ret
= sht15_update_single_val(data
, SHT15_MEASURE_RH
, 160);
285 data
->flag
= SHT15_READING_TEMP
;
286 ret
= sht15_update_single_val(data
, SHT15_MEASURE_TEMP
, 400);
290 data
->last_updat
= jiffies
;
293 mutex_unlock(&data
->read_lock
);
299 * sht15_calc_temp() - convert the raw reading to a temperature
300 * @data: device state
302 * As per section 4.3 of the data sheet.
304 static inline int sht15_calc_temp(struct sht15_data
*data
)
306 int d1
= temppoints
[0].d1
;
309 for (i
= ARRAY_SIZE(temppoints
) - 1; i
> 0; i
--)
310 /* Find pointer to interpolate */
311 if (data
->supply_uV
> temppoints
[i
- 1].vdd
) {
312 d1
= (data
->supply_uV
- temppoints
[i
- 1].vdd
)
313 * (temppoints
[i
].d1
- temppoints
[i
- 1].d1
)
314 / (temppoints
[i
].vdd
- temppoints
[i
- 1].vdd
)
315 + temppoints
[i
- 1].d1
;
319 return data
->val_temp
*10 + d1
;
323 * sht15_calc_humid() - using last temperature convert raw to humid
324 * @data: device state
326 * This is the temperature compensated version as per section 4.2 of
329 static inline int sht15_calc_humid(struct sht15_data
*data
)
331 int RHlinear
; /* milli percent */
332 int temp
= sht15_calc_temp(data
);
335 const int c2
= 40500; /* x 10 ^ -6 */
336 const int c3
= -2800; /* x10 ^ -9 */
339 + c2
* data
->val_humid
/1000
340 + (data
->val_humid
* data
->val_humid
* c3
)/1000000;
341 return (temp
- 25000) * (10000 + 80 * data
->val_humid
)
342 / 1000000 + RHlinear
;
345 static ssize_t
sht15_show_temp(struct device
*dev
,
346 struct device_attribute
*attr
,
350 struct sht15_data
*data
= dev_get_drvdata(dev
);
352 /* Technically no need to read humidity as well */
353 ret
= sht15_update_vals(data
);
355 return ret
? ret
: sprintf(buf
, "%d\n",
356 sht15_calc_temp(data
));
359 static ssize_t
sht15_show_humidity(struct device
*dev
,
360 struct device_attribute
*attr
,
364 struct sht15_data
*data
= dev_get_drvdata(dev
);
366 ret
= sht15_update_vals(data
);
368 return ret
? ret
: sprintf(buf
, "%d\n", sht15_calc_humid(data
));
371 static ssize_t
show_name(struct device
*dev
,
372 struct device_attribute
*attr
,
375 struct platform_device
*pdev
= to_platform_device(dev
);
376 return sprintf(buf
, "%s\n", pdev
->name
);
379 static SENSOR_DEVICE_ATTR(temp1_input
,
380 S_IRUGO
, sht15_show_temp
,
382 static SENSOR_DEVICE_ATTR(humidity1_input
,
383 S_IRUGO
, sht15_show_humidity
,
385 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
386 static struct attribute
*sht15_attrs
[] = {
387 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
388 &sensor_dev_attr_humidity1_input
.dev_attr
.attr
,
393 static const struct attribute_group sht15_attr_group
= {
394 .attrs
= sht15_attrs
,
397 static irqreturn_t
sht15_interrupt_fired(int irq
, void *d
)
399 struct sht15_data
*data
= d
;
400 /* First disable the interrupt */
401 disable_irq_nosync(irq
);
402 atomic_inc(&data
->interrupt_handled
);
403 /* Then schedule a reading work struct */
404 if (data
->flag
!= SHT15_READING_NOTHING
)
405 schedule_work(&data
->read_work
);
409 /* Each byte of data is acknowledged by pulling the data line
410 * low for one clock pulse.
412 static void sht15_ack(struct sht15_data
*data
)
414 gpio_direction_output(data
->pdata
->gpio_data
, 0);
416 gpio_set_value(data
->pdata
->gpio_sck
, 1);
418 gpio_set_value(data
->pdata
->gpio_sck
, 0);
420 gpio_set_value(data
->pdata
->gpio_data
, 1);
422 gpio_direction_input(data
->pdata
->gpio_data
);
425 * sht15_end_transmission() - notify device of end of transmission
426 * @data: device state
428 * This is basically a NAK. (single clock pulse, data high)
430 static void sht15_end_transmission(struct sht15_data
*data
)
432 gpio_direction_output(data
->pdata
->gpio_data
, 1);
434 gpio_set_value(data
->pdata
->gpio_sck
, 1);
436 gpio_set_value(data
->pdata
->gpio_sck
, 0);
440 static void sht15_bh_read_data(struct work_struct
*work_s
)
444 struct sht15_data
*data
445 = container_of(work_s
, struct sht15_data
,
447 /* Firstly, verify the line is low */
448 if (gpio_get_value(data
->pdata
->gpio_data
)) {
449 /* If not, then start the interrupt again - care
450 here as could have gone low in meantime so verify
453 atomic_set(&data
->interrupt_handled
, 0);
454 enable_irq(gpio_to_irq(data
->pdata
->gpio_data
));
455 /* If still not occured or another handler has been scheduled */
456 if (gpio_get_value(data
->pdata
->gpio_data
)
457 || atomic_read(&data
->interrupt_handled
))
460 /* Read the data back from the device */
461 for (i
= 0; i
< 16; ++i
) {
463 gpio_set_value(data
->pdata
->gpio_sck
, 1);
465 val
|= !!gpio_get_value(data
->pdata
->gpio_data
);
466 gpio_set_value(data
->pdata
->gpio_sck
, 0);
471 /* Tell the device we are done */
472 sht15_end_transmission(data
);
474 switch (data
->flag
) {
475 case SHT15_READING_TEMP
:
476 data
->val_temp
= val
;
478 case SHT15_READING_HUMID
:
479 data
->val_humid
= val
;
483 data
->flag
= SHT15_READING_NOTHING
;
484 wake_up(&data
->wait_queue
);
487 static void sht15_update_voltage(struct work_struct
*work_s
)
489 struct sht15_data
*data
490 = container_of(work_s
, struct sht15_data
,
492 data
->supply_uV
= regulator_get_voltage(data
->reg
);
496 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
497 * @nb: associated notification structure
498 * @event: voltage regulator state change event code
499 * @ignored: function parameter - ignored here
501 * Note that as the notification code holds the regulator lock, we have
502 * to schedule an update of the supply voltage rather than getting it directly.
504 static int sht15_invalidate_voltage(struct notifier_block
*nb
,
508 struct sht15_data
*data
= container_of(nb
, struct sht15_data
, nb
);
510 if (event
== REGULATOR_EVENT_VOLTAGE_CHANGE
)
511 data
->supply_uV_valid
= false;
512 schedule_work(&data
->update_supply_work
);
517 static int __devinit
sht15_probe(struct platform_device
*pdev
)
520 struct sht15_data
*data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
524 dev_err(&pdev
->dev
, "kzalloc failed");
528 INIT_WORK(&data
->read_work
, sht15_bh_read_data
);
529 INIT_WORK(&data
->update_supply_work
, sht15_update_voltage
);
530 platform_set_drvdata(pdev
, data
);
531 mutex_init(&data
->read_lock
);
532 data
->dev
= &pdev
->dev
;
533 init_waitqueue_head(&data
->wait_queue
);
535 if (pdev
->dev
.platform_data
== NULL
) {
536 dev_err(&pdev
->dev
, "no platform data supplied");
539 data
->pdata
= pdev
->dev
.platform_data
;
540 data
->supply_uV
= data
->pdata
->supply_mv
*1000;
542 /* If a regulator is available, query what the supply voltage actually is!*/
543 data
->reg
= regulator_get(data
->dev
, "vcc");
544 if (!IS_ERR(data
->reg
)) {
547 voltage
= regulator_get_voltage(data
->reg
);
549 data
->supply_uV
= voltage
;
551 regulator_enable(data
->reg
);
552 /* setup a notifier block to update this if another device
553 * causes the voltage to change */
554 data
->nb
.notifier_call
= &sht15_invalidate_voltage
;
555 ret
= regulator_register_notifier(data
->reg
, &data
->nb
);
557 /* Try requesting the GPIOs */
558 ret
= gpio_request(data
->pdata
->gpio_sck
, "SHT15 sck");
560 dev_err(&pdev
->dev
, "gpio request failed");
563 gpio_direction_output(data
->pdata
->gpio_sck
, 0);
564 ret
= gpio_request(data
->pdata
->gpio_data
, "SHT15 data");
566 dev_err(&pdev
->dev
, "gpio request failed");
567 goto err_release_gpio_sck
;
569 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &sht15_attr_group
);
571 dev_err(&pdev
->dev
, "sysfs create failed");
572 goto err_release_gpio_data
;
575 ret
= request_irq(gpio_to_irq(data
->pdata
->gpio_data
),
576 sht15_interrupt_fired
,
577 IRQF_TRIGGER_FALLING
,
581 dev_err(&pdev
->dev
, "failed to get irq for data line");
582 goto err_release_gpio_data
;
584 disable_irq_nosync(gpio_to_irq(data
->pdata
->gpio_data
));
585 sht15_connection_reset(data
);
586 sht15_send_cmd(data
, 0x1E);
588 data
->hwmon_dev
= hwmon_device_register(data
->dev
);
589 if (IS_ERR(data
->hwmon_dev
)) {
590 ret
= PTR_ERR(data
->hwmon_dev
);
591 goto err_release_irq
;
596 free_irq(gpio_to_irq(data
->pdata
->gpio_data
), data
);
597 err_release_gpio_data
:
598 gpio_free(data
->pdata
->gpio_data
);
599 err_release_gpio_sck
:
600 gpio_free(data
->pdata
->gpio_sck
);
608 static int __devexit
sht15_remove(struct platform_device
*pdev
)
610 struct sht15_data
*data
= platform_get_drvdata(pdev
);
612 /* Make sure any reads from the device are done and
613 * prevent new ones beginnning */
614 mutex_lock(&data
->read_lock
);
615 hwmon_device_unregister(data
->hwmon_dev
);
616 sysfs_remove_group(&pdev
->dev
.kobj
, &sht15_attr_group
);
617 if (!IS_ERR(data
->reg
)) {
618 regulator_unregister_notifier(data
->reg
, &data
->nb
);
619 regulator_disable(data
->reg
);
620 regulator_put(data
->reg
);
623 free_irq(gpio_to_irq(data
->pdata
->gpio_data
), data
);
624 gpio_free(data
->pdata
->gpio_data
);
625 gpio_free(data
->pdata
->gpio_sck
);
626 mutex_unlock(&data
->read_lock
);
633 * sht_drivers simultaneously refers to __devinit and __devexit function
634 * which causes spurious section mismatch warning. So use __refdata to
637 static struct platform_driver __refdata sht_drivers
[] = {
641 .owner
= THIS_MODULE
,
643 .probe
= sht15_probe
,
644 .remove
= __devexit_p(sht15_remove
),
648 .owner
= THIS_MODULE
,
650 .probe
= sht15_probe
,
651 .remove
= __devexit_p(sht15_remove
),
655 .owner
= THIS_MODULE
,
657 .probe
= sht15_probe
,
658 .remove
= __devexit_p(sht15_remove
),
662 .owner
= THIS_MODULE
,
664 .probe
= sht15_probe
,
665 .remove
= __devexit_p(sht15_remove
),
669 .owner
= THIS_MODULE
,
671 .probe
= sht15_probe
,
672 .remove
= __devexit_p(sht15_remove
),
677 static int __init
sht15_init(void)
682 for (i
= 0; i
< ARRAY_SIZE(sht_drivers
); i
++) {
683 ret
= platform_driver_register(&sht_drivers
[i
]);
692 platform_driver_unregister(&sht_drivers
[i
]);
696 module_init(sht15_init
);
698 static void __exit
sht15_exit(void)
701 for (i
= ARRAY_SIZE(sht_drivers
) - 1; i
>= 0; i
--)
702 platform_driver_unregister(&sht_drivers
[i
]);
704 module_exit(sht15_exit
);
706 MODULE_LICENSE("GPL");