1
0
Fork 0
Commit Graph

50 Commits (26d7f2f9dffccafb99e3610e418003687dba80c6)

Author SHA1 Message Date
Lars Ivar Miljeteig 26d7f2f9df max77818: Add licensing info 2020-10-08 20:41:29 +02:00
Lars Ivar Miljeteig a357ab051e power: max77818_battery: Handle errors
- Handle return code for regmap_write & regmap_read
- Rename some functions with misleading names
  i.e "verify" when it actually writes, differentiate
  between reading params from chip or device tree, etc
- Don't clear POR bit if writing data failed
- Set init complete even if "write custom params" fail,
  as it will eventually time out and continue init anyhow
2020-10-08 20:41:29 +02:00
Steinar Bakkemo ba81965560 max77818-battery: add boot-time verification for critical FG params 2020-10-08 20:41:29 +02:00
Steinar Bakkemo 618b6d5751 max77818-battery: add locking option for all custom params
In order to be able to synchronize initial register write with other
drivers possibly accessing the same registers during init, an option
to apply a mutex lock (defined at the mfd level) is introduced.

The config register is also given a requirement to be locked, as this
register holds the FGCC bit which is the main cause of this option.
2020-10-08 20:41:28 +02:00
Steinar Bakkemo ea9cb1b6b8 max77818-battery: skip default verify/write for all custom params
Due to uncertainty related to how safe it is to verify each custom
param during every boot, set the skip_verify flag for every custom param except
for the config register which is verified against current DT and reset if changed
for some reason since last boot.

This ensures that if an operation performed through the max77818 charger driver
was aborted by a reboot for instance, and the FGCC bit remained 0, it will be
restored to 1 after a reboot.
2020-10-08 20:41:28 +02:00
Steinar Bakkemo 19d33b0ba8 max77818-battery: add skip flag to learn-cfg custom param 2020-10-08 20:41:28 +02:00
Steinar Bakkemo 4fdb9ac163 max77818-battery: do initial max current adj. during boot (after other init)
Due to the normal FG driver init configuring FGCC to be enabled during its normal
run, where all custom params configured in DT are verified and restored to the
original value if changed since last boot, charger driver sync has to be done
after completion of normal init.

This is now done in a separate worker which waits for the other init to complete
using a simple completion obj.

Also, unlock mutex at early return errors.
2020-10-08 20:41:28 +02:00
Steinar Bakkemo d5cc3519ad max77818-battery: fix initial status_ex read bug
The status_ex is read during driver initiation, but the local shadow
variable was not updated, causing the status_ex to be "not connected"
even if the device was powered on with USB-C charger connected.max77818-battery: fix initial status_ex read bug
2020-10-08 20:41:28 +02:00
Steinar Bakkemo 68e34eecb1 max77818-battery: add init routine for charger init ops and disable chgin by def
Operations done during FG driver initiation which address the charger driver is
collected in new init routine, which now reads initial status information required
to be shadowed in the FG driver (status_ex) and disables the chgin interface by
default.
2020-10-08 20:41:27 +02:00
Steinar Bakkemo 815f0aad7f max77818-battery/charger: disable chgin (CHGINSEL=0) when max current=0
When charger driver receives a request to set max current for the chgin
interface to 0, the CHGINSEL bit is set to 0 to disable the interface.

When it receives a request to set a non-zero max current for the chgin
interface, the CHGINSEL bit is reset to 1 to re-enable the interface.

When the FG driver receives a notification about a connection change
for the chgin interface, it set the max current for the chgin interface
to 0 (i.e turning it off) through the charger driver.
2020-10-08 20:41:27 +02:00
Steinar Bakkemo 583e35efdf max77818-battery: fix usb1/usb2 references => chgin/wcin 2020-10-08 20:41:26 +02:00
Steinar Bakkemo a244fdca79 max77818-battery: limit max current to 100 mA for chgin interface
As a protection against pirate accessory charger equipment, the max
charge current is always limited to 100 mA for the chgin interface.
2020-10-08 20:41:26 +02:00
Steinar Bakkemo 6304416131 max77818-battery: add errorhandling/deallocation before exiting probe with error 2020-10-08 20:41:26 +02:00
Steinar Bakkemo bcfc06b15e max77818-battery: aplit up probe to clarify initiation process 2020-10-08 20:41:26 +02:00
Steinar Bakkemo 79237edd4e max77818-battery: add support for new usb_safe_max_current DT param
Use new usb_safe_max_current DT param to set default max current during init,
and not hard-coded value of 500 mA.
2020-10-08 20:41:26 +02:00
Steinar Bakkemo 89215a876d max77818-battery: minor debug comment fixup 2020-10-08 20:41:26 +02:00
Steinar Bakkemo 1af382239d max77818-battery: fix what seems to be a bug that might cause race during probe
Clear and not set the init_complete flag at the start of probe, before
power supply device is registered.
2020-10-08 20:41:26 +02:00
Steinar Bakkemo e6013dc800 max77818-mfd/battery/charger: move charger IRQ handling to battery driver
- Do initiation of the charger irq regmap in the MFD driver while disabling FGCC
- Initiate charger interrupts in battery driver while disabling FGCC
- Add new charger irq handler in battery driver requesting status_ex
  property from charger driver while disabling FGCC
  (enabling separate status for chgin/wcin)
2020-10-08 20:41:26 +02:00
Steinar Bakkemo fa988b33eb max77818-battery/battery-utils: move FGCC handling to new max77818-utils module
In order for the FGCC flag to be flipped from modules/drivers external to the
max77818-battery driver, the FGCC enable/disable routine and corresponding
macro doing a non-FGCC operation by flipping the FGCC bit to disable FGCC,
do the operation and re-enable FGCC is moved to an external module max77818-utils.
2020-10-08 20:41:25 +02:00
Steinar Bakkemo c97f033c81 max77818-battery: add MAX77818_DO_NON_FGCC_OP macro to standardize FGCC flip ops
All operations performed by charger driver which require FGCC mode to be disabled
in order for the driver to be able to communicate with the charger device
basically do the following which now are done with the MAX77818_DO_NON_FGCC_OP
macro:

Disable FGCC
Do required operation
Re-enable FGCC
2020-10-08 20:41:25 +02:00
Steinar Bakkemo 36b8388b08 max77818-battery/charger: replace POGO/USB-C refs with chgin/wcin 2020-10-08 20:41:25 +02:00
Steinar Bakkemo f23dba4f13 max77818-battery: add usb-phy notification handling for dynamic max current adj.
The max77818-charger driver is used to set updated max current on given
interface, flipping the FGCC bit appropriately to enable charger device config
temporarily.
2020-10-08 20:41:25 +02:00
Steinar Bakkemo 3e7f312291 max77818-battery: improve FGCC enable/disable (configurable delay)
Add post_fgcc_change_delay_us module parameter with default value
100000 us.

Move delay into FGCC enable/disable function.
Add delay both after FGCC enable and disable.
2020-10-08 20:41:23 +02:00
Steinar Bakkemo 03c05f6d75 max77818-battery: fix mixed up lock/write/unlock => unlock/write/lock 2020-10-08 20:41:23 +02:00
Steinar Bakkemo 15bc7fa6cd max77818-battery: set 'is_learn_value' for iavg-min register 2020-10-08 20:41:23 +02:00
Steinar Bakkemo adb89d3867 max77818-battery: set 'is_learn_value' flag for dpacc and dqacc registers
As the dpacc and dqacc registers change over time, these are skipped in addition
to other learned values to prevent them from being overwritten on every boot.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo 889eb9a32b max77818-battery: add unlocking of extra config registers in verify_custom_patems
Unlock and re-lock of extra config registers were separated into common
functions to be used both in initial write_custom_params and new verify/write
routine.

Verification of the RelaxCfg register was also added to the new verify/write routine.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo 96de964264 max77818-battery: enable default config-update mode (verify)
- Set 'verify' as the default config-update mode
- Add flag in max77818_of_property (is_learned_value) in order to prevent config
  values given in DT as initial values that is expected to change from being
  reset when device reboots and params are synced to curent DT.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo 390c586d4e max77818-battery: add synchronized access to init_done flag
The power_supply get_property handler is protected by a flag indicating
when the config init/update thread is complete, but this flag was not
synchronized.

In addition to this, the flag is now cleared before the driver is registered
as a power_supply device to properly tell the power_supply initiation to wait.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo 2334273c25 max77818-battery: add config_update type 'verify'
If max77818.config_update=verify, and POR is not set, all custom params will
be verified against current DT configuration and updated if required.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo 4b6c4e8daa max77818-battery: add FG config update params
Add module parameter enabling complete FG config rewrite from DT or
only custom params update from DT depending on what is required.

The driver may be given either "max77818_battery.config_update=complete" or
"max77818_battery.config_update=partial" respectively through the kernel commandline
from u-boot.

Versioning scheme may then be implemented outside of the kernel if required,
and which update to apply to a given device will then be chosen by update scripts.
2020-10-08 20:41:22 +02:00
Steinar Bakkemo fbd41b2b95 max77818-battery: bugfix that was missed before merge (FGCC support) 2020-10-08 20:41:21 +02:00
Steinar Bakkemo bee9783e9d max77818-battery/charger: fix IRQ init issue (charger)
Fix flaw in probe causing IRQ init to be performed in FGCC mode and not when
FGCC is disabled (which is the intension).

Simplify FGCC re-enable code during FG driver init.
2020-10-08 20:41:21 +02:00
Steinar Bakkemo 064253197a max77818-battery: add missing mutex init 2020-10-08 20:41:21 +02:00
Steinar Bakkemo 9a79902db6 max77818-battery/charger: changes as requested in PR
MAX77818 MFD driver:
- Let drvdata->regmap_chg remain NULL if not initialized,
  for simple validity check
- Remove no longer used label

MAX77818 charger driver:
- Indicate error if chgin_stat_gpio state/wcin_stat_gpio states cannot be read
- Let chgin_stat_gpio/wcin_stat_gpio remain NULL if not initialized,
  for simple validity check
- Define MODE_ALL_OFF = 0x00 (POWER_SUPPLY_MODE_ALL_OFF)
- Add initial charger_mode read from device during initialization,
  and set to Charger if unknown mode is read
  (not corresponding to defined power supply charger modes)
- charger_update routine no longer returns a value

MAX77818 battery/FG driver:
- Define CONFIG_FGCC_BIT
- Add mutex to drvdata
- Improve variable naming
- Add mutex locking to charger_mode get/set functions
- Add proper FGCC mode restoration check in charger_mode get function
- Add warnings if regmap read/writes fails during custom param init
2020-10-08 20:41:20 +02:00
Steinar Bakkemo 1ee8bde61f max77818-battery/charger: add support for charger_mode readback through FG drv 2020-10-08 20:41:20 +02:00
Steinar Bakkemo 68b21489b5 max77818-battery: add charger_mode property to be forwarded to charger
charger_mode property was added to FG driver in order to disable
FGCC mode before forwarding the property change request to the charger driver,
before turning the FGCC mode back on after successfully changing the charger mode.
2020-10-08 20:41:20 +02:00
Steinar Bakkemo bf1959d8ef max77818-battery: fix "comma+space" issue in custom_param_list (PR comment) 2020-10-08 20:41:20 +02:00
Steinar Bakkemo efb95553a4 max77818-battery: improve custom property retrieval from DT
Parameters are verified, and not-given parameters now results in
a warning and default values are kept instead of writing 0 for all
properties not found in DT.
2020-10-08 20:41:20 +02:00
Steinar Bakkemo e6be52aeed max77818-battery: add debug output/warnings in DT parameter parsing
Optional debug output when parameter is written.
Warning if parameter is not given.
Warning if paraeter is given, but value is not expected format (not u16).
2020-10-08 20:41:20 +02:00
Steinar Bakkemo 41f6aa6aa3 max77818-battery: add support for DT params related to smart charging/FGCC
Add support for new params defined in DT (register definitions/driver init):
    TAlrtTh
    TAlrtTh2
    JEITA_Curr
    JEITA_Volt
    ChargeState0..7
2020-10-08 20:41:20 +02:00
Shawn Guo 092e8c9a38 max77818_battery: work around battery presence issue
MAX77818 battery presence STATUS bit is not working on our device for
some reason unknown yet. We are working around the issue here by reading
temperature register, in which a negative value indicates absence of
battery.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 20:41:18 +02:00
Steinar Bakkemo e9e3814946 max77818-battery: add support for tGain/tOFF/tCurve FG params
Read tGain/tOFF/tCurve from DT during driver init.

Note:
FG/battery driver has to re-init the device for changes to occur on
already configured devices, and this is normally only done right after
initial power-on with un-configured device.

A solution for this is however under development, which will make sure
that selected FG parameters are validated during each boot.
2020-10-08 20:41:18 +02:00
Shawn Guo 71ccb636fb max77818_battery: add SOC alert event support
The MAX77818 SOC max/min alert is being used to report 1% SOC change.
It's not such an useful event.  We need to use the alert to report low
battery event to user space.  This patch drops 1% SOC reporting and uses
the max/min alert to report battery SOC event to user space by calling
sysfs_notify() on corresponding sysfs entry.

It disables max SOC alert and set min SOC alert as 10% by default, but
the value can be updated by writing desired value into

 /sys/class/power_supply/max77818_battery/capacity_alert_min
 /sys/class/power_supply/max77818_battery/capacity_alert_max

Saying we have capacity_alert_min be 10, once SOC goes to 9, INTR_SOCMIN
will be triggered, and the user space program polling capacity_alert_min
sysfs entry will return successfully.

While dropping max77818_set_soc_threshold(), .suspend/.resume hooks are
removed altogether, as enable_irq_wake() has been handled by MAX77818
mfd driver, and disable_irq() has been handled by PM core anyway.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 20:07:16 +02:00
Shawn Guo ac13f89f6f max77818_battery: drop some unhelpful properties
Right now, max77818_battery driver already has 31 sysfs entries
(and thus uevents for power_supply class), which is reaching the
maximum number 32 (see UEVENT_NUM_ENVP in include/linux/kobject.h).
So when trying to add more properties, we will get the following
kernel WARNING in calling power_supply_uevent() -> add_uevent_var().

  WARNING: CPU: 1 PID: 93 at lib/kobject_uevent.c:568 add_uevent_var+0xa4/0xcc
  add_uevent_var: too many keys

Let's drop some unhelpful properties which do not reflect any hardware
states to leave room for other new useful properties.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 20:07:16 +02:00
Shawn Guo 5d28b95c80 battery: improve the comment in max77818_resume()
Improve the comment on max77818_set_soc_threshold() call in
max77818_resume(), to make it clear why we need to reprogram SOC
threshold when we are back from suspend/sleep state.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 01:38:40 +02:00
Shawn Guo 9a508cfbbf power: supply: add max77818 battery driver
The max77818 battery block is simillar to max17042_battery, so we choose
to create max77818_battery by reusing majority of max17042_battery
driver code.

The max77818_battery reuses header max17042_battery.h directly and adds
max77818 specific defines in there.  But we do not reuse max17042_battery.c
directly, because it supports I2C device probing while we need platform
device probing.  Also the startup sequence and initialization is quite
different between max17042 and max77818. So we end up creating a new
max77818_battery driver with max77818 specific custom parameter parsing
from DT and startup sequence implementation.  The majority of
.get_property/.set_property code are copied from max17042 driver, and
we support two more properties, TIME_TO_EMPTY_NOW and TIME_TO_FULL_NOW,
with max77818 driver though.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 01:38:39 +02:00
Shawn Guo 4b7d7344ce power: supply: remove max77818_battery driver
The driver is buggy and not functional at all.  Let's remove it and
start over again.

Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2020-10-08 01:38:39 +02:00
Steinar Bakkemo 0b65333af4 max77818: fix power supply reg. issue causing kernal panic/add sep. max cur. adj
Extract powersupply config to static data structure.
Add support for setting max current separately for chgin/wcin.
2020-10-08 01:37:33 +02:00
Steinar Bakkemo e4c9b4df47 max77818: add max77818 driver (driver received from Maxim, ported to 4.14.79 kernel) 2020-10-08 01:37:04 +02:00