Lines Matching +full:on +full:- +full:resistance

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
31 #include <linux/fixp-arith.h>
33 #include "ab8500-bm.h"
35 #define BTEMP_THERMAL_LOW_LIMIT -10
51 * struct ab8500_btemp_interrupts - ab8500 interrupts
77 * struct ab8500_btemp - ab8500 BTEMP device information
125 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
130 * This function returns the battery resistance that is
137 if (is_ab8500_1p1_or_earlier(di->parent)) { in ab8500_btemp_batctrl_volt_to_res()
142 return (450000 * (v_batctrl)) / (1800 - v_batctrl); in ab8500_btemp_batctrl_volt_to_res()
149 return (80000 * (v_batctrl)) / (1800 - v_batctrl); in ab8500_btemp_batctrl_volt_to_res()
153 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
156 * This function returns the voltage on BATCTRL. Returns value in mV.
163 ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp); in ab8500_btemp_read_batctrl_voltage()
165 dev_err(di->dev, in ab8500_btemp_read_batctrl_voltage()
175 * ab8500_btemp_get_batctrl_res() - get battery resistance
178 * This function returns the battery pack identification resistance.
189 if (!di->fg) in ab8500_btemp_get_batctrl_res()
190 di->fg = ab8500_fg_get(); in ab8500_btemp_get_batctrl_res()
191 if (!di->fg) { in ab8500_btemp_get_batctrl_res()
192 dev_err(di->dev, "No fg found\n"); in ab8500_btemp_get_batctrl_res()
193 return -EINVAL; in ab8500_btemp_get_batctrl_res()
196 ret = ab8500_fg_inst_curr_start(di->fg); in ab8500_btemp_get_batctrl_res()
199 dev_err(di->dev, "Failed to start current measurement\n"); in ab8500_btemp_get_batctrl_res()
205 } while (!ab8500_fg_inst_curr_started(di->fg)); in ab8500_btemp_get_batctrl_res()
213 } while (!ab8500_fg_inst_curr_done(di->fg)); in ab8500_btemp_get_batctrl_res()
216 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr); in ab8500_btemp_get_batctrl_res()
218 dev_err(di->dev, "Failed to finalize current measurement\n"); in ab8500_btemp_get_batctrl_res()
224 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n", in ab8500_btemp_get_batctrl_res()
231 * ab8500_btemp_id() - Identify the connected battery
240 struct power_supply_battery_info *bi = di->bm->bi; in ab8500_btemp_id()
243 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA; in ab8500_btemp_id()
247 dev_err(di->dev, "%s get batctrl res failed\n", __func__); in ab8500_btemp_id()
248 return -ENXIO; in ab8500_btemp_id()
252 dev_info(di->dev, "Battery detected on BATCTRL (pin C3)" in ab8500_btemp_id()
253 " resistance %d Ohm = %d Ohm +/- %d%%\n", in ab8500_btemp_id()
254 res, bi->bti_resistance_ohm, in ab8500_btemp_id()
255 bi->bti_resistance_tolerance); in ab8500_btemp_id()
257 dev_warn(di->dev, "Battery identified as unknown" in ab8500_btemp_id()
258 ", resistance %d Ohm\n", res); in ab8500_btemp_id()
259 return -ENXIO; in ab8500_btemp_id()
266 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
281 if (!di->initialized) { in ab8500_btemp_periodic_work()
284 dev_warn(di->dev, "failed to identify the battery\n"); in ab8500_btemp_periodic_work()
288 ret = thermal_zone_get_temp(di->tz, &bat_temp); in ab8500_btemp_periodic_work()
290 dev_err(di->dev, "error reading temperature\n"); in ab8500_btemp_periodic_work()
300 * Allow direct updates on temperature only if two samples result in in ab8500_btemp_periodic_work()
304 if ((bat_temp == di->prev_bat_temp) || !di->initialized) { in ab8500_btemp_periodic_work()
305 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) { in ab8500_btemp_periodic_work()
306 di->initialized = true; in ab8500_btemp_periodic_work()
307 di->bat_temp = bat_temp; in ab8500_btemp_periodic_work()
308 power_supply_changed(di->btemp_psy); in ab8500_btemp_periodic_work()
310 } else if (bat_temp < di->prev_bat_temp) { in ab8500_btemp_periodic_work()
311 di->bat_temp--; in ab8500_btemp_periodic_work()
312 power_supply_changed(di->btemp_psy); in ab8500_btemp_periodic_work()
313 } else if (bat_temp > di->prev_bat_temp) { in ab8500_btemp_periodic_work()
314 di->bat_temp++; in ab8500_btemp_periodic_work()
315 power_supply_changed(di->btemp_psy); in ab8500_btemp_periodic_work()
317 di->prev_bat_temp = bat_temp; in ab8500_btemp_periodic_work()
319 if (di->events.ac_conn || di->events.usb_conn) in ab8500_btemp_periodic_work()
320 interval = di->bm->temp_interval_chg; in ab8500_btemp_periodic_work()
322 interval = di->bm->temp_interval_nochg; in ab8500_btemp_periodic_work()
325 queue_delayed_work(di->btemp_wq, in ab8500_btemp_periodic_work()
326 &di->btemp_periodic_work, in ab8500_btemp_periodic_work()
331 * ab8500_btemp_batctrlindb_handler() - battery removal detected
340 dev_err(di->dev, "Battery removal detected!\n"); in ab8500_btemp_batctrlindb_handler()
342 di->events.batt_rem = true; in ab8500_btemp_batctrlindb_handler()
343 power_supply_changed(di->btemp_psy); in ab8500_btemp_batctrlindb_handler()
349 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
359 if (is_ab8500_3p3_or_earlier(di->parent)) { in ab8500_btemp_templow_handler()
360 dev_dbg(di->dev, "Ignore false btemp low irq" in ab8500_btemp_templow_handler()
363 dev_crit(di->dev, "Battery temperature lower than -10deg c\n"); in ab8500_btemp_templow_handler()
365 di->events.btemp_low = true; in ab8500_btemp_templow_handler()
366 di->events.btemp_high = false; in ab8500_btemp_templow_handler()
367 di->events.btemp_medhigh = false; in ab8500_btemp_templow_handler()
368 di->events.btemp_lowmed = false; in ab8500_btemp_templow_handler()
369 power_supply_changed(di->btemp_psy); in ab8500_btemp_templow_handler()
376 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
386 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n"); in ab8500_btemp_temphigh_handler()
388 di->events.btemp_high = true; in ab8500_btemp_temphigh_handler()
389 di->events.btemp_medhigh = false; in ab8500_btemp_temphigh_handler()
390 di->events.btemp_lowmed = false; in ab8500_btemp_temphigh_handler()
391 di->events.btemp_low = false; in ab8500_btemp_temphigh_handler()
392 power_supply_changed(di->btemp_psy); in ab8500_btemp_temphigh_handler()
398 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
408 dev_dbg(di->dev, "Battery temperature is between low and medium\n"); in ab8500_btemp_lowmed_handler()
410 di->events.btemp_lowmed = true; in ab8500_btemp_lowmed_handler()
411 di->events.btemp_medhigh = false; in ab8500_btemp_lowmed_handler()
412 di->events.btemp_high = false; in ab8500_btemp_lowmed_handler()
413 di->events.btemp_low = false; in ab8500_btemp_lowmed_handler()
414 power_supply_changed(di->btemp_psy); in ab8500_btemp_lowmed_handler()
420 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
430 dev_dbg(di->dev, "Battery temperature is between medium and high\n"); in ab8500_btemp_medhigh_handler()
432 di->events.btemp_medhigh = true; in ab8500_btemp_medhigh_handler()
433 di->events.btemp_lowmed = false; in ab8500_btemp_medhigh_handler()
434 di->events.btemp_high = false; in ab8500_btemp_medhigh_handler()
435 di->events.btemp_low = false; in ab8500_btemp_medhigh_handler()
436 power_supply_changed(di->btemp_psy); in ab8500_btemp_medhigh_handler()
442 * ab8500_btemp_periodic() - Periodic temperature measurements
452 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n", in ab8500_btemp_periodic()
458 cancel_delayed_work_sync(&di->btemp_periodic_work); in ab8500_btemp_periodic()
461 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0); in ab8500_btemp_periodic()
465 * ab8500_btemp_get_temp() - get battery temperature
475 * The BTEMP events are not reliabe on AB8500 cut3.3 in ab8500_btemp_get_temp()
478 if (is_ab8500_3p3_or_earlier(di->parent)) { in ab8500_btemp_get_temp()
479 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
481 if (di->events.btemp_low) { in ab8500_btemp_get_temp()
482 if (temp > di->btemp_ranges.btemp_low_limit) in ab8500_btemp_get_temp()
483 temp = di->btemp_ranges.btemp_low_limit * 10; in ab8500_btemp_get_temp()
485 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
486 } else if (di->events.btemp_high) { in ab8500_btemp_get_temp()
487 if (temp < di->btemp_ranges.btemp_high_limit) in ab8500_btemp_get_temp()
488 temp = di->btemp_ranges.btemp_high_limit * 10; in ab8500_btemp_get_temp()
490 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
491 } else if (di->events.btemp_lowmed) { in ab8500_btemp_get_temp()
492 if (temp > di->btemp_ranges.btemp_med_limit) in ab8500_btemp_get_temp()
493 temp = di->btemp_ranges.btemp_med_limit * 10; in ab8500_btemp_get_temp()
495 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
496 } else if (di->events.btemp_medhigh) { in ab8500_btemp_get_temp()
497 if (temp < di->btemp_ranges.btemp_med_limit) in ab8500_btemp_get_temp()
498 temp = di->btemp_ranges.btemp_med_limit * 10; in ab8500_btemp_get_temp()
500 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
502 temp = di->bat_temp * 10; in ab8500_btemp_get_temp()
508 * ab8500_btemp_get_property() - get the btemp properties
519 * Returns error code in case of failure else 0(on success)
530 if (di->events.batt_rem) in ab8500_btemp_get_property()
531 val->intval = 0; in ab8500_btemp_get_property()
533 val->intval = 1; in ab8500_btemp_get_property()
536 if (di->bm->bi) in ab8500_btemp_get_property()
537 val->intval = di->bm->bi->technology; in ab8500_btemp_get_property()
539 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; in ab8500_btemp_get_property()
542 val->intval = ab8500_btemp_get_temp(di); in ab8500_btemp_get_property()
545 return -EINVAL; in ab8500_btemp_get_property()
554 const char **supplicants = (const char **)ext->supplied_to; in ab8500_btemp_get_ext_psy_data()
566 j = match_string(supplicants, ext->num_supplicants, psy->desc->name); in ab8500_btemp_get_ext_psy_data()
571 for (j = 0; j < ext->desc->num_properties; j++) { in ab8500_btemp_get_ext_psy_data()
573 prop = ext->desc->properties[j]; in ab8500_btemp_get_ext_psy_data()
580 switch (ext->desc->type) { in ab8500_btemp_get_ext_psy_data()
583 if (!ret.intval && di->events.ac_conn) { in ab8500_btemp_get_ext_psy_data()
584 di->events.ac_conn = false; in ab8500_btemp_get_ext_psy_data()
587 else if (ret.intval && !di->events.ac_conn) { in ab8500_btemp_get_ext_psy_data()
588 di->events.ac_conn = true; in ab8500_btemp_get_ext_psy_data()
589 if (!di->events.usb_conn) in ab8500_btemp_get_ext_psy_data()
595 if (!ret.intval && di->events.usb_conn) { in ab8500_btemp_get_ext_psy_data()
596 di->events.usb_conn = false; in ab8500_btemp_get_ext_psy_data()
599 else if (ret.intval && !di->events.usb_conn) { in ab8500_btemp_get_ext_psy_data()
600 di->events.usb_conn = true; in ab8500_btemp_get_ext_psy_data()
601 if (!di->events.ac_conn) in ab8500_btemp_get_ext_psy_data()
617 * ab8500_btemp_external_power_changed() - callback for power supply changes
630 di->btemp_psy, ab8500_btemp_get_ext_psy_data); in ab8500_btemp_external_power_changed()
680 di->btemp_wq = in ab8500_btemp_bind()
682 if (di->btemp_wq == NULL) { in ab8500_btemp_bind()
684 return -ENOMEM; in ab8500_btemp_bind()
699 destroy_workqueue(di->btemp_wq); in ab8500_btemp_unbind()
710 struct device *dev = &pdev->dev; in ab8500_btemp_probe()
717 return -ENOMEM; in ab8500_btemp_probe()
719 di->bm = &ab8500_bm_data; in ab8500_btemp_probe()
722 di->dev = dev; in ab8500_btemp_probe()
723 di->parent = dev_get_drvdata(pdev->dev.parent); in ab8500_btemp_probe()
726 di->tz = thermal_zone_get_zone_by_name("battery-thermal"); in ab8500_btemp_probe()
727 if (IS_ERR(di->tz)) { in ab8500_btemp_probe()
728 ret = PTR_ERR(di->tz); in ab8500_btemp_probe()
733 if (ret == -ENODEV) in ab8500_btemp_probe()
734 ret = -EPROBE_DEFER; in ab8500_btemp_probe()
738 di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl"); in ab8500_btemp_probe()
739 if (IS_ERR(di->bat_ctrl)) { in ab8500_btemp_probe()
740 ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl), in ab8500_btemp_probe()
745 di->initialized = false; in ab8500_btemp_probe()
752 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work, in ab8500_btemp_probe()
756 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT; in ab8500_btemp_probe()
757 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT; in ab8500_btemp_probe()
768 di->btemp_ranges.btemp_high_limit = in ab8500_btemp_probe()
772 di->btemp_ranges.btemp_high_limit = in ab8500_btemp_probe()
776 di->btemp_ranges.btemp_high_limit = in ab8500_btemp_probe()
782 di->btemp_psy = devm_power_supply_register(dev, &ab8500_btemp_desc, in ab8500_btemp_probe()
784 if (IS_ERR(di->btemp_psy)) { in ab8500_btemp_probe()
786 return PTR_ERR(di->btemp_psy); in ab8500_btemp_probe()
811 list_add_tail(&di->node, &ab8500_btemp_list); in ab8500_btemp_probe()
818 component_del(&pdev->dev, &ab8500_btemp_component_ops); in ab8500_btemp_remove()
826 { .compatible = "stericsson,ab8500-btemp", },
835 .name = "ab8500-btemp",
842 MODULE_ALIAS("platform:ab8500-btemp");