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 rk805_regmap_config = {
76 .max_register = RK805_OFF_SOURCE_REG,
77 .cache_type = REGCACHE_RBTREE,
78 .volatile_reg = rk808_is_volatile_reg,
81 static const struct regmap_config rk808_regmap_config = {
84 .max_register = RK808_IO_POL_REG,
85 .cache_type = REGCACHE_RBTREE,
86 .volatile_reg = rk808_is_volatile_reg,
89 static struct resource rtc_resources[] = {
91 .start = RK808_IRQ_RTC_ALARM,
92 .end = RK808_IRQ_RTC_ALARM,
93 .flags = IORESOURCE_IRQ,
97 static struct resource rk805_key_resources[] = {
99 .start = RK805_IRQ_PWRON_FALL,
100 .end = RK805_IRQ_PWRON_FALL,
101 .flags = IORESOURCE_IRQ,
104 .start = RK805_IRQ_PWRON_RISE,
105 .end = RK805_IRQ_PWRON_RISE,
106 .flags = IORESOURCE_IRQ,
110 static const struct mfd_cell rk805s[] = {
111 { .name = "rk808-clkout", },
112 { .name = "rk808-regulator", },
113 { .name = "rk805-pinctrl", },
116 .num_resources = ARRAY_SIZE(rtc_resources),
117 .resources = &rtc_resources[0],
119 { .name = "rk805-pwrkey",
120 .num_resources = ARRAY_SIZE(rk805_key_resources),
121 .resources = &rk805_key_resources[0],
125 static const struct mfd_cell rk808s[] = {
126 { .name = "rk808-clkout", },
127 { .name = "rk808-regulator", },
130 .num_resources = ARRAY_SIZE(rtc_resources),
131 .resources = rtc_resources,
135 static const struct mfd_cell rk818s[] = {
136 { .name = "rk808-clkout", },
137 { .name = "rk808-regulator", },
140 .num_resources = ARRAY_SIZE(rtc_resources),
141 .resources = rtc_resources,
145 static const struct rk808_reg_data rk805_pre_init_reg[] = {
146 {RK805_BUCK1_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
147 RK805_BUCK1_2_ILMAX_4000MA},
148 {RK805_BUCK2_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
149 RK805_BUCK1_2_ILMAX_4000MA},
150 {RK805_BUCK3_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
151 RK805_BUCK3_ILMAX_3000MA},
152 {RK805_BUCK4_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
153 RK805_BUCK4_ILMAX_3500MA},
154 {RK805_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_400MA},
155 {RK805_GPIO_IO_POL_REG, SLP_SD_MSK, SLEEP_FUN},
156 {RK805_THERMAL_REG, TEMP_HOTDIE_MSK, TEMP115C},
159 static const struct rk808_reg_data rk808_pre_init_reg[] = {
160 { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
161 { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
162 { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
163 { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
164 { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
165 { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
166 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
170 static const struct rk808_reg_data rk818_pre_init_reg[] = {
171 /* improve efficiency */
172 { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
173 { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
174 { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
175 { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
176 RK818_USB_ILMIN_2000MA },
177 /* close charger when usb lower then 3.4V */
178 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
180 /* no action when vref */
181 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
183 { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
184 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
188 static const struct regmap_irq rk805_irqs[] = {
189 [RK805_IRQ_PWRON_RISE] = {
190 .mask = RK805_IRQ_PWRON_RISE_MSK,
193 [RK805_IRQ_VB_LOW] = {
194 .mask = RK805_IRQ_VB_LOW_MSK,
197 [RK805_IRQ_PWRON] = {
198 .mask = RK805_IRQ_PWRON_MSK,
201 [RK805_IRQ_PWRON_LP] = {
202 .mask = RK805_IRQ_PWRON_LP_MSK,
205 [RK805_IRQ_HOTDIE] = {
206 .mask = RK805_IRQ_HOTDIE_MSK,
209 [RK805_IRQ_RTC_ALARM] = {
210 .mask = RK805_IRQ_RTC_ALARM_MSK,
213 [RK805_IRQ_RTC_PERIOD] = {
214 .mask = RK805_IRQ_RTC_PERIOD_MSK,
217 [RK805_IRQ_PWRON_FALL] = {
218 .mask = RK805_IRQ_PWRON_FALL_MSK,
223 static const struct regmap_irq rk808_irqs[] = {
225 [RK808_IRQ_VOUT_LO] = {
226 .mask = RK808_IRQ_VOUT_LO_MSK,
229 [RK808_IRQ_VB_LO] = {
230 .mask = RK808_IRQ_VB_LO_MSK,
233 [RK808_IRQ_PWRON] = {
234 .mask = RK808_IRQ_PWRON_MSK,
237 [RK808_IRQ_PWRON_LP] = {
238 .mask = RK808_IRQ_PWRON_LP_MSK,
241 [RK808_IRQ_HOTDIE] = {
242 .mask = RK808_IRQ_HOTDIE_MSK,
245 [RK808_IRQ_RTC_ALARM] = {
246 .mask = RK808_IRQ_RTC_ALARM_MSK,
249 [RK808_IRQ_RTC_PERIOD] = {
250 .mask = RK808_IRQ_RTC_PERIOD_MSK,
255 [RK808_IRQ_PLUG_IN_INT] = {
256 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
259 [RK808_IRQ_PLUG_OUT_INT] = {
260 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
265 static const struct regmap_irq rk818_irqs[] = {
267 [RK818_IRQ_VOUT_LO] = {
268 .mask = RK818_IRQ_VOUT_LO_MSK,
271 [RK818_IRQ_VB_LO] = {
272 .mask = RK818_IRQ_VB_LO_MSK,
275 [RK818_IRQ_PWRON] = {
276 .mask = RK818_IRQ_PWRON_MSK,
279 [RK818_IRQ_PWRON_LP] = {
280 .mask = RK818_IRQ_PWRON_LP_MSK,
283 [RK818_IRQ_HOTDIE] = {
284 .mask = RK818_IRQ_HOTDIE_MSK,
287 [RK818_IRQ_RTC_ALARM] = {
288 .mask = RK818_IRQ_RTC_ALARM_MSK,
291 [RK818_IRQ_RTC_PERIOD] = {
292 .mask = RK818_IRQ_RTC_PERIOD_MSK,
295 [RK818_IRQ_USB_OV] = {
296 .mask = RK818_IRQ_USB_OV_MSK,
301 [RK818_IRQ_PLUG_IN] = {
302 .mask = RK818_IRQ_PLUG_IN_MSK,
305 [RK818_IRQ_PLUG_OUT] = {
306 .mask = RK818_IRQ_PLUG_OUT_MSK,
309 [RK818_IRQ_CHG_OK] = {
310 .mask = RK818_IRQ_CHG_OK_MSK,
313 [RK818_IRQ_CHG_TE] = {
314 .mask = RK818_IRQ_CHG_TE_MSK,
317 [RK818_IRQ_CHG_TS1] = {
318 .mask = RK818_IRQ_CHG_TS1_MSK,
322 .mask = RK818_IRQ_TS2_MSK,
325 [RK818_IRQ_CHG_CVTLIM] = {
326 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
329 [RK818_IRQ_DISCHG_ILIM] = {
330 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
335 static struct regmap_irq_chip rk805_irq_chip = {
338 .num_irqs = ARRAY_SIZE(rk805_irqs),
340 .status_base = RK805_INT_STS_REG,
341 .mask_base = RK805_INT_STS_MSK_REG,
342 .ack_base = RK805_INT_STS_REG,
343 .init_ack_masked = true,
346 static const struct regmap_irq_chip rk808_irq_chip = {
349 .num_irqs = ARRAY_SIZE(rk808_irqs),
352 .status_base = RK808_INT_STS_REG1,
353 .mask_base = RK808_INT_STS_MSK_REG1,
354 .ack_base = RK808_INT_STS_REG1,
355 .init_ack_masked = true,
358 static const struct regmap_irq_chip rk818_irq_chip = {
361 .num_irqs = ARRAY_SIZE(rk818_irqs),
364 .status_base = RK818_INT_STS_REG1,
365 .mask_base = RK818_INT_STS_MSK_REG1,
366 .ack_base = RK818_INT_STS_REG1,
367 .init_ack_masked = true,
370 static struct i2c_client *rk808_i2c_client;
372 static void rk805_device_shutdown(void)
375 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
378 dev_warn(&rk808_i2c_client->dev,
379 "have no rk805, so do nothing here\n");
383 ret = regmap_update_bits(rk808->regmap,
387 dev_err(&rk808_i2c_client->dev, "power off error!\n");
390 static void rk808_device_shutdown(void)
393 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
396 dev_warn(&rk808_i2c_client->dev,
397 "have no rk808, so do nothing here\n");
401 ret = regmap_update_bits(rk808->regmap,
403 DEV_OFF_RST, DEV_OFF_RST);
405 dev_err(&rk808_i2c_client->dev, "power off error!\n");
408 static void rk818_device_shutdown(void)
411 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
414 dev_warn(&rk808_i2c_client->dev,
415 "have no rk818, so do nothing here\n");
419 ret = regmap_update_bits(rk808->regmap,
423 dev_err(&rk808_i2c_client->dev, "power off error!\n");
426 static const struct of_device_id rk808_of_match[] = {
427 { .compatible = "rockchip,rk805" },
428 { .compatible = "rockchip,rk808" },
429 { .compatible = "rockchip,rk818" },
432 MODULE_DEVICE_TABLE(of, rk808_of_match);
434 static int rk808_probe(struct i2c_client *client,
435 const struct i2c_device_id *id)
437 struct device_node *np = client->dev.of_node;
439 const struct rk808_reg_data *pre_init_reg;
440 const struct mfd_cell *cells;
441 void (*pm_pwroff_fn)(void);
442 int nr_pre_init_regs;
444 int pm_off = 0, msb, lsb;
448 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
452 /* Read chip variant */
453 msb = i2c_smbus_read_byte_data(client, RK808_ID_MSB);
455 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
460 lsb = i2c_smbus_read_byte_data(client, RK808_ID_LSB);
462 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
467 rk808->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
468 dev_info(&client->dev, "chip id: 0x%x\n", (unsigned int)rk808->variant);
470 switch (rk808->variant) {
472 rk808->regmap_cfg = &rk805_regmap_config;
473 rk808->regmap_irq_chip = &rk805_irq_chip;
474 pre_init_reg = rk805_pre_init_reg;
475 nr_pre_init_regs = ARRAY_SIZE(rk805_pre_init_reg);
477 nr_cells = ARRAY_SIZE(rk805s);
478 pm_pwroff_fn = rk805_device_shutdown;
481 rk808->regmap_cfg = &rk808_regmap_config;
482 rk808->regmap_irq_chip = &rk808_irq_chip;
483 pre_init_reg = rk808_pre_init_reg;
484 nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
486 nr_cells = ARRAY_SIZE(rk808s);
487 pm_pwroff_fn = rk808_device_shutdown;
490 rk808->regmap_cfg = &rk818_regmap_config;
491 rk808->regmap_irq_chip = &rk818_irq_chip;
492 pre_init_reg = rk818_pre_init_reg;
493 nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
495 nr_cells = ARRAY_SIZE(rk818s);
496 pm_pwroff_fn = rk818_device_shutdown;
499 dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
505 i2c_set_clientdata(client, rk808);
507 rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
508 if (IS_ERR(rk808->regmap)) {
509 dev_err(&client->dev, "regmap initialization failed\n");
510 return PTR_ERR(rk808->regmap);
514 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
518 ret = regmap_add_irq_chip(rk808->regmap, client->irq,
520 rk808->regmap_irq_chip, &rk808->irq_data);
522 dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
526 for (i = 0; i < nr_pre_init_regs; i++) {
527 ret = regmap_update_bits(rk808->regmap,
528 pre_init_reg[i].addr,
529 pre_init_reg[i].mask,
530 pre_init_reg[i].value);
532 dev_err(&client->dev,
534 pre_init_reg[i].addr);
539 ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
540 cells, nr_cells, NULL, 0,
541 regmap_irq_get_domain(rk808->irq_data));
543 dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
547 pm_off = of_property_read_bool(np,
548 "rockchip,system-power-controller");
549 if (pm_off && !pm_power_off) {
550 rk808_i2c_client = client;
551 pm_power_off = pm_pwroff_fn;
557 regmap_del_irq_chip(client->irq, rk808->irq_data);
561 static int rk808_remove(struct i2c_client *client)
563 struct rk808 *rk808 = i2c_get_clientdata(client);
565 regmap_del_irq_chip(client->irq, rk808->irq_data);
571 static const struct i2c_device_id rk808_ids[] = {
577 MODULE_DEVICE_TABLE(i2c, rk808_ids);
579 static struct i2c_driver rk808_i2c_driver = {
582 .of_match_table = rk808_of_match,
584 .probe = rk808_probe,
585 .remove = rk808_remove,
586 .id_table = rk808_ids,
589 module_i2c_driver(rk808_i2c_driver);
591 MODULE_LICENSE("GPL");
592 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
593 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
594 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
595 MODULE_DESCRIPTION("RK808/RK818 PMIC driver");