aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/sht15.c
diff options
context:
space:
mode:
authorVivien Didelot <vivien.didelot@savoirfairelinux.com>2011-04-12 15:34:38 -0400
committerGuenter Roeck <guenter.roeck@ericsson.com>2011-05-19 11:19:35 -0400
commitcc15c7ebb424e45ba2c5ceecbe52d025219ee970 (patch)
treeb5f752332d07352bc3d38381c554a3d71beeb588 /drivers/hwmon/sht15.c
parent181148ae68799f5b17f09b418b276f56e9179e99 (diff)
hwmon: (sht15) add support for the status register
* Add support for: - Heater. - End of battery notice. - Ability not to reload from OTP. - Low resolution (12bit temp, 8bit humidity). * Add an utility function to read individual bytes from the device. Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Acked-by: Jonathan Cameron <jic23@cam.ac.uk> Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com>
Diffstat (limited to 'drivers/hwmon/sht15.c')
-rw-r--r--drivers/hwmon/sht15.c247
1 files changed, 214 insertions, 33 deletions
diff --git a/drivers/hwmon/sht15.c b/drivers/hwmon/sht15.c
index 3182b3f578e2..49da089b5de9 100644
--- a/drivers/hwmon/sht15.c
+++ b/drivers/hwmon/sht15.c
@@ -1,6 +1,9 @@
1/* 1/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor 2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 * 3 *
4 * Portions Copyright (c) 2010-2011 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6 *
4 * Copyright (c) 2009 Jonathan Cameron 7 * Copyright (c) 2009 Jonathan Cameron
5 * 8 *
6 * Copyright (c) 2007 Wouter Horre 9 * Copyright (c) 2007 Wouter Horre
@@ -33,6 +36,8 @@
33/* Commands */ 36/* Commands */
34#define SHT15_MEASURE_TEMP 0x03 37#define SHT15_MEASURE_TEMP 0x03
35#define SHT15_MEASURE_RH 0x05 38#define SHT15_MEASURE_RH 0x05
39#define SHT15_WRITE_STATUS 0x06
40#define SHT15_READ_STATUS 0x07
36#define SHT15_SOFT_RESET 0x1E 41#define SHT15_SOFT_RESET 0x1E
37 42
38/* Min timings */ 43/* Min timings */
@@ -41,6 +46,12 @@
41#define SHT15_TSU 150 /* (nsecs) data setup time */ 46#define SHT15_TSU 150 /* (nsecs) data setup time */
42#define SHT15_TSRST 11 /* (msecs) soft reset time */ 47#define SHT15_TSRST 11 /* (msecs) soft reset time */
43 48
49/* Status Register Bits */
50#define SHT15_STATUS_LOW_RESOLUTION 0x01
51#define SHT15_STATUS_NO_OTP_RELOAD 0x02
52#define SHT15_STATUS_HEATER 0x04
53#define SHT15_STATUS_LOW_BATTERY 0x40
54
44/* Actions the driver may be doing */ 55/* Actions the driver may be doing */
45enum sht15_state { 56enum sht15_state {
46 SHT15_READING_NOTHING, 57 SHT15_READING_NOTHING,
@@ -74,9 +85,12 @@ static const struct sht15_temppair temppoints[] = {
74 * @wait_queue: wait queue for getting values from device. 85 * @wait_queue: wait queue for getting values from device.
75 * @val_temp: last temperature value read from device. 86 * @val_temp: last temperature value read from device.
76 * @val_humid: last humidity value read from device. 87 * @val_humid: last humidity value read from device.
88 * @val_status: last status register value read from device.
77 * @state: state identifying the action the driver is doing. 89 * @state: state identifying the action the driver is doing.
78 * @measurements_valid: are the current stored measures valid (start condition). 90 * @measurements_valid: are the current stored measures valid (start condition).
91 * @status_valid: is the current stored status valid (start condition).
79 * @last_measurement: time of last measure. 92 * @last_measurement: time of last measure.
93 * @last_status: time of last status reading.
80 * @read_lock: mutex to ensure only one read in progress at a time. 94 * @read_lock: mutex to ensure only one read in progress at a time.
81 * @dev: associate device structure. 95 * @dev: associate device structure.
82 * @hwmon_dev: device associated with hwmon subsystem. 96 * @hwmon_dev: device associated with hwmon subsystem.
@@ -97,9 +111,12 @@ struct sht15_data {
97 wait_queue_head_t wait_queue; 111 wait_queue_head_t wait_queue;
98 uint16_t val_temp; 112 uint16_t val_temp;
99 uint16_t val_humid; 113 uint16_t val_humid;
114 u8 val_status;
100 enum sht15_state state; 115 enum sht15_state state;
101 bool measurements_valid; 116 bool measurements_valid;
117 bool status_valid;
102 unsigned long last_measurement; 118 unsigned long last_measurement;
119 unsigned long last_status;
103 struct mutex read_lock; 120 struct mutex read_lock;
104 struct device *dev; 121 struct device *dev;
105 struct device *hwmon_dev; 122 struct device *hwmon_dev;
@@ -244,11 +261,106 @@ static int sht15_soft_reset(struct sht15_data *data)
244 if (ret) 261 if (ret)
245 return ret; 262 return ret;
246 msleep(SHT15_TSRST); 263 msleep(SHT15_TSRST);
264 /* device resets default hardware status register value */
265 data->val_status = 0;
266
267 return ret;
268}
269
270/**
271 * sht15_end_transmission() - notify device of end of transmission
272 * @data: device state.
273 *
274 * This is basically a NAK (single clock pulse, data high).
275 */
276static void sht15_end_transmission(struct sht15_data *data)
277{
278 gpio_direction_output(data->pdata->gpio_data, 1);
279 ndelay(SHT15_TSU);
280 gpio_set_value(data->pdata->gpio_sck, 1);
281 ndelay(SHT15_TSCKH);
282 gpio_set_value(data->pdata->gpio_sck, 0);
283 ndelay(SHT15_TSCKL);
284}
285
286/**
287 * sht15_read_byte() - Read a byte back from the device
288 * @data: device state.
289 */
290static u8 sht15_read_byte(struct sht15_data *data)
291{
292 int i;
293 u8 byte = 0;
294
295 for (i = 0; i < 8; ++i) {
296 byte <<= 1;
297 gpio_set_value(data->pdata->gpio_sck, 1);
298 ndelay(SHT15_TSCKH);
299 byte |= !!gpio_get_value(data->pdata->gpio_data);
300 gpio_set_value(data->pdata->gpio_sck, 0);
301 ndelay(SHT15_TSCKL);
302 }
303 return byte;
304}
305
306/**
307 * sht15_send_status() - write the status register byte
308 * @data: sht15 specific data.
309 * @status: the byte to set the status register with.
310 *
311 * As described in figure 14 and table 5 of the datasheet.
312 */
313static int sht15_send_status(struct sht15_data *data, u8 status)
314{
315 int ret;
316
317 ret = sht15_send_cmd(data, SHT15_WRITE_STATUS);
318 if (ret)
319 return ret;
320 gpio_direction_output(data->pdata->gpio_data, 1);
321 ndelay(SHT15_TSU);
322 sht15_send_byte(data, status);
323 ret = sht15_wait_for_response(data);
324 if (ret)
325 return ret;
247 326
327 data->val_status = status;
248 return 0; 328 return 0;
249} 329}
250 330
251/** 331/**
332 * sht15_update_status() - get updated status register from device if too old
333 * @data: device instance specific data.
334 *
335 * As described in figure 15 and table 5 of the datasheet.
336 */
337static int sht15_update_status(struct sht15_data *data)
338{
339 int ret = 0;
340 u8 status;
341 int timeout = HZ;
342
343 mutex_lock(&data->read_lock);
344 if (time_after(jiffies, data->last_status + timeout)
345 || !data->status_valid) {
346 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
347 if (ret)
348 goto error_ret;
349 status = sht15_read_byte(data);
350
351 sht15_end_transmission(data);
352
353 data->val_status = status;
354 data->status_valid = true;
355 data->last_status = jiffies;
356 }
357error_ret:
358 mutex_unlock(&data->read_lock);
359
360 return ret;
361}
362
363/**
252 * sht15_measurement() - get a new value from device 364 * sht15_measurement() - get a new value from device
253 * @data: device instance specific data 365 * @data: device instance specific data
254 * @command: command sent to request value 366 * @command: command sent to request value
@@ -324,6 +436,7 @@ error_ret:
324static inline int sht15_calc_temp(struct sht15_data *data) 436static inline int sht15_calc_temp(struct sht15_data *data)
325{ 437{
326 int d1 = temppoints[0].d1; 438 int d1 = temppoints[0].d1;
439 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
327 int i; 440 int i;
328 441
329 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--) 442 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
@@ -336,7 +449,7 @@ static inline int sht15_calc_temp(struct sht15_data *data)
336 break; 449 break;
337 } 450 }
338 451
339 return data->val_temp * 10 + d1; 452 return data->val_temp * d2 + d1;
340} 453}
341 454
342/** 455/**
@@ -353,19 +466,86 @@ static inline int sht15_calc_humid(struct sht15_data *data)
353{ 466{
354 int rh_linear; /* milli percent */ 467 int rh_linear; /* milli percent */
355 int temp = sht15_calc_temp(data); 468 int temp = sht15_calc_temp(data);
356 469 int c2, c3;
470 int t2;
357 const int c1 = -4; 471 const int c1 = -4;
358 const int c2 = 40500; /* x 10 ^ -6 */ 472
359 const int c3 = -28; /* x 10 ^ -7 */ 473 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
474 c2 = 648000; /* x 10 ^ -6 */
475 c3 = -7200; /* x 10 ^ -7 */
476 t2 = 1280;
477 } else {
478 c2 = 40500; /* x 10 ^ -6 */
479 c3 = -28; /* x 10 ^ -7 */
480 t2 = 80;
481 }
360 482
361 rh_linear = c1 * 1000 483 rh_linear = c1 * 1000
362 + c2 * data->val_humid / 1000 484 + c2 * data->val_humid / 1000
363 + (data->val_humid * data->val_humid * c3) / 10000; 485 + (data->val_humid * data->val_humid * c3) / 10000;
364 return (temp - 25000) * (10000 + 80 * data->val_humid) 486 return (temp - 25000) * (10000 + t2 * data->val_humid)
365 / 1000000 + rh_linear; 487 / 1000000 + rh_linear;
366} 488}
367 489
368/** 490/**
491 * sht15_show_status() - show status information in sysfs
492 * @dev: device.
493 * @attr: device attribute.
494 * @buf: sysfs buffer where information is written to.
495 *
496 * Will be called on read access to temp1_fault, humidity1_fault
497 * and heater_enable sysfs attributes.
498 * Returns number of bytes written into buffer, negative errno on error.
499 */
500static ssize_t sht15_show_status(struct device *dev,
501 struct device_attribute *attr,
502 char *buf)
503{
504 int ret;
505 struct sht15_data *data = dev_get_drvdata(dev);
506 u8 bit = to_sensor_dev_attr(attr)->index;
507
508 ret = sht15_update_status(data);
509
510 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
511}
512
513/**
514 * sht15_store_heater() - change heater state via sysfs
515 * @dev: device.
516 * @attr: device attribute.
517 * @buf: sysfs buffer to read the new heater state from.
518 * @count: length of the data.
519 *
520 * Will be called on read access to heater_enable sysfs attribute.
521 * Returns number of bytes actually decoded, negative errno on error.
522 */
523static ssize_t sht15_store_heater(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
526{
527 int ret;
528 struct sht15_data *data = dev_get_drvdata(dev);
529 long value;
530 u8 status;
531
532 if (strict_strtol(buf, 10, &value))
533 return -EINVAL;
534
535 mutex_lock(&data->read_lock);
536 status = data->val_status & 0x07;
537 if (!!value)
538 status |= SHT15_STATUS_HEATER;
539 else
540 status &= ~SHT15_STATUS_HEATER;
541
542 ret = sht15_send_status(data, status);
543 mutex_unlock(&data->read_lock);
544
545 return ret ? ret : count;
546}
547
548/**
369 * sht15_show_temp() - show temperature measurement value in sysfs 549 * sht15_show_temp() - show temperature measurement value in sysfs
370 * @dev: device. 550 * @dev: device.
371 * @attr: device attribute. 551 * @attr: device attribute.
@@ -407,7 +587,6 @@ static ssize_t sht15_show_humidity(struct device *dev,
407 ret = sht15_update_measurements(data); 587 ret = sht15_update_measurements(data);
408 588
409 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data)); 589 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
410
411} 590}
412 591
413static ssize_t show_name(struct device *dev, 592static ssize_t show_name(struct device *dev,
@@ -422,10 +601,19 @@ static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
422 sht15_show_temp, NULL, 0); 601 sht15_show_temp, NULL, 0);
423static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, 602static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
424 sht15_show_humidity, NULL, 0); 603 sht15_show_humidity, NULL, 0);
604static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
605 SHT15_STATUS_LOW_BATTERY);
606static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
607 SHT15_STATUS_LOW_BATTERY);
608static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
609 sht15_store_heater, SHT15_STATUS_HEATER);
425static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 610static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
426static struct attribute *sht15_attrs[] = { 611static struct attribute *sht15_attrs[] = {
427 &sensor_dev_attr_temp1_input.dev_attr.attr, 612 &sensor_dev_attr_temp1_input.dev_attr.attr,
428 &sensor_dev_attr_humidity1_input.dev_attr.attr, 613 &sensor_dev_attr_humidity1_input.dev_attr.attr,
614 &sensor_dev_attr_temp1_fault.dev_attr.attr,
615 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
616 &sensor_dev_attr_heater_enable.dev_attr.attr,
429 &dev_attr_name.attr, 617 &dev_attr_name.attr,
430 NULL, 618 NULL,
431}; 619};
@@ -466,25 +654,8 @@ static void sht15_ack(struct sht15_data *data)
466 gpio_direction_input(data->pdata->gpio_data); 654 gpio_direction_input(data->pdata->gpio_data);
467} 655}
468 656
469/**
470 * sht15_end_transmission() - notify device of end of transmission
471 * @data: device state
472 *
473 * This is basically a NAK. (single clock pulse, data high)
474 */
475static void sht15_end_transmission(struct sht15_data *data)
476{
477 gpio_direction_output(data->pdata->gpio_data, 1);
478 ndelay(SHT15_TSU);
479 gpio_set_value(data->pdata->gpio_sck, 1);
480 ndelay(SHT15_TSCKH);
481 gpio_set_value(data->pdata->gpio_sck, 0);
482 ndelay(SHT15_TSCKL);
483}
484
485static void sht15_bh_read_data(struct work_struct *work_s) 657static void sht15_bh_read_data(struct work_struct *work_s)
486{ 658{
487 int i;
488 uint16_t val = 0; 659 uint16_t val = 0;
489 struct sht15_data *data 660 struct sht15_data *data
490 = container_of(work_s, struct sht15_data, 661 = container_of(work_s, struct sht15_data,
@@ -505,16 +676,10 @@ static void sht15_bh_read_data(struct work_struct *work_s)
505 } 676 }
506 677
507 /* Read the data back from the device */ 678 /* Read the data back from the device */
508 for (i = 0; i < 16; ++i) { 679 val = sht15_read_byte(data);
509 val <<= 1; 680 val <<= 8;
510 gpio_set_value(data->pdata->gpio_sck, 1); 681 sht15_ack(data);
511 ndelay(SHT15_TSCKH); 682 val |= sht15_read_byte(data);
512 val |= !!gpio_get_value(data->pdata->gpio_data);
513 gpio_set_value(data->pdata->gpio_sck, 0);
514 ndelay(SHT15_TSCKL);
515 if (i == 7)
516 sht15_ack(data);
517 }
518 683
519 /* Tell the device we are done */ 684 /* Tell the device we are done */
520 sht15_end_transmission(data); 685 sht15_end_transmission(data);
@@ -568,6 +733,7 @@ static int __devinit sht15_probe(struct platform_device *pdev)
568{ 733{
569 int ret = 0; 734 int ret = 0;
570 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL); 735 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
736 u8 status = 0;
571 737
572 if (!data) { 738 if (!data) {
573 ret = -ENOMEM; 739 ret = -ENOMEM;
@@ -588,6 +754,10 @@ static int __devinit sht15_probe(struct platform_device *pdev)
588 } 754 }
589 data->pdata = pdev->dev.platform_data; 755 data->pdata = pdev->dev.platform_data;
590 data->supply_uV = data->pdata->supply_mv * 1000; 756 data->supply_uV = data->pdata->supply_mv * 1000;
757 if (data->pdata->no_otp_reload)
758 status |= SHT15_STATUS_NO_OTP_RELOAD;
759 if (data->pdata->low_resolution)
760 status |= SHT15_STATUS_LOW_RESOLUTION;
591 761
592 /* 762 /*
593 * If a regulator is available, 763 * If a regulator is available,
@@ -646,6 +816,13 @@ static int __devinit sht15_probe(struct platform_device *pdev)
646 if (ret) 816 if (ret)
647 goto err_release_irq; 817 goto err_release_irq;
648 818
819 /* write status with platform data options */
820 if (status) {
821 ret = sht15_send_status(data, status);
822 if (ret)
823 goto err_release_irq;
824 }
825
649 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group); 826 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
650 if (ret) { 827 if (ret) {
651 dev_err(&pdev->dev, "sysfs create failed\n"); 828 dev_err(&pdev->dev, "sysfs create failed\n");
@@ -689,6 +866,10 @@ static int __devexit sht15_remove(struct platform_device *pdev)
689 * prevent new ones beginning 866 * prevent new ones beginning
690 */ 867 */
691 mutex_lock(&data->read_lock); 868 mutex_lock(&data->read_lock);
869 if (sht15_soft_reset(data)) {
870 mutex_unlock(&data->read_lock);
871 return -EFAULT;
872 }
692 hwmon_device_unregister(data->hwmon_dev); 873 hwmon_device_unregister(data->hwmon_dev);
693 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); 874 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
694 if (!IS_ERR(data->reg)) { 875 if (!IS_ERR(data->reg)) {