blob: 3182b3f578e2e122ffd1f83e45ccf80363c76e48 [file] [log] [blame]
Jonathan Cameron251eb402009-04-13 14:39:45 -07001/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
4 * Copyright (c) 2009 Jonathan Cameron
5 *
6 * Copyright (c) 2007 Wouter Horre
7 *
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
10 * published by the Free Software Foundation.
11 *
Vivien Didelot99a03782011-04-12 15:34:36 -040012 * For further information, see the Documentation/hwmon/sht15 file.
Jonathan Cameron251eb402009-04-13 14:39:45 -070013 */
14
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/gpio.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
22#include <linux/mutex.h>
23#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070025#include <linux/delay.h>
26#include <linux/jiffies.h>
27#include <linux/err.h>
28#include <linux/sht15.h>
29#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070031#include <asm/atomic.h>
32
Vivien Didelot99a03782011-04-12 15:34:36 -040033/* Commands */
34#define SHT15_MEASURE_TEMP 0x03
35#define SHT15_MEASURE_RH 0x05
Vivien Didelot181148a2011-04-12 15:34:37 -040036#define SHT15_SOFT_RESET 0x1E
Jonathan Cameron251eb402009-04-13 14:39:45 -070037
Vivien Didelot99a03782011-04-12 15:34:36 -040038/* Min timings */
39#define SHT15_TSCKL 100 /* (nsecs) clock low */
40#define SHT15_TSCKH 100 /* (nsecs) clock high */
41#define SHT15_TSU 150 /* (nsecs) data setup time */
Vivien Didelot181148a2011-04-12 15:34:37 -040042#define SHT15_TSRST 11 /* (msecs) soft reset time */
Jonathan Cameron251eb402009-04-13 14:39:45 -070043
Vivien Didelot99a03782011-04-12 15:34:36 -040044/* Actions the driver may be doing */
45enum sht15_state {
46 SHT15_READING_NOTHING,
47 SHT15_READING_TEMP,
48 SHT15_READING_HUMID
49};
Jonathan Cameron251eb402009-04-13 14:39:45 -070050
51/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * struct sht15_temppair - elements of voltage dependent temp calc
Jonathan Cameron251eb402009-04-13 14:39:45 -070053 * @vdd: supply voltage in microvolts
54 * @d1: see data sheet
55 */
56struct sht15_temppair {
57 int vdd; /* microvolts */
58 int d1;
59};
60
Vivien Didelot99a03782011-04-12 15:34:36 -040061/* Table 9 from datasheet - relates temperature calculation to supply voltage */
Jonathan Cameron251eb402009-04-13 14:39:45 -070062static const struct sht15_temppair temppoints[] = {
63 { 2500000, -39400 },
64 { 3000000, -39600 },
65 { 3500000, -39700 },
66 { 4000000, -39800 },
67 { 5000000, -40100 },
68};
69
70/**
71 * struct sht15_data - device instance specific data
Vivien Didelot99a03782011-04-12 15:34:36 -040072 * @pdata: platform data (gpio's etc).
73 * @read_work: bh of interrupt handler.
74 * @wait_queue: wait queue for getting values from device.
75 * @val_temp: last temperature value read from device.
76 * @val_humid: last humidity value read from device.
77 * @state: state identifying the action the driver is doing.
78 * @measurements_valid: are the current stored measures valid (start condition).
79 * @last_measurement: time of last measure.
80 * @read_lock: mutex to ensure only one read in progress at a time.
81 * @dev: associate device structure.
82 * @hwmon_dev: device associated with hwmon subsystem.
83 * @reg: associated regulator (if specified).
84 * @nb: notifier block to handle notifications of voltage
85 * changes.
86 * @supply_uV: local copy of supply voltage used to allow use of
87 * regulator consumer if available.
88 * @supply_uV_valid: indicates that an updated value has not yet been
89 * obtained from the regulator and so any calculations
90 * based upon it will be invalid.
91 * @update_supply_work: work struct that is used to update the supply_uV.
92 * @interrupt_handled: flag used to indicate a handler has been scheduled.
Jonathan Cameron251eb402009-04-13 14:39:45 -070093 */
94struct sht15_data {
95 struct sht15_platform_data *pdata;
96 struct work_struct read_work;
97 wait_queue_head_t wait_queue;
98 uint16_t val_temp;
99 uint16_t val_humid;
Vivien Didelot99a03782011-04-12 15:34:36 -0400100 enum sht15_state state;
101 bool measurements_valid;
102 unsigned long last_measurement;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700103 struct mutex read_lock;
104 struct device *dev;
105 struct device *hwmon_dev;
106 struct regulator *reg;
107 struct notifier_block nb;
108 int supply_uV;
Vivien Didelot99a03782011-04-12 15:34:36 -0400109 bool supply_uV_valid;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700110 struct work_struct update_supply_work;
111 atomic_t interrupt_handled;
112};
113
114/**
115 * sht15_connection_reset() - reset the comms interface
116 * @data: sht15 specific data
117 *
118 * This implements section 3.4 of the data sheet
119 */
120static void sht15_connection_reset(struct sht15_data *data)
121{
122 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400123
Jonathan Cameron251eb402009-04-13 14:39:45 -0700124 gpio_direction_output(data->pdata->gpio_data, 1);
125 ndelay(SHT15_TSCKL);
126 gpio_set_value(data->pdata->gpio_sck, 0);
127 ndelay(SHT15_TSCKL);
128 for (i = 0; i < 9; ++i) {
129 gpio_set_value(data->pdata->gpio_sck, 1);
130 ndelay(SHT15_TSCKH);
131 gpio_set_value(data->pdata->gpio_sck, 0);
132 ndelay(SHT15_TSCKL);
133 }
134}
Vivien Didelot99a03782011-04-12 15:34:36 -0400135
Jonathan Cameron251eb402009-04-13 14:39:45 -0700136/**
137 * sht15_send_bit() - send an individual bit to the device
138 * @data: device state data
139 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400140 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700141static inline void sht15_send_bit(struct sht15_data *data, int val)
142{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700143 gpio_set_value(data->pdata->gpio_data, val);
144 ndelay(SHT15_TSU);
145 gpio_set_value(data->pdata->gpio_sck, 1);
146 ndelay(SHT15_TSCKH);
147 gpio_set_value(data->pdata->gpio_sck, 0);
148 ndelay(SHT15_TSCKL); /* clock low time */
149}
150
151/**
152 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700153 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400154 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700155 * Timings for this are not documented on the data sheet, so very
156 * conservative ones used in implementation. This implements
157 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400158 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700159static void sht15_transmission_start(struct sht15_data *data)
160{
161 /* ensure data is high and output */
162 gpio_direction_output(data->pdata->gpio_data, 1);
163 ndelay(SHT15_TSU);
164 gpio_set_value(data->pdata->gpio_sck, 0);
165 ndelay(SHT15_TSCKL);
166 gpio_set_value(data->pdata->gpio_sck, 1);
167 ndelay(SHT15_TSCKH);
168 gpio_set_value(data->pdata->gpio_data, 0);
169 ndelay(SHT15_TSU);
170 gpio_set_value(data->pdata->gpio_sck, 0);
171 ndelay(SHT15_TSCKL);
172 gpio_set_value(data->pdata->gpio_sck, 1);
173 ndelay(SHT15_TSCKH);
174 gpio_set_value(data->pdata->gpio_data, 1);
175 ndelay(SHT15_TSU);
176 gpio_set_value(data->pdata->gpio_sck, 0);
177 ndelay(SHT15_TSCKL);
178}
Vivien Didelot99a03782011-04-12 15:34:36 -0400179
Jonathan Cameron251eb402009-04-13 14:39:45 -0700180/**
181 * sht15_send_byte() - send a single byte to the device
182 * @data: device state
183 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400184 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700185static void sht15_send_byte(struct sht15_data *data, u8 byte)
186{
187 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400188
Jonathan Cameron251eb402009-04-13 14:39:45 -0700189 for (i = 0; i < 8; i++) {
190 sht15_send_bit(data, !!(byte & 0x80));
191 byte <<= 1;
192 }
193}
Vivien Didelot99a03782011-04-12 15:34:36 -0400194
Jonathan Cameron251eb402009-04-13 14:39:45 -0700195/**
196 * sht15_wait_for_response() - checks for ack from device
197 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400198 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700199static int sht15_wait_for_response(struct sht15_data *data)
200{
201 gpio_direction_input(data->pdata->gpio_data);
202 gpio_set_value(data->pdata->gpio_sck, 1);
203 ndelay(SHT15_TSCKH);
204 if (gpio_get_value(data->pdata->gpio_data)) {
205 gpio_set_value(data->pdata->gpio_sck, 0);
206 dev_err(data->dev, "Command not acknowledged\n");
207 sht15_connection_reset(data);
208 return -EIO;
209 }
210 gpio_set_value(data->pdata->gpio_sck, 0);
211 ndelay(SHT15_TSCKL);
212 return 0;
213}
214
215/**
216 * sht15_send_cmd() - Sends a command to the device.
217 * @data: device state
218 * @cmd: command byte to be sent
219 *
220 * On entry, sck is output low, data is output pull high
221 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400222 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700223static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
224{
225 int ret = 0;
Vivien Didelot99a03782011-04-12 15:34:36 -0400226
Jonathan Cameron251eb402009-04-13 14:39:45 -0700227 sht15_transmission_start(data);
228 sht15_send_byte(data, cmd);
229 ret = sht15_wait_for_response(data);
230 return ret;
231}
Vivien Didelot99a03782011-04-12 15:34:36 -0400232
Jonathan Cameron251eb402009-04-13 14:39:45 -0700233/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400234 * sht15_soft_reset() - send a soft reset command
235 * @data: sht15 specific data.
236 *
237 * As described in section 3.2 of the datasheet.
238 */
239static int sht15_soft_reset(struct sht15_data *data)
240{
241 int ret;
242
243 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
244 if (ret)
245 return ret;
246 msleep(SHT15_TSRST);
247
248 return 0;
249}
250
251/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400252 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700253 * @data: device instance specific data
254 * @command: command sent to request value
255 * @timeout_msecs: timeout after which comms are assumed
256 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400257 */
258static int sht15_measurement(struct sht15_data *data,
259 int command,
260 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700261{
262 int ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400263
Jonathan Cameron251eb402009-04-13 14:39:45 -0700264 ret = sht15_send_cmd(data, command);
265 if (ret)
266 return ret;
267
268 gpio_direction_input(data->pdata->gpio_data);
269 atomic_set(&data->interrupt_handled, 0);
270
271 enable_irq(gpio_to_irq(data->pdata->gpio_data));
272 if (gpio_get_value(data->pdata->gpio_data) == 0) {
273 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300274 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700275 if (!atomic_read(&data->interrupt_handled))
276 schedule_work(&data->read_work);
277 }
278 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400279 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700280 msecs_to_jiffies(timeout_msecs));
281 if (ret == 0) {/* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200282 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Jonathan Cameron251eb402009-04-13 14:39:45 -0700283 sht15_connection_reset(data);
284 return -ETIME;
285 }
286 return 0;
287}
288
289/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400290 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700291 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400292 */
293static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700294{
295 int ret = 0;
296 int timeout = HZ;
297
298 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400299 if (time_after(jiffies, data->last_measurement + timeout)
300 || !data->measurements_valid) {
301 data->state = SHT15_READING_HUMID;
302 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700303 if (ret)
304 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400305 data->state = SHT15_READING_TEMP;
306 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700307 if (ret)
308 goto error_ret;
Vivien Didelot99a03782011-04-12 15:34:36 -0400309 data->measurements_valid = true;
310 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700311 }
312error_ret:
313 mutex_unlock(&data->read_lock);
314
315 return ret;
316}
317
318/**
319 * sht15_calc_temp() - convert the raw reading to a temperature
320 * @data: device state
321 *
322 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400323 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700324static inline int sht15_calc_temp(struct sht15_data *data)
325{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200326 int d1 = temppoints[0].d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700327 int i;
328
Jerome Oufella328a2c22010-04-14 16:14:07 +0200329 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700330 /* Find pointer to interpolate */
331 if (data->supply_uV > temppoints[i - 1].vdd) {
Jerome Oufella328a2c22010-04-14 16:14:07 +0200332 d1 = (data->supply_uV - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700333 * (temppoints[i].d1 - temppoints[i - 1].d1)
334 / (temppoints[i].vdd - temppoints[i - 1].vdd)
335 + temppoints[i - 1].d1;
336 break;
337 }
338
Vivien Didelot99a03782011-04-12 15:34:36 -0400339 return data->val_temp * 10 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700340}
341
342/**
343 * sht15_calc_humid() - using last temperature convert raw to humid
344 * @data: device state
345 *
346 * This is the temperature compensated version as per section 4.2 of
347 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400348 *
349 * The sensor is assumed to be V3, which is compatible with V4.
350 * Humidity conversion coefficients are shown in table 7 of the datasheet.
351 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700352static inline int sht15_calc_humid(struct sht15_data *data)
353{
Vivien Didelot99a03782011-04-12 15:34:36 -0400354 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700355 int temp = sht15_calc_temp(data);
356
357 const int c1 = -4;
358 const int c2 = 40500; /* x 10 ^ -6 */
Vivien Didelot99a03782011-04-12 15:34:36 -0400359 const int c3 = -28; /* x 10 ^ -7 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700360
Vivien Didelot99a03782011-04-12 15:34:36 -0400361 rh_linear = c1 * 1000
362 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100363 + (data->val_humid * data->val_humid * c3) / 10000;
Jonathan Cameron4235f682009-12-16 21:38:28 +0100364 return (temp - 25000) * (10000 + 80 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400365 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700366}
367
Vivien Didelot99a03782011-04-12 15:34:36 -0400368/**
369 * sht15_show_temp() - show temperature measurement value in sysfs
370 * @dev: device.
371 * @attr: device attribute.
372 * @buf: sysfs buffer where measurement values are written to.
373 *
374 * Will be called on read access to temp1_input sysfs attribute.
375 * Returns number of bytes written into buffer, negative errno on error.
376 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700377static ssize_t sht15_show_temp(struct device *dev,
378 struct device_attribute *attr,
379 char *buf)
380{
381 int ret;
382 struct sht15_data *data = dev_get_drvdata(dev);
383
384 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400385 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700386
387 return ret ? ret : sprintf(buf, "%d\n",
388 sht15_calc_temp(data));
389}
390
Vivien Didelot99a03782011-04-12 15:34:36 -0400391/**
392 * sht15_show_humidity() - show humidity measurement value in sysfs
393 * @dev: device.
394 * @attr: device attribute.
395 * @buf: sysfs buffer where measurement values are written to.
396 *
397 * Will be called on read access to humidity1_input sysfs attribute.
398 * Returns number of bytes written into buffer, negative errno on error.
399 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700400static ssize_t sht15_show_humidity(struct device *dev,
401 struct device_attribute *attr,
402 char *buf)
403{
404 int ret;
405 struct sht15_data *data = dev_get_drvdata(dev);
406
Vivien Didelot99a03782011-04-12 15:34:36 -0400407 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700408
409 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
410
Vivien Didelot99a03782011-04-12 15:34:36 -0400411}
412
Jonathan Cameron251eb402009-04-13 14:39:45 -0700413static ssize_t show_name(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
416{
417 struct platform_device *pdev = to_platform_device(dev);
418 return sprintf(buf, "%s\n", pdev->name);
419}
420
Vivien Didelot99a03782011-04-12 15:34:36 -0400421static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
422 sht15_show_temp, NULL, 0);
423static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
424 sht15_show_humidity, NULL, 0);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700425static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
426static struct attribute *sht15_attrs[] = {
427 &sensor_dev_attr_temp1_input.dev_attr.attr,
428 &sensor_dev_attr_humidity1_input.dev_attr.attr,
429 &dev_attr_name.attr,
430 NULL,
431};
432
433static const struct attribute_group sht15_attr_group = {
434 .attrs = sht15_attrs,
435};
436
437static irqreturn_t sht15_interrupt_fired(int irq, void *d)
438{
439 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400440
Jonathan Cameron251eb402009-04-13 14:39:45 -0700441 /* First disable the interrupt */
442 disable_irq_nosync(irq);
443 atomic_inc(&data->interrupt_handled);
444 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400445 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700446 schedule_work(&data->read_work);
447 return IRQ_HANDLED;
448}
449
Vivien Didelot99a03782011-04-12 15:34:36 -0400450/**
451 * sht15_ack() - Send an ack to the device
452 *
453 * Each byte of data is acknowledged by pulling the data line
Jonathan Cameron251eb402009-04-13 14:39:45 -0700454 * low for one clock pulse.
455 */
456static void sht15_ack(struct sht15_data *data)
457{
458 gpio_direction_output(data->pdata->gpio_data, 0);
459 ndelay(SHT15_TSU);
460 gpio_set_value(data->pdata->gpio_sck, 1);
461 ndelay(SHT15_TSU);
462 gpio_set_value(data->pdata->gpio_sck, 0);
463 ndelay(SHT15_TSU);
464 gpio_set_value(data->pdata->gpio_data, 1);
465
466 gpio_direction_input(data->pdata->gpio_data);
467}
Vivien Didelot99a03782011-04-12 15:34:36 -0400468
Jonathan Cameron251eb402009-04-13 14:39:45 -0700469/**
470 * sht15_end_transmission() - notify device of end of transmission
471 * @data: device state
472 *
473 * This is basically a NAK. (single clock pulse, data high)
Vivien Didelot99a03782011-04-12 15:34:36 -0400474 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700475static void sht15_end_transmission(struct sht15_data *data)
476{
477 gpio_direction_output(data->pdata->gpio_data, 1);
478 ndelay(SHT15_TSU);
479 gpio_set_value(data->pdata->gpio_sck, 1);
480 ndelay(SHT15_TSCKH);
481 gpio_set_value(data->pdata->gpio_sck, 0);
482 ndelay(SHT15_TSCKL);
483}
484
485static void sht15_bh_read_data(struct work_struct *work_s)
486{
487 int i;
488 uint16_t val = 0;
489 struct sht15_data *data
490 = container_of(work_s, struct sht15_data,
491 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400492
Jonathan Cameron251eb402009-04-13 14:39:45 -0700493 /* Firstly, verify the line is low */
494 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400495 /*
496 * If not, then start the interrupt again - care here as could
497 * have gone low in meantime so verify it hasn't!
498 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700499 atomic_set(&data->interrupt_handled, 0);
500 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300501 /* If still not occurred or another handler has been scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700502 if (gpio_get_value(data->pdata->gpio_data)
503 || atomic_read(&data->interrupt_handled))
504 return;
505 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400506
Jonathan Cameron251eb402009-04-13 14:39:45 -0700507 /* Read the data back from the device */
508 for (i = 0; i < 16; ++i) {
509 val <<= 1;
510 gpio_set_value(data->pdata->gpio_sck, 1);
511 ndelay(SHT15_TSCKH);
512 val |= !!gpio_get_value(data->pdata->gpio_data);
513 gpio_set_value(data->pdata->gpio_sck, 0);
514 ndelay(SHT15_TSCKL);
515 if (i == 7)
516 sht15_ack(data);
517 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400518
Jonathan Cameron251eb402009-04-13 14:39:45 -0700519 /* Tell the device we are done */
520 sht15_end_transmission(data);
521
Vivien Didelot99a03782011-04-12 15:34:36 -0400522 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700523 case SHT15_READING_TEMP:
524 data->val_temp = val;
525 break;
526 case SHT15_READING_HUMID:
527 data->val_humid = val;
528 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400529 default:
530 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700531 }
532
Vivien Didelot99a03782011-04-12 15:34:36 -0400533 data->state = SHT15_READING_NOTHING;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700534 wake_up(&data->wait_queue);
535}
536
537static void sht15_update_voltage(struct work_struct *work_s)
538{
539 struct sht15_data *data
540 = container_of(work_s, struct sht15_data,
541 update_supply_work);
542 data->supply_uV = regulator_get_voltage(data->reg);
543}
544
545/**
546 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
547 * @nb: associated notification structure
548 * @event: voltage regulator state change event code
549 * @ignored: function parameter - ignored here
550 *
551 * Note that as the notification code holds the regulator lock, we have
552 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400553 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700554static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400555 unsigned long event,
556 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700557{
558 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
559
560 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
561 data->supply_uV_valid = false;
562 schedule_work(&data->update_supply_work);
563
564 return NOTIFY_OK;
565}
566
567static int __devinit sht15_probe(struct platform_device *pdev)
568{
569 int ret = 0;
570 struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
571
572 if (!data) {
573 ret = -ENOMEM;
Vivien Didelot99a03782011-04-12 15:34:36 -0400574 dev_err(&pdev->dev, "kzalloc failed\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700575 goto error_ret;
576 }
577
578 INIT_WORK(&data->read_work, sht15_bh_read_data);
579 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
580 platform_set_drvdata(pdev, data);
581 mutex_init(&data->read_lock);
582 data->dev = &pdev->dev;
583 init_waitqueue_head(&data->wait_queue);
584
585 if (pdev->dev.platform_data == NULL) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400586 dev_err(&pdev->dev, "no platform data supplied\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700587 goto err_free_data;
588 }
589 data->pdata = pdev->dev.platform_data;
Vivien Didelot99a03782011-04-12 15:34:36 -0400590 data->supply_uV = data->pdata->supply_mv * 1000;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700591
Vivien Didelot99a03782011-04-12 15:34:36 -0400592 /*
593 * If a regulator is available,
594 * query what the supply voltage actually is!
595 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700596 data->reg = regulator_get(data->dev, "vcc");
597 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +0200598 int voltage;
599
600 voltage = regulator_get_voltage(data->reg);
601 if (voltage)
602 data->supply_uV = voltage;
603
Jonathan Cameron251eb402009-04-13 14:39:45 -0700604 regulator_enable(data->reg);
Vivien Didelot99a03782011-04-12 15:34:36 -0400605 /*
606 * Setup a notifier block to update this if another device
607 * causes the voltage to change
608 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700609 data->nb.notifier_call = &sht15_invalidate_voltage;
610 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -0400611 if (ret) {
612 dev_err(&pdev->dev,
613 "regulator notifier request failed\n");
614 regulator_disable(data->reg);
615 regulator_put(data->reg);
616 goto err_free_data;
617 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700618 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400619
620 /* Try requesting the GPIOs */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700621 ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
622 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400623 dev_err(&pdev->dev, "gpio request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -0400624 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700625 }
626 gpio_direction_output(data->pdata->gpio_sck, 0);
Vivien Didelot99a03782011-04-12 15:34:36 -0400627
Jonathan Cameron251eb402009-04-13 14:39:45 -0700628 ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
629 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400630 dev_err(&pdev->dev, "gpio request failed\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700631 goto err_release_gpio_sck;
632 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700633
634 ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
635 sht15_interrupt_fired,
636 IRQF_TRIGGER_FALLING,
637 "sht15 data",
638 data);
639 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400640 dev_err(&pdev->dev, "failed to get irq for data line\n");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700641 goto err_release_gpio_data;
642 }
643 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
644 sht15_connection_reset(data);
Vivien Didelot181148a2011-04-12 15:34:37 -0400645 ret = sht15_soft_reset(data);
646 if (ret)
647 goto err_release_irq;
648
649 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
650 if (ret) {
651 dev_err(&pdev->dev, "sysfs create failed\n");
652 goto err_release_irq;
653 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700654
655 data->hwmon_dev = hwmon_device_register(data->dev);
656 if (IS_ERR(data->hwmon_dev)) {
657 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -0400658 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700659 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400660
Jonathan Cameron251eb402009-04-13 14:39:45 -0700661 return 0;
662
Vivien Didelot181148a2011-04-12 15:34:37 -0400663err_release_sysfs_group:
664 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Roel Kluin560a64a2009-09-21 17:04:48 -0700665err_release_irq:
666 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700667err_release_gpio_data:
668 gpio_free(data->pdata->gpio_data);
669err_release_gpio_sck:
670 gpio_free(data->pdata->gpio_sck);
Vivien Didelot181148a2011-04-12 15:34:37 -0400671err_release_reg:
672 if (!IS_ERR(data->reg)) {
673 regulator_unregister_notifier(data->reg, &data->nb);
674 regulator_disable(data->reg);
675 regulator_put(data->reg);
676 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700677err_free_data:
678 kfree(data);
679error_ret:
Jonathan Cameron251eb402009-04-13 14:39:45 -0700680 return ret;
681}
682
683static int __devexit sht15_remove(struct platform_device *pdev)
684{
685 struct sht15_data *data = platform_get_drvdata(pdev);
686
Vivien Didelot99a03782011-04-12 15:34:36 -0400687 /*
688 * Make sure any reads from the device are done and
689 * prevent new ones beginning
690 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700691 mutex_lock(&data->read_lock);
692 hwmon_device_unregister(data->hwmon_dev);
693 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
694 if (!IS_ERR(data->reg)) {
695 regulator_unregister_notifier(data->reg, &data->nb);
696 regulator_disable(data->reg);
697 regulator_put(data->reg);
698 }
699
700 free_irq(gpio_to_irq(data->pdata->gpio_data), data);
701 gpio_free(data->pdata->gpio_data);
702 gpio_free(data->pdata->gpio_sck);
703 mutex_unlock(&data->read_lock);
704 kfree(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400705
Jonathan Cameron251eb402009-04-13 14:39:45 -0700706 return 0;
707}
708
Rakib Mullickcb0f1a12009-10-09 20:35:17 +0200709/*
710 * sht_drivers simultaneously refers to __devinit and __devexit function
711 * which causes spurious section mismatch warning. So use __refdata to
712 * get rid from this.
713 */
714static struct platform_driver __refdata sht_drivers[] = {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700715 {
716 .driver = {
717 .name = "sht10",
718 .owner = THIS_MODULE,
719 },
720 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200721 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700722 }, {
723 .driver = {
724 .name = "sht11",
725 .owner = THIS_MODULE,
726 },
727 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200728 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700729 }, {
730 .driver = {
731 .name = "sht15",
732 .owner = THIS_MODULE,
733 },
734 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200735 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700736 }, {
737 .driver = {
738 .name = "sht71",
739 .owner = THIS_MODULE,
740 },
741 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200742 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700743 }, {
744 .driver = {
745 .name = "sht75",
746 .owner = THIS_MODULE,
747 },
748 .probe = sht15_probe,
Jean Delvarecd659fd2009-06-15 18:39:45 +0200749 .remove = __devexit_p(sht15_remove),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700750 },
751};
752
Jonathan Cameron251eb402009-04-13 14:39:45 -0700753static int __init sht15_init(void)
754{
755 int ret;
756 int i;
757
758 for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
759 ret = platform_driver_register(&sht_drivers[i]);
760 if (ret)
761 goto error_unreg;
762 }
763
764 return 0;
765
766error_unreg:
767 while (--i >= 0)
768 platform_driver_unregister(&sht_drivers[i]);
769
770 return ret;
771}
772module_init(sht15_init);
773
774static void __exit sht15_exit(void)
775{
776 int i;
777 for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
778 platform_driver_unregister(&sht_drivers[i]);
779}
780module_exit(sht15_exit);
781
782MODULE_LICENSE("GPL");