blob: 7e05428c756d47c6936d5f39f7808a0394c95872 [file] [log] [blame]
Chanwoo Choi83871c02012-05-14 22:50:39 +02001/*
2 * max77693.c - mfd core driver for the MAX 77693
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * SangYoung Son <hello.son@smasung.com>
6 *
7 * This program is not provided / owned by Maxim Integrated Products.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * This driver is based on max8997.c
24 */
25
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/err.h>
30#include <linux/interrupt.h>
Sachin Kamat71c7f932013-10-18 16:11:57 +053031#include <linux/of.h>
Chanwoo Choi83871c02012-05-14 22:50:39 +020032#include <linux/pm_runtime.h>
33#include <linux/mutex.h>
34#include <linux/mfd/core.h>
35#include <linux/mfd/max77693.h>
36#include <linux/mfd/max77693-private.h>
37#include <linux/regulator/machine.h>
38#include <linux/regmap.h>
39
40#define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
41#define I2C_ADDR_MUIC (0x4A >> 1)
42#define I2C_ADDR_HAPTIC (0x90 >> 1)
43
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010044static const struct mfd_cell max77693_devs[] = {
Chanwoo Choi83871c02012-05-14 22:50:39 +020045 { .name = "max77693-pmic", },
46 { .name = "max77693-charger", },
47 { .name = "max77693-flash", },
48 { .name = "max77693-muic", },
49 { .name = "max77693-haptic", },
50};
51
52int max77693_read_reg(struct regmap *map, u8 reg, u8 *dest)
53{
54 unsigned int val;
55 int ret;
56
57 ret = regmap_read(map, reg, &val);
58 *dest = val;
59
60 return ret;
61}
62EXPORT_SYMBOL_GPL(max77693_read_reg);
63
64int max77693_bulk_read(struct regmap *map, u8 reg, int count, u8 *buf)
65{
66 int ret;
67
68 ret = regmap_bulk_read(map, reg, buf, count);
69
70 return ret;
71}
72EXPORT_SYMBOL_GPL(max77693_bulk_read);
73
74int max77693_write_reg(struct regmap *map, u8 reg, u8 value)
75{
76 int ret;
77
78 ret = regmap_write(map, reg, value);
79
80 return ret;
81}
82EXPORT_SYMBOL_GPL(max77693_write_reg);
83
84int max77693_bulk_write(struct regmap *map, u8 reg, int count, u8 *buf)
85{
86 int ret;
87
88 ret = regmap_bulk_write(map, reg, buf, count);
89
90 return ret;
91}
92EXPORT_SYMBOL_GPL(max77693_bulk_write);
93
94int max77693_update_reg(struct regmap *map, u8 reg, u8 val, u8 mask)
95{
96 int ret;
97
98 ret = regmap_update_bits(map, reg, mask, val);
99
100 return ret;
101}
102EXPORT_SYMBOL_GPL(max77693_update_reg);
103
104static const struct regmap_config max77693_regmap_config = {
105 .reg_bits = 8,
106 .val_bits = 8,
107 .max_register = MAX77693_PMIC_REG_END,
108};
109
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100110static const struct regmap_config max77693_regmap_muic_config = {
111 .reg_bits = 8,
112 .val_bits = 8,
113 .max_register = MAX77693_MUIC_REG_END,
114};
115
Chanwoo Choi83871c02012-05-14 22:50:39 +0200116static int max77693_i2c_probe(struct i2c_client *i2c,
117 const struct i2c_device_id *id)
118{
119 struct max77693_dev *max77693;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200120 u8 reg_data;
121 int ret = 0;
122
123 max77693 = devm_kzalloc(&i2c->dev,
124 sizeof(struct max77693_dev), GFP_KERNEL);
125 if (max77693 == NULL)
126 return -ENOMEM;
127
Chanwoo Choi83871c02012-05-14 22:50:39 +0200128 i2c_set_clientdata(i2c, max77693);
129 max77693->dev = &i2c->dev;
130 max77693->i2c = i2c;
131 max77693->irq = i2c->irq;
132 max77693->type = id->driver_data;
133
Axel Lin2429d862012-12-25 00:28:36 +0800134 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
135 if (IS_ERR(max77693->regmap)) {
136 ret = PTR_ERR(max77693->regmap);
137 dev_err(max77693->dev, "failed to allocate register map: %d\n",
138 ret);
139 return ret;
140 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200141
Axel Lin2429d862012-12-25 00:28:36 +0800142 ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
143 &reg_data);
144 if (ret < 0) {
Chanwoo Choi83871c02012-05-14 22:50:39 +0200145 dev_err(max77693->dev, "device not found on this channel\n");
Axel Lin2429d862012-12-25 00:28:36 +0800146 return ret;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200147 } else
148 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
149
150 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100151 if (!max77693->muic) {
152 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
153 return -ENODEV;
154 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200155 i2c_set_clientdata(max77693->muic, max77693);
156
157 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100158 if (!max77693->haptic) {
159 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
160 ret = -ENODEV;
161 goto err_i2c_haptic;
162 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200163 i2c_set_clientdata(max77693->haptic, max77693);
164
Chanwoo Choib186b122012-08-21 15:16:23 +0900165 /*
166 * Initialize register map for MUIC device because use regmap-muic
167 * instance of MUIC device when irq of max77693 is initialized
168 * before call max77693-muic probe() function.
169 */
170 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100171 &max77693_regmap_muic_config);
Chanwoo Choib186b122012-08-21 15:16:23 +0900172 if (IS_ERR(max77693->regmap_muic)) {
173 ret = PTR_ERR(max77693->regmap_muic);
174 dev_err(max77693->dev,
175 "failed to allocate register map: %d\n", ret);
Axel Lin2429d862012-12-25 00:28:36 +0800176 goto err_regmap_muic;
Chanwoo Choib186b122012-08-21 15:16:23 +0900177 }
178
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200179 ret = max77693_irq_init(max77693);
180 if (ret < 0)
Axel Linff2b7ac2012-06-08 08:35:37 +0800181 goto err_irq;
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200182
Chanwoo Choi83871c02012-05-14 22:50:39 +0200183 pm_runtime_set_active(max77693->dev);
184
185 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800186 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200187 if (ret < 0)
188 goto err_mfd;
189
190 return ret;
191
192err_mfd:
Axel Linff2b7ac2012-06-08 08:35:37 +0800193 max77693_irq_exit(max77693);
194err_irq:
Axel Lin2429d862012-12-25 00:28:36 +0800195err_regmap_muic:
Chanwoo Choi83871c02012-05-14 22:50:39 +0200196 i2c_unregister_device(max77693->haptic);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100197err_i2c_haptic:
198 i2c_unregister_device(max77693->muic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200199 return ret;
200}
201
202static int max77693_i2c_remove(struct i2c_client *i2c)
203{
204 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
205
206 mfd_remove_devices(max77693->dev);
Axel Linff2b7ac2012-06-08 08:35:37 +0800207 max77693_irq_exit(max77693);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200208 i2c_unregister_device(max77693->muic);
209 i2c_unregister_device(max77693->haptic);
210
211 return 0;
212}
213
214static const struct i2c_device_id max77693_i2c_id[] = {
215 { "max77693", TYPE_MAX77693 },
216 { }
217};
218MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
219
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200220static int max77693_suspend(struct device *dev)
221{
222 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
223 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
224
225 if (device_may_wakeup(dev))
226 irq_set_irq_wake(max77693->irq, 1);
227 return 0;
228}
229
230static int max77693_resume(struct device *dev)
231{
232 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
233 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
234
235 if (device_may_wakeup(dev))
236 irq_set_irq_wake(max77693->irq, 0);
237 return max77693_irq_resume(max77693);
238}
239
Mark Brown12477a32012-06-05 16:15:59 +0100240static const struct dev_pm_ops max77693_pm = {
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200241 .suspend = max77693_suspend,
242 .resume = max77693_resume,
243};
244
Andrzej Hajda46571852013-09-27 09:27:46 +0200245#ifdef CONFIG_OF
Krzysztof Kozlowski5ce9a552014-05-13 12:58:52 +0200246static const struct of_device_id max77693_dt_match[] = {
Andrzej Hajda46571852013-09-27 09:27:46 +0200247 { .compatible = "maxim,max77693" },
248 {},
249};
250#endif
251
Chanwoo Choi83871c02012-05-14 22:50:39 +0200252static struct i2c_driver max77693_i2c_driver = {
253 .driver = {
254 .name = "max77693",
255 .owner = THIS_MODULE,
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200256 .pm = &max77693_pm,
Andrzej Hajda46571852013-09-27 09:27:46 +0200257 .of_match_table = of_match_ptr(max77693_dt_match),
Chanwoo Choi83871c02012-05-14 22:50:39 +0200258 },
259 .probe = max77693_i2c_probe,
260 .remove = max77693_i2c_remove,
261 .id_table = max77693_i2c_id,
262};
263
264static int __init max77693_i2c_init(void)
265{
266 return i2c_add_driver(&max77693_i2c_driver);
267}
268/* init early so consumer devices can complete system boot */
269subsys_initcall(max77693_i2c_init);
270
271static void __exit max77693_i2c_exit(void)
272{
273 i2c_del_driver(&max77693_i2c_driver);
274}
275module_exit(max77693_i2c_exit);
276
277MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
278MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
279MODULE_LICENSE("GPL");