d9e15cf7c6c8839ba09b6e505730f62b5820798d
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / intel_soc_pmic_core.c
1 /*
2  * intel_soc_pmic_core.c - Intel SoC PMIC MFD Driver
3  *
4  * Copyright (C) 2013, 2014 Intel Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 as published by the Free Software Foundation.
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  * Author: Yang, Bin <bin.yang@intel.com>
16  * Author: Zhu, Lejun <lejun.zhu@linux.intel.com>
17  */
18
19 #include <linux/module.h>
20 #include <linux/mfd/core.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/acpi.h>
25 #include <linux/regmap.h>
26 #include <linux/mfd/intel_soc_pmic.h>
27 #include <linux/gpio/machine.h>
28 #include <linux/pwm.h>
29 #include "intel_soc_pmic_core.h"
30
31 /* Lookup table for the Panel Enable/Disable line as GPIO signals */
32 static struct gpiod_lookup_table panel_gpio_table = {
33         /* Intel GFX is consumer */
34         .dev_id = "0000:00:02.0",
35         .table = {
36                 /* Panel EN/DISABLE */
37                 GPIO_LOOKUP("gpio_crystalcove", 94, "panel", GPIO_ACTIVE_HIGH),
38         },
39 };
40
41 /* PWM consumed by the Intel GFX */
42 static struct pwm_lookup crc_pwm_lookup[] = {
43         PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_backlight", 0, PWM_POLARITY_NORMAL),
44 };
45
46 static int intel_soc_pmic_find_gpio_irq(struct device *dev)
47 {
48         struct gpio_desc *desc;
49         int irq;
50
51         desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0, GPIOD_IN);
52         if (IS_ERR(desc))
53                 return PTR_ERR(desc);
54
55         irq = gpiod_to_irq(desc);
56         if (irq < 0)
57                 dev_warn(dev, "Can't get irq: %d\n", irq);
58
59         return irq;
60 }
61
62 static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
63                                     const struct i2c_device_id *i2c_id)
64 {
65         struct device *dev = &i2c->dev;
66         const struct acpi_device_id *id;
67         struct intel_soc_pmic_config *config;
68         struct intel_soc_pmic *pmic;
69         int ret;
70         int irq;
71
72         id = acpi_match_device(dev->driver->acpi_match_table, dev);
73         if (!id || !id->driver_data)
74                 return -ENODEV;
75
76         config = (struct intel_soc_pmic_config *)id->driver_data;
77
78         pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
79         if (!pmic)
80                 return -ENOMEM;
81
82         dev_set_drvdata(dev, pmic);
83
84         pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
85
86         /*
87          * On some boards the PMIC interrupt may come from a GPIO line. Try to
88          * lookup the ACPI table for a such connection and setup a GPIO
89          * interrupt if it exists. Otherwise use the IRQ provided by I2C
90          */
91         irq = intel_soc_pmic_find_gpio_irq(dev);
92         pmic->irq = (irq < 0) ? i2c->irq : irq;
93
94         ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
95                                   config->irq_flags | IRQF_ONESHOT,
96                                   0, config->irq_chip,
97                                   &pmic->irq_chip_data);
98         if (ret)
99                 return ret;
100
101         ret = enable_irq_wake(pmic->irq);
102         if (ret)
103                 dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
104
105         /* Add lookup table binding for Panel Control to the GPIO Chip */
106         gpiod_add_lookup_table(&panel_gpio_table);
107
108         /* Add lookup table for crc-pwm */
109         pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
110
111         ret = mfd_add_devices(dev, -1, config->cell_dev,
112                               config->n_cell_devs, NULL, 0,
113                               regmap_irq_get_domain(pmic->irq_chip_data));
114         if (ret)
115                 goto err_del_irq_chip;
116
117         return 0;
118
119 err_del_irq_chip:
120         regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
121         return ret;
122 }
123
124 static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
125 {
126         struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
127
128         regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
129
130         /* Remove lookup table for Panel Control from the GPIO Chip */
131         gpiod_remove_lookup_table(&panel_gpio_table);
132
133         /* remove crc-pwm lookup table */
134         pwm_remove_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
135
136         mfd_remove_devices(&i2c->dev);
137
138         return 0;
139 }
140
141 static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
142 {
143         struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
144
145         disable_irq(pmic->irq);
146
147         return;
148 }
149
150 #if defined(CONFIG_PM_SLEEP)
151 static int intel_soc_pmic_suspend(struct device *dev)
152 {
153         struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
154
155         disable_irq(pmic->irq);
156
157         return 0;
158 }
159
160 static int intel_soc_pmic_resume(struct device *dev)
161 {
162         struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
163
164         enable_irq(pmic->irq);
165
166         return 0;
167 }
168 #endif
169
170 static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
171                          intel_soc_pmic_resume);
172
173 static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
174         { }
175 };
176 MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
177
178 #if defined(CONFIG_ACPI)
179 static const struct acpi_device_id intel_soc_pmic_acpi_match[] = {
180         {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc},
181         { },
182 };
183 MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
184 #endif
185
186 static struct i2c_driver intel_soc_pmic_i2c_driver = {
187         .driver = {
188                 .name = "intel_soc_pmic_i2c",
189                 .pm = &intel_soc_pmic_pm_ops,
190                 .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
191         },
192         .probe = intel_soc_pmic_i2c_probe,
193         .remove = intel_soc_pmic_i2c_remove,
194         .id_table = intel_soc_pmic_i2c_id,
195         .shutdown = intel_soc_pmic_shutdown,
196 };
197
198 module_i2c_driver(intel_soc_pmic_i2c_driver);
199
200 MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
201 MODULE_LICENSE("GPL v2");
202 MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
203 MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");