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