cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

tosa_battery.c (14017B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * Battery and Power Management code for the Sharp SL-6000x
      4 *
      5 * Copyright (c) 2005 Dirk Opfer
      6 * Copyright (c) 2008 Dmitry Baryshkov
      7 */
      8#include <linux/kernel.h>
      9#include <linux/module.h>
     10#include <linux/power_supply.h>
     11#include <linux/wm97xx.h>
     12#include <linux/delay.h>
     13#include <linux/spinlock.h>
     14#include <linux/interrupt.h>
     15#include <linux/gpio/consumer.h>
     16
     17#include <asm/mach-types.h>
     18
     19static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
     20static struct work_struct bat_work;
     21
     22struct tosa_bat {
     23	int status;
     24	struct power_supply *psy;
     25	int full_chrg;
     26
     27	struct mutex work_lock; /* protects data */
     28
     29	bool (*is_present)(struct tosa_bat *bat);
     30	struct gpio_desc *gpiod_full;
     31	struct gpio_desc *gpiod_charge_off;
     32
     33	int technology;
     34
     35	struct gpio_desc *gpiod_bat;
     36	int adc_bat;
     37	int adc_bat_divider;
     38	int bat_max;
     39	int bat_min;
     40
     41	struct gpio_desc *gpiod_temp;
     42	int adc_temp;
     43	int adc_temp_divider;
     44};
     45
     46static struct gpio_desc *jacket_detect;
     47static struct tosa_bat tosa_bat_main;
     48static struct tosa_bat tosa_bat_jacket;
     49
     50static unsigned long tosa_read_bat(struct tosa_bat *bat)
     51{
     52	unsigned long value = 0;
     53
     54	if (!bat->gpiod_bat || bat->adc_bat < 0)
     55		return 0;
     56
     57	mutex_lock(&bat_lock);
     58	gpiod_set_value(bat->gpiod_bat, 1);
     59	msleep(5);
     60	value = wm97xx_read_aux_adc(dev_get_drvdata(bat->psy->dev.parent),
     61			bat->adc_bat);
     62	gpiod_set_value(bat->gpiod_bat, 0);
     63	mutex_unlock(&bat_lock);
     64
     65	value = value * 1000000 / bat->adc_bat_divider;
     66
     67	return value;
     68}
     69
     70static unsigned long tosa_read_temp(struct tosa_bat *bat)
     71{
     72	unsigned long value = 0;
     73
     74	if (!bat->gpiod_temp || bat->adc_temp < 0)
     75		return 0;
     76
     77	mutex_lock(&bat_lock);
     78	gpiod_set_value(bat->gpiod_temp, 1);
     79	msleep(5);
     80	value = wm97xx_read_aux_adc(dev_get_drvdata(bat->psy->dev.parent),
     81			bat->adc_temp);
     82	gpiod_set_value(bat->gpiod_temp, 0);
     83	mutex_unlock(&bat_lock);
     84
     85	value = value * 10000 / bat->adc_temp_divider;
     86
     87	return value;
     88}
     89
     90static int tosa_bat_get_property(struct power_supply *psy,
     91			    enum power_supply_property psp,
     92			    union power_supply_propval *val)
     93{
     94	int ret = 0;
     95	struct tosa_bat *bat = power_supply_get_drvdata(psy);
     96
     97	if (bat->is_present && !bat->is_present(bat)
     98			&& psp != POWER_SUPPLY_PROP_PRESENT) {
     99		return -ENODEV;
    100	}
    101
    102	switch (psp) {
    103	case POWER_SUPPLY_PROP_STATUS:
    104		val->intval = bat->status;
    105		break;
    106	case POWER_SUPPLY_PROP_TECHNOLOGY:
    107		val->intval = bat->technology;
    108		break;
    109	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
    110		val->intval = tosa_read_bat(bat);
    111		break;
    112	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
    113		if (bat->full_chrg == -1)
    114			val->intval = bat->bat_max;
    115		else
    116			val->intval = bat->full_chrg;
    117		break;
    118	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
    119		val->intval = bat->bat_max;
    120		break;
    121	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
    122		val->intval = bat->bat_min;
    123		break;
    124	case POWER_SUPPLY_PROP_TEMP:
    125		val->intval = tosa_read_temp(bat);
    126		break;
    127	case POWER_SUPPLY_PROP_PRESENT:
    128		val->intval = bat->is_present ? bat->is_present(bat) : 1;
    129		break;
    130	default:
    131		ret = -EINVAL;
    132		break;
    133	}
    134	return ret;
    135}
    136
    137static bool tosa_jacket_bat_is_present(struct tosa_bat *bat)
    138{
    139	return gpiod_get_value(jacket_detect) == 0;
    140}
    141
    142static void tosa_bat_external_power_changed(struct power_supply *psy)
    143{
    144	schedule_work(&bat_work);
    145}
    146
    147static irqreturn_t tosa_bat_gpio_isr(int irq, void *data)
    148{
    149	pr_info("tosa_bat_gpio irq\n");
    150	schedule_work(&bat_work);
    151	return IRQ_HANDLED;
    152}
    153
    154static void tosa_bat_update(struct tosa_bat *bat)
    155{
    156	int old;
    157	struct power_supply *psy = bat->psy;
    158
    159	mutex_lock(&bat->work_lock);
    160
    161	old = bat->status;
    162
    163	if (bat->is_present && !bat->is_present(bat)) {
    164		printk(KERN_NOTICE "%s not present\n", psy->desc->name);
    165		bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
    166		bat->full_chrg = -1;
    167	} else if (power_supply_am_i_supplied(psy)) {
    168		if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
    169			gpiod_set_value(bat->gpiod_charge_off, 0);
    170			mdelay(15);
    171		}
    172
    173		if (gpiod_get_value(bat->gpiod_full)) {
    174			if (old == POWER_SUPPLY_STATUS_CHARGING ||
    175					bat->full_chrg == -1)
    176				bat->full_chrg = tosa_read_bat(bat);
    177
    178			gpiod_set_value(bat->gpiod_charge_off, 1);
    179			bat->status = POWER_SUPPLY_STATUS_FULL;
    180		} else {
    181			gpiod_set_value(bat->gpiod_charge_off, 0);
    182			bat->status = POWER_SUPPLY_STATUS_CHARGING;
    183		}
    184	} else {
    185		gpiod_set_value(bat->gpiod_charge_off, 1);
    186		bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
    187	}
    188
    189	if (old != bat->status)
    190		power_supply_changed(psy);
    191
    192	mutex_unlock(&bat->work_lock);
    193}
    194
    195static void tosa_bat_work(struct work_struct *work)
    196{
    197	tosa_bat_update(&tosa_bat_main);
    198	tosa_bat_update(&tosa_bat_jacket);
    199}
    200
    201
    202static enum power_supply_property tosa_bat_main_props[] = {
    203	POWER_SUPPLY_PROP_STATUS,
    204	POWER_SUPPLY_PROP_TECHNOLOGY,
    205	POWER_SUPPLY_PROP_VOLTAGE_NOW,
    206	POWER_SUPPLY_PROP_VOLTAGE_MAX,
    207	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
    208	POWER_SUPPLY_PROP_TEMP,
    209	POWER_SUPPLY_PROP_PRESENT,
    210};
    211
    212static enum power_supply_property tosa_bat_bu_props[] = {
    213	POWER_SUPPLY_PROP_STATUS,
    214	POWER_SUPPLY_PROP_TECHNOLOGY,
    215	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
    216	POWER_SUPPLY_PROP_VOLTAGE_NOW,
    217	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
    218	POWER_SUPPLY_PROP_PRESENT,
    219};
    220
    221static const struct power_supply_desc tosa_bat_main_desc = {
    222	.name		= "main-battery",
    223	.type		= POWER_SUPPLY_TYPE_BATTERY,
    224	.properties	= tosa_bat_main_props,
    225	.num_properties	= ARRAY_SIZE(tosa_bat_main_props),
    226	.get_property	= tosa_bat_get_property,
    227	.external_power_changed = tosa_bat_external_power_changed,
    228	.use_for_apm	= 1,
    229};
    230
    231static const struct power_supply_desc tosa_bat_jacket_desc = {
    232	.name		= "jacket-battery",
    233	.type		= POWER_SUPPLY_TYPE_BATTERY,
    234	.properties	= tosa_bat_main_props,
    235	.num_properties	= ARRAY_SIZE(tosa_bat_main_props),
    236	.get_property	= tosa_bat_get_property,
    237	.external_power_changed = tosa_bat_external_power_changed,
    238};
    239
    240static const struct power_supply_desc tosa_bat_bu_desc = {
    241	.name		= "backup-battery",
    242	.type		= POWER_SUPPLY_TYPE_BATTERY,
    243	.properties	= tosa_bat_bu_props,
    244	.num_properties	= ARRAY_SIZE(tosa_bat_bu_props),
    245	.get_property	= tosa_bat_get_property,
    246	.external_power_changed = tosa_bat_external_power_changed,
    247};
    248
    249static struct tosa_bat tosa_bat_main = {
    250	.status = POWER_SUPPLY_STATUS_DISCHARGING,
    251	.full_chrg = -1,
    252	.psy = NULL,
    253
    254	.gpiod_full = NULL,
    255	.gpiod_charge_off = NULL,
    256
    257	.technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
    258
    259	.gpiod_bat = NULL,
    260	.adc_bat = WM97XX_AUX_ID3,
    261	.adc_bat_divider = 414,
    262	.bat_max = 4310000,
    263	.bat_min = 1551 * 1000000 / 414,
    264
    265	.gpiod_temp = NULL,
    266	.adc_temp = WM97XX_AUX_ID2,
    267	.adc_temp_divider = 10000,
    268};
    269
    270static struct tosa_bat tosa_bat_jacket = {
    271	.status = POWER_SUPPLY_STATUS_DISCHARGING,
    272	.full_chrg = -1,
    273	.psy = NULL,
    274
    275	.is_present = tosa_jacket_bat_is_present,
    276	.gpiod_full = NULL,
    277	.gpiod_charge_off = NULL,
    278
    279	.technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
    280
    281	.gpiod_bat = NULL,
    282	.adc_bat = WM97XX_AUX_ID3,
    283	.adc_bat_divider = 414,
    284	.bat_max = 4310000,
    285	.bat_min = 1551 * 1000000 / 414,
    286
    287	.gpiod_temp = NULL,
    288	.adc_temp = WM97XX_AUX_ID2,
    289	.adc_temp_divider = 10000,
    290};
    291
    292static struct tosa_bat tosa_bat_bu = {
    293	.status = POWER_SUPPLY_STATUS_UNKNOWN,
    294	.full_chrg = -1,
    295	.psy = NULL,
    296
    297	.gpiod_full = NULL,
    298	.gpiod_charge_off = NULL,
    299
    300	.technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
    301
    302	.gpiod_bat = NULL,
    303	.adc_bat = WM97XX_AUX_ID4,
    304	.adc_bat_divider = 1266,
    305
    306	.gpiod_temp = NULL,
    307	.adc_temp = -1,
    308	.adc_temp_divider = -1,
    309};
    310
    311#ifdef CONFIG_PM
    312static int tosa_bat_suspend(struct platform_device *dev, pm_message_t state)
    313{
    314	/* flush all pending status updates */
    315	flush_work(&bat_work);
    316	return 0;
    317}
    318
    319static int tosa_bat_resume(struct platform_device *dev)
    320{
    321	/* things may have changed while we were away */
    322	schedule_work(&bat_work);
    323	return 0;
    324}
    325#else
    326#define tosa_bat_suspend NULL
    327#define tosa_bat_resume NULL
    328#endif
    329
    330static int tosa_bat_probe(struct platform_device *pdev)
    331{
    332	int ret;
    333	struct power_supply_config main_psy_cfg = {},
    334				   jacket_psy_cfg = {},
    335				   bu_psy_cfg = {};
    336	struct device *dev = &pdev->dev;
    337	struct gpio_desc *dummy;
    338
    339	if (!machine_is_tosa())
    340		return -ENODEV;
    341
    342	/* Main charging control GPIOs */
    343	tosa_bat_main.gpiod_charge_off = devm_gpiod_get(dev, "main charge off", GPIOD_OUT_HIGH);
    344	if (IS_ERR(tosa_bat_main.gpiod_charge_off))
    345		return dev_err_probe(dev, PTR_ERR(tosa_bat_main.gpiod_charge_off),
    346				     "no main charger GPIO\n");
    347	tosa_bat_jacket.gpiod_charge_off = devm_gpiod_get(dev, "jacket charge off", GPIOD_OUT_HIGH);
    348	if (IS_ERR(tosa_bat_jacket.gpiod_charge_off))
    349		return dev_err_probe(dev, PTR_ERR(tosa_bat_jacket.gpiod_charge_off),
    350				     "no jacket charger GPIO\n");
    351
    352	/* Per-battery output check (routes battery voltage to ADC) */
    353	tosa_bat_main.gpiod_bat = devm_gpiod_get(dev, "main battery", GPIOD_OUT_LOW);
    354	if (IS_ERR(tosa_bat_main.gpiod_bat))
    355		return dev_err_probe(dev, PTR_ERR(tosa_bat_main.gpiod_bat),
    356				     "no main battery GPIO\n");
    357	tosa_bat_jacket.gpiod_bat = devm_gpiod_get(dev, "jacket battery", GPIOD_OUT_LOW);
    358	if (IS_ERR(tosa_bat_jacket.gpiod_bat))
    359		return dev_err_probe(dev, PTR_ERR(tosa_bat_jacket.gpiod_bat),
    360				     "no jacket battery GPIO\n");
    361	tosa_bat_bu.gpiod_bat = devm_gpiod_get(dev, "backup battery", GPIOD_OUT_LOW);
    362	if (IS_ERR(tosa_bat_bu.gpiod_bat))
    363		return dev_err_probe(dev, PTR_ERR(tosa_bat_bu.gpiod_bat),
    364				     "no backup battery GPIO\n");
    365
    366	/* Battery full detect GPIOs (using PXA SoC GPIOs) */
    367	tosa_bat_main.gpiod_full = devm_gpiod_get(dev, "main battery full", GPIOD_IN);
    368	if (IS_ERR(tosa_bat_main.gpiod_full))
    369		return dev_err_probe(dev, PTR_ERR(tosa_bat_main.gpiod_full),
    370				     "no main battery full GPIO\n");
    371	tosa_bat_jacket.gpiod_full = devm_gpiod_get(dev, "jacket battery full", GPIOD_IN);
    372	if (IS_ERR(tosa_bat_jacket.gpiod_full))
    373		return dev_err_probe(dev, PTR_ERR(tosa_bat_jacket.gpiod_full),
    374				     "no jacket battery full GPIO\n");
    375
    376	/* Battery temperature GPIOs (routes thermistor voltage to ADC) */
    377	tosa_bat_main.gpiod_temp = devm_gpiod_get(dev, "main battery temp", GPIOD_OUT_LOW);
    378	if (IS_ERR(tosa_bat_main.gpiod_temp))
    379		return dev_err_probe(dev, PTR_ERR(tosa_bat_main.gpiod_temp),
    380				     "no main battery temp GPIO\n");
    381	tosa_bat_jacket.gpiod_temp = devm_gpiod_get(dev, "jacket battery temp", GPIOD_OUT_LOW);
    382	if (IS_ERR(tosa_bat_jacket.gpiod_temp))
    383		return dev_err_probe(dev, PTR_ERR(tosa_bat_jacket.gpiod_temp),
    384				     "no jacket battery temp GPIO\n");
    385
    386	/* Jacket detect GPIO */
    387	jacket_detect = devm_gpiod_get(dev, "jacket detect", GPIOD_IN);
    388	if (IS_ERR(jacket_detect))
    389		return dev_err_probe(dev, PTR_ERR(jacket_detect),
    390				     "no jacket detect GPIO\n");
    391
    392	/* Battery low indication GPIOs (not used, we just request them) */
    393	dummy = devm_gpiod_get(dev, "main battery low", GPIOD_IN);
    394	if (IS_ERR(dummy))
    395		return dev_err_probe(dev, PTR_ERR(dummy),
    396				     "no main battery low GPIO\n");
    397	dummy = devm_gpiod_get(dev, "jacket battery low", GPIOD_IN);
    398	if (IS_ERR(dummy))
    399		return dev_err_probe(dev, PTR_ERR(dummy),
    400				     "no jacket battery low GPIO\n");
    401
    402	/* Battery switch GPIO (not used just requested) */
    403	dummy = devm_gpiod_get(dev, "battery switch", GPIOD_OUT_LOW);
    404	if (IS_ERR(dummy))
    405		return dev_err_probe(dev, PTR_ERR(dummy),
    406				     "no battery switch GPIO\n");
    407
    408	mutex_init(&tosa_bat_main.work_lock);
    409	mutex_init(&tosa_bat_jacket.work_lock);
    410
    411	INIT_WORK(&bat_work, tosa_bat_work);
    412
    413	main_psy_cfg.drv_data = &tosa_bat_main;
    414	tosa_bat_main.psy = power_supply_register(dev,
    415						  &tosa_bat_main_desc,
    416						  &main_psy_cfg);
    417	if (IS_ERR(tosa_bat_main.psy)) {
    418		ret = PTR_ERR(tosa_bat_main.psy);
    419		goto err_psy_reg_main;
    420	}
    421
    422	jacket_psy_cfg.drv_data = &tosa_bat_jacket;
    423	tosa_bat_jacket.psy = power_supply_register(dev,
    424						    &tosa_bat_jacket_desc,
    425						    &jacket_psy_cfg);
    426	if (IS_ERR(tosa_bat_jacket.psy)) {
    427		ret = PTR_ERR(tosa_bat_jacket.psy);
    428		goto err_psy_reg_jacket;
    429	}
    430
    431	bu_psy_cfg.drv_data = &tosa_bat_bu;
    432	tosa_bat_bu.psy = power_supply_register(dev, &tosa_bat_bu_desc,
    433						&bu_psy_cfg);
    434	if (IS_ERR(tosa_bat_bu.psy)) {
    435		ret = PTR_ERR(tosa_bat_bu.psy);
    436		goto err_psy_reg_bu;
    437	}
    438
    439	ret = request_irq(gpiod_to_irq(tosa_bat_main.gpiod_full),
    440				tosa_bat_gpio_isr,
    441				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
    442				"main full", &tosa_bat_main);
    443	if (ret)
    444		goto err_req_main;
    445
    446	ret = request_irq(gpiod_to_irq(tosa_bat_jacket.gpiod_full),
    447				tosa_bat_gpio_isr,
    448				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
    449				"jacket full", &tosa_bat_jacket);
    450	if (ret)
    451		goto err_req_jacket;
    452
    453	ret = request_irq(gpiod_to_irq(jacket_detect),
    454				tosa_bat_gpio_isr,
    455				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
    456				"jacket detect", &tosa_bat_jacket);
    457	if (!ret) {
    458		schedule_work(&bat_work);
    459		return 0;
    460	}
    461
    462	free_irq(gpiod_to_irq(tosa_bat_jacket.gpiod_full), &tosa_bat_jacket);
    463err_req_jacket:
    464	free_irq(gpiod_to_irq(tosa_bat_main.gpiod_full), &tosa_bat_main);
    465err_req_main:
    466	power_supply_unregister(tosa_bat_bu.psy);
    467err_psy_reg_bu:
    468	power_supply_unregister(tosa_bat_jacket.psy);
    469err_psy_reg_jacket:
    470	power_supply_unregister(tosa_bat_main.psy);
    471err_psy_reg_main:
    472
    473	/* see comment in tosa_bat_remove */
    474	cancel_work_sync(&bat_work);
    475
    476	return ret;
    477}
    478
    479static int tosa_bat_remove(struct platform_device *dev)
    480{
    481	free_irq(gpiod_to_irq(jacket_detect), &tosa_bat_jacket);
    482	free_irq(gpiod_to_irq(tosa_bat_jacket.gpiod_full), &tosa_bat_jacket);
    483	free_irq(gpiod_to_irq(tosa_bat_main.gpiod_full), &tosa_bat_main);
    484
    485	power_supply_unregister(tosa_bat_bu.psy);
    486	power_supply_unregister(tosa_bat_jacket.psy);
    487	power_supply_unregister(tosa_bat_main.psy);
    488
    489	/*
    490	 * Now cancel the bat_work.  We won't get any more schedules,
    491	 * since all sources (isr and external_power_changed) are
    492	 * unregistered now.
    493	 */
    494	cancel_work_sync(&bat_work);
    495	return 0;
    496}
    497
    498static struct platform_driver tosa_bat_driver = {
    499	.driver.name	= "wm97xx-battery",
    500	.driver.owner	= THIS_MODULE,
    501	.probe		= tosa_bat_probe,
    502	.remove		= tosa_bat_remove,
    503	.suspend	= tosa_bat_suspend,
    504	.resume		= tosa_bat_resume,
    505};
    506
    507module_platform_driver(tosa_bat_driver);
    508
    509MODULE_LICENSE("GPL");
    510MODULE_AUTHOR("Dmitry Baryshkov");
    511MODULE_DESCRIPTION("Tosa battery driver");
    512MODULE_ALIAS("platform:wm97xx-battery");