aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mfd/max77686.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mfd/max77686.c')
-rw-r--r--drivers/mfd/max77686.c329
1 files changed, 293 insertions, 36 deletions
diff --git a/drivers/mfd/max77686.c b/drivers/mfd/max77686.c
index ce869acf27ae..86e552348db4 100644
--- a/drivers/mfd/max77686.c
+++ b/drivers/mfd/max77686.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * max77686.c - mfd core driver for the Maxim 77686 2 * max77686.c - mfd core driver for the Maxim 77686/802
3 * 3 *
4 * Copyright (C) 2012 Samsung Electronics 4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@smasung.com> 5 * Chiwoong Byun <woong.byun@smasung.com>
@@ -25,6 +25,8 @@
25#include <linux/export.h> 25#include <linux/export.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/i2c.h> 27#include <linux/i2c.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
28#include <linux/pm_runtime.h> 30#include <linux/pm_runtime.h>
29#include <linux/module.h> 31#include <linux/module.h>
30#include <linux/mfd/core.h> 32#include <linux/mfd/core.h>
@@ -41,15 +43,166 @@ static const struct mfd_cell max77686_devs[] = {
41 { .name = "max77686-clk", }, 43 { .name = "max77686-clk", },
42}; 44};
43 45
46static const struct mfd_cell max77802_devs[] = {
47 { .name = "max77802-pmic", },
48 { .name = "max77802-clk", },
49 { .name = "max77802-rtc", },
50};
51
52static bool max77802_pmic_is_accessible_reg(struct device *dev,
53 unsigned int reg)
54{
55 return (reg >= MAX77802_REG_DEVICE_ID && reg < MAX77802_REG_PMIC_END);
56}
57
58static bool max77802_rtc_is_accessible_reg(struct device *dev,
59 unsigned int reg)
60{
61 return (reg >= MAX77802_RTC_INT && reg < MAX77802_RTC_END);
62}
63
64static bool max77802_is_accessible_reg(struct device *dev, unsigned int reg)
65{
66 return (max77802_pmic_is_accessible_reg(dev, reg) ||
67 max77802_rtc_is_accessible_reg(dev, reg));
68}
69
70static bool max77802_pmic_is_precious_reg(struct device *dev, unsigned int reg)
71{
72 return (reg == MAX77802_REG_INTSRC || reg == MAX77802_REG_INT1 ||
73 reg == MAX77802_REG_INT2);
74}
75
76static bool max77802_rtc_is_precious_reg(struct device *dev, unsigned int reg)
77{
78 return (reg == MAX77802_RTC_INT ||
79 reg == MAX77802_RTC_UPDATE0 ||
80 reg == MAX77802_RTC_UPDATE1);
81}
82
83static bool max77802_is_precious_reg(struct device *dev, unsigned int reg)
84{
85 return (max77802_pmic_is_precious_reg(dev, reg) ||
86 max77802_rtc_is_precious_reg(dev, reg));
87}
88
89static bool max77802_pmic_is_volatile_reg(struct device *dev, unsigned int reg)
90{
91 return (max77802_is_precious_reg(dev, reg) ||
92 reg == MAX77802_REG_STATUS1 || reg == MAX77802_REG_STATUS2 ||
93 reg == MAX77802_REG_PWRON);
94}
95
96static bool max77802_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
97{
98 return (max77802_rtc_is_precious_reg(dev, reg) ||
99 reg == MAX77802_RTC_SEC ||
100 reg == MAX77802_RTC_MIN ||
101 reg == MAX77802_RTC_HOUR ||
102 reg == MAX77802_RTC_WEEKDAY ||
103 reg == MAX77802_RTC_MONTH ||
104 reg == MAX77802_RTC_YEAR ||
105 reg == MAX77802_RTC_DATE);
106}
107
108static bool max77802_is_volatile_reg(struct device *dev, unsigned int reg)
109{
110 return (max77802_pmic_is_volatile_reg(dev, reg) ||
111 max77802_rtc_is_volatile_reg(dev, reg));
112}
113
44static struct regmap_config max77686_regmap_config = { 114static struct regmap_config max77686_regmap_config = {
45 .reg_bits = 8, 115 .reg_bits = 8,
46 .val_bits = 8, 116 .val_bits = 8,
47}; 117};
48 118
49#ifdef CONFIG_OF 119static struct regmap_config max77686_rtc_regmap_config = {
120 .reg_bits = 8,
121 .val_bits = 8,
122};
123
124static struct regmap_config max77802_regmap_config = {
125 .reg_bits = 8,
126 .val_bits = 8,
127 .writeable_reg = max77802_is_accessible_reg,
128 .readable_reg = max77802_is_accessible_reg,
129 .precious_reg = max77802_is_precious_reg,
130 .volatile_reg = max77802_is_volatile_reg,
131 .name = "max77802-pmic",
132 .cache_type = REGCACHE_RBTREE,
133};
134
135static const struct regmap_irq max77686_irqs[] = {
136 /* INT1 interrupts */
137 { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, },
138 { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, },
139 { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, },
140 { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, },
141 { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, },
142 { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, },
143 { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, },
144 { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, },
145 /* INT2 interrupts */
146 { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, },
147 { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, },
148};
149
150static const struct regmap_irq_chip max77686_irq_chip = {
151 .name = "max77686-pmic",
152 .status_base = MAX77686_REG_INT1,
153 .mask_base = MAX77686_REG_INT1MSK,
154 .num_regs = 2,
155 .irqs = max77686_irqs,
156 .num_irqs = ARRAY_SIZE(max77686_irqs),
157};
158
159static const struct regmap_irq max77686_rtc_irqs[] = {
160 /* RTC interrupts */
161 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC60S_MSK, },
162 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA1_MSK, },
163 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA2_MSK, },
164 { .reg_offset = 0, .mask = MAX77686_RTCINT_SMPL_MSK, },
165 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC1S_MSK, },
166 { .reg_offset = 0, .mask = MAX77686_RTCINT_WTSR_MSK, },
167};
168
169static const struct regmap_irq_chip max77686_rtc_irq_chip = {
170 .name = "max77686-rtc",
171 .status_base = MAX77686_RTC_INT,
172 .mask_base = MAX77686_RTC_INTM,
173 .num_regs = 1,
174 .irqs = max77686_rtc_irqs,
175 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
176};
177
178static const struct regmap_irq_chip max77802_irq_chip = {
179 .name = "max77802-pmic",
180 .status_base = MAX77802_REG_INT1,
181 .mask_base = MAX77802_REG_INT1MSK,
182 .num_regs = 2,
183 .irqs = max77686_irqs, /* same masks as 77686 */
184 .num_irqs = ARRAY_SIZE(max77686_irqs),
185};
186
187static const struct regmap_irq_chip max77802_rtc_irq_chip = {
188 .name = "max77802-rtc",
189 .status_base = MAX77802_RTC_INT,
190 .mask_base = MAX77802_RTC_INTM,
191 .num_regs = 1,
192 .irqs = max77686_rtc_irqs, /* same masks as 77686 */
193 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
194};
195
50static const struct of_device_id max77686_pmic_dt_match[] = { 196static const struct of_device_id max77686_pmic_dt_match[] = {
51 {.compatible = "maxim,max77686", .data = NULL}, 197 {
52 {}, 198 .compatible = "maxim,max77686",
199 .data = (void *)TYPE_MAX77686,
200 },
201 {
202 .compatible = "maxim,max77802",
203 .data = (void *)TYPE_MAX77802,
204 },
205 { },
53}; 206};
54 207
55static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device 208static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
@@ -58,53 +211,74 @@ static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
58 struct max77686_platform_data *pd; 211 struct max77686_platform_data *pd;
59 212
60 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); 213 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
61 if (!pd) { 214 if (!pd)
62 dev_err(dev, "could not allocate memory for pdata\n");
63 return NULL; 215 return NULL;
64 }
65 216
66 dev->platform_data = pd; 217 dev->platform_data = pd;
67 return pd; 218 return pd;
68} 219}
69#else
70static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
71 *dev)
72{
73 return 0;
74}
75#endif
76 220
77static int max77686_i2c_probe(struct i2c_client *i2c, 221static int max77686_i2c_probe(struct i2c_client *i2c,
78 const struct i2c_device_id *id) 222 const struct i2c_device_id *id)
79{ 223{
80 struct max77686_dev *max77686 = NULL; 224 struct max77686_dev *max77686 = NULL;
81 struct max77686_platform_data *pdata = dev_get_platdata(&i2c->dev); 225 struct max77686_platform_data *pdata = dev_get_platdata(&i2c->dev);
226 const struct of_device_id *match;
82 unsigned int data; 227 unsigned int data;
83 int ret = 0; 228 int ret = 0;
229 const struct regmap_config *config;
230 const struct regmap_irq_chip *irq_chip;
231 const struct regmap_irq_chip *rtc_irq_chip;
232 struct regmap **rtc_regmap;
233 const struct mfd_cell *cells;
234 int n_devs;
84 235
85 if (i2c->dev.of_node) 236 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node && !pdata)
86 pdata = max77686_i2c_parse_dt_pdata(&i2c->dev); 237 pdata = max77686_i2c_parse_dt_pdata(&i2c->dev);
87 238
88 if (!pdata) { 239 if (!pdata) {
89 dev_err(&i2c->dev, "No platform data found.\n"); 240 dev_err(&i2c->dev, "No platform data found.\n");
90 return -EIO; 241 return -EINVAL;
91 } 242 }
92 243
93 max77686 = devm_kzalloc(&i2c->dev, 244 max77686 = devm_kzalloc(&i2c->dev,
94 sizeof(struct max77686_dev), GFP_KERNEL); 245 sizeof(struct max77686_dev), GFP_KERNEL);
95 if (max77686 == NULL) 246 if (!max77686)
96 return -ENOMEM; 247 return -ENOMEM;
97 248
249 if (i2c->dev.of_node) {
250 match = of_match_node(max77686_pmic_dt_match, i2c->dev.of_node);
251 if (!match)
252 return -EINVAL;
253
254 max77686->type = (unsigned long)match->data;
255 } else
256 max77686->type = id->driver_data;
257
98 i2c_set_clientdata(i2c, max77686); 258 i2c_set_clientdata(i2c, max77686);
99 max77686->dev = &i2c->dev; 259 max77686->dev = &i2c->dev;
100 max77686->i2c = i2c; 260 max77686->i2c = i2c;
101 max77686->type = id->driver_data;
102 261
103 max77686->wakeup = pdata->wakeup; 262 max77686->wakeup = pdata->wakeup;
104 max77686->irq_gpio = pdata->irq_gpio;
105 max77686->irq = i2c->irq; 263 max77686->irq = i2c->irq;
106 264
107 max77686->regmap = devm_regmap_init_i2c(i2c, &max77686_regmap_config); 265 if (max77686->type == TYPE_MAX77686) {
266 config = &max77686_regmap_config;
267 irq_chip = &max77686_irq_chip;
268 rtc_irq_chip = &max77686_rtc_irq_chip;
269 rtc_regmap = &max77686->rtc_regmap;
270 cells = max77686_devs;
271 n_devs = ARRAY_SIZE(max77686_devs);
272 } else {
273 config = &max77802_regmap_config;
274 irq_chip = &max77802_irq_chip;
275 rtc_irq_chip = &max77802_rtc_irq_chip;
276 rtc_regmap = &max77686->regmap;
277 cells = max77802_devs;
278 n_devs = ARRAY_SIZE(max77802_devs);
279 }
280
281 max77686->regmap = devm_regmap_init_i2c(i2c, config);
108 if (IS_ERR(max77686->regmap)) { 282 if (IS_ERR(max77686->regmap)) {
109 ret = PTR_ERR(max77686->regmap); 283 ret = PTR_ERR(max77686->regmap);
110 dev_err(max77686->dev, "Failed to allocate register map: %d\n", 284 dev_err(max77686->dev, "Failed to allocate register map: %d\n",
@@ -112,30 +286,68 @@ static int max77686_i2c_probe(struct i2c_client *i2c,
112 return ret; 286 return ret;
113 } 287 }
114 288
115 if (regmap_read(max77686->regmap, 289 ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data);
116 MAX77686_REG_DEVICE_ID, &data) < 0) { 290 if (ret < 0) {
117 dev_err(max77686->dev, 291 dev_err(max77686->dev,
118 "device not found on this channel (this is not an error)\n"); 292 "device not found on this channel (this is not an error)\n");
119 return -ENODEV; 293 return -ENODEV;
120 } else 294 }
121 dev_info(max77686->dev, "device found\n");
122 295
123 max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC); 296 if (max77686->type == TYPE_MAX77686) {
124 if (!max77686->rtc) { 297 max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
125 dev_err(max77686->dev, "Failed to allocate I2C device for RTC\n"); 298 if (!max77686->rtc) {
126 return -ENODEV; 299 dev_err(max77686->dev,
300 "Failed to allocate I2C device for RTC\n");
301 return -ENODEV;
302 }
303 i2c_set_clientdata(max77686->rtc, max77686);
304
305 max77686->rtc_regmap =
306 devm_regmap_init_i2c(max77686->rtc,
307 &max77686_rtc_regmap_config);
308 if (IS_ERR(max77686->rtc_regmap)) {
309 ret = PTR_ERR(max77686->rtc_regmap);
310 dev_err(max77686->dev,
311 "failed to allocate RTC regmap: %d\n",
312 ret);
313 goto err_unregister_i2c;
314 }
315 }
316
317 ret = regmap_add_irq_chip(max77686->regmap, max77686->irq,
318 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
319 IRQF_SHARED, 0, irq_chip,
320 &max77686->irq_data);
321 if (ret) {
322 dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret);
323 goto err_unregister_i2c;
127 } 324 }
128 i2c_set_clientdata(max77686->rtc, max77686);
129 325
130 max77686_irq_init(max77686); 326 ret = regmap_add_irq_chip(*rtc_regmap, max77686->irq,
327 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
328 IRQF_SHARED, 0, rtc_irq_chip,
329 &max77686->rtc_irq_data);
330 if (ret) {
331 dev_err(&i2c->dev, "failed to add RTC irq chip: %d\n", ret);
332 goto err_del_irqc;
333 }
131 334
132 ret = mfd_add_devices(max77686->dev, -1, max77686_devs, 335 ret = mfd_add_devices(max77686->dev, -1, cells, n_devs, NULL, 0, NULL);
133 ARRAY_SIZE(max77686_devs), NULL, 0, NULL);
134 if (ret < 0) { 336 if (ret < 0) {
135 mfd_remove_devices(max77686->dev); 337 dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
136 i2c_unregister_device(max77686->rtc); 338 goto err_del_rtc_irqc;
137 } 339 }
138 340
341 return 0;
342
343err_del_rtc_irqc:
344 regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
345err_del_irqc:
346 regmap_del_irq_chip(max77686->irq, max77686->irq_data);
347err_unregister_i2c:
348 if (max77686->type == TYPE_MAX77686)
349 i2c_unregister_device(max77686->rtc);
350
139 return ret; 351 return ret;
140} 352}
141 353
@@ -144,7 +356,12 @@ static int max77686_i2c_remove(struct i2c_client *i2c)
144 struct max77686_dev *max77686 = i2c_get_clientdata(i2c); 356 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
145 357
146 mfd_remove_devices(max77686->dev); 358 mfd_remove_devices(max77686->dev);
147 i2c_unregister_device(max77686->rtc); 359
360 regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
361 regmap_del_irq_chip(max77686->irq, max77686->irq_data);
362
363 if (max77686->type == TYPE_MAX77686)
364 i2c_unregister_device(max77686->rtc);
148 365
149 return 0; 366 return 0;
150} 367}
@@ -155,10 +372,50 @@ static const struct i2c_device_id max77686_i2c_id[] = {
155}; 372};
156MODULE_DEVICE_TABLE(i2c, max77686_i2c_id); 373MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
157 374
375#ifdef CONFIG_PM_SLEEP
376static int max77686_suspend(struct device *dev)
377{
378 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
379 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
380
381 if (device_may_wakeup(dev))
382 enable_irq_wake(max77686->irq);
383
384 /*
385 * IRQ must be disabled during suspend because if it happens
386 * while suspended it will be handled before resuming I2C.
387 *
388 * When device is woken up from suspend (e.g. by RTC wake alarm),
389 * an interrupt occurs before resuming I2C bus controller.
390 * Interrupt handler tries to read registers but this read
391 * will fail because I2C is still suspended.
392 */
393 disable_irq(max77686->irq);
394
395 return 0;
396}
397
398static int max77686_resume(struct device *dev)
399{
400 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
401 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
402
403 if (device_may_wakeup(dev))
404 disable_irq_wake(max77686->irq);
405
406 enable_irq(max77686->irq);
407
408 return 0;
409}
410#endif /* CONFIG_PM_SLEEP */
411
412static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume);
413
158static struct i2c_driver max77686_i2c_driver = { 414static struct i2c_driver max77686_i2c_driver = {
159 .driver = { 415 .driver = {
160 .name = "max77686", 416 .name = "max77686",
161 .owner = THIS_MODULE, 417 .owner = THIS_MODULE,
418 .pm = &max77686_pm,
162 .of_match_table = of_match_ptr(max77686_pmic_dt_match), 419 .of_match_table = of_match_ptr(max77686_pmic_dt_match),
163 }, 420 },
164 .probe = max77686_i2c_probe, 421 .probe = max77686_i2c_probe,
@@ -179,6 +436,6 @@ static void __exit max77686_i2c_exit(void)
179} 436}
180module_exit(max77686_i2c_exit); 437module_exit(max77686_i2c_exit);
181 438
182MODULE_DESCRIPTION("MAXIM 77686 multi-function core driver"); 439MODULE_DESCRIPTION("MAXIM 77686/802 multi-function core driver");
183MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>"); 440MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
184MODULE_LICENSE("GPL"); 441MODULE_LICENSE("GPL");