1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2022 Richtek Technology Corp.
4 *
5 * Authors: Alina Yu <alina_yu@richtek.com>
6 * ChiYuan Huang <cy_huang@richtek.com>
7 */
8
9 #include <linux/bits.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kstrtox.h>
14 #include <linux/linear_range.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 #include <linux/power_supply.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/sysfs.h>
23
24 #define RT9471_REG_OTGCFG 0x00
25 #define RT9471_REG_TOP 0x01
26 #define RT9471_REG_FUNC 0x02
27 #define RT9471_REG_IBUS 0x03
28 #define RT9471_REG_VBUS 0x04
29 #define RT9471_REG_PRECHG 0x05
30 #define RT9471_REG_VCHG 0x07
31 #define RT9471_REG_ICHG 0x08
32 #define RT9471_REG_CHGTMR 0x09
33 #define RT9471_REG_EOC 0x0A
34 #define RT9471_REG_INFO 0x0B
35 #define RT9471_REG_JEITA 0x0C
36 #define RT9471_REG_PUMP_EXP 0x0D
37 #define RT9471_REG_DPDMDET 0x0E
38 #define RT9471_REG_ICSTAT 0x0F
39 #define RT9471_REG_STAT0 0x10
40 #define RT9471_REG_STAT1 0x11
41 #define RT9471_REG_STAT2 0x12
42 #define RT9471_REG_IRQ0 0x20
43 #define RT9471_REG_MASK0 0x30
44
45 #define RT9471_OTGCV_MASK GENMASK(7, 6)
46 #define RT9471_OTGCC_MASK BIT(0)
47 #define RT9471_OTGEN_MASK BIT(1)
48 #define RT9471_CHGFAULT_MASK GENMASK(4, 1)
49
50 #define RT9471_NUM_IRQ_REGS 4
51 #define RT9471_OTGCV_MINUV 4850000
52 #define RT9471_OTGCV_STEPUV 150000
53 #define RT9471_NUM_VOTG 4
54 #define RT9471_VCHG_MAXUV 4700000
55 #define RT9471_ICHG_MAXUA 3150000
56
57 /* Device ID */
58 #define RT9470_DEVID 0x09
59 #define RT9470D_DEVID 0x0A
60 #define RT9471_DEVID 0x0D
61 #define RT9471D_DEVID 0x0E
62
63 /* IRQ number */
64 #define RT9471_IRQ_BC12_DONE 0
65 #define RT9471_IRQ_DETACH 1
66 #define RT9471_IRQ_RECHG 2
67 #define RT9471_IRQ_CHG_DONE 3
68 #define RT9471_IRQ_BG_CHG 4
69 #define RT9471_IRQ_IE0C 5
70 #define RT9471_IRQ_CHG_RDY 6
71 #define RT9471_IRQ_VBUS_GD 7
72 #define RT9471_IRQ_CHG_BATOV 9
73 #define RT9471_IRQ_CHG_SYSOV 10
74 #define RT9471_IRQ_CHG_TOUT 11
75 #define RT9471_IRQ_CHG_BUSUV 12
76 #define RT9471_IRQ_CHG_THREG 13
77 #define RT9471_IRQ_CHG_AICR 14
78 #define RT9471_IRQ_CHG_MIVR 15
79 #define RT9471_IRQ_SYS_SHORT 16
80 #define RT9471_IRQ_SYS_MIN 17
81 #define RT9471_IRQ_AICC_DONE 18
82 #define RT9471_IRQ_PE_DONE 19
83 #define RT9471_IRQ_JEITA_COLD 20
84 #define RT9471_IRQ_JEITA_COOL 21
85 #define RT9471_IRQ_JEITA_WARM 22
86 #define RT9471_IRQ_JEITA_HOT 23
87 #define RT9471_IRQ_OTG_FAULT 24
88 #define RT9471_IRQ_OTG_LBP 25
89 #define RT9471_IRQ_OTG_CC 26
90 #define RT9471_IRQ_WDT 29
91 #define RT9471_IRQ_VAC_OV 30
92 #define RT9471_IRQ_OTP 31
93
94 enum rt9471_fields {
95 F_WDT = 0,
96 F_WDT_RST,
97 F_CHG_EN,
98 F_HZ,
99 F_BATFET_DIS,
100 F_AICR,
101 F_AICC_EN,
102 F_MIVR,
103 F_IPRE_CHG,
104 F_VPRE_CHG,
105 F_VBAT_REG,
106 F_ICHG_REG,
107 F_EOC_RST,
108 F_TE,
109 F_IEOC_CHG,
110 F_DEVICE_ID,
111 F_REG_RST,
112 F_BC12_EN,
113 F_IC_STAT,
114 F_PORT_STAT,
115 F_ST_CHG_DONE,
116 F_ST_CHG_RDY,
117 F_ST_VBUS_GD,
118 F_MAX_FIELDS
119 };
120
121 enum rt9471_ranges {
122 RT9471_RANGE_AICR = 0,
123 RT9471_RANGE_MIVR,
124 RT9471_RANGE_IPRE,
125 RT9471_RANGE_VCHG,
126 RT9471_RANGE_ICHG,
127 RT9471_RANGE_IEOC,
128 RT9471_MAX_RANGES
129 };
130
131 enum {
132 RT9471_PORTSTAT_APPLE_10W = 8,
133 RT9471_PORTSTAT_SAMSUNG_10W,
134 RT9471_PORTSTAT_APPLE_5W,
135 RT9471_PORTSTAT_APPLE_12W,
136 RT9471_PORTSTAT_NSTD,
137 RT9471_PORTSTAT_SDP,
138 RT9471_PORTSTAT_CDP,
139 RT9471_PORTSTAT_DCP,
140 };
141
142 struct rt9471_chip {
143 struct device *dev;
144 struct regmap *regmap;
145 struct regmap_field *rm_fields[F_MAX_FIELDS];
146 struct regmap_irq_chip_data *irq_chip_data;
147 struct regulator_dev *otg_rdev;
148 struct power_supply *psy;
149 struct power_supply_desc psy_desc;
150 struct mutex var_lock;
151 enum power_supply_usb_type psy_usb_type;
152 int psy_usb_curr;
153 };
154
155 static const struct reg_field rt9471_reg_fields[F_MAX_FIELDS] = {
156 [F_WDT] = REG_FIELD(RT9471_REG_TOP, 0, 0),
157 [F_WDT_RST] = REG_FIELD(RT9471_REG_TOP, 1, 1),
158 [F_CHG_EN] = REG_FIELD(RT9471_REG_FUNC, 0, 0),
159 [F_HZ] = REG_FIELD(RT9471_REG_FUNC, 5, 5),
160 [F_BATFET_DIS] = REG_FIELD(RT9471_REG_FUNC, 7, 7),
161 [F_AICR] = REG_FIELD(RT9471_REG_IBUS, 0, 5),
162 [F_AICC_EN] = REG_FIELD(RT9471_REG_IBUS, 7, 7),
163 [F_MIVR] = REG_FIELD(RT9471_REG_VBUS, 0, 3),
164 [F_IPRE_CHG] = REG_FIELD(RT9471_REG_PRECHG, 0, 3),
165 [F_VPRE_CHG] = REG_FIELD(RT9471_REG_PRECHG, 4, 6),
166 [F_VBAT_REG] = REG_FIELD(RT9471_REG_VCHG, 0, 6),
167 [F_ICHG_REG] = REG_FIELD(RT9471_REG_ICHG, 0, 5),
168 [F_EOC_RST] = REG_FIELD(RT9471_REG_EOC, 0, 0),
169 [F_TE] = REG_FIELD(RT9471_REG_EOC, 1, 1),
170 [F_IEOC_CHG] = REG_FIELD(RT9471_REG_EOC, 4, 7),
171 [F_DEVICE_ID] = REG_FIELD(RT9471_REG_INFO, 3, 6),
172 [F_REG_RST] = REG_FIELD(RT9471_REG_INFO, 7, 7),
173 [F_BC12_EN] = REG_FIELD(RT9471_REG_DPDMDET, 7, 7),
174 [F_IC_STAT] = REG_FIELD(RT9471_REG_ICSTAT, 0, 3),
175 [F_PORT_STAT] = REG_FIELD(RT9471_REG_ICSTAT, 4, 7),
176 [F_ST_CHG_DONE] = REG_FIELD(RT9471_REG_STAT0, 3, 3),
177 [F_ST_CHG_RDY] = REG_FIELD(RT9471_REG_STAT0, 6, 6),
178 [F_ST_VBUS_GD] = REG_FIELD(RT9471_REG_STAT0, 7, 7),
179 };
180
181 static const struct linear_range rt9471_chg_ranges[RT9471_MAX_RANGES] = {
182 [RT9471_RANGE_AICR] = { .min = 50000, .min_sel = 1, .max_sel = 63, .step = 50000 },
183 [RT9471_RANGE_MIVR] = { .min = 3900000, .min_sel = 0, .max_sel = 15, .step = 100000 },
184 [RT9471_RANGE_IPRE] = { .min = 50000, .min_sel = 0, .max_sel = 15, .step = 50000 },
185 [RT9471_RANGE_VCHG] = { .min = 3900000, .min_sel = 0, .max_sel = 80, .step = 10000 },
186 [RT9471_RANGE_ICHG] = { .min = 0, .min_sel = 0, .max_sel = 63, .step = 50000 },
187 [RT9471_RANGE_IEOC] = { .min = 50000, .min_sel = 0, .max_sel = 15, .step = 50000 },
188 };
189
rt9471_set_value_by_field_range(struct rt9471_chip * chip,enum rt9471_fields field,enum rt9471_ranges range,int val)190 static int rt9471_set_value_by_field_range(struct rt9471_chip *chip,
191 enum rt9471_fields field,
192 enum rt9471_ranges range, int val)
193 {
194 unsigned int sel;
195
196 if (val < 0)
197 return -EINVAL;
198
199 linear_range_get_selector_within(rt9471_chg_ranges + range, val, &sel);
200
201 return regmap_field_write(chip->rm_fields[field], sel);
202 }
203
204
rt9471_get_value_by_field_range(struct rt9471_chip * chip,enum rt9471_fields field,enum rt9471_ranges range,int * val)205 static int rt9471_get_value_by_field_range(struct rt9471_chip *chip,
206 enum rt9471_fields field,
207 enum rt9471_ranges range, int *val)
208 {
209 unsigned int sel, rvalue;
210 int ret;
211
212 ret = regmap_field_read(chip->rm_fields[field], &sel);
213 if (ret)
214 return ret;
215
216 ret = linear_range_get_value(rt9471_chg_ranges + range, sel, &rvalue);
217 if (ret)
218 return ret;
219
220 *val = rvalue;
221 return 0;
222 }
223
rt9471_set_ieoc(struct rt9471_chip * chip,int microamp)224 static int rt9471_set_ieoc(struct rt9471_chip *chip, int microamp)
225 {
226 int ret;
227
228 if (microamp == 0)
229 return regmap_field_write(chip->rm_fields[F_TE], 0);
230
231 ret = rt9471_set_value_by_field_range(chip, F_IEOC_CHG, RT9471_RANGE_IEOC, microamp);
232 if (ret)
233 return ret;
234
235 /* After applying the new IEOC value, enable charge termination */
236 return regmap_field_write(chip->rm_fields[F_TE], 1);
237 }
238
rt9471_get_ieoc(struct rt9471_chip * chip,int * microamp)239 static int rt9471_get_ieoc(struct rt9471_chip *chip, int *microamp)
240 {
241 unsigned int chg_term_enable;
242 int ret;
243
244 ret = regmap_field_read(chip->rm_fields[F_TE], &chg_term_enable);
245 if (ret)
246 return ret;
247
248 if (!chg_term_enable) {
249 *microamp = 0;
250 return 0;
251 }
252
253 return rt9471_get_value_by_field_range(chip, F_IEOC_CHG, RT9471_RANGE_IEOC, microamp);
254 }
255
rt9471_get_status(struct rt9471_chip * chip,int * status)256 static int rt9471_get_status(struct rt9471_chip *chip, int *status)
257 {
258 unsigned int chg_ready, chg_done, fault_stat;
259 int ret;
260
261 ret = regmap_field_read(chip->rm_fields[F_ST_CHG_RDY], &chg_ready);
262 if (ret)
263 return ret;
264
265 ret = regmap_field_read(chip->rm_fields[F_ST_CHG_DONE], &chg_done);
266 if (ret)
267 return ret;
268
269 ret = regmap_read(chip->regmap, RT9471_REG_STAT1, &fault_stat);
270 if (ret)
271 return ret;
272
273 fault_stat &= RT9471_CHGFAULT_MASK;
274
275 if (chg_ready && chg_done)
276 *status = POWER_SUPPLY_STATUS_FULL;
277 else if (chg_ready && fault_stat)
278 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
279 else if (chg_ready && !fault_stat)
280 *status = POWER_SUPPLY_STATUS_CHARGING;
281 else
282 *status = POWER_SUPPLY_STATUS_DISCHARGING;
283
284 return 0;
285 }
286
rt9471_get_vbus_good(struct rt9471_chip * chip,int * stat)287 static int rt9471_get_vbus_good(struct rt9471_chip *chip, int *stat)
288 {
289 unsigned int vbus_gd;
290 int ret;
291
292 ret = regmap_field_read(chip->rm_fields[F_ST_VBUS_GD], &vbus_gd);
293 if (ret)
294 return ret;
295
296 *stat = vbus_gd;
297 return 0;
298 }
299
rt9471_get_usb_type(struct rt9471_chip * chip,int * usb_type)300 static int rt9471_get_usb_type(struct rt9471_chip *chip, int *usb_type)
301 {
302 mutex_lock(&chip->var_lock);
303 *usb_type = chip->psy_usb_type;
304 mutex_unlock(&chip->var_lock);
305
306 return 0;
307 }
308
rt9471_get_usb_type_current(struct rt9471_chip * chip,int * microamp)309 static int rt9471_get_usb_type_current(struct rt9471_chip *chip,
310 int *microamp)
311 {
312 mutex_lock(&chip->var_lock);
313 *microamp = chip->psy_usb_curr;
314 mutex_unlock(&chip->var_lock);
315
316 return 0;
317 }
318
319 static enum power_supply_property rt9471_charger_properties[] = {
320 POWER_SUPPLY_PROP_STATUS,
321 POWER_SUPPLY_PROP_ONLINE,
322 POWER_SUPPLY_PROP_CURRENT_MAX,
323 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
324 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
325 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
326 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
327 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
328 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
329 POWER_SUPPLY_PROP_USB_TYPE,
330 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
331 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
332 POWER_SUPPLY_PROP_MODEL_NAME,
333 POWER_SUPPLY_PROP_MANUFACTURER,
334 };
335
rt9471_charger_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)336 static int rt9471_charger_property_is_writeable(struct power_supply *psy,
337 enum power_supply_property psp)
338 {
339 switch (psp) {
340 case POWER_SUPPLY_PROP_STATUS:
341 case POWER_SUPPLY_PROP_ONLINE:
342 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
343 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
344 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
345 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
346 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
347 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
348 return 1;
349 default:
350 return 0;
351 }
352 }
353
rt9471_charger_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)354 static int rt9471_charger_set_property(struct power_supply *psy,
355 enum power_supply_property psp,
356 const union power_supply_propval *val)
357 {
358 struct rt9471_chip *chip = power_supply_get_drvdata(psy);
359 int value = val->intval;
360
361 switch (psp) {
362 case POWER_SUPPLY_PROP_STATUS:
363 return regmap_field_write(chip->rm_fields[F_CHG_EN], !!value);
364 case POWER_SUPPLY_PROP_ONLINE:
365 return regmap_field_write(chip->rm_fields[F_HZ], !value);
366 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
367 return rt9471_set_value_by_field_range(chip, F_ICHG_REG, RT9471_RANGE_ICHG, value);
368 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
369 return rt9471_set_value_by_field_range(chip, F_VBAT_REG, RT9471_RANGE_VCHG, value);
370 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
371 return rt9471_set_value_by_field_range(chip, F_AICR, RT9471_RANGE_AICR, value);
372 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
373 return rt9471_set_value_by_field_range(chip, F_MIVR, RT9471_RANGE_MIVR, value);
374 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
375 return rt9471_set_value_by_field_range(chip, F_IPRE_CHG, RT9471_RANGE_IPRE, value);
376 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
377 return rt9471_set_ieoc(chip, val->intval);
378 default:
379 return -EINVAL;
380 }
381 }
382
383 static const char * const rt9471_manufacturer = "Richtek Technology Corp.";
384 static const char * const rt9471_model = "RT9471";
385
rt9471_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)386 static int rt9471_charger_get_property(struct power_supply *psy,
387 enum power_supply_property psp,
388 union power_supply_propval *val)
389 {
390 struct rt9471_chip *chip = power_supply_get_drvdata(psy);
391 int *pvalue = &val->intval;
392
393 switch (psp) {
394 case POWER_SUPPLY_PROP_STATUS:
395 return rt9471_get_status(chip, pvalue);
396 case POWER_SUPPLY_PROP_ONLINE:
397 return rt9471_get_vbus_good(chip, pvalue);
398 case POWER_SUPPLY_PROP_CURRENT_MAX:
399 return rt9471_get_usb_type_current(chip, pvalue);
400 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
401 return rt9471_get_value_by_field_range(chip, F_ICHG_REG, RT9471_RANGE_ICHG, pvalue);
402 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
403 *pvalue = RT9471_ICHG_MAXUA;
404 return 0;
405 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
406 return rt9471_get_value_by_field_range(chip, F_VBAT_REG, RT9471_RANGE_VCHG, pvalue);
407 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
408 val->intval = RT9471_VCHG_MAXUV;
409 return 0;
410 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
411 return rt9471_get_value_by_field_range(chip, F_AICR, RT9471_RANGE_AICR, pvalue);
412 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
413 return rt9471_get_value_by_field_range(chip, F_MIVR, RT9471_RANGE_MIVR, pvalue);
414 case POWER_SUPPLY_PROP_USB_TYPE:
415 return rt9471_get_usb_type(chip, pvalue);
416 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
417 return rt9471_get_value_by_field_range(chip, F_IPRE_CHG, RT9471_RANGE_IPRE, pvalue);
418 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
419 return rt9471_get_ieoc(chip, pvalue);
420 case POWER_SUPPLY_PROP_MODEL_NAME:
421 val->strval = rt9471_model;
422 return 0;
423 case POWER_SUPPLY_PROP_MANUFACTURER:
424 val->strval = rt9471_manufacturer;
425 return 0;
426 default:
427 return -ENODATA;
428 }
429 }
430
rt9471_vbus_gd_handler(int irqno,void * devid)431 static irqreturn_t rt9471_vbus_gd_handler(int irqno, void *devid)
432 {
433 struct rt9471_chip *chip = devid;
434
435 power_supply_changed(chip->psy);
436
437 return IRQ_HANDLED;
438 }
439
rt9471_detach_handler(int irqno,void * devid)440 static irqreturn_t rt9471_detach_handler(int irqno, void *devid)
441 {
442 struct rt9471_chip *chip = devid;
443 unsigned int vbus_gd;
444 int ret;
445
446 ret = regmap_field_read(chip->rm_fields[F_ST_VBUS_GD], &vbus_gd);
447 if (ret)
448 return IRQ_NONE;
449
450 /* Only focus on really detached */
451 if (vbus_gd)
452 return IRQ_HANDLED;
453
454 mutex_lock(&chip->var_lock);
455 chip->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
456 chip->psy_usb_curr = 0;
457 mutex_unlock(&chip->var_lock);
458
459 power_supply_changed(chip->psy);
460
461 return IRQ_HANDLED;
462 }
463
rt9471_bc12_done_handler(int irqno,void * devid)464 static irqreturn_t rt9471_bc12_done_handler(int irqno, void *devid)
465 {
466 struct rt9471_chip *chip = devid;
467 enum power_supply_usb_type usb_type;
468 unsigned int port_stat;
469 int usb_curr, ret;
470
471 ret = regmap_field_read(chip->rm_fields[F_PORT_STAT], &port_stat);
472 if (ret)
473 return IRQ_NONE;
474
475 switch (port_stat) {
476 case RT9471_PORTSTAT_APPLE_10W:
477 usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
478 usb_curr = 2000000;
479 break;
480 case RT9471_PORTSTAT_APPLE_5W:
481 usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
482 usb_curr = 1000000;
483 break;
484 case RT9471_PORTSTAT_APPLE_12W:
485 usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
486 usb_curr = 2400000;
487 break;
488 case RT9471_PORTSTAT_SAMSUNG_10W:
489 usb_type = POWER_SUPPLY_USB_TYPE_DCP;
490 usb_curr = 2000000;
491 break;
492 case RT9471_PORTSTAT_DCP:
493 usb_type = POWER_SUPPLY_USB_TYPE_DCP;
494 usb_curr = 1500000;
495 break;
496 case RT9471_PORTSTAT_NSTD:
497 case RT9471_PORTSTAT_SDP:
498 usb_type = POWER_SUPPLY_USB_TYPE_SDP;
499 usb_curr = 500000;
500 break;
501 case RT9471_PORTSTAT_CDP:
502 usb_type = POWER_SUPPLY_USB_TYPE_CDP;
503 usb_curr = 1500000;
504 break;
505 default:
506 usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
507 usb_curr = 0;
508 break;
509 }
510
511 mutex_lock(&chip->var_lock);
512 chip->psy_usb_type = usb_type;
513 chip->psy_usb_curr = usb_curr;
514 mutex_unlock(&chip->var_lock);
515
516 power_supply_changed(chip->psy);
517
518 return IRQ_HANDLED;
519 }
520
rt9471_wdt_handler(int irqno,void * devid)521 static irqreturn_t rt9471_wdt_handler(int irqno, void *devid)
522 {
523 struct rt9471_chip *chip = devid;
524 int ret;
525
526 ret = regmap_field_write(chip->rm_fields[F_WDT_RST], 1);
527
528 return ret ? IRQ_NONE : IRQ_HANDLED;
529 }
530
rt9471_otg_fault_handler(int irqno,void * devid)531 static irqreturn_t rt9471_otg_fault_handler(int irqno, void *devid)
532 {
533 struct rt9471_chip *chip = devid;
534
535 regulator_notifier_call_chain(chip->otg_rdev, REGULATOR_EVENT_FAIL, NULL);
536
537 return IRQ_HANDLED;
538 }
539
540 #define RT9471_IRQ_DESC(_name, _hwirq) \
541 { \
542 .name = #_name, \
543 .hwirq = _hwirq, \
544 .handler = rt9471_##_name##_handler, \
545 }
546
rt9471_register_interrupts(struct rt9471_chip * chip)547 static int rt9471_register_interrupts(struct rt9471_chip *chip)
548 {
549 struct device *dev = chip->dev;
550 static const struct {
551 char *name;
552 int hwirq;
553 irq_handler_t handler;
554 } chg_irqs[] = {
555 RT9471_IRQ_DESC(vbus_gd, RT9471_IRQ_VBUS_GD),
556 RT9471_IRQ_DESC(detach, RT9471_IRQ_DETACH),
557 RT9471_IRQ_DESC(bc12_done, RT9471_IRQ_BC12_DONE),
558 RT9471_IRQ_DESC(wdt, RT9471_IRQ_WDT),
559 RT9471_IRQ_DESC(otg_fault, RT9471_IRQ_OTG_FAULT),
560 }, *curr;
561 int i, virq, ret;
562
563 for (i = 0; i < ARRAY_SIZE(chg_irqs); i++) {
564 curr = chg_irqs + i;
565
566 virq = regmap_irq_get_virq(chip->irq_chip_data, curr->hwirq);
567 if (virq <= 0)
568 return virq;
569
570 ret = devm_request_threaded_irq(dev, virq, NULL, curr->handler,
571 IRQF_ONESHOT, curr->name, chip);
572 if (ret)
573 return dev_err_probe(dev, ret, "Failed to register IRQ (%s)\n",
574 curr->name);
575 }
576
577 return 0;
578 }
579
580 static const struct regulator_ops rt9471_otg_ops = {
581 .enable = regulator_enable_regmap,
582 .disable = regulator_disable_regmap,
583 .is_enabled = regulator_is_enabled_regmap,
584 .list_voltage = regulator_list_voltage_linear,
585 .get_voltage_sel = regulator_get_voltage_sel_regmap,
586 .set_voltage_sel = regulator_set_voltage_sel_regmap,
587 .set_current_limit = regulator_set_current_limit_regmap,
588 .get_current_limit = regulator_get_current_limit_regmap,
589 };
590
591 static const unsigned int rt9471_otg_microamp[] = { 500000, 1200000, };
592
593 static const struct regulator_desc rt9471_otg_rdesc = {
594 .of_match = of_match_ptr("usb-otg-vbus-regulator"),
595 .name = "rt9471-otg-vbus",
596 .owner = THIS_MODULE,
597 .type = REGULATOR_VOLTAGE,
598 .ops = &rt9471_otg_ops,
599 .min_uV = RT9471_OTGCV_MINUV,
600 .uV_step = RT9471_OTGCV_STEPUV,
601 .n_voltages = RT9471_NUM_VOTG,
602 .curr_table = rt9471_otg_microamp,
603 .n_current_limits = ARRAY_SIZE(rt9471_otg_microamp),
604 .enable_mask = RT9471_OTGEN_MASK,
605 .enable_reg = RT9471_REG_FUNC,
606 .vsel_reg = RT9471_REG_OTGCFG,
607 .vsel_mask = RT9471_OTGCV_MASK,
608 .csel_reg = RT9471_REG_OTGCFG,
609 .csel_mask = RT9471_OTGCC_MASK,
610 };
611
rt9471_register_otg_regulator(struct rt9471_chip * chip)612 static int rt9471_register_otg_regulator(struct rt9471_chip *chip)
613 {
614 struct device *dev = chip->dev;
615 struct regulator_config cfg = { .dev = dev, .driver_data = chip };
616
617 chip->otg_rdev = devm_regulator_register(dev, &rt9471_otg_rdesc, &cfg);
618
619 return PTR_ERR_OR_ZERO(chip->otg_rdev);
620 }
621
psy_device_to_chip(struct device * dev)622 static inline struct rt9471_chip *psy_device_to_chip(struct device *dev)
623 {
624 return power_supply_get_drvdata(to_power_supply(dev));
625 }
626
sysoff_enable_show(struct device * dev,struct device_attribute * attr,char * buf)627 static ssize_t sysoff_enable_show(struct device *dev,
628 struct device_attribute *attr, char *buf)
629 {
630 struct rt9471_chip *chip = psy_device_to_chip(dev);
631 unsigned int sysoff_enable;
632 int ret;
633
634 ret = regmap_field_read(chip->rm_fields[F_BATFET_DIS], &sysoff_enable);
635 if (ret)
636 return ret;
637
638 return sysfs_emit(buf, "%d\n", sysoff_enable);
639 }
640
sysoff_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)641 static ssize_t sysoff_enable_store(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf, size_t count)
644 {
645 struct rt9471_chip *chip = psy_device_to_chip(dev);
646 unsigned int tmp;
647 int ret;
648
649 ret = kstrtouint(buf, 10, &tmp);
650 if (ret)
651 return ret;
652
653 ret = regmap_field_write(chip->rm_fields[F_BATFET_DIS], !!tmp);
654 if (ret)
655 return ret;
656
657 return count;
658 }
659
port_detect_enable_show(struct device * dev,struct device_attribute * attr,char * buf)660 static ssize_t port_detect_enable_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
662 {
663 struct rt9471_chip *chip = psy_device_to_chip(dev);
664 unsigned int bc12_enable;
665 int ret;
666
667 ret = regmap_field_read(chip->rm_fields[F_BC12_EN], &bc12_enable);
668 if (ret)
669 return ret;
670
671 return sysfs_emit(buf, "%d\n", bc12_enable);
672 }
673
port_detect_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)674 static ssize_t port_detect_enable_store(struct device *dev,
675 struct device_attribute *attr,
676 const char *buf, size_t count)
677 {
678 struct rt9471_chip *chip = psy_device_to_chip(dev);
679 unsigned int tmp;
680 int ret;
681
682 ret = kstrtouint(buf, 10, &tmp);
683 if (ret)
684 return ret;
685
686 ret = regmap_field_write(chip->rm_fields[F_BC12_EN], !!tmp);
687 if (ret)
688 return ret;
689
690 return count;
691 }
692
693 static DEVICE_ATTR_RW(sysoff_enable);
694 static DEVICE_ATTR_RW(port_detect_enable);
695
696 static struct attribute *rt9471_sysfs_attrs[] = {
697 &dev_attr_sysoff_enable.attr,
698 &dev_attr_port_detect_enable.attr,
699 NULL
700 };
701
702 ATTRIBUTE_GROUPS(rt9471_sysfs);
703
rt9471_register_psy(struct rt9471_chip * chip)704 static int rt9471_register_psy(struct rt9471_chip *chip)
705 {
706 struct device *dev = chip->dev;
707 struct power_supply_desc *desc = &chip->psy_desc;
708 struct power_supply_config cfg = {};
709 char *psy_name;
710
711 cfg.drv_data = chip;
712 cfg.of_node = dev->of_node;
713 cfg.attr_grp = rt9471_sysfs_groups;
714
715 psy_name = devm_kasprintf(dev, GFP_KERNEL, "rt9471-%s", dev_name(dev));
716 if (!psy_name)
717 return -ENOMEM;
718
719 desc->name = psy_name;
720 desc->type = POWER_SUPPLY_TYPE_USB;
721 desc->usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) |
722 BIT(POWER_SUPPLY_USB_TYPE_CDP) |
723 BIT(POWER_SUPPLY_USB_TYPE_DCP) |
724 BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID) |
725 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN);
726 desc->properties = rt9471_charger_properties;
727 desc->num_properties = ARRAY_SIZE(rt9471_charger_properties);
728 desc->get_property = rt9471_charger_get_property;
729 desc->set_property = rt9471_charger_set_property;
730 desc->property_is_writeable = rt9471_charger_property_is_writeable;
731
732 chip->psy = devm_power_supply_register(dev, desc, &cfg);
733
734 return PTR_ERR_OR_ZERO(chip->psy);
735 }
736
737 static const struct regmap_irq rt9471_regmap_irqs[] = {
738 REGMAP_IRQ_REG_LINE(RT9471_IRQ_BC12_DONE, 8),
739 REGMAP_IRQ_REG_LINE(RT9471_IRQ_DETACH, 8),
740 REGMAP_IRQ_REG_LINE(RT9471_IRQ_RECHG, 8),
741 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_DONE, 8),
742 REGMAP_IRQ_REG_LINE(RT9471_IRQ_BG_CHG, 8),
743 REGMAP_IRQ_REG_LINE(RT9471_IRQ_IE0C, 8),
744 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_RDY, 8),
745 REGMAP_IRQ_REG_LINE(RT9471_IRQ_VBUS_GD, 8),
746 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_BATOV, 8),
747 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_SYSOV, 8),
748 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_TOUT, 8),
749 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_BUSUV, 8),
750 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_THREG, 8),
751 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_AICR, 8),
752 REGMAP_IRQ_REG_LINE(RT9471_IRQ_CHG_MIVR, 8),
753 REGMAP_IRQ_REG_LINE(RT9471_IRQ_SYS_SHORT, 8),
754 REGMAP_IRQ_REG_LINE(RT9471_IRQ_SYS_MIN, 8),
755 REGMAP_IRQ_REG_LINE(RT9471_IRQ_AICC_DONE, 8),
756 REGMAP_IRQ_REG_LINE(RT9471_IRQ_PE_DONE, 8),
757 REGMAP_IRQ_REG_LINE(RT9471_IRQ_JEITA_COLD, 8),
758 REGMAP_IRQ_REG_LINE(RT9471_IRQ_JEITA_COOL, 8),
759 REGMAP_IRQ_REG_LINE(RT9471_IRQ_JEITA_WARM, 8),
760 REGMAP_IRQ_REG_LINE(RT9471_IRQ_JEITA_HOT, 8),
761 REGMAP_IRQ_REG_LINE(RT9471_IRQ_OTG_FAULT, 8),
762 REGMAP_IRQ_REG_LINE(RT9471_IRQ_OTG_LBP, 8),
763 REGMAP_IRQ_REG_LINE(RT9471_IRQ_OTG_CC, 8),
764 REGMAP_IRQ_REG_LINE(RT9471_IRQ_WDT, 8),
765 REGMAP_IRQ_REG_LINE(RT9471_IRQ_VAC_OV, 8),
766 REGMAP_IRQ_REG_LINE(RT9471_IRQ_OTP, 8),
767 };
768
769 static const struct regmap_irq_chip rt9471_irq_chip = {
770 .name = "rt9471-irqs",
771 .status_base = RT9471_REG_IRQ0,
772 .mask_base = RT9471_REG_MASK0,
773 .num_regs = RT9471_NUM_IRQ_REGS,
774 .irqs = rt9471_regmap_irqs,
775 .num_irqs = ARRAY_SIZE(rt9471_regmap_irqs),
776 };
777
778 static const struct reg_sequence rt9471_init_regs[] = {
779 REG_SEQ0(RT9471_REG_INFO, 0x80), /* REG_RST */
780 REG_SEQ0(RT9471_REG_TOP, 0xC0), /* WDT = 0 */
781 REG_SEQ0(RT9471_REG_FUNC, 0x01), /* BATFET_DIS_DLY = 0 */
782 REG_SEQ0(RT9471_REG_IBUS, 0x0A), /* AUTO_AICR = 0 */
783 REG_SEQ0(RT9471_REG_VBUS, 0xC6), /* VAC_OVP = 14V */
784 REG_SEQ0(RT9471_REG_JEITA, 0x38), /* JEITA = 0 */
785 REG_SEQ0(RT9471_REG_DPDMDET, 0x31), /* BC12_EN = 0, DCP_DP_OPT = 1 */
786 };
787
rt9471_check_devinfo(struct rt9471_chip * chip)788 static int rt9471_check_devinfo(struct rt9471_chip *chip)
789 {
790 struct device *dev = chip->dev;
791 unsigned int dev_id;
792 int ret;
793
794 ret = regmap_field_read(chip->rm_fields[F_DEVICE_ID], &dev_id);
795 if (ret)
796 return dev_err_probe(dev, ret, "Failed to read device_id\n");
797
798 switch (dev_id) {
799 case RT9470_DEVID:
800 case RT9470D_DEVID:
801 case RT9471_DEVID:
802 case RT9471D_DEVID:
803 return 0;
804 default:
805 return dev_err_probe(dev, -ENODEV, "Incorrect device id\n");
806 }
807 }
808
rt9471_accessible_reg(struct device * dev,unsigned int reg)809 static bool rt9471_accessible_reg(struct device *dev, unsigned int reg)
810 {
811 switch (reg) {
812 case 0x00 ... 0x0F:
813 case 0x10 ... 0x13:
814 case 0x20 ... 0x33:
815 case 0x40 ... 0xA1:
816 return true;
817 default:
818 return false;
819 }
820 }
821
822 static const struct regmap_config rt9471_regmap_config = {
823 .reg_bits = 8,
824 .val_bits = 8,
825 .max_register = 0xA1,
826 .writeable_reg = rt9471_accessible_reg,
827 .readable_reg = rt9471_accessible_reg,
828 };
829
rt9471_probe(struct i2c_client * i2c)830 static int rt9471_probe(struct i2c_client *i2c)
831 {
832 struct device *dev = &i2c->dev;
833 struct rt9471_chip *chip;
834 struct gpio_desc *ce_gpio;
835 struct regmap *regmap;
836 int ret;
837
838 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
839 if (!chip)
840 return -ENOMEM;
841
842 chip->dev = dev;
843 mutex_init(&chip->var_lock);
844 i2c_set_clientdata(i2c, chip);
845
846 /* Default pull charge enable gpio to make 'CHG_EN' by SW control only */
847 ce_gpio = devm_gpiod_get_optional(dev, "charge-enable", GPIOD_OUT_HIGH);
848 if (IS_ERR(ce_gpio))
849 return dev_err_probe(dev, PTR_ERR(ce_gpio),
850 "Failed to config charge enable gpio\n");
851
852 regmap = devm_regmap_init_i2c(i2c, &rt9471_regmap_config);
853 if (IS_ERR(regmap))
854 return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init regmap\n");
855
856 chip->regmap = regmap;
857
858 ret = devm_regmap_field_bulk_alloc(dev, regmap, chip->rm_fields,
859 rt9471_reg_fields,
860 ARRAY_SIZE(rt9471_reg_fields));
861 if (ret)
862 return dev_err_probe(dev, ret, "Failed to alloc regmap field\n");
863
864 ret = rt9471_check_devinfo(chip);
865 if (ret)
866 return ret;
867
868 ret = regmap_register_patch(regmap, rt9471_init_regs,
869 ARRAY_SIZE(rt9471_init_regs));
870 if (ret)
871 return dev_err_probe(dev, ret, "Failed to init registers\n");
872
873 ret = devm_regmap_add_irq_chip(dev, regmap, i2c->irq,
874 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
875 &rt9471_irq_chip, &chip->irq_chip_data);
876 if (ret)
877 return dev_err_probe(dev, ret, "Failed to add IRQ chip\n");
878
879 ret = rt9471_register_psy(chip);
880 if (ret)
881 return dev_err_probe(dev, ret, "Failed to register psy\n");
882
883 ret = rt9471_register_otg_regulator(chip);
884 if (ret)
885 return dev_err_probe(dev, ret, "Failed to register otg\n");
886
887 ret = rt9471_register_interrupts(chip);
888 if (ret)
889 return ret;
890
891 /* After IRQs are all initialized, enable port detection by default */
892 return regmap_field_write(chip->rm_fields[F_BC12_EN], 1);
893 }
894
rt9471_shutdown(struct i2c_client * i2c)895 static void rt9471_shutdown(struct i2c_client *i2c)
896 {
897 struct rt9471_chip *chip = i2c_get_clientdata(i2c);
898
899 /*
900 * There's no external reset pin. Do register reset to guarantee charger
901 * function is normal after shutdown
902 */
903 regmap_field_write(chip->rm_fields[F_REG_RST], 1);
904 }
905
906 static const struct of_device_id rt9471_of_device_id[] = {
907 { .compatible = "richtek,rt9471" },
908 {}
909 };
910 MODULE_DEVICE_TABLE(of, rt9471_of_device_id);
911
912 static struct i2c_driver rt9471_driver = {
913 .driver = {
914 .name = "rt9471",
915 .of_match_table = rt9471_of_device_id,
916 },
917 .probe = rt9471_probe,
918 .shutdown = rt9471_shutdown,
919 };
920 module_i2c_driver(rt9471_driver);
921
922 MODULE_DESCRIPTION("Richtek RT9471 charger driver");
923 MODULE_AUTHOR("Alina Yu <alina_yu@richtek.com>");
924 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
925 MODULE_LICENSE("GPL");
926