0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/err.h>
0011 #include <linux/power_supply.h>
0012 #include <linux/adb.h>
0013 #include <linux/pmu.h>
0014 #include <linux/slab.h>
0015
0016 static struct pmu_battery_dev {
0017 struct power_supply *bat;
0018 struct power_supply_desc bat_desc;
0019 struct pmu_battery_info *pbi;
0020 char name[16];
0021 int propval;
0022 } *pbats[PMU_MAX_BATTERIES];
0023
0024 #define to_pmu_battery_dev(x) power_supply_get_drvdata(x)
0025
0026
0027
0028
0029
0030 static int pmu_get_ac_prop(struct power_supply *psy,
0031 enum power_supply_property psp,
0032 union power_supply_propval *val)
0033 {
0034 switch (psp) {
0035 case POWER_SUPPLY_PROP_ONLINE:
0036 val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
0037 (pmu_battery_count == 0);
0038 break;
0039 default:
0040 return -EINVAL;
0041 }
0042
0043 return 0;
0044 }
0045
0046 static enum power_supply_property pmu_ac_props[] = {
0047 POWER_SUPPLY_PROP_ONLINE,
0048 };
0049
0050 static const struct power_supply_desc pmu_ac_desc = {
0051 .name = "pmu-ac",
0052 .type = POWER_SUPPLY_TYPE_MAINS,
0053 .properties = pmu_ac_props,
0054 .num_properties = ARRAY_SIZE(pmu_ac_props),
0055 .get_property = pmu_get_ac_prop,
0056 };
0057
0058 static struct power_supply *pmu_ac;
0059
0060
0061
0062
0063
0064 static char *pmu_batt_types[] = {
0065 "Smart", "Comet", "Hooper", "Unknown"
0066 };
0067
0068 static char *pmu_bat_get_model_name(struct pmu_battery_info *pbi)
0069 {
0070 switch (pbi->flags & PMU_BATT_TYPE_MASK) {
0071 case PMU_BATT_TYPE_SMART:
0072 return pmu_batt_types[0];
0073 case PMU_BATT_TYPE_COMET:
0074 return pmu_batt_types[1];
0075 case PMU_BATT_TYPE_HOOPER:
0076 return pmu_batt_types[2];
0077 default: break;
0078 }
0079 return pmu_batt_types[3];
0080 }
0081
0082 static int pmu_bat_get_property(struct power_supply *psy,
0083 enum power_supply_property psp,
0084 union power_supply_propval *val)
0085 {
0086 struct pmu_battery_dev *pbat = to_pmu_battery_dev(psy);
0087 struct pmu_battery_info *pbi = pbat->pbi;
0088
0089 switch (psp) {
0090 case POWER_SUPPLY_PROP_STATUS:
0091 if (pbi->flags & PMU_BATT_CHARGING)
0092 val->intval = POWER_SUPPLY_STATUS_CHARGING;
0093 else if (pmu_power_flags & PMU_PWR_AC_PRESENT)
0094 val->intval = POWER_SUPPLY_STATUS_FULL;
0095 else
0096 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0097 break;
0098 case POWER_SUPPLY_PROP_PRESENT:
0099 val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
0100 break;
0101 case POWER_SUPPLY_PROP_MODEL_NAME:
0102 val->strval = pmu_bat_get_model_name(pbi);
0103 break;
0104 case POWER_SUPPLY_PROP_ENERGY_AVG:
0105 val->intval = pbi->charge * 1000;
0106 break;
0107 case POWER_SUPPLY_PROP_ENERGY_FULL:
0108 val->intval = pbi->max_charge * 1000;
0109 break;
0110 case POWER_SUPPLY_PROP_CURRENT_AVG:
0111 val->intval = pbi->amperage * 1000;
0112 break;
0113 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
0114 val->intval = pbi->voltage * 1000;
0115 break;
0116 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
0117 val->intval = pbi->time_remaining;
0118 break;
0119 default:
0120 return -EINVAL;
0121 }
0122
0123 return 0;
0124 }
0125
0126 static enum power_supply_property pmu_bat_props[] = {
0127 POWER_SUPPLY_PROP_STATUS,
0128 POWER_SUPPLY_PROP_PRESENT,
0129 POWER_SUPPLY_PROP_MODEL_NAME,
0130 POWER_SUPPLY_PROP_ENERGY_AVG,
0131 POWER_SUPPLY_PROP_ENERGY_FULL,
0132 POWER_SUPPLY_PROP_CURRENT_AVG,
0133 POWER_SUPPLY_PROP_VOLTAGE_AVG,
0134 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
0135 };
0136
0137
0138
0139
0140
0141 static struct platform_device *bat_pdev;
0142
0143 static int __init pmu_bat_init(void)
0144 {
0145 int ret = 0;
0146 int i;
0147
0148 bat_pdev = platform_device_register_simple("pmu-battery",
0149 0, NULL, 0);
0150 if (IS_ERR(bat_pdev)) {
0151 ret = PTR_ERR(bat_pdev);
0152 goto pdev_register_failed;
0153 }
0154
0155 pmu_ac = power_supply_register(&bat_pdev->dev, &pmu_ac_desc, NULL);
0156 if (IS_ERR(pmu_ac)) {
0157 ret = PTR_ERR(pmu_ac);
0158 goto ac_register_failed;
0159 }
0160
0161 for (i = 0; i < pmu_battery_count; i++) {
0162 struct power_supply_config psy_cfg = {};
0163 struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
0164 GFP_KERNEL);
0165 if (!pbat)
0166 break;
0167
0168 sprintf(pbat->name, "PMU_battery_%d", i);
0169 pbat->bat_desc.name = pbat->name;
0170 pbat->bat_desc.properties = pmu_bat_props;
0171 pbat->bat_desc.num_properties = ARRAY_SIZE(pmu_bat_props);
0172 pbat->bat_desc.get_property = pmu_bat_get_property;
0173 pbat->pbi = &pmu_batteries[i];
0174 psy_cfg.drv_data = pbat;
0175
0176 pbat->bat = power_supply_register(&bat_pdev->dev,
0177 &pbat->bat_desc,
0178 &psy_cfg);
0179 if (IS_ERR(pbat->bat)) {
0180 ret = PTR_ERR(pbat->bat);
0181 kfree(pbat);
0182 goto battery_register_failed;
0183 }
0184 pbats[i] = pbat;
0185 }
0186
0187 goto success;
0188
0189 battery_register_failed:
0190 while (i--) {
0191 if (!pbats[i])
0192 continue;
0193 power_supply_unregister(pbats[i]->bat);
0194 kfree(pbats[i]);
0195 }
0196 power_supply_unregister(pmu_ac);
0197 ac_register_failed:
0198 platform_device_unregister(bat_pdev);
0199 pdev_register_failed:
0200 success:
0201 return ret;
0202 }
0203
0204 static void __exit pmu_bat_exit(void)
0205 {
0206 int i;
0207
0208 for (i = 0; i < PMU_MAX_BATTERIES; i++) {
0209 if (!pbats[i])
0210 continue;
0211 power_supply_unregister(pbats[i]->bat);
0212 kfree(pbats[i]);
0213 }
0214 power_supply_unregister(pmu_ac);
0215 platform_device_unregister(bat_pdev);
0216 }
0217
0218 module_init(pmu_bat_init);
0219 module_exit(pmu_bat_exit);
0220
0221 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
0222 MODULE_LICENSE("GPL");
0223 MODULE_DESCRIPTION("PMU battery driver");