staging: rtl8192e: Delete unused struct members
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / sht15.c
bloba610e7880fb3e5498b5d836d456ec4e25c58e278
1 /*
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.
15 * Heater not enabled.
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 */
54 /**
55 * struct sht15_temppair - elements of voltage dependant temp calc
56 * @vdd: supply voltage in microvolts
57 * @d1: see data sheet
59 struct sht15_temppair {
60 int vdd; /* microvolts */
61 int d1;
64 /* Table 9 from data sheet - relates temperature calculation
65 * to supply voltage.
67 static const struct sht15_temppair temppoints[] = {
68 { 2500000, -39400 },
69 { 3000000, -39600 },
70 { 3500000, -39700 },
71 { 4000000, -39800 },
72 { 5000000, -40100 },
75 /**
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
86 * at a time.
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
99 struct sht15_data {
100 struct sht15_platform_data *pdata;
101 struct work_struct read_work;
102 wait_queue_head_t wait_queue;
103 uint16_t val_temp;
104 uint16_t val_humid;
105 u8 flag;
106 u8 valid;
107 unsigned long last_updat;
108 struct mutex read_lock;
109 struct device *dev;
110 struct device *hwmon_dev;
111 struct regulator *reg;
112 struct notifier_block nb;
113 int supply_uV;
114 int supply_uV_valid;
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)
127 int i;
128 gpio_direction_output(data->pdata->gpio_data, 1);
129 ndelay(SHT15_TSCKL);
130 gpio_set_value(data->pdata->gpio_sck, 0);
131 ndelay(SHT15_TSCKL);
132 for (i = 0; i < 9; ++i) {
133 gpio_set_value(data->pdata->gpio_sck, 1);
134 ndelay(SHT15_TSCKH);
135 gpio_set_value(data->pdata->gpio_sck, 0);
136 ndelay(SHT15_TSCKL);
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);
148 ndelay(SHT15_TSU);
149 gpio_set_value(data->pdata->gpio_sck, 1);
150 ndelay(SHT15_TSCKH);
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);
167 ndelay(SHT15_TSU);
168 gpio_set_value(data->pdata->gpio_sck, 0);
169 ndelay(SHT15_TSCKL);
170 gpio_set_value(data->pdata->gpio_sck, 1);
171 ndelay(SHT15_TSCKH);
172 gpio_set_value(data->pdata->gpio_data, 0);
173 ndelay(SHT15_TSU);
174 gpio_set_value(data->pdata->gpio_sck, 0);
175 ndelay(SHT15_TSCKL);
176 gpio_set_value(data->pdata->gpio_sck, 1);
177 ndelay(SHT15_TSCKH);
178 gpio_set_value(data->pdata->gpio_data, 1);
179 ndelay(SHT15_TSU);
180 gpio_set_value(data->pdata->gpio_sck, 0);
181 ndelay(SHT15_TSCKL);
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)
190 int i;
191 for (i = 0; i < 8; i++) {
192 sht15_send_bit(data, !!(byte & 0x80));
193 byte <<= 1;
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);
204 ndelay(SHT15_TSCKH);
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);
209 return -EIO;
211 gpio_set_value(data->pdata->gpio_sck, 0);
212 ndelay(SHT15_TSCKL);
213 return 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)
226 int ret = 0;
227 sht15_transmission_start(data);
228 sht15_send_byte(data, cmd);
229 ret = sht15_wait_for_response(data);
230 return ret;
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,
240 int command,
241 int timeout_msecs)
243 int ret;
244 ret = sht15_send_cmd(data, command);
245 if (ret)
246 return ret;
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);
264 return -ETIME;
266 return 0;
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)
275 int ret = 0;
276 int timeout = HZ;
278 mutex_lock(&data->read_lock);
279 if (time_after(jiffies, data->last_updat + timeout)
280 || !data->valid) {
281 data->flag = SHT15_READING_HUMID;
282 ret = sht15_update_single_val(data, SHT15_MEASURE_RH, 160);
283 if (ret)
284 goto error_ret;
285 data->flag = SHT15_READING_TEMP;
286 ret = sht15_update_single_val(data, SHT15_MEASURE_TEMP, 400);
287 if (ret)
288 goto error_ret;
289 data->valid = 1;
290 data->last_updat = jiffies;
292 error_ret:
293 mutex_unlock(&data->read_lock);
295 return ret;
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;
307 int i;
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;
316 break;
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
327 * the data sheet.
329 static inline int sht15_calc_humid(struct sht15_data *data)
331 int RHlinear; /* milli percent */
332 int temp = sht15_calc_temp(data);
334 const int c1 = -4;
335 const int c2 = 40500; /* x 10 ^ -6 */
336 const int c3 = -2800; /* x10 ^ -9 */
338 RHlinear = c1*1000
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,
347 char *buf)
349 int ret;
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,
361 char *buf)
363 int ret;
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,
373 char *buf)
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,
381 NULL, 0);
382 static SENSOR_DEVICE_ATTR(humidity1_input,
383 S_IRUGO, sht15_show_humidity,
384 NULL, 0);
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,
389 &dev_attr_name.attr,
390 NULL,
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);
406 return IRQ_HANDLED;
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);
415 ndelay(SHT15_TSU);
416 gpio_set_value(data->pdata->gpio_sck, 1);
417 ndelay(SHT15_TSU);
418 gpio_set_value(data->pdata->gpio_sck, 0);
419 ndelay(SHT15_TSU);
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);
433 ndelay(SHT15_TSU);
434 gpio_set_value(data->pdata->gpio_sck, 1);
435 ndelay(SHT15_TSCKH);
436 gpio_set_value(data->pdata->gpio_sck, 0);
437 ndelay(SHT15_TSCKL);
440 static void sht15_bh_read_data(struct work_struct *work_s)
442 int i;
443 uint16_t val = 0;
444 struct sht15_data *data
445 = container_of(work_s, struct sht15_data,
446 read_work);
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
451 it hasn't!
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))
458 return;
460 /* Read the data back from the device */
461 for (i = 0; i < 16; ++i) {
462 val <<= 1;
463 gpio_set_value(data->pdata->gpio_sck, 1);
464 ndelay(SHT15_TSCKH);
465 val |= !!gpio_get_value(data->pdata->gpio_data);
466 gpio_set_value(data->pdata->gpio_sck, 0);
467 ndelay(SHT15_TSCKL);
468 if (i == 7)
469 sht15_ack(data);
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;
477 break;
478 case SHT15_READING_HUMID:
479 data->val_humid = val;
480 break;
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,
491 update_supply_work);
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,
505 unsigned long event,
506 void *ignored)
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);
514 return NOTIFY_OK;
517 static int __devinit sht15_probe(struct platform_device *pdev)
519 int ret = 0;
520 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
522 if (!data) {
523 ret = -ENOMEM;
524 dev_err(&pdev->dev, "kzalloc failed");
525 goto error_ret;
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");
537 goto err_free_data;
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)) {
545 int voltage;
547 voltage = regulator_get_voltage(data->reg);
548 if (voltage)
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");
559 if (ret) {
560 dev_err(&pdev->dev, "gpio request failed");
561 goto err_free_data;
563 gpio_direction_output(data->pdata->gpio_sck, 0);
564 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
565 if (ret) {
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);
570 if (ret) {
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,
578 "sht15 data",
579 data);
580 if (ret) {
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;
593 return 0;
595 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);
601 err_free_data:
602 kfree(data);
603 error_ret:
605 return ret;
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);
627 kfree(data);
628 return 0;
633 * sht_drivers simultaneously refers to __devinit and __devexit function
634 * which causes spurious section mismatch warning. So use __refdata to
635 * get rid from this.
637 static struct platform_driver __refdata sht_drivers[] = {
639 .driver = {
640 .name = "sht10",
641 .owner = THIS_MODULE,
643 .probe = sht15_probe,
644 .remove = __devexit_p(sht15_remove),
645 }, {
646 .driver = {
647 .name = "sht11",
648 .owner = THIS_MODULE,
650 .probe = sht15_probe,
651 .remove = __devexit_p(sht15_remove),
652 }, {
653 .driver = {
654 .name = "sht15",
655 .owner = THIS_MODULE,
657 .probe = sht15_probe,
658 .remove = __devexit_p(sht15_remove),
659 }, {
660 .driver = {
661 .name = "sht71",
662 .owner = THIS_MODULE,
664 .probe = sht15_probe,
665 .remove = __devexit_p(sht15_remove),
666 }, {
667 .driver = {
668 .name = "sht75",
669 .owner = THIS_MODULE,
671 .probe = sht15_probe,
672 .remove = __devexit_p(sht15_remove),
677 static int __init sht15_init(void)
679 int ret;
680 int i;
682 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
683 ret = platform_driver_register(&sht_drivers[i]);
684 if (ret)
685 goto error_unreg;
688 return 0;
690 error_unreg:
691 while (--i >= 0)
692 platform_driver_unregister(&sht_drivers[i]);
694 return ret;
696 module_init(sht15_init);
698 static void __exit sht15_exit(void)
700 int i;
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");