blob: c04723efc70709d4dd81db288be1fa11fc6deccb [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>
31#include <linux/pm_runtime.h>
32#include <linux/mutex.h>
33#include <linux/mfd/core.h>
34#include <linux/mfd/max77693.h>
35#include <linux/mfd/max77693-private.h>
36#include <linux/regulator/machine.h>
37#include <linux/regmap.h>
38
39#define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
40#define I2C_ADDR_MUIC (0x4A >> 1)
41#define I2C_ADDR_HAPTIC (0x90 >> 1)
42
43static struct mfd_cell max77693_devs[] = {
44 { .name = "max77693-pmic", },
45 { .name = "max77693-charger", },
46 { .name = "max77693-flash", },
47 { .name = "max77693-muic", },
48 { .name = "max77693-haptic", },
49};
50
51int max77693_read_reg(struct regmap *map, u8 reg, u8 *dest)
52{
53 unsigned int val;
54 int ret;
55
56 ret = regmap_read(map, reg, &val);
57 *dest = val;
58
59 return ret;
60}
61EXPORT_SYMBOL_GPL(max77693_read_reg);
62
63int max77693_bulk_read(struct regmap *map, u8 reg, int count, u8 *buf)
64{
65 int ret;
66
67 ret = regmap_bulk_read(map, reg, buf, count);
68
69 return ret;
70}
71EXPORT_SYMBOL_GPL(max77693_bulk_read);
72
73int max77693_write_reg(struct regmap *map, u8 reg, u8 value)
74{
75 int ret;
76
77 ret = regmap_write(map, reg, value);
78
79 return ret;
80}
81EXPORT_SYMBOL_GPL(max77693_write_reg);
82
83int max77693_bulk_write(struct regmap *map, u8 reg, int count, u8 *buf)
84{
85 int ret;
86
87 ret = regmap_bulk_write(map, reg, buf, count);
88
89 return ret;
90}
91EXPORT_SYMBOL_GPL(max77693_bulk_write);
92
93int max77693_update_reg(struct regmap *map, u8 reg, u8 val, u8 mask)
94{
95 int ret;
96
97 ret = regmap_update_bits(map, reg, mask, val);
98
99 return ret;
100}
101EXPORT_SYMBOL_GPL(max77693_update_reg);
102
103static const struct regmap_config max77693_regmap_config = {
104 .reg_bits = 8,
105 .val_bits = 8,
106 .max_register = MAX77693_PMIC_REG_END,
107};
108
109static int max77693_i2c_probe(struct i2c_client *i2c,
110 const struct i2c_device_id *id)
111{
112 struct max77693_dev *max77693;
Jingoo Han334a41c2013-07-30 17:10:05 +0900113 struct max77693_platform_data *pdata = dev_get_platdata(&i2c->dev);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200114 u8 reg_data;
115 int ret = 0;
116
Axel Lin2429d862012-12-25 00:28:36 +0800117 if (!pdata) {
118 dev_err(&i2c->dev, "No platform data found.\n");
119 return -EINVAL;
120 }
121
Chanwoo Choi83871c02012-05-14 22:50:39 +0200122 max77693 = devm_kzalloc(&i2c->dev,
123 sizeof(struct max77693_dev), GFP_KERNEL);
124 if (max77693 == NULL)
125 return -ENOMEM;
126
Chanwoo Choi83871c02012-05-14 22:50:39 +0200127 i2c_set_clientdata(i2c, max77693);
128 max77693->dev = &i2c->dev;
129 max77693->i2c = i2c;
130 max77693->irq = i2c->irq;
131 max77693->type = id->driver_data;
132
Axel Lin2429d862012-12-25 00:28:36 +0800133 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
134 if (IS_ERR(max77693->regmap)) {
135 ret = PTR_ERR(max77693->regmap);
136 dev_err(max77693->dev, "failed to allocate register map: %d\n",
137 ret);
138 return ret;
139 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200140
141 max77693->wakeup = pdata->wakeup;
142
Axel Lin2429d862012-12-25 00:28:36 +0800143 ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
144 &reg_data);
145 if (ret < 0) {
Chanwoo Choi83871c02012-05-14 22:50:39 +0200146 dev_err(max77693->dev, "device not found on this channel\n");
Axel Lin2429d862012-12-25 00:28:36 +0800147 return ret;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200148 } else
149 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
150
151 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
152 i2c_set_clientdata(max77693->muic, max77693);
153
154 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
155 i2c_set_clientdata(max77693->haptic, max77693);
156
Chanwoo Choib186b122012-08-21 15:16:23 +0900157 /*
158 * Initialize register map for MUIC device because use regmap-muic
159 * instance of MUIC device when irq of max77693 is initialized
160 * before call max77693-muic probe() function.
161 */
162 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
163 &max77693_regmap_config);
164 if (IS_ERR(max77693->regmap_muic)) {
165 ret = PTR_ERR(max77693->regmap_muic);
166 dev_err(max77693->dev,
167 "failed to allocate register map: %d\n", ret);
Axel Lin2429d862012-12-25 00:28:36 +0800168 goto err_regmap_muic;
Chanwoo Choib186b122012-08-21 15:16:23 +0900169 }
170
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200171 ret = max77693_irq_init(max77693);
172 if (ret < 0)
Axel Linff2b7ac2012-06-08 08:35:37 +0800173 goto err_irq;
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200174
Chanwoo Choi83871c02012-05-14 22:50:39 +0200175 pm_runtime_set_active(max77693->dev);
176
177 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800178 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200179 if (ret < 0)
180 goto err_mfd;
181
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200182 device_init_wakeup(max77693->dev, pdata->wakeup);
183
Chanwoo Choi83871c02012-05-14 22:50:39 +0200184 return ret;
185
186err_mfd:
Axel Linff2b7ac2012-06-08 08:35:37 +0800187 max77693_irq_exit(max77693);
188err_irq:
Axel Lin2429d862012-12-25 00:28:36 +0800189err_regmap_muic:
Chanwoo Choi83871c02012-05-14 22:50:39 +0200190 i2c_unregister_device(max77693->muic);
191 i2c_unregister_device(max77693->haptic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200192 return ret;
193}
194
195static int max77693_i2c_remove(struct i2c_client *i2c)
196{
197 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
198
199 mfd_remove_devices(max77693->dev);
Axel Linff2b7ac2012-06-08 08:35:37 +0800200 max77693_irq_exit(max77693);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200201 i2c_unregister_device(max77693->muic);
202 i2c_unregister_device(max77693->haptic);
203
204 return 0;
205}
206
207static const struct i2c_device_id max77693_i2c_id[] = {
208 { "max77693", TYPE_MAX77693 },
209 { }
210};
211MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
212
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200213static int max77693_suspend(struct device *dev)
214{
215 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
216 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
217
218 if (device_may_wakeup(dev))
219 irq_set_irq_wake(max77693->irq, 1);
220 return 0;
221}
222
223static int max77693_resume(struct device *dev)
224{
225 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
226 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
227
228 if (device_may_wakeup(dev))
229 irq_set_irq_wake(max77693->irq, 0);
230 return max77693_irq_resume(max77693);
231}
232
Mark Brown12477a32012-06-05 16:15:59 +0100233static const struct dev_pm_ops max77693_pm = {
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200234 .suspend = max77693_suspend,
235 .resume = max77693_resume,
236};
237
Chanwoo Choi83871c02012-05-14 22:50:39 +0200238static struct i2c_driver max77693_i2c_driver = {
239 .driver = {
240 .name = "max77693",
241 .owner = THIS_MODULE,
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200242 .pm = &max77693_pm,
Chanwoo Choi83871c02012-05-14 22:50:39 +0200243 },
244 .probe = max77693_i2c_probe,
245 .remove = max77693_i2c_remove,
246 .id_table = max77693_i2c_id,
247};
248
249static int __init max77693_i2c_init(void)
250{
251 return i2c_add_driver(&max77693_i2c_driver);
252}
253/* init early so consumer devices can complete system boot */
254subsys_initcall(max77693_i2c_init);
255
256static void __exit max77693_i2c_exit(void)
257{
258 i2c_del_driver(&max77693_i2c_driver);
259}
260module_exit(max77693_i2c_exit);
261
262MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
263MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
264MODULE_LICENSE("GPL");