Lines Matching +full:micro +full:- +full:volt
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()
253 /* Conversion table from -3 to 55 degrees Celcius */
295 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
311 dev_err(madc->dev, "unable to read register 0x%X\n", reg); in twl4030_madc_channel_raw_read()
325 int temp, curr, volt, res, ret; in twl4030battery_temperature() local
327 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; in twl4030battery_temperature()
328 /* Getting and calculating the supply current in micro amperes */ in twl4030battery_temperature()
336 res = volt * 1000 / curr; in twl4030battery_temperature()
338 for (temp = 58; temp >= 0; temp--) { in twl4030battery_temperature()
340 if ((actual - res) >= 0) in twl4030battery_temperature()
364 * @madc - pointer to twl4030_madc_data struct
365 * @reg_base - Base address of the first channel
366 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
367 * @buf - The channel values are stored here. if read fails error
368 * @raw - Return raw values without conversion
385 dev_err(madc->dev, "Unable to read register 0x%X\n", in twl4030_madc_read_channels()
397 dev_err(madc->dev, "err reading current\n"); in twl4030_madc_read_channels()
401 buf[i] = buf[i] - 750; in twl4030_madc_read_channels()
407 dev_err(madc->dev, "err reading temperature\n"); in twl4030_madc_read_channels()
410 buf[i] -= 3; in twl4030_madc_read_channels()
417 * conv_result = decimal value of 10-bit conversion in twl4030_madc_read_channels()
419 * step size = 1.5 / (2 ^ 10 -1) in twl4030_madc_read_channels()
435 * @madc - pointer to twl4030_madc_data struct
436 * @id - irq number to be disabled
446 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); in twl4030_madc_disable_irq()
448 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_disable_irq()
449 madc->imr); in twl4030_madc_disable_irq()
453 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); in twl4030_madc_disable_irq()
455 dev_err(madc->dev, in twl4030_madc_disable_irq()
456 "unable to write imr register 0x%X\n", madc->imr); in twl4030_madc_disable_irq()
471 mutex_lock(&madc->lock); in twl4030_madc_threaded_irq_handler()
472 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); in twl4030_madc_threaded_irq_handler()
474 dev_err(madc->dev, "unable to read isr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
475 madc->isr); in twl4030_madc_threaded_irq_handler()
478 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); in twl4030_madc_threaded_irq_handler()
480 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
481 madc->imr); in twl4030_madc_threaded_irq_handler()
490 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); in twl4030_madc_threaded_irq_handler()
491 madc->requests[i].result_pending = true; in twl4030_madc_threaded_irq_handler()
494 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
496 if (!r->result_pending) in twl4030_madc_threaded_irq_handler()
498 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
500 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
501 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
503 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
504 r->active = false; in twl4030_madc_threaded_irq_handler()
506 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
516 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
517 if (!r->active) in twl4030_madc_threaded_irq_handler()
519 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
521 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
522 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
524 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
525 r->active = false; in twl4030_madc_threaded_irq_handler()
527 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
535 * @madc - pointer to twl4030_madc_data struct
536 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
547 return -ENOTSUPP; in twl4030_madc_start_conversion()
551 method->ctrl); in twl4030_madc_start_conversion()
553 dev_err(madc->dev, "unable to write ctrl register 0x%X\n", in twl4030_madc_start_conversion()
554 method->ctrl); in twl4030_madc_start_conversion()
563 * @madc - pointer to twl4030_madc_data struct
564 * @timeout_ms - timeout value in milliseconds
565 * @status_reg - ctrl register
581 dev_err(madc->dev, in twl4030_madc_wait_conversion_ready()
590 dev_err(madc->dev, "conversion timeout!\n"); in twl4030_madc_wait_conversion_ready()
592 return -EAGAIN; in twl4030_madc_wait_conversion_ready()
598 * req->rbuf will be filled with read values of channels based on the
609 return -EINVAL; in twl4030_madc_conversion()
611 mutex_lock(&twl4030_madc->lock); in twl4030_madc_conversion()
612 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { in twl4030_madc_conversion()
613 ret = -EINVAL; in twl4030_madc_conversion()
617 if (twl4030_madc->requests[req->method].active) { in twl4030_madc_conversion()
618 ret = -EBUSY; in twl4030_madc_conversion()
621 method = &twl4030_conversion_methods[req->method]; in twl4030_madc_conversion()
623 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel); in twl4030_madc_conversion()
625 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
626 "unable to write sel register 0x%X\n", method->sel); in twl4030_madc_conversion()
630 if (req->do_avg) { in twl4030_madc_conversion()
631 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, in twl4030_madc_conversion()
632 method->avg); in twl4030_madc_conversion()
634 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
636 method->avg); in twl4030_madc_conversion()
641 if (req->method == TWL4030_MADC_RT) { in twl4030_madc_conversion()
642 ret = -EINVAL; in twl4030_madc_conversion()
645 ret = twl4030_madc_start_conversion(twl4030_madc, req->method); in twl4030_madc_conversion()
648 twl4030_madc->requests[req->method].active = true; in twl4030_madc_conversion()
650 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); in twl4030_madc_conversion()
652 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
655 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, in twl4030_madc_conversion()
656 req->channels, req->rbuf, req->raw); in twl4030_madc_conversion()
657 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
660 mutex_unlock(&twl4030_madc->lock); in twl4030_madc_conversion()
666 * twl4030_madc_set_current_generator() - setup bias current
670 * 0 - Enables bias current for main battery type reading
671 * 1 - Enables bias current for main battery temperature sensing
687 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", in twl4030_madc_set_current_generator()
701 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", in twl4030_madc_set_current_generator()
711 * @madc - pointer to twl4030_madc_data struct
712 * @on - Enable or disable MADC software power on bit.
723 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
733 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
747 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); in twl4030_madc_probe()
748 struct device_node *np = pdev->dev.of_node; in twl4030_madc_probe()
754 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); in twl4030_madc_probe()
755 return -EINVAL; in twl4030_madc_probe()
758 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); in twl4030_madc_probe()
760 dev_err(&pdev->dev, "failed allocating iio device\n"); in twl4030_madc_probe()
761 return -ENOMEM; in twl4030_madc_probe()
765 madc->dev = &pdev->dev; in twl4030_madc_probe()
767 iio_dev->name = dev_name(&pdev->dev); in twl4030_madc_probe()
768 iio_dev->info = &twl4030_madc_iio_info; in twl4030_madc_probe()
769 iio_dev->modes = INDIO_DIRECT_MODE; in twl4030_madc_probe()
770 iio_dev->channels = twl4030_madc_iio_channels; in twl4030_madc_probe()
771 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); in twl4030_madc_probe()
779 madc->use_second_irq = (pdata->irq_line != 1); in twl4030_madc_probe()
781 madc->use_second_irq = of_property_read_bool(np, in twl4030_madc_probe()
782 "ti,system-uses-second-madc-irq"); in twl4030_madc_probe()
784 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : in twl4030_madc_probe()
786 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : in twl4030_madc_probe()
799 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", in twl4030_madc_probe()
807 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", in twl4030_madc_probe()
815 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", in twl4030_madc_probe()
822 dev_info(&pdev->dev, "clk disabled, enabling\n"); in twl4030_madc_probe()
827 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", in twl4030_madc_probe()
834 mutex_init(&madc->lock); in twl4030_madc_probe()
837 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, in twl4030_madc_probe()
842 dev_err(&pdev->dev, "could not request irq\n"); in twl4030_madc_probe()
851 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
859 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
865 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1"); in twl4030_madc_probe()
866 if (IS_ERR(madc->usb3v1)) { in twl4030_madc_probe()
867 ret = -ENODEV; in twl4030_madc_probe()
871 ret = regulator_enable(madc->usb3v1); in twl4030_madc_probe()
873 dev_err(madc->dev, "could not enable 3v1 bias regulator\n"); in twl4030_madc_probe()
879 dev_err(&pdev->dev, "could not register iio device\n"); in twl4030_madc_probe()
886 regulator_disable(madc->usb3v1); in twl4030_madc_probe()
904 regulator_disable(madc->usb3v1); in twl4030_madc_remove()
911 { .compatible = "ti,twl4030-madc", },