blob: 49da089b5de95bdf068d01c11d12262f998cb216 [file] [log] [blame]
Jonathan Cameron251eb402009-04-13 14:39:45 -07001/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04004 * Portions Copyright (c) 2010-2011 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6 *
Jonathan Cameron251eb402009-04-13 14:39:45 -07007 * Copyright (c) 2009 Jonathan Cameron
8 *
9 * Copyright (c) 2007 Wouter Horre
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
Vivien Didelot99a03782011-04-12 15:34:36 -040015 * For further information, see the Documentation/hwmon/sht15 file.
Jonathan Cameron251eb402009-04-13 14:39:45 -070016 */
17
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/gpio.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/mutex.h>
26#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040027#include <linux/sched.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070028#include <linux/delay.h>
29#include <linux/jiffies.h>
30#include <linux/err.h>
31#include <linux/sht15.h>
32#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070034#include <asm/atomic.h>
35
Vivien Didelot99a03782011-04-12 15:34:36 -040036/* Commands */
37#define SHT15_MEASURE_TEMP 0x03
38#define SHT15_MEASURE_RH 0x05
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040039#define SHT15_WRITE_STATUS 0x06
40#define SHT15_READ_STATUS 0x07
Vivien Didelot181148a2011-04-12 15:34:37 -040041#define SHT15_SOFT_RESET 0x1E
Jonathan Cameron251eb402009-04-13 14:39:45 -070042
Vivien Didelot99a03782011-04-12 15:34:36 -040043/* Min timings */
44#define SHT15_TSCKL 100 /* (nsecs) clock low */
45#define SHT15_TSCKH 100 /* (nsecs) clock high */
46#define SHT15_TSU 150 /* (nsecs) data setup time */
Vivien Didelot181148a2011-04-12 15:34:37 -040047#define SHT15_TSRST 11 /* (msecs) soft reset time */
Jonathan Cameron251eb402009-04-13 14:39:45 -070048
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040049/* Status Register Bits */
50#define SHT15_STATUS_LOW_RESOLUTION 0x01
51#define SHT15_STATUS_NO_OTP_RELOAD 0x02
52#define SHT15_STATUS_HEATER 0x04
53#define SHT15_STATUS_LOW_BATTERY 0x40
54
Vivien Didelot99a03782011-04-12 15:34:36 -040055/* Actions the driver may be doing */
56enum sht15_state {
57 SHT15_READING_NOTHING,
58 SHT15_READING_TEMP,
59 SHT15_READING_HUMID
60};
Jonathan Cameron251eb402009-04-13 14:39:45 -070061
62/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030063 * struct sht15_temppair - elements of voltage dependent temp calc
Jonathan Cameron251eb402009-04-13 14:39:45 -070064 * @vdd: supply voltage in microvolts
65 * @d1: see data sheet
66 */
67struct sht15_temppair {
68 int vdd; /* microvolts */
69 int d1;
70};
71
Vivien Didelot99a03782011-04-12 15:34:36 -040072/* Table 9 from datasheet - relates temperature calculation to supply voltage */
Jonathan Cameron251eb402009-04-13 14:39:45 -070073static const struct sht15_temppair temppoints[] = {
74 { 2500000, -39400 },
75 { 3000000, -39600 },
76 { 3500000, -39700 },
77 { 4000000, -39800 },
78 { 5000000, -40100 },
79};
80
81/**
82 * struct sht15_data - device instance specific data
Vivien Didelot99a03782011-04-12 15:34:36 -040083 * @pdata: platform data (gpio's etc).
84 * @read_work: bh of interrupt handler.
85 * @wait_queue: wait queue for getting values from device.
86 * @val_temp: last temperature value read from device.
87 * @val_humid: last humidity value read from device.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040088 * @val_status: last status register value read from device.
Vivien Didelot99a03782011-04-12 15:34:36 -040089 * @state: state identifying the action the driver is doing.
90 * @measurements_valid: are the current stored measures valid (start condition).
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040091 * @status_valid: is the current stored status valid (start condition).
Vivien Didelot99a03782011-04-12 15:34:36 -040092 * @last_measurement: time of last measure.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040093 * @last_status: time of last status reading.
Vivien Didelot99a03782011-04-12 15:34:36 -040094 * @read_lock: mutex to ensure only one read in progress at a time.
95 * @dev: associate device structure.
96 * @hwmon_dev: device associated with hwmon subsystem.
97 * @reg: associated regulator (if specified).
98 * @nb: notifier block to handle notifications of voltage
99 * changes.
100 * @supply_uV: local copy of supply voltage used to allow use of
101 * regulator consumer if available.
102 * @supply_uV_valid: indicates that an updated value has not yet been
103 * obtained from the regulator and so any calculations
104 * based upon it will be invalid.
105 * @update_supply_work: work struct that is used to update the supply_uV.
106 * @interrupt_handled: flag used to indicate a handler has been scheduled.
Jonathan Cameron251eb402009-04-13 14:39:45 -0700107 */
108struct sht15_data {
109 struct sht15_platform_data *pdata;
110 struct work_struct read_work;
111 wait_queue_head_t wait_queue;
112 uint16_t val_temp;
113 uint16_t val_humid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400114 u8 val_status;
Vivien Didelot99a03782011-04-12 15:34:36 -0400115 enum sht15_state state;
116 bool measurements_valid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400117 bool status_valid;
Vivien Didelot99a03782011-04-12 15:34:36 -0400118 unsigned long last_measurement;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400119 unsigned long last_status;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700120 struct mutex read_lock;
121 struct device *dev;
122 struct device *hwmon_dev;
123 struct regulator *reg;
124 struct notifier_block nb;
125 int supply_uV;
Vivien Didelot99a03782011-04-12 15:34:36 -0400126 bool supply_uV_valid;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700127 struct work_struct update_supply_work;
128 atomic_t interrupt_handled;
129};
130
131/**
132 * sht15_connection_reset() - reset the comms interface
133 * @data: sht15 specific data
134 *
135 * This implements section 3.4 of the data sheet
136 */
137static void sht15_connection_reset(struct sht15_data *data)
138{
139 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400140
Jonathan Cameron251eb402009-04-13 14:39:45 -0700141 gpio_direction_output(data->pdata->gpio_data, 1);
142 ndelay(SHT15_TSCKL);
143 gpio_set_value(data->pdata->gpio_sck, 0);
144 ndelay(SHT15_TSCKL);
145 for (i = 0; i < 9; ++i) {
146 gpio_set_value(data->pdata->gpio_sck, 1);
147 ndelay(SHT15_TSCKH);
148 gpio_set_value(data->pdata->gpio_sck, 0);
149 ndelay(SHT15_TSCKL);
150 }
151}
Vivien Didelot99a03782011-04-12 15:34:36 -0400152
Jonathan Cameron251eb402009-04-13 14:39:45 -0700153/**
154 * sht15_send_bit() - send an individual bit to the device
155 * @data: device state data
156 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400157 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700158static inline void sht15_send_bit(struct sht15_data *data, int val)
159{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700160 gpio_set_value(data->pdata->gpio_data, val);
161 ndelay(SHT15_TSU);
162 gpio_set_value(data->pdata->gpio_sck, 1);
163 ndelay(SHT15_TSCKH);
164 gpio_set_value(data->pdata->gpio_sck, 0);
165 ndelay(SHT15_TSCKL); /* clock low time */
166}
167
168/**
169 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700170 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400171 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700172 * Timings for this are not documented on the data sheet, so very
173 * conservative ones used in implementation. This implements
174 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400175 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700176static void sht15_transmission_start(struct sht15_data *data)
177{
178 /* ensure data is high and output */
179 gpio_direction_output(data->pdata->gpio_data, 1);
180 ndelay(SHT15_TSU);
181 gpio_set_value(data->pdata->gpio_sck, 0);
182 ndelay(SHT15_TSCKL);
183 gpio_set_value(data->pdata->gpio_sck, 1);
184 ndelay(SHT15_TSCKH);
185 gpio_set_value(data->pdata->gpio_data, 0);
186 ndelay(SHT15_TSU);
187 gpio_set_value(data->pdata->gpio_sck, 0);
188 ndelay(SHT15_TSCKL);
189 gpio_set_value(data->pdata->gpio_sck, 1);
190 ndelay(SHT15_TSCKH);
191 gpio_set_value(data->pdata->gpio_data, 1);
192 ndelay(SHT15_TSU);
193 gpio_set_value(data->pdata->gpio_sck, 0);
194 ndelay(SHT15_TSCKL);
195}
Vivien Didelot99a03782011-04-12 15:34:36 -0400196
Jonathan Cameron251eb402009-04-13 14:39:45 -0700197/**
198 * sht15_send_byte() - send a single byte to the device
199 * @data: device state
200 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400201 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700202static void sht15_send_byte(struct sht15_data *data, u8 byte)
203{
204 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400205
Jonathan Cameron251eb402009-04-13 14:39:45 -0700206 for (i = 0; i < 8; i++) {
207 sht15_send_bit(data, !!(byte & 0x80));
208 byte <<= 1;
209 }
210}
Vivien Didelot99a03782011-04-12 15:34:36 -0400211
Jonathan Cameron251eb402009-04-13 14:39:45 -0700212/**
213 * sht15_wait_for_response() - checks for ack from device
214 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400215 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700216static int sht15_wait_for_response(struct sht15_data *data)
217{
218 gpio_direction_input(data->pdata->gpio_data);
219 gpio_set_value(data->pdata->gpio_sck, 1);
220 ndelay(SHT15_TSCKH);
221 if (gpio_get_value(data->pdata->gpio_data)) {
222 gpio_set_value(data->pdata->gpio_sck, 0);
223 dev_err(data->dev, "Command not acknowledged\n");
224 sht15_connection_reset(data);
225 return -EIO;
226 }
227 gpio_set_value(data->pdata->gpio_sck, 0);
228 ndelay(SHT15_TSCKL);
229 return 0;
230}
231
232/**
233 * sht15_send_cmd() - Sends a command to the device.
234 * @data: device state
235 * @cmd: command byte to be sent
236 *
237 * On entry, sck is output low, data is output pull high
238 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400239 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700240static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
241{
242 int ret = 0;
Vivien Didelot99a03782011-04-12 15:34:36 -0400243
Jonathan Cameron251eb402009-04-13 14:39:45 -0700244 sht15_transmission_start(data);
245 sht15_send_byte(data, cmd);
246 ret = sht15_wait_for_response(data);
247 return ret;
248}
Vivien Didelot99a03782011-04-12 15:34:36 -0400249
Jonathan Cameron251eb402009-04-13 14:39:45 -0700250/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400251 * sht15_soft_reset() - send a soft reset command
252 * @data: sht15 specific data.
253 *
254 * As described in section 3.2 of the datasheet.
255 */
256static int sht15_soft_reset(struct sht15_data *data)
257{
258 int ret;
259
260 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
261 if (ret)
262 return ret;
263 msleep(SHT15_TSRST);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400264 /* device resets default hardware status register value */
265 data->val_status = 0;
Vivien Didelot181148a2011-04-12 15:34:37 -0400266
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400267 return ret;
268}
269
270/**
271 * sht15_end_transmission() - notify device of end of transmission
272 * @data: device state.
273 *
274 * This is basically a NAK (single clock pulse, data high).
275 */
276static void sht15_end_transmission(struct sht15_data *data)
277{
278 gpio_direction_output(data->pdata->gpio_data, 1);
279 ndelay(SHT15_TSU);
280 gpio_set_value(data->pdata->gpio_sck, 1);
281 ndelay(SHT15_TSCKH);
282 gpio_set_value(data->pdata->gpio_sck, 0);
283 ndelay(SHT15_TSCKL);
284}
285
286/**
287 * sht15_read_byte() - Read a byte back from the device
288 * @data: device state.
289 */
290static u8 sht15_read_byte(struct sht15_data *data)
291{
292 int i;
293 u8 byte = 0;
294
295 for (i = 0; i < 8; ++i) {
296 byte <<= 1;
297 gpio_set_value(data->pdata->gpio_sck, 1);
298 ndelay(SHT15_TSCKH);
299 byte |= !!gpio_get_value(data->pdata->gpio_data);
300 gpio_set_value(data->pdata->gpio_sck, 0);
301 ndelay(SHT15_TSCKL);
302 }
303 return byte;
304}
305
306/**
307 * sht15_send_status() - write the status register byte
308 * @data: sht15 specific data.
309 * @status: the byte to set the status register with.
310 *
311 * As described in figure 14 and table 5 of the datasheet.
312 */
313static int sht15_send_status(struct sht15_data *data, u8 status)
314{
315 int ret;
316
317 ret = sht15_send_cmd(data, SHT15_WRITE_STATUS);
318 if (ret)
319 return ret;
320 gpio_direction_output(data->pdata->gpio_data, 1);
321 ndelay(SHT15_TSU);
322 sht15_send_byte(data, status);
323 ret = sht15_wait_for_response(data);
324 if (ret)
325 return ret;
326
327 data->val_status = status;
Vivien Didelot181148a2011-04-12 15:34:37 -0400328 return 0;
329}
330
331/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400332 * sht15_update_status() - get updated status register from device if too old
333 * @data: device instance specific data.
334 *
335 * As described in figure 15 and table 5 of the datasheet.
336 */
337static int sht15_update_status(struct sht15_data *data)
338{
339 int ret = 0;
340 u8 status;
341 int timeout = HZ;
342
343 mutex_lock(&data->read_lock);
344 if (time_after(jiffies, data->last_status + timeout)
345 || !data->status_valid) {
346 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
347 if (ret)
348 goto error_ret;
349 status = sht15_read_byte(data);
350
351 sht15_end_transmission(data);
352
353 data->val_status = status;
354 data->status_valid = true;
355 data->last_status = jiffies;
356 }
357error_ret:
358 mutex_unlock(&data->read_lock);
359
360 return ret;
361}
362
363/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400364 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700365 * @data: device instance specific data
366 * @command: command sent to request value
367 * @timeout_msecs: timeout after which comms are assumed
368 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400369 */
370static int sht15_measurement(struct sht15_data *data,
371 int command,
372 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700373{
374 int ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400375
Jonathan Cameron251eb402009-04-13 14:39:45 -0700376 ret = sht15_send_cmd(data, command);
377 if (ret)
378 return ret;
379
380 gpio_direction_input(data->pdata->gpio_data);
381 atomic_set(&data->interrupt_handled, 0);
382
383 enable_irq(gpio_to_irq(data->pdata->gpio_data));
384 if (gpio_get_value(data->pdata->gpio_data) == 0) {
385 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300386 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700387 if (!atomic_read(&data->interrupt_handled))
388 schedule_work(&data->read_work);
389 }
390 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400391 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700392 msecs_to_jiffies(timeout_msecs));
393 if (ret == 0) {/* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200394 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Jonathan Cameron251eb402009-04-13 14:39:45 -0700395 sht15_connection_reset(data);
396 return -ETIME;
397 }
398 return 0;
399}
400
401/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400402 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700403 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400404 */
405static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700406{
407 int ret = 0;
408 int timeout = HZ;
409
410 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400411 if (time_after(jiffies, data->last_measurement + timeout)
412 || !data->measurements_valid) {
413 data->state = SHT15_READING_HUMID;
414 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700415 if (ret)
416 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400417 data->state = SHT15_READING_TEMP;
418 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700419 if (ret)
420 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400421 data->measurements_valid = true;
422 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700423 }
424error_ret:
425 mutex_unlock(&data->read_lock);
426
427 return ret;
428}
429
430/**
431 * sht15_calc_temp() - convert the raw reading to a temperature
432 * @data: device state
433 *
434 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400435 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700436static inline int sht15_calc_temp(struct sht15_data *data)
437{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200438 int d1 = temppoints[0].d1;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400439 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700440 int i;
441
Jerome Oufella328a2c22010-04-14 16:14:07 +0200442 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700443 /* Find pointer to interpolate */
444 if (data->supply_uV > temppoints[i - 1].vdd) {
Jerome Oufella328a2c22010-04-14 16:14:07 +0200445 d1 = (data->supply_uV - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700446 * (temppoints[i].d1 - temppoints[i - 1].d1)
447 / (temppoints[i].vdd - temppoints[i - 1].vdd)
448 + temppoints[i - 1].d1;
449 break;
450 }
451
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400452 return data->val_temp * d2 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700453}
454
455/**
456 * sht15_calc_humid() - using last temperature convert raw to humid
457 * @data: device state
458 *
459 * This is the temperature compensated version as per section 4.2 of
460 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400461 *
462 * The sensor is assumed to be V3, which is compatible with V4.
463 * Humidity conversion coefficients are shown in table 7 of the datasheet.
464 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700465static inline int sht15_calc_humid(struct sht15_data *data)
466{
Vivien Didelot99a03782011-04-12 15:34:36 -0400467 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700468 int temp = sht15_calc_temp(data);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400469 int c2, c3;
470 int t2;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700471 const int c1 = -4;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400472
473 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
474 c2 = 648000; /* x 10 ^ -6 */
475 c3 = -7200; /* x 10 ^ -7 */
476 t2 = 1280;
477 } else {
478 c2 = 40500; /* x 10 ^ -6 */
479 c3 = -28; /* x 10 ^ -7 */
480 t2 = 80;
481 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700482
Vivien Didelot99a03782011-04-12 15:34:36 -0400483 rh_linear = c1 * 1000
484 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100485 + (data->val_humid * data->val_humid * c3) / 10000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400486 return (temp - 25000) * (10000 + t2 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400487 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700488}
489
Vivien Didelot99a03782011-04-12 15:34:36 -0400490/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400491 * sht15_show_status() - show status information in sysfs
492 * @dev: device.
493 * @attr: device attribute.
494 * @buf: sysfs buffer where information is written to.
495 *
496 * Will be called on read access to temp1_fault, humidity1_fault
497 * and heater_enable sysfs attributes.
498 * Returns number of bytes written into buffer, negative errno on error.
499 */
500static ssize_t sht15_show_status(struct device *dev,
501 struct device_attribute *attr,
502 char *buf)
503{
504 int ret;
505 struct sht15_data *data = dev_get_drvdata(dev);
506 u8 bit = to_sensor_dev_attr(attr)->index;
507
508 ret = sht15_update_status(data);
509
510 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
511}
512
513/**
514 * sht15_store_heater() - change heater state via sysfs
515 * @dev: device.
516 * @attr: device attribute.
517 * @buf: sysfs buffer to read the new heater state from.
518 * @count: length of the data.
519 *
520 * Will be called on read access to heater_enable sysfs attribute.
521 * Returns number of bytes actually decoded, negative errno on error.
522 */
523static ssize_t sht15_store_heater(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
526{
527 int ret;
528 struct sht15_data *data = dev_get_drvdata(dev);
529 long value;
530 u8 status;
531
532 if (strict_strtol(buf, 10, &value))
533 return -EINVAL;
534
535 mutex_lock(&data->read_lock);
536 status = data->val_status & 0x07;
537 if (!!value)
538 status |= SHT15_STATUS_HEATER;
539 else
540 status &= ~SHT15_STATUS_HEATER;
541
542 ret = sht15_send_status(data, status);
543 mutex_unlock(&data->read_lock);
544
545 return ret ? ret : count;
546}
547
548/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400549 * sht15_show_temp() - show temperature measurement value in sysfs
550 * @dev: device.
551 * @attr: device attribute.
552 * @buf: sysfs buffer where measurement values are written to.
553 *
554 * Will be called on read access to temp1_input sysfs attribute.
555 * Returns number of bytes written into buffer, negative errno on error.
556 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700557static ssize_t sht15_show_temp(struct device *dev,
558 struct device_attribute *attr,
559 char *buf)
560{
561 int ret;
562 struct sht15_data *data = dev_get_drvdata(dev);
563
564 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400565 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700566
567 return ret ? ret : sprintf(buf, "%d\n",
568 sht15_calc_temp(data));
569}
570
Vivien Didelot99a03782011-04-12 15:34:36 -0400571/**
572 * sht15_show_humidity() - show humidity measurement value in sysfs
573 * @dev: device.
574 * @attr: device attribute.
575 * @buf: sysfs buffer where measurement values are written to.
576 *
577 * Will be called on read access to humidity1_input sysfs attribute.
578 * Returns number of bytes written into buffer, negative errno on error.
579 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700580static ssize_t sht15_show_humidity(struct device *dev,
581 struct device_attribute *attr,
582 char *buf)
583{
584 int ret;
585 struct sht15_data *data = dev_get_drvdata(dev);
586
Vivien Didelot99a03782011-04-12 15:34:36 -0400587 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700588
589 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
Vivien Didelot99a03782011-04-12 15:34:36 -0400590}
591
Jonathan Cameron251eb402009-04-13 14:39:45 -0700592static ssize_t show_name(struct device *dev,
593 struct device_attribute *attr,
594 char *buf)
595{
596 struct platform_device *pdev = to_platform_device(dev);
597 return sprintf(buf, "%s\n", pdev->name);
598}
599
Vivien Didelot99a03782011-04-12 15:34:36 -0400600static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
601 sht15_show_temp, NULL, 0);
602static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
603 sht15_show_humidity, NULL, 0);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400604static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
605 SHT15_STATUS_LOW_BATTERY);
606static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
607 SHT15_STATUS_LOW_BATTERY);
608static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
609 sht15_store_heater, SHT15_STATUS_HEATER);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700610static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
611static struct attribute *sht15_attrs[] = {
612 &sensor_dev_attr_temp1_input.dev_attr.attr,
613 &sensor_dev_attr_humidity1_input.dev_attr.attr,
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400614 &sensor_dev_attr_temp1_fault.dev_attr.attr,
615 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
616 &sensor_dev_attr_heater_enable.dev_attr.attr,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700617 &dev_attr_name.attr,
618 NULL,
619};
620
621static const struct attribute_group sht15_attr_group = {
622 .attrs = sht15_attrs,
623};
624
625static irqreturn_t sht15_interrupt_fired(int irq, void *d)
626{
627 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400628
Jonathan Cameron251eb402009-04-13 14:39:45 -0700629 /* First disable the interrupt */
630 disable_irq_nosync(irq);
631 atomic_inc(&data->interrupt_handled);
632 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400633 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700634 schedule_work(&data->read_work);
635 return IRQ_HANDLED;
636}
637
Vivien Didelot99a03782011-04-12 15:34:36 -0400638/**
639 * sht15_ack() - Send an ack to the device
640 *
641 * Each byte of data is acknowledged by pulling the data line
Jonathan Cameron251eb402009-04-13 14:39:45 -0700642 * low for one clock pulse.
643 */
644static void sht15_ack(struct sht15_data *data)
645{
646 gpio_direction_output(data->pdata->gpio_data, 0);
647 ndelay(SHT15_TSU);
648 gpio_set_value(data->pdata->gpio_sck, 1);
649 ndelay(SHT15_TSU);
650 gpio_set_value(data->pdata->gpio_sck, 0);
651 ndelay(SHT15_TSU);
652 gpio_set_value(data->pdata->gpio_data, 1);
653
654 gpio_direction_input(data->pdata->gpio_data);
655}
Vivien Didelot99a03782011-04-12 15:34:36 -0400656
Jonathan Cameron251eb402009-04-13 14:39:45 -0700657static void sht15_bh_read_data(struct work_struct *work_s)
658{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700659 uint16_t val = 0;
660 struct sht15_data *data
661 = container_of(work_s, struct sht15_data,
662 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400663
Jonathan Cameron251eb402009-04-13 14:39:45 -0700664 /* Firstly, verify the line is low */
665 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400666 /*
667 * If not, then start the interrupt again - care here as could
668 * have gone low in meantime so verify it hasn't!
669 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700670 atomic_set(&data->interrupt_handled, 0);
671 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300672 /* If still not occurred or another handler has been scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700673 if (gpio_get_value(data->pdata->gpio_data)
674 || atomic_read(&data->interrupt_handled))
675 return;
676 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400677
Jonathan Cameron251eb402009-04-13 14:39:45 -0700678 /* Read the data back from the device */
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400679 val = sht15_read_byte(data);
680 val <<= 8;
681 sht15_ack(data);
682 val |= sht15_read_byte(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400683
Jonathan Cameron251eb402009-04-13 14:39:45 -0700684 /* Tell the device we are done */
685 sht15_end_transmission(data);
686
Vivien Didelot99a03782011-04-12 15:34:36 -0400687 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700688 case SHT15_READING_TEMP:
689 data->val_temp = val;
690 break;
691 case SHT15_READING_HUMID:
692 data->val_humid = val;
693 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400694 default:
695 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700696 }
697
Vivien Didelot99a03782011-04-12 15:34:36 -0400698 data->state = SHT15_READING_NOTHING;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700699 wake_up(&data->wait_queue);
700}
701
702static void sht15_update_voltage(struct work_struct *work_s)
703{
704 struct sht15_data *data
705 = container_of(work_s, struct sht15_data,
706 update_supply_work);
707 data->supply_uV = regulator_get_voltage(data->reg);
708}
709
710/**
711 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
712 * @nb: associated notification structure
713 * @event: voltage regulator state change event code
714 * @ignored: function parameter - ignored here
715 *
716 * Note that as the notification code holds the regulator lock, we have
717 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400718 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700719static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400720 unsigned long event,
721 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700722{
723 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
724
725 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
726 data->supply_uV_valid = false;
727 schedule_work(&data->update_supply_work);
728
729 return NOTIFY_OK;
730}
731
732static int __devinit sht15_probe(struct platform_device *pdev)
733{
734 int ret = 0;
735 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400736 u8 status = 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700737
738 if (!data) {
739 ret = -ENOMEM;
Vivien Didelot99a03782011-04-12 15:34:36 -0400740 dev_err(&pdev->dev, "kzalloc failed\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700741 goto error_ret;
742 }
743
744 INIT_WORK(&data->read_work, sht15_bh_read_data);
745 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
746 platform_set_drvdata(pdev, data);
747 mutex_init(&data->read_lock);
748 data->dev = &pdev->dev;
749 init_waitqueue_head(&data->wait_queue);
750
751 if (pdev->dev.platform_data == NULL) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400752 dev_err(&pdev->dev, "no platform data supplied\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700753 goto err_free_data;
754 }
755 data->pdata = pdev->dev.platform_data;
Vivien Didelot99a03782011-04-12 15:34:36 -0400756 data->supply_uV = data->pdata->supply_mv * 1000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400757 if (data->pdata->no_otp_reload)
758 status |= SHT15_STATUS_NO_OTP_RELOAD;
759 if (data->pdata->low_resolution)
760 status |= SHT15_STATUS_LOW_RESOLUTION;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700761
Vivien Didelot99a03782011-04-12 15:34:36 -0400762 /*
763 * If a regulator is available,
764 * query what the supply voltage actually is!
765 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700766 data->reg = regulator_get(data->dev, "vcc");
767 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +0200768 int voltage;
769
770 voltage = regulator_get_voltage(data->reg);
771 if (voltage)
772 data->supply_uV = voltage;
773
Jonathan Cameron251eb402009-04-13 14:39:45 -0700774 regulator_enable(data->reg);
Vivien Didelot99a03782011-04-12 15:34:36 -0400775 /*
776 * Setup a notifier block to update this if another device
777 * causes the voltage to change
778 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700779 data->nb.notifier_call = &sht15_invalidate_voltage;
780 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -0400781 if (ret) {
782 dev_err(&pdev->dev,
783 "regulator notifier request failed\n");
784 regulator_disable(data->reg);
785 regulator_put(data->reg);
786 goto err_free_data;
787 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700788 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400789
790 /* Try requesting the GPIOs */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700791 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
792 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400793 dev_err(&pdev->dev, "gpio request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -0400794 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700795 }
796 gpio_direction_output(data->pdata->gpio_sck, 0);
Vivien Didelot99a03782011-04-12 15:34:36 -0400797
Jonathan Cameron251eb402009-04-13 14:39:45 -0700798 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
799 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400800 dev_err(&pdev->dev, "gpio request failed\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700801 goto err_release_gpio_sck;
802 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700803
804 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
805 sht15_interrupt_fired,
806 IRQF_TRIGGER_FALLING,
807 "sht15 data",
808 data);
809 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400810 dev_err(&pdev->dev, "failed to get irq for data line\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700811 goto err_release_gpio_data;
812 }
813 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
814 sht15_connection_reset(data);
Vivien Didelot181148a2011-04-12 15:34:37 -0400815 ret = sht15_soft_reset(data);
816 if (ret)
817 goto err_release_irq;
818
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400819 /* write status with platform data options */
820 if (status) {
821 ret = sht15_send_status(data, status);
822 if (ret)
823 goto err_release_irq;
824 }
825
Vivien Didelot181148a2011-04-12 15:34:37 -0400826 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
827 if (ret) {
828 dev_err(&pdev->dev, "sysfs create failed\n");
829 goto err_release_irq;
830 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700831
832 data->hwmon_dev = hwmon_device_register(data->dev);
833 if (IS_ERR(data->hwmon_dev)) {
834 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -0400835 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700836 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400837
Jonathan Cameron251eb402009-04-13 14:39:45 -0700838 return 0;
839
Vivien Didelot181148a2011-04-12 15:34:37 -0400840err_release_sysfs_group:
841 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Roel Kluin560a64a2009-09-21 17:04:48 -0700842err_release_irq:
843 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700844err_release_gpio_data:
845 gpio_free(data->pdata->gpio_data);
846err_release_gpio_sck:
847 gpio_free(data->pdata->gpio_sck);
Vivien Didelot181148a2011-04-12 15:34:37 -0400848err_release_reg:
849 if (!IS_ERR(data->reg)) {
850 regulator_unregister_notifier(data->reg, &data->nb);
851 regulator_disable(data->reg);
852 regulator_put(data->reg);
853 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700854err_free_data:
855 kfree(data);
856error_ret:
Jonathan Cameron251eb402009-04-13 14:39:45 -0700857 return ret;
858}
859
860static int __devexit sht15_remove(struct platform_device *pdev)
861{
862 struct sht15_data *data = platform_get_drvdata(pdev);
863
Vivien Didelot99a03782011-04-12 15:34:36 -0400864 /*
865 * Make sure any reads from the device are done and
866 * prevent new ones beginning
867 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700868 mutex_lock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400869 if (sht15_soft_reset(data)) {
870 mutex_unlock(&data->read_lock);
871 return -EFAULT;
872 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700873 hwmon_device_unregister(data->hwmon_dev);
874 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
875 if (!IS_ERR(data->reg)) {
876 regulator_unregister_notifier(data->reg, &data->nb);
877 regulator_disable(data->reg);
878 regulator_put(data->reg);
879 }
880
881 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
882 gpio_free(data->pdata->gpio_data);
883 gpio_free(data->pdata->gpio_sck);
884 mutex_unlock(&data->read_lock);
885 kfree(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400886
Jonathan Cameron251eb402009-04-13 14:39:45 -0700887 return 0;
888}
889
Rakib Mullickcb0f1a12009-10-09 20:35:17 +0200890/*
891 * sht_drivers simultaneously refers to __devinit and __devexit function
892 * which causes spurious section mismatch warning. So use __refdata to
893 * get rid from this.
894 */
895static struct platform_driver __refdata sht_drivers[] = {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700896 {
897 .driver = {
898 .name = "sht10",
899 .owner = THIS_MODULE,
900 },
901 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200902 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700903 }, {
904 .driver = {
905 .name = "sht11",
906 .owner = THIS_MODULE,
907 },
908 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200909 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700910 }, {
911 .driver = {
912 .name = "sht15",
913 .owner = THIS_MODULE,
914 },
915 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200916 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700917 }, {
918 .driver = {
919 .name = "sht71",
920 .owner = THIS_MODULE,
921 },
922 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200923 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700924 }, {
925 .driver = {
926 .name = "sht75",
927 .owner = THIS_MODULE,
928 },
929 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200930 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700931 },
932};
933
Jonathan Cameron251eb402009-04-13 14:39:45 -0700934static int __init sht15_init(void)
935{
936 int ret;
937 int i;
938
939 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
940 ret = platform_driver_register(&sht_drivers[i]);
941 if (ret)
942 goto error_unreg;
943 }
944
945 return 0;
946
947error_unreg:
948 while (--i >= 0)
949 platform_driver_unregister(&sht_drivers[i]);
950
951 return ret;
952}
953module_init(sht15_init);
954
955static void __exit sht15_exit(void)
956{
957 int i;
958 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
959 platform_driver_unregister(&sht_drivers[i]);
960}
961module_exit(sht15_exit);
962
963MODULE_LICENSE("GPL");