Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * sht15.c - support for the SHT15 Temperature and Humidity Sensor
0004  *
0005  * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
0006  *          Jerome Oufella <jerome.oufella@savoirfairelinux.com>
0007  *          Vivien Didelot <vivien.didelot@savoirfairelinux.com>
0008  *
0009  * Copyright (c) 2009 Jonathan Cameron
0010  *
0011  * Copyright (c) 2007 Wouter Horre
0012  *
0013  * For further information, see the Documentation/hwmon/sht15.rst file.
0014  */
0015 
0016 #include <linux/interrupt.h>
0017 #include <linux/irq.h>
0018 #include <linux/module.h>
0019 #include <linux/init.h>
0020 #include <linux/hwmon.h>
0021 #include <linux/hwmon-sysfs.h>
0022 #include <linux/mutex.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/sched.h>
0025 #include <linux/delay.h>
0026 #include <linux/jiffies.h>
0027 #include <linux/err.h>
0028 #include <linux/regulator/consumer.h>
0029 #include <linux/slab.h>
0030 #include <linux/atomic.h>
0031 #include <linux/bitrev.h>
0032 #include <linux/gpio/consumer.h>
0033 #include <linux/of.h>
0034 
0035 /* Commands */
0036 #define SHT15_MEASURE_TEMP      0x03
0037 #define SHT15_MEASURE_RH        0x05
0038 #define SHT15_WRITE_STATUS      0x06
0039 #define SHT15_READ_STATUS       0x07
0040 #define SHT15_SOFT_RESET        0x1E
0041 
0042 /* Min timings */
0043 #define SHT15_TSCKL         100 /* (nsecs) clock low */
0044 #define SHT15_TSCKH         100 /* (nsecs) clock high */
0045 #define SHT15_TSU           150 /* (nsecs) data setup time */
0046 #define SHT15_TSRST         11  /* (msecs) soft reset time */
0047 
0048 /* Status Register Bits */
0049 #define SHT15_STATUS_LOW_RESOLUTION 0x01
0050 #define SHT15_STATUS_NO_OTP_RELOAD  0x02
0051 #define SHT15_STATUS_HEATER     0x04
0052 #define SHT15_STATUS_LOW_BATTERY    0x40
0053 
0054 /* List of supported chips */
0055 enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
0056 
0057 /* Actions the driver may be doing */
0058 enum sht15_state {
0059     SHT15_READING_NOTHING,
0060     SHT15_READING_TEMP,
0061     SHT15_READING_HUMID
0062 };
0063 
0064 /**
0065  * struct sht15_temppair - elements of voltage dependent temp calc
0066  * @vdd:    supply voltage in microvolts
0067  * @d1:     see data sheet
0068  */
0069 struct sht15_temppair {
0070     int vdd; /* microvolts */
0071     int d1;
0072 };
0073 
0074 /* Table 9 from datasheet - relates temperature calculation to supply voltage */
0075 static const struct sht15_temppair temppoints[] = {
0076     { 2500000, -39400 },
0077     { 3000000, -39600 },
0078     { 3500000, -39700 },
0079     { 4000000, -39800 },
0080     { 5000000, -40100 },
0081 };
0082 
0083 /* Table from CRC datasheet, section 2.4 */
0084 static const u8 sht15_crc8_table[] = {
0085     0,  49, 98, 83, 196,    245,    166,    151,
0086     185,    136,    219,    234,    125,    76, 31, 46,
0087     67, 114,    33, 16, 135,    182,    229,    212,
0088     250,    203,    152,    169,    62, 15, 92, 109,
0089     134,    183,    228,    213,    66, 115,    32, 17,
0090     63, 14, 93, 108,    251,    202,    153,    168,
0091     197,    244,    167,    150,    1,  48, 99, 82,
0092     124,    77, 30, 47, 184,    137,    218,    235,
0093     61, 12, 95, 110,    249,    200,    155,    170,
0094     132,    181,    230,    215,    64, 113,    34, 19,
0095     126,    79, 28, 45, 186,    139,    216,    233,
0096     199,    246,    165,    148,    3,  50, 97, 80,
0097     187,    138,    217,    232,    127,    78, 29, 44,
0098     2,  51, 96, 81, 198,    247,    164,    149,
0099     248,    201,    154,    171,    60, 13, 94, 111,
0100     65, 112,    35, 18, 133,    180,    231,    214,
0101     122,    75, 24, 41, 190,    143,    220,    237,
0102     195,    242,    161,    144,    7,  54, 101,    84,
0103     57, 8,  91, 106,    253,    204,    159,    174,
0104     128,    177,    226,    211,    68, 117,    38, 23,
0105     252,    205,    158,    175,    56, 9,  90, 107,
0106     69, 116,    39, 22, 129,    176,    227,    210,
0107     191,    142,    221,    236,    123,    74, 25, 40,
0108     6,  55, 100,    85, 194,    243,    160,    145,
0109     71, 118,    37, 20, 131,    178,    225,    208,
0110     254,    207,    156,    173,    58, 11, 88, 105,
0111     4,  53, 102,    87, 192,    241,    162,    147,
0112     189,    140,    223,    238,    121,    72, 27, 42,
0113     193,    240,    163,    146,    5,  52, 103,    86,
0114     120,    73, 26, 43, 188,    141,    222,    239,
0115     130,    179,    224,    209,    70, 119,    36, 21,
0116     59, 10, 89, 104,    255,    206,    157,    172
0117 };
0118 
0119 /**
0120  * struct sht15_data - device instance specific data
0121  * @sck:        clock GPIO line
0122  * @data:       data GPIO line
0123  * @read_work:      bh of interrupt handler.
0124  * @wait_queue:     wait queue for getting values from device.
0125  * @val_temp:       last temperature value read from device.
0126  * @val_humid:      last humidity value read from device.
0127  * @val_status:     last status register value read from device.
0128  * @checksum_ok:    last value read from the device passed CRC validation.
0129  * @checksumming:   flag used to enable the data validation with CRC.
0130  * @state:      state identifying the action the driver is doing.
0131  * @measurements_valid: are the current stored measures valid (start condition).
0132  * @status_valid:   is the current stored status valid (start condition).
0133  * @last_measurement:   time of last measure.
0134  * @last_status:    time of last status reading.
0135  * @read_lock:      mutex to ensure only one read in progress at a time.
0136  * @dev:        associate device structure.
0137  * @hwmon_dev:      device associated with hwmon subsystem.
0138  * @reg:        associated regulator (if specified).
0139  * @nb:         notifier block to handle notifications of voltage
0140  *                      changes.
0141  * @supply_uv:      local copy of supply voltage used to allow use of
0142  *                      regulator consumer if available.
0143  * @supply_uv_valid:    indicates that an updated value has not yet been
0144  *          obtained from the regulator and so any calculations
0145  *          based upon it will be invalid.
0146  * @update_supply_work: work struct that is used to update the supply_uv.
0147  * @interrupt_handled:  flag used to indicate a handler has been scheduled.
0148  */
0149 struct sht15_data {
0150     struct gpio_desc        *sck;
0151     struct gpio_desc        *data;
0152     struct work_struct      read_work;
0153     wait_queue_head_t       wait_queue;
0154     uint16_t            val_temp;
0155     uint16_t            val_humid;
0156     u8              val_status;
0157     bool                checksum_ok;
0158     bool                checksumming;
0159     enum sht15_state        state;
0160     bool                measurements_valid;
0161     bool                status_valid;
0162     unsigned long           last_measurement;
0163     unsigned long           last_status;
0164     struct mutex            read_lock;
0165     struct device           *dev;
0166     struct device           *hwmon_dev;
0167     struct regulator        *reg;
0168     struct notifier_block       nb;
0169     int             supply_uv;
0170     bool                supply_uv_valid;
0171     struct work_struct      update_supply_work;
0172     atomic_t            interrupt_handled;
0173 };
0174 
0175 /**
0176  * sht15_crc8() - compute crc8
0177  * @data:   sht15 specific data.
0178  * @value:  sht15 retrieved data.
0179  * @len:    Length of retrieved data
0180  *
0181  * This implements section 2 of the CRC datasheet.
0182  */
0183 static u8 sht15_crc8(struct sht15_data *data,
0184         const u8 *value,
0185         int len)
0186 {
0187     u8 crc = bitrev8(data->val_status & 0x0F);
0188 
0189     while (len--) {
0190         crc = sht15_crc8_table[*value ^ crc];
0191         value++;
0192     }
0193 
0194     return crc;
0195 }
0196 
0197 /**
0198  * sht15_connection_reset() - reset the comms interface
0199  * @data:   sht15 specific data
0200  *
0201  * This implements section 3.4 of the data sheet
0202  */
0203 static int sht15_connection_reset(struct sht15_data *data)
0204 {
0205     int i, err;
0206 
0207     err = gpiod_direction_output(data->data, 1);
0208     if (err)
0209         return err;
0210     ndelay(SHT15_TSCKL);
0211     gpiod_set_value(data->sck, 0);
0212     ndelay(SHT15_TSCKL);
0213     for (i = 0; i < 9; ++i) {
0214         gpiod_set_value(data->sck, 1);
0215         ndelay(SHT15_TSCKH);
0216         gpiod_set_value(data->sck, 0);
0217         ndelay(SHT15_TSCKL);
0218     }
0219     return 0;
0220 }
0221 
0222 /**
0223  * sht15_send_bit() - send an individual bit to the device
0224  * @data:   device state data
0225  * @val:    value of bit to be sent
0226  */
0227 static inline void sht15_send_bit(struct sht15_data *data, int val)
0228 {
0229     gpiod_set_value(data->data, val);
0230     ndelay(SHT15_TSU);
0231     gpiod_set_value(data->sck, 1);
0232     ndelay(SHT15_TSCKH);
0233     gpiod_set_value(data->sck, 0);
0234     ndelay(SHT15_TSCKL); /* clock low time */
0235 }
0236 
0237 /**
0238  * sht15_transmission_start() - specific sequence for new transmission
0239  * @data:   device state data
0240  *
0241  * Timings for this are not documented on the data sheet, so very
0242  * conservative ones used in implementation. This implements
0243  * figure 12 on the data sheet.
0244  */
0245 static int sht15_transmission_start(struct sht15_data *data)
0246 {
0247     int err;
0248 
0249     /* ensure data is high and output */
0250     err = gpiod_direction_output(data->data, 1);
0251     if (err)
0252         return err;
0253     ndelay(SHT15_TSU);
0254     gpiod_set_value(data->sck, 0);
0255     ndelay(SHT15_TSCKL);
0256     gpiod_set_value(data->sck, 1);
0257     ndelay(SHT15_TSCKH);
0258     gpiod_set_value(data->data, 0);
0259     ndelay(SHT15_TSU);
0260     gpiod_set_value(data->sck, 0);
0261     ndelay(SHT15_TSCKL);
0262     gpiod_set_value(data->sck, 1);
0263     ndelay(SHT15_TSCKH);
0264     gpiod_set_value(data->data, 1);
0265     ndelay(SHT15_TSU);
0266     gpiod_set_value(data->sck, 0);
0267     ndelay(SHT15_TSCKL);
0268     return 0;
0269 }
0270 
0271 /**
0272  * sht15_send_byte() - send a single byte to the device
0273  * @data:   device state
0274  * @byte:   value to be sent
0275  */
0276 static void sht15_send_byte(struct sht15_data *data, u8 byte)
0277 {
0278     int i;
0279 
0280     for (i = 0; i < 8; i++) {
0281         sht15_send_bit(data, !!(byte & 0x80));
0282         byte <<= 1;
0283     }
0284 }
0285 
0286 /**
0287  * sht15_wait_for_response() - checks for ack from device
0288  * @data:   device state
0289  */
0290 static int sht15_wait_for_response(struct sht15_data *data)
0291 {
0292     int err;
0293 
0294     err = gpiod_direction_input(data->data);
0295     if (err)
0296         return err;
0297     gpiod_set_value(data->sck, 1);
0298     ndelay(SHT15_TSCKH);
0299     if (gpiod_get_value(data->data)) {
0300         gpiod_set_value(data->sck, 0);
0301         dev_err(data->dev, "Command not acknowledged\n");
0302         err = sht15_connection_reset(data);
0303         if (err)
0304             return err;
0305         return -EIO;
0306     }
0307     gpiod_set_value(data->sck, 0);
0308     ndelay(SHT15_TSCKL);
0309     return 0;
0310 }
0311 
0312 /**
0313  * sht15_send_cmd() - Sends a command to the device.
0314  * @data:   device state
0315  * @cmd:    command byte to be sent
0316  *
0317  * On entry, sck is output low, data is output pull high
0318  * and the interrupt disabled.
0319  */
0320 static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
0321 {
0322     int err;
0323 
0324     err = sht15_transmission_start(data);
0325     if (err)
0326         return err;
0327     sht15_send_byte(data, cmd);
0328     return sht15_wait_for_response(data);
0329 }
0330 
0331 /**
0332  * sht15_soft_reset() - send a soft reset command
0333  * @data:   sht15 specific data.
0334  *
0335  * As described in section 3.2 of the datasheet.
0336  */
0337 static int sht15_soft_reset(struct sht15_data *data)
0338 {
0339     int ret;
0340 
0341     ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
0342     if (ret)
0343         return ret;
0344     msleep(SHT15_TSRST);
0345     /* device resets default hardware status register value */
0346     data->val_status = 0;
0347 
0348     return ret;
0349 }
0350 
0351 /**
0352  * sht15_ack() - send a ack
0353  * @data:   sht15 specific data.
0354  *
0355  * Each byte of data is acknowledged by pulling the data line
0356  * low for one clock pulse.
0357  */
0358 static int sht15_ack(struct sht15_data *data)
0359 {
0360     int err;
0361 
0362     err = gpiod_direction_output(data->data, 0);
0363     if (err)
0364         return err;
0365     ndelay(SHT15_TSU);
0366     gpiod_set_value(data->sck, 1);
0367     ndelay(SHT15_TSU);
0368     gpiod_set_value(data->sck, 0);
0369     ndelay(SHT15_TSU);
0370     gpiod_set_value(data->data, 1);
0371 
0372     return gpiod_direction_input(data->data);
0373 }
0374 
0375 /**
0376  * sht15_end_transmission() - notify device of end of transmission
0377  * @data:   device state.
0378  *
0379  * This is basically a NAK (single clock pulse, data high).
0380  */
0381 static int sht15_end_transmission(struct sht15_data *data)
0382 {
0383     int err;
0384 
0385     err = gpiod_direction_output(data->data, 1);
0386     if (err)
0387         return err;
0388     ndelay(SHT15_TSU);
0389     gpiod_set_value(data->sck, 1);
0390     ndelay(SHT15_TSCKH);
0391     gpiod_set_value(data->sck, 0);
0392     ndelay(SHT15_TSCKL);
0393     return 0;
0394 }
0395 
0396 /**
0397  * sht15_read_byte() - Read a byte back from the device
0398  * @data:   device state.
0399  */
0400 static u8 sht15_read_byte(struct sht15_data *data)
0401 {
0402     int i;
0403     u8 byte = 0;
0404 
0405     for (i = 0; i < 8; ++i) {
0406         byte <<= 1;
0407         gpiod_set_value(data->sck, 1);
0408         ndelay(SHT15_TSCKH);
0409         byte |= !!gpiod_get_value(data->data);
0410         gpiod_set_value(data->sck, 0);
0411         ndelay(SHT15_TSCKL);
0412     }
0413     return byte;
0414 }
0415 
0416 /**
0417  * sht15_send_status() - write the status register byte
0418  * @data:   sht15 specific data.
0419  * @status: the byte to set the status register with.
0420  *
0421  * As described in figure 14 and table 5 of the datasheet.
0422  */
0423 static int sht15_send_status(struct sht15_data *data, u8 status)
0424 {
0425     int err;
0426 
0427     err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
0428     if (err)
0429         return err;
0430     err = gpiod_direction_output(data->data, 1);
0431     if (err)
0432         return err;
0433     ndelay(SHT15_TSU);
0434     sht15_send_byte(data, status);
0435     err = sht15_wait_for_response(data);
0436     if (err)
0437         return err;
0438 
0439     data->val_status = status;
0440     return 0;
0441 }
0442 
0443 /**
0444  * sht15_update_status() - get updated status register from device if too old
0445  * @data:   device instance specific data.
0446  *
0447  * As described in figure 15 and table 5 of the datasheet.
0448  */
0449 static int sht15_update_status(struct sht15_data *data)
0450 {
0451     int ret = 0;
0452     u8 status;
0453     u8 previous_config;
0454     u8 dev_checksum = 0;
0455     u8 checksum_vals[2];
0456     int timeout = HZ;
0457 
0458     mutex_lock(&data->read_lock);
0459     if (time_after(jiffies, data->last_status + timeout)
0460             || !data->status_valid) {
0461         ret = sht15_send_cmd(data, SHT15_READ_STATUS);
0462         if (ret)
0463             goto unlock;
0464         status = sht15_read_byte(data);
0465 
0466         if (data->checksumming) {
0467             sht15_ack(data);
0468             dev_checksum = bitrev8(sht15_read_byte(data));
0469             checksum_vals[0] = SHT15_READ_STATUS;
0470             checksum_vals[1] = status;
0471             data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
0472                     == dev_checksum);
0473         }
0474 
0475         ret = sht15_end_transmission(data);
0476         if (ret)
0477             goto unlock;
0478 
0479         /*
0480          * Perform checksum validation on the received data.
0481          * Specification mentions that in case a checksum verification
0482          * fails, a soft reset command must be sent to the device.
0483          */
0484         if (data->checksumming && !data->checksum_ok) {
0485             previous_config = data->val_status & 0x07;
0486             ret = sht15_soft_reset(data);
0487             if (ret)
0488                 goto unlock;
0489             if (previous_config) {
0490                 ret = sht15_send_status(data, previous_config);
0491                 if (ret) {
0492                     dev_err(data->dev,
0493                         "CRC validation failed, unable "
0494                         "to restore device settings\n");
0495                     goto unlock;
0496                 }
0497             }
0498             ret = -EAGAIN;
0499             goto unlock;
0500         }
0501 
0502         data->val_status = status;
0503         data->status_valid = true;
0504         data->last_status = jiffies;
0505     }
0506 
0507 unlock:
0508     mutex_unlock(&data->read_lock);
0509     return ret;
0510 }
0511 
0512 /**
0513  * sht15_measurement() - get a new value from device
0514  * @data:       device instance specific data
0515  * @command:        command sent to request value
0516  * @timeout_msecs:  timeout after which comms are assumed
0517  *          to have failed are reset.
0518  */
0519 static int sht15_measurement(struct sht15_data *data,
0520                  int command,
0521                  int timeout_msecs)
0522 {
0523     int ret;
0524     u8 previous_config;
0525 
0526     ret = sht15_send_cmd(data, command);
0527     if (ret)
0528         return ret;
0529 
0530     ret = gpiod_direction_input(data->data);
0531     if (ret)
0532         return ret;
0533     atomic_set(&data->interrupt_handled, 0);
0534 
0535     enable_irq(gpiod_to_irq(data->data));
0536     if (gpiod_get_value(data->data) == 0) {
0537         disable_irq_nosync(gpiod_to_irq(data->data));
0538         /* Only relevant if the interrupt hasn't occurred. */
0539         if (!atomic_read(&data->interrupt_handled))
0540             schedule_work(&data->read_work);
0541     }
0542     ret = wait_event_timeout(data->wait_queue,
0543                  (data->state == SHT15_READING_NOTHING),
0544                  msecs_to_jiffies(timeout_msecs));
0545     if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
0546         data->state = SHT15_READING_NOTHING;
0547         return -EIO;
0548     } else if (ret == 0) { /* timeout occurred */
0549         disable_irq_nosync(gpiod_to_irq(data->data));
0550         ret = sht15_connection_reset(data);
0551         if (ret)
0552             return ret;
0553         return -ETIME;
0554     }
0555 
0556     /*
0557      *  Perform checksum validation on the received data.
0558      *  Specification mentions that in case a checksum verification fails,
0559      *  a soft reset command must be sent to the device.
0560      */
0561     if (data->checksumming && !data->checksum_ok) {
0562         previous_config = data->val_status & 0x07;
0563         ret = sht15_soft_reset(data);
0564         if (ret)
0565             return ret;
0566         if (previous_config) {
0567             ret = sht15_send_status(data, previous_config);
0568             if (ret) {
0569                 dev_err(data->dev,
0570                     "CRC validation failed, unable "
0571                     "to restore device settings\n");
0572                 return ret;
0573             }
0574         }
0575         return -EAGAIN;
0576     }
0577 
0578     return 0;
0579 }
0580 
0581 /**
0582  * sht15_update_measurements() - get updated measures from device if too old
0583  * @data:   device state
0584  */
0585 static int sht15_update_measurements(struct sht15_data *data)
0586 {
0587     int ret = 0;
0588     int timeout = HZ;
0589 
0590     mutex_lock(&data->read_lock);
0591     if (time_after(jiffies, data->last_measurement + timeout)
0592         || !data->measurements_valid) {
0593         data->state = SHT15_READING_HUMID;
0594         ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
0595         if (ret)
0596             goto unlock;
0597         data->state = SHT15_READING_TEMP;
0598         ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
0599         if (ret)
0600             goto unlock;
0601         data->measurements_valid = true;
0602         data->last_measurement = jiffies;
0603     }
0604 
0605 unlock:
0606     mutex_unlock(&data->read_lock);
0607     return ret;
0608 }
0609 
0610 /**
0611  * sht15_calc_temp() - convert the raw reading to a temperature
0612  * @data:   device state
0613  *
0614  * As per section 4.3 of the data sheet.
0615  */
0616 static inline int sht15_calc_temp(struct sht15_data *data)
0617 {
0618     int d1 = temppoints[0].d1;
0619     int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
0620     int i;
0621 
0622     for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
0623         /* Find pointer to interpolate */
0624         if (data->supply_uv > temppoints[i - 1].vdd) {
0625             d1 = (data->supply_uv - temppoints[i - 1].vdd)
0626                 * (temppoints[i].d1 - temppoints[i - 1].d1)
0627                 / (temppoints[i].vdd - temppoints[i - 1].vdd)
0628                 + temppoints[i - 1].d1;
0629             break;
0630         }
0631 
0632     return data->val_temp * d2 + d1;
0633 }
0634 
0635 /**
0636  * sht15_calc_humid() - using last temperature convert raw to humid
0637  * @data:   device state
0638  *
0639  * This is the temperature compensated version as per section 4.2 of
0640  * the data sheet.
0641  *
0642  * The sensor is assumed to be V3, which is compatible with V4.
0643  * Humidity conversion coefficients are shown in table 7 of the datasheet.
0644  */
0645 static inline int sht15_calc_humid(struct sht15_data *data)
0646 {
0647     int rh_linear; /* milli percent */
0648     int temp = sht15_calc_temp(data);
0649     int c2, c3;
0650     int t2;
0651     const int c1 = -4;
0652 
0653     if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
0654         c2 = 648000; /* x 10 ^ -6 */
0655         c3 = -7200;  /* x 10 ^ -7 */
0656         t2 = 1280;
0657     } else {
0658         c2 = 40500;  /* x 10 ^ -6 */
0659         c3 = -28;    /* x 10 ^ -7 */
0660         t2 = 80;
0661     }
0662 
0663     rh_linear = c1 * 1000
0664         + c2 * data->val_humid / 1000
0665         + (data->val_humid * data->val_humid * c3) / 10000;
0666     return (temp - 25000) * (10000 + t2 * data->val_humid)
0667         / 1000000 + rh_linear;
0668 }
0669 
0670 /**
0671  * sht15_show_status() - show status information in sysfs
0672  * @dev:    device.
0673  * @attr:   device attribute.
0674  * @buf:    sysfs buffer where information is written to.
0675  *
0676  * Will be called on read access to temp1_fault, humidity1_fault
0677  * and heater_enable sysfs attributes.
0678  * Returns number of bytes written into buffer, negative errno on error.
0679  */
0680 static ssize_t sht15_status_show(struct device *dev,
0681                  struct device_attribute *attr, char *buf)
0682 {
0683     int ret;
0684     struct sht15_data *data = dev_get_drvdata(dev);
0685     u8 bit = to_sensor_dev_attr(attr)->index;
0686 
0687     ret = sht15_update_status(data);
0688 
0689     return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
0690 }
0691 
0692 /**
0693  * sht15_store_heater() - change heater state via sysfs
0694  * @dev:    device.
0695  * @attr:   device attribute.
0696  * @buf:    sysfs buffer to read the new heater state from.
0697  * @count:  length of the data.
0698  *
0699  * Will be called on write access to heater_enable sysfs attribute.
0700  * Returns number of bytes actually decoded, negative errno on error.
0701  */
0702 static ssize_t sht15_status_store(struct device *dev,
0703                   struct device_attribute *attr,
0704                   const char *buf, size_t count)
0705 {
0706     int ret;
0707     struct sht15_data *data = dev_get_drvdata(dev);
0708     long value;
0709     u8 status;
0710 
0711     if (kstrtol(buf, 10, &value))
0712         return -EINVAL;
0713 
0714     mutex_lock(&data->read_lock);
0715     status = data->val_status & 0x07;
0716     if (!!value)
0717         status |= SHT15_STATUS_HEATER;
0718     else
0719         status &= ~SHT15_STATUS_HEATER;
0720 
0721     ret = sht15_send_status(data, status);
0722     mutex_unlock(&data->read_lock);
0723 
0724     return ret ? ret : count;
0725 }
0726 
0727 /**
0728  * sht15_show_temp() - show temperature measurement value in sysfs
0729  * @dev:    device.
0730  * @attr:   device attribute.
0731  * @buf:    sysfs buffer where measurement values are written to.
0732  *
0733  * Will be called on read access to temp1_input sysfs attribute.
0734  * Returns number of bytes written into buffer, negative errno on error.
0735  */
0736 static ssize_t sht15_temp_show(struct device *dev,
0737                    struct device_attribute *attr, char *buf)
0738 {
0739     int ret;
0740     struct sht15_data *data = dev_get_drvdata(dev);
0741 
0742     /* Technically no need to read humidity as well */
0743     ret = sht15_update_measurements(data);
0744 
0745     return ret ? ret : sprintf(buf, "%d\n",
0746                    sht15_calc_temp(data));
0747 }
0748 
0749 /**
0750  * sht15_show_humidity() - show humidity measurement value in sysfs
0751  * @dev:    device.
0752  * @attr:   device attribute.
0753  * @buf:    sysfs buffer where measurement values are written to.
0754  *
0755  * Will be called on read access to humidity1_input sysfs attribute.
0756  * Returns number of bytes written into buffer, negative errno on error.
0757  */
0758 static ssize_t sht15_humidity_show(struct device *dev,
0759                    struct device_attribute *attr, char *buf)
0760 {
0761     int ret;
0762     struct sht15_data *data = dev_get_drvdata(dev);
0763 
0764     ret = sht15_update_measurements(data);
0765 
0766     return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
0767 }
0768 
0769 static ssize_t name_show(struct device *dev,
0770              struct device_attribute *attr,
0771              char *buf)
0772 {
0773     struct platform_device *pdev = to_platform_device(dev);
0774     return sprintf(buf, "%s\n", pdev->name);
0775 }
0776 
0777 static SENSOR_DEVICE_ATTR_RO(temp1_input, sht15_temp, 0);
0778 static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht15_humidity, 0);
0779 static SENSOR_DEVICE_ATTR_RO(temp1_fault, sht15_status,
0780                  SHT15_STATUS_LOW_BATTERY);
0781 static SENSOR_DEVICE_ATTR_RO(humidity1_fault, sht15_status,
0782                  SHT15_STATUS_LOW_BATTERY);
0783 static SENSOR_DEVICE_ATTR_RW(heater_enable, sht15_status, SHT15_STATUS_HEATER);
0784 static DEVICE_ATTR_RO(name);
0785 static struct attribute *sht15_attrs[] = {
0786     &sensor_dev_attr_temp1_input.dev_attr.attr,
0787     &sensor_dev_attr_humidity1_input.dev_attr.attr,
0788     &sensor_dev_attr_temp1_fault.dev_attr.attr,
0789     &sensor_dev_attr_humidity1_fault.dev_attr.attr,
0790     &sensor_dev_attr_heater_enable.dev_attr.attr,
0791     &dev_attr_name.attr,
0792     NULL,
0793 };
0794 
0795 static const struct attribute_group sht15_attr_group = {
0796     .attrs = sht15_attrs,
0797 };
0798 
0799 static irqreturn_t sht15_interrupt_fired(int irq, void *d)
0800 {
0801     struct sht15_data *data = d;
0802 
0803     /* First disable the interrupt */
0804     disable_irq_nosync(irq);
0805     atomic_inc(&data->interrupt_handled);
0806     /* Then schedule a reading work struct */
0807     if (data->state != SHT15_READING_NOTHING)
0808         schedule_work(&data->read_work);
0809     return IRQ_HANDLED;
0810 }
0811 
0812 static void sht15_bh_read_data(struct work_struct *work_s)
0813 {
0814     uint16_t val = 0;
0815     u8 dev_checksum = 0;
0816     u8 checksum_vals[3];
0817     struct sht15_data *data
0818         = container_of(work_s, struct sht15_data,
0819                    read_work);
0820 
0821     /* Firstly, verify the line is low */
0822     if (gpiod_get_value(data->data)) {
0823         /*
0824          * If not, then start the interrupt again - care here as could
0825          * have gone low in meantime so verify it hasn't!
0826          */
0827         atomic_set(&data->interrupt_handled, 0);
0828         enable_irq(gpiod_to_irq(data->data));
0829         /* If still not occurred or another handler was scheduled */
0830         if (gpiod_get_value(data->data)
0831             || atomic_read(&data->interrupt_handled))
0832             return;
0833     }
0834 
0835     /* Read the data back from the device */
0836     val = sht15_read_byte(data);
0837     val <<= 8;
0838     if (sht15_ack(data))
0839         goto wakeup;
0840     val |= sht15_read_byte(data);
0841 
0842     if (data->checksumming) {
0843         /*
0844          * Ask the device for a checksum and read it back.
0845          * Note: the device sends the checksum byte reversed.
0846          */
0847         if (sht15_ack(data))
0848             goto wakeup;
0849         dev_checksum = bitrev8(sht15_read_byte(data));
0850         checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
0851             SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
0852         checksum_vals[1] = (u8) (val >> 8);
0853         checksum_vals[2] = (u8) val;
0854         data->checksum_ok
0855             = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
0856     }
0857 
0858     /* Tell the device we are done */
0859     if (sht15_end_transmission(data))
0860         goto wakeup;
0861 
0862     switch (data->state) {
0863     case SHT15_READING_TEMP:
0864         data->val_temp = val;
0865         break;
0866     case SHT15_READING_HUMID:
0867         data->val_humid = val;
0868         break;
0869     default:
0870         break;
0871     }
0872 
0873     data->state = SHT15_READING_NOTHING;
0874 wakeup:
0875     wake_up(&data->wait_queue);
0876 }
0877 
0878 static void sht15_update_voltage(struct work_struct *work_s)
0879 {
0880     struct sht15_data *data
0881         = container_of(work_s, struct sht15_data,
0882                    update_supply_work);
0883     data->supply_uv = regulator_get_voltage(data->reg);
0884 }
0885 
0886 /**
0887  * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
0888  * @nb:     associated notification structure
0889  * @event:  voltage regulator state change event code
0890  * @ignored:    function parameter - ignored here
0891  *
0892  * Note that as the notification code holds the regulator lock, we have
0893  * to schedule an update of the supply voltage rather than getting it directly.
0894  */
0895 static int sht15_invalidate_voltage(struct notifier_block *nb,
0896                     unsigned long event,
0897                     void *ignored)
0898 {
0899     struct sht15_data *data = container_of(nb, struct sht15_data, nb);
0900 
0901     if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
0902         data->supply_uv_valid = false;
0903     schedule_work(&data->update_supply_work);
0904 
0905     return NOTIFY_OK;
0906 }
0907 
0908 #ifdef CONFIG_OF
0909 static const struct of_device_id sht15_dt_match[] = {
0910     { .compatible = "sensirion,sht15" },
0911     { },
0912 };
0913 MODULE_DEVICE_TABLE(of, sht15_dt_match);
0914 #endif
0915 
0916 static int sht15_probe(struct platform_device *pdev)
0917 {
0918     int ret;
0919     struct sht15_data *data;
0920 
0921     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0922     if (!data)
0923         return -ENOMEM;
0924 
0925     INIT_WORK(&data->read_work, sht15_bh_read_data);
0926     INIT_WORK(&data->update_supply_work, sht15_update_voltage);
0927     platform_set_drvdata(pdev, data);
0928     mutex_init(&data->read_lock);
0929     data->dev = &pdev->dev;
0930     init_waitqueue_head(&data->wait_queue);
0931 
0932     /*
0933      * If a regulator is available,
0934      * query what the supply voltage actually is!
0935      */
0936     data->reg = devm_regulator_get_optional(data->dev, "vcc");
0937     if (!IS_ERR(data->reg)) {
0938         int voltage;
0939 
0940         voltage = regulator_get_voltage(data->reg);
0941         if (voltage)
0942             data->supply_uv = voltage;
0943 
0944         ret = regulator_enable(data->reg);
0945         if (ret != 0) {
0946             dev_err(&pdev->dev,
0947                 "failed to enable regulator: %d\n", ret);
0948             return ret;
0949         }
0950 
0951         /*
0952          * Setup a notifier block to update this if another device
0953          * causes the voltage to change
0954          */
0955         data->nb.notifier_call = &sht15_invalidate_voltage;
0956         ret = regulator_register_notifier(data->reg, &data->nb);
0957         if (ret) {
0958             dev_err(&pdev->dev,
0959                 "regulator notifier request failed\n");
0960             regulator_disable(data->reg);
0961             return ret;
0962         }
0963     }
0964 
0965     /* Try requesting the GPIOs */
0966     data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW);
0967     if (IS_ERR(data->sck)) {
0968         ret = PTR_ERR(data->sck);
0969         dev_err(&pdev->dev, "clock line GPIO request failed\n");
0970         goto err_release_reg;
0971     }
0972     data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
0973     if (IS_ERR(data->data)) {
0974         ret = PTR_ERR(data->data);
0975         dev_err(&pdev->dev, "data line GPIO request failed\n");
0976         goto err_release_reg;
0977     }
0978 
0979     ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data),
0980                    sht15_interrupt_fired,
0981                    IRQF_TRIGGER_FALLING,
0982                    "sht15 data",
0983                    data);
0984     if (ret) {
0985         dev_err(&pdev->dev, "failed to get irq for data line\n");
0986         goto err_release_reg;
0987     }
0988     disable_irq_nosync(gpiod_to_irq(data->data));
0989     ret = sht15_connection_reset(data);
0990     if (ret)
0991         goto err_release_reg;
0992     ret = sht15_soft_reset(data);
0993     if (ret)
0994         goto err_release_reg;
0995 
0996     ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
0997     if (ret) {
0998         dev_err(&pdev->dev, "sysfs create failed\n");
0999         goto err_release_reg;
1000     }
1001 
1002     data->hwmon_dev = hwmon_device_register(data->dev);
1003     if (IS_ERR(data->hwmon_dev)) {
1004         ret = PTR_ERR(data->hwmon_dev);
1005         goto err_release_sysfs_group;
1006     }
1007 
1008     return 0;
1009 
1010 err_release_sysfs_group:
1011     sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1012 err_release_reg:
1013     if (!IS_ERR(data->reg)) {
1014         regulator_unregister_notifier(data->reg, &data->nb);
1015         regulator_disable(data->reg);
1016     }
1017     return ret;
1018 }
1019 
1020 static int sht15_remove(struct platform_device *pdev)
1021 {
1022     struct sht15_data *data = platform_get_drvdata(pdev);
1023     int ret;
1024 
1025     hwmon_device_unregister(data->hwmon_dev);
1026     sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1027 
1028     ret = sht15_soft_reset(data);
1029     if (ret)
1030         dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
1031 
1032     if (!IS_ERR(data->reg)) {
1033         regulator_unregister_notifier(data->reg, &data->nb);
1034         regulator_disable(data->reg);
1035     }
1036 
1037     return 0;
1038 }
1039 
1040 static const struct platform_device_id sht15_device_ids[] = {
1041     { "sht10", sht10 },
1042     { "sht11", sht11 },
1043     { "sht15", sht15 },
1044     { "sht71", sht71 },
1045     { "sht75", sht75 },
1046     { }
1047 };
1048 MODULE_DEVICE_TABLE(platform, sht15_device_ids);
1049 
1050 static struct platform_driver sht15_driver = {
1051     .driver = {
1052         .name = "sht15",
1053         .of_match_table = of_match_ptr(sht15_dt_match),
1054     },
1055     .probe = sht15_probe,
1056     .remove = sht15_remove,
1057     .id_table = sht15_device_ids,
1058 };
1059 module_platform_driver(sht15_driver);
1060 
1061 MODULE_LICENSE("GPL");
1062 MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");