blob: e4d642b673c6d9e5dfebb7bc23360f4d8f46fb3e [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>
Marco Franchi2f1736f2017-02-16 10:23:43 -020037#include <linux/of_gpio.h>
Jonathan Cameron251eb402009-04-13 14:39:45 -070038
Vivien Didelot99a03782011-04-12 15:34:36 -040039/* Commands */
40#define SHT15_MEASURE_TEMP 0x03
41#define SHT15_MEASURE_RH 0x05
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040042#define SHT15_WRITE_STATUS 0x06
43#define SHT15_READ_STATUS 0x07
Vivien Didelot181148a2011-04-12 15:34:37 -040044#define SHT15_SOFT_RESET 0x1E
Jonathan Cameron251eb402009-04-13 14:39:45 -070045
Vivien Didelot99a03782011-04-12 15:34:36 -040046/* Min timings */
47#define SHT15_TSCKL 100 /* (nsecs) clock low */
48#define SHT15_TSCKH 100 /* (nsecs) clock high */
49#define SHT15_TSU 150 /* (nsecs) data setup time */
Vivien Didelot181148a2011-04-12 15:34:37 -040050#define SHT15_TSRST 11 /* (msecs) soft reset time */
Jonathan Cameron251eb402009-04-13 14:39:45 -070051
Vivien Didelotcc15c7e2011-04-12 15:34:38 -040052/* Status Register Bits */
53#define SHT15_STATUS_LOW_RESOLUTION 0x01
54#define SHT15_STATUS_NO_OTP_RELOAD 0x02
55#define SHT15_STATUS_HEATER 0x04
56#define SHT15_STATUS_LOW_BATTERY 0x40
57
Vivien Didelotedec5af2012-08-30 21:46:19 -040058/* List of supported chips */
59enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
60
Vivien Didelot99a03782011-04-12 15:34:36 -040061/* Actions the driver may be doing */
62enum sht15_state {
63 SHT15_READING_NOTHING,
64 SHT15_READING_TEMP,
65 SHT15_READING_HUMID
66};
Jonathan Cameron251eb402009-04-13 14:39:45 -070067
68/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030069 * struct sht15_temppair - elements of voltage dependent temp calc
Jonathan Cameron251eb402009-04-13 14:39:45 -070070 * @vdd: supply voltage in microvolts
71 * @d1: see data sheet
72 */
73struct sht15_temppair {
74 int vdd; /* microvolts */
75 int d1;
76};
77
Vivien Didelot99a03782011-04-12 15:34:36 -040078/* Table 9 from datasheet - relates temperature calculation to supply voltage */
Jonathan Cameron251eb402009-04-13 14:39:45 -070079static const struct sht15_temppair temppoints[] = {
80 { 2500000, -39400 },
81 { 3000000, -39600 },
82 { 3500000, -39700 },
83 { 4000000, -39800 },
84 { 5000000, -40100 },
85};
86
Jerome Oufella82c74652011-04-12 15:34:39 -040087/* Table from CRC datasheet, section 2.4 */
88static const u8 sht15_crc8_table[] = {
89 0, 49, 98, 83, 196, 245, 166, 151,
90 185, 136, 219, 234, 125, 76, 31, 46,
91 67, 114, 33, 16, 135, 182, 229, 212,
92 250, 203, 152, 169, 62, 15, 92, 109,
93 134, 183, 228, 213, 66, 115, 32, 17,
94 63, 14, 93, 108, 251, 202, 153, 168,
95 197, 244, 167, 150, 1, 48, 99, 82,
96 124, 77, 30, 47, 184, 137, 218, 235,
97 61, 12, 95, 110, 249, 200, 155, 170,
98 132, 181, 230, 215, 64, 113, 34, 19,
99 126, 79, 28, 45, 186, 139, 216, 233,
100 199, 246, 165, 148, 3, 50, 97, 80,
101 187, 138, 217, 232, 127, 78, 29, 44,
102 2, 51, 96, 81, 198, 247, 164, 149,
103 248, 201, 154, 171, 60, 13, 94, 111,
104 65, 112, 35, 18, 133, 180, 231, 214,
105 122, 75, 24, 41, 190, 143, 220, 237,
106 195, 242, 161, 144, 7, 54, 101, 84,
107 57, 8, 91, 106, 253, 204, 159, 174,
108 128, 177, 226, 211, 68, 117, 38, 23,
109 252, 205, 158, 175, 56, 9, 90, 107,
110 69, 116, 39, 22, 129, 176, 227, 210,
111 191, 142, 221, 236, 123, 74, 25, 40,
112 6, 55, 100, 85, 194, 243, 160, 145,
113 71, 118, 37, 20, 131, 178, 225, 208,
114 254, 207, 156, 173, 58, 11, 88, 105,
115 4, 53, 102, 87, 192, 241, 162, 147,
116 189, 140, 223, 238, 121, 72, 27, 42,
117 193, 240, 163, 146, 5, 52, 103, 86,
118 120, 73, 26, 43, 188, 141, 222, 239,
119 130, 179, 224, 209, 70, 119, 36, 21,
120 59, 10, 89, 104, 255, 206, 157, 172
121};
122
Jonathan Cameron251eb402009-04-13 14:39:45 -0700123/**
124 * struct sht15_data - device instance specific data
Vivien Didelot99a03782011-04-12 15:34:36 -0400125 * @pdata: platform data (gpio's etc).
126 * @read_work: bh of interrupt handler.
127 * @wait_queue: wait queue for getting values from device.
128 * @val_temp: last temperature value read from device.
129 * @val_humid: last humidity value read from device.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400130 * @val_status: last status register value read from device.
Jerome Oufella82c74652011-04-12 15:34:39 -0400131 * @checksum_ok: last value read from the device passed CRC validation.
132 * @checksumming: flag used to enable the data validation with CRC.
Vivien Didelot99a03782011-04-12 15:34:36 -0400133 * @state: state identifying the action the driver is doing.
134 * @measurements_valid: are the current stored measures valid (start condition).
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400135 * @status_valid: is the current stored status valid (start condition).
Vivien Didelot99a03782011-04-12 15:34:36 -0400136 * @last_measurement: time of last measure.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400137 * @last_status: time of last status reading.
Vivien Didelot99a03782011-04-12 15:34:36 -0400138 * @read_lock: mutex to ensure only one read in progress at a time.
139 * @dev: associate device structure.
140 * @hwmon_dev: device associated with hwmon subsystem.
141 * @reg: associated regulator (if specified).
142 * @nb: notifier block to handle notifications of voltage
143 * changes.
Vivien Didelot142c0902013-01-07 14:18:38 -0500144 * @supply_uv: local copy of supply voltage used to allow use of
Vivien Didelot99a03782011-04-12 15:34:36 -0400145 * regulator consumer if available.
Vivien Didelot142c0902013-01-07 14:18:38 -0500146 * @supply_uv_valid: indicates that an updated value has not yet been
Vivien Didelot99a03782011-04-12 15:34:36 -0400147 * obtained from the regulator and so any calculations
148 * based upon it will be invalid.
Vivien Didelot142c0902013-01-07 14:18:38 -0500149 * @update_supply_work: work struct that is used to update the supply_uv.
Vivien Didelot99a03782011-04-12 15:34:36 -0400150 * @interrupt_handled: flag used to indicate a handler has been scheduled.
Jonathan Cameron251eb402009-04-13 14:39:45 -0700151 */
152struct sht15_data {
153 struct sht15_platform_data *pdata;
154 struct work_struct read_work;
155 wait_queue_head_t wait_queue;
156 uint16_t val_temp;
157 uint16_t val_humid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400158 u8 val_status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400159 bool checksum_ok;
160 bool checksumming;
Vivien Didelot99a03782011-04-12 15:34:36 -0400161 enum sht15_state state;
162 bool measurements_valid;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400163 bool status_valid;
Vivien Didelot99a03782011-04-12 15:34:36 -0400164 unsigned long last_measurement;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400165 unsigned long last_status;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700166 struct mutex read_lock;
167 struct device *dev;
168 struct device *hwmon_dev;
169 struct regulator *reg;
170 struct notifier_block nb;
Vivien Didelot142c0902013-01-07 14:18:38 -0500171 int supply_uv;
172 bool supply_uv_valid;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700173 struct work_struct update_supply_work;
174 atomic_t interrupt_handled;
175};
176
177/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400178 * sht15_crc8() - compute crc8
179 * @data: sht15 specific data.
180 * @value: sht15 retrieved data.
181 *
182 * This implements section 2 of the CRC datasheet.
183 */
184static u8 sht15_crc8(struct sht15_data *data,
185 const u8 *value,
186 int len)
187{
yalin wang33836ee2015-08-10 22:41:43 +0800188 u8 crc = bitrev8(data->val_status & 0x0F);
Jerome Oufella82c74652011-04-12 15:34:39 -0400189
190 while (len--) {
191 crc = sht15_crc8_table[*value ^ crc];
192 value++;
193 }
194
195 return crc;
196}
197
198/**
Jonathan Cameron251eb402009-04-13 14:39:45 -0700199 * sht15_connection_reset() - reset the comms interface
200 * @data: sht15 specific data
201 *
202 * This implements section 3.4 of the data sheet
203 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500204static int sht15_connection_reset(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700205{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500206 int i, err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400207
Vivien Didelot412e29c2013-01-15 13:33:06 -0500208 err = gpio_direction_output(data->pdata->gpio_data, 1);
209 if (err)
210 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700211 ndelay(SHT15_TSCKL);
212 gpio_set_value(data->pdata->gpio_sck, 0);
213 ndelay(SHT15_TSCKL);
214 for (i = 0; i < 9; ++i) {
215 gpio_set_value(data->pdata->gpio_sck, 1);
216 ndelay(SHT15_TSCKH);
217 gpio_set_value(data->pdata->gpio_sck, 0);
218 ndelay(SHT15_TSCKL);
219 }
Vivien Didelot412e29c2013-01-15 13:33:06 -0500220 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700221}
Vivien Didelot99a03782011-04-12 15:34:36 -0400222
Jonathan Cameron251eb402009-04-13 14:39:45 -0700223/**
224 * sht15_send_bit() - send an individual bit to the device
225 * @data: device state data
226 * @val: value of bit to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400227 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700228static inline void sht15_send_bit(struct sht15_data *data, int val)
229{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700230 gpio_set_value(data->pdata->gpio_data, val);
231 ndelay(SHT15_TSU);
232 gpio_set_value(data->pdata->gpio_sck, 1);
233 ndelay(SHT15_TSCKH);
234 gpio_set_value(data->pdata->gpio_sck, 0);
235 ndelay(SHT15_TSCKL); /* clock low time */
236}
237
238/**
239 * sht15_transmission_start() - specific sequence for new transmission
Jonathan Cameron251eb402009-04-13 14:39:45 -0700240 * @data: device state data
Vivien Didelot99a03782011-04-12 15:34:36 -0400241 *
Jonathan Cameron251eb402009-04-13 14:39:45 -0700242 * Timings for this are not documented on the data sheet, so very
243 * conservative ones used in implementation. This implements
244 * figure 12 on the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400245 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500246static int sht15_transmission_start(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700247{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500248 int err;
249
Jonathan Cameron251eb402009-04-13 14:39:45 -0700250 /* ensure data is high and output */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500251 err = gpio_direction_output(data->pdata->gpio_data, 1);
252 if (err)
253 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700254 ndelay(SHT15_TSU);
255 gpio_set_value(data->pdata->gpio_sck, 0);
256 ndelay(SHT15_TSCKL);
257 gpio_set_value(data->pdata->gpio_sck, 1);
258 ndelay(SHT15_TSCKH);
259 gpio_set_value(data->pdata->gpio_data, 0);
260 ndelay(SHT15_TSU);
261 gpio_set_value(data->pdata->gpio_sck, 0);
262 ndelay(SHT15_TSCKL);
263 gpio_set_value(data->pdata->gpio_sck, 1);
264 ndelay(SHT15_TSCKH);
265 gpio_set_value(data->pdata->gpio_data, 1);
266 ndelay(SHT15_TSU);
267 gpio_set_value(data->pdata->gpio_sck, 0);
268 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500269 return 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700270}
Vivien Didelot99a03782011-04-12 15:34:36 -0400271
Jonathan Cameron251eb402009-04-13 14:39:45 -0700272/**
273 * sht15_send_byte() - send a single byte to the device
274 * @data: device state
275 * @byte: value to be sent
Vivien Didelot99a03782011-04-12 15:34:36 -0400276 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700277static void sht15_send_byte(struct sht15_data *data, u8 byte)
278{
279 int i;
Vivien Didelot99a03782011-04-12 15:34:36 -0400280
Jonathan Cameron251eb402009-04-13 14:39:45 -0700281 for (i = 0; i < 8; i++) {
282 sht15_send_bit(data, !!(byte & 0x80));
283 byte <<= 1;
284 }
285}
Vivien Didelot99a03782011-04-12 15:34:36 -0400286
Jonathan Cameron251eb402009-04-13 14:39:45 -0700287/**
288 * sht15_wait_for_response() - checks for ack from device
289 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400290 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700291static int sht15_wait_for_response(struct sht15_data *data)
292{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500293 int err;
294
295 err = gpio_direction_input(data->pdata->gpio_data);
296 if (err)
297 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700298 gpio_set_value(data->pdata->gpio_sck, 1);
299 ndelay(SHT15_TSCKH);
300 if (gpio_get_value(data->pdata->gpio_data)) {
301 gpio_set_value(data->pdata->gpio_sck, 0);
302 dev_err(data->dev, "Command not acknowledged\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500303 err = sht15_connection_reset(data);
304 if (err)
305 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700306 return -EIO;
307 }
308 gpio_set_value(data->pdata->gpio_sck, 0);
309 ndelay(SHT15_TSCKL);
310 return 0;
311}
312
313/**
314 * sht15_send_cmd() - Sends a command to the device.
315 * @data: device state
316 * @cmd: command byte to be sent
317 *
318 * On entry, sck is output low, data is output pull high
319 * and the interrupt disabled.
Vivien Didelot99a03782011-04-12 15:34:36 -0400320 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700321static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
322{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500323 int err;
Vivien Didelot99a03782011-04-12 15:34:36 -0400324
Vivien Didelot412e29c2013-01-15 13:33:06 -0500325 err = sht15_transmission_start(data);
326 if (err)
327 return err;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700328 sht15_send_byte(data, cmd);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500329 return sht15_wait_for_response(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700330}
Vivien Didelot99a03782011-04-12 15:34:36 -0400331
Jonathan Cameron251eb402009-04-13 14:39:45 -0700332/**
Vivien Didelot181148a2011-04-12 15:34:37 -0400333 * sht15_soft_reset() - send a soft reset command
334 * @data: sht15 specific data.
335 *
336 * As described in section 3.2 of the datasheet.
337 */
338static int sht15_soft_reset(struct sht15_data *data)
339{
340 int ret;
341
342 ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
343 if (ret)
344 return ret;
345 msleep(SHT15_TSRST);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400346 /* device resets default hardware status register value */
347 data->val_status = 0;
Vivien Didelot181148a2011-04-12 15:34:37 -0400348
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400349 return ret;
350}
351
352/**
Jerome Oufella82c74652011-04-12 15:34:39 -0400353 * sht15_ack() - send a ack
354 * @data: sht15 specific data.
355 *
356 * Each byte of data is acknowledged by pulling the data line
357 * low for one clock pulse.
358 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500359static int sht15_ack(struct sht15_data *data)
Jerome Oufella82c74652011-04-12 15:34:39 -0400360{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500361 int err;
362
363 err = gpio_direction_output(data->pdata->gpio_data, 0);
364 if (err)
365 return err;
Jerome Oufella82c74652011-04-12 15:34:39 -0400366 ndelay(SHT15_TSU);
367 gpio_set_value(data->pdata->gpio_sck, 1);
368 ndelay(SHT15_TSU);
369 gpio_set_value(data->pdata->gpio_sck, 0);
370 ndelay(SHT15_TSU);
371 gpio_set_value(data->pdata->gpio_data, 1);
372
Vivien Didelot412e29c2013-01-15 13:33:06 -0500373 return gpio_direction_input(data->pdata->gpio_data);
Jerome Oufella82c74652011-04-12 15:34:39 -0400374}
375
376/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400377 * sht15_end_transmission() - notify device of end of transmission
378 * @data: device state.
379 *
380 * This is basically a NAK (single clock pulse, data high).
381 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500382static int sht15_end_transmission(struct sht15_data *data)
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400383{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500384 int err;
385
386 err = gpio_direction_output(data->pdata->gpio_data, 1);
387 if (err)
388 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400389 ndelay(SHT15_TSU);
390 gpio_set_value(data->pdata->gpio_sck, 1);
391 ndelay(SHT15_TSCKH);
392 gpio_set_value(data->pdata->gpio_sck, 0);
393 ndelay(SHT15_TSCKL);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500394 return 0;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400395}
396
397/**
398 * sht15_read_byte() - Read a byte back from the device
399 * @data: device state.
400 */
401static u8 sht15_read_byte(struct sht15_data *data)
402{
403 int i;
404 u8 byte = 0;
405
406 for (i = 0; i < 8; ++i) {
407 byte <<= 1;
408 gpio_set_value(data->pdata->gpio_sck, 1);
409 ndelay(SHT15_TSCKH);
410 byte |= !!gpio_get_value(data->pdata->gpio_data);
411 gpio_set_value(data->pdata->gpio_sck, 0);
412 ndelay(SHT15_TSCKL);
413 }
414 return byte;
415}
416
417/**
418 * sht15_send_status() - write the status register byte
419 * @data: sht15 specific data.
420 * @status: the byte to set the status register with.
421 *
422 * As described in figure 14 and table 5 of the datasheet.
423 */
424static int sht15_send_status(struct sht15_data *data, u8 status)
425{
Vivien Didelot412e29c2013-01-15 13:33:06 -0500426 int err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400427
Vivien Didelot412e29c2013-01-15 13:33:06 -0500428 err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
429 if (err)
430 return err;
431 err = gpio_direction_output(data->pdata->gpio_data, 1);
432 if (err)
433 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400434 ndelay(SHT15_TSU);
435 sht15_send_byte(data, status);
Vivien Didelot412e29c2013-01-15 13:33:06 -0500436 err = sht15_wait_for_response(data);
437 if (err)
438 return err;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400439
440 data->val_status = status;
Vivien Didelot181148a2011-04-12 15:34:37 -0400441 return 0;
442}
443
444/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400445 * sht15_update_status() - get updated status register from device if too old
446 * @data: device instance specific data.
447 *
448 * As described in figure 15 and table 5 of the datasheet.
449 */
450static int sht15_update_status(struct sht15_data *data)
451{
452 int ret = 0;
453 u8 status;
Jerome Oufella82c74652011-04-12 15:34:39 -0400454 u8 previous_config;
455 u8 dev_checksum = 0;
456 u8 checksum_vals[2];
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400457 int timeout = HZ;
458
459 mutex_lock(&data->read_lock);
460 if (time_after(jiffies, data->last_status + timeout)
461 || !data->status_valid) {
462 ret = sht15_send_cmd(data, SHT15_READ_STATUS);
463 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500464 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400465 status = sht15_read_byte(data);
466
Jerome Oufella82c74652011-04-12 15:34:39 -0400467 if (data->checksumming) {
468 sht15_ack(data);
yalin wang33836ee2015-08-10 22:41:43 +0800469 dev_checksum = bitrev8(sht15_read_byte(data));
Jerome Oufella82c74652011-04-12 15:34:39 -0400470 checksum_vals[0] = SHT15_READ_STATUS;
471 checksum_vals[1] = status;
472 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
473 == dev_checksum);
474 }
475
Vivien Didelot412e29c2013-01-15 13:33:06 -0500476 ret = sht15_end_transmission(data);
477 if (ret)
478 goto unlock;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400479
Jerome Oufella82c74652011-04-12 15:34:39 -0400480 /*
481 * Perform checksum validation on the received data.
482 * Specification mentions that in case a checksum verification
483 * fails, a soft reset command must be sent to the device.
484 */
485 if (data->checksumming && !data->checksum_ok) {
486 previous_config = data->val_status & 0x07;
487 ret = sht15_soft_reset(data);
488 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500489 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400490 if (previous_config) {
491 ret = sht15_send_status(data, previous_config);
492 if (ret) {
493 dev_err(data->dev,
494 "CRC validation failed, unable "
495 "to restore device settings\n");
Vivien Didelot412e29c2013-01-15 13:33:06 -0500496 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400497 }
498 }
499 ret = -EAGAIN;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500500 goto unlock;
Jerome Oufella82c74652011-04-12 15:34:39 -0400501 }
502
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400503 data->val_status = status;
504 data->status_valid = true;
505 data->last_status = jiffies;
506 }
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400507
Vivien Didelot412e29c2013-01-15 13:33:06 -0500508unlock:
509 mutex_unlock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400510 return ret;
511}
512
513/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400514 * sht15_measurement() - get a new value from device
Jonathan Cameron251eb402009-04-13 14:39:45 -0700515 * @data: device instance specific data
516 * @command: command sent to request value
517 * @timeout_msecs: timeout after which comms are assumed
518 * to have failed are reset.
Vivien Didelot99a03782011-04-12 15:34:36 -0400519 */
520static int sht15_measurement(struct sht15_data *data,
521 int command,
522 int timeout_msecs)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700523{
524 int ret;
Jerome Oufella82c74652011-04-12 15:34:39 -0400525 u8 previous_config;
Vivien Didelot99a03782011-04-12 15:34:36 -0400526
Jonathan Cameron251eb402009-04-13 14:39:45 -0700527 ret = sht15_send_cmd(data, command);
528 if (ret)
529 return ret;
530
Vivien Didelot412e29c2013-01-15 13:33:06 -0500531 ret = gpio_direction_input(data->pdata->gpio_data);
532 if (ret)
533 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700534 atomic_set(&data->interrupt_handled, 0);
535
536 enable_irq(gpio_to_irq(data->pdata->gpio_data));
537 if (gpio_get_value(data->pdata->gpio_data) == 0) {
538 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300539 /* Only relevant if the interrupt hasn't occurred. */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700540 if (!atomic_read(&data->interrupt_handled))
541 schedule_work(&data->read_work);
542 }
543 ret = wait_event_timeout(data->wait_queue,
Vivien Didelot99a03782011-04-12 15:34:36 -0400544 (data->state == SHT15_READING_NOTHING),
Jonathan Cameron251eb402009-04-13 14:39:45 -0700545 msecs_to_jiffies(timeout_msecs));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500546 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
547 data->state = SHT15_READING_NOTHING;
548 return -EIO;
549 } else if (ret == 0) { /* timeout occurred */
Joe Perches24205e02009-07-11 13:42:37 +0200550 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -0500551 ret = sht15_connection_reset(data);
552 if (ret)
553 return ret;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700554 return -ETIME;
555 }
Jerome Oufella82c74652011-04-12 15:34:39 -0400556
557 /*
558 * Perform checksum validation on the received data.
559 * Specification mentions that in case a checksum verification fails,
560 * a soft reset command must be sent to the device.
561 */
562 if (data->checksumming && !data->checksum_ok) {
563 previous_config = data->val_status & 0x07;
564 ret = sht15_soft_reset(data);
565 if (ret)
566 return ret;
567 if (previous_config) {
568 ret = sht15_send_status(data, previous_config);
569 if (ret) {
570 dev_err(data->dev,
571 "CRC validation failed, unable "
572 "to restore device settings\n");
573 return ret;
574 }
575 }
576 return -EAGAIN;
577 }
578
Jonathan Cameron251eb402009-04-13 14:39:45 -0700579 return 0;
580}
581
582/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400583 * sht15_update_measurements() - get updated measures from device if too old
Jonathan Cameron251eb402009-04-13 14:39:45 -0700584 * @data: device state
Vivien Didelot99a03782011-04-12 15:34:36 -0400585 */
586static int sht15_update_measurements(struct sht15_data *data)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700587{
588 int ret = 0;
589 int timeout = HZ;
590
591 mutex_lock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -0400592 if (time_after(jiffies, data->last_measurement + timeout)
593 || !data->measurements_valid) {
594 data->state = SHT15_READING_HUMID;
595 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700596 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500597 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400598 data->state = SHT15_READING_TEMP;
599 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700600 if (ret)
Vivien Didelot412e29c2013-01-15 13:33:06 -0500601 goto unlock;
Vivien Didelot99a03782011-04-12 15:34:36 -0400602 data->measurements_valid = true;
603 data->last_measurement = jiffies;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700604 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700605
Vivien Didelot412e29c2013-01-15 13:33:06 -0500606unlock:
607 mutex_unlock(&data->read_lock);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700608 return ret;
609}
610
611/**
612 * sht15_calc_temp() - convert the raw reading to a temperature
613 * @data: device state
614 *
615 * As per section 4.3 of the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400616 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700617static inline int sht15_calc_temp(struct sht15_data *data)
618{
Jerome Oufella328a2c22010-04-14 16:14:07 +0200619 int d1 = temppoints[0].d1;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400620 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700621 int i;
622
Jerome Oufella328a2c22010-04-14 16:14:07 +0200623 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700624 /* Find pointer to interpolate */
Vivien Didelot142c0902013-01-07 14:18:38 -0500625 if (data->supply_uv > temppoints[i - 1].vdd) {
626 d1 = (data->supply_uv - temppoints[i - 1].vdd)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700627 * (temppoints[i].d1 - temppoints[i - 1].d1)
628 / (temppoints[i].vdd - temppoints[i - 1].vdd)
629 + temppoints[i - 1].d1;
630 break;
631 }
632
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400633 return data->val_temp * d2 + d1;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700634}
635
636/**
637 * sht15_calc_humid() - using last temperature convert raw to humid
638 * @data: device state
639 *
640 * This is the temperature compensated version as per section 4.2 of
641 * the data sheet.
Vivien Didelot99a03782011-04-12 15:34:36 -0400642 *
643 * The sensor is assumed to be V3, which is compatible with V4.
644 * Humidity conversion coefficients are shown in table 7 of the datasheet.
645 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700646static inline int sht15_calc_humid(struct sht15_data *data)
647{
Vivien Didelot99a03782011-04-12 15:34:36 -0400648 int rh_linear; /* milli percent */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700649 int temp = sht15_calc_temp(data);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400650 int c2, c3;
651 int t2;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700652 const int c1 = -4;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400653
654 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
655 c2 = 648000; /* x 10 ^ -6 */
656 c3 = -7200; /* x 10 ^ -7 */
657 t2 = 1280;
658 } else {
659 c2 = 40500; /* x 10 ^ -6 */
660 c3 = -28; /* x 10 ^ -7 */
661 t2 = 80;
662 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700663
Vivien Didelot99a03782011-04-12 15:34:36 -0400664 rh_linear = c1 * 1000
665 + c2 * data->val_humid / 1000
Vivien Didelotccd32e72011-03-21 17:59:35 +0100666 + (data->val_humid * data->val_humid * c3) / 10000;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400667 return (temp - 25000) * (10000 + t2 * data->val_humid)
Vivien Didelot99a03782011-04-12 15:34:36 -0400668 / 1000000 + rh_linear;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700669}
670
Vivien Didelot99a03782011-04-12 15:34:36 -0400671/**
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400672 * sht15_show_status() - show status information in sysfs
673 * @dev: device.
674 * @attr: device attribute.
675 * @buf: sysfs buffer where information is written to.
676 *
677 * Will be called on read access to temp1_fault, humidity1_fault
678 * and heater_enable sysfs attributes.
679 * Returns number of bytes written into buffer, negative errno on error.
680 */
681static ssize_t sht15_show_status(struct device *dev,
682 struct device_attribute *attr,
683 char *buf)
684{
685 int ret;
686 struct sht15_data *data = dev_get_drvdata(dev);
687 u8 bit = to_sensor_dev_attr(attr)->index;
688
689 ret = sht15_update_status(data);
690
691 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
692}
693
694/**
695 * sht15_store_heater() - change heater state via sysfs
696 * @dev: device.
697 * @attr: device attribute.
698 * @buf: sysfs buffer to read the new heater state from.
699 * @count: length of the data.
700 *
Vivien Didelote9b6e9f2011-07-25 21:46:10 +0200701 * Will be called on write access to heater_enable sysfs attribute.
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400702 * Returns number of bytes actually decoded, negative errno on error.
703 */
704static ssize_t sht15_store_heater(struct device *dev,
705 struct device_attribute *attr,
706 const char *buf, size_t count)
707{
708 int ret;
709 struct sht15_data *data = dev_get_drvdata(dev);
710 long value;
711 u8 status;
712
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100713 if (kstrtol(buf, 10, &value))
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400714 return -EINVAL;
715
716 mutex_lock(&data->read_lock);
717 status = data->val_status & 0x07;
718 if (!!value)
719 status |= SHT15_STATUS_HEATER;
720 else
721 status &= ~SHT15_STATUS_HEATER;
722
723 ret = sht15_send_status(data, status);
724 mutex_unlock(&data->read_lock);
725
726 return ret ? ret : count;
727}
728
729/**
Vivien Didelot99a03782011-04-12 15:34:36 -0400730 * sht15_show_temp() - show temperature measurement value in sysfs
731 * @dev: device.
732 * @attr: device attribute.
733 * @buf: sysfs buffer where measurement values are written to.
734 *
735 * Will be called on read access to temp1_input sysfs attribute.
736 * Returns number of bytes written into buffer, negative errno on error.
737 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700738static ssize_t sht15_show_temp(struct device *dev,
739 struct device_attribute *attr,
740 char *buf)
741{
742 int ret;
743 struct sht15_data *data = dev_get_drvdata(dev);
744
745 /* Technically no need to read humidity as well */
Vivien Didelot99a03782011-04-12 15:34:36 -0400746 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700747
748 return ret ? ret : sprintf(buf, "%d\n",
749 sht15_calc_temp(data));
750}
751
Vivien Didelot99a03782011-04-12 15:34:36 -0400752/**
753 * sht15_show_humidity() - show humidity measurement value in sysfs
754 * @dev: device.
755 * @attr: device attribute.
756 * @buf: sysfs buffer where measurement values are written to.
757 *
758 * Will be called on read access to humidity1_input sysfs attribute.
759 * Returns number of bytes written into buffer, negative errno on error.
760 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700761static ssize_t sht15_show_humidity(struct device *dev,
762 struct device_attribute *attr,
763 char *buf)
764{
765 int ret;
766 struct sht15_data *data = dev_get_drvdata(dev);
767
Vivien Didelot99a03782011-04-12 15:34:36 -0400768 ret = sht15_update_measurements(data);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700769
770 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
Vivien Didelot99a03782011-04-12 15:34:36 -0400771}
772
Julia Lawallaf3d3872016-12-22 13:05:04 +0100773static ssize_t name_show(struct device *dev,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700774 struct device_attribute *attr,
775 char *buf)
776{
777 struct platform_device *pdev = to_platform_device(dev);
778 return sprintf(buf, "%s\n", pdev->name);
779}
780
Vivien Didelot99a03782011-04-12 15:34:36 -0400781static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
782 sht15_show_temp, NULL, 0);
783static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
784 sht15_show_humidity, NULL, 0);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400785static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
786 SHT15_STATUS_LOW_BATTERY);
787static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
788 SHT15_STATUS_LOW_BATTERY);
789static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
790 sht15_store_heater, SHT15_STATUS_HEATER);
Julia Lawallaf3d3872016-12-22 13:05:04 +0100791static DEVICE_ATTR_RO(name);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700792static struct attribute *sht15_attrs[] = {
793 &sensor_dev_attr_temp1_input.dev_attr.attr,
794 &sensor_dev_attr_humidity1_input.dev_attr.attr,
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400795 &sensor_dev_attr_temp1_fault.dev_attr.attr,
796 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
797 &sensor_dev_attr_heater_enable.dev_attr.attr,
Jonathan Cameron251eb402009-04-13 14:39:45 -0700798 &dev_attr_name.attr,
799 NULL,
800};
801
802static const struct attribute_group sht15_attr_group = {
803 .attrs = sht15_attrs,
804};
805
806static irqreturn_t sht15_interrupt_fired(int irq, void *d)
807{
808 struct sht15_data *data = d;
Vivien Didelot99a03782011-04-12 15:34:36 -0400809
Jonathan Cameron251eb402009-04-13 14:39:45 -0700810 /* First disable the interrupt */
811 disable_irq_nosync(irq);
812 atomic_inc(&data->interrupt_handled);
813 /* Then schedule a reading work struct */
Vivien Didelot99a03782011-04-12 15:34:36 -0400814 if (data->state != SHT15_READING_NOTHING)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700815 schedule_work(&data->read_work);
816 return IRQ_HANDLED;
817}
818
Jonathan Cameron251eb402009-04-13 14:39:45 -0700819static void sht15_bh_read_data(struct work_struct *work_s)
820{
Jonathan Cameron251eb402009-04-13 14:39:45 -0700821 uint16_t val = 0;
Jerome Oufella82c74652011-04-12 15:34:39 -0400822 u8 dev_checksum = 0;
823 u8 checksum_vals[3];
Jonathan Cameron251eb402009-04-13 14:39:45 -0700824 struct sht15_data *data
825 = container_of(work_s, struct sht15_data,
826 read_work);
Vivien Didelot99a03782011-04-12 15:34:36 -0400827
Jonathan Cameron251eb402009-04-13 14:39:45 -0700828 /* Firstly, verify the line is low */
829 if (gpio_get_value(data->pdata->gpio_data)) {
Vivien Didelot99a03782011-04-12 15:34:36 -0400830 /*
831 * If not, then start the interrupt again - care here as could
832 * have gone low in meantime so verify it hasn't!
833 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700834 atomic_set(&data->interrupt_handled, 0);
835 enable_irq(gpio_to_irq(data->pdata->gpio_data));
Frans Meulenbroeksc9e14982012-01-08 19:34:06 +0100836 /* If still not occurred or another handler was scheduled */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700837 if (gpio_get_value(data->pdata->gpio_data)
838 || atomic_read(&data->interrupt_handled))
839 return;
840 }
Vivien Didelot99a03782011-04-12 15:34:36 -0400841
Jonathan Cameron251eb402009-04-13 14:39:45 -0700842 /* Read the data back from the device */
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400843 val = sht15_read_byte(data);
844 val <<= 8;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500845 if (sht15_ack(data))
846 goto wakeup;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400847 val |= sht15_read_byte(data);
Vivien Didelot99a03782011-04-12 15:34:36 -0400848
Jerome Oufella82c74652011-04-12 15:34:39 -0400849 if (data->checksumming) {
850 /*
851 * Ask the device for a checksum and read it back.
852 * Note: the device sends the checksum byte reversed.
853 */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500854 if (sht15_ack(data))
855 goto wakeup;
yalin wang33836ee2015-08-10 22:41:43 +0800856 dev_checksum = bitrev8(sht15_read_byte(data));
Jerome Oufella82c74652011-04-12 15:34:39 -0400857 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
858 SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
859 checksum_vals[1] = (u8) (val >> 8);
860 checksum_vals[2] = (u8) val;
861 data->checksum_ok
862 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
863 }
864
Jonathan Cameron251eb402009-04-13 14:39:45 -0700865 /* Tell the device we are done */
Vivien Didelot412e29c2013-01-15 13:33:06 -0500866 if (sht15_end_transmission(data))
867 goto wakeup;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700868
Vivien Didelot99a03782011-04-12 15:34:36 -0400869 switch (data->state) {
Jonathan Cameron251eb402009-04-13 14:39:45 -0700870 case SHT15_READING_TEMP:
871 data->val_temp = val;
872 break;
873 case SHT15_READING_HUMID:
874 data->val_humid = val;
875 break;
Vivien Didelot99a03782011-04-12 15:34:36 -0400876 default:
877 break;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700878 }
879
Vivien Didelot99a03782011-04-12 15:34:36 -0400880 data->state = SHT15_READING_NOTHING;
Vivien Didelot412e29c2013-01-15 13:33:06 -0500881wakeup:
Jonathan Cameron251eb402009-04-13 14:39:45 -0700882 wake_up(&data->wait_queue);
883}
884
885static void sht15_update_voltage(struct work_struct *work_s)
886{
887 struct sht15_data *data
888 = container_of(work_s, struct sht15_data,
889 update_supply_work);
Vivien Didelot142c0902013-01-07 14:18:38 -0500890 data->supply_uv = regulator_get_voltage(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -0700891}
892
893/**
894 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
895 * @nb: associated notification structure
896 * @event: voltage regulator state change event code
897 * @ignored: function parameter - ignored here
898 *
899 * Note that as the notification code holds the regulator lock, we have
900 * to schedule an update of the supply voltage rather than getting it directly.
Vivien Didelot99a03782011-04-12 15:34:36 -0400901 */
Jonathan Cameron251eb402009-04-13 14:39:45 -0700902static int sht15_invalidate_voltage(struct notifier_block *nb,
Vivien Didelot99a03782011-04-12 15:34:36 -0400903 unsigned long event,
904 void *ignored)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700905{
906 struct sht15_data *data = container_of(nb, struct sht15_data, nb);
907
908 if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
Vivien Didelot142c0902013-01-07 14:18:38 -0500909 data->supply_uv_valid = false;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700910 schedule_work(&data->update_supply_work);
911
912 return NOTIFY_OK;
913}
914
Marco Franchi2f1736f2017-02-16 10:23:43 -0200915#ifdef CONFIG_OF
916static const struct of_device_id sht15_dt_match[] = {
917 { .compatible = "sensirion,sht15" },
918 { },
919};
920MODULE_DEVICE_TABLE(of, sht15_dt_match);
921
922/*
923 * This function returns NULL if pdev isn't a device instatiated by dt,
924 * a pointer to pdata if it could successfully get all information
925 * from dt or a negative ERR_PTR() on error.
926 */
927static struct sht15_platform_data *sht15_probe_dt(struct device *dev)
928{
929 struct device_node *np = dev->of_node;
930 struct sht15_platform_data *pdata;
931
932 /* no device tree device */
933 if (!np)
934 return NULL;
935
936 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
937 if (!pdata)
938 return ERR_PTR(-ENOMEM);
939
940 pdata->gpio_data = of_get_named_gpio(np, "data-gpios", 0);
941 if (pdata->gpio_data < 0) {
942 if (pdata->gpio_data != -EPROBE_DEFER)
943 dev_err(dev, "data-gpios not found\n");
944 return ERR_PTR(pdata->gpio_data);
945 }
946
947 pdata->gpio_sck = of_get_named_gpio(np, "clk-gpios", 0);
948 if (pdata->gpio_sck < 0) {
949 if (pdata->gpio_sck != -EPROBE_DEFER)
950 dev_err(dev, "clk-gpios not found\n");
951 return ERR_PTR(pdata->gpio_sck);
952 }
953
954 return pdata;
955}
956#else
957static inline struct sht15_platform_data *sht15_probe_dt(struct device *dev)
958{
959 return NULL;
960}
961#endif
962
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500963static int sht15_probe(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -0700964{
Vivien Didelot6edf3c32012-01-26 15:59:00 -0500965 int ret;
Guenter Roeck38fe7562012-06-02 11:20:22 -0700966 struct sht15_data *data;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400967 u8 status = 0;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700968
Guenter Roeck38fe7562012-06-02 11:20:22 -0700969 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
970 if (!data)
971 return -ENOMEM;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700972
973 INIT_WORK(&data->read_work, sht15_bh_read_data);
974 INIT_WORK(&data->update_supply_work, sht15_update_voltage);
975 platform_set_drvdata(pdev, data);
976 mutex_init(&data->read_lock);
977 data->dev = &pdev->dev;
978 init_waitqueue_head(&data->wait_queue);
979
Marco Franchi2f1736f2017-02-16 10:23:43 -0200980 data->pdata = sht15_probe_dt(&pdev->dev);
981 if (IS_ERR(data->pdata))
982 return PTR_ERR(data->pdata);
983 if (data->pdata == NULL) {
984 data->pdata = dev_get_platdata(&pdev->dev);
985 if (data->pdata == NULL) {
986 dev_err(&pdev->dev, "no platform data supplied\n");
987 return -EINVAL;
988 }
Jonathan Cameron251eb402009-04-13 14:39:45 -0700989 }
Marco Franchi2f1736f2017-02-16 10:23:43 -0200990
Vivien Didelot142c0902013-01-07 14:18:38 -0500991 data->supply_uv = data->pdata->supply_mv * 1000;
Jerome Oufella82c74652011-04-12 15:34:39 -0400992 if (data->pdata->checksum)
993 data->checksumming = true;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -0400994 if (data->pdata->no_otp_reload)
995 status |= SHT15_STATUS_NO_OTP_RELOAD;
996 if (data->pdata->low_resolution)
997 status |= SHT15_STATUS_LOW_RESOLUTION;
Jonathan Cameron251eb402009-04-13 14:39:45 -0700998
Vivien Didelot99a03782011-04-12 15:34:36 -0400999 /*
1000 * If a regulator is available,
1001 * query what the supply voltage actually is!
1002 */
Mark Brown9e059ba2013-08-18 17:35:52 +01001003 data->reg = devm_regulator_get_optional(data->dev, "vcc");
Jonathan Cameron251eb402009-04-13 14:39:45 -07001004 if (!IS_ERR(data->reg)) {
Jean Delvarec7a78d22010-04-14 16:14:08 +02001005 int voltage;
1006
1007 voltage = regulator_get_voltage(data->reg);
1008 if (voltage)
Vivien Didelot142c0902013-01-07 14:18:38 -05001009 data->supply_uv = voltage;
Jean Delvarec7a78d22010-04-14 16:14:08 +02001010
Mark Brown3e780802013-03-02 15:33:30 +08001011 ret = regulator_enable(data->reg);
1012 if (ret != 0) {
1013 dev_err(&pdev->dev,
1014 "failed to enable regulator: %d\n", ret);
1015 return ret;
1016 }
1017
Vivien Didelot99a03782011-04-12 15:34:36 -04001018 /*
1019 * Setup a notifier block to update this if another device
1020 * causes the voltage to change
1021 */
Jonathan Cameron251eb402009-04-13 14:39:45 -07001022 data->nb.notifier_call = &sht15_invalidate_voltage;
1023 ret = regulator_register_notifier(data->reg, &data->nb);
Vivien Didelot181148a2011-04-12 15:34:37 -04001024 if (ret) {
1025 dev_err(&pdev->dev,
1026 "regulator notifier request failed\n");
1027 regulator_disable(data->reg);
Guenter Roeck38fe7562012-06-02 11:20:22 -07001028 return ret;
Vivien Didelot181148a2011-04-12 15:34:37 -04001029 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001030 }
Vivien Didelot99a03782011-04-12 15:34:36 -04001031
1032 /* Try requesting the GPIOs */
Vivien Didelot412e29c2013-01-15 13:33:06 -05001033 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
1034 GPIOF_OUT_INIT_LOW, "SHT15 sck");
Jonathan Cameron251eb402009-04-13 14:39:45 -07001035 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -05001036 dev_err(&pdev->dev, "clock line GPIO request failed\n");
Vivien Didelot181148a2011-04-12 15:34:37 -04001037 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001038 }
Vivien Didelot99a03782011-04-12 15:34:36 -04001039
Guenter Roeck38fe7562012-06-02 11:20:22 -07001040 ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
1041 "SHT15 data");
Jonathan Cameron251eb402009-04-13 14:39:45 -07001042 if (ret) {
Vivien Didelot412e29c2013-01-15 13:33:06 -05001043 dev_err(&pdev->dev, "data line GPIO request failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001044 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001045 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001046
Guenter Roeck38fe7562012-06-02 11:20:22 -07001047 ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
1048 sht15_interrupt_fired,
1049 IRQF_TRIGGER_FALLING,
1050 "sht15 data",
1051 data);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001052 if (ret) {
Vivien Didelot99a03782011-04-12 15:34:36 -04001053 dev_err(&pdev->dev, "failed to get irq for data line\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001054 goto err_release_reg;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001055 }
1056 disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
Vivien Didelot412e29c2013-01-15 13:33:06 -05001057 ret = sht15_connection_reset(data);
1058 if (ret)
1059 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001060 ret = sht15_soft_reset(data);
1061 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001062 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001063
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001064 /* write status with platform data options */
1065 if (status) {
1066 ret = sht15_send_status(data, status);
1067 if (ret)
Guenter Roeck38fe7562012-06-02 11:20:22 -07001068 goto err_release_reg;
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001069 }
1070
Vivien Didelot181148a2011-04-12 15:34:37 -04001071 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
1072 if (ret) {
1073 dev_err(&pdev->dev, "sysfs create failed\n");
Guenter Roeck38fe7562012-06-02 11:20:22 -07001074 goto err_release_reg;
Vivien Didelot181148a2011-04-12 15:34:37 -04001075 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001076
1077 data->hwmon_dev = hwmon_device_register(data->dev);
1078 if (IS_ERR(data->hwmon_dev)) {
1079 ret = PTR_ERR(data->hwmon_dev);
Vivien Didelot181148a2011-04-12 15:34:37 -04001080 goto err_release_sysfs_group;
Jonathan Cameron251eb402009-04-13 14:39:45 -07001081 }
Vivien Didelot99a03782011-04-12 15:34:36 -04001082
Jonathan Cameron251eb402009-04-13 14:39:45 -07001083 return 0;
1084
Vivien Didelot181148a2011-04-12 15:34:37 -04001085err_release_sysfs_group:
1086 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
Vivien Didelot181148a2011-04-12 15:34:37 -04001087err_release_reg:
1088 if (!IS_ERR(data->reg)) {
1089 regulator_unregister_notifier(data->reg, &data->nb);
1090 regulator_disable(data->reg);
Vivien Didelot181148a2011-04-12 15:34:37 -04001091 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001092 return ret;
1093}
1094
Bill Pemberton281dfd02012-11-19 13:25:51 -05001095static int sht15_remove(struct platform_device *pdev)
Jonathan Cameron251eb402009-04-13 14:39:45 -07001096{
1097 struct sht15_data *data = platform_get_drvdata(pdev);
1098
Vivien Didelot99a03782011-04-12 15:34:36 -04001099 /*
1100 * Make sure any reads from the device are done and
1101 * prevent new ones beginning
1102 */
Jonathan Cameron251eb402009-04-13 14:39:45 -07001103 mutex_lock(&data->read_lock);
Vivien Didelotcc15c7e2011-04-12 15:34:38 -04001104 if (sht15_soft_reset(data)) {
1105 mutex_unlock(&data->read_lock);
1106 return -EFAULT;
1107 }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001108 hwmon_device_unregister(data->hwmon_dev);
1109 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
1110 if (!IS_ERR(data->reg)) {
1111 regulator_unregister_notifier(data->reg, &data->nb);
1112 regulator_disable(data->reg);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001113 }
1114
Jonathan Cameron251eb402009-04-13 14:39:45 -07001115 mutex_unlock(&data->read_lock);
Vivien Didelot99a03782011-04-12 15:34:36 -04001116
Jonathan Cameron251eb402009-04-13 14:39:45 -07001117 return 0;
1118}
1119
Krzysztof Kozlowski9c407232015-05-02 00:56:23 +09001120static const struct platform_device_id sht15_device_ids[] = {
Vivien Didelotedec5af2012-08-30 21:46:19 -04001121 { "sht10", sht10 },
1122 { "sht11", sht11 },
1123 { "sht15", sht15 },
1124 { "sht71", sht71 },
1125 { "sht75", sht75 },
1126 { }
Jonathan Cameron251eb402009-04-13 14:39:45 -07001127};
Vivien Didelotedec5af2012-08-30 21:46:19 -04001128MODULE_DEVICE_TABLE(platform, sht15_device_ids);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001129
Vivien Didelotedec5af2012-08-30 21:46:19 -04001130static struct platform_driver sht15_driver = {
1131 .driver = {
1132 .name = "sht15",
Marco Franchi2f1736f2017-02-16 10:23:43 -02001133 .of_match_table = of_match_ptr(sht15_dt_match),
Vivien Didelotedec5af2012-08-30 21:46:19 -04001134 },
1135 .probe = sht15_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001136 .remove = sht15_remove,
Vivien Didelotedec5af2012-08-30 21:46:19 -04001137 .id_table = sht15_device_ids,
1138};
1139module_platform_driver(sht15_driver);
Jonathan Cameron251eb402009-04-13 14:39:45 -07001140
1141MODULE_LICENSE("GPL");
Vivien Didelotedec5af2012-08-30 21:46:19 -04001142MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");