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