109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
317825ff6SDragos Bogdan * I2C client/driver for the Linear Technology LTC2941, LTC2942, LTC2943
417825ff6SDragos Bogdan * and LTC2944 Battery Gas Gauge IC
58c0984e5SSebastian Reichel *
68c0984e5SSebastian Reichel * Copyright (C) 2014 Topic Embedded Systems
78c0984e5SSebastian Reichel *
88c0984e5SSebastian Reichel * Author: Auryn Verwegen
98c0984e5SSebastian Reichel * Author: Mike Looijmans
108c0984e5SSebastian Reichel */
116d0c5de2SMatti Vaittinen #include <linux/devm-helpers.h>
128c0984e5SSebastian Reichel #include <linux/kernel.h>
138c0984e5SSebastian Reichel #include <linux/module.h>
14*2ce8284cSRob Herring #include <linux/of.h>
158c0984e5SSebastian Reichel #include <linux/types.h>
168c0984e5SSebastian Reichel #include <linux/errno.h>
178c0984e5SSebastian Reichel #include <linux/swab.h>
188c0984e5SSebastian Reichel #include <linux/i2c.h>
198c0984e5SSebastian Reichel #include <linux/delay.h>
208c0984e5SSebastian Reichel #include <linux/power_supply.h>
218c0984e5SSebastian Reichel #include <linux/slab.h>
228c0984e5SSebastian Reichel
238c0984e5SSebastian Reichel #define I16_MSB(x) ((x >> 8) & 0xFF)
248c0984e5SSebastian Reichel #define I16_LSB(x) (x & 0xFF)
258c0984e5SSebastian Reichel
268c0984e5SSebastian Reichel #define LTC294X_WORK_DELAY 10 /* Update delay in seconds */
278c0984e5SSebastian Reichel
288c0984e5SSebastian Reichel #define LTC294X_MAX_VALUE 0xFFFF
298c0984e5SSebastian Reichel #define LTC294X_MID_SUPPLY 0x7FFF
308c0984e5SSebastian Reichel
318c0984e5SSebastian Reichel #define LTC2941_MAX_PRESCALER_EXP 7
328c0984e5SSebastian Reichel #define LTC2943_MAX_PRESCALER_EXP 6
338c0984e5SSebastian Reichel
348c0984e5SSebastian Reichel enum ltc294x_reg {
358c0984e5SSebastian Reichel LTC294X_REG_STATUS = 0x00,
368c0984e5SSebastian Reichel LTC294X_REG_CONTROL = 0x01,
378c0984e5SSebastian Reichel LTC294X_REG_ACC_CHARGE_MSB = 0x02,
388c0984e5SSebastian Reichel LTC294X_REG_ACC_CHARGE_LSB = 0x03,
393bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_HIGH_MSB = 0x04,
403bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_HIGH_LSB = 0x05,
413bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_LOW_MSB = 0x06,
423bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_LOW_LSB = 0x07,
438c0984e5SSebastian Reichel LTC294X_REG_VOLTAGE_MSB = 0x08,
448c0984e5SSebastian Reichel LTC294X_REG_VOLTAGE_LSB = 0x09,
4563e67c57SLadislav Michl LTC2942_REG_TEMPERATURE_MSB = 0x0C,
4663e67c57SLadislav Michl LTC2942_REG_TEMPERATURE_LSB = 0x0D,
4763e67c57SLadislav Michl LTC2943_REG_CURRENT_MSB = 0x0E,
4863e67c57SLadislav Michl LTC2943_REG_CURRENT_LSB = 0x0F,
4963e67c57SLadislav Michl LTC2943_REG_TEMPERATURE_MSB = 0x14,
5063e67c57SLadislav Michl LTC2943_REG_TEMPERATURE_LSB = 0x15,
5163e67c57SLadislav Michl };
5263e67c57SLadislav Michl
5363e67c57SLadislav Michl enum ltc294x_id {
5463e67c57SLadislav Michl LTC2941_ID,
55a65df832SLadislav Michl LTC2942_ID,
5663e67c57SLadislav Michl LTC2943_ID,
5717825ff6SDragos Bogdan LTC2944_ID,
588c0984e5SSebastian Reichel };
598c0984e5SSebastian Reichel
60a65df832SLadislav Michl #define LTC2941_REG_STATUS_CHIP_ID BIT(7)
61a65df832SLadislav Michl
62a65df832SLadislav Michl #define LTC2942_REG_CONTROL_MODE_SCAN (BIT(7) | BIT(6))
638c0984e5SSebastian Reichel #define LTC2943_REG_CONTROL_MODE_SCAN BIT(7)
648c0984e5SSebastian Reichel #define LTC294X_REG_CONTROL_PRESCALER_MASK (BIT(5) | BIT(4) | BIT(3))
658c0984e5SSebastian Reichel #define LTC294X_REG_CONTROL_SHUTDOWN_MASK (BIT(0))
668c0984e5SSebastian Reichel #define LTC294X_REG_CONTROL_PRESCALER_SET(x) \
678c0984e5SSebastian Reichel ((x << 3) & LTC294X_REG_CONTROL_PRESCALER_MASK)
688c0984e5SSebastian Reichel #define LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED 0
695f2f0d61SMike Looijmans #define LTC294X_REG_CONTROL_ADC_DISABLE(x) ((x) & ~(BIT(7) | BIT(6)))
708c0984e5SSebastian Reichel
718c0984e5SSebastian Reichel struct ltc294x_info {
728c0984e5SSebastian Reichel struct i2c_client *client; /* I2C Client pointer */
738c0984e5SSebastian Reichel struct power_supply *supply; /* Supply pointer */
748c0984e5SSebastian Reichel struct power_supply_desc supply_desc; /* Supply description */
758c0984e5SSebastian Reichel struct delayed_work work; /* Work scheduler */
7663e67c57SLadislav Michl enum ltc294x_id id; /* Chip type */
778c0984e5SSebastian Reichel int charge; /* Last charge register content */
788c0984e5SSebastian Reichel int r_sense; /* mOhm */
798c0984e5SSebastian Reichel int Qlsb; /* nAh */
808c0984e5SSebastian Reichel };
818c0984e5SSebastian Reichel
convert_bin_to_uAh(const struct ltc294x_info * info,int Q)828c0984e5SSebastian Reichel static inline int convert_bin_to_uAh(
838c0984e5SSebastian Reichel const struct ltc294x_info *info, int Q)
848c0984e5SSebastian Reichel {
858c0984e5SSebastian Reichel return ((Q * (info->Qlsb / 10))) / 100;
868c0984e5SSebastian Reichel }
878c0984e5SSebastian Reichel
convert_uAh_to_bin(const struct ltc294x_info * info,int uAh)888c0984e5SSebastian Reichel static inline int convert_uAh_to_bin(
898c0984e5SSebastian Reichel const struct ltc294x_info *info, int uAh)
908c0984e5SSebastian Reichel {
918c0984e5SSebastian Reichel int Q;
928c0984e5SSebastian Reichel
938c0984e5SSebastian Reichel Q = (uAh * 100) / (info->Qlsb/10);
948c0984e5SSebastian Reichel return (Q < LTC294X_MAX_VALUE) ? Q : LTC294X_MAX_VALUE;
958c0984e5SSebastian Reichel }
968c0984e5SSebastian Reichel
ltc294x_read_regs(struct i2c_client * client,enum ltc294x_reg reg,u8 * buf,int num_regs)978c0984e5SSebastian Reichel static int ltc294x_read_regs(struct i2c_client *client,
988c0984e5SSebastian Reichel enum ltc294x_reg reg, u8 *buf, int num_regs)
998c0984e5SSebastian Reichel {
1008c0984e5SSebastian Reichel int ret;
1018c0984e5SSebastian Reichel struct i2c_msg msgs[2] = { };
1028c0984e5SSebastian Reichel u8 reg_start = reg;
1038c0984e5SSebastian Reichel
1048c0984e5SSebastian Reichel msgs[0].addr = client->addr;
1058c0984e5SSebastian Reichel msgs[0].len = 1;
1068c0984e5SSebastian Reichel msgs[0].buf = ®_start;
1078c0984e5SSebastian Reichel
1088c0984e5SSebastian Reichel msgs[1].addr = client->addr;
1098c0984e5SSebastian Reichel msgs[1].len = num_regs;
1108c0984e5SSebastian Reichel msgs[1].buf = buf;
1118c0984e5SSebastian Reichel msgs[1].flags = I2C_M_RD;
1128c0984e5SSebastian Reichel
1138c0984e5SSebastian Reichel ret = i2c_transfer(client->adapter, &msgs[0], 2);
1148c0984e5SSebastian Reichel if (ret < 0) {
115513e3b53SMichał Mirosław dev_err(&client->dev, "ltc2941 read_reg(0x%x[%d]) failed: %pe\n",
116513e3b53SMichał Mirosław reg, num_regs, ERR_PTR(ret));
1178c0984e5SSebastian Reichel return ret;
1188c0984e5SSebastian Reichel }
1198c0984e5SSebastian Reichel
1208c0984e5SSebastian Reichel dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
1218c0984e5SSebastian Reichel __func__, reg, num_regs, *buf);
1228c0984e5SSebastian Reichel
1238c0984e5SSebastian Reichel return 0;
1248c0984e5SSebastian Reichel }
1258c0984e5SSebastian Reichel
ltc294x_write_regs(struct i2c_client * client,enum ltc294x_reg reg,const u8 * buf,int num_regs)1268c0984e5SSebastian Reichel static int ltc294x_write_regs(struct i2c_client *client,
1278c0984e5SSebastian Reichel enum ltc294x_reg reg, const u8 *buf, int num_regs)
1288c0984e5SSebastian Reichel {
1298c0984e5SSebastian Reichel int ret;
1308c0984e5SSebastian Reichel u8 reg_start = reg;
1318c0984e5SSebastian Reichel
1328c0984e5SSebastian Reichel ret = i2c_smbus_write_i2c_block_data(client, reg_start, num_regs, buf);
1338c0984e5SSebastian Reichel if (ret < 0) {
134513e3b53SMichał Mirosław dev_err(&client->dev, "ltc2941 write_reg(0x%x[%d]) failed: %pe\n",
135513e3b53SMichał Mirosław reg, num_regs, ERR_PTR(ret));
1368c0984e5SSebastian Reichel return ret;
1378c0984e5SSebastian Reichel }
1388c0984e5SSebastian Reichel
1398c0984e5SSebastian Reichel dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
1408c0984e5SSebastian Reichel __func__, reg, num_regs, *buf);
1418c0984e5SSebastian Reichel
1428c0984e5SSebastian Reichel return 0;
1438c0984e5SSebastian Reichel }
1448c0984e5SSebastian Reichel
ltc294x_reset(const struct ltc294x_info * info,int prescaler_exp)1458c0984e5SSebastian Reichel static int ltc294x_reset(const struct ltc294x_info *info, int prescaler_exp)
1468c0984e5SSebastian Reichel {
1478c0984e5SSebastian Reichel int ret;
1488c0984e5SSebastian Reichel u8 value;
1498c0984e5SSebastian Reichel u8 control;
1508c0984e5SSebastian Reichel
1518c0984e5SSebastian Reichel /* Read status and control registers */
1528c0984e5SSebastian Reichel ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
153513e3b53SMichał Mirosław if (ret < 0)
154513e3b53SMichał Mirosław return ret;
1558c0984e5SSebastian Reichel
1568c0984e5SSebastian Reichel control = LTC294X_REG_CONTROL_PRESCALER_SET(prescaler_exp) |
1578c0984e5SSebastian Reichel LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED;
158a65df832SLadislav Michl /* Put device into "monitor" mode */
159a65df832SLadislav Michl switch (info->id) {
160a65df832SLadislav Michl case LTC2942_ID: /* 2942 measures every 2 sec */
161a65df832SLadislav Michl control |= LTC2942_REG_CONTROL_MODE_SCAN;
162a65df832SLadislav Michl break;
16317825ff6SDragos Bogdan case LTC2943_ID:
16417825ff6SDragos Bogdan case LTC2944_ID: /* 2943 and 2944 measure every 10 sec */
1658c0984e5SSebastian Reichel control |= LTC2943_REG_CONTROL_MODE_SCAN;
166a65df832SLadislav Michl break;
167a65df832SLadislav Michl default:
168a65df832SLadislav Michl break;
169a65df832SLadislav Michl }
1708c0984e5SSebastian Reichel
1718c0984e5SSebastian Reichel if (value != control) {
1728c0984e5SSebastian Reichel ret = ltc294x_write_regs(info->client,
1738c0984e5SSebastian Reichel LTC294X_REG_CONTROL, &control, 1);
174513e3b53SMichał Mirosław if (ret < 0)
175513e3b53SMichał Mirosław return ret;
1768c0984e5SSebastian Reichel }
1778c0984e5SSebastian Reichel
1788c0984e5SSebastian Reichel return 0;
1798c0984e5SSebastian Reichel }
1808c0984e5SSebastian Reichel
ltc294x_read_charge_register(const struct ltc294x_info * info,enum ltc294x_reg reg)1813bf4e03dSLadislav Michl static int ltc294x_read_charge_register(const struct ltc294x_info *info,
1823bf4e03dSLadislav Michl enum ltc294x_reg reg)
1838c0984e5SSebastian Reichel {
1848c0984e5SSebastian Reichel int ret;
1858c0984e5SSebastian Reichel u8 datar[2];
1868c0984e5SSebastian Reichel
1873bf4e03dSLadislav Michl ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
1888c0984e5SSebastian Reichel if (ret < 0)
1898c0984e5SSebastian Reichel return ret;
1908c0984e5SSebastian Reichel return (datar[0] << 8) + datar[1];
1918c0984e5SSebastian Reichel }
1928c0984e5SSebastian Reichel
ltc294x_get_charge(const struct ltc294x_info * info,enum ltc294x_reg reg,int * val)1933bf4e03dSLadislav Michl static int ltc294x_get_charge(const struct ltc294x_info *info,
1943bf4e03dSLadislav Michl enum ltc294x_reg reg, int *val)
1958c0984e5SSebastian Reichel {
1963bf4e03dSLadislav Michl int value = ltc294x_read_charge_register(info, reg);
1978c0984e5SSebastian Reichel
1988c0984e5SSebastian Reichel if (value < 0)
1998c0984e5SSebastian Reichel return value;
2008c0984e5SSebastian Reichel /* When r_sense < 0, this counts up when the battery discharges */
2018c0984e5SSebastian Reichel if (info->Qlsb < 0)
2028c0984e5SSebastian Reichel value -= 0xFFFF;
2038c0984e5SSebastian Reichel *val = convert_bin_to_uAh(info, value);
2048c0984e5SSebastian Reichel return 0;
2058c0984e5SSebastian Reichel }
2068c0984e5SSebastian Reichel
ltc294x_set_charge_now(const struct ltc294x_info * info,int val)2078c0984e5SSebastian Reichel static int ltc294x_set_charge_now(const struct ltc294x_info *info, int val)
2088c0984e5SSebastian Reichel {
2098c0984e5SSebastian Reichel int ret;
2108c0984e5SSebastian Reichel u8 dataw[2];
2118c0984e5SSebastian Reichel u8 ctrl_reg;
2128c0984e5SSebastian Reichel s32 value;
2138c0984e5SSebastian Reichel
2148c0984e5SSebastian Reichel value = convert_uAh_to_bin(info, val);
2158c0984e5SSebastian Reichel /* Direction depends on how sense+/- were connected */
2168c0984e5SSebastian Reichel if (info->Qlsb < 0)
2178c0984e5SSebastian Reichel value += 0xFFFF;
2188c0984e5SSebastian Reichel if ((value < 0) || (value > 0xFFFF)) /* input validation */
2198c0984e5SSebastian Reichel return -EINVAL;
2208c0984e5SSebastian Reichel
2218c0984e5SSebastian Reichel /* Read control register */
2228c0984e5SSebastian Reichel ret = ltc294x_read_regs(info->client,
2238c0984e5SSebastian Reichel LTC294X_REG_CONTROL, &ctrl_reg, 1);
2248c0984e5SSebastian Reichel if (ret < 0)
2258c0984e5SSebastian Reichel return ret;
2268c0984e5SSebastian Reichel /* Disable analog section */
2278c0984e5SSebastian Reichel ctrl_reg |= LTC294X_REG_CONTROL_SHUTDOWN_MASK;
2288c0984e5SSebastian Reichel ret = ltc294x_write_regs(info->client,
2298c0984e5SSebastian Reichel LTC294X_REG_CONTROL, &ctrl_reg, 1);
2308c0984e5SSebastian Reichel if (ret < 0)
2318c0984e5SSebastian Reichel return ret;
2328c0984e5SSebastian Reichel /* Set new charge value */
2338c0984e5SSebastian Reichel dataw[0] = I16_MSB(value);
2348c0984e5SSebastian Reichel dataw[1] = I16_LSB(value);
2358c0984e5SSebastian Reichel ret = ltc294x_write_regs(info->client,
2368c0984e5SSebastian Reichel LTC294X_REG_ACC_CHARGE_MSB, &dataw[0], 2);
2378c0984e5SSebastian Reichel if (ret < 0)
2388c0984e5SSebastian Reichel goto error_exit;
2398c0984e5SSebastian Reichel /* Enable analog section */
2408c0984e5SSebastian Reichel error_exit:
2418c0984e5SSebastian Reichel ctrl_reg &= ~LTC294X_REG_CONTROL_SHUTDOWN_MASK;
2428c0984e5SSebastian Reichel ret = ltc294x_write_regs(info->client,
2438c0984e5SSebastian Reichel LTC294X_REG_CONTROL, &ctrl_reg, 1);
2448c0984e5SSebastian Reichel
2458c0984e5SSebastian Reichel return ret < 0 ? ret : 0;
2468c0984e5SSebastian Reichel }
2478c0984e5SSebastian Reichel
ltc294x_set_charge_thr(const struct ltc294x_info * info,enum ltc294x_reg reg,int val)2483bf4e03dSLadislav Michl static int ltc294x_set_charge_thr(const struct ltc294x_info *info,
2493bf4e03dSLadislav Michl enum ltc294x_reg reg, int val)
2503bf4e03dSLadislav Michl {
2513bf4e03dSLadislav Michl u8 dataw[2];
2523bf4e03dSLadislav Michl s32 value;
2533bf4e03dSLadislav Michl
2543bf4e03dSLadislav Michl value = convert_uAh_to_bin(info, val);
2553bf4e03dSLadislav Michl /* Direction depends on how sense+/- were connected */
2563bf4e03dSLadislav Michl if (info->Qlsb < 0)
2573bf4e03dSLadislav Michl value += 0xFFFF;
2583bf4e03dSLadislav Michl if ((value < 0) || (value > 0xFFFF)) /* input validation */
2593bf4e03dSLadislav Michl return -EINVAL;
2603bf4e03dSLadislav Michl
2613bf4e03dSLadislav Michl /* Set new charge value */
2623bf4e03dSLadislav Michl dataw[0] = I16_MSB(value);
2633bf4e03dSLadislav Michl dataw[1] = I16_LSB(value);
2643bf4e03dSLadislav Michl return ltc294x_write_regs(info->client, reg, &dataw[0], 2);
2653bf4e03dSLadislav Michl }
2663bf4e03dSLadislav Michl
ltc294x_get_charge_counter(const struct ltc294x_info * info,int * val)2678c0984e5SSebastian Reichel static int ltc294x_get_charge_counter(
2688c0984e5SSebastian Reichel const struct ltc294x_info *info, int *val)
2698c0984e5SSebastian Reichel {
2703bf4e03dSLadislav Michl int value = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
2718c0984e5SSebastian Reichel
2728c0984e5SSebastian Reichel if (value < 0)
2738c0984e5SSebastian Reichel return value;
2748c0984e5SSebastian Reichel value -= LTC294X_MID_SUPPLY;
2758c0984e5SSebastian Reichel *val = convert_bin_to_uAh(info, value);
2768c0984e5SSebastian Reichel return 0;
2778c0984e5SSebastian Reichel }
2788c0984e5SSebastian Reichel
ltc294x_get_voltage(const struct ltc294x_info * info,int * val)2798c0984e5SSebastian Reichel static int ltc294x_get_voltage(const struct ltc294x_info *info, int *val)
2808c0984e5SSebastian Reichel {
2818c0984e5SSebastian Reichel int ret;
2828c0984e5SSebastian Reichel u8 datar[2];
2838c0984e5SSebastian Reichel u32 value;
2848c0984e5SSebastian Reichel
2858c0984e5SSebastian Reichel ret = ltc294x_read_regs(info->client,
2868c0984e5SSebastian Reichel LTC294X_REG_VOLTAGE_MSB, &datar[0], 2);
2878c0984e5SSebastian Reichel value = (datar[0] << 8) | datar[1];
288a65df832SLadislav Michl switch (info->id) {
289a65df832SLadislav Michl case LTC2943_ID:
290a65df832SLadislav Michl value *= 23600 * 2;
291a65df832SLadislav Michl value /= 0xFFFF;
292a65df832SLadislav Michl value *= 1000 / 2;
293a65df832SLadislav Michl break;
29417825ff6SDragos Bogdan case LTC2944_ID:
29517825ff6SDragos Bogdan value *= 70800 / 5*4;
29617825ff6SDragos Bogdan value /= 0xFFFF;
29717825ff6SDragos Bogdan value *= 1000 * 5/4;
29817825ff6SDragos Bogdan break;
299a65df832SLadislav Michl default:
300a65df832SLadislav Michl value *= 6000 * 10;
301a65df832SLadislav Michl value /= 0xFFFF;
302a65df832SLadislav Michl value *= 1000 / 10;
303a65df832SLadislav Michl break;
304a65df832SLadislav Michl }
305a65df832SLadislav Michl *val = value;
3068c0984e5SSebastian Reichel return ret;
3078c0984e5SSebastian Reichel }
3088c0984e5SSebastian Reichel
ltc294x_get_current(const struct ltc294x_info * info,int * val)3098c0984e5SSebastian Reichel static int ltc294x_get_current(const struct ltc294x_info *info, int *val)
3108c0984e5SSebastian Reichel {
3118c0984e5SSebastian Reichel int ret;
3128c0984e5SSebastian Reichel u8 datar[2];
3138c0984e5SSebastian Reichel s32 value;
3148c0984e5SSebastian Reichel
3158c0984e5SSebastian Reichel ret = ltc294x_read_regs(info->client,
31663e67c57SLadislav Michl LTC2943_REG_CURRENT_MSB, &datar[0], 2);
3178c0984e5SSebastian Reichel value = (datar[0] << 8) | datar[1];
3188c0984e5SSebastian Reichel value -= 0x7FFF;
31917825ff6SDragos Bogdan if (info->id == LTC2944_ID)
32017825ff6SDragos Bogdan value *= 64000;
32117825ff6SDragos Bogdan else
32217825ff6SDragos Bogdan value *= 60000;
3238c0984e5SSebastian Reichel /* Value is in range -32k..+32k, r_sense is usually 10..50 mOhm,
3248c0984e5SSebastian Reichel * the formula below keeps everything in s32 range while preserving
3258c0984e5SSebastian Reichel * enough digits */
32617825ff6SDragos Bogdan *val = 1000 * (value / (info->r_sense * 0x7FFF)); /* in uA */
3278c0984e5SSebastian Reichel return ret;
3288c0984e5SSebastian Reichel }
3298c0984e5SSebastian Reichel
ltc294x_get_temperature(const struct ltc294x_info * info,int * val)3308c0984e5SSebastian Reichel static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val)
3318c0984e5SSebastian Reichel {
332a65df832SLadislav Michl enum ltc294x_reg reg;
3338c0984e5SSebastian Reichel int ret;
3348c0984e5SSebastian Reichel u8 datar[2];
3358c0984e5SSebastian Reichel u32 value;
3368c0984e5SSebastian Reichel
337a65df832SLadislav Michl if (info->id == LTC2942_ID) {
338a65df832SLadislav Michl reg = LTC2942_REG_TEMPERATURE_MSB;
339dde5953fSLadislav Michl value = 6000; /* Full-scale is 600 Kelvin */
340a65df832SLadislav Michl } else {
341a65df832SLadislav Michl reg = LTC2943_REG_TEMPERATURE_MSB;
342dde5953fSLadislav Michl value = 5100; /* Full-scale is 510 Kelvin */
343a65df832SLadislav Michl }
344a65df832SLadislav Michl ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
345a65df832SLadislav Michl value *= (datar[0] << 8) | datar[1];
346dde5953fSLadislav Michl /* Convert to tenths of degree Celsius */
347dde5953fSLadislav Michl *val = value / 0xFFFF - 2722;
3488c0984e5SSebastian Reichel return ret;
3498c0984e5SSebastian Reichel }
3508c0984e5SSebastian Reichel
ltc294x_get_property(struct power_supply * psy,enum power_supply_property prop,union power_supply_propval * val)3518c0984e5SSebastian Reichel static int ltc294x_get_property(struct power_supply *psy,
3528c0984e5SSebastian Reichel enum power_supply_property prop,
3538c0984e5SSebastian Reichel union power_supply_propval *val)
3548c0984e5SSebastian Reichel {
3558c0984e5SSebastian Reichel struct ltc294x_info *info = power_supply_get_drvdata(psy);
3568c0984e5SSebastian Reichel
3578c0984e5SSebastian Reichel switch (prop) {
3583bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_FULL:
3593bf4e03dSLadislav Michl return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_HIGH_MSB,
3603bf4e03dSLadislav Michl &val->intval);
3613bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_EMPTY:
3623bf4e03dSLadislav Michl return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_LOW_MSB,
3633bf4e03dSLadislav Michl &val->intval);
3648c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
3653bf4e03dSLadislav Michl return ltc294x_get_charge(info, LTC294X_REG_ACC_CHARGE_MSB,
3663bf4e03dSLadislav Michl &val->intval);
3678c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3688c0984e5SSebastian Reichel return ltc294x_get_charge_counter(info, &val->intval);
3698c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3708c0984e5SSebastian Reichel return ltc294x_get_voltage(info, &val->intval);
3718c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_NOW:
3728c0984e5SSebastian Reichel return ltc294x_get_current(info, &val->intval);
3738c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TEMP:
3748c0984e5SSebastian Reichel return ltc294x_get_temperature(info, &val->intval);
3758c0984e5SSebastian Reichel default:
3768c0984e5SSebastian Reichel return -EINVAL;
3778c0984e5SSebastian Reichel }
3788c0984e5SSebastian Reichel }
3798c0984e5SSebastian Reichel
ltc294x_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)3808c0984e5SSebastian Reichel static int ltc294x_set_property(struct power_supply *psy,
3818c0984e5SSebastian Reichel enum power_supply_property psp,
3828c0984e5SSebastian Reichel const union power_supply_propval *val)
3838c0984e5SSebastian Reichel {
3848c0984e5SSebastian Reichel struct ltc294x_info *info = power_supply_get_drvdata(psy);
3858c0984e5SSebastian Reichel
3868c0984e5SSebastian Reichel switch (psp) {
3873bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_FULL:
3883bf4e03dSLadislav Michl return ltc294x_set_charge_thr(info,
3893bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_HIGH_MSB, val->intval);
3903bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_EMPTY:
3913bf4e03dSLadislav Michl return ltc294x_set_charge_thr(info,
3923bf4e03dSLadislav Michl LTC294X_REG_CHARGE_THR_LOW_MSB, val->intval);
3938c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
3948c0984e5SSebastian Reichel return ltc294x_set_charge_now(info, val->intval);
3958c0984e5SSebastian Reichel default:
3968c0984e5SSebastian Reichel return -EPERM;
3978c0984e5SSebastian Reichel }
3988c0984e5SSebastian Reichel }
3998c0984e5SSebastian Reichel
ltc294x_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)4008c0984e5SSebastian Reichel static int ltc294x_property_is_writeable(
4018c0984e5SSebastian Reichel struct power_supply *psy, enum power_supply_property psp)
4028c0984e5SSebastian Reichel {
4038c0984e5SSebastian Reichel switch (psp) {
4043bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_FULL:
4053bf4e03dSLadislav Michl case POWER_SUPPLY_PROP_CHARGE_EMPTY:
4068c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
4078c0984e5SSebastian Reichel return 1;
4088c0984e5SSebastian Reichel default:
4098c0984e5SSebastian Reichel return 0;
4108c0984e5SSebastian Reichel }
4118c0984e5SSebastian Reichel }
4128c0984e5SSebastian Reichel
ltc294x_update(struct ltc294x_info * info)4138c0984e5SSebastian Reichel static void ltc294x_update(struct ltc294x_info *info)
4148c0984e5SSebastian Reichel {
4153bf4e03dSLadislav Michl int charge = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
4168c0984e5SSebastian Reichel
4178c0984e5SSebastian Reichel if (charge != info->charge) {
4188c0984e5SSebastian Reichel info->charge = charge;
4198c0984e5SSebastian Reichel power_supply_changed(info->supply);
4208c0984e5SSebastian Reichel }
4218c0984e5SSebastian Reichel }
4228c0984e5SSebastian Reichel
ltc294x_work(struct work_struct * work)4238c0984e5SSebastian Reichel static void ltc294x_work(struct work_struct *work)
4248c0984e5SSebastian Reichel {
4258c0984e5SSebastian Reichel struct ltc294x_info *info;
4268c0984e5SSebastian Reichel
4278c0984e5SSebastian Reichel info = container_of(work, struct ltc294x_info, work.work);
4288c0984e5SSebastian Reichel ltc294x_update(info);
4298c0984e5SSebastian Reichel schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
4308c0984e5SSebastian Reichel }
4318c0984e5SSebastian Reichel
4328c0984e5SSebastian Reichel static enum power_supply_property ltc294x_properties[] = {
4338c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_COUNTER,
4343bf4e03dSLadislav Michl POWER_SUPPLY_PROP_CHARGE_FULL,
4353bf4e03dSLadislav Michl POWER_SUPPLY_PROP_CHARGE_EMPTY,
4368c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW,
4378c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW,
4388c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP,
43963e67c57SLadislav Michl POWER_SUPPLY_PROP_CURRENT_NOW,
4408c0984e5SSebastian Reichel };
4418c0984e5SSebastian Reichel
ltc294x_i2c_probe(struct i2c_client * client)44266d9e8fcSUwe Kleine-König static int ltc294x_i2c_probe(struct i2c_client *client)
4438c0984e5SSebastian Reichel {
4448c0984e5SSebastian Reichel struct power_supply_config psy_cfg = {};
4458c0984e5SSebastian Reichel struct ltc294x_info *info;
446a65df832SLadislav Michl struct device_node *np;
4478c0984e5SSebastian Reichel int ret;
4488c0984e5SSebastian Reichel u32 prescaler_exp;
4498c0984e5SSebastian Reichel s32 r_sense;
450a65df832SLadislav Michl u8 status;
4518c0984e5SSebastian Reichel
4528c0984e5SSebastian Reichel info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
4538c0984e5SSebastian Reichel if (info == NULL)
4548c0984e5SSebastian Reichel return -ENOMEM;
4558c0984e5SSebastian Reichel
4568c0984e5SSebastian Reichel i2c_set_clientdata(client, info);
4578c0984e5SSebastian Reichel
4588c0984e5SSebastian Reichel np = of_node_get(client->dev.of_node);
4598c0984e5SSebastian Reichel
46058d1620cSIskren Chernev info->id = (enum ltc294x_id) (uintptr_t) of_device_get_match_data(
46158d1620cSIskren Chernev &client->dev);
4628c0984e5SSebastian Reichel info->supply_desc.name = np->name;
4638c0984e5SSebastian Reichel
4648c0984e5SSebastian Reichel /* r_sense can be negative, when sense+ is connected to the battery
4658c0984e5SSebastian Reichel * instead of the sense-. This results in reversed measurements. */
4668c0984e5SSebastian Reichel ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
467513e3b53SMichał Mirosław if (ret < 0)
468513e3b53SMichał Mirosław return dev_err_probe(&client->dev, ret,
4698c0984e5SSebastian Reichel "Could not find lltc,resistor-sense in devicetree\n");
4708c0984e5SSebastian Reichel info->r_sense = r_sense;
4718c0984e5SSebastian Reichel
4728c0984e5SSebastian Reichel ret = of_property_read_u32(np, "lltc,prescaler-exponent",
4738c0984e5SSebastian Reichel &prescaler_exp);
4748c0984e5SSebastian Reichel if (ret < 0) {
4758c0984e5SSebastian Reichel dev_warn(&client->dev,
4768c0984e5SSebastian Reichel "lltc,prescaler-exponent not in devicetree\n");
4778c0984e5SSebastian Reichel prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
4788c0984e5SSebastian Reichel }
4798c0984e5SSebastian Reichel
48063e67c57SLadislav Michl if (info->id == LTC2943_ID) {
4818c0984e5SSebastian Reichel if (prescaler_exp > LTC2943_MAX_PRESCALER_EXP)
4828c0984e5SSebastian Reichel prescaler_exp = LTC2943_MAX_PRESCALER_EXP;
483cf215c37SMichał Mirosław info->Qlsb = ((340 * 50000) / r_sense) >>
484cf215c37SMichał Mirosław (12 - 2*prescaler_exp);
4858c0984e5SSebastian Reichel } else {
4868c0984e5SSebastian Reichel if (prescaler_exp > LTC2941_MAX_PRESCALER_EXP)
4878c0984e5SSebastian Reichel prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
488cf215c37SMichał Mirosław info->Qlsb = ((85 * 50000) / r_sense) >>
489cf215c37SMichał Mirosław (7 - prescaler_exp);
4908c0984e5SSebastian Reichel }
4918c0984e5SSebastian Reichel
492a65df832SLadislav Michl /* Read status register to check for LTC2942 */
493a65df832SLadislav Michl if (info->id == LTC2941_ID || info->id == LTC2942_ID) {
494a65df832SLadislav Michl ret = ltc294x_read_regs(client, LTC294X_REG_STATUS, &status, 1);
495513e3b53SMichał Mirosław if (ret < 0)
496513e3b53SMichał Mirosław return dev_err_probe(&client->dev, ret,
497a65df832SLadislav Michl "Could not read status register\n");
498a65df832SLadislav Michl if (status & LTC2941_REG_STATUS_CHIP_ID)
499a65df832SLadislav Michl info->id = LTC2941_ID;
500a65df832SLadislav Michl else
501a65df832SLadislav Michl info->id = LTC2942_ID;
502a65df832SLadislav Michl }
503a65df832SLadislav Michl
5048c0984e5SSebastian Reichel info->client = client;
5058c0984e5SSebastian Reichel info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
5068c0984e5SSebastian Reichel info->supply_desc.properties = ltc294x_properties;
50763e67c57SLadislav Michl switch (info->id) {
50817825ff6SDragos Bogdan case LTC2944_ID:
50963e67c57SLadislav Michl case LTC2943_ID:
5108c0984e5SSebastian Reichel info->supply_desc.num_properties =
5118c0984e5SSebastian Reichel ARRAY_SIZE(ltc294x_properties);
51263e67c57SLadislav Michl break;
513a65df832SLadislav Michl case LTC2942_ID:
514a65df832SLadislav Michl info->supply_desc.num_properties =
515a65df832SLadislav Michl ARRAY_SIZE(ltc294x_properties) - 1;
516a65df832SLadislav Michl break;
51763e67c57SLadislav Michl case LTC2941_ID:
51863e67c57SLadislav Michl default:
5198c0984e5SSebastian Reichel info->supply_desc.num_properties =
5208c0984e5SSebastian Reichel ARRAY_SIZE(ltc294x_properties) - 3;
52163e67c57SLadislav Michl break;
52263e67c57SLadislav Michl }
5238c0984e5SSebastian Reichel info->supply_desc.get_property = ltc294x_get_property;
5248c0984e5SSebastian Reichel info->supply_desc.set_property = ltc294x_set_property;
5258c0984e5SSebastian Reichel info->supply_desc.property_is_writeable = ltc294x_property_is_writeable;
5268c0984e5SSebastian Reichel info->supply_desc.external_power_changed = NULL;
5278c0984e5SSebastian Reichel
5288c0984e5SSebastian Reichel psy_cfg.drv_data = info;
5298c0984e5SSebastian Reichel
5306d0c5de2SMatti Vaittinen ret = devm_delayed_work_autocancel(&client->dev, &info->work,
5316d0c5de2SMatti Vaittinen ltc294x_work);
5326d0c5de2SMatti Vaittinen if (ret)
5336d0c5de2SMatti Vaittinen return ret;
5348c0984e5SSebastian Reichel
5358c0984e5SSebastian Reichel ret = ltc294x_reset(info, prescaler_exp);
536513e3b53SMichał Mirosław if (ret < 0)
537513e3b53SMichał Mirosław return dev_err_probe(&client->dev, ret,
538513e3b53SMichał Mirosław "Communication with chip failed\n");
5398c0984e5SSebastian Reichel
5406d0c5de2SMatti Vaittinen info->supply = devm_power_supply_register(&client->dev,
5416d0c5de2SMatti Vaittinen &info->supply_desc, &psy_cfg);
542513e3b53SMichał Mirosław if (IS_ERR(info->supply))
543513e3b53SMichał Mirosław return dev_err_probe(&client->dev, PTR_ERR(info->supply),
544513e3b53SMichał Mirosław "failed to register ltc2941\n");
545513e3b53SMichał Mirosław
5468c0984e5SSebastian Reichel schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
5478c0984e5SSebastian Reichel
5488c0984e5SSebastian Reichel return 0;
5498c0984e5SSebastian Reichel }
5508c0984e5SSebastian Reichel
ltc294x_i2c_shutdown(struct i2c_client * client)5515f2f0d61SMike Looijmans static void ltc294x_i2c_shutdown(struct i2c_client *client)
5525f2f0d61SMike Looijmans {
5535f2f0d61SMike Looijmans struct ltc294x_info *info = i2c_get_clientdata(client);
5545f2f0d61SMike Looijmans int ret;
5555f2f0d61SMike Looijmans u8 value;
5565f2f0d61SMike Looijmans u8 control;
5575f2f0d61SMike Looijmans
5585f2f0d61SMike Looijmans /* The LTC2941 does not need any special handling */
5595f2f0d61SMike Looijmans if (info->id == LTC2941_ID)
5605f2f0d61SMike Looijmans return;
5615f2f0d61SMike Looijmans
5625f2f0d61SMike Looijmans /* Read control register */
5635f2f0d61SMike Looijmans ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
5645f2f0d61SMike Looijmans if (ret < 0)
5655f2f0d61SMike Looijmans return;
5665f2f0d61SMike Looijmans
5675f2f0d61SMike Looijmans /* Disable continuous ADC conversion as this drains the battery */
5685f2f0d61SMike Looijmans control = LTC294X_REG_CONTROL_ADC_DISABLE(value);
5695f2f0d61SMike Looijmans if (control != value)
5705f2f0d61SMike Looijmans ltc294x_write_regs(info->client, LTC294X_REG_CONTROL,
5715f2f0d61SMike Looijmans &control, 1);
5725f2f0d61SMike Looijmans }
5735f2f0d61SMike Looijmans
5748c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
5758c0984e5SSebastian Reichel
ltc294x_suspend(struct device * dev)5768c0984e5SSebastian Reichel static int ltc294x_suspend(struct device *dev)
5778c0984e5SSebastian Reichel {
5788c0984e5SSebastian Reichel struct i2c_client *client = to_i2c_client(dev);
5798c0984e5SSebastian Reichel struct ltc294x_info *info = i2c_get_clientdata(client);
5808c0984e5SSebastian Reichel
5818c0984e5SSebastian Reichel cancel_delayed_work(&info->work);
5828c0984e5SSebastian Reichel return 0;
5838c0984e5SSebastian Reichel }
5848c0984e5SSebastian Reichel
ltc294x_resume(struct device * dev)5858c0984e5SSebastian Reichel static int ltc294x_resume(struct device *dev)
5868c0984e5SSebastian Reichel {
5878c0984e5SSebastian Reichel struct i2c_client *client = to_i2c_client(dev);
5888c0984e5SSebastian Reichel struct ltc294x_info *info = i2c_get_clientdata(client);
5898c0984e5SSebastian Reichel
5908c0984e5SSebastian Reichel schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
5918c0984e5SSebastian Reichel return 0;
5928c0984e5SSebastian Reichel }
5938c0984e5SSebastian Reichel
5948c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume);
5958c0984e5SSebastian Reichel #define LTC294X_PM_OPS (<c294x_pm_ops)
5968c0984e5SSebastian Reichel
5978c0984e5SSebastian Reichel #else
5988c0984e5SSebastian Reichel #define LTC294X_PM_OPS NULL
5998c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */
6008c0984e5SSebastian Reichel
6018c0984e5SSebastian Reichel
6028c0984e5SSebastian Reichel static const struct i2c_device_id ltc294x_i2c_id[] = {
60363e67c57SLadislav Michl { "ltc2941", LTC2941_ID, },
604a65df832SLadislav Michl { "ltc2942", LTC2942_ID, },
60563e67c57SLadislav Michl { "ltc2943", LTC2943_ID, },
60617825ff6SDragos Bogdan { "ltc2944", LTC2944_ID, },
6078c0984e5SSebastian Reichel { },
6088c0984e5SSebastian Reichel };
6098c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id);
6108c0984e5SSebastian Reichel
6119697277eSJavier Martinez Canillas static const struct of_device_id ltc294x_i2c_of_match[] = {
6129697277eSJavier Martinez Canillas {
6139697277eSJavier Martinez Canillas .compatible = "lltc,ltc2941",
61463e67c57SLadislav Michl .data = (void *)LTC2941_ID,
6159697277eSJavier Martinez Canillas },
6169697277eSJavier Martinez Canillas {
617a65df832SLadislav Michl .compatible = "lltc,ltc2942",
618a65df832SLadislav Michl .data = (void *)LTC2942_ID,
619a65df832SLadislav Michl },
620a65df832SLadislav Michl {
6219697277eSJavier Martinez Canillas .compatible = "lltc,ltc2943",
62263e67c57SLadislav Michl .data = (void *)LTC2943_ID,
6239697277eSJavier Martinez Canillas },
62417825ff6SDragos Bogdan {
62517825ff6SDragos Bogdan .compatible = "lltc,ltc2944",
62617825ff6SDragos Bogdan .data = (void *)LTC2944_ID,
62717825ff6SDragos Bogdan },
6289697277eSJavier Martinez Canillas { },
6299697277eSJavier Martinez Canillas };
6309697277eSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, ltc294x_i2c_of_match);
6319697277eSJavier Martinez Canillas
6328c0984e5SSebastian Reichel static struct i2c_driver ltc294x_driver = {
6338c0984e5SSebastian Reichel .driver = {
6348c0984e5SSebastian Reichel .name = "LTC2941",
6359697277eSJavier Martinez Canillas .of_match_table = ltc294x_i2c_of_match,
6368c0984e5SSebastian Reichel .pm = LTC294X_PM_OPS,
6378c0984e5SSebastian Reichel },
638fe20b1dcSUwe Kleine-König .probe = ltc294x_i2c_probe,
6395f2f0d61SMike Looijmans .shutdown = ltc294x_i2c_shutdown,
6408c0984e5SSebastian Reichel .id_table = ltc294x_i2c_id,
6418c0984e5SSebastian Reichel };
6428c0984e5SSebastian Reichel module_i2c_driver(ltc294x_driver);
6438c0984e5SSebastian Reichel
6448c0984e5SSebastian Reichel MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems");
6458c0984e5SSebastian Reichel MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products");
64617825ff6SDragos Bogdan MODULE_DESCRIPTION("LTC2941/LTC2942/LTC2943/LTC2944 Battery Gas Gauge IC driver");
6478c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
648