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