2 * MFD core driver for Rockchip RK808/RK818
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
9 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11 * Author: Wadim Egorov <w.egorov@phytec.de>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 #include <linux/i2c.h>
24 #include <linux/interrupt.h>
25 #include <linux/mfd/rk808.h>
26 #include <linux/mfd/core.h>
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/regmap.h>
31 struct rk808_reg_data
{
37 static bool rk808_is_volatile_reg(struct device
*dev
, unsigned int reg
)
41 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
42 * we don't use that feature. It's better to cache.
43 * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
44 * bits are cleared in case when we shutoff anyway, but better safe.
48 case RK808_SECONDS_REG
... RK808_WEEKS_REG
:
49 case RK808_RTC_STATUS_REG
:
50 case RK808_VB_MON_REG
:
51 case RK808_THERMAL_REG
:
52 case RK808_DCDC_UV_STS_REG
:
53 case RK808_LDO_UV_STS_REG
:
54 case RK808_DCDC_PG_REG
:
55 case RK808_LDO_PG_REG
:
56 case RK808_DEVCTRL_REG
:
57 case RK808_INT_STS_REG1
:
58 case RK808_INT_STS_REG2
:
65 static const struct regmap_config rk818_regmap_config
= {
68 .max_register
= RK818_USB_CTRL_REG
,
69 .cache_type
= REGCACHE_RBTREE
,
70 .volatile_reg
= rk808_is_volatile_reg
,
73 static const struct regmap_config rk808_regmap_config
= {
76 .max_register
= RK808_IO_POL_REG
,
77 .cache_type
= REGCACHE_RBTREE
,
78 .volatile_reg
= rk808_is_volatile_reg
,
81 static struct resource rtc_resources
[] = {
83 .start
= RK808_IRQ_RTC_ALARM
,
84 .end
= RK808_IRQ_RTC_ALARM
,
85 .flags
= IORESOURCE_IRQ
,
89 static const struct mfd_cell rk808s
[] = {
90 { .name
= "rk808-clkout", },
91 { .name
= "rk808-regulator", },
94 .num_resources
= ARRAY_SIZE(rtc_resources
),
95 .resources
= rtc_resources
,
99 static const struct mfd_cell rk818s
[] = {
100 { .name
= "rk808-clkout", },
101 { .name
= "rk808-regulator", },
104 .num_resources
= ARRAY_SIZE(rtc_resources
),
105 .resources
= rtc_resources
,
109 static const struct rk808_reg_data rk808_pre_init_reg
[] = {
110 { RK808_BUCK3_CONFIG_REG
, BUCK_ILMIN_MASK
, BUCK_ILMIN_150MA
},
111 { RK808_BUCK4_CONFIG_REG
, BUCK_ILMIN_MASK
, BUCK_ILMIN_200MA
},
112 { RK808_BOOST_CONFIG_REG
, BOOST_ILMIN_MASK
, BOOST_ILMIN_100MA
},
113 { RK808_BUCK1_CONFIG_REG
, BUCK1_RATE_MASK
, BUCK_ILMIN_200MA
},
114 { RK808_BUCK2_CONFIG_REG
, BUCK2_RATE_MASK
, BUCK_ILMIN_200MA
},
115 { RK808_DCDC_UV_ACT_REG
, BUCK_UV_ACT_MASK
, BUCK_UV_ACT_DISABLE
},
116 { RK808_VB_MON_REG
, MASK_ALL
, VB_LO_ACT
|
120 static const struct rk808_reg_data rk818_pre_init_reg
[] = {
121 /* improve efficiency */
122 { RK818_BUCK2_CONFIG_REG
, BUCK2_RATE_MASK
, BUCK_ILMIN_250MA
},
123 { RK818_BUCK4_CONFIG_REG
, BUCK_ILMIN_MASK
, BUCK_ILMIN_250MA
},
124 { RK818_BOOST_CONFIG_REG
, BOOST_ILMIN_MASK
, BOOST_ILMIN_100MA
},
125 { RK818_USB_CTRL_REG
, RK818_USB_ILIM_SEL_MASK
,
126 RK818_USB_ILMIN_2000MA
},
127 /* close charger when usb lower then 3.4V */
128 { RK818_USB_CTRL_REG
, RK818_USB_CHG_SD_VSEL_MASK
,
130 /* no action when vref */
131 { RK818_H5V_EN_REG
, BIT(1), RK818_REF_RDY_CTRL
},
133 { RK818_H5V_EN_REG
, BIT(0), RK818_H5V_EN
},
134 { RK808_VB_MON_REG
, MASK_ALL
, VB_LO_ACT
|
138 static const struct regmap_irq rk808_irqs
[] = {
140 [RK808_IRQ_VOUT_LO
] = {
141 .mask
= RK808_IRQ_VOUT_LO_MSK
,
144 [RK808_IRQ_VB_LO
] = {
145 .mask
= RK808_IRQ_VB_LO_MSK
,
148 [RK808_IRQ_PWRON
] = {
149 .mask
= RK808_IRQ_PWRON_MSK
,
152 [RK808_IRQ_PWRON_LP
] = {
153 .mask
= RK808_IRQ_PWRON_LP_MSK
,
156 [RK808_IRQ_HOTDIE
] = {
157 .mask
= RK808_IRQ_HOTDIE_MSK
,
160 [RK808_IRQ_RTC_ALARM
] = {
161 .mask
= RK808_IRQ_RTC_ALARM_MSK
,
164 [RK808_IRQ_RTC_PERIOD
] = {
165 .mask
= RK808_IRQ_RTC_PERIOD_MSK
,
170 [RK808_IRQ_PLUG_IN_INT
] = {
171 .mask
= RK808_IRQ_PLUG_IN_INT_MSK
,
174 [RK808_IRQ_PLUG_OUT_INT
] = {
175 .mask
= RK808_IRQ_PLUG_OUT_INT_MSK
,
180 static const struct regmap_irq rk818_irqs
[] = {
182 [RK818_IRQ_VOUT_LO
] = {
183 .mask
= RK818_IRQ_VOUT_LO_MSK
,
186 [RK818_IRQ_VB_LO
] = {
187 .mask
= RK818_IRQ_VB_LO_MSK
,
190 [RK818_IRQ_PWRON
] = {
191 .mask
= RK818_IRQ_PWRON_MSK
,
194 [RK818_IRQ_PWRON_LP
] = {
195 .mask
= RK818_IRQ_PWRON_LP_MSK
,
198 [RK818_IRQ_HOTDIE
] = {
199 .mask
= RK818_IRQ_HOTDIE_MSK
,
202 [RK818_IRQ_RTC_ALARM
] = {
203 .mask
= RK818_IRQ_RTC_ALARM_MSK
,
206 [RK818_IRQ_RTC_PERIOD
] = {
207 .mask
= RK818_IRQ_RTC_PERIOD_MSK
,
210 [RK818_IRQ_USB_OV
] = {
211 .mask
= RK818_IRQ_USB_OV_MSK
,
216 [RK818_IRQ_PLUG_IN
] = {
217 .mask
= RK818_IRQ_PLUG_IN_MSK
,
220 [RK818_IRQ_PLUG_OUT
] = {
221 .mask
= RK818_IRQ_PLUG_OUT_MSK
,
224 [RK818_IRQ_CHG_OK
] = {
225 .mask
= RK818_IRQ_CHG_OK_MSK
,
228 [RK818_IRQ_CHG_TE
] = {
229 .mask
= RK818_IRQ_CHG_TE_MSK
,
232 [RK818_IRQ_CHG_TS1
] = {
233 .mask
= RK818_IRQ_CHG_TS1_MSK
,
237 .mask
= RK818_IRQ_TS2_MSK
,
240 [RK818_IRQ_CHG_CVTLIM
] = {
241 .mask
= RK818_IRQ_CHG_CVTLIM_MSK
,
244 [RK818_IRQ_DISCHG_ILIM
] = {
245 .mask
= RK818_IRQ_DISCHG_ILIM_MSK
,
250 static const struct regmap_irq_chip rk808_irq_chip
= {
253 .num_irqs
= ARRAY_SIZE(rk808_irqs
),
256 .status_base
= RK808_INT_STS_REG1
,
257 .mask_base
= RK808_INT_STS_MSK_REG1
,
258 .ack_base
= RK808_INT_STS_REG1
,
259 .init_ack_masked
= true,
262 static const struct regmap_irq_chip rk818_irq_chip
= {
265 .num_irqs
= ARRAY_SIZE(rk818_irqs
),
268 .status_base
= RK818_INT_STS_REG1
,
269 .mask_base
= RK818_INT_STS_MSK_REG1
,
270 .ack_base
= RK818_INT_STS_REG1
,
271 .init_ack_masked
= true,
274 static struct i2c_client
*rk808_i2c_client
;
275 static void rk808_device_shutdown(void)
278 struct rk808
*rk808
= i2c_get_clientdata(rk808_i2c_client
);
281 dev_warn(&rk808_i2c_client
->dev
,
282 "have no rk808, so do nothing here\n");
286 ret
= regmap_update_bits(rk808
->regmap
,
288 DEV_OFF_RST
, DEV_OFF_RST
);
290 dev_err(&rk808_i2c_client
->dev
, "power off error!\n");
293 static void rk818_device_shutdown(void)
296 struct rk808
*rk808
= i2c_get_clientdata(rk808_i2c_client
);
299 dev_warn(&rk808_i2c_client
->dev
,
300 "have no rk818, so do nothing here\n");
304 ret
= regmap_update_bits(rk808
->regmap
,
308 dev_err(&rk808_i2c_client
->dev
, "power off error!\n");
311 static const struct of_device_id rk808_of_match
[] = {
312 { .compatible
= "rockchip,rk808" },
313 { .compatible
= "rockchip,rk818" },
316 MODULE_DEVICE_TABLE(of
, rk808_of_match
);
318 static int rk808_probe(struct i2c_client
*client
,
319 const struct i2c_device_id
*id
)
321 struct device_node
*np
= client
->dev
.of_node
;
323 const struct rk808_reg_data
*pre_init_reg
;
324 const struct mfd_cell
*cells
;
325 void (*pm_pwroff_fn
)(void);
326 int nr_pre_init_regs
;
332 rk808
= devm_kzalloc(&client
->dev
, sizeof(*rk808
), GFP_KERNEL
);
336 rk808
->variant
= i2c_smbus_read_word_data(client
, RK808_ID_MSB
);
337 if (rk808
->variant
< 0) {
338 dev_err(&client
->dev
, "Failed to read the chip id at 0x%02x\n",
340 return rk808
->variant
;
343 dev_dbg(&client
->dev
, "Chip id: 0x%x\n", (unsigned int)rk808
->variant
);
345 switch (rk808
->variant
) {
347 rk808
->regmap_cfg
= &rk808_regmap_config
;
348 rk808
->regmap_irq_chip
= &rk808_irq_chip
;
349 pre_init_reg
= rk808_pre_init_reg
;
350 nr_pre_init_regs
= ARRAY_SIZE(rk808_pre_init_reg
);
352 nr_cells
= ARRAY_SIZE(rk808s
);
353 pm_pwroff_fn
= rk808_device_shutdown
;
356 rk808
->regmap_cfg
= &rk818_regmap_config
;
357 rk808
->regmap_irq_chip
= &rk818_irq_chip
;
358 pre_init_reg
= rk818_pre_init_reg
;
359 nr_pre_init_regs
= ARRAY_SIZE(rk818_pre_init_reg
);
361 nr_cells
= ARRAY_SIZE(rk818s
);
362 pm_pwroff_fn
= rk818_device_shutdown
;
365 dev_err(&client
->dev
, "Unsupported RK8XX ID %lu\n",
371 i2c_set_clientdata(client
, rk808
);
373 rk808
->regmap
= devm_regmap_init_i2c(client
, rk808
->regmap_cfg
);
374 if (IS_ERR(rk808
->regmap
)) {
375 dev_err(&client
->dev
, "regmap initialization failed\n");
376 return PTR_ERR(rk808
->regmap
);
380 dev_err(&client
->dev
, "No interrupt support, no core IRQ\n");
384 ret
= regmap_add_irq_chip(rk808
->regmap
, client
->irq
,
386 rk808
->regmap_irq_chip
, &rk808
->irq_data
);
388 dev_err(&client
->dev
, "Failed to add irq_chip %d\n", ret
);
392 for (i
= 0; i
< nr_pre_init_regs
; i
++) {
393 ret
= regmap_update_bits(rk808
->regmap
,
394 pre_init_reg
[i
].addr
,
395 pre_init_reg
[i
].mask
,
396 pre_init_reg
[i
].value
);
398 dev_err(&client
->dev
,
400 pre_init_reg
[i
].addr
);
405 ret
= devm_mfd_add_devices(&client
->dev
, PLATFORM_DEVID_NONE
,
406 cells
, nr_cells
, NULL
, 0,
407 regmap_irq_get_domain(rk808
->irq_data
));
409 dev_err(&client
->dev
, "failed to add MFD devices %d\n", ret
);
413 pm_off
= of_property_read_bool(np
,
414 "rockchip,system-power-controller");
415 if (pm_off
&& !pm_power_off
) {
416 rk808_i2c_client
= client
;
417 pm_power_off
= pm_pwroff_fn
;
423 regmap_del_irq_chip(client
->irq
, rk808
->irq_data
);
427 static int rk808_remove(struct i2c_client
*client
)
429 struct rk808
*rk808
= i2c_get_clientdata(client
);
431 regmap_del_irq_chip(client
->irq
, rk808
->irq_data
);
437 static const struct i2c_device_id rk808_ids
[] = {
442 MODULE_DEVICE_TABLE(i2c
, rk808_ids
);
444 static struct i2c_driver rk808_i2c_driver
= {
447 .of_match_table
= rk808_of_match
,
449 .probe
= rk808_probe
,
450 .remove
= rk808_remove
,
451 .id_table
= rk808_ids
,
454 module_i2c_driver(rk808_i2c_driver
);
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
458 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
459 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
460 MODULE_DESCRIPTION("RK808/RK818 PMIC driver");