123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
- #define pr_fmt(fmt) "%s: " fmt, __func__
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/platform_device.h>
- #include <linux/errno.h>
- #include <linux/device.h>
- #include <linux/power_supply.h>
- #include <linux/delay.h>
- #include <linux/slab.h>
- #define BCL_DEV_NAME "battery_current_limit"
- #define BCL_NAME_LENGTH 20
- /*
- * Default BCL poll interval 1000 msec
- */
- #define BCL_POLL_INTERVAL 1000
- /*
- * Mininum BCL poll interval 10 msec
- */
- #define MIN_BCL_POLL_INTERVAL 10
- #define BATTERY_VOLTAGE_MIN 3400
- static const char bcl_type[] = "bcl";
- /*
- * Battery Current Limit Enable or Not
- */
- enum bcl_device_mode {
- BCL_DEVICE_DISABLED = 0,
- BCL_DEVICE_ENABLED,
- };
- /*
- * Battery Current Limit Iavail Threshold Mode set
- */
- enum bcl_iavail_threshold_mode {
- BCL_IAVAIL_THRESHOLD_DISABLED = 0,
- BCL_IAVAIL_THRESHOLD_ENABLED,
- };
- /*
- * Battery Current Limit Iavail Threshold Mode
- */
- enum bcl_iavail_threshold_type {
- BCL_IAVAIL_LOW_THRESHOLD_TYPE = 0,
- BCL_IAVAIL_HIGH_THRESHOLD_TYPE,
- BCL_IAVAIL_THRESHOLD_TYPE_MAX,
- };
- /**
- * BCL control block
- *
- */
- struct bcl_context {
- /* BCL device */
- struct device *dev;
- /* BCL related config parameter */
- /* BCL mode enable or not */
- enum bcl_device_mode bcl_mode;
- /* BCL Iavail Threshold Activate or Not */
- enum bcl_iavail_threshold_mode
- bcl_threshold_mode[BCL_IAVAIL_THRESHOLD_TYPE_MAX];
- /* BCL Iavail Threshold value in milli Amp */
- int bcl_threshold_value_ma[BCL_IAVAIL_THRESHOLD_TYPE_MAX];
- /* BCL Type */
- char bcl_type[BCL_NAME_LENGTH];
- /* BCL poll in msec */
- int bcl_poll_interval_msec;
- /* BCL realtime value based on poll */
- /* BCL realtime vbat in mV*/
- int bcl_vbat_mv;
- /* BCL realtime rbat in mOhms*/
- int bcl_rbat_mohm;
- /*BCL realtime iavail in milli Amp*/
- int bcl_iavail;
- /*BCL vbatt min in mV*/
- int bcl_vbat_min;
- /* BCL period poll delay work structure */
- struct delayed_work bcl_iavail_work;
- };
- static struct bcl_context *gbcl;
- static int bcl_get_battery_voltage(int *vbatt_mv)
- {
- static struct power_supply *psy;
- union power_supply_propval ret = {0,};
- if (psy == NULL) {
- psy = power_supply_get_by_name("battery");
- if (psy == NULL) {
- pr_err("failed to get ps battery\n");
- return -EINVAL;
- }
- }
- if (psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &ret))
- return -EINVAL;
- if (ret.intval <= 0)
- return -EINVAL;
- *vbatt_mv = ret.intval / 1000;
- return 0;
- }
- static int bcl_get_resistance(int *rbatt_mohm)
- {
- static struct power_supply *psy;
- union power_supply_propval ret = {0,};
- if (psy == NULL) {
- psy = power_supply_get_by_name("bms");
- if (psy == NULL) {
- pr_err("failed to get ps bms\n");
- return -EINVAL;
- }
- }
- if (psy->get_property(psy, POWER_SUPPLY_PROP_RESISTANCE, &ret))
- return -EINVAL;
- if (ret.intval <= 0)
- return -EINVAL;
- *rbatt_mohm = ret.intval / 1000;
- return 0;
- }
- /*
- * BCL iavail calculation and trigger notification to user space
- * if iavail cross threshold
- */
- static void bcl_calculate_iavail_trigger(void)
- {
- int iavail_ma = 0;
- int vbatt_mv;
- int rbatt_mohm;
- bool threshold_cross = false;
- if (!gbcl) {
- pr_err("called before initialization\n");
- return;
- }
- if (bcl_get_battery_voltage(&vbatt_mv))
- return;
- if (bcl_get_resistance(&rbatt_mohm))
- return;
- iavail_ma = (vbatt_mv - gbcl->bcl_vbat_min) * 1000 / rbatt_mohm;
- gbcl->bcl_rbat_mohm = rbatt_mohm;
- gbcl->bcl_vbat_mv = vbatt_mv;
- gbcl->bcl_iavail = iavail_ma;
- pr_debug("iavail %d, vbatt %d rbatt %d\n", iavail_ma, vbatt_mv,
- rbatt_mohm);
- if ((gbcl->bcl_threshold_mode[BCL_IAVAIL_HIGH_THRESHOLD_TYPE] ==
- BCL_IAVAIL_THRESHOLD_ENABLED)
- && (iavail_ma >=
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_HIGH_THRESHOLD_TYPE]))
- threshold_cross = true;
- else if ((gbcl->bcl_threshold_mode[BCL_IAVAIL_LOW_THRESHOLD_TYPE]
- == BCL_IAVAIL_THRESHOLD_ENABLED)
- && (iavail_ma <=
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_LOW_THRESHOLD_TYPE]))
- threshold_cross = true;
- if (threshold_cross)
- sysfs_notify(&gbcl->dev->kobj, NULL, "type");
- }
- /*
- * BCL iavail work
- */
- static void bcl_iavail_work(struct work_struct *work)
- {
- struct bcl_context *bcl = container_of(work,
- struct bcl_context, bcl_iavail_work.work);
- if (gbcl->bcl_mode == BCL_DEVICE_ENABLED) {
- bcl_calculate_iavail_trigger();
- /* restart the delay work for caculating imax */
- schedule_delayed_work(&bcl->bcl_iavail_work,
- msecs_to_jiffies(bcl->bcl_poll_interval_msec));
- }
- }
- /*
- * Set BCL mode
- */
- static void bcl_mode_set(enum bcl_device_mode mode)
- {
- if (!gbcl)
- return;
- if (gbcl->bcl_mode == mode)
- return;
- if (gbcl->bcl_mode == BCL_DEVICE_DISABLED
- && mode == BCL_DEVICE_ENABLED) {
- gbcl->bcl_mode = mode;
- bcl_iavail_work(&(gbcl->bcl_iavail_work.work));
- return;
- } else if (gbcl->bcl_mode == BCL_DEVICE_ENABLED
- && mode == BCL_DEVICE_DISABLED) {
- gbcl->bcl_mode = mode;
- cancel_delayed_work_sync(&(gbcl->bcl_iavail_work));
- return;
- }
- return;
- }
- #define show_bcl(name, variable, format) \
- static ssize_t \
- name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
- { \
- if (gbcl) \
- return snprintf(buf, PAGE_SIZE, format, gbcl->variable); \
- else \
- return -EPERM; \
- }
- show_bcl(type, bcl_type, "%s\n")
- show_bcl(vbat, bcl_vbat_mv, "%d\n")
- show_bcl(rbat, bcl_rbat_mohm, "%d\n")
- show_bcl(iavail, bcl_iavail, "%d\n")
- show_bcl(vbat_min, bcl_vbat_min, "%d\n");
- show_bcl(poll_interval, bcl_poll_interval_msec, "%d\n")
- static ssize_t
- mode_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- if (!gbcl)
- return -EPERM;
- return snprintf(buf, PAGE_SIZE, "%s\n",
- gbcl->bcl_mode == BCL_DEVICE_ENABLED ? "enabled"
- : "disabled");
- }
- static ssize_t
- mode_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count)
- {
- if (!gbcl)
- return -EPERM;
- if (!strncmp(buf, "enabled", 7))
- bcl_mode_set(BCL_DEVICE_ENABLED);
- else if (!strncmp(buf, "disabled", 8))
- bcl_mode_set(BCL_DEVICE_DISABLED);
- else
- return -EINVAL;
- return count;
- }
- static ssize_t
- poll_interval_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- int value;
- if (!gbcl)
- return -EPERM;
- if (!sscanf(buf, "%d", &value))
- return -EINVAL;
- if (value < MIN_BCL_POLL_INTERVAL)
- return -EINVAL;
- gbcl->bcl_poll_interval_msec = value;
- return count;
- }
- static ssize_t vbat_min_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- int value;
- int ret;
- if (!gbcl)
- return -EPERM;
- ret = kstrtoint(buf, 10, &value);
- if (ret || (value < 0)) {
- pr_err("Incorrect vbatt min value\n");
- return -EINVAL;
- }
- gbcl->bcl_vbat_min = value;
- return count;
- }
- static ssize_t iavail_low_threshold_mode_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- if (!gbcl)
- return -EPERM;
- return snprintf(buf, PAGE_SIZE, "%s\n",
- gbcl->bcl_threshold_mode[BCL_IAVAIL_LOW_THRESHOLD_TYPE]
- == BCL_IAVAIL_THRESHOLD_ENABLED ? "enabled" : "disabled");
- }
- static ssize_t iavail_low_threshold_mode_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- if (!gbcl)
- return -EPERM;
- if (!strncmp(buf, "enabled", 7))
- gbcl->bcl_threshold_mode[BCL_IAVAIL_LOW_THRESHOLD_TYPE]
- = BCL_IAVAIL_THRESHOLD_ENABLED;
- else if (!strncmp(buf, "disabled", 7))
- gbcl->bcl_threshold_mode[BCL_IAVAIL_LOW_THRESHOLD_TYPE]
- = BCL_IAVAIL_THRESHOLD_DISABLED;
- else
- return -EINVAL;
- return count;
- }
- static ssize_t iavail_high_threshold_mode_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- if (!gbcl)
- return -EPERM;
- return snprintf(buf, PAGE_SIZE, "%s\n",
- gbcl->bcl_threshold_mode[BCL_IAVAIL_HIGH_THRESHOLD_TYPE]
- == BCL_IAVAIL_THRESHOLD_ENABLED ? "enabled" : "disabled");
- }
- static ssize_t iavail_high_threshold_mode_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- if (!gbcl)
- return -EPERM;
- if (!strncmp(buf, "enabled", 7))
- gbcl->bcl_threshold_mode[BCL_IAVAIL_HIGH_THRESHOLD_TYPE]
- = BCL_IAVAIL_THRESHOLD_ENABLED;
- else if (!strncmp(buf, "disabled", 7))
- gbcl->bcl_threshold_mode[BCL_IAVAIL_HIGH_THRESHOLD_TYPE]
- = BCL_IAVAIL_THRESHOLD_DISABLED;
- else
- return -EINVAL;
- return count;
- }
- static ssize_t iavail_low_threshold_value_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- if (!gbcl)
- return -EPERM;
- return snprintf(buf, PAGE_SIZE, "%d\n",
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_LOW_THRESHOLD_TYPE]);
- }
- static ssize_t iavail_low_threshold_value_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- int val;
- int ret;
- ret = kstrtoint(buf, 10, &val);
- if (ret || (val < 0)) {
- pr_err("Incorrect available current threshold value\n");
- return -EINVAL;
- }
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_LOW_THRESHOLD_TYPE] = val;
- return count;
- }
- static ssize_t iavail_high_threshold_value_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- if (!gbcl)
- return -EPERM;
- return snprintf(buf, PAGE_SIZE, "%d\n",
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_HIGH_THRESHOLD_TYPE]);
- }
- static ssize_t iavail_high_threshold_value_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- int val;
- int ret;
- ret = kstrtoint(buf, 10, &val);
- if (ret || (val < 0)) {
- pr_err("Incorrect available current threshold value\n");
- return -EINVAL;
- }
- gbcl->bcl_threshold_value_ma[BCL_IAVAIL_HIGH_THRESHOLD_TYPE] = val;
- return count;
- }
- /*
- * BCL device attributes
- */
- static struct device_attribute bcl_dev_attr[] = {
- __ATTR(type, 0444, type_show, NULL),
- __ATTR(iavail, 0444, iavail_show, NULL),
- __ATTR(vbat_min, 0644, vbat_min_show, vbat_min_store),
- __ATTR(vbat, 0444, vbat_show, NULL),
- __ATTR(rbat, 0444, rbat_show, NULL),
- __ATTR(mode, 0644, mode_show, mode_store),
- __ATTR(poll_interval, 0644,
- poll_interval_show, poll_interval_store),
- __ATTR(iavail_low_threshold_mode, 0644,
- iavail_low_threshold_mode_show,
- iavail_low_threshold_mode_store),
- __ATTR(iavail_high_threshold_mode, 0644,
- iavail_high_threshold_mode_show,
- iavail_high_threshold_mode_store),
- __ATTR(iavail_low_threshold_value, 0644,
- iavail_low_threshold_value_show,
- iavail_low_threshold_value_store),
- __ATTR(iavail_high_threshold_value, 0644,
- iavail_high_threshold_value_show,
- iavail_high_threshold_value_store),
- };
- static int create_bcl_sysfs(struct bcl_context *bcl)
- {
- int result = 0;
- int num_attr = sizeof(bcl_dev_attr)/sizeof(struct device_attribute);
- int i;
- for (i = 0; i < num_attr; i++) {
- result = device_create_file(bcl->dev, &bcl_dev_attr[i]);
- if (result < 0)
- return result;
- }
- return 0;
- }
- static void remove_bcl_sysfs(struct bcl_context *bcl)
- {
- int num_attr = sizeof(bcl_dev_attr)/sizeof(struct device_attribute);
- int i;
- for (i = 0; i < num_attr; i++)
- device_remove_file(bcl->dev, &bcl_dev_attr[i]);
- return;
- }
- static int __devinit bcl_probe(struct platform_device *pdev)
- {
- struct bcl_context *bcl;
- int ret = 0;
- bcl = kzalloc(sizeof(struct bcl_context), GFP_KERNEL);
- if (!bcl) {
- pr_err("Cannot allocate bcl_context\n");
- return -ENOMEM;
- }
- gbcl = bcl;
- /* For BCL */
- /* Init default BCL params */
- bcl->dev = &pdev->dev;
- bcl->bcl_mode = BCL_DEVICE_DISABLED;
- bcl->bcl_threshold_mode[BCL_IAVAIL_LOW_THRESHOLD_TYPE] =
- BCL_IAVAIL_THRESHOLD_DISABLED;
- bcl->bcl_threshold_mode[BCL_IAVAIL_HIGH_THRESHOLD_TYPE] =
- BCL_IAVAIL_THRESHOLD_DISABLED;
- bcl->bcl_threshold_value_ma[BCL_IAVAIL_LOW_THRESHOLD_TYPE] = 0;
- bcl->bcl_threshold_value_ma[BCL_IAVAIL_HIGH_THRESHOLD_TYPE] = 0;
- bcl->bcl_vbat_min = BATTERY_VOLTAGE_MIN;
- snprintf(bcl->bcl_type, BCL_NAME_LENGTH, "%s", bcl_type);
- bcl->bcl_poll_interval_msec = BCL_POLL_INTERVAL;
- ret = create_bcl_sysfs(bcl);
- if (ret < 0) {
- pr_err("Cannot create bcl sysfs\n");
- kfree(bcl);
- return ret;
- }
- platform_set_drvdata(pdev, bcl);
- INIT_DELAYED_WORK_DEFERRABLE(&bcl->bcl_iavail_work, bcl_iavail_work);
- return 0;
- }
- static int __devexit bcl_remove(struct platform_device *pdev)
- {
- remove_bcl_sysfs(gbcl);
- kfree(gbcl);
- gbcl = NULL;
- platform_set_drvdata(pdev, NULL);
- return 0;
- }
- static struct of_device_id bcl_match_table[] = {
- {.compatible = "qcom,bcl"},
- {},
- };
- static struct platform_driver bcl_driver = {
- .probe = bcl_probe,
- .remove = __devexit_p(bcl_remove),
- .driver = {
- .name = BCL_DEV_NAME,
- .owner = THIS_MODULE,
- .of_match_table = bcl_match_table,
- },
- };
- static int __init bcl_init(void)
- {
- return platform_driver_register(&bcl_driver);
- }
- static void __exit bcl_exit(void)
- {
- platform_driver_unregister(&bcl_driver);
- }
- late_initcall(bcl_init);
- module_exit(bcl_exit);
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("battery current limit driver");
- MODULE_ALIAS("platform:" BCL_DEV_NAME);
|