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