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