1 // SPDX-License-Identifier: GPL-2.0
3 * Power-source driver for Bay Trail Crystal Cove PMIC
5 * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
7 * Based on intel_crystalcove_pwrsrc.c from Android kernel sources, which is:
8 * Copyright (C) 2013 Intel Corporation
11 #include <linux/debugfs.h>
12 #include <linux/mfd/intel_soc_pmic.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
17 #define CRYSTALCOVE_SPWRSRC_REG 0x1E
18 #define CRYSTALCOVE_RESETSRC0_REG 0x20
19 #define CRYSTALCOVE_RESETSRC1_REG 0x21
20 #define CRYSTALCOVE_WAKESRC_REG 0x22
22 struct crc_pwrsrc_data
{
23 struct regmap
*regmap
;
24 struct dentry
*debug_dentry
;
25 unsigned int resetsrc0
;
26 unsigned int resetsrc1
;
30 static const char * const pwrsrc_pwrsrc_info
[] = {
33 /* bit 2 */ "Battery",
37 static const char * const pwrsrc_resetsrc0_info
[] = {
38 /* bit 0 */ "SOC reporting a thermal event",
39 /* bit 1 */ "critical PMIC temperature",
40 /* bit 2 */ "critical system temperature",
41 /* bit 3 */ "critical battery temperature",
42 /* bit 4 */ "VSYS under voltage",
43 /* bit 5 */ "VSYS over voltage",
44 /* bit 6 */ "battery removal",
48 static const char * const pwrsrc_resetsrc1_info
[] = {
49 /* bit 0 */ "VCRIT threshold",
50 /* bit 1 */ "BATID reporting battery removal",
51 /* bit 2 */ "user pressing the power button",
55 static const char * const pwrsrc_wakesrc_info
[] = {
56 /* bit 0 */ "user pressing the power button",
57 /* bit 1 */ "a battery insertion",
58 /* bit 2 */ "a USB charger insertion",
59 /* bit 3 */ "an adapter insertion",
63 static void crc_pwrsrc_log(struct seq_file
*seq
, const char *prefix
,
64 const char * const *info
, unsigned int reg_val
)
68 for (i
= 0; info
[i
]; i
++) {
70 seq_printf(seq
, "%s by %s\n", prefix
, info
[i
]);
74 static int pwrsrc_show(struct seq_file
*seq
, void *unused
)
76 struct crc_pwrsrc_data
*data
= seq
->private;
80 ret
= regmap_read(data
->regmap
, CRYSTALCOVE_SPWRSRC_REG
, ®_val
);
84 crc_pwrsrc_log(seq
, "System powered", pwrsrc_pwrsrc_info
, reg_val
);
88 static int resetsrc_show(struct seq_file
*seq
, void *unused
)
90 struct crc_pwrsrc_data
*data
= seq
->private;
92 crc_pwrsrc_log(seq
, "Last shutdown caused", pwrsrc_resetsrc0_info
, data
->resetsrc0
);
93 crc_pwrsrc_log(seq
, "Last shutdown caused", pwrsrc_resetsrc1_info
, data
->resetsrc1
);
97 static int wakesrc_show(struct seq_file
*seq
, void *unused
)
99 struct crc_pwrsrc_data
*data
= seq
->private;
101 crc_pwrsrc_log(seq
, "Last wake caused", pwrsrc_wakesrc_info
, data
->wakesrc
);
105 DEFINE_SHOW_ATTRIBUTE(pwrsrc
);
106 DEFINE_SHOW_ATTRIBUTE(resetsrc
);
107 DEFINE_SHOW_ATTRIBUTE(wakesrc
);
109 static int crc_pwrsrc_read_and_clear(struct crc_pwrsrc_data
*data
,
110 unsigned int reg
, unsigned int *val
)
114 ret
= regmap_read(data
->regmap
, reg
, val
);
118 return regmap_write(data
->regmap
, reg
, *val
);
121 static int crc_pwrsrc_probe(struct platform_device
*pdev
)
123 struct intel_soc_pmic
*pmic
= dev_get_drvdata(pdev
->dev
.parent
);
124 struct crc_pwrsrc_data
*data
;
127 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
131 data
->regmap
= pmic
->regmap
;
134 * Read + clear resetsrc0/1 and wakesrc now, so that they get
135 * cleared even if the debugfs interface is never used.
137 * Properly clearing the wakesrc is important, leaving bit 0 of it
138 * set turns reboot into poweroff on some tablets.
140 ret
= crc_pwrsrc_read_and_clear(data
, CRYSTALCOVE_RESETSRC0_REG
, &data
->resetsrc0
);
144 ret
= crc_pwrsrc_read_and_clear(data
, CRYSTALCOVE_RESETSRC1_REG
, &data
->resetsrc1
);
148 ret
= crc_pwrsrc_read_and_clear(data
, CRYSTALCOVE_WAKESRC_REG
, &data
->wakesrc
);
152 data
->debug_dentry
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
153 debugfs_create_file("pwrsrc", 0444, data
->debug_dentry
, data
, &pwrsrc_fops
);
154 debugfs_create_file("resetsrc", 0444, data
->debug_dentry
, data
, &resetsrc_fops
);
155 debugfs_create_file("wakesrc", 0444, data
->debug_dentry
, data
, &wakesrc_fops
);
157 platform_set_drvdata(pdev
, data
);
161 static void crc_pwrsrc_remove(struct platform_device
*pdev
)
163 struct crc_pwrsrc_data
*data
= platform_get_drvdata(pdev
);
165 debugfs_remove_recursive(data
->debug_dentry
);
168 static struct platform_driver crc_pwrsrc_driver
= {
169 .probe
= crc_pwrsrc_probe
,
170 .remove
= crc_pwrsrc_remove
,
172 .name
= "crystal_cove_pwrsrc",
175 module_platform_driver(crc_pwrsrc_driver
);
177 MODULE_ALIAS("platform:crystal_cove_pwrsrc");
178 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
179 MODULE_DESCRIPTION("Power-source driver for Bay Trail Crystal Cove PMIC");
180 MODULE_LICENSE("GPL");