blob: e950f8bf422d58305e3ab491cb97e414b7f8cb0c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Base driver for ASR PM803
3 *
4 * Copyright 2018 ASR Microelectronics (Shanghai) Co., Ltd.
5 *
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of this
8 * archive for more details.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/mfd/core.h>
24#include <linux/mfd/88pm80x.h>
25#include <linux/mfd/pm803.h>
26#include <linux/slab.h>
27#include <linux/of_device.h>
28#include <linux/reboot.h>
29#include <soc/asr/addr-map.h>
30#include <linux/cputype.h>
31
32/* Interrupt Registers */
33#define PM803_INT_STATUS1 (0x05)
34#define PM803_ONKEY_INT_STS1 (1 << 0)
35#define PM803_EXTON1_INT_STS1 (1 << 1)
36#define PM803_RTC_INT_STS1 (1 << 2)
37#define PM803_BAT_INT_STS1 (1 << 3)
38
39#define PM803_INT_ENA_1 (0x09)
40#define PM803_ONKEY_INT_ENA1 (1 << 0)
41#define PM803_EXTON_INT_ENA1 (1 << 1)
42#define PM803_RTC_INT_ENA1 (1 << 2)
43#define PM803_BAT_INT_ENA1 (1 << 3)
44
45/* number of INT_ENA & INT_STATUS regs */
46#define PM803_INT_REG_NUM (1)
47#define PM803_BUCK1_REG_15_8 (0x24)
48
49/* Interrupt Number in 88PM803 */
50enum {
51 PM803_IRQ_ONKEY, /*EN1b0 *//*0 */
52 PM803_IRQ_EXTON1, /*EN1b1 */
53 PM803_IRQ_BAT, /*EN1b3 */
54 PM803_IRQ_RTC, /*EN1b4 */
55 PM803_MAX_IRQ,
56};
57
58#define SCS_SYNC_RTC_CNRL (0x1 << 0 | 0x1 << 4)
59#define SCS_CLEAR_SYNC_DONE (0x1 << 0 | 0x1 << 6)
60#define SCR_RTC_SYNC_UDELAY (130)
61#define SCS_RTC_ACTIVE_LOW (0x1 << 2)
62#define SCS_RTC_CNTL (0x0c)
63#define SCS_RTC_SYNC_CFG (0x14)
64#define SCS_DCS_MODE (0x1c)
65
66#define SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x03E000)
67#define ASR1903_SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x0C0000)
68
69/* PM803: generation identification number */
70#define PM803_CHIP_GEN_ID_NUM 0x3
71extern struct pm80x_chip *pm80x_chip_g;
72
73static const struct i2c_device_id pm80x_id_table[] = {
74 {"pm803", 0},
75 {} /* NULL terminated */
76};
77MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
78
79static const struct of_device_id pm80x_dt_ids[] = {
80 { .compatible = "asr,pm803", },
81 {},
82};
83MODULE_DEVICE_TABLE(of, pm80x_dt_ids);
84
85static struct resource rtc_resources[] = {
86 {
87 .name = "scs-rtc",
88 .start = PM803_IRQ_RTC,
89 .end = PM803_IRQ_RTC,
90 .flags = IORESOURCE_IRQ,
91 },
92};
93
94static struct mfd_cell rtc_devs[] = {
95 {
96 .name = "scs-rtc",
97 .of_compatible = "asr,scs-rtc",
98 .num_resources = ARRAY_SIZE(rtc_resources),
99 .resources = &rtc_resources[0],
100 .id = -1,
101 },
102};
103
104static struct resource onkey_resources[] = {
105 {
106 .name = "88pm80x-onkey",
107 .start = PM803_IRQ_ONKEY,
108 .end = PM803_IRQ_ONKEY,
109 .flags = IORESOURCE_IRQ,
110 },
111};
112
113static struct mfd_cell onkey_devs[] = {
114 {
115 .name = "88pm80x-onkey",
116 .of_compatible = "marvell,88pm80x-onkey",
117 .num_resources = 1,
118 .resources = &onkey_resources[0],
119 .id = -1,
120 },
121};
122
123static struct mfd_cell regulator_devs[] = {
124 {
125 .name = "pm803-regulator",
126 .of_compatible = "asr,pm803-regulator",
127 .id = -1,
128 },
129};
130
131static struct resource bat_resources[] = {
132 {
133 .name = "pm803-bat",
134 .start = PM803_IRQ_BAT,
135 .end = PM803_IRQ_BAT,
136 .flags = IORESOURCE_IRQ,
137 },
138};
139
140static struct mfd_cell bat_devs[] = {
141 {
142 .name = "pm803-bat",
143 .of_compatible = "asr,pm803-bat",
144 .num_resources = 1,
145 .resources = &bat_resources[0],
146 .id = -1,
147 },
148};
149
150#ifdef CONFIG_PAPI_88PM80X
151static struct mfd_cell papi_devs[] = {
152 {
153 .name = "88pm80x-papi",
154 .of_compatible = "marvell,88pm80x-papi",
155 .id = -1,
156 },
157};
158#endif
159
160static struct resource usb_resources[] = {
161 {
162 .name = "88pm80x-usb",
163 .start = PM803_IRQ_EXTON1,
164 .end = PM803_IRQ_EXTON1,
165 .flags = IORESOURCE_IRQ,
166 },
167};
168
169static struct mfd_cell usb_devs[] = {
170 {
171 .name = "88pm80x-usb",
172 .of_compatible = "marvell,88pm80x-usb",
173 .num_resources = ARRAY_SIZE(usb_resources),
174 .resources = &usb_resources[0],
175 .id = -1,
176 },
177};
178
179static struct mfd_cell dvc_devs[] = {
180 {
181 .name = "88pm8xx-dvc",
182 .of_compatible = "marvell,88pm8xx-dvc",
183 .id = -1,
184 },
185};
186
187static struct mfd_cell debug_devs[] = {
188 {
189 .name = "88pm80x-debug",
190 .of_compatible = "marvell,88pm80x-debug",
191 .id = -1,
192 },
193};
194
195static struct mfd_cell wdt_devs[] = {
196 {
197 .name = "88pm80x-wdt",
198 .of_compatible = "marvell,88pm80x-wdt",
199 .id = -1,
200 },
201};
202
203static const struct regmap_irq pm803_irqs[] = {
204 /* INT0 */
205 [PM803_IRQ_ONKEY] = {
206 .mask = PM803_ONKEY_INT_ENA1,
207 },
208 [PM803_IRQ_EXTON1] = {
209 .mask = PM803_EXTON_INT_ENA1,
210 },
211 [PM803_IRQ_RTC] = {
212 .mask = PM803_RTC_INT_ENA1,
213 },
214 [PM803_IRQ_BAT] = {
215 .mask = PM803_BAT_INT_ENA1,
216 },
217};
218
219static bool scs_int_active_high;
220static bool is_scs_mode = false;
221
222int pm803_extern_read(int page, int reg)
223{
224 int ret;
225 unsigned int val;
226 struct pm80x_chip *chip = pm80x_chip_g;
227 if (!chip) {
228 pr_err("%s: chip is NULL\n", __func__);
229 return -EINVAL;
230 }
231 switch (page) {
232 case PM803_BASE_PAGE:
233 ret = regmap_read(chip->regmap, reg, &val);
234 break;
235 case PM803_POWER_PAGE:
236 ret = regmap_read(chip->subchip->regmap_power,
237 reg, &val);
238 break;
239 default:
240 ret = -1;
241 break;
242 }
243
244 if (ret < 0) {
245 pr_err("fail to read reg 0x%x\n", reg);
246 return ret;
247 }
248
249 return val;
250}
251EXPORT_SYMBOL(pm803_extern_read);
252
253int pm803_extern_write(int page, int reg, unsigned char val)
254{
255 int ret;
256 struct pm80x_chip *chip = pm80x_chip_g;
257 if (!chip) {
258 pr_err("%s: chip is NULL\n", __func__);
259 return -EINVAL;
260 }
261 switch (page) {
262 case PM803_BASE_PAGE:
263 ret = regmap_write(chip->regmap, reg, val);
264 break;
265 case PM803_POWER_PAGE:
266 ret = regmap_write(chip->subchip->regmap_power,
267 reg, val);
268 break;
269 default:
270 ret = -1;
271 break;
272 }
273 return ret;
274}
275EXPORT_SYMBOL(pm803_extern_write);
276
277int pm803_extern_setbits(int page, int reg,
278 unsigned char mask, unsigned char val)
279{
280 int ret;
281 struct pm80x_chip *chip = pm80x_chip_g;
282 if (!chip) {
283 pr_err("%s: chip is NULL\n", __func__);
284 return -EINVAL;
285 }
286 switch (page) {
287 case PM803_BASE_PAGE:
288 ret = regmap_update_bits(chip->regmap, reg, mask, val);
289 break;
290 case PM803_POWER_PAGE:
291 ret = regmap_update_bits(chip->subchip->regmap_power,
292 reg, mask, val);
293 break;
294 default:
295 ret = -1;
296 break;
297 }
298 return ret;
299}
300EXPORT_SYMBOL(pm803_extern_setbits);
301
302static int __pm803_sw_poweroff(void)
303{
304 int ret = 0;
305
306 /* clear fault wakeup */
307 ret = pmic_rw_reg(PM803_RTC_MISC5, (0x1 << 1), 0x0);
308 if (ret < 0) {
309 pr_err("%s, set PM803_RTC_MISC5 fail\n", __func__);
310 return ret;
311 }
312
313 /* discharge timer should be set greater than or equal to 1s */
314 ret = pmic_rw_reg(PM803_RTC_MISC2, 0xf, 0x1);
315 if (ret < 0) {
316 pr_err("%s, set PM803_RTC_MISC2 fail\n", __func__);
317 return ret;
318 }
319 ret = pmic_rw_reg(PM803_RTC_MISC4, (0x1 << 1), 0x0);
320 if (ret < 0) {
321 pr_err("%s, set PM803_RTC_MISC4 fail\n", __func__);
322 return ret;
323 }
324
325 ret = pmic_rw_reg(PM803_WAKEUP1, PM803_SW_PDOWN, PM803_SW_PDOWN);
326 if (ret < 0)
327 pr_err("%s, turn off power fail\n", __func__);
328
329 return ret;
330}
331
332static void pm803_sw_poweroff(void)
333{
334 int ret;
335
336 /* try 3 times to reduce the fail rate */
337 pr_info("turning off power....\n");
338
339 ret = __pm803_sw_poweroff();
340 if (ret < 0)
341 ret = __pm803_sw_poweroff();
342 if (ret < 0)
343 ret = __pm803_sw_poweroff();
344 if (ret < 0)
345 pr_err("%s, turn off power failed\n", __func__);
346}
347
348static int device_rtc_init(struct pm80x_chip *chip,
349 struct pm80x_platform_data *pdata)
350{
351 int ret;
352
353 rtc_devs[0].platform_data = pdata->rtc;
354 rtc_devs[0].pdata_size =
355 pdata->rtc ? sizeof(struct pm80x_rtc_pdata) : 0;
356 ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
357 ARRAY_SIZE(rtc_devs), NULL,
358 regmap_irq_chip_get_base(chip->irq_data), NULL);
359 if (ret) {
360 dev_err(chip->dev, "Failed to add rtc subdev\n");
361 return ret;
362 }
363
364 return 0;
365}
366
367static int device_onkey_init(struct pm80x_chip *chip,
368 struct pm80x_platform_data *pdata)
369{
370 int ret;
371
372 ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
373 ARRAY_SIZE(onkey_devs), &onkey_resources[0],
374 regmap_irq_chip_get_base(chip->irq_data), NULL);
375
376 if (ret) {
377 dev_err(chip->dev, "Failed to add onkey subdev\n");
378 return ret;
379 }
380
381 return 0;
382}
383
384static int device_regulator_init(struct pm80x_chip *chip,
385 struct pm80x_platform_data *pdata)
386{
387 int ret;
388
389 ret = mfd_add_devices(chip->dev, 0, &regulator_devs[0],
390 ARRAY_SIZE(regulator_devs), NULL, 0, NULL);
391 if (ret) {
392 dev_err(chip->dev, "Failed to add regulator subdev\n");
393 return ret;
394 }
395
396 return 0;
397}
398
399static int device_bat_init(struct pm80x_chip *chip,
400 struct pm80x_platform_data *pdata)
401{
402 int ret;
403
404 ret = mfd_add_devices(chip->dev, 0, &bat_devs[0],
405 ARRAY_SIZE(bat_devs), NULL,
406 regmap_irq_chip_get_base(chip->irq_data), NULL);
407 if (ret) {
408 dev_err(chip->dev, "Failed to add battery subdev\n");
409 return ret;
410 }
411
412 return 0;
413}
414
415#ifdef CONFIG_PAPI_88PM80X
416static int device_papi_init(struct pm80x_chip *chip)
417{
418 int ret;
419
420 ret = mfd_add_devices(chip->dev, 0, &papi_devs[0],
421 ARRAY_SIZE(papi_devs), NULL, 0, NULL);
422 if (ret) {
423 dev_err(chip->dev, "Failed to add papi subdev\n");
424 return ret;
425 }
426
427 return 0;
428}
429#endif
430
431static int device_usb_init(struct pm80x_chip *chip,
432 struct pm80x_platform_data *pdata)
433{
434 int ret;
435
436 usb_devs[0].platform_data = pdata->usb;
437 usb_devs[0].pdata_size = sizeof(struct pm80x_usb_pdata);
438 ret = mfd_add_devices(chip->dev, 0, &usb_devs[0],
439 ARRAY_SIZE(usb_devs), NULL,
440 regmap_irq_chip_get_base(chip->irq_data), NULL);
441 if (ret < 0) {
442 dev_err(chip->dev, "Failed to add usb subdev\n");
443 return ret;
444 }
445
446 return 0;
447}
448
449static int device_dvc_init(struct pm80x_chip *chip,
450 struct pm80x_platform_data *pdata)
451{
452 int ret;
453
454#ifdef CONFIG_CPU_ASR1901
455 return 0;
456#endif
457
458 ret = mfd_add_devices(chip->dev, 0, &dvc_devs[0],
459 ARRAY_SIZE(dvc_devs), NULL, 0, NULL);
460 if (ret) {
461 dev_err(chip->dev, "Failed to add dvc subdev\n");
462 return ret;
463 }
464 return 0;
465}
466
467static int device_debug_init(struct pm80x_chip *chip)
468{
469 return mfd_add_devices(chip->dev, 0, &debug_devs[0],
470 ARRAY_SIZE(debug_devs), NULL, 0, NULL);
471}
472
473static int device_wdt_init(struct pm80x_chip *chip)
474{
475 return mfd_add_devices(chip->dev, 0, &wdt_devs[0],
476 ARRAY_SIZE(wdt_devs), NULL, 0, NULL);
477}
478
479static int device_irq_init_803(struct pm80x_chip *chip)
480{
481 struct regmap *map = chip->regmap;
482 unsigned long flags = IRQF_ONESHOT;
483 int data, mask, ret = -EINVAL;
484
485 if (!map || !chip->irq) {
486 if (!map)
487 pr_info("%s: map == NULL\n", __func__);
488 else
489 pr_info("%s: chip.irq == 0\n", __func__);
490 dev_err(chip->dev, "incorrect parameters\n");
491 return -EINVAL;
492 }
493
494 /*
495 * irq_mode defines the way of clearing interrupt. it's read-clear by
496 * default.
497 */
498 mask =
499 PM803_WAKEUP2_INV_INT | PM803_WAKEUP2_INT_CLEAR |
500 PM803_WAKEUP2_INT_MASK;
501
502 data = PM803_WAKEUP2_INT_CLEAR;
503 ret = regmap_update_bits(map, PM803_WAKEUP2, mask, data);
504
505 if (ret < 0)
506 goto out;
507
508 ret =
509 regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1,
510 chip->regmap_irq_chip, &chip->irq_data);
511
512out:
513 return ret;
514}
515
516static void device_irq_exit_803(struct pm80x_chip *chip)
517{
518 regmap_del_irq_chip(chip->irq, chip->irq_data);
519}
520
521static struct regmap_irq_chip pm803_irq_chip = {
522 .name = "ASRPM803",
523 .irqs = pm803_irqs,
524 .num_irqs = ARRAY_SIZE(pm803_irqs),
525
526 .num_regs = 1,
527 .status_base = PM803_INT_STATUS1,
528 .mask_base = PM803_INT_ENA_1,
529 .ack_base = PM803_INT_STATUS1,
530 .mask_invert = 1,
531};
532
533static int pm803_pages_init(struct pm80x_chip *chip)
534{
535 struct pm80x_subchip *subchip;
536 struct i2c_client *client = chip->client;
537
538 int ret = 0;
539
540 subchip = chip->subchip;
541 if (!subchip || !subchip->power_page_addr)
542 return -ENODEV;
543
544 /* PM803 block power page */
545 subchip->power_page = i2c_new_dummy(client->adapter,
546 subchip->power_page_addr);
547 if (subchip->power_page == NULL) {
548 ret = -ENODEV;
549 goto out;
550 }
551
552 subchip->regmap_power = devm_regmap_init_i2c(subchip->power_page,
553 &pm80x_regmap_config);
554 if (IS_ERR(subchip->regmap_power)) {
555 ret = PTR_ERR(subchip->regmap_power);
556 dev_err(chip->dev,
557 "Failed to allocate regmap_power: %d\n", ret);
558 goto out;
559 }
560
561 i2c_set_clientdata(subchip->power_page, chip);
562out:
563 return ret;
564}
565
566static void pm803_pages_exit(struct pm80x_chip *chip)
567{
568 struct pm80x_subchip *subchip;
569
570 subchip = chip->subchip;
571
572 if (subchip && subchip->power_page)
573 i2c_unregister_device(subchip->power_page);
574}
575
576static int device_803_init(struct pm80x_chip *chip,
577 struct pm80x_platform_data *pdata)
578{
579 int ret;
580 void __iomem *scs_base;
581
582 chip->regmap_irq_chip = &pm803_irq_chip;
583
584 ret = device_irq_init_803(chip);
585 if (ret < 0) {
586 dev_err(chip->dev, "[%s]Failed to init PM803 irq\n", __func__);
587 goto out;
588 }
589
590 ret = device_onkey_init(chip, pdata);
591 if (ret) {
592 dev_err(chip->dev, "Failed to add onkey subdev\n");
593 goto out_dev;
594 }
595
596 if (cpu_is_asr1803() || cpu_is_asr1806() || cpu_is_asr1903()) {
597 if (cpu_is_asr1903())
598 scs_base = ASR1903_SCS_RTC_VIRT_BASE;
599 else
600 scs_base = SCS_RTC_VIRT_BASE;
601
602 is_scs_mode = (!readl(scs_base + SCS_DCS_MODE));
603 pr_info("is_scs_mode: %d, scs_int_active_high: %d\n",
604 is_scs_mode, scs_int_active_high);
605
606 if (scs_int_active_high) {
607 writel(readl(scs_base + SCS_RTC_CNTL) & (~SCS_RTC_ACTIVE_LOW),
608 scs_base + SCS_RTC_CNTL);
609 } else
610 writel(readl(scs_base + SCS_RTC_CNTL) | (SCS_RTC_ACTIVE_LOW),
611 scs_base + SCS_RTC_CNTL);
612 writel(SCS_SYNC_RTC_CNRL, scs_base + SCS_RTC_SYNC_CFG);
613 udelay(SCR_RTC_SYNC_UDELAY);
614 writel(SCS_CLEAR_SYNC_DONE, scs_base + SCS_RTC_SYNC_CFG);
615 }
616
617 ret = device_rtc_init(chip, pdata);
618 if (ret) {
619 dev_err(chip->dev, "Failed to add rtc subdev\n");
620 goto out;
621 }
622
623 ret = device_regulator_init(chip, pdata);
624 if (ret) {
625 dev_err(chip->dev, "Failed to add regulators subdev\n");
626 goto out;
627 }
628
629 ret = device_bat_init(chip, pdata);
630 if (ret) {
631 dev_err(chip->dev, "Failed to add bat subdev\n");
632 goto out;
633 }
634
635 ret = device_dvc_init(chip, pdata);
636 if (ret)
637 dev_warn(chip->dev, "Failed to add dvc subdev\n");
638
639 ret = device_usb_init(chip, pdata);
640 if (ret)
641 dev_warn(chip->dev, "Failed to add usb subdev\n");
642
643 ret = device_debug_init(chip);
644 if (ret) {
645 dev_err(chip->dev, "failed to add debug subdev\n");
646 goto out_dev;
647 }
648
649 ret = device_wdt_init(chip);
650 if (ret) {
651 dev_err(chip->dev, "failed to add wdt subdev\n");
652 goto out_dev;
653 }
654
655#ifdef CONFIG_PAPI_88PM80X
656 ret = device_papi_init(chip);
657 if (ret) {
658 dev_err(chip->dev, "Failed to add papi subdev\n");
659 goto out;
660 }
661#endif
662 return 0;
663out_dev:
664 mfd_remove_devices(chip->dev);
665 device_irq_exit_803(chip);
666out:
667 return ret;
668}
669
670static void parse_powerup_log(struct pm80x_chip *chip)
671{
672 int powerup, bit;
673 char *powerup_name[5] = {
674 "ONKEY_WAKEUP ",
675 "EXTON1_WAKEUP ",
676 "RTC_ALARM_WAKEUP",
677 "BAT_WAKEUP ",
678 "FAULT_WAKEUP ",
679 };
680
681 /*power up log*/
682 regmap_read(chip->regmap, PM803_POWER_UP_LOG, &powerup);
683 dev_info(chip->dev, "Powerup log 0x%x: 0x%x\n",
684 PM803_POWER_UP_LOG, powerup);
685 dev_info(chip->dev, " -------------------------------\n");
686 dev_info(chip->dev, "| name(power up) | status |\n");
687 dev_info(chip->dev, "|--------------------|----------|\n");
688 for (bit = 0; bit < 5; bit++)
689 dev_info(chip->dev, "| %s | %x |\n",
690 powerup_name[bit], (powerup >> bit) & 1);
691 dev_info(chip->dev, " -------------------------------\n");
692
693 /* keep globals for external usage */
694 chip->powerup = powerup;
695}
696
697static void parse_powerdown_log(struct pm80x_chip *chip)
698{
699 int powerdown1, powerdown2, bit;
700
701 char *powerdown1_name[8] = {
702 "OVER_TEMP ",
703 "UV_VSYS1 ",
704 "SW_PDOWN ",
705 "SYSEN_EVENT ",
706 "WD ",
707 "LONG_ONKEY",
708 "OV_VSYS ",
709 "RTC_RESET "
710 };
711 char *powerdown2_name[5] = {
712 "HYB_DONE ",
713 "UV_VSYS2 ",
714 "HW_RESET ",
715 "PGOOD_PDOWN",
716 "LONKEY_RTC "
717 };
718
719 /*power down log1*/
720 regmap_read(chip->regmap, PM803_RTC_SPARE7, &powerdown1);
721 regmap_write(chip->regmap, PM803_RTC_SPARE7, 0x0);
722 dev_info(chip->dev, "PowerDW Log1 0x%x: 0x%x\n",
723 PM803_POWER_DOWN_LOG1, powerdown1);
724 dev_info(chip->dev, " -------------------------------\n");
725 dev_info(chip->dev, "| name(power down1) | status |\n");
726 dev_info(chip->dev, "|--------------------|----------|\n");
727 for (bit = 0; bit < 8; bit++)
728 dev_info(chip->dev, "| %s | %x |\n",
729 powerdown1_name[bit], (powerdown1 >> bit) & 1);
730 dev_info(chip->dev, " -------------------------------\n");
731
732 /*power down log2*/
733 regmap_read(chip->regmap, PM803_RTC_SPARE8, &powerdown2);
734 regmap_write(chip->regmap, PM803_RTC_SPARE8, 0x0);
735 dev_info(chip->dev, "PowerDW Log2 0x%x: 0x%x\n",
736 PM803_POWER_DOWN_LOG2, powerdown2);
737 dev_info(chip->dev, " -------------------------------\n");
738 dev_info(chip->dev, "| name(power down2) | status |\n");
739 dev_info(chip->dev, "|--------------------|----------|\n");
740 for (bit = 0; bit < 5; bit++)
741 dev_info(chip->dev, "| %s | %x |\n",
742 powerdown2_name[bit], (powerdown2 >> bit) & 1);
743 dev_info(chip->dev, " -------------------------------\n");
744
745 /* write to clear */
746 regmap_write(chip->regmap, PM803_POWER_DOWN_LOG1, 0xFF);
747 regmap_write(chip->regmap, PM803_POWER_DOWN_LOG2, 0xFF);
748
749 /* mask reserved bits and sleep indication */
750 powerdown2 &= 0x1E;
751
752 /* keep globals for external usage */
753 chip->powerdown1 = powerdown1;
754 chip->powerdown2 = powerdown2;
755}
756
757static int pm803_init_config(struct pm80x_chip *chip, struct device_node *np)
758{
759 int data;
760 if (!chip || !chip->regmap || !chip->subchip
761 || !chip->subchip->regmap_power) {
762 pr_err("%s:chip is not availiable!\n", __func__);
763 return -EINVAL;
764 }
765
766 /* disable alarm pull down and rtc rtp reload */
767 regmap_read(chip->regmap, PM803_RTC_CONTROL, &data);
768 data |= ((1 << 7) | (0x1 << 6));
769 regmap_write(chip->regmap, PM803_RTC_CONTROL, data);
770
771 /* Enable voltage change in pmic, POWER_HOLD = 1 */
772 regmap_read(chip->regmap, PM803_WAKEUP1, &data);
773 data |= (1 << 7);
774 regmap_write(chip->regmap, PM803_WAKEUP1, data);
775 /*
776 * Enable buck1 sleep mode.
777 */
778 regmap_update_bits(chip->subchip->regmap_power, PM803_BUCK_SLP1,
779 (0x3 << 3), (0x2 << 3));
780
781 /* dump power up log */
782 parse_powerup_log(chip);
783 parse_powerdown_log(chip);
784 return 0;
785}
786
787static int pm803_dt_init(struct device_node *np,
788 struct device *dev,
789 struct pm80x_platform_data *pdata)
790{
791 pdata->irq_mode =
792 !of_property_read_bool(np, "asr,pm803-irq-write-clear");
793 pdata->batt_det =
794 of_property_read_bool(np, "asr,pm803-battery-detection");
795
796 scs_int_active_high =
797 of_property_read_bool(np, "scs-int-active-high");
798 return 0;
799}
800
801static int pm803_probe(struct i2c_client *client,
802 const struct i2c_device_id *id)
803{
804 int ret = 0;
805 struct pm80x_chip *chip;
806 struct pm80x_platform_data *pdata = client->dev.platform_data;
807 struct device_node *node = client->dev.of_node;
808 struct pm80x_subchip *subchip;
809
810 if (IS_ENABLED(CONFIG_OF)) {
811 if (!pdata) {
812 pdata = devm_kzalloc(&client->dev,
813 sizeof(*pdata), GFP_KERNEL);
814 if (!pdata)
815 return -ENOMEM;
816 }
817 ret = pm803_dt_init(node, &client->dev, pdata);
818 if (ret)
819 return ret;
820 } else if (!pdata) {
821 return -EINVAL;
822 }
823
824 /*
825 * RTC in pmic can run even the core is powered off, and user can set
826 * alarm in RTC. When the alarm is time out, the PMIC will power up
827 * the core, and the whole system will boot up. When PMIC driver is
828 * probed, it will read out some register to find out whether this
829 * boot is caused by RTC timeout or not, and it need pass this
830 * information to RTC driver.
831 * So we need rtc platform data to be existed to pass this information.
832 */
833 if (!pdata->rtc) {
834 pdata->rtc = devm_kzalloc(&client->dev,
835 sizeof(*(pdata->rtc)), GFP_KERNEL);
836 if (!pdata->rtc)
837 return -ENOMEM;
838 }
839
840 ret = pm80x_init(client);
841 if (ret) {
842 dev_err(&client->dev, "PM803_init fail\n");
843 goto out_init;
844 }
845
846 chip = i2c_get_clientdata(client);
847
848 if (chip->type != CHIP_PM803) {
849 dev_err(&client->dev, "PM803 not present\n");
850 ret = -ENODEV;
851 goto out_init;
852 }
853
854 /* init subchip for PM803 */
855 subchip =
856 devm_kzalloc(&client->dev, sizeof(struct pm80x_subchip),
857 GFP_KERNEL);
858 if (!subchip) {
859 ret = -ENOMEM;
860 goto err_subchip_alloc;
861 }
862
863 /* PM803 has 2 addtional pages to support power. */
864 subchip->power_page_addr = client->addr + 1;
865 chip->subchip = subchip;
866
867 ret = pm803_pages_init(chip);
868 if (ret) {
869 dev_err(&client->dev, "PM803_pages_init failed!\n");
870 goto err_page_init;
871 }
872
873 ret = device_803_init(chip, pdata);
874 if (ret) {
875 dev_err(chip->dev, "Failed to initialize 88PM803 devices\n");
876 goto err_device_init;
877 }
878
879 if (pdata && pdata->plat_config)
880 pdata->plat_config(chip, pdata);
881
882 pm803_init_config(chip, NULL);
883
884 if (!pm80x_chip_g)
885 pm80x_chip_g = chip;
886 pm_power_off = pm803_sw_poweroff;
887
888 return 0;
889
890err_device_init:
891 pm803_pages_exit(chip);
892err_page_init:
893err_subchip_alloc:
894 pm80x_deinit();
895out_init:
896 return ret;
897}
898
899static int pm803_remove(struct i2c_client *client)
900{
901 struct pm80x_chip *chip = i2c_get_clientdata(client);
902
903 mfd_remove_devices(chip->dev);
904 device_irq_exit_803(chip);
905
906 pm803_pages_exit(chip);
907 pm80x_deinit();
908 pm80x_chip_g = NULL;
909
910 return 0;
911}
912
913void pm803_shutdown(struct i2c_client *client)
914{
915 u32 data;
916 struct pm80x_chip *chip = i2c_get_clientdata(client);
917
918 /* save reboot flag */
919 regmap_read(chip->regmap, PM803_DUMMY_REG0, &data);
920 data &= 0x0f;
921 data |= 0x50;
922 regmap_write(chip->regmap, PM803_DUMMY_REG0, data);
923 pr_info("%s\n", __func__);
924}
925
926static struct i2c_driver pm803_driver = {
927 .driver = {
928 .name = "ASRPM803",
929 .owner = THIS_MODULE,
930 .pm = &pm80x_pm_ops,
931 .of_match_table = of_match_ptr(pm80x_dt_ids),
932 },
933 .probe = pm803_probe,
934 .remove = pm803_remove,
935 .shutdown = pm803_shutdown,
936 .id_table = pm80x_id_table,
937};
938
939static int __init pm803_i2c_init(void)
940{
941 return i2c_add_driver(&pm803_driver);
942}
943subsys_initcall(pm803_i2c_init);
944
945static void __exit pm803_i2c_exit(void)
946{
947 i2c_del_driver(&pm803_driver);
948}
949module_exit(pm803_i2c_exit);
950
951MODULE_DESCRIPTION("PMIC Driver for ASR PM803");
952MODULE_LICENSE("GPL");