2 * drivers/mfd/rt5036-irq.c
3 * Driver for Richtek RT5036 PMIC IRQ driver
5 * Copyright (C) 2014 Richtek Technology Corp.
6 * cy_huang <cy_huang@richtek.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/i2c.h>
20 #include <linux/of_gpio.h>
21 #include <linux/workqueue.h>
22 #include <linux/interrupt.h>
24 #include <linux/mfd/rt5036/rt5036.h>
25 #include <linux/mfd/rt5036/rt5036-irq.h>
27 struct rt5036_irq_info {
28 struct i2c_client *i2c;
29 struct rt5036_chip *chip;
32 unsigned char suspend:1;
33 struct delayed_work irq_delayed_work;
36 static irqreturn_t rt5036_irq_handler(int irqno, void *param)
38 struct rt5036_irq_info *ii = param;
39 unsigned char regval[3];
40 unsigned int irq_event;
44 schedule_delayed_work(&ii->irq_delayed_work,
45 msecs_to_jiffies(10));
50 rt5036_reg_block_read(ii->i2c, RT5036_REG_CHGIRQ1,
51 ARRAY_SIZE(regval), regval);
53 dev_err(ii->dev, "read charger irq event fail\n");
55 irq_event = regval[0] << 16 | regval[1] << 8 | regval[2];
56 RTINFO("chg event %06x\n", irq_event);
57 #ifdef CONFIG_CHARGER_RT5036
58 if (irq_event && ii->chip->chg_info)
59 rt5036_charger_irq_handler(ii->chip->chg_info,
61 #endif /* #ifdef CONFIG_CHARGER_RT5036 */
65 rt5036_reg_block_read(ii->i2c, RT5036_REG_BUCKLDOIRQ,
66 ARRAY_SIZE(regval), regval);
68 dev_err(ii->dev, "read misc irq event fail\n");
70 irq_event = regval[0] << 16 | regval[1] << 8 | regval[2];
71 RTINFO("misc event %06x\n", irq_event);
72 #ifdef CONFIG_MISC_RT5036
73 if (irq_event && ii->chip->misc_info)
74 rt5036_misc_irq_handler(ii->chip->misc_info, irq_event);
75 #endif /* #ifdef CONFIG_MISC_RT5036 */
78 ret = rt5036_reg_read(ii->i2c, RT5036_REG_STBWACKIRQ);
80 dev_err(ii->dev, "read rtc irq event fail\n");
83 RTINFO("rtc event %02x\n", irq_event);
84 #ifdef CONFIG_RTC_RT5036
85 if (irq_event && ii->chip->rtc_info)
86 rt5036_rtc_irq_handler(ii->chip->rtc_info, irq_event);
87 #endif /* #ifdef CONFIG_RTC_RT5036 */
89 rt5036_set_bits(ii->i2c, RT5036_REG_BUCKVN1, RT5036_IRQPREZ_MASK);
94 static void rt5036_irq_delayed_work(struct work_struct *work)
96 struct rt5036_irq_info *ii =
97 (struct rt5036_irq_info *)container_of(work, struct rt5036_irq_info,
98 irq_delayed_work.work);
99 rt5036_irq_handler(ii->irq, ii);
102 static int rt_parse_dt(struct rt5036_irq_info *ii, struct device *dev)
105 struct device_node *np = dev->of_node;
108 val = of_get_named_gpio(np, "rt,irq-gpio", 0);
109 if (gpio_is_valid(val)) {
110 if (gpio_request(val, "rt5036_irq") >= 0) {
111 gpio_direction_input(val);
112 ii->irq = gpio_to_irq(val);
119 #endif /* #ifdef CONFIG_OF */
124 static int rt_parse_pdata(struct rt5036_irq_info *ii,
127 struct rt5036_irq_data *pdata = dev->platform_data;
129 if (gpio_is_valid(pdata->irq_gpio)) {
130 if (gpio_request(pdata->irq_gpio, "rt5036_irq") >= 0) {
131 gpio_direction_input(pdata->irq_gpio);
132 ii->irq = gpio_to_irq(pdata->irq_gpio);
143 static int rt5036_irq_probe(struct platform_device *pdev)
145 struct rt5036_chip *chip = dev_get_drvdata(pdev->dev.parent);
146 struct rt5036_platform_data *pdata = (pdev->dev.parent)->platform_data;
147 struct rt5036_irq_info *ii;
148 bool use_dt = pdev->dev.of_node;
150 ii = devm_kzalloc(&pdev->dev, sizeof(*ii), GFP_KERNEL);
156 ii->dev = &pdev->dev;
158 rt_parse_dt(ii, &pdev->dev);
162 pdev->dev.platform_data = pdata->irq_pdata;
163 rt_parse_pdata(ii, &pdev->dev);
164 dev_info(&pdev->dev, "ii->irq %d\n", ii->irq);
166 INIT_DELAYED_WORK(&ii->irq_delayed_work, rt5036_irq_delayed_work);
168 platform_set_drvdata(pdev, ii);
170 if (devm_request_threaded_irq
171 (&pdev->dev, ii->irq, NULL,rt5036_irq_handler,
172 IRQF_TRIGGER_FALLING | IRQF_NO_SUSPEND | IRQF_ONESHOT,
174 dev_err(&pdev->dev, "request threaded irq fail\n");
177 enable_irq_wake(ii->irq);
178 schedule_delayed_work(&ii->irq_delayed_work,
179 msecs_to_jiffies(500));
181 dev_info(&pdev->dev, "driver successfully loaded\n");
187 static int rt5036_irq_remove(struct platform_device *pdev)
189 struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
192 devm_free_irq(&pdev->dev, ii->irq, ii);
196 static int rt5036_irq_suspend(struct platform_device *pdev, pm_message_t state)
198 struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
204 static int rt5036_irq_resume(struct platform_device *pdev)
206 struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
212 static const struct of_device_id rt_match_table[] = {
213 {.compatible = "rt,rt5036-irq",},
217 static struct platform_driver rt5036_irq_driver = {
219 .name = RT5036_DEV_NAME "-irq",
220 .owner = THIS_MODULE,
221 .of_match_table = rt_match_table,
223 .probe = rt5036_irq_probe,
224 .remove = rt5036_irq_remove,
225 .suspend = rt5036_irq_suspend,
226 .resume = rt5036_irq_resume,
229 static int __init rt5036_irq_init(void)
231 return platform_driver_register(&rt5036_irq_driver);
233 subsys_initcall_sync(rt5036_irq_init);
235 static void __exit rt5036_irq_exit(void)
237 platform_driver_unregister(&rt5036_irq_driver);
239 module_exit(rt5036_irq_exit);
241 MODULE_LICENSE("GPL");
242 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
243 MODULE_DESCRIPTION("IRQ driver for RT5036");
244 MODULE_ALIAS("platform:" RT5036_DEV_NAME "-irq");
245 MODULE_VERSION(RT5036_DRV_VER);