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