Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sht15.c
Go to the documentation of this file.
1 /*
2  * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3  *
4  * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
5  * Jerome Oufella <[email protected]>
6  * Vivien Didelot <[email protected]>
7  *
8  * Copyright (c) 2009 Jonathan Cameron
9  *
10  * Copyright (c) 2007 Wouter Horre
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * For further information, see the Documentation/hwmon/sht15 file.
17  */
18 
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/gpio.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/mutex.h>
28 #include <linux/platform_device.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/jiffies.h>
32 #include <linux/err.h>
34 #include <linux/slab.h>
35 #include <linux/atomic.h>
36 
37 /* Commands */
38 #define SHT15_MEASURE_TEMP 0x03
39 #define SHT15_MEASURE_RH 0x05
40 #define SHT15_WRITE_STATUS 0x06
41 #define SHT15_READ_STATUS 0x07
42 #define SHT15_SOFT_RESET 0x1E
43 
44 /* Min timings */
45 #define SHT15_TSCKL 100 /* (nsecs) clock low */
46 #define SHT15_TSCKH 100 /* (nsecs) clock high */
47 #define SHT15_TSU 150 /* (nsecs) data setup time */
48 #define SHT15_TSRST 11 /* (msecs) soft reset time */
49 
50 /* Status Register Bits */
51 #define SHT15_STATUS_LOW_RESOLUTION 0x01
52 #define SHT15_STATUS_NO_OTP_RELOAD 0x02
53 #define SHT15_STATUS_HEATER 0x04
54 #define SHT15_STATUS_LOW_BATTERY 0x40
55 
56 /* List of supported chips */
58 
59 /* Actions the driver may be doing */
64 };
65 
72  int vdd; /* microvolts */
73  int d1;
74 };
75 
76 /* Table 9 from datasheet - relates temperature calculation to supply voltage */
77 static const struct sht15_temppair temppoints[] = {
78  { 2500000, -39400 },
79  { 3000000, -39600 },
80  { 3500000, -39700 },
81  { 4000000, -39800 },
82  { 5000000, -40100 },
83 };
84 
85 /* Table from CRC datasheet, section 2.4 */
86 static const u8 sht15_crc8_table[] = {
87  0, 49, 98, 83, 196, 245, 166, 151,
88  185, 136, 219, 234, 125, 76, 31, 46,
89  67, 114, 33, 16, 135, 182, 229, 212,
90  250, 203, 152, 169, 62, 15, 92, 109,
91  134, 183, 228, 213, 66, 115, 32, 17,
92  63, 14, 93, 108, 251, 202, 153, 168,
93  197, 244, 167, 150, 1, 48, 99, 82,
94  124, 77, 30, 47, 184, 137, 218, 235,
95  61, 12, 95, 110, 249, 200, 155, 170,
96  132, 181, 230, 215, 64, 113, 34, 19,
97  126, 79, 28, 45, 186, 139, 216, 233,
98  199, 246, 165, 148, 3, 50, 97, 80,
99  187, 138, 217, 232, 127, 78, 29, 44,
100  2, 51, 96, 81, 198, 247, 164, 149,
101  248, 201, 154, 171, 60, 13, 94, 111,
102  65, 112, 35, 18, 133, 180, 231, 214,
103  122, 75, 24, 41, 190, 143, 220, 237,
104  195, 242, 161, 144, 7, 54, 101, 84,
105  57, 8, 91, 106, 253, 204, 159, 174,
106  128, 177, 226, 211, 68, 117, 38, 23,
107  252, 205, 158, 175, 56, 9, 90, 107,
108  69, 116, 39, 22, 129, 176, 227, 210,
109  191, 142, 221, 236, 123, 74, 25, 40,
110  6, 55, 100, 85, 194, 243, 160, 145,
111  71, 118, 37, 20, 131, 178, 225, 208,
112  254, 207, 156, 173, 58, 11, 88, 105,
113  4, 53, 102, 87, 192, 241, 162, 147,
114  189, 140, 223, 238, 121, 72, 27, 42,
115  193, 240, 163, 146, 5, 52, 103, 86,
116  120, 73, 26, 43, 188, 141, 222, 239,
117  130, 179, 224, 209, 70, 119, 36, 21,
118  59, 10, 89, 104, 255, 206, 157, 172
119 };
120 
150 struct sht15_data {
162  unsigned long last_measurement;
163  unsigned long last_status;
164  struct mutex read_lock;
165  struct device *dev;
166  struct device *hwmon_dev;
167  struct regulator *reg;
173 };
174 
179 static u8 sht15_reverse(u8 byte)
180 {
181  u8 i, c;
182 
183  for (c = 0, i = 0; i < 8; i++)
184  c |= (!!(byte & (1 << i))) << (7 - i);
185  return c;
186 }
187 
195 static u8 sht15_crc8(struct sht15_data *data,
196  const u8 *value,
197  int len)
198 {
199  u8 crc = sht15_reverse(data->val_status & 0x0F);
200 
201  while (len--) {
202  crc = sht15_crc8_table[*value ^ crc];
203  value++;
204  }
205 
206  return crc;
207 }
208 
215 static void sht15_connection_reset(struct sht15_data *data)
216 {
217  int i;
218 
219  gpio_direction_output(data->pdata->gpio_data, 1);
221  gpio_set_value(data->pdata->gpio_sck, 0);
223  for (i = 0; i < 9; ++i) {
224  gpio_set_value(data->pdata->gpio_sck, 1);
226  gpio_set_value(data->pdata->gpio_sck, 0);
228  }
229 }
230 
236 static inline void sht15_send_bit(struct sht15_data *data, int val)
237 {
238  gpio_set_value(data->pdata->gpio_data, val);
239  ndelay(SHT15_TSU);
240  gpio_set_value(data->pdata->gpio_sck, 1);
242  gpio_set_value(data->pdata->gpio_sck, 0);
243  ndelay(SHT15_TSCKL); /* clock low time */
244 }
245 
254 static void sht15_transmission_start(struct sht15_data *data)
255 {
256  /* ensure data is high and output */
257  gpio_direction_output(data->pdata->gpio_data, 1);
258  ndelay(SHT15_TSU);
259  gpio_set_value(data->pdata->gpio_sck, 0);
261  gpio_set_value(data->pdata->gpio_sck, 1);
263  gpio_set_value(data->pdata->gpio_data, 0);
264  ndelay(SHT15_TSU);
265  gpio_set_value(data->pdata->gpio_sck, 0);
267  gpio_set_value(data->pdata->gpio_sck, 1);
269  gpio_set_value(data->pdata->gpio_data, 1);
270  ndelay(SHT15_TSU);
271  gpio_set_value(data->pdata->gpio_sck, 0);
273 }
274 
280 static void sht15_send_byte(struct sht15_data *data, u8 byte)
281 {
282  int i;
283 
284  for (i = 0; i < 8; i++) {
285  sht15_send_bit(data, !!(byte & 0x80));
286  byte <<= 1;
287  }
288 }
289 
294 static int sht15_wait_for_response(struct sht15_data *data)
295 {
296  gpio_direction_input(data->pdata->gpio_data);
297  gpio_set_value(data->pdata->gpio_sck, 1);
299  if (gpio_get_value(data->pdata->gpio_data)) {
300  gpio_set_value(data->pdata->gpio_sck, 0);
301  dev_err(data->dev, "Command not acknowledged\n");
302  sht15_connection_reset(data);
303  return -EIO;
304  }
305  gpio_set_value(data->pdata->gpio_sck, 0);
307  return 0;
308 }
309 
318 static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
319 {
320  int ret = 0;
321 
322  sht15_transmission_start(data);
323  sht15_send_byte(data, cmd);
324  ret = sht15_wait_for_response(data);
325  return ret;
326 }
327 
334 static int sht15_soft_reset(struct sht15_data *data)
335 {
336  int ret;
337 
338  ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
339  if (ret)
340  return ret;
342  /* device resets default hardware status register value */
343  data->val_status = 0;
344 
345  return ret;
346 }
347 
355 static void sht15_ack(struct sht15_data *data)
356 {
357  gpio_direction_output(data->pdata->gpio_data, 0);
358  ndelay(SHT15_TSU);
359  gpio_set_value(data->pdata->gpio_sck, 1);
360  ndelay(SHT15_TSU);
361  gpio_set_value(data->pdata->gpio_sck, 0);
362  ndelay(SHT15_TSU);
363  gpio_set_value(data->pdata->gpio_data, 1);
364 
365  gpio_direction_input(data->pdata->gpio_data);
366 }
367 
374 static void sht15_end_transmission(struct sht15_data *data)
375 {
376  gpio_direction_output(data->pdata->gpio_data, 1);
377  ndelay(SHT15_TSU);
378  gpio_set_value(data->pdata->gpio_sck, 1);
380  gpio_set_value(data->pdata->gpio_sck, 0);
382 }
383 
388 static u8 sht15_read_byte(struct sht15_data *data)
389 {
390  int i;
391  u8 byte = 0;
392 
393  for (i = 0; i < 8; ++i) {
394  byte <<= 1;
395  gpio_set_value(data->pdata->gpio_sck, 1);
397  byte |= !!gpio_get_value(data->pdata->gpio_data);
398  gpio_set_value(data->pdata->gpio_sck, 0);
400  }
401  return byte;
402 }
403 
411 static int sht15_send_status(struct sht15_data *data, u8 status)
412 {
413  int ret;
414 
415  ret = sht15_send_cmd(data, SHT15_WRITE_STATUS);
416  if (ret)
417  return ret;
418  gpio_direction_output(data->pdata->gpio_data, 1);
419  ndelay(SHT15_TSU);
420  sht15_send_byte(data, status);
421  ret = sht15_wait_for_response(data);
422  if (ret)
423  return ret;
424 
425  data->val_status = status;
426  return 0;
427 }
428 
435 static int sht15_update_status(struct sht15_data *data)
436 {
437  int ret = 0;
438  u8 status;
439  u8 previous_config;
440  u8 dev_checksum = 0;
441  u8 checksum_vals[2];
442  int timeout = HZ;
443 
444  mutex_lock(&data->read_lock);
445  if (time_after(jiffies, data->last_status + timeout)
446  || !data->status_valid) {
447  ret = sht15_send_cmd(data, SHT15_READ_STATUS);
448  if (ret)
449  goto error_ret;
450  status = sht15_read_byte(data);
451 
452  if (data->checksumming) {
453  sht15_ack(data);
454  dev_checksum = sht15_reverse(sht15_read_byte(data));
455  checksum_vals[0] = SHT15_READ_STATUS;
456  checksum_vals[1] = status;
457  data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
458  == dev_checksum);
459  }
460 
461  sht15_end_transmission(data);
462 
463  /*
464  * Perform checksum validation on the received data.
465  * Specification mentions that in case a checksum verification
466  * fails, a soft reset command must be sent to the device.
467  */
468  if (data->checksumming && !data->checksum_ok) {
469  previous_config = data->val_status & 0x07;
470  ret = sht15_soft_reset(data);
471  if (ret)
472  goto error_ret;
473  if (previous_config) {
474  ret = sht15_send_status(data, previous_config);
475  if (ret) {
476  dev_err(data->dev,
477  "CRC validation failed, unable "
478  "to restore device settings\n");
479  goto error_ret;
480  }
481  }
482  ret = -EAGAIN;
483  goto error_ret;
484  }
485 
486  data->val_status = status;
487  data->status_valid = true;
488  data->last_status = jiffies;
489  }
490 error_ret:
491  mutex_unlock(&data->read_lock);
492 
493  return ret;
494 }
495 
503 static int sht15_measurement(struct sht15_data *data,
504  int command,
505  int timeout_msecs)
506 {
507  int ret;
508  u8 previous_config;
509 
510  ret = sht15_send_cmd(data, command);
511  if (ret)
512  return ret;
513 
514  gpio_direction_input(data->pdata->gpio_data);
515  atomic_set(&data->interrupt_handled, 0);
516 
517  enable_irq(gpio_to_irq(data->pdata->gpio_data));
518  if (gpio_get_value(data->pdata->gpio_data) == 0) {
519  disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
520  /* Only relevant if the interrupt hasn't occurred. */
521  if (!atomic_read(&data->interrupt_handled))
522  schedule_work(&data->read_work);
523  }
524  ret = wait_event_timeout(data->wait_queue,
525  (data->state == SHT15_READING_NOTHING),
526  msecs_to_jiffies(timeout_msecs));
527  if (ret == 0) {/* timeout occurred */
528  disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
529  sht15_connection_reset(data);
530  return -ETIME;
531  }
532 
533  /*
534  * Perform checksum validation on the received data.
535  * Specification mentions that in case a checksum verification fails,
536  * a soft reset command must be sent to the device.
537  */
538  if (data->checksumming && !data->checksum_ok) {
539  previous_config = data->val_status & 0x07;
540  ret = sht15_soft_reset(data);
541  if (ret)
542  return ret;
543  if (previous_config) {
544  ret = sht15_send_status(data, previous_config);
545  if (ret) {
546  dev_err(data->dev,
547  "CRC validation failed, unable "
548  "to restore device settings\n");
549  return ret;
550  }
551  }
552  return -EAGAIN;
553  }
554 
555  return 0;
556 }
557 
562 static int sht15_update_measurements(struct sht15_data *data)
563 {
564  int ret = 0;
565  int timeout = HZ;
566 
567  mutex_lock(&data->read_lock);
568  if (time_after(jiffies, data->last_measurement + timeout)
569  || !data->measurements_valid) {
570  data->state = SHT15_READING_HUMID;
571  ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
572  if (ret)
573  goto error_ret;
574  data->state = SHT15_READING_TEMP;
575  ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
576  if (ret)
577  goto error_ret;
578  data->measurements_valid = true;
579  data->last_measurement = jiffies;
580  }
581 error_ret:
582  mutex_unlock(&data->read_lock);
583 
584  return ret;
585 }
586 
593 static inline int sht15_calc_temp(struct sht15_data *data)
594 {
595  int d1 = temppoints[0].d1;
596  int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
597  int i;
598 
599  for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
600  /* Find pointer to interpolate */
601  if (data->supply_uV > temppoints[i - 1].vdd) {
602  d1 = (data->supply_uV - temppoints[i - 1].vdd)
603  * (temppoints[i].d1 - temppoints[i - 1].d1)
604  / (temppoints[i].vdd - temppoints[i - 1].vdd)
605  + temppoints[i - 1].d1;
606  break;
607  }
608 
609  return data->val_temp * d2 + d1;
610 }
611 
622 static inline int sht15_calc_humid(struct sht15_data *data)
623 {
624  int rh_linear; /* milli percent */
625  int temp = sht15_calc_temp(data);
626  int c2, c3;
627  int t2;
628  const int c1 = -4;
629 
631  c2 = 648000; /* x 10 ^ -6 */
632  c3 = -7200; /* x 10 ^ -7 */
633  t2 = 1280;
634  } else {
635  c2 = 40500; /* x 10 ^ -6 */
636  c3 = -28; /* x 10 ^ -7 */
637  t2 = 80;
638  }
639 
640  rh_linear = c1 * 1000
641  + c2 * data->val_humid / 1000
642  + (data->val_humid * data->val_humid * c3) / 10000;
643  return (temp - 25000) * (10000 + t2 * data->val_humid)
644  / 1000000 + rh_linear;
645 }
646 
657 static ssize_t sht15_show_status(struct device *dev,
658  struct device_attribute *attr,
659  char *buf)
660 {
661  int ret;
662  struct sht15_data *data = dev_get_drvdata(dev);
663  u8 bit = to_sensor_dev_attr(attr)->index;
664 
665  ret = sht15_update_status(data);
666 
667  return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
668 }
669 
680 static ssize_t sht15_store_heater(struct device *dev,
681  struct device_attribute *attr,
682  const char *buf, size_t count)
683 {
684  int ret;
685  struct sht15_data *data = dev_get_drvdata(dev);
686  long value;
687  u8 status;
688 
689  if (kstrtol(buf, 10, &value))
690  return -EINVAL;
691 
692  mutex_lock(&data->read_lock);
693  status = data->val_status & 0x07;
694  if (!!value)
695  status |= SHT15_STATUS_HEATER;
696  else
697  status &= ~SHT15_STATUS_HEATER;
698 
699  ret = sht15_send_status(data, status);
700  mutex_unlock(&data->read_lock);
701 
702  return ret ? ret : count;
703 }
704 
714 static ssize_t sht15_show_temp(struct device *dev,
715  struct device_attribute *attr,
716  char *buf)
717 {
718  int ret;
719  struct sht15_data *data = dev_get_drvdata(dev);
720 
721  /* Technically no need to read humidity as well */
722  ret = sht15_update_measurements(data);
723 
724  return ret ? ret : sprintf(buf, "%d\n",
725  sht15_calc_temp(data));
726 }
727 
737 static ssize_t sht15_show_humidity(struct device *dev,
738  struct device_attribute *attr,
739  char *buf)
740 {
741  int ret;
742  struct sht15_data *data = dev_get_drvdata(dev);
743 
744  ret = sht15_update_measurements(data);
745 
746  return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
747 }
748 
749 static ssize_t show_name(struct device *dev,
750  struct device_attribute *attr,
751  char *buf)
752 {
753  struct platform_device *pdev = to_platform_device(dev);
754  return sprintf(buf, "%s\n", pdev->name);
755 }
756 
757 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
758  sht15_show_temp, NULL, 0);
759 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
760  sht15_show_humidity, NULL, 0);
761 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
763 static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
765 static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
766  sht15_store_heater, SHT15_STATUS_HEATER);
767 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
768 static struct attribute *sht15_attrs[] = {
769  &sensor_dev_attr_temp1_input.dev_attr.attr,
770  &sensor_dev_attr_humidity1_input.dev_attr.attr,
771  &sensor_dev_attr_temp1_fault.dev_attr.attr,
772  &sensor_dev_attr_humidity1_fault.dev_attr.attr,
773  &sensor_dev_attr_heater_enable.dev_attr.attr,
774  &dev_attr_name.attr,
775  NULL,
776 };
777 
778 static const struct attribute_group sht15_attr_group = {
779  .attrs = sht15_attrs,
780 };
781 
782 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
783 {
784  struct sht15_data *data = d;
785 
786  /* First disable the interrupt */
787  disable_irq_nosync(irq);
789  /* Then schedule a reading work struct */
790  if (data->state != SHT15_READING_NOTHING)
791  schedule_work(&data->read_work);
792  return IRQ_HANDLED;
793 }
794 
795 static void sht15_bh_read_data(struct work_struct *work_s)
796 {
797  uint16_t val = 0;
798  u8 dev_checksum = 0;
799  u8 checksum_vals[3];
800  struct sht15_data *data
801  = container_of(work_s, struct sht15_data,
802  read_work);
803 
804  /* Firstly, verify the line is low */
805  if (gpio_get_value(data->pdata->gpio_data)) {
806  /*
807  * If not, then start the interrupt again - care here as could
808  * have gone low in meantime so verify it hasn't!
809  */
810  atomic_set(&data->interrupt_handled, 0);
811  enable_irq(gpio_to_irq(data->pdata->gpio_data));
812  /* If still not occurred or another handler was scheduled */
813  if (gpio_get_value(data->pdata->gpio_data)
814  || atomic_read(&data->interrupt_handled))
815  return;
816  }
817 
818  /* Read the data back from the device */
819  val = sht15_read_byte(data);
820  val <<= 8;
821  sht15_ack(data);
822  val |= sht15_read_byte(data);
823 
824  if (data->checksumming) {
825  /*
826  * Ask the device for a checksum and read it back.
827  * Note: the device sends the checksum byte reversed.
828  */
829  sht15_ack(data);
830  dev_checksum = sht15_reverse(sht15_read_byte(data));
831  checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
833  checksum_vals[1] = (u8) (val >> 8);
834  checksum_vals[2] = (u8) val;
835  data->checksum_ok
836  = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
837  }
838 
839  /* Tell the device we are done */
840  sht15_end_transmission(data);
841 
842  switch (data->state) {
843  case SHT15_READING_TEMP:
844  data->val_temp = val;
845  break;
846  case SHT15_READING_HUMID:
847  data->val_humid = val;
848  break;
849  default:
850  break;
851  }
852 
854  wake_up(&data->wait_queue);
855 }
856 
857 static void sht15_update_voltage(struct work_struct *work_s)
858 {
859  struct sht15_data *data
860  = container_of(work_s, struct sht15_data,
862  data->supply_uV = regulator_get_voltage(data->reg);
863 }
864 
874 static int sht15_invalidate_voltage(struct notifier_block *nb,
875  unsigned long event,
876  void *ignored)
877 {
878  struct sht15_data *data = container_of(nb, struct sht15_data, nb);
879 
880  if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
881  data->supply_uV_valid = false;
883 
884  return NOTIFY_OK;
885 }
886 
887 static int __devinit sht15_probe(struct platform_device *pdev)
888 {
889  int ret;
890  struct sht15_data *data;
891  u8 status = 0;
892 
893  data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
894  if (!data)
895  return -ENOMEM;
896 
897  INIT_WORK(&data->read_work, sht15_bh_read_data);
898  INIT_WORK(&data->update_supply_work, sht15_update_voltage);
899  platform_set_drvdata(pdev, data);
900  mutex_init(&data->read_lock);
901  data->dev = &pdev->dev;
903 
904  if (pdev->dev.platform_data == NULL) {
905  dev_err(&pdev->dev, "no platform data supplied\n");
906  return -EINVAL;
907  }
908  data->pdata = pdev->dev.platform_data;
909  data->supply_uV = data->pdata->supply_mv * 1000;
910  if (data->pdata->checksum)
911  data->checksumming = true;
912  if (data->pdata->no_otp_reload)
913  status |= SHT15_STATUS_NO_OTP_RELOAD;
914  if (data->pdata->low_resolution)
915  status |= SHT15_STATUS_LOW_RESOLUTION;
916 
917  /*
918  * If a regulator is available,
919  * query what the supply voltage actually is!
920  */
921  data->reg = devm_regulator_get(data->dev, "vcc");
922  if (!IS_ERR(data->reg)) {
923  int voltage;
924 
925  voltage = regulator_get_voltage(data->reg);
926  if (voltage)
927  data->supply_uV = voltage;
928 
929  regulator_enable(data->reg);
930  /*
931  * Setup a notifier block to update this if another device
932  * causes the voltage to change
933  */
934  data->nb.notifier_call = &sht15_invalidate_voltage;
935  ret = regulator_register_notifier(data->reg, &data->nb);
936  if (ret) {
937  dev_err(&pdev->dev,
938  "regulator notifier request failed\n");
939  regulator_disable(data->reg);
940  return ret;
941  }
942  }
943 
944  /* Try requesting the GPIOs */
945  ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_sck, "SHT15 sck");
946  if (ret) {
947  dev_err(&pdev->dev, "gpio request failed\n");
948  goto err_release_reg;
949  }
950  gpio_direction_output(data->pdata->gpio_sck, 0);
951 
952  ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
953  "SHT15 data");
954  if (ret) {
955  dev_err(&pdev->dev, "gpio request failed\n");
956  goto err_release_reg;
957  }
958 
959  ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
960  sht15_interrupt_fired,
962  "sht15 data",
963  data);
964  if (ret) {
965  dev_err(&pdev->dev, "failed to get irq for data line\n");
966  goto err_release_reg;
967  }
968  disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
969  sht15_connection_reset(data);
970  ret = sht15_soft_reset(data);
971  if (ret)
972  goto err_release_reg;
973 
974  /* write status with platform data options */
975  if (status) {
976  ret = sht15_send_status(data, status);
977  if (ret)
978  goto err_release_reg;
979  }
980 
981  ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
982  if (ret) {
983  dev_err(&pdev->dev, "sysfs create failed\n");
984  goto err_release_reg;
985  }
986 
987  data->hwmon_dev = hwmon_device_register(data->dev);
988  if (IS_ERR(data->hwmon_dev)) {
989  ret = PTR_ERR(data->hwmon_dev);
990  goto err_release_sysfs_group;
991  }
992 
993  return 0;
994 
995 err_release_sysfs_group:
996  sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
997 err_release_reg:
998  if (!IS_ERR(data->reg)) {
999  regulator_unregister_notifier(data->reg, &data->nb);
1000  regulator_disable(data->reg);
1001  }
1002  return ret;
1003 }
1004 
1005 static int __devexit sht15_remove(struct platform_device *pdev)
1006 {
1007  struct sht15_data *data = platform_get_drvdata(pdev);
1008 
1009  /*
1010  * Make sure any reads from the device are done and
1011  * prevent new ones beginning
1012  */
1013  mutex_lock(&data->read_lock);
1014  if (sht15_soft_reset(data)) {
1015  mutex_unlock(&data->read_lock);
1016  return -EFAULT;
1017  }
1019  sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1020  if (!IS_ERR(data->reg)) {
1021  regulator_unregister_notifier(data->reg, &data->nb);
1022  regulator_disable(data->reg);
1023  }
1024 
1025  mutex_unlock(&data->read_lock);
1026 
1027  return 0;
1028 }
1029 
1030 static struct platform_device_id sht15_device_ids[] = {
1031  { "sht10", sht10 },
1032  { "sht11", sht11 },
1033  { "sht15", sht15 },
1034  { "sht71", sht71 },
1035  { "sht75", sht75 },
1036  { }
1037 };
1038 MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1039 
1040 static struct platform_driver sht15_driver = {
1041  .driver = {
1042  .name = "sht15",
1043  .owner = THIS_MODULE,
1044  },
1045  .probe = sht15_probe,
1046  .remove = __devexit_p(sht15_remove),
1047  .id_table = sht15_device_ids,
1048 };
1049 module_platform_driver(sht15_driver);
1050 
1051 MODULE_LICENSE("GPL");
1052 MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");