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 * For further information, see the Documentation/hwmon/sht15 file.
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/gpio.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/jiffies.h>
27 #include <linux/err.h>
28 #include <linux/sht15.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/slab.h>
31 #include <asm/atomic.h>
34 #define SHT15_MEASURE_TEMP 0x03
35 #define SHT15_MEASURE_RH 0x05
38 #define SHT15_TSCKL 100 /* (nsecs) clock low */
39 #define SHT15_TSCKH 100 /* (nsecs) clock high */
40 #define SHT15_TSU 150 /* (nsecs) data setup time */
42 /* Actions the driver may be doing */
44 SHT15_READING_NOTHING,
50 * struct sht15_temppair - elements of voltage dependent temp calc
51 * @vdd: supply voltage in microvolts
54 struct sht15_temppair {
55 int vdd; /* microvolts */
59 /* Table 9 from datasheet - relates temperature calculation to supply voltage */
60 static const struct sht15_temppair temppoints[] = {
69 * struct sht15_data - device instance specific data
70 * @pdata: platform data (gpio's etc).
71 * @read_work: bh of interrupt handler.
72 * @wait_queue: wait queue for getting values from device.
73 * @val_temp: last temperature value read from device.
74 * @val_humid: last humidity value read from device.
75 * @state: state identifying the action the driver is doing.
76 * @measurements_valid: are the current stored measures valid (start condition).
77 * @last_measurement: time of last measure.
78 * @read_lock: mutex to ensure only one read in progress at a time.
79 * @dev: associate device structure.
80 * @hwmon_dev: device associated with hwmon subsystem.
81 * @reg: associated regulator (if specified).
82 * @nb: notifier block to handle notifications of voltage
84 * @supply_uV: local copy of supply voltage used to allow use of
85 * regulator consumer if available.
86 * @supply_uV_valid: indicates that an updated value has not yet been
87 * obtained from the regulator and so any calculations
88 * based upon it will be invalid.
89 * @update_supply_work: work struct that is used to update the supply_uV.
90 * @interrupt_handled: flag used to indicate a handler has been scheduled.
93 struct sht15_platform_data *pdata;
94 struct work_struct read_work;
95 wait_queue_head_t wait_queue;
98 enum sht15_state state;
99 bool measurements_valid;
100 unsigned long last_measurement;
101 struct mutex read_lock;
103 struct device *hwmon_dev;
104 struct regulator *reg;
105 struct notifier_block nb;
107 bool supply_uV_valid;
108 struct work_struct update_supply_work;
109 atomic_t interrupt_handled;
113 * sht15_connection_reset() - reset the comms interface
114 * @data: sht15 specific data
116 * This implements section 3.4 of the data sheet
118 static void sht15_connection_reset(struct sht15_data *data)
122 gpio_direction_output(data->pdata->gpio_data, 1);
124 gpio_set_value(data->pdata->gpio_sck, 0);
126 for (i = 0; i < 9; ++i) {
127 gpio_set_value(data->pdata->gpio_sck, 1);
129 gpio_set_value(data->pdata->gpio_sck, 0);
135 * sht15_send_bit() - send an individual bit to the device
136 * @data: device state data
137 * @val: value of bit to be sent
139 static inline void sht15_send_bit(struct sht15_data *data, int val)
141 gpio_set_value(data->pdata->gpio_data, val);
143 gpio_set_value(data->pdata->gpio_sck, 1);
145 gpio_set_value(data->pdata->gpio_sck, 0);
146 ndelay(SHT15_TSCKL); /* clock low time */
150 * sht15_transmission_start() - specific sequence for new transmission
151 * @data: device state data
153 * Timings for this are not documented on the data sheet, so very
154 * conservative ones used in implementation. This implements
155 * figure 12 on the data sheet.
157 static void sht15_transmission_start(struct sht15_data *data)
159 /* ensure data is high and output */
160 gpio_direction_output(data->pdata->gpio_data, 1);
162 gpio_set_value(data->pdata->gpio_sck, 0);
164 gpio_set_value(data->pdata->gpio_sck, 1);
166 gpio_set_value(data->pdata->gpio_data, 0);
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, 1);
174 gpio_set_value(data->pdata->gpio_sck, 0);
179 * sht15_send_byte() - send a single byte to the device
180 * @data: device state
181 * @byte: value to be sent
183 static void sht15_send_byte(struct sht15_data *data, u8 byte)
187 for (i = 0; i < 8; i++) {
188 sht15_send_bit(data, !!(byte & 0x80));
194 * sht15_wait_for_response() - checks for ack from device
195 * @data: device state
197 static int sht15_wait_for_response(struct sht15_data *data)
199 gpio_direction_input(data->pdata->gpio_data);
200 gpio_set_value(data->pdata->gpio_sck, 1);
202 if (gpio_get_value(data->pdata->gpio_data)) {
203 gpio_set_value(data->pdata->gpio_sck, 0);
204 dev_err(data->dev, "Command not acknowledged\n");
205 sht15_connection_reset(data);
208 gpio_set_value(data->pdata->gpio_sck, 0);
214 * sht15_send_cmd() - Sends a command to the device.
215 * @data: device state
216 * @cmd: command byte to be sent
218 * On entry, sck is output low, data is output pull high
219 * and the interrupt disabled.
221 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);
232 * sht15_measurement() - 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 int sht15_measurement(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 occurred. */
255 if (!atomic_read(&data->interrupt_handled))
256 schedule_work(&data->read_work);
258 ret = wait_event_timeout(data->wait_queue,
259 (data->state == 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_measurements() - get updated measures from device if too old
271 * @data: device state
273 static int sht15_update_measurements(struct sht15_data *data)
278 mutex_lock(&data->read_lock);
279 if (time_after(jiffies, data->last_measurement + timeout)
280 || !data->measurements_valid) {
281 data->state = SHT15_READING_HUMID;
282 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
285 data->state = SHT15_READING_TEMP;
286 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
289 data->measurements_valid = true;
290 data->last_measurement = 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 * The sensor is assumed to be V3, which is compatible with V4.
330 * Humidity conversion coefficients are shown in table 7 of the datasheet.
332 static inline int sht15_calc_humid(struct sht15_data *data)
334 int rh_linear; /* milli percent */
335 int temp = sht15_calc_temp(data);
338 const int c2 = 40500; /* x 10 ^ -6 */
339 const int c3 = -28; /* x 10 ^ -7 */
341 rh_linear = c1 * 1000
342 + c2 * data->val_humid / 1000
343 + (data->val_humid * data->val_humid * c3) / 10000;
344 return (temp - 25000) * (10000 + 80 * data->val_humid)
345 / 1000000 + rh_linear;
349 * sht15_show_temp() - show temperature measurement value in sysfs
351 * @attr: device attribute.
352 * @buf: sysfs buffer where measurement values are written to.
354 * Will be called on read access to temp1_input sysfs attribute.
355 * Returns number of bytes written into buffer, negative errno on error.
357 static ssize_t sht15_show_temp(struct device *dev,
358 struct device_attribute *attr,
362 struct sht15_data *data = dev_get_drvdata(dev);
364 /* Technically no need to read humidity as well */
365 ret = sht15_update_measurements(data);
367 return ret ? ret : sprintf(buf, "%d\n",
368 sht15_calc_temp(data));
372 * sht15_show_humidity() - show humidity measurement value in sysfs
374 * @attr: device attribute.
375 * @buf: sysfs buffer where measurement values are written to.
377 * Will be called on read access to humidity1_input sysfs attribute.
378 * Returns number of bytes written into buffer, negative errno on error.
380 static ssize_t sht15_show_humidity(struct device *dev,
381 struct device_attribute *attr,
385 struct sht15_data *data = dev_get_drvdata(dev);
387 ret = sht15_update_measurements(data);
389 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
393 static ssize_t show_name(struct device *dev,
394 struct device_attribute *attr,
397 struct platform_device *pdev = to_platform_device(dev);
398 return sprintf(buf, "%s\n", pdev->name);
401 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
402 sht15_show_temp, NULL, 0);
403 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
404 sht15_show_humidity, NULL, 0);
405 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
406 static struct attribute *sht15_attrs[] = {
407 &sensor_dev_attr_temp1_input.dev_attr.attr,
408 &sensor_dev_attr_humidity1_input.dev_attr.attr,
413 static const struct attribute_group sht15_attr_group = {
414 .attrs = sht15_attrs,
417 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
419 struct sht15_data *data = d;
421 /* First disable the interrupt */
422 disable_irq_nosync(irq);
423 atomic_inc(&data->interrupt_handled);
424 /* Then schedule a reading work struct */
425 if (data->state != SHT15_READING_NOTHING)
426 schedule_work(&data->read_work);
431 * sht15_ack() - Send an ack to the device
433 * Each byte of data is acknowledged by pulling the data line
434 * low for one clock pulse.
436 static void sht15_ack(struct sht15_data *data)
438 gpio_direction_output(data->pdata->gpio_data, 0);
440 gpio_set_value(data->pdata->gpio_sck, 1);
442 gpio_set_value(data->pdata->gpio_sck, 0);
444 gpio_set_value(data->pdata->gpio_data, 1);
446 gpio_direction_input(data->pdata->gpio_data);
450 * sht15_end_transmission() - notify device of end of transmission
451 * @data: device state
453 * This is basically a NAK. (single clock pulse, data high)
455 static void sht15_end_transmission(struct sht15_data *data)
457 gpio_direction_output(data->pdata->gpio_data, 1);
459 gpio_set_value(data->pdata->gpio_sck, 1);
461 gpio_set_value(data->pdata->gpio_sck, 0);
465 static void sht15_bh_read_data(struct work_struct *work_s)
469 struct sht15_data *data
470 = container_of(work_s, struct sht15_data,
473 /* Firstly, verify the line is low */
474 if (gpio_get_value(data->pdata->gpio_data)) {
476 * If not, then start the interrupt again - care here as could
477 * have gone low in meantime so verify it hasn't!
479 atomic_set(&data->interrupt_handled, 0);
480 enable_irq(gpio_to_irq(data->pdata->gpio_data));
481 /* If still not occurred or another handler has been scheduled */
482 if (gpio_get_value(data->pdata->gpio_data)
483 || atomic_read(&data->interrupt_handled))
487 /* Read the data back from the device */
488 for (i = 0; i < 16; ++i) {
490 gpio_set_value(data->pdata->gpio_sck, 1);
492 val |= !!gpio_get_value(data->pdata->gpio_data);
493 gpio_set_value(data->pdata->gpio_sck, 0);
499 /* Tell the device we are done */
500 sht15_end_transmission(data);
502 switch (data->state) {
503 case SHT15_READING_TEMP:
504 data->val_temp = val;
506 case SHT15_READING_HUMID:
507 data->val_humid = val;
513 data->state = SHT15_READING_NOTHING;
514 wake_up(&data->wait_queue);
517 static void sht15_update_voltage(struct work_struct *work_s)
519 struct sht15_data *data
520 = container_of(work_s, struct sht15_data,
522 data->supply_uV = regulator_get_voltage(data->reg);
526 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
527 * @nb: associated notification structure
528 * @event: voltage regulator state change event code
529 * @ignored: function parameter - ignored here
531 * Note that as the notification code holds the regulator lock, we have
532 * to schedule an update of the supply voltage rather than getting it directly.
534 static int sht15_invalidate_voltage(struct notifier_block *nb,
538 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
540 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
541 data->supply_uV_valid = false;
542 schedule_work(&data->update_supply_work);
547 static int __devinit sht15_probe(struct platform_device *pdev)
550 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
554 dev_err(&pdev->dev, "kzalloc failed\n");
558 INIT_WORK(&data->read_work, sht15_bh_read_data);
559 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
560 platform_set_drvdata(pdev, data);
561 mutex_init(&data->read_lock);
562 data->dev = &pdev->dev;
563 init_waitqueue_head(&data->wait_queue);
565 if (pdev->dev.platform_data == NULL) {
566 dev_err(&pdev->dev, "no platform data supplied\n");
569 data->pdata = pdev->dev.platform_data;
570 data->supply_uV = data->pdata->supply_mv * 1000;
573 * If a regulator is available,
574 * query what the supply voltage actually is!
576 data->reg = regulator_get(data->dev, "vcc");
577 if (!IS_ERR(data->reg)) {
580 voltage = regulator_get_voltage(data->reg);
582 data->supply_uV = voltage;
584 regulator_enable(data->reg);
586 * Setup a notifier block to update this if another device
587 * causes the voltage to change
589 data->nb.notifier_call = &sht15_invalidate_voltage;
590 ret = regulator_register_notifier(data->reg, &data->nb);
593 /* Try requesting the GPIOs */
594 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
596 dev_err(&pdev->dev, "gpio request failed\n");
599 gpio_direction_output(data->pdata->gpio_sck, 0);
601 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
603 dev_err(&pdev->dev, "gpio request failed\n");
604 goto err_release_gpio_sck;
606 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
608 dev_err(&pdev->dev, "sysfs create failed");
609 goto err_release_gpio_data;
612 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
613 sht15_interrupt_fired,
614 IRQF_TRIGGER_FALLING,
618 dev_err(&pdev->dev, "failed to get irq for data line\n");
619 goto err_release_gpio_data;
621 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
622 sht15_connection_reset(data);
623 sht15_send_cmd(data, 0x1E);
625 data->hwmon_dev = hwmon_device_register(data->dev);
626 if (IS_ERR(data->hwmon_dev)) {
627 ret = PTR_ERR(data->hwmon_dev);
628 goto err_release_irq;
634 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
635 err_release_gpio_data:
636 gpio_free(data->pdata->gpio_data);
637 err_release_gpio_sck:
638 gpio_free(data->pdata->gpio_sck);
645 static int __devexit sht15_remove(struct platform_device *pdev)
647 struct sht15_data *data = platform_get_drvdata(pdev);
650 * Make sure any reads from the device are done and
651 * prevent new ones beginning
653 mutex_lock(&data->read_lock);
654 hwmon_device_unregister(data->hwmon_dev);
655 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
656 if (!IS_ERR(data->reg)) {
657 regulator_unregister_notifier(data->reg, &data->nb);
658 regulator_disable(data->reg);
659 regulator_put(data->reg);
662 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
663 gpio_free(data->pdata->gpio_data);
664 gpio_free(data->pdata->gpio_sck);
665 mutex_unlock(&data->read_lock);
672 * sht_drivers simultaneously refers to __devinit and __devexit function
673 * which causes spurious section mismatch warning. So use __refdata to
676 static struct platform_driver __refdata sht_drivers[] = {
680 .owner = THIS_MODULE,
682 .probe = sht15_probe,
683 .remove = __devexit_p(sht15_remove),
687 .owner = THIS_MODULE,
689 .probe = sht15_probe,
690 .remove = __devexit_p(sht15_remove),
694 .owner = THIS_MODULE,
696 .probe = sht15_probe,
697 .remove = __devexit_p(sht15_remove),
701 .owner = THIS_MODULE,
703 .probe = sht15_probe,
704 .remove = __devexit_p(sht15_remove),
708 .owner = THIS_MODULE,
710 .probe = sht15_probe,
711 .remove = __devexit_p(sht15_remove),
715 static int __init sht15_init(void)
720 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
721 ret = platform_driver_register(&sht_drivers[i]);
730 platform_driver_unregister(&sht_drivers[i]);
734 module_init(sht15_init);
736 static void __exit sht15_exit(void)
739 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
740 platform_driver_unregister(&sht_drivers[i]);
742 module_exit(sht15_exit);
744 MODULE_LICENSE("GPL");