Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/hwmon/sht15.c

  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 <jerome.oufella@savoirfairelinux.com>
  6  *          Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  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>
 27 #include <linux/platform_data/sht15.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>
 33 #include <linux/regulator/consumer.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 */
 57 enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
 58 
 59 /* Actions the driver may be doing */
 60 enum sht15_state {
 61         SHT15_READING_NOTHING,
 62         SHT15_READING_TEMP,
 63         SHT15_READING_HUMID
 64 };
 65 
 66 /**
 67  * struct sht15_temppair - elements of voltage dependent temp calc
 68  * @vdd:        supply voltage in microvolts
 69  * @d1:         see data sheet
 70  */
 71 struct sht15_temppair {
 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 
121 /**
122  * struct sht15_data - device instance specific data
123  * @pdata:              platform data (gpio's etc).
124  * @read_work:          bh of interrupt handler.
125  * @wait_queue:         wait queue for getting values from device.
126  * @val_temp:           last temperature value read from device.
127  * @val_humid:          last humidity value read from device.
128  * @val_status:         last status register value read from device.
129  * @checksum_ok:        last value read from the device passed CRC validation.
130  * @checksumming:       flag used to enable the data validation with CRC.
131  * @state:              state identifying the action the driver is doing.
132  * @measurements_valid: are the current stored measures valid (start condition).
133  * @status_valid:       is the current stored status valid (start condition).
134  * @last_measurement:   time of last measure.
135  * @last_status:        time of last status reading.
136  * @read_lock:          mutex to ensure only one read in progress at a time.
137  * @dev:                associate device structure.
138  * @hwmon_dev:          device associated with hwmon subsystem.
139  * @reg:                associated regulator (if specified).
140  * @nb:                 notifier block to handle notifications of voltage
141  *                      changes.
142  * @supply_uv:          local copy of supply voltage used to allow use of
143  *                      regulator consumer if available.
144  * @supply_uv_valid:    indicates that an updated value has not yet been
145  *                      obtained from the regulator and so any calculations
146  *                      based upon it will be invalid.
147  * @update_supply_work: work struct that is used to update the supply_uv.
148  * @interrupt_handled:  flag used to indicate a handler has been scheduled.
149  */
150 struct sht15_data {
151         struct sht15_platform_data      *pdata;
152         struct work_struct              read_work;
153         wait_queue_head_t               wait_queue;
154         uint16_t                        val_temp;
155         uint16_t                        val_humid;
156         u8                              val_status;
157         bool                            checksum_ok;
158         bool                            checksumming;
159         enum sht15_state                state;
160         bool                            measurements_valid;
161         bool                            status_valid;
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;
168         struct notifier_block           nb;
169         int                             supply_uv;
170         bool                            supply_uv_valid;
171         struct work_struct              update_supply_work;
172         atomic_t                        interrupt_handled;
173 };
174 
175 /**
176  * sht15_reverse() - reverse a byte
177  * @byte:    byte to reverse.
178  */
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 
188 /**
189  * sht15_crc8() - compute crc8
190  * @data:       sht15 specific data.
191  * @value:      sht15 retrieved data.
192  *
193  * This implements section 2 of the CRC datasheet.
194  */
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 
209 /**
210  * sht15_connection_reset() - reset the comms interface
211  * @data:       sht15 specific data
212  *
213  * This implements section 3.4 of the data sheet
214  */
215 static int sht15_connection_reset(struct sht15_data *data)
216 {
217         int i, err;
218 
219         err = gpio_direction_output(data->pdata->gpio_data, 1);
220         if (err)
221                 return err;
222         ndelay(SHT15_TSCKL);
223         gpio_set_value(data->pdata->gpio_sck, 0);
224         ndelay(SHT15_TSCKL);
225         for (i = 0; i < 9; ++i) {
226                 gpio_set_value(data->pdata->gpio_sck, 1);
227                 ndelay(SHT15_TSCKH);
228                 gpio_set_value(data->pdata->gpio_sck, 0);
229                 ndelay(SHT15_TSCKL);
230         }
231         return 0;
232 }
233 
234 /**
235  * sht15_send_bit() - send an individual bit to the device
236  * @data:       device state data
237  * @val:        value of bit to be sent
238  */
239 static inline void sht15_send_bit(struct sht15_data *data, int val)
240 {
241         gpio_set_value(data->pdata->gpio_data, val);
242         ndelay(SHT15_TSU);
243         gpio_set_value(data->pdata->gpio_sck, 1);
244         ndelay(SHT15_TSCKH);
245         gpio_set_value(data->pdata->gpio_sck, 0);
246         ndelay(SHT15_TSCKL); /* clock low time */
247 }
248 
249 /**
250  * sht15_transmission_start() - specific sequence for new transmission
251  * @data:       device state data
252  *
253  * Timings for this are not documented on the data sheet, so very
254  * conservative ones used in implementation. This implements
255  * figure 12 on the data sheet.
256  */
257 static int sht15_transmission_start(struct sht15_data *data)
258 {
259         int err;
260 
261         /* ensure data is high and output */
262         err = gpio_direction_output(data->pdata->gpio_data, 1);
263         if (err)
264                 return err;
265         ndelay(SHT15_TSU);
266         gpio_set_value(data->pdata->gpio_sck, 0);
267         ndelay(SHT15_TSCKL);
268         gpio_set_value(data->pdata->gpio_sck, 1);
269         ndelay(SHT15_TSCKH);
270         gpio_set_value(data->pdata->gpio_data, 0);
271         ndelay(SHT15_TSU);
272         gpio_set_value(data->pdata->gpio_sck, 0);
273         ndelay(SHT15_TSCKL);
274         gpio_set_value(data->pdata->gpio_sck, 1);
275         ndelay(SHT15_TSCKH);
276         gpio_set_value(data->pdata->gpio_data, 1);
277         ndelay(SHT15_TSU);
278         gpio_set_value(data->pdata->gpio_sck, 0);
279         ndelay(SHT15_TSCKL);
280         return 0;
281 }
282 
283 /**
284  * sht15_send_byte() - send a single byte to the device
285  * @data:       device state
286  * @byte:       value to be sent
287  */
288 static void sht15_send_byte(struct sht15_data *data, u8 byte)
289 {
290         int i;
291 
292         for (i = 0; i < 8; i++) {
293                 sht15_send_bit(data, !!(byte & 0x80));
294                 byte <<= 1;
295         }
296 }
297 
298 /**
299  * sht15_wait_for_response() - checks for ack from device
300  * @data:       device state
301  */
302 static int sht15_wait_for_response(struct sht15_data *data)
303 {
304         int err;
305 
306         err = gpio_direction_input(data->pdata->gpio_data);
307         if (err)
308                 return err;
309         gpio_set_value(data->pdata->gpio_sck, 1);
310         ndelay(SHT15_TSCKH);
311         if (gpio_get_value(data->pdata->gpio_data)) {
312                 gpio_set_value(data->pdata->gpio_sck, 0);
313                 dev_err(data->dev, "Command not acknowledged\n");
314                 err = sht15_connection_reset(data);
315                 if (err)
316                         return err;
317                 return -EIO;
318         }
319         gpio_set_value(data->pdata->gpio_sck, 0);
320         ndelay(SHT15_TSCKL);
321         return 0;
322 }
323 
324 /**
325  * sht15_send_cmd() - Sends a command to the device.
326  * @data:       device state
327  * @cmd:        command byte to be sent
328  *
329  * On entry, sck is output low, data is output pull high
330  * and the interrupt disabled.
331  */
332 static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
333 {
334         int err;
335 
336         err = sht15_transmission_start(data);
337         if (err)
338                 return err;
339         sht15_send_byte(data, cmd);
340         return sht15_wait_for_response(data);
341 }
342 
343 /**
344  * sht15_soft_reset() - send a soft reset command
345  * @data:       sht15 specific data.
346  *
347  * As described in section 3.2 of the datasheet.
348  */
349 static int sht15_soft_reset(struct sht15_data *data)
350 {
351         int ret;
352 
353         ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
354         if (ret)
355                 return ret;
356         msleep(SHT15_TSRST);
357         /* device resets default hardware status register value */
358         data->val_status = 0;
359 
360         return ret;
361 }
362 
363 /**
364  * sht15_ack() - send a ack
365  * @data:       sht15 specific data.
366  *
367  * Each byte of data is acknowledged by pulling the data line
368  * low for one clock pulse.
369  */
370 static int sht15_ack(struct sht15_data *data)
371 {
372         int err;
373 
374         err = gpio_direction_output(data->pdata->gpio_data, 0);
375         if (err)
376                 return err;
377         ndelay(SHT15_TSU);
378         gpio_set_value(data->pdata->gpio_sck, 1);
379         ndelay(SHT15_TSU);
380         gpio_set_value(data->pdata->gpio_sck, 0);
381         ndelay(SHT15_TSU);
382         gpio_set_value(data->pdata->gpio_data, 1);
383 
384         return gpio_direction_input(data->pdata->gpio_data);
385 }
386 
387 /**
388  * sht15_end_transmission() - notify device of end of transmission
389  * @data:       device state.
390  *
391  * This is basically a NAK (single clock pulse, data high).
392  */
393 static int sht15_end_transmission(struct sht15_data *data)
394 {
395         int err;
396 
397         err = gpio_direction_output(data->pdata->gpio_data, 1);
398         if (err)
399                 return err;
400         ndelay(SHT15_TSU);
401         gpio_set_value(data->pdata->gpio_sck, 1);
402         ndelay(SHT15_TSCKH);
403         gpio_set_value(data->pdata->gpio_sck, 0);
404         ndelay(SHT15_TSCKL);
405         return 0;
406 }
407 
408 /**
409  * sht15_read_byte() - Read a byte back from the device
410  * @data:       device state.
411  */
412 static u8 sht15_read_byte(struct sht15_data *data)
413 {
414         int i;
415         u8 byte = 0;
416 
417         for (i = 0; i < 8; ++i) {
418                 byte <<= 1;
419                 gpio_set_value(data->pdata->gpio_sck, 1);
420                 ndelay(SHT15_TSCKH);
421                 byte |= !!gpio_get_value(data->pdata->gpio_data);
422                 gpio_set_value(data->pdata->gpio_sck, 0);
423                 ndelay(SHT15_TSCKL);
424         }
425         return byte;
426 }
427 
428 /**
429  * sht15_send_status() - write the status register byte
430  * @data:       sht15 specific data.
431  * @status:     the byte to set the status register with.
432  *
433  * As described in figure 14 and table 5 of the datasheet.
434  */
435 static int sht15_send_status(struct sht15_data *data, u8 status)
436 {
437         int err;
438 
439         err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
440         if (err)
441                 return err;
442         err = gpio_direction_output(data->pdata->gpio_data, 1);
443         if (err)
444                 return err;
445         ndelay(SHT15_TSU);
446         sht15_send_byte(data, status);
447         err = sht15_wait_for_response(data);
448         if (err)
449                 return err;
450 
451         data->val_status = status;
452         return 0;
453 }
454 
455 /**
456  * sht15_update_status() - get updated status register from device if too old
457  * @data:       device instance specific data.
458  *
459  * As described in figure 15 and table 5 of the datasheet.
460  */
461 static int sht15_update_status(struct sht15_data *data)
462 {
463         int ret = 0;
464         u8 status;
465         u8 previous_config;
466         u8 dev_checksum = 0;
467         u8 checksum_vals[2];
468         int timeout = HZ;
469 
470         mutex_lock(&data->read_lock);
471         if (time_after(jiffies, data->last_status + timeout)
472                         || !data->status_valid) {
473                 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
474                 if (ret)
475                         goto unlock;
476                 status = sht15_read_byte(data);
477 
478                 if (data->checksumming) {
479                         sht15_ack(data);
480                         dev_checksum = sht15_reverse(sht15_read_byte(data));
481                         checksum_vals[0] = SHT15_READ_STATUS;
482                         checksum_vals[1] = status;
483                         data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
484                                         == dev_checksum);
485                 }
486 
487                 ret = sht15_end_transmission(data);
488                 if (ret)
489                         goto unlock;
490 
491                 /*
492                  * Perform checksum validation on the received data.
493                  * Specification mentions that in case a checksum verification
494                  * fails, a soft reset command must be sent to the device.
495                  */
496                 if (data->checksumming && !data->checksum_ok) {
497                         previous_config = data->val_status & 0x07;
498                         ret = sht15_soft_reset(data);
499                         if (ret)
500                                 goto unlock;
501                         if (previous_config) {
502                                 ret = sht15_send_status(data, previous_config);
503                                 if (ret) {
504                                         dev_err(data->dev,
505                                                 "CRC validation failed, unable "
506                                                 "to restore device settings\n");
507                                         goto unlock;
508                                 }
509                         }
510                         ret = -EAGAIN;
511                         goto unlock;
512                 }
513 
514                 data->val_status = status;
515                 data->status_valid = true;
516                 data->last_status = jiffies;
517         }
518 
519 unlock:
520         mutex_unlock(&data->read_lock);
521         return ret;
522 }
523 
524 /**
525  * sht15_measurement() - get a new value from device
526  * @data:               device instance specific data
527  * @command:            command sent to request value
528  * @timeout_msecs:      timeout after which comms are assumed
529  *                      to have failed are reset.
530  */
531 static int sht15_measurement(struct sht15_data *data,
532                              int command,
533                              int timeout_msecs)
534 {
535         int ret;
536         u8 previous_config;
537 
538         ret = sht15_send_cmd(data, command);
539         if (ret)
540                 return ret;
541 
542         ret = gpio_direction_input(data->pdata->gpio_data);
543         if (ret)
544                 return ret;
545         atomic_set(&data->interrupt_handled, 0);
546 
547         enable_irq(gpio_to_irq(data->pdata->gpio_data));
548         if (gpio_get_value(data->pdata->gpio_data) == 0) {
549                 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
550                 /* Only relevant if the interrupt hasn't occurred. */
551                 if (!atomic_read(&data->interrupt_handled))
552                         schedule_work(&data->read_work);
553         }
554         ret = wait_event_timeout(data->wait_queue,
555                                  (data->state == SHT15_READING_NOTHING),
556                                  msecs_to_jiffies(timeout_msecs));
557         if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
558                 data->state = SHT15_READING_NOTHING;
559                 return -EIO;
560         } else if (ret == 0) { /* timeout occurred */
561                 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
562                 ret = sht15_connection_reset(data);
563                 if (ret)
564                         return ret;
565                 return -ETIME;
566         }
567 
568         /*
569          *  Perform checksum validation on the received data.
570          *  Specification mentions that in case a checksum verification fails,
571          *  a soft reset command must be sent to the device.
572          */
573         if (data->checksumming && !data->checksum_ok) {
574                 previous_config = data->val_status & 0x07;
575                 ret = sht15_soft_reset(data);
576                 if (ret)
577                         return ret;
578                 if (previous_config) {
579                         ret = sht15_send_status(data, previous_config);
580                         if (ret) {
581                                 dev_err(data->dev,
582                                         "CRC validation failed, unable "
583                                         "to restore device settings\n");
584                                 return ret;
585                         }
586                 }
587                 return -EAGAIN;
588         }
589 
590         return 0;
591 }
592 
593 /**
594  * sht15_update_measurements() - get updated measures from device if too old
595  * @data:       device state
596  */
597 static int sht15_update_measurements(struct sht15_data *data)
598 {
599         int ret = 0;
600         int timeout = HZ;
601 
602         mutex_lock(&data->read_lock);
603         if (time_after(jiffies, data->last_measurement + timeout)
604             || !data->measurements_valid) {
605                 data->state = SHT15_READING_HUMID;
606                 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
607                 if (ret)
608                         goto unlock;
609                 data->state = SHT15_READING_TEMP;
610                 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
611                 if (ret)
612                         goto unlock;
613                 data->measurements_valid = true;
614                 data->last_measurement = jiffies;
615         }
616 
617 unlock:
618         mutex_unlock(&data->read_lock);
619         return ret;
620 }
621 
622 /**
623  * sht15_calc_temp() - convert the raw reading to a temperature
624  * @data:       device state
625  *
626  * As per section 4.3 of the data sheet.
627  */
628 static inline int sht15_calc_temp(struct sht15_data *data)
629 {
630         int d1 = temppoints[0].d1;
631         int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
632         int i;
633 
634         for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
635                 /* Find pointer to interpolate */
636                 if (data->supply_uv > temppoints[i - 1].vdd) {
637                         d1 = (data->supply_uv - temppoints[i - 1].vdd)
638                                 * (temppoints[i].d1 - temppoints[i - 1].d1)
639                                 / (temppoints[i].vdd - temppoints[i - 1].vdd)
640                                 + temppoints[i - 1].d1;
641                         break;
642                 }
643 
644         return data->val_temp * d2 + d1;
645 }
646 
647 /**
648  * sht15_calc_humid() - using last temperature convert raw to humid
649  * @data:       device state
650  *
651  * This is the temperature compensated version as per section 4.2 of
652  * the data sheet.
653  *
654  * The sensor is assumed to be V3, which is compatible with V4.
655  * Humidity conversion coefficients are shown in table 7 of the datasheet.
656  */
657 static inline int sht15_calc_humid(struct sht15_data *data)
658 {
659         int rh_linear; /* milli percent */
660         int temp = sht15_calc_temp(data);
661         int c2, c3;
662         int t2;
663         const int c1 = -4;
664 
665         if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
666                 c2 = 648000; /* x 10 ^ -6 */
667                 c3 = -7200;  /* x 10 ^ -7 */
668                 t2 = 1280;
669         } else {
670                 c2 = 40500;  /* x 10 ^ -6 */
671                 c3 = -28;    /* x 10 ^ -7 */
672                 t2 = 80;
673         }
674 
675         rh_linear = c1 * 1000
676                 + c2 * data->val_humid / 1000
677                 + (data->val_humid * data->val_humid * c3) / 10000;
678         return (temp - 25000) * (10000 + t2 * data->val_humid)
679                 / 1000000 + rh_linear;
680 }
681 
682 /**
683  * sht15_show_status() - show status information in sysfs
684  * @dev:        device.
685  * @attr:       device attribute.
686  * @buf:        sysfs buffer where information is written to.
687  *
688  * Will be called on read access to temp1_fault, humidity1_fault
689  * and heater_enable sysfs attributes.
690  * Returns number of bytes written into buffer, negative errno on error.
691  */
692 static ssize_t sht15_show_status(struct device *dev,
693                                  struct device_attribute *attr,
694                                  char *buf)
695 {
696         int ret;
697         struct sht15_data *data = dev_get_drvdata(dev);
698         u8 bit = to_sensor_dev_attr(attr)->index;
699 
700         ret = sht15_update_status(data);
701 
702         return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
703 }
704 
705 /**
706  * sht15_store_heater() - change heater state via sysfs
707  * @dev:        device.
708  * @attr:       device attribute.
709  * @buf:        sysfs buffer to read the new heater state from.
710  * @count:      length of the data.
711  *
712  * Will be called on write access to heater_enable sysfs attribute.
713  * Returns number of bytes actually decoded, negative errno on error.
714  */
715 static ssize_t sht15_store_heater(struct device *dev,
716                                   struct device_attribute *attr,
717                                   const char *buf, size_t count)
718 {
719         int ret;
720         struct sht15_data *data = dev_get_drvdata(dev);
721         long value;
722         u8 status;
723 
724         if (kstrtol(buf, 10, &value))
725                 return -EINVAL;
726 
727         mutex_lock(&data->read_lock);
728         status = data->val_status & 0x07;
729         if (!!value)
730                 status |= SHT15_STATUS_HEATER;
731         else
732                 status &= ~SHT15_STATUS_HEATER;
733 
734         ret = sht15_send_status(data, status);
735         mutex_unlock(&data->read_lock);
736 
737         return ret ? ret : count;
738 }
739 
740 /**
741  * sht15_show_temp() - show temperature measurement value in sysfs
742  * @dev:        device.
743  * @attr:       device attribute.
744  * @buf:        sysfs buffer where measurement values are written to.
745  *
746  * Will be called on read access to temp1_input sysfs attribute.
747  * Returns number of bytes written into buffer, negative errno on error.
748  */
749 static ssize_t sht15_show_temp(struct device *dev,
750                                struct device_attribute *attr,
751                                char *buf)
752 {
753         int ret;
754         struct sht15_data *data = dev_get_drvdata(dev);
755 
756         /* Technically no need to read humidity as well */
757         ret = sht15_update_measurements(data);
758 
759         return ret ? ret : sprintf(buf, "%d\n",
760                                    sht15_calc_temp(data));
761 }
762 
763 /**
764  * sht15_show_humidity() - show humidity measurement value in sysfs
765  * @dev:        device.
766  * @attr:       device attribute.
767  * @buf:        sysfs buffer where measurement values are written to.
768  *
769  * Will be called on read access to humidity1_input sysfs attribute.
770  * Returns number of bytes written into buffer, negative errno on error.
771  */
772 static ssize_t sht15_show_humidity(struct device *dev,
773                                    struct device_attribute *attr,
774                                    char *buf)
775 {
776         int ret;
777         struct sht15_data *data = dev_get_drvdata(dev);
778 
779         ret = sht15_update_measurements(data);
780 
781         return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
782 }
783 
784 static ssize_t show_name(struct device *dev,
785                          struct device_attribute *attr,
786                          char *buf)
787 {
788         struct platform_device *pdev = to_platform_device(dev);
789         return sprintf(buf, "%s\n", pdev->name);
790 }
791 
792 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
793                           sht15_show_temp, NULL, 0);
794 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
795                           sht15_show_humidity, NULL, 0);
796 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
797                           SHT15_STATUS_LOW_BATTERY);
798 static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
799                           SHT15_STATUS_LOW_BATTERY);
800 static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
801                           sht15_store_heater, SHT15_STATUS_HEATER);
802 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
803 static struct attribute *sht15_attrs[] = {
804         &sensor_dev_attr_temp1_input.dev_attr.attr,
805         &sensor_dev_attr_humidity1_input.dev_attr.attr,
806         &sensor_dev_attr_temp1_fault.dev_attr.attr,
807         &sensor_dev_attr_humidity1_fault.dev_attr.attr,
808         &sensor_dev_attr_heater_enable.dev_attr.attr,
809         &dev_attr_name.attr,
810         NULL,
811 };
812 
813 static const struct attribute_group sht15_attr_group = {
814         .attrs = sht15_attrs,
815 };
816 
817 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
818 {
819         struct sht15_data *data = d;
820 
821         /* First disable the interrupt */
822         disable_irq_nosync(irq);
823         atomic_inc(&data->interrupt_handled);
824         /* Then schedule a reading work struct */
825         if (data->state != SHT15_READING_NOTHING)
826                 schedule_work(&data->read_work);
827         return IRQ_HANDLED;
828 }
829 
830 static void sht15_bh_read_data(struct work_struct *work_s)
831 {
832         uint16_t val = 0;
833         u8 dev_checksum = 0;
834         u8 checksum_vals[3];
835         struct sht15_data *data
836                 = container_of(work_s, struct sht15_data,
837                                read_work);
838 
839         /* Firstly, verify the line is low */
840         if (gpio_get_value(data->pdata->gpio_data)) {
841                 /*
842                  * If not, then start the interrupt again - care here as could
843                  * have gone low in meantime so verify it hasn't!
844                  */
845                 atomic_set(&data->interrupt_handled, 0);
846                 enable_irq(gpio_to_irq(data->pdata->gpio_data));
847                 /* If still not occurred or another handler was scheduled */
848                 if (gpio_get_value(data->pdata->gpio_data)
849                     || atomic_read(&data->interrupt_handled))
850                         return;
851         }
852 
853         /* Read the data back from the device */
854         val = sht15_read_byte(data);
855         val <<= 8;
856         if (sht15_ack(data))
857                 goto wakeup;
858         val |= sht15_read_byte(data);
859 
860         if (data->checksumming) {
861                 /*
862                  * Ask the device for a checksum and read it back.
863                  * Note: the device sends the checksum byte reversed.
864                  */
865                 if (sht15_ack(data))
866                         goto wakeup;
867                 dev_checksum = sht15_reverse(sht15_read_byte(data));
868                 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
869                         SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
870                 checksum_vals[1] = (u8) (val >> 8);
871                 checksum_vals[2] = (u8) val;
872                 data->checksum_ok
873                         = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
874         }
875 
876         /* Tell the device we are done */
877         if (sht15_end_transmission(data))
878                 goto wakeup;
879 
880         switch (data->state) {
881         case SHT15_READING_TEMP:
882                 data->val_temp = val;
883                 break;
884         case SHT15_READING_HUMID:
885                 data->val_humid = val;
886                 break;
887         default:
888                 break;
889         }
890 
891         data->state = SHT15_READING_NOTHING;
892 wakeup:
893         wake_up(&data->wait_queue);
894 }
895 
896 static void sht15_update_voltage(struct work_struct *work_s)
897 {
898         struct sht15_data *data
899                 = container_of(work_s, struct sht15_data,
900                                update_supply_work);
901         data->supply_uv = regulator_get_voltage(data->reg);
902 }
903 
904 /**
905  * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
906  * @nb:         associated notification structure
907  * @event:      voltage regulator state change event code
908  * @ignored:    function parameter - ignored here
909  *
910  * Note that as the notification code holds the regulator lock, we have
911  * to schedule an update of the supply voltage rather than getting it directly.
912  */
913 static int sht15_invalidate_voltage(struct notifier_block *nb,
914                                     unsigned long event,
915                                     void *ignored)
916 {
917         struct sht15_data *data = container_of(nb, struct sht15_data, nb);
918 
919         if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
920                 data->supply_uv_valid = false;
921         schedule_work(&data->update_supply_work);
922 
923         return NOTIFY_OK;
924 }
925 
926 static int sht15_probe(struct platform_device *pdev)
927 {
928         int ret;
929         struct sht15_data *data;
930         u8 status = 0;
931 
932         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
933         if (!data)
934                 return -ENOMEM;
935 
936         INIT_WORK(&data->read_work, sht15_bh_read_data);
937         INIT_WORK(&data->update_supply_work, sht15_update_voltage);
938         platform_set_drvdata(pdev, data);
939         mutex_init(&data->read_lock);
940         data->dev = &pdev->dev;
941         init_waitqueue_head(&data->wait_queue);
942 
943         if (dev_get_platdata(&pdev->dev) == NULL) {
944                 dev_err(&pdev->dev, "no platform data supplied\n");
945                 return -EINVAL;
946         }
947         data->pdata = dev_get_platdata(&pdev->dev);
948         data->supply_uv = data->pdata->supply_mv * 1000;
949         if (data->pdata->checksum)
950                 data->checksumming = true;
951         if (data->pdata->no_otp_reload)
952                 status |= SHT15_STATUS_NO_OTP_RELOAD;
953         if (data->pdata->low_resolution)
954                 status |= SHT15_STATUS_LOW_RESOLUTION;
955 
956         /*
957          * If a regulator is available,
958          * query what the supply voltage actually is!
959          */
960         data->reg = devm_regulator_get_optional(data->dev, "vcc");
961         if (!IS_ERR(data->reg)) {
962                 int voltage;
963 
964                 voltage = regulator_get_voltage(data->reg);
965                 if (voltage)
966                         data->supply_uv = voltage;
967 
968                 ret = regulator_enable(data->reg);
969                 if (ret != 0) {
970                         dev_err(&pdev->dev,
971                                 "failed to enable regulator: %d\n", ret);
972                         return ret;
973                 }
974 
975                 /*
976                  * Setup a notifier block to update this if another device
977                  * causes the voltage to change
978                  */
979                 data->nb.notifier_call = &sht15_invalidate_voltage;
980                 ret = regulator_register_notifier(data->reg, &data->nb);
981                 if (ret) {
982                         dev_err(&pdev->dev,
983                                 "regulator notifier request failed\n");
984                         regulator_disable(data->reg);
985                         return ret;
986                 }
987         }
988 
989         /* Try requesting the GPIOs */
990         ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
991                         GPIOF_OUT_INIT_LOW, "SHT15 sck");
992         if (ret) {
993                 dev_err(&pdev->dev, "clock line GPIO request failed\n");
994                 goto err_release_reg;
995         }
996 
997         ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
998                                 "SHT15 data");
999         if (ret) {
1000                 dev_err(&pdev->dev, "data line GPIO request failed\n");
1001                 goto err_release_reg;
1002         }
1003 
1004         ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
1005                                sht15_interrupt_fired,
1006                                IRQF_TRIGGER_FALLING,
1007                                "sht15 data",
1008                                data);
1009         if (ret) {
1010                 dev_err(&pdev->dev, "failed to get irq for data line\n");
1011                 goto err_release_reg;
1012         }
1013         disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
1014         ret = sht15_connection_reset(data);
1015         if (ret)
1016                 goto err_release_reg;
1017         ret = sht15_soft_reset(data);
1018         if (ret)
1019                 goto err_release_reg;
1020 
1021         /* write status with platform data options */
1022         if (status) {
1023                 ret = sht15_send_status(data, status);
1024                 if (ret)
1025                         goto err_release_reg;
1026         }
1027 
1028         ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1029         if (ret) {
1030                 dev_err(&pdev->dev, "sysfs create failed\n");
1031                 goto err_release_reg;
1032         }
1033 
1034         data->hwmon_dev = hwmon_device_register(data->dev);
1035         if (IS_ERR(data->hwmon_dev)) {
1036                 ret = PTR_ERR(data->hwmon_dev);
1037                 goto err_release_sysfs_group;
1038         }
1039 
1040         return 0;
1041 
1042 err_release_sysfs_group:
1043         sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1044 err_release_reg:
1045         if (!IS_ERR(data->reg)) {
1046                 regulator_unregister_notifier(data->reg, &data->nb);
1047                 regulator_disable(data->reg);
1048         }
1049         return ret;
1050 }
1051 
1052 static int sht15_remove(struct platform_device *pdev)
1053 {
1054         struct sht15_data *data = platform_get_drvdata(pdev);
1055 
1056         /*
1057          * Make sure any reads from the device are done and
1058          * prevent new ones beginning
1059          */
1060         mutex_lock(&data->read_lock);
1061         if (sht15_soft_reset(data)) {
1062                 mutex_unlock(&data->read_lock);
1063                 return -EFAULT;
1064         }
1065         hwmon_device_unregister(data->hwmon_dev);
1066         sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1067         if (!IS_ERR(data->reg)) {
1068                 regulator_unregister_notifier(data->reg, &data->nb);
1069                 regulator_disable(data->reg);
1070         }
1071 
1072         mutex_unlock(&data->read_lock);
1073 
1074         return 0;
1075 }
1076 
1077 static struct platform_device_id sht15_device_ids[] = {
1078         { "sht10", sht10 },
1079         { "sht11", sht11 },
1080         { "sht15", sht15 },
1081         { "sht71", sht71 },
1082         { "sht75", sht75 },
1083         { }
1084 };
1085 MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1086 
1087 static struct platform_driver sht15_driver = {
1088         .driver = {
1089                 .name = "sht15",
1090                 .owner = THIS_MODULE,
1091         },
1092         .probe = sht15_probe,
1093         .remove = sht15_remove,
1094         .id_table = sht15_device_ids,
1095 };
1096 module_platform_driver(sht15_driver);
1097 
1098 MODULE_LICENSE("GPL");
1099 MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");
1100 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us