Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Charging control driver for the Wilco EC
0004  *
0005  * Copyright 2019 Google LLC
0006  *
0007  * See Documentation/ABI/testing/sysfs-class-power and
0008  * Documentation/ABI/testing/sysfs-class-power-wilco for userspace interface
0009  * and other info.
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/platform_data/wilco-ec.h>
0015 #include <linux/power_supply.h>
0016 
0017 #define DRV_NAME "wilco-charger"
0018 
0019 /* Property IDs and related EC constants */
0020 #define PID_CHARGE_MODE     0x0710
0021 #define PID_CHARGE_LOWER_LIMIT  0x0711
0022 #define PID_CHARGE_UPPER_LIMIT  0x0712
0023 
0024 enum charge_mode {
0025     CHARGE_MODE_STD = 1,    /* Used for Standard */
0026     CHARGE_MODE_EXP = 2,    /* Express Charge, used for Fast */
0027     CHARGE_MODE_AC = 3, /* Mostly AC use, used for Trickle */
0028     CHARGE_MODE_AUTO = 4,   /* Used for Adaptive */
0029     CHARGE_MODE_CUSTOM = 5, /* Used for Custom */
0030     CHARGE_MODE_LONGLIFE = 6, /* Used for Long Life */
0031 };
0032 
0033 #define CHARGE_LOWER_LIMIT_MIN  50
0034 #define CHARGE_LOWER_LIMIT_MAX  95
0035 #define CHARGE_UPPER_LIMIT_MIN  55
0036 #define CHARGE_UPPER_LIMIT_MAX  100
0037 
0038 /* Convert from POWER_SUPPLY_PROP_CHARGE_TYPE value to the EC's charge mode */
0039 static int psp_val_to_charge_mode(int psp_val)
0040 {
0041     switch (psp_val) {
0042     case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
0043         return CHARGE_MODE_AC;
0044     case POWER_SUPPLY_CHARGE_TYPE_FAST:
0045         return CHARGE_MODE_EXP;
0046     case POWER_SUPPLY_CHARGE_TYPE_STANDARD:
0047         return CHARGE_MODE_STD;
0048     case POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE:
0049         return CHARGE_MODE_AUTO;
0050     case POWER_SUPPLY_CHARGE_TYPE_CUSTOM:
0051         return CHARGE_MODE_CUSTOM;
0052     case POWER_SUPPLY_CHARGE_TYPE_LONGLIFE:
0053         return CHARGE_MODE_LONGLIFE;
0054     default:
0055         return -EINVAL;
0056     }
0057 }
0058 
0059 /* Convert from EC's charge mode to POWER_SUPPLY_PROP_CHARGE_TYPE value */
0060 static int charge_mode_to_psp_val(enum charge_mode mode)
0061 {
0062     switch (mode) {
0063     case CHARGE_MODE_AC:
0064         return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
0065     case CHARGE_MODE_EXP:
0066         return POWER_SUPPLY_CHARGE_TYPE_FAST;
0067     case CHARGE_MODE_STD:
0068         return POWER_SUPPLY_CHARGE_TYPE_STANDARD;
0069     case CHARGE_MODE_AUTO:
0070         return POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE;
0071     case CHARGE_MODE_CUSTOM:
0072         return POWER_SUPPLY_CHARGE_TYPE_CUSTOM;
0073     case CHARGE_MODE_LONGLIFE:
0074         return POWER_SUPPLY_CHARGE_TYPE_LONGLIFE;
0075     default:
0076         return -EINVAL;
0077     }
0078 }
0079 
0080 static enum power_supply_property wilco_charge_props[] = {
0081     POWER_SUPPLY_PROP_CHARGE_TYPE,
0082     POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
0083     POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
0084 };
0085 
0086 static int wilco_charge_get_property(struct power_supply *psy,
0087                      enum power_supply_property psp,
0088                      union power_supply_propval *val)
0089 {
0090     struct wilco_ec_device *ec = power_supply_get_drvdata(psy);
0091     u32 property_id;
0092     int ret;
0093     u8 raw;
0094 
0095     switch (psp) {
0096     case POWER_SUPPLY_PROP_CHARGE_TYPE:
0097         property_id = PID_CHARGE_MODE;
0098         break;
0099     case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
0100         property_id = PID_CHARGE_LOWER_LIMIT;
0101         break;
0102     case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
0103         property_id = PID_CHARGE_UPPER_LIMIT;
0104         break;
0105     default:
0106         return -EINVAL;
0107     }
0108 
0109     ret = wilco_ec_get_byte_property(ec, property_id, &raw);
0110     if (ret < 0)
0111         return ret;
0112     if (property_id == PID_CHARGE_MODE) {
0113         ret = charge_mode_to_psp_val(raw);
0114         if (ret < 0)
0115             return -EBADMSG;
0116         raw = ret;
0117     }
0118     val->intval = raw;
0119 
0120     return 0;
0121 }
0122 
0123 static int wilco_charge_set_property(struct power_supply *psy,
0124                      enum power_supply_property psp,
0125                      const union power_supply_propval *val)
0126 {
0127     struct wilco_ec_device *ec = power_supply_get_drvdata(psy);
0128     int mode;
0129 
0130     switch (psp) {
0131     case POWER_SUPPLY_PROP_CHARGE_TYPE:
0132         mode = psp_val_to_charge_mode(val->intval);
0133         if (mode < 0)
0134             return -EINVAL;
0135         return wilco_ec_set_byte_property(ec, PID_CHARGE_MODE, mode);
0136     case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
0137         if (val->intval < CHARGE_LOWER_LIMIT_MIN ||
0138             val->intval > CHARGE_LOWER_LIMIT_MAX)
0139             return -EINVAL;
0140         return wilco_ec_set_byte_property(ec, PID_CHARGE_LOWER_LIMIT,
0141                           val->intval);
0142     case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
0143         if (val->intval < CHARGE_UPPER_LIMIT_MIN ||
0144             val->intval > CHARGE_UPPER_LIMIT_MAX)
0145             return -EINVAL;
0146         return wilco_ec_set_byte_property(ec, PID_CHARGE_UPPER_LIMIT,
0147                           val->intval);
0148     default:
0149         return -EINVAL;
0150     }
0151 }
0152 
0153 static int wilco_charge_property_is_writeable(struct power_supply *psy,
0154                           enum power_supply_property psp)
0155 {
0156     return 1;
0157 }
0158 
0159 static const struct power_supply_desc wilco_ps_desc = {
0160     .properties     = wilco_charge_props,
0161     .num_properties     = ARRAY_SIZE(wilco_charge_props),
0162     .get_property       = wilco_charge_get_property,
0163     .set_property       = wilco_charge_set_property,
0164     .property_is_writeable  = wilco_charge_property_is_writeable,
0165     .name           = DRV_NAME,
0166     .type           = POWER_SUPPLY_TYPE_MAINS,
0167 };
0168 
0169 static int wilco_charge_probe(struct platform_device *pdev)
0170 {
0171     struct wilco_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
0172     struct power_supply_config psy_cfg = {};
0173     struct power_supply *psy;
0174 
0175     psy_cfg.drv_data = ec;
0176     psy = devm_power_supply_register(&pdev->dev, &wilco_ps_desc, &psy_cfg);
0177 
0178     return PTR_ERR_OR_ZERO(psy);
0179 }
0180 
0181 static struct platform_driver wilco_charge_driver = {
0182     .probe  = wilco_charge_probe,
0183     .driver = {
0184         .name = DRV_NAME,
0185     }
0186 };
0187 module_platform_driver(wilco_charge_driver);
0188 
0189 MODULE_ALIAS("platform:" DRV_NAME);
0190 MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
0191 MODULE_LICENSE("GPL v2");
0192 MODULE_DESCRIPTION("Wilco EC charge control driver");