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