blob: 56cd6004da36e1ec08f94e05b890f61239323c08 [file] [log] [blame]
Jonathan Cameron251eb402009-04-13 14:39:45 -07001/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
4 * Copyright (c) 2009 Jonathan Cameron
5 *
6 * Copyright (c) 2007 Wouter Horre
7 *
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.
11 *
12 * Currently ignoring checksum on readings.
13 * Default resolution only (14bit temp, 12bit humidity)
14 * Ignoring battery status.
15 * Heater not enabled.
16 * Timings are all conservative.
17 *
18 * Data sheet available (1/2009) at
19 * http://www.sensirion.ch/en/pdf/product_information/Datasheet-humidity-sensor-SHT1x.pdf
20 *
21 * Regulator supply name = vcc
22 */
23
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>
39
40#define SHT15_MEASURE_TEMP 3
41#define SHT15_MEASURE_RH 5
42
43#define SHT15_READING_NOTHING 0
44#define SHT15_READING_TEMP 1
45#define SHT15_READING_HUMID 2
46
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 */
51
52/**
53 * struct sht15_temppair - elements of voltage dependant temp calc
54 * @vdd: supply voltage in microvolts
55 * @d1: see data sheet
56 */
57struct sht15_temppair {
58 int vdd; /* microvolts */
59 int d1;
60};
61
62/* Table 9 from data sheet - relates temperature calculation
63 * to supply voltage.
64 */
65static const struct sht15_temppair temppoints[] = {
66 { 2500000, -39400 },
67 { 3000000, -39600 },
68 { 3500000, -39700 },
69 { 4000000, -39800 },
70 { 5000000, -40100 },
71};
72
73/**
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
84 * at a time.
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
96 */
97struct sht15_data {
98 struct sht15_platform_data *pdata;
99 struct work_struct read_work;
100 wait_queue_head_t wait_queue;
101 uint16_t val_temp;
102 uint16_t val_humid;
103 u8 flag;
104 u8 valid;
105 unsigned long last_updat;
106 struct mutex read_lock;
107 struct device *dev;
108 struct device *hwmon_dev;
109 struct regulator *reg;
110 struct notifier_block nb;
111 int supply_uV;
112 int supply_uV_valid;
113 struct work_struct update_supply_work;
114 atomic_t interrupt_handled;
115};
116
117/**
118 * sht15_connection_reset() - reset the comms interface
119 * @data: sht15 specific data
120 *
121 * This implements section 3.4 of the data sheet
122 */
123static void sht15_connection_reset(struct sht15_data *data)
124{
125 int i;
126 gpio_direction_output(data->pdata->gpio_data, 1);
127 ndelay(SHT15_TSCKL);
128 gpio_set_value(data->pdata->gpio_sck, 0);
129 ndelay(SHT15_TSCKL);
130 for (i = 0; i < 9; ++i) {
131 gpio_set_value(data->pdata->gpio_sck, 1);
132 ndelay(SHT15_TSCKH);
133 gpio_set_value(data->pdata->gpio_sck, 0);
134 ndelay(SHT15_TSCKL);
135 }
136}
137/**
138 * sht15_send_bit() - send an individual bit to the device
139 * @data: device state data
140 * @val: value of bit to be sent
141 **/
142static inline void sht15_send_bit(struct sht15_data *data, int val)
143{
144
145 gpio_set_value(data->pdata->gpio_data, val);
146 ndelay(SHT15_TSU);
147 gpio_set_value(data->pdata->gpio_sck, 1);
148 ndelay(SHT15_TSCKH);
149 gpio_set_value(data->pdata->gpio_sck, 0);
150 ndelay(SHT15_TSCKL); /* clock low time */
151}
152
153/**
154 * sht15_transmission_start() - specific sequence for new transmission
155 *
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.
160 **/
161static void sht15_transmission_start(struct sht15_data *data)
162{
163 /* ensure data is high and output */
164 gpio_direction_output(data->pdata->gpio_data, 1);
165 ndelay(SHT15_TSU);
166 gpio_set_value(data->pdata->gpio_sck, 0);
167 ndelay(SHT15_TSCKL);
168 gpio_set_value(data->pdata->gpio_sck, 1);
169 ndelay(SHT15_TSCKH);
170 gpio_set_value(data->pdata->gpio_data, 0);
171 ndelay(SHT15_TSU);
172 gpio_set_value(data->pdata->gpio_sck, 0);
173 ndelay(SHT15_TSCKL);
174 gpio_set_value(data->pdata->gpio_sck, 1);
175 ndelay(SHT15_TSCKH);
176 gpio_set_value(data->pdata->gpio_data, 1);
177 ndelay(SHT15_TSU);
178 gpio_set_value(data->pdata->gpio_sck, 0);
179 ndelay(SHT15_TSCKL);
180}
181/**
182 * sht15_send_byte() - send a single byte to the device
183 * @data: device state
184 * @byte: value to be sent
185 **/
186static void sht15_send_byte(struct sht15_data *data, u8 byte)
187{
188 int i;
189 for (i = 0; i < 8; i++) {
190 sht15_send_bit(data, !!(byte & 0x80));
191 byte <<= 1;
192 }
193}
194/**
195 * sht15_wait_for_response() - checks for ack from device
196 * @data: device state
197 **/
198static int sht15_wait_for_response(struct sht15_data *data)
199{
200 gpio_direction_input(data->pdata->gpio_data);
201 gpio_set_value(data->pdata->gpio_sck, 1);
202 ndelay(SHT15_TSCKH);
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);
207 return -EIO;
208 }
209 gpio_set_value(data->pdata->gpio_sck, 0);
210 ndelay(SHT15_TSCKL);
211 return 0;
212}
213
214/**
215 * sht15_send_cmd() - Sends a command to the device.
216 * @data: device state
217 * @cmd: command byte to be sent
218 *
219 * On entry, sck is output low, data is output pull high
220 * and the interrupt disabled.
221 **/
222static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
223{
224 int ret = 0;
225 sht15_transmission_start(data);
226 sht15_send_byte(data, cmd);
227 ret = sht15_wait_for_response(data);
228 return ret;
229}
230/**
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.
236 **/
237static inline int sht15_update_single_val(struct sht15_data *data,
238 int command,
239 int timeout_msecs)
240{
241 int ret;
242 ret = sht15_send_cmd(data, command);
243 if (ret)
244 return ret;
245
246 gpio_direction_input(data->pdata->gpio_data);
247 atomic_set(&data->interrupt_handled, 0);
248
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);
255 }
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);
262 return -ETIME;
263 }
264 return 0;
265}
266
267/**
268 * sht15_update_vals() - get updated readings from device if too old
269 * @data: device state
270 **/
271static int sht15_update_vals(struct sht15_data *data)
272{
273 int ret = 0;
274 int timeout = HZ;
275
276 mutex_lock(&data->read_lock);
277 if (time_after(jiffies, data->last_updat + timeout)
278 || !data->valid) {
279 data->flag = SHT15_READING_HUMID;
280 ret = sht15_update_single_val(data, SHT15_MEASURE_RH, 160);
281 if (ret)
282 goto error_ret;
283 data->flag = SHT15_READING_TEMP;
284 ret = sht15_update_single_val(data, SHT15_MEASURE_TEMP, 400);
285 if (ret)
286 goto error_ret;
287 data->valid = 1;
288 data->last_updat = jiffies;
289 }
290error_ret:
291 mutex_unlock(&data->read_lock);
292
293 return ret;
294}
295
296/**
297 * sht15_calc_temp() - convert the raw reading to a temperature
298 * @data: device state
299 *
300 * As per section 4.3 of the data sheet.
301 **/
302static inline int sht15_calc_temp(struct sht15_data *data)
303{
304 int d1 = 0;
305 int i;
306
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;
314 break;
315 }
316
317 return data->val_temp*10 + d1;
318}
319
320/**
321 * sht15_calc_humid() - using last temperature convert raw to humid
322 * @data: device state
323 *
324 * This is the temperature compensated version as per section 4.2 of
325 * the data sheet.
326 **/
327static inline int sht15_calc_humid(struct sht15_data *data)
328{
329 int RHlinear; /* milli percent */
330 int temp = sht15_calc_temp(data);
331
332 const int c1 = -4;
333 const int c2 = 40500; /* x 10 ^ -6 */
334 const int c3 = 2800; /* x10 ^ -9 */
335
336 RHlinear = c1*1000
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;
341}
342
343static ssize_t sht15_show_temp(struct device *dev,
344 struct device_attribute *attr,
345 char *buf)
346{
347 int ret;
348 struct sht15_data *data = dev_get_drvdata(dev);
349
350 /* Technically no need to read humidity as well */
351 ret = sht15_update_vals(data);
352
353 return ret ? ret : sprintf(buf, "%d\n",
354 sht15_calc_temp(data));
355}
356
357static ssize_t sht15_show_humidity(struct device *dev,
358 struct device_attribute *attr,
359 char *buf)
360{
361 int ret;
362 struct sht15_data *data = dev_get_drvdata(dev);
363
364 ret = sht15_update_vals(data);
365
366 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
367
368};
369static ssize_t show_name(struct device *dev,
370 struct device_attribute *attr,
371 char *buf)
372{
373 struct platform_device *pdev = to_platform_device(dev);
374 return sprintf(buf, "%s\n", pdev->name);
375}
376
377static SENSOR_DEVICE_ATTR(temp1_input,
378 S_IRUGO, sht15_show_temp,
379 NULL, 0);
380static SENSOR_DEVICE_ATTR(humidity1_input,
381 S_IRUGO, sht15_show_humidity,
382 NULL, 0);
383static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
384static struct attribute *sht15_attrs[] = {
385 &sensor_dev_attr_temp1_input.dev_attr.attr,
386 &sensor_dev_attr_humidity1_input.dev_attr.attr,
387 &dev_attr_name.attr,
388 NULL,
389};
390
391static const struct attribute_group sht15_attr_group = {
392 .attrs = sht15_attrs,
393};
394
395static irqreturn_t sht15_interrupt_fired(int irq, void *d)
396{
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);
404 return IRQ_HANDLED;
405}
406
407/* Each byte of data is acknowledged by pulling the data line
408 * low for one clock pulse.
409 */
410static void sht15_ack(struct sht15_data *data)
411{
412 gpio_direction_output(data->pdata->gpio_data, 0);
413 ndelay(SHT15_TSU);
414 gpio_set_value(data->pdata->gpio_sck, 1);
415 ndelay(SHT15_TSU);
416 gpio_set_value(data->pdata->gpio_sck, 0);
417 ndelay(SHT15_TSU);
418 gpio_set_value(data->pdata->gpio_data, 1);
419
420 gpio_direction_input(data->pdata->gpio_data);
421}
422/**
423 * sht15_end_transmission() - notify device of end of transmission
424 * @data: device state
425 *
426 * This is basically a NAK. (single clock pulse, data high)
427 **/
428static void sht15_end_transmission(struct sht15_data *data)
429{
430 gpio_direction_output(data->pdata->gpio_data, 1);
431 ndelay(SHT15_TSU);
432 gpio_set_value(data->pdata->gpio_sck, 1);
433 ndelay(SHT15_TSCKH);
434 gpio_set_value(data->pdata->gpio_sck, 0);
435 ndelay(SHT15_TSCKL);
436}
437
438static void sht15_bh_read_data(struct work_struct *work_s)
439{
440 int i;
441 uint16_t val = 0;
442 struct sht15_data *data
443 = container_of(work_s, struct sht15_data,
444 read_work);
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
449 it hasn't!
450 */
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))
456 return;
457 }
458 /* Read the data back from the device */
459 for (i = 0; i < 16; ++i) {
460 val <<= 1;
461 gpio_set_value(data->pdata->gpio_sck, 1);
462 ndelay(SHT15_TSCKH);
463 val |= !!gpio_get_value(data->pdata->gpio_data);
464 gpio_set_value(data->pdata->gpio_sck, 0);
465 ndelay(SHT15_TSCKL);
466 if (i == 7)
467 sht15_ack(data);
468 }
469 /* Tell the device we are done */
470 sht15_end_transmission(data);
471
472 switch (data->flag) {
473 case SHT15_READING_TEMP:
474 data->val_temp = val;
475 break;
476 case SHT15_READING_HUMID:
477 data->val_humid = val;
478 break;
479 }
480
481 data->flag = SHT15_READING_NOTHING;
482 wake_up(&data->wait_queue);
483}
484
485static void sht15_update_voltage(struct work_struct *work_s)
486{
487 struct sht15_data *data
488 = container_of(work_s, struct sht15_data,
489 update_supply_work);
490 data->supply_uV = regulator_get_voltage(data->reg);
491}
492
493/**
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
498 *
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.
501 **/
502static int sht15_invalidate_voltage(struct notifier_block *nb,
503 unsigned long event,
504 void *ignored)
505{
506 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
507
508 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
509 data->supply_uV_valid = false;
510 schedule_work(&data->update_supply_work);
511
512 return NOTIFY_OK;
513}
514
515static int __devinit sht15_probe(struct platform_device *pdev)
516{
517 int ret = 0;
518 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
519
520 if (!data) {
521 ret = -ENOMEM;
522 dev_err(&pdev->dev, "kzalloc failed");
523 goto error_ret;
524 }
525
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);
532
533 if (pdev->dev.platform_data == NULL) {
534 dev_err(&pdev->dev, "no platform data supplied");
535 goto err_free_data;
536 }
537 data->pdata = pdev->dev.platform_data;
538 data->supply_uV = data->pdata->supply_mv*1000;
539
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);
549 }
550/* Try requesting the GPIOs */
551 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
552 if (ret) {
553 dev_err(&pdev->dev, "gpio request failed");
554 goto err_free_data;
555 }
556 gpio_direction_output(data->pdata->gpio_sck, 0);
557 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
558 if (ret) {
559 dev_err(&pdev->dev, "gpio request failed");
560 goto err_release_gpio_sck;
561 }
562 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
563 if (ret) {
564 dev_err(&pdev->dev, "sysfs create failed");
565 goto err_free_data;
566 }
567
568 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
569 sht15_interrupt_fired,
570 IRQF_TRIGGER_FALLING,
571 "sht15 data",
572 data);
573 if (ret) {
574 dev_err(&pdev->dev, "failed to get irq for data line");
575 goto err_release_gpio_data;
576 }
577 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
578 sht15_connection_reset(data);
579 sht15_send_cmd(data, 0x1E);
580
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;
585 }
586 return 0;
587
588err_release_gpio_data:
589 gpio_free(data->pdata->gpio_data);
590err_release_gpio_sck:
591 gpio_free(data->pdata->gpio_sck);
592err_free_data:
593 kfree(data);
594error_ret:
595
596 return ret;
597}
598
599static int __devexit sht15_remove(struct platform_device *pdev)
600{
601 struct sht15_data *data = platform_get_drvdata(pdev);
602
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);
612 }
613
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);
618 kfree(data);
619 return 0;
620}
621
622
623static struct platform_driver sht_drivers[] = {
624 {
625 .driver = {
626 .name = "sht10",
627 .owner = THIS_MODULE,
628 },
629 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200630 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700631 }, {
632 .driver = {
633 .name = "sht11",
634 .owner = THIS_MODULE,
635 },
636 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200637 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700638 }, {
639 .driver = {
640 .name = "sht15",
641 .owner = THIS_MODULE,
642 },
643 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200644 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700645 }, {
646 .driver = {
647 .name = "sht71",
648 .owner = THIS_MODULE,
649 },
650 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200651 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700652 }, {
653 .driver = {
654 .name = "sht75",
655 .owner = THIS_MODULE,
656 },
657 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200658 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700659 },
660};
661
662
663static int __init sht15_init(void)
664{
665 int ret;
666 int i;
667
668 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
669 ret = platform_driver_register(&sht_drivers[i]);
670 if (ret)
671 goto error_unreg;
672 }
673
674 return 0;
675
676error_unreg:
677 while (--i >= 0)
678 platform_driver_unregister(&sht_drivers[i]);
679
680 return ret;
681}
682module_init(sht15_init);
683
684static void __exit sht15_exit(void)
685{
686 int i;
687 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
688 platform_driver_unregister(&sht_drivers[i]);
689}
690module_exit(sht15_exit);
691
692MODULE_LICENSE("GPL");