Lines Matching refs:adc

146 static int palmas_disable_auto_conversion(struct palmas_gpadc *adc)  in palmas_disable_auto_conversion()  argument
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()
182 struct palmas_gpadc *adc = data; in palmas_gpadc_irq() local
184 complete(&adc->conv_completion); in palmas_gpadc_irq()
191 struct palmas_gpadc *adc = data; in palmas_gpadc_irq_auto() local
193 dev_dbg(adc->dev, "Threshold interrupt %d occurs\n", irq); in palmas_gpadc_irq_auto()
194 palmas_disable_auto_conversion(adc); in palmas_gpadc_irq_auto()
199 static int palmas_gpadc_start_mask_interrupt(struct palmas_gpadc *adc, in palmas_gpadc_start_mask_interrupt() argument
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()
219 static int palmas_gpadc_enable(struct palmas_gpadc *adc, int adc_chan, in palmas_gpadc_enable() argument
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()
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()
279 static int palmas_gpadc_read_prepare(struct palmas_gpadc *adc, int adc_chan) in palmas_gpadc_read_prepare() argument
283 ret = palmas_gpadc_enable(adc, adc_chan, true); in palmas_gpadc_read_prepare()
287 return palmas_gpadc_start_mask_interrupt(adc, 0); in palmas_gpadc_read_prepare()
290 static void palmas_gpadc_read_done(struct palmas_gpadc *adc, int adc_chan) in palmas_gpadc_read_done() argument
292 palmas_gpadc_start_mask_interrupt(adc, 1); in palmas_gpadc_read_done()
293 palmas_gpadc_enable(adc, adc_chan, false); in palmas_gpadc_read_done()
296 static int palmas_gpadc_calibrate(struct palmas_gpadc *adc, int adc_chan) in palmas_gpadc_calibrate() argument
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()
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()
337 static int palmas_gpadc_start_conversion(struct palmas_gpadc *adc, int adc_chan) in palmas_gpadc_start_conversion() argument
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()
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()
371 static int palmas_gpadc_get_calibrated_code(struct palmas_gpadc *adc, in palmas_gpadc_get_calibrated_code() argument
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()
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()
392 struct palmas_gpadc *adc = iio_priv(indio_dev); in palmas_gpadc_read_raw() local
399 mutex_lock(&adc->lock); in palmas_gpadc_read_raw()
404 ret = palmas_gpadc_read_prepare(adc, adc_chan); in palmas_gpadc_read_raw()
408 ret = palmas_gpadc_start_conversion(adc, adc_chan); in palmas_gpadc_read_raw()
410 dev_err(adc->dev, in palmas_gpadc_read_raw()
417 adc, adc_chan, ret); in palmas_gpadc_read_raw()
425 mutex_unlock(&adc->lock); in palmas_gpadc_read_raw()
429 palmas_gpadc_read_done(adc, adc_chan); in palmas_gpadc_read_raw()
430 mutex_unlock(&adc->lock); in palmas_gpadc_read_raw()
498 struct palmas_gpadc *adc; in palmas_gpadc_probe() local
517 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc)); in palmas_gpadc_probe()
523 adc = iio_priv(indio_dev); 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()
544 adc); 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()
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()
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()
585 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_0; in palmas_gpadc_probe()
587 adc->ch0_current = PALMAS_ADC_CH0_CURRENT_SRC_5; 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()
595 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_0; in palmas_gpadc_probe()
597 adc->ch3_current = PALMAS_ADC_CH3_CURRENT_SRC_10; 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()
613 dev_err(adc->dev, "iio_device_register() failed: %d\n", ret); in palmas_gpadc_probe()
619 if (!(adc->adc_info[i].is_uncalibrated)) in palmas_gpadc_probe()
620 palmas_gpadc_calibrate(adc, i); in palmas_gpadc_probe()
623 if (adc->wakeup1_enable || adc->wakeup2_enable) in palmas_gpadc_probe()
630 free_irq(adc->irq_auto_1, adc); 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()
643 struct palmas_gpadc *adc = iio_priv(indio_dev); in palmas_gpadc_remove() local
645 if (adc->wakeup1_enable || adc->wakeup2_enable) 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()
657 static int palmas_adc_wakeup_configure(struct palmas_gpadc *adc) in palmas_adc_wakeup_configure() argument
665 adc_period = adc->auto_conversion_period; 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()
762 static int palmas_adc_wakeup_reset(struct palmas_gpadc *adc) in palmas_adc_wakeup_reset() argument
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()
773 ret = palmas_disable_auto_conversion(adc); in palmas_adc_wakeup_reset()
775 dev_err(adc->dev, "Disable auto conversion failed: %d\n", ret); in palmas_adc_wakeup_reset()
783 struct palmas_gpadc *adc = iio_priv(indio_dev); in palmas_gpadc_suspend() local
784 int wakeup = adc->wakeup1_enable || adc->wakeup2_enable; in palmas_gpadc_suspend()
790 ret = palmas_adc_wakeup_configure(adc); 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()
806 struct palmas_gpadc *adc = iio_priv(indio_dev); in palmas_gpadc_resume() local
807 int wakeup = adc->wakeup1_enable || adc->wakeup2_enable; in palmas_gpadc_resume()
813 ret = palmas_adc_wakeup_reset(adc); 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()