Commit | Line | Data |
---|---|---|
9d60595a AK |
1 | /* |
2 | * Maxim MAX14656 / AL32 USB Charger Detector driver | |
3 | * | |
4 | * Copyright (C) 2014 LG Electronics, Inc | |
5 | * Copyright (C) 2016 Alexander Kurz <akurz@blala.de> | |
6 | * | |
7 | * Components from Maxim AL32 Charger detection Driver for MX50 Yoshi Board | |
8 | * Copyright (C) Amazon Technologies Inc. All rights reserved. | |
9 | * Manish Lachwani (lachwani@lab126.com) | |
10 | * | |
11 | * This package is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 and | |
13 | * only version 2 as published by the Free Software Foundation. | |
14 | * | |
15 | */ | |
16 | #include <linux/module.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/delay.h> | |
19 | #include <linux/i2c.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/gpio.h> | |
23 | #include <linux/of_gpio.h> | |
24 | #include <linux/of_device.h> | |
25 | #include <linux/workqueue.h> | |
26 | #include <linux/power_supply.h> | |
27 | ||
28 | #define MAX14656_MANUFACTURER "Maxim Integrated" | |
29 | #define MAX14656_NAME "max14656" | |
30 | ||
31 | #define MAX14656_DEVICE_ID 0x00 | |
32 | #define MAX14656_INTERRUPT_1 0x01 | |
33 | #define MAX14656_INTERRUPT_2 0x02 | |
34 | #define MAX14656_STATUS_1 0x03 | |
35 | #define MAX14656_STATUS_2 0x04 | |
36 | #define MAX14656_INTMASK_1 0x05 | |
37 | #define MAX14656_INTMASK_2 0x06 | |
38 | #define MAX14656_CONTROL_1 0x07 | |
39 | #define MAX14656_CONTROL_2 0x08 | |
40 | #define MAX14656_CONTROL_3 0x09 | |
41 | ||
42 | #define DEVICE_VENDOR_MASK 0xf0 | |
43 | #define DEVICE_REV_MASK 0x0f | |
44 | #define INT_EN_REG_MASK BIT(4) | |
45 | #define CHG_TYPE_INT_MASK BIT(0) | |
46 | #define STATUS1_VB_VALID_MASK BIT(4) | |
47 | #define STATUS1_CHG_TYPE_MASK 0xf | |
48 | #define INT1_DCD_TIMEOUT_MASK BIT(7) | |
49 | #define CONTROL1_DEFAULT 0x0d | |
50 | #define CONTROL1_INT_EN BIT(4) | |
51 | #define CONTROL1_INT_ACTIVE_HIGH BIT(5) | |
52 | #define CONTROL1_EDGE BIT(7) | |
53 | #define CONTROL2_DEFAULT 0x8e | |
54 | #define CONTROL2_ADC_EN BIT(0) | |
55 | #define CONTROL3_DEFAULT 0x8d | |
56 | ||
57 | enum max14656_chg_type { | |
58 | MAX14656_NO_CHARGER = 0, | |
59 | MAX14656_SDP_CHARGER, | |
60 | MAX14656_CDP_CHARGER, | |
61 | MAX14656_DCP_CHARGER, | |
62 | MAX14656_APPLE_500MA_CHARGER, | |
63 | MAX14656_APPLE_1A_CHARGER, | |
64 | MAX14656_APPLE_2A_CHARGER, | |
65 | MAX14656_SPECIAL_500MA_CHARGER, | |
66 | MAX14656_APPLE_12W, | |
67 | MAX14656_CHARGER_LAST | |
68 | }; | |
69 | ||
70 | static const struct max14656_chg_type_props { | |
71 | enum power_supply_type type; | |
72 | } chg_type_props[] = { | |
73 | { POWER_SUPPLY_TYPE_UNKNOWN }, | |
74 | { POWER_SUPPLY_TYPE_USB }, | |
75 | { POWER_SUPPLY_TYPE_USB_CDP }, | |
76 | { POWER_SUPPLY_TYPE_USB_DCP }, | |
77 | { POWER_SUPPLY_TYPE_USB_DCP }, | |
78 | { POWER_SUPPLY_TYPE_USB_DCP }, | |
79 | { POWER_SUPPLY_TYPE_USB_DCP }, | |
80 | { POWER_SUPPLY_TYPE_USB_DCP }, | |
81 | { POWER_SUPPLY_TYPE_USB }, | |
82 | }; | |
83 | ||
84 | struct max14656_chip { | |
85 | struct i2c_client *client; | |
86 | struct power_supply *detect_psy; | |
87 | struct power_supply_desc psy_desc; | |
88 | struct delayed_work irq_work; | |
89 | ||
90 | int irq; | |
91 | int online; | |
92 | }; | |
93 | ||
94 | static int max14656_read_reg(struct i2c_client *client, int reg, u8 *val) | |
95 | { | |
96 | s32 ret; | |
97 | ||
98 | ret = i2c_smbus_read_byte_data(client, reg); | |
99 | if (ret < 0) { | |
100 | dev_err(&client->dev, | |
101 | "i2c read fail: can't read from %02x: %d\n", | |
102 | reg, ret); | |
103 | return ret; | |
104 | } | |
105 | *val = ret; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int max14656_write_reg(struct i2c_client *client, int reg, u8 val) | |
110 | { | |
111 | s32 ret; | |
112 | ||
113 | ret = i2c_smbus_write_byte_data(client, reg, val); | |
114 | if (ret < 0) { | |
115 | dev_err(&client->dev, | |
116 | "i2c write fail: can't write %02x to %02x: %d\n", | |
117 | val, reg, ret); | |
118 | return ret; | |
119 | } | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static int max14656_read_block_reg(struct i2c_client *client, u8 reg, | |
124 | u8 length, u8 *val) | |
125 | { | |
126 | int ret; | |
127 | ||
128 | ret = i2c_smbus_read_i2c_block_data(client, reg, length, val); | |
129 | if (ret < 0) { | |
130 | dev_err(&client->dev, "failed to block read reg 0x%x: %d\n", | |
131 | reg, ret); | |
132 | return ret; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | #define REG_TOTAL_NUM 5 | |
139 | static void max14656_irq_worker(struct work_struct *work) | |
140 | { | |
141 | struct max14656_chip *chip = | |
142 | container_of(work, struct max14656_chip, irq_work.work); | |
143 | ||
144 | u8 buf[REG_TOTAL_NUM]; | |
145 | u8 chg_type; | |
146 | int ret = 0; | |
147 | ||
148 | ret = max14656_read_block_reg(chip->client, MAX14656_DEVICE_ID, | |
149 | REG_TOTAL_NUM, buf); | |
150 | ||
151 | if ((buf[MAX14656_STATUS_1] & STATUS1_VB_VALID_MASK) && | |
152 | (buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK)) { | |
153 | chg_type = buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK; | |
154 | if (chg_type < MAX14656_CHARGER_LAST) | |
155 | chip->psy_desc.type = chg_type_props[chg_type].type; | |
156 | else | |
157 | chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN; | |
158 | chip->online = 1; | |
159 | } else { | |
160 | chip->online = 0; | |
161 | chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN; | |
162 | } | |
163 | ||
164 | power_supply_changed(chip->detect_psy); | |
165 | } | |
166 | ||
167 | static irqreturn_t max14656_irq(int irq, void *dev_id) | |
168 | { | |
169 | struct max14656_chip *chip = dev_id; | |
170 | ||
171 | schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(100)); | |
172 | ||
173 | return IRQ_HANDLED; | |
174 | } | |
175 | ||
176 | static int max14656_hw_init(struct max14656_chip *chip) | |
177 | { | |
178 | uint8_t val = 0; | |
179 | uint8_t rev; | |
180 | struct i2c_client *client = chip->client; | |
181 | ||
182 | if (max14656_read_reg(client, MAX14656_DEVICE_ID, &val)) | |
183 | return -ENODEV; | |
184 | ||
185 | if ((val & DEVICE_VENDOR_MASK) != 0x20) { | |
186 | dev_err(&client->dev, "wrong vendor ID %d\n", | |
187 | ((val & DEVICE_VENDOR_MASK) >> 4)); | |
188 | return -ENODEV; | |
189 | } | |
190 | rev = val & DEVICE_REV_MASK; | |
191 | ||
192 | /* Turn on ADC_EN */ | |
193 | if (max14656_write_reg(client, MAX14656_CONTROL_2, CONTROL2_ADC_EN)) | |
194 | return -EINVAL; | |
195 | ||
196 | /* turn on interrupts and low power mode */ | |
197 | if (max14656_write_reg(client, MAX14656_CONTROL_1, | |
198 | CONTROL1_DEFAULT | | |
199 | CONTROL1_INT_EN | | |
200 | CONTROL1_INT_ACTIVE_HIGH | | |
201 | CONTROL1_EDGE)) | |
202 | return -EINVAL; | |
203 | ||
204 | if (max14656_write_reg(client, MAX14656_INTMASK_1, 0x3)) | |
205 | return -EINVAL; | |
206 | ||
207 | if (max14656_write_reg(client, MAX14656_INTMASK_2, 0x1)) | |
208 | return -EINVAL; | |
209 | ||
210 | dev_info(&client->dev, "detected revision %d\n", rev); | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static int max14656_get_property(struct power_supply *psy, | |
215 | enum power_supply_property psp, | |
216 | union power_supply_propval *val) | |
217 | { | |
218 | struct max14656_chip *chip = power_supply_get_drvdata(psy); | |
219 | ||
220 | switch (psp) { | |
221 | case POWER_SUPPLY_PROP_ONLINE: | |
222 | val->intval = chip->online; | |
223 | break; | |
224 | case POWER_SUPPLY_PROP_MODEL_NAME: | |
225 | val->strval = MAX14656_NAME; | |
226 | break; | |
227 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
228 | val->strval = MAX14656_MANUFACTURER; | |
229 | break; | |
230 | default: | |
231 | return -EINVAL; | |
232 | } | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static enum power_supply_property max14656_battery_props[] = { | |
238 | POWER_SUPPLY_PROP_ONLINE, | |
239 | POWER_SUPPLY_PROP_MODEL_NAME, | |
240 | POWER_SUPPLY_PROP_MANUFACTURER, | |
241 | }; | |
242 | ||
243 | static int max14656_probe(struct i2c_client *client, | |
244 | const struct i2c_device_id *id) | |
245 | { | |
246 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
247 | struct device *dev = &client->dev; | |
248 | struct power_supply_config psy_cfg = {}; | |
249 | struct max14656_chip *chip; | |
250 | int irq = client->irq; | |
251 | int ret = 0; | |
252 | ||
253 | if (irq <= 0) { | |
254 | dev_err(dev, "invalid irq number: %d\n", irq); | |
255 | return -ENODEV; | |
256 | } | |
257 | ||
258 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | |
259 | dev_err(dev, "No support for SMBUS_BYTE_DATA\n"); | |
260 | return -ENODEV; | |
261 | } | |
262 | ||
263 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); | |
264 | if (!chip) | |
265 | return -ENOMEM; | |
266 | ||
267 | psy_cfg.drv_data = chip; | |
268 | chip->client = client; | |
269 | chip->online = 0; | |
270 | chip->psy_desc.name = MAX14656_NAME; | |
271 | chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN; | |
272 | chip->psy_desc.properties = max14656_battery_props; | |
273 | chip->psy_desc.num_properties = ARRAY_SIZE(max14656_battery_props); | |
274 | chip->psy_desc.get_property = max14656_get_property; | |
275 | chip->irq = irq; | |
276 | ||
277 | ret = max14656_hw_init(chip); | |
278 | if (ret) | |
279 | return -ENODEV; | |
280 | ||
281 | INIT_DELAYED_WORK(&chip->irq_work, max14656_irq_worker); | |
282 | ||
283 | ret = devm_request_irq(dev, chip->irq, max14656_irq, | |
284 | IRQF_TRIGGER_FALLING, | |
285 | MAX14656_NAME, chip); | |
286 | if (ret) { | |
287 | dev_err(dev, "request_irq %d failed\n", chip->irq); | |
288 | return -EINVAL; | |
289 | } | |
290 | enable_irq_wake(chip->irq); | |
291 | ||
292 | chip->detect_psy = devm_power_supply_register(dev, | |
293 | &chip->psy_desc, &psy_cfg); | |
294 | if (IS_ERR(chip->detect_psy)) { | |
295 | dev_err(dev, "power_supply_register failed\n"); | |
296 | return -EINVAL; | |
297 | } | |
298 | ||
299 | schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(2000)); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static const struct i2c_device_id max14656_id[] = { | |
305 | { "max14656", 0 }, | |
306 | {} | |
307 | }; | |
166e8dbd | 308 | MODULE_DEVICE_TABLE(i2c, max14656_id); |
9d60595a AK |
309 | |
310 | static const struct of_device_id max14656_match_table[] = { | |
311 | { .compatible = "maxim,max14656", }, | |
312 | {} | |
313 | }; | |
166e8dbd | 314 | MODULE_DEVICE_TABLE(of, max14656_match_table); |
9d60595a AK |
315 | |
316 | static struct i2c_driver max14656_i2c_driver = { | |
317 | .driver = { | |
318 | .name = "max14656", | |
9d60595a AK |
319 | .of_match_table = max14656_match_table, |
320 | }, | |
321 | .probe = max14656_probe, | |
322 | .id_table = max14656_id, | |
323 | }; | |
324 | module_i2c_driver(max14656_i2c_driver); | |
325 | ||
326 | MODULE_DESCRIPTION("MAX14656 USB charger detector"); | |
327 | MODULE_LICENSE("GPL v2"); |