vdso: don't require 64-bit math in standalone test
[linux/fpc-iii.git] / drivers / mfd / max14577.c
blobde96b7fb1f6d8e94c95230e090a0e415e281cbaa
1 /*
2 * max14577.c - mfd core driver for the Maxim 14577/77836
4 * Copyright (C) 2014 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
6 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * This driver is based on max8997.c
21 #include <linux/err.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/of_device.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/max14577.h>
27 #include <linux/mfd/max14577-private.h>
30 * Table of valid charger currents for different Maxim chipsets.
31 * It is placed here because it is used by both charger and regulator driver.
33 const struct maxim_charger_current maxim_charger_currents[] = {
34 [MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 },
35 [MAXIM_DEVICE_TYPE_MAX14577] = {
36 .min = MAX14577_CHARGER_CURRENT_LIMIT_MIN,
37 .high_start = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START,
38 .high_step = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP,
39 .max = MAX14577_CHARGER_CURRENT_LIMIT_MAX,
41 [MAXIM_DEVICE_TYPE_MAX77836] = {
42 .min = MAX77836_CHARGER_CURRENT_LIMIT_MIN,
43 .high_start = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START,
44 .high_step = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP,
45 .max = MAX77836_CHARGER_CURRENT_LIMIT_MAX,
48 EXPORT_SYMBOL_GPL(maxim_charger_currents);
51 * maxim_charger_calc_reg_current - Calculate register value for current
52 * @limits: constraints for charger, matching the MBCICHWRC register
53 * @min_ua: minimal requested current, micro Amps
54 * @max_ua: maximum requested current, micro Amps
55 * @dst: destination to store calculated register value
57 * Calculates the value of MBCICHWRC (Fast Battery Charge Current) register
58 * for given current and stores it under pointed 'dst'. The stored value
59 * combines low bit (MBCICHWRCL) and high bits (MBCICHWRCH). It is also
60 * properly shifted.
62 * The calculated register value matches the current which:
63 * - is always between <limits.min, limits.max>;
64 * - is always less or equal to max_ua;
65 * - is the highest possible value;
66 * - may be lower than min_ua.
68 * On success returns 0. On error returns -EINVAL (requested min/max current
69 * is outside of given charger limits) and 'dst' is not set.
71 int maxim_charger_calc_reg_current(const struct maxim_charger_current *limits,
72 unsigned int min_ua, unsigned int max_ua, u8 *dst)
74 unsigned int current_bits = 0xf;
76 if (min_ua > max_ua)
77 return -EINVAL;
79 if (min_ua > limits->max || max_ua < limits->min)
80 return -EINVAL;
82 if (max_ua < limits->high_start) {
84 * Less than high_start, so set the minimal current
85 * (turn Low Bit off, 0 as high bits).
87 *dst = 0x0;
88 return 0;
91 /* max_ua is in range: <high_start, infinite>, cut it to limits.max */
92 max_ua = min(limits->max, max_ua);
93 max_ua -= limits->high_start;
95 * There is no risk of overflow 'max_ua' here because:
96 * - max_ua >= limits.high_start
97 * - BUILD_BUG checks that 'limits' are: max >= high_start + high_step
99 current_bits = max_ua / limits->high_step;
101 /* Turn Low Bit on (use range <limits.high_start, limits.max>) ... */
102 *dst = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
103 /* and set proper High Bits */
104 *dst |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
106 return 0;
108 EXPORT_SYMBOL_GPL(maxim_charger_calc_reg_current);
110 static const struct mfd_cell max14577_devs[] = {
112 .name = "max14577-muic",
113 .of_compatible = "maxim,max14577-muic",
116 .name = "max14577-regulator",
117 .of_compatible = "maxim,max14577-regulator",
120 .name = "max14577-charger",
121 .of_compatible = "maxim,max14577-charger",
125 static const struct mfd_cell max77836_devs[] = {
127 .name = "max77836-muic",
128 .of_compatible = "maxim,max77836-muic",
131 .name = "max77836-regulator",
132 .of_compatible = "maxim,max77836-regulator",
135 .name = "max77836-charger",
136 .of_compatible = "maxim,max77836-charger",
139 .name = "max77836-battery",
140 .of_compatible = "maxim,max77836-battery",
144 static const struct of_device_id max14577_dt_match[] = {
146 .compatible = "maxim,max14577",
147 .data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
150 .compatible = "maxim,max77836",
151 .data = (void *)MAXIM_DEVICE_TYPE_MAX77836,
156 static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
158 switch (reg) {
159 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
160 return true;
161 default:
162 break;
164 return false;
167 static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg)
169 /* Any max14577 volatile registers are also max77836 volatile. */
170 if (max14577_muic_volatile_reg(dev, reg))
171 return true;
173 switch (reg) {
174 case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB:
175 case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB:
176 case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L:
177 case MAX77836_PMIC_REG_INTSRC:
178 case MAX77836_PMIC_REG_TOPSYS_INT:
179 case MAX77836_PMIC_REG_TOPSYS_STAT:
180 return true;
181 default:
182 break;
184 return false;
187 static const struct regmap_config max14577_muic_regmap_config = {
188 .reg_bits = 8,
189 .val_bits = 8,
190 .volatile_reg = max14577_muic_volatile_reg,
191 .max_register = MAX14577_REG_END,
194 static const struct regmap_config max77836_pmic_regmap_config = {
195 .reg_bits = 8,
196 .val_bits = 8,
197 .volatile_reg = max77836_muic_volatile_reg,
198 .max_register = MAX77836_PMIC_REG_END,
201 static const struct regmap_irq max14577_irqs[] = {
202 /* INT1 interrupts */
203 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
204 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
205 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
206 /* INT2 interrupts */
207 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
208 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
209 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
210 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
211 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
212 /* INT3 interrupts */
213 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
214 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
215 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
216 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
219 static const struct regmap_irq_chip max14577_irq_chip = {
220 .name = "max14577",
221 .status_base = MAX14577_REG_INT1,
222 .mask_base = MAX14577_REG_INTMASK1,
223 .mask_invert = true,
224 .num_regs = 3,
225 .irqs = max14577_irqs,
226 .num_irqs = ARRAY_SIZE(max14577_irqs),
229 static const struct regmap_irq max77836_muic_irqs[] = {
230 /* INT1 interrupts */
231 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
232 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
233 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
234 { .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, },
235 /* INT2 interrupts */
236 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
237 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
238 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
239 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
240 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
241 { .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, },
242 /* INT3 interrupts */
243 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
244 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
245 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
246 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
249 static const struct regmap_irq_chip max77836_muic_irq_chip = {
250 .name = "max77836-muic",
251 .status_base = MAX14577_REG_INT1,
252 .mask_base = MAX14577_REG_INTMASK1,
253 .mask_invert = true,
254 .num_regs = 3,
255 .irqs = max77836_muic_irqs,
256 .num_irqs = ARRAY_SIZE(max77836_muic_irqs),
259 static const struct regmap_irq max77836_pmic_irqs[] = {
260 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, },
261 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, },
264 static const struct regmap_irq_chip max77836_pmic_irq_chip = {
265 .name = "max77836-pmic",
266 .status_base = MAX77836_PMIC_REG_TOPSYS_INT,
267 .mask_base = MAX77836_PMIC_REG_TOPSYS_INT_MASK,
268 .mask_invert = false,
269 .num_regs = 1,
270 .irqs = max77836_pmic_irqs,
271 .num_irqs = ARRAY_SIZE(max77836_pmic_irqs),
274 static void max14577_print_dev_type(struct max14577 *max14577)
276 u8 reg_data, vendor_id, device_id;
277 int ret;
279 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
280 &reg_data);
281 if (ret) {
282 dev_err(max14577->dev,
283 "Failed to read DEVICEID register: %d\n", ret);
284 return;
287 vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
288 DEVID_VENDORID_SHIFT);
289 device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
290 DEVID_DEVICEID_SHIFT);
292 dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
293 max14577->dev_type, device_id, vendor_id);
297 * Max77836 specific initialization code for driver probe.
298 * Adds new I2C dummy device, regmap and regmap IRQ chip.
299 * Unmasks Interrupt Source register.
301 * On success returns 0.
302 * On failure returns errno and reverts any changes done so far (e.g. remove
303 * I2C dummy device), except masking the INT SRC register.
305 static int max77836_init(struct max14577 *max14577)
307 int ret;
308 u8 intsrc_mask;
310 max14577->i2c_pmic = i2c_new_dummy(max14577->i2c->adapter,
311 I2C_ADDR_PMIC);
312 if (!max14577->i2c_pmic) {
313 dev_err(max14577->dev, "Failed to register PMIC I2C device\n");
314 return -ENODEV;
316 i2c_set_clientdata(max14577->i2c_pmic, max14577);
318 max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic,
319 &max77836_pmic_regmap_config);
320 if (IS_ERR(max14577->regmap_pmic)) {
321 ret = PTR_ERR(max14577->regmap_pmic);
322 dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n",
323 ret);
324 goto err;
327 /* Un-mask MAX77836 Interrupt Source register */
328 ret = max14577_read_reg(max14577->regmap_pmic,
329 MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask);
330 if (ret < 0) {
331 dev_err(max14577->dev, "Failed to read PMIC register\n");
332 goto err;
335 intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK);
336 intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK);
337 ret = max14577_write_reg(max14577->regmap_pmic,
338 MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask);
339 if (ret < 0) {
340 dev_err(max14577->dev, "Failed to write PMIC register\n");
341 goto err;
344 ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq,
345 IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
346 0, &max77836_pmic_irq_chip,
347 &max14577->irq_data_pmic);
348 if (ret != 0) {
349 dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n",
350 max14577->irq, ret);
351 goto err;
354 return 0;
356 err:
357 i2c_unregister_device(max14577->i2c_pmic);
359 return ret;
363 * Max77836 specific de-initialization code for driver remove.
365 static void max77836_remove(struct max14577 *max14577)
367 regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic);
368 i2c_unregister_device(max14577->i2c_pmic);
371 static int max14577_i2c_probe(struct i2c_client *i2c,
372 const struct i2c_device_id *id)
374 struct max14577 *max14577;
375 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
376 struct device_node *np = i2c->dev.of_node;
377 int ret = 0;
378 const struct regmap_irq_chip *irq_chip;
379 const struct mfd_cell *mfd_devs;
380 unsigned int mfd_devs_size;
381 int irq_flags;
383 if (np) {
384 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
385 if (!pdata)
386 return -ENOMEM;
387 i2c->dev.platform_data = pdata;
390 if (!pdata) {
391 dev_err(&i2c->dev, "No platform data found.\n");
392 return -EINVAL;
395 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
396 if (!max14577)
397 return -ENOMEM;
399 i2c_set_clientdata(i2c, max14577);
400 max14577->dev = &i2c->dev;
401 max14577->i2c = i2c;
402 max14577->irq = i2c->irq;
404 max14577->regmap = devm_regmap_init_i2c(i2c,
405 &max14577_muic_regmap_config);
406 if (IS_ERR(max14577->regmap)) {
407 ret = PTR_ERR(max14577->regmap);
408 dev_err(max14577->dev, "Failed to allocate register map: %d\n",
409 ret);
410 return ret;
413 if (np) {
414 const struct of_device_id *of_id;
416 of_id = of_match_device(max14577_dt_match, &i2c->dev);
417 if (of_id)
418 max14577->dev_type =
419 (enum maxim_device_type)of_id->data;
420 } else {
421 max14577->dev_type = id->driver_data;
424 max14577_print_dev_type(max14577);
426 switch (max14577->dev_type) {
427 case MAXIM_DEVICE_TYPE_MAX77836:
428 irq_chip = &max77836_muic_irq_chip;
429 mfd_devs = max77836_devs;
430 mfd_devs_size = ARRAY_SIZE(max77836_devs);
431 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
432 break;
433 case MAXIM_DEVICE_TYPE_MAX14577:
434 default:
435 irq_chip = &max14577_irq_chip;
436 mfd_devs = max14577_devs;
437 mfd_devs_size = ARRAY_SIZE(max14577_devs);
438 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
439 break;
442 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
443 irq_flags, 0, irq_chip,
444 &max14577->irq_data);
445 if (ret != 0) {
446 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
447 max14577->irq, ret);
448 return ret;
451 /* Max77836 specific initialization code (additional regmap) */
452 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) {
453 ret = max77836_init(max14577);
454 if (ret < 0)
455 goto err_max77836;
458 ret = mfd_add_devices(max14577->dev, -1, mfd_devs,
459 mfd_devs_size, NULL, 0, NULL);
460 if (ret < 0)
461 goto err_mfd;
463 device_init_wakeup(max14577->dev, 1);
465 return 0;
467 err_mfd:
468 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
469 max77836_remove(max14577);
470 err_max77836:
471 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
473 return ret;
476 static int max14577_i2c_remove(struct i2c_client *i2c)
478 struct max14577 *max14577 = i2c_get_clientdata(i2c);
480 mfd_remove_devices(max14577->dev);
481 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
482 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
483 max77836_remove(max14577);
485 return 0;
488 static const struct i2c_device_id max14577_i2c_id[] = {
489 { "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
490 { "max77836", MAXIM_DEVICE_TYPE_MAX77836, },
493 MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
495 #ifdef CONFIG_PM_SLEEP
496 static int max14577_suspend(struct device *dev)
498 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
499 struct max14577 *max14577 = i2c_get_clientdata(i2c);
501 if (device_may_wakeup(dev))
502 enable_irq_wake(max14577->irq);
504 * MUIC IRQ must be disabled during suspend because if it happens
505 * while suspended it will be handled before resuming I2C.
507 * When device is woken up from suspend (e.g. by ADC change),
508 * an interrupt occurs before resuming I2C bus controller.
509 * Interrupt handler tries to read registers but this read
510 * will fail because I2C is still suspended.
512 disable_irq(max14577->irq);
514 return 0;
517 static int max14577_resume(struct device *dev)
519 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
520 struct max14577 *max14577 = i2c_get_clientdata(i2c);
522 if (device_may_wakeup(dev))
523 disable_irq_wake(max14577->irq);
524 enable_irq(max14577->irq);
526 return 0;
528 #endif /* CONFIG_PM_SLEEP */
530 static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
532 static struct i2c_driver max14577_i2c_driver = {
533 .driver = {
534 .name = "max14577",
535 .owner = THIS_MODULE,
536 .pm = &max14577_pm,
537 .of_match_table = max14577_dt_match,
539 .probe = max14577_i2c_probe,
540 .remove = max14577_i2c_remove,
541 .id_table = max14577_i2c_id,
544 static int __init max14577_i2c_init(void)
546 BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
547 BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);
549 /* Valid charger current values must be provided for each chipset */
550 BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM);
552 /* Check for valid values for charger */
553 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START +
554 MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
555 MAX14577_CHARGER_CURRENT_LIMIT_MAX);
556 BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);
558 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START +
559 MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
560 MAX77836_CHARGER_CURRENT_LIMIT_MAX);
561 BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);
563 return i2c_add_driver(&max14577_i2c_driver);
565 subsys_initcall(max14577_i2c_init);
567 static void __exit max14577_i2c_exit(void)
569 i2c_del_driver(&max14577_i2c_driver);
571 module_exit(max14577_i2c_exit);
573 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
574 MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver");
575 MODULE_LICENSE("GPL");