Lines Matching +full:regulator +full:- +full:soft +full:- +full:start

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>
28 #include <linux/regulator/consumer.h>
46 #define SHT15_TSRST 11 /* (msecs) soft reset time */
65 * struct sht15_temppair - elements of voltage dependent temp calc
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
131 * @measurements_valid: are the current stored measures valid (start condition).
132 * @status_valid: is the current stored status valid (start condition).
138 * @reg: associated regulator (if specified).
142 * regulator consumer if available.
144 * obtained from the regulator and so any calculations
167 struct regulator *reg;
176 * sht15_crc8() - compute crc8
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
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
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
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
287 * sht15_wait_for_response() - checks for ack from device
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()
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.
332 * sht15_soft_reset() - send a soft reset command
346 data->val_status = 0; in sht15_soft_reset()
352 * sht15_ack() - send a ack
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
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
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
430 err = gpiod_direction_output(data->data, 1); 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
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()
466 if (data->checksumming) { in sht15_update_status()
471 data->checksum_ok = (sht15_crc8(data, checksum_vals, 2) in sht15_update_status()
482 * fails, a soft reset command must be sent to the device. 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()
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
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()
553 return -ETIME; in sht15_measurement()
559 * a soft reset command must be sent to the device. in sht15_measurement()
561 if (data->checksumming && !data->checksum_ok) { in sht15_measurement()
562 previous_config = data->val_status & 0x07; 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
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()
597 data->state = SHT15_READING_TEMP; 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
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
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_show_status() - show status information in sysfs
685 u8 bit = to_sensor_dev_attr(attr)->index; in sht15_status_show()
689 return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit)); in sht15_status_show()
693 * sht15_store_heater() - change heater state via sysfs
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()
722 mutex_unlock(&data->read_lock); in sht15_status_store()
728 * sht15_show_temp() - show temperature measurement value in sysfs
750 * sht15_show_humidity() - show humidity measurement value in sysfs
774 return sprintf(buf, "%s\n", pdev->name); in name_show()
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()
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()
842 if (data->checksumming) { 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()
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()
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
889 * @event: voltage regulator state change event code
890 * @ignored: function parameter - ignored here
892 * Note that as the notification code holds the regulator lock, we have
902 data->supply_uv_valid = false; in sht15_invalidate_voltage()
903 schedule_work(&data->update_supply_work); in sht15_invalidate_voltage()
921 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 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()
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()
933 * If a regulator is available, 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()
947 "failed to enable regulator: %d\n", ret); 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()
959 "regulator notifier request failed\n"); 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()
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()
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()
1028 mutex_lock(&data->read_lock); in sht15_remove()
1030 mutex_unlock(&data->read_lock); in sht15_remove()
1031 return -EFAULT; in sht15_remove()
1033 hwmon_device_unregister(data->hwmon_dev); in sht15_remove()
1034 sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); in sht15_remove()
1035 if (!IS_ERR(data->reg)) { in sht15_remove()
1036 regulator_unregister_notifier(data->reg, &data->nb); in sht15_remove()
1037 regulator_disable(data->reg); in sht15_remove()
1040 mutex_unlock(&data->read_lock); in sht15_remove()