treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / mfd / rk808.c
bloba69a6742ecdc3786e69652b5a79f304e11d74a2e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MFD core driver for Rockchip RK808/RK818
5 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
7 * Author: Chris Zhong <zyw@rock-chips.com>
8 * Author: Zhang Qing <zhangqing@rock-chips.com>
10 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
12 * Author: Wadim Egorov <w.egorov@phytec.de>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/rk808.h>
18 #include <linux/mfd/core.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/regmap.h>
22 #include <linux/syscore_ops.h>
24 struct rk808_reg_data {
25 int addr;
26 int mask;
27 int value;
30 static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
33 * Notes:
34 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
35 * we don't use that feature. It's better to cache.
36 * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
37 * bits are cleared in case when we shutoff anyway, but better safe.
40 switch (reg) {
41 case RK808_SECONDS_REG ... RK808_WEEKS_REG:
42 case RK808_RTC_STATUS_REG:
43 case RK808_VB_MON_REG:
44 case RK808_THERMAL_REG:
45 case RK808_DCDC_UV_STS_REG:
46 case RK808_LDO_UV_STS_REG:
47 case RK808_DCDC_PG_REG:
48 case RK808_LDO_PG_REG:
49 case RK808_DEVCTRL_REG:
50 case RK808_INT_STS_REG1:
51 case RK808_INT_STS_REG2:
52 return true;
55 return false;
58 static bool rk817_is_volatile_reg(struct device *dev, unsigned int reg)
61 * Notes:
62 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
63 * we don't use that feature. It's better to cache.
66 switch (reg) {
67 case RK817_SECONDS_REG ... RK817_WEEKS_REG:
68 case RK817_RTC_STATUS_REG:
69 case RK817_INT_STS_REG0:
70 case RK817_INT_STS_REG1:
71 case RK817_INT_STS_REG2:
72 case RK817_SYS_STS:
73 return true;
76 return true;
79 static const struct regmap_config rk818_regmap_config = {
80 .reg_bits = 8,
81 .val_bits = 8,
82 .max_register = RK818_USB_CTRL_REG,
83 .cache_type = REGCACHE_RBTREE,
84 .volatile_reg = rk808_is_volatile_reg,
87 static const struct regmap_config rk805_regmap_config = {
88 .reg_bits = 8,
89 .val_bits = 8,
90 .max_register = RK805_OFF_SOURCE_REG,
91 .cache_type = REGCACHE_RBTREE,
92 .volatile_reg = rk808_is_volatile_reg,
95 static const struct regmap_config rk808_regmap_config = {
96 .reg_bits = 8,
97 .val_bits = 8,
98 .max_register = RK808_IO_POL_REG,
99 .cache_type = REGCACHE_RBTREE,
100 .volatile_reg = rk808_is_volatile_reg,
103 static const struct regmap_config rk817_regmap_config = {
104 .reg_bits = 8,
105 .val_bits = 8,
106 .max_register = RK817_GPIO_INT_CFG,
107 .cache_type = REGCACHE_NONE,
108 .volatile_reg = rk817_is_volatile_reg,
111 static struct resource rtc_resources[] = {
112 DEFINE_RES_IRQ(RK808_IRQ_RTC_ALARM),
115 static struct resource rk817_rtc_resources[] = {
116 DEFINE_RES_IRQ(RK817_IRQ_RTC_ALARM),
119 static struct resource rk805_key_resources[] = {
120 DEFINE_RES_IRQ(RK805_IRQ_PWRON_RISE),
121 DEFINE_RES_IRQ(RK805_IRQ_PWRON_FALL),
124 static struct resource rk817_pwrkey_resources[] = {
125 DEFINE_RES_IRQ(RK817_IRQ_PWRON_RISE),
126 DEFINE_RES_IRQ(RK817_IRQ_PWRON_FALL),
129 static const struct mfd_cell rk805s[] = {
130 { .name = "rk808-clkout", },
131 { .name = "rk808-regulator", },
132 { .name = "rk805-pinctrl", },
134 .name = "rk808-rtc",
135 .num_resources = ARRAY_SIZE(rtc_resources),
136 .resources = &rtc_resources[0],
138 { .name = "rk805-pwrkey",
139 .num_resources = ARRAY_SIZE(rk805_key_resources),
140 .resources = &rk805_key_resources[0],
144 static const struct mfd_cell rk808s[] = {
145 { .name = "rk808-clkout", },
146 { .name = "rk808-regulator", },
148 .name = "rk808-rtc",
149 .num_resources = ARRAY_SIZE(rtc_resources),
150 .resources = rtc_resources,
154 static const struct mfd_cell rk817s[] = {
155 { .name = "rk808-clkout",},
156 { .name = "rk808-regulator",},
158 .name = "rk805-pwrkey",
159 .num_resources = ARRAY_SIZE(rk817_pwrkey_resources),
160 .resources = &rk817_pwrkey_resources[0],
163 .name = "rk808-rtc",
164 .num_resources = ARRAY_SIZE(rk817_rtc_resources),
165 .resources = &rk817_rtc_resources[0],
169 static const struct mfd_cell rk818s[] = {
170 { .name = "rk808-clkout", },
171 { .name = "rk808-regulator", },
173 .name = "rk808-rtc",
174 .num_resources = ARRAY_SIZE(rtc_resources),
175 .resources = rtc_resources,
179 static const struct rk808_reg_data rk805_pre_init_reg[] = {
180 {RK805_BUCK1_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
181 RK805_BUCK1_2_ILMAX_4000MA},
182 {RK805_BUCK2_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
183 RK805_BUCK1_2_ILMAX_4000MA},
184 {RK805_BUCK3_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
185 RK805_BUCK3_ILMAX_3000MA},
186 {RK805_BUCK4_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
187 RK805_BUCK4_ILMAX_3500MA},
188 {RK805_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_400MA},
189 {RK805_GPIO_IO_POL_REG, SLP_SD_MSK, SLEEP_FUN},
190 {RK805_THERMAL_REG, TEMP_HOTDIE_MSK, TEMP115C},
193 static const struct rk808_reg_data rk808_pre_init_reg[] = {
194 { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
195 { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
196 { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
197 { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
198 { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
199 { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
200 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
201 VB_LO_SEL_3500MV },
204 static const struct rk808_reg_data rk817_pre_init_reg[] = {
205 {RK817_RTC_CTRL_REG, RTC_STOP, RTC_STOP},
206 {RK817_GPIO_INT_CFG, RK817_INT_POL_MSK, RK817_INT_POL_L},
207 {RK817_SYS_CFG(1), RK817_HOTDIE_TEMP_MSK | RK817_TSD_TEMP_MSK,
208 RK817_HOTDIE_105 | RK817_TSD_140},
211 static const struct rk808_reg_data rk818_pre_init_reg[] = {
212 /* improve efficiency */
213 { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
214 { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
215 { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
216 { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
217 RK818_USB_ILMIN_2000MA },
218 /* close charger when usb lower then 3.4V */
219 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
220 (0x7 << 4) },
221 /* no action when vref */
222 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
223 /* enable HDMI 5V */
224 { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
225 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
226 VB_LO_SEL_3500MV },
229 static const struct regmap_irq rk805_irqs[] = {
230 [RK805_IRQ_PWRON_RISE] = {
231 .mask = RK805_IRQ_PWRON_RISE_MSK,
232 .reg_offset = 0,
234 [RK805_IRQ_VB_LOW] = {
235 .mask = RK805_IRQ_VB_LOW_MSK,
236 .reg_offset = 0,
238 [RK805_IRQ_PWRON] = {
239 .mask = RK805_IRQ_PWRON_MSK,
240 .reg_offset = 0,
242 [RK805_IRQ_PWRON_LP] = {
243 .mask = RK805_IRQ_PWRON_LP_MSK,
244 .reg_offset = 0,
246 [RK805_IRQ_HOTDIE] = {
247 .mask = RK805_IRQ_HOTDIE_MSK,
248 .reg_offset = 0,
250 [RK805_IRQ_RTC_ALARM] = {
251 .mask = RK805_IRQ_RTC_ALARM_MSK,
252 .reg_offset = 0,
254 [RK805_IRQ_RTC_PERIOD] = {
255 .mask = RK805_IRQ_RTC_PERIOD_MSK,
256 .reg_offset = 0,
258 [RK805_IRQ_PWRON_FALL] = {
259 .mask = RK805_IRQ_PWRON_FALL_MSK,
260 .reg_offset = 0,
264 static const struct regmap_irq rk808_irqs[] = {
265 /* INT_STS */
266 [RK808_IRQ_VOUT_LO] = {
267 .mask = RK808_IRQ_VOUT_LO_MSK,
268 .reg_offset = 0,
270 [RK808_IRQ_VB_LO] = {
271 .mask = RK808_IRQ_VB_LO_MSK,
272 .reg_offset = 0,
274 [RK808_IRQ_PWRON] = {
275 .mask = RK808_IRQ_PWRON_MSK,
276 .reg_offset = 0,
278 [RK808_IRQ_PWRON_LP] = {
279 .mask = RK808_IRQ_PWRON_LP_MSK,
280 .reg_offset = 0,
282 [RK808_IRQ_HOTDIE] = {
283 .mask = RK808_IRQ_HOTDIE_MSK,
284 .reg_offset = 0,
286 [RK808_IRQ_RTC_ALARM] = {
287 .mask = RK808_IRQ_RTC_ALARM_MSK,
288 .reg_offset = 0,
290 [RK808_IRQ_RTC_PERIOD] = {
291 .mask = RK808_IRQ_RTC_PERIOD_MSK,
292 .reg_offset = 0,
295 /* INT_STS2 */
296 [RK808_IRQ_PLUG_IN_INT] = {
297 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
298 .reg_offset = 1,
300 [RK808_IRQ_PLUG_OUT_INT] = {
301 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
302 .reg_offset = 1,
306 static const struct regmap_irq rk818_irqs[] = {
307 /* INT_STS */
308 [RK818_IRQ_VOUT_LO] = {
309 .mask = RK818_IRQ_VOUT_LO_MSK,
310 .reg_offset = 0,
312 [RK818_IRQ_VB_LO] = {
313 .mask = RK818_IRQ_VB_LO_MSK,
314 .reg_offset = 0,
316 [RK818_IRQ_PWRON] = {
317 .mask = RK818_IRQ_PWRON_MSK,
318 .reg_offset = 0,
320 [RK818_IRQ_PWRON_LP] = {
321 .mask = RK818_IRQ_PWRON_LP_MSK,
322 .reg_offset = 0,
324 [RK818_IRQ_HOTDIE] = {
325 .mask = RK818_IRQ_HOTDIE_MSK,
326 .reg_offset = 0,
328 [RK818_IRQ_RTC_ALARM] = {
329 .mask = RK818_IRQ_RTC_ALARM_MSK,
330 .reg_offset = 0,
332 [RK818_IRQ_RTC_PERIOD] = {
333 .mask = RK818_IRQ_RTC_PERIOD_MSK,
334 .reg_offset = 0,
336 [RK818_IRQ_USB_OV] = {
337 .mask = RK818_IRQ_USB_OV_MSK,
338 .reg_offset = 0,
341 /* INT_STS2 */
342 [RK818_IRQ_PLUG_IN] = {
343 .mask = RK818_IRQ_PLUG_IN_MSK,
344 .reg_offset = 1,
346 [RK818_IRQ_PLUG_OUT] = {
347 .mask = RK818_IRQ_PLUG_OUT_MSK,
348 .reg_offset = 1,
350 [RK818_IRQ_CHG_OK] = {
351 .mask = RK818_IRQ_CHG_OK_MSK,
352 .reg_offset = 1,
354 [RK818_IRQ_CHG_TE] = {
355 .mask = RK818_IRQ_CHG_TE_MSK,
356 .reg_offset = 1,
358 [RK818_IRQ_CHG_TS1] = {
359 .mask = RK818_IRQ_CHG_TS1_MSK,
360 .reg_offset = 1,
362 [RK818_IRQ_TS2] = {
363 .mask = RK818_IRQ_TS2_MSK,
364 .reg_offset = 1,
366 [RK818_IRQ_CHG_CVTLIM] = {
367 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
368 .reg_offset = 1,
370 [RK818_IRQ_DISCHG_ILIM] = {
371 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
372 .reg_offset = 1,
376 static const struct regmap_irq rk817_irqs[RK817_IRQ_END] = {
377 REGMAP_IRQ_REG_LINE(0, 8),
378 REGMAP_IRQ_REG_LINE(1, 8),
379 REGMAP_IRQ_REG_LINE(2, 8),
380 REGMAP_IRQ_REG_LINE(3, 8),
381 REGMAP_IRQ_REG_LINE(4, 8),
382 REGMAP_IRQ_REG_LINE(5, 8),
383 REGMAP_IRQ_REG_LINE(6, 8),
384 REGMAP_IRQ_REG_LINE(7, 8),
385 REGMAP_IRQ_REG_LINE(8, 8),
386 REGMAP_IRQ_REG_LINE(9, 8),
387 REGMAP_IRQ_REG_LINE(10, 8),
388 REGMAP_IRQ_REG_LINE(11, 8),
389 REGMAP_IRQ_REG_LINE(12, 8),
390 REGMAP_IRQ_REG_LINE(13, 8),
391 REGMAP_IRQ_REG_LINE(14, 8),
392 REGMAP_IRQ_REG_LINE(15, 8),
393 REGMAP_IRQ_REG_LINE(16, 8),
394 REGMAP_IRQ_REG_LINE(17, 8),
395 REGMAP_IRQ_REG_LINE(18, 8),
396 REGMAP_IRQ_REG_LINE(19, 8),
397 REGMAP_IRQ_REG_LINE(20, 8),
398 REGMAP_IRQ_REG_LINE(21, 8),
399 REGMAP_IRQ_REG_LINE(22, 8),
400 REGMAP_IRQ_REG_LINE(23, 8)
403 static struct regmap_irq_chip rk805_irq_chip = {
404 .name = "rk805",
405 .irqs = rk805_irqs,
406 .num_irqs = ARRAY_SIZE(rk805_irqs),
407 .num_regs = 1,
408 .status_base = RK805_INT_STS_REG,
409 .mask_base = RK805_INT_STS_MSK_REG,
410 .ack_base = RK805_INT_STS_REG,
411 .init_ack_masked = true,
414 static const struct regmap_irq_chip rk808_irq_chip = {
415 .name = "rk808",
416 .irqs = rk808_irqs,
417 .num_irqs = ARRAY_SIZE(rk808_irqs),
418 .num_regs = 2,
419 .irq_reg_stride = 2,
420 .status_base = RK808_INT_STS_REG1,
421 .mask_base = RK808_INT_STS_MSK_REG1,
422 .ack_base = RK808_INT_STS_REG1,
423 .init_ack_masked = true,
426 static struct regmap_irq_chip rk817_irq_chip = {
427 .name = "rk817",
428 .irqs = rk817_irqs,
429 .num_irqs = ARRAY_SIZE(rk817_irqs),
430 .num_regs = 3,
431 .irq_reg_stride = 2,
432 .status_base = RK817_INT_STS_REG0,
433 .mask_base = RK817_INT_STS_MSK_REG0,
434 .ack_base = RK817_INT_STS_REG0,
435 .init_ack_masked = true,
438 static const struct regmap_irq_chip rk818_irq_chip = {
439 .name = "rk818",
440 .irqs = rk818_irqs,
441 .num_irqs = ARRAY_SIZE(rk818_irqs),
442 .num_regs = 2,
443 .irq_reg_stride = 2,
444 .status_base = RK818_INT_STS_REG1,
445 .mask_base = RK818_INT_STS_MSK_REG1,
446 .ack_base = RK818_INT_STS_REG1,
447 .init_ack_masked = true,
450 static struct i2c_client *rk808_i2c_client;
452 static void rk805_device_shutdown(void)
454 int ret;
455 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
457 if (!rk808)
458 return;
460 ret = regmap_update_bits(rk808->regmap,
461 RK805_DEV_CTRL_REG,
462 DEV_OFF, DEV_OFF);
463 if (ret)
464 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
467 static void rk805_device_shutdown_prepare(void)
469 int ret;
470 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
472 if (!rk808)
473 return;
475 ret = regmap_update_bits(rk808->regmap,
476 RK805_GPIO_IO_POL_REG,
477 SLP_SD_MSK, SHUTDOWN_FUN);
478 if (ret)
479 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
482 static void rk808_device_shutdown(void)
484 int ret;
485 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
487 if (!rk808)
488 return;
490 ret = regmap_update_bits(rk808->regmap,
491 RK808_DEVCTRL_REG,
492 DEV_OFF_RST, DEV_OFF_RST);
493 if (ret)
494 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
497 static void rk818_device_shutdown(void)
499 int ret;
500 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
502 if (!rk808)
503 return;
505 ret = regmap_update_bits(rk808->regmap,
506 RK818_DEVCTRL_REG,
507 DEV_OFF, DEV_OFF);
508 if (ret)
509 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
512 static void rk8xx_syscore_shutdown(void)
514 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
515 int ret;
517 if (system_state == SYSTEM_POWER_OFF &&
518 (rk808->variant == RK809_ID || rk808->variant == RK817_ID)) {
519 ret = regmap_update_bits(rk808->regmap,
520 RK817_SYS_CFG(3),
521 RK817_SLPPIN_FUNC_MSK,
522 SLPPIN_DN_FUN);
523 if (ret) {
524 dev_warn(&rk808_i2c_client->dev,
525 "Cannot switch to power down function\n");
530 static struct syscore_ops rk808_syscore_ops = {
531 .shutdown = rk8xx_syscore_shutdown,
534 static const struct of_device_id rk808_of_match[] = {
535 { .compatible = "rockchip,rk805" },
536 { .compatible = "rockchip,rk808" },
537 { .compatible = "rockchip,rk809" },
538 { .compatible = "rockchip,rk817" },
539 { .compatible = "rockchip,rk818" },
540 { },
542 MODULE_DEVICE_TABLE(of, rk808_of_match);
544 static int rk808_probe(struct i2c_client *client,
545 const struct i2c_device_id *id)
547 struct device_node *np = client->dev.of_node;
548 struct rk808 *rk808;
549 const struct rk808_reg_data *pre_init_reg;
550 const struct mfd_cell *cells;
551 int nr_pre_init_regs;
552 int nr_cells;
553 int pm_off = 0, msb, lsb;
554 unsigned char pmic_id_msb, pmic_id_lsb;
555 int ret;
556 int i;
558 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
559 if (!rk808)
560 return -ENOMEM;
562 if (of_device_is_compatible(np, "rockchip,rk817") ||
563 of_device_is_compatible(np, "rockchip,rk809")) {
564 pmic_id_msb = RK817_ID_MSB;
565 pmic_id_lsb = RK817_ID_LSB;
566 } else {
567 pmic_id_msb = RK808_ID_MSB;
568 pmic_id_lsb = RK808_ID_LSB;
571 /* Read chip variant */
572 msb = i2c_smbus_read_byte_data(client, pmic_id_msb);
573 if (msb < 0) {
574 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
575 RK808_ID_MSB);
576 return msb;
579 lsb = i2c_smbus_read_byte_data(client, pmic_id_lsb);
580 if (lsb < 0) {
581 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
582 RK808_ID_LSB);
583 return lsb;
586 rk808->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
587 dev_info(&client->dev, "chip id: 0x%x\n", (unsigned int)rk808->variant);
589 switch (rk808->variant) {
590 case RK805_ID:
591 rk808->regmap_cfg = &rk805_regmap_config;
592 rk808->regmap_irq_chip = &rk805_irq_chip;
593 pre_init_reg = rk805_pre_init_reg;
594 nr_pre_init_regs = ARRAY_SIZE(rk805_pre_init_reg);
595 cells = rk805s;
596 nr_cells = ARRAY_SIZE(rk805s);
597 rk808->pm_pwroff_fn = rk805_device_shutdown;
598 rk808->pm_pwroff_prep_fn = rk805_device_shutdown_prepare;
599 break;
600 case RK808_ID:
601 rk808->regmap_cfg = &rk808_regmap_config;
602 rk808->regmap_irq_chip = &rk808_irq_chip;
603 pre_init_reg = rk808_pre_init_reg;
604 nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
605 cells = rk808s;
606 nr_cells = ARRAY_SIZE(rk808s);
607 rk808->pm_pwroff_fn = rk808_device_shutdown;
608 break;
609 case RK818_ID:
610 rk808->regmap_cfg = &rk818_regmap_config;
611 rk808->regmap_irq_chip = &rk818_irq_chip;
612 pre_init_reg = rk818_pre_init_reg;
613 nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
614 cells = rk818s;
615 nr_cells = ARRAY_SIZE(rk818s);
616 rk808->pm_pwroff_fn = rk818_device_shutdown;
617 break;
618 case RK809_ID:
619 case RK817_ID:
620 rk808->regmap_cfg = &rk817_regmap_config;
621 rk808->regmap_irq_chip = &rk817_irq_chip;
622 pre_init_reg = rk817_pre_init_reg;
623 nr_pre_init_regs = ARRAY_SIZE(rk817_pre_init_reg);
624 cells = rk817s;
625 nr_cells = ARRAY_SIZE(rk817s);
626 register_syscore_ops(&rk808_syscore_ops);
627 break;
628 default:
629 dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
630 rk808->variant);
631 return -EINVAL;
634 rk808->i2c = client;
635 i2c_set_clientdata(client, rk808);
637 rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
638 if (IS_ERR(rk808->regmap)) {
639 dev_err(&client->dev, "regmap initialization failed\n");
640 return PTR_ERR(rk808->regmap);
643 if (!client->irq) {
644 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
645 return -EINVAL;
648 ret = regmap_add_irq_chip(rk808->regmap, client->irq,
649 IRQF_ONESHOT, -1,
650 rk808->regmap_irq_chip, &rk808->irq_data);
651 if (ret) {
652 dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
653 return ret;
656 for (i = 0; i < nr_pre_init_regs; i++) {
657 ret = regmap_update_bits(rk808->regmap,
658 pre_init_reg[i].addr,
659 pre_init_reg[i].mask,
660 pre_init_reg[i].value);
661 if (ret) {
662 dev_err(&client->dev,
663 "0x%x write err\n",
664 pre_init_reg[i].addr);
665 return ret;
669 ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
670 cells, nr_cells, NULL, 0,
671 regmap_irq_get_domain(rk808->irq_data));
672 if (ret) {
673 dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
674 goto err_irq;
677 pm_off = of_property_read_bool(np,
678 "rockchip,system-power-controller");
679 if (pm_off && !pm_power_off) {
680 rk808_i2c_client = client;
681 pm_power_off = rk808->pm_pwroff_fn;
684 if (pm_off && !pm_power_off_prepare) {
685 if (!rk808_i2c_client)
686 rk808_i2c_client = client;
687 pm_power_off_prepare = rk808->pm_pwroff_prep_fn;
690 return 0;
692 err_irq:
693 regmap_del_irq_chip(client->irq, rk808->irq_data);
694 return ret;
697 static int rk808_remove(struct i2c_client *client)
699 struct rk808 *rk808 = i2c_get_clientdata(client);
701 regmap_del_irq_chip(client->irq, rk808->irq_data);
704 * pm_power_off may points to a function from another module.
705 * Check if the pointer is set by us and only then overwrite it.
707 if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn)
708 pm_power_off = NULL;
711 * As above, check if the pointer is set by us before overwrite.
713 if (rk808->pm_pwroff_prep_fn &&
714 pm_power_off_prepare == rk808->pm_pwroff_prep_fn)
715 pm_power_off_prepare = NULL;
717 return 0;
720 static int __maybe_unused rk8xx_suspend(struct device *dev)
722 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
723 int ret = 0;
725 switch (rk808->variant) {
726 case RK809_ID:
727 case RK817_ID:
728 ret = regmap_update_bits(rk808->regmap,
729 RK817_SYS_CFG(3),
730 RK817_SLPPIN_FUNC_MSK,
731 SLPPIN_SLP_FUN);
732 break;
733 default:
734 break;
737 return ret;
740 static int __maybe_unused rk8xx_resume(struct device *dev)
742 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
743 int ret = 0;
745 switch (rk808->variant) {
746 case RK809_ID:
747 case RK817_ID:
748 ret = regmap_update_bits(rk808->regmap,
749 RK817_SYS_CFG(3),
750 RK817_SLPPIN_FUNC_MSK,
751 SLPPIN_NULL_FUN);
752 break;
753 default:
754 break;
757 return ret;
759 static SIMPLE_DEV_PM_OPS(rk8xx_pm_ops, rk8xx_suspend, rk8xx_resume);
761 static struct i2c_driver rk808_i2c_driver = {
762 .driver = {
763 .name = "rk808",
764 .of_match_table = rk808_of_match,
765 .pm = &rk8xx_pm_ops,
767 .probe = rk808_probe,
768 .remove = rk808_remove,
771 module_i2c_driver(rk808_i2c_driver);
773 MODULE_LICENSE("GPL");
774 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
775 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
776 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
777 MODULE_DESCRIPTION("RK808/RK818 PMIC driver");