| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * sht15.c - support for the SHT15 Temperature and Humidity Sensor | 
|  | 3 | * | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 4 | * Portions Copyright (c) 2010-2011 Savoir-faire Linux Inc. | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 5 | *          Jerome Oufella <jerome.oufella@savoirfairelinux.com> | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 6 | *          Vivien Didelot <vivien.didelot@savoirfairelinux.com> | 
|  | 7 | * | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 8 | * 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 Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 16 | * For further information, see the Documentation/hwmon/sht15 file. | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 17 | */ | 
|  | 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> | 
|  | 27 | #include <linux/platform_device.h> | 
| Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 28 | #include <linux/sched.h> | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 29 | #include <linux/delay.h> | 
|  | 30 | #include <linux/jiffies.h> | 
|  | 31 | #include <linux/err.h> | 
|  | 32 | #include <linux/sht15.h> | 
|  | 33 | #include <linux/regulator/consumer.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> | 
| Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 35 | #include <linux/atomic.h> | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 36 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 37 | /* Commands */ | 
|  | 38 | #define SHT15_MEASURE_TEMP		0x03 | 
|  | 39 | #define SHT15_MEASURE_RH		0x05 | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 40 | #define SHT15_WRITE_STATUS		0x06 | 
|  | 41 | #define SHT15_READ_STATUS		0x07 | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 42 | #define SHT15_SOFT_RESET		0x1E | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 43 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 44 | /* Min timings */ | 
|  | 45 | #define SHT15_TSCKL			100	/* (nsecs) clock low */ | 
|  | 46 | #define SHT15_TSCKH			100	/* (nsecs) clock high */ | 
|  | 47 | #define SHT15_TSU			150	/* (nsecs) data setup time */ | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 48 | #define SHT15_TSRST			11	/* (msecs) soft reset time */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 49 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 50 | /* Status Register Bits */ | 
|  | 51 | #define SHT15_STATUS_LOW_RESOLUTION	0x01 | 
|  | 52 | #define SHT15_STATUS_NO_OTP_RELOAD	0x02 | 
|  | 53 | #define SHT15_STATUS_HEATER		0x04 | 
|  | 54 | #define SHT15_STATUS_LOW_BATTERY	0x40 | 
|  | 55 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 56 | /* Actions the driver may be doing */ | 
|  | 57 | enum sht15_state { | 
|  | 58 | SHT15_READING_NOTHING, | 
|  | 59 | SHT15_READING_TEMP, | 
|  | 60 | SHT15_READING_HUMID | 
|  | 61 | }; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 62 |  | 
|  | 63 | /** | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 64 | * struct sht15_temppair - elements of voltage dependent temp calc | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 65 | * @vdd:	supply voltage in microvolts | 
|  | 66 | * @d1:		see data sheet | 
|  | 67 | */ | 
|  | 68 | struct sht15_temppair { | 
|  | 69 | int vdd; /* microvolts */ | 
|  | 70 | int d1; | 
|  | 71 | }; | 
|  | 72 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 73 | /* Table 9 from datasheet - relates temperature calculation to supply voltage */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 74 | static const struct sht15_temppair temppoints[] = { | 
|  | 75 | { 2500000, -39400 }, | 
|  | 76 | { 3000000, -39600 }, | 
|  | 77 | { 3500000, -39700 }, | 
|  | 78 | { 4000000, -39800 }, | 
|  | 79 | { 5000000, -40100 }, | 
|  | 80 | }; | 
|  | 81 |  | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 82 | /* Table from CRC datasheet, section 2.4 */ | 
|  | 83 | static const u8 sht15_crc8_table[] = { | 
|  | 84 | 0,	49,	98,	83,	196,	245,	166,	151, | 
|  | 85 | 185,	136,	219,	234,	125,	76,	31,	46, | 
|  | 86 | 67,	114,	33,	16,	135,	182,	229,	212, | 
|  | 87 | 250,	203,	152,	169,	62,	15,	92,	109, | 
|  | 88 | 134,	183,	228,	213,	66,	115,	32,	17, | 
|  | 89 | 63,	14,	93,	108,	251,	202,	153,	168, | 
|  | 90 | 197,	244,	167,	150,	1,	48,	99,	82, | 
|  | 91 | 124,	77,	30,	47,	184,	137,	218,	235, | 
|  | 92 | 61,	12,	95,	110,	249,	200,	155,	170, | 
|  | 93 | 132,	181,	230,	215,	64,	113,	34,	19, | 
|  | 94 | 126,	79,	28,	45,	186,	139,	216,	233, | 
|  | 95 | 199,	246,	165,	148,	3,	50,	97,	80, | 
|  | 96 | 187,	138,	217,	232,	127,	78,	29,	44, | 
|  | 97 | 2,	51,	96,	81,	198,	247,	164,	149, | 
|  | 98 | 248,	201,	154,	171,	60,	13,	94,	111, | 
|  | 99 | 65,	112,	35,	18,	133,	180,	231,	214, | 
|  | 100 | 122,	75,	24,	41,	190,	143,	220,	237, | 
|  | 101 | 195,	242,	161,	144,	7,	54,	101,	84, | 
|  | 102 | 57,	8,	91,	106,	253,	204,	159,	174, | 
|  | 103 | 128,	177,	226,	211,	68,	117,	38,	23, | 
|  | 104 | 252,	205,	158,	175,	56,	9,	90,	107, | 
|  | 105 | 69,	116,	39,	22,	129,	176,	227,	210, | 
|  | 106 | 191,	142,	221,	236,	123,	74,	25,	40, | 
|  | 107 | 6,	55,	100,	85,	194,	243,	160,	145, | 
|  | 108 | 71,	118,	37,	20,	131,	178,	225,	208, | 
|  | 109 | 254,	207,	156,	173,	58,	11,	88,	105, | 
|  | 110 | 4,	53,	102,	87,	192,	241,	162,	147, | 
|  | 111 | 189,	140,	223,	238,	121,	72,	27,	42, | 
|  | 112 | 193,	240,	163,	146,	5,	52,	103,	86, | 
|  | 113 | 120,	73,	26,	43,	188,	141,	222,	239, | 
|  | 114 | 130,	179,	224,	209,	70,	119,	36,	21, | 
|  | 115 | 59,	10,	89,	104,	255,	206,	157,	172 | 
|  | 116 | }; | 
|  | 117 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 118 | /** | 
|  | 119 | * struct sht15_data - device instance specific data | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 120 | * @pdata:		platform data (gpio's etc). | 
|  | 121 | * @read_work:		bh of interrupt handler. | 
|  | 122 | * @wait_queue:		wait queue for getting values from device. | 
|  | 123 | * @val_temp:		last temperature value read from device. | 
|  | 124 | * @val_humid:		last humidity value read from device. | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 125 | * @val_status:		last status register value read from device. | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 126 | * @checksum_ok:	last value read from the device passed CRC validation. | 
|  | 127 | * @checksumming:	flag used to enable the data validation with CRC. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 128 | * @state:		state identifying the action the driver is doing. | 
|  | 129 | * @measurements_valid:	are the current stored measures valid (start condition). | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 130 | * @status_valid:	is the current stored status valid (start condition). | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 131 | * @last_measurement:	time of last measure. | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 132 | * @last_status:	time of last status reading. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 133 | * @read_lock:		mutex to ensure only one read in progress at a time. | 
|  | 134 | * @dev:		associate device structure. | 
|  | 135 | * @hwmon_dev:		device associated with hwmon subsystem. | 
|  | 136 | * @reg:		associated regulator (if specified). | 
|  | 137 | * @nb:			notifier block to handle notifications of voltage | 
|  | 138 | *                      changes. | 
|  | 139 | * @supply_uV:		local copy of supply voltage used to allow use of | 
|  | 140 | *                      regulator consumer if available. | 
|  | 141 | * @supply_uV_valid:	indicates that an updated value has not yet been | 
|  | 142 | *			obtained from the regulator and so any calculations | 
|  | 143 | *			based upon it will be invalid. | 
|  | 144 | * @update_supply_work:	work struct that is used to update the supply_uV. | 
|  | 145 | * @interrupt_handled:	flag used to indicate a handler has been scheduled. | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 146 | */ | 
|  | 147 | struct sht15_data { | 
|  | 148 | struct sht15_platform_data	*pdata; | 
|  | 149 | struct work_struct		read_work; | 
|  | 150 | wait_queue_head_t		wait_queue; | 
|  | 151 | uint16_t			val_temp; | 
|  | 152 | uint16_t			val_humid; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 153 | u8				val_status; | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 154 | bool				checksum_ok; | 
|  | 155 | bool				checksumming; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 156 | enum sht15_state		state; | 
|  | 157 | bool				measurements_valid; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 158 | bool				status_valid; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 159 | unsigned long			last_measurement; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 160 | unsigned long			last_status; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 161 | struct mutex			read_lock; | 
|  | 162 | struct device			*dev; | 
|  | 163 | struct device			*hwmon_dev; | 
|  | 164 | struct regulator		*reg; | 
|  | 165 | struct notifier_block		nb; | 
|  | 166 | int				supply_uV; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 167 | bool				supply_uV_valid; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 168 | struct work_struct		update_supply_work; | 
|  | 169 | atomic_t			interrupt_handled; | 
|  | 170 | }; | 
|  | 171 |  | 
|  | 172 | /** | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 173 | * sht15_reverse() - reverse a byte | 
|  | 174 | * @byte:    byte to reverse. | 
|  | 175 | */ | 
|  | 176 | static u8 sht15_reverse(u8 byte) | 
|  | 177 | { | 
|  | 178 | u8 i, c; | 
|  | 179 |  | 
|  | 180 | for (c = 0, i = 0; i < 8; i++) | 
|  | 181 | c |= (!!(byte & (1 << i))) << (7 - i); | 
|  | 182 | return c; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | /** | 
|  | 186 | * sht15_crc8() - compute crc8 | 
|  | 187 | * @data:	sht15 specific data. | 
|  | 188 | * @value:	sht15 retrieved data. | 
|  | 189 | * | 
|  | 190 | * This implements section 2 of the CRC datasheet. | 
|  | 191 | */ | 
|  | 192 | static u8 sht15_crc8(struct sht15_data *data, | 
|  | 193 | const u8 *value, | 
|  | 194 | int len) | 
|  | 195 | { | 
|  | 196 | u8 crc = sht15_reverse(data->val_status & 0x0F); | 
|  | 197 |  | 
|  | 198 | while (len--) { | 
|  | 199 | crc = sht15_crc8_table[*value ^ crc]; | 
|  | 200 | value++; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | return crc; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | /** | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 207 | * sht15_connection_reset() - reset the comms interface | 
|  | 208 | * @data:	sht15 specific data | 
|  | 209 | * | 
|  | 210 | * This implements section 3.4 of the data sheet | 
|  | 211 | */ | 
|  | 212 | static void sht15_connection_reset(struct sht15_data *data) | 
|  | 213 | { | 
|  | 214 | int i; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 215 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 216 | gpio_direction_output(data->pdata->gpio_data, 1); | 
|  | 217 | ndelay(SHT15_TSCKL); | 
|  | 218 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 219 | ndelay(SHT15_TSCKL); | 
|  | 220 | for (i = 0; i < 9; ++i) { | 
|  | 221 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 222 | ndelay(SHT15_TSCKH); | 
|  | 223 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 224 | ndelay(SHT15_TSCKL); | 
|  | 225 | } | 
|  | 226 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 227 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 228 | /** | 
|  | 229 | * sht15_send_bit() - send an individual bit to the device | 
|  | 230 | * @data:	device state data | 
|  | 231 | * @val:	value of bit to be sent | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 232 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 233 | static inline void sht15_send_bit(struct sht15_data *data, int val) | 
|  | 234 | { | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 235 | gpio_set_value(data->pdata->gpio_data, val); | 
|  | 236 | ndelay(SHT15_TSU); | 
|  | 237 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 238 | ndelay(SHT15_TSCKH); | 
|  | 239 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 240 | ndelay(SHT15_TSCKL); /* clock low time */ | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | /** | 
|  | 244 | * sht15_transmission_start() - specific sequence for new transmission | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 245 | * @data:	device state data | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 246 | * | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 247 | * Timings for this are not documented on the data sheet, so very | 
|  | 248 | * conservative ones used in implementation. This implements | 
|  | 249 | * figure 12 on the data sheet. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 250 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 251 | static void sht15_transmission_start(struct sht15_data *data) | 
|  | 252 | { | 
|  | 253 | /* ensure data is high and output */ | 
|  | 254 | gpio_direction_output(data->pdata->gpio_data, 1); | 
|  | 255 | ndelay(SHT15_TSU); | 
|  | 256 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 257 | ndelay(SHT15_TSCKL); | 
|  | 258 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 259 | ndelay(SHT15_TSCKH); | 
|  | 260 | gpio_set_value(data->pdata->gpio_data, 0); | 
|  | 261 | ndelay(SHT15_TSU); | 
|  | 262 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 263 | ndelay(SHT15_TSCKL); | 
|  | 264 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 265 | ndelay(SHT15_TSCKH); | 
|  | 266 | gpio_set_value(data->pdata->gpio_data, 1); | 
|  | 267 | ndelay(SHT15_TSU); | 
|  | 268 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 269 | ndelay(SHT15_TSCKL); | 
|  | 270 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 271 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 272 | /** | 
|  | 273 | * sht15_send_byte() - send a single byte to the device | 
|  | 274 | * @data:	device state | 
|  | 275 | * @byte:	value to be sent | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 276 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 277 | static void sht15_send_byte(struct sht15_data *data, u8 byte) | 
|  | 278 | { | 
|  | 279 | int i; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 280 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 281 | for (i = 0; i < 8; i++) { | 
|  | 282 | sht15_send_bit(data, !!(byte & 0x80)); | 
|  | 283 | byte <<= 1; | 
|  | 284 | } | 
|  | 285 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 286 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 287 | /** | 
|  | 288 | * sht15_wait_for_response() - checks for ack from device | 
|  | 289 | * @data:	device state | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 290 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 291 | static int sht15_wait_for_response(struct sht15_data *data) | 
|  | 292 | { | 
|  | 293 | gpio_direction_input(data->pdata->gpio_data); | 
|  | 294 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 295 | ndelay(SHT15_TSCKH); | 
|  | 296 | if (gpio_get_value(data->pdata->gpio_data)) { | 
|  | 297 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 298 | dev_err(data->dev, "Command not acknowledged\n"); | 
|  | 299 | sht15_connection_reset(data); | 
|  | 300 | return -EIO; | 
|  | 301 | } | 
|  | 302 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 303 | ndelay(SHT15_TSCKL); | 
|  | 304 | return 0; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | /** | 
|  | 308 | * sht15_send_cmd() - Sends a command to the device. | 
|  | 309 | * @data:	device state | 
|  | 310 | * @cmd:	command byte to be sent | 
|  | 311 | * | 
|  | 312 | * On entry, sck is output low, data is output pull high | 
|  | 313 | * and the interrupt disabled. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 314 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 315 | static int sht15_send_cmd(struct sht15_data *data, u8 cmd) | 
|  | 316 | { | 
|  | 317 | int ret = 0; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 318 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 319 | sht15_transmission_start(data); | 
|  | 320 | sht15_send_byte(data, cmd); | 
|  | 321 | ret = sht15_wait_for_response(data); | 
|  | 322 | return ret; | 
|  | 323 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 324 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 325 | /** | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 326 | * sht15_soft_reset() - send a soft reset command | 
|  | 327 | * @data:	sht15 specific data. | 
|  | 328 | * | 
|  | 329 | * As described in section 3.2 of the datasheet. | 
|  | 330 | */ | 
|  | 331 | static int sht15_soft_reset(struct sht15_data *data) | 
|  | 332 | { | 
|  | 333 | int ret; | 
|  | 334 |  | 
|  | 335 | ret = sht15_send_cmd(data, SHT15_SOFT_RESET); | 
|  | 336 | if (ret) | 
|  | 337 | return ret; | 
|  | 338 | msleep(SHT15_TSRST); | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 339 | /* device resets default hardware status register value */ | 
|  | 340 | data->val_status = 0; | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 341 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 342 | return ret; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | /** | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 346 | * sht15_ack() - send a ack | 
|  | 347 | * @data:	sht15 specific data. | 
|  | 348 | * | 
|  | 349 | * Each byte of data is acknowledged by pulling the data line | 
|  | 350 | * low for one clock pulse. | 
|  | 351 | */ | 
|  | 352 | static void sht15_ack(struct sht15_data *data) | 
|  | 353 | { | 
|  | 354 | gpio_direction_output(data->pdata->gpio_data, 0); | 
|  | 355 | ndelay(SHT15_TSU); | 
|  | 356 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 357 | ndelay(SHT15_TSU); | 
|  | 358 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 359 | ndelay(SHT15_TSU); | 
|  | 360 | gpio_set_value(data->pdata->gpio_data, 1); | 
|  | 361 |  | 
|  | 362 | gpio_direction_input(data->pdata->gpio_data); | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | /** | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 366 | * sht15_end_transmission() - notify device of end of transmission | 
|  | 367 | * @data:	device state. | 
|  | 368 | * | 
|  | 369 | * This is basically a NAK (single clock pulse, data high). | 
|  | 370 | */ | 
|  | 371 | static void sht15_end_transmission(struct sht15_data *data) | 
|  | 372 | { | 
|  | 373 | gpio_direction_output(data->pdata->gpio_data, 1); | 
|  | 374 | ndelay(SHT15_TSU); | 
|  | 375 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 376 | ndelay(SHT15_TSCKH); | 
|  | 377 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 378 | ndelay(SHT15_TSCKL); | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | /** | 
|  | 382 | * sht15_read_byte() - Read a byte back from the device | 
|  | 383 | * @data:	device state. | 
|  | 384 | */ | 
|  | 385 | static u8 sht15_read_byte(struct sht15_data *data) | 
|  | 386 | { | 
|  | 387 | int i; | 
|  | 388 | u8 byte = 0; | 
|  | 389 |  | 
|  | 390 | for (i = 0; i < 8; ++i) { | 
|  | 391 | byte <<= 1; | 
|  | 392 | gpio_set_value(data->pdata->gpio_sck, 1); | 
|  | 393 | ndelay(SHT15_TSCKH); | 
|  | 394 | byte |= !!gpio_get_value(data->pdata->gpio_data); | 
|  | 395 | gpio_set_value(data->pdata->gpio_sck, 0); | 
|  | 396 | ndelay(SHT15_TSCKL); | 
|  | 397 | } | 
|  | 398 | return byte; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | /** | 
|  | 402 | * sht15_send_status() - write the status register byte | 
|  | 403 | * @data:	sht15 specific data. | 
|  | 404 | * @status:	the byte to set the status register with. | 
|  | 405 | * | 
|  | 406 | * As described in figure 14 and table 5 of the datasheet. | 
|  | 407 | */ | 
|  | 408 | static int sht15_send_status(struct sht15_data *data, u8 status) | 
|  | 409 | { | 
|  | 410 | int ret; | 
|  | 411 |  | 
|  | 412 | ret = sht15_send_cmd(data, SHT15_WRITE_STATUS); | 
|  | 413 | if (ret) | 
|  | 414 | return ret; | 
|  | 415 | gpio_direction_output(data->pdata->gpio_data, 1); | 
|  | 416 | ndelay(SHT15_TSU); | 
|  | 417 | sht15_send_byte(data, status); | 
|  | 418 | ret = sht15_wait_for_response(data); | 
|  | 419 | if (ret) | 
|  | 420 | return ret; | 
|  | 421 |  | 
|  | 422 | data->val_status = status; | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 423 | return 0; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | /** | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 427 | * sht15_update_status() - get updated status register from device if too old | 
|  | 428 | * @data:	device instance specific data. | 
|  | 429 | * | 
|  | 430 | * As described in figure 15 and table 5 of the datasheet. | 
|  | 431 | */ | 
|  | 432 | static int sht15_update_status(struct sht15_data *data) | 
|  | 433 | { | 
|  | 434 | int ret = 0; | 
|  | 435 | u8 status; | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 436 | u8 previous_config; | 
|  | 437 | u8 dev_checksum = 0; | 
|  | 438 | u8 checksum_vals[2]; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 439 | int timeout = HZ; | 
|  | 440 |  | 
|  | 441 | mutex_lock(&data->read_lock); | 
|  | 442 | if (time_after(jiffies, data->last_status + timeout) | 
|  | 443 | || !data->status_valid) { | 
|  | 444 | ret = sht15_send_cmd(data, SHT15_READ_STATUS); | 
|  | 445 | if (ret) | 
|  | 446 | goto error_ret; | 
|  | 447 | status = sht15_read_byte(data); | 
|  | 448 |  | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 449 | if (data->checksumming) { | 
|  | 450 | sht15_ack(data); | 
|  | 451 | dev_checksum = sht15_reverse(sht15_read_byte(data)); | 
|  | 452 | checksum_vals[0] = SHT15_READ_STATUS; | 
|  | 453 | checksum_vals[1] = status; | 
|  | 454 | data->checksum_ok = (sht15_crc8(data, checksum_vals, 2) | 
|  | 455 | == dev_checksum); | 
|  | 456 | } | 
|  | 457 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 458 | sht15_end_transmission(data); | 
|  | 459 |  | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 460 | /* | 
|  | 461 | * Perform checksum validation on the received data. | 
|  | 462 | * Specification mentions that in case a checksum verification | 
|  | 463 | * fails, a soft reset command must be sent to the device. | 
|  | 464 | */ | 
|  | 465 | if (data->checksumming && !data->checksum_ok) { | 
|  | 466 | previous_config = data->val_status & 0x07; | 
|  | 467 | ret = sht15_soft_reset(data); | 
|  | 468 | if (ret) | 
|  | 469 | goto error_ret; | 
|  | 470 | if (previous_config) { | 
|  | 471 | ret = sht15_send_status(data, previous_config); | 
|  | 472 | if (ret) { | 
|  | 473 | dev_err(data->dev, | 
|  | 474 | "CRC validation failed, unable " | 
|  | 475 | "to restore device settings\n"); | 
|  | 476 | goto error_ret; | 
|  | 477 | } | 
|  | 478 | } | 
|  | 479 | ret = -EAGAIN; | 
|  | 480 | goto error_ret; | 
|  | 481 | } | 
|  | 482 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 483 | data->val_status = status; | 
|  | 484 | data->status_valid = true; | 
|  | 485 | data->last_status = jiffies; | 
|  | 486 | } | 
|  | 487 | error_ret: | 
|  | 488 | mutex_unlock(&data->read_lock); | 
|  | 489 |  | 
|  | 490 | return ret; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | /** | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 494 | * sht15_measurement() - get a new value from device | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 495 | * @data:		device instance specific data | 
|  | 496 | * @command:		command sent to request value | 
|  | 497 | * @timeout_msecs:	timeout after which comms are assumed | 
|  | 498 | *			to have failed are reset. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 499 | */ | 
|  | 500 | static int sht15_measurement(struct sht15_data *data, | 
|  | 501 | int command, | 
|  | 502 | int timeout_msecs) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 503 | { | 
|  | 504 | int ret; | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 505 | u8 previous_config; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 506 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 507 | ret = sht15_send_cmd(data, command); | 
|  | 508 | if (ret) | 
|  | 509 | return ret; | 
|  | 510 |  | 
|  | 511 | gpio_direction_input(data->pdata->gpio_data); | 
|  | 512 | atomic_set(&data->interrupt_handled, 0); | 
|  | 513 |  | 
|  | 514 | enable_irq(gpio_to_irq(data->pdata->gpio_data)); | 
|  | 515 | if (gpio_get_value(data->pdata->gpio_data) == 0) { | 
|  | 516 | disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data)); | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 517 | /* Only relevant if the interrupt hasn't occurred. */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 518 | if (!atomic_read(&data->interrupt_handled)) | 
|  | 519 | schedule_work(&data->read_work); | 
|  | 520 | } | 
|  | 521 | ret = wait_event_timeout(data->wait_queue, | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 522 | (data->state == SHT15_READING_NOTHING), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 523 | msecs_to_jiffies(timeout_msecs)); | 
|  | 524 | if (ret == 0) {/* timeout occurred */ | 
| Joe Perches | 24205e0 | 2009-07-11 13:42:37 +0200 | [diff] [blame] | 525 | disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data)); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 526 | sht15_connection_reset(data); | 
|  | 527 | return -ETIME; | 
|  | 528 | } | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 529 |  | 
|  | 530 | /* | 
|  | 531 | *  Perform checksum validation on the received data. | 
|  | 532 | *  Specification mentions that in case a checksum verification fails, | 
|  | 533 | *  a soft reset command must be sent to the device. | 
|  | 534 | */ | 
|  | 535 | if (data->checksumming && !data->checksum_ok) { | 
|  | 536 | previous_config = data->val_status & 0x07; | 
|  | 537 | ret = sht15_soft_reset(data); | 
|  | 538 | if (ret) | 
|  | 539 | return ret; | 
|  | 540 | if (previous_config) { | 
|  | 541 | ret = sht15_send_status(data, previous_config); | 
|  | 542 | if (ret) { | 
|  | 543 | dev_err(data->dev, | 
|  | 544 | "CRC validation failed, unable " | 
|  | 545 | "to restore device settings\n"); | 
|  | 546 | return ret; | 
|  | 547 | } | 
|  | 548 | } | 
|  | 549 | return -EAGAIN; | 
|  | 550 | } | 
|  | 551 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 552 | return 0; | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | /** | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 556 | * sht15_update_measurements() - get updated measures from device if too old | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 557 | * @data:	device state | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 558 | */ | 
|  | 559 | static int sht15_update_measurements(struct sht15_data *data) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 560 | { | 
|  | 561 | int ret = 0; | 
|  | 562 | int timeout = HZ; | 
|  | 563 |  | 
|  | 564 | mutex_lock(&data->read_lock); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 565 | if (time_after(jiffies, data->last_measurement + timeout) | 
|  | 566 | || !data->measurements_valid) { | 
|  | 567 | data->state = SHT15_READING_HUMID; | 
|  | 568 | ret = sht15_measurement(data, SHT15_MEASURE_RH, 160); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 569 | if (ret) | 
|  | 570 | goto error_ret; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 571 | data->state = SHT15_READING_TEMP; | 
|  | 572 | ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 573 | if (ret) | 
|  | 574 | goto error_ret; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 575 | data->measurements_valid = true; | 
|  | 576 | data->last_measurement = jiffies; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 577 | } | 
|  | 578 | error_ret: | 
|  | 579 | mutex_unlock(&data->read_lock); | 
|  | 580 |  | 
|  | 581 | return ret; | 
|  | 582 | } | 
|  | 583 |  | 
|  | 584 | /** | 
|  | 585 | * sht15_calc_temp() - convert the raw reading to a temperature | 
|  | 586 | * @data:	device state | 
|  | 587 | * | 
|  | 588 | * As per section 4.3 of the data sheet. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 589 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 590 | static inline int sht15_calc_temp(struct sht15_data *data) | 
|  | 591 | { | 
| Jerome Oufella | 328a2c2 | 2010-04-14 16:14:07 +0200 | [diff] [blame] | 592 | int d1 = temppoints[0].d1; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 593 | int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 594 | int i; | 
|  | 595 |  | 
| Jerome Oufella | 328a2c2 | 2010-04-14 16:14:07 +0200 | [diff] [blame] | 596 | for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 597 | /* Find pointer to interpolate */ | 
|  | 598 | if (data->supply_uV > temppoints[i - 1].vdd) { | 
| Jerome Oufella | 328a2c2 | 2010-04-14 16:14:07 +0200 | [diff] [blame] | 599 | d1 = (data->supply_uV - temppoints[i - 1].vdd) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 600 | * (temppoints[i].d1 - temppoints[i - 1].d1) | 
|  | 601 | / (temppoints[i].vdd - temppoints[i - 1].vdd) | 
|  | 602 | + temppoints[i - 1].d1; | 
|  | 603 | break; | 
|  | 604 | } | 
|  | 605 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 606 | return data->val_temp * d2 + d1; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 607 | } | 
|  | 608 |  | 
|  | 609 | /** | 
|  | 610 | * sht15_calc_humid() - using last temperature convert raw to humid | 
|  | 611 | * @data:	device state | 
|  | 612 | * | 
|  | 613 | * This is the temperature compensated version as per section 4.2 of | 
|  | 614 | * the data sheet. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 615 | * | 
|  | 616 | * The sensor is assumed to be V3, which is compatible with V4. | 
|  | 617 | * Humidity conversion coefficients are shown in table 7 of the datasheet. | 
|  | 618 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 619 | static inline int sht15_calc_humid(struct sht15_data *data) | 
|  | 620 | { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 621 | int rh_linear; /* milli percent */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 622 | int temp = sht15_calc_temp(data); | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 623 | int c2, c3; | 
|  | 624 | int t2; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 625 | const int c1 = -4; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 626 |  | 
|  | 627 | if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) { | 
|  | 628 | c2 = 648000; /* x 10 ^ -6 */ | 
|  | 629 | c3 = -7200;  /* x 10 ^ -7 */ | 
|  | 630 | t2 = 1280; | 
|  | 631 | } else { | 
|  | 632 | c2 = 40500;  /* x 10 ^ -6 */ | 
|  | 633 | c3 = -28;    /* x 10 ^ -7 */ | 
|  | 634 | t2 = 80; | 
|  | 635 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 636 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 637 | rh_linear = c1 * 1000 | 
|  | 638 | + c2 * data->val_humid / 1000 | 
| Vivien Didelot | ccd32e7 | 2011-03-21 17:59:35 +0100 | [diff] [blame] | 639 | + (data->val_humid * data->val_humid * c3) / 10000; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 640 | return (temp - 25000) * (10000 + t2 * data->val_humid) | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 641 | / 1000000 + rh_linear; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 642 | } | 
|  | 643 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 644 | /** | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 645 | * sht15_show_status() - show status information in sysfs | 
|  | 646 | * @dev:	device. | 
|  | 647 | * @attr:	device attribute. | 
|  | 648 | * @buf:	sysfs buffer where information is written to. | 
|  | 649 | * | 
|  | 650 | * Will be called on read access to temp1_fault, humidity1_fault | 
|  | 651 | * and heater_enable sysfs attributes. | 
|  | 652 | * Returns number of bytes written into buffer, negative errno on error. | 
|  | 653 | */ | 
|  | 654 | static ssize_t sht15_show_status(struct device *dev, | 
|  | 655 | struct device_attribute *attr, | 
|  | 656 | char *buf) | 
|  | 657 | { | 
|  | 658 | int ret; | 
|  | 659 | struct sht15_data *data = dev_get_drvdata(dev); | 
|  | 660 | u8 bit = to_sensor_dev_attr(attr)->index; | 
|  | 661 |  | 
|  | 662 | ret = sht15_update_status(data); | 
|  | 663 |  | 
|  | 664 | return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit)); | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | /** | 
|  | 668 | * sht15_store_heater() - change heater state via sysfs | 
|  | 669 | * @dev:	device. | 
|  | 670 | * @attr:	device attribute. | 
|  | 671 | * @buf:	sysfs buffer to read the new heater state from. | 
|  | 672 | * @count:	length of the data. | 
|  | 673 | * | 
| Vivien Didelot | e9b6e9f | 2011-07-25 21:46:10 +0200 | [diff] [blame] | 674 | * Will be called on write access to heater_enable sysfs attribute. | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 675 | * Returns number of bytes actually decoded, negative errno on error. | 
|  | 676 | */ | 
|  | 677 | static ssize_t sht15_store_heater(struct device *dev, | 
|  | 678 | struct device_attribute *attr, | 
|  | 679 | const char *buf, size_t count) | 
|  | 680 | { | 
|  | 681 | int ret; | 
|  | 682 | struct sht15_data *data = dev_get_drvdata(dev); | 
|  | 683 | long value; | 
|  | 684 | u8 status; | 
|  | 685 |  | 
|  | 686 | if (strict_strtol(buf, 10, &value)) | 
|  | 687 | return -EINVAL; | 
|  | 688 |  | 
|  | 689 | mutex_lock(&data->read_lock); | 
|  | 690 | status = data->val_status & 0x07; | 
|  | 691 | if (!!value) | 
|  | 692 | status |= SHT15_STATUS_HEATER; | 
|  | 693 | else | 
|  | 694 | status &= ~SHT15_STATUS_HEATER; | 
|  | 695 |  | 
|  | 696 | ret = sht15_send_status(data, status); | 
|  | 697 | mutex_unlock(&data->read_lock); | 
|  | 698 |  | 
|  | 699 | return ret ? ret : count; | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | /** | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 703 | * sht15_show_temp() - show temperature measurement value in sysfs | 
|  | 704 | * @dev:	device. | 
|  | 705 | * @attr:	device attribute. | 
|  | 706 | * @buf:	sysfs buffer where measurement values are written to. | 
|  | 707 | * | 
|  | 708 | * Will be called on read access to temp1_input sysfs attribute. | 
|  | 709 | * Returns number of bytes written into buffer, negative errno on error. | 
|  | 710 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 711 | static ssize_t sht15_show_temp(struct device *dev, | 
|  | 712 | struct device_attribute *attr, | 
|  | 713 | char *buf) | 
|  | 714 | { | 
|  | 715 | int ret; | 
|  | 716 | struct sht15_data *data = dev_get_drvdata(dev); | 
|  | 717 |  | 
|  | 718 | /* Technically no need to read humidity as well */ | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 719 | ret = sht15_update_measurements(data); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 720 |  | 
|  | 721 | return ret ? ret : sprintf(buf, "%d\n", | 
|  | 722 | sht15_calc_temp(data)); | 
|  | 723 | } | 
|  | 724 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 725 | /** | 
|  | 726 | * sht15_show_humidity() - show humidity measurement value in sysfs | 
|  | 727 | * @dev:	device. | 
|  | 728 | * @attr:	device attribute. | 
|  | 729 | * @buf:	sysfs buffer where measurement values are written to. | 
|  | 730 | * | 
|  | 731 | * Will be called on read access to humidity1_input sysfs attribute. | 
|  | 732 | * Returns number of bytes written into buffer, negative errno on error. | 
|  | 733 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 734 | static ssize_t sht15_show_humidity(struct device *dev, | 
|  | 735 | struct device_attribute *attr, | 
|  | 736 | char *buf) | 
|  | 737 | { | 
|  | 738 | int ret; | 
|  | 739 | struct sht15_data *data = dev_get_drvdata(dev); | 
|  | 740 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 741 | ret = sht15_update_measurements(data); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 742 |  | 
|  | 743 | return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data)); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 744 | } | 
|  | 745 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 746 | static ssize_t show_name(struct device *dev, | 
|  | 747 | struct device_attribute *attr, | 
|  | 748 | char *buf) | 
|  | 749 | { | 
|  | 750 | struct platform_device *pdev = to_platform_device(dev); | 
|  | 751 | return sprintf(buf, "%s\n", pdev->name); | 
|  | 752 | } | 
|  | 753 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 754 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, | 
|  | 755 | sht15_show_temp, NULL, 0); | 
|  | 756 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, | 
|  | 757 | sht15_show_humidity, NULL, 0); | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 758 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL, | 
|  | 759 | SHT15_STATUS_LOW_BATTERY); | 
|  | 760 | static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL, | 
|  | 761 | SHT15_STATUS_LOW_BATTERY); | 
|  | 762 | static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status, | 
|  | 763 | sht15_store_heater, SHT15_STATUS_HEATER); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 764 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | 
|  | 765 | static struct attribute *sht15_attrs[] = { | 
|  | 766 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 767 | &sensor_dev_attr_humidity1_input.dev_attr.attr, | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 768 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | 
|  | 769 | &sensor_dev_attr_humidity1_fault.dev_attr.attr, | 
|  | 770 | &sensor_dev_attr_heater_enable.dev_attr.attr, | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 771 | &dev_attr_name.attr, | 
|  | 772 | NULL, | 
|  | 773 | }; | 
|  | 774 |  | 
|  | 775 | static const struct attribute_group sht15_attr_group = { | 
|  | 776 | .attrs = sht15_attrs, | 
|  | 777 | }; | 
|  | 778 |  | 
|  | 779 | static irqreturn_t sht15_interrupt_fired(int irq, void *d) | 
|  | 780 | { | 
|  | 781 | struct sht15_data *data = d; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 782 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 783 | /* First disable the interrupt */ | 
|  | 784 | disable_irq_nosync(irq); | 
|  | 785 | atomic_inc(&data->interrupt_handled); | 
|  | 786 | /* Then schedule a reading work struct */ | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 787 | if (data->state != SHT15_READING_NOTHING) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 788 | schedule_work(&data->read_work); | 
|  | 789 | return IRQ_HANDLED; | 
|  | 790 | } | 
|  | 791 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 792 | static void sht15_bh_read_data(struct work_struct *work_s) | 
|  | 793 | { | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 794 | uint16_t val = 0; | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 795 | u8 dev_checksum = 0; | 
|  | 796 | u8 checksum_vals[3]; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 797 | struct sht15_data *data | 
|  | 798 | = container_of(work_s, struct sht15_data, | 
|  | 799 | read_work); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 800 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 801 | /* Firstly, verify the line is low */ | 
|  | 802 | if (gpio_get_value(data->pdata->gpio_data)) { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 803 | /* | 
|  | 804 | * If not, then start the interrupt again - care here as could | 
|  | 805 | * have gone low in meantime so verify it hasn't! | 
|  | 806 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 807 | atomic_set(&data->interrupt_handled, 0); | 
|  | 808 | enable_irq(gpio_to_irq(data->pdata->gpio_data)); | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 809 | /* If still not occurred or another handler has been scheduled */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 810 | if (gpio_get_value(data->pdata->gpio_data) | 
|  | 811 | || atomic_read(&data->interrupt_handled)) | 
|  | 812 | return; | 
|  | 813 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 814 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 815 | /* Read the data back from the device */ | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 816 | val = sht15_read_byte(data); | 
|  | 817 | val <<= 8; | 
|  | 818 | sht15_ack(data); | 
|  | 819 | val |= sht15_read_byte(data); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 820 |  | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 821 | if (data->checksumming) { | 
|  | 822 | /* | 
|  | 823 | * Ask the device for a checksum and read it back. | 
|  | 824 | * Note: the device sends the checksum byte reversed. | 
|  | 825 | */ | 
|  | 826 | sht15_ack(data); | 
|  | 827 | dev_checksum = sht15_reverse(sht15_read_byte(data)); | 
|  | 828 | checksum_vals[0] = (data->state == SHT15_READING_TEMP) ? | 
|  | 829 | SHT15_MEASURE_TEMP : SHT15_MEASURE_RH; | 
|  | 830 | checksum_vals[1] = (u8) (val >> 8); | 
|  | 831 | checksum_vals[2] = (u8) val; | 
|  | 832 | data->checksum_ok | 
|  | 833 | = (sht15_crc8(data, checksum_vals, 3) == dev_checksum); | 
|  | 834 | } | 
|  | 835 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 836 | /* Tell the device we are done */ | 
|  | 837 | sht15_end_transmission(data); | 
|  | 838 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 839 | switch (data->state) { | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 840 | case SHT15_READING_TEMP: | 
|  | 841 | data->val_temp = val; | 
|  | 842 | break; | 
|  | 843 | case SHT15_READING_HUMID: | 
|  | 844 | data->val_humid = val; | 
|  | 845 | break; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 846 | default: | 
|  | 847 | break; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 848 | } | 
|  | 849 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 850 | data->state = SHT15_READING_NOTHING; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 851 | wake_up(&data->wait_queue); | 
|  | 852 | } | 
|  | 853 |  | 
|  | 854 | static void sht15_update_voltage(struct work_struct *work_s) | 
|  | 855 | { | 
|  | 856 | struct sht15_data *data | 
|  | 857 | = container_of(work_s, struct sht15_data, | 
|  | 858 | update_supply_work); | 
|  | 859 | data->supply_uV = regulator_get_voltage(data->reg); | 
|  | 860 | } | 
|  | 861 |  | 
|  | 862 | /** | 
|  | 863 | * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg | 
|  | 864 | * @nb:		associated notification structure | 
|  | 865 | * @event:	voltage regulator state change event code | 
|  | 866 | * @ignored:	function parameter - ignored here | 
|  | 867 | * | 
|  | 868 | * Note that as the notification code holds the regulator lock, we have | 
|  | 869 | * to schedule an update of the supply voltage rather than getting it directly. | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 870 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 871 | static int sht15_invalidate_voltage(struct notifier_block *nb, | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 872 | unsigned long event, | 
|  | 873 | void *ignored) | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 874 | { | 
|  | 875 | struct sht15_data *data = container_of(nb, struct sht15_data, nb); | 
|  | 876 |  | 
|  | 877 | if (event == REGULATOR_EVENT_VOLTAGE_CHANGE) | 
|  | 878 | data->supply_uV_valid = false; | 
|  | 879 | schedule_work(&data->update_supply_work); | 
|  | 880 |  | 
|  | 881 | return NOTIFY_OK; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | static int __devinit sht15_probe(struct platform_device *pdev) | 
|  | 885 | { | 
|  | 886 | int ret = 0; | 
|  | 887 | struct sht15_data *data = kzalloc(sizeof(*data), GFP_KERNEL); | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 888 | u8 status = 0; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 889 |  | 
|  | 890 | if (!data) { | 
|  | 891 | ret = -ENOMEM; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 892 | dev_err(&pdev->dev, "kzalloc failed\n"); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 893 | goto error_ret; | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | INIT_WORK(&data->read_work, sht15_bh_read_data); | 
|  | 897 | INIT_WORK(&data->update_supply_work, sht15_update_voltage); | 
|  | 898 | platform_set_drvdata(pdev, data); | 
|  | 899 | mutex_init(&data->read_lock); | 
|  | 900 | data->dev = &pdev->dev; | 
|  | 901 | init_waitqueue_head(&data->wait_queue); | 
|  | 902 |  | 
|  | 903 | if (pdev->dev.platform_data == NULL) { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 904 | dev_err(&pdev->dev, "no platform data supplied\n"); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 905 | goto err_free_data; | 
|  | 906 | } | 
|  | 907 | data->pdata = pdev->dev.platform_data; | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 908 | data->supply_uV = data->pdata->supply_mv * 1000; | 
| Jerome Oufella | 82c7465 | 2011-04-12 15:34:39 -0400 | [diff] [blame] | 909 | if (data->pdata->checksum) | 
|  | 910 | data->checksumming = true; | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 911 | if (data->pdata->no_otp_reload) | 
|  | 912 | status |= SHT15_STATUS_NO_OTP_RELOAD; | 
|  | 913 | if (data->pdata->low_resolution) | 
|  | 914 | status |= SHT15_STATUS_LOW_RESOLUTION; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 915 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 916 | /* | 
|  | 917 | * If a regulator is available, | 
|  | 918 | * query what the supply voltage actually is! | 
|  | 919 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 920 | data->reg = regulator_get(data->dev, "vcc"); | 
|  | 921 | if (!IS_ERR(data->reg)) { | 
| Jean Delvare | c7a78d2 | 2010-04-14 16:14:08 +0200 | [diff] [blame] | 922 | int voltage; | 
|  | 923 |  | 
|  | 924 | voltage = regulator_get_voltage(data->reg); | 
|  | 925 | if (voltage) | 
|  | 926 | data->supply_uV = voltage; | 
|  | 927 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 928 | regulator_enable(data->reg); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 929 | /* | 
|  | 930 | * Setup a notifier block to update this if another device | 
|  | 931 | * causes the voltage to change | 
|  | 932 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 933 | data->nb.notifier_call = &sht15_invalidate_voltage; | 
|  | 934 | ret = regulator_register_notifier(data->reg, &data->nb); | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 935 | if (ret) { | 
|  | 936 | dev_err(&pdev->dev, | 
|  | 937 | "regulator notifier request failed\n"); | 
|  | 938 | regulator_disable(data->reg); | 
|  | 939 | regulator_put(data->reg); | 
|  | 940 | goto err_free_data; | 
|  | 941 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 942 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 943 |  | 
|  | 944 | /* Try requesting the GPIOs */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 945 | ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck"); | 
|  | 946 | if (ret) { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 947 | dev_err(&pdev->dev, "gpio request failed\n"); | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 948 | goto err_release_reg; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 949 | } | 
|  | 950 | gpio_direction_output(data->pdata->gpio_sck, 0); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 951 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 952 | ret = gpio_request(data->pdata->gpio_data, "SHT15 data"); | 
|  | 953 | if (ret) { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 954 | dev_err(&pdev->dev, "gpio request failed\n"); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 955 | goto err_release_gpio_sck; | 
|  | 956 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 957 |  | 
|  | 958 | ret = request_irq(gpio_to_irq(data->pdata->gpio_data), | 
|  | 959 | sht15_interrupt_fired, | 
|  | 960 | IRQF_TRIGGER_FALLING, | 
|  | 961 | "sht15 data", | 
|  | 962 | data); | 
|  | 963 | if (ret) { | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 964 | dev_err(&pdev->dev, "failed to get irq for data line\n"); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 965 | goto err_release_gpio_data; | 
|  | 966 | } | 
|  | 967 | disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data)); | 
|  | 968 | sht15_connection_reset(data); | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 969 | ret = sht15_soft_reset(data); | 
|  | 970 | if (ret) | 
|  | 971 | goto err_release_irq; | 
|  | 972 |  | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 973 | /* write status with platform data options */ | 
|  | 974 | if (status) { | 
|  | 975 | ret = sht15_send_status(data, status); | 
|  | 976 | if (ret) | 
|  | 977 | goto err_release_irq; | 
|  | 978 | } | 
|  | 979 |  | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 980 | ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group); | 
|  | 981 | if (ret) { | 
|  | 982 | dev_err(&pdev->dev, "sysfs create failed\n"); | 
|  | 983 | goto err_release_irq; | 
|  | 984 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 985 |  | 
|  | 986 | data->hwmon_dev = hwmon_device_register(data->dev); | 
|  | 987 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 988 | ret = PTR_ERR(data->hwmon_dev); | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 989 | goto err_release_sysfs_group; | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 990 | } | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 991 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 992 | return 0; | 
|  | 993 |  | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 994 | err_release_sysfs_group: | 
|  | 995 | sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); | 
| Roel Kluin | 560a64a | 2009-09-21 17:04:48 -0700 | [diff] [blame] | 996 | err_release_irq: | 
|  | 997 | free_irq(gpio_to_irq(data->pdata->gpio_data), data); | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 998 | err_release_gpio_data: | 
|  | 999 | gpio_free(data->pdata->gpio_data); | 
|  | 1000 | err_release_gpio_sck: | 
|  | 1001 | gpio_free(data->pdata->gpio_sck); | 
| Vivien Didelot | 181148a | 2011-04-12 15:34:37 -0400 | [diff] [blame] | 1002 | err_release_reg: | 
|  | 1003 | if (!IS_ERR(data->reg)) { | 
|  | 1004 | regulator_unregister_notifier(data->reg, &data->nb); | 
|  | 1005 | regulator_disable(data->reg); | 
|  | 1006 | regulator_put(data->reg); | 
|  | 1007 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1008 | err_free_data: | 
|  | 1009 | kfree(data); | 
|  | 1010 | error_ret: | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1011 | return ret; | 
|  | 1012 | } | 
|  | 1013 |  | 
|  | 1014 | static int __devexit sht15_remove(struct platform_device *pdev) | 
|  | 1015 | { | 
|  | 1016 | struct sht15_data *data = platform_get_drvdata(pdev); | 
|  | 1017 |  | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 1018 | /* | 
|  | 1019 | * Make sure any reads from the device are done and | 
|  | 1020 | * prevent new ones beginning | 
|  | 1021 | */ | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1022 | mutex_lock(&data->read_lock); | 
| Vivien Didelot | cc15c7e | 2011-04-12 15:34:38 -0400 | [diff] [blame] | 1023 | if (sht15_soft_reset(data)) { | 
|  | 1024 | mutex_unlock(&data->read_lock); | 
|  | 1025 | return -EFAULT; | 
|  | 1026 | } | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1027 | hwmon_device_unregister(data->hwmon_dev); | 
|  | 1028 | sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); | 
|  | 1029 | if (!IS_ERR(data->reg)) { | 
|  | 1030 | regulator_unregister_notifier(data->reg, &data->nb); | 
|  | 1031 | regulator_disable(data->reg); | 
|  | 1032 | regulator_put(data->reg); | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | free_irq(gpio_to_irq(data->pdata->gpio_data), data); | 
|  | 1036 | gpio_free(data->pdata->gpio_data); | 
|  | 1037 | gpio_free(data->pdata->gpio_sck); | 
|  | 1038 | mutex_unlock(&data->read_lock); | 
|  | 1039 | kfree(data); | 
| Vivien Didelot | 99a0378 | 2011-04-12 15:34:36 -0400 | [diff] [blame] | 1040 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1041 | return 0; | 
|  | 1042 | } | 
|  | 1043 |  | 
| Rakib Mullick | cb0f1a1 | 2009-10-09 20:35:17 +0200 | [diff] [blame] | 1044 | /* | 
|  | 1045 | * sht_drivers simultaneously refers to __devinit and __devexit function | 
|  | 1046 | * which causes spurious section mismatch warning. So use __refdata to | 
|  | 1047 | * get rid from this. | 
|  | 1048 | */ | 
|  | 1049 | static struct platform_driver __refdata sht_drivers[] = { | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1050 | { | 
|  | 1051 | .driver = { | 
|  | 1052 | .name = "sht10", | 
|  | 1053 | .owner = THIS_MODULE, | 
|  | 1054 | }, | 
|  | 1055 | .probe = sht15_probe, | 
| Jean Delvare | cd659fd | 2009-06-15 18:39:45 +0200 | [diff] [blame] | 1056 | .remove = __devexit_p(sht15_remove), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1057 | }, { | 
|  | 1058 | .driver = { | 
|  | 1059 | .name = "sht11", | 
|  | 1060 | .owner = THIS_MODULE, | 
|  | 1061 | }, | 
|  | 1062 | .probe = sht15_probe, | 
| Jean Delvare | cd659fd | 2009-06-15 18:39:45 +0200 | [diff] [blame] | 1063 | .remove = __devexit_p(sht15_remove), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1064 | }, { | 
|  | 1065 | .driver = { | 
|  | 1066 | .name = "sht15", | 
|  | 1067 | .owner = THIS_MODULE, | 
|  | 1068 | }, | 
|  | 1069 | .probe = sht15_probe, | 
| Jean Delvare | cd659fd | 2009-06-15 18:39:45 +0200 | [diff] [blame] | 1070 | .remove = __devexit_p(sht15_remove), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1071 | }, { | 
|  | 1072 | .driver = { | 
|  | 1073 | .name = "sht71", | 
|  | 1074 | .owner = THIS_MODULE, | 
|  | 1075 | }, | 
|  | 1076 | .probe = sht15_probe, | 
| Jean Delvare | cd659fd | 2009-06-15 18:39:45 +0200 | [diff] [blame] | 1077 | .remove = __devexit_p(sht15_remove), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1078 | }, { | 
|  | 1079 | .driver = { | 
|  | 1080 | .name = "sht75", | 
|  | 1081 | .owner = THIS_MODULE, | 
|  | 1082 | }, | 
|  | 1083 | .probe = sht15_probe, | 
| Jean Delvare | cd659fd | 2009-06-15 18:39:45 +0200 | [diff] [blame] | 1084 | .remove = __devexit_p(sht15_remove), | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1085 | }, | 
|  | 1086 | }; | 
|  | 1087 |  | 
| Jonathan Cameron | 251eb40 | 2009-04-13 14:39:45 -0700 | [diff] [blame] | 1088 | static int __init sht15_init(void) | 
|  | 1089 | { | 
|  | 1090 | int ret; | 
|  | 1091 | int i; | 
|  | 1092 |  | 
|  | 1093 | for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) { | 
|  | 1094 | ret = platform_driver_register(&sht_drivers[i]); | 
|  | 1095 | if (ret) | 
|  | 1096 | goto error_unreg; | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | return 0; | 
|  | 1100 |  | 
|  | 1101 | error_unreg: | 
|  | 1102 | while (--i >= 0) | 
|  | 1103 | platform_driver_unregister(&sht_drivers[i]); | 
|  | 1104 |  | 
|  | 1105 | return ret; | 
|  | 1106 | } | 
|  | 1107 | module_init(sht15_init); | 
|  | 1108 |  | 
|  | 1109 | static void __exit sht15_exit(void) | 
|  | 1110 | { | 
|  | 1111 | int i; | 
|  | 1112 | for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--) | 
|  | 1113 | platform_driver_unregister(&sht_drivers[i]); | 
|  | 1114 | } | 
|  | 1115 | module_exit(sht15_exit); | 
|  | 1116 |  | 
|  | 1117 | MODULE_LICENSE("GPL"); |