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