Lines Matching +full:data +full:- +full:sheet

1 // SPDX-License-Identifier: GPL-2.0
3 * sht15.c - support for the SHT15 Temperature and Humidity Sensor
5 * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
21 #include <linux/hwmon-sysfs.h>
45 #define SHT15_TSU 150 /* (nsecs) data setup time */
65 * struct sht15_temppair - elements of voltage dependent temp calc
67 * @d1: see data sheet
74 /* Table 9 from datasheet - relates temperature calculation to supply voltage */
76 { 2500000, -39400 },
77 { 3000000, -39600 },
78 { 3500000, -39700 },
79 { 4000000, -39800 },
80 { 5000000, -40100 },
120 * struct sht15_data - device instance specific data
122 * @data: data GPIO line
129 * @checksumming: flag used to enable the data validation with CRC.
151 struct gpio_desc *data; member
176 * sht15_crc8() - compute crc8
177 * @data: sht15 specific data.
178 * @value: sht15 retrieved data.
179 * @len: Length of retrieved data
183 static u8 sht15_crc8(struct sht15_data *data, in sht15_crc8() argument
187 u8 crc = bitrev8(data->val_status & 0x0F); in sht15_crc8()
189 while (len--) { in sht15_crc8()
198 * sht15_connection_reset() - reset the comms interface
199 * @data: sht15 specific data
201 * This implements section 3.4 of the data sheet
203 static int sht15_connection_reset(struct sht15_data *data) in sht15_connection_reset() argument
207 err = gpiod_direction_output(data->data, 1); in sht15_connection_reset()
211 gpiod_set_value(data->sck, 0); in sht15_connection_reset()
214 gpiod_set_value(data->sck, 1); in sht15_connection_reset()
216 gpiod_set_value(data->sck, 0); in sht15_connection_reset()
223 * sht15_send_bit() - send an individual bit to the device
224 * @data: device state data
227 static inline void sht15_send_bit(struct sht15_data *data, int val) in sht15_send_bit() argument
229 gpiod_set_value(data->data, val); in sht15_send_bit()
231 gpiod_set_value(data->sck, 1); in sht15_send_bit()
233 gpiod_set_value(data->sck, 0); in sht15_send_bit()
238 * sht15_transmission_start() - specific sequence for new transmission
239 * @data: device state data
241 * Timings for this are not documented on the data sheet, so very
243 * figure 12 on the data sheet.
245 static int sht15_transmission_start(struct sht15_data *data) in sht15_transmission_start() argument
249 /* ensure data is high and output */ in sht15_transmission_start()
250 err = gpiod_direction_output(data->data, 1); in sht15_transmission_start()
254 gpiod_set_value(data->sck, 0); in sht15_transmission_start()
256 gpiod_set_value(data->sck, 1); in sht15_transmission_start()
258 gpiod_set_value(data->data, 0); in sht15_transmission_start()
260 gpiod_set_value(data->sck, 0); in sht15_transmission_start()
262 gpiod_set_value(data->sck, 1); in sht15_transmission_start()
264 gpiod_set_value(data->data, 1); in sht15_transmission_start()
266 gpiod_set_value(data->sck, 0); in sht15_transmission_start()
272 * sht15_send_byte() - send a single byte to the device
273 * @data: device state
276 static void sht15_send_byte(struct sht15_data *data, u8 byte) in sht15_send_byte() argument
281 sht15_send_bit(data, !!(byte & 0x80)); in sht15_send_byte()
287 * sht15_wait_for_response() - checks for ack from device
288 * @data: device state
290 static int sht15_wait_for_response(struct sht15_data *data) in sht15_wait_for_response() argument
294 err = gpiod_direction_input(data->data); in sht15_wait_for_response()
297 gpiod_set_value(data->sck, 1); in sht15_wait_for_response()
299 if (gpiod_get_value(data->data)) { in sht15_wait_for_response()
300 gpiod_set_value(data->sck, 0); in sht15_wait_for_response()
301 dev_err(data->dev, "Command not acknowledged\n"); in sht15_wait_for_response()
302 err = sht15_connection_reset(data); in sht15_wait_for_response()
305 return -EIO; in sht15_wait_for_response()
307 gpiod_set_value(data->sck, 0); in sht15_wait_for_response()
313 * sht15_send_cmd() - Sends a command to the device.
314 * @data: device state
317 * On entry, sck is output low, data is output pull high
320 static int sht15_send_cmd(struct sht15_data *data, u8 cmd) in sht15_send_cmd() argument
324 err = sht15_transmission_start(data); in sht15_send_cmd()
327 sht15_send_byte(data, cmd); in sht15_send_cmd()
328 return sht15_wait_for_response(data); in sht15_send_cmd()
332 * sht15_soft_reset() - send a soft reset command
333 * @data: sht15 specific data.
337 static int sht15_soft_reset(struct sht15_data *data) in sht15_soft_reset() argument
341 ret = sht15_send_cmd(data, SHT15_SOFT_RESET); in sht15_soft_reset()
346 data->val_status = 0; in sht15_soft_reset()
352 * sht15_ack() - send a ack
353 * @data: sht15 specific data.
355 * Each byte of data is acknowledged by pulling the data line
358 static int sht15_ack(struct sht15_data *data) in sht15_ack() argument
362 err = gpiod_direction_output(data->data, 0); in sht15_ack()
366 gpiod_set_value(data->sck, 1); in sht15_ack()
368 gpiod_set_value(data->sck, 0); in sht15_ack()
370 gpiod_set_value(data->data, 1); in sht15_ack()
372 return gpiod_direction_input(data->data); in sht15_ack()
376 * sht15_end_transmission() - notify device of end of transmission
377 * @data: device state.
379 * This is basically a NAK (single clock pulse, data high).
381 static int sht15_end_transmission(struct sht15_data *data) in sht15_end_transmission() argument
385 err = gpiod_direction_output(data->data, 1); in sht15_end_transmission()
389 gpiod_set_value(data->sck, 1); in sht15_end_transmission()
391 gpiod_set_value(data->sck, 0); in sht15_end_transmission()
397 * sht15_read_byte() - Read a byte back from the device
398 * @data: device state.
400 static u8 sht15_read_byte(struct sht15_data *data) in sht15_read_byte() argument
407 gpiod_set_value(data->sck, 1); in sht15_read_byte()
409 byte |= !!gpiod_get_value(data->data); in sht15_read_byte()
410 gpiod_set_value(data->sck, 0); in sht15_read_byte()
417 * sht15_send_status() - write the status register byte
418 * @data: sht15 specific data.
423 static int sht15_send_status(struct sht15_data *data, u8 status) in sht15_send_status() argument
427 err = sht15_send_cmd(data, SHT15_WRITE_STATUS); in sht15_send_status()
430 err = gpiod_direction_output(data->data, 1); in sht15_send_status()
434 sht15_send_byte(data, status); in sht15_send_status()
435 err = sht15_wait_for_response(data); in sht15_send_status()
439 data->val_status = status; in sht15_send_status()
444 * sht15_update_status() - get updated status register from device if too old
445 * @data: device instance specific data.
449 static int sht15_update_status(struct sht15_data *data) in sht15_update_status() argument
458 mutex_lock(&data->read_lock); in sht15_update_status()
459 if (time_after(jiffies, data->last_status + timeout) in sht15_update_status()
460 || !data->status_valid) { in sht15_update_status()
461 ret = sht15_send_cmd(data, SHT15_READ_STATUS); in sht15_update_status()
464 status = sht15_read_byte(data); in sht15_update_status()
466 if (data->checksumming) { in sht15_update_status()
467 sht15_ack(data); in sht15_update_status()
468 dev_checksum = bitrev8(sht15_read_byte(data)); in sht15_update_status()
471 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2) in sht15_update_status()
475 ret = sht15_end_transmission(data); in sht15_update_status()
480 * Perform checksum validation on the received data. in sht15_update_status()
484 if (data->checksumming && !data->checksum_ok) { in sht15_update_status()
485 previous_config = data->val_status & 0x07; in sht15_update_status()
486 ret = sht15_soft_reset(data); in sht15_update_status()
490 ret = sht15_send_status(data, previous_config); in sht15_update_status()
492 dev_err(data->dev, in sht15_update_status()
498 ret = -EAGAIN; in sht15_update_status()
502 data->val_status = status; in sht15_update_status()
503 data->status_valid = true; in sht15_update_status()
504 data->last_status = jiffies; in sht15_update_status()
508 mutex_unlock(&data->read_lock); in sht15_update_status()
513 * sht15_measurement() - get a new value from device
514 * @data: device instance specific data
519 static int sht15_measurement(struct sht15_data *data, in sht15_measurement() argument
526 ret = sht15_send_cmd(data, command); in sht15_measurement()
530 ret = gpiod_direction_input(data->data); in sht15_measurement()
533 atomic_set(&data->interrupt_handled, 0); in sht15_measurement()
535 enable_irq(gpiod_to_irq(data->data)); in sht15_measurement()
536 if (gpiod_get_value(data->data) == 0) { in sht15_measurement()
537 disable_irq_nosync(gpiod_to_irq(data->data)); in sht15_measurement()
539 if (!atomic_read(&data->interrupt_handled)) in sht15_measurement()
540 schedule_work(&data->read_work); in sht15_measurement()
542 ret = wait_event_timeout(data->wait_queue, in sht15_measurement()
543 (data->state == SHT15_READING_NOTHING), in sht15_measurement()
545 if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */ in sht15_measurement()
546 data->state = SHT15_READING_NOTHING; in sht15_measurement()
547 return -EIO; in sht15_measurement()
549 disable_irq_nosync(gpiod_to_irq(data->data)); in sht15_measurement()
550 ret = sht15_connection_reset(data); in sht15_measurement()
553 return -ETIME; in sht15_measurement()
557 * Perform checksum validation on the received data. in sht15_measurement()
561 if (data->checksumming && !data->checksum_ok) { in sht15_measurement()
562 previous_config = data->val_status & 0x07; in sht15_measurement()
563 ret = sht15_soft_reset(data); in sht15_measurement()
567 ret = sht15_send_status(data, previous_config); in sht15_measurement()
569 dev_err(data->dev, in sht15_measurement()
575 return -EAGAIN; in sht15_measurement()
582 * sht15_update_measurements() - get updated measures from device if too old
583 * @data: device state
585 static int sht15_update_measurements(struct sht15_data *data) in sht15_update_measurements() argument
590 mutex_lock(&data->read_lock); in sht15_update_measurements()
591 if (time_after(jiffies, data->last_measurement + timeout) in sht15_update_measurements()
592 || !data->measurements_valid) { in sht15_update_measurements()
593 data->state = SHT15_READING_HUMID; in sht15_update_measurements()
594 ret = sht15_measurement(data, SHT15_MEASURE_RH, 160); in sht15_update_measurements()
597 data->state = SHT15_READING_TEMP; in sht15_update_measurements()
598 ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400); in sht15_update_measurements()
601 data->measurements_valid = true; in sht15_update_measurements()
602 data->last_measurement = jiffies; in sht15_update_measurements()
606 mutex_unlock(&data->read_lock); in sht15_update_measurements()
611 * sht15_calc_temp() - convert the raw reading to a temperature
612 * @data: device state
614 * As per section 4.3 of the data sheet.
616 static inline int sht15_calc_temp(struct sht15_data *data) in sht15_calc_temp() argument
619 int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10; in sht15_calc_temp()
622 for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--) in sht15_calc_temp()
624 if (data->supply_uv > temppoints[i - 1].vdd) { in sht15_calc_temp()
625 d1 = (data->supply_uv - temppoints[i - 1].vdd) in sht15_calc_temp()
626 * (temppoints[i].d1 - temppoints[i - 1].d1) in sht15_calc_temp()
627 / (temppoints[i].vdd - temppoints[i - 1].vdd) in sht15_calc_temp()
628 + temppoints[i - 1].d1; in sht15_calc_temp()
632 return data->val_temp * d2 + d1; in sht15_calc_temp()
636 * sht15_calc_humid() - using last temperature convert raw to humid
637 * @data: device state
640 * the data sheet.
645 static inline int sht15_calc_humid(struct sht15_data *data) in sht15_calc_humid() argument
648 int temp = sht15_calc_temp(data); in sht15_calc_humid()
651 const int c1 = -4; in sht15_calc_humid()
653 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) { in sht15_calc_humid()
654 c2 = 648000; /* x 10 ^ -6 */ in sht15_calc_humid()
655 c3 = -7200; /* x 10 ^ -7 */ in sht15_calc_humid()
658 c2 = 40500; /* x 10 ^ -6 */ in sht15_calc_humid()
659 c3 = -28; /* x 10 ^ -7 */ in sht15_calc_humid()
664 + c2 * data->val_humid / 1000 in sht15_calc_humid()
665 + (data->val_humid * data->val_humid * c3) / 10000; in sht15_calc_humid()
666 return (temp - 25000) * (10000 + t2 * data->val_humid) in sht15_calc_humid()
671 * sht15_status_show() - show status information in sysfs
684 struct sht15_data *data = dev_get_drvdata(dev); in sht15_status_show() local
685 u8 bit = to_sensor_dev_attr(attr)->index; in sht15_status_show()
687 ret = sht15_update_status(data); in sht15_status_show()
689 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit)); in sht15_status_show()
693 * sht15_status_store() - change heater state via sysfs
697 * @count: length of the data.
707 struct sht15_data *data = dev_get_drvdata(dev); in sht15_status_store() local
712 return -EINVAL; in sht15_status_store()
714 mutex_lock(&data->read_lock); in sht15_status_store()
715 status = data->val_status & 0x07; in sht15_status_store()
721 ret = sht15_send_status(data, status); in sht15_status_store()
722 mutex_unlock(&data->read_lock); in sht15_status_store()
728 * sht15_temp_show() - show temperature measurement value in sysfs
740 struct sht15_data *data = dev_get_drvdata(dev); in sht15_temp_show() local
743 ret = sht15_update_measurements(data); in sht15_temp_show()
746 sht15_calc_temp(data)); in sht15_temp_show()
750 * sht15_humidity_show() - show humidity measurement value in sysfs
762 struct sht15_data *data = dev_get_drvdata(dev); in sht15_humidity_show() local
764 ret = sht15_update_measurements(data); in sht15_humidity_show()
766 return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data)); in sht15_humidity_show()
774 return sprintf(buf, "%s\n", pdev->name); in name_show()
801 struct sht15_data *data = d; in sht15_interrupt_fired() local
805 atomic_inc(&data->interrupt_handled); in sht15_interrupt_fired()
807 if (data->state != SHT15_READING_NOTHING) in sht15_interrupt_fired()
808 schedule_work(&data->read_work); in sht15_interrupt_fired()
817 struct sht15_data *data in sht15_bh_read_data() local
822 if (gpiod_get_value(data->data)) { in sht15_bh_read_data()
824 * If not, then start the interrupt again - care here as could in sht15_bh_read_data()
827 atomic_set(&data->interrupt_handled, 0); in sht15_bh_read_data()
828 enable_irq(gpiod_to_irq(data->data)); in sht15_bh_read_data()
830 if (gpiod_get_value(data->data) in sht15_bh_read_data()
831 || atomic_read(&data->interrupt_handled)) in sht15_bh_read_data()
835 /* Read the data back from the device */ in sht15_bh_read_data()
836 val = sht15_read_byte(data); in sht15_bh_read_data()
838 if (sht15_ack(data)) in sht15_bh_read_data()
840 val |= sht15_read_byte(data); in sht15_bh_read_data()
842 if (data->checksumming) { in sht15_bh_read_data()
847 if (sht15_ack(data)) in sht15_bh_read_data()
849 dev_checksum = bitrev8(sht15_read_byte(data)); in sht15_bh_read_data()
850 checksum_vals[0] = (data->state == SHT15_READING_TEMP) ? in sht15_bh_read_data()
854 data->checksum_ok in sht15_bh_read_data()
855 = (sht15_crc8(data, checksum_vals, 3) == dev_checksum); in sht15_bh_read_data()
859 if (sht15_end_transmission(data)) in sht15_bh_read_data()
862 switch (data->state) { in sht15_bh_read_data()
864 data->val_temp = val; in sht15_bh_read_data()
867 data->val_humid = val; in sht15_bh_read_data()
873 data->state = SHT15_READING_NOTHING; in sht15_bh_read_data()
875 wake_up(&data->wait_queue); in sht15_bh_read_data()
880 struct sht15_data *data in sht15_update_voltage() local
883 data->supply_uv = regulator_get_voltage(data->reg); in sht15_update_voltage()
887 * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
890 * @ignored: function parameter - ignored here
899 struct sht15_data *data = container_of(nb, struct sht15_data, nb); in sht15_invalidate_voltage() local
902 data->supply_uv_valid = false; in sht15_invalidate_voltage()
903 schedule_work(&data->update_supply_work); in sht15_invalidate_voltage()
919 struct sht15_data *data; in sht15_probe() local
921 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); in sht15_probe()
922 if (!data) in sht15_probe()
923 return -ENOMEM; in sht15_probe()
925 INIT_WORK(&data->read_work, sht15_bh_read_data); in sht15_probe()
926 INIT_WORK(&data->update_supply_work, sht15_update_voltage); in sht15_probe()
927 platform_set_drvdata(pdev, data); in sht15_probe()
928 mutex_init(&data->read_lock); in sht15_probe()
929 data->dev = &pdev->dev; in sht15_probe()
930 init_waitqueue_head(&data->wait_queue); in sht15_probe()
936 data->reg = devm_regulator_get_optional(data->dev, "vcc"); in sht15_probe()
937 if (!IS_ERR(data->reg)) { in sht15_probe()
940 voltage = regulator_get_voltage(data->reg); in sht15_probe()
942 data->supply_uv = voltage; in sht15_probe()
944 ret = regulator_enable(data->reg); in sht15_probe()
946 dev_err(&pdev->dev, in sht15_probe()
955 data->nb.notifier_call = &sht15_invalidate_voltage; in sht15_probe()
956 ret = regulator_register_notifier(data->reg, &data->nb); in sht15_probe()
958 dev_err(&pdev->dev, in sht15_probe()
960 regulator_disable(data->reg); in sht15_probe()
966 data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW); in sht15_probe()
967 if (IS_ERR(data->sck)) { in sht15_probe()
968 ret = PTR_ERR(data->sck); in sht15_probe()
969 dev_err(&pdev->dev, "clock line GPIO request failed\n"); in sht15_probe()
972 data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN); in sht15_probe()
973 if (IS_ERR(data->data)) { in sht15_probe()
974 ret = PTR_ERR(data->data); in sht15_probe()
975 dev_err(&pdev->dev, "data line GPIO request failed\n"); in sht15_probe()
979 ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data), in sht15_probe()
982 "sht15 data", in sht15_probe()
983 data); in sht15_probe()
985 dev_err(&pdev->dev, "failed to get irq for data line\n"); in sht15_probe()
988 disable_irq_nosync(gpiod_to_irq(data->data)); in sht15_probe()
989 ret = sht15_connection_reset(data); in sht15_probe()
992 ret = sht15_soft_reset(data); in sht15_probe()
996 ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group); in sht15_probe()
998 dev_err(&pdev->dev, "sysfs create failed\n"); in sht15_probe()
1002 data->hwmon_dev = hwmon_device_register(data->dev); in sht15_probe()
1003 if (IS_ERR(data->hwmon_dev)) { in sht15_probe()
1004 ret = PTR_ERR(data->hwmon_dev); in sht15_probe()
1011 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); in sht15_probe()
1013 if (!IS_ERR(data->reg)) { in sht15_probe()
1014 regulator_unregister_notifier(data->reg, &data->nb); in sht15_probe()
1015 regulator_disable(data->reg); in sht15_probe()
1022 struct sht15_data *data = platform_get_drvdata(pdev); in sht15_remove() local
1025 hwmon_device_unregister(data->hwmon_dev); in sht15_remove()
1026 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); in sht15_remove()
1028 ret = sht15_soft_reset(data); in sht15_remove()
1030 dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret)); in sht15_remove()
1032 if (!IS_ERR(data->reg)) { in sht15_remove()
1033 regulator_unregister_notifier(data->reg, &data->nb); in sht15_remove()
1034 regulator_disable(data->reg); in sht15_remove()