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