blob: a2fdbb7d20edcc92f9a1307f4f7105475b41e7ca [file] [log] [blame]
Jonathan Cameron251eb402009-04-13 14:39:45 -07001/*
2 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
3 *
Vivien Didelotedec5af2012-08-30 21:46:19 -04004 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
Jerome Oufella82c74652011-04-12 15:34:39 -04005 * Jerome Oufella <jerome.oufella@savoirfairelinux.com>
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04006 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 *
Jonathan Cameron251eb402009-04-13 14:39:45 -07008 * Copyright (c) 2009 Jonathan Cameron
9 *
10 * Copyright (c) 2007 Wouter Horre
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
Vivien Didelot99a03782011-04-12 15:34:36 -040016 * For further information, see the Documentation/hwmon/sht15 file.
Jonathan Cameron251eb402009-04-13 14:39:45 -070017 */
18
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/gpio.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/hwmon.h>
25#include <linux/hwmon-sysfs.h>
26#include <linux/mutex.h>
Vivien Didelotf9b693e2012-09-12 12:23:44 -040027#include <linux/platform_data/sht15.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070028#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040029#include <linux/sched.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070030#include <linux/delay.h>
31#include <linux/jiffies.h>
32#include <linux/err.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070033#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
yalin wang33836ee2015-08-10 22:41:43 +080036#include <linux/bitrev.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070037
Vivien Didelot99a03782011-04-12 15:34:36 -040038/* Commands */
39#define SHT15_MEASURE_TEMP 0x03
40#define SHT15_MEASURE_RH 0x05
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040041#define SHT15_WRITE_STATUS 0x06
42#define SHT15_READ_STATUS 0x07
Vivien Didelot181148a2011-04-12 15:34:37 -040043#define SHT15_SOFT_RESET 0x1E
Jonathan Cameron251eb402009-04-13 14:39:45 -070044
Vivien Didelot99a03782011-04-12 15:34:36 -040045/* Min timings */
46#define SHT15_TSCKL 100 /* (nsecs) clock low */
47#define SHT15_TSCKH 100 /* (nsecs) clock high */
48#define SHT15_TSU 150 /* (nsecs) data setup time */
Vivien Didelot181148a2011-04-12 15:34:37 -040049#define SHT15_TSRST 11 /* (msecs) soft reset time */
Jonathan Cameron251eb402009-04-13 14:39:45 -070050
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040051/* Status Register Bits */
52#define SHT15_STATUS_LOW_RESOLUTION 0x01
53#define SHT15_STATUS_NO_OTP_RELOAD 0x02
54#define SHT15_STATUS_HEATER 0x04
55#define SHT15_STATUS_LOW_BATTERY 0x40
56
Vivien Didelotedec5af2012-08-30 21:46:19 -040057/* List of supported chips */
58enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
59
Vivien Didelot99a03782011-04-12 15:34:36 -040060/* Actions the driver may be doing */
61enum sht15_state {
62 SHT15_READING_NOTHING,
63 SHT15_READING_TEMP,
64 SHT15_READING_HUMID
65};
Jonathan Cameron251eb402009-04-13 14:39:45 -070066
67/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030068 * struct sht15_temppair - elements of voltage dependent temp calc
Jonathan Cameron251eb402009-04-13 14:39:45 -070069 * @vdd: supply voltage in microvolts
70 * @d1: see data sheet
71 */
72struct sht15_temppair {
73 int vdd; /* microvolts */
74 int d1;
75};
76
Vivien Didelot99a03782011-04-12 15:34:36 -040077/* Table 9 from datasheet - relates temperature calculation to supply voltage */
Jonathan Cameron251eb402009-04-13 14:39:45 -070078static const struct sht15_temppair temppoints[] = {
79 { 2500000, -39400 },
80 { 3000000, -39600 },
81 { 3500000, -39700 },
82 { 4000000, -39800 },
83 { 5000000, -40100 },
84};
85
Jerome Oufella82c74652011-04-12 15:34:39 -040086/* Table from CRC datasheet, section 2.4 */
87static const u8 sht15_crc8_table[] = {
88 0, 49, 98, 83, 196, 245, 166, 151,
89 185, 136, 219, 234, 125, 76, 31, 46,
90 67, 114, 33, 16, 135, 182, 229, 212,
91 250, 203, 152, 169, 62, 15, 92, 109,
92 134, 183, 228, 213, 66, 115, 32, 17,
93 63, 14, 93, 108, 251, 202, 153, 168,
94 197, 244, 167, 150, 1, 48, 99, 82,
95 124, 77, 30, 47, 184, 137, 218, 235,
96 61, 12, 95, 110, 249, 200, 155, 170,
97 132, 181, 230, 215, 64, 113, 34, 19,
98 126, 79, 28, 45, 186, 139, 216, 233,
99 199, 246, 165, 148, 3, 50, 97, 80,
100 187, 138, 217, 232, 127, 78, 29, 44,
101 2, 51, 96, 81, 198, 247, 164, 149,
102 248, 201, 154, 171, 60, 13, 94, 111,
103 65, 112, 35, 18, 133, 180, 231, 214,
104 122, 75, 24, 41, 190, 143, 220, 237,
105 195, 242, 161, 144, 7, 54, 101, 84,
106 57, 8, 91, 106, 253, 204, 159, 174,
107 128, 177, 226, 211, 68, 117, 38, 23,
108 252, 205, 158, 175, 56, 9, 90, 107,
109 69, 116, 39, 22, 129, 176, 227, 210,
110 191, 142, 221, 236, 123, 74, 25, 40,
111 6, 55, 100, 85, 194, 243, 160, 145,
112 71, 118, 37, 20, 131, 178, 225, 208,
113 254, 207, 156, 173, 58, 11, 88, 105,
114 4, 53, 102, 87, 192, 241, 162, 147,
115 189, 140, 223, 238, 121, 72, 27, 42,
116 193, 240, 163, 146, 5, 52, 103, 86,
117 120, 73, 26, 43, 188, 141, 222, 239,
118 130, 179, 224, 209, 70, 119, 36, 21,
119 59, 10, 89, 104, 255, 206, 157, 172
120};
121
Jonathan Cameron251eb402009-04-13 14:39:45 -0700122/**
123 * struct sht15_data - device instance specific data
Vivien Didelot99a03782011-04-12 15:34:36 -0400124 * @pdata: platform data (gpio's etc).
125 * @read_work: bh of interrupt handler.
126 * @wait_queue: wait queue for getting values from device.
127 * @val_temp: last temperature value read from device.
128 * @val_humid: last humidity value read from device.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400129 * @val_status: last status register value read from device.
Jerome Oufella82c74652011-04-12 15:34:39 -0400130 * @checksum_ok: last value read from the device passed CRC validation.
131 * @checksumming: flag used to enable the data validation with CRC.
Vivien Didelot99a03782011-04-12 15:34:36 -0400132 * @state: state identifying the action the driver is doing.
133 * @measurements_valid: are the current stored measures valid (start condition).
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400134 * @status_valid: is the current stored status valid (start condition).
Vivien Didelot99a03782011-04-12 15:34:36 -0400135 * @last_measurement: time of last measure.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400136 * @last_status: time of last status reading.
Vivien Didelot99a03782011-04-12 15:34:36 -0400137 * @read_lock: mutex to ensure only one read in progress at a time.
138 * @dev: associate device structure.
139 * @hwmon_dev: device associated with hwmon subsystem.
140 * @reg: associated regulator (if specified).
141 * @nb: notifier block to handle notifications of voltage
142 * changes.
Vivien Didelot142c0902013-01-07 14:18:38 -0500143 * @supply_uv: local copy of supply voltage used to allow use of
Vivien Didelot99a03782011-04-12 15:34:36 -0400144 * regulator consumer if available.
Vivien Didelot142c0902013-01-07 14:18:38 -0500145 * @supply_uv_valid: indicates that an updated value has not yet been
Vivien Didelot99a03782011-04-12 15:34:36 -0400146 * obtained from the regulator and so any calculations
147 * based upon it will be invalid.
Vivien Didelot142c0902013-01-07 14:18:38 -0500148 * @update_supply_work: work struct that is used to update the supply_uv.
Vivien Didelot99a03782011-04-12 15:34:36 -0400149 * @interrupt_handled: flag used to indicate a handler has been scheduled.
Jonathan Cameron251eb402009-04-13 14:39:45 -0700150 */
151struct sht15_data {
152 struct sht15_platform_data *pdata;
153 struct work_struct read_work;
154 wait_queue_head_t wait_queue;
155 uint16_t val_temp;
156 uint16_t val_humid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400157 u8 val_status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400158 bool checksum_ok;
159 bool checksumming;
Vivien Didelot99a03782011-04-12 15:34:36 -0400160 enum sht15_state state;
161 bool measurements_valid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400162 bool status_valid;
Vivien Didelot99a03782011-04-12 15:34:36 -0400163 unsigned long last_measurement;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400164 unsigned long last_status;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700165 struct mutex read_lock;
166 struct device *dev;
167 struct device *hwmon_dev;
168 struct regulator *reg;
169 struct notifier_block nb;
Vivien Didelot142c0902013-01-07 14:18:38 -0500170 int supply_uv;
171 bool supply_uv_valid;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700172 struct work_struct update_supply_work;
173 atomic_t interrupt_handled;
174};
175
176/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400177 * sht15_crc8() - compute crc8
178 * @data: sht15 specific data.
179 * @value: sht15 retrieved data.
180 *
181 * This implements section 2 of the CRC datasheet.
182 */
183static u8 sht15_crc8(struct sht15_data *data,
184 const u8 *value,
185 int len)
186{
yalin wang33836ee2015-08-10 22:41:43 +0800187 u8 crc = bitrev8(data->val_status & 0x0F);
Jerome Oufella82c74652011-04-12 15:34:39 -0400188
189 while (len--) {
190 crc = sht15_crc8_table[*value ^ crc];
191 value++;
192 }
193
194 return crc;
195}
196
197/**
Jonathan Cameron251eb402009-04-13 14:39:45 -0700198 * sht15_connection_reset() - reset the comms interface
199 * @data: sht15 specific data
200 *
201 * This implements section 3.4 of the data sheet
202 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500203static int sht15_connection_reset(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700204{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500205 int i, err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400206
Vivien Didelot412e29c2013-01-15 13:33:06 -0500207 err = gpio_direction_output(data->pdata->gpio_data, 1);
208 if (err)
209 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700210 ndelay(SHT15_TSCKL);
211 gpio_set_value(data->pdata->gpio_sck, 0);
212 ndelay(SHT15_TSCKL);
213 for (i = 0; i < 9; ++i) {
214 gpio_set_value(data->pdata->gpio_sck, 1);
215 ndelay(SHT15_TSCKH);
216 gpio_set_value(data->pdata->gpio_sck, 0);
217 ndelay(SHT15_TSCKL);
218 }
Vivien Didelot412e29c2013-01-15 13:33:06 -0500219 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700220}
Vivien Didelot99a03782011-04-12 15:34:36 -0400221
Jonathan Cameron251eb402009-04-13 14:39:45 -0700222/**
223 * sht15_send_bit() - send an individual bit to the device
224 * @data: device state data
225 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400226 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700227static inline void sht15_send_bit(struct sht15_data *data, int val)
228{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700229 gpio_set_value(data->pdata->gpio_data, val);
230 ndelay(SHT15_TSU);
231 gpio_set_value(data->pdata->gpio_sck, 1);
232 ndelay(SHT15_TSCKH);
233 gpio_set_value(data->pdata->gpio_sck, 0);
234 ndelay(SHT15_TSCKL); /* clock low time */
235}
236
237/**
238 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700239 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400240 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700241 * Timings for this are not documented on the data sheet, so very
242 * conservative ones used in implementation. This implements
243 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400244 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500245static int sht15_transmission_start(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700246{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500247 int err;
248
Jonathan Cameron251eb402009-04-13 14:39:45 -0700249 /* ensure data is high and output */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500250 err = gpio_direction_output(data->pdata->gpio_data, 1);
251 if (err)
252 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700253 ndelay(SHT15_TSU);
254 gpio_set_value(data->pdata->gpio_sck, 0);
255 ndelay(SHT15_TSCKL);
256 gpio_set_value(data->pdata->gpio_sck, 1);
257 ndelay(SHT15_TSCKH);
258 gpio_set_value(data->pdata->gpio_data, 0);
259 ndelay(SHT15_TSU);
260 gpio_set_value(data->pdata->gpio_sck, 0);
261 ndelay(SHT15_TSCKL);
262 gpio_set_value(data->pdata->gpio_sck, 1);
263 ndelay(SHT15_TSCKH);
264 gpio_set_value(data->pdata->gpio_data, 1);
265 ndelay(SHT15_TSU);
266 gpio_set_value(data->pdata->gpio_sck, 0);
267 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500268 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700269}
Vivien Didelot99a03782011-04-12 15:34:36 -0400270
Jonathan Cameron251eb402009-04-13 14:39:45 -0700271/**
272 * sht15_send_byte() - send a single byte to the device
273 * @data: device state
274 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400275 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700276static void sht15_send_byte(struct sht15_data *data, u8 byte)
277{
278 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400279
Jonathan Cameron251eb402009-04-13 14:39:45 -0700280 for (i = 0; i < 8; i++) {
281 sht15_send_bit(data, !!(byte & 0x80));
282 byte <<= 1;
283 }
284}
Vivien Didelot99a03782011-04-12 15:34:36 -0400285
Jonathan Cameron251eb402009-04-13 14:39:45 -0700286/**
287 * sht15_wait_for_response() - checks for ack from device
288 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400289 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700290static int sht15_wait_for_response(struct sht15_data *data)
291{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500292 int err;
293
294 err = gpio_direction_input(data->pdata->gpio_data);
295 if (err)
296 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700297 gpio_set_value(data->pdata->gpio_sck, 1);
298 ndelay(SHT15_TSCKH);
299 if (gpio_get_value(data->pdata->gpio_data)) {
300 gpio_set_value(data->pdata->gpio_sck, 0);
301 dev_err(data->dev, "Command not acknowledged\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500302 err = sht15_connection_reset(data);
303 if (err)
304 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700305 return -EIO;
306 }
307 gpio_set_value(data->pdata->gpio_sck, 0);
308 ndelay(SHT15_TSCKL);
309 return 0;
310}
311
312/**
313 * sht15_send_cmd() - Sends a command to the device.
314 * @data: device state
315 * @cmd: command byte to be sent
316 *
317 * On entry, sck is output low, data is output pull high
318 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400319 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700320static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
321{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500322 int err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400323
Vivien Didelot412e29c2013-01-15 13:33:06 -0500324 err = sht15_transmission_start(data);
325 if (err)
326 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700327 sht15_send_byte(data, cmd);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500328 return sht15_wait_for_response(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700329}
Vivien Didelot99a03782011-04-12 15:34:36 -0400330
Jonathan Cameron251eb402009-04-13 14:39:45 -0700331/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400332 * sht15_soft_reset() - send a soft reset command
333 * @data: sht15 specific data.
334 *
335 * As described in section 3.2 of the datasheet.
336 */
337static int sht15_soft_reset(struct sht15_data *data)
338{
339 int ret;
340
341 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
342 if (ret)
343 return ret;
344 msleep(SHT15_TSRST);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400345 /* device resets default hardware status register value */
346 data->val_status = 0;
Vivien Didelot181148a2011-04-12 15:34:37 -0400347
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400348 return ret;
349}
350
351/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400352 * sht15_ack() - send a ack
353 * @data: sht15 specific data.
354 *
355 * Each byte of data is acknowledged by pulling the data line
356 * low for one clock pulse.
357 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500358static int sht15_ack(struct sht15_data *data)
Jerome Oufella82c74652011-04-12 15:34:39 -0400359{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500360 int err;
361
362 err = gpio_direction_output(data->pdata->gpio_data, 0);
363 if (err)
364 return err;
Jerome Oufella82c74652011-04-12 15:34:39 -0400365 ndelay(SHT15_TSU);
366 gpio_set_value(data->pdata->gpio_sck, 1);
367 ndelay(SHT15_TSU);
368 gpio_set_value(data->pdata->gpio_sck, 0);
369 ndelay(SHT15_TSU);
370 gpio_set_value(data->pdata->gpio_data, 1);
371
Vivien Didelot412e29c2013-01-15 13:33:06 -0500372 return gpio_direction_input(data->pdata->gpio_data);
Jerome Oufella82c74652011-04-12 15:34:39 -0400373}
374
375/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400376 * sht15_end_transmission() - notify device of end of transmission
377 * @data: device state.
378 *
379 * This is basically a NAK (single clock pulse, data high).
380 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500381static int sht15_end_transmission(struct sht15_data *data)
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400382{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500383 int err;
384
385 err = gpio_direction_output(data->pdata->gpio_data, 1);
386 if (err)
387 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400388 ndelay(SHT15_TSU);
389 gpio_set_value(data->pdata->gpio_sck, 1);
390 ndelay(SHT15_TSCKH);
391 gpio_set_value(data->pdata->gpio_sck, 0);
392 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500393 return 0;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400394}
395
396/**
397 * sht15_read_byte() - Read a byte back from the device
398 * @data: device state.
399 */
400static u8 sht15_read_byte(struct sht15_data *data)
401{
402 int i;
403 u8 byte = 0;
404
405 for (i = 0; i < 8; ++i) {
406 byte <<= 1;
407 gpio_set_value(data->pdata->gpio_sck, 1);
408 ndelay(SHT15_TSCKH);
409 byte |= !!gpio_get_value(data->pdata->gpio_data);
410 gpio_set_value(data->pdata->gpio_sck, 0);
411 ndelay(SHT15_TSCKL);
412 }
413 return byte;
414}
415
416/**
417 * sht15_send_status() - write the status register byte
418 * @data: sht15 specific data.
419 * @status: the byte to set the status register with.
420 *
421 * As described in figure 14 and table 5 of the datasheet.
422 */
423static int sht15_send_status(struct sht15_data *data, u8 status)
424{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500425 int err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400426
Vivien Didelot412e29c2013-01-15 13:33:06 -0500427 err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
428 if (err)
429 return err;
430 err = gpio_direction_output(data->pdata->gpio_data, 1);
431 if (err)
432 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400433 ndelay(SHT15_TSU);
434 sht15_send_byte(data, status);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500435 err = sht15_wait_for_response(data);
436 if (err)
437 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400438
439 data->val_status = status;
Vivien Didelot181148a2011-04-12 15:34:37 -0400440 return 0;
441}
442
443/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400444 * sht15_update_status() - get updated status register from device if too old
445 * @data: device instance specific data.
446 *
447 * As described in figure 15 and table 5 of the datasheet.
448 */
449static int sht15_update_status(struct sht15_data *data)
450{
451 int ret = 0;
452 u8 status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400453 u8 previous_config;
454 u8 dev_checksum = 0;
455 u8 checksum_vals[2];
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400456 int timeout = HZ;
457
458 mutex_lock(&data->read_lock);
459 if (time_after(jiffies, data->last_status + timeout)
460 || !data->status_valid) {
461 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
462 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500463 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400464 status = sht15_read_byte(data);
465
Jerome Oufella82c74652011-04-12 15:34:39 -0400466 if (data->checksumming) {
467 sht15_ack(data);
yalin wang33836ee2015-08-10 22:41:43 +0800468 dev_checksum = bitrev8(sht15_read_byte(data));
Jerome Oufella82c74652011-04-12 15:34:39 -0400469 checksum_vals[0] = SHT15_READ_STATUS;
470 checksum_vals[1] = status;
471 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
472 == dev_checksum);
473 }
474
Vivien Didelot412e29c2013-01-15 13:33:06 -0500475 ret = sht15_end_transmission(data);
476 if (ret)
477 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400478
Jerome Oufella82c74652011-04-12 15:34:39 -0400479 /*
480 * Perform checksum validation on the received data.
481 * Specification mentions that in case a checksum verification
482 * fails, a soft reset command must be sent to the device.
483 */
484 if (data->checksumming && !data->checksum_ok) {
485 previous_config = data->val_status & 0x07;
486 ret = sht15_soft_reset(data);
487 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500488 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400489 if (previous_config) {
490 ret = sht15_send_status(data, previous_config);
491 if (ret) {
492 dev_err(data->dev,
493 "CRC validation failed, unable "
494 "to restore device settings\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500495 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400496 }
497 }
498 ret = -EAGAIN;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500499 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400500 }
501
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400502 data->val_status = status;
503 data->status_valid = true;
504 data->last_status = jiffies;
505 }
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400506
Vivien Didelot412e29c2013-01-15 13:33:06 -0500507unlock:
508 mutex_unlock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400509 return ret;
510}
511
512/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400513 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700514 * @data: device instance specific data
515 * @command: command sent to request value
516 * @timeout_msecs: timeout after which comms are assumed
517 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400518 */
519static int sht15_measurement(struct sht15_data *data,
520 int command,
521 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700522{
523 int ret;
Jerome Oufella82c74652011-04-12 15:34:39 -0400524 u8 previous_config;
Vivien Didelot99a03782011-04-12 15:34:36 -0400525
Jonathan Cameron251eb402009-04-13 14:39:45 -0700526 ret = sht15_send_cmd(data, command);
527 if (ret)
528 return ret;
529
Vivien Didelot412e29c2013-01-15 13:33:06 -0500530 ret = gpio_direction_input(data->pdata->gpio_data);
531 if (ret)
532 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700533 atomic_set(&data->interrupt_handled, 0);
534
535 enable_irq(gpio_to_irq(data->pdata->gpio_data));
536 if (gpio_get_value(data->pdata->gpio_data) == 0) {
537 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300538 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700539 if (!atomic_read(&data->interrupt_handled))
540 schedule_work(&data->read_work);
541 }
542 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400543 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700544 msecs_to_jiffies(timeout_msecs));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500545 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
546 data->state = SHT15_READING_NOTHING;
547 return -EIO;
548 } else if (ret == 0) { /* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200549 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500550 ret = sht15_connection_reset(data);
551 if (ret)
552 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700553 return -ETIME;
554 }
Jerome Oufella82c74652011-04-12 15:34:39 -0400555
556 /*
557 * Perform checksum validation on the received data.
558 * Specification mentions that in case a checksum verification fails,
559 * a soft reset command must be sent to the device.
560 */
561 if (data->checksumming && !data->checksum_ok) {
562 previous_config = data->val_status & 0x07;
563 ret = sht15_soft_reset(data);
564 if (ret)
565 return ret;
566 if (previous_config) {
567 ret = sht15_send_status(data, previous_config);
568 if (ret) {
569 dev_err(data->dev,
570 "CRC validation failed, unable "
571 "to restore device settings\n");
572 return ret;
573 }
574 }
575 return -EAGAIN;
576 }
577
Jonathan Cameron251eb402009-04-13 14:39:45 -0700578 return 0;
579}
580
581/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400582 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700583 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400584 */
585static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700586{
587 int ret = 0;
588 int timeout = HZ;
589
590 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400591 if (time_after(jiffies, data->last_measurement + timeout)
592 || !data->measurements_valid) {
593 data->state = SHT15_READING_HUMID;
594 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700595 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500596 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400597 data->state = SHT15_READING_TEMP;
598 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700599 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500600 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400601 data->measurements_valid = true;
602 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700603 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700604
Vivien Didelot412e29c2013-01-15 13:33:06 -0500605unlock:
606 mutex_unlock(&data->read_lock);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700607 return ret;
608}
609
610/**
611 * sht15_calc_temp() - convert the raw reading to a temperature
612 * @data: device state
613 *
614 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400615 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700616static inline int sht15_calc_temp(struct sht15_data *data)
617{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200618 int d1 = temppoints[0].d1;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400619 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700620 int i;
621
Jerome Oufella328a2c22010-04-14 16:14:07 +0200622 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700623 /* Find pointer to interpolate */
Vivien Didelot142c0902013-01-07 14:18:38 -0500624 if (data->supply_uv > temppoints[i - 1].vdd) {
625 d1 = (data->supply_uv - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700626 * (temppoints[i].d1 - temppoints[i - 1].d1)
627 / (temppoints[i].vdd - temppoints[i - 1].vdd)
628 + temppoints[i - 1].d1;
629 break;
630 }
631
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400632 return data->val_temp * d2 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700633}
634
635/**
636 * sht15_calc_humid() - using last temperature convert raw to humid
637 * @data: device state
638 *
639 * This is the temperature compensated version as per section 4.2 of
640 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400641 *
642 * The sensor is assumed to be V3, which is compatible with V4.
643 * Humidity conversion coefficients are shown in table 7 of the datasheet.
644 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700645static inline int sht15_calc_humid(struct sht15_data *data)
646{
Vivien Didelot99a03782011-04-12 15:34:36 -0400647 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700648 int temp = sht15_calc_temp(data);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400649 int c2, c3;
650 int t2;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700651 const int c1 = -4;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400652
653 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
654 c2 = 648000; /* x 10 ^ -6 */
655 c3 = -7200; /* x 10 ^ -7 */
656 t2 = 1280;
657 } else {
658 c2 = 40500; /* x 10 ^ -6 */
659 c3 = -28; /* x 10 ^ -7 */
660 t2 = 80;
661 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700662
Vivien Didelot99a03782011-04-12 15:34:36 -0400663 rh_linear = c1 * 1000
664 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100665 + (data->val_humid * data->val_humid * c3) / 10000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400666 return (temp - 25000) * (10000 + t2 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400667 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700668}
669
Vivien Didelot99a03782011-04-12 15:34:36 -0400670/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400671 * sht15_show_status() - show status information in sysfs
672 * @dev: device.
673 * @attr: device attribute.
674 * @buf: sysfs buffer where information is written to.
675 *
676 * Will be called on read access to temp1_fault, humidity1_fault
677 * and heater_enable sysfs attributes.
678 * Returns number of bytes written into buffer, negative errno on error.
679 */
680static ssize_t sht15_show_status(struct device *dev,
681 struct device_attribute *attr,
682 char *buf)
683{
684 int ret;
685 struct sht15_data *data = dev_get_drvdata(dev);
686 u8 bit = to_sensor_dev_attr(attr)->index;
687
688 ret = sht15_update_status(data);
689
690 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
691}
692
693/**
694 * sht15_store_heater() - change heater state via sysfs
695 * @dev: device.
696 * @attr: device attribute.
697 * @buf: sysfs buffer to read the new heater state from.
698 * @count: length of the data.
699 *
Vivien Didelote9b6e9f2011-07-25 21:46:10 +0200700 * Will be called on write access to heater_enable sysfs attribute.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400701 * Returns number of bytes actually decoded, negative errno on error.
702 */
703static ssize_t sht15_store_heater(struct device *dev,
704 struct device_attribute *attr,
705 const char *buf, size_t count)
706{
707 int ret;
708 struct sht15_data *data = dev_get_drvdata(dev);
709 long value;
710 u8 status;
711
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100712 if (kstrtol(buf, 10, &value))
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400713 return -EINVAL;
714
715 mutex_lock(&data->read_lock);
716 status = data->val_status & 0x07;
717 if (!!value)
718 status |= SHT15_STATUS_HEATER;
719 else
720 status &= ~SHT15_STATUS_HEATER;
721
722 ret = sht15_send_status(data, status);
723 mutex_unlock(&data->read_lock);
724
725 return ret ? ret : count;
726}
727
728/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400729 * sht15_show_temp() - show temperature measurement value in sysfs
730 * @dev: device.
731 * @attr: device attribute.
732 * @buf: sysfs buffer where measurement values are written to.
733 *
734 * Will be called on read access to temp1_input sysfs attribute.
735 * Returns number of bytes written into buffer, negative errno on error.
736 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700737static ssize_t sht15_show_temp(struct device *dev,
738 struct device_attribute *attr,
739 char *buf)
740{
741 int ret;
742 struct sht15_data *data = dev_get_drvdata(dev);
743
744 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400745 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700746
747 return ret ? ret : sprintf(buf, "%d\n",
748 sht15_calc_temp(data));
749}
750
Vivien Didelot99a03782011-04-12 15:34:36 -0400751/**
752 * sht15_show_humidity() - show humidity measurement value in sysfs
753 * @dev: device.
754 * @attr: device attribute.
755 * @buf: sysfs buffer where measurement values are written to.
756 *
757 * Will be called on read access to humidity1_input sysfs attribute.
758 * Returns number of bytes written into buffer, negative errno on error.
759 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700760static ssize_t sht15_show_humidity(struct device *dev,
761 struct device_attribute *attr,
762 char *buf)
763{
764 int ret;
765 struct sht15_data *data = dev_get_drvdata(dev);
766
Vivien Didelot99a03782011-04-12 15:34:36 -0400767 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700768
769 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
Vivien Didelot99a03782011-04-12 15:34:36 -0400770}
771
Jonathan Cameron251eb402009-04-13 14:39:45 -0700772static ssize_t show_name(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
775{
776 struct platform_device *pdev = to_platform_device(dev);
777 return sprintf(buf, "%s\n", pdev->name);
778}
779
Vivien Didelot99a03782011-04-12 15:34:36 -0400780static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
781 sht15_show_temp, NULL, 0);
782static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
783 sht15_show_humidity, NULL, 0);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400784static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
785 SHT15_STATUS_LOW_BATTERY);
786static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
787 SHT15_STATUS_LOW_BATTERY);
788static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
789 sht15_store_heater, SHT15_STATUS_HEATER);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700790static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
791static struct attribute *sht15_attrs[] = {
792 &sensor_dev_attr_temp1_input.dev_attr.attr,
793 &sensor_dev_attr_humidity1_input.dev_attr.attr,
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400794 &sensor_dev_attr_temp1_fault.dev_attr.attr,
795 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
796 &sensor_dev_attr_heater_enable.dev_attr.attr,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700797 &dev_attr_name.attr,
798 NULL,
799};
800
801static const struct attribute_group sht15_attr_group = {
802 .attrs = sht15_attrs,
803};
804
805static irqreturn_t sht15_interrupt_fired(int irq, void *d)
806{
807 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400808
Jonathan Cameron251eb402009-04-13 14:39:45 -0700809 /* First disable the interrupt */
810 disable_irq_nosync(irq);
811 atomic_inc(&data->interrupt_handled);
812 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400813 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700814 schedule_work(&data->read_work);
815 return IRQ_HANDLED;
816}
817
Jonathan Cameron251eb402009-04-13 14:39:45 -0700818static void sht15_bh_read_data(struct work_struct *work_s)
819{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700820 uint16_t val = 0;
Jerome Oufella82c74652011-04-12 15:34:39 -0400821 u8 dev_checksum = 0;
822 u8 checksum_vals[3];
Jonathan Cameron251eb402009-04-13 14:39:45 -0700823 struct sht15_data *data
824 = container_of(work_s, struct sht15_data,
825 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400826
Jonathan Cameron251eb402009-04-13 14:39:45 -0700827 /* Firstly, verify the line is low */
828 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400829 /*
830 * If not, then start the interrupt again - care here as could
831 * have gone low in meantime so verify it hasn't!
832 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700833 atomic_set(&data->interrupt_handled, 0);
834 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Frans Meulenbroeksc9e14982012-01-08 19:34:06 +0100835 /* If still not occurred or another handler was scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700836 if (gpio_get_value(data->pdata->gpio_data)
837 || atomic_read(&data->interrupt_handled))
838 return;
839 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400840
Jonathan Cameron251eb402009-04-13 14:39:45 -0700841 /* Read the data back from the device */
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400842 val = sht15_read_byte(data);
843 val <<= 8;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500844 if (sht15_ack(data))
845 goto wakeup;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400846 val |= sht15_read_byte(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400847
Jerome Oufella82c74652011-04-12 15:34:39 -0400848 if (data->checksumming) {
849 /*
850 * Ask the device for a checksum and read it back.
851 * Note: the device sends the checksum byte reversed.
852 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500853 if (sht15_ack(data))
854 goto wakeup;
yalin wang33836ee2015-08-10 22:41:43 +0800855 dev_checksum = bitrev8(sht15_read_byte(data));
Jerome Oufella82c74652011-04-12 15:34:39 -0400856 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
857 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
858 checksum_vals[1] = (u8) (val >> 8);
859 checksum_vals[2] = (u8) val;
860 data->checksum_ok
861 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
862 }
863
Jonathan Cameron251eb402009-04-13 14:39:45 -0700864 /* Tell the device we are done */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500865 if (sht15_end_transmission(data))
866 goto wakeup;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700867
Vivien Didelot99a03782011-04-12 15:34:36 -0400868 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700869 case SHT15_READING_TEMP:
870 data->val_temp = val;
871 break;
872 case SHT15_READING_HUMID:
873 data->val_humid = val;
874 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400875 default:
876 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700877 }
878
Vivien Didelot99a03782011-04-12 15:34:36 -0400879 data->state = SHT15_READING_NOTHING;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500880wakeup:
Jonathan Cameron251eb402009-04-13 14:39:45 -0700881 wake_up(&data->wait_queue);
882}
883
884static void sht15_update_voltage(struct work_struct *work_s)
885{
886 struct sht15_data *data
887 = container_of(work_s, struct sht15_data,
888 update_supply_work);
Vivien Didelot142c0902013-01-07 14:18:38 -0500889 data->supply_uv = regulator_get_voltage(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700890}
891
892/**
893 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
894 * @nb: associated notification structure
895 * @event: voltage regulator state change event code
896 * @ignored: function parameter - ignored here
897 *
898 * Note that as the notification code holds the regulator lock, we have
899 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400900 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700901static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400902 unsigned long event,
903 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700904{
905 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
906
907 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
Vivien Didelot142c0902013-01-07 14:18:38 -0500908 data->supply_uv_valid = false;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700909 schedule_work(&data->update_supply_work);
910
911 return NOTIFY_OK;
912}
913
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500914static int sht15_probe(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700915{
Vivien Didelot6edf3c32012-01-26 15:59:00 -0500916 int ret;
Guenter Roeck38fe7562012-06-02 11:20:22 -0700917 struct sht15_data *data;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400918 u8 status = 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700919
Guenter Roeck38fe7562012-06-02 11:20:22 -0700920 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
921 if (!data)
922 return -ENOMEM;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700923
924 INIT_WORK(&data->read_work, sht15_bh_read_data);
925 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
926 platform_set_drvdata(pdev, data);
927 mutex_init(&data->read_lock);
928 data->dev = &pdev->dev;
929 init_waitqueue_head(&data->wait_queue);
930
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900931 if (dev_get_platdata(&pdev->dev) == NULL) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400932 dev_err(&pdev->dev, "no platform data supplied\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700933 return -EINVAL;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700934 }
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900935 data->pdata = dev_get_platdata(&pdev->dev);
Vivien Didelot142c0902013-01-07 14:18:38 -0500936 data->supply_uv = data->pdata->supply_mv * 1000;
Jerome Oufella82c74652011-04-12 15:34:39 -0400937 if (data->pdata->checksum)
938 data->checksumming = true;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400939 if (data->pdata->no_otp_reload)
940 status |= SHT15_STATUS_NO_OTP_RELOAD;
941 if (data->pdata->low_resolution)
942 status |= SHT15_STATUS_LOW_RESOLUTION;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700943
Vivien Didelot99a03782011-04-12 15:34:36 -0400944 /*
945 * If a regulator is available,
946 * query what the supply voltage actually is!
947 */
Mark Brown9e059ba2013-08-18 17:35:52 +0100948 data->reg = devm_regulator_get_optional(data->dev, "vcc");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700949 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +0200950 int voltage;
951
952 voltage = regulator_get_voltage(data->reg);
953 if (voltage)
Vivien Didelot142c0902013-01-07 14:18:38 -0500954 data->supply_uv = voltage;
Jean Delvarec7a78d22010-04-14 16:14:08 +0200955
Mark Brown3e780802013-03-02 15:33:30 +0800956 ret = regulator_enable(data->reg);
957 if (ret != 0) {
958 dev_err(&pdev->dev,
959 "failed to enable regulator: %d\n", ret);
960 return ret;
961 }
962
Vivien Didelot99a03782011-04-12 15:34:36 -0400963 /*
964 * Setup a notifier block to update this if another device
965 * causes the voltage to change
966 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700967 data->nb.notifier_call = &sht15_invalidate_voltage;
968 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -0400969 if (ret) {
970 dev_err(&pdev->dev,
971 "regulator notifier request failed\n");
972 regulator_disable(data->reg);
Guenter Roeck38fe7562012-06-02 11:20:22 -0700973 return ret;
Vivien Didelot181148a2011-04-12 15:34:37 -0400974 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700975 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400976
977 /* Try requesting the GPIOs */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500978 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
979 GPIOF_OUT_INIT_LOW, "SHT15 sck");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700980 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -0500981 dev_err(&pdev->dev, "clock line GPIO request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -0400982 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700983 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400984
Guenter Roeck38fe7562012-06-02 11:20:22 -0700985 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
986 "SHT15 data");
Jonathan Cameron251eb402009-04-13 14:39:45 -0700987 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -0500988 dev_err(&pdev->dev, "data line GPIO request failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700989 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700990 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700991
Guenter Roeck38fe7562012-06-02 11:20:22 -0700992 ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
993 sht15_interrupt_fired,
994 IRQF_TRIGGER_FALLING,
995 "sht15 data",
996 data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700997 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400998 dev_err(&pdev->dev, "failed to get irq for data line\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -0700999 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001000 }
1001 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -05001002 ret = sht15_connection_reset(data);
1003 if (ret)
1004 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001005 ret = sht15_soft_reset(data);
1006 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001007 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001008
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001009 /* write status with platform data options */
1010 if (status) {
1011 ret = sht15_send_status(data, status);
1012 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001013 goto err_release_reg;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001014 }
1015
Vivien Didelot181148a2011-04-12 15:34:37 -04001016 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1017 if (ret) {
1018 dev_err(&pdev->dev, "sysfs create failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001019 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001020 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001021
1022 data->hwmon_dev = hwmon_device_register(data->dev);
1023 if (IS_ERR(data->hwmon_dev)) {
1024 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -04001025 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001026 }
Vivien Didelot99a03782011-04-12 15:34:36 -04001027
Jonathan Cameron251eb402009-04-13 14:39:45 -07001028 return 0;
1029
Vivien Didelot181148a2011-04-12 15:34:37 -04001030err_release_sysfs_group:
1031 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Vivien Didelot181148a2011-04-12 15:34:37 -04001032err_release_reg:
1033 if (!IS_ERR(data->reg)) {
1034 regulator_unregister_notifier(data->reg, &data->nb);
1035 regulator_disable(data->reg);
Vivien Didelot181148a2011-04-12 15:34:37 -04001036 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001037 return ret;
1038}
1039
Bill Pemberton281dfd02012-11-19 13:25:51 -05001040static int sht15_remove(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -07001041{
1042 struct sht15_data *data = platform_get_drvdata(pdev);
1043
Vivien Didelot99a03782011-04-12 15:34:36 -04001044 /*
1045 * Make sure any reads from the device are done and
1046 * prevent new ones beginning
1047 */
Jonathan Cameron251eb402009-04-13 14:39:45 -07001048 mutex_lock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001049 if (sht15_soft_reset(data)) {
1050 mutex_unlock(&data->read_lock);
1051 return -EFAULT;
1052 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001053 hwmon_device_unregister(data->hwmon_dev);
1054 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1055 if (!IS_ERR(data->reg)) {
1056 regulator_unregister_notifier(data->reg, &data->nb);
1057 regulator_disable(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001058 }
1059
Jonathan Cameron251eb402009-04-13 14:39:45 -07001060 mutex_unlock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -04001061
Jonathan Cameron251eb402009-04-13 14:39:45 -07001062 return 0;
1063}
1064
Krzysztof Kozlowski9c407232015-05-02 00:56:23 +09001065static const struct platform_device_id sht15_device_ids[] = {
Vivien Didelotedec5af2012-08-30 21:46:19 -04001066 { "sht10", sht10 },
1067 { "sht11", sht11 },
1068 { "sht15", sht15 },
1069 { "sht71", sht71 },
1070 { "sht75", sht75 },
1071 { }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001072};
Vivien Didelotedec5af2012-08-30 21:46:19 -04001073MODULE_DEVICE_TABLE(platform, sht15_device_ids);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001074
Vivien Didelotedec5af2012-08-30 21:46:19 -04001075static struct platform_driver sht15_driver = {
1076 .driver = {
1077 .name = "sht15",
Vivien Didelotedec5af2012-08-30 21:46:19 -04001078 },
1079 .probe = sht15_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001080 .remove = sht15_remove,
Vivien Didelotedec5af2012-08-30 21:46:19 -04001081 .id_table = sht15_device_ids,
1082};
1083module_platform_driver(sht15_driver);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001084
1085MODULE_LICENSE("GPL");
Vivien Didelotedec5af2012-08-30 21:46:19 -04001086MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");