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
119 * struct twl4030_madc_request - madc request packet for channel conversion
154 * struct twl4030_madc_data - a container for madc info
159 * @use_second_irq: IRQ selection (main or co-processor)
183 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; in twl4030_madc_read()
185 req.channels = BIT(chan->channel); in twl4030_madc_read()
195 *val = req.rbuf[chan->channel]; in twl4030_madc_read()
254 /* Conversion table from -3 to 55 degrees Celcius */
296 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
312 dev_err(madc->dev, "unable to read register 0x%X\n", reg); in twl4030_madc_channel_raw_read()
326 int temp, curr, volt, res, ret; in twl4030battery_temperature() local
328 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; in twl4030battery_temperature()
329 /* Getting and calculating the supply current in micro amperes */ in twl4030battery_temperature()
337 res = volt * 1000 / curr; in twl4030battery_temperature()
339 for (temp = 58; temp >= 0; temp--) { in twl4030battery_temperature()
341 if ((actual - res) >= 0) in twl4030battery_temperature()
365 * @madc - pointer to twl4030_madc_data struct
366 * @reg_base - Base address of the first channel
367 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
368 * @buf - The channel values are stored here. if read fails error
369 * @raw - Return raw values without conversion
386 dev_err(madc->dev, "Unable to read register 0x%X\n", in twl4030_madc_read_channels()
398 dev_err(madc->dev, "err reading current\n"); in twl4030_madc_read_channels()
402 buf[i] = buf[i] - 750; in twl4030_madc_read_channels()
408 dev_err(madc->dev, "err reading temperature\n"); in twl4030_madc_read_channels()
411 buf[i] -= 3; in twl4030_madc_read_channels()
418 * conv_result = decimal value of 10-bit conversion in twl4030_madc_read_channels()
420 * step size = 1.5 / (2 ^ 10 -1) in twl4030_madc_read_channels()
436 * @madc - pointer to twl4030_madc_data struct
437 * @id - irq number to be disabled
447 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); in twl4030_madc_disable_irq()
449 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_disable_irq()
450 madc->imr); in twl4030_madc_disable_irq()
454 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); in twl4030_madc_disable_irq()
456 dev_err(madc->dev, in twl4030_madc_disable_irq()
457 "unable to write imr register 0x%X\n", madc->imr); in twl4030_madc_disable_irq()
472 mutex_lock(&madc->lock); in twl4030_madc_threaded_irq_handler()
473 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); in twl4030_madc_threaded_irq_handler()
475 dev_err(madc->dev, "unable to read isr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
476 madc->isr); in twl4030_madc_threaded_irq_handler()
479 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); in twl4030_madc_threaded_irq_handler()
481 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
482 madc->imr); in twl4030_madc_threaded_irq_handler()
491 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); in twl4030_madc_threaded_irq_handler()
492 madc->requests[i].result_pending = true; in twl4030_madc_threaded_irq_handler()
495 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
497 if (!r->result_pending) in twl4030_madc_threaded_irq_handler()
499 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
501 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
502 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
504 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
505 r->active = false; in twl4030_madc_threaded_irq_handler()
507 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
517 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
518 if (!r->active) in twl4030_madc_threaded_irq_handler()
520 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
522 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
523 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
525 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
526 r->active = false; in twl4030_madc_threaded_irq_handler()
528 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
536 * @madc - pointer to twl4030_madc_data struct
537 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
548 return -ENOTSUPP; in twl4030_madc_start_conversion()
552 method->ctrl); in twl4030_madc_start_conversion()
554 dev_err(madc->dev, "unable to write ctrl register 0x%X\n", in twl4030_madc_start_conversion()
555 method->ctrl); in twl4030_madc_start_conversion()
564 * @madc - pointer to twl4030_madc_data struct
565 * @timeout_ms - timeout value in milliseconds
566 * @status_reg - ctrl register
582 dev_err(madc->dev, in twl4030_madc_wait_conversion_ready()
591 dev_err(madc->dev, "conversion timeout!\n"); in twl4030_madc_wait_conversion_ready()
593 return -EAGAIN; in twl4030_madc_wait_conversion_ready()
599 * req->rbuf will be filled with read values of channels based on the
610 return -EINVAL; in twl4030_madc_conversion()
612 mutex_lock(&twl4030_madc->lock); in twl4030_madc_conversion()
613 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { in twl4030_madc_conversion()
614 ret = -EINVAL; in twl4030_madc_conversion()
618 if (twl4030_madc->requests[req->method].active) { in twl4030_madc_conversion()
619 ret = -EBUSY; in twl4030_madc_conversion()
622 method = &twl4030_conversion_methods[req->method]; in twl4030_madc_conversion()
624 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel); in twl4030_madc_conversion()
626 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
627 "unable to write sel register 0x%X\n", method->sel); in twl4030_madc_conversion()
631 if (req->do_avg) { in twl4030_madc_conversion()
632 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, in twl4030_madc_conversion()
633 method->avg); in twl4030_madc_conversion()
635 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
637 method->avg); in twl4030_madc_conversion()
642 if (req->method == TWL4030_MADC_RT) { in twl4030_madc_conversion()
643 ret = -EINVAL; in twl4030_madc_conversion()
646 ret = twl4030_madc_start_conversion(twl4030_madc, req->method); in twl4030_madc_conversion()
649 twl4030_madc->requests[req->method].active = true; in twl4030_madc_conversion()
651 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); in twl4030_madc_conversion()
653 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
656 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, in twl4030_madc_conversion()
657 req->channels, req->rbuf, req->raw); in twl4030_madc_conversion()
658 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
661 mutex_unlock(&twl4030_madc->lock); in twl4030_madc_conversion()
667 * twl4030_madc_set_current_generator() - setup bias current
671 * 0 - Enables bias current for main battery type reading
672 * 1 - Enables bias current for main battery temperature sensing
688 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", in twl4030_madc_set_current_generator()
702 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", in twl4030_madc_set_current_generator()
712 * @madc - pointer to twl4030_madc_data struct
713 * @on - Enable or disable MADC software power on bit.
724 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
734 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
748 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); in twl4030_madc_probe()
749 struct device_node *np = pdev->dev.of_node; in twl4030_madc_probe()
755 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); in twl4030_madc_probe()
756 return -EINVAL; in twl4030_madc_probe()
759 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); in twl4030_madc_probe()
761 dev_err(&pdev->dev, "failed allocating iio device\n"); in twl4030_madc_probe()
762 return -ENOMEM; in twl4030_madc_probe()
766 madc->dev = &pdev->dev; in twl4030_madc_probe()
768 iio_dev->name = dev_name(&pdev->dev); in twl4030_madc_probe()
769 iio_dev->info = &twl4030_madc_iio_info; in twl4030_madc_probe()
770 iio_dev->modes = INDIO_DIRECT_MODE; in twl4030_madc_probe()
771 iio_dev->channels = twl4030_madc_iio_channels; in twl4030_madc_probe()
772 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); in twl4030_madc_probe()
780 madc->use_second_irq = (pdata->irq_line != 1); in twl4030_madc_probe()
782 madc->use_second_irq = of_property_read_bool(np, in twl4030_madc_probe()
783 "ti,system-uses-second-madc-irq"); in twl4030_madc_probe()
785 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : in twl4030_madc_probe()
787 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : in twl4030_madc_probe()
800 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", in twl4030_madc_probe()
808 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", in twl4030_madc_probe()
816 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", in twl4030_madc_probe()
823 dev_info(&pdev->dev, "clk disabled, enabling\n"); in twl4030_madc_probe()
828 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", in twl4030_madc_probe()
835 mutex_init(&madc->lock); in twl4030_madc_probe()
838 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, in twl4030_madc_probe()
843 dev_err(&pdev->dev, "could not request irq\n"); in twl4030_madc_probe()
852 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
860 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
866 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1"); in twl4030_madc_probe()
867 if (IS_ERR(madc->usb3v1)) { in twl4030_madc_probe()
868 ret = -ENODEV; in twl4030_madc_probe()
872 ret = regulator_enable(madc->usb3v1); in twl4030_madc_probe()
874 dev_err(madc->dev, "could not enable 3v1 bias regulator\n"); in twl4030_madc_probe()
880 dev_err(&pdev->dev, "could not register iio device\n"); in twl4030_madc_probe()
887 regulator_disable(madc->usb3v1); in twl4030_madc_probe()
905 regulator_disable(madc->usb3v1); in twl4030_madc_remove()
912 { .compatible = "ti,twl4030-madc", },