mfd: fusb302: change to host when connect type-c to standard-a cable
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rt5036-irq.c
1 /*
2  *  drivers/mfd/rt5036-irq.c
3  *  Driver for Richtek RT5036 PMIC IRQ driver
4  *
5  *  Copyright (C) 2014 Richtek Technology Corp.
6  *  cy_huang <cy_huang@richtek.com>
7  *
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.
12  */
13
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>
23
24 #include <linux/mfd/rt5036/rt5036.h>
25 #include <linux/mfd/rt5036/rt5036-irq.h>
26
27 struct rt5036_irq_info {
28         struct i2c_client *i2c;
29         struct rt5036_chip *chip;
30         struct device *dev;
31         int irq;
32         unsigned char suspend:1;
33         struct delayed_work irq_delayed_work;
34 };
35
36 static irqreturn_t rt5036_irq_handler(int irqno, void *param)
37 {
38         struct rt5036_irq_info *ii = param;
39         unsigned char regval[3];
40         unsigned int irq_event;
41         int ret = 0;
42
43         if (ii->suspend) {
44                 schedule_delayed_work(&ii->irq_delayed_work,
45                                       msecs_to_jiffies(10));
46                 goto irq_fin;
47         }
48
49         ret =
50             rt5036_reg_block_read(ii->i2c, RT5036_REG_CHGIRQ1,
51                                   ARRAY_SIZE(regval), regval);
52         if (ret < 0) {
53                 dev_err(ii->dev, "read charger irq event fail\n");
54         } else {
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,
60                                                    irq_event);
61 #endif /* #ifdef CONFIG_CHARGER_RT5036 */
62         }
63
64         ret =
65             rt5036_reg_block_read(ii->i2c, RT5036_REG_BUCKLDOIRQ,
66                                   ARRAY_SIZE(regval), regval);
67         if (ret < 0) {
68                 dev_err(ii->dev, "read misc irq event fail\n");
69         } else {
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 */
76         }
77
78         ret = rt5036_reg_read(ii->i2c, RT5036_REG_STBWACKIRQ);
79         if (ret < 0) {
80                 dev_err(ii->dev, "read rtc irq event fail\n");
81         } else {
82                 irq_event = ret;
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 */
88         }
89         rt5036_set_bits(ii->i2c, RT5036_REG_BUCKVN1, RT5036_IRQPREZ_MASK);
90 irq_fin:
91         return IRQ_HANDLED;
92 }
93
94 static void rt5036_irq_delayed_work(struct work_struct *work)
95 {
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);
100 }
101
102 static int rt_parse_dt(struct rt5036_irq_info *ii, struct device *dev)
103 {
104 #ifdef CONFIG_OF
105         struct device_node *np = dev->of_node;
106         int val;
107
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);
113                 } else {
114                         ii->irq = -1;
115                 }
116         } else {
117                 ii->irq = -1;
118         }
119 #endif /* #ifdef CONFIG_OF */
120         RTINFO("\n");
121         return 0;
122 }
123
124 static int rt_parse_pdata(struct rt5036_irq_info *ii,
125                                     struct device *dev)
126 {
127         struct rt5036_irq_data *pdata = dev->platform_data;
128
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);
133                 } else {
134                         ii->irq = -1;
135                 }
136         } else {
137                 ii->irq = -1;
138         }
139         RTINFO("\n");
140         return 0;
141 }
142
143 static int rt5036_irq_probe(struct platform_device *pdev)
144 {
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;
149
150         ii = devm_kzalloc(&pdev->dev, sizeof(*ii), GFP_KERNEL);
151         if (!ii)
152                 return -ENOMEM;
153
154         ii->i2c = chip->i2c;
155         ii->chip = chip;
156         ii->dev = &pdev->dev;
157         if (use_dt) {
158                 rt_parse_dt(ii, &pdev->dev);
159         } else {
160                 if (!pdata)
161                         goto out_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);
165         }
166         INIT_DELAYED_WORK(&ii->irq_delayed_work, rt5036_irq_delayed_work);
167
168         platform_set_drvdata(pdev, ii);
169         if (ii->irq >= 0) {
170                 if (devm_request_threaded_irq
171                     (&pdev->dev, ii->irq, NULL,rt5036_irq_handler,
172                      IRQF_TRIGGER_FALLING | IRQF_NO_SUSPEND | IRQF_ONESHOT,
173                      "rt5036_irq", ii)) {
174                         dev_err(&pdev->dev, "request threaded irq fail\n");
175                         goto out_dev;
176                 }
177                 enable_irq_wake(ii->irq);
178                 schedule_delayed_work(&ii->irq_delayed_work,
179                                       msecs_to_jiffies(500));
180         }
181         dev_info(&pdev->dev, "driver successfully loaded\n");
182         return 0;
183 out_dev:
184         return -EINVAL;
185 }
186
187 static int rt5036_irq_remove(struct platform_device *pdev)
188 {
189         struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
190
191         if (ii->irq >= 0)
192                 devm_free_irq(&pdev->dev, ii->irq, ii);
193         return 0;
194 }
195
196 static int rt5036_irq_suspend(struct platform_device *pdev, pm_message_t state)
197 {
198         struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
199
200         ii->suspend = 1;
201         return 0;
202 }
203
204 static int rt5036_irq_resume(struct platform_device *pdev)
205 {
206         struct rt5036_irq_info *ii = platform_get_drvdata(pdev);
207
208         ii->suspend = 0;
209         return 0;
210 }
211
212 static const struct of_device_id rt_match_table[] = {
213         {.compatible = "rt,rt5036-irq",},
214         {},
215 };
216
217 static struct platform_driver rt5036_irq_driver = {
218         .driver = {
219                    .name = RT5036_DEV_NAME "-irq",
220                    .owner = THIS_MODULE,
221                    .of_match_table = rt_match_table,
222                    },
223         .probe = rt5036_irq_probe,
224         .remove = rt5036_irq_remove,
225         .suspend = rt5036_irq_suspend,
226         .resume = rt5036_irq_resume,
227 };
228
229 static int __init rt5036_irq_init(void)
230 {
231         return platform_driver_register(&rt5036_irq_driver);
232 }
233 subsys_initcall_sync(rt5036_irq_init);
234
235 static void __exit rt5036_irq_exit(void)
236 {
237         platform_driver_unregister(&rt5036_irq_driver);
238 }
239 module_exit(rt5036_irq_exit);
240
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);