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