Commit | Line | Data |
---|---|---|
51652384 ZL |
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 "intel_soc_pmic_core.h" | |
28 | ||
29 | /* | |
30 | * On some boards the PMIC interrupt may come from a GPIO line. | |
31 | * Try to lookup the ACPI table and see if such connection exists. If not, | |
32 | * return -ENOENT and use the IRQ provided by I2C. | |
33 | */ | |
34 | static int intel_soc_pmic_find_gpio_irq(struct device *dev) | |
35 | { | |
36 | struct gpio_desc *desc; | |
37 | int irq; | |
38 | ||
39 | desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0); | |
40 | if (IS_ERR(desc)) | |
41 | return -ENOENT; | |
42 | ||
43 | irq = gpiod_to_irq(desc); | |
44 | if (irq < 0) | |
45 | dev_warn(dev, "Can't get irq: %d\n", irq); | |
46 | ||
47 | return irq; | |
48 | } | |
49 | ||
50 | static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c, | |
51 | const struct i2c_device_id *i2c_id) | |
52 | { | |
53 | struct device *dev = &i2c->dev; | |
54 | const struct acpi_device_id *id; | |
55 | struct intel_soc_pmic_config *config; | |
56 | struct intel_soc_pmic *pmic; | |
57 | int ret; | |
58 | int irq; | |
59 | ||
60 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | |
61 | if (!id || !id->driver_data) | |
62 | return -ENODEV; | |
63 | ||
64 | config = (struct intel_soc_pmic_config *)id->driver_data; | |
65 | ||
66 | pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL); | |
67 | dev_set_drvdata(dev, pmic); | |
68 | ||
69 | pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config); | |
70 | ||
71 | irq = intel_soc_pmic_find_gpio_irq(dev); | |
72 | pmic->irq = (irq < 0) ? i2c->irq : irq; | |
73 | ||
74 | ret = regmap_add_irq_chip(pmic->regmap, pmic->irq, | |
75 | config->irq_flags | IRQF_ONESHOT, | |
76 | 0, config->irq_chip, | |
77 | &pmic->irq_chip_data); | |
78 | if (ret) | |
79 | return ret; | |
80 | ||
81 | ret = enable_irq_wake(pmic->irq); | |
82 | if (ret) | |
83 | dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret); | |
84 | ||
85 | ret = mfd_add_devices(dev, -1, config->cell_dev, | |
86 | config->n_cell_devs, NULL, 0, | |
87 | regmap_irq_get_domain(pmic->irq_chip_data)); | |
88 | if (ret) | |
89 | goto err_del_irq_chip; | |
90 | ||
91 | return 0; | |
92 | ||
93 | err_del_irq_chip: | |
94 | regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data); | |
95 | return ret; | |
96 | } | |
97 | ||
98 | static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c) | |
99 | { | |
100 | struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev); | |
101 | ||
102 | regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data); | |
103 | ||
104 | mfd_remove_devices(&i2c->dev); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | static void intel_soc_pmic_shutdown(struct i2c_client *i2c) | |
110 | { | |
111 | struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev); | |
112 | ||
113 | disable_irq(pmic->irq); | |
114 | ||
115 | return; | |
116 | } | |
117 | ||
bdaf6703 | 118 | #if defined(CONFIG_PM_SLEEP) |
51652384 ZL |
119 | static int intel_soc_pmic_suspend(struct device *dev) |
120 | { | |
121 | struct intel_soc_pmic *pmic = dev_get_drvdata(dev); | |
122 | ||
123 | disable_irq(pmic->irq); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | static int intel_soc_pmic_resume(struct device *dev) | |
129 | { | |
130 | struct intel_soc_pmic *pmic = dev_get_drvdata(dev); | |
131 | ||
132 | enable_irq(pmic->irq); | |
133 | ||
134 | return 0; | |
135 | } | |
bdaf6703 | 136 | #endif |
51652384 ZL |
137 | |
138 | static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend, | |
139 | intel_soc_pmic_resume); | |
140 | ||
141 | static const struct i2c_device_id intel_soc_pmic_i2c_id[] = { | |
142 | { } | |
143 | }; | |
144 | MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id); | |
145 | ||
52764fd9 | 146 | #if defined(CONFIG_ACPI) |
51652384 ZL |
147 | static struct acpi_device_id intel_soc_pmic_acpi_match[] = { |
148 | {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc}, | |
149 | { }, | |
150 | }; | |
151 | MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match); | |
52764fd9 | 152 | #endif |
51652384 ZL |
153 | |
154 | static struct i2c_driver intel_soc_pmic_i2c_driver = { | |
155 | .driver = { | |
156 | .name = "intel_soc_pmic_i2c", | |
157 | .owner = THIS_MODULE, | |
158 | .pm = &intel_soc_pmic_pm_ops, | |
159 | .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match), | |
160 | }, | |
161 | .probe = intel_soc_pmic_i2c_probe, | |
162 | .remove = intel_soc_pmic_i2c_remove, | |
163 | .id_table = intel_soc_pmic_i2c_id, | |
164 | .shutdown = intel_soc_pmic_shutdown, | |
165 | }; | |
166 | ||
167 | module_i2c_driver(intel_soc_pmic_i2c_driver); | |
168 | ||
169 | MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC"); | |
170 | MODULE_LICENSE("GPL v2"); | |
171 | MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>"); | |
172 | MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>"); |