144ff56c0SDan Murphy // SPDX-License-Identifier: GPL-2.0
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * BQ27xxx battery monitor I2C driver
48c0984e5SSebastian Reichel  *
581bd45fcSAlexander A. Klimov  * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
68c0984e5SSebastian Reichel  *	Andrew F. Davis <afd@ti.com>
78c0984e5SSebastian Reichel  */
88c0984e5SSebastian Reichel 
98c0984e5SSebastian Reichel #include <linux/i2c.h>
108c0984e5SSebastian Reichel #include <linux/interrupt.h>
118c0984e5SSebastian Reichel #include <linux/module.h>
128c0984e5SSebastian Reichel #include <asm/unaligned.h>
138c0984e5SSebastian Reichel 
148c0984e5SSebastian Reichel #include <linux/power/bq27xxx_battery.h>
158c0984e5SSebastian Reichel 
168c0984e5SSebastian Reichel static DEFINE_IDR(battery_id);
178c0984e5SSebastian Reichel static DEFINE_MUTEX(battery_mutex);
188c0984e5SSebastian Reichel 
bq27xxx_battery_irq_handler_thread(int irq,void * data)198c0984e5SSebastian Reichel static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
208c0984e5SSebastian Reichel {
218c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = data;
228c0984e5SSebastian Reichel 
238c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
248c0984e5SSebastian Reichel 
258c0984e5SSebastian Reichel 	return IRQ_HANDLED;
268c0984e5SSebastian Reichel }
278c0984e5SSebastian Reichel 
bq27xxx_battery_i2c_read(struct bq27xxx_device_info * di,u8 reg,bool single)288c0984e5SSebastian Reichel static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
298c0984e5SSebastian Reichel 				    bool single)
308c0984e5SSebastian Reichel {
318c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(di->dev);
328c0984e5SSebastian Reichel 	struct i2c_msg msg[2];
3314073f66SMatt Ranostay 	u8 data[2];
348c0984e5SSebastian Reichel 	int ret;
358c0984e5SSebastian Reichel 
368c0984e5SSebastian Reichel 	if (!client->adapter)
378c0984e5SSebastian Reichel 		return -ENODEV;
388c0984e5SSebastian Reichel 
398c0984e5SSebastian Reichel 	msg[0].addr = client->addr;
408c0984e5SSebastian Reichel 	msg[0].flags = 0;
418c0984e5SSebastian Reichel 	msg[0].buf = &reg;
428c0984e5SSebastian Reichel 	msg[0].len = sizeof(reg);
438c0984e5SSebastian Reichel 	msg[1].addr = client->addr;
448c0984e5SSebastian Reichel 	msg[1].flags = I2C_M_RD;
458c0984e5SSebastian Reichel 	msg[1].buf = data;
468c0984e5SSebastian Reichel 	if (single)
478c0984e5SSebastian Reichel 		msg[1].len = 1;
488c0984e5SSebastian Reichel 	else
498c0984e5SSebastian Reichel 		msg[1].len = 2;
508c0984e5SSebastian Reichel 
518c0984e5SSebastian Reichel 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
528c0984e5SSebastian Reichel 	if (ret < 0)
538c0984e5SSebastian Reichel 		return ret;
548c0984e5SSebastian Reichel 
558c0984e5SSebastian Reichel 	if (!single)
568c0984e5SSebastian Reichel 		ret = get_unaligned_le16(data);
578c0984e5SSebastian Reichel 	else
588c0984e5SSebastian Reichel 		ret = data[0];
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel 	return ret;
618c0984e5SSebastian Reichel }
628c0984e5SSebastian Reichel 
bq27xxx_battery_i2c_write(struct bq27xxx_device_info * di,u8 reg,int value,bool single)6314073f66SMatt Ranostay static int bq27xxx_battery_i2c_write(struct bq27xxx_device_info *di, u8 reg,
6414073f66SMatt Ranostay 				     int value, bool single)
6514073f66SMatt Ranostay {
6614073f66SMatt Ranostay 	struct i2c_client *client = to_i2c_client(di->dev);
6714073f66SMatt Ranostay 	struct i2c_msg msg;
6814073f66SMatt Ranostay 	u8 data[4];
6914073f66SMatt Ranostay 	int ret;
7014073f66SMatt Ranostay 
7114073f66SMatt Ranostay 	if (!client->adapter)
7214073f66SMatt Ranostay 		return -ENODEV;
7314073f66SMatt Ranostay 
7414073f66SMatt Ranostay 	data[0] = reg;
7514073f66SMatt Ranostay 	if (single) {
7614073f66SMatt Ranostay 		data[1] = (u8) value;
7714073f66SMatt Ranostay 		msg.len = 2;
7814073f66SMatt Ranostay 	} else {
7914073f66SMatt Ranostay 		put_unaligned_le16(value, &data[1]);
8014073f66SMatt Ranostay 		msg.len = 3;
8114073f66SMatt Ranostay 	}
8214073f66SMatt Ranostay 
8314073f66SMatt Ranostay 	msg.buf = data;
8414073f66SMatt Ranostay 	msg.addr = client->addr;
8514073f66SMatt Ranostay 	msg.flags = 0;
8614073f66SMatt Ranostay 
8714073f66SMatt Ranostay 	ret = i2c_transfer(client->adapter, &msg, 1);
8814073f66SMatt Ranostay 	if (ret < 0)
8914073f66SMatt Ranostay 		return ret;
9014073f66SMatt Ranostay 	if (ret != 1)
9114073f66SMatt Ranostay 		return -EINVAL;
9214073f66SMatt Ranostay 	return 0;
9314073f66SMatt Ranostay }
9414073f66SMatt Ranostay 
bq27xxx_battery_i2c_bulk_read(struct bq27xxx_device_info * di,u8 reg,u8 * data,int len)9514073f66SMatt Ranostay static int bq27xxx_battery_i2c_bulk_read(struct bq27xxx_device_info *di, u8 reg,
9614073f66SMatt Ranostay 					 u8 *data, int len)
9714073f66SMatt Ranostay {
9814073f66SMatt Ranostay 	struct i2c_client *client = to_i2c_client(di->dev);
9914073f66SMatt Ranostay 	int ret;
10014073f66SMatt Ranostay 
10114073f66SMatt Ranostay 	if (!client->adapter)
10214073f66SMatt Ranostay 		return -ENODEV;
10314073f66SMatt Ranostay 
10414073f66SMatt Ranostay 	ret = i2c_smbus_read_i2c_block_data(client, reg, len, data);
10514073f66SMatt Ranostay 	if (ret < 0)
10614073f66SMatt Ranostay 		return ret;
10714073f66SMatt Ranostay 	if (ret != len)
10814073f66SMatt Ranostay 		return -EINVAL;
10914073f66SMatt Ranostay 	return 0;
11014073f66SMatt Ranostay }
11114073f66SMatt Ranostay 
bq27xxx_battery_i2c_bulk_write(struct bq27xxx_device_info * di,u8 reg,u8 * data,int len)11214073f66SMatt Ranostay static int bq27xxx_battery_i2c_bulk_write(struct bq27xxx_device_info *di,
11314073f66SMatt Ranostay 					  u8 reg, u8 *data, int len)
11414073f66SMatt Ranostay {
11514073f66SMatt Ranostay 	struct i2c_client *client = to_i2c_client(di->dev);
11614073f66SMatt Ranostay 	struct i2c_msg msg;
11714073f66SMatt Ranostay 	u8 buf[33];
11814073f66SMatt Ranostay 	int ret;
11914073f66SMatt Ranostay 
12014073f66SMatt Ranostay 	if (!client->adapter)
12114073f66SMatt Ranostay 		return -ENODEV;
12214073f66SMatt Ranostay 
12314073f66SMatt Ranostay 	buf[0] = reg;
12414073f66SMatt Ranostay 	memcpy(&buf[1], data, len);
12514073f66SMatt Ranostay 
12614073f66SMatt Ranostay 	msg.buf = buf;
12714073f66SMatt Ranostay 	msg.addr = client->addr;
12814073f66SMatt Ranostay 	msg.flags = 0;
12914073f66SMatt Ranostay 	msg.len = len + 1;
13014073f66SMatt Ranostay 
13114073f66SMatt Ranostay 	ret = i2c_transfer(client->adapter, &msg, 1);
13214073f66SMatt Ranostay 	if (ret < 0)
13314073f66SMatt Ranostay 		return ret;
13414073f66SMatt Ranostay 	if (ret != 1)
13514073f66SMatt Ranostay 		return -EINVAL;
13614073f66SMatt Ranostay 	return 0;
13714073f66SMatt Ranostay }
13814073f66SMatt Ranostay 
bq27xxx_battery_i2c_probe(struct i2c_client * client)13967f56c79SUwe Kleine-König static int bq27xxx_battery_i2c_probe(struct i2c_client *client)
1408c0984e5SSebastian Reichel {
14167f56c79SUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
1428c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di;
1438c0984e5SSebastian Reichel 	int ret;
1448c0984e5SSebastian Reichel 	char *name;
1458c0984e5SSebastian Reichel 	int num;
1468c0984e5SSebastian Reichel 
1478c0984e5SSebastian Reichel 	/* Get new ID for the new battery device */
1488c0984e5SSebastian Reichel 	mutex_lock(&battery_mutex);
1498c0984e5SSebastian Reichel 	num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
1508c0984e5SSebastian Reichel 	mutex_unlock(&battery_mutex);
1518c0984e5SSebastian Reichel 	if (num < 0)
1528c0984e5SSebastian Reichel 		return num;
1538c0984e5SSebastian Reichel 
1548c0984e5SSebastian Reichel 	name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
1558c0984e5SSebastian Reichel 	if (!name)
1568c0984e5SSebastian Reichel 		goto err_mem;
1578c0984e5SSebastian Reichel 
1588c0984e5SSebastian Reichel 	di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
1598c0984e5SSebastian Reichel 	if (!di)
1608c0984e5SSebastian Reichel 		goto err_mem;
1618c0984e5SSebastian Reichel 
1628c0984e5SSebastian Reichel 	di->id = num;
1638c0984e5SSebastian Reichel 	di->dev = &client->dev;
1648c0984e5SSebastian Reichel 	di->chip = id->driver_data;
1658c0984e5SSebastian Reichel 	di->name = name;
16614073f66SMatt Ranostay 
1678c0984e5SSebastian Reichel 	di->bus.read = bq27xxx_battery_i2c_read;
16814073f66SMatt Ranostay 	di->bus.write = bq27xxx_battery_i2c_write;
16914073f66SMatt Ranostay 	di->bus.read_bulk = bq27xxx_battery_i2c_bulk_read;
17014073f66SMatt Ranostay 	di->bus.write_bulk = bq27xxx_battery_i2c_bulk_write;
1718c0984e5SSebastian Reichel 
1728c0984e5SSebastian Reichel 	ret = bq27xxx_battery_setup(di);
1738c0984e5SSebastian Reichel 	if (ret)
1748c0984e5SSebastian Reichel 		goto err_failed;
1758c0984e5SSebastian Reichel 
1768c0984e5SSebastian Reichel 	/* Schedule a polling after about 1 min */
1778c0984e5SSebastian Reichel 	schedule_delayed_work(&di->work, 60 * HZ);
1788c0984e5SSebastian Reichel 
1798c0984e5SSebastian Reichel 	i2c_set_clientdata(client, di);
1808c0984e5SSebastian Reichel 
1818c0984e5SSebastian Reichel 	if (client->irq) {
182444ff007SHans de Goede 		ret = request_threaded_irq(client->irq,
1838c0984e5SSebastian Reichel 				NULL, bq27xxx_battery_irq_handler_thread,
1848c0984e5SSebastian Reichel 				IRQF_ONESHOT,
1858c0984e5SSebastian Reichel 				di->name, di);
1868c0984e5SSebastian Reichel 		if (ret) {
1878c0984e5SSebastian Reichel 			dev_err(&client->dev,
1888c0984e5SSebastian Reichel 				"Unable to register IRQ %d error %d\n",
1898c0984e5SSebastian Reichel 				client->irq, ret);
190cdf10ffeSHans de Goede 			bq27xxx_battery_teardown(di);
191cdf10ffeSHans de Goede 			goto err_failed;
1928c0984e5SSebastian Reichel 		}
1938c0984e5SSebastian Reichel 	}
1948c0984e5SSebastian Reichel 
1958c0984e5SSebastian Reichel 	return 0;
1968c0984e5SSebastian Reichel 
1978c0984e5SSebastian Reichel err_mem:
1988c0984e5SSebastian Reichel 	ret = -ENOMEM;
1998c0984e5SSebastian Reichel 
2008c0984e5SSebastian Reichel err_failed:
2018c0984e5SSebastian Reichel 	mutex_lock(&battery_mutex);
2028c0984e5SSebastian Reichel 	idr_remove(&battery_id, num);
2038c0984e5SSebastian Reichel 	mutex_unlock(&battery_mutex);
2048c0984e5SSebastian Reichel 
2058c0984e5SSebastian Reichel 	return ret;
2068c0984e5SSebastian Reichel }
2078c0984e5SSebastian Reichel 
bq27xxx_battery_i2c_remove(struct i2c_client * client)208ed5c2f5fSUwe Kleine-König static void bq27xxx_battery_i2c_remove(struct i2c_client *client)
2098c0984e5SSebastian Reichel {
2108c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = i2c_get_clientdata(client);
2118c0984e5SSebastian Reichel 
212*fbca8baeSHans de Goede 	if (client->irq)
213444ff007SHans de Goede 		free_irq(client->irq, di);
214*fbca8baeSHans de Goede 
2158c0984e5SSebastian Reichel 	bq27xxx_battery_teardown(di);
2168c0984e5SSebastian Reichel 
2178c0984e5SSebastian Reichel 	mutex_lock(&battery_mutex);
2188c0984e5SSebastian Reichel 	idr_remove(&battery_id, di->id);
2198c0984e5SSebastian Reichel 	mutex_unlock(&battery_mutex);
2208c0984e5SSebastian Reichel }
2218c0984e5SSebastian Reichel 
2228c0984e5SSebastian Reichel static const struct i2c_device_id bq27xxx_i2c_id_table[] = {
2238c0984e5SSebastian Reichel 	{ "bq27200", BQ27000 },
2248c0984e5SSebastian Reichel 	{ "bq27210", BQ27010 },
225818e3012SChris Lapa 	{ "bq27500", BQ2750X },
2266da6e4bdSChris Lapa 	{ "bq27510", BQ2751X },
2273a731c64SLiam Breck 	{ "bq27520", BQ2752X },
22832833635SChris Lapa 	{ "bq27500-1", BQ27500 },
229bd28177fSChris Lapa 	{ "bq27510g1", BQ27510G1 },
230698a2bf5SChris Lapa 	{ "bq27510g2", BQ27510G2 },
23171375aa7SChris Lapa 	{ "bq27510g3", BQ27510G3 },
23268f2a813SChris Lapa 	{ "bq27520g1", BQ27520G1 },
233a5deb9a9SChris Lapa 	{ "bq27520g2", BQ27520G2 },
234825e915bSChris Lapa 	{ "bq27520g3", BQ27520G3 },
2358835cae5SChris Lapa 	{ "bq27520g4", BQ27520G4 },
23670a39e10SPavel Machek 	{ "bq27521", BQ27521 },
2378c0984e5SSebastian Reichel 	{ "bq27530", BQ27530 },
2383a731c64SLiam Breck 	{ "bq27531", BQ27531 },
2398c0984e5SSebastian Reichel 	{ "bq27541", BQ27541 },
2403a731c64SLiam Breck 	{ "bq27542", BQ27542 },
2413a731c64SLiam Breck 	{ "bq27546", BQ27546 },
2423a731c64SLiam Breck 	{ "bq27742", BQ27742 },
2438c0984e5SSebastian Reichel 	{ "bq27545", BQ27545 },
244457b42f0SLiu Xiang 	{ "bq27411", BQ27411 },
2458c0984e5SSebastian Reichel 	{ "bq27421", BQ27421 },
2463a731c64SLiam Breck 	{ "bq27425", BQ27425 },
2475ef6a160SAndrew F. Davis 	{ "bq27426", BQ27426 },
2483a731c64SLiam Breck 	{ "bq27441", BQ27441 },
2493a731c64SLiam Breck 	{ "bq27621", BQ27621 },
2506f24ff97SDan Murphy 	{ "bq27z561", BQ27Z561 },
251707d678aSDan Murphy 	{ "bq28z610", BQ28Z610 },
25241a7431dSKrzysztof Kozlowski 	{ "bq34z100", BQ34Z100 },
2534eed7f5aSLI Qingwu 	{ "bq78z100", BQ78Z100 },
2548c0984e5SSebastian Reichel 	{},
2558c0984e5SSebastian Reichel };
2568c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, bq27xxx_i2c_id_table);
2578c0984e5SSebastian Reichel 
2588c0984e5SSebastian Reichel #ifdef CONFIG_OF
2598c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_i2c_of_match_table[] = {
2608c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27200" },
2618c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27210" },
2628c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27500" },
2638c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27510" },
2648c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27520" },
26532833635SChris Lapa 	{ .compatible = "ti,bq27500-1" },
266bd28177fSChris Lapa 	{ .compatible = "ti,bq27510g1" },
267698a2bf5SChris Lapa 	{ .compatible = "ti,bq27510g2" },
26871375aa7SChris Lapa 	{ .compatible = "ti,bq27510g3" },
26968f2a813SChris Lapa 	{ .compatible = "ti,bq27520g1" },
270a5deb9a9SChris Lapa 	{ .compatible = "ti,bq27520g2" },
271825e915bSChris Lapa 	{ .compatible = "ti,bq27520g3" },
2728835cae5SChris Lapa 	{ .compatible = "ti,bq27520g4" },
27370a39e10SPavel Machek 	{ .compatible = "ti,bq27521" },
2748c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27530" },
2758c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27531" },
2768c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27541" },
2778c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27542" },
2788c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27546" },
2798c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27742" },
2808c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27545" },
281457b42f0SLiu Xiang 	{ .compatible = "ti,bq27411" },
2828c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27421" },
2838c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27425" },
2845ef6a160SAndrew F. Davis 	{ .compatible = "ti,bq27426" },
2858c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27441" },
2868c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27621" },
2876f24ff97SDan Murphy 	{ .compatible = "ti,bq27z561" },
288707d678aSDan Murphy 	{ .compatible = "ti,bq28z610" },
28941a7431dSKrzysztof Kozlowski 	{ .compatible = "ti,bq34z100" },
2904eed7f5aSLI Qingwu 	{ .compatible = "ti,bq78z100" },
2918c0984e5SSebastian Reichel 	{},
2928c0984e5SSebastian Reichel };
2938c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_i2c_of_match_table);
2948c0984e5SSebastian Reichel #endif
2958c0984e5SSebastian Reichel 
2968c0984e5SSebastian Reichel static struct i2c_driver bq27xxx_battery_i2c_driver = {
2978c0984e5SSebastian Reichel 	.driver = {
2988c0984e5SSebastian Reichel 		.name = "bq27xxx-battery",
2998c0984e5SSebastian Reichel 		.of_match_table = of_match_ptr(bq27xxx_battery_i2c_of_match_table),
3008c0984e5SSebastian Reichel 	},
301fe20b1dcSUwe Kleine-König 	.probe = bq27xxx_battery_i2c_probe,
3028c0984e5SSebastian Reichel 	.remove = bq27xxx_battery_i2c_remove,
3038c0984e5SSebastian Reichel 	.id_table = bq27xxx_i2c_id_table,
3048c0984e5SSebastian Reichel };
3058c0984e5SSebastian Reichel module_i2c_driver(bq27xxx_battery_i2c_driver);
3068c0984e5SSebastian Reichel 
3078c0984e5SSebastian Reichel MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
3088c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor i2c driver");
3098c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
310