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