blob: 9429e66be09641fd818f8da593d47c45124d3efe [file] [log] [blame]
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001/*
2 * SCI Reset driver for Keystone based devices
3 *
4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
5 * Andrew F. Davis <afd@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <asm/unaligned.h>
21
22#include <linux/power/bq27xxx_battery.h>
23
24static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
25{
26 struct bq27xxx_device_info *di = data;
27
28 bq27xxx_battery_update(di);
29
30 return IRQ_HANDLED;
31}
32
33static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
34 bool single)
35{
36 struct i2c_client *client = to_i2c_client(di->dev);
37 struct i2c_msg msg[2];
38 unsigned char data[2];
39 int ret;
40
41 if (!client->adapter)
42 return -ENODEV;
43
44 msg[0].addr = client->addr;
45 msg[0].flags = 0;
46 msg[0].buf = &reg;
47 msg[0].len = sizeof(reg);
48 msg[1].addr = client->addr;
49 msg[1].flags = I2C_M_RD;
50 msg[1].buf = data;
51 if (single)
52 msg[1].len = 1;
53 else
54 msg[1].len = 2;
55
56 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
57 if (ret < 0)
58 return ret;
59
60 if (!single)
61 ret = get_unaligned_le16(data);
62 else
63 ret = data[0];
64
65 return ret;
66}
67
68static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
69 const struct i2c_device_id *id)
70{
71 struct bq27xxx_device_info *di;
72 int ret;
73
74 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
75 if (!di)
76 return -ENOMEM;
77
78 di->dev = &client->dev;
79 di->chip = id->driver_data;
80 di->name = id->name;
81 di->bus.read = bq27xxx_battery_i2c_read;
82
83 ret = bq27xxx_battery_setup(di);
84 if (ret)
85 return ret;
86
87 /* Schedule a polling after about 1 min */
88 schedule_delayed_work(&di->work, 60 * HZ);
89
90 i2c_set_clientdata(client, di);
91
92 if (client->irq) {
93 ret = devm_request_threaded_irq(&client->dev, client->irq,
94 NULL, bq27xxx_battery_irq_handler_thread,
95 IRQF_ONESHOT,
96 di->name, di);
97 if (ret) {
98 dev_err(&client->dev,
99 "Unable to register IRQ %d error %d\n",
100 client->irq, ret);
101 return ret;
102 }
103 }
104
105 return 0;
106}
107
108static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
109{
110 struct bq27xxx_device_info *di = i2c_get_clientdata(client);
111
112 bq27xxx_battery_teardown(di);
113
114 return 0;
115}
116
117static const struct i2c_device_id bq27xxx_i2c_id_table[] = {
118 { "bq27200", BQ27000 },
119 { "bq27210", BQ27010 },
120 { "bq27500", BQ27500 },
121 { "bq27510", BQ27500 },
122 { "bq27520", BQ27500 },
123 { "bq27530", BQ27530 },
124 { "bq27531", BQ27530 },
125 { "bq27541", BQ27541 },
126 { "bq27542", BQ27541 },
127 { "bq27546", BQ27541 },
128 { "bq27742", BQ27541 },
129 { "bq27545", BQ27545 },
130 { "bq27421", BQ27421 },
131 { "bq27425", BQ27421 },
132 { "bq27441", BQ27421 },
133 { "bq27621", BQ27421 },
134 {},
135};
136MODULE_DEVICE_TABLE(i2c, bq27xxx_i2c_id_table);
137
138static struct i2c_driver bq27xxx_battery_i2c_driver = {
139 .driver = {
140 .name = "bq27xxx-battery",
141 },
142 .probe = bq27xxx_battery_i2c_probe,
143 .remove = bq27xxx_battery_i2c_remove,
144 .id_table = bq27xxx_i2c_id_table,
145};
146module_i2c_driver(bq27xxx_battery_i2c_driver);
147
148MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
149MODULE_DESCRIPTION("BQ27xxx battery monitor i2c driver");
150MODULE_LICENSE("GPL");