22 #include <linux/module.h>
29 #include <linux/sched.h>
34 #include <linux/slab.h>
38 #define SHT15_MEASURE_TEMP 0x03
39 #define SHT15_MEASURE_RH 0x05
40 #define SHT15_WRITE_STATUS 0x06
41 #define SHT15_READ_STATUS 0x07
42 #define SHT15_SOFT_RESET 0x1E
45 #define SHT15_TSCKL 100
46 #define SHT15_TSCKH 100
48 #define SHT15_TSRST 11
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
86 static const u8 sht15_crc8_table[] = {
87 0, 49, 98, 83, 196, 245, 166, 151,
88 185, 136, 219, 234, 125, 76, 31, 46,
89 67, 114, 33, 16, 135, 182, 229, 212,
90 250, 203, 152, 169, 62, 15, 92, 109,
91 134, 183, 228, 213, 66, 115, 32, 17,
92 63, 14, 93, 108, 251, 202, 153, 168,
93 197, 244, 167, 150, 1, 48, 99, 82,
94 124, 77, 30, 47, 184, 137, 218, 235,
95 61, 12, 95, 110, 249, 200, 155, 170,
96 132, 181, 230, 215, 64, 113, 34, 19,
97 126, 79, 28, 45, 186, 139, 216, 233,
98 199, 246, 165, 148, 3, 50, 97, 80,
99 187, 138, 217, 232, 127, 78, 29, 44,
100 2, 51, 96, 81, 198, 247, 164, 149,
101 248, 201, 154, 171, 60, 13, 94, 111,
102 65, 112, 35, 18, 133, 180, 231, 214,
103 122, 75, 24, 41, 190, 143, 220, 237,
104 195, 242, 161, 144, 7, 54, 101, 84,
105 57, 8, 91, 106, 253, 204, 159, 174,
106 128, 177, 226, 211, 68, 117, 38, 23,
107 252, 205, 158, 175, 56, 9, 90, 107,
108 69, 116, 39, 22, 129, 176, 227, 210,
109 191, 142, 221, 236, 123, 74, 25, 40,
110 6, 55, 100, 85, 194, 243, 160, 145,
111 71, 118, 37, 20, 131, 178, 225, 208,
112 254, 207, 156, 173, 58, 11, 88, 105,
113 4, 53, 102, 87, 192, 241, 162, 147,
114 189, 140, 223, 238, 121, 72, 27, 42,
115 193, 240, 163, 146, 5, 52, 103, 86,
116 120, 73, 26, 43, 188, 141, 222, 239,
117 130, 179, 224, 209, 70, 119, 36, 21,
118 59, 10, 89, 104, 255, 206, 157, 172
183 for (c = 0, i = 0; i < 8; i++)
184 c |= (!!(byte & (1 << i))) << (7 -
i);
202 crc = sht15_crc8_table[*value ^
crc];
215 static void sht15_connection_reset(
struct sht15_data *data)
223 for (i = 0; i < 9; ++
i) {
236 static inline void sht15_send_bit(
struct sht15_data *data,
int val)
254 static void sht15_transmission_start(
struct sht15_data *data)
284 for (i = 0; i < 8; i++) {
285 sht15_send_bit(data, !!(byte & 0x80));
294 static int sht15_wait_for_response(
struct sht15_data *data)
301 dev_err(data->
dev,
"Command not acknowledged\n");
302 sht15_connection_reset(data);
322 sht15_transmission_start(data);
323 sht15_send_byte(data, cmd);
324 ret = sht15_wait_for_response(data);
334 static int sht15_soft_reset(
struct sht15_data *data)
355 static void sht15_ack(
struct sht15_data *data)
374 static void sht15_end_transmission(
struct sht15_data *data)
393 for (i = 0; i < 8; ++
i) {
420 sht15_send_byte(data, status);
421 ret = sht15_wait_for_response(data);
435 static int sht15_update_status(
struct sht15_data *data)
450 status = sht15_read_byte(data);
454 dev_checksum = sht15_reverse(sht15_read_byte(data));
456 checksum_vals[1] =
status;
457 data->
checksum_ok = (sht15_crc8(data, checksum_vals, 2)
461 sht15_end_transmission(data);
470 ret = sht15_soft_reset(data);
473 if (previous_config) {
474 ret = sht15_send_status(data, previous_config);
477 "CRC validation failed, unable "
478 "to restore device settings\n");
503 static int sht15_measurement(
struct sht15_data *data,
510 ret = sht15_send_cmd(data, command);
529 sht15_connection_reset(data);
540 ret = sht15_soft_reset(data);
543 if (previous_config) {
544 ret = sht15_send_status(data, previous_config);
547 "CRC validation failed, unable "
548 "to restore device settings\n");
562 static int sht15_update_measurements(
struct sht15_data *data)
593 static inline int sht15_calc_temp(
struct sht15_data *data)
595 int d1 = temppoints[0].
d1;
599 for (i =
ARRAY_SIZE(temppoints) - 1; i > 0; i--)
603 * (temppoints[i].d1 - temppoints[i - 1].d1)
604 / (temppoints[
i].
vdd - temppoints[i - 1].
vdd)
605 + temppoints[i - 1].d1;
622 static inline int sht15_calc_humid(
struct sht15_data *data)
625 int temp = sht15_calc_temp(data);
640 rh_linear = c1 * 1000
643 return (temp - 25000) * (10000 + t2 * data->
val_humid)
644 / 1000000 + rh_linear;
665 ret = sht15_update_status(data);
682 const char *buf,
size_t count)
689 if (kstrtol(buf, 10, &value))
699 ret = sht15_send_status(data, status);
702 return ret ? ret :
count;
722 ret = sht15_update_measurements(data);
724 return ret ? ret :
sprintf(buf,
"%d\n",
725 sht15_calc_temp(data));
744 ret = sht15_update_measurements(data);
746 return ret ? ret :
sprintf(buf,
"%d\n", sht15_calc_humid(data));
758 sht15_show_temp,
NULL, 0);
760 sht15_show_humidity,
NULL, 0);
768 static struct attribute *sht15_attrs[] = {
769 &sensor_dev_attr_temp1_input.dev_attr.attr,
770 &sensor_dev_attr_humidity1_input.dev_attr.attr,
771 &sensor_dev_attr_temp1_fault.dev_attr.attr,
772 &sensor_dev_attr_humidity1_fault.dev_attr.attr,
773 &sensor_dev_attr_heater_enable.dev_attr.attr,
779 .attrs = sht15_attrs,
782 static irqreturn_t sht15_interrupt_fired(
int irq,
void *
d)
795 static void sht15_bh_read_data(
struct work_struct *work_s)
819 val = sht15_read_byte(data);
822 val |= sht15_read_byte(data);
830 dev_checksum = sht15_reverse(sht15_read_byte(data));
833 checksum_vals[1] = (
u8) (val >> 8);
834 checksum_vals[2] = (
u8) val;
836 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
840 sht15_end_transmission(data);
842 switch (data->
state) {
857 static void sht15_update_voltage(
struct work_struct *work_s)
899 platform_set_drvdata(pdev, data);
904 if (pdev->
dev.platform_data ==
NULL) {
905 dev_err(&pdev->
dev,
"no platform data supplied\n");
908 data->
pdata = pdev->
dev.platform_data;
910 if (data->
pdata->checksum)
912 if (data->
pdata->no_otp_reload)
914 if (data->
pdata->low_resolution)
922 if (!IS_ERR(data->
reg)) {
934 data->
nb.notifier_call = &sht15_invalidate_voltage;
938 "regulator notifier request failed\n");
948 goto err_release_reg;
956 goto err_release_reg;
960 sht15_interrupt_fired,
965 dev_err(&pdev->
dev,
"failed to get irq for data line\n");
966 goto err_release_reg;
969 sht15_connection_reset(data);
970 ret = sht15_soft_reset(data);
972 goto err_release_reg;
976 ret = sht15_send_status(data, status);
978 goto err_release_reg;
984 goto err_release_reg;
990 goto err_release_sysfs_group;
995 err_release_sysfs_group:
998 if (!IS_ERR(data->
reg)) {
1007 struct sht15_data *data = platform_get_drvdata(pdev);
1014 if (sht15_soft_reset(data)) {
1020 if (!IS_ERR(data->
reg)) {
1045 .probe = sht15_probe,
1047 .id_table = sht15_device_ids,