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 <linux/unaligned.h>
14 #include <linux/power/bq27xxx_battery.h>
16 static DEFINE_IDA(battery_id
);
18 static irqreturn_t
bq27xxx_battery_irq_handler_thread(int irq
, void *data
)
20 struct bq27xxx_device_info
*di
= data
;
22 bq27xxx_battery_update(di
);
27 static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info
*di
, u8 reg
,
30 struct i2c_client
*client
= to_i2c_client(di
->dev
);
31 struct i2c_msg msg
[2];
38 msg
[0].addr
= client
->addr
;
41 msg
[0].len
= sizeof(reg
);
42 msg
[1].addr
= client
->addr
;
43 msg
[1].flags
= I2C_M_RD
;
50 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
55 ret
= get_unaligned_le16(data
);
62 static int bq27xxx_battery_i2c_write(struct bq27xxx_device_info
*di
, u8 reg
,
63 int value
, bool single
)
65 struct i2c_client
*client
= to_i2c_client(di
->dev
);
78 put_unaligned_le16(value
, &data
[1]);
83 msg
.addr
= client
->addr
;
86 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
94 static int bq27xxx_battery_i2c_bulk_read(struct bq27xxx_device_info
*di
, u8 reg
,
97 struct i2c_client
*client
= to_i2c_client(di
->dev
);
100 if (!client
->adapter
)
103 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, len
, data
);
111 static int bq27xxx_battery_i2c_bulk_write(struct bq27xxx_device_info
*di
,
112 u8 reg
, u8
*data
, int len
)
114 struct i2c_client
*client
= to_i2c_client(di
->dev
);
119 if (!client
->adapter
)
123 memcpy(&buf
[1], data
, len
);
126 msg
.addr
= client
->addr
;
130 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
138 static void bq27xxx_battery_i2c_devm_ida_free(void *data
)
140 int num
= (long)data
;
142 ida_free(&battery_id
, num
);
145 static int bq27xxx_battery_i2c_probe(struct i2c_client
*client
)
147 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
148 struct bq27xxx_device_info
*di
;
153 /* Get new ID for the new battery device */
154 num
= ida_alloc(&battery_id
, GFP_KERNEL
);
157 ret
= devm_add_action_or_reset(&client
->dev
,
158 bq27xxx_battery_i2c_devm_ida_free
,
163 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "%s-%ld", id
->name
, num
);
167 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
171 di
->dev
= &client
->dev
;
172 di
->chip
= id
->driver_data
;
175 di
->bus
.read
= bq27xxx_battery_i2c_read
;
176 di
->bus
.write
= bq27xxx_battery_i2c_write
;
177 di
->bus
.read_bulk
= bq27xxx_battery_i2c_bulk_read
;
178 di
->bus
.write_bulk
= bq27xxx_battery_i2c_bulk_write
;
180 ret
= bq27xxx_battery_setup(di
);
184 /* Schedule a polling after about 1 min */
185 schedule_delayed_work(&di
->work
, 60 * HZ
);
187 i2c_set_clientdata(client
, di
);
190 ret
= request_threaded_irq(client
->irq
,
191 NULL
, bq27xxx_battery_irq_handler_thread
,
195 dev_err(&client
->dev
,
196 "Unable to register IRQ %d error %d\n",
198 bq27xxx_battery_teardown(di
);
206 static void bq27xxx_battery_i2c_remove(struct i2c_client
*client
)
208 struct bq27xxx_device_info
*di
= i2c_get_clientdata(client
);
211 free_irq(client
->irq
, di
);
213 bq27xxx_battery_teardown(di
);
216 static const struct i2c_device_id bq27xxx_i2c_id_table
[] = {
217 { "bq27200", BQ27000
},
218 { "bq27210", BQ27010
},
219 { "bq27500", BQ2750X
},
220 { "bq27510", BQ2751X
},
221 { "bq27520", BQ2752X
},
222 { "bq27500-1", BQ27500
},
223 { "bq27510g1", BQ27510G1
},
224 { "bq27510g2", BQ27510G2
},
225 { "bq27510g3", BQ27510G3
},
226 { "bq27520g1", BQ27520G1
},
227 { "bq27520g2", BQ27520G2
},
228 { "bq27520g3", BQ27520G3
},
229 { "bq27520g4", BQ27520G4
},
230 { "bq27521", BQ27521
},
231 { "bq27530", BQ27530
},
232 { "bq27531", BQ27531
},
233 { "bq27541", BQ27541
},
234 { "bq27542", BQ27542
},
235 { "bq27546", BQ27546
},
236 { "bq27742", BQ27742
},
237 { "bq27545", BQ27545
},
238 { "bq27411", BQ27411
},
239 { "bq27421", BQ27421
},
240 { "bq27425", BQ27425
},
241 { "bq27426", BQ27426
},
242 { "bq27441", BQ27441
},
243 { "bq27621", BQ27621
},
244 { "bq27z561", BQ27Z561
},
245 { "bq28z610", BQ28Z610
},
246 { "bq34z100", BQ34Z100
},
247 { "bq78z100", BQ78Z100
},
250 MODULE_DEVICE_TABLE(i2c
, bq27xxx_i2c_id_table
);
253 static const struct of_device_id bq27xxx_battery_i2c_of_match_table
[] = {
254 { .compatible
= "ti,bq27200" },
255 { .compatible
= "ti,bq27210" },
256 { .compatible
= "ti,bq27500" },
257 { .compatible
= "ti,bq27510" },
258 { .compatible
= "ti,bq27520" },
259 { .compatible
= "ti,bq27500-1" },
260 { .compatible
= "ti,bq27510g1" },
261 { .compatible
= "ti,bq27510g2" },
262 { .compatible
= "ti,bq27510g3" },
263 { .compatible
= "ti,bq27520g1" },
264 { .compatible
= "ti,bq27520g2" },
265 { .compatible
= "ti,bq27520g3" },
266 { .compatible
= "ti,bq27520g4" },
267 { .compatible
= "ti,bq27521" },
268 { .compatible
= "ti,bq27530" },
269 { .compatible
= "ti,bq27531" },
270 { .compatible
= "ti,bq27541" },
271 { .compatible
= "ti,bq27542" },
272 { .compatible
= "ti,bq27546" },
273 { .compatible
= "ti,bq27742" },
274 { .compatible
= "ti,bq27545" },
275 { .compatible
= "ti,bq27411" },
276 { .compatible
= "ti,bq27421" },
277 { .compatible
= "ti,bq27425" },
278 { .compatible
= "ti,bq27426" },
279 { .compatible
= "ti,bq27441" },
280 { .compatible
= "ti,bq27621" },
281 { .compatible
= "ti,bq27z561" },
282 { .compatible
= "ti,bq28z610" },
283 { .compatible
= "ti,bq34z100" },
284 { .compatible
= "ti,bq78z100" },
287 MODULE_DEVICE_TABLE(of
, bq27xxx_battery_i2c_of_match_table
);
290 static struct i2c_driver bq27xxx_battery_i2c_driver
= {
292 .name
= "bq27xxx-battery",
293 .of_match_table
= of_match_ptr(bq27xxx_battery_i2c_of_match_table
),
294 .pm
= &bq27xxx_battery_battery_pm_ops
,
296 .probe
= bq27xxx_battery_i2c_probe
,
297 .remove
= bq27xxx_battery_i2c_remove
,
298 .id_table
= bq27xxx_i2c_id_table
,
300 module_i2c_driver(bq27xxx_battery_i2c_driver
);
302 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
303 MODULE_DESCRIPTION("BQ27xxx battery monitor i2c driver");
304 MODULE_LICENSE("GPL");