Lines Matching +full:resistance +full:- +full:temp +full:- +full:table
1 // SPDX-License-Identifier: GPL-2.0-only
4 * TWL4030 MADC module driver-This driver monitors the real time
8 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
9 * J Keerthy <j-keerthy@ti.com>
11 * Based on twl4030-madc.c
118 * struct twl4030_madc_request - madc request packet for channel conversion
153 * struct twl4030_madc_data - a container for madc info
158 * @use_second_irq: IRQ selection (main or co-processor)
182 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; in twl4030_madc_read()
184 req.channels = BIT(chan->channel); in twl4030_madc_read()
194 *val = req.rbuf[chan->channel]; in twl4030_madc_read()
260 /* Conversion table from -3 to 55 degrees Celcius */
302 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
318 dev_err(madc->dev, "unable to read register 0x%X\n", reg); in twl4030_madc_channel_raw_read()
332 int temp, curr, volt, res, ret; in twl4030battery_temperature() local
342 /* Getting and calculating the thermistor resistance in ohms */ in twl4030battery_temperature()
345 for (temp = 58; temp >= 0; temp--) { in twl4030battery_temperature()
346 int actual = twl4030_therm_tbl[temp]; in twl4030battery_temperature()
347 if ((actual - res) >= 0) in twl4030battery_temperature()
351 return temp + 1; in twl4030battery_temperature()
371 * @madc - pointer to twl4030_madc_data struct
372 * @reg_base - Base address of the first channel
373 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
374 * @buf - The channel values are stored here. if read fails error
375 * @raw - Return raw values without conversion
392 dev_err(madc->dev, "Unable to read register 0x%X\n", in twl4030_madc_read_channels()
404 dev_err(madc->dev, "err reading current\n"); in twl4030_madc_read_channels()
408 buf[i] = buf[i] - 750; in twl4030_madc_read_channels()
414 dev_err(madc->dev, "err reading temperature\n"); in twl4030_madc_read_channels()
417 buf[i] -= 3; in twl4030_madc_read_channels()
424 * conv_result = decimal value of 10-bit conversion in twl4030_madc_read_channels()
426 * step size = 1.5 / (2 ^ 10 -1) in twl4030_madc_read_channels()
442 * @madc - pointer to twl4030_madc_data struct
443 * @id - irq number to be disabled
453 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); in twl4030_madc_disable_irq()
455 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_disable_irq()
456 madc->imr); in twl4030_madc_disable_irq()
460 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); in twl4030_madc_disable_irq()
462 dev_err(madc->dev, in twl4030_madc_disable_irq()
463 "unable to write imr register 0x%X\n", madc->imr); in twl4030_madc_disable_irq()
478 mutex_lock(&madc->lock); in twl4030_madc_threaded_irq_handler()
479 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); in twl4030_madc_threaded_irq_handler()
481 dev_err(madc->dev, "unable to read isr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
482 madc->isr); in twl4030_madc_threaded_irq_handler()
485 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); in twl4030_madc_threaded_irq_handler()
487 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
488 madc->imr); in twl4030_madc_threaded_irq_handler()
497 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); in twl4030_madc_threaded_irq_handler()
498 madc->requests[i].result_pending = true; in twl4030_madc_threaded_irq_handler()
501 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
503 if (!r->result_pending) in twl4030_madc_threaded_irq_handler()
505 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
507 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
508 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
510 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
511 r->active = false; in twl4030_madc_threaded_irq_handler()
513 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
523 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
524 if (!r->active) in twl4030_madc_threaded_irq_handler()
526 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
528 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
529 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
531 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
532 r->active = false; in twl4030_madc_threaded_irq_handler()
534 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
542 * @madc - pointer to twl4030_madc_data struct
543 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
554 return -ENOTSUPP; in twl4030_madc_start_conversion()
558 method->ctrl); in twl4030_madc_start_conversion()
560 dev_err(madc->dev, "unable to write ctrl register 0x%X\n", in twl4030_madc_start_conversion()
561 method->ctrl); in twl4030_madc_start_conversion()
570 * @madc - pointer to twl4030_madc_data struct
571 * @timeout_ms - timeout value in milliseconds
572 * @status_reg - ctrl register
588 dev_err(madc->dev, in twl4030_madc_wait_conversion_ready()
597 dev_err(madc->dev, "conversion timeout!\n"); in twl4030_madc_wait_conversion_ready()
599 return -EAGAIN; in twl4030_madc_wait_conversion_ready()
605 * req->rbuf will be filled with read values of channels based on the
616 return -EINVAL; in twl4030_madc_conversion()
618 mutex_lock(&twl4030_madc->lock); in twl4030_madc_conversion()
619 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { in twl4030_madc_conversion()
620 ret = -EINVAL; in twl4030_madc_conversion()
624 if (twl4030_madc->requests[req->method].active) { in twl4030_madc_conversion()
625 ret = -EBUSY; in twl4030_madc_conversion()
628 method = &twl4030_conversion_methods[req->method]; in twl4030_madc_conversion()
630 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel); in twl4030_madc_conversion()
632 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
633 "unable to write sel register 0x%X\n", method->sel); in twl4030_madc_conversion()
637 if (req->do_avg) { in twl4030_madc_conversion()
638 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, in twl4030_madc_conversion()
639 method->avg); in twl4030_madc_conversion()
641 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
643 method->avg); in twl4030_madc_conversion()
648 if (req->method == TWL4030_MADC_RT) { in twl4030_madc_conversion()
649 ret = -EINVAL; in twl4030_madc_conversion()
652 ret = twl4030_madc_start_conversion(twl4030_madc, req->method); in twl4030_madc_conversion()
655 twl4030_madc->requests[req->method].active = true; in twl4030_madc_conversion()
657 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); in twl4030_madc_conversion()
659 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
662 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, in twl4030_madc_conversion()
663 req->channels, req->rbuf, req->raw); in twl4030_madc_conversion()
664 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
667 mutex_unlock(&twl4030_madc->lock); in twl4030_madc_conversion()
673 * twl4030_madc_set_current_generator() - setup bias current
677 * 0 - Enables bias current for main battery type reading
678 * 1 - Enables bias current for main battery temperature sensing
694 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", in twl4030_madc_set_current_generator()
708 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", in twl4030_madc_set_current_generator()
718 * @madc - pointer to twl4030_madc_data struct
719 * @on - Enable or disable MADC software power on bit.
730 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
740 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
754 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); in twl4030_madc_probe()
755 struct device_node *np = pdev->dev.of_node; in twl4030_madc_probe()
761 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); in twl4030_madc_probe()
762 return -EINVAL; in twl4030_madc_probe()
765 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); in twl4030_madc_probe()
767 dev_err(&pdev->dev, "failed allocating iio device\n"); in twl4030_madc_probe()
768 return -ENOMEM; in twl4030_madc_probe()
772 madc->dev = &pdev->dev; in twl4030_madc_probe()
774 iio_dev->name = dev_name(&pdev->dev); in twl4030_madc_probe()
775 iio_dev->info = &twl4030_madc_iio_info; in twl4030_madc_probe()
776 iio_dev->modes = INDIO_DIRECT_MODE; in twl4030_madc_probe()
777 iio_dev->channels = twl4030_madc_iio_channels; in twl4030_madc_probe()
778 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); in twl4030_madc_probe()
786 madc->use_second_irq = (pdata->irq_line != 1); in twl4030_madc_probe()
788 madc->use_second_irq = of_property_read_bool(np, in twl4030_madc_probe()
789 "ti,system-uses-second-madc-irq"); in twl4030_madc_probe()
791 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : in twl4030_madc_probe()
793 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : in twl4030_madc_probe()
806 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", in twl4030_madc_probe()
814 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", in twl4030_madc_probe()
822 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", in twl4030_madc_probe()
829 dev_info(&pdev->dev, "clk disabled, enabling\n"); in twl4030_madc_probe()
834 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", in twl4030_madc_probe()
841 mutex_init(&madc->lock); in twl4030_madc_probe()
844 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, in twl4030_madc_probe()
849 dev_err(&pdev->dev, "could not request irq\n"); in twl4030_madc_probe()
858 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
866 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
872 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1"); in twl4030_madc_probe()
873 if (IS_ERR(madc->usb3v1)) { in twl4030_madc_probe()
874 ret = -ENODEV; in twl4030_madc_probe()
878 ret = regulator_enable(madc->usb3v1); in twl4030_madc_probe()
880 dev_err(madc->dev, "could not enable 3v1 bias regulator\n"); in twl4030_madc_probe()
886 dev_err(&pdev->dev, "could not register iio device\n"); in twl4030_madc_probe()
893 regulator_disable(madc->usb3v1); in twl4030_madc_probe()
911 regulator_disable(madc->usb3v1); in twl4030_madc_remove()
918 { .compatible = "ti,twl4030-madc", },