xref: /openbmc/linux/drivers/input/misc/axp20x-pek.c (revision e04a088b)
15b6c26a9SCarlo Caione /*
25b6c26a9SCarlo Caione  * axp20x power button driver.
35b6c26a9SCarlo Caione  *
45b6c26a9SCarlo Caione  * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
55b6c26a9SCarlo Caione  *
65b6c26a9SCarlo Caione  * This file is subject to the terms and conditions of the GNU General
75b6c26a9SCarlo Caione  * Public License. See the file "COPYING" in the main directory of this
85b6c26a9SCarlo Caione  * archive for more details.
95b6c26a9SCarlo Caione  *
105b6c26a9SCarlo Caione  * This program is distributed in the hope that it will be useful,
115b6c26a9SCarlo Caione  * but WITHOUT ANY WARRANTY; without even the implied warranty of
125b6c26a9SCarlo Caione  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135b6c26a9SCarlo Caione  * GNU General Public License for more details.
145b6c26a9SCarlo Caione  */
155b6c26a9SCarlo Caione 
169b13a4caSHans de Goede #include <linux/acpi.h>
175b6c26a9SCarlo Caione #include <linux/errno.h>
185b6c26a9SCarlo Caione #include <linux/irq.h>
195b6c26a9SCarlo Caione #include <linux/init.h>
205b6c26a9SCarlo Caione #include <linux/input.h>
215b6c26a9SCarlo Caione #include <linux/interrupt.h>
225b6c26a9SCarlo Caione #include <linux/kernel.h>
235b6c26a9SCarlo Caione #include <linux/mfd/axp20x.h>
245b6c26a9SCarlo Caione #include <linux/module.h>
255ecc1e94SHans de Goede #include <linux/platform_data/x86/soc.h>
265b6c26a9SCarlo Caione #include <linux/platform_device.h>
275b6c26a9SCarlo Caione #include <linux/regmap.h>
285b6c26a9SCarlo Caione #include <linux/slab.h>
295b6c26a9SCarlo Caione 
305b6c26a9SCarlo Caione #define AXP20X_PEK_STARTUP_MASK		(0xc0)
315b6c26a9SCarlo Caione #define AXP20X_PEK_SHUTDOWN_MASK	(0x03)
325b6c26a9SCarlo Caione 
33fbc1b323SQuentin Schulz struct axp20x_info {
34fbc1b323SQuentin Schulz 	const struct axp20x_time *startup_time;
35fbc1b323SQuentin Schulz 	unsigned int startup_mask;
36fbc1b323SQuentin Schulz 	const struct axp20x_time *shutdown_time;
37fbc1b323SQuentin Schulz 	unsigned int shutdown_mask;
38fbc1b323SQuentin Schulz };
39fbc1b323SQuentin Schulz 
405b6c26a9SCarlo Caione struct axp20x_pek {
415b6c26a9SCarlo Caione 	struct axp20x_dev *axp20x;
425b6c26a9SCarlo Caione 	struct input_dev *input;
43fbc1b323SQuentin Schulz 	struct axp20x_info *info;
445b6c26a9SCarlo Caione 	int irq_dbr;
455b6c26a9SCarlo Caione 	int irq_dbf;
465b6c26a9SCarlo Caione };
475b6c26a9SCarlo Caione 
485b6c26a9SCarlo Caione struct axp20x_time {
495b6c26a9SCarlo Caione 	unsigned int time;
505b6c26a9SCarlo Caione 	unsigned int idx;
515b6c26a9SCarlo Caione };
525b6c26a9SCarlo Caione 
535b6c26a9SCarlo Caione static const struct axp20x_time startup_time[] = {
545b6c26a9SCarlo Caione 	{ .time = 128,  .idx = 0 },
555b6c26a9SCarlo Caione 	{ .time = 1000, .idx = 2 },
565b6c26a9SCarlo Caione 	{ .time = 3000, .idx = 1 },
575b6c26a9SCarlo Caione 	{ .time = 2000, .idx = 3 },
585b6c26a9SCarlo Caione };
595b6c26a9SCarlo Caione 
60c3cc9447SQuentin Schulz static const struct axp20x_time axp221_startup_time[] = {
61c3cc9447SQuentin Schulz 	{ .time = 128,  .idx = 0 },
62c3cc9447SQuentin Schulz 	{ .time = 1000, .idx = 1 },
63c3cc9447SQuentin Schulz 	{ .time = 2000, .idx = 2 },
64c3cc9447SQuentin Schulz 	{ .time = 3000, .idx = 3 },
65c3cc9447SQuentin Schulz };
66c3cc9447SQuentin Schulz 
675b6c26a9SCarlo Caione static const struct axp20x_time shutdown_time[] = {
685b6c26a9SCarlo Caione 	{ .time = 4000,  .idx = 0 },
695b6c26a9SCarlo Caione 	{ .time = 6000,  .idx = 1 },
705b6c26a9SCarlo Caione 	{ .time = 8000,  .idx = 2 },
715b6c26a9SCarlo Caione 	{ .time = 10000, .idx = 3 },
725b6c26a9SCarlo Caione };
735b6c26a9SCarlo Caione 
74fbc1b323SQuentin Schulz static const struct axp20x_info axp20x_info = {
75fbc1b323SQuentin Schulz 	.startup_time = startup_time,
76fbc1b323SQuentin Schulz 	.startup_mask = AXP20X_PEK_STARTUP_MASK,
77fbc1b323SQuentin Schulz 	.shutdown_time = shutdown_time,
78fbc1b323SQuentin Schulz 	.shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
795b6c26a9SCarlo Caione };
805b6c26a9SCarlo Caione 
81c3cc9447SQuentin Schulz static const struct axp20x_info axp221_info = {
82c3cc9447SQuentin Schulz 	.startup_time = axp221_startup_time,
83c3cc9447SQuentin Schulz 	.startup_mask = AXP20X_PEK_STARTUP_MASK,
84c3cc9447SQuentin Schulz 	.shutdown_time = shutdown_time,
85c3cc9447SQuentin Schulz 	.shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
86c3cc9447SQuentin Schulz };
87c3cc9447SQuentin Schulz 
axp20x_show_attr(struct device * dev,const struct axp20x_time * time,unsigned int mask,char * buf)88fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr(struct device *dev,
89fbc1b323SQuentin Schulz 				const struct axp20x_time *time,
90fbc1b323SQuentin Schulz 				unsigned int mask, char *buf)
915b6c26a9SCarlo Caione {
925b6c26a9SCarlo Caione 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
935b6c26a9SCarlo Caione 	unsigned int val;
945b6c26a9SCarlo Caione 	int ret, i;
955b6c26a9SCarlo Caione 
965b6c26a9SCarlo Caione 	ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
975b6c26a9SCarlo Caione 	if (ret != 0)
985b6c26a9SCarlo Caione 		return ret;
995b6c26a9SCarlo Caione 
100fbc1b323SQuentin Schulz 	val &= mask;
101fbc1b323SQuentin Schulz 	val >>= ffs(mask) - 1;
1025b6c26a9SCarlo Caione 
1035b6c26a9SCarlo Caione 	for (i = 0; i < 4; i++)
104fbc1b323SQuentin Schulz 		if (val == time[i].idx)
105fbc1b323SQuentin Schulz 			val = time[i].time;
1065b6c26a9SCarlo Caione 
1075b6c26a9SCarlo Caione 	return sprintf(buf, "%u\n", val);
1085b6c26a9SCarlo Caione }
1095b6c26a9SCarlo Caione 
axp20x_show_attr_startup(struct device * dev,struct device_attribute * attr,char * buf)110fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr_startup(struct device *dev,
1115b6c26a9SCarlo Caione 					struct device_attribute *attr,
112fbc1b323SQuentin Schulz 					char *buf)
1135b6c26a9SCarlo Caione {
1145b6c26a9SCarlo Caione 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
115fbc1b323SQuentin Schulz 
116fbc1b323SQuentin Schulz 	return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
117fbc1b323SQuentin Schulz 				axp20x_pek->info->startup_mask, buf);
118fbc1b323SQuentin Schulz }
119fbc1b323SQuentin Schulz 
axp20x_show_attr_shutdown(struct device * dev,struct device_attribute * attr,char * buf)120fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr_shutdown(struct device *dev,
121fbc1b323SQuentin Schulz 					 struct device_attribute *attr,
122fbc1b323SQuentin Schulz 					 char *buf)
123fbc1b323SQuentin Schulz {
124fbc1b323SQuentin Schulz 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
125fbc1b323SQuentin Schulz 
126fbc1b323SQuentin Schulz 	return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
127fbc1b323SQuentin Schulz 				axp20x_pek->info->shutdown_mask, buf);
128fbc1b323SQuentin Schulz }
129fbc1b323SQuentin Schulz 
axp20x_store_attr(struct device * dev,const struct axp20x_time * time,unsigned int mask,const char * buf,size_t count)130fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr(struct device *dev,
131fbc1b323SQuentin Schulz 				 const struct axp20x_time *time,
132fbc1b323SQuentin Schulz 				 unsigned int mask, const char *buf,
133fbc1b323SQuentin Schulz 				 size_t count)
134fbc1b323SQuentin Schulz {
135fbc1b323SQuentin Schulz 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
1365b6c26a9SCarlo Caione 	char val_str[20];
1375b6c26a9SCarlo Caione 	size_t len;
1385b6c26a9SCarlo Caione 	int ret, i;
1395b6c26a9SCarlo Caione 	unsigned int val, idx = 0;
1405b6c26a9SCarlo Caione 	unsigned int best_err = UINT_MAX;
1415b6c26a9SCarlo Caione 
1425b6c26a9SCarlo Caione 	val_str[sizeof(val_str) - 1] = '\0';
1435b6c26a9SCarlo Caione 	strncpy(val_str, buf, sizeof(val_str) - 1);
1445b6c26a9SCarlo Caione 	len = strlen(val_str);
1455b6c26a9SCarlo Caione 
1465b6c26a9SCarlo Caione 	if (len && val_str[len - 1] == '\n')
1475b6c26a9SCarlo Caione 		val_str[len - 1] = '\0';
1485b6c26a9SCarlo Caione 
1495b6c26a9SCarlo Caione 	ret = kstrtouint(val_str, 10, &val);
1505b6c26a9SCarlo Caione 	if (ret)
1515b6c26a9SCarlo Caione 		return ret;
1525b6c26a9SCarlo Caione 
1535b6c26a9SCarlo Caione 	for (i = 3; i >= 0; i--) {
1545b6c26a9SCarlo Caione 		unsigned int err;
1555b6c26a9SCarlo Caione 
156fbc1b323SQuentin Schulz 		err = abs(time[i].time - val);
1575b6c26a9SCarlo Caione 		if (err < best_err) {
1585b6c26a9SCarlo Caione 			best_err = err;
159fbc1b323SQuentin Schulz 			idx = time[i].idx;
1605b6c26a9SCarlo Caione 		}
1615b6c26a9SCarlo Caione 
1625b6c26a9SCarlo Caione 		if (!err)
1635b6c26a9SCarlo Caione 			break;
1645b6c26a9SCarlo Caione 	}
1655b6c26a9SCarlo Caione 
166fbc1b323SQuentin Schulz 	idx <<= ffs(mask) - 1;
167fbc1b323SQuentin Schulz 	ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
168fbc1b323SQuentin Schulz 				 mask, idx);
1695b6c26a9SCarlo Caione 	if (ret != 0)
1705b6c26a9SCarlo Caione 		return -EINVAL;
171b388de88SDmitry Torokhov 
1725b6c26a9SCarlo Caione 	return count;
1735b6c26a9SCarlo Caione }
1745b6c26a9SCarlo Caione 
axp20x_store_attr_startup(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)175fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr_startup(struct device *dev,
176fbc1b323SQuentin Schulz 					 struct device_attribute *attr,
177fbc1b323SQuentin Schulz 					 const char *buf, size_t count)
178fbc1b323SQuentin Schulz {
179fbc1b323SQuentin Schulz 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
1805b6c26a9SCarlo Caione 
181fbc1b323SQuentin Schulz 	return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
182fbc1b323SQuentin Schulz 				 axp20x_pek->info->startup_mask, buf, count);
183fbc1b323SQuentin Schulz }
184fbc1b323SQuentin Schulz 
axp20x_store_attr_shutdown(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)185fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr_shutdown(struct device *dev,
186fbc1b323SQuentin Schulz 					  struct device_attribute *attr,
187fbc1b323SQuentin Schulz 					  const char *buf, size_t count)
188fbc1b323SQuentin Schulz {
189fbc1b323SQuentin Schulz 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
190fbc1b323SQuentin Schulz 
191fbc1b323SQuentin Schulz 	return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
192fbc1b323SQuentin Schulz 				 axp20x_pek->info->shutdown_mask, buf, count);
193fbc1b323SQuentin Schulz }
194fbc1b323SQuentin Schulz 
195cbe821a2SBen Dooks (Codethink) static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
196cbe821a2SBen Dooks (Codethink) 		   axp20x_store_attr_startup);
197cbe821a2SBen Dooks (Codethink) static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
198fbc1b323SQuentin Schulz 		   axp20x_store_attr_shutdown);
199b388de88SDmitry Torokhov 
200d99995a4SGreg Kroah-Hartman static struct attribute *axp20x_attrs[] = {
201fbc1b323SQuentin Schulz 	&dev_attr_startup.attr,
202fbc1b323SQuentin Schulz 	&dev_attr_shutdown.attr,
203b388de88SDmitry Torokhov 	NULL,
204b388de88SDmitry Torokhov };
205d99995a4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(axp20x);
2065b6c26a9SCarlo Caione 
axp20x_pek_irq(int irq,void * pwr)2075b6c26a9SCarlo Caione static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
2085b6c26a9SCarlo Caione {
2098a78050eSHans de Goede 	struct input_dev *idev = pwr;
2108a78050eSHans de Goede 	struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
2115b6c26a9SCarlo Caione 
212eeeee40fSHans de Goede 	/*
213eeeee40fSHans de Goede 	 * The power-button is connected to ground so a falling edge (dbf)
214eeeee40fSHans de Goede 	 * means it is pressed.
215eeeee40fSHans de Goede 	 */
216eeeee40fSHans de Goede 	if (irq == axp20x_pek->irq_dbf)
2175b6c26a9SCarlo Caione 		input_report_key(idev, KEY_POWER, true);
218eeeee40fSHans de Goede 	else if (irq == axp20x_pek->irq_dbr)
2195b6c26a9SCarlo Caione 		input_report_key(idev, KEY_POWER, false);
2205b6c26a9SCarlo Caione 
2215b6c26a9SCarlo Caione 	input_sync(idev);
2225b6c26a9SCarlo Caione 
2235b6c26a9SCarlo Caione 	return IRQ_HANDLED;
2245b6c26a9SCarlo Caione }
2255b6c26a9SCarlo Caione 
axp20x_pek_probe_input_device(struct axp20x_pek * axp20x_pek,struct platform_device * pdev)226f2bd5a9eSHans de Goede static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
227f2bd5a9eSHans de Goede 					 struct platform_device *pdev)
2285b6c26a9SCarlo Caione {
2298a78050eSHans de Goede 	struct axp20x_dev *axp20x = axp20x_pek->axp20x;
2305b6c26a9SCarlo Caione 	struct input_dev *idev;
2315b6c26a9SCarlo Caione 	int error;
2325b6c26a9SCarlo Caione 
2338a78050eSHans de Goede 	axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
2348a78050eSHans de Goede 	if (axp20x_pek->irq_dbr < 0)
2358a78050eSHans de Goede 		return axp20x_pek->irq_dbr;
2368a78050eSHans de Goede 	axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
2378a78050eSHans de Goede 						  axp20x_pek->irq_dbr);
2388a78050eSHans de Goede 
2398a78050eSHans de Goede 	axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
2408a78050eSHans de Goede 	if (axp20x_pek->irq_dbf < 0)
2418a78050eSHans de Goede 		return axp20x_pek->irq_dbf;
2428a78050eSHans de Goede 	axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
2438a78050eSHans de Goede 						  axp20x_pek->irq_dbf);
2448a78050eSHans de Goede 
2455b6c26a9SCarlo Caione 	axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
2465b6c26a9SCarlo Caione 	if (!axp20x_pek->input)
2475b6c26a9SCarlo Caione 		return -ENOMEM;
2485b6c26a9SCarlo Caione 
2495b6c26a9SCarlo Caione 	idev = axp20x_pek->input;
2505b6c26a9SCarlo Caione 
2515b6c26a9SCarlo Caione 	idev->name = "axp20x-pek";
2525b6c26a9SCarlo Caione 	idev->phys = "m1kbd/input2";
2535b6c26a9SCarlo Caione 	idev->dev.parent = &pdev->dev;
2545b6c26a9SCarlo Caione 
2555b6c26a9SCarlo Caione 	input_set_capability(idev, EV_KEY, KEY_POWER);
2565b6c26a9SCarlo Caione 
2575b6c26a9SCarlo Caione 	input_set_drvdata(idev, axp20x_pek);
2585b6c26a9SCarlo Caione 
2598a78050eSHans de Goede 	error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
2608a78050eSHans de Goede 					     axp20x_pek_irq, 0,
2618a78050eSHans de Goede 					     "axp20x-pek-dbr", idev);
2628a78050eSHans de Goede 	if (error < 0) {
2638a78050eSHans de Goede 		dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
2648a78050eSHans de Goede 			axp20x_pek->irq_dbr, error);
2658a78050eSHans de Goede 		return error;
2668a78050eSHans de Goede 	}
2678a78050eSHans de Goede 
2688a78050eSHans de Goede 	error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
2698a78050eSHans de Goede 					  axp20x_pek_irq, 0,
2708a78050eSHans de Goede 					  "axp20x-pek-dbf", idev);
2718a78050eSHans de Goede 	if (error < 0) {
2728a78050eSHans de Goede 		dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
2738a78050eSHans de Goede 			axp20x_pek->irq_dbf, error);
2748a78050eSHans de Goede 		return error;
2758a78050eSHans de Goede 	}
2768a78050eSHans de Goede 
277f2bd5a9eSHans de Goede 	error = input_register_device(idev);
278f2bd5a9eSHans de Goede 	if (error) {
279f2bd5a9eSHans de Goede 		dev_err(&pdev->dev, "Can't register input device: %d\n",
280f2bd5a9eSHans de Goede 			error);
281f2bd5a9eSHans de Goede 		return error;
282f2bd5a9eSHans de Goede 	}
283f2bd5a9eSHans de Goede 
2848a78050eSHans de Goede 	device_init_wakeup(&pdev->dev, true);
2858a78050eSHans de Goede 
286f2bd5a9eSHans de Goede 	return 0;
287f2bd5a9eSHans de Goede }
288f2bd5a9eSHans de Goede 
axp20x_pek_should_register_input(struct axp20x_pek * axp20x_pek)2895ecc1e94SHans de Goede static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek)
2908d4b3137SHans de Goede {
2918d4b3137SHans de Goede 	if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
2928d4b3137SHans de Goede 	    axp20x_pek->axp20x->variant == AXP288_ID) {
2938d4b3137SHans de Goede 		/*
2948d4b3137SHans de Goede 		 * On Cherry Trail platforms (hrv == 3), do not register the
2950fd5f221SHans de Goede 		 * input device if there is an "INTCFD9" or "ACPI0011" gpio
2968d4b3137SHans de Goede 		 * button ACPI device, as that handles the power button too,
2978d4b3137SHans de Goede 		 * and otherwise we end up reporting all presses twice.
2988d4b3137SHans de Goede 		 */
2995ecc1e94SHans de Goede 		if (soc_intel_is_cht() &&
3005ecc1e94SHans de Goede 				(acpi_dev_present("INTCFD9", NULL, -1) ||
3010fd5f221SHans de Goede 				 acpi_dev_present("ACPI0011", NULL, -1)))
3028d4b3137SHans de Goede 			return false;
3038d4b3137SHans de Goede 	}
3048d4b3137SHans de Goede 
3058d4b3137SHans de Goede 	return true;
3068d4b3137SHans de Goede }
3078d4b3137SHans de Goede 
axp20x_pek_probe(struct platform_device * pdev)308f2bd5a9eSHans de Goede static int axp20x_pek_probe(struct platform_device *pdev)
309f2bd5a9eSHans de Goede {
310f2bd5a9eSHans de Goede 	struct axp20x_pek *axp20x_pek;
311fbc1b323SQuentin Schulz 	const struct platform_device_id *match = platform_get_device_id(pdev);
312f2bd5a9eSHans de Goede 	int error;
313f2bd5a9eSHans de Goede 
314fbc1b323SQuentin Schulz 	if (!match) {
315fbc1b323SQuentin Schulz 		dev_err(&pdev->dev, "Failed to get platform_device_id\n");
316fbc1b323SQuentin Schulz 		return -EINVAL;
317fbc1b323SQuentin Schulz 	}
318fbc1b323SQuentin Schulz 
319f2bd5a9eSHans de Goede 	axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
320f2bd5a9eSHans de Goede 				  GFP_KERNEL);
321f2bd5a9eSHans de Goede 	if (!axp20x_pek)
322f2bd5a9eSHans de Goede 		return -ENOMEM;
323f2bd5a9eSHans de Goede 
324f2bd5a9eSHans de Goede 	axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
325f2bd5a9eSHans de Goede 
3265ecc1e94SHans de Goede 	if (axp20x_pek_should_register_input(axp20x_pek)) {
327f2bd5a9eSHans de Goede 		error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
328f2bd5a9eSHans de Goede 		if (error)
329f2bd5a9eSHans de Goede 			return error;
3309b13a4caSHans de Goede 	}
331f2bd5a9eSHans de Goede 
332fbc1b323SQuentin Schulz 	axp20x_pek->info = (struct axp20x_info *)match->driver_data;
333fbc1b323SQuentin Schulz 
334b388de88SDmitry Torokhov 	platform_set_drvdata(pdev, axp20x_pek);
3355b6c26a9SCarlo Caione 
3365b6c26a9SCarlo Caione 	return 0;
3375b6c26a9SCarlo Caione }
3385b6c26a9SCarlo Caione 
axp20x_pek_suspend(struct device * dev)339*e04a088bSJonathan Cameron static int axp20x_pek_suspend(struct device *dev)
340fe77f9bbSSamuel Holland {
341fe77f9bbSSamuel Holland 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
342fe77f9bbSSamuel Holland 
343fe77f9bbSSamuel Holland 	/*
344fe77f9bbSSamuel Holland 	 * As nested threaded IRQs are not automatically disabled during
345fe77f9bbSSamuel Holland 	 * suspend, we must explicitly disable non-wakeup IRQs.
346fe77f9bbSSamuel Holland 	 */
347fe77f9bbSSamuel Holland 	if (device_may_wakeup(dev)) {
348fe77f9bbSSamuel Holland 		enable_irq_wake(axp20x_pek->irq_dbf);
349fe77f9bbSSamuel Holland 		enable_irq_wake(axp20x_pek->irq_dbr);
350fe77f9bbSSamuel Holland 	} else {
351fe77f9bbSSamuel Holland 		disable_irq(axp20x_pek->irq_dbf);
352fe77f9bbSSamuel Holland 		disable_irq(axp20x_pek->irq_dbr);
353fe77f9bbSSamuel Holland 	}
354fe77f9bbSSamuel Holland 
355fe77f9bbSSamuel Holland 	return 0;
356fe77f9bbSSamuel Holland }
357fe77f9bbSSamuel Holland 
axp20x_pek_resume(struct device * dev)358*e04a088bSJonathan Cameron static int axp20x_pek_resume(struct device *dev)
359fe77f9bbSSamuel Holland {
360fe77f9bbSSamuel Holland 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
361fe77f9bbSSamuel Holland 
362fe77f9bbSSamuel Holland 	if (device_may_wakeup(dev)) {
363fe77f9bbSSamuel Holland 		disable_irq_wake(axp20x_pek->irq_dbf);
364fe77f9bbSSamuel Holland 		disable_irq_wake(axp20x_pek->irq_dbr);
365fe77f9bbSSamuel Holland 	} else {
366fe77f9bbSSamuel Holland 		enable_irq(axp20x_pek->irq_dbf);
367fe77f9bbSSamuel Holland 		enable_irq(axp20x_pek->irq_dbr);
368fe77f9bbSSamuel Holland 	}
369fe77f9bbSSamuel Holland 
370fe77f9bbSSamuel Holland 	return 0;
371fe77f9bbSSamuel Holland }
372fe77f9bbSSamuel Holland 
axp20x_pek_resume_noirq(struct device * dev)37358be7689SHans de Goede static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
37458be7689SHans de Goede {
37558be7689SHans de Goede 	struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
37658be7689SHans de Goede 
37758be7689SHans de Goede 	if (axp20x_pek->axp20x->variant != AXP288_ID)
37858be7689SHans de Goede 		return 0;
37958be7689SHans de Goede 
38058be7689SHans de Goede 	/*
38158be7689SHans de Goede 	 * Clear interrupts from button presses during suspend, to avoid
38258be7689SHans de Goede 	 * a wakeup power-button press getting reported to userspace.
38358be7689SHans de Goede 	 */
38458be7689SHans de Goede 	regmap_write(axp20x_pek->axp20x->regmap,
38558be7689SHans de Goede 		     AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
38658be7689SHans de Goede 		     BIT(AXP288_IRQ_POKN % 8));
38758be7689SHans de Goede 
38858be7689SHans de Goede 	return 0;
38958be7689SHans de Goede }
39058be7689SHans de Goede 
39158be7689SHans de Goede static const struct dev_pm_ops axp20x_pek_pm_ops = {
392*e04a088bSJonathan Cameron 	SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
393*e04a088bSJonathan Cameron 	.resume_noirq = pm_sleep_ptr(axp20x_pek_resume_noirq),
39458be7689SHans de Goede };
39558be7689SHans de Goede 
396fbc1b323SQuentin Schulz static const struct platform_device_id axp_pek_id_match[] = {
397fbc1b323SQuentin Schulz 	{
398fbc1b323SQuentin Schulz 		.name = "axp20x-pek",
399fbc1b323SQuentin Schulz 		.driver_data = (kernel_ulong_t)&axp20x_info,
400fbc1b323SQuentin Schulz 	},
401c3cc9447SQuentin Schulz 	{
402c3cc9447SQuentin Schulz 		.name = "axp221-pek",
403c3cc9447SQuentin Schulz 		.driver_data = (kernel_ulong_t)&axp221_info,
404c3cc9447SQuentin Schulz 	},
405fbc1b323SQuentin Schulz 	{ /* sentinel */ }
406fbc1b323SQuentin Schulz };
407481c209fSHans de Goede MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
408fbc1b323SQuentin Schulz 
4095b6c26a9SCarlo Caione static struct platform_driver axp20x_pek_driver = {
4105b6c26a9SCarlo Caione 	.probe		= axp20x_pek_probe,
411fbc1b323SQuentin Schulz 	.id_table	= axp_pek_id_match,
4125b6c26a9SCarlo Caione 	.driver		= {
4135b6c26a9SCarlo Caione 		.name		= "axp20x-pek",
414*e04a088bSJonathan Cameron 		.pm		= pm_sleep_ptr(&axp20x_pek_pm_ops),
415d99995a4SGreg Kroah-Hartman 		.dev_groups	= axp20x_groups,
4165b6c26a9SCarlo Caione 	},
4175b6c26a9SCarlo Caione };
4185b6c26a9SCarlo Caione module_platform_driver(axp20x_pek_driver);
4195b6c26a9SCarlo Caione 
4205b6c26a9SCarlo Caione MODULE_DESCRIPTION("axp20x Power Button");
4215b6c26a9SCarlo Caione MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
4225b6c26a9SCarlo Caione MODULE_LICENSE("GPL");
423