Lines Matching +full:channel0 +full:- +full:current +full:- +full:microamp
1 // SPDX-License-Identifier: GPL-2.0-only
3 * palmas-adc.c -- TI PALMAS GPADC.
27 #define MOD_NAME "palmas-gpadc"
30 #define PALMAS_GPADC_TRIMINVALID -1
80 * struct palmas_gpadc - the palmas_gpadc structure
81 * @ch0_current: channel 0 current source setting
86 * @ch3_current: channel 0 current source setting
100 * This is the palmas_gpadc structure to store run-time information
150 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_disable_auto_conversion()
155 dev_err(adc->dev, "GPADC_CTRL1 update failed: %d\n", ret); in palmas_disable_auto_conversion()
159 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_disable_auto_conversion()
165 dev_err(adc->dev, "AUTO_CTRL update failed: %d\n", ret); in palmas_disable_auto_conversion()
171 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_disable_auto_conversion()
175 dev_err(adc->dev, "GPADC_CTRL1 update failed: %d\n", ret); in palmas_disable_auto_conversion()
184 complete(&adc->conv_completion); in palmas_gpadc_irq()
193 dev_dbg(adc->dev, "Threshold interrupt %d occurs\n", irq); in palmas_gpadc_irq_auto()
205 ret = palmas_update_bits(adc->palmas, PALMAS_INTERRUPT_BASE, in palmas_gpadc_start_mask_interrupt()
209 ret = palmas_update_bits(adc->palmas, PALMAS_INTERRUPT_BASE, in palmas_gpadc_start_mask_interrupt()
214 dev_err(adc->dev, "GPADC INT MASK update failed: %d\n", ret); in palmas_gpadc_start_mask_interrupt()
226 val = (adc->extended_delay in palmas_gpadc_enable()
228 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_enable()
232 dev_err(adc->dev, "RT_CTRL update failed: %d\n", ret); in palmas_gpadc_enable()
239 val = (adc->ch0_current in palmas_gpadc_enable()
241 val |= (adc->ch3_current in palmas_gpadc_enable()
244 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_enable()
247 dev_err(adc->dev, in palmas_gpadc_enable()
248 "Failed to update current setting: %d\n", ret); in palmas_gpadc_enable()
255 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_enable()
258 dev_err(adc->dev, "SW_SELECT update failed: %d\n", ret); in palmas_gpadc_enable()
262 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_enable()
265 dev_err(adc->dev, "SW_SELECT write failed: %d\n", ret); in palmas_gpadc_enable()
267 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_enable()
271 dev_err(adc->dev, "CTRL1 update failed: %d\n", ret); in palmas_gpadc_enable()
303 int x1 = adc->adc_info[adc_chan].x1; in palmas_gpadc_calibrate()
304 int x2 = adc->adc_info[adc_chan].x2; in palmas_gpadc_calibrate()
305 int v1 = adc->adc_info[adc_chan].v1; in palmas_gpadc_calibrate()
306 int v2 = adc->adc_info[adc_chan].v2; in palmas_gpadc_calibrate()
308 ret = palmas_read(adc->palmas, PALMAS_TRIM_GPADC_BASE, in palmas_gpadc_calibrate()
309 adc->adc_info[adc_chan].trim1_reg, &d1); in palmas_gpadc_calibrate()
311 dev_err(adc->dev, "TRIM read failed: %d\n", ret); in palmas_gpadc_calibrate()
315 ret = palmas_read(adc->palmas, PALMAS_TRIM_GPADC_BASE, in palmas_gpadc_calibrate()
316 adc->adc_info[adc_chan].trim2_reg, &d2); in palmas_gpadc_calibrate()
318 dev_err(adc->dev, "TRIM read failed: %d\n", ret); in palmas_gpadc_calibrate()
323 k = (1000 + (1000 * (d2 - d1)) / (x2 - x1)); in palmas_gpadc_calibrate()
326 gain = ((v2 - v1) * 1000) / (x2 - x1); in palmas_gpadc_calibrate()
328 adc->adc_info[adc_chan].gain_error = k; in palmas_gpadc_calibrate()
329 adc->adc_info[adc_chan].gain = gain; in palmas_gpadc_calibrate()
331 adc->adc_info[adc_chan].offset = (d1 * 1000) - ((k - 1000) * x1); in palmas_gpadc_calibrate()
342 init_completion(&adc->conv_completion); in palmas_gpadc_start_conversion()
343 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_start_conversion()
348 dev_err(adc->dev, "SELECT_SW_START write failed: %d\n", ret); in palmas_gpadc_start_conversion()
352 ret = wait_for_completion_timeout(&adc->conv_completion, in palmas_gpadc_start_conversion()
355 dev_err(adc->dev, "conversion not completed\n"); in palmas_gpadc_start_conversion()
356 return -ETIMEDOUT; in palmas_gpadc_start_conversion()
359 ret = palmas_bulk_read(adc->palmas, PALMAS_GPADC_BASE, in palmas_gpadc_start_conversion()
362 dev_err(adc->dev, "SW_CONV0_LSB read failed: %d\n", ret); in palmas_gpadc_start_conversion()
374 if (!adc->adc_info[adc_chan].is_uncalibrated) in palmas_gpadc_get_calibrated_code()
375 val = (val*1000 - adc->adc_info[adc_chan].offset) / in palmas_gpadc_get_calibrated_code()
376 adc->adc_info[adc_chan].gain_error; in palmas_gpadc_get_calibrated_code()
379 if (val < -10) in palmas_gpadc_get_calibrated_code()
380 dev_err(adc->dev, "Mismatch with calibration var = %d\n", val); in palmas_gpadc_get_calibrated_code()
384 val = (val * adc->adc_info[adc_chan].gain) / 1000; in palmas_gpadc_get_calibrated_code()
393 int adc_chan = chan->channel; in palmas_gpadc_read_raw()
397 return -EINVAL; in palmas_gpadc_read_raw()
399 mutex_lock(&adc->lock); in palmas_gpadc_read_raw()
410 dev_err(adc->dev, in palmas_gpadc_read_raw()
425 mutex_unlock(&adc->lock); in palmas_gpadc_read_raw()
430 mutex_unlock(&adc->lock); in palmas_gpadc_read_raw()
471 struct device_node *np = pdev->dev.of_node; in palmas_gpadc_get_adc_dt_data()
476 gp_data = devm_kzalloc(&pdev->dev, sizeof(*gp_data), GFP_KERNEL); in palmas_gpadc_get_adc_dt_data()
478 return -ENOMEM; in palmas_gpadc_get_adc_dt_data()
480 ret = of_property_read_u32(np, "ti,channel0-current-microamp", &pval); in palmas_gpadc_get_adc_dt_data()
482 gp_data->ch0_current = pval; in palmas_gpadc_get_adc_dt_data()
484 ret = of_property_read_u32(np, "ti,channel3-current-microamp", &pval); in palmas_gpadc_get_adc_dt_data()
486 gp_data->ch3_current = pval; in palmas_gpadc_get_adc_dt_data()
488 gp_data->extended_delay = of_property_read_bool(np, in palmas_gpadc_get_adc_dt_data()
489 "ti,enable-extended-delay"); in palmas_gpadc_get_adc_dt_data()
504 pdata = dev_get_platdata(pdev->dev.parent); in palmas_gpadc_probe()
506 if (pdata && pdata->gpadc_pdata) in palmas_gpadc_probe()
507 gpadc_pdata = pdata->gpadc_pdata; in palmas_gpadc_probe()
509 if (!gpadc_pdata && pdev->dev.of_node) { in palmas_gpadc_probe()
515 return -EINVAL; in palmas_gpadc_probe()
517 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc)); in palmas_gpadc_probe()
519 dev_err(&pdev->dev, "iio_device_alloc failed\n"); in palmas_gpadc_probe()
520 return -ENOMEM; in palmas_gpadc_probe()
524 adc->dev = &pdev->dev; in palmas_gpadc_probe()
525 adc->palmas = dev_get_drvdata(pdev->dev.parent); in palmas_gpadc_probe()
526 adc->adc_info = palmas_gpadc_info; in palmas_gpadc_probe()
528 mutex_init(&adc->lock); in palmas_gpadc_probe()
530 init_completion(&adc->conv_completion); in palmas_gpadc_probe()
533 adc->auto_conversion_period = gpadc_pdata->auto_conversion_period_ms; in palmas_gpadc_probe()
534 adc->irq = palmas_irq_get_virq(adc->palmas, PALMAS_GPADC_EOC_SW_IRQ); in palmas_gpadc_probe()
535 if (adc->irq < 0) { in palmas_gpadc_probe()
536 dev_err(adc->dev, in palmas_gpadc_probe()
537 "get virq failed: %d\n", adc->irq); in palmas_gpadc_probe()
538 ret = adc->irq; in palmas_gpadc_probe()
541 ret = request_threaded_irq(adc->irq, NULL, in palmas_gpadc_probe()
543 IRQF_ONESHOT, dev_name(adc->dev), in palmas_gpadc_probe()
546 dev_err(adc->dev, in palmas_gpadc_probe()
547 "request irq %d failed: %d\n", adc->irq, ret); in palmas_gpadc_probe()
551 if (gpadc_pdata->adc_wakeup1_data) { in palmas_gpadc_probe()
552 memcpy(&adc->wakeup1_data, gpadc_pdata->adc_wakeup1_data, in palmas_gpadc_probe()
553 sizeof(adc->wakeup1_data)); in palmas_gpadc_probe()
554 adc->wakeup1_enable = true; in palmas_gpadc_probe()
555 adc->irq_auto_0 = platform_get_irq(pdev, 1); in palmas_gpadc_probe()
556 ret = request_threaded_irq(adc->irq_auto_0, NULL, in palmas_gpadc_probe()
559 "palmas-adc-auto-0", adc); in palmas_gpadc_probe()
561 dev_err(adc->dev, "request auto0 irq %d failed: %d\n", in palmas_gpadc_probe()
562 adc->irq_auto_0, ret); in palmas_gpadc_probe()
567 if (gpadc_pdata->adc_wakeup2_data) { in palmas_gpadc_probe()
568 memcpy(&adc->wakeup2_data, gpadc_pdata->adc_wakeup2_data, in palmas_gpadc_probe()
569 sizeof(adc->wakeup2_data)); in palmas_gpadc_probe()
570 adc->wakeup2_enable = true; in palmas_gpadc_probe()
571 adc->irq_auto_1 = platform_get_irq(pdev, 2); in palmas_gpadc_probe()
572 ret = request_threaded_irq(adc->irq_auto_1, NULL, in palmas_gpadc_probe()
575 "palmas-adc-auto-1", adc); in palmas_gpadc_probe()
577 dev_err(adc->dev, "request auto1 irq %d failed: %d\n", in palmas_gpadc_probe()
578 adc->irq_auto_1, ret); in palmas_gpadc_probe()
583 /* set the current source 0 (value 0/5/15/20 uA => 0..3) */ in palmas_gpadc_probe()
584 if (gpadc_pdata->ch0_current <= 1) in palmas_gpadc_probe()
585 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_0; in palmas_gpadc_probe()
586 else if (gpadc_pdata->ch0_current <= 5) in palmas_gpadc_probe()
587 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_5; in palmas_gpadc_probe()
588 else if (gpadc_pdata->ch0_current <= 15) in palmas_gpadc_probe()
589 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_15; in palmas_gpadc_probe()
591 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_20; in palmas_gpadc_probe()
593 /* set the current source 3 (value 0/10/400/800 uA => 0..3) */ in palmas_gpadc_probe()
594 if (gpadc_pdata->ch3_current <= 1) in palmas_gpadc_probe()
595 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_0; in palmas_gpadc_probe()
596 else if (gpadc_pdata->ch3_current <= 10) in palmas_gpadc_probe()
597 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_10; in palmas_gpadc_probe()
598 else if (gpadc_pdata->ch3_current <= 400) in palmas_gpadc_probe()
599 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_400; in palmas_gpadc_probe()
601 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_800; in palmas_gpadc_probe()
603 adc->extended_delay = gpadc_pdata->extended_delay; in palmas_gpadc_probe()
605 indio_dev->name = MOD_NAME; in palmas_gpadc_probe()
606 indio_dev->info = &palmas_gpadc_iio_info; in palmas_gpadc_probe()
607 indio_dev->modes = INDIO_DIRECT_MODE; in palmas_gpadc_probe()
608 indio_dev->channels = palmas_gpadc_iio_channel; in palmas_gpadc_probe()
609 indio_dev->num_channels = ARRAY_SIZE(palmas_gpadc_iio_channel); in palmas_gpadc_probe()
613 dev_err(adc->dev, "iio_device_register() failed: %d\n", ret); in palmas_gpadc_probe()
617 device_set_wakeup_capable(&pdev->dev, 1); in palmas_gpadc_probe()
619 if (!(adc->adc_info[i].is_uncalibrated)) in palmas_gpadc_probe()
623 if (adc->wakeup1_enable || adc->wakeup2_enable) in palmas_gpadc_probe()
624 device_wakeup_enable(&pdev->dev); in palmas_gpadc_probe()
629 if (gpadc_pdata->adc_wakeup2_data) in palmas_gpadc_probe()
630 free_irq(adc->irq_auto_1, adc); in palmas_gpadc_probe()
632 if (gpadc_pdata->adc_wakeup1_data) in palmas_gpadc_probe()
633 free_irq(adc->irq_auto_0, adc); in palmas_gpadc_probe()
635 free_irq(adc->irq, adc); in palmas_gpadc_probe()
642 struct iio_dev *indio_dev = dev_to_iio_dev(&pdev->dev); in palmas_gpadc_remove()
645 if (adc->wakeup1_enable || adc->wakeup2_enable) in palmas_gpadc_remove()
646 device_wakeup_disable(&pdev->dev); in palmas_gpadc_remove()
648 free_irq(adc->irq, adc); in palmas_gpadc_remove()
649 if (adc->wakeup1_enable) in palmas_gpadc_remove()
650 free_irq(adc->irq_auto_0, adc); in palmas_gpadc_remove()
651 if (adc->wakeup2_enable) in palmas_gpadc_remove()
652 free_irq(adc->irq_auto_1, adc); in palmas_gpadc_remove()
665 adc_period = adc->auto_conversion_period; in palmas_adc_wakeup_configure()
671 i--; in palmas_adc_wakeup_configure()
673 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
678 dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret); in palmas_adc_wakeup_configure()
683 if (adc->wakeup1_enable) { in palmas_adc_wakeup_configure()
686 ch0 = adc->wakeup1_data.adc_channel_number; in palmas_adc_wakeup_configure()
688 if (adc->wakeup1_data.adc_high_threshold > 0) { in palmas_adc_wakeup_configure()
689 thres = adc->wakeup1_data.adc_high_threshold; in palmas_adc_wakeup_configure()
692 thres = adc->wakeup1_data.adc_low_threshold; in palmas_adc_wakeup_configure()
696 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
699 dev_err(adc->dev, in palmas_adc_wakeup_configure()
704 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
708 dev_err(adc->dev, in palmas_adc_wakeup_configure()
714 if (adc->wakeup2_enable) { in palmas_adc_wakeup_configure()
717 ch1 = adc->wakeup2_data.adc_channel_number; in palmas_adc_wakeup_configure()
719 if (adc->wakeup2_data.adc_high_threshold > 0) { in palmas_adc_wakeup_configure()
720 thres = adc->wakeup2_data.adc_high_threshold; in palmas_adc_wakeup_configure()
723 thres = adc->wakeup2_data.adc_low_threshold; in palmas_adc_wakeup_configure()
727 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
730 dev_err(adc->dev, in palmas_adc_wakeup_configure()
735 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
739 dev_err(adc->dev, in palmas_adc_wakeup_configure()
745 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
748 dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret); in palmas_adc_wakeup_configure()
752 ret = palmas_update_bits(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_configure()
757 dev_err(adc->dev, "AUTO_CTRL write failed: %d\n", ret); in palmas_adc_wakeup_configure()
766 ret = palmas_write(adc->palmas, PALMAS_GPADC_BASE, in palmas_adc_wakeup_reset()
769 dev_err(adc->dev, "AUTO_SELECT write failed: %d\n", ret); in palmas_adc_wakeup_reset()
775 dev_err(adc->dev, "Disable auto conversion failed: %d\n", ret); in palmas_adc_wakeup_reset()
784 int wakeup = adc->wakeup1_enable || adc->wakeup2_enable; in palmas_gpadc_suspend()
794 if (adc->wakeup1_enable) in palmas_gpadc_suspend()
795 enable_irq_wake(adc->irq_auto_0); in palmas_gpadc_suspend()
797 if (adc->wakeup2_enable) in palmas_gpadc_suspend()
798 enable_irq_wake(adc->irq_auto_1); in palmas_gpadc_suspend()
807 int wakeup = adc->wakeup1_enable || adc->wakeup2_enable; in palmas_gpadc_resume()
817 if (adc->wakeup1_enable) in palmas_gpadc_resume()
818 disable_irq_wake(adc->irq_auto_0); in palmas_gpadc_resume()
820 if (adc->wakeup2_enable) in palmas_gpadc_resume()
821 disable_irq_wake(adc->irq_auto_1); in palmas_gpadc_resume()
830 { .compatible = "ti,palmas-gpadc", },
848 MODULE_ALIAS("platform:palmas-gpadc");