Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * MFD core driver for the Maxim MAX77843 |
| 3 | * |
| 4 | * Copyright (C) 2015 Samsung Electronics |
| 5 | * Author: Jaewon Kim <jaewon02.kim@samsung.com> |
| 6 | * Author: Beomho Seo <beomho.seo@samsung.com> |
| 7 | * |
| 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. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mfd/core.h> |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 20 | #include <linux/mfd/max77693-common.h> |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 21 | #include <linux/mfd/max77843-private.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | |
| 25 | static const struct mfd_cell max77843_devs[] = { |
| 26 | { |
| 27 | .name = "max77843-muic", |
| 28 | .of_compatible = "maxim,max77843-muic", |
| 29 | }, { |
| 30 | .name = "max77843-regulator", |
| 31 | .of_compatible = "maxim,max77843-regulator", |
| 32 | }, { |
| 33 | .name = "max77843-charger", |
| 34 | .of_compatible = "maxim,max77843-charger" |
| 35 | }, { |
| 36 | .name = "max77843-fuelgauge", |
| 37 | .of_compatible = "maxim,max77843-fuelgauge", |
| 38 | }, { |
| 39 | .name = "max77843-haptic", |
| 40 | .of_compatible = "maxim,max77843-haptic", |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | static const struct regmap_config max77843_charger_regmap_config = { |
| 45 | .reg_bits = 8, |
| 46 | .val_bits = 8, |
| 47 | .max_register = MAX77843_CHG_REG_END, |
| 48 | }; |
| 49 | |
| 50 | static const struct regmap_config max77843_regmap_config = { |
| 51 | .reg_bits = 8, |
| 52 | .val_bits = 8, |
| 53 | .max_register = MAX77843_SYS_REG_END, |
| 54 | }; |
| 55 | |
| 56 | static const struct regmap_irq max77843_irqs[] = { |
| 57 | /* TOPSYS interrupts */ |
| 58 | { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSUVLO_INT, }, |
| 59 | { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSOVLO_INT, }, |
| 60 | { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TSHDN_INT, }, |
| 61 | { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TM_INT, }, |
| 62 | }; |
| 63 | |
| 64 | static const struct regmap_irq_chip max77843_irq_chip = { |
| 65 | .name = "max77843", |
| 66 | .status_base = MAX77843_SYS_REG_SYSINTSRC, |
| 67 | .mask_base = MAX77843_SYS_REG_SYSINTMASK, |
| 68 | .mask_invert = false, |
| 69 | .num_regs = 1, |
| 70 | .irqs = max77843_irqs, |
| 71 | .num_irqs = ARRAY_SIZE(max77843_irqs), |
| 72 | }; |
| 73 | |
| 74 | /* Charger and Charger regulator use same regmap. */ |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 75 | static int max77843_chg_init(struct max77693_dev *max77843) |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | max77843->i2c_chg = i2c_new_dummy(max77843->i2c->adapter, I2C_ADDR_CHG); |
| 80 | if (!max77843->i2c_chg) { |
| 81 | dev_err(&max77843->i2c->dev, |
| 82 | "Cannot allocate I2C device for Charger\n"); |
Javier Martinez Canillas | 1b52e50 | 2015-09-21 14:26:54 +0200 | [diff] [blame] | 83 | return -ENODEV; |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 84 | } |
| 85 | i2c_set_clientdata(max77843->i2c_chg, max77843); |
| 86 | |
| 87 | max77843->regmap_chg = devm_regmap_init_i2c(max77843->i2c_chg, |
| 88 | &max77843_charger_regmap_config); |
| 89 | if (IS_ERR(max77843->regmap_chg)) { |
| 90 | ret = PTR_ERR(max77843->regmap_chg); |
| 91 | goto err_chg_i2c; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | |
| 96 | err_chg_i2c: |
| 97 | i2c_unregister_device(max77843->i2c_chg); |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int max77843_probe(struct i2c_client *i2c, |
| 103 | const struct i2c_device_id *id) |
| 104 | { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 105 | struct max77693_dev *max77843; |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 106 | unsigned int reg_data; |
| 107 | int ret; |
| 108 | |
| 109 | max77843 = devm_kzalloc(&i2c->dev, sizeof(*max77843), GFP_KERNEL); |
| 110 | if (!max77843) |
| 111 | return -ENOMEM; |
| 112 | |
| 113 | i2c_set_clientdata(i2c, max77843); |
| 114 | max77843->dev = &i2c->dev; |
| 115 | max77843->i2c = i2c; |
| 116 | max77843->irq = i2c->irq; |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 117 | max77843->type = id->driver_data; |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 118 | |
| 119 | max77843->regmap = devm_regmap_init_i2c(i2c, |
| 120 | &max77843_regmap_config); |
| 121 | if (IS_ERR(max77843->regmap)) { |
| 122 | dev_err(&i2c->dev, "Failed to allocate topsys register map\n"); |
| 123 | return PTR_ERR(max77843->regmap); |
| 124 | } |
| 125 | |
| 126 | ret = regmap_add_irq_chip(max77843->regmap, max77843->irq, |
| 127 | IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED, |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 128 | 0, &max77843_irq_chip, &max77843->irq_data_topsys); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 129 | if (ret) { |
| 130 | dev_err(&i2c->dev, "Failed to add TOPSYS IRQ chip\n"); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | ret = regmap_read(max77843->regmap, |
| 135 | MAX77843_SYS_REG_PMICID, ®_data); |
| 136 | if (ret < 0) { |
| 137 | dev_err(&i2c->dev, "Failed to read PMIC ID\n"); |
| 138 | goto err_pmic_id; |
| 139 | } |
| 140 | dev_info(&i2c->dev, "device ID: 0x%x\n", reg_data); |
| 141 | |
| 142 | ret = max77843_chg_init(max77843); |
| 143 | if (ret) { |
| 144 | dev_err(&i2c->dev, "Failed to init Charger\n"); |
| 145 | goto err_pmic_id; |
| 146 | } |
| 147 | |
| 148 | ret = regmap_update_bits(max77843->regmap, |
| 149 | MAX77843_SYS_REG_INTSRCMASK, |
| 150 | MAX77843_INTSRC_MASK_MASK, |
| 151 | (unsigned int)~MAX77843_INTSRC_MASK_MASK); |
| 152 | if (ret < 0) { |
| 153 | dev_err(&i2c->dev, "Failed to unmask interrupt source\n"); |
| 154 | goto err_pmic_id; |
| 155 | } |
| 156 | |
| 157 | ret = mfd_add_devices(max77843->dev, -1, max77843_devs, |
| 158 | ARRAY_SIZE(max77843_devs), NULL, 0, NULL); |
| 159 | if (ret < 0) { |
| 160 | dev_err(&i2c->dev, "Failed to add mfd device\n"); |
| 161 | goto err_pmic_id; |
| 162 | } |
| 163 | |
| 164 | device_init_wakeup(max77843->dev, true); |
| 165 | |
| 166 | return 0; |
| 167 | |
| 168 | err_pmic_id: |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 169 | regmap_del_irq_chip(max77843->irq, max77843->irq_data_topsys); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static int max77843_remove(struct i2c_client *i2c) |
| 175 | { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 176 | struct max77693_dev *max77843 = i2c_get_clientdata(i2c); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 177 | |
| 178 | mfd_remove_devices(max77843->dev); |
| 179 | |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 180 | regmap_del_irq_chip(max77843->irq, max77843->irq_data_topsys); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 181 | |
| 182 | i2c_unregister_device(max77843->i2c_chg); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static const struct of_device_id max77843_dt_match[] = { |
| 188 | { .compatible = "maxim,max77843", }, |
| 189 | { }, |
| 190 | }; |
| 191 | |
| 192 | static const struct i2c_device_id max77843_id[] = { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 193 | { "max77843", TYPE_MAX77843, }, |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 194 | { }, |
| 195 | }; |
| 196 | MODULE_DEVICE_TABLE(i2c, max77843_id); |
| 197 | |
| 198 | static int __maybe_unused max77843_suspend(struct device *dev) |
| 199 | { |
| 200 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 201 | struct max77693_dev *max77843 = i2c_get_clientdata(i2c); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 202 | |
| 203 | disable_irq(max77843->irq); |
| 204 | if (device_may_wakeup(dev)) |
| 205 | enable_irq_wake(max77843->irq); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int __maybe_unused max77843_resume(struct device *dev) |
| 211 | { |
| 212 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 213 | struct max77693_dev *max77843 = i2c_get_clientdata(i2c); |
Jaewon Kim | c7f585f | 2015-03-02 19:10:34 +0900 | [diff] [blame] | 214 | |
| 215 | if (device_may_wakeup(dev)) |
| 216 | disable_irq_wake(max77843->irq); |
| 217 | enable_irq(max77843->irq); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static SIMPLE_DEV_PM_OPS(max77843_pm, max77843_suspend, max77843_resume); |
| 223 | |
| 224 | static struct i2c_driver max77843_i2c_driver = { |
| 225 | .driver = { |
| 226 | .name = "max77843", |
| 227 | .pm = &max77843_pm, |
| 228 | .of_match_table = max77843_dt_match, |
| 229 | }, |
| 230 | .probe = max77843_probe, |
| 231 | .remove = max77843_remove, |
| 232 | .id_table = max77843_id, |
| 233 | }; |
| 234 | |
| 235 | static int __init max77843_i2c_init(void) |
| 236 | { |
| 237 | return i2c_add_driver(&max77843_i2c_driver); |
| 238 | } |
| 239 | subsys_initcall(max77843_i2c_init); |
| 240 | |
| 241 | static void __exit max77843_i2c_exit(void) |
| 242 | { |
| 243 | i2c_del_driver(&max77843_i2c_driver); |
| 244 | } |
| 245 | module_exit(max77843_i2c_exit); |