Lines Matching refs:data

151 	struct gpio_desc		*data;  member
183 static u8 sht15_crc8(struct sht15_data *data, in sht15_crc8() argument
187 u8 crc = bitrev8(data->val_status & 0x0F); in sht15_crc8()
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()
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()
245 static int sht15_transmission_start(struct sht15_data *data) in sht15_transmission_start() argument
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()
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()
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()
307 gpiod_set_value(data->sck, 0); in sht15_wait_for_response()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
549 disable_irq_nosync(gpiod_to_irq(data->data)); in sht15_measurement()
550 ret = sht15_connection_reset(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()
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()
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()
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()
632 return data->val_temp * d2 + d1; in sht15_calc_temp()
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()
653 if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) { 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()
684 struct sht15_data *data = dev_get_drvdata(dev); in sht15_status_show() local
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()
707 struct sht15_data *data = dev_get_drvdata(dev); in sht15_status_store() local
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()
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()
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()
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()
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()
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()
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()
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()
955 data->nb.notifier_call = &sht15_invalidate_voltage; in sht15_probe()
956 ret = regulator_register_notifier(data->reg, &data->nb); 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()
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()
979 ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data), in sht15_probe()
983 data); 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()
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()
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()
1028 ret = sht15_soft_reset(data); 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()