aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVivien Didelot <vivien.didelot@savoirfairelinux.com>2011-04-12 15:34:36 -0400
committerGuenter Roeck <guenter.roeck@ericsson.com>2011-05-19 11:19:33 -0400
commit99a0378de9f887fd4d501f1baa50aaf16d01a8e8 (patch)
tree1c13bd8f5c89eccfd94e2c8e61323dd70f0a5191
parent83bffbce745795572d20f0109395cb61690c6dcf (diff)
hwmon: (sht15) general code clean-up
* Add a documentation file for the device. * Respect a bit more the kernel-doc syntax. * Rename some variables for clarity. * Use bool type for flags. * Use an enum for states (actions being done). Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Acked-by: Jonathan Cameron <jic23@cam.ac.uk> Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com>
-rw-r--r--Documentation/hwmon/sht1542
-rw-r--r--drivers/hwmon/sht15.c268
-rw-r--r--include/linux/sht15.h12
3 files changed, 203 insertions, 119 deletions
diff --git a/Documentation/hwmon/sht15 b/Documentation/hwmon/sht15
new file mode 100644
index 000000000000..2919c516fdbc
--- /dev/null
+++ b/Documentation/hwmon/sht15
@@ -0,0 +1,42 @@
1Kernel driver sht15
2===================
3
4Authors:
5 * Wouter Horre
6 * Jonathan Cameron
7
8Supported chips:
9 * Sensirion SHT10
10 Prefix: 'sht10'
11
12 * Sensirion SHT11
13 Prefix: 'sht11'
14
15 * Sensirion SHT15
16 Prefix: 'sht15'
17
18 * Sensirion SHT71
19 Prefix: 'sht71'
20
21 * Sensirion SHT75
22 Prefix: 'sht75'
23
24Datasheet: Publicly available at the Sensirion website
25http://www.sensirion.ch/en/pdf/product_information/Datasheet-humidity-sensor-SHT1x.pdf
26
27Description
28-----------
29
30The SHT10, SHT11, SHT15, SHT71, and SHT75 are humidity and temperature
31sensors.
32
33The devices communicate using two GPIO lines and use the default
34resolution settings of 14 bits for temperature and 12 bits for humidity.
35
36Note: The regulator supply name is set to "vcc".
37
38Sysfs interface
39---------------
40
41* temp1_input: temperature input
42* humidity1_input: humidity input
diff --git a/drivers/hwmon/sht15.c b/drivers/hwmon/sht15.c
index f4e617adb220..080af75c517b 100644
--- a/drivers/hwmon/sht15.c
+++ b/drivers/hwmon/sht15.c
@@ -9,16 +9,7 @@
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 * 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 */ 13 */
23 14
24#include <linux/interrupt.h> 15#include <linux/interrupt.h>
@@ -39,17 +30,21 @@
39#include <linux/slab.h> 30#include <linux/slab.h>
40#include <asm/atomic.h> 31#include <asm/atomic.h>
41 32
42#define SHT15_MEASURE_TEMP 3 33/* Commands */
43#define SHT15_MEASURE_RH 5 34#define SHT15_MEASURE_TEMP 0x03
35#define SHT15_MEASURE_RH 0x05
44 36
45#define SHT15_READING_NOTHING 0 37/* Min timings */
46#define SHT15_READING_TEMP 1 38#define SHT15_TSCKL 100 /* (nsecs) clock low */
47#define SHT15_READING_HUMID 2 39#define SHT15_TSCKH 100 /* (nsecs) clock high */
40#define SHT15_TSU 150 /* (nsecs) data setup time */
48 41
49/* Min timings in nsecs */ 42/* Actions the driver may be doing */
50#define SHT15_TSCKL 100 /* clock low */ 43enum sht15_state {
51#define SHT15_TSCKH 100 /* clock high */ 44 SHT15_READING_NOTHING,
52#define SHT15_TSU 150 /* data setup time */ 45 SHT15_READING_TEMP,
46 SHT15_READING_HUMID
47};
53 48
54/** 49/**
55 * struct sht15_temppair - elements of voltage dependent temp calc 50 * struct sht15_temppair - elements of voltage dependent temp calc
@@ -61,9 +56,7 @@ struct sht15_temppair {
61 int d1; 56 int d1;
62}; 57};
63 58
64/* Table 9 from data sheet - relates temperature calculation 59/* Table 9 from datasheet - relates temperature calculation to supply voltage */
65 * to supply voltage.
66 */
67static const struct sht15_temppair temppoints[] = { 60static const struct sht15_temppair temppoints[] = {
68 { 2500000, -39400 }, 61 { 2500000, -39400 },
69 { 3000000, -39600 }, 62 { 3000000, -39600 },
@@ -74,27 +67,27 @@ static const struct sht15_temppair temppoints[] = {
74 67
75/** 68/**
76 * struct sht15_data - device instance specific data 69 * struct sht15_data - device instance specific data
77 * @pdata: platform data (gpio's etc) 70 * @pdata: platform data (gpio's etc).
78 * @read_work: bh of interrupt handler 71 * @read_work: bh of interrupt handler.
79 * @wait_queue: wait queue for getting values from device 72 * @wait_queue: wait queue for getting values from device.
80 * @val_temp: last temperature value read from device 73 * @val_temp: last temperature value read from device.
81 * @val_humid: last humidity value read from device 74 * @val_humid: last humidity value read from device.
82 * @flag: status flag used to identify what the last request was 75 * @state: state identifying the action the driver is doing.
83 * @valid: are the current stored values valid (start condition) 76 * @measurements_valid: are the current stored measures valid (start condition).
84 * @last_updat: time of last update 77 * @last_measurement: time of last measure.
85 * @read_lock: mutex to ensure only one read in progress 78 * @read_lock: mutex to ensure only one read in progress at a time.
86 * at a time. 79 * @dev: associate device structure.
87 * @dev: associate device structure 80 * @hwmon_dev: device associated with hwmon subsystem.
88 * @hwmon_dev: device associated with hwmon subsystem 81 * @reg: associated regulator (if specified).
89 * @reg: associated regulator (if specified) 82 * @nb: notifier block to handle notifications of voltage
90 * @nb: notifier block to handle notifications of voltage changes 83 * changes.
91 * @supply_uV: local copy of supply voltage used to allow 84 * @supply_uV: local copy of supply voltage used to allow use of
92 * use of regulator consumer if available 85 * regulator consumer if available.
93 * @supply_uV_valid: indicates that an updated value has not yet 86 * @supply_uV_valid: indicates that an updated value has not yet been
94 * been obtained from the regulator and so any calculations 87 * obtained from the regulator and so any calculations
95 * based upon it will be invalid. 88 * based upon it will be invalid.
96 * @update_supply_work: work struct that is used to update the supply_uV 89 * @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 90 * @interrupt_handled: flag used to indicate a handler has been scheduled.
98 */ 91 */
99struct sht15_data { 92struct sht15_data {
100 struct sht15_platform_data *pdata; 93 struct sht15_platform_data *pdata;
@@ -102,16 +95,16 @@ struct sht15_data {
102 wait_queue_head_t wait_queue; 95 wait_queue_head_t wait_queue;
103 uint16_t val_temp; 96 uint16_t val_temp;
104 uint16_t val_humid; 97 uint16_t val_humid;
105 u8 flag; 98 enum sht15_state state;
106 u8 valid; 99 bool measurements_valid;
107 unsigned long last_updat; 100 unsigned long last_measurement;
108 struct mutex read_lock; 101 struct mutex read_lock;
109 struct device *dev; 102 struct device *dev;
110 struct device *hwmon_dev; 103 struct device *hwmon_dev;
111 struct regulator *reg; 104 struct regulator *reg;
112 struct notifier_block nb; 105 struct notifier_block nb;
113 int supply_uV; 106 int supply_uV;
114 int supply_uV_valid; 107 bool supply_uV_valid;
115 struct work_struct update_supply_work; 108 struct work_struct update_supply_work;
116 atomic_t interrupt_handled; 109 atomic_t interrupt_handled;
117}; 110};
@@ -125,6 +118,7 @@ struct sht15_data {
125static void sht15_connection_reset(struct sht15_data *data) 118static void sht15_connection_reset(struct sht15_data *data)
126{ 119{
127 int i; 120 int i;
121
128 gpio_direction_output(data->pdata->gpio_data, 1); 122 gpio_direction_output(data->pdata->gpio_data, 1);
129 ndelay(SHT15_TSCKL); 123 ndelay(SHT15_TSCKL);
130 gpio_set_value(data->pdata->gpio_sck, 0); 124 gpio_set_value(data->pdata->gpio_sck, 0);
@@ -136,14 +130,14 @@ static void sht15_connection_reset(struct sht15_data *data)
136 ndelay(SHT15_TSCKL); 130 ndelay(SHT15_TSCKL);
137 } 131 }
138} 132}
133
139/** 134/**
140 * sht15_send_bit() - send an individual bit to the device 135 * sht15_send_bit() - send an individual bit to the device
141 * @data: device state data 136 * @data: device state data
142 * @val: value of bit to be sent 137 * @val: value of bit to be sent
143 **/ 138 */
144static inline void sht15_send_bit(struct sht15_data *data, int val) 139static inline void sht15_send_bit(struct sht15_data *data, int val)
145{ 140{
146
147 gpio_set_value(data->pdata->gpio_data, val); 141 gpio_set_value(data->pdata->gpio_data, val);
148 ndelay(SHT15_TSU); 142 ndelay(SHT15_TSU);
149 gpio_set_value(data->pdata->gpio_sck, 1); 143 gpio_set_value(data->pdata->gpio_sck, 1);
@@ -154,12 +148,12 @@ static inline void sht15_send_bit(struct sht15_data *data, int val)
154 148
155/** 149/**
156 * sht15_transmission_start() - specific sequence for new transmission 150 * sht15_transmission_start() - specific sequence for new transmission
157 *
158 * @data: device state data 151 * @data: device state data
152 *
159 * Timings for this are not documented on the data sheet, so very 153 * Timings for this are not documented on the data sheet, so very
160 * conservative ones used in implementation. This implements 154 * conservative ones used in implementation. This implements
161 * figure 12 on the data sheet. 155 * figure 12 on the data sheet.
162 **/ 156 */
163static void sht15_transmission_start(struct sht15_data *data) 157static void sht15_transmission_start(struct sht15_data *data)
164{ 158{
165 /* ensure data is high and output */ 159 /* ensure data is high and output */
@@ -180,23 +174,26 @@ static void sht15_transmission_start(struct sht15_data *data)
180 gpio_set_value(data->pdata->gpio_sck, 0); 174 gpio_set_value(data->pdata->gpio_sck, 0);
181 ndelay(SHT15_TSCKL); 175 ndelay(SHT15_TSCKL);
182} 176}
177
183/** 178/**
184 * sht15_send_byte() - send a single byte to the device 179 * sht15_send_byte() - send a single byte to the device
185 * @data: device state 180 * @data: device state
186 * @byte: value to be sent 181 * @byte: value to be sent
187 **/ 182 */
188static void sht15_send_byte(struct sht15_data *data, u8 byte) 183static void sht15_send_byte(struct sht15_data *data, u8 byte)
189{ 184{
190 int i; 185 int i;
186
191 for (i = 0; i < 8; i++) { 187 for (i = 0; i < 8; i++) {
192 sht15_send_bit(data, !!(byte & 0x80)); 188 sht15_send_bit(data, !!(byte & 0x80));
193 byte <<= 1; 189 byte <<= 1;
194 } 190 }
195} 191}
192
196/** 193/**
197 * sht15_wait_for_response() - checks for ack from device 194 * sht15_wait_for_response() - checks for ack from device
198 * @data: device state 195 * @data: device state
199 **/ 196 */
200static int sht15_wait_for_response(struct sht15_data *data) 197static int sht15_wait_for_response(struct sht15_data *data)
201{ 198{
202 gpio_direction_input(data->pdata->gpio_data); 199 gpio_direction_input(data->pdata->gpio_data);
@@ -220,27 +217,30 @@ static int sht15_wait_for_response(struct sht15_data *data)
220 * 217 *
221 * On entry, sck is output low, data is output pull high 218 * On entry, sck is output low, data is output pull high
222 * and the interrupt disabled. 219 * and the interrupt disabled.
223 **/ 220 */
224static int sht15_send_cmd(struct sht15_data *data, u8 cmd) 221static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
225{ 222{
226 int ret = 0; 223 int ret = 0;
224
227 sht15_transmission_start(data); 225 sht15_transmission_start(data);
228 sht15_send_byte(data, cmd); 226 sht15_send_byte(data, cmd);
229 ret = sht15_wait_for_response(data); 227 ret = sht15_wait_for_response(data);
230 return ret; 228 return ret;
231} 229}
230
232/** 231/**
233 * sht15_update_single_val() - get a new value from device 232 * sht15_measurement() - get a new value from device
234 * @data: device instance specific data 233 * @data: device instance specific data
235 * @command: command sent to request value 234 * @command: command sent to request value
236 * @timeout_msecs: timeout after which comms are assumed 235 * @timeout_msecs: timeout after which comms are assumed
237 * to have failed are reset. 236 * to have failed are reset.
238 **/ 237 */
239static inline int sht15_update_single_val(struct sht15_data *data, 238static int sht15_measurement(struct sht15_data *data,
240 int command, 239 int command,
241 int timeout_msecs) 240 int timeout_msecs)
242{ 241{
243 int ret; 242 int ret;
243
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;
@@ -256,7 +256,7 @@ static inline int sht15_update_single_val(struct sht15_data *data,
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->state == 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));
@@ -267,27 +267,27 @@ static inline int sht15_update_single_val(struct sht15_data *data,
267} 267}
268 268
269/** 269/**
270 * sht15_update_vals() - get updated readings from device if too old 270 * sht15_update_measurements() - get updated measures from device if too old
271 * @data: device state 271 * @data: device state
272 **/ 272 */
273static int sht15_update_vals(struct sht15_data *data) 273static int sht15_update_measurements(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_measurement + timeout)
280 || !data->valid) { 280 || !data->measurements_valid) {
281 data->flag = SHT15_READING_HUMID; 281 data->state = SHT15_READING_HUMID;
282 ret = sht15_update_single_val(data, SHT15_MEASURE_RH, 160); 282 ret = sht15_measurement(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->state = SHT15_READING_TEMP;
286 ret = sht15_update_single_val(data, SHT15_MEASURE_TEMP, 400); 286 ret = sht15_measurement(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->measurements_valid = true;
290 data->last_updat = jiffies; 290 data->last_measurement = jiffies;
291 } 291 }
292error_ret: 292error_ret:
293 mutex_unlock(&data->read_lock); 293 mutex_unlock(&data->read_lock);
@@ -300,7 +300,7 @@ error_ret:
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 */
304static inline int sht15_calc_temp(struct sht15_data *data) 304static inline int sht15_calc_temp(struct sht15_data *data)
305{ 305{
306 int d1 = temppoints[0].d1; 306 int d1 = temppoints[0].d1;
@@ -316,7 +316,7 @@ static inline int sht15_calc_temp(struct sht15_data *data)
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/**
@@ -325,23 +325,35 @@ static inline int sht15_calc_temp(struct sht15_data *data)
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 * The sensor is assumed to be V3, which is compatible with V4.
330 * Humidity conversion coefficients are shown in table 7 of the datasheet.
331 */
329static inline int sht15_calc_humid(struct sht15_data *data) 332static inline int sht15_calc_humid(struct sht15_data *data)
330{ 333{
331 int RHlinear; /* milli percent */ 334 int rh_linear; /* milli percent */
332 int temp = sht15_calc_temp(data); 335 int temp = sht15_calc_temp(data);
333 336
334 const int c1 = -4; 337 const int c1 = -4;
335 const int c2 = 40500; /* x 10 ^ -6 */ 338 const int c2 = 40500; /* x 10 ^ -6 */
336 const int c3 = -28; /* x 10 ^ -7 */ 339 const int c3 = -28; /* x 10 ^ -7 */
337 340
338 RHlinear = c1*1000 341 rh_linear = c1 * 1000
339 + c2 * data->val_humid/1000 342 + c2 * data->val_humid / 1000
340 + (data->val_humid * data->val_humid * c3) / 10000; 343 + (data->val_humid * data->val_humid * c3) / 10000;
341 return (temp - 25000) * (10000 + 80 * data->val_humid) 344 return (temp - 25000) * (10000 + 80 * data->val_humid)
342 / 1000000 + RHlinear; 345 / 1000000 + rh_linear;
343} 346}
344 347
348/**
349 * sht15_show_temp() - show temperature measurement value in sysfs
350 * @dev: device.
351 * @attr: device attribute.
352 * @buf: sysfs buffer where measurement values are written to.
353 *
354 * Will be called on read access to temp1_input sysfs attribute.
355 * Returns number of bytes written into buffer, negative errno on error.
356 */
345static ssize_t sht15_show_temp(struct device *dev, 357static ssize_t sht15_show_temp(struct device *dev,
346 struct device_attribute *attr, 358 struct device_attribute *attr,
347 char *buf) 359 char *buf)
@@ -350,12 +362,21 @@ static ssize_t sht15_show_temp(struct device *dev,
350 struct sht15_data *data = dev_get_drvdata(dev); 362 struct sht15_data *data = dev_get_drvdata(dev);
351 363
352 /* Technically no need to read humidity as well */ 364 /* Technically no need to read humidity as well */
353 ret = sht15_update_vals(data); 365 ret = sht15_update_measurements(data);
354 366
355 return ret ? ret : sprintf(buf, "%d\n", 367 return ret ? ret : sprintf(buf, "%d\n",
356 sht15_calc_temp(data)); 368 sht15_calc_temp(data));
357} 369}
358 370
371/**
372 * sht15_show_humidity() - show humidity measurement value in sysfs
373 * @dev: device.
374 * @attr: device attribute.
375 * @buf: sysfs buffer where measurement values are written to.
376 *
377 * Will be called on read access to humidity1_input sysfs attribute.
378 * Returns number of bytes written into buffer, negative errno on error.
379 */
359static ssize_t sht15_show_humidity(struct device *dev, 380static ssize_t sht15_show_humidity(struct device *dev,
360 struct device_attribute *attr, 381 struct device_attribute *attr,
361 char *buf) 382 char *buf)
@@ -363,11 +384,12 @@ static ssize_t sht15_show_humidity(struct device *dev,
363 int ret; 384 int ret;
364 struct sht15_data *data = dev_get_drvdata(dev); 385 struct sht15_data *data = dev_get_drvdata(dev);
365 386
366 ret = sht15_update_vals(data); 387 ret = sht15_update_measurements(data);
367 388
368 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data)); 389 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
369 390
370}; 391}
392
371static ssize_t show_name(struct device *dev, 393static ssize_t show_name(struct device *dev,
372 struct device_attribute *attr, 394 struct device_attribute *attr,
373 char *buf) 395 char *buf)
@@ -376,12 +398,10 @@ static ssize_t show_name(struct device *dev,
376 return sprintf(buf, "%s\n", pdev->name); 398 return sprintf(buf, "%s\n", pdev->name);
377} 399}
378 400
379static SENSOR_DEVICE_ATTR(temp1_input, 401static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
380 S_IRUGO, sht15_show_temp, 402 sht15_show_temp, NULL, 0);
381 NULL, 0); 403static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
382static SENSOR_DEVICE_ATTR(humidity1_input, 404 sht15_show_humidity, NULL, 0);
383 S_IRUGO, sht15_show_humidity,
384 NULL, 0);
385static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 405static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
386static struct attribute *sht15_attrs[] = { 406static struct attribute *sht15_attrs[] = {
387 &sensor_dev_attr_temp1_input.dev_attr.attr, 407 &sensor_dev_attr_temp1_input.dev_attr.attr,
@@ -397,16 +417,20 @@ static const struct attribute_group sht15_attr_group = {
397static irqreturn_t sht15_interrupt_fired(int irq, void *d) 417static irqreturn_t sht15_interrupt_fired(int irq, void *d)
398{ 418{
399 struct sht15_data *data = d; 419 struct sht15_data *data = d;
420
400 /* First disable the interrupt */ 421 /* First disable the interrupt */
401 disable_irq_nosync(irq); 422 disable_irq_nosync(irq);
402 atomic_inc(&data->interrupt_handled); 423 atomic_inc(&data->interrupt_handled);
403 /* Then schedule a reading work struct */ 424 /* Then schedule a reading work struct */
404 if (data->flag != SHT15_READING_NOTHING) 425 if (data->state != SHT15_READING_NOTHING)
405 schedule_work(&data->read_work); 426 schedule_work(&data->read_work);
406 return IRQ_HANDLED; 427 return IRQ_HANDLED;
407} 428}
408 429
409/* Each byte of data is acknowledged by pulling the data line 430/**
431 * sht15_ack() - Send an ack to the device
432 *
433 * Each byte of data is acknowledged by pulling the data line
410 * low for one clock pulse. 434 * low for one clock pulse.
411 */ 435 */
412static void sht15_ack(struct sht15_data *data) 436static void sht15_ack(struct sht15_data *data)
@@ -421,12 +445,13 @@ static void sht15_ack(struct sht15_data *data)
421 445
422 gpio_direction_input(data->pdata->gpio_data); 446 gpio_direction_input(data->pdata->gpio_data);
423} 447}
448
424/** 449/**
425 * sht15_end_transmission() - notify device of end of transmission 450 * sht15_end_transmission() - notify device of end of transmission
426 * @data: device state 451 * @data: device state
427 * 452 *
428 * This is basically a NAK. (single clock pulse, data high) 453 * This is basically a NAK. (single clock pulse, data high)
429 **/ 454 */
430static void sht15_end_transmission(struct sht15_data *data) 455static void sht15_end_transmission(struct sht15_data *data)
431{ 456{
432 gpio_direction_output(data->pdata->gpio_data, 1); 457 gpio_direction_output(data->pdata->gpio_data, 1);
@@ -444,12 +469,13 @@ static void sht15_bh_read_data(struct work_struct *work_s)
444 struct sht15_data *data 469 struct sht15_data *data
445 = container_of(work_s, struct sht15_data, 470 = container_of(work_s, struct sht15_data,
446 read_work); 471 read_work);
472
447 /* Firstly, verify the line is low */ 473 /* Firstly, verify the line is low */
448 if (gpio_get_value(data->pdata->gpio_data)) { 474 if (gpio_get_value(data->pdata->gpio_data)) {
449 /* If not, then start the interrupt again - care 475 /*
450 here as could have gone low in meantime so verify 476 * If not, then start the interrupt again - care here as could
451 it hasn't! 477 * have gone low in meantime so verify it hasn't!
452 */ 478 */
453 atomic_set(&data->interrupt_handled, 0); 479 atomic_set(&data->interrupt_handled, 0);
454 enable_irq(gpio_to_irq(data->pdata->gpio_data)); 480 enable_irq(gpio_to_irq(data->pdata->gpio_data));
455 /* If still not occurred or another handler has been scheduled */ 481 /* If still not occurred or another handler has been scheduled */
@@ -457,6 +483,7 @@ static void sht15_bh_read_data(struct work_struct *work_s)
457 || atomic_read(&data->interrupt_handled)) 483 || atomic_read(&data->interrupt_handled))
458 return; 484 return;
459 } 485 }
486
460 /* Read the data back from the device */ 487 /* Read the data back from the device */
461 for (i = 0; i < 16; ++i) { 488 for (i = 0; i < 16; ++i) {
462 val <<= 1; 489 val <<= 1;
@@ -468,19 +495,22 @@ static void sht15_bh_read_data(struct work_struct *work_s)
468 if (i == 7) 495 if (i == 7)
469 sht15_ack(data); 496 sht15_ack(data);
470 } 497 }
498
471 /* Tell the device we are done */ 499 /* Tell the device we are done */
472 sht15_end_transmission(data); 500 sht15_end_transmission(data);
473 501
474 switch (data->flag) { 502 switch (data->state) {
475 case SHT15_READING_TEMP: 503 case SHT15_READING_TEMP:
476 data->val_temp = val; 504 data->val_temp = val;
477 break; 505 break;
478 case SHT15_READING_HUMID: 506 case SHT15_READING_HUMID:
479 data->val_humid = val; 507 data->val_humid = val;
480 break; 508 break;
509 default:
510 break;
481 } 511 }
482 512
483 data->flag = SHT15_READING_NOTHING; 513 data->state = SHT15_READING_NOTHING;
484 wake_up(&data->wait_queue); 514 wake_up(&data->wait_queue);
485} 515}
486 516
@@ -500,10 +530,10 @@ static void sht15_update_voltage(struct work_struct *work_s)
500 * 530 *
501 * Note that as the notification code holds the regulator lock, we have 531 * 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. 532 * to schedule an update of the supply voltage rather than getting it directly.
503 **/ 533 */
504static int sht15_invalidate_voltage(struct notifier_block *nb, 534static int sht15_invalidate_voltage(struct notifier_block *nb,
505 unsigned long event, 535 unsigned long event,
506 void *ignored) 536 void *ignored)
507{ 537{
508 struct sht15_data *data = container_of(nb, struct sht15_data, nb); 538 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
509 539
@@ -521,7 +551,7 @@ static int __devinit sht15_probe(struct platform_device *pdev)
521 551
522 if (!data) { 552 if (!data) {
523 ret = -ENOMEM; 553 ret = -ENOMEM;
524 dev_err(&pdev->dev, "kzalloc failed"); 554 dev_err(&pdev->dev, "kzalloc failed\n");
525 goto error_ret; 555 goto error_ret;
526 } 556 }
527 557
@@ -533,13 +563,16 @@ static int __devinit sht15_probe(struct platform_device *pdev)
533 init_waitqueue_head(&data->wait_queue); 563 init_waitqueue_head(&data->wait_queue);
534 564
535 if (pdev->dev.platform_data == NULL) { 565 if (pdev->dev.platform_data == NULL) {
536 dev_err(&pdev->dev, "no platform data supplied"); 566 dev_err(&pdev->dev, "no platform data supplied\n");
537 goto err_free_data; 567 goto err_free_data;
538 } 568 }
539 data->pdata = pdev->dev.platform_data; 569 data->pdata = pdev->dev.platform_data;
540 data->supply_uV = data->pdata->supply_mv*1000; 570 data->supply_uV = data->pdata->supply_mv * 1000;
541 571
542/* If a regulator is available, query what the supply voltage actually is!*/ 572 /*
573 * If a regulator is available,
574 * query what the supply voltage actually is!
575 */
543 data->reg = regulator_get(data->dev, "vcc"); 576 data->reg = regulator_get(data->dev, "vcc");
544 if (!IS_ERR(data->reg)) { 577 if (!IS_ERR(data->reg)) {
545 int voltage; 578 int voltage;
@@ -549,21 +582,25 @@ static int __devinit sht15_probe(struct platform_device *pdev)
549 data->supply_uV = voltage; 582 data->supply_uV = voltage;
550 583
551 regulator_enable(data->reg); 584 regulator_enable(data->reg);
552 /* setup a notifier block to update this if another device 585 /*
553 * causes the voltage to change */ 586 * Setup a notifier block to update this if another device
587 * causes the voltage to change
588 */
554 data->nb.notifier_call = &sht15_invalidate_voltage; 589 data->nb.notifier_call = &sht15_invalidate_voltage;
555 ret = regulator_register_notifier(data->reg, &data->nb); 590 ret = regulator_register_notifier(data->reg, &data->nb);
556 } 591 }
557/* Try requesting the GPIOs */ 592
593 /* Try requesting the GPIOs */
558 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck"); 594 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
559 if (ret) { 595 if (ret) {
560 dev_err(&pdev->dev, "gpio request failed"); 596 dev_err(&pdev->dev, "gpio request failed\n");
561 goto err_free_data; 597 goto err_free_data;
562 } 598 }
563 gpio_direction_output(data->pdata->gpio_sck, 0); 599 gpio_direction_output(data->pdata->gpio_sck, 0);
600
564 ret = gpio_request(data->pdata->gpio_data, "SHT15 data"); 601 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
565 if (ret) { 602 if (ret) {
566 dev_err(&pdev->dev, "gpio request failed"); 603 dev_err(&pdev->dev, "gpio request failed\n");
567 goto err_release_gpio_sck; 604 goto err_release_gpio_sck;
568 } 605 }
569 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group); 606 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
@@ -578,7 +615,7 @@ static int __devinit sht15_probe(struct platform_device *pdev)
578 "sht15 data", 615 "sht15 data",
579 data); 616 data);
580 if (ret) { 617 if (ret) {
581 dev_err(&pdev->dev, "failed to get irq for data line"); 618 dev_err(&pdev->dev, "failed to get irq for data line\n");
582 goto err_release_gpio_data; 619 goto err_release_gpio_data;
583 } 620 }
584 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data)); 621 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
@@ -590,6 +627,7 @@ static int __devinit sht15_probe(struct platform_device *pdev)
590 ret = PTR_ERR(data->hwmon_dev); 627 ret = PTR_ERR(data->hwmon_dev);
591 goto err_release_irq; 628 goto err_release_irq;
592 } 629 }
630
593 return 0; 631 return 0;
594 632
595err_release_irq: 633err_release_irq:
@@ -601,7 +639,6 @@ err_release_gpio_sck:
601err_free_data: 639err_free_data:
602 kfree(data); 640 kfree(data);
603error_ret: 641error_ret:
604
605 return ret; 642 return ret;
606} 643}
607 644
@@ -609,8 +646,10 @@ static int __devexit sht15_remove(struct platform_device *pdev)
609{ 646{
610 struct sht15_data *data = platform_get_drvdata(pdev); 647 struct sht15_data *data = platform_get_drvdata(pdev);
611 648
612 /* Make sure any reads from the device are done and 649 /*
613 * prevent new ones from beginning */ 650 * Make sure any reads from the device are done and
651 * prevent new ones beginning
652 */
614 mutex_lock(&data->read_lock); 653 mutex_lock(&data->read_lock);
615 hwmon_device_unregister(data->hwmon_dev); 654 hwmon_device_unregister(data->hwmon_dev);
616 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); 655 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
@@ -625,10 +664,10 @@ static int __devexit sht15_remove(struct platform_device *pdev)
625 gpio_free(data->pdata->gpio_sck); 664 gpio_free(data->pdata->gpio_sck);
626 mutex_unlock(&data->read_lock); 665 mutex_unlock(&data->read_lock);
627 kfree(data); 666 kfree(data);
667
628 return 0; 668 return 0;
629} 669}
630 670
631
632/* 671/*
633 * sht_drivers simultaneously refers to __devinit and __devexit function 672 * sht_drivers simultaneously refers to __devinit and __devexit function
634 * which causes spurious section mismatch warning. So use __refdata to 673 * which causes spurious section mismatch warning. So use __refdata to
@@ -673,7 +712,6 @@ static struct platform_driver __refdata sht_drivers[] = {
673 }, 712 },
674}; 713};
675 714
676
677static int __init sht15_init(void) 715static int __init sht15_init(void)
678{ 716{
679 int ret; 717 int ret;
diff --git a/include/linux/sht15.h b/include/linux/sht15.h
index 046bce05ecab..1e302146bdc8 100644
--- a/include/linux/sht15.h
+++ b/include/linux/sht15.h
@@ -8,14 +8,18 @@
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 *
12 * For further information, see the Documentation/hwmon/sht15 file.
11 */ 13 */
12 14
13/** 15/**
14 * struct sht15_platform_data - sht15 connectivity info 16 * struct sht15_platform_data - sht15 connectivity info
15 * @gpio_data: no. of gpio to which bidirectional data line is connected 17 * @gpio_data: no. of gpio to which bidirectional data line is
16 * @gpio_sck: no. of gpio to which the data clock is connected. 18 * connected.
17 * @supply_mv: supply voltage in mv. Overridden by regulator if available. 19 * @gpio_sck: no. of gpio to which the data clock is connected.
18 **/ 20 * @supply_mv: supply voltage in mv. Overridden by regulator if
21 * available.
22 */
19struct sht15_platform_data { 23struct sht15_platform_data {
20 int gpio_data; 24 int gpio_data;
21 int gpio_sck; 25 int gpio_sck;