1 // SPDX-License-Identifier: GPL-2.0
3 * BQ27xxx battery monitor I2C driver
5 * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <asm/unaligned.h>
14 #include <linux/power/bq27xxx_battery.h>
16 static DEFINE_IDR(battery_id
);
17 static DEFINE_MUTEX(battery_mutex
);
19 static irqreturn_t
bq27xxx_battery_irq_handler_thread(int irq
, void *data
)
21 struct bq27xxx_device_info
*di
= data
;
23 bq27xxx_battery_update(di
);
28 static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info
*di
, u8 reg
,
31 struct i2c_client
*client
= to_i2c_client(di
->dev
);
32 struct i2c_msg msg
[2];
39 msg
[0].addr
= client
->addr
;
42 msg
[0].len
= sizeof(reg
);
43 msg
[1].addr
= client
->addr
;
44 msg
[1].flags
= I2C_M_RD
;
51 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
56 ret
= get_unaligned_le16(data
);
63 static int bq27xxx_battery_i2c_write(struct bq27xxx_device_info
*di
, u8 reg
,
64 int value
, bool single
)
66 struct i2c_client
*client
= to_i2c_client(di
->dev
);
79 put_unaligned_le16(value
, &data
[1]);
84 msg
.addr
= client
->addr
;
87 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
95 static int bq27xxx_battery_i2c_bulk_read(struct bq27xxx_device_info
*di
, u8 reg
,
98 struct i2c_client
*client
= to_i2c_client(di
->dev
);
101 if (!client
->adapter
)
104 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, len
, data
);
112 static int bq27xxx_battery_i2c_bulk_write(struct bq27xxx_device_info
*di
,
113 u8 reg
, u8
*data
, int len
)
115 struct i2c_client
*client
= to_i2c_client(di
->dev
);
120 if (!client
->adapter
)
124 memcpy(&buf
[1], data
, len
);
127 msg
.addr
= client
->addr
;
131 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
139 static int bq27xxx_battery_i2c_probe(struct i2c_client
*client
,
140 const struct i2c_device_id
*id
)
142 struct bq27xxx_device_info
*di
;
147 /* Get new ID for the new battery device */
148 mutex_lock(&battery_mutex
);
149 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
150 mutex_unlock(&battery_mutex
);
154 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "%s-%d", id
->name
, num
);
158 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
163 di
->dev
= &client
->dev
;
164 di
->chip
= id
->driver_data
;
167 di
->bus
.read
= bq27xxx_battery_i2c_read
;
168 di
->bus
.write
= bq27xxx_battery_i2c_write
;
169 di
->bus
.read_bulk
= bq27xxx_battery_i2c_bulk_read
;
170 di
->bus
.write_bulk
= bq27xxx_battery_i2c_bulk_write
;
172 ret
= bq27xxx_battery_setup(di
);
176 /* Schedule a polling after about 1 min */
177 schedule_delayed_work(&di
->work
, 60 * HZ
);
179 i2c_set_clientdata(client
, di
);
182 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
183 NULL
, bq27xxx_battery_irq_handler_thread
,
187 dev_err(&client
->dev
,
188 "Unable to register IRQ %d error %d\n",
200 mutex_lock(&battery_mutex
);
201 idr_remove(&battery_id
, num
);
202 mutex_unlock(&battery_mutex
);
207 static int bq27xxx_battery_i2c_remove(struct i2c_client
*client
)
209 struct bq27xxx_device_info
*di
= i2c_get_clientdata(client
);
211 bq27xxx_battery_teardown(di
);
213 mutex_lock(&battery_mutex
);
214 idr_remove(&battery_id
, di
->id
);
215 mutex_unlock(&battery_mutex
);
220 static const struct i2c_device_id bq27xxx_i2c_id_table
[] = {
221 { "bq27200", BQ27000
},
222 { "bq27210", BQ27010
},
223 { "bq27500", BQ2750X
},
224 { "bq27510", BQ2751X
},
225 { "bq27520", BQ2752X
},
226 { "bq27500-1", BQ27500
},
227 { "bq27510g1", BQ27510G1
},
228 { "bq27510g2", BQ27510G2
},
229 { "bq27510g3", BQ27510G3
},
230 { "bq27520g1", BQ27520G1
},
231 { "bq27520g2", BQ27520G2
},
232 { "bq27520g3", BQ27520G3
},
233 { "bq27520g4", BQ27520G4
},
234 { "bq27521", BQ27521
},
235 { "bq27530", BQ27530
},
236 { "bq27531", BQ27531
},
237 { "bq27541", BQ27541
},
238 { "bq27542", BQ27542
},
239 { "bq27546", BQ27546
},
240 { "bq27742", BQ27742
},
241 { "bq27545", BQ27545
},
242 { "bq27411", BQ27411
},
243 { "bq27421", BQ27421
},
244 { "bq27425", BQ27425
},
245 { "bq27426", BQ27426
},
246 { "bq27441", BQ27441
},
247 { "bq27621", BQ27621
},
248 { "bq27z561", BQ27Z561
},
249 { "bq28z610", BQ28Z610
},
250 { "bq34z100", BQ34Z100
},
253 MODULE_DEVICE_TABLE(i2c
, bq27xxx_i2c_id_table
);
256 static const struct of_device_id bq27xxx_battery_i2c_of_match_table
[] = {
257 { .compatible
= "ti,bq27200" },
258 { .compatible
= "ti,bq27210" },
259 { .compatible
= "ti,bq27500" },
260 { .compatible
= "ti,bq27510" },
261 { .compatible
= "ti,bq27520" },
262 { .compatible
= "ti,bq27500-1" },
263 { .compatible
= "ti,bq27510g1" },
264 { .compatible
= "ti,bq27510g2" },
265 { .compatible
= "ti,bq27510g3" },
266 { .compatible
= "ti,bq27520g1" },
267 { .compatible
= "ti,bq27520g2" },
268 { .compatible
= "ti,bq27520g3" },
269 { .compatible
= "ti,bq27520g4" },
270 { .compatible
= "ti,bq27521" },
271 { .compatible
= "ti,bq27530" },
272 { .compatible
= "ti,bq27531" },
273 { .compatible
= "ti,bq27541" },
274 { .compatible
= "ti,bq27542" },
275 { .compatible
= "ti,bq27546" },
276 { .compatible
= "ti,bq27742" },
277 { .compatible
= "ti,bq27545" },
278 { .compatible
= "ti,bq27411" },
279 { .compatible
= "ti,bq27421" },
280 { .compatible
= "ti,bq27425" },
281 { .compatible
= "ti,bq27426" },
282 { .compatible
= "ti,bq27441" },
283 { .compatible
= "ti,bq27621" },
284 { .compatible
= "ti,bq27z561" },
285 { .compatible
= "ti,bq28z610" },
286 { .compatible
= "ti,bq34z100" },
289 MODULE_DEVICE_TABLE(of
, bq27xxx_battery_i2c_of_match_table
);
292 static struct i2c_driver bq27xxx_battery_i2c_driver
= {
294 .name
= "bq27xxx-battery",
295 .of_match_table
= of_match_ptr(bq27xxx_battery_i2c_of_match_table
),
297 .probe
= bq27xxx_battery_i2c_probe
,
298 .remove
= bq27xxx_battery_i2c_remove
,
299 .id_table
= bq27xxx_i2c_id_table
,
301 module_i2c_driver(bq27xxx_battery_i2c_driver
);
303 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
304 MODULE_DESCRIPTION("BQ27xxx battery monitor i2c driver");
305 MODULE_LICENSE("GPL");