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