Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Battery driver for the Ingenic JZ47xx SoCs
0004  * Copyright (c) 2019 Artur Rojek <contact@artur-rojek.eu>
0005  *
0006  * based on drivers/power/supply/jz4740-battery.c
0007  */
0008 
0009 #include <linux/iio/consumer.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/power_supply.h>
0014 #include <linux/property.h>
0015 
0016 struct ingenic_battery {
0017     struct device *dev;
0018     struct iio_channel *channel;
0019     struct power_supply_desc desc;
0020     struct power_supply *battery;
0021     struct power_supply_battery_info *info;
0022 };
0023 
0024 static int ingenic_battery_get_property(struct power_supply *psy,
0025                     enum power_supply_property psp,
0026                     union power_supply_propval *val)
0027 {
0028     struct ingenic_battery *bat = power_supply_get_drvdata(psy);
0029     struct power_supply_battery_info *info = bat->info;
0030     int ret;
0031 
0032     switch (psp) {
0033     case POWER_SUPPLY_PROP_HEALTH:
0034         ret = iio_read_channel_processed(bat->channel, &val->intval);
0035         val->intval *= 1000;
0036         if (val->intval < info->voltage_min_design_uv)
0037             val->intval = POWER_SUPPLY_HEALTH_DEAD;
0038         else if (val->intval > info->voltage_max_design_uv)
0039             val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
0040         else
0041             val->intval = POWER_SUPPLY_HEALTH_GOOD;
0042         return ret;
0043     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0044         ret = iio_read_channel_processed(bat->channel, &val->intval);
0045         val->intval *= 1000;
0046         return ret;
0047     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0048         val->intval = info->voltage_min_design_uv;
0049         return 0;
0050     case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0051         val->intval = info->voltage_max_design_uv;
0052         return 0;
0053     default:
0054         return -EINVAL;
0055     }
0056 }
0057 
0058 /* Set the most appropriate IIO channel voltage reference scale
0059  * based on the battery's max voltage.
0060  */
0061 static int ingenic_battery_set_scale(struct ingenic_battery *bat)
0062 {
0063     const int *scale_raw;
0064     int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
0065     u64 max_mV;
0066 
0067     ret = iio_read_max_channel_raw(bat->channel, &max_raw);
0068     if (ret) {
0069         dev_err(bat->dev, "Unable to read max raw channel value\n");
0070         return ret;
0071     }
0072 
0073     ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
0074                            &scale_type, &scale_len,
0075                            IIO_CHAN_INFO_SCALE);
0076     if (ret < 0) {
0077         dev_err(bat->dev, "Unable to read channel avail scale\n");
0078         return ret;
0079     }
0080     if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2)
0081         return -EINVAL;
0082 
0083     max_mV = bat->info->voltage_max_design_uv / 1000;
0084 
0085     for (i = 0; i < scale_len; i += 2) {
0086         u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
0087 
0088         if (scale_mV < max_mV)
0089             continue;
0090 
0091         if (best_idx >= 0 && scale_mV > best_mV)
0092             continue;
0093 
0094         best_mV = scale_mV;
0095         best_idx = i;
0096     }
0097 
0098     if (best_idx < 0) {
0099         dev_err(bat->dev, "Unable to find matching voltage scale\n");
0100         return -EINVAL;
0101     }
0102 
0103     /* Only set scale if there is more than one (fractional) entry */
0104     if (scale_len > 2) {
0105         ret = iio_write_channel_attribute(bat->channel,
0106                           scale_raw[best_idx],
0107                           scale_raw[best_idx + 1],
0108                           IIO_CHAN_INFO_SCALE);
0109         if (ret)
0110             return ret;
0111     }
0112 
0113     return 0;
0114 }
0115 
0116 static enum power_supply_property ingenic_battery_properties[] = {
0117     POWER_SUPPLY_PROP_HEALTH,
0118     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0119     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0120     POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
0121 };
0122 
0123 static int ingenic_battery_probe(struct platform_device *pdev)
0124 {
0125     struct device *dev = &pdev->dev;
0126     struct ingenic_battery *bat;
0127     struct power_supply_config psy_cfg = {};
0128     struct power_supply_desc *desc;
0129     int ret;
0130 
0131     bat = devm_kzalloc(dev, sizeof(*bat), GFP_KERNEL);
0132     if (!bat)
0133         return -ENOMEM;
0134 
0135     bat->dev = dev;
0136     bat->channel = devm_iio_channel_get(dev, "battery");
0137     if (IS_ERR(bat->channel))
0138         return PTR_ERR(bat->channel);
0139 
0140     desc = &bat->desc;
0141     desc->name = "jz-battery";
0142     desc->type = POWER_SUPPLY_TYPE_BATTERY;
0143     desc->properties = ingenic_battery_properties;
0144     desc->num_properties = ARRAY_SIZE(ingenic_battery_properties);
0145     desc->get_property = ingenic_battery_get_property;
0146     psy_cfg.drv_data = bat;
0147     psy_cfg.of_node = dev->of_node;
0148 
0149     bat->battery = devm_power_supply_register(dev, desc, &psy_cfg);
0150     if (IS_ERR(bat->battery))
0151         return dev_err_probe(dev, PTR_ERR(bat->battery),
0152                      "Unable to register battery\n");
0153 
0154     ret = power_supply_get_battery_info(bat->battery, &bat->info);
0155     if (ret) {
0156         dev_err(dev, "Unable to get battery info: %d\n", ret);
0157         return ret;
0158     }
0159     if (bat->info->voltage_min_design_uv < 0) {
0160         dev_err(dev, "Unable to get voltage min design\n");
0161         return bat->info->voltage_min_design_uv;
0162     }
0163     if (bat->info->voltage_max_design_uv < 0) {
0164         dev_err(dev, "Unable to get voltage max design\n");
0165         return bat->info->voltage_max_design_uv;
0166     }
0167 
0168     return ingenic_battery_set_scale(bat);
0169 }
0170 
0171 #ifdef CONFIG_OF
0172 static const struct of_device_id ingenic_battery_of_match[] = {
0173     { .compatible = "ingenic,jz4740-battery", },
0174     { },
0175 };
0176 MODULE_DEVICE_TABLE(of, ingenic_battery_of_match);
0177 #endif
0178 
0179 static struct platform_driver ingenic_battery_driver = {
0180     .driver = {
0181         .name = "ingenic-battery",
0182         .of_match_table = of_match_ptr(ingenic_battery_of_match),
0183     },
0184     .probe = ingenic_battery_probe,
0185 };
0186 module_platform_driver(ingenic_battery_driver);
0187 
0188 MODULE_DESCRIPTION("Battery driver for Ingenic JZ47xx SoCs");
0189 MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
0190 MODULE_LICENSE("GPL");