Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Battery and Power Management code for the Sharp SL-5x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2009 Thomas Kunze
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * based on tosa_battery.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/ucb1x00.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/mach/sharpsl_param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/mach-types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <mach/collie.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static struct work_struct bat_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static struct ucb1x00 *ucb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct collie_bat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int full_chrg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct mutex work_lock; /* protects data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	bool (*is_present)(struct collie_bat *bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int gpio_full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int gpio_charge_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int technology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int gpio_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int adc_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int adc_bat_divider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int bat_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int bat_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int gpio_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int adc_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int adc_temp_divider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static struct collie_bat collie_bat_main;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static unsigned long collie_read_bat(struct collie_bat *bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	unsigned long value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (bat->gpio_bat < 0 || bat->adc_bat < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	mutex_lock(&bat_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	gpio_set_value(bat->gpio_bat, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	msleep(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ucb1x00_adc_enable(ucb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	value = ucb1x00_adc_read(ucb, bat->adc_bat, UCB_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ucb1x00_adc_disable(ucb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	gpio_set_value(bat->gpio_bat, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	mutex_unlock(&bat_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	value = value * 1000000 / bat->adc_bat_divider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static unsigned long collie_read_temp(struct collie_bat *bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned long value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (bat->gpio_temp < 0 || bat->adc_temp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	mutex_lock(&bat_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	gpio_set_value(bat->gpio_temp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	msleep(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	ucb1x00_adc_enable(ucb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	value = ucb1x00_adc_read(ucb, bat->adc_temp, UCB_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ucb1x00_adc_disable(ucb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	gpio_set_value(bat->gpio_temp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	mutex_unlock(&bat_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	value = value * 10000 / bat->adc_temp_divider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int collie_bat_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			    enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			    union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct collie_bat *bat = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (bat->is_present && !bat->is_present(bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			&& psp != POWER_SUPPLY_PROP_PRESENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		val->intval = bat->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		val->intval = bat->technology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		val->intval = collie_read_bat(bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		if (bat->full_chrg == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			val->intval = bat->bat_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			val->intval = bat->full_chrg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		val->intval = bat->bat_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		val->intval = bat->bat_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		val->intval = collie_read_temp(bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		val->intval = bat->is_present ? bat->is_present(bat) : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void collie_bat_external_power_changed(struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	schedule_work(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static irqreturn_t collie_bat_gpio_isr(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	pr_info("collie_bat_gpio irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	schedule_work(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void collie_bat_update(struct collie_bat *bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct power_supply *psy = bat->psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	mutex_lock(&bat->work_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	old = bat->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (bat->is_present && !bat->is_present(bat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		printk(KERN_NOTICE "%s not present\n", psy->desc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		bat->full_chrg = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	} else if (power_supply_am_i_supplied(psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			gpio_set_value(bat->gpio_charge_on, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			mdelay(15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (gpio_get_value(bat->gpio_full)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			if (old == POWER_SUPPLY_STATUS_CHARGING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					bat->full_chrg == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				bat->full_chrg = collie_read_bat(bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			gpio_set_value(bat->gpio_charge_on, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			bat->status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			gpio_set_value(bat->gpio_charge_on, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			bat->status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		gpio_set_value(bat->gpio_charge_on, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (old != bat->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		power_supply_changed(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	mutex_unlock(&bat->work_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static void collie_bat_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	collie_bat_update(&collie_bat_main);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static enum power_supply_property collie_bat_main_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static enum power_supply_property collie_bat_bu_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static const struct power_supply_desc collie_bat_main_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.name		= "main-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.type		= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	.properties	= collie_bat_main_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	.num_properties	= ARRAY_SIZE(collie_bat_main_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	.get_property	= collie_bat_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.external_power_changed = collie_bat_external_power_changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.use_for_apm	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static struct collie_bat collie_bat_main = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	.status = POWER_SUPPLY_STATUS_DISCHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.full_chrg = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.psy = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	.gpio_full = COLLIE_GPIO_CO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	.gpio_charge_on = COLLIE_GPIO_CHARGE_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	.gpio_bat = COLLIE_GPIO_MBAT_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	.adc_bat = UCB_ADC_INP_AD1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.adc_bat_divider = 155,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.bat_max = 4310000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	.bat_min = 1551 * 1000000 / 414,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	.gpio_temp = COLLIE_GPIO_TMP_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	.adc_temp = UCB_ADC_INP_AD0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	.adc_temp_divider = 10000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static const struct power_supply_desc collie_bat_bu_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.name		= "backup-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.type		= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	.properties	= collie_bat_bu_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	.num_properties	= ARRAY_SIZE(collie_bat_bu_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	.get_property	= collie_bat_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.external_power_changed = collie_bat_external_power_changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static struct collie_bat collie_bat_bu = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	.status = POWER_SUPPLY_STATUS_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	.full_chrg = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	.psy = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	.gpio_full = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	.gpio_charge_on = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	.technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.gpio_bat = COLLIE_GPIO_BBAT_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.adc_bat = UCB_ADC_INP_AD1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.adc_bat_divider = 155,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.bat_max = 3000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.bat_min = 1900000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	.gpio_temp = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	.adc_temp = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	.adc_temp_divider = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static struct gpio collie_batt_gpios[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	{ COLLIE_GPIO_CO,	    GPIOF_IN,		"main battery full" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	{ COLLIE_GPIO_MAIN_BAT_LOW, GPIOF_IN,		"main battery low" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	{ COLLIE_GPIO_CHARGE_ON,    GPIOF_OUT_INIT_LOW,	"main charge on" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	{ COLLIE_GPIO_MBAT_ON,	    GPIOF_OUT_INIT_LOW,	"main battery" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	{ COLLIE_GPIO_TMP_ON,	    GPIOF_OUT_INIT_LOW,	"main battery temp" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	{ COLLIE_GPIO_BBAT_ON,	    GPIOF_OUT_INIT_LOW,	"backup battery" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int wakeup_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int collie_bat_suspend(struct ucb1x00_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/* flush all pending status updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	flush_work(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (device_may_wakeup(&dev->ucb->dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	    collie_bat_main.status == POWER_SUPPLY_STATUS_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		wakeup_enabled = !enable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		wakeup_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int collie_bat_resume(struct ucb1x00_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (wakeup_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		disable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	/* things may have changed while we were away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	schedule_work(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #define collie_bat_suspend NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #define collie_bat_resume NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int collie_bat_probe(struct ucb1x00_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct power_supply_config psy_main_cfg = {}, psy_bu_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (!machine_is_collie())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ucb = dev->ucb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	ret = gpio_request_array(collie_batt_gpios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				 ARRAY_SIZE(collie_batt_gpios));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_init(&collie_bat_main.work_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	INIT_WORK(&bat_work, collie_bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	psy_main_cfg.drv_data = &collie_bat_main;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	collie_bat_main.psy = power_supply_register(&dev->ucb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 						    &collie_bat_main_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 						    &psy_main_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (IS_ERR(collie_bat_main.psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		ret = PTR_ERR(collie_bat_main.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		goto err_psy_reg_main;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	psy_bu_cfg.drv_data = &collie_bat_bu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	collie_bat_bu.psy = power_supply_register(&dev->ucb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 						  &collie_bat_bu_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 						  &psy_bu_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (IS_ERR(collie_bat_bu.psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		ret = PTR_ERR(collie_bat_bu.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		goto err_psy_reg_bu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	ret = request_irq(gpio_to_irq(COLLIE_GPIO_CO),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				collie_bat_gpio_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				"main full", &collie_bat_main);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	device_init_wakeup(&ucb->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	schedule_work(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) err_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	power_supply_unregister(collie_bat_bu.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err_psy_reg_bu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	power_supply_unregister(collie_bat_main.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) err_psy_reg_main:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* see comment in collie_bat_remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	cancel_work_sync(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void collie_bat_remove(struct ucb1x00_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	power_supply_unregister(collie_bat_bu.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	power_supply_unregister(collie_bat_main.psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * Now cancel the bat_work.  We won't get any more schedules,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 * since all sources (isr and external_power_changed) are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 * unregistered now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	cancel_work_sync(&bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static struct ucb1x00_driver collie_bat_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	.add		= collie_bat_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	.remove		= collie_bat_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	.suspend	= collie_bat_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	.resume		= collie_bat_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int __init collie_bat_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return ucb1x00_register_driver(&collie_bat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static void __exit collie_bat_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	ucb1x00_unregister_driver(&collie_bat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) module_init(collie_bat_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) module_exit(collie_bat_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) MODULE_AUTHOR("Thomas Kunze");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) MODULE_DESCRIPTION("Collie battery driver");