1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Richtek Technology Corp.
5 * Author: ChiYuan Huang <cy_huang@richtek.com>
6 * ChiaEn Wu <chiaen_wu@richtek.com>
9 #include <linux/bits.h>
10 #include <linux/bitfield.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/kstrtox.h>
18 #include <linux/linear_range.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/mutex.h>
23 #include <linux/power_supply.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/units.h>
27 #include <linux/sysfs.h>
29 #define RT9467_REG_CORE_CTRL0 0x00
30 #define RT9467_REG_CHG_CTRL1 0x01
31 #define RT9467_REG_CHG_CTRL2 0x02
32 #define RT9467_REG_CHG_CTRL3 0x03
33 #define RT9467_REG_CHG_CTRL4 0x04
34 #define RT9467_REG_CHG_CTRL5 0x05
35 #define RT9467_REG_CHG_CTRL6 0x06
36 #define RT9467_REG_CHG_CTRL7 0x07
37 #define RT9467_REG_CHG_CTRL8 0x08
38 #define RT9467_REG_CHG_CTRL9 0x09
39 #define RT9467_REG_CHG_CTRL10 0x0A
40 #define RT9467_REG_CHG_CTRL12 0x0C
41 #define RT9467_REG_CHG_CTRL13 0x0D
42 #define RT9467_REG_CHG_CTRL14 0x0E
43 #define RT9467_REG_CHG_ADC 0x11
44 #define RT9467_REG_CHG_DPDM1 0x12
45 #define RT9467_REG_CHG_DPDM2 0x13
46 #define RT9467_REG_DEVICE_ID 0x40
47 #define RT9467_REG_CHG_STAT 0x42
48 #define RT9467_REG_ADC_DATA_H 0x44
49 #define RT9467_REG_CHG_STATC 0x50
50 #define RT9467_REG_CHG_IRQ1 0x53
51 #define RT9467_REG_CHG_STATC_CTRL 0x60
52 #define RT9467_REG_CHG_IRQ1_CTRL 0x63
54 #define RT9467_MASK_PWR_RDY BIT(7)
55 #define RT9467_MASK_MIVR_STAT BIT(6)
56 #define RT9467_MASK_OTG_CSEL GENMASK(2, 0)
57 #define RT9467_MASK_OTG_VSEL GENMASK(7, 2)
58 #define RT9467_MASK_OTG_EN BIT(0)
59 #define RT9467_MASK_ADC_IN_SEL GENMASK(7, 4)
60 #define RT9467_MASK_ADC_START BIT(0)
62 #define RT9467_NUM_IRQ_REGS 4
63 #define RT9467_ICHG_MIN_uA 100000
64 #define RT9467_ICHG_MAX_uA 5000000
65 #define RT9467_CV_MAX_uV 4710000
66 #define RT9467_OTG_MIN_uV 4425000
67 #define RT9467_OTG_MAX_uV 5825000
68 #define RT9467_OTG_STEP_uV 25000
69 #define RT9467_NUM_VOTG (RT9467_OTG_MAX_uV - RT9467_OTG_MIN_uV + 1)
70 #define RT9467_AICLVTH_GAP_uV 200000
71 #define RT9467_ADCCONV_TIME_MS 35
73 #define RT9466_VID 0x8
74 #define RT9467_VID 0x9
77 #define RT9467_IRQ_TS_STATC 0
78 #define RT9467_IRQ_CHG_FAULT 1
79 #define RT9467_IRQ_CHG_STATC 2
80 #define RT9467_IRQ_CHG_TMR 3
81 #define RT9467_IRQ_CHG_BATABS 4
82 #define RT9467_IRQ_CHG_ADPBAD 5
83 #define RT9467_IRQ_CHG_RVP 6
84 #define RT9467_IRQ_OTP 7
86 #define RT9467_IRQ_CHG_AICLM 8
87 #define RT9467_IRQ_CHG_ICHGM 9
88 #define RT9467_IRQ_WDTMR 11
89 #define RT9467_IRQ_SSFINISH 12
90 #define RT9467_IRQ_CHG_RECHG 13
91 #define RT9467_IRQ_CHG_TERM 14
92 #define RT9467_IRQ_CHG_IEOC 15
94 #define RT9467_IRQ_ADC_DONE 16
95 #define RT9467_IRQ_PUMPX_DONE 17
96 #define RT9467_IRQ_BST_BATUV 21
97 #define RT9467_IRQ_BST_MIDOV 22
98 #define RT9467_IRQ_BST_OLP 23
100 #define RT9467_IRQ_ATTACH 24
101 #define RT9467_IRQ_DETACH 25
102 #define RT9467_IRQ_HVDCP_DET 29
103 #define RT9467_IRQ_CHGDET 30
104 #define RT9467_IRQ_DCDT 31
107 /* RT9467_REG_CORE_CTRL0 */
109 /* RT9467_REG_CHG_CTRL1 */
110 F_HZ, F_OTG_PIN_EN, F_OPA_MODE,
111 /* RT9467_REG_CHG_CTRL2 */
112 F_SHIP_MODE, F_TE, F_IINLMTSEL, F_CFO_EN, F_CHG_EN,
113 /* RT9467_REG_CHG_CTRL3 */
115 /* RT9467_REG_CHG_CTRL4 */
117 /* RT9467_REG_CHG_CTRL6 */
119 /* RT9467_REG_CHG_CTRL7 */
121 /* RT9467_REG_CHG_CTRL8 */
123 /* RT9467_REG_CHG_CTRL9 */
125 /* RT9467_REG_CHG_CTRL12 */
127 /* RT9467_REG_CHG_CTRL13 */
129 /* RT9467_REG_CHG_CTRL14 */
130 F_AICL_MEAS, F_AICL_VTH,
131 /* RT9467_REG_CHG_DPDM1 */
133 /* RT9467_REG_CHG_DPDM2 */
135 /* RT9467_REG_DEVICE_ID */
137 /* RT9467_REG_CHG_STAT */
139 /* RT9467_REG_CHG_STATC */
140 F_PWR_RDY, F_CHG_MIVR,
144 static const struct regmap_irq rt9467_irqs[] = {
145 REGMAP_IRQ_REG_LINE(RT9467_IRQ_TS_STATC, 8),
146 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_FAULT, 8),
147 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_STATC, 8),
148 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_TMR, 8),
149 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_BATABS, 8),
150 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_ADPBAD, 8),
151 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_RVP, 8),
152 REGMAP_IRQ_REG_LINE(RT9467_IRQ_OTP, 8),
153 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_AICLM, 8),
154 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_ICHGM, 8),
155 REGMAP_IRQ_REG_LINE(RT9467_IRQ_WDTMR, 8),
156 REGMAP_IRQ_REG_LINE(RT9467_IRQ_SSFINISH, 8),
157 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_RECHG, 8),
158 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_TERM, 8),
159 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHG_IEOC, 8),
160 REGMAP_IRQ_REG_LINE(RT9467_IRQ_ADC_DONE, 8),
161 REGMAP_IRQ_REG_LINE(RT9467_IRQ_PUMPX_DONE, 8),
162 REGMAP_IRQ_REG_LINE(RT9467_IRQ_BST_BATUV, 8),
163 REGMAP_IRQ_REG_LINE(RT9467_IRQ_BST_MIDOV, 8),
164 REGMAP_IRQ_REG_LINE(RT9467_IRQ_BST_OLP, 8),
165 REGMAP_IRQ_REG_LINE(RT9467_IRQ_ATTACH, 8),
166 REGMAP_IRQ_REG_LINE(RT9467_IRQ_DETACH, 8),
167 REGMAP_IRQ_REG_LINE(RT9467_IRQ_HVDCP_DET, 8),
168 REGMAP_IRQ_REG_LINE(RT9467_IRQ_CHGDET, 8),
169 REGMAP_IRQ_REG_LINE(RT9467_IRQ_DCDT, 8)
172 static const struct regmap_irq_chip rt9467_irq_chip = {
173 .name = "rt9467-irqs",
174 .status_base = RT9467_REG_CHG_IRQ1,
175 .mask_base = RT9467_REG_CHG_IRQ1_CTRL,
176 .num_regs = RT9467_NUM_IRQ_REGS,
178 .num_irqs = ARRAY_SIZE(rt9467_irqs),
182 RT9467_RANGE_IAICR = 0,
188 RT9467_RANGE_AICL_VTH,
192 static const struct linear_range rt9467_ranges[RT9467_RANGES_MAX] = {
193 LINEAR_RANGE_IDX(RT9467_RANGE_IAICR, 100000, 0x0, 0x3F, 50000),
194 LINEAR_RANGE_IDX(RT9467_RANGE_VOREG, 3900000, 0x0, 0x51, 10000),
195 LINEAR_RANGE_IDX(RT9467_RANGE_VMIVR, 3900000, 0x0, 0x5F, 100000),
196 LINEAR_RANGE_IDX(RT9467_RANGE_ICHG, 900000, 0x08, 0x31, 100000),
197 LINEAR_RANGE_IDX(RT9467_RANGE_IPREC, 100000, 0x0, 0x0F, 50000),
198 LINEAR_RANGE_IDX(RT9467_RANGE_IEOC, 100000, 0x0, 0x0F, 50000),
199 LINEAR_RANGE_IDX(RT9467_RANGE_AICL_VTH, 4100000, 0x0, 0x7, 100000),
202 static const struct reg_field rt9467_chg_fields[] = {
203 [F_RST] = REG_FIELD(RT9467_REG_CORE_CTRL0, 7, 7),
204 [F_HZ] = REG_FIELD(RT9467_REG_CHG_CTRL1, 2, 2),
205 [F_OTG_PIN_EN] = REG_FIELD(RT9467_REG_CHG_CTRL1, 1, 1),
206 [F_OPA_MODE] = REG_FIELD(RT9467_REG_CHG_CTRL1, 0, 0),
207 [F_SHIP_MODE] = REG_FIELD(RT9467_REG_CHG_CTRL2, 7, 7),
208 [F_TE] = REG_FIELD(RT9467_REG_CHG_CTRL2, 4, 4),
209 [F_IINLMTSEL] = REG_FIELD(RT9467_REG_CHG_CTRL2, 2, 3),
210 [F_CFO_EN] = REG_FIELD(RT9467_REG_CHG_CTRL2, 1, 1),
211 [F_CHG_EN] = REG_FIELD(RT9467_REG_CHG_CTRL2, 0, 0),
212 [F_IAICR] = REG_FIELD(RT9467_REG_CHG_CTRL3, 2, 7),
213 [F_ILIM_EN] = REG_FIELD(RT9467_REG_CHG_CTRL3, 0, 0),
214 [F_VOREG] = REG_FIELD(RT9467_REG_CHG_CTRL4, 1, 7),
215 [F_VMIVR] = REG_FIELD(RT9467_REG_CHG_CTRL6, 1, 7),
216 [F_ICHG] = REG_FIELD(RT9467_REG_CHG_CTRL7, 2, 7),
217 [F_IPREC] = REG_FIELD(RT9467_REG_CHG_CTRL8, 0, 3),
218 [F_IEOC] = REG_FIELD(RT9467_REG_CHG_CTRL9, 4, 7),
219 [F_WT_FC] = REG_FIELD(RT9467_REG_CHG_CTRL12, 5, 7),
220 [F_OCP] = REG_FIELD(RT9467_REG_CHG_CTRL13, 2, 2),
221 [F_AICL_MEAS] = REG_FIELD(RT9467_REG_CHG_CTRL14, 7, 7),
222 [F_AICL_VTH] = REG_FIELD(RT9467_REG_CHG_CTRL14, 0, 2),
223 [F_USBCHGEN] = REG_FIELD(RT9467_REG_CHG_DPDM1, 7, 7),
224 [F_USB_STATUS] = REG_FIELD(RT9467_REG_CHG_DPDM2, 0, 2),
225 [F_VENDOR] = REG_FIELD(RT9467_REG_DEVICE_ID, 4, 7),
226 [F_CHG_STAT] = REG_FIELD(RT9467_REG_CHG_STAT, 6, 7),
227 [F_PWR_RDY] = REG_FIELD(RT9467_REG_CHG_STATC, 7, 7),
228 [F_CHG_MIVR] = REG_FIELD(RT9467_REG_CHG_STATC, 6, 6),
232 RT9467_STAT_READY = 0,
233 RT9467_STAT_PROGRESS,
234 RT9467_STAT_CHARGE_DONE,
238 enum rt9467_adc_chan {
239 RT9467_ADC_VBUS_DIV5 = 0,
240 RT9467_ADC_VBUS_DIV2,
250 enum rt9467_chg_type {
251 RT9467_CHG_TYPE_NOVBUS = 0,
252 RT9467_CHG_TYPE_UNDER_GOING,
254 RT9467_CHG_TYPE_SDPNSTD,
260 enum rt9467_iin_limit_sel {
261 RT9467_IINLMTSEL_3_2A = 0,
262 RT9467_IINLMTSEL_CHG_TYP,
263 RT9467_IINLMTSEL_AICR,
264 RT9467_IINLMTSEL_LOWER_LEVEL, /* lower of above three */
267 struct rt9467_chg_data {
269 struct regmap *regmap;
270 struct regmap_field *rm_field[F_MAX_FIELDS];
271 struct regmap_irq_chip_data *irq_chip_data;
272 struct power_supply *psy;
273 struct mutex adc_lock;
274 struct mutex attach_lock;
275 struct mutex ichg_ieoc_lock;
276 struct regulator_dev *rdev;
277 struct completion aicl_done;
278 enum power_supply_usb_type psy_usb_type;
279 unsigned int old_stat;
285 static int rt9467_otg_of_parse_cb(struct device_node *of,
286 const struct regulator_desc *desc,
287 struct regulator_config *cfg)
289 struct rt9467_chg_data *data = cfg->driver_data;
291 cfg->ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(of),
292 "enable", 0, GPIOD_OUT_LOW |
293 GPIOD_FLAGS_BIT_NONEXCLUSIVE,
295 if (IS_ERR(cfg->ena_gpiod)) {
296 cfg->ena_gpiod = NULL;
300 return regmap_field_write(data->rm_field[F_OTG_PIN_EN], 1);
303 static const struct regulator_ops rt9467_otg_regulator_ops = {
304 .enable = regulator_enable_regmap,
305 .disable = regulator_disable_regmap,
306 .is_enabled = regulator_is_enabled_regmap,
307 .list_voltage = regulator_list_voltage_linear,
308 .set_voltage_sel = regulator_set_voltage_sel_regmap,
309 .get_voltage_sel = regulator_get_voltage_sel_regmap,
310 .set_current_limit = regulator_set_current_limit_regmap,
311 .get_current_limit = regulator_get_current_limit_regmap,
314 static const u32 rt9467_otg_microamp[] = {
315 500000, 700000, 1100000, 1300000, 1800000, 2100000, 2400000, 3000000
318 static const struct regulator_desc rt9467_otg_desc = {
319 .name = "rt9476-usb-otg-vbus",
320 .of_match = "usb-otg-vbus-regulator",
321 .of_parse_cb = rt9467_otg_of_parse_cb,
322 .type = REGULATOR_VOLTAGE,
323 .owner = THIS_MODULE,
324 .min_uV = RT9467_OTG_MIN_uV,
325 .uV_step = RT9467_OTG_STEP_uV,
326 .n_voltages = RT9467_NUM_VOTG,
327 .curr_table = rt9467_otg_microamp,
328 .n_current_limits = ARRAY_SIZE(rt9467_otg_microamp),
329 .csel_reg = RT9467_REG_CHG_CTRL10,
330 .csel_mask = RT9467_MASK_OTG_CSEL,
331 .vsel_reg = RT9467_REG_CHG_CTRL5,
332 .vsel_mask = RT9467_MASK_OTG_VSEL,
333 .enable_reg = RT9467_REG_CHG_CTRL1,
334 .enable_mask = RT9467_MASK_OTG_EN,
335 .ops = &rt9467_otg_regulator_ops,
338 static int rt9467_register_otg_regulator(struct rt9467_chg_data *data)
340 struct regulator_config cfg = {
342 .regmap = data->regmap,
346 data->rdev = devm_regulator_register(data->dev, &rt9467_otg_desc, &cfg);
347 return PTR_ERR_OR_ZERO(data->rdev);
350 static int rt9467_get_value_from_ranges(struct rt9467_chg_data *data,
351 enum rt9467_fields field,
352 enum rt9467_ranges rsel,
355 const struct linear_range *range = rt9467_ranges + rsel;
359 ret = regmap_field_read(data->rm_field[field], &sel);
363 return linear_range_get_value(range, sel, value);
366 static int rt9467_set_value_from_ranges(struct rt9467_chg_data *data,
367 enum rt9467_fields field,
368 enum rt9467_ranges rsel,
371 const struct linear_range *range = rt9467_ranges + rsel;
376 if (rsel == RT9467_RANGE_VMIVR) {
377 ret = linear_range_get_selector_high(range, value, &sel, &found);
379 value = range->max_sel;
381 linear_range_get_selector_within(range, value, &sel);
384 return regmap_field_write(data->rm_field[field], sel);
387 static int rt9467_get_adc_sel(enum rt9467_adc_chan chan, int *sel)
390 case RT9467_ADC_VBUS_DIV5:
391 case RT9467_ADC_VBUS_DIV2:
392 case RT9467_ADC_VSYS:
393 case RT9467_ADC_VBAT:
396 case RT9467_ADC_TS_BAT:
399 case RT9467_ADC_IBUS:
400 case RT9467_ADC_IBAT:
403 case RT9467_ADC_REGN:
404 case RT9467_ADC_TEMP_JC:
412 static int rt9467_get_adc_raw_data(struct rt9467_chg_data *data,
413 enum rt9467_adc_chan chan, int *val)
415 unsigned int adc_stat, reg_val, adc_sel;
416 __be16 chan_raw_data;
419 mutex_lock(&data->adc_lock);
421 ret = rt9467_get_adc_sel(chan, &adc_sel);
425 ret = regmap_write(data->regmap, RT9467_REG_CHG_ADC, 0);
427 dev_err(data->dev, "Failed to clear ADC enable\n");
431 reg_val = RT9467_MASK_ADC_START | FIELD_PREP(RT9467_MASK_ADC_IN_SEL, adc_sel);
432 ret = regmap_write(data->regmap, RT9467_REG_CHG_ADC, reg_val);
436 /* Minimum wait time for one channel processing */
437 msleep(RT9467_ADCCONV_TIME_MS);
439 ret = regmap_read_poll_timeout(data->regmap, RT9467_REG_CHG_ADC,
441 !(adc_stat & RT9467_MASK_ADC_START),
442 MILLI, RT9467_ADCCONV_TIME_MS * MILLI);
444 dev_err(data->dev, "Failed to wait ADC conversion, chan = %d\n", chan);
448 ret = regmap_raw_read(data->regmap, RT9467_REG_ADC_DATA_H,
449 &chan_raw_data, sizeof(chan_raw_data));
453 *val = be16_to_cpu(chan_raw_data);
456 mutex_unlock(&data->adc_lock);
460 static int rt9467_get_adc(struct rt9467_chg_data *data,
461 enum rt9467_adc_chan chan, int *val)
463 unsigned int aicr_ua, ichg_ua;
466 ret = rt9467_get_adc_raw_data(data, chan, val);
471 case RT9467_ADC_VBUS_DIV5:
474 case RT9467_ADC_VBUS_DIV2:
477 case RT9467_ADC_VBAT:
478 case RT9467_ADC_VSYS:
479 case RT9467_ADC_REGN:
482 case RT9467_ADC_TS_BAT:
485 case RT9467_ADC_IBUS:
486 /* UUG MOS turn-on ratio will affect the IBUS adc scale */
487 ret = rt9467_get_value_from_ranges(data, F_IAICR,
488 RT9467_RANGE_IAICR, &aicr_ua);
492 *val *= aicr_ua < 400000 ? 29480 : 50000;
494 case RT9467_ADC_IBAT:
495 /* PP MOS turn-on ratio will affect the ICHG adc scale */
496 ret = rt9467_get_value_from_ranges(data, F_ICHG,
497 RT9467_RANGE_ICHG, &ichg_ua);
501 *val *= ichg_ua <= 400000 ? 28500 :
502 ichg_ua <= 800000 ? 31500 : 500000;
504 case RT9467_ADC_TEMP_JC:
505 *val = ((*val * 2) - 40) * 10;
512 static int rt9467_psy_get_status(struct rt9467_chg_data *data, int *state)
517 ret = regmap_field_read(data->rm_field[F_CHG_STAT], &status);
522 case RT9467_STAT_READY:
523 *state = POWER_SUPPLY_STATUS_NOT_CHARGING;
525 case RT9467_STAT_PROGRESS:
526 *state = POWER_SUPPLY_STATUS_CHARGING;
528 case RT9467_STAT_CHARGE_DONE:
529 *state = POWER_SUPPLY_STATUS_FULL;
532 *state = POWER_SUPPLY_STATUS_UNKNOWN;
537 static int rt9467_psy_set_ichg(struct rt9467_chg_data *data, int microamp)
541 mutex_lock(&data->ichg_ieoc_lock);
543 if (microamp < 500000) {
544 dev_err(data->dev, "Minimum value must be 500mA\n");
548 ret = rt9467_set_value_from_ranges(data, F_ICHG, RT9467_RANGE_ICHG, microamp);
552 ret = rt9467_get_value_from_ranges(data, F_ICHG, RT9467_RANGE_ICHG,
558 mutex_unlock(&data->ichg_ieoc_lock);
562 static int rt9467_run_aicl(struct rt9467_chg_data *data)
564 unsigned int statc, aicl_vth;
565 int mivr_vth, aicr_get;
569 ret = regmap_read(data->regmap, RT9467_REG_CHG_STATC, &statc);
571 dev_err(data->dev, "Failed to read status\n");
575 if (!(statc & RT9467_MASK_PWR_RDY) || !(statc & RT9467_MASK_MIVR_STAT)) {
576 dev_info(data->dev, "Condition not matched %d\n", statc);
580 ret = rt9467_get_value_from_ranges(data, F_VMIVR, RT9467_RANGE_VMIVR,
583 dev_err(data->dev, "Failed to get mivr\n");
587 /* AICL_VTH = MIVR_VTH + 200mV */
588 aicl_vth = mivr_vth + RT9467_AICLVTH_GAP_uV;
589 ret = rt9467_set_value_from_ranges(data, F_AICL_VTH,
590 RT9467_RANGE_AICL_VTH, aicl_vth);
592 /* Trigger AICL function */
593 ret = regmap_field_write(data->rm_field[F_AICL_MEAS], 1);
595 dev_err(data->dev, "Failed to set aicl measurement\n");
599 reinit_completion(&data->aicl_done);
600 ret = wait_for_completion_timeout(&data->aicl_done, msecs_to_jiffies(3500));
604 ret = rt9467_get_value_from_ranges(data, F_IAICR, RT9467_RANGE_IAICR, &aicr_get);
606 dev_err(data->dev, "Failed to get aicr\n");
610 dev_info(data->dev, "aicr get = %d uA\n", aicr_get);
614 static int rt9467_psy_set_ieoc(struct rt9467_chg_data *data, int microamp)
618 mutex_lock(&data->ichg_ieoc_lock);
620 ret = rt9467_set_value_from_ranges(data, F_IEOC, RT9467_RANGE_IEOC, microamp);
624 ret = rt9467_get_value_from_ranges(data, F_IEOC, RT9467_RANGE_IEOC, &data->ieoc_ua);
629 mutex_unlock(&data->ichg_ieoc_lock);
633 static const enum power_supply_usb_type rt9467_chg_usb_types[] = {
634 POWER_SUPPLY_USB_TYPE_UNKNOWN,
635 POWER_SUPPLY_USB_TYPE_SDP,
636 POWER_SUPPLY_USB_TYPE_DCP,
637 POWER_SUPPLY_USB_TYPE_CDP,
640 static const enum power_supply_property rt9467_chg_properties[] = {
641 POWER_SUPPLY_PROP_STATUS,
642 POWER_SUPPLY_PROP_ONLINE,
643 POWER_SUPPLY_PROP_CURRENT_MAX,
644 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
645 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
646 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
647 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
648 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
649 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
650 POWER_SUPPLY_PROP_USB_TYPE,
651 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
652 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
655 static int rt9467_psy_get_property(struct power_supply *psy,
656 enum power_supply_property psp,
657 union power_supply_propval *val)
659 struct rt9467_chg_data *data = power_supply_get_drvdata(psy);
662 case POWER_SUPPLY_PROP_STATUS:
663 return rt9467_psy_get_status(data, &val->intval);
664 case POWER_SUPPLY_PROP_ONLINE:
665 return regmap_field_read(data->rm_field[F_PWR_RDY], &val->intval);
666 case POWER_SUPPLY_PROP_CURRENT_MAX:
667 mutex_lock(&data->attach_lock);
668 if (data->psy_usb_type == POWER_SUPPLY_USB_TYPE_UNKNOWN ||
669 data->psy_usb_type == POWER_SUPPLY_USB_TYPE_SDP)
670 val->intval = 500000;
672 val->intval = 1500000;
673 mutex_unlock(&data->attach_lock);
675 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
676 mutex_lock(&data->ichg_ieoc_lock);
677 val->intval = data->ichg_ua;
678 mutex_unlock(&data->ichg_ieoc_lock);
680 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
681 val->intval = RT9467_ICHG_MAX_uA;
683 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
684 return rt9467_get_value_from_ranges(data, F_VOREG,
687 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
688 val->intval = RT9467_CV_MAX_uV;
690 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
691 return rt9467_get_value_from_ranges(data, F_IAICR,
694 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
695 return rt9467_get_value_from_ranges(data, F_VMIVR,
698 case POWER_SUPPLY_PROP_USB_TYPE:
699 mutex_lock(&data->attach_lock);
700 val->intval = data->psy_usb_type;
701 mutex_unlock(&data->attach_lock);
703 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
704 return rt9467_get_value_from_ranges(data, F_IPREC,
707 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
708 mutex_lock(&data->ichg_ieoc_lock);
709 val->intval = data->ieoc_ua;
710 mutex_unlock(&data->ichg_ieoc_lock);
717 static int rt9467_psy_set_property(struct power_supply *psy,
718 enum power_supply_property psp,
719 const union power_supply_propval *val)
721 struct rt9467_chg_data *data = power_supply_get_drvdata(psy);
724 case POWER_SUPPLY_PROP_STATUS:
725 return regmap_field_write(data->rm_field[F_CHG_EN], val->intval);
726 case POWER_SUPPLY_PROP_ONLINE:
727 return regmap_field_write(data->rm_field[F_HZ], val->intval);
728 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
729 return rt9467_psy_set_ichg(data, val->intval);
730 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
731 return rt9467_set_value_from_ranges(data, F_VOREG,
732 RT9467_RANGE_VOREG, val->intval);
733 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
734 if (val->intval == -1)
735 return rt9467_run_aicl(data);
737 return rt9467_set_value_from_ranges(data, F_IAICR,
740 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
741 return rt9467_set_value_from_ranges(data, F_VMIVR,
742 RT9467_RANGE_VMIVR, val->intval);
743 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
744 return rt9467_set_value_from_ranges(data, F_IPREC,
745 RT9467_RANGE_IPREC, val->intval);
746 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
747 return rt9467_psy_set_ieoc(data, val->intval);
748 case POWER_SUPPLY_PROP_USB_TYPE:
749 return regmap_field_write(data->rm_field[F_USBCHGEN], val->intval);
755 static int rt9467_chg_prop_is_writeable(struct power_supply *psy,
756 enum power_supply_property psp)
759 case POWER_SUPPLY_PROP_STATUS:
760 case POWER_SUPPLY_PROP_ONLINE:
761 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
762 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
763 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
764 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
765 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
766 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
767 case POWER_SUPPLY_PROP_USB_TYPE:
774 static const struct power_supply_desc rt9467_chg_psy_desc = {
775 .name = "rt9467-charger",
776 .type = POWER_SUPPLY_TYPE_USB,
777 .usb_types = rt9467_chg_usb_types,
778 .num_usb_types = ARRAY_SIZE(rt9467_chg_usb_types),
779 .properties = rt9467_chg_properties,
780 .num_properties = ARRAY_SIZE(rt9467_chg_properties),
781 .property_is_writeable = rt9467_chg_prop_is_writeable,
782 .get_property = rt9467_psy_get_property,
783 .set_property = rt9467_psy_set_property,
786 static inline struct rt9467_chg_data *psy_device_to_chip(struct device *dev)
788 return power_supply_get_drvdata(to_power_supply(dev));
791 static ssize_t sysoff_enable_show(struct device *dev,
792 struct device_attribute *attr, char *buf)
794 struct rt9467_chg_data *data = psy_device_to_chip(dev);
795 unsigned int sysoff_enable;
798 ret = regmap_field_read(data->rm_field[F_SHIP_MODE], &sysoff_enable);
802 return sysfs_emit(buf, "%d\n", sysoff_enable);
805 static ssize_t sysoff_enable_store(struct device *dev,
806 struct device_attribute *attr,
807 const char *buf, size_t count)
809 struct rt9467_chg_data *data = psy_device_to_chip(dev);
813 ret = kstrtouint(buf, 10, &tmp);
817 ret = regmap_field_write(data->rm_field[F_SHIP_MODE], !!tmp);
824 static DEVICE_ATTR_RW(sysoff_enable);
826 static struct attribute *rt9467_sysfs_attrs[] = {
827 &dev_attr_sysoff_enable.attr,
831 ATTRIBUTE_GROUPS(rt9467_sysfs);
833 static int rt9467_register_psy(struct rt9467_chg_data *data)
835 struct power_supply_config cfg = {
837 .of_node = dev_of_node(data->dev),
838 .attr_grp = rt9467_sysfs_groups,
841 data->psy = devm_power_supply_register(data->dev, &rt9467_chg_psy_desc,
843 return PTR_ERR_OR_ZERO(data->psy);
846 static int rt9467_mivr_handler(struct rt9467_chg_data *data)
848 unsigned int mivr_act;
852 * back-boost workaround
853 * If (mivr_active & ibus < 100mA), toggle cfo bit
855 ret = regmap_field_read(data->rm_field[F_CHG_MIVR], &mivr_act);
857 dev_err(data->dev, "Failed to read MIVR stat\n");
864 ret = rt9467_get_adc(data, RT9467_ADC_IBUS, &ibus_ma);
866 dev_err(data->dev, "Failed to get IBUS\n");
870 if (ibus_ma < 100000) {
871 ret = regmap_field_write(data->rm_field[F_CFO_EN], 0);
872 ret |= regmap_field_write(data->rm_field[F_CFO_EN], 1);
874 dev_err(data->dev, "Failed to toggle cfo\n");
880 static irqreturn_t rt9467_statc_handler(int irq, void *priv)
882 struct rt9467_chg_data *data = priv;
883 unsigned int new_stat, evts = 0;
886 ret = regmap_read(data->regmap, RT9467_REG_CHG_STATC, &new_stat);
888 dev_err(data->dev, "Failed to read chg_statc\n");
892 evts = data->old_stat ^ new_stat;
893 data->old_stat = new_stat;
895 if ((evts & new_stat) & RT9467_MASK_MIVR_STAT) {
896 ret = rt9467_mivr_handler(data);
898 dev_err(data->dev, "Failed to handle mivr stat\n");
904 static irqreturn_t rt9467_wdt_handler(int irq, void *priv)
906 struct rt9467_chg_data *data = priv;
910 /* Any i2c communication can kick watchdog timer */
911 ret = regmap_read(data->regmap, RT9467_REG_DEVICE_ID, &dev_id);
913 dev_err(data->dev, "Failed to kick wdt (%d)\n", ret);
920 static int rt9467_report_usb_state(struct rt9467_chg_data *data)
922 unsigned int usb_stat, power_ready;
923 bool psy_changed = true;
926 ret = regmap_field_read(data->rm_field[F_USB_STATUS], &usb_stat);
927 ret |= regmap_field_read(data->rm_field[F_PWR_RDY], &power_ready);
932 usb_stat = RT9467_CHG_TYPE_NOVBUS;
934 mutex_lock(&data->attach_lock);
937 case RT9467_CHG_TYPE_NOVBUS:
938 data->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
940 case RT9467_CHG_TYPE_SDP:
941 data->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
943 case RT9467_CHG_TYPE_SDPNSTD:
944 data->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
946 case RT9467_CHG_TYPE_DCP:
947 data->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
949 case RT9467_CHG_TYPE_CDP:
950 data->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
952 case RT9467_CHG_TYPE_UNDER_GOING:
958 mutex_unlock(&data->attach_lock);
961 power_supply_changed(data->psy);
966 static irqreturn_t rt9467_usb_state_handler(int irq, void *priv)
968 struct rt9467_chg_data *data = priv;
971 ret = rt9467_report_usb_state(data);
973 dev_err(data->dev, "Failed to report attach type (%d)\n", ret);
980 static irqreturn_t rt9467_aiclmeas_handler(int irq, void *priv)
982 struct rt9467_chg_data *data = priv;
984 complete(&data->aicl_done);
988 #define RT9467_IRQ_DESC(_name, _handler_func, _hwirq) \
991 .handler = rt9467_##_handler_func##_handler, \
995 static int rt9467_request_interrupt(struct rt9467_chg_data *data)
997 struct device *dev = data->dev;
998 static const struct {
1001 irq_handler_t handler;
1002 } rt9467_exclusive_irqs[] = {
1003 RT9467_IRQ_DESC(statc, statc, RT9467_IRQ_TS_STATC),
1004 RT9467_IRQ_DESC(wdt, wdt, RT9467_IRQ_WDTMR),
1005 RT9467_IRQ_DESC(attach, usb_state, RT9467_IRQ_ATTACH),
1006 RT9467_IRQ_DESC(detach, usb_state, RT9467_IRQ_DETACH),
1007 RT9467_IRQ_DESC(aiclmeas, aiclmeas, RT9467_IRQ_CHG_AICLM),
1008 }, rt9466_exclusive_irqs[] = {
1009 RT9467_IRQ_DESC(statc, statc, RT9467_IRQ_TS_STATC),
1010 RT9467_IRQ_DESC(wdt, wdt, RT9467_IRQ_WDTMR),
1011 RT9467_IRQ_DESC(aiclmeas, aiclmeas, RT9467_IRQ_CHG_AICLM),
1013 int num_chg_irqs, i, virq, ret;
1015 if (data->vid == RT9466_VID) {
1016 chg_irqs = rt9466_exclusive_irqs;
1017 num_chg_irqs = ARRAY_SIZE(rt9466_exclusive_irqs);
1019 chg_irqs = rt9467_exclusive_irqs;
1020 num_chg_irqs = ARRAY_SIZE(rt9467_exclusive_irqs);
1023 for (i = 0; i < num_chg_irqs; i++) {
1024 virq = regmap_irq_get_virq(data->irq_chip_data, chg_irqs[i].hwirq);
1026 return dev_err_probe(dev, -EINVAL, "Failed to get (%s) irq\n",
1029 ret = devm_request_threaded_irq(dev, virq, NULL, chg_irqs[i].handler,
1030 IRQF_ONESHOT, chg_irqs[i].name, data);
1032 return dev_err_probe(dev, ret, "Failed to request (%s) irq\n",
1039 static int rt9467_do_charger_init(struct rt9467_chg_data *data)
1041 struct device *dev = data->dev;
1044 ret = regmap_write(data->regmap, RT9467_REG_CHG_ADC, 0);
1046 return dev_err_probe(dev, ret, "Failed to reset ADC\n");
1048 ret = rt9467_get_value_from_ranges(data, F_ICHG, RT9467_RANGE_ICHG,
1050 ret |= rt9467_get_value_from_ranges(data, F_IEOC, RT9467_RANGE_IEOC,
1053 return dev_err_probe(dev, ret, "Failed to init ichg/ieoc value\n");
1055 ret = regmap_update_bits(data->regmap, RT9467_REG_CHG_STATC_CTRL,
1056 RT9467_MASK_PWR_RDY | RT9467_MASK_MIVR_STAT, 0);
1058 return dev_err_probe(dev, ret, "Failed to make statc unmask\n");
1060 /* Select IINLMTSEL to use AICR */
1061 ret = regmap_field_write(data->rm_field[F_IINLMTSEL],
1062 RT9467_IINLMTSEL_AICR);
1064 return dev_err_probe(dev, ret, "Failed to set iinlmtsel to AICR\n");
1066 /* Wait for AICR Rampping */
1069 /* Disable hardware ILIM */
1070 ret = regmap_field_write(data->rm_field[F_ILIM_EN], 0);
1072 return dev_err_probe(dev, ret, "Failed to disable hardware ILIM\n");
1074 /* Set inductor OCP to high level */
1075 ret = regmap_field_write(data->rm_field[F_OCP], 1);
1077 return dev_err_probe(dev, ret, "Failed to set higher inductor OCP level\n");
1079 /* Set charge termination default enable */
1080 ret = regmap_field_write(data->rm_field[F_TE], 1);
1082 return dev_err_probe(dev, ret, "Failed to set TE=1\n");
1084 /* Set 12hrs fast charger timer */
1085 ret = regmap_field_write(data->rm_field[F_WT_FC], 4);
1087 return dev_err_probe(dev, ret, "Failed to set WT_FC\n");
1089 /* Toggle BC12 function */
1090 ret = regmap_field_write(data->rm_field[F_USBCHGEN], 0);
1092 return dev_err_probe(dev, ret, "Failed to disable BC12\n");
1094 return regmap_field_write(data->rm_field[F_USBCHGEN], 1);
1097 static bool rt9467_is_accessible_reg(struct device *dev, unsigned int reg)
1113 static const struct regmap_config rt9467_regmap_config = {
1116 .max_register = 0x85,
1117 .writeable_reg = rt9467_is_accessible_reg,
1118 .readable_reg = rt9467_is_accessible_reg,
1121 static int rt9467_check_vendor_info(struct rt9467_chg_data *data)
1126 ret = regmap_field_read(data->rm_field[F_VENDOR], &vid);
1128 dev_err(data->dev, "Failed to get vid\n");
1132 if ((vid != RT9466_VID) && (vid != RT9467_VID))
1133 return dev_err_probe(data->dev, -ENODEV,
1134 "VID not correct [0x%02X]\n", vid);
1140 static int rt9467_reset_chip(struct rt9467_chg_data *data)
1144 /* Disable HZ before reset chip */
1145 ret = regmap_field_write(data->rm_field[F_HZ], 0);
1149 return regmap_field_write(data->rm_field[F_RST], 1);
1152 static void rt9467_chg_destroy_adc_lock(void *data)
1154 struct mutex *adc_lock = data;
1156 mutex_destroy(adc_lock);
1159 static void rt9467_chg_destroy_attach_lock(void *data)
1161 struct mutex *attach_lock = data;
1163 mutex_destroy(attach_lock);
1166 static void rt9467_chg_destroy_ichg_ieoc_lock(void *data)
1168 struct mutex *ichg_ieoc_lock = data;
1170 mutex_destroy(ichg_ieoc_lock);
1173 static void rt9467_chg_complete_aicl_done(void *data)
1175 struct completion *aicl_done = data;
1177 complete(aicl_done);
1180 static int rt9467_charger_probe(struct i2c_client *i2c)
1182 struct device *dev = &i2c->dev;
1183 struct rt9467_chg_data *data;
1184 struct gpio_desc *ceb_gpio;
1187 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1191 data->dev = &i2c->dev;
1192 i2c_set_clientdata(i2c, data);
1194 /* Default pull charge enable gpio to make 'CHG_EN' by SW control only */
1195 ceb_gpio = devm_gpiod_get_optional(dev, "charge-enable", GPIOD_OUT_HIGH);
1196 if (IS_ERR(ceb_gpio))
1197 return dev_err_probe(dev, PTR_ERR(ceb_gpio),
1198 "Failed to config charge enable gpio\n");
1200 data->regmap = devm_regmap_init_i2c(i2c, &rt9467_regmap_config);
1201 if (IS_ERR(data->regmap))
1202 return dev_err_probe(dev, PTR_ERR(data->regmap),
1203 "Failed to init regmap\n");
1205 ret = devm_regmap_field_bulk_alloc(dev, data->regmap,
1206 data->rm_field, rt9467_chg_fields,
1207 ARRAY_SIZE(rt9467_chg_fields));
1209 return dev_err_probe(dev, ret, "Failed to alloc regmap fields\n");
1211 ret = rt9467_check_vendor_info(data);
1213 return dev_err_probe(dev, ret, "Failed to check vendor info");
1215 ret = rt9467_reset_chip(data);
1217 return dev_err_probe(dev, ret, "Failed to reset chip\n");
1219 ret = devm_regmap_add_irq_chip(dev, data->regmap, i2c->irq,
1220 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 0,
1221 &rt9467_irq_chip, &data->irq_chip_data);
1223 return dev_err_probe(dev, ret, "Failed to add irq chip\n");
1225 mutex_init(&data->adc_lock);
1226 ret = devm_add_action_or_reset(dev, rt9467_chg_destroy_adc_lock,
1229 return dev_err_probe(dev, ret, "Failed to init ADC lock\n");
1231 mutex_init(&data->attach_lock);
1232 ret = devm_add_action_or_reset(dev, rt9467_chg_destroy_attach_lock,
1233 &data->attach_lock);
1235 return dev_err_probe(dev, ret, "Failed to init attach lock\n");
1237 mutex_init(&data->ichg_ieoc_lock);
1238 ret = devm_add_action_or_reset(dev, rt9467_chg_destroy_ichg_ieoc_lock,
1239 &data->ichg_ieoc_lock);
1241 return dev_err_probe(dev, ret, "Failed to init ICHG/IEOC lock\n");
1243 init_completion(&data->aicl_done);
1244 ret = devm_add_action_or_reset(dev, rt9467_chg_complete_aicl_done,
1247 return dev_err_probe(dev, ret, "Failed to init AICL done completion\n");
1249 ret = rt9467_do_charger_init(data);
1253 ret = rt9467_register_otg_regulator(data);
1257 ret = rt9467_register_psy(data);
1261 return rt9467_request_interrupt(data);
1264 static const struct of_device_id rt9467_charger_of_match_table[] = {
1265 { .compatible = "richtek,rt9467", },
1268 MODULE_DEVICE_TABLE(of, rt9467_charger_of_match_table);
1270 static struct i2c_driver rt9467_charger_driver = {
1272 .name = "rt9467-charger",
1273 .of_match_table = rt9467_charger_of_match_table,
1275 .probe = rt9467_charger_probe,
1277 module_i2c_driver(rt9467_charger_driver);
1279 MODULE_DESCRIPTION("Richtek RT9467 Charger Driver");
1280 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
1281 MODULE_AUTHOR("ChiaEn Wu <chiaen_wu@richtek.com>");
1282 MODULE_LICENSE("GPL");