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)
305 int d1 = temppoints[0].d1;
308 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
309 /* Find pointer to interpolate */
310 if (data->supply_uV > temppoints[i - 1].vdd) {
311 d1 = (data->supply_uV - 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)) {
546 voltage = regulator_get_voltage(data->reg);
548 data->supply_uV = voltage;
550 regulator_enable(data->reg);
551 /* setup a notifier block to update this if another device
552 * causes the voltage to change */
553 data->nb.notifier_call = &sht15_invalidate_voltage;
554 ret = regulator_register_notifier(data->reg, &data->nb);
556 /* Try requesting the GPIOs */
557 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
559 dev_err(&pdev->dev, "gpio request failed");
562 gpio_direction_output(data->pdata->gpio_sck, 0);
563 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
565 dev_err(&pdev->dev, "gpio request failed");
566 goto err_release_gpio_sck;
568 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
570 dev_err(&pdev->dev, "sysfs create failed");
571 goto err_release_gpio_data;
574 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
575 sht15_interrupt_fired,
576 IRQF_TRIGGER_FALLING,
580 dev_err(&pdev->dev, "failed to get irq for data line");
581 goto err_release_gpio_data;
583 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
584 sht15_connection_reset(data);
585 sht15_send_cmd(data, 0x1E);
587 data->hwmon_dev = hwmon_device_register(data->dev);
588 if (IS_ERR(data->hwmon_dev)) {
589 ret = PTR_ERR(data->hwmon_dev);
590 goto err_release_irq;
595 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
596 err_release_gpio_data:
597 gpio_free(data->pdata->gpio_data);
598 err_release_gpio_sck:
599 gpio_free(data->pdata->gpio_sck);
607 static int __devexit sht15_remove(struct platform_device *pdev)
609 struct sht15_data *data = platform_get_drvdata(pdev);
611 /* Make sure any reads from the device are done and
612 * prevent new ones beginnning */
613 mutex_lock(&data->read_lock);
614 hwmon_device_unregister(data->hwmon_dev);
615 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
616 if (!IS_ERR(data->reg)) {
617 regulator_unregister_notifier(data->reg, &data->nb);
618 regulator_disable(data->reg);
619 regulator_put(data->reg);
622 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
623 gpio_free(data->pdata->gpio_data);
624 gpio_free(data->pdata->gpio_sck);
625 mutex_unlock(&data->read_lock);
632 * sht_drivers simultaneously refers to __devinit and __devexit function
633 * which causes spurious section mismatch warning. So use __refdata to
636 static struct platform_driver __refdata sht_drivers[] = {
640 .owner = THIS_MODULE,
642 .probe = sht15_probe,
643 .remove = __devexit_p(sht15_remove),
647 .owner = THIS_MODULE,
649 .probe = sht15_probe,
650 .remove = __devexit_p(sht15_remove),
654 .owner = THIS_MODULE,
656 .probe = sht15_probe,
657 .remove = __devexit_p(sht15_remove),
661 .owner = THIS_MODULE,
663 .probe = sht15_probe,
664 .remove = __devexit_p(sht15_remove),
668 .owner = THIS_MODULE,
670 .probe = sht15_probe,
671 .remove = __devexit_p(sht15_remove),
676 static int __init sht15_init(void)
681 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
682 ret = platform_driver_register(&sht_drivers[i]);
691 platform_driver_unregister(&sht_drivers[i]);
695 module_init(sht15_init);
697 static void __exit sht15_exit(void)
700 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
701 platform_driver_unregister(&sht_drivers[i]);
703 module_exit(sht15_exit);
705 MODULE_LICENSE("GPL");