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