Lines Matching +full:chan +full:- +full:step +full:- +full:avg

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
89 /* Step size and prescaler ratio */
114 u8 avg; member
120 * struct twl4030_madc_request - madc request packet for channel conversion
155 * struct twl4030_madc_data - a container for madc info
160 * @use_second_irq: IRQ selection (main or co-processor)
177 const struct iio_chan_spec *chan, in twl4030_madc_read() argument
184 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; in twl4030_madc_read()
186 req.channels = BIT(chan->channel); in twl4030_madc_read()
196 *val = req.rbuf[chan->channel]; in twl4030_madc_read()
255 /* Conversion table from -3 to 55 degrees Celcius */
279 .avg = TWL4030_MADC_RTAVERAGE_LSB,
284 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
290 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
297 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
313 dev_err(madc->dev, "unable to read register 0x%X\n", reg); in twl4030_madc_channel_raw_read()
340 for (temp = 58; temp >= 0; temp--) { in twl4030battery_temperature()
342 if ((actual - res) >= 0) in twl4030battery_temperature()
366 * @madc - pointer to twl4030_madc_data struct
367 * @reg_base - Base address of the first channel
368 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
369 * @buf - The channel values are stored here. if read fails error
370 * @raw - Return raw values without conversion
387 dev_err(madc->dev, "Unable to read register 0x%X\n", in twl4030_madc_read_channels()
399 dev_err(madc->dev, "err reading current\n"); in twl4030_madc_read_channels()
403 buf[i] = buf[i] - 750; in twl4030_madc_read_channels()
409 dev_err(madc->dev, "err reading temperature\n"); in twl4030_madc_read_channels()
412 buf[i] -= 3; in twl4030_madc_read_channels()
419 * conv_result = decimal value of 10-bit conversion in twl4030_madc_read_channels()
421 * step size = 1.5 / (2 ^ 10 -1) in twl4030_madc_read_channels()
437 * @madc - pointer to twl4030_madc_data struct
438 * @id - irq number to be disabled
448 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); in twl4030_madc_disable_irq()
450 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_disable_irq()
451 madc->imr); in twl4030_madc_disable_irq()
455 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); in twl4030_madc_disable_irq()
457 dev_err(madc->dev, in twl4030_madc_disable_irq()
458 "unable to write imr register 0x%X\n", madc->imr); in twl4030_madc_disable_irq()
473 mutex_lock(&madc->lock); in twl4030_madc_threaded_irq_handler()
474 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); in twl4030_madc_threaded_irq_handler()
476 dev_err(madc->dev, "unable to read isr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
477 madc->isr); in twl4030_madc_threaded_irq_handler()
480 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); in twl4030_madc_threaded_irq_handler()
482 dev_err(madc->dev, "unable to read imr register 0x%X\n", in twl4030_madc_threaded_irq_handler()
483 madc->imr); in twl4030_madc_threaded_irq_handler()
492 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); in twl4030_madc_threaded_irq_handler()
493 madc->requests[i].result_pending = true; in twl4030_madc_threaded_irq_handler()
496 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
498 if (!r->result_pending) in twl4030_madc_threaded_irq_handler()
500 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
502 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
503 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
505 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
506 r->active = false; in twl4030_madc_threaded_irq_handler()
508 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
518 r = &madc->requests[i]; in twl4030_madc_threaded_irq_handler()
519 if (!r->active) in twl4030_madc_threaded_irq_handler()
521 method = &twl4030_conversion_methods[r->method]; in twl4030_madc_threaded_irq_handler()
523 twl4030_madc_read_channels(madc, method->rbase, in twl4030_madc_threaded_irq_handler()
524 r->channels, r->rbuf, r->raw); in twl4030_madc_threaded_irq_handler()
526 r->result_pending = false; in twl4030_madc_threaded_irq_handler()
527 r->active = false; in twl4030_madc_threaded_irq_handler()
529 mutex_unlock(&madc->lock); in twl4030_madc_threaded_irq_handler()
537 * @madc - pointer to twl4030_madc_data struct
538 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
549 return -ENOTSUPP; in twl4030_madc_start_conversion()
553 method->ctrl); in twl4030_madc_start_conversion()
555 dev_err(madc->dev, "unable to write ctrl register 0x%X\n", in twl4030_madc_start_conversion()
556 method->ctrl); in twl4030_madc_start_conversion()
565 * @madc - pointer to twl4030_madc_data struct
566 * @timeout_ms - timeout value in milliseconds
567 * @status_reg - ctrl register
583 dev_err(madc->dev, in twl4030_madc_wait_conversion_ready()
592 dev_err(madc->dev, "conversion timeout!\n"); in twl4030_madc_wait_conversion_ready()
594 return -EAGAIN; in twl4030_madc_wait_conversion_ready()
600 * req->rbuf will be filled with read values of channels based on the
611 return -EINVAL; in twl4030_madc_conversion()
613 mutex_lock(&twl4030_madc->lock); in twl4030_madc_conversion()
614 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { in twl4030_madc_conversion()
615 ret = -EINVAL; in twl4030_madc_conversion()
619 if (twl4030_madc->requests[req->method].active) { in twl4030_madc_conversion()
620 ret = -EBUSY; in twl4030_madc_conversion()
623 method = &twl4030_conversion_methods[req->method]; in twl4030_madc_conversion()
625 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel); in twl4030_madc_conversion()
627 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
628 "unable to write sel register 0x%X\n", method->sel); in twl4030_madc_conversion()
632 if (req->do_avg) { in twl4030_madc_conversion()
633 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, in twl4030_madc_conversion()
634 method->avg); in twl4030_madc_conversion()
636 dev_err(twl4030_madc->dev, in twl4030_madc_conversion()
637 "unable to write avg register 0x%X\n", in twl4030_madc_conversion()
638 method->avg); in twl4030_madc_conversion()
643 if (req->method == TWL4030_MADC_RT) { in twl4030_madc_conversion()
644 ret = -EINVAL; in twl4030_madc_conversion()
647 ret = twl4030_madc_start_conversion(twl4030_madc, req->method); in twl4030_madc_conversion()
650 twl4030_madc->requests[req->method].active = true; in twl4030_madc_conversion()
652 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); in twl4030_madc_conversion()
654 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
657 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, in twl4030_madc_conversion()
658 req->channels, req->rbuf, req->raw); in twl4030_madc_conversion()
659 twl4030_madc->requests[req->method].active = false; in twl4030_madc_conversion()
662 mutex_unlock(&twl4030_madc->lock); in twl4030_madc_conversion()
668 * twl4030_madc_set_current_generator() - setup bias current
671 * @chan: can be one of the two values:
672 * 0 - Enables bias current for main battery type reading
673 * 1 - Enables bias current for main battery temperature sensing
674 * @on: enable or disable chan.
680 int chan, int on) in twl4030_madc_set_current_generator() argument
689 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", in twl4030_madc_set_current_generator()
694 regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; in twl4030_madc_set_current_generator()
703 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", in twl4030_madc_set_current_generator()
713 * @madc - pointer to twl4030_madc_data struct
714 * @on - Enable or disable MADC software power on bit.
725 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
735 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", in twl4030_madc_set_power()
748 struct device *dev = &pdev->dev; in twl4030_madc_probe()
756 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); in twl4030_madc_probe()
757 return -EINVAL; in twl4030_madc_probe()
760 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); in twl4030_madc_probe()
762 dev_err(&pdev->dev, "failed allocating iio device\n"); in twl4030_madc_probe()
763 return -ENOMEM; in twl4030_madc_probe()
767 madc->dev = &pdev->dev; in twl4030_madc_probe()
769 iio_dev->name = dev_name(&pdev->dev); in twl4030_madc_probe()
770 iio_dev->info = &twl4030_madc_iio_info; in twl4030_madc_probe()
771 iio_dev->modes = INDIO_DIRECT_MODE; in twl4030_madc_probe()
772 iio_dev->channels = twl4030_madc_iio_channels; in twl4030_madc_probe()
773 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); in twl4030_madc_probe()
781 madc->use_second_irq = (pdata->irq_line != 1); in twl4030_madc_probe()
783 madc->use_second_irq = device_property_read_bool(dev, in twl4030_madc_probe()
784 "ti,system-uses-second-madc-irq"); in twl4030_madc_probe()
786 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : in twl4030_madc_probe()
788 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : in twl4030_madc_probe()
801 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", in twl4030_madc_probe()
809 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", in twl4030_madc_probe()
817 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", in twl4030_madc_probe()
824 dev_info(&pdev->dev, "clk disabled, enabling\n"); in twl4030_madc_probe()
829 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", in twl4030_madc_probe()
836 mutex_init(&madc->lock); in twl4030_madc_probe()
839 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, in twl4030_madc_probe()
844 dev_err(&pdev->dev, "could not request irq\n"); in twl4030_madc_probe()
853 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
861 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n", in twl4030_madc_probe()
867 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1"); in twl4030_madc_probe()
868 if (IS_ERR(madc->usb3v1)) { in twl4030_madc_probe()
869 ret = -ENODEV; in twl4030_madc_probe()
873 ret = regulator_enable(madc->usb3v1); in twl4030_madc_probe()
875 dev_err(madc->dev, "could not enable 3v1 bias regulator\n"); in twl4030_madc_probe()
881 dev_err(&pdev->dev, "could not register iio device\n"); in twl4030_madc_probe()
888 regulator_disable(madc->usb3v1); in twl4030_madc_probe()
906 regulator_disable(madc->usb3v1); in twl4030_madc_remove()
910 { .compatible = "ti,twl4030-madc", },