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/delay.h>
34 #include <linux/jiffies.h>
35 #include <linux/err.h>
36 #include <linux/sht15.h>
37 #include <linux/regulator/consumer.h>
38 #include <asm/atomic.h>
40 #define SHT15_MEASURE_TEMP 3
41 #define SHT15_MEASURE_RH 5
43 #define SHT15_READING_NOTHING 0
44 #define SHT15_READING_TEMP 1
45 #define SHT15_READING_HUMID 2
47 /* Min timings in nsecs */
48 #define SHT15_TSCKL 100 /* clock low */
49 #define SHT15_TSCKH 100 /* clock high */
50 #define SHT15_TSU 150 /* data setup time */
53 * struct sht15_temppair - elements of voltage dependant temp calc
54 * @vdd: supply voltage in microvolts
57 struct sht15_temppair {
58 int vdd; /* microvolts */
62 /* Table 9 from data sheet - relates temperature calculation
65 static const struct sht15_temppair temppoints[] = {
74 * struct sht15_data - device instance specific data
75 * @pdata: platform data (gpio's etc)
76 * @read_work: bh of interrupt handler
77 * @wait_queue: wait queue for getting values from device
78 * @val_temp: last temperature value read from device
79 * @val_humid: last humidity value read from device
80 * @flag: status flag used to identify what the last request was
81 * @valid: are the current stored values valid (start condition)
82 * @last_updat: time of last update
83 * @read_lock: mutex to ensure only one read in progress
85 * @dev: associate device structure
86 * @hwmon_dev: device associated with hwmon subsystem
87 * @reg: associated regulator (if specified)
88 * @nb: notifier block to handle notifications of voltage changes
89 * @supply_uV: local copy of supply voltage used to allow
90 * use of regulator consumer if available
91 * @supply_uV_valid: indicates that an updated value has not yet
92 * been obtained from the regulator and so any calculations
93 * based upon it will be invalid.
94 * @update_supply_work: work struct that is used to update the supply_uV
95 * @interrupt_handled: flag used to indicate a hander has been scheduled
98 struct sht15_platform_data *pdata;
99 struct work_struct read_work;
100 wait_queue_head_t wait_queue;
105 unsigned long last_updat;
106 struct mutex read_lock;
108 struct device *hwmon_dev;
109 struct regulator *reg;
110 struct notifier_block nb;
113 struct work_struct update_supply_work;
114 atomic_t interrupt_handled;
118 * sht15_connection_reset() - reset the comms interface
119 * @data: sht15 specific data
121 * This implements section 3.4 of the data sheet
123 static void sht15_connection_reset(struct sht15_data *data)
126 gpio_direction_output(data->pdata->gpio_data, 1);
128 gpio_set_value(data->pdata->gpio_sck, 0);
130 for (i = 0; i < 9; ++i) {
131 gpio_set_value(data->pdata->gpio_sck, 1);
133 gpio_set_value(data->pdata->gpio_sck, 0);
138 * sht15_send_bit() - send an individual bit to the device
139 * @data: device state data
140 * @val: value of bit to be sent
142 static inline void sht15_send_bit(struct sht15_data *data, int val)
145 gpio_set_value(data->pdata->gpio_data, val);
147 gpio_set_value(data->pdata->gpio_sck, 1);
149 gpio_set_value(data->pdata->gpio_sck, 0);
150 ndelay(SHT15_TSCKL); /* clock low time */
154 * sht15_transmission_start() - specific sequence for new transmission
156 * @data: device state data
157 * Timings for this are not documented on the data sheet, so very
158 * conservative ones used in implementation. This implements
159 * figure 12 on the data sheet.
161 static void sht15_transmission_start(struct sht15_data *data)
163 /* ensure data is high and output */
164 gpio_direction_output(data->pdata->gpio_data, 1);
166 gpio_set_value(data->pdata->gpio_sck, 0);
168 gpio_set_value(data->pdata->gpio_sck, 1);
170 gpio_set_value(data->pdata->gpio_data, 0);
172 gpio_set_value(data->pdata->gpio_sck, 0);
174 gpio_set_value(data->pdata->gpio_sck, 1);
176 gpio_set_value(data->pdata->gpio_data, 1);
178 gpio_set_value(data->pdata->gpio_sck, 0);
182 * sht15_send_byte() - send a single byte to the device
183 * @data: device state
184 * @byte: value to be sent
186 static void sht15_send_byte(struct sht15_data *data, u8 byte)
189 for (i = 0; i < 8; i++) {
190 sht15_send_bit(data, !!(byte & 0x80));
195 * sht15_wait_for_response() - checks for ack from device
196 * @data: device state
198 static int sht15_wait_for_response(struct sht15_data *data)
200 gpio_direction_input(data->pdata->gpio_data);
201 gpio_set_value(data->pdata->gpio_sck, 1);
203 if (gpio_get_value(data->pdata->gpio_data)) {
204 gpio_set_value(data->pdata->gpio_sck, 0);
205 dev_err(data->dev, "Command not acknowledged\n");
206 sht15_connection_reset(data);
209 gpio_set_value(data->pdata->gpio_sck, 0);
215 * sht15_send_cmd() - Sends a command to the device.
216 * @data: device state
217 * @cmd: command byte to be sent
219 * On entry, sck is output low, data is output pull high
220 * and the interrupt disabled.
222 static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
225 sht15_transmission_start(data);
226 sht15_send_byte(data, cmd);
227 ret = sht15_wait_for_response(data);
231 * sht15_update_single_val() - get a new value from device
232 * @data: device instance specific data
233 * @command: command sent to request value
234 * @timeout_msecs: timeout after which comms are assumed
235 * to have failed are reset.
237 static inline int sht15_update_single_val(struct sht15_data *data,
242 ret = sht15_send_cmd(data, command);
246 gpio_direction_input(data->pdata->gpio_data);
247 atomic_set(&data->interrupt_handled, 0);
249 enable_irq(gpio_to_irq(data->pdata->gpio_data));
250 if (gpio_get_value(data->pdata->gpio_data) == 0) {
251 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
252 /* Only relevant if the interrupt hasn't occured. */
253 if (!atomic_read(&data->interrupt_handled))
254 schedule_work(&data->read_work);
256 ret = wait_event_timeout(data->wait_queue,
257 (data->flag == SHT15_READING_NOTHING),
258 msecs_to_jiffies(timeout_msecs));
259 if (ret == 0) {/* timeout occurred */
260 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));;
261 sht15_connection_reset(data);
268 * sht15_update_vals() - get updated readings from device if too old
269 * @data: device state
271 static int sht15_update_vals(struct sht15_data *data)
276 mutex_lock(&data->read_lock);
277 if (time_after(jiffies, data->last_updat + timeout)
279 data->flag = SHT15_READING_HUMID;
280 ret = sht15_update_single_val(data, SHT15_MEASURE_RH, 160);
283 data->flag = SHT15_READING_TEMP;
284 ret = sht15_update_single_val(data, SHT15_MEASURE_TEMP, 400);
288 data->last_updat = jiffies;
291 mutex_unlock(&data->read_lock);
297 * sht15_calc_temp() - convert the raw reading to a temperature
298 * @data: device state
300 * As per section 4.3 of the data sheet.
302 static inline int sht15_calc_temp(struct sht15_data *data)
307 for (i = 1; i < ARRAY_SIZE(temppoints) - 1; i++)
308 /* Find pointer to interpolate */
309 if (data->supply_uV > temppoints[i - 1].vdd) {
310 d1 = (data->supply_uV/1000 - temppoints[i - 1].vdd)
311 * (temppoints[i].d1 - temppoints[i - 1].d1)
312 / (temppoints[i].vdd - temppoints[i - 1].vdd)
313 + temppoints[i - 1].d1;
317 return data->val_temp*10 + d1;
321 * sht15_calc_humid() - using last temperature convert raw to humid
322 * @data: device state
324 * This is the temperature compensated version as per section 4.2 of
327 static inline int sht15_calc_humid(struct sht15_data *data)
329 int RHlinear; /* milli percent */
330 int temp = sht15_calc_temp(data);
333 const int c2 = 40500; /* x 10 ^ -6 */
334 const int c3 = 2800; /* x10 ^ -9 */
337 + c2 * data->val_humid/1000
338 + (data->val_humid * data->val_humid * c3)/1000000;
339 return (temp - 25000) * (10000 + 800 * data->val_humid)
340 / 1000000 + RHlinear;
343 static ssize_t sht15_show_temp(struct device *dev,
344 struct device_attribute *attr,
348 struct sht15_data *data = dev_get_drvdata(dev);
350 /* Technically no need to read humidity as well */
351 ret = sht15_update_vals(data);
353 return ret ? ret : sprintf(buf, "%d\n",
354 sht15_calc_temp(data));
357 static ssize_t sht15_show_humidity(struct device *dev,
358 struct device_attribute *attr,
362 struct sht15_data *data = dev_get_drvdata(dev);
364 ret = sht15_update_vals(data);
366 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
369 static ssize_t show_name(struct device *dev,
370 struct device_attribute *attr,
373 struct platform_device *pdev = to_platform_device(dev);
374 return sprintf(buf, "%s\n", pdev->name);
377 static SENSOR_DEVICE_ATTR(temp1_input,
378 S_IRUGO, sht15_show_temp,
380 static SENSOR_DEVICE_ATTR(humidity1_input,
381 S_IRUGO, sht15_show_humidity,
383 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
384 static struct attribute *sht15_attrs[] = {
385 &sensor_dev_attr_temp1_input.dev_attr.attr,
386 &sensor_dev_attr_humidity1_input.dev_attr.attr,
391 static const struct attribute_group sht15_attr_group = {
392 .attrs = sht15_attrs,
395 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
397 struct sht15_data *data = d;
398 /* First disable the interrupt */
399 disable_irq_nosync(irq);
400 atomic_inc(&data->interrupt_handled);
401 /* Then schedule a reading work struct */
402 if (data->flag != SHT15_READING_NOTHING)
403 schedule_work(&data->read_work);
407 /* Each byte of data is acknowledged by pulling the data line
408 * low for one clock pulse.
410 static void sht15_ack(struct sht15_data *data)
412 gpio_direction_output(data->pdata->gpio_data, 0);
414 gpio_set_value(data->pdata->gpio_sck, 1);
416 gpio_set_value(data->pdata->gpio_sck, 0);
418 gpio_set_value(data->pdata->gpio_data, 1);
420 gpio_direction_input(data->pdata->gpio_data);
423 * sht15_end_transmission() - notify device of end of transmission
424 * @data: device state
426 * This is basically a NAK. (single clock pulse, data high)
428 static void sht15_end_transmission(struct sht15_data *data)
430 gpio_direction_output(data->pdata->gpio_data, 1);
432 gpio_set_value(data->pdata->gpio_sck, 1);
434 gpio_set_value(data->pdata->gpio_sck, 0);
438 static void sht15_bh_read_data(struct work_struct *work_s)
442 struct sht15_data *data
443 = container_of(work_s, struct sht15_data,
445 /* Firstly, verify the line is low */
446 if (gpio_get_value(data->pdata->gpio_data)) {
447 /* If not, then start the interrupt again - care
448 here as could have gone low in meantime so verify
451 atomic_set(&data->interrupt_handled, 0);
452 enable_irq(gpio_to_irq(data->pdata->gpio_data));
453 /* If still not occured or another handler has been scheduled */
454 if (gpio_get_value(data->pdata->gpio_data)
455 || atomic_read(&data->interrupt_handled))
458 /* Read the data back from the device */
459 for (i = 0; i < 16; ++i) {
461 gpio_set_value(data->pdata->gpio_sck, 1);
463 val |= !!gpio_get_value(data->pdata->gpio_data);
464 gpio_set_value(data->pdata->gpio_sck, 0);
469 /* Tell the device we are done */
470 sht15_end_transmission(data);
472 switch (data->flag) {
473 case SHT15_READING_TEMP:
474 data->val_temp = val;
476 case SHT15_READING_HUMID:
477 data->val_humid = val;
481 data->flag = SHT15_READING_NOTHING;
482 wake_up(&data->wait_queue);
485 static void sht15_update_voltage(struct work_struct *work_s)
487 struct sht15_data *data
488 = container_of(work_s, struct sht15_data,
490 data->supply_uV = regulator_get_voltage(data->reg);
494 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
495 * @nb: associated notification structure
496 * @event: voltage regulator state change event code
497 * @ignored: function parameter - ignored here
499 * Note that as the notification code holds the regulator lock, we have
500 * to schedule an update of the supply voltage rather than getting it directly.
502 static int sht15_invalidate_voltage(struct notifier_block *nb,
506 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
508 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
509 data->supply_uV_valid = false;
510 schedule_work(&data->update_supply_work);
515 static int __devinit sht15_probe(struct platform_device *pdev)
518 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
522 dev_err(&pdev->dev, "kzalloc failed");
526 INIT_WORK(&data->read_work, sht15_bh_read_data);
527 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
528 platform_set_drvdata(pdev, data);
529 mutex_init(&data->read_lock);
530 data->dev = &pdev->dev;
531 init_waitqueue_head(&data->wait_queue);
533 if (pdev->dev.platform_data == NULL) {
534 dev_err(&pdev->dev, "no platform data supplied");
537 data->pdata = pdev->dev.platform_data;
538 data->supply_uV = data->pdata->supply_mv*1000;
540 /* If a regulator is available, query what the supply voltage actually is!*/
541 data->reg = regulator_get(data->dev, "vcc");
542 if (!IS_ERR(data->reg)) {
543 data->supply_uV = regulator_get_voltage(data->reg);
544 regulator_enable(data->reg);
545 /* setup a notifier block to update this if another device
546 * causes the voltage to change */
547 data->nb.notifier_call = &sht15_invalidate_voltage;
548 ret = regulator_register_notifier(data->reg, &data->nb);
550 /* Try requesting the GPIOs */
551 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
553 dev_err(&pdev->dev, "gpio request failed");
556 gpio_direction_output(data->pdata->gpio_sck, 0);
557 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
559 dev_err(&pdev->dev, "gpio request failed");
560 goto err_release_gpio_sck;
562 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
564 dev_err(&pdev->dev, "sysfs create failed");
568 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
569 sht15_interrupt_fired,
570 IRQF_TRIGGER_FALLING,
574 dev_err(&pdev->dev, "failed to get irq for data line");
575 goto err_release_gpio_data;
577 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
578 sht15_connection_reset(data);
579 sht15_send_cmd(data, 0x1E);
581 data->hwmon_dev = hwmon_device_register(data->dev);
582 if (IS_ERR(data->hwmon_dev)) {
583 ret = PTR_ERR(data->hwmon_dev);
584 goto err_release_gpio_data;
588 err_release_gpio_data:
589 gpio_free(data->pdata->gpio_data);
590 err_release_gpio_sck:
591 gpio_free(data->pdata->gpio_sck);
599 static int __devexit sht15_remove(struct platform_device *pdev)
601 struct sht15_data *data = platform_get_drvdata(pdev);
603 /* Make sure any reads from the device are done and
604 * prevent new ones beginnning */
605 mutex_lock(&data->read_lock);
606 hwmon_device_unregister(data->hwmon_dev);
607 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
608 if (!IS_ERR(data->reg)) {
609 regulator_unregister_notifier(data->reg, &data->nb);
610 regulator_disable(data->reg);
611 regulator_put(data->reg);
614 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
615 gpio_free(data->pdata->gpio_data);
616 gpio_free(data->pdata->gpio_sck);
617 mutex_unlock(&data->read_lock);
623 static struct platform_driver sht_drivers[] = {
627 .owner = THIS_MODULE,
629 .probe = sht15_probe,
630 .remove = __devexit_p(sht15_remove),
634 .owner = THIS_MODULE,
636 .probe = sht15_probe,
637 .remove = __devexit_p(sht15_remove),
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),
663 static int __init sht15_init(void)
668 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
669 ret = platform_driver_register(&sht_drivers[i]);
678 platform_driver_unregister(&sht_drivers[i]);
682 module_init(sht15_init);
684 static void __exit sht15_exit(void)
687 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
688 platform_driver_unregister(&sht_drivers[i]);
690 module_exit(sht15_exit);
692 MODULE_LICENSE("GPL");